@buoy-gg/agent-core 7.0.41 → 7.0.42
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/lib/commonjs/blocks/receipts.js +1 -761
- package/lib/commonjs/blocks/runId.js +1 -31
- package/lib/commonjs/blocks/types.js +1 -140
- package/lib/commonjs/blocks/uiTool.js +6 -1405
- package/lib/commonjs/catalog/catalog.g.js +5 -4556
- package/lib/commonjs/catalog/catalog.source.json +249 -0
- package/lib/commonjs/catalog/catalog.types.g.js +1 -46
- package/lib/commonjs/catalog/normalizeParams.js +2 -332
- package/lib/commonjs/catalog/signature.js +1 -68
- package/lib/commonjs/catalog/snapshotReads.js +1 -263
- package/lib/commonjs/catalog/toProviderTools.js +2 -145
- package/lib/commonjs/catalog/validateParams.js +1 -129
- package/lib/commonjs/context/buildContextPack.js +1 -489
- package/lib/commonjs/effects/digest.js +1 -34
- package/lib/commonjs/effects/ledger.js +1 -345
- package/lib/commonjs/engine/askGate.js +1 -287
- package/lib/commonjs/engine/effectFor.js +1 -223
- package/lib/commonjs/engine/evidence.js +3 -111
- package/lib/commonjs/engine/historyBudget.js +1 -363
- package/lib/commonjs/engine/retrieve.js +1 -214
- package/lib/commonjs/engine/runAgentTurn.js +8 -1836
- package/lib/commonjs/engine/systemPrompt.js +54 -163
- package/lib/commonjs/engine/textToolCalls.js +1 -276
- package/lib/commonjs/engine/tokenCalibration.js +1 -81
- package/lib/commonjs/engine/verify.js +4 -297
- package/lib/commonjs/index.js +1 -384
- package/lib/commonjs/policy/labels.js +1 -319
- package/lib/commonjs/policy/policy.js +1 -148
- package/lib/commonjs/policy/redact.js +1 -172
- package/lib/commonjs/providers/anthropic.js +3 -445
- package/lib/commonjs/providers/openai.js +4 -324
- package/lib/commonjs/providers/problem.js +1 -98
- package/lib/commonjs/providers/sse.js +1 -240
- package/lib/commonjs/providers/streamTimer.js +1 -123
- package/lib/commonjs/providers/transport.js +1 -123
- package/lib/commonjs/providers/types.js +1 -6
- package/lib/commonjs/providers/xhrStream.js +1 -212
- package/lib/commonjs/realNow.js +1 -0
- package/lib/commonjs/session.js +1 -393
- package/lib/commonjs/types.js +0 -1
- package/lib/module/blocks/receipts.js +1 -755
- package/lib/module/blocks/runId.js +1 -26
- package/lib/module/blocks/types.js +1 -134
- package/lib/module/blocks/uiTool.js +6 -1396
- package/lib/module/catalog/catalog.g.js +5 -4552
- package/lib/module/catalog/catalog.source.json +249 -0
- package/lib/module/catalog/catalog.types.g.js +1 -42
- package/lib/module/catalog/normalizeParams.js +2 -326
- package/lib/module/catalog/signature.js +1 -63
- package/lib/module/catalog/snapshotReads.js +1 -259
- package/lib/module/catalog/toProviderTools.js +2 -139
- package/lib/module/catalog/validateParams.js +1 -124
- package/lib/module/context/buildContextPack.js +1 -485
- package/lib/module/effects/digest.js +1 -29
- package/lib/module/effects/ledger.js +1 -339
- package/lib/module/engine/askGate.js +1 -280
- package/lib/module/engine/effectFor.js +1 -220
- package/lib/module/engine/evidence.js +3 -105
- package/lib/module/engine/historyBudget.js +1 -355
- package/lib/module/engine/retrieve.js +1 -210
- package/lib/module/engine/runAgentTurn.js +8 -1827
- package/lib/module/engine/systemPrompt.js +54 -157
- package/lib/module/engine/textToolCalls.js +1 -270
- package/lib/module/engine/tokenCalibration.js +1 -75
- package/lib/module/engine/verify.js +4 -289
- package/lib/module/index.js +1 -35
- package/lib/module/policy/labels.js +1 -312
- package/lib/module/policy/policy.js +1 -142
- package/lib/module/policy/redact.js +1 -165
- package/lib/module/providers/anthropic.js +3 -441
- package/lib/module/providers/openai.js +4 -320
- package/lib/module/providers/problem.js +1 -92
- package/lib/module/providers/sse.js +1 -231
- package/lib/module/providers/streamTimer.js +1 -118
- package/lib/module/providers/transport.js +1 -119
- package/lib/module/providers/types.js +1 -4
- package/lib/module/providers/xhrStream.js +1 -207
- package/lib/module/realNow.js +1 -0
- package/lib/module/session.js +1 -369
- package/lib/module/types.js +0 -1
- package/lib/typescript/catalog/catalog.g.d.ts +3 -3
- package/lib/typescript/catalog/catalog.types.g.d.ts +3 -2
- package/lib/typescript/providers/problem.d.ts +0 -20
- package/lib/typescript/providers/streamTimer.d.ts +0 -24
- package/lib/typescript/realNow.d.ts +18 -0
- package/lib/web/index.mjs +52 -53
- package/package.json +1 -1
- package/lib/commonjs/blocks/receipts.js.map +0 -1
- package/lib/commonjs/blocks/runId.js.map +0 -1
- package/lib/commonjs/blocks/types.js.map +0 -1
- package/lib/commonjs/blocks/uiTool.js.map +0 -1
- package/lib/commonjs/catalog/catalog.g.js.map +0 -1
- package/lib/commonjs/catalog/catalog.types.g.js.map +0 -1
- package/lib/commonjs/catalog/normalizeParams.js.map +0 -1
- package/lib/commonjs/catalog/signature.js.map +0 -1
- package/lib/commonjs/catalog/snapshotReads.js.map +0 -1
- package/lib/commonjs/catalog/toProviderTools.js.map +0 -1
- package/lib/commonjs/catalog/validateParams.js.map +0 -1
- package/lib/commonjs/context/buildContextPack.js.map +0 -1
- package/lib/commonjs/effects/digest.js.map +0 -1
- package/lib/commonjs/effects/ledger.js.map +0 -1
- package/lib/commonjs/engine/askGate.js.map +0 -1
- package/lib/commonjs/engine/effectFor.js.map +0 -1
- package/lib/commonjs/engine/evidence.js.map +0 -1
- package/lib/commonjs/engine/historyBudget.js.map +0 -1
- package/lib/commonjs/engine/retrieve.js.map +0 -1
- package/lib/commonjs/engine/runAgentTurn.js.map +0 -1
- package/lib/commonjs/engine/systemPrompt.js.map +0 -1
- package/lib/commonjs/engine/textToolCalls.js.map +0 -1
- package/lib/commonjs/engine/tokenCalibration.js.map +0 -1
- package/lib/commonjs/engine/verify.js.map +0 -1
- package/lib/commonjs/index.js.map +0 -1
- package/lib/commonjs/policy/labels.js.map +0 -1
- package/lib/commonjs/policy/policy.js.map +0 -1
- package/lib/commonjs/policy/redact.js.map +0 -1
- package/lib/commonjs/providers/anthropic.js.map +0 -1
- package/lib/commonjs/providers/openai.js.map +0 -1
- package/lib/commonjs/providers/problem.js.map +0 -1
- package/lib/commonjs/providers/sse.js.map +0 -1
- package/lib/commonjs/providers/streamTimer.js.map +0 -1
- package/lib/commonjs/providers/transport.js.map +0 -1
- package/lib/commonjs/providers/types.js.map +0 -1
- package/lib/commonjs/providers/xhrStream.js.map +0 -1
- package/lib/commonjs/session.js.map +0 -1
- package/lib/commonjs/types.js.map +0 -1
- package/lib/module/blocks/receipts.js.map +0 -1
- package/lib/module/blocks/runId.js.map +0 -1
- package/lib/module/blocks/types.js.map +0 -1
- package/lib/module/blocks/uiTool.js.map +0 -1
- package/lib/module/catalog/catalog.g.js.map +0 -1
- package/lib/module/catalog/catalog.types.g.js.map +0 -1
- package/lib/module/catalog/normalizeParams.js.map +0 -1
- package/lib/module/catalog/signature.js.map +0 -1
- package/lib/module/catalog/snapshotReads.js.map +0 -1
- package/lib/module/catalog/toProviderTools.js.map +0 -1
- package/lib/module/catalog/validateParams.js.map +0 -1
- package/lib/module/context/buildContextPack.js.map +0 -1
- package/lib/module/effects/digest.js.map +0 -1
- package/lib/module/effects/ledger.js.map +0 -1
- package/lib/module/engine/askGate.js.map +0 -1
- package/lib/module/engine/effectFor.js.map +0 -1
- package/lib/module/engine/evidence.js.map +0 -1
- package/lib/module/engine/historyBudget.js.map +0 -1
- package/lib/module/engine/retrieve.js.map +0 -1
- package/lib/module/engine/runAgentTurn.js.map +0 -1
- package/lib/module/engine/systemPrompt.js.map +0 -1
- package/lib/module/engine/textToolCalls.js.map +0 -1
- package/lib/module/engine/tokenCalibration.js.map +0 -1
- package/lib/module/engine/verify.js.map +0 -1
- package/lib/module/index.js.map +0 -1
- package/lib/module/policy/labels.js.map +0 -1
- package/lib/module/policy/policy.js.map +0 -1
- package/lib/module/policy/redact.js.map +0 -1
- package/lib/module/providers/anthropic.js.map +0 -1
- package/lib/module/providers/openai.js.map +0 -1
- package/lib/module/providers/problem.js.map +0 -1
- package/lib/module/providers/sse.js.map +0 -1
- package/lib/module/providers/streamTimer.js.map +0 -1
- package/lib/module/providers/transport.js.map +0 -1
- package/lib/module/providers/types.js.map +0 -1
- package/lib/module/providers/xhrStream.js.map +0 -1
- package/lib/module/session.js.map +0 -1
- package/lib/module/types.js.map +0 -1
- package/lib/typescript/blocks/receipts.d.ts.map +0 -1
- package/lib/typescript/blocks/runId.d.ts.map +0 -1
- package/lib/typescript/blocks/types.d.ts.map +0 -1
- package/lib/typescript/blocks/uiTool.d.ts.map +0 -1
- package/lib/typescript/catalog/catalog.g.d.ts.map +0 -1
- package/lib/typescript/catalog/catalog.types.g.d.ts.map +0 -1
- package/lib/typescript/catalog/normalizeParams.d.ts.map +0 -1
- package/lib/typescript/catalog/signature.d.ts.map +0 -1
- package/lib/typescript/catalog/snapshotReads.d.ts.map +0 -1
- package/lib/typescript/catalog/toProviderTools.d.ts.map +0 -1
- package/lib/typescript/catalog/validateParams.d.ts.map +0 -1
- package/lib/typescript/context/buildContextPack.d.ts.map +0 -1
- package/lib/typescript/effects/digest.d.ts.map +0 -1
- package/lib/typescript/effects/ledger.d.ts.map +0 -1
- package/lib/typescript/engine/askGate.d.ts.map +0 -1
- package/lib/typescript/engine/effectFor.d.ts.map +0 -1
- package/lib/typescript/engine/evidence.d.ts.map +0 -1
- package/lib/typescript/engine/historyBudget.d.ts.map +0 -1
- package/lib/typescript/engine/retrieve.d.ts.map +0 -1
- package/lib/typescript/engine/runAgentTurn.d.ts.map +0 -1
- package/lib/typescript/engine/systemPrompt.d.ts.map +0 -1
- package/lib/typescript/engine/textToolCalls.d.ts.map +0 -1
- package/lib/typescript/engine/tokenCalibration.d.ts.map +0 -1
- package/lib/typescript/engine/verify.d.ts.map +0 -1
- package/lib/typescript/index.d.ts.map +0 -1
- package/lib/typescript/policy/labels.d.ts.map +0 -1
- package/lib/typescript/policy/policy.d.ts.map +0 -1
- package/lib/typescript/policy/redact.d.ts.map +0 -1
- package/lib/typescript/providers/anthropic.d.ts.map +0 -1
- package/lib/typescript/providers/openai.d.ts.map +0 -1
- package/lib/typescript/providers/problem.d.ts.map +0 -1
- package/lib/typescript/providers/sse.d.ts.map +0 -1
- package/lib/typescript/providers/streamTimer.d.ts.map +0 -1
- package/lib/typescript/providers/transport.d.ts.map +0 -1
- package/lib/typescript/providers/types.d.ts.map +0 -1
- package/lib/typescript/providers/xhrStream.d.ts.map +0 -1
- package/lib/typescript/session.d.ts.map +0 -1
- package/lib/typescript/types.d.ts.map +0 -1
|
@@ -1,172 +1 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
6
|
-
exports.isSelfTraffic = isSelfTraffic;
|
|
7
|
-
exports.redact = redact;
|
|
8
|
-
exports.registerSelfEndpoint = registerSelfEndpoint;
|
|
9
|
-
exports.stripSelfTraffic = stripSelfTraffic;
|
|
10
|
-
/**
|
|
11
|
-
* Scrub tool results before they leave the device.
|
|
12
|
-
*
|
|
13
|
-
* Two separate reasons, and the second is the one people forget:
|
|
14
|
-
*
|
|
15
|
-
* 1. EGRESS. Tool results go to the customer's own model endpoint. That is
|
|
16
|
-
* their data boundary, not ours — but a response body carrying an
|
|
17
|
-
* `Authorization` header or a Buoy `licenseKey` is a credential, and
|
|
18
|
-
* credentials do not belong in a chat transcript that may be retained for
|
|
19
|
-
* 30 days for abuse monitoring.
|
|
20
|
-
* 2. SELF-INJECTION. Buoy's own network interceptor captures the agent's
|
|
21
|
-
* requests to the model endpoint. Without exclusion, the model reads back
|
|
22
|
-
* its own request headers — including whatever the host app's `headers()`
|
|
23
|
-
* returned — out of `network.getRequests`. That is a credential leak and a
|
|
24
|
-
* prompt-injection channel in one call.
|
|
25
|
-
*/
|
|
26
|
-
|
|
27
|
-
/**
|
|
28
|
-
* Key names that mean "credential", compared with separators stripped —
|
|
29
|
-
* `X_API_KEY`, `x-api-key` and `xApiKey` are the same word. A list of hyphen
|
|
30
|
-
* spellings let `EXPO_PUBLIC_X_API_KEY=…` through to the model whole on a real
|
|
31
|
-
* app, alongside `EXPO_PUBLIC_BUOY_KEY`, which is the Pro licence that gates
|
|
32
|
-
* this very tool. Both went out in plaintext while `…_PASSWORD` was caught.
|
|
33
|
-
*/
|
|
34
|
-
const SECRET_KEYS = ["authorization", "cookie", "setcookie", "apikey", "proxyauthorization", "authtoken", "accesstoken", "refreshtoken", "idtoken", "sessiontoken", "bearertoken", "licensekey", "licencekey", "buoykey", "privatekey", "secret", "password", "passwd", "credential"];
|
|
35
|
-
const REDACTED = "[redacted by Buoy]";
|
|
36
|
-
|
|
37
|
-
/**
|
|
38
|
-
* Credential SHAPES, for values whose key name gives nothing away — a JWT
|
|
39
|
-
* stored under `session`, a Bearer header quoted inside a response body. The
|
|
40
|
-
* key-name check alone let those through whole. Same shapes the broker wire
|
|
41
|
-
* scrubber uses; duplicated here because agent-core cannot depend on ask-buoy.
|
|
42
|
-
*/
|
|
43
|
-
const SECRET_VALUE_PATTERNS = [{
|
|
44
|
-
re: /\bBearer\s+[A-Za-z0-9._~+/-]{12,}=*/gi,
|
|
45
|
-
label: "[bearer token removed]"
|
|
46
|
-
}, {
|
|
47
|
-
re: /\beyJ[A-Za-z0-9._-]{20,}/g,
|
|
48
|
-
label: "[jwt removed]"
|
|
49
|
-
}, {
|
|
50
|
-
re: /\bsk-[A-Za-z0-9_-]{16,}/g,
|
|
51
|
-
label: "[api key removed]"
|
|
52
|
-
}, {
|
|
53
|
-
re: /\bBUOY-[A-Z0-9-]{8,}/g,
|
|
54
|
-
label: "[license key removed]"
|
|
55
|
-
},
|
|
56
|
-
// Keygen-issued Buoy Pro keys: five hex sextets and a version suffix.
|
|
57
|
-
{
|
|
58
|
-
re: /\b[A-F0-9]{6}(?:-[A-F0-9]{6}){4}-V\d\b/g,
|
|
59
|
-
label: "[license key removed]"
|
|
60
|
-
}, {
|
|
61
|
-
// Key-name spellings inside free text: `X_API_KEY=…`, `apiKey: "…"`,
|
|
62
|
-
// `EXPO_PUBLIC_BUOY_KEY=…`. Word-prefixed forms (EXPO_PUBLIC_, REACT_APP_)
|
|
63
|
-
// are covered because the name only has to END in the credential word.
|
|
64
|
-
re: /("|')?[A-Za-z0-9_-]*(authorization|api[_-]?key|password|passwd|secret|access[_-]?token|refresh[_-]?token|auth[_-]?token|session[_-]?token|license[_-]?key|licence[_-]?key|buoy[_-]?key|private[_-]?key)\1?\s*[:=]\s*("|')?[^\s"',}]{6,}/gi,
|
|
65
|
-
label: "[credential removed]"
|
|
66
|
-
}];
|
|
67
|
-
function scrubString(s) {
|
|
68
|
-
let out = s;
|
|
69
|
-
for (const {
|
|
70
|
-
re,
|
|
71
|
-
label
|
|
72
|
-
} of SECRET_VALUE_PATTERNS) {
|
|
73
|
-
out = out.replace(re, label);
|
|
74
|
-
}
|
|
75
|
-
return out;
|
|
76
|
-
}
|
|
77
|
-
function isSecretKey(key) {
|
|
78
|
-
const k = key.toLowerCase().replace(/[-_.\s]/g, "");
|
|
79
|
-
return SECRET_KEYS.some(s => k.includes(s));
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
/**
|
|
83
|
-
* Deep-redact by key name AND by value shape. Depth-limited because tool
|
|
84
|
-
* results can be deeply nested state trees and a runaway walk on a megabyte
|
|
85
|
-
* payload is a frame drop on the JS thread the app is also using.
|
|
86
|
-
*/
|
|
87
|
-
function redact(value, depth = 0) {
|
|
88
|
-
if (typeof value === "string") return scrubString(value);
|
|
89
|
-
if (depth > 12 || value === null || typeof value !== "object") return value;
|
|
90
|
-
if (Array.isArray(value)) {
|
|
91
|
-
return value.map(v => redact(v, depth + 1));
|
|
92
|
-
}
|
|
93
|
-
const out = {};
|
|
94
|
-
for (const [key, v] of Object.entries(value)) {
|
|
95
|
-
out[key] = isSecretKey(key) ? REDACTED : redact(v, depth + 1);
|
|
96
|
-
}
|
|
97
|
-
return out;
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
/**
|
|
101
|
-
* Hosts that must never appear in a tool result, because they are the agent's
|
|
102
|
-
* own traffic. Registered by the runtime from the configured endpoint.
|
|
103
|
-
*/
|
|
104
|
-
const selfHosts = new Set();
|
|
105
|
-
|
|
106
|
-
/**
|
|
107
|
-
* Where the network tool looks for hosts it must not touch.
|
|
108
|
-
*
|
|
109
|
-
* A GLOBAL, not an import, because the dependency cannot go either way:
|
|
110
|
-
* `agent-core` ships with no dependencies at all, and `@buoy-gg/network` is
|
|
111
|
-
* optional — most apps that install Ask Buoy do not have it, and the ones that
|
|
112
|
-
* do may load it after. A well-known key is the only seam that works in both
|
|
113
|
-
* directions without either package requiring the other.
|
|
114
|
-
*
|
|
115
|
-
* Registering a host here means the interceptor passes its requests straight
|
|
116
|
-
* through: not captured, not overridable, and — the reason this exists — not
|
|
117
|
-
* held open while the interceptor reads the body. See `networkListener.ts`.
|
|
118
|
-
*/
|
|
119
|
-
const INTERNAL_HOSTS_KEY = "__BUOY_INTERNAL_HOSTS__";
|
|
120
|
-
function registerSelfEndpoint(endpoint) {
|
|
121
|
-
try {
|
|
122
|
-
// No URL polyfill assumptions — a plain parse is enough for a host match.
|
|
123
|
-
const match = /^[a-z]+:\/\/([^/?#]+)/i.exec(endpoint);
|
|
124
|
-
if (!match?.[1]) return;
|
|
125
|
-
const host = match[1].toLowerCase();
|
|
126
|
-
selfHosts.add(host);
|
|
127
|
-
const g = globalThis;
|
|
128
|
-
const existing = g[INTERNAL_HOSTS_KEY];
|
|
129
|
-
const hosts = existing instanceof Set ? existing : new Set();
|
|
130
|
-
hosts.add(host);
|
|
131
|
-
g[INTERNAL_HOSTS_KEY] = hosts;
|
|
132
|
-
} catch {
|
|
133
|
-
/* an unparseable endpoint just means no exclusion */
|
|
134
|
-
}
|
|
135
|
-
}
|
|
136
|
-
function isSelfTraffic(url) {
|
|
137
|
-
if (typeof url !== "string") return false;
|
|
138
|
-
const lower = url.toLowerCase();
|
|
139
|
-
for (const host of selfHosts) {
|
|
140
|
-
if (lower.includes(host)) return true;
|
|
141
|
-
}
|
|
142
|
-
return false;
|
|
143
|
-
}
|
|
144
|
-
function hasSelfUrl(item) {
|
|
145
|
-
if (!item || typeof item !== "object") return false;
|
|
146
|
-
const rec = item;
|
|
147
|
-
return isSelfTraffic(rec.url ?? rec.uri ?? rec.href);
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
/**
|
|
151
|
-
* Drop the agent's own requests out of a network-shaped result before the
|
|
152
|
-
* model sees them.
|
|
153
|
-
*
|
|
154
|
-
* WALKS INTO OBJECTS, not just top-level arrays. A network reader returns
|
|
155
|
-
* `{ totalCaptured, requests: [...] }`, not a bare array, and a version of
|
|
156
|
-
* this that only handled the bare array quietly did nothing — the agent's own
|
|
157
|
-
* POST to the model endpoint, headers and all, came straight back to it out
|
|
158
|
-
* of the network read. Depth-limited for the same reason `redact` is: this
|
|
159
|
-
* runs on the JS thread the app is drawing on.
|
|
160
|
-
*/
|
|
161
|
-
function stripSelfTraffic(value, depth = 0) {
|
|
162
|
-
if (depth > 6 || value === null || typeof value !== "object") return value;
|
|
163
|
-
if (Array.isArray(value)) {
|
|
164
|
-
return value.filter(item => !hasSelfUrl(item)).map(item => stripSelfTraffic(item, depth + 1));
|
|
165
|
-
}
|
|
166
|
-
const out = {};
|
|
167
|
-
for (const [key, v] of Object.entries(value)) {
|
|
168
|
-
out[key] = stripSelfTraffic(v, depth + 1);
|
|
169
|
-
}
|
|
170
|
-
return out;
|
|
171
|
-
}
|
|
172
|
-
//# sourceMappingURL=redact.js.map
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:true});exports.isSelfTraffic=isSelfTraffic;exports.redact=redact;exports.registerSelfEndpoint=registerSelfEndpoint;exports.stripSelfTraffic=stripSelfTraffic;const SECRET_KEYS=["authorization","cookie","setcookie","apikey","proxyauthorization","authtoken","accesstoken","refreshtoken","idtoken","sessiontoken","bearertoken","licensekey","licencekey","buoykey","privatekey","secret","password","passwd","credential"];const REDACTED="[redacted by Buoy]";const SECRET_VALUE_PATTERNS=[{re:/\bBearer\s+[A-Za-z0-9._~+/-]{12,}=*/gi,label:"[bearer token removed]"},{re:/\beyJ[A-Za-z0-9._-]{20,}/g,label:"[jwt removed]"},{re:/\bsk-[A-Za-z0-9_-]{16,}/g,label:"[api key removed]"},{re:/\bBUOY-[A-Z0-9-]{8,}/g,label:"[license key removed]"},{re:/\b[A-F0-9]{6}(?:-[A-F0-9]{6}){4}-V\d\b/g,label:"[license key removed]"},{re:/("|')?[A-Za-z0-9_-]*(authorization|api[_-]?key|password|passwd|secret|access[_-]?token|refresh[_-]?token|auth[_-]?token|session[_-]?token|license[_-]?key|licence[_-]?key|buoy[_-]?key|private[_-]?key)\1?\s*[:=]\s*("|')?[^\s"',}]{6,}/gi,label:"[credential removed]"}];function scrubString(e){let r=e;for(const{re:o,label:t}of SECRET_VALUE_PATTERNS){r=r.replace(o,t)}return r}function isSecretKey(e){const r=e.toLowerCase().replace(/[-_.\s]/g,"");return SECRET_KEYS.some(o=>r.includes(o))}function redact(e,r=0){if(typeof e==="string")return scrubString(e);if(r>12||e===null||typeof e!=="object")return e;if(Array.isArray(e)){return e.map(t=>redact(t,r+1))}const o={};for(const[t,s]of Object.entries(e)){o[t]=isSecretKey(t)?REDACTED:redact(s,r+1)}return o}const selfHosts=new Set;const INTERNAL_HOSTS_KEY="__BUOY_INTERNAL_HOSTS__";function registerSelfEndpoint(e){try{const r=/^[a-z]+:\/\/([^/?#]+)/i.exec(e);if(!r?.[1])return;const o=r[1].toLowerCase();selfHosts.add(o);const t=globalThis;const s=t[INTERNAL_HOSTS_KEY];const n=s instanceof Set?s:new Set;n.add(o);t[INTERNAL_HOSTS_KEY]=n}catch{}}function isSelfTraffic(e){if(typeof e!=="string")return false;const r=e.toLowerCase();for(const o of selfHosts){if(r.includes(o))return true}return false}function hasSelfUrl(e){if(!e||typeof e!=="object")return false;const r=e;return isSelfTraffic(r.url??r.uri??r.href)}function stripSelfTraffic(e,r=0){if(r>6||e===null||typeof e!=="object")return e;if(Array.isArray(e)){return e.filter(t=>!hasSelfUrl(t)).map(t=>stripSelfTraffic(t,r+1))}const o={};for(const[t,s]of Object.entries(e)){o[t]=stripSelfTraffic(s,r+1)}return o}
|
|
@@ -1,447 +1,5 @@
|
|
|
1
|
-
"use strict";
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:true});exports.createAnthropicProvider=createAnthropicProvider;var _sse=require("./sse");var _streamTimer=require("./streamTimer");var _transport=require("./transport");var _problem=require("./problem");const DEFAULT_VERSION="2023-06-01";function markLastBlock(a){const i=a[a.length-1];const s=i?.content[i.content.length-1];if(s){s.cache_control={type:"ephemeral"}}return a}function toWireMessages(a){const i=[];for(const s of a){if(s.role==="user"){i.push({role:"user",content:[{type:"text",text:s.text}]})}else if(s.role==="assistant"){const o=[];for(const r of s.thinking??[])o.push(r);if(s.text)o.push({type:"text",text:s.text});for(const r of s.toolCalls??[]){o.push({type:"tool_use",id:r.id,name:r.name,input:r.input})}if(o.length)i.push({role:"assistant",content:o})}else{i.push({role:"user",content:s.results.map(o=>({type:"tool_result",tool_use_id:o.toolCallId,content:o.content,...o.isError?{is_error:true}:{}}))})}}return i}function createAnthropicProvider(a){const i=(0,_transport.createTransport)(a);return{protocol:"anthropic",async*send(s){const o=(0,_streamTimer.createStreamTimer)(s.signal,a);const r={truncated:false};try{const l={"content-type":"application/json","anthropic-version":a.anthropicVersion??DEFAULT_VERSION,...await(a.headers?.()??{})};if(a.apiKey&&!l["x-api-key"]&&!l.Authorization){l["x-api-key"]=a.apiKey;l["anthropic-dangerous-direct-browser-access"]="true"}const p=await i.open({headers:l,signal:o.signal,body:JSON.stringify({model:s.model,max_tokens:s.maxTokens,stream:true,system:a.cache===false?s.systemVolatile?`${s.system}
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
6
|
-
exports.createAnthropicProvider = createAnthropicProvider;
|
|
7
|
-
var _sse = require("./sse");
|
|
8
|
-
var _streamTimer = require("./streamTimer");
|
|
9
|
-
var _transport = require("./transport");
|
|
10
|
-
var _problem = require("./problem");
|
|
11
|
-
/**
|
|
12
|
-
* Anthropic Messages protocol.
|
|
13
|
-
*
|
|
14
|
-
* Four things here are not obvious and each one is a silent failure if missed:
|
|
15
|
-
*
|
|
16
|
-
* 1. `content_block_start` for a `tool_use` carries `"input": {}` — an EMPTY
|
|
17
|
-
* object, never the real arguments. The arguments only exist after
|
|
18
|
-
* concatenating every `input_json_delta.partial_json` and parsing at
|
|
19
|
-
* `content_block_stop`. Reading `.input` at start yields a tool call with no
|
|
20
|
-
* arguments and no error.
|
|
21
|
-
* 2. There is NO `data: [DONE]` sentinel (removed in the 2023-06-01 version).
|
|
22
|
-
* The stream ends at `message_stop`.
|
|
23
|
-
* 3. `error` events arrive MID-STREAM on an already-committed HTTP 200 —
|
|
24
|
-
* `overloaded_error` being the common one. A 200 is not success.
|
|
25
|
-
* 4. Ordering is 400-level strict: `tool_result` blocks must come FIRST in the
|
|
26
|
-
* user content array with any text after, and that user message must
|
|
27
|
-
* immediately follow the assistant turn. "Here are the results:" placed
|
|
28
|
-
* before the blocks is an instant 400.
|
|
29
|
-
* 5. Every current Claude model thinks by DEFAULT, and a `thinking` block must
|
|
30
|
-
* be replayed VERBATIM (signature and all) at the FRONT of the assistant
|
|
31
|
-
* turn it came from whenever that turn's tool results are sent back. An
|
|
32
|
-
* adapter that keeps only text and tool_use 400s on the second request of
|
|
33
|
-
* every tool-using turn — which is every turn Ask Buoy actually makes.
|
|
34
|
-
*/
|
|
3
|
+
---
|
|
35
4
|
|
|
36
|
-
const
|
|
37
|
-
/**
|
|
38
|
-
* Mark the last content block of the last message as a cache breakpoint.
|
|
39
|
-
*
|
|
40
|
-
* WHY THERE IS A SECOND BREAKPOINT AT ALL. The first one, at the end of
|
|
41
|
-
* `system`, caches the tools and the system prompt — the big fixed prefix. It
|
|
42
|
-
* does nothing for the messages, which were left uncached on the reasoning
|
|
43
|
-
* that they grow every turn and so would pay the write multiplier for a prefix
|
|
44
|
-
* never read back. That is true of a turn with ONE request. It is false of the
|
|
45
|
-
* turns this tool actually makes: a turn runs 3 to 8 model requests, each
|
|
46
|
-
* re-sending the whole conversation plus everything the previous steps read,
|
|
47
|
-
* and steps 2..N are exactly a re-read of what step 1 already sent.
|
|
48
|
-
*
|
|
49
|
-
* So the breakpoint MOVES: each request marks its own last block, and the next
|
|
50
|
-
* request hits it (Anthropic looks back ~20 blocks for a match). Step 1 writes
|
|
51
|
-
* the messages at 1.25x, every later step reads them at 0.1x and writes only
|
|
52
|
-
* its own delta.
|
|
53
|
-
*
|
|
54
|
-
* What it does NOT buy is cross-TURN caching, because the volatile "right now"
|
|
55
|
-
* block sits at the end of `system` and changes every turn, invalidating
|
|
56
|
-
* anything after it. Within a turn is where the multiplier lives, and that is
|
|
57
|
-
* what this collects.
|
|
58
|
-
*/
|
|
59
|
-
function markLastBlock(out) {
|
|
60
|
-
const lastMessage = out[out.length - 1];
|
|
61
|
-
const lastBlock = lastMessage?.content[lastMessage.content.length - 1];
|
|
62
|
-
if (lastBlock) {
|
|
63
|
-
lastBlock.cache_control = {
|
|
64
|
-
type: "ephemeral"
|
|
65
|
-
};
|
|
66
|
-
}
|
|
67
|
-
return out;
|
|
68
|
-
}
|
|
69
|
-
function toWireMessages(messages) {
|
|
70
|
-
const out = [];
|
|
71
|
-
for (const m of messages) {
|
|
72
|
-
if (m.role === "user") {
|
|
73
|
-
out.push({
|
|
74
|
-
role: "user",
|
|
75
|
-
content: [{
|
|
76
|
-
type: "text",
|
|
77
|
-
text: m.text
|
|
78
|
-
}]
|
|
79
|
-
});
|
|
80
|
-
} else if (m.role === "assistant") {
|
|
81
|
-
const content = [];
|
|
82
|
-
// Thinking FIRST — see note 5. Signatures are opaque; never edit them.
|
|
83
|
-
for (const t of m.thinking ?? []) content.push(t);
|
|
84
|
-
if (m.text) content.push({
|
|
85
|
-
type: "text",
|
|
86
|
-
text: m.text
|
|
87
|
-
});
|
|
88
|
-
for (const c of m.toolCalls ?? []) {
|
|
89
|
-
content.push({
|
|
90
|
-
type: "tool_use",
|
|
91
|
-
id: c.id,
|
|
92
|
-
name: c.name,
|
|
93
|
-
input: c.input
|
|
94
|
-
});
|
|
95
|
-
}
|
|
96
|
-
if (content.length) out.push({
|
|
97
|
-
role: "assistant",
|
|
98
|
-
content
|
|
99
|
-
});
|
|
100
|
-
} else {
|
|
101
|
-
// tool_result blocks FIRST — see note 4. No preamble text, ever.
|
|
102
|
-
out.push({
|
|
103
|
-
role: "user",
|
|
104
|
-
content: m.results.map(r => ({
|
|
105
|
-
type: "tool_result",
|
|
106
|
-
tool_use_id: r.toolCallId,
|
|
107
|
-
content: r.content,
|
|
108
|
-
...(r.isError ? {
|
|
109
|
-
is_error: true
|
|
110
|
-
} : {})
|
|
111
|
-
}))
|
|
112
|
-
});
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
return out;
|
|
116
|
-
}
|
|
117
|
-
function createAnthropicProvider(config) {
|
|
118
|
-
// One per provider, so the `auto` transport decision is made once and not
|
|
119
|
-
// re-learned (and re-buffered) at the start of every turn.
|
|
120
|
-
const transport = (0, _transport.createTransport)(config);
|
|
121
|
-
return {
|
|
122
|
-
protocol: "anthropic",
|
|
123
|
-
async *send(req) {
|
|
124
|
-
const timer = (0, _streamTimer.createStreamTimer)(req.signal, config);
|
|
125
|
-
const status = {
|
|
126
|
-
truncated: false
|
|
127
|
-
};
|
|
128
|
-
try {
|
|
129
|
-
const headers = {
|
|
130
|
-
"content-type": "application/json",
|
|
131
|
-
"anthropic-version": config.anthropicVersion ?? DEFAULT_VERSION,
|
|
132
|
-
...(await (config.headers?.() ?? {}))
|
|
133
|
-
};
|
|
134
|
-
// Dev-only direct mode. A distributed build uses `headers` against the
|
|
135
|
-
// org's own gateway and never carries a provider key at all.
|
|
136
|
-
if (config.apiKey && !headers["x-api-key"] && !headers.Authorization) {
|
|
137
|
-
headers["x-api-key"] = config.apiKey;
|
|
138
|
-
// Without this the POST comes back with no access-control-allow-origin
|
|
139
|
-
// and a browser/Electron renderer silently discards an otherwise fine
|
|
140
|
-
// response — a 200 in the network log and "Failed to fetch" in JS.
|
|
141
|
-
headers["anthropic-dangerous-direct-browser-access"] = "true";
|
|
142
|
-
}
|
|
143
|
-
const stream = await transport.open({
|
|
144
|
-
headers,
|
|
145
|
-
// The timer's signal, not the caller's: it aborts on the user's Stop
|
|
146
|
-
// AND on either clock running out. See providers/streamTimer.ts.
|
|
147
|
-
signal: timer.signal,
|
|
148
|
-
body: JSON.stringify({
|
|
149
|
-
model: req.model,
|
|
150
|
-
max_tokens: req.maxTokens,
|
|
151
|
-
stream: true,
|
|
152
|
-
// ONE cache breakpoint, at the end of `system`.
|
|
153
|
-
//
|
|
154
|
-
// Anthropic caches the prefix in a fixed order — tools, then system,
|
|
155
|
-
// then messages — up to and including the block carrying
|
|
156
|
-
// `cache_control`. So marking the end of the system prompt caches
|
|
157
|
-
// the tool definitions with it, which is the point: the tools are
|
|
158
|
-
// ~20k tokens and by far the larger half. A second breakpoint on the
|
|
159
|
-
// last tool would buy nothing and spend one of the four available.
|
|
160
|
-
//
|
|
161
|
-
// Messages stay uncached on purpose. They grow every turn, so they
|
|
162
|
-
// would invalidate on each request and pay the write multiplier for
|
|
163
|
-
// a prefix that is never read back.
|
|
164
|
-
//
|
|
165
|
-
// The volatile block (what is on screen right now) goes in a SECOND
|
|
166
|
-
// text block after the breakpoint. It changes every turn; inside the
|
|
167
|
-
// cached block it would invalidate the whole prefix each time.
|
|
168
|
-
system: config.cache === false ? req.systemVolatile ? `${req.system}\n\n---\n\n${req.systemVolatile}` : req.system : [{
|
|
169
|
-
type: "text",
|
|
170
|
-
text: req.system,
|
|
171
|
-
cache_control: {
|
|
172
|
-
type: "ephemeral"
|
|
173
|
-
}
|
|
174
|
-
}, ...(req.systemVolatile ? [{
|
|
175
|
-
type: "text",
|
|
176
|
-
text: req.systemVolatile
|
|
177
|
-
}] : [])],
|
|
178
|
-
tools: req.tools.map(t => ({
|
|
179
|
-
name: t.name,
|
|
180
|
-
description: t.description,
|
|
181
|
-
input_schema: t.inputSchema,
|
|
182
|
-
// Anthropic-only. Each example must validate against input_schema
|
|
183
|
-
// or the API 400s, so these are generated from the catalog rather
|
|
184
|
-
// than written by hand.
|
|
185
|
-
...(t.inputExamples?.length ? {
|
|
186
|
-
input_examples: t.inputExamples
|
|
187
|
-
} : {})
|
|
188
|
-
})),
|
|
189
|
-
messages: config.cache === false ? toWireMessages(req.messages) : markLastBlock(toWireMessages(req.messages)),
|
|
190
|
-
...config.requestOverrides
|
|
191
|
-
})
|
|
192
|
-
});
|
|
193
|
-
|
|
194
|
-
// Headers are in: the connect clock stops here and the idle one starts.
|
|
195
|
-
timer.connected();
|
|
196
|
-
if (stream.problem) {
|
|
197
|
-
yield {
|
|
198
|
-
type: "error",
|
|
199
|
-
message: stream.problem,
|
|
200
|
-
kind: "provider",
|
|
201
|
-
problem: stream.problemInfo
|
|
202
|
-
};
|
|
203
|
-
return;
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
// Partial tool_use args accumulate here, keyed by content-block index.
|
|
207
|
-
const pending = new Map();
|
|
208
|
-
// Partial thinking blocks, same keying. `thinking_delta` carries the
|
|
209
|
-
// text and `signature_delta` the signature, and the signature usually
|
|
210
|
-
// arrives in ONE delta right at the end — a reader that stops at
|
|
211
|
-
// thinking_delta replays an unsigned block, which the API rejects.
|
|
212
|
-
const thinking = new Map();
|
|
213
|
-
let stopReason;
|
|
214
|
-
let usage;
|
|
215
|
-
let model;
|
|
216
|
-
/** The protocol's end marker; ends the read. See `sawTerminal` below. */
|
|
217
|
-
let sawStop = false;
|
|
218
|
-
for await (const ev of stream.frames({
|
|
219
|
-
status,
|
|
220
|
-
onFrame: timer.activity,
|
|
221
|
-
signal: timer.signal
|
|
222
|
-
})) {
|
|
223
|
-
let payload;
|
|
224
|
-
try {
|
|
225
|
-
payload = JSON.parse(ev.data);
|
|
226
|
-
} catch {
|
|
227
|
-
continue;
|
|
228
|
-
}
|
|
229
|
-
const kind = payload.type ?? ev.event;
|
|
230
|
-
switch (kind) {
|
|
231
|
-
case "content_block_start":
|
|
232
|
-
{
|
|
233
|
-
const block = payload.content_block;
|
|
234
|
-
if (block?.type === "thinking") {
|
|
235
|
-
thinking.set(payload.index, {
|
|
236
|
-
thinking: block.thinking ?? "",
|
|
237
|
-
signature: block.signature ?? ""
|
|
238
|
-
});
|
|
239
|
-
} else if (block?.type === "redacted_thinking") {
|
|
240
|
-
// No deltas for these — the whole (encrypted) payload is here.
|
|
241
|
-
yield {
|
|
242
|
-
type: "thinking",
|
|
243
|
-
block: {
|
|
244
|
-
type: "redacted_thinking",
|
|
245
|
-
data: block.data
|
|
246
|
-
}
|
|
247
|
-
};
|
|
248
|
-
} else if (block?.type === "tool_use") {
|
|
249
|
-
// NOT block.input — see note 1.
|
|
250
|
-
pending.set(payload.index, {
|
|
251
|
-
id: block.id,
|
|
252
|
-
name: block.name,
|
|
253
|
-
json: ""
|
|
254
|
-
});
|
|
255
|
-
}
|
|
256
|
-
break;
|
|
257
|
-
}
|
|
258
|
-
case "content_block_delta":
|
|
259
|
-
{
|
|
260
|
-
const delta = payload.delta;
|
|
261
|
-
if (delta?.type === "text_delta") {
|
|
262
|
-
yield {
|
|
263
|
-
type: "text",
|
|
264
|
-
delta: delta.text
|
|
265
|
-
};
|
|
266
|
-
} else if (delta?.type === "input_json_delta") {
|
|
267
|
-
const slot = pending.get(payload.index);
|
|
268
|
-
if (slot) slot.json += delta.partial_json ?? "";
|
|
269
|
-
} else if (delta?.type === "thinking_delta") {
|
|
270
|
-
const slot = thinking.get(payload.index);
|
|
271
|
-
if (slot) slot.thinking += delta.thinking ?? "";
|
|
272
|
-
} else if (delta?.type === "signature_delta") {
|
|
273
|
-
const slot = thinking.get(payload.index);
|
|
274
|
-
if (slot) slot.signature += delta.signature ?? "";
|
|
275
|
-
}
|
|
276
|
-
break;
|
|
277
|
-
}
|
|
278
|
-
case "content_block_stop":
|
|
279
|
-
{
|
|
280
|
-
const thought = thinking.get(payload.index);
|
|
281
|
-
if (thought) {
|
|
282
|
-
thinking.delete(payload.index);
|
|
283
|
-
yield {
|
|
284
|
-
type: "thinking",
|
|
285
|
-
block: {
|
|
286
|
-
type: "thinking",
|
|
287
|
-
...thought
|
|
288
|
-
}
|
|
289
|
-
};
|
|
290
|
-
}
|
|
291
|
-
const slot = pending.get(payload.index);
|
|
292
|
-
if (slot) {
|
|
293
|
-
pending.delete(payload.index);
|
|
294
|
-
let input = {};
|
|
295
|
-
try {
|
|
296
|
-
input = slot.json ? JSON.parse(slot.json) : {};
|
|
297
|
-
} catch {
|
|
298
|
-
yield {
|
|
299
|
-
type: "error",
|
|
300
|
-
kind: "provider",
|
|
301
|
-
// Same evidence as the OpenAI path. The stop reason is not
|
|
302
|
-
// known yet here — it rides on `message_delta`, which comes
|
|
303
|
-
// after this block closes — so only the raw tail is carried.
|
|
304
|
-
message: (0, _sse.unparseableMessage)(slot.name, slot.json)
|
|
305
|
-
};
|
|
306
|
-
break;
|
|
307
|
-
}
|
|
308
|
-
const call = {
|
|
309
|
-
id: slot.id,
|
|
310
|
-
name: slot.name,
|
|
311
|
-
input
|
|
312
|
-
};
|
|
313
|
-
yield {
|
|
314
|
-
type: "tool-call",
|
|
315
|
-
call
|
|
316
|
-
};
|
|
317
|
-
}
|
|
318
|
-
break;
|
|
319
|
-
}
|
|
320
|
-
case "message_start":
|
|
321
|
-
{
|
|
322
|
-
// The cache counters arrive HERE, not on message_delta — the
|
|
323
|
-
// prefix is accounted for before a single token is generated. A
|
|
324
|
-
// reader that only watches message_delta sees cacheRead 0 on
|
|
325
|
-
// every request and concludes caching is not working.
|
|
326
|
-
const m = payload.message;
|
|
327
|
-
// The served model id is named here and nowhere else — a gateway
|
|
328
|
-
// resolves an alias to a dated snapshot, and this is that id.
|
|
329
|
-
if (typeof m?.model === "string" && m.model) model = m.model;
|
|
330
|
-
const u = m?.usage;
|
|
331
|
-
if (u) {
|
|
332
|
-
usage = {
|
|
333
|
-
input: u.input_tokens ?? 0,
|
|
334
|
-
output: u.output_tokens ?? 0,
|
|
335
|
-
...(u.cache_read_input_tokens !== undefined ? {
|
|
336
|
-
cacheRead: u.cache_read_input_tokens
|
|
337
|
-
} : {}),
|
|
338
|
-
...(u.cache_creation_input_tokens !== undefined ? {
|
|
339
|
-
cacheWrite: u.cache_creation_input_tokens
|
|
340
|
-
} : {})
|
|
341
|
-
};
|
|
342
|
-
}
|
|
343
|
-
break;
|
|
344
|
-
}
|
|
345
|
-
case "message_delta":
|
|
346
|
-
{
|
|
347
|
-
const delta = payload.delta;
|
|
348
|
-
// `stop_reason` is the model's own statement that it finished,
|
|
349
|
-
// and counts as terminal on its own (see `sawTerminal`). It does
|
|
350
|
-
// NOT end the read: `message_stop` is one frame away and there is
|
|
351
|
-
// no reason to cancel a stream that is about to close itself.
|
|
352
|
-
if (delta?.stop_reason) stopReason = delta.stop_reason;
|
|
353
|
-
// Cumulative, not incremental — assign, never add. Output tokens
|
|
354
|
-
// are only final here, so keep whatever message_start reported
|
|
355
|
-
// about the cache rather than replacing the whole record.
|
|
356
|
-
const u = payload.usage;
|
|
357
|
-
if (u) {
|
|
358
|
-
usage = {
|
|
359
|
-
...usage,
|
|
360
|
-
input: u.input_tokens ?? usage?.input ?? 0,
|
|
361
|
-
output: u.output_tokens ?? 0
|
|
362
|
-
};
|
|
363
|
-
}
|
|
364
|
-
break;
|
|
365
|
-
}
|
|
366
|
-
case "error":
|
|
367
|
-
{
|
|
368
|
-
const err = payload.error;
|
|
369
|
-
const message = err?.message ?? "Provider error";
|
|
370
|
-
yield {
|
|
371
|
-
type: "error",
|
|
372
|
-
message,
|
|
373
|
-
kind: "provider",
|
|
374
|
-
// `overloaded_error` on a committed 200 is the common shape —
|
|
375
|
-
// classified so the engine can retry it when nothing came first.
|
|
376
|
-
problem: (0, _problem.classifyProblem)({
|
|
377
|
-
type: typeof err?.type === "string" ? err.type : undefined,
|
|
378
|
-
message
|
|
379
|
-
})
|
|
380
|
-
};
|
|
381
|
-
return;
|
|
382
|
-
}
|
|
383
|
-
case "message_stop":
|
|
384
|
-
// The end. There is no [DONE] — see note 2.
|
|
385
|
-
sawStop = true;
|
|
386
|
-
break;
|
|
387
|
-
default:
|
|
388
|
-
break;
|
|
389
|
-
}
|
|
390
|
-
// `break` inside the switch leaves the switch, not the loop.
|
|
391
|
-
if (sawStop) break;
|
|
392
|
-
}
|
|
393
|
-
|
|
394
|
-
/**
|
|
395
|
-
* Did the provider actually say it was finished?
|
|
396
|
-
*
|
|
397
|
-
* `message_stop` is the protocol's end marker. `stop_reason` counts
|
|
398
|
-
* too: it is the model's own statement of why it stopped, which is the
|
|
399
|
-
* stronger claim — `message_stop` only says the SSE stream closed. A
|
|
400
|
-
* proxy that forwards the delta and drops the final frame has still
|
|
401
|
-
* delivered a whole answer, and refusing it would break working
|
|
402
|
-
* gateways for no safety gain. NEITHER arriving is the case that
|
|
403
|
-
* matters: bare EOF, which used to be read as success.
|
|
404
|
-
*/
|
|
405
|
-
const sawTerminal = sawStop || stopReason !== undefined;
|
|
406
|
-
if (!sawTerminal) {
|
|
407
|
-
// Bare EOF. Whatever text arrived is real and the engine will show it,
|
|
408
|
-
// but the plan behind it is half-written: a connection cut after
|
|
409
|
-
// `{"key":"cart","value":[]}` leaves valid JSON that reads exactly like
|
|
410
|
-
// a finished thought. Nothing here is admitted for execution.
|
|
411
|
-
yield {
|
|
412
|
-
type: "error",
|
|
413
|
-
message: (0, _sse.truncatedMessage)(status.truncated),
|
|
414
|
-
kind: "truncated"
|
|
415
|
-
};
|
|
416
|
-
return;
|
|
417
|
-
}
|
|
418
|
-
yield {
|
|
419
|
-
type: "done",
|
|
420
|
-
outcome: stopReason === "max_tokens" ? "output-limited" : "completed",
|
|
421
|
-
stopReason,
|
|
422
|
-
usage,
|
|
423
|
-
model
|
|
424
|
-
};
|
|
425
|
-
} catch (err) {
|
|
426
|
-
// The user's Stop wins the label even if a clock fired in the same
|
|
427
|
-
// tick: they know why the turn ended, and "timed out" would be a lie.
|
|
428
|
-
if (req.signal?.aborted) throw err;
|
|
429
|
-
const message = timer.message();
|
|
430
|
-
if (message) {
|
|
431
|
-
yield {
|
|
432
|
-
type: "error",
|
|
433
|
-
message,
|
|
434
|
-
kind: "timeout"
|
|
435
|
-
};
|
|
436
|
-
return;
|
|
437
|
-
}
|
|
438
|
-
// A genuine transport failure — DNS, refused connection, TLS. It has
|
|
439
|
-
// always propagated to the failed-turn card, and still does.
|
|
440
|
-
throw err;
|
|
441
|
-
} finally {
|
|
442
|
-
timer.dispose();
|
|
443
|
-
}
|
|
444
|
-
}
|
|
445
|
-
};
|
|
446
|
-
}
|
|
447
|
-
//# sourceMappingURL=anthropic.js.map
|
|
5
|
+
${s.systemVolatile}`:s.system:[{type:"text",text:s.system,cache_control:{type:"ephemeral"}},...s.systemVolatile?[{type:"text",text:s.systemVolatile}]:[]],tools:s.tools.map(c=>({name:c.name,description:c.description,input_schema:c.inputSchema,...c.inputExamples?.length?{input_examples:c.inputExamples}:{}})),messages:a.cache===false?toWireMessages(s.messages):markLastBlock(toWireMessages(s.messages)),...a.requestOverrides})});o.connected();if(p.problem){yield{type:"error",message:p.problem,kind:"provider",problem:p.problemInfo};return}const m=new Map;const d=new Map;let y;let u;let h;let _=false;for await(const c of p.frames({status:r,onFrame:o.activity,signal:o.signal})){let n;try{n=JSON.parse(c.data)}catch{continue}const f=n.type??c.event;switch(f){case"content_block_start":{const e=n.content_block;if(e?.type==="thinking"){d.set(n.index,{thinking:e.thinking??"",signature:e.signature??""})}else if(e?.type==="redacted_thinking"){yield{type:"thinking",block:{type:"redacted_thinking",data:e.data}}}else if(e?.type==="tool_use"){m.set(n.index,{id:e.id,name:e.name,json:""})}break}case"content_block_delta":{const e=n.delta;if(e?.type==="text_delta"){yield{type:"text",delta:e.text}}else if(e?.type==="input_json_delta"){const t=m.get(n.index);if(t)t.json+=e.partial_json??""}else if(e?.type==="thinking_delta"){const t=d.get(n.index);if(t)t.thinking+=e.thinking??""}else if(e?.type==="signature_delta"){const t=d.get(n.index);if(t)t.signature+=e.signature??""}break}case"content_block_stop":{const e=d.get(n.index);if(e){d.delete(n.index);yield{type:"thinking",block:{type:"thinking",...e}}}const t=m.get(n.index);if(t){m.delete(n.index);let g={};try{g=t.json?JSON.parse(t.json):{}}catch{yield{type:"error",kind:"provider",message:(0,_sse.unparseableMessage)(t.name,t.json)};break}const x={id:t.id,name:t.name,input:g};yield{type:"tool-call",call:x}}break}case"message_start":{const e=n.message;if(typeof e?.model==="string"&&e.model)h=e.model;const t=e?.usage;if(t){u={input:t.input_tokens??0,output:t.output_tokens??0,...t.cache_read_input_tokens!==void 0?{cacheRead:t.cache_read_input_tokens}:{},...t.cache_creation_input_tokens!==void 0?{cacheWrite:t.cache_creation_input_tokens}:{}}}break}case"message_delta":{const e=n.delta;if(e?.stop_reason)y=e.stop_reason;const t=n.usage;if(t){u={...u,input:t.input_tokens??u?.input??0,output:t.output_tokens??0}}break}case"error":{const e=n.error;const t=e?.message??"Provider error";yield{type:"error",message:t,kind:"provider",problem:(0,_problem.classifyProblem)({type:typeof e?.type==="string"?e.type:void 0,message:t})};return}case"message_stop":_=true;break;default:break}if(_)break}const k=_||y!==void 0;if(!k){yield{type:"error",message:(0,_sse.truncatedMessage)(r.truncated),kind:"truncated"};return}yield{type:"done",outcome:y==="max_tokens"?"output-limited":"completed",stopReason:y,usage:u,model:h}}catch(l){if(s.signal?.aborted)throw l;const p=o.message();if(p){yield{type:"error",message:p,kind:"timeout"};return}throw l}finally{o.dispose()}}}}
|