-
Notifications
You must be signed in to change notification settings - Fork 66
Strip unnecessary fields in wpcom_request response #3005
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
5fccba7
9962651
c457f53
e21a84e
42ecc04
b7eca66
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,6 +3,39 @@ import wpcomFactory from '@studio/common/lib/wpcom-factory'; | |
| import wpcomXhrRequest from '@studio/common/lib/wpcom-xhr-request-factory'; | ||
| import { z } from 'zod/v4'; | ||
|
|
||
| /** | ||
| * Special case: the WP.com /sites/{id} endpoint returns a `plan` object whose | ||
| * `features` sub-field alone is 60K+ characters, which pushes the tool result past | ||
| * Claude Code's MCP output limit (~100k chars). The API doesn't support sub-field | ||
| * filtering (e.g. `fields=plan.product_slug`), so we can't solve this via query | ||
| * params. The agent only needs a few plan properties to gate features since the | ||
| * system prompt hardcodes what each plan tier can do. | ||
| * | ||
| * This is NOT a pattern to follow for other endpoints. For general large responses, | ||
| * the system prompt instructs the agent to use `_fields` (wp/v2) or `fields` (v1.1) | ||
| * query params to request only the properties it needs. | ||
| */ | ||
| function compactResponse( result: ApiResponse ): ApiResponse { | ||
|
||
| if ( | ||
| result && | ||
| typeof result === 'object' && | ||
| ! Array.isArray( result ) && | ||
| result.plan?.features | ||
| ) { | ||
| return { | ||
| ...result, | ||
| plan: { | ||
| product_id: result.plan.product_id, | ||
| product_slug: result.plan.product_slug, | ||
| product_name_short: result.plan.product_name_short, | ||
| expired: result.plan.expired, | ||
| is_free: result.plan.is_free, | ||
| }, | ||
| }; | ||
| } | ||
| return result; | ||
| } | ||
|
|
||
| function errorResult( message: string ) { | ||
| return { | ||
| content: [ { type: 'text' as const, text: message } ], | ||
|
|
@@ -106,7 +139,8 @@ export function createWpcomToolDefinitions( token: string, siteId: number ) { | |
| break; | ||
| } | ||
|
|
||
| return textResult( JSON.stringify( result, null, 2 ) ); | ||
| const compacted = compactResponse( result ); | ||
| return textResult( JSON.stringify( compacted, null, 2 ) ); | ||
|
||
| } catch ( error ) { | ||
| return errorResult( | ||
| `WP.com API request failed (${ args.method } ${ args.path }): ${ getErrorMessage( | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this accurate? I didn’t see the system prompt mention
fieldsfor v1.1. Should we add that information to the system prompt?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great catch! I have updated the system prompt as part of e21a84e as I identified that sometimes
fieldswere failing. I have added a suggestion to use both_fieldsforwp/v2orfieldsforv1.1to reduce the response size. I have tested multiple scenarios and they all work fine. I have tested:coming-soontemplate to have a light background instead of the current one?And I have not obtained the error