@voltojs/providers 0.0.0-stage → 0.2.0
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 +21 -0
- package/dist/anthropic/index.d.ts +5 -0
- package/dist/anthropic/index.d.ts.map +1 -0
- package/dist/anthropic/index.js +3 -0
- package/dist/anthropic/index.js.map +1 -0
- package/dist/anthropic/model-port.d.ts +34 -0
- package/dist/anthropic/model-port.d.ts.map +1 -0
- package/dist/anthropic/model-port.js +337 -0
- package/dist/anthropic/model-port.js.map +1 -0
- package/dist/anthropic/stream-normalize.d.ts +27 -0
- package/dist/anthropic/stream-normalize.d.ts.map +1 -0
- package/dist/anthropic/stream-normalize.js +140 -0
- package/dist/anthropic/stream-normalize.js.map +1 -0
- package/dist/embedder-openai/embedder.d.ts +18 -0
- package/dist/embedder-openai/embedder.d.ts.map +1 -0
- package/dist/embedder-openai/embedder.js +44 -0
- package/dist/embedder-openai/embedder.js.map +1 -0
- package/dist/embedder-openai/index.d.ts +3 -0
- package/dist/embedder-openai/index.d.ts.map +1 -0
- package/dist/embedder-openai/index.js +2 -0
- package/dist/embedder-openai/index.js.map +1 -0
- package/dist/openai/index.d.ts +5 -0
- package/dist/openai/index.d.ts.map +1 -0
- package/dist/openai/index.js +3 -0
- package/dist/openai/index.js.map +1 -0
- package/dist/openai/model-port.d.ts +37 -0
- package/dist/openai/model-port.d.ts.map +1 -0
- package/dist/openai/model-port.js +330 -0
- package/dist/openai/model-port.js.map +1 -0
- package/dist/openai/stream-normalize.d.ts +70 -0
- package/dist/openai/stream-normalize.d.ts.map +1 -0
- package/dist/openai/stream-normalize.js +185 -0
- package/dist/openai/stream-normalize.js.map +1 -0
- package/dist/pi-ai/catalog.d.ts +40 -0
- package/dist/pi-ai/catalog.d.ts.map +1 -0
- package/dist/pi-ai/catalog.js +38 -0
- package/dist/pi-ai/catalog.js.map +1 -0
- package/dist/pi-ai/collection.d.ts +133 -0
- package/dist/pi-ai/collection.d.ts.map +1 -0
- package/dist/pi-ai/collection.js +532 -0
- package/dist/pi-ai/collection.js.map +1 -0
- package/dist/pi-ai/context.d.ts +15 -0
- package/dist/pi-ai/context.d.ts.map +1 -0
- package/dist/pi-ai/context.js +414 -0
- package/dist/pi-ai/context.js.map +1 -0
- package/dist/pi-ai/index.d.ts +8 -0
- package/dist/pi-ai/index.d.ts.map +1 -0
- package/dist/pi-ai/index.js +4 -0
- package/dist/pi-ai/index.js.map +1 -0
- package/dist/pi-ai/model-port.d.ts +45 -0
- package/dist/pi-ai/model-port.d.ts.map +1 -0
- package/dist/pi-ai/model-port.js +168 -0
- package/dist/pi-ai/model-port.js.map +1 -0
- package/dist/pi-ai/replay.d.ts +46 -0
- package/dist/pi-ai/replay.d.ts.map +1 -0
- package/dist/pi-ai/replay.js +99 -0
- package/dist/pi-ai/replay.js.map +1 -0
- package/dist/pi-ai/stream-normalize.d.ts +13 -0
- package/dist/pi-ai/stream-normalize.d.ts.map +1 -0
- package/dist/pi-ai/stream-normalize.js +118 -0
- package/dist/pi-ai/stream-normalize.js.map +1 -0
- package/package.json +48 -3
- package/README.md +0 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 volto contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export { createAnthropicModelPort, anthropicFamilyVision } from "./model-port.js";
|
|
2
|
+
export type { AnthropicModelPortOptions } from "./model-port.js";
|
|
3
|
+
export { normalizeStream } from "./stream-normalize.js";
|
|
4
|
+
export type { RawAnthropicEvent } from "./stream-normalize.js";
|
|
5
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/anthropic/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,wBAAwB,EAAE,qBAAqB,EAAE,MAAM,iBAAiB,CAAC;AAClF,YAAY,EAAE,yBAAyB,EAAE,MAAM,iBAAiB,CAAC;AACjE,OAAO,EAAE,eAAe,EAAE,MAAM,uBAAuB,CAAC;AACxD,YAAY,EAAE,iBAAiB,EAAE,MAAM,uBAAuB,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/anthropic/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,wBAAwB,EAAE,qBAAqB,EAAE,MAAM,iBAAiB,CAAC;AAElF,OAAO,EAAE,eAAe,EAAE,MAAM,uBAAuB,CAAC"}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Anthropic ModelPort adapter.
|
|
3
|
+
*
|
|
4
|
+
* Connects to /v1/messages with stream:true,
|
|
5
|
+
* normalizes the SSE response into core ModelEvents.
|
|
6
|
+
*/
|
|
7
|
+
import type { ModelPort, Message, ModelInputValue } from "@voltojs/core";
|
|
8
|
+
export interface AnthropicModelPortOptions {
|
|
9
|
+
/** Base URL of the Anthropic API (default: https://api.anthropic.com) */
|
|
10
|
+
baseURL?: string;
|
|
11
|
+
/** API key for x-api-key header */
|
|
12
|
+
apiKey: string;
|
|
13
|
+
/** Default model if not specified in request */
|
|
14
|
+
defaultModel?: string;
|
|
15
|
+
/**
|
|
16
|
+
* Input-modalities override for every model this port serves. Absent lets
|
|
17
|
+
* {@link anthropicFamilyVision} decide from well-known Anthropic model ids;
|
|
18
|
+
* unrecognized ids stay UNKNOWN rather than guessing text-only.
|
|
19
|
+
*/
|
|
20
|
+
input?: ModelInputValue;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Image-input support for known Anthropic model-id families.
|
|
24
|
+
* Claude 3 and later accept images; Claude 2 / Instant do not. Unrecognized
|
|
25
|
+
* ids (proxied, renamed, or future models) return undefined = UNKNOWN.
|
|
26
|
+
*/
|
|
27
|
+
export declare function anthropicFamilyVision(model: string): boolean | undefined;
|
|
28
|
+
export declare function createAnthropicModelPort(opts: AnthropicModelPortOptions): ModelPort;
|
|
29
|
+
/**
|
|
30
|
+
* Map core messages to Anthropic's message array. Exported for tests;
|
|
31
|
+
* buildRequestBody applies cache breakpoints on top of this.
|
|
32
|
+
*/
|
|
33
|
+
export declare function toAnthropicMessages(messages: Message[]): Array<Record<string, unknown>>;
|
|
34
|
+
//# sourceMappingURL=model-port.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"model-port.d.ts","sourceRoot":"","sources":["../../src/anthropic/model-port.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,KAAK,EAAE,SAAS,EAA4B,OAAO,EAAgB,eAAe,EAAsC,MAAM,eAAe,CAAC;AAGrJ,MAAM,WAAW,yBAAyB;IACxC,yEAAyE;IACzE,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,mCAAmC;IACnC,MAAM,EAAE,MAAM,CAAC;IACf,gDAAgD;IAChD,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB;;;;OAIG;IACH,KAAK,CAAC,EAAE,eAAe,CAAC;CACzB;AAED;;;;GAIG;AACH,wBAAgB,qBAAqB,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,GAAG,SAAS,CAIxE;AAQD,wBAAgB,wBAAwB,CAAC,IAAI,EAAE,yBAAyB,GAAG,SAAS,CA6DnF;AAiKD;;;GAGG;AACH,wBAAgB,mBAAmB,CAAC,QAAQ,EAAE,OAAO,EAAE,GAAG,KAAK,CAAC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,CAEvF"}
|
|
@@ -0,0 +1,337 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Anthropic ModelPort adapter.
|
|
3
|
+
*
|
|
4
|
+
* Connects to /v1/messages with stream:true,
|
|
5
|
+
* normalizes the SSE response into core ModelEvents.
|
|
6
|
+
*/
|
|
7
|
+
import { normalizeStream } from "./stream-normalize.js";
|
|
8
|
+
/**
|
|
9
|
+
* Image-input support for known Anthropic model-id families.
|
|
10
|
+
* Claude 3 and later accept images; Claude 2 / Instant do not. Unrecognized
|
|
11
|
+
* ids (proxied, renamed, or future models) return undefined = UNKNOWN.
|
|
12
|
+
*/
|
|
13
|
+
export function anthropicFamilyVision(model) {
|
|
14
|
+
if (/^claude-instant|^claude-2/.test(model))
|
|
15
|
+
return false;
|
|
16
|
+
if (/^claude-3|^claude-(?:sonnet|opus|haiku|fable)-\d/.test(model))
|
|
17
|
+
return true;
|
|
18
|
+
return undefined;
|
|
19
|
+
}
|
|
20
|
+
/** bool→input at the projection boundary; keeps the family helpers boolean-shaped. */
|
|
21
|
+
function toInput(vision) {
|
|
22
|
+
if (vision === undefined)
|
|
23
|
+
return undefined;
|
|
24
|
+
return vision ? ["text", "image"] : ["text"];
|
|
25
|
+
}
|
|
26
|
+
export function createAnthropicModelPort(opts) {
|
|
27
|
+
const baseURL = (opts.baseURL ?? "https://api.anthropic.com").replace(/\/+$/, "");
|
|
28
|
+
return {
|
|
29
|
+
async *stream(req) {
|
|
30
|
+
const model = req.model ?? opts.defaultModel ?? "claude-sonnet-4-20250514";
|
|
31
|
+
const body = buildRequestBody(req, model);
|
|
32
|
+
let response;
|
|
33
|
+
try {
|
|
34
|
+
response = await fetch(`${baseURL}/v1/messages`, {
|
|
35
|
+
method: "POST",
|
|
36
|
+
headers: {
|
|
37
|
+
"Content-Type": "application/json",
|
|
38
|
+
"x-api-key": opts.apiKey,
|
|
39
|
+
"anthropic-version": "2023-06-01",
|
|
40
|
+
},
|
|
41
|
+
body: JSON.stringify(body),
|
|
42
|
+
signal: req.abortSignal,
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
catch (err) {
|
|
46
|
+
if (err instanceof Error && err.name === "AbortError") {
|
|
47
|
+
yield { type: "error", error: "Request aborted", retriable: false };
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
yield {
|
|
51
|
+
type: "error",
|
|
52
|
+
error: err instanceof Error ? err.message : String(err),
|
|
53
|
+
retriable: true,
|
|
54
|
+
};
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
if (!response.ok) {
|
|
58
|
+
const errorText = await response.text().catch(() => "unknown error");
|
|
59
|
+
yield {
|
|
60
|
+
type: "error",
|
|
61
|
+
error: `Anthropic API error ${response.status}: ${errorText}`,
|
|
62
|
+
retriable: response.status >= 500 || response.status === 429,
|
|
63
|
+
};
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
if (!response.body) {
|
|
67
|
+
yield { type: "error", error: "Empty response body", retriable: true };
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
const rawEvents = parseSSEStream(response.body);
|
|
71
|
+
yield* normalizeStream(rawEvents);
|
|
72
|
+
},
|
|
73
|
+
describeModel: async (model) => {
|
|
74
|
+
const info = { model };
|
|
75
|
+
const input = opts.input ?? toInput(anthropicFamilyVision(model));
|
|
76
|
+
if (input !== undefined) {
|
|
77
|
+
info.input = input;
|
|
78
|
+
}
|
|
79
|
+
return info;
|
|
80
|
+
},
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
// ──────────────────────────────────────────────
|
|
84
|
+
// Request body builder
|
|
85
|
+
// ──────────────────────────────────────────────
|
|
86
|
+
/**
|
|
87
|
+
* Project a system message's content to the plain string Anthropic's
|
|
88
|
+
* top-level `system` field accepts. Core state can carry a BLOCK-ARRAY
|
|
89
|
+
* system message — the compaction image-preservation summary lands one
|
|
90
|
+
* behind a documented cast (spec 2026-10-16 D5) — and system prompts are
|
|
91
|
+
* text-only on this provider's wire (there is no native system-image).
|
|
92
|
+
* Without this projection the block array stringifies to
|
|
93
|
+
* "[object Object]" garbage; with it the model sees the summary and the
|
|
94
|
+
* preserved-image marker, and image blocks degrade to the same
|
|
95
|
+
* `[image: WxH mediaType]` placeholder core's extractBlockText renders.
|
|
96
|
+
*/
|
|
97
|
+
function projectSystemContent(content) {
|
|
98
|
+
if (typeof content === "string")
|
|
99
|
+
return content;
|
|
100
|
+
return content.map(systemBlockText).join("\n");
|
|
101
|
+
}
|
|
102
|
+
/** One system block's text projection — mirrors core's extractBlockText. */
|
|
103
|
+
function systemBlockText(block) {
|
|
104
|
+
switch (block.type) {
|
|
105
|
+
case "text":
|
|
106
|
+
case "structured":
|
|
107
|
+
return block.text;
|
|
108
|
+
case "image": {
|
|
109
|
+
const dims = block.width !== undefined && block.height !== undefined
|
|
110
|
+
? `${block.width}x${block.height} `
|
|
111
|
+
: "";
|
|
112
|
+
return `[image: ${dims}${block.source.mediaType}]`;
|
|
113
|
+
}
|
|
114
|
+
case "tool_use":
|
|
115
|
+
return `[tool: ${block.name}]`;
|
|
116
|
+
case "tool_result":
|
|
117
|
+
return block.content.map(systemBlockText).join("\n");
|
|
118
|
+
case "reasoning":
|
|
119
|
+
return "";
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
function buildRequestBody(req, model) {
|
|
123
|
+
const body = {
|
|
124
|
+
model,
|
|
125
|
+
max_tokens: req.maxTokens ?? 4096,
|
|
126
|
+
stream: true,
|
|
127
|
+
};
|
|
128
|
+
// System prompt goes as a top-level field (not a message role)
|
|
129
|
+
const systemParts = [];
|
|
130
|
+
const otherMessages = [];
|
|
131
|
+
for (const msg of req.messages) {
|
|
132
|
+
if (msg.role === "system") {
|
|
133
|
+
// String-typed in core, but a block array can arrive at runtime (see
|
|
134
|
+
// projectSystemContent) — project instead of shipping garbage.
|
|
135
|
+
systemParts.push(projectSystemContent(msg.content));
|
|
136
|
+
}
|
|
137
|
+
else {
|
|
138
|
+
otherMessages.push(msg);
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
// Cache breakpoints: convert system to block format if cache_control needed
|
|
142
|
+
const hasSystemCacheBreakpoint = (req.cacheBreakpoints ?? []).some((bp) => bp === 0 && systemParts.length > 0);
|
|
143
|
+
if (hasSystemCacheBreakpoint) {
|
|
144
|
+
// Anthropic system field can be an array of content blocks with cache_control
|
|
145
|
+
body.system = [
|
|
146
|
+
{
|
|
147
|
+
type: "text",
|
|
148
|
+
text: systemParts.join("\n\n"),
|
|
149
|
+
cache_control: { type: "ephemeral" },
|
|
150
|
+
},
|
|
151
|
+
];
|
|
152
|
+
}
|
|
153
|
+
else if (systemParts.length > 0) {
|
|
154
|
+
body.system = systemParts.join("\n\n");
|
|
155
|
+
}
|
|
156
|
+
// Convert messages and apply cache_control at breakpoint indices
|
|
157
|
+
const breakpoints = new Set(req.cacheBreakpoints ?? []);
|
|
158
|
+
// Adjust breakpoints: system messages are extracted, so message indices shift
|
|
159
|
+
const systemCount = systemParts.length;
|
|
160
|
+
const convertedMessages = otherMessages.map((msg, i) => {
|
|
161
|
+
const converted = convertMessage(msg);
|
|
162
|
+
// Breakpoint index i+systemCount in original = breakpoint at i in otherMessages
|
|
163
|
+
if (breakpoints.has(i + systemCount)) {
|
|
164
|
+
return { ...converted, cache_control: { type: "ephemeral" } };
|
|
165
|
+
}
|
|
166
|
+
return converted;
|
|
167
|
+
});
|
|
168
|
+
body.messages = convertedMessages;
|
|
169
|
+
if (req.tools.length > 0) {
|
|
170
|
+
body.tools = req.tools.map((t) => ({
|
|
171
|
+
name: t.name,
|
|
172
|
+
description: t.description,
|
|
173
|
+
input_schema: t.inputSchema,
|
|
174
|
+
}));
|
|
175
|
+
}
|
|
176
|
+
if (req.temperature !== undefined) {
|
|
177
|
+
body.temperature = req.temperature;
|
|
178
|
+
}
|
|
179
|
+
// Structured / JSON output mode — Anthropic doesn't have native JSON schema
|
|
180
|
+
// mode, so inject a system instruction to enforce JSON output.
|
|
181
|
+
if (req.responseFormat?.type === "json") {
|
|
182
|
+
const schemaHint = req.responseFormat.schema
|
|
183
|
+
? `\nYou MUST respond with valid JSON matching this schema:\n${JSON.stringify(req.responseFormat.schema, null, 2)}`
|
|
184
|
+
: "\nYou MUST respond with valid JSON only. No markdown, no explanations outside the JSON.";
|
|
185
|
+
systemParts.push(schemaHint);
|
|
186
|
+
body.system = systemParts.join("\n\n");
|
|
187
|
+
}
|
|
188
|
+
return body;
|
|
189
|
+
}
|
|
190
|
+
/**
|
|
191
|
+
* Map a request-surface image block (base64 wire shape produced by the
|
|
192
|
+
* Runner's resolveImageRefs) to Anthropic's image block. Returns null when
|
|
193
|
+
* the block is not a resolvable base64 image so callers can degrade.
|
|
194
|
+
*/
|
|
195
|
+
function toAnthropicImageBlock(block) {
|
|
196
|
+
if (block.type !== "image")
|
|
197
|
+
return null;
|
|
198
|
+
// Core's ImageContent is ref-only; the wire shape arrives as an untyped
|
|
199
|
+
// cast at the request boundary. Narrow at runtime rather than widening core.
|
|
200
|
+
const src = block.source;
|
|
201
|
+
if (src.type === "base64" && typeof src.mediaType === "string" && typeof src.data === "string") {
|
|
202
|
+
return {
|
|
203
|
+
type: "image",
|
|
204
|
+
source: {
|
|
205
|
+
type: "base64",
|
|
206
|
+
media_type: src.mediaType,
|
|
207
|
+
data: src.data,
|
|
208
|
+
},
|
|
209
|
+
};
|
|
210
|
+
}
|
|
211
|
+
return null;
|
|
212
|
+
}
|
|
213
|
+
/** Map a tool-result content block (text | structured | image) to Anthropic's shape. */
|
|
214
|
+
function toAnthropicToolResultBlock(block) {
|
|
215
|
+
if (block.type === "text") {
|
|
216
|
+
return { type: "text", text: block.text };
|
|
217
|
+
}
|
|
218
|
+
if (block.type === "structured") {
|
|
219
|
+
// Anthropic tool_result content accepts text/image only — degrade the
|
|
220
|
+
// structured block to a text block carrying its text projection rather
|
|
221
|
+
// than pretending native structured support or silently dropping it.
|
|
222
|
+
return { type: "text", text: block.text };
|
|
223
|
+
}
|
|
224
|
+
const image = toAnthropicImageBlock(block);
|
|
225
|
+
return image ?? { type: "text", text: "[image omitted]" };
|
|
226
|
+
}
|
|
227
|
+
/**
|
|
228
|
+
* Map core messages to Anthropic's message array. Exported for tests;
|
|
229
|
+
* buildRequestBody applies cache breakpoints on top of this.
|
|
230
|
+
*/
|
|
231
|
+
export function toAnthropicMessages(messages) {
|
|
232
|
+
return messages.map(convertMessage);
|
|
233
|
+
}
|
|
234
|
+
function convertMessage(msg) {
|
|
235
|
+
switch (msg.role) {
|
|
236
|
+
case "user": {
|
|
237
|
+
if (typeof msg.content === "string") {
|
|
238
|
+
return { role: "user", content: msg.content };
|
|
239
|
+
}
|
|
240
|
+
// ContentBlock[] — convert to Anthropic content blocks
|
|
241
|
+
return {
|
|
242
|
+
role: "user",
|
|
243
|
+
content: msg.content.map((block) => {
|
|
244
|
+
if (block.type === "text") {
|
|
245
|
+
return { type: "text", text: block.text };
|
|
246
|
+
}
|
|
247
|
+
if (block.type === "image") {
|
|
248
|
+
const image = toAnthropicImageBlock(block);
|
|
249
|
+
return image ?? { type: "text", text: "[image omitted]" };
|
|
250
|
+
}
|
|
251
|
+
if (block.type === "tool_result") {
|
|
252
|
+
return {
|
|
253
|
+
type: "tool_result",
|
|
254
|
+
tool_use_id: block.toolUseId,
|
|
255
|
+
content: block.content.map(toAnthropicToolResultBlock),
|
|
256
|
+
...(block.isError ? { is_error: true } : {}),
|
|
257
|
+
};
|
|
258
|
+
}
|
|
259
|
+
return block;
|
|
260
|
+
}),
|
|
261
|
+
};
|
|
262
|
+
}
|
|
263
|
+
case "assistant": {
|
|
264
|
+
const content = [];
|
|
265
|
+
for (const block of msg.content) {
|
|
266
|
+
if (block.type === "text") {
|
|
267
|
+
content.push({ type: "text", text: block.text });
|
|
268
|
+
}
|
|
269
|
+
else if (block.type === "tool_use") {
|
|
270
|
+
content.push({
|
|
271
|
+
type: "tool_use",
|
|
272
|
+
id: block.id,
|
|
273
|
+
name: block.name,
|
|
274
|
+
input: block.input,
|
|
275
|
+
});
|
|
276
|
+
}
|
|
277
|
+
// The core does carry reasoning blocks now, but faithful replay of a
|
|
278
|
+
// "thinking" block back to Anthropic needs its per-block signature,
|
|
279
|
+
// which travels in modelMeta — this adapter does not read that yet,
|
|
280
|
+
// so the reasoning text is dropped here rather than resent unsigned.
|
|
281
|
+
}
|
|
282
|
+
return { role: "assistant", content };
|
|
283
|
+
}
|
|
284
|
+
case "tool": {
|
|
285
|
+
// Anthropic uses "user" role with tool_result content blocks
|
|
286
|
+
return {
|
|
287
|
+
role: "user",
|
|
288
|
+
content: msg.content.map((tc) => ({
|
|
289
|
+
type: "tool_result",
|
|
290
|
+
tool_use_id: tc.toolUseId,
|
|
291
|
+
content: tc.content.map(toAnthropicToolResultBlock),
|
|
292
|
+
...(tc.isError ? { is_error: true } : {}),
|
|
293
|
+
})),
|
|
294
|
+
};
|
|
295
|
+
}
|
|
296
|
+
default:
|
|
297
|
+
// System messages are filtered in buildRequestBody; this path is unreachable
|
|
298
|
+
return msg;
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
// ──────────────────────────────────────────────
|
|
302
|
+
// SSE parser
|
|
303
|
+
// ──────────────────────────────────────────────
|
|
304
|
+
async function* parseSSEStream(body) {
|
|
305
|
+
const reader = body.getReader();
|
|
306
|
+
const decoder = new TextDecoder();
|
|
307
|
+
let buffer = "";
|
|
308
|
+
try {
|
|
309
|
+
while (true) {
|
|
310
|
+
const { done, value } = await reader.read();
|
|
311
|
+
if (done)
|
|
312
|
+
break;
|
|
313
|
+
buffer += decoder.decode(value, { stream: true });
|
|
314
|
+
const lines = buffer.split("\n");
|
|
315
|
+
buffer = lines.pop() ?? "";
|
|
316
|
+
for (const line of lines) {
|
|
317
|
+
const trimmed = line.trim();
|
|
318
|
+
if (!trimmed || trimmed.startsWith(":"))
|
|
319
|
+
continue;
|
|
320
|
+
if (!trimmed.startsWith("data: "))
|
|
321
|
+
continue;
|
|
322
|
+
const data = trimmed.slice(6);
|
|
323
|
+
try {
|
|
324
|
+
const parsed = JSON.parse(data);
|
|
325
|
+
yield parsed;
|
|
326
|
+
}
|
|
327
|
+
catch {
|
|
328
|
+
// Skip malformed JSON lines
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
finally {
|
|
334
|
+
reader.releaseLock();
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
//# sourceMappingURL=model-port.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"model-port.js","sourceRoot":"","sources":["../../src/anthropic/model-port.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAGH,OAAO,EAAE,eAAe,EAA0B,MAAM,uBAAuB,CAAC;AAiBhF;;;;GAIG;AACH,MAAM,UAAU,qBAAqB,CAAC,KAAa;IACjD,IAAI,2BAA2B,CAAC,IAAI,CAAC,KAAK,CAAC;QAAE,OAAO,KAAK,CAAC;IAC1D,IAAI,kDAAkD,CAAC,IAAI,CAAC,KAAK,CAAC;QAAE,OAAO,IAAI,CAAC;IAChF,OAAO,SAAS,CAAC;AACnB,CAAC;AAED,sFAAsF;AACtF,SAAS,OAAO,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G,IAAI,WAAW,EAAE,CAAC;IAClC,IAAI,MAAM,GAAG,EAAE,CAAC;IAEhB,IAAI,CAAC;QACH,OAAO,IAAI,EAAE,CAAC;YACZ,MAAM,EAAE,IAAI,EAAE,KAAK,EAAE,GAAG,MAAM,MAAM,CAAC,IAAI,EAAE,CAAC;YAC5C,IAAI,IAAI;gBAAE,MAAM;YAEhB,MAAM,IAAI,OAAO,CAAC,MAAM,CAAC,KAAK,EAAE,EAAE,MAAM,EAAE,IAAI,EAAE,CAAC,CAAC;YAClD,MAAM,KAAK,GAAG,MAAM,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;YACjC,MAAM,GAAG,KAAK,CAAC,GAAG,EAAE,IAAI,EAAE,CAAC;YAE3B,KAAK,MAAM,IAAI,IAAI,KAAK,EAAE,CAAC;gBACzB,MAAM,OAAO,GAAG,IAAI,CAAC,IAAI,EAAE,CAAC;gBAC5B,IAAI,CAAC,OAAO,IAAI,OAAO,CAAC,UAAU,CAAC,GAAG,CAAC;oBAAE,SAAS;gBAClD,IAAI,CAAC,OAAO,CAAC,UAAU,CAAC,QAAQ,CAAC;oBAAE,SAAS;gBAE5C,MAAM,IAAI,GAAG,OAAO,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;gBAC9B,IAAI,CAAC;oBACH,MAAM,MAAM,GAAG,IAAI,CAAC,KAAK,CAAC,IAAI,CAAsB,CAAC;oBACrD,MAAM,MAAM,CAAC;gBACf,CAAC;gBAAC,MAAM,CAAC;oBACP,4BAA4B;gBAC9B,CAAC;YACH,CAAC;QACH,CAAC;IACH,CAAC;YAAS,CAAC;QACT,MAAM,CAAC,WAAW,EAAE,CAAC;IACvB,CAAC;AACH,CAAC"}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* stream-normalize — converts raw Anthropic streaming events into core ModelEvents.
|
|
3
|
+
*
|
|
4
|
+
* Handles:
|
|
5
|
+
* - message_start → captures input_tokens (cache read/write)
|
|
6
|
+
* - content_block_start (text / thinking / tool_use / server_tool_use)
|
|
7
|
+
* - content_block_delta (text_delta, thinking_delta, input_json_delta, signature_delta)
|
|
8
|
+
* - message_delta → output_tokens (cumulative) + stop_reason → done + usage event
|
|
9
|
+
* - message_stop → stream complete
|
|
10
|
+
* - ping → skip
|
|
11
|
+
* - error → error event with retriable flag
|
|
12
|
+
*/
|
|
13
|
+
import type { ModelEvent } from "@voltojs/core";
|
|
14
|
+
/** Raw SSE event parsed from Anthropic Messages API stream */
|
|
15
|
+
export interface RawAnthropicEvent {
|
|
16
|
+
type: string;
|
|
17
|
+
[key: string]: unknown;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Normalize a stream of raw Anthropic events into core ModelEvents.
|
|
21
|
+
*
|
|
22
|
+
* Stateful: tracks content_block type by index to route deltas correctly.
|
|
23
|
+
* Usage is emitted as a single event: input tokens from message_start,
|
|
24
|
+
* output tokens from message_delta (which carries the final cumulative count).
|
|
25
|
+
*/
|
|
26
|
+
export declare function normalizeStream(rawEvents: AsyncIterable<RawAnthropicEvent>): AsyncGenerator<ModelEvent>;
|
|
27
|
+
//# sourceMappingURL=stream-normalize.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"stream-normalize.d.ts","sourceRoot":"","sources":["../../src/anthropic/stream-normalize.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAEH,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,eAAe,CAAC;AAEhD,8DAA8D;AAC9D,MAAM,WAAW,iBAAiB;IAChC,IAAI,EAAE,MAAM,CAAC;IACb,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC;CACxB;AAQD;;;;;;GAMG;AACH,wBAAuB,eAAe,CACpC,SAAS,EAAE,aAAa,CAAC,iBAAiB,CAAC,GAC1C,cAAc,CAAC,UAAU,CAAC,CAoH5B"}
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* stream-normalize — converts raw Anthropic streaming events into core ModelEvents.
|
|
3
|
+
*
|
|
4
|
+
* Handles:
|
|
5
|
+
* - message_start → captures input_tokens (cache read/write)
|
|
6
|
+
* - content_block_start (text / thinking / tool_use / server_tool_use)
|
|
7
|
+
* - content_block_delta (text_delta, thinking_delta, input_json_delta, signature_delta)
|
|
8
|
+
* - message_delta → output_tokens (cumulative) + stop_reason → done + usage event
|
|
9
|
+
* - message_stop → stream complete
|
|
10
|
+
* - ping → skip
|
|
11
|
+
* - error → error event with retriable flag
|
|
12
|
+
*/
|
|
13
|
+
/**
|
|
14
|
+
* Normalize a stream of raw Anthropic events into core ModelEvents.
|
|
15
|
+
*
|
|
16
|
+
* Stateful: tracks content_block type by index to route deltas correctly.
|
|
17
|
+
* Usage is emitted as a single event: input tokens from message_start,
|
|
18
|
+
* output tokens from message_delta (which carries the final cumulative count).
|
|
19
|
+
*/
|
|
20
|
+
export async function* normalizeStream(rawEvents) {
|
|
21
|
+
/** Map content_block index → block type */
|
|
22
|
+
const blockTypes = new Map();
|
|
23
|
+
/** Map content_block index → tool_use metadata */
|
|
24
|
+
const toolUseBlocks = new Map();
|
|
25
|
+
/** Captured from message_start for final usage event */
|
|
26
|
+
let inputTokens = 0;
|
|
27
|
+
let cacheReadTokens;
|
|
28
|
+
let cacheWriteTokens;
|
|
29
|
+
for await (const event of rawEvents) {
|
|
30
|
+
switch (event.type) {
|
|
31
|
+
case "message_start": {
|
|
32
|
+
const msg = event.message;
|
|
33
|
+
const usage = msg?.usage;
|
|
34
|
+
if (usage) {
|
|
35
|
+
inputTokens = usage.input_tokens ?? 0;
|
|
36
|
+
cacheReadTokens = usage.cache_read_input_tokens ?? undefined;
|
|
37
|
+
cacheWriteTokens = usage.cache_creation_input_tokens ?? undefined;
|
|
38
|
+
}
|
|
39
|
+
break;
|
|
40
|
+
}
|
|
41
|
+
case "content_block_start": {
|
|
42
|
+
const index = event.index;
|
|
43
|
+
const block = event.content_block;
|
|
44
|
+
const blockType = block?.type;
|
|
45
|
+
if (blockType === "text") {
|
|
46
|
+
blockTypes.set(index, "text");
|
|
47
|
+
}
|
|
48
|
+
else if (blockType === "thinking") {
|
|
49
|
+
blockTypes.set(index, "thinking");
|
|
50
|
+
}
|
|
51
|
+
else if (blockType === "tool_use" || blockType === "server_tool_use") {
|
|
52
|
+
blockTypes.set(index, blockType);
|
|
53
|
+
const entry = {
|
|
54
|
+
toolCallId: block?.id ?? "",
|
|
55
|
+
toolName: block?.name ?? "",
|
|
56
|
+
};
|
|
57
|
+
toolUseBlocks.set(index, entry);
|
|
58
|
+
yield {
|
|
59
|
+
type: "tool_use_start",
|
|
60
|
+
toolCallId: entry.toolCallId,
|
|
61
|
+
toolName: entry.toolName,
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
break;
|
|
65
|
+
}
|
|
66
|
+
case "content_block_delta": {
|
|
67
|
+
const index = event.index;
|
|
68
|
+
const delta = event.delta;
|
|
69
|
+
if (delta?.type === "text_delta" && delta.text) {
|
|
70
|
+
yield { type: "text_delta", text: delta.text };
|
|
71
|
+
}
|
|
72
|
+
else if (delta?.type === "thinking_delta" && delta.thinking) {
|
|
73
|
+
yield { type: "reasoning_delta", text: delta.thinking };
|
|
74
|
+
}
|
|
75
|
+
else if (delta?.type === "input_json_delta" && delta.partial_json != null) {
|
|
76
|
+
const toolEntry = toolUseBlocks.get(index);
|
|
77
|
+
if (toolEntry) {
|
|
78
|
+
yield {
|
|
79
|
+
type: "tool_use_delta",
|
|
80
|
+
toolCallId: toolEntry.toolCallId,
|
|
81
|
+
argumentsDelta: delta.partial_json,
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
// signature_delta → skip
|
|
86
|
+
break;
|
|
87
|
+
}
|
|
88
|
+
case "content_block_stop": {
|
|
89
|
+
const index = event.index;
|
|
90
|
+
blockTypes.delete(index);
|
|
91
|
+
toolUseBlocks.delete(index);
|
|
92
|
+
break;
|
|
93
|
+
}
|
|
94
|
+
case "message_delta": {
|
|
95
|
+
const delta = event.delta;
|
|
96
|
+
const usage = event.usage;
|
|
97
|
+
const outputTokens = usage?.output_tokens ?? 0;
|
|
98
|
+
yield {
|
|
99
|
+
type: "usage",
|
|
100
|
+
inputTokens,
|
|
101
|
+
outputTokens,
|
|
102
|
+
cacheReadTokens: cacheReadTokens ?? undefined,
|
|
103
|
+
cacheWriteTokens: cacheWriteTokens ?? undefined,
|
|
104
|
+
};
|
|
105
|
+
if (delta?.stop_reason) {
|
|
106
|
+
yield { type: "done", stopReason: mapStopReason(delta.stop_reason) };
|
|
107
|
+
}
|
|
108
|
+
break;
|
|
109
|
+
}
|
|
110
|
+
case "message_stop":
|
|
111
|
+
break;
|
|
112
|
+
case "ping":
|
|
113
|
+
break;
|
|
114
|
+
case "error": {
|
|
115
|
+
const errData = event.error;
|
|
116
|
+
const errMsg = errData?.message ?? errData?.type ?? "Unknown Anthropic error";
|
|
117
|
+
const isOverloaded = errData?.type === "overloaded_error";
|
|
118
|
+
yield {
|
|
119
|
+
type: "error",
|
|
120
|
+
error: `Anthropic API error: ${errMsg}`,
|
|
121
|
+
retriable: isOverloaded,
|
|
122
|
+
};
|
|
123
|
+
break;
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
function mapStopReason(reason) {
|
|
129
|
+
switch (reason) {
|
|
130
|
+
case "end_turn":
|
|
131
|
+
return "end_turn";
|
|
132
|
+
case "tool_use":
|
|
133
|
+
return "tool_use";
|
|
134
|
+
case "max_tokens":
|
|
135
|
+
return "max_tokens";
|
|
136
|
+
default:
|
|
137
|
+
return "end_turn";
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
//# sourceMappingURL=stream-normalize.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"stream-normalize.js","sourceRoot":"","sources":["../../src/anthropic/stream-normalize.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAgBH;;;;;;GAMG;AAC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AAE,wBAAwB,MAAM,EAAE;oBACvC,SAAS,EAAE,YAAY;iBACxB,CAAC;gBACF,MAAM;YACR,CAAC;QACH,CAAC;IACH,CAAC;AACH,CAAC;AAED,SAAS,aAAa,CAAC,MAAc;IACnC,QAAQ,MAAM,EAAE,CAAC;QACf,KAAK,UAAU;YACb,OAAO,UAAU,CAAC;QACpB,KAAK,UAAU;YACb,OAAO,UAAU,CAAC;QACpB,KAAK,YAAY;YACf,OAAO,YAAY,CAAC;QACtB;YACE,OAAO,UAAU,CAAC;IACtB,CAAC;AACH,CAAC"}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* OpenAI-compatible Embedder implementation.
|
|
3
|
+
*
|
|
4
|
+
* Implements the M8 `Embedder` port using OpenAI's /v1/embeddings API.
|
|
5
|
+
* Compatible with any OpenAI-compatible gateway via configurable baseURL.
|
|
6
|
+
* Errors are normalized (no exceptions thrown).
|
|
7
|
+
*/
|
|
8
|
+
import type { Embedder } from "@voltojs/core";
|
|
9
|
+
export interface OpenAIEmbedderOptions {
|
|
10
|
+
/** Base URL of the OpenAI-compatible API (default: https://api.openai.com) */
|
|
11
|
+
baseURL?: string;
|
|
12
|
+
/** API key for Authorization header */
|
|
13
|
+
apiKey: string;
|
|
14
|
+
/** Embedding model (default: text-embedding-3-small) */
|
|
15
|
+
model?: string;
|
|
16
|
+
}
|
|
17
|
+
export declare function createOpenAIEmbedder(opts: OpenAIEmbedderOptions): Embedder;
|
|
18
|
+
//# sourceMappingURL=embedder.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"embedder.d.ts","sourceRoot":"","sources":["../../src/embedder-openai/embedder.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AAE9C,MAAM,WAAW,qBAAqB;IACpC,8EAA8E;IAC9E,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,uCAAuC;IACvC,MAAM,EAAE,MAAM,CAAC;IACf,wDAAwD;IACxD,KAAK,CAAC,EAAE,MAAM,CAAC;CAChB;AAED,wBAAgB,oBAAoB,CAAC,IAAI,EAAE,qBAAqB,GAAG,QAAQ,CA2C1E"}
|