@anvia/client 1.2.0 → 1.3.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/README.md +62 -70
- package/dist/chunk-63JX2QNP.js +86 -0
- package/dist/chunk-63JX2QNP.js.map +1 -0
- package/dist/{chunk-2KGXQZWA.js → chunk-HZZDET2H.js} +5 -83
- package/dist/chunk-HZZDET2H.js.map +1 -0
- package/dist/durable.d.ts +64 -0
- package/dist/durable.js +242 -0
- package/dist/durable.js.map +1 -0
- package/dist/index.js +2 -1
- package/dist/index.js.map +1 -1
- package/dist/transport/index.js +4 -2
- package/package.json +16 -5
- package/dist/chunk-2KGXQZWA.js.map +0 -1
package/dist/durable.js
ADDED
|
@@ -0,0 +1,242 @@
|
|
|
1
|
+
import {
|
|
2
|
+
readSseStream
|
|
3
|
+
} from "./chunk-63JX2QNP.js";
|
|
4
|
+
|
|
5
|
+
// src/durable.ts
|
|
6
|
+
import {
|
|
7
|
+
parseTaskSnapshot,
|
|
8
|
+
parseTaskGraph,
|
|
9
|
+
parseTaskPage,
|
|
10
|
+
parseTaskEvent
|
|
11
|
+
} from "@anvia/durable/protocol";
|
|
12
|
+
import {
|
|
13
|
+
parseDurableEvent,
|
|
14
|
+
parseDurableGraphSnapshot,
|
|
15
|
+
parseDurableGraphEvent,
|
|
16
|
+
parseDurableGraphPage,
|
|
17
|
+
parseDurableRunPage,
|
|
18
|
+
parseDurableSnapshot
|
|
19
|
+
} from "@anvia/durable/protocol";
|
|
20
|
+
var DurableHttpError = class extends Error {
|
|
21
|
+
constructor(status) {
|
|
22
|
+
super(`Durable request failed with HTTP ${status}.`);
|
|
23
|
+
this.status = status;
|
|
24
|
+
this.name = "DurableHttpError";
|
|
25
|
+
}
|
|
26
|
+
status;
|
|
27
|
+
};
|
|
28
|
+
var DurableClient = class {
|
|
29
|
+
constructor(options) {
|
|
30
|
+
this.options = options;
|
|
31
|
+
const endpoint = new URL(options.endpoint);
|
|
32
|
+
if (!["http:", "https:"].includes(endpoint.protocol) || endpoint.search || endpoint.hash || endpoint.username || endpoint.password)
|
|
33
|
+
throw new TypeError(
|
|
34
|
+
"endpoint must be an HTTP(S) base URL without credentials, query, or fragment."
|
|
35
|
+
);
|
|
36
|
+
this.endpoint = endpoint.href.replace(/\/$/, "");
|
|
37
|
+
}
|
|
38
|
+
options;
|
|
39
|
+
endpoint;
|
|
40
|
+
async submitTask(submission, options = {}) {
|
|
41
|
+
return parseTaskSnapshot(
|
|
42
|
+
await (await this.request("/tasks", "POST", submission, options)).json()
|
|
43
|
+
);
|
|
44
|
+
}
|
|
45
|
+
async taskSnapshot(id, options = {}) {
|
|
46
|
+
const snapshot = parseTaskSnapshot(
|
|
47
|
+
await (await this.request(this.taskPath(id), "GET", void 0, options)).json()
|
|
48
|
+
);
|
|
49
|
+
if (snapshot.task.id !== id) throw new TypeError("Snapshot belongs to another task.");
|
|
50
|
+
return snapshot;
|
|
51
|
+
}
|
|
52
|
+
async taskGraph(id, options = {}) {
|
|
53
|
+
const graph = parseTaskGraph(
|
|
54
|
+
await (await this.request(`${this.taskPath(id)}/graph`, "GET", void 0, options)).json()
|
|
55
|
+
);
|
|
56
|
+
if (!graph.nodes.some((task) => task.id === id))
|
|
57
|
+
throw new TypeError("Graph belongs to another task.");
|
|
58
|
+
return graph;
|
|
59
|
+
}
|
|
60
|
+
async listTasks(filter, options = {}) {
|
|
61
|
+
const query = new URLSearchParams();
|
|
62
|
+
for (const [key, value] of Object.entries(filter))
|
|
63
|
+
if (value !== void 0) query.set(key, String(value));
|
|
64
|
+
return parseTaskPage(
|
|
65
|
+
await (await this.request(`/tasks?${query}`, "GET", void 0, options)).json()
|
|
66
|
+
);
|
|
67
|
+
}
|
|
68
|
+
async signalTask(id, name, requestId, value, options = {}) {
|
|
69
|
+
await this.request(`${this.taskPath(id)}/signal`, "POST", { name, requestId, value }, options);
|
|
70
|
+
}
|
|
71
|
+
async resolveEffect(id, key, value, options = {}) {
|
|
72
|
+
await this.request(`${this.taskPath(id)}/resolve-effect`, "POST", { key, value }, options);
|
|
73
|
+
}
|
|
74
|
+
async retryTask(id, options = {}) {
|
|
75
|
+
await this.request(`${this.taskPath(id)}/retry`, "POST", void 0, options);
|
|
76
|
+
}
|
|
77
|
+
async cancelTask(id, options = {}) {
|
|
78
|
+
await this.request(`${this.taskPath(id)}/cancel`, "POST", void 0, options);
|
|
79
|
+
}
|
|
80
|
+
async *streamTask(id, options = {}) {
|
|
81
|
+
let cursor = options.after ?? 0;
|
|
82
|
+
if (!Number.isSafeInteger(cursor) || cursor < 0) throw new TypeError("Invalid task cursor.");
|
|
83
|
+
const snapshot = await this.taskSnapshot(id, options);
|
|
84
|
+
const response = await this.request(
|
|
85
|
+
`${this.taskPath(id)}/events?after=${cursor}`,
|
|
86
|
+
"GET",
|
|
87
|
+
void 0,
|
|
88
|
+
options
|
|
89
|
+
);
|
|
90
|
+
if (!response.headers.get("content-type")?.startsWith("text/event-stream") || response.body === null)
|
|
91
|
+
throw new TypeError("Expected a task SSE response.");
|
|
92
|
+
for await (const value of readSseStream(response.body)) {
|
|
93
|
+
options.abortSignal?.throwIfAborted();
|
|
94
|
+
const event = parseTaskEvent(value);
|
|
95
|
+
if (event.rootId !== snapshot.task.rootId || event.sequence <= cursor)
|
|
96
|
+
throw new TypeError("Invalid task event order or root ID.");
|
|
97
|
+
cursor = event.sequence;
|
|
98
|
+
yield event;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
taskPath(id) {
|
|
102
|
+
if (id.trim().length === 0 || id === "." || id === "..") throw new TypeError("Invalid taskId.");
|
|
103
|
+
return `/tasks/${encodeURIComponent(id)}`;
|
|
104
|
+
}
|
|
105
|
+
async submitGraph(submission, options = {}) {
|
|
106
|
+
return parseDurableGraphSnapshot(
|
|
107
|
+
await (await this.request("/graphs", "POST", submission, options)).json()
|
|
108
|
+
);
|
|
109
|
+
}
|
|
110
|
+
async graphSnapshot(graphId, options = {}) {
|
|
111
|
+
const snapshot = parseDurableGraphSnapshot(
|
|
112
|
+
await (await this.request(this.graphPath(graphId), "GET", void 0, options)).json()
|
|
113
|
+
);
|
|
114
|
+
if (snapshot.id !== graphId) throw new TypeError("Snapshot belongs to another graph.");
|
|
115
|
+
return snapshot;
|
|
116
|
+
}
|
|
117
|
+
async listGraphs(filter, options = {}) {
|
|
118
|
+
const query = new URLSearchParams();
|
|
119
|
+
for (const [key, value] of Object.entries(filter))
|
|
120
|
+
if (value !== void 0) query.set(key, String(value));
|
|
121
|
+
return parseDurableGraphPage(
|
|
122
|
+
await (await this.request(`/graphs?${query}`, "GET", void 0, options)).json()
|
|
123
|
+
);
|
|
124
|
+
}
|
|
125
|
+
async cancelGraph(graphId, options = {}) {
|
|
126
|
+
await this.request(`${this.graphPath(graphId)}/cancel`, "POST", void 0, options);
|
|
127
|
+
}
|
|
128
|
+
async *streamGraph(graphId, options = {}) {
|
|
129
|
+
let cursor = options.after ?? 0;
|
|
130
|
+
if (!Number.isSafeInteger(cursor) || cursor < 0) throw new TypeError("Invalid graph cursor.");
|
|
131
|
+
const response = await this.request(
|
|
132
|
+
`${this.graphPath(graphId)}/events?after=${cursor}`,
|
|
133
|
+
"GET",
|
|
134
|
+
void 0,
|
|
135
|
+
options
|
|
136
|
+
);
|
|
137
|
+
if (!response.headers.get("content-type")?.startsWith("text/event-stream") || response.body === null)
|
|
138
|
+
throw new TypeError("Expected a graph SSE response.");
|
|
139
|
+
for await (const value of readSseStream(response.body)) {
|
|
140
|
+
options.abortSignal?.throwIfAborted();
|
|
141
|
+
const event = parseDurableGraphEvent(value);
|
|
142
|
+
if (event.graphId !== graphId || event.sequence <= cursor)
|
|
143
|
+
throw new TypeError("Invalid graph event order or ID.");
|
|
144
|
+
cursor = event.sequence;
|
|
145
|
+
yield event;
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
async submit(submission, options = {}) {
|
|
149
|
+
return parseDurableSnapshot(
|
|
150
|
+
await (await this.request(
|
|
151
|
+
"/runs",
|
|
152
|
+
"POST",
|
|
153
|
+
{ ...submission, ...options.enqueue === void 0 ? {} : { enqueue: options.enqueue } },
|
|
154
|
+
options
|
|
155
|
+
)).json()
|
|
156
|
+
);
|
|
157
|
+
}
|
|
158
|
+
async listRuns(filter, options = {}) {
|
|
159
|
+
const query = new URLSearchParams();
|
|
160
|
+
for (const [key, value] of Object.entries(filter))
|
|
161
|
+
if (value !== void 0) query.set(key, String(value));
|
|
162
|
+
return parseDurableRunPage(
|
|
163
|
+
await (await this.request(`/runs?${query}`, "GET", void 0, options)).json()
|
|
164
|
+
);
|
|
165
|
+
}
|
|
166
|
+
async snapshot(runId, options = {}) {
|
|
167
|
+
const snapshot = parseDurableSnapshot(
|
|
168
|
+
await (await this.request(this.path(runId), "GET", void 0, options)).json()
|
|
169
|
+
);
|
|
170
|
+
if (snapshot.run.id !== runId) throw new TypeError("Durable snapshot belongs to another run.");
|
|
171
|
+
return snapshot;
|
|
172
|
+
}
|
|
173
|
+
async *stream(runId, options = {}) {
|
|
174
|
+
let cursor = options.after ?? 0;
|
|
175
|
+
if (!Number.isSafeInteger(cursor) || cursor < 0) throw new TypeError("Invalid event cursor.");
|
|
176
|
+
const response = await this.request(
|
|
177
|
+
`${this.path(runId)}/events?after=${cursor}`,
|
|
178
|
+
"GET",
|
|
179
|
+
void 0,
|
|
180
|
+
options
|
|
181
|
+
);
|
|
182
|
+
if (!response.headers.get("content-type")?.startsWith("text/event-stream") || response.body === null)
|
|
183
|
+
throw new TypeError("Expected a durable SSE response.");
|
|
184
|
+
for await (const value of readSseStream(response.body)) {
|
|
185
|
+
options.abortSignal?.throwIfAborted();
|
|
186
|
+
const event = parseDurableEvent(value);
|
|
187
|
+
if (event.runId !== runId || event.sequence <= cursor)
|
|
188
|
+
throw new TypeError("Invalid durable event order or run ID.");
|
|
189
|
+
cursor = event.sequence;
|
|
190
|
+
yield event;
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
async respond(runId, interactionId, response, options = {}) {
|
|
194
|
+
await this.request(`${this.path(runId)}/respond`, "POST", { interactionId, response }, options);
|
|
195
|
+
}
|
|
196
|
+
async resolveTool(runId, operationId, output, options = {}) {
|
|
197
|
+
await this.request(
|
|
198
|
+
`${this.path(runId)}/resolve-tool`,
|
|
199
|
+
"POST",
|
|
200
|
+
{ operationId, output },
|
|
201
|
+
options
|
|
202
|
+
);
|
|
203
|
+
}
|
|
204
|
+
async retry(runId, options = {}) {
|
|
205
|
+
await this.request(`${this.path(runId)}/retry`, "POST", void 0, options);
|
|
206
|
+
}
|
|
207
|
+
async cancel(runId, options = {}) {
|
|
208
|
+
await this.request(`${this.path(runId)}/cancel`, "POST", void 0, options);
|
|
209
|
+
}
|
|
210
|
+
graphPath(id) {
|
|
211
|
+
if (id.trim().length === 0 || id === "." || id === "..")
|
|
212
|
+
throw new TypeError("Invalid graphId.");
|
|
213
|
+
return `/graphs/${encodeURIComponent(id)}`;
|
|
214
|
+
}
|
|
215
|
+
path(id) {
|
|
216
|
+
if (id.trim().length === 0) throw new TypeError("runId must not be empty.");
|
|
217
|
+
return `/runs/${encodeURIComponent(id)}`;
|
|
218
|
+
}
|
|
219
|
+
async request(path, method, body, options) {
|
|
220
|
+
options.abortSignal?.throwIfAborted();
|
|
221
|
+
const supplied = typeof this.options.headers === "function" ? await this.options.headers() : this.options.headers;
|
|
222
|
+
const headers = new Headers(supplied);
|
|
223
|
+
if (body !== void 0) headers.set("content-type", "application/json");
|
|
224
|
+
const response = await (this.options.fetch ?? globalThis.fetch)(`${this.endpoint}${path}`, {
|
|
225
|
+
method,
|
|
226
|
+
headers,
|
|
227
|
+
...body === void 0 ? {} : { body: JSON.stringify(body) },
|
|
228
|
+
...options.abortSignal === void 0 ? {} : { signal: options.abortSignal },
|
|
229
|
+
...this.options.credentials === void 0 ? {} : { credentials: this.options.credentials }
|
|
230
|
+
});
|
|
231
|
+
if (!response.ok) {
|
|
232
|
+
await response.body?.cancel();
|
|
233
|
+
throw new DurableHttpError(response.status);
|
|
234
|
+
}
|
|
235
|
+
return response;
|
|
236
|
+
}
|
|
237
|
+
};
|
|
238
|
+
export {
|
|
239
|
+
DurableClient,
|
|
240
|
+
DurableHttpError
|
|
241
|
+
};
|
|
242
|
+
//# sourceMappingURL=durable.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/durable.ts"],"sourcesContent":["import {\n parseTaskSnapshot,\n parseTaskGraph,\n parseTaskPage,\n parseTaskEvent,\n type TaskSubmission,\n type TaskSnapshot,\n type TaskGraphSnapshot,\n type TaskPage,\n type TaskListOptions,\n type TaskEvent,\n} from \"@anvia/durable/protocol\";\nimport type { AgentInteractionResponse } from \"@anvia/core/agent/interactions\";\nimport type { JsonValue, ToolResultOutput } from \"@anvia/core/completion\";\nimport {\n parseDurableEvent,\n parseDurableGraphSnapshot,\n parseDurableGraphEvent,\n parseDurableGraphPage,\n type DurableGraphSubmission,\n type DurableGraphSnapshot,\n type DurableGraphListOptions,\n type DurableGraphPage,\n type DurableGraphEvent,\n parseDurableRunPage,\n parseDurableSnapshot,\n type DurableEvent,\n type DurableListOptions,\n type DurableRunPage,\n type DurableSnapshot,\n type DurableSubmission,\n type DurableSubmitOptions,\n} from \"@anvia/durable/protocol\";\nimport { readSseStream } from \"./transport/streams\";\n\nexport type DurableRequestOptions = { abortSignal?: AbortSignal };\nexport type DurableClientOptions = {\n /** Absolute HTTP(S) URL matching the server handler's basePath. */\n endpoint: string;\n fetch?: typeof fetch;\n headers?: HeadersInit | (() => HeadersInit | Promise<HeadersInit>);\n credentials?: RequestCredentials;\n};\nexport class DurableHttpError extends Error {\n constructor(readonly status: number) {\n super(`Durable request failed with HTTP ${status}.`);\n this.name = \"DurableHttpError\";\n }\n}\n\n/** Browser-safe HTTP client. Reconnect explicitly using a snapshot or last applied event cursor. */\nexport class DurableClient {\n private readonly endpoint: string;\n constructor(private readonly options: DurableClientOptions) {\n const endpoint = new URL(options.endpoint);\n if (\n ![\"http:\", \"https:\"].includes(endpoint.protocol) ||\n endpoint.search ||\n endpoint.hash ||\n endpoint.username ||\n endpoint.password\n )\n throw new TypeError(\n \"endpoint must be an HTTP(S) base URL without credentials, query, or fragment.\",\n );\n this.endpoint = endpoint.href.replace(/\\/$/, \"\");\n }\n\n async submitTask(\n submission: TaskSubmission,\n options: DurableRequestOptions = {},\n ): Promise<TaskSnapshot> {\n return parseTaskSnapshot(\n await (await this.request(\"/tasks\", \"POST\", submission, options)).json(),\n );\n }\n async taskSnapshot(id: string, options: DurableRequestOptions = {}): Promise<TaskSnapshot> {\n const snapshot = parseTaskSnapshot(\n await (await this.request(this.taskPath(id), \"GET\", undefined, options)).json(),\n );\n if (snapshot.task.id !== id) throw new TypeError(\"Snapshot belongs to another task.\");\n return snapshot;\n }\n async taskGraph(id: string, options: DurableRequestOptions = {}): Promise<TaskGraphSnapshot> {\n const graph = parseTaskGraph(\n await (await this.request(`${this.taskPath(id)}/graph`, \"GET\", undefined, options)).json(),\n );\n if (!graph.nodes.some((task) => task.id === id))\n throw new TypeError(\"Graph belongs to another task.\");\n return graph;\n }\n async listTasks(\n filter: TaskListOptions & { sessionId: string },\n options: DurableRequestOptions = {},\n ): Promise<TaskPage> {\n const query = new URLSearchParams();\n for (const [key, value] of Object.entries(filter))\n if (value !== undefined) query.set(key, String(value));\n return parseTaskPage(\n await (await this.request(`/tasks?${query}`, \"GET\", undefined, options)).json(),\n );\n }\n async signalTask(\n id: string,\n name: string,\n requestId: string,\n value: JsonValue,\n options: DurableRequestOptions = {},\n ): Promise<void> {\n await this.request(`${this.taskPath(id)}/signal`, \"POST\", { name, requestId, value }, options);\n }\n async resolveEffect(\n id: string,\n key: string,\n value: JsonValue,\n options: DurableRequestOptions = {},\n ): Promise<void> {\n await this.request(`${this.taskPath(id)}/resolve-effect`, \"POST\", { key, value }, options);\n }\n async retryTask(id: string, options: DurableRequestOptions = {}): Promise<void> {\n await this.request(`${this.taskPath(id)}/retry`, \"POST\", undefined, options);\n }\n async cancelTask(id: string, options: DurableRequestOptions = {}): Promise<void> {\n await this.request(`${this.taskPath(id)}/cancel`, \"POST\", undefined, options);\n }\n async *streamTask(\n id: string,\n options: DurableRequestOptions & { after?: number } = {},\n ): AsyncIterable<TaskEvent> {\n let cursor = options.after ?? 0;\n if (!Number.isSafeInteger(cursor) || cursor < 0) throw new TypeError(\"Invalid task cursor.\");\n const snapshot = await this.taskSnapshot(id, options);\n const response = await this.request(\n `${this.taskPath(id)}/events?after=${cursor}`,\n \"GET\",\n undefined,\n options,\n );\n if (\n !response.headers.get(\"content-type\")?.startsWith(\"text/event-stream\") ||\n response.body === null\n )\n throw new TypeError(\"Expected a task SSE response.\");\n for await (const value of readSseStream<unknown>(response.body)) {\n options.abortSignal?.throwIfAborted();\n const event = parseTaskEvent(value);\n if (event.rootId !== snapshot.task.rootId || event.sequence <= cursor)\n throw new TypeError(\"Invalid task event order or root ID.\");\n cursor = event.sequence;\n yield event;\n }\n }\n private taskPath(id: string): string {\n if (id.trim().length === 0 || id === \".\" || id === \"..\") throw new TypeError(\"Invalid taskId.\");\n return `/tasks/${encodeURIComponent(id)}`;\n }\n\n async submitGraph(\n submission: DurableGraphSubmission,\n options: DurableRequestOptions = {},\n ): Promise<DurableGraphSnapshot> {\n return parseDurableGraphSnapshot(\n await (await this.request(\"/graphs\", \"POST\", submission, options)).json(),\n );\n }\n async graphSnapshot(\n graphId: string,\n options: DurableRequestOptions = {},\n ): Promise<DurableGraphSnapshot> {\n const snapshot = parseDurableGraphSnapshot(\n await (await this.request(this.graphPath(graphId), \"GET\", undefined, options)).json(),\n );\n if (snapshot.id !== graphId) throw new TypeError(\"Snapshot belongs to another graph.\");\n return snapshot;\n }\n async listGraphs(\n filter: DurableGraphListOptions & { sessionId: string },\n options: DurableRequestOptions = {},\n ): Promise<DurableGraphPage> {\n const query = new URLSearchParams();\n for (const [key, value] of Object.entries(filter))\n if (value !== undefined) query.set(key, String(value));\n return parseDurableGraphPage(\n await (await this.request(`/graphs?${query}`, \"GET\", undefined, options)).json(),\n );\n }\n async cancelGraph(graphId: string, options: DurableRequestOptions = {}): Promise<void> {\n await this.request(`${this.graphPath(graphId)}/cancel`, \"POST\", undefined, options);\n }\n async *streamGraph(\n graphId: string,\n options: DurableRequestOptions & { after?: number } = {},\n ): AsyncIterable<DurableGraphEvent> {\n let cursor = options.after ?? 0;\n if (!Number.isSafeInteger(cursor) || cursor < 0) throw new TypeError(\"Invalid graph cursor.\");\n const response = await this.request(\n `${this.graphPath(graphId)}/events?after=${cursor}`,\n \"GET\",\n undefined,\n options,\n );\n if (\n !response.headers.get(\"content-type\")?.startsWith(\"text/event-stream\") ||\n response.body === null\n )\n throw new TypeError(\"Expected a graph SSE response.\");\n for await (const value of readSseStream<unknown>(response.body)) {\n options.abortSignal?.throwIfAborted();\n const event = parseDurableGraphEvent(value);\n if (event.graphId !== graphId || event.sequence <= cursor)\n throw new TypeError(\"Invalid graph event order or ID.\");\n cursor = event.sequence;\n yield event;\n }\n }\n\n async submit(\n submission: DurableSubmission,\n options: DurableSubmitOptions & DurableRequestOptions = {},\n ): Promise<DurableSnapshot> {\n return parseDurableSnapshot(\n await (\n await this.request(\n \"/runs\",\n \"POST\",\n { ...submission, ...(options.enqueue === undefined ? {} : { enqueue: options.enqueue }) },\n options,\n )\n ).json(),\n );\n }\n async listRuns(\n filter: DurableListOptions & { sessionId: string },\n options: DurableRequestOptions = {},\n ): Promise<DurableRunPage> {\n const query = new URLSearchParams();\n for (const [key, value] of Object.entries(filter))\n if (value !== undefined) query.set(key, String(value));\n return parseDurableRunPage(\n await (await this.request(`/runs?${query}`, \"GET\", undefined, options)).json(),\n );\n }\n async snapshot(runId: string, options: DurableRequestOptions = {}): Promise<DurableSnapshot> {\n const snapshot = parseDurableSnapshot(\n await (await this.request(this.path(runId), \"GET\", undefined, options)).json(),\n );\n if (snapshot.run.id !== runId) throw new TypeError(\"Durable snapshot belongs to another run.\");\n return snapshot;\n }\n async *stream(\n runId: string,\n options: DurableRequestOptions & { after?: number } = {},\n ): AsyncIterable<DurableEvent> {\n let cursor = options.after ?? 0;\n if (!Number.isSafeInteger(cursor) || cursor < 0) throw new TypeError(\"Invalid event cursor.\");\n const response = await this.request(\n `${this.path(runId)}/events?after=${cursor}`,\n \"GET\",\n undefined,\n options,\n );\n if (\n !response.headers.get(\"content-type\")?.startsWith(\"text/event-stream\") ||\n response.body === null\n )\n throw new TypeError(\"Expected a durable SSE response.\");\n for await (const value of readSseStream<unknown>(response.body)) {\n options.abortSignal?.throwIfAborted();\n const event = parseDurableEvent(value);\n if (event.runId !== runId || event.sequence <= cursor)\n throw new TypeError(\"Invalid durable event order or run ID.\");\n cursor = event.sequence;\n yield event;\n }\n }\n async respond(\n runId: string,\n interactionId: string,\n response: AgentInteractionResponse,\n options: DurableRequestOptions = {},\n ): Promise<void> {\n await this.request(`${this.path(runId)}/respond`, \"POST\", { interactionId, response }, options);\n }\n async resolveTool(\n runId: string,\n operationId: string,\n output: ToolResultOutput,\n options: DurableRequestOptions = {},\n ): Promise<void> {\n await this.request(\n `${this.path(runId)}/resolve-tool`,\n \"POST\",\n { operationId, output },\n options,\n );\n }\n async retry(runId: string, options: DurableRequestOptions = {}): Promise<void> {\n await this.request(`${this.path(runId)}/retry`, \"POST\", undefined, options);\n }\n async cancel(runId: string, options: DurableRequestOptions = {}): Promise<void> {\n await this.request(`${this.path(runId)}/cancel`, \"POST\", undefined, options);\n }\n\n private graphPath(id: string): string {\n if (id.trim().length === 0 || id === \".\" || id === \"..\")\n throw new TypeError(\"Invalid graphId.\");\n return `/graphs/${encodeURIComponent(id)}`;\n }\n private path(id: string): string {\n if (id.trim().length === 0) throw new TypeError(\"runId must not be empty.\");\n return `/runs/${encodeURIComponent(id)}`;\n }\n private async request(\n path: string,\n method: string,\n body: unknown,\n options: DurableRequestOptions,\n ): Promise<Response> {\n options.abortSignal?.throwIfAborted();\n const supplied =\n typeof this.options.headers === \"function\"\n ? await this.options.headers()\n : this.options.headers;\n const headers = new Headers(supplied);\n if (body !== undefined) headers.set(\"content-type\", \"application/json\");\n const response = await (this.options.fetch ?? globalThis.fetch)(`${this.endpoint}${path}`, {\n method,\n headers,\n ...(body === undefined ? {} : { body: JSON.stringify(body) }),\n ...(options.abortSignal === undefined ? {} : { signal: options.abortSignal }),\n ...(this.options.credentials === undefined ? {} : { credentials: this.options.credentials }),\n });\n if (!response.ok) {\n await response.body?.cancel();\n throw new DurableHttpError(response.status);\n }\n return response;\n }\n}\n"],"mappings":";;;;;AAAA;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OAOK;AAGP;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EAMA;AAAA,EACA;AAAA,OAOK;AAWA,IAAM,mBAAN,cAA+B,MAAM;AAAA,EAC1C,YAAqB,QAAgB;AACnC,UAAM,oCAAoC,MAAM,GAAG;AADhC;AAEnB,SAAK,OAAO;AAAA,EACd;AAAA,EAHqB;AAIvB;AAGO,IAAM,gBAAN,MAAoB;AAAA,EAEzB,YAA6B,SAA+B;AAA/B;AAC3B,UAAM,WAAW,IAAI,IAAI,QAAQ,QAAQ;AACzC,QACE,CAAC,CAAC,SAAS,QAAQ,EAAE,SAAS,SAAS,QAAQ,KAC/C,SAAS,UACT,SAAS,QACT,SAAS,YACT,SAAS;AAET,YAAM,IAAI;AAAA,QACR;AAAA,MACF;AACF,SAAK,WAAW,SAAS,KAAK,QAAQ,OAAO,EAAE;AAAA,EACjD;AAAA,EAb6B;AAAA,EADZ;AAAA,EAgBjB,MAAM,WACJ,YACA,UAAiC,CAAC,GACX;AACvB,WAAO;AAAA,MACL,OAAO,MAAM,KAAK,QAAQ,UAAU,QAAQ,YAAY,OAAO,GAAG,KAAK;AAAA,IACzE;AAAA,EACF;AAAA,EACA,MAAM,aAAa,IAAY,UAAiC,CAAC,GAA0B;AACzF,UAAM,WAAW;AAAA,MACf,OAAO,MAAM,KAAK,QAAQ,KAAK,SAAS,EAAE,GAAG,OAAO,QAAW,OAAO,GAAG,KAAK;AAAA,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