kraken-ai 0.0.6 → 0.0.8
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.
|
@@ -107,7 +107,7 @@ var buildRequestConfig = (params) => {
|
|
|
107
107
|
if (params.maxOutputTokens != null) generateConfig.maxOutputTokens = params.maxOutputTokens;
|
|
108
108
|
if (params.responseSchema) {
|
|
109
109
|
generateConfig.responseMimeType = "application/json";
|
|
110
|
-
generateConfig.
|
|
110
|
+
generateConfig.responseJsonSchema = params.responseSchema;
|
|
111
111
|
}
|
|
112
112
|
if (tools) generateConfig.tools = tools;
|
|
113
113
|
return generateConfig;
|
package/dist/index.cjs
CHANGED
|
@@ -204,7 +204,7 @@ var init_google = __esm({
|
|
|
204
204
|
if (params.maxOutputTokens != null) generateConfig.maxOutputTokens = params.maxOutputTokens;
|
|
205
205
|
if (params.responseSchema) {
|
|
206
206
|
generateConfig.responseMimeType = "application/json";
|
|
207
|
-
generateConfig.
|
|
207
|
+
generateConfig.responseJsonSchema = params.responseSchema;
|
|
208
208
|
}
|
|
209
209
|
if (tools) generateConfig.tools = tools;
|
|
210
210
|
return generateConfig;
|
|
@@ -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) {
|
|
@@ -644,7 +697,10 @@ var createRunner = (config2) => {
|
|
|
644
697
|
}
|
|
645
698
|
}
|
|
646
699
|
}
|
|
647
|
-
|
|
700
|
+
if (!foundTool) {
|
|
701
|
+
throw new ToolError(`Unknown tool: ${tc.name}`, tc.name);
|
|
702
|
+
}
|
|
703
|
+
const executeFn = () => foundTool.execute(tc.args);
|
|
648
704
|
approved.push({ tc, executeFn });
|
|
649
705
|
}
|
|
650
706
|
if (!approved.length) continue;
|
|
@@ -676,6 +732,7 @@ var createRunner = (config2) => {
|
|
|
676
732
|
ctx,
|
|
677
733
|
executeFn,
|
|
678
734
|
tc.name,
|
|
735
|
+
tc.id,
|
|
679
736
|
tc.args
|
|
680
737
|
);
|
|
681
738
|
const startTime = Date.now();
|
|
@@ -1014,7 +1071,7 @@ var formatEvent = (e) => {
|
|
|
1014
1071
|
case "interrupt":
|
|
1015
1072
|
return `${tag} interrupt: ${e.toolName} awaiting approval`;
|
|
1016
1073
|
case "kill":
|
|
1017
|
-
return `${tag} killed
|
|
1074
|
+
return `${tag} killed: ${e.reason}`;
|
|
1018
1075
|
case "error":
|
|
1019
1076
|
return `${tag} error: ${e.message}`;
|
|
1020
1077
|
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
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import {
|
|
2
2
|
google
|
|
3
|
-
} from "./chunk-
|
|
3
|
+
} from "./chunk-M2U6HNWW.js";
|
|
4
4
|
import {
|
|
5
5
|
openai
|
|
6
6
|
} from "./chunk-D2AU5FGJ.js";
|
|
@@ -40,7 +40,7 @@ var createLazyModel = (provider, modelId) => {
|
|
|
40
40
|
const ensureResolved = async () => {
|
|
41
41
|
if (resolvedModel) return resolvedModel;
|
|
42
42
|
if (provider === "google") {
|
|
43
|
-
const { google: google2 } = await import("./google-
|
|
43
|
+
const { google: google2 } = await import("./google-3DFIROPW.js");
|
|
44
44
|
resolvedModel = google2(modelId);
|
|
45
45
|
} else if (provider === "openai") {
|
|
46
46
|
const { openai: openai2 } = await import("./openai-STUANOLN.js");
|
|
@@ -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) {
|
|
@@ -326,7 +379,10 @@ var createRunner = (config2) => {
|
|
|
326
379
|
}
|
|
327
380
|
}
|
|
328
381
|
}
|
|
329
|
-
|
|
382
|
+
if (!foundTool) {
|
|
383
|
+
throw new ToolError(`Unknown tool: ${tc.name}`, tc.name);
|
|
384
|
+
}
|
|
385
|
+
const executeFn = () => foundTool.execute(tc.args);
|
|
330
386
|
approved.push({ tc, executeFn });
|
|
331
387
|
}
|
|
332
388
|
if (!approved.length) continue;
|
|
@@ -358,6 +414,7 @@ var createRunner = (config2) => {
|
|
|
358
414
|
ctx,
|
|
359
415
|
executeFn,
|
|
360
416
|
tc.name,
|
|
417
|
+
tc.id,
|
|
361
418
|
tc.args
|
|
362
419
|
);
|
|
363
420
|
const startTime = Date.now();
|
|
@@ -693,7 +750,7 @@ var formatEvent = (e) => {
|
|
|
693
750
|
case "interrupt":
|
|
694
751
|
return `${tag} interrupt: ${e.toolName} awaiting approval`;
|
|
695
752
|
case "kill":
|
|
696
|
-
return `${tag} killed
|
|
753
|
+
return `${tag} killed: ${e.reason}`;
|
|
697
754
|
case "error":
|
|
698
755
|
return `${tag} error: ${e.message}`;
|
|
699
756
|
case "notification":
|