kernl 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (257) hide show
  1. package/.turbo/turbo-build.log +5 -0
  2. package/CHANGELOG.md +53 -0
  3. package/LICENSE +201 -0
  4. package/dist/agent.d.ts +43 -0
  5. package/dist/agent.d.ts.map +1 -0
  6. package/dist/agent.js +130 -0
  7. package/dist/context.d.ts +70 -0
  8. package/dist/context.d.ts.map +1 -0
  9. package/dist/context.js +111 -0
  10. package/dist/env.d.ts +45 -0
  11. package/dist/env.d.ts.map +1 -0
  12. package/dist/env.js +31 -0
  13. package/dist/error.d.ts +1 -0
  14. package/dist/error.d.ts.map +1 -0
  15. package/dist/error.js +1 -0
  16. package/dist/guardrail.d.ts +178 -0
  17. package/dist/guardrail.d.ts.map +1 -0
  18. package/dist/guardrail.js +34 -0
  19. package/dist/index.d.ts +4 -0
  20. package/dist/index.d.ts.map +1 -0
  21. package/dist/index.js +2 -0
  22. package/dist/kernel.d.ts +7 -0
  23. package/dist/kernel.d.ts.map +1 -0
  24. package/dist/kernel.js +7 -0
  25. package/dist/kernl.d.ts +18 -0
  26. package/dist/kernl.d.ts.map +1 -0
  27. package/dist/kernl.js +16 -0
  28. package/dist/lib/env.d.ts +43 -0
  29. package/dist/lib/env.d.ts.map +1 -0
  30. package/dist/lib/env.js +29 -0
  31. package/dist/lib/error.d.ts +88 -0
  32. package/dist/lib/error.d.ts.map +1 -0
  33. package/dist/lib/error.js +117 -0
  34. package/dist/lib/logger.d.ts +36 -0
  35. package/dist/lib/logger.d.ts.map +1 -0
  36. package/dist/lib/logger.js +43 -0
  37. package/dist/lib/serde/__tests__/codec.test.d.ts +2 -0
  38. package/dist/lib/serde/__tests__/codec.test.d.ts.map +1 -0
  39. package/dist/lib/serde/__tests__/codec.test.js +75 -0
  40. package/dist/lib/serde/codec.d.ts +12 -0
  41. package/dist/lib/serde/codec.d.ts.map +1 -0
  42. package/dist/lib/serde/codec.js +54 -0
  43. package/dist/lib/serde/json.d.ts +8 -0
  44. package/dist/lib/serde/json.d.ts.map +1 -0
  45. package/dist/lib/serde/json.js +13 -0
  46. package/dist/lib/serde/thread.d.ts +1 -0
  47. package/dist/lib/serde/thread.d.ts.map +1 -0
  48. package/dist/lib/serde/thread.js +172 -0
  49. package/dist/lib/serde/tool.d.ts +36 -0
  50. package/dist/lib/serde/tool.d.ts.map +1 -0
  51. package/dist/lib/serde/tool.js +1 -0
  52. package/dist/lib/utils.d.ts +19 -0
  53. package/dist/lib/utils.d.ts.map +1 -0
  54. package/dist/lib/utils.js +41 -0
  55. package/dist/lifecycle.d.ts +133 -0
  56. package/dist/lifecycle.d.ts.map +1 -0
  57. package/dist/lifecycle.js +29 -0
  58. package/dist/logger.d.ts +36 -0
  59. package/dist/logger.d.ts.map +1 -0
  60. package/dist/logger.js +43 -0
  61. package/dist/mcp/__tests__/base.test.d.ts +2 -0
  62. package/dist/mcp/__tests__/base.test.d.ts.map +1 -0
  63. package/dist/mcp/__tests__/base.test.js +268 -0
  64. package/dist/mcp/__tests__/fixtures/echo-server.d.ts +3 -0
  65. package/dist/mcp/__tests__/fixtures/echo-server.d.ts.map +1 -0
  66. package/dist/mcp/__tests__/fixtures/echo-server.js +92 -0
  67. package/dist/mcp/__tests__/fixtures/math-server.d.ts +3 -0
  68. package/dist/mcp/__tests__/fixtures/math-server.d.ts.map +1 -0
  69. package/dist/mcp/__tests__/fixtures/math-server.js +98 -0
  70. package/dist/mcp/__tests__/fixtures/server.d.ts +3 -0
  71. package/dist/mcp/__tests__/fixtures/server.d.ts.map +1 -0
  72. package/dist/mcp/__tests__/fixtures/server.js +162 -0
  73. package/dist/mcp/__tests__/fixtures/test-server.d.ts +3 -0
  74. package/dist/mcp/__tests__/fixtures/test-server.d.ts.map +1 -0
  75. package/dist/mcp/__tests__/fixtures/test-server.js +163 -0
  76. package/dist/mcp/__tests__/fixtures/utils.d.ts +17 -0
  77. package/dist/mcp/__tests__/fixtures/utils.d.ts.map +1 -0
  78. package/dist/mcp/__tests__/fixtures/utils.js +42 -0
  79. package/dist/mcp/__tests__/integration.test.d.ts +2 -0
  80. package/dist/mcp/__tests__/integration.test.d.ts.map +1 -0
  81. package/dist/mcp/__tests__/integration.test.js +360 -0
  82. package/dist/mcp/__tests__/stdio.test.d.ts +2 -0
  83. package/dist/mcp/__tests__/stdio.test.d.ts.map +1 -0
  84. package/dist/mcp/__tests__/stdio.test.js +180 -0
  85. package/dist/mcp/__tests__/test-utils.d.ts +17 -0
  86. package/dist/mcp/__tests__/test-utils.d.ts.map +1 -0
  87. package/dist/mcp/__tests__/test-utils.js +42 -0
  88. package/dist/mcp/__tests__/utils.test.d.ts +2 -0
  89. package/dist/mcp/__tests__/utils.test.d.ts.map +1 -0
  90. package/dist/mcp/__tests__/utils.test.js +300 -0
  91. package/dist/mcp/base.d.ts +88 -0
  92. package/dist/mcp/base.d.ts.map +1 -0
  93. package/dist/mcp/base.js +68 -0
  94. package/dist/mcp/http.d.ts +34 -0
  95. package/dist/mcp/http.d.ts.map +1 -0
  96. package/dist/mcp/http.js +100 -0
  97. package/dist/mcp/node.d.ts +60 -0
  98. package/dist/mcp/node.d.ts.map +1 -0
  99. package/dist/mcp/node.js +297 -0
  100. package/dist/mcp/sse.d.ts +34 -0
  101. package/dist/mcp/sse.d.ts.map +1 -0
  102. package/dist/mcp/sse.js +97 -0
  103. package/dist/mcp/stdio.d.ts +32 -0
  104. package/dist/mcp/stdio.d.ts.map +1 -0
  105. package/dist/mcp/stdio.js +96 -0
  106. package/dist/mcp/types.d.ts +172 -0
  107. package/dist/mcp/types.d.ts.map +1 -0
  108. package/dist/mcp/types.js +16 -0
  109. package/dist/mcp/utils.d.ts +23 -0
  110. package/dist/mcp/utils.d.ts.map +1 -0
  111. package/dist/mcp/utils.js +44 -0
  112. package/dist/model.d.ts +175 -0
  113. package/dist/model.d.ts.map +1 -0
  114. package/dist/model.js +1 -0
  115. package/dist/providers/ai.d.ts +1 -0
  116. package/dist/providers/ai.d.ts.map +1 -0
  117. package/dist/providers/ai.js +1 -0
  118. package/dist/providers/default.d.ts +16 -0
  119. package/dist/providers/default.d.ts.map +1 -0
  120. package/dist/providers/default.js +17 -0
  121. package/dist/providers/registry.d.ts +1 -0
  122. package/dist/providers/registry.d.ts.map +1 -0
  123. package/dist/providers/registry.js +1 -0
  124. package/dist/sched/scheduler.d.ts +20 -0
  125. package/dist/sched/scheduler.d.ts.map +1 -0
  126. package/dist/sched/scheduler.js +1 -0
  127. package/dist/sched/task.d.ts +92 -0
  128. package/dist/sched/task.d.ts.map +1 -0
  129. package/dist/sched/task.js +102 -0
  130. package/dist/serde/__tests__/codec.test.d.ts +2 -0
  131. package/dist/serde/__tests__/codec.test.d.ts.map +1 -0
  132. package/dist/serde/__tests__/codec.test.js +75 -0
  133. package/dist/serde/codec.d.ts +12 -0
  134. package/dist/serde/codec.d.ts.map +1 -0
  135. package/dist/serde/codec.js +54 -0
  136. package/dist/serde/json.d.ts +8 -0
  137. package/dist/serde/json.d.ts.map +1 -0
  138. package/dist/serde/json.js +13 -0
  139. package/dist/serde/thread.d.ts +687 -0
  140. package/dist/serde/thread.d.ts.map +1 -0
  141. package/dist/serde/thread.js +158 -0
  142. package/dist/serde/tool.d.ts +36 -0
  143. package/dist/serde/tool.d.ts.map +1 -0
  144. package/dist/serde/tool.js +1 -0
  145. package/dist/session.d.ts +1 -0
  146. package/dist/session.d.ts.map +1 -0
  147. package/dist/session.js +1 -0
  148. package/dist/task.d.ts +87 -0
  149. package/dist/task.d.ts.map +1 -0
  150. package/dist/task.js +97 -0
  151. package/dist/thread/__tests__/mock.d.ts +28 -0
  152. package/dist/thread/__tests__/mock.d.ts.map +1 -0
  153. package/dist/thread/__tests__/mock.js +74 -0
  154. package/dist/thread/__tests__/thread.test.d.ts +2 -0
  155. package/dist/thread/__tests__/thread.test.d.ts.map +1 -0
  156. package/dist/thread/__tests__/thread.test.js +1412 -0
  157. package/dist/thread/index.d.ts +2 -0
  158. package/dist/thread/index.d.ts.map +1 -0
  159. package/dist/thread/index.js +1 -0
  160. package/dist/thread/thread.d.ts +66 -0
  161. package/dist/thread/thread.d.ts.map +1 -0
  162. package/dist/thread/thread.js +472 -0
  163. package/dist/thread/utils.d.ts +19 -0
  164. package/dist/thread/utils.d.ts.map +1 -0
  165. package/dist/thread/utils.js +50 -0
  166. package/dist/tool/__tests__/fixtures.d.ts +45 -0
  167. package/dist/tool/__tests__/fixtures.d.ts.map +1 -0
  168. package/dist/tool/__tests__/fixtures.js +97 -0
  169. package/dist/tool/__tests__/tool.test.d.ts +2 -0
  170. package/dist/tool/__tests__/tool.test.d.ts.map +1 -0
  171. package/dist/tool/__tests__/tool.test.js +172 -0
  172. package/dist/tool/__tests__/toolkit.test.d.ts +2 -0
  173. package/dist/tool/__tests__/toolkit.test.d.ts.map +1 -0
  174. package/dist/tool/__tests__/toolkit.test.js +134 -0
  175. package/dist/tool/index.d.ts +4 -0
  176. package/dist/tool/index.d.ts.map +1 -0
  177. package/dist/tool/index.js +2 -0
  178. package/dist/tool/mcp.d.ts +75 -0
  179. package/dist/tool/mcp.d.ts.map +1 -0
  180. package/dist/tool/mcp.js +111 -0
  181. package/dist/tool/tool.d.ts +95 -0
  182. package/dist/tool/tool.d.ts.map +1 -0
  183. package/dist/tool/tool.js +176 -0
  184. package/dist/tool/toolkit.d.ts +121 -0
  185. package/dist/tool/toolkit.d.ts.map +1 -0
  186. package/dist/tool/toolkit.js +180 -0
  187. package/dist/tool/types.d.ts +187 -0
  188. package/dist/tool/types.d.ts.map +1 -0
  189. package/dist/tool/types.js +1 -0
  190. package/dist/tools.d.ts +362 -0
  191. package/dist/tools.d.ts.map +1 -0
  192. package/dist/tools.js +220 -0
  193. package/dist/trace/processor.d.ts +1 -0
  194. package/dist/trace/processor.d.ts.map +1 -0
  195. package/dist/trace/processor.js +1 -0
  196. package/dist/trace/traces.d.ts +1 -0
  197. package/dist/trace/traces.d.ts.map +1 -0
  198. package/dist/trace/traces.js +73 -0
  199. package/dist/trace/utils.d.ts +22 -0
  200. package/dist/trace/utils.d.ts.map +1 -0
  201. package/dist/trace/utils.js +30 -0
  202. package/dist/types/agent.d.ts +91 -0
  203. package/dist/types/agent.d.ts.map +1 -0
  204. package/dist/types/agent.js +1 -0
  205. package/dist/types/proto.d.ts +1551 -0
  206. package/dist/types/proto.d.ts.map +1 -0
  207. package/dist/types/proto.js +531 -0
  208. package/dist/types/thread.d.ts +71 -0
  209. package/dist/types/thread.d.ts.map +1 -0
  210. package/dist/types/thread.js +5 -0
  211. package/dist/usage.d.ts +43 -0
  212. package/dist/usage.d.ts.map +1 -0
  213. package/dist/usage.js +61 -0
  214. package/package.json +52 -0
  215. package/src/agent.ts +203 -0
  216. package/src/context.ts +265 -0
  217. package/src/guardrail.ts +277 -0
  218. package/src/index.ts +3 -0
  219. package/src/kernl.ts +22 -0
  220. package/src/lib/env.ts +36 -0
  221. package/src/lib/error.ts +158 -0
  222. package/src/lib/logger.ts +78 -0
  223. package/src/lib/serde/json.ts +18 -0
  224. package/src/lib/serde/thread.ts +188 -0
  225. package/src/lifecycle.ts +181 -0
  226. package/src/mcp/__tests__/base.test.ts +344 -0
  227. package/src/mcp/__tests__/fixtures/server.ts +179 -0
  228. package/src/mcp/__tests__/fixtures/utils.ts +58 -0
  229. package/src/mcp/__tests__/integration.test.ts +447 -0
  230. package/src/mcp/__tests__/stdio.test.ts +236 -0
  231. package/src/mcp/__tests__/utils.test.ts +360 -0
  232. package/src/mcp/base.ts +162 -0
  233. package/src/mcp/http.ts +147 -0
  234. package/src/mcp/sse.ts +137 -0
  235. package/src/mcp/stdio.ts +136 -0
  236. package/src/mcp/types.ts +202 -0
  237. package/src/mcp/utils.ts +62 -0
  238. package/src/task.ts +119 -0
  239. package/src/thread/__tests__/mock.ts +95 -0
  240. package/src/thread/__tests__/thread.test.ts +1574 -0
  241. package/src/thread/index.ts +1 -0
  242. package/src/thread/thread.ts +611 -0
  243. package/src/thread/utils.ts +67 -0
  244. package/src/tool/__tests__/fixtures.ts +106 -0
  245. package/src/tool/__tests__/tool.test.ts +235 -0
  246. package/src/tool/__tests__/toolkit.test.ts +174 -0
  247. package/src/tool/index.ts +10 -0
  248. package/src/tool/tool.ts +264 -0
  249. package/src/tool/toolkit.ts +234 -0
  250. package/src/tool/types.ts +243 -0
  251. package/src/trace/processor.ts +0 -0
  252. package/src/trace/traces.ts +86 -0
  253. package/src/trace/utils.ts +38 -0
  254. package/src/types/agent.ts +145 -0
  255. package/src/types/thread.ts +86 -0
  256. package/tsconfig.json +13 -0
  257. package/vitest.config.ts +14 -0
@@ -0,0 +1 @@
1
+ export { Thread } from "./thread";
@@ -0,0 +1,611 @@
1
+ import assert from "assert";
2
+
3
+ import { Kernl } from "@/kernl";
4
+ import { Agent } from "@/agent";
5
+ import { Context } from "@/context";
6
+ import type { Task } from "@/task";
7
+
8
+ import {
9
+ ToolCall,
10
+ LanguageModel,
11
+ LanguageModelRequest,
12
+ LanguageModelResponse,
13
+ LanguageModelItem,
14
+ FAILED,
15
+ } from "@kernl/protocol";
16
+ import { randomID, filter } from "@kernl/shared/lib";
17
+
18
+ import type {
19
+ ActionSet,
20
+ ThreadEvent,
21
+ ThreadOptions,
22
+ ThreadExecuteResult,
23
+ PerformActionsResult,
24
+ TickResult,
25
+ } from "@/types/thread";
26
+ import type { AgentResponseType } from "@/types/agent";
27
+ import type { ResolvedAgentResponse } from "@/guardrail";
28
+
29
+ import { getFinalResponse, parseFinalResponse } from "./utils";
30
+
31
+ /**
32
+ * A thread drives the execution loop for an agent.
33
+ */
34
+ export class Thread<
35
+ TContext = unknown,
36
+ TResponse extends AgentResponseType = "text",
37
+ > {
38
+ private kernl: Kernl;
39
+
40
+ readonly id: string;
41
+ readonly agent: Agent<TContext, TResponse>;
42
+ readonly context: Context<TContext>;
43
+ readonly model: LanguageModel; /* inherited from the agent unless specified */
44
+ readonly parent: Task<TContext> | null; /* parent task which spawned this thread */
45
+ readonly mode: "blocking" | "stream"; /* TODO */
46
+
47
+ /* state */
48
+ readonly state: ThreadState;
49
+ readonly input: ThreadEvent[] | string; /* the initial input for the thread */
50
+ private history: ThreadEvent[] /* events generated during this thread's execution */;
51
+
52
+ constructor(
53
+ kernl: Kernl,
54
+ agent: Agent<TContext, TResponse>,
55
+ input: ThreadEvent[] | string,
56
+ options?: ThreadOptions<TContext>,
57
+ ) {
58
+ this.id = `tid_${randomID()}`;
59
+ this.agent = agent;
60
+ this.context = options?.context ?? new Context<TContext>();
61
+ this.kernl = kernl;
62
+ this.parent = options?.task ?? null;
63
+ this.model = options?.model ?? agent.model;
64
+ this.state = new ThreadState(); // (TODO): checkpoint ?? new ThreadState()
65
+ this.mode = "blocking"; // (TODO): add streaming
66
+ this.input = input;
67
+
68
+ // Convert string input to user message and initialize history
69
+ if (typeof input === "string") {
70
+ this.history = [
71
+ {
72
+ kind: "message",
73
+ id: `msg_${randomID()}`,
74
+ role: "user",
75
+ content: [
76
+ {
77
+ kind: "text",
78
+ text: input,
79
+ },
80
+ ],
81
+ },
82
+ ];
83
+ } else {
84
+ this.history = input;
85
+ }
86
+ }
87
+
88
+ /**
89
+ * Main thread execution loop - runs until terminal state or interruption
90
+ */
91
+ async execute(): Promise<
92
+ ThreadExecuteResult<ResolvedAgentResponse<TResponse>>
93
+ > {
94
+ while (true) {
95
+ const { events, intentions } = await this.tick(); // actions: { syscalls, functions, mcpApprovalRequests }
96
+
97
+ this.history.push(...events);
98
+
99
+ // // priority 1: syscalls first - these override all other actions
100
+ // if (actions.syscalls.length > 0) {
101
+ // switch (actions.syscalls.kind) { // is it possible to have more than one?
102
+ // case SYS_WAIT:
103
+ // return this.state;
104
+ // case SYS_EXIT:
105
+ // return { state: this.state, output: this.output }
106
+ // default:
107
+ // }
108
+ // }
109
+
110
+ // if model returns a message with no actions intentions -> terminal state
111
+ if (!intentions) {
112
+ const text = getFinalResponse(events);
113
+ if (!text) continue; // run again, policy-dependent?
114
+
115
+ const parsed = parseFinalResponse(text, this.agent.responseType);
116
+
117
+ // await this.agent.runOutputGuardails(context, state);
118
+ // this.kernl.emit("thread.terminated", context, output);
119
+ return { response: parsed, state: this.state };
120
+ }
121
+
122
+ // perform the actions intended by the model
123
+ const { actions, pendingApprovals } =
124
+ await this.performActions(intentions);
125
+
126
+ this.history.push(...actions);
127
+
128
+ if (pendingApprovals.length > 0) {
129
+ // publish a batch approval request containing all of them
130
+ //
131
+ // const reqid = randomID();
132
+ // this.kernl.publish(channel, approvalRequest);
133
+ //
134
+ // const filter = { reqid }
135
+ // await wait_event(Action.ApprovalResponse, filter);
136
+ }
137
+ }
138
+ }
139
+
140
+ // ----------------------
141
+ // Internal helpers
142
+ // ----------------------
143
+
144
+ /**
145
+ * A single tick of the thread's execution.
146
+ *
147
+ * Prepares the input for the model, gets the response, and then parses into a TickResult
148
+ * with the events generated and the model's intentions (actions).
149
+ */
150
+ private async tick(): Promise<TickResult> {
151
+ this.state.tick++;
152
+
153
+ // // check limits
154
+ // if (this.state.tick > this.limits.maxTicks) {
155
+ // throw new RuntimeError("resource_limit:max_ticks_exceeded");
156
+ // }
157
+
158
+ // run guardrails on the first tick
159
+ if (this.state.tick === 1) {
160
+ // await this.agent.runInputGuardrails(this.context, ...?);
161
+ }
162
+
163
+ const req = await this.prepareModelRequest(this.history); // (TODO): how to get input for this tick?
164
+
165
+ // if (this.mode === "stream") {
166
+ // const stream = this.model.stream(input, {
167
+ // system: systemPrompt,
168
+ // tools: this.agent.tools /* [systools, tools] */,
169
+ // settings: this.agent.modelSettings,
170
+ // responseSchema: this.agent.responseType,
171
+ // });
172
+ // for await (const event of stream) {
173
+ // // handle streaming events
174
+ // }
175
+ // response = stream.collect(); // something like this
176
+ // } else {
177
+ const res = await this.model.generate(req);
178
+
179
+ this.state.modelResponses.push(res);
180
+ // this.stats.usage.add(response.usage);
181
+
182
+ return this.parseModelResponse(res);
183
+ }
184
+
185
+ /**
186
+ * Perform the actions returned by the model
187
+ */
188
+ private async performActions(
189
+ intentions: ActionSet,
190
+ ): Promise<PerformActionsResult> {
191
+ // (TODO): refactor into a general actions system - probably shouldn't be handled by Thread
192
+ const toolEvents = await this.executeTools(intentions.toolCalls);
193
+ // const mcpEvents = await this.executeMCPRequests(actions.mcpRequests);
194
+
195
+ // Separate events and pending approvals
196
+ const actions: ThreadEvent[] = [];
197
+ const pendingApprovals: ToolCall[] = [];
198
+
199
+ // (TODO): clean this - approval tracking should be handled differently
200
+ for (const e of toolEvents) {
201
+ if (
202
+ e.kind === "tool-result" &&
203
+ (e.state as any) === "requires_approval"
204
+ ) {
205
+ // Find the original tool call for this pending approval
206
+ const originalCall = intentions.toolCalls.find(
207
+ (call) => call.callId === e.callId,
208
+ );
209
+ if (originalCall) {
210
+ pendingApprovals.push(originalCall);
211
+ }
212
+ } else {
213
+ actions.push(e);
214
+ }
215
+ }
216
+
217
+ return {
218
+ actions: actions,
219
+ pendingApprovals,
220
+ };
221
+ }
222
+
223
+ /**
224
+ * Execute function calls requested by the model
225
+ *
226
+ * TODO: refactor into actions system
227
+ */
228
+ private async executeTools(calls: ToolCall[]): Promise<ThreadEvent[]> {
229
+ return await Promise.all(
230
+ calls.map(async (call: ToolCall) => {
231
+ try {
232
+ const tool = this.agent.tool(call.toolId);
233
+ if (!tool) {
234
+ throw new Error(`Tool ${call.toolId} not found`);
235
+ }
236
+
237
+ // hosted tools are executed server-side by the provider, not locally
238
+ assert(
239
+ tool.type === "function",
240
+ `Tool ${call.id} is a hosted tool and should not be executed locally`,
241
+ );
242
+
243
+ // (TMP) - passing the approval status through the context until actions system
244
+ // is refined
245
+ const ctx = new Context(this.context.context);
246
+ ctx.approve(call.callId); // mark this call as approved
247
+ const res = await tool.invoke(ctx, call.arguments, call.callId);
248
+
249
+ return {
250
+ kind: "tool-result" as const,
251
+ callId: call.callId,
252
+ toolId: call.toolId,
253
+ state: res.state,
254
+ result: res.result,
255
+ error: res.error,
256
+ };
257
+ } catch (error) {
258
+ // Handles both tool not found AND any execution errors
259
+ return {
260
+ kind: "tool-result" as const,
261
+ callId: call.callId,
262
+ toolId: call.toolId,
263
+ state: FAILED,
264
+ result: undefined as any,
265
+ error: error instanceof Error ? error.message : String(error),
266
+ };
267
+ }
268
+ }),
269
+ );
270
+ }
271
+
272
+ /**
273
+ * Applies call-level filters and prepares the model request for the language model
274
+ */
275
+ private async prepareModelRequest(
276
+ history: ThreadEvent[],
277
+ ): Promise<LanguageModelRequest> {
278
+ let settings = {
279
+ ...this.agent.modelSettings,
280
+ };
281
+
282
+ // // TODO: what do we want to do with this?
283
+ // settings = maybeResetToolChoice(this.agent, this.state.toolUse, settings);
284
+
285
+ const system = await this.agent.instructions(this.context);
286
+ const input: LanguageModelItem[] = system
287
+ ? [
288
+ // (TODO): add message(role, text) helper
289
+ {
290
+ kind: "message",
291
+ id: randomID(),
292
+ role: "system",
293
+ content: [{ kind: "text", text: system }],
294
+ },
295
+ ...history, // (TODO): filter for LanguageModelItem specifically - there may be other thread events
296
+ ]
297
+ : history;
298
+
299
+ // TODO: apply custom input filters - arguably want global + agent-scoped -> apply in a middleware-like chain
300
+ // const filtered = await applyInputFilters(inputWithSystem, context);
301
+
302
+ const filtered = input;
303
+
304
+ // serialize action repertoire
305
+ const allTools = await this.agent.tools(this.context);
306
+ const enabled = await filter(
307
+ allTools,
308
+ async (tool) => await tool.isEnabled(this.context, this.agent),
309
+ );
310
+ const tools = enabled.map((tool) => tool.serialize());
311
+
312
+ return {
313
+ input: filtered,
314
+ settings,
315
+ tools,
316
+ };
317
+ }
318
+
319
+ /**
320
+ * @internal
321
+ * Parses the model's response into events (for history) and actions (for execution).
322
+ */
323
+ private parseModelResponse(res: LanguageModelResponse): TickResult {
324
+ const events: ThreadEvent[] = [];
325
+ const toolCalls: ToolCall[] = [];
326
+
327
+ for (const event of res.content) {
328
+ switch (event.kind) {
329
+ case "tool-call":
330
+ // Add to both actions (for execution) and events (for history)
331
+ toolCalls.push(event);
332
+ // fallthrough
333
+ default:
334
+ events.push(event);
335
+ break;
336
+ }
337
+ }
338
+
339
+ return {
340
+ events,
341
+ intentions: toolCalls.length > 0 ? { toolCalls } : null,
342
+ };
343
+ }
344
+ }
345
+
346
+ /**
347
+ * ThreadState tracks the execution state of a single thread.
348
+ *
349
+ * A thread is created each time a task is scheduled and executes
350
+ * the main tick() loop until terminal state.
351
+ */
352
+ export class ThreadState {
353
+ tick: number /* current tick number (starts at 0, increments on each model call) */;
354
+ modelResponses: LanguageModelResponse[] /* all model responses received during this thread's execution */;
355
+
356
+ constructor() {
357
+ this.tick = 0;
358
+ this.modelResponses = [];
359
+ }
360
+
361
+ // /**
362
+ // * Check if the thread is in a terminal state - true when last event is an assistant
363
+ // * message with no tool calls
364
+ // */
365
+ // isTerminal(): boolean {
366
+ // if (this.history.length === 0) return false;
367
+
368
+ // const lastEvent = this.history[this.history.length - 1];
369
+ // return lastEvent.kind === "message" && lastEvent.role === "assistant";
370
+ // }
371
+ }
372
+
373
+ /**
374
+ * Common thread options shared between streaming and non-streaming execution pathways.
375
+ */
376
+ type SharedThreadOptions<TContext = undefined> = {
377
+ context?: TContext | Context<TContext>;
378
+ maxTurns?: number;
379
+ abort?: AbortSignal;
380
+ conversationId?: string;
381
+ // sessionInputCallback?: SessionInputCallback;
382
+ // callModelInputFilter?: CallModelInputFilter;
383
+ };
384
+
385
+ // /**
386
+ // * The result of an agent run in streaming mode.
387
+ // */
388
+ // export class StreamedRunResult<
389
+ // TContext,
390
+ // TAgent extends Agent<TContext, AgentResponseType>,
391
+ // >
392
+ // extends RunResultBase<TContext, TAgent>
393
+ // implements AsyncIterable<ThreadStreamEvent>
394
+ // {
395
+ // /**
396
+ // * The current agent that is running
397
+ // */
398
+ // public get currentAgent(): TAgent | undefined {
399
+ // return this.lastAgent;
400
+ // }
401
+
402
+ // /**
403
+ // * The current turn number
404
+ // */
405
+ // public currentTurn: number = 0;
406
+
407
+ // /**
408
+ // * The maximum number of turns that can be run
409
+ // */
410
+ // public maxTurns: number | undefined;
411
+
412
+ // #error: unknown = null;
413
+ // #signal?: AbortSignal;
414
+ // #readableController:
415
+ // | ReadableStreamDefaultController<ThreadStreamEvent>
416
+ // | undefined;
417
+ // #readableStream: ReadableStream<ThreadStreamEvent>;
418
+ // #completedPromise: Promise<void>;
419
+ // #completedPromiseResolve: (() => void) | undefined;
420
+ // #completedPromiseReject: ((err: unknown) => void) | undefined;
421
+ // #cancelled: boolean = false;
422
+ // #streamLoopPromise: Promise<void> | undefined;
423
+
424
+ // constructor(
425
+ // result: {
426
+ // state: ThreadState<TContext, TAgent>;
427
+ // signal?: AbortSignal;
428
+ // } = {} as any,
429
+ // ) {
430
+ // super(result.state);
431
+
432
+ // this.#signal = result.signal;
433
+
434
+ // this.#readableStream = new ReadableStream<ThreadStreamEvent>({
435
+ // start: (controller) => {
436
+ // this.#readableController = controller;
437
+ // },
438
+ // cancel: () => {
439
+ // this.#cancelled = true;
440
+ // },
441
+ // });
442
+
443
+ // this.#completedPromise = new Promise((resolve, reject) => {
444
+ // this.#completedPromiseResolve = resolve;
445
+ // this.#completedPromiseReject = reject;
446
+ // });
447
+
448
+ // if (this.#signal) {
449
+ // const handleAbort = () => {
450
+ // if (this.#cancelled) {
451
+ // return;
452
+ // }
453
+
454
+ // this.#cancelled = true;
455
+
456
+ // const controller = this.#readableController;
457
+ // this.#readableController = undefined;
458
+
459
+ // if (this.#readableStream.locked) {
460
+ // if (controller) {
461
+ // try {
462
+ // controller.close();
463
+ // } catch (err) {
464
+ // logger.debug(`Failed to close readable stream on abort: ${err}`);
465
+ // }
466
+ // }
467
+ // } else {
468
+ // void this.#readableStream
469
+ // .cancel(this.#signal?.reason)
470
+ // .catch((err) => {
471
+ // logger.debug(`Failed to cancel readable stream on abort: ${err}`);
472
+ // });
473
+ // }
474
+
475
+ // this.#completedPromiseResolve?.();
476
+ // };
477
+
478
+ // if (this.#signal.aborted) {
479
+ // handleAbort();
480
+ // } else {
481
+ // this.#signal.addEventListener("abort", handleAbort, { once: true });
482
+ // }
483
+ // }
484
+ // }
485
+
486
+ // /**
487
+ // * @internal
488
+ // * Adds an item to the stream of output items
489
+ // */
490
+ // _addItem(item: ThreadStreamEvent) {
491
+ // if (!this.cancelled) {
492
+ // this.#readableController?.enqueue(item);
493
+ // }
494
+ // }
495
+
496
+ // /**
497
+ // * @internal
498
+ // * Indicates that the stream has been completed
499
+ // */
500
+ // _done() {
501
+ // if (!this.cancelled && this.#readableController) {
502
+ // this.#readableController.close();
503
+ // this.#readableController = undefined;
504
+ // this.#completedPromiseResolve?.();
505
+ // }
506
+ // }
507
+
508
+ // /**
509
+ // * @internal
510
+ // * Handles an error in the stream loop.
511
+ // */
512
+ // _raiseError(err: unknown) {
513
+ // if (!this.cancelled && this.#readableController) {
514
+ // this.#readableController.error(err);
515
+ // this.#readableController = undefined;
516
+ // }
517
+ // this.#error = err;
518
+ // this.#completedPromiseReject?.(err);
519
+ // this.#completedPromise.catch((e) => {
520
+ // logger.debug(`Resulted in an error: ${e}`);
521
+ // });
522
+ // }
523
+
524
+ // /**
525
+ // * Returns true if the stream has been cancelled.
526
+ // */
527
+ // get cancelled(): boolean {
528
+ // return this.#cancelled;
529
+ // }
530
+
531
+ // /**
532
+ // * Returns the underlying readable stream.
533
+ // * @returns A readable stream of the agent run.
534
+ // */
535
+ // toStream(): ReadableStream<ThreadStreamEvent> {
536
+ // return this.#readableStream as ReadableStream<ThreadStreamEvent>;
537
+ // }
538
+
539
+ // /**
540
+ // * Await this promise to ensure that the stream has been completed if you are not consuming the
541
+ // * stream directly.
542
+ // */
543
+ // get completed() {
544
+ // return this.#completedPromise;
545
+ // }
546
+
547
+ // /**
548
+ // * Error thrown during the run, if any.
549
+ // */
550
+ // get error() {
551
+ // return this.#error;
552
+ // }
553
+
554
+ // /**
555
+ // * Returns a readable stream of the final text output of the agent run.
556
+ // *
557
+ // * @returns A readable stream of the final output of the agent run.
558
+ // * @remarks Pass `{ compatibleWithNodeStreams: true }` to receive a Node.js compatible stream
559
+ // * instance.
560
+ // */
561
+ // toTextStream(): ReadableStream<string>;
562
+ // toTextStream(options?: { compatibleWithNodeStreams: true }): Readable;
563
+ // toTextStream(options?: {
564
+ // compatibleWithNodeStreams?: false;
565
+ // }): ReadableStream<string>;
566
+ // toTextStream(
567
+ // options: { compatibleWithNodeStreams?: boolean } = {},
568
+ // ): Readable | ReadableStream<string> {
569
+ // const stream = this.#readableStream.pipeThrough(
570
+ // new TransformStream<ThreadStreamEvent, string>({
571
+ // transform(event, controller) {
572
+ // if (
573
+ // event.kind === "raw_model_stream_event" && // (TODO): what to do here?
574
+ // event.data.kind === "text-delta"
575
+ // ) {
576
+ // const item = TextDeltaEvent.parse(event); // ??
577
+ // controller.enqueue(item.text); // (TODO): is it just the text that we want to return here?
578
+ // }
579
+ // },
580
+ // }),
581
+ // );
582
+
583
+ // if (options.compatibleWithNodeStreams) {
584
+ // return Readable.fromWeb(stream);
585
+ // }
586
+
587
+ // return stream as ReadableStream<string>;
588
+ // }
589
+
590
+ // [Symbol.asyncIterator](): AsyncIterator<ThreadStreamEvent> {
591
+ // return this.#readableStream[Symbol.asyncIterator]();
592
+ // }
593
+
594
+ // /**
595
+ // * @internal
596
+ // * Sets the stream loop promise that completes when the internal stream loop finishes.
597
+ // * This is used to defer trace end until all agent work is complete.
598
+ // */
599
+ // _setStreamLoopPromise(promise: Promise<void>) {
600
+ // this.#streamLoopPromise = promise;
601
+ // }
602
+
603
+ // /**
604
+ // * @internal
605
+ // * Returns a promise that resolves when the stream loop completes.
606
+ // * This is used by the tracing system to wait for all agent work before ending the trace.
607
+ // */
608
+ // _getStreamLoopPromise(): Promise<void> | undefined {
609
+ // return this.#streamLoopPromise;
610
+ // }
611
+ // }
@@ -0,0 +1,67 @@
1
+ import { ZodType } from "zod";
2
+
3
+ import type { ResolvedAgentResponse } from "@/guardrail";
4
+
5
+ /* lib */
6
+ import { json } from "@kernl/shared/lib";
7
+ import { ModelBehaviorError } from "@/lib/error";
8
+
9
+ /* types */
10
+ import type { AgentResponseType } from "@/types/agent";
11
+ import type { ThreadEvent } from "@/types/thread";
12
+
13
+ /**
14
+ * Extract the final text response from a list of events.
15
+ * Returns null if no assistant message with text content is found.
16
+ */
17
+ export function getFinalResponse(events: ThreadEvent[]): string | null {
18
+ // Scan backwards for the last assistant message
19
+ for (let i = events.length - 1; i >= 0; i--) {
20
+ const event = events[i];
21
+ if (event.kind === "message" && event.role === "assistant") {
22
+ // Extract text from content parts
23
+ for (const part of event.content) {
24
+ if (part.kind === "text") {
25
+ return part.text;
26
+ }
27
+ }
28
+ }
29
+ }
30
+ return null;
31
+ }
32
+
33
+ /**
34
+ * (TODO): This should run through the language model's native structured output (if avail)
35
+ *
36
+ * Parse the final response according to the response type schema.
37
+ * - If responseType is "text", returns the text as-is
38
+ * - If responseType is a ZodType, parses and validates the text as JSON
39
+ *
40
+ * @throws {ModelBehaviorError} if structured output validation fails
41
+ */
42
+ export function parseFinalResponse<TResponse extends AgentResponseType>(
43
+ text: string,
44
+ responseType: TResponse,
45
+ ): ResolvedAgentResponse<TResponse> {
46
+ if (responseType === "text") {
47
+ return text as ResolvedAgentResponse<TResponse>; // text output - return as-is
48
+ }
49
+
50
+ // structured output - decode JSON and validate with schema
51
+ if (responseType && typeof responseType === "object") {
52
+ // (TODO): prob better way of checking this here
53
+ const schema = responseType as ZodType;
54
+
55
+ try {
56
+ const validated = json(schema).decode(text); // (TODO): it would be nice if we could use `decodeSafe` here
57
+ return validated as ResolvedAgentResponse<TResponse>;
58
+ } catch (error) {
59
+ throw new ModelBehaviorError(
60
+ `Failed to parse structured output: ${error instanceof Error ? error.message : String(error)}`,
61
+ );
62
+ }
63
+ }
64
+
65
+ // Fallback - should not reach here
66
+ return text as ResolvedAgentResponse<TResponse>;
67
+ }