kernl 0.12.0 → 0.12.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 +1 -1
- package/CHANGELOG.md +15 -0
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/thread/__tests__/mock.d.ts +2 -3
- package/dist/thread/__tests__/mock.d.ts.map +1 -1
- package/dist/thread/thread.d.ts +4 -0
- package/dist/thread/thread.d.ts.map +1 -1
- package/dist/thread/thread.js +43 -75
- package/dist/thread/types.d.ts +18 -3
- package/dist/thread/types.d.ts.map +1 -1
- package/dist/thread/utils.d.ts +7 -7
- package/dist/thread/utils.d.ts.map +1 -1
- package/dist/thread/utils.js +1 -1
- package/package.json +2 -4
- package/src/index.ts +1 -0
- package/src/thread/__tests__/mock.ts +2 -2
- package/src/thread/thread.ts +47 -78
- package/src/thread/types.ts +49 -3
- package/src/thread/utils.ts +14 -7
- package/dist/thread/__tests__/integration.test.d.ts +0 -2
- package/dist/thread/__tests__/integration.test.d.ts.map +0 -1
- package/dist/thread/__tests__/integration.test.js +0 -320
- package/src/thread/__tests__/integration.test.ts +0 -434
package/.turbo/turbo-build.log
CHANGED
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,20 @@
|
|
|
1
1
|
# @kernl/core
|
|
2
2
|
|
|
3
|
+
## 0.12.1
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- 58e9db2: Fix provider normalization and ThreadStreamEvent types
|
|
8
|
+
- Normalize AI SDK provider strings (`anthropic.messages` -> `anthropic`, etc.)
|
|
9
|
+
- Export `ThreadStreamEvent` from kernl for consumers
|
|
10
|
+
- Update `toUIMessageStream` to accept `ThreadStreamEvent` from `agent.stream()`
|
|
11
|
+
- Add `kernl` as dependency to `@kernl-sdk/ai` (breaking circular devDep)
|
|
12
|
+
|
|
13
|
+
- 320b76a: Add emit helper and sequence streamed events
|
|
14
|
+
- Thread now yields `ThreadEvent` with `seq` for complete items (messages, tool calls, tool results)
|
|
15
|
+
- Delta and control events remain ephemeral `StreamEvent` without seq
|
|
16
|
+
- Internal `emit()` helper reduces boilerplate in event emission
|
|
17
|
+
|
|
3
18
|
## 0.12.0
|
|
4
19
|
|
|
5
20
|
### Minor Changes
|
package/dist/index.d.ts
CHANGED
|
@@ -10,7 +10,7 @@ export { MCPServerSSE } from "./mcp/sse.js";
|
|
|
10
10
|
export { MCPServerStreamableHttp } from "./mcp/http.js";
|
|
11
11
|
export type { MThread as Thread, MThreadEvent as ThreadEvent, MThreadEventBase as ThreadEventBase, } from "./api/models/index.js";
|
|
12
12
|
export type { RThreadsListParams as ThreadsListParams, RThreadGetOptions as ThreadGetOptions, RThreadHistoryParams as ThreadHistoryParams, RThreadCreateParams as ThreadCreateParams, RThreadUpdateParams as ThreadUpdateParams, } from "./api/resources/threads/index.js";
|
|
13
|
-
export { THREAD_STATES, type ThreadState, type PublicThreadEvent, } from "./thread/types.js";
|
|
13
|
+
export { THREAD_STATES, type ThreadState, type PublicThreadEvent, type ThreadStreamEvent, } from "./thread/types.js";
|
|
14
14
|
export type { ThreadStore, NewThread, ThreadUpdate, ThreadFilter, ThreadHistoryOptions, ThreadInclude, ThreadListOptions, SortOrder, KernlStorage, Transaction, } from "./storage/index.js";
|
|
15
15
|
export { Memory, MemoryByteEncoder } from "./memory/index.js";
|
|
16
16
|
export type { MemoryStore, MemoryScope, NewMemory, MemoryConfig, MemoryReindexParams, MemoryRecord, MemoryKind, MemoryRecordUpdate, MemoryFilter, MemoryListOptions, MemorySearchQuery, WorkingMemorySnapshot, ShortTermMemorySnapshot, MemoryIndexBase, MemorySearchIndex, MemoryGraphIndex, MemoryArchiveIndex, GraphTraversalQuery, GraphTraversalResult, ArchiveQuery, ArchiveResult, MemoryByte, MemoryByteCodec, } from "./memory/index.js";
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.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AAC;AAIrD,YAAY,EACV,OAAO,IAAI,MAAM,EACjB,YAAY,IAAI,WAAW,EAC3B,gBAAgB,IAAI,eAAe,GACpC,MAAM,cAAc,CAAC;AAEtB,YAAY,EACV,kBAAkB,IAAI,iBAAiB,EACvC,iBAAiB,IAAI,gBAAgB,EACrC,oBAAoB,IAAI,mBAAmB,EAC3C,mBAAmB,IAAI,kBAAkB,EACzC,mBAAmB,IAAI,kBAAkB,GAC1C,MAAM,yBAAyB,CAAC;AAEjC,OAAO,EACL,aAAa,EACb,KAAK,WAAW,EAChB,KAAK,iBAAiB,EACtB,KAAK,iBAAiB,GACvB,MAAM,gBAAgB,CAAC;AAIxB,YAAY,EACV,WAAW,EACX,SAAS,EACT,YAAY,EACZ,YAAY,EACZ,oBAAoB,EACpB,aAAa,EACb,iBAAiB,EACjB,SAAS,EACT,YAAY,EACZ,WAAW,GACZ,MAAM,WAAW,CAAC;AAGnB,OAAO,EAAE,MAAM,EAAE,iBAAiB,EAAE,MAAM,UAAU,CAAC;AACrD,YAAY,EACV,WAAW,EACX,WAAW,EACX,SAAS,EACT,YAAY,EACZ,mBAAmB,EACnB,YAAY,EACZ,UAAU,EACV,kBAAkB,EAClB,YAAY,EACZ,iBAAiB,EACjB,iBAAiB,EACjB,qBAAqB,EACrB,uBAAuB,EACvB,eAAe,EACf,iBAAiB,EACjB,gBAAgB,EAChB,kBAAkB,EAClB,mBAAmB,EACnB,oBAAoB,EACpB,YAAY,EACZ,aAAa,EACb,UAAU,EACV,eAAe,GAChB,MAAM,UAAU,CAAC"}
|
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import { LanguageModel, LanguageModelRequest, LanguageModelResponse } from "@kernl-sdk/protocol";
|
|
2
|
-
import type { ThreadStreamEvent } from "../../thread/types.js";
|
|
1
|
+
import { LanguageModel, LanguageModelRequest, LanguageModelResponse, LanguageModelStreamEvent } from "@kernl-sdk/protocol";
|
|
3
2
|
/**
|
|
4
3
|
* A mock language model that echoes the user input back as an assistant message.
|
|
5
4
|
* Useful for testing the execution flow without calling a real LLM.
|
|
@@ -11,7 +10,7 @@ export declare class MockLanguageModel implements LanguageModel {
|
|
|
11
10
|
readonly provider = "mock";
|
|
12
11
|
readonly modelId = "mock-model";
|
|
13
12
|
generate(request: LanguageModelRequest): Promise<LanguageModelResponse>;
|
|
14
|
-
stream(request: LanguageModelRequest): AsyncIterable<
|
|
13
|
+
stream(request: LanguageModelRequest): AsyncIterable<LanguageModelStreamEvent>;
|
|
15
14
|
/**
|
|
16
15
|
* Extract text from the input (LanguageModelItem[])
|
|
17
16
|
*/
|
|
@@ -1 +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,
|
|
1
|
+
{"version":3,"file":"mock.d.ts","sourceRoot":"","sources":["../../../src/thread/__tests__/mock.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,aAAa,EACb,oBAAoB,EACpB,qBAAqB,EAGrB,wBAAwB,EACzB,MAAM,qBAAqB,CAAC;AAE7B;;;;;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,wBAAwB,CAAC;IAK1C;;OAEG;IACH,OAAO,CAAC,eAAe;IAgBvB;;OAEG;IACH,OAAO,CAAC,WAAW;IAKnB;;OAEG;IACH,OAAO,CAAC,UAAU;CAGnB"}
|
package/dist/thread/thread.d.ts
CHANGED
|
@@ -113,6 +113,10 @@ export declare class Thread<TContext = unknown, TOutput extends AgentOutputType
|
|
|
113
113
|
* TODO: Emit thread.stop when cancelled (neither result nor error set)
|
|
114
114
|
*/
|
|
115
115
|
cancel(): void;
|
|
116
|
+
/**
|
|
117
|
+
* Emit an agent event with common fields auto-filled.
|
|
118
|
+
*/
|
|
119
|
+
private emit;
|
|
116
120
|
/**
|
|
117
121
|
* Perform the actions returned by the model
|
|
118
122
|
*/
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"thread.d.ts","sourceRoot":"","sources":["../../src/thread/thread.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,KAAK,EAAE,MAAM,SAAS,CAAC;AAChC,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AACpC,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,QAAQ,CAAC;AACnC,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,aAAa,CAAC;AAKzD,OAAO,EAML,aAAa,
|
|
1
|
+
{"version":3,"file":"thread.d.ts","sourceRoot":"","sources":["../../src/thread/thread.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,KAAK,EAAE,MAAM,SAAS,CAAC;AAChC,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AACpC,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,QAAQ,CAAC;AACnC,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,aAAa,CAAC;AAKzD,OAAO,EAML,aAAa,EAMd,MAAM,qBAAqB,CAAC;AAG7B,OAAO,KAAK,EAEV,WAAW,EACX,WAAW,EACX,aAAa,EACb,gBAAgB,EAChB,iBAAiB,EACjB,mBAAmB,EAGpB,MAAM,SAAS,CAAC;AACjB,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,eAAe,CAAC;AAWrD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAuCG;AACH,qBAAa,MAAM,CACjB,QAAQ,GAAG,OAAO,EAClB,OAAO,SAAS,eAAe,GAAG,MAAM;IAExC,QAAQ,CAAC,GAAG,E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AEG;YACW,cAAc;IAyC5B;;;;OAIG;YACW,YAAY;IAsE1B;;OAEG;YACW,mBAAmB;CAsDlC"}
|
package/dist/thread/thread.js
CHANGED
|
@@ -120,33 +120,14 @@ export class Thread {
|
|
|
120
120
|
this.abort = new AbortController();
|
|
121
121
|
this.tickres = undefined; // reset for this run
|
|
122
122
|
await this.checkpoint(); /* c1: persist RUNNING state + initial input */
|
|
123
|
-
this.
|
|
124
|
-
kind: "thread.start",
|
|
125
|
-
threadId: this.tid,
|
|
126
|
-
agentId: this.agent.id,
|
|
127
|
-
namespace: this.namespace,
|
|
128
|
-
context: this.context,
|
|
129
|
-
});
|
|
123
|
+
this.emit("thread.start");
|
|
130
124
|
yield { kind: "stream.start" }; // always yield start immediately
|
|
131
125
|
try {
|
|
132
126
|
yield* this._execute();
|
|
133
|
-
this.
|
|
134
|
-
kind: "thread.stop",
|
|
135
|
-
threadId: this.tid,
|
|
136
|
-
agentId: this.agent.id,
|
|
137
|
-
namespace: this.namespace,
|
|
138
|
-
context: this.context,
|
|
139
|
-
state: STOPPED,
|
|
140
|
-
result: this.tickres,
|
|
141
|
-
});
|
|
127
|
+
this.emit("thread.stop", { state: STOPPED, result: this.tickres });
|
|
142
128
|
}
|
|
143
129
|
catch (err) {
|
|
144
|
-
this.
|
|
145
|
-
kind: "thread.stop",
|
|
146
|
-
threadId: this.tid,
|
|
147
|
-
agentId: this.agent.id,
|
|
148
|
-
namespace: this.namespace,
|
|
149
|
-
context: this.context,
|
|
130
|
+
this.emit("thread.stop", {
|
|
150
131
|
state: STOPPED,
|
|
151
132
|
error: err instanceof Error ? err.message : String(err),
|
|
152
133
|
});
|
|
@@ -176,12 +157,15 @@ export class Thread {
|
|
|
176
157
|
err = e.error;
|
|
177
158
|
logger.error(e.error); // (TODO): onError callback in options
|
|
178
159
|
}
|
|
179
|
-
//
|
|
160
|
+
// complete items get persisted with seq, deltas are ephemeral
|
|
180
161
|
if (notDelta(e)) {
|
|
181
|
-
|
|
182
|
-
|
|
162
|
+
const [seqd] = this.append(e);
|
|
163
|
+
events.push(seqd);
|
|
164
|
+
yield seqd;
|
|
165
|
+
}
|
|
166
|
+
else {
|
|
167
|
+
yield e;
|
|
183
168
|
}
|
|
184
|
-
yield e;
|
|
185
169
|
}
|
|
186
170
|
// if an error event occurred → throw it
|
|
187
171
|
if (err) {
|
|
@@ -201,10 +185,10 @@ export class Thread {
|
|
|
201
185
|
}
|
|
202
186
|
// perform intended actions
|
|
203
187
|
const { actions, pendingApprovals } = await this.performActions(intentions);
|
|
204
|
-
// append + yield action events
|
|
188
|
+
// append + yield action events (sequenced)
|
|
205
189
|
for (const a of actions) {
|
|
206
|
-
this.append(a);
|
|
207
|
-
yield
|
|
190
|
+
const [seqd] = this.append(a);
|
|
191
|
+
yield seqd;
|
|
208
192
|
}
|
|
209
193
|
await this.checkpoint(); /* c3: tick complete */
|
|
210
194
|
if (pendingApprovals.length > 0) {
|
|
@@ -228,16 +212,9 @@ export class Thread {
|
|
|
228
212
|
this._tick++;
|
|
229
213
|
// (TODO): check limits (if this._tick > this.limits.maxTicks)
|
|
230
214
|
// (TODO): run input guardrails on first tick (if this._tick === 1)
|
|
215
|
+
// (TODO): compaction if necessary
|
|
231
216
|
const req = await this.prepareModelRequest(this.history);
|
|
232
|
-
this.
|
|
233
|
-
kind: "model.call.start",
|
|
234
|
-
provider: this.model.provider,
|
|
235
|
-
modelId: this.model.modelId,
|
|
236
|
-
settings: req.settings ?? {},
|
|
237
|
-
threadId: this.tid,
|
|
238
|
-
agentId: this.agent.id,
|
|
239
|
-
context: this.context,
|
|
240
|
-
});
|
|
217
|
+
this.emit("model.call.start", { settings: req.settings ?? {} });
|
|
241
218
|
let usage;
|
|
242
219
|
let finishReason = "unknown";
|
|
243
220
|
try {
|
|
@@ -259,27 +236,10 @@ export class Thread {
|
|
|
259
236
|
yield event;
|
|
260
237
|
}
|
|
261
238
|
}
|
|
262
|
-
this.
|
|
263
|
-
kind: "model.call.end",
|
|
264
|
-
provider: this.model.provider,
|
|
265
|
-
modelId: this.model.modelId,
|
|
266
|
-
finishReason,
|
|
267
|
-
usage,
|
|
268
|
-
threadId: this.tid,
|
|
269
|
-
agentId: this.agent.id,
|
|
270
|
-
context: this.context,
|
|
271
|
-
});
|
|
239
|
+
this.emit("model.call.end", { finishReason, usage });
|
|
272
240
|
}
|
|
273
241
|
catch (error) {
|
|
274
|
-
this.
|
|
275
|
-
kind: "model.call.end",
|
|
276
|
-
provider: this.model.provider,
|
|
277
|
-
modelId: this.model.modelId,
|
|
278
|
-
finishReason: "error",
|
|
279
|
-
threadId: this.tid,
|
|
280
|
-
agentId: this.agent.id,
|
|
281
|
-
context: this.context,
|
|
282
|
-
});
|
|
242
|
+
this.emit("model.call.end", { finishReason: "error" });
|
|
283
243
|
yield {
|
|
284
244
|
kind: "error",
|
|
285
245
|
error: error instanceof Error ? error : new Error(String(error)),
|
|
@@ -356,9 +316,29 @@ export class Thread {
|
|
|
356
316
|
cancel() {
|
|
357
317
|
this.abort?.abort();
|
|
358
318
|
}
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
319
|
+
/**
|
|
320
|
+
* Emit an agent event with common fields auto-filled.
|
|
321
|
+
*/
|
|
322
|
+
emit(kind, payload) {
|
|
323
|
+
const base = {
|
|
324
|
+
kind,
|
|
325
|
+
threadId: this.tid,
|
|
326
|
+
agentId: this.agent.id,
|
|
327
|
+
context: this.context,
|
|
328
|
+
};
|
|
329
|
+
let auto = {};
|
|
330
|
+
switch (kind) {
|
|
331
|
+
case "thread.start":
|
|
332
|
+
case "thread.stop":
|
|
333
|
+
auto = { namespace: this.namespace };
|
|
334
|
+
break;
|
|
335
|
+
case "model.call.start":
|
|
336
|
+
case "model.call.end":
|
|
337
|
+
auto = { provider: this.model.provider, modelId: this.model.modelId };
|
|
338
|
+
break;
|
|
339
|
+
}
|
|
340
|
+
this.agent.emit(kind, { ...base, ...auto, ...payload });
|
|
341
|
+
}
|
|
362
342
|
/**
|
|
363
343
|
* Perform the actions returned by the model
|
|
364
344
|
*/
|
|
@@ -404,11 +384,7 @@ export class Thread {
|
|
|
404
384
|
async executeTools(calls) {
|
|
405
385
|
return await Promise.all(calls.map(async (call) => {
|
|
406
386
|
const parsedArgs = JSON.parse(call.arguments || "{}");
|
|
407
|
-
this.
|
|
408
|
-
kind: "tool.call.start",
|
|
409
|
-
threadId: this.tid,
|
|
410
|
-
agentId: this.agent.id,
|
|
411
|
-
context: this.context,
|
|
387
|
+
this.emit("tool.call.start", {
|
|
412
388
|
toolId: call.toolId,
|
|
413
389
|
callId: call.callId,
|
|
414
390
|
args: parsedArgs,
|
|
@@ -426,11 +402,7 @@ export class Thread {
|
|
|
426
402
|
ctx.agent = this.agent;
|
|
427
403
|
ctx.approve(call.callId); // mark this call as approved
|
|
428
404
|
const res = await tool.invoke(ctx, call.arguments, call.callId);
|
|
429
|
-
this.
|
|
430
|
-
kind: "tool.call.end",
|
|
431
|
-
threadId: this.tid,
|
|
432
|
-
agentId: this.agent.id,
|
|
433
|
-
context: this.context,
|
|
405
|
+
this.emit("tool.call.end", {
|
|
434
406
|
toolId: call.toolId,
|
|
435
407
|
callId: call.callId,
|
|
436
408
|
state: res.state,
|
|
@@ -449,11 +421,7 @@ export class Thread {
|
|
|
449
421
|
};
|
|
450
422
|
}
|
|
451
423
|
catch (error) {
|
|
452
|
-
this.
|
|
453
|
-
kind: "tool.call.end",
|
|
454
|
-
threadId: this.tid,
|
|
455
|
-
agentId: this.agent.id,
|
|
456
|
-
context: this.context,
|
|
424
|
+
this.emit("tool.call.end", {
|
|
457
425
|
toolId: call.toolId,
|
|
458
426
|
callId: call.callId,
|
|
459
427
|
state: FAILED,
|
package/dist/thread/types.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { ToolCall, LanguageModel, LanguageModelItem,
|
|
1
|
+
import { ToolCall, LanguageModel, LanguageModelItem, RUNNING, STOPPED, INTERRUPTIBLE, UNINTERRUPTIBLE, ZOMBIE, DEAD, TextStartEvent, TextEndEvent, TextDeltaEvent, ReasoningStartEvent, ReasoningEndEvent, ReasoningDeltaEvent, ToolInputStartEvent, ToolInputEndEvent, ToolInputDeltaEvent, StartEvent, FinishEvent, AbortEvent, ErrorEvent, RawEvent } from "@kernl-sdk/protocol";
|
|
2
2
|
import { Task } from "../task.js";
|
|
3
3
|
import { Context } from "../context.js";
|
|
4
4
|
import { Agent } from "../agent.js";
|
|
@@ -79,9 +79,24 @@ export interface ThreadSystemEvent extends ThreadEventBase {
|
|
|
79
79
|
*/
|
|
80
80
|
export type ThreadEvent = (LanguageModelItem & ThreadEventBase) | ThreadSystemEvent;
|
|
81
81
|
/**
|
|
82
|
-
*
|
|
82
|
+
* Incremental content chunks (ephemeral, not persisted).
|
|
83
83
|
*/
|
|
84
|
-
export type
|
|
84
|
+
export type StreamDeltaEvent = TextDeltaEvent | ReasoningDeltaEvent | ToolInputDeltaEvent;
|
|
85
|
+
/**
|
|
86
|
+
* Boundary markers + control flow (ephemeral, not persisted).
|
|
87
|
+
*/
|
|
88
|
+
export type StreamControlEvent = TextStartEvent | TextEndEvent | ReasoningStartEvent | ReasoningEndEvent | ToolInputStartEvent | ToolInputEndEvent | StartEvent | FinishEvent | AbortEvent | ErrorEvent | RawEvent;
|
|
89
|
+
/**
|
|
90
|
+
* All ephemeral stream types (not persisted to history).
|
|
91
|
+
*/
|
|
92
|
+
export type StreamEvent = StreamDeltaEvent | StreamControlEvent;
|
|
93
|
+
/**
|
|
94
|
+
* Thread stream events = sequenced ThreadEvents + ephemeral StreamEvents.
|
|
95
|
+
*
|
|
96
|
+
* Complete items (Message, ToolCall, etc.) are yielded as ThreadEvents with seq.
|
|
97
|
+
* Deltas and control events are yielded as StreamEvents without seq.
|
|
98
|
+
*/
|
|
99
|
+
export type ThreadStreamEvent = ThreadEvent | StreamEvent;
|
|
85
100
|
/**
|
|
86
101
|
* Result of thread execution
|
|
87
102
|
*/
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/thread/types.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,QAAQ,EACR,aAAa,EACb,iBAAiB,EACjB,
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/thread/types.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,QAAQ,EACR,aAAa,EACb,iBAAiB,EACjB,OAAO,EACP,OAAO,EACP,aAAa,EACb,eAAe,EACf,MAAM,EACN,IAAI,EAEJ,cAAc,EACd,YAAY,EACZ,cAAc,EACd,mBAAmB,EACnB,iBAAiB,EACjB,mBAAmB,EACnB,mBAAmB,EACnB,iBAAiB,EACjB,mBAAmB,EACnB,UAAU,EACV,WAAW,EACX,UAAU,EACV,UAAU,EACV,QAAQ,EACT,MAAM,qBAAqB,CAAC;AAE7B,OAAO,EAAE,IAAI,EAAE,MAAM,QAAQ,CAAC;AAC9B,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AACpC,OAAO,EAAE,KAAK,EAAE,MAAM,SAAS,CAAC;AAEhC,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,eAAe,CAAC;AACrD,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,WAAW,CAAC;AAE7C;;GAEG;AACH,MAAM,MAAM,iBAAiB,GAAG,iBAAiB,GAAG,eAAe,CAAC;AAEpE;;GAEG;AACH,MAAM,MAAM,UAAU,GAAG,MAAM,CAAC;AAEhC;;GAEG;AACH,eAAO,MAAM,aAAa,uFAOhB,CAAC;AAEX;;GAEG;AACH,MAAM,MAAM,WAAW,GACnB,OAAO,OAAO,GACd,OAAO,OAAO,GACd,OAAO,aAAa,GACpB,OAAO,eAAe,GACtB,OAAO,MAAM,GACb,OAAO,IAAI,CAAC;AAEhB;;;GAGG;AACH,eAAO,MAAM,iBAAiB,sBAAsB,CAAC;AAErD;;;;GAIG;AACH,MAAM,WAAW,OAAO,CACtB,QAAQ,GAAG,OAAO,EAClB,OAAO,SAAS,eAAe,GAAG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MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,IAAI,CAAC;IAC1C;;;;OAIG;IACH,SAAS,CAAC,EAAE,OAAO,CAAC;CACrB;AAED;;GAEG;AACH,MAAM,WAAW,oBAAoB,CAAC,QAAQ;IAC5C,OAAO,CAAC,EAAE,QAAQ,CAAC;IACnB,KAAK,CAAC,EAAE,aAAa,CAAC;IACtB,IAAI,CAAC,EAAE,IAAI,CAAC,QAAQ,CAAC,CAAC;IACtB,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,KAAK,CAAC,EAAE,WAAW,CAAC;CACrB;AAED;;GAEG;AACH,MAAM,WAAW,SAAS;IACxB,SAAS,EAAE,QAAQ,EAAE,CAAC;CAEvB;AAED;;GAEG;AACH,MAAM,WAAW,oBAAoB;IACnC;;OAEG;IACH,OAAO,EAAE,gBAAgB,EAAE,CAAC;IAC5B;;OAEG;IACH,gBAAgB,EAAE,QAAQ,EAAE,CAAC;CAC9B"}
|
package/dist/thread/utils.d.ts
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import type { ResolvedAgentResponse } from "../guardrail.js";
|
|
2
|
-
import { ToolCall, LanguageModelItem } from "@kernl-sdk/protocol";
|
|
2
|
+
import { ToolCall, LanguageModelItem, LanguageModelStreamEvent } from "@kernl-sdk/protocol";
|
|
3
3
|
import type { AgentOutputType } from "../agent/types.js";
|
|
4
|
-
import type { ThreadEvent,
|
|
4
|
+
import type { ThreadEvent, ThreadEventBase, ActionSet, PublicThreadEvent } from "./types.js";
|
|
5
5
|
/**
|
|
6
6
|
* Create a ThreadEvent from a LanguageModelItem with thread metadata.
|
|
7
7
|
*
|
|
8
8
|
* @example
|
|
9
|
-
* ```
|
|
9
|
+
* ```ts
|
|
10
10
|
* tevent({
|
|
11
11
|
* kind: "message",
|
|
12
12
|
* seq: 0,
|
|
@@ -28,17 +28,17 @@ export declare function tevent(event: {
|
|
|
28
28
|
/**
|
|
29
29
|
* Check if an event is a tool call
|
|
30
30
|
*/
|
|
31
|
-
export declare function isActionIntention(event:
|
|
31
|
+
export declare function isActionIntention(event: ThreadEvent): event is ToolCall & ThreadEventBase;
|
|
32
32
|
/**
|
|
33
33
|
* Extract action intentions from a list of events.
|
|
34
34
|
* Returns ActionSet if there are any tool calls, null otherwise.
|
|
35
35
|
*/
|
|
36
|
-
export declare function getIntentions(events:
|
|
36
|
+
export declare function getIntentions(events: ThreadEvent[]): ActionSet | null;
|
|
37
37
|
/**
|
|
38
38
|
* Check if an event is NOT a delta/start/end event (i.e., a complete item).
|
|
39
39
|
* Returns true for complete items: Message, Reasoning, ToolCall, ToolResult
|
|
40
40
|
*/
|
|
41
|
-
export declare function notDelta(event:
|
|
41
|
+
export declare function notDelta(event: LanguageModelStreamEvent): event is LanguageModelItem;
|
|
42
42
|
/**
|
|
43
43
|
* Check if an event is public/client-facing (not internal).
|
|
44
44
|
* Filters out internal system events that clients don't need.
|
|
@@ -48,7 +48,7 @@ export declare function isPublicEvent(event: ThreadEvent): event is PublicThread
|
|
|
48
48
|
* Extract the final text response from a list of items.
|
|
49
49
|
* Returns null if no assistant message with text content is found.
|
|
50
50
|
*/
|
|
51
|
-
export declare function getFinalResponse(items:
|
|
51
|
+
export declare function getFinalResponse(items: ThreadEvent[]): string | null;
|
|
52
52
|
/**
|
|
53
53
|
* Parse the final response according to the output type schema.
|
|
54
54
|
*
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../src/thread/utils.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,aAAa,CAAC;AAIzD,OAAO,
|
|
1
|
+
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../src/thread/utils.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,aAAa,CAAC;AAI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}
|
package/dist/thread/utils.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "kernl",
|
|
3
|
-
"version": "0.12.
|
|
3
|
+
"version": "0.12.1",
|
|
4
4
|
"description": "A modern AI agent framework",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"kernl",
|
|
@@ -42,13 +42,11 @@
|
|
|
42
42
|
"zod": "^4.1.8"
|
|
43
43
|
},
|
|
44
44
|
"devDependencies": {
|
|
45
|
-
"@ai-sdk/openai": "3.0.0-beta.57",
|
|
46
45
|
"@types/node": "^24.10.0",
|
|
47
46
|
"tsc-alias": "^1.8.10",
|
|
48
47
|
"typescript": "5.9.2",
|
|
49
48
|
"vitest": "^4.0.8",
|
|
50
|
-
"zod": "^4.1.8"
|
|
51
|
-
"@kernl-sdk/ai": "0.4.0"
|
|
49
|
+
"zod": "^4.1.8"
|
|
52
50
|
},
|
|
53
51
|
"scripts": {
|
|
54
52
|
"clean": "rm -rf dist",
|
package/src/index.ts
CHANGED
|
@@ -4,8 +4,8 @@ import {
|
|
|
4
4
|
LanguageModelResponse,
|
|
5
5
|
LanguageModelResponseItem,
|
|
6
6
|
LanguageModelItem,
|
|
7
|
+
LanguageModelStreamEvent,
|
|
7
8
|
} from "@kernl-sdk/protocol";
|
|
8
|
-
import type { ThreadStreamEvent } from "@/thread/types";
|
|
9
9
|
|
|
10
10
|
/**
|
|
11
11
|
* A mock language model that echoes the user input back as an assistant message.
|
|
@@ -54,7 +54,7 @@ export class MockLanguageModel implements LanguageModel {
|
|
|
54
54
|
|
|
55
55
|
async *stream(
|
|
56
56
|
request: LanguageModelRequest,
|
|
57
|
-
): AsyncIterable<
|
|
57
|
+
): AsyncIterable<LanguageModelStreamEvent> {
|
|
58
58
|
// TODO: Implement streaming (not needed for hello world)
|
|
59
59
|
throw new Error("MockLanguageModel.stream() not implemented yet");
|
|
60
60
|
}
|