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,95 @@
1
+ import { Context, UnknownContext } from "../context";
2
+ import type { LanguageModelTool } from "@kernl/protocol";
3
+ import type { ToolConfig, ToolApprovalFunction, ToolEnabledFunction, ToolErrorFunction, ToolInputParameters, ToolResult } from "./types";
4
+ /**
5
+ * Exposes a function to the agent as a tool to be called
6
+ *
7
+ * @param config The options for the tool
8
+ * @returns A new tool instance
9
+ */
10
+ export declare function tool<TContext = UnknownContext, TParameters extends ToolInputParameters = undefined, TResult = string>(config: ToolConfig<TContext, TParameters, TResult>): FunctionTool<TContext, TParameters, TResult>;
11
+ /**
12
+ * Base class for all tools (function and hosted)
13
+ */
14
+ export declare abstract class BaseTool<TContext = UnknownContext> {
15
+ abstract readonly type: "function" | "hosted-tool";
16
+ abstract readonly id: string;
17
+ abstract readonly name?: string;
18
+ /**
19
+ * The function to invoke when an error occurs while running the tool.
20
+ */
21
+ abstract errorfn: ToolErrorFunction | null;
22
+ /**
23
+ * Whether the tool requires human approval before it can be called.
24
+ */
25
+ abstract requiresApproval: ToolApprovalFunction<any>;
26
+ /**
27
+ * Determines whether the tool should be exposed to the model for the current run.
28
+ */
29
+ abstract isEnabled(context: Context<TContext>, agent: any): Promise<boolean>;
30
+ /**
31
+ * Serialize this tool for sending to the model
32
+ */
33
+ abstract serialize(): LanguageModelTool;
34
+ }
35
+ /**
36
+ * A function tool that can be used by agents.
37
+ */
38
+ export declare class FunctionTool<TContext = UnknownContext, TParameters extends ToolInputParameters = undefined, TResult = unknown> extends BaseTool<TContext> {
39
+ readonly type: "function";
40
+ readonly id: string;
41
+ readonly name?: string;
42
+ readonly description: string;
43
+ readonly parameters?: TParameters;
44
+ readonly mode: "blocking" | "async";
45
+ private execute;
46
+ errorfn: ToolErrorFunction | null;
47
+ requiresApproval: ToolApprovalFunction<TParameters>;
48
+ isEnabled: ToolEnabledFunction<TContext>;
49
+ constructor(config: ToolConfig<TContext, TParameters, TResult>);
50
+ /**
51
+ * Main invocation method -
52
+ *
53
+ * Wraps execute with parsing, approval, and error handling
54
+ */
55
+ invoke(context: Context<TContext>, args: string, callId?: string): Promise<ToolResult<TResult>>;
56
+ /**
57
+ * Executes the tool with the provided execute() function
58
+ */
59
+ private _invoke;
60
+ /**
61
+ * Serialize this function tool for sending to the model
62
+ */
63
+ serialize(): LanguageModelTool;
64
+ }
65
+ /**
66
+ * Hosted tool executed server-side by the model provider
67
+ */
68
+ export declare class HostedTool extends BaseTool {
69
+ readonly type: "hosted-tool";
70
+ readonly id: string;
71
+ readonly name?: string;
72
+ readonly providerData?: Record<string, any>;
73
+ /**
74
+ * Hosted tools use the default error function
75
+ */
76
+ errorfn: ToolErrorFunction | null;
77
+ /**
78
+ * Hosted tools do not require approval by default
79
+ */
80
+ requiresApproval: ToolApprovalFunction<any>;
81
+ constructor(config: {
82
+ id: string;
83
+ name?: string;
84
+ providerData?: Record<string, any>;
85
+ });
86
+ /**
87
+ * Hosted tools are always enabled
88
+ */
89
+ isEnabled(): Promise<boolean>;
90
+ /**
91
+ * Serialize this hosted tool for sending to the model
92
+ */
93
+ serialize(): LanguageModelTool;
94
+ }
95
+ //# sourceMappingURL=tool.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"tool.d.ts","sourceRoot":"","sources":["../../src/tool/tool.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,cAAc,EAAE,MAAM,WAAW,CAAC;AAMpD,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AAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;KACpC;IAOD;;OAEG;IACG,SAAS,IAAI,OAAO,CAAC,OAAO,CAAC;IAInC;;OAEG;IACH,SAAS,IAAI,iBAAiB;CAQ/B"}
@@ -0,0 +1,176 @@
1
+ import { ModelBehaviorError } from "../lib/error";
2
+ import { logger } from "../lib/logger";
3
+ import { json } from "@kernl/shared/lib";
4
+ import { FAILED, COMPLETED, INTERRUPTIBLE } from "@kernl/protocol";
5
+ /**
6
+ * Exposes a function to the agent as a tool to be called
7
+ *
8
+ * @param config The options for the tool
9
+ * @returns A new tool instance
10
+ */
11
+ export function tool(config) {
12
+ return new FunctionTool(config);
13
+ }
14
+ /**
15
+ * Base class for all tools (function and hosted)
16
+ */
17
+ export class BaseTool {
18
+ }
19
+ /**
20
+ * A function tool that can be used by agents.
21
+ */
22
+ export class FunctionTool extends BaseTool {
23
+ type = "function";
24
+ id;
25
+ name;
26
+ description;
27
+ parameters;
28
+ mode;
29
+ execute;
30
+ errorfn;
31
+ requiresApproval;
32
+ isEnabled;
33
+ constructor(config) {
34
+ super();
35
+ this.id = config.id;
36
+ this.name = config.name;
37
+ this.description = config.description;
38
+ this.parameters = config.parameters;
39
+ this.mode = config.mode ?? "blocking";
40
+ this.execute = config.execute;
41
+ // setup error function
42
+ this.errorfn =
43
+ typeof config.errorfn === "undefined"
44
+ ? defaultToolErrorFunction
45
+ : config.errorfn;
46
+ // setup approval function
47
+ this.requiresApproval =
48
+ typeof config.requiresApproval === "function"
49
+ ? config.requiresApproval
50
+ : async () => typeof config.requiresApproval === "boolean"
51
+ ? config.requiresApproval
52
+ : false;
53
+ // setup enabled function
54
+ this.isEnabled =
55
+ typeof config.isEnabled === "function"
56
+ ? async (context, agent) => {
57
+ const predicate = config.isEnabled;
58
+ const result = await predicate({ context, agent });
59
+ return Boolean(result);
60
+ }
61
+ : async () => typeof config.isEnabled === "boolean" ? config.isEnabled : true;
62
+ }
63
+ /**
64
+ * Main invocation method -
65
+ *
66
+ * Wraps execute with parsing, approval, and error handling
67
+ */
68
+ async invoke(context, args, callId) {
69
+ return this._invoke(context, args, callId).catch((error) => {
70
+ const msg = this.errorfn
71
+ ? this.errorfn(context, error)
72
+ : error instanceof Error
73
+ ? error.message
74
+ : String(error);
75
+ return {
76
+ state: FAILED,
77
+ result: undefined,
78
+ error: msg,
79
+ };
80
+ });
81
+ }
82
+ /**
83
+ * Executes the tool with the provided execute() function
84
+ */
85
+ async _invoke(context, args, callId) {
86
+ let parsed = args;
87
+ if (this.parameters) {
88
+ try {
89
+ parsed = json(this.parameters).decode(args);
90
+ }
91
+ catch (error) {
92
+ logger.debug(`Invalid JSON input for tool ${this.id}: ${args}`);
93
+ throw new ModelBehaviorError("Invalid JSON input for tool");
94
+ }
95
+ }
96
+ // Check if approval is required
97
+ const needsApproval = await this.requiresApproval(context, parsed, callId);
98
+ const approvalStatus = callId ? context.approvals.get(callId) : undefined;
99
+ if (needsApproval && approvalStatus !== "approved") {
100
+ return {
101
+ state: INTERRUPTIBLE,
102
+ result: undefined,
103
+ error: null,
104
+ };
105
+ }
106
+ const result = await this.execute(context, parsed);
107
+ return {
108
+ state: COMPLETED,
109
+ result: result,
110
+ error: null,
111
+ };
112
+ }
113
+ /**
114
+ * Serialize this function tool for sending to the model
115
+ */
116
+ serialize() {
117
+ return {
118
+ kind: "function",
119
+ name: this.id,
120
+ description: this.description,
121
+ parameters: this.parameters, // TODO: convert Zod to JSON Schema
122
+ };
123
+ }
124
+ }
125
+ /**
126
+ * Hosted tool executed server-side by the model provider
127
+ */
128
+ export class HostedTool extends BaseTool {
129
+ type = "hosted-tool";
130
+ id;
131
+ name;
132
+ providerData;
133
+ /**
134
+ * Hosted tools use the default error function
135
+ */
136
+ errorfn = defaultToolErrorFunction;
137
+ /**
138
+ * Hosted tools do not require approval by default
139
+ */
140
+ requiresApproval = async () => false;
141
+ constructor(config) {
142
+ super();
143
+ this.id = config.id;
144
+ this.name = config.name;
145
+ this.providerData = config.providerData;
146
+ }
147
+ /**
148
+ * Hosted tools are always enabled
149
+ */
150
+ async isEnabled() {
151
+ return true;
152
+ }
153
+ /**
154
+ * Serialize this hosted tool for sending to the model
155
+ */
156
+ serialize() {
157
+ return {
158
+ kind: "provider-defined",
159
+ id: this.id,
160
+ name: this.name || this.id,
161
+ args: this.providerData || {},
162
+ };
163
+ }
164
+ }
165
+ /**
166
+ * The default function to invoke when an error occurs while running the tool.
167
+ *
168
+ * Always returns `An error occurred while running the tool. Please try again. Error: <error details>`
169
+ *
170
+ * @param context - An instance of the current Context
171
+ * @param error - The error that occurred
172
+ */
173
+ function defaultToolErrorFunction(context, error) {
174
+ const details = error instanceof Error ? error.toString() : String(error);
175
+ return `An error occurred while running the tool. Please try again. Error: ${details}`;
176
+ }
@@ -0,0 +1,121 @@
1
+ import type { Agent } from "../agent";
2
+ import type { Context, UnknownContext } from "../context";
3
+ import type { Tool } from ".";
4
+ import type { FunctionToolkitConfig, MCPToolkitConfig } from "./types";
5
+ /**
6
+ * A toolkit is a collection of related tools that can be used by an agent.
7
+ *
8
+ * Toolkits can be static (FunctionToolkit) or dynamic (MCPToolkit), and provide
9
+ * a unified interface for tool discovery and management.
10
+ */
11
+ export declare abstract class Toolkit<TContext = UnknownContext> {
12
+ /**
13
+ * Unique identifier for this toolkit
14
+ */
15
+ abstract readonly id: string;
16
+ /**
17
+ * The agent this toolkit is bound to (if any)
18
+ */
19
+ protected agent?: Agent<TContext, any>;
20
+ /**
21
+ * Bind this toolkit to an agent.
22
+ * Called by Agent constructor.
23
+ */
24
+ bind(agent: Agent<TContext, any>): void;
25
+ /**
26
+ * Get a specific tool by its ID.
27
+ *
28
+ * @param id The tool ID to look up
29
+ * @returns The tool if found, undefined otherwise
30
+ */
31
+ abstract get(id: string): Tool<TContext> | undefined;
32
+ /**
33
+ * List all tools available for the given context.
34
+ * If no context provided, returns all tools without filtering.
35
+ *
36
+ * @param context Optional context for filtering tools
37
+ * @returns Array of tools available in this toolkit
38
+ */
39
+ abstract list(context?: Context<TContext>): Promise<Tool<TContext>[]>;
40
+ /**
41
+ * Cleanup resources held by this toolkit.
42
+ * Override if your toolkit needs cleanup (e.g., closing connections).
43
+ * Default implementation does nothing.
44
+ */
45
+ destroy(): Promise<void>;
46
+ }
47
+ /**
48
+ * A toolkit containing static function tools.
49
+ *
50
+ * @example
51
+ * ```ts
52
+ * const fs = new FunctionToolkit({
53
+ * id: "fs",
54
+ * tools: [readFile, writeFile, listDir, ...]
55
+ * });
56
+ * ```
57
+ */
58
+ export declare class FunctionToolkit<TContext = UnknownContext> extends Toolkit<TContext> {
59
+ readonly id: string;
60
+ private tools;
61
+ /**
62
+ * Create a new function toolkit.
63
+ *
64
+ * @param config Toolkit configuration with id and tools array
65
+ */
66
+ constructor(config: FunctionToolkitConfig<TContext>);
67
+ /**
68
+ * Get a specific tool by ID.
69
+ *
70
+ * @param id The tool ID to look up
71
+ * @returns The tool if found, undefined otherwise
72
+ */
73
+ get(id: string): Tool<TContext> | undefined;
74
+ /**
75
+ * List all tools in this toolkit.
76
+ *
77
+ * @param context Optional context for filtering tools (currently unused)
78
+ * @returns Array of all tools in this toolkit
79
+ */
80
+ list(context?: Context<TContext>): Promise<Tool<TContext>[]>;
81
+ }
82
+ export declare class MCPToolkit<TContext = UnknownContext> extends Toolkit<TContext> {
83
+ readonly id: string;
84
+ private server;
85
+ private cache;
86
+ private filter;
87
+ private connected;
88
+ private cached;
89
+ /**
90
+ * Create a new MCP toolkit.
91
+ *
92
+ * @param config Toolkit configuration with id and server instance
93
+ */
94
+ constructor(config: MCPToolkitConfig<TContext>);
95
+ /**
96
+ * Get a specific tool by ID.
97
+ *
98
+ * Returns the tool from the local cache. The cache is populated on the first
99
+ * call to list(). Returns undefined if list() hasn't been called yet.
100
+ *
101
+ * @param id The tool ID to look up
102
+ * @returns The tool if found in cache, undefined otherwise
103
+ */
104
+ get(id: string): Tool<TContext> | undefined;
105
+ /**
106
+ * List all tools available from the MCP server.
107
+ *
108
+ * Connects to the server lazily on first call. Tools are cached locally after
109
+ * the first fetch. The MCP server itself also handles caching via the
110
+ * cacheToolsList option, so the network call is only made once.
111
+ *
112
+ * @param context Optional context for filtering tools
113
+ * @returns Array of tools from the MCP server
114
+ */
115
+ list(context?: Context<TContext>): Promise<Tool<TContext>[]>;
116
+ /**
117
+ * Cleanup resources and close the MCP server connection.
118
+ */
119
+ destroy(): Promise<void>;
120
+ }
121
+ //# sourceMappingURL=toolkit.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"toolkit.d.ts","sourceRoot":"","sources":["../../src/tool/toolkit.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,SAAS,CAAC;AACrC,OAAO,KAAK,EAAE,OAAO,EAAE,cAAc,EAAE,MAAM,WAAW,CAAC;AAMzD,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,GAAG,CAAC;AAC9B,OAAO,KAAK,EACV,qBAAqB,EACrB,gBAAgB,EAEjB,MAAM,SAAS,CAAC;AAEjB;;;;;GAKG;AACH,8BAAsB,OAAO,CAAC,QAAQ,GAAG,cAAc;IACrD;;OAEG;IACH,QAAQ,CAAC,QAAQ,CAAC,EAAE,EAAE,MAAM,CAAC;IAE7B;;OAEG;IACH,SAAS,CAAC,KAAK,CAAC,EAAE,KAAK,CAAC,QAAQ,EAAE,GAAG,CAAC,CAAC;IAEvC;;;OAGG;IACH,IAAI,CAAC,KAAK,EAAE,KAAK,CAAC,QAAQ,EAAE,GAAG,CAAC,GAAG,IAAI;IAIvC;;;;;OAKG;IACH,QAAQ,CAAC,GAAG,CAAC,EAAE,EAAE,MAAM,GAAG,IAAI,CAAC,QAAQ,CAAC,GAAG,SAAS;IAEpD;;;;;;OAMG;IACH,QAAQ,CAAC,IAAI,CAAC,OAAO,CAAC,EAAE,OAAO,CAAC,QAAQ,CAAC,GAAG,OAAO,CAAC,IAAI,CAAC,QAAQ,CAAC,EAAE,CAAC;IAErE;;;;OAIG;IACG,OAAO,IAA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@kernl/shared/lib";
3
+ /**
4
+ * A toolkit is a collection of related tools that can be used by an agent.
5
+ *
6
+ * Toolkits can be static (FunctionToolkit) or dynamic (MCPToolkit), and provide
7
+ * a unified interface for tool discovery and management.
8
+ */
9
+ export class Toolkit {
10
+ /**
11
+ * The agent this toolkit is bound to (if any)
12
+ */
13
+ agent;
14
+ /**
15
+ * Bind this toolkit to an agent.
16
+ * Called by Agent constructor.
17
+ */
18
+ bind(agent) {
19
+ this.agent = agent;
20
+ }
21
+ /**
22
+ * Cleanup resources held by this toolkit.
23
+ * Override if your toolkit needs cleanup (e.g., closing connections).
24
+ * Default implementation does nothing.
25
+ */
26
+ async destroy() {
27
+ // Default: no-op
28
+ }
29
+ }
30
+ /**
31
+ * A toolkit containing static function tools.
32
+ *
33
+ * @example
34
+ * ```ts
35
+ * const fs = new FunctionToolkit({
36
+ * id: "fs",
37
+ * tools: [readFile, writeFile, listDir, ...]
38
+ * });
39
+ * ```
40
+ */
41
+ export class FunctionToolkit extends Toolkit {
42
+ id;
43
+ tools;
44
+ /**
45
+ * Create a new function toolkit.
46
+ *
47
+ * @param config Toolkit configuration with id and tools array
48
+ */
49
+ constructor(config) {
50
+ super();
51
+ this.id = config.id;
52
+ this.tools = new Map(config.tools.map((t) => [t.id, t]));
53
+ }
54
+ /**
55
+ * Get a specific tool by ID.
56
+ *
57
+ * @param id The tool ID to look up
58
+ * @returns The tool if found, undefined otherwise
59
+ */
60
+ get(id) {
61
+ return this.tools.get(id);
62
+ }
63
+ /**
64
+ * List all tools in this toolkit.
65
+ *
66
+ * @param context Optional context for filtering tools (currently unused)
67
+ * @returns Array of all tools in this toolkit
68
+ */
69
+ async list(context) {
70
+ return Array.from(this.tools.values());
71
+ }
72
+ }
73
+ /*
74
+ * A toolkit that wraps an MCP server and provides tools from it.
75
+ *
76
+ * Handles connection lifecycle automatically - connects lazily on first tool request
77
+ * and provides cleanup via destroy().
78
+ *
79
+ * @example
80
+ * ```ts
81
+ * const server = new MCPServerStdio({
82
+ * id: "github",
83
+ * command: "npx",
84
+ * args: ["-y", "@modelcontextprotocol/server-github"],
85
+ * env: {
86
+ * GITHUB_TOKEN: process.env.GITHUB_TOKEN,
87
+ * },
88
+ * });
89
+ *
90
+ * const github = new MCPToolkit({
91
+ * id: "github",
92
+ * server,
93
+ * filter: async (ctx, tool) => {
94
+ * // Only allow certain tools
95
+ * return !tool.id.startsWith("dangerous_");
96
+ * },
97
+ * });
98
+ *
99
+ * const agent = new Agent({
100
+ * toolkits: [github],
101
+ * });
102
+ * ```
103
+ */
104
+ export class MCPToolkit extends Toolkit {
105
+ id;
106
+ server;
107
+ cache;
108
+ filter;
109
+ connected = false;
110
+ cached = false;
111
+ /**
112
+ * Create a new MCP toolkit.
113
+ *
114
+ * @param config Toolkit configuration with id and server instance
115
+ */
116
+ constructor(config) {
117
+ super();
118
+ this.id = config.id;
119
+ this.server = config.server;
120
+ this.filter = config.filter ?? (() => true);
121
+ this.cache = new Map();
122
+ }
123
+ /**
124
+ * Get a specific tool by ID.
125
+ *
126
+ * Returns the tool from the local cache. The cache is populated on the first
127
+ * call to list(). Returns undefined if list() hasn't been called yet.
128
+ *
129
+ * @param id The tool ID to look up
130
+ * @returns The tool if found in cache, undefined otherwise
131
+ */
132
+ get(id) {
133
+ return this.cache.get(id);
134
+ }
135
+ /**
136
+ * List all tools available from the MCP server.
137
+ *
138
+ * Connects to the server lazily on first call. Tools are cached locally after
139
+ * the first fetch. The MCP server itself also handles caching via the
140
+ * cacheToolsList option, so the network call is only made once.
141
+ *
142
+ * @param context Optional context for filtering tools
143
+ * @returns Array of tools from the MCP server
144
+ */
145
+ async list(context) {
146
+ if (!this.connected) {
147
+ await this.server.connect();
148
+ this.connected = true;
149
+ }
150
+ // lazy cache population
151
+ if (!this.cached) {
152
+ const mcpTools = await this.server.listTools();
153
+ for (const mcpTool of mcpTools) {
154
+ const tool = mcpToFunctionTool(this.server, mcpTool);
155
+ this.cache.set(tool.id, tool);
156
+ }
157
+ this.cached = true;
158
+ }
159
+ const tools = Array.from(this.cache.values());
160
+ // apply filter
161
+ if (context && this.agent) {
162
+ const ctx = { context, agent: this.agent, toolkitId: this.id };
163
+ return filter(tools, async (tool) => {
164
+ return await this.filter(ctx, tool);
165
+ });
166
+ }
167
+ return tools;
168
+ }
169
+ /**
170
+ * Cleanup resources and close the MCP server connection.
171
+ */
172
+ async destroy() {
173
+ if (this.connected) {
174
+ await this.server.close();
175
+ this.connected = false;
176
+ this.cache.clear();
177
+ this.cached = false;
178
+ }
179
+ }
180
+ }