Skip to content

Commit 323ad09

Browse files
committed
Updated REST API calls to use new combined workflow
1 parent be57631 commit 323ad09

1 file changed

Lines changed: 29 additions & 7 deletions

File tree

projects/packages/search/src/instant-search/components/search-app.jsx

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ class SearchApp extends Component {
7474
aiExtendedLoadingText: '',
7575
// Which answer the panel is currently showing
7676
aiShowExtended: false,
77+
// Session ID returned by the brief request, reused for the extended request
78+
aiSessionId: null,
7779
};
7880

7981
this.getResults = debounce( this.getResults, 200 );
@@ -268,14 +270,25 @@ class SearchApp extends Component {
268270
* Stream a single AI answer request and write results into the given state slice.
269271
*
270272
* @param {object} args - Arguments.
271-
* @param {string} args.agentId - The AI agent workflow ID to call.
272273
* @param {AbortController} args.controller - AbortController for this request.
273274
* @param {string} args.statePrefix - 'aiBrief' or 'aiExtended'.
274275
* @param {string} args.query - The search query.
275276
* @param {string} args.siteId - The site ID.
276277
* @param {object} args.options - The server options object.
278+
* @param {string} args.format - 'brief' or 'extended'.
279+
* @param {string|null} args.sessionId - Session ID from the brief request, for extended.
280+
* @param {Function|null} args.onSessionId - Callback invoked with the session ID when received.
277281
*/
278-
streamAiAnswer = ( { agentId, controller, statePrefix, query, siteId, options } ) => {
282+
streamAiAnswer = ( {
283+
controller,
284+
statePrefix,
285+
query,
286+
siteId,
287+
options,
288+
format,
289+
sessionId = null,
290+
onSessionId = null,
291+
} ) => {
279292
const keys = {
280293
status: statePrefix + 'Status',
281294
text: statePrefix + 'Text',
@@ -290,7 +303,8 @@ class SearchApp extends Component {
290303
[ keys.error ]: null,
291304
} );
292305

293-
const url = `https://public-api.wordpress.com/wpcom/v2/ai/agent/${ agentId }`;
306+
const url =
307+
'https://public-api.wordpress.com/wpcom/v2/ai/agent/jetpack-workflow-search_summarizer';
294308

295309
const HTTP_STATUS_NAMES = {
296310
400: 'Bad Request',
@@ -314,10 +328,11 @@ class SearchApp extends Component {
314328
},
315329
body: JSON.stringify( {
316330
jsonrpc: '2.0',
317-
id: `req-${ Date.now() }`,
331+
id: `req-${ format }`,
318332
method: 'message/stream',
319333
constructor_arguments: {},
320334
params: {
335+
...( sessionId ? { sessionId } : {} ),
321336
message: {
322337
role: 'user',
323338
parts: [
@@ -330,13 +345,14 @@ class SearchApp extends Component {
330345
site_url: options.homeUrl || '',
331346
filters: this.props.filters,
332347
locale: options.locale || 'en',
348+
aiAnswersFormat: format,
333349
},
334350
},
335351
metadata: {},
336352
},
337353
],
338354
kind: 'message',
339-
messageId: `msg-${ Date.now() }`,
355+
messageId: `msg-${ format }`,
340356
},
341357
},
342358
tokenStreaming: true,
@@ -355,6 +371,9 @@ class SearchApp extends Component {
355371
onmessage: event => {
356372
try {
357373
const data = JSON.parse( event.data );
374+
if ( data.result?.sessionId && onSessionId ) {
375+
onSessionId( data.result.sessionId );
376+
}
358377
if ( data.method === 'message/delta' && data.params?.delta?.deltaType === 'content' ) {
359378
// flushSync forces a synchronous render for each token so the
360379
// text streams visibly rather than batching into one update
@@ -422,6 +441,7 @@ class SearchApp extends Component {
422441
aiExtendedError: null,
423442
aiExtendedLoadingText: '',
424443
aiShowExtended: false,
444+
aiSessionId: null,
425445
};
426446

427447
if ( ! query || query.length < 3 ) {
@@ -448,12 +468,13 @@ class SearchApp extends Component {
448468
this.setState( { aiShowExtended: false } );
449469

450470
this.streamAiAnswer( {
451-
agentId: 'jetpack-workflow-search_brief_summarizer',
452471
controller: this.aiBriefController,
453472
statePrefix: 'aiBrief',
454473
query,
455474
siteId,
456475
options,
476+
format: 'brief',
477+
onSessionId: id => this.setState( { aiSessionId: id } ),
457478
} );
458479
};
459480

@@ -488,12 +509,13 @@ class SearchApp extends Component {
488509
this.aiExtendedController = new AbortController();
489510
this.setState( { aiShowExtended: true, aiExtendedLoadingText } );
490511
this.streamAiAnswer( {
491-
agentId: 'jetpack-workflow-search_summarizer',
492512
controller: this.aiExtendedController,
493513
statePrefix: 'aiExtended',
494514
query,
495515
siteId,
496516
options,
517+
format: 'extended',
518+
sessionId: this.state.aiSessionId,
497519
} );
498520
};
499521

0 commit comments

Comments
 (0)