@mupt-ai/dari-router 0.2.5 → 1.0.290

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 (48) hide show
  1. package/README.md +148 -55
  2. package/dist/auto_router.d.ts +1 -1
  3. package/dist/auto_router.js.map +1 -1
  4. package/dist/cache_scope.js +5 -5
  5. package/dist/cache_scope.js.map +1 -1
  6. package/dist/dari_policy.d.ts +0 -3
  7. package/dist/dari_policy.js +2 -2
  8. package/dist/dari_policy.js.map +1 -1
  9. package/dist/eval_score_imputation.js +2 -0
  10. package/dist/eval_score_imputation.js.map +1 -1
  11. package/dist/framework_router.d.ts +2 -1
  12. package/dist/framework_router.js +264 -47
  13. package/dist/framework_router.js.map +1 -1
  14. package/dist/framework_types.d.ts +9 -0
  15. package/dist/framework_validation.js +1 -0
  16. package/dist/framework_validation.js.map +1 -1
  17. package/dist/gateway_executor.d.ts +16 -0
  18. package/dist/gateway_executor.js +523 -0
  19. package/dist/gateway_executor.js.map +1 -0
  20. package/dist/index.d.ts +2 -2
  21. package/dist/index.js +1 -1
  22. package/dist/index.js.map +1 -1
  23. package/examples/auto_router.ts +16 -11
  24. package/examples/basic_route.ts +19 -19
  25. package/examples/lease_stores.ts +21 -15
  26. package/examples/managed/README.md +3 -4
  27. package/examples/managed/byok-router/router.yml +4 -4
  28. package/examples/managed/custom-rules-router/router.yml +9 -9
  29. package/examples/managed/dari/router.yml +3 -2
  30. package/package.json +5 -14
  31. package/dist/pi_context.d.ts +0 -4
  32. package/dist/pi_context.js +0 -237
  33. package/dist/pi_context.js.map +0 -1
  34. package/dist/pi_options.d.ts +0 -3
  35. package/dist/pi_options.js +0 -300
  36. package/dist/pi_options.js.map +0 -1
  37. package/dist/pi_output.d.ts +0 -4
  38. package/dist/pi_output.js +0 -328
  39. package/dist/pi_output.js.map +0 -1
  40. package/dist/pi_runtime.d.ts +0 -3
  41. package/dist/pi_runtime.js +0 -163
  42. package/dist/pi_runtime.js.map +0 -1
  43. package/dist/pi_types.d.ts +0 -35
  44. package/dist/pi_types.js +0 -2
  45. package/dist/pi_types.js.map +0 -1
  46. package/examples/managed/eval-router/README.md +0 -18
  47. package/examples/managed/eval-router/router.yml +0 -11
  48. package/examples/pi_runtime.ts +0 -55
@@ -1,300 +0,0 @@
1
- import { isOpenAIResponsesApi } from "./continuation_state.js";
2
- import { RouterFrameworkError } from "./framework_error.js";
3
- import { isRecord } from "./types.js";
4
- const PAYLOAD_PATCH_APIS = new Set([
5
- "anthropic-messages",
6
- "azure-openai-responses",
7
- "openai-completions",
8
- "openai-responses",
9
- ]);
10
- export async function piOptions(runtime, execution, model) {
11
- rejectUnsupportedOptions(execution.request, model);
12
- const apiKey = typeof runtime.apiKey === "string"
13
- ? runtime.apiKey
14
- : await runtime.apiKey({
15
- provider: model.provider,
16
- model: execution.candidate.id,
17
- api: model.api,
18
- purpose: execution.purpose,
19
- });
20
- if (typeof apiKey !== "string" || !apiKey.trim()) {
21
- throw new RouterFrameworkError("configuration", `No API key was provided for Pi model '${execution.candidate.id}'.`, "pi_api_key_missing");
22
- }
23
- const request = execution.request;
24
- const options = {
25
- apiKey,
26
- signal: execution.signal,
27
- ...(runtime.timeoutMs === undefined ? {} : { timeoutMs: runtime.timeoutMs }),
28
- ...(runtime.maxRetries === undefined ? {} : { maxRetries: runtime.maxRetries }),
29
- ...(request.generation.temperature === undefined
30
- ? {}
31
- : { temperature: request.generation.temperature }),
32
- ...(request.generation.maxOutputTokens === undefined
33
- ? {}
34
- : { maxTokens: request.generation.maxOutputTokens }),
35
- ...(request.cacheKey === undefined ? {} : { sessionId: request.cacheKey }),
36
- // Only anthropic-messages reads metadata (and only user_id); no other
37
- // fork API consumes metadata fields.
38
- ...(request.user !== undefined && model.api === "anthropic-messages"
39
- ? { metadata: { user_id: request.user } }
40
- : {}),
41
- ...(execution.reasoningEffort === "off" ? {} : { reasoning: execution.reasoningEffort }),
42
- };
43
- const budget = request.reasoning?.budgetTokens;
44
- if (budget !== undefined &&
45
- execution.reasoningEffort !== "off" &&
46
- execution.reasoningEffort !== "xhigh" &&
47
- execution.reasoningEffort !== "max") {
48
- options.thinkingBudgets = { [execution.reasoningEffort]: budget };
49
- }
50
- const patch = payloadPatch(request, model, execution);
51
- if (patch !== undefined)
52
- options.onPayload = patch;
53
- return options;
54
- }
55
- // Reject controls the provider API cannot represent before starting a request.
56
- function rejectUnsupportedOptions(request, model) {
57
- if (request.generation.stop !== undefined && isOpenAIResponsesApi(model.api)) {
58
- throw unsupportedPiOption("stop sequences", model.api);
59
- }
60
- if (request.generation.topP !== undefined && model.api === "anthropic-messages") {
61
- throw unsupportedPiOption("top_p", model.api);
62
- }
63
- if (typeof request.toolChoice === "object" &&
64
- (model.api === "google-generative-ai" || model.api === "google-vertex")) {
65
- throw unsupportedPiOption("forcing a specific tool with tool_choice", model.api);
66
- }
67
- if (request.toolChoice === "none" && model.api === "bedrock-converse-stream") {
68
- throw unsupportedPiOption("disabling tools with tool_choice", model.api);
69
- }
70
- if (request.responseFormat?.type === "json_object" && model.api === "bedrock-converse-stream") {
71
- throw unsupportedPiOption("response_format json_object", model.api);
72
- }
73
- }
74
- function unsupportedPiOption(option, api) {
75
- return new RouterFrameworkError("invalid_request", `Pi API '${api}' does not support ${option}.`, "pi_option_unsupported");
76
- }
77
- function piResponseFormat(format, api) {
78
- if (api === "anthropic-messages") {
79
- // Anthropic has no json_object mode; use a permissive object schema.
80
- return {
81
- type: "json_schema",
82
- schema: format.type === "json_schema" ? format.schema : { type: "object" },
83
- };
84
- }
85
- if (format.type !== "json_schema")
86
- return format;
87
- const { type: _, ...schema } = format;
88
- return isOpenAIResponsesApi(api)
89
- ? { type: "json_schema", ...schema }
90
- : { type: "json_schema", json_schema: schema };
91
- }
92
- function piToolChoice(choice, api) {
93
- if (api === "anthropic-messages") {
94
- return typeof choice === "string"
95
- ? { type: choice === "required" ? "any" : choice }
96
- : { type: "tool", name: choice.name };
97
- }
98
- if (typeof choice === "string")
99
- return choice;
100
- return isOpenAIResponsesApi(api)
101
- ? { type: "function", name: choice.name }
102
- : { type: "function", function: { name: choice.name } };
103
- }
104
- function payloadPatch(request, model, execution) {
105
- const strictTools = request.tools.some((tool) => tool.strict !== undefined);
106
- const parallel = request.parallelToolCalls;
107
- const imageDetails = imageDetailQueues(request);
108
- const thinkingPatch = model.api === "anthropic-messages" && (request.reasoning?.enabled === false || request.reasoning?.budgetTokens !== undefined);
109
- const imageDetailPatch = imageDetails.size > 0 &&
110
- (isOpenAIResponsesApi(model.api) || model.api === "openai-completions");
111
- if (imageDetails.size > 0 &&
112
- !imageDetailPatch &&
113
- [...imageDetails.values()].some((queue) => queue.some((detail) => detail !== "auto"))) {
114
- throw new RouterFrameworkError("invalid_request", `Pi API '${model.api}' cannot preserve image detail controls.`, "pi_image_detail_unsupported");
115
- }
116
- if (!strictTools &&
117
- parallel === undefined &&
118
- !thinkingPatch &&
119
- !imageDetailPatch &&
120
- request.responseFormat === undefined &&
121
- request.toolChoice === undefined &&
122
- request.generation.topP === undefined &&
123
- request.generation.stop === undefined) {
124
- return undefined;
125
- }
126
- if ((strictTools || parallel !== undefined) && !PAYLOAD_PATCH_APIS.has(model.api)) {
127
- throw new RouterFrameworkError("invalid_request", `Pi API '${model.api}' cannot preserve strict or parallel tool controls.`, "pi_tool_control_unsupported");
128
- }
129
- return (value) => {
130
- if (!isRecord(value))
131
- return undefined;
132
- let payload = value;
133
- const api = model.api;
134
- const google = api === "google-generative-ai" || api === "google-vertex";
135
- const config = isRecord(payload.config) ? payload.config : {};
136
- if (request.generation.topP !== undefined || request.generation.stop !== undefined) {
137
- const stop = request.generation.stop;
138
- const sequences = typeof stop === "string" ? [stop] : stop;
139
- const controls = {
140
- ...(request.generation.topP === undefined ? {} : { [google || api === "bedrock-converse-stream" ? "topP" : "top_p"]: request.generation.topP }),
141
- ...(stop === undefined ? {} : { [google || api === "bedrock-converse-stream" ? "stopSequences" : api === "anthropic-messages" ? "stop_sequences" : "stop"]: api === "openai-completions" ? stop : sequences }),
142
- };
143
- payload = google ? { ...payload, config: { ...config, ...controls } }
144
- : api === "bedrock-converse-stream" ? { ...payload, inferenceConfig: { ...(isRecord(payload.inferenceConfig) ? payload.inferenceConfig : {}), ...controls } }
145
- : { ...payload, ...controls };
146
- }
147
- if (request.responseFormat !== undefined &&
148
- (request.responseFormat.type !== "text" || isOpenAIResponsesApi(api) || api === "openai-completions")) {
149
- const format = piResponseFormat(request.responseFormat, api);
150
- if (google) {
151
- payload = { ...payload, config: { ...(isRecord(payload.config) ? payload.config : {}), responseMimeType: "application/json", ...(request.responseFormat.type === "json_schema" ? { responseJsonSchema: request.responseFormat.schema } : {}) } };
152
- }
153
- else if (api === "bedrock-converse-stream") {
154
- const response = request.responseFormat;
155
- if (response.type === "json_schema")
156
- payload = { ...payload, outputConfig: { textFormat: { type: "json_schema", structure: { jsonSchema: { name: response.name, schema: JSON.stringify(response.schema), ...(response.description === undefined ? {} : { description: response.description }) } } } } };
157
- }
158
- else if (isOpenAIResponsesApi(api)) {
159
- payload = { ...payload, text: { ...(isRecord(payload.text) ? payload.text : {}), format } };
160
- }
161
- else if (api === "anthropic-messages") {
162
- payload = { ...payload, output_config: { ...(isRecord(payload.output_config) ? payload.output_config : {}), format } };
163
- }
164
- else {
165
- payload = { ...payload, response_format: format };
166
- }
167
- }
168
- if (request.toolChoice !== undefined) {
169
- const choice = request.toolChoice;
170
- if (google) {
171
- const mode = choice === "none" ? "NONE" : choice === "required" ? "ANY" : "AUTO";
172
- payload = { ...payload, config: { ...(isRecord(payload.config) ? payload.config : {}), toolConfig: { functionCallingConfig: { mode } } } };
173
- }
174
- else if (api === "bedrock-converse-stream") {
175
- payload = { ...payload, toolConfig: { ...(isRecord(payload.toolConfig) ? payload.toolConfig : {}), toolChoice: typeof choice === "string" ? choice === "required" ? { any: {} } : { auto: {} } : { tool: { name: choice.name } } } };
176
- }
177
- else if (Array.isArray(payload.tools) && payload.tools.length > 0) {
178
- payload = { ...payload, tool_choice: piToolChoice(choice, api) };
179
- }
180
- else {
181
- const { tool_choice: _, ...withoutToolChoice } = payload;
182
- payload = withoutToolChoice;
183
- }
184
- }
185
- if (strictTools)
186
- payload = patchStrictTools(payload, request, model.api);
187
- if (parallel !== undefined)
188
- payload = patchParallelTools(payload, parallel, model.api);
189
- if (imageDetailPatch)
190
- payload = patchImageDetails(payload, imageDetails);
191
- if (thinkingPatch)
192
- payload = patchAnthropicThinking(payload, request);
193
- if (api !== "bedrock-converse-stream" &&
194
- !google &&
195
- (!Array.isArray(payload.tools) || payload.tools.length === 0)) {
196
- const { tool_choice: _, ...withoutToolChoice } = payload;
197
- payload = withoutToolChoice;
198
- }
199
- return payload;
200
- };
201
- }
202
- function patchStrictTools(payload, request, api) {
203
- if (!Array.isArray(payload.tools))
204
- return payload;
205
- const tools = new Map(request.tools.map((tool) => [tool.name, tool]));
206
- return {
207
- ...payload,
208
- tools: payload.tools.map((entry) => {
209
- if (!isRecord(entry))
210
- return entry;
211
- if (api === "anthropic-messages") {
212
- const tool = typeof entry.name === "string" ? tools.get(entry.name) : undefined;
213
- return tool?.strict === undefined ? entry : { ...entry, strict: tool.strict };
214
- }
215
- const definition = api === "openai-completions" && isRecord(entry.function)
216
- ? entry.function
217
- : entry;
218
- const tool = typeof definition.name === "string" ? tools.get(definition.name) : undefined;
219
- if (tool?.strict === undefined)
220
- return entry;
221
- return api === "openai-completions"
222
- ? { ...entry, function: { ...definition, strict: tool.strict } }
223
- : { ...entry, strict: tool.strict };
224
- }),
225
- };
226
- }
227
- function patchParallelTools(payload, enabled, api) {
228
- if (api !== "anthropic-messages")
229
- return { ...payload, parallel_tool_calls: enabled };
230
- if (enabled)
231
- return payload;
232
- const current = isRecord(payload.tool_choice) ? payload.tool_choice : {};
233
- return {
234
- ...payload,
235
- tool_choice: {
236
- type: typeof current.type === "string" ? current.type : "auto",
237
- ...current,
238
- disable_parallel_tool_use: true,
239
- },
240
- };
241
- }
242
- function patchImageDetails(payload, details) {
243
- const queues = new Map([...details].map(([url, queue]) => [url, [...queue]]));
244
- const patch = (value) => {
245
- if (Array.isArray(value))
246
- return value.map(patch);
247
- if (!isRecord(value))
248
- return value;
249
- const mapped = Object.fromEntries(Object.entries(value).map(([key, entry]) => [key, patch(entry)]));
250
- const responseUrl = mapped.type === "input_image" && typeof mapped.image_url === "string"
251
- ? mapped.image_url
252
- : undefined;
253
- const chatImage = mapped.type === "image_url" && isRecord(mapped.image_url)
254
- ? mapped.image_url
255
- : undefined;
256
- const chatUrl = typeof chatImage?.url === "string" ? chatImage.url : undefined;
257
- const detail = queues.get(responseUrl ?? chatUrl ?? "")?.shift();
258
- if (detail === undefined)
259
- return mapped;
260
- return responseUrl === undefined
261
- ? { ...mapped, image_url: { ...chatImage, detail } }
262
- : { ...mapped, detail };
263
- };
264
- const patched = patch(payload);
265
- return isRecord(patched) ? patched : payload;
266
- }
267
- function imageDetailQueues(request) {
268
- const details = new Map();
269
- const add = (content) => {
270
- for (const part of content) {
271
- if (part.type !== "image" || part.detail === undefined)
272
- continue;
273
- const queue = details.get(part.url) ?? [];
274
- queue.push(part.detail);
275
- details.set(part.url, queue);
276
- }
277
- };
278
- for (const item of request.items) {
279
- if (item.type === "message" || item.type === "tool_result")
280
- add(item.content);
281
- }
282
- return details;
283
- }
284
- function patchAnthropicThinking(payload, request) {
285
- if (request.reasoning?.enabled === false) {
286
- return { ...payload, thinking: { type: "disabled" } };
287
- }
288
- const budget = request.reasoning?.budgetTokens;
289
- if (budget === undefined)
290
- return payload;
291
- return {
292
- ...payload,
293
- thinking: {
294
- ...(isRecord(payload.thinking) ? payload.thinking : {}),
295
- type: "enabled",
296
- budget_tokens: budget,
297
- },
298
- };
299
- }
300
- //# sourceMappingURL=pi_options.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"pi_options.js","sourceRoot":"","sources":["../src/pi_options.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,oBAAoB,EAAE,MAAM,yBAAyB,CAAC;AAC/D,OAAO,EAAE,oBAAoB,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D,CAAC;IACD,MAAM,MAAM,GAAG,OAAO,CAAC,SAAS,EAAE,YAAY,CAAC;IAC/C,IAAI,MAAM,KAAK,SAAS;QAAE,OAAO,OAAO,CAAC;IACzC,OAAO;QACL,GAAG,OAAO;QACV,QAAQ,EAAE;YACR,GAAG,CAAC,QAAQ,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC,CAAC,EAAE,CAAC;YACvD,IAAI,EAAE,SAAS;YACf,aAAa,EAAE,MAAM;SACtB;KACF,CAAC;AACJ,CAAC"}
@@ -1,4 +0,0 @@
1
- import type { AssistantMessage, AssistantMessageEvent } from "@mupt-ai/pi-ai";
2
- import { type RouterCompletion, type RouterStreamEvent } from "./framework_types.js";
3
- export declare function routerCompletion(message: AssistantMessage, signal: AbortSignal): RouterCompletion;
4
- export declare function routerEvents(events: AsyncIterable<AssistantMessageEvent>, signal: AbortSignal): AsyncIterable<RouterStreamEvent>;
package/dist/pi_output.js DELETED
@@ -1,328 +0,0 @@
1
- import { RouterFrameworkError } from "./framework_error.js";
2
- import { decodeOpenAIReasoningSignature } from "./pi_reasoning_signature.js";
3
- import { isRecord } from "./types.js";
4
- import { isOpenAIReasoningContinuationApi, } from "./continuation_state.js";
5
- import { isRouterHostedToolCallStatus, } from "./framework_types.js";
6
- export function routerCompletion(message, signal) {
7
- assertPiSuccess(message, signal);
8
- const content = [];
9
- const source = providerIdentity(message);
10
- for (const part of message.content) {
11
- if (part.type === "text")
12
- content.push({ type: "text", text: part.text });
13
- if (part.type === "thinking")
14
- content.push(routerReasoning(part, source));
15
- if (part.type === "toolCall") {
16
- content.push({
17
- type: "tool_call",
18
- id: part.id,
19
- name: part.name,
20
- arguments: part.arguments,
21
- });
22
- }
23
- const hostedItem = providerItemRecord(part);
24
- if (hostedItem !== null)
25
- content.push(hostedToolCall(hostedItem, source));
26
- }
27
- return {
28
- ...(message.responseId === undefined ? {} : { id: message.responseId }),
29
- createdAtMs: message.timestamp,
30
- content,
31
- finishReason: routerFinishReason(message.stopReason),
32
- usage: routerUsage(message),
33
- };
34
- }
35
- export async function* routerEvents(events, signal) {
36
- const text = new Map();
37
- const thinking = new Map();
38
- const endedThinking = new Set();
39
- const openTools = new Map();
40
- const completedTools = new Map();
41
- for await (const event of events) {
42
- if (event.type === "text_start") {
43
- text.set(event.contentIndex, text.get(event.contentIndex) ?? "");
44
- continue;
45
- }
46
- if (event.type === "text_delta") {
47
- if (event.delta.length > 0) {
48
- text.set(event.contentIndex, (text.get(event.contentIndex) ?? "") + event.delta);
49
- yield { type: "text_delta", index: event.contentIndex, delta: event.delta };
50
- }
51
- continue;
52
- }
53
- if (event.type === "text_end") {
54
- const suffix = remainingText(text.get(event.contentIndex) ?? "", event.content);
55
- if (suffix.length > 0)
56
- yield { type: "text_delta", index: event.contentIndex, delta: suffix };
57
- text.set(event.contentIndex, event.content);
58
- continue;
59
- }
60
- if (event.type === "thinking_start") {
61
- thinking.set(event.contentIndex, "");
62
- continue;
63
- }
64
- if (event.type === "thinking_delta") {
65
- if (event.delta.length > 0) {
66
- thinking.set(event.contentIndex, (thinking.get(event.contentIndex) ?? "") + event.delta);
67
- yield { type: "reasoning_delta", index: event.contentIndex, delta: event.delta };
68
- }
69
- continue;
70
- }
71
- if (event.type === "thinking_end") {
72
- const streamed = thinking.get(event.contentIndex) ?? "";
73
- const suffix = remainingText(streamed, event.content);
74
- if (suffix.length > 0)
75
- yield { type: "reasoning_delta", index: event.contentIndex, delta: suffix };
76
- thinking.set(event.contentIndex, event.content);
77
- endedThinking.add(event.contentIndex);
78
- const part = event.partial.content[event.contentIndex];
79
- const source = providerIdentity(event.partial);
80
- if (part?.type === "thinking") {
81
- const reasoning = routerReasoning(part, source);
82
- yield {
83
- type: "reasoning_end",
84
- index: event.contentIndex,
85
- ...(part.redacted === true ? { redacted: true } : {}),
86
- source,
87
- ...(reasoning.continuation ? { continuation: reasoning.continuation } : {}),
88
- };
89
- }
90
- else {
91
- yield { type: "reasoning_end", index: event.contentIndex, source };
92
- }
93
- continue;
94
- }
95
- if (event.type === "toolcall_start") {
96
- const call = event.partial.content[event.contentIndex];
97
- if (call?.type !== "toolCall")
98
- throw invalidPiStream("tool-call start is missing its tool call.");
99
- openTools.set(event.contentIndex, { receivedDelta: false });
100
- yield {
101
- type: "tool_call_start",
102
- index: event.contentIndex,
103
- id: call.id,
104
- name: call.name,
105
- };
106
- continue;
107
- }
108
- if (event.type === "toolcall_delta") {
109
- const state = openTools.get(event.contentIndex);
110
- if (state === undefined)
111
- throw invalidPiStream("tool-call delta arrived before its start.");
112
- if (event.delta.length > 0) {
113
- state.receivedDelta = true;
114
- yield { type: "tool_call_delta", index: event.contentIndex, delta: event.delta };
115
- }
116
- continue;
117
- }
118
- if (event.type === "toolcall_end") {
119
- const state = openTools.get(event.contentIndex);
120
- if (state === undefined) {
121
- yield* completeToolCall(event.contentIndex, event.toolCall);
122
- completedTools.set(event.contentIndex, event.toolCall);
123
- continue;
124
- }
125
- if (!state.receivedDelta) {
126
- yield {
127
- type: "tool_call_delta",
128
- index: event.contentIndex,
129
- delta: JSON.stringify(event.toolCall.arguments),
130
- };
131
- }
132
- openTools.delete(event.contentIndex);
133
- completedTools.set(event.contentIndex, event.toolCall);
134
- yield { type: "tool_call_end", index: event.contentIndex };
135
- continue;
136
- }
137
- if (event.type === "done") {
138
- assertPiSuccess(event.message, signal);
139
- if (event.reason !== "stop" && event.reason !== "length" && event.reason !== "toolUse") {
140
- throw invalidPiStream(`stream ended with invalid reason '${String(event.reason)}'.`);
141
- }
142
- if (openTools.size > 0)
143
- throw invalidPiStream("stream finished with open tool calls.");
144
- yield* reconcileTerminalOutput(event.message, text, thinking, endedThinking, completedTools);
145
- yield {
146
- type: "finish",
147
- finishReason: routerFinishReason(event.reason),
148
- usage: routerUsage(event.message),
149
- };
150
- return;
151
- }
152
- if (event.type === "error") {
153
- if (event.reason === "aborted")
154
- throw piCancellation(event.error, signal);
155
- throw piProviderError(event.error.errorMessage ?? "Pi provider execution failed.");
156
- }
157
- }
158
- throw invalidPiStream("stream ended without a terminal event.");
159
- }
160
- function* reconcileTerminalOutput(message, text, thinking, endedThinking, completedTools) {
161
- for (const index of text.keys()) {
162
- if (message.content[index]?.type !== "text") {
163
- throw invalidPiStream(`terminal output changed text block ${index}.`);
164
- }
165
- }
166
- for (const index of thinking.keys()) {
167
- if (message.content[index]?.type !== "thinking") {
168
- throw invalidPiStream(`terminal output changed thinking block ${index}.`);
169
- }
170
- }
171
- for (const [index, streamed] of completedTools) {
172
- const terminal = message.content[index];
173
- if (terminal?.type !== "toolCall" || terminal.id !== streamed.id || terminal.name !== streamed.name) {
174
- throw invalidPiStream(`terminal output changed tool call ${index}.`);
175
- }
176
- }
177
- for (const [index, part] of message.content.entries()) {
178
- if (part.type === "text") {
179
- const suffix = remainingText(text.get(index) ?? "", part.text);
180
- if (suffix.length > 0)
181
- yield { type: "text_delta", index, delta: suffix };
182
- continue;
183
- }
184
- if (part.type === "thinking") {
185
- // Started-but-unended thinking blocks reconcile like unended text:
186
- // emit the remaining delta, then the proper reasoning_end.
187
- if (!endedThinking.has(index)) {
188
- yield* completeReasoning(index, part, providerIdentity(message), thinking.get(index) ?? "");
189
- }
190
- continue;
191
- }
192
- if (part.type === "toolCall") {
193
- if (!completedTools.has(index))
194
- yield* completeToolCall(index, part);
195
- continue;
196
- }
197
- const hostedItem = providerItemRecord(part);
198
- if (hostedItem !== null) {
199
- yield { ...hostedToolCall(hostedItem, providerIdentity(message)), index };
200
- }
201
- }
202
- }
203
- function* completeToolCall(index, call) {
204
- yield { type: "tool_call_start", index, id: call.id, name: call.name };
205
- yield { type: "tool_call_delta", index, delta: JSON.stringify(call.arguments) };
206
- yield { type: "tool_call_end", index };
207
- }
208
- function* completeReasoning(index, part, source, streamed = "") {
209
- const suffix = remainingText(streamed, part.thinking);
210
- if (suffix.length > 0) {
211
- yield { type: "reasoning_delta", index, delta: suffix };
212
- }
213
- const reasoning = routerReasoning(part, source);
214
- yield {
215
- type: "reasoning_end",
216
- index,
217
- ...(part.redacted === true ? { redacted: true } : {}),
218
- source,
219
- ...(reasoning.continuation ? { continuation: reasoning.continuation } : {}),
220
- };
221
- }
222
- function providerItemRecord(part) {
223
- if (!isRecord(part) || part.type !== "providerItem")
224
- return null;
225
- if (!isRecord(part.item)) {
226
- throw unsupportedHostedTool("the provider item has no payload record");
227
- }
228
- return part.item;
229
- }
230
- function hostedToolCall(item, source) {
231
- if (item.type !== "web_search_call") {
232
- throw unsupportedHostedTool(`only web_search_call items are replayable, got '${String(item.type)}'`);
233
- }
234
- if (typeof item.id !== "string" || item.id.length === 0) {
235
- throw unsupportedHostedTool("the web_search_call item has no id");
236
- }
237
- const status = hostedToolCallStatus(item.status);
238
- return {
239
- type: "hosted_tool_call",
240
- id: item.id,
241
- tool: "web_search",
242
- providerType: "web_search_call",
243
- ...(status === undefined ? {} : { status }),
244
- payload: item,
245
- source,
246
- };
247
- }
248
- function hostedToolCallStatus(value) {
249
- if (value === undefined || isRouterHostedToolCallStatus(value))
250
- return value;
251
- throw unsupportedHostedTool(`web_search_call status '${String(value)}' is not replayable`);
252
- }
253
- function unsupportedHostedTool(detail) {
254
- return new RouterFrameworkError("executor", `Pi returned a hosted tool item the router cannot replay: ${detail}.`, "pi_hosted_tool_unsupported");
255
- }
256
- function remainingText(streamed, terminal) {
257
- if (!terminal.startsWith(streamed)) {
258
- throw invalidPiStream("terminal text does not extend the streamed text.");
259
- }
260
- return terminal.slice(streamed.length);
261
- }
262
- function providerIdentity(message) {
263
- return { provider: message.provider, api: message.api, model: message.model };
264
- }
265
- function routerReasoning(part, source) {
266
- const signature = typeof part.thinkingSignature === "string" && part.thinkingSignature.length > 0
267
- ? part.thinkingSignature
268
- : undefined;
269
- let continuation;
270
- if (isOpenAIReasoningContinuationApi(source.api) && signature) {
271
- const native = decodeOpenAIReasoningSignature(signature);
272
- if (native?.encryptedContent) {
273
- continuation = {
274
- kind: "openai_reasoning",
275
- source,
276
- encryptedContent: native.encryptedContent,
277
- ...(native.id ? { providerItemId: native.id } : {}),
278
- };
279
- }
280
- }
281
- else if (source.api === "anthropic-messages" && signature) {
282
- continuation = part.redacted === true
283
- ? { kind: "anthropic_redacted_thinking", source, data: signature }
284
- : { kind: "anthropic_thinking", source, thinking: part.thinking, signature };
285
- }
286
- return {
287
- type: "reasoning",
288
- text: part.thinking,
289
- ...(part.redacted === true ? { redacted: true } : {}),
290
- source,
291
- ...(continuation ? { continuation } : {}),
292
- };
293
- }
294
- function assertPiSuccess(message, signal) {
295
- if (message.stopReason === "error") {
296
- throw piProviderError(message.errorMessage ?? "Pi provider execution failed.");
297
- }
298
- if (message.stopReason === "aborted")
299
- throw piCancellation(message, signal);
300
- }
301
- function routerFinishReason(reason) {
302
- if (reason === "length")
303
- return "length";
304
- if (reason === "toolUse")
305
- return "tool_calls";
306
- return "stop";
307
- }
308
- function routerUsage(message) {
309
- return {
310
- inputTokens: message.usage.input,
311
- outputTokens: message.usage.output,
312
- cacheReadTokens: message.usage.cacheRead,
313
- cacheWriteTokens: message.usage.cacheWrite,
314
- totalTokens: message.usage.totalTokens,
315
- };
316
- }
317
- function piCancellation(message, signal) {
318
- return new RouterFrameworkError("cancelled", typeof signal.reason === "string"
319
- ? signal.reason
320
- : message.errorMessage ?? "Request cancelled.", "request_cancelled");
321
- }
322
- function piProviderError(message) {
323
- return new RouterFrameworkError("executor", message, "pi_provider_error");
324
- }
325
- function invalidPiStream(message) {
326
- return new RouterFrameworkError("executor", `Pi ${message}`, "pi_stream_invalid");
327
- }
328
- //# sourceMappingURL=pi_output.js.map