@goah/cli 0.13.0 → 0.13.1

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 (236) hide show
  1. package/dist/LICENSE +201 -0
  2. package/dist/THIRD-PARTY-NOTICES.md +2319 -0
  3. package/dist/anthropic-messages-WBFOHES5.js +7214 -0
  4. package/dist/azure-openai-responses-NZFH3PO6.js +253 -0
  5. package/dist/chunk-25XSBFX6.js +639 -0
  6. package/dist/chunk-2QBQZGXF.js +110 -0
  7. package/dist/chunk-2ZP2NBW3.js +345 -0
  8. package/dist/chunk-4KA63VNZ.js +2 -0
  9. package/dist/chunk-4M6TXTT5.js +86 -0
  10. package/dist/chunk-4US7ZHT3.js +20 -0
  11. package/dist/chunk-6M3OOCKO.js +11809 -0
  12. package/dist/chunk-7AXAFDPG.js +17 -0
  13. package/dist/chunk-BXA47DTF.js +548 -0
  14. package/dist/chunk-FSBUS2BI.js +26 -0
  15. package/dist/chunk-GKDRAHLI.js +4787 -0
  16. package/dist/chunk-H462EFJP.js +89 -0
  17. package/dist/chunk-IIJQ3DW4.js +35 -0
  18. package/dist/chunk-JQD5EWJY.js +34 -0
  19. package/dist/chunk-K26BVR6O.js +61 -0
  20. package/dist/chunk-KAO7WIXG.js +31 -0
  21. package/dist/chunk-LBKI23CC.js +9810 -0
  22. package/dist/chunk-LKBKZEHT.js +41 -0
  23. package/dist/chunk-MD7TLTXZ.js +18 -0
  24. package/dist/chunk-MPXK7QBL.js +868 -0
  25. package/dist/chunk-NRLGWZJC.js +11 -0
  26. package/dist/chunk-PFEV3HWI.js +1178 -0
  27. package/dist/chunk-QBD6EJ2Z.js +1746 -0
  28. package/dist/chunk-R6I3VKME.js +132 -0
  29. package/dist/chunk-TJON22J4.js +87 -0
  30. package/dist/chunk-ULY36ME4.js +33097 -0
  31. package/dist/chunk-VWEV4LTH.js +25 -0
  32. package/dist/chunk-X7UWW2PN.js +883 -0
  33. package/dist/chunk-XALZLRYK.js +9221 -0
  34. package/dist/chunk-XPQKM3L7.js +57 -0
  35. package/dist/chunk-YH6YJ2IB.js +1773 -0
  36. package/dist/cli.d.ts +0 -2
  37. package/dist/cli.js +13123 -580
  38. package/dist/console/assets/{index-Rtc_B7cs.js → index-B1vGMXKr.js} +11 -11
  39. package/dist/console/assets/{index-Rtc_B7cs.js.map → index-B1vGMXKr.js.map} +1 -1
  40. package/dist/console/index.html +1 -1
  41. package/dist/dist-65MDQA4Q.js +1126 -0
  42. package/dist/dist-KNR2XL6J.js +39 -0
  43. package/dist/execution.d.ts +557 -2
  44. package/dist/execution.js +37 -2
  45. package/dist/faux-runner-worker.js +128 -0
  46. package/dist/google-generative-ai-YMHXZSFE.js +422 -0
  47. package/dist/google-vertex-X6DB3ZQ6.js +471 -0
  48. package/dist/index.d.ts +853 -25
  49. package/dist/index.js +84 -385
  50. package/dist/kernel.d.ts +39 -2
  51. package/dist/kernel.js +21 -2
  52. package/dist/mistral-conversations-TY3MMD6G.js +718 -0
  53. package/dist/multipart-parser-F6M4NJZU.js +373 -0
  54. package/dist/openai-codex-responses-BRDUMLN5.js +1374 -0
  55. package/dist/openai-completions-CX634XN3.js +1139 -0
  56. package/dist/openai-responses-HMCXJIHT.js +300 -0
  57. package/dist/openrouter-images-KTWET2H2.js +148 -0
  58. package/dist/pi-messages-XL5OEC4K.js +307 -0
  59. package/dist/pi-worker.js +671 -0
  60. package/dist/runner-pi.d.ts +342 -2
  61. package/dist/runner-pi.js +46 -2
  62. package/dist/runner-registry-M776E3SE.js +22 -0
  63. package/dist/sqlite.d.ts +446 -2
  64. package/dist/sqlite.js +15 -2
  65. package/dist/src-F2KBSV4T.js +1398 -0
  66. package/dist/supervisor.d.ts +625 -2
  67. package/dist/supervisor.js +39 -2
  68. package/dist/testkit.d.ts +564 -2
  69. package/dist/testkit.js +127 -2
  70. package/dist/transcript.d.ts +84 -2
  71. package/dist/transcript.js +25 -2
  72. package/dist/verification-worker.js +72 -0
  73. package/package.json +2 -26
  74. package/dist/cli.d.ts.map +0 -1
  75. package/dist/cli.js.map +0 -1
  76. package/dist/control.d.ts +0 -106
  77. package/dist/control.d.ts.map +0 -1
  78. package/dist/control.js +0 -242
  79. package/dist/control.js.map +0 -1
  80. package/dist/execution.d.ts.map +0 -1
  81. package/dist/execution.js.map +0 -1
  82. package/dist/index.d.ts.map +0 -1
  83. package/dist/index.js.map +0 -1
  84. package/dist/kernel.d.ts.map +0 -1
  85. package/dist/kernel.js.map +0 -1
  86. package/dist/live-config.d.ts +0 -10
  87. package/dist/live-config.d.ts.map +0 -1
  88. package/dist/live-config.js +0 -44
  89. package/dist/live-config.js.map +0 -1
  90. package/dist/prompt-queue.d.ts +0 -13
  91. package/dist/prompt-queue.d.ts.map +0 -1
  92. package/dist/prompt-queue.js +0 -35
  93. package/dist/prompt-queue.js.map +0 -1
  94. package/dist/runner-pi.d.ts.map +0 -1
  95. package/dist/runner-pi.js.map +0 -1
  96. package/dist/runner-registry.d.ts +0 -8
  97. package/dist/runner-registry.d.ts.map +0 -1
  98. package/dist/runner-registry.js +0 -12
  99. package/dist/runner-registry.js.map +0 -1
  100. package/dist/searchable-select.d.ts +0 -24
  101. package/dist/searchable-select.d.ts.map +0 -1
  102. package/dist/searchable-select.js +0 -61
  103. package/dist/searchable-select.js.map +0 -1
  104. package/dist/secret-input.d.ts +0 -8
  105. package/dist/secret-input.d.ts.map +0 -1
  106. package/dist/secret-input.js +0 -14
  107. package/dist/secret-input.js.map +0 -1
  108. package/dist/setup-wizard.d.ts +0 -22
  109. package/dist/setup-wizard.d.ts.map +0 -1
  110. package/dist/setup-wizard.js +0 -154
  111. package/dist/setup-wizard.js.map +0 -1
  112. package/dist/sqlite.d.ts.map +0 -1
  113. package/dist/sqlite.js.map +0 -1
  114. package/dist/supervisor.d.ts.map +0 -1
  115. package/dist/supervisor.js.map +0 -1
  116. package/dist/testkit.d.ts.map +0 -1
  117. package/dist/testkit.js.map +0 -1
  118. package/dist/thread-inspect.d.ts +0 -47
  119. package/dist/thread-inspect.d.ts.map +0 -1
  120. package/dist/thread-inspect.js +0 -78
  121. package/dist/thread-inspect.js.map +0 -1
  122. package/dist/transcript.d.ts.map +0 -1
  123. package/dist/transcript.js.map +0 -1
  124. package/dist/tui-theme.d.ts +0 -17
  125. package/dist/tui-theme.d.ts.map +0 -1
  126. package/dist/tui-theme.js +0 -19
  127. package/dist/tui-theme.js.map +0 -1
  128. package/dist/tui.d.ts +0 -24
  129. package/dist/tui.d.ts.map +0 -1
  130. package/dist/tui.js +0 -784
  131. package/dist/tui.js.map +0 -1
  132. package/dist/update.d.ts +0 -16
  133. package/dist/update.d.ts.map +0 -1
  134. package/dist/update.js +0 -67
  135. package/dist/update.js.map +0 -1
  136. package/dist/web-console.d.ts +0 -33
  137. package/dist/web-console.d.ts.map +0 -1
  138. package/dist/web-console.js +0 -242
  139. package/dist/web-console.js.map +0 -1
  140. package/dist/welcome.d.ts +0 -30
  141. package/dist/welcome.d.ts.map +0 -1
  142. package/dist/welcome.js +0 -69
  143. package/dist/welcome.js.map +0 -1
  144. package/node_modules/goah-ledger-contract/dist/execution.d.ts +0 -544
  145. package/node_modules/goah-ledger-contract/dist/execution.d.ts.map +0 -1
  146. package/node_modules/goah-ledger-contract/dist/execution.js +0 -52
  147. package/node_modules/goah-ledger-contract/dist/execution.js.map +0 -1
  148. package/node_modules/goah-ledger-contract/dist/index.d.ts +0 -4
  149. package/node_modules/goah-ledger-contract/dist/index.d.ts.map +0 -1
  150. package/node_modules/goah-ledger-contract/dist/index.js +0 -4
  151. package/node_modules/goah-ledger-contract/dist/index.js.map +0 -1
  152. package/node_modules/goah-ledger-contract/dist/kernel.d.ts +0 -37
  153. package/node_modules/goah-ledger-contract/dist/kernel.d.ts.map +0 -1
  154. package/node_modules/goah-ledger-contract/dist/kernel.js +0 -8
  155. package/node_modules/goah-ledger-contract/dist/kernel.js.map +0 -1
  156. package/node_modules/goah-ledger-contract/dist/transcript.d.ts +0 -63
  157. package/node_modules/goah-ledger-contract/dist/transcript.d.ts.map +0 -1
  158. package/node_modules/goah-ledger-contract/dist/transcript.js +0 -115
  159. package/node_modules/goah-ledger-contract/dist/transcript.js.map +0 -1
  160. package/node_modules/goah-ledger-contract/package.json +0 -28
  161. package/node_modules/goah-ledger-sqlite/dist/index.d.ts +0 -86
  162. package/node_modules/goah-ledger-sqlite/dist/index.d.ts.map +0 -1
  163. package/node_modules/goah-ledger-sqlite/dist/index.js +0 -1254
  164. package/node_modules/goah-ledger-sqlite/dist/index.js.map +0 -1
  165. package/node_modules/goah-ledger-sqlite/package.json +0 -26
  166. package/node_modules/goah-runner-pi/dist/configurator.d.ts +0 -20
  167. package/node_modules/goah-runner-pi/dist/configurator.d.ts.map +0 -1
  168. package/node_modules/goah-runner-pi/dist/configurator.js +0 -423
  169. package/node_modules/goah-runner-pi/dist/configurator.js.map +0 -1
  170. package/node_modules/goah-runner-pi/dist/credential-store.d.ts +0 -14
  171. package/node_modules/goah-runner-pi/dist/credential-store.d.ts.map +0 -1
  172. package/node_modules/goah-runner-pi/dist/credential-store.js +0 -163
  173. package/node_modules/goah-runner-pi/dist/credential-store.js.map +0 -1
  174. package/node_modules/goah-runner-pi/dist/env-spec.d.ts +0 -17
  175. package/node_modules/goah-runner-pi/dist/env-spec.d.ts.map +0 -1
  176. package/node_modules/goah-runner-pi/dist/env-spec.js +0 -95
  177. package/node_modules/goah-runner-pi/dist/env-spec.js.map +0 -1
  178. package/node_modules/goah-runner-pi/dist/index.d.ts +0 -76
  179. package/node_modules/goah-runner-pi/dist/index.d.ts.map +0 -1
  180. package/node_modules/goah-runner-pi/dist/index.js +0 -326
  181. package/node_modules/goah-runner-pi/dist/index.js.map +0 -1
  182. package/node_modules/goah-runner-pi/dist/model-provider.d.ts +0 -30
  183. package/node_modules/goah-runner-pi/dist/model-provider.d.ts.map +0 -1
  184. package/node_modules/goah-runner-pi/dist/model-provider.js +0 -96
  185. package/node_modules/goah-runner-pi/dist/model-provider.js.map +0 -1
  186. package/node_modules/goah-runner-pi/dist/pi-worker.d.ts +0 -31
  187. package/node_modules/goah-runner-pi/dist/pi-worker.d.ts.map +0 -1
  188. package/node_modules/goah-runner-pi/dist/pi-worker.js +0 -579
  189. package/node_modules/goah-runner-pi/dist/pi-worker.js.map +0 -1
  190. package/node_modules/goah-runner-pi/dist/steering-no-ack.test-fixture.d.ts +0 -2
  191. package/node_modules/goah-runner-pi/dist/steering-no-ack.test-fixture.d.ts.map +0 -1
  192. package/node_modules/goah-runner-pi/dist/steering-no-ack.test-fixture.js +0 -3
  193. package/node_modules/goah-runner-pi/dist/steering-no-ack.test-fixture.js.map +0 -1
  194. package/node_modules/goah-runner-pi/dist/steering-reject.test-fixture.d.ts +0 -2
  195. package/node_modules/goah-runner-pi/dist/steering-reject.test-fixture.d.ts.map +0 -1
  196. package/node_modules/goah-runner-pi/dist/steering-reject.test-fixture.js +0 -5
  197. package/node_modules/goah-runner-pi/dist/steering-reject.test-fixture.js.map +0 -1
  198. package/node_modules/goah-runner-pi/dist/steering-worker.test-fixture.d.ts +0 -2
  199. package/node_modules/goah-runner-pi/dist/steering-worker.test-fixture.d.ts.map +0 -1
  200. package/node_modules/goah-runner-pi/dist/steering-worker.test-fixture.js +0 -5
  201. package/node_modules/goah-runner-pi/dist/steering-worker.test-fixture.js.map +0 -1
  202. package/node_modules/goah-runner-pi/dist/test-helpers.d.ts +0 -4
  203. package/node_modules/goah-runner-pi/dist/test-helpers.d.ts.map +0 -1
  204. package/node_modules/goah-runner-pi/dist/test-helpers.js +0 -13
  205. package/node_modules/goah-runner-pi/dist/test-helpers.js.map +0 -1
  206. package/node_modules/goah-runner-pi/dist/verification-worker.d.ts +0 -2
  207. package/node_modules/goah-runner-pi/dist/verification-worker.d.ts.map +0 -1
  208. package/node_modules/goah-runner-pi/dist/verification-worker.js +0 -46
  209. package/node_modules/goah-runner-pi/dist/verification-worker.js.map +0 -1
  210. package/node_modules/goah-runner-pi/package.json +0 -28
  211. package/node_modules/goah-supervisor/dist/context-view.d.ts +0 -30
  212. package/node_modules/goah-supervisor/dist/context-view.d.ts.map +0 -1
  213. package/node_modules/goah-supervisor/dist/context-view.js +0 -58
  214. package/node_modules/goah-supervisor/dist/context-view.js.map +0 -1
  215. package/node_modules/goah-supervisor/dist/index.d.ts +0 -96
  216. package/node_modules/goah-supervisor/dist/index.d.ts.map +0 -1
  217. package/node_modules/goah-supervisor/dist/index.js +0 -1075
  218. package/node_modules/goah-supervisor/dist/index.js.map +0 -1
  219. package/node_modules/goah-supervisor/dist/roles.d.ts +0 -4
  220. package/node_modules/goah-supervisor/dist/roles.d.ts.map +0 -1
  221. package/node_modules/goah-supervisor/dist/roles.js +0 -29
  222. package/node_modules/goah-supervisor/dist/roles.js.map +0 -1
  223. package/node_modules/goah-supervisor/dist/verification.d.ts +0 -61
  224. package/node_modules/goah-supervisor/dist/verification.d.ts.map +0 -1
  225. package/node_modules/goah-supervisor/dist/verification.js +0 -135
  226. package/node_modules/goah-supervisor/dist/verification.js.map +0 -1
  227. package/node_modules/goah-supervisor/package.json +0 -26
  228. package/node_modules/goah-testkit/dist/faux-runner-worker.d.ts +0 -2
  229. package/node_modules/goah-testkit/dist/faux-runner-worker.d.ts.map +0 -1
  230. package/node_modules/goah-testkit/dist/faux-runner-worker.js +0 -82
  231. package/node_modules/goah-testkit/dist/faux-runner-worker.js.map +0 -1
  232. package/node_modules/goah-testkit/dist/index.d.ts +0 -49
  233. package/node_modules/goah-testkit/dist/index.d.ts.map +0 -1
  234. package/node_modules/goah-testkit/dist/index.js +0 -96
  235. package/node_modules/goah-testkit/dist/index.js.map +0 -1
  236. package/node_modules/goah-testkit/package.json +0 -29
@@ -0,0 +1,639 @@
1
+ import { createRequire as __goahCreateRequire } from "node:module";
2
+ const require = __goahCreateRequire(import.meta.url);
3
+ import {
4
+ shortHash
5
+ } from "./chunk-4US7ZHT3.js";
6
+ import {
7
+ appendGrammarToolInputJsonDelta,
8
+ getGrammarToolInput,
9
+ getJsonSchemaToolParameters,
10
+ resolveGrammarConstrainedSampling,
11
+ resolveJsonSchemaStrictSampling,
12
+ transformMessages
13
+ } from "./chunk-BXA47DTF.js";
14
+ import {
15
+ sanitizeSurrogates
16
+ } from "./chunk-NRLGWZJC.js";
17
+ import {
18
+ parseStreamingJson
19
+ } from "./chunk-2ZP2NBW3.js";
20
+ import {
21
+ calculateCost
22
+ } from "./chunk-MPXK7QBL.js";
23
+
24
+ // ../../node_modules/@earendil-works/pi-ai/dist/api/openai-responses-shared.js
25
+ function encodeTextSignatureV1(id, phase) {
26
+ const payload = { v: 1, id };
27
+ if (phase)
28
+ payload.phase = phase;
29
+ return JSON.stringify(payload);
30
+ }
31
+ function parseTextSignature(signature) {
32
+ if (!signature)
33
+ return void 0;
34
+ if (signature.startsWith("{")) {
35
+ try {
36
+ const parsed = JSON.parse(signature);
37
+ if (parsed.v === 1 && typeof parsed.id === "string") {
38
+ if (parsed.phase === "commentary" || parsed.phase === "final_answer") {
39
+ return { id: parsed.id, phase: parsed.phase };
40
+ }
41
+ return { id: parsed.id };
42
+ }
43
+ } catch {
44
+ }
45
+ }
46
+ return { id: signature };
47
+ }
48
+ function convertToolResultOutput(model, content) {
49
+ const textResult = content.filter((c) => c.type === "text").map((c) => c.text).join("\n");
50
+ const images = content.filter((c) => c.type === "image");
51
+ const hasText = textResult.length > 0;
52
+ if (images.length === 0 || !model.input.includes("image")) {
53
+ return sanitizeSurrogates(hasText ? textResult : images.length > 0 ? "(see attached image)" : "(no tool output)");
54
+ }
55
+ const output = [];
56
+ if (hasText) {
57
+ output.push({ type: "input_text", text: sanitizeSurrogates(textResult) });
58
+ }
59
+ for (const image of images) {
60
+ output.push({
61
+ type: "input_image",
62
+ detail: "auto",
63
+ image_url: `data:${image.mimeType};base64,${image.data}`
64
+ });
65
+ }
66
+ return output;
67
+ }
68
+ function convertResponsesMessages(model, context, allowedToolCallProviders, options) {
69
+ const messages = [];
70
+ const loadedToolNames = /* @__PURE__ */ new Set();
71
+ const normalizeIdPart = (part) => {
72
+ const sanitized = part.replace(/[^a-zA-Z0-9_-]/g, "_");
73
+ const normalized = sanitized.length > 64 ? sanitized.slice(0, 64) : sanitized;
74
+ return normalized.replace(/_+$/, "");
75
+ };
76
+ const buildForeignResponsesItemId = (itemId) => {
77
+ const normalized = `fc_${shortHash(itemId)}`;
78
+ return normalized.length > 64 ? normalized.slice(0, 64) : normalized;
79
+ };
80
+ const normalizeToolCallId = (id, _targetModel, source) => {
81
+ if (!allowedToolCallProviders.has(model.provider))
82
+ return normalizeIdPart(id);
83
+ if (!id.includes("|"))
84
+ return normalizeIdPart(id);
85
+ const [callId, itemId] = id.split("|");
86
+ const normalizedCallId = normalizeIdPart(callId);
87
+ const isForeignToolCall = source.provider !== model.provider || source.api !== model.api;
88
+ let normalizedItemId = isForeignToolCall ? buildForeignResponsesItemId(itemId) : normalizeIdPart(itemId);
89
+ if (!normalizedItemId.startsWith("fc_")) {
90
+ normalizedItemId = normalizeIdPart(`fc_${normalizedItemId}`);
91
+ }
92
+ return `${normalizedCallId}|${normalizedItemId}`;
93
+ };
94
+ const transformedMessages = transformMessages(context.messages, model, normalizeToolCallId);
95
+ const includeSystemPrompt = options?.includeSystemPrompt ?? true;
96
+ if (includeSystemPrompt && context.systemPrompt) {
97
+ const compat = model.compat;
98
+ const role = model.reasoning && compat?.supportsDeveloperRole !== false ? "developer" : "system";
99
+ messages.push({
100
+ role,
101
+ content: sanitizeSurrogates(context.systemPrompt)
102
+ });
103
+ }
104
+ let msgIndex = 0;
105
+ for (const msg of transformedMessages) {
106
+ if (msg.role === "user") {
107
+ if (typeof msg.content === "string") {
108
+ messages.push({
109
+ role: "user",
110
+ content: [{ type: "input_text", text: sanitizeSurrogates(msg.content) }]
111
+ });
112
+ } else {
113
+ const content = msg.content.map((item) => {
114
+ if (item.type === "text") {
115
+ return {
116
+ type: "input_text",
117
+ text: sanitizeSurrogates(item.text)
118
+ };
119
+ }
120
+ return {
121
+ type: "input_image",
122
+ detail: "auto",
123
+ image_url: `data:${item.mimeType};base64,${item.data}`
124
+ };
125
+ });
126
+ if (content.length === 0)
127
+ continue;
128
+ messages.push({
129
+ role: "user",
130
+ content
131
+ });
132
+ }
133
+ } else if (msg.role === "assistant") {
134
+ const output = [];
135
+ const assistantMsg = msg;
136
+ const isSameProviderAndApi = assistantMsg.provider === model.provider && assistantMsg.api === model.api;
137
+ const isSameModel = isSameProviderAndApi && assistantMsg.model === model.id;
138
+ const isDifferentModel = isSameProviderAndApi && assistantMsg.model !== model.id;
139
+ let textBlockIndex = 0;
140
+ for (const block of msg.content) {
141
+ if (block.type === "thinking") {
142
+ if (block.thinkingSignature) {
143
+ const reasoningItem = JSON.parse(block.thinkingSignature);
144
+ output.push(reasoningItem);
145
+ }
146
+ } else if (block.type === "text") {
147
+ const textBlock = block;
148
+ const parsedSignature = parseTextSignature(textBlock.textSignature);
149
+ const fallbackMessageId = textBlockIndex === 0 ? `msg_pi_${msgIndex}` : `msg_pi_${msgIndex}_${textBlockIndex}`;
150
+ textBlockIndex++;
151
+ let msgId = parsedSignature?.id;
152
+ if (!msgId) {
153
+ msgId = fallbackMessageId;
154
+ } else if (msgId.length > 64) {
155
+ msgId = `msg_${shortHash(msgId)}`;
156
+ }
157
+ output.push({
158
+ type: "message",
159
+ role: "assistant",
160
+ content: [{ type: "output_text", text: sanitizeSurrogates(textBlock.text), annotations: [] }],
161
+ status: "completed",
162
+ id: msgId,
163
+ phase: parsedSignature?.phase
164
+ });
165
+ } else if (block.type === "toolCall") {
166
+ const toolCall = block;
167
+ const [callId, itemIdRaw] = toolCall.id.split("|");
168
+ const customInputProperty = options?.grammarToolInputProperties?.get(toolCall.name);
169
+ let itemId = itemIdRaw;
170
+ if (isDifferentModel && itemId?.startsWith("fc_") || customInputProperty === void 0 && !itemId?.startsWith("fc_")) {
171
+ itemId = void 0;
172
+ }
173
+ const canReplayNamespace = isSameModel || options?.deferredTools?.has(toolCall.name) === true;
174
+ if (customInputProperty !== void 0) {
175
+ output.push({
176
+ type: "custom_tool_call",
177
+ id: itemId,
178
+ call_id: callId,
179
+ name: toolCall.name,
180
+ input: sanitizeSurrogates(getGrammarToolInput(toolCall.name, toolCall.arguments, customInputProperty)),
181
+ ...canReplayNamespace && toolCall.namespace !== void 0 ? { namespace: toolCall.namespace } : {}
182
+ });
183
+ } else {
184
+ output.push({
185
+ type: "function_call",
186
+ id: itemId,
187
+ call_id: callId,
188
+ name: toolCall.name,
189
+ arguments: JSON.stringify(toolCall.arguments),
190
+ ...canReplayNamespace && toolCall.namespace !== void 0 ? { namespace: toolCall.namespace } : {}
191
+ });
192
+ }
193
+ }
194
+ }
195
+ if (output.length === 0)
196
+ continue;
197
+ messages.push(...output);
198
+ } else if (msg.role === "toolResult") {
199
+ const [callId] = msg.toolCallId.split("|");
200
+ const output = convertToolResultOutput(model, msg.content);
201
+ if (options?.grammarToolInputProperties?.has(msg.toolName)) {
202
+ messages.push({
203
+ type: "custom_tool_call_output",
204
+ call_id: callId,
205
+ output
206
+ });
207
+ } else {
208
+ messages.push({
209
+ type: "function_call_output",
210
+ call_id: callId,
211
+ output
212
+ });
213
+ }
214
+ const deferredTools = [];
215
+ for (const name of msg.addedToolNames ?? []) {
216
+ const tool = options?.deferredTools?.get(name);
217
+ if (!tool || loadedToolNames.has(name))
218
+ continue;
219
+ loadedToolNames.add(name);
220
+ deferredTools.push(tool);
221
+ }
222
+ if (deferredTools.length > 0 && options?.deferredToolsMode === "additional-tools") {
223
+ messages.push({
224
+ type: "additional_tools",
225
+ role: "developer",
226
+ tools: convertResponsesTools(deferredTools, options.toolOptions)
227
+ });
228
+ } else if (deferredTools.length > 0 && options?.deferredToolsMode === "tool-search") {
229
+ const names = deferredTools.map((tool) => tool.name);
230
+ const searchCallId = `pi_tool_load_${shortHash(`${msg.toolCallId}:${names.join(",")}`)}`;
231
+ messages.push({
232
+ type: "tool_search_call",
233
+ call_id: searchCallId,
234
+ execution: "client",
235
+ status: "completed",
236
+ arguments: { query: names.join(" "), limit: names.length }
237
+ });
238
+ messages.push({
239
+ type: "tool_search_output",
240
+ call_id: searchCallId,
241
+ execution: "client",
242
+ status: "completed",
243
+ tools: convertResponsesTools(deferredTools, {
244
+ ...options.toolOptions,
245
+ deferLoading: true
246
+ })
247
+ });
248
+ }
249
+ }
250
+ msgIndex++;
251
+ }
252
+ return messages;
253
+ }
254
+ function convertResponsesTools(tools, options) {
255
+ const defaultStrict = options?.strict === void 0 ? false : options.strict;
256
+ const supportsStrictMode = options?.supportsStrictMode ?? true;
257
+ const supportsOpenAIGrammarTools = options?.supportsOpenAIGrammarTools ?? false;
258
+ return tools.map((tool) => {
259
+ const grammar = resolveGrammarConstrainedSampling(tool, supportsOpenAIGrammarTools);
260
+ if (grammar) {
261
+ return {
262
+ type: "custom",
263
+ name: tool.name,
264
+ description: tool.description,
265
+ format: {
266
+ type: "grammar",
267
+ syntax: grammar.format,
268
+ definition: grammar.definition
269
+ },
270
+ ...options?.deferLoading ? { defer_loading: true } : {}
271
+ };
272
+ }
273
+ const constrainedStrict = resolveJsonSchemaStrictSampling(tool, supportsStrictMode);
274
+ const strict = constrainedStrict ?? defaultStrict;
275
+ const functionTool = {
276
+ type: "function",
277
+ name: tool.name,
278
+ description: tool.description,
279
+ parameters: getJsonSchemaToolParameters(tool, strict === true),
280
+ ...options?.deferLoading ? { defer_loading: true } : {}
281
+ };
282
+ if (supportsStrictMode) {
283
+ functionTool.strict = strict;
284
+ }
285
+ return functionTool;
286
+ });
287
+ }
288
+ function getCustomToolCallInput(block) {
289
+ const property = block.customInput?.property;
290
+ if (property === void 0)
291
+ return "";
292
+ const value = block.arguments[property];
293
+ return typeof value === "string" ? value : "";
294
+ }
295
+ function appendCustomToolCallInput(block, nextInput, close) {
296
+ const customInput = block.customInput;
297
+ if (!customInput)
298
+ return void 0;
299
+ const delta = appendGrammarToolInputJsonDelta(customInput.jsonBuffer, customInput.property, nextInput, close);
300
+ block.arguments = { [customInput.property]: nextInput };
301
+ return delta;
302
+ }
303
+ async function processResponsesStream(openaiStream, output, stream, model, options) {
304
+ let sawTerminalResponseEvent = false;
305
+ const outputSlots = /* @__PURE__ */ new Map();
306
+ const reasoningBlocksById = /* @__PURE__ */ new Map();
307
+ const applyMessagePhaseStopReason = (item) => {
308
+ if (item.type === "message" && item.phase === "final_answer") {
309
+ output.stopReason = "stop";
310
+ }
311
+ };
312
+ const getSlot = (outputIndex, type) => {
313
+ const slot = outputSlots.get(outputIndex);
314
+ return slot?.type === type ? slot : void 0;
315
+ };
316
+ const pushToolCallDelta = (slot, delta) => {
317
+ if (delta === void 0)
318
+ return;
319
+ stream.push({
320
+ type: "toolcall_delta",
321
+ contentIndex: slot.contentIndex,
322
+ delta,
323
+ partial: output
324
+ });
325
+ };
326
+ const createSlot = (outputIndex, item) => {
327
+ if (item.type === "reasoning") {
328
+ const block = { type: "thinking", thinking: "" };
329
+ output.content.push(block);
330
+ const slot = {
331
+ type: "thinking",
332
+ block,
333
+ contentIndex: output.content.length - 1
334
+ };
335
+ outputSlots.set(outputIndex, slot);
336
+ stream.push({ type: "thinking_start", contentIndex: slot.contentIndex, partial: output });
337
+ return slot;
338
+ }
339
+ if (item.type === "message") {
340
+ applyMessagePhaseStopReason(item);
341
+ const block = { type: "text", text: "" };
342
+ output.content.push(block);
343
+ const slot = { type: "text", block, contentIndex: output.content.length - 1 };
344
+ outputSlots.set(outputIndex, slot);
345
+ stream.push({ type: "text_start", contentIndex: slot.contentIndex, partial: output });
346
+ return slot;
347
+ }
348
+ if (item.type === "function_call") {
349
+ const block = {
350
+ type: "toolCall",
351
+ id: `${item.call_id}|${item.id}`,
352
+ name: item.name,
353
+ arguments: {},
354
+ ...item.namespace !== void 0 ? { namespace: item.namespace } : {},
355
+ partialJson: item.arguments || ""
356
+ };
357
+ output.content.push(block);
358
+ const slot = {
359
+ type: "toolCall",
360
+ block,
361
+ contentIndex: output.content.length - 1
362
+ };
363
+ outputSlots.set(outputIndex, slot);
364
+ stream.push({ type: "toolcall_start", contentIndex: slot.contentIndex, partial: output });
365
+ return slot;
366
+ }
367
+ if (item.type === "custom_tool_call") {
368
+ const inputProperty = options?.grammarToolInputProperties?.get(item.name) ?? "input";
369
+ const input = item.input || "";
370
+ const block = {
371
+ type: "toolCall",
372
+ id: `${item.call_id}|${item.id}`,
373
+ name: item.name,
374
+ arguments: { [inputProperty]: input },
375
+ ...item.namespace !== void 0 ? { namespace: item.namespace } : {},
376
+ customInput: {
377
+ property: inputProperty,
378
+ jsonBuffer: { input: "", started: false, closed: false }
379
+ }
380
+ };
381
+ output.content.push(block);
382
+ const slot = {
383
+ type: "toolCall",
384
+ block,
385
+ contentIndex: output.content.length - 1
386
+ };
387
+ outputSlots.set(outputIndex, slot);
388
+ stream.push({ type: "toolcall_start", contentIndex: slot.contentIndex, partial: output });
389
+ return slot;
390
+ }
391
+ return void 0;
392
+ };
393
+ const getOrCreateSlot = (outputIndex, item) => {
394
+ return outputSlots.get(outputIndex) ?? createSlot(outputIndex, item);
395
+ };
396
+ const backfillReasoningSignatures = (responseOutput) => {
397
+ for (const item of responseOutput) {
398
+ if (item.type !== "reasoning" || !item.encrypted_content)
399
+ continue;
400
+ const block = reasoningBlocksById.get(item.id);
401
+ if (!block?.thinkingSignature)
402
+ continue;
403
+ const storedItem = JSON.parse(block.thinkingSignature);
404
+ if (storedItem.encrypted_content)
405
+ continue;
406
+ block.thinkingSignature = JSON.stringify({
407
+ ...storedItem,
408
+ encrypted_content: item.encrypted_content
409
+ });
410
+ }
411
+ };
412
+ const finalizeResponse = (response) => {
413
+ sawTerminalResponseEvent = true;
414
+ backfillReasoningSignatures(response.output ?? []);
415
+ if (response?.id) {
416
+ output.responseId = response.id;
417
+ }
418
+ if (response?.usage) {
419
+ const inputDetails = response.usage.input_tokens_details;
420
+ const cachedTokens = inputDetails?.cached_tokens || 0;
421
+ const cacheWriteTokens = inputDetails?.cache_write_tokens || 0;
422
+ output.usage = {
423
+ // OpenAI includes cached and cache-write tokens in input_tokens, so subtract both.
424
+ input: Math.max(0, (response.usage.input_tokens || 0) - cachedTokens - cacheWriteTokens),
425
+ output: response.usage.output_tokens || 0,
426
+ cacheRead: cachedTokens,
427
+ cacheWrite: cacheWriteTokens,
428
+ reasoning: response.usage.output_tokens_details?.reasoning_tokens || 0,
429
+ totalTokens: response.usage.total_tokens || 0,
430
+ cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 }
431
+ };
432
+ }
433
+ calculateCost(model, output.usage);
434
+ if (options?.applyServiceTierPricing) {
435
+ const serviceTier = options.resolveServiceTier ? options.resolveServiceTier(response?.service_tier, options.serviceTier) : response?.service_tier ?? options.serviceTier;
436
+ options.applyServiceTierPricing(output.usage, serviceTier);
437
+ }
438
+ const status = response?.status;
439
+ const incompleteDetails = response?.incomplete_details;
440
+ const incompleteReason = typeof incompleteDetails?.reason === "string" ? incompleteDetails.reason : void 0;
441
+ output.rawStopReason = incompleteReason ? `${status}.${incompleteReason}` : status;
442
+ const mappedStop = mapStopReason(status, incompleteReason);
443
+ output.stopReason = mappedStop.stopReason;
444
+ output.errorMessage = mappedStop.errorMessage;
445
+ if (output.content.some((b) => b.type === "toolCall") && output.stopReason === "stop") {
446
+ output.stopReason = "toolUse";
447
+ }
448
+ };
449
+ for await (const event of openaiStream) {
450
+ if (event.type === "response.created") {
451
+ output.responseId = event.response.id;
452
+ } else if (event.type === "response.output_item.added") {
453
+ createSlot(event.output_index, event.item);
454
+ } else if (event.type === "response.reasoning_summary_text.delta") {
455
+ const slot = getSlot(event.output_index, "thinking");
456
+ if (!slot)
457
+ continue;
458
+ slot.block.thinking += event.delta;
459
+ stream.push({
460
+ type: "thinking_delta",
461
+ contentIndex: slot.contentIndex,
462
+ delta: event.delta,
463
+ partial: output
464
+ });
465
+ } else if (event.type === "response.reasoning_summary_part.done") {
466
+ const slot = getSlot(event.output_index, "thinking");
467
+ if (!slot)
468
+ continue;
469
+ slot.block.thinking += "\n\n";
470
+ stream.push({
471
+ type: "thinking_delta",
472
+ contentIndex: slot.contentIndex,
473
+ delta: "\n\n",
474
+ partial: output
475
+ });
476
+ } else if (event.type === "response.reasoning_text.delta") {
477
+ const slot = getSlot(event.output_index, "thinking");
478
+ if (!slot)
479
+ continue;
480
+ slot.block.thinking += event.delta;
481
+ stream.push({
482
+ type: "thinking_delta",
483
+ contentIndex: slot.contentIndex,
484
+ delta: event.delta,
485
+ partial: output
486
+ });
487
+ } else if (event.type === "response.output_text.delta") {
488
+ const slot = getSlot(event.output_index, "text");
489
+ if (!slot)
490
+ continue;
491
+ slot.block.text += event.delta;
492
+ stream.push({
493
+ type: "text_delta",
494
+ contentIndex: slot.contentIndex,
495
+ delta: event.delta,
496
+ partial: output
497
+ });
498
+ } else if (event.type === "response.refusal.delta") {
499
+ const slot = getSlot(event.output_index, "text");
500
+ if (!slot)
501
+ continue;
502
+ slot.block.text += event.delta;
503
+ stream.push({
504
+ type: "text_delta",
505
+ contentIndex: slot.contentIndex,
506
+ delta: event.delta,
507
+ partial: output
508
+ });
509
+ } else if (event.type === "response.function_call_arguments.delta") {
510
+ const slot = getSlot(event.output_index, "toolCall");
511
+ if (!slot || slot.block.partialJson === void 0)
512
+ continue;
513
+ slot.block.partialJson += event.delta;
514
+ slot.block.arguments = parseStreamingJson(slot.block.partialJson);
515
+ pushToolCallDelta(slot, event.delta);
516
+ } else if (event.type === "response.function_call_arguments.done") {
517
+ const slot = getSlot(event.output_index, "toolCall");
518
+ if (!slot || slot.block.partialJson === void 0)
519
+ continue;
520
+ const previousPartialJson = slot.block.partialJson;
521
+ slot.block.partialJson = event.arguments;
522
+ slot.block.arguments = parseStreamingJson(slot.block.partialJson);
523
+ if (event.arguments.startsWith(previousPartialJson)) {
524
+ const delta = event.arguments.slice(previousPartialJson.length);
525
+ if (delta.length > 0)
526
+ pushToolCallDelta(slot, delta);
527
+ }
528
+ } else if (event.type === "response.custom_tool_call_input.delta") {
529
+ const slot = getSlot(event.output_index, "toolCall");
530
+ if (!slot || !slot.block.customInput)
531
+ continue;
532
+ pushToolCallDelta(slot, appendCustomToolCallInput(slot.block, getCustomToolCallInput(slot.block) + event.delta, false));
533
+ } else if (event.type === "response.custom_tool_call_input.done") {
534
+ const slot = getSlot(event.output_index, "toolCall");
535
+ if (!slot || !slot.block.customInput)
536
+ continue;
537
+ pushToolCallDelta(slot, appendCustomToolCallInput(slot.block, event.input, true));
538
+ } else if (event.type === "response.output_item.done") {
539
+ const item = event.item;
540
+ applyMessagePhaseStopReason(item);
541
+ const slot = getOrCreateSlot(event.output_index, item);
542
+ if (item.type === "reasoning" && slot?.type === "thinking") {
543
+ const summaryText = item.summary?.map((s) => s.text).join("\n\n") || "";
544
+ const contentText = item.content?.map((c) => c.text).join("\n\n") || "";
545
+ slot.block.thinking = summaryText || contentText || slot.block.thinking;
546
+ slot.block.thinkingSignature = JSON.stringify(item);
547
+ reasoningBlocksById.set(item.id, slot.block);
548
+ stream.push({
549
+ type: "thinking_end",
550
+ contentIndex: slot.contentIndex,
551
+ content: slot.block.thinking,
552
+ partial: output
553
+ });
554
+ outputSlots.delete(event.output_index);
555
+ } else if (item.type === "message" && slot?.type === "text") {
556
+ slot.block.text = item.content?.map((c) => c.type === "output_text" ? c.text : c.refusal).join("") || "";
557
+ slot.block.textSignature = encodeTextSignatureV1(item.id, item.phase ?? void 0);
558
+ stream.push({
559
+ type: "text_end",
560
+ contentIndex: slot.contentIndex,
561
+ content: slot.block.text,
562
+ partial: output
563
+ });
564
+ outputSlots.delete(event.output_index);
565
+ } else if (item.type === "function_call" && slot?.type === "toolCall" && slot.block.partialJson !== void 0) {
566
+ slot.block.arguments = parseStreamingJson(item.arguments || slot.block.partialJson || "{}");
567
+ if (item.namespace !== void 0)
568
+ slot.block.namespace = item.namespace;
569
+ delete slot.block.partialJson;
570
+ stream.push({
571
+ type: "toolcall_end",
572
+ contentIndex: slot.contentIndex,
573
+ toolCall: slot.block,
574
+ partial: output
575
+ });
576
+ outputSlots.delete(event.output_index);
577
+ } else if (item.type === "custom_tool_call" && slot?.type === "toolCall" && slot.block.customInput) {
578
+ pushToolCallDelta(slot, appendCustomToolCallInput(slot.block, item.input ?? getCustomToolCallInput(slot.block), true));
579
+ if (item.namespace !== void 0)
580
+ slot.block.namespace = item.namespace;
581
+ delete slot.block.customInput;
582
+ stream.push({
583
+ type: "toolcall_end",
584
+ contentIndex: slot.contentIndex,
585
+ toolCall: slot.block,
586
+ partial: output
587
+ });
588
+ outputSlots.delete(event.output_index);
589
+ }
590
+ } else if (event.type === "response.completed" || event.type === "response.incomplete") {
591
+ finalizeResponse(event.response);
592
+ } else if (event.type === "error") {
593
+ throw new Error(`Error Code ${event.code}: ${event.message}` || "Unknown error");
594
+ } else if (event.type === "response.failed") {
595
+ sawTerminalResponseEvent = true;
596
+ output.rawStopReason = event.response?.status;
597
+ const error = event.response?.error;
598
+ const details = event.response?.incomplete_details;
599
+ const msg = error ? `${error.code || "unknown"}: ${error.message || "no message"}` : details?.reason ? `incomplete: ${details.reason}` : "Unknown error (no error details in response)";
600
+ throw new Error(msg);
601
+ }
602
+ }
603
+ if (!sawTerminalResponseEvent) {
604
+ throw new Error("OpenAI Responses stream ended before a terminal response event");
605
+ }
606
+ }
607
+ function mapStopReason(status, incompleteReason) {
608
+ if (!status)
609
+ return { stopReason: "stop" };
610
+ switch (status) {
611
+ case "completed":
612
+ return { stopReason: "stop" };
613
+ case "incomplete":
614
+ if (incompleteReason === "max_output_tokens") {
615
+ return { stopReason: "length" };
616
+ }
617
+ return {
618
+ stopReason: "error",
619
+ errorMessage: incompleteReason ? `Response incomplete: ${incompleteReason}` : "Response incomplete without a provider reason"
620
+ };
621
+ case "failed":
622
+ case "cancelled":
623
+ return { stopReason: "error" };
624
+ // These two are wonky ...
625
+ case "in_progress":
626
+ case "queued":
627
+ return { stopReason: "stop" };
628
+ default: {
629
+ const _exhaustive = status;
630
+ throw new Error(`Unhandled stop reason: ${_exhaustive}`);
631
+ }
632
+ }
633
+ }
634
+
635
+ export {
636
+ convertResponsesMessages,
637
+ convertResponsesTools,
638
+ processResponsesStream
639
+ };