@absolutejs/agent-runtime
Advanced tools
| import type { AgentRuntime } from "./types"; | ||
| export type AgentRuntimeWorkerMetrics = { | ||
| active: boolean; | ||
| claimed: number; | ||
| completed: number; | ||
| draining: boolean; | ||
| failed: number; | ||
| lastRunMs: number; | ||
| polls: number; | ||
| }; | ||
| export declare const createAgentRuntimeWorker: (options: { | ||
| onError?: (error: unknown) => void; | ||
| pollIntervalMs?: number; | ||
| runtime: Pick<AgentRuntime, "workOne">; | ||
| workerId?: string; | ||
| }) => { | ||
| drain: () => void; | ||
| metrics: () => AgentRuntimeWorkerMetrics; | ||
| runOnce: () => Promise<import("./types").AgentRun | undefined>; | ||
| start: () => void; | ||
| stop: () => Promise<void>; | ||
| }; |
+1
-0
@@ -6,1 +6,2 @@ export * from "./types"; | ||
| export * from "./postgres"; | ||
| export * from "./worker"; |
+78
-0
@@ -673,2 +673,79 @@ // @bun | ||
| }; | ||
| // src/worker.ts | ||
| var createAgentRuntimeWorker = (options) => { | ||
| const pollIntervalMs = options.pollIntervalMs ?? 1000; | ||
| const workerId = options.workerId ?? crypto.randomUUID(); | ||
| let active = false; | ||
| let claimed = 0; | ||
| let completed = 0; | ||
| let draining = false; | ||
| let failed = 0; | ||
| let lastRunMs = 0; | ||
| let polls = 0; | ||
| let timer; | ||
| const runOnce = async () => { | ||
| if (active || draining) | ||
| return; | ||
| active = true; | ||
| polls += 1; | ||
| const startedAt = Date.now(); | ||
| try { | ||
| const run = await options.runtime.workOne(workerId); | ||
| if (run) { | ||
| claimed += 1; | ||
| if (["cancelled", "completed", "failed", "handed_off"].includes(run.status)) | ||
| completed += 1; | ||
| } | ||
| return run; | ||
| } catch (error) { | ||
| failed += 1; | ||
| options.onError?.(error); | ||
| throw error; | ||
| } finally { | ||
| lastRunMs = Date.now() - startedAt; | ||
| active = false; | ||
| } | ||
| }; | ||
| const schedule = () => { | ||
| if (timer || draining) | ||
| return; | ||
| timer = setTimeout(async () => { | ||
| timer = undefined; | ||
| try { | ||
| await runOnce(); | ||
| } catch {} | ||
| schedule(); | ||
| }, pollIntervalMs); | ||
| }; | ||
| return { | ||
| drain: () => { | ||
| draining = true; | ||
| if (timer) | ||
| clearTimeout(timer); | ||
| timer = undefined; | ||
| }, | ||
| metrics: () => ({ | ||
| active, | ||
| claimed, | ||
| completed, | ||
| draining, | ||
| failed, | ||
| lastRunMs, | ||
| polls | ||
| }), | ||
| runOnce, | ||
| start: () => { | ||
| draining = false; | ||
| schedule(); | ||
| }, | ||
| stop: async () => { | ||
| draining = true; | ||
| if (timer) | ||
| clearTimeout(timer); | ||
| timer = undefined; | ||
| while (active) | ||
| await Bun.sleep(1); | ||
| } | ||
| }; | ||
| }; | ||
| export { | ||
@@ -680,2 +757,3 @@ zeroUsage, | ||
| createMemoryAgentRuntimeStore, | ||
| createAgentRuntimeWorker, | ||
| createAgentRuntime, | ||
@@ -682,0 +760,0 @@ budgetExceeded, |
+1
-1
| { | ||
| "name": "@absolutejs/agent-runtime", | ||
| "version": "0.2.0", | ||
| "version": "0.2.1", | ||
| "description": "Durable provider-neutral AI agent runs, steps, checkpoints, budgets, leases, timers, effects, and handoffs.", | ||
@@ -5,0 +5,0 @@ "type": "module", |
39266
6.38%14
7.69%1103
10.08%