@botbotgo/agent-harness 0.0.108 → 0.0.109
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.
- package/dist/package-version.d.ts +1 -1
- package/dist/package-version.js +1 -1
- package/dist/runtime/adapter/execution-context.d.ts +7 -0
- package/dist/runtime/adapter/execution-context.js +9 -2
- package/dist/runtime/adapter/middleware-assembly.js +2 -2
- package/dist/runtime/adapter/runnable-config.d.ts +44 -0
- package/dist/runtime/adapter/runnable-config.js +51 -0
- package/dist/runtime/adapter/runtime-adapter-support.d.ts +0 -1
- package/dist/runtime/adapter/runtime-adapter-support.js +1 -4
- package/dist/runtime/agent-runtime-adapter.js +3 -5
- package/dist/runtime/harness/events/listener-runtime.d.ts +18 -0
- package/dist/runtime/harness/events/listener-runtime.js +9 -0
- package/dist/runtime/harness/events/runtime-event-operations.d.ts +17 -0
- package/dist/runtime/harness/events/runtime-event-operations.js +9 -0
- package/dist/runtime/harness/run/recovery.d.ts +1 -1
- package/dist/runtime/harness/run/resume-runtime.d.ts +55 -0
- package/dist/runtime/harness/run/resume-runtime.js +26 -0
- package/dist/runtime/harness/run/routing.d.ts +8 -0
- package/dist/runtime/harness/run/routing.js +21 -0
- package/dist/runtime/harness/run/run-operations.d.ts +47 -0
- package/dist/runtime/harness/run/run-operations.js +67 -0
- package/dist/runtime/harness/run/start-run.d.ts +82 -0
- package/dist/runtime/harness/run/start-run.js +88 -0
- package/dist/runtime/harness/run/startup-runtime.d.ts +2 -1
- package/dist/runtime/harness/run/startup-runtime.js +38 -3
- package/dist/runtime/harness/run/stream-runtime.d.ts +48 -0
- package/dist/runtime/harness/run/stream-runtime.js +14 -0
- package/dist/runtime/harness.d.ts +4 -3
- package/dist/runtime/harness.js +177 -252
- package/dist/runtime/support/runtime-adapter-options.d.ts +17 -0
- package/dist/runtime/support/runtime-adapter-options.js +29 -0
- package/package.json +1 -1
- package/dist/runtime/adapter/deepagent-runnable-config.d.ts +0 -13
- package/dist/runtime/adapter/deepagent-runnable-config.js +0 -29
- package/dist/runtime/adapter/langchain-runnable-config.d.ts +0 -11
- package/dist/runtime/adapter/langchain-runnable-config.js +0 -24
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import { AUTO_AGENT_ID } from "../../../contracts/types.js";
|
|
2
|
+
import { createPersistentId } from "../../../utils/id.js";
|
|
3
|
+
import { normalizeMessageContent } from "../../../utils/message-content.js";
|
|
4
|
+
import { buildPersistedRunRequest, normalizeRunPriority } from "./helpers.js";
|
|
5
|
+
import { getRequiredWorkspaceBinding } from "../bindings.js";
|
|
6
|
+
import { getBindingAdapterKind } from "../../support/compiled-binding.js";
|
|
7
|
+
export async function ensureThreadStarted(runtime, input) {
|
|
8
|
+
const { selectedAgentId, binding, message, runRequest, existingThreadId } = input;
|
|
9
|
+
const threadId = existingThreadId ?? createPersistentId();
|
|
10
|
+
const runId = createPersistentId();
|
|
11
|
+
const createdAt = new Date().toISOString();
|
|
12
|
+
const isNewThread = !existingThreadId;
|
|
13
|
+
const userMessage = {
|
|
14
|
+
role: "user",
|
|
15
|
+
content: normalizeMessageContent(message),
|
|
16
|
+
runId,
|
|
17
|
+
createdAt,
|
|
18
|
+
};
|
|
19
|
+
if (typeof runtime.persistence.bootstrapRun === "function") {
|
|
20
|
+
await runtime.persistence.bootstrapRun({
|
|
21
|
+
threadId,
|
|
22
|
+
agentId: binding.agent.id,
|
|
23
|
+
runId,
|
|
24
|
+
status: "running",
|
|
25
|
+
createdAt,
|
|
26
|
+
executionMode: getBindingAdapterKind(binding),
|
|
27
|
+
adapterKind: getBindingAdapterKind(binding),
|
|
28
|
+
userMessage,
|
|
29
|
+
runRequest,
|
|
30
|
+
createThread: isNewThread,
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
else {
|
|
34
|
+
if (isNewThread) {
|
|
35
|
+
await runtime.persistence.createThread({
|
|
36
|
+
threadId,
|
|
37
|
+
agentId: selectedAgentId,
|
|
38
|
+
runId,
|
|
39
|
+
status: "running",
|
|
40
|
+
createdAt,
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
await Promise.all([
|
|
44
|
+
runtime.persistence.appendThreadMessage(threadId, userMessage),
|
|
45
|
+
runtime.persistence.createRun({
|
|
46
|
+
threadId,
|
|
47
|
+
runId,
|
|
48
|
+
agentId: binding.agent.id,
|
|
49
|
+
executionMode: getBindingAdapterKind(binding),
|
|
50
|
+
adapterKind: getBindingAdapterKind(binding),
|
|
51
|
+
createdAt,
|
|
52
|
+
}),
|
|
53
|
+
runtime.persistence.saveRunRequest(threadId, runId, runRequest),
|
|
54
|
+
]);
|
|
55
|
+
}
|
|
56
|
+
return { threadId, runId, createdAt, isNewThread };
|
|
57
|
+
}
|
|
58
|
+
export async function prepareRunStart(runtime, input) {
|
|
59
|
+
const { options, invocation, runCreatedPayload } = input;
|
|
60
|
+
const selectedAgentId = await runtime.resolveSelectedAgentId(options.input, options.agentId, options.threadId);
|
|
61
|
+
const binding = getRequiredWorkspaceBinding(runtime.workspace, selectedAgentId);
|
|
62
|
+
const policyDecision = runtime.policyEngine.evaluate(binding);
|
|
63
|
+
if (!policyDecision.allowed) {
|
|
64
|
+
throw new Error(`Policy evaluation blocked agent ${selectedAgentId}: ${policyDecision.reasons.join(", ")}`);
|
|
65
|
+
}
|
|
66
|
+
const priority = normalizeRunPriority(options.priority);
|
|
67
|
+
const runRequest = buildPersistedRunRequest(options.input, invocation, priority);
|
|
68
|
+
const { threadId, runId, isNewThread } = await ensureThreadStarted(runtime, {
|
|
69
|
+
selectedAgentId,
|
|
70
|
+
binding,
|
|
71
|
+
message: options.input,
|
|
72
|
+
runRequest,
|
|
73
|
+
existingThreadId: options.threadId,
|
|
74
|
+
});
|
|
75
|
+
return {
|
|
76
|
+
binding,
|
|
77
|
+
selectedAgentId,
|
|
78
|
+
priority,
|
|
79
|
+
threadId,
|
|
80
|
+
runId,
|
|
81
|
+
isNewThread,
|
|
82
|
+
runCreatedEventPromise: runtime.emitRunCreated(threadId, runId, runCreatedPayload(binding, selectedAgentId)),
|
|
83
|
+
releaseRunSlotPromise: runtime.acquireRunSlot(threadId, runId, "running", priority),
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
export function defaultRequestedAgentId(agentId) {
|
|
87
|
+
return agentId ?? AUTO_AGENT_ID;
|
|
88
|
+
}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import type { RunResult, ThreadSummary } from "../../../contracts/types.js";
|
|
2
2
|
import type { RuntimePersistence } from "../../../persistence/types.js";
|
|
3
3
|
import type { ConcurrencyConfig, RecoveryConfig } from "../../../workspace/support/workspace-ref-utils.js";
|
|
4
|
+
import { type StartupRecoveryContext } from "./recovery.js";
|
|
4
5
|
type Startable = {
|
|
5
6
|
start(): Promise<void>;
|
|
6
7
|
};
|
|
@@ -11,7 +12,7 @@ export declare function initializeHarnessRuntime(input: {
|
|
|
11
12
|
export declare function recoverStartupRuns(input: {
|
|
12
13
|
recoveryConfig: RecoveryConfig;
|
|
13
14
|
persistence: RuntimePersistence;
|
|
14
|
-
createStartupRecoveryContext: () =>
|
|
15
|
+
createStartupRecoveryContext: () => StartupRecoveryContext;
|
|
15
16
|
reclaimExpiredClaimedRuns: (nowIso?: string) => Promise<void>;
|
|
16
17
|
}): Promise<void>;
|
|
17
18
|
export declare function reclaimExpiredClaimedRuns(input: {
|
|
@@ -1,13 +1,48 @@
|
|
|
1
|
+
import { recoverQueuedStartupRun, recoverResumingStartupRun, recoverRunningStartupRun, } from "./recovery.js";
|
|
1
2
|
export async function initializeHarnessRuntime(input) {
|
|
2
3
|
await input.persistence.initialize();
|
|
3
4
|
await input.healthMonitor?.start();
|
|
4
5
|
}
|
|
5
6
|
export async function recoverStartupRuns(input) {
|
|
6
|
-
|
|
7
|
+
if (!input.recoveryConfig.enabled) {
|
|
8
|
+
return;
|
|
9
|
+
}
|
|
10
|
+
await input.reclaimExpiredClaimedRuns();
|
|
11
|
+
const threads = await input.persistence.listSessions();
|
|
12
|
+
if (threads.length === 0) {
|
|
13
|
+
return;
|
|
14
|
+
}
|
|
15
|
+
const context = input.createStartupRecoveryContext();
|
|
16
|
+
for (const thread of threads) {
|
|
17
|
+
if (await recoverQueuedStartupRun(context, thread)) {
|
|
18
|
+
continue;
|
|
19
|
+
}
|
|
20
|
+
if (await recoverRunningStartupRun(context, thread)) {
|
|
21
|
+
continue;
|
|
22
|
+
}
|
|
23
|
+
await recoverResumingStartupRun(context, thread);
|
|
24
|
+
}
|
|
7
25
|
}
|
|
8
26
|
export async function reclaimExpiredClaimedRuns(input, nowIso = new Date().toISOString()) {
|
|
9
|
-
|
|
10
|
-
|
|
27
|
+
const expiredRuns = await input.persistence.listExpiredClaimedRuns(nowIso);
|
|
28
|
+
if (expiredRuns.length === 0) {
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
const activeRunCount = input.getActiveRunSlots();
|
|
32
|
+
const maxConcurrentRuns = input.concurrencyConfig.maxConcurrentRuns;
|
|
33
|
+
for (const [index, claimedRun] of expiredRuns.entries()) {
|
|
34
|
+
await input.persistence.releaseRunClaim(claimedRun.runId);
|
|
35
|
+
await input.setRunStateAndEmit(claimedRun.threadId, claimedRun.runId, 100, "queued", {
|
|
36
|
+
previousState: "claimed",
|
|
37
|
+
});
|
|
38
|
+
await input.emit(claimedRun.threadId, claimedRun.runId, 101, "run.queued", {
|
|
39
|
+
queuePosition: activeRunCount + index + 1,
|
|
40
|
+
activeRunCount,
|
|
41
|
+
...(maxConcurrentRuns !== undefined ? { maxConcurrentRuns } : {}),
|
|
42
|
+
priority: claimedRun.priority,
|
|
43
|
+
recoveryKind: "startup-claim-recovery",
|
|
44
|
+
});
|
|
45
|
+
}
|
|
11
46
|
}
|
|
12
47
|
export async function isStaleRunningRun(input, thread, nowMs = Date.now()) {
|
|
13
48
|
const control = await input.persistence.getRunControl(thread.latestRunId);
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import type { CompiledAgentBinding, HarnessEvent, MessageContent, RunResult, TranscriptMessage } from "../../../contracts/types.js";
|
|
2
|
+
import type { RuntimePersistence } from "../../../persistence/types.js";
|
|
3
|
+
import type { AgentRuntimeAdapter } from "../../agent-runtime-adapter.js";
|
|
4
|
+
export declare function createStreamRunRuntime(input: {
|
|
5
|
+
persistence: RuntimePersistence;
|
|
6
|
+
runtimeAdapter: AgentRuntimeAdapter;
|
|
7
|
+
emit: (threadId: string, runId: string, sequence: number, eventType: string, payload: Record<string, unknown>) => Promise<HarnessEvent>;
|
|
8
|
+
setRunStateAndEmit: (threadId: string, runId: string, sequence: number, state: RunResult["state"], options: {
|
|
9
|
+
previousState: string | null;
|
|
10
|
+
checkpointRef?: string | null;
|
|
11
|
+
error?: string;
|
|
12
|
+
}) => Promise<HarnessEvent>;
|
|
13
|
+
requestApprovalAndEmit: (threadId: string, runId: string, input: MessageContent, interruptContent: string | undefined, checkpointRef: string, sequence: number) => Promise<{
|
|
14
|
+
approval: {
|
|
15
|
+
approvalId: string;
|
|
16
|
+
pendingActionId: string;
|
|
17
|
+
};
|
|
18
|
+
event: HarnessEvent;
|
|
19
|
+
}>;
|
|
20
|
+
loadPriorHistory: (threadId: string, runId: string) => Promise<TranscriptMessage[]>;
|
|
21
|
+
invokeWithHistory: (binding: CompiledAgentBinding, input: MessageContent, threadId: string, runId: string) => Promise<RunResult>;
|
|
22
|
+
emitSyntheticFallback: (threadId: string, runId: string, selectedAgentId: string, error: unknown) => Promise<void>;
|
|
23
|
+
}): {
|
|
24
|
+
loadPriorHistory: (threadId: string, runId: string) => Promise<TranscriptMessage[]>;
|
|
25
|
+
stream: (binding: CompiledAgentBinding, message: MessageContent, threadId: string, priorHistory: TranscriptMessage[], options: {
|
|
26
|
+
context?: Record<string, unknown>;
|
|
27
|
+
state?: Record<string, unknown>;
|
|
28
|
+
files?: Record<string, unknown>;
|
|
29
|
+
runId: string;
|
|
30
|
+
}) => AsyncGenerator<string | import("../../parsing/stream-event-parsing.js").RuntimeStreamChunk, any, any>;
|
|
31
|
+
invokeWithHistory: (binding: CompiledAgentBinding, message: MessageContent, threadId: string, runId: string) => Promise<RunResult>;
|
|
32
|
+
emit: (threadId: string, runId: string, sequence: number, eventType: string, payload: Record<string, unknown>) => Promise<HarnessEvent>;
|
|
33
|
+
setRunStateAndEmit: (threadId: string, runId: string, sequence: number, state: RunResult["state"], options: {
|
|
34
|
+
previousState: string | null;
|
|
35
|
+
checkpointRef?: string | null;
|
|
36
|
+
error?: string;
|
|
37
|
+
}) => Promise<HarnessEvent>;
|
|
38
|
+
requestApprovalAndEmit: (threadId: string, runId: string, message: MessageContent, interruptContent: string | undefined, checkpointRef: string, sequence: number) => Promise<{
|
|
39
|
+
approval: {
|
|
40
|
+
approvalId: string;
|
|
41
|
+
pendingActionId: string;
|
|
42
|
+
};
|
|
43
|
+
event: HarnessEvent;
|
|
44
|
+
}>;
|
|
45
|
+
appendAssistantMessage: (threadId: string, runId: string, content?: string) => Promise<void>;
|
|
46
|
+
clearRunRequest: (threadId: string, runId: string) => Promise<void>;
|
|
47
|
+
emitSyntheticFallback: (threadId: string, runId: string, selectedAgentId: string, error: unknown) => Promise<void>;
|
|
48
|
+
};
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { appendAssistantMessage as appendLifecycleAssistantMessage } from "./run-lifecycle.js";
|
|
2
|
+
export function createStreamRunRuntime(input) {
|
|
3
|
+
return {
|
|
4
|
+
loadPriorHistory: (threadId, runId) => input.loadPriorHistory(threadId, runId),
|
|
5
|
+
stream: (binding, message, threadId, priorHistory, options) => input.runtimeAdapter.stream(binding, message, threadId, priorHistory, options),
|
|
6
|
+
invokeWithHistory: (binding, message, threadId, runId) => input.invokeWithHistory(binding, message, threadId, runId),
|
|
7
|
+
emit: (threadId, runId, sequence, eventType, payload) => input.emit(threadId, runId, sequence, eventType, payload),
|
|
8
|
+
setRunStateAndEmit: (threadId, runId, sequence, state, options) => input.setRunStateAndEmit(threadId, runId, sequence, state, options),
|
|
9
|
+
requestApprovalAndEmit: (threadId, runId, message, interruptContent, checkpointRef, sequence) => input.requestApprovalAndEmit(threadId, runId, message, interruptContent, checkpointRef, sequence),
|
|
10
|
+
appendAssistantMessage: (threadId, runId, content) => appendLifecycleAssistantMessage(input.persistence, threadId, runId, content),
|
|
11
|
+
clearRunRequest: (threadId, runId) => input.persistence.clearRunRequest(threadId, runId),
|
|
12
|
+
emitSyntheticFallback: (threadId, runId, selectedAgentId, error) => input.emitSyntheticFallback(threadId, runId, selectedAgentId, error),
|
|
13
|
+
};
|
|
14
|
+
}
|
|
@@ -33,7 +33,10 @@ export declare class AgentHarnessRuntime {
|
|
|
33
33
|
private pendingRunInsertionOrder;
|
|
34
34
|
private readonly pendingRunSlots;
|
|
35
35
|
private runtimeEventSequence;
|
|
36
|
+
private initialized;
|
|
37
|
+
private closed;
|
|
36
38
|
private readonly backgroundEventRuntime;
|
|
39
|
+
private readonly runtimeEventOperations;
|
|
37
40
|
private defaultRunRoot;
|
|
38
41
|
private getDefaultHostAgentId;
|
|
39
42
|
private resolveSelectedAgentId;
|
|
@@ -75,7 +78,7 @@ export declare class AgentHarnessRuntime {
|
|
|
75
78
|
}): Promise<string>;
|
|
76
79
|
private emit;
|
|
77
80
|
private trackBackgroundTask;
|
|
78
|
-
private
|
|
81
|
+
private resolveToolMcpServerTools;
|
|
79
82
|
private loadPriorHistory;
|
|
80
83
|
private loadRunInput;
|
|
81
84
|
private getRunCancellation;
|
|
@@ -89,10 +92,8 @@ export declare class AgentHarnessRuntime {
|
|
|
89
92
|
private requestApprovalAndEmit;
|
|
90
93
|
private isDecisionRun;
|
|
91
94
|
private notifyListener;
|
|
92
|
-
private prepareRunStart;
|
|
93
95
|
private acquireRunSlot;
|
|
94
96
|
private dropPendingRunSlot;
|
|
95
|
-
private dispatchRunListeners;
|
|
96
97
|
run(options: RunOptions): Promise<RunResult>;
|
|
97
98
|
streamEvents(options: RunStartOptions): AsyncGenerator<HarnessStreamItem>;
|
|
98
99
|
resume(options: ResumeOptions): Promise<RunResult>;
|