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
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"thread.d.ts","sourceRoot":"","sources":["../../src/serde/thread.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAIxB;;;;GAIG;AAEH;;;;GAIG;AACH,eAAO,MAAM,sBAAsB,EAAG,KAAc,CAAC;AAQrD,QAAA,MAAM,kBAAkB;;;;;;;;;;;;iBActB,CAAC;AAEH,KAAK,kBAAkB,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,kBAAkB,CAAC,GAAG;IAC7D,aAAa,CAAC,EAAE,kBAAkB,CAAC;CACpC,CAAC;AAkHF,eAAO,MAAM,gBAAgB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBA8B3B,CAAC;AAEH,MAAM,MAAM,gBAAgB,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,gBAAgB,CAAC,CAAC"}
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import * as events from "../types/thread";
|
|
3
|
+
/**
|
|
4
|
+
* Utilities and schemas for serializing the run-state so that it can be paused / resumed later.
|
|
5
|
+
*
|
|
6
|
+
* (TODO): should be able to define this as a codec for the ThreadState schema
|
|
7
|
+
*/
|
|
8
|
+
/**
|
|
9
|
+
* The schema version of the serialized run state. This is used to ensure that the serialized
|
|
10
|
+
* run state is compatible with the current version of the SDK.
|
|
11
|
+
* If anything in this schema changes, the version will have to be incremented.
|
|
12
|
+
*/
|
|
13
|
+
export const CURRENT_SCHEMA_VERSION = "1.0";
|
|
14
|
+
const $schemaVersion = z.literal(CURRENT_SCHEMA_VERSION);
|
|
15
|
+
const serializedAgentSchema = z.object({
|
|
16
|
+
id: z.string(),
|
|
17
|
+
name: z.string().optional(),
|
|
18
|
+
});
|
|
19
|
+
const serializedSpanBase = z.object({
|
|
20
|
+
object: z.literal("trace.span"),
|
|
21
|
+
id: z.string(),
|
|
22
|
+
trace_id: z.string(),
|
|
23
|
+
parent_id: z.string().nullable(),
|
|
24
|
+
started_at: z.string().nullable(),
|
|
25
|
+
ended_at: z.string().nullable(),
|
|
26
|
+
error: z
|
|
27
|
+
.object({
|
|
28
|
+
message: z.string(),
|
|
29
|
+
data: z.record(z.string(), z.any()).optional(),
|
|
30
|
+
})
|
|
31
|
+
.nullable(),
|
|
32
|
+
span_data: z.record(z.string(), z.any()),
|
|
33
|
+
});
|
|
34
|
+
const SerializedSpan = serializedSpanBase.extend({
|
|
35
|
+
previous_span: z.lazy(() => SerializedSpan).optional(),
|
|
36
|
+
});
|
|
37
|
+
const usageSchema = z.object({
|
|
38
|
+
requests: z.number(),
|
|
39
|
+
inputTokens: z.number(),
|
|
40
|
+
outputTokens: z.number(),
|
|
41
|
+
totalTokens: z.number(),
|
|
42
|
+
});
|
|
43
|
+
const modelResponseSchema = z.object({
|
|
44
|
+
usage: usageSchema,
|
|
45
|
+
events: z.array(events.ThreadEvent),
|
|
46
|
+
responseId: z.string().optional(),
|
|
47
|
+
providerData: z.record(z.string(), z.any()).optional(),
|
|
48
|
+
});
|
|
49
|
+
const itemSchema = z.discriminatedUnion("kind", [
|
|
50
|
+
z.object({
|
|
51
|
+
kind: z.literal("message"),
|
|
52
|
+
rawItem: events.AssistantMessage,
|
|
53
|
+
agent: serializedAgentSchema,
|
|
54
|
+
}),
|
|
55
|
+
z.object({
|
|
56
|
+
kind: z.literal("tool-call"),
|
|
57
|
+
// rawItem: events.ToolCall.or(events.HostedToolCall),
|
|
58
|
+
rawItem: events.ToolCall,
|
|
59
|
+
agent: serializedAgentSchema,
|
|
60
|
+
}),
|
|
61
|
+
z.object({
|
|
62
|
+
kind: z.literal("tool-result"),
|
|
63
|
+
rawItem: events.ToolResultEvent,
|
|
64
|
+
agent: serializedAgentSchema,
|
|
65
|
+
output: z.string(),
|
|
66
|
+
}),
|
|
67
|
+
z.object({
|
|
68
|
+
kind: z.literal("reasoning"),
|
|
69
|
+
rawItem: events.Reasoning,
|
|
70
|
+
agent: serializedAgentSchema,
|
|
71
|
+
}),
|
|
72
|
+
]);
|
|
73
|
+
const serializedTraceSchema = z.object({
|
|
74
|
+
object: z.literal("trace"),
|
|
75
|
+
id: z.string(),
|
|
76
|
+
workflow_name: z.string(),
|
|
77
|
+
group_id: z.string().nullable(),
|
|
78
|
+
metadata: z.record(z.string(), z.any()),
|
|
79
|
+
});
|
|
80
|
+
const serializedProcessedResponseSchema = z.object({
|
|
81
|
+
newItems: z.array(itemSchema),
|
|
82
|
+
toolsUsed: z.array(z.string()),
|
|
83
|
+
functions: z.array(z.object({
|
|
84
|
+
toolCall: z.any(),
|
|
85
|
+
tool: z.any(),
|
|
86
|
+
})),
|
|
87
|
+
mcpApprovalRequests: z
|
|
88
|
+
.array(z.object({
|
|
89
|
+
requestItem: z.object({
|
|
90
|
+
// protocol.HostedToolCallItem
|
|
91
|
+
rawItem: z.object({
|
|
92
|
+
type: z.literal("hosted_tool_call"),
|
|
93
|
+
name: z.string(),
|
|
94
|
+
arguments: z.string().optional(),
|
|
95
|
+
status: z.string().optional(),
|
|
96
|
+
output: z.string().optional(),
|
|
97
|
+
// this always exists but marked as optional for early version compatibility; when releasing 1.0, we can remove the nullable and optional
|
|
98
|
+
providerData: z.record(z.string(), z.any()).nullable().optional(),
|
|
99
|
+
}),
|
|
100
|
+
}),
|
|
101
|
+
// HostedMCPTool
|
|
102
|
+
mcpTool: z.object({
|
|
103
|
+
type: z.literal("hosted_tool"),
|
|
104
|
+
name: z.literal("hosted_mcp"),
|
|
105
|
+
providerData: z.record(z.string(), z.any()),
|
|
106
|
+
}),
|
|
107
|
+
}))
|
|
108
|
+
.optional(),
|
|
109
|
+
});
|
|
110
|
+
const guardrailFunctionOutputSchema = z.object({
|
|
111
|
+
tripwireTriggered: z.boolean(),
|
|
112
|
+
outputInfo: z.any(),
|
|
113
|
+
});
|
|
114
|
+
const inputGuardrailResultSchema = z.object({
|
|
115
|
+
guardrail: z.object({
|
|
116
|
+
type: z.literal("input"),
|
|
117
|
+
name: z.string(),
|
|
118
|
+
}),
|
|
119
|
+
output: guardrailFunctionOutputSchema,
|
|
120
|
+
});
|
|
121
|
+
const outputGuardrailResultSchema = z.object({
|
|
122
|
+
guardrail: z.object({
|
|
123
|
+
type: z.literal("output"),
|
|
124
|
+
name: z.string(),
|
|
125
|
+
}),
|
|
126
|
+
agentOutput: z.any(),
|
|
127
|
+
agent: serializedAgentSchema,
|
|
128
|
+
output: guardrailFunctionOutputSchema,
|
|
129
|
+
});
|
|
130
|
+
// (TODO): define z.codec
|
|
131
|
+
export const SerializedThread = z.object({
|
|
132
|
+
$schemaVersion,
|
|
133
|
+
currentTurn: z.number(),
|
|
134
|
+
currentAgent: serializedAgentSchema, // (TODO): in our case we probably don't need to serialize the whole agent - an ID would suffice
|
|
135
|
+
originalInput: z.string().or(z.array(events.ThreadEvent)),
|
|
136
|
+
modelResponses: z.array(modelResponseSchema),
|
|
137
|
+
context: z.object({
|
|
138
|
+
usage: usageSchema, // (TODO): move to stats
|
|
139
|
+
// (TODO): belongs elsewhere
|
|
140
|
+
approvals: z.record(z.string(), z.object({
|
|
141
|
+
approved: z.array(z.string()).or(z.boolean()),
|
|
142
|
+
rejected: z.array(z.string()).or(z.boolean()),
|
|
143
|
+
})),
|
|
144
|
+
context: z.record(z.string(), z.any()),
|
|
145
|
+
}),
|
|
146
|
+
toolUseTracker: z.record(z.string(), z.array(z.string())),
|
|
147
|
+
maxTurns: z.number(),
|
|
148
|
+
currentAgentSpan: SerializedSpan.nullable().optional(),
|
|
149
|
+
noActiveAgentRun: z.boolean(),
|
|
150
|
+
inputGuardrailResults: z.array(inputGuardrailResultSchema),
|
|
151
|
+
outputGuardrailResults: z.array(outputGuardrailResultSchema),
|
|
152
|
+
// (TODO): currentStep: nextStepSchema.optional(),
|
|
153
|
+
lastModelResponse: modelResponseSchema.optional(),
|
|
154
|
+
generatedItems: z.array(itemSchema),
|
|
155
|
+
lastProcessedResponse: serializedProcessedResponseSchema.optional(),
|
|
156
|
+
currentTurnPersistedItemCount: z.number().int().min(0).optional(),
|
|
157
|
+
trace: serializedTraceSchema.nullable(),
|
|
158
|
+
});
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import type { ToolInputParameters } from "../tool/types";
|
|
2
|
+
/**
|
|
3
|
+
* Serialized function tool for model requests
|
|
4
|
+
*/
|
|
5
|
+
export type SerializedFunctionTool = {
|
|
6
|
+
/**
|
|
7
|
+
* The type of the tool.
|
|
8
|
+
*/
|
|
9
|
+
type: "function";
|
|
10
|
+
/**
|
|
11
|
+
* The name of the tool.
|
|
12
|
+
*/
|
|
13
|
+
name?: string;
|
|
14
|
+
/**
|
|
15
|
+
* The description of the tool that helps the model to understand when to use the tool
|
|
16
|
+
*/
|
|
17
|
+
description: string;
|
|
18
|
+
/**
|
|
19
|
+
* A JSON schema describing the parameters of the tool.
|
|
20
|
+
*/
|
|
21
|
+
parameters: ToolInputParameters;
|
|
22
|
+
};
|
|
23
|
+
/**
|
|
24
|
+
* Serialized hosted tool for model requests
|
|
25
|
+
*/
|
|
26
|
+
export type SerializedHostedTool = {
|
|
27
|
+
type: "hosted-tool";
|
|
28
|
+
id: string;
|
|
29
|
+
name?: string;
|
|
30
|
+
providerData?: Record<string, any>;
|
|
31
|
+
};
|
|
32
|
+
/**
|
|
33
|
+
* Union of all serialized tool types
|
|
34
|
+
*/
|
|
35
|
+
export type SerializedTool = SerializedFunctionTool | SerializedHostedTool;
|
|
36
|
+
//# sourceMappingURL=tool.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tool.d.ts","sourceRoot":"","sources":["../../src/serde/tool.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,cAAc,CAAC;AAExD;;GAEG;AACH,MAAM,MAAM,sBAAsB,GAAG;IACnC;;OAEG;IACH,IAAI,EAAE,UAAU,CAAC;IAEjB;;OAEG;IACH,IAAI,CAAC,EAAE,MAAM,CAAC;IAEd;;OAEG;IACH,WAAW,EAAE,MAAM,CAAC;IAEpB;;OAEG;IACH,UAAU,EAAE,mBAAmB,CAAC;CACjC,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,oBAAoB,GAAG;IACjC,IAAI,EAAE,aAAa,CAAC;IACpB,EAAE,EAAE,MAAM,CAAC;IACX,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,YAAY,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;CACpC,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,cAAc,GAAG,sBAAsB,GAAG,oBAAoB,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
//# sourceMappingURL=session.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"session.d.ts","sourceRoot":"","sources":["../src/session.ts"],"names":[],"mappings":""}
|
package/dist/session.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";
|
package/dist/task.d.ts
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import type { Agent } from "./agent";
|
|
2
|
+
import type { Context, UnknownContext } from "./context";
|
|
3
|
+
import type { Thread } from "./thread";
|
|
4
|
+
/**
|
|
5
|
+
* A Task represents a unit of work scheduled by the kernl.
|
|
6
|
+
* Analogous to task_struct in the Linux kernel.
|
|
7
|
+
*/
|
|
8
|
+
export declare class Task<TContext = UnknownContext, TResult = unknown> {
|
|
9
|
+
pid: string;
|
|
10
|
+
instructions: string | ((context: Context<TContext>) => string);
|
|
11
|
+
state: TaskState;
|
|
12
|
+
owner: Agent<TContext>;
|
|
13
|
+
context: Context<TContext>;
|
|
14
|
+
/**
|
|
15
|
+
* Might want the ability to pick up a new thread of execution from a compressed task checkpoint instead of including
|
|
16
|
+
* all of the events from the old threads (which may no longer be useful if they completed with good artifacts + reasoning / action summaries).
|
|
17
|
+
* At that point its really more about tracing.
|
|
18
|
+
*/
|
|
19
|
+
threads: Thread<TContext>[];
|
|
20
|
+
current: Thread<TContext> | null;
|
|
21
|
+
result: TResult | null;
|
|
22
|
+
constructor(init: {
|
|
23
|
+
pid: string;
|
|
24
|
+
instructions: string | ((context: Context<TContext>) => string);
|
|
25
|
+
state: TaskState;
|
|
26
|
+
owner: Agent<TContext>;
|
|
27
|
+
context: Context<TContext>;
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* The various states that a task may be in.
|
|
32
|
+
*/
|
|
33
|
+
export declare enum TaskState {
|
|
34
|
+
/**
|
|
35
|
+
* Task is either:
|
|
36
|
+
* - Currently executing
|
|
37
|
+
* - In run queue waiting to be scheduled (might want to differentiate between running + queued here)
|
|
38
|
+
*/
|
|
39
|
+
RUNNING = "running",
|
|
40
|
+
/**
|
|
41
|
+
* Task is sleeping/blocked, waiting for a condition.
|
|
42
|
+
* Can be woken up by:
|
|
43
|
+
* - The condition being met (e.g., approval granted)
|
|
44
|
+
* - A signal (e.g., user cancellation)
|
|
45
|
+
*
|
|
46
|
+
* Examples:
|
|
47
|
+
* - Waiting for tool approval
|
|
48
|
+
* - Waiting for user input
|
|
49
|
+
* - Sleeping on a timer
|
|
50
|
+
*/
|
|
51
|
+
INTERRUPTIBLE = "interruptible",
|
|
52
|
+
/**
|
|
53
|
+
* Task is sleeping/blocked and CANNOT be interrupted by signals.
|
|
54
|
+
* Only wakes when the condition is met.
|
|
55
|
+
*
|
|
56
|
+
* Examples:
|
|
57
|
+
* - Waiting for critical I/O (model API call)
|
|
58
|
+
* - Waiting for resource that MUST complete
|
|
59
|
+
*
|
|
60
|
+
* Use sparingly - these tasks can't be cancelled!
|
|
61
|
+
*/
|
|
62
|
+
UNINTERRUPTIBLE = "uninterruptible",
|
|
63
|
+
/**
|
|
64
|
+
* Task has been stopped by a signal (SIGSTOP).
|
|
65
|
+
* Will remain stopped until explicitly continued (SIGCONT).
|
|
66
|
+
*
|
|
67
|
+
* Examples:
|
|
68
|
+
* - User explicitly paused the agent
|
|
69
|
+
* - Debugger attached
|
|
70
|
+
*/
|
|
71
|
+
STOPPED = "stopped",
|
|
72
|
+
/**
|
|
73
|
+
* Task has finished execution but hasn't been cleaned up yet.
|
|
74
|
+
* Waiting for parent to read exit status (wait/waitpid).
|
|
75
|
+
*
|
|
76
|
+
* Examples:
|
|
77
|
+
* - Agent completed but result not yet retrieved
|
|
78
|
+
* - Child agent finished, parent needs to collect result
|
|
79
|
+
*/
|
|
80
|
+
ZOMBIE = "zombie",
|
|
81
|
+
/**
|
|
82
|
+
* Task is being removed from the system.
|
|
83
|
+
* Final cleanup in progress, about to be fully deleted.
|
|
84
|
+
*/
|
|
85
|
+
DEAD = "dead"
|
|
86
|
+
}
|
|
87
|
+
//# sourceMappingURL=task.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"task.d.ts","sourceRoot":"","sources":["../src/task.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,SAAS,CAAC;AACrC,OAAO,KAAK,EAAE,OAAO,EAAE,cAAc,EAAE,MAAM,WAAW,CAAC;AAEzD,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,UAAU,CAAC;AAEvC;;;GAGG;AACH,qBAAa,IAAI,CAAC,QAAQ,GAAG,cAAc,EAAE,OAAO,GAAG,OAAO;IAC5D,GAAG,EAAE,MAAM,CAAoD;IAG/D,YAAY,EACR,MAAM,GACN,CAAC,CAAC,OAAO,EAAE,OAAO,CAAC,QAAQ,CAAC,KAAK,MAAM,CAAC,CAA4B;IAExE,KAAK,EAAE,SAAS,CAAiC;IACjD,KAAK,EAAE,KAAK,CAAC,QAAQ,CAAC,CAA2C;IAEjE,OAAO,EAAE,OAAO,CAAC,QAAQ,CAAC,CAA0D;IAIpF;;;;OAIG;IACH,OAAO,EAAE,MAAM,CAAC,QAAQ,CAAC,EAAE,CAA6C;IACxE,OAAO,EAAE,MAAM,CAAC,QAAQ,CAAC,GAAG,IAAI,CAAyC;IACzE,MAAM,EAAE,OAAO,GAAG,IAAI,CAAgC;gBAQ1C,IAAI,EAAE;QAChB,GAAG,EAAE,MAAM,CAAC;QACZ,YAAY,EAAE,MAAM,GAAG,CAAC,CAAC,OAAO,EAAE,OAAO,CAAC,QAAQ,CAAC,KAAK,MAAM,CAAC,CAAC;QAChE,KAAK,EAAE,SAAS,CAAC;QACjB,KAAK,EAAE,KAAK,CAAC,QAAQ,CAAC,CAAC;QACvB,OAAO,EAAE,OAAO,CAAC,QAAQ,CAAC,CAAC;KAC5B;CAUF;AAED;;GAEG;AACH,oBAAY,SAAS;IACnB;;;;OAIG;IACH,OAAO,YAAY;IAEnB;;;;;;;;;;OAUG;IACH,aAAa,kBAAkB;IAE/B;;;;;;;;;OASG;IACH,eAAe,oBAAoB;IAEnC;;;;;;;OAOG;IACH,OAAO,YAAY;IAEnB;;;;;;;OAOG;IACH,MAAM,WAAW;IAEjB;;;OAGG;IACH,IAAI,SAAS;CACd"}
|
package/dist/task.js
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A Task represents a unit of work scheduled by the kernl.
|
|
3
|
+
* Analogous to task_struct in the Linux kernel.
|
|
4
|
+
*/
|
|
5
|
+
export class Task {
|
|
6
|
+
pid;
|
|
7
|
+
// tgid: string | null; /* task groupid */
|
|
8
|
+
// prio: TaskPriority;
|
|
9
|
+
instructions;
|
|
10
|
+
// sched: TaskSched; /* scheduling (timers, timeouts, deadlines) */
|
|
11
|
+
state;
|
|
12
|
+
owner;
|
|
13
|
+
context;
|
|
14
|
+
// cred: Credentials; /* Effective (overridable) subjective task credentials (COW): */
|
|
15
|
+
// realcred: Credentials; /* Objective and real subjective task credentials (COW): */
|
|
16
|
+
/**
|
|
17
|
+
* Might want the ability to pick up a new thread of execution from a compressed task checkpoint instead of including
|
|
18
|
+
* all of the events from the old threads (which may no longer be useful if they completed with good artifacts + reasoning / action summaries).
|
|
19
|
+
* At that point its really more about tracing.
|
|
20
|
+
*/
|
|
21
|
+
threads;
|
|
22
|
+
current;
|
|
23
|
+
result;
|
|
24
|
+
// checkpoints: TaskCheckpoint<TContext>[]; /* checkpoints for resuming execution */
|
|
25
|
+
// TODO: Deferred fields for later implementation
|
|
26
|
+
// tgid: string; // Thread group ID
|
|
27
|
+
// limits: TaskLimits; // Resource limits (max ticks, tokens, timeout)
|
|
28
|
+
// nsproxy: NamespaceProxy; // Namespace isolation
|
|
29
|
+
constructor(init) {
|
|
30
|
+
this.pid = init.pid;
|
|
31
|
+
this.instructions = init.instructions;
|
|
32
|
+
this.state = init.state;
|
|
33
|
+
this.owner = init.owner;
|
|
34
|
+
this.context = init.context;
|
|
35
|
+
this.threads = [];
|
|
36
|
+
this.current = null;
|
|
37
|
+
this.result = null;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* The various states that a task may be in.
|
|
42
|
+
*/
|
|
43
|
+
export var TaskState;
|
|
44
|
+
(function (TaskState) {
|
|
45
|
+
/**
|
|
46
|
+
* Task is either:
|
|
47
|
+
* - Currently executing
|
|
48
|
+
* - In run queue waiting to be scheduled (might want to differentiate between running + queued here)
|
|
49
|
+
*/
|
|
50
|
+
TaskState["RUNNING"] = "running";
|
|
51
|
+
/**
|
|
52
|
+
* Task is sleeping/blocked, waiting for a condition.
|
|
53
|
+
* Can be woken up by:
|
|
54
|
+
* - The condition being met (e.g., approval granted)
|
|
55
|
+
* - A signal (e.g., user cancellation)
|
|
56
|
+
*
|
|
57
|
+
* Examples:
|
|
58
|
+
* - Waiting for tool approval
|
|
59
|
+
* - Waiting for user input
|
|
60
|
+
* - Sleeping on a timer
|
|
61
|
+
*/
|
|
62
|
+
TaskState["INTERRUPTIBLE"] = "interruptible";
|
|
63
|
+
/**
|
|
64
|
+
* Task is sleeping/blocked and CANNOT be interrupted by signals.
|
|
65
|
+
* Only wakes when the condition is met.
|
|
66
|
+
*
|
|
67
|
+
* Examples:
|
|
68
|
+
* - Waiting for critical I/O (model API call)
|
|
69
|
+
* - Waiting for resource that MUST complete
|
|
70
|
+
*
|
|
71
|
+
* Use sparingly - these tasks can't be cancelled!
|
|
72
|
+
*/
|
|
73
|
+
TaskState["UNINTERRUPTIBLE"] = "uninterruptible";
|
|
74
|
+
/**
|
|
75
|
+
* Task has been stopped by a signal (SIGSTOP).
|
|
76
|
+
* Will remain stopped until explicitly continued (SIGCONT).
|
|
77
|
+
*
|
|
78
|
+
* Examples:
|
|
79
|
+
* - User explicitly paused the agent
|
|
80
|
+
* - Debugger attached
|
|
81
|
+
*/
|
|
82
|
+
TaskState["STOPPED"] = "stopped";
|
|
83
|
+
/**
|
|
84
|
+
* Task has finished execution but hasn't been cleaned up yet.
|
|
85
|
+
* Waiting for parent to read exit status (wait/waitpid).
|
|
86
|
+
*
|
|
87
|
+
* Examples:
|
|
88
|
+
* - Agent completed but result not yet retrieved
|
|
89
|
+
* - Child agent finished, parent needs to collect result
|
|
90
|
+
*/
|
|
91
|
+
TaskState["ZOMBIE"] = "zombie";
|
|
92
|
+
/**
|
|
93
|
+
* Task is being removed from the system.
|
|
94
|
+
* Final cleanup in progress, about to be fully deleted.
|
|
95
|
+
*/
|
|
96
|
+
TaskState["DEAD"] = "dead";
|
|
97
|
+
})(TaskState || (TaskState = {}));
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { LanguageModel, LanguageModelRequest, LanguageModelResponse } from "@kernl/protocol";
|
|
2
|
+
import type { ThreadStreamEvent } from "../../types/thread";
|
|
3
|
+
/**
|
|
4
|
+
* A mock language model that echoes the user input back as an assistant message.
|
|
5
|
+
* Useful for testing the execution flow without calling a real LLM.
|
|
6
|
+
*
|
|
7
|
+
* (TODO): Support tool calls for testing.
|
|
8
|
+
*/
|
|
9
|
+
export declare class MockLanguageModel implements LanguageModel {
|
|
10
|
+
readonly spec: "1.0";
|
|
11
|
+
readonly provider = "mock";
|
|
12
|
+
readonly modelId = "mock-model";
|
|
13
|
+
generate(request: LanguageModelRequest): Promise<LanguageModelResponse>;
|
|
14
|
+
stream(request: LanguageModelRequest): AsyncIterable<ThreadStreamEvent>;
|
|
15
|
+
/**
|
|
16
|
+
* Extract text from the input (LanguageModelItem[])
|
|
17
|
+
*/
|
|
18
|
+
private extractUserText;
|
|
19
|
+
/**
|
|
20
|
+
* Simple token counter (fake approximation)
|
|
21
|
+
*/
|
|
22
|
+
private countTokens;
|
|
23
|
+
/**
|
|
24
|
+
* Generate a simple ID for messages
|
|
25
|
+
*/
|
|
26
|
+
private generateId;
|
|
27
|
+
}
|
|
28
|
+
//# sourceMappingURL=mock.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mock.d.ts","sourceRoot":"","sources":["../../../src/thread/__tests__/mock.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,aAAa,EACb,oBAAoB,EACpB,qBAAqB,EAGtB,MAAM,iBAAiB,CAAC;AACzB,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,gBAAgB,CAAC;AAExD;;;;;GAKG;AACH,qBAAa,iBAAkB,YAAW,aAAa;IACrD,QAAQ,CAAC,IAAI,EAAG,KAAK,CAAU;IAC/B,QAAQ,CAAC,QAAQ,UAAU;IAC3B,QAAQ,CAAC,OAAO,gBAAgB;IAE1B,QAAQ,CACZ,OAAO,EAAE,oBAAoB,GAC5B,OAAO,CAAC,qBAAqB,CAAC;IAgC1B,MAAM,CACX,OAAO,EAAE,oBAAoB,GAC5B,aAAa,CAAC,iBAAiB,CAAC;IAKnC;;OAEG;IACH,OAAO,CAAC,eAAe;IAgBvB;;OAEG;IACH,OAAO,CAAC,WAAW;IAKnB;;OAEG;IACH,OAAO,CAAC,UAAU;CAGnB"}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A mock language model that echoes the user input back as an assistant message.
|
|
3
|
+
* Useful for testing the execution flow without calling a real LLM.
|
|
4
|
+
*
|
|
5
|
+
* (TODO): Support tool calls for testing.
|
|
6
|
+
*/
|
|
7
|
+
export class MockLanguageModel {
|
|
8
|
+
spec = "1.0";
|
|
9
|
+
provider = "mock";
|
|
10
|
+
modelId = "mock-model";
|
|
11
|
+
async generate(request) {
|
|
12
|
+
// Extract user message text from input
|
|
13
|
+
const userText = this.extractUserText(request.input);
|
|
14
|
+
// Create assistant message that echoes the input
|
|
15
|
+
const content = [
|
|
16
|
+
{
|
|
17
|
+
kind: "message",
|
|
18
|
+
id: this.generateId(),
|
|
19
|
+
role: "assistant",
|
|
20
|
+
content: [
|
|
21
|
+
{
|
|
22
|
+
kind: "text",
|
|
23
|
+
text: userText,
|
|
24
|
+
},
|
|
25
|
+
],
|
|
26
|
+
},
|
|
27
|
+
];
|
|
28
|
+
// Return with fake usage stats
|
|
29
|
+
return {
|
|
30
|
+
content,
|
|
31
|
+
finishReason: "stop",
|
|
32
|
+
usage: {
|
|
33
|
+
inputTokens: this.countTokens(userText),
|
|
34
|
+
outputTokens: this.countTokens(userText),
|
|
35
|
+
totalTokens: this.countTokens(userText) * 2,
|
|
36
|
+
},
|
|
37
|
+
warnings: [],
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
async *stream(request) {
|
|
41
|
+
// TODO: Implement streaming (not needed for hello world)
|
|
42
|
+
throw new Error("MockLanguageModel.stream() not implemented yet");
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Extract text from the input (LanguageModelItem[])
|
|
46
|
+
*/
|
|
47
|
+
extractUserText(input) {
|
|
48
|
+
// Extract text from LanguageModelItem array
|
|
49
|
+
const text = [];
|
|
50
|
+
for (const item of input) {
|
|
51
|
+
if (item.kind === "message" && item.role === "user") {
|
|
52
|
+
for (const part of item.content) {
|
|
53
|
+
if (part.kind === "text") {
|
|
54
|
+
text.push(part.text);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
return text.join(" ");
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Simple token counter (fake approximation)
|
|
63
|
+
*/
|
|
64
|
+
countTokens(text) {
|
|
65
|
+
// Rough approximation: 1 token H 4 characters
|
|
66
|
+
return Math.ceil(text.length / 4);
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Generate a simple ID for messages
|
|
70
|
+
*/
|
|
71
|
+
generateId() {
|
|
72
|
+
return `msg_${Date.now()}_${Math.random().toString(36).substring(2, 9)}`;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"thread.test.d.ts","sourceRoot":"","sources":["../../../src/thread/__tests__/thread.test.ts"],"names":[],"mappings":""}
|