@bitkyc08/opencodex 2.97.0 → 2.98.0
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/bin/ocx.mjs +1 -1
- package/gui/dist/assets/App-6TFtZsUF.css +1 -0
- package/gui/dist/assets/App-FQ0OtiMJ.js +59 -0
- package/gui/dist/assets/Tray-D2d5wqLq.js +1 -0
- package/gui/dist/assets/WantedSansVariable.split.0-Dwvb1WRx.woff2 +0 -0
- package/gui/dist/assets/WantedSansVariable.split.1-1wKD2RxI.woff2 +0 -0
- package/gui/dist/assets/WantedSansVariable.split.10-CAKA6LIP.woff2 +0 -0
- package/gui/dist/assets/WantedSansVariable.split.11-k5H85R4b.woff2 +0 -0
- package/gui/dist/assets/WantedSansVariable.split.12-BHboLsUv.woff2 +0 -0
- package/gui/dist/assets/WantedSansVariable.split.13-9GV377ix.woff2 +0 -0
- package/gui/dist/assets/WantedSansVariable.split.14-DovDXnrj.woff2 +0 -0
- package/gui/dist/assets/WantedSansVariable.split.15-DTdykCig.woff2 +0 -0
- package/gui/dist/assets/WantedSansVariable.split.16-CKz2ccgK.woff2 +0 -0
- package/gui/dist/assets/WantedSansVariable.split.17-Z2RD_KRZ.woff2 +0 -0
- package/gui/dist/assets/WantedSansVariable.split.18-BcKtfonJ.woff2 +0 -0
- package/gui/dist/assets/WantedSansVariable.split.19-DEQPOSZn.woff2 +0 -0
- package/gui/dist/assets/WantedSansVariable.split.2-DryboyuA.woff2 +0 -0
- package/gui/dist/assets/WantedSansVariable.split.20-Bia5JMls.woff2 +0 -0
- package/gui/dist/assets/WantedSansVariable.split.21-Cr4jFw-E.woff2 +0 -0
- package/gui/dist/assets/WantedSansVariable.split.22-DjjB69TZ.woff2 +0 -0
- package/gui/dist/assets/WantedSansVariable.split.23-CvUzJskt.woff2 +0 -0
- package/gui/dist/assets/WantedSansVariable.split.24-i_eMg5ox.woff2 +0 -0
- package/gui/dist/assets/WantedSansVariable.split.25-2aKHwPX-.woff2 +0 -0
- package/gui/dist/assets/WantedSansVariable.split.26-B6wXoCEL.woff2 +0 -0
- package/gui/dist/assets/WantedSansVariable.split.27-D4h1WfGM.woff2 +0 -0
- package/gui/dist/assets/WantedSansVariable.split.28-kvRPCStd.woff2 +0 -0
- package/gui/dist/assets/WantedSansVariable.split.29-DHAiwleD.woff2 +0 -0
- package/gui/dist/assets/WantedSansVariable.split.3-7H--eyZi.woff2 +0 -0
- package/gui/dist/assets/WantedSansVariable.split.30-C66cfAY2.woff2 +0 -0
- package/gui/dist/assets/WantedSansVariable.split.31-FDmtiNim.woff2 +0 -0
- package/gui/dist/assets/WantedSansVariable.split.32-DouHt2Dd.woff2 +0 -0
- package/gui/dist/assets/WantedSansVariable.split.33-B8HVvkJX.woff2 +0 -0
- package/gui/dist/assets/WantedSansVariable.split.34-BtkRu1FN.woff2 +0 -0
- package/gui/dist/assets/WantedSansVariable.split.35-DBylQbtk.woff2 +0 -0
- package/gui/dist/assets/WantedSansVariable.split.36-Sg089Kgh.woff2 +0 -0
- package/gui/dist/assets/WantedSansVariable.split.37-CYtvbriS.woff2 +0 -0
- package/gui/dist/assets/WantedSansVariable.split.38-DKkSLf8Z.woff2 +0 -0
- package/gui/dist/assets/WantedSansVariable.split.39-CecotKZD.woff2 +0 -0
- package/gui/dist/assets/WantedSansVariable.split.4-Bv8O3ZMb.woff2 +0 -0
- package/gui/dist/assets/WantedSansVariable.split.40-DdCvkmOU.woff2 +0 -0
- package/gui/dist/assets/WantedSansVariable.split.41-ESn674Hc.woff2 +0 -0
- package/gui/dist/assets/WantedSansVariable.split.42-DL9V7NNV.woff2 +0 -0
- package/gui/dist/assets/WantedSansVariable.split.43-yipO0cyk.woff2 +0 -0
- package/gui/dist/assets/WantedSansVariable.split.44-6rTmLZhm.woff2 +0 -0
- package/gui/dist/assets/WantedSansVariable.split.45-BJAj8lrm.woff2 +0 -0
- package/gui/dist/assets/WantedSansVariable.split.46-BDTzMn2c.woff2 +0 -0
- package/gui/dist/assets/WantedSansVariable.split.47-Bxd4wGgi.woff2 +0 -0
- package/gui/dist/assets/WantedSansVariable.split.48-DxnFBLnH.woff2 +0 -0
- package/gui/dist/assets/WantedSansVariable.split.49-DnVD0aOD.woff2 +0 -0
- package/gui/dist/assets/WantedSansVariable.split.5-B1wI2pp5.woff2 +0 -0
- package/gui/dist/assets/WantedSansVariable.split.50-D9JlkZd6.woff2 +0 -0
- package/gui/dist/assets/WantedSansVariable.split.51-D9Vw6Bu3.woff2 +0 -0
- package/gui/dist/assets/WantedSansVariable.split.52-ClEJk4kn.woff2 +0 -0
- package/gui/dist/assets/WantedSansVariable.split.53-BSXU0vh-.woff2 +0 -0
- package/gui/dist/assets/WantedSansVariable.split.54-EcuxkR9v.woff2 +0 -0
- package/gui/dist/assets/WantedSansVariable.split.55-CYiFhQX8.woff2 +0 -0
- package/gui/dist/assets/WantedSansVariable.split.56--Gk9g8BK.woff2 +0 -0
- package/gui/dist/assets/WantedSansVariable.split.57-DnkOPZcy.woff2 +0 -0
- package/gui/dist/assets/WantedSansVariable.split.58-9MB8504z.woff2 +0 -0
- package/gui/dist/assets/WantedSansVariable.split.59-ByxIsN72.woff2 +0 -0
- package/gui/dist/assets/WantedSansVariable.split.6-Bmk0dRVw.woff2 +0 -0
- package/gui/dist/assets/WantedSansVariable.split.60-Bxwx_aWQ.woff2 +0 -0
- package/gui/dist/assets/WantedSansVariable.split.61-DA2z5sm7.woff2 +0 -0
- package/gui/dist/assets/WantedSansVariable.split.62-C5iJPJZn.woff2 +0 -0
- package/gui/dist/assets/WantedSansVariable.split.63-DAqrmaEc.woff2 +0 -0
- package/gui/dist/assets/WantedSansVariable.split.64-BgXFsljb.woff2 +0 -0
- package/gui/dist/assets/WantedSansVariable.split.65-DKiWujvu.woff2 +0 -0
- package/gui/dist/assets/WantedSansVariable.split.66-8po4vUZ4.woff2 +0 -0
- package/gui/dist/assets/WantedSansVariable.split.67-Cy5qyI9O.woff2 +0 -0
- package/gui/dist/assets/WantedSansVariable.split.68-BKny7Fy3.woff2 +0 -0
- package/gui/dist/assets/WantedSansVariable.split.69-B4VU4u_b.woff2 +0 -0
- package/gui/dist/assets/WantedSansVariable.split.7-BuqwNpgA.woff2 +0 -0
- package/gui/dist/assets/WantedSansVariable.split.70-BCd4n346.woff2 +0 -0
- package/gui/dist/assets/WantedSansVariable.split.71-CkQppaWn.woff2 +0 -0
- package/gui/dist/assets/WantedSansVariable.split.72-Db7vaoFs.woff2 +0 -0
- package/gui/dist/assets/WantedSansVariable.split.73-DI80TYWl.woff2 +0 -0
- package/gui/dist/assets/WantedSansVariable.split.74-tMCtDAHB.woff2 +0 -0
- package/gui/dist/assets/WantedSansVariable.split.75-Ciw0D5WG.woff2 +0 -0
- package/gui/dist/assets/WantedSansVariable.split.76-DoLjFgp-.woff2 +0 -0
- package/gui/dist/assets/WantedSansVariable.split.77-CpT2uvMy.woff2 +0 -0
- package/gui/dist/assets/WantedSansVariable.split.78-CkcJ4iZ1.woff2 +0 -0
- package/gui/dist/assets/WantedSansVariable.split.79-DsOUJjEM.woff2 +0 -0
- package/gui/dist/assets/WantedSansVariable.split.8-DsjypPd6.woff2 +0 -0
- package/gui/dist/assets/WantedSansVariable.split.80-C0VwHTte.woff2 +0 -0
- package/gui/dist/assets/WantedSansVariable.split.81-DTelu57i.woff2 +0 -0
- package/gui/dist/assets/WantedSansVariable.split.82-BiMndGN1.woff2 +0 -0
- package/gui/dist/assets/WantedSansVariable.split.83-CVcC6eDG.woff2 +0 -0
- package/gui/dist/assets/WantedSansVariable.split.84-DlhZVt_O.woff2 +0 -0
- package/gui/dist/assets/WantedSansVariable.split.85-S8I2D3pA.woff2 +0 -0
- package/gui/dist/assets/WantedSansVariable.split.86-DozXXpH5.woff2 +0 -0
- package/gui/dist/assets/WantedSansVariable.split.87-Dsn0Vuuo.woff2 +0 -0
- package/gui/dist/assets/WantedSansVariable.split.88-iNwxxDCr.woff2 +0 -0
- package/gui/dist/assets/WantedSansVariable.split.89-B63u_9CZ.woff2 +0 -0
- package/gui/dist/assets/WantedSansVariable.split.9-Bi3IzpMI.woff2 +0 -0
- package/gui/dist/assets/WantedSansVariable.split.90-ByeH1Pqf.woff2 +0 -0
- package/gui/dist/assets/WantedSansVariable.split.91-UQLlagpm.woff2 +0 -0
- package/gui/dist/assets/brand-mark-A03mw_9y.js +1 -0
- package/gui/dist/assets/geist-cyrillic-ext-wght-normal-DjL33-gN.woff2 +0 -0
- package/gui/dist/assets/geist-cyrillic-wght-normal-BEAKL7Jp.woff2 +0 -0
- package/gui/dist/assets/geist-latin-ext-wght-normal-DC-KSUi6.woff2 +0 -0
- package/gui/dist/assets/geist-latin-wght-normal-BgDaEnEv.woff2 +0 -0
- package/gui/dist/assets/geist-mono-cyrillic-ext-wght-normal-X_5orZeX.woff2 +0 -0
- package/gui/dist/assets/geist-mono-cyrillic-wght-normal-DiZS0aHC.woff2 +0 -0
- package/gui/dist/assets/geist-mono-latin-ext-wght-normal-Bwz-egvJ.woff2 +0 -0
- package/gui/dist/assets/geist-mono-latin-wght-normal-XN7g48iV.woff2 +0 -0
- package/gui/dist/assets/geist-mono-symbols2-wght-normal-CO5SzqOn.woff2 +0 -0
- package/gui/dist/assets/geist-mono-vietnamese-wght-normal-DadHysG0.woff2 +0 -0
- package/gui/dist/assets/geist-vietnamese-wght-normal-6IgcOCM7.woff2 +0 -0
- package/gui/dist/assets/index-5REODpG3.css +2 -0
- package/gui/dist/assets/index-BjTIN4to.js +94 -0
- package/gui/dist/assets/segment-thumb-features-Bf6vpwf8.js +1 -0
- package/gui/dist/classic/assets/App-C9Eg3nUo.js +58 -0
- package/gui/dist/{assets/Tray-CEw0y0jR.js → classic/assets/Tray-B8P8cokE.js} +1 -1
- package/gui/dist/classic/assets/index-DFAFnven.js +94 -0
- package/gui/dist/classic/assets/quota-summary-C-4HP4_5.js +1 -0
- package/gui/dist/classic/index.html +47 -0
- package/gui/dist/favicon.png +0 -0
- package/gui/dist/favicon.svg +14 -0
- package/gui/dist/index.html +15 -2
- package/package.json +1 -1
- package/src/adapters/anthropic.ts +17 -8
- package/src/adapters/google.ts +7 -7
- package/src/adapters/kiro/adapter.ts +18 -3
- package/src/adapters/kiro/conversation.ts +8 -8
- package/src/adapters/kiro/payload.ts +45 -12
- package/src/adapters/kiro/stream.ts +169 -12
- package/src/adapters/kiro/structured-output-json.ts +191 -0
- package/src/adapters/kiro/structured-output-schema.ts +542 -0
- package/src/adapters/kiro/structured-output.ts +367 -0
- package/src/adapters/kiro-constants.ts +9 -0
- package/src/adapters/kiro-tools.ts +14 -9
- package/src/adapters/kiro-wire.ts +9 -5
- package/src/adapters/openai-chat/messages.ts +58 -5
- package/src/adapters/openai-chat/mistral-image-limit.ts +50 -0
- package/src/adapters/openai-chat/passthrough.ts +5 -3
- package/src/adapters/openai-chat/tool-schema.ts +83 -13
- package/src/adapters/openai-responses/opengateway-input.ts +18 -6
- package/src/adapters/openai-responses/tool-output-recovery.ts +79 -0
- package/src/adapters/qoder/live-models.ts +115 -1
- package/src/claude/failure-verdict.ts +21 -0
- package/src/claude/inbound-content-options.ts +15 -2
- package/src/claude/inbound-model-options.ts +2 -1
- package/src/claude/inbound-records.ts +2 -0
- package/src/claude/inbound.ts +17 -11
- package/src/claude/outbound.ts +52 -23
- package/src/cli/capabilities-access-remote.ts +13 -0
- package/src/cli/capabilities-base.ts +1 -0
- package/src/cli/capabilities-provider-models.ts +3 -3
- package/src/cli/codex-shim-autorestore.ts +7 -0
- package/src/cli/config-command.ts +11 -2
- package/src/cli/connect.ts +17 -2
- package/src/cli/dispatch.ts +3 -1
- package/src/cli/doctor.ts +95 -7
- package/src/cli/index.ts +12 -7
- package/src/cli/link-apply-restart.ts +9 -2
- package/src/cli/models.ts +124 -38
- package/src/cli/registry.ts +1 -1
- package/src/cli/restart-failure.ts +17 -0
- package/src/cli/stop-approval.ts +2 -2
- package/src/cli/sync-command.ts +1 -1
- package/src/cli/sync-fanout.ts +15 -4
- package/src/cli/system-restart-client.ts +5 -0
- package/src/cli/uninstall-client-state.ts +12 -1
- package/src/cli/update-restart-child.ts +13 -3
- package/src/cli/update-restart-home.ts +6 -3
- package/src/cli/update-restart.ts +36 -19
- package/src/client/connect.ts +4 -1
- package/src/client/hub-client.ts +5 -0
- package/src/client/link-management-relay.ts +39 -2
- package/src/client/link-readiness.ts +3 -1
- package/src/client/machine-api.ts +2 -1
- package/src/client/machine-listener.ts +7 -0
- package/src/client/orphan-token-recovery.ts +322 -0
- package/src/codex/catalog/provider-models.ts +12 -4
- package/src/codex/native-profile-api.ts +27 -1
- package/src/codex/shim-diagnostics.ts +7 -0
- package/src/codex/shim-legacy-record.ts +290 -0
- package/src/codex/shim-windows-refresh.ts +9 -2
- package/src/codex/shim-wrapper-write.ts +7 -0
- package/src/codex/shim.ts +80 -21
- package/src/config/diagnostics.ts +9 -9
- package/src/config/load-degrade.ts +27 -27
- package/src/config/provider-validation.ts +9 -2
- package/src/config/removed-opencode-free-backup.ts +6 -3
- package/src/config/tracked-runtime-inspection.ts +3 -3
- package/src/generated/compatibility-version.json +219 -147
- package/src/images/loop.ts +2 -2
- package/src/lib/client-error-body.ts +37 -6
- package/src/lib/config-ownership-removal.ts +422 -0
- package/src/lib/config-ownership.ts +646 -81
- package/src/lib/context-overflow-verdict.ts +3 -3
- package/src/lib/crash-guard.ts +4 -2
- package/src/lib/desktop-cli-handoff.d.mts +3 -4
- package/src/lib/desktop-cli-handoff.mjs +7 -16
- package/src/lib/errors.ts +1 -0
- package/src/lib/local-management-capability.ts +3 -1
- package/src/lib/messages-error-body.ts +162 -0
- package/src/lib/messages-sse-error-presentation.ts +93 -0
- package/src/lib/passive-read.ts +9 -4
- package/src/lib/socks5-fetch.ts +21 -9
- package/src/lib/upstream-retry.ts +81 -1
- package/src/lib/windows-owner-acl.ts +9 -3
- package/src/lib/winsw.ts +5 -5
- package/src/lib/workflow-budget.ts +3 -1
- package/src/oauth/index.ts +7 -0
- package/src/oauth/store.ts +20 -10
- package/src/protocols/codecs/messages-ir.ts +15 -9
- package/src/protocols/encoders/adapter-events.ts +26 -7
- package/src/protocols/encoders/messages.ts +72 -28
- package/src/providers/antigravity-models.ts +45 -12
- package/src/providers/free-directory.ts +2 -2
- package/src/providers/reasoning-metadata.ts +2 -1
- package/src/providers/registry/entries-extended.ts +9 -0
- package/src/reasoning-effort.ts +7 -2
- package/src/remote/hub-usage.ts +3 -0
- package/src/server/admission-model-scope.ts +1 -1
- package/src/server/admitted-http-turn.ts +2 -1
- package/src/server/anthropic-rate-limit-headers.ts +75 -3
- package/src/server/claude-messages.ts +130 -67
- package/src/server/gui-static.ts +4 -1
- package/src/server/hub-usage.ts +7 -2
- package/src/server/index/link-management.ts +8 -0
- package/src/server/index/ollama-routes.ts +2 -0
- package/src/server/index/serve-options.ts +23 -20
- package/src/server/index/startup-warnings.ts +5 -12
- package/src/server/index/websocket-handler.ts +1 -0
- package/src/server/inference/client-encoder-delivery.ts +5 -1
- package/src/server/management/logs-usage-routes.ts +89 -4
- package/src/server/management/provider-routes.ts +2 -22
- package/src/server/management/qoder-provider-probe.ts +105 -0
- package/src/server/management/route-registry.ts +3 -0
- package/src/server/management/usage-aggregate-cache.ts +15 -5
- package/src/server/management/usage-source-facets.ts +38 -0
- package/src/server/messages-native-tool-guard.ts +11 -3
- package/src/server/messages-native.ts +111 -40
- package/src/server/messages-response-headers.ts +25 -11
- package/src/server/messages-shared.ts +94 -64
- package/src/server/messages-sse-error-filter.ts +199 -0
- package/src/server/request-log.ts +12 -0
- package/src/server/responses/adapter-continuation.ts +11 -5
- package/src/server/responses/adapter-delivery.ts +14 -8
- package/src/server/responses/adapter-dispatch.ts +8 -8
- package/src/server/responses/anthropic-thinking-recovery.ts +2 -2
- package/src/server/responses/codex-auth-error.ts +8 -4
- package/src/server/responses/codex-ws-wire.ts +19 -3
- package/src/server/responses/compact.ts +4 -1
- package/src/server/responses/core-auth.ts +1 -1
- package/src/server/responses/core-combo-failure.ts +2 -0
- package/src/server/responses/core-combo.ts +2 -0
- package/src/server/responses/core-errors.ts +5 -4
- package/src/server/responses/core-options.ts +2 -0
- package/src/server/responses/empty-completion-guard.ts +4 -1
- package/src/server/responses/pacing-overload.ts +1 -1
- package/src/server/responses/passthrough-dispatch.ts +5 -5
- package/src/server/responses/request-prepare.ts +3 -1
- package/src/server/responses/request-transport.ts +9 -8
- package/src/server/responses/response-effects.ts +3 -2
- package/src/server/responses/terminal-guard.ts +30 -0
- package/src/server/restart-replacement.ts +3 -1
- package/src/server/sse-payload-rewrite.ts +23 -11
- package/src/server/workflow-refusal.ts +1 -1
- package/src/server/ws-bridge.ts +4 -0
- package/src/service/definition.ts +36 -4
- package/src/service/launchd.ts +89 -32
- package/src/service/logging.ts +34 -7
- package/src/service/plist-xml.ts +59 -0
- package/src/service/state.ts +8 -0
- package/src/service/systemd-env.ts +2 -0
- package/src/service/systemd.ts +31 -7
- package/src/service/windows-taskxml.ts +16 -7
- package/src/service-manager-probe.ts +4 -10
- package/src/types/provider.ts +2 -0
- package/src/types/request.ts +13 -1
- package/src/update/config-preflight.ts +48 -0
- package/src/update/index.ts +15 -1
- package/src/usage/client.ts +43 -0
- package/src/usage/error-detail.ts +1 -1
- package/src/usage/log.ts +4 -0
- package/src/usage/source-facets.ts +51 -0
- package/src/usage/source-filter.ts +65 -0
- package/src/usage/summary.ts +50 -8
- package/src/web-search/anthropic-executor.ts +105 -24
- package/src/web-search/executor.ts +5 -3
- package/src/web-search/loop.ts +2 -1
- package/src/web-search/progress-stream.ts +64 -5
- package/gui/dist/assets/App-DuNhggE1.js +0 -58
- package/gui/dist/assets/index-CynE_Rue.js +0 -94
- package/gui/dist/assets/quota-summary-VHFSlUuM.js +0 -1
- /package/gui/dist/{assets → classic/assets}/App-B4j-Txmb.css +0 -0
- /package/gui/dist/{assets → classic/assets}/index-CHWwDo9L.css +0 -0
|
@@ -6,7 +6,11 @@ import {
|
|
|
6
6
|
} from "../../lib/translator-budget";
|
|
7
7
|
import type { AdapterEvent, OcxUsage } from "../../types";
|
|
8
8
|
import { recordKiroCalibration, rekeyKiroCalibration } from "../kiro-calibration";
|
|
9
|
-
import {
|
|
9
|
+
import {
|
|
10
|
+
KIRO_COMPLETION_TOOL_NAME,
|
|
11
|
+
KIRO_STRUCTURED_OUTPUT_TOOL_NAME,
|
|
12
|
+
type KiroCompletionMode,
|
|
13
|
+
} from "../kiro-constants";
|
|
10
14
|
import {
|
|
11
15
|
classifyKiroEventError,
|
|
12
16
|
classifyKiroHttpError,
|
|
@@ -22,6 +26,16 @@ import { isCompleteKiroToolInput, kiroTruncationErrorMessage } from "../kiro-tru
|
|
|
22
26
|
import { isValidKiroConversationId } from "../kiro-wire";
|
|
23
27
|
import { readDisplaySafeErrorPayloadText } from "../upstream-http-error";
|
|
24
28
|
import { tagKiroReasoningBlob } from "./reasoning";
|
|
29
|
+
import {
|
|
30
|
+
isKiroStructuredCleanStop,
|
|
31
|
+
KIRO_STRUCTURED_EMPTY_FAILURE,
|
|
32
|
+
KIRO_STRUCTURED_TEXT_FAILURE,
|
|
33
|
+
kiroStructuredOutputFailure,
|
|
34
|
+
kiroStructuredStopFailure,
|
|
35
|
+
parseKiroStructuredText,
|
|
36
|
+
parseKiroStructuredToolInput,
|
|
37
|
+
type KiroStructuredOutputSpec,
|
|
38
|
+
} from "./structured-output";
|
|
25
39
|
import { estimateKiroTokens, kiroUpstreamContextWindow } from "./usage";
|
|
26
40
|
|
|
27
41
|
// Stream parsing (shared by parseStream + parseResponse)
|
|
@@ -258,6 +272,10 @@ export function isRetryableKiroStreamCatchError(err: unknown, emittedOutput: boo
|
|
|
258
272
|
/** Native clean-stop reason eligible for bounded private-completion validation. */
|
|
259
273
|
const KIRO_END_TURN_STOP_REASON = "END_TURN";
|
|
260
274
|
|
|
275
|
+
/**
|
|
276
|
+
* Run one attempt through `parseKiroAttemptEvents` while holding commentary until completion is
|
|
277
|
+
* validated, and hand the held events back to the caller so a bounded retry can release or drop them.
|
|
278
|
+
*/
|
|
261
279
|
async function* parseKiroAttempt(
|
|
262
280
|
response: Response,
|
|
263
281
|
budget: TranslatorBudget,
|
|
@@ -271,6 +289,7 @@ async function* parseKiroAttempt(
|
|
|
271
289
|
/** True when an earlier attempt has output that must survive a failed retry. */
|
|
272
290
|
priorEmittedOutput = false,
|
|
273
291
|
priorAttempt?: KiroAttemptResult,
|
|
292
|
+
structuredOutput?: KiroStructuredOutputSpec,
|
|
274
293
|
): AsyncGenerator<AdapterEvent, KiroAttemptResult> {
|
|
275
294
|
// Hold commentary through completion validation; tools and failures still release progress.
|
|
276
295
|
const deferred: AdapterEvent[] = [];
|
|
@@ -302,7 +321,13 @@ async function* parseKiroAttempt(
|
|
|
302
321
|
contextInputEstimate,
|
|
303
322
|
priorEmittedOutput,
|
|
304
323
|
priorAttempt,
|
|
324
|
+
structuredOutput,
|
|
305
325
|
);
|
|
326
|
+
// Held text that no terminal claimed is prose. On an ordinary turn that is still real progress
|
|
327
|
+
// and is released before a failure; for a structured-output caller it is exactly the wrong-format
|
|
328
|
+
// answer the contract exists to withhold, so only its non-text events are released. A real tool
|
|
329
|
+
// call has already flushed its leading commentary by the time anything reaches these drains.
|
|
330
|
+
const dropHeldProse = structuredOutput !== undefined;
|
|
306
331
|
let handedOff = false;
|
|
307
332
|
try {
|
|
308
333
|
const result = yield* attempt;
|
|
@@ -315,8 +340,8 @@ async function* parseKiroAttempt(
|
|
|
315
340
|
if (staged && !result.needsFallback) {
|
|
316
341
|
recordKiroCalibration(staged.conversationId, staged.estimated, staged.charged);
|
|
317
342
|
}
|
|
318
|
-
if (priorAttempt) yield* priorAttempt.drainDeferred();
|
|
319
|
-
if (!result.needsFallback) yield* drainDeferred();
|
|
343
|
+
if (priorAttempt) yield* priorAttempt.drainDeferred(dropHeldProse);
|
|
344
|
+
if (!result.needsFallback) yield* drainDeferred(dropHeldProse);
|
|
320
345
|
handedOff = true;
|
|
321
346
|
return {
|
|
322
347
|
...result, drainDeferred,
|
|
@@ -348,6 +373,7 @@ async function* parseKiroAttemptEvents(
|
|
|
348
373
|
contextInputEstimate?: number,
|
|
349
374
|
priorEmittedOutput = false,
|
|
350
375
|
priorAttempt?: KiroAttemptResult,
|
|
376
|
+
structuredOutput?: KiroStructuredOutputSpec,
|
|
351
377
|
): AsyncGenerator<AdapterEvent, KiroAttemptParseResult> {
|
|
352
378
|
const emptyResult = (): KiroAttemptParseResult => ({ assistantText: "", sawReasoning: false });
|
|
353
379
|
// Every early return below is a failure path that stages nothing; only the completion path
|
|
@@ -377,6 +403,12 @@ async function* parseKiroAttemptEvents(
|
|
|
377
403
|
let sawRealTool = false;
|
|
378
404
|
let completionAnswer: string | undefined;
|
|
379
405
|
let completionCalls = 0;
|
|
406
|
+
// A structured-output call whose input failed validation on the FIRST attempt. It earns the one
|
|
407
|
+
// bounded retry instead of failing the turn, because the likely fault (a missing required key, a
|
|
408
|
+
// wrong root type) is one the model can correct when told; the retry itself gets no second one.
|
|
409
|
+
let structuredRetry = false;
|
|
410
|
+
// Structured output replaces the completion tool as the turn's terminal channel (payload.ts).
|
|
411
|
+
const terminalToolName = structuredOutput ? KIRO_STRUCTURED_OUTPUT_TOOL_NAME : KIRO_COMPLETION_TOOL_NAME;
|
|
380
412
|
let authoritativeUsage: OcxUsage | undefined;
|
|
381
413
|
let providerCredits: number | undefined;
|
|
382
414
|
let stopReason: string | undefined;
|
|
@@ -445,6 +477,7 @@ async function* parseKiroAttemptEvents(
|
|
|
445
477
|
};
|
|
446
478
|
};
|
|
447
479
|
|
|
480
|
+
/** The terminal for a classified upstream error frame; retryable only while nothing has been emitted. */
|
|
448
481
|
const classifiedTerminal = (failure: KiroErrorClassification): AdapterEvent => {
|
|
449
482
|
// Upstream exception/error frames can arrive after commentary was already staged (and will be
|
|
450
483
|
// flushed before this terminal is yielded). Replaying after that content would duplicate it.
|
|
@@ -470,8 +503,16 @@ async function* parseKiroAttemptEvents(
|
|
|
470
503
|
};
|
|
471
504
|
};
|
|
472
505
|
|
|
506
|
+
/**
|
|
507
|
+
* The terminal for a protocol violation. A malformed private-tool answer on the bounded retry is
|
|
508
|
+
* a retryable incomplete for ordinary turns and a non-retryable failure for structured output;
|
|
509
|
+
* everything else is a non-retryable 502.
|
|
510
|
+
*/
|
|
473
511
|
const protocolTerminal = (message: string, malformedCompletion = false): AdapterEvent => {
|
|
474
512
|
if (mode === "text_fallback" && malformedCompletion) {
|
|
513
|
+
// The bounded retry was the structured contract's second ask; a malformed answer on it ends
|
|
514
|
+
// the turn with the non-retryable structured failure, never a replayable incomplete.
|
|
515
|
+
if (structuredOutput) return kiroStructuredOutputFailure(message, usage());
|
|
475
516
|
return retryableKiroIncomplete(
|
|
476
517
|
"malformed_kiro_completion",
|
|
477
518
|
message,
|
|
@@ -500,13 +541,23 @@ async function* parseKiroAttemptEvents(
|
|
|
500
541
|
console.warn("[kiro] dropped the private final-answer call; the turn also carried a real tool call");
|
|
501
542
|
};
|
|
502
543
|
|
|
544
|
+
/**
|
|
545
|
+
* Mark a tool call as the turn's private terminal tool or an ordinary client tool, and return a
|
|
546
|
+
* terminal when the call names a reserved tool this turn did not advertise or completion is disabled.
|
|
547
|
+
*/
|
|
503
548
|
const classifyTool = (
|
|
504
549
|
tool: { id: string; name: string; chunks: string[]; completion: boolean },
|
|
505
550
|
): AdapterEvent | undefined => {
|
|
506
|
-
if (tool.name !== KIRO_COMPLETION_TOOL_NAME) {
|
|
551
|
+
if (tool.name !== KIRO_COMPLETION_TOOL_NAME && tool.name !== KIRO_STRUCTURED_OUTPUT_TOOL_NAME) {
|
|
507
552
|
tool.completion = false;
|
|
508
553
|
return undefined;
|
|
509
554
|
}
|
|
555
|
+
// Both private names are reserved against client tools (kiro-wire.ts), so a call to the one
|
|
556
|
+
// this turn did not advertise can only be the model inventing it. Relaying it as a client
|
|
557
|
+
// tool would hand the client a call to a tool it never declared.
|
|
558
|
+
if (tool.name !== terminalToolName) {
|
|
559
|
+
return protocolTerminal("Kiro returned a reserved private tool this turn did not advertise");
|
|
560
|
+
}
|
|
510
561
|
if (mode === "disabled") {
|
|
511
562
|
return protocolTerminal("Kiro returned the reserved private final-answer tool while explicit completion was disabled");
|
|
512
563
|
}
|
|
@@ -538,6 +589,11 @@ async function* parseKiroAttemptEvents(
|
|
|
538
589
|
return [{ type: "heartbeat" }];
|
|
539
590
|
};
|
|
540
591
|
|
|
592
|
+
/**
|
|
593
|
+
* Account for one content event and route it by completion mode: emitted directly when completion
|
|
594
|
+
* is disabled, held until validation in `required` mode, and retained for the bounded retry's
|
|
595
|
+
* decision in `text_fallback` mode.
|
|
596
|
+
*/
|
|
541
597
|
const stage = (event: AdapterEvent): AdapterEvent[] => {
|
|
542
598
|
if (event.type === "text_delta") {
|
|
543
599
|
const nextAssistantTextBytes = appendedUtf8Bytes(assistantText, assistantTextBytes, event.text);
|
|
@@ -584,7 +640,12 @@ async function* parseKiroAttemptEvents(
|
|
|
584
640
|
return mode === "required" ? defer(event) : [event];
|
|
585
641
|
};
|
|
586
642
|
|
|
643
|
+
/** Parse the private terminal tool's input into the answer text: the validated JSON for structured output, otherwise the completion tool's `answer`. */
|
|
587
644
|
const parseCompletion = (chunks: string[]): string | Error => {
|
|
645
|
+
if (structuredOutput) {
|
|
646
|
+
const parsed = parseKiroStructuredToolInput(structuredOutput, chunks.join(""));
|
|
647
|
+
return "json" in parsed ? parsed.json : new Error(parsed.error);
|
|
648
|
+
}
|
|
588
649
|
const raw = chunks.join("").trim();
|
|
589
650
|
let value: unknown;
|
|
590
651
|
try {
|
|
@@ -602,6 +663,10 @@ async function* parseKiroAttemptEvents(
|
|
|
602
663
|
return answer;
|
|
603
664
|
};
|
|
604
665
|
|
|
666
|
+
/**
|
|
667
|
+
* Close the open tool call: record a private terminal answer (or schedule its bounded retry), or
|
|
668
|
+
* emit a real client tool call. Returns a terminal when the call cannot be accepted.
|
|
669
|
+
*/
|
|
605
670
|
const flushOpen = (): { events: AdapterEvent[]; terminal?: AdapterEvent } => {
|
|
606
671
|
if (!open) return { events: [] };
|
|
607
672
|
const tool = open;
|
|
@@ -609,6 +674,14 @@ async function* parseKiroAttemptEvents(
|
|
|
609
674
|
closeOpenCall();
|
|
610
675
|
const input = tool.chunks.join("");
|
|
611
676
|
if (!isCompleteKiroToolInput(input)) {
|
|
677
|
+
// A truncated structured answer is the same correctable fault as one that fails to parse: it
|
|
678
|
+
// earns the bounded retry, decided with the stop reason at the end of the attempt. The retry
|
|
679
|
+
// itself (text_fallback) ends non-retryably through protocolTerminal.
|
|
680
|
+
if (structuredOutput && mode === "required" && tool.completion) {
|
|
681
|
+
completionCalls++;
|
|
682
|
+
structuredRetry = true;
|
|
683
|
+
return { events: [] };
|
|
684
|
+
}
|
|
612
685
|
return { events: [], terminal: protocolTerminal(kiroTruncationErrorMessage("incomplete tool input JSON"), tool.completion) };
|
|
613
686
|
}
|
|
614
687
|
if (tool.completion) {
|
|
@@ -617,6 +690,11 @@ async function* parseKiroAttemptEvents(
|
|
|
617
690
|
return { events: [], terminal: protocolTerminal("Kiro returned more than one private final-answer tool call", true) };
|
|
618
691
|
}
|
|
619
692
|
const answer = parseCompletion(tool.chunks);
|
|
693
|
+
if (answer instanceof Error && structuredOutput) {
|
|
694
|
+
if (mode !== "required") return { events: [], terminal: kiroStructuredOutputFailure(answer.message, usage()) };
|
|
695
|
+
structuredRetry = true;
|
|
696
|
+
return { events: [] };
|
|
697
|
+
}
|
|
620
698
|
if (answer instanceof Error) return { events: [], terminal: protocolTerminal(answer.message, true) };
|
|
621
699
|
if (sawRealTool) {
|
|
622
700
|
noteDroppedCompletion();
|
|
@@ -796,7 +874,11 @@ async function* parseKiroAttemptEvents(
|
|
|
796
874
|
}
|
|
797
875
|
if (open) {
|
|
798
876
|
const input = open.chunks.join("");
|
|
799
|
-
|
|
877
|
+
// A truncated structured answer still open at end of stream goes through flushOpen like one
|
|
878
|
+
// closed by a tool-stop frame: the bounded retry, decided with the stop reason below. Every
|
|
879
|
+
// other open call keeps this guard's terminal.
|
|
880
|
+
const structuredTruncation = structuredOutput !== undefined && mode === "required" && open.completion;
|
|
881
|
+
if (!structuredTruncation && !isCompleteKiroToolInput(input)) {
|
|
800
882
|
const privateTool = open.completion;
|
|
801
883
|
open = null;
|
|
802
884
|
return {
|
|
@@ -862,7 +944,12 @@ async function* parseKiroAttemptEvents(
|
|
|
862
944
|
sawRealTool,
|
|
863
945
|
completionCalls,
|
|
864
946
|
nativeCompletionStop,
|
|
865
|
-
...(
|
|
947
|
+
...(structuredOutput ? { structuredOutput: true, structuredRetry } : {}),
|
|
948
|
+
...(stopReason !== undefined ? { stopReason: [
|
|
949
|
+
"END_TURN", "STOP_SEQUENCE", "MAX_TOKENS", "MODEL_CONTEXT_WINDOW_EXCEEDED",
|
|
950
|
+
"MALFORMED_TOOL_USE", "MALFORMED_MODEL_OUTPUT", "TOOL_USE",
|
|
951
|
+
"CONTENT_FILTERED", "GUARDRAIL_INTERVENED",
|
|
952
|
+
].includes(normalizedStopReason ?? "") ? normalizedStopReason : "other" } : {}),
|
|
866
953
|
assistantChars: assistantText.length,
|
|
867
954
|
});
|
|
868
955
|
|
|
@@ -876,8 +963,10 @@ async function* parseKiroAttemptEvents(
|
|
|
876
963
|
// the first attempt's held progress is released rather than superseded (retry text normally
|
|
877
964
|
// replaces it), and the filtered retry text follows as the commentary it was staged as.
|
|
878
965
|
if (completionAnswer === undefined && !sawRealTool && isKiroContentFilterStop(normalizedStopReason)) {
|
|
879
|
-
|
|
880
|
-
|
|
966
|
+
// A structured-output caller never receives prose: text from either attempt is dropped and
|
|
967
|
+
// only their non-text events and the filter terminal remain.
|
|
968
|
+
if (priorAttempt) yield* priorAttempt.drainDeferred(structuredOutput !== undefined);
|
|
969
|
+
yield* structuredOutput ? consumeSupersededByCompletion(fallbackEvents) : emitRetained(fallbackEvents);
|
|
881
970
|
return {
|
|
882
971
|
assistantText,
|
|
883
972
|
sawReasoning,
|
|
@@ -885,7 +974,11 @@ async function* parseKiroAttemptEvents(
|
|
|
885
974
|
};
|
|
886
975
|
}
|
|
887
976
|
if (priorAttempt) {
|
|
888
|
-
|
|
977
|
+
// Structured output supersedes first-attempt prose on every outcome but a real tool call:
|
|
978
|
+
// success replaces it, and failure must not relay it (see `dropHeldProse`).
|
|
979
|
+
yield* priorAttempt.drainDeferred(structuredOutput
|
|
980
|
+
? !sawRealTool
|
|
981
|
+
: completionAnswer !== undefined || (sawText && !sawRealTool));
|
|
889
982
|
}
|
|
890
983
|
if (completionAnswer !== undefined) {
|
|
891
984
|
yield* consumeSupersededByCompletion(fallbackEvents);
|
|
@@ -904,6 +997,25 @@ async function* parseKiroAttemptEvents(
|
|
|
904
997
|
terminal: { type: "done", usage: finalUsage, endTurn: false, ...(finalProviderState ? { providerState: finalProviderState } : {}) },
|
|
905
998
|
};
|
|
906
999
|
}
|
|
1000
|
+
if (sawText && structuredOutput) {
|
|
1001
|
+
// Same clean-stop rule as the first attempt: text a MAX_TOKENS cut ended is not an answer,
|
|
1002
|
+
// even when its prefix parses.
|
|
1003
|
+
const cleanStop = isKiroStructuredCleanStop(normalizedStopReason);
|
|
1004
|
+
const json = cleanStop ? parseKiroStructuredText(structuredOutput, assistantText) : undefined;
|
|
1005
|
+
yield* consumeSupersededByCompletion(fallbackEvents);
|
|
1006
|
+
if (json === undefined) {
|
|
1007
|
+
const message = !cleanStop && normalizedStopReason
|
|
1008
|
+
? kiroStructuredStopFailure(normalizedStopReason)
|
|
1009
|
+
: KIRO_STRUCTURED_TEXT_FAILURE;
|
|
1010
|
+
return { assistantText, sawReasoning, terminal: kiroStructuredOutputFailure(message, finalUsage) };
|
|
1011
|
+
}
|
|
1012
|
+
yield { type: "text_delta", text: json, phase: "final_answer" };
|
|
1013
|
+
return {
|
|
1014
|
+
assistantText,
|
|
1015
|
+
sawReasoning,
|
|
1016
|
+
terminal: { type: "done", usage: finalUsage, endTurn: true, ...(finalProviderState ? { providerState: finalProviderState } : {}) },
|
|
1017
|
+
};
|
|
1018
|
+
}
|
|
907
1019
|
if (sawText) {
|
|
908
1020
|
for (const event of fallbackEvents) {
|
|
909
1021
|
try {
|
|
@@ -919,6 +1031,13 @@ async function* parseKiroAttemptEvents(
|
|
|
919
1031
|
terminal: { type: "done", usage: finalUsage, endTurn: true, ...(finalProviderState ? { providerState: finalProviderState } : {}) },
|
|
920
1032
|
};
|
|
921
1033
|
}
|
|
1034
|
+
if (structuredOutput) {
|
|
1035
|
+
// The model was already asked twice inside this turn; an empty or reasoning-only retry ends
|
|
1036
|
+
// the structured contract with its non-retryable failure, never a retryable empty fallback
|
|
1037
|
+
// that would let the proxy replay the whole turn.
|
|
1038
|
+
yield* consumeSupersededByCompletion(fallbackEvents);
|
|
1039
|
+
return { assistantText, sawReasoning, terminal: kiroStructuredOutputFailure(KIRO_STRUCTURED_EMPTY_FAILURE, finalUsage) };
|
|
1040
|
+
}
|
|
922
1041
|
yield* emitRetained(fallbackEvents);
|
|
923
1042
|
return {
|
|
924
1043
|
assistantText,
|
|
@@ -962,6 +1081,34 @@ async function* parseKiroAttemptEvents(
|
|
|
962
1081
|
terminal: { type: "done", usage: finalUsage, endTurn: false, ...(finalProviderState ? { providerState: finalProviderState } : {}) },
|
|
963
1082
|
};
|
|
964
1083
|
}
|
|
1084
|
+
if (mode === "required" && structuredOutput) {
|
|
1085
|
+
// Any stop reason other than a clean one falls through to its own handling below: a failed
|
|
1086
|
+
// structured answer under MAX_TOKENS, a content filter or an exhausted context window earns no
|
|
1087
|
+
// retry, and a MAX_TOKENS cut is never mistaken for an answer even when its prefix parses.
|
|
1088
|
+
const cleanStop = isKiroStructuredCleanStop(normalizedStopReason);
|
|
1089
|
+
if (structuredRetry && cleanStop) {
|
|
1090
|
+
return { assistantText, sawReasoning, needsFallback: true, usage: finalUsage, providerState: finalProviderState };
|
|
1091
|
+
}
|
|
1092
|
+
// A model that wrote the requested JSON as text, and stopped cleanly, has answered: accept it
|
|
1093
|
+
// without a second paid request.
|
|
1094
|
+
const json = sawText && cleanStop && completionCalls === 0
|
|
1095
|
+
? parseKiroStructuredText(structuredOutput, assistantText)
|
|
1096
|
+
: undefined;
|
|
1097
|
+
if (json !== undefined) {
|
|
1098
|
+
yield* consumeSupersededByCompletion(deferred);
|
|
1099
|
+
yield { type: "text_delta", text: json, phase: "final_answer" };
|
|
1100
|
+
return {
|
|
1101
|
+
assistantText,
|
|
1102
|
+
sawReasoning,
|
|
1103
|
+
terminal: { type: "done", usage: finalUsage, endTurn: true, ...(finalProviderState ? { providerState: finalProviderState } : {}) },
|
|
1104
|
+
};
|
|
1105
|
+
}
|
|
1106
|
+
// A clean stop without an accepted answer, prose or nothing at all, earns the one bounded
|
|
1107
|
+
// retry: an empty END_TURN is as recoverable as prose, and the caller still needs JSON.
|
|
1108
|
+
if (cleanStop) {
|
|
1109
|
+
return { assistantText, sawReasoning, needsFallback: true, usage: finalUsage, providerState: finalProviderState };
|
|
1110
|
+
}
|
|
1111
|
+
}
|
|
965
1112
|
if (mode === "required" && nativeCompletionStop) {
|
|
966
1113
|
return {
|
|
967
1114
|
assistantText,
|
|
@@ -1101,6 +1248,10 @@ async function* parseKiroAttemptEvents(
|
|
|
1101
1248
|
}
|
|
1102
1249
|
}
|
|
1103
1250
|
|
|
1251
|
+
/**
|
|
1252
|
+
* Parse a Kiro response into adapter events, running at most one bounded retry through
|
|
1253
|
+
* `fallbackFactory` when the first attempt ends without a validated answer.
|
|
1254
|
+
*/
|
|
1104
1255
|
export async function* parseKiroStream(
|
|
1105
1256
|
response: Response,
|
|
1106
1257
|
budget: TranslatorBudget,
|
|
@@ -1112,8 +1263,11 @@ export async function* parseKiroStream(
|
|
|
1112
1263
|
completionMode: KiroCompletionMode = "disabled",
|
|
1113
1264
|
fallbackFactory?: KiroFallbackFactory,
|
|
1114
1265
|
contextInputEstimate?: number,
|
|
1266
|
+
structuredOutput?: KiroStructuredOutputSpec,
|
|
1115
1267
|
): AsyncGenerator<AdapterEvent> {
|
|
1116
1268
|
const contextWindowState: KiroContextWindowState = { value: contextWindow };
|
|
1269
|
+
// See `dropHeldProse` in parseKiroAttempt: a structured-output caller never receives held prose.
|
|
1270
|
+
const dropHeldProse = structuredOutput !== undefined;
|
|
1117
1271
|
const firstResult = yield* parseKiroAttempt(
|
|
1118
1272
|
response,
|
|
1119
1273
|
budget,
|
|
@@ -1125,6 +1279,8 @@ export async function* parseKiroStream(
|
|
|
1125
1279
|
conversationId,
|
|
1126
1280
|
contextInputEstimate,
|
|
1127
1281
|
false,
|
|
1282
|
+
undefined,
|
|
1283
|
+
structuredOutput,
|
|
1128
1284
|
);
|
|
1129
1285
|
try {
|
|
1130
1286
|
if (!firstResult.needsFallback) {
|
|
@@ -1132,7 +1288,7 @@ export async function* parseKiroStream(
|
|
|
1132
1288
|
return;
|
|
1133
1289
|
}
|
|
1134
1290
|
if (!fallbackFactory) {
|
|
1135
|
-
yield* firstResult.drainDeferred();
|
|
1291
|
+
yield* firstResult.drainDeferred(dropHeldProse);
|
|
1136
1292
|
yield retryableKiroIncomplete(
|
|
1137
1293
|
"uncompleted_kiro_response",
|
|
1138
1294
|
"Kiro produced progress without an explicit final answer and no bounded retry transport was available",
|
|
@@ -1157,7 +1313,7 @@ export async function* parseKiroStream(
|
|
|
1157
1313
|
budget,
|
|
1158
1314
|
);
|
|
1159
1315
|
} catch (err) {
|
|
1160
|
-
yield* firstResult.drainDeferred();
|
|
1316
|
+
yield* firstResult.drainDeferred(dropHeldProse);
|
|
1161
1317
|
firstAssistantText = "";
|
|
1162
1318
|
firstResult.assistantText = "";
|
|
1163
1319
|
firstResult.releaseRetained();
|
|
@@ -1189,7 +1345,7 @@ export async function* parseKiroStream(
|
|
|
1189
1345
|
firstResult.releaseCollectors();
|
|
1190
1346
|
fallback.releaseRequestBody?.();
|
|
1191
1347
|
if (!fallback.response.ok) {
|
|
1192
|
-
yield* firstResult.drainDeferred();
|
|
1348
|
+
yield* firstResult.drainDeferred(dropHeldProse);
|
|
1193
1349
|
const payload = await readDisplaySafeErrorPayloadText(fallback.response, fallback.abortSignal);
|
|
1194
1350
|
const failure = classifyKiroHttpError(fallback.response.status, fallback.response.headers, payload);
|
|
1195
1351
|
yield {
|
|
@@ -1217,6 +1373,7 @@ export async function* parseKiroStream(
|
|
|
1217
1373
|
// Failed validation will release the held first-attempt progress.
|
|
1218
1374
|
priorEmittedOutput,
|
|
1219
1375
|
firstResult,
|
|
1376
|
+
structuredOutput,
|
|
1220
1377
|
);
|
|
1221
1378
|
try {
|
|
1222
1379
|
if (!secondResult.terminal) {
|
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Lexeme-preserving JSON reading and exact decimal arithmetic for Kiro strict structured output.
|
|
3
|
+
*
|
|
4
|
+
* Strict validation has to judge the bytes it relays. `JSON.parse` rounds every number to a double,
|
|
5
|
+
* so `9007199254740993` would compare equal to a `const` of `9007199254740992` and
|
|
6
|
+
* `10.0000000000000001` would pass `maximum: 10`, while the client receives the original text.
|
|
7
|
+
* This reader keeps each number's lexeme, and the decimal helpers compare and divide those lexemes
|
|
8
|
+
* exactly, without converting them to doubles.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
/** One parsed JSON value with its byte span in the source text. */
|
|
12
|
+
export type KiroJsonValue = { start: number; end: number } & (
|
|
13
|
+
| { kind: "null" }
|
|
14
|
+
| { kind: "boolean"; value: boolean }
|
|
15
|
+
| { kind: "number"; lexeme: string }
|
|
16
|
+
| { kind: "string"; value: string }
|
|
17
|
+
| { kind: "array"; items: KiroJsonValue[] }
|
|
18
|
+
| { kind: "object"; entries: Map<string, KiroJsonValue> }
|
|
19
|
+
);
|
|
20
|
+
|
|
21
|
+
/** A parsed document and how many values it holds, which sizes the validation budget. */
|
|
22
|
+
export interface KiroJsonDocument {
|
|
23
|
+
value: KiroJsonValue;
|
|
24
|
+
nodes: number;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** Deepest array/object nesting the reader accepts; deeper values are reported as non-conforming. */
|
|
28
|
+
export const KIRO_JSON_MAX_NESTING = 512;
|
|
29
|
+
|
|
30
|
+
const WHITESPACE = new Set([" ", "\t", "\n", "\r"]);
|
|
31
|
+
const NUMBER = /-?(?:0|[1-9]\d*)(?:\.\d+)?(?:[eE][+-]?\d+)?/y;
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Read `text` into a lexeme-preserving tree, or undefined when it is not JSON or nests deeper than
|
|
35
|
+
* {@link KIRO_JSON_MAX_NESTING}. Duplicate object keys keep the last value, as `JSON.parse` does.
|
|
36
|
+
*/
|
|
37
|
+
export function readKiroJson(text: string): KiroJsonDocument | undefined {
|
|
38
|
+
let pos = 0;
|
|
39
|
+
let nodes = 0;
|
|
40
|
+
const skip = (): void => {
|
|
41
|
+
while (pos < text.length && WHITESPACE.has(text[pos]!)) pos++;
|
|
42
|
+
};
|
|
43
|
+
const readString = (): string => {
|
|
44
|
+
const start = pos;
|
|
45
|
+
pos++;
|
|
46
|
+
while (pos < text.length && text[pos] !== '"') pos += text[pos] === "\\" ? 2 : 1;
|
|
47
|
+
if (pos >= text.length) throw new SyntaxError("unterminated string");
|
|
48
|
+
pos++;
|
|
49
|
+
return JSON.parse(text.slice(start, pos)) as string;
|
|
50
|
+
};
|
|
51
|
+
const read = (depth: number): KiroJsonValue => {
|
|
52
|
+
if (depth > KIRO_JSON_MAX_NESTING) throw new RangeError("nesting");
|
|
53
|
+
skip();
|
|
54
|
+
nodes++;
|
|
55
|
+
const start = pos;
|
|
56
|
+
const ch = text[pos];
|
|
57
|
+
if (ch === '"') {
|
|
58
|
+
const value = readString();
|
|
59
|
+
return { kind: "string", value, start, end: pos };
|
|
60
|
+
}
|
|
61
|
+
if (ch === "[" || ch === "{") {
|
|
62
|
+
pos++;
|
|
63
|
+
skip();
|
|
64
|
+
const close = ch === "[" ? "]" : "}";
|
|
65
|
+
const items: KiroJsonValue[] = [];
|
|
66
|
+
const entries = new Map<string, KiroJsonValue>();
|
|
67
|
+
if (text[pos] === close) pos++;
|
|
68
|
+
else {
|
|
69
|
+
for (;;) {
|
|
70
|
+
if (ch === "[") items.push(read(depth + 1));
|
|
71
|
+
else {
|
|
72
|
+
if (text[pos] !== '"') throw new SyntaxError("expected a key");
|
|
73
|
+
const key = readString();
|
|
74
|
+
skip();
|
|
75
|
+
if (text[pos++] !== ":") throw new SyntaxError("expected a colon");
|
|
76
|
+
entries.set(key, read(depth + 1));
|
|
77
|
+
}
|
|
78
|
+
skip();
|
|
79
|
+
const next = text[pos++];
|
|
80
|
+
if (next === close) break;
|
|
81
|
+
if (next !== ",") throw new SyntaxError("expected a comma");
|
|
82
|
+
skip();
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
return ch === "["
|
|
86
|
+
? { kind: "array", items, start, end: pos }
|
|
87
|
+
: { kind: "object", entries, start, end: pos };
|
|
88
|
+
}
|
|
89
|
+
for (const [word, value] of [["true", true], ["false", false], ["null", null]] as const) {
|
|
90
|
+
if (text.startsWith(word, pos)) {
|
|
91
|
+
pos += word.length;
|
|
92
|
+
return value === null ? { kind: "null", start, end: pos } : { kind: "boolean", value, start, end: pos };
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
NUMBER.lastIndex = pos;
|
|
96
|
+
const match = NUMBER.exec(text);
|
|
97
|
+
if (!match) throw new SyntaxError("unexpected token");
|
|
98
|
+
pos += match[0].length;
|
|
99
|
+
return { kind: "number", lexeme: match[0], start, end: pos };
|
|
100
|
+
};
|
|
101
|
+
try {
|
|
102
|
+
const value = read(0);
|
|
103
|
+
skip();
|
|
104
|
+
return pos === text.length ? { value, nodes } : undefined;
|
|
105
|
+
} catch {
|
|
106
|
+
return undefined;
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* An exact decimal: `sign × 0.digits × 10^exponent`, with `digits` free of leading and trailing
|
|
112
|
+
* zeros. Zero has sign 0 and empty digits. The exponent is a bigint because a lexeme such as
|
|
113
|
+
* `1e99999999999999999999` is valid JSON.
|
|
114
|
+
*/
|
|
115
|
+
export interface KiroDecimal {
|
|
116
|
+
sign: -1 | 0 | 1;
|
|
117
|
+
digits: string;
|
|
118
|
+
exponent: bigint;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
const DECIMAL = /^(-?)(\d+)(?:\.(\d+))?(?:[eE]([+-]?\d+))?$/;
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* Largest exponent magnitude kept exactly. A model-written exponent can be millions of digits long,
|
|
125
|
+
* and converting it to a bigint costs more than linear time, so a longer one saturates here. No
|
|
126
|
+
* lexeme within the answer size limits has as many digits, so a saturated value still compares,
|
|
127
|
+
* and tests as an integer, exactly as its true value would against any schema bound.
|
|
128
|
+
*/
|
|
129
|
+
const EXPONENT_LIMIT = 10n ** 15n;
|
|
130
|
+
|
|
131
|
+
/** The exponent text as a bigint, saturated at {@link EXPONENT_LIMIT}; linear in its length. */
|
|
132
|
+
function boundedExponent(exp: string): bigint {
|
|
133
|
+
const negative = exp.startsWith("-");
|
|
134
|
+
let start = exp.startsWith("-") || exp.startsWith("+") ? 1 : 0;
|
|
135
|
+
while (start < exp.length - 1 && exp.charCodeAt(start) === 48) start += 1;
|
|
136
|
+
const magnitude = exp.length - start > 15 ? EXPONENT_LIMIT : BigInt(exp.slice(start));
|
|
137
|
+
return negative ? -magnitude : magnitude;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/** The exact value of a JSON number lexeme (or of `String(n)` for a finite double). */
|
|
141
|
+
export function kiroDecimal(lexeme: string): KiroDecimal {
|
|
142
|
+
const match = DECIMAL.exec(lexeme);
|
|
143
|
+
if (!match) throw new Error(`not a JSON number: ${lexeme}`);
|
|
144
|
+
const [, minus, whole = "", fraction = "", exp = "0"] = match;
|
|
145
|
+
const all = whole + fraction;
|
|
146
|
+
const lead = all.search(/[1-9]/);
|
|
147
|
+
if (lead < 0) return { sign: 0, digits: "", exponent: 0n };
|
|
148
|
+
// One backward scan for trailing zeros: `/0+$/` retries a long zero run at every position.
|
|
149
|
+
let end = all.length;
|
|
150
|
+
while (end > lead && all.charCodeAt(end - 1) === 48) end -= 1;
|
|
151
|
+
return {
|
|
152
|
+
sign: minus ? -1 : 1,
|
|
153
|
+
digits: all.slice(lead, end),
|
|
154
|
+
exponent: BigInt(whole.length - lead) + boundedExponent(exp),
|
|
155
|
+
};
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/** -1, 0 or 1 as `a` is below, equal to or above `b`. */
|
|
159
|
+
export function compareKiroDecimal(a: KiroDecimal, b: KiroDecimal): number {
|
|
160
|
+
if (a.sign !== b.sign) return a.sign < b.sign ? -1 : 1;
|
|
161
|
+
if (a.sign === 0) return 0;
|
|
162
|
+
let magnitude = 0;
|
|
163
|
+
if (a.exponent !== b.exponent) magnitude = a.exponent < b.exponent ? -1 : 1;
|
|
164
|
+
// Without trailing zeros, digit strings order as their fractions do: "12" < "123" < "13".
|
|
165
|
+
else if (a.digits !== b.digits) magnitude = a.digits < b.digits ? -1 : 1;
|
|
166
|
+
return magnitude * a.sign;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/** Whether the decimal is a whole number. */
|
|
170
|
+
export function isKiroDecimalInteger(value: KiroDecimal): boolean {
|
|
171
|
+
return value.sign === 0 || BigInt(value.digits.length) <= value.exponent;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/** `base^power mod modulus` by square-and-multiply, so a huge exponent costs only its bit length. */
|
|
175
|
+
function modPow(base: bigint, power: bigint, modulus: bigint): bigint {
|
|
176
|
+
let result = 1n % modulus;
|
|
177
|
+
let factor = base % modulus;
|
|
178
|
+
for (let rest = power; rest > 0n; rest >>= 1n) {
|
|
179
|
+
if (rest & 1n) result = (result * factor) % modulus;
|
|
180
|
+
factor = (factor * factor) % modulus;
|
|
181
|
+
}
|
|
182
|
+
return result;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/** Whether the decimal is an integer multiple of the positive integer `divisor`. */
|
|
186
|
+
export function isKiroDecimalMultipleOf(value: KiroDecimal, divisor: bigint): boolean {
|
|
187
|
+
if (value.sign === 0) return true;
|
|
188
|
+
if (!isKiroDecimalInteger(value)) return false;
|
|
189
|
+
const zeros = value.exponent - BigInt(value.digits.length);
|
|
190
|
+
return (BigInt(value.digits) % divisor) * modPow(10n, zeros, divisor) % divisor === 0n;
|
|
191
|
+
}
|