@runtypelabs/sdk 11.18.2 → 11.19.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.mjs CHANGED
@@ -221,7 +221,8 @@ function createFlowEventTranslator() {
221
221
  awaitedAt: data.awaitedAt,
222
222
  origin: data.origin,
223
223
  pageOrigin: data.pageOrigin,
224
- annotations: data.annotations
224
+ annotations: data.annotations,
225
+ subagent: data.subagent
225
226
  })];
226
227
  default: return [];
227
228
  }
@@ -869,7 +870,7 @@ var FlowResult = class {
869
870
  */
870
871
  async stream(callbacks = {}) {
871
872
  if (this.consumed && this.cachedSummary) {
872
- if (callbacks.onFlowComplete) callbacks.onFlowComplete({
873
+ if (callbacks.onFlowComplete && !this.cachedSummary.paused) callbacks.onFlowComplete({
873
874
  type: "flow_complete",
874
875
  flowId: this.cachedSummary.flowId,
875
876
  totalSteps: this.cachedSummary.totalSteps,
@@ -1806,6 +1807,13 @@ function createExternalTool(config) {
1806
1807
  };
1807
1808
  }
1808
1809
  // src/local-tool-pause.ts
1810
+ function subagentOf(value) {
1811
+ const subagent = value;
1812
+ return typeof subagent?.toolName === "string" && typeof subagent.parentToolCallId === "string" ? {
1813
+ toolName: subagent.toolName,
1814
+ parentToolCallId: subagent.parentToolCallId
1815
+ } : void 0;
1816
+ }
1809
1817
  /**
1810
1818
  * Fold a unified `await` event, or an SDK stable-callback `flow_await` /
1811
1819
  * `step_await`, into the per-tool paused-state map. Compatibility frames may
@@ -1826,10 +1834,12 @@ function collectLocalToolAwait(pausedTools, event) {
1826
1834
  const pending = pausedTools.get(PENDING_NAMELESS_KEY);
1827
1835
  if (pending) pausedTools.delete(PENDING_NAMELESS_KEY);
1828
1836
  const prev = pausedTools.get(name) ?? pending;
1837
+ const subagent = subagentOf(event.subagent) ?? prev?.subagent;
1829
1838
  pausedTools.set(name, {
1830
1839
  toolName: name,
1831
1840
  executionId: executionId ?? prev?.executionId ?? "",
1832
- parameters: event.parameters ?? prev?.parameters
1841
+ parameters: event.parameters ?? prev?.parameters,
1842
+ ...subagent ? { subagent } : {}
1833
1843
  });
1834
1844
  return;
1835
1845
  }
@@ -1852,6 +1862,55 @@ function resolveBatchExecutionId(pausedTools) {
1852
1862
  for (const tool of pausedTools) if (tool.executionId) return tool.executionId;
1853
1863
  return "";
1854
1864
  }
1865
+ /**
1866
+ * Splits a paused batch into the calls this caller runs and the sub-agent requests it holds no handler for.
1867
+ * Those are left paused on the parent for whoever watches it (an approver, a UI), never thrown as missing.
1868
+ */
1869
+ function partitionLocalToolBatch(pausedTools, hasHandler) {
1870
+ const leftToParent = pausedTools.filter((tool) => tool.subagent && !hasHandler(tool.toolName));
1871
+ return {
1872
+ runnable: pausedTools.filter((tool) => !leftToParent.includes(tool)),
1873
+ leftToParent
1874
+ };
1875
+ }
1876
+ /** The result a run returns when every remaining request belongs to a sub-agent this caller cannot answer. */
1877
+ function pausedOnSubagentEnvelope(executionId, leftToParent) {
1878
+ return {
1879
+ status: "paused",
1880
+ executionId,
1881
+ pausedTools: leftToParent
1882
+ };
1883
+ }
1884
+ /** A run's summary when it stopped on sub-agent requests this caller cannot answer, so every result getter reads it. */
1885
+ function pausedOnSubagentSummary(executionId, leftToParent) {
1886
+ return {
1887
+ flowId: "",
1888
+ flowName: "",
1889
+ totalSteps: 0,
1890
+ successfulSteps: 0,
1891
+ failedSteps: 0,
1892
+ executionTime: 0,
1893
+ results: /* @__PURE__ */ new Map(),
1894
+ success: true,
1895
+ paused: pausedOnSubagentEnvelope(executionId, leftToParent)
1896
+ };
1897
+ }
1898
+ /** The next batch with the sub-agent requests still held from an earlier cycle, each once. */
1899
+ function withCarriedRequests(carried, pausedTools) {
1900
+ const key = (tool) => `${tool.subagent?.parentToolCallId ?? ""}:${tool.toolName}`;
1901
+ const seen = new Set(pausedTools.map(key));
1902
+ return [...pausedTools, ...carried.filter((tool) => !seen.has(key(tool)))];
1903
+ }
1904
+ /** Whether a buffered envelope reached the run's end; one whose follow closed before a terminal frame did not. */
1905
+ function bufferedReachedTerminal(data) {
1906
+ if (typeof data !== "object" || data === null) return true;
1907
+ const { status, events } = data;
1908
+ if (status !== "failed" || !Array.isArray(events)) return true;
1909
+ return events.some((event) => {
1910
+ const type = event?.type;
1911
+ return type === "execution_complete" || type === "execution_error";
1912
+ });
1913
+ }
1855
1914
  // src/detached-reconnect.ts
1856
1915
  /**
1857
1916
  * Transparent reconnect for a DETACHED agent stream (ADR 0020 point 6;
@@ -10791,6 +10850,7 @@ var RuntypeFlowBuilder = class {
10791
10850
  };
10792
10851
  const processStep = async (response) => {
10793
10852
  if (isStreaming) {
10853
+ let reachedTerminal = false;
10794
10854
  const pausedTools = /* @__PURE__ */ new Map();
10795
10855
  const wrappedCallbacks = {
10796
10856
  ...callbacks,
@@ -10819,9 +10879,12 @@ var RuntypeFlowBuilder = class {
10819
10879
  wrappedCallbacks.onStepComplete?.(event.result, event);
10820
10880
  break;
10821
10881
  case "flow_complete":
10882
+ reachedTerminal = true;
10822
10883
  wrappedCallbacks.onFlowComplete?.(event);
10823
10884
  break;
10824
- case "flow_error": wrappedCallbacks.onError?.(new Error(flowErrorMessage(event)));
10885
+ case "flow_error":
10886
+ reachedTerminal = true;
10887
+ wrappedCallbacks.onError?.(new Error(flowErrorMessage(event)));
10825
10888
  }
10826
10889
  }
10827
10890
  } catch (e) {
@@ -10837,7 +10900,8 @@ var RuntypeFlowBuilder = class {
10837
10900
  };
10838
10901
  return {
10839
10902
  done: true,
10840
- result: accumulatedSummary
10903
+ result: accumulatedSummary,
10904
+ terminal: reachedTerminal
10841
10905
  };
10842
10906
  } else {
10843
10907
  const data = await response.json();
@@ -10860,10 +10924,15 @@ var RuntypeFlowBuilder = class {
10860
10924
  }
10861
10925
  return {
10862
10926
  done: true,
10863
- result: data
10927
+ result: data,
10928
+ terminal: bufferedReachedTerminal(data)
10864
10929
  };
10865
10930
  }
10866
10931
  };
10932
+ const pausedOnSubagent = (left) => {
10933
+ const summary = pausedOnSubagentSummary(resolveBatchExecutionId(left), left);
10934
+ return new FlowResult(new Response(JSON.stringify(summary.paused), { headers: { "content-type": "application/json" } }), summary);
10935
+ };
10867
10936
  let currentResponse;
10868
10937
  let carriedRelease = {};
10869
10938
  if (isStreaming) currentResponse = await client.dispatch(config);
@@ -10872,8 +10941,11 @@ var RuntypeFlowBuilder = class {
10872
10941
  carriedRelease = releaseFieldsOf(data);
10873
10942
  currentResponse = new Response(JSON.stringify(data), { headers: { "content-type": "application/json" } });
10874
10943
  }
10944
+ let carried = [];
10875
10945
  while (true) {
10876
- const { done, result } = await processStep(currentResponse);
10946
+ const { done, result, terminal } = await processStep(currentResponse);
10947
+ const pausedNow = result;
10948
+ if (carried.length > 0 && (done ? terminal === false : !(pausedNow.status === "paused" && pausedNow.pausedTools?.length))) return pausedOnSubagent(carried);
10877
10949
  if (done) {
10878
10950
  if (isStreaming) return new FlowResult(new Response(JSON.stringify(result), { headers: { "content-type": "application/json" } }), result);
10879
10951
  return new FlowResult(new Response(JSON.stringify(result), { headers: { "content-type": "application/json" } }));
@@ -10881,8 +10953,11 @@ var RuntypeFlowBuilder = class {
10881
10953
  const pausedResult = result;
10882
10954
  if (pausedResult.status === "paused" && pausedResult.pausedTools?.length) {
10883
10955
  const executionId = resolveBatchExecutionId(pausedResult.pausedTools);
10956
+ const { runnable, leftToParent } = partitionLocalToolBatch(withCarriedRequests(carried, pausedResult.pausedTools), (toolName) => Boolean(localTools[toolName]));
10957
+ if (runnable.length === 0) return pausedOnSubagent(leftToParent);
10958
+ carried = leftToParent;
10884
10959
  try {
10885
- const outputs = await Promise.all(pausedResult.pausedTools.map(async ({ toolName, parameters }) => {
10960
+ const outputs = await Promise.all(runnable.map(async ({ toolName, parameters }) => {
10886
10961
  if (!localTools[toolName]) throw new Error(`Local tool "${toolName}" required but not provided in localTools map`);
10887
10962
  try {
10888
10963
  return [toolName, await localTools[toolName](parameters)];
@@ -14702,9 +14777,15 @@ var SimulationsEndpoint = class {
14702
14777
  async previewVariant(simulationId, variantId, data) {
14703
14778
  return this.client.post(`/simulations/${segment(simulationId)}/variants/${segment(variantId)}/preview`, data);
14704
14779
  }
14705
- /** Discard a run variant's frozen synthesized answers so its next run synthesizes again. */
14706
- async resynthesizeVariant(runId, variantId) {
14707
- return this.client.post(`/simulations/runs/${segment(runId)}/variants/${segment(variantId)}/resynthesize`);
14780
+ /**
14781
+ * Discard a run variant's frozen synthesized answers so its next run synthesizes again; `tools` limits it to those
14782
+ * server tools' answers.
14783
+ */
14784
+ async resynthesizeVariant(runId, variantId, options) {
14785
+ const query = new URLSearchParams();
14786
+ for (const tool of options?.tools ?? []) query.append("tools", tool);
14787
+ const search = query.toString() ? `?${query.toString()}` : "";
14788
+ return this.client.post(`/simulations/runs/${segment(runId)}/variants/${segment(variantId)}/resynthesize${search}`);
14708
14789
  }
14709
14790
  /** Mark a synthesized answer plausible or implausible; `null` clears the review. */
14710
14791
  async reviewSynthesizedAnswer(runId, exchangeId, plausible) {
@@ -14748,7 +14829,7 @@ function transformQueryParams(params) {
14748
14829
  return result;
14749
14830
  }
14750
14831
  // src/version.ts
14751
- const SDK_VERSION = "11.18.2";
14832
+ const SDK_VERSION = "11.19.1";
14752
14833
  /**
14753
14834
  * Canonical client identity the API uses for attribution
14754
14835
  * (`detectActorSource` in apps/api/src/lib/audit-log.ts keys `'sdk'` off the
@@ -14765,6 +14846,14 @@ const RUNTYPE_CLIENT_KIND = "sdk";
14765
14846
  */
14766
14847
  const SDK_USER_AGENT = `runtype-sdk/${SDK_VERSION} (typescript)`;
14767
14848
  // src/client.ts
14849
+ /** The handler for a paused tool: its exact name first, then the webmcp:-stripped form, so no namespace shadows one. */
14850
+ function findLocalToolEntry(localTools, toolName) {
14851
+ const BARE_NAME_NAMESPACES = ["webmcp:"];
14852
+ const lookupCandidates = [toolName];
14853
+ for (const ns of BARE_NAME_NAMESPACES) if (toolName.startsWith(ns)) lookupCandidates.push(toolName.slice(ns.length));
14854
+ const matchedKey = lookupCandidates.find((key) => localTools[key]);
14855
+ return matchedKey ? localTools[matchedKey] : void 0;
14856
+ }
14768
14857
  function isObjectRecord(value) {
14769
14858
  return typeof value === "object" && value !== null;
14770
14859
  }
@@ -14893,6 +14982,7 @@ var RuntypeClient = class {
14893
14982
  };
14894
14983
  const processStep = async (response) => {
14895
14984
  if (isStreaming) {
14985
+ let reachedTerminal = false;
14896
14986
  const pausedTools = /* @__PURE__ */ new Map();
14897
14987
  const wrappedCallbacks = {
14898
14988
  ...callbacks,
@@ -14925,9 +15015,12 @@ var RuntypeClient = class {
14925
15015
  wrappedCallbacks.onStepComplete?.(event.result, event);
14926
15016
  break;
14927
15017
  case "flow_complete":
15018
+ reachedTerminal = true;
14928
15019
  wrappedCallbacks.onFlowComplete?.(event);
14929
15020
  break;
14930
- case "flow_error": wrappedCallbacks.onError?.(new Error(flowErrorMessage(event)));
15021
+ case "flow_error":
15022
+ reachedTerminal = true;
15023
+ wrappedCallbacks.onError?.(new Error(flowErrorMessage(event)));
14931
15024
  }
14932
15025
  }
14933
15026
  } catch (e) {
@@ -14943,7 +15036,8 @@ var RuntypeClient = class {
14943
15036
  };
14944
15037
  return {
14945
15038
  done: true,
14946
- result: summary
15039
+ result: summary,
15040
+ terminal: reachedTerminal
14947
15041
  };
14948
15042
  } else {
14949
15043
  const data = await response.json();
@@ -14966,10 +15060,16 @@ var RuntypeClient = class {
14966
15060
  }
14967
15061
  return {
14968
15062
  done: true,
14969
- result: data
15063
+ result: data,
15064
+ terminal: bufferedReachedTerminal(data)
14970
15065
  };
14971
15066
  }
14972
15067
  };
15068
+ const pausedOnSubagent = (left) => {
15069
+ const summary = pausedOnSubagentSummary(resolveBatchExecutionId(left), left);
15070
+ if (isStreaming) return summary;
15071
+ return new FlowResult(new Response(JSON.stringify(summary.paused), { headers: { "content-type": "application/json" } }), summary);
15072
+ };
14973
15073
  let currentResponse;
14974
15074
  let carriedRelease = {};
14975
15075
  if (isStreaming) currentResponse = await this.dispatch.executeStream(modifiedRequest);
@@ -14978,21 +15078,22 @@ var RuntypeClient = class {
14978
15078
  carriedRelease = releaseFieldsOf(data);
14979
15079
  currentResponse = new Response(JSON.stringify(data), { headers: { "content-type": "application/json" } });
14980
15080
  }
15081
+ let carried = [];
14981
15082
  while (true) {
14982
- const { done, result } = await processStep(currentResponse);
15083
+ const { done, result, terminal } = await processStep(currentResponse);
15084
+ if (carried.length > 0 && (done ? terminal === false : !isPausedLocalActionResult(result))) return pausedOnSubagent(carried);
14983
15085
  if (done) {
14984
15086
  if (isStreaming) return result;
14985
15087
  return new FlowResult(new Response(JSON.stringify(result), { headers: { "content-type": "application/json" } }));
14986
15088
  }
14987
15089
  if (isPausedLocalActionResult(result)) {
14988
15090
  const executionId = resolveBatchExecutionId(result.pausedTools);
15091
+ const { runnable, leftToParent } = partitionLocalToolBatch(withCarriedRequests(carried, result.pausedTools), (toolName) => Boolean(findLocalToolEntry(localTools, toolName)));
15092
+ if (runnable.length === 0) return pausedOnSubagent(leftToParent);
15093
+ carried = leftToParent;
14989
15094
  try {
14990
- const outputs = await Promise.all(result.pausedTools.map(async ({ toolName, parameters }) => {
14991
- const BARE_NAME_NAMESPACES = ["webmcp:"];
14992
- const lookupCandidates = [toolName];
14993
- for (const ns of BARE_NAME_NAMESPACES) if (toolName.startsWith(ns)) lookupCandidates.push(toolName.slice(ns.length));
14994
- const matchedKey = lookupCandidates.find((key) => localTools[key]);
14995
- const toolEntry = matchedKey ? localTools[matchedKey] : void 0;
15095
+ const outputs = await Promise.all(runnable.map(async ({ toolName, parameters }) => {
15096
+ const toolEntry = findLocalToolEntry(localTools, toolName);
14996
15097
  if (!toolEntry) throw new Error(`Local tool "${toolName}" required but not provided in localTools map`);
14997
15098
  const handler = hasToolEntryShape(toolEntry) ? toolEntry.execute : toolEntry;
14998
15099
  try {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@runtypelabs/sdk",
3
- "version": "11.18.2",
3
+ "version": "11.19.1",
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",
@@ -24,7 +24,7 @@
24
24
  ],
25
25
  "dependencies": {},
26
26
  "devDependencies": {
27
- "@runtypelabs/shared": "3.113.0",
27
+ "@runtypelabs/shared": "3.114.1",
28
28
  "openapi-typescript": "^7.13.0",
29
29
  "tsdown": "^0.23.2",
30
30
  "typescript": "^6.0.3",