@byok-sdk/client 0.4.1 → 0.4.2

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.js CHANGED
@@ -878,6 +878,24 @@ function mapPermissionPolicyToPiArgs(policy) {
878
878
  }
879
879
 
880
880
  // src/adapters/pi/events.ts
881
+ function requireToolCallId(msg) {
882
+ if (typeof msg.toolCallId === "string" && msg.toolCallId.trim().length > 0) return msg.toolCallId;
883
+ throw new RuntimeExecutionFailure({
884
+ phase: "run",
885
+ category: "authority",
886
+ retry: "non-retryable",
887
+ reason: `pi ${msg.type} frame had no authoritative tool call id`
888
+ });
889
+ }
890
+ function requireToolResultOutcome(msg) {
891
+ if (typeof msg.isError === "boolean") return msg.isError;
892
+ throw new RuntimeExecutionFailure({
893
+ phase: "run",
894
+ category: "authority",
895
+ retry: "non-retryable",
896
+ reason: "pi tool_execution_end frame had no authoritative isError outcome"
897
+ });
898
+ }
881
899
  function mapPiMessageToAgentEvent(msg) {
882
900
  switch (msg.type) {
883
901
  case "message_update": {
@@ -888,16 +906,22 @@ function mapPiMessageToAgentEvent(msg) {
888
906
  return void 0;
889
907
  }
890
908
  case "tool_execution_start": {
909
+ const toolCallId = requireToolCallId(msg);
891
910
  if (typeof msg.toolName !== "string") return void 0;
892
- return { type: "tool_use", tool: msg.toolName, input: msg.args };
911
+ return { type: "tool_use", tool: msg.toolName, input: msg.args, toolCallId };
893
912
  }
894
913
  case "tool_execution_end": {
914
+ const toolCallId = requireToolCallId(msg);
915
+ const isError = requireToolResultOutcome(msg);
895
916
  if (typeof msg.toolName !== "string") return void 0;
896
- return {
917
+ const event = {
897
918
  type: "tool_result",
898
919
  tool: msg.toolName,
899
- output: { result: msg.result, isError: msg.isError === true }
920
+ output: { result: msg.result },
921
+ toolCallId,
922
+ isError
900
923
  };
924
+ return event;
901
925
  }
902
926
  case "agent_settled":
903
927
  return { type: "turn_end" };
@@ -1909,6 +1933,17 @@ function subtractDenied(tools, denyTools) {
1909
1933
  function createToolUseCorrelation() {
1910
1934
  return { toolNameByUseId: /* @__PURE__ */ new Map() };
1911
1935
  }
1936
+ function missingToolCallIdFailure(frame) {
1937
+ return new RuntimeExecutionFailure({
1938
+ phase: "run",
1939
+ category: "authority",
1940
+ retry: "non-retryable",
1941
+ reason: `claude ${frame} frame had no authoritative tool call id`
1942
+ });
1943
+ }
1944
+ function isAuthoritativeToolCallId(value) {
1945
+ return typeof value === "string" && value.trim().length > 0;
1946
+ }
1912
1947
  var ROUTINE_CLAUDE_SYSTEM_SUBTYPES = /* @__PURE__ */ new Set([
1913
1948
  "init",
1914
1949
  "hook_started",
@@ -1952,9 +1987,12 @@ function mapAssistant(msg, correlation) {
1952
1987
  }
1953
1988
  break;
1954
1989
  case "tool_use":
1955
- if (typeof block.id === "string" && typeof block.name === "string") {
1990
+ if (!isAuthoritativeToolCallId(block.id)) {
1991
+ return { events: [], terminalFailure: missingToolCallIdFailure("tool_use") };
1992
+ }
1993
+ if (typeof block.name === "string") {
1956
1994
  correlation.toolNameByUseId.set(block.id, block.name);
1957
- events.push({ type: "tool_use", tool: block.name, input: block.input });
1995
+ events.push({ type: "tool_use", tool: block.name, input: block.input, toolCallId: block.id });
1958
1996
  }
1959
1997
  break;
1960
1998
  // Deliberately NOT mapped to `progress` — mirrors pi's own choice to
@@ -1990,11 +2028,20 @@ function mapUser(msg, correlation, options) {
1990
2028
  unmappedLabel = unmappedLabel ?? `user-block:${String(block.type)}`;
1991
2029
  continue;
1992
2030
  }
1993
- const toolUseId = typeof block.tool_use_id === "string" ? block.tool_use_id : void 0;
1994
- const tool = toolUseId && correlation.toolNameByUseId.get(toolUseId) || "unknown";
1995
- const isError = block.is_error === true;
1996
- events.push({ type: "tool_result", tool, output: { content: block.content, isError } });
1997
- if (!isError && FILE_WRITING_TOOLS.has(tool)) {
2031
+ if (!isAuthoritativeToolCallId(block.tool_use_id)) {
2032
+ return { events: [], terminalFailure: missingToolCallIdFailure("tool_result") };
2033
+ }
2034
+ const tool = correlation.toolNameByUseId.get(block.tool_use_id) ?? "unknown";
2035
+ const isError = typeof block.is_error === "boolean" ? block.is_error : void 0;
2036
+ const event = {
2037
+ type: "tool_result",
2038
+ tool,
2039
+ output: { content: block.content },
2040
+ toolCallId: block.tool_use_id
2041
+ };
2042
+ if (isError !== void 0) event.isError = isError;
2043
+ events.push(event);
2044
+ if (isError === false && FILE_WRITING_TOOLS.has(tool)) {
1998
2045
  const artifact = tryBuildArtifactEvent(msg, options.workspaceDir);
1999
2046
  if (artifact) events.push(artifact);
2000
2047
  }
@@ -2617,8 +2664,8 @@ var ClaudeSession = class {
2617
2664
  for (; ; ) {
2618
2665
  const buffered = pending.shift();
2619
2666
  if (buffered) return { value: buffered, done: false };
2667
+ if (terminalFailure) throw terminalFailure;
2620
2668
  if (turnSettled) {
2621
- if (terminalFailure) throw terminalFailure;
2622
2669
  return { value: void 0, done: true };
2623
2670
  }
2624
2671
  let raw;
@@ -2820,6 +2867,15 @@ function extractCodexUsageEvent(rawUsage) {
2820
2867
  function toNonNegativeInt2(value) {
2821
2868
  return typeof value === "number" && Number.isInteger(value) && value >= 0 ? value : void 0;
2822
2869
  }
2870
+ function requireToolCallId2(item) {
2871
+ if (typeof item.id === "string" && item.id.trim().length > 0) return item.id;
2872
+ throw new RuntimeExecutionFailure({
2873
+ phase: "run",
2874
+ category: "authority",
2875
+ retry: "non-retryable",
2876
+ reason: "codex tool item had no authoritative tool call id"
2877
+ });
2878
+ }
2823
2879
  function mapItem(rawItem, phase, workspaceDir) {
2824
2880
  if (!rawItem || typeof rawItem !== "object") return [];
2825
2881
  const item = rawItem;
@@ -2830,9 +2886,10 @@ function mapItem(rawItem, phase, workspaceDir) {
2830
2886
  return typeof item.text === "string" ? [{ type: "progress", text: item.text }] : [];
2831
2887
  }
2832
2888
  case "command_execution": {
2889
+ const toolCallId = requireToolCallId2(item);
2833
2890
  const command = typeof item.command === "string" ? item.command : void 0;
2834
2891
  if (phase === "started") {
2835
- return command !== void 0 ? [{ type: "tool_use", tool: "command_execution", input: { command } }] : [];
2892
+ return command !== void 0 ? [{ type: "tool_use", tool: "command_execution", input: { command }, toolCallId }] : [];
2836
2893
  }
2837
2894
  return [
2838
2895
  {
@@ -2843,17 +2900,19 @@ function mapItem(rawItem, phase, workspaceDir) {
2843
2900
  aggregatedOutput: item.aggregated_output,
2844
2901
  exitCode: item.exit_code,
2845
2902
  status: item.status
2846
- }
2903
+ },
2904
+ toolCallId
2847
2905
  }
2848
2906
  ];
2849
2907
  }
2850
2908
  case "file_change": {
2909
+ const toolCallId = requireToolCallId2(item);
2851
2910
  const changes = Array.isArray(item.changes) ? item.changes : [];
2852
2911
  if (phase === "started") {
2853
- return [{ type: "tool_use", tool: "file_change", input: { changes } }];
2912
+ return [{ type: "tool_use", tool: "file_change", input: { changes }, toolCallId }];
2854
2913
  }
2855
2914
  return [
2856
- { type: "tool_result", tool: "file_change", output: { changes, status: item.status } },
2915
+ { type: "tool_result", tool: "file_change", output: { changes, status: item.status }, toolCallId },
2857
2916
  ...extractArtifactEvents(changes, workspaceDir)
2858
2917
  ];
2859
2918
  }
@@ -3276,7 +3335,15 @@ async function runCodexTurn(params) {
3276
3335
  }
3277
3336
  return;
3278
3337
  }
3279
- const mapped = mapCodexEventToAgentEvents(evt, params.workspaceDir);
3338
+ let mapped;
3339
+ try {
3340
+ mapped = mapCodexEventToAgentEvents(evt, params.workspaceDir);
3341
+ } catch (cause) {
3342
+ if (!isRuntimeExecutionFailure(cause)) throw cause;
3343
+ params.terminal.failure = cause;
3344
+ params.queue.end();
3345
+ return;
3346
+ }
3280
3347
  for (const agentEvent of mapped) {
3281
3348
  if (agentEvent.type === "turn_end") turnEnded = true;
3282
3349
  params.queue.push(agentEvent);