Skip to content

Commit 695733d

Browse files
gibrownclaude
andcommitted
Search: update SSE request to JSON-RPC 2.0 format
Switch from simple query body to JSON-RPC 2.0 message/stream format with constructor_arguments and site_url in clientContext; fix endpoint URL to remove sites/{site_id} path segment. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent 8c1cb6c commit 695733d

2 files changed

Lines changed: 34 additions & 6 deletions

File tree

projects/packages/search/docs/plans/search-ai-answers-api-design.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ Accept: text/event-stream
99
Content-Type: application/json
1010
```
1111

12+
Note: no `sites/{site_id}` in the path — the site is identified via `selectedSiteId` in the request body.
13+
1214
## Authentication
1315

1416
Two auth paths:
@@ -25,13 +27,14 @@ Rotates hourly; wpcom accepts both the current and previous hour's token to hand
2527

2628
## Request Format
2729

28-
JSON-RPC 2.0 with `message/stream` method. The search query goes in the `text` part; site context (site ID, active filters, locale) goes in the `data` part:
30+
JSON-RPC 2.0 with `message/stream` method. The search query goes in the `text` part; site context (site ID, site URL, active filters, locale) goes in the `data` part. `constructor_arguments` may be used for versioning:
2931

3032
```json
3133
{
3234
"jsonrpc": "2.0",
3335
"id": "req-1",
3436
"method": "message/stream",
37+
"constructor_arguments": {},
3538
"params": {
3639
"message": {
3740
"role": "user",
@@ -42,6 +45,7 @@ JSON-RPC 2.0 with `message/stream` method. The search query goes in the `text` p
4245
"data": {
4346
"clientContext": {
4447
"selectedSiteId": 12345,
48+
"site_url": "https://example.com",
4549
"filters": { "post_type": ["post", "page"], "category": [] },
4650
"locale": "en"
4751
}
@@ -67,4 +71,4 @@ JSON-RPC 2.0 with `message/stream` method. The search query goes in the `text` p
6771

6872
## Quota
6973

70-
500 requests per calendar month per site (all plans). Returns `error` with `code: quota_exceeded` when exceeded; overlay falls back to standard search results.
74+
500 requests per calendar month per site (all plans, subject to change — see `search-ai-answers-roadmap.md`). Returns `error` with `code: quota_exceeded` when exceeded; overlay falls back to standard search results.

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

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ class SearchApp extends Component {
270270

271271
this.setState( { aiStatus: 'loading', aiText: '', aiCitations: [] } );
272272

273-
const url = `https://public-api.wordpress.com/wpcom/v2/sites/${ siteId }/ai/agent/jetpack-search-answers`;
273+
const url = 'https://public-api.wordpress.com/wpcom/v2/ai/agent/jetpack-search-answers';
274274

275275
fetchEventSource( url, {
276276
method: 'POST',
@@ -279,9 +279,33 @@ class SearchApp extends Component {
279279
Authorization: `Bearer ${ token }`,
280280
},
281281
body: JSON.stringify( {
282-
query,
283-
filters: this.props.filters,
284-
locale: options.locale || 'en',
282+
jsonrpc: '2.0',
283+
id: `req-${ Date.now() }`,
284+
method: 'message/stream',
285+
constructor_arguments: {},
286+
params: {
287+
message: {
288+
role: 'user',
289+
parts: [
290+
{ type: 'text', text: query },
291+
{
292+
type: 'data',
293+
data: {
294+
clientContext: {
295+
selectedSiteId: siteId,
296+
site_url: options.homeUrl || '',
297+
filters: this.props.filters,
298+
locale: options.locale || 'en',
299+
},
300+
},
301+
metadata: {},
302+
},
303+
],
304+
kind: 'message',
305+
messageId: `msg-${ Date.now() }`,
306+
},
307+
},
308+
tokenStreaming: true,
285309
} ),
286310
signal: controller.signal,
287311
onopen: async response => {

0 commit comments

Comments
 (0)