@buoy-gg/agent-core 7.0.33
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/LICENSE +58 -0
- package/README.md +42 -0
- package/lib/commonjs/blocks/receipts.js +702 -0
- package/lib/commonjs/blocks/receipts.js.map +1 -0
- package/lib/commonjs/blocks/types.js +119 -0
- package/lib/commonjs/blocks/types.js.map +1 -0
- package/lib/commonjs/blocks/uiTool.js +994 -0
- package/lib/commonjs/blocks/uiTool.js.map +1 -0
- package/lib/commonjs/catalog/catalog.g.js +4044 -0
- package/lib/commonjs/catalog/catalog.g.js.map +1 -0
- package/lib/commonjs/catalog/catalog.source.json +4956 -0
- package/lib/commonjs/catalog/snapshotReads.js +256 -0
- package/lib/commonjs/catalog/snapshotReads.js.map +1 -0
- package/lib/commonjs/catalog/toProviderTools.js +107 -0
- package/lib/commonjs/catalog/toProviderTools.js.map +1 -0
- package/lib/commonjs/catalog/validateParams.js +129 -0
- package/lib/commonjs/catalog/validateParams.js.map +1 -0
- package/lib/commonjs/context/buildContextPack.js +141 -0
- package/lib/commonjs/context/buildContextPack.js.map +1 -0
- package/lib/commonjs/effects/ledger.js +229 -0
- package/lib/commonjs/effects/ledger.js.map +1 -0
- package/lib/commonjs/engine/effectFor.js +95 -0
- package/lib/commonjs/engine/effectFor.js.map +1 -0
- package/lib/commonjs/engine/runAgentTurn.js +667 -0
- package/lib/commonjs/engine/runAgentTurn.js.map +1 -0
- package/lib/commonjs/engine/systemPrompt.js +86 -0
- package/lib/commonjs/engine/systemPrompt.js.map +1 -0
- package/lib/commonjs/index.js +288 -0
- package/lib/commonjs/index.js.map +1 -0
- package/lib/commonjs/package.json +1 -0
- package/lib/commonjs/policy/labels.js +303 -0
- package/lib/commonjs/policy/labels.js.map +1 -0
- package/lib/commonjs/policy/policy.js +143 -0
- package/lib/commonjs/policy/policy.js.map +1 -0
- package/lib/commonjs/policy/redact.js +135 -0
- package/lib/commonjs/policy/redact.js.map +1 -0
- package/lib/commonjs/providers/anthropic.js +272 -0
- package/lib/commonjs/providers/anthropic.js.map +1 -0
- package/lib/commonjs/providers/openai.js +193 -0
- package/lib/commonjs/providers/openai.js.map +1 -0
- package/lib/commonjs/providers/sse.js +104 -0
- package/lib/commonjs/providers/sse.js.map +1 -0
- package/lib/commonjs/providers/types.js +6 -0
- package/lib/commonjs/providers/types.js.map +1 -0
- package/lib/commonjs/session.js +173 -0
- package/lib/commonjs/session.js.map +1 -0
- package/lib/commonjs/types.js +2 -0
- package/lib/commonjs/types.js.map +1 -0
- package/lib/module/blocks/receipts.js +696 -0
- package/lib/module/blocks/receipts.js.map +1 -0
- package/lib/module/blocks/types.js +113 -0
- package/lib/module/blocks/types.js.map +1 -0
- package/lib/module/blocks/uiTool.js +987 -0
- package/lib/module/blocks/uiTool.js.map +1 -0
- package/lib/module/catalog/catalog.g.js +4040 -0
- package/lib/module/catalog/catalog.g.js.map +1 -0
- package/lib/module/catalog/catalog.source.json +4956 -0
- package/lib/module/catalog/snapshotReads.js +252 -0
- package/lib/module/catalog/snapshotReads.js.map +1 -0
- package/lib/module/catalog/toProviderTools.js +101 -0
- package/lib/module/catalog/toProviderTools.js.map +1 -0
- package/lib/module/catalog/validateParams.js +124 -0
- package/lib/module/catalog/validateParams.js.map +1 -0
- package/lib/module/context/buildContextPack.js +138 -0
- package/lib/module/context/buildContextPack.js.map +1 -0
- package/lib/module/effects/ledger.js +223 -0
- package/lib/module/effects/ledger.js.map +1 -0
- package/lib/module/engine/effectFor.js +91 -0
- package/lib/module/engine/effectFor.js.map +1 -0
- package/lib/module/engine/runAgentTurn.js +663 -0
- package/lib/module/engine/runAgentTurn.js.map +1 -0
- package/lib/module/engine/systemPrompt.js +82 -0
- package/lib/module/engine/systemPrompt.js.map +1 -0
- package/lib/module/index.js +30 -0
- package/lib/module/index.js.map +1 -0
- package/lib/module/policy/labels.js +296 -0
- package/lib/module/policy/labels.js.map +1 -0
- package/lib/module/policy/policy.js +137 -0
- package/lib/module/policy/policy.js.map +1 -0
- package/lib/module/policy/redact.js +128 -0
- package/lib/module/policy/redact.js.map +1 -0
- package/lib/module/providers/anthropic.js +268 -0
- package/lib/module/providers/anthropic.js.map +1 -0
- package/lib/module/providers/openai.js +189 -0
- package/lib/module/providers/openai.js.map +1 -0
- package/lib/module/providers/sse.js +99 -0
- package/lib/module/providers/sse.js.map +1 -0
- package/lib/module/providers/types.js +4 -0
- package/lib/module/providers/types.js.map +1 -0
- package/lib/module/session.js +168 -0
- package/lib/module/session.js.map +1 -0
- package/lib/module/types.js +2 -0
- package/lib/module/types.js.map +1 -0
- package/lib/typescript/blocks/receipts.d.ts +32 -0
- package/lib/typescript/blocks/receipts.d.ts.map +1 -0
- package/lib/typescript/blocks/types.d.ts +576 -0
- package/lib/typescript/blocks/types.d.ts.map +1 -0
- package/lib/typescript/blocks/uiTool.d.ts +15 -0
- package/lib/typescript/blocks/uiTool.d.ts.map +1 -0
- package/lib/typescript/catalog/catalog.g.d.ts +11 -0
- package/lib/typescript/catalog/catalog.g.d.ts.map +1 -0
- package/lib/typescript/catalog/snapshotReads.d.ts +37 -0
- package/lib/typescript/catalog/snapshotReads.d.ts.map +1 -0
- package/lib/typescript/catalog/toProviderTools.d.ts +52 -0
- package/lib/typescript/catalog/toProviderTools.d.ts.map +1 -0
- package/lib/typescript/catalog/validateParams.d.ts +33 -0
- package/lib/typescript/catalog/validateParams.d.ts.map +1 -0
- package/lib/typescript/context/buildContextPack.d.ts +32 -0
- package/lib/typescript/context/buildContextPack.d.ts.map +1 -0
- package/lib/typescript/effects/ledger.d.ts +119 -0
- package/lib/typescript/effects/ledger.d.ts.map +1 -0
- package/lib/typescript/engine/effectFor.d.ts +21 -0
- package/lib/typescript/engine/effectFor.d.ts.map +1 -0
- package/lib/typescript/engine/runAgentTurn.d.ts +140 -0
- package/lib/typescript/engine/runAgentTurn.d.ts.map +1 -0
- package/lib/typescript/engine/systemPrompt.d.ts +49 -0
- package/lib/typescript/engine/systemPrompt.d.ts.map +1 -0
- package/lib/typescript/index.d.ts +29 -0
- package/lib/typescript/index.d.ts.map +1 -0
- package/lib/typescript/policy/labels.d.ts +10 -0
- package/lib/typescript/policy/labels.d.ts.map +1 -0
- package/lib/typescript/policy/policy.d.ts +106 -0
- package/lib/typescript/policy/policy.d.ts.map +1 -0
- package/lib/typescript/policy/redact.d.ts +37 -0
- package/lib/typescript/policy/redact.d.ts.map +1 -0
- package/lib/typescript/providers/anthropic.d.ts +22 -0
- package/lib/typescript/providers/anthropic.d.ts.map +1 -0
- package/lib/typescript/providers/openai.d.ts +19 -0
- package/lib/typescript/providers/openai.d.ts.map +1 -0
- package/lib/typescript/providers/sse.d.ts +18 -0
- package/lib/typescript/providers/sse.d.ts.map +1 -0
- package/lib/typescript/providers/types.d.ts +129 -0
- package/lib/typescript/providers/types.d.ts.map +1 -0
- package/lib/typescript/session.d.ts +58 -0
- package/lib/typescript/session.d.ts.map +1 -0
- package/lib/typescript/types.d.ts +119 -0
- package/lib/typescript/types.d.ts.map +1 -0
- package/package.json +58 -0
|
@@ -0,0 +1,272 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.createAnthropicProvider = createAnthropicProvider;
|
|
7
|
+
var _sse = require("./sse");
|
|
8
|
+
/**
|
|
9
|
+
* Anthropic Messages protocol.
|
|
10
|
+
*
|
|
11
|
+
* Four things here are not obvious and each one is a silent failure if missed:
|
|
12
|
+
*
|
|
13
|
+
* 1. `content_block_start` for a `tool_use` carries `"input": {}` — an EMPTY
|
|
14
|
+
* object, never the real arguments. The arguments only exist after
|
|
15
|
+
* concatenating every `input_json_delta.partial_json` and parsing at
|
|
16
|
+
* `content_block_stop`. Reading `.input` at start yields a tool call with no
|
|
17
|
+
* arguments and no error.
|
|
18
|
+
* 2. There is NO `data: [DONE]` sentinel (removed in the 2023-06-01 version).
|
|
19
|
+
* The stream ends at `message_stop`.
|
|
20
|
+
* 3. `error` events arrive MID-STREAM on an already-committed HTTP 200 —
|
|
21
|
+
* `overloaded_error` being the common one. A 200 is not success.
|
|
22
|
+
* 4. Ordering is 400-level strict: `tool_result` blocks must come FIRST in the
|
|
23
|
+
* user content array with any text after, and that user message must
|
|
24
|
+
* immediately follow the assistant turn. "Here are the results:" placed
|
|
25
|
+
* before the blocks is an instant 400.
|
|
26
|
+
*/
|
|
27
|
+
|
|
28
|
+
const DEFAULT_VERSION = "2023-06-01";
|
|
29
|
+
function toWireMessages(messages) {
|
|
30
|
+
const out = [];
|
|
31
|
+
for (const m of messages) {
|
|
32
|
+
if (m.role === "user") {
|
|
33
|
+
out.push({
|
|
34
|
+
role: "user",
|
|
35
|
+
content: [{
|
|
36
|
+
type: "text",
|
|
37
|
+
text: m.text
|
|
38
|
+
}]
|
|
39
|
+
});
|
|
40
|
+
} else if (m.role === "assistant") {
|
|
41
|
+
const content = [];
|
|
42
|
+
if (m.text) content.push({
|
|
43
|
+
type: "text",
|
|
44
|
+
text: m.text
|
|
45
|
+
});
|
|
46
|
+
for (const c of m.toolCalls ?? []) {
|
|
47
|
+
content.push({
|
|
48
|
+
type: "tool_use",
|
|
49
|
+
id: c.id,
|
|
50
|
+
name: c.name,
|
|
51
|
+
input: c.input
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
if (content.length) out.push({
|
|
55
|
+
role: "assistant",
|
|
56
|
+
content
|
|
57
|
+
});
|
|
58
|
+
} else {
|
|
59
|
+
// tool_result blocks FIRST — see note 4. No preamble text, ever.
|
|
60
|
+
out.push({
|
|
61
|
+
role: "user",
|
|
62
|
+
content: m.results.map(r => ({
|
|
63
|
+
type: "tool_result",
|
|
64
|
+
tool_use_id: r.toolCallId,
|
|
65
|
+
content: r.content,
|
|
66
|
+
...(r.isError ? {
|
|
67
|
+
is_error: true
|
|
68
|
+
} : {})
|
|
69
|
+
}))
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
return out;
|
|
74
|
+
}
|
|
75
|
+
function createAnthropicProvider(config) {
|
|
76
|
+
return {
|
|
77
|
+
protocol: "anthropic",
|
|
78
|
+
async *send(req) {
|
|
79
|
+
const headers = {
|
|
80
|
+
"content-type": "application/json",
|
|
81
|
+
"anthropic-version": config.anthropicVersion ?? DEFAULT_VERSION,
|
|
82
|
+
...(await (config.headers?.() ?? {}))
|
|
83
|
+
};
|
|
84
|
+
// Dev-only direct mode. A distributed build uses `headers` against the
|
|
85
|
+
// org's own gateway and never carries a provider key at all.
|
|
86
|
+
if (config.apiKey && !headers["x-api-key"] && !headers.Authorization) {
|
|
87
|
+
headers["x-api-key"] = config.apiKey;
|
|
88
|
+
// Without this the POST comes back with no access-control-allow-origin
|
|
89
|
+
// and a browser/Electron renderer silently discards an otherwise fine
|
|
90
|
+
// response — a 200 in the network log and "Failed to fetch" in JS.
|
|
91
|
+
headers["anthropic-dangerous-direct-browser-access"] = "true";
|
|
92
|
+
}
|
|
93
|
+
const response = await fetch(config.endpoint, {
|
|
94
|
+
method: "POST",
|
|
95
|
+
headers,
|
|
96
|
+
signal: req.signal,
|
|
97
|
+
body: JSON.stringify({
|
|
98
|
+
model: req.model,
|
|
99
|
+
max_tokens: req.maxTokens,
|
|
100
|
+
stream: true,
|
|
101
|
+
// ONE cache breakpoint, at the end of `system`.
|
|
102
|
+
//
|
|
103
|
+
// Anthropic caches the prefix in a fixed order — tools, then system,
|
|
104
|
+
// then messages — up to and including the block carrying
|
|
105
|
+
// `cache_control`. So marking the end of the system prompt caches
|
|
106
|
+
// the tool definitions with it, which is the point: the tools are
|
|
107
|
+
// ~20k tokens and by far the larger half. A second breakpoint on the
|
|
108
|
+
// last tool would buy nothing and spend one of the four available.
|
|
109
|
+
//
|
|
110
|
+
// Messages stay uncached on purpose. They grow every turn, so they
|
|
111
|
+
// would invalidate on each request and pay the write multiplier for
|
|
112
|
+
// a prefix that is never read back.
|
|
113
|
+
system: config.cache === false ? req.system : [{
|
|
114
|
+
type: "text",
|
|
115
|
+
text: req.system,
|
|
116
|
+
cache_control: {
|
|
117
|
+
type: "ephemeral"
|
|
118
|
+
}
|
|
119
|
+
}],
|
|
120
|
+
tools: req.tools.map(t => ({
|
|
121
|
+
name: t.name,
|
|
122
|
+
description: t.description,
|
|
123
|
+
input_schema: t.inputSchema
|
|
124
|
+
})),
|
|
125
|
+
messages: toWireMessages(req.messages),
|
|
126
|
+
...config.requestOverrides
|
|
127
|
+
})
|
|
128
|
+
});
|
|
129
|
+
if (!response.ok) {
|
|
130
|
+
yield {
|
|
131
|
+
type: "error",
|
|
132
|
+
message: await (0, _sse.errorBody)(response)
|
|
133
|
+
};
|
|
134
|
+
return;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
// Partial tool_use args accumulate here, keyed by content-block index.
|
|
138
|
+
const pending = new Map();
|
|
139
|
+
let stopReason;
|
|
140
|
+
let usage;
|
|
141
|
+
for await (const ev of (0, _sse.readSse)(response)) {
|
|
142
|
+
let payload;
|
|
143
|
+
try {
|
|
144
|
+
payload = JSON.parse(ev.data);
|
|
145
|
+
} catch {
|
|
146
|
+
continue;
|
|
147
|
+
}
|
|
148
|
+
const kind = payload.type ?? ev.event;
|
|
149
|
+
switch (kind) {
|
|
150
|
+
case "content_block_start":
|
|
151
|
+
{
|
|
152
|
+
const block = payload.content_block;
|
|
153
|
+
if (block?.type === "tool_use") {
|
|
154
|
+
// NOT block.input — see note 1.
|
|
155
|
+
pending.set(payload.index, {
|
|
156
|
+
id: block.id,
|
|
157
|
+
name: block.name,
|
|
158
|
+
json: ""
|
|
159
|
+
});
|
|
160
|
+
}
|
|
161
|
+
break;
|
|
162
|
+
}
|
|
163
|
+
case "content_block_delta":
|
|
164
|
+
{
|
|
165
|
+
const delta = payload.delta;
|
|
166
|
+
if (delta?.type === "text_delta") {
|
|
167
|
+
yield {
|
|
168
|
+
type: "text",
|
|
169
|
+
delta: delta.text
|
|
170
|
+
};
|
|
171
|
+
} else if (delta?.type === "input_json_delta") {
|
|
172
|
+
const slot = pending.get(payload.index);
|
|
173
|
+
if (slot) slot.json += delta.partial_json ?? "";
|
|
174
|
+
}
|
|
175
|
+
break;
|
|
176
|
+
}
|
|
177
|
+
case "content_block_stop":
|
|
178
|
+
{
|
|
179
|
+
const slot = pending.get(payload.index);
|
|
180
|
+
if (slot) {
|
|
181
|
+
pending.delete(payload.index);
|
|
182
|
+
let input = {};
|
|
183
|
+
try {
|
|
184
|
+
input = slot.json ? JSON.parse(slot.json) : {};
|
|
185
|
+
} catch {
|
|
186
|
+
yield {
|
|
187
|
+
type: "error",
|
|
188
|
+
message: `Model sent unparseable arguments for ${slot.name}`
|
|
189
|
+
};
|
|
190
|
+
break;
|
|
191
|
+
}
|
|
192
|
+
const call = {
|
|
193
|
+
id: slot.id,
|
|
194
|
+
name: slot.name,
|
|
195
|
+
input
|
|
196
|
+
};
|
|
197
|
+
yield {
|
|
198
|
+
type: "tool-call",
|
|
199
|
+
call
|
|
200
|
+
};
|
|
201
|
+
}
|
|
202
|
+
break;
|
|
203
|
+
}
|
|
204
|
+
case "message_start":
|
|
205
|
+
{
|
|
206
|
+
// The cache counters arrive HERE, not on message_delta — the
|
|
207
|
+
// prefix is accounted for before a single token is generated. A
|
|
208
|
+
// reader that only watches message_delta sees cacheRead 0 on
|
|
209
|
+
// every request and concludes caching is not working.
|
|
210
|
+
const m = payload.message;
|
|
211
|
+
const u = m?.usage;
|
|
212
|
+
if (u) {
|
|
213
|
+
usage = {
|
|
214
|
+
input: u.input_tokens ?? 0,
|
|
215
|
+
output: u.output_tokens ?? 0,
|
|
216
|
+
...(u.cache_read_input_tokens !== undefined ? {
|
|
217
|
+
cacheRead: u.cache_read_input_tokens
|
|
218
|
+
} : {}),
|
|
219
|
+
...(u.cache_creation_input_tokens !== undefined ? {
|
|
220
|
+
cacheWrite: u.cache_creation_input_tokens
|
|
221
|
+
} : {})
|
|
222
|
+
};
|
|
223
|
+
}
|
|
224
|
+
break;
|
|
225
|
+
}
|
|
226
|
+
case "message_delta":
|
|
227
|
+
{
|
|
228
|
+
const delta = payload.delta;
|
|
229
|
+
if (delta?.stop_reason) stopReason = delta.stop_reason;
|
|
230
|
+
// Cumulative, not incremental — assign, never add. Output tokens
|
|
231
|
+
// are only final here, so keep whatever message_start reported
|
|
232
|
+
// about the cache rather than replacing the whole record.
|
|
233
|
+
const u = payload.usage;
|
|
234
|
+
if (u) {
|
|
235
|
+
usage = {
|
|
236
|
+
...usage,
|
|
237
|
+
input: u.input_tokens ?? usage?.input ?? 0,
|
|
238
|
+
output: u.output_tokens ?? 0
|
|
239
|
+
};
|
|
240
|
+
}
|
|
241
|
+
break;
|
|
242
|
+
}
|
|
243
|
+
case "error":
|
|
244
|
+
{
|
|
245
|
+
const err = payload.error;
|
|
246
|
+
yield {
|
|
247
|
+
type: "error",
|
|
248
|
+
message: err?.message ?? "Provider error"
|
|
249
|
+
};
|
|
250
|
+
return;
|
|
251
|
+
}
|
|
252
|
+
case "message_stop":
|
|
253
|
+
// The end. There is no [DONE] — see note 2.
|
|
254
|
+
yield {
|
|
255
|
+
type: "done",
|
|
256
|
+
stopReason,
|
|
257
|
+
usage
|
|
258
|
+
};
|
|
259
|
+
return;
|
|
260
|
+
default:
|
|
261
|
+
break;
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
yield {
|
|
265
|
+
type: "done",
|
|
266
|
+
stopReason,
|
|
267
|
+
usage
|
|
268
|
+
};
|
|
269
|
+
}
|
|
270
|
+
};
|
|
271
|
+
}
|
|
272
|
+
//# sourceMappingURL=anthropic.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["_sse","require","DEFAULT_VERSION","toWireMessages","messages","out","m","role","push","content","type","text","c","toolCalls","id","name","input","length","results","map","r","tool_use_id","toolCallId","isError","is_error","createAnthropicProvider","config","protocol","send","req","headers","anthropicVersion","apiKey","Authorization","response","fetch","endpoint","method","signal","body","JSON","stringify","model","max_tokens","maxTokens","stream","system","cache","cache_control","tools","t","description","input_schema","inputSchema","requestOverrides","ok","message","errorBody","pending","Map","stopReason","usage","ev","readSse","payload","parse","data","kind","event","block","content_block","set","index","json","delta","slot","get","partial_json","delete","call","u","input_tokens","output","output_tokens","cache_read_input_tokens","undefined","cacheRead","cache_creation_input_tokens","cacheWrite","stop_reason","err","error"],"sourceRoot":"../../../src","sources":["providers/anthropic.ts"],"mappings":";;;;;;AA4BA,IAAAA,IAAA,GAAAC,OAAA;AA5BA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AAYA,MAAMC,eAAe,GAAG,YAAY;AAOpC,SAASC,cAAcA,CAACC,QAAwB,EAAE;EAChD,MAAMC,GAAuD,GAAG,EAAE;EAClE,KAAK,MAAMC,CAAC,IAAIF,QAAQ,EAAE;IACxB,IAAIE,CAAC,CAACC,IAAI,KAAK,MAAM,EAAE;MACrBF,GAAG,CAACG,IAAI,CAAC;QAAED,IAAI,EAAE,MAAM;QAAEE,OAAO,EAAE,CAAC;UAAEC,IAAI,EAAE,MAAM;UAAEC,IAAI,EAAEL,CAAC,CAACK;QAAK,CAAC;MAAE,CAAC,CAAC;IACvE,CAAC,MAAM,IAAIL,CAAC,CAACC,IAAI,KAAK,WAAW,EAAE;MACjC,MAAME,OAAgB,GAAG,EAAE;MAC3B,IAAIH,CAAC,CAACK,IAAI,EAAEF,OAAO,CAACD,IAAI,CAAC;QAAEE,IAAI,EAAE,MAAM;QAAEC,IAAI,EAAEL,CAAC,CAACK;MAAK,CAAC,CAAC;MACxD,KAAK,MAAMC,CAAC,IAAIN,CAAC,CAACO,SAAS,IAAI,EAAE,EAAE;QACjCJ,OAAO,CAACD,IAAI,CAAC;UAAEE,IAAI,EAAE,UAAU;UAAEI,EAAE,EAAEF,CAAC,CAACE,EAAE;UAAEC,IAAI,EAAEH,CAAC,CAACG,IAAI;UAAEC,KAAK,EAAEJ,CAAC,CAACI;QAAM,CAAC,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,EAAEgE,CAAC,CAACC,YAAY,IAAIpB,KAAK,EAAE7C,KAAK,IAAI,CAAC;kBAC1CkE,MAAM,EAAEF,CAAC,CAACG,aAAa,IAAI;gBAC7B,CAAC;cACH;cACA;YACF;UACA,KAAK,OAAO;YAAE;cACZ,MAAMO,GAAG,GAAG1B,OAAO,CAAC2B,KAA4C;cAChE,MAAM;gBACJjF,IAAI,EAAE,OAAO;gBACb8C,OAAO,EAAGkC,GAAG,EAAElC,OAAO,IAAe;cACvC,CAAC;cACD;YACF;UACA,KAAK,cAAc;YACjB;YACA,MAAM;cAAE9C,IAAI,EAAE,MAAM;cAAEkD,UAAU;cAAEC;YAAM,CAAC;YACzC;UACF;YACE;QACJ;MACF;MAEA,MAAM;QAAEnD,IAAI,EAAE,MAAM;QAAEkD,UAAU;QAAEC;MAAM,CAAC;IAC3C;EACF,CAAC;AACH","ignoreList":[]}
|
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.createOpenAIProvider = createOpenAIProvider;
|
|
7
|
+
var _sse = require("./sse");
|
|
8
|
+
/**
|
|
9
|
+
* OpenAI chat-completions protocol.
|
|
10
|
+
*
|
|
11
|
+
* The differences from Anthropic that actually matter to the code:
|
|
12
|
+
* - Tool results are their own `{role:"tool", tool_call_id, content}` MESSAGE,
|
|
13
|
+
* not blocks inside a user message.
|
|
14
|
+
* - Tool arguments arrive as a JSON STRING, streamed in fragments, and are
|
|
15
|
+
* keyed by an `index` that is stable across deltas while `id`/`name` only
|
|
16
|
+
* appear on the first fragment.
|
|
17
|
+
* - The stream DOES have a `data: [DONE]` sentinel, unlike Anthropic's.
|
|
18
|
+
*
|
|
19
|
+
* This adapter also serves Azure and any OpenAI-compatible org gateway, which
|
|
20
|
+
* is the common shape for an internal LLM proxy. Note Anthropic's own
|
|
21
|
+
* OpenAI-compatibility endpoint is explicitly not production-ready and silently
|
|
22
|
+
* ignores several fields — use the native Anthropic adapter for Anthropic.
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
function toWireMessages(system, messages) {
|
|
26
|
+
const out = [{
|
|
27
|
+
role: "system",
|
|
28
|
+
content: system
|
|
29
|
+
}];
|
|
30
|
+
for (const m of messages) {
|
|
31
|
+
if (m.role === "user") {
|
|
32
|
+
out.push({
|
|
33
|
+
role: "user",
|
|
34
|
+
content: m.text
|
|
35
|
+
});
|
|
36
|
+
} else if (m.role === "assistant") {
|
|
37
|
+
out.push({
|
|
38
|
+
role: "assistant",
|
|
39
|
+
content: m.text || null,
|
|
40
|
+
...(m.toolCalls?.length ? {
|
|
41
|
+
tool_calls: m.toolCalls.map(c => ({
|
|
42
|
+
id: c.id,
|
|
43
|
+
type: "function",
|
|
44
|
+
function: {
|
|
45
|
+
name: c.name,
|
|
46
|
+
arguments: JSON.stringify(c.input)
|
|
47
|
+
}
|
|
48
|
+
}))
|
|
49
|
+
} : {})
|
|
50
|
+
});
|
|
51
|
+
} else {
|
|
52
|
+
// One message per result, not one message with many.
|
|
53
|
+
for (const r of m.results) {
|
|
54
|
+
out.push({
|
|
55
|
+
role: "tool",
|
|
56
|
+
tool_call_id: r.toolCallId,
|
|
57
|
+
content: r.content
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
return out;
|
|
63
|
+
}
|
|
64
|
+
function createOpenAIProvider(config) {
|
|
65
|
+
return {
|
|
66
|
+
protocol: "openai",
|
|
67
|
+
async *send(req) {
|
|
68
|
+
const headers = {
|
|
69
|
+
"content-type": "application/json",
|
|
70
|
+
...(await (config.headers?.() ?? {}))
|
|
71
|
+
};
|
|
72
|
+
if (config.apiKey && !headers.Authorization && !headers["api-key"]) {
|
|
73
|
+
headers.Authorization = `Bearer ${config.apiKey}`;
|
|
74
|
+
}
|
|
75
|
+
const response = await fetch(config.endpoint, {
|
|
76
|
+
method: "POST",
|
|
77
|
+
headers,
|
|
78
|
+
signal: req.signal,
|
|
79
|
+
body: JSON.stringify({
|
|
80
|
+
model: req.model,
|
|
81
|
+
max_tokens: req.maxTokens,
|
|
82
|
+
stream: true,
|
|
83
|
+
stream_options: {
|
|
84
|
+
include_usage: true
|
|
85
|
+
},
|
|
86
|
+
messages: toWireMessages(req.system, req.messages),
|
|
87
|
+
tools: req.tools.map(t => ({
|
|
88
|
+
type: "function",
|
|
89
|
+
function: {
|
|
90
|
+
name: t.name,
|
|
91
|
+
// Azure caps function descriptions at 1024 chars while Anthropic's
|
|
92
|
+
// guidance asks for several sentences. Trim rather than 400.
|
|
93
|
+
description: t.description.slice(0, 1024),
|
|
94
|
+
parameters: t.inputSchema
|
|
95
|
+
}
|
|
96
|
+
})),
|
|
97
|
+
...config.requestOverrides
|
|
98
|
+
})
|
|
99
|
+
});
|
|
100
|
+
if (!response.ok) {
|
|
101
|
+
yield {
|
|
102
|
+
type: "error",
|
|
103
|
+
message: await (0, _sse.errorBody)(response)
|
|
104
|
+
};
|
|
105
|
+
return;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
// Keyed by the stream's `index`; id and name arrive only on the first
|
|
109
|
+
// fragment, arguments accumulate across all of them.
|
|
110
|
+
const pending = new Map();
|
|
111
|
+
let stopReason;
|
|
112
|
+
let usage;
|
|
113
|
+
const flush = function* () {
|
|
114
|
+
for (const [, slot] of pending) {
|
|
115
|
+
let input = {};
|
|
116
|
+
try {
|
|
117
|
+
input = slot.args ? JSON.parse(slot.args) : {};
|
|
118
|
+
} catch {
|
|
119
|
+
yield {
|
|
120
|
+
type: "error",
|
|
121
|
+
message: `Model sent unparseable arguments for ${slot.name}`
|
|
122
|
+
};
|
|
123
|
+
continue;
|
|
124
|
+
}
|
|
125
|
+
yield {
|
|
126
|
+
type: "tool-call",
|
|
127
|
+
call: {
|
|
128
|
+
id: slot.id,
|
|
129
|
+
name: slot.name,
|
|
130
|
+
input
|
|
131
|
+
}
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
pending.clear();
|
|
135
|
+
};
|
|
136
|
+
for await (const ev of (0, _sse.readSse)(response)) {
|
|
137
|
+
if (ev.data === "[DONE]") break;
|
|
138
|
+
let payload;
|
|
139
|
+
try {
|
|
140
|
+
payload = JSON.parse(ev.data);
|
|
141
|
+
} catch {
|
|
142
|
+
continue;
|
|
143
|
+
}
|
|
144
|
+
if (payload.error) {
|
|
145
|
+
const err = payload.error;
|
|
146
|
+
yield {
|
|
147
|
+
type: "error",
|
|
148
|
+
message: err.message ?? "Provider error"
|
|
149
|
+
};
|
|
150
|
+
return;
|
|
151
|
+
}
|
|
152
|
+
const u = payload.usage;
|
|
153
|
+
if (u) {
|
|
154
|
+
usage = {
|
|
155
|
+
input: u.prompt_tokens ?? 0,
|
|
156
|
+
output: u.completion_tokens ?? 0
|
|
157
|
+
};
|
|
158
|
+
}
|
|
159
|
+
const choice = payload.choices?.[0];
|
|
160
|
+
if (!choice) continue;
|
|
161
|
+
if (choice.finish_reason) stopReason = choice.finish_reason;
|
|
162
|
+
const delta = choice.delta;
|
|
163
|
+
if (typeof delta?.content === "string" && delta.content) {
|
|
164
|
+
yield {
|
|
165
|
+
type: "text",
|
|
166
|
+
delta: delta.content
|
|
167
|
+
};
|
|
168
|
+
}
|
|
169
|
+
const calls = delta?.tool_calls;
|
|
170
|
+
for (const c of calls ?? []) {
|
|
171
|
+
const index = c.index ?? 0;
|
|
172
|
+
const fn = c.function;
|
|
173
|
+
const slot = pending.get(index) ?? {
|
|
174
|
+
id: "",
|
|
175
|
+
name: "",
|
|
176
|
+
args: ""
|
|
177
|
+
};
|
|
178
|
+
if (c.id) slot.id = c.id;
|
|
179
|
+
if (fn?.name) slot.name = fn.name;
|
|
180
|
+
if (typeof fn?.arguments === "string") slot.args += fn.arguments;
|
|
181
|
+
pending.set(index, slot);
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
yield* flush();
|
|
185
|
+
yield {
|
|
186
|
+
type: "done",
|
|
187
|
+
stopReason,
|
|
188
|
+
usage
|
|
189
|
+
};
|
|
190
|
+
}
|
|
191
|
+
};
|
|
192
|
+
}
|
|
193
|
+
//# sourceMappingURL=openai.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["_sse","require","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","createOpenAIProvider","config","protocol","send","req","headers","apiKey","Authorization","response","fetch","endpoint","method","signal","body","model","max_tokens","maxTokens","stream","stream_options","include_usage","tools","t","description","slice","parameters","inputSchema","requestOverrides","ok","message","errorBody","pending","Map","stopReason","usage","flush","slot","args","parse","call","clear","ev","readSse","data","payload","error","err","u","prompt_tokens","output","completion_tokens","choice","choices","finish_reason","delta","calls","index","fn","get","set"],"sourceRoot":"../../../src","sources":["providers/openai.ts"],"mappings":";;;;;;AAuBA,IAAAA,IAAA,GAAAC,OAAA;AAvBA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AAqBA,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,EAAE,WAAW;QACjBC,OAAO,EAAEC,CAAC,CAACE,IAAI,IAAI,IAAI;QACvB,IAAIF,C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,KAAK,QAAQ,IAAI0E,KAAK,CAAC1E,OAAO,EAAE;UACvD,MAAM;YAAEU,IAAI,EAAE,MAAM;YAAEgE,KAAK,EAAEA,KAAK,CAAC1E;UAAQ,CAAC;QAC9C;QAEA,MAAM2E,KAAK,GAAGD,KAAK,EAAEpE,UAAmD;QACxE,KAAK,MAAME,CAAC,IAAImE,KAAK,IAAI,EAAE,EAAE;UAC3B,MAAMC,KAAK,GAAIpE,CAAC,CAACoE,KAAK,IAAe,CAAC;UACtC,MAAMC,EAAE,GAAGrE,CAAC,CAACG,QAA+C;UAC5D,MAAM6C,IAAI,GAAGL,OAAO,CAAC2B,GAAG,CAACF,KAAK,CAAC,IAAI;YAAEnE,EAAE,EAAE,EAAE;YAAEG,IAAI,EAAE,EAAE;YAAE6C,IAAI,EAAE;UAAG,CAAC;UACjE,IAAIjD,CAAC,CAACC,EAAE,EAAE+C,IAAI,CAAC/C,EAAE,GAAGD,CAAC,CAACC,EAAY;UAClC,IAAIoE,EAAE,EAAEjE,IAAI,EAAE4C,IAAI,CAAC5C,IAAI,GAAGiE,EAAE,CAACjE,IAAc;UAC3C,IAAI,OAAOiE,EAAE,EAAEhE,SAAS,KAAK,QAAQ,EAAE2C,IAAI,CAACC,IAAI,IAAIoB,EAAE,CAAChE,SAAS;UAChEsC,OAAO,CAAC4B,GAAG,CAACH,KAAK,EAAEpB,IAAI,CAAC;QAC1B;MACF;MAEA,OAAOD,KAAK,CAAC,CAAC;MACd,MAAM;QAAE7C,IAAI,EAAE,MAAM;QAAE2C,UAAU;QAAEC;MAAM,CAAC;IAC3C;EACF,CAAC;AACH","ignoreList":[]}
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.errorBody = errorBody;
|
|
7
|
+
exports.readSse = readSse;
|
|
8
|
+
/**
|
|
9
|
+
* SSE reading, with the React Native reality baked in.
|
|
10
|
+
*
|
|
11
|
+
* RN's built-in `fetch` does not implement `response.body`. Under Expo you get
|
|
12
|
+
* a real `ReadableStream` because `expo/fetch` is the global — but
|
|
13
|
+
* `EXPO_PUBLIC_USE_RN_FETCH=1` reverts that, and the bare-RN, NativeScript and
|
|
14
|
+
* TV-CLI lanes never had it. So `readSse` degrades to reading the whole body
|
|
15
|
+
* and replaying it as events rather than failing: a non-streaming answer is a
|
|
16
|
+
* slower chat, while a hard failure is no chat at all.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
function* parseChunk(buffer) {
|
|
20
|
+
let rest = buffer;
|
|
21
|
+
let idx;
|
|
22
|
+
// Frames are separated by a blank line. \r\n\r\n is legal too.
|
|
23
|
+
while ((idx = rest.search(/\r?\n\r?\n/)) !== -1) {
|
|
24
|
+
const raw = rest.slice(0, idx);
|
|
25
|
+
rest = rest.slice(idx + (rest[idx] === "\r" ? 4 : 2));
|
|
26
|
+
let event;
|
|
27
|
+
const dataLines = [];
|
|
28
|
+
for (const line of raw.split(/\r?\n/)) {
|
|
29
|
+
if (line.startsWith(":")) continue; // comment / keepalive
|
|
30
|
+
if (line.startsWith("event:")) event = line.slice(6).trim();else if (line.startsWith("data:")) dataLines.push(line.slice(5).replace(/^ /, ""));
|
|
31
|
+
}
|
|
32
|
+
if (dataLines.length) yield {
|
|
33
|
+
event,
|
|
34
|
+
data: dataLines.join("\n")
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
return rest;
|
|
38
|
+
}
|
|
39
|
+
async function* readSse(response) {
|
|
40
|
+
const body = response.body;
|
|
41
|
+
if (!body || typeof body.getReader !== "function") {
|
|
42
|
+
// No streaming on this platform — read it all, then replay.
|
|
43
|
+
const text = await response.text();
|
|
44
|
+
let rest = "";
|
|
45
|
+
const gen = parseChunk(text + "\n\n");
|
|
46
|
+
let next = gen.next();
|
|
47
|
+
while (!next.done) {
|
|
48
|
+
yield next.value;
|
|
49
|
+
next = gen.next();
|
|
50
|
+
}
|
|
51
|
+
rest = next.value;
|
|
52
|
+
if (rest.trim()) {
|
|
53
|
+
const tail = parseChunk(rest + "\n\n");
|
|
54
|
+
let t = tail.next();
|
|
55
|
+
while (!t.done) {
|
|
56
|
+
yield t.value;
|
|
57
|
+
t = tail.next();
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
const reader = body.getReader();
|
|
63
|
+
const decoder = new TextDecoder();
|
|
64
|
+
let buffer = "";
|
|
65
|
+
try {
|
|
66
|
+
for (;;) {
|
|
67
|
+
const {
|
|
68
|
+
done,
|
|
69
|
+
value
|
|
70
|
+
} = await reader.read();
|
|
71
|
+
if (done) break;
|
|
72
|
+
buffer += decoder.decode(value, {
|
|
73
|
+
stream: true
|
|
74
|
+
});
|
|
75
|
+
const gen = parseChunk(buffer);
|
|
76
|
+
let next = gen.next();
|
|
77
|
+
while (!next.done) {
|
|
78
|
+
yield next.value;
|
|
79
|
+
next = gen.next();
|
|
80
|
+
}
|
|
81
|
+
buffer = next.value;
|
|
82
|
+
}
|
|
83
|
+
} finally {
|
|
84
|
+
// Cancelling on abort keeps the socket from lingering after the user
|
|
85
|
+
// closes the sheet mid-answer.
|
|
86
|
+
try {
|
|
87
|
+
reader.releaseLock();
|
|
88
|
+
} catch {
|
|
89
|
+
/* already released */
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/** Body text for a failed response, trimmed to something a user can read. */
|
|
95
|
+
async function errorBody(response) {
|
|
96
|
+
let detail = "";
|
|
97
|
+
try {
|
|
98
|
+
detail = (await response.text()).slice(0, 600);
|
|
99
|
+
} catch {
|
|
100
|
+
/* body already consumed or unreadable */
|
|
101
|
+
}
|
|
102
|
+
return `HTTP ${response.status}${detail ? ` — ${detail}` : ""}`;
|
|
103
|
+
}
|
|
104
|
+
//# sourceMappingURL=sse.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["parseChunk","buffer","rest","idx","search","raw","slice","event","dataLines","line","split","startsWith","trim","push","replace","length","data","join","readSse","response","body","getReader","text","gen","next","done","value","tail","t","reader","decoder","TextDecoder","read","decode","stream","releaseLock","errorBody","detail","status"],"sourceRoot":"../../../src","sources":["providers/sse.ts"],"mappings":";;;;;;;AAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AAOA,UAAUA,UAAUA,CAACC,MAAc,EAAqC;EACtE,IAAIC,IAAI,GAAGD,MAAM;EACjB,IAAIE,GAAW;EACf;EACA,OAAO,CAACA,GAAG,GAAGD,IAAI,CAACE,MAAM,CAAC,YAAY,CAAC,MAAM,CAAC,CAAC,EAAE;IAC/C,MAAMC,GAAG,GAAGH,IAAI,CAACI,KAAK,CAAC,CAAC,EAAEH,GAAG,CAAC;IAC9BD,IAAI,GAAGA,IAAI,CAACI,KAAK,CAACH,GAAG,IAAID,IAAI,CAACC,GAAG,CAAC,KAAK,IAAI,GAAG,CAAC,GAAG,CAAC,CAAC,CAAC;IAErD,IAAII,KAAyB;IAC7B,MAAMC,SAAmB,GAAG,EAAE;IAC9B,KAAK,MAAMC,IAAI,IAAIJ,GAAG,CAACK,KAAK,CAAC,OAAO,CAAC,EAAE;MACrC,IAAID,IAAI,CAACE,UAAU,CAAC,GAAG,CAAC,EAAE,SAAS,CAAC;MACpC,IAAIF,IAAI,CAACE,UAAU,CAAC,QAAQ,CAAC,EAAEJ,KAAK,GAAGE,IAAI,CAACH,KAAK,CAAC,CAAC,CAAC,CAACM,IAAI,CAAC,CAAC,CAAC,KACvD,IAAIH,IAAI,CAACE,UAAU,CAAC,OAAO,CAAC,EAAEH,SAAS,CAACK,IAAI,CAACJ,IAAI,CAACH,KAAK,CAAC,CAAC,CAAC,CAACQ,OAAO,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC;IACpF;IACA,IAAIN,SAAS,CAACO,MAAM,EAAE,MAAM;MAAER,KAAK;MAAES,IAAI,EAAER,SAAS,CA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,OAAO,CAACG,MAAM,CAACP,KAAK,EAAE;QAAEQ,MAAM,EAAE;MAAK,CAAC,CAAC;MACjD,MAAMX,GAAG,GAAGvB,UAAU,CAACC,MAAM,CAAC;MAC9B,IAAIuB,IAAI,GAAGD,GAAG,CAACC,IAAI,CAAC,CAAC;MACrB,OAAO,CAACA,IAAI,CAACC,IAAI,EAAE;QACjB,MAAMD,IAAI,CAACE,KAAK;QAChBF,IAAI,GAAGD,GAAG,CAACC,IAAI,CAAC,CAAC;MACnB;MACAvB,MAAM,GAAGuB,IAAI,CAACE,KAAK;IACrB;EACF,CAAC,SAAS;IACR;IACA;IACA,IAAI;MACFG,MAAM,CAACM,WAAW,CAAC,CAAC;IACtB,CAAC,CAAC,MAAM;MACN;IAAA;EAEJ;AACF;;AAEA;AACO,eAAeC,SAASA,CAACjB,QAAkB,EAAmB;EACnE,IAAIkB,MAAM,GAAG,EAAE;EACf,IAAI;IACFA,MAAM,GAAG,CAAC,MAAMlB,QAAQ,CAACG,IAAI,CAAC,CAAC,EAAEhB,KAAK,CAAC,CAAC,EAAE,GAAG,CAAC;EAChD,CAAC,CAAC,MAAM;IACN;EAAA;EAEF,OAAO,QAAQa,QAAQ,CAACmB,MAAM,GAAGD,MAAM,GAAG,MAAMA,MAAM,EAAE,GAAG,EAAE,EAAE;AACjE","ignoreList":[]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":[],"sourceRoot":"../../../src","sources":["providers/types.ts"],"mappings":"","ignoreList":[]}
|