@smithers-orchestrator/agents 0.16.0
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/LICENSE +21 -0
- package/package.json +65 -0
- package/src/AgentLike.ts +28 -0
- package/src/AmpAgent.js +232 -0
- package/src/AmpAgentOptions.ts +26 -0
- package/src/AnthropicAgent.js +54 -0
- package/src/AnthropicAgentOptions.ts +8 -0
- package/src/BaseCliAgent/AgentCliActionKind.ts +10 -0
- package/src/BaseCliAgent/AgentCliEvent.ts +44 -0
- package/src/BaseCliAgent/BaseCliAgent.js +874 -0
- package/src/BaseCliAgent/BaseCliAgentOptions.ts +13 -0
- package/src/BaseCliAgent/CliOutputInterpreter.ts +8 -0
- package/src/BaseCliAgent/CliUsageInfo.ts +7 -0
- package/src/BaseCliAgent/CodexConfigOverrides.ts +3 -0
- package/src/BaseCliAgent/PiExtensionUiRequest.ts +10 -0
- package/src/BaseCliAgent/PiExtensionUiResponse.ts +7 -0
- package/src/BaseCliAgent/RunCommandResult.ts +5 -0
- package/src/BaseCliAgent/buildGenerateResult.js +57 -0
- package/src/BaseCliAgent/combineNonEmpty.js +8 -0
- package/src/BaseCliAgent/createAgentStdoutTextEmitter.js +198 -0
- package/src/BaseCliAgent/extractPrompt.js +88 -0
- package/src/BaseCliAgent/extractTextFromJsonValue.js +46 -0
- package/src/BaseCliAgent/index.js +32 -0
- package/src/BaseCliAgent/normalizeCodexConfig.js +22 -0
- package/src/BaseCliAgent/parseHelpers.js +111 -0
- package/src/BaseCliAgent/pushFlag.js +18 -0
- package/src/BaseCliAgent/pushList.js +10 -0
- package/src/BaseCliAgent/resolveTimeouts.js +24 -0
- package/src/BaseCliAgent/runCommandEffect.js +32 -0
- package/src/BaseCliAgent/runRpcCommandEffect.js +365 -0
- package/src/BaseCliAgent/truncateToBytes.js +13 -0
- package/src/BaseCliAgent/tryParseJson.js +18 -0
- package/src/ClaudeCodeAgent.js +455 -0
- package/src/ClaudeCodeAgentOptions.ts +52 -0
- package/src/CodexAgent.js +593 -0
- package/src/CodexAgentOptions.ts +23 -0
- package/src/ForgeAgent.js +128 -0
- package/src/ForgeAgentOptions.ts +14 -0
- package/src/GeminiAgent.js +273 -0
- package/src/GeminiAgentOptions.ts +20 -0
- package/src/KimiAgent.js +260 -0
- package/src/KimiAgentOptions.ts +21 -0
- package/src/OpenAIAgent.js +54 -0
- package/src/OpenAIAgentOptions.ts +8 -0
- package/src/PiAgent.js +468 -0
- package/src/PiAgentOptions.ts +40 -0
- package/src/SdkAgentOptions.ts +16 -0
- package/src/agent-contract/SmithersAgentContract.ts +10 -0
- package/src/agent-contract/SmithersAgentContractTool.ts +8 -0
- package/src/agent-contract/SmithersAgentToolCategory.ts +6 -0
- package/src/agent-contract/SmithersListedTool.ts +4 -0
- package/src/agent-contract/SmithersToolSurface.ts +1 -0
- package/src/agent-contract/createSmithersAgentContract.js +188 -0
- package/src/agent-contract/index.js +10 -0
- package/src/agent-contract/renderSmithersAgentPromptGuidance.js +81 -0
- package/src/capability-registry/AgentCapabilityRegistry.ts +22 -0
- package/src/capability-registry/AgentToolDescriptor.ts +4 -0
- package/src/capability-registry/hashCapabilityRegistry.js +43 -0
- package/src/capability-registry/index.js +8 -0
- package/src/capability-registry/normalizeCapabilityRegistry.js +52 -0
- package/src/capability-registry/normalizeCapabilityStringList.js +9 -0
- package/src/cli-capabilities/CliAgentCapabilityAdapterId.ts +6 -0
- package/src/cli-capabilities/CliAgentCapabilityDoctorReport.ts +18 -0
- package/src/cli-capabilities/CliAgentCapabilityReportEntry.ts +9 -0
- package/src/cli-capabilities/formatCliAgentCapabilityDoctorReport.js +24 -0
- package/src/cli-capabilities/getCliAgentCapabilityDoctorReport.js +92 -0
- package/src/cli-capabilities/getCliAgentCapabilityReport.js +52 -0
- package/src/cli-capabilities/index.js +11 -0
- package/src/diagnostics/DiagnosticCheck.ts +11 -0
- package/src/diagnostics/DiagnosticCheckId.ts +4 -0
- package/src/diagnostics/DiagnosticContext.ts +4 -0
- package/src/diagnostics/DiagnosticReport.ts +9 -0
- package/src/diagnostics/enrichReportWithErrorAnalysis.js +34 -0
- package/src/diagnostics/formatDiagnosticSummary.js +17 -0
- package/src/diagnostics/getDiagnosticStrategy.js +503 -0
- package/src/diagnostics/index.js +13 -0
- package/src/diagnostics/launchDiagnostics.js +16 -0
- package/src/diagnostics/runDiagnostics.js +52 -0
- package/src/index.d.ts +872 -0
- package/src/index.js +39 -0
- package/src/resolveSdkModel.js +9 -0
- package/src/sanitizeForOpenAI.js +47 -0
- package/src/streamResultToGenerateResult.js +70 -0
- package/src/zodToOpenAISchema.js +16 -0
|
@@ -0,0 +1,455 @@
|
|
|
1
|
+
import { BaseCliAgent, pushFlag, pushList, isRecord, asString, truncate, toolKindFromName, shouldSurfaceUnparsedStdout, isLikelyRuntimeMetadata, createSyntheticIdGenerator, } from "./BaseCliAgent/index.js";
|
|
2
|
+
import { normalizeCapabilityStringList, } from "./capability-registry/index.js";
|
|
3
|
+
import { logWarning } from "@smithers-orchestrator/observability/logging";
|
|
4
|
+
/** @typedef {import("./BaseCliAgent/BaseCliAgentOptions.ts").BaseCliAgentOptions} BaseCliAgentOptions */
|
|
5
|
+
/** @typedef {import("./capability-registry/AgentCapabilityRegistry.ts").AgentCapabilityRegistry} AgentCapabilityRegistry */
|
|
6
|
+
/** @typedef {import("./ClaudeCodeAgentOptions.ts").ClaudeCodeAgentOptions} ClaudeCodeAgentOptions */
|
|
7
|
+
/** @typedef {import("./BaseCliAgent/CliOutputInterpreter.ts").CliOutputInterpreter} CliOutputInterpreter */
|
|
8
|
+
/** @typedef {import("./BaseCliAgent/AgentCliEvent.ts").AgentCliEvent} AgentCliEvent */
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* @param {ClaudeCodeAgentOptions} opts
|
|
12
|
+
*/
|
|
13
|
+
function resolveClaudeBuiltIns(opts) {
|
|
14
|
+
if (opts.tools === "") {
|
|
15
|
+
return [];
|
|
16
|
+
}
|
|
17
|
+
const allowed = opts.allowedTools?.length
|
|
18
|
+
? opts.allowedTools
|
|
19
|
+
: Array.isArray(opts.tools) && opts.tools.length
|
|
20
|
+
? opts.tools
|
|
21
|
+
: opts.tools === "default" || opts.tools === undefined
|
|
22
|
+
? ["default"]
|
|
23
|
+
: [];
|
|
24
|
+
const denied = (opts.disallowedTools ?? []).map((tool) => `!${tool}`);
|
|
25
|
+
const slashCommands = opts.disableSlashCommands ? [] : ["slash-commands"];
|
|
26
|
+
return normalizeCapabilityStringList([
|
|
27
|
+
...allowed,
|
|
28
|
+
...denied,
|
|
29
|
+
...slashCommands,
|
|
30
|
+
]);
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* @param {ClaudeCodeAgentOptions} [opts]
|
|
34
|
+
* @returns {AgentCapabilityRegistry}
|
|
35
|
+
*/
|
|
36
|
+
export function createClaudeCodeCapabilityRegistry(opts = {}) {
|
|
37
|
+
return {
|
|
38
|
+
version: 1,
|
|
39
|
+
engine: "claude-code",
|
|
40
|
+
runtimeTools: {},
|
|
41
|
+
mcp: {
|
|
42
|
+
bootstrap: "project-config",
|
|
43
|
+
supportsProjectScope: true,
|
|
44
|
+
supportsUserScope: true,
|
|
45
|
+
},
|
|
46
|
+
skills: {
|
|
47
|
+
supportsSkills: true,
|
|
48
|
+
installMode: "plugin",
|
|
49
|
+
smithersSkillIds: normalizeCapabilityStringList((opts.pluginDir ?? []).map((entry) => `plugin:${entry}`)),
|
|
50
|
+
},
|
|
51
|
+
humanInteraction: {
|
|
52
|
+
supportsUiRequests: false,
|
|
53
|
+
methods: [],
|
|
54
|
+
},
|
|
55
|
+
builtIns: resolveClaudeBuiltIns(opts),
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
const TOOL_OUTPUT_MAX_CHARS = 500;
|
|
59
|
+
/**
|
|
60
|
+
* @param {string} toolName
|
|
61
|
+
* @param {string | undefined} rawOutput
|
|
62
|
+
*/
|
|
63
|
+
function summarizeToolOutput(toolName, rawOutput) {
|
|
64
|
+
const output = rawOutput?.trim();
|
|
65
|
+
if (!output) {
|
|
66
|
+
return undefined;
|
|
67
|
+
}
|
|
68
|
+
const toolErrorMatch = output.match(/<tool_use_error>([\s\S]*?)<\/tool_use_error>/i);
|
|
69
|
+
if (toolErrorMatch?.[1]) {
|
|
70
|
+
return `Tool error: ${truncate(toolErrorMatch[1].trim(), 240)}`;
|
|
71
|
+
}
|
|
72
|
+
if (isLikelyRuntimeMetadata(output)) {
|
|
73
|
+
return "Tool output omitted (runtime metadata).";
|
|
74
|
+
}
|
|
75
|
+
const normalizedToolName = toolName.toLowerCase();
|
|
76
|
+
if (normalizedToolName.includes("read")) {
|
|
77
|
+
const numberedLines = output.split("\n").filter((line) => /^\s*\d+→/.test(line));
|
|
78
|
+
if (numberedLines.length > 8) {
|
|
79
|
+
return `Read output (${numberedLines.length} lines)`;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
const lines = output.split("\n").map((line) => line.trim()).filter(Boolean);
|
|
83
|
+
if (lines.length > 5) {
|
|
84
|
+
const preview = lines.slice(0, 3).join("\n");
|
|
85
|
+
return `${truncate(preview, 300)}\n… (+${lines.length - 3} lines)`;
|
|
86
|
+
}
|
|
87
|
+
return truncate(output, TOOL_OUTPUT_MAX_CHARS);
|
|
88
|
+
}
|
|
89
|
+
export class ClaudeCodeAgent extends BaseCliAgent {
|
|
90
|
+
opts;
|
|
91
|
+
capabilities;
|
|
92
|
+
cliEngine = "claude-code";
|
|
93
|
+
/**
|
|
94
|
+
* @param {ClaudeCodeAgentOptions} [opts]
|
|
95
|
+
*/
|
|
96
|
+
constructor(opts = {}) {
|
|
97
|
+
// Clear env vars that cause "Cannot run nested Claude Code instances" errors.
|
|
98
|
+
// CLAUDE_CODE_ENTRYPOINT / CLAUDECODE are set by a parent Claude Code process;
|
|
99
|
+
// child instances refuse to start when they detect these.
|
|
100
|
+
// ANTHROPIC_API_KEY is cleared so Claude Code uses the subscription instead of API billing.
|
|
101
|
+
const parentEnvOverrides = {};
|
|
102
|
+
if (process.env.CLAUDE_CODE_ENTRYPOINT)
|
|
103
|
+
parentEnvOverrides.CLAUDE_CODE_ENTRYPOINT = "";
|
|
104
|
+
if (process.env.CLAUDECODE)
|
|
105
|
+
parentEnvOverrides.CLAUDECODE = "";
|
|
106
|
+
if (process.env.ANTHROPIC_API_KEY) {
|
|
107
|
+
logWarning("ClaudeCodeAgent: unsetting ANTHROPIC_API_KEY so Claude Code uses your subscription. " +
|
|
108
|
+
"To use API billing instead, use ToolLoopAgent from 'ai' with anthropic() provider.", {}, "agent.init");
|
|
109
|
+
parentEnvOverrides.ANTHROPIC_API_KEY = "";
|
|
110
|
+
}
|
|
111
|
+
if (Object.keys(parentEnvOverrides).length > 0) {
|
|
112
|
+
opts = { ...opts, env: { ...parentEnvOverrides, ...opts.env } };
|
|
113
|
+
}
|
|
114
|
+
super(opts);
|
|
115
|
+
this.opts = opts;
|
|
116
|
+
this.capabilities = createClaudeCodeCapabilityRegistry(opts);
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* @returns {CliOutputInterpreter}
|
|
120
|
+
*/
|
|
121
|
+
createOutputInterpreter() {
|
|
122
|
+
let sessionId;
|
|
123
|
+
let didEmitStarted = false;
|
|
124
|
+
let didEmitCompleted = false;
|
|
125
|
+
let lastAssistantText = "";
|
|
126
|
+
const toolNameByUseId = new Map();
|
|
127
|
+
const nextSyntheticId = createSyntheticIdGenerator();
|
|
128
|
+
/**
|
|
129
|
+
* @param {string} title
|
|
130
|
+
* @param {string} message
|
|
131
|
+
* @param {"warning" | "error"} [level]
|
|
132
|
+
* @returns {AgentCliEvent}
|
|
133
|
+
*/
|
|
134
|
+
const warningAction = (title, message, level = "warning") => ({
|
|
135
|
+
type: "action",
|
|
136
|
+
engine: this.cliEngine,
|
|
137
|
+
phase: "completed",
|
|
138
|
+
entryType: "thought",
|
|
139
|
+
action: {
|
|
140
|
+
id: nextSyntheticId("claude-warning"),
|
|
141
|
+
kind: "warning",
|
|
142
|
+
title,
|
|
143
|
+
detail: {},
|
|
144
|
+
},
|
|
145
|
+
message,
|
|
146
|
+
ok: level !== "error",
|
|
147
|
+
level,
|
|
148
|
+
});
|
|
149
|
+
/**
|
|
150
|
+
* @param {string} line
|
|
151
|
+
* @returns {AgentCliEvent[]}
|
|
152
|
+
*/
|
|
153
|
+
const parseLine = (line) => {
|
|
154
|
+
const trimmedLine = line.trim();
|
|
155
|
+
if (!trimmedLine) {
|
|
156
|
+
return [];
|
|
157
|
+
}
|
|
158
|
+
let payload;
|
|
159
|
+
try {
|
|
160
|
+
payload = JSON.parse(trimmedLine);
|
|
161
|
+
}
|
|
162
|
+
catch {
|
|
163
|
+
if (!shouldSurfaceUnparsedStdout(trimmedLine)) {
|
|
164
|
+
return [];
|
|
165
|
+
}
|
|
166
|
+
return [warningAction("stdout", truncate(trimmedLine, 220), "warning")];
|
|
167
|
+
}
|
|
168
|
+
if (!isRecord(payload)) {
|
|
169
|
+
return [];
|
|
170
|
+
}
|
|
171
|
+
const payloadType = asString(payload.type);
|
|
172
|
+
if (!payloadType) {
|
|
173
|
+
return [];
|
|
174
|
+
}
|
|
175
|
+
if (payloadType === "system" && asString(payload.subtype) === "init") {
|
|
176
|
+
const parsedSessionId = asString(payload.session_id);
|
|
177
|
+
if (parsedSessionId) {
|
|
178
|
+
sessionId = parsedSessionId;
|
|
179
|
+
}
|
|
180
|
+
if (!didEmitStarted) {
|
|
181
|
+
didEmitStarted = true;
|
|
182
|
+
return [
|
|
183
|
+
{
|
|
184
|
+
type: "started",
|
|
185
|
+
engine: this.cliEngine,
|
|
186
|
+
title: "Claude Code",
|
|
187
|
+
resume: sessionId,
|
|
188
|
+
detail: sessionId ? { sessionId } : undefined,
|
|
189
|
+
},
|
|
190
|
+
];
|
|
191
|
+
}
|
|
192
|
+
return [];
|
|
193
|
+
}
|
|
194
|
+
if (payloadType === "assistant" || payloadType === "user") {
|
|
195
|
+
const message = isRecord(payload.message) ? payload.message : null;
|
|
196
|
+
const contentBlocks = message && Array.isArray(message.content) ? message.content : [];
|
|
197
|
+
const events = [];
|
|
198
|
+
for (const block of contentBlocks) {
|
|
199
|
+
if (!isRecord(block))
|
|
200
|
+
continue;
|
|
201
|
+
const blockType = asString(block.type);
|
|
202
|
+
if (!blockType)
|
|
203
|
+
continue;
|
|
204
|
+
if (blockType === "text") {
|
|
205
|
+
const text = asString(block.text)?.trim();
|
|
206
|
+
if (payloadType === "assistant" && text) {
|
|
207
|
+
lastAssistantText = text;
|
|
208
|
+
events.push({
|
|
209
|
+
type: "action",
|
|
210
|
+
engine: this.cliEngine,
|
|
211
|
+
phase: "updated",
|
|
212
|
+
entryType: "message",
|
|
213
|
+
action: {
|
|
214
|
+
id: nextSyntheticId("claude-text"),
|
|
215
|
+
kind: "note",
|
|
216
|
+
title: "assistant",
|
|
217
|
+
detail: {},
|
|
218
|
+
},
|
|
219
|
+
message: text,
|
|
220
|
+
ok: true,
|
|
221
|
+
level: "info",
|
|
222
|
+
});
|
|
223
|
+
}
|
|
224
|
+
continue;
|
|
225
|
+
}
|
|
226
|
+
if (blockType === "tool_use") {
|
|
227
|
+
const toolUseId = asString(block.id);
|
|
228
|
+
const toolName = asString(block.name) ?? "tool";
|
|
229
|
+
if (!toolUseId)
|
|
230
|
+
continue;
|
|
231
|
+
toolNameByUseId.set(toolUseId, toolName);
|
|
232
|
+
events.push({
|
|
233
|
+
type: "action",
|
|
234
|
+
engine: this.cliEngine,
|
|
235
|
+
phase: "started",
|
|
236
|
+
entryType: "thought",
|
|
237
|
+
action: {
|
|
238
|
+
id: toolUseId,
|
|
239
|
+
kind: toolKindFromName(toolName),
|
|
240
|
+
title: toolName,
|
|
241
|
+
detail: isRecord(block.input)
|
|
242
|
+
? {
|
|
243
|
+
input: block.input,
|
|
244
|
+
}
|
|
245
|
+
: {},
|
|
246
|
+
},
|
|
247
|
+
message: `Running ${toolName}`,
|
|
248
|
+
level: "info",
|
|
249
|
+
});
|
|
250
|
+
continue;
|
|
251
|
+
}
|
|
252
|
+
if (blockType === "tool_result") {
|
|
253
|
+
const toolUseId = asString(block.tool_use_id);
|
|
254
|
+
if (!toolUseId)
|
|
255
|
+
continue;
|
|
256
|
+
const toolName = toolNameByUseId.get(toolUseId) ?? "tool";
|
|
257
|
+
const toolResultContent = block.content;
|
|
258
|
+
const resultSummary = typeof toolResultContent === "string"
|
|
259
|
+
? toolResultContent
|
|
260
|
+
: Array.isArray(toolResultContent)
|
|
261
|
+
? toolResultContent
|
|
262
|
+
.map((entry) => (isRecord(entry) ? asString(entry.text) : undefined))
|
|
263
|
+
.filter((entry) => Boolean(entry))
|
|
264
|
+
.join("\n")
|
|
265
|
+
: undefined;
|
|
266
|
+
const isToolError = block.is_error === true;
|
|
267
|
+
const summarizedMessage = summarizeToolOutput(toolName, resultSummary);
|
|
268
|
+
events.push({
|
|
269
|
+
type: "action",
|
|
270
|
+
engine: this.cliEngine,
|
|
271
|
+
phase: "completed",
|
|
272
|
+
entryType: "thought",
|
|
273
|
+
action: {
|
|
274
|
+
id: toolUseId,
|
|
275
|
+
kind: toolKindFromName(toolName),
|
|
276
|
+
title: toolName,
|
|
277
|
+
detail: {},
|
|
278
|
+
},
|
|
279
|
+
message: summarizedMessage,
|
|
280
|
+
ok: !isToolError,
|
|
281
|
+
level: isToolError ? "warning" : "info",
|
|
282
|
+
});
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
return events;
|
|
286
|
+
}
|
|
287
|
+
if (payloadType === "result") {
|
|
288
|
+
if (didEmitCompleted) {
|
|
289
|
+
return [];
|
|
290
|
+
}
|
|
291
|
+
const denials = Array.isArray(payload.permission_denials) ? payload.permission_denials : [];
|
|
292
|
+
const events = denials
|
|
293
|
+
.map((denial) => {
|
|
294
|
+
if (!isRecord(denial))
|
|
295
|
+
return null;
|
|
296
|
+
const toolName = asString(denial.tool_name) ?? "tool";
|
|
297
|
+
return warningAction(`permission denied: ${toolName}`, `Permission denied for ${toolName}`, "warning");
|
|
298
|
+
})
|
|
299
|
+
.filter((event) => Boolean(event));
|
|
300
|
+
const subtype = asString(payload.subtype) ?? "success";
|
|
301
|
+
const isError = payload.is_error === true || subtype === "error";
|
|
302
|
+
const resultText = asString(payload.result);
|
|
303
|
+
const resultError = asString(payload.error);
|
|
304
|
+
didEmitCompleted = true;
|
|
305
|
+
events.push({
|
|
306
|
+
type: "completed",
|
|
307
|
+
engine: this.cliEngine,
|
|
308
|
+
ok: !isError,
|
|
309
|
+
answer: !isError ? resultText || lastAssistantText || undefined : undefined,
|
|
310
|
+
error: isError ? resultError || "Claude run failed" : undefined,
|
|
311
|
+
resume: asString(payload.session_id) ?? sessionId,
|
|
312
|
+
usage: isRecord(payload.usage) ? payload.usage : undefined,
|
|
313
|
+
});
|
|
314
|
+
return events;
|
|
315
|
+
}
|
|
316
|
+
return [];
|
|
317
|
+
};
|
|
318
|
+
return {
|
|
319
|
+
onStdoutLine: parseLine,
|
|
320
|
+
onStderrLine: (line) => {
|
|
321
|
+
const trimmedLine = line.trim();
|
|
322
|
+
if (!trimmedLine) {
|
|
323
|
+
return [];
|
|
324
|
+
}
|
|
325
|
+
return [warningAction("stderr", truncate(trimmedLine, 220), "warning")];
|
|
326
|
+
},
|
|
327
|
+
onExit: (result) => {
|
|
328
|
+
if (didEmitCompleted) {
|
|
329
|
+
return [];
|
|
330
|
+
}
|
|
331
|
+
didEmitCompleted = true;
|
|
332
|
+
const isSuccess = (result.exitCode ?? 0) === 0;
|
|
333
|
+
return [
|
|
334
|
+
{
|
|
335
|
+
type: "completed",
|
|
336
|
+
engine: this.cliEngine,
|
|
337
|
+
ok: isSuccess,
|
|
338
|
+
answer: isSuccess ? lastAssistantText || undefined : undefined,
|
|
339
|
+
error: isSuccess ? undefined : `Claude exited with code ${result.exitCode ?? -1}`,
|
|
340
|
+
resume: sessionId,
|
|
341
|
+
},
|
|
342
|
+
];
|
|
343
|
+
},
|
|
344
|
+
};
|
|
345
|
+
}
|
|
346
|
+
/**
|
|
347
|
+
* @param {{ prompt: string; systemPrompt?: string; cwd: string; options: any; }} params
|
|
348
|
+
*/
|
|
349
|
+
async buildCommand(params) {
|
|
350
|
+
const args = ["--print"];
|
|
351
|
+
// Default to "stream-json" to capture NDJSON events that include token
|
|
352
|
+
// usage (message_start has input_tokens, message_delta has output_tokens).
|
|
353
|
+
// BaseCliAgent.extractUsageFromOutput will parse these for metrics.
|
|
354
|
+
const outputFormat = this.opts.outputFormat ?? "stream-json";
|
|
355
|
+
// Recent Claude CLI builds require --verbose when --print is combined with
|
|
356
|
+
// --output-format=stream-json.
|
|
357
|
+
const requiresVerbose = outputFormat === "stream-json";
|
|
358
|
+
pushList(args, "--add-dir", this.opts.addDir);
|
|
359
|
+
pushFlag(args, "--agent", this.opts.agent);
|
|
360
|
+
if (this.opts.agents) {
|
|
361
|
+
const agentsJson = typeof this.opts.agents === "string"
|
|
362
|
+
? this.opts.agents
|
|
363
|
+
: JSON.stringify(this.opts.agents);
|
|
364
|
+
pushFlag(args, "--agents", agentsJson);
|
|
365
|
+
}
|
|
366
|
+
const yoloEnabled = this.opts.yolo ?? this.yolo;
|
|
367
|
+
if (yoloEnabled) {
|
|
368
|
+
args.push("--allow-dangerously-skip-permissions");
|
|
369
|
+
args.push("--dangerously-skip-permissions");
|
|
370
|
+
if (!this.opts.permissionMode) {
|
|
371
|
+
args.push("--permission-mode", "bypassPermissions");
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
if (this.opts.allowDangerouslySkipPermissions)
|
|
375
|
+
args.push("--allow-dangerously-skip-permissions");
|
|
376
|
+
if (this.opts.dangerouslySkipPermissions)
|
|
377
|
+
args.push("--dangerously-skip-permissions");
|
|
378
|
+
pushList(args, "--allowed-tools", this.opts.allowedTools);
|
|
379
|
+
pushFlag(args, "--append-system-prompt", this.opts.appendSystemPrompt);
|
|
380
|
+
pushList(args, "--betas", this.opts.betas);
|
|
381
|
+
if (this.opts.chrome)
|
|
382
|
+
args.push("--chrome");
|
|
383
|
+
if (this.opts.noChrome)
|
|
384
|
+
args.push("--no-chrome");
|
|
385
|
+
if (this.opts.continue)
|
|
386
|
+
args.push("--continue");
|
|
387
|
+
if (this.opts.debug === true) {
|
|
388
|
+
args.push("--debug");
|
|
389
|
+
}
|
|
390
|
+
else if (typeof this.opts.debug === "string") {
|
|
391
|
+
pushFlag(args, "--debug", this.opts.debug);
|
|
392
|
+
}
|
|
393
|
+
pushFlag(args, "--debug-file", this.opts.debugFile);
|
|
394
|
+
if (this.opts.disableSlashCommands)
|
|
395
|
+
args.push("--disable-slash-commands");
|
|
396
|
+
pushList(args, "--disallowed-tools", this.opts.disallowedTools);
|
|
397
|
+
pushFlag(args, "--fallback-model", this.opts.fallbackModel);
|
|
398
|
+
pushList(args, "--file", this.opts.file);
|
|
399
|
+
if (this.opts.forkSession)
|
|
400
|
+
args.push("--fork-session");
|
|
401
|
+
pushFlag(args, "--from-pr", this.opts.fromPr);
|
|
402
|
+
if (this.opts.ide)
|
|
403
|
+
args.push("--ide");
|
|
404
|
+
if (this.opts.includePartialMessages)
|
|
405
|
+
args.push("--include-partial-messages");
|
|
406
|
+
pushFlag(args, "--input-format", this.opts.inputFormat);
|
|
407
|
+
pushFlag(args, "--json-schema", this.opts.jsonSchema);
|
|
408
|
+
pushFlag(args, "--max-budget-usd", this.opts.maxBudgetUsd);
|
|
409
|
+
pushList(args, "--mcp-config", this.opts.mcpConfig);
|
|
410
|
+
if (this.opts.mcpDebug)
|
|
411
|
+
args.push("--mcp-debug");
|
|
412
|
+
pushFlag(args, "--model", this.opts.model ?? this.model);
|
|
413
|
+
if (this.opts.noSessionPersistence)
|
|
414
|
+
args.push("--no-session-persistence");
|
|
415
|
+
pushFlag(args, "--output-format", outputFormat);
|
|
416
|
+
pushFlag(args, "--permission-mode", this.opts.permissionMode);
|
|
417
|
+
pushList(args, "--plugin-dir", this.opts.pluginDir);
|
|
418
|
+
if (this.opts.replayUserMessages)
|
|
419
|
+
args.push("--replay-user-messages");
|
|
420
|
+
const resumeSession = typeof params.options?.resumeSession === "string"
|
|
421
|
+
? params.options.resumeSession
|
|
422
|
+
: undefined;
|
|
423
|
+
pushFlag(args, "--resume", resumeSession ?? this.opts.resume);
|
|
424
|
+
pushFlag(args, "--session-id", this.opts.sessionId);
|
|
425
|
+
pushFlag(args, "--setting-sources", this.opts.settingSources);
|
|
426
|
+
pushFlag(args, "--settings", this.opts.settings);
|
|
427
|
+
if (this.opts.strictMcpConfig)
|
|
428
|
+
args.push("--strict-mcp-config");
|
|
429
|
+
if (params.systemPrompt) {
|
|
430
|
+
pushFlag(args, "--system-prompt", params.systemPrompt);
|
|
431
|
+
}
|
|
432
|
+
if (this.opts.tools !== undefined) {
|
|
433
|
+
if (this.opts.tools === "") {
|
|
434
|
+
pushFlag(args, "--tools", "");
|
|
435
|
+
}
|
|
436
|
+
else if (this.opts.tools === "default") {
|
|
437
|
+
pushFlag(args, "--tools", "default");
|
|
438
|
+
}
|
|
439
|
+
else {
|
|
440
|
+
pushList(args, "--tools", this.opts.tools);
|
|
441
|
+
}
|
|
442
|
+
}
|
|
443
|
+
if (this.opts.verbose || requiresVerbose)
|
|
444
|
+
args.push("--verbose");
|
|
445
|
+
if (this.extraArgs?.length)
|
|
446
|
+
args.push(...this.extraArgs);
|
|
447
|
+
if (params.prompt)
|
|
448
|
+
args.push(params.prompt);
|
|
449
|
+
return {
|
|
450
|
+
command: "claude",
|
|
451
|
+
args,
|
|
452
|
+
outputFormat,
|
|
453
|
+
};
|
|
454
|
+
}
|
|
455
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import type { BaseCliAgentOptions } from "./BaseCliAgent/BaseCliAgentOptions";
|
|
2
|
+
|
|
3
|
+
export type ClaudeCodeAgentOptions = BaseCliAgentOptions & {
|
|
4
|
+
addDir?: string[];
|
|
5
|
+
agent?: string;
|
|
6
|
+
agents?:
|
|
7
|
+
| Record<string, { description?: string; prompt?: string }>
|
|
8
|
+
| string;
|
|
9
|
+
allowDangerouslySkipPermissions?: boolean;
|
|
10
|
+
allowedTools?: string[];
|
|
11
|
+
appendSystemPrompt?: string;
|
|
12
|
+
betas?: string[];
|
|
13
|
+
chrome?: boolean;
|
|
14
|
+
continue?: boolean;
|
|
15
|
+
dangerouslySkipPermissions?: boolean;
|
|
16
|
+
debug?: boolean | string;
|
|
17
|
+
debugFile?: string;
|
|
18
|
+
disableSlashCommands?: boolean;
|
|
19
|
+
disallowedTools?: string[];
|
|
20
|
+
fallbackModel?: string;
|
|
21
|
+
file?: string[];
|
|
22
|
+
forkSession?: boolean;
|
|
23
|
+
fromPr?: string;
|
|
24
|
+
ide?: boolean;
|
|
25
|
+
includePartialMessages?: boolean;
|
|
26
|
+
inputFormat?: "text" | "stream-json";
|
|
27
|
+
jsonSchema?: string;
|
|
28
|
+
maxBudgetUsd?: number;
|
|
29
|
+
mcpConfig?: string[];
|
|
30
|
+
mcpDebug?: boolean;
|
|
31
|
+
model?: string;
|
|
32
|
+
noChrome?: boolean;
|
|
33
|
+
noSessionPersistence?: boolean;
|
|
34
|
+
outputFormat?: "text" | "json" | "stream-json";
|
|
35
|
+
permissionMode?:
|
|
36
|
+
| "acceptEdits"
|
|
37
|
+
| "bypassPermissions"
|
|
38
|
+
| "default"
|
|
39
|
+
| "delegate"
|
|
40
|
+
| "dontAsk"
|
|
41
|
+
| "plan";
|
|
42
|
+
pluginDir?: string[];
|
|
43
|
+
replayUserMessages?: boolean;
|
|
44
|
+
resume?: string;
|
|
45
|
+
sessionId?: string;
|
|
46
|
+
settingSources?: string;
|
|
47
|
+
settings?: string;
|
|
48
|
+
strictMcpConfig?: boolean;
|
|
49
|
+
systemPrompt?: string;
|
|
50
|
+
tools?: string[] | "default" | "";
|
|
51
|
+
verbose?: boolean;
|
|
52
|
+
};
|