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,277 @@
1
+ import { z, type ZodType } from "zod";
2
+
3
+ import { LanguageModelResponse } from "@kernl/protocol";
4
+
5
+ import { Agent } from "./agent";
6
+ import { Context, UnknownContext } from "./context";
7
+ import type { AgentResponseType } from "./types/agent";
8
+ import type { TextResponse, ThreadEvent } from "./types/thread";
9
+
10
+ /**
11
+ * Resolves the agent output type based on the response type.
12
+ * - If TResponse is "text" → output is string
13
+ * - If TResponse is a ZodType → output is the inferred type from that schema
14
+ */
15
+ export type ResolvedAgentResponse<TResponse extends AgentResponseType> =
16
+ TResponse extends TextResponse
17
+ ? string
18
+ : TResponse extends ZodType
19
+ ? z.infer<TResponse>
20
+ : never;
21
+
22
+ /**
23
+ * The output of a guardrail function.
24
+ */
25
+ export interface GuardrailFunctionOutput {
26
+ /**
27
+ * Whether the tripwire was triggered. If triggered, the agent's execution will be halted.
28
+ */
29
+ tripwireTriggered: boolean;
30
+ /**
31
+ * Optional information about the guardrail's output.
32
+ * For example, the guardrail could include information about the checks it performed and granular results.
33
+ */
34
+ outputInfo: any; // (TODO): better name
35
+ }
36
+
37
+ // ----------------------------------------------------------
38
+ // Input Guardrail
39
+ // ----------------------------------------------------------
40
+
41
+ /**
42
+ * A guardrail that checks the input to the agent.
43
+ */
44
+ export interface InputGuardrail {
45
+ /**
46
+ * The name of the guardrail.
47
+ */
48
+ name: string;
49
+
50
+ /**
51
+ * The function that performs the guardrail check
52
+ */
53
+ execute: InputGuardrailFunction;
54
+ }
55
+
56
+ /**
57
+ * The result of an input guardrail execution.
58
+ */
59
+ export interface InputGuardrailResult {
60
+ /**
61
+ * The metadata of the guardrail.
62
+ */
63
+ guardrail: InputGuardrailMetadata;
64
+
65
+ /**
66
+ * The output of the guardrail.
67
+ */
68
+ output: GuardrailFunctionOutput;
69
+ }
70
+
71
+ /**
72
+ * Arguments for an input guardrail function.
73
+ */
74
+ export interface InputGuardrailFunctionArgs<TContext = UnknownContext> {
75
+ /**
76
+ * The agent that is being run.
77
+ */
78
+ agent: Agent<any, any>;
79
+
80
+ /**
81
+ * The input to the agent.
82
+ */
83
+ input: string | ThreadEvent[];
84
+
85
+ /**
86
+ * The context of the agent run.
87
+ */
88
+ context: Context<TContext>;
89
+ }
90
+
91
+ /**
92
+ * The function that performs the actual input guardrail check and returns the decision on whether
93
+ * a guardrail was triggered.
94
+ */
95
+ export type InputGuardrailFunction = (
96
+ args: InputGuardrailFunctionArgs,
97
+ ) => Promise<GuardrailFunctionOutput>;
98
+
99
+ /**
100
+ * Metadata for an input guardrail.
101
+ */
102
+ export interface InputGuardrailMetadata {
103
+ type: "input";
104
+ name: string;
105
+ }
106
+
107
+ /**
108
+ * Definition of an input guardrail. SDK users usually do not need to create this.
109
+ */
110
+ export interface InputGuardrailDefinition extends InputGuardrailMetadata {
111
+ guardrailFunction: InputGuardrailFunction;
112
+ run(args: InputGuardrailFunctionArgs): Promise<InputGuardrailResult>;
113
+ }
114
+
115
+ /**
116
+ * Arguments for defining an input guardrail definition.
117
+ */
118
+ export interface DefineInputGuardrailArgs {
119
+ name: string;
120
+ execute: InputGuardrailFunction;
121
+ }
122
+
123
+ /**
124
+ * Defines an input guardrail definition.
125
+ */
126
+ export function defineInputGuardrail({
127
+ name,
128
+ execute,
129
+ }: DefineInputGuardrailArgs): InputGuardrailDefinition {
130
+ return {
131
+ type: "input",
132
+ name,
133
+ guardrailFunction: execute,
134
+ async run(args: InputGuardrailFunctionArgs): Promise<InputGuardrailResult> {
135
+ return {
136
+ guardrail: { type: "input", name },
137
+ output: await execute(args),
138
+ };
139
+ },
140
+ };
141
+ }
142
+
143
+ // ----------------------------------------------------------
144
+ // Output Guardrail
145
+ // ----------------------------------------------------------
146
+
147
+ /**
148
+ * Arguments for an output guardrail function.
149
+ */
150
+ export interface OutputGuardrailFunctionArgs<
151
+ TContext = UnknownContext,
152
+ TResponse extends AgentResponseType = TextResponse,
153
+ > {
154
+ agent: Agent<any, any>;
155
+ agentOutput: ResolvedAgentResponse<TResponse>; // ??
156
+ context: Context<TContext>;
157
+ /**
158
+ * Additional details about the agent output.
159
+ */
160
+ details?: {
161
+ /** Model response associated with the output if available. */
162
+ modelResponse?: LanguageModelResponse;
163
+ };
164
+ }
165
+ /**
166
+ * A function that takes an output guardrail function arguments and returns a `GuardrailFunctionOutput`.
167
+ */
168
+ export type OutputGuardrailFunction<
169
+ TResponse extends AgentResponseType = TextResponse,
170
+ > = (
171
+ args: OutputGuardrailFunctionArgs<UnknownContext, TResponse>,
172
+ ) => Promise<GuardrailFunctionOutput>;
173
+
174
+ /**
175
+ * A guardrail that checks the output of the agent.
176
+ */
177
+ export interface OutputGuardrail<
178
+ TResponse extends AgentResponseType = TextResponse,
179
+ > {
180
+ /**
181
+ * The name of the guardrail.
182
+ */
183
+ name: string;
184
+
185
+ /**
186
+ * The function that performs the guardrail check.
187
+ */
188
+ execute: OutputGuardrailFunction<TResponse>;
189
+ }
190
+
191
+ /**
192
+ * Metadata for an output guardrail.
193
+ */
194
+ export interface OutputGuardrailMetadata {
195
+ type: "output";
196
+ name: string;
197
+ }
198
+
199
+ /**
200
+ * The result of an output guardrail execution.
201
+ */
202
+ export interface OutputGuardrailResult<
203
+ TMeta = OutputGuardrailMetadata,
204
+ TResponse extends AgentResponseType = TextResponse,
205
+ > {
206
+ /**
207
+ * The metadata of the guardrail.
208
+ */
209
+ guardrail: TMeta;
210
+
211
+ /**
212
+ * The output of the agent that ran.
213
+ */
214
+ agentOutput: ResolvedAgentResponse<TResponse>; // ??
215
+
216
+ /**
217
+ * The agent that ran.
218
+ */
219
+ agent: Agent<UnknownContext, TResponse>;
220
+
221
+ /**
222
+ * The output of the guardrail.
223
+ */
224
+ output: GuardrailFunctionOutput;
225
+ }
226
+
227
+ /**
228
+ * Definition of an output guardrail.
229
+ */
230
+ export interface OutputGuardrailDefinition<
231
+ TMeta = OutputGuardrailMetadata,
232
+ TResponse extends AgentResponseType = TextResponse,
233
+ > extends OutputGuardrailMetadata {
234
+ guardrailFunction: OutputGuardrailFunction<TResponse>;
235
+ run(
236
+ args: OutputGuardrailFunctionArgs<UnknownContext, TResponse>,
237
+ ): Promise<OutputGuardrailResult<TMeta, TResponse>>;
238
+ }
239
+
240
+ /**
241
+ * Arguments for defining an output guardrail definition.
242
+ */
243
+ export interface DefineOutputGuardrailArgs<
244
+ TResponse extends AgentResponseType = TextResponse,
245
+ > {
246
+ name: string;
247
+ execute: OutputGuardrailFunction<TResponse>;
248
+ }
249
+
250
+ /**
251
+ * Creates an output guardrail definition.
252
+ */
253
+ export function defineOutputGuardrail<
254
+ TResponse extends AgentResponseType = TextResponse,
255
+ >({
256
+ name,
257
+ execute,
258
+ }: DefineOutputGuardrailArgs<TResponse>): OutputGuardrailDefinition<
259
+ OutputGuardrailMetadata,
260
+ TResponse
261
+ > {
262
+ return {
263
+ type: "output",
264
+ name,
265
+ guardrailFunction: execute,
266
+ async run(
267
+ args: OutputGuardrailFunctionArgs<UnknownContext, TResponse>,
268
+ ): Promise<OutputGuardrailResult<OutputGuardrailMetadata, TResponse>> {
269
+ return {
270
+ guardrail: { type: "output", name },
271
+ agent: args.agent,
272
+ agentOutput: args.agentOutput,
273
+ output: await execute(args),
274
+ };
275
+ },
276
+ };
277
+ }
package/src/index.ts ADDED
@@ -0,0 +1,3 @@
1
+ export { Kernl } from "./kernl";
2
+ export { Agent } from "./agent";
3
+ export type { Context } from "./context";
package/src/kernl.ts ADDED
@@ -0,0 +1,22 @@
1
+ import { Agent } from "./agent";
2
+ import { UnknownContext } from "./context";
3
+ import { KernlHooks } from "./lifecycle";
4
+
5
+ import type { AgentResponseType } from "./types/agent";
6
+
7
+ /**
8
+ * Central coordinator for the entire application.
9
+ *
10
+ * Orchestrates agent execution, including guardrails, tool calls, session persistence, and
11
+ * tracing.
12
+ */
13
+ export class Kernl extends KernlHooks<UnknownContext, AgentResponseType> {
14
+ private agents: Map<string, Agent> = new Map();
15
+
16
+ /**
17
+ * Registers a new agent with the kernl instance.
18
+ */
19
+ register(agent: Agent): void {
20
+ // TODO: Implement agent registration
21
+ }
22
+ }
package/src/lib/env.ts ADDED
@@ -0,0 +1,36 @@
1
+ import { z } from "zod";
2
+
3
+ /**
4
+ * Environment variable validation schema
5
+ *
6
+ * This ensures all environment variables are valid at startup.
7
+ * All variables are optional with sensible defaults.
8
+ */
9
+ const envSchema = z.object({
10
+ // --- Logging ---
11
+ LOG_LEVEL: z
12
+ .enum(["trace", "debug", "info", "warn", "error", "fatal"])
13
+ .default("info"),
14
+ KERNL_LOG_MODEL_DATA: z
15
+ .enum(["true", "false"])
16
+ .optional()
17
+ .transform((val) => val === "true"),
18
+ KERNL_LOG_TOOL_DATA: z
19
+ .enum(["true", "false"])
20
+ .optional()
21
+ .transform((val) => val === "true"),
22
+ });
23
+
24
+ /**
25
+ * Validated and type-safe environment variables
26
+ *
27
+ * @example
28
+ * import { env } from '@/env';
29
+ * console.log(env.LOG_LEVEL);
30
+ */
31
+ export const env = envSchema.parse(process.env);
32
+
33
+ /**
34
+ * Type of the validated environment variables
35
+ */
36
+ export type Env = z.infer<typeof envSchema>;
@@ -0,0 +1,158 @@
1
+ import {
2
+ InputGuardrailResult,
3
+ OutputGuardrailMetadata,
4
+ OutputGuardrailResult,
5
+ } from "@/guardrail";
6
+
7
+ import { randomID } from "@kernl/shared/lib";
8
+ // TODO: implement checkpointing/serialization
9
+ // import { SerializedThread } from "@/lib/serde/thread";
10
+ type SerializedThread = any;
11
+
12
+ import { AgentResponseType } from "@/types/agent";
13
+ import { TextResponse } from "@/types/thread";
14
+
15
+ /**
16
+ * Abstract base class for all `kernl` errors
17
+ * Requires subclasses to implement JSON serialization
18
+ */
19
+ export abstract class BaseError extends Error {
20
+ constructor(message: string) {
21
+ super(message);
22
+ this.name = this.constructor.name;
23
+ }
24
+
25
+ /**
26
+ * Convert error to JSON representation
27
+ * Must be implemented by subclasses
28
+ */
29
+ abstract toJSON(): Record<string, any>;
30
+ }
31
+
32
+ /**
33
+ * Runtime error with trace ID for debugging
34
+ */
35
+ export class RuntimeError extends BaseError {
36
+ public readonly traceId: string;
37
+
38
+ constructor(message: string, traceId?: string) {
39
+ super(message);
40
+ this.traceId = traceId || randomID();
41
+ }
42
+
43
+ toJSON(): Record<string, any> {
44
+ return {
45
+ name: this.name,
46
+ message: this.message,
47
+ traceId: this.traceId,
48
+ stack: this.stack,
49
+ };
50
+ }
51
+ }
52
+
53
+ /**
54
+ * Base class for all errors thrown by the library.
55
+ */
56
+ export abstract class AgentError extends RuntimeError {
57
+ thread?: SerializedThread;
58
+
59
+ constructor(message: string, state?: SerializedThread, traceId?: string) {
60
+ super(message, traceId);
61
+ this.thread = state;
62
+ }
63
+
64
+ override toJSON(): Record<string, any> {
65
+ return {
66
+ ...super.toJSON(),
67
+ thread: this.thread,
68
+ };
69
+ }
70
+ }
71
+
72
+ /**
73
+ * System error thrown when the library encounters an error that is not caused by the user's
74
+ * misconfiguration.
75
+ */
76
+ export class SystemError extends RuntimeError {}
77
+
78
+ /**
79
+ * Thrown due to user misconfiguration
80
+ *
81
+ * This error indicates that the library was not configured correctly by the user.
82
+ * Common causes include invalid configuration options, missing required fields,
83
+ * or incompatible configuration combinations.
84
+ */
85
+ export class MisconfiguredError extends BaseError {
86
+ toJSON(): Record<string, any> {
87
+ return {
88
+ name: this.name,
89
+ message: this.message,
90
+ stack: this.stack,
91
+ };
92
+ }
93
+ }
94
+
95
+ /**
96
+ * Error thrown when the maximum number of turns is exceeded.
97
+ */
98
+ export class MaxTurnsExceededError extends AgentError {}
99
+
100
+ /**
101
+ * Error thrown when a model behavior is unexpected.
102
+ */
103
+ export class ModelBehaviorError extends AgentError {}
104
+
105
+ /**
106
+ * Error thrown when a guardrail execution fails.
107
+ */
108
+ export class GuardrailExecutionError extends AgentError {
109
+ error: Error;
110
+ constructor(message: string, error: Error, thread?: SerializedThread) {
111
+ super(message, thread);
112
+ this.error = error;
113
+ }
114
+ }
115
+
116
+ /**
117
+ * Error thrown when a tool call fails.
118
+ */
119
+ export class ToolCallError extends AgentError {
120
+ error: Error;
121
+ constructor(message: string, error: Error, thread?: SerializedThread) {
122
+ super(message, thread);
123
+ this.error = error;
124
+ }
125
+ }
126
+
127
+ /**
128
+ * Error thrown when an input guardrail tripwire is triggered.
129
+ */
130
+ export class InputGuardrailTripwireTriggered extends AgentError {
131
+ result: InputGuardrailResult;
132
+ constructor(
133
+ message: string,
134
+ result: InputGuardrailResult,
135
+ thread?: SerializedThread,
136
+ ) {
137
+ super(message, thread);
138
+ this.result = result;
139
+ }
140
+ }
141
+
142
+ /**
143
+ * Error thrown when an output guardrail tripwire is triggered.
144
+ */
145
+ export class OutputGuardrailTripwireTriggered<
146
+ TMeta extends OutputGuardrailMetadata,
147
+ TOutputType extends AgentResponseType = TextResponse,
148
+ > extends AgentError {
149
+ result: OutputGuardrailResult<TMeta, TOutputType>;
150
+ constructor(
151
+ message: string,
152
+ result: OutputGuardrailResult<TMeta, TOutputType>,
153
+ thread?: SerializedThread,
154
+ ) {
155
+ super(message, thread);
156
+ this.result = result;
157
+ }
158
+ }
@@ -0,0 +1,78 @@
1
+ import pino from "pino";
2
+
3
+ import { env } from "./env";
4
+
5
+ /**
6
+ * By default we don't log LLM inputs/outputs, to prevent exposing sensitive data.
7
+ * Set KERNL_LOG_MODEL_DATA=true to enable.
8
+ */
9
+ const dontLogModelData = !env.KERNL_LOG_MODEL_DATA;
10
+
11
+ /**
12
+ * By default we don't log tool inputs/outputs, to prevent exposing sensitive data.
13
+ * Set KERNL_LOG_TOOL_DATA=true to enable.
14
+ */
15
+ const dontLogToolData = !env.KERNL_LOG_TOOL_DATA;
16
+
17
+ /**
18
+ * Base pino logger instance
19
+ */
20
+ const base = pino({
21
+ level: env.LOG_LEVEL,
22
+ });
23
+
24
+ /**
25
+ * A logger instance with namespace support and sensitive data flags.
26
+ */
27
+ export type Logger = {
28
+ /**
29
+ * The namespace used for the logger.
30
+ */
31
+ namespace: string;
32
+
33
+ trace: (message: any, ...args: any[]) => void;
34
+ debug: (message: any, ...args: any[]) => void;
35
+ info: (message: any, ...args: any[]) => void;
36
+ warn: (message: any, ...args: any[]) => void;
37
+ error: (message: any, ...args: any[]) => void;
38
+ fatal: (message: any, ...args: any[]) => void;
39
+
40
+ /**
41
+ * Whether to log model data.
42
+ */
43
+ dontLogModelData: boolean;
44
+
45
+ /**
46
+ * Whether to log tool data.
47
+ */
48
+ dontLogToolData: boolean;
49
+ };
50
+
51
+ /**
52
+ * Get a logger for a given namespace.
53
+ *
54
+ * @param namespace - the namespace to use for the logger (e.g., 'kernl:core', 'kernl:agent').
55
+ * @returns A logger object with all pino log levels and sensitive data flags.
56
+ */
57
+ export function getLogger(namespace: string = "kernl"): Logger {
58
+ const child = base.child({ namespace });
59
+
60
+ return {
61
+ namespace,
62
+ trace: child.trace.bind(child),
63
+ debug: child.debug.bind(child),
64
+ info: child.info.bind(child),
65
+ warn: child.warn.bind(child),
66
+ error: child.error.bind(child),
67
+ fatal: child.fatal.bind(child),
68
+ dontLogModelData,
69
+ dontLogToolData,
70
+ };
71
+ }
72
+
73
+ /**
74
+ * Default logger instance for the core library.
75
+ */
76
+ export const logger = getLogger("kernl");
77
+
78
+ export default logger;
@@ -0,0 +1,18 @@
1
+ import { z } from "zod";
2
+
3
+ /**
4
+ * Represents a JSON-serializable value that can be safely stringified and sent to the model.
5
+ * Recursive type that allows nested structures.
6
+ */
7
+ export const JSONValue: z.ZodType<unknown> = z.lazy(() =>
8
+ z.union([
9
+ z.string(),
10
+ z.number(),
11
+ z.boolean(),
12
+ z.null(),
13
+ z.array(JSONValue),
14
+ z.record(z.string(), JSONValue),
15
+ ]),
16
+ );
17
+
18
+ export type JSONValue = z.infer<typeof JSONValue>;