@agen-ai/agent-orchestration 0.0.0-stage → 0.4.0
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/LICENSE +21 -0
- package/README.md +413 -2
- package/dist/catalog/parse.d.ts +3 -0
- package/dist/catalog/parse.js +13 -0
- package/dist/catalog/schemas.d.ts +8 -0
- package/dist/catalog/schemas.js +78 -0
- package/dist/catalog/types.d.ts +53 -0
- package/dist/catalog/types.js +0 -0
- package/dist/coordinator/admission.d.ts +7 -0
- package/dist/coordinator/admission.js +189 -0
- package/dist/coordinator/cancel.d.ts +7 -0
- package/dist/coordinator/cancel.js +111 -0
- package/dist/coordinator/create.d.ts +3 -0
- package/dist/coordinator/create.js +87 -0
- package/dist/coordinator/parse.d.ts +10 -0
- package/dist/coordinator/parse.js +60 -0
- package/dist/coordinator/read.d.ts +4 -0
- package/dist/coordinator/read.js +267 -0
- package/dist/coordinator/recovery.d.ts +4 -0
- package/dist/coordinator/recovery.js +24 -0
- package/dist/coordinator/shutdown.d.ts +7 -0
- package/dist/coordinator/shutdown.js +66 -0
- package/dist/coordinator/types.d.ts +154 -0
- package/dist/coordinator/types.js +0 -0
- package/dist/delivery/calls.d.ts +4 -0
- package/dist/delivery/calls.js +34 -0
- package/dist/delivery/journal.d.ts +7 -0
- package/dist/delivery/journal.js +57 -0
- package/dist/delivery/parse.d.ts +4 -0
- package/dist/delivery/parse.js +31 -0
- package/dist/delivery/recovery.d.ts +3 -0
- package/dist/delivery/recovery.js +18 -0
- package/dist/delivery/scheduler.d.ts +3 -0
- package/dist/delivery/scheduler.js +123 -0
- package/dist/delivery/setup.d.ts +7 -0
- package/dist/delivery/setup.js +37 -0
- package/dist/delivery/types.d.ts +36 -0
- package/dist/delivery/types.js +0 -0
- package/dist/execution/authority.d.ts +16 -0
- package/dist/execution/authority.js +86 -0
- package/dist/execution/cleanup.d.ts +3 -0
- package/dist/execution/cleanup.js +75 -0
- package/dist/execution/control.d.ts +6 -0
- package/dist/execution/control.js +231 -0
- package/dist/execution/finish.d.ts +6 -0
- package/dist/execution/finish.js +49 -0
- package/dist/execution/journal.d.ts +19 -0
- package/dist/execution/journal.js +28 -0
- package/dist/execution/prepare.d.ts +3 -0
- package/dist/execution/prepare.js +153 -0
- package/dist/execution/scheduler.d.ts +4 -0
- package/dist/execution/scheduler.js +136 -0
- package/dist/execution/state.d.ts +107 -0
- package/dist/execution/state.js +120 -0
- package/dist/execution/stream.d.ts +9 -0
- package/dist/execution/stream.js +108 -0
- package/dist/host/types.d.ts +95 -0
- package/dist/host/types.js +0 -0
- package/dist/index.d.ts +20 -0
- package/dist/index.js +58 -0
- package/dist/mcp/bridge.d.ts +4 -0
- package/dist/mcp/bridge.js +222 -0
- package/dist/mcp/http.d.ts +11 -0
- package/dist/mcp/http.js +74 -0
- package/dist/mcp/index.d.ts +3 -0
- package/dist/mcp/index.js +4 -0
- package/dist/mcp/limits.d.ts +10 -0
- package/dist/mcp/limits.js +30 -0
- package/dist/mcp/protocol.d.ts +50 -0
- package/dist/mcp/protocol.js +81 -0
- package/dist/mcp/types.d.ts +22 -0
- package/dist/mcp/types.js +0 -0
- package/dist/policy/matching.d.ts +4 -0
- package/dist/policy/matching.js +18 -0
- package/dist/policy/parse.d.ts +3 -0
- package/dist/policy/parse.js +13 -0
- package/dist/policy/resolve.d.ts +7 -0
- package/dist/policy/resolve.js +67 -0
- package/dist/policy/schemas.d.ts +5 -0
- package/dist/policy/schemas.js +59 -0
- package/dist/policy/types.d.ts +60 -0
- package/dist/policy/types.js +0 -0
- package/dist/shared/bounds.d.ts +6 -0
- package/dist/shared/bounds.js +88 -0
- package/dist/shared/errors.d.ts +9 -0
- package/dist/shared/errors.js +27 -0
- package/dist/shared/identity.d.ts +6 -0
- package/dist/shared/identity.js +32 -0
- package/dist/shared/immutable.d.ts +2 -0
- package/dist/shared/immutable.js +10 -0
- package/dist/shared/limits.d.ts +32 -0
- package/dist/shared/limits.js +69 -0
- package/dist/shared/operational-errors.d.ts +7 -0
- package/dist/shared/operational-errors.js +15 -0
- package/dist/shared/schemas.d.ts +11 -0
- package/dist/shared/schemas.js +23 -0
- package/dist/shared/text.d.ts +2 -0
- package/dist/shared/text.js +15 -0
- package/dist/store/memory.d.ts +7 -0
- package/dist/store/memory.js +215 -0
- package/dist/store/pages.d.ts +16 -0
- package/dist/store/pages.js +46 -0
- package/dist/store/parse.d.ts +5 -0
- package/dist/store/parse.js +137 -0
- package/dist/store/types.d.ts +219 -0
- package/dist/store/types.js +0 -0
- package/dist/store-adapter/admission.d.ts +6 -0
- package/dist/store-adapter/admission.js +90 -0
- package/dist/store-adapter/context.d.ts +20 -0
- package/dist/store-adapter/context.js +107 -0
- package/dist/store-adapter/delivery.d.ts +15 -0
- package/dist/store-adapter/delivery.js +81 -0
- package/dist/store-adapter/execution.d.ts +12 -0
- package/dist/store-adapter/execution.js +151 -0
- package/dist/store-adapter/index.d.ts +7 -0
- package/dist/store-adapter/index.js +27 -0
- package/dist/store-adapter/lifecycle.d.ts +15 -0
- package/dist/store-adapter/lifecycle.js +80 -0
- package/dist/store-adapter/plan.d.ts +7 -0
- package/dist/store-adapter/plan.js +168 -0
- package/dist/store-adapter/types.d.ts +45 -0
- package/dist/store-adapter/types.js +0 -0
- package/dist/tasks/failure.d.ts +4 -0
- package/dist/tasks/failure.js +24 -0
- package/dist/tasks/parse.d.ts +18 -0
- package/dist/tasks/parse.js +66 -0
- package/dist/tasks/schemas.d.ts +47 -0
- package/dist/tasks/schemas.js +215 -0
- package/dist/tasks/types.d.ts +192 -0
- package/dist/tasks/types.js +0 -0
- package/dist/tools/create.d.ts +4 -0
- package/dist/tools/create.js +69 -0
- package/dist/tools/errors.d.ts +4 -0
- package/dist/tools/errors.js +35 -0
- package/dist/tools/guidance.d.ts +4 -0
- package/dist/tools/guidance.js +28 -0
- package/dist/tools/handlers.d.ts +4 -0
- package/dist/tools/handlers.js +84 -0
- package/dist/tools/projection.d.ts +13 -0
- package/dist/tools/projection.js +72 -0
- package/dist/tools/schemas.d.ts +6 -0
- package/dist/tools/schemas.js +93 -0
- package/dist/tools/types.d.ts +28 -0
- package/dist/tools/types.js +0 -0
- package/package.json +64 -3
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import type { AgentProviderSession, AgentProviderSessionCleanupError, AgentProviderTurnExecution } from "@agen-ai/agent-runtime";
|
|
2
|
+
import type { AgentRequest, AgentSessionId, AgentTurnId } from "@agen-ai/agent-protocol";
|
|
3
|
+
import type { DelegationTarget } from "../catalog/types.js";
|
|
4
|
+
import type { CreateDelegationCoordinatorOptions } from "../coordinator/types.js";
|
|
5
|
+
import type { DelegationChildResources, DelegationContinuation, DelegationExecutionLease, DelegationHostRequestHandle } from "../host/types.js";
|
|
6
|
+
import type { DelegationCommitReceipt, DelegationMutationFence, DelegationStoreCommand } from "../store/types.js";
|
|
7
|
+
import type { DelegationOwnerToken, DelegationParentScope, DelegationPolicyCeiling, DelegationResultReference, DelegationRunOutcome, DelegationRunRecord, DelegationTaskRecord, DelegationTaskView } from "../tasks/types.js";
|
|
8
|
+
export interface ParentBinding {
|
|
9
|
+
readonly scope: DelegationParentScope;
|
|
10
|
+
readonly ancestor?: Readonly<{
|
|
11
|
+
taskId: string;
|
|
12
|
+
runId: string;
|
|
13
|
+
}>;
|
|
14
|
+
readonly ceiling?: DelegationPolicyCeiling;
|
|
15
|
+
readonly depth?: number;
|
|
16
|
+
}
|
|
17
|
+
export interface LiveRequest {
|
|
18
|
+
readonly handle: DelegationHostRequestHandle;
|
|
19
|
+
readonly live: LiveRun;
|
|
20
|
+
readonly request: AgentRequest;
|
|
21
|
+
}
|
|
22
|
+
export interface LiveRun {
|
|
23
|
+
readonly taskId: string;
|
|
24
|
+
readonly runId: string;
|
|
25
|
+
readonly scope: DelegationParentScope;
|
|
26
|
+
readonly target: DelegationTarget;
|
|
27
|
+
readonly ceiling: DelegationPolicyCeiling;
|
|
28
|
+
readonly lifetime: AbortController;
|
|
29
|
+
readonly controlAbort: AbortController;
|
|
30
|
+
readonly sessionId: AgentSessionId;
|
|
31
|
+
readonly turnId: AgentTurnId;
|
|
32
|
+
readonly controls: Set<Promise<unknown>>;
|
|
33
|
+
readonly pendingOperations: Map<string, Readonly<{
|
|
34
|
+
digest: string;
|
|
35
|
+
promise: Promise<unknown>;
|
|
36
|
+
}>>;
|
|
37
|
+
execution?: DelegationExecutionLease;
|
|
38
|
+
resources?: DelegationChildResources;
|
|
39
|
+
session?: AgentProviderSession;
|
|
40
|
+
turn?: AgentProviderTurnExecution;
|
|
41
|
+
rejectedOpening?: AgentProviderSessionCleanupError;
|
|
42
|
+
stream?: Promise<void>;
|
|
43
|
+
request?: LiveRequest;
|
|
44
|
+
requestResolution?: Promise<unknown>;
|
|
45
|
+
observedStart: boolean;
|
|
46
|
+
recordedStart: boolean;
|
|
47
|
+
controlTail: Promise<void>;
|
|
48
|
+
uncertain?: Extract<DelegationRunOutcome, {
|
|
49
|
+
kind: "recovery_required";
|
|
50
|
+
}>["reason"];
|
|
51
|
+
terminal?: DelegationRunOutcome;
|
|
52
|
+
summary: string;
|
|
53
|
+
truncated: boolean;
|
|
54
|
+
references: DelegationResultReference[];
|
|
55
|
+
runtimeCleanupAttempts: number;
|
|
56
|
+
resourceReleaseAttempts: number;
|
|
57
|
+
executionReleaseAttempts: number;
|
|
58
|
+
runtimeClosed: boolean;
|
|
59
|
+
resourcesReleased: boolean;
|
|
60
|
+
executionReleased: boolean;
|
|
61
|
+
cleanup?: Promise<boolean>;
|
|
62
|
+
cancellation?: Promise<unknown>;
|
|
63
|
+
finishing?: Promise<void>;
|
|
64
|
+
ready?: Promise<void>;
|
|
65
|
+
requestHistory?: DelegationRunRecord["pendingRequest"];
|
|
66
|
+
schedule?: () => void;
|
|
67
|
+
deadline?: ReturnType<typeof setTimeout>;
|
|
68
|
+
removeRevocation?: () => void;
|
|
69
|
+
}
|
|
70
|
+
export declare class CoordinatorState {
|
|
71
|
+
readonly options: CreateDelegationCoordinatorOptions;
|
|
72
|
+
readonly owner: DelegationOwnerToken;
|
|
73
|
+
readonly live: Map<string, LiveRun>;
|
|
74
|
+
readonly continuations: Map<string, DelegationContinuation>;
|
|
75
|
+
readonly requests: WeakMap<DelegationHostRequestHandle, LiveRequest>;
|
|
76
|
+
readonly waiters: Set<() => void>;
|
|
77
|
+
readonly producers: Set<Promise<unknown>>;
|
|
78
|
+
readonly deliveryCalls: Map<string, Readonly<{
|
|
79
|
+
controller: AbortController;
|
|
80
|
+
taskId: string;
|
|
81
|
+
promise: Promise<unknown>;
|
|
82
|
+
}>>;
|
|
83
|
+
closing: boolean;
|
|
84
|
+
draining: boolean;
|
|
85
|
+
failed: boolean;
|
|
86
|
+
released: boolean;
|
|
87
|
+
pump?: Promise<void>;
|
|
88
|
+
queueTimer?: ReturnType<typeof setTimeout>;
|
|
89
|
+
schedulePending: boolean;
|
|
90
|
+
onFailure?: () => void;
|
|
91
|
+
wakeDelivery?: () => void;
|
|
92
|
+
stopDelivery?: (abort: boolean) => void;
|
|
93
|
+
private tail;
|
|
94
|
+
constructor(options: CreateDelegationCoordinatorOptions, owner: DelegationOwnerToken);
|
|
95
|
+
gate<Value>(action: () => Promise<Value>): Promise<Value>;
|
|
96
|
+
changed(): void;
|
|
97
|
+
fail(): void;
|
|
98
|
+
track<Value>(promise: Promise<Value>, live?: LiveRun): Promise<Value>;
|
|
99
|
+
task(taskId: string): Promise<DelegationTaskView>;
|
|
100
|
+
run(taskId: string, runId: string): Promise<DelegationRunRecord>;
|
|
101
|
+
commit(command: DelegationStoreCommand): Promise<DelegationCommitReceipt>;
|
|
102
|
+
mutate(taskId: string, runId: string, operationId: string, inputDigest: string, command: (fence: DelegationMutationFence, task: DelegationTaskRecord, run: DelegationRunRecord) => DelegationStoreCommand | undefined): Promise<DelegationCommitReceipt | undefined>;
|
|
103
|
+
}
|
|
104
|
+
export declare function sameScopeIdentity(left: DelegationParentScope, right: DelegationParentScope): boolean;
|
|
105
|
+
export declare function now(): string;
|
|
106
|
+
export declare function truncateUtf8(text: string, maximum: number): string;
|
|
107
|
+
//# sourceMappingURL=state.d.ts.map
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
import { DelegationOperationError, failDelegation } from "../shared/operational-errors.js";
|
|
2
|
+
class CoordinatorState {
|
|
3
|
+
constructor(options, owner) {
|
|
4
|
+
this.options = options;
|
|
5
|
+
this.owner = owner;
|
|
6
|
+
}
|
|
7
|
+
options;
|
|
8
|
+
owner;
|
|
9
|
+
live = /* @__PURE__ */ new Map();
|
|
10
|
+
continuations = /* @__PURE__ */ new Map();
|
|
11
|
+
requests = /* @__PURE__ */ new WeakMap();
|
|
12
|
+
waiters = /* @__PURE__ */ new Set();
|
|
13
|
+
producers = /* @__PURE__ */ new Set();
|
|
14
|
+
deliveryCalls = /* @__PURE__ */ new Map();
|
|
15
|
+
closing = false;
|
|
16
|
+
draining = false;
|
|
17
|
+
failed = false;
|
|
18
|
+
released = false;
|
|
19
|
+
pump;
|
|
20
|
+
queueTimer;
|
|
21
|
+
schedulePending = false;
|
|
22
|
+
onFailure;
|
|
23
|
+
wakeDelivery;
|
|
24
|
+
stopDelivery;
|
|
25
|
+
tail = Promise.resolve();
|
|
26
|
+
gate(action) {
|
|
27
|
+
const next = this.tail.then(action, action);
|
|
28
|
+
this.tail = next.catch(() => void 0);
|
|
29
|
+
return next;
|
|
30
|
+
}
|
|
31
|
+
changed() {
|
|
32
|
+
for (const waiter of this.waiters) waiter();
|
|
33
|
+
this.waiters.clear();
|
|
34
|
+
}
|
|
35
|
+
fail() {
|
|
36
|
+
const first = !this.failed;
|
|
37
|
+
this.failed = true;
|
|
38
|
+
this.closing = true;
|
|
39
|
+
this.draining = false;
|
|
40
|
+
if (this.queueTimer) {
|
|
41
|
+
clearTimeout(this.queueTimer);
|
|
42
|
+
this.queueTimer = void 0;
|
|
43
|
+
}
|
|
44
|
+
this.stopDelivery?.(true);
|
|
45
|
+
if (first) this.onFailure?.();
|
|
46
|
+
this.changed();
|
|
47
|
+
}
|
|
48
|
+
track(promise, live) {
|
|
49
|
+
this.producers.add(promise);
|
|
50
|
+
live?.controls.add(promise);
|
|
51
|
+
void promise.finally(() => {
|
|
52
|
+
this.producers.delete(promise);
|
|
53
|
+
live?.controls.delete(promise);
|
|
54
|
+
this.changed();
|
|
55
|
+
}).catch(() => void 0);
|
|
56
|
+
return promise;
|
|
57
|
+
}
|
|
58
|
+
async task(taskId) {
|
|
59
|
+
const result = await this.options.store.read({ kind: "task", taskId, limit: this.options.limits.maxPageRecords });
|
|
60
|
+
if (result.kind !== "task" || !result.view) return failDelegation("request_unavailable", "The managed task is unavailable.");
|
|
61
|
+
return result.view;
|
|
62
|
+
}
|
|
63
|
+
async run(taskId, runId) {
|
|
64
|
+
const result = await this.options.store.read({ kind: "run", taskId, runId });
|
|
65
|
+
if (result.kind !== "run" || !result.run) return failDelegation("request_unavailable", "The managed run is unavailable.");
|
|
66
|
+
return result.run;
|
|
67
|
+
}
|
|
68
|
+
async commit(command) {
|
|
69
|
+
if (this.released) failDelegation("owner_lost", "The coordinator no longer owns this store.");
|
|
70
|
+
try {
|
|
71
|
+
const receipt = await this.options.store.commit(command);
|
|
72
|
+
this.changed();
|
|
73
|
+
if (command.kind === "settle" || command.kind === "cancel") this.wakeDelivery?.();
|
|
74
|
+
return receipt;
|
|
75
|
+
} catch (error) {
|
|
76
|
+
if (error instanceof DelegationOperationError && error.code !== "store_failed" && error.code !== "owner_lost") throw error;
|
|
77
|
+
this.fail();
|
|
78
|
+
if (error instanceof DelegationOperationError) throw error;
|
|
79
|
+
return failDelegation("store_failed", "The managed store could not commit this operation.");
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
mutate(taskId, runId, operationId, inputDigest, command) {
|
|
83
|
+
return this.gate(async () => {
|
|
84
|
+
const [{ task }, run] = await Promise.all([this.task(taskId), this.run(taskId, runId)]);
|
|
85
|
+
const mutation = command({
|
|
86
|
+
owner: this.owner,
|
|
87
|
+
operationId,
|
|
88
|
+
inputDigest,
|
|
89
|
+
taskId,
|
|
90
|
+
expectedTaskRevision: task.revision,
|
|
91
|
+
runId,
|
|
92
|
+
expectedRunRevision: run.revision
|
|
93
|
+
}, task, run);
|
|
94
|
+
return mutation === void 0 ? void 0 : this.commit(mutation);
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
function sameScopeIdentity(left, right) {
|
|
99
|
+
return left.parentId === right.parentId && left.scopeId === right.scopeId;
|
|
100
|
+
}
|
|
101
|
+
function now() {
|
|
102
|
+
return (/* @__PURE__ */ new Date()).toISOString();
|
|
103
|
+
}
|
|
104
|
+
function truncateUtf8(text, maximum) {
|
|
105
|
+
const encoder = new TextEncoder();
|
|
106
|
+
let bytes = 0;
|
|
107
|
+
let result = "";
|
|
108
|
+
for (const character of text) {
|
|
109
|
+
bytes += encoder.encode(character).byteLength;
|
|
110
|
+
if (bytes > maximum) break;
|
|
111
|
+
result += character;
|
|
112
|
+
}
|
|
113
|
+
return result;
|
|
114
|
+
}
|
|
115
|
+
export {
|
|
116
|
+
CoordinatorState,
|
|
117
|
+
now,
|
|
118
|
+
sameScopeIdentity,
|
|
119
|
+
truncateUtf8
|
|
120
|
+
};
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { AgentProviderOutput } from "@agen-ai/agent-runtime";
|
|
2
|
+
import type { DelegationHistoricalRequest } from "../tasks/types.js";
|
|
3
|
+
import { type CoordinatorState, type LiveRun } from "./state.js";
|
|
4
|
+
export declare function markRequest(state: CoordinatorState, live: LiveRun, stateChange: "opened" | "resolution_intended" | "resolved" | "unavailable", request: DelegationHistoricalRequest, identity?: {
|
|
5
|
+
operationId: string;
|
|
6
|
+
inputDigest: string;
|
|
7
|
+
}): Promise<void>;
|
|
8
|
+
export declare function consumeTurn(state: CoordinatorState, live: LiveRun, outputs: AsyncIterable<AgentProviderOutput>): Promise<void>;
|
|
9
|
+
//# sourceMappingURL=stream.d.ts.map
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import { delegationDataBytes, delegationInputDigest, delegationSubordinateId } from "../shared/identity.js";
|
|
2
|
+
import { fitDelegationText } from "../shared/text.js";
|
|
3
|
+
import { projectDelegationFailure } from "../tasks/failure.js";
|
|
4
|
+
import { parseDelegationResultReference } from "../tasks/parse.js";
|
|
5
|
+
import { finishRun } from "./finish.js";
|
|
6
|
+
import { operationIdentity, operationReceipt } from "./journal.js";
|
|
7
|
+
import { now } from "./state.js";
|
|
8
|
+
function accumulate(live, text) {
|
|
9
|
+
const candidate = live.summary + text;
|
|
10
|
+
const bounded = fitDelegationText(candidate, (value) => delegationDataBytes({ text: value, truncated: false, references: live.references }) <= 8192);
|
|
11
|
+
if (bounded !== candidate) live.truncated = true;
|
|
12
|
+
live.summary = bounded;
|
|
13
|
+
}
|
|
14
|
+
async function observe(state, live, projection) {
|
|
15
|
+
const id = delegationSubordinateId(live.runId, "observations");
|
|
16
|
+
await state.mutate(live.taskId, live.runId, id, delegationInputDigest({ runId: live.runId, kind: "observe" }), (fence, _task, run) => {
|
|
17
|
+
const base = { storeId: state.options.store.storeId, taskId: live.taskId, runId: live.runId, index: run.observationIndex + 1, at: now(), ...projection };
|
|
18
|
+
const text = base.text === void 0 ? void 0 : fitDelegationText(base.text, (value) => delegationDataBytes({ ...base, text: value }) <= state.options.limits.maxObservationBytes);
|
|
19
|
+
return { ...fence, kind: "observe", observation: { ...base, ...text === void 0 ? {} : { text } } };
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
async function markRequest(state, live, stateChange, request, identity = operationIdentity(live, `request:${request.requestId}:${stateChange}`)) {
|
|
23
|
+
await state.mutate(
|
|
24
|
+
live.taskId,
|
|
25
|
+
live.runId,
|
|
26
|
+
identity.operationId,
|
|
27
|
+
identity.inputDigest,
|
|
28
|
+
(fence) => ({ ...fence, kind: "request_state", state: stateChange, request, at: now() })
|
|
29
|
+
);
|
|
30
|
+
}
|
|
31
|
+
async function routeRequest(state, live, request) {
|
|
32
|
+
const historical = {
|
|
33
|
+
requestId: request.requestId,
|
|
34
|
+
requestKind: request.requestKind,
|
|
35
|
+
openedAt: now(),
|
|
36
|
+
...request.expiresAt ? { expiresAt: request.expiresAt } : {},
|
|
37
|
+
state: "waiting"
|
|
38
|
+
};
|
|
39
|
+
await markRequest(state, live, "opened", historical);
|
|
40
|
+
live.requestHistory = historical;
|
|
41
|
+
const handle = Object.freeze({});
|
|
42
|
+
const pending = { handle, live, request };
|
|
43
|
+
live.request = pending;
|
|
44
|
+
state.requests.set(handle, pending);
|
|
45
|
+
await observe(state, live, { kind: "request", request: historical });
|
|
46
|
+
const callback = Promise.resolve().then(async () => {
|
|
47
|
+
const [{ task }, run] = await Promise.all([state.task(live.taskId), state.run(live.taskId, live.runId)]);
|
|
48
|
+
if (live.lifetime.signal.aborted || live.request !== pending) return;
|
|
49
|
+
await state.options.host.onRequest({ handle, task, run, request, signal: live.lifetime.signal });
|
|
50
|
+
});
|
|
51
|
+
state.track(callback, live);
|
|
52
|
+
void callback.catch(() => {
|
|
53
|
+
live.uncertain = "request_executor_lost";
|
|
54
|
+
void finishRun(state, live, { kind: "recovery_required", reason: "request_executor_lost" }).catch(() => void 0);
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
async function reduceOutput(state, live, output) {
|
|
58
|
+
if (live.observedStart && !live.recordedStart) {
|
|
59
|
+
await operationReceipt(state, live, "delegated", operationIdentity(live, "turn"));
|
|
60
|
+
live.recordedStart = true;
|
|
61
|
+
}
|
|
62
|
+
if (output.kind === "artifact" && state.options.host.publishArtifact && live.references.length < 8) {
|
|
63
|
+
const [{ task }, run] = await Promise.all([state.task(live.taskId), state.run(live.taskId, live.runId)]);
|
|
64
|
+
const reference = parseDelegationResultReference(await state.options.host.publishArtifact({ task, run, candidate: output.candidate, signal: live.lifetime.signal }));
|
|
65
|
+
if (delegationDataBytes(reference) <= 256 && delegationDataBytes({ text: live.summary, truncated: live.truncated, references: [...live.references, reference] }) <= 8192) live.references.push(reference);
|
|
66
|
+
else live.truncated = true;
|
|
67
|
+
}
|
|
68
|
+
if (output.kind !== "event") return {};
|
|
69
|
+
const event = output.event;
|
|
70
|
+
if (event.type === "content.delta" && event.payload.streamKind === "assistant_text") {
|
|
71
|
+
accumulate(live, event.payload.delta);
|
|
72
|
+
await observe(state, live, { kind: "text", text: event.payload.delta });
|
|
73
|
+
} else if (event.type === "turn.started") await observe(state, live, { kind: "state", phase: "running" });
|
|
74
|
+
else if (event.type === "request.opened") return { request: event.payload.request };
|
|
75
|
+
else if (event.type === "turn.completed") {
|
|
76
|
+
return { terminal: event.payload.outcome === "completed" ? { kind: "succeeded", result: { text: live.summary, truncated: live.truncated, references: [...live.references] } } : event.payload.outcome === "canceled" ? { kind: "canceled", confirmation: "provider_terminal" } : { kind: "failed", error: projectDelegationFailure(event.payload.error) } };
|
|
77
|
+
}
|
|
78
|
+
return {};
|
|
79
|
+
}
|
|
80
|
+
async function consumeTurn(state, live, outputs) {
|
|
81
|
+
let terminal;
|
|
82
|
+
try {
|
|
83
|
+
for await (const output of outputs) {
|
|
84
|
+
const projected = await reduceOutput(state, live, output);
|
|
85
|
+
if (projected.request || projected.terminal) await live.requestResolution?.catch(() => void 0);
|
|
86
|
+
if (projected.request && !live.uncertain && !live.lifetime.signal.aborted) await routeRequest(state, live, projected.request);
|
|
87
|
+
terminal = projected.terminal ?? terminal;
|
|
88
|
+
}
|
|
89
|
+
live.stream = void 0;
|
|
90
|
+
if (!terminal) terminal = { kind: "recovery_required", reason: live.cancellation ? "cancellation_uncertain" : live.requestHistory ? "request_executor_lost" : "dispatch_uncertain" };
|
|
91
|
+
} catch {
|
|
92
|
+
live.stream = void 0;
|
|
93
|
+
terminal = live.observedStart ? { kind: "recovery_required", reason: live.uncertain ?? (live.cancellation ? "cancellation_uncertain" : "dispatch_uncertain") } : { kind: "failed", error: { category: "unknown", code: "turn_rejected", message: "The managed child turn was rejected before provider execution.", recovery: { kind: "unknown" } } };
|
|
94
|
+
if (!live.observedStart) {
|
|
95
|
+
try {
|
|
96
|
+
await operationReceipt(state, live, "rejected", operationIdentity(live, "turn"));
|
|
97
|
+
} catch {
|
|
98
|
+
live.uncertain = "storage_failed";
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
live.terminal = terminal;
|
|
103
|
+
await finishRun(state, live, terminal);
|
|
104
|
+
}
|
|
105
|
+
export {
|
|
106
|
+
consumeTurn,
|
|
107
|
+
markRequest
|
|
108
|
+
};
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import type { AgentArtifactCandidate, AgentProviderRegistry, AgentProviderSession, AgentSessionHostBinding, MaybePromise } from "@agen-ai/agent-runtime";
|
|
2
|
+
import type { AgentConfigurationCatalog, AgentRequest, AgentSessionBinding, AgentSessionConfiguration } from "@agen-ai/agent-protocol";
|
|
3
|
+
import type { DelegationTarget, DelegationTargetCatalog, DelegationTargetReference } from "../catalog/types.js";
|
|
4
|
+
import type { DelegationPolicySnapshot } from "../policy/types.js";
|
|
5
|
+
import type { DelegationParentScope, DelegationResultReference, DelegationRunRecord, DelegationTaskRecord } from "../tasks/types.js";
|
|
6
|
+
export interface DelegationExecutionLease {
|
|
7
|
+
readonly signal: AbortSignal;
|
|
8
|
+
readonly assertCurrent: () => void;
|
|
9
|
+
readonly release: () => MaybePromise<void>;
|
|
10
|
+
}
|
|
11
|
+
export interface DelegationHostPolicy {
|
|
12
|
+
readonly scope: DelegationParentScope;
|
|
13
|
+
readonly catalog: DelegationTargetCatalog;
|
|
14
|
+
readonly policy: DelegationPolicySnapshot;
|
|
15
|
+
}
|
|
16
|
+
export interface DelegationContinuation {
|
|
17
|
+
readonly runId: string;
|
|
18
|
+
readonly binding: AgentSessionBinding;
|
|
19
|
+
readonly target: DelegationTarget;
|
|
20
|
+
}
|
|
21
|
+
export interface DelegationChildResources {
|
|
22
|
+
readonly workingDirectory: Readonly<{
|
|
23
|
+
policy: "shared" | "isolated";
|
|
24
|
+
authorizedRef: string;
|
|
25
|
+
path: string;
|
|
26
|
+
}>;
|
|
27
|
+
readonly hostBinding?: AgentSessionHostBinding;
|
|
28
|
+
readonly continuation?: Readonly<{
|
|
29
|
+
kind: "resume";
|
|
30
|
+
allowTargetChange: boolean;
|
|
31
|
+
}>;
|
|
32
|
+
/** Owns unclaimed binding cleanup; shares once-success release state with hostBinding.release. */
|
|
33
|
+
readonly release: () => MaybePromise<void>;
|
|
34
|
+
}
|
|
35
|
+
export interface DelegationAppliedConfiguration {
|
|
36
|
+
readonly target: DelegationTargetReference;
|
|
37
|
+
readonly configuration: AgentSessionConfiguration;
|
|
38
|
+
}
|
|
39
|
+
declare const requestHandleBrand: unique symbol;
|
|
40
|
+
export interface DelegationHostRequestHandle {
|
|
41
|
+
readonly [requestHandleBrand]: true;
|
|
42
|
+
}
|
|
43
|
+
export interface DelegationHostRequest {
|
|
44
|
+
readonly handle: DelegationHostRequestHandle;
|
|
45
|
+
readonly task: DelegationTaskRecord;
|
|
46
|
+
readonly run: DelegationRunRecord;
|
|
47
|
+
readonly request: AgentRequest;
|
|
48
|
+
}
|
|
49
|
+
export interface DelegationHost {
|
|
50
|
+
readonly registry: AgentProviderRegistry;
|
|
51
|
+
readonly currentPolicy: (scope: DelegationParentScope) => DelegationHostPolicy;
|
|
52
|
+
readonly assertResultAccess: (input: Readonly<{
|
|
53
|
+
scope: DelegationParentScope;
|
|
54
|
+
task?: DelegationTaskRecord;
|
|
55
|
+
}>) => void;
|
|
56
|
+
readonly assertControl: (input: Readonly<{
|
|
57
|
+
scope: DelegationParentScope;
|
|
58
|
+
task: DelegationTaskRecord;
|
|
59
|
+
}>) => void;
|
|
60
|
+
readonly assertRequestAccess: (input: DelegationHostRequest) => void;
|
|
61
|
+
readonly acquireExecution: (input: Readonly<{
|
|
62
|
+
scope: DelegationParentScope;
|
|
63
|
+
task: DelegationTaskRecord;
|
|
64
|
+
run: DelegationRunRecord;
|
|
65
|
+
target: DelegationTarget;
|
|
66
|
+
signal: AbortSignal;
|
|
67
|
+
}>) => MaybePromise<DelegationExecutionLease>;
|
|
68
|
+
readonly acquireChildResources: (input: Readonly<{
|
|
69
|
+
scope: DelegationParentScope;
|
|
70
|
+
task: DelegationTaskRecord;
|
|
71
|
+
run: DelegationRunRecord;
|
|
72
|
+
target: DelegationTarget;
|
|
73
|
+
continuation?: DelegationContinuation;
|
|
74
|
+
signal: AbortSignal;
|
|
75
|
+
}>) => MaybePromise<DelegationChildResources>;
|
|
76
|
+
readonly verifyAppliedConfiguration: (input: Readonly<{
|
|
77
|
+
task: DelegationTaskRecord;
|
|
78
|
+
run: DelegationRunRecord;
|
|
79
|
+
target: DelegationTarget;
|
|
80
|
+
session: AgentProviderSession;
|
|
81
|
+
observedConfiguration?: AgentConfigurationCatalog;
|
|
82
|
+
signal: AbortSignal;
|
|
83
|
+
}>) => MaybePromise<DelegationAppliedConfiguration>;
|
|
84
|
+
readonly onRequest: (input: DelegationHostRequest & Readonly<{
|
|
85
|
+
signal: AbortSignal;
|
|
86
|
+
}>) => MaybePromise<void>;
|
|
87
|
+
readonly publishArtifact?: (input: Readonly<{
|
|
88
|
+
task: DelegationTaskRecord;
|
|
89
|
+
run: DelegationRunRecord;
|
|
90
|
+
candidate: AgentArtifactCandidate;
|
|
91
|
+
signal: AbortSignal;
|
|
92
|
+
}>) => MaybePromise<DelegationResultReference>;
|
|
93
|
+
}
|
|
94
|
+
export {};
|
|
95
|
+
//# sourceMappingURL=types.d.ts.map
|
|
File without changes
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
export type { DelegationExecutionMode, DelegationReasoning, DelegationSemanticFact, DelegationTarget, DelegationTargetCatalog, DelegationTargetOption, DelegationTargetReference, } from "./catalog/types.js";
|
|
2
|
+
export { parseDelegationTargetCatalog } from "./catalog/parse.js";
|
|
3
|
+
export type { DelegationDeveloperPolicy, DelegationExclusion, DelegationPolicyOutcome, DelegationPolicySnapshot, DelegationTargetDecision, DelegationTargetIdentity, DelegationUserPolicy, DelegationUserSelection, ResolvedDelegationPolicy, } from "./policy/types.js";
|
|
4
|
+
export { parseDelegationPolicySnapshot } from "./policy/parse.js";
|
|
5
|
+
export { resolveDelegationPolicy } from "./policy/resolve.js";
|
|
6
|
+
export { DelegationValidationError, type DelegationValidationIssue } from "./shared/errors.js";
|
|
7
|
+
export { DelegationOperationError, type DelegationErrorCode, } from "./shared/operational-errors.js";
|
|
8
|
+
export { DEFAULT_DELEGATION_LIMITS, parseDelegationLimits, type DelegationLimits, } from "./shared/limits.js";
|
|
9
|
+
export type { CreateDelegationCoordinatorOptions, DelegationAdmissionReceipt, DelegationBindingSnapshot, DelegationCancelReceipt, DelegationClient, DelegationCoordinator, DelegationRecoveryPage, DelegationRequestReceipt, DelegationResolveHostRequestInput, DelegationSendReceipt, DelegationSendTaskInput, DelegationShutdownReceipt, DelegationSpawnTaskInput, DelegationTargetPage, DelegationWaitReceipt, } from "./coordinator/types.js";
|
|
10
|
+
export { createDelegationCoordinator } from "./coordinator/create.js";
|
|
11
|
+
export type { DelegationInboxAcceptance, DelegationInboxInspection, DelegationParentDeliveryPort, } from "./delivery/types.js";
|
|
12
|
+
export { parseDelegationInboxAcceptance, parseDelegationInboxInspection, } from "./delivery/parse.js";
|
|
13
|
+
export type { DelegationAppliedConfiguration, DelegationChildResources, DelegationContinuation, DelegationExecutionLease, DelegationHost, DelegationHostPolicy, DelegationHostRequest, DelegationHostRequestHandle, } from "./host/types.js";
|
|
14
|
+
export type { DelegationCommitReceipt, DelegationCriticalSlot, DelegationDeliveryResult, DelegationMutationFence, DelegationOperationDisposition, DelegationOperationKind, DelegationOperationRecord, DelegationRunAdmission, DelegationStore, DelegationStoreCapacity, DelegationStoreCommand, DelegationStoreReadInput, DelegationStoreReadPage, DelegationStoreScanInput, DelegationStoreScanPage, } from "./store/types.js";
|
|
15
|
+
export { createInMemoryDelegationStore } from "./store/memory.js";
|
|
16
|
+
export type { DelegationCompletionEnvelope, DelegationCompletionPolicy, DelegationDeliveryCommitRecord, DelegationHistoricalRequest, DelegationNotificationRecord, DelegationObservationGap, DelegationObservedTaskObservation, DelegationOwnerToken, DelegationPage, DelegationParentScope, DelegationPersistence, DelegationPolicyCeiling, DelegationPublicFailure, DelegationResultReference, DelegationResultSummary, DelegationRunOutcome, DelegationRunPhase, DelegationRunRecord, DelegationRunSummary, DelegationTaskObservation, DelegationTaskRecord, DelegationTaskState, DelegationTaskView, } from "./tasks/types.js";
|
|
17
|
+
export { createDelegationPolicyCeiling, parseDelegationCompletionEnvelope, parseDelegationHistoricalRequest, parseDelegationNotificationRecord, parseDelegationOwnerToken, parseDelegationParentScope, parseDelegationPolicyCeiling, parseDelegationResultReference, parseDelegationRunOutcome, parseDelegationRunRecord, parseDelegationTaskObservation, parseDelegationTaskRecord, } from "./tasks/parse.js";
|
|
18
|
+
export { createDelegationHostBinding } from "./tools/create.js";
|
|
19
|
+
export type { CreateDelegationHostBindingOptions, DelegationToolTarget, DelegationToolTargetPage, DelegationToolRunSummary, DelegationToolTaskStatus, } from "./tools/types.js";
|
|
20
|
+
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { parseDelegationTargetCatalog } from "./catalog/parse.js";
|
|
2
|
+
import { parseDelegationPolicySnapshot } from "./policy/parse.js";
|
|
3
|
+
import { resolveDelegationPolicy } from "./policy/resolve.js";
|
|
4
|
+
import { DelegationValidationError } from "./shared/errors.js";
|
|
5
|
+
import {
|
|
6
|
+
DelegationOperationError
|
|
7
|
+
} from "./shared/operational-errors.js";
|
|
8
|
+
import {
|
|
9
|
+
DEFAULT_DELEGATION_LIMITS,
|
|
10
|
+
parseDelegationLimits
|
|
11
|
+
} from "./shared/limits.js";
|
|
12
|
+
import { createDelegationCoordinator } from "./coordinator/create.js";
|
|
13
|
+
import {
|
|
14
|
+
parseDelegationInboxAcceptance,
|
|
15
|
+
parseDelegationInboxInspection
|
|
16
|
+
} from "./delivery/parse.js";
|
|
17
|
+
import { createInMemoryDelegationStore } from "./store/memory.js";
|
|
18
|
+
import {
|
|
19
|
+
createDelegationPolicyCeiling,
|
|
20
|
+
parseDelegationCompletionEnvelope,
|
|
21
|
+
parseDelegationHistoricalRequest,
|
|
22
|
+
parseDelegationNotificationRecord,
|
|
23
|
+
parseDelegationOwnerToken,
|
|
24
|
+
parseDelegationParentScope,
|
|
25
|
+
parseDelegationPolicyCeiling,
|
|
26
|
+
parseDelegationResultReference,
|
|
27
|
+
parseDelegationRunOutcome,
|
|
28
|
+
parseDelegationRunRecord,
|
|
29
|
+
parseDelegationTaskObservation,
|
|
30
|
+
parseDelegationTaskRecord
|
|
31
|
+
} from "./tasks/parse.js";
|
|
32
|
+
import { createDelegationHostBinding } from "./tools/create.js";
|
|
33
|
+
export {
|
|
34
|
+
DEFAULT_DELEGATION_LIMITS,
|
|
35
|
+
DelegationOperationError,
|
|
36
|
+
DelegationValidationError,
|
|
37
|
+
createDelegationCoordinator,
|
|
38
|
+
createDelegationHostBinding,
|
|
39
|
+
createDelegationPolicyCeiling,
|
|
40
|
+
createInMemoryDelegationStore,
|
|
41
|
+
parseDelegationCompletionEnvelope,
|
|
42
|
+
parseDelegationHistoricalRequest,
|
|
43
|
+
parseDelegationInboxAcceptance,
|
|
44
|
+
parseDelegationInboxInspection,
|
|
45
|
+
parseDelegationLimits,
|
|
46
|
+
parseDelegationNotificationRecord,
|
|
47
|
+
parseDelegationOwnerToken,
|
|
48
|
+
parseDelegationParentScope,
|
|
49
|
+
parseDelegationPolicyCeiling,
|
|
50
|
+
parseDelegationPolicySnapshot,
|
|
51
|
+
parseDelegationResultReference,
|
|
52
|
+
parseDelegationRunOutcome,
|
|
53
|
+
parseDelegationRunRecord,
|
|
54
|
+
parseDelegationTargetCatalog,
|
|
55
|
+
parseDelegationTaskObservation,
|
|
56
|
+
parseDelegationTaskRecord,
|
|
57
|
+
resolveDelegationPolicy
|
|
58
|
+
};
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import type { DelegationMcpBridge, StartDelegationMcpBridgeOptions } from "./types.js";
|
|
2
|
+
/** Starts one scoped listener. Pure runtime validation does not claim or release the supplied binding. */
|
|
3
|
+
export declare function startDelegationMcpBridge(options: StartDelegationMcpBridgeOptions): Promise<DelegationMcpBridge>;
|
|
4
|
+
//# sourceMappingURL=bridge.d.ts.map
|