kernl 0.2.1 → 0.6.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 (292) hide show
  1. package/.turbo/turbo-build.log +5 -4
  2. package/.turbo/turbo-check-types.log +4 -0
  3. package/CHANGELOG.md +147 -0
  4. package/LICENSE +1 -1
  5. package/dist/agent/__tests__/concurrency.test.d.ts +2 -0
  6. package/dist/agent/__tests__/concurrency.test.d.ts.map +1 -0
  7. package/dist/agent/__tests__/concurrency.test.js +152 -0
  8. package/dist/agent/__tests__/run.test.d.ts +2 -0
  9. package/dist/agent/__tests__/run.test.d.ts.map +1 -0
  10. package/dist/agent/__tests__/run.test.js +357 -0
  11. package/dist/agent/index.d.ts +1 -0
  12. package/dist/agent/index.d.ts.map +1 -0
  13. package/dist/agent.d.ts +35 -12
  14. package/dist/agent.d.ts.map +1 -1
  15. package/dist/agent.js +102 -15
  16. package/dist/api/__tests__/cursor-page.test.d.ts +2 -0
  17. package/dist/api/__tests__/cursor-page.test.d.ts.map +1 -0
  18. package/dist/api/__tests__/cursor-page.test.js +414 -0
  19. package/dist/api/__tests__/offset-page.test.d.ts +2 -0
  20. package/dist/api/__tests__/offset-page.test.d.ts.map +1 -0
  21. package/dist/api/__tests__/offset-page.test.js +510 -0
  22. package/dist/api/__tests__/threads.test.d.ts +2 -0
  23. package/dist/api/__tests__/threads.test.d.ts.map +1 -0
  24. package/dist/api/__tests__/threads.test.js +338 -0
  25. package/dist/api/models/index.d.ts +2 -0
  26. package/dist/api/models/index.d.ts.map +1 -0
  27. package/dist/api/models/thread.d.ts +120 -0
  28. package/dist/api/models/thread.d.ts.map +1 -0
  29. package/dist/api/pagination/base.d.ts +48 -0
  30. package/dist/api/pagination/base.d.ts.map +1 -0
  31. package/dist/api/pagination/base.js +45 -0
  32. package/dist/api/pagination/cursor.d.ts +44 -0
  33. package/dist/api/pagination/cursor.d.ts.map +1 -0
  34. package/dist/api/pagination/cursor.js +52 -0
  35. package/dist/api/pagination/offset.d.ts +42 -0
  36. package/dist/api/pagination/offset.d.ts.map +1 -0
  37. package/dist/api/pagination/offset.js +55 -0
  38. package/dist/api/resources/threads/events.d.ts +21 -0
  39. package/dist/api/resources/threads/events.d.ts.map +1 -0
  40. package/dist/api/resources/threads/events.js +24 -0
  41. package/dist/api/resources/threads/index.d.ts +4 -0
  42. package/dist/api/resources/threads/index.d.ts.map +1 -0
  43. package/dist/api/resources/threads/index.js +2 -0
  44. package/dist/api/resources/threads/threads.d.ts +57 -0
  45. package/dist/api/resources/threads/threads.d.ts.map +1 -0
  46. package/dist/api/resources/threads/threads.js +199 -0
  47. package/dist/api/resources/threads/types.d.ts +123 -0
  48. package/dist/api/resources/threads/types.d.ts.map +1 -0
  49. package/dist/api/resources/threads/utils.d.ts +18 -0
  50. package/dist/api/resources/threads/utils.d.ts.map +1 -0
  51. package/dist/api/resources/threads/utils.js +78 -0
  52. package/dist/context.d.ts +5 -1
  53. package/dist/context.d.ts.map +1 -1
  54. package/dist/context.js +6 -1
  55. package/dist/guardrail.d.ts +4 -4
  56. package/dist/index.d.ts +12 -4
  57. package/dist/index.d.ts.map +1 -1
  58. package/dist/index.js +10 -3
  59. package/dist/internal.d.ts +4 -0
  60. package/dist/internal.d.ts.map +1 -0
  61. package/dist/internal.js +2 -0
  62. package/dist/kernl/index.d.ts +3 -0
  63. package/dist/kernl/index.d.ts.map +1 -0
  64. package/dist/kernl/index.js +2 -0
  65. package/dist/kernl/kernl.d.ts +64 -0
  66. package/dist/kernl/kernl.d.ts.map +1 -0
  67. package/dist/kernl/kernl.js +116 -0
  68. package/dist/kernl/threads.d.ts +110 -0
  69. package/dist/kernl/threads.d.ts.map +1 -0
  70. package/dist/kernl/threads.js +126 -0
  71. package/dist/kernl.d.ts +27 -11
  72. package/dist/kernl.d.ts.map +1 -1
  73. package/dist/kernl.js +74 -11
  74. package/dist/lib/env.d.ts +3 -3
  75. package/dist/lib/env.js +1 -1
  76. package/dist/lib/error.d.ts +3 -3
  77. package/dist/lib/logger.js +1 -1
  78. package/dist/lifecycle.d.ts +5 -5
  79. package/dist/mcp/__tests__/base.test.js +2 -2
  80. package/dist/mcp/__tests__/fixtures/utils.d.ts +1 -1
  81. package/dist/mcp/__tests__/fixtures/utils.js +1 -1
  82. package/dist/mcp/__tests__/integration.test.js +16 -16
  83. package/dist/mcp/__tests__/stdio.test.js +2 -2
  84. package/dist/mcp/__tests__/utils.test.js +8 -8
  85. package/dist/mcp/base.d.ts +2 -2
  86. package/dist/mcp/http.d.ts +3 -3
  87. package/dist/mcp/http.d.ts.map +1 -1
  88. package/dist/mcp/http.js +11 -11
  89. package/dist/mcp/sse.d.ts +3 -3
  90. package/dist/mcp/sse.d.ts.map +1 -1
  91. package/dist/mcp/sse.js +9 -9
  92. package/dist/mcp/stdio.d.ts +2 -2
  93. package/dist/mcp/stdio.js +2 -2
  94. package/dist/mcp/types.d.ts +3 -3
  95. package/dist/mcp/utils.d.ts +4 -4
  96. package/dist/mcp/utils.d.ts.map +1 -1
  97. package/dist/mcp/utils.js +5 -6
  98. package/dist/storage/__tests__/in-memory.test.d.ts +2 -0
  99. package/dist/storage/__tests__/in-memory.test.d.ts.map +1 -0
  100. package/dist/storage/__tests__/in-memory.test.js +455 -0
  101. package/dist/storage/base.d.ts +64 -0
  102. package/dist/storage/base.d.ts.map +1 -0
  103. package/dist/storage/base.js +4 -0
  104. package/dist/storage/in-memory.d.ts +62 -0
  105. package/dist/storage/in-memory.d.ts.map +1 -0
  106. package/dist/storage/in-memory.js +283 -0
  107. package/dist/storage/index.d.ts +10 -0
  108. package/dist/storage/index.d.ts.map +1 -0
  109. package/dist/storage/index.js +7 -0
  110. package/dist/storage/thread.d.ts +123 -0
  111. package/dist/storage/thread.d.ts.map +1 -0
  112. package/dist/storage/thread.js +4 -0
  113. package/dist/task.d.ts +8 -6
  114. package/dist/task.d.ts.map +1 -1
  115. package/dist/task.js +10 -8
  116. package/dist/thread/__tests__/fixtures/mock-model.d.ts +1 -2
  117. package/dist/thread/__tests__/fixtures/mock-model.d.ts.map +1 -1
  118. package/dist/thread/__tests__/integration.test.js +10 -10
  119. package/dist/thread/__tests__/mock.d.ts +1 -1
  120. package/dist/thread/__tests__/namespace.test.d.ts +2 -0
  121. package/dist/thread/__tests__/namespace.test.d.ts.map +1 -0
  122. package/dist/thread/__tests__/namespace.test.js +131 -0
  123. package/dist/thread/__tests__/thread-persistence.test.d.ts +2 -0
  124. package/dist/thread/__tests__/thread-persistence.test.d.ts.map +1 -0
  125. package/dist/thread/__tests__/thread-persistence.test.js +351 -0
  126. package/dist/thread/__tests__/thread.test.js +55 -57
  127. package/dist/thread/index.d.ts +1 -1
  128. package/dist/thread/index.js +1 -1
  129. package/dist/thread/thread.d.ts +74 -22
  130. package/dist/thread/thread.d.ts.map +1 -1
  131. package/dist/thread/thread.js +212 -74
  132. package/dist/thread/utils.d.ts +38 -10
  133. package/dist/thread/utils.d.ts.map +1 -1
  134. package/dist/thread/utils.js +53 -9
  135. package/dist/tool/__tests__/fixtures.d.ts +8 -8
  136. package/dist/tool/__tests__/fixtures.js +3 -3
  137. package/dist/tool/__tests__/tool.test.js +2 -2
  138. package/dist/tool/__tests__/toolkit.test.js +17 -14
  139. package/dist/tool/index.d.ts +3 -3
  140. package/dist/tool/index.js +2 -2
  141. package/dist/tool/tool.d.ts +2 -2
  142. package/dist/tool/tool.js +5 -5
  143. package/dist/tool/toolkit.d.ts +4 -4
  144. package/dist/tool/toolkit.js +1 -1
  145. package/dist/tool/types.d.ts +4 -4
  146. package/dist/trace/traces.js +2 -2
  147. package/dist/types/agent.d.ts +4 -4
  148. package/dist/types/kernl.d.ts +42 -0
  149. package/dist/types/kernl.d.ts.map +1 -0
  150. package/dist/types/thread.d.ts +110 -24
  151. package/dist/types/thread.d.ts.map +1 -1
  152. package/dist/types/thread.js +12 -0
  153. package/package.json +12 -8
  154. package/src/agent/__tests__/concurrency.test.ts +194 -0
  155. package/src/agent/__tests__/run.test.ts +441 -0
  156. package/src/agent/index.ts +0 -0
  157. package/src/agent.ts +139 -24
  158. package/src/api/__tests__/cursor-page.test.ts +512 -0
  159. package/src/api/__tests__/offset-page.test.ts +624 -0
  160. package/src/api/__tests__/threads.test.ts +415 -0
  161. package/src/api/models/index.ts +6 -0
  162. package/src/api/models/thread.ts +138 -0
  163. package/src/api/pagination/base.ts +79 -0
  164. package/src/api/pagination/cursor.ts +86 -0
  165. package/src/api/pagination/offset.ts +89 -0
  166. package/src/api/resources/threads/events.ts +26 -0
  167. package/src/api/resources/threads/index.ts +9 -0
  168. package/src/api/resources/threads/threads.ts +256 -0
  169. package/src/api/resources/threads/types.ts +143 -0
  170. package/src/api/resources/threads/utils.ts +104 -0
  171. package/src/context.ts +10 -1
  172. package/src/index.ts +49 -1
  173. package/src/internal.ts +15 -0
  174. package/src/kernl.ts +86 -17
  175. package/src/mcp/__tests__/integration.test.ts +8 -9
  176. package/src/mcp/__tests__/utils.test.ts +6 -6
  177. package/src/mcp/http.ts +9 -9
  178. package/src/mcp/sse.ts +7 -7
  179. package/src/mcp/utils.ts +6 -5
  180. package/src/storage/__tests__/in-memory.test.ts +534 -0
  181. package/src/storage/base.ts +77 -0
  182. package/src/storage/in-memory.ts +372 -0
  183. package/src/storage/index.ts +21 -0
  184. package/src/storage/thread.ts +141 -0
  185. package/src/task.ts +12 -10
  186. package/src/thread/__tests__/fixtures/mock-model.ts +2 -4
  187. package/src/thread/__tests__/integration.test.ts +13 -12
  188. package/src/thread/__tests__/namespace.test.ts +158 -0
  189. package/src/thread/__tests__/thread-persistence.test.ts +367 -0
  190. package/src/thread/__tests__/thread.test.ts +52 -54
  191. package/src/thread/thread.ts +247 -96
  192. package/src/thread/utils.ts +76 -13
  193. package/src/tool/__tests__/fixtures.ts +1 -1
  194. package/src/tool/__tests__/toolkit.test.ts +15 -12
  195. package/src/tool/tool.ts +3 -3
  196. package/src/types/kernl.ts +51 -0
  197. package/src/types/thread.ts +139 -25
  198. package/vitest.config.ts +1 -0
  199. package/dist/env.d.ts +0 -45
  200. package/dist/env.d.ts.map +0 -1
  201. package/dist/env.js +0 -31
  202. package/dist/error.d.ts +0 -1
  203. package/dist/error.d.ts.map +0 -1
  204. package/dist/kernel.d.ts +0 -7
  205. package/dist/kernel.d.ts.map +0 -1
  206. package/dist/kernel.js +0 -7
  207. package/dist/lib/serde/__tests__/codec.test.d.ts +0 -2
  208. package/dist/lib/serde/__tests__/codec.test.d.ts.map +0 -1
  209. package/dist/lib/serde/__tests__/codec.test.js +0 -75
  210. package/dist/lib/serde/codec.d.ts +0 -12
  211. package/dist/lib/serde/codec.d.ts.map +0 -1
  212. package/dist/lib/serde/codec.js +0 -54
  213. package/dist/lib/serde/thread.d.ts +0 -1
  214. package/dist/lib/serde/thread.d.ts.map +0 -1
  215. package/dist/lib/serde/thread.js +0 -172
  216. package/dist/lib/serde/tool.d.ts +0 -36
  217. package/dist/lib/serde/tool.d.ts.map +0 -1
  218. package/dist/lib/utils.d.ts +0 -19
  219. package/dist/lib/utils.d.ts.map +0 -1
  220. package/dist/lib/utils.js +0 -41
  221. package/dist/logger.d.ts +0 -36
  222. package/dist/logger.d.ts.map +0 -1
  223. package/dist/logger.js +0 -43
  224. package/dist/mcp/__tests__/fixtures/echo-server.d.ts +0 -3
  225. package/dist/mcp/__tests__/fixtures/echo-server.d.ts.map +0 -1
  226. package/dist/mcp/__tests__/fixtures/echo-server.js +0 -92
  227. package/dist/mcp/__tests__/fixtures/math-server.d.ts +0 -3
  228. package/dist/mcp/__tests__/fixtures/math-server.d.ts.map +0 -1
  229. package/dist/mcp/__tests__/fixtures/math-server.js +0 -98
  230. package/dist/mcp/__tests__/fixtures/test-server.d.ts +0 -3
  231. package/dist/mcp/__tests__/fixtures/test-server.d.ts.map +0 -1
  232. package/dist/mcp/__tests__/fixtures/test-server.js +0 -163
  233. package/dist/mcp/__tests__/test-utils.d.ts +0 -17
  234. package/dist/mcp/__tests__/test-utils.d.ts.map +0 -1
  235. package/dist/mcp/__tests__/test-utils.js +0 -42
  236. package/dist/mcp/node.d.ts +0 -60
  237. package/dist/mcp/node.d.ts.map +0 -1
  238. package/dist/mcp/node.js +0 -297
  239. package/dist/model.d.ts +0 -175
  240. package/dist/model.d.ts.map +0 -1
  241. package/dist/providers/ai.d.ts +0 -1
  242. package/dist/providers/ai.d.ts.map +0 -1
  243. package/dist/providers/ai.js +0 -1
  244. package/dist/providers/default.d.ts +0 -16
  245. package/dist/providers/default.d.ts.map +0 -1
  246. package/dist/providers/default.js +0 -17
  247. package/dist/providers/registry.d.ts +0 -1
  248. package/dist/providers/registry.d.ts.map +0 -1
  249. package/dist/providers/registry.js +0 -1
  250. package/dist/sched/scheduler.d.ts +0 -20
  251. package/dist/sched/scheduler.d.ts.map +0 -1
  252. package/dist/sched/task.d.ts +0 -92
  253. package/dist/sched/task.d.ts.map +0 -1
  254. package/dist/sched/task.js +0 -102
  255. package/dist/serde/__tests__/codec.test.d.ts +0 -2
  256. package/dist/serde/__tests__/codec.test.d.ts.map +0 -1
  257. package/dist/serde/__tests__/codec.test.js +0 -75
  258. package/dist/serde/codec.d.ts +0 -12
  259. package/dist/serde/codec.d.ts.map +0 -1
  260. package/dist/serde/codec.js +0 -54
  261. package/dist/serde/json.d.ts +0 -8
  262. package/dist/serde/json.d.ts.map +0 -1
  263. package/dist/serde/json.js +0 -13
  264. package/dist/serde/thread.d.ts +0 -687
  265. package/dist/serde/thread.d.ts.map +0 -1
  266. package/dist/serde/thread.js +0 -158
  267. package/dist/serde/tool.d.ts +0 -36
  268. package/dist/serde/tool.d.ts.map +0 -1
  269. package/dist/session.d.ts +0 -1
  270. package/dist/session.d.ts.map +0 -1
  271. package/dist/session.js +0 -1
  272. package/dist/thread/__tests__/stream.test.d.ts +0 -2
  273. package/dist/thread/__tests__/stream.test.d.ts.map +0 -1
  274. package/dist/thread/__tests__/stream.test.js +0 -244
  275. package/dist/tool/mcp.d.ts +0 -75
  276. package/dist/tool/mcp.d.ts.map +0 -1
  277. package/dist/tool/mcp.js +0 -111
  278. package/dist/tools.d.ts +0 -362
  279. package/dist/tools.d.ts.map +0 -1
  280. package/dist/tools.js +0 -220
  281. package/dist/types/proto.d.ts +0 -1551
  282. package/dist/types/proto.d.ts.map +0 -1
  283. package/dist/types/proto.js +0 -531
  284. package/dist/usage.d.ts +0 -43
  285. package/dist/usage.d.ts.map +0 -1
  286. package/dist/usage.js +0 -61
  287. package/src/lib/serde/thread.ts +0 -188
  288. /package/dist/{error.js → agent/index.js} +0 -0
  289. /package/dist/{lib/serde/tool.js → api/models/index.js} +0 -0
  290. /package/dist/{model.js → api/models/thread.js} +0 -0
  291. /package/dist/{sched/scheduler.js → api/resources/threads/types.js} +0 -0
  292. /package/dist/{serde/tool.js → types/kernl.js} +0 -0
package/dist/tools.d.ts DELETED
@@ -1,362 +0,0 @@
1
- import { z } from "zod";
2
- import type { Agent } from "./agent";
3
- import type { RunResult } from "./result";
4
- import { RunToolApprovalItem, RunToolCallOutputItem } from "./items";
5
- import * as protocol from "./types/proto";
6
- import { Context, UnknownContext } from "./context";
7
- export type { ToolOutputText, ToolOutputImage, ToolOutputFileContent, ToolCallStructuredOutput, ToolCallOutputContent, } from "./types/proto";
8
- /**
9
- * A function that determines if a tool call should be approved.
10
- *
11
- * @param runContext The current run context
12
- * @param input The input to the tool
13
- * @param callId The ID of the tool call
14
- * @returns True if the tool call should be approved, false otherwise
15
- */
16
- export type ToolApprovalFunction<TParameters extends ToolInputParameters> = (context: Context, input: ToolExecuteArgument<TParameters>, callId?: string) => Promise<boolean>;
17
- export type ToolEnabledFunction<TContext = UnknownContext> = (context: Context<TContext>, agent: Agent<any, any>) => Promise<boolean>;
18
- type ToolEnabledPredicate<TContext = UnknownContext> = (args: {
19
- context: Context<TContext>;
20
- agent: Agent<any, any>;
21
- }) => boolean | Promise<boolean>;
22
- type ToolEnabledOption<Context = UnknownContext> = boolean | ToolEnabledPredicate<Context>;
23
- /**
24
- * Exposes a function to the agent as a tool to be called
25
- *
26
- * @param Context The context of the tool
27
- * @param Result The result of the tool
28
- */
29
- export type FunctionTool<TContext = UnknownContext, TParameters extends ToolInputParameters = undefined, Result = unknown> = {
30
- type: "function";
31
- /**
32
- * The name of the tool.
33
- */
34
- name: string;
35
- /**
36
- * The description of the tool that helps the model to understand when to use the tool
37
- */
38
- description: string;
39
- /**
40
- * A JSON schema describing the parameters of the tool.
41
- */
42
- parameters: JsonObjectSchema<any>;
43
- /**
44
- * Whether the tool is strict. If true, the model must try to strictly follow the schema (might result in slower response times).
45
- */
46
- strict: boolean;
47
- /**
48
- * The function to invoke when the tool is called.
49
- */
50
- invoke: (context: Context<TContext>, input: string, details?: {
51
- toolCall: protocol.FunctionCallItem;
52
- }) => Promise<string | Result>;
53
- /**
54
- * Whether the tool needs human approval before it can be called. If this is true, the run will result in an `interruption` that the
55
- * program has to resolve by approving or rejecting the tool call.
56
- */
57
- needsApproval: ToolApprovalFunction<TParameters>;
58
- /**
59
- * Determines whether the tool should be made available to the model for the current run.
60
- */
61
- isEnabled: ToolEnabledFunction<Context>;
62
- };
63
- /**
64
- * Exposes a computer to the model as a tool to be called
65
- *
66
- * @param Context The context of the tool
67
- * @param Result The result of the tool
68
- */
69
- export type ComputerTool = {
70
- type: "computer";
71
- /**
72
- * The name of the tool.
73
- */
74
- name: "computer_use_preview" | string;
75
- };
76
- export type HostedMCPApprovalFunction<TContext = UnknownContext> = (context: Context<TContext>, data: RunToolApprovalItem) => Promise<{
77
- approve: boolean;
78
- reason?: string;
79
- }>;
80
- /**
81
- * A hosted MCP tool that lets the model call a remote MCP server directly
82
- * without a round trip back to your code.
83
- */
84
- export type HostedMCPTool<TContext = UnknownContext> = HostedTool & {
85
- name: "hosted_mcp";
86
- providerData: ProviderData.HostedMCPTool<TContext>;
87
- };
88
- /**
89
- * Creates a hosted MCP tool definition.
90
- *
91
- * @param options - Configuration for the hosted MCP tool, including server connection details
92
- * and approval requirements.
93
- */
94
- export declare function hostedMcpTool<TContext = UnknownContext>(options: {
95
- allowedTools?: string[] | {
96
- toolNames?: string[];
97
- };
98
- } & ({
99
- serverLabel: string;
100
- serverUrl?: string;
101
- authorization?: string;
102
- headers?: Record<string, string>;
103
- } | {
104
- serverLabel: string;
105
- connectorId: string;
106
- authorization?: string;
107
- headers?: Record<string, string>;
108
- }) & ({
109
- requireApproval?: never;
110
- } | {
111
- requireApproval: "never";
112
- } | {
113
- requireApproval: "always" | {
114
- never?: {
115
- toolNames: string[];
116
- };
117
- always?: {
118
- toolNames: string[];
119
- };
120
- };
121
- onApproval?: HostedMCPApprovalFunction<Context>;
122
- })): HostedMCPTool<Context>;
123
- /**
124
- * A built-in hosted tool that will be executed directly by the model during the request and won't result in local code executions.
125
- * Examples of these are `web_search_call` or `file_search_call`.
126
- *
127
- * @param Context The context of the tool
128
- * @param Result The result of the tool
129
- */
130
- export type HostedTool = {
131
- type: "hosted_tool";
132
- /**
133
- * A unique name for the tool.
134
- */
135
- name: string;
136
- /**
137
- * Additional configuration data that gets passed to the tool
138
- */
139
- providerData?: Record<string, any>;
140
- };
141
- /**
142
- * A tool that can be called by the model.
143
- * @template Context The context passed to the tool
144
- */
145
- export type Tool<Context = unknown> = FunctionTool<Context, any, any> | ComputerTool | HostedTool;
146
- /**
147
- * The result of invoking a function tool. Either the actual output of the execution or a tool
148
- * approval request.
149
- *
150
- * These get passed for example to the `toolUseBehavior` option of the `Agent` constructor.
151
- */
152
- export type FunctionToolResult<TContext = UnknownContext, TParameters extends ToolInputParameters = any, Result = any> = {
153
- type: "function_output";
154
- /**
155
- * The tool that was called.
156
- */
157
- tool: FunctionTool<TContext, TParameters, Result>;
158
- /**
159
- * The output of the tool call. This can be a string or a stringifable item.
160
- */
161
- output: string | unknown;
162
- /**
163
- * The run item representing the tool call output.
164
- */
165
- runItem: RunToolCallOutputItem;
166
- /**
167
- * The result returned when the tool execution runs another agent. Populated when the
168
- * invocation originated from {@link Agent.asTool} and the nested agent completed a run.
169
- */
170
- agentRunResult?: RunResult<TContext, Agent<TContext, any>>;
171
- /**
172
- * Any interruptions collected while the nested agent executed. These are surfaced to allow
173
- * callers to pause and resume workflows that require approvals.
174
- */
175
- interruptions?: RunToolApprovalItem[];
176
- } | {
177
- /**
178
- * Indicates that the tool requires approval before it can be called.
179
- */
180
- type: "function_approval";
181
- /**
182
- * The tool that is requiring to be approved.
183
- */
184
- tool: FunctionTool<TContext, TParameters, Result>;
185
- /**
186
- * The item representing the tool call that is requiring approval.
187
- */
188
- runItem: RunToolApprovalItem;
189
- } | {
190
- /**
191
- * Indicates that the tool requires approval before it can be called.
192
- */
193
- type: "hosted_mcp_tool_approval";
194
- /**
195
- * The tool that is requiring to be approved.
196
- */
197
- tool: HostedMCPTool<TContext>;
198
- /**
199
- * The item representing the tool call that is requiring approval.
200
- */
201
- runItem: RunToolApprovalItem;
202
- };
203
- /**
204
- * The parameters of a tool.
205
- *
206
- * This can be a Zod schema, a JSON schema or undefined.
207
- *
208
- * If a Zod schema is provided, the arguments to the tool will automatically be parsed and validated
209
- * against the schema.
210
- *
211
- * If a JSON schema is provided, the arguments to the tool will be passed as is.
212
- *
213
- * If undefined is provided, the arguments to the tool will be passed as a string.
214
- */
215
- export type ToolInputParameters = undefined | ZodObjectLike | JsonObjectSchema<any>;
216
- /**
217
- * The parameters of a tool that has strict mode enabled.
218
- *
219
- * This can be a Zod schema, a JSON schema or undefined.
220
- *
221
- * If a Zod schema is provided, the arguments to the tool will automatically be parsed and validated
222
- * against the schema.
223
- *
224
- * If a JSON schema is provided, the arguments to the tool will be parsed as JSON but not validated.
225
- *
226
- * If undefined is provided, the arguments to the tool will be passed as a string.
227
- */
228
- export type ToolInputParametersStrict = undefined | ZodObjectLike | JsonObjectSchemaStrict<any>;
229
- /**
230
- * The parameters of a tool that has strict mode disabled.
231
- *
232
- * If a JSON schema is provided, the arguments to the tool will be parsed as JSON but not validated.
233
- *
234
- * Zod schemas are not supported without strict: true.
235
- */
236
- export type ToolInputParametersNonStrict = undefined | JsonObjectSchemaNonStrict<any>;
237
- /**
238
- * The arguments to a tool.
239
- *
240
- * The type of the arguments are derived from the parameters passed to the tool definition.
241
- *
242
- * If the parameters are passed as a JSON schema the type is `unknown`. For Zod schemas it will
243
- * match the inferred Zod type. Otherwise the type is `string`
244
- */
245
- export type ToolExecuteArgument<TParameters extends ToolInputParameters> = TParameters extends ZodObjectLike ? z.infer<TParameters> : TParameters extends JsonObjectSchema<any> ? unknown : string;
246
- /**
247
- * The function to invoke when the tool is called.
248
- *
249
- * @param input The arguments to the tool (see ToolExecuteArgument)
250
- * @param context An instance of the current RunContext
251
- */
252
- type ToolExecuteFunction<TContext = UnknownContext, TParameters extends ToolInputParameters> = (context: Context<TContext>, input: ToolExecuteArgument<TParameters>, details?: {
253
- toolCall: protocol.FunctionCallItem;
254
- }) => Promise<unknown> | unknown;
255
- /**
256
- * The function to invoke when an error occurs while running the tool. This can be used to define
257
- * what the model should receive as tool output in case of an error. It can be used to provide
258
- * for example additional context or a fallback value.
259
- *
260
- * @param context An instance of the current RunContext
261
- * @param error The error that occurred
262
- */
263
- type ToolErrorFunction = (context: Context, error: Error | unknown) => Promise<string> | string;
264
- /**
265
- * The options for a tool that has strict mode enabled.
266
- *
267
- * @param TParameters The parameters of the tool
268
- * @param Context The context of the tool
269
- */
270
- type StrictToolOptions<TParameters extends ToolInputParametersStrict, Context = UnknownContext> = {
271
- /**
272
- * The name of the tool. Must be unique within the agent.
273
- */
274
- name?: string;
275
- /**
276
- * The description of the tool. This is used to help the model understand when to use the tool.
277
- */
278
- description: string;
279
- /**
280
- * A Zod schema or JSON schema describing the parameters of the tool.
281
- * If a Zod schema is provided, the arguments to the tool will automatically be parsed and validated
282
- * against the schema.
283
- */
284
- parameters: TParameters;
285
- /**
286
- * Whether the tool is strict. If true, the model must try to strictly follow the schema (might result in slower response times).
287
- */
288
- strict?: true;
289
- /**
290
- * The function to invoke when the tool is called.
291
- */
292
- execute: ToolExecuteFunction<TParameters, Context>;
293
- /**
294
- * The function to invoke when an error occurs while running the tool.
295
- */
296
- errorFunction?: ToolErrorFunction | null;
297
- /**
298
- * Whether the tool needs human approval before it can be called. If this is true, the run will result in an `interruption` that the
299
- * program has to resolve by approving or rejecting the tool call.
300
- */
301
- needsApproval?: boolean | ToolApprovalFunction<TParameters>;
302
- /**
303
- * Determines whether the tool should be exposed to the model for the current run.
304
- */
305
- isEnabled?: ToolEnabledOption<Context>;
306
- };
307
- /**
308
- * The options for a tool that has strict mode disabled.
309
- *
310
- * @param TParameters The parameters of the tool
311
- * @param Context The context of the tool
312
- */
313
- type NonStrictToolOptions<TParameters extends ToolInputParametersNonStrict, TContext = UnknownContext> = {
314
- /**
315
- * The name of the tool. Must be unique within the agent.
316
- */
317
- name?: string;
318
- /**
319
- * The description of the tool. This is used to help the model understand when to use the tool.
320
- */
321
- description: string;
322
- /**
323
- * A JSON schema of the tool. To use a Zod schema, you need to use a `strict` schema.
324
- */
325
- parameters: TParameters;
326
- /**
327
- * Whether the tool is strict If true, the model must try to strictly follow the schema (might result in slower response times).
328
- */
329
- strict: false;
330
- /**
331
- * The function to invoke when the tool is called.
332
- */
333
- execute: ToolExecuteFunction<TContext, TParameters>;
334
- /**
335
- * The function to invoke when an error occurs while running the tool.
336
- */
337
- errorFunction?: ToolErrorFunction | null;
338
- /**
339
- * Whether the tool needs human approval before it can be called. If this is true, the run will result in an `interruption` that the
340
- * program has to resolve by approving or rejecting the tool call.
341
- */
342
- needsApproval?: boolean | ToolApprovalFunction<TParameters>;
343
- /**
344
- * Determines whether the tool should be exposed to the model for the current run.
345
- */
346
- isEnabled?: ToolEnabledOption<TContext>;
347
- };
348
- /**
349
- * The options for a tool.
350
- *
351
- * @param TContext The context of the tool
352
- * @param TParameters The parameters of the tool
353
- */
354
- export type ToolOptions<TContext = UnknownContext, TParameters extends ToolInputParameters> = StrictToolOptions<Extract<TParameters, ToolInputParametersStrict>, TContext> | NonStrictToolOptions<Extract<TParameters, ToolInputParametersNonStrict>, TContext>;
355
- /**
356
- * Exposes a function to the agent as a tool to be called
357
- *
358
- * @param options The options for the tool
359
- * @returns A new tool
360
- */
361
- export declare function tool<TContext = UnknownContext, TParameters extends ToolInputParameters = undefined, Result = string>(options: ToolOptions<TContext, TParameters>): FunctionTool<TContext, TParameters, Result>;
362
- //# sourceMappingURL=tools.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"tools.d.ts","sourceRoot":"","sources":["../src/tools.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAExB,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,SAAS,CAAC;AACrC,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,UAAU,CAAC;AAC1C,OAAO,EAAE,mBAAmB,EAAE,qBAAqB,EAAE,MAAM,SAAS,CAAC;AAYrE,OAAO,KAAK,QAAQ,MAAM,eAAe,CAAC;AAE1C,OAAO,EAAE,OAAO,EAAE,cAAc,EAAE,MAAM,WAAW,CAAC;AAEpD,YAAY,EACV,cAAc,EACd,eAAe,EACf,qBAAqB,EACrB,wBAAwB,EACxB,qBAAqB,GACtB,MAAM,eAAe,CAAC;AAEvB;;;;;;;GAOG;AACH,MAAM,MAAM,oBAAoB,CAAC,WAAW,SAAS,mBAAmB,IAAI,CAC1E,OAAO,EAAE,OAAO,EAChB,KAAK,EAAE,mBAAmB,CAAC,WAAW,CAAC,EACvC,MAAM,CAAC,EAAE,MAAM,KACZ,OAAO,CAAC,OAAO,CAAC,CAAC;AAEtB,MAAM,MAAM,mBAAmB,CAAC,QAAQ,GAAG,cAAc,IAAI,CAC3D,OAAO,EAAE,OAAO,CAAC,QAAQ,CAAC,EAC1B,KAAK,EAAE,KAAK,CAAC,GAAG,EAAE,GAAG,CAAC,KACnB,OAAO,CAAC,OAAO,CAAC,CAAC;AAEtB,KAAK,oBAAoB,CAAC,QAAQ,GAAG,cAAc,IAAI,CAAC,IAAI,EAAE;IAC5D,OAAO,EAAE,OAAO,CAAC,QAAQ,CAAC,CAAC;IAC3B,KAAK,EAAE,KAAK,CAAC,G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OAAO,EAAE,WAAW,CAAC,QAAQ,EAAE,WAAW,CAAC,GAC1C,YAAY,CAAC,QAAQ,EAAE,WAAW,EAAE,MAAM,CAAC,CA4G7C"}
package/dist/tools.js DELETED
@@ -1,220 +0,0 @@
1
- import logger from "./logger";
2
- import { getCurrentSpan } from "./tracing";
3
- // --- utils ---
4
- import { safeExecute } from "./utils/safeExecute";
5
- import { toFunctionToolName } from "./utils/tools";
6
- import { getSchemaAndParserFromInputType } from "./utils/tools";
7
- import { isZodObject } from "./utils/typeGuards";
8
- /**
9
- * Creates a hosted MCP tool definition.
10
- *
11
- * @param options - Configuration for the hosted MCP tool, including server connection details
12
- * and approval requirements.
13
- */
14
- export function hostedMcpTool(options) {
15
- if ("serverUrl" in options) {
16
- // the MCP servers comaptible with the specification
17
- const providerData = typeof options.requireApproval === "undefined" ||
18
- options.requireApproval === "never"
19
- ? {
20
- type: "mcp",
21
- server_label: options.serverLabel,
22
- server_url: options.serverUrl,
23
- authorization: options.authorization,
24
- require_approval: "never",
25
- allowed_tools: toMcpAllowedToolsFilter(options.allowedTools),
26
- headers: options.headers,
27
- }
28
- : {
29
- type: "mcp",
30
- server_label: options.serverLabel,
31
- server_url: options.serverUrl,
32
- authorization: options.authorization,
33
- allowed_tools: toMcpAllowedToolsFilter(options.allowedTools),
34
- headers: options.headers,
35
- require_approval: typeof options.requireApproval === "string"
36
- ? "always"
37
- : buildRequireApproval(options.requireApproval),
38
- on_approval: options.onApproval,
39
- };
40
- return {
41
- type: "hosted_tool",
42
- name: "hosted_mcp",
43
- providerData,
44
- };
45
- }
46
- else if ("connectorId" in options) {
47
- // OpenAI's connectors
48
- const providerData = typeof options.requireApproval === "undefined" ||
49
- options.requireApproval === "never"
50
- ? {
51
- type: "mcp",
52
- server_label: options.serverLabel,
53
- connector_id: options.connectorId,
54
- authorization: options.authorization,
55
- require_approval: "never",
56
- allowed_tools: toMcpAllowedToolsFilter(options.allowedTools),
57
- headers: options.headers,
58
- }
59
- : {
60
- type: "mcp",
61
- server_label: options.serverLabel,
62
- connector_id: options.connectorId,
63
- authorization: options.authorization,
64
- allowed_tools: toMcpAllowedToolsFilter(options.allowedTools),
65
- headers: options.headers,
66
- require_approval: typeof options.requireApproval === "string"
67
- ? "always"
68
- : buildRequireApproval(options.requireApproval),
69
- on_approval: options.onApproval,
70
- };
71
- return {
72
- type: "hosted_tool",
73
- name: "hosted_mcp",
74
- providerData,
75
- };
76
- }
77
- else {
78
- // the MCP servers comaptible with the specification
79
- const providerData = typeof options.requireApproval === "undefined" ||
80
- options.requireApproval === "never"
81
- ? {
82
- type: "mcp",
83
- server_label: options.serverLabel,
84
- require_approval: "never",
85
- allowed_tools: toMcpAllowedToolsFilter(options.allowedTools),
86
- }
87
- : {
88
- type: "mcp",
89
- server_label: options.serverLabel,
90
- allowed_tools: toMcpAllowedToolsFilter(options.allowedTools),
91
- require_approval: typeof options.requireApproval === "string"
92
- ? "always"
93
- : buildRequireApproval(options.requireApproval),
94
- on_approval: options.onApproval,
95
- };
96
- return {
97
- type: "hosted_tool",
98
- name: "hosted_mcp",
99
- providerData,
100
- };
101
- }
102
- }
103
- /**
104
- * The default function to invoke when an error occurs while running the tool.
105
- *
106
- * Always returns `An error occurred while running the tool. Please try again. Error: <error details>`
107
- *
108
- * @param context An instance of the current RunContext
109
- * @param error The error that occurred
110
- */
111
- function defaultToolErrorFunction(context, error) {
112
- const details = error instanceof Error ? error.toString() : String(error);
113
- return `An error occurred while running the tool. Please try again. Error: ${details}`;
114
- }
115
- /**
116
- * Exposes a function to the agent as a tool to be called
117
- *
118
- * @param options The options for the tool
119
- * @returns A new tool
120
- */
121
- export function tool(options) {
122
- const name = options.name
123
- ? toFunctionToolName(options.name)
124
- : toFunctionToolName(options.execute.name);
125
- const toolErrorFunction = typeof options.errorFunction === "undefined"
126
- ? defaultToolErrorFunction
127
- : options.errorFunction;
128
- if (!name) {
129
- throw new Error("Tool name cannot be empty. Either name your function or provide a name in the options.");
130
- }
131
- const strictMode = options.strict ?? true;
132
- if (!strictMode && isZodObject(options.parameters)) {
133
- throw new UserError("Strict mode is required for Zod parameters");
134
- }
135
- const { parser, schema: parameters } = getSchemaAndParserFromInputType(options.parameters, name);
136
- async function _invoke(context, input, details) {
137
- const [error, parsed] = await safeExecute(() => parser(input));
138
- if (error !== null) {
139
- if (logger.dontLogToolData) {
140
- logger.debug(`Invalid JSON input for tool ${name}`);
141
- }
142
- else {
143
- logger.debug(`Invalid JSON input for tool ${name}: ${input}`);
144
- }
145
- throw new ModelBehaviorError("Invalid JSON input for tool");
146
- }
147
- if (logger.dontLogToolData) {
148
- logger.debug(`Invoking tool ${name}`);
149
- }
150
- else {
151
- logger.debug(`Invoking tool ${name} with input ${input}`);
152
- }
153
- const result = await options.execute(parsed, context, details);
154
- const stringResult = toSmartString(result);
155
- if (logger.dontLogToolData) {
156
- logger.debug(`Tool ${name} completed`);
157
- }
158
- else {
159
- logger.debug(`Tool ${name} returned: ${stringResult}`);
160
- }
161
- return result;
162
- }
163
- async function invoke(context, input, details) {
164
- return _invoke(context, input, details).catch((error) => {
165
- if (toolErrorFunction) {
166
- const currentSpan = getCurrentSpan();
167
- currentSpan?.setError({
168
- message: "Error running tool (non-fatal)",
169
- data: {
170
- tool_name: name,
171
- error: error.toString(),
172
- },
173
- });
174
- return toolErrorFunction(context, error);
175
- }
176
- throw error;
177
- });
178
- }
179
- const needsApproval = typeof options.needsApproval === "function"
180
- ? options.needsApproval
181
- : async () => typeof options.needsApproval === "boolean"
182
- ? options.needsApproval
183
- : false;
184
- const isEnabled = typeof options.isEnabled === "function"
185
- ? async (context, agent) => {
186
- const predicate = options.isEnabled;
187
- const result = await predicate({ context, agent });
188
- return Boolean(result);
189
- }
190
- : async () => typeof options.isEnabled === "boolean" ? options.isEnabled : true;
191
- return {
192
- type: "function",
193
- name,
194
- description: options.description,
195
- parameters,
196
- strict: strictMode,
197
- invoke,
198
- needsApproval,
199
- isEnabled,
200
- };
201
- }
202
- function buildRequireApproval(requireApproval) {
203
- const result = {};
204
- if (requireApproval.always) {
205
- result.always = { tool_names: requireApproval.always.toolNames };
206
- }
207
- if (requireApproval.never) {
208
- result.never = { tool_names: requireApproval.never.toolNames };
209
- }
210
- return result;
211
- }
212
- function toMcpAllowedToolsFilter(allowedTools) {
213
- if (typeof allowedTools === "undefined") {
214
- return undefined;
215
- }
216
- if (Array.isArray(allowedTools)) {
217
- return { tool_names: allowedTools };
218
- }
219
- return { tool_names: allowedTools?.toolNames ?? [] };
220
- }