@herbertgao/pi-extensions 2026.11.3 → 2026.11.4
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 +9 -0
- package/README.md +7 -7
- package/THIRD_PARTY_NOTICES.md +2 -2
- package/node_modules/@herbertgao/pi-cc-extensions/CHANGELOG.md +6 -0
- package/node_modules/@herbertgao/pi-cc-extensions/README.en.md +2 -2
- package/node_modules/@herbertgao/pi-cc-extensions/README.md +2 -2
- package/node_modules/@herbertgao/pi-cc-extensions/extensions/renderer/mouse/interaction.ts +33 -5
- package/node_modules/@herbertgao/pi-cc-extensions/extensions/renderer/tool/ccstyle-tool.ts +72 -17
- package/node_modules/@herbertgao/pi-cc-extensions/extensions/renderer/tool/grouping.ts +10 -2
- package/node_modules/@herbertgao/pi-cc-extensions/extensions/renderer/tool/tool-io.ts +16 -7
- package/node_modules/@herbertgao/pi-cc-extensions/package.json +7 -5
- package/node_modules/@herbertgao/pi-cc-extensions/themes/cc-dark-hc.json +92 -0
- package/node_modules/@herbertgao/pi-cc-extensions/themes/cc-light-hc.json +92 -0
- package/node_modules/pi-commandcode-provider/CHANGELOG.md +27 -0
- package/node_modules/pi-commandcode-provider/CONTRIBUTING.md +64 -5
- package/node_modules/pi-commandcode-provider/README.md +40 -26
- package/node_modules/pi-commandcode-provider/RELEASE.md +25 -0
- package/node_modules/pi-commandcode-provider/index.ts +212 -38
- package/node_modules/pi-commandcode-provider/package.json +9 -8
- package/node_modules/pi-commandcode-provider/scripts/live-e2e-profile.mjs +6 -13
- package/node_modules/pi-commandcode-provider/scripts/typecheck-entrypoint.mjs +88 -0
- package/node_modules/pi-commandcode-provider/src/api-key.ts +43 -2
- package/node_modules/pi-commandcode-provider/src/commandcode-catalog-overrides.ts +1 -1
- package/node_modules/pi-commandcode-provider/src/commandcode-catalog.ts +9 -2
- package/node_modules/pi-commandcode-provider/src/cost.ts +3 -37
- package/node_modules/pi-commandcode-provider/src/diagnostics.ts +215 -0
- package/node_modules/pi-commandcode-provider/src/oauth.ts +1 -1
- package/node_modules/pi-commandcode-provider/src/quota-command.ts +1 -1
- package/node_modules/pi-commandcode-provider/src/routing.ts +166 -0
- package/node_modules/pi-commandcode-provider/src/runtime.ts +15 -9
- package/node_modules/pi-commandcode-provider/src/transport.ts +79 -85
- package/node_modules/pi-commandcode-provider/src/types.ts +20 -39
- package/node_modules/pi-commandcode-provider/src/usage.ts +1 -1
- package/node_modules/pi-lens/CHANGELOG-USER.md +31 -0
- package/node_modules/pi-lens/CHANGELOG.md +178 -0
- package/node_modules/pi-lens/README.md +2 -1
- package/node_modules/pi-lens/config/dependency-cruiser-eager-allowlist.json +1 -1
- package/node_modules/pi-lens/dist/{chunk-3O5EII3W.js → chunk-2VZ3VN3N.js} +2 -2
- package/node_modules/pi-lens/dist/{chunk-SDFKOLSF.js → chunk-3HTZDY3C.js} +8 -6
- package/node_modules/pi-lens/dist/{chunk-A5GRLYPZ.js → chunk-3NPWO5UO.js} +7 -13
- package/node_modules/pi-lens/dist/{chunk-AGGUWKRK.js → chunk-47UCC4MM.js} +1 -1
- package/node_modules/pi-lens/dist/chunk-4ENEJHF4.js +198 -0
- package/node_modules/pi-lens/dist/{chunk-WXUDRAG6.js → chunk-5GEYJJHG.js} +10 -12
- package/node_modules/pi-lens/dist/{chunk-HHR7WGCI.js → chunk-5YSEQHJN.js} +3 -3
- package/node_modules/pi-lens/dist/{chunk-L3CLUGDY.js → chunk-6GPB3JLL.js} +3 -3
- package/node_modules/pi-lens/dist/{chunk-5F3XF3SX.js → chunk-7R5GZIA6.js} +6 -6
- package/node_modules/pi-lens/dist/{chunk-CUDD35TW.js → chunk-7UTSNTWM.js} +4 -4
- package/node_modules/pi-lens/dist/{chunk-DYRZGUMA.js → chunk-7YTRG6YW.js} +2 -2
- package/node_modules/pi-lens/dist/{chunk-JZRP4DGP.js → chunk-AACN5GNE.js} +2 -2
- package/node_modules/pi-lens/dist/{chunk-7ANZ7XDD.js → chunk-ACRJ2GZ5.js} +2 -2
- package/node_modules/pi-lens/dist/{chunk-6FIRFP6A.js → chunk-C3YRO3EU.js} +10 -12
- package/node_modules/pi-lens/dist/{chunk-GMROJJRN.js → chunk-CB7PEVOJ.js} +9 -1
- package/node_modules/pi-lens/dist/{chunk-BBET6HD6.js → chunk-CDFJF5YD.js} +1 -1
- package/node_modules/pi-lens/dist/{chunk-NSPVR3FN.js → chunk-CJS6WE66.js} +17 -19
- package/node_modules/pi-lens/dist/{chunk-6YUNI7B5.js → chunk-CM6BDY4E.js} +14 -60
- package/node_modules/pi-lens/dist/{chunk-G5GV2Q7K.js → chunk-CZCAZVTD.js} +16 -2
- package/node_modules/pi-lens/dist/chunk-DGKFWQK4.js +23 -0
- package/node_modules/pi-lens/dist/{chunk-VOP4PJKA.js → chunk-E65WJVI5.js} +7 -9
- package/node_modules/pi-lens/dist/{chunk-TV6RXRYH.js → chunk-EKNYSXFB.js} +8 -10
- package/node_modules/pi-lens/dist/{chunk-4LL2VRNY.js → chunk-EY6LY2HO.js} +16 -18
- package/node_modules/pi-lens/dist/{chunk-MC6VB2E5.js → chunk-F6BEQWAK.js} +17 -5
- package/node_modules/pi-lens/dist/{chunk-R2QQC33W.js → chunk-FGNSESAC.js} +12 -14
- package/node_modules/pi-lens/dist/{chunk-OC7VWJBZ.js → chunk-FRUBVFXV.js} +35 -37
- package/node_modules/pi-lens/dist/{chunk-7CGIDRIK.js → chunk-G22UNQID.js} +14 -16
- package/node_modules/pi-lens/dist/{chunk-L4NEVZ4T.js → chunk-GQTQUNCG.js} +25 -27
- package/node_modules/pi-lens/dist/{chunk-ZKLTNLAW.js → chunk-H3BVMK47.js} +6 -6
- package/node_modules/pi-lens/dist/chunk-H6FLQ5NW.js +28 -0
- package/node_modules/pi-lens/dist/{chunk-2ZE3AUJQ.js → chunk-HEA3EM23.js} +13 -15
- package/node_modules/pi-lens/dist/{chunk-CZL44WFN.js → chunk-HSOBIN22.js} +2 -2
- package/node_modules/pi-lens/dist/{chunk-PKSUMEPT.js → chunk-HYSKJMZK.js} +3 -3
- package/node_modules/pi-lens/dist/{chunk-VN3L2JWZ.js → chunk-IICERRFF.js} +5 -5
- package/node_modules/pi-lens/dist/{chunk-O4N5BQ3T.js → chunk-IILEH3ZC.js} +8 -10
- package/node_modules/pi-lens/dist/{chunk-WC6WRNQT.js → chunk-J3Q2NYJR.js} +2 -2
- package/node_modules/pi-lens/dist/{chunk-SFLHSL5R.js → chunk-JUZ67QZQ.js} +18 -7
- package/node_modules/pi-lens/dist/chunk-KR5VGMVT.js +25 -0
- package/node_modules/pi-lens/dist/{chunk-UHHLR2VY.js → chunk-KV6GQOGN.js} +38 -41
- package/node_modules/pi-lens/dist/{chunk-HUAM7QG3.js → chunk-KXMJLOZF.js} +25 -23
- package/node_modules/pi-lens/dist/{chunk-BUZIQDFS.js → chunk-L4Y3ROKQ.js} +3 -3
- package/node_modules/pi-lens/dist/{chunk-JBP7KESM.js → chunk-L6WBJGQS.js} +1 -1
- package/node_modules/pi-lens/dist/{chunk-35WKJWO5.js → chunk-L7O774U7.js} +14 -14
- package/node_modules/pi-lens/dist/{chunk-BOPDQ7AZ.js → chunk-LAHHGNAY.js} +19 -21
- package/node_modules/pi-lens/dist/{chunk-RGGH6M34.js → chunk-LT62P654.js} +1 -1
- package/node_modules/pi-lens/dist/{chunk-5T5A6YAJ.js → chunk-LWOM6LVS.js} +33 -34
- package/node_modules/pi-lens/dist/{chunk-JYLOYTZD.js → chunk-MKPDIAZH.js} +5 -1
- package/node_modules/pi-lens/dist/{chunk-CM73PPGD.js → chunk-MN635QYA.js} +3 -4
- package/node_modules/pi-lens/dist/{chunk-BQG2QFVO.js → chunk-MQ4PPCJB.js} +21 -29
- package/node_modules/pi-lens/dist/{chunk-HXFUTMX2.js → chunk-MRLH4MYU.js} +2 -2
- package/node_modules/pi-lens/dist/{chunk-XOMGO63G.js → chunk-N56BNAA6.js} +19 -21
- package/node_modules/pi-lens/dist/{chunk-7MAD5ZAY.js → chunk-N5CEGQPG.js} +14 -16
- package/node_modules/pi-lens/dist/{chunk-ZFORSIED.js → chunk-NCLO6E6N.js} +13 -33
- package/node_modules/pi-lens/dist/{chunk-SQC7K3SP.js → chunk-NL5LSKVD.js} +2 -2
- package/node_modules/pi-lens/dist/{chunk-I22THIHI.js → chunk-NMUSOZ2D.js} +10 -12
- package/node_modules/pi-lens/dist/{chunk-4NBWFYWW.js → chunk-NOWH2VA2.js} +8 -33
- package/node_modules/pi-lens/dist/{chunk-NM2NWH55.js → chunk-NUSXS6YB.js} +6 -5
- package/node_modules/pi-lens/dist/{chunk-OZWXLJMX.js → chunk-OSEKJEUU.js} +2 -2
- package/node_modules/pi-lens/dist/{chunk-2BKUFQKR.js → chunk-OUYAUXAT.js} +405 -181
- package/node_modules/pi-lens/dist/{chunk-JPRDTY5S.js → chunk-PBZNYHMM.js} +8 -10
- package/node_modules/pi-lens/dist/{chunk-JE6HKLC7.js → chunk-PIIUNJIT.js} +2 -4
- package/node_modules/pi-lens/dist/{chunk-ZZDIOAAJ.js → chunk-PRHYIA6X.js} +6 -6
- package/node_modules/pi-lens/dist/{chunk-4W52ZKZX.js → chunk-PXQ45SGD.js} +2 -2
- package/node_modules/pi-lens/dist/chunk-Q6JQKWPK.js +25 -0
- package/node_modules/pi-lens/dist/{chunk-PIQQM32Y.js → chunk-QB7T2PHK.js} +2 -2
- package/node_modules/pi-lens/dist/{chunk-HKIL6DCL.js → chunk-QGITU23N.js} +1 -1
- package/node_modules/pi-lens/dist/chunk-QJJKOHLH.js +57 -0
- package/node_modules/pi-lens/dist/{chunk-6LJOJ6EO.js → chunk-QP33NKYC.js} +14 -16
- package/node_modules/pi-lens/dist/{chunk-DIWK3LBC.js → chunk-QVBOXT3L.js} +8 -10
- package/node_modules/pi-lens/dist/{chunk-IYVYZQRL.js → chunk-RNJFRXEN.js} +5 -7
- package/node_modules/pi-lens/dist/{chunk-G6JKM4O7.js → chunk-RNXOWDM6.js} +10 -10
- package/node_modules/pi-lens/dist/{chunk-5RNB4HVK.js → chunk-RTFMDIKV.js} +1 -1
- package/node_modules/pi-lens/dist/{chunk-XWIME22V.js → chunk-SIQHOB6P.js} +11 -13
- package/node_modules/pi-lens/dist/{chunk-T2S3FNNJ.js → chunk-SLSMJPEF.js} +41 -42
- package/node_modules/pi-lens/dist/chunk-T5QSLSMM.js +50 -0
- package/node_modules/pi-lens/dist/chunk-TGQDAG4B.js +88 -0
- package/node_modules/pi-lens/dist/{chunk-6GMGEKQN.js → chunk-THYHUIDR.js} +39 -12
- package/node_modules/pi-lens/dist/{chunk-WMDUT4KH.js → chunk-TMNUTMT5.js} +72 -89
- package/node_modules/pi-lens/dist/{chunk-7TNILKGY.js → chunk-TOE2RRCS.js} +1 -1
- package/node_modules/pi-lens/dist/chunk-TYHPVE5H.js +25 -0
- package/node_modules/pi-lens/dist/chunk-UAKO2M6B.js +73 -0
- package/node_modules/pi-lens/dist/chunk-UC5AXW5Z.js +20 -0
- package/node_modules/pi-lens/dist/chunk-UTJP6WOS.js +28 -0
- package/node_modules/pi-lens/dist/{chunk-FRLAUP2B.js → chunk-UXTQ3QPD.js} +4 -4
- package/node_modules/pi-lens/dist/{chunk-4QG55RRC.js → chunk-V2WDBOLZ.js} +10 -12
- package/node_modules/pi-lens/dist/{chunk-FHEV6T4L.js → chunk-V67D6TMU.js} +8 -10
- package/node_modules/pi-lens/dist/{chunk-VLQ7UKZH.js → chunk-VCRVNLE7.js} +135 -2
- package/node_modules/pi-lens/dist/{chunk-KZ4ALBCH.js → chunk-VO4JFPTV.js} +2 -2
- package/node_modules/pi-lens/dist/{chunk-GIRBSBGL.js → chunk-VWLSU3XV.js} +3 -3
- package/node_modules/pi-lens/dist/{chunk-2453RRJ2.js → chunk-W5E72HJA.js} +59 -22
- package/node_modules/pi-lens/dist/{chunk-VPLBLWFO.js → chunk-W7TY6HYX.js} +1 -1
- package/node_modules/pi-lens/dist/{chunk-SP4TFY5F.js → chunk-WSSLVMDA.js} +11 -13
- package/node_modules/pi-lens/dist/{chunk-PPKECBPP.js → chunk-WTB4GKMH.js} +10 -1
- package/node_modules/pi-lens/dist/{chunk-X35S7HSB.js → chunk-X5BJ3G2P.js} +41 -30
- package/node_modules/pi-lens/dist/{chunk-X66YJ2SS.js → chunk-XKFO2SZV.js} +33 -34
- package/node_modules/pi-lens/dist/{chunk-OWTG6UMO.js → chunk-XLAI7UYY.js} +75 -22
- package/node_modules/pi-lens/dist/{chunk-4B2YFN4U.js → chunk-XO756WE5.js} +45 -33
- package/node_modules/pi-lens/dist/{chunk-VOXTCULR.js → chunk-XVB3Q2PG.js} +2 -4
- package/node_modules/pi-lens/dist/{chunk-XYLC35E4.js → chunk-YOXCCJDF.js} +517 -308
- package/node_modules/pi-lens/dist/{chunk-O2ARHQKF.js → chunk-ZCQBNPRH.js} +2 -2
- package/node_modules/pi-lens/dist/chunk-ZD4KVFXI.js +234 -0
- package/node_modules/pi-lens/dist/{chunk-LSFXZLPW.js → chunk-ZDMFGACN.js} +184 -235
- package/node_modules/pi-lens/dist/{chunk-4BKU7PV3.js → chunk-ZMMHHXSW.js} +1 -1
- package/node_modules/pi-lens/dist/{chunk-OY7WZI7Q.js → chunk-ZPM7ZIFL.js} +6 -6
- package/node_modules/pi-lens/dist/{chunk-R4QWJQ2H.js → chunk-ZVCDHYWZ.js} +9 -11
- package/node_modules/pi-lens/dist/{chunk-RZG472YW.js → chunk-ZZN74OGD.js} +83 -46
- package/node_modules/pi-lens/dist/{chunk-BHWW6FAX.js → chunk-ZZSJAR76.js} +37 -19
- package/node_modules/pi-lens/dist/index.js +4910 -4218
- package/node_modules/pi-lens/dist/mcp/analyze-cli.js +12 -17
- package/node_modules/pi-lens/dist/mcp/cli.js +41 -42
- package/node_modules/pi-lens/dist/mcp/server.js +1451 -648
- package/node_modules/pi-lens/dist/mcp/worker.js +59 -60
- package/node_modules/pi-lens/dist/probes/installer.js +17 -19
- package/node_modules/pi-lens/docs/agent-tools.md +40 -8
- package/node_modules/pi-lens/docs/analysisall.md +1 -1
- package/node_modules/pi-lens/docs/ci-bot-pr-checks.md +7 -5
- package/node_modules/pi-lens/docs/configuration.md +51 -9
- package/node_modules/pi-lens/docs/defect-shapes.md +52 -3
- package/node_modules/pi-lens/docs/environment-variables.md +18 -1
- package/node_modules/pi-lens/docs/features.md +12 -5
- package/node_modules/pi-lens/docs/globalconfig.md +11 -3
- package/node_modules/pi-lens/docs/invariants/lsp-trust-exec.md +9 -2
- package/node_modules/pi-lens/docs/invariants/session-telemetry.md +2 -2
- package/node_modules/pi-lens/docs/language-coverage.md +1 -0
- package/node_modules/pi-lens/docs/lsp-capability-matrix.md +57 -35
- package/node_modules/pi-lens/docs/lsp-idle-eviction.md +4 -1
- package/node_modules/pi-lens/docs/merge-queue-rollout.md +10 -8
- package/node_modules/pi-lens/docs/module-report-read-symbol.md +34 -23
- package/node_modules/pi-lens/docs/pi-lens-fixer.md +11 -1
- package/node_modules/pi-lens/docs/pi-lens-investigator.md +3 -0
- package/node_modules/pi-lens/docs/pi-lens-merge-policy.md +15 -7
- package/node_modules/pi-lens/docs/pi-lens-reviewer.md +1 -1
- package/node_modules/pi-lens/docs/pi-lens-subagent.md +13 -0
- package/node_modules/pi-lens/docs/public-api-stability.md +39 -5
- package/node_modules/pi-lens/docs/release-qa-baseline.md +7 -4
- package/node_modules/pi-lens/docs/schema/pi-lens-config-v1.json +11 -2
- package/node_modules/pi-lens/docs/servercapabilities.md +3 -1
- package/node_modules/pi-lens/docs/settings.md +2 -2
- package/node_modules/pi-lens/docs/word-index.md +3 -2
- package/node_modules/pi-lens/package.json +8 -3
- package/node_modules/pi-lens/rules/ast-grep-rules/rules/no-sql-in-code-execute-js.yml +1 -2
- package/node_modules/pi-lens/rules/ast-grep-rules/rules/no-sql-in-code-execute.yml +1 -2
- package/node_modules/pi-lens/scripts/download-grammars.js +3 -2
- package/node_modules/pi-lens/skills/pi-lens-lsp-navigation/SKILL.md +5 -1
- package/node_modules/pi-provider-qoder/dist/index.js +473 -62
- package/node_modules/pi-provider-qoder/package.json +1 -1
- package/package.json +10 -8
- package/node_modules/pi-commandcode-provider/src/converters.ts +0 -419
- package/node_modules/pi-commandcode-provider/src/core.ts +0 -861
- package/node_modules/pi-commandcode-provider/src/json-schema.ts +0 -417
- package/node_modules/pi-commandcode-provider/src/transcript.ts +0 -56
- package/node_modules/pi-lens/dist/chunk-2TS4GLOJ.js +0 -31
- package/node_modules/pi-lens/dist/chunk-3JAHQBSY.js +0 -27
- package/node_modules/pi-lens/dist/chunk-447YZL4S.js +0 -25
- package/node_modules/pi-lens/dist/chunk-565VRUP4.js +0 -136
- package/node_modules/pi-lens/dist/chunk-57X2CDRX.js +0 -49
- package/node_modules/pi-lens/dist/chunk-GGYOZWHW.js +0 -27
- package/node_modules/pi-lens/dist/chunk-IE5D3RA5.js +0 -58
- package/node_modules/pi-lens/dist/chunk-LPYTPNOJ.js +0 -22
- package/node_modules/pi-lens/dist/chunk-OFNMHPBT.js +0 -89
- package/node_modules/pi-lens/dist/chunk-U3AS2FR6.js +0 -106
- package/node_modules/pi-lens/dist/chunk-W3BGKBBK.js +0 -27
- package/node_modules/pi-lens/dist/chunk-WQV6BI4Q.js +0 -48
- package/node_modules/pi-lens/dist/chunk-YHGYAEFH.js +0 -443
- package/node_modules/pi-lens/dist/chunk-ZONZ6YY3.js +0 -74
- package/node_modules/pi-lens/dist/chunk-ZSM35JF4.js +0 -30
|
@@ -1,861 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Testable Command Code provider core.
|
|
3
|
-
*
|
|
4
|
-
* The runtime imports live in index.ts; this module takes injected stream/cost
|
|
5
|
-
* dependencies so tests can exercise the real serialization and stream parser.
|
|
6
|
-
*/
|
|
7
|
-
|
|
8
|
-
import { randomUUID } from "node:crypto"
|
|
9
|
-
|
|
10
|
-
import { COMMAND_CODE_CLI_VERSION } from "./commandcode-catalog.ts"
|
|
11
|
-
import { commandCodeErrorMessage, redactCommandCodeErrorText } from "./overflow.ts"
|
|
12
|
-
import { modelSupportsImageInput } from "./models.ts"
|
|
13
|
-
import {
|
|
14
|
-
getApiKey,
|
|
15
|
-
getEnvironmentInfo,
|
|
16
|
-
isRecord,
|
|
17
|
-
assertTextOnlyMessages,
|
|
18
|
-
mapFinishReason,
|
|
19
|
-
messagesToCC,
|
|
20
|
-
numberValue,
|
|
21
|
-
parseStreamEventLine,
|
|
22
|
-
pickCommandCodeApiKey,
|
|
23
|
-
recordOrEmpty,
|
|
24
|
-
stringValue,
|
|
25
|
-
toolsToJson,
|
|
26
|
-
systemPromptToText,
|
|
27
|
-
} from "./converters.ts"
|
|
28
|
-
import type {
|
|
29
|
-
AssistantMessageEventStreamLike,
|
|
30
|
-
AssistantMessageLike,
|
|
31
|
-
ContextLike,
|
|
32
|
-
CoreDependencies,
|
|
33
|
-
ErrorReason,
|
|
34
|
-
ModelLike,
|
|
35
|
-
StopReason,
|
|
36
|
-
StreamOptions,
|
|
37
|
-
TerminalReason,
|
|
38
|
-
TextContent,
|
|
39
|
-
ToolCallContent,
|
|
40
|
-
Usage,
|
|
41
|
-
} from "./types.ts"
|
|
42
|
-
|
|
43
|
-
export * from "./converters.ts"
|
|
44
|
-
export * from "./overflow.ts"
|
|
45
|
-
export * from "./types.ts"
|
|
46
|
-
|
|
47
|
-
export const DEFAULT_API_BASE = "https://api.commandcode.ai"
|
|
48
|
-
export { COMMAND_CODE_CLI_VERSION }
|
|
49
|
-
|
|
50
|
-
const DEFAULT_GENERATE_MAX_TOKENS = 64_000
|
|
51
|
-
const DEFAULT_MAX_RETRIES = 0
|
|
52
|
-
const DEFAULT_MAX_RETRY_DELAY_MS = 60_000
|
|
53
|
-
const BASE_RETRY_DELAY_MS = 500
|
|
54
|
-
|
|
55
|
-
function isRetryableStatus(status: number): boolean {
|
|
56
|
-
return status === 429 || (status >= 500 && status < 600)
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
function parseRetryAfterSeconds(value: string | null): number | undefined {
|
|
60
|
-
if (!value) return undefined
|
|
61
|
-
const seconds = Number(value)
|
|
62
|
-
if (Number.isFinite(seconds) && seconds >= 0) return seconds
|
|
63
|
-
const date = Date.parse(value)
|
|
64
|
-
if (!Number.isNaN(date)) return Math.max(0, (date - Date.now()) / 1000)
|
|
65
|
-
return undefined
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
function effectiveMaxRetryDelayMs(value: number | undefined): number {
|
|
69
|
-
if (value === undefined) return DEFAULT_MAX_RETRY_DELAY_MS
|
|
70
|
-
if (value === 0) return Number.POSITIVE_INFINITY
|
|
71
|
-
return value
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
function retryDelayMs(
|
|
75
|
-
attempt: number,
|
|
76
|
-
retryAfterHeader: string | null,
|
|
77
|
-
maxDelayMs: number,
|
|
78
|
-
): number {
|
|
79
|
-
const retryAfterMs = parseRetryAfterSeconds(retryAfterHeader)
|
|
80
|
-
if (retryAfterMs !== undefined) {
|
|
81
|
-
if (retryAfterMs * 1000 > maxDelayMs) return -1
|
|
82
|
-
return retryAfterMs * 1000
|
|
83
|
-
}
|
|
84
|
-
const exponential = BASE_RETRY_DELAY_MS * 2 ** attempt
|
|
85
|
-
const jitter = exponential * 0.2 * Math.random()
|
|
86
|
-
return Math.min(exponential + jitter, maxDelayMs)
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
function defaultUsage(): Usage {
|
|
90
|
-
return {
|
|
91
|
-
input: 0,
|
|
92
|
-
output: 0,
|
|
93
|
-
cacheRead: 0,
|
|
94
|
-
cacheWrite: 0,
|
|
95
|
-
totalTokens: 0,
|
|
96
|
-
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
function commandCodeUsage(event: Record<string, unknown>): Record<string, unknown> | undefined {
|
|
101
|
-
return isRecord(event.totalUsage) ? event.totalUsage : undefined
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
function commandCodeInputTokenDetails(
|
|
105
|
-
usage: Record<string, unknown>,
|
|
106
|
-
): Record<string, unknown> | undefined {
|
|
107
|
-
return isRecord(usage.inputTokenDetails) ? usage.inputTokenDetails : undefined
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
function headersToRecord(headers: Headers): Record<string, string> {
|
|
111
|
-
const out: Record<string, string> = {}
|
|
112
|
-
headers.forEach((value, key) => {
|
|
113
|
-
out[key] = value
|
|
114
|
-
})
|
|
115
|
-
return out
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
function abortError(message = "The operation was aborted"): DOMException {
|
|
119
|
-
return new DOMException(message, "AbortError")
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
function timeoutError(timeoutMs: number | undefined): Error {
|
|
123
|
-
return new Error(
|
|
124
|
-
timeoutMs === undefined
|
|
125
|
-
? "Command Code API request timed out"
|
|
126
|
-
: `Command Code API request timed out after ${timeoutMs}ms`,
|
|
127
|
-
)
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
function successStopReason(reason: TerminalReason): StopReason {
|
|
131
|
-
if (reason === "length" || reason === "toolUse") return reason
|
|
132
|
-
return "stop"
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
function generateMaxTokens(model: ModelLike, options?: StreamOptions): number {
|
|
136
|
-
return Math.min(
|
|
137
|
-
options?.maxTokens ?? model.maxTokens,
|
|
138
|
-
model.maxTokens,
|
|
139
|
-
DEFAULT_GENERATE_MAX_TOKENS,
|
|
140
|
-
)
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
function mappedReasoningEffort(model: ModelLike, options?: StreamOptions): string | undefined {
|
|
144
|
-
const level = options?.reasoning
|
|
145
|
-
if (!level || level === "off" || !model.reasoning) return undefined
|
|
146
|
-
|
|
147
|
-
const effortMap = model.thinking?.effortMap ?? model.thinkingLevelMap
|
|
148
|
-
const mapped = effortMap?.[level]
|
|
149
|
-
return typeof mapped === "string" && mapped !== "off" ? mapped : undefined
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
/**
|
|
153
|
-
* Whether the generate request opts the account into Command Code's
|
|
154
|
-
* server-side taste learning. The official CLI sends its `tasteLearning`
|
|
155
|
-
* setting on every request (default on). This extension never reads the
|
|
156
|
-
* learned profile back, so learning is off unless the user enables it, and
|
|
157
|
-
* always off under the zero-data-retention flag.
|
|
158
|
-
*/
|
|
159
|
-
export function tasteLearningEnabled(env: NodeJS.ProcessEnv): boolean {
|
|
160
|
-
if (env.CMD_ZDR === "1" || env.COMMANDCODE_ZDR === "1") return false
|
|
161
|
-
return env.COMMANDCODE_TASTE_LEARNING === "1"
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
function isUuid(value: string): boolean {
|
|
165
|
-
return /^[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i.test(value)
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
export function projectSlugFromPath(pathName: string): string {
|
|
169
|
-
const slug = pathName
|
|
170
|
-
.toLowerCase()
|
|
171
|
-
.replace(/^[a-z]:/i, "")
|
|
172
|
-
.replace(/[^a-z0-9]+/g, "-")
|
|
173
|
-
.replace(/^-+|-+$/g, "")
|
|
174
|
-
return slug || "project"
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
export function createStreamCommandCode(deps: CoreDependencies) {
|
|
178
|
-
const apiBase = deps.apiBase ?? DEFAULT_API_BASE
|
|
179
|
-
const fetchImpl = deps.fetchImpl ?? fetch
|
|
180
|
-
const env = () => deps.env ?? process.env
|
|
181
|
-
const cwd = deps.cwd ?? (() => process.cwd())
|
|
182
|
-
const now = deps.now ?? (() => Date.now())
|
|
183
|
-
const uuid = deps.uuid ?? (() => randomUUID())
|
|
184
|
-
const delay =
|
|
185
|
-
deps.delay ??
|
|
186
|
-
((ms: number, signal: AbortSignal) => {
|
|
187
|
-
if (signal.aborted) return Promise.reject(abortError())
|
|
188
|
-
return new Promise<void>((resolve, reject) => {
|
|
189
|
-
const id = setTimeout(() => {
|
|
190
|
-
signal.removeEventListener("abort", onAbort)
|
|
191
|
-
resolve()
|
|
192
|
-
}, ms)
|
|
193
|
-
const onAbort = () => {
|
|
194
|
-
clearTimeout(id)
|
|
195
|
-
reject(abortError())
|
|
196
|
-
}
|
|
197
|
-
signal.addEventListener("abort", onAbort, { once: true })
|
|
198
|
-
})
|
|
199
|
-
})
|
|
200
|
-
|
|
201
|
-
function raceAbort<T>(promise: Promise<T>, signal: AbortSignal): Promise<T> {
|
|
202
|
-
if (signal.aborted) return Promise.reject(abortError())
|
|
203
|
-
|
|
204
|
-
return new Promise<T>((resolve, reject) => {
|
|
205
|
-
const onAbort = () => reject(abortError())
|
|
206
|
-
signal.addEventListener("abort", onAbort, { once: true })
|
|
207
|
-
promise.then(
|
|
208
|
-
(value) => {
|
|
209
|
-
signal.removeEventListener("abort", onAbort)
|
|
210
|
-
resolve(value)
|
|
211
|
-
},
|
|
212
|
-
(error: unknown) => {
|
|
213
|
-
signal.removeEventListener("abort", onAbort)
|
|
214
|
-
reject(error)
|
|
215
|
-
},
|
|
216
|
-
)
|
|
217
|
-
})
|
|
218
|
-
}
|
|
219
|
-
|
|
220
|
-
function raceAbortWithTimeout<T>(
|
|
221
|
-
promise: Promise<T>,
|
|
222
|
-
controller: AbortController,
|
|
223
|
-
timeoutMs: number | undefined,
|
|
224
|
-
): Promise<T> {
|
|
225
|
-
if (timeoutMs === undefined) return raceAbort(promise, controller.signal)
|
|
226
|
-
|
|
227
|
-
return new Promise<T>((resolve, reject) => {
|
|
228
|
-
const timer = setTimeout(() => {
|
|
229
|
-
controller.abort()
|
|
230
|
-
reject(timeoutError(timeoutMs))
|
|
231
|
-
}, timeoutMs)
|
|
232
|
-
raceAbort(promise, controller.signal).then(
|
|
233
|
-
(value) => {
|
|
234
|
-
clearTimeout(timer)
|
|
235
|
-
resolve(value)
|
|
236
|
-
},
|
|
237
|
-
(error: unknown) => {
|
|
238
|
-
clearTimeout(timer)
|
|
239
|
-
reject(error)
|
|
240
|
-
},
|
|
241
|
-
)
|
|
242
|
-
})
|
|
243
|
-
}
|
|
244
|
-
|
|
245
|
-
return function streamCommandCode(
|
|
246
|
-
model: ModelLike,
|
|
247
|
-
context: ContextLike,
|
|
248
|
-
options?: StreamOptions,
|
|
249
|
-
): AssistantMessageEventStreamLike {
|
|
250
|
-
const stream = deps.createStream()
|
|
251
|
-
|
|
252
|
-
async function run() {
|
|
253
|
-
// Some hosts pass a literal env-var reference instead of resolving it.
|
|
254
|
-
const apiKey = pickCommandCodeApiKey(
|
|
255
|
-
options?.apiKey,
|
|
256
|
-
getApiKey({
|
|
257
|
-
env: deps.env,
|
|
258
|
-
authPaths: deps.authPaths,
|
|
259
|
-
homeDir: deps.homeDir,
|
|
260
|
-
}),
|
|
261
|
-
)
|
|
262
|
-
|
|
263
|
-
if (!apiKey) {
|
|
264
|
-
const msg: AssistantMessageLike = {
|
|
265
|
-
role: "assistant",
|
|
266
|
-
content: [],
|
|
267
|
-
api: model.api,
|
|
268
|
-
provider: model.provider,
|
|
269
|
-
model: model.id,
|
|
270
|
-
usage: defaultUsage(),
|
|
271
|
-
stopReason: "error",
|
|
272
|
-
errorMessage:
|
|
273
|
-
"No Command Code API key. Run /login and select Command Code, set COMMAND_CODE_API_KEY (or legacy COMMANDCODE_API_KEY), or configure ~/.commandcode/auth.json, ~/.pi/agent/auth.json or ~/.omp/agent/auth.json",
|
|
274
|
-
timestamp: now(),
|
|
275
|
-
}
|
|
276
|
-
stream.push({ type: "error", reason: "error", error: msg })
|
|
277
|
-
stream.end()
|
|
278
|
-
return
|
|
279
|
-
}
|
|
280
|
-
|
|
281
|
-
const output: AssistantMessageLike = {
|
|
282
|
-
role: "assistant",
|
|
283
|
-
content: [],
|
|
284
|
-
api: model.api,
|
|
285
|
-
provider: model.provider,
|
|
286
|
-
model: model.id,
|
|
287
|
-
usage: defaultUsage(),
|
|
288
|
-
stopReason: "stop",
|
|
289
|
-
timestamp: now(),
|
|
290
|
-
}
|
|
291
|
-
|
|
292
|
-
const controller = new AbortController()
|
|
293
|
-
let reader: ReadableStreamDefaultReader<Uint8Array> | undefined
|
|
294
|
-
let textBlock: TextContent | undefined
|
|
295
|
-
let currentTextIdx = -1
|
|
296
|
-
let thinkingIdx = -1
|
|
297
|
-
const streamingToolCalls = new Map<
|
|
298
|
-
string,
|
|
299
|
-
{ contentIndex: number; toolCall: ToolCallContent; partialArgs: string }
|
|
300
|
-
>()
|
|
301
|
-
let finished = false
|
|
302
|
-
|
|
303
|
-
const abortUpstream = () => {
|
|
304
|
-
if (!controller.signal.aborted) controller.abort()
|
|
305
|
-
try {
|
|
306
|
-
reader?.cancel().catch(() => undefined)
|
|
307
|
-
} catch {
|
|
308
|
-
// Reader cancellation is best-effort.
|
|
309
|
-
}
|
|
310
|
-
}
|
|
311
|
-
|
|
312
|
-
if (options?.signal?.aborted) {
|
|
313
|
-
abortUpstream()
|
|
314
|
-
} else {
|
|
315
|
-
options?.signal?.addEventListener("abort", abortUpstream, {
|
|
316
|
-
once: true,
|
|
317
|
-
})
|
|
318
|
-
}
|
|
319
|
-
|
|
320
|
-
const endTextBlock = () => {
|
|
321
|
-
if (!textBlock) return
|
|
322
|
-
stream.push({
|
|
323
|
-
type: "text_end",
|
|
324
|
-
contentIndex: currentTextIdx,
|
|
325
|
-
content: textBlock.text,
|
|
326
|
-
partial: output,
|
|
327
|
-
})
|
|
328
|
-
textBlock = undefined
|
|
329
|
-
currentTextIdx = -1
|
|
330
|
-
}
|
|
331
|
-
|
|
332
|
-
const endThinking = () => {
|
|
333
|
-
if (thinkingIdx < 0) return
|
|
334
|
-
const tc = output.content[thinkingIdx]
|
|
335
|
-
if (tc && tc.type === "thinking") {
|
|
336
|
-
stream.push({
|
|
337
|
-
type: "thinking_end",
|
|
338
|
-
contentIndex: thinkingIdx,
|
|
339
|
-
content: (tc as { thinking: string }).thinking,
|
|
340
|
-
partial: output,
|
|
341
|
-
})
|
|
342
|
-
}
|
|
343
|
-
thinkingIdx = -1
|
|
344
|
-
}
|
|
345
|
-
|
|
346
|
-
const handleEvent = (event: unknown) => {
|
|
347
|
-
if (!isRecord(event)) return
|
|
348
|
-
|
|
349
|
-
switch (event.type) {
|
|
350
|
-
case "text-delta": {
|
|
351
|
-
endThinking()
|
|
352
|
-
if (!textBlock) {
|
|
353
|
-
textBlock = { type: "text", text: "" }
|
|
354
|
-
output.content.push(textBlock)
|
|
355
|
-
currentTextIdx = output.content.length - 1
|
|
356
|
-
stream.push({
|
|
357
|
-
type: "text_start",
|
|
358
|
-
contentIndex: currentTextIdx,
|
|
359
|
-
partial: output,
|
|
360
|
-
})
|
|
361
|
-
}
|
|
362
|
-
const delta = stringValue(event.text) ?? ""
|
|
363
|
-
textBlock.text += delta
|
|
364
|
-
stream.push({
|
|
365
|
-
type: "text_delta",
|
|
366
|
-
contentIndex: currentTextIdx,
|
|
367
|
-
delta,
|
|
368
|
-
partial: output,
|
|
369
|
-
})
|
|
370
|
-
break
|
|
371
|
-
}
|
|
372
|
-
|
|
373
|
-
case "reasoning-start": {
|
|
374
|
-
endTextBlock()
|
|
375
|
-
break
|
|
376
|
-
}
|
|
377
|
-
|
|
378
|
-
case "reasoning-delta": {
|
|
379
|
-
endTextBlock()
|
|
380
|
-
const delta = stringValue(event.text) ?? ""
|
|
381
|
-
if (thinkingIdx < 0) {
|
|
382
|
-
output.content.push({ type: "thinking", thinking: delta })
|
|
383
|
-
thinkingIdx = output.content.length - 1
|
|
384
|
-
stream.push({
|
|
385
|
-
type: "thinking_start",
|
|
386
|
-
contentIndex: thinkingIdx,
|
|
387
|
-
partial: output,
|
|
388
|
-
})
|
|
389
|
-
} else {
|
|
390
|
-
const tc = output.content[thinkingIdx]
|
|
391
|
-
if (tc && tc.type === "thinking") {
|
|
392
|
-
;(tc as { thinking: string }).thinking += delta
|
|
393
|
-
}
|
|
394
|
-
}
|
|
395
|
-
stream.push({
|
|
396
|
-
type: "thinking_delta",
|
|
397
|
-
contentIndex: thinkingIdx,
|
|
398
|
-
delta,
|
|
399
|
-
partial: output,
|
|
400
|
-
})
|
|
401
|
-
break
|
|
402
|
-
}
|
|
403
|
-
|
|
404
|
-
case "reasoning-end": {
|
|
405
|
-
endThinking()
|
|
406
|
-
break
|
|
407
|
-
}
|
|
408
|
-
|
|
409
|
-
case "tool-result": {
|
|
410
|
-
break
|
|
411
|
-
}
|
|
412
|
-
|
|
413
|
-
case "tool-input-start": {
|
|
414
|
-
endTextBlock()
|
|
415
|
-
endThinking()
|
|
416
|
-
const id = stringValue(event.id)
|
|
417
|
-
if (!id || streamingToolCalls.has(id)) break
|
|
418
|
-
|
|
419
|
-
const toolCall: ToolCallContent = {
|
|
420
|
-
type: "toolCall",
|
|
421
|
-
id,
|
|
422
|
-
name: stringValue(event.toolName) ?? "",
|
|
423
|
-
arguments: {},
|
|
424
|
-
}
|
|
425
|
-
output.content.push(toolCall)
|
|
426
|
-
const contentIndex = output.content.length - 1
|
|
427
|
-
streamingToolCalls.set(id, { contentIndex, toolCall, partialArgs: "" })
|
|
428
|
-
stream.push({
|
|
429
|
-
type: "toolcall_start",
|
|
430
|
-
contentIndex,
|
|
431
|
-
partial: output,
|
|
432
|
-
})
|
|
433
|
-
break
|
|
434
|
-
}
|
|
435
|
-
|
|
436
|
-
case "tool-input-delta": {
|
|
437
|
-
const id = stringValue(event.id)
|
|
438
|
-
const delta = stringValue(event.delta)
|
|
439
|
-
if (!id || delta === undefined) break
|
|
440
|
-
const active = streamingToolCalls.get(id)
|
|
441
|
-
if (!active) break
|
|
442
|
-
|
|
443
|
-
active.partialArgs += delta
|
|
444
|
-
active.toolCall.arguments = recordOrEmpty(active.partialArgs)
|
|
445
|
-
stream.push({
|
|
446
|
-
type: "toolcall_delta",
|
|
447
|
-
contentIndex: active.contentIndex,
|
|
448
|
-
delta,
|
|
449
|
-
partial: output,
|
|
450
|
-
})
|
|
451
|
-
break
|
|
452
|
-
}
|
|
453
|
-
|
|
454
|
-
case "tool-input-end": {
|
|
455
|
-
break
|
|
456
|
-
}
|
|
457
|
-
|
|
458
|
-
case "tool-call": {
|
|
459
|
-
endTextBlock()
|
|
460
|
-
endThinking()
|
|
461
|
-
const id = stringValue(event.toolCallId) ?? ""
|
|
462
|
-
const active = streamingToolCalls.get(id)
|
|
463
|
-
const toolCall: ToolCallContent = active?.toolCall ?? {
|
|
464
|
-
type: "toolCall",
|
|
465
|
-
id,
|
|
466
|
-
name: stringValue(event.toolName) ?? "",
|
|
467
|
-
arguments: {},
|
|
468
|
-
}
|
|
469
|
-
toolCall.name = stringValue(event.toolName) ?? toolCall.name
|
|
470
|
-
toolCall.arguments = recordOrEmpty(event.input ?? event.args ?? event.arguments)
|
|
471
|
-
|
|
472
|
-
let contentIndex: number
|
|
473
|
-
if (active) {
|
|
474
|
-
contentIndex = active.contentIndex
|
|
475
|
-
streamingToolCalls.delete(id)
|
|
476
|
-
} else {
|
|
477
|
-
output.content.push(toolCall)
|
|
478
|
-
contentIndex = output.content.length - 1
|
|
479
|
-
stream.push({
|
|
480
|
-
type: "toolcall_start",
|
|
481
|
-
contentIndex,
|
|
482
|
-
partial: output,
|
|
483
|
-
})
|
|
484
|
-
}
|
|
485
|
-
stream.push({
|
|
486
|
-
type: "toolcall_end",
|
|
487
|
-
contentIndex,
|
|
488
|
-
toolCall,
|
|
489
|
-
partial: output,
|
|
490
|
-
})
|
|
491
|
-
break
|
|
492
|
-
}
|
|
493
|
-
|
|
494
|
-
case "finish": {
|
|
495
|
-
const rawFinishReason = stringValue(event.rawFinishReason)
|
|
496
|
-
if (
|
|
497
|
-
rawFinishReason &&
|
|
498
|
-
/^(?:network|connection|upstream)[-_\s]?error$/i.test(rawFinishReason)
|
|
499
|
-
) {
|
|
500
|
-
throw new Error(
|
|
501
|
-
`Provider finished with reason "${rawFinishReason}" — upstream connection failed mid-stream`,
|
|
502
|
-
)
|
|
503
|
-
}
|
|
504
|
-
const usage = commandCodeUsage(event)
|
|
505
|
-
if (usage) {
|
|
506
|
-
const details = commandCodeInputTokenDetails(usage)
|
|
507
|
-
const totalInput = numberValue(usage.inputTokens) ?? 0
|
|
508
|
-
const input = numberValue(details?.noCacheTokens)
|
|
509
|
-
const cacheRead = numberValue(details?.cacheReadTokens) ?? 0
|
|
510
|
-
const cacheWrite = numberValue(details?.cacheWriteTokens) ?? 0
|
|
511
|
-
output.usage.input = input ?? Math.max(0, totalInput - cacheRead - cacheWrite)
|
|
512
|
-
output.usage.output = numberValue(usage.outputTokens) ?? 0
|
|
513
|
-
output.usage.cacheRead = cacheRead
|
|
514
|
-
output.usage.cacheWrite = cacheWrite
|
|
515
|
-
output.usage.totalTokens =
|
|
516
|
-
output.usage.input +
|
|
517
|
-
output.usage.output +
|
|
518
|
-
output.usage.cacheRead +
|
|
519
|
-
output.usage.cacheWrite
|
|
520
|
-
deps.calculateCost(model, output.usage, output.timestamp)
|
|
521
|
-
}
|
|
522
|
-
output.stopReason = mapFinishReason(event.finishReason)
|
|
523
|
-
finished = true
|
|
524
|
-
break
|
|
525
|
-
}
|
|
526
|
-
|
|
527
|
-
case "abort": {
|
|
528
|
-
throw abortError("Request aborted")
|
|
529
|
-
}
|
|
530
|
-
|
|
531
|
-
case "error": {
|
|
532
|
-
const message =
|
|
533
|
-
commandCodeErrorMessage(event.error) ??
|
|
534
|
-
commandCodeErrorMessage(event.message) ??
|
|
535
|
-
"Stream error"
|
|
536
|
-
output.stopReason = "error"
|
|
537
|
-
output.errorMessage = message
|
|
538
|
-
throw new Error(message)
|
|
539
|
-
}
|
|
540
|
-
}
|
|
541
|
-
}
|
|
542
|
-
|
|
543
|
-
try {
|
|
544
|
-
stream.push({ type: "start", partial: output })
|
|
545
|
-
if (controller.signal.aborted) throw abortError("Aborted")
|
|
546
|
-
|
|
547
|
-
const workingDir = cwd()
|
|
548
|
-
const threadId = options?.sessionId
|
|
549
|
-
? isUuid(options.sessionId)
|
|
550
|
-
? options.sessionId
|
|
551
|
-
: undefined
|
|
552
|
-
: uuid()
|
|
553
|
-
const reasoningEffort = mappedReasoningEffort(model, options)
|
|
554
|
-
const timeoutMs = options?.timeoutMs
|
|
555
|
-
|
|
556
|
-
const allowImages = modelSupportsImageInput(model.id, model.input)
|
|
557
|
-
if (!allowImages) assertTextOnlyMessages(context.messages)
|
|
558
|
-
|
|
559
|
-
let body: unknown = {
|
|
560
|
-
config: {
|
|
561
|
-
workingDir,
|
|
562
|
-
date: new Date(now()).toISOString().split("T")[0],
|
|
563
|
-
environment: getEnvironmentInfo(),
|
|
564
|
-
structure: [],
|
|
565
|
-
isGitRepo: false,
|
|
566
|
-
currentBranch: "",
|
|
567
|
-
mainBranch: "",
|
|
568
|
-
gitStatus: "",
|
|
569
|
-
recentCommits: [],
|
|
570
|
-
},
|
|
571
|
-
memory: null,
|
|
572
|
-
taste: null,
|
|
573
|
-
skills: null,
|
|
574
|
-
params: {
|
|
575
|
-
model: model.id,
|
|
576
|
-
messages: messagesToCC(context.messages, { allowImages }),
|
|
577
|
-
tools: toolsToJson(context.tools, model.id),
|
|
578
|
-
system: systemPromptToText(context.systemPrompt),
|
|
579
|
-
max_tokens: generateMaxTokens(model, options),
|
|
580
|
-
stream: true,
|
|
581
|
-
...(options?.temperature !== undefined ? { temperature: options.temperature } : {}),
|
|
582
|
-
...(reasoningEffort ? { reasoning_effort: reasoningEffort } : {}),
|
|
583
|
-
},
|
|
584
|
-
threadId,
|
|
585
|
-
}
|
|
586
|
-
|
|
587
|
-
const payloadController = new AbortController()
|
|
588
|
-
const onPayloadAbort = () => payloadController.abort()
|
|
589
|
-
controller.signal.addEventListener("abort", onPayloadAbort, { once: true })
|
|
590
|
-
let nextBody: unknown
|
|
591
|
-
try {
|
|
592
|
-
nextBody = await raceAbortWithTimeout(
|
|
593
|
-
Promise.resolve(options?.onPayload?.(body, model)),
|
|
594
|
-
payloadController,
|
|
595
|
-
timeoutMs,
|
|
596
|
-
)
|
|
597
|
-
} finally {
|
|
598
|
-
controller.signal.removeEventListener("abort", onPayloadAbort)
|
|
599
|
-
}
|
|
600
|
-
if (nextBody !== undefined) body = nextBody
|
|
601
|
-
|
|
602
|
-
const maxRetries = options?.maxRetries ?? DEFAULT_MAX_RETRIES
|
|
603
|
-
const maxRetryDelayMs = effectiveMaxRetryDelayMs(options?.maxRetryDelayMs)
|
|
604
|
-
// The privacy policy is authoritative even for differently cased caller headers.
|
|
605
|
-
const callerHeaders = Object.fromEntries(
|
|
606
|
-
Object.entries(options?.headers ?? {}).filter(
|
|
607
|
-
([name]) => name.toLowerCase() !== "x-taste-learning",
|
|
608
|
-
),
|
|
609
|
-
)
|
|
610
|
-
const requestHeaders = {
|
|
611
|
-
"Content-Type": "application/json",
|
|
612
|
-
Authorization: `Bearer ${apiKey}`,
|
|
613
|
-
"x-command-code-version": COMMAND_CODE_CLI_VERSION,
|
|
614
|
-
"x-cli-environment": "production",
|
|
615
|
-
"x-project-slug": projectSlugFromPath(workingDir),
|
|
616
|
-
"x-taste-learning": String(tasteLearningEnabled(env())),
|
|
617
|
-
...(options?.sessionId ? { "x-session-id": options.sessionId } : {}),
|
|
618
|
-
"User-Agent": "cli",
|
|
619
|
-
...callerHeaders,
|
|
620
|
-
}
|
|
621
|
-
const bodyStr = JSON.stringify(body)
|
|
622
|
-
|
|
623
|
-
let response!: Response
|
|
624
|
-
retryLoop: for (let attempt = 0; ; attempt++) {
|
|
625
|
-
const attemptController = new AbortController()
|
|
626
|
-
let attemptTimedOut = false
|
|
627
|
-
let attemptTimeoutId: ReturnType<typeof setTimeout> | undefined
|
|
628
|
-
|
|
629
|
-
const clearAttemptTimeout = () => {
|
|
630
|
-
if (attemptTimeoutId !== undefined) {
|
|
631
|
-
clearTimeout(attemptTimeoutId)
|
|
632
|
-
attemptTimeoutId = undefined
|
|
633
|
-
}
|
|
634
|
-
}
|
|
635
|
-
|
|
636
|
-
if (timeoutMs !== undefined) {
|
|
637
|
-
attemptTimeoutId = setTimeout(() => {
|
|
638
|
-
attemptTimedOut = true
|
|
639
|
-
attemptController.abort()
|
|
640
|
-
}, timeoutMs)
|
|
641
|
-
}
|
|
642
|
-
const onOuterAbort = () => attemptController.abort()
|
|
643
|
-
controller.signal.addEventListener("abort", onOuterAbort, { once: true })
|
|
644
|
-
const raceAttempt = <T>(promise: Promise<T>): Promise<T> =>
|
|
645
|
-
raceAbort(promise, attemptController.signal).catch((error: unknown) => {
|
|
646
|
-
if (attemptTimedOut) throw timeoutError(timeoutMs)
|
|
647
|
-
throw error
|
|
648
|
-
})
|
|
649
|
-
|
|
650
|
-
try {
|
|
651
|
-
try {
|
|
652
|
-
response = await fetchImpl(`${apiBase}/alpha/generate`, {
|
|
653
|
-
method: "POST",
|
|
654
|
-
headers: requestHeaders,
|
|
655
|
-
body: bodyStr,
|
|
656
|
-
signal: attemptController.signal,
|
|
657
|
-
})
|
|
658
|
-
} catch (fetchError: unknown) {
|
|
659
|
-
if (controller.signal.aborted) throw abortError("Aborted")
|
|
660
|
-
if (attemptTimedOut) {
|
|
661
|
-
if (attempt < maxRetries) continue retryLoop
|
|
662
|
-
throw timeoutError(timeoutMs)
|
|
663
|
-
}
|
|
664
|
-
throw fetchError
|
|
665
|
-
}
|
|
666
|
-
|
|
667
|
-
// --- HTTP-level retry ---
|
|
668
|
-
if (!response.ok && isRetryableStatus(response.status)) {
|
|
669
|
-
const retryAfter = response.headers.get("retry-after")
|
|
670
|
-
const waitMs = retryDelayMs(attempt, retryAfter, maxRetryDelayMs)
|
|
671
|
-
if (waitMs < 0) {
|
|
672
|
-
const requestedSeconds = parseRetryAfterSeconds(retryAfter) ?? 0
|
|
673
|
-
const capLabel =
|
|
674
|
-
maxRetryDelayMs === Number.POSITIVE_INFINITY ? "disabled" : `${maxRetryDelayMs}ms`
|
|
675
|
-
throw new Error(`Retry-After delay ${requestedSeconds}s exceeds max ${capLabel}`)
|
|
676
|
-
}
|
|
677
|
-
if (attempt < maxRetries) {
|
|
678
|
-
await response.text().catch(() => "")
|
|
679
|
-
if (waitMs > 0) await delay(waitMs, controller.signal)
|
|
680
|
-
continue retryLoop
|
|
681
|
-
}
|
|
682
|
-
}
|
|
683
|
-
|
|
684
|
-
try {
|
|
685
|
-
await raceAttempt(
|
|
686
|
-
Promise.resolve(
|
|
687
|
-
options?.onResponse?.(
|
|
688
|
-
{
|
|
689
|
-
status: response.status,
|
|
690
|
-
headers: headersToRecord(response.headers),
|
|
691
|
-
},
|
|
692
|
-
model,
|
|
693
|
-
),
|
|
694
|
-
),
|
|
695
|
-
)
|
|
696
|
-
} catch (error: unknown) {
|
|
697
|
-
if (attemptTimedOut && attempt < maxRetries) continue retryLoop
|
|
698
|
-
throw error
|
|
699
|
-
}
|
|
700
|
-
|
|
701
|
-
if (!response.ok) {
|
|
702
|
-
const errBody = await raceAttempt(response.text().catch(() => ""))
|
|
703
|
-
let errorDetail: string | undefined
|
|
704
|
-
try {
|
|
705
|
-
const parsedBody: unknown = JSON.parse(errBody)
|
|
706
|
-
errorDetail = commandCodeErrorMessage(parsedBody)
|
|
707
|
-
} catch {
|
|
708
|
-
// Preserve useful plain-text provider errors only after secret
|
|
709
|
-
// redaction; upstream/proxy bodies may echo credentials.
|
|
710
|
-
}
|
|
711
|
-
const safeBody = redactCommandCodeErrorText(errBody).slice(0, 500)
|
|
712
|
-
const detail = redactCommandCodeErrorText(
|
|
713
|
-
errorDetail ?? (safeBody || "Provider returned an error"),
|
|
714
|
-
)
|
|
715
|
-
throw new Error(`Command Code API error ${response.status}: ${detail}`)
|
|
716
|
-
}
|
|
717
|
-
|
|
718
|
-
// --- Read response stream ---
|
|
719
|
-
reader = response.body?.getReader()
|
|
720
|
-
if (!reader) throw new Error("No response body")
|
|
721
|
-
|
|
722
|
-
const decoder = new TextDecoder()
|
|
723
|
-
let buffer = ""
|
|
724
|
-
|
|
725
|
-
try {
|
|
726
|
-
readLoop: for (;;) {
|
|
727
|
-
if (controller.signal.aborted) throw abortError("Aborted")
|
|
728
|
-
const { done, value } = await raceAbort(reader.read(), attemptController.signal)
|
|
729
|
-
if (done) {
|
|
730
|
-
clearAttemptTimeout()
|
|
731
|
-
if (buffer.trim()) handleEvent(parseStreamEventLine(buffer))
|
|
732
|
-
if (!finished) {
|
|
733
|
-
throw new Error(
|
|
734
|
-
"Stream ended unexpectedly before completion (no finish event) — response was truncated",
|
|
735
|
-
)
|
|
736
|
-
}
|
|
737
|
-
break
|
|
738
|
-
}
|
|
739
|
-
if (timeoutMs !== undefined) {
|
|
740
|
-
clearAttemptTimeout()
|
|
741
|
-
attemptTimeoutId = setTimeout(() => {
|
|
742
|
-
attemptTimedOut = true
|
|
743
|
-
attemptController.abort()
|
|
744
|
-
}, timeoutMs)
|
|
745
|
-
}
|
|
746
|
-
if (controller.signal.aborted) throw abortError("Aborted")
|
|
747
|
-
|
|
748
|
-
buffer += decoder.decode(value, { stream: true })
|
|
749
|
-
const lines = buffer.split("\n")
|
|
750
|
-
buffer = lines.pop() ?? ""
|
|
751
|
-
|
|
752
|
-
for (const line of lines) {
|
|
753
|
-
if (controller.signal.aborted) throw abortError("Aborted")
|
|
754
|
-
handleEvent(parseStreamEventLine(line))
|
|
755
|
-
if (finished) break readLoop
|
|
756
|
-
}
|
|
757
|
-
}
|
|
758
|
-
} catch (streamError: unknown) {
|
|
759
|
-
// Stream-level error (e.g. API returned 200 OK but sent an error event)
|
|
760
|
-
// or per-attempt timeout during stream reading.
|
|
761
|
-
await reader.cancel().catch(() => {})
|
|
762
|
-
try {
|
|
763
|
-
reader.releaseLock()
|
|
764
|
-
} catch {}
|
|
765
|
-
reader = undefined
|
|
766
|
-
|
|
767
|
-
// The per-attempt timer aborts `attemptController`, so a stalled
|
|
768
|
-
// body surfaces as an AbortError here. Classify it as a timeout
|
|
769
|
-
// before the user-abort check, or it would be reported as a
|
|
770
|
-
// cancellation and never retried.
|
|
771
|
-
if (controller.signal.aborted) throw streamError
|
|
772
|
-
if (
|
|
773
|
-
!attemptTimedOut &&
|
|
774
|
-
streamError instanceof Error &&
|
|
775
|
-
streamError.name === "AbortError"
|
|
776
|
-
) {
|
|
777
|
-
throw streamError
|
|
778
|
-
}
|
|
779
|
-
|
|
780
|
-
// Never retry after visible content was emitted (including timeout mid-stream).
|
|
781
|
-
const canRetry = output.content.length === 0 && attempt < maxRetries
|
|
782
|
-
if (canRetry) {
|
|
783
|
-
output.content.length = 0
|
|
784
|
-
textBlock = undefined
|
|
785
|
-
currentTextIdx = -1
|
|
786
|
-
thinkingIdx = -1
|
|
787
|
-
output.stopReason = "stop"
|
|
788
|
-
output.errorMessage = undefined
|
|
789
|
-
finished = false
|
|
790
|
-
const waitMs = attemptTimedOut ? 0 : retryDelayMs(attempt, null, maxRetryDelayMs)
|
|
791
|
-
if (waitMs > 0) await delay(waitMs, controller.signal)
|
|
792
|
-
continue retryLoop
|
|
793
|
-
}
|
|
794
|
-
if (attemptTimedOut) throw timeoutError(timeoutMs)
|
|
795
|
-
throw streamError
|
|
796
|
-
}
|
|
797
|
-
|
|
798
|
-
// Stream completed successfully.
|
|
799
|
-
endTextBlock()
|
|
800
|
-
endThinking()
|
|
801
|
-
|
|
802
|
-
stream.push({
|
|
803
|
-
type: "done",
|
|
804
|
-
reason: successStopReason(output.stopReason),
|
|
805
|
-
message: output,
|
|
806
|
-
})
|
|
807
|
-
stream.end()
|
|
808
|
-
break retryLoop
|
|
809
|
-
} finally {
|
|
810
|
-
controller.signal.removeEventListener("abort", onOuterAbort)
|
|
811
|
-
clearAttemptTimeout()
|
|
812
|
-
}
|
|
813
|
-
}
|
|
814
|
-
} catch (error: unknown) {
|
|
815
|
-
const reason: ErrorReason =
|
|
816
|
-
controller.signal.aborted || (error instanceof Error && error.name === "AbortError")
|
|
817
|
-
? "aborted"
|
|
818
|
-
: "error"
|
|
819
|
-
output.stopReason = reason
|
|
820
|
-
output.errorMessage =
|
|
821
|
-
reason === "aborted"
|
|
822
|
-
? "Request aborted"
|
|
823
|
-
: redactCommandCodeErrorText(error instanceof Error ? error.message : String(error))
|
|
824
|
-
stream.push({ type: "error", reason, error: output })
|
|
825
|
-
stream.end()
|
|
826
|
-
} finally {
|
|
827
|
-
options?.signal?.removeEventListener("abort", abortUpstream)
|
|
828
|
-
try {
|
|
829
|
-
await reader?.cancel()
|
|
830
|
-
} catch {
|
|
831
|
-
// Reader may already be closed/cancelled.
|
|
832
|
-
}
|
|
833
|
-
try {
|
|
834
|
-
reader?.releaseLock()
|
|
835
|
-
} catch {
|
|
836
|
-
// Reader may already be released/cancelled by the abort path.
|
|
837
|
-
}
|
|
838
|
-
}
|
|
839
|
-
}
|
|
840
|
-
|
|
841
|
-
run().catch((error: unknown) => {
|
|
842
|
-
const msg: AssistantMessageLike = {
|
|
843
|
-
role: "assistant",
|
|
844
|
-
content: [],
|
|
845
|
-
api: model.api,
|
|
846
|
-
provider: model.provider,
|
|
847
|
-
model: model.id,
|
|
848
|
-
usage: defaultUsage(),
|
|
849
|
-
stopReason: "error",
|
|
850
|
-
errorMessage: redactCommandCodeErrorText(
|
|
851
|
-
error instanceof Error ? error.message : String(error),
|
|
852
|
-
),
|
|
853
|
-
timestamp: now(),
|
|
854
|
-
}
|
|
855
|
-
stream.push({ type: "error", reason: "error", error: msg })
|
|
856
|
-
stream.end()
|
|
857
|
-
})
|
|
858
|
-
|
|
859
|
-
return stream
|
|
860
|
-
}
|
|
861
|
-
}
|