@runtypelabs/sdk 4.20.0 → 4.21.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/dist/index.cjs +610 -43
- package/dist/index.d.cts +177 -1076
- package/dist/index.d.ts +177 -1076
- package/dist/index.mjs +604 -42
- package/package.json +1 -1
package/dist/index.mjs
CHANGED
|
@@ -1,3 +1,503 @@
|
|
|
1
|
+
// src/unified-stream-adapter.ts
|
|
2
|
+
var UNIFIED_EVENTS_QUERY = "events=unified";
|
|
3
|
+
function withUnifiedEvents(path) {
|
|
4
|
+
return path.includes("?") ? `${path}&${UNIFIED_EVENTS_QUERY}` : `${path}?${UNIFIED_EVENTS_QUERY}`;
|
|
5
|
+
}
|
|
6
|
+
var UNIFIED_ONLY_EVENT_TYPES = /* @__PURE__ */ new Set([
|
|
7
|
+
"execution_start",
|
|
8
|
+
"execution_complete",
|
|
9
|
+
"execution_error",
|
|
10
|
+
"turn_start",
|
|
11
|
+
"turn_complete",
|
|
12
|
+
"text_start",
|
|
13
|
+
"text_delta",
|
|
14
|
+
"text_complete",
|
|
15
|
+
"reasoning_start",
|
|
16
|
+
"reasoning_delta",
|
|
17
|
+
"reasoning_complete",
|
|
18
|
+
"media_start",
|
|
19
|
+
"media_delta",
|
|
20
|
+
"media_complete",
|
|
21
|
+
"tool_output_delta",
|
|
22
|
+
"await"
|
|
23
|
+
]);
|
|
24
|
+
function isUnifiedEventType(type) {
|
|
25
|
+
return typeof type === "string" && UNIFIED_ONLY_EVENT_TYPES.has(type);
|
|
26
|
+
}
|
|
27
|
+
function str(value) {
|
|
28
|
+
return typeof value === "string" ? value : void 0;
|
|
29
|
+
}
|
|
30
|
+
function num(value) {
|
|
31
|
+
return typeof value === "number" ? value : void 0;
|
|
32
|
+
}
|
|
33
|
+
function compact(event) {
|
|
34
|
+
const out = { type: event.type };
|
|
35
|
+
for (const [k, v] of Object.entries(event)) {
|
|
36
|
+
if (k !== "type" && v !== void 0) out[k] = v;
|
|
37
|
+
}
|
|
38
|
+
return out;
|
|
39
|
+
}
|
|
40
|
+
var LEGACY_FLOW_PASSTHROUGH = /* @__PURE__ */ new Set([
|
|
41
|
+
"flow_start",
|
|
42
|
+
"flow_complete",
|
|
43
|
+
"flow_error",
|
|
44
|
+
"flow_await",
|
|
45
|
+
"step_await",
|
|
46
|
+
"step_delta"
|
|
47
|
+
]);
|
|
48
|
+
function createFlowEventTranslator() {
|
|
49
|
+
let currentStepId;
|
|
50
|
+
let currentStepName;
|
|
51
|
+
return (raw) => {
|
|
52
|
+
if (!raw || typeof raw !== "object") return [];
|
|
53
|
+
const data = raw;
|
|
54
|
+
const type = str(data.type);
|
|
55
|
+
if (!type) return [];
|
|
56
|
+
const executionId = str(data.executionId);
|
|
57
|
+
const seq = num(data.seq);
|
|
58
|
+
if (LEGACY_FLOW_PASSTHROUGH.has(type)) return [data];
|
|
59
|
+
switch (type) {
|
|
60
|
+
case "execution_start":
|
|
61
|
+
if (data.kind !== "flow") return [];
|
|
62
|
+
return [
|
|
63
|
+
compact({
|
|
64
|
+
type: "flow_start",
|
|
65
|
+
executionId,
|
|
66
|
+
seq,
|
|
67
|
+
flowId: data.flowId,
|
|
68
|
+
flowName: data.flowName,
|
|
69
|
+
totalSteps: data.totalSteps,
|
|
70
|
+
startedAt: data.startedAt,
|
|
71
|
+
source: data.source
|
|
72
|
+
})
|
|
73
|
+
];
|
|
74
|
+
case "step_start": {
|
|
75
|
+
const id = str(data.id) ?? str(data.stepId);
|
|
76
|
+
const name = str(data.name) ?? str(data.stepName);
|
|
77
|
+
currentStepId = id;
|
|
78
|
+
currentStepName = name;
|
|
79
|
+
return [
|
|
80
|
+
compact({
|
|
81
|
+
type: "step_start",
|
|
82
|
+
executionId,
|
|
83
|
+
seq,
|
|
84
|
+
stepId: id,
|
|
85
|
+
name,
|
|
86
|
+
stepName: name,
|
|
87
|
+
stepType: data.stepType,
|
|
88
|
+
index: data.index,
|
|
89
|
+
totalSteps: data.totalSteps,
|
|
90
|
+
startedAt: data.startedAt,
|
|
91
|
+
outputVariable: data.outputVariable
|
|
92
|
+
})
|
|
93
|
+
];
|
|
94
|
+
}
|
|
95
|
+
case "text_delta":
|
|
96
|
+
return [
|
|
97
|
+
compact({
|
|
98
|
+
type: "step_delta",
|
|
99
|
+
executionId,
|
|
100
|
+
seq,
|
|
101
|
+
delta: str(data.delta) ?? "",
|
|
102
|
+
text: str(data.delta) ?? "",
|
|
103
|
+
stepId: currentStepId,
|
|
104
|
+
name: currentStepName,
|
|
105
|
+
stepName: currentStepName
|
|
106
|
+
})
|
|
107
|
+
];
|
|
108
|
+
case "step_complete": {
|
|
109
|
+
const id = str(data.id) ?? str(data.stepId);
|
|
110
|
+
const name = str(data.name) ?? str(data.stepName);
|
|
111
|
+
const out = compact({
|
|
112
|
+
type: "step_complete",
|
|
113
|
+
executionId,
|
|
114
|
+
seq,
|
|
115
|
+
stepId: id,
|
|
116
|
+
name,
|
|
117
|
+
stepName: name,
|
|
118
|
+
stepType: data.stepType,
|
|
119
|
+
result: data.result,
|
|
120
|
+
success: data.success,
|
|
121
|
+
executionTime: num(data.durationMs) ?? num(data.executionTime),
|
|
122
|
+
stopReason: data.stopReason,
|
|
123
|
+
completedAt: data.completedAt,
|
|
124
|
+
unresolvedVariables: data.unresolvedVariables,
|
|
125
|
+
error: data.error
|
|
126
|
+
});
|
|
127
|
+
currentStepId = void 0;
|
|
128
|
+
currentStepName = void 0;
|
|
129
|
+
return [out];
|
|
130
|
+
}
|
|
131
|
+
case "execution_complete":
|
|
132
|
+
if (data.kind !== "flow") return [];
|
|
133
|
+
return [
|
|
134
|
+
compact({
|
|
135
|
+
type: "flow_complete",
|
|
136
|
+
executionId,
|
|
137
|
+
seq,
|
|
138
|
+
success: data.success,
|
|
139
|
+
totalSteps: data.totalSteps,
|
|
140
|
+
successfulSteps: data.successfulSteps,
|
|
141
|
+
failedSteps: data.failedSteps,
|
|
142
|
+
executionTime: data.durationMs,
|
|
143
|
+
finalOutput: data.finalOutput,
|
|
144
|
+
completedAt: data.completedAt
|
|
145
|
+
})
|
|
146
|
+
];
|
|
147
|
+
case "execution_error":
|
|
148
|
+
return [
|
|
149
|
+
compact({
|
|
150
|
+
type: "flow_error",
|
|
151
|
+
executionId,
|
|
152
|
+
seq,
|
|
153
|
+
error: data.error,
|
|
154
|
+
code: data.code,
|
|
155
|
+
upgradeUrl: data.upgradeUrl
|
|
156
|
+
})
|
|
157
|
+
];
|
|
158
|
+
case "await":
|
|
159
|
+
return [
|
|
160
|
+
compact({
|
|
161
|
+
type: "flow_await",
|
|
162
|
+
executionId,
|
|
163
|
+
seq,
|
|
164
|
+
toolId: data.toolId ?? data.toolCallId,
|
|
165
|
+
toolName: data.toolName,
|
|
166
|
+
parameters: data.parameters,
|
|
167
|
+
awaitedAt: data.awaitedAt,
|
|
168
|
+
origin: data.origin,
|
|
169
|
+
pageOrigin: data.pageOrigin
|
|
170
|
+
})
|
|
171
|
+
];
|
|
172
|
+
// Channels/markers/tool-family/approval/skip/source/custom/ping the flow
|
|
173
|
+
// consumer never read → no legacy event. (A non-terminal `error` is only
|
|
174
|
+
// produced on agent streams; dropping it here avoids a false flow_error.)
|
|
175
|
+
default:
|
|
176
|
+
return [];
|
|
177
|
+
}
|
|
178
|
+
};
|
|
179
|
+
}
|
|
180
|
+
function createAgentEventTranslator() {
|
|
181
|
+
let agentId;
|
|
182
|
+
let iteration;
|
|
183
|
+
let turnId;
|
|
184
|
+
let reasoningScope;
|
|
185
|
+
let media = null;
|
|
186
|
+
return (raw) => {
|
|
187
|
+
if (!raw || typeof raw !== "object") return [];
|
|
188
|
+
const data = raw;
|
|
189
|
+
const type = str(data.type);
|
|
190
|
+
if (!type) return [];
|
|
191
|
+
const executionId = str(data.executionId);
|
|
192
|
+
const seq = num(data.seq);
|
|
193
|
+
const iter = num(data.iteration) ?? iteration;
|
|
194
|
+
if (type.startsWith("agent_")) return [data];
|
|
195
|
+
switch (type) {
|
|
196
|
+
case "execution_start":
|
|
197
|
+
if (data.kind !== "agent") return [];
|
|
198
|
+
agentId = str(data.agentId);
|
|
199
|
+
return [
|
|
200
|
+
compact({
|
|
201
|
+
type: "agent_start",
|
|
202
|
+
executionId,
|
|
203
|
+
seq,
|
|
204
|
+
agentId: data.agentId,
|
|
205
|
+
agentName: data.agentName,
|
|
206
|
+
maxTurns: data.maxTurns,
|
|
207
|
+
startedAt: data.startedAt,
|
|
208
|
+
config: data.config
|
|
209
|
+
})
|
|
210
|
+
];
|
|
211
|
+
case "turn_start":
|
|
212
|
+
turnId = str(data.id);
|
|
213
|
+
iteration = num(data.iteration) ?? iteration;
|
|
214
|
+
return [
|
|
215
|
+
compact({
|
|
216
|
+
type: "agent_turn_start",
|
|
217
|
+
executionId,
|
|
218
|
+
seq,
|
|
219
|
+
iteration: data.iteration ?? iteration,
|
|
220
|
+
turnId: data.id,
|
|
221
|
+
turnIndex: data.turnIndex,
|
|
222
|
+
role: data.role
|
|
223
|
+
})
|
|
224
|
+
];
|
|
225
|
+
case "text_delta":
|
|
226
|
+
return [
|
|
227
|
+
compact({
|
|
228
|
+
type: "agent_turn_delta",
|
|
229
|
+
executionId,
|
|
230
|
+
seq,
|
|
231
|
+
iteration: iter,
|
|
232
|
+
turnId,
|
|
233
|
+
delta: str(data.delta) ?? "",
|
|
234
|
+
contentType: "text"
|
|
235
|
+
})
|
|
236
|
+
];
|
|
237
|
+
case "reasoning_start":
|
|
238
|
+
reasoningScope = data.scope === "loop" ? "loop" : "turn";
|
|
239
|
+
return [];
|
|
240
|
+
case "reasoning_delta":
|
|
241
|
+
return [
|
|
242
|
+
compact({
|
|
243
|
+
type: "agent_turn_delta",
|
|
244
|
+
executionId,
|
|
245
|
+
seq,
|
|
246
|
+
iteration: iter,
|
|
247
|
+
turnId,
|
|
248
|
+
delta: str(data.delta) ?? "",
|
|
249
|
+
contentType: "thinking"
|
|
250
|
+
})
|
|
251
|
+
];
|
|
252
|
+
case "reasoning_complete": {
|
|
253
|
+
const scope = data.scope === "loop" ? "loop" : reasoningScope;
|
|
254
|
+
reasoningScope = void 0;
|
|
255
|
+
if (scope === "loop") {
|
|
256
|
+
return [
|
|
257
|
+
compact({
|
|
258
|
+
type: "agent_reflection",
|
|
259
|
+
executionId,
|
|
260
|
+
seq,
|
|
261
|
+
iteration: iter,
|
|
262
|
+
reflection: data.text
|
|
263
|
+
})
|
|
264
|
+
];
|
|
265
|
+
}
|
|
266
|
+
return [];
|
|
267
|
+
}
|
|
268
|
+
case "turn_complete":
|
|
269
|
+
return [
|
|
270
|
+
compact({
|
|
271
|
+
type: "agent_turn_complete",
|
|
272
|
+
executionId,
|
|
273
|
+
seq,
|
|
274
|
+
iteration: data.iteration ?? iter,
|
|
275
|
+
turnId: data.id,
|
|
276
|
+
role: data.role,
|
|
277
|
+
completedAt: data.completedAt,
|
|
278
|
+
content: data.content,
|
|
279
|
+
tokens: data.tokens,
|
|
280
|
+
cost: data.cost,
|
|
281
|
+
stopReason: data.stopReason
|
|
282
|
+
})
|
|
283
|
+
];
|
|
284
|
+
case "tool_start":
|
|
285
|
+
return [
|
|
286
|
+
compact({
|
|
287
|
+
type: "agent_tool_start",
|
|
288
|
+
executionId,
|
|
289
|
+
seq,
|
|
290
|
+
iteration: iter,
|
|
291
|
+
toolCallId: data.toolCallId,
|
|
292
|
+
toolName: data.toolName,
|
|
293
|
+
toolType: data.toolType,
|
|
294
|
+
parameters: data.parameters,
|
|
295
|
+
origin: data.origin,
|
|
296
|
+
pageOrigin: data.pageOrigin
|
|
297
|
+
})
|
|
298
|
+
];
|
|
299
|
+
case "tool_output_delta":
|
|
300
|
+
return [
|
|
301
|
+
compact({
|
|
302
|
+
type: "agent_tool_delta",
|
|
303
|
+
executionId,
|
|
304
|
+
seq,
|
|
305
|
+
iteration: iter,
|
|
306
|
+
toolCallId: data.toolCallId,
|
|
307
|
+
delta: str(data.delta) ?? ""
|
|
308
|
+
})
|
|
309
|
+
];
|
|
310
|
+
case "tool_input_delta":
|
|
311
|
+
return [
|
|
312
|
+
compact({
|
|
313
|
+
type: "agent_tool_input_delta",
|
|
314
|
+
executionId,
|
|
315
|
+
seq,
|
|
316
|
+
iteration: iter,
|
|
317
|
+
toolCallId: data.toolCallId,
|
|
318
|
+
delta: str(data.delta) ?? ""
|
|
319
|
+
})
|
|
320
|
+
];
|
|
321
|
+
case "tool_input_complete":
|
|
322
|
+
return [
|
|
323
|
+
compact({
|
|
324
|
+
type: "agent_tool_input_complete",
|
|
325
|
+
executionId,
|
|
326
|
+
seq,
|
|
327
|
+
iteration: iter,
|
|
328
|
+
toolCallId: data.toolCallId,
|
|
329
|
+
toolName: data.toolName,
|
|
330
|
+
parameters: data.parameters ?? {},
|
|
331
|
+
hiddenParameterNames: data.hiddenParameterNames
|
|
332
|
+
})
|
|
333
|
+
];
|
|
334
|
+
case "tool_complete":
|
|
335
|
+
return [
|
|
336
|
+
compact({
|
|
337
|
+
type: "agent_tool_complete",
|
|
338
|
+
executionId,
|
|
339
|
+
seq,
|
|
340
|
+
iteration: num(data.iteration) ?? iter,
|
|
341
|
+
toolCallId: data.toolCallId,
|
|
342
|
+
toolName: data.toolName,
|
|
343
|
+
success: data.success,
|
|
344
|
+
result: data.result,
|
|
345
|
+
executionTime: data.executionTime
|
|
346
|
+
})
|
|
347
|
+
];
|
|
348
|
+
case "media_start":
|
|
349
|
+
media = {
|
|
350
|
+
mediaType: str(data.mediaType),
|
|
351
|
+
role: str(data.role),
|
|
352
|
+
toolCallId: str(data.toolCallId),
|
|
353
|
+
chunks: []
|
|
354
|
+
};
|
|
355
|
+
return [];
|
|
356
|
+
case "media_delta": {
|
|
357
|
+
const delta = str(data.delta);
|
|
358
|
+
if (media && delta) media.chunks.push(delta);
|
|
359
|
+
return [];
|
|
360
|
+
}
|
|
361
|
+
case "media_complete": {
|
|
362
|
+
const acc = media;
|
|
363
|
+
media = null;
|
|
364
|
+
const mediaType = str(data.mediaType) ?? acc?.mediaType;
|
|
365
|
+
const toolCallId = str(data.toolCallId) ?? acc?.toolCallId;
|
|
366
|
+
const url = str(data.url);
|
|
367
|
+
const inlineData = str(data.data) ?? (acc && acc.chunks.length ? acc.chunks.join("") : void 0);
|
|
368
|
+
let item;
|
|
369
|
+
if (url) {
|
|
370
|
+
const kind = mediaType && mediaType.startsWith("image/") ? "image-url" : "file-url";
|
|
371
|
+
item = compact({ type: kind, url, mediaType });
|
|
372
|
+
} else {
|
|
373
|
+
item = compact({ type: "media", data: inlineData ?? "", mediaType });
|
|
374
|
+
}
|
|
375
|
+
return [
|
|
376
|
+
compact({
|
|
377
|
+
type: "agent_media",
|
|
378
|
+
executionId,
|
|
379
|
+
seq,
|
|
380
|
+
iteration: iter,
|
|
381
|
+
toolCallId,
|
|
382
|
+
media: [item]
|
|
383
|
+
})
|
|
384
|
+
];
|
|
385
|
+
}
|
|
386
|
+
case "approval_start":
|
|
387
|
+
return [
|
|
388
|
+
compact({
|
|
389
|
+
type: "agent_approval_start",
|
|
390
|
+
executionId,
|
|
391
|
+
seq,
|
|
392
|
+
iteration: iter,
|
|
393
|
+
approvalId: data.approvalId,
|
|
394
|
+
toolCallId: data.toolCallId,
|
|
395
|
+
toolName: data.toolName,
|
|
396
|
+
toolType: data.toolType,
|
|
397
|
+
description: data.description,
|
|
398
|
+
reason: data.reason,
|
|
399
|
+
parameters: data.parameters,
|
|
400
|
+
timeout: data.timeout,
|
|
401
|
+
startedAt: data.startedAt
|
|
402
|
+
})
|
|
403
|
+
];
|
|
404
|
+
case "approval_complete":
|
|
405
|
+
return [
|
|
406
|
+
compact({
|
|
407
|
+
type: "agent_approval_complete",
|
|
408
|
+
executionId,
|
|
409
|
+
seq,
|
|
410
|
+
approvalId: data.approvalId,
|
|
411
|
+
decision: data.decision,
|
|
412
|
+
completedAt: data.completedAt,
|
|
413
|
+
resolvedBy: data.resolvedBy
|
|
414
|
+
})
|
|
415
|
+
];
|
|
416
|
+
case "await":
|
|
417
|
+
return [
|
|
418
|
+
compact({
|
|
419
|
+
type: "agent_await",
|
|
420
|
+
executionId,
|
|
421
|
+
seq,
|
|
422
|
+
toolId: data.toolId ?? data.toolCallId,
|
|
423
|
+
toolName: data.toolName,
|
|
424
|
+
parameters: data.parameters,
|
|
425
|
+
awaitedAt: data.awaitedAt,
|
|
426
|
+
origin: data.origin,
|
|
427
|
+
pageOrigin: data.pageOrigin
|
|
428
|
+
})
|
|
429
|
+
];
|
|
430
|
+
case "execution_complete":
|
|
431
|
+
if (data.kind !== "agent") return [];
|
|
432
|
+
return [
|
|
433
|
+
compact({
|
|
434
|
+
type: "agent_complete",
|
|
435
|
+
executionId,
|
|
436
|
+
seq,
|
|
437
|
+
agentId: data.agentId ?? agentId,
|
|
438
|
+
success: data.success,
|
|
439
|
+
iterations: data.iterations,
|
|
440
|
+
stopReason: data.stopReason,
|
|
441
|
+
completedAt: data.completedAt,
|
|
442
|
+
totalCost: data.totalCost,
|
|
443
|
+
totalTokens: data.totalTokens,
|
|
444
|
+
finalOutput: data.finalOutput,
|
|
445
|
+
duration: data.durationMs
|
|
446
|
+
})
|
|
447
|
+
];
|
|
448
|
+
case "execution_error":
|
|
449
|
+
return [
|
|
450
|
+
compact({
|
|
451
|
+
type: "agent_complete",
|
|
452
|
+
executionId,
|
|
453
|
+
seq,
|
|
454
|
+
agentId,
|
|
455
|
+
success: false,
|
|
456
|
+
stopReason: "error",
|
|
457
|
+
error: errorMessage(data.error),
|
|
458
|
+
completedAt: data.completedAt
|
|
459
|
+
})
|
|
460
|
+
];
|
|
461
|
+
case "error":
|
|
462
|
+
return [
|
|
463
|
+
compact({
|
|
464
|
+
type: "agent_error",
|
|
465
|
+
executionId,
|
|
466
|
+
seq,
|
|
467
|
+
iteration: iter,
|
|
468
|
+
error: errorObject(data.error),
|
|
469
|
+
recoverable: true
|
|
470
|
+
})
|
|
471
|
+
];
|
|
472
|
+
case "ping":
|
|
473
|
+
return [compact({ type: "agent_ping", executionId, seq, timestamp: data.timestamp })];
|
|
474
|
+
// turn/text/reasoning open+close markers, step_*, artifact_*, source,
|
|
475
|
+
// custom (fallback beat / routing), and the skill-fold result envelope
|
|
476
|
+
// the SDK never consumed → no legacy event.
|
|
477
|
+
default:
|
|
478
|
+
return [];
|
|
479
|
+
}
|
|
480
|
+
};
|
|
481
|
+
}
|
|
482
|
+
function errorMessage(error) {
|
|
483
|
+
if (typeof error === "string") return error;
|
|
484
|
+
if (error && typeof error === "object" && "message" in error) {
|
|
485
|
+
const message = error.message;
|
|
486
|
+
if (typeof message === "string") return message;
|
|
487
|
+
}
|
|
488
|
+
return error === void 0 ? "Agent execution failed" : JSON.stringify(error);
|
|
489
|
+
}
|
|
490
|
+
function errorObject(error) {
|
|
491
|
+
if (typeof error === "string") return { code: "error", message: error };
|
|
492
|
+
if (error && typeof error === "object") {
|
|
493
|
+
const e = error;
|
|
494
|
+
const obj = { code: e.code ?? "error", message: e.message ?? "" };
|
|
495
|
+
if (e.details !== void 0) obj.details = e.details;
|
|
496
|
+
return obj;
|
|
497
|
+
}
|
|
498
|
+
return { code: "error", message: String(error) };
|
|
499
|
+
}
|
|
500
|
+
|
|
1
501
|
// src/stream-utils.ts
|
|
2
502
|
function parseSSEChunk(chunk, buffer) {
|
|
3
503
|
buffer += chunk;
|
|
@@ -35,7 +535,7 @@ function parseFinalBuffer(buffer) {
|
|
|
35
535
|
}
|
|
36
536
|
return null;
|
|
37
537
|
}
|
|
38
|
-
async function processStream(response, callbacks = {}) {
|
|
538
|
+
async function processStream(response, callbacks = {}, options = {}) {
|
|
39
539
|
if (!response.ok) {
|
|
40
540
|
const error = new Error(`HTTP ${response.status}: ${response.statusText}`);
|
|
41
541
|
callbacks.onError?.(error);
|
|
@@ -59,6 +559,20 @@ async function processStream(response, callbacks = {}) {
|
|
|
59
559
|
executionTime: 0,
|
|
60
560
|
success: true
|
|
61
561
|
};
|
|
562
|
+
let translate = options.unified === true ? createFlowEventTranslator() : null;
|
|
563
|
+
const autoDetect = options.unified === void 0;
|
|
564
|
+
const dispatch = (parsed) => {
|
|
565
|
+
if (autoDetect && !translate && isUnifiedEventType(parsed?.type)) {
|
|
566
|
+
translate = createFlowEventTranslator();
|
|
567
|
+
}
|
|
568
|
+
if (translate) {
|
|
569
|
+
for (const ev of translate(parsed)) {
|
|
570
|
+
handleEvent(ev, callbacks, results, flowSummary);
|
|
571
|
+
}
|
|
572
|
+
} else {
|
|
573
|
+
handleEvent(parsed, callbacks, results, flowSummary);
|
|
574
|
+
}
|
|
575
|
+
};
|
|
62
576
|
const contentType = response.headers.get("content-type");
|
|
63
577
|
if (contentType?.includes("application/json")) {
|
|
64
578
|
try {
|
|
@@ -115,8 +629,7 @@ async function processStream(response, callbacks = {}) {
|
|
|
115
629
|
buffer = remainingBuffer;
|
|
116
630
|
for (const eventStr of events) {
|
|
117
631
|
try {
|
|
118
|
-
|
|
119
|
-
handleEvent(event, callbacks, results, flowSummary);
|
|
632
|
+
dispatch(JSON.parse(eventStr));
|
|
120
633
|
} catch {
|
|
121
634
|
console.warn("Failed to parse SSE event:", eventStr);
|
|
122
635
|
}
|
|
@@ -125,8 +638,7 @@ async function processStream(response, callbacks = {}) {
|
|
|
125
638
|
const finalEvent = parseFinalBuffer(buffer);
|
|
126
639
|
if (finalEvent) {
|
|
127
640
|
try {
|
|
128
|
-
|
|
129
|
-
handleEvent(event, callbacks, results, flowSummary);
|
|
641
|
+
dispatch(JSON.parse(finalEvent));
|
|
130
642
|
} catch {
|
|
131
643
|
}
|
|
132
644
|
}
|
|
@@ -196,10 +708,17 @@ function handleEvent(event, callbacks, results, summary) {
|
|
|
196
708
|
break;
|
|
197
709
|
}
|
|
198
710
|
}
|
|
199
|
-
async function* streamEvents(response) {
|
|
711
|
+
async function* streamEvents(response, options = {}) {
|
|
200
712
|
if (!response.ok) {
|
|
201
713
|
throw new Error(`HTTP ${response.status}: ${response.statusText}`);
|
|
202
714
|
}
|
|
715
|
+
let translate = options.unified === true ? createFlowEventTranslator() : null;
|
|
716
|
+
const autoDetect = options.unified === void 0;
|
|
717
|
+
const maybeDetect = (parsed) => {
|
|
718
|
+
if (autoDetect && !translate && isUnifiedEventType(parsed?.type)) {
|
|
719
|
+
translate = createFlowEventTranslator();
|
|
720
|
+
}
|
|
721
|
+
};
|
|
203
722
|
const contentType = response.headers.get("content-type");
|
|
204
723
|
if (contentType?.includes("application/json")) {
|
|
205
724
|
try {
|
|
@@ -229,19 +748,35 @@ async function* streamEvents(response) {
|
|
|
229
748
|
const { events, remainingBuffer } = parseSSEChunk(chunk, buffer);
|
|
230
749
|
buffer = remainingBuffer;
|
|
231
750
|
for (const eventStr of events) {
|
|
751
|
+
let parsed;
|
|
232
752
|
try {
|
|
233
|
-
|
|
234
|
-
yield event;
|
|
753
|
+
parsed = JSON.parse(eventStr);
|
|
235
754
|
} catch {
|
|
755
|
+
continue;
|
|
756
|
+
}
|
|
757
|
+
maybeDetect(parsed);
|
|
758
|
+
if (translate) {
|
|
759
|
+
for (const ev of translate(parsed)) yield ev;
|
|
760
|
+
} else {
|
|
761
|
+
yield parsed;
|
|
236
762
|
}
|
|
237
763
|
}
|
|
238
764
|
}
|
|
239
765
|
const finalEvent = parseFinalBuffer(buffer);
|
|
240
766
|
if (finalEvent) {
|
|
767
|
+
let parsed;
|
|
241
768
|
try {
|
|
242
|
-
|
|
243
|
-
yield event;
|
|
769
|
+
parsed = JSON.parse(finalEvent);
|
|
244
770
|
} catch {
|
|
771
|
+
parsed = void 0;
|
|
772
|
+
}
|
|
773
|
+
if (parsed !== void 0) {
|
|
774
|
+
maybeDetect(parsed);
|
|
775
|
+
if (translate) {
|
|
776
|
+
for (const ev of translate(parsed)) yield ev;
|
|
777
|
+
} else {
|
|
778
|
+
yield parsed;
|
|
779
|
+
}
|
|
245
780
|
}
|
|
246
781
|
}
|
|
247
782
|
} finally {
|
|
@@ -251,10 +786,16 @@ async function* streamEvents(response) {
|
|
|
251
786
|
|
|
252
787
|
// src/flow-result.ts
|
|
253
788
|
var FlowResult = class {
|
|
254
|
-
|
|
789
|
+
/**
|
|
790
|
+
* @param options.unified - The response is a unified-vocabulary SSE stream
|
|
791
|
+
* (`/dispatch` since the unified-SSE cutover). Set by dispatch call sites;
|
|
792
|
+
* left unset by still-legacy producers (`/prompts/{id}/run`, eval stream).
|
|
793
|
+
*/
|
|
794
|
+
constructor(response, summary, options = {}) {
|
|
255
795
|
this.consumed = false;
|
|
256
796
|
this.cachedSummary = null;
|
|
257
797
|
this.response = response;
|
|
798
|
+
this.streamOptions = options;
|
|
258
799
|
if (summary) {
|
|
259
800
|
this.cachedSummary = summary;
|
|
260
801
|
this.consumed = true;
|
|
@@ -307,7 +848,7 @@ var FlowResult = class {
|
|
|
307
848
|
}
|
|
308
849
|
this.ensureNotConsumed();
|
|
309
850
|
this.consumed = true;
|
|
310
|
-
this.cachedSummary = await processStream(this.response, callbacks);
|
|
851
|
+
this.cachedSummary = await processStream(this.response, callbacks, this.streamOptions);
|
|
311
852
|
return this.cachedSummary;
|
|
312
853
|
}
|
|
313
854
|
/**
|
|
@@ -1153,9 +1694,9 @@ var FlowBuilder = class {
|
|
|
1153
1694
|
}
|
|
1154
1695
|
const response = await client.dispatch(config);
|
|
1155
1696
|
if (callbacks) {
|
|
1156
|
-
return processStream(response, callbacks);
|
|
1697
|
+
return processStream(response, callbacks, { unified: true });
|
|
1157
1698
|
}
|
|
1158
|
-
return new FlowResult(response);
|
|
1699
|
+
return new FlowResult(response, void 0, { unified: true });
|
|
1159
1700
|
}
|
|
1160
1701
|
/**
|
|
1161
1702
|
* Set a run condition (when predicate) on the last added step.
|
|
@@ -1287,9 +1828,9 @@ var ClientFlowBuilder = class extends FlowBuilder {
|
|
|
1287
1828
|
}
|
|
1288
1829
|
const response = await dispatchClient.dispatch(config);
|
|
1289
1830
|
if (runCallbacks) {
|
|
1290
|
-
return processStream(response, runCallbacks);
|
|
1831
|
+
return processStream(response, runCallbacks, { unified: true });
|
|
1291
1832
|
}
|
|
1292
|
-
return new FlowResult(response);
|
|
1833
|
+
return new FlowResult(response, void 0, { unified: true });
|
|
1293
1834
|
}
|
|
1294
1835
|
};
|
|
1295
1836
|
function isStreamCallbacks(obj) {
|
|
@@ -2307,7 +2848,7 @@ var RuntypeFlowBuilder = class {
|
|
|
2307
2848
|
config.options = { ...config.options, streamResponse: true };
|
|
2308
2849
|
const client = this.getClient();
|
|
2309
2850
|
const response = await this.dispatchWithPersistedFlow(client, config);
|
|
2310
|
-
const result = new FlowResult(response);
|
|
2851
|
+
const result = new FlowResult(response, void 0, { unified: true });
|
|
2311
2852
|
if (callbacks) {
|
|
2312
2853
|
await result.stream(callbacks);
|
|
2313
2854
|
}
|
|
@@ -2336,7 +2877,7 @@ var RuntypeFlowBuilder = class {
|
|
|
2336
2877
|
config.options = { ...config.options, streamResponse: true };
|
|
2337
2878
|
const client = this.getClient();
|
|
2338
2879
|
const response = await this.dispatchWithPersistedFlow(client, config);
|
|
2339
|
-
const result = new FlowResult(response);
|
|
2880
|
+
const result = new FlowResult(response, void 0, { unified: true });
|
|
2340
2881
|
await result.getSummary();
|
|
2341
2882
|
return result;
|
|
2342
2883
|
}
|
|
@@ -2371,7 +2912,7 @@ var RuntypeFlowBuilder = class {
|
|
|
2371
2912
|
onError: (error) => callbacks?.onError?.(error)
|
|
2372
2913
|
};
|
|
2373
2914
|
try {
|
|
2374
|
-
for await (const event of streamEvents(response)) {
|
|
2915
|
+
for await (const event of streamEvents(response, { unified: true })) {
|
|
2375
2916
|
collectLocalToolAwait(pausedTools, event);
|
|
2376
2917
|
switch (event.type) {
|
|
2377
2918
|
case "flow_start":
|
|
@@ -2479,7 +3020,7 @@ var RuntypeFlowBuilder = class {
|
|
|
2479
3020
|
streamResponse: isStreaming
|
|
2480
3021
|
};
|
|
2481
3022
|
if (isStreaming) {
|
|
2482
|
-
currentResponse = await client.requestStream("/dispatch/resume", {
|
|
3023
|
+
currentResponse = await client.requestStream(withUnifiedEvents("/dispatch/resume"), {
|
|
2483
3024
|
method: "POST",
|
|
2484
3025
|
body: JSON.stringify(resumeData)
|
|
2485
3026
|
});
|
|
@@ -4723,7 +5264,7 @@ var RuntypeClient = class {
|
|
|
4723
5264
|
* Dispatch flow execution (streaming)
|
|
4724
5265
|
*/
|
|
4725
5266
|
async dispatch(config) {
|
|
4726
|
-
return this.requestStream("/dispatch", {
|
|
5267
|
+
return this.requestStream(withUnifiedEvents("/dispatch"), {
|
|
4727
5268
|
method: "POST",
|
|
4728
5269
|
body: JSON.stringify(transformRequest(config))
|
|
4729
5270
|
});
|
|
@@ -5094,7 +5635,7 @@ var Runtype = class {
|
|
|
5094
5635
|
|
|
5095
5636
|
// src/version.ts
|
|
5096
5637
|
var FALLBACK_VERSION = "0.0.0";
|
|
5097
|
-
var SDK_VERSION = "4.
|
|
5638
|
+
var SDK_VERSION = "4.21.0".length > 0 ? "4.21.0" : FALLBACK_VERSION;
|
|
5098
5639
|
var RUNTYPE_CLIENT_KIND = "sdk";
|
|
5099
5640
|
var SDK_USER_AGENT = `runtype-sdk/${SDK_VERSION} (typescript)`;
|
|
5100
5641
|
|
|
@@ -7588,7 +8129,7 @@ var DispatchEndpoint = class {
|
|
|
7588
8129
|
* Dispatch with streaming response
|
|
7589
8130
|
*/
|
|
7590
8131
|
async executeStream(data) {
|
|
7591
|
-
return this.client.requestStream("/dispatch", {
|
|
8132
|
+
return this.client.requestStream(withUnifiedEvents("/dispatch"), {
|
|
7592
8133
|
method: "POST",
|
|
7593
8134
|
body: JSON.stringify(data)
|
|
7594
8135
|
});
|
|
@@ -7604,7 +8145,7 @@ var DispatchEndpoint = class {
|
|
|
7604
8145
|
*/
|
|
7605
8146
|
async resume(data) {
|
|
7606
8147
|
if (data.streamResponse) {
|
|
7607
|
-
return this.client.requestStream("/dispatch/resume", {
|
|
8148
|
+
return this.client.requestStream(withUnifiedEvents("/dispatch/resume"), {
|
|
7608
8149
|
method: "POST",
|
|
7609
8150
|
body: JSON.stringify(data)
|
|
7610
8151
|
});
|
|
@@ -8119,6 +8660,19 @@ async function processAgentStream(body, callbacks) {
|
|
|
8119
8660
|
const reader = body.getReader();
|
|
8120
8661
|
const decoder = new TextDecoder();
|
|
8121
8662
|
let buffer = "";
|
|
8663
|
+
let translate = null;
|
|
8664
|
+
const dispatch = (event) => {
|
|
8665
|
+
if (!translate && isUnifiedEventType(event.data?.type ?? event.eventType)) {
|
|
8666
|
+
translate = createAgentEventTranslator();
|
|
8667
|
+
}
|
|
8668
|
+
if (translate) {
|
|
8669
|
+
for (const legacy of translate(event.data)) {
|
|
8670
|
+
dispatchAgentEvent({ eventType: legacy.type, data: legacy }, callbacks);
|
|
8671
|
+
}
|
|
8672
|
+
} else {
|
|
8673
|
+
dispatchAgentEvent(event, callbacks);
|
|
8674
|
+
}
|
|
8675
|
+
};
|
|
8122
8676
|
try {
|
|
8123
8677
|
while (true) {
|
|
8124
8678
|
const { done, value } = await reader.read();
|
|
@@ -8126,7 +8680,7 @@ async function processAgentStream(body, callbacks) {
|
|
|
8126
8680
|
if (buffer.trim()) {
|
|
8127
8681
|
const { events: events2 } = parseSSEChunkWithEventType(buffer + "\n\n", "");
|
|
8128
8682
|
for (const event of events2) {
|
|
8129
|
-
|
|
8683
|
+
dispatch(event);
|
|
8130
8684
|
}
|
|
8131
8685
|
}
|
|
8132
8686
|
break;
|
|
@@ -8135,7 +8689,7 @@ async function processAgentStream(body, callbacks) {
|
|
|
8135
8689
|
const { events, remainingBuffer } = parseSSEChunkWithEventType(chunk, buffer);
|
|
8136
8690
|
buffer = remainingBuffer;
|
|
8137
8691
|
for (const event of events) {
|
|
8138
|
-
|
|
8692
|
+
dispatch(event);
|
|
8139
8693
|
}
|
|
8140
8694
|
}
|
|
8141
8695
|
} finally {
|
|
@@ -8323,7 +8877,7 @@ var _AgentsEndpoint = class _AgentsEndpoint {
|
|
|
8323
8877
|
* ```
|
|
8324
8878
|
*/
|
|
8325
8879
|
async executeStream(id, data, init) {
|
|
8326
|
-
return this.client.requestStream(`/agents/${id}/execute
|
|
8880
|
+
return this.client.requestStream(withUnifiedEvents(`/agents/${id}/execute`), {
|
|
8327
8881
|
method: "POST",
|
|
8328
8882
|
body: JSON.stringify({
|
|
8329
8883
|
...data,
|
|
@@ -8675,16 +9229,19 @@ var _AgentsEndpoint = class _AgentsEndpoint {
|
|
|
8675
9229
|
}
|
|
8676
9230
|
let resumeResponse;
|
|
8677
9231
|
try {
|
|
8678
|
-
resumeResponse = await this.client.requestStream(
|
|
8679
|
-
|
|
8680
|
-
|
|
8681
|
-
|
|
8682
|
-
|
|
8683
|
-
|
|
8684
|
-
|
|
8685
|
-
|
|
8686
|
-
|
|
8687
|
-
|
|
9232
|
+
resumeResponse = await this.client.requestStream(
|
|
9233
|
+
withUnifiedEvents(`/agents/${id}/resume`),
|
|
9234
|
+
{
|
|
9235
|
+
method: "POST",
|
|
9236
|
+
body: JSON.stringify({
|
|
9237
|
+
executionId,
|
|
9238
|
+
toolOutputs: { [toolName]: toolResult },
|
|
9239
|
+
streamResponse: true,
|
|
9240
|
+
debugMode: data.debugMode
|
|
9241
|
+
}),
|
|
9242
|
+
...abortSignal ? { signal: abortSignal } : {}
|
|
9243
|
+
}
|
|
9244
|
+
);
|
|
8688
9245
|
} catch (error) {
|
|
8689
9246
|
if (abortSignal?.aborted) return finishAborted(executionId);
|
|
8690
9247
|
throw error;
|
|
@@ -10149,9 +10706,9 @@ var _AgentsEndpoint = class _AgentsEndpoint {
|
|
|
10149
10706
|
}
|
|
10150
10707
|
/** Returns true if a server-side session error message indicates a transient
|
|
10151
10708
|
* network failure that is safe to retry. */
|
|
10152
|
-
static isRetryableSessionError(
|
|
10153
|
-
if (!
|
|
10154
|
-
const lower =
|
|
10709
|
+
static isRetryableSessionError(errorMessage2) {
|
|
10710
|
+
if (!errorMessage2) return false;
|
|
10711
|
+
const lower = errorMessage2.toLowerCase();
|
|
10155
10712
|
return _AgentsEndpoint.RETRYABLE_SESSION_ERROR_PATTERNS.some(
|
|
10156
10713
|
(pattern) => lower.includes(pattern)
|
|
10157
10714
|
);
|
|
@@ -11603,7 +12160,7 @@ var RuntypeClient2 = class {
|
|
|
11603
12160
|
success: true
|
|
11604
12161
|
};
|
|
11605
12162
|
try {
|
|
11606
|
-
for await (const event of streamEvents(response)) {
|
|
12163
|
+
for await (const event of streamEvents(response, { unified: true })) {
|
|
11607
12164
|
collectLocalToolAwait(pausedTools, event);
|
|
11608
12165
|
switch (event.type) {
|
|
11609
12166
|
case "flow_start":
|
|
@@ -12786,6 +13343,7 @@ export {
|
|
|
12786
13343
|
ToolEnsureConflictError,
|
|
12787
13344
|
ToolsEndpoint,
|
|
12788
13345
|
ToolsNamespace,
|
|
13346
|
+
UNIFIED_EVENTS_QUERY,
|
|
12789
13347
|
UsersEndpoint,
|
|
12790
13348
|
applyGeneratedRuntimeToolProposalToDispatchRequest,
|
|
12791
13349
|
attachRuntimeToolsToDispatchRequest,
|
|
@@ -12802,8 +13360,10 @@ export {
|
|
|
12802
13360
|
computeSkillContentHash,
|
|
12803
13361
|
computeSurfaceContentHash,
|
|
12804
13362
|
computeToolContentHash,
|
|
13363
|
+
createAgentEventTranslator,
|
|
12805
13364
|
createClient,
|
|
12806
13365
|
createExternalTool,
|
|
13366
|
+
createFlowEventTranslator,
|
|
12807
13367
|
defaultWorkflow,
|
|
12808
13368
|
defaultWorkflowConfig,
|
|
12809
13369
|
defineAgent,
|
|
@@ -12826,6 +13386,7 @@ export {
|
|
|
12826
13386
|
isDiscoveryToolName,
|
|
12827
13387
|
isMarathonArtifactPath,
|
|
12828
13388
|
isPreservationSensitiveTask,
|
|
13389
|
+
isUnifiedEventType,
|
|
12829
13390
|
isWorkflowHookRef,
|
|
12830
13391
|
listWorkflowHooks,
|
|
12831
13392
|
normalizeAgentDefinition,
|
|
@@ -12848,5 +13409,6 @@ export {
|
|
|
12848
13409
|
shouldInjectEmptySessionNudge,
|
|
12849
13410
|
shouldRequestModelEscalation,
|
|
12850
13411
|
streamEvents,
|
|
12851
|
-
unregisterWorkflowHook
|
|
13412
|
+
unregisterWorkflowHook,
|
|
13413
|
+
withUnifiedEvents
|
|
12852
13414
|
};
|