Skip to content

Commit

Permalink
Download requested pages, extract requested paragraphs, send them to AI
Browse files Browse the repository at this point in the history
  • Loading branch information
edwardspec committed Dec 7, 2024
1 parent 3cd7ab1 commit 86487d8
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 10 deletions.
5 changes: 3 additions & 2 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"root": true,
"extends": [
"wikimedia/client",
"wikimedia/client-es6",
"wikimedia/jquery",
"wikimedia/mediawiki"
],
Expand All @@ -11,6 +11,7 @@
"rules": {
"no-jquery/no-event-shorthand": "off",
"no-jquery/no-global-selector": "off",
"no-console": "off"
"no-console": "off",
"compat/compat": "off"
}
}
2 changes: 1 addition & 1 deletion Gruntfile.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-env node */
module.exports = function ( grunt ) {
var conf = grunt.file.readJSON( 'extension.json' );
const conf = grunt.file.readJSON( 'extension.json' );

grunt.loadNpmTasks( 'grunt-banana-checker' );
grunt.loadNpmTasks( 'grunt-eslint' );
Expand Down
3 changes: 3 additions & 0 deletions extension.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@
],
"messages": [
"askai-submit-failed"
],
"dependencies": [
"mediawiki.Title"
]
}
},
Expand Down
5 changes: 4 additions & 1 deletion includes/SpecialAI.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ protected function getFormFields() {
'type' => 'text',
'label-message' => 'askai-field-prompt',
'required' => true
],
'Extract' => [
'type' => 'hidden'
]
];
}
Expand All @@ -77,7 +80,7 @@ public function onSubmit( array $data ) {
return Status::newFatal( 'askai-unknown-service' );
}

$response = $ai->query( $data['Prompt'], $data['Pages'] );
$response = $ai->query( $data['Prompt'], $data['Extract'] );

$this->getOutput()->disable();
echo Xml::element( 'div', [
Expand Down
89 changes: 83 additions & 6 deletions modules/ext.askai.js
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 );
}() );

0 comments on commit 86487d8

Please sign in to comment.