@humain/humain-code 0.4.4 → 0.4.6
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 +41 -0
- package/dist/bundle/agent-monitor.js +1 -1
- package/dist/bundle/chunks/anthropic-messages-OHNAMPAM.js +69 -0
- package/dist/bundle/chunks/{azure-openai-responses-OJKB2O3D.js → azure-openai-responses-FREWTLSS.js} +6 -5
- package/dist/bundle/chunks/bedrock-converse-stream.js +61 -55
- package/dist/bundle/chunks/{chunk-VZV7GLZT.js → chunk-2NX3BVH6.js} +6 -5
- package/dist/bundle/chunks/{chunk-JCLZBP27.js → chunk-BBBBZ5HY.js} +2 -2
- package/dist/bundle/chunks/{chunk-T2IF7HO3.js → chunk-FP2SXW5M.js} +129 -121
- package/dist/bundle/chunks/{chunk-O2WQYQO4.js → chunk-IMR3ZJM5.js} +1 -1
- package/dist/bundle/chunks/{chunk-PEZZKT73.js → chunk-KLIRQQH5.js} +1 -1
- package/dist/bundle/chunks/{chunk-2KKLZK2N.js → chunk-LFXFQLII.js} +1 -1
- package/dist/bundle/chunks/{chunk-ERT2BD7V.js → chunk-PBBQBIHY.js} +1 -1
- package/dist/bundle/chunks/{chunk-HDP4USTE.js → chunk-RXHSJSYY.js} +1 -1
- package/dist/bundle/chunks/{chunk-524BLM37.js → chunk-SKIP6KJV.js} +1 -1
- package/dist/bundle/chunks/{chunk-Q5TQLAOM.js → chunk-UBQSONVR.js} +1 -1
- package/dist/bundle/chunks/{chunk-PI3NYWSK.js → chunk-UTMYL2YO.js} +8 -6
- package/dist/bundle/chunks/{chunk-VS3OEOQ4.js → chunk-UU4WS4BQ.js} +57 -57
- package/dist/bundle/chunks/{chunk-T45HXZW7.js → chunk-VQTXF23B.js} +8 -8
- package/dist/bundle/chunks/{chunk-RTFCP3VQ.js → chunk-WPLW452J.js} +1 -1
- package/dist/bundle/chunks/chunk-YTVFIA3A.js +2 -0
- package/dist/bundle/chunks/{cloudflare-workers-ai-system-one-3QIT2RE2.js → cloudflare-workers-ai-system-one-YPOLSCPT.js} +1 -1
- package/dist/bundle/chunks/{compat-U2AA66JI.js → compat-LJ4ZIVJZ.js} +3 -3
- package/dist/bundle/chunks/{dist-SIXP5HVD.js → dist-QHFJK4TH.js} +2 -2
- package/dist/bundle/chunks/{dist-NKHGTDJG.js → dist-UAQRGPZO.js} +520 -464
- package/dist/bundle/chunks/{event-streams-PKTMLNE5.js → event-streams-CGCQQL5M.js} +1 -1
- package/dist/bundle/chunks/{execute-KEUC5IWA.js → execute-MFIDTEMB.js} +1 -1
- package/dist/bundle/chunks/{extract-WUAT5T4V.js → extract-EWN2GRAC.js} +2 -2
- package/dist/bundle/chunks/google-generative-ai-TS4ZE2RO.js +21 -0
- package/dist/bundle/chunks/google-vertex-MJK2SMNE.js +25 -0
- package/dist/bundle/chunks/{host-SPBJ4YAA.js → host-3OGMFGBY.js} +1 -1
- package/dist/bundle/chunks/mistral-conversations-5HCMHK6E.js +43 -0
- package/dist/bundle/chunks/{openai-codex-responses-B7CTZNPK.js → openai-codex-responses-5AJAJHTK.js} +5 -5
- package/dist/bundle/chunks/{openai-completions-2FYSVT4M.js → openai-completions-65RRYKEQ.js} +35 -34
- package/dist/bundle/chunks/{openai-decisions-BLNICGXO.js → openai-decisions-MKH4OJHH.js} +1 -1
- package/dist/bundle/chunks/{openai-responses-APOZMFDL.js → openai-responses-72W3PGZ7.js} +4 -4
- package/dist/bundle/chunks/{pi-messages-WNFZL3B7.js → pi-messages-RLLYRBDT.js} +8 -8
- package/dist/bundle/chunks/{typesafe-system-one-L7FINSCN.js → typesafe-system-one-PAYJECQK.js} +1 -1
- package/dist/bundle/chunks/{virtual-modules-6XA5HC3B.js → virtual-modules-LWYOTTHQ.js} +2 -2
- package/dist/bundle/cli-runtime.js +2 -2
- package/dist/bundle/flow/assets/profiles/anthropic.json +3 -3
- package/dist/bundle/flow/assets/profiles/openai.json +3 -3
- package/dist/bundle/flow/assets/profiles/oss-first.json +3 -3
- package/dist/bundle/flow/assets/profiles/premium.json +92 -8
- package/dist/bundle/flow/package.json +1 -1
- package/dist/bundle/index.js +4 -4
- package/dist/bundle/rpc-entry.js +2 -2
- package/dist/core/agent-session.d.ts +2 -0
- package/dist/core/agent-session.d.ts.map +1 -1
- package/dist/core/agent-session.js +1 -0
- package/dist/core/agent-session.js.map +1 -1
- package/dist/core/tools/subagent.d.ts.map +1 -1
- package/dist/core/tools/subagent.js +2 -0
- package/dist/core/tools/subagent.js.map +1 -1
- package/dist/core/usage-totals.d.ts +2 -0
- package/dist/core/usage-totals.d.ts.map +1 -1
- package/dist/core/usage-totals.js +5 -1
- package/dist/core/usage-totals.js.map +1 -1
- package/dist/humain/bedrock-provider/vendor/index.bundle.mjs +18 -0
- package/dist/humain/node-http.d.ts.map +1 -1
- package/dist/humain/node-http.js +29 -0
- package/dist/humain/node-http.js.map +1 -1
- package/dist/humain/trace-capture/index.d.ts.map +1 -1
- package/dist/humain/trace-capture/index.js +15 -12
- package/dist/humain/trace-capture/index.js.map +1 -1
- package/docs/flow-profiles.md +2 -0
- package/npm-shrinkwrap.json +6 -6
- package/package.json +2 -2
- package/dist/bundle/chunks/anthropic-messages-RKOVHQPW.js +0 -67
- package/dist/bundle/chunks/google-generative-ai-MDUVUFFQ.js +0 -20
- package/dist/bundle/chunks/google-vertex-UQGG6LEW.js +0 -24
- package/dist/bundle/chunks/mistral-conversations-56EA73FO.js +0 -42
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import type { Usage } from "@earendil-works/pi-ai/compat";
|
|
2
2
|
import type { SessionEntry } from "./session-manager.ts";
|
|
3
3
|
export interface UsageTotals {
|
|
4
|
+
usageIncomplete?: boolean;
|
|
4
5
|
input: number;
|
|
5
6
|
output: number;
|
|
6
7
|
cacheRead: number;
|
|
@@ -12,6 +13,7 @@ export declare function addUsageToTotals(totals: UsageTotals, usage: Usage): voi
|
|
|
12
13
|
/** Sum of two usages, keeping the optional token splits when either side reports them. */
|
|
13
14
|
export declare function combineUsage(first: Usage, second: Usage): Usage;
|
|
14
15
|
export interface UsageCostBreakdownEntry {
|
|
16
|
+
usageIncomplete?: boolean;
|
|
15
17
|
key: string;
|
|
16
18
|
cost: number;
|
|
17
19
|
tokens: number;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"usage-totals.d.ts","sourceRoot":"","sources":["../../src/core/usage-totals.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,8BAA8B,CAAC;AAC1D,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,sBAAsB,CAAC;AAEzD,MAAM,WAAW,WAAW;IAC3B,KAAK,EAAE,MAAM,CAAC;IACd,MAAM,EAAE,MAAM,CAAC;IACf,SAAS,EAAE,MAAM,CAAC;IAClB,UAAU,EAAE,MAAM,CAAC;IACnB,IAAI,EAAE,MAAM,CAAC;CACb;AAED,wBAAgB,iBAAiB,IAAI,WAAW,CAQ/C;AAED,wBAAgB,gBAAgB,CAAC,MAAM,EAAE,WAAW,EAAE,KAAK,EAAE,KAAK,GAAG,IAAI,
|
|
1
|
+
{"version":3,"file":"usage-totals.d.ts","sourceRoot":"","sources":["../../src/core/usage-totals.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,8BAA8B,CAAC;AAC1D,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,sBAAsB,CAAC;AAEzD,MAAM,WAAW,WAAW;IAC3B,eAAe,CAAC,EAAE,OAAO,CAAC;IAC1B,KAAK,EAAE,MAAM,CAAC;IACd,MAAM,EAAE,MAAM,CAAC;IACf,SAAS,EAAE,MAAM,CAAC;IAClB,UAAU,EAAE,MAAM,CAAC;IACnB,IAAI,EAAE,MAAM,CAAC;CACb;AAED,wBAAgB,iBAAiB,IAAI,WAAW,CAQ/C;AAED,wBAAgB,gBAAgB,CAAC,MAAM,EAAE,WAAW,EAAE,KAAK,EAAE,KAAK,GAAG,IAAI,CAOxE;AAED,0FAA0F;AAC1F,wBAAgB,YAAY,CAAC,KAAK,EAAE,KAAK,EAAE,MAAM,EAAE,KAAK,GAAG,KAAK,CAsB/D;AAED,MAAM,WAAW,uBAAuB;IACvC,eAAe,CAAC,EAAE,OAAO,CAAC;IAC1B,GAAG,EAAE,MAAM,CAAC;IACZ,IAAI,EAAE,MAAM,CAAC;IACb,MAAM,EAAE,MAAM,CAAC;CACf;AAED,wFAAwF;AACxF,wBAAgB,qBAAqB,CAAC,OAAO,EAAE,YAAY,EAAE,GAAG,uBAAuB,EAAE,CAqCxF"}
|
|
@@ -8,6 +8,8 @@ export function createUsageTotals() {
|
|
|
8
8
|
};
|
|
9
9
|
}
|
|
10
10
|
export function addUsageToTotals(totals, usage) {
|
|
11
|
+
if (usage.usageIncomplete)
|
|
12
|
+
totals.usageIncomplete = true;
|
|
11
13
|
totals.input += usage.input;
|
|
12
14
|
totals.output += usage.output;
|
|
13
15
|
totals.cacheRead += usage.cacheRead;
|
|
@@ -17,6 +19,7 @@ export function addUsageToTotals(totals, usage) {
|
|
|
17
19
|
/** Sum of two usages, keeping the optional token splits when either side reports them. */
|
|
18
20
|
export function combineUsage(first, second) {
|
|
19
21
|
return {
|
|
22
|
+
...(first.usageIncomplete || second.usageIncomplete ? { usageIncomplete: true } : {}),
|
|
20
23
|
input: first.input + second.input,
|
|
21
24
|
output: first.output + second.output,
|
|
22
25
|
cacheRead: first.cacheRead + second.cacheRead,
|
|
@@ -70,10 +73,11 @@ export function getUsageCostBreakdown(entries) {
|
|
|
70
73
|
}
|
|
71
74
|
return Array.from(totalsByKey, ([key, totals]) => ({
|
|
72
75
|
key,
|
|
76
|
+
...(totals.usageIncomplete ? { usageIncomplete: true } : {}),
|
|
73
77
|
cost: totals.cost,
|
|
74
78
|
tokens: totals.input + totals.output + totals.cacheRead + totals.cacheWrite,
|
|
75
79
|
}))
|
|
76
|
-
.filter((entry) => entry.cost > 0 || entry.tokens > 0)
|
|
80
|
+
.filter((entry) => entry.usageIncomplete || entry.cost > 0 || entry.tokens > 0)
|
|
77
81
|
.sort((a, b) => b.cost - a.cost);
|
|
78
82
|
}
|
|
79
83
|
//# sourceMappingURL=usage-totals.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"usage-totals.js","sourceRoot":"","sources":["../../src/core/usage-totals.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"usage-totals.js","sourceRoot":"","sources":["../../src/core/usage-totals.ts"],"names":[],"mappings":"AAYA,MAAM,UAAU,iBAAiB;IAChC,OAAO;QACN,KAAK,EAAE,CAAC;QACR,MAAM,EAAE,CAAC;QACT,SAAS,EAAE,CAAC;QACZ,UAAU,EAAE,CAAC;QACb,IAAI,EAAE,CAAC;KACP,CAAC;AACH,CAAC;AAED,MAAM,UAAU,gBAAgB,CAAC,MAAmB,EAAE,KAAY;IACjE,IAAI,KAAK,CAAC,eAAe;QAAE,MAAM,CAAC,eAAe,GAAG,IAAI,CAAC;IACzD,MAAM,CAAC,KAAK,IAAI,KAAK,CAAC,KAAK,CAAC;IAC5B,MAAM,CAAC,MAAM,IAAI,KAAK,CAAC,MAAM,CAAC;IAC9B,MAAM,CAAC,SAAS,IAAI,KAAK,CAAC,SAAS,CAAC;IACpC,MAAM,CAAC,UAAU,IAAI,KAAK,CAAC,UAAU,CAAC;IACtC,MAAM,CAAC,IAAI,IAAI,KAAK,CAAC,IAAI,CAAC,KAAK,CAAC;AACjC,CAAC;AAED,0FAA0F;AAC1F,MAAM,UAAU,YAAY,CAAC,KAAY,EAAE,MAAa;IACvD,OAAO;QACN,GAAG,CAAC,KAAK,CAAC,eAAe,IAAI,MAAM,CAAC,eAAe,CAAC,CAAC,CAAC,EAAE,eAAe,EAAE,IAAI,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;QACrF,KAAK,EAAE,KAAK,CAAC,KAAK,GAAG,MAAM,CAAC,KAAK;QACjC,MAAM,EAAE,KAAK,CAAC,MAAM,GAAG,MAAM,CAAC,MA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ACb,MAAM,GAAG,iBAAiB,EAAE,CAAC;YAC7B,WAAW,CAAC,GAAG,CAAC,GAAG,EAAE,MAAM,CAAC,CAAC;QAC9B,CAAC;QACD,gBAAgB,CAAC,MAAM,EAAE,KAAK,CAAC,CAAC;IACjC,CAAC;IAED,OAAO,KAAK,CAAC,IAAI,CAAC,WAAW,EAAE,CAAC,CAAC,GAAG,EAAE,MAAM,CAAC,EAAE,EAAE,CAAC,CAAC;QAClD,GAAG;QACH,GAAG,CAAC,MAAM,CAAC,eAAe,CAAC,CAAC,CAAC,EAAE,eAAe,EAAE,IAAI,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;QAC5D,IAAI,EAAE,MAAM,CAAC,IAAI;QACjB,MAAM,EAAE,MAAM,CAAC,KAAK,GAAG,MAAM,CAAC,MAAM,GAAG,MAAM,CAAC,SAAS,GAAG,MAAM,CAAC,UAAU;KAC3E,CAAC,CAAC;SACD,MAAM,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,KAAK,CAAC,eAAe,IAAI,KAAK,CAAC,IAAI,GAAG,CAAC,IAAI,KAAK,CAAC,MAAM,GAAG,CAAC,CAAC;SAC9E,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,IAAI,GAAG,CAAC,CAAC,IAAI,CAAC,CAAC;AACnC,CAAC","sourcesContent":["import type { Usage } from \"@earendil-works/pi-ai/compat\";\nimport type { SessionEntry } from \"./session-manager.ts\";\n\nexport interface UsageTotals {\n\tusageIncomplete?: boolean;\n\tinput: number;\n\toutput: number;\n\tcacheRead: number;\n\tcacheWrite: number;\n\tcost: number;\n}\n\nexport function createUsageTotals(): UsageTotals {\n\treturn {\n\t\tinput: 0,\n\t\toutput: 0,\n\t\tcacheRead: 0,\n\t\tcacheWrite: 0,\n\t\tcost: 0,\n\t};\n}\n\nexport function addUsageToTotals(totals: UsageTotals, usage: Usage): void {\n\tif (usage.usageIncomplete) totals.usageIncomplete = true;\n\ttotals.input += usage.input;\n\ttotals.output += usage.output;\n\ttotals.cacheRead += usage.cacheRead;\n\ttotals.cacheWrite += usage.cacheWrite;\n\ttotals.cost += usage.cost.total;\n}\n\n/** Sum of two usages, keeping the optional token splits when either side reports them. */\nexport function combineUsage(first: Usage, second: Usage): Usage {\n\treturn {\n\t\t...(first.usageIncomplete || second.usageIncomplete ? { usageIncomplete: true } : {}),\n\t\tinput: first.input + second.input,\n\t\toutput: first.output + second.output,\n\t\tcacheRead: first.cacheRead + second.cacheRead,\n\t\tcacheWrite: first.cacheWrite + second.cacheWrite,\n\t\t...(first.cacheWrite1h !== undefined || second.cacheWrite1h !== undefined\n\t\t\t? { cacheWrite1h: (first.cacheWrite1h ?? 0) + (second.cacheWrite1h ?? 0) }\n\t\t\t: {}),\n\t\t...(first.reasoning !== undefined || second.reasoning !== undefined\n\t\t\t? { reasoning: (first.reasoning ?? 0) + (second.reasoning ?? 0) }\n\t\t\t: {}),\n\t\ttotalTokens: first.totalTokens + second.totalTokens,\n\t\tcost: {\n\t\t\tinput: first.cost.input + second.cost.input,\n\t\t\toutput: first.cost.output + second.cost.output,\n\t\t\tcacheRead: first.cost.cacheRead + second.cost.cacheRead,\n\t\t\tcacheWrite: first.cost.cacheWrite + second.cost.cacheWrite,\n\t\t\ttotal: first.cost.total + second.cost.total,\n\t\t},\n\t};\n}\n\nexport interface UsageCostBreakdownEntry {\n\tusageIncomplete?: boolean;\n\tkey: string;\n\tcost: number;\n\ttokens: number;\n}\n\n/** Group model-attributed usage by model and all other usage into a separate bucket. */\nexport function getUsageCostBreakdown(entries: SessionEntry[]): UsageCostBreakdownEntry[] {\n\tconst totalsByKey = new Map<string, UsageTotals>();\n\n\tfor (const entry of entries) {\n\t\tlet key: string | undefined;\n\t\tlet usage: Usage | undefined;\n\t\tif (entry.type === \"message\" && entry.message.role === \"assistant\") {\n\t\t\tkey = `${entry.message.provider}/${entry.message.responseModel ?? entry.message.model}`;\n\t\t\tusage = entry.message.usage;\n\t\t} else if (entry.type === \"usage\") {\n\t\t\tkey = `${entry.provider}/${entry.model}`;\n\t\t\tusage = entry.usage;\n\t\t} else if (entry.type === \"message\" && entry.message.role === \"toolResult\" && entry.message.usage) {\n\t\t\tkey = \"Tools/summaries\";\n\t\t\tusage = entry.message.usage;\n\t\t} else if ((entry.type === \"branch_summary\" || entry.type === \"compaction\") && entry.usage) {\n\t\t\tkey = \"Tools/summaries\";\n\t\t\tusage = entry.usage;\n\t\t}\n\t\tif (!key || !usage) continue;\n\n\t\tlet totals = totalsByKey.get(key);\n\t\tif (!totals) {\n\t\t\ttotals = createUsageTotals();\n\t\t\ttotalsByKey.set(key, totals);\n\t\t}\n\t\taddUsageToTotals(totals, usage);\n\t}\n\n\treturn Array.from(totalsByKey, ([key, totals]) => ({\n\t\tkey,\n\t\t...(totals.usageIncomplete ? { usageIncomplete: true } : {}),\n\t\tcost: totals.cost,\n\t\ttokens: totals.input + totals.output + totals.cacheRead + totals.cacheWrite,\n\t}))\n\t\t.filter((entry) => entry.usageIncomplete || entry.cost > 0 || entry.tokens > 0)\n\t\t.sort((a, b) => b.cost - a.cost);\n}\n"]}
|
|
@@ -634,6 +634,13 @@ function sanitizeSurrogates(text) {
|
|
|
634
634
|
return text.replace(/[\uD800-\uDBFF](?![\uDC00-\uDFFF])|(?<![\uD800-\uDBFF])[\uDC00-\uDFFF]/g, "");
|
|
635
635
|
}
|
|
636
636
|
|
|
637
|
+
// packages/ai/src/utils/usage.ts
|
|
638
|
+
function hasIncompleteUsage(required, optional = []) {
|
|
639
|
+
return [...required, ...optional.filter((value) => value !== void 0)].some(
|
|
640
|
+
(value) => typeof value !== "number" || !Number.isSafeInteger(value) || value < 0
|
|
641
|
+
);
|
|
642
|
+
}
|
|
643
|
+
|
|
637
644
|
// packages/ai/src/api/constrained-sampling.ts
|
|
638
645
|
var UnsupportedStrictJsonSchemaError = class extends Error {
|
|
639
646
|
};
|
|
@@ -1086,6 +1093,7 @@ var stream = (model, context, options = {}) => {
|
|
|
1086
1093
|
provider: model.provider,
|
|
1087
1094
|
model: model.id,
|
|
1088
1095
|
usage: {
|
|
1096
|
+
usageIncomplete: true,
|
|
1089
1097
|
input: 0,
|
|
1090
1098
|
output: 0,
|
|
1091
1099
|
cacheRead: 0,
|
|
@@ -1496,6 +1504,15 @@ function finalizeStreamingBlock(block) {
|
|
|
1496
1504
|
}
|
|
1497
1505
|
function handleMetadata(event, model, output) {
|
|
1498
1506
|
if (event.usage) {
|
|
1507
|
+
output.usage.usageIncomplete = hasIncompleteUsage(
|
|
1508
|
+
[event.usage.inputTokens, event.usage.outputTokens],
|
|
1509
|
+
[
|
|
1510
|
+
event.usage.totalTokens,
|
|
1511
|
+
event.usage.cacheReadInputTokens,
|
|
1512
|
+
event.usage.cacheWriteInputTokens,
|
|
1513
|
+
...event.usage.cacheDetails?.map((detail) => detail.inputTokens) ?? []
|
|
1514
|
+
]
|
|
1515
|
+
);
|
|
1499
1516
|
output.usage.input = event.usage.inputTokens || 0;
|
|
1500
1517
|
output.usage.output = event.usage.outputTokens || 0;
|
|
1501
1518
|
output.usage.cacheRead = event.usage.cacheReadInputTokens || 0;
|
|
@@ -1505,6 +1522,7 @@ function handleMetadata(event, model, output) {
|
|
|
1505
1522
|
0
|
|
1506
1523
|
);
|
|
1507
1524
|
output.usage.totalTokens = event.usage.totalTokens || output.usage.input + output.usage.output;
|
|
1525
|
+
output.usage.usageIncomplete ||= hasIncompleteUsage([output.usage.totalTokens], [output.usage.cacheWrite1h]);
|
|
1508
1526
|
calculateCost(model, output.usage);
|
|
1509
1527
|
}
|
|
1510
1528
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"node-http.d.ts","sourceRoot":"","sources":["../../src/humain/node-http.ts"],"names":[],"mappings":"AAAA,OAAO,EAON,KAAK,eAAe,EAGpB,MAAM,uBAAuB,CAAC;AAmB/B,wBAAgB,kBAAkB,CAAC,GAAG,EAAE,eAAe,GAAG,eAAe,
|
|
1
|
+
{"version":3,"file":"node-http.d.ts","sourceRoot":"","sources":["../../src/humain/node-http.ts"],"names":[],"mappings":"AAAA,OAAO,EAON,KAAK,eAAe,EAGpB,MAAM,uBAAuB,CAAC;AAmB/B,wBAAgB,kBAAkB,CAAC,GAAG,EAAE,eAAe,GAAG,eAAe,CAoJxE"}
|
package/dist/humain/node-http.js
CHANGED
|
@@ -23,6 +23,16 @@ export function withHumainNodeHttp(api) {
|
|
|
23
23
|
let retryAfter = null;
|
|
24
24
|
const key = options?.apiKey;
|
|
25
25
|
const redact = (value) => (key ? value.replaceAll(key, "[redacted]") : value);
|
|
26
|
+
const redactJson = (value) => {
|
|
27
|
+
if (typeof value === "string")
|
|
28
|
+
return redact(value);
|
|
29
|
+
if (Array.isArray(value))
|
|
30
|
+
return value.map(redactJson);
|
|
31
|
+
if (value !== null && typeof value === "object") {
|
|
32
|
+
return Object.fromEntries(Object.entries(value).map(([name, value]) => [redact(name), redactJson(value)]));
|
|
33
|
+
}
|
|
34
|
+
return value;
|
|
35
|
+
};
|
|
26
36
|
const fetcher = options?.fetch ?? globalThis.fetch;
|
|
27
37
|
const requestOptions = {
|
|
28
38
|
...options,
|
|
@@ -100,6 +110,25 @@ export function withHumainNodeHttp(api) {
|
|
|
100
110
|
: api.stream(model, context, requestOptions);
|
|
101
111
|
return (async function* () {
|
|
102
112
|
for await (const event of source) {
|
|
113
|
+
if (event.type === "done" || event.type === "error") {
|
|
114
|
+
const message = event.type === "done" ? event.message : event.error;
|
|
115
|
+
if (message.diagnostics) {
|
|
116
|
+
message.diagnostics = message.diagnostics.map((diagnostic) => ({
|
|
117
|
+
...diagnostic,
|
|
118
|
+
type: redact(diagnostic.type),
|
|
119
|
+
...(diagnostic.error && {
|
|
120
|
+
error: {
|
|
121
|
+
...diagnostic.error,
|
|
122
|
+
message: redact(diagnostic.error.message),
|
|
123
|
+
...(diagnostic.error.name && { name: redact(diagnostic.error.name) }),
|
|
124
|
+
...(diagnostic.error.stack && { stack: redact(diagnostic.error.stack) }),
|
|
125
|
+
...(typeof diagnostic.error.code === "string" && { code: redact(diagnostic.error.code) }),
|
|
126
|
+
},
|
|
127
|
+
}),
|
|
128
|
+
...(diagnostic.details && { details: redactJson(diagnostic.details) }),
|
|
129
|
+
}));
|
|
130
|
+
}
|
|
131
|
+
}
|
|
103
132
|
if (event.type === "error") {
|
|
104
133
|
event.error.errorMessage = redact([
|
|
105
134
|
event.error.errorMessage ?? "HUMAIN Node request failed",
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"node-http.js","sourceRoot":"","sources":["../../src/humain/node-http.ts"],"names":[],"mappings":"AAAA,OAAO,EAKN,UAAU,GAKV,MAAM,uBAAuB,CAAC;AAE/B,SAAS,WAAW,CAAC,KAAc;IAClC,IAAI,KAAK,KAAK,IAAI,IAAI,OAAO,KAAK,KAAK,SAAS,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,OAAO,KAAK,KAAK,QAAQ,EAAE,CAAC;QAC5G,OAAO,IAAI,CAAC;IACb,CAAC;IACD,IAAI,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC;QAAE,OAAO,KAAK,CAAC,KAAK,CAAC,WAAW,CAAC,CAAC;IAC1D,IAAI,OAAO,KAAK,KAAK,QAAQ;QAAE,OAAO,KAAK,CAAC;IAC5C,OAAO,MAAM,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,KAAK,CAAC,WAAW,CAAC,CAAC;AAChD,CAAC;AAED,MAAM,qBAAqB,GAAG,IAAI,GAAG,CAAC;IACrC,qBAAqB;IACrB,gBAAgB;IAChB,+BAA+B;IAC/B,oBAAoB;IACpB,4BAA4B;CAC5B,CAAC,CAAC;AAEH,MAAM,UAAU,kBAAkB,CAAC,GAAoB;IACtD,MAAM,GAAG,GAAG,CACX,KAAiB,EACjB,OAA0B,EAC1B,OAAwC,EACxC,MAAe,EACd,EAAE,CACH,UAAU,CAAC,KAAK,EAAE,KAAK,IAAI,EAAE;QAC5B,IAAI,SAAiC,CAAC;QACtC,IAAI,S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CAAC;wBACf,KAAK,CAAC,KAAK,CAAC,WAAW,GAAG;4BACzB,GAAG,CAAC,KAAK,CAAC,KAAK,CAAC,WAAW,IAAI,EAAE,CAAC;4BAClC,EAAE,IAAI,EAAE,qBAAqB,EAAE,SAAS,EAAE,IAAI,CAAC,GAAG,EAAE,EAAE,OAAO,EAAE,SAAS,EAAE;yBAC1E,CAAC;oBACH,CAAC;gBACF,CAAC;gBACD,MAAM,KAAK,CAAC;YACb,CAAC;QACF,CAAC,CAAC,EAAE,CAAC;IACN,CAAC,CAAC,CAAC;IACJ,OAAO;QACN,MAAM,EAAE,CAAC,KAAK,EAAE,OAAO,EAAE,OAAO,EAAE,EAAE,CAAC,GAAG,CAAC,KAAK,EAAE,OAAO,EAAE,OAAO,EAAE,KAAK,CAAC;QACxE,YAAY,EAAE,CAAC,KAAK,EAAE,OAAO,EAAE,OAAO,EAAE,EAAE,CAAC,GAAG,CAAC,KAAK,EAAE,OAAO,EAAE,OAAO,EAAE,IAAI,CAAC;KAC7E,CAAC;AACH,CAAC","sourcesContent":["import {\n\ttype Api,\n\ttype AssistantMessageEvent,\n\ttype JsonObject,\n\ttype JsonValue,\n\tlazyStream,\n\ttype Model,\n\ttype ProviderStreams,\n\ttype SimpleStreamOptions,\n\ttype TranscriptContext,\n} from \"@earendil-works/pi-ai\";\n\nfunction isJsonValue(value: unknown): value is JsonValue {\n\tif (value === null || typeof value === \"boolean\" || typeof value === \"number\" || typeof value === \"string\") {\n\t\treturn true;\n\t}\n\tif (Array.isArray(value)) return value.every(isJsonValue);\n\tif (typeof value !== \"object\") return false;\n\treturn Object.values(value).every(isJsonValue);\n}\n\nconst PERMANENT_LIMIT_CODES = new Set([\n\t\"weekly_cap_exceeded\",\n\t\"quota_exceeded\",\n\t\"partner_credit_limit_exceeded\",\n\t\"insufficient_quota\",\n\t\"billing_hard_limit_reached\",\n]);\n\nexport function withHumainNodeHttp(api: ProviderStreams): ProviderStreams {\n\tconst run = (\n\t\tmodel: Model<Api>,\n\t\tcontext: TranscriptContext,\n\t\toptions: SimpleStreamOptions | undefined,\n\t\tsimple: boolean,\n\t) =>\n\t\tlazyStream(model, async () => {\n\t\t\tlet httpError: JsonObject | undefined;\n\t\t\tlet requestId: string | null = null;\n\t\t\tlet retryAfter: string | null = null;\n\t\t\tconst key = options?.apiKey;\n\t\t\tconst redact = (value: string): string => (key ? value.replaceAll(key, \"[redacted]\") : value);\n\t\t\tconst fetcher = options?.fetch ?? globalThis.fetch;\n\t\t\tconst requestOptions = {\n\t\t\t\t...options,\n\t\t\t\t// The Anthropic adapter must not interpret a gateway key as an OAuth token.\n\t\t\t\tapiKey: model.api === \"anthropic-messages\" ? \"humain-node\" : key,\n\t\t\t\tfetch: async (input: Parameters<typeof fetch>[0], init?: RequestInit): Promise<Response> => {\n\t\t\t\t\tconst headers = new Headers(init?.headers ?? (input instanceof Request ? input.headers : undefined));\n\t\t\t\t\tif (key) headers.set(\"Authorization\", `Bearer ${key}`);\n\t\t\t\t\theaders.delete(\"x-api-key\");\n\t\t\t\t\tif (model.api === \"anthropic-messages\") {\n\t\t\t\t\t\t// The SDK appends /v1/messages?beta=true; Node exposes <configured base>/messages.\n\t\t\t\t\t\tconst url = `${model.baseUrl.replace(/\\/+$/u, \"\")}/messages`;\n\t\t\t\t\t\tinput = input instanceof Request ? new Request(url, input) : url;\n\t\t\t\t\t\theaders.delete(\"anthropic-beta\");\n\t\t\t\t\t}\n\t\t\t\t\trequestId = null;\n\t\t\t\t\tretryAfter = null;\n\t\t\t\t\thttpError = undefined;\n\t\t\t\t\tconst response = await fetcher(input, { ...init, headers, redirect: \"error\" });\n\t\t\t\t\trequestId = response.headers.get(\"x-request-id\");\n\t\t\t\t\tretryAfter = response.headers.get(\"retry-after\");\n\t\t\t\t\tif (response.ok) return response;\n\t\t\t\t\tconst responseBody: unknown = await response\n\t\t\t\t\t\t.clone()\n\t\t\t\t\t\t.json()\n\t\t\t\t\t\t.catch(() => undefined);\n\t\t\t\t\tconst payload = isJsonValue(responseBody) ? responseBody : undefined;\n\t\t\t\t\tconst error =\n\t\t\t\t\t\ttypeof payload === \"object\" && payload !== null && \"error\" in payload ? payload.error : undefined;\n\t\t\t\t\tconst code =\n\t\t\t\t\t\ttypeof error === \"object\" && error !== null\n\t\t\t\t\t\t\t? \"code\" in error && typeof error.code === \"string\"\n\t\t\t\t\t\t\t\t? error.code\n\t\t\t\t\t\t\t\t: \"type\" in error && typeof error.type === \"string\"\n\t\t\t\t\t\t\t\t\t? error.type\n\t\t\t\t\t\t\t\t\t: undefined\n\t\t\t\t\t\t\t: undefined;\n\t\t\t\t\tlet retryable =\n\t\t\t\t\t\t!PERMANENT_LIMIT_CODES.has(code ?? \"\") &&\n\t\t\t\t\t\tresponse.headers.get(\"x-should-retry\") !== \"false\" &&\n\t\t\t\t\t\t([408, 409, 429].includes(response.status) || response.status >= 500);\n\t\t\t\t\thttpError = { status: response.status, retryable };\n\t\t\t\t\tif (code !== undefined) httpError.code = redact(code);\n\t\t\t\t\tif (retryAfter) {\n\t\t\t\t\t\tconst retryAt = /^\\d+(?:\\.\\d+)?$/u.test(retryAfter)\n\t\t\t\t\t\t\t? Date.now() + Number(retryAfter) * 1000\n\t\t\t\t\t\t\t: Date.parse(retryAfter);\n\t\t\t\t\t\tif (Number.isFinite(retryAt)) {\n\t\t\t\t\t\t\tconst delay = retryAt - Date.now();\n\t\t\t\t\t\t\tconst maxDelay = options?.maxRetryDelayMs ?? 60_000;\n\t\t\t\t\t\t\t// Fail instead of shortening a server delay or overflowing the JS timer.\n\t\t\t\t\t\t\tif (delay > 2_147_483_647 || (maxDelay > 0 && delay > maxDelay)) {\n\t\t\t\t\t\t\t\tretryable = false;\n\t\t\t\t\t\t\t\thttpError.retryable = false;\n\t\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\t\thttpError.retryAt = retryAt;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t\tif (!retryable) {\n\t\t\t\t\t\tconst responseHeaders = new Headers(response.headers);\n\t\t\t\t\t\tresponseHeaders.set(\"x-should-retry\", \"false\");\n\t\t\t\t\t\treturn new Response(response.body, {\n\t\t\t\t\t\t\tstatus: response.status,\n\t\t\t\t\t\t\tstatusText: response.statusText,\n\t\t\t\t\t\t\theaders: responseHeaders,\n\t\t\t\t\t\t});\n\t\t\t\t\t}\n\t\t\t\t\treturn response;\n\t\t\t\t},\n\t\t\t};\n\t\t\tconst source = simple\n\t\t\t\t? api.streamSimple(model, context, requestOptions)\n\t\t\t\t: api.stream(model, context, requestOptions);\n\t\t\treturn (async function* (): AsyncGenerator<AssistantMessageEvent> {\n\t\t\t\tfor await (const event of source) {\n\t\t\t\t\tif (event.type === \"error\") {\n\t\t\t\t\t\tevent.error.errorMessage = redact(\n\t\t\t\t\t\t\t[\n\t\t\t\t\t\t\t\tevent.error.errorMessage ?? \"HUMAIN Node request failed\",\n\t\t\t\t\t\t\t\thttpError?.code && `code: ${httpError.code}`,\n\t\t\t\t\t\t\t\thttpError?.status === 401 && \"Run /login humain-node to replace the API key.\",\n\t\t\t\t\t\t\t\thttpError?.status === 404 &&\n\t\t\t\t\t\t\t\t\thttpError.code === \"model_not_found\" &&\n\t\t\t\t\t\t\t\t\t\"Use /model to refresh the catalog and choose an available model (or restart with --list-models).\",\n\t\t\t\t\t\t\t\tretryAfter && `Retry-After: ${retryAfter}`,\n\t\t\t\t\t\t\t\trequestId && `X-Request-ID: ${requestId}`,\n\t\t\t\t\t\t\t]\n\t\t\t\t\t\t\t\t.filter(Boolean)\n\t\t\t\t\t\t\t\t.join(\"; \"),\n\t\t\t\t\t\t);\n\t\t\t\t\t\tif (httpError) {\n\t\t\t\t\t\t\tevent.error.diagnostics = [\n\t\t\t\t\t\t\t\t...(event.error.diagnostics ?? []),\n\t\t\t\t\t\t\t\t{ type: \"provider_http_error\", timestamp: Date.now(), details: httpError },\n\t\t\t\t\t\t\t];\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t\tyield event;\n\t\t\t\t}\n\t\t\t})();\n\t\t});\n\treturn {\n\t\tstream: (model, context, options) => run(model, context, options, false),\n\t\tstreamSimple: (model, context, options) => run(model, context, options, true),\n\t};\n}\n"]}
|
|
1
|
+
{"version":3,"file":"node-http.js","sourceRoot":"","sources":["../../src/humain/node-http.ts"],"names":[],"mappings":"AAAA,OAAO,EAKN,UAAU,GAKV,MAAM,uBAAuB,CAAC;AAE/B,SAAS,WAAW,CAAC,KAAc;IAClC,IAAI,KAAK,KAAK,IAAI,IAAI,OAAO,KAAK,KAAK,SAAS,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,OAAO,KAAK,KAAK,QAAQ,EAAE,CAAC;QAC5G,OAAO,IAAI,CAAC;IACb,CAAC;IACD,IAAI,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC;QAAE,OAAO,KAAK,CAAC,KAAK,CAAC,WAAW,CAAC,CAAC;IAC1D,IAAI,OAAO,KAAK,KAAK,QAAQ;QAAE,OAAO,KAAK,CAAC;IAC5C,OAAO,MAAM,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,KAAK,CAAC,WAAW,CAAC,CAAC;AAChD,CAAC;AAED,MAAM,qBAAqB,GAAG,IAAI,GAAG,CAAC;IACrC,qBAAqB;IACrB,gBAAgB;IAChB,+BAA+B;IAC/B,oBAAoB;IACpB,4BAA4B;CAC5B,CAAC,CAAC;AAEH,MAAM,UAAU,kBAAkB,CAAC,GAAoB;IACtD,MAAM,GAAG,GAAG,CACX,KAAiB,EACjB,OAA0B,EAC1B,OAAwC,EACxC,MAAe,EACd,EAAE,CACH,UAAU,CAAC,KAAK,EAAE,KAAK,IAAI,EAAE;QAC5B,IAAI,SAAiC,CAAC;QACtC,IAAI,SAAS,GAAkB,IAAI,CAAC;QACpC,IAAI,UAAU,GAAkB,IAAI,CAAC;QACrC,MAAM,GAAG,GAAG,OAAO,EAAE,MAAM,CAAC;QAC5B,MAAM,MAAM,GAAG,CAAC,KAAa,EAAU,EAAE,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BAA4B;wBACxD,SAAS,EAAE,IAAI,IAAI,SAAS,SAAS,CAAC,IAAI,EAAE;wBAC5C,SAAS,EAAE,MAAM,KAAK,GAAG,IAAI,gDAAgD;wBAC7E,SAAS,EAAE,MAAM,KAAK,GAAG;4BACxB,SAAS,CAAC,IAAI,KAAK,iBAAiB;4BACpC,kGAAkG;wBACnG,UAAU,IAAI,gBAAgB,UAAU,EAAE;wBAC1C,SAAS,IAAI,iBAAiB,SAAS,EAAE;qBACzC;yBACC,MAAM,CAAC,OAAO,CAAC;yBACf,IAAI,CAAC,IAAI,CAAC,CACZ,CAAC;oBACF,IAAI,SAAS,EAAE,CAAC;wBACf,KAAK,CAAC,KAAK,CAAC,WAAW,GAAG;4BACzB,GAAG,CAAC,KAAK,CAAC,KAAK,CAAC,WAAW,IAAI,EAAE,CAAC;4BAClC,EAAE,IAAI,EAAE,qBAAqB,EAAE,SAAS,EAAE,IAAI,CAAC,GAAG,EAAE,EAAE,OAAO,EAAE,SAAS,EAAE;yBAC1E,CAAC;oBACH,CAAC;gBACF,CAAC;gBACD,MAAM,KAAK,CAAC;YACb,CAAC;QACF,CAAC,CAAC,EAAE,CAAC;IACN,CAAC,CAAC,CAAC;IACJ,OAAO;QACN,MAAM,EAAE,CAAC,KAAK,EAAE,OAAO,EAAE,OAAO,EAAE,EAAE,CAAC,GAAG,CAAC,KAAK,EAAE,OAAO,EAAE,OAAO,EAAE,KAAK,CAAC;QACxE,YAAY,EAAE,CAAC,KAAK,EAAE,OAAO,EAAE,OAAO,EAAE,EAAE,CAAC,GAAG,CAAC,KAAK,EAAE,OAAO,EAAE,OAAO,EAAE,IAAI,CAAC;KAC7E,CAAC;AACH,CAAC","sourcesContent":["import {\n\ttype Api,\n\ttype AssistantMessageEvent,\n\ttype JsonObject,\n\ttype JsonValue,\n\tlazyStream,\n\ttype Model,\n\ttype ProviderStreams,\n\ttype SimpleStreamOptions,\n\ttype TranscriptContext,\n} from \"@earendil-works/pi-ai\";\n\nfunction isJsonValue(value: unknown): value is JsonValue {\n\tif (value === null || typeof value === \"boolean\" || typeof value === \"number\" || typeof value === \"string\") {\n\t\treturn true;\n\t}\n\tif (Array.isArray(value)) return value.every(isJsonValue);\n\tif (typeof value !== \"object\") return false;\n\treturn Object.values(value).every(isJsonValue);\n}\n\nconst PERMANENT_LIMIT_CODES = new Set([\n\t\"weekly_cap_exceeded\",\n\t\"quota_exceeded\",\n\t\"partner_credit_limit_exceeded\",\n\t\"insufficient_quota\",\n\t\"billing_hard_limit_reached\",\n]);\n\nexport function withHumainNodeHttp(api: ProviderStreams): ProviderStreams {\n\tconst run = (\n\t\tmodel: Model<Api>,\n\t\tcontext: TranscriptContext,\n\t\toptions: SimpleStreamOptions | undefined,\n\t\tsimple: boolean,\n\t) =>\n\t\tlazyStream(model, async () => {\n\t\t\tlet httpError: JsonObject | undefined;\n\t\t\tlet requestId: string | null = null;\n\t\t\tlet retryAfter: string | null = null;\n\t\t\tconst key = options?.apiKey;\n\t\t\tconst redact = (value: string): string => (key ? value.replaceAll(key, \"[redacted]\") : value);\n\t\t\tconst redactJson = (value: JsonValue): JsonValue => {\n\t\t\t\tif (typeof value === \"string\") return redact(value);\n\t\t\t\tif (Array.isArray(value)) return value.map(redactJson);\n\t\t\t\tif (value !== null && typeof value === \"object\") {\n\t\t\t\t\treturn Object.fromEntries(\n\t\t\t\t\t\tObject.entries(value).map(([name, value]) => [redact(name), redactJson(value)]),\n\t\t\t\t\t);\n\t\t\t\t}\n\t\t\t\treturn value;\n\t\t\t};\n\t\t\tconst fetcher = options?.fetch ?? globalThis.fetch;\n\t\t\tconst requestOptions = {\n\t\t\t\t...options,\n\t\t\t\t// The Anthropic adapter must not interpret a gateway key as an OAuth token.\n\t\t\t\tapiKey: model.api === \"anthropic-messages\" ? \"humain-node\" : key,\n\t\t\t\tfetch: async (input: Parameters<typeof fetch>[0], init?: RequestInit): Promise<Response> => {\n\t\t\t\t\tconst headers = new Headers(init?.headers ?? (input instanceof Request ? input.headers : undefined));\n\t\t\t\t\tif (key) headers.set(\"Authorization\", `Bearer ${key}`);\n\t\t\t\t\theaders.delete(\"x-api-key\");\n\t\t\t\t\tif (model.api === \"anthropic-messages\") {\n\t\t\t\t\t\t// The SDK appends /v1/messages?beta=true; Node exposes <configured base>/messages.\n\t\t\t\t\t\tconst url = `${model.baseUrl.replace(/\\/+$/u, \"\")}/messages`;\n\t\t\t\t\t\tinput = input instanceof Request ? new Request(url, input) : url;\n\t\t\t\t\t\theaders.delete(\"anthropic-beta\");\n\t\t\t\t\t}\n\t\t\t\t\trequestId = null;\n\t\t\t\t\tretryAfter = null;\n\t\t\t\t\thttpError = undefined;\n\t\t\t\t\tconst response = await fetcher(input, { ...init, headers, redirect: \"error\" });\n\t\t\t\t\trequestId = response.headers.get(\"x-request-id\");\n\t\t\t\t\tretryAfter = response.headers.get(\"retry-after\");\n\t\t\t\t\tif (response.ok) return response;\n\t\t\t\t\tconst responseBody: unknown = await response\n\t\t\t\t\t\t.clone()\n\t\t\t\t\t\t.json()\n\t\t\t\t\t\t.catch(() => undefined);\n\t\t\t\t\tconst payload = isJsonValue(responseBody) ? responseBody : undefined;\n\t\t\t\t\tconst error =\n\t\t\t\t\t\ttypeof payload === \"object\" && payload !== null && \"error\" in payload ? payload.error : undefined;\n\t\t\t\t\tconst code =\n\t\t\t\t\t\ttypeof error === \"object\" && error !== null\n\t\t\t\t\t\t\t? \"code\" in error && typeof error.code === \"string\"\n\t\t\t\t\t\t\t\t? error.code\n\t\t\t\t\t\t\t\t: \"type\" in error && typeof error.type === \"string\"\n\t\t\t\t\t\t\t\t\t? error.type\n\t\t\t\t\t\t\t\t\t: undefined\n\t\t\t\t\t\t\t: undefined;\n\t\t\t\t\tlet retryable =\n\t\t\t\t\t\t!PERMANENT_LIMIT_CODES.has(code ?? \"\") &&\n\t\t\t\t\t\tresponse.headers.get(\"x-should-retry\") !== \"false\" &&\n\t\t\t\t\t\t([408, 409, 429].includes(response.status) || response.status >= 500);\n\t\t\t\t\thttpError = { status: response.status, retryable };\n\t\t\t\t\tif (code !== undefined) httpError.code = redact(code);\n\t\t\t\t\tif (retryAfter) {\n\t\t\t\t\t\tconst retryAt = /^\\d+(?:\\.\\d+)?$/u.test(retryAfter)\n\t\t\t\t\t\t\t? Date.now() + Number(retryAfter) * 1000\n\t\t\t\t\t\t\t: Date.parse(retryAfter);\n\t\t\t\t\t\tif (Number.isFinite(retryAt)) {\n\t\t\t\t\t\t\tconst delay = retryAt - Date.now();\n\t\t\t\t\t\t\tconst maxDelay = options?.maxRetryDelayMs ?? 60_000;\n\t\t\t\t\t\t\t// Fail instead of shortening a server delay or overflowing the JS timer.\n\t\t\t\t\t\t\tif (delay > 2_147_483_647 || (maxDelay > 0 && delay > maxDelay)) {\n\t\t\t\t\t\t\t\tretryable = false;\n\t\t\t\t\t\t\t\thttpError.retryable = false;\n\t\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\t\thttpError.retryAt = retryAt;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t\tif (!retryable) {\n\t\t\t\t\t\tconst responseHeaders = new Headers(response.headers);\n\t\t\t\t\t\tresponseHeaders.set(\"x-should-retry\", \"false\");\n\t\t\t\t\t\treturn new Response(response.body, {\n\t\t\t\t\t\t\tstatus: response.status,\n\t\t\t\t\t\t\tstatusText: response.statusText,\n\t\t\t\t\t\t\theaders: responseHeaders,\n\t\t\t\t\t\t});\n\t\t\t\t\t}\n\t\t\t\t\treturn response;\n\t\t\t\t},\n\t\t\t};\n\t\t\tconst source = simple\n\t\t\t\t? api.streamSimple(model, context, requestOptions)\n\t\t\t\t: api.stream(model, context, requestOptions);\n\t\t\treturn (async function* (): AsyncGenerator<AssistantMessageEvent> {\n\t\t\t\tfor await (const event of source) {\n\t\t\t\t\tif (event.type === \"done\" || event.type === \"error\") {\n\t\t\t\t\t\tconst message = event.type === \"done\" ? event.message : event.error;\n\t\t\t\t\t\tif (message.diagnostics) {\n\t\t\t\t\t\t\tmessage.diagnostics = message.diagnostics.map((diagnostic) => ({\n\t\t\t\t\t\t\t\t...diagnostic,\n\t\t\t\t\t\t\t\ttype: redact(diagnostic.type),\n\t\t\t\t\t\t\t\t...(diagnostic.error && {\n\t\t\t\t\t\t\t\t\terror: {\n\t\t\t\t\t\t\t\t\t\t...diagnostic.error,\n\t\t\t\t\t\t\t\t\t\tmessage: redact(diagnostic.error.message),\n\t\t\t\t\t\t\t\t\t\t...(diagnostic.error.name && { name: redact(diagnostic.error.name) }),\n\t\t\t\t\t\t\t\t\t\t...(diagnostic.error.stack && { stack: redact(diagnostic.error.stack) }),\n\t\t\t\t\t\t\t\t\t\t...(typeof diagnostic.error.code === \"string\" && { code: redact(diagnostic.error.code) }),\n\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t}),\n\t\t\t\t\t\t\t\t...(diagnostic.details && { details: redactJson(diagnostic.details) as JsonObject }),\n\t\t\t\t\t\t\t}));\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t\tif (event.type === \"error\") {\n\t\t\t\t\t\tevent.error.errorMessage = redact(\n\t\t\t\t\t\t\t[\n\t\t\t\t\t\t\t\tevent.error.errorMessage ?? \"HUMAIN Node request failed\",\n\t\t\t\t\t\t\t\thttpError?.code && `code: ${httpError.code}`,\n\t\t\t\t\t\t\t\thttpError?.status === 401 && \"Run /login humain-node to replace the API key.\",\n\t\t\t\t\t\t\t\thttpError?.status === 404 &&\n\t\t\t\t\t\t\t\t\thttpError.code === \"model_not_found\" &&\n\t\t\t\t\t\t\t\t\t\"Use /model to refresh the catalog and choose an available model (or restart with --list-models).\",\n\t\t\t\t\t\t\t\tretryAfter && `Retry-After: ${retryAfter}`,\n\t\t\t\t\t\t\t\trequestId && `X-Request-ID: ${requestId}`,\n\t\t\t\t\t\t\t]\n\t\t\t\t\t\t\t\t.filter(Boolean)\n\t\t\t\t\t\t\t\t.join(\"; \"),\n\t\t\t\t\t\t);\n\t\t\t\t\t\tif (httpError) {\n\t\t\t\t\t\t\tevent.error.diagnostics = [\n\t\t\t\t\t\t\t\t...(event.error.diagnostics ?? []),\n\t\t\t\t\t\t\t\t{ type: \"provider_http_error\", timestamp: Date.now(), details: httpError },\n\t\t\t\t\t\t\t];\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t\tyield event;\n\t\t\t\t}\n\t\t\t})();\n\t\t});\n\treturn {\n\t\tstream: (model, context, options) => run(model, context, options, false),\n\t\tstreamSimple: (model, context, options) => run(model, context, options, true),\n\t};\n}\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/humain/trace-capture/index.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EACX,kBAAkB,EAMlB,MAAM,qCAAqC,CAAC;AAK7C,OAAO,KAAK,EAEX,gBAAgB,EAIhB,MAAM,gCAAgC,CAAC;AAQxC,OAAO,EAA0B,KAAK,iBAAiB,EAAE,MAAM,6BAA6B,CAAC;AAC7F,OAAO,EAGN,KAAK,sBAAsB,EAI3B,MAAM,4BAA4B,CAAC;AACpC,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,sCAAsC,CAAC;AAwCjF,OAAO,EAA6B,KAAK,sBAAsB,EAAE,MAAM,mBAAmB,CAAC;AAC3F,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,mBAAmB,CAAC;AAG5D,OAAO,EAAE,sBAAsB,EAAE,MAAM,sBAAsB,CAAC;AAC9D,OAAO,EAAE,sBAAsB,EAAE,kBAAkB,EAAE,MAAM,mBAAmB,CAAC;AA+B/E,6EAA6E;AAC7E,MAAM,WAAW,mBAAmB;IACnC,4FAA4F;IAC5F,UAAU,CAAC,EAAE,sBAAsB,CAAC;IACpC,gFAAgF;IAChF,WAAW,CAAC,EAAE,sBAAsB,CAAC;IACrC,mEAAmE;IACnE,OAAO,CAAC,EAAE,kBAAkB,CAAC;IAC7B,6EAA6E;IAC7E,cAAc,CAAC,E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BAAkB,CAAC;IAC7B,6EAA6E;IAC7E,cAAc,CAAC,EAAE,SAAS,MAAM,EAAE,CAAC;IACnC,+GAA+G;IAC/G,SAAS,CAAC,EAAE,kBAAkB,CAAC;IAC/B,cAAc,CAAC,EAAE,MAAM,CAAC;IACxB,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB,0FAA0F;IAC1F,YAAY,CAAC,EAAE,iBAAiB,CAAC;IACjC,wEAAwE;IACxE,sBAAsB,CAAC,EAAE,CAAC,OAAO,EAAE,iBAAiB,EAAE,MAAM,CAAC,EAAE,WAAW,KAAK,OAAO,CAAC,oBAAoB,CAAC,CAAC;IAC7G,iGAAiG;IACjG,SAAS,CAAC,EAAE,oBAAoB,CAAC;CACjC;AAi1ED;;;;GAIG;AACH,wBAAgB,wCAAwC,CACvD,GAAG,EAAE,MAAM,CAAC,UAAU,EACtB,OAAO,GAAE,mBAAwB,GAC/B,gBAAgB,CAgClB"}
|
|
@@ -1054,20 +1054,23 @@ function recordModelOutcome(capture, recorder, event, cwd) {
|
|
|
1054
1054
|
}
|
|
1055
1055
|
const usage = event.usage;
|
|
1056
1056
|
if (usage !== undefined &&
|
|
1057
|
-
|
|
1057
|
+
(usage.usageIncomplete !== undefined ||
|
|
1058
|
+
[usage.totalTokens, usage.input, usage.output, usage.cacheRead, usage.cacheWrite, usage.reasoning].some((count) => count !== undefined && count > 0))) {
|
|
1058
1059
|
payload.usage = {
|
|
1059
1060
|
origin: { owner: "submission", submissionId: event.submissionId },
|
|
1060
|
-
measurement:
|
|
1061
|
-
state: "
|
|
1062
|
-
|
|
1063
|
-
|
|
1064
|
-
|
|
1065
|
-
|
|
1066
|
-
|
|
1067
|
-
|
|
1068
|
-
|
|
1069
|
-
|
|
1070
|
-
|
|
1061
|
+
measurement: usage.usageIncomplete
|
|
1062
|
+
? { state: "unknown" }
|
|
1063
|
+
: {
|
|
1064
|
+
state: "reported",
|
|
1065
|
+
input: usage.input,
|
|
1066
|
+
output: usage.output,
|
|
1067
|
+
cacheRead: usage.cacheRead,
|
|
1068
|
+
cacheWrite: usage.cacheWrite,
|
|
1069
|
+
totalTokens: usage.totalTokens,
|
|
1070
|
+
...(usage.reasoning !== undefined ? { reasoning: usage.reasoning } : {}),
|
|
1071
|
+
// The usage cost carries no currency or price provenance, so it stays unknown here.
|
|
1072
|
+
cost: { state: "unknown" },
|
|
1073
|
+
},
|
|
1071
1074
|
};
|
|
1072
1075
|
}
|
|
1073
1076
|
const outcome = event.superseded
|