kernl 0.2.0 → 0.6.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 (267) hide show
  1. package/.turbo/turbo-build.log +4 -5
  2. package/.turbo/turbo-check-types.log +4 -0
  3. package/CHANGELOG.md +147 -0
  4. package/LICENSE +1 -1
  5. package/dist/agent/__tests__/concurrency.test.d.ts +2 -0
  6. package/dist/agent/__tests__/concurrency.test.d.ts.map +1 -0
  7. package/dist/agent/__tests__/concurrency.test.js +152 -0
  8. package/dist/agent/__tests__/run.test.d.ts +2 -0
  9. package/dist/agent/__tests__/run.test.d.ts.map +1 -0
  10. package/dist/agent/__tests__/run.test.js +357 -0
  11. package/dist/agent/index.d.ts +1 -0
  12. package/dist/agent/index.d.ts.map +1 -0
  13. package/dist/agent.d.ts +32 -9
  14. package/dist/agent.d.ts.map +1 -1
  15. package/dist/agent.js +102 -14
  16. package/dist/api/__tests__/cursor-page.test.d.ts +2 -0
  17. package/dist/api/__tests__/cursor-page.test.d.ts.map +1 -0
  18. package/dist/api/__tests__/cursor-page.test.js +414 -0
  19. package/dist/api/__tests__/offset-page.test.d.ts +2 -0
  20. package/dist/api/__tests__/offset-page.test.d.ts.map +1 -0
  21. package/dist/api/__tests__/offset-page.test.js +510 -0
  22. package/dist/api/__tests__/threads.test.d.ts +2 -0
  23. package/dist/api/__tests__/threads.test.d.ts.map +1 -0
  24. package/dist/api/__tests__/threads.test.js +338 -0
  25. package/dist/api/models/index.d.ts +2 -0
  26. package/dist/api/models/index.d.ts.map +1 -0
  27. package/dist/api/models/thread.d.ts +120 -0
  28. package/dist/api/models/thread.d.ts.map +1 -0
  29. package/dist/api/pagination/base.d.ts +48 -0
  30. package/dist/api/pagination/base.d.ts.map +1 -0
  31. package/dist/api/pagination/base.js +45 -0
  32. package/dist/api/pagination/cursor.d.ts +44 -0
  33. package/dist/api/pagination/cursor.d.ts.map +1 -0
  34. package/dist/api/pagination/cursor.js +52 -0
  35. package/dist/api/pagination/offset.d.ts +42 -0
  36. package/dist/api/pagination/offset.d.ts.map +1 -0
  37. package/dist/api/pagination/offset.js +55 -0
  38. package/dist/api/resources/threads/events.d.ts +21 -0
  39. package/dist/api/resources/threads/events.d.ts.map +1 -0
  40. package/dist/api/resources/threads/events.js +24 -0
  41. package/dist/api/resources/threads/index.d.ts +4 -0
  42. package/dist/api/resources/threads/index.d.ts.map +1 -0
  43. package/dist/api/resources/threads/index.js +2 -0
  44. package/dist/api/resources/threads/threads.d.ts +57 -0
  45. package/dist/api/resources/threads/threads.d.ts.map +1 -0
  46. package/dist/api/resources/threads/threads.js +199 -0
  47. package/dist/api/resources/threads/types.d.ts +123 -0
  48. package/dist/api/resources/threads/types.d.ts.map +1 -0
  49. package/dist/api/resources/threads/utils.d.ts +18 -0
  50. package/dist/api/resources/threads/utils.d.ts.map +1 -0
  51. package/dist/api/resources/threads/utils.js +78 -0
  52. package/dist/context.d.ts +5 -1
  53. package/dist/context.d.ts.map +1 -1
  54. package/dist/context.js +6 -1
  55. package/dist/index.d.ts +9 -1
  56. package/dist/index.d.ts.map +1 -1
  57. package/dist/index.js +7 -0
  58. package/dist/internal.d.ts +4 -0
  59. package/dist/internal.d.ts.map +1 -0
  60. package/dist/internal.js +2 -0
  61. package/dist/kernl/index.d.ts +3 -0
  62. package/dist/kernl/index.d.ts.map +1 -0
  63. package/dist/kernl/index.js +2 -0
  64. package/dist/kernl/kernl.d.ts +64 -0
  65. package/dist/kernl/kernl.d.ts.map +1 -0
  66. package/dist/kernl/kernl.js +116 -0
  67. package/dist/kernl/threads.d.ts +110 -0
  68. package/dist/kernl/threads.d.ts.map +1 -0
  69. package/dist/kernl/threads.js +126 -0
  70. package/dist/kernl.d.ts +22 -6
  71. package/dist/kernl.d.ts.map +1 -1
  72. package/dist/kernl.js +73 -10
  73. package/dist/lib/env.d.ts +3 -3
  74. package/dist/lib/env.js +1 -1
  75. package/dist/mcp/__tests__/integration.test.js +8 -8
  76. package/dist/mcp/__tests__/utils.test.js +6 -6
  77. package/dist/mcp/http.d.ts +1 -1
  78. package/dist/mcp/http.d.ts.map +1 -1
  79. package/dist/mcp/http.js +9 -9
  80. package/dist/mcp/sse.d.ts +1 -1
  81. package/dist/mcp/sse.d.ts.map +1 -1
  82. package/dist/mcp/sse.js +7 -7
  83. package/dist/mcp/utils.d.ts +1 -1
  84. package/dist/mcp/utils.d.ts.map +1 -1
  85. package/dist/mcp/utils.js +4 -5
  86. package/dist/storage/__tests__/in-memory.test.d.ts +2 -0
  87. package/dist/storage/__tests__/in-memory.test.d.ts.map +1 -0
  88. package/dist/storage/__tests__/in-memory.test.js +455 -0
  89. package/dist/storage/base.d.ts +64 -0
  90. package/dist/storage/base.d.ts.map +1 -0
  91. package/dist/storage/base.js +4 -0
  92. package/dist/storage/in-memory.d.ts +62 -0
  93. package/dist/storage/in-memory.d.ts.map +1 -0
  94. package/dist/storage/in-memory.js +283 -0
  95. package/dist/storage/index.d.ts +10 -0
  96. package/dist/storage/index.d.ts.map +1 -0
  97. package/dist/storage/index.js +7 -0
  98. package/dist/storage/thread.d.ts +123 -0
  99. package/dist/storage/thread.d.ts.map +1 -0
  100. package/dist/storage/thread.js +4 -0
  101. package/dist/task.d.ts +5 -3
  102. package/dist/task.d.ts.map +1 -1
  103. package/dist/task.js +10 -8
  104. package/dist/thread/__tests__/fixtures/mock-model.d.ts +1 -2
  105. package/dist/thread/__tests__/fixtures/mock-model.d.ts.map +1 -1
  106. package/dist/thread/__tests__/integration.test.js +73 -5
  107. package/dist/thread/__tests__/namespace.test.d.ts +2 -0
  108. package/dist/thread/__tests__/namespace.test.d.ts.map +1 -0
  109. package/dist/thread/__tests__/namespace.test.js +131 -0
  110. package/dist/thread/__tests__/thread-persistence.test.d.ts +2 -0
  111. package/dist/thread/__tests__/thread-persistence.test.d.ts.map +1 -0
  112. package/dist/thread/__tests__/thread-persistence.test.js +351 -0
  113. package/dist/thread/__tests__/thread.test.js +49 -51
  114. package/dist/thread/thread.d.ts +70 -18
  115. package/dist/thread/thread.d.ts.map +1 -1
  116. package/dist/thread/thread.js +211 -73
  117. package/dist/thread/utils.d.ts +36 -8
  118. package/dist/thread/utils.d.ts.map +1 -1
  119. package/dist/thread/utils.js +52 -8
  120. package/dist/tool/__tests__/fixtures.js +1 -1
  121. package/dist/tool/__tests__/toolkit.test.js +15 -12
  122. package/dist/tool/tool.js +3 -3
  123. package/dist/types/kernl.d.ts +42 -0
  124. package/dist/types/kernl.d.ts.map +1 -0
  125. package/dist/types/thread.d.ts +108 -22
  126. package/dist/types/thread.d.ts.map +1 -1
  127. package/dist/types/thread.js +12 -0
  128. package/package.json +11 -7
  129. package/src/agent/__tests__/concurrency.test.ts +194 -0
  130. package/src/agent/__tests__/run.test.ts +441 -0
  131. package/src/agent/index.ts +0 -0
  132. package/src/agent.ts +141 -24
  133. package/src/api/__tests__/cursor-page.test.ts +512 -0
  134. package/src/api/__tests__/offset-page.test.ts +624 -0
  135. package/src/api/__tests__/threads.test.ts +415 -0
  136. package/src/api/models/index.ts +6 -0
  137. package/src/api/models/thread.ts +138 -0
  138. package/src/api/pagination/base.ts +79 -0
  139. package/src/api/pagination/cursor.ts +86 -0
  140. package/src/api/pagination/offset.ts +89 -0
  141. package/src/api/resources/threads/events.ts +26 -0
  142. package/src/api/resources/threads/index.ts +9 -0
  143. package/src/api/resources/threads/threads.ts +256 -0
  144. package/src/api/resources/threads/types.ts +143 -0
  145. package/src/api/resources/threads/utils.ts +104 -0
  146. package/src/context.ts +10 -1
  147. package/src/index.ts +49 -1
  148. package/src/internal.ts +15 -0
  149. package/src/kernl.ts +86 -17
  150. package/src/mcp/__tests__/integration.test.ts +8 -9
  151. package/src/mcp/__tests__/utils.test.ts +6 -6
  152. package/src/mcp/http.ts +9 -9
  153. package/src/mcp/sse.ts +7 -7
  154. package/src/mcp/utils.ts +6 -5
  155. package/src/storage/__tests__/in-memory.test.ts +534 -0
  156. package/src/storage/base.ts +77 -0
  157. package/src/storage/in-memory.ts +372 -0
  158. package/src/storage/index.ts +21 -0
  159. package/src/storage/thread.ts +141 -0
  160. package/src/task.ts +12 -10
  161. package/src/thread/__tests__/fixtures/mock-model.ts +2 -4
  162. package/src/thread/__tests__/integration.test.ts +111 -10
  163. package/src/thread/__tests__/namespace.test.ts +158 -0
  164. package/src/thread/__tests__/thread-persistence.test.ts +367 -0
  165. package/src/thread/__tests__/thread.test.ts +52 -54
  166. package/src/thread/thread.ts +247 -96
  167. package/src/thread/utils.ts +76 -13
  168. package/src/tool/__tests__/fixtures.ts +1 -1
  169. package/src/tool/__tests__/toolkit.test.ts +15 -12
  170. package/src/tool/tool.ts +3 -3
  171. package/src/types/kernl.ts +51 -0
  172. package/src/types/thread.ts +139 -25
  173. package/vitest.config.ts +1 -0
  174. package/dist/env.d.ts +0 -45
  175. package/dist/env.d.ts.map +0 -1
  176. package/dist/env.js +0 -31
  177. package/dist/error.d.ts +0 -1
  178. package/dist/error.d.ts.map +0 -1
  179. package/dist/kernel.d.ts +0 -7
  180. package/dist/kernel.d.ts.map +0 -1
  181. package/dist/kernel.js +0 -7
  182. package/dist/lib/serde/__tests__/codec.test.d.ts +0 -2
  183. package/dist/lib/serde/__tests__/codec.test.d.ts.map +0 -1
  184. package/dist/lib/serde/__tests__/codec.test.js +0 -75
  185. package/dist/lib/serde/codec.d.ts +0 -12
  186. package/dist/lib/serde/codec.d.ts.map +0 -1
  187. package/dist/lib/serde/codec.js +0 -54
  188. package/dist/lib/serde/thread.d.ts +0 -1
  189. package/dist/lib/serde/thread.d.ts.map +0 -1
  190. package/dist/lib/serde/thread.js +0 -172
  191. package/dist/lib/serde/tool.d.ts +0 -36
  192. package/dist/lib/serde/tool.d.ts.map +0 -1
  193. package/dist/lib/utils.d.ts +0 -19
  194. package/dist/lib/utils.d.ts.map +0 -1
  195. package/dist/lib/utils.js +0 -41
  196. package/dist/logger.d.ts +0 -36
  197. package/dist/logger.d.ts.map +0 -1
  198. package/dist/logger.js +0 -43
  199. package/dist/mcp/__tests__/fixtures/echo-server.d.ts +0 -3
  200. package/dist/mcp/__tests__/fixtures/echo-server.d.ts.map +0 -1
  201. package/dist/mcp/__tests__/fixtures/echo-server.js +0 -92
  202. package/dist/mcp/__tests__/fixtures/math-server.d.ts +0 -3
  203. package/dist/mcp/__tests__/fixtures/math-server.d.ts.map +0 -1
  204. package/dist/mcp/__tests__/fixtures/math-server.js +0 -98
  205. package/dist/mcp/__tests__/fixtures/test-server.d.ts +0 -3
  206. package/dist/mcp/__tests__/fixtures/test-server.d.ts.map +0 -1
  207. package/dist/mcp/__tests__/fixtures/test-server.js +0 -163
  208. package/dist/mcp/__tests__/test-utils.d.ts +0 -17
  209. package/dist/mcp/__tests__/test-utils.d.ts.map +0 -1
  210. package/dist/mcp/__tests__/test-utils.js +0 -42
  211. package/dist/mcp/node.d.ts +0 -60
  212. package/dist/mcp/node.d.ts.map +0 -1
  213. package/dist/mcp/node.js +0 -297
  214. package/dist/model.d.ts +0 -175
  215. package/dist/model.d.ts.map +0 -1
  216. package/dist/providers/ai.d.ts +0 -1
  217. package/dist/providers/ai.d.ts.map +0 -1
  218. package/dist/providers/ai.js +0 -1
  219. package/dist/providers/default.d.ts +0 -16
  220. package/dist/providers/default.d.ts.map +0 -1
  221. package/dist/providers/default.js +0 -17
  222. package/dist/providers/registry.d.ts +0 -1
  223. package/dist/providers/registry.d.ts.map +0 -1
  224. package/dist/providers/registry.js +0 -1
  225. package/dist/sched/scheduler.d.ts +0 -20
  226. package/dist/sched/scheduler.d.ts.map +0 -1
  227. package/dist/sched/task.d.ts +0 -92
  228. package/dist/sched/task.d.ts.map +0 -1
  229. package/dist/sched/task.js +0 -102
  230. package/dist/serde/__tests__/codec.test.d.ts +0 -2
  231. package/dist/serde/__tests__/codec.test.d.ts.map +0 -1
  232. package/dist/serde/__tests__/codec.test.js +0 -75
  233. package/dist/serde/codec.d.ts +0 -12
  234. package/dist/serde/codec.d.ts.map +0 -1
  235. package/dist/serde/codec.js +0 -54
  236. package/dist/serde/json.d.ts +0 -8
  237. package/dist/serde/json.d.ts.map +0 -1
  238. package/dist/serde/json.js +0 -13
  239. package/dist/serde/thread.d.ts +0 -687
  240. package/dist/serde/thread.d.ts.map +0 -1
  241. package/dist/serde/thread.js +0 -158
  242. package/dist/serde/tool.d.ts +0 -36
  243. package/dist/serde/tool.d.ts.map +0 -1
  244. package/dist/session.d.ts +0 -1
  245. package/dist/session.d.ts.map +0 -1
  246. package/dist/session.js +0 -1
  247. package/dist/thread/__tests__/stream.test.d.ts +0 -2
  248. package/dist/thread/__tests__/stream.test.d.ts.map +0 -1
  249. package/dist/thread/__tests__/stream.test.js +0 -244
  250. package/dist/tool/mcp.d.ts +0 -75
  251. package/dist/tool/mcp.d.ts.map +0 -1
  252. package/dist/tool/mcp.js +0 -111
  253. package/dist/tools.d.ts +0 -362
  254. package/dist/tools.d.ts.map +0 -1
  255. package/dist/tools.js +0 -220
  256. package/dist/types/proto.d.ts +0 -1551
  257. package/dist/types/proto.d.ts.map +0 -1
  258. package/dist/types/proto.js +0 -531
  259. package/dist/usage.d.ts +0 -43
  260. package/dist/usage.d.ts.map +0 -1
  261. package/dist/usage.js +0 -61
  262. package/src/lib/serde/thread.ts +0 -188
  263. /package/dist/{error.js → agent/index.js} +0 -0
  264. /package/dist/{lib/serde/tool.js → api/models/index.js} +0 -0
  265. /package/dist/{model.js → api/models/thread.js} +0 -0
  266. /package/dist/{sched/scheduler.js → api/resources/threads/types.js} +0 -0
  267. /package/dist/{serde/tool.js → types/kernl.js} +0 -0
@@ -1 +0,0 @@
1
- {"version":3,"file":"thread.d.ts","sourceRoot":"","sources":["../../src/serde/thread.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAIxB;;;;GAIG;AAEH;;;;GAIG;AACH,eAAO,MAAM,sBAAsB,EAAG,KAAc,CAAC;AAQrD,QAAA,MAAM,kBAAkB;;;;;;;;;;;;iBActB,CAAC;AAEH,KAAK,kBAAkB,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,kBAAkB,CAAC,GAAG;IAC7D,aAAa,CAAC,EAAE,kBAAkB,CAAC;CACpC,CAAC;AAkHF,eAAO,MAAM,gBAAgB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBA8B3B,CAAC;AAEH,MAAM,MAAM,gBAAgB,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,gBAAgB,CAAC,CAAC"}
@@ -1,158 +0,0 @@
1
- import { z } from "zod";
2
- import * as events from "../types/thread";
3
- /**
4
- * Utilities and schemas for serializing the run-state so that it can be paused / resumed later.
5
- *
6
- * (TODO): should be able to define this as a codec for the ThreadState schema
7
- */
8
- /**
9
- * The schema version of the serialized run state. This is used to ensure that the serialized
10
- * run state is compatible with the current version of the SDK.
11
- * If anything in this schema changes, the version will have to be incremented.
12
- */
13
- export const CURRENT_SCHEMA_VERSION = "1.0";
14
- const $schemaVersion = z.literal(CURRENT_SCHEMA_VERSION);
15
- const serializedAgentSchema = z.object({
16
- id: z.string(),
17
- name: z.string().optional(),
18
- });
19
- const serializedSpanBase = z.object({
20
- object: z.literal("trace.span"),
21
- id: z.string(),
22
- trace_id: z.string(),
23
- parent_id: z.string().nullable(),
24
- started_at: z.string().nullable(),
25
- ended_at: z.string().nullable(),
26
- error: z
27
- .object({
28
- message: z.string(),
29
- data: z.record(z.string(), z.any()).optional(),
30
- })
31
- .nullable(),
32
- span_data: z.record(z.string(), z.any()),
33
- });
34
- const SerializedSpan = serializedSpanBase.extend({
35
- previous_span: z.lazy(() => SerializedSpan).optional(),
36
- });
37
- const usageSchema = z.object({
38
- requests: z.number(),
39
- inputTokens: z.number(),
40
- outputTokens: z.number(),
41
- totalTokens: z.number(),
42
- });
43
- const modelResponseSchema = z.object({
44
- usage: usageSchema,
45
- events: z.array(events.ThreadEvent),
46
- responseId: z.string().optional(),
47
- providerData: z.record(z.string(), z.any()).optional(),
48
- });
49
- const itemSchema = z.discriminatedUnion("kind", [
50
- z.object({
51
- kind: z.literal("message"),
52
- rawItem: events.AssistantMessage,
53
- agent: serializedAgentSchema,
54
- }),
55
- z.object({
56
- kind: z.literal("tool-call"),
57
- // rawItem: events.ToolCall.or(events.HostedToolCall),
58
- rawItem: events.ToolCall,
59
- agent: serializedAgentSchema,
60
- }),
61
- z.object({
62
- kind: z.literal("tool-result"),
63
- rawItem: events.ToolResultEvent,
64
- agent: serializedAgentSchema,
65
- output: z.string(),
66
- }),
67
- z.object({
68
- kind: z.literal("reasoning"),
69
- rawItem: events.Reasoning,
70
- agent: serializedAgentSchema,
71
- }),
72
- ]);
73
- const serializedTraceSchema = z.object({
74
- object: z.literal("trace"),
75
- id: z.string(),
76
- workflow_name: z.string(),
77
- group_id: z.string().nullable(),
78
- metadata: z.record(z.string(), z.any()),
79
- });
80
- const serializedProcessedResponseSchema = z.object({
81
- newItems: z.array(itemSchema),
82
- toolsUsed: z.array(z.string()),
83
- functions: z.array(z.object({
84
- toolCall: z.any(),
85
- tool: z.any(),
86
- })),
87
- mcpApprovalRequests: z
88
- .array(z.object({
89
- requestItem: z.object({
90
- // protocol.HostedToolCallItem
91
- rawItem: z.object({
92
- type: z.literal("hosted_tool_call"),
93
- name: z.string(),
94
- arguments: z.string().optional(),
95
- status: z.string().optional(),
96
- output: z.string().optional(),
97
- // this always exists but marked as optional for early version compatibility; when releasing 1.0, we can remove the nullable and optional
98
- providerData: z.record(z.string(), z.any()).nullable().optional(),
99
- }),
100
- }),
101
- // HostedMCPTool
102
- mcpTool: z.object({
103
- type: z.literal("hosted_tool"),
104
- name: z.literal("hosted_mcp"),
105
- providerData: z.record(z.string(), z.any()),
106
- }),
107
- }))
108
- .optional(),
109
- });
110
- const guardrailFunctionOutputSchema = z.object({
111
- tripwireTriggered: z.boolean(),
112
- outputInfo: z.any(),
113
- });
114
- const inputGuardrailResultSchema = z.object({
115
- guardrail: z.object({
116
- type: z.literal("input"),
117
- name: z.string(),
118
- }),
119
- output: guardrailFunctionOutputSchema,
120
- });
121
- const outputGuardrailResultSchema = z.object({
122
- guardrail: z.object({
123
- type: z.literal("output"),
124
- name: z.string(),
125
- }),
126
- agentOutput: z.any(),
127
- agent: serializedAgentSchema,
128
- output: guardrailFunctionOutputSchema,
129
- });
130
- // (TODO): define z.codec
131
- export const SerializedThread = z.object({
132
- $schemaVersion,
133
- currentTurn: z.number(),
134
- currentAgent: serializedAgentSchema, // (TODO): in our case we probably don't need to serialize the whole agent - an ID would suffice
135
- originalInput: z.string().or(z.array(events.ThreadEvent)),
136
- modelResponses: z.array(modelResponseSchema),
137
- context: z.object({
138
- usage: usageSchema, // (TODO): move to stats
139
- // (TODO): belongs elsewhere
140
- approvals: z.record(z.string(), z.object({
141
- approved: z.array(z.string()).or(z.boolean()),
142
- rejected: z.array(z.string()).or(z.boolean()),
143
- })),
144
- context: z.record(z.string(), z.any()),
145
- }),
146
- toolUseTracker: z.record(z.string(), z.array(z.string())),
147
- maxTurns: z.number(),
148
- currentAgentSpan: SerializedSpan.nullable().optional(),
149
- noActiveAgentRun: z.boolean(),
150
- inputGuardrailResults: z.array(inputGuardrailResultSchema),
151
- outputGuardrailResults: z.array(outputGuardrailResultSchema),
152
- // (TODO): currentStep: nextStepSchema.optional(),
153
- lastModelResponse: modelResponseSchema.optional(),
154
- generatedItems: z.array(itemSchema),
155
- lastProcessedResponse: serializedProcessedResponseSchema.optional(),
156
- currentTurnPersistedItemCount: z.number().int().min(0).optional(),
157
- trace: serializedTraceSchema.nullable(),
158
- });
@@ -1,36 +0,0 @@
1
- import type { ToolInputParameters } from "../tool/types";
2
- /**
3
- * Serialized function tool for model requests
4
- */
5
- export type SerializedFunctionTool = {
6
- /**
7
- * The type of the tool.
8
- */
9
- type: "function";
10
- /**
11
- * The name of the tool.
12
- */
13
- name?: string;
14
- /**
15
- * The description of the tool that helps the model to understand when to use the tool
16
- */
17
- description: string;
18
- /**
19
- * A JSON schema describing the parameters of the tool.
20
- */
21
- parameters: ToolInputParameters;
22
- };
23
- /**
24
- * Serialized hosted tool for model requests
25
- */
26
- export type SerializedHostedTool = {
27
- type: "hosted-tool";
28
- id: string;
29
- name?: string;
30
- providerData?: Record<string, any>;
31
- };
32
- /**
33
- * Union of all serialized tool types
34
- */
35
- export type SerializedTool = SerializedFunctionTool | SerializedHostedTool;
36
- //# sourceMappingURL=tool.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"tool.d.ts","sourceRoot":"","sources":["../../src/serde/tool.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,cAAc,CAAC;AAExD;;GAEG;AACH,MAAM,MAAM,sBAAsB,GAAG;IACnC;;OAEG;IACH,IAAI,EAAE,UAAU,CAAC;IAEjB;;OAEG;IACH,IAAI,CAAC,EAAE,MAAM,CAAC;IAEd;;OAEG;IACH,WAAW,EAAE,MAAM,CAAC;IAEpB;;OAEG;IACH,UAAU,EAAE,mBAAmB,CAAC;CACjC,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,oBAAoB,GAAG;IACjC,IAAI,EAAE,aAAa,CAAC;IACpB,EAAE,EAAE,MAAM,CAAC;IACX,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,YAAY,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;CACpC,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,cAAc,GAAG,sBAAsB,GAAG,oBAAoB,CAAC"}
package/dist/session.d.ts DELETED
@@ -1 +0,0 @@
1
- //# sourceMappingURL=session.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"session.d.ts","sourceRoot":"","sources":["../src/session.ts"],"names":[],"mappings":""}
package/dist/session.js DELETED
@@ -1 +0,0 @@
1
- "use strict";
@@ -1,2 +0,0 @@
1
- export {};
2
- //# sourceMappingURL=stream.test.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"stream.test.d.ts","sourceRoot":"","sources":["../../../src/thread/__tests__/stream.test.ts"],"names":[],"mappings":""}
@@ -1,244 +0,0 @@
1
- import { describe, it, expect, beforeAll } from "vitest";
2
- import { z } from "zod";
3
- import { openai } from "@ai-sdk/openai";
4
- import { AISDKLanguageModel } from "@kernl-sdk/ai";
5
- import { Agent } from "../../agent";
6
- import { Kernl } from "../../kernl";
7
- import { tool, Toolkit } from "../../tool";
8
- import { Thread } from "../thread";
9
- /**
10
- * Integration tests for Thread streaming with real AI SDK providers.
11
- *
12
- * These tests require an OPENAI_API_KEY environment variable to be set.
13
- * They will be skipped if the API key is not available.
14
- *
15
- * Run with: OPENAI_API_KEY=your-key pnpm test:run
16
- */
17
- const SKIP_INTEGRATION_TESTS = !process.env.OPENAI_API_KEY;
18
- describe.skipIf(SKIP_INTEGRATION_TESTS)("Thread streaming integration", () => {
19
- let kernl;
20
- let model;
21
- beforeAll(() => {
22
- kernl = new Kernl();
23
- model = new AISDKLanguageModel(openai("gpt-4o-mini"));
24
- });
25
- describe("stream()", () => {
26
- it("should yield both delta events and complete items", async () => {
27
- const agent = new Agent({
28
- id: "test-stream",
29
- name: "Test Stream Agent",
30
- instructions: "You are a helpful assistant.",
31
- model,
32
- });
33
- const input = [
34
- {
35
- kind: "message",
36
- id: "msg-1",
37
- role: "user",
38
- content: [
39
- { kind: "text", text: "Say 'Hello World' and nothing else." },
40
- ],
41
- },
42
- ];
43
- const thread = new Thread(kernl, agent, input);
44
- const events = [];
45
- for await (const event of thread.stream()) {
46
- events.push(event);
47
- }
48
- expect(events.length).toBeGreaterThan(0);
49
- // Should have text-delta events (for streaming UX)
50
- const textDeltas = events.filter((e) => e.kind === "text-delta");
51
- expect(textDeltas.length).toBeGreaterThan(0);
52
- // Should have text-start event
53
- const textStarts = events.filter((e) => e.kind === "text-start");
54
- expect(textStarts.length).toBeGreaterThan(0);
55
- // Should have text-end event
56
- const textEnds = events.filter((e) => e.kind === "text-end");
57
- expect(textEnds.length).toBeGreaterThan(0);
58
- // Should have complete Message item (for history)
59
- const messages = events.filter((e) => e.kind === "message");
60
- expect(messages.length).toBeGreaterThan(0);
61
- const assistantMessage = messages.find((m) => m.role === "assistant");
62
- expect(assistantMessage).toBeDefined();
63
- expect(assistantMessage.content).toBeDefined();
64
- expect(assistantMessage.content.length).toBeGreaterThan(0);
65
- // Message should have accumulated text from all deltas
66
- const textContent = assistantMessage.content.find((c) => c.kind === "text");
67
- expect(textContent).toBeDefined();
68
- expect(textContent.text).toBeDefined();
69
- expect(textContent.text.length).toBeGreaterThan(0);
70
- // Verify accumulated text matches concatenated deltas
71
- const accumulatedFromDeltas = textDeltas.map((d) => d.text).join("");
72
- expect(textContent.text).toBe(accumulatedFromDeltas);
73
- // Should have finish event
74
- const finishEvents = events.filter((e) => e.kind === "finish");
75
- expect(finishEvents.length).toBe(1);
76
- });
77
- it("should filter deltas from history but include complete items", async () => {
78
- const agent = new Agent({
79
- id: "test-history",
80
- name: "Test History Agent",
81
- instructions: "You are a helpful assistant.",
82
- model,
83
- });
84
- const input = [
85
- {
86
- kind: "message",
87
- id: "msg-1",
88
- role: "user",
89
- content: [{ kind: "text", text: "Count to 3" }],
90
- },
91
- ];
92
- const thread = new Thread(kernl, agent, input);
93
- const streamEvents = [];
94
- for await (const event of thread.stream()) {
95
- streamEvents.push(event);
96
- }
97
- // Access private history via type assertion for testing
98
- const history = thread.history;
99
- // History should only contain complete items (message, reasoning, tool-call, tool-result)
100
- // TypeScript already enforces this via ThreadEvent type, but let's verify at runtime
101
- for (const event of history) {
102
- expect(["message", "reasoning", "tool-call", "tool-result"]).toContain(event.kind);
103
- }
104
- // Stream events should include deltas (but history should not)
105
- const streamDeltas = streamEvents.filter((e) => e.kind === "text-delta" ||
106
- e.kind === "text-start" ||
107
- e.kind === "text-end");
108
- expect(streamDeltas.length).toBeGreaterThan(0);
109
- // History should contain the input message
110
- expect(history[0]).toEqual(input[0]);
111
- // History should contain complete Message items
112
- const historyMessages = history.filter((e) => e.kind === "message");
113
- expect(historyMessages.length).toBeGreaterThan(1); // input + assistant response
114
- // Verify assistant message has complete text (not deltas)
115
- const assistantMessage = historyMessages.find((m) => m.role === "assistant");
116
- expect(assistantMessage).toBeDefined();
117
- const textContent = assistantMessage.content.find((c) => c.kind === "text");
118
- expect(textContent.text).toBeTruthy();
119
- expect(textContent.text.length).toBeGreaterThan(0);
120
- });
121
- it("should work with tool calls", async () => {
122
- const calculateTool = tool({
123
- name: "calculate",
124
- description: "Perform a mathematical calculation",
125
- parameters: z.object({
126
- expression: z
127
- .string()
128
- .describe("The mathematical expression to evaluate"),
129
- }),
130
- execute: async (ctx, { expression }) => {
131
- // Simple eval for testing (don't do this in production!)
132
- try {
133
- return { result: eval(expression) };
134
- }
135
- catch {
136
- return { error: "Invalid expression" };
137
- }
138
- },
139
- });
140
- const toolkit = new Toolkit({
141
- id: "math",
142
- tools: [calculateTool],
143
- });
144
- const agent = new Agent({
145
- id: "test-tools",
146
- name: "Test Tools Agent",
147
- instructions: "You are a helpful assistant that can do calculations.",
148
- model,
149
- toolkits: [toolkit],
150
- });
151
- const input = [
152
- {
153
- kind: "message",
154
- id: "msg-1",
155
- role: "user",
156
- content: [{ kind: "text", text: "What is 25 + 17?" }],
157
- },
158
- ];
159
- const thread = new Thread(kernl, agent, input);
160
- const events = [];
161
- for await (const event of thread.stream()) {
162
- events.push(event);
163
- }
164
- expect(events.length).toBeGreaterThan(0);
165
- // Should have tool calls
166
- const toolCalls = events.filter((e) => e.kind === "tool-call");
167
- expect(toolCalls.length).toBeGreaterThan(0);
168
- // Should have tool results
169
- const toolResults = events.filter((e) => e.kind === "tool-result");
170
- expect(toolResults.length).toBeGreaterThan(0);
171
- // History should contain tool calls and results
172
- const history = thread.history;
173
- const historyToolCalls = history.filter((e) => e.kind === "tool-call");
174
- const historyToolResults = history.filter((e) => e.kind === "tool-result");
175
- expect(historyToolCalls.length).toBe(toolCalls.length);
176
- expect(historyToolResults.length).toBe(toolResults.length);
177
- });
178
- });
179
- describe("execute()", () => {
180
- it("should consume stream and return final response", async () => {
181
- const agent = new Agent({
182
- id: "test-blocking",
183
- name: "Test Blocking Agent",
184
- instructions: "You are a helpful assistant.",
185
- model,
186
- });
187
- const input = [
188
- {
189
- kind: "message",
190
- id: "msg-1",
191
- role: "user",
192
- content: [{ kind: "text", text: "Say 'Testing' and nothing else." }],
193
- },
194
- ];
195
- const thread = new Thread(kernl, agent, input);
196
- const result = await thread.execute();
197
- // Should have a response
198
- expect(result.response).toBeDefined();
199
- expect(typeof result.response).toBe("string");
200
- expect(result.response.length).toBeGreaterThan(0);
201
- // Should have final state
202
- expect(result.state).toBe("stopped");
203
- // History should NOT contain deltas
204
- const history = thread.history;
205
- const historyDeltas = history.filter((e) => e.kind === "text-delta" ||
206
- e.kind === "text-start" ||
207
- e.kind === "text-end");
208
- expect(historyDeltas.length).toBe(0);
209
- });
210
- it("should validate structured output in blocking mode", async () => {
211
- const responseSchema = z.object({
212
- name: z.string(),
213
- age: z.number(),
214
- });
215
- const agent = new Agent({
216
- id: "test-structured",
217
- name: "Test Structured Agent",
218
- instructions: "You are a helpful assistant. Return JSON with name and age fields.",
219
- model,
220
- responseType: responseSchema,
221
- });
222
- const input = [
223
- {
224
- kind: "message",
225
- id: "msg-1",
226
- role: "user",
227
- content: [
228
- {
229
- kind: "text",
230
- text: 'Return a JSON object with name "Alice" and age 30',
231
- },
232
- ],
233
- },
234
- ];
235
- const thread = new Thread(kernl, agent, input);
236
- const result = await thread.execute();
237
- // Response should be validated and parsed
238
- expect(result.response).toBeDefined();
239
- expect(typeof result.response).toBe("object");
240
- expect(result.response.name).toBeTruthy();
241
- expect(typeof result.response.age).toBe("number");
242
- });
243
- });
244
- });
@@ -1,75 +0,0 @@
1
- import type { Context, UnknownContext } from "../context";
2
- import { Toolkit } from "./toolkit";
3
- import type { Tool } from ".";
4
- import type { MCPToolkitConfig } from "./types";
5
- /**
6
- * A toolkit that wraps an MCP server and provides tools from it.
7
- *
8
- * Handles connection lifecycle automatically - connects lazily on first tool request
9
- * and provides cleanup via destroy().
10
- *
11
- * @example
12
- * ```ts
13
- * const server = new MCPServerStdio({
14
- * id: "github",
15
- * command: "npx",
16
- * args: ["-y", "@modelcontextprotocol/server-github"],
17
- * env: {
18
- * GITHUB_TOKEN: process.env.GITHUB_TOKEN,
19
- * },
20
- * });
21
- *
22
- * const github = new MCPToolkit({
23
- * id: "github",
24
- * server,
25
- * filter: async (ctx, tool) => {
26
- * // Only allow certain tools
27
- * return !tool.id.startsWith("dangerous_");
28
- * },
29
- * });
30
- *
31
- * const agent = new Agent({
32
- * toolkits: [github],
33
- * });
34
- * ```
35
- */
36
- export declare class MCPToolkit<TContext = UnknownContext> extends Toolkit<TContext> {
37
- readonly id: string;
38
- private server;
39
- private cache;
40
- private filter;
41
- private connected;
42
- private cached;
43
- /**
44
- * Create a new MCP toolkit.
45
- *
46
- * @param config Toolkit configuration with id and server instance
47
- */
48
- constructor(config: MCPToolkitConfig<TContext>);
49
- /**
50
- * Get a specific tool by ID.
51
- *
52
- * Returns the tool from the local cache. The cache is populated on the first
53
- * call to list(). Returns undefined if list() hasn't been called yet.
54
- *
55
- * @param id The tool ID to look up
56
- * @returns The tool if found in cache, undefined otherwise
57
- */
58
- get(id: string): Tool<TContext> | undefined;
59
- /**
60
- * List all tools available from the MCP server.
61
- *
62
- * Connects to the server lazily on first call. Tools are cached locally after
63
- * the first fetch. The MCP server itself also handles caching via the
64
- * cacheToolsList option, so the network call is only made once.
65
- *
66
- * @param context Optional context for filtering tools
67
- * @returns Array of tools from the MCP server
68
- */
69
- list(context?: Context<TContext>): Promise<Tool<TContext>[]>;
70
- /**
71
- * Cleanup resources and close the MCP server connection.
72
- */
73
- destroy(): Promise<void>;
74
- }
75
- //# sourceMappingURL=mcp.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"mcp.d.ts","sourceRoot":"","sources":["../../src/tool/mcp.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,cAAc,EAAE,MAAM,WAAW,CAAC;AAOzD,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AACpC,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,GAAG,CAAC;AAC9B,OAAO,KAAK,EAAE,gBAAgB,EAAiB,MAAM,SAAS,CAAC;AAE/D;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8BG;AACH,qBAAa,UAAU,CAAC,QAAQ,GAAG,cAAc,CAAE,SAAQ,OAAO,CAAC,QAAQ,CAAC;IAC1E,QAAQ,CAAC,EAAE,EAAE,MAAM,CAAC;IACpB,OAAO,CAAC,MAAM,CAAY;IAC1B,OAAO,CAAC,KAAK,CAA8B;IAC3C,OAAO,CAAC,MAAM,CAA0B;IAExC,OAAO,CAAC,SAAS,CAAS;IAC1B,OAAO,CAAC,MAAM,CAAS;IAEvB;;;;OAIG;gBACS,MAAM,EAAE,gBAAgB,CAAC,QAAQ,CAAC;IAQ9C;;;;;;;;OAQG;IACH,GAAG,CAAC,EAAE,EAAE,MAAM,GAAG,IAAI,CAAC,QAAQ,CAAC,GAAG,SAAS;IAI3C;;;;;;;;;OASG;IACG,IAAI,CAAC,OAAO,CAAC,EAAE,OAAO,CAAC,QAAQ,CAAC,GAAG,OAAO,CAAC,IAAI,CAAC,QAAQ,CAAC,EAAE,CAAC;IA+BlE;;OAEG;IACG,OAAO,IAAI,OAAO,CAAC,IAAI,CAAC;CAQ/B"}
package/dist/tool/mcp.js DELETED
@@ -1,111 +0,0 @@
1
- import { mcpToFunctionTool } from "../mcp/utils";
2
- import { filter } from "../lib/utils";
3
- import { Toolkit } from "./toolkit";
4
- /**
5
- * A toolkit that wraps an MCP server and provides tools from it.
6
- *
7
- * Handles connection lifecycle automatically - connects lazily on first tool request
8
- * and provides cleanup via destroy().
9
- *
10
- * @example
11
- * ```ts
12
- * const server = new MCPServerStdio({
13
- * id: "github",
14
- * command: "npx",
15
- * args: ["-y", "@modelcontextprotocol/server-github"],
16
- * env: {
17
- * GITHUB_TOKEN: process.env.GITHUB_TOKEN,
18
- * },
19
- * });
20
- *
21
- * const github = new MCPToolkit({
22
- * id: "github",
23
- * server,
24
- * filter: async (ctx, tool) => {
25
- * // Only allow certain tools
26
- * return !tool.id.startsWith("dangerous_");
27
- * },
28
- * });
29
- *
30
- * const agent = new Agent({
31
- * toolkits: [github],
32
- * });
33
- * ```
34
- */
35
- export class MCPToolkit extends Toolkit {
36
- id;
37
- server;
38
- cache;
39
- filter;
40
- connected = false;
41
- cached = false;
42
- /**
43
- * Create a new MCP toolkit.
44
- *
45
- * @param config Toolkit configuration with id and server instance
46
- */
47
- constructor(config) {
48
- super();
49
- this.id = config.id;
50
- this.server = config.server;
51
- this.filter = config.filter ?? (() => true);
52
- this.cache = new Map();
53
- }
54
- /**
55
- * Get a specific tool by ID.
56
- *
57
- * Returns the tool from the local cache. The cache is populated on the first
58
- * call to list(). Returns undefined if list() hasn't been called yet.
59
- *
60
- * @param id The tool ID to look up
61
- * @returns The tool if found in cache, undefined otherwise
62
- */
63
- get(id) {
64
- return this.cache.get(id);
65
- }
66
- /**
67
- * List all tools available from the MCP server.
68
- *
69
- * Connects to the server lazily on first call. Tools are cached locally after
70
- * the first fetch. The MCP server itself also handles caching via the
71
- * cacheToolsList option, so the network call is only made once.
72
- *
73
- * @param context Optional context for filtering tools
74
- * @returns Array of tools from the MCP server
75
- */
76
- async list(context) {
77
- if (!this.connected) {
78
- await this.server.connect();
79
- this.connected = true;
80
- }
81
- // lazy cache population
82
- if (!this.cached) {
83
- const mcpTools = await this.server.listTools();
84
- for (const mcpTool of mcpTools) {
85
- const tool = mcpToFunctionTool(this.server, mcpTool);
86
- this.cache.set(tool.id, tool);
87
- }
88
- this.cached = true;
89
- }
90
- const tools = Array.from(this.cache.values());
91
- // apply filter
92
- if (context && this.agent) {
93
- const ctx = { context, agent: this.agent, toolkitId: this.id };
94
- return filter(tools, async (tool) => {
95
- return await this.filter(ctx, tool);
96
- });
97
- }
98
- return tools;
99
- }
100
- /**
101
- * Cleanup resources and close the MCP server connection.
102
- */
103
- async destroy() {
104
- if (this.connected) {
105
- await this.server.close();
106
- this.connected = false;
107
- this.cache.clear();
108
- this.cached = false;
109
- }
110
- }
111
- }