@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
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
import { buildModels, cacheCapability, describeModelInfo, registryProviderId, resolveConfiguredApiKey, } from "./collection.js";
|
|
2
|
+
import { toPiContext } from "./context.js";
|
|
3
|
+
import { normalizeStream } from "./stream-normalize.js";
|
|
4
|
+
/**
|
|
5
|
+
* Build a volto ModelPort backed by pi-ai. `req.model` is treated as a model
|
|
6
|
+
* code within the single configured route (`defaultProviderId`, or the sole
|
|
7
|
+
* provider when only one is configured). Fails synchronously at construction
|
|
8
|
+
* time if the route is ambiguous — see resolveProviderId.
|
|
9
|
+
*/
|
|
10
|
+
export function createPiAiModelPort(opts) {
|
|
11
|
+
const configId = resolveProviderId(opts);
|
|
12
|
+
const models = buildModels(opts.providers);
|
|
13
|
+
const apiKey = resolveConfiguredApiKey(opts.providers, configId);
|
|
14
|
+
const config = opts.providers.find((provider) => provider.id === configId);
|
|
15
|
+
// Lookups go through the registry key, which for a catalog provider is
|
|
16
|
+
// pi-ai's own provider id rather than volto's config id (collection.ts).
|
|
17
|
+
return createPiAiModelPortFromModels(models, registryProviderId(opts.providers, configId), opts.defaultModel, apiKey, config === undefined
|
|
18
|
+
? undefined
|
|
19
|
+
: {
|
|
20
|
+
...(config.auth === undefined ? {} : { auth: config.auth }),
|
|
21
|
+
...(config.headers === undefined ? {} : { headers: config.headers }),
|
|
22
|
+
...(config.apiKey === undefined ? {} : { apiKey: config.apiKey }),
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Same as {@link createPiAiModelPort}, but takes an already-built `Models`
|
|
27
|
+
* collection directly. Exported (not re-exported from index.ts) so tests can
|
|
28
|
+
* inject a `Models` collection carrying a faux/mocked provider without going
|
|
29
|
+
* through real network-backed provider construction — see model-port.test.ts.
|
|
30
|
+
*/
|
|
31
|
+
export function createPiAiModelPortFromModels(models, providerId, defaultModel,
|
|
32
|
+
/**
|
|
33
|
+
* API key injected into every `streamSimple` call for this route (rule 2 —
|
|
34
|
+
* catalog providers carry no baked-in auth; the key travels per request).
|
|
35
|
+
* Undefined for custom providers, whose key is already baked into their
|
|
36
|
+
* `fixedApiKeyAuth` by `buildModels()`.
|
|
37
|
+
*/
|
|
38
|
+
requestApiKey, route) {
|
|
39
|
+
return {
|
|
40
|
+
async *stream(req) {
|
|
41
|
+
// Entire body is wrapped in try/catch so that any throw (e.g. from
|
|
42
|
+
// describeModelInfo on an unknown model) is converted into an error
|
|
43
|
+
// event rather than escaping the generator. This is a belt-and-braces
|
|
44
|
+
// measure: an async generator function does not execute any of its
|
|
45
|
+
// body until the first `.next()` call, so a throw before the first
|
|
46
|
+
// `yield` already surfaces as a rejected promise (from that `.next()`
|
|
47
|
+
// call), never as a synchronous throw at `stream(req)` call time. The
|
|
48
|
+
// try/catch here does not change that timing; it exists so that a
|
|
49
|
+
// throw is *always* turned into a `{ type: "error" }` event fed back
|
|
50
|
+
// into the ModelPort contract, consistent with how normalizeStream and
|
|
51
|
+
// the rest of this adapter never let a provider failure surface as an
|
|
52
|
+
// uncaught exception.
|
|
53
|
+
try {
|
|
54
|
+
// Step 1: resolve req.model to an exact provider/model.
|
|
55
|
+
const code = req.model ?? defaultModel;
|
|
56
|
+
const model = code === undefined ? undefined : models.getModel(providerId, code);
|
|
57
|
+
if (model === undefined) {
|
|
58
|
+
yield {
|
|
59
|
+
type: "error",
|
|
60
|
+
error: `pi-ai: unable to resolve model "${code ?? "<none>"}" for provider "${providerId}"`,
|
|
61
|
+
retriable: false,
|
|
62
|
+
};
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
// Step 2: validate req.reasoningEffort against the model's declared
|
|
66
|
+
// efforts BEFORE any I/O. describeModelInfo re-looks-up the model,
|
|
67
|
+
// but the model is already known to exist (checked above), so this
|
|
68
|
+
// call cannot throw here.
|
|
69
|
+
if (req.reasoningEffort !== undefined) {
|
|
70
|
+
const info = describeModelInfo(models, providerId, code);
|
|
71
|
+
const supported = info.reasoning?.efforts.map((e) => e.id) ?? [];
|
|
72
|
+
if (!supported.includes(req.reasoningEffort)) {
|
|
73
|
+
yield {
|
|
74
|
+
type: "error",
|
|
75
|
+
error: `pi-ai: model "${code}" does not support reasoning effort "${req.reasoningEffort}"; ` +
|
|
76
|
+
`supported: ${supported.length > 0 ? supported.join(", ") : "(none)"}`,
|
|
77
|
+
retriable: false,
|
|
78
|
+
};
|
|
79
|
+
return;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
// Step 2b: resolve auth headers for this protocol. A bearer route
|
|
83
|
+
// must suppress the Anthropic SDK's default x-api-key; pi-ai treats a
|
|
84
|
+
// null header value as "remove this default".
|
|
85
|
+
const auth = route?.auth ?? (model.api === "anthropic-messages"
|
|
86
|
+
? { kind: "x-api-key" }
|
|
87
|
+
: { kind: "bearer" });
|
|
88
|
+
const routeHeaders = { ...(route?.headers ?? {}) };
|
|
89
|
+
// Only an explicitly configured auth scheme may synthesize headers.
|
|
90
|
+
// Untouched catalog providers keep their built-in SDK auth (and
|
|
91
|
+
// providers like Google/Bedrock must not receive a stray Bearer).
|
|
92
|
+
if (route?.auth !== undefined && route.apiKey !== undefined) {
|
|
93
|
+
if (auth.kind === "bearer") {
|
|
94
|
+
routeHeaders["authorization"] = `Bearer ${route.apiKey}`;
|
|
95
|
+
// Anthropic defaults to x-api-key; suppress it so a bearer-only
|
|
96
|
+
// compatible gateway never receives two credentials.
|
|
97
|
+
routeHeaders["x-api-key"] = null;
|
|
98
|
+
}
|
|
99
|
+
else if (auth.kind === "x-api-key") {
|
|
100
|
+
routeHeaders["x-api-key"] = route.apiKey;
|
|
101
|
+
// The OpenAI SDK would also emit Bearer from the resolved key.
|
|
102
|
+
routeHeaders["authorization"] = null;
|
|
103
|
+
}
|
|
104
|
+
else if (auth.kind === "header" && auth.name !== undefined) {
|
|
105
|
+
routeHeaders[auth.name] = route.apiKey;
|
|
106
|
+
// A custom header is the only credential this route wants.
|
|
107
|
+
routeHeaders["authorization"] = null;
|
|
108
|
+
routeHeaders["x-api-key"] = null;
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
// Step 3: translate core's Message[] into a pi-ai Context.
|
|
112
|
+
const context = toPiContext(req, model);
|
|
113
|
+
// Step 4: invoke pi-ai's streaming call.
|
|
114
|
+
// maxTokens/temperature ARE forwarded — per api-notes.md item 2,
|
|
115
|
+
// `SimpleStreamOptions extends StreamOptions`, which declares both
|
|
116
|
+
// `temperature` and `maxTokens` as real per-request fields (not
|
|
117
|
+
// model-level-only). `reasoning` is omitted entirely when no
|
|
118
|
+
// effort was requested: `SimpleStreamOptions.reasoning` is typed as
|
|
119
|
+
// `ThinkingLevel`, which has no "off" member (only the model-level
|
|
120
|
+
// `ModelThinkingLevel` does) — passing the literal "off" would be a
|
|
121
|
+
// type error, so "no reasoning" means omitting the option, not
|
|
122
|
+
// sending an explicit off value.
|
|
123
|
+
const events = models.streamSimple(model, context, {
|
|
124
|
+
signal: req.abortSignal,
|
|
125
|
+
...(requestApiKey !== undefined ? { apiKey: requestApiKey } : {}),
|
|
126
|
+
...(req.reasoningEffort !== undefined
|
|
127
|
+
? { reasoning: req.reasoningEffort }
|
|
128
|
+
: {}),
|
|
129
|
+
...(req.maxTokens !== undefined ? { maxTokens: req.maxTokens } : {}),
|
|
130
|
+
...(req.temperature !== undefined ? { temperature: req.temperature } : {}),
|
|
131
|
+
// `cacheBreakpoints` cannot be forwarded index-by-index: pi-ai's
|
|
132
|
+
// public request options expose only `cacheRetention`, and its
|
|
133
|
+
// adapters place Anthropic-style cache_control markers themselves
|
|
134
|
+
// (system prompt, last tool, last user/assistant text) — the same
|
|
135
|
+
// prefix-plus-tail a multi-turn request needs. Passing the retention
|
|
136
|
+
// when the kernel asked for caching keeps that on and makes the
|
|
137
|
+
// intent explicit instead of relying on pi-ai's implicit default.
|
|
138
|
+
...((req.cacheBreakpoints?.length ?? 0) > 0
|
|
139
|
+
? { cacheRetention: "short" }
|
|
140
|
+
: {}),
|
|
141
|
+
...(Object.keys(routeHeaders).length > 0 ? { headers: routeHeaders } : {}),
|
|
142
|
+
});
|
|
143
|
+
// Step 5: normalize pi-ai's event stream into volto's ModelEvent stream.
|
|
144
|
+
yield* normalizeStream(events, model.id, cacheCapability(model).mode);
|
|
145
|
+
}
|
|
146
|
+
catch (err) {
|
|
147
|
+
yield {
|
|
148
|
+
type: "error",
|
|
149
|
+
error: err instanceof Error ? err.message : String(err),
|
|
150
|
+
retriable: false,
|
|
151
|
+
};
|
|
152
|
+
}
|
|
153
|
+
},
|
|
154
|
+
describeModel: async (model) => describeModelInfo(models, providerId, model),
|
|
155
|
+
};
|
|
156
|
+
}
|
|
157
|
+
function resolveProviderId(opts) {
|
|
158
|
+
if (opts.defaultProviderId !== undefined) {
|
|
159
|
+
return opts.defaultProviderId;
|
|
160
|
+
}
|
|
161
|
+
if (opts.providers.length === 1 && opts.providers[0] !== undefined) {
|
|
162
|
+
return opts.providers[0].id;
|
|
163
|
+
}
|
|
164
|
+
throw new Error("createPiAiModelPort: multiple providers configured but no defaultProviderId given; " +
|
|
165
|
+
"cross-provider routing is not supported in this phase. Pass exactly one provider, " +
|
|
166
|
+
"or set defaultProviderId to name the route.");
|
|
167
|
+
}
|
|
168
|
+
//# sourceMappingURL=model-port.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"model-port.js","sourceRoot":"","sources":["../../src/pi-ai/model-port.ts"],"names":[],"mappings":"AAEA,OAAO,EACL,WAAW,EACX,eAAe,EACf,iBAAiB,EACjB,kBAAkB,EAClB,uBAAuB,GAGxB,MAAM,iBAAiB,CAAC;AACzB,OAAO,EAAE,WAAW,EAAE,MAAM,cAAc,CAAC;AAC3C,OAAO,EAAE,eAAe,EAAE,MAAM,uBAAuB,CAAC;AAwBxD;;;;;GAKG;AACH,MAAM,UAAU,mBAAmB,CAAC,IAAgC;IAClE,MAAM,QAAQ,GAAG,iBAAiB,CAAC,IAAI,CAAC,CAAC;IACzC,MAAM,MAAM,GAAG,WAAW,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC;IAC3C,MAAM,MAAM,GAAG,uBAAuB,CAAC,IAAI,CAAC,SAAS,EAAE,QAAQ,CAAC,CAAC;IACjE,MAAM,MAAM,GAAG,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,CAAC,QAAQ,EAAE,EAAE,CAAC,QAAQ,CAAC,EAAE,KAAK,QAAQ,CAAC,CAAC;IAC3E,uEAAuE;IACvE,yEAAyE;IACzE,OAAO,6BAA6B,CAClC,MAAM,EACN,kBAAkB,CAAC,IAAI,CAAC,SAAS,EAAE,QAAQ,CAAC,EAC5C,IAAI,CAAC,YAAY,EACjB,MAAM,EACN,MAAM,KAAK,SAAS;QAClB,CAAC,CAAC,SAAS;QACX,CAAC,CAAC;YACE,GAAG,CAAC,MAAM,CAAC,IAAI,KAAK,SAAS,CAAC,CAAC,CAAC,EAAE,CAAC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E,UAAU,EAAE,KAAK,CAAC;KAC3B,CAAC;AACxB,CAAC;AAED,SAAS,iBAAiB,CAAC,IAAgC;IACzD,IAAI,IAAI,CAAC,iBAAiB,KAAK,SAAS,EAAE,CAAC;QACzC,OAAO,IAAI,CAAC,iBAAiB,CAAC;IAChC,CAAC;IACD,IAAI,IAAI,CAAC,SAAS,CAAC,MAAM,KAAK,CAAC,IAAI,IAAI,CAAC,SAAS,CAAC,CAAC,CAAC,KAAK,SAAS,EAAE,CAAC;QACnE,OAAO,IAAI,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC;IAC9B,CAAC;IACD,MAAM,IAAI,KAAK,CACb,qFAAqF;QACnF,oFAAoF;QACpF,6CAA6C,CAChD,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import type { AssistantMessage } from "@earendil-works/pi-ai";
|
|
2
|
+
/**
|
|
3
|
+
* Adapter-private, pure-JSON envelope that carries pi-ai's per-block replay
|
|
4
|
+
* signatures (textSignature / thinkingSignature / thoughtSignature) across
|
|
5
|
+
* core's neutral AssistantMessage representation.
|
|
6
|
+
*
|
|
7
|
+
* Core stores and returns this verbatim (as AssistantMessage.modelMeta) and
|
|
8
|
+
* never reads it. It is persisted through a JSONL transcript store and
|
|
9
|
+
* rebuilt in another process, so it must stay pure JSON: no Map, no
|
|
10
|
+
* Uint8Array, no class instances, no undefined-valued keys.
|
|
11
|
+
*
|
|
12
|
+
* The envelope is block-aligned: one entry per assistant content block, in
|
|
13
|
+
* order. Neutral content (text, reasoning text, tool call arguments) is
|
|
14
|
+
* already carried by core's AssistantMessage.content; this envelope only
|
|
15
|
+
* carries the provider-private signature/marker fields.
|
|
16
|
+
*/
|
|
17
|
+
export interface PiReplayEnvelope {
|
|
18
|
+
version: 1;
|
|
19
|
+
model: string;
|
|
20
|
+
blocks: PiReplayBlock[];
|
|
21
|
+
}
|
|
22
|
+
export type PiReplayBlock = {
|
|
23
|
+
type: "text";
|
|
24
|
+
textSignature?: string;
|
|
25
|
+
} | {
|
|
26
|
+
type: "reasoning";
|
|
27
|
+
thinkingSignature?: string;
|
|
28
|
+
redacted?: boolean;
|
|
29
|
+
} | {
|
|
30
|
+
type: "tool_use";
|
|
31
|
+
thoughtSignature?: string;
|
|
32
|
+
};
|
|
33
|
+
/**
|
|
34
|
+
* Build the replay envelope for a finished pi-ai AssistantMessage. One
|
|
35
|
+
* envelope block per content block, in the same order.
|
|
36
|
+
*/
|
|
37
|
+
export declare function toReplayEnvelope(message: AssistantMessage, model: string): PiReplayEnvelope;
|
|
38
|
+
/**
|
|
39
|
+
* Parse and validate a replay envelope from persisted/foreign data.
|
|
40
|
+
*
|
|
41
|
+
* Never throws: malformed input, a model mismatch, or a wrong shape all
|
|
42
|
+
* return `undefined` ("foreign replay"), so the caller can degrade to
|
|
43
|
+
* unsigned blocks rather than failing the turn.
|
|
44
|
+
*/
|
|
45
|
+
export declare function readReplayEnvelope(value: unknown, model: string): PiReplayEnvelope | undefined;
|
|
46
|
+
//# sourceMappingURL=replay.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"replay.d.ts","sourceRoot":"","sources":["../../src/pi-ai/replay.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,uBAAuB,CAAC;AAE9D;;;;;;;;;;;;;;GAcG;AACH,MAAM,WAAW,gBAAgB;IAC/B,OAAO,EAAE,CAAC,CAAC;IACX,KAAK,EAAE,MAAM,CAAC;IACd,MAAM,EAAE,aAAa,EAAE,CAAC;CACzB;AAED,MAAM,MAAM,aAAa,GACrB;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,aAAa,CAAC,EAAE,MAAM,CAAA;CAAE,GACxC;IAAE,IAAI,EAAE,WAAW,CAAC;IAAC,iBAAiB,CAAC,EAAE,MAAM,CAAC;IAAC,QAAQ,CAAC,EAAE,OAAO,CAAA;CAAE,GACrE;IAAE,IAAI,EAAE,UAAU,CAAC;IAAC,gBAAgB,CAAC,EAAE,MAAM,CAAA;CAAE,CAAC;AAIpD;;;GAGG;AACH,wBAAgB,gBAAgB,CAAC,OAAO,EAAE,gBAAgB,EAAE,KAAK,EAAE,MAAM,GAAG,gBAAgB,CAM3F;AAgCD;;;;;;GAMG;AACH,wBAAgB,kBAAkB,CAAC,KAAK,EAAE,OAAO,EAAE,KAAK,EAAE,MAAM,GAAG,gBAAgB,GAAG,SAAS,CA2B9F"}
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Build the replay envelope for a finished pi-ai AssistantMessage. One
|
|
3
|
+
* envelope block per content block, in the same order.
|
|
4
|
+
*/
|
|
5
|
+
export function toReplayEnvelope(message, model) {
|
|
6
|
+
return {
|
|
7
|
+
version: 1,
|
|
8
|
+
model,
|
|
9
|
+
blocks: message.content.map(toReplayBlock),
|
|
10
|
+
};
|
|
11
|
+
}
|
|
12
|
+
function toReplayBlock(block) {
|
|
13
|
+
switch (block.type) {
|
|
14
|
+
case "text":
|
|
15
|
+
return withOptional({ type: "text" }, "textSignature", block.textSignature);
|
|
16
|
+
case "thinking":
|
|
17
|
+
return withOptional(withOptional({ type: "reasoning" }, "thinkingSignature", block.thinkingSignature), "redacted", block.redacted);
|
|
18
|
+
case "toolCall":
|
|
19
|
+
return withOptional({ type: "tool_use" }, "thoughtSignature", block.thoughtSignature);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
// Only set the key when the value is present, so an absent signature never
|
|
23
|
+
// round-trips as an explicit `undefined`-valued key (which JSON.stringify
|
|
24
|
+
// drops silently, but which would still show up via `in`/`Object.keys` on
|
|
25
|
+
// the in-memory object before serialization).
|
|
26
|
+
function withOptional(obj, key, value) {
|
|
27
|
+
if (value === undefined) {
|
|
28
|
+
return obj;
|
|
29
|
+
}
|
|
30
|
+
return { ...obj, [key]: value };
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Parse and validate a replay envelope from persisted/foreign data.
|
|
34
|
+
*
|
|
35
|
+
* Never throws: malformed input, a model mismatch, or a wrong shape all
|
|
36
|
+
* return `undefined` ("foreign replay"), so the caller can degrade to
|
|
37
|
+
* unsigned blocks rather than failing the turn.
|
|
38
|
+
*/
|
|
39
|
+
export function readReplayEnvelope(value, model) {
|
|
40
|
+
if (typeof value !== "object" || value === null) {
|
|
41
|
+
return undefined;
|
|
42
|
+
}
|
|
43
|
+
const candidate = value;
|
|
44
|
+
if (candidate.version !== 1) {
|
|
45
|
+
return undefined;
|
|
46
|
+
}
|
|
47
|
+
if (typeof candidate.model !== "string" || candidate.model !== model) {
|
|
48
|
+
return undefined;
|
|
49
|
+
}
|
|
50
|
+
if (!Array.isArray(candidate.blocks)) {
|
|
51
|
+
return undefined;
|
|
52
|
+
}
|
|
53
|
+
const blocks = [];
|
|
54
|
+
for (const rawBlock of candidate.blocks) {
|
|
55
|
+
const block = readReplayBlock(rawBlock);
|
|
56
|
+
if (block === undefined) {
|
|
57
|
+
return undefined;
|
|
58
|
+
}
|
|
59
|
+
blocks.push(block);
|
|
60
|
+
}
|
|
61
|
+
return { version: 1, model: candidate.model, blocks };
|
|
62
|
+
}
|
|
63
|
+
function readReplayBlock(value) {
|
|
64
|
+
if (typeof value !== "object" || value === null) {
|
|
65
|
+
return undefined;
|
|
66
|
+
}
|
|
67
|
+
const candidate = value;
|
|
68
|
+
switch (candidate.type) {
|
|
69
|
+
case "text": {
|
|
70
|
+
if (candidate.textSignature !== undefined && typeof candidate.textSignature !== "string") {
|
|
71
|
+
return undefined;
|
|
72
|
+
}
|
|
73
|
+
const block = { type: "text" };
|
|
74
|
+
return withOptional(block, "textSignature", candidate.textSignature);
|
|
75
|
+
}
|
|
76
|
+
case "reasoning": {
|
|
77
|
+
if (candidate.thinkingSignature !== undefined &&
|
|
78
|
+
typeof candidate.thinkingSignature !== "string") {
|
|
79
|
+
return undefined;
|
|
80
|
+
}
|
|
81
|
+
if (candidate.redacted !== undefined && typeof candidate.redacted !== "boolean") {
|
|
82
|
+
return undefined;
|
|
83
|
+
}
|
|
84
|
+
const block = { type: "reasoning" };
|
|
85
|
+
return withOptional(withOptional(block, "thinkingSignature", candidate.thinkingSignature), "redacted", candidate.redacted);
|
|
86
|
+
}
|
|
87
|
+
case "tool_use": {
|
|
88
|
+
if (candidate.thoughtSignature !== undefined &&
|
|
89
|
+
typeof candidate.thoughtSignature !== "string") {
|
|
90
|
+
return undefined;
|
|
91
|
+
}
|
|
92
|
+
const block = { type: "tool_use" };
|
|
93
|
+
return withOptional(block, "thoughtSignature", candidate.thoughtSignature);
|
|
94
|
+
}
|
|
95
|
+
default:
|
|
96
|
+
return undefined;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
//# sourceMappingURL=replay.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"replay.js","sourceRoot":"","sources":["../../src/pi-ai/replay.ts"],"names":[],"mappings":"AA8BA;;;GAGG;AACH,MAAM,UAAU,gBAAgB,CAAC,OAAyB,EAAE,KAAa;IACvE,OAAO;QACL,OAAO,EAAE,CAAC;QACV,KAAK;QACL,MAAM,EAAE,OAAO,CAAC,OAAO,CAAC,GAAG,CAAC,aAAa,CAAC;KAC3C,CAAC;AACJ,CAAC;AAED,SAAS,aAAa,CAAC,KAAyB;IAC9C,QAAQ,KAAK,CAAC,IAAI,EAAE,CAAC;QACnB,KAAK,MAAM;YACT,OAAO,YAAY,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,EAAE,eAAe,EAAE,KAAK,CAAC,aAAa,CAAC,CAAC;QAC9E,KAAK,UAAU;YACb,OAAO,YAAY,CACjB,YAAY,CAAC,EAAE,IAAI,EAAE,WAAW,EAAE,EAAE,mBAAmB,EAAE,KAAK,CAAC,iBAAiB,CAAC,EACjF,UAAU,EACV,KAAK,CAAC,QAAQ,CACf,CAAC;QACJ,KAAK,UAAU;YACb,OAAO,YAAY,CAAC,EAAE,IAAI,EAAE,UAAU,EAAE,EAAE,kBAA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CAAC,gBAAgB,KAAK,QAAQ,EAC9C,CAAC;gBACD,OAAO,SAAS,CAAC;YACnB,CAAC;YACD,MAAM,KAAK,GAAkB,EAAE,IAAI,EAAE,UAAU,EAAE,CAAC;YAClD,OAAO,YAAY,CAAC,KAAK,EAAE,kBAAkB,EAAE,SAAS,CAAC,gBAAsC,CAAC,CAAC;QACnG,CAAC;QACD;YACE,OAAO,SAAS,CAAC;IACrB,CAAC;AACH,CAAC"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { AssistantMessageEvent } from "@earendil-works/pi-ai";
|
|
2
|
+
import type { ModelEvent } from "@voltojs/core";
|
|
3
|
+
/**
|
|
4
|
+
* Normalize pi-ai's AssistantMessageEvent stream into volto's neutral
|
|
5
|
+
* ModelEvent stream.
|
|
6
|
+
*
|
|
7
|
+
* pi-ai never throws out of the stream itself; failures arrive as an
|
|
8
|
+
* `{ type: "error" }` event carrying the failed AssistantMessage. This
|
|
9
|
+
* generator preserves that property end to end: it never throws, it only
|
|
10
|
+
* ever yields ModelEvents (including a `{ type: "error" }` one).
|
|
11
|
+
*/
|
|
12
|
+
export declare function normalizeStream(events: AsyncIterable<AssistantMessageEvent>, model: string, cacheMode?: "none" | "implicit" | "explicit"): AsyncIterable<ModelEvent>;
|
|
13
|
+
//# sourceMappingURL=stream-normalize.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"stream-normalize.d.ts","sourceRoot":"","sources":["../../src/pi-ai/stream-normalize.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,uBAAuB,CAAC;AACnE,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,eAAe,CAAC;AAGhD;;;;;;;;GAQG;AACH,wBAAuB,eAAe,CACpC,MAAM,EAAE,aAAa,CAAC,qBAAqB,CAAC,EAC5C,KAAK,EAAE,MAAM,EACb,SAAS,CAAC,EAAE,MAAM,GAAG,UAAU,GAAG,UAAU,GAC3C,aAAa,CAAC,UAAU,CAAC,CAsG3B"}
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import { toReplayEnvelope } from "./replay.js";
|
|
2
|
+
/**
|
|
3
|
+
* Normalize pi-ai's AssistantMessageEvent stream into volto's neutral
|
|
4
|
+
* ModelEvent stream.
|
|
5
|
+
*
|
|
6
|
+
* pi-ai never throws out of the stream itself; failures arrive as an
|
|
7
|
+
* `{ type: "error" }` event carrying the failed AssistantMessage. This
|
|
8
|
+
* generator preserves that property end to end: it never throws, it only
|
|
9
|
+
* ever yields ModelEvents (including a `{ type: "error" }` one).
|
|
10
|
+
*/
|
|
11
|
+
export async function* normalizeStream(events, model, cacheMode) {
|
|
12
|
+
for await (const event of events) {
|
|
13
|
+
switch (event.type) {
|
|
14
|
+
case "text_delta":
|
|
15
|
+
yield { type: "text_delta", text: event.delta };
|
|
16
|
+
break;
|
|
17
|
+
case "thinking_delta":
|
|
18
|
+
yield { type: "reasoning_delta", text: event.delta };
|
|
19
|
+
break;
|
|
20
|
+
case "toolcall_start": {
|
|
21
|
+
// toolcall_start itself carries no name, only contentIndex — the
|
|
22
|
+
// name lives on the matching block of `partial.content` (see
|
|
23
|
+
// api-notes.md item 5 / task-4 brief). Without it, core's reducer
|
|
24
|
+
// treats the following tool_use_delta chunks as argument fragments
|
|
25
|
+
// for an already-open call rather than the start of a new one
|
|
26
|
+
// (it gates "new tool call" on toolCallId + toolCallName both being
|
|
27
|
+
// truthy), so a missing name here would silently swallow the call.
|
|
28
|
+
const block = event.partial.content[event.contentIndex];
|
|
29
|
+
if (block?.type !== "toolCall") {
|
|
30
|
+
// Malformed/unexpected pi-ai stream shape: no toolCall block at
|
|
31
|
+
// this index. Nothing sane to emit — skip rather than yielding a
|
|
32
|
+
// nameless tool_use_start that core would misinterpret.
|
|
33
|
+
break;
|
|
34
|
+
}
|
|
35
|
+
yield { type: "tool_use_start", toolCallId: block.id, toolName: block.name };
|
|
36
|
+
break;
|
|
37
|
+
}
|
|
38
|
+
case "toolcall_delta": {
|
|
39
|
+
const block = event.partial.content[event.contentIndex];
|
|
40
|
+
if (block?.type !== "toolCall") {
|
|
41
|
+
break;
|
|
42
|
+
}
|
|
43
|
+
yield {
|
|
44
|
+
type: "tool_use_delta",
|
|
45
|
+
toolCallId: block.id,
|
|
46
|
+
argumentsDelta: event.delta,
|
|
47
|
+
};
|
|
48
|
+
break;
|
|
49
|
+
}
|
|
50
|
+
case "done": {
|
|
51
|
+
const usage = event.message.usage;
|
|
52
|
+
if (usage) {
|
|
53
|
+
yield {
|
|
54
|
+
type: "usage",
|
|
55
|
+
inputTokens: usage.input,
|
|
56
|
+
outputTokens: usage.output,
|
|
57
|
+
totalTokens: usage.totalTokens,
|
|
58
|
+
cacheReadTokens: usage.cacheRead,
|
|
59
|
+
cacheWriteTokens: usage.cacheWrite,
|
|
60
|
+
// pi-ai normalizes missing cache fields to 0, so a zero cannot
|
|
61
|
+
// distinguish "reported zero" from "not reported". For implicit
|
|
62
|
+
// caches, only a positive bucket proves the provider reported
|
|
63
|
+
// it; a zero stays unknown instead of becoming a fake 0.0% hit
|
|
64
|
+
// rate. Anthropic's explicit protocol always reports both fields,
|
|
65
|
+
// so its zero is meaningful.
|
|
66
|
+
cacheReporting: cacheMode === "none"
|
|
67
|
+
? "unsupported"
|
|
68
|
+
: usage.cacheRead > 0 || usage.cacheWrite > 0
|
|
69
|
+
? "reported"
|
|
70
|
+
: cacheMode === "explicit"
|
|
71
|
+
? "reported"
|
|
72
|
+
: "not-reported",
|
|
73
|
+
...(usage.reasoning !== undefined ? { reasoningTokens: usage.reasoning } : {}),
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
yield {
|
|
77
|
+
type: "done",
|
|
78
|
+
stopReason: mapStopReason(event.reason),
|
|
79
|
+
meta: toReplayEnvelope(event.message, model),
|
|
80
|
+
};
|
|
81
|
+
break;
|
|
82
|
+
}
|
|
83
|
+
case "error": {
|
|
84
|
+
// `event.error` is a full AssistantMessage, not a string/Error — the
|
|
85
|
+
// human-readable text lives at event.error.errorMessage (api-notes.md
|
|
86
|
+
// item 5 / trap #1). An abort is never retriable. For a plain
|
|
87
|
+
// "error" reason, pi-ai gives us nothing beyond a free-text message —
|
|
88
|
+
// no status code, no error class — so there is no reliable signal to
|
|
89
|
+
// decide retriability from. Default to false: spinning retries
|
|
90
|
+
// against a permanently failing endpoint is worse than not retrying
|
|
91
|
+
// a transient one.
|
|
92
|
+
yield {
|
|
93
|
+
type: "error",
|
|
94
|
+
error: event.error.errorMessage ?? `pi-ai stream ${event.reason}`,
|
|
95
|
+
retriable: false,
|
|
96
|
+
};
|
|
97
|
+
break;
|
|
98
|
+
}
|
|
99
|
+
// "start"/"text_start"/"text_end"/"thinking_start"/"thinking_end"/
|
|
100
|
+
// "toolcall_end" carry no information volto's ModelEvent needs beyond
|
|
101
|
+
// what the deltas and the final `done` message already provide.
|
|
102
|
+
default:
|
|
103
|
+
break;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
function mapStopReason(reason) {
|
|
108
|
+
switch (reason) {
|
|
109
|
+
case "toolUse":
|
|
110
|
+
return "tool_use";
|
|
111
|
+
case "length":
|
|
112
|
+
return "max_tokens";
|
|
113
|
+
case "stop":
|
|
114
|
+
case "deferred":
|
|
115
|
+
return "end_turn";
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
//# sourceMappingURL=stream-normalize.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"stream-normalize.js","sourceRoot":"","sources":["../../src/pi-ai/stream-normalize.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAC;AAE/C;;;;;;;;GAQG;AACH,MAAM,CAAC,KAAK,SAAS,CAAC,CAAC,eAAe,CACpC,MAA4C,EAC5C,KAAa,EACb,SAA4C;IAE5C,IAAI,KAAK,EAAE,MAAM,KAAK,IAAI,MAAM,EAAE,CAAC;QACjC,QAAQ,KAA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C,YAAY,IAAI,gBAAgB,KAAK,CAAC,MAAM,EAAE;oBACjE,SAAS,EAAE,KAAK;iBACjB,CAAC;gBACF,MAAM;YACR,CAAC;YAED,mEAAmE;YACnE,sEAAsE;YACtE,gEAAgE;YAChE;gBACE,MAAM;QACV,CAAC;IACH,CAAC;AACH,CAAC;AAED,SAAS,aAAa,CACpB,MAAkE;IAElE,QAAQ,MAAM,EAAE,CAAC;QACf,KAAK,SAAS;YACZ,OAAO,UAAU,CAAC;QACpB,KAAK,QAAQ;YACX,OAAO,YAAY,CAAC;QACtB,KAAK,MAAM,CAAC;QACZ,KAAK,UAAU;YACb,OAAO,UAAU,CAAC;IACtB,CAAC;AACH,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,51 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@voltojs/providers",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"main": "./dist/pi-ai/index.js",
|
|
6
|
+
"types": "./dist/pi-ai/index.d.ts",
|
|
7
|
+
"exports": {
|
|
8
|
+
"./pi-ai": {
|
|
9
|
+
"types": "./dist/pi-ai/index.d.ts",
|
|
10
|
+
"import": "./dist/pi-ai/index.js"
|
|
11
|
+
},
|
|
12
|
+
"./openai": {
|
|
13
|
+
"types": "./dist/openai/index.d.ts",
|
|
14
|
+
"import": "./dist/openai/index.js"
|
|
15
|
+
},
|
|
16
|
+
"./anthropic": {
|
|
17
|
+
"types": "./dist/anthropic/index.d.ts",
|
|
18
|
+
"import": "./dist/anthropic/index.js"
|
|
19
|
+
},
|
|
20
|
+
"./embedder-openai": {
|
|
21
|
+
"types": "./dist/embedder-openai/index.d.ts",
|
|
22
|
+
"import": "./dist/embedder-openai/index.js"
|
|
23
|
+
},
|
|
24
|
+
"./package.json": "./package.json"
|
|
25
|
+
},
|
|
26
|
+
"files": [
|
|
27
|
+
"dist",
|
|
28
|
+
"!dist/**/__tests__"
|
|
29
|
+
],
|
|
30
|
+
"publishConfig": {
|
|
31
|
+
"access": "public"
|
|
32
|
+
},
|
|
33
|
+
"engines": {
|
|
34
|
+
"node": ">=22"
|
|
35
|
+
},
|
|
36
|
+
"license": "MIT",
|
|
37
|
+
"repository": {
|
|
38
|
+
"type": "git",
|
|
39
|
+
"url": "git+https://github.com/voltojs/volto.git"
|
|
40
|
+
},
|
|
41
|
+
"dependencies": {
|
|
42
|
+
"@earendil-works/pi-ai": "^1.5.0",
|
|
43
|
+
"@voltojs/core": "^0.2.0"
|
|
44
|
+
},
|
|
45
|
+
"scripts": {
|
|
46
|
+
"build": "tsc --build",
|
|
47
|
+
"typecheck": "tsc --noEmit",
|
|
48
|
+
"lint": "eslint src/",
|
|
49
|
+
"test": "vitest run"
|
|
50
|
+
}
|
|
6
51
|
}
|
package/README.md
DELETED