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.
- package/.turbo/turbo-build.log +5 -0
- package/CHANGELOG.md +53 -0
- package/LICENSE +201 -0
- package/dist/agent.d.ts +43 -0
- package/dist/agent.d.ts.map +1 -0
- package/dist/agent.js +130 -0
- package/dist/context.d.ts +70 -0
- package/dist/context.d.ts.map +1 -0
- package/dist/context.js +111 -0
- package/dist/env.d.ts +45 -0
- package/dist/env.d.ts.map +1 -0
- package/dist/env.js +31 -0
- package/dist/error.d.ts +1 -0
- package/dist/error.d.ts.map +1 -0
- package/dist/error.js +1 -0
- package/dist/guardrail.d.ts +178 -0
- package/dist/guardrail.d.ts.map +1 -0
- package/dist/guardrail.js +34 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +2 -0
- package/dist/kernel.d.ts +7 -0
- package/dist/kernel.d.ts.map +1 -0
- package/dist/kernel.js +7 -0
- package/dist/kernl.d.ts +18 -0
- package/dist/kernl.d.ts.map +1 -0
- package/dist/kernl.js +16 -0
- package/dist/lib/env.d.ts +43 -0
- package/dist/lib/env.d.ts.map +1 -0
- package/dist/lib/env.js +29 -0
- package/dist/lib/error.d.ts +88 -0
- package/dist/lib/error.d.ts.map +1 -0
- package/dist/lib/error.js +117 -0
- package/dist/lib/logger.d.ts +36 -0
- package/dist/lib/logger.d.ts.map +1 -0
- package/dist/lib/logger.js +43 -0
- package/dist/lib/serde/__tests__/codec.test.d.ts +2 -0
- package/dist/lib/serde/__tests__/codec.test.d.ts.map +1 -0
- package/dist/lib/serde/__tests__/codec.test.js +75 -0
- package/dist/lib/serde/codec.d.ts +12 -0
- package/dist/lib/serde/codec.d.ts.map +1 -0
- package/dist/lib/serde/codec.js +54 -0
- package/dist/lib/serde/json.d.ts +8 -0
- package/dist/lib/serde/json.d.ts.map +1 -0
- package/dist/lib/serde/json.js +13 -0
- package/dist/lib/serde/thread.d.ts +1 -0
- package/dist/lib/serde/thread.d.ts.map +1 -0
- package/dist/lib/serde/thread.js +172 -0
- package/dist/lib/serde/tool.d.ts +36 -0
- package/dist/lib/serde/tool.d.ts.map +1 -0
- package/dist/lib/serde/tool.js +1 -0
- package/dist/lib/utils.d.ts +19 -0
- package/dist/lib/utils.d.ts.map +1 -0
- package/dist/lib/utils.js +41 -0
- package/dist/lifecycle.d.ts +133 -0
- package/dist/lifecycle.d.ts.map +1 -0
- package/dist/lifecycle.js +29 -0
- package/dist/logger.d.ts +36 -0
- package/dist/logger.d.ts.map +1 -0
- package/dist/logger.js +43 -0
- package/dist/mcp/__tests__/base.test.d.ts +2 -0
- package/dist/mcp/__tests__/base.test.d.ts.map +1 -0
- package/dist/mcp/__tests__/base.test.js +268 -0
- package/dist/mcp/__tests__/fixtures/echo-server.d.ts +3 -0
- package/dist/mcp/__tests__/fixtures/echo-server.d.ts.map +1 -0
- package/dist/mcp/__tests__/fixtures/echo-server.js +92 -0
- package/dist/mcp/__tests__/fixtures/math-server.d.ts +3 -0
- package/dist/mcp/__tests__/fixtures/math-server.d.ts.map +1 -0
- package/dist/mcp/__tests__/fixtures/math-server.js +98 -0
- package/dist/mcp/__tests__/fixtures/server.d.ts +3 -0
- package/dist/mcp/__tests__/fixtures/server.d.ts.map +1 -0
- package/dist/mcp/__tests__/fixtures/server.js +162 -0
- package/dist/mcp/__tests__/fixtures/test-server.d.ts +3 -0
- package/dist/mcp/__tests__/fixtures/test-server.d.ts.map +1 -0
- package/dist/mcp/__tests__/fixtures/test-server.js +163 -0
- package/dist/mcp/__tests__/fixtures/utils.d.ts +17 -0
- package/dist/mcp/__tests__/fixtures/utils.d.ts.map +1 -0
- package/dist/mcp/__tests__/fixtures/utils.js +42 -0
- package/dist/mcp/__tests__/integration.test.d.ts +2 -0
- package/dist/mcp/__tests__/integration.test.d.ts.map +1 -0
- package/dist/mcp/__tests__/integration.test.js +360 -0
- package/dist/mcp/__tests__/stdio.test.d.ts +2 -0
- package/dist/mcp/__tests__/stdio.test.d.ts.map +1 -0
- package/dist/mcp/__tests__/stdio.test.js +180 -0
- package/dist/mcp/__tests__/test-utils.d.ts +17 -0
- package/dist/mcp/__tests__/test-utils.d.ts.map +1 -0
- package/dist/mcp/__tests__/test-utils.js +42 -0
- package/dist/mcp/__tests__/utils.test.d.ts +2 -0
- package/dist/mcp/__tests__/utils.test.d.ts.map +1 -0
- package/dist/mcp/__tests__/utils.test.js +300 -0
- package/dist/mcp/base.d.ts +88 -0
- package/dist/mcp/base.d.ts.map +1 -0
- package/dist/mcp/base.js +68 -0
- package/dist/mcp/http.d.ts +34 -0
- package/dist/mcp/http.d.ts.map +1 -0
- package/dist/mcp/http.js +100 -0
- package/dist/mcp/node.d.ts +60 -0
- package/dist/mcp/node.d.ts.map +1 -0
- package/dist/mcp/node.js +297 -0
- package/dist/mcp/sse.d.ts +34 -0
- package/dist/mcp/sse.d.ts.map +1 -0
- package/dist/mcp/sse.js +97 -0
- package/dist/mcp/stdio.d.ts +32 -0
- package/dist/mcp/stdio.d.ts.map +1 -0
- package/dist/mcp/stdio.js +96 -0
- package/dist/mcp/types.d.ts +172 -0
- package/dist/mcp/types.d.ts.map +1 -0
- package/dist/mcp/types.js +16 -0
- package/dist/mcp/utils.d.ts +23 -0
- package/dist/mcp/utils.d.ts.map +1 -0
- package/dist/mcp/utils.js +44 -0
- package/dist/model.d.ts +175 -0
- package/dist/model.d.ts.map +1 -0
- package/dist/model.js +1 -0
- package/dist/providers/ai.d.ts +1 -0
- package/dist/providers/ai.d.ts.map +1 -0
- package/dist/providers/ai.js +1 -0
- package/dist/providers/default.d.ts +16 -0
- package/dist/providers/default.d.ts.map +1 -0
- package/dist/providers/default.js +17 -0
- package/dist/providers/registry.d.ts +1 -0
- package/dist/providers/registry.d.ts.map +1 -0
- package/dist/providers/registry.js +1 -0
- package/dist/sched/scheduler.d.ts +20 -0
- package/dist/sched/scheduler.d.ts.map +1 -0
- package/dist/sched/scheduler.js +1 -0
- package/dist/sched/task.d.ts +92 -0
- package/dist/sched/task.d.ts.map +1 -0
- package/dist/sched/task.js +102 -0
- package/dist/serde/__tests__/codec.test.d.ts +2 -0
- package/dist/serde/__tests__/codec.test.d.ts.map +1 -0
- package/dist/serde/__tests__/codec.test.js +75 -0
- package/dist/serde/codec.d.ts +12 -0
- package/dist/serde/codec.d.ts.map +1 -0
- package/dist/serde/codec.js +54 -0
- package/dist/serde/json.d.ts +8 -0
- package/dist/serde/json.d.ts.map +1 -0
- package/dist/serde/json.js +13 -0
- package/dist/serde/thread.d.ts +687 -0
- package/dist/serde/thread.d.ts.map +1 -0
- package/dist/serde/thread.js +158 -0
- package/dist/serde/tool.d.ts +36 -0
- package/dist/serde/tool.d.ts.map +1 -0
- package/dist/serde/tool.js +1 -0
- package/dist/session.d.ts +1 -0
- package/dist/session.d.ts.map +1 -0
- package/dist/session.js +1 -0
- package/dist/task.d.ts +87 -0
- package/dist/task.d.ts.map +1 -0
- package/dist/task.js +97 -0
- package/dist/thread/__tests__/mock.d.ts +28 -0
- package/dist/thread/__tests__/mock.d.ts.map +1 -0
- package/dist/thread/__tests__/mock.js +74 -0
- package/dist/thread/__tests__/thread.test.d.ts +2 -0
- package/dist/thread/__tests__/thread.test.d.ts.map +1 -0
- package/dist/thread/__tests__/thread.test.js +1412 -0
- package/dist/thread/index.d.ts +2 -0
- package/dist/thread/index.d.ts.map +1 -0
- package/dist/thread/index.js +1 -0
- package/dist/thread/thread.d.ts +66 -0
- package/dist/thread/thread.d.ts.map +1 -0
- package/dist/thread/thread.js +472 -0
- package/dist/thread/utils.d.ts +19 -0
- package/dist/thread/utils.d.ts.map +1 -0
- package/dist/thread/utils.js +50 -0
- package/dist/tool/__tests__/fixtures.d.ts +45 -0
- package/dist/tool/__tests__/fixtures.d.ts.map +1 -0
- package/dist/tool/__tests__/fixtures.js +97 -0
- package/dist/tool/__tests__/tool.test.d.ts +2 -0
- package/dist/tool/__tests__/tool.test.d.ts.map +1 -0
- package/dist/tool/__tests__/tool.test.js +172 -0
- package/dist/tool/__tests__/toolkit.test.d.ts +2 -0
- package/dist/tool/__tests__/toolkit.test.d.ts.map +1 -0
- package/dist/tool/__tests__/toolkit.test.js +134 -0
- package/dist/tool/index.d.ts +4 -0
- package/dist/tool/index.d.ts.map +1 -0
- package/dist/tool/index.js +2 -0
- package/dist/tool/mcp.d.ts +75 -0
- package/dist/tool/mcp.d.ts.map +1 -0
- package/dist/tool/mcp.js +111 -0
- package/dist/tool/tool.d.ts +95 -0
- package/dist/tool/tool.d.ts.map +1 -0
- package/dist/tool/tool.js +176 -0
- package/dist/tool/toolkit.d.ts +121 -0
- package/dist/tool/toolkit.d.ts.map +1 -0
- package/dist/tool/toolkit.js +180 -0
- package/dist/tool/types.d.ts +187 -0
- package/dist/tool/types.d.ts.map +1 -0
- package/dist/tool/types.js +1 -0
- package/dist/tools.d.ts +362 -0
- package/dist/tools.d.ts.map +1 -0
- package/dist/tools.js +220 -0
- package/dist/trace/processor.d.ts +1 -0
- package/dist/trace/processor.d.ts.map +1 -0
- package/dist/trace/processor.js +1 -0
- package/dist/trace/traces.d.ts +1 -0
- package/dist/trace/traces.d.ts.map +1 -0
- package/dist/trace/traces.js +73 -0
- package/dist/trace/utils.d.ts +22 -0
- package/dist/trace/utils.d.ts.map +1 -0
- package/dist/trace/utils.js +30 -0
- package/dist/types/agent.d.ts +91 -0
- package/dist/types/agent.d.ts.map +1 -0
- package/dist/types/agent.js +1 -0
- package/dist/types/proto.d.ts +1551 -0
- package/dist/types/proto.d.ts.map +1 -0
- package/dist/types/proto.js +531 -0
- package/dist/types/thread.d.ts +71 -0
- package/dist/types/thread.d.ts.map +1 -0
- package/dist/types/thread.js +5 -0
- package/dist/usage.d.ts +43 -0
- package/dist/usage.d.ts.map +1 -0
- package/dist/usage.js +61 -0
- package/package.json +52 -0
- package/src/agent.ts +203 -0
- package/src/context.ts +265 -0
- package/src/guardrail.ts +277 -0
- package/src/index.ts +3 -0
- package/src/kernl.ts +22 -0
- package/src/lib/env.ts +36 -0
- package/src/lib/error.ts +158 -0
- package/src/lib/logger.ts +78 -0
- package/src/lib/serde/json.ts +18 -0
- package/src/lib/serde/thread.ts +188 -0
- package/src/lifecycle.ts +181 -0
- package/src/mcp/__tests__/base.test.ts +344 -0
- package/src/mcp/__tests__/fixtures/server.ts +179 -0
- package/src/mcp/__tests__/fixtures/utils.ts +58 -0
- package/src/mcp/__tests__/integration.test.ts +447 -0
- package/src/mcp/__tests__/stdio.test.ts +236 -0
- package/src/mcp/__tests__/utils.test.ts +360 -0
- package/src/mcp/base.ts +162 -0
- package/src/mcp/http.ts +147 -0
- package/src/mcp/sse.ts +137 -0
- package/src/mcp/stdio.ts +136 -0
- package/src/mcp/types.ts +202 -0
- package/src/mcp/utils.ts +62 -0
- package/src/task.ts +119 -0
- package/src/thread/__tests__/mock.ts +95 -0
- package/src/thread/__tests__/thread.test.ts +1574 -0
- package/src/thread/index.ts +1 -0
- package/src/thread/thread.ts +611 -0
- package/src/thread/utils.ts +67 -0
- package/src/tool/__tests__/fixtures.ts +106 -0
- package/src/tool/__tests__/tool.test.ts +235 -0
- package/src/tool/__tests__/toolkit.test.ts +174 -0
- package/src/tool/index.ts +10 -0
- package/src/tool/tool.ts +264 -0
- package/src/tool/toolkit.ts +234 -0
- package/src/tool/types.ts +243 -0
- package/src/trace/processor.ts +0 -0
- package/src/trace/traces.ts +86 -0
- package/src/trace/utils.ts +38 -0
- package/src/types/agent.ts +145 -0
- package/src/types/thread.ts +86 -0
- package/tsconfig.json +13 -0
- package/vitest.config.ts +14 -0
package/dist/tools.js
ADDED
|
@@ -0,0 +1,220 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
//# sourceMappingURL=processor.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"processor.d.ts","sourceRoot":"","sources":["../../src/trace/processor.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
//# sourceMappingURL=traces.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"traces.d.ts","sourceRoot":"","sources":["../../src/trace/traces.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// import { defaultProcessor, TracingProcessor } from "./processor";
|
|
3
|
+
// import { generateTraceId } from "./utils";
|
|
4
|
+
// export type TraceOptions = {
|
|
5
|
+
// traceId?: string;
|
|
6
|
+
// name?: string;
|
|
7
|
+
// groupId?: string;
|
|
8
|
+
// metadata?: Record<string, any>;
|
|
9
|
+
// started?: boolean;
|
|
10
|
+
// };
|
|
11
|
+
// export class Trace {
|
|
12
|
+
// public type = "trace" as const;
|
|
13
|
+
// public traceId: string;
|
|
14
|
+
// public name: string;
|
|
15
|
+
// public groupId: string | null = null;
|
|
16
|
+
// public metadata?: Record<string, any>;
|
|
17
|
+
// #processor: TracingProcessor;
|
|
18
|
+
// #started: boolean;
|
|
19
|
+
// constructor(options: TraceOptions, processor?: TracingProcessor) {
|
|
20
|
+
// this.traceId = options.traceId ?? generateTraceId();
|
|
21
|
+
// this.name = options.name ?? "Agent workflow";
|
|
22
|
+
// this.groupId = options.groupId ?? null;
|
|
23
|
+
// this.metadata = options.metadata ?? {};
|
|
24
|
+
// this.#processor = processor ?? defaultProcessor();
|
|
25
|
+
// this.#started = options.started ?? false;
|
|
26
|
+
// }
|
|
27
|
+
// async start() {
|
|
28
|
+
// if (this.#started) {
|
|
29
|
+
// return;
|
|
30
|
+
// }
|
|
31
|
+
// this.#started = true;
|
|
32
|
+
// await this.#processor.onTraceStart(this);
|
|
33
|
+
// }
|
|
34
|
+
// async end() {
|
|
35
|
+
// if (!this.#started) {
|
|
36
|
+
// return;
|
|
37
|
+
// }
|
|
38
|
+
// this.#started = false;
|
|
39
|
+
// await this.#processor.onTraceEnd(this);
|
|
40
|
+
// }
|
|
41
|
+
// clone(): Trace {
|
|
42
|
+
// return new Trace({
|
|
43
|
+
// traceId: this.traceId,
|
|
44
|
+
// name: this.name,
|
|
45
|
+
// groupId: this.groupId ?? undefined,
|
|
46
|
+
// metadata: this.metadata,
|
|
47
|
+
// started: this.#started,
|
|
48
|
+
// });
|
|
49
|
+
// }
|
|
50
|
+
// toJSON(): object | null {
|
|
51
|
+
// return {
|
|
52
|
+
// object: this.type,
|
|
53
|
+
// id: this.traceId,
|
|
54
|
+
// workflow_name: this.name, // (TODO): why workflow?
|
|
55
|
+
// group_id: this.groupId,
|
|
56
|
+
// metadata: this.metadata,
|
|
57
|
+
// };
|
|
58
|
+
// }
|
|
59
|
+
// }
|
|
60
|
+
// export class NoopTrace extends Trace {
|
|
61
|
+
// constructor() {
|
|
62
|
+
// super({});
|
|
63
|
+
// }
|
|
64
|
+
// async start(): Promise<void> {
|
|
65
|
+
// return;
|
|
66
|
+
// }
|
|
67
|
+
// async end(): Promise<void> {
|
|
68
|
+
// return;
|
|
69
|
+
// }
|
|
70
|
+
// toJSON(): object | null {
|
|
71
|
+
// return null;
|
|
72
|
+
// }
|
|
73
|
+
// }
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Generate a trace ID using 16 random bytes (128-bit, 32 hex chars).
|
|
3
|
+
* @returns A trace ID prefixed with `trace_`.
|
|
4
|
+
*/
|
|
5
|
+
export declare function generateTraceId(): string;
|
|
6
|
+
/**
|
|
7
|
+
* Generate a span ID using 12 random bytes (96-bit, 24 hex chars).
|
|
8
|
+
* @returns A span ID prefixed with `span_`.
|
|
9
|
+
*/
|
|
10
|
+
export declare function generateSpanId(): string;
|
|
11
|
+
/**
|
|
12
|
+
* Generate a group ID using 12 random bytes (96-bit, 24 hex chars).
|
|
13
|
+
* @returns A group ID prefixed with `group_`.
|
|
14
|
+
*/
|
|
15
|
+
export declare function generateGroupId(): string;
|
|
16
|
+
/**
|
|
17
|
+
* Remove fields that start with an underscore from an object.
|
|
18
|
+
* @param obj - The object to remove private fields from.
|
|
19
|
+
* @returns A new object with private fields removed.
|
|
20
|
+
*/
|
|
21
|
+
export declare function removePrivateFields(obj: Record<string, any>): Record<string, any>;
|
|
22
|
+
//# sourceMappingURL=utils.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../src/trace/utils.ts"],"names":[],"mappings":"AAEA;;;GAGG;AACH,wBAAgB,eAAe,IAAI,MAAM,CAExC;AAED;;;GAGG;AACH,wBAAgB,cAAc,IAAI,MAAM,CAEvC;AAED;;;GAGG;AACH,wBAAgB,eAAe,IAAI,MAAM,CAExC;AAED;;;;GAIG;AACH,wBAAgB,mBAAmB,CACjC,GAAG,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,GACvB,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAIrB"}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { randomID } from "@kernl/shared/lib";
|
|
2
|
+
/**
|
|
3
|
+
* Generate a trace ID using 16 random bytes (128-bit, 32 hex chars).
|
|
4
|
+
* @returns A trace ID prefixed with `trace_`.
|
|
5
|
+
*/
|
|
6
|
+
export function generateTraceId() {
|
|
7
|
+
return `trace_${randomID(16)}`;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Generate a span ID using 12 random bytes (96-bit, 24 hex chars).
|
|
11
|
+
* @returns A span ID prefixed with `span_`.
|
|
12
|
+
*/
|
|
13
|
+
export function generateSpanId() {
|
|
14
|
+
return `span_${randomID(12)}`;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Generate a group ID using 12 random bytes (96-bit, 24 hex chars).
|
|
18
|
+
* @returns A group ID prefixed with `group_`.
|
|
19
|
+
*/
|
|
20
|
+
export function generateGroupId() {
|
|
21
|
+
return `group_${randomID(12)}`;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Remove fields that start with an underscore from an object.
|
|
25
|
+
* @param obj - The object to remove private fields from.
|
|
26
|
+
* @returns A new object with private fields removed.
|
|
27
|
+
*/
|
|
28
|
+
export function removePrivateFields(obj) {
|
|
29
|
+
return Object.fromEntries(Object.entries(obj).filter(([key]) => !key.startsWith("_")));
|
|
30
|
+
}
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import { type ZodType } from "zod";
|
|
2
|
+
import { Context, UnknownContext } from "../context";
|
|
3
|
+
import { LanguageModel, LanguageModelRequestSettings } from "@kernl/protocol";
|
|
4
|
+
import { InputGuardrail, OutputGuardrail } from "../guardrail";
|
|
5
|
+
import { Toolkit } from "../tool";
|
|
6
|
+
import { TextResponse } from "./thread";
|
|
7
|
+
/**
|
|
8
|
+
* Configuration for an agent.
|
|
9
|
+
*/
|
|
10
|
+
export interface AgentConfig<TContext = UnknownContext, TResponse extends AgentResponseType = TextResponse> {
|
|
11
|
+
id: string;
|
|
12
|
+
name: string;
|
|
13
|
+
/**
|
|
14
|
+
* The instructions for the agent. Will be used as the "system prompt" when this agent is
|
|
15
|
+
* invoked. Describes what the agent should do, and how it responds.
|
|
16
|
+
*
|
|
17
|
+
* Can either be a string, or a function that dynamically generates instructions for the agent.
|
|
18
|
+
* If you provide a function, it will be called with the context and the agent instance. It
|
|
19
|
+
* must return a string.
|
|
20
|
+
*/
|
|
21
|
+
instructions: string | ((context: Context<TContext>) => Promise<string> | string);
|
|
22
|
+
/**
|
|
23
|
+
* The model implementation to use when invoking the LLM.
|
|
24
|
+
*
|
|
25
|
+
* By default, if not set, the agent will use a default model that throws an error when called.
|
|
26
|
+
*/
|
|
27
|
+
model?: LanguageModel;
|
|
28
|
+
/**
|
|
29
|
+
* Configures model-specific tuning parameters (e.g. temperature, top_p, etc.)
|
|
30
|
+
*/
|
|
31
|
+
modelSettings?: LanguageModelRequestSettings;
|
|
32
|
+
/**
|
|
33
|
+
* A list of toolkits the agent can use. Toolkits are collections of related tools
|
|
34
|
+
* that can be static (FunctionToolkit) or dynamic (MCPToolkit).
|
|
35
|
+
*
|
|
36
|
+
* @example
|
|
37
|
+
* ```typescript
|
|
38
|
+
* const myTools = new FunctionToolkit({
|
|
39
|
+
* id: "custom",
|
|
40
|
+
* tools: [tool1, tool2]
|
|
41
|
+
* });
|
|
42
|
+
*
|
|
43
|
+
* const github = new MCPToolkit({
|
|
44
|
+
* id: "github",
|
|
45
|
+
* server: githubServer
|
|
46
|
+
* });
|
|
47
|
+
*
|
|
48
|
+
* const agent = new Agent({
|
|
49
|
+
* name: "Assistant",
|
|
50
|
+
* instructions: "...",
|
|
51
|
+
* toolkits: [myTools, github]
|
|
52
|
+
* });
|
|
53
|
+
* ```
|
|
54
|
+
*/
|
|
55
|
+
toolkits?: Toolkit<TContext>[];
|
|
56
|
+
/**
|
|
57
|
+
* A list of checks that run in parallel to the agent's execution on the input + output for the agent,
|
|
58
|
+
* depending on the configuration.
|
|
59
|
+
*/
|
|
60
|
+
guardrails?: AgentGuardrails<TResponse>;
|
|
61
|
+
/**
|
|
62
|
+
* The type of the response that the agent will return. If not provided, response will be a string.
|
|
63
|
+
*/
|
|
64
|
+
responseType?: TResponse;
|
|
65
|
+
/**
|
|
66
|
+
* Whether to reset the tool choice to the default value after a tool has been called. Defaults
|
|
67
|
+
* to `true`. This ensures that the agent doesn't enter an infinite loop of tool usage.
|
|
68
|
+
*/
|
|
69
|
+
resetToolChoice?: boolean;
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Guardrails for an agent.
|
|
73
|
+
*/
|
|
74
|
+
export interface AgentGuardrails<TResponse extends AgentResponseType = TextResponse> {
|
|
75
|
+
/**
|
|
76
|
+
* A list of checks that run in parallel to the agent's execution, before generating a response.
|
|
77
|
+
* Runs only if the agent is the first agent in the chain.
|
|
78
|
+
*/
|
|
79
|
+
input: InputGuardrail[];
|
|
80
|
+
/**
|
|
81
|
+
* A list of checks that run on the final output of the agent, after generating a response. Runs
|
|
82
|
+
* only if the agent produces a final output.
|
|
83
|
+
*/
|
|
84
|
+
output: OutputGuardrail<TResponse>[];
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* The type of the output object. If not provided, the output will be a string.
|
|
88
|
+
* 'text' is a special type that indicates the output will be a string.
|
|
89
|
+
*/
|
|
90
|
+
export type AgentResponseType = TextResponse | ZodType;
|
|
91
|
+
//# sourceMappingURL=agent.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"agent.d.ts","sourceRoot":"","sources":["../../src/types/agent.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,OAAO,EAAE,MAAM,KAAK,CAAC;AAEnC,OAAO,EAAE,OAAO,EAAE,cAAc,EAAE,MAAM,WAAW,CAAC;AACpD,OAAO,EAAE,aAAa,EAAE,4BAA4B,EAAE,MAAM,iBAAiB,CAAC;AAC9E,OAAO,EAAE,cAAc,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AAC9D,OAAO,EAAE,OAAO,EAAE,MAAM,QAAQ,CAAC;AAEjC,OAAO,EAAE,YAAY,EAAE,MAAM,UAAU,CAAC;AAExC;;GAEG;AACH,MAAM,WAAW,WAAW,CAC1B,QAAQ,GAAG,cAAc,EACzB,SAAS,SAAS,iBAAiB,GAAG,YAAY;IAGlD,EAAE,EAAE,MAAM,CAAC;IAGX,IAAI,EAAE,MAAM,CAAC;IAEb;;;;;;;OAOG;IACH,YAAY,EACR,MAAM,GACN,CAAC,CAAC,OAAO,EAAE,OAAO,CAAC,QAAQ,CAAC,KAAK,OAAO,CAAC,MAAM,CAAC,GAAG,MAAM,CAAC,CAAC;IAe/D;;;;OAIG;IACH,KAAK,CAAC,EAAE,aAAa,CAAC;IAEtB;;OAEG;IACH,aAAa,CAAC,EAAE,4BAA4B,CAAC;IAE7C;;;;;;;;;;;;;;;;;;;;;;OAsBG;IACH,QAAQ,CAAC,EAAE,OAAO,CAAC,QAAQ,CAAC,EAAE,CAAC;IAE/B;;;OAGG;IACH,UAAU,CAAC,EAAE,eAAe,CAAC,SAAS,CAAC,CAAC;IAExC;;OAEG;IACH,YAAY,CAAC,EAAE,SAAS,CAAC;IAsBzB;;;OAGG;IACH,eAAe,CAAC,EAAE,OAAO,CAAC;CAC3B;AAED;;GAEG;AACH,MAAM,WAAW,eAAe,CAC9B,SAAS,SAAS,iBAAiB,GAAG,YAAY;IAElD;;;OAGG;IACH,KAAK,EAAE,cAAc,EAAE,CAAC;IACxB;;;OAGG;IACH,MAAM,EAAE,eAAe,CAAC,SAAS,CAAC,EAAE,CAAC;CACtC;AAED;;;GAGG;AACH,MAAM,MAAM,iBAAiB,GAAG,YAAY,GAAG,OAAO,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|