From 3a954b6edeee931792be633311ceefb96117fd9e Mon Sep 17 00:00:00 2001 From: Edward Chernenko Date: Sat, 11 Jan 2025 08:07:05 +0300 Subject: [PATCH] Use new API to reduce the number in HTTP queries in "Chat with AI" --- modules/ext.askai.chatwith.js | 25 +++++-------------------- modules/ext.askai.findpar.js | 19 ++++++++----------- modules/ext.askai.search.js | 9 ++++++--- 3 files changed, 19 insertions(+), 34 deletions(-) diff --git a/modules/ext.askai.chatwith.js b/modules/ext.askai.chatwith.js index f932999..67c8a31 100644 --- a/modules/ext.askai.chatwith.js +++ b/modules/ext.askai.chatwith.js @@ -89,34 +89,19 @@ $( function () { displayProgress( pageNames.map( mw.html.escape ).join( '
' ) ); // 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 ) { diff --git a/modules/ext.askai.findpar.js b/modules/ext.askai.findpar.js index c52c92a..f7cb315 100644 --- a/modules/ext.askai.findpar.js +++ b/modules/ext.askai.findpar.js @@ -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} 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} 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(); diff --git a/modules/ext.askai.search.js b/modules/ext.askai.search.js index b4e0825..40d764d 100644 --- a/modules/ext.askai.search.js +++ b/modules/ext.askai.search.js @@ -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' ) );