@goah/cli 0.11.5 → 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 (242) 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 +13148 -506
  38. package/dist/console/assets/index-3sYrDMmt.css +1 -0
  39. package/dist/console/assets/index-B1vGMXKr.js +52 -0
  40. package/dist/console/assets/index-B1vGMXKr.js.map +1 -0
  41. package/dist/console/index.html +2 -2
  42. package/dist/dist-65MDQA4Q.js +1126 -0
  43. package/dist/dist-KNR2XL6J.js +39 -0
  44. package/dist/execution.d.ts +557 -2
  45. package/dist/execution.js +37 -2
  46. package/dist/faux-runner-worker.js +128 -0
  47. package/dist/google-generative-ai-YMHXZSFE.js +422 -0
  48. package/dist/google-vertex-X6DB3ZQ6.js +471 -0
  49. package/dist/index.d.ts +855 -44
  50. package/dist/index.js +84 -257
  51. package/dist/kernel.d.ts +39 -2
  52. package/dist/kernel.js +21 -2
  53. package/dist/mistral-conversations-TY3MMD6G.js +718 -0
  54. package/dist/multipart-parser-F6M4NJZU.js +373 -0
  55. package/dist/openai-codex-responses-BRDUMLN5.js +1374 -0
  56. package/dist/openai-completions-CX634XN3.js +1139 -0
  57. package/dist/openai-responses-HMCXJIHT.js +300 -0
  58. package/dist/openrouter-images-KTWET2H2.js +148 -0
  59. package/dist/pi-messages-XL5OEC4K.js +307 -0
  60. package/dist/pi-worker.js +671 -0
  61. package/dist/runner-pi.d.ts +342 -2
  62. package/dist/runner-pi.js +46 -2
  63. package/dist/runner-registry-M776E3SE.js +22 -0
  64. package/dist/sqlite.d.ts +446 -2
  65. package/dist/sqlite.js +15 -2
  66. package/dist/src-F2KBSV4T.js +1398 -0
  67. package/dist/supervisor.d.ts +625 -2
  68. package/dist/supervisor.js +39 -2
  69. package/dist/testkit.d.ts +564 -2
  70. package/dist/testkit.js +127 -2
  71. package/dist/transcript.d.ts +84 -0
  72. package/dist/transcript.js +25 -0
  73. package/dist/verification-worker.js +72 -0
  74. package/package.json +3 -28
  75. package/dist/cli.d.ts.map +0 -1
  76. package/dist/cli.js.map +0 -1
  77. package/dist/console/assets/index-BJTGvlCH.js +0 -52
  78. package/dist/console/assets/index-BJTGvlCH.js.map +0 -1
  79. package/dist/console/assets/index-msBn41Cl.css +0 -1
  80. package/dist/control.d.ts +0 -106
  81. package/dist/control.d.ts.map +0 -1
  82. package/dist/control.js +0 -215
  83. package/dist/control.js.map +0 -1
  84. package/dist/execution.d.ts.map +0 -1
  85. package/dist/execution.js.map +0 -1
  86. package/dist/index.d.ts.map +0 -1
  87. package/dist/index.js.map +0 -1
  88. package/dist/inspect.d.ts +0 -61
  89. package/dist/inspect.d.ts.map +0 -1
  90. package/dist/inspect.js +0 -110
  91. package/dist/inspect.js.map +0 -1
  92. package/dist/kernel.d.ts.map +0 -1
  93. package/dist/kernel.js.map +0 -1
  94. package/dist/live-config.d.ts +0 -10
  95. package/dist/live-config.d.ts.map +0 -1
  96. package/dist/live-config.js +0 -44
  97. package/dist/live-config.js.map +0 -1
  98. package/dist/metrics.d.ts +0 -2
  99. package/dist/metrics.d.ts.map +0 -1
  100. package/dist/metrics.js +0 -2
  101. package/dist/metrics.js.map +0 -1
  102. package/dist/prompt-queue.d.ts +0 -13
  103. package/dist/prompt-queue.d.ts.map +0 -1
  104. package/dist/prompt-queue.js +0 -30
  105. package/dist/prompt-queue.js.map +0 -1
  106. package/dist/runner-pi.d.ts.map +0 -1
  107. package/dist/runner-pi.js.map +0 -1
  108. package/dist/runner-registry.d.ts +0 -8
  109. package/dist/runner-registry.d.ts.map +0 -1
  110. package/dist/runner-registry.js +0 -12
  111. package/dist/runner-registry.js.map +0 -1
  112. package/dist/searchable-select.d.ts +0 -24
  113. package/dist/searchable-select.d.ts.map +0 -1
  114. package/dist/searchable-select.js +0 -61
  115. package/dist/searchable-select.js.map +0 -1
  116. package/dist/secret-input.d.ts +0 -8
  117. package/dist/secret-input.d.ts.map +0 -1
  118. package/dist/secret-input.js +0 -14
  119. package/dist/secret-input.js.map +0 -1
  120. package/dist/session.d.ts +0 -2
  121. package/dist/session.d.ts.map +0 -1
  122. package/dist/session.js +0 -2
  123. package/dist/session.js.map +0 -1
  124. package/dist/setup-wizard.d.ts +0 -11
  125. package/dist/setup-wizard.d.ts.map +0 -1
  126. package/dist/setup-wizard.js +0 -130
  127. package/dist/setup-wizard.js.map +0 -1
  128. package/dist/sqlite.d.ts.map +0 -1
  129. package/dist/sqlite.js.map +0 -1
  130. package/dist/supervisor.d.ts.map +0 -1
  131. package/dist/supervisor.js.map +0 -1
  132. package/dist/testkit.d.ts.map +0 -1
  133. package/dist/testkit.js.map +0 -1
  134. package/dist/tui-theme.d.ts +0 -16
  135. package/dist/tui-theme.d.ts.map +0 -1
  136. package/dist/tui-theme.js +0 -18
  137. package/dist/tui-theme.js.map +0 -1
  138. package/dist/tui.d.ts +0 -18
  139. package/dist/tui.d.ts.map +0 -1
  140. package/dist/tui.js +0 -583
  141. package/dist/tui.js.map +0 -1
  142. package/dist/update.d.ts +0 -15
  143. package/dist/update.d.ts.map +0 -1
  144. package/dist/update.js +0 -59
  145. package/dist/update.js.map +0 -1
  146. package/dist/web-console.d.ts +0 -33
  147. package/dist/web-console.d.ts.map +0 -1
  148. package/dist/web-console.js +0 -252
  149. package/dist/web-console.js.map +0 -1
  150. package/dist/welcome.d.ts +0 -30
  151. package/dist/welcome.d.ts.map +0 -1
  152. package/dist/welcome.js +0 -81
  153. package/dist/welcome.js.map +0 -1
  154. package/node_modules/goah-ledger-contract/dist/execution.d.ts +0 -402
  155. package/node_modules/goah-ledger-contract/dist/execution.d.ts.map +0 -1
  156. package/node_modules/goah-ledger-contract/dist/execution.js +0 -40
  157. package/node_modules/goah-ledger-contract/dist/execution.js.map +0 -1
  158. package/node_modules/goah-ledger-contract/dist/index.d.ts +0 -5
  159. package/node_modules/goah-ledger-contract/dist/index.d.ts.map +0 -1
  160. package/node_modules/goah-ledger-contract/dist/index.js +0 -5
  161. package/node_modules/goah-ledger-contract/dist/index.js.map +0 -1
  162. package/node_modules/goah-ledger-contract/dist/kernel.d.ts +0 -37
  163. package/node_modules/goah-ledger-contract/dist/kernel.d.ts.map +0 -1
  164. package/node_modules/goah-ledger-contract/dist/kernel.js +0 -8
  165. package/node_modules/goah-ledger-contract/dist/kernel.js.map +0 -1
  166. package/node_modules/goah-ledger-contract/dist/metrics.d.ts +0 -40
  167. package/node_modules/goah-ledger-contract/dist/metrics.d.ts.map +0 -1
  168. package/node_modules/goah-ledger-contract/dist/metrics.js +0 -25
  169. package/node_modules/goah-ledger-contract/dist/metrics.js.map +0 -1
  170. package/node_modules/goah-ledger-contract/dist/session.d.ts +0 -63
  171. package/node_modules/goah-ledger-contract/dist/session.d.ts.map +0 -1
  172. package/node_modules/goah-ledger-contract/dist/session.js +0 -115
  173. package/node_modules/goah-ledger-contract/dist/session.js.map +0 -1
  174. package/node_modules/goah-ledger-contract/package.json +0 -29
  175. package/node_modules/goah-ledger-sqlite/dist/index.d.ts +0 -77
  176. package/node_modules/goah-ledger-sqlite/dist/index.d.ts.map +0 -1
  177. package/node_modules/goah-ledger-sqlite/dist/index.js +0 -884
  178. package/node_modules/goah-ledger-sqlite/dist/index.js.map +0 -1
  179. package/node_modules/goah-ledger-sqlite/package.json +0 -26
  180. package/node_modules/goah-runner-pi/dist/configurator.d.ts +0 -20
  181. package/node_modules/goah-runner-pi/dist/configurator.d.ts.map +0 -1
  182. package/node_modules/goah-runner-pi/dist/configurator.js +0 -267
  183. package/node_modules/goah-runner-pi/dist/configurator.js.map +0 -1
  184. package/node_modules/goah-runner-pi/dist/credential-store.d.ts +0 -12
  185. package/node_modules/goah-runner-pi/dist/credential-store.d.ts.map +0 -1
  186. package/node_modules/goah-runner-pi/dist/credential-store.js +0 -69
  187. package/node_modules/goah-runner-pi/dist/credential-store.js.map +0 -1
  188. package/node_modules/goah-runner-pi/dist/env-spec.d.ts +0 -17
  189. package/node_modules/goah-runner-pi/dist/env-spec.d.ts.map +0 -1
  190. package/node_modules/goah-runner-pi/dist/env-spec.js +0 -95
  191. package/node_modules/goah-runner-pi/dist/env-spec.js.map +0 -1
  192. package/node_modules/goah-runner-pi/dist/index.d.ts +0 -64
  193. package/node_modules/goah-runner-pi/dist/index.d.ts.map +0 -1
  194. package/node_modules/goah-runner-pi/dist/index.js +0 -241
  195. package/node_modules/goah-runner-pi/dist/index.js.map +0 -1
  196. package/node_modules/goah-runner-pi/dist/model-provider.d.ts +0 -30
  197. package/node_modules/goah-runner-pi/dist/model-provider.d.ts.map +0 -1
  198. package/node_modules/goah-runner-pi/dist/model-provider.js +0 -96
  199. package/node_modules/goah-runner-pi/dist/model-provider.js.map +0 -1
  200. package/node_modules/goah-runner-pi/dist/pi-worker.d.ts +0 -29
  201. package/node_modules/goah-runner-pi/dist/pi-worker.d.ts.map +0 -1
  202. package/node_modules/goah-runner-pi/dist/pi-worker.js +0 -428
  203. package/node_modules/goah-runner-pi/dist/pi-worker.js.map +0 -1
  204. package/node_modules/goah-runner-pi/dist/test-helpers.d.ts +0 -4
  205. package/node_modules/goah-runner-pi/dist/test-helpers.d.ts.map +0 -1
  206. package/node_modules/goah-runner-pi/dist/test-helpers.js +0 -11
  207. package/node_modules/goah-runner-pi/dist/test-helpers.js.map +0 -1
  208. package/node_modules/goah-runner-pi/dist/verification-worker.d.ts +0 -2
  209. package/node_modules/goah-runner-pi/dist/verification-worker.d.ts.map +0 -1
  210. package/node_modules/goah-runner-pi/dist/verification-worker.js +0 -48
  211. package/node_modules/goah-runner-pi/dist/verification-worker.js.map +0 -1
  212. package/node_modules/goah-runner-pi/package.json +0 -28
  213. package/node_modules/goah-supervisor/dist/context-view.d.ts +0 -31
  214. package/node_modules/goah-supervisor/dist/context-view.d.ts.map +0 -1
  215. package/node_modules/goah-supervisor/dist/context-view.js +0 -71
  216. package/node_modules/goah-supervisor/dist/context-view.js.map +0 -1
  217. package/node_modules/goah-supervisor/dist/index.d.ts +0 -94
  218. package/node_modules/goah-supervisor/dist/index.d.ts.map +0 -1
  219. package/node_modules/goah-supervisor/dist/index.js +0 -888
  220. package/node_modules/goah-supervisor/dist/index.js.map +0 -1
  221. package/node_modules/goah-supervisor/dist/roles.d.ts +0 -3
  222. package/node_modules/goah-supervisor/dist/roles.d.ts.map +0 -1
  223. package/node_modules/goah-supervisor/dist/roles.js +0 -14
  224. package/node_modules/goah-supervisor/dist/roles.js.map +0 -1
  225. package/node_modules/goah-supervisor/dist/verification.d.ts +0 -82
  226. package/node_modules/goah-supervisor/dist/verification.d.ts.map +0 -1
  227. package/node_modules/goah-supervisor/dist/verification.js +0 -110
  228. package/node_modules/goah-supervisor/dist/verification.js.map +0 -1
  229. package/node_modules/goah-supervisor/package.json +0 -26
  230. package/node_modules/goah-testkit/dist/faux-runner-worker.d.ts +0 -2
  231. package/node_modules/goah-testkit/dist/faux-runner-worker.d.ts.map +0 -1
  232. package/node_modules/goah-testkit/dist/faux-runner-worker.js +0 -75
  233. package/node_modules/goah-testkit/dist/faux-runner-worker.js.map +0 -1
  234. package/node_modules/goah-testkit/dist/index.d.ts +0 -52
  235. package/node_modules/goah-testkit/dist/index.d.ts.map +0 -1
  236. package/node_modules/goah-testkit/dist/index.js +0 -113
  237. package/node_modules/goah-testkit/dist/index.js.map +0 -1
  238. package/node_modules/goah-testkit/dist/mock-connector-worker.d.ts +0 -2
  239. package/node_modules/goah-testkit/dist/mock-connector-worker.d.ts.map +0 -1
  240. package/node_modules/goah-testkit/dist/mock-connector-worker.js +0 -29
  241. package/node_modules/goah-testkit/dist/mock-connector-worker.js.map +0 -1
  242. package/node_modules/goah-testkit/package.json +0 -29
@@ -0,0 +1,128 @@
1
+ import { createRequire as __goahCreateRequire } from "node:module";
2
+ const require = __goahCreateRequire(import.meta.url);
3
+ import {
4
+ runProcessWorker
5
+ } from "./chunk-X7UWW2PN.js";
6
+ import "./chunk-6M3OOCKO.js";
7
+ import "./chunk-K26BVR6O.js";
8
+ import "./chunk-IIJQ3DW4.js";
9
+ import "./chunk-2ZP2NBW3.js";
10
+ import "./chunk-MPXK7QBL.js";
11
+ import "./chunk-2QBQZGXF.js";
12
+ import "./chunk-4KA63VNZ.js";
13
+ import "./chunk-KAO7WIXG.js";
14
+ import "./chunk-R6I3VKME.js";
15
+ import "./chunk-TJON22J4.js";
16
+ import "./chunk-XPQKM3L7.js";
17
+
18
+ // node_modules/goah-testkit/dist/faux-runner-worker.js
19
+ import { mkdirSync, writeFileSync } from "node:fs";
20
+ import { dirname, join } from "node:path";
21
+ await runProcessWorker(async (request, emit, rpc) => {
22
+ if (process.env.GOAH_FAUX_CONTEXT_FILE)
23
+ writeFileSync(process.env.GOAH_FAUX_CONTEXT_FILE, JSON.stringify(request.context));
24
+ const byAgent = JSON.parse(process.env.GOAH_FAUX_STEPS_BY_AGENT ?? "{}");
25
+ const byTrigger = JSON.parse(process.env.GOAH_FAUX_STEPS_BY_TRIGGER ?? "{}");
26
+ const triggerSteps = Object.entries(byTrigger).find(([selector]) => {
27
+ const separator = selector.indexOf("|");
28
+ const agent = separator < 0 ? null : selector.slice(0, separator);
29
+ const prefix = separator < 0 ? selector : selector.slice(separator + 1);
30
+ return (!agent || agent === request.agent) && request.sourceWakeTriggers?.some((trigger) => trigger.triggerRef.startsWith(prefix));
31
+ })?.[1];
32
+ const steps = triggerSteps ?? byAgent[request.agent] ?? JSON.parse(process.env.GOAH_FAUX_STEPS ?? "[]");
33
+ let goalCommitment = request.turn?.goalCommitment;
34
+ let recordUpdated = false;
35
+ for (const step of steps) {
36
+ if (step.write) {
37
+ const path = join(process.cwd(), step.write.path);
38
+ mkdirSync(dirname(path), { recursive: true });
39
+ writeFileSync(path, step.write.content);
40
+ }
41
+ for (const trace of step.trace ?? [])
42
+ emit(trace);
43
+ if (step.rpc) {
44
+ const result = await rpc(step.rpc.method, resolveParams(step.rpc.params, request.context));
45
+ emit({ type: "runner.rpc.result", data: result });
46
+ if (step.rpc.method === "goal.create" || step.rpc.method === "goal.work")
47
+ goalCommitment = commitmentFrom(result) ?? goalCommitment;
48
+ if (step.rpc.method === "work_record.update")
49
+ recordUpdated = true;
50
+ }
51
+ if (step.crash)
52
+ throw new Error(step.crash);
53
+ if (step.delayMs)
54
+ await new Promise((resolve) => setTimeout(resolve, step.delayMs));
55
+ if (step.hang)
56
+ await new Promise(() => void 0);
57
+ if (step.response !== void 0)
58
+ return { outcome: "completed", response: { content: step.response } };
59
+ if (step.handoff) {
60
+ const draft = step.handoff;
61
+ if (goalCommitment && !recordUpdated) {
62
+ const current = await rpc("work_record.read", { goalId: goalCommitment.goalId });
63
+ const record = current && typeof current === "object" && !Array.isArray(current) ? current : {};
64
+ const evidence = sourceSeqs(request.context);
65
+ await rpc("work_record.update", { expectedRevision: Number(record.recordRevision ?? 0), content: handoffRecord(draft.handoff, request.execution.id), reason: "record faux Goal progress", evidence: evidence.length ? [Math.max(...evidence)] : [] });
66
+ }
67
+ const validation = await rpc("goal.handoff.validate", { handoff: draft.handoff, candidateMessage: draft.response.content });
68
+ emit({ type: "message.assistant.completed", data: { message: { id: `faux:${request.execution.id}:handoff:${validation.attemptId}`, role: "assistant", content: [{ type: "text", text: draft.response.content }], stopReason: "toolUse" }, commitState: "provisional" } });
69
+ if (!validation.accepted) {
70
+ emit({ type: "runner.handoff_rejected", data: { attemptId: validation.attemptId, issues: validation.issues } });
71
+ if (validation.fatal)
72
+ return { outcome: "abnormal", reason: validation.issues.map((issue) => issue.message).join("; ") };
73
+ continue;
74
+ }
75
+ const output = { validationAttemptId: validation.attemptId, validationToken: validation.token, handoff: draft.handoff };
76
+ return { outcome: "completed", response: draft.response, handoff: output };
77
+ }
78
+ }
79
+ return { outcome: "abnormal", reason: "faux worker stopped without handoff" };
80
+ });
81
+ function resolveParams(value, context) {
82
+ const latest = Math.max(0, ...sourceSeqs(context));
83
+ const visit = (item) => {
84
+ if (item === "$LATEST_SOURCE_SEQ")
85
+ return latest;
86
+ if (Array.isArray(item))
87
+ return item.map(visit);
88
+ if (item && typeof item === "object")
89
+ return Object.fromEntries(Object.entries(item).map(([key, nested]) => [key, visit(nested)]));
90
+ return item;
91
+ };
92
+ return visit(value);
93
+ }
94
+ function sourceSeqs(context) {
95
+ return context && typeof context === "object" && !Array.isArray(context) && Array.isArray(context.sourceSeqs) ? context.sourceSeqs.filter((item) => typeof item === "number") : [];
96
+ }
97
+ function commitmentFrom(value) {
98
+ if (!value || typeof value !== "object" || Array.isArray(value) || !value.goalCommitment || typeof value.goalCommitment !== "object" || Array.isArray(value.goalCommitment))
99
+ return void 0;
100
+ const binding = value.goalCommitment;
101
+ return typeof binding.goalId === "string" && typeof binding.goalRevision === "number" ? { goalId: binding.goalId, goalRevision: binding.goalRevision } : void 0;
102
+ }
103
+ function handoffRecord(handoff, turnId) {
104
+ return `# Current State
105
+
106
+ Goal outcome: ${handoff.outcome}.
107
+
108
+ # Observations
109
+
110
+ See Ledger evidence ${handoff.evidence.join(", ") || "none"}.
111
+
112
+ # Work Completed
113
+
114
+ Recorded Goal progress.
115
+
116
+ # Decisions
117
+
118
+ Recorded by the faux Goal runner in ${turnId}.
119
+
120
+ # Blockers
121
+
122
+ ${handoff.outcome === "blocked" ? "Blocked." : "None."}
123
+
124
+ # Next Steps
125
+
126
+ Continue from the current Goal state.
127
+ `;
128
+ }
@@ -0,0 +1,422 @@
1
+ import { createRequire as __goahCreateRequire } from "node:module";
2
+ const require = __goahCreateRequire(import.meta.url);
3
+ import {
4
+ GoogleGenAI,
5
+ convertMessages,
6
+ convertTools,
7
+ isThinkingPart,
8
+ mapStopReason,
9
+ resolveGoogleFunctionCallingMode,
10
+ retainThoughtSignature,
11
+ retryGoogleRequest,
12
+ supportsGoogleStrictToolSampling
13
+ } from "./chunk-ULY36ME4.js";
14
+ import {
15
+ formatProviderError,
16
+ normalizeProviderError
17
+ } from "./chunk-H462EFJP.js";
18
+ import {
19
+ buildBaseOptions
20
+ } from "./chunk-BXA47DTF.js";
21
+ import "./chunk-4M6TXTT5.js";
22
+ import {
23
+ sanitizeSurrogates
24
+ } from "./chunk-NRLGWZJC.js";
25
+ import {
26
+ providerHeadersToRecord
27
+ } from "./chunk-FSBUS2BI.js";
28
+ import {
29
+ calculateCost,
30
+ clampThinkingLevel
31
+ } from "./chunk-MPXK7QBL.js";
32
+ import {
33
+ AssistantMessageEventStream
34
+ } from "./chunk-2QBQZGXF.js";
35
+ import "./chunk-XPQKM3L7.js";
36
+
37
+ // ../../node_modules/@earendil-works/pi-ai/dist/api/google-generative-ai.js
38
+ var toolCallCounter = 0;
39
+ var stream = (model, context, options) => {
40
+ const stream2 = new AssistantMessageEventStream();
41
+ (async () => {
42
+ const output = {
43
+ role: "assistant",
44
+ content: [],
45
+ api: "google-generative-ai",
46
+ provider: model.provider,
47
+ model: model.id,
48
+ usage: {
49
+ input: 0,
50
+ output: 0,
51
+ cacheRead: 0,
52
+ cacheWrite: 0,
53
+ totalTokens: 0,
54
+ cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 }
55
+ },
56
+ stopReason: "pending",
57
+ timestamp: Date.now()
58
+ };
59
+ try {
60
+ if (options?.fetch && options.fetch !== globalThis.fetch) {
61
+ throw new Error("Custom fetch is not supported by the Google Generative AI adapter");
62
+ }
63
+ const apiKey = options?.apiKey;
64
+ if (!apiKey) {
65
+ throw new Error(`No API key for provider: ${model.provider}`);
66
+ }
67
+ const client = createClient(model, apiKey, options?.headers);
68
+ let params = buildParams(model, context, options);
69
+ const nextParams = await options?.onPayload?.(params, model);
70
+ if (nextParams !== void 0) {
71
+ params = nextParams;
72
+ }
73
+ const googleStream = await retryGoogleRequest(() => client.models.generateContentStream(params), options);
74
+ stream2.push({ type: "start", partial: output });
75
+ let currentBlock = null;
76
+ const blocks = output.content;
77
+ const blockIndex = () => blocks.length - 1;
78
+ for await (const chunk of googleStream) {
79
+ output.responseId ||= chunk.responseId;
80
+ const candidate = chunk.candidates?.[0];
81
+ if (candidate?.content?.parts) {
82
+ for (const part of candidate.content.parts) {
83
+ if (part.text !== void 0) {
84
+ const isThinking = isThinkingPart(part);
85
+ if (!currentBlock || isThinking && currentBlock.type !== "thinking" || !isThinking && currentBlock.type !== "text") {
86
+ if (currentBlock) {
87
+ if (currentBlock.type === "text") {
88
+ stream2.push({
89
+ type: "text_end",
90
+ contentIndex: blocks.length - 1,
91
+ content: currentBlock.text,
92
+ partial: output
93
+ });
94
+ } else {
95
+ stream2.push({
96
+ type: "thinking_end",
97
+ contentIndex: blockIndex(),
98
+ content: currentBlock.thinking,
99
+ partial: output
100
+ });
101
+ }
102
+ }
103
+ if (isThinking) {
104
+ currentBlock = { type: "thinking", thinking: "", thinkingSignature: void 0 };
105
+ output.content.push(currentBlock);
106
+ stream2.push({ type: "thinking_start", contentIndex: blockIndex(), partial: output });
107
+ } else {
108
+ currentBlock = { type: "text", text: "" };
109
+ output.content.push(currentBlock);
110
+ stream2.push({ type: "text_start", contentIndex: blockIndex(), partial: output });
111
+ }
112
+ }
113
+ if (currentBlock.type === "thinking") {
114
+ currentBlock.thinking += part.text;
115
+ currentBlock.thinkingSignature = retainThoughtSignature(currentBlock.thinkingSignature, part.thoughtSignature);
116
+ stream2.push({
117
+ type: "thinking_delta",
118
+ contentIndex: blockIndex(),
119
+ delta: part.text,
120
+ partial: output
121
+ });
122
+ } else {
123
+ currentBlock.text += part.text;
124
+ currentBlock.textSignature = retainThoughtSignature(currentBlock.textSignature, part.thoughtSignature);
125
+ stream2.push({
126
+ type: "text_delta",
127
+ contentIndex: blockIndex(),
128
+ delta: part.text,
129
+ partial: output
130
+ });
131
+ }
132
+ }
133
+ if (part.functionCall) {
134
+ if (currentBlock) {
135
+ if (currentBlock.type === "text") {
136
+ stream2.push({
137
+ type: "text_end",
138
+ contentIndex: blockIndex(),
139
+ content: currentBlock.text,
140
+ partial: output
141
+ });
142
+ } else {
143
+ stream2.push({
144
+ type: "thinking_end",
145
+ contentIndex: blockIndex(),
146
+ content: currentBlock.thinking,
147
+ partial: output
148
+ });
149
+ }
150
+ currentBlock = null;
151
+ }
152
+ const providedId = part.functionCall.id;
153
+ const needsNewId = !providedId || output.content.some((b) => b.type === "toolCall" && b.id === providedId);
154
+ const toolCallId = needsNewId ? `${part.functionCall.name}_${Date.now()}_${++toolCallCounter}` : providedId;
155
+ const toolCall = {
156
+ type: "toolCall",
157
+ id: toolCallId,
158
+ name: part.functionCall.name || "",
159
+ arguments: part.functionCall.args ?? {},
160
+ ...part.thoughtSignature && { thoughtSignature: part.thoughtSignature }
161
+ };
162
+ output.content.push(toolCall);
163
+ stream2.push({ type: "toolcall_start", contentIndex: blockIndex(), partial: output });
164
+ stream2.push({
165
+ type: "toolcall_delta",
166
+ contentIndex: blockIndex(),
167
+ delta: JSON.stringify(toolCall.arguments),
168
+ partial: output
169
+ });
170
+ stream2.push({ type: "toolcall_end", contentIndex: blockIndex(), toolCall, partial: output });
171
+ }
172
+ }
173
+ }
174
+ if (candidate?.finishReason) {
175
+ output.rawStopReason = candidate.finishReason;
176
+ output.stopReason = mapStopReason(candidate.finishReason);
177
+ if (output.content.some((b) => b.type === "toolCall") && output.stopReason === "stop") {
178
+ output.stopReason = "toolUse";
179
+ }
180
+ }
181
+ if (chunk.usageMetadata) {
182
+ output.usage = {
183
+ input: (chunk.usageMetadata.promptTokenCount || 0) - (chunk.usageMetadata.cachedContentTokenCount || 0),
184
+ output: (chunk.usageMetadata.candidatesTokenCount || 0) + (chunk.usageMetadata.thoughtsTokenCount || 0),
185
+ cacheRead: chunk.usageMetadata.cachedContentTokenCount || 0,
186
+ cacheWrite: 0,
187
+ reasoning: chunk.usageMetadata.thoughtsTokenCount || 0,
188
+ totalTokens: chunk.usageMetadata.totalTokenCount || 0,
189
+ cost: {
190
+ input: 0,
191
+ output: 0,
192
+ cacheRead: 0,
193
+ cacheWrite: 0,
194
+ total: 0
195
+ }
196
+ };
197
+ calculateCost(model, output.usage);
198
+ }
199
+ }
200
+ if (currentBlock) {
201
+ if (currentBlock.type === "text") {
202
+ stream2.push({
203
+ type: "text_end",
204
+ contentIndex: blockIndex(),
205
+ content: currentBlock.text,
206
+ partial: output
207
+ });
208
+ } else {
209
+ stream2.push({
210
+ type: "thinking_end",
211
+ contentIndex: blockIndex(),
212
+ content: currentBlock.thinking,
213
+ partial: output
214
+ });
215
+ }
216
+ }
217
+ if (options?.signal?.aborted) {
218
+ throw new Error("Request was aborted");
219
+ }
220
+ if (output.stopReason === "pending") {
221
+ throw new Error("Google stream ended without a finish reason");
222
+ }
223
+ if (output.stopReason === "aborted" || output.stopReason === "error") {
224
+ const errorMessage = output.rawStopReason ? `Provider stopped with: ${output.rawStopReason}` : "An unknown error occurred";
225
+ throw new Error(errorMessage);
226
+ }
227
+ stream2.push({ type: "done", reason: output.stopReason, message: output });
228
+ stream2.end();
229
+ } catch (error) {
230
+ for (const block of output.content) {
231
+ if ("index" in block) {
232
+ delete block.index;
233
+ }
234
+ }
235
+ output.stopReason = options?.signal?.aborted ? "aborted" : "error";
236
+ output.errorMessage = formatProviderError(normalizeProviderError(error));
237
+ stream2.push({ type: "error", reason: output.stopReason, error: output });
238
+ stream2.end();
239
+ }
240
+ })();
241
+ return stream2;
242
+ };
243
+ var streamSimple = (model, context, options) => {
244
+ const apiKey = options?.apiKey;
245
+ if (!apiKey) {
246
+ throw new Error(`No API key for provider: ${model.provider}`);
247
+ }
248
+ const base = buildBaseOptions(model, context, options, apiKey);
249
+ if (!options?.reasoning) {
250
+ return stream(model, context, { ...base, thinking: { enabled: false } });
251
+ }
252
+ const clampedReasoning = clampThinkingLevel(model, options.reasoning);
253
+ const effort = clampedReasoning === "off" ? "high" : clampedReasoning;
254
+ const googleModel = model;
255
+ if (isGemini3ProModel(googleModel) || isGemini3FlashModel(googleModel) || isGemma4Model(googleModel)) {
256
+ return stream(model, context, {
257
+ ...base,
258
+ thinking: {
259
+ enabled: true,
260
+ level: getThinkingLevel(effort, googleModel)
261
+ }
262
+ });
263
+ }
264
+ return stream(model, context, {
265
+ ...base,
266
+ thinking: {
267
+ enabled: true,
268
+ budgetTokens: getGoogleBudget(googleModel, effort, options.thinkingBudgets)
269
+ }
270
+ });
271
+ };
272
+ function createClient(model, apiKey, optionsHeaders) {
273
+ const httpOptions = {};
274
+ if (model.baseUrl) {
275
+ httpOptions.baseUrl = model.baseUrl;
276
+ httpOptions.apiVersion = "";
277
+ }
278
+ const headers = providerHeadersToRecord({ ...model.headers, ...optionsHeaders });
279
+ if (headers) {
280
+ httpOptions.headers = headers;
281
+ }
282
+ return new GoogleGenAI({
283
+ apiKey,
284
+ httpOptions: Object.keys(httpOptions).length > 0 ? httpOptions : void 0
285
+ });
286
+ }
287
+ function buildParams(model, context, options = {}) {
288
+ const contents = convertMessages(model, context);
289
+ const generationConfig = {};
290
+ if (options.temperature !== void 0) {
291
+ generationConfig.temperature = options.temperature;
292
+ }
293
+ if (options.maxTokens !== void 0) {
294
+ generationConfig.maxOutputTokens = options.maxTokens;
295
+ }
296
+ const supportsStrictMode = supportsGoogleStrictToolSampling(model.id);
297
+ const functionCallingMode = context.tools?.length ? resolveGoogleFunctionCallingMode(context.tools, options.toolChoice, supportsStrictMode) : void 0;
298
+ const config = {
299
+ ...Object.keys(generationConfig).length > 0 && generationConfig,
300
+ ...context.systemPrompt && { systemInstruction: sanitizeSurrogates(context.systemPrompt) },
301
+ ...context.tools && context.tools.length > 0 && {
302
+ tools: convertTools(context.tools, false, supportsStrictMode)
303
+ },
304
+ ...functionCallingMode !== void 0 && {
305
+ toolConfig: { functionCallingConfig: { mode: functionCallingMode } }
306
+ }
307
+ };
308
+ if (options.thinking?.enabled && model.reasoning) {
309
+ const thinkingConfig = { includeThoughts: true };
310
+ if (options.thinking.level !== void 0) {
311
+ thinkingConfig.thinkingLevel = options.thinking.level;
312
+ } else if (options.thinking.budgetTokens !== void 0) {
313
+ thinkingConfig.thinkingBudget = options.thinking.budgetTokens;
314
+ }
315
+ config.thinkingConfig = thinkingConfig;
316
+ } else if (model.reasoning && options.thinking && !options.thinking.enabled) {
317
+ config.thinkingConfig = getDisabledThinkingConfig(model);
318
+ }
319
+ if (options.signal) {
320
+ if (options.signal.aborted) {
321
+ throw new Error("Request aborted");
322
+ }
323
+ config.abortSignal = options.signal;
324
+ }
325
+ const params = {
326
+ model: model.id,
327
+ contents,
328
+ config
329
+ };
330
+ return params;
331
+ }
332
+ function isGemma4Model(model) {
333
+ return /gemma-?4/.test(model.id.toLowerCase());
334
+ }
335
+ function isGemini3ProModel(model) {
336
+ return /gemini-3(?:\.\d+)?-pro/.test(model.id.toLowerCase());
337
+ }
338
+ function isGemini3FlashModel(model) {
339
+ const id = model.id.toLowerCase();
340
+ return /gemini-3(?:\.\d+)?-flash/.test(id) || id === "gemini-flash-latest" || id === "gemini-flash-lite-latest";
341
+ }
342
+ function getDisabledThinkingConfig(model) {
343
+ if (isGemini3ProModel(model)) {
344
+ return { thinkingLevel: "LOW" };
345
+ }
346
+ if (isGemini3FlashModel(model)) {
347
+ return { thinkingLevel: "MINIMAL" };
348
+ }
349
+ if (isGemma4Model(model)) {
350
+ return { thinkingLevel: "MINIMAL" };
351
+ }
352
+ return { thinkingBudget: 0 };
353
+ }
354
+ function getThinkingLevel(effort, model) {
355
+ if (isGemini3ProModel(model)) {
356
+ switch (effort) {
357
+ case "minimal":
358
+ case "low":
359
+ return "LOW";
360
+ case "medium":
361
+ case "high":
362
+ return "HIGH";
363
+ }
364
+ }
365
+ if (isGemma4Model(model)) {
366
+ switch (effort) {
367
+ case "minimal":
368
+ case "low":
369
+ return "MINIMAL";
370
+ case "medium":
371
+ case "high":
372
+ return "HIGH";
373
+ }
374
+ }
375
+ switch (effort) {
376
+ case "minimal":
377
+ return "MINIMAL";
378
+ case "low":
379
+ return "LOW";
380
+ case "medium":
381
+ return "MEDIUM";
382
+ case "high":
383
+ return "HIGH";
384
+ }
385
+ }
386
+ function getGoogleBudget(model, effort, customBudgets) {
387
+ if (customBudgets?.[effort] !== void 0) {
388
+ return customBudgets[effort];
389
+ }
390
+ if (model.id.includes("2.5-pro")) {
391
+ const budgets = {
392
+ minimal: 128,
393
+ low: 2048,
394
+ medium: 8192,
395
+ high: 32768
396
+ };
397
+ return budgets[effort];
398
+ }
399
+ if (model.id.includes("2.5-flash-lite")) {
400
+ const budgets = {
401
+ minimal: 512,
402
+ low: 2048,
403
+ medium: 8192,
404
+ high: 24576
405
+ };
406
+ return budgets[effort];
407
+ }
408
+ if (model.id.includes("2.5-flash")) {
409
+ const budgets = {
410
+ minimal: 128,
411
+ low: 2048,
412
+ medium: 8192,
413
+ high: 24576
414
+ };
415
+ return budgets[effort];
416
+ }
417
+ return -1;
418
+ }
419
+ export {
420
+ stream,
421
+ streamSimple
422
+ };