@converge.ai/a13n 0.0.93 → 0.0.98
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 +87 -61
- package/THIRD_PARTY_NOTICES.txt +83 -0
- package/dist/client.d.ts +1 -1
- package/dist/client.js +2 -2
- package/dist/client.js.map +1 -1
- package/dist/dispatch.d.ts +6 -0
- package/dist/dispatch.d.ts.map +1 -0
- package/dist/dispatch.js +19 -0
- package/dist/dispatch.js.map +1 -0
- package/dist/errors.d.ts +23 -1
- package/dist/errors.d.ts.map +1 -1
- package/dist/errors.js +38 -5
- package/dist/errors.js.map +1 -1
- package/dist/index.d.ts +7 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -1
- package/dist/index.js.map +1 -1
- package/dist/resources/base.d.ts.map +1 -1
- package/dist/resources/base.js +5 -2
- package/dist/resources/base.js.map +1 -1
- package/dist/resources/generated.d.ts +3638 -1221
- package/dist/resources/generated.d.ts.map +1 -1
- package/dist/resources/generated.js +2312 -495
- package/dist/resources/generated.js.map +1 -1
- package/dist/resources/interaction.d.ts +1 -7
- package/dist/resources/interaction.d.ts.map +1 -1
- package/dist/resources/interaction.js +2 -30
- package/dist/resources/interaction.js.map +1 -1
- package/dist/schema.d.ts +15555 -6210
- package/dist/schema.d.ts.map +1 -1
- package/dist/semantic.d.ts +107 -119
- package/dist/semantic.d.ts.map +1 -1
- package/dist/semantic.js +461 -168
- package/dist/semantic.js.map +1 -1
- package/dist/session-stream-schema.d.ts +1851 -0
- package/dist/session-stream-schema.d.ts.map +1 -0
- package/dist/session-stream-schema.js +2 -0
- package/dist/session-stream-schema.js.map +1 -0
- package/dist/session-stream-validation.d.ts +129 -0
- package/dist/session-stream-validation.d.ts.map +1 -0
- package/dist/session-stream-validation.js +30324 -0
- package/dist/session-stream-validation.js.map +1 -0
- package/dist/streams/session-stream.d.ts +12 -0
- package/dist/streams/session-stream.d.ts.map +1 -0
- package/dist/streams/session-stream.js +36 -0
- package/dist/streams/session-stream.js.map +1 -0
- package/dist/streams/sse.d.ts +5 -1
- package/dist/streams/sse.d.ts.map +1 -1
- package/dist/streams/sse.js +11 -4
- package/dist/streams/sse.js.map +1 -1
- package/dist/streams/thread-stream.d.ts +8 -2
- package/dist/streams/thread-stream.d.ts.map +1 -1
- package/dist/streams/thread-stream.js +4 -2
- package/dist/streams/thread-stream.js.map +1 -1
- package/dist/transport.d.ts.map +1 -1
- package/dist/transport.js +3 -0
- package/dist/transport.js.map +1 -1
- package/dist/workflow.d.ts +117 -0
- package/dist/workflow.d.ts.map +1 -0
- package/dist/workflow.js +90 -0
- package/dist/workflow.js.map +1 -0
- package/dist/workspace-scope.d.ts.map +1 -1
- package/dist/workspace-scope.js +132 -0
- package/dist/workspace-scope.js.map +1 -1
- package/openapi.json +35484 -11086
- package/package.json +5 -1
package/dist/semantic.js
CHANGED
|
@@ -1,8 +1,43 @@
|
|
|
1
|
-
import { ProtocolError } from "./errors.js";
|
|
2
|
-
import { observation
|
|
1
|
+
import { isServiceRejection, isRecord, PreviewInterruptedError, ProtocolError, SubmissionUnknownError, } from "./errors.js";
|
|
2
|
+
import { observation } from "./resources/interaction.js";
|
|
3
3
|
import { abortable, delay } from "./transport.js";
|
|
4
|
+
import { trackDispatch } from "./dispatch.js";
|
|
4
5
|
import { threadStream } from "./streams/thread-stream.js";
|
|
5
|
-
|
|
6
|
+
import { messagePayload, resumePayload, sendFields, startFields, submissionKey, } from "./workflow.js";
|
|
7
|
+
async function mutation(operation, identities, options, submit) {
|
|
8
|
+
const key = submissionKey(options.idempotencyKey);
|
|
9
|
+
const signal = AbortSignal.any(options.signal ? [options.signal] : []);
|
|
10
|
+
const dispatch = trackDispatch(signal);
|
|
11
|
+
try {
|
|
12
|
+
signal.throwIfAborted();
|
|
13
|
+
return { value: await submit({ idempotencyKey: key, signal }), key };
|
|
14
|
+
}
|
|
15
|
+
catch (cause) {
|
|
16
|
+
if (!dispatch.attempted || isServiceRejection(cause))
|
|
17
|
+
throw cause;
|
|
18
|
+
throw new SubmissionUnknownError(key, operation, Object.freeze({ ...identities }), cause);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
function submittedIdentity(receipt, threadId) {
|
|
22
|
+
const value = receipt.data;
|
|
23
|
+
if (!isRecord(value) ||
|
|
24
|
+
!isRecord(value.thread) ||
|
|
25
|
+
!isRecord(value.entry) ||
|
|
26
|
+
typeof value.thread.id !== "string" ||
|
|
27
|
+
!value.thread.id ||
|
|
28
|
+
typeof value.entry.id !== "string" ||
|
|
29
|
+
!value.entry.id ||
|
|
30
|
+
value.entry.thread_id !== value.thread.id ||
|
|
31
|
+
(threadId !== undefined && value.thread.id !== threadId) ||
|
|
32
|
+
!(value.run === null ||
|
|
33
|
+
(isRecord(value.run) &&
|
|
34
|
+
typeof value.run.id === "string" &&
|
|
35
|
+
value.run.id &&
|
|
36
|
+
value.run.thread_id === value.thread.id)))
|
|
37
|
+
throw new ProtocolError("Service returned an invalid submission receipt identity.");
|
|
38
|
+
return { threadId: value.thread.id, entryId: value.entry.id };
|
|
39
|
+
}
|
|
40
|
+
/** The Entry was settled without incorporation; this is not a failed Run. */
|
|
6
41
|
export class SubmissionDispositionError extends Error {
|
|
7
42
|
snapshot;
|
|
8
43
|
name = "SubmissionDispositionError";
|
|
@@ -17,7 +52,7 @@ export class SubmissionDispositionError extends Error {
|
|
|
17
52
|
this.status = snapshot.data.status;
|
|
18
53
|
}
|
|
19
54
|
}
|
|
20
|
-
/**
|
|
55
|
+
/** Authoritative exact-Run HTTP evidence, never reconstructed from Items or preview. */
|
|
21
56
|
export class RunOutcome {
|
|
22
57
|
run;
|
|
23
58
|
snapshot;
|
|
@@ -31,240 +66,480 @@ export class RunOutcome {
|
|
|
31
66
|
get output() {
|
|
32
67
|
return this.snapshot.data.output;
|
|
33
68
|
}
|
|
69
|
+
get text() {
|
|
70
|
+
return typeof this.output === "string" ? this.output : undefined;
|
|
71
|
+
}
|
|
34
72
|
get pending() {
|
|
35
73
|
return this.snapshot.data.pending;
|
|
36
74
|
}
|
|
75
|
+
get pendingApprovals() {
|
|
76
|
+
return this.pending?.approvals ?? [];
|
|
77
|
+
}
|
|
78
|
+
get pendingCalls() {
|
|
79
|
+
return this.pending?.calls ?? [];
|
|
80
|
+
}
|
|
37
81
|
get failure() {
|
|
38
82
|
return this.snapshot.data.failure;
|
|
39
83
|
}
|
|
84
|
+
get threadId() {
|
|
85
|
+
return this.snapshot.data.thread_id;
|
|
86
|
+
}
|
|
87
|
+
get runId() {
|
|
88
|
+
return this.snapshot.data.id;
|
|
89
|
+
}
|
|
40
90
|
}
|
|
41
91
|
export class RunHandle {
|
|
42
92
|
id;
|
|
43
93
|
resources;
|
|
44
94
|
shutdown;
|
|
45
|
-
|
|
46
|
-
constructor(id, resources, shutdown,
|
|
47
|
-
/** Original resume receipt when this handle was returned from resume(). */
|
|
48
|
-
receipt) {
|
|
95
|
+
constructor(id, resources, shutdown) {
|
|
49
96
|
this.id = id;
|
|
50
97
|
this.resources = resources;
|
|
51
98
|
this.shutdown = shutdown;
|
|
52
|
-
this.receipt = receipt;
|
|
53
99
|
}
|
|
54
|
-
get(options = {}) {
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
}, this.shutdown, options);
|
|
65
|
-
|
|
100
|
+
async get(options = {}) {
|
|
101
|
+
const result = await this.resources.runs
|
|
102
|
+
.ref(this.id)
|
|
103
|
+
.get({ ...options, query: { view: "detailed" } });
|
|
104
|
+
if (("view" in result.data && result.data.view === "compact") ||
|
|
105
|
+
"representation" in result.data)
|
|
106
|
+
throw new ProtocolError("Expected detailed Run response");
|
|
107
|
+
return { ...result, data: result.data };
|
|
108
|
+
}
|
|
109
|
+
observe(options = {}) {
|
|
110
|
+
return new Interaction({ origin: "exact_run", runId: this.id }, this.resources, this.shutdown, undefined, undefined, options);
|
|
111
|
+
}
|
|
112
|
+
async result(options = {}) {
|
|
113
|
+
return this.observe(options).result();
|
|
66
114
|
}
|
|
67
|
-
/** Committed Run Items; stream deltas are not a replacement for this readback. */
|
|
68
115
|
items(options = {}) {
|
|
69
116
|
return this.resources.runs.ref(this.id).items.get(options);
|
|
70
117
|
}
|
|
71
118
|
interrupt(options = {}) {
|
|
72
119
|
return this.resources.runs.ref(this.id).interrupt(options);
|
|
73
120
|
}
|
|
74
|
-
/**
|
|
75
|
-
async resume(
|
|
76
|
-
const
|
|
77
|
-
|
|
78
|
-
.
|
|
79
|
-
|
|
121
|
+
/** Atomic complete answer map; Service alone validates coverage. Returns the exact successor. */
|
|
122
|
+
async resume(answers, options = {}) {
|
|
123
|
+
const request = resumePayload(answers, options.input);
|
|
124
|
+
const { value: receipt, key } = await mutation("resume", { runId: this.id }, options, async (requestOptions) => {
|
|
125
|
+
const result = await this.resources.runs
|
|
126
|
+
.ref(this.id)
|
|
127
|
+
.resume(request, requestOptions);
|
|
128
|
+
const value = result.data;
|
|
129
|
+
if (!isRecord(value) ||
|
|
130
|
+
typeof value.id !== "string" ||
|
|
131
|
+
!value.id ||
|
|
132
|
+
value.id === this.id ||
|
|
133
|
+
typeof value.thread_id !== "string" ||
|
|
134
|
+
!value.thread_id)
|
|
135
|
+
throw new ProtocolError("Service returned an invalid successor receipt identity.");
|
|
136
|
+
return result;
|
|
137
|
+
});
|
|
138
|
+
return new Interaction({
|
|
139
|
+
origin: "resumed",
|
|
140
|
+
runId: receipt.data.id,
|
|
141
|
+
threadId: receipt.data.thread_id,
|
|
142
|
+
idempotencyKey: key,
|
|
143
|
+
}, this.resources, this.shutdown, options.signal, receipt);
|
|
80
144
|
}
|
|
81
145
|
}
|
|
82
146
|
export class ThreadHandle {
|
|
83
147
|
id;
|
|
84
148
|
resources;
|
|
85
|
-
|
|
149
|
+
shutdown;
|
|
150
|
+
constructor(id, resources, shutdown) {
|
|
86
151
|
this.id = id;
|
|
87
152
|
this.resources = resources;
|
|
153
|
+
this.shutdown = shutdown;
|
|
88
154
|
}
|
|
89
155
|
get(options = {}) {
|
|
90
156
|
return this.resources.threads.ref(this.id).get(options);
|
|
91
157
|
}
|
|
92
158
|
entry(id) {
|
|
93
|
-
return new EntryHandle(id, this.id, this.resources);
|
|
159
|
+
return new EntryHandle(id, this.id, this.resources, this.shutdown);
|
|
94
160
|
}
|
|
95
161
|
}
|
|
96
162
|
export class EntryHandle {
|
|
97
163
|
id;
|
|
98
164
|
threadId;
|
|
99
165
|
resources;
|
|
100
|
-
|
|
166
|
+
shutdown;
|
|
167
|
+
constructor(id, threadId, resources, shutdown) {
|
|
101
168
|
this.id = id;
|
|
102
169
|
this.threadId = threadId;
|
|
103
170
|
this.resources = resources;
|
|
171
|
+
this.shutdown = shutdown;
|
|
104
172
|
}
|
|
105
|
-
get(options = {}) {
|
|
106
|
-
|
|
173
|
+
async get(options = {}) {
|
|
174
|
+
const result = await this.resources.threads
|
|
107
175
|
.ref(this.threadId)
|
|
108
176
|
.inbox.ref(this.id)
|
|
109
|
-
.get(options);
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
resources;
|
|
115
|
-
shutdown;
|
|
116
|
-
thread;
|
|
117
|
-
entry;
|
|
118
|
-
run;
|
|
119
|
-
constructor(receipt, resources, shutdown) {
|
|
120
|
-
this.receipt = receipt;
|
|
121
|
-
this.resources = resources;
|
|
122
|
-
this.shutdown = shutdown;
|
|
123
|
-
this.thread = new ThreadHandle(receipt.data.thread.id, resources);
|
|
124
|
-
this.entry = this.thread.entry(receipt.data.entry.id);
|
|
125
|
-
this.run = receipt.data.run
|
|
126
|
-
? new RunHandle(receipt.data.run.id, resources, shutdown)
|
|
127
|
-
: null;
|
|
177
|
+
.get({ ...options, query: { view: "detailed" } });
|
|
178
|
+
if (("view" in result.data && result.data.view === "compact") ||
|
|
179
|
+
"representation" in result.data)
|
|
180
|
+
throw new ProtocolError("Expected detailed Entry response");
|
|
181
|
+
return { ...result, data: result.data };
|
|
128
182
|
}
|
|
129
|
-
/**
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
try {
|
|
133
|
-
while (true) {
|
|
134
|
-
watch.signal.throwIfAborted();
|
|
135
|
-
const entry = await abortable(this.entry.get({ signal: watch.signal }), watch.signal);
|
|
136
|
-
if (entry.data.id !== this.entry.id ||
|
|
137
|
-
entry.data.thread_id !== this.thread.id)
|
|
138
|
-
throw new ProtocolError("Service returned a different submission entry.");
|
|
139
|
-
if (entry.data.status === "failed" || entry.data.status === "withdrawn")
|
|
140
|
-
throw new SubmissionDispositionError(entry);
|
|
141
|
-
if (entry.data.status === "consumed") {
|
|
142
|
-
if (!entry.data.assigned_run_id)
|
|
143
|
-
throw new ProtocolError("Consumed submission has no incorporating Run ID.");
|
|
144
|
-
const run = new RunHandle(entry.data.assigned_run_id, this.resources, this.shutdown);
|
|
145
|
-
onBound?.(run);
|
|
146
|
-
while (true) {
|
|
147
|
-
watch.signal.throwIfAborted();
|
|
148
|
-
const snapshot = await abortable(run.get({ signal: watch.signal }), watch.signal);
|
|
149
|
-
if (snapshot.data.id !== run.id ||
|
|
150
|
-
snapshot.data.thread_id !== this.thread.id)
|
|
151
|
-
throw new ProtocolError("Service returned a different incorporating Run.");
|
|
152
|
-
if (["completed", "waiting", "failed", "cancelled"].includes(snapshot.data.status)) {
|
|
153
|
-
watch.signal.throwIfAborted();
|
|
154
|
-
return new RunOutcome(run, snapshot);
|
|
155
|
-
}
|
|
156
|
-
await delay(watch.pollIntervalMs, watch.signal);
|
|
157
|
-
}
|
|
158
|
-
}
|
|
159
|
-
// An assigned entry can return to pending at a seal; neither is incorporation.
|
|
160
|
-
await delay(watch.pollIntervalMs, watch.signal);
|
|
161
|
-
}
|
|
162
|
-
}
|
|
163
|
-
catch (error) {
|
|
164
|
-
if (watch.signal.aborted)
|
|
165
|
-
throw watch.signal.reason;
|
|
166
|
-
throw error;
|
|
167
|
-
}
|
|
168
|
-
finally {
|
|
169
|
-
watch.close();
|
|
170
|
-
}
|
|
183
|
+
/** Recover only an identity, without I/O or a fabricated receipt. */
|
|
184
|
+
interaction(options = {}) {
|
|
185
|
+
return new Interaction({ origin: "recovered_entry", threadId: this.threadId, entryId: this.id }, this.resources, this.shutdown, undefined, undefined, options);
|
|
171
186
|
}
|
|
172
187
|
}
|
|
173
|
-
/**
|
|
188
|
+
/** One finite exact-bound observation, shared by submission, resume and identity recovery. */
|
|
174
189
|
export class Interaction {
|
|
175
|
-
|
|
190
|
+
resources;
|
|
176
191
|
shutdown;
|
|
177
192
|
invocationSignal;
|
|
178
193
|
receipt;
|
|
179
|
-
|
|
194
|
+
initialOptions;
|
|
195
|
+
identity;
|
|
196
|
+
receiptRun;
|
|
180
197
|
entry;
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
198
|
+
threadHandle;
|
|
199
|
+
runHandle = null;
|
|
200
|
+
localAbort = new AbortController();
|
|
201
|
+
watch;
|
|
184
202
|
outcomePromise;
|
|
185
203
|
boundPromise;
|
|
204
|
+
reader;
|
|
205
|
+
binary;
|
|
186
206
|
streamAbort;
|
|
207
|
+
readerClose;
|
|
208
|
+
closePromise;
|
|
187
209
|
closed = false;
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
210
|
+
settled = false;
|
|
211
|
+
consumer = null;
|
|
212
|
+
rawUsed = false;
|
|
213
|
+
previewUsed = false;
|
|
214
|
+
previewFailure;
|
|
215
|
+
constructor(identity, resources, shutdown, invocationSignal, receipt, initialOptions = {}) {
|
|
216
|
+
this.resources = resources;
|
|
191
217
|
this.shutdown = shutdown;
|
|
192
218
|
this.invocationSignal = invocationSignal;
|
|
193
|
-
this.observer = new SubmissionObserver(receipt, interactionResources, shutdown);
|
|
194
219
|
this.receipt = receipt;
|
|
195
|
-
this.
|
|
196
|
-
this.
|
|
197
|
-
this.
|
|
220
|
+
this.initialOptions = initialOptions;
|
|
221
|
+
this.identity = Object.freeze({ ...identity });
|
|
222
|
+
this.threadHandle = identity.threadId
|
|
223
|
+
? new ThreadHandle(identity.threadId, resources, shutdown)
|
|
224
|
+
: null;
|
|
225
|
+
this.entry =
|
|
226
|
+
identity.entryId && identity.threadId
|
|
227
|
+
? new EntryHandle(identity.entryId, identity.threadId, resources, shutdown)
|
|
228
|
+
: null;
|
|
229
|
+
const provisional = identity.origin === "submitted" && receipt
|
|
230
|
+
? receipt.data.run
|
|
231
|
+
: null;
|
|
232
|
+
this.receiptRun = provisional
|
|
233
|
+
? new RunHandle(provisional.id, resources, shutdown)
|
|
234
|
+
: null;
|
|
235
|
+
}
|
|
236
|
+
get thread() {
|
|
237
|
+
return this.threadHandle;
|
|
238
|
+
}
|
|
239
|
+
/** Authoritative only after consumed Entry binding or a validated exact Run read. */
|
|
240
|
+
get run() {
|
|
241
|
+
return this.runHandle;
|
|
198
242
|
}
|
|
199
|
-
|
|
243
|
+
get idempotencyKey() {
|
|
244
|
+
return this.identity.idempotencyKey;
|
|
245
|
+
}
|
|
246
|
+
/** No SSE for result-only callers. First observation fixes one deadline for all consumers. */
|
|
200
247
|
result(options = {}) {
|
|
201
248
|
if (this.outcomePromise) {
|
|
202
249
|
if (Object.keys(options).length)
|
|
203
|
-
throw new TypeError("The interaction observation has already started; set options
|
|
250
|
+
throw new TypeError("The interaction observation has already started; set options before starting it.");
|
|
204
251
|
return this.outcomePromise;
|
|
205
252
|
}
|
|
206
253
|
if (this.closed)
|
|
207
|
-
return Promise.reject(this.
|
|
254
|
+
return Promise.reject(this.localAbort.signal.reason);
|
|
255
|
+
const settings = { ...this.initialOptions, ...options };
|
|
256
|
+
const signal = AbortSignal.any([
|
|
257
|
+
this.localAbort.signal,
|
|
258
|
+
...(this.invocationSignal ? [this.invocationSignal] : []),
|
|
259
|
+
...(this.initialOptions.signal ? [this.initialOptions.signal] : []),
|
|
260
|
+
...(options.signal ? [options.signal] : []),
|
|
261
|
+
]);
|
|
262
|
+
this.watch = observation(this.shutdown, { ...settings, signal });
|
|
208
263
|
let resolveBound;
|
|
209
264
|
let rejectBound;
|
|
210
265
|
this.boundPromise = new Promise((resolve, reject) => {
|
|
211
266
|
resolveBound = resolve;
|
|
212
267
|
rejectBound = reject;
|
|
213
268
|
});
|
|
214
|
-
// Result-only callers never await the binding promise.
|
|
215
269
|
void this.boundPromise.catch(() => undefined);
|
|
216
|
-
|
|
217
|
-
this.
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
this.outcomePromise = this.observer.wait({ ...options, signal }, resolveBound);
|
|
270
|
+
this.outcomePromise = this.poll(this.watch, resolveBound).finally(() => {
|
|
271
|
+
this.settled = true;
|
|
272
|
+
this.watch.close();
|
|
273
|
+
void this.stopReader().catch(() => undefined);
|
|
274
|
+
});
|
|
222
275
|
void this.outcomePromise.catch(rejectBound);
|
|
223
276
|
return this.outcomePromise;
|
|
224
277
|
}
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
278
|
+
async poll(watch, onBound) {
|
|
279
|
+
try {
|
|
280
|
+
let runId = this.identity.runId;
|
|
281
|
+
if (this.entry) {
|
|
282
|
+
while (!runId) {
|
|
283
|
+
watch.signal.throwIfAborted();
|
|
284
|
+
const snapshot = await abortable(this.entry.get({ signal: watch.signal }), watch.signal);
|
|
285
|
+
if (snapshot.data.id !== this.entry.id ||
|
|
286
|
+
snapshot.data.thread_id !== this.entry.threadId)
|
|
287
|
+
throw new ProtocolError("Service returned a different submission entry.");
|
|
288
|
+
if (snapshot.data.status === "failed" ||
|
|
289
|
+
snapshot.data.status === "withdrawn")
|
|
290
|
+
throw new SubmissionDispositionError(snapshot);
|
|
291
|
+
if (snapshot.data.status === "consumed") {
|
|
292
|
+
if (!snapshot.data.assigned_run_id)
|
|
293
|
+
throw new ProtocolError("Consumed submission has no incorporating Run ID.");
|
|
294
|
+
runId = snapshot.data.assigned_run_id;
|
|
295
|
+
this.runHandle = new RunHandle(runId, this.resources, this.shutdown);
|
|
296
|
+
onBound(this.runHandle);
|
|
297
|
+
}
|
|
298
|
+
else
|
|
299
|
+
await delay(watch.pollIntervalMs, watch.signal);
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
if (!runId)
|
|
303
|
+
throw new ProtocolError("Interaction has no exact Run or Entry identity.");
|
|
304
|
+
const run = this.runHandle ?? new RunHandle(runId, this.resources, this.shutdown);
|
|
305
|
+
while (true) {
|
|
306
|
+
watch.signal.throwIfAborted();
|
|
307
|
+
const snapshot = await abortable(run.get({ signal: watch.signal }), watch.signal);
|
|
308
|
+
if (snapshot.data.id !== runId ||
|
|
309
|
+
typeof snapshot.data.thread_id !== "string" ||
|
|
310
|
+
!snapshot.data.thread_id ||
|
|
311
|
+
(this.threadHandle &&
|
|
312
|
+
snapshot.data.thread_id !== this.threadHandle.id))
|
|
313
|
+
throw new ProtocolError("Service returned a different exact Run.");
|
|
314
|
+
if (!this.runHandle) {
|
|
315
|
+
this.runHandle = run;
|
|
316
|
+
this.threadHandle = new ThreadHandle(snapshot.data.thread_id, this.resources, this.shutdown);
|
|
317
|
+
onBound(run);
|
|
318
|
+
}
|
|
319
|
+
if (["completed", "waiting", "failed", "cancelled"].includes(snapshot.data.status)) {
|
|
320
|
+
watch.signal.throwIfAborted();
|
|
321
|
+
return new RunOutcome(run, snapshot);
|
|
322
|
+
}
|
|
323
|
+
await delay(watch.pollIntervalMs, watch.signal);
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
catch (cause) {
|
|
327
|
+
if (watch.signal.aborted)
|
|
328
|
+
throw watch.signal.reason;
|
|
329
|
+
throw cause;
|
|
330
|
+
}
|
|
233
331
|
}
|
|
234
|
-
async
|
|
235
|
-
if (this.iterated)
|
|
236
|
-
throw new TypeError("An interaction may be iterated only once.");
|
|
237
|
-
this.iterated = true;
|
|
238
|
-
if (this.closed)
|
|
239
|
-
return;
|
|
332
|
+
async nextEvent() {
|
|
240
333
|
const outcome = this.result();
|
|
241
334
|
const run = await this.boundPromise;
|
|
242
335
|
if (this.closed)
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
.
|
|
250
|
-
|
|
336
|
+
this.localAbort.signal.throwIfAborted();
|
|
337
|
+
if (this.settled) {
|
|
338
|
+
await outcome;
|
|
339
|
+
return { done: true, value: undefined };
|
|
340
|
+
}
|
|
341
|
+
if (!this.reader) {
|
|
342
|
+
this.streamAbort = new AbortController();
|
|
343
|
+
const signal = AbortSignal.any([
|
|
344
|
+
this.watch.signal,
|
|
345
|
+
this.streamAbort.signal,
|
|
346
|
+
]);
|
|
347
|
+
this.reader = threadStream(async (options) => {
|
|
348
|
+
const opening = this.resources.threads
|
|
349
|
+
.ref(this.threadHandle.id)
|
|
350
|
+
.stream.get(options);
|
|
351
|
+
// A late opening is still owned and must be closed after local cancellation.
|
|
352
|
+
void opening.then((binary) => {
|
|
353
|
+
if (signal.aborted)
|
|
354
|
+
void binary.close().catch(() => undefined);
|
|
355
|
+
}, () => undefined);
|
|
356
|
+
const binary = await abortable(opening, signal);
|
|
357
|
+
this.binary = binary;
|
|
358
|
+
return binary;
|
|
359
|
+
}, this.shutdown, { signal });
|
|
360
|
+
}
|
|
361
|
+
try {
|
|
362
|
+
while (true) {
|
|
363
|
+
const next = await this.reader.next();
|
|
364
|
+
if (this.closed)
|
|
365
|
+
this.localAbort.signal.throwIfAborted();
|
|
366
|
+
if (this.settled) {
|
|
367
|
+
await outcome;
|
|
368
|
+
return { done: true, value: undefined };
|
|
369
|
+
}
|
|
370
|
+
if (next.done)
|
|
371
|
+
return next;
|
|
372
|
+
if (next.value.frame.type !== "changed" &&
|
|
373
|
+
next.value.frame.data.run_id === run.id)
|
|
374
|
+
return next;
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
catch (cause) {
|
|
378
|
+
if (this.settled) {
|
|
379
|
+
await outcome;
|
|
380
|
+
return { done: true, value: undefined };
|
|
381
|
+
}
|
|
382
|
+
throw cause;
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
stopReader() {
|
|
386
|
+
this.streamAbort?.abort(new DOMException("Interaction reader stopped.", "AbortError"));
|
|
387
|
+
if (!this.readerClose && this.reader)
|
|
388
|
+
this.readerClose = (async () => {
|
|
389
|
+
// Cancel the actual transport read first, even if the consumer is paused at yield.
|
|
390
|
+
try {
|
|
391
|
+
await this.binary
|
|
392
|
+
?.close()
|
|
393
|
+
.catch((cause) => this.readerCancellation(cause));
|
|
394
|
+
}
|
|
395
|
+
finally {
|
|
396
|
+
await this.reader.return(undefined).catch((cause) => this.readerCancellation(cause));
|
|
397
|
+
}
|
|
398
|
+
})();
|
|
399
|
+
return this.readerClose ?? Promise.resolve();
|
|
400
|
+
}
|
|
401
|
+
readerCancellation(cause) {
|
|
402
|
+
for (const signal of [
|
|
403
|
+
this.watch?.signal,
|
|
404
|
+
this.localAbort.signal,
|
|
405
|
+
this.streamAbort?.signal,
|
|
406
|
+
this.shutdown,
|
|
407
|
+
]) {
|
|
408
|
+
if (signal?.aborted && cause === signal.reason)
|
|
409
|
+
return;
|
|
410
|
+
}
|
|
411
|
+
throw cause;
|
|
412
|
+
}
|
|
413
|
+
/** Joined local cleanup. Never interrupts or resumes remote execution. */
|
|
414
|
+
close() {
|
|
415
|
+
if (!this.closePromise) {
|
|
416
|
+
this.closed = true;
|
|
417
|
+
this.localAbort.abort(new DOMException("Interaction closed.", "AbortError"));
|
|
418
|
+
this.closePromise = (async () => {
|
|
419
|
+
try {
|
|
420
|
+
await this.stopReader();
|
|
421
|
+
}
|
|
422
|
+
finally {
|
|
423
|
+
await this.outcomePromise?.catch(() => undefined);
|
|
424
|
+
}
|
|
425
|
+
})();
|
|
426
|
+
}
|
|
427
|
+
return this.closePromise;
|
|
428
|
+
}
|
|
429
|
+
async *[Symbol.asyncIterator]() {
|
|
430
|
+
if (this.consumer || this.rawUsed)
|
|
431
|
+
throw new TypeError("Only one raw or text consumer may read an Interaction at a time.");
|
|
432
|
+
this.rawUsed = true;
|
|
433
|
+
this.consumer = "raw";
|
|
251
434
|
try {
|
|
252
|
-
|
|
253
|
-
|
|
435
|
+
while (true) {
|
|
436
|
+
const next = await this.nextEvent();
|
|
437
|
+
if (next.done)
|
|
438
|
+
return;
|
|
439
|
+
yield next.value;
|
|
440
|
+
}
|
|
441
|
+
}
|
|
442
|
+
finally {
|
|
443
|
+
this.consumer = null;
|
|
444
|
+
await this.close();
|
|
445
|
+
}
|
|
446
|
+
}
|
|
447
|
+
/** Lazy preview only; interruption is sticky and leaves result/sequential raw access open. */
|
|
448
|
+
async *textStream() {
|
|
449
|
+
if (this.previewFailure)
|
|
450
|
+
throw this.previewFailure;
|
|
451
|
+
if (this.consumer || this.previewUsed || this.rawUsed)
|
|
452
|
+
throw new TypeError("Only one preview consumer is allowed, before raw consumption.");
|
|
453
|
+
this.previewUsed = true;
|
|
454
|
+
this.consumer = "text";
|
|
455
|
+
const active = new Map();
|
|
456
|
+
let previous;
|
|
457
|
+
try {
|
|
458
|
+
while (true) {
|
|
459
|
+
const next = await this.nextEvent();
|
|
460
|
+
if (next.done)
|
|
461
|
+
return;
|
|
462
|
+
const frame = next.value.frame;
|
|
463
|
+
if (frame.type === "gap" || frame.type === "reset")
|
|
464
|
+
throw new PreviewInterruptedError(`Text preview interrupted by ${frame.type}.`);
|
|
465
|
+
if (frame.type === "boundary") {
|
|
466
|
+
if (previous &&
|
|
467
|
+
(frame.data.attempt !== previous.attempt ||
|
|
468
|
+
frame.data.sequence > previous.sequence))
|
|
469
|
+
throw new PreviewInterruptedError("Text preview boundary covers unseen Service deltas.");
|
|
470
|
+
// A first boundary establishes only a coordinate, never reconstructed message content.
|
|
471
|
+
previous ??= {
|
|
472
|
+
attempt: frame.data.attempt,
|
|
473
|
+
sequence: frame.data.sequence,
|
|
474
|
+
};
|
|
254
475
|
continue;
|
|
255
|
-
|
|
256
|
-
|
|
476
|
+
}
|
|
477
|
+
if (frame.type !== "delta")
|
|
478
|
+
continue;
|
|
479
|
+
// Service positions include child and hidden deltas; Redis and inner AG-UI coordinates do not.
|
|
480
|
+
// Late boundaries never regress the received delta high-water mark.
|
|
481
|
+
if (previous &&
|
|
482
|
+
(frame.data.attempt !== previous.attempt ||
|
|
483
|
+
frame.data.sequence !== previous.sequence + 1))
|
|
484
|
+
throw new PreviewInterruptedError("Text preview encountered a discontinuous Service delta position.");
|
|
485
|
+
previous = {
|
|
486
|
+
attempt: frame.data.attempt,
|
|
487
|
+
sequence: frame.data.sequence,
|
|
488
|
+
};
|
|
489
|
+
const event = frame.data.event;
|
|
490
|
+
if (event.subagentRunId !== undefined && event.subagentRunId !== null)
|
|
491
|
+
continue;
|
|
492
|
+
const hidden = isRecord(event.metadata) && event.metadata.display === false;
|
|
493
|
+
if (hidden &&
|
|
494
|
+
event.type !== "TEXT_MESSAGE_START" &&
|
|
495
|
+
event.type !== "TEXT_MESSAGE_END")
|
|
496
|
+
continue;
|
|
497
|
+
if (event.type === "CUSTOM" && event.name === "a13n.stream.fragment")
|
|
498
|
+
throw new PreviewInterruptedError("Text preview does not assemble root custom fragments.");
|
|
499
|
+
if (event.type === "TEXT_MESSAGE_START") {
|
|
500
|
+
if (typeof event.messageId !== "string" ||
|
|
501
|
+
typeof event.role !== "string" ||
|
|
502
|
+
active.has(event.messageId))
|
|
503
|
+
throw new PreviewInterruptedError("Text preview received an ambiguous message start.");
|
|
504
|
+
if (active.size >= 8)
|
|
505
|
+
throw new PreviewInterruptedError("Text preview exceeded eight active root message identities.");
|
|
506
|
+
active.set(event.messageId, !hidden && event.role === "assistant");
|
|
507
|
+
}
|
|
508
|
+
else if (event.type === "TEXT_MESSAGE_CONTENT") {
|
|
509
|
+
if (typeof event.messageId !== "string" ||
|
|
510
|
+
!active.has(event.messageId))
|
|
511
|
+
throw new PreviewInterruptedError("Text preview content has no trustworthy start or string delta.");
|
|
512
|
+
if (!active.get(event.messageId))
|
|
513
|
+
continue;
|
|
514
|
+
if (typeof event.delta !== "string")
|
|
515
|
+
throw new PreviewInterruptedError("Text preview content has no string delta.");
|
|
516
|
+
yield event.delta;
|
|
517
|
+
}
|
|
518
|
+
else if (event.type === "TEXT_MESSAGE_END") {
|
|
519
|
+
if (hidden &&
|
|
520
|
+
(typeof event.messageId !== "string" ||
|
|
521
|
+
!active.has(event.messageId)))
|
|
522
|
+
continue;
|
|
523
|
+
if (typeof event.messageId !== "string" ||
|
|
524
|
+
!active.delete(event.messageId))
|
|
525
|
+
throw new PreviewInterruptedError("Text preview received an unmatched message end.");
|
|
526
|
+
}
|
|
257
527
|
}
|
|
258
528
|
}
|
|
259
|
-
catch (
|
|
260
|
-
if (
|
|
261
|
-
|
|
262
|
-
|
|
529
|
+
catch (cause) {
|
|
530
|
+
if (cause instanceof PreviewInterruptedError ||
|
|
531
|
+
cause instanceof ProtocolError) {
|
|
532
|
+
this.previewFailure =
|
|
533
|
+
cause instanceof PreviewInterruptedError
|
|
534
|
+
? cause
|
|
535
|
+
: new PreviewInterruptedError("Text preview interrupted by a protocol discontinuity.", cause);
|
|
536
|
+
throw this.previewFailure;
|
|
537
|
+
}
|
|
538
|
+
throw cause;
|
|
263
539
|
}
|
|
264
540
|
finally {
|
|
265
|
-
this.
|
|
541
|
+
this.consumer = null;
|
|
266
542
|
}
|
|
267
|
-
await outcome;
|
|
268
543
|
}
|
|
269
544
|
}
|
|
270
545
|
export class AgentHandle {
|
|
@@ -276,25 +551,41 @@ export class AgentHandle {
|
|
|
276
551
|
this.resources = resources;
|
|
277
552
|
this.shutdown = shutdown;
|
|
278
553
|
}
|
|
279
|
-
async start(input, options) {
|
|
280
|
-
const
|
|
281
|
-
|
|
282
|
-
...fields,
|
|
554
|
+
async start(input, options = {}) {
|
|
555
|
+
const body = {
|
|
556
|
+
...startFields(options),
|
|
283
557
|
agent_id: this.id,
|
|
284
|
-
payload:
|
|
285
|
-
}
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
558
|
+
payload: messagePayload(input),
|
|
559
|
+
};
|
|
560
|
+
const { value: receipt, key } = await mutation("start", { agentId: this.id }, options, async (request) => {
|
|
561
|
+
const result = await this.resources.threads.create(body, request);
|
|
562
|
+
submittedIdentity(result);
|
|
563
|
+
return result;
|
|
564
|
+
});
|
|
565
|
+
return new Interaction({
|
|
566
|
+
origin: "submitted",
|
|
567
|
+
...submittedIdentity(receipt),
|
|
568
|
+
idempotencyKey: key,
|
|
569
|
+
}, this.resources, this.shutdown, options.signal, receipt);
|
|
570
|
+
}
|
|
571
|
+
async send(threadId, input, options = {}) {
|
|
572
|
+
const body = {
|
|
573
|
+
...sendFields(options),
|
|
292
574
|
agent_id: this.id,
|
|
293
|
-
payload:
|
|
294
|
-
}
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
575
|
+
payload: messagePayload(input),
|
|
576
|
+
};
|
|
577
|
+
const { value: receipt, key } = await mutation("send", { agentId: this.id, threadId }, options, async (request) => {
|
|
578
|
+
const result = await this.resources.threads
|
|
579
|
+
.ref(threadId)
|
|
580
|
+
.inbox.create(body, request);
|
|
581
|
+
submittedIdentity(result, threadId);
|
|
582
|
+
return result;
|
|
583
|
+
});
|
|
584
|
+
return new Interaction({
|
|
585
|
+
origin: "submitted",
|
|
586
|
+
...submittedIdentity(receipt),
|
|
587
|
+
idempotencyKey: key,
|
|
588
|
+
}, this.resources, this.shutdown, options.signal, receipt);
|
|
298
589
|
}
|
|
299
590
|
}
|
|
300
591
|
export class AgentCollection {
|
|
@@ -310,11 +601,13 @@ export class AgentCollection {
|
|
|
310
601
|
}
|
|
311
602
|
export class ThreadCollection {
|
|
312
603
|
resources;
|
|
313
|
-
|
|
604
|
+
shutdown;
|
|
605
|
+
constructor(resources, shutdown) {
|
|
314
606
|
this.resources = resources;
|
|
607
|
+
this.shutdown = shutdown;
|
|
315
608
|
}
|
|
316
609
|
ref(id) {
|
|
317
|
-
return new ThreadHandle(id, this.resources);
|
|
610
|
+
return new ThreadHandle(id, this.resources, this.shutdown);
|
|
318
611
|
}
|
|
319
612
|
}
|
|
320
613
|
export class RunCollection {
|