@ai-sdk/harness-claude-code 1.0.95 → 1.0.97

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
@@ -93,7 +93,8 @@ import { homedir } from "os";
93
93
  import { join } from "path";
94
94
  import {
95
95
  createCredentialRequestTransformation,
96
- getAiGatewayAuthFromEnv
96
+ getAiGatewayAuthFromEnv,
97
+ isHarnessAuthenticationEnvironment
97
98
  } from "@ai-sdk/harness/utils";
98
99
  var CLAUDE_CODE_CREDENTIAL_ENVIRONMENT_VARIABLES = [
99
100
  "AI_GATEWAY_API_KEY",
@@ -136,70 +137,49 @@ function createClaudeCodeRequestTransformations({
136
137
  return transformations;
137
138
  }
138
139
  function resolveClaudeCodeEnv(auth, processEnv = process.env, options = {}) {
139
- const normalizedAuth = normalizeClaudeCodeAuthToLegacyAuth(auth);
140
- const readApiKey = options.readApiKeyHelper ?? readApiKeyHelper;
141
- if (normalizedAuth?.anthropic) {
140
+ const suppliedEnvironment = isHarnessAuthenticationEnvironment(auth);
141
+ const authenticationEnvironment = suppliedEnvironment ? auth : processEnv;
142
+ const readApiKey = suppliedEnvironment || auth === "direct" ? () => void 0 : options.readApiKeyHelper ?? readApiKeyHelper;
143
+ if (auth === "direct") {
142
144
  return pickAnthropic({
143
- explicit: normalizedAuth.anthropic,
144
- processEnv,
145
+ processEnv: authenticationEnvironment,
145
146
  readApiKey
146
147
  });
147
148
  }
148
- const gatewayAuthFromEnv = getAiGatewayAuthFromEnv({ env: processEnv });
149
- if (normalizedAuth?.gateway) {
150
- return pickGateway({
151
- explicit: normalizedAuth.gateway,
152
- gatewayAuthFromEnv
153
- });
154
- }
155
- if (gatewayAuthFromEnv.apiKey) {
156
- return pickGateway({
157
- explicit: {},
158
- gatewayAuthFromEnv
159
- });
149
+ const gatewayAuthFromEnv = getAiGatewayAuthFromEnv({
150
+ env: authenticationEnvironment
151
+ });
152
+ if (auth === "ai-gateway" || gatewayAuthFromEnv.apiKey) {
153
+ return pickGateway({ gatewayAuthFromEnv });
160
154
  }
161
- return pickAnthropic({ processEnv, readApiKey });
155
+ return pickAnthropic({
156
+ processEnv: authenticationEnvironment,
157
+ readApiKey
158
+ });
162
159
  }
163
160
  function resolveClaudeCodeAuthenticationMode(auth, processEnv = process.env) {
164
- if (auth === "direct" || typeof auth !== "string" && auth?.anthropic) {
161
+ if (isHarnessAuthenticationEnvironment(auth)) {
162
+ return getAiGatewayAuthFromEnv({ env: auth }).apiKey ? "ai-gateway" : "direct";
163
+ }
164
+ if (auth === "direct") {
165
165
  return "direct";
166
166
  }
167
- if (auth === "ai-gateway" || typeof auth !== "string" && auth?.gateway) {
167
+ if (auth === "ai-gateway") {
168
168
  return "ai-gateway";
169
169
  }
170
170
  return getAiGatewayAuthFromEnv({ env: processEnv }).apiKey ? "ai-gateway" : "direct";
171
171
  }
172
- function normalizeClaudeCodeAuthToLegacyAuth(auth) {
173
- if (auth == null || auth === "auto") {
174
- return void 0;
175
- }
176
- if (typeof auth === "string") {
177
- switch (auth) {
178
- case "direct":
179
- return { anthropic: {} };
180
- case "ai-gateway":
181
- return { gateway: {} };
182
- default:
183
- return void 0;
184
- }
185
- }
186
- console.warn(
187
- '[claude-code] Passing an object to auth options is deprecated. Use a string mode ("auto" | "direct" | "ai-gateway") instead, and pass credentials via environment variables.'
188
- );
189
- return auth;
190
- }
191
172
  function pickAnthropic({
192
- explicit,
193
173
  processEnv,
194
174
  readApiKey
195
175
  }) {
196
176
  const env = {};
197
- const helperKey = explicit ? void 0 : readApiKey();
198
- const apiKey = explicit?.apiKey ?? processEnv.ANTHROPIC_API_KEY ?? helperKey;
199
- const authToken = explicit?.authToken ?? processEnv.ANTHROPIC_AUTH_TOKEN ?? helperKey;
177
+ const helperKey = readApiKey();
178
+ const apiKey = processEnv.ANTHROPIC_API_KEY ?? helperKey;
179
+ const authToken = processEnv.ANTHROPIC_AUTH_TOKEN ?? helperKey;
200
180
  if (apiKey) env.ANTHROPIC_API_KEY = apiKey;
201
181
  if (authToken) env.ANTHROPIC_AUTH_TOKEN = authToken;
202
- const baseUrl = explicit?.baseUrl ?? processEnv.ANTHROPIC_BASE_URL;
182
+ const baseUrl = processEnv.ANTHROPIC_BASE_URL;
203
183
  if (baseUrl) env.ANTHROPIC_BASE_URL = baseUrl;
204
184
  return env;
205
185
  }
@@ -232,11 +212,10 @@ function readApiKeyHelper() {
232
212
  }
233
213
  }
234
214
  function pickGateway({
235
- explicit,
236
215
  gatewayAuthFromEnv
237
216
  }) {
238
- const apiKey = explicit.apiKey ?? gatewayAuthFromEnv.apiKey;
239
- const baseUrl = explicit.baseUrl ?? gatewayAuthFromEnv.baseUrl;
217
+ const apiKey = gatewayAuthFromEnv.apiKey;
218
+ const baseUrl = gatewayAuthFromEnv.baseUrl;
240
219
  const env = {};
241
220
  if (apiKey) {
242
221
  env.AI_GATEWAY_API_KEY = apiKey;
@@ -287,7 +266,7 @@ var inboundMessageSchema = z.discriminatedUnion("type", [
287
266
  ]);
288
267
 
289
268
  // src/version.ts
290
- var VERSION = true ? "1.0.95" : "0.0.0-test";
269
+ var VERSION = true ? "1.0.97" : "0.0.0-test";
291
270
 
292
271
  // src/claude-code-harness.ts
293
272
  var CLAUDE_CODE_CLIENT_APP = `ai-sdk/harness-claude-code/${VERSION}`;
@@ -913,6 +892,7 @@ function createClaudeCode(settings = {}) {
913
892
  lifecycleStateSchema: claudeCodeResumeStateSchema,
914
893
  getBootstrap: getClaudeCodeBootstrap,
915
894
  doStart: async (startOpts) => {
895
+ const model = startOpts.model ?? settings.model;
916
896
  const sandboxSession = startOpts.sandboxSession;
917
897
  const toolSafeSandboxSession = getRestrictedSandboxSession(sandboxSession);
918
898
  const sandboxId = "id" in sandboxSession ? sandboxSession.id : void 0;
@@ -986,6 +966,10 @@ function createClaudeCode(settings = {}) {
986
966
  CLAUDE_CODE_BOOTSTRAP_DIR
987
967
  );
988
968
  const workDir = startOpts.sessionWorkDir;
969
+ const sandboxHomeDir = await resolveSandboxHomeDir({
970
+ sandbox: toolSafeSandboxSession,
971
+ abortSignal: startOpts.abortSignal
972
+ });
989
973
  const sessionDataDir = `${defaultWorkingDirectory}/.agent-runs/${startOpts.sessionId}`;
990
974
  const bridgeStateDir = `${sessionDataDir}/bridge`;
991
975
  const timeoutMs = settings.startupTimeoutMs ?? 12e4;
@@ -1036,7 +1020,7 @@ function createClaudeCode(settings = {}) {
1036
1020
  // process handle. The session lifecycle method decides whether the
1037
1021
  // sandbox is left running, stopped, or destroyed.
1038
1022
  proc: void 0,
1039
- model: settings.model,
1023
+ model,
1040
1024
  maxTurns: settings.maxTurns,
1041
1025
  env: sandboxClaudeEnvironment,
1042
1026
  thinking,
@@ -1051,7 +1035,8 @@ function createClaudeCode(settings = {}) {
1051
1035
  debug: startOpts.observability?.debug,
1052
1036
  permissionMode: startOpts.permissionMode,
1053
1037
  builtinToolFiltering: startOpts.builtinToolFiltering,
1054
- skills: startOpts.skills ?? [],
1038
+ sandbox: toolSafeSandboxSession,
1039
+ sandboxHomeDir,
1055
1040
  mcpServers: settings.mcpServers,
1056
1041
  supportsUserMessageResponses: () => supportsUserMessageResponses
1057
1042
  });
@@ -1070,10 +1055,6 @@ function createClaudeCode(settings = {}) {
1070
1055
  respawnStrategy = "replay";
1071
1056
  }
1072
1057
  }
1073
- const sandboxHomeDir = startOpts.skills && startOpts.skills.length > 0 ? await resolveSandboxHomeDir({
1074
- sandbox: toolSafeSandboxSession,
1075
- abortSignal: startOpts.abortSignal
1076
- }) : void 0;
1077
1058
  const port = resolveBridgePort({
1078
1059
  sandboxSession,
1079
1060
  override: settings.port
@@ -1082,7 +1063,6 @@ function createClaudeCode(settings = {}) {
1082
1063
  const env = {
1083
1064
  BRIDGE_CHANNEL_TOKEN: token,
1084
1065
  BRIDGE_WS_PORT: String(port),
1085
- ...sandboxHomeDir ? { HOME: sandboxHomeDir } : {},
1086
1066
  ...respawnStrategy === "replay" ? { BRIDGE_REPLAY_FROM_DISK: "1" } : {}
1087
1067
  };
1088
1068
  if (respawnStrategy === void 0) {
@@ -1090,17 +1070,6 @@ function createClaudeCode(settings = {}) {
1090
1070
  command: `mkdir -p ${shellQuote(workDir)} ${shellQuote(bridgeStateDir)}`,
1091
1071
  abortSignal: startOpts.abortSignal
1092
1072
  });
1093
- if (startOpts.skills && startOpts.skills.length > 0) {
1094
- if (!sandboxHomeDir) {
1095
- throw new Error("Unable to resolve sandbox HOME directory.");
1096
- }
1097
- await writeClaudeCodeSkills({
1098
- sandbox: toolSafeSandboxSession,
1099
- homeDir: sandboxHomeDir,
1100
- skills: startOpts.skills,
1101
- abortSignal: startOpts.abortSignal
1102
- });
1103
- }
1104
1073
  }
1105
1074
  await markBridgeStarting({
1106
1075
  sandbox: toolSafeSandboxSession,
@@ -1167,7 +1136,7 @@ function createClaudeCode(settings = {}) {
1167
1136
  sessionId: startOpts.sessionId,
1168
1137
  channel,
1169
1138
  proc,
1170
- model: settings.model,
1139
+ model,
1171
1140
  maxTurns: settings.maxTurns,
1172
1141
  env: sandboxClaudeEnvironment,
1173
1142
  thinking,
@@ -1182,7 +1151,8 @@ function createClaudeCode(settings = {}) {
1182
1151
  debug: startOpts.observability?.debug,
1183
1152
  permissionMode: startOpts.permissionMode,
1184
1153
  builtinToolFiltering: startOpts.builtinToolFiltering,
1185
- skills: startOpts.skills ?? [],
1154
+ sandbox: toolSafeSandboxSession,
1155
+ sandboxHomeDir,
1186
1156
  mcpServers: settings.mcpServers,
1187
1157
  supportsUserMessageResponses: () => supportsUserMessageResponses
1188
1158
  });
@@ -1430,7 +1400,8 @@ function createSession({
1430
1400
  debug,
1431
1401
  permissionMode,
1432
1402
  builtinToolFiltering,
1433
- skills,
1403
+ sandbox,
1404
+ sandboxHomeDir,
1434
1405
  mcpServers,
1435
1406
  supportsUserMessageResponses
1436
1407
  }) {
@@ -1570,10 +1541,19 @@ function createSession({
1570
1541
  harnessId: "claude-code"
1571
1542
  });
1572
1543
  }
1544
+ await writeClaudeCodeSkills({
1545
+ sandbox,
1546
+ homeDir: sandboxHomeDir,
1547
+ skills: promptOpts.skills,
1548
+ abortSignal: promptOpts.abortSignal
1549
+ });
1573
1550
  const control = wireTurn({
1574
1551
  emit: promptOpts.emit,
1575
1552
  abortSignal: promptOpts.abortSignal
1576
1553
  });
1554
+ if (promptOpts.abortSignal?.aborted) {
1555
+ return control;
1556
+ }
1577
1557
  const startMessage = {
1578
1558
  type: "start",
1579
1559
  prompt: extractUserText(promptOpts.prompt),
@@ -1589,7 +1569,7 @@ function createSession({
1589
1569
  ...env !== void 0 ? { env } : {},
1590
1570
  thinking,
1591
1571
  ...effort !== void 0 ? { effort } : {},
1592
- ...skills.length > 0 ? { skills: skills.map((skill) => skill.name) } : {},
1572
+ ...promptOpts.skills.length > 0 ? { skills: promptOpts.skills.map((skill) => skill.name) } : {},
1593
1573
  ...mcpServers == null ? {} : { mcpServers },
1594
1574
  ...permissionMode ? { permissionMode } : {},
1595
1575
  ...builtinToolFiltering ? { builtinToolFiltering } : {},
@@ -1607,11 +1587,17 @@ function createSession({
1607
1587
  harnessId: "claude-code"
1608
1588
  });
1609
1589
  }
1590
+ await writeClaudeCodeSkills({
1591
+ sandbox,
1592
+ homeDir: sandboxHomeDir,
1593
+ skills: continueOpts.skills,
1594
+ abortSignal: continueOpts.abortSignal
1595
+ });
1610
1596
  const control = wireTurn({
1611
1597
  emit: continueOpts.emit,
1612
1598
  abortSignal: continueOpts.abortSignal
1613
1599
  });
1614
- if (rerunContinue) {
1600
+ if (rerunContinue && !continueOpts.abortSignal?.aborted) {
1615
1601
  pendingResumeFlag = false;
1616
1602
  channel.send({
1617
1603
  type: "start",
@@ -1635,7 +1621,7 @@ function createSession({
1635
1621
  ...env !== void 0 ? { env } : {},
1636
1622
  thinking,
1637
1623
  ...effort !== void 0 ? { effort } : {},
1638
- ...skills.length > 0 ? { skills: skills.map((skill) => skill.name) } : {},
1624
+ ...continueOpts.skills.length > 0 ? { skills: continueOpts.skills.map((skill) => skill.name) } : {},
1639
1625
  ...mcpServers == null ? {} : { mcpServers },
1640
1626
  ...permissionMode ? { permissionMode } : {},
1641
1627
  ...builtinToolFiltering ? { builtinToolFiltering } : {},