@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.
Files changed (63) hide show
  1. package/LICENSE +21 -0
  2. package/dist/anthropic/index.d.ts +5 -0
  3. package/dist/anthropic/index.d.ts.map +1 -0
  4. package/dist/anthropic/index.js +3 -0
  5. package/dist/anthropic/index.js.map +1 -0
  6. package/dist/anthropic/model-port.d.ts +34 -0
  7. package/dist/anthropic/model-port.d.ts.map +1 -0
  8. package/dist/anthropic/model-port.js +337 -0
  9. package/dist/anthropic/model-port.js.map +1 -0
  10. package/dist/anthropic/stream-normalize.d.ts +27 -0
  11. package/dist/anthropic/stream-normalize.d.ts.map +1 -0
  12. package/dist/anthropic/stream-normalize.js +140 -0
  13. package/dist/anthropic/stream-normalize.js.map +1 -0
  14. package/dist/embedder-openai/embedder.d.ts +18 -0
  15. package/dist/embedder-openai/embedder.d.ts.map +1 -0
  16. package/dist/embedder-openai/embedder.js +44 -0
  17. package/dist/embedder-openai/embedder.js.map +1 -0
  18. package/dist/embedder-openai/index.d.ts +3 -0
  19. package/dist/embedder-openai/index.d.ts.map +1 -0
  20. package/dist/embedder-openai/index.js +2 -0
  21. package/dist/embedder-openai/index.js.map +1 -0
  22. package/dist/openai/index.d.ts +5 -0
  23. package/dist/openai/index.d.ts.map +1 -0
  24. package/dist/openai/index.js +3 -0
  25. package/dist/openai/index.js.map +1 -0
  26. package/dist/openai/model-port.d.ts +37 -0
  27. package/dist/openai/model-port.d.ts.map +1 -0
  28. package/dist/openai/model-port.js +330 -0
  29. package/dist/openai/model-port.js.map +1 -0
  30. package/dist/openai/stream-normalize.d.ts +70 -0
  31. package/dist/openai/stream-normalize.d.ts.map +1 -0
  32. package/dist/openai/stream-normalize.js +185 -0
  33. package/dist/openai/stream-normalize.js.map +1 -0
  34. package/dist/pi-ai/catalog.d.ts +40 -0
  35. package/dist/pi-ai/catalog.d.ts.map +1 -0
  36. package/dist/pi-ai/catalog.js +38 -0
  37. package/dist/pi-ai/catalog.js.map +1 -0
  38. package/dist/pi-ai/collection.d.ts +133 -0
  39. package/dist/pi-ai/collection.d.ts.map +1 -0
  40. package/dist/pi-ai/collection.js +532 -0
  41. package/dist/pi-ai/collection.js.map +1 -0
  42. package/dist/pi-ai/context.d.ts +15 -0
  43. package/dist/pi-ai/context.d.ts.map +1 -0
  44. package/dist/pi-ai/context.js +414 -0
  45. package/dist/pi-ai/context.js.map +1 -0
  46. package/dist/pi-ai/index.d.ts +8 -0
  47. package/dist/pi-ai/index.d.ts.map +1 -0
  48. package/dist/pi-ai/index.js +4 -0
  49. package/dist/pi-ai/index.js.map +1 -0
  50. package/dist/pi-ai/model-port.d.ts +45 -0
  51. package/dist/pi-ai/model-port.d.ts.map +1 -0
  52. package/dist/pi-ai/model-port.js +168 -0
  53. package/dist/pi-ai/model-port.js.map +1 -0
  54. package/dist/pi-ai/replay.d.ts +46 -0
  55. package/dist/pi-ai/replay.d.ts.map +1 -0
  56. package/dist/pi-ai/replay.js +99 -0
  57. package/dist/pi-ai/replay.js.map +1 -0
  58. package/dist/pi-ai/stream-normalize.d.ts +13 -0
  59. package/dist/pi-ai/stream-normalize.d.ts.map +1 -0
  60. package/dist/pi-ai/stream-normalize.js +118 -0
  61. package/dist/pi-ai/stream-normalize.js.map +1 -0
  62. package/package.json +48 -3
  63. package/README.md +0 -3
@@ -0,0 +1,185 @@
1
+ /**
2
+ * stream-normalize — converts raw OpenAI streaming chunks into core ModelEvents.
3
+ *
4
+ * Handles:
5
+ * - Text delta accumulation
6
+ * - Reasoning delta (field names vary: reasoning_content / reasoning / thinking)
7
+ * - Tool call incremental streaming (start + delta by index, arguments concatenation)
8
+ * - Usage (prompt_tokens + completion_tokens)
9
+ * - finish_reason → stopReason mapping
10
+ */
11
+ /**
12
+ * Normalize a stream of raw OpenAI chunks into core ModelEvents.
13
+ * Statefully accumulates tool call arguments across chunks.
14
+ *
15
+ * Some OpenAI-compatible providers (Azure, vLLM, etc.) send the initial
16
+ * tool call chunk without `id` or `name` — those arrive in a later delta.
17
+ * We buffer argument-only chunks and merge them once the identity arrives.
18
+ */
19
+ export async function* normalizeStream(rawChunks, options = {}) {
20
+ const toolCalls = new Map();
21
+ /** Buffer argument deltas for tool call indices that haven't received id/name yet. */
22
+ const pendingArgs = new Map();
23
+ let hasEmittedUsage = false;
24
+ /**
25
+ * Terminal event held back until a requested usage chunk lands. OpenAI
26
+ * sends usage in a final chunk AFTER the chunk carrying `finish_reason`
27
+ * (`stream_options.include_usage`), so emitting `done` immediately would
28
+ * strand that usage behind the terminal event — and consumers such as the
29
+ * Runner's `lastUsage` (reset on `done`) drop it, losing the turn's token
30
+ * record. Usage is therefore always emitted before `done`.
31
+ */
32
+ let pendingStopReason;
33
+ let bufferedUsage;
34
+ /** Buffered usage first, then the terminal event — the order consumers rely on. */
35
+ const flushTerminal = () => {
36
+ const out = [];
37
+ if (bufferedUsage !== undefined) {
38
+ out.push(bufferedUsage);
39
+ bufferedUsage = undefined;
40
+ }
41
+ if (pendingStopReason !== undefined) {
42
+ out.push({ type: "done", stopReason: pendingStopReason });
43
+ pendingStopReason = undefined;
44
+ }
45
+ return out;
46
+ };
47
+ for await (const chunk of rawChunks) {
48
+ // Usage may arrive in the final chunk — either together with
49
+ // finish_reason or, as OpenAI sends it, in a trailing choices-less chunk.
50
+ if (chunk.usage && !hasEmittedUsage) {
51
+ hasEmittedUsage = true;
52
+ const promptTokens = chunk.usage.prompt_tokens ?? 0;
53
+ // Two vendor spellings of the same split. DeepSeek reports hit/miss
54
+ // directly; OpenAI reports the cached slice inside prompt_tokens_details
55
+ // while prompt_tokens stays the full prompt.
56
+ const hit = chunk.usage.prompt_cache_hit_tokens;
57
+ const miss = chunk.usage.prompt_cache_miss_tokens;
58
+ const cachedDetail = chunk.usage.prompt_tokens_details?.cached_tokens;
59
+ // prompt_tokens is the authority: a hostile/inconsistent payload that
60
+ // sends prompt_cache_miss_tokens without prompt_cache_hit_tokens must
61
+ // still derive cachedTokens from it, otherwise cacheReadTokens ends up
62
+ // undefined while inputTokens (= miss) silently underreports the real
63
+ // prompt size, breaking the input + cacheRead + cacheWrite === prompt
64
+ // identity downstream.
65
+ const cachedTokens = hit ?? cachedDetail ?? (miss !== undefined ? Math.max(0, promptTokens - miss) : undefined);
66
+ const uncached = Math.max(0, promptTokens - (cachedTokens ?? 0));
67
+ bufferedUsage = {
68
+ type: "usage",
69
+ // inputTokens is the UNCACHED prompt across all providers (Anthropic's
70
+ // input_tokens already has this shape); cached tokens ride in
71
+ // cacheReadTokens, so input + cacheRead + cacheWrite is the full
72
+ // prompt. Reporting prompt_tokens verbatim here made CostMeter price
73
+ // the cached portion twice.
74
+ inputTokens: uncached,
75
+ outputTokens: chunk.usage.completion_tokens ?? 0,
76
+ cacheReadTokens: cachedTokens,
77
+ totalTokens: chunk.usage.total_tokens ?? undefined,
78
+ reasoningTokens: chunk.usage.completion_tokens_details?.reasoning_tokens ?? undefined,
79
+ };
80
+ // A trailing usage chunk after finish_reason is the usual shape: the
81
+ // terminal event was waiting for it, so both can go out now.
82
+ if (pendingStopReason !== undefined) {
83
+ for (const event of flushTerminal())
84
+ yield event;
85
+ }
86
+ }
87
+ const choice = chunk.choices?.[0];
88
+ if (!choice)
89
+ continue;
90
+ const { delta, finish_reason } = choice;
91
+ // Text content
92
+ if (delta.content) {
93
+ yield { type: "text_delta", text: delta.content };
94
+ }
95
+ // Reasoning — try multiple field names for compatibility
96
+ const reasoningText = delta.reasoning_content ?? delta.reasoning ?? delta.thinking ?? null;
97
+ if (reasoningText) {
98
+ yield { type: "reasoning_delta", text: reasoningText };
99
+ }
100
+ // Tool calls — incremental streaming by index
101
+ if (delta.tool_calls) {
102
+ for (const tc of delta.tool_calls) {
103
+ const existing = toolCalls.get(tc.index);
104
+ const hasIdentity = Boolean(tc.id && tc.function?.name);
105
+ if (hasIdentity && !existing) {
106
+ // New tool call — may have buffered argument-only chunks from earlier.
107
+ const buffered = pendingArgs.get(tc.index) ?? "";
108
+ const entry = {
109
+ id: tc.id,
110
+ name: tc.function.name,
111
+ argsAccum: buffered + (tc.function?.arguments ?? ""),
112
+ emittedStart: true,
113
+ };
114
+ toolCalls.set(tc.index, entry);
115
+ pendingArgs.delete(tc.index);
116
+ yield {
117
+ type: "tool_use_start",
118
+ toolCallId: entry.id,
119
+ toolName: entry.name,
120
+ };
121
+ // Emit any buffered args first, then current chunk's args.
122
+ if (entry.argsAccum) {
123
+ yield {
124
+ type: "tool_use_delta",
125
+ toolCallId: entry.id,
126
+ argumentsDelta: entry.argsAccum,
127
+ };
128
+ }
129
+ }
130
+ else if (existing) {
131
+ // Arguments delta for existing tool call.
132
+ const argsDelta = tc.function?.arguments ?? "";
133
+ if (argsDelta) {
134
+ existing.argsAccum += argsDelta;
135
+ yield {
136
+ type: "tool_use_delta",
137
+ toolCallId: existing.id,
138
+ argumentsDelta: argsDelta,
139
+ };
140
+ }
141
+ }
142
+ else if (!hasIdentity) {
143
+ // Argument-only chunk before identity arrives — buffer it.
144
+ const argsDelta = tc.function?.arguments ?? "";
145
+ if (argsDelta) {
146
+ pendingArgs.set(tc.index, (pendingArgs.get(tc.index) ?? "") + argsDelta);
147
+ }
148
+ }
149
+ }
150
+ }
151
+ // finish_reason: hold the terminal event when a usage chunk was requested
152
+ // but has not landed yet (see pendingStopReason above).
153
+ if (finish_reason) {
154
+ pendingStopReason = mapFinishReason(finish_reason);
155
+ if (bufferedUsage !== undefined || !options.includeUsageRequested) {
156
+ for (const event of flushTerminal())
157
+ yield event;
158
+ }
159
+ }
160
+ }
161
+ // Flush whatever is still buffered: a stream that ends right after
162
+ // finish_reason without a usage chunk, or usage that arrived on a stream
163
+ // that was cut before any terminal event.
164
+ for (const event of flushTerminal())
165
+ yield event;
166
+ // The stream ended without ever yielding a usage chunk, despite the
167
+ // request having asked for one — a silent provider degradation. Fire at
168
+ // most once per stream.
169
+ if (options.includeUsageRequested && !hasEmittedUsage) {
170
+ yield { type: "usage_unavailable" };
171
+ }
172
+ }
173
+ function mapFinishReason(reason) {
174
+ switch (reason) {
175
+ case "stop":
176
+ return "end_turn";
177
+ case "tool_calls":
178
+ return "tool_use";
179
+ case "length":
180
+ return "max_tokens";
181
+ default:
182
+ return "end_turn";
183
+ }
184
+ }
185
+ //# sourceMappingURL=stream-normalize.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"stream-normalize.js","sourceRoot":"","sources":["../../src/openai/stream-normalize.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AA8DH;;;;;;;GAOG;AACH,MAAM,CAAC,KAAK,SAAS,CAAC,CAAC,eAAe,CACpC,SAAwC,EACxC,UAAkC,EAAE;IAEpC,MAAM,SAAS,GAAG,IAAI,GAAG,EAAyB,CAAC;IACnD,sFAAsF;IACtF,MAAM,WAAW,GAAG,IAAI,GAAG,EAAkB,CAAC;IAC9C,IAAI,eAAe,GAAG,KAAK,CAAC;IAC5B;;;;;;;OAOG;IACH,IAAI,iBAAqE,CAAC;IAC1E,IAAI,aAAiE,CAAC;IAEtE,mFAAmF;IACnF,MAAM,aAAa,GAAG,GAAiB,EAAE;QACvC,MAAM,GAAG,GAAiB,EAAE,CAAC;QAC7B,IAAI,aAAa,KAAK,SAAS,EAAE,CAAC;YAChC,GAAG,CAAC,IAAI,CAAC,aAAa,CAAC,CAAC;YACxB,aAAa,GAAG,SAAS,CAAC;QAC5B,CAAC;QACD,IAAI,iBAAiB,KAAK,SAAS,EAAE,CAAC;YACpC,GAAG,CAAC,IAAI,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,UAAU,EAAE,iBAAiB,EAAE,CAAC,CAAC;YAC1D,iBAAiB,GAAG,SAAS,CAAC;QAChC,CAAC;QACD,OAAO,GAAG,CAAC;IACb,CAAC,CAAC;IAEF,IAAI,KAAK,EAAE,MAAM,KAAK,IAAI,SAAS,EAAE,CAAC;QACpC,6DAA6D;QAC7D,0EAA0E;QAC1E,IAAI,KAAK,CAAC,KAAK,IAAI,CAAC,e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C,CAAC;oBAC3E,CAAC;gBACH,CAAC;YACH,CAAC;QACH,CAAC;QAED,0EAA0E;QAC1E,wDAAwD;QACxD,IAAI,aAAa,EAAE,CAAC;YAClB,iBAAiB,GAAG,eAAe,CAAC,aAAa,CAAC,CAAC;YACnD,IAAI,aAAa,KAAK,SAAS,IAAI,CAAC,OAAO,CAAC,qBAAqB,EAAE,CAAC;gBAClE,KAAK,MAAM,KAAK,IAAI,aAAa,EAAE;oBAAE,MAAM,KAAK,CAAC;YACnD,CAAC;QACH,CAAC;IACH,CAAC;IAED,mEAAmE;IACnE,yEAAyE;IACzE,0CAA0C;IAC1C,KAAK,MAAM,KAAK,IAAI,aAAa,EAAE;QAAE,MAAM,KAAK,CAAC;IAEjD,oEAAoE;IACpE,wEAAwE;IACxE,wBAAwB;IACxB,IAAI,OAAO,CAAC,qBAAqB,IAAI,CAAC,eAAe,EAAE,CAAC;QACtD,MAAM,EAAE,IAAI,EAAE,mBAAmB,EAAE,CAAC;IACtC,CAAC;AACH,CAAC;AAED,SAAS,eAAe,CAAC,MAAc;IACrC,QAAQ,MAAM,EAAE,CAAC;QACf,KAAK,MAAM;YACT,OAAO,UAAU,CAAC;QACpB,KAAK,YAAY;YACf,OAAO,UAAU,CAAC;QACpB,KAAK,QAAQ;YACX,OAAO,YAAY,CAAC;QACtB;YACE,OAAO,UAAU,CAAC;IACtB,CAAC;AACH,CAAC"}
@@ -0,0 +1,40 @@
1
+ import type { ModelInputValue, ModelInputLimits } from "@voltojs/core";
2
+ /** A single model surfaced from pi-ai's built-in catalog. */
3
+ export interface CatalogModel {
4
+ code: string;
5
+ name: string;
6
+ contextWindow?: number;
7
+ maxOutputTokens?: number;
8
+ reasoning: boolean;
9
+ /** Input modalities from pi-ai's catalog (verbatim). */
10
+ input?: ModelInputValue;
11
+ /** Provider input limits from pi-ai's catalog (verbatim; absent for most models). */
12
+ inputLimits?: ModelInputLimits;
13
+ }
14
+ /** A single provider surfaced from pi-ai's built-in catalog. */
15
+ export interface CatalogProvider {
16
+ id: string;
17
+ name: string;
18
+ /**
19
+ * The provider's own address, when it declares one. Surfaced so a settings
20
+ * UI can show it as the default a per-provider override would replace —
21
+ * these differ per provider in both shape (`/v1` suffix or not) and API
22
+ * dialect, so it is never synthesized or normalized.
23
+ */
24
+ baseUrl?: string;
25
+ models: CatalogModel[];
26
+ }
27
+ /**
28
+ * Project pi-ai's built-in provider catalog into the shape volto's settings
29
+ * UI can offer. Pure function over pi-ai's statically generated catalog: no
30
+ * network, no I/O.
31
+ *
32
+ * Only providers with `auth.apiKey` are listed. The settings UI can only
33
+ * collect a single API key from the user, so an OAuth-only provider (today,
34
+ * only `openai-codex`) would be unusable there and is excluded. Providers
35
+ * that have both `apiKey` and `oauth` (e.g. `anthropic`, `openrouter`,
36
+ * `xai`) are kept: the apiKey path still works even though OAuth is also
37
+ * available.
38
+ */
39
+ export declare function listCatalogProviders(): CatalogProvider[];
40
+ //# sourceMappingURL=catalog.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"catalog.d.ts","sourceRoot":"","sources":["../../src/pi-ai/catalog.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,eAAe,EAAE,gBAAgB,EAAE,MAAM,eAAe,CAAC;AAEvE,6DAA6D;AAC7D,MAAM,WAAW,YAAY;IAC3B,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,CAAC;IACb,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB,eAAe,CAAC,EAAE,MAAM,CAAC;IACzB,SAAS,EAAE,OAAO,CAAC;IACnB,wDAAwD;IACxD,KAAK,CAAC,EAAE,eAAe,CAAC;IACxB,qFAAqF;IACrF,WAAW,CAAC,EAAE,gBAAgB,CAAC;CAChC;AAED,gEAAgE;AAChE,MAAM,WAAW,eAAe;IAC9B,EAAE,EAAE,MAAM,CAAC;IACX,IAAI,EAAE,MAAM,CAAC;IACb;;;;;OAKG;IACH,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,MAAM,EAAE,YAAY,EAAE,CAAC;CACxB;AAED;;;;;;;;;;;GAWG;AACH,wBAAgB,oBAAoB,IAAI,eAAe,EAAE,CAuBxD"}
@@ -0,0 +1,38 @@
1
+ import { builtinProviders } from "@earendil-works/pi-ai/providers/all";
2
+ /**
3
+ * Project pi-ai's built-in provider catalog into the shape volto's settings
4
+ * UI can offer. Pure function over pi-ai's statically generated catalog: no
5
+ * network, no I/O.
6
+ *
7
+ * Only providers with `auth.apiKey` are listed. The settings UI can only
8
+ * collect a single API key from the user, so an OAuth-only provider (today,
9
+ * only `openai-codex`) would be unusable there and is excluded. Providers
10
+ * that have both `apiKey` and `oauth` (e.g. `anthropic`, `openrouter`,
11
+ * `xai`) are kept: the apiKey path still works even though OAuth is also
12
+ * available.
13
+ */
14
+ export function listCatalogProviders() {
15
+ return builtinProviders()
16
+ .filter((provider) => provider.auth?.apiKey !== undefined)
17
+ .map((provider) => ({
18
+ id: provider.id,
19
+ name: provider.name,
20
+ baseUrl: provider.baseUrl,
21
+ models: provider.getModels().map((model) => ({
22
+ code: model.id,
23
+ name: model.name,
24
+ contextWindow: model.contextWindow,
25
+ maxOutputTokens: model.maxTokens,
26
+ // Taken directly from pi-ai's model metadata, not inferred.
27
+ reasoning: model.reasoning,
28
+ // Verbatim from pi-ai's model metadata, same as reasoning above.
29
+ input: model.input,
30
+ ...(model.inputLimits === undefined ? {} : { inputLimits: model.inputLimits }),
31
+ })),
32
+ }))
33
+ // Dynamic providers (e.g. "radius") report no models until
34
+ // refreshModels() runs; a settings UI has nothing to offer for them yet,
35
+ // so exclude providers with an empty static model list.
36
+ .filter((provider) => provider.models.length > 0);
37
+ }
38
+ //# sourceMappingURL=catalog.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"catalog.js","sourceRoot":"","sources":["../../src/pi-ai/catalog.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,MAAM,qCAAqC,CAAC;AA8BvE;;;;;;;;;;;GAWG;AACH,MAAM,UAAU,oBAAoB;IAClC,OAAO,gBAAgB,EAAE;SACtB,MAAM,CAAC,CAAC,QAAQ,EAAE,EAAE,CAAC,QAAQ,CAAC,IAAI,EAAE,MAAM,KAAK,SAAS,CAAC;SACzD,GAAG,CAAC,CAAC,QAAQ,EAAE,EAAE,CAAC,CAAC;QAClB,EAAE,EAAE,QAAQ,CAAC,EAAE;QACf,IAAI,EAAE,QAAQ,CAAC,IAAI;QACnB,OAAO,EAAE,QAAQ,CAAC,OAAO;QACzB,MAAM,EAAE,QAAQ,CAAC,SAAS,EAAE,CAAC,GAAG,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,CAAC;YAC3C,IAAI,EAAE,KAAK,CAAC,EAAE;YACd,IAAI,EAAE,KAAK,CAAC,IAAI;YAChB,aAAa,EAAE,KAAK,CAAC,aAAa;YAClC,eAAe,EAAE,KAAK,CAAC,SAAS;YAChC,4DAA4D;YAC5D,SAAS,EAAE,KAAK,CAAC,SAAS;YAC1B,iEAAiE;YACjE,KAAK,EAAE,KAAK,CAAC,KAAK;YAClB,GAAG,CAAC,KAAK,CAAC,WAAW,KAAK,SAAS,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,WAAW,EAAE,KAAK,CAAC,WAAW,EAAE,CAAC;SAC/E,CAAC,CAAC;KACJ,CAAC,CAAC;QACH,2DAA2D;QAC3D,yEAAyE;QACzE,wDAAwD;SACvD,MAAM,CAAC,CAAC,QAAQ,EAAE,EAAE,CAAC,QAAQ,CAAC,MAAM,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC;AACtD,CAAC"}
@@ -0,0 +1,133 @@
1
+ import { type Api, type Model, type Models, type MutableModels } from "@earendil-works/pi-ai";
2
+ import type { ModelCacheCapability, ModelInputValue, ResolvedModelInfo } from "@voltojs/core";
3
+ /** The three protocols Volto exposes as first-class routes. */
4
+ export type ProviderProtocol = "openai-completions" | "openai-responses" | "anthropic-messages";
5
+ /** `auto` is a config-only value; resolution must produce one concrete protocol. */
6
+ export type ConfiguredProtocol = ProviderProtocol | "auto";
7
+ export interface ProviderAuth {
8
+ kind: "bearer" | "x-api-key" | "header";
9
+ /** Header name for `kind: "header"`; the value comes from the apiKey. */
10
+ name?: string;
11
+ }
12
+ export declare function isProviderProtocol(value: unknown): value is ProviderProtocol;
13
+ /**
14
+ * Resolve one model's protocol without side effects.
15
+ *
16
+ * Priority: model override > provider default > catalog api > custom default.
17
+ * `auto` at the provider level means "follow the catalog"; custom providers
18
+ * have no catalog entry, so they fall back to openai-completions.
19
+ */
20
+ export declare function resolveProviderProtocol(config: PiAiProviderConfig, model: string, catalogApi?: string): ProviderProtocol | undefined;
21
+ /**
22
+ * One configured pi-ai provider. Either a reference into pi-ai's built-in
23
+ * catalog (`kind: "catalog"`) or a hand-declared OpenAI-completions gateway
24
+ * (`kind: "custom"`, unchanged from phase two aside from the new `reasoning`
25
+ * flag on each model).
26
+ */
27
+ export type PiAiProviderConfig = {
28
+ kind: "catalog";
29
+ /**
30
+ * volto-side config id. NOT the key models are registered under — a
31
+ * catalog provider registers under its pi-ai id, see
32
+ * {@link registryProviderId}.
33
+ */
34
+ id: string;
35
+ /** pi-ai's built-in provider id, e.g. "openai", "deepseek". */
36
+ piProvider: string;
37
+ apiKey?: string;
38
+ /**
39
+ * Replaces the catalog provider's own address (a self-hosted mirror or
40
+ * a corporate gateway). Used verbatim — see {@link buildCatalogProvider}.
41
+ */
42
+ baseURL?: string;
43
+ /** User-selected model codes; an empty array means no usable models. */
44
+ modelCodes: string[];
45
+ /** Provider-level protocol default; `auto` follows the catalog api. */
46
+ protocol?: ConfiguredProtocol;
47
+ /** Per-model protocol override, winning over `protocol`. */
48
+ modelProtocols?: Record<string, ProviderProtocol>;
49
+ /** How the API key is sent; omitted derives from the protocol. */
50
+ auth?: ProviderAuth;
51
+ /** Extra non-secret request headers. */
52
+ headers?: Record<string, string>;
53
+ /**
54
+ * Per-model user override of pi-ai's catalog `reasoning` flag.
55
+ * `false` forces the model non-reasoning; `true` forces reasoning
56
+ * (default thinking levels when the catalog itself declares none).
57
+ * A model absent from this map keeps the catalog's own value.
58
+ */
59
+ reasoningOverrides?: Record<string, boolean>;
60
+ /**
61
+ * Per-model user override of pi-ai's catalog `input` modalities.
62
+ * Absent = follow the catalog. Valid values: ["text"] / ["text","image"]
63
+ * (the store validates before this layer).
64
+ */
65
+ inputOverrides?: Record<string, ModelInputValue>;
66
+ } | {
67
+ kind: "custom";
68
+ id: string;
69
+ name?: string;
70
+ baseURL: string;
71
+ apiKey?: string;
72
+ /** Default protocol for every model; omitted means openai-completions. */
73
+ protocol?: ProviderProtocol;
74
+ /** How the API key is sent; omitted derives from the protocol. */
75
+ auth?: ProviderAuth;
76
+ /** Extra non-secret request headers. */
77
+ headers?: Record<string, string>;
78
+ models: Array<{
79
+ code: string;
80
+ name?: string;
81
+ /** Per-model protocol override. */
82
+ protocol?: ProviderProtocol;
83
+ contextWindow?: number;
84
+ maxOutputTokens?: number;
85
+ reasoning?: boolean;
86
+ /**
87
+ * Input modalities for a hand-declared gateway model, tri-state
88
+ * (spec §7 / D2): absent means UNKNOWN, and UNKNOWN still sends
89
+ * images — a vanished image path is harder to diagnose than a
90
+ * provider 400. Only an explicit `["text"]` gates them off.
91
+ */
92
+ input?: ModelInputValue;
93
+ }>;
94
+ };
95
+ /**
96
+ * Assemble a pi-ai `Models` collection from volto's configured providers.
97
+ * `kind: "custom"` entries become a hand-declared gateway via
98
+ * `createProvider`, exactly as in phase two. `kind: "catalog"` entries
99
+ * register a restricted view of one of pi-ai's built-in providers — see
100
+ * {@link buildCatalogProvider}.
101
+ */
102
+ /**
103
+ * The id one configured provider is registered under in the `Models`
104
+ * collection, which is what `getModel`/`describeModelInfo` must be called
105
+ * with — pi-ai's own provider id for catalog entries (see
106
+ * {@link buildCatalogProvider}), the config id for hand-declared ones.
107
+ */
108
+ export declare function registryProviderId(providers: PiAiProviderConfig[], configId: string): string;
109
+ export declare function buildModels(providers: PiAiProviderConfig[]): MutableModels;
110
+ /**
111
+ * API key to inject on each `streamSimple`/`stream` request for a configured
112
+ * provider. Catalog providers use per-request injection (rule 2): the key is
113
+ * read straight from the live config on every call, never baked into an auth
114
+ * object. Custom providers already bake their key into a `fixedApiKeyAuth`
115
+ * at `buildModels()` time, so this returns `undefined` for them and the
116
+ * caller simply omits the request-level `apiKey` option.
117
+ */
118
+ export declare function resolveConfiguredApiKey(providers: PiAiProviderConfig[], providerId: string): string | undefined;
119
+ /** Cache support implied by the wire protocol and pi-ai's promptCache metadata. */
120
+ export declare function cacheCapability(model: Model<Api>): ModelCacheCapability;
121
+ /**
122
+ * Resolve capability metadata for one exact model. `reasoning` is present
123
+ * only when the model is flagged `reasoning: true` and
124
+ * `getSupportedThinkingLevels()` yields at least one non-"off" level; never a
125
+ * synthesized off-only list (an off-only control would be byte-identical to
126
+ * omitting the option entirely).
127
+ *
128
+ * `input` is a tri-state: catalog models publish the catalog's own
129
+ * `input` modalities (a fact either way); custom and synthesized models
130
+ * publish only an explicit config declaration — absence stays UNKNOWN.
131
+ */
132
+ export declare function describeModelInfo(models: Models, providerId: string, code: string): ResolvedModelInfo;
133
+ //# sourceMappingURL=collection.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"collection.d.ts","sourceRoot":"","sources":["../../src/pi-ai/collection.ts"],"names":[],"mappings":"AAAA,OAAO,EAIL,KAAK,GAAG,EAER,KAAK,KAAK,EACV,KAAK,MAAM,EACX,KAAK,aAAa,EAGnB,MAAM,uBAAuB,CAAC;AAK/B,OAAO,KAAK,EACV,oBAAoB,EACpB,eAAe,EAGf,iBAAiB,EAClB,MAAM,eAAe,CAAC;AAEvB,+DAA+D;AAC/D,MAAM,MAAM,gBAAgB,GACxB,oBAAoB,GACpB,kBAAkB,GAClB,oBAAoB,CAAC;AAEzB,oFAAoF;AACpF,MAAM,MAAM,kBAAkB,GAAG,gBAAgB,GAAG,MAAM,CAAC;AAE3D,MAAM,WAAW,YAAY;IAC3B,IAAI,EAAE,QAAQ,G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