Skip to content

Commit

Permalink
Use new API to reduce the number in HTTP queries in "Chat with AI"
Browse files Browse the repository at this point in the history
  • Loading branch information
edwardspec committed Jan 11, 2025
1 parent 262a593 commit 3a954b6
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 34 deletions.
25 changes: 5 additions & 20 deletions modules/ext.askai.chatwith.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,34 +89,19 @@ $( function () {
displayProgress( pageNames.map( mw.html.escape ).join( '<br>' ) );

// Download each of the articles and find the paragraphs that have the snippet.
return Promise.all(
pageNames.map( ( pageName ) => {
const $todo2 = displayProgress(
mw.msg( 'askai-progress-findpar', mw.html.escape( pageName ) ) );

return mw.askai.findparInPage(
snippets[ pageName ],
pageName
).then( ( res ) => {
$todo2.append( res ? mw.msg( 'askai-progress-findpar-ok', res ) :
mw.msg( 'askai-progress-findpar-empty' ) );
return res;
} );
} )
);
} ).then( ( res ) => {
const pages = res.filter( ( x ) => x );
if ( !pages.length ) {
return mw.askai.findparInPages( snippets );
} ).then( ( foundPages ) => {
if ( !foundPages.length ) {
displayProgress( mw.msg( 'askai-progress-findpar-all-empty' ) );
return;
}

displayProgress( mw.msg( 'askai-progress-findpar-all-ok',
mw.html.escape( pages.join( ', ' ) ) ) );
mw.html.escape( foundPages.join( ', ' ) ) ) );

const specialTitle = new mw.Title( 'Special:AI' ),
url = specialTitle.getUrl( {
wpPages: pages.join( '\n' )
wpPages: foundPages.join( '\n' )
} );

if ( window.location.href.indexOf( 'redirect=no' ) === -1 ) {
Expand Down
19 changes: 8 additions & 11 deletions modules/ext.askai.findpar.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,25 @@
mw.askai = mw.askai || {};

/**
* Search pages for paragraphs that contain "textToFind", return paragraph numbers.
* Search pages for paragraphs that contain certain text, return paragraph numbers.
*
* @param {string} textToFind Arbitrary string, e.g. "Sentence is a sequence of words."
* @param {string} pageName Page in this wiki that should be searched.
* @return {Promise<string>} Resolves into "Page_title#p1-7,10-12,15" or (if failed) "".
* @param {Object} titleToSnippet Text to find in each page, e.g. { "Page 1": "Some sentence" }.
* @return {Promise<string>} Resolves into [ "Page_title#p1-7,10-12,15", ... ].
*/
mw.askai.findparInPage = function ( textToFind, pageName ) {
mw.askai.findparInPages = function ( titleToSnippet ) {
const $d = $.Deferred(),
api = new mw.Api(),
pagesToSearch = {};
api = new mw.Api();

pagesToSearch[ pageName ] = textToFind;
api.post( {
format: 'json',
formatversion: 2,
action: 'query',
prop: 'findparagraph',
fpjson: JSON.stringify( pagesToSearch )
fpjson: JSON.stringify( titleToSnippet )
} ).done( function ( ret ) {
$d.resolve( ret.query.findparagraph.found[ 0 ] || '' );
$d.resolve( ret.query.findparagraph.found );
} ).fail( function () {
$d.resolve( '' );
$d.resolve( [] );
} );

return $d.promise();
Expand Down
9 changes: 6 additions & 3 deletions modules/ext.askai.search.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,17 @@ $( function () {
);
}

// Download the article and find the paragraph(s) that contain "matchedText".
mw.askai.findparInPage( snippet, pageName ).then( ( pageAndParagraphs ) => {
// Find the paragraph(s) that contain matched text.
const snippets = {};
snippets[ pageName ] = snippet;

mw.askai.findparInPages( snippets ).then( ( pageAndParagraphs ) => {
if ( !pageAndParagraphs ) {
showAddPageLink( mw.msg( 'askai-search-add-not-found' ) );
return;
}

addedSources.push( pageAndParagraphs );
addedSources.push( ...pageAndParagraphs );
replaceWithViewLink( $loading );
} ).catch( () => {
showAddPageLink( mw.msg( 'askai-search-add-failed' ) );
Expand Down

0 comments on commit 3a954b6

Please sign in to comment.