forked from Lilly-Protocol/agentlily-runtime
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask-runner.ts
More file actions
71 lines (63 loc) · 2.28 KB
/
Copy pathtask-runner.ts
File metadata and controls
71 lines (63 loc) · 2.28 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
import type { ActionExecutor } from "../actions/action-executor.js";
import { RuntimeError } from "../errors/runtime-errors.js";
import { assertNonEmptyValue } from "../guards/runtime-guards.js";
import type { MemoryStore } from "../memory/memory-store.js";
import type { RuntimeContext } from "../runtime/context.js";
import type { RuntimeTask, TaskExecutionResult } from "./task-types.js";
export class TaskRunner {
private readonly actionExecutor: ActionExecutor;
private readonly memoryStore: MemoryStore;
public constructor(actionExecutor: ActionExecutor, memoryStore: MemoryStore) {
this.actionExecutor = actionExecutor;
this.memoryStore = memoryStore;
}
public async run<TPayload, TResult>(
task: RuntimeTask<TPayload>,
context: RuntimeContext
): Promise<TaskExecutionResult<TResult>> {
assertNonEmptyValue(task.taskId, "taskId");
assertNonEmptyValue(task.agentId, "agentId");
assertNonEmptyValue(task.toolName, "toolName");
assertNonEmptyValue(task.input, "input");
const startTime = performance.now();
const startedAt = new Date().toISOString();
try {
const output = await this.actionExecutor.execute<TPayload, TResult>(
task.toolName,
task.payload,
context
);
const endTime = performance.now();
const completedAt = new Date().toISOString();
const durationMs = Math.max(0, Math.round(endTime - startTime));
await this.memoryStore.append({
agentId: task.agentId,
taskId: task.taskId,
input: task.input,
output,
recordedAt: completedAt
});
return {
taskId: task.taskId,
agentId: task.agentId,
toolName: task.toolName,
output,
startedAt,
completedAt,
durationMs
};
} catch (error) {
// Typed runtime errors propagate unchanged; anything else thrown by a
// tool or the memory store is reported as an unexpected execution
// failure while preserving the original error message.
if (error instanceof RuntimeError) {
throw error;
}
throw new RuntimeError(
"EXECUTION_FAILED",
error instanceof Error ? error.message : "Task execution failed.",
error instanceof Error ? { cause: error.message } : undefined
);
}
}
}