@ai-sdk/harness-claude-code 1.0.200 → 1.0.202
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 +17 -0
- package/dist/bridge/index.mjs +105 -25
- package/dist/bridge/index.mjs.map +1 -1
- package/dist/index.js +1 -1
- package/package.json +3 -3
- package/src/bridge/create-emit-stream-event.ts +9 -6
- package/src/bridge/host-tool-names.ts +14 -0
- package/src/bridge/index.ts +32 -2
- package/src/bridge/question-tool.ts +24 -8
- package/src/bridge/tool-filtering.ts +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,22 @@
|
|
|
1
1
|
# @ai-sdk/harness-claude-code
|
|
2
2
|
|
|
3
|
+
## 1.0.202
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- Updated dependencies [0f4f233]
|
|
8
|
+
- @ai-sdk/harness@1.0.198
|
|
9
|
+
|
|
10
|
+
## 1.0.201
|
|
11
|
+
|
|
12
|
+
### Patch Changes
|
|
13
|
+
|
|
14
|
+
- 3435be9: fix(harness-claude-code): route bare-name host tool calls through built-in tool aliases and classify them as host-executed calls throughout streaming and permission handling
|
|
15
|
+
- 4bd3cb2: fix(harness-claude-code): preserve selected `AskUserQuestion` options when an answer also includes free-form text by sending the text as per-question notes
|
|
16
|
+
- Updated dependencies [743e15b]
|
|
17
|
+
- @ai-sdk/provider-utils@5.0.86
|
|
18
|
+
- @ai-sdk/harness@1.0.197
|
|
19
|
+
|
|
3
20
|
## 1.0.200
|
|
4
21
|
|
|
5
22
|
### Patch Changes
|
package/dist/bridge/index.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { appendFile, lstat, mkdir, readdir, realpath, writeFile } from "node:fs/promises";
|
|
1
|
+
import { appendFile, lstat, mkdir, readdir, realpath, stat, writeFile } from "node:fs/promises";
|
|
2
2
|
import { existsSync, readFileSync } from "node:fs";
|
|
3
3
|
import { createPublicKey, randomBytes, randomUUID, verify } from "node:crypto";
|
|
4
4
|
import { argv, env, pid, stdout } from "node:process";
|
|
@@ -91,17 +91,44 @@ var HarnessBridgeCapabilityUnsupportedError = class extends Error {
|
|
|
91
91
|
function assertWriteProtectedPathEntries(entries) {
|
|
92
92
|
for (const entry of entries) if (typeof entry !== "string" || entry.length === 0 || path.posix.isAbsolute(entry) || entry.split("/").some((segment) => segment === "" || segment === "." || segment === "..")) throw new TypeError(`Write-protected path entries must be relative paths without empty, "." or ".." segments; received ${JSON.stringify(entry)}.`);
|
|
93
93
|
}
|
|
94
|
-
function isInsideWriteProtectionPath({ parent, candidate }) {
|
|
95
|
-
const
|
|
96
|
-
|
|
94
|
+
function isInsideWriteProtectionPath({ parent, candidate, pathIdentities }) {
|
|
95
|
+
const parentSegments = path.posix.normalize(parent).split("/").filter(Boolean);
|
|
96
|
+
const candidateSegments = path.posix.normalize(candidate).split("/").filter(Boolean);
|
|
97
|
+
if (parentSegments.length > candidateSegments.length) return false;
|
|
98
|
+
let parentPrefix = "";
|
|
99
|
+
let candidatePrefix = "";
|
|
100
|
+
for (let index = 0; index < parentSegments.length; index++) {
|
|
101
|
+
const parentSegment = parentSegments[index];
|
|
102
|
+
const candidateSegment = candidateSegments[index];
|
|
103
|
+
parentPrefix += `/${parentSegment}`;
|
|
104
|
+
candidatePrefix += `/${candidateSegment}`;
|
|
105
|
+
if (parentSegment === candidateSegment) continue;
|
|
106
|
+
if (pathIdentities != null) {
|
|
107
|
+
const parentIdentity = pathIdentities.get(parentPrefix);
|
|
108
|
+
const candidateIdentity = pathIdentities.get(candidatePrefix);
|
|
109
|
+
if (parentIdentity === void 0 || candidateIdentity === void 0) return true;
|
|
110
|
+
if (parentIdentity !== null && candidateIdentity !== null) {
|
|
111
|
+
if (parentIdentity !== candidateIdentity) return false;
|
|
112
|
+
continue;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
if (isAsciiSegment(parentSegment) && isAsciiSegment(candidateSegment) && parentSegment.toLowerCase() !== candidateSegment.toLowerCase()) return false;
|
|
116
|
+
}
|
|
117
|
+
return true;
|
|
97
118
|
}
|
|
98
|
-
function
|
|
119
|
+
function isAsciiSegment(segment) {
|
|
120
|
+
for (const character of segment) if (character.charCodeAt(0) > 127) return false;
|
|
121
|
+
return true;
|
|
122
|
+
}
|
|
123
|
+
function matchesWriteProtectedPath({ targets, protectedPaths, anyProtectedPathExists, pathIdentities }) {
|
|
99
124
|
return targets.some((target) => protectedPaths.some((protectedPath) => isInsideWriteProtectionPath({
|
|
100
125
|
parent: protectedPath,
|
|
101
|
-
candidate: target.path
|
|
126
|
+
candidate: target.path,
|
|
127
|
+
pathIdentities
|
|
102
128
|
}) || isInsideWriteProtectionPath({
|
|
103
129
|
parent: target.path,
|
|
104
|
-
candidate: protectedPath
|
|
130
|
+
candidate: protectedPath,
|
|
131
|
+
pathIdentities
|
|
105
132
|
})) || anyProtectedPathExists && target.isMultiLinkFile);
|
|
106
133
|
}
|
|
107
134
|
/**
|
|
@@ -125,6 +152,9 @@ function matchesWriteProtectedPath({ targets, protectedPaths, anyProtectedPathEx
|
|
|
125
152
|
* written through an unprotected alias. While any protected path exists,
|
|
126
153
|
* writes to files with more than one link match too. Any resolution failure,
|
|
127
154
|
* including dangling or looping symlinks, reports the write as protected.
|
|
155
|
+
* Existing path components are compared by filesystem identity. Missing names
|
|
156
|
+
* are matched conservatively, including ASCII case variants and ambiguous
|
|
157
|
+
* Unicode spellings, even on case-sensitive filesystems.
|
|
128
158
|
*/
|
|
129
159
|
async function isWriteProtectedPath({ workDir, cwd = workDir, paths, writeProtectedPaths }) {
|
|
130
160
|
assertWriteProtectedPathEntries(writeProtectedPaths);
|
|
@@ -150,9 +180,28 @@ async function isWriteProtectedPath({ workDir, cwd = workDir, paths, writeProtec
|
|
|
150
180
|
});
|
|
151
181
|
}
|
|
152
182
|
}
|
|
183
|
+
const protectedPaths = [...writeProtectedPaths.map((entry) => path.posix.join(canonicalWorkDir, entry)), ...protection.paths];
|
|
184
|
+
const pathIdentities = /* @__PURE__ */ new Map();
|
|
185
|
+
for (const filePath of [...protectedPaths, ...targets.map((target) => target.path)]) {
|
|
186
|
+
let prefix = filePath;
|
|
187
|
+
while (!pathIdentities.has(prefix)) {
|
|
188
|
+
try {
|
|
189
|
+
const stats = await stat(prefix, { bigint: true });
|
|
190
|
+
if (stats.ino === 0n) return true;
|
|
191
|
+
pathIdentities.set(prefix, `${stats.dev}:${stats.ino}`);
|
|
192
|
+
} catch (error) {
|
|
193
|
+
if (error == null || typeof error !== "object" || !("code" in error) || error.code !== "ENOENT") throw error;
|
|
194
|
+
pathIdentities.set(prefix, null);
|
|
195
|
+
}
|
|
196
|
+
const parent = path.posix.dirname(prefix);
|
|
197
|
+
if (parent === prefix) break;
|
|
198
|
+
prefix = parent;
|
|
199
|
+
}
|
|
200
|
+
}
|
|
153
201
|
return matchesWriteProtectedPath({
|
|
154
202
|
targets,
|
|
155
|
-
protectedPaths
|
|
203
|
+
protectedPaths,
|
|
204
|
+
pathIdentities,
|
|
156
205
|
anyProtectedPathExists: protection.anyRootExists
|
|
157
206
|
});
|
|
158
207
|
} catch {
|
|
@@ -904,6 +953,13 @@ function toClaudeSkillsOption(skills) {
|
|
|
904
953
|
return skills && skills.length > 0 ? "all" : void 0;
|
|
905
954
|
}
|
|
906
955
|
//#endregion
|
|
956
|
+
//#region src/bridge/host-tool-names.ts
|
|
957
|
+
const HOST_TOOL_PREFIX = "mcp__harness-tools__";
|
|
958
|
+
function resolveHostToolName(toolName, toolAliases) {
|
|
959
|
+
const resolvedName = toolAliases != null && Object.hasOwn(toolAliases, toolName) ? toolAliases[toolName] : toolName;
|
|
960
|
+
return resolvedName.startsWith("mcp__harness-tools__") ? resolvedName.slice(20) : void 0;
|
|
961
|
+
}
|
|
962
|
+
//#endregion
|
|
907
963
|
//#region src/bridge/create-emit-stream-event.ts
|
|
908
964
|
function createClaudeStreamEventState() {
|
|
909
965
|
return {
|
|
@@ -934,7 +990,6 @@ const UNRECOVERABLE_API_RETRY_STATUSES = /* @__PURE__ */ new Set([
|
|
|
934
990
|
403,
|
|
935
991
|
404
|
|
936
992
|
]);
|
|
937
|
-
const HOST_TOOL_PREFIX = "mcp__harness-tools__";
|
|
938
993
|
const RAW_TASK_MESSAGE_SUBTYPES = /* @__PURE__ */ new Set([
|
|
939
994
|
"background_tasks_changed",
|
|
940
995
|
"task_started",
|
|
@@ -943,9 +998,9 @@ const RAW_TASK_MESSAGE_SUBTYPES = /* @__PURE__ */ new Set([
|
|
|
943
998
|
"task_notification"
|
|
944
999
|
]);
|
|
945
1000
|
function isExternalMcpTool(nativeName) {
|
|
946
|
-
return nativeName.startsWith("mcp__") && !nativeName.startsWith(
|
|
1001
|
+
return nativeName.startsWith("mcp__") && !nativeName.startsWith("mcp__harness-tools__");
|
|
947
1002
|
}
|
|
948
|
-
function createEmitStreamEvent({ state, emit, emitWarning, emitTerminalError, onCompactionBoundary, toCommonName }) {
|
|
1003
|
+
function createEmitStreamEvent({ state, emit, emitWarning, emitTerminalError, onCompactionBoundary, toCommonName, toHostToolName = resolveHostToolName }) {
|
|
949
1004
|
let streamStarted = false;
|
|
950
1005
|
return (msg) => {
|
|
951
1006
|
const type = msg.type;
|
|
@@ -1019,7 +1074,8 @@ function createEmitStreamEvent({ state, emit, emitWarning, emitTerminalError, on
|
|
|
1019
1074
|
message: msg,
|
|
1020
1075
|
state,
|
|
1021
1076
|
send: emit,
|
|
1022
|
-
toCommonName
|
|
1077
|
+
toCommonName,
|
|
1078
|
+
toHostToolName
|
|
1023
1079
|
});
|
|
1024
1080
|
return;
|
|
1025
1081
|
}
|
|
@@ -1034,8 +1090,8 @@ function createEmitStreamEvent({ state, emit, emitWarning, emitTerminalError, on
|
|
|
1034
1090
|
state.structuredOutputToolUseIds.add(block.id);
|
|
1035
1091
|
continue;
|
|
1036
1092
|
}
|
|
1037
|
-
|
|
1038
|
-
|
|
1093
|
+
const hostToolName = toHostToolName(block.name);
|
|
1094
|
+
if (hostToolName !== void 0) {
|
|
1039
1095
|
state.pendingStepToolUseIds.add(block.id);
|
|
1040
1096
|
state.mcpToolCalls.set(block.id, {
|
|
1041
1097
|
toolName: hostToolName,
|
|
@@ -1263,7 +1319,7 @@ function formatApiRetryWarning(msg) {
|
|
|
1263
1319
|
if (msg.error) details.push(msg.error);
|
|
1264
1320
|
return details.length > 0 ? `Claude Code API retry: ${details.join("; ")}` : "Claude Code API retry";
|
|
1265
1321
|
}
|
|
1266
|
-
function handleStreamEvent({ event, message, state, send, toCommonName }) {
|
|
1322
|
+
function handleStreamEvent({ event, message, state, send, toCommonName, toHostToolName }) {
|
|
1267
1323
|
if (!event) return;
|
|
1268
1324
|
if (event.type === "message_start") {
|
|
1269
1325
|
state.modelResponseStreaming = true;
|
|
@@ -1327,7 +1383,7 @@ function handleStreamEvent({ event, message, state, send, toCommonName }) {
|
|
|
1327
1383
|
const nativeName = event.content_block.name;
|
|
1328
1384
|
if (nativeName === "StructuredOutput") return;
|
|
1329
1385
|
if (nativeName === "AskUserQuestion") return;
|
|
1330
|
-
const hostToolName =
|
|
1386
|
+
const hostToolName = toHostToolName(nativeName);
|
|
1331
1387
|
const dynamic = isExternalMcpTool(nativeName);
|
|
1332
1388
|
partialBlocks.set(index, {
|
|
1333
1389
|
id,
|
|
@@ -1668,26 +1724,34 @@ function toClaudeCodeQuestionResult(input) {
|
|
|
1668
1724
|
message: `The user ${output.action} the questions.`
|
|
1669
1725
|
};
|
|
1670
1726
|
const canonicalInput = toHarnessQuestionsInput(input.nativeInput);
|
|
1727
|
+
const annotations = /* @__PURE__ */ new Map();
|
|
1671
1728
|
const answers = Object.fromEntries(canonicalInput.questions.flatMap((question, questionIndex) => {
|
|
1672
1729
|
const answer = output.answers[question.id];
|
|
1673
1730
|
if (answer == null) return [];
|
|
1674
1731
|
const nativeQuestion = input.nativeInput.questions[questionIndex];
|
|
1675
|
-
|
|
1732
|
+
const selectedAnswer = toClaudeCodeAnswer({
|
|
1676
1733
|
nativeQuestion,
|
|
1677
|
-
optionIds: answer.optionIds
|
|
1678
|
-
|
|
1679
|
-
|
|
1734
|
+
optionIds: answer.optionIds
|
|
1735
|
+
});
|
|
1736
|
+
if (selectedAnswer !== "" && answer.freeform !== void 0) annotations.set(nativeQuestion.question, {
|
|
1737
|
+
...input.nativeInput.annotations?.[nativeQuestion.question],
|
|
1738
|
+
notes: answer.freeform
|
|
1739
|
+
});
|
|
1740
|
+
return [[nativeQuestion.question, selectedAnswer === "" ? answer.freeform ?? "" : selectedAnswer]];
|
|
1680
1741
|
}));
|
|
1681
1742
|
return {
|
|
1682
1743
|
behavior: "allow",
|
|
1683
1744
|
updatedInput: {
|
|
1684
1745
|
...input.nativeInput,
|
|
1685
|
-
answers
|
|
1746
|
+
answers,
|
|
1747
|
+
...annotations.size > 0 ? { annotations: {
|
|
1748
|
+
...input.nativeInput.annotations,
|
|
1749
|
+
...Object.fromEntries(annotations)
|
|
1750
|
+
} } : {}
|
|
1686
1751
|
}
|
|
1687
1752
|
};
|
|
1688
1753
|
}
|
|
1689
1754
|
function toClaudeCodeAnswer(input) {
|
|
1690
|
-
if (input.freeform !== void 0) return input.freeform;
|
|
1691
1755
|
const labels = input.optionIds.flatMap((optionId) => {
|
|
1692
1756
|
const index = positionalIdIndex({
|
|
1693
1757
|
id: optionId,
|
|
@@ -1826,8 +1890,9 @@ function createPermissionOptions(input) {
|
|
|
1826
1890
|
return {
|
|
1827
1891
|
...baseOptions,
|
|
1828
1892
|
canUseTool: async (toolName, toolInput, options) => {
|
|
1829
|
-
if (
|
|
1893
|
+
if (input.toHostToolName(toolName) !== void 0 || !autoMode && !await needsHostApproval({
|
|
1830
1894
|
toolName,
|
|
1895
|
+
toHostToolName: input.toHostToolName,
|
|
1831
1896
|
toolInput,
|
|
1832
1897
|
permissionMode,
|
|
1833
1898
|
inactiveNativeTools,
|
|
@@ -1960,7 +2025,7 @@ function createGatewayModelOverrides(model, env) {
|
|
|
1960
2025
|
return Object.keys(overrides).length > 0 ? overrides : void 0;
|
|
1961
2026
|
}
|
|
1962
2027
|
async function needsHostApproval(input) {
|
|
1963
|
-
if (input.toolName
|
|
2028
|
+
if (input.toHostToolName(input.toolName) !== void 0) return false;
|
|
1964
2029
|
if (input.inactiveNativeTools.has(input.toolName) || nativeToolRequiresApproval({
|
|
1965
2030
|
nativeName: input.toolName,
|
|
1966
2031
|
permissionMode: input.permissionMode
|
|
@@ -2055,6 +2120,7 @@ function createApprovalPreToolUseHook(input) {
|
|
|
2055
2120
|
cwd: hookInput.cwd
|
|
2056
2121
|
}) : await needsHostApproval({
|
|
2057
2122
|
toolName,
|
|
2123
|
+
toHostToolName: input.toHostToolName,
|
|
2058
2124
|
toolInput: hookInput.tool_input,
|
|
2059
2125
|
cwd: hookInput.cwd,
|
|
2060
2126
|
readableSkillRoots: input.readableSkillRoots,
|
|
@@ -2091,6 +2157,16 @@ async function runTurn(start, turn) {
|
|
|
2091
2157
|
if (turn.abortSignal.aborted) abortCtl.abort();
|
|
2092
2158
|
else turn.abortSignal.addEventListener("abort", onHostAbort, { once: true });
|
|
2093
2159
|
const streamEventState = createClaudeStreamEventState();
|
|
2160
|
+
const reservedToolNames = /* @__PURE__ */ new Set([
|
|
2161
|
+
...claudeAgentSdk.BUILTIN_TOOL_NAMES ?? [],
|
|
2162
|
+
...Object.keys(claudeAgentSdk.LEGACY_TOOL_NAME_ALIASES ?? {}),
|
|
2163
|
+
...Object.keys(NATIVE_TOOL_KINDS),
|
|
2164
|
+
...Object.keys(NATIVE_TO_COMMON),
|
|
2165
|
+
...Object.values(PUBLIC_TO_NATIVE),
|
|
2166
|
+
"StructuredOutput"
|
|
2167
|
+
]);
|
|
2168
|
+
const toolAliases = Object.fromEntries((start.tools ?? []).filter((tool) => !reservedToolNames.has(tool.name) && !tool.name.startsWith("mcp__")).map((tool) => [tool.name, `${HOST_TOOL_PREFIX}${tool.name}`]));
|
|
2169
|
+
const toHostToolName = (toolName) => resolveHostToolName(toolName, toolAliases);
|
|
2094
2170
|
const mcpServers = { ...start.mcpServers };
|
|
2095
2171
|
if (start.tools && start.tools.length > 0) {
|
|
2096
2172
|
const server = new mcpModule.McpServer({
|
|
@@ -2193,6 +2269,7 @@ async function runTurn(start, turn) {
|
|
|
2193
2269
|
const approvalDecisions = /* @__PURE__ */ new Map();
|
|
2194
2270
|
const toolCwds = /* @__PURE__ */ new Map();
|
|
2195
2271
|
const permissionOptions = createPermissionOptions({
|
|
2272
|
+
toHostToolName,
|
|
2196
2273
|
toolCwds,
|
|
2197
2274
|
approvalDecisions,
|
|
2198
2275
|
start,
|
|
@@ -2219,6 +2296,7 @@ async function runTurn(start, turn) {
|
|
|
2219
2296
|
const modelOverrides = createGatewayModelOverrides(start.model, sdkEnv);
|
|
2220
2297
|
const deferredToolUseIds = /* @__PURE__ */ new Set();
|
|
2221
2298
|
const approvalPreToolUseHook = createApprovalPreToolUseHook({
|
|
2299
|
+
toHostToolName,
|
|
2222
2300
|
toolCwds,
|
|
2223
2301
|
readableSkillRoots: start.readableSkillRoots ?? [],
|
|
2224
2302
|
additionalDirectories: start.additionalDirectories ?? [],
|
|
@@ -2285,6 +2363,7 @@ async function runTurn(start, turn) {
|
|
|
2285
2363
|
...permissionOptions,
|
|
2286
2364
|
...Object.keys(settings).length > 0 ? { settings } : {},
|
|
2287
2365
|
mcpServers,
|
|
2366
|
+
...Object.keys(toolAliases).length > 0 ? { toolAliases } : {},
|
|
2288
2367
|
settingSources: ["user"],
|
|
2289
2368
|
cwd: workdir,
|
|
2290
2369
|
abortController: abortCtl
|
|
@@ -2365,7 +2444,8 @@ async function runTurn(start, turn) {
|
|
|
2365
2444
|
emitWarning: turn.emitWarning,
|
|
2366
2445
|
emitTerminalError,
|
|
2367
2446
|
onCompactionBoundary: (boundary) => compaction.onBoundary(boundary),
|
|
2368
|
-
toCommonName
|
|
2447
|
+
toCommonName,
|
|
2448
|
+
toHostToolName
|
|
2369
2449
|
});
|
|
2370
2450
|
async function* messages() {
|
|
2371
2451
|
for (;;) {
|