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
69 lines (61 loc) · 2.27 KB
/
Copy pathtask-runner.ts
File metadata and controls
69 lines (61 loc) · 2.27 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
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();
// Tool execution errors are part of the tool contract and must propagate
// unchanged so callers retain the original error identity and code.
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));
try {
await this.memoryStore.append({
agentId: task.agentId,
taskId: task.taskId,
input: task.input,
output,
recordedAt: completedAt
});
} catch (error) {
// Persistence failures are runtime execution failures even when the
// underlying store happens to throw a typed RuntimeError of its own.
throw new RuntimeError(
"EXECUTION_FAILED",
error instanceof Error ? error.message : "Task execution failed.",
error instanceof Error ? { cause: error.message } : undefined
);
}
return {
taskId: task.taskId,
agentId: task.agentId,
toolName: task.toolName,
output,
startedAt,
completedAt,
durationMs
};
}
}