@minpeter/pss-runtime 0.1.0-next.0 → 0.1.0-next.2
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/README.md +223 -191
- package/dist/agent-child-runs.js +16 -0
- package/dist/agent-child-runs.js.map +1 -0
- package/dist/agent-host-capabilities.js +9 -0
- package/dist/agent-host-capabilities.js.map +1 -0
- package/dist/agent-host-session-store.js +12 -0
- package/dist/agent-host-session-store.js.map +1 -0
- package/dist/agent-loop.js +59 -35
- package/dist/agent-loop.js.map +1 -1
- package/dist/agent-namespace.js +24 -0
- package/dist/agent-namespace.js.map +1 -0
- package/dist/agent-options.d.ts +35 -0
- package/dist/agent-options.js +16 -0
- package/dist/agent-options.js.map +1 -0
- package/dist/agent-resume.js +143 -0
- package/dist/agent-resume.js.map +1 -0
- package/dist/agent-session-entry.d.ts +13 -0
- package/dist/agent-validation.js +35 -0
- package/dist/agent-validation.js.map +1 -0
- package/dist/agent.d.ts +8 -33
- package/dist/agent.js +131 -55
- package/dist/agent.js.map +1 -1
- package/dist/child-session-cleanups.js +61 -0
- package/dist/child-session-cleanups.js.map +1 -0
- package/dist/execution/host.js +14 -0
- package/dist/execution/host.js.map +1 -0
- package/dist/execution/index.d.ts +4 -0
- package/dist/execution/index.js +3 -0
- package/dist/execution/memory-notifications.js +54 -0
- package/dist/execution/memory-notifications.js.map +1 -0
- package/dist/execution/memory-state.js +34 -0
- package/dist/execution/memory-state.js.map +1 -0
- package/dist/execution/memory-store.js +203 -0
- package/dist/execution/memory-store.js.map +1 -0
- package/dist/execution/memory.d.ts +7 -0
- package/dist/execution/memory.js +28 -0
- package/dist/execution/memory.js.map +1 -0
- package/dist/execution/run.js +55 -0
- package/dist/execution/run.js.map +1 -0
- package/dist/execution/types.d.ts +155 -0
- package/dist/index.d.ts +9 -10
- package/dist/index.js +1 -6
- package/dist/llm-tool-execution.d.ts +35 -0
- package/dist/llm-tool-execution.js +126 -0
- package/dist/llm-tool-execution.js.map +1 -0
- package/dist/llm.d.ts +11 -15
- package/dist/llm.js +5 -9
- package/dist/llm.js.map +1 -1
- package/dist/plugins.d.ts +20 -0
- package/dist/plugins.js +14 -0
- package/dist/plugins.js.map +1 -0
- package/dist/session/events.d.ts +26 -20
- package/dist/session/input-normalization.js +66 -0
- package/dist/session/input-normalization.js.map +1 -0
- package/dist/session/input.d.ts +0 -4
- package/dist/session/mapping.js +1 -2
- package/dist/session/mapping.js.map +1 -1
- package/dist/session/run.js +1 -0
- package/dist/session/run.js.map +1 -1
- package/dist/session/runtime-input.js +20 -58
- package/dist/session/runtime-input.js.map +1 -1
- package/dist/session/session-errors.js +18 -0
- package/dist/session/session-errors.js.map +1 -0
- package/dist/session/session-events.js +59 -0
- package/dist/session/session-events.js.map +1 -0
- package/dist/session/session-execution.js +88 -0
- package/dist/session/session-execution.js.map +1 -0
- package/dist/session/session-kill.js +23 -0
- package/dist/session/session-kill.js.map +1 -0
- package/dist/session/session-notification.js +58 -0
- package/dist/session/session-notification.js.map +1 -0
- package/dist/session/session-runtime-drain.js +22 -0
- package/dist/session/session-runtime-drain.js.map +1 -0
- package/dist/session/session-state.js +102 -0
- package/dist/session/session-state.js.map +1 -0
- package/dist/session/session-turn-error.js +35 -0
- package/dist/session/session-turn-error.js.map +1 -0
- package/dist/session/session-turn-processor.js +135 -0
- package/dist/session/session-turn-processor.js.map +1 -0
- package/dist/session/session.js +125 -335
- package/dist/session/session.js.map +1 -1
- package/dist/session/snapshot.js +5 -31
- package/dist/session/snapshot.js.map +1 -1
- package/dist/session/store/file.d.ts +1 -0
- package/dist/session/store/file.js +14 -0
- package/dist/session/store/file.js.map +1 -1
- package/dist/session/store/memory.d.ts +1 -0
- package/dist/session/store/memory.js +5 -0
- package/dist/session/store/memory.js.map +1 -1
- package/dist/session/store/types.d.ts +1 -0
- package/dist/subagent-background-child-run-state.js +51 -0
- package/dist/subagent-background-child-run-state.js.map +1 -0
- package/dist/subagent-background-child-run.js +103 -0
- package/dist/subagent-background-child-run.js.map +1 -0
- package/dist/subagent-background-in-process.js +98 -0
- package/dist/subagent-background-in-process.js.map +1 -0
- package/dist/subagent-background-notification-inbox.js +106 -0
- package/dist/subagent-background-notification-inbox.js.map +1 -0
- package/dist/subagent-background-notify.js +136 -0
- package/dist/subagent-background-notify.js.map +1 -0
- package/dist/subagent-background-resume-group.js +99 -0
- package/dist/subagent-background-resume-group.js.map +1 -0
- package/dist/subagent-background-runner.js +115 -0
- package/dist/subagent-background-runner.js.map +1 -0
- package/dist/subagent-background-schedule.js +43 -0
- package/dist/subagent-background-schedule.js.map +1 -0
- package/dist/subagent-child-run.js +68 -0
- package/dist/subagent-child-run.js.map +1 -0
- package/dist/subagent-job-cancel.js +84 -0
- package/dist/subagent-job-cancel.js.map +1 -0
- package/dist/subagent-job-observer.js +19 -0
- package/dist/subagent-job-observer.js.map +1 -0
- package/dist/subagent-job-output.js +87 -0
- package/dist/subagent-job-output.js.map +1 -0
- package/dist/subagent-job-state.js +66 -0
- package/dist/subagent-job-state.js.map +1 -0
- package/dist/subagent-jobs.js +96 -0
- package/dist/subagent-jobs.js.map +1 -0
- package/dist/subagent-prompt-schema.js +114 -0
- package/dist/subagent-prompt-schema.js.map +1 -0
- package/dist/subagent-run.js +111 -0
- package/dist/subagent-run.js.map +1 -0
- package/dist/subagents.js +125 -0
- package/dist/subagents.js.map +1 -0
- package/package.json +11 -6
- package/dist/plugins/compaction.d.ts +0 -15
- package/dist/plugins/compaction.js +0 -98
- package/dist/plugins/compaction.js.map +0 -1
- package/dist/plugins/index.d.ts +0 -5
- package/dist/plugins/index.js +0 -5
- package/dist/plugins/memory.d.ts +0 -11
- package/dist/plugins/memory.js +0 -146
- package/dist/plugins/memory.js.map +0 -1
- package/dist/plugins/runner.d.ts +0 -1
- package/dist/plugins/runner.js +0 -83
- package/dist/plugins/runner.js.map +0 -1
- package/dist/plugins/scope.js +0 -13
- package/dist/plugins/scope.js.map +0 -1
- package/dist/plugins/sessions.d.ts +0 -12
- package/dist/plugins/sessions.js +0 -34
- package/dist/plugins/sessions.js.map +0 -1
- package/dist/plugins/tool-hook-handlers.js +0 -77
- package/dist/plugins/tool-hook-handlers.js.map +0 -1
- package/dist/plugins/tool-hook-results.js +0 -64
- package/dist/plugins/tool-hook-results.js.map +0 -1
- package/dist/plugins/tool-hooks.js +0 -111
- package/dist/plugins/tool-hooks.js.map +0 -1
- package/dist/plugins/types.d.ts +0 -105
- package/dist/plugins/types.js +0 -20
- package/dist/plugins/types.js.map +0 -1
- package/dist/session/lifecycle.d.ts +0 -12
- package/dist/session/lifecycle.js +0 -126
- package/dist/session/lifecycle.js.map +0 -1
- package/dist/session/overlay-anchor.js +0 -151
- package/dist/session/overlay-anchor.js.map +0 -1
- package/dist/session/overlay.js +0 -141
- package/dist/session/overlay.js.map +0 -1
- package/dist/session/snapshot.d.ts +0 -1
- /package/dist/{agent-loop.d.ts → session/history.d.ts} +0 -0
- /package/dist/session/{runtime-input.d.ts → session-execution.d.ts} +0 -0
- /package/dist/{plugins/scope.d.ts → session/session-state.d.ts} +0 -0
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
import { InMemoryNotificationInbox } from "./memory-notifications.js";
|
|
2
|
+
import { cloneState, createEmptyState } from "./memory-state.js";
|
|
3
|
+
//#region src/execution/memory-store.ts
|
|
4
|
+
const claimableStatuses = new Set([
|
|
5
|
+
"leased",
|
|
6
|
+
"queued",
|
|
7
|
+
"running",
|
|
8
|
+
"suspended"
|
|
9
|
+
]);
|
|
10
|
+
var InMemoryExecutionStore = class {
|
|
11
|
+
#state = createEmptyState();
|
|
12
|
+
#transactionChain = Promise.resolve();
|
|
13
|
+
checkpoints = new InMemoryCheckpointStore(() => this.#state);
|
|
14
|
+
events = new InMemoryEventStore(() => this.#state);
|
|
15
|
+
notifications = new InMemoryNotificationInbox(() => this.#state);
|
|
16
|
+
runs = new InMemoryRunStore(() => this.#state);
|
|
17
|
+
sessions = new InMemoryExecutionSessionStore(() => this.#state);
|
|
18
|
+
async transaction(fn) {
|
|
19
|
+
const previousTransaction = this.#transactionChain;
|
|
20
|
+
let releaseTransaction = () => void 0;
|
|
21
|
+
this.#transactionChain = new Promise((resolve) => {
|
|
22
|
+
releaseTransaction = resolve;
|
|
23
|
+
});
|
|
24
|
+
await previousTransaction;
|
|
25
|
+
const transactionState = cloneState(this.#state);
|
|
26
|
+
const transactionStore = new InMemoryTransactionStore(transactionState);
|
|
27
|
+
try {
|
|
28
|
+
const result = await fn(transactionStore);
|
|
29
|
+
this.#state = transactionState;
|
|
30
|
+
return result;
|
|
31
|
+
} finally {
|
|
32
|
+
releaseTransaction();
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
};
|
|
36
|
+
var InMemoryTransactionStore = class {
|
|
37
|
+
checkpoints;
|
|
38
|
+
events;
|
|
39
|
+
notifications;
|
|
40
|
+
runs;
|
|
41
|
+
sessions;
|
|
42
|
+
#state;
|
|
43
|
+
constructor(state) {
|
|
44
|
+
this.#state = state;
|
|
45
|
+
this.checkpoints = new InMemoryCheckpointStore(() => this.#state);
|
|
46
|
+
this.events = new InMemoryEventStore(() => this.#state);
|
|
47
|
+
this.notifications = new InMemoryNotificationInbox(() => this.#state);
|
|
48
|
+
this.runs = new InMemoryRunStore(() => this.#state);
|
|
49
|
+
this.sessions = new InMemoryExecutionSessionStore(() => this.#state);
|
|
50
|
+
}
|
|
51
|
+
};
|
|
52
|
+
var InMemoryExecutionSessionStore = class {
|
|
53
|
+
#state;
|
|
54
|
+
constructor(state) {
|
|
55
|
+
this.#state = state;
|
|
56
|
+
}
|
|
57
|
+
commit(key, next, options) {
|
|
58
|
+
const state = this.#state();
|
|
59
|
+
const currentVersion = state.sessions.get(key)?.version ?? null;
|
|
60
|
+
if (options.expectedVersion !== currentVersion) return Promise.resolve({
|
|
61
|
+
ok: false,
|
|
62
|
+
reason: "conflict"
|
|
63
|
+
});
|
|
64
|
+
const versionNumber = (state.sessionVersions.get(key) ?? 0) + 1;
|
|
65
|
+
const version = String(versionNumber);
|
|
66
|
+
state.sessionVersions.set(key, versionNumber);
|
|
67
|
+
state.sessions.set(key, structuredClone({
|
|
68
|
+
state: next.state,
|
|
69
|
+
version
|
|
70
|
+
}));
|
|
71
|
+
return Promise.resolve({
|
|
72
|
+
ok: true,
|
|
73
|
+
version
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
delete(key) {
|
|
77
|
+
const state = this.#state();
|
|
78
|
+
state.sessions.delete(key);
|
|
79
|
+
state.sessionVersions.delete(key);
|
|
80
|
+
return Promise.resolve();
|
|
81
|
+
}
|
|
82
|
+
load(key) {
|
|
83
|
+
const stored = this.#state().sessions.get(key);
|
|
84
|
+
return Promise.resolve(stored ? structuredClone(stored) : null);
|
|
85
|
+
}
|
|
86
|
+
};
|
|
87
|
+
var InMemoryRunStore = class {
|
|
88
|
+
#state;
|
|
89
|
+
constructor(state) {
|
|
90
|
+
this.#state = state;
|
|
91
|
+
}
|
|
92
|
+
create(record) {
|
|
93
|
+
const stored = structuredClone(record);
|
|
94
|
+
this.#state().runs.set(record.runId, stored);
|
|
95
|
+
return Promise.resolve(structuredClone(stored));
|
|
96
|
+
}
|
|
97
|
+
get(runId) {
|
|
98
|
+
const record = this.#state().runs.get(runId);
|
|
99
|
+
return Promise.resolve(record ? structuredClone(record) : null);
|
|
100
|
+
}
|
|
101
|
+
getByDedupeKey(dedupeKey) {
|
|
102
|
+
const record = [...this.#state().runs.values()].find((candidate) => candidate.dedupeKey === dedupeKey);
|
|
103
|
+
return Promise.resolve(record ? structuredClone(record) : null);
|
|
104
|
+
}
|
|
105
|
+
listByParentRunId(parentRunId) {
|
|
106
|
+
const records = [...this.#state().runs.values()].filter((candidate) => candidate.parentRunId === parentRunId);
|
|
107
|
+
return Promise.resolve(records.map((record) => structuredClone(record)));
|
|
108
|
+
}
|
|
109
|
+
update(record) {
|
|
110
|
+
const stored = structuredClone(record);
|
|
111
|
+
this.#state().runs.set(record.runId, stored);
|
|
112
|
+
return Promise.resolve(structuredClone(stored));
|
|
113
|
+
}
|
|
114
|
+
claim(runId, options) {
|
|
115
|
+
const record = this.#state().runs.get(runId);
|
|
116
|
+
if (!record) return Promise.resolve({
|
|
117
|
+
ok: false,
|
|
118
|
+
reason: "not-found"
|
|
119
|
+
});
|
|
120
|
+
if (!claimableStatuses.has(record.status)) return Promise.resolve({
|
|
121
|
+
ok: false,
|
|
122
|
+
reason: "not-claimable"
|
|
123
|
+
});
|
|
124
|
+
if (record.lease && record.lease.leaseUntilMs > options.nowMs) return Promise.resolve({
|
|
125
|
+
ok: false,
|
|
126
|
+
reason: "leased"
|
|
127
|
+
});
|
|
128
|
+
const lease = {
|
|
129
|
+
attempt: options.attempt,
|
|
130
|
+
leaseId: options.leaseId,
|
|
131
|
+
leaseUntilMs: options.nowMs + options.leaseMs
|
|
132
|
+
};
|
|
133
|
+
const claimed = {
|
|
134
|
+
...record,
|
|
135
|
+
lease,
|
|
136
|
+
status: "leased"
|
|
137
|
+
};
|
|
138
|
+
this.#state().runs.set(runId, claimed);
|
|
139
|
+
return Promise.resolve({
|
|
140
|
+
lease,
|
|
141
|
+
ok: true,
|
|
142
|
+
record: structuredClone(claimed)
|
|
143
|
+
});
|
|
144
|
+
}
|
|
145
|
+
};
|
|
146
|
+
var InMemoryCheckpointStore = class {
|
|
147
|
+
#state;
|
|
148
|
+
constructor(state) {
|
|
149
|
+
this.#state = state;
|
|
150
|
+
}
|
|
151
|
+
append(checkpoint, options) {
|
|
152
|
+
const run = this.#state().runs.get(checkpoint.runId);
|
|
153
|
+
const currentVersion = run?.checkpointVersion ?? 0;
|
|
154
|
+
if (currentVersion !== options.expectedVersion) return Promise.resolve({
|
|
155
|
+
currentVersion,
|
|
156
|
+
ok: false,
|
|
157
|
+
reason: "stale-version"
|
|
158
|
+
});
|
|
159
|
+
const stored = structuredClone(checkpoint);
|
|
160
|
+
const checkpoints = this.#state().checkpoints.get(checkpoint.runId) ?? [];
|
|
161
|
+
checkpoints.push(stored);
|
|
162
|
+
this.#state().checkpoints.set(checkpoint.runId, checkpoints);
|
|
163
|
+
if (run) this.#state().runs.set(checkpoint.runId, {
|
|
164
|
+
...run,
|
|
165
|
+
checkpointVersion: checkpoint.version
|
|
166
|
+
});
|
|
167
|
+
return Promise.resolve({
|
|
168
|
+
ok: true,
|
|
169
|
+
version: checkpoint.version
|
|
170
|
+
});
|
|
171
|
+
}
|
|
172
|
+
latest(runId) {
|
|
173
|
+
const checkpoint = (this.#state().checkpoints.get(runId) ?? []).at(-1);
|
|
174
|
+
return Promise.resolve(checkpoint ? structuredClone(checkpoint) : null);
|
|
175
|
+
}
|
|
176
|
+
};
|
|
177
|
+
var InMemoryEventStore = class {
|
|
178
|
+
#state;
|
|
179
|
+
constructor(state) {
|
|
180
|
+
this.#state = state;
|
|
181
|
+
}
|
|
182
|
+
append(runId, event) {
|
|
183
|
+
const events = this.#state().events.get(runId) ?? [];
|
|
184
|
+
const cursor = { offset: events.length + 1 };
|
|
185
|
+
events.push({
|
|
186
|
+
cursor,
|
|
187
|
+
event: structuredClone(event),
|
|
188
|
+
runId
|
|
189
|
+
});
|
|
190
|
+
this.#state().events.set(runId, events);
|
|
191
|
+
return Promise.resolve(cursor);
|
|
192
|
+
}
|
|
193
|
+
async *read(runId, cursor) {
|
|
194
|
+
await Promise.resolve();
|
|
195
|
+
const events = this.#state().events.get(runId) ?? [];
|
|
196
|
+
const start = cursor?.offset ?? 0;
|
|
197
|
+
for (const event of events.slice(start)) yield structuredClone(event);
|
|
198
|
+
}
|
|
199
|
+
};
|
|
200
|
+
//#endregion
|
|
201
|
+
export { InMemoryExecutionStore };
|
|
202
|
+
|
|
203
|
+
//# sourceMappingURL=memory-store.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"memory-store.js","names":["#state","#transactionChain"],"sources":["../../src/execution/memory-store.ts"],"sourcesContent":["import type { AgentEvent } from \"../session/events\";\nimport type {\n CommitResult,\n ExpectedSessionVersion,\n SessionStore,\n SessionStoreCommit,\n StoredSession,\n} from \"../session/store/types\";\nimport { InMemoryNotificationInbox } from \"./memory-notifications\";\nimport type { ExecutionState } from \"./memory-state\";\nimport { cloneState, createEmptyState } from \"./memory-state\";\nimport type {\n CheckpointStore,\n CheckpointWriteResult,\n ClaimRunOptions,\n ClaimRunResult,\n EventCursor,\n EventStore,\n ExecutionStore,\n ExecutionStoreTransaction,\n NotificationInbox,\n RunCheckpoint,\n RunRecord,\n RunStatus,\n RunStore,\n StoredAgentEvent,\n} from \"./types\";\n\nconst claimableStatuses = new Set<RunStatus>([\n \"leased\",\n \"queued\",\n \"running\",\n \"suspended\",\n]);\n\nexport class InMemoryExecutionStore implements ExecutionStore {\n #state = createEmptyState();\n #transactionChain: Promise<void> = Promise.resolve();\n readonly checkpoints: CheckpointStore = new InMemoryCheckpointStore(\n () => this.#state\n );\n readonly events: EventStore = new InMemoryEventStore(() => this.#state);\n readonly notifications: NotificationInbox = new InMemoryNotificationInbox(\n () => this.#state\n );\n readonly runs: RunStore = new InMemoryRunStore(() => this.#state);\n readonly sessions: SessionStore = new InMemoryExecutionSessionStore(\n () => this.#state\n );\n\n async transaction<T>(\n fn: (tx: ExecutionStoreTransaction) => Promise<T>\n ): Promise<T> {\n const previousTransaction = this.#transactionChain;\n let releaseTransaction: () => void = () => undefined;\n this.#transactionChain = new Promise<void>((resolve) => {\n releaseTransaction = resolve;\n });\n await previousTransaction;\n const transactionState = cloneState(this.#state);\n const transactionStore = new InMemoryTransactionStore(transactionState);\n try {\n const result = await fn(transactionStore);\n this.#state = transactionState;\n return result;\n } finally {\n releaseTransaction();\n }\n }\n}\n\nclass InMemoryTransactionStore implements ExecutionStoreTransaction {\n readonly checkpoints: CheckpointStore;\n readonly events: EventStore;\n readonly notifications: NotificationInbox;\n readonly runs: RunStore;\n readonly sessions: SessionStore;\n readonly #state: ExecutionState;\n\n constructor(state: ExecutionState) {\n this.#state = state;\n this.checkpoints = new InMemoryCheckpointStore(() => this.#state);\n this.events = new InMemoryEventStore(() => this.#state);\n this.notifications = new InMemoryNotificationInbox(() => this.#state);\n this.runs = new InMemoryRunStore(() => this.#state);\n this.sessions = new InMemoryExecutionSessionStore(() => this.#state);\n }\n}\n\nclass InMemoryExecutionSessionStore implements SessionStore {\n readonly #state: () => ExecutionState;\n\n constructor(state: () => ExecutionState) {\n this.#state = state;\n }\n\n commit(\n key: string,\n next: SessionStoreCommit,\n options: { readonly expectedVersion: ExpectedSessionVersion }\n ): Promise<CommitResult> {\n const state = this.#state();\n const current = state.sessions.get(key);\n const currentVersion = current?.version ?? null;\n\n if (options.expectedVersion !== currentVersion) {\n return Promise.resolve({ ok: false, reason: \"conflict\" });\n }\n\n const versionNumber = (state.sessionVersions.get(key) ?? 0) + 1;\n const version = String(versionNumber);\n state.sessionVersions.set(key, versionNumber);\n state.sessions.set(key, structuredClone({ state: next.state, version }));\n return Promise.resolve({ ok: true, version });\n }\n\n delete(key: string): Promise<void> {\n const state = this.#state();\n state.sessions.delete(key);\n state.sessionVersions.delete(key);\n return Promise.resolve();\n }\n\n load(key: string): Promise<StoredSession | null> {\n const stored = this.#state().sessions.get(key);\n return Promise.resolve(stored ? structuredClone(stored) : null);\n }\n}\n\nclass InMemoryRunStore implements RunStore {\n readonly #state: () => ExecutionState;\n\n constructor(state: () => ExecutionState) {\n this.#state = state;\n }\n\n create(record: RunRecord): Promise<RunRecord> {\n const stored = structuredClone(record);\n this.#state().runs.set(record.runId, stored);\n return Promise.resolve(structuredClone(stored));\n }\n\n get(runId: string): Promise<RunRecord | null> {\n const record = this.#state().runs.get(runId);\n return Promise.resolve(record ? structuredClone(record) : null);\n }\n\n getByDedupeKey(dedupeKey: string): Promise<RunRecord | null> {\n const record = [...this.#state().runs.values()].find(\n (candidate) => candidate.dedupeKey === dedupeKey\n );\n return Promise.resolve(record ? structuredClone(record) : null);\n }\n\n listByParentRunId(parentRunId: string): Promise<readonly RunRecord[]> {\n const records = [...this.#state().runs.values()].filter(\n (candidate) => candidate.parentRunId === parentRunId\n );\n return Promise.resolve(records.map((record) => structuredClone(record)));\n }\n\n update(record: RunRecord): Promise<RunRecord> {\n const stored = structuredClone(record);\n this.#state().runs.set(record.runId, stored);\n return Promise.resolve(structuredClone(stored));\n }\n\n claim(runId: string, options: ClaimRunOptions): Promise<ClaimRunResult> {\n const record = this.#state().runs.get(runId);\n if (!record) {\n return Promise.resolve({ ok: false, reason: \"not-found\" });\n }\n\n if (!claimableStatuses.has(record.status)) {\n return Promise.resolve({ ok: false, reason: \"not-claimable\" });\n }\n\n if (record.lease && record.lease.leaseUntilMs > options.nowMs) {\n return Promise.resolve({ ok: false, reason: \"leased\" });\n }\n\n const lease = {\n attempt: options.attempt,\n leaseId: options.leaseId,\n leaseUntilMs: options.nowMs + options.leaseMs,\n };\n const claimed: RunRecord = {\n ...record,\n lease,\n status: \"leased\",\n };\n this.#state().runs.set(runId, claimed);\n return Promise.resolve({\n lease,\n ok: true,\n record: structuredClone(claimed),\n });\n }\n}\n\nclass InMemoryCheckpointStore implements CheckpointStore {\n readonly #state: () => ExecutionState;\n\n constructor(state: () => ExecutionState) {\n this.#state = state;\n }\n\n append(\n checkpoint: RunCheckpoint,\n options: { readonly expectedVersion: number }\n ): Promise<CheckpointWriteResult> {\n const run = this.#state().runs.get(checkpoint.runId);\n const currentVersion = run?.checkpointVersion ?? 0;\n if (currentVersion !== options.expectedVersion) {\n return Promise.resolve({\n currentVersion,\n ok: false,\n reason: \"stale-version\",\n });\n }\n\n const stored = structuredClone(checkpoint);\n const checkpoints = this.#state().checkpoints.get(checkpoint.runId) ?? [];\n checkpoints.push(stored);\n this.#state().checkpoints.set(checkpoint.runId, checkpoints);\n if (run) {\n this.#state().runs.set(checkpoint.runId, {\n ...run,\n checkpointVersion: checkpoint.version,\n });\n }\n\n return Promise.resolve({ ok: true, version: checkpoint.version });\n }\n\n latest(runId: string): Promise<RunCheckpoint | null> {\n const checkpoints = this.#state().checkpoints.get(runId) ?? [];\n const checkpoint = checkpoints.at(-1);\n return Promise.resolve(checkpoint ? structuredClone(checkpoint) : null);\n }\n}\n\nclass InMemoryEventStore implements EventStore {\n readonly #state: () => ExecutionState;\n\n constructor(state: () => ExecutionState) {\n this.#state = state;\n }\n\n append(runId: string, event: AgentEvent): Promise<EventCursor> {\n const events = this.#state().events.get(runId) ?? [];\n const cursor = { offset: events.length + 1 };\n events.push({\n cursor,\n event: structuredClone(event),\n runId,\n });\n this.#state().events.set(runId, events);\n return Promise.resolve(cursor);\n }\n\n async *read(\n runId: string,\n cursor?: EventCursor\n ): AsyncIterable<StoredAgentEvent> {\n await Promise.resolve();\n const events = this.#state().events.get(runId) ?? [];\n const start = cursor?.offset ?? 0;\n for (const event of events.slice(start)) {\n yield structuredClone(event);\n }\n }\n}\n"],"mappings":";;;AA4BA,MAAM,oBAAoB,IAAI,IAAe;CAC3C;CACA;CACA;CACA;AACF,CAAC;AAED,IAAa,yBAAb,MAA8D;CAC5D,SAAS,iBAAiB;CAC1B,oBAAmC,QAAQ,QAAQ;CACnD,cAAwC,IAAI,8BACpC,KAAKA,MACb;CACA,SAA8B,IAAI,yBAAyB,KAAKA,MAAM;CACtE,gBAA4C,IAAI,gCACxC,KAAKA,MACb;CACA,OAA0B,IAAI,uBAAuB,KAAKA,MAAM;CAChE,WAAkC,IAAI,oCAC9B,KAAKA,MACb;CAEA,MAAM,YACJ,IACY;EACZ,MAAM,sBAAsB,KAAKC;EACjC,IAAI,2BAAuC,KAAA;EAC3C,KAAKA,oBAAoB,IAAI,SAAe,YAAY;GACtD,qBAAqB;EACvB,CAAC;EACD,MAAM;EACN,MAAM,mBAAmB,WAAW,KAAKD,MAAM;EAC/C,MAAM,mBAAmB,IAAI,yBAAyB,gBAAgB;EACtE,IAAI;GACF,MAAM,SAAS,MAAM,GAAG,gBAAgB;GACxC,KAAKA,SAAS;GACd,OAAO;EACT,UAAU;GACR,mBAAmB;EACrB;CACF;AACF;AAEA,IAAM,2BAAN,MAAoE;CAClE;CACA;CACA;CACA;CACA;CACA;CAEA,YAAY,OAAuB;E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KA,OAAO,EAAE,OAAO,IAAI,KAAK,KAAK,CAAC;EACnD,MAAM,SAAS,EAAE,QAAQ,OAAO,SAAS,EAAE;EAC3C,OAAO,KAAK;GACV;GACA,OAAO,gBAAgB,KAAK;GAC5B;EACF,CAAC;EACD,KAAKA,OAAO,EAAE,OAAO,IAAI,OAAO,MAAM;EACtC,OAAO,QAAQ,QAAQ,MAAM;CAC/B;CAEA,OAAO,KACL,OACA,QACiC;EACjC,MAAM,QAAQ,QAAQ;EACtB,MAAM,SAAS,KAAKA,OAAO,EAAE,OAAO,IAAI,KAAK,KAAK,CAAC;EACnD,MAAM,QAAQ,QAAQ,UAAU;EAChC,KAAK,MAAM,SAAS,OAAO,MAAM,KAAK,GACpC,MAAM,gBAAgB,KAAK;CAE/B;AACF"}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { InMemoryExecutionStore } from "./memory-store.js";
|
|
2
|
+
//#region src/execution/memory.ts
|
|
3
|
+
function createInMemoryExecutionHost() {
|
|
4
|
+
return {
|
|
5
|
+
capabilities: { backgroundSubagents: "in-process" },
|
|
6
|
+
store: new InMemoryExecutionStore(),
|
|
7
|
+
scheduler: new InMemoryExecutionScheduler()
|
|
8
|
+
};
|
|
9
|
+
}
|
|
10
|
+
var InMemoryExecutionScheduler = class {
|
|
11
|
+
#queuedRunIds = [];
|
|
12
|
+
#resumedSessions = [];
|
|
13
|
+
enqueueRun(runId) {
|
|
14
|
+
this.#queuedRunIds.push(runId);
|
|
15
|
+
return Promise.resolve();
|
|
16
|
+
}
|
|
17
|
+
resumeSession(sessionKey, options) {
|
|
18
|
+
this.#resumedSessions.push({
|
|
19
|
+
options,
|
|
20
|
+
sessionKey
|
|
21
|
+
});
|
|
22
|
+
return Promise.resolve();
|
|
23
|
+
}
|
|
24
|
+
};
|
|
25
|
+
//#endregion
|
|
26
|
+
export { createInMemoryExecutionHost };
|
|
27
|
+
|
|
28
|
+
//# sourceMappingURL=memory.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"memory.js","names":["#queuedRunIds","#resumedSessions"],"sources":["../../src/execution/memory.ts"],"sourcesContent":["import { InMemoryExecutionStore } from \"./memory-store\";\nimport type {\n ExecutionHost,\n ExecutionScheduler,\n ResumeSessionOptions,\n} from \"./types\";\n\nexport function createInMemoryExecutionHost(): ExecutionHost {\n return {\n capabilities: {\n backgroundSubagents: \"in-process\",\n },\n store: new InMemoryExecutionStore(),\n scheduler: new InMemoryExecutionScheduler(),\n };\n}\n\nclass InMemoryExecutionScheduler implements ExecutionScheduler {\n readonly #queuedRunIds: string[] = [];\n readonly #resumedSessions: {\n readonly options: ResumeSessionOptions;\n readonly sessionKey: string;\n }[] = [];\n\n enqueueRun(runId: string): Promise<void> {\n this.#queuedRunIds.push(runId);\n return Promise.resolve();\n }\n\n resumeSession(\n sessionKey: string,\n options: ResumeSessionOptions\n ): Promise<void> {\n this.#resumedSessions.push({ options, sessionKey });\n return Promise.resolve();\n }\n}\n"],"mappings":";;AAOA,SAAgB,8BAA6C;CAC3D,OAAO;EACL,cAAc,EACZ,qBAAqB,aACvB;EACA,OAAO,IAAI,uBAAuB;EAClC,WAAW,IAAI,2BAA2B;CAC5C;AACF;AAEA,IAAM,6BAAN,MAA+D;CAC7D,gBAAmC,CAAC;CACpC,mBAGM,CAAC;CAEP,WAAW,OAA8B;EACvC,KAAKA,cAAc,KAAK,KAAK;EAC7B,OAAO,QAAQ,QAAQ;CACzB;CAEA,cACE,YACA,SACe;EACf,KAAKC,iBAAiB,KAAK;GAAE;GAAS;EAAW,CAAC;EAClD,OAAO,QAAQ,QAAQ;CACzB;AACF"}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
//#region src/execution/run.ts
|
|
2
|
+
var StoredAgentRun = class {
|
|
3
|
+
#cursor;
|
|
4
|
+
#eventStore;
|
|
5
|
+
#runId;
|
|
6
|
+
#eventsStarted = false;
|
|
7
|
+
constructor({ cursor, eventStore, runId }) {
|
|
8
|
+
this.#cursor = cursor;
|
|
9
|
+
this.#eventStore = eventStore;
|
|
10
|
+
this.#runId = runId;
|
|
11
|
+
}
|
|
12
|
+
events() {
|
|
13
|
+
if (this.#eventsStarted) throw new Error("AgentRun.events() can only be consumed once");
|
|
14
|
+
this.#eventsStarted = true;
|
|
15
|
+
return new StoredAgentEventIterator(this.#eventStore.read(this.#runId, this.#cursor));
|
|
16
|
+
}
|
|
17
|
+
};
|
|
18
|
+
var StoredAgentEventIterator = class {
|
|
19
|
+
#source;
|
|
20
|
+
#nextPending = false;
|
|
21
|
+
constructor(events) {
|
|
22
|
+
this.#source = events[Symbol.asyncIterator]();
|
|
23
|
+
}
|
|
24
|
+
[Symbol.asyncIterator]() {
|
|
25
|
+
return this;
|
|
26
|
+
}
|
|
27
|
+
async next() {
|
|
28
|
+
if (this.#nextPending) throw new Error("AgentRun.events() does not allow concurrent next() calls");
|
|
29
|
+
this.#nextPending = true;
|
|
30
|
+
try {
|
|
31
|
+
const next = await this.#source.next();
|
|
32
|
+
if (next.done) return {
|
|
33
|
+
done: true,
|
|
34
|
+
value: void 0
|
|
35
|
+
};
|
|
36
|
+
return {
|
|
37
|
+
done: false,
|
|
38
|
+
value: next.value.event
|
|
39
|
+
};
|
|
40
|
+
} finally {
|
|
41
|
+
this.#nextPending = false;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
async return() {
|
|
45
|
+
await this.#source.return?.();
|
|
46
|
+
return {
|
|
47
|
+
done: true,
|
|
48
|
+
value: void 0
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
};
|
|
52
|
+
//#endregion
|
|
53
|
+
export { StoredAgentRun };
|
|
54
|
+
|
|
55
|
+
//# sourceMappingURL=run.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"run.js","names":["#cursor","#eventStore","#runId","#eventsStarted","#source","#nextPending"],"sources":["../../src/execution/run.ts"],"sourcesContent":["import type { AgentEvent } from \"../session/events\";\nimport type { AgentRun } from \"../session/run\";\nimport type { EventCursor, EventStore, StoredAgentEvent } from \"./types\";\n\nexport class StoredAgentRun implements AgentRun {\n readonly #cursor: EventCursor | undefined;\n readonly #eventStore: EventStore;\n readonly #runId: string;\n #eventsStarted = false;\n\n constructor({\n cursor,\n eventStore,\n runId,\n }: {\n readonly cursor?: EventCursor;\n readonly eventStore: EventStore;\n readonly runId: string;\n }) {\n this.#cursor = cursor;\n this.#eventStore = eventStore;\n this.#runId = runId;\n }\n\n events(): AsyncIterable<AgentEvent> {\n if (this.#eventsStarted) {\n throw new Error(\"AgentRun.events() can only be consumed once\");\n }\n this.#eventsStarted = true;\n\n return new StoredAgentEventIterator(\n this.#eventStore.read(this.#runId, this.#cursor)\n );\n }\n}\n\nclass StoredAgentEventIterator implements AsyncIterableIterator<AgentEvent> {\n readonly #source: AsyncIterator<StoredAgentEvent>;\n #nextPending = false;\n\n constructor(events: AsyncIterable<StoredAgentEvent>) {\n this.#source = events[Symbol.asyncIterator]();\n }\n\n [Symbol.asyncIterator](): AsyncIterableIterator<AgentEvent> {\n return this;\n }\n\n async next(): Promise<IteratorResult<AgentEvent>> {\n if (this.#nextPending) {\n throw new Error(\n \"AgentRun.events() does not allow concurrent next() calls\"\n );\n }\n\n this.#nextPending = true;\n try {\n const next = await this.#source.next();\n if (next.done) {\n return { done: true, value: undefined };\n }\n\n return { done: false, value: next.value.event };\n } finally {\n this.#nextPending = false;\n }\n }\n\n async return(): Promise<IteratorResult<AgentEvent>> {\n await this.#source.return?.();\n return { done: true, value: undefined };\n }\n}\n"],"mappings":";AAIA,IAAa,iBAAb,MAAgD;CAC9C;CACA;CACA;CACA,iBAAiB;CAEjB,YAAY,EACV,QACA,YACA,SAKC;EACD,KAAKA,UAAU;EACf,KAAKC,cAAc;EACnB,KAAKC,SAAS;CAChB;CAEA,SAAoC;EAClC,IAAI,KAAKC,gBACP,MAAM,IAAI,MAAM,6CAA6C;EAE/D,KAAKA,iBAAiB;EAEtB,OAAO,IAAI,yBACT,KAAKF,YAAY,KAAK,KAAKC,QAAQ,KAAKF,OAAO,CACjD;CACF;AACF;AAEA,IAAM,2BAAN,MAA4E;CAC1E;CACA,eAAe;CAEf,YAAY,QAAyC;EACnD,KAAKI,UAAU,OAAO,OAAO,eAAe;CAC9C;CAEA,CAAC,OAAO,iBAAoD;EAC1D,OAAO;CACT;CAEA,MAAM,OAA4C;EAChD,IAAI,KAAKC,cACP,MAAM,IAAI,MACR,0DACF;EAGF,KAAKA,eAAe;EACpB,IAAI;GACF,MAAM,OAAO,MAAM,KAAKD,QAAQ,KAAK;GACrC,IAAI,KAAK,MACP,OAAO;IAAE,MAAM;IAAM,OAAO,KAAA;GAAU;GAGxC,OAAO;IAAE,MAAM;IAAO,OAAO,KAAK,MAAM;GAAM;EAChD,UAAU;GACR,KAAKC,eAAe;EACtB;CACF;CAEA,MAAM,SAA8C;EAClD,MAAM,KAAKD,QAAQ,SAAS;EAC5B,OAAO;GAAE,MAAM;GAAM,OAAO,KAAA;EAAU;CACxC;AACF"}
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
import { UserInput } from "../session/input.js";
|
|
2
|
+
import { AgentEvent } from "../session/events.js";
|
|
3
|
+
import { SessionStore } from "../session/store/types.js";
|
|
4
|
+
|
|
5
|
+
//#region src/execution/types.d.ts
|
|
6
|
+
type AgentHost = SessionHost | ExecutionHost;
|
|
7
|
+
interface SessionHost {
|
|
8
|
+
readonly capabilities?: AgentHostCapabilities;
|
|
9
|
+
readonly sessionStore?: SessionStore;
|
|
10
|
+
}
|
|
11
|
+
interface ExecutionHost {
|
|
12
|
+
readonly capabilities: AgentHostCapabilities;
|
|
13
|
+
readonly scheduler: ExecutionScheduler;
|
|
14
|
+
readonly store: ExecutionStore;
|
|
15
|
+
}
|
|
16
|
+
interface AgentHostCapabilities {
|
|
17
|
+
readonly backgroundSubagents?: "durable" | "in-process";
|
|
18
|
+
}
|
|
19
|
+
interface ExecutionScheduler {
|
|
20
|
+
enqueueRun(runId: string, options?: {
|
|
21
|
+
readonly runAfterMs?: number;
|
|
22
|
+
}): Promise<void>;
|
|
23
|
+
resumeSession(sessionKey: string, options: ResumeSessionOptions): Promise<void>;
|
|
24
|
+
}
|
|
25
|
+
interface ResumeSessionOptions {
|
|
26
|
+
readonly idempotencyKey?: string;
|
|
27
|
+
readonly notificationId?: string;
|
|
28
|
+
readonly runId: string;
|
|
29
|
+
}
|
|
30
|
+
type RunKind = "background-subagent" | "notification" | "subagent" | "tool-recovery" | "user-turn";
|
|
31
|
+
type RunStatus = "cancelled" | "completed" | "error" | "leased" | "needs-recovery" | "queued" | "running" | "suspended";
|
|
32
|
+
interface RunLease {
|
|
33
|
+
readonly attempt: number;
|
|
34
|
+
readonly leaseId: string;
|
|
35
|
+
readonly leaseUntilMs: number;
|
|
36
|
+
}
|
|
37
|
+
interface RunRecord {
|
|
38
|
+
readonly checkpointVersion: number;
|
|
39
|
+
readonly dedupeKey?: string;
|
|
40
|
+
readonly kind: RunKind;
|
|
41
|
+
readonly lease?: RunLease;
|
|
42
|
+
readonly output?: unknown;
|
|
43
|
+
readonly ownerNamespace?: string;
|
|
44
|
+
readonly parentRunId?: string;
|
|
45
|
+
readonly publicTaskId?: string;
|
|
46
|
+
readonly rootRunId: string;
|
|
47
|
+
readonly runId: string;
|
|
48
|
+
readonly sessionKey: string;
|
|
49
|
+
readonly status: RunStatus;
|
|
50
|
+
}
|
|
51
|
+
type ClaimRunResult = {
|
|
52
|
+
readonly lease: RunLease;
|
|
53
|
+
readonly ok: true;
|
|
54
|
+
readonly record: RunRecord;
|
|
55
|
+
} | {
|
|
56
|
+
readonly ok: false;
|
|
57
|
+
readonly reason: "leased" | "not-claimable" | "not-found";
|
|
58
|
+
};
|
|
59
|
+
interface ClaimRunOptions {
|
|
60
|
+
readonly attempt: number;
|
|
61
|
+
readonly leaseId: string;
|
|
62
|
+
readonly leaseMs: number;
|
|
63
|
+
readonly nowMs: number;
|
|
64
|
+
}
|
|
65
|
+
type CheckpointPhase = "after-model" | "after-notification" | "after-tool" | "before-child-run" | "before-model" | "before-notification" | "before-tool" | "child-linked" | "suspended";
|
|
66
|
+
interface RunCheckpoint {
|
|
67
|
+
readonly checkpointId: string;
|
|
68
|
+
readonly childRunId?: string;
|
|
69
|
+
readonly pendingToolCall?: unknown;
|
|
70
|
+
readonly phase: CheckpointPhase;
|
|
71
|
+
readonly runId: string;
|
|
72
|
+
readonly runtimeState: unknown;
|
|
73
|
+
readonly sessionSnapshot: unknown;
|
|
74
|
+
readonly version: number;
|
|
75
|
+
}
|
|
76
|
+
type CheckpointWriteResult = {
|
|
77
|
+
readonly ok: true;
|
|
78
|
+
readonly version: number;
|
|
79
|
+
} | {
|
|
80
|
+
readonly currentVersion: number;
|
|
81
|
+
readonly ok: false;
|
|
82
|
+
readonly reason: "stale-version";
|
|
83
|
+
};
|
|
84
|
+
interface EventCursor {
|
|
85
|
+
readonly offset: number;
|
|
86
|
+
}
|
|
87
|
+
interface StoredAgentEvent {
|
|
88
|
+
readonly cursor: EventCursor;
|
|
89
|
+
readonly event: AgentEvent;
|
|
90
|
+
readonly runId: string;
|
|
91
|
+
}
|
|
92
|
+
type NotificationStatus = "acked" | "cancelled" | "pending";
|
|
93
|
+
interface NotificationRecord {
|
|
94
|
+
readonly idempotencyKey: string;
|
|
95
|
+
readonly input: UserInput;
|
|
96
|
+
readonly notificationId: string;
|
|
97
|
+
readonly observerEvents?: readonly AgentEvent[];
|
|
98
|
+
readonly ownerNamespace?: string;
|
|
99
|
+
readonly runId: string;
|
|
100
|
+
readonly sessionKey: string;
|
|
101
|
+
readonly status: NotificationStatus;
|
|
102
|
+
}
|
|
103
|
+
type NotificationWriteResult = {
|
|
104
|
+
readonly ok: true;
|
|
105
|
+
} | {
|
|
106
|
+
readonly existingNotificationId: string;
|
|
107
|
+
readonly ok: false;
|
|
108
|
+
readonly reason: "duplicate";
|
|
109
|
+
};
|
|
110
|
+
type NotificationClaimResult = {
|
|
111
|
+
readonly ok: true;
|
|
112
|
+
readonly record: NotificationRecord;
|
|
113
|
+
} | {
|
|
114
|
+
readonly ok: false;
|
|
115
|
+
readonly reason: "already-claimed" | "not-found";
|
|
116
|
+
readonly record?: NotificationRecord;
|
|
117
|
+
};
|
|
118
|
+
interface RunStore {
|
|
119
|
+
claim(runId: string, options: ClaimRunOptions): Promise<ClaimRunResult>;
|
|
120
|
+
create(record: RunRecord): Promise<RunRecord>;
|
|
121
|
+
get(runId: string): Promise<RunRecord | null>;
|
|
122
|
+
getByDedupeKey(dedupeKey: string): Promise<RunRecord | null>;
|
|
123
|
+
listByParentRunId(parentRunId: string): Promise<readonly RunRecord[]>;
|
|
124
|
+
update(record: RunRecord): Promise<RunRecord>;
|
|
125
|
+
}
|
|
126
|
+
interface CheckpointStore {
|
|
127
|
+
append(checkpoint: RunCheckpoint, options: {
|
|
128
|
+
readonly expectedVersion: number;
|
|
129
|
+
}): Promise<CheckpointWriteResult>;
|
|
130
|
+
latest(runId: string): Promise<RunCheckpoint | null>;
|
|
131
|
+
}
|
|
132
|
+
interface EventStore {
|
|
133
|
+
append(runId: string, event: AgentEvent): Promise<EventCursor>;
|
|
134
|
+
read(runId: string, cursor?: EventCursor): AsyncIterable<StoredAgentEvent>;
|
|
135
|
+
}
|
|
136
|
+
interface NotificationInbox {
|
|
137
|
+
claimByIdempotencyKey(idempotencyKey: string): Promise<NotificationClaimResult>;
|
|
138
|
+
enqueue(record: NotificationRecord): Promise<NotificationWriteResult>;
|
|
139
|
+
getByIdempotencyKey(idempotencyKey: string): Promise<NotificationRecord | null>;
|
|
140
|
+
releaseByIdempotencyKey(idempotencyKey: string): Promise<void>;
|
|
141
|
+
}
|
|
142
|
+
interface ExecutionStorePorts {
|
|
143
|
+
readonly checkpoints: CheckpointStore;
|
|
144
|
+
readonly events: EventStore;
|
|
145
|
+
readonly notifications: NotificationInbox;
|
|
146
|
+
readonly runs: RunStore;
|
|
147
|
+
readonly sessions: SessionStore;
|
|
148
|
+
}
|
|
149
|
+
interface ExecutionStoreTransaction extends ExecutionStorePorts {}
|
|
150
|
+
interface ExecutionStore extends ExecutionStorePorts {
|
|
151
|
+
transaction<T>(fn: (tx: ExecutionStoreTransaction) => Promise<T>): Promise<T>;
|
|
152
|
+
}
|
|
153
|
+
//#endregion
|
|
154
|
+
export { AgentHost, AgentHostCapabilities, CheckpointPhase, CheckpointStore, CheckpointWriteResult, ClaimRunOptions, ClaimRunResult, EventCursor, EventStore, ExecutionHost, ExecutionScheduler, ExecutionStore, ExecutionStoreTransaction, NotificationClaimResult, NotificationInbox, NotificationRecord, NotificationStatus, NotificationWriteResult, ResumeSessionOptions, RunCheckpoint, RunKind, RunLease, RunRecord, RunStatus, RunStore, StoredAgentEvent };
|
|
155
|
+
//# sourceMappingURL=types.d.ts.map
|
package/dist/index.d.ts
CHANGED
|
@@ -1,12 +1,11 @@
|
|
|
1
|
-
import { AgentToolChoice,
|
|
1
|
+
import { AgentToolChoice, RuntimeCreateLlmOptions, RuntimeLlm, RuntimeLlmContext, RuntimeLlmOutput, RuntimeLlmOutputPart, createLlm } from "./llm.js";
|
|
2
2
|
import { AgentInput, SessionInput, UserInput, UserMessage, UserMessageContent, UserMessageContentPart, UserMessageFileData, UserMessageFilePart, UserMessageImagePart, UserMessageTextPart, UserText, UserTextContent } from "./session/input.js";
|
|
3
|
-
import { AgentEvent, AgentEventListener, AssistantReasoning, AssistantText,
|
|
4
|
-
import { AgentRun } from "./session/run.js";
|
|
3
|
+
import { AgentEvent, AgentEventListener, AssistantReasoning, AssistantText, RuntimeInput, ToolCall, ToolResult } from "./session/events.js";
|
|
5
4
|
import { CommitResult, ExpectedSessionVersion, SessionStore, SessionStoreCommit, StoredSession } from "./session/store/types.js";
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
8
|
-
import {
|
|
9
|
-
import {
|
|
10
|
-
import {
|
|
11
|
-
import { Agent
|
|
12
|
-
export { Agent, type
|
|
5
|
+
import { AgentHost } from "./execution/types.js";
|
|
6
|
+
import { AgentEventContext, AgentPlugin } from "./plugins.js";
|
|
7
|
+
import { AgentOptions } from "./agent-options.js";
|
|
8
|
+
import { AgentRun } from "./session/run.js";
|
|
9
|
+
import { SessionHandle } from "./agent-session-entry.js";
|
|
10
|
+
import { Agent } from "./agent.js";
|
|
11
|
+
export { Agent, type AgentEvent, type AgentEventContext, type AgentEventListener, type AgentHost, type AgentInput, type AgentOptions, type AgentPlugin, type AgentRun, type AgentToolChoice, type AssistantReasoning, type AssistantText, type CommitResult, type ExpectedSessionVersion, type RuntimeCreateLlmOptions, type RuntimeInput, type RuntimeLlm, type RuntimeLlmContext, type RuntimeLlmOutput, type RuntimeLlmOutputPart, type SessionHandle, type SessionInput, type SessionStore, type SessionStoreCommit, type StoredSession, type ToolCall, type ToolResult, type UserInput, type UserMessage, type UserMessageContent, type UserMessageContentPart, type UserMessageFileData, type UserMessageFilePart, type UserMessageImagePart, type UserMessageTextPart, type UserText, type UserTextContent, createLlm };
|
package/dist/index.js
CHANGED
|
@@ -1,8 +1,3 @@
|
|
|
1
1
|
import { createLlm } from "./llm.js";
|
|
2
|
-
import { definePlugin } from "./plugins/types.js";
|
|
3
2
|
import { Agent } from "./agent.js";
|
|
4
|
-
|
|
5
|
-
import { memory } from "./plugins/memory.js";
|
|
6
|
-
import { sessions } from "./plugins/sessions.js";
|
|
7
|
-
import "./plugins/index.js";
|
|
8
|
-
export { Agent, compaction, createLlm, definePlugin, memory, sessions };
|
|
3
|
+
export { Agent, createLlm };
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { ToolSet } from "ai";
|
|
2
|
+
|
|
3
|
+
//#region src/llm-tool-execution.d.ts
|
|
4
|
+
type RuntimeToolRetryPolicy = "idempotent" | "manual-recovery" | "pure";
|
|
5
|
+
interface RuntimeToolExecutionCheckpointMetadata {
|
|
6
|
+
readonly attempt: number;
|
|
7
|
+
readonly idempotencyKey: string;
|
|
8
|
+
readonly policy: RuntimeToolRetryPolicy;
|
|
9
|
+
readonly toolCallId: string;
|
|
10
|
+
readonly toolName: string;
|
|
11
|
+
}
|
|
12
|
+
interface RuntimeToolExecutionCheckpoint extends RuntimeToolExecutionCheckpointMetadata {
|
|
13
|
+
readonly input: unknown;
|
|
14
|
+
}
|
|
15
|
+
type RuntimeToolExecutionDecision = {
|
|
16
|
+
readonly status: "needs-recovery";
|
|
17
|
+
} | undefined;
|
|
18
|
+
interface RuntimeToolExecutionContext {
|
|
19
|
+
readonly afterTool?: (checkpoint: RuntimeToolExecutionCheckpoint & {
|
|
20
|
+
readonly output: unknown;
|
|
21
|
+
}) => Promise<void> | void;
|
|
22
|
+
readonly attempt: number;
|
|
23
|
+
readonly beforeTool?: (checkpoint: RuntimeToolExecutionCheckpoint) => Promise<RuntimeToolExecutionDecision> | RuntimeToolExecutionDecision;
|
|
24
|
+
readonly runId: string;
|
|
25
|
+
}
|
|
26
|
+
declare class ToolExecutionNeedsRecoveryError extends Error {
|
|
27
|
+
readonly idempotencyKey: string;
|
|
28
|
+
readonly status = "needs-recovery";
|
|
29
|
+
readonly toolCallId: string;
|
|
30
|
+
readonly toolName: string;
|
|
31
|
+
constructor(checkpoint: RuntimeToolExecutionCheckpointMetadata);
|
|
32
|
+
}
|
|
33
|
+
//#endregion
|
|
34
|
+
export { RuntimeToolExecutionCheckpoint, RuntimeToolExecutionCheckpointMetadata, RuntimeToolExecutionContext, RuntimeToolExecutionDecision, RuntimeToolRetryPolicy, ToolExecutionNeedsRecoveryError };
|
|
35
|
+
//# sourceMappingURL=llm-tool-execution.d.ts.map
|