forked from Lilly-Protocol/agentlily-runtime
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaction-executor.ts
More file actions
156 lines (138 loc) · 4.6 KB
/
Copy pathaction-executor.ts
File metadata and controls
156 lines (138 loc) · 4.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import type { RuntimeEventBus } from "../events/runtime-events.js";
import { assertMaxToolCalls } from "../guards/runtime-guards.js";
import type { RuntimeLogger } from "../logger/runtime-logger.js";
import type { RuntimeContext } from "../runtime/context.js";
import { ToolRegistry } from "../tools/tool-registry.js";
function resolveAgentId(
agent: { agentId?: string; id?: string } | undefined
): string {
return agent?.agentId ?? agent?.id ?? "";
}
export class ActionExecutor {
private readonly toolCallCounts = new Map<string, number>();
private readonly logger: RuntimeLogger | undefined;
private readonly eventBus: RuntimeEventBus | undefined;
private readonly maxToolCallsPerTask: number | undefined;
private readonly maxTrackedTasks: number;
public constructor(
private readonly toolRegistry: ToolRegistry,
maxToolCallsPerTaskOrLogger?: number | RuntimeLogger,
eventBus?: RuntimeEventBus,
maxTrackedTasks = 1_000
) {
if (!Number.isInteger(maxTrackedTasks) || maxTrackedTasks < 1) {
throw new RangeError("maxTrackedTasks must be a positive integer.");
}
if (typeof maxToolCallsPerTaskOrLogger === "number") {
this.maxToolCallsPerTask = maxToolCallsPerTaskOrLogger;
if (
eventBusOrLogger !== undefined &&
"emit" in eventBusOrLogger &&
typeof eventBusOrLogger.emit === "function"
) {
this.eventBus = eventBusOrLogger;
this.logger = logger;
} else {
this.eventBus = undefined;
this.logger = (eventBusOrLogger as RuntimeLogger | undefined) ?? logger;
}
} else if (
maxToolCallsPerTaskOrLogger !== undefined &&
typeof maxToolCallsPerTaskOrLogger === "object" &&
("info" in maxToolCallsPerTaskOrLogger ||
"warn" in maxToolCallsPerTaskOrLogger ||
"debug" in maxToolCallsPerTaskOrLogger ||
"error" in maxToolCallsPerTaskOrLogger)
) {
this.maxToolCallsPerTask = undefined;
this.logger = maxToolCallsPerTaskOrLogger;
if (
eventBusOrLogger !== undefined &&
"emit" in eventBusOrLogger &&
typeof eventBusOrLogger.emit === "function"
) {
this.eventBus = eventBusOrLogger;
} else {
this.eventBus = undefined;
}
} else {
this.maxToolCallsPerTask = undefined;
if (
eventBusOrLogger !== undefined &&
"emit" in eventBusOrLogger &&
typeof eventBusOrLogger.emit === "function"
) {
this.eventBus = eventBusOrLogger;
this.logger = logger;
} else {
this.eventBus = undefined;
this.logger = (eventBusOrLogger as RuntimeLogger | undefined) ?? logger;
}
}
this.eventBus = eventBus;
this.maxTrackedTasks = maxTrackedTasks;
}
public getToolCallCount(taskId: string): number {
return this.toolCallCounts.get(taskId) ?? 0;
}
/** Clears the retained call budget for one completed task. */
public reset(taskId: string): void {
this.toolCallCounts.delete(taskId);
}
/** Clears all retained per-task call budgets. */
public resetAll(): void {
this.toolCallCounts.clear();
}
public async execute<TPayload, TResult>(
toolName: string,
payload: TPayload,
context: RuntimeContext
): Promise<TResult> {
const tool = this.toolRegistry.get(toolName);
const currentCount = this.getToolCallCount(context.taskId);
if (this.maxToolCallsPerTask !== undefined) {
assertMaxToolCalls(currentCount, this.maxToolCallsPerTask);
}
const tool = this.toolRegistry.get(toolName);
this.toolCallCounts.set(context.taskId, currentCount + 1);
const startedAt = Date.now();
this.eventBus?.emit({
name: "runtime.tool.invoked",
payload: {
runtimeId: context.runtimeId,
taskId: context.taskId,
agentId: resolveAgentId(context.agent),
toolName,
},
});
try {
const result = await tool.execute(payload, context);
const duration = Date.now() - startedAt;
this.eventBus?.emit({
name: "runtime.tool.success",
payload: {
runtimeId: context.runtimeId,
taskId: context.taskId,
agentId: resolveAgentId(context.agent),
toolName,
duration,
},
});
return result;
} catch (error) {
const duration = Date.now() - startedAt;
this.eventBus?.emit({
name: "runtime.tool.error",
payload: {
runtimeId: context.runtimeId,
taskId: context.taskId,
agentId: resolveAgentId(context.agent),
toolName,
duration,
error,
},
});
throw error;
}
}
}