melchizedek-agents 0.18.0 → 0.20.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (293) hide show
  1. package/.env.example +12 -0
  2. package/CHANGELOG.md +1001 -0
  3. package/DOCUMENTATION.md +252 -65
  4. package/QUICKSTART.md +9 -6
  5. package/README.md +42 -4
  6. package/config/agents/examples/harness.yaml +3 -1
  7. package/config/agents/examples/model_zoo.yaml +10 -9
  8. package/config/agents/syndicateSchema.yaml +53 -16
  9. package/config/agents/templates/account_memory.yaml +2 -6
  10. package/config/agents/templates/case_desk.yaml +3 -9
  11. package/config/agents/templates/draft_review.yaml +3 -9
  12. package/config/agents/templates/intake_extractor.yaml +1 -3
  13. package/config/agents/templates/research_brief.yaml +3 -9
  14. package/config/agents/templates/research_desk.yaml +3 -9
  15. package/config/agents/templates/review_panel.yaml +4 -12
  16. package/config/agents/templates/support_triage.yaml +4 -12
  17. package/config/agents/templates/systems_operator.yaml +2 -6
  18. package/db/hardening.sql +7 -1
  19. package/db/migrations/0013_tool_credentials.sql +260 -0
  20. package/dist/lib/a2a/app.d.ts +70 -6
  21. package/dist/lib/a2a/app.js +107 -2
  22. package/dist/lib/a2a/executor.d.ts +20 -3
  23. package/dist/lib/a2a/executor.js +66 -7
  24. package/dist/lib/a2a/remoteAgent.d.ts +24 -9
  25. package/dist/lib/a2a/remoteAgent.js +31 -14
  26. package/dist/lib/adkPeer.d.ts +63 -0
  27. package/dist/lib/adkPeer.js +240 -0
  28. package/dist/lib/compile.d.ts +159 -30
  29. package/dist/lib/compile.js +215 -134
  30. package/dist/lib/compileAdk.d.ts +32 -0
  31. package/dist/lib/compileAdk.js +142 -0
  32. package/dist/lib/compileNative.d.ts +111 -0
  33. package/dist/lib/compileNative.js +278 -0
  34. package/dist/lib/config.d.ts +6 -5
  35. package/dist/lib/config.js +6 -5
  36. package/dist/lib/doctor.d.ts +28 -1
  37. package/dist/lib/doctor.js +64 -2
  38. package/dist/lib/index.d.ts +6 -1
  39. package/dist/lib/index.js +7 -1
  40. package/dist/lib/loadSyndicate.d.ts +18 -0
  41. package/dist/lib/memory/erase.d.ts +5 -3
  42. package/dist/lib/memory/erase.js +5 -2
  43. package/dist/lib/memory/namespace.d.ts +8 -1
  44. package/dist/lib/memory/namespace.js +12 -6
  45. package/dist/lib/memory/providers.js +2 -1
  46. package/dist/lib/memory/supabaseMemoryService.d.ts +37 -3
  47. package/dist/lib/memory/supabaseMemoryService.js +42 -2
  48. package/dist/lib/model.d.ts +47 -0
  49. package/dist/lib/model.js +37 -0
  50. package/dist/lib/models/adapterResolver.d.ts +115 -0
  51. package/dist/lib/models/adapterResolver.js +137 -0
  52. package/dist/lib/models/adkGeminiAdapter.d.ts +90 -0
  53. package/dist/lib/models/adkGeminiAdapter.js +369 -0
  54. package/dist/lib/models/adkShim.d.ts +111 -0
  55. package/dist/lib/models/adkShim.js +136 -0
  56. package/dist/lib/models/capabilities.d.ts +3 -0
  57. package/dist/lib/models/capabilities.js +33 -23
  58. package/dist/lib/models/chatCompletionsAdapter.d.ts +279 -0
  59. package/dist/lib/models/chatCompletionsAdapter.js +854 -0
  60. package/dist/lib/models/circuitBreaker.d.ts +42 -0
  61. package/dist/lib/models/circuitBreaker.js +70 -0
  62. package/dist/lib/models/claudeAdapter.d.ts +103 -0
  63. package/dist/lib/models/claudeAdapter.js +669 -0
  64. package/dist/lib/models/claudeLlm.d.ts +39 -62
  65. package/dist/lib/models/claudeLlm.js +45 -350
  66. package/dist/lib/models/claudeModels.d.ts +127 -0
  67. package/dist/lib/models/claudeModels.js +237 -0
  68. package/dist/lib/models/contract.d.ts +395 -0
  69. package/dist/lib/models/contract.js +39 -0
  70. package/dist/lib/models/endpoints.js +2 -1
  71. package/dist/lib/models/errorResponse.d.ts +50 -0
  72. package/dist/lib/models/errorResponse.js +48 -0
  73. package/dist/lib/models/fallback.d.ts +18 -11
  74. package/dist/lib/models/fallback.js +54 -43
  75. package/dist/lib/models/fallbackAdapter.d.ts +55 -0
  76. package/dist/lib/models/fallbackAdapter.js +62 -0
  77. package/dist/lib/models/gateway.js +2 -1
  78. package/dist/lib/models/gatewayAdapter.d.ts +51 -0
  79. package/dist/lib/models/gatewayAdapter.js +81 -0
  80. package/dist/lib/models/gatewayLlm.d.ts +12 -33
  81. package/dist/lib/models/gatewayLlm.js +16 -77
  82. package/dist/lib/models/geminiAdapter.d.ts +125 -0
  83. package/dist/lib/models/geminiAdapter.js +854 -0
  84. package/dist/lib/models/geminiState.d.ts +46 -0
  85. package/dist/lib/models/geminiState.js +51 -0
  86. package/dist/lib/models/genaiMapping.d.ts +283 -0
  87. package/dist/lib/models/genaiMapping.js +965 -0
  88. package/dist/lib/models/gptAdapter.d.ts +240 -0
  89. package/dist/lib/models/gptAdapter.js +804 -0
  90. package/dist/lib/models/gptLlm.d.ts +63 -123
  91. package/dist/lib/models/gptLlm.js +89 -603
  92. package/dist/lib/models/grokAdapter.d.ts +86 -0
  93. package/dist/lib/models/grokAdapter.js +142 -0
  94. package/dist/lib/models/grokLlm.d.ts +22 -45
  95. package/dist/lib/models/grokLlm.js +24 -69
  96. package/dist/lib/models/kimiAdapter.d.ts +157 -0
  97. package/dist/lib/models/kimiAdapter.js +203 -0
  98. package/dist/lib/models/kimiLlm.d.ts +15 -107
  99. package/dist/lib/models/kimiLlm.js +19 -155
  100. package/dist/lib/models/ollamaAdapter.d.ts +125 -0
  101. package/dist/lib/models/ollamaAdapter.js +155 -0
  102. package/dist/lib/models/ollamaLlm.d.ts +11 -107
  103. package/dist/lib/models/ollamaLlm.js +15 -153
  104. package/dist/lib/models/openAiCompatibleLlm.d.ts +73 -216
  105. package/dist/lib/models/openAiCompatibleLlm.js +109 -671
  106. package/dist/lib/models/providerState.d.ts +63 -0
  107. package/dist/lib/models/providerState.js +61 -0
  108. package/dist/lib/models/reasoning.d.ts +26 -0
  109. package/dist/lib/models/reasoning.js +59 -0
  110. package/dist/lib/models/registry.d.ts +62 -35
  111. package/dist/lib/models/registry.js +100 -124
  112. package/dist/lib/models/retry.d.ts +2 -1
  113. package/dist/lib/models/retry.js +3 -2
  114. package/dist/lib/models/schemaNormalize.d.ts +98 -0
  115. package/dist/lib/models/schemaNormalize.js +321 -1
  116. package/dist/lib/models/tracedGemini.d.ts +41 -0
  117. package/dist/lib/models/tracedGemini.js +98 -0
  118. package/dist/lib/models/urls.d.ts +11 -0
  119. package/dist/lib/models/urls.js +16 -0
  120. package/dist/lib/net/redirects.d.ts +21 -2
  121. package/dist/lib/net/redirects.js +11 -34
  122. package/dist/lib/observability/audit.d.ts +5 -1
  123. package/dist/lib/observability/lineage.d.ts +14 -0
  124. package/dist/lib/observability/lineage.js +33 -0
  125. package/dist/lib/observability/supabaseSpanExporter.d.ts +10 -4
  126. package/dist/lib/observability/supabaseSpanExporter.js +17 -9
  127. package/dist/lib/observability/tracer.d.ts +26 -8
  128. package/dist/lib/observability/tracer.js +43 -18
  129. package/dist/lib/runtime/adkMemoryBridge.d.ts +46 -0
  130. package/dist/lib/runtime/adkMemoryBridge.js +68 -0
  131. package/dist/lib/runtime/adkSessionBridge.d.ts +78 -0
  132. package/dist/lib/runtime/adkSessionBridge.js +176 -0
  133. package/dist/lib/runtime/approvals.d.ts +5 -0
  134. package/dist/lib/runtime/approvals.js +39 -8
  135. package/dist/lib/runtime/credentials.d.ts +61 -0
  136. package/dist/lib/runtime/credentials.js +67 -0
  137. package/dist/lib/runtime/events.d.ts +229 -0
  138. package/dist/lib/runtime/events.js +274 -0
  139. package/dist/lib/runtime/logging.d.ts +32 -0
  140. package/dist/lib/runtime/logging.js +55 -0
  141. package/dist/lib/runtime/memoryService.d.ts +72 -0
  142. package/dist/lib/runtime/memoryService.js +25 -0
  143. package/dist/lib/runtime/native/agentLoop.d.ts +209 -0
  144. package/dist/lib/runtime/native/agentLoop.js +705 -0
  145. package/dist/lib/runtime/native/compaction.d.ts +103 -0
  146. package/dist/lib/runtime/native/compaction.js +191 -0
  147. package/dist/lib/runtime/native/delegate.d.ts +151 -0
  148. package/dist/lib/runtime/native/delegate.js +256 -0
  149. package/dist/lib/runtime/native/history.d.ts +83 -0
  150. package/dist/lib/runtime/native/history.js +312 -0
  151. package/dist/lib/runtime/native/interrupts.d.ts +129 -0
  152. package/dist/lib/runtime/native/interrupts.js +310 -0
  153. package/dist/lib/runtime/native/request.d.ts +215 -0
  154. package/dist/lib/runtime/native/request.js +585 -0
  155. package/dist/lib/runtime/native/selfCorrection.d.ts +164 -0
  156. package/dist/lib/runtime/native/selfCorrection.js +327 -0
  157. package/dist/lib/runtime/native/step.d.ts +156 -0
  158. package/dist/lib/runtime/native/step.js +263 -0
  159. package/dist/lib/runtime/native/taskMode.d.ts +64 -0
  160. package/dist/lib/runtime/native/taskMode.js +126 -0
  161. package/dist/lib/runtime/native/telemetry.d.ts +75 -0
  162. package/dist/lib/runtime/native/telemetry.js +299 -0
  163. package/dist/lib/runtime/native/tempState.d.ts +31 -0
  164. package/dist/lib/runtime/native/tempState.js +56 -0
  165. package/dist/lib/runtime/nativeTurn.d.ts +86 -0
  166. package/dist/lib/runtime/nativeTurn.js +107 -0
  167. package/dist/lib/runtime/questions.d.ts +29 -7
  168. package/dist/lib/runtime/questions.js +47 -32
  169. package/dist/lib/runtime/runtimeFlag.d.ts +38 -0
  170. package/dist/lib/runtime/runtimeFlag.js +58 -0
  171. package/dist/lib/runtime/sessions.d.ts +166 -0
  172. package/dist/lib/runtime/sessions.js +201 -0
  173. package/dist/lib/runtime/syndicateTurn.d.ts +46 -5
  174. package/dist/lib/runtime/syndicateTurn.js +233 -36
  175. package/dist/lib/runtime/valueDepth.d.ts +23 -0
  176. package/dist/lib/runtime/valueDepth.js +41 -0
  177. package/dist/lib/session/supabaseSessionService.d.ts +65 -18
  178. package/dist/lib/session/supabaseSessionService.js +148 -105
  179. package/dist/lib/session/transcript.d.ts +69 -23
  180. package/dist/lib/session/transcript.js +105 -29
  181. package/dist/lib/skills.js +33 -7
  182. package/dist/lib/storage/postgres/credentialStore.d.ts +20 -0
  183. package/dist/lib/storage/postgres/credentialStore.js +82 -0
  184. package/dist/lib/storage/postgres/index.d.ts +12 -0
  185. package/dist/lib/storage/postgres/index.js +6 -1
  186. package/dist/lib/storage/postgres/sessionService.d.ts +44 -16
  187. package/dist/lib/storage/postgres/sessionService.js +128 -66
  188. package/dist/lib/syndicateSchema.d.ts +49 -0
  189. package/dist/lib/syndicateSchema.js +99 -9
  190. package/dist/lib/toolRegistry.d.ts +26 -2
  191. package/dist/lib/toolRegistry.js +73 -17
  192. package/dist/lib/tools/adkTool.d.ts +81 -0
  193. package/dist/lib/tools/adkTool.js +268 -0
  194. package/dist/lib/tools/auth.d.ts +118 -0
  195. package/dist/lib/tools/auth.js +89 -0
  196. package/dist/lib/tools/collectionsSearchTool.d.ts +8 -7
  197. package/dist/lib/tools/collectionsSearchTool.js +12 -27
  198. package/dist/lib/tools/credentialCipher.d.ts +58 -0
  199. package/dist/lib/tools/credentialCipher.js +116 -0
  200. package/dist/lib/tools/credentialStore.d.ts +77 -0
  201. package/dist/lib/tools/credentialStore.js +246 -0
  202. package/dist/lib/tools/examples.d.ts +43 -0
  203. package/dist/lib/tools/examples.js +62 -0
  204. package/dist/lib/tools/generateImageTool.d.ts +15 -6
  205. package/dist/lib/tools/generateImageTool.js +19 -29
  206. package/dist/lib/tools/inspectImageTool.d.ts +9 -6
  207. package/dist/lib/tools/inspectImageTool.js +13 -17
  208. package/dist/lib/tools/mcpToolFactory.d.ts +42 -1
  209. package/dist/lib/tools/mcpToolFactory.js +46 -47
  210. package/dist/lib/tools/memoryTools.d.ts +61 -0
  211. package/dist/lib/tools/memoryTools.js +119 -0
  212. package/dist/lib/tools/nativeTools.d.ts +38 -0
  213. package/dist/lib/tools/nativeTools.js +43 -0
  214. package/dist/lib/tools/oauthConsent.d.ts +181 -0
  215. package/dist/lib/tools/oauthConsent.js +330 -0
  216. package/dist/lib/tools/openapi/call.d.ts +108 -0
  217. package/dist/lib/tools/openapi/call.js +295 -0
  218. package/dist/lib/tools/openapi/parse.d.ts +147 -0
  219. package/dist/lib/tools/openapi/parse.js +570 -0
  220. package/dist/lib/tools/openapiTools.d.ts +25 -17
  221. package/dist/lib/tools/openapiTools.js +124 -121
  222. package/dist/lib/tools/scienceTools.d.ts +14 -14
  223. package/dist/lib/tools/skillToolset.d.ts +58 -87
  224. package/dist/lib/tools/skillToolset.js +61 -142
  225. package/dist/lib/tools/skills/env.d.ts +58 -0
  226. package/dist/lib/tools/skills/env.js +165 -0
  227. package/dist/lib/tools/skills/executor.d.ts +114 -0
  228. package/dist/lib/tools/skills/executor.js +331 -0
  229. package/dist/lib/tools/skills/frontmatter.d.ts +79 -0
  230. package/dist/lib/tools/skills/frontmatter.js +215 -0
  231. package/dist/lib/tools/skills/loader.d.ts +91 -0
  232. package/dist/lib/tools/skills/loader.js +257 -0
  233. package/dist/lib/tools/skills/tools.d.ts +187 -0
  234. package/dist/lib/tools/skills/tools.js +386 -0
  235. package/dist/lib/tools/taskTools.d.ts +15 -15
  236. package/dist/lib/tools/tool.d.ts +354 -0
  237. package/dist/lib/tools/tool.js +304 -0
  238. package/dist/lib/tools/toolContract.d.ts +89 -33
  239. package/dist/lib/tools/toolContract.js +104 -82
  240. package/dist/lib/tools/urlContextTool.d.ts +4 -1
  241. package/dist/lib/tools/urlContextTool.js +6 -2
  242. package/dist/lib/tools/webExtractTool.d.ts +3 -3
  243. package/dist/lib/tools/webExtractTool.js +2 -1
  244. package/dist/lib/tools/webSearchTool.d.ts +7 -10
  245. package/dist/lib/tools/webSearchTool.js +15 -60
  246. package/dist/lib/tools/wikiTools.d.ts +19 -19
  247. package/dist/lib/tools/wikiTools.js +13 -9
  248. package/dist/lib/tools/xApiSearchTool.d.ts +3 -3
  249. package/dist/lib/tools/xApiSearchTool.js +2 -1
  250. package/dist/lib/tools/xSearchTool.d.ts +8 -10
  251. package/dist/lib/tools/xSearchTool.js +12 -63
  252. package/dist/lib/tools/xaiSearchParams.d.ts +41 -0
  253. package/dist/lib/tools/xaiSearchParams.js +126 -0
  254. package/dist/lib/wiki/agentRun.d.ts +12 -0
  255. package/dist/lib/wiki/agentRun.js +74 -35
  256. package/dist/lib/wiki/entities.d.ts +12 -0
  257. package/dist/lib/wiki/entities.js +45 -3
  258. package/dist/lib/workflow/agentNode.d.ts +153 -0
  259. package/dist/lib/workflow/agentNode.js +365 -0
  260. package/dist/lib/workflow/graph.d.ts +178 -0
  261. package/dist/lib/workflow/graph.js +468 -0
  262. package/dist/lib/workflow/nodeEvents.d.ts +50 -0
  263. package/dist/lib/workflow/nodeEvents.js +101 -0
  264. package/dist/lib/workflow/pause.d.ts +122 -0
  265. package/dist/lib/workflow/pause.js +180 -0
  266. package/dist/lib/workflow/resume.d.ts +166 -0
  267. package/dist/lib/workflow/resume.js +441 -0
  268. package/dist/lib/workflow/route.d.ts +58 -0
  269. package/dist/lib/workflow/route.js +69 -0
  270. package/dist/lib/workflow/scheduler.d.ts +418 -0
  271. package/dist/lib/workflow/scheduler.js +756 -0
  272. package/dist/lib/workflow/toolNode.d.ts +127 -0
  273. package/dist/lib/workflow/toolNode.js +236 -0
  274. package/dist/lib/workflow/turn.d.ts +118 -0
  275. package/dist/lib/workflow/turn.js +216 -0
  276. package/dist/lib/workflow.d.ts +33 -11
  277. package/dist/lib/workflow.js +49 -27
  278. package/dist/lib/workflowConfig.d.ts +7 -2
  279. package/dist/lib/workflowConfig.js +7 -12
  280. package/dist/scripts/a2a_server.js +4 -3
  281. package/dist/scripts/assistant_worker.d.ts +2 -1
  282. package/dist/scripts/assistant_worker.js +9 -4
  283. package/dist/scripts/doctor.d.ts +3 -1
  284. package/dist/scripts/doctor.js +4 -2
  285. package/dist/scripts/syndicate_chat.js +19 -10
  286. package/package.json +9 -3
  287. package/skills/briefs/melchizedek-author.md +3 -2
  288. package/skills/briefs/melchizedek-models.md +13 -9
  289. package/skills/briefs/melchizedek.md +3 -3
  290. package/skills/melchizedek/SKILL.md +2 -1
  291. package/skills/melchizedek-author/SKILL.md +2 -1
  292. package/skills/melchizedek-author/assets/minimal.yaml +2 -0
  293. package/skills/melchizedek-models/SKILL.md +23 -6
@@ -0,0 +1,25 @@
1
+ /**
2
+ * lib/runtime/memoryService.ts — the engine's own long-term memory interface
3
+ * (ADR 0045, ADR 0052).
4
+ *
5
+ * WHY this file exists:
6
+ * The native runtime asks long-term memory for what it needs through this
7
+ * interface, never through ADK's BaseMemoryService. The engine's service
8
+ * (lib/memory/supabaseMemoryService.ts) implements it, and also ADK's
9
+ * interface, whose methods hand their arguments to this one's, so the ADK
10
+ * runtime reaches the same logic (ADR 0059). The shapes include the
11
+ * extras: per-syndicate extraction rules and model, erasure, retention and
12
+ * the boot-time dimension check, which the A2A server reaches by name
13
+ * (lib/a2a/app.ts). The memory tools reach `search` through the tool
14
+ * context's `searchMemory` (lib/tools/tool.ts, lib/tools/memoryTools.ts).
15
+ *
16
+ * SILOS: every fact is filed under `<appName>/<userId>`, and a search reads
17
+ * that key alone. `appName` is the memory namespace, not the agent that
18
+ * asks: the runtime pins it to the root syndicate's `memory_namespace`
19
+ * (lib/memory/namespace.ts, ADR 0020), so a subagent finds what its
20
+ * orchestrator's conversations stored.
21
+ *
22
+ * Types only, and every import is a type: nothing in this module's import
23
+ * graph names @google/* (tests/events.test.ts asserts it).
24
+ */
25
+ export {};
@@ -0,0 +1,209 @@
1
+ /**
2
+ * lib/runtime/native/agentLoop.ts — the native loop for one agent: a model
3
+ * step, the step's tool calls, their results fed back, until the model
4
+ * stops calling tools or the turn stops it (ADR 0045, ADR 0066).
5
+ *
6
+ * WHY this file exists:
7
+ * On the ADK runtime this is LlmAgent.runAsyncImpl: run one step
8
+ * (lib/runtime/native/step.ts is the native step), run the answer's
9
+ * function calls (ADK's handleFunctionCallList), store the response
10
+ * event, and step again unless the last event is a final response. A
11
+ * session either runtime wrote must be one the other continues, so every
12
+ * event this loop stores is the one ADK stores for the same answer and the
13
+ * same tool results, ids and times aside. tests/nativeLoop.test.ts runs
14
+ * the boundary suite's single-agent cases both ways and compares.
15
+ *
16
+ * ONE STEP, AS ADK RUNS IT:
17
+ * 1. The model step (runModelStep). Its partial events are yielded as they
18
+ * arrive and stored nowhere, so drainAgentStream hands their text to
19
+ * onTextDelta exactly as it does on the ADK path. With a
20
+ * `fallback_model`, the step runs once per leaf adapter (ADR 0053): a
21
+ * provider-side failure before anything was produced is not stored,
22
+ * and the fallback answers the same request under its own model id, by
23
+ * ADK's FallbackLlm's rules and the shared circuit breaker (ADR 0044).
24
+ * 2. The answer's calls run in parallel, each with its own context (its
25
+ * own state delta and actions). Results are kept in call order:
26
+ * - a call naming no declared tool answers
27
+ * `{ error: "Function <name> is not found in the toolsDict." }`;
28
+ * - a tool that throws answers `{ error: "Error in tool '<name>': …" }`,
29
+ * the text ADK's FunctionTool writes (lib/tools/adkTool.ts);
30
+ * - with self-correction's tool side on (retries.tool_errors, default
31
+ * 3), those two answer with reflection guidance instead, counted per
32
+ * tool in call order (lib/runtime/native/selfCorrection.ts);
33
+ * - a tool that requires approval and has none asks for it, as
34
+ * FunctionTool's gate does: `{ error: APPROVAL_TEXTS.pending }`,
35
+ * `skipSummarization`, and the request under the call's id;
36
+ * - a long-running tool (ask_user) that returns nothing answers
37
+ * nothing; its actions, when it set any, make an event of their own;
38
+ * - a result that is not an object is wrapped `{ result }`, an array
39
+ * `{ results }`, as ADK wraps them;
40
+ * - a result nested deeper than MAX_VALUE_DEPTH levels answers
41
+ * `{ error: TOO_DEEP_RESULT }` instead: every later reader of the
42
+ * session would overflow its stack on it (lib/runtime/valueDepth.ts,
43
+ * ADR 0101). ADK keeps it.
44
+ * One call's response is stored as its own event; several are merged
45
+ * into one, parts in call order and actions merged (ADK's
46
+ * mergeParallelFunctionResponseEvents). ADK runs the calls one after
47
+ * another and lets a call read the state an earlier call of the same
48
+ * step wrote; here each call reads the state as the step left it.
49
+ * 3. An approval request ends the run: the response is NOT stored; in its
50
+ * place the event ADK stores, a call to `adk_request_confirmation`
51
+ * (args: the original call and the confirmation; an `adk-` id listed in
52
+ * longRunningToolIds), carrying the response's actions. The answer
53
+ * resumes it: before each step, lib/runtime/native/interrupts.ts runs
54
+ * the pinned call the latest user message approves or refuses, as ADK's
55
+ * request-confirmation processor does, and its response is stored
56
+ * before the request is built.
57
+ * 4. The agent's outputKey is written into each final event's stateDelta
58
+ * before it is stored (ADK's maybeSaveOutputToState, case for case).
59
+ * The `temp:` keys of every event stored are kept for the rest of the
60
+ * run, beside the session and never in it, and each later step and
61
+ * call reads them (lib/runtime/native/tempState.ts).
62
+ * 5. The loop goes on unless the step's last event is final (ADK's
63
+ * isFinalResponse), the turn stopped the step, or the step stored
64
+ * nothing. A model call that waits on a person (ask_user) is final: its
65
+ * model event lists the call in longRunningToolIds, and the run ends
66
+ * with no response to it, the call pending. The answer resumes it: the
67
+ * turn runner stores the person's next message as the call's function
68
+ * response, and the next run's first step reads the call and its answer
69
+ * from the history (history.ts), as ADK's content processor does. No
70
+ * request processor acts on it (ADR 0079).
71
+ *
72
+ * DELEGATION: a call to a subagent tool runs the subagent as its own child
73
+ * loop, as ADK's AgentTool runs it (lib/runtime/native/delegate.ts, ADR 0074).
74
+ *
75
+ * SELF-CORRECTION (ADR 0034, ADR 0075) is ADK's reflect-and-retry plugins,
76
+ * ported to lib/runtime/native/selfCorrection.ts: the step declares the
77
+ * reflection tool and passes each response through its model side, and
78
+ * each call's error or answer goes through its tool side. On by default,
79
+ * as runSyndicateTurn installs the plugins by default.
80
+ *
81
+ * COMPACTION (ADR 0033): with the agent's `context:`, each step may first
82
+ * store ADK's compacted event, written by the summary model
83
+ * (lib/runtime/native/compaction.ts). It is yielded like any stored event
84
+ * and is not the run's `lastEvent`.
85
+ *
86
+ * TASK MODE (ADR 0033, ADR 0081): finish_task is an own tool the request
87
+ * declares; a run marked `taskNode` ends on its successful answer
88
+ * (lib/runtime/native/taskMode.ts).
89
+ *
90
+ * WORKFLOW NODES (ADR 0090): a run for a workflow agent node passes
91
+ * `nodeStamp`, which writes the node's output and path on each event before
92
+ * it is stored (lib/workflow/agentNode.ts).
93
+ *
94
+ * CONSENT (WS6-3b, ADR 0085): with `credentials`, each call's
95
+ * `accessToken(provider)` reads the user's grant (ADR 0072). With `consent`,
96
+ * a call whose provider the user has not granted asks for it
97
+ * (`requestCredential`, or `accessToken` by itself): the call answers
98
+ * CONSENT_TEXTS.pending, ADK's `adk_request_credential` event is stored
99
+ * before the response (ADK's generateAuthEvent), and the run ends paused on
100
+ * it. The flow's secrets stay in the consent step (lib/tools/oauthConsent.ts).
101
+ * Before each step, a credential request the latest user message answers
102
+ * runs its paused call again (interrupts.ts grantedCalls, ADK's auth
103
+ * preprocessor), ahead of the approval resume.
104
+ *
105
+ * NOT HERE (later tickets): transfer_to_agent (no compiled syndicate sets
106
+ * subAgents). The run's spans (agent.invoke, model.call,
107
+ * tool.execute) are lib/runtime/native/telemetry.ts.
108
+ *
109
+ * ADK stays out of this file: an ADK tool an agent still lists during the
110
+ * dual period (a registry FunctionTool, the skills toolset's tools) is run
111
+ * through its runAsync with a context shaped like ADK's, by shape.
112
+ */
113
+ import type { ModelAdapter } from '../../models/contract.ts';
114
+ import type { CredentialStore } from '../../tools/auth.ts';
115
+ import type { OAuthConsent } from '../../tools/oauthConsent.ts';
116
+ import type { TurnEvent } from '../events.ts';
117
+ import type { NativeAgent } from './request.ts';
118
+ import { SelfCorrection } from './selfCorrection.ts';
119
+ import type { ModelStepOptions, StepStop } from './step.ts';
120
+ /**
121
+ * Where one agent runs: what runModelStep takes besides the agent and its
122
+ * adapter. The session already holds the run's user event; every event the
123
+ * loop stores is appended to it.
124
+ */
125
+ export interface AgentLoopContext extends Omit<ModelStepOptions, 'agent' | 'adapter' | 'onPartial' | 'model' | 'beforeAppend' | 'redirect' | 'correction' | 'stateOverlay'> {
126
+ /** The leaf adapter for a model id: the agent's, and its fallback's. Default resolveAdapter (lib/models/registry.ts). */
127
+ adapterFor?: (model: string) => ModelAdapter;
128
+ /** Where the fallback's redirect notice goes. Default console.warn, as compile's. */
129
+ log?: (message: string) => void;
130
+ /**
131
+ * Self-correction (ADR 0034, ADR 0075): the turn's one instance, built from
132
+ * the syndicate's `retries:`. Default: retries at their defaults, as
133
+ * runSyndicateTurn installs ADK's plugins by default.
134
+ */
135
+ selfCorrection?: SelfCorrection;
136
+ /**
137
+ * The run is a `mode: task` workflow node's (ADK's runTaskMode): it ends
138
+ * once finish_task answers with success, that event carrying the node's
139
+ * output (lib/runtime/native/taskMode.ts). Default false: a plain run
140
+ * goes on after finish_task's answer, as LlmAgent.runAsync does.
141
+ */
142
+ taskNode?: boolean;
143
+ /**
144
+ * The run is a workflow node's: called on each event before it is stored,
145
+ * after the outputKey and task hooks, as ADK's node runner stamps an event
146
+ * (its output, `nodeInfo.path` and `outputFor`) before its Runner appends
147
+ * it (lib/workflow/agentNode.ts). Not passed to a delegated subagent's loop.
148
+ */
149
+ nodeStamp?: (event: TurnEvent) => void;
150
+ /**
151
+ * The run's tool credentials, already pinned to its app
152
+ * (pinnedCredentialStore, ADR 0072): each call's `accessToken` reads them
153
+ * under the session's user.
154
+ */
155
+ credentials?: Pick<CredentialStore, 'get'>;
156
+ /**
157
+ * The consent step (ADR 0085): a call whose provider the user has not
158
+ * granted asks for it (`requestCredential`, or `accessToken` by itself),
159
+ * and the run pauses on ADK's `adk_request_credential` call. Not passed to
160
+ * a delegated subagent's loop.
161
+ */
162
+ consent?: Pick<OAuthConsent, 'has' | 'begin'>;
163
+ }
164
+ /** How the run ended. */
165
+ export type AgentLoopEndReason =
166
+ /** The last event is a final answer (text, a set_model_response answer, a response that skips summarization). */
167
+ 'final'
168
+ /** A call waits on a person: an ask_user call, or an approval request. `pending` holds the ids. */
169
+ | 'paused'
170
+ /** The last event carries an error (a failed model call), as the ADK path stores it. */
171
+ | 'error'
172
+ /** The turn stopped a step (cancel, deadline, max_steps): no event was stored for it. */
173
+ | 'stopped'
174
+ /** The model answered nothing: no parts, no error, no usage. */
175
+ | 'empty';
176
+ export interface AgentLoopEnd {
177
+ reason: AgentLoopEndReason;
178
+ /** Model steps run (a fallback's answer counts with the step it answered). */
179
+ steps: number;
180
+ /** The last event stored. */
181
+ lastEvent?: TurnEvent;
182
+ /** For `paused`: the ids waiting on a person (the ask_user call ids, or the adk_request_confirmation call ids). */
183
+ pending?: string[];
184
+ /** For `stopped`: the turn's code and message. */
185
+ stop?: StepStop;
186
+ /** For a task-mode node's run that finished: the finish_task arguments, extracted (lib/runtime/native/taskMode.ts). */
187
+ output?: unknown;
188
+ }
189
+ /**
190
+ * Writes the agent's final answer into `event`'s stateDelta under its
191
+ * outputKey: the text of its parts; with an output schema, the parsed and
192
+ * validated JSON (the text itself when it does not parse; the parsed value
193
+ * when it does not validate).
194
+ */
195
+ export declare function saveOutput(agent: Pick<NativeAgent, 'name' | 'outputKey' | 'outputSchema'>, event: TurnEvent): void;
196
+ /** What a tool's result becomes when it nests past MAX_VALUE_DEPTH (ADR 0101). */
197
+ export declare const TOO_DEEP_RESULT: (toolName: string) => string;
198
+ /**
199
+ * Runs `agent` on the session in `ctx` until its answer is final, yielding
200
+ * every event in order: partials as they arrive (never stored), then each
201
+ * event as the store returned it. Returns how the run ended.
202
+ *
203
+ * Never throws for a failed model call or a failing tool: the first is the
204
+ * last event, stored with its error, the second the call's error response.
205
+ * An adapter that throws an Error ends the step on ADK's error event for
206
+ * it (lib/runtime/native/step.ts). Throws where the ADK runtime throws: a
207
+ * request that cannot be built, a store that throws.
208
+ */
209
+ export declare function runAgentLoop(agent: NativeAgent, ctx: AgentLoopContext): AsyncGenerator<TurnEvent, AgentLoopEnd>;