Skip to content

Commit

Permalink
Add JavaScript that submits the form to Special:AI and shows the result
Browse files Browse the repository at this point in the history
  • Loading branch information
edwardspec committed Dec 7, 2024
1 parent dce5d2f commit 1831f9a
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 13 deletions.
3 changes: 3 additions & 0 deletions extension.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@
"targets": [
"desktop",
"mobile"
],
"messages": [
"askai-submit-failed"
]
}
},
Expand Down
2 changes: 2 additions & 0 deletions i18n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
"askai-field-pages": "List of wiki pages (and paragraphs in them) to be analyzed by the AI:",
"askai-field-prompt": "Question to ask:",
"askai-field-response": "Response from the AI:",
"askai-openai-failed": "Failed to contact OpenAI: $1",
"askai-openai-not-configured": "Error: OpenAI not configured: apiKey, apiUrl or model are not set.",
"askai-submit-failed": "HTTP error when submitting the form: $1",
"askai-unknown-service": "Not configured: incorrect value of $wgAskAIServiceClass.",
"right-askai": "Send queries to Special:AI."
}
4 changes: 3 additions & 1 deletion i18n/qqq.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
"askai-field-pages": "Label of field for the list of pages/paragraphs.",
"askai-field-prompt": "Label of field for a question to the AI.:",
"askai-field-response": "Label of readonly field that will show the response from the AI.",
"askai-openai-failed": "Error message if HTTP request to OpenAI resulted in an error.",
"askai-openai-not-configured": "Error message if OpenAI API key, etc. are not configured.",
"askai-submit-failed": "Error message if HTTP POST request to Special:AI resulted in an error.",
"askai-unknown-service": "Error message if failed to create an AI service.",
"right-askai": "{{doc-right|askai}}\nAllows to use Special:AI.",
"right-askai": "{{doc-right|askai}}\nAllows to use Special:AI."
}
2 changes: 1 addition & 1 deletion includes/Service/OpenAI.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public function query( $prompt, $instructions = '' ) {

$status = $req->execute();
if ( !$status->isOK() ) {
return "HTTP request failed: " . $status->getMessage()->plain();
return wfMessage( 'askai-openai-failed', $status->getMessage()->plain() );
}
$ret = FormatJson::decode( $req->getContent(), true );

Expand Down
23 changes: 12 additions & 11 deletions includes/SpecialAI.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,19 +44,19 @@ public function requiresWrite() {
/** @inheritDoc */
protected function getFormFields() {
return [
'pagesAndParagraphs' => [
'Pages' => [
'type' => 'textarea',
'rows' => 5,
'rows' => 3,
'label-message' => 'askai-field-pages',
'required' => true
],
'response' => [
'Response' => [
'type' => 'textarea',
'rows' => 10,
'rows' => 15,
'label-message' => 'askai-field-response',
'readonly' => true
],
'prompt' => [
'Prompt' => [
'type' => 'text',
'label-message' => 'askai-field-prompt',
'required' => true
Expand All @@ -66,6 +66,8 @@ protected function getFormFields() {

/** @inheritDoc */
protected function alterForm( HTMLForm $form ) {
$form->setId( 'mw-askai' );
$this->getOutput()->addModules( 'ext.askai' );
}

/** @inheritDoc */
Expand All @@ -75,18 +77,17 @@ public function onSubmit( array $data ) {
return Status::newFatal( 'askai-unknown-service' );
}

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

$this->getOutput()->addHTML( Xml::element( 'div', [
$this->getOutput()->disable();
echo Xml::element( 'div', [
'id' => 'mw-askai-response',
'style' => 'white-space: pre-wrap'
], $response ) );
], $response );

return Status::newGood();
}

public function onSuccess() {
}

/** @inheritDoc */
protected function getDisplayFormat() {
return 'ooui';
Expand Down
29 changes: 29 additions & 0 deletions modules/ext.askai.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/* Submits the form [[Special:AI]] and displays results without reloading the page. */

$( function () {
var $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();

$.post( url, {
wpPages: $pages.val(),
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 );
} );
}

$form.on( 'submit', onsubmit );
}() );

0 comments on commit 1831f9a

Please sign in to comment.