kraken-ai 0.0.6 → 0.0.7
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/dist/index.cjs +59 -5
- package/dist/index.d.cts +14 -2
- package/dist/index.d.ts +14 -2
- package/dist/index.js +59 -5
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -492,14 +492,14 @@ var composeOnLLMCall = (middleware, ctx, coreFn) => {
|
|
|
492
492
|
}
|
|
493
493
|
return fn;
|
|
494
494
|
};
|
|
495
|
-
var composeOnToolCall = (middleware, ctx, coreFn, toolName, args) => {
|
|
495
|
+
var composeOnToolCall = (middleware, ctx, coreFn, toolName, toolCallId, args) => {
|
|
496
496
|
let fn = coreFn;
|
|
497
497
|
for (let i = middleware.length - 1; i >= 0; i--) {
|
|
498
498
|
const mw = middleware[i];
|
|
499
499
|
if (mw?.onToolCall) {
|
|
500
500
|
const prev = fn;
|
|
501
501
|
const hook = mw.onToolCall;
|
|
502
|
-
fn = () => hook(ctx, toolName, args, prev);
|
|
502
|
+
fn = () => hook(ctx, toolName, toolCallId, args, prev);
|
|
503
503
|
}
|
|
504
504
|
}
|
|
505
505
|
return fn;
|
|
@@ -511,6 +511,41 @@ var applyTransformTools = (middleware, ctx, tools) => {
|
|
|
511
511
|
}
|
|
512
512
|
return result;
|
|
513
513
|
};
|
|
514
|
+
var composeOnEvent = (middleware, ctx, sink, warn) => {
|
|
515
|
+
const hooks = [];
|
|
516
|
+
for (const mw of middleware) {
|
|
517
|
+
if (mw.onEvent) hooks.push(mw.onEvent);
|
|
518
|
+
}
|
|
519
|
+
if (hooks.length === 0) return sink;
|
|
520
|
+
return (event) => {
|
|
521
|
+
const origSeq = event.seq;
|
|
522
|
+
const origRunId = event.runId;
|
|
523
|
+
const origAgentName = event.agentName;
|
|
524
|
+
let next = () => {
|
|
525
|
+
if (event.seq !== origSeq || event.runId !== origRunId || event.agentName !== origAgentName) {
|
|
526
|
+
warn?.("onEvent middleware mutated immutable fields (seq/runId/agentName) \u2014 restoring");
|
|
527
|
+
event.seq = origSeq;
|
|
528
|
+
event.runId = origRunId;
|
|
529
|
+
event.agentName = origAgentName;
|
|
530
|
+
}
|
|
531
|
+
sink?.(event);
|
|
532
|
+
};
|
|
533
|
+
for (let i = hooks.length - 1; i >= 0; i--) {
|
|
534
|
+
const hook = hooks[i];
|
|
535
|
+
if (!hook) continue;
|
|
536
|
+
const prev = next;
|
|
537
|
+
next = () => {
|
|
538
|
+
try {
|
|
539
|
+
hook(ctx, event, prev);
|
|
540
|
+
} catch (err) {
|
|
541
|
+
warn?.(`onEvent middleware threw: ${err instanceof Error ? err.message : String(err)}`);
|
|
542
|
+
prev();
|
|
543
|
+
}
|
|
544
|
+
};
|
|
545
|
+
}
|
|
546
|
+
next();
|
|
547
|
+
};
|
|
548
|
+
};
|
|
514
549
|
var createRunner = (config2) => {
|
|
515
550
|
const { agent, model, tools } = config2;
|
|
516
551
|
const toolMap = new Map(tools.map((t) => [t.name, t]));
|
|
@@ -532,7 +567,13 @@ var createRunner = (config2) => {
|
|
|
532
567
|
runId,
|
|
533
568
|
parentRunId: options?.parentRunId
|
|
534
569
|
};
|
|
535
|
-
const
|
|
570
|
+
const composedOnEvent = composeOnEvent(
|
|
571
|
+
middleware,
|
|
572
|
+
ctx,
|
|
573
|
+
options?.onEvent,
|
|
574
|
+
(msg) => logger.warn(msg)
|
|
575
|
+
);
|
|
576
|
+
const emitter = createEventEmitter(agent.name, runId, composedOnEvent, options?.parentRunId);
|
|
536
577
|
const lifecycle = new LifecycleManager((from, to) => {
|
|
537
578
|
emitter.emit({ type: "lifecycle", from, to });
|
|
538
579
|
for (const mw of middleware) mw.onTransition?.(ctx, from, to);
|
|
@@ -586,7 +627,19 @@ var createRunner = (config2) => {
|
|
|
586
627
|
thinkingLevel: config2.thinkingLevel,
|
|
587
628
|
responseSchema: config2.responseSchema
|
|
588
629
|
};
|
|
589
|
-
|
|
630
|
+
let response;
|
|
631
|
+
try {
|
|
632
|
+
response = await wrappedGenerate(params);
|
|
633
|
+
} catch (err) {
|
|
634
|
+
emitter.emit({
|
|
635
|
+
type: "llm_call",
|
|
636
|
+
inputTokens: 0,
|
|
637
|
+
outputTokens: 0,
|
|
638
|
+
output: "",
|
|
639
|
+
error: err instanceof Error ? err.message : String(err)
|
|
640
|
+
});
|
|
641
|
+
throw err;
|
|
642
|
+
}
|
|
590
643
|
usage.inputTokens += response.usage.inputTokens;
|
|
591
644
|
usage.outputTokens += response.usage.outputTokens;
|
|
592
645
|
if (!model.generateStream && response.thoughts) {
|
|
@@ -676,6 +729,7 @@ var createRunner = (config2) => {
|
|
|
676
729
|
ctx,
|
|
677
730
|
executeFn,
|
|
678
731
|
tc.name,
|
|
732
|
+
tc.id,
|
|
679
733
|
tc.args
|
|
680
734
|
);
|
|
681
735
|
const startTime = Date.now();
|
|
@@ -1014,7 +1068,7 @@ var formatEvent = (e) => {
|
|
|
1014
1068
|
case "interrupt":
|
|
1015
1069
|
return `${tag} interrupt: ${e.toolName} awaiting approval`;
|
|
1016
1070
|
case "kill":
|
|
1017
|
-
return `${tag} killed
|
|
1071
|
+
return `${tag} killed: ${e.reason}`;
|
|
1018
1072
|
case "error":
|
|
1019
1073
|
return `${tag} error: ${e.message}`;
|
|
1020
1074
|
case "notification":
|
package/dist/index.d.cts
CHANGED
|
@@ -36,7 +36,7 @@ interface Middleware {
|
|
|
36
36
|
/** Augment tool definitions visible to the LLM. Called once at run start. */
|
|
37
37
|
transformTools?: (ctx: MiddlewareContext, tools: ToolDefinition[]) => ToolDefinition[];
|
|
38
38
|
/** Wrap tool execution. Called for EVERY tool call including delegation. */
|
|
39
|
-
onToolCall?: (ctx: MiddlewareContext, toolName: string, args: unknown, next: () => Promise<unknown>) => Promise<unknown>;
|
|
39
|
+
onToolCall?: (ctx: MiddlewareContext, toolName: string, toolCallId: string, args: unknown, next: () => Promise<unknown>) => Promise<unknown>;
|
|
40
40
|
/** Wrap LLM generation. Pre-LLM and post-LLM gates via next(). */
|
|
41
41
|
onLLMCall?: (ctx: MiddlewareContext, params: GenerateParams, next: () => Promise<GenerateResponse>) => Promise<GenerateResponse>;
|
|
42
42
|
/** Called on every lifecycle transition. */
|
|
@@ -196,11 +196,21 @@ interface AgentLLMCallEvent extends AgentEventBase {
|
|
|
196
196
|
name: string;
|
|
197
197
|
args: Record<string, unknown>;
|
|
198
198
|
}[];
|
|
199
|
+
/** Set when the LLM call failed. */
|
|
200
|
+
error?: string;
|
|
201
|
+
/** Middleware enrichment: correlation ID linking this LLM call to its tool calls. */
|
|
202
|
+
turnId?: string;
|
|
203
|
+
/** Middleware enrichment: wall-clock duration in milliseconds. */
|
|
204
|
+
durationMs?: number;
|
|
205
|
+
/** Middleware enrichment: number of tool calls in the response. */
|
|
206
|
+
toolCallCount?: number;
|
|
199
207
|
}
|
|
200
208
|
interface AgentThoughtEvent extends AgentEventBase {
|
|
201
209
|
type: "thought";
|
|
202
210
|
text: string;
|
|
203
211
|
callIndex: number;
|
|
212
|
+
/** Middleware enrichment: correlation ID of the active LLM call turn. */
|
|
213
|
+
turnId?: string;
|
|
204
214
|
}
|
|
205
215
|
interface AgentToolCallEvent extends AgentEventBase {
|
|
206
216
|
type: "tool_call";
|
|
@@ -211,6 +221,8 @@ interface AgentToolCallEvent extends AgentEventBase {
|
|
|
211
221
|
result?: unknown;
|
|
212
222
|
error?: string;
|
|
213
223
|
durationMs: number;
|
|
224
|
+
/** Middleware enrichment: correlation ID linking this tool call to its parent LLM call. */
|
|
225
|
+
turnId?: string;
|
|
214
226
|
}
|
|
215
227
|
interface AgentDelegationEvent extends AgentEventBase {
|
|
216
228
|
type: "delegation";
|
|
@@ -227,7 +239,7 @@ interface AgentInterruptEvent extends AgentEventBase {
|
|
|
227
239
|
}
|
|
228
240
|
interface AgentKillEvent extends AgentEventBase {
|
|
229
241
|
type: "kill";
|
|
230
|
-
reason
|
|
242
|
+
reason: string;
|
|
231
243
|
}
|
|
232
244
|
interface AgentErrorEvent extends AgentEventBase {
|
|
233
245
|
type: "error";
|
package/dist/index.d.ts
CHANGED
|
@@ -36,7 +36,7 @@ interface Middleware {
|
|
|
36
36
|
/** Augment tool definitions visible to the LLM. Called once at run start. */
|
|
37
37
|
transformTools?: (ctx: MiddlewareContext, tools: ToolDefinition[]) => ToolDefinition[];
|
|
38
38
|
/** Wrap tool execution. Called for EVERY tool call including delegation. */
|
|
39
|
-
onToolCall?: (ctx: MiddlewareContext, toolName: string, args: unknown, next: () => Promise<unknown>) => Promise<unknown>;
|
|
39
|
+
onToolCall?: (ctx: MiddlewareContext, toolName: string, toolCallId: string, args: unknown, next: () => Promise<unknown>) => Promise<unknown>;
|
|
40
40
|
/** Wrap LLM generation. Pre-LLM and post-LLM gates via next(). */
|
|
41
41
|
onLLMCall?: (ctx: MiddlewareContext, params: GenerateParams, next: () => Promise<GenerateResponse>) => Promise<GenerateResponse>;
|
|
42
42
|
/** Called on every lifecycle transition. */
|
|
@@ -196,11 +196,21 @@ interface AgentLLMCallEvent extends AgentEventBase {
|
|
|
196
196
|
name: string;
|
|
197
197
|
args: Record<string, unknown>;
|
|
198
198
|
}[];
|
|
199
|
+
/** Set when the LLM call failed. */
|
|
200
|
+
error?: string;
|
|
201
|
+
/** Middleware enrichment: correlation ID linking this LLM call to its tool calls. */
|
|
202
|
+
turnId?: string;
|
|
203
|
+
/** Middleware enrichment: wall-clock duration in milliseconds. */
|
|
204
|
+
durationMs?: number;
|
|
205
|
+
/** Middleware enrichment: number of tool calls in the response. */
|
|
206
|
+
toolCallCount?: number;
|
|
199
207
|
}
|
|
200
208
|
interface AgentThoughtEvent extends AgentEventBase {
|
|
201
209
|
type: "thought";
|
|
202
210
|
text: string;
|
|
203
211
|
callIndex: number;
|
|
212
|
+
/** Middleware enrichment: correlation ID of the active LLM call turn. */
|
|
213
|
+
turnId?: string;
|
|
204
214
|
}
|
|
205
215
|
interface AgentToolCallEvent extends AgentEventBase {
|
|
206
216
|
type: "tool_call";
|
|
@@ -211,6 +221,8 @@ interface AgentToolCallEvent extends AgentEventBase {
|
|
|
211
221
|
result?: unknown;
|
|
212
222
|
error?: string;
|
|
213
223
|
durationMs: number;
|
|
224
|
+
/** Middleware enrichment: correlation ID linking this tool call to its parent LLM call. */
|
|
225
|
+
turnId?: string;
|
|
214
226
|
}
|
|
215
227
|
interface AgentDelegationEvent extends AgentEventBase {
|
|
216
228
|
type: "delegation";
|
|
@@ -227,7 +239,7 @@ interface AgentInterruptEvent extends AgentEventBase {
|
|
|
227
239
|
}
|
|
228
240
|
interface AgentKillEvent extends AgentEventBase {
|
|
229
241
|
type: "kill";
|
|
230
|
-
reason
|
|
242
|
+
reason: string;
|
|
231
243
|
}
|
|
232
244
|
interface AgentErrorEvent extends AgentEventBase {
|
|
233
245
|
type: "error";
|
package/dist/index.js
CHANGED
|
@@ -174,14 +174,14 @@ var composeOnLLMCall = (middleware, ctx, coreFn) => {
|
|
|
174
174
|
}
|
|
175
175
|
return fn;
|
|
176
176
|
};
|
|
177
|
-
var composeOnToolCall = (middleware, ctx, coreFn, toolName, args) => {
|
|
177
|
+
var composeOnToolCall = (middleware, ctx, coreFn, toolName, toolCallId, args) => {
|
|
178
178
|
let fn = coreFn;
|
|
179
179
|
for (let i = middleware.length - 1; i >= 0; i--) {
|
|
180
180
|
const mw = middleware[i];
|
|
181
181
|
if (mw?.onToolCall) {
|
|
182
182
|
const prev = fn;
|
|
183
183
|
const hook = mw.onToolCall;
|
|
184
|
-
fn = () => hook(ctx, toolName, args, prev);
|
|
184
|
+
fn = () => hook(ctx, toolName, toolCallId, args, prev);
|
|
185
185
|
}
|
|
186
186
|
}
|
|
187
187
|
return fn;
|
|
@@ -193,6 +193,41 @@ var applyTransformTools = (middleware, ctx, tools) => {
|
|
|
193
193
|
}
|
|
194
194
|
return result;
|
|
195
195
|
};
|
|
196
|
+
var composeOnEvent = (middleware, ctx, sink, warn) => {
|
|
197
|
+
const hooks = [];
|
|
198
|
+
for (const mw of middleware) {
|
|
199
|
+
if (mw.onEvent) hooks.push(mw.onEvent);
|
|
200
|
+
}
|
|
201
|
+
if (hooks.length === 0) return sink;
|
|
202
|
+
return (event) => {
|
|
203
|
+
const origSeq = event.seq;
|
|
204
|
+
const origRunId = event.runId;
|
|
205
|
+
const origAgentName = event.agentName;
|
|
206
|
+
let next = () => {
|
|
207
|
+
if (event.seq !== origSeq || event.runId !== origRunId || event.agentName !== origAgentName) {
|
|
208
|
+
warn?.("onEvent middleware mutated immutable fields (seq/runId/agentName) \u2014 restoring");
|
|
209
|
+
event.seq = origSeq;
|
|
210
|
+
event.runId = origRunId;
|
|
211
|
+
event.agentName = origAgentName;
|
|
212
|
+
}
|
|
213
|
+
sink?.(event);
|
|
214
|
+
};
|
|
215
|
+
for (let i = hooks.length - 1; i >= 0; i--) {
|
|
216
|
+
const hook = hooks[i];
|
|
217
|
+
if (!hook) continue;
|
|
218
|
+
const prev = next;
|
|
219
|
+
next = () => {
|
|
220
|
+
try {
|
|
221
|
+
hook(ctx, event, prev);
|
|
222
|
+
} catch (err) {
|
|
223
|
+
warn?.(`onEvent middleware threw: ${err instanceof Error ? err.message : String(err)}`);
|
|
224
|
+
prev();
|
|
225
|
+
}
|
|
226
|
+
};
|
|
227
|
+
}
|
|
228
|
+
next();
|
|
229
|
+
};
|
|
230
|
+
};
|
|
196
231
|
var createRunner = (config2) => {
|
|
197
232
|
const { agent, model, tools } = config2;
|
|
198
233
|
const toolMap = new Map(tools.map((t) => [t.name, t]));
|
|
@@ -214,7 +249,13 @@ var createRunner = (config2) => {
|
|
|
214
249
|
runId,
|
|
215
250
|
parentRunId: options?.parentRunId
|
|
216
251
|
};
|
|
217
|
-
const
|
|
252
|
+
const composedOnEvent = composeOnEvent(
|
|
253
|
+
middleware,
|
|
254
|
+
ctx,
|
|
255
|
+
options?.onEvent,
|
|
256
|
+
(msg) => logger.warn(msg)
|
|
257
|
+
);
|
|
258
|
+
const emitter = createEventEmitter(agent.name, runId, composedOnEvent, options?.parentRunId);
|
|
218
259
|
const lifecycle = new LifecycleManager((from, to) => {
|
|
219
260
|
emitter.emit({ type: "lifecycle", from, to });
|
|
220
261
|
for (const mw of middleware) mw.onTransition?.(ctx, from, to);
|
|
@@ -268,7 +309,19 @@ var createRunner = (config2) => {
|
|
|
268
309
|
thinkingLevel: config2.thinkingLevel,
|
|
269
310
|
responseSchema: config2.responseSchema
|
|
270
311
|
};
|
|
271
|
-
|
|
312
|
+
let response;
|
|
313
|
+
try {
|
|
314
|
+
response = await wrappedGenerate(params);
|
|
315
|
+
} catch (err) {
|
|
316
|
+
emitter.emit({
|
|
317
|
+
type: "llm_call",
|
|
318
|
+
inputTokens: 0,
|
|
319
|
+
outputTokens: 0,
|
|
320
|
+
output: "",
|
|
321
|
+
error: err instanceof Error ? err.message : String(err)
|
|
322
|
+
});
|
|
323
|
+
throw err;
|
|
324
|
+
}
|
|
272
325
|
usage.inputTokens += response.usage.inputTokens;
|
|
273
326
|
usage.outputTokens += response.usage.outputTokens;
|
|
274
327
|
if (!model.generateStream && response.thoughts) {
|
|
@@ -358,6 +411,7 @@ var createRunner = (config2) => {
|
|
|
358
411
|
ctx,
|
|
359
412
|
executeFn,
|
|
360
413
|
tc.name,
|
|
414
|
+
tc.id,
|
|
361
415
|
tc.args
|
|
362
416
|
);
|
|
363
417
|
const startTime = Date.now();
|
|
@@ -693,7 +747,7 @@ var formatEvent = (e) => {
|
|
|
693
747
|
case "interrupt":
|
|
694
748
|
return `${tag} interrupt: ${e.toolName} awaiting approval`;
|
|
695
749
|
case "kill":
|
|
696
|
-
return `${tag} killed
|
|
750
|
+
return `${tag} killed: ${e.reason}`;
|
|
697
751
|
case "error":
|
|
698
752
|
return `${tag} error: ${e.message}`;
|
|
699
753
|
case "notification":
|