@opengsd/gsd-pi 1.1.1-dev.75048e7 → 1.1.1-dev.9f86580
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/resources/.managed-resources-content-hash +1 -1
- package/dist/resources/extensions/browser-tools/engine/managed-gsd-browser.js +18 -2
- package/dist/resources/extensions/browser-tools/engine/selection.js +1 -1
- package/dist/resources/extensions/browser-tools/extension-manifest.json +1 -1
- package/dist/resources/extensions/browser-tools/index.js +29 -2
- package/dist/resources/extensions/browser-tools/web-app-detect.js +52 -0
- package/dist/resources/extensions/gsd/auto/phases.js +45 -3
- package/dist/resources/extensions/gsd/auto/session.js +2 -0
- package/dist/resources/extensions/gsd/auto-dispatch.js +10 -2
- package/dist/resources/extensions/gsd/auto-model-selection.js +26 -0
- package/dist/resources/extensions/gsd/auto-timers.js +24 -10
- package/dist/resources/extensions/gsd/auto.js +26 -4
- package/dist/resources/extensions/gsd/bootstrap/register-hooks.js +29 -21
- package/dist/resources/extensions/gsd/bootstrap/system-context.js +1 -1
- package/dist/resources/extensions/gsd/commands/handlers/auto.js +10 -0
- package/dist/resources/extensions/gsd/commands-mcp-status.js +1 -1
- package/dist/resources/extensions/gsd/config-overlay.js +1 -0
- package/dist/resources/extensions/gsd/context-masker.js +129 -5
- package/dist/resources/extensions/gsd/guided-flow.js +4 -1
- package/dist/resources/extensions/gsd/planner-handoff.js +98 -0
- package/dist/resources/extensions/gsd/preferences-models.js +1 -0
- package/dist/resources/extensions/gsd/prompts/plan-milestone.md +1 -1
- package/dist/resources/extensions/gsd/prompts/run-uat.md +2 -2
- package/dist/resources/extensions/gsd/prompts/system.md +1 -1
- package/dist/resources/extensions/gsd/skill-manifest.js +12 -0
- package/dist/resources/extensions/gsd/tool-contract.js +1 -1
- package/dist/resources/extensions/gsd/tool-presentation-plan.js +19 -2
- package/dist/resources/extensions/gsd/tools/complete-slice.js +28 -1
- package/dist/resources/extensions/gsd/tools/workflow-tool-executors.js +32 -4
- package/dist/resources/extensions/gsd/unit-tool-contracts.js +38 -14
- package/dist/resources/extensions/gsd/workflow-mcp.js +2 -3
- package/dist/resources/extensions/gsd/worktree-manager.js +26 -0
- package/dist/resources/extensions/gsd/worktree-reentry.js +96 -0
- package/dist/resources/extensions/shared/gsd-browser-cli.js +6 -0
- package/dist/web/standalone/.next/BUILD_ID +1 -1
- package/dist/web/standalone/.next/app-path-routes-manifest.json +8 -8
- package/dist/web/standalone/.next/build-manifest.json +2 -2
- package/dist/web/standalone/.next/prerender-manifest.json +3 -3
- package/dist/web/standalone/.next/server/app/_global-error.html +1 -1
- package/dist/web/standalone/.next/server/app/_global-error.rsc +1 -1
- package/dist/web/standalone/.next/server/app/_global-error.segments/_full.segment.rsc +1 -1
- package/dist/web/standalone/.next/server/app/_global-error.segments/_global-error/__PAGE__.segment.rsc +1 -1
- package/dist/web/standalone/.next/server/app/_global-error.segments/_global-error.segment.rsc +1 -1
- package/dist/web/standalone/.next/server/app/_global-error.segments/_head.segment.rsc +1 -1
- package/dist/web/standalone/.next/server/app/_global-error.segments/_index.segment.rsc +1 -1
- package/dist/web/standalone/.next/server/app/_global-error.segments/_tree.segment.rsc +1 -1
- package/dist/web/standalone/.next/server/app/_not-found.html +1 -1
- package/dist/web/standalone/.next/server/app/_not-found.rsc +1 -1
- package/dist/web/standalone/.next/server/app/_not-found.segments/_full.segment.rsc +1 -1
- package/dist/web/standalone/.next/server/app/_not-found.segments/_head.segment.rsc +1 -1
- package/dist/web/standalone/.next/server/app/_not-found.segments/_index.segment.rsc +1 -1
- package/dist/web/standalone/.next/server/app/_not-found.segments/_not-found/__PAGE__.segment.rsc +1 -1
- package/dist/web/standalone/.next/server/app/_not-found.segments/_not-found.segment.rsc +1 -1
- package/dist/web/standalone/.next/server/app/_not-found.segments/_tree.segment.rsc +1 -1
- package/dist/web/standalone/.next/server/app/index.html +1 -1
- package/dist/web/standalone/.next/server/app/index.rsc +1 -1
- package/dist/web/standalone/.next/server/app/index.segments/__PAGE__.segment.rsc +1 -1
- package/dist/web/standalone/.next/server/app/index.segments/_full.segment.rsc +1 -1
- package/dist/web/standalone/.next/server/app/index.segments/_head.segment.rsc +1 -1
- package/dist/web/standalone/.next/server/app/index.segments/_index.segment.rsc +1 -1
- package/dist/web/standalone/.next/server/app/index.segments/_tree.segment.rsc +1 -1
- package/dist/web/standalone/.next/server/app-paths-manifest.json +8 -8
- package/dist/web/standalone/.next/server/chunks/8357.js +1 -1
- package/dist/web/standalone/.next/server/middleware-build-manifest.js +1 -1
- package/dist/web/standalone/.next/server/pages/404.html +1 -1
- package/dist/web/standalone/.next/server/pages/500.html +1 -1
- package/dist/web/standalone/.next/server/server-reference-manifest.json +1 -1
- package/package.json +1 -1
- package/packages/cloud-mcp-gateway/package.json +2 -2
- package/packages/contracts/package.json +1 -1
- package/packages/daemon/package.json +4 -4
- package/packages/gsd-agent-core/package.json +5 -5
- package/packages/gsd-agent-modes/package.json +7 -7
- package/packages/mcp-server/package.json +3 -3
- package/packages/native/package.json +1 -1
- package/packages/pi-agent-core/package.json +1 -1
- package/packages/pi-ai/dist/models.generated.d.ts +158 -2
- package/packages/pi-ai/dist/models.generated.d.ts.map +1 -1
- package/packages/pi-ai/dist/models.generated.js +149 -9
- package/packages/pi-ai/dist/models.generated.js.map +1 -1
- package/packages/pi-ai/dist/providers/transform-messages.d.ts.map +1 -1
- package/packages/pi-ai/dist/providers/transform-messages.js +8 -1
- package/packages/pi-ai/dist/providers/transform-messages.js.map +1 -1
- package/packages/pi-ai/package.json +1 -1
- package/packages/pi-coding-agent/package.json +7 -7
- package/packages/pi-tui/package.json +1 -1
- package/packages/rpc-client/package.json +2 -2
- package/pkg/package.json +1 -1
- package/scripts/install/handoff.js +16 -3
- package/src/resources/extensions/browser-tools/engine/managed-gsd-browser.ts +21 -2
- package/src/resources/extensions/browser-tools/engine/selection.ts +1 -1
- package/src/resources/extensions/browser-tools/extension-manifest.json +1 -1
- package/src/resources/extensions/browser-tools/index.ts +36 -5
- package/src/resources/extensions/browser-tools/tests/browser-engine-selection.test.mjs +2 -2
- package/src/resources/extensions/browser-tools/tests/gsd-browser-launch-config.test.mjs +37 -0
- package/src/resources/extensions/browser-tools/tests/web-app-detect.test.mjs +68 -0
- package/src/resources/extensions/browser-tools/web-app-detect.ts +63 -0
- package/src/resources/extensions/gsd/auto/phases.ts +48 -6
- package/src/resources/extensions/gsd/auto/session.ts +2 -0
- package/src/resources/extensions/gsd/auto-dispatch.ts +34 -2
- package/src/resources/extensions/gsd/auto-model-selection.ts +26 -0
- package/src/resources/extensions/gsd/auto-timers.ts +25 -9
- package/src/resources/extensions/gsd/auto.ts +28 -4
- package/src/resources/extensions/gsd/bootstrap/register-hooks.ts +40 -21
- package/src/resources/extensions/gsd/bootstrap/system-context.ts +1 -1
- package/src/resources/extensions/gsd/commands/handlers/auto.ts +9 -0
- package/src/resources/extensions/gsd/commands-mcp-status.ts +1 -1
- package/src/resources/extensions/gsd/config-overlay.ts +1 -0
- package/src/resources/extensions/gsd/context-masker.ts +152 -5
- package/src/resources/extensions/gsd/guided-flow.ts +4 -1
- package/src/resources/extensions/gsd/planner-handoff.ts +149 -0
- package/src/resources/extensions/gsd/preferences-models.ts +1 -0
- package/src/resources/extensions/gsd/preferences-types.ts +8 -0
- package/src/resources/extensions/gsd/prompts/plan-milestone.md +1 -1
- package/src/resources/extensions/gsd/prompts/run-uat.md +2 -2
- package/src/resources/extensions/gsd/prompts/system.md +1 -1
- package/src/resources/extensions/gsd/skill-manifest.ts +12 -0
- package/src/resources/extensions/gsd/tests/auto-loop.test.ts +99 -0
- package/src/resources/extensions/gsd/tests/auto-model-selection-tool-poisoning.test.ts +66 -4
- package/src/resources/extensions/gsd/tests/auto-supervisor.test.mjs +4 -0
- package/src/resources/extensions/gsd/tests/bundled-skill-triggers.test.ts +9 -0
- package/src/resources/extensions/gsd/tests/complete-slice-verification-gate.test.ts +118 -0
- package/src/resources/extensions/gsd/tests/context-masker.test.ts +56 -1
- package/src/resources/extensions/gsd/tests/custom-engine-loop-integration.test.ts +1 -0
- package/src/resources/extensions/gsd/tests/dispatch-rule-coverage.test.ts +24 -0
- package/src/resources/extensions/gsd/tests/integration/run-uat.test.ts +1 -1
- package/src/resources/extensions/gsd/tests/interrupted-session-auto.test.ts +27 -0
- package/src/resources/extensions/gsd/tests/journal-integration.test.ts +1 -0
- package/src/resources/extensions/gsd/tests/mcp-project-config.test.ts +7 -1
- package/src/resources/extensions/gsd/tests/mcp-status.test.ts +1 -1
- package/src/resources/extensions/gsd/tests/planner-handoff.test.ts +100 -0
- package/src/resources/extensions/gsd/tests/prompt-contracts.test.ts +113 -1
- package/src/resources/extensions/gsd/tests/provider-switch-observer.test.ts +55 -0
- package/src/resources/extensions/gsd/tests/runtime-invariant-modules.test.ts +20 -0
- package/src/resources/extensions/gsd/tests/skill-manifest.test.ts +4 -3
- package/src/resources/extensions/gsd/tests/workflow-mcp.test.ts +77 -10
- package/src/resources/extensions/gsd/tests/workflow-tool-executors.test.ts +131 -2
- package/src/resources/extensions/gsd/tests/worktree-reentry.test.ts +102 -0
- package/src/resources/extensions/gsd/tool-contract.ts +1 -1
- package/src/resources/extensions/gsd/tool-presentation-plan.ts +21 -2
- package/src/resources/extensions/gsd/tools/complete-slice.ts +29 -1
- package/src/resources/extensions/gsd/tools/workflow-tool-executors.ts +46 -4
- package/src/resources/extensions/gsd/unit-tool-contracts.ts +38 -14
- package/src/resources/extensions/gsd/workflow-mcp.ts +2 -3
- package/src/resources/extensions/gsd/worktree-manager.ts +32 -0
- package/src/resources/extensions/gsd/worktree-reentry.ts +103 -0
- package/src/resources/extensions/shared/gsd-browser-cli.ts +6 -0
- /package/dist/web/standalone/.next/static/{h4TGni4xJzlZjGkxaT6uU → zzYMrKpPGfRQRxSFO32Jr}/_buildManifest.js +0 -0
- /package/dist/web/standalone/.next/static/{h4TGni4xJzlZjGkxaT6uU → zzYMrKpPGfRQRxSFO32Jr}/_ssgManifest.js +0 -0
|
@@ -1 +1 @@
|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,SAAS,CAAC,EAAE,MAAM,GAChB,OAAO,EAAE,CA2KX"}
|
|
@@ -220,7 +220,14 @@ export function transformMessagesWithReport(messages, model, normalizeToolCallId
|
|
|
220
220
|
}
|
|
221
221
|
// If the conversation ends with unresolved tool calls, synthesize results now.
|
|
222
222
|
insertSyntheticToolResults();
|
|
223
|
-
|
|
223
|
+
// Only surface a provider-switch report when the source and target APIs
|
|
224
|
+
// actually differ. Within-API transforms — most notably synthetic
|
|
225
|
+
// tool-result backfills inserted when a same-provider conversation ends on
|
|
226
|
+
// an unresolved tool call — are not cross-provider data loss and must not be
|
|
227
|
+
// reported as a "provider switch". Callers that omit `sourceApi` default
|
|
228
|
+
// `fromApi` to the target api, so without this guard every such call emits a
|
|
229
|
+
// spurious same→same report that floods telemetry and buries real switches.
|
|
230
|
+
if (hasReportChanges(report) && report.fromApi !== report.toApi) {
|
|
224
231
|
notifyProviderSwitchObserver(report);
|
|
225
232
|
}
|
|
226
233
|
return result;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"transform-messages.js","sourceRoot":"","sources":["../../src/providers/transform-messages.ts"],"names":[],"mappings":"AAuBA,IAAI,sBAA0D,CAAC;AAE/D,SAAS,eAAe,CAAC,OAAe,EAAE,KAAa;IACtD,OAAO;QACN,OAAO;QACP,KAAK;QACL,qBAAqB,EAAE,CAAC;QACxB,wBAAwB,EAAE,CAAC;QAC3B,mBAAmB,EAAE,CAAC;QACtB,4BAA4B,EAAE,CAAC;QAC/B,wBAAwB,EAAE,CAAC;KAC3B,CAAC;AACH,CAAC;AAED,SAAS,gBAAgB,CAAC,MAA4B;IACrD,OAAO,CACN,MAAM,CAAC,qBAAqB,GAAG,CAAC;QAChC,MAAM,CAAC,wBAAwB,GAAG,CAAC;QACnC,MAAM,CAAC,mBAAmB,GAAG,CAAC;QAC9B,MAAM,CAAC,4BAA4B,GAAG,CAAC;QACvC,MAAM,CAAC,wBAAwB,GAAG,CAAC,CACnC,CAAC;AACH,CAAC;A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AuB,CAAC;YAC7C,IAAI,YAAY,CAAC,UAAU,KAAK,OAAO,IAAI,YAAY,CAAC,UAAU,KAAK,SAAS,EAAE,CAAC;gBAClF,SAAS;YACV,CAAC;YAED,+CAA+C;YAC/C,MAAM,SAAS,GAAG,YAAY,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,IAAI,KAAK,UAAU,CAAe,CAAC;YAC1F,IAAI,SAAS,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;gBAC1B,gBAAgB,GAAG,SAAS,CAAC;gBAC7B,qBAAqB,GAAG,IAAI,GAAG,EAAE,CAAC;YACnC,CAAC;YAED,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;QAClB,CAAC;aAAM,IAAI,GAAG,CAAC,IAAI,KAAK,YAAY,EAAE,CAAC;YACtC,qBAAqB,CAAC,GAAG,CAAC,GAAG,CAAC,UAAU,CAAC,CAAC;YAC1C,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;QAClB,CAAC;aAAM,IAAI,GAAG,CAAC,IAAI,KAAK,MAAM,EAAE,CAAC;YAChC,kFAAkF;YAClF,0BAA0B,EAAE,CAAC;YAC7B,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;QAClB,CAAC;aAAM,CAAC;YACP,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;QAClB,CAAC;IACF,CAAC;IAED,+EAA+E;IAC/E,0BAA0B,EAAE,CAAC;IAE7B,IAAI,gBAAgB,CAAC,MAAM,CAAC,EAAE,CAAC;QAC9B,4BAA4B,CAAC,MAAM,CAAC,CAAC;IACtC,CAAC;IAED,OAAO,MAAM,CAAC;AACf,CAAC","sourcesContent":["import type {\n\tApi,\n\tAssistantMessage,\n\tImageContent,\n\tMessage,\n\tModel,\n\tTextContent,\n\tToolCall,\n\tToolResultMessage,\n} from \"../types.js\";\n\nexport interface ProviderSwitchReport {\n\tfromApi: string;\n\ttoApi: string;\n\tthinkingBlocksDropped: number;\n\tthinkingBlocksDowngraded: number;\n\ttoolCallIdsRemapped: number;\n\tsyntheticToolResultsInserted: number;\n\tthoughtSignaturesDropped: number;\n}\n\nexport type ProviderSwitchObserver = (report: ProviderSwitchReport) => void;\n\nlet providerSwitchObserver: ProviderSwitchObserver | undefined;\n\nfunction makeEmptyReport(fromApi: string, toApi: string): ProviderSwitchReport {\n\treturn {\n\t\tfromApi,\n\t\ttoApi,\n\t\tthinkingBlocksDropped: 0,\n\t\tthinkingBlocksDowngraded: 0,\n\t\ttoolCallIdsRemapped: 0,\n\t\tsyntheticToolResultsInserted: 0,\n\t\tthoughtSignaturesDropped: 0,\n\t};\n}\n\nfunction hasReportChanges(report: ProviderSwitchReport): boolean {\n\treturn (\n\t\treport.thinkingBlocksDropped > 0 ||\n\t\treport.thinkingBlocksDowngraded > 0 ||\n\t\treport.toolCallIdsRemapped > 0 ||\n\t\treport.syntheticToolResultsInserted > 0 ||\n\t\treport.thoughtSignaturesDropped > 0\n\t);\n}\n\nexport function setProviderSwitchObserver(observer: ProviderSwitchObserver | undefined): void {\n\tproviderSwitchObserver = observer;\n}\n\nexport function notifyProviderSwitchObserver(report: ProviderSwitchReport): void {\n\ttry {\n\t\tproviderSwitchObserver?.(report);\n\t} catch {\n\t\t// Observer errors must not break streaming.\n\t}\n}\n\nconst NON_VISION_USER_IMAGE_PLACEHOLDER = \"(image omitted: model does not support images)\";\nconst NON_VISION_TOOL_IMAGE_PLACEHOLDER = \"(tool image omitted: model does not support images)\";\n\nfunction replaceImagesWithPlaceholder(content: (TextContent | ImageContent)[], placeholder: string): TextContent[] {\n\tconst result: TextContent[] = [];\n\tlet previousWasPlaceholder = false;\n\n\tfor (const block of content) {\n\t\tif (block.type === \"image\") {\n\t\t\tif (!previousWasPlaceholder) {\n\t\t\t\tresult.push({ type: \"text\", text: placeholder });\n\t\t\t}\n\t\t\tpreviousWasPlaceholder = true;\n\t\t\tcontinue;\n\t\t}\n\n\t\tresult.push(block);\n\t\tpreviousWasPlaceholder = block.text === placeholder;\n\t}\n\n\treturn result;\n}\n\nfunction downgradeUnsupportedImages<TApi extends Api>(messages: Message[], model: Model<TApi>): Message[] {\n\tif (model.input.includes(\"image\")) {\n\t\treturn messages;\n\t}\n\n\treturn messages.map((msg) => {\n\t\tif (msg.role === \"user\" && Array.isArray(msg.content)) {\n\t\t\treturn {\n\t\t\t\t...msg,\n\t\t\t\tcontent: replaceImagesWithPlaceholder(msg.content, NON_VISION_USER_IMAGE_PLACEHOLDER),\n\t\t\t};\n\t\t}\n\n\t\tif (msg.role === \"toolResult\") {\n\t\t\treturn {\n\t\t\t\t...msg,\n\t\t\t\tcontent: replaceImagesWithPlaceholder(msg.content, NON_VISION_TOOL_IMAGE_PLACEHOLDER),\n\t\t\t};\n\t\t}\n\n\t\treturn msg;\n\t});\n}\n\n/**\n * Normalize tool call ID for cross-provider compatibility.\n * OpenAI Responses API generates IDs that are 450+ chars with special characters like `|`.\n * Anthropic APIs require IDs matching ^[a-zA-Z0-9_-]+$ (max 64 chars).\n */\nexport function transformMessages<TApi extends Api>(\n\tmessages: Message[],\n\tmodel: Model<TApi>,\n\tnormalizeToolCallId?: (id: string, model: Model<TApi>, source: AssistantMessage) => string,\n): Message[] {\n\treturn transformMessagesWithReport(messages, model, normalizeToolCallId);\n}\n\nexport function transformMessagesWithReport<TApi extends Api>(\n\tmessages: Message[],\n\tmodel: Model<TApi>,\n\tnormalizeToolCallId?: (id: string, model: Model<TApi>, source: AssistantMessage) => string,\n\tsourceApi?: string,\n): Message[] {\n\t// Build a map of original tool call IDs to normalized IDs\n\tconst toolCallIdMap = new Map<string, string>();\n\tconst imageAwareMessages = downgradeUnsupportedImages(messages, model);\n\tconst report = makeEmptyReport(sourceApi ?? model.api, model.api);\n\n\t// First pass: transform messages (unsupported image downgrade, thinking blocks, tool call ID normalization)\n\tconst transformed = imageAwareMessages.map((msg) => {\n\t\t// User messages pass through unchanged\n\t\tif (msg.role === \"user\") {\n\t\t\treturn msg;\n\t\t}\n\n\t\t// Handle toolResult messages - normalize toolCallId if we have a mapping\n\t\tif (msg.role === \"toolResult\") {\n\t\t\tconst normalizedId = toolCallIdMap.get(msg.toolCallId);\n\t\t\tif (normalizedId && normalizedId !== msg.toolCallId) {\n\t\t\t\treturn { ...msg, toolCallId: normalizedId };\n\t\t\t}\n\t\t\treturn msg;\n\t\t}\n\n\t\t// Assistant messages need transformation check\n\t\tif (msg.role === \"assistant\") {\n\t\t\tconst assistantMsg = msg as AssistantMessage;\n\t\t\tconst isSameModel =\n\t\t\t\tassistantMsg.provider === model.provider &&\n\t\t\t\tassistantMsg.api === model.api &&\n\t\t\t\tassistantMsg.model === model.id;\n\n\t\t\tconst transformedContent = assistantMsg.content.flatMap((block) => {\n\t\t\t\tif (block.type === \"thinking\") {\n\t\t\t\t\t// Redacted thinking is opaque encrypted content, only valid for the same model.\n\t\t\t\t\t// Drop it for cross-model to avoid API errors.\n\t\t\t\t\tif (block.redacted) {\n\t\t\t\t\t\tif (!isSameModel) {\n\t\t\t\t\t\t\treport.thinkingBlocksDropped += 1;\n\t\t\t\t\t\t}\n\t\t\t\t\t\treturn isSameModel ? block : [];\n\t\t\t\t\t}\n\t\t\t\t\t// For same model: keep thinking blocks with signatures (needed for replay)\n\t\t\t\t\t// even if the thinking text is empty (OpenAI encrypted reasoning)\n\t\t\t\t\tif (isSameModel && block.thinkingSignature) return block;\n\t\t\t\t\t// Skip empty thinking blocks, convert others to plain text\n\t\t\t\t\tif (!block.thinking || block.thinking.trim() === \"\") return [];\n\t\t\t\t\tif (isSameModel) return block;\n\t\t\t\t\treport.thinkingBlocksDowngraded += 1;\n\t\t\t\t\treturn {\n\t\t\t\t\t\ttype: \"text\" as const,\n\t\t\t\t\t\ttext: block.thinking,\n\t\t\t\t\t};\n\t\t\t\t}\n\n\t\t\t\tif (block.type === \"text\") {\n\t\t\t\t\tif (isSameModel) return block;\n\t\t\t\t\treturn {\n\t\t\t\t\t\ttype: \"text\" as const,\n\t\t\t\t\t\ttext: block.text,\n\t\t\t\t\t};\n\t\t\t\t}\n\n\t\t\t\tif (block.type === \"toolCall\") {\n\t\t\t\t\tconst toolCall = block as ToolCall;\n\t\t\t\t\tlet normalizedToolCall: ToolCall = toolCall;\n\n\t\t\t\t\tif (!isSameModel && toolCall.thoughtSignature) {\n\t\t\t\t\t\tnormalizedToolCall = { ...toolCall };\n\t\t\t\t\t\tdelete (normalizedToolCall as { thoughtSignature?: string }).thoughtSignature;\n\t\t\t\t\t\treport.thoughtSignaturesDropped += 1;\n\t\t\t\t\t}\n\n\t\t\t\t\tif (!isSameModel && normalizeToolCallId) {\n\t\t\t\t\t\tconst normalizedId = normalizeToolCallId(toolCall.id, model, assistantMsg);\n\t\t\t\t\t\tif (normalizedId !== toolCall.id) {\n\t\t\t\t\t\t\ttoolCallIdMap.set(toolCall.id, normalizedId);\n\t\t\t\t\t\t\tnormalizedToolCall = { ...normalizedToolCall, id: normalizedId };\n\t\t\t\t\t\t\treport.toolCallIdsRemapped += 1;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\n\t\t\t\t\treturn normalizedToolCall;\n\t\t\t\t}\n\n\t\t\t\treturn block;\n\t\t\t});\n\n\t\t\treturn {\n\t\t\t\t...assistantMsg,\n\t\t\t\tcontent: transformedContent,\n\t\t\t};\n\t\t}\n\t\treturn msg;\n\t});\n\n\t// Second pass: insert synthetic empty tool results for orphaned tool calls\n\t// This preserves thinking signatures and satisfies API requirements\n\tconst result: Message[] = [];\n\tlet pendingToolCalls: ToolCall[] = [];\n\tlet existingToolResultIds = new Set<string>();\n\tconst insertSyntheticToolResults = () => {\n\t\tif (pendingToolCalls.length > 0) {\n\t\t\tfor (const tc of pendingToolCalls) {\n\t\t\t\tif (!existingToolResultIds.has(tc.id)) {\n\t\t\t\t\treport.syntheticToolResultsInserted += 1;\n\t\t\t\t\tresult.push({\n\t\t\t\t\t\trole: \"toolResult\",\n\t\t\t\t\t\ttoolCallId: tc.id,\n\t\t\t\t\t\ttoolName: tc.name,\n\t\t\t\t\t\tcontent: [{ type: \"text\", text: \"No result provided\" }],\n\t\t\t\t\t\tisError: true,\n\t\t\t\t\t\ttimestamp: Date.now(),\n\t\t\t\t\t} as ToolResultMessage);\n\t\t\t\t}\n\t\t\t}\n\t\t\tpendingToolCalls = [];\n\t\t\texistingToolResultIds = new Set();\n\t\t}\n\t};\n\n\tfor (let i = 0; i < transformed.length; i++) {\n\t\tconst msg = transformed[i];\n\n\t\tif (msg.role === \"assistant\") {\n\t\t\t// If we have pending orphaned tool calls from a previous assistant, insert synthetic results now\n\t\t\tinsertSyntheticToolResults();\n\n\t\t\t// Skip errored/aborted assistant messages entirely.\n\t\t\t// These are incomplete turns that shouldn't be replayed:\n\t\t\t// - May have partial content (reasoning without message, incomplete tool calls)\n\t\t\t// - Replaying them can cause API errors (e.g., OpenAI \"reasoning without following item\")\n\t\t\t// - The model should retry from the last valid state\n\t\t\tconst assistantMsg = msg as AssistantMessage;\n\t\t\tif (assistantMsg.stopReason === \"error\" || assistantMsg.stopReason === \"aborted\") {\n\t\t\t\tcontinue;\n\t\t\t}\n\n\t\t\t// Track tool calls from this assistant message\n\t\t\tconst toolCalls = assistantMsg.content.filter((b) => b.type === \"toolCall\") as ToolCall[];\n\t\t\tif (toolCalls.length > 0) {\n\t\t\t\tpendingToolCalls = toolCalls;\n\t\t\t\texistingToolResultIds = new Set();\n\t\t\t}\n\n\t\t\tresult.push(msg);\n\t\t} else if (msg.role === \"toolResult\") {\n\t\t\texistingToolResultIds.add(msg.toolCallId);\n\t\t\tresult.push(msg);\n\t\t} else if (msg.role === \"user\") {\n\t\t\t// User message interrupts tool flow - insert synthetic results for orphaned calls\n\t\t\tinsertSyntheticToolResults();\n\t\t\tresult.push(msg);\n\t\t} else {\n\t\t\tresult.push(msg);\n\t\t}\n\t}\n\n\t// If the conversation ends with unresolved tool calls, synthesize results now.\n\tinsertSyntheticToolResults();\n\n\tif (hasReportChanges(report)) {\n\t\tnotifyProviderSwitchObserver(report);\n\t}\n\n\treturn result;\n}\n"]}
|
|
1
|
+
{"version":3,"file":"transform-messages.js","sourceRoot":"","sources":["../../src/providers/transform-messages.ts"],"names":[],"mappings":"AAuBA,IAAI,sBAA0D,CAAC;AAE/D,SAAS,eAAe,CAAC,OAAe,EAAE,KAAa;IACtD,OAAO;QACN,OAAO;QACP,KAAK;QACL,qBAAqB,EAAE,CAAC;QACxB,wBAAwB,EAAE,CAAC;QAC3B,mBAAmB,EAAE,CAAC;QACtB,4BAA4B,EAAE,CAAC;QAC/B,wBAAwB,EAAE,CAAC;KAC3B,CAAC;AACH,CAAC;AAED,SAAS,gBAAgB,CAAC,MAA4B;IACrD,OAAO,CACN,MAAM,CAAC,qBAAqB,GAAG,CAAC;QAChC,MAAM,CAAC,wBAAwB,GAAG,CAAC;QACnC,MAAM,CAAC,mBAAmB,GAAG,CAAC;QAC9B,MAAM,CAAC,4BAA4B,GAAG,CAAC;QACvC,MAAM,CAAC,wBAAwB,GAAG,CAAC,CACnC,CAAC;AACH,CAAC;AAED,MAAM,UAAU,yBAAyB,CAAC,QAA4C;IACrF,sBAAsB,GAAG,QAAQ,CAAC;AACnC,CAAC;AAED,MAAM,UAAU,4BAA4B,CAAC,MAA4B;IACxE,IAAI,CAAC;QACJ,sBAAsB,EAAE,CAAC,MAAM,CAAC,CAAC;IAClC,CAAC;IAAC,MAAM,CAAC;QACR,4CAA4C;IAC7C,CAAC;AACF,CAAC;AAED,MAAM,iCAAiC,GAAG,gDAAgD,CAAC;AAC3F,MAAM,iCAAiC,GAAG,qDAAqD,CAAC;AAEhG,SAAS,4BAA4B,CAAC,OAAuC,EAAE,WAAmB;IACjG,MAAM,MAAM,GAAkB,EAAE,CAAC;IACjC,IAAI,sBAAsB,GAAG,KAAK,CAAC;IAEnC,KAAK,MAAM,KAAK,IAAI,OAAO,EAAE,CAAC;QAC7B,IAAI,KAAK,CAAC,IAAI,KAAK,OAAO,EAAE,CAAC;YAC5B,IAAI,CAAC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UAAU,CAAC,CAAC;YAC1C,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;QAClB,CAAC;aAAM,IAAI,GAAG,CAAC,IAAI,KAAK,MAAM,EAAE,CAAC;YAChC,kFAAkF;YAClF,0BAA0B,EAAE,CAAC;YAC7B,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;QAClB,CAAC;aAAM,CAAC;YACP,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;QAClB,CAAC;IACF,CAAC;IAED,+EAA+E;IAC/E,0BAA0B,EAAE,CAAC;IAE7B,wEAAwE;IACxE,kEAAkE;IAClE,2EAA2E;IAC3E,6EAA6E;IAC7E,yEAAyE;IACzE,6EAA6E;IAC7E,4EAA4E;IAC5E,IAAI,gBAAgB,CAAC,MAAM,CAAC,IAAI,MAAM,CAAC,OAAO,KAAK,MAAM,CAAC,KAAK,EAAE,CAAC;QACjE,4BAA4B,CAAC,MAAM,CAAC,CAAC;IACtC,CAAC;IAED,OAAO,MAAM,CAAC;AACf,CAAC","sourcesContent":["import type {\n\tApi,\n\tAssistantMessage,\n\tImageContent,\n\tMessage,\n\tModel,\n\tTextContent,\n\tToolCall,\n\tToolResultMessage,\n} from \"../types.js\";\n\nexport interface ProviderSwitchReport {\n\tfromApi: string;\n\ttoApi: string;\n\tthinkingBlocksDropped: number;\n\tthinkingBlocksDowngraded: number;\n\ttoolCallIdsRemapped: number;\n\tsyntheticToolResultsInserted: number;\n\tthoughtSignaturesDropped: number;\n}\n\nexport type ProviderSwitchObserver = (report: ProviderSwitchReport) => void;\n\nlet providerSwitchObserver: ProviderSwitchObserver | undefined;\n\nfunction makeEmptyReport(fromApi: string, toApi: string): ProviderSwitchReport {\n\treturn {\n\t\tfromApi,\n\t\ttoApi,\n\t\tthinkingBlocksDropped: 0,\n\t\tthinkingBlocksDowngraded: 0,\n\t\ttoolCallIdsRemapped: 0,\n\t\tsyntheticToolResultsInserted: 0,\n\t\tthoughtSignaturesDropped: 0,\n\t};\n}\n\nfunction hasReportChanges(report: ProviderSwitchReport): boolean {\n\treturn (\n\t\treport.thinkingBlocksDropped > 0 ||\n\t\treport.thinkingBlocksDowngraded > 0 ||\n\t\treport.toolCallIdsRemapped > 0 ||\n\t\treport.syntheticToolResultsInserted > 0 ||\n\t\treport.thoughtSignaturesDropped > 0\n\t);\n}\n\nexport function setProviderSwitchObserver(observer: ProviderSwitchObserver | undefined): void {\n\tproviderSwitchObserver = observer;\n}\n\nexport function notifyProviderSwitchObserver(report: ProviderSwitchReport): void {\n\ttry {\n\t\tproviderSwitchObserver?.(report);\n\t} catch {\n\t\t// Observer errors must not break streaming.\n\t}\n}\n\nconst NON_VISION_USER_IMAGE_PLACEHOLDER = \"(image omitted: model does not support images)\";\nconst NON_VISION_TOOL_IMAGE_PLACEHOLDER = \"(tool image omitted: model does not support images)\";\n\nfunction replaceImagesWithPlaceholder(content: (TextContent | ImageContent)[], placeholder: string): TextContent[] {\n\tconst result: TextContent[] = [];\n\tlet previousWasPlaceholder = false;\n\n\tfor (const block of content) {\n\t\tif (block.type === \"image\") {\n\t\t\tif (!previousWasPlaceholder) {\n\t\t\t\tresult.push({ type: \"text\", text: placeholder });\n\t\t\t}\n\t\t\tpreviousWasPlaceholder = true;\n\t\t\tcontinue;\n\t\t}\n\n\t\tresult.push(block);\n\t\tpreviousWasPlaceholder = block.text === placeholder;\n\t}\n\n\treturn result;\n}\n\nfunction downgradeUnsupportedImages<TApi extends Api>(messages: Message[], model: Model<TApi>): Message[] {\n\tif (model.input.includes(\"image\")) {\n\t\treturn messages;\n\t}\n\n\treturn messages.map((msg) => {\n\t\tif (msg.role === \"user\" && Array.isArray(msg.content)) {\n\t\t\treturn {\n\t\t\t\t...msg,\n\t\t\t\tcontent: replaceImagesWithPlaceholder(msg.content, NON_VISION_USER_IMAGE_PLACEHOLDER),\n\t\t\t};\n\t\t}\n\n\t\tif (msg.role === \"toolResult\") {\n\t\t\treturn {\n\t\t\t\t...msg,\n\t\t\t\tcontent: replaceImagesWithPlaceholder(msg.content, NON_VISION_TOOL_IMAGE_PLACEHOLDER),\n\t\t\t};\n\t\t}\n\n\t\treturn msg;\n\t});\n}\n\n/**\n * Normalize tool call ID for cross-provider compatibility.\n * OpenAI Responses API generates IDs that are 450+ chars with special characters like `|`.\n * Anthropic APIs require IDs matching ^[a-zA-Z0-9_-]+$ (max 64 chars).\n */\nexport function transformMessages<TApi extends Api>(\n\tmessages: Message[],\n\tmodel: Model<TApi>,\n\tnormalizeToolCallId?: (id: string, model: Model<TApi>, source: AssistantMessage) => string,\n): Message[] {\n\treturn transformMessagesWithReport(messages, model, normalizeToolCallId);\n}\n\nexport function transformMessagesWithReport<TApi extends Api>(\n\tmessages: Message[],\n\tmodel: Model<TApi>,\n\tnormalizeToolCallId?: (id: string, model: Model<TApi>, source: AssistantMessage) => string,\n\tsourceApi?: string,\n): Message[] {\n\t// Build a map of original tool call IDs to normalized IDs\n\tconst toolCallIdMap = new Map<string, string>();\n\tconst imageAwareMessages = downgradeUnsupportedImages(messages, model);\n\tconst report = makeEmptyReport(sourceApi ?? model.api, model.api);\n\n\t// First pass: transform messages (unsupported image downgrade, thinking blocks, tool call ID normalization)\n\tconst transformed = imageAwareMessages.map((msg) => {\n\t\t// User messages pass through unchanged\n\t\tif (msg.role === \"user\") {\n\t\t\treturn msg;\n\t\t}\n\n\t\t// Handle toolResult messages - normalize toolCallId if we have a mapping\n\t\tif (msg.role === \"toolResult\") {\n\t\t\tconst normalizedId = toolCallIdMap.get(msg.toolCallId);\n\t\t\tif (normalizedId && normalizedId !== msg.toolCallId) {\n\t\t\t\treturn { ...msg, toolCallId: normalizedId };\n\t\t\t}\n\t\t\treturn msg;\n\t\t}\n\n\t\t// Assistant messages need transformation check\n\t\tif (msg.role === \"assistant\") {\n\t\t\tconst assistantMsg = msg as AssistantMessage;\n\t\t\tconst isSameModel =\n\t\t\t\tassistantMsg.provider === model.provider &&\n\t\t\t\tassistantMsg.api === model.api &&\n\t\t\t\tassistantMsg.model === model.id;\n\n\t\t\tconst transformedContent = assistantMsg.content.flatMap((block) => {\n\t\t\t\tif (block.type === \"thinking\") {\n\t\t\t\t\t// Redacted thinking is opaque encrypted content, only valid for the same model.\n\t\t\t\t\t// Drop it for cross-model to avoid API errors.\n\t\t\t\t\tif (block.redacted) {\n\t\t\t\t\t\tif (!isSameModel) {\n\t\t\t\t\t\t\treport.thinkingBlocksDropped += 1;\n\t\t\t\t\t\t}\n\t\t\t\t\t\treturn isSameModel ? block : [];\n\t\t\t\t\t}\n\t\t\t\t\t// For same model: keep thinking blocks with signatures (needed for replay)\n\t\t\t\t\t// even if the thinking text is empty (OpenAI encrypted reasoning)\n\t\t\t\t\tif (isSameModel && block.thinkingSignature) return block;\n\t\t\t\t\t// Skip empty thinking blocks, convert others to plain text\n\t\t\t\t\tif (!block.thinking || block.thinking.trim() === \"\") return [];\n\t\t\t\t\tif (isSameModel) return block;\n\t\t\t\t\treport.thinkingBlocksDowngraded += 1;\n\t\t\t\t\treturn {\n\t\t\t\t\t\ttype: \"text\" as const,\n\t\t\t\t\t\ttext: block.thinking,\n\t\t\t\t\t};\n\t\t\t\t}\n\n\t\t\t\tif (block.type === \"text\") {\n\t\t\t\t\tif (isSameModel) return block;\n\t\t\t\t\treturn {\n\t\t\t\t\t\ttype: \"text\" as const,\n\t\t\t\t\t\ttext: block.text,\n\t\t\t\t\t};\n\t\t\t\t}\n\n\t\t\t\tif (block.type === \"toolCall\") {\n\t\t\t\t\tconst toolCall = block as ToolCall;\n\t\t\t\t\tlet normalizedToolCall: ToolCall = toolCall;\n\n\t\t\t\t\tif (!isSameModel && toolCall.thoughtSignature) {\n\t\t\t\t\t\tnormalizedToolCall = { ...toolCall };\n\t\t\t\t\t\tdelete (normalizedToolCall as { thoughtSignature?: string }).thoughtSignature;\n\t\t\t\t\t\treport.thoughtSignaturesDropped += 1;\n\t\t\t\t\t}\n\n\t\t\t\t\tif (!isSameModel && normalizeToolCallId) {\n\t\t\t\t\t\tconst normalizedId = normalizeToolCallId(toolCall.id, model, assistantMsg);\n\t\t\t\t\t\tif (normalizedId !== toolCall.id) {\n\t\t\t\t\t\t\ttoolCallIdMap.set(toolCall.id, normalizedId);\n\t\t\t\t\t\t\tnormalizedToolCall = { ...normalizedToolCall, id: normalizedId };\n\t\t\t\t\t\t\treport.toolCallIdsRemapped += 1;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\n\t\t\t\t\treturn normalizedToolCall;\n\t\t\t\t}\n\n\t\t\t\treturn block;\n\t\t\t});\n\n\t\t\treturn {\n\t\t\t\t...assistantMsg,\n\t\t\t\tcontent: transformedContent,\n\t\t\t};\n\t\t}\n\t\treturn msg;\n\t});\n\n\t// Second pass: insert synthetic empty tool results for orphaned tool calls\n\t// This preserves thinking signatures and satisfies API requirements\n\tconst result: Message[] = [];\n\tlet pendingToolCalls: ToolCall[] = [];\n\tlet existingToolResultIds = new Set<string>();\n\tconst insertSyntheticToolResults = () => {\n\t\tif (pendingToolCalls.length > 0) {\n\t\t\tfor (const tc of pendingToolCalls) {\n\t\t\t\tif (!existingToolResultIds.has(tc.id)) {\n\t\t\t\t\treport.syntheticToolResultsInserted += 1;\n\t\t\t\t\tresult.push({\n\t\t\t\t\t\trole: \"toolResult\",\n\t\t\t\t\t\ttoolCallId: tc.id,\n\t\t\t\t\t\ttoolName: tc.name,\n\t\t\t\t\t\tcontent: [{ type: \"text\", text: \"No result provided\" }],\n\t\t\t\t\t\tisError: true,\n\t\t\t\t\t\ttimestamp: Date.now(),\n\t\t\t\t\t} as ToolResultMessage);\n\t\t\t\t}\n\t\t\t}\n\t\t\tpendingToolCalls = [];\n\t\t\texistingToolResultIds = new Set();\n\t\t}\n\t};\n\n\tfor (let i = 0; i < transformed.length; i++) {\n\t\tconst msg = transformed[i];\n\n\t\tif (msg.role === \"assistant\") {\n\t\t\t// If we have pending orphaned tool calls from a previous assistant, insert synthetic results now\n\t\t\tinsertSyntheticToolResults();\n\n\t\t\t// Skip errored/aborted assistant messages entirely.\n\t\t\t// These are incomplete turns that shouldn't be replayed:\n\t\t\t// - May have partial content (reasoning without message, incomplete tool calls)\n\t\t\t// - Replaying them can cause API errors (e.g., OpenAI \"reasoning without following item\")\n\t\t\t// - The model should retry from the last valid state\n\t\t\tconst assistantMsg = msg as AssistantMessage;\n\t\t\tif (assistantMsg.stopReason === \"error\" || assistantMsg.stopReason === \"aborted\") {\n\t\t\t\tcontinue;\n\t\t\t}\n\n\t\t\t// Track tool calls from this assistant message\n\t\t\tconst toolCalls = assistantMsg.content.filter((b) => b.type === \"toolCall\") as ToolCall[];\n\t\t\tif (toolCalls.length > 0) {\n\t\t\t\tpendingToolCalls = toolCalls;\n\t\t\t\texistingToolResultIds = new Set();\n\t\t\t}\n\n\t\t\tresult.push(msg);\n\t\t} else if (msg.role === \"toolResult\") {\n\t\t\texistingToolResultIds.add(msg.toolCallId);\n\t\t\tresult.push(msg);\n\t\t} else if (msg.role === \"user\") {\n\t\t\t// User message interrupts tool flow - insert synthetic results for orphaned calls\n\t\t\tinsertSyntheticToolResults();\n\t\t\tresult.push(msg);\n\t\t} else {\n\t\t\tresult.push(msg);\n\t\t}\n\t}\n\n\t// If the conversation ends with unresolved tool calls, synthesize results now.\n\tinsertSyntheticToolResults();\n\n\t// Only surface a provider-switch report when the source and target APIs\n\t// actually differ. Within-API transforms — most notably synthetic\n\t// tool-result backfills inserted when a same-provider conversation ends on\n\t// an unresolved tool call — are not cross-provider data loss and must not be\n\t// reported as a \"provider switch\". Callers that omit `sourceApi` default\n\t// `fromApi` to the target api, so without this guard every such call emits a\n\t// spurious same→same report that floods telemetry and buries real switches.\n\tif (hasReportChanges(report) && report.fromApi !== report.toApi) {\n\t\tnotifyProviderSwitchObserver(report);\n\t}\n\n\treturn result;\n}\n"]}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@gsd/pi-coding-agent",
|
|
3
|
-
"version": "1.1.1-dev.
|
|
3
|
+
"version": "1.1.1-dev.9f86580",
|
|
4
4
|
"description": "Coding agent CLI (vendored from earendil-works/pi)",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"gsd": {
|
|
@@ -33,7 +33,7 @@
|
|
|
33
33
|
"copy-assets": "node scripts/copy-assets.cjs"
|
|
34
34
|
},
|
|
35
35
|
"dependencies": {
|
|
36
|
-
"@opengsd/contracts": "^1.1.1-dev.
|
|
36
|
+
"@opengsd/contracts": "^1.1.1-dev.9f86580",
|
|
37
37
|
"@mariozechner/jiti": "^2.6.2",
|
|
38
38
|
"@silvia-odwyer/photon-node": "0.3.4",
|
|
39
39
|
"chalk": "5.6.2",
|
|
@@ -53,11 +53,11 @@
|
|
|
53
53
|
"typebox": "1.1.38",
|
|
54
54
|
"undici": "7.26.0",
|
|
55
55
|
"yaml": "2.9.0",
|
|
56
|
-
"@gsd/agent-core": "^1.1.1-dev.
|
|
57
|
-
"@gsd/native": "^1.1.1-dev.
|
|
58
|
-
"@gsd/pi-agent-core": "^1.1.1-dev.
|
|
59
|
-
"@gsd/pi-ai": "^1.1.1-dev.
|
|
60
|
-
"@gsd/pi-tui": "^1.1.1-dev.
|
|
56
|
+
"@gsd/agent-core": "^1.1.1-dev.9f86580",
|
|
57
|
+
"@gsd/native": "^1.1.1-dev.9f86580",
|
|
58
|
+
"@gsd/pi-agent-core": "^1.1.1-dev.9f86580",
|
|
59
|
+
"@gsd/pi-ai": "^1.1.1-dev.9f86580",
|
|
60
|
+
"@gsd/pi-tui": "^1.1.1-dev.9f86580",
|
|
61
61
|
"@sinclair/typebox": "^0.34.41"
|
|
62
62
|
},
|
|
63
63
|
"devDependencies": {
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@opengsd/rpc-client",
|
|
3
|
-
"version": "1.1.1-dev.
|
|
3
|
+
"version": "1.1.1-dev.9f86580",
|
|
4
4
|
"description": "Standalone RPC client SDK for GSD — zero internal dependencies",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"gsd": {
|
|
@@ -34,7 +34,7 @@
|
|
|
34
34
|
"test": "node --test dist/rpc-client.test.js"
|
|
35
35
|
},
|
|
36
36
|
"dependencies": {
|
|
37
|
-
"@opengsd/contracts": "^1.1.1-dev.
|
|
37
|
+
"@opengsd/contracts": "^1.1.1-dev.9f86580"
|
|
38
38
|
},
|
|
39
39
|
"engines": {
|
|
40
40
|
"node": ">=22.0.0"
|
package/pkg/package.json
CHANGED
|
@@ -22,10 +22,21 @@ export function resolveGsdBin({ isLocal, cwd = process.cwd() }) {
|
|
|
22
22
|
return join(binDir, 'gsd')
|
|
23
23
|
}
|
|
24
24
|
|
|
25
|
+
// On Windows, .cmd shims cannot be executed directly by spawnSync without a
|
|
26
|
+
// shell. Use `cmd /c <bin> <args>` to avoid both ENOENT failures and the
|
|
27
|
+
// Node 22 DEP0190 deprecation triggered by `shell: true` with args.
|
|
28
|
+
function buildSpawnInvocation(bin, args) {
|
|
29
|
+
if (process.platform === 'win32') {
|
|
30
|
+
return { cmd: 'cmd', args: ['/c', bin, ...args] }
|
|
31
|
+
}
|
|
32
|
+
return { cmd: bin, args }
|
|
33
|
+
}
|
|
34
|
+
|
|
25
35
|
export function runConfigHandoff({ bin, nonInteractive }) {
|
|
26
36
|
if (nonInteractive) return { skipped: true }
|
|
27
37
|
|
|
28
|
-
const
|
|
38
|
+
const inv = buildSpawnInvocation(bin, ['config'])
|
|
39
|
+
const result = spawnSync(inv.cmd, inv.args, {
|
|
29
40
|
stdio: 'inherit',
|
|
30
41
|
timeout: 600_000,
|
|
31
42
|
env: { ...process.env, [GSD_SUPPRESS_LOGO_ENV]: '1' },
|
|
@@ -52,7 +63,8 @@ export async function promptLaunch({ bin, clack: p, nonInteractive }) {
|
|
|
52
63
|
|
|
53
64
|
if (p.isCancel(launch) || !launch) return false
|
|
54
65
|
|
|
55
|
-
const
|
|
66
|
+
const inv = buildSpawnInvocation(bin, [])
|
|
67
|
+
const result = spawnSync(inv.cmd, inv.args, {
|
|
56
68
|
stdio: 'inherit',
|
|
57
69
|
})
|
|
58
70
|
|
|
@@ -64,7 +76,8 @@ export async function promptLaunch({ bin, clack: p, nonInteractive }) {
|
|
|
64
76
|
}
|
|
65
77
|
|
|
66
78
|
export function verifyInstall(bin) {
|
|
67
|
-
const
|
|
79
|
+
const inv = buildSpawnInvocation(bin, ['--version'])
|
|
80
|
+
const result = spawnSync(inv.cmd, inv.args, {
|
|
68
81
|
encoding: 'utf-8',
|
|
69
82
|
stdio: ['ignore', 'pipe', 'pipe'],
|
|
70
83
|
timeout: 10_000,
|
|
@@ -514,12 +514,31 @@ function formatManagedBrowserError(toolName: string, error: unknown): string {
|
|
|
514
514
|
return [
|
|
515
515
|
`gsd-browser engine or tool unavailable for ${toolName}: ${message}`,
|
|
516
516
|
"",
|
|
517
|
-
"
|
|
517
|
+
"The managed gsd-browser engine is enabled for this session but is unavailable.",
|
|
518
518
|
"Run /gsd doctor or reinstall dependencies so @opengsd/gsd-browser is available.",
|
|
519
|
-
"
|
|
519
|
+
"Unset GSD_BROWSER_ENGINE or set GSD_BROWSER_ENGINE=playwright to use the default Playwright engine.",
|
|
520
520
|
].join("\n");
|
|
521
521
|
}
|
|
522
522
|
|
|
523
|
+
/**
|
|
524
|
+
* Eagerly establish the managed gsd-browser connection so browser tools are
|
|
525
|
+
* ready before first use. Best-effort: returns the error instead of throwing so
|
|
526
|
+
* callers (e.g. session-start warm-up) can surface a warning without failing the
|
|
527
|
+
* session. Connecting only spawns the gsd-browser MCP daemon; it does not launch
|
|
528
|
+
* Chrome (that happens lazily on the first navigation).
|
|
529
|
+
*/
|
|
530
|
+
export async function warmUpManagedGsdBrowser(
|
|
531
|
+
ctx?: ExtensionContext,
|
|
532
|
+
signal?: AbortSignal,
|
|
533
|
+
): Promise<{ ok: true } | { ok: false; error: string }> {
|
|
534
|
+
try {
|
|
535
|
+
await getOrConnectManagedGsdBrowser(ctx, signal);
|
|
536
|
+
return { ok: true };
|
|
537
|
+
} catch (error) {
|
|
538
|
+
return { ok: false, error: error instanceof Error ? error.message : String(error) };
|
|
539
|
+
}
|
|
540
|
+
}
|
|
541
|
+
|
|
523
542
|
export function registerManagedGsdBrowserTools(pi: ExtensionAPI): void {
|
|
524
543
|
for (const tool of MANAGED_BROWSER_TOOLS) {
|
|
525
544
|
pi.registerTool({
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
export type BrowserEngineMode = "gsd-browser" | "legacy" | "off";
|
|
2
2
|
|
|
3
|
-
const DEFAULT_BROWSER_ENGINE: BrowserEngineMode = "
|
|
3
|
+
const DEFAULT_BROWSER_ENGINE: BrowserEngineMode = "legacy";
|
|
4
4
|
|
|
5
5
|
export function resolveBrowserEngineMode(env: NodeJS.ProcessEnv = process.env): BrowserEngineMode {
|
|
6
6
|
const raw = env.GSD_BROWSER_ENGINE?.trim();
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"id": "browser-tools",
|
|
3
3
|
"name": "Browser Tools",
|
|
4
4
|
"version": "1.0.0",
|
|
5
|
-
"description": "GSD browser automation contract adapter backed by
|
|
5
|
+
"description": "GSD browser automation contract adapter backed by Playwright with optional managed gsd-browser support",
|
|
6
6
|
"tier": "bundled",
|
|
7
7
|
"requires": { "platform": ">=2.29.0" },
|
|
8
8
|
"provides": {
|
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
/** browser-tools — Pi Browser Automation Contract adapter. */
|
|
2
|
-
import { importExtensionModule, type ExtensionAPI } from "@gsd/pi-coding-agent";
|
|
2
|
+
import { importExtensionModule, type ExtensionAPI, type ExtensionContext } from "@gsd/pi-coding-agent";
|
|
3
3
|
|
|
4
|
-
import { closeManagedGsdBrowser, registerManagedGsdBrowserTools } from "./engine/managed-gsd-browser.js";
|
|
4
|
+
import { closeManagedGsdBrowser, registerManagedGsdBrowserTools, warmUpManagedGsdBrowser } from "./engine/managed-gsd-browser.js";
|
|
5
5
|
import { resolveBrowserEngineMode, type BrowserEngineMode } from "./engine/selection.js";
|
|
6
|
+
import { detectWebApp } from "./web-app-detect.js";
|
|
6
7
|
|
|
7
8
|
let legacyRegistrationPromise: Promise<void> | null = null;
|
|
8
9
|
let managedRegistrationPromise: Promise<void> | null = null;
|
|
@@ -184,6 +185,33 @@ async function registerBrowserTools(pi: ExtensionAPI): Promise<void> {
|
|
|
184
185
|
}
|
|
185
186
|
}
|
|
186
187
|
|
|
188
|
+
function isWarmUpDisabled(): boolean {
|
|
189
|
+
const value = process.env.GSD_BROWSER_WARMUP?.trim().toLowerCase();
|
|
190
|
+
return value === "0" || value === "false" || value === "off";
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/**
|
|
194
|
+
* Auto-initialize the managed gsd-browser engine only when explicitly selected
|
|
195
|
+
* for a web app. Best-effort and non-blocking: warm-up runs in the background
|
|
196
|
+
* and only surfaces a warning if it fails.
|
|
197
|
+
*/
|
|
198
|
+
function maybeWarmUpManagedEngine(pi: ExtensionAPI, ctx: ExtensionContext): void {
|
|
199
|
+
if (isWarmUpDisabled()) return;
|
|
200
|
+
if (resolveBrowserEngineMode() !== "gsd-browser") return;
|
|
201
|
+
|
|
202
|
+
const projectRoot = ctx.cwd || process.cwd();
|
|
203
|
+
if (!detectWebApp(projectRoot)) return;
|
|
204
|
+
|
|
205
|
+
void warmUpManagedGsdBrowser(ctx).then((result) => {
|
|
206
|
+
if (!result.ok && ctx.hasUI) {
|
|
207
|
+
ctx.ui.notify(
|
|
208
|
+
`gsd-browser auto-init failed: ${result.error}. Browser UAT tools will retry on first use; run /gsd doctor if this persists.`,
|
|
209
|
+
"warning",
|
|
210
|
+
);
|
|
211
|
+
}
|
|
212
|
+
});
|
|
213
|
+
}
|
|
214
|
+
|
|
187
215
|
async function closeActiveBrowserEngines(): Promise<void> {
|
|
188
216
|
await closeManagedGsdBrowser();
|
|
189
217
|
if (legacyRegistrationPromise) {
|
|
@@ -195,13 +223,16 @@ async function closeActiveBrowserEngines(): Promise<void> {
|
|
|
195
223
|
export default function (pi: ExtensionAPI) {
|
|
196
224
|
pi.on("session_start", async (_event, ctx) => {
|
|
197
225
|
if (ctx.hasUI) {
|
|
198
|
-
void registerBrowserTools(pi)
|
|
199
|
-
|
|
200
|
-
|
|
226
|
+
void registerBrowserTools(pi)
|
|
227
|
+
.then(() => maybeWarmUpManagedEngine(pi, ctx))
|
|
228
|
+
.catch((error) => {
|
|
229
|
+
ctx.ui.notify(`browser-tools failed to load: ${error instanceof Error ? error.message : String(error)}`, "warning");
|
|
230
|
+
});
|
|
201
231
|
return;
|
|
202
232
|
}
|
|
203
233
|
|
|
204
234
|
await registerBrowserTools(pi);
|
|
235
|
+
maybeWarmUpManagedEngine(pi, ctx);
|
|
205
236
|
});
|
|
206
237
|
|
|
207
238
|
pi.on("session_shutdown", async () => {
|
|
@@ -11,8 +11,8 @@ const jiti = require("jiti")(__dirname, { interopDefault: true, debug: false });
|
|
|
11
11
|
const { resolveBrowserEngineMode } = jiti("../engine/selection.ts");
|
|
12
12
|
|
|
13
13
|
describe("resolveBrowserEngineMode", () => {
|
|
14
|
-
it("defaults to
|
|
15
|
-
assert.equal(resolveBrowserEngineMode({}), "
|
|
14
|
+
it("defaults to the Playwright engine", () => {
|
|
15
|
+
assert.equal(resolveBrowserEngineMode({}), "legacy");
|
|
16
16
|
});
|
|
17
17
|
|
|
18
18
|
it("accepts the explicit engine modes", () => {
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { describe, it } from "node:test";
|
|
2
|
+
import assert from "node:assert/strict";
|
|
3
|
+
|
|
4
|
+
const { resolveGsdBrowserMcpLaunchConfig } = await import("../../shared/gsd-browser-cli.ts");
|
|
5
|
+
|
|
6
|
+
describe("resolveGsdBrowserMcpLaunchConfig identity flags", () => {
|
|
7
|
+
it("emits a non-empty --identity-key alongside --identity-scope", () => {
|
|
8
|
+
// Regression: gsd-browser exits immediately ("Connection closed") when
|
|
9
|
+
// --identity-scope is supplied without --identity-key.
|
|
10
|
+
const { args } = resolveGsdBrowserMcpLaunchConfig("/tmp/example-project", {});
|
|
11
|
+
|
|
12
|
+
const scopeIndex = args.indexOf("--identity-scope");
|
|
13
|
+
const keyIndex = args.indexOf("--identity-key");
|
|
14
|
+
|
|
15
|
+
assert.ok(scopeIndex >= 0, "expected --identity-scope in args");
|
|
16
|
+
assert.ok(keyIndex >= 0, "expected --identity-key in args");
|
|
17
|
+
assert.equal(args[keyIndex + 1] && args[keyIndex + 1].length > 0, true, "identity-key must be non-empty");
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
it("keeps the identity-key stable across sessions for the same project", () => {
|
|
21
|
+
const a = resolveGsdBrowserMcpLaunchConfig("/tmp/example-project", {}, { sessionSuffix: "pi-aaa" });
|
|
22
|
+
const b = resolveGsdBrowserMcpLaunchConfig("/tmp/example-project", {}, { sessionSuffix: "pi-bbb" });
|
|
23
|
+
|
|
24
|
+
const keyOf = (cfg) => cfg.args[cfg.args.indexOf("--identity-key") + 1];
|
|
25
|
+
// Session names differ per pi process, but the persistent browser identity
|
|
26
|
+
// must not, so cookies/profile survive across sessions.
|
|
27
|
+
assert.notEqual(a.sessionName, b.sessionName);
|
|
28
|
+
assert.equal(keyOf(a), keyOf(b));
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
it("honors GSD_BROWSER_IDENTITY_KEY override", () => {
|
|
32
|
+
const { args } = resolveGsdBrowserMcpLaunchConfig("/tmp/example-project", {
|
|
33
|
+
GSD_BROWSER_IDENTITY_KEY: "custom-key",
|
|
34
|
+
});
|
|
35
|
+
assert.equal(args[args.indexOf("--identity-key") + 1], "custom-key");
|
|
36
|
+
});
|
|
37
|
+
});
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { describe, it, before, after } from "node:test";
|
|
2
|
+
import assert from "node:assert/strict";
|
|
3
|
+
import { mkdtempSync, mkdirSync, rmSync, writeFileSync } from "node:fs";
|
|
4
|
+
import { join } from "node:path";
|
|
5
|
+
import { tmpdir } from "node:os";
|
|
6
|
+
|
|
7
|
+
const { detectWebApp } = await import("../web-app-detect.ts");
|
|
8
|
+
|
|
9
|
+
function makeProject(files) {
|
|
10
|
+
const root = mkdtempSync(join(tmpdir(), "gsd-webapp-detect-"));
|
|
11
|
+
for (const [relPath, contents] of Object.entries(files)) {
|
|
12
|
+
const full = join(root, relPath);
|
|
13
|
+
mkdirSync(join(full, ".."), { recursive: true });
|
|
14
|
+
writeFileSync(full, contents);
|
|
15
|
+
}
|
|
16
|
+
return root;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
describe("detectWebApp", () => {
|
|
20
|
+
const roots = [];
|
|
21
|
+
after(() => roots.forEach((root) => rmSync(root, { recursive: true, force: true })));
|
|
22
|
+
|
|
23
|
+
const project = (files) => {
|
|
24
|
+
const root = makeProject(files);
|
|
25
|
+
roots.push(root);
|
|
26
|
+
return root;
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
it("detects a React dependency", () => {
|
|
30
|
+
const root = project({ "package.json": JSON.stringify({ dependencies: { react: "^18.0.0" } }) });
|
|
31
|
+
assert.equal(detectWebApp(root), true);
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
it("detects a Vite/Next dev dependency", () => {
|
|
35
|
+
const root = project({ "package.json": JSON.stringify({ devDependencies: { vite: "^5.0.0" } }) });
|
|
36
|
+
assert.equal(detectWebApp(root), true);
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
it("detects a dev-server script", () => {
|
|
40
|
+
const root = project({ "package.json": JSON.stringify({ scripts: { dev: "next dev" } }) });
|
|
41
|
+
assert.equal(detectWebApp(root), true);
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
it("detects a static index.html site", () => {
|
|
45
|
+
const root = project({ "index.html": "<!doctype html>" });
|
|
46
|
+
assert.equal(detectWebApp(root), true);
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
it("returns false for a CLI/library package", () => {
|
|
50
|
+
const root = project({
|
|
51
|
+
"package.json": JSON.stringify({
|
|
52
|
+
dependencies: { commander: "^12.0.0" },
|
|
53
|
+
scripts: { build: "tsc", test: "node --test" },
|
|
54
|
+
}),
|
|
55
|
+
});
|
|
56
|
+
assert.equal(detectWebApp(root), false);
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
it("returns false when there is no package.json or index.html", () => {
|
|
60
|
+
const root = project({ "README.md": "# nothing" });
|
|
61
|
+
assert.equal(detectWebApp(root), false);
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
it("does not throw on malformed package.json", () => {
|
|
65
|
+
const root = project({ "package.json": "{ not valid json" });
|
|
66
|
+
assert.equal(detectWebApp(root), false);
|
|
67
|
+
});
|
|
68
|
+
});
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* web-app-detect — lightweight, synchronous heuristic for deciding whether the
|
|
3
|
+
* project under development is a web app. Used only when the optional managed
|
|
4
|
+
* gsd-browser engine is selected and can be warmed before first use.
|
|
5
|
+
*/
|
|
6
|
+
import { existsSync, readFileSync } from "node:fs";
|
|
7
|
+
import { resolve } from "node:path";
|
|
8
|
+
|
|
9
|
+
// Frontend frameworks / bundlers whose presence in dependencies indicates a
|
|
10
|
+
// browser-facing web app worth warming the optional managed engine for.
|
|
11
|
+
const WEB_DEPENDENCY_RE =
|
|
12
|
+
/^(react|react-dom|next|nuxt|vue|@vue\/|svelte|@sveltejs\/|solid-js|astro|@remix-run\/|gatsby|preact|@angular\/core|vite|@vitejs\/|@builder\.io\/qwik|@web\/dev-server|@11ty\/eleventy)/;
|
|
13
|
+
|
|
14
|
+
// package.json scripts that imply a dev server / browser-facing build.
|
|
15
|
+
const WEB_SCRIPT_RE = /\b(vite|next|nuxt|astro|remix|webpack(-dev-server)?|parcel|ng serve|serve\b|http-server|live-server|gatsby)\b/;
|
|
16
|
+
|
|
17
|
+
interface MinimalPackageJson {
|
|
18
|
+
dependencies?: Record<string, unknown>;
|
|
19
|
+
devDependencies?: Record<string, unknown>;
|
|
20
|
+
peerDependencies?: Record<string, unknown>;
|
|
21
|
+
scripts?: Record<string, unknown>;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function readPackageJson(projectRoot: string): MinimalPackageJson | null {
|
|
25
|
+
const packageJsonPath = resolve(projectRoot, "package.json");
|
|
26
|
+
if (!existsSync(packageJsonPath)) return null;
|
|
27
|
+
try {
|
|
28
|
+
const parsed = JSON.parse(readFileSync(packageJsonPath, "utf-8")) as unknown;
|
|
29
|
+
return parsed && typeof parsed === "object" ? (parsed as MinimalPackageJson) : null;
|
|
30
|
+
} catch {
|
|
31
|
+
return null;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function dependencyNames(pkg: MinimalPackageJson): string[] {
|
|
36
|
+
return [
|
|
37
|
+
...Object.keys(pkg.dependencies ?? {}),
|
|
38
|
+
...Object.keys(pkg.devDependencies ?? {}),
|
|
39
|
+
...Object.keys(pkg.peerDependencies ?? {}),
|
|
40
|
+
];
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Returns true when the project looks like a browser-facing web app. Conservative
|
|
45
|
+
* and dependency-free: a false negative just means lazy connection (the prior
|
|
46
|
+
* behavior); a false positive only warms an idle engine connection.
|
|
47
|
+
*/
|
|
48
|
+
export function detectWebApp(projectRoot: string): boolean {
|
|
49
|
+
const pkg = readPackageJson(projectRoot);
|
|
50
|
+
if (pkg) {
|
|
51
|
+
if (dependencyNames(pkg).some((name) => WEB_DEPENDENCY_RE.test(name))) return true;
|
|
52
|
+
const scriptValues = Object.values(pkg.scripts ?? {}).filter(
|
|
53
|
+
(value): value is string => typeof value === "string",
|
|
54
|
+
);
|
|
55
|
+
if (scriptValues.some((script) => WEB_SCRIPT_RE.test(script))) return true;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// No package.json signal — fall back to a top-level index.html (static sites).
|
|
59
|
+
if (existsSync(resolve(projectRoot, "index.html"))) return true;
|
|
60
|
+
if (existsSync(resolve(projectRoot, "public", "index.html"))) return true;
|
|
61
|
+
|
|
62
|
+
return false;
|
|
63
|
+
}
|