@runtypelabs/sdk 6.2.0 → 6.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/dist/index.mjs CHANGED
@@ -1,14 +1,17 @@
1
1
  // src/unified-stream-adapter.ts
2
2
  var UNIFIED_EVENTS_QUERY = "events=unified";
3
3
  function withUnifiedEvents(path) {
4
- return path.includes("?") ? `${path}&${UNIFIED_EVENTS_QUERY}` : `${path}?${UNIFIED_EVENTS_QUERY}`;
4
+ return path;
5
5
  }
6
- var UNIFIED_ONLY_EVENT_TYPES = /* @__PURE__ */ new Set([
6
+ var UNIFIED_EVENT_TYPES = /* @__PURE__ */ new Set([
7
7
  "execution_start",
8
8
  "execution_complete",
9
9
  "execution_error",
10
10
  "turn_start",
11
11
  "turn_complete",
12
+ "step_start",
13
+ "step_complete",
14
+ "step_skip",
12
15
  "text_start",
13
16
  "text_delta",
14
17
  "text_complete",
@@ -18,11 +21,25 @@ var UNIFIED_ONLY_EVENT_TYPES = /* @__PURE__ */ new Set([
18
21
  "media_start",
19
22
  "media_delta",
20
23
  "media_complete",
24
+ "artifact_start",
25
+ "artifact_delta",
26
+ "artifact_update",
27
+ "artifact_complete",
28
+ "source",
29
+ "tool_start",
30
+ "tool_input_delta",
31
+ "tool_input_complete",
21
32
  "tool_output_delta",
22
- "await"
33
+ "tool_complete",
34
+ "approval_start",
35
+ "approval_complete",
36
+ "await",
37
+ "error",
38
+ "ping",
39
+ "custom"
23
40
  ]);
24
41
  function isUnifiedEventType(type) {
25
- return typeof type === "string" && UNIFIED_ONLY_EVENT_TYPES.has(type);
42
+ return typeof type === "string" && UNIFIED_EVENT_TYPES.has(type);
26
43
  }
27
44
  function str(value) {
28
45
  return typeof value === "string" ? value : void 0;
@@ -37,14 +54,6 @@ function compact(event) {
37
54
  }
38
55
  return out;
39
56
  }
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
57
  function createFlowEventTranslator() {
49
58
  let currentStepId;
50
59
  let currentStepName;
@@ -55,7 +64,6 @@ function createFlowEventTranslator() {
55
64
  if (!type) return [];
56
65
  const executionId = str(data.executionId);
57
66
  const seq = num(data.seq);
58
- if (LEGACY_FLOW_PASSTHROUGH.has(type)) return [data];
59
67
  switch (type) {
60
68
  case "execution_start":
61
69
  if (data.kind !== "flow") return [];
@@ -72,8 +80,8 @@ function createFlowEventTranslator() {
72
80
  })
73
81
  ];
74
82
  case "step_start": {
75
- const id = str(data.id) ?? str(data.stepId);
76
- const name = str(data.name) ?? str(data.stepName);
83
+ const id = str(data.id);
84
+ const name = str(data.name);
77
85
  currentStepId = id;
78
86
  currentStepName = name;
79
87
  return [
@@ -106,8 +114,8 @@ function createFlowEventTranslator() {
106
114
  })
107
115
  ];
108
116
  case "step_complete": {
109
- const id = str(data.id) ?? str(data.stepId);
110
- const name = str(data.name) ?? str(data.stepName);
117
+ const id = str(data.id);
118
+ const name = str(data.name);
111
119
  const out = compact({
112
120
  type: "step_complete",
113
121
  executionId,
@@ -118,7 +126,7 @@ function createFlowEventTranslator() {
118
126
  stepType: data.stepType,
119
127
  result: data.result,
120
128
  success: data.success,
121
- executionTime: num(data.durationMs) ?? num(data.executionTime),
129
+ executionTime: num(data.durationMs),
122
130
  stopReason: data.stopReason,
123
131
  completedAt: data.completedAt,
124
132
  unresolvedVariables: data.unresolvedVariables,
@@ -171,7 +179,7 @@ function createFlowEventTranslator() {
171
179
  })
172
180
  ];
173
181
  // Channels/markers/tool-family/approval/skip/source/custom/ping the flow
174
- // consumer never read → no legacy event. (A non-terminal `error` is only
182
+ // consumer never read → no callback event. (A non-terminal `error` is only
175
183
  // produced on agent streams; dropping it here avoids a false flow_error.)
176
184
  default:
177
185
  return [];
@@ -192,7 +200,6 @@ function createAgentEventTranslator() {
192
200
  const executionId = str(data.executionId);
193
201
  const seq = num(data.seq);
194
202
  const iter = num(data.iteration) ?? iteration;
195
- if (type.startsWith("agent_")) return [data];
196
203
  switch (type) {
197
204
  case "execution_start":
198
205
  if (data.kind !== "agent") return [];
@@ -399,7 +406,8 @@ function createAgentEventTranslator() {
399
406
  reason: data.reason,
400
407
  parameters: data.parameters,
401
408
  timeout: data.timeout,
402
- startedAt: data.startedAt
409
+ startedAt: data.startedAt,
410
+ externalAgent: data.externalAgent
403
411
  })
404
412
  ];
405
413
  case "approval_complete":
@@ -443,7 +451,8 @@ function createAgentEventTranslator() {
443
451
  totalCost: data.totalCost,
444
452
  totalTokens: data.totalTokens,
445
453
  finalOutput: data.finalOutput,
446
- duration: data.durationMs
454
+ duration: data.durationMs,
455
+ externalAgent: data.externalAgent
447
456
  })
448
457
  ];
449
458
  case "execution_error":
@@ -474,7 +483,7 @@ function createAgentEventTranslator() {
474
483
  return [compact({ type: "agent_ping", executionId, seq, timestamp: data.timestamp })];
475
484
  // turn/text/reasoning open+close markers, step_*, artifact_*, source,
476
485
  // custom (fallback beat / routing), and the skill-fold result envelope
477
- // the SDK never consumed → no legacy event.
486
+ // the SDK never consumed → no callback event.
478
487
  default:
479
488
  return [];
480
489
  }
@@ -536,7 +545,7 @@ function parseFinalBuffer(buffer) {
536
545
  }
537
546
  return null;
538
547
  }
539
- async function processStream(response, callbacks = {}, options = {}) {
548
+ async function processStream(response, callbacks = {}, _options = {}) {
540
549
  if (!response.ok) {
541
550
  const error = new Error(`HTTP ${response.status}: ${response.statusText}`);
542
551
  callbacks.onError?.(error);
@@ -560,18 +569,10 @@ async function processStream(response, callbacks = {}, options = {}) {
560
569
  executionTime: 0,
561
570
  success: true
562
571
  };
563
- let translate = options.unified === true ? createFlowEventTranslator() : null;
564
- const autoDetect = options.unified === void 0;
572
+ const translate = createFlowEventTranslator();
565
573
  const dispatch = (parsed) => {
566
- if (autoDetect && !translate && isUnifiedEventType(parsed?.type)) {
567
- translate = createFlowEventTranslator();
568
- }
569
- if (translate) {
570
- for (const ev of translate(parsed)) {
571
- handleEvent(ev, callbacks, results, flowSummary);
572
- }
573
- } else {
574
- handleEvent(parsed, callbacks, results, flowSummary);
574
+ for (const ev of translate(parsed)) {
575
+ handleEvent(ev, callbacks, results, flowSummary);
575
576
  }
576
577
  };
577
578
  const contentType = response.headers.get("content-type");
@@ -709,17 +710,11 @@ function handleEvent(event, callbacks, results, summary) {
709
710
  break;
710
711
  }
711
712
  }
712
- async function* streamEvents(response, options = {}) {
713
+ async function* streamEvents(response, _options = {}) {
713
714
  if (!response.ok) {
714
715
  throw new Error(`HTTP ${response.status}: ${response.statusText}`);
715
716
  }
716
- let translate = options.unified === true ? createFlowEventTranslator() : null;
717
- const autoDetect = options.unified === void 0;
718
- const maybeDetect = (parsed) => {
719
- if (autoDetect && !translate && isUnifiedEventType(parsed?.type)) {
720
- translate = createFlowEventTranslator();
721
- }
722
- };
717
+ const translate = createFlowEventTranslator();
723
718
  const contentType = response.headers.get("content-type");
724
719
  if (contentType?.includes("application/json")) {
725
720
  try {
@@ -755,12 +750,7 @@ async function* streamEvents(response, options = {}) {
755
750
  } catch {
756
751
  continue;
757
752
  }
758
- maybeDetect(parsed);
759
- if (translate) {
760
- for (const ev of translate(parsed)) yield ev;
761
- } else {
762
- yield parsed;
763
- }
753
+ for (const ev of translate(parsed)) yield ev;
764
754
  }
765
755
  }
766
756
  const finalEvent = parseFinalBuffer(buffer);
@@ -772,12 +762,7 @@ async function* streamEvents(response, options = {}) {
772
762
  parsed = void 0;
773
763
  }
774
764
  if (parsed !== void 0) {
775
- maybeDetect(parsed);
776
- if (translate) {
777
- for (const ev of translate(parsed)) yield ev;
778
- } else {
779
- yield parsed;
780
- }
765
+ for (const ev of translate(parsed)) yield ev;
781
766
  }
782
767
  }
783
768
  } finally {
@@ -788,15 +773,13 @@ async function* streamEvents(response, options = {}) {
788
773
  // src/flow-result.ts
789
774
  var FlowResult = class {
790
775
  /**
791
- * @param options.unified - The response is a unified-vocabulary SSE stream
792
- * (`/dispatch` since the unified-SSE cutover). Set by dispatch call sites;
793
- * left unset by the still-legacy eval stream producer.
776
+ * @param _options Deprecated stream-format options retained for source
777
+ * compatibility. The API wire is always unified.
794
778
  */
795
- constructor(response, summary, options = {}) {
779
+ constructor(response, summary, _options = {}) {
796
780
  this.consumed = false;
797
781
  this.cachedSummary = null;
798
782
  this.response = response;
799
- this.streamOptions = options;
800
783
  if (summary) {
801
784
  this.cachedSummary = summary;
802
785
  this.consumed = true;
@@ -849,7 +832,7 @@ var FlowResult = class {
849
832
  }
850
833
  this.ensureNotConsumed();
851
834
  this.consumed = true;
852
- this.cachedSummary = await processStream(this.response, callbacks, this.streamOptions);
835
+ this.cachedSummary = await processStream(this.response, callbacks);
853
836
  return this.cachedSummary;
854
837
  }
855
838
  /**
@@ -1702,9 +1685,9 @@ var FlowBuilder = class {
1702
1685
  }
1703
1686
  const response = await client.dispatch(config);
1704
1687
  if (callbacks) {
1705
- return processStream(response, callbacks, { unified: true });
1688
+ return processStream(response, callbacks);
1706
1689
  }
1707
- return new FlowResult(response, void 0, { unified: true });
1690
+ return new FlowResult(response);
1708
1691
  }
1709
1692
  /**
1710
1693
  * Set a run condition (when predicate) on the last added step.
@@ -1836,9 +1819,9 @@ var ClientFlowBuilder = class extends FlowBuilder {
1836
1819
  }
1837
1820
  const response = await dispatchClient.dispatch(config);
1838
1821
  if (runCallbacks) {
1839
- return processStream(response, runCallbacks, { unified: true });
1822
+ return processStream(response, runCallbacks);
1840
1823
  }
1841
- return new FlowResult(response, void 0, { unified: true });
1824
+ return new FlowResult(response);
1842
1825
  }
1843
1826
  };
1844
1827
  function isStreamCallbacks(obj) {
@@ -1863,7 +1846,9 @@ function createExternalTool(config) {
1863
1846
  // src/local-tool-pause.ts
1864
1847
  var PENDING_NAMELESS_KEY = "";
1865
1848
  function collectLocalToolAwait(pausedTools, event) {
1866
- if (event.type !== "flow_await" && event.type !== "step_await") return;
1849
+ if (event.type !== "await" && event.type !== "flow_await" && event.type !== "step_await") {
1850
+ return;
1851
+ }
1867
1852
  const name = typeof event.toolName === "string" && event.toolName ? event.toolName : "";
1868
1853
  const executionId = typeof event.executionId === "string" && event.executionId ? event.executionId : void 0;
1869
1854
  if (name) {
@@ -2195,6 +2180,29 @@ function normalizeCaseInput(input, where) {
2195
2180
  return { role: m.role, content: m.content };
2196
2181
  });
2197
2182
  }
2183
+ if (input.toolMocks !== void 0) {
2184
+ if (!Array.isArray(input.toolMocks)) {
2185
+ throw new Error(`defineEval: ${where} "input.toolMocks" must be an array`);
2186
+ }
2187
+ out.toolMocks = input.toolMocks.map((m, i) => {
2188
+ if (!isPlainObject(m) || typeof m.toolName !== "string" || m.toolName.length === 0) {
2189
+ throw new Error(
2190
+ `defineEval: ${where} "input.toolMocks[${i}]" must be an object with a non-empty "toolName"`
2191
+ );
2192
+ }
2193
+ if (!("output" in m)) {
2194
+ throw new Error(
2195
+ `defineEval: ${where} "input.toolMocks[${i}]" must include "output" (the value the mock returns; use null for none)`
2196
+ );
2197
+ }
2198
+ const mock = { toolName: m.toolName, output: m.output };
2199
+ if (m.input !== void 0) mock.input = m.input;
2200
+ if (typeof m.isError === "boolean") mock.isError = m.isError;
2201
+ if (typeof m.sourceToolExecutionId === "string") mock.sourceToolExecutionId = m.sourceToolExecutionId;
2202
+ if (typeof m.truncated === "boolean") mock.truncated = m.truncated;
2203
+ return mock;
2204
+ });
2205
+ }
2198
2206
  return out;
2199
2207
  }
2200
2208
  function defineEval(input) {
@@ -3358,7 +3366,7 @@ var RuntypeFlowBuilder = class {
3358
3366
  config.options = { ...config.options, streamResponse: true };
3359
3367
  const client = this.getClient();
3360
3368
  const response = await this.dispatchWithPersistedFlow(client, config);
3361
- const result = new FlowResult(response, void 0, { unified: true });
3369
+ const result = new FlowResult(response);
3362
3370
  if (callbacks) {
3363
3371
  await result.stream(callbacks);
3364
3372
  }
@@ -3387,7 +3395,7 @@ var RuntypeFlowBuilder = class {
3387
3395
  config.options = { ...config.options, streamResponse: true };
3388
3396
  const client = this.getClient();
3389
3397
  const response = await this.dispatchWithPersistedFlow(client, config);
3390
- const result = new FlowResult(response, void 0, { unified: true });
3398
+ const result = new FlowResult(response);
3391
3399
  await result.getSummary();
3392
3400
  return result;
3393
3401
  }
@@ -3422,7 +3430,7 @@ var RuntypeFlowBuilder = class {
3422
3430
  onError: (error) => callbacks?.onError?.(error)
3423
3431
  };
3424
3432
  try {
3425
- for await (const event of streamEvents(response, { unified: true })) {
3433
+ for await (const event of streamEvents(response)) {
3426
3434
  collectLocalToolAwait(pausedTools, event);
3427
3435
  switch (event.type) {
3428
3436
  case "flow_start":
@@ -3470,7 +3478,7 @@ var RuntypeFlowBuilder = class {
3470
3478
  if (pausedTools.size === 0 && typeof data.pausedReason === "object" && data.pausedReason !== null && data.pausedReason.type === "local_action") {
3471
3479
  collectLocalToolAwait(pausedTools, {
3472
3480
  ...data.pausedReason,
3473
- type: "flow_await"
3481
+ type: "await"
3474
3482
  });
3475
3483
  }
3476
3484
  return {
@@ -3530,7 +3538,7 @@ var RuntypeFlowBuilder = class {
3530
3538
  streamResponse: isStreaming
3531
3539
  };
3532
3540
  if (isStreaming) {
3533
- currentResponse = await client.requestStream(withUnifiedEvents("/dispatch/resume"), {
3541
+ currentResponse = await client.requestStream("/dispatch/resume", {
3534
3542
  method: "POST",
3535
3543
  body: JSON.stringify(resumeData)
3536
3544
  });
@@ -5796,7 +5804,7 @@ var RuntypeClient = class {
5796
5804
  * Dispatch flow execution (streaming)
5797
5805
  */
5798
5806
  async dispatch(config) {
5799
- return this.requestStream(withUnifiedEvents("/dispatch"), {
5807
+ return this.requestStream("/dispatch", {
5800
5808
  method: "POST",
5801
5809
  body: JSON.stringify(transformRequest(config))
5802
5810
  });
@@ -6167,7 +6175,7 @@ var Runtype = class {
6167
6175
 
6168
6176
  // src/version.ts
6169
6177
  var FALLBACK_VERSION = "0.0.0";
6170
- var SDK_VERSION = "6.2.0".length > 0 ? "6.2.0" : FALLBACK_VERSION;
6178
+ var SDK_VERSION = "6.3.0".length > 0 ? "6.3.0" : FALLBACK_VERSION;
6171
6179
  var RUNTYPE_CLIENT_KIND = "sdk";
6172
6180
  var SDK_USER_AGENT = `runtype-sdk/${SDK_VERSION} (typescript)`;
6173
6181
 
@@ -8718,7 +8726,7 @@ var DispatchEndpoint = class {
8718
8726
  * Dispatch with streaming response
8719
8727
  */
8720
8728
  async executeStream(data) {
8721
- return this.client.requestStream(withUnifiedEvents("/dispatch"), {
8729
+ return this.client.requestStream("/dispatch", {
8722
8730
  method: "POST",
8723
8731
  body: JSON.stringify(data)
8724
8732
  });
@@ -8734,7 +8742,7 @@ var DispatchEndpoint = class {
8734
8742
  */
8735
8743
  async resume(data) {
8736
8744
  if (data.streamResponse) {
8737
- return this.client.requestStream(withUnifiedEvents("/dispatch/resume"), {
8745
+ return this.client.requestStream("/dispatch/resume", {
8738
8746
  method: "POST",
8739
8747
  body: JSON.stringify(data)
8740
8748
  });
@@ -9257,17 +9265,10 @@ async function processAgentStream(body, callbacks) {
9257
9265
  const reader = body.getReader();
9258
9266
  const decoder = new TextDecoder();
9259
9267
  let buffer = "";
9260
- let translate = null;
9268
+ const translate = createAgentEventTranslator();
9261
9269
  const dispatch = (event) => {
9262
- if (!translate && isUnifiedEventType(event.data?.type ?? event.eventType)) {
9263
- translate = createAgentEventTranslator();
9264
- }
9265
- if (translate) {
9266
- for (const legacy of translate(event.data)) {
9267
- dispatchAgentEvent({ eventType: legacy.type, data: legacy }, callbacks);
9268
- }
9269
- } else {
9270
- dispatchAgentEvent(event, callbacks);
9270
+ for (const callbackEvent of translate(event.data)) {
9271
+ dispatchAgentEvent({ eventType: callbackEvent.type, data: callbackEvent }, callbacks);
9271
9272
  }
9272
9273
  };
9273
9274
  try {
@@ -9474,7 +9475,7 @@ var _AgentsEndpoint = class _AgentsEndpoint {
9474
9475
  * ```
9475
9476
  */
9476
9477
  async executeStream(id, data, init) {
9477
- return this.client.requestStream(withUnifiedEvents(`/agents/${id}/execute`), {
9478
+ return this.client.requestStream(`/agents/${id}/execute`, {
9478
9479
  method: "POST",
9479
9480
  body: JSON.stringify({
9480
9481
  ...data,
@@ -9826,19 +9827,16 @@ var _AgentsEndpoint = class _AgentsEndpoint {
9826
9827
  }
9827
9828
  let resumeResponse;
9828
9829
  try {
9829
- resumeResponse = await this.client.requestStream(
9830
- withUnifiedEvents(`/agents/${id}/resume`),
9831
- {
9832
- method: "POST",
9833
- body: JSON.stringify({
9834
- executionId,
9835
- toolOutputs: { [toolName]: toolResult },
9836
- streamResponse: true,
9837
- debugMode: data.debugMode
9838
- }),
9839
- ...abortSignal ? { signal: abortSignal } : {}
9840
- }
9841
- );
9830
+ resumeResponse = await this.client.requestStream(`/agents/${id}/resume`, {
9831
+ method: "POST",
9832
+ body: JSON.stringify({
9833
+ executionId,
9834
+ toolOutputs: { [toolName]: toolResult },
9835
+ streamResponse: true,
9836
+ debugMode: data.debugMode
9837
+ }),
9838
+ ...abortSignal ? { signal: abortSignal } : {}
9839
+ });
9842
9840
  } catch (error) {
9843
9841
  if (abortSignal?.aborted) return finishAborted(executionId);
9844
9842
  throw error;
@@ -12737,7 +12735,7 @@ var RuntypeClient2 = class {
12737
12735
  success: true
12738
12736
  };
12739
12737
  try {
12740
- for await (const event of streamEvents(response, { unified: true })) {
12738
+ for await (const event of streamEvents(response)) {
12741
12739
  collectLocalToolAwait(pausedTools, event);
12742
12740
  switch (event.type) {
12743
12741
  case "flow_start":
@@ -12785,7 +12783,7 @@ var RuntypeClient2 = class {
12785
12783
  if (pausedTools.size === 0 && isObjectRecord(data.pausedReason) && data.pausedReason.type === "local_action") {
12786
12784
  collectLocalToolAwait(pausedTools, {
12787
12785
  ...data.pausedReason,
12788
- type: "flow_await"
12786
+ type: "await"
12789
12787
  });
12790
12788
  }
12791
12789
  return {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@runtypelabs/sdk",
3
- "version": "6.2.0",
3
+ "version": "6.3.0",
4
4
  "type": "module",
5
5
  "description": "TypeScript SDK for the Runtype API with fluent methods. Use it to quickly realize AI products, agents, and workflows.",
6
6
  "main": "dist/index.cjs",