@buoy-gg/agent-core 7.0.35 → 7.0.38
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/README.md +21 -14
- package/lib/commonjs/blocks/receipts.js +57 -1
- package/lib/commonjs/blocks/receipts.js.map +1 -1
- package/lib/commonjs/blocks/types.js +23 -2
- package/lib/commonjs/blocks/types.js.map +1 -1
- package/lib/commonjs/blocks/uiTool.js +423 -10
- package/lib/commonjs/blocks/uiTool.js.map +1 -1
- package/lib/commonjs/catalog/catalog.g.js +213 -48
- package/lib/commonjs/catalog/catalog.g.js.map +1 -1
- package/lib/commonjs/catalog/catalog.source.json +226 -48
- package/lib/commonjs/catalog/catalog.types.g.js +44 -0
- package/lib/commonjs/catalog/catalog.types.g.js.map +1 -0
- package/lib/commonjs/catalog/normalizeParams.js +333 -0
- package/lib/commonjs/catalog/normalizeParams.js.map +1 -0
- package/lib/commonjs/catalog/signature.js +68 -0
- package/lib/commonjs/catalog/signature.js.map +1 -0
- package/lib/commonjs/catalog/snapshotReads.js +13 -6
- package/lib/commonjs/catalog/snapshotReads.js.map +1 -1
- package/lib/commonjs/catalog/toProviderTools.js +44 -6
- package/lib/commonjs/catalog/toProviderTools.js.map +1 -1
- package/lib/commonjs/context/buildContextPack.js +367 -19
- package/lib/commonjs/context/buildContextPack.js.map +1 -1
- package/lib/commonjs/effects/digest.js +34 -0
- package/lib/commonjs/effects/digest.js.map +1 -0
- package/lib/commonjs/effects/ledger.js +98 -2
- package/lib/commonjs/effects/ledger.js.map +1 -1
- package/lib/commonjs/engine/askGate.js +287 -0
- package/lib/commonjs/engine/askGate.js.map +1 -0
- package/lib/commonjs/engine/effectFor.js +82 -1
- package/lib/commonjs/engine/effectFor.js.map +1 -1
- package/lib/commonjs/engine/evidence.js +113 -0
- package/lib/commonjs/engine/evidence.js.map +1 -0
- package/lib/commonjs/engine/historyBudget.js +363 -0
- package/lib/commonjs/engine/historyBudget.js.map +1 -0
- package/lib/commonjs/engine/retrieve.js +214 -0
- package/lib/commonjs/engine/retrieve.js.map +1 -0
- package/lib/commonjs/engine/runAgentTurn.js +1231 -120
- package/lib/commonjs/engine/runAgentTurn.js.map +1 -1
- package/lib/commonjs/engine/systemPrompt.js +103 -9
- package/lib/commonjs/engine/systemPrompt.js.map +1 -1
- package/lib/commonjs/engine/textToolCalls.js +276 -0
- package/lib/commonjs/engine/textToolCalls.js.map +1 -0
- package/lib/commonjs/engine/tokenCalibration.js +81 -0
- package/lib/commonjs/engine/tokenCalibration.js.map +1 -0
- package/lib/commonjs/engine/verify.js +300 -0
- package/lib/commonjs/engine/verify.js.map +1 -0
- package/lib/commonjs/index.js +74 -0
- package/lib/commonjs/index.js.map +1 -1
- package/lib/commonjs/policy/labels.js +19 -3
- package/lib/commonjs/policy/labels.js.map +1 -1
- package/lib/commonjs/policy/policy.js +6 -1
- package/lib/commonjs/policy/policy.js.map +1 -1
- package/lib/commonjs/policy/redact.js +42 -5
- package/lib/commonjs/policy/redact.js.map +1 -1
- package/lib/commonjs/providers/anthropic.js +340 -208
- package/lib/commonjs/providers/anthropic.js.map +1 -1
- package/lib/commonjs/providers/openai.js +237 -103
- package/lib/commonjs/providers/openai.js.map +1 -1
- package/lib/commonjs/providers/problem.js +98 -0
- package/lib/commonjs/providers/problem.js.map +1 -0
- package/lib/commonjs/providers/sse.js +170 -34
- package/lib/commonjs/providers/sse.js.map +1 -1
- package/lib/commonjs/providers/streamTimer.js +123 -0
- package/lib/commonjs/providers/streamTimer.js.map +1 -0
- package/lib/commonjs/providers/transport.js +123 -0
- package/lib/commonjs/providers/transport.js.map +1 -0
- package/lib/commonjs/providers/xhrStream.js +212 -0
- package/lib/commonjs/providers/xhrStream.js.map +1 -0
- package/lib/commonjs/session.js +242 -58
- package/lib/commonjs/session.js.map +1 -1
- package/lib/module/blocks/receipts.js +57 -1
- package/lib/module/blocks/receipts.js.map +1 -1
- package/lib/module/blocks/types.js +23 -2
- package/lib/module/blocks/types.js.map +1 -1
- package/lib/module/blocks/uiTool.js +421 -10
- package/lib/module/blocks/uiTool.js.map +1 -1
- package/lib/module/catalog/catalog.g.js +213 -48
- package/lib/module/catalog/catalog.g.js.map +1 -1
- package/lib/module/catalog/catalog.source.json +226 -48
- package/lib/module/catalog/catalog.types.g.js +40 -0
- package/lib/module/catalog/catalog.types.g.js.map +1 -0
- package/lib/module/catalog/normalizeParams.js +327 -0
- package/lib/module/catalog/normalizeParams.js.map +1 -0
- package/lib/module/catalog/signature.js +63 -0
- package/lib/module/catalog/signature.js.map +1 -0
- package/lib/module/catalog/snapshotReads.js +13 -6
- package/lib/module/catalog/snapshotReads.js.map +1 -1
- package/lib/module/catalog/toProviderTools.js +44 -6
- package/lib/module/catalog/toProviderTools.js.map +1 -1
- package/lib/module/context/buildContextPack.js +366 -19
- package/lib/module/context/buildContextPack.js.map +1 -1
- package/lib/module/effects/digest.js +29 -0
- package/lib/module/effects/digest.js.map +1 -0
- package/lib/module/effects/ledger.js +98 -2
- package/lib/module/effects/ledger.js.map +1 -1
- package/lib/module/engine/askGate.js +280 -0
- package/lib/module/engine/askGate.js.map +1 -0
- package/lib/module/engine/effectFor.js +83 -1
- package/lib/module/engine/effectFor.js.map +1 -1
- package/lib/module/engine/evidence.js +107 -0
- package/lib/module/engine/evidence.js.map +1 -0
- package/lib/module/engine/historyBudget.js +355 -0
- package/lib/module/engine/historyBudget.js.map +1 -0
- package/lib/module/engine/retrieve.js +210 -0
- package/lib/module/engine/retrieve.js.map +1 -0
- package/lib/module/engine/runAgentTurn.js +1227 -121
- package/lib/module/engine/runAgentTurn.js.map +1 -1
- package/lib/module/engine/systemPrompt.js +101 -9
- package/lib/module/engine/systemPrompt.js.map +1 -1
- package/lib/module/engine/textToolCalls.js +270 -0
- package/lib/module/engine/textToolCalls.js.map +1 -0
- package/lib/module/engine/tokenCalibration.js +75 -0
- package/lib/module/engine/tokenCalibration.js.map +1 -0
- package/lib/module/engine/verify.js +292 -0
- package/lib/module/engine/verify.js.map +1 -0
- package/lib/module/index.js +7 -3
- package/lib/module/index.js.map +1 -1
- package/lib/module/policy/labels.js +19 -3
- package/lib/module/policy/labels.js.map +1 -1
- package/lib/module/policy/policy.js +6 -1
- package/lib/module/policy/policy.js.map +1 -1
- package/lib/module/policy/redact.js +42 -5
- package/lib/module/policy/redact.js.map +1 -1
- package/lib/module/providers/anthropic.js +341 -209
- package/lib/module/providers/anthropic.js.map +1 -1
- package/lib/module/providers/openai.js +238 -104
- package/lib/module/providers/openai.js.map +1 -1
- package/lib/module/providers/problem.js +92 -0
- package/lib/module/providers/problem.js.map +1 -0
- package/lib/module/providers/sse.js +166 -34
- package/lib/module/providers/sse.js.map +1 -1
- package/lib/module/providers/streamTimer.js +118 -0
- package/lib/module/providers/streamTimer.js.map +1 -0
- package/lib/module/providers/transport.js +119 -0
- package/lib/module/providers/transport.js.map +1 -0
- package/lib/module/providers/xhrStream.js +207 -0
- package/lib/module/providers/xhrStream.js.map +1 -0
- package/lib/module/session.js +226 -60
- package/lib/module/session.js.map +1 -1
- package/lib/typescript/blocks/receipts.d.ts +5 -0
- package/lib/typescript/blocks/receipts.d.ts.map +1 -1
- package/lib/typescript/blocks/types.d.ts +23 -2
- package/lib/typescript/blocks/types.d.ts.map +1 -1
- package/lib/typescript/blocks/uiTool.d.ts +11 -2
- package/lib/typescript/blocks/uiTool.d.ts.map +1 -1
- package/lib/typescript/catalog/catalog.g.d.ts +2 -2
- package/lib/typescript/catalog/catalog.g.d.ts.map +1 -1
- package/lib/typescript/catalog/catalog.types.g.d.ts +40 -0
- package/lib/typescript/catalog/catalog.types.g.d.ts.map +1 -0
- package/lib/typescript/catalog/normalizeParams.d.ts +43 -0
- package/lib/typescript/catalog/normalizeParams.d.ts.map +1 -0
- package/lib/typescript/catalog/signature.d.ts +29 -0
- package/lib/typescript/catalog/signature.d.ts.map +1 -0
- package/lib/typescript/catalog/snapshotReads.d.ts.map +1 -1
- package/lib/typescript/catalog/toProviderTools.d.ts +28 -15
- package/lib/typescript/catalog/toProviderTools.d.ts.map +1 -1
- package/lib/typescript/context/buildContextPack.d.ts +24 -2
- package/lib/typescript/context/buildContextPack.d.ts.map +1 -1
- package/lib/typescript/effects/digest.d.ts +10 -0
- package/lib/typescript/effects/digest.d.ts.map +1 -0
- package/lib/typescript/effects/ledger.d.ts +73 -0
- package/lib/typescript/effects/ledger.d.ts.map +1 -1
- package/lib/typescript/engine/askGate.d.ts +29 -0
- package/lib/typescript/engine/askGate.d.ts.map +1 -0
- package/lib/typescript/engine/effectFor.d.ts +6 -1
- package/lib/typescript/engine/effectFor.d.ts.map +1 -1
- package/lib/typescript/engine/evidence.d.ts +67 -0
- package/lib/typescript/engine/evidence.d.ts.map +1 -0
- package/lib/typescript/engine/historyBudget.d.ts +115 -0
- package/lib/typescript/engine/historyBudget.d.ts.map +1 -0
- package/lib/typescript/engine/retrieve.d.ts +32 -0
- package/lib/typescript/engine/retrieve.d.ts.map +1 -0
- package/lib/typescript/engine/runAgentTurn.d.ts +179 -3
- package/lib/typescript/engine/runAgentTurn.d.ts.map +1 -1
- package/lib/typescript/engine/systemPrompt.d.ts +80 -0
- package/lib/typescript/engine/systemPrompt.d.ts.map +1 -1
- package/lib/typescript/engine/textToolCalls.d.ts +58 -0
- package/lib/typescript/engine/textToolCalls.d.ts.map +1 -0
- package/lib/typescript/engine/tokenCalibration.d.ts +46 -0
- package/lib/typescript/engine/tokenCalibration.d.ts.map +1 -0
- package/lib/typescript/engine/verify.d.ts +67 -0
- package/lib/typescript/engine/verify.d.ts.map +1 -0
- package/lib/typescript/index.d.ts +7 -3
- package/lib/typescript/index.d.ts.map +1 -1
- package/lib/typescript/policy/labels.d.ts.map +1 -1
- package/lib/typescript/policy/policy.d.ts.map +1 -1
- package/lib/typescript/policy/redact.d.ts.map +1 -1
- package/lib/typescript/providers/anthropic.d.ts.map +1 -1
- package/lib/typescript/providers/openai.d.ts +0 -16
- package/lib/typescript/providers/openai.d.ts.map +1 -1
- package/lib/typescript/providers/problem.d.ts +50 -0
- package/lib/typescript/providers/problem.d.ts.map +1 -0
- package/lib/typescript/providers/sse.d.ts +86 -2
- package/lib/typescript/providers/sse.d.ts.map +1 -1
- package/lib/typescript/providers/streamTimer.d.ts +59 -0
- package/lib/typescript/providers/streamTimer.d.ts.map +1 -0
- package/lib/typescript/providers/transport.d.ts +48 -0
- package/lib/typescript/providers/transport.d.ts.map +1 -0
- package/lib/typescript/providers/types.d.ts +105 -3
- package/lib/typescript/providers/types.d.ts.map +1 -1
- package/lib/typescript/providers/xhrStream.d.ts +45 -0
- package/lib/typescript/providers/xhrStream.d.ts.map +1 -0
- package/lib/typescript/session.d.ts +55 -4
- package/lib/typescript/session.d.ts.map +1 -1
- package/lib/typescript/types.d.ts +6 -0
- package/lib/typescript/types.d.ts.map +1 -1
- package/package.json +10 -10
- package/LICENSE +0 -58
|
@@ -15,9 +15,25 @@
|
|
|
15
15
|
* is the common shape for an internal LLM proxy. Note Anthropic's own
|
|
16
16
|
* OpenAI-compatibility endpoint is explicitly not production-ready and silently
|
|
17
17
|
* ignores several fields — use the native Anthropic adapter for Anthropic.
|
|
18
|
+
*
|
|
19
|
+
* THE 1024-CHAR TRAP. Azure caps a function description at 1024 chars, so
|
|
20
|
+
* this adapter trims to fit. The catalog renders a tool's summary FIRST and
|
|
21
|
+
* its `Actions:` list LAST, and every tool's full description is longer than
|
|
22
|
+
* the cap — so trimming `t.description` silently removed every action line
|
|
23
|
+
* for 23 of the 24 tools — the 24th kept one line of sixteen — measured
|
|
24
|
+
* against the catalog as it is. A model on this protocol (api.openai.com, Azure,
|
|
25
|
+
* OpenRouter, fal — every row of the model-floor sweep) saw a summary cut
|
|
26
|
+
* mid-sentence and a bare `action` enum: no per-action summary, no
|
|
27
|
+
* [DESTRUCTIVE] / [changes state] tag, no "prefer X over Y" guidance, and a
|
|
28
|
+
* catalog wording change could not move its score. So the description is
|
|
29
|
+
* the SUMMARY only, and the `Actions:` block goes into
|
|
30
|
+
* `parameters.properties.action.description`, which nothing caps.
|
|
18
31
|
*/
|
|
19
32
|
|
|
20
|
-
import {
|
|
33
|
+
import { truncatedMessage, unparseableMessage } from "./sse";
|
|
34
|
+
import { createStreamTimer } from "./streamTimer";
|
|
35
|
+
import { createTransport } from "./transport";
|
|
36
|
+
import { classifyProblem } from "./problem";
|
|
21
37
|
function toWireMessages(system, messages) {
|
|
22
38
|
const out = [{
|
|
23
39
|
role: "system",
|
|
@@ -57,132 +73,250 @@ function toWireMessages(system, messages) {
|
|
|
57
73
|
}
|
|
58
74
|
return out;
|
|
59
75
|
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* The tool's input schema with the `Actions:` block on the `action`
|
|
79
|
+
* property — the uncapped home for the per-action lines (header note).
|
|
80
|
+
*/
|
|
81
|
+
function withActionsBlock(t) {
|
|
82
|
+
const action = t.inputSchema.properties.action;
|
|
83
|
+
return {
|
|
84
|
+
...t.inputSchema,
|
|
85
|
+
properties: {
|
|
86
|
+
...t.inputSchema.properties,
|
|
87
|
+
action: {
|
|
88
|
+
...action,
|
|
89
|
+
description: `Which action to run.\n\n${t.actionsBlock}`
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
};
|
|
93
|
+
}
|
|
60
94
|
export function createOpenAIProvider(config) {
|
|
95
|
+
// One per provider, so the `auto` transport decision is made once and not
|
|
96
|
+
// re-learned (and re-buffered) at the start of every turn.
|
|
97
|
+
const transport = createTransport(config);
|
|
61
98
|
return {
|
|
62
99
|
protocol: "openai",
|
|
63
100
|
async *send(req) {
|
|
64
|
-
const
|
|
65
|
-
|
|
66
|
-
|
|
101
|
+
const timer = createStreamTimer(req.signal, config);
|
|
102
|
+
const status = {
|
|
103
|
+
truncated: false
|
|
67
104
|
};
|
|
68
|
-
|
|
69
|
-
headers
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
method: "POST",
|
|
73
|
-
headers,
|
|
74
|
-
signal: req.signal,
|
|
75
|
-
body: JSON.stringify({
|
|
76
|
-
model: req.model,
|
|
77
|
-
max_tokens: req.maxTokens,
|
|
78
|
-
stream: true,
|
|
79
|
-
stream_options: {
|
|
80
|
-
include_usage: true
|
|
81
|
-
},
|
|
82
|
-
messages: toWireMessages(req.system, req.messages),
|
|
83
|
-
tools: req.tools.map(t => ({
|
|
84
|
-
type: "function",
|
|
85
|
-
function: {
|
|
86
|
-
name: t.name,
|
|
87
|
-
// Azure caps function descriptions at 1024 chars while Anthropic's
|
|
88
|
-
// guidance asks for several sentences. Trim rather than 400.
|
|
89
|
-
description: t.description.slice(0, 1024),
|
|
90
|
-
parameters: t.inputSchema
|
|
91
|
-
}
|
|
92
|
-
})),
|
|
93
|
-
...config.requestOverrides
|
|
94
|
-
})
|
|
95
|
-
});
|
|
96
|
-
if (!response.ok) {
|
|
97
|
-
yield {
|
|
98
|
-
type: "error",
|
|
99
|
-
message: await errorBody(response)
|
|
105
|
+
try {
|
|
106
|
+
const headers = {
|
|
107
|
+
"content-type": "application/json",
|
|
108
|
+
...(await (config.headers?.() ?? {}))
|
|
100
109
|
};
|
|
101
|
-
|
|
102
|
-
|
|
110
|
+
if (config.apiKey && !headers.Authorization && !headers["api-key"]) {
|
|
111
|
+
headers.Authorization = `Bearer ${config.apiKey}`;
|
|
112
|
+
}
|
|
113
|
+
const stream = await transport.open({
|
|
114
|
+
headers,
|
|
115
|
+
// The timer's signal, not the caller's: it aborts on the user's Stop
|
|
116
|
+
// AND on either clock running out. See providers/streamTimer.ts.
|
|
117
|
+
signal: timer.signal,
|
|
118
|
+
body: JSON.stringify({
|
|
119
|
+
model: req.model,
|
|
120
|
+
max_tokens: req.maxTokens,
|
|
121
|
+
stream: true,
|
|
122
|
+
stream_options: {
|
|
123
|
+
include_usage: true
|
|
124
|
+
},
|
|
125
|
+
messages: toWireMessages(req.systemVolatile ? `${req.system}\n\n---\n\n${req.systemVolatile}` : req.system, req.messages),
|
|
126
|
+
tools: req.tools.map(t => ({
|
|
127
|
+
type: "function",
|
|
128
|
+
function: {
|
|
129
|
+
name: t.name,
|
|
130
|
+
// Azure caps function descriptions at 1024 chars while Anthropic's
|
|
131
|
+
// guidance asks for several sentences. Trim rather than 400 — but
|
|
132
|
+
// trim the SUMMARY only: the action lines travel on `parameters`
|
|
133
|
+
// (withActionsBlock), where nothing is capped. See the header.
|
|
134
|
+
description: t.summaryDescription.slice(0, 1024),
|
|
135
|
+
parameters: withActionsBlock(t)
|
|
136
|
+
}
|
|
137
|
+
})),
|
|
138
|
+
...config.requestOverrides
|
|
139
|
+
})
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
// Headers are in: the connect clock stops here and the idle one starts.
|
|
143
|
+
timer.connected();
|
|
144
|
+
if (stream.problem) {
|
|
145
|
+
yield {
|
|
146
|
+
type: "error",
|
|
147
|
+
message: stream.problem,
|
|
148
|
+
kind: "provider",
|
|
149
|
+
problem: stream.problemInfo
|
|
150
|
+
};
|
|
151
|
+
return;
|
|
152
|
+
}
|
|
103
153
|
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
154
|
+
// Keyed by the stream's `index`; id and name arrive only on the first
|
|
155
|
+
// fragment, arguments accumulate across all of them.
|
|
156
|
+
const pending = new Map();
|
|
157
|
+
let stopReason;
|
|
158
|
+
let usage;
|
|
159
|
+
// The served model id, from the first chunk that names one. A gateway
|
|
160
|
+
// resolves `gpt-4.1` to a dated snapshot, and that is the id a cost
|
|
161
|
+
// table or a benchmark has to attribute a drift to.
|
|
162
|
+
let model;
|
|
163
|
+
/** The `[DONE]` sentinel; ends the read. See `sawTerminal` below. */
|
|
164
|
+
let sawDone = false;
|
|
165
|
+
const flush = function* () {
|
|
166
|
+
for (const [, slot] of pending) {
|
|
167
|
+
let input = {};
|
|
168
|
+
try {
|
|
169
|
+
input = slot.args ? JSON.parse(slot.args) : {};
|
|
170
|
+
} catch {
|
|
171
|
+
yield {
|
|
172
|
+
type: "error",
|
|
173
|
+
kind: "provider",
|
|
174
|
+
// Carry the evidence. Without it this error says only THAT the
|
|
175
|
+
// arguments were bad, and diagnosing it needs a live repro of a
|
|
176
|
+
// model that may not be reachable any more — a Sonnet bench run
|
|
177
|
+
// produced 15 of these and left nothing to read. `length` here
|
|
178
|
+
// means the model was cut off mid-argument and `max_tokens` is
|
|
179
|
+
// the fix; anything else is a genuinely malformed stream.
|
|
180
|
+
message: unparseableMessage(slot.name, slot.args, stopReason)
|
|
181
|
+
};
|
|
182
|
+
continue;
|
|
183
|
+
}
|
|
184
|
+
yield {
|
|
185
|
+
type: "tool-call",
|
|
186
|
+
call: {
|
|
187
|
+
id: slot.id,
|
|
188
|
+
name: slot.name,
|
|
189
|
+
input
|
|
190
|
+
}
|
|
191
|
+
};
|
|
192
|
+
}
|
|
193
|
+
pending.clear();
|
|
194
|
+
};
|
|
195
|
+
for await (const ev of stream.frames({
|
|
196
|
+
status,
|
|
197
|
+
onFrame: timer.activity,
|
|
198
|
+
signal: timer.signal
|
|
199
|
+
})) {
|
|
200
|
+
if (ev.data === "[DONE]") {
|
|
201
|
+
sawDone = true;
|
|
202
|
+
break;
|
|
203
|
+
}
|
|
204
|
+
let payload;
|
|
112
205
|
try {
|
|
113
|
-
|
|
206
|
+
payload = JSON.parse(ev.data);
|
|
114
207
|
} catch {
|
|
208
|
+
continue;
|
|
209
|
+
}
|
|
210
|
+
if (payload.error) {
|
|
211
|
+
const err = payload.error;
|
|
212
|
+
const message = err.message ?? "Provider error";
|
|
213
|
+
const type = typeof err.code === "string" ? err.code : typeof err.type === "string" ? err.type : undefined;
|
|
115
214
|
yield {
|
|
116
215
|
type: "error",
|
|
117
|
-
message
|
|
216
|
+
message,
|
|
217
|
+
kind: "provider",
|
|
218
|
+
problem: classifyProblem({
|
|
219
|
+
type,
|
|
220
|
+
message
|
|
221
|
+
})
|
|
118
222
|
};
|
|
119
|
-
|
|
223
|
+
return;
|
|
224
|
+
}
|
|
225
|
+
if (!model && typeof payload.model === "string" && payload.model) model = payload.model;
|
|
226
|
+
const u = payload.usage;
|
|
227
|
+
if (u) {
|
|
228
|
+
// `prompt_tokens_details.cached_tokens` is this protocol's cache
|
|
229
|
+
// counter. Unlike Anthropic's it is INCLUDED in `prompt_tokens`
|
|
230
|
+
// rather than reported beside it; it is still carried as
|
|
231
|
+
// `cacheRead` so a cost table can price that slice at the
|
|
232
|
+
// discounted rate.
|
|
233
|
+
const details = u.prompt_tokens_details;
|
|
234
|
+
const cached = details?.cached_tokens;
|
|
235
|
+
usage = {
|
|
236
|
+
input: typeof u.prompt_tokens === "number" ? u.prompt_tokens : 0,
|
|
237
|
+
output: typeof u.completion_tokens === "number" ? u.completion_tokens : 0,
|
|
238
|
+
...(typeof cached === "number" ? {
|
|
239
|
+
cacheRead: cached
|
|
240
|
+
} : {})
|
|
241
|
+
};
|
|
242
|
+
}
|
|
243
|
+
const choice = payload.choices?.[0];
|
|
244
|
+
if (!choice) continue;
|
|
245
|
+
if (choice.finish_reason) stopReason = choice.finish_reason;
|
|
246
|
+
const delta = choice.delta;
|
|
247
|
+
if (typeof delta?.content === "string" && delta.content) {
|
|
248
|
+
yield {
|
|
249
|
+
type: "text",
|
|
250
|
+
delta: delta.content
|
|
251
|
+
};
|
|
252
|
+
}
|
|
253
|
+
const calls = delta?.tool_calls;
|
|
254
|
+
for (const c of calls ?? []) {
|
|
255
|
+
const index = c.index ?? 0;
|
|
256
|
+
const fn = c.function;
|
|
257
|
+
const slot = pending.get(index) ?? {
|
|
258
|
+
id: "",
|
|
259
|
+
name: "",
|
|
260
|
+
args: ""
|
|
261
|
+
};
|
|
262
|
+
if (c.id) slot.id = c.id;
|
|
263
|
+
if (fn?.name) slot.name = fn.name;
|
|
264
|
+
if (typeof fn?.arguments === "string") slot.args += fn.arguments;
|
|
265
|
+
pending.set(index, slot);
|
|
120
266
|
}
|
|
121
|
-
yield {
|
|
122
|
-
type: "tool-call",
|
|
123
|
-
call: {
|
|
124
|
-
id: slot.id,
|
|
125
|
-
name: slot.name,
|
|
126
|
-
input
|
|
127
|
-
}
|
|
128
|
-
};
|
|
129
|
-
}
|
|
130
|
-
pending.clear();
|
|
131
|
-
};
|
|
132
|
-
for await (const ev of readSse(response)) {
|
|
133
|
-
if (ev.data === "[DONE]") break;
|
|
134
|
-
let payload;
|
|
135
|
-
try {
|
|
136
|
-
payload = JSON.parse(ev.data);
|
|
137
|
-
} catch {
|
|
138
|
-
continue;
|
|
139
267
|
}
|
|
140
|
-
|
|
141
|
-
|
|
268
|
+
|
|
269
|
+
/**
|
|
270
|
+
* Did the provider actually say it was finished?
|
|
271
|
+
*
|
|
272
|
+
* `[DONE]` is the transport sentinel. `finish_reason` counts too, and is
|
|
273
|
+
* the stronger claim: it is the model's own statement of why it stopped,
|
|
274
|
+
* while `[DONE]` only says the SSE stream closed. Plenty of
|
|
275
|
+
* OpenAI-compatible gateways send one and not the other, and refusing
|
|
276
|
+
* either shape would break working endpoints for no safety gain. NEITHER
|
|
277
|
+
* arriving is the case that matters — bare EOF, which used to be read as
|
|
278
|
+
* success, and which is how a complete-looking `storage.setItem` came out
|
|
279
|
+
* of a cut connection and ran.
|
|
280
|
+
*/
|
|
281
|
+
const sawTerminal = sawDone || stopReason !== undefined;
|
|
282
|
+
if (!sawTerminal) {
|
|
283
|
+
// Nothing is flushed: the accumulated arguments may be a whole JSON
|
|
284
|
+
// object and still be half a plan. Text already yielded stays on
|
|
285
|
+
// screen; the engine shows it and offers Continue.
|
|
142
286
|
yield {
|
|
143
287
|
type: "error",
|
|
144
|
-
message:
|
|
288
|
+
message: truncatedMessage(status.truncated),
|
|
289
|
+
kind: "truncated"
|
|
145
290
|
};
|
|
146
291
|
return;
|
|
147
292
|
}
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
if
|
|
158
|
-
|
|
159
|
-
if (
|
|
293
|
+
yield* flush();
|
|
294
|
+
yield {
|
|
295
|
+
type: "done",
|
|
296
|
+
outcome: stopReason === "length" ? "output-limited" : "completed",
|
|
297
|
+
stopReason,
|
|
298
|
+
usage,
|
|
299
|
+
model
|
|
300
|
+
};
|
|
301
|
+
} catch (err) {
|
|
302
|
+
// The user's Stop wins the label even if a clock fired in the same
|
|
303
|
+
// tick: they know why the turn ended, and "timed out" would be a lie.
|
|
304
|
+
if (req.signal?.aborted) throw err;
|
|
305
|
+
const message = timer.message();
|
|
306
|
+
if (message) {
|
|
160
307
|
yield {
|
|
161
|
-
type: "
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
}
|
|
165
|
-
const calls = delta?.tool_calls;
|
|
166
|
-
for (const c of calls ?? []) {
|
|
167
|
-
const index = c.index ?? 0;
|
|
168
|
-
const fn = c.function;
|
|
169
|
-
const slot = pending.get(index) ?? {
|
|
170
|
-
id: "",
|
|
171
|
-
name: "",
|
|
172
|
-
args: ""
|
|
308
|
+
type: "error",
|
|
309
|
+
message,
|
|
310
|
+
kind: "timeout"
|
|
173
311
|
};
|
|
174
|
-
|
|
175
|
-
if (fn?.name) slot.name = fn.name;
|
|
176
|
-
if (typeof fn?.arguments === "string") slot.args += fn.arguments;
|
|
177
|
-
pending.set(index, slot);
|
|
312
|
+
return;
|
|
178
313
|
}
|
|
314
|
+
// A genuine transport failure — DNS, refused connection, TLS. It has
|
|
315
|
+
// always propagated to the failed-turn card, and still does.
|
|
316
|
+
throw err;
|
|
317
|
+
} finally {
|
|
318
|
+
timer.dispose();
|
|
179
319
|
}
|
|
180
|
-
yield* flush();
|
|
181
|
-
yield {
|
|
182
|
-
type: "done",
|
|
183
|
-
stopReason,
|
|
184
|
-
usage
|
|
185
|
-
};
|
|
186
320
|
}
|
|
187
321
|
};
|
|
188
322
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["
|
|
1
|
+
{"version":3,"names":["truncatedMessage","unparseableMessage","createStreamTimer","createTransport","classifyProblem","toWireMessages","system","messages","out","role","content","m","push","text","toolCalls","length","tool_calls","map","c","id","type","function","name","arguments","JSON","stringify","input","r","results","tool_call_id","toolCallId","withActionsBlock","t","action","inputSchema","properties","description","actionsBlock","createOpenAIProvider","config","transport","protocol","send","req","timer","signal","status","truncated","headers","apiKey","Authorization","stream","open","body","model","max_tokens","maxTokens","stream_options","include_usage","systemVolatile","tools","summaryDescription","slice","parameters","requestOverrides","connected","problem","message","kind","problemInfo","pending","Map","stopReason","usage","sawDone","flush","slot","args","parse","call","clear","ev","frames","onFrame","activity","data","payload","error","err","code","undefined","u","details","prompt_tokens_details","cached","cached_tokens","prompt_tokens","output","completion_tokens","cacheRead","choice","choices","finish_reason","delta","calls","index","fn","get","set","sawTerminal","outcome","aborted","dispose"],"sourceRoot":"../../../src","sources":["providers/openai.ts"],"mappings":";;AAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AAUA,SAASA,gBAAgB,EAAEC,kBAAkB,QAA4B,OAAO;AAChF,SAASC,iBAAiB,QAAQ,eAAe;AACjD,SAASC,eAAe,QAAQ,aAAa;AAC7C,SAASC,eAAe,QAAQ,WAAW;AAa3C,SAASC,cAAcA,CAACC,MAAc,EAAEC,QAAwB,EAAiB;EAC/E,MAAMC,GAAkB,GAAG,CAAC;IAAEC,IAAI,EAAE,QAAQ;IAAEC,OAAO,EAAEJ;EAAO,CAAC,CAAC;EAChE,KAAK,MAAMK,CAAC,IAAIJ,QAAQ,EAAE;IACxB,IAAII,CAAC,CAACF,IAAI,KAAK,MAAM,EAAE;MACrBD,GAAG,CAACI,IAAI,CAAC;QAAEH,IAAI,EAAE,MAAM;QAAEC,OAAO,EAAEC,CAAC,CAACE;MAAK,CAAC,CAAC;IAC7C,CAAC,MAAM,IAAIF,CAAC,CAACF,IAAI,KAAK,WAAW,EAAE;MACjCD,GAAG,CAACI,IAAI,CAAC;QACPH,IAAI,EA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|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* What kind of failure a provider reported — decided once, from the status,
|
|
5
|
+
* the error type and the message, so the engine can act on the KIND rather
|
|
6
|
+
* than grep a string.
|
|
7
|
+
*
|
|
8
|
+
* Only two kinds are acted on, and both only when nothing has been generated:
|
|
9
|
+
*
|
|
10
|
+
* - `throttled` — 429 / 529 / 503, a `rate_limit_error` or `overloaded_error`
|
|
11
|
+
* frame, or a body that says so. Waiting and asking again is the right move;
|
|
12
|
+
* the request never reached a model.
|
|
13
|
+
* - `overflow` — the provider refused the request as too big. Trimming older
|
|
14
|
+
* history and asking once more is the right move; the proactive budget is
|
|
15
|
+
* an estimate (chars ÷ 4) and JSON tokenizes worse than prose.
|
|
16
|
+
*
|
|
17
|
+
* Everything else — auth, a bad schema, an unknown model, a mid-answer
|
|
18
|
+
* `api_error` — is reported and NOT retried. The phrase lists are the union
|
|
19
|
+
* observed across provider responses by the Strands SDK (harness-sdk
|
|
20
|
+
* `381ab48`, `models/anthropic.ts` + `models/openai/errors.ts`, Apache-2.0),
|
|
21
|
+
* matched case-insensitively on the lowered message.
|
|
22
|
+
*/
|
|
23
|
+
|
|
24
|
+
const THROTTLE_STATUSES = new Set([429, 503, 529]);
|
|
25
|
+
const AUTH_STATUSES = new Set([401, 403]);
|
|
26
|
+
|
|
27
|
+
/** Provider error `type` values (Anthropic frames, OpenAI `error.type` / `code`). */
|
|
28
|
+
const THROTTLE_TYPES = new Set(["overloaded_error", "rate_limit_error", "rate_limit_exceeded", "insufficient_quota", "server_overloaded"]);
|
|
29
|
+
const AUTH_TYPES = new Set(["authentication_error", "permission_error", "invalid_api_key"]);
|
|
30
|
+
const OVERFLOW_TYPES = new Set(["request_too_large", "context_length_exceeded"]);
|
|
31
|
+
const THROTTLE_PHRASES = ["rate_limit_exceeded", "rate limit", "too many requests", "overloaded", "capacity", "try again later"];
|
|
32
|
+
const OVERFLOW_PHRASES = [
|
|
33
|
+
// Anthropic
|
|
34
|
+
"prompt is too long", "input too long", "input is too long", "input length exceeds context window", "input and output tokens exceed your context limit",
|
|
35
|
+
// OpenAI and compatible gateways
|
|
36
|
+
"maximum context length", "context_length_exceeded", "too many tokens", "context length", "input is too long for requested model", "input length and `max_tokens` exceed context limit", "too many total text bytes", "exceed customer model maximum"];
|
|
37
|
+
export function classifyProblem(input) {
|
|
38
|
+
const lowered = input.message.toLowerCase();
|
|
39
|
+
const type = input.type?.toLowerCase();
|
|
40
|
+
const retryAfterMs = parseRetryAfter(input.retryAfter);
|
|
41
|
+
const withStatus = kind => ({
|
|
42
|
+
kind,
|
|
43
|
+
...(input.status !== undefined ? {
|
|
44
|
+
status: input.status
|
|
45
|
+
} : {}),
|
|
46
|
+
...(retryAfterMs !== undefined ? {
|
|
47
|
+
retryAfterMs
|
|
48
|
+
} : {})
|
|
49
|
+
});
|
|
50
|
+
if (input.status !== undefined && AUTH_STATUSES.has(input.status)) return withStatus("auth");
|
|
51
|
+
if (type && AUTH_TYPES.has(type)) return withStatus("auth");
|
|
52
|
+
if (input.status !== undefined && THROTTLE_STATUSES.has(input.status)) return withStatus("throttled");
|
|
53
|
+
if (type && THROTTLE_TYPES.has(type)) return withStatus("throttled");
|
|
54
|
+
if (type && OVERFLOW_TYPES.has(type)) return withStatus("overflow");
|
|
55
|
+
// Overflow before throttling on phrases: "context length" is more specific
|
|
56
|
+
// than "capacity", and a 400 that says both is about the size.
|
|
57
|
+
if (OVERFLOW_PHRASES.some(p => lowered.includes(p))) return withStatus("overflow");
|
|
58
|
+
if (THROTTLE_PHRASES.some(p => lowered.includes(p))) return withStatus("throttled");
|
|
59
|
+
return withStatus("other");
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* `Retry-After` is either delay-seconds or an HTTP-date. Anything unparseable
|
|
64
|
+
* or in the past is undefined, so the caller falls back to its own backoff.
|
|
65
|
+
*/
|
|
66
|
+
export function parseRetryAfter(value) {
|
|
67
|
+
if (!value) return undefined;
|
|
68
|
+
const trimmed = value.trim();
|
|
69
|
+
if (/^\d+$/.test(trimmed)) return Number(trimmed) * 1000;
|
|
70
|
+
const at = Date.parse(trimmed);
|
|
71
|
+
if (Number.isNaN(at)) return undefined;
|
|
72
|
+
const ms = at - Date.now();
|
|
73
|
+
return ms > 0 ? ms : undefined;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Pull the provider's error `type` out of an error body, for the HTTP path
|
|
78
|
+
* where the body is JSON like `{"error":{"type":"overloaded_error",…}}` or
|
|
79
|
+
* `{"error":{"code":"context_length_exceeded",…}}`. Returns undefined for
|
|
80
|
+
* anything that is not that shape; the message phrases still apply.
|
|
81
|
+
*/
|
|
82
|
+
export function errorTypeOf(body) {
|
|
83
|
+
try {
|
|
84
|
+
const parsed = JSON.parse(body);
|
|
85
|
+
const err = typeof parsed.error === "object" && parsed.error ? parsed.error : undefined;
|
|
86
|
+
const type = err?.type ?? err?.code ?? parsed.type;
|
|
87
|
+
return typeof type === "string" ? type : undefined;
|
|
88
|
+
} catch {
|
|
89
|
+
return undefined;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
//# sourceMappingURL=problem.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["THROTTLE_STATUSES","Set","AUTH_STATUSES","THROTTLE_TYPES","AUTH_TYPES","OVERFLOW_TYPES","THROTTLE_PHRASES","OVERFLOW_PHRASES","classifyProblem","input","lowered","message","toLowerCase","type","retryAfterMs","parseRetryAfter","retryAfter","withStatus","kind","status","undefined","has","some","p","includes","value","trimmed","trim","test","Number","at","Date","parse","isNaN","ms","now","errorTypeOf","body","parsed","JSON","err","error","code"],"sourceRoot":"../../../src","sources":["providers/problem.ts"],"mappings":";;AAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AAYA,MAAMA,iBAAiB,GAAG,IAAIC,GA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