@runtypelabs/sdk 6.2.0 → 6.3.1

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 CHANGED
@@ -188,14 +188,17 @@ module.exports = __toCommonJS(index_exports);
188
188
  // src/unified-stream-adapter.ts
189
189
  var UNIFIED_EVENTS_QUERY = "events=unified";
190
190
  function withUnifiedEvents(path) {
191
- return path.includes("?") ? `${path}&${UNIFIED_EVENTS_QUERY}` : `${path}?${UNIFIED_EVENTS_QUERY}`;
191
+ return path;
192
192
  }
193
- var UNIFIED_ONLY_EVENT_TYPES = /* @__PURE__ */ new Set([
193
+ var UNIFIED_EVENT_TYPES = /* @__PURE__ */ new Set([
194
194
  "execution_start",
195
195
  "execution_complete",
196
196
  "execution_error",
197
197
  "turn_start",
198
198
  "turn_complete",
199
+ "step_start",
200
+ "step_complete",
201
+ "step_skip",
199
202
  "text_start",
200
203
  "text_delta",
201
204
  "text_complete",
@@ -205,11 +208,25 @@ var UNIFIED_ONLY_EVENT_TYPES = /* @__PURE__ */ new Set([
205
208
  "media_start",
206
209
  "media_delta",
207
210
  "media_complete",
211
+ "artifact_start",
212
+ "artifact_delta",
213
+ "artifact_update",
214
+ "artifact_complete",
215
+ "source",
216
+ "tool_start",
217
+ "tool_input_delta",
218
+ "tool_input_complete",
208
219
  "tool_output_delta",
209
- "await"
220
+ "tool_complete",
221
+ "approval_start",
222
+ "approval_complete",
223
+ "await",
224
+ "error",
225
+ "ping",
226
+ "custom"
210
227
  ]);
211
228
  function isUnifiedEventType(type) {
212
- return typeof type === "string" && UNIFIED_ONLY_EVENT_TYPES.has(type);
229
+ return typeof type === "string" && UNIFIED_EVENT_TYPES.has(type);
213
230
  }
214
231
  function str(value) {
215
232
  return typeof value === "string" ? value : void 0;
@@ -224,14 +241,6 @@ function compact(event) {
224
241
  }
225
242
  return out;
226
243
  }
227
- var LEGACY_FLOW_PASSTHROUGH = /* @__PURE__ */ new Set([
228
- "flow_start",
229
- "flow_complete",
230
- "flow_error",
231
- "flow_await",
232
- "step_await",
233
- "step_delta"
234
- ]);
235
244
  function createFlowEventTranslator() {
236
245
  let currentStepId;
237
246
  let currentStepName;
@@ -242,7 +251,6 @@ function createFlowEventTranslator() {
242
251
  if (!type) return [];
243
252
  const executionId = str(data.executionId);
244
253
  const seq = num(data.seq);
245
- if (LEGACY_FLOW_PASSTHROUGH.has(type)) return [data];
246
254
  switch (type) {
247
255
  case "execution_start":
248
256
  if (data.kind !== "flow") return [];
@@ -259,8 +267,8 @@ function createFlowEventTranslator() {
259
267
  })
260
268
  ];
261
269
  case "step_start": {
262
- const id = str(data.id) ?? str(data.stepId);
263
- const name = str(data.name) ?? str(data.stepName);
270
+ const id = str(data.id);
271
+ const name = str(data.name);
264
272
  currentStepId = id;
265
273
  currentStepName = name;
266
274
  return [
@@ -293,8 +301,8 @@ function createFlowEventTranslator() {
293
301
  })
294
302
  ];
295
303
  case "step_complete": {
296
- const id = str(data.id) ?? str(data.stepId);
297
- const name = str(data.name) ?? str(data.stepName);
304
+ const id = str(data.id);
305
+ const name = str(data.name);
298
306
  const out = compact({
299
307
  type: "step_complete",
300
308
  executionId,
@@ -305,7 +313,7 @@ function createFlowEventTranslator() {
305
313
  stepType: data.stepType,
306
314
  result: data.result,
307
315
  success: data.success,
308
- executionTime: num(data.durationMs) ?? num(data.executionTime),
316
+ executionTime: num(data.durationMs),
309
317
  stopReason: data.stopReason,
310
318
  completedAt: data.completedAt,
311
319
  unresolvedVariables: data.unresolvedVariables,
@@ -358,7 +366,7 @@ function createFlowEventTranslator() {
358
366
  })
359
367
  ];
360
368
  // Channels/markers/tool-family/approval/skip/source/custom/ping the flow
361
- // consumer never read → no legacy event. (A non-terminal `error` is only
369
+ // consumer never read → no callback event. (A non-terminal `error` is only
362
370
  // produced on agent streams; dropping it here avoids a false flow_error.)
363
371
  default:
364
372
  return [];
@@ -379,7 +387,6 @@ function createAgentEventTranslator() {
379
387
  const executionId = str(data.executionId);
380
388
  const seq = num(data.seq);
381
389
  const iter = num(data.iteration) ?? iteration;
382
- if (type.startsWith("agent_")) return [data];
383
390
  switch (type) {
384
391
  case "execution_start":
385
392
  if (data.kind !== "agent") return [];
@@ -586,7 +593,8 @@ function createAgentEventTranslator() {
586
593
  reason: data.reason,
587
594
  parameters: data.parameters,
588
595
  timeout: data.timeout,
589
- startedAt: data.startedAt
596
+ startedAt: data.startedAt,
597
+ externalAgent: data.externalAgent
590
598
  })
591
599
  ];
592
600
  case "approval_complete":
@@ -630,7 +638,8 @@ function createAgentEventTranslator() {
630
638
  totalCost: data.totalCost,
631
639
  totalTokens: data.totalTokens,
632
640
  finalOutput: data.finalOutput,
633
- duration: data.durationMs
641
+ duration: data.durationMs,
642
+ externalAgent: data.externalAgent
634
643
  })
635
644
  ];
636
645
  case "execution_error":
@@ -661,7 +670,7 @@ function createAgentEventTranslator() {
661
670
  return [compact({ type: "agent_ping", executionId, seq, timestamp: data.timestamp })];
662
671
  // turn/text/reasoning open+close markers, step_*, artifact_*, source,
663
672
  // custom (fallback beat / routing), and the skill-fold result envelope
664
- // the SDK never consumed → no legacy event.
673
+ // the SDK never consumed → no callback event.
665
674
  default:
666
675
  return [];
667
676
  }
@@ -723,7 +732,7 @@ function parseFinalBuffer(buffer) {
723
732
  }
724
733
  return null;
725
734
  }
726
- async function processStream(response, callbacks = {}, options = {}) {
735
+ async function processStream(response, callbacks = {}, _options = {}) {
727
736
  if (!response.ok) {
728
737
  const error = new Error(`HTTP ${response.status}: ${response.statusText}`);
729
738
  callbacks.onError?.(error);
@@ -747,18 +756,10 @@ async function processStream(response, callbacks = {}, options = {}) {
747
756
  executionTime: 0,
748
757
  success: true
749
758
  };
750
- let translate = options.unified === true ? createFlowEventTranslator() : null;
751
- const autoDetect = options.unified === void 0;
759
+ const translate = createFlowEventTranslator();
752
760
  const dispatch = (parsed) => {
753
- if (autoDetect && !translate && isUnifiedEventType(parsed?.type)) {
754
- translate = createFlowEventTranslator();
755
- }
756
- if (translate) {
757
- for (const ev of translate(parsed)) {
758
- handleEvent(ev, callbacks, results, flowSummary);
759
- }
760
- } else {
761
- handleEvent(parsed, callbacks, results, flowSummary);
761
+ for (const ev of translate(parsed)) {
762
+ handleEvent(ev, callbacks, results, flowSummary);
762
763
  }
763
764
  };
764
765
  const contentType = response.headers.get("content-type");
@@ -896,17 +897,11 @@ function handleEvent(event, callbacks, results, summary) {
896
897
  break;
897
898
  }
898
899
  }
899
- async function* streamEvents(response, options = {}) {
900
+ async function* streamEvents(response, _options = {}) {
900
901
  if (!response.ok) {
901
902
  throw new Error(`HTTP ${response.status}: ${response.statusText}`);
902
903
  }
903
- let translate = options.unified === true ? createFlowEventTranslator() : null;
904
- const autoDetect = options.unified === void 0;
905
- const maybeDetect = (parsed) => {
906
- if (autoDetect && !translate && isUnifiedEventType(parsed?.type)) {
907
- translate = createFlowEventTranslator();
908
- }
909
- };
904
+ const translate = createFlowEventTranslator();
910
905
  const contentType = response.headers.get("content-type");
911
906
  if (contentType?.includes("application/json")) {
912
907
  try {
@@ -942,12 +937,7 @@ async function* streamEvents(response, options = {}) {
942
937
  } catch {
943
938
  continue;
944
939
  }
945
- maybeDetect(parsed);
946
- if (translate) {
947
- for (const ev of translate(parsed)) yield ev;
948
- } else {
949
- yield parsed;
950
- }
940
+ for (const ev of translate(parsed)) yield ev;
951
941
  }
952
942
  }
953
943
  const finalEvent = parseFinalBuffer(buffer);
@@ -959,12 +949,7 @@ async function* streamEvents(response, options = {}) {
959
949
  parsed = void 0;
960
950
  }
961
951
  if (parsed !== void 0) {
962
- maybeDetect(parsed);
963
- if (translate) {
964
- for (const ev of translate(parsed)) yield ev;
965
- } else {
966
- yield parsed;
967
- }
952
+ for (const ev of translate(parsed)) yield ev;
968
953
  }
969
954
  }
970
955
  } finally {
@@ -975,15 +960,13 @@ async function* streamEvents(response, options = {}) {
975
960
  // src/flow-result.ts
976
961
  var FlowResult = class {
977
962
  /**
978
- * @param options.unified - The response is a unified-vocabulary SSE stream
979
- * (`/dispatch` since the unified-SSE cutover). Set by dispatch call sites;
980
- * left unset by the still-legacy eval stream producer.
963
+ * @param _options Deprecated stream-format options retained for source
964
+ * compatibility. The API wire is always unified.
981
965
  */
982
- constructor(response, summary, options = {}) {
966
+ constructor(response, summary, _options = {}) {
983
967
  this.consumed = false;
984
968
  this.cachedSummary = null;
985
969
  this.response = response;
986
- this.streamOptions = options;
987
970
  if (summary) {
988
971
  this.cachedSummary = summary;
989
972
  this.consumed = true;
@@ -1036,7 +1019,7 @@ var FlowResult = class {
1036
1019
  }
1037
1020
  this.ensureNotConsumed();
1038
1021
  this.consumed = true;
1039
- this.cachedSummary = await processStream(this.response, callbacks, this.streamOptions);
1022
+ this.cachedSummary = await processStream(this.response, callbacks);
1040
1023
  return this.cachedSummary;
1041
1024
  }
1042
1025
  /**
@@ -1889,9 +1872,9 @@ var FlowBuilder = class {
1889
1872
  }
1890
1873
  const response = await client.dispatch(config);
1891
1874
  if (callbacks) {
1892
- return processStream(response, callbacks, { unified: true });
1875
+ return processStream(response, callbacks);
1893
1876
  }
1894
- return new FlowResult(response, void 0, { unified: true });
1877
+ return new FlowResult(response);
1895
1878
  }
1896
1879
  /**
1897
1880
  * Set a run condition (when predicate) on the last added step.
@@ -2023,9 +2006,9 @@ var ClientFlowBuilder = class extends FlowBuilder {
2023
2006
  }
2024
2007
  const response = await dispatchClient.dispatch(config);
2025
2008
  if (runCallbacks) {
2026
- return processStream(response, runCallbacks, { unified: true });
2009
+ return processStream(response, runCallbacks);
2027
2010
  }
2028
- return new FlowResult(response, void 0, { unified: true });
2011
+ return new FlowResult(response);
2029
2012
  }
2030
2013
  };
2031
2014
  function isStreamCallbacks(obj) {
@@ -2050,7 +2033,9 @@ function createExternalTool(config) {
2050
2033
  // src/local-tool-pause.ts
2051
2034
  var PENDING_NAMELESS_KEY = "";
2052
2035
  function collectLocalToolAwait(pausedTools, event) {
2053
- if (event.type !== "flow_await" && event.type !== "step_await") return;
2036
+ if (event.type !== "await" && event.type !== "flow_await" && event.type !== "step_await") {
2037
+ return;
2038
+ }
2054
2039
  const name = typeof event.toolName === "string" && event.toolName ? event.toolName : "";
2055
2040
  const executionId = typeof event.executionId === "string" && event.executionId ? event.executionId : void 0;
2056
2041
  if (name) {
@@ -2382,6 +2367,29 @@ function normalizeCaseInput(input, where) {
2382
2367
  return { role: m.role, content: m.content };
2383
2368
  });
2384
2369
  }
2370
+ if (input.toolMocks !== void 0) {
2371
+ if (!Array.isArray(input.toolMocks)) {
2372
+ throw new Error(`defineEval: ${where} "input.toolMocks" must be an array`);
2373
+ }
2374
+ out.toolMocks = input.toolMocks.map((m, i) => {
2375
+ if (!isPlainObject(m) || typeof m.toolName !== "string" || m.toolName.length === 0) {
2376
+ throw new Error(
2377
+ `defineEval: ${where} "input.toolMocks[${i}]" must be an object with a non-empty "toolName"`
2378
+ );
2379
+ }
2380
+ if (!("output" in m)) {
2381
+ throw new Error(
2382
+ `defineEval: ${where} "input.toolMocks[${i}]" must include "output" (the value the mock returns; use null for none)`
2383
+ );
2384
+ }
2385
+ const mock = { toolName: m.toolName, output: m.output };
2386
+ if (m.input !== void 0) mock.input = m.input;
2387
+ if (typeof m.isError === "boolean") mock.isError = m.isError;
2388
+ if (typeof m.sourceToolExecutionId === "string") mock.sourceToolExecutionId = m.sourceToolExecutionId;
2389
+ if (typeof m.truncated === "boolean") mock.truncated = m.truncated;
2390
+ return mock;
2391
+ });
2392
+ }
2385
2393
  return out;
2386
2394
  }
2387
2395
  function defineEval(input) {
@@ -3545,7 +3553,7 @@ var RuntypeFlowBuilder = class {
3545
3553
  config.options = { ...config.options, streamResponse: true };
3546
3554
  const client = this.getClient();
3547
3555
  const response = await this.dispatchWithPersistedFlow(client, config);
3548
- const result = new FlowResult(response, void 0, { unified: true });
3556
+ const result = new FlowResult(response);
3549
3557
  if (callbacks) {
3550
3558
  await result.stream(callbacks);
3551
3559
  }
@@ -3574,7 +3582,7 @@ var RuntypeFlowBuilder = class {
3574
3582
  config.options = { ...config.options, streamResponse: true };
3575
3583
  const client = this.getClient();
3576
3584
  const response = await this.dispatchWithPersistedFlow(client, config);
3577
- const result = new FlowResult(response, void 0, { unified: true });
3585
+ const result = new FlowResult(response);
3578
3586
  await result.getSummary();
3579
3587
  return result;
3580
3588
  }
@@ -3609,7 +3617,7 @@ var RuntypeFlowBuilder = class {
3609
3617
  onError: (error) => callbacks?.onError?.(error)
3610
3618
  };
3611
3619
  try {
3612
- for await (const event of streamEvents(response, { unified: true })) {
3620
+ for await (const event of streamEvents(response)) {
3613
3621
  collectLocalToolAwait(pausedTools, event);
3614
3622
  switch (event.type) {
3615
3623
  case "flow_start":
@@ -3657,7 +3665,7 @@ var RuntypeFlowBuilder = class {
3657
3665
  if (pausedTools.size === 0 && typeof data.pausedReason === "object" && data.pausedReason !== null && data.pausedReason.type === "local_action") {
3658
3666
  collectLocalToolAwait(pausedTools, {
3659
3667
  ...data.pausedReason,
3660
- type: "flow_await"
3668
+ type: "await"
3661
3669
  });
3662
3670
  }
3663
3671
  return {
@@ -3717,7 +3725,7 @@ var RuntypeFlowBuilder = class {
3717
3725
  streamResponse: isStreaming
3718
3726
  };
3719
3727
  if (isStreaming) {
3720
- currentResponse = await client.requestStream(withUnifiedEvents("/dispatch/resume"), {
3728
+ currentResponse = await client.requestStream("/dispatch/resume", {
3721
3729
  method: "POST",
3722
3730
  body: JSON.stringify(resumeData)
3723
3731
  });
@@ -5983,7 +5991,7 @@ var RuntypeClient = class {
5983
5991
  * Dispatch flow execution (streaming)
5984
5992
  */
5985
5993
  async dispatch(config) {
5986
- return this.requestStream(withUnifiedEvents("/dispatch"), {
5994
+ return this.requestStream("/dispatch", {
5987
5995
  method: "POST",
5988
5996
  body: JSON.stringify(transformRequest(config))
5989
5997
  });
@@ -6354,7 +6362,7 @@ var Runtype = class {
6354
6362
 
6355
6363
  // src/version.ts
6356
6364
  var FALLBACK_VERSION = "0.0.0";
6357
- var SDK_VERSION = "6.2.0".length > 0 ? "6.2.0" : FALLBACK_VERSION;
6365
+ var SDK_VERSION = "6.3.1".length > 0 ? "6.3.1" : FALLBACK_VERSION;
6358
6366
  var RUNTYPE_CLIENT_KIND = "sdk";
6359
6367
  var SDK_USER_AGENT = `runtype-sdk/${SDK_VERSION} (typescript)`;
6360
6368
 
@@ -8905,7 +8913,7 @@ var DispatchEndpoint = class {
8905
8913
  * Dispatch with streaming response
8906
8914
  */
8907
8915
  async executeStream(data) {
8908
- return this.client.requestStream(withUnifiedEvents("/dispatch"), {
8916
+ return this.client.requestStream("/dispatch", {
8909
8917
  method: "POST",
8910
8918
  body: JSON.stringify(data)
8911
8919
  });
@@ -8921,7 +8929,7 @@ var DispatchEndpoint = class {
8921
8929
  */
8922
8930
  async resume(data) {
8923
8931
  if (data.streamResponse) {
8924
- return this.client.requestStream(withUnifiedEvents("/dispatch/resume"), {
8932
+ return this.client.requestStream("/dispatch/resume", {
8925
8933
  method: "POST",
8926
8934
  body: JSON.stringify(data)
8927
8935
  });
@@ -9444,17 +9452,10 @@ async function processAgentStream(body, callbacks) {
9444
9452
  const reader = body.getReader();
9445
9453
  const decoder = new TextDecoder();
9446
9454
  let buffer = "";
9447
- let translate = null;
9455
+ const translate = createAgentEventTranslator();
9448
9456
  const dispatch = (event) => {
9449
- if (!translate && isUnifiedEventType(event.data?.type ?? event.eventType)) {
9450
- translate = createAgentEventTranslator();
9451
- }
9452
- if (translate) {
9453
- for (const legacy of translate(event.data)) {
9454
- dispatchAgentEvent({ eventType: legacy.type, data: legacy }, callbacks);
9455
- }
9456
- } else {
9457
- dispatchAgentEvent(event, callbacks);
9457
+ for (const callbackEvent of translate(event.data)) {
9458
+ dispatchAgentEvent({ eventType: callbackEvent.type, data: callbackEvent }, callbacks);
9458
9459
  }
9459
9460
  };
9460
9461
  try {
@@ -9661,7 +9662,7 @@ var _AgentsEndpoint = class _AgentsEndpoint {
9661
9662
  * ```
9662
9663
  */
9663
9664
  async executeStream(id, data, init) {
9664
- return this.client.requestStream(withUnifiedEvents(`/agents/${id}/execute`), {
9665
+ return this.client.requestStream(`/agents/${id}/execute`, {
9665
9666
  method: "POST",
9666
9667
  body: JSON.stringify({
9667
9668
  ...data,
@@ -10013,19 +10014,16 @@ var _AgentsEndpoint = class _AgentsEndpoint {
10013
10014
  }
10014
10015
  let resumeResponse;
10015
10016
  try {
10016
- resumeResponse = await this.client.requestStream(
10017
- withUnifiedEvents(`/agents/${id}/resume`),
10018
- {
10019
- method: "POST",
10020
- body: JSON.stringify({
10021
- executionId,
10022
- toolOutputs: { [toolName]: toolResult },
10023
- streamResponse: true,
10024
- debugMode: data.debugMode
10025
- }),
10026
- ...abortSignal ? { signal: abortSignal } : {}
10027
- }
10028
- );
10017
+ resumeResponse = await this.client.requestStream(`/agents/${id}/resume`, {
10018
+ method: "POST",
10019
+ body: JSON.stringify({
10020
+ executionId,
10021
+ toolOutputs: { [toolName]: toolResult },
10022
+ streamResponse: true,
10023
+ debugMode: data.debugMode
10024
+ }),
10025
+ ...abortSignal ? { signal: abortSignal } : {}
10026
+ });
10029
10027
  } catch (error) {
10030
10028
  if (abortSignal?.aborted) return finishAborted(executionId);
10031
10029
  throw error;
@@ -12924,7 +12922,7 @@ var RuntypeClient2 = class {
12924
12922
  success: true
12925
12923
  };
12926
12924
  try {
12927
- for await (const event of streamEvents(response, { unified: true })) {
12925
+ for await (const event of streamEvents(response)) {
12928
12926
  collectLocalToolAwait(pausedTools, event);
12929
12927
  switch (event.type) {
12930
12928
  case "flow_start":
@@ -12972,7 +12970,7 @@ var RuntypeClient2 = class {
12972
12970
  if (pausedTools.size === 0 && isObjectRecord(data.pausedReason) && data.pausedReason.type === "local_action") {
12973
12971
  collectLocalToolAwait(pausedTools, {
12974
12972
  ...data.pausedReason,
12975
- type: "flow_await"
12973
+ type: "await"
12976
12974
  });
12977
12975
  }
12978
12976
  return {