-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Download requested pages, extract requested paragraphs, send them to AI
- Loading branch information
1 parent
3cd7ab1
commit 86487d8
Showing
5 changed files
with
94 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -57,6 +57,9 @@ | |
], | ||
"messages": [ | ||
"askai-submit-failed" | ||
], | ||
"dependencies": [ | ||
"mediawiki.Title" | ||
] | ||
} | ||
}, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,106 @@ | ||
/* Submits the form [[Special:AI]] and displays results without reloading the page. */ | ||
|
||
$( function () { | ||
var $form = $( '#mw-askai' ), | ||
const $form = $( '#mw-askai' ), | ||
$response = $form.find( '[name="wpResponse"]' ), | ||
$pages = $form.find( '[name="wpPages"]' ), | ||
$prompt = $form.find( '[name="wpPrompt"]' ), | ||
token = $( '#wpEditToken' ).val(), | ||
url = $form[ 0 ].action; | ||
|
||
function onsubmit( ev ) { | ||
ev.preventDefault(); | ||
function extractParagraphs() { | ||
// List of pages isn't useful to the AI (it doesn't know what to do with it), | ||
// we need to retrieve the text of paragraphs (e.g. [[Some page#p6-8]]) | ||
// and send this text to AI as a part of instructions (not the user-chosen Prompt). | ||
const promises = $pages.val().split( '\n' ).map( function ( pageName ) { | ||
let title; | ||
try { | ||
title = new mw.Title( pageName ); | ||
} catch ( error ) { | ||
// Invalid title. | ||
return []; | ||
} | ||
|
||
const fragment = title.fragment, | ||
parNumbers = new Set(); | ||
|
||
if ( fragment && fragment.match( /^p[0-9\-,]+$/ ) ) { | ||
// Anchor is the list of paragraphs, e.g. "p4", or "p6-8", or "p3,5,7". | ||
fragment.slice( 1 ).split( ',' ).forEach( function ( pair ) { | ||
const range = pair.split( '-' ), | ||
start = range[ 0 ], | ||
end = range.length > 1 ? range[ 1 ] : start; | ||
|
||
for ( let idx = start; idx <= end; idx++ ) { | ||
parNumbers.add( idx ); | ||
} | ||
} ); | ||
} | ||
|
||
const $d = $.Deferred(); | ||
|
||
$.get( title.getUrl() ).done( function ( html ) { | ||
const $paragraphs = $( '<div>' ).append( html ).find( '.mw-parser-output > p' ); | ||
|
||
let extract; | ||
if ( parNumbers.size === 0 ) { | ||
// Use the entire page (no paragraph numbers were selected). | ||
extract = $paragraphs.toArray(); | ||
} else { | ||
extract = []; | ||
Array.from( parNumbers ).sort().forEach( function ( idx ) { | ||
const p = $paragraphs[ idx ]; | ||
if ( p ) { | ||
extract.push( p ); | ||
} | ||
} ); | ||
} | ||
|
||
$d.resolve( { | ||
title: title, | ||
extract: extract.map( function ( p ) { | ||
return p.innerText.trim(); | ||
} ).join( '\n\n' ) | ||
} ); | ||
} ); | ||
|
||
return $d.promise(); | ||
} ); | ||
|
||
// Accumulate the results into 1 string. | ||
return Promise.all( promises ).then( function ( pageResults ) { | ||
return pageResults.map( function ( ret, idx ) { | ||
let text = '(Source #' + ( idx + 1 ) + ') [' + | ||
ret.title.getPrefixedText(); | ||
|
||
if ( ret.title.fragment ) { | ||
text += '#' + ret.title.fragment; | ||
} | ||
|
||
text += ']\n\n' + ret.extract; | ||
return text; | ||
} ).join( '\n\n' ); | ||
} ); | ||
} | ||
|
||
function sendPrompt( extract ) { | ||
$.post( url, { | ||
wpPages: $pages.val(), | ||
wpExtract: extract, | ||
wpPrompt: $prompt.val(), | ||
wpEditToken: token | ||
} ).fail( function ( xhr ) { | ||
$response.val( mw.msg( 'askai-submit-failed', | ||
xhr.statusText + ' (' + url + ')' | ||
) ); | ||
} ).done( function ( ret ) { | ||
var responseText = $( '<div>' ).append( ret ).find( '#mw-askai-response' ).text(); | ||
$response.val( responseText ); | ||
$response.val( $( '<div>' ).append( ret ).find( '#mw-askai-response' ).text() ); | ||
} ); | ||
} | ||
|
||
function onsubmit( ev ) { | ||
ev.preventDefault(); | ||
extractParagraphs().then( sendPrompt ); | ||
} | ||
|
||
$form.on( 'submit', onsubmit ); | ||
}() ); |