@namzu/sdk 0.1.4 → 0.1.5-rc.1-fix

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 (325) hide show
  1. package/CHANGELOG.md +8 -0
  2. package/dist/advisory/executor.d.ts +2 -2
  3. package/dist/advisory/executor.d.ts.map +1 -1
  4. package/dist/advisory/executor.js.map +1 -1
  5. package/dist/agents/AbstractAgent.d.ts +20 -2
  6. package/dist/agents/AbstractAgent.d.ts.map +1 -1
  7. package/dist/agents/AbstractAgent.js +23 -1
  8. package/dist/agents/AbstractAgent.js.map +1 -1
  9. package/dist/agents/PipelineAgent.d.ts.map +1 -1
  10. package/dist/agents/PipelineAgent.js +1 -1
  11. package/dist/agents/PipelineAgent.js.map +1 -1
  12. package/dist/agents/ReactiveAgent.d.ts.map +1 -1
  13. package/dist/agents/ReactiveAgent.js +1 -0
  14. package/dist/agents/ReactiveAgent.js.map +1 -1
  15. package/dist/agents/RouterAgent.d.ts.map +1 -1
  16. package/dist/agents/RouterAgent.js +4 -2
  17. package/dist/agents/RouterAgent.js.map +1 -1
  18. package/dist/agents/SupervisorAgent.d.ts.map +1 -1
  19. package/dist/agents/SupervisorAgent.js +4 -1
  20. package/dist/agents/SupervisorAgent.js.map +1 -1
  21. package/dist/agents/__tests__/lock.test.d.ts +2 -0
  22. package/dist/agents/__tests__/lock.test.d.ts.map +1 -0
  23. package/dist/agents/__tests__/lock.test.js +131 -0
  24. package/dist/agents/__tests__/lock.test.js.map +1 -0
  25. package/dist/agents/index.d.ts +2 -0
  26. package/dist/agents/index.d.ts.map +1 -1
  27. package/dist/agents/index.js +1 -0
  28. package/dist/agents/index.js.map +1 -1
  29. package/dist/agents/lock.d.ts +42 -0
  30. package/dist/agents/lock.d.ts.map +1 -0
  31. package/dist/agents/lock.js +54 -0
  32. package/dist/agents/lock.js.map +1 -0
  33. package/dist/bridge/a2a/message.d.ts.map +1 -1
  34. package/dist/bridge/a2a/message.js.map +1 -1
  35. package/dist/bridge/tools/connector/router.d.ts +4 -5
  36. package/dist/bridge/tools/connector/router.d.ts.map +1 -1
  37. package/dist/bridge/tools/connector/router.js.map +1 -1
  38. package/dist/compaction/__tests__/SlidingWindowManager.test.d.ts +2 -0
  39. package/dist/compaction/__tests__/SlidingWindowManager.test.d.ts.map +1 -0
  40. package/dist/compaction/__tests__/SlidingWindowManager.test.js +113 -0
  41. package/dist/compaction/__tests__/SlidingWindowManager.test.js.map +1 -0
  42. package/dist/compaction/__tests__/dangling.test.d.ts +2 -0
  43. package/dist/compaction/__tests__/dangling.test.d.ts.map +1 -0
  44. package/dist/compaction/__tests__/dangling.test.js +356 -0
  45. package/dist/compaction/__tests__/dangling.test.js.map +1 -0
  46. package/dist/compaction/__tests__/factory.test.d.ts +2 -0
  47. package/dist/compaction/__tests__/factory.test.d.ts.map +1 -0
  48. package/dist/compaction/__tests__/factory.test.js +43 -0
  49. package/dist/compaction/__tests__/factory.test.js.map +1 -0
  50. package/dist/compaction/dangling.d.ts +96 -0
  51. package/dist/compaction/dangling.d.ts.map +1 -0
  52. package/dist/compaction/dangling.js +274 -0
  53. package/dist/compaction/dangling.js.map +1 -0
  54. package/dist/compaction/factory.d.ts +20 -0
  55. package/dist/compaction/factory.d.ts.map +1 -0
  56. package/dist/compaction/factory.js +35 -0
  57. package/dist/compaction/factory.js.map +1 -0
  58. package/dist/compaction/index.d.ts +5 -0
  59. package/dist/compaction/index.d.ts.map +1 -1
  60. package/dist/compaction/index.js +3 -0
  61. package/dist/compaction/index.js.map +1 -1
  62. package/dist/compaction/interface.d.ts +33 -0
  63. package/dist/compaction/interface.d.ts.map +1 -0
  64. package/dist/compaction/interface.js +2 -0
  65. package/dist/compaction/interface.js.map +1 -0
  66. package/dist/compaction/managers/index.d.ts +4 -0
  67. package/dist/compaction/managers/index.d.ts.map +1 -0
  68. package/dist/compaction/managers/index.js +4 -0
  69. package/dist/compaction/managers/index.js.map +1 -0
  70. package/dist/compaction/managers/null.d.ts +12 -0
  71. package/dist/compaction/managers/null.d.ts.map +1 -0
  72. package/dist/compaction/managers/null.js +15 -0
  73. package/dist/compaction/managers/null.js.map +1 -0
  74. package/dist/compaction/managers/slidingWindow.d.ts +27 -0
  75. package/dist/compaction/managers/slidingWindow.d.ts.map +1 -0
  76. package/dist/compaction/managers/slidingWindow.js +41 -0
  77. package/dist/compaction/managers/slidingWindow.js.map +1 -0
  78. package/dist/compaction/managers/structured.d.ts +23 -0
  79. package/dist/compaction/managers/structured.d.ts.map +1 -0
  80. package/dist/compaction/managers/structured.js +144 -0
  81. package/dist/compaction/managers/structured.js.map +1 -0
  82. package/dist/compaction/types.d.ts +1 -1
  83. package/dist/compaction/types.d.ts.map +1 -1
  84. package/dist/config/runtime.d.ts +16 -16
  85. package/dist/config/runtime.js +1 -1
  86. package/dist/config/runtime.js.map +1 -1
  87. package/dist/constants/agent/index.d.ts +1 -1
  88. package/dist/constants/agent/index.d.ts.map +1 -1
  89. package/dist/gateway/local.d.ts +2 -2
  90. package/dist/gateway/local.d.ts.map +1 -1
  91. package/dist/gateway/local.js +10 -1
  92. package/dist/gateway/local.js.map +1 -1
  93. package/dist/index.d.ts +18 -4
  94. package/dist/index.d.ts.map +1 -1
  95. package/dist/index.js +12 -2
  96. package/dist/index.js.map +1 -1
  97. package/dist/manager/agent/lifecycle.d.ts.map +1 -1
  98. package/dist/manager/agent/lifecycle.js +3 -2
  99. package/dist/manager/agent/lifecycle.js.map +1 -1
  100. package/dist/manager/run/persistence.d.ts +1 -2
  101. package/dist/manager/run/persistence.d.ts.map +1 -1
  102. package/dist/manager/run/persistence.js +2 -1
  103. package/dist/manager/run/persistence.js.map +1 -1
  104. package/dist/plugin/__tests__/lifecycle.test.d.ts +2 -0
  105. package/dist/plugin/__tests__/lifecycle.test.d.ts.map +1 -0
  106. package/dist/plugin/__tests__/lifecycle.test.js +332 -0
  107. package/dist/plugin/__tests__/lifecycle.test.js.map +1 -0
  108. package/dist/plugin/lifecycle.d.ts +2 -2
  109. package/dist/plugin/lifecycle.d.ts.map +1 -1
  110. package/dist/plugin/lifecycle.js +28 -2
  111. package/dist/plugin/lifecycle.js.map +1 -1
  112. package/dist/plugin/resolver.d.ts +2 -2
  113. package/dist/plugin/resolver.d.ts.map +1 -1
  114. package/dist/plugin/resolver.js.map +1 -1
  115. package/dist/registry/agent/definitions.d.ts +3 -2
  116. package/dist/registry/agent/definitions.d.ts.map +1 -1
  117. package/dist/registry/agent/definitions.js.map +1 -1
  118. package/dist/registry/tool/execute.d.ts +2 -5
  119. package/dist/registry/tool/execute.d.ts.map +1 -1
  120. package/dist/registry/tool/execute.js.map +1 -1
  121. package/dist/runtime/decision/parser.d.ts.map +1 -1
  122. package/dist/runtime/decision/parser.js +15 -40
  123. package/dist/runtime/decision/parser.js.map +1 -1
  124. package/dist/runtime/query/context-cache.d.ts +3 -3
  125. package/dist/runtime/query/context-cache.d.ts.map +1 -1
  126. package/dist/runtime/query/context-cache.js.map +1 -1
  127. package/dist/runtime/query/context.d.ts +1 -1
  128. package/dist/runtime/query/context.d.ts.map +1 -1
  129. package/dist/runtime/query/context.js.map +1 -1
  130. package/dist/runtime/query/events.js +11 -11
  131. package/dist/runtime/query/events.js.map +1 -1
  132. package/dist/runtime/query/executor.d.ts +4 -2
  133. package/dist/runtime/query/executor.d.ts.map +1 -1
  134. package/dist/runtime/query/executor.js +1 -0
  135. package/dist/runtime/query/executor.js.map +1 -1
  136. package/dist/runtime/query/index.d.ts +5 -3
  137. package/dist/runtime/query/index.d.ts.map +1 -1
  138. package/dist/runtime/query/index.js +2 -1
  139. package/dist/runtime/query/index.js.map +1 -1
  140. package/dist/runtime/query/iteration/index.d.ts +2 -2
  141. package/dist/runtime/query/iteration/index.d.ts.map +1 -1
  142. package/dist/runtime/query/iteration/index.js.map +1 -1
  143. package/dist/runtime/query/iteration/phases/advisory.d.ts.map +1 -1
  144. package/dist/runtime/query/iteration/phases/advisory.js.map +1 -1
  145. package/dist/runtime/query/iteration/phases/checkpoint.d.ts +1 -1
  146. package/dist/runtime/query/iteration/phases/checkpoint.d.ts.map +1 -1
  147. package/dist/runtime/query/iteration/phases/checkpoint.js.map +1 -1
  148. package/dist/runtime/query/iteration/phases/context.d.ts +2 -2
  149. package/dist/runtime/query/iteration/phases/context.d.ts.map +1 -1
  150. package/dist/runtime/query/iteration/phases/plan.d.ts +1 -1
  151. package/dist/runtime/query/iteration/phases/plan.d.ts.map +1 -1
  152. package/dist/runtime/query/iteration/phases/plan.js.map +1 -1
  153. package/dist/runtime/query/prompt.d.ts +2 -2
  154. package/dist/runtime/query/prompt.d.ts.map +1 -1
  155. package/dist/runtime/query/prompt.js.map +1 -1
  156. package/dist/runtime/query/result.d.ts +1 -1
  157. package/dist/runtime/query/result.d.ts.map +1 -1
  158. package/dist/runtime/query/result.js.map +1 -1
  159. package/dist/runtime/query/tooling.d.ts +4 -2
  160. package/dist/runtime/query/tooling.d.ts.map +1 -1
  161. package/dist/runtime/query/tooling.js +1 -0
  162. package/dist/runtime/query/tooling.js.map +1 -1
  163. package/dist/store/conversation/memory.d.ts +1 -1
  164. package/dist/store/conversation/memory.d.ts.map +1 -1
  165. package/dist/store/conversation/memory.js +15 -3
  166. package/dist/store/conversation/memory.js.map +1 -1
  167. package/dist/store/run/disk.d.ts +1 -2
  168. package/dist/store/run/disk.d.ts.map +1 -1
  169. package/dist/store/run/disk.js +21 -13
  170. package/dist/store/run/disk.js.map +1 -1
  171. package/dist/tools/builtins/__tests__/structuredOutput.example.d.ts +140 -0
  172. package/dist/tools/builtins/__tests__/structuredOutput.example.d.ts.map +1 -0
  173. package/dist/tools/builtins/__tests__/structuredOutput.example.js +183 -0
  174. package/dist/tools/builtins/__tests__/structuredOutput.example.js.map +1 -0
  175. package/dist/tools/builtins/__tests__/structuredOutput.test.d.ts +2 -0
  176. package/dist/tools/builtins/__tests__/structuredOutput.test.d.ts.map +1 -0
  177. package/dist/tools/builtins/__tests__/structuredOutput.test.js +224 -0
  178. package/dist/tools/builtins/__tests__/structuredOutput.test.js.map +1 -0
  179. package/dist/tools/builtins/grep.d.ts.map +1 -1
  180. package/dist/tools/builtins/grep.js +1 -2
  181. package/dist/tools/builtins/grep.js.map +1 -1
  182. package/dist/tools/builtins/index.d.ts +1 -0
  183. package/dist/tools/builtins/index.d.ts.map +1 -1
  184. package/dist/tools/builtins/index.js +3 -0
  185. package/dist/tools/builtins/index.js.map +1 -1
  186. package/dist/tools/builtins/ls.d.ts +1 -1
  187. package/dist/tools/builtins/structuredOutput.d.ts +27 -0
  188. package/dist/tools/builtins/structuredOutput.d.ts.map +1 -0
  189. package/dist/tools/builtins/structuredOutput.js +46 -0
  190. package/dist/tools/builtins/structuredOutput.js.map +1 -0
  191. package/dist/tools/task/list.d.ts +1 -1
  192. package/dist/tools/task/list.d.ts.map +1 -1
  193. package/dist/tools/task/list.js.map +1 -1
  194. package/dist/types/agent/base.d.ts +4 -1
  195. package/dist/types/agent/base.d.ts.map +1 -1
  196. package/dist/types/agent/index.d.ts +1 -0
  197. package/dist/types/agent/index.d.ts.map +1 -1
  198. package/dist/types/agent/index.js +1 -0
  199. package/dist/types/agent/index.js.map +1 -1
  200. package/dist/types/agent/manager.d.ts +27 -0
  201. package/dist/types/agent/manager.d.ts.map +1 -0
  202. package/dist/types/agent/manager.js +2 -0
  203. package/dist/types/agent/manager.js.map +1 -0
  204. package/dist/types/agent/reactive.d.ts +2 -2
  205. package/dist/types/agent/reactive.d.ts.map +1 -1
  206. package/dist/types/agent/supervisor.d.ts +2 -2
  207. package/dist/types/agent/supervisor.d.ts.map +1 -1
  208. package/dist/types/agent/task.d.ts +0 -2
  209. package/dist/types/agent/task.d.ts.map +1 -1
  210. package/dist/types/agent/task.js +0 -2
  211. package/dist/types/agent/task.js.map +1 -1
  212. package/dist/types/common/index.d.ts +0 -1
  213. package/dist/types/common/index.d.ts.map +1 -1
  214. package/dist/types/common/index.js +0 -1
  215. package/dist/types/common/index.js.map +1 -1
  216. package/dist/types/hitl/index.d.ts +1 -2
  217. package/dist/types/hitl/index.d.ts.map +1 -1
  218. package/dist/types/hitl/index.js.map +1 -1
  219. package/dist/types/invocation/__tests__/state.test.d.ts +2 -0
  220. package/dist/types/invocation/__tests__/state.test.d.ts.map +1 -0
  221. package/dist/types/invocation/__tests__/state.test.js +167 -0
  222. package/dist/types/invocation/__tests__/state.test.js.map +1 -0
  223. package/dist/types/invocation/index.d.ts +37 -0
  224. package/dist/types/invocation/index.d.ts.map +1 -0
  225. package/dist/types/invocation/index.js +23 -0
  226. package/dist/types/invocation/index.js.map +1 -0
  227. package/dist/types/plugin/index.d.ts +6 -0
  228. package/dist/types/plugin/index.d.ts.map +1 -1
  229. package/dist/types/plugin/index.js +16 -0
  230. package/dist/types/plugin/index.js.map +1 -1
  231. package/dist/types/run/events.d.ts +1 -1
  232. package/dist/types/run/events.d.ts.map +1 -1
  233. package/dist/types/run/index.d.ts +1 -0
  234. package/dist/types/run/index.d.ts.map +1 -1
  235. package/dist/types/run/index.js +1 -0
  236. package/dist/types/run/index.js.map +1 -1
  237. package/dist/types/run/metadata.d.ts +1 -1
  238. package/dist/types/run/metadata.d.ts.map +1 -1
  239. package/dist/types/run/state.d.ts +1 -1
  240. package/dist/types/run/state.d.ts.map +1 -1
  241. package/dist/types/run/stop-reason.d.ts +2 -0
  242. package/dist/types/run/stop-reason.d.ts.map +1 -0
  243. package/dist/types/run/stop-reason.js +2 -0
  244. package/dist/types/run/stop-reason.js.map +1 -0
  245. package/dist/types/structured-output/index.d.ts +51 -0
  246. package/dist/types/structured-output/index.d.ts.map +1 -0
  247. package/dist/types/structured-output/index.js +2 -0
  248. package/dist/types/structured-output/index.js.map +1 -0
  249. package/dist/types/tool/index.d.ts +36 -0
  250. package/dist/types/tool/index.d.ts.map +1 -1
  251. package/package.json +1 -1
  252. package/src/advisory/executor.ts +2 -4
  253. package/src/agents/AbstractAgent.ts +26 -3
  254. package/src/agents/PipelineAgent.ts +1 -1
  255. package/src/agents/ReactiveAgent.ts +1 -0
  256. package/src/agents/RouterAgent.ts +8 -2
  257. package/src/agents/SupervisorAgent.ts +5 -1
  258. package/src/agents/__tests__/lock.test.ts +158 -0
  259. package/src/agents/index.ts +2 -0
  260. package/src/agents/lock.ts +66 -0
  261. package/src/bridge/a2a/message.ts +1 -2
  262. package/src/bridge/tools/connector/router.ts +4 -5
  263. package/src/compaction/__tests__/SlidingWindowManager.test.ts +139 -0
  264. package/src/compaction/__tests__/dangling.test.ts +447 -0
  265. package/src/compaction/__tests__/factory.test.ts +53 -0
  266. package/src/compaction/dangling.ts +321 -0
  267. package/src/compaction/factory.ts +41 -0
  268. package/src/compaction/index.ts +14 -0
  269. package/src/compaction/interface.ts +35 -0
  270. package/src/compaction/managers/index.ts +3 -0
  271. package/src/compaction/managers/null.ts +19 -0
  272. package/src/compaction/managers/slidingWindow.ts +57 -0
  273. package/src/compaction/managers/structured.ts +169 -0
  274. package/src/compaction/types.ts +1 -1
  275. package/src/config/runtime.ts +1 -1
  276. package/src/constants/agent/index.ts +1 -1
  277. package/src/gateway/local.ts +13 -4
  278. package/src/index.ts +38 -1
  279. package/src/manager/agent/lifecycle.ts +3 -2
  280. package/src/manager/run/persistence.ts +3 -8
  281. package/src/plugin/__tests__/lifecycle.test.ts +430 -0
  282. package/src/plugin/lifecycle.ts +32 -6
  283. package/src/plugin/resolver.ts +3 -3
  284. package/src/registry/agent/definitions.ts +3 -2
  285. package/src/registry/tool/execute.ts +2 -5
  286. package/src/runtime/decision/parser.ts +15 -40
  287. package/src/runtime/query/context-cache.ts +3 -4
  288. package/src/runtime/query/context.ts +1 -2
  289. package/src/runtime/query/events.ts +11 -11
  290. package/src/runtime/query/executor.ts +5 -3
  291. package/src/runtime/query/index.ts +11 -4
  292. package/src/runtime/query/iteration/index.ts +2 -2
  293. package/src/runtime/query/iteration/phases/advisory.ts +1 -2
  294. package/src/runtime/query/iteration/phases/checkpoint.ts +1 -2
  295. package/src/runtime/query/iteration/phases/context.ts +2 -2
  296. package/src/runtime/query/iteration/phases/plan.ts +1 -2
  297. package/src/runtime/query/prompt.ts +3 -3
  298. package/src/runtime/query/result.ts +1 -2
  299. package/src/runtime/query/tooling.ts +5 -2
  300. package/src/store/conversation/memory.ts +21 -5
  301. package/src/store/run/disk.ts +18 -16
  302. package/src/tools/builtins/__tests__/structuredOutput.example.ts +221 -0
  303. package/src/tools/builtins/__tests__/structuredOutput.test.ts +275 -0
  304. package/src/tools/builtins/grep.ts +1 -2
  305. package/src/tools/builtins/index.ts +3 -0
  306. package/src/tools/builtins/structuredOutput.ts +55 -0
  307. package/src/tools/task/list.ts +1 -2
  308. package/src/types/agent/base.ts +5 -1
  309. package/src/types/agent/index.ts +1 -0
  310. package/src/types/agent/manager.ts +36 -0
  311. package/src/types/agent/reactive.ts +2 -2
  312. package/src/types/agent/supervisor.ts +2 -2
  313. package/src/types/agent/task.ts +0 -4
  314. package/src/types/common/index.ts +0 -2
  315. package/src/types/hitl/index.ts +1 -2
  316. package/src/types/invocation/__tests__/state.test.ts +210 -0
  317. package/src/types/invocation/index.ts +55 -0
  318. package/src/types/plugin/index.ts +19 -0
  319. package/src/types/run/events.ts +1 -10
  320. package/src/types/run/index.ts +1 -0
  321. package/src/types/run/metadata.ts +1 -1
  322. package/src/types/run/state.ts +1 -1
  323. package/src/types/run/stop-reason.ts +10 -0
  324. package/src/types/structured-output/index.ts +56 -0
  325. package/src/types/tool/index.ts +45 -0
@@ -0,0 +1,221 @@
1
+ /**
2
+ * Real-world examples of using createStructuredOutputTool
3
+ *
4
+ * These examples demonstrate how to use the structured output tool
5
+ * to force agents to produce validated, typed output.
6
+ */
7
+
8
+ import { z } from 'zod'
9
+ import { createStructuredOutputTool } from '../structuredOutput.js'
10
+
11
+ // ============================================================================
12
+ // Example 1: Code Review Analysis
13
+ // ============================================================================
14
+
15
+ const CodeReviewSchema = z.object({
16
+ status: z.enum(['approved', 'changes_requested', 'rejected']),
17
+ summary: z.string().describe('Overall summary of the review'),
18
+ issues: z.array(
19
+ z.object({
20
+ file: z.string(),
21
+ line: z.number().int().positive(),
22
+ severity: z.enum(['critical', 'major', 'minor']),
23
+ message: z.string(),
24
+ suggestion: z.string().optional(),
25
+ }),
26
+ ),
27
+ strengths: z.array(z.string()).describe('What was done well'),
28
+ suggestions: z.array(z.string()).describe('Actionable improvements'),
29
+ })
30
+
31
+ export const createCodeReviewTool = () => createStructuredOutputTool(CodeReviewSchema)
32
+
33
+ // Usage example:
34
+ // const tool = createCodeReviewTool()
35
+ // await toolRegistry.register(tool)
36
+ // // Model analyzes code...
37
+ // const review = extractToolResult(messages, 'structured_output')
38
+ // const typedReview: z.infer<typeof CodeReviewSchema> = JSON.parse(review.output)
39
+
40
+ // ============================================================================
41
+ // Example 2: Document Summarization
42
+ // ============================================================================
43
+
44
+ const DocumentSummarySchema = z.object({
45
+ title: z.string(),
46
+ category: z.enum(['technical', 'business', 'legal', 'other']),
47
+ summary: z.string().min(100).max(500),
48
+ key_points: z.array(z.string()).min(3).max(10),
49
+ metadata: z.object({
50
+ source: z.string().optional(),
51
+ date: z.string().datetime().optional(),
52
+ language: z.string().default('en'),
53
+ }),
54
+ })
55
+
56
+ export const createDocumentSummaryTool = () => createStructuredOutputTool(DocumentSummarySchema)
57
+
58
+ // ============================================================================
59
+ // Example 3: Data Extraction
60
+ // ============================================================================
61
+
62
+ const ExtractedDataSchema = z.object({
63
+ entities: z.array(
64
+ z.object({
65
+ type: z.enum(['person', 'organization', 'location', 'date', 'amount']),
66
+ text: z.string(),
67
+ confidence: z.number().min(0).max(1),
68
+ }),
69
+ ),
70
+ relationships: z.array(
71
+ z.object({
72
+ subject: z.string(),
73
+ predicate: z.string(),
74
+ object: z.string(),
75
+ }),
76
+ ),
77
+ })
78
+
79
+ export const createDataExtractionTool = () => createStructuredOutputTool(ExtractedDataSchema)
80
+
81
+ // ============================================================================
82
+ // Example 4: Task Execution Status
83
+ // ============================================================================
84
+
85
+ const TaskResultSchema = z.object({
86
+ task_id: z.string(),
87
+ status: z.enum(['success', 'partial', 'failed']),
88
+ result: z.record(z.unknown()).describe('Task-specific result data'),
89
+ errors: z
90
+ .array(
91
+ z.object({
92
+ code: z.string(),
93
+ message: z.string(),
94
+ details: z.string().optional(),
95
+ }),
96
+ )
97
+ .default([]),
98
+ metadata: z.object({
99
+ duration_ms: z.number().int().positive().optional(),
100
+ attempts: z.number().int().min(1),
101
+ timestamp: z.string().datetime(),
102
+ }),
103
+ })
104
+
105
+ export const createTaskResultTool = () => createStructuredOutputTool(TaskResultSchema)
106
+
107
+ // ============================================================================
108
+ // Example 5: Diagnosis/Decision Making
109
+ // ============================================================================
110
+
111
+ const DiagnosisSchema = z.object({
112
+ diagnosis: z.string(),
113
+ confidence: z.number().min(0).max(1),
114
+ differential: z.array(
115
+ z.object({
116
+ diagnosis: z.string(),
117
+ probability: z.number().min(0).max(1),
118
+ }),
119
+ ),
120
+ recommended_actions: z.array(z.string()),
121
+ urgency: z.enum(['low', 'medium', 'high', 'critical']),
122
+ })
123
+
124
+ export const createDiagnosisTool = () => createStructuredOutputTool(DiagnosisSchema)
125
+
126
+ // ============================================================================
127
+ // Example 6: Multi-language Translation
128
+ // ============================================================================
129
+
130
+ const TranslationSchema = z.object({
131
+ source_text: z.string(),
132
+ source_language: z.string(),
133
+ target_language: z.string(),
134
+ translation: z.string(),
135
+ confidence: z.number().min(0).max(1),
136
+ notes: z.string().optional(),
137
+ alternatives: z
138
+ .array(
139
+ z.object({
140
+ text: z.string(),
141
+ note: z.string(),
142
+ }),
143
+ )
144
+ .optional(),
145
+ })
146
+
147
+ export const createTranslationTool = () => createStructuredOutputTool(TranslationSchema)
148
+
149
+ // ============================================================================
150
+ // Example 7: Test Report Generation
151
+ // ============================================================================
152
+
153
+ const TestReportSchema = z.object({
154
+ total_tests: z.number().int().nonnegative(),
155
+ passed: z.number().int().nonnegative(),
156
+ failed: z.number().int().nonnegative(),
157
+ skipped: z.number().int().nonnegative(),
158
+ coverage_percent: z.number().min(0).max(100),
159
+ failures: z.array(
160
+ z.object({
161
+ test_name: z.string(),
162
+ error_message: z.string(),
163
+ stack_trace: z.string().optional(),
164
+ }),
165
+ ),
166
+ summary: z.string(),
167
+ })
168
+
169
+ export const createTestReportTool = () => createStructuredOutputTool(TestReportSchema)
170
+
171
+ // ============================================================================
172
+ // Integration Pattern
173
+ // ============================================================================
174
+
175
+ /**
176
+ * Example of how to integrate structured output into an agent iteration:
177
+ *
178
+ * ```typescript
179
+ * async function queryWithStructuredOutput(
180
+ * toolRegistry: ToolRegistry,
181
+ * schema: z.ZodType,
182
+ * messages: Message[]
183
+ * ): Promise<any> {
184
+ * const tool = createStructuredOutputTool(schema)
185
+ *
186
+ * try {
187
+ * // Register the tool
188
+ * toolRegistry.register(tool)
189
+ *
190
+ * // Run the iteration with this tool available
191
+ * // (in real usage, this would be integrated with the iteration loop)
192
+ * const response = await provider.chat({
193
+ * messages,
194
+ * tools: toolRegistry.toLLMTools(),
195
+ * toolChoice: {
196
+ * type: 'function',
197
+ * function: { name: 'structured_output' }
198
+ * }
199
+ * })
200
+ *
201
+ * // Extract the result
202
+ * const toolCall = response.message.toolCalls?.find(
203
+ * tc => tc.function.name === 'structured_output'
204
+ * )
205
+ *
206
+ * if (toolCall) {
207
+ * const args = JSON.parse(toolCall.function.arguments)
208
+ * return {
209
+ * success: true,
210
+ * data: args
211
+ * }
212
+ * }
213
+ *
214
+ * return { success: false, error: 'Tool was not called' }
215
+ * } finally {
216
+ * // Clean up
217
+ * toolRegistry.unregister('structured_output')
218
+ * }
219
+ * }
220
+ * ```
221
+ */
@@ -0,0 +1,275 @@
1
+ import { describe, expect, it } from 'vitest'
2
+ import { z } from 'zod'
3
+ import type { ToolContext } from '../../../types/tool/index.js'
4
+ import { STRUCTURED_OUTPUT_TOOL_NAME, createStructuredOutputTool } from '../structuredOutput.js'
5
+
6
+ describe('structuredOutput', () => {
7
+ describe('STRUCTURED_OUTPUT_TOOL_NAME', () => {
8
+ it('should export the constant with correct value', () => {
9
+ expect(STRUCTURED_OUTPUT_TOOL_NAME).toBe('structured_output')
10
+ })
11
+ })
12
+
13
+ describe('createStructuredOutputTool', () => {
14
+ it('should create a tool with correct name and description', () => {
15
+ const schema = z.object({ result: z.string() })
16
+ const tool = createStructuredOutputTool(schema)
17
+
18
+ expect(tool.name).toBe('structured_output')
19
+ expect(tool.description).toContain('MUST call this tool')
20
+ })
21
+
22
+ it('should have correct tool properties', () => {
23
+ const schema = z.object({ result: z.string() })
24
+ const tool = createStructuredOutputTool(schema)
25
+
26
+ expect(tool.category).toBe('analysis')
27
+ expect(tool.isReadOnly?.({} as any)).toBe(true)
28
+ expect(tool.isDestructive?.({} as any)).toBe(false)
29
+ expect(tool.isConcurrencySafe?.({} as any)).toBe(true)
30
+ expect(tool.permissions).toEqual([])
31
+ })
32
+
33
+ it('should use the provided schema as input schema', () => {
34
+ const schema = z.object({
35
+ title: z.string(),
36
+ count: z.number(),
37
+ })
38
+ const tool = createStructuredOutputTool(schema)
39
+
40
+ expect(tool.inputSchema).toBe(schema)
41
+ })
42
+
43
+ it('should execute successfully with valid input', async () => {
44
+ const schema = z.object({
45
+ title: z.string(),
46
+ summary: z.string(),
47
+ })
48
+ const tool = createStructuredOutputTool(schema)
49
+
50
+ const input = {
51
+ title: 'Test Title',
52
+ summary: 'Test Summary',
53
+ }
54
+
55
+ const mockContext: ToolContext = {
56
+ runId: 'run_test' as any,
57
+ workingDirectory: '/tmp',
58
+ abortSignal: new AbortController().signal,
59
+ env: {},
60
+ log: () => {},
61
+ }
62
+
63
+ const result = await tool.execute(input, mockContext)
64
+
65
+ expect(result.success).toBe(true)
66
+ expect(result.data).toEqual(input)
67
+ expect(result.output).toBe(JSON.stringify(input))
68
+ expect(result.error).toBeUndefined()
69
+ })
70
+
71
+ it('should handle complex nested schemas', async () => {
72
+ const schema = z.object({
73
+ name: z.string(),
74
+ metadata: z.object({
75
+ tags: z.array(z.string()),
76
+ settings: z.record(z.unknown()),
77
+ }),
78
+ })
79
+ const tool = createStructuredOutputTool(schema)
80
+
81
+ const input = {
82
+ name: 'Complex Object',
83
+ metadata: {
84
+ tags: ['tag1', 'tag2'],
85
+ settings: { timeout: 5000, retries: 3 },
86
+ },
87
+ }
88
+
89
+ const mockContext: ToolContext = {
90
+ runId: 'run_test' as any,
91
+ workingDirectory: '/tmp',
92
+ abortSignal: new AbortController().signal,
93
+ env: {},
94
+ log: () => {},
95
+ }
96
+
97
+ const result = await tool.execute(input, mockContext)
98
+
99
+ expect(result.success).toBe(true)
100
+ expect(result.data).toEqual(input)
101
+ const parsed = JSON.parse(result.output)
102
+ expect(parsed).toEqual(input)
103
+ })
104
+
105
+ it('should handle arrays in schema', async () => {
106
+ const schema = z.object({
107
+ items: z.array(
108
+ z.object({
109
+ id: z.number(),
110
+ value: z.string(),
111
+ }),
112
+ ),
113
+ })
114
+ const tool = createStructuredOutputTool(schema)
115
+
116
+ const input = {
117
+ items: [
118
+ { id: 1, value: 'first' },
119
+ { id: 2, value: 'second' },
120
+ ],
121
+ }
122
+
123
+ const mockContext: ToolContext = {
124
+ runId: 'run_test' as any,
125
+ workingDirectory: '/tmp',
126
+ abortSignal: new AbortController().signal,
127
+ env: {},
128
+ log: () => {},
129
+ }
130
+
131
+ const result = await tool.execute(input, mockContext)
132
+
133
+ expect(result.success).toBe(true)
134
+ expect(result.data).toEqual(input)
135
+ expect(JSON.parse(result.output)).toEqual(input)
136
+ })
137
+
138
+ it('should handle optional fields', async () => {
139
+ const schema = z.object({
140
+ required: z.string(),
141
+ optional: z.string().optional(),
142
+ })
143
+ const tool = createStructuredOutputTool(schema)
144
+
145
+ const input = {
146
+ required: 'value',
147
+ optional: undefined,
148
+ }
149
+
150
+ const mockContext: ToolContext = {
151
+ runId: 'run_test' as any,
152
+ workingDirectory: '/tmp',
153
+ abortSignal: new AbortController().signal,
154
+ env: {},
155
+ log: () => {},
156
+ }
157
+
158
+ const result = await tool.execute(input, mockContext)
159
+
160
+ expect(result.success).toBe(true)
161
+ expect((result.data as typeof input)?.required).toBe('value')
162
+ })
163
+
164
+ it('should handle boolean and number values', async () => {
165
+ const schema = z.object({
166
+ active: z.boolean(),
167
+ count: z.number(),
168
+ rate: z.number(),
169
+ })
170
+ const tool = createStructuredOutputTool(schema)
171
+
172
+ const input = {
173
+ active: true,
174
+ count: 42,
175
+ rate: 3.14,
176
+ }
177
+
178
+ const mockContext: ToolContext = {
179
+ runId: 'run_test' as any,
180
+ workingDirectory: '/tmp',
181
+ abortSignal: new AbortController().signal,
182
+ env: {},
183
+ log: () => {},
184
+ }
185
+
186
+ const result = await tool.execute(input, mockContext)
187
+
188
+ expect(result.success).toBe(true)
189
+ expect(result.data).toEqual(input)
190
+ })
191
+
192
+ it('should support union types', async () => {
193
+ const schema = z.object({
194
+ status: z.enum(['pending', 'completed', 'failed']),
195
+ result: z.union([z.string(), z.number()]),
196
+ })
197
+ const tool = createStructuredOutputTool(schema)
198
+
199
+ const input = {
200
+ status: 'completed' as const,
201
+ result: 'success',
202
+ }
203
+
204
+ const mockContext: ToolContext = {
205
+ runId: 'run_test' as any,
206
+ workingDirectory: '/tmp',
207
+ abortSignal: new AbortController().signal,
208
+ env: {},
209
+ log: () => {},
210
+ }
211
+
212
+ const result = await tool.execute(input, mockContext)
213
+
214
+ expect(result.success).toBe(true)
215
+ expect((result.data as typeof input)?.status).toBe('completed')
216
+ expect((result.data as typeof input)?.result).toBe('success')
217
+ })
218
+
219
+ it('should create multiple independent tools', () => {
220
+ const schema1 = z.object({ type: z.literal('type1'), data: z.string() })
221
+ const schema2 = z.object({ type: z.literal('type2'), count: z.number() })
222
+
223
+ const tool1 = createStructuredOutputTool(schema1)
224
+ const tool2 = createStructuredOutputTool(schema2)
225
+
226
+ // Both should have the same name (that's the point - it's a special tool)
227
+ expect(tool1.name).toBe(tool2.name)
228
+ expect(tool1.name).toBe('structured_output')
229
+
230
+ // But their input schemas should be different
231
+ expect(tool1.inputSchema).not.toBe(tool2.inputSchema)
232
+ })
233
+
234
+ it('should not throw on execute', async () => {
235
+ const schema = z.object({ value: z.string() })
236
+ const tool = createStructuredOutputTool(schema)
237
+
238
+ const mockContext: ToolContext = {
239
+ runId: 'run_test' as any,
240
+ workingDirectory: '/tmp',
241
+ abortSignal: new AbortController().signal,
242
+ env: {},
243
+ log: () => {},
244
+ }
245
+
246
+ await expect(tool.execute({ value: 'test' }, mockContext)).resolves.toBeDefined()
247
+ })
248
+
249
+ it('should handle null values in nullable fields', async () => {
250
+ const schema = z.object({
251
+ required: z.string(),
252
+ nullable: z.string().nullable(),
253
+ })
254
+ const tool = createStructuredOutputTool(schema)
255
+
256
+ const input = {
257
+ required: 'value',
258
+ nullable: null,
259
+ }
260
+
261
+ const mockContext: ToolContext = {
262
+ runId: 'run_test' as any,
263
+ workingDirectory: '/tmp',
264
+ abortSignal: new AbortController().signal,
265
+ env: {},
266
+ log: () => {},
267
+ }
268
+
269
+ const result = await tool.execute(input, mockContext)
270
+
271
+ expect(result.success).toBe(true)
272
+ expect((result.data as typeof input)?.nullable).toBeNull()
273
+ })
274
+ })
275
+ })
@@ -1,5 +1,4 @@
1
- import { readFile } from 'node:fs/promises'
2
- import { glob } from 'node:fs/promises'
1
+ import { glob, readFile } from 'node:fs/promises'
3
2
  import { relative, resolve } from 'node:path'
4
3
  import { z } from 'zod'
5
4
  import { defineTool } from '../defineTool.js'
@@ -6,6 +6,7 @@ export { GlobTool } from './glob.js'
6
6
  export { GrepTool } from './grep.js'
7
7
  export { LsTool } from './ls.js'
8
8
  export { SearchToolsTool } from './search-tools.js'
9
+ export { createStructuredOutputTool, STRUCTURED_OUTPUT_TOOL_NAME } from './structuredOutput.js'
9
10
 
10
11
  import type { ToolDefinition } from '../../types/tool/index.js'
11
12
  import { BashTool } from './bash.js'
@@ -16,6 +17,8 @@ import { LsTool } from './ls.js'
16
17
  import { ReadFileTool } from './read-file.js'
17
18
  import { SearchToolsTool } from './search-tools.js'
18
19
  import { WriteFileTool } from './write-file.js'
20
+ // Note: createStructuredOutputTool is not included in getBuiltinTools()
21
+ // because it requires a schema parameter and is created per-use case
19
22
 
20
23
  export function getBuiltinTools(): ToolDefinition[] {
21
24
  return [
@@ -0,0 +1,55 @@
1
+ import type { z } from 'zod'
2
+ import type { ToolContext, ToolResult } from '../../types/tool/index.js'
3
+ import { defineTool } from '../defineTool.js'
4
+
5
+ export const STRUCTURED_OUTPUT_TOOL_NAME = 'structured_output' as const
6
+
7
+ /**
8
+ * Creates a structured output tool that wraps a Zod schema.
9
+ *
10
+ * This tool is used to force the model to produce validated output by presenting
11
+ * the output schema as a special tool that must be called. The tool executes
12
+ * immediately upon invocation (validation happens via Zod schema).
13
+ *
14
+ * @param schema - A Zod schema that defines the output structure
15
+ * @returns A ToolDefinition that validates and returns the structured output
16
+ *
17
+ * @example
18
+ * ```typescript
19
+ * const outputSchema = z.object({
20
+ * title: z.string(),
21
+ * summary: z.string(),
22
+ * })
23
+ *
24
+ * const tool = createStructuredOutputTool(outputSchema)
25
+ * // Model calls: structured_output({ title: "...", summary: "..." })
26
+ * // Tool returns: { success: true, output: JSON.stringify({...}) }
27
+ * ```
28
+ */
29
+ export function createStructuredOutputTool<TSchema extends z.ZodType>(
30
+ schema: TSchema,
31
+ ): ReturnType<typeof defineTool<TSchema>> {
32
+ type TInput = z.infer<TSchema>
33
+
34
+ return defineTool({
35
+ name: STRUCTURED_OUTPUT_TOOL_NAME,
36
+ description:
37
+ 'Use this tool to provide your final structured response. You MUST call this tool with your answer.',
38
+ inputSchema: schema,
39
+ category: 'analysis',
40
+ permissions: [],
41
+ readOnly: true,
42
+ destructive: false,
43
+ concurrencySafe: true,
44
+
45
+ async execute(input: TInput, _context: ToolContext): Promise<ToolResult> {
46
+ // Input is already validated by Zod schema before this function is called.
47
+ // Simply return the validated output.
48
+ return {
49
+ success: true,
50
+ output: JSON.stringify(input),
51
+ data: input,
52
+ }
53
+ },
54
+ })
55
+ }
@@ -1,7 +1,6 @@
1
1
  import { z } from 'zod'
2
2
  import type { RunId } from '../../types/ids/index.js'
3
- import type { TaskStore } from '../../types/task/index.js'
4
- import { isTerminalTaskStatus } from '../../types/task/index.js'
3
+ import { type TaskStore, isTerminalTaskStatus } from '../../types/task/index.js'
5
4
  import type { ToolDefinition } from '../../types/tool/index.js'
6
5
  import { defineTool } from '../defineTool.js'
7
6
 
@@ -1,8 +1,9 @@
1
1
  import type { AgentStatus, CostInfo, TokenUsage } from '../common/index.js'
2
2
  import type { RunId, ThreadId } from '../ids/index.js'
3
+ import type { InvocationState } from '../invocation/index.js'
3
4
  import type { Message } from '../message/index.js'
4
5
  import type { PermissionMode } from '../permission/index.js'
5
- import type { StopReason } from '../run/index.js'
6
+ import type { StopReason } from '../run/stop-reason.js'
6
7
  import type { TaskStore } from '../task/index.js'
7
8
  import type { ToolAvailability } from '../tool/index.js'
8
9
 
@@ -28,6 +29,9 @@ export interface BaseAgentConfig {
28
29
  depth?: number
29
30
 
30
31
  contextLevel?: AgentContextLevel
32
+
33
+ /** Shared invocation state passed through agent hierarchies */
34
+ invocationState?: InvocationState
31
35
  }
32
36
 
33
37
  export type RuntimeToolOverrides = Record<string, ToolAvailability | 'disabled'>
@@ -8,3 +8,4 @@ export * from './factory.js'
8
8
  export * from './gateway.js'
9
9
  export * from './task.js'
10
10
  export * from './lifecycle-event.js'
11
+ export * from './manager.js'
@@ -0,0 +1,36 @@
1
+ import type { RunId, TaskId } from '../ids/index.js'
2
+ import type { Message } from '../message/index.js'
3
+ import type { RunEventListener } from '../run/events.js'
4
+ import type { AgentLifecycleListener } from './lifecycle-event.js'
5
+ import type { AgentTask, AgentTaskContext, AgentTaskState, SendMessageOptions } from './task.js'
6
+
7
+ /**
8
+ * Agent task lifecycle contract — task creation, cancellation, messaging, and completion tracking.
9
+ * Concrete implementation: `AgentManager` in `manager/agent/lifecycle.ts`.
10
+ */
11
+ export interface AgentManagerContract {
12
+ sendMessage(
13
+ options: SendMessageOptions,
14
+ context: AgentTaskContext,
15
+ listener?: RunEventListener,
16
+ ): Promise<AgentTask>
17
+
18
+ cancel(taskId: TaskId): void
19
+ cancelAll(parentRunId: RunId): void
20
+
21
+ continueTask(taskId: TaskId, message: string): Promise<void>
22
+ queueMessage(taskId: TaskId, message: Message): void
23
+ drainMessages(taskId: TaskId): Message[]
24
+
25
+ waitForCompletion(taskId: TaskId): Promise<void>
26
+ getInstance(taskId: TaskId): AgentTask | undefined
27
+ listByParent(parentRunId: RunId): AgentTask[]
28
+ listActive(): AgentTask[]
29
+ getState(taskId: TaskId): AgentTaskState | undefined
30
+
31
+ on(listener: AgentLifecycleListener): void
32
+ off(listener: AgentLifecycleListener): void
33
+
34
+ cleanup(): void
35
+ dispose(): void
36
+ }
@@ -1,8 +1,8 @@
1
- import type { ToolRegistry } from '../../registry/tool/execute.js'
2
1
  import type { AdvisoryConfig } from '../advisory/index.js'
3
2
  import type { AgentPersona } from '../persona/index.js'
4
3
  import type { LLMProvider } from '../provider/index.js'
5
4
  import type { Skill } from '../skills/index.js'
5
+ import type { ToolRegistryContract } from '../tool/index.js'
6
6
  import type { BaseAgentConfig, BaseAgentResult } from './base.js'
7
7
 
8
8
  export interface ReactiveAgentConfig extends BaseAgentConfig {
@@ -14,7 +14,7 @@ export interface ReactiveAgentConfig extends BaseAgentConfig {
14
14
 
15
15
  basePrompt?: string
16
16
  provider: LLMProvider
17
- tools: ToolRegistry
17
+ tools: ToolRegistryContract
18
18
 
19
19
  advisory?: AdvisoryConfig
20
20
  }
@@ -1,10 +1,10 @@
1
- import type { AgentManager } from '../../manager/agent/lifecycle.js'
2
1
  import type { AdvisoryConfig } from '../advisory/index.js'
3
2
  import type { LLMProvider } from '../provider/index.js'
4
3
  import type { TaskRouterConfig } from '../router/index.js'
5
4
  import type { BaseAgentConfig, BaseAgentResult } from './base.js'
6
5
  import type { AgentFactoryOptions } from './factory.js'
7
6
  import type { TaskGateway } from './gateway.js'
7
+ import type { AgentManagerContract } from './manager.js'
8
8
 
9
9
  export interface SupervisorAgentConfig extends BaseAgentConfig {
10
10
  provider: LLMProvider
@@ -12,7 +12,7 @@ export interface SupervisorAgentConfig extends BaseAgentConfig {
12
12
  agentIds: string[]
13
13
 
14
14
  gateway?: TaskGateway
15
- agentManager?: AgentManager
15
+ agentManager?: AgentManagerContract
16
16
 
17
17
  systemPrompt: string
18
18