@ai-sdk/harness-claude-code 1.0.178 → 1.0.180
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/CHANGELOG.md +24 -0
- package/dist/bridge/index.mjs +32 -4
- package/dist/bridge/index.mjs.map +1 -1
- package/dist/index.d.ts +11 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +37 -13
- package/dist/index.js.map +1 -1
- package/package.json +4 -4
- package/src/bridge/index.ts +24 -1
- package/src/bridge/permission-paths.ts +17 -7
- package/src/claude-code-auth.ts +26 -6
- package/src/claude-code-bridge-protocol.ts +1 -0
- package/src/claude-code-harness.ts +22 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ai-sdk/harness-claude-code",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.180",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"sideEffects": false,
|
|
@@ -26,9 +26,9 @@
|
|
|
26
26
|
}
|
|
27
27
|
},
|
|
28
28
|
"dependencies": {
|
|
29
|
-
"@ai-sdk/harness": "1.0.
|
|
30
|
-
"@ai-sdk/provider": "4.0.
|
|
31
|
-
"@ai-sdk/provider-utils": "5.0.
|
|
29
|
+
"@ai-sdk/harness": "1.0.176",
|
|
30
|
+
"@ai-sdk/provider": "4.0.37",
|
|
31
|
+
"@ai-sdk/provider-utils": "5.0.76",
|
|
32
32
|
"ws": "^8.21.0"
|
|
33
33
|
},
|
|
34
34
|
"peerDependencies": {
|
package/src/bridge/index.ts
CHANGED
|
@@ -153,6 +153,9 @@ let lastClaudeSessionId: string | undefined;
|
|
|
153
153
|
await runBridge<StartMessage>({
|
|
154
154
|
bridgeType: 'claude-code',
|
|
155
155
|
bridgeStateDir,
|
|
156
|
+
...(args.hostDisconnectTimeoutMs !== undefined
|
|
157
|
+
? { hostDisconnectTimeoutMs: args.hostDisconnectTimeoutMs }
|
|
158
|
+
: {}),
|
|
156
159
|
onStart: async (start, turn) => {
|
|
157
160
|
const emit: BridgeTurn['emit'] = event => {
|
|
158
161
|
if (turn.abortSignal.aborted) return;
|
|
@@ -256,6 +259,7 @@ function createPermissionOptions(input: {
|
|
|
256
259
|
inactiveNativeTools,
|
|
257
260
|
cwd: input.toolCwds.get(options.toolUseID) ?? '',
|
|
258
261
|
readableSkillRoots: input.start.readableSkillRoots ?? [],
|
|
262
|
+
additionalDirectories: input.start.additionalDirectories ?? [],
|
|
259
263
|
blockedPath: options.blockedPath,
|
|
260
264
|
}))
|
|
261
265
|
) {
|
|
@@ -459,6 +463,7 @@ async function needsHostApproval(input: {
|
|
|
459
463
|
toolInput: unknown;
|
|
460
464
|
cwd: string;
|
|
461
465
|
readableSkillRoots: readonly string[];
|
|
466
|
+
additionalDirectories: readonly string[];
|
|
462
467
|
blockedPath?: string;
|
|
463
468
|
permissionMode: 'allow-reads' | 'allow-edits' | 'allow-all';
|
|
464
469
|
inactiveNativeTools: ReadonlySet<string>;
|
|
@@ -519,6 +524,7 @@ async function needsHostApproval(input: {
|
|
|
519
524
|
paths,
|
|
520
525
|
access,
|
|
521
526
|
readableSkillRoots: input.readableSkillRoots,
|
|
527
|
+
additionalDirectories: input.additionalDirectories,
|
|
522
528
|
}));
|
|
523
529
|
}
|
|
524
530
|
|
|
@@ -536,6 +542,7 @@ function createApprovalPreToolUseHook(input: {
|
|
|
536
542
|
deferredToolUseIds: Set<string>;
|
|
537
543
|
nativeToolCallNames: Map<string, string>;
|
|
538
544
|
readableSkillRoots: readonly string[];
|
|
545
|
+
additionalDirectories: readonly string[];
|
|
539
546
|
toolCwds: Map<string, string>;
|
|
540
547
|
}): HookCallback {
|
|
541
548
|
return async (hookInput, toolUseID): Promise<HookJSONOutput> => {
|
|
@@ -573,6 +580,7 @@ function createApprovalPreToolUseHook(input: {
|
|
|
573
580
|
toolInput: hookInput.tool_input,
|
|
574
581
|
cwd: hookInput.cwd,
|
|
575
582
|
readableSkillRoots: input.readableSkillRoots,
|
|
583
|
+
additionalDirectories: input.additionalDirectories,
|
|
576
584
|
permissionMode: input.permissionMode,
|
|
577
585
|
inactiveNativeTools: input.inactiveNativeTools,
|
|
578
586
|
}))
|
|
@@ -814,6 +822,7 @@ async function runTurn(start: StartMessage, turn: BridgeTurn): Promise<void> {
|
|
|
814
822
|
const approvalPreToolUseHook = createApprovalPreToolUseHook({
|
|
815
823
|
toolCwds,
|
|
816
824
|
readableSkillRoots: start.readableSkillRoots ?? [],
|
|
825
|
+
additionalDirectories: start.additionalDirectories ?? [],
|
|
817
826
|
permissionMode,
|
|
818
827
|
inactiveNativeTools: new Set(inactiveNativeTools),
|
|
819
828
|
decisions: approvalDecisions,
|
|
@@ -845,6 +854,9 @@ async function runTurn(start: StartMessage, turn: BridgeTurn): Promise<void> {
|
|
|
845
854
|
...(start.maxBudgetUsd !== undefined
|
|
846
855
|
? { maxBudgetUsd: start.maxBudgetUsd }
|
|
847
856
|
: {}),
|
|
857
|
+
...(start.additionalDirectories !== undefined
|
|
858
|
+
? { additionalDirectories: start.additionalDirectories }
|
|
859
|
+
: {}),
|
|
848
860
|
...(start.strictMcpConfig !== undefined
|
|
849
861
|
? { strictMcpConfig: start.strictMcpConfig }
|
|
850
862
|
: {}),
|
|
@@ -925,6 +937,7 @@ async function runTurn(start: StartMessage, turn: BridgeTurn): Promise<void> {
|
|
|
925
937
|
...permissionOptions,
|
|
926
938
|
...(Object.keys(settings).length > 0 ? { settings } : {}),
|
|
927
939
|
mcpServers,
|
|
940
|
+
settingSources: ['user'],
|
|
928
941
|
cwd: workdir,
|
|
929
942
|
abortSignal: abortCtl.signal,
|
|
930
943
|
},
|
|
@@ -1515,13 +1528,23 @@ function addUsage(
|
|
|
1515
1528
|
function parseArgs(args: string[]): {
|
|
1516
1529
|
workdir?: string;
|
|
1517
1530
|
bridgeStateDir?: string;
|
|
1531
|
+
hostDisconnectTimeoutMs?: number;
|
|
1518
1532
|
} {
|
|
1519
|
-
const out: {
|
|
1533
|
+
const out: {
|
|
1534
|
+
workdir?: string;
|
|
1535
|
+
bridgeStateDir?: string;
|
|
1536
|
+
hostDisconnectTimeoutMs?: number;
|
|
1537
|
+
} = {};
|
|
1520
1538
|
for (let i = 0; i < args.length; i++) {
|
|
1521
1539
|
if (args[i] === '--workdir' && i + 1 < args.length) {
|
|
1522
1540
|
out.workdir = args[++i];
|
|
1523
1541
|
} else if (args[i] === '--bridge-state-dir' && i + 1 < args.length) {
|
|
1524
1542
|
out.bridgeStateDir = args[++i];
|
|
1543
|
+
} else if (
|
|
1544
|
+
args[i] === '--host-disconnect-timeout-ms' &&
|
|
1545
|
+
i + 1 < args.length
|
|
1546
|
+
) {
|
|
1547
|
+
out.hostDisconnectTimeoutMs = Number(args[++i]);
|
|
1525
1548
|
}
|
|
1526
1549
|
}
|
|
1527
1550
|
return out;
|
|
@@ -7,17 +7,27 @@ export async function areFileToolPathsTrusted(options: {
|
|
|
7
7
|
paths: readonly string[];
|
|
8
8
|
access: 'read' | 'write';
|
|
9
9
|
readableSkillRoots: readonly string[];
|
|
10
|
+
additionalDirectories?: readonly string[];
|
|
10
11
|
}): Promise<boolean> {
|
|
11
12
|
if (options.paths.length === 0) return false;
|
|
12
13
|
try {
|
|
13
14
|
const roots = [await realpath(options.workDir)];
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
15
|
+
/*
|
|
16
|
+
* Additional directories are granted to Claude Code for reading and
|
|
17
|
+
* writing, like the working directory. Only absolute paths are trusted so
|
|
18
|
+
* a relative entry cannot widen trust relative to an unknown directory.
|
|
19
|
+
*/
|
|
20
|
+
const trustedRoots = [
|
|
21
|
+
...(options.additionalDirectories ?? []).filter(root =>
|
|
22
|
+
path.posix.isAbsolute(root),
|
|
23
|
+
),
|
|
24
|
+
...(options.access === 'read' ? options.readableSkillRoots : []),
|
|
25
|
+
];
|
|
26
|
+
for (const root of trustedRoots) {
|
|
27
|
+
try {
|
|
28
|
+
roots.push(await realpath(root));
|
|
29
|
+
} catch {
|
|
30
|
+
continue;
|
|
21
31
|
}
|
|
22
32
|
}
|
|
23
33
|
for (const filePath of options.paths) {
|
package/src/claude-code-auth.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { execFileSync } from 'node:child_process';
|
|
1
|
+
import { execFileSync, execSync } from 'node:child_process';
|
|
2
2
|
import { readFileSync } from 'node:fs';
|
|
3
3
|
import { homedir } from 'node:os';
|
|
4
4
|
import { join } from 'node:path';
|
|
@@ -169,7 +169,10 @@ function pickAnthropic({
|
|
|
169
169
|
* it. The `claude` CLI uses this hook to fetch credentials from password
|
|
170
170
|
* managers and similar tools; mirroring it here lets users with that
|
|
171
171
|
* setup run the harness without having to set `ANTHROPIC_API_KEY`
|
|
172
|
-
* explicitly.
|
|
172
|
+
* explicitly. It preserves existing helper commands by using `sh` from
|
|
173
|
+
* PATH when available. If `sh` cannot be found, it uses the system shell:
|
|
174
|
+
* `/bin/sh` on macOS and Linux, `cmd.exe` on Windows. Only a missing shell
|
|
175
|
+
* triggers fallback, so a helper that fails is never executed twice.
|
|
173
176
|
*/
|
|
174
177
|
function readApiKeyHelper(): string | undefined {
|
|
175
178
|
const home = homedir();
|
|
@@ -189,10 +192,27 @@ function readApiKeyHelper(): string | undefined {
|
|
|
189
192
|
const command = settings.apiKeyHelper;
|
|
190
193
|
if (typeof command !== 'string' || command.length === 0) return undefined;
|
|
191
194
|
try {
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
195
|
+
let output: string;
|
|
196
|
+
try {
|
|
197
|
+
output = execFileSync('sh', ['-c', command], {
|
|
198
|
+
encoding: 'utf8',
|
|
199
|
+
stdio: ['ignore', 'pipe', 'ignore'],
|
|
200
|
+
windowsHide: true,
|
|
201
|
+
});
|
|
202
|
+
} catch (error) {
|
|
203
|
+
if (
|
|
204
|
+
!(error instanceof Error) ||
|
|
205
|
+
!('code' in error) ||
|
|
206
|
+
error.code !== 'ENOENT'
|
|
207
|
+
) {
|
|
208
|
+
return undefined;
|
|
209
|
+
}
|
|
210
|
+
output = execSync(command, {
|
|
211
|
+
encoding: 'utf8',
|
|
212
|
+
stdio: ['ignore', 'pipe', 'ignore'],
|
|
213
|
+
windowsHide: true,
|
|
214
|
+
});
|
|
215
|
+
}
|
|
196
216
|
const trimmed = output.trim();
|
|
197
217
|
return trimmed.length > 0 ? trimmed : undefined;
|
|
198
218
|
} catch {
|
|
@@ -48,6 +48,7 @@ export const startMessageSchema = harnessV1BridgeStartBaseSchema.extend({
|
|
|
48
48
|
maxTurns: z.number().optional(),
|
|
49
49
|
skillOverrides: skillOverridesSchema.optional(),
|
|
50
50
|
maxBudgetUsd: z.number().optional(),
|
|
51
|
+
additionalDirectories: z.array(z.string()).optional(),
|
|
51
52
|
strictMcpConfig: z.boolean().optional(),
|
|
52
53
|
questionTimeoutMs: z.number().optional(),
|
|
53
54
|
excludeDynamicSections: z.boolean().optional(),
|
|
@@ -134,6 +134,11 @@ export type ClaudeCodeHarnessSettings = {
|
|
|
134
134
|
* the query stops and the turn ends with an error. Unset means no limit.
|
|
135
135
|
*/
|
|
136
136
|
readonly maxBudgetUsd?: number;
|
|
137
|
+
/**
|
|
138
|
+
* Absolute paths of directories Claude can access in addition to the session
|
|
139
|
+
* working directory.
|
|
140
|
+
*/
|
|
141
|
+
readonly additionalDirectories?: ReadonlyArray<string>;
|
|
137
142
|
/**
|
|
138
143
|
* Only use the MCP servers configured with `mcpServers`, and ignore any other
|
|
139
144
|
* MCP configuration such as a project `.mcp.json` or user settings.
|
|
@@ -191,6 +196,12 @@ export type ClaudeCodeHarnessSettings = {
|
|
|
191
196
|
readonly startupTimeoutMs?: number;
|
|
192
197
|
/** Maximum milliseconds to wait for the first turn event. Defaults to 120000. */
|
|
193
198
|
readonly turnStartTimeoutMs?: number;
|
|
199
|
+
/**
|
|
200
|
+
* Abort a running turn when its host disconnects and does not reconnect
|
|
201
|
+
* within this many milliseconds, so the turn stops spending. A suspended
|
|
202
|
+
* turn is exempt. Unset keeps the turn running until it finishes.
|
|
203
|
+
*/
|
|
204
|
+
readonly hostDisconnectTimeoutMs?: number;
|
|
194
205
|
/**
|
|
195
206
|
* Configures reconnection attempts after an established bridge connection
|
|
196
207
|
* drops. The reconnect window includes connection establishment and
|
|
@@ -1112,6 +1123,7 @@ export function createClaudeCode(
|
|
|
1112
1123
|
maxTurns: settings.maxTurns,
|
|
1113
1124
|
skillOverrides: settings.skillOverrides,
|
|
1114
1125
|
maxBudgetUsd: settings.maxBudgetUsd,
|
|
1126
|
+
additionalDirectories: settings.additionalDirectories,
|
|
1115
1127
|
strictMcpConfig: settings.strictMcpConfig,
|
|
1116
1128
|
questionTimeoutMs: settings.questionTimeoutMs,
|
|
1117
1129
|
excludeDynamicSections: settings.excludeDynamicSections,
|
|
@@ -1203,7 +1215,7 @@ export function createClaudeCode(
|
|
|
1203
1215
|
});
|
|
1204
1216
|
|
|
1205
1217
|
const proc = await toolSafeSandboxSession.spawn({
|
|
1206
|
-
command: `node ${shellQuote(`${bootstrapDir}/bridge.mjs`)} --workdir ${shellQuote(workDir)} --bridge-state-dir ${shellQuote(bridgeStateDir)}`,
|
|
1218
|
+
command: `node ${shellQuote(`${bootstrapDir}/bridge.mjs`)} --workdir ${shellQuote(workDir)} --bridge-state-dir ${shellQuote(bridgeStateDir)}${settings.hostDisconnectTimeoutMs !== undefined ? ` --host-disconnect-timeout-ms ${shellQuote(String(settings.hostDisconnectTimeoutMs))}` : ''}`,
|
|
1207
1219
|
env,
|
|
1208
1220
|
abortSignal: startOpts.abortSignal,
|
|
1209
1221
|
});
|
|
@@ -1291,6 +1303,7 @@ export function createClaudeCode(
|
|
|
1291
1303
|
maxTurns: settings.maxTurns,
|
|
1292
1304
|
skillOverrides: settings.skillOverrides,
|
|
1293
1305
|
maxBudgetUsd: settings.maxBudgetUsd,
|
|
1306
|
+
additionalDirectories: settings.additionalDirectories,
|
|
1294
1307
|
strictMcpConfig: settings.strictMcpConfig,
|
|
1295
1308
|
questionTimeoutMs: settings.questionTimeoutMs,
|
|
1296
1309
|
excludeDynamicSections: settings.excludeDynamicSections,
|
|
@@ -1347,6 +1360,7 @@ function createSession({
|
|
|
1347
1360
|
maxTurns,
|
|
1348
1361
|
skillOverrides,
|
|
1349
1362
|
maxBudgetUsd,
|
|
1363
|
+
additionalDirectories,
|
|
1350
1364
|
strictMcpConfig,
|
|
1351
1365
|
questionTimeoutMs,
|
|
1352
1366
|
excludeDynamicSections,
|
|
@@ -1384,6 +1398,7 @@ function createSession({
|
|
|
1384
1398
|
maxTurns: number | undefined;
|
|
1385
1399
|
skillOverrides: ClaudeCodeHarnessSettings['skillOverrides'];
|
|
1386
1400
|
maxBudgetUsd: number | undefined;
|
|
1401
|
+
additionalDirectories: ReadonlyArray<string> | undefined;
|
|
1387
1402
|
strictMcpConfig: boolean | undefined;
|
|
1388
1403
|
questionTimeoutMs: number | undefined;
|
|
1389
1404
|
excludeDynamicSections: boolean | undefined;
|
|
@@ -1794,6 +1809,9 @@ function createSession({
|
|
|
1794
1809
|
maxTurns,
|
|
1795
1810
|
...(skillOverrides !== undefined ? { skillOverrides } : {}),
|
|
1796
1811
|
...(maxBudgetUsd !== undefined ? { maxBudgetUsd } : {}),
|
|
1812
|
+
...(additionalDirectories !== undefined
|
|
1813
|
+
? { additionalDirectories: [...additionalDirectories] }
|
|
1814
|
+
: {}),
|
|
1797
1815
|
...(strictMcpConfig !== undefined ? { strictMcpConfig } : {}),
|
|
1798
1816
|
...(questionTimeoutMs !== undefined ? { questionTimeoutMs } : {}),
|
|
1799
1817
|
...(excludeDynamicSections !== undefined
|
|
@@ -1881,6 +1899,9 @@ function createSession({
|
|
|
1881
1899
|
maxTurns,
|
|
1882
1900
|
...(skillOverrides !== undefined ? { skillOverrides } : {}),
|
|
1883
1901
|
...(maxBudgetUsd !== undefined ? { maxBudgetUsd } : {}),
|
|
1902
|
+
...(additionalDirectories !== undefined
|
|
1903
|
+
? { additionalDirectories: [...additionalDirectories] }
|
|
1904
|
+
: {}),
|
|
1884
1905
|
...(strictMcpConfig !== undefined ? { strictMcpConfig } : {}),
|
|
1885
1906
|
...(questionTimeoutMs !== undefined ? { questionTimeoutMs } : {}),
|
|
1886
1907
|
...(excludeDynamicSections !== undefined
|