pagegraph 0.12.0 → 0.14.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/README.md +325 -66
- package/dist/anthropic-messages-TqKpKmfW.js +1067 -0
- package/dist/anthropic-messages-TqKpKmfW.js.map +1 -0
- package/dist/audit.d.ts +4 -2
- package/dist/audit.js +5 -5
- package/dist/audit.js.map +1 -1
- package/dist/azure-openai-responses-DJjBLwoI.js +231 -0
- package/dist/azure-openai-responses-DJjBLwoI.js.map +1 -0
- package/dist/build-only/audit.js +1 -1
- package/dist/build-only/claims.d.ts +1 -0
- package/dist/build-only/claims.js +6 -0
- package/dist/build-only/claims.js.map +1 -0
- package/dist/build-only/config.js +1 -1
- package/dist/build-only/oxlint.d.ts +1 -0
- package/dist/build-only/oxlint.js +6 -0
- package/dist/build-only/oxlint.js.map +1 -0
- package/dist/build-only/tanstack-start.js +1 -1
- package/dist/build-only/vite.js +1 -1
- package/dist/{checks-uZk5Em9A.js → checks-BS-aDXB_.js} +2 -2
- package/dist/checks-BS-aDXB_.js.map +1 -0
- package/dist/claims-Bch7Lxlo.d.ts +92 -0
- package/dist/claims-Bv7voeEV.js +431 -0
- package/dist/claims-Bv7voeEV.js.map +1 -0
- package/dist/claims.d.ts +94 -0
- package/dist/claims.js +2 -0
- package/dist/cli.js +17607 -43387
- package/dist/cli.js.map +1 -1
- package/dist/cloudflare-workers-ai-system-one-f3ZrvNYZ.js +41 -0
- package/dist/cloudflare-workers-ai-system-one-f3ZrvNYZ.js.map +1 -0
- package/dist/config.d.ts +9 -29
- package/dist/config.js +2 -2
- package/dist/config.js.map +1 -1
- package/dist/{content-kcEnQdfi.js → content-CHd0X4s6.js} +2 -2
- package/dist/{content-kcEnQdfi.js.map → content-CHd0X4s6.js.map} +1 -1
- package/dist/{content-B1cPtL8b.d.ts → content-DgNr7apS.d.ts} +4 -5
- package/dist/declare-D5o7hwmv.js.map +1 -1
- package/dist/diagnostics-Wt5faNDM.js +108 -0
- package/dist/diagnostics-Wt5faNDM.js.map +1 -0
- package/dist/dist-B6zg_Z3Z.js +4794 -0
- package/dist/dist-B6zg_Z3Z.js.map +1 -0
- package/dist/document-CztEu3Vd.d.ts +109 -0
- package/dist/document-Dvemcv7V.js +379 -0
- package/dist/document-Dvemcv7V.js.map +1 -0
- package/dist/env-api-keys-C-OA0yM-.js +124 -0
- package/dist/env-api-keys-C-OA0yM-.js.map +1 -0
- package/dist/error-body-CyiejW4b.js +103 -0
- package/dist/error-body-CyiejW4b.js.map +1 -0
- package/dist/facts-1OKclPOk.js +115 -0
- package/dist/facts-1OKclPOk.js.map +1 -0
- package/dist/github-copilot-headers-CEpMOpQF.js +24 -0
- package/dist/github-copilot-headers-CEpMOpQF.js.map +1 -0
- package/dist/google-generative-ai-BxABKIKO.js +330 -0
- package/dist/google-generative-ai-BxABKIKO.js.map +1 -0
- package/dist/google-shared-0gpM2jFi.js +19471 -0
- package/dist/google-shared-0gpM2jFi.js.map +1 -0
- package/dist/google-vertex-D8Eb5cbu.js +376 -0
- package/dist/google-vertex-D8Eb5cbu.js.map +1 -0
- package/dist/{declare-DVgdEdOL.d.ts → graph-D1PIndsC.d.ts} +117 -6
- package/dist/{graph-Bcsourlv.js → graph-DMNOLXLu.js} +23 -13
- package/dist/graph-DMNOLXLu.js.map +1 -0
- package/dist/hash-Dp7EzZUo.js +18 -0
- package/dist/hash-Dp7EzZUo.js.map +1 -0
- package/dist/headers-BRNMVpzu.js +19 -0
- package/dist/headers-BRNMVpzu.js.map +1 -0
- package/dist/index-C5w9ripz.d.ts +204 -0
- package/dist/index.d.ts +6 -160
- package/dist/index.js +37 -5
- package/dist/index.js.map +1 -1
- package/dist/inline-BbS93GAT.js +682 -0
- package/dist/inline-BbS93GAT.js.map +1 -0
- package/dist/json-parse-C10znB7l.js +383 -0
- package/dist/json-parse-C10znB7l.js.map +1 -0
- package/dist/llms-Dhr_5u2O.js +19 -0
- package/dist/llms-Dhr_5u2O.js.map +1 -0
- package/dist/markdown-C6dRE9zD.js +168 -0
- package/dist/markdown-C6dRE9zD.js.map +1 -0
- package/dist/markdown-path-lJB9BAVZ.js +6 -0
- package/dist/markdown-path-lJB9BAVZ.js.map +1 -0
- package/dist/{message-DT_CjUdt.js → message-CuBjgZ74.js} +2 -2
- package/dist/{message-DT_CjUdt.js.map → message-CuBjgZ74.js.map} +1 -1
- package/dist/mistral-conversations-CvaqBWjR.js +690 -0
- package/dist/mistral-conversations-CvaqBWjR.js.map +1 -0
- package/dist/models-DcT5qOEL.js +1203 -0
- package/dist/models-DcT5qOEL.js.map +1 -0
- package/dist/node-Ccvru9GE.js +26 -0
- package/dist/node-Ccvru9GE.js.map +1 -0
- package/dist/node-MEosBOF2.js +2403 -0
- package/dist/node-MEosBOF2.js.map +1 -0
- package/dist/openai-BCBEd2eO.js +17350 -0
- package/dist/openai-BCBEd2eO.js.map +1 -0
- package/dist/openai-codex-responses-vJqxeeRC.js +1268 -0
- package/dist/openai-codex-responses-vJqxeeRC.js.map +1 -0
- package/dist/openai-completions-Cb_fRAbI.js +989 -0
- package/dist/openai-completions-Cb_fRAbI.js.map +1 -0
- package/dist/openai-prompt-cache-Bc6qNWt0.js +10 -0
- package/dist/openai-prompt-cache-Bc6qNWt0.js.map +1 -0
- package/dist/openai-responses-sDuh82G5.js +268 -0
- package/dist/openai-responses-sDuh82G5.js.map +1 -0
- package/dist/openai-responses-shared-mhuo1Jov.js +599 -0
- package/dist/openai-responses-shared-mhuo1Jov.js.map +1 -0
- package/dist/openrouter-images-CRmBkzpX.js +127 -0
- package/dist/openrouter-images-CRmBkzpX.js.map +1 -0
- package/dist/oxlint.d.ts +58 -0
- package/dist/oxlint.js +202 -0
- package/dist/oxlint.js.map +1 -0
- package/dist/page-heads-BfjWQvau.d.ts +108 -0
- package/dist/{page-heads-6y-CARRQ.js → page-heads-BgbaIjj9.js} +3 -3
- package/dist/page-heads-BgbaIjj9.js.map +1 -0
- package/dist/pi-Bvw6J0gh.js +56795 -0
- package/dist/pi-Bvw6J0gh.js.map +1 -0
- package/dist/pi-messages-CejiVYvO.js +308 -0
- package/dist/pi-messages-CejiVYvO.js.map +1 -0
- package/dist/{projections-DeEFe7XT.d.ts → projections-Bk-qOOsR.d.ts} +2 -2
- package/dist/provider-env-t38GK5FF.js +39 -0
- package/dist/provider-env-t38GK5FF.js.map +1 -0
- package/dist/provider-retry-DzKoh1X7.js +80 -0
- package/dist/provider-retry-DzKoh1X7.js.map +1 -0
- package/dist/react.d.ts +3 -2
- package/dist/react.js +2 -1
- package/dist/react.js.map +1 -1
- package/dist/rolldown-runtime-B-lAHAz2.js +37 -0
- package/dist/sanitize-unicode-Ds14l9Om.js +28 -0
- package/dist/sanitize-unicode-Ds14l9Om.js.map +1 -0
- package/dist/sdk-C9_wiO6w.js +12503 -0
- package/dist/sdk-C9_wiO6w.js.map +1 -0
- package/dist/state-BuJP_3Lr.js +28102 -0
- package/dist/state-BuJP_3Lr.js.map +1 -0
- package/dist/system-one-shared-Dl10ZF7Z.js +167 -0
- package/dist/system-one-shared-Dl10ZF7Z.js.map +1 -0
- package/dist/tanstack-start/index.d.ts +29 -13
- package/dist/tanstack-start/index.js +391 -42
- package/dist/tanstack-start/index.js.map +1 -1
- package/dist/tanstack-start/markdown.d.ts +12 -0
- package/dist/tanstack-start/markdown.js +149 -0
- package/dist/tanstack-start/markdown.js.map +1 -0
- package/dist/tanstack-start/prerender-worker.d.ts +2 -0
- package/dist/tanstack-start/prerender-worker.js +7 -0
- package/dist/tanstack-start/prerender-worker.js.map +1 -0
- package/dist/tanstack-start/react.d.ts +7 -0
- package/dist/tanstack-start/react.js +12 -0
- package/dist/tanstack-start/react.js.map +1 -0
- package/dist/tanstack-start/server.d.ts +9 -3
- package/dist/tanstack-start/server.js +26 -2
- package/dist/tanstack-start/server.js.map +1 -1
- package/dist/transform-messages-BLJ8EfK6.js +468 -0
- package/dist/transform-messages-BLJ8EfK6.js.map +1 -0
- package/dist/typesafe-system-one-CwEWMV8P.js +25 -0
- package/dist/typesafe-system-one-CwEWMV8P.js.map +1 -0
- package/dist/{vite-graph-loader-01p1WxEq.js → vite-graph-loader-CsGiYNWE.js} +6 -5
- package/dist/vite-graph-loader-CsGiYNWE.js.map +1 -0
- package/dist/vite-graph-loader-DZCwejuJ.d.ts +133 -0
- package/package.json +47 -10
- package/dist/checks-uZk5Em9A.js.map +0 -1
- package/dist/graph-Bcsourlv.js.map +0 -1
- package/dist/graph-BxCY90vq.d.ts +0 -104
- package/dist/graph-CBIY71w6.d.ts +0 -57
- package/dist/page-heads-6y-CARRQ.js.map +0 -1
- package/dist/page-heads-DNkjMUEN.d.ts +0 -21
- package/dist/vite-graph-loader-01p1WxEq.js.map +0 -1
- package/dist/vite-graph-loader-Btgjz5RH.d.ts +0 -56
|
@@ -0,0 +1,599 @@
|
|
|
1
|
+
import { C as renderSystemMessageUpdate, S as getSystemMessageText, t as calculateCost, v as resolveTranscript, y as resolveTranscriptTools } from "./models-DcT5qOEL.js";
|
|
2
|
+
import { n as parseStreamingJson } from "./json-parse-C10znB7l.js";
|
|
3
|
+
import { d as resolveGrammarConstrainedSampling, f as resolveJsonSchemaStrictSampling, l as getGrammarToolInput, s as appendGrammarToolInputJsonDelta, t as transformMessages, u as getJsonSchemaToolParameters } from "./transform-messages-BLJ8EfK6.js";
|
|
4
|
+
import { t as sanitizeSurrogates } from "./sanitize-unicode-Ds14l9Om.js";
|
|
5
|
+
import { t as shortHash } from "./hash-Dp7EzZUo.js";
|
|
6
|
+
//#region node_modules/@earendil-works/pi-ai/dist/api/openai-responses-shared.js
|
|
7
|
+
function encodeTextSignatureV1(id, phase) {
|
|
8
|
+
const payload = {
|
|
9
|
+
v: 1,
|
|
10
|
+
id
|
|
11
|
+
};
|
|
12
|
+
if (phase) payload.phase = phase;
|
|
13
|
+
return JSON.stringify(payload);
|
|
14
|
+
}
|
|
15
|
+
function parseTextSignature(signature) {
|
|
16
|
+
if (!signature) return void 0;
|
|
17
|
+
if (signature.startsWith("{")) try {
|
|
18
|
+
const parsed = JSON.parse(signature);
|
|
19
|
+
if (parsed.v === 1 && typeof parsed.id === "string") {
|
|
20
|
+
if (parsed.phase === "commentary" || parsed.phase === "final_answer") return {
|
|
21
|
+
id: parsed.id,
|
|
22
|
+
phase: parsed.phase
|
|
23
|
+
};
|
|
24
|
+
return { id: parsed.id };
|
|
25
|
+
}
|
|
26
|
+
} catch {}
|
|
27
|
+
return { id: signature };
|
|
28
|
+
}
|
|
29
|
+
function convertToolResultOutput(model, content) {
|
|
30
|
+
const textResult = content.filter((c) => c.type === "text").map((c) => c.text).join("\n");
|
|
31
|
+
const images = content.filter((c) => c.type === "image");
|
|
32
|
+
const hasText = textResult.length > 0;
|
|
33
|
+
if (images.length === 0 || !model.input.includes("image")) return sanitizeSurrogates(hasText ? textResult : images.length > 0 ? "(see attached image)" : "(no tool output)");
|
|
34
|
+
const output = [];
|
|
35
|
+
if (hasText) output.push({
|
|
36
|
+
type: "input_text",
|
|
37
|
+
text: sanitizeSurrogates(textResult)
|
|
38
|
+
});
|
|
39
|
+
for (const image of images) output.push({
|
|
40
|
+
type: "input_image",
|
|
41
|
+
detail: "auto",
|
|
42
|
+
image_url: `data:${image.mimeType};base64,${image.data}`
|
|
43
|
+
});
|
|
44
|
+
return output;
|
|
45
|
+
}
|
|
46
|
+
function convertResponsesMessages(model, context, allowedToolCallProviders, options) {
|
|
47
|
+
const normalizedContext = resolveTranscript(context, options?.supportsMidConvoSystemMessages);
|
|
48
|
+
const messages = [];
|
|
49
|
+
const normalizeIdPart = (part) => {
|
|
50
|
+
const sanitized = part.replace(/[^a-zA-Z0-9_-]/g, "_");
|
|
51
|
+
return (sanitized.length > 64 ? sanitized.slice(0, 64) : sanitized).replace(/_+$/, "");
|
|
52
|
+
};
|
|
53
|
+
const buildForeignResponsesItemId = (itemId) => {
|
|
54
|
+
const normalized = `fc_${shortHash(itemId)}`;
|
|
55
|
+
return normalized.length > 64 ? normalized.slice(0, 64) : normalized;
|
|
56
|
+
};
|
|
57
|
+
const normalizeToolCallId = (id, _targetModel, source) => {
|
|
58
|
+
if (!allowedToolCallProviders.has(model.provider)) return normalizeIdPart(id);
|
|
59
|
+
if (!id.includes("|")) return normalizeIdPart(id);
|
|
60
|
+
const [callId, itemId] = id.split("|");
|
|
61
|
+
const normalizedCallId = normalizeIdPart(callId);
|
|
62
|
+
let normalizedItemId = source.provider !== model.provider || source.api !== model.api ? buildForeignResponsesItemId(itemId) : normalizeIdPart(itemId);
|
|
63
|
+
if (!normalizedItemId.startsWith("fc_")) normalizedItemId = normalizeIdPart(`fc_${normalizedItemId}`);
|
|
64
|
+
return `${normalizedCallId}|${normalizedItemId}`;
|
|
65
|
+
};
|
|
66
|
+
const transformedMessages = transformMessages(normalizedContext.messages, model, normalizeToolCallId);
|
|
67
|
+
const transcriptTools = resolveTranscriptTools(normalizedContext.messages, (options?.supportsAdditionalTools ?? false) || (options?.supportsToolSearch ?? false));
|
|
68
|
+
const appendSystemToolAdditions = (message, seed) => {
|
|
69
|
+
const tools = transcriptTools.anchorsAdditions ? message.toolsAdded ?? [] : [];
|
|
70
|
+
if (tools.length === 0) return;
|
|
71
|
+
if (options?.supportsAdditionalTools) {
|
|
72
|
+
messages.push({
|
|
73
|
+
type: "additional_tools",
|
|
74
|
+
role: "developer",
|
|
75
|
+
tools: convertResponsesTools(tools, options.toolOptions)
|
|
76
|
+
});
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
if (!options?.supportsToolSearch) return;
|
|
80
|
+
const names = tools.map((tool) => tool.name);
|
|
81
|
+
const callId = `pi_tool_load_${shortHash(`${seed}:${names.join(",")}`)}`;
|
|
82
|
+
messages.push({
|
|
83
|
+
type: "tool_search_call",
|
|
84
|
+
call_id: callId,
|
|
85
|
+
execution: "client",
|
|
86
|
+
status: "completed",
|
|
87
|
+
arguments: {
|
|
88
|
+
query: names.join(" "),
|
|
89
|
+
limit: names.length
|
|
90
|
+
}
|
|
91
|
+
});
|
|
92
|
+
messages.push({
|
|
93
|
+
type: "tool_search_output",
|
|
94
|
+
call_id: callId,
|
|
95
|
+
execution: "client",
|
|
96
|
+
status: "completed",
|
|
97
|
+
tools: convertResponsesTools(tools, {
|
|
98
|
+
...options.toolOptions,
|
|
99
|
+
toolSearchResult: true
|
|
100
|
+
})
|
|
101
|
+
});
|
|
102
|
+
};
|
|
103
|
+
const includeInitialSystemMessage = options?.includeSystemPrompt ?? true;
|
|
104
|
+
const compat = model.compat;
|
|
105
|
+
const instructionRole = model.reasoning && compat?.supportsDeveloperRole !== false ? "developer" : "system";
|
|
106
|
+
let msgIndex = 0;
|
|
107
|
+
let sourceIndex = 0;
|
|
108
|
+
for (const msg of transformedMessages) {
|
|
109
|
+
const isLeadingSystemMessage = sourceIndex++ === 0 && msg.role === "system";
|
|
110
|
+
if (msg.role === "system") {
|
|
111
|
+
if (!isLeadingSystemMessage) appendSystemToolAdditions(msg, `system:${msgIndex}`);
|
|
112
|
+
if (!isLeadingSystemMessage || includeInitialSystemMessage) {
|
|
113
|
+
const text = isLeadingSystemMessage ? getSystemMessageText(msg) : renderSystemMessageUpdate(msg);
|
|
114
|
+
if (text.length > 0) messages.push({
|
|
115
|
+
role: instructionRole,
|
|
116
|
+
content: sanitizeSurrogates(text)
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
} else if (msg.role === "user") if (typeof msg.content === "string") messages.push({
|
|
120
|
+
role: "user",
|
|
121
|
+
content: [{
|
|
122
|
+
type: "input_text",
|
|
123
|
+
text: sanitizeSurrogates(msg.content)
|
|
124
|
+
}]
|
|
125
|
+
});
|
|
126
|
+
else {
|
|
127
|
+
const content = msg.content.map((item) => {
|
|
128
|
+
if (item.type === "text") return {
|
|
129
|
+
type: "input_text",
|
|
130
|
+
text: sanitizeSurrogates(item.text)
|
|
131
|
+
};
|
|
132
|
+
return {
|
|
133
|
+
type: "input_image",
|
|
134
|
+
detail: "auto",
|
|
135
|
+
image_url: `data:${item.mimeType};base64,${item.data}`
|
|
136
|
+
};
|
|
137
|
+
});
|
|
138
|
+
if (content.length === 0) continue;
|
|
139
|
+
messages.push({
|
|
140
|
+
role: "user",
|
|
141
|
+
content
|
|
142
|
+
});
|
|
143
|
+
}
|
|
144
|
+
else if (msg.role === "assistant") {
|
|
145
|
+
const output = [];
|
|
146
|
+
const assistantMsg = msg;
|
|
147
|
+
const isSameProviderAndApi = assistantMsg.provider === model.provider && assistantMsg.api === model.api;
|
|
148
|
+
const isSameModel = isSameProviderAndApi && assistantMsg.model === model.id;
|
|
149
|
+
const isDifferentModel = isSameProviderAndApi && assistantMsg.model !== model.id;
|
|
150
|
+
let textBlockIndex = 0;
|
|
151
|
+
for (const block of msg.content) if (block.type === "thinking") {
|
|
152
|
+
if (block.thinkingSignature) {
|
|
153
|
+
const reasoningItem = JSON.parse(block.thinkingSignature);
|
|
154
|
+
output.push(reasoningItem);
|
|
155
|
+
}
|
|
156
|
+
} else if (block.type === "text") {
|
|
157
|
+
const textBlock = block;
|
|
158
|
+
const parsedSignature = parseTextSignature(textBlock.textSignature);
|
|
159
|
+
const fallbackMessageId = textBlockIndex === 0 ? `msg_pi_${msgIndex}` : `msg_pi_${msgIndex}_${textBlockIndex}`;
|
|
160
|
+
textBlockIndex++;
|
|
161
|
+
let msgId = parsedSignature?.id;
|
|
162
|
+
if (!msgId) msgId = fallbackMessageId;
|
|
163
|
+
else if (msgId.length > 64) msgId = `msg_${shortHash(msgId)}`;
|
|
164
|
+
output.push({
|
|
165
|
+
type: "message",
|
|
166
|
+
role: "assistant",
|
|
167
|
+
content: [{
|
|
168
|
+
type: "output_text",
|
|
169
|
+
text: sanitizeSurrogates(textBlock.text),
|
|
170
|
+
annotations: []
|
|
171
|
+
}],
|
|
172
|
+
status: "completed",
|
|
173
|
+
id: msgId,
|
|
174
|
+
phase: parsedSignature?.phase
|
|
175
|
+
});
|
|
176
|
+
} else if (block.type === "toolCall") {
|
|
177
|
+
const toolCall = block;
|
|
178
|
+
const [callId, itemIdRaw] = toolCall.id.split("|");
|
|
179
|
+
const customInputProperty = options?.grammarToolInputProperties?.get(toolCall.name);
|
|
180
|
+
let itemId = itemIdRaw;
|
|
181
|
+
const itemIdPrefix = customInputProperty === void 0 ? "fc_" : "ctc_";
|
|
182
|
+
if (isDifferentModel || !itemId?.startsWith(itemIdPrefix)) itemId = void 0;
|
|
183
|
+
if (customInputProperty !== void 0) output.push({
|
|
184
|
+
type: "custom_tool_call",
|
|
185
|
+
id: itemId,
|
|
186
|
+
call_id: callId,
|
|
187
|
+
name: toolCall.name,
|
|
188
|
+
input: sanitizeSurrogates(getGrammarToolInput(toolCall.name, toolCall.arguments, customInputProperty)),
|
|
189
|
+
...isSameModel && toolCall.namespace !== void 0 ? { namespace: toolCall.namespace } : {}
|
|
190
|
+
});
|
|
191
|
+
else output.push({
|
|
192
|
+
type: "function_call",
|
|
193
|
+
id: itemId,
|
|
194
|
+
call_id: callId,
|
|
195
|
+
name: toolCall.name,
|
|
196
|
+
arguments: JSON.stringify(toolCall.arguments),
|
|
197
|
+
...isSameModel && toolCall.namespace !== void 0 ? { namespace: toolCall.namespace } : {}
|
|
198
|
+
});
|
|
199
|
+
}
|
|
200
|
+
if (output.length === 0) continue;
|
|
201
|
+
messages.push(...output);
|
|
202
|
+
} else if (msg.role === "toolResult") {
|
|
203
|
+
const [callId] = msg.toolCallId.split("|");
|
|
204
|
+
const output = convertToolResultOutput(model, msg.content);
|
|
205
|
+
if (options?.grammarToolInputProperties?.has(msg.toolName)) messages.push({
|
|
206
|
+
type: "custom_tool_call_output",
|
|
207
|
+
call_id: callId,
|
|
208
|
+
output
|
|
209
|
+
});
|
|
210
|
+
else messages.push({
|
|
211
|
+
type: "function_call_output",
|
|
212
|
+
call_id: callId,
|
|
213
|
+
output
|
|
214
|
+
});
|
|
215
|
+
}
|
|
216
|
+
if (!isLeadingSystemMessage) msgIndex++;
|
|
217
|
+
}
|
|
218
|
+
return messages;
|
|
219
|
+
}
|
|
220
|
+
function convertResponsesTools(tools, options) {
|
|
221
|
+
const defaultStrict = options?.strict === void 0 ? false : options.strict;
|
|
222
|
+
const supportsStrictMode = options?.supportsStrictMode ?? true;
|
|
223
|
+
const supportsOpenAIGrammarTools = options?.supportsOpenAIGrammarTools ?? false;
|
|
224
|
+
return tools.map((tool) => {
|
|
225
|
+
const grammar = resolveGrammarConstrainedSampling(tool, supportsOpenAIGrammarTools);
|
|
226
|
+
if (grammar) return {
|
|
227
|
+
type: "custom",
|
|
228
|
+
name: tool.name,
|
|
229
|
+
description: tool.description,
|
|
230
|
+
format: {
|
|
231
|
+
type: "grammar",
|
|
232
|
+
syntax: grammar.format,
|
|
233
|
+
definition: grammar.definition
|
|
234
|
+
},
|
|
235
|
+
...options?.toolSearchResult ? { defer_loading: true } : {}
|
|
236
|
+
};
|
|
237
|
+
const strict = resolveJsonSchemaStrictSampling(tool, supportsStrictMode) ?? defaultStrict;
|
|
238
|
+
const functionTool = {
|
|
239
|
+
type: "function",
|
|
240
|
+
name: tool.name,
|
|
241
|
+
description: tool.description,
|
|
242
|
+
parameters: getJsonSchemaToolParameters(tool, strict === true),
|
|
243
|
+
...options?.toolSearchResult ? { defer_loading: true } : {}
|
|
244
|
+
};
|
|
245
|
+
if (supportsStrictMode) functionTool.strict = strict;
|
|
246
|
+
return functionTool;
|
|
247
|
+
});
|
|
248
|
+
}
|
|
249
|
+
function getCustomToolCallInput(block) {
|
|
250
|
+
const property = block.customInput?.property;
|
|
251
|
+
if (property === void 0) return "";
|
|
252
|
+
const value = block.arguments[property];
|
|
253
|
+
return typeof value === "string" ? value : "";
|
|
254
|
+
}
|
|
255
|
+
function appendCustomToolCallInput(block, nextInput, close) {
|
|
256
|
+
const customInput = block.customInput;
|
|
257
|
+
if (!customInput) return void 0;
|
|
258
|
+
const delta = appendGrammarToolInputJsonDelta(customInput.jsonBuffer, customInput.property, nextInput, close);
|
|
259
|
+
block.arguments = { [customInput.property]: nextInput };
|
|
260
|
+
return delta;
|
|
261
|
+
}
|
|
262
|
+
async function processResponsesStream(openaiStream, output, stream, model, options) {
|
|
263
|
+
let sawTerminalResponseEvent = false;
|
|
264
|
+
const outputSlots = /* @__PURE__ */ new Map();
|
|
265
|
+
const reasoningBlocksById = /* @__PURE__ */ new Map();
|
|
266
|
+
const applyMessagePhaseStopReason = (item) => {
|
|
267
|
+
if (item.type === "message" && item.phase === "final_answer") output.stopReason = "stop";
|
|
268
|
+
};
|
|
269
|
+
const getSlot = (outputIndex, type) => {
|
|
270
|
+
const slot = outputSlots.get(outputIndex);
|
|
271
|
+
return slot?.type === type ? slot : void 0;
|
|
272
|
+
};
|
|
273
|
+
const pushToolCallDelta = (slot, delta) => {
|
|
274
|
+
if (delta === void 0) return;
|
|
275
|
+
stream.push({
|
|
276
|
+
type: "toolcall_delta",
|
|
277
|
+
contentIndex: slot.contentIndex,
|
|
278
|
+
delta,
|
|
279
|
+
partial: output
|
|
280
|
+
});
|
|
281
|
+
};
|
|
282
|
+
const createSlot = (outputIndex, item) => {
|
|
283
|
+
if (item.type === "reasoning") {
|
|
284
|
+
const block = {
|
|
285
|
+
type: "thinking",
|
|
286
|
+
thinking: ""
|
|
287
|
+
};
|
|
288
|
+
output.content.push(block);
|
|
289
|
+
const slot = {
|
|
290
|
+
type: "thinking",
|
|
291
|
+
block,
|
|
292
|
+
contentIndex: output.content.length - 1
|
|
293
|
+
};
|
|
294
|
+
outputSlots.set(outputIndex, slot);
|
|
295
|
+
stream.push({
|
|
296
|
+
type: "thinking_start",
|
|
297
|
+
contentIndex: slot.contentIndex,
|
|
298
|
+
partial: output
|
|
299
|
+
});
|
|
300
|
+
return slot;
|
|
301
|
+
}
|
|
302
|
+
if (item.type === "message") {
|
|
303
|
+
applyMessagePhaseStopReason(item);
|
|
304
|
+
const block = {
|
|
305
|
+
type: "text",
|
|
306
|
+
text: ""
|
|
307
|
+
};
|
|
308
|
+
output.content.push(block);
|
|
309
|
+
const slot = {
|
|
310
|
+
type: "text",
|
|
311
|
+
block,
|
|
312
|
+
contentIndex: output.content.length - 1
|
|
313
|
+
};
|
|
314
|
+
outputSlots.set(outputIndex, slot);
|
|
315
|
+
stream.push({
|
|
316
|
+
type: "text_start",
|
|
317
|
+
contentIndex: slot.contentIndex,
|
|
318
|
+
partial: output
|
|
319
|
+
});
|
|
320
|
+
return slot;
|
|
321
|
+
}
|
|
322
|
+
if (item.type === "function_call") {
|
|
323
|
+
const block = {
|
|
324
|
+
type: "toolCall",
|
|
325
|
+
id: `${item.call_id}|${item.id}`,
|
|
326
|
+
name: item.name,
|
|
327
|
+
arguments: {},
|
|
328
|
+
...item.namespace !== void 0 ? { namespace: item.namespace } : {},
|
|
329
|
+
partialJson: item.arguments || ""
|
|
330
|
+
};
|
|
331
|
+
output.content.push(block);
|
|
332
|
+
const slot = {
|
|
333
|
+
type: "toolCall",
|
|
334
|
+
block,
|
|
335
|
+
contentIndex: output.content.length - 1
|
|
336
|
+
};
|
|
337
|
+
outputSlots.set(outputIndex, slot);
|
|
338
|
+
stream.push({
|
|
339
|
+
type: "toolcall_start",
|
|
340
|
+
contentIndex: slot.contentIndex,
|
|
341
|
+
partial: output
|
|
342
|
+
});
|
|
343
|
+
return slot;
|
|
344
|
+
}
|
|
345
|
+
if (item.type === "custom_tool_call") {
|
|
346
|
+
const inputProperty = options?.grammarToolInputProperties?.get(item.name) ?? "input";
|
|
347
|
+
const input = item.input || "";
|
|
348
|
+
const block = {
|
|
349
|
+
type: "toolCall",
|
|
350
|
+
id: `${item.call_id}|${item.id}`,
|
|
351
|
+
name: item.name,
|
|
352
|
+
arguments: { [inputProperty]: input },
|
|
353
|
+
...item.namespace !== void 0 ? { namespace: item.namespace } : {},
|
|
354
|
+
customInput: {
|
|
355
|
+
property: inputProperty,
|
|
356
|
+
jsonBuffer: {
|
|
357
|
+
input: "",
|
|
358
|
+
started: false,
|
|
359
|
+
closed: false
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
};
|
|
363
|
+
output.content.push(block);
|
|
364
|
+
const slot = {
|
|
365
|
+
type: "toolCall",
|
|
366
|
+
block,
|
|
367
|
+
contentIndex: output.content.length - 1
|
|
368
|
+
};
|
|
369
|
+
outputSlots.set(outputIndex, slot);
|
|
370
|
+
stream.push({
|
|
371
|
+
type: "toolcall_start",
|
|
372
|
+
contentIndex: slot.contentIndex,
|
|
373
|
+
partial: output
|
|
374
|
+
});
|
|
375
|
+
return slot;
|
|
376
|
+
}
|
|
377
|
+
};
|
|
378
|
+
const getOrCreateSlot = (outputIndex, item) => {
|
|
379
|
+
return outputSlots.get(outputIndex) ?? createSlot(outputIndex, item);
|
|
380
|
+
};
|
|
381
|
+
const backfillReasoningSignatures = (responseOutput) => {
|
|
382
|
+
for (const item of responseOutput) {
|
|
383
|
+
if (item.type !== "reasoning" || !item.encrypted_content) continue;
|
|
384
|
+
const block = reasoningBlocksById.get(item.id);
|
|
385
|
+
if (!block?.thinkingSignature) continue;
|
|
386
|
+
const storedItem = JSON.parse(block.thinkingSignature);
|
|
387
|
+
if (storedItem.encrypted_content) continue;
|
|
388
|
+
block.thinkingSignature = JSON.stringify({
|
|
389
|
+
...storedItem,
|
|
390
|
+
encrypted_content: item.encrypted_content
|
|
391
|
+
});
|
|
392
|
+
}
|
|
393
|
+
};
|
|
394
|
+
const finalizeResponse = (response) => {
|
|
395
|
+
sawTerminalResponseEvent = true;
|
|
396
|
+
backfillReasoningSignatures(response.output ?? []);
|
|
397
|
+
if (response?.id) output.responseId = response.id;
|
|
398
|
+
if (response?.usage) {
|
|
399
|
+
const inputDetails = response.usage.input_tokens_details;
|
|
400
|
+
const cachedTokens = inputDetails?.cached_tokens || 0;
|
|
401
|
+
const cacheWriteTokens = inputDetails?.cache_write_tokens || 0;
|
|
402
|
+
output.usage = {
|
|
403
|
+
input: Math.max(0, (response.usage.input_tokens || 0) - cachedTokens - cacheWriteTokens),
|
|
404
|
+
output: response.usage.output_tokens || 0,
|
|
405
|
+
cacheRead: cachedTokens,
|
|
406
|
+
cacheWrite: cacheWriteTokens,
|
|
407
|
+
reasoning: response.usage.output_tokens_details?.reasoning_tokens || 0,
|
|
408
|
+
totalTokens: response.usage.total_tokens || 0,
|
|
409
|
+
cost: {
|
|
410
|
+
input: 0,
|
|
411
|
+
output: 0,
|
|
412
|
+
cacheRead: 0,
|
|
413
|
+
cacheWrite: 0,
|
|
414
|
+
total: 0
|
|
415
|
+
}
|
|
416
|
+
};
|
|
417
|
+
}
|
|
418
|
+
calculateCost(model, output.usage);
|
|
419
|
+
if (options?.applyServiceTierPricing) {
|
|
420
|
+
const serviceTier = options.resolveServiceTier ? options.resolveServiceTier(response?.service_tier, options.serviceTier) : response?.service_tier ?? options.serviceTier;
|
|
421
|
+
options.applyServiceTierPricing(output.usage, serviceTier);
|
|
422
|
+
}
|
|
423
|
+
const status = response?.status;
|
|
424
|
+
const incompleteDetails = response?.incomplete_details;
|
|
425
|
+
const incompleteReason = typeof incompleteDetails?.reason === "string" ? incompleteDetails.reason : void 0;
|
|
426
|
+
output.rawStopReason = incompleteReason ? `${status}.${incompleteReason}` : status;
|
|
427
|
+
const mappedStop = mapStopReason(status, incompleteReason);
|
|
428
|
+
output.stopReason = mappedStop.stopReason;
|
|
429
|
+
if (mappedStop.errorMessage === void 0) delete output.errorMessage;
|
|
430
|
+
else output.errorMessage = mappedStop.errorMessage;
|
|
431
|
+
if (output.content.some((b) => b.type === "toolCall") && output.stopReason === "stop") output.stopReason = "toolUse";
|
|
432
|
+
};
|
|
433
|
+
for await (const event of openaiStream) {
|
|
434
|
+
await options?.onProviderStreamEvent?.(event, model);
|
|
435
|
+
if (event.type === "response.created") output.responseId = event.response.id;
|
|
436
|
+
else if (event.type === "response.output_item.added") createSlot(event.output_index, event.item);
|
|
437
|
+
else if (event.type === "response.reasoning_summary_text.delta") {
|
|
438
|
+
const slot = getSlot(event.output_index, "thinking");
|
|
439
|
+
if (!slot) continue;
|
|
440
|
+
slot.block.thinking += event.delta;
|
|
441
|
+
stream.push({
|
|
442
|
+
type: "thinking_delta",
|
|
443
|
+
contentIndex: slot.contentIndex,
|
|
444
|
+
delta: event.delta,
|
|
445
|
+
partial: output
|
|
446
|
+
});
|
|
447
|
+
} else if (event.type === "response.reasoning_summary_part.done") {
|
|
448
|
+
const slot = getSlot(event.output_index, "thinking");
|
|
449
|
+
if (!slot) continue;
|
|
450
|
+
slot.block.thinking += "\n\n";
|
|
451
|
+
stream.push({
|
|
452
|
+
type: "thinking_delta",
|
|
453
|
+
contentIndex: slot.contentIndex,
|
|
454
|
+
delta: "\n\n",
|
|
455
|
+
partial: output
|
|
456
|
+
});
|
|
457
|
+
} else if (event.type === "response.reasoning_text.delta") {
|
|
458
|
+
const slot = getSlot(event.output_index, "thinking");
|
|
459
|
+
if (!slot) continue;
|
|
460
|
+
slot.block.thinking += event.delta;
|
|
461
|
+
stream.push({
|
|
462
|
+
type: "thinking_delta",
|
|
463
|
+
contentIndex: slot.contentIndex,
|
|
464
|
+
delta: event.delta,
|
|
465
|
+
partial: output
|
|
466
|
+
});
|
|
467
|
+
} else if (event.type === "response.output_text.delta") {
|
|
468
|
+
const slot = getSlot(event.output_index, "text");
|
|
469
|
+
if (!slot) continue;
|
|
470
|
+
slot.block.text += event.delta;
|
|
471
|
+
stream.push({
|
|
472
|
+
type: "text_delta",
|
|
473
|
+
contentIndex: slot.contentIndex,
|
|
474
|
+
delta: event.delta,
|
|
475
|
+
partial: output
|
|
476
|
+
});
|
|
477
|
+
} else if (event.type === "response.refusal.delta") {
|
|
478
|
+
const slot = getSlot(event.output_index, "text");
|
|
479
|
+
if (!slot) continue;
|
|
480
|
+
slot.block.text += event.delta;
|
|
481
|
+
stream.push({
|
|
482
|
+
type: "text_delta",
|
|
483
|
+
contentIndex: slot.contentIndex,
|
|
484
|
+
delta: event.delta,
|
|
485
|
+
partial: output
|
|
486
|
+
});
|
|
487
|
+
} else if (event.type === "response.function_call_arguments.delta") {
|
|
488
|
+
const slot = getSlot(event.output_index, "toolCall");
|
|
489
|
+
if (!slot || slot.block.partialJson === void 0) continue;
|
|
490
|
+
slot.block.partialJson += event.delta;
|
|
491
|
+
slot.block.arguments = parseStreamingJson(slot.block.partialJson);
|
|
492
|
+
pushToolCallDelta(slot, event.delta);
|
|
493
|
+
} else if (event.type === "response.function_call_arguments.done") {
|
|
494
|
+
const slot = getSlot(event.output_index, "toolCall");
|
|
495
|
+
if (!slot || slot.block.partialJson === void 0) continue;
|
|
496
|
+
const previousPartialJson = slot.block.partialJson;
|
|
497
|
+
slot.block.partialJson = event.arguments;
|
|
498
|
+
slot.block.arguments = parseStreamingJson(slot.block.partialJson);
|
|
499
|
+
if (event.arguments.startsWith(previousPartialJson)) {
|
|
500
|
+
const delta = event.arguments.slice(previousPartialJson.length);
|
|
501
|
+
if (delta.length > 0) pushToolCallDelta(slot, delta);
|
|
502
|
+
}
|
|
503
|
+
} else if (event.type === "response.custom_tool_call_input.delta") {
|
|
504
|
+
const slot = getSlot(event.output_index, "toolCall");
|
|
505
|
+
if (!slot || !slot.block.customInput) continue;
|
|
506
|
+
pushToolCallDelta(slot, appendCustomToolCallInput(slot.block, getCustomToolCallInput(slot.block) + event.delta, false));
|
|
507
|
+
} else if (event.type === "response.custom_tool_call_input.done") {
|
|
508
|
+
const slot = getSlot(event.output_index, "toolCall");
|
|
509
|
+
if (!slot || !slot.block.customInput) continue;
|
|
510
|
+
pushToolCallDelta(slot, appendCustomToolCallInput(slot.block, event.input, true));
|
|
511
|
+
} else if (event.type === "response.output_item.done") {
|
|
512
|
+
const item = event.item;
|
|
513
|
+
applyMessagePhaseStopReason(item);
|
|
514
|
+
const slot = getOrCreateSlot(event.output_index, item);
|
|
515
|
+
if (item.type === "reasoning" && slot?.type === "thinking") {
|
|
516
|
+
const summaryText = item.summary?.map((s) => s.text).join("\n\n") || "";
|
|
517
|
+
const contentText = item.content?.map((c) => c.text).join("\n\n") || "";
|
|
518
|
+
slot.block.thinking = summaryText || contentText || slot.block.thinking;
|
|
519
|
+
slot.block.thinkingSignature = JSON.stringify(item);
|
|
520
|
+
reasoningBlocksById.set(item.id, slot.block);
|
|
521
|
+
stream.push({
|
|
522
|
+
type: "thinking_end",
|
|
523
|
+
contentIndex: slot.contentIndex,
|
|
524
|
+
content: slot.block.thinking,
|
|
525
|
+
partial: output
|
|
526
|
+
});
|
|
527
|
+
outputSlots.delete(event.output_index);
|
|
528
|
+
} else if (item.type === "message" && slot?.type === "text") {
|
|
529
|
+
slot.block.text = item.content?.map((c) => c.type === "output_text" ? c.text : c.refusal).join("") || "";
|
|
530
|
+
slot.block.textSignature = encodeTextSignatureV1(item.id, item.phase ?? void 0);
|
|
531
|
+
stream.push({
|
|
532
|
+
type: "text_end",
|
|
533
|
+
contentIndex: slot.contentIndex,
|
|
534
|
+
content: slot.block.text,
|
|
535
|
+
partial: output
|
|
536
|
+
});
|
|
537
|
+
outputSlots.delete(event.output_index);
|
|
538
|
+
} else if (item.type === "function_call" && slot?.type === "toolCall" && slot.block.partialJson !== void 0) {
|
|
539
|
+
slot.block.arguments = parseStreamingJson(item.arguments || slot.block.partialJson || "{}");
|
|
540
|
+
if (item.namespace !== void 0) slot.block.namespace = item.namespace;
|
|
541
|
+
delete slot.block.partialJson;
|
|
542
|
+
stream.push({
|
|
543
|
+
type: "toolcall_end",
|
|
544
|
+
contentIndex: slot.contentIndex,
|
|
545
|
+
toolCall: slot.block,
|
|
546
|
+
partial: output
|
|
547
|
+
});
|
|
548
|
+
outputSlots.delete(event.output_index);
|
|
549
|
+
} else if (item.type === "custom_tool_call" && slot?.type === "toolCall" && slot.block.customInput) {
|
|
550
|
+
pushToolCallDelta(slot, appendCustomToolCallInput(slot.block, item.input ?? getCustomToolCallInput(slot.block), true));
|
|
551
|
+
if (item.namespace !== void 0) slot.block.namespace = item.namespace;
|
|
552
|
+
delete slot.block.customInput;
|
|
553
|
+
stream.push({
|
|
554
|
+
type: "toolcall_end",
|
|
555
|
+
contentIndex: slot.contentIndex,
|
|
556
|
+
toolCall: slot.block,
|
|
557
|
+
partial: output
|
|
558
|
+
});
|
|
559
|
+
outputSlots.delete(event.output_index);
|
|
560
|
+
}
|
|
561
|
+
} else if (event.type === "response.completed" || event.type === "response.incomplete") finalizeResponse(event.response);
|
|
562
|
+
else if (event.type === "error") throw new Error(`Error Code ${event.code}: ${event.message}` || "Unknown error");
|
|
563
|
+
else if (event.type === "response.failed") {
|
|
564
|
+
sawTerminalResponseEvent = true;
|
|
565
|
+
output.rawStopReason = event.response?.status;
|
|
566
|
+
const error = event.response?.error;
|
|
567
|
+
const details = event.response?.incomplete_details;
|
|
568
|
+
const msg = error ? `${error.code || "unknown"}: ${error.message || "no message"}` : details?.reason ? `incomplete: ${details.reason}` : "Unknown error (no error details in response)";
|
|
569
|
+
throw new Error(msg);
|
|
570
|
+
}
|
|
571
|
+
}
|
|
572
|
+
if (!sawTerminalResponseEvent) throw new Error("OpenAI Responses stream ended before a terminal response event");
|
|
573
|
+
if (output.stopReason === "toolUse") for (const block of output.content) {
|
|
574
|
+
if (block.type !== "toolCall") continue;
|
|
575
|
+
const toolCall = block;
|
|
576
|
+
if (toolCall.partialJson !== void 0 || toolCall.customInput !== void 0) throw new Error(`OpenAI Responses stream completed with an unfinished tool call: ${toolCall.name} (${toolCall.id})`);
|
|
577
|
+
}
|
|
578
|
+
}
|
|
579
|
+
function mapStopReason(status, incompleteReason) {
|
|
580
|
+
if (!status) return { stopReason: "stop" };
|
|
581
|
+
switch (status) {
|
|
582
|
+
case "completed": return { stopReason: "stop" };
|
|
583
|
+
case "incomplete":
|
|
584
|
+
if (incompleteReason === "max_output_tokens") return { stopReason: "length" };
|
|
585
|
+
return {
|
|
586
|
+
stopReason: "error",
|
|
587
|
+
errorMessage: incompleteReason ? `Response incomplete: ${incompleteReason}` : "Response incomplete without a provider reason"
|
|
588
|
+
};
|
|
589
|
+
case "failed":
|
|
590
|
+
case "cancelled": return { stopReason: "error" };
|
|
591
|
+
case "in_progress":
|
|
592
|
+
case "queued": return { stopReason: "stop" };
|
|
593
|
+
default: throw new Error(`Unhandled stop reason: ${status}`);
|
|
594
|
+
}
|
|
595
|
+
}
|
|
596
|
+
//#endregion
|
|
597
|
+
export { convertResponsesTools as n, processResponsesStream as r, convertResponsesMessages as t };
|
|
598
|
+
|
|
599
|
+
//# sourceMappingURL=openai-responses-shared-mhuo1Jov.js.map
|