@code-yeongyu/senpi 2026.8.22 → 2026.8.24
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/CHANGELOG.md +55 -0
- package/dist/bun-runtime.d.ts +58 -0
- package/dist/bun-runtime.d.ts.map +1 -0
- package/dist/bun-runtime.js +163 -0
- package/dist/bun-runtime.js.map +1 -0
- package/dist/cli.js +45 -2
- package/dist/cli.js.map +1 -1
- package/dist/core/agent-abort-provenance.d.ts +5 -5
- package/dist/core/agent-abort-provenance.d.ts.map +1 -1
- package/dist/core/agent-abort-provenance.js +3 -0
- package/dist/core/agent-abort-provenance.js.map +1 -1
- package/dist/core/agent-session.d.ts +10 -2
- package/dist/core/agent-session.d.ts.map +1 -1
- package/dist/core/agent-session.js +85 -26
- package/dist/core/agent-session.js.map +1 -1
- package/dist/core/agent-settled-delivery.d.ts +13 -1
- package/dist/core/agent-settled-delivery.d.ts.map +1 -1
- package/dist/core/agent-settled-delivery.js +33 -2
- package/dist/core/agent-settled-delivery.js.map +1 -1
- package/dist/core/extensions/builtin/goal/agent-end-continuation.d.ts.map +1 -1
- package/dist/core/extensions/builtin/goal/agent-end-continuation.js +10 -0
- package/dist/core/extensions/builtin/goal/agent-end-continuation.js.map +1 -1
- package/dist/core/extensions/builtin/goal/cache-warm-renderer.d.ts.map +1 -1
- package/dist/core/extensions/builtin/goal/cache-warm-renderer.js +2 -3
- package/dist/core/extensions/builtin/goal/cache-warm-renderer.js.map +1 -1
- package/dist/core/extensions/builtin/goal/cache-warm.d.ts +6 -0
- package/dist/core/extensions/builtin/goal/cache-warm.d.ts.map +1 -1
- package/dist/core/extensions/builtin/goal/cache-warm.js +43 -0
- package/dist/core/extensions/builtin/goal/cache-warm.js.map +1 -1
- package/dist/core/extensions/builtin/goal/continuation.d.ts +1 -1
- package/dist/core/extensions/builtin/goal/continuation.d.ts.map +1 -1
- package/dist/core/extensions/builtin/goal/continuation.js +1 -1
- package/dist/core/extensions/builtin/goal/continuation.js.map +1 -1
- package/dist/core/extensions/builtin/goal/index.d.ts.map +1 -1
- package/dist/core/extensions/builtin/goal/index.js +0 -8
- package/dist/core/extensions/builtin/goal/index.js.map +1 -1
- package/dist/core/extensions/builtin/goal/monitor-continuation-types.d.ts +1 -0
- package/dist/core/extensions/builtin/goal/monitor-continuation-types.d.ts.map +1 -1
- package/dist/core/extensions/builtin/goal/monitor-continuation-types.js.map +1 -1
- package/dist/core/extensions/builtin/goal/monitor-continuation.d.ts +2 -1
- package/dist/core/extensions/builtin/goal/monitor-continuation.d.ts.map +1 -1
- package/dist/core/extensions/builtin/goal/monitor-continuation.js +31 -3
- package/dist/core/extensions/builtin/goal/monitor-continuation.js.map +1 -1
- package/dist/core/extensions/builtin/goal/wait-ticker.d.ts.map +1 -1
- package/dist/core/extensions/builtin/goal/wait-ticker.js +14 -9
- package/dist/core/extensions/builtin/goal/wait-ticker.js.map +1 -1
- package/dist/core/extensions/builtin/webfetch/webfetch/fetcher.d.ts.map +1 -1
- package/dist/core/extensions/builtin/webfetch/webfetch/fetcher.js +5 -23
- package/dist/core/extensions/builtin/webfetch/webfetch/fetcher.js.map +1 -1
- package/dist/core/extensions/builtin/webfetch/webfetch/response-body.d.ts +11 -0
- package/dist/core/extensions/builtin/webfetch/webfetch/response-body.d.ts.map +1 -0
- package/dist/core/extensions/builtin/webfetch/webfetch/response-body.js +54 -0
- package/dist/core/extensions/builtin/webfetch/webfetch/response-body.js.map +1 -0
- package/dist/core/model-config-schema.d.ts +12 -12
- package/dist/core/model-config-schema.js +2 -2
- package/dist/core/model-config-schema.js.map +1 -1
- package/dist/core/model-resolver.js +2 -2
- package/dist/core/model-resolver.js.map +1 -1
- package/dist/core/session-work-barrier.d.ts.map +1 -1
- package/dist/core/session-work-barrier.js +38 -1
- package/dist/core/session-work-barrier.js.map +1 -1
- package/dist/core/tools/read.d.ts.map +1 -1
- package/dist/core/tools/read.js +5 -0
- package/dist/core/tools/read.js.map +1 -1
- package/dist/modes/interactive/aborted-error-label.d.ts +2 -1
- package/dist/modes/interactive/aborted-error-label.d.ts.map +1 -1
- package/dist/modes/interactive/aborted-error-label.js +20 -5
- package/dist/modes/interactive/aborted-error-label.js.map +1 -1
- package/dist/modes/interactive/components/status-indicator.d.ts +3 -0
- package/dist/modes/interactive/components/status-indicator.d.ts.map +1 -1
- package/dist/modes/interactive/components/status-indicator.js +7 -1
- package/dist/modes/interactive/components/status-indicator.js.map +1 -1
- package/dist/modes/interactive/interactive-mode.d.ts +2 -0
- package/dist/modes/interactive/interactive-mode.d.ts.map +1 -1
- package/dist/modes/interactive/interactive-mode.js +50 -10
- package/dist/modes/interactive/interactive-mode.js.map +1 -1
- package/dist/modes/interactive/streaming-reveal.d.ts +8 -0
- package/dist/modes/interactive/streaming-reveal.d.ts.map +1 -1
- package/dist/modes/interactive/streaming-reveal.js +12 -0
- package/dist/modes/interactive/streaming-reveal.js.map +1 -1
- package/dist/senpi +45 -2
- package/docs/AGENTS.md +3 -1
- package/examples/AGENTS.md +21 -6
- package/examples/extensions/AGENTS.md +50 -0
- package/node_modules/@anthropic-ai/claude-agent-sdk/bridge.mjs +90 -88
- package/node_modules/@anthropic-ai/claude-agent-sdk/browser-sdk.js +40 -40
- package/node_modules/@anthropic-ai/claude-agent-sdk/manifest.json +20 -21
- package/node_modules/@anthropic-ai/claude-agent-sdk/manifest.zst.json +24 -25
- package/node_modules/@anthropic-ai/claude-agent-sdk/package.json +2 -2
- package/node_modules/@anthropic-ai/claude-agent-sdk/sdk-tools.d.ts +102 -5
- package/node_modules/@anthropic-ai/claude-agent-sdk/sdk.d.ts +31 -15
- package/node_modules/@anthropic-ai/claude-agent-sdk/sdk.mjs +80 -78
- package/node_modules/@aws-sdk/client-bedrock-runtime/dist-cjs/index.js +1 -1
- package/node_modules/@aws-sdk/client-bedrock-runtime/package.json +13 -13
- package/node_modules/@aws-sdk/core/package.json +5 -5
- package/node_modules/@aws-sdk/credential-provider-env/package.json +5 -5
- package/node_modules/@aws-sdk/credential-provider-http/package.json +7 -7
- package/node_modules/@aws-sdk/credential-provider-ini/package.json +12 -12
- package/node_modules/@aws-sdk/credential-provider-login/package.json +6 -6
- package/node_modules/@aws-sdk/credential-provider-node/package.json +10 -10
- package/node_modules/@aws-sdk/credential-provider-process/package.json +5 -5
- package/node_modules/@aws-sdk/credential-provider-sso/package.json +7 -7
- package/node_modules/@aws-sdk/credential-provider-web-identity/package.json +6 -6
- package/node_modules/@aws-sdk/eventstream-handler-node/package.json +4 -4
- package/node_modules/@aws-sdk/middleware-eventstream/package.json +4 -4
- package/node_modules/@aws-sdk/middleware-websocket/package.json +6 -6
- package/node_modules/@aws-sdk/nested-clients/dist-cjs/submodules/cognito-identity/index.js +1 -1
- package/node_modules/@aws-sdk/nested-clients/dist-cjs/submodules/signin/index.js +1 -1
- package/node_modules/@aws-sdk/nested-clients/dist-cjs/submodules/sso/index.js +1 -1
- package/node_modules/@aws-sdk/nested-clients/dist-cjs/submodules/sso-oidc/index.js +1 -1
- package/node_modules/@aws-sdk/nested-clients/dist-cjs/submodules/sts/index.js +1 -1
- package/node_modules/@aws-sdk/nested-clients/package.json +8 -8
- package/node_modules/@aws-sdk/signature-v4-multi-region/package.json +3 -3
- package/node_modules/@aws-sdk/token-providers/package.json +6 -6
- package/node_modules/@aws-sdk/types/package.json +2 -2
- package/node_modules/@aws-sdk/xml-builder/package.json +2 -2
- package/node_modules/@code-yeongyu/senpi-codemode/CHANGELOG.md +46 -0
- package/node_modules/@code-yeongyu/senpi-codemode/README.md +5 -0
- package/node_modules/@code-yeongyu/senpi-codemode/package.json +5 -5
- package/node_modules/@code-yeongyu/senpi-codemode/src/extension/eval-notifier.ts +16 -4
- package/node_modules/@code-yeongyu/senpi-codemode/src/extension/runtime-factory.ts +4 -1
- package/node_modules/@code-yeongyu/senpi-codemode/src/extension/runtime-info.ts +41 -0
- package/node_modules/@code-yeongyu/senpi-codemode/src/index.ts +9 -3
- package/node_modules/@code-yeongyu/senpi-codemode/src/interpreters/detect.ts +17 -4
- package/node_modules/@code-yeongyu/senpi-codemode/src/interpreters/resolve-command.ts +68 -0
- package/node_modules/@code-yeongyu/senpi-codemode/src/kernels/AGENTS.md +40 -0
- package/node_modules/@code-yeongyu/senpi-codemode/src/timeouts/idle-timeout.ts +33 -4
- package/node_modules/@code-yeongyu/senpi-codemode/src/tool/AGENTS.md +40 -0
- package/node_modules/@code-yeongyu/senpi-codemode/src/tool/cell-runtime.ts +5 -1
- package/node_modules/@code-yeongyu/senpi-codemode/src/tool/detached-cell-manager.ts +1 -1
- package/node_modules/@code-yeongyu/senpi-codemode/src/tool/detached-cell-notification.ts +3 -8
- package/node_modules/@code-yeongyu/senpi-codemode/src/tool/detached-notification-queue.ts +2 -6
- package/node_modules/@code-yeongyu/senpi-codemode/src/tool/eval-tool-options.ts +3 -0
- package/node_modules/@code-yeongyu/senpi-codemode/src/tool/eval-tool.ts +2 -0
- package/node_modules/@code-yeongyu/senpi-codemode/src/tool/render.ts +60 -3
- package/node_modules/@code-yeongyu/senpi-codemode/src/tool/runtime-label.ts +49 -0
- package/node_modules/@code-yeongyu/senpi-codemode/src/tool/types.ts +13 -0
- package/node_modules/@earendil-works/pi-agent-core/package.json +4 -4
- package/node_modules/@earendil-works/pi-ai/dist/api/cursor-agent.d.ts +5 -1
- package/node_modules/@earendil-works/pi-ai/dist/api/cursor-agent.d.ts.map +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/api/cursor-agent.js +13 -5
- package/node_modules/@earendil-works/pi-ai/dist/api/cursor-agent.js.map +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/cursor/composer-prompt.d.ts +27 -0
- package/node_modules/@earendil-works/pi-ai/dist/cursor/composer-prompt.d.ts.map +1 -0
- package/node_modules/@earendil-works/pi-ai/dist/cursor/composer-prompt.js +54 -0
- package/node_modules/@earendil-works/pi-ai/dist/cursor/composer-prompt.js.map +1 -0
- package/node_modules/@earendil-works/pi-ai/dist/providers/data/.manifest.json +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/providers/data/nvidia.json +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/providers/data/opencode-go.json +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/providers/data/opengateway.json +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/providers/data/openrouter.json +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/providers/data/vercel-ai-gateway.json +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/protocols/kimi-xtml/markers.d.ts +4 -0
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/protocols/kimi-xtml/markers.d.ts.map +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/protocols/kimi-xtml/markers.js +14 -0
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/protocols/kimi-xtml/markers.js.map +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/protocols/kimi-xtml/recovery-stream.d.ts.map +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/protocols/kimi-xtml/recovery-stream.js +65 -35
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/protocols/kimi-xtml/recovery-stream.js.map +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/protocols/kimi-xtml/thinking-recovery.d.ts.map +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/protocols/kimi-xtml/thinking-recovery.js +40 -12
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/protocols/kimi-xtml/thinking-recovery.js.map +1 -1
- package/node_modules/@earendil-works/pi-ai/package.json +4 -4
- package/node_modules/@earendil-works/pi-pty/package.json +1 -1
- package/node_modules/@earendil-works/pi-telemetry/package.json +1 -1
- package/node_modules/@earendil-works/pi-tui/dist/terminal.d.ts.map +1 -1
- package/node_modules/@earendil-works/pi-tui/dist/terminal.js +7 -4
- package/node_modules/@earendil-works/pi-tui/dist/terminal.js.map +1 -1
- package/node_modules/@earendil-works/pi-tui/package.json +1 -1
- package/node_modules/@smithy/signature-v4/dist-cjs/index.js +19 -7
- package/node_modules/@smithy/signature-v4/dist-es/HeaderFormatter.js +4 -1
- package/node_modules/@smithy/signature-v4/dist-es/getCanonicalQuery.js +4 -1
- package/node_modules/@smithy/signature-v4/dist-es/getPayloadHash.js +4 -1
- package/node_modules/@smithy/signature-v4/dist-es/headerUtil.js +10 -3
- package/node_modules/@smithy/signature-v4/dist-es/moveHeadersToQuery.js +4 -1
- package/node_modules/@smithy/signature-v4/dist-es/prepareRequest.js +4 -1
- package/node_modules/@smithy/signature-v4/package.json +2 -2
- package/node_modules/typebox/build/format/email.mjs +1 -1
- package/node_modules/typebox/build/format/idna/idn-hostname.mjs +19 -1
- package/node_modules/typebox/build/format/json_pointer.d.mts +2 -2
- package/node_modules/typebox/build/format/json_pointer.mjs +2 -2
- package/node_modules/typebox/build/format/json_pointer_uri_fragment.d.mts +2 -2
- package/node_modules/typebox/build/format/json_pointer_uri_fragment.mjs +2 -2
- package/node_modules/typebox/build/format/relative_json_pointer.d.mts +2 -2
- package/node_modules/typebox/build/format/relative_json_pointer.mjs +2 -2
- package/node_modules/typebox/build/system/memory/assign.mjs +2 -1
- package/node_modules/typebox/build/system/memory/create.mjs +3 -4
- package/node_modules/typebox/build/system/memory/discard.mjs +2 -2
- package/node_modules/typebox/build/system/memory/freeze.d.mts +4 -0
- package/node_modules/typebox/build/system/memory/freeze.mjs +6 -0
- package/node_modules/typebox/build/system/memory/update.mjs +3 -2
- package/node_modules/typebox/package.json +1 -1
- package/package.json +36 -36
- package/node_modules/@aws-sdk/credential-provider-sso/node_modules/@aws-sdk/token-providers/LICENSE +0 -201
- package/node_modules/@aws-sdk/credential-provider-sso/node_modules/@aws-sdk/token-providers/README.md +0 -62
- package/node_modules/@aws-sdk/credential-provider-sso/node_modules/@aws-sdk/token-providers/dist-cjs/index.js +0 -154
- package/node_modules/@aws-sdk/credential-provider-sso/node_modules/@aws-sdk/token-providers/dist-es/constants.js +0 -2
- package/node_modules/@aws-sdk/credential-provider-sso/node_modules/@aws-sdk/token-providers/dist-es/fromEnvSigningName.js +0 -16
- package/node_modules/@aws-sdk/credential-provider-sso/node_modules/@aws-sdk/token-providers/dist-es/fromSso.js +0 -80
- package/node_modules/@aws-sdk/credential-provider-sso/node_modules/@aws-sdk/token-providers/dist-es/fromStatic.js +0 -8
- package/node_modules/@aws-sdk/credential-provider-sso/node_modules/@aws-sdk/token-providers/dist-es/getNewSsoOidcToken.js +0 -11
- package/node_modules/@aws-sdk/credential-provider-sso/node_modules/@aws-sdk/token-providers/dist-es/getSsoOidcClient.js +0 -10
- package/node_modules/@aws-sdk/credential-provider-sso/node_modules/@aws-sdk/token-providers/dist-es/index.js +0 -4
- package/node_modules/@aws-sdk/credential-provider-sso/node_modules/@aws-sdk/token-providers/dist-es/nodeProvider.js +0 -5
- package/node_modules/@aws-sdk/credential-provider-sso/node_modules/@aws-sdk/token-providers/dist-es/validateTokenExpiry.js +0 -7
- package/node_modules/@aws-sdk/credential-provider-sso/node_modules/@aws-sdk/token-providers/dist-es/validateTokenKey.js +0 -7
- package/node_modules/@aws-sdk/credential-provider-sso/node_modules/@aws-sdk/token-providers/dist-es/writeSSOTokenToFile.js +0 -8
- package/node_modules/@aws-sdk/credential-provider-sso/node_modules/@aws-sdk/token-providers/dist-types/constants.d.ts +0 -8
- package/node_modules/@aws-sdk/credential-provider-sso/node_modules/@aws-sdk/token-providers/dist-types/fromEnvSigningName.d.ts +0 -18
- package/node_modules/@aws-sdk/credential-provider-sso/node_modules/@aws-sdk/token-providers/dist-types/fromSso.d.ts +0 -12
- package/node_modules/@aws-sdk/credential-provider-sso/node_modules/@aws-sdk/token-providers/dist-types/fromStatic.d.ts +0 -9
- package/node_modules/@aws-sdk/credential-provider-sso/node_modules/@aws-sdk/token-providers/dist-types/getNewSsoOidcToken.d.ts +0 -8
- package/node_modules/@aws-sdk/credential-provider-sso/node_modules/@aws-sdk/token-providers/dist-types/getSsoOidcClient.d.ts +0 -7
- package/node_modules/@aws-sdk/credential-provider-sso/node_modules/@aws-sdk/token-providers/dist-types/index.d.ts +0 -7
- package/node_modules/@aws-sdk/credential-provider-sso/node_modules/@aws-sdk/token-providers/dist-types/nodeProvider.d.ts +0 -18
- package/node_modules/@aws-sdk/credential-provider-sso/node_modules/@aws-sdk/token-providers/dist-types/ts3.4/constants.d.ts +0 -3
- package/node_modules/@aws-sdk/credential-provider-sso/node_modules/@aws-sdk/token-providers/dist-types/ts3.4/fromEnvSigningName.d.ts +0 -8
- package/node_modules/@aws-sdk/credential-provider-sso/node_modules/@aws-sdk/token-providers/dist-types/ts3.4/fromSso.d.ts +0 -10
- package/node_modules/@aws-sdk/credential-provider-sso/node_modules/@aws-sdk/token-providers/dist-types/ts3.4/fromStatic.d.ts +0 -5
- package/node_modules/@aws-sdk/credential-provider-sso/node_modules/@aws-sdk/token-providers/dist-types/ts3.4/getNewSsoOidcToken.d.ts +0 -9
- package/node_modules/@aws-sdk/credential-provider-sso/node_modules/@aws-sdk/token-providers/dist-types/ts3.4/getSsoOidcClient.d.ts +0 -7
- package/node_modules/@aws-sdk/credential-provider-sso/node_modules/@aws-sdk/token-providers/dist-types/ts3.4/index.d.ts +0 -7
- package/node_modules/@aws-sdk/credential-provider-sso/node_modules/@aws-sdk/token-providers/dist-types/ts3.4/nodeProvider.d.ts +0 -3
- package/node_modules/@aws-sdk/credential-provider-sso/node_modules/@aws-sdk/token-providers/dist-types/ts3.4/validateTokenExpiry.d.ts +0 -2
- package/node_modules/@aws-sdk/credential-provider-sso/node_modules/@aws-sdk/token-providers/dist-types/ts3.4/validateTokenKey.d.ts +0 -1
- package/node_modules/@aws-sdk/credential-provider-sso/node_modules/@aws-sdk/token-providers/dist-types/ts3.4/writeSSOTokenToFile.d.ts +0 -2
- package/node_modules/@aws-sdk/credential-provider-sso/node_modules/@aws-sdk/token-providers/dist-types/validateTokenExpiry.d.ts +0 -5
- package/node_modules/@aws-sdk/credential-provider-sso/node_modules/@aws-sdk/token-providers/dist-types/validateTokenKey.d.ts +0 -4
- package/node_modules/@aws-sdk/credential-provider-sso/node_modules/@aws-sdk/token-providers/dist-types/writeSSOTokenToFile.d.ts +0 -5
- package/node_modules/@aws-sdk/credential-provider-sso/node_modules/@aws-sdk/token-providers/package.json +0 -69
|
@@ -5,6 +5,10 @@ export declare const XTML_CALL_CLOSE = "<|close|>call<|sep|>";
|
|
|
5
5
|
export declare const XTML_ARGUMENT_OPEN = "<|open|>argument ";
|
|
6
6
|
export declare const XTML_ARGUMENT_CLOSE = "<|close|>argument<|sep|>";
|
|
7
7
|
export declare const XTML_SEP = "<|sep|>";
|
|
8
|
+
export declare const XTML_OPEN_PREFIX = "<|open|>";
|
|
9
|
+
export declare const XTML_CLOSE_PREFIX = "<|close|>";
|
|
10
|
+
export declare const XTML_CHANNEL_MARKER_PATTERN: RegExp;
|
|
11
|
+
export declare function matchXtmlChannelMarker(text: string): string | undefined;
|
|
8
12
|
export declare function parseXtmlAttributes(header: string): Record<string, string>;
|
|
9
13
|
export declare function getPartialXtmlSuffix(text: string, tokens: readonly string[]): string;
|
|
10
14
|
//# sourceMappingURL=markers.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"markers.d.ts","sourceRoot":"","sources":["../../../../src/tool-call-middleware/protocols/kimi-xtml/markers.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,eAAe,yBAAyB,CAAC;AACtD,eAAO,MAAM,gBAAgB,0BAA0B,CAAC;AACxD,eAAO,MAAM,cAAc,kBAAkB,CAAC;AAC9C,eAAO,MAAM,eAAe,yBAAyB,CAAC;AACtD,eAAO,MAAM,kBAAkB,sBAAsB,CAAC;AACtD,eAAO,MAAM,mBAAmB,6BAA6B,CAAC;AAC9D,eAAO,MAAM,QAAQ,YAAY,CAAC;
|
|
1
|
+
{"version":3,"file":"markers.d.ts","sourceRoot":"","sources":["../../../../src/tool-call-middleware/protocols/kimi-xtml/markers.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,eAAe,yBAAyB,CAAC;AACtD,eAAO,MAAM,gBAAgB,0BAA0B,CAAC;AACxD,eAAO,MAAM,cAAc,kBAAkB,CAAC;AAC9C,eAAO,MAAM,eAAe,yBAAyB,CAAC;AACtD,eAAO,MAAM,kBAAkB,sBAAsB,CAAC;AACtD,eAAO,MAAM,mBAAmB,6BAA6B,CAAC;AAC9D,eAAO,MAAM,QAAQ,YAAY,CAAC;AAElC,eAAO,MAAM,gBAAgB,aAAa,CAAC;AAC3C,eAAO,MAAM,iBAAiB,cAAc,CAAC;AAM7C,eAAO,MAAM,2BAA2B,QAOvC,CAAC;AAIF,wBAAgB,sBAAsB,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS,CAEvE;AAID,wBAAgB,mBAAmB,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAQ1E;AAED,wBAAgB,oBAAoB,CAAC,IAAI,EAAE,MAAM,EAAE,MAAM,EAAE,SAAS,MAAM,EAAE,GAAG,MAAM,CAUpF"}
|
package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/protocols/kimi-xtml/markers.js
CHANGED
|
@@ -5,6 +5,20 @@ export const XTML_CALL_CLOSE = "<|close|>call<|sep|>";
|
|
|
5
5
|
export const XTML_ARGUMENT_OPEN = "<|open|>argument ";
|
|
6
6
|
export const XTML_ARGUMENT_CLOSE = "<|close|>argument<|sep|>";
|
|
7
7
|
export const XTML_SEP = "<|sep|>";
|
|
8
|
+
export const XTML_OPEN_PREFIX = "<|open|>";
|
|
9
|
+
export const XTML_CLOSE_PREFIX = "<|close|>";
|
|
10
|
+
const CHANNEL_NAME = "[a-zA-Z_][a-zA-Z0-9_]*";
|
|
11
|
+
const STRUCTURAL_CHANNEL_NAME = "(?:call|argument)\\b";
|
|
12
|
+
const SEPLESS_CHANNEL_NAME = `(?!${STRUCTURAL_CHANNEL_NAME})${CHANNEL_NAME}`;
|
|
13
|
+
export const XTML_CHANNEL_MARKER_PATTERN = new RegExp([
|
|
14
|
+
`<\\|(?:open|close)\\|>(?:${CHANNEL_NAME})?<\\|sep\\|>`,
|
|
15
|
+
`<\\|(?:open|close)\\|>(?:${SEPLESS_CHANNEL_NAME})?(?=$|[^a-zA-Z0-9_])`,
|
|
16
|
+
`<\\|sep\\|>`,
|
|
17
|
+
].join("|"), "g");
|
|
18
|
+
const ANCHORED_CHANNEL_MARKER_PATTERN = new RegExp(`^(?:${XTML_CHANNEL_MARKER_PATTERN.source})`);
|
|
19
|
+
export function matchXtmlChannelMarker(text) {
|
|
20
|
+
return ANCHORED_CHANNEL_MARKER_PATTERN.exec(text)?.[0];
|
|
21
|
+
}
|
|
8
22
|
const ATTRIBUTE_PATTERN = /([\w-]+)\s*=\s*(?:"([^"]*)"|'([^']*)'|([^\s<]+))/g;
|
|
9
23
|
export function parseXtmlAttributes(header) {
|
|
10
24
|
const attributes = {};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"markers.js","sourceRoot":"","sources":["../../../../src/tool-call-middleware/protocols/kimi-xtml/markers.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,MAAM,eAAe,GAAG,sBAAsB,CAAC;AACtD,MAAM,CAAC,MAAM,gBAAgB,GAAG,uBAAuB,CAAC;AACxD,MAAM,CAAC,MAAM,cAAc,GAAG,eAAe,CAAC;AAC9C,MAAM,CAAC,MAAM,eAAe,GAAG,sBAAsB,CAAC;AACtD,MAAM,CAAC,MAAM,kBAAkB,GAAG,mBAAmB,CAAC;AACtD,MAAM,CAAC,MAAM,mBAAmB,GAAG,0BAA0B,CAAC;AAC9D,MAAM,CAAC,MAAM,QAAQ,GAAG,SAAS,CAAC;AAElC,MAAM,iBAAiB,GAAG,mDAAmD,CAAC;AAE9E,MAAM,UAAU,mBAAmB,CAAC,MAAc;IACjD,MAAM,UAAU,GAA2B,EAAE,CAAC;IAC9C,KAAK,MAAM,KAAK,IAAI,MAAM,CAAC,QAAQ,CAAC,iBAAiB,CAAC,EAAE,CAAC;QACxD,MAAM,GAAG,GAAG,KAAK,CAAC,CAAC,CAAC,CAAC;QACrB,IAAI,CAAC,GAAG;YAAE,SAAS;QACnB,UAAU,CAAC,GAAG,CAAC,GAAG,KAAK,CAAC,CAAC,CAAC,IAAI,KAAK,CAAC,CAAC,CAAC,IAAI,KAAK,CAAC,CAAC,CAAC,IAAI,EAAE,CAAC;IAC1D,CAAC;IACD,OAAO,UAAU,CAAC;AACnB,CAAC;AAED,MAAM,UAAU,oBAAoB,CAAC,IAAY,EAAE,MAAyB;IAC3E,KAAK,MAAM,KAAK,IAAI,MAAM,EAAE,CAAC;QAC5B,KAAK,IAAI,MAAM,GAAG,KAAK,CAAC,MAAM,GAAG,CAAC,EAAE,MAAM,GAAG,CAAC,EAAE,MAAM,IAAI,CAAC,EAAE,CAAC;YAC7D,MAAM,MAAM,GAAG,KAAK,CAAC,KAAK,CAAC,CAAC,EAAE,MAAM,CAAC,CAAC;YACtC,IAAI,IAAI,CAAC,QAAQ,CAAC,MAAM,CAAC,EAAE,CAAC;gBAC3B,OAAO,MAAM,CAAC;YACf,CAAC;QACF,CAAC;IACF,CAAC;IACD,OAAO,EAAE,CAAC;AACX,CAAC","sourcesContent":["export const XTML_TOOLS_OPEN = \"<|open|>tools<|sep|>\";\nexport const XTML_TOOLS_CLOSE = \"<|close|>tools<|sep|>\";\nexport const XTML_CALL_OPEN = \"<|open|>call \";\nexport const XTML_CALL_CLOSE = \"<|close|>call<|sep|>\";\nexport const XTML_ARGUMENT_OPEN = \"<|open|>argument \";\nexport const XTML_ARGUMENT_CLOSE = \"<|close|>argument<|sep|>\";\nexport const XTML_SEP = \"<|sep|>\";\n\nconst ATTRIBUTE_PATTERN = /([\\w-]+)\\s*=\\s*(?:\"([^\"]*)\"|'([^']*)'|([^\\s<]+))/g;\n\nexport function parseXtmlAttributes(header: string): Record<string, string> {\n\tconst attributes: Record<string, string> = {};\n\tfor (const match of header.matchAll(ATTRIBUTE_PATTERN)) {\n\t\tconst key = match[1];\n\t\tif (!key) continue;\n\t\tattributes[key] = match[2] ?? match[3] ?? match[4] ?? \"\";\n\t}\n\treturn attributes;\n}\n\nexport function getPartialXtmlSuffix(text: string, tokens: readonly string[]): string {\n\tfor (const token of tokens) {\n\t\tfor (let length = token.length - 1; length > 0; length -= 1) {\n\t\t\tconst prefix = token.slice(0, length);\n\t\t\tif (text.endsWith(prefix)) {\n\t\t\t\treturn prefix;\n\t\t\t}\n\t\t}\n\t}\n\treturn \"\";\n}\n"]}
|
|
1
|
+
{"version":3,"file":"markers.js","sourceRoot":"","sources":["../../../../src/tool-call-middleware/protocols/kimi-xtml/markers.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,MAAM,eAAe,GAAG,sBAAsB,CAAC;AACtD,MAAM,CAAC,MAAM,gBAAgB,GAAG,uBAAuB,CAAC;AACxD,MAAM,CAAC,MAAM,cAAc,GAAG,eAAe,CAAC;AAC9C,MAAM,CAAC,MAAM,eAAe,GAAG,sBAAsB,CAAC;AACtD,MAAM,CAAC,MAAM,kBAAkB,GAAG,mBAAmB,CAAC;AACtD,MAAM,CAAC,MAAM,mBAAmB,GAAG,0BAA0B,CAAC;AAC9D,MAAM,CAAC,MAAM,QAAQ,GAAG,SAAS,CAAC;AAElC,MAAM,CAAC,MAAM,gBAAgB,GAAG,UAAU,CAAC;AAC3C,MAAM,CAAC,MAAM,iBAAiB,GAAG,WAAW,CAAC;AAE7C,MAAM,YAAY,GAAG,wBAAwB,CAAC;AAC9C,MAAM,uBAAuB,GAAG,sBAAsB,CAAC;AACvD,MAAM,oBAAoB,GAAG,MAAM,uBAAuB,IAAI,YAAY,EAAE,CAAC;AAE7E,MAAM,CAAC,MAAM,2BAA2B,GAAG,IAAI,MAAM,CACpD;IACC,4BAA4B,YAAY,eAAe;IACvD,4BAA4B,oBAAoB,uBAAuB;IACvE,aAAa;CACb,CAAC,IAAI,CAAC,GAAG,CAAC,EACX,GAAG,CACH,CAAC;AAEF,MAA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sep|>\";\nexport const XTML_CALL_OPEN = \"<|open|>call \";\nexport const XTML_CALL_CLOSE = \"<|close|>call<|sep|>\";\nexport const XTML_ARGUMENT_OPEN = \"<|open|>argument \";\nexport const XTML_ARGUMENT_CLOSE = \"<|close|>argument<|sep|>\";\nexport const XTML_SEP = \"<|sep|>\";\n\nexport const XTML_OPEN_PREFIX = \"<|open|>\";\nexport const XTML_CLOSE_PREFIX = \"<|close|>\";\n\nconst CHANNEL_NAME = \"[a-zA-Z_][a-zA-Z0-9_]*\";\nconst STRUCTURAL_CHANNEL_NAME = \"(?:call|argument)\\\\b\";\nconst SEPLESS_CHANNEL_NAME = `(?!${STRUCTURAL_CHANNEL_NAME})${CHANNEL_NAME}`;\n\nexport const XTML_CHANNEL_MARKER_PATTERN = new RegExp(\n\t[\n\t\t`<\\\\|(?:open|close)\\\\|>(?:${CHANNEL_NAME})?<\\\\|sep\\\\|>`,\n\t\t`<\\\\|(?:open|close)\\\\|>(?:${SEPLESS_CHANNEL_NAME})?(?=$|[^a-zA-Z0-9_])`,\n\t\t`<\\\\|sep\\\\|>`,\n\t].join(\"|\"),\n\t\"g\",\n);\n\nconst ANCHORED_CHANNEL_MARKER_PATTERN = new RegExp(`^(?:${XTML_CHANNEL_MARKER_PATTERN.source})`);\n\nexport function matchXtmlChannelMarker(text: string): string | undefined {\n\treturn ANCHORED_CHANNEL_MARKER_PATTERN.exec(text)?.[0];\n}\n\nconst ATTRIBUTE_PATTERN = /([\\w-]+)\\s*=\\s*(?:\"([^\"]*)\"|'([^']*)'|([^\\s<]+))/g;\n\nexport function parseXtmlAttributes(header: string): Record<string, string> {\n\tconst attributes: Record<string, string> = {};\n\tfor (const match of header.matchAll(ATTRIBUTE_PATTERN)) {\n\t\tconst key = match[1];\n\t\tif (!key) continue;\n\t\tattributes[key] = match[2] ?? match[3] ?? match[4] ?? \"\";\n\t}\n\treturn attributes;\n}\n\nexport function getPartialXtmlSuffix(text: string, tokens: readonly string[]): string {\n\tfor (const token of tokens) {\n\t\tfor (let length = token.length - 1; length > 0; length -= 1) {\n\t\t\tconst prefix = token.slice(0, length);\n\t\t\tif (text.endsWith(prefix)) {\n\t\t\t\treturn prefix;\n\t\t\t}\n\t\t}\n\t}\n\treturn \"\";\n}\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"recovery-stream.d.ts","sourceRoot":"","sources":["../../../../src/tool-call-middleware/protocols/kimi-xtml/recovery-stream.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,mBAAmB,CAAC;
|
|
1
|
+
{"version":3,"file":"recovery-stream.d.ts","sourceRoot":"","sources":["../../../../src/tool-call-middleware/protocols/kimi-xtml/recovery-stream.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,mBAAmB,CAAC;AAE9C,OAAO,KAAK,EAAE,aAAa,EAAqB,MAAM,gBAAgB,CAAC;AACvE,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,qCAAqC,CAAC;AAkDhF,wBAAgB,8BAA8B,CAAC,KAAK,EAAE,SAAS,IAAI,EAAE,EAAE,OAAO,CAAC,EAAE,aAAa,GAAG,oBAAoB,CAyMpH"}
|
|
@@ -1,25 +1,27 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { createRecoveryCodeMask } from "../../recovery-code-mask.js";
|
|
2
|
+
import { getPartialXtmlSuffix, matchXtmlChannelMarker, parseXtmlAttributes, XTML_ARGUMENT_CLOSE, XTML_ARGUMENT_OPEN, XTML_CALL_CLOSE, XTML_CALL_OPEN, XTML_CLOSE_PREFIX, XTML_OPEN_PREFIX, XTML_SEP, XTML_TOOLS_CLOSE, XTML_TOOLS_OPEN, } from "./markers.js";
|
|
2
3
|
import { coerceXtmlArgumentValue } from "./parse.js";
|
|
3
|
-
const
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
return
|
|
4
|
+
const MARKER_PREFIXES = [XTML_OPEN_PREFIX, XTML_CLOSE_PREFIX, XTML_SEP];
|
|
5
|
+
function findMarkerStart(text) {
|
|
6
|
+
return MARKER_PREFIXES.map((prefix) => text.indexOf(prefix))
|
|
7
|
+
.filter((index) => index !== -1)
|
|
8
|
+
.sort((a, b) => a - b)[0];
|
|
9
|
+
}
|
|
10
|
+
function couldExtendMarker(buffer, marker) {
|
|
11
|
+
if (marker.endsWith(XTML_SEP))
|
|
12
|
+
return false;
|
|
13
|
+
const rest = buffer.slice(marker.length);
|
|
14
|
+
return rest.length === 0 || XTML_SEP.startsWith(rest);
|
|
15
|
+
}
|
|
16
|
+
const STRUCTURAL_MARKER_OPENS = [XTML_CALL_OPEN, XTML_ARGUMENT_OPEN];
|
|
17
|
+
function startsCompleteStructuralMarker(buffer) {
|
|
18
|
+
return STRUCTURAL_MARKER_OPENS.some((open) => buffer.startsWith(open));
|
|
19
|
+
}
|
|
20
|
+
function startsPartialStructuralMarker(buffer) {
|
|
21
|
+
return STRUCTURAL_MARKER_OPENS.some((open) => buffer.length < open.length && open.startsWith(buffer));
|
|
21
22
|
}
|
|
22
23
|
export function createXtmlRecoveryStreamParser(tools, options) {
|
|
24
|
+
const mask = createRecoveryCodeMask();
|
|
23
25
|
let buffer = "";
|
|
24
26
|
let mode = "text";
|
|
25
27
|
let callIndex = -1;
|
|
@@ -59,11 +61,9 @@ export function createXtmlRecoveryStreamParser(tools, options) {
|
|
|
59
61
|
mode = "tools";
|
|
60
62
|
}
|
|
61
63
|
function processText(events) {
|
|
62
|
-
const
|
|
63
|
-
const closeIndex = buffer.indexOf(CLOSE_PREFIX);
|
|
64
|
-
const markerIndex = [openIndex, closeIndex].filter((index) => index !== -1).sort((a, b) => a - b)[0];
|
|
64
|
+
const markerIndex = findMarkerStart(buffer);
|
|
65
65
|
if (markerIndex === undefined) {
|
|
66
|
-
const partial = getPartialXtmlSuffix(buffer,
|
|
66
|
+
const partial = getPartialXtmlSuffix(buffer, MARKER_PREFIXES);
|
|
67
67
|
const flushable = partial ? buffer.slice(0, -partial.length) : buffer;
|
|
68
68
|
if (flushable)
|
|
69
69
|
events.push({ type: "text", text: flushable });
|
|
@@ -75,16 +75,21 @@ export function createXtmlRecoveryStreamParser(tools, options) {
|
|
|
75
75
|
buffer = buffer.slice(markerIndex);
|
|
76
76
|
return true;
|
|
77
77
|
}
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
78
|
+
if (startsCompleteStructuralMarker(buffer)) {
|
|
79
|
+
mode = "tools";
|
|
80
|
+
return true;
|
|
81
|
+
}
|
|
82
|
+
if (startsPartialStructuralMarker(buffer))
|
|
83
|
+
return false;
|
|
84
|
+
const marker = matchXtmlChannelMarker(buffer);
|
|
85
|
+
if (marker) {
|
|
86
|
+
if (couldExtendMarker(buffer, marker))
|
|
87
|
+
return false;
|
|
81
88
|
buffer = buffer.slice(marker.length);
|
|
82
89
|
if (marker === XTML_TOOLS_OPEN)
|
|
83
90
|
mode = "tools";
|
|
84
91
|
return true;
|
|
85
92
|
}
|
|
86
|
-
if (couldBeMarkerPrefix(buffer))
|
|
87
|
-
return false;
|
|
88
93
|
events.push({ type: "text", text: buffer.slice(0, 2) });
|
|
89
94
|
buffer = buffer.slice(2);
|
|
90
95
|
return true;
|
|
@@ -174,12 +179,28 @@ export function createXtmlRecoveryStreamParser(tools, options) {
|
|
|
174
179
|
mode = "tools";
|
|
175
180
|
}
|
|
176
181
|
}
|
|
182
|
+
function consume(segments) {
|
|
183
|
+
const events = [];
|
|
184
|
+
for (const segment of segments) {
|
|
185
|
+
if (segment.scan) {
|
|
186
|
+
buffer += segment.text;
|
|
187
|
+
process(events);
|
|
188
|
+
continue;
|
|
189
|
+
}
|
|
190
|
+
if (mode === "text" && buffer) {
|
|
191
|
+
events.push({ type: "text", text: buffer });
|
|
192
|
+
buffer = "";
|
|
193
|
+
}
|
|
194
|
+
if (mode === "text")
|
|
195
|
+
events.push({ type: "text", text: segment.text });
|
|
196
|
+
else
|
|
197
|
+
buffer += segment.text;
|
|
198
|
+
}
|
|
199
|
+
return events;
|
|
200
|
+
}
|
|
177
201
|
return {
|
|
178
202
|
feed(textDelta) {
|
|
179
|
-
|
|
180
|
-
const events = [];
|
|
181
|
-
process(events);
|
|
182
|
-
return events;
|
|
203
|
+
return consume(mask.feed(textDelta));
|
|
183
204
|
},
|
|
184
205
|
interrupt() {
|
|
185
206
|
const events = [];
|
|
@@ -190,10 +211,19 @@ export function createXtmlRecoveryStreamParser(tools, options) {
|
|
|
190
211
|
return events;
|
|
191
212
|
},
|
|
192
213
|
finish() {
|
|
193
|
-
const events =
|
|
214
|
+
const events = consume(mask.finish());
|
|
194
215
|
if (mode === "text") {
|
|
195
|
-
|
|
196
|
-
|
|
216
|
+
let rest = buffer;
|
|
217
|
+
for (;;) {
|
|
218
|
+
const marker = matchXtmlChannelMarker(rest);
|
|
219
|
+
if (!marker)
|
|
220
|
+
break;
|
|
221
|
+
rest = rest.slice(marker.length);
|
|
222
|
+
}
|
|
223
|
+
if (XTML_SEP.startsWith(rest) && rest.length > 0)
|
|
224
|
+
rest = "";
|
|
225
|
+
if (rest)
|
|
226
|
+
events.push({ type: "text", text: rest });
|
|
197
227
|
}
|
|
198
228
|
else if (callStarted) {
|
|
199
229
|
endCall(events, true);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"recovery-stream.js","sourceRoot":"","sources":["../../../../src/tool-call-middleware/protocols/kimi-xtml/recovery-stream.ts"],"names":[],"mappings":"AAGA,OAAO,EACN,oBAAoB,EACpB,mBAAmB,EACnB,mBAAmB,EACnB,kBAAkB,EAClB,eAAe,EACf,cAAc,EACd,QAAQ,EACR,gBAAgB,EAChB,eAAe,GACf,MAAM,cAAc,CAAC;AACtB,OAAO,EAAE,uBAAuB,EAA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Event } from \"../../types.ts\";\nimport type { RecoveryStreamParser } from \"../anthropic-xml/recovery-stream.ts\";\nimport {\n\tgetPartialXtmlSuffix,\n\tparseXtmlAttributes,\n\tXTML_ARGUMENT_CLOSE,\n\tXTML_ARGUMENT_OPEN,\n\tXTML_CALL_CLOSE,\n\tXTML_CALL_OPEN,\n\tXTML_SEP,\n\tXTML_TOOLS_CLOSE,\n\tXTML_TOOLS_OPEN,\n} from \"./markers.ts\";\nimport { coerceXtmlArgumentValue } from \"./parse.ts\";\n\ntype ParserMode =\n\t| \"text\"\n\t| \"tools\"\n\t| \"call-header\"\n\t| \"call-body\"\n\t| \"argument-header\"\n\t| \"argument-value\"\n\t| \"discard-call\";\n\nconst CHANNEL_MARKER_PATTERN = /^<\\|(?:open|close)\\|>(?:[a-zA-Z_][a-zA-Z0-9_]*)?<\\|sep\\|>/;\nconst OPEN_PREFIX = \"<|open|>\";\nconst CLOSE_PREFIX = \"<|close|>\";\n\nfunction couldBeMarkerPrefix(tail: string): boolean {\n\tif (OPEN_PREFIX.startsWith(tail) || CLOSE_PREFIX.startsWith(tail)) return true;\n\tfor (const prefix of [OPEN_PREFIX, CLOSE_PREFIX]) {\n\t\tif (!tail.startsWith(prefix)) continue;\n\t\tconst rest = tail.slice(prefix.length);\n\t\tconst angleIndex = rest.indexOf(\"<\");\n\t\tif (angleIndex === -1) return /^[a-zA-Z0-9_]*$/.test(rest);\n\t\tif (!/^(?:[a-zA-Z_][a-zA-Z0-9_]*)?$/.test(rest.slice(0, angleIndex))) return false;\n\t\treturn XTML_SEP.startsWith(rest.slice(angleIndex));\n\t}\n\treturn false;\n}\n\nexport function createXtmlRecoveryStreamParser(tools: readonly Tool[], options?: ParserOptions): RecoveryStreamParser {\n\tlet buffer = \"\";\n\tlet mode: ParserMode = \"text\";\n\tlet callIndex = -1;\n\tlet callName = \"\";\n\tlet callArgs: Record<string, unknown> = {};\n\tlet callStarted = false;\n\tlet callInvalidReason: string | null = null;\n\tlet argumentKey = \"\";\n\tlet argumentType: string | undefined;\n\n\tfunction resetCall(): void {\n\t\tcallName = \"\";\n\t\tcallArgs = {};\n\t\tcallStarted = false;\n\t\tcallInvalidReason = null;\n\t\targumentKey = \"\";\n\t\targumentType = undefined;\n\t}\n\n\tfunction endCall(events: StreamParserEvent[], incomplete: boolean): void {\n\t\tif (!callStarted) {\n\t\t\tresetCall();\n\t\t\treturn;\n\t\t}\n\t\tconst event: StreamParserEvent = {\n\t\t\ttype: \"toolcall_end\",\n\t\t\tindex: callIndex,\n\t\t\tname: callName,\n\t\t\tid: `recovered-xtml-${callIndex}`,\n\t\t\targuments: callArgs,\n\t\t};\n\t\tif (incomplete) {\n\t\t\tevent.incomplete = true;\n\t\t\tif (callInvalidReason) event.errorMessage = callInvalidReason;\n\t\t}\n\t\tevents.push(event);\n\t\tresetCall();\n\t\tmode = \"tools\";\n\t}\n\n\tfunction processText(events: StreamParserEvent[]): boolean {\n\t\tconst openIndex = buffer.indexOf(OPEN_PREFIX);\n\t\tconst closeIndex = buffer.indexOf(CLOSE_PREFIX);\n\t\tconst markerIndex = [openIndex, closeIndex].filter((index) => index !== -1).sort((a, b) => a - b)[0];\n\t\tif (markerIndex === undefined) {\n\t\t\tconst partial = getPartialXtmlSuffix(buffer, [OPEN_PREFIX, CLOSE_PREFIX]);\n\t\t\tconst flushable = partial ? buffer.slice(0, -partial.length) : buffer;\n\t\t\tif (flushable) events.push({ type: \"text\", text: flushable });\n\t\t\tbuffer = partial;\n\t\t\treturn false;\n\t\t}\n\t\tif (markerIndex > 0) {\n\t\t\tevents.push({ type: \"text\", text: buffer.slice(0, markerIndex) });\n\t\t\tbuffer = buffer.slice(markerIndex);\n\t\t\treturn true;\n\t\t}\n\t\tconst markerMatch = CHANNEL_MARKER_PATTERN.exec(buffer);\n\t\tif (markerMatch) {\n\t\t\tconst marker = markerMatch[0];\n\t\t\tbuffer = buffer.slice(marker.length);\n\t\t\tif (marker === XTML_TOOLS_OPEN) mode = \"tools\";\n\t\t\treturn true;\n\t\t}\n\t\tif (couldBeMarkerPrefix(buffer)) return false;\n\t\tevents.push({ type: \"text\", text: buffer.slice(0, 2) });\n\t\tbuffer = buffer.slice(2);\n\t\treturn true;\n\t}\n\n\tfunction process(events: StreamParserEvent[]): void {\n\t\tfor (;;) {\n\t\t\tif (mode === \"text\") {\n\t\t\t\tif (!processText(events)) return;\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tif (mode === \"tools\") {\n\t\t\t\tconst callStart = buffer.indexOf(XTML_CALL_OPEN);\n\t\t\t\tconst toolsEnd = buffer.indexOf(XTML_TOOLS_CLOSE);\n\t\t\t\tif (toolsEnd !== -1 && (callStart === -1 || toolsEnd < callStart)) {\n\t\t\t\t\tbuffer = buffer.slice(toolsEnd + XTML_TOOLS_CLOSE.length);\n\t\t\t\t\tmode = \"text\";\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\t\t\t\tif (callStart === -1) return;\n\t\t\t\tbuffer = buffer.slice(callStart + XTML_CALL_OPEN.length);\n\t\t\t\tmode = \"call-header\";\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tif (mode === \"call-header\" || mode === \"argument-header\") {\n\t\t\t\tconst sepIndex = buffer.indexOf(XTML_SEP);\n\t\t\t\tif (sepIndex === -1) return;\n\t\t\t\tconst attributes = parseXtmlAttributes(buffer.slice(0, sepIndex));\n\t\t\t\tbuffer = buffer.slice(sepIndex + XTML_SEP.length);\n\t\t\t\tif (mode === \"call-header\") {\n\t\t\t\t\tconst name = attributes.tool ?? \"\";\n\t\t\t\t\tif (!tools.some((candidate) => candidate.name === name)) {\n\t\t\t\t\t\toptions?.onError?.(`kimi-xtml recovery: call for unknown tool \"${name}\".`, {});\n\t\t\t\t\t\tmode = \"discard-call\";\n\t\t\t\t\t\tcontinue;\n\t\t\t\t\t}\n\t\t\t\t\tcallIndex += 1;\n\t\t\t\t\tcallName = name;\n\t\t\t\t\tcallStarted = true;\n\t\t\t\t\tevents.push({ type: \"toolcall_start\", index: callIndex, name, id: `recovered-xtml-${callIndex}` });\n\t\t\t\t\tmode = \"call-body\";\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\t\t\t\targumentKey = attributes.key ?? \"\";\n\t\t\t\targumentType = attributes.type;\n\t\t\t\tmode = \"argument-value\";\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tif (mode === \"call-body\") {\n\t\t\t\tconst argStart = buffer.indexOf(XTML_ARGUMENT_OPEN);\n\t\t\t\tconst callEnd = buffer.indexOf(XTML_CALL_CLOSE);\n\t\t\t\tif (callEnd !== -1 && (argStart === -1 || callEnd < argStart)) {\n\t\t\t\t\tbuffer = buffer.slice(callEnd + XTML_CALL_CLOSE.length);\n\t\t\t\t\tendCall(events, callInvalidReason !== null);\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\t\t\t\tif (argStart === -1) return;\n\t\t\t\tbuffer = buffer.slice(argStart + XTML_ARGUMENT_OPEN.length);\n\t\t\t\tmode = \"argument-header\";\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tif (mode === \"argument-value\") {\n\t\t\t\tconst valueEnd = buffer.indexOf(XTML_ARGUMENT_CLOSE);\n\t\t\t\tif (valueEnd === -1) return;\n\t\t\t\tconst coerced = coerceXtmlArgumentValue(buffer.slice(0, valueEnd), argumentType);\n\t\t\t\tbuffer = buffer.slice(valueEnd + XTML_ARGUMENT_CLOSE.length);\n\t\t\t\tif (!coerced.ok) {\n\t\t\t\t\tcallInvalidReason = `kimi-xtml recovery: invalid value for argument \"${argumentKey}\".`;\n\t\t\t\t\toptions?.onError?.(callInvalidReason, { toolCall: callName });\n\t\t\t\t} else if (argumentKey) {\n\t\t\t\t\tcallArgs[argumentKey] = coerced.value;\n\t\t\t\t\tevents.push({ type: \"toolcall_delta\", index: callIndex, argumentsDelta: JSON.stringify(callArgs) });\n\t\t\t\t}\n\t\t\t\tmode = \"call-body\";\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tconst callEnd = buffer.indexOf(XTML_CALL_CLOSE);\n\t\t\tif (callEnd === -1) return;\n\t\t\tbuffer = buffer.slice(callEnd + XTML_CALL_CLOSE.length);\n\t\t\tresetCall();\n\t\t\tmode = \"tools\";\n\t\t}\n\t}\n\n\treturn {\n\t\tfeed(textDelta: string): StreamParserEvent[] {\n\t\t\tbuffer += textDelta;\n\t\t\tconst events: StreamParserEvent[] = [];\n\t\t\tprocess(events);\n\t\t\treturn events;\n\t\t},\n\t\tinterrupt(): StreamParserEvent[] {\n\t\t\tconst events: StreamParserEvent[] = [];\n\t\t\tif (mode === \"text\" && buffer) {\n\t\t\t\tevents.push({ type: \"text\", text: buffer });\n\t\t\t\tbuffer = \"\";\n\t\t\t}\n\t\t\treturn events;\n\t\t},\n\t\tfinish(): StreamParserEvent[] {\n\t\t\tconst events: StreamParserEvent[] = [];\n\t\t\tif (mode === \"text\") {\n\t\t\t\tif (buffer) events.push({ type: \"text\", text: buffer });\n\t\t\t} else if (callStarted) {\n\t\t\t\tendCall(events, true);\n\t\t\t}\n\t\t\tbuffer = \"\";\n\t\t\tmode = \"text\";\n\t\t\treturn events;\n\t\t},\n\t};\n}\n"]}
|
|
1
|
+
{"version":3,"file":"recovery-stream.js","sourceRoot":"","sources":["../../../../src/tool-call-middleware/protocols/kimi-xtml/recovery-stream.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,sBAAsB,EAAE,MAAM,6BAA6B,CAAC;AAGrE,OAAO,EACN,oBAAoB,EACpB,sBAAsB,EACtB,mBAAmB,EACnB,mBAAmB,EACnB,kBAAkB,EAClB,eAAe,EACf,cAAc,EACd,iBAAiB,EACjB,gBAAgB,EAChB,QAAQ,EACR,gBAAgB,EAChB,eAAe,GACf,MAAM,cAAc,CAAC;AACtB,OAAO,EAAE,uBAAuB,EAAE,MAAM,YAAY,CAAC;AAWrD,MAAM,eAAe,GAAG,CAAC,gBAAgB,EAAE,iBAAiB,EAAE,QAAQ,CAAU,CAAC;AAEjF,SAAS,eAAe,CAAC,IAAY;IACpC,OAAO,eAA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AE,CAAC,CAAC;gBAC5C,MAAM,GAAG,EAAE,CAAC;YACb,CAAC;YACD,OAAO,MAAM,CAAC;QACf,CAAC;QACD,MAAM;YACL,MAAM,MAAM,GAAwB,OAAO,CAAC,IAAI,CAAC,MAAM,EAAE,CAAC,CAAC;YAC3D,IAAI,IAAI,KAAK,MAAM,EAAE,CAAC;gBACrB,IAAI,IAAI,GAAG,MAAM,CAAC;gBAClB,SAAS,CAAC;oBACT,MAAM,MAAM,GAAG,sBAAsB,CAAC,IAAI,CAAC,CAAC;oBAC5C,IAAI,CAAC,MAAM;wBAAE,MAAM;oBACnB,IAAI,GAAG,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;gBAClC,CAAC;gBACD,IAAI,QAAQ,CAAC,UAAU,CAAC,IAAI,CAAC,IAAI,IAAI,CAAC,MAAM,GAAG,CAAC;oBAAE,IAAI,GAAG,EAAE,CAAC;gBAC5D,IAAI,IAAI;oBAAE,MAAM,CAAC,IAAI,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,IAAI,EAAE,CAAC,CAAC;YACrD,CAAC;iBAAM,IAAI,WAAW,EAAE,CAAC;gBACxB,OAAO,CAAC,MAAM,EAAE,IAAI,CAAC,CAAC;YACvB,CAAC;YACD,MAAM,GAAG,EAAE,CAAC;YACZ,IAAI,GAAG,MAAM,CAAC;YACd,OAAO,MAAM,CAAC;QACf,CAAC;KACD,CAAC;AACH,CAAC","sourcesContent":["import type { Tool } from \"../../../types.ts\";\nimport { createRecoveryCodeMask } from \"../../recovery-code-mask.ts\";\nimport type { ParserOptions, StreamParserEvent } from \"../../types.ts\";\nimport type { RecoveryStreamParser } from \"../anthropic-xml/recovery-stream.ts\";\nimport {\n\tgetPartialXtmlSuffix,\n\tmatchXtmlChannelMarker,\n\tparseXtmlAttributes,\n\tXTML_ARGUMENT_CLOSE,\n\tXTML_ARGUMENT_OPEN,\n\tXTML_CALL_CLOSE,\n\tXTML_CALL_OPEN,\n\tXTML_CLOSE_PREFIX,\n\tXTML_OPEN_PREFIX,\n\tXTML_SEP,\n\tXTML_TOOLS_CLOSE,\n\tXTML_TOOLS_OPEN,\n} from \"./markers.ts\";\nimport { coerceXtmlArgumentValue } from \"./parse.ts\";\n\ntype ParserMode =\n\t| \"text\"\n\t| \"tools\"\n\t| \"call-header\"\n\t| \"call-body\"\n\t| \"argument-header\"\n\t| \"argument-value\"\n\t| \"discard-call\";\n\nconst MARKER_PREFIXES = [XTML_OPEN_PREFIX, XTML_CLOSE_PREFIX, XTML_SEP] as const;\n\nfunction findMarkerStart(text: string): number | undefined {\n\treturn MARKER_PREFIXES.map((prefix) => text.indexOf(prefix))\n\t\t.filter((index) => index !== -1)\n\t\t.sort((a, b) => a - b)[0];\n}\n\nfunction couldExtendMarker(buffer: string, marker: string): boolean {\n\tif (marker.endsWith(XTML_SEP)) return false;\n\tconst rest = buffer.slice(marker.length);\n\treturn rest.length === 0 || XTML_SEP.startsWith(rest);\n}\n\nconst STRUCTURAL_MARKER_OPENS = [XTML_CALL_OPEN, XTML_ARGUMENT_OPEN] as const;\n\nfunction startsCompleteStructuralMarker(buffer: string): boolean {\n\treturn STRUCTURAL_MARKER_OPENS.some((open) => buffer.startsWith(open));\n}\n\nfunction startsPartialStructuralMarker(buffer: string): boolean {\n\treturn STRUCTURAL_MARKER_OPENS.some((open) => buffer.length < open.length && open.startsWith(buffer));\n}\n\nexport function createXtmlRecoveryStreamParser(tools: readonly Tool[], options?: ParserOptions): RecoveryStreamParser {\n\tconst mask = createRecoveryCodeMask();\n\tlet buffer = \"\";\n\tlet mode: ParserMode = \"text\";\n\tlet callIndex = -1;\n\tlet callName = \"\";\n\tlet callArgs: Record<string, unknown> = {};\n\tlet callStarted = false;\n\tlet callInvalidReason: string | null = null;\n\tlet argumentKey = \"\";\n\tlet argumentType: string | undefined;\n\n\tfunction resetCall(): void {\n\t\tcallName = \"\";\n\t\tcallArgs = {};\n\t\tcallStarted = false;\n\t\tcallInvalidReason = null;\n\t\targumentKey = \"\";\n\t\targumentType = undefined;\n\t}\n\n\tfunction endCall(events: StreamParserEvent[], incomplete: boolean): void {\n\t\tif (!callStarted) {\n\t\t\tresetCall();\n\t\t\treturn;\n\t\t}\n\t\tconst event: StreamParserEvent = {\n\t\t\ttype: \"toolcall_end\",\n\t\t\tindex: callIndex,\n\t\t\tname: callName,\n\t\t\tid: `recovered-xtml-${callIndex}`,\n\t\t\targuments: callArgs,\n\t\t};\n\t\tif (incomplete) {\n\t\t\tevent.incomplete = true;\n\t\t\tif (callInvalidReason) event.errorMessage = callInvalidReason;\n\t\t}\n\t\tevents.push(event);\n\t\tresetCall();\n\t\tmode = \"tools\";\n\t}\n\n\tfunction processText(events: StreamParserEvent[]): boolean {\n\t\tconst markerIndex = findMarkerStart(buffer);\n\t\tif (markerIndex === undefined) {\n\t\t\tconst partial = getPartialXtmlSuffix(buffer, MARKER_PREFIXES);\n\t\t\tconst flushable = partial ? buffer.slice(0, -partial.length) : buffer;\n\t\t\tif (flushable) events.push({ type: \"text\", text: flushable });\n\t\t\tbuffer = partial;\n\t\t\treturn false;\n\t\t}\n\t\tif (markerIndex > 0) {\n\t\t\tevents.push({ type: \"text\", text: buffer.slice(0, markerIndex) });\n\t\t\tbuffer = buffer.slice(markerIndex);\n\t\t\treturn true;\n\t\t}\n\t\tif (startsCompleteStructuralMarker(buffer)) {\n\t\t\tmode = \"tools\";\n\t\t\treturn true;\n\t\t}\n\t\tif (startsPartialStructuralMarker(buffer)) return false;\n\t\tconst marker = matchXtmlChannelMarker(buffer);\n\t\tif (marker) {\n\t\t\tif (couldExtendMarker(buffer, marker)) return false;\n\t\t\tbuffer = buffer.slice(marker.length);\n\t\t\tif (marker === XTML_TOOLS_OPEN) mode = \"tools\";\n\t\t\treturn true;\n\t\t}\n\t\tevents.push({ type: \"text\", text: buffer.slice(0, 2) });\n\t\tbuffer = buffer.slice(2);\n\t\treturn true;\n\t}\n\n\tfunction process(events: StreamParserEvent[]): void {\n\t\tfor (;;) {\n\t\t\tif (mode === \"text\") {\n\t\t\t\tif (!processText(events)) return;\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tif (mode === \"tools\") {\n\t\t\t\tconst callStart = buffer.indexOf(XTML_CALL_OPEN);\n\t\t\t\tconst toolsEnd = buffer.indexOf(XTML_TOOLS_CLOSE);\n\t\t\t\tif (toolsEnd !== -1 && (callStart === -1 || toolsEnd < callStart)) {\n\t\t\t\t\tbuffer = buffer.slice(toolsEnd + XTML_TOOLS_CLOSE.length);\n\t\t\t\t\tmode = \"text\";\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\t\t\t\tif (callStart === -1) return;\n\t\t\t\tbuffer = buffer.slice(callStart + XTML_CALL_OPEN.length);\n\t\t\t\tmode = \"call-header\";\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tif (mode === \"call-header\" || mode === \"argument-header\") {\n\t\t\t\tconst sepIndex = buffer.indexOf(XTML_SEP);\n\t\t\t\tif (sepIndex === -1) return;\n\t\t\t\tconst attributes = parseXtmlAttributes(buffer.slice(0, sepIndex));\n\t\t\t\tbuffer = buffer.slice(sepIndex + XTML_SEP.length);\n\t\t\t\tif (mode === \"call-header\") {\n\t\t\t\t\tconst name = attributes.tool ?? \"\";\n\t\t\t\t\tif (!tools.some((candidate) => candidate.name === name)) {\n\t\t\t\t\t\toptions?.onError?.(`kimi-xtml recovery: call for unknown tool \"${name}\".`, {});\n\t\t\t\t\t\tmode = \"discard-call\";\n\t\t\t\t\t\tcontinue;\n\t\t\t\t\t}\n\t\t\t\t\tcallIndex += 1;\n\t\t\t\t\tcallName = name;\n\t\t\t\t\tcallStarted = true;\n\t\t\t\t\tevents.push({ type: \"toolcall_start\", index: callIndex, name, id: `recovered-xtml-${callIndex}` });\n\t\t\t\t\tmode = \"call-body\";\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\t\t\t\targumentKey = attributes.key ?? \"\";\n\t\t\t\targumentType = attributes.type;\n\t\t\t\tmode = \"argument-value\";\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tif (mode === \"call-body\") {\n\t\t\t\tconst argStart = buffer.indexOf(XTML_ARGUMENT_OPEN);\n\t\t\t\tconst callEnd = buffer.indexOf(XTML_CALL_CLOSE);\n\t\t\t\tif (callEnd !== -1 && (argStart === -1 || callEnd < argStart)) {\n\t\t\t\t\tbuffer = buffer.slice(callEnd + XTML_CALL_CLOSE.length);\n\t\t\t\t\tendCall(events, callInvalidReason !== null);\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\t\t\t\tif (argStart === -1) return;\n\t\t\t\tbuffer = buffer.slice(argStart + XTML_ARGUMENT_OPEN.length);\n\t\t\t\tmode = \"argument-header\";\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tif (mode === \"argument-value\") {\n\t\t\t\tconst valueEnd = buffer.indexOf(XTML_ARGUMENT_CLOSE);\n\t\t\t\tif (valueEnd === -1) return;\n\t\t\t\tconst coerced = coerceXtmlArgumentValue(buffer.slice(0, valueEnd), argumentType);\n\t\t\t\tbuffer = buffer.slice(valueEnd + XTML_ARGUMENT_CLOSE.length);\n\t\t\t\tif (!coerced.ok) {\n\t\t\t\t\tcallInvalidReason = `kimi-xtml recovery: invalid value for argument \"${argumentKey}\".`;\n\t\t\t\t\toptions?.onError?.(callInvalidReason, { toolCall: callName });\n\t\t\t\t} else if (argumentKey) {\n\t\t\t\t\tcallArgs[argumentKey] = coerced.value;\n\t\t\t\t\tevents.push({ type: \"toolcall_delta\", index: callIndex, argumentsDelta: JSON.stringify(callArgs) });\n\t\t\t\t}\n\t\t\t\tmode = \"call-body\";\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tconst callEnd = buffer.indexOf(XTML_CALL_CLOSE);\n\t\t\tif (callEnd === -1) return;\n\t\t\tbuffer = buffer.slice(callEnd + XTML_CALL_CLOSE.length);\n\t\t\tresetCall();\n\t\t\tmode = \"tools\";\n\t\t}\n\t}\n\n\tfunction consume(segments: readonly { readonly text: string; readonly scan: boolean }[]): StreamParserEvent[] {\n\t\tconst events: StreamParserEvent[] = [];\n\t\tfor (const segment of segments) {\n\t\t\tif (segment.scan) {\n\t\t\t\tbuffer += segment.text;\n\t\t\t\tprocess(events);\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tif (mode === \"text\" && buffer) {\n\t\t\t\tevents.push({ type: \"text\", text: buffer });\n\t\t\t\tbuffer = \"\";\n\t\t\t}\n\t\t\tif (mode === \"text\") events.push({ type: \"text\", text: segment.text });\n\t\t\telse buffer += segment.text;\n\t\t}\n\t\treturn events;\n\t}\n\n\treturn {\n\t\tfeed(textDelta: string): StreamParserEvent[] {\n\t\t\treturn consume(mask.feed(textDelta));\n\t\t},\n\t\tinterrupt(): StreamParserEvent[] {\n\t\t\tconst events: StreamParserEvent[] = [];\n\t\t\tif (mode === \"text\" && buffer) {\n\t\t\t\tevents.push({ type: \"text\", text: buffer });\n\t\t\t\tbuffer = \"\";\n\t\t\t}\n\t\t\treturn events;\n\t\t},\n\t\tfinish(): StreamParserEvent[] {\n\t\t\tconst events: StreamParserEvent[] = consume(mask.finish());\n\t\t\tif (mode === \"text\") {\n\t\t\t\tlet rest = buffer;\n\t\t\t\tfor (;;) {\n\t\t\t\t\tconst marker = matchXtmlChannelMarker(rest);\n\t\t\t\t\tif (!marker) break;\n\t\t\t\t\trest = rest.slice(marker.length);\n\t\t\t\t}\n\t\t\t\tif (XTML_SEP.startsWith(rest) && rest.length > 0) rest = \"\";\n\t\t\t\tif (rest) events.push({ type: \"text\", text: rest });\n\t\t\t} else if (callStarted) {\n\t\t\t\tendCall(events, true);\n\t\t\t}\n\t\t\tbuffer = \"\";\n\t\t\tmode = \"text\";\n\t\t\treturn events;\n\t\t},\n\t};\n}\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"thinking-recovery.d.ts","sourceRoot":"","sources":["../../../../src/tool-call-middleware/protocols/kimi-xtml/thinking-recovery.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAmB,MAAM,mBAAmB,CAAC;
|
|
1
|
+
{"version":3,"file":"thinking-recovery.d.ts","sourceRoot":"","sources":["../../../../src/tool-call-middleware/protocols/kimi-xtml/thinking-recovery.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAmB,MAAM,mBAAmB,CAAC;AAiF3E,wBAAgB,uBAAuB,CAAC,OAAO,EAAE,gBAAgB,GAAG,gBAAgB,CAwCnF"}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { createRecoveryCodeMask } from "../../recovery-code-mask.js";
|
|
2
|
-
|
|
2
|
+
import { XTML_CHANNEL_MARKER_PATTERN } from "./markers.js";
|
|
3
|
+
const NAMED_MARKER_PATTERN = /^<\|(open|close)\|>([a-zA-Z_][a-zA-Z0-9_]*)?/;
|
|
3
4
|
function recoverThinkingContent(input) {
|
|
4
5
|
const mask = createRecoveryCodeMask();
|
|
5
6
|
let channel = "thinking";
|
|
@@ -15,13 +16,14 @@ function recoverThinkingContent(input) {
|
|
|
15
16
|
};
|
|
16
17
|
const scan = (text) => {
|
|
17
18
|
let offset = 0;
|
|
18
|
-
for (const match of text.matchAll(
|
|
19
|
+
for (const match of text.matchAll(XTML_CHANNEL_MARKER_PATTERN)) {
|
|
19
20
|
const index = match.index;
|
|
20
21
|
if (index === undefined)
|
|
21
22
|
continue;
|
|
22
23
|
append(text.slice(offset, index));
|
|
23
|
-
const
|
|
24
|
-
const
|
|
24
|
+
const named = NAMED_MARKER_PATTERN.exec(match[0]);
|
|
25
|
+
const action = named?.[1];
|
|
26
|
+
const name = named?.[2];
|
|
25
27
|
changed = true;
|
|
26
28
|
if (action === "open" && name === "response") {
|
|
27
29
|
channel = "response";
|
|
@@ -48,17 +50,41 @@ function recoverThinkingContent(input) {
|
|
|
48
50
|
function recoveredThinkingBlock(block, thinking) {
|
|
49
51
|
return { ...block, thinking };
|
|
50
52
|
}
|
|
53
|
+
function strippedVisibleText(input) {
|
|
54
|
+
const mask = createRecoveryCodeMask();
|
|
55
|
+
let text = "";
|
|
56
|
+
let changed = false;
|
|
57
|
+
for (const segment of [...mask.feed(input), ...mask.finish()]) {
|
|
58
|
+
if (!segment.scan) {
|
|
59
|
+
text += segment.text;
|
|
60
|
+
continue;
|
|
61
|
+
}
|
|
62
|
+
const stripped = segment.text.replace(XTML_CHANNEL_MARKER_PATTERN, "");
|
|
63
|
+
if (stripped !== segment.text)
|
|
64
|
+
changed = true;
|
|
65
|
+
text += stripped;
|
|
66
|
+
}
|
|
67
|
+
return { text, changed };
|
|
68
|
+
}
|
|
51
69
|
export function recoverKimiXtmlThinking(message) {
|
|
52
70
|
let changed = false;
|
|
71
|
+
let thinkingChanged = false;
|
|
53
72
|
let recoveredResponse = false;
|
|
54
73
|
const content = [];
|
|
55
74
|
for (const block of message.content) {
|
|
75
|
+
if (block.type === "text") {
|
|
76
|
+
const stripped = strippedVisibleText(block.text);
|
|
77
|
+
changed = changed || stripped.changed;
|
|
78
|
+
content.push(stripped.changed ? { ...block, text: stripped.text } : block);
|
|
79
|
+
continue;
|
|
80
|
+
}
|
|
56
81
|
if (block.type !== "thinking") {
|
|
57
82
|
content.push(block);
|
|
58
83
|
continue;
|
|
59
84
|
}
|
|
60
85
|
const recovered = recoverThinkingContent(block.thinking);
|
|
61
86
|
changed = changed || recovered.changed;
|
|
87
|
+
thinkingChanged = thinkingChanged || recovered.changed;
|
|
62
88
|
recoveredResponse = recoveredResponse || recovered.recoveredResponse;
|
|
63
89
|
content.push(recoveredThinkingBlock(block, recovered.thinking));
|
|
64
90
|
if (recovered.response.length > 0)
|
|
@@ -69,14 +95,16 @@ export function recoverKimiXtmlThinking(message) {
|
|
|
69
95
|
return {
|
|
70
96
|
...message,
|
|
71
97
|
content,
|
|
72
|
-
diagnostics:
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
98
|
+
diagnostics: thinkingChanged
|
|
99
|
+
? [
|
|
100
|
+
...(message.diagnostics ?? []),
|
|
101
|
+
{
|
|
102
|
+
type: "kimi_xtml_thinking_recovery",
|
|
103
|
+
timestamp: Date.now(),
|
|
104
|
+
details: { recoveredResponse },
|
|
105
|
+
},
|
|
106
|
+
]
|
|
107
|
+
: message.diagnostics,
|
|
80
108
|
};
|
|
81
109
|
}
|
|
82
110
|
//# sourceMappingURL=thinking-recovery.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"thinking-recovery.js","sourceRoot":"","sources":["../../../../src/tool-call-middleware/protocols/kimi-xtml/thinking-recovery.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,sBAAsB,EAAE,MAAM,6BAA6B,CAAC;
|
|
1
|
+
{"version":3,"file":"thinking-recovery.js","sourceRoot":"","sources":["../../../../src/tool-call-middleware/protocols/kimi-xtml/thinking-recovery.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,sBAAsB,EAAE,MAAM,6BAA6B,CAAC;AACrE,OAAO,EAAE,2BAA2B,EAAE,MAAM,cAAc,CAAC;AAW3D,MAAM,oBAAoB,GAAG,8CAA8C,CAAC;AAE5E,SAAS,sBAAsB,CAAC,KAAa;IAC5C,MAAM,IAAI,GAAG,sBAAsB,EAAE,CAAC;IACtC,IAAI,OAAO,GAAkB,UAAU,CAAC;IACxC,IAAI,QAAQ,GAAG,EAAE,CAAC;IAClB,IAAI,QAAQ,GAAG,EAAE,CAAC;IAClB,IAAI,OAAO,GAAG,KAAK,CAAC;IACpB,IAAI,iBAAiB,GAAG,KAAK,CAAC;IAE9B,MAAM,MAAM,GAAG,CAAC,IAAY,EAAQ,EAAE;QACrC,IAAI,OAAO,KAAK,UAAU;YAAE,QAAQ,IAAI,IAAI,CAAC;;YACxC,QAAQ,IAAI,IAAI,CAAC;IACvB,CAAC,CAAC;IAEF,MAAM,IAAI,GAAG,CAAC,IAAY,EAAQ,EAAE;QACnC,IAAI,MAAM,GAAG,CAAC,CAAC;QACf,KA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AAE,iBAAiB,EAAE;iBAC9B;aACD;YACF,CAAC,CAAC,OAAO,CAAC,WAAW;KACtB,CAAC;AACH,CAAC","sourcesContent":["import type { AssistantMessage, ThinkingContent } from \"../../../types.ts\";\nimport { createRecoveryCodeMask } from \"../../recovery-code-mask.ts\";\nimport { XTML_CHANNEL_MARKER_PATTERN } from \"./markers.ts\";\n\ntype OutputChannel = \"thinking\" | \"response\";\n\ntype RecoveredThinking = {\n\treadonly thinking: string;\n\treadonly response: string;\n\treadonly changed: boolean;\n\treadonly recoveredResponse: boolean;\n};\n\nconst NAMED_MARKER_PATTERN = /^<\\|(open|close)\\|>([a-zA-Z_][a-zA-Z0-9_]*)?/;\n\nfunction recoverThinkingContent(input: string): RecoveredThinking {\n\tconst mask = createRecoveryCodeMask();\n\tlet channel: OutputChannel = \"thinking\";\n\tlet thinking = \"\";\n\tlet response = \"\";\n\tlet changed = false;\n\tlet recoveredResponse = false;\n\n\tconst append = (text: string): void => {\n\t\tif (channel === \"response\") response += text;\n\t\telse thinking += text;\n\t};\n\n\tconst scan = (text: string): void => {\n\t\tlet offset = 0;\n\t\tfor (const match of text.matchAll(XTML_CHANNEL_MARKER_PATTERN)) {\n\t\t\tconst index = match.index;\n\t\t\tif (index === undefined) continue;\n\t\t\tappend(text.slice(offset, index));\n\t\t\tconst named = NAMED_MARKER_PATTERN.exec(match[0]);\n\t\t\tconst action = named?.[1];\n\t\t\tconst name = named?.[2];\n\t\t\tchanged = true;\n\t\t\tif (action === \"open\" && name === \"response\") {\n\t\t\t\tchannel = \"response\";\n\t\t\t\trecoveredResponse = true;\n\t\t\t} else if (action === \"open\" && name === \"think\") {\n\t\t\t\tchannel = \"thinking\";\n\t\t\t} else if (action === \"close\" && name === \"response\") {\n\t\t\t\tchannel = \"thinking\";\n\t\t\t}\n\t\t\toffset = index + match[0].length;\n\t\t}\n\t\tappend(text.slice(offset));\n\t};\n\n\tfor (const segment of [...mask.feed(input), ...mask.finish()]) {\n\t\tif (segment.scan) scan(segment.text);\n\t\telse append(segment.text);\n\t}\n\n\treturn { thinking, response, changed, recoveredResponse };\n}\n\nfunction recoveredThinkingBlock(block: ThinkingContent, thinking: string): ThinkingContent {\n\treturn { ...block, thinking };\n}\n\nfunction strippedVisibleText(input: string): { readonly text: string; readonly changed: boolean } {\n\tconst mask = createRecoveryCodeMask();\n\tlet text = \"\";\n\tlet changed = false;\n\n\tfor (const segment of [...mask.feed(input), ...mask.finish()]) {\n\t\tif (!segment.scan) {\n\t\t\ttext += segment.text;\n\t\t\tcontinue;\n\t\t}\n\t\tconst stripped = segment.text.replace(XTML_CHANNEL_MARKER_PATTERN, \"\");\n\t\tif (stripped !== segment.text) changed = true;\n\t\ttext += stripped;\n\t}\n\n\treturn { text, changed };\n}\n\nexport function recoverKimiXtmlThinking(message: AssistantMessage): AssistantMessage {\n\tlet changed = false;\n\tlet thinkingChanged = false;\n\tlet recoveredResponse = false;\n\tconst content: AssistantMessage[\"content\"] = [];\n\n\tfor (const block of message.content) {\n\t\tif (block.type === \"text\") {\n\t\t\tconst stripped = strippedVisibleText(block.text);\n\t\t\tchanged = changed || stripped.changed;\n\t\t\tcontent.push(stripped.changed ? { ...block, text: stripped.text } : block);\n\t\t\tcontinue;\n\t\t}\n\t\tif (block.type !== \"thinking\") {\n\t\t\tcontent.push(block);\n\t\t\tcontinue;\n\t\t}\n\t\tconst recovered = recoverThinkingContent(block.thinking);\n\t\tchanged = changed || recovered.changed;\n\t\tthinkingChanged = thinkingChanged || recovered.changed;\n\t\trecoveredResponse = recoveredResponse || recovered.recoveredResponse;\n\t\tcontent.push(recoveredThinkingBlock(block, recovered.thinking));\n\t\tif (recovered.response.length > 0) content.push({ type: \"text\", text: recovered.response });\n\t}\n\n\tif (!changed) return message;\n\treturn {\n\t\t...message,\n\t\tcontent,\n\t\tdiagnostics: thinkingChanged\n\t\t\t? [\n\t\t\t\t\t...(message.diagnostics ?? []),\n\t\t\t\t\t{\n\t\t\t\t\t\ttype: \"kimi_xtml_thinking_recovery\",\n\t\t\t\t\t\ttimestamp: Date.now(),\n\t\t\t\t\t\tdetails: { recoveredResponse },\n\t\t\t\t\t},\n\t\t\t\t]\n\t\t\t: message.diagnostics,\n\t};\n}\n"]}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@earendil-works/pi-ai",
|
|
3
3
|
"private": true,
|
|
4
|
-
"version": "2026.8.
|
|
4
|
+
"version": "2026.8.24",
|
|
5
5
|
"description": "Unified LLM API with automatic model discovery and provider configuration",
|
|
6
6
|
"type": "module",
|
|
7
7
|
"main": "./dist/index.js",
|
|
@@ -72,9 +72,9 @@
|
|
|
72
72
|
},
|
|
73
73
|
"dependencies": {
|
|
74
74
|
"@anthropic-ai/sdk": "0.91.1",
|
|
75
|
-
"@aws-sdk/client-bedrock-runtime": "3.
|
|
75
|
+
"@aws-sdk/client-bedrock-runtime": "3.1116.0",
|
|
76
76
|
"@bufbuild/protobuf": "2.14.0",
|
|
77
|
-
"@earendil-works/pi-telemetry": "npm:@code-yeongyu/senpi-telemetry@2026.8.
|
|
77
|
+
"@earendil-works/pi-telemetry": "npm:@code-yeongyu/senpi-telemetry@2026.8.24",
|
|
78
78
|
"@google/genai": "2.18.0",
|
|
79
79
|
"@smithy/node-http-handler": "4.11.3",
|
|
80
80
|
"@smithy/types": "4.17.2",
|
|
@@ -82,7 +82,7 @@
|
|
|
82
82
|
"https-proxy-agent": "9.1.0",
|
|
83
83
|
"openai": "6.26.0",
|
|
84
84
|
"partial-json": "0.1.7",
|
|
85
|
-
"typebox": "1.3.
|
|
85
|
+
"typebox": "1.3.18",
|
|
86
86
|
"yaml": "2.9.0"
|
|
87
87
|
},
|
|
88
88
|
"keywords": [
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"terminal.d.ts","sourceRoot":"","sources":["../src/terminal.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"terminal.d.ts","sourceRoot":"","sources":["../src/terminal.ts"],"names":[],"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PIPE", "ENOTCONN"]);
|
|
15
|
+
// Bun's macOS tty shim can report synchronous ioctl EIO as raw positive errno 5 without a string code.
|
|
16
|
+
const EIO_ERRNO = 5;
|
|
15
17
|
const KEYBOARD_PROTOCOL_RESPONSE_FRAGMENT_TIMEOUT_MS = 150;
|
|
16
18
|
const KITTY_KEYBOARD_PROTOCOL_QUERY = `\x1b[>${DESIRED_KITTY_KEYBOARD_PROTOCOL_FLAGS}u\x1b[?u\x1b[c`;
|
|
17
19
|
export function parseKeyboardProtocolNegotiationSequence(sequence) {
|
|
@@ -45,10 +47,11 @@ export function keyboardEnhancementEnabled() {
|
|
|
45
47
|
return !["0", "false", "no", "off"].includes(value.toLowerCase());
|
|
46
48
|
}
|
|
47
49
|
function isDeadTerminalError(error) {
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
DEAD_TERMINAL_ERROR_CODES.has(error.code)
|
|
50
|
+
if (!(error instanceof Error))
|
|
51
|
+
return false;
|
|
52
|
+
if ("code" in error && typeof error.code === "string")
|
|
53
|
+
return DEAD_TERMINAL_ERROR_CODES.has(error.code);
|
|
54
|
+
return "errno" in error && error.errno === EIO_ERRNO;
|
|
52
55
|
}
|
|
53
56
|
const DEFAULT_ESCAPE_TIMEOUT_MS = 10;
|
|
54
57
|
const DEFAULT_SSH_ESCAPE_TIMEOUT_MS = 100;
|