@polka-codes/core 0.9.27 → 0.9.29
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/_tsup-dts-rollup.d.ts +85 -233
- package/dist/index.d.ts +3 -2
- package/dist/index.js +276 -478
- package/package.json +3 -3
package/dist/index.js
CHANGED
|
@@ -1,28 +1,3 @@
|
|
|
1
|
-
var __defProp = Object.defineProperty;
|
|
2
|
-
var __export = (target, all) => {
|
|
3
|
-
for (var name in all)
|
|
4
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
5
|
-
};
|
|
6
|
-
|
|
7
|
-
// src/tools/allTools.ts
|
|
8
|
-
var allTools_exports = {};
|
|
9
|
-
__export(allTools_exports, {
|
|
10
|
-
askFollowupQuestion: () => askFollowupQuestion_default,
|
|
11
|
-
attemptCompletion: () => attemptCompletion_default,
|
|
12
|
-
delegate: () => delegate_default,
|
|
13
|
-
executeCommand: () => executeCommand_default,
|
|
14
|
-
fetchUrl: () => fetchUrl_default,
|
|
15
|
-
handOver: () => handOver_default,
|
|
16
|
-
listFiles: () => listFiles_default,
|
|
17
|
-
readBinaryFile: () => readBinaryFile_default,
|
|
18
|
-
readFile: () => readFile_default,
|
|
19
|
-
removeFile: () => removeFile_default,
|
|
20
|
-
renameFile: () => renameFile_default,
|
|
21
|
-
replaceInFile: () => replaceInFile_default,
|
|
22
|
-
searchFiles: () => searchFiles_default,
|
|
23
|
-
writeToFile: () => writeToFile_default
|
|
24
|
-
});
|
|
25
|
-
|
|
26
1
|
// src/tools/askFollowupQuestion.ts
|
|
27
2
|
import { z } from "zod";
|
|
28
3
|
|
|
@@ -112,9 +87,9 @@ var handler = async (provider, args) => {
|
|
|
112
87
|
}
|
|
113
88
|
const answers = [];
|
|
114
89
|
for (const question of questions) {
|
|
115
|
-
const { prompt:
|
|
116
|
-
const answer = await provider.askFollowupQuestion(
|
|
117
|
-
answers.push(`<ask_followup_question_answer question="${
|
|
90
|
+
const { prompt: prompt5, options } = question;
|
|
91
|
+
const answer = await provider.askFollowupQuestion(prompt5, options);
|
|
92
|
+
answers.push(`<ask_followup_question_answer question="${prompt5}">
|
|
118
93
|
${answer}
|
|
119
94
|
</ask_followup_question_answer>`);
|
|
120
95
|
}
|
|
@@ -287,11 +262,11 @@ var handler4 = async (provider, args) => {
|
|
|
287
262
|
}
|
|
288
263
|
};
|
|
289
264
|
}
|
|
290
|
-
const { command, requiresApproval } = toolInfo4.parameters.parse(args);
|
|
265
|
+
const { command: command2, requiresApproval } = toolInfo4.parameters.parse(args);
|
|
291
266
|
try {
|
|
292
|
-
console.log("Executing command:",
|
|
293
|
-
const result = await provider.executeCommand(
|
|
294
|
-
const message = `<command>${
|
|
267
|
+
console.log("Executing command:", command2, "Requires approval:", requiresApproval);
|
|
268
|
+
const result = await provider.executeCommand(command2, requiresApproval);
|
|
269
|
+
const message = `<command>${command2}</command>
|
|
295
270
|
<command_exit_code>${result.exitCode}</command_exit_code>
|
|
296
271
|
<command_stdout>
|
|
297
272
|
${result.stdout}
|
|
@@ -542,6 +517,37 @@ var listFiles_default = {
|
|
|
542
517
|
isAvailable: isAvailable7
|
|
543
518
|
};
|
|
544
519
|
|
|
520
|
+
// src/tools/provider.ts
|
|
521
|
+
var MockProvider = class {
|
|
522
|
+
async readFile(_path, _includeIgnored) {
|
|
523
|
+
return "mock content";
|
|
524
|
+
}
|
|
525
|
+
async writeFile(_path, _content) {
|
|
526
|
+
return;
|
|
527
|
+
}
|
|
528
|
+
async removeFile(_path) {
|
|
529
|
+
return;
|
|
530
|
+
}
|
|
531
|
+
async renameFile(_sourcePath, _targetPath) {
|
|
532
|
+
return;
|
|
533
|
+
}
|
|
534
|
+
async listFiles(_path, _recursive, _maxCount, _includeIgnored) {
|
|
535
|
+
return [["mock-file.txt"], false];
|
|
536
|
+
}
|
|
537
|
+
async searchFiles(_path, _regex, _filePattern) {
|
|
538
|
+
return ["mock-file.txt"];
|
|
539
|
+
}
|
|
540
|
+
async executeCommand(_command, _needApprove) {
|
|
541
|
+
return { stdout: "mock output", stderr: "", exitCode: 0 };
|
|
542
|
+
}
|
|
543
|
+
async askFollowupQuestion(_question, _options) {
|
|
544
|
+
return "mock answer";
|
|
545
|
+
}
|
|
546
|
+
async attemptCompletion(_result) {
|
|
547
|
+
return "mock completion";
|
|
548
|
+
}
|
|
549
|
+
};
|
|
550
|
+
|
|
545
551
|
// src/tools/readBinaryFile.ts
|
|
546
552
|
import { z as z8 } from "zod";
|
|
547
553
|
var toolInfo8 = {
|
|
@@ -1213,47 +1219,14 @@ var writeToFile_default = {
|
|
|
1213
1219
|
isAvailable: isAvailable14
|
|
1214
1220
|
};
|
|
1215
1221
|
|
|
1216
|
-
// src/tools/provider.ts
|
|
1217
|
-
var MockProvider = class {
|
|
1218
|
-
async readFile(_path, _includeIgnored) {
|
|
1219
|
-
return "mock content";
|
|
1220
|
-
}
|
|
1221
|
-
async writeFile(_path, _content) {
|
|
1222
|
-
return;
|
|
1223
|
-
}
|
|
1224
|
-
async removeFile(_path) {
|
|
1225
|
-
return;
|
|
1226
|
-
}
|
|
1227
|
-
async renameFile(_sourcePath, _targetPath) {
|
|
1228
|
-
return;
|
|
1229
|
-
}
|
|
1230
|
-
async listFiles(_path, _recursive, _maxCount, _includeIgnored) {
|
|
1231
|
-
return [["mock-file.txt"], false];
|
|
1232
|
-
}
|
|
1233
|
-
async searchFiles(_path, _regex, _filePattern) {
|
|
1234
|
-
return ["mock-file.txt"];
|
|
1235
|
-
}
|
|
1236
|
-
async executeCommand(_command, _needApprove) {
|
|
1237
|
-
return { stdout: "mock output", stderr: "", exitCode: 0 };
|
|
1238
|
-
}
|
|
1239
|
-
async askFollowupQuestion(_question, _options) {
|
|
1240
|
-
return "mock answer";
|
|
1241
|
-
}
|
|
1242
|
-
async attemptCompletion(_result) {
|
|
1243
|
-
return "mock completion";
|
|
1244
|
-
}
|
|
1245
|
-
};
|
|
1246
|
-
|
|
1247
1222
|
// src/getAvailableTools.ts
|
|
1248
1223
|
var getAvailableTools = ({
|
|
1249
1224
|
provider,
|
|
1250
1225
|
allTools,
|
|
1251
|
-
hasAgent
|
|
1252
|
-
interactive
|
|
1226
|
+
hasAgent
|
|
1253
1227
|
}) => {
|
|
1254
|
-
const filteredTools = interactive ? allTools : allTools.filter((tool) => tool.name !== askFollowupQuestion_default.name);
|
|
1255
1228
|
const tools = [];
|
|
1256
|
-
for (const tool of
|
|
1229
|
+
for (const tool of allTools) {
|
|
1257
1230
|
if (!hasAgent) {
|
|
1258
1231
|
switch (tool.name) {
|
|
1259
1232
|
case handOver_default.name:
|
|
@@ -1270,8 +1243,7 @@ var getAvailableTools = ({
|
|
|
1270
1243
|
|
|
1271
1244
|
// src/UsageMeter.ts
|
|
1272
1245
|
var UsageMeter = class {
|
|
1273
|
-
#totals = { input: 0, output: 0, cachedRead: 0, cost: 0 };
|
|
1274
|
-
#calls = 0;
|
|
1246
|
+
#totals = { input: 0, output: 0, cachedRead: 0, cost: 0, messageCount: 0 };
|
|
1275
1247
|
#modelInfos;
|
|
1276
1248
|
#maxMessages;
|
|
1277
1249
|
#maxCost;
|
|
@@ -1354,7 +1326,7 @@ var UsageMeter = class {
|
|
|
1354
1326
|
this.#totals.output += result.output;
|
|
1355
1327
|
this.#totals.cachedRead += result.cachedRead;
|
|
1356
1328
|
this.#totals.cost += result.cost;
|
|
1357
|
-
this.#
|
|
1329
|
+
this.#totals.messageCount += 1;
|
|
1358
1330
|
}
|
|
1359
1331
|
/** Override the running totals (e.g., restore from saved state). */
|
|
1360
1332
|
setUsage(newUsage) {
|
|
@@ -1362,20 +1334,19 @@ var UsageMeter = class {
|
|
|
1362
1334
|
if (newUsage.output != null) this.#totals.output = newUsage.output;
|
|
1363
1335
|
if (newUsage.cachedRead != null) this.#totals.cachedRead = newUsage.cachedRead;
|
|
1364
1336
|
if (newUsage.cost != null) this.#totals.cost = newUsage.cost;
|
|
1365
|
-
if (newUsage.
|
|
1337
|
+
if (newUsage.messageCount != null) this.#totals.messageCount = newUsage.messageCount;
|
|
1366
1338
|
}
|
|
1367
|
-
/** Manually bump the message count (useful if you record some
|
|
1339
|
+
/** Manually bump the message count (useful if you record some messages without token info). */
|
|
1368
1340
|
incrementMessageCount(n = 1) {
|
|
1369
|
-
this.#
|
|
1341
|
+
this.#totals.messageCount += n;
|
|
1370
1342
|
}
|
|
1371
1343
|
/** Reset the running totals. */
|
|
1372
1344
|
resetUsage() {
|
|
1373
|
-
this.#totals = { input: 0, output: 0, cachedRead: 0, cost: 0 };
|
|
1374
|
-
this.#calls = 0;
|
|
1345
|
+
this.#totals = { input: 0, output: 0, cachedRead: 0, cost: 0, messageCount: 0 };
|
|
1375
1346
|
}
|
|
1376
1347
|
/** Return true once either messages or cost exceed the configured caps. */
|
|
1377
1348
|
isLimitExceeded() {
|
|
1378
|
-
const messageCount = this.#maxMessages > 0 && this.#
|
|
1349
|
+
const messageCount = this.#maxMessages > 0 && this.#totals.messageCount >= this.#maxMessages;
|
|
1379
1350
|
const cost = this.#maxCost > 0 && this.#totals.cost >= this.#maxCost;
|
|
1380
1351
|
return {
|
|
1381
1352
|
messageCount,
|
|
@@ -1396,7 +1367,7 @@ var UsageMeter = class {
|
|
|
1396
1367
|
}
|
|
1397
1368
|
/** Getter for the aggregated totals (immutable copy). */
|
|
1398
1369
|
get usage() {
|
|
1399
|
-
return { ...this.#totals
|
|
1370
|
+
return { ...this.#totals };
|
|
1400
1371
|
}
|
|
1401
1372
|
/** Print a concise usage summary to console. */
|
|
1402
1373
|
printUsage(customConsole = console) {
|
|
@@ -1774,14 +1745,14 @@ The following additional instructions are provided by the user, and should be fo
|
|
|
1774
1745
|
${joined}`;
|
|
1775
1746
|
};
|
|
1776
1747
|
var customScripts = (commands) => {
|
|
1777
|
-
const joined = Object.entries(commands).map(([name,
|
|
1778
|
-
if (typeof
|
|
1748
|
+
const joined = Object.entries(commands).map(([name, command2]) => {
|
|
1749
|
+
if (typeof command2 === "string") {
|
|
1779
1750
|
return `- ${name}
|
|
1780
|
-
- Command: \`${
|
|
1751
|
+
- Command: \`${command2}\``;
|
|
1781
1752
|
}
|
|
1782
1753
|
return `- ${name}
|
|
1783
|
-
- Command: \`${
|
|
1784
|
-
- Description: ${
|
|
1754
|
+
- Command: \`${command2.command}\`
|
|
1755
|
+
- Description: ${command2.description}`;
|
|
1785
1756
|
}).join("\n");
|
|
1786
1757
|
if (joined.trim() === "") {
|
|
1787
1758
|
return "";
|
|
@@ -1866,7 +1837,7 @@ Avoid unnecessary escape characters or special characters.
|
|
|
1866
1837
|
];
|
|
1867
1838
|
}
|
|
1868
1839
|
},
|
|
1869
|
-
commandResult: (
|
|
1840
|
+
commandResult: (command2, exitCode, stdout, stderr) => `<command>${command2}</command>
|
|
1870
1841
|
<command_exit_code>${exitCode}</command_exit_code>
|
|
1871
1842
|
<command_stdout>
|
|
1872
1843
|
${stdout}
|
|
@@ -1972,18 +1943,18 @@ ${instance.prompt}`;
|
|
|
1972
1943
|
async #callback(event) {
|
|
1973
1944
|
await this.config.callback?.(event);
|
|
1974
1945
|
}
|
|
1975
|
-
async start(
|
|
1946
|
+
async start(prompt5) {
|
|
1976
1947
|
this.#callback({ kind: "StartTask" /* StartTask */, agent: this, systemPrompt: this.config.systemPrompt });
|
|
1977
|
-
return await this.#processLoop(
|
|
1948
|
+
return await this.#processLoop(prompt5);
|
|
1978
1949
|
}
|
|
1979
|
-
async step(
|
|
1950
|
+
async step(prompt5) {
|
|
1980
1951
|
if (this.#messages.length === 0) {
|
|
1981
1952
|
this.#callback({ kind: "StartTask" /* StartTask */, agent: this, systemPrompt: this.config.systemPrompt });
|
|
1982
1953
|
}
|
|
1983
1954
|
return await this.#request([
|
|
1984
1955
|
{
|
|
1985
1956
|
role: "user",
|
|
1986
|
-
content:
|
|
1957
|
+
content: prompt5
|
|
1987
1958
|
}
|
|
1988
1959
|
]);
|
|
1989
1960
|
}
|
|
@@ -2404,8 +2375,8 @@ Retrying request ${i + 2} of ${retryCount}`);
|
|
|
2404
2375
|
}
|
|
2405
2376
|
async #invokeTool(name, args) {
|
|
2406
2377
|
try {
|
|
2407
|
-
const
|
|
2408
|
-
if (!
|
|
2378
|
+
const handler15 = this.handlers[name]?.handler;
|
|
2379
|
+
if (!handler15) {
|
|
2409
2380
|
return {
|
|
2410
2381
|
type: "Error" /* Error */,
|
|
2411
2382
|
message: {
|
|
@@ -2427,7 +2398,7 @@ Retrying request ${i + 2} of ${retryCount}`);
|
|
|
2427
2398
|
if (resp) {
|
|
2428
2399
|
return resp;
|
|
2429
2400
|
}
|
|
2430
|
-
return await
|
|
2401
|
+
return await handler15(this.config.provider, args);
|
|
2431
2402
|
} catch (error) {
|
|
2432
2403
|
return {
|
|
2433
2404
|
type: "Error" /* Error */,
|
|
@@ -2477,15 +2448,14 @@ ${customScripts(scripts)}
|
|
|
2477
2448
|
`;
|
|
2478
2449
|
|
|
2479
2450
|
// src/Agent/AnalyzerAgent/index.ts
|
|
2480
|
-
var agentTools = [
|
|
2451
|
+
var agentTools = [attemptCompletion_default, delegate_default, fetchUrl_default, handOver_default, listFiles_default, readBinaryFile_default, readFile_default, searchFiles_default];
|
|
2481
2452
|
var AnalyzerAgent = class extends AgentBase {
|
|
2482
2453
|
constructor(options) {
|
|
2483
2454
|
const combinedTools = [...options.additionalTools ?? [], ...agentTools];
|
|
2484
2455
|
const tools = getAvailableTools({
|
|
2485
2456
|
provider: options.provider,
|
|
2486
2457
|
allTools: combinedTools,
|
|
2487
|
-
hasAgent: (options.agents?.length ?? 0) > 0
|
|
2488
|
-
interactive: true
|
|
2458
|
+
hasAgent: (options.agents?.length ?? 0) > 0
|
|
2489
2459
|
});
|
|
2490
2460
|
const toolNamePrefix = options.toolFormat === "native" ? "" : "tool_";
|
|
2491
2461
|
const systemPrompt = fullSystemPrompt(
|
|
@@ -2503,7 +2473,6 @@ var AnalyzerAgent = class extends AgentBase {
|
|
|
2503
2473
|
tools,
|
|
2504
2474
|
toolNamePrefix,
|
|
2505
2475
|
provider: options.provider,
|
|
2506
|
-
interactive: options.interactive,
|
|
2507
2476
|
agents: options.agents,
|
|
2508
2477
|
scripts: options.scripts,
|
|
2509
2478
|
callback: options.callback,
|
|
@@ -2576,15 +2545,14 @@ ${customScripts(scripts)}
|
|
|
2576
2545
|
`;
|
|
2577
2546
|
|
|
2578
2547
|
// src/Agent/ArchitectAgent/index.ts
|
|
2579
|
-
var agentTools2 = [
|
|
2548
|
+
var agentTools2 = [attemptCompletion_default, delegate_default, fetchUrl_default, handOver_default, listFiles_default, readBinaryFile_default, readFile_default, searchFiles_default];
|
|
2580
2549
|
var ArchitectAgent = class extends AgentBase {
|
|
2581
2550
|
constructor(options) {
|
|
2582
2551
|
const combinedTools = [...options.additionalTools ?? [], ...agentTools2];
|
|
2583
2552
|
const tools = getAvailableTools({
|
|
2584
2553
|
provider: options.provider,
|
|
2585
2554
|
allTools: combinedTools,
|
|
2586
|
-
hasAgent: (options.agents?.length ?? 0) > 0
|
|
2587
|
-
interactive: true
|
|
2555
|
+
hasAgent: (options.agents?.length ?? 0) > 0
|
|
2588
2556
|
});
|
|
2589
2557
|
const toolNamePrefix = options.toolFormat === "native" ? "" : "tool_";
|
|
2590
2558
|
const systemPrompt = fullSystemPrompt2(
|
|
@@ -2602,7 +2570,6 @@ var ArchitectAgent = class extends AgentBase {
|
|
|
2602
2570
|
tools,
|
|
2603
2571
|
toolNamePrefix,
|
|
2604
2572
|
provider: options.provider,
|
|
2605
|
-
interactive: options.interactive,
|
|
2606
2573
|
agents: options.agents,
|
|
2607
2574
|
scripts: options.scripts,
|
|
2608
2575
|
callback: options.callback,
|
|
@@ -2694,7 +2661,7 @@ RETRY GUIDELINES
|
|
|
2694
2661
|
- Explain why the issue remains
|
|
2695
2662
|
- Suggest manual intervention steps
|
|
2696
2663
|
- Report any partial improvements`;
|
|
2697
|
-
var fullSystemPrompt3 = (info, tools, toolNamePrefix, instructions, scripts,
|
|
2664
|
+
var fullSystemPrompt3 = (info, tools, toolNamePrefix, instructions, scripts, useNativeTool) => `
|
|
2698
2665
|
${basePrompt}
|
|
2699
2666
|
${toolUsePrompt(useNativeTool, tools, toolNamePrefix)}
|
|
2700
2667
|
${codeFixingStrategies}
|
|
@@ -2705,16 +2672,30 @@ ${customInstructions(instructions)}
|
|
|
2705
2672
|
${customScripts(scripts)}`;
|
|
2706
2673
|
|
|
2707
2674
|
// src/Agent/CodeFixerAgent/index.ts
|
|
2675
|
+
var agentTools3 = [
|
|
2676
|
+
attemptCompletion_default,
|
|
2677
|
+
delegate_default,
|
|
2678
|
+
executeCommand_default,
|
|
2679
|
+
fetchUrl_default,
|
|
2680
|
+
handOver_default,
|
|
2681
|
+
listFiles_default,
|
|
2682
|
+
readBinaryFile_default,
|
|
2683
|
+
readFile_default,
|
|
2684
|
+
removeFile_default,
|
|
2685
|
+
renameFile_default,
|
|
2686
|
+
replaceInFile_default,
|
|
2687
|
+
searchFiles_default,
|
|
2688
|
+
writeToFile_default
|
|
2689
|
+
];
|
|
2708
2690
|
var CodeFixerAgent = class extends AgentBase {
|
|
2709
2691
|
#maxRetries;
|
|
2710
2692
|
#retryCount = 0;
|
|
2711
2693
|
constructor(options) {
|
|
2712
|
-
const combinedTools = [...options.additionalTools ?? [], ...
|
|
2694
|
+
const combinedTools = [...options.additionalTools ?? [], ...agentTools3];
|
|
2713
2695
|
const tools = getAvailableTools({
|
|
2714
2696
|
provider: options.provider,
|
|
2715
2697
|
allTools: combinedTools,
|
|
2716
|
-
hasAgent: (options.agents?.length ?? 0) > 0
|
|
2717
|
-
interactive: true
|
|
2698
|
+
hasAgent: (options.agents?.length ?? 0) > 0
|
|
2718
2699
|
});
|
|
2719
2700
|
const toolNamePrefix = options.toolFormat === "native" ? "" : "tool_";
|
|
2720
2701
|
const systemPrompt = fullSystemPrompt3(
|
|
@@ -2725,7 +2706,6 @@ var CodeFixerAgent = class extends AgentBase {
|
|
|
2725
2706
|
toolNamePrefix,
|
|
2726
2707
|
options.customInstructions ?? [],
|
|
2727
2708
|
options.scripts ?? {},
|
|
2728
|
-
options.interactive,
|
|
2729
2709
|
options.toolFormat === "native"
|
|
2730
2710
|
);
|
|
2731
2711
|
super(codeFixerAgentInfo.name, options.ai, {
|
|
@@ -2733,7 +2713,6 @@ var CodeFixerAgent = class extends AgentBase {
|
|
|
2733
2713
|
tools,
|
|
2734
2714
|
toolNamePrefix,
|
|
2735
2715
|
provider: options.provider,
|
|
2736
|
-
interactive: options.interactive,
|
|
2737
2716
|
agents: options.agents,
|
|
2738
2717
|
scripts: options.scripts,
|
|
2739
2718
|
callback: options.callback,
|
|
@@ -2920,14 +2899,28 @@ ${customScripts(scripts)}
|
|
|
2920
2899
|
`;
|
|
2921
2900
|
|
|
2922
2901
|
// src/Agent/CoderAgent/index.ts
|
|
2902
|
+
var agentTools4 = [
|
|
2903
|
+
attemptCompletion_default,
|
|
2904
|
+
delegate_default,
|
|
2905
|
+
executeCommand_default,
|
|
2906
|
+
fetchUrl_default,
|
|
2907
|
+
handOver_default,
|
|
2908
|
+
listFiles_default,
|
|
2909
|
+
readBinaryFile_default,
|
|
2910
|
+
readFile_default,
|
|
2911
|
+
removeFile_default,
|
|
2912
|
+
renameFile_default,
|
|
2913
|
+
replaceInFile_default,
|
|
2914
|
+
searchFiles_default,
|
|
2915
|
+
writeToFile_default
|
|
2916
|
+
];
|
|
2923
2917
|
var CoderAgent = class extends AgentBase {
|
|
2924
2918
|
constructor(options) {
|
|
2925
|
-
const combinedTools = [...options.additionalTools ?? [], ...
|
|
2919
|
+
const combinedTools = [...options.additionalTools ?? [], ...agentTools4];
|
|
2926
2920
|
const tools = getAvailableTools({
|
|
2927
2921
|
provider: options.provider,
|
|
2928
2922
|
allTools: combinedTools,
|
|
2929
|
-
hasAgent: (options.agents?.length ?? 0) > 0
|
|
2930
|
-
interactive: true
|
|
2923
|
+
hasAgent: (options.agents?.length ?? 0) > 0
|
|
2931
2924
|
});
|
|
2932
2925
|
const toolNamePrefix = options.toolFormat === "native" ? "" : "tool_";
|
|
2933
2926
|
const systemPrompt = fullSystemPrompt4(
|
|
@@ -2945,7 +2938,6 @@ var CoderAgent = class extends AgentBase {
|
|
|
2945
2938
|
tools,
|
|
2946
2939
|
toolNamePrefix,
|
|
2947
2940
|
provider: options.provider,
|
|
2948
|
-
interactive: options.interactive,
|
|
2949
2941
|
agents: options.agents,
|
|
2950
2942
|
scripts: options.scripts,
|
|
2951
2943
|
callback: options.callback,
|
|
@@ -2963,21 +2955,21 @@ var CoderAgent = class extends AgentBase {
|
|
|
2963
2955
|
return;
|
|
2964
2956
|
}
|
|
2965
2957
|
const script = this.config.scripts?.[scriptName];
|
|
2966
|
-
const
|
|
2967
|
-
if (
|
|
2958
|
+
const command2 = typeof script === "string" ? script : script?.command;
|
|
2959
|
+
if (command2) {
|
|
2968
2960
|
try {
|
|
2969
|
-
const { exitCode, stdout, stderr } = await executeCommand(
|
|
2961
|
+
const { exitCode, stdout, stderr } = await executeCommand(command2, false);
|
|
2970
2962
|
if (exitCode !== 0 && shouldReplyWithError) {
|
|
2971
2963
|
return {
|
|
2972
2964
|
type: "Reply" /* Reply */,
|
|
2973
2965
|
message: {
|
|
2974
2966
|
type: "error-text",
|
|
2975
|
-
value: responsePrompts.commandResult(
|
|
2967
|
+
value: responsePrompts.commandResult(command2, exitCode, stdout, stderr)
|
|
2976
2968
|
}
|
|
2977
2969
|
};
|
|
2978
2970
|
}
|
|
2979
2971
|
} catch (error) {
|
|
2980
|
-
console.warn(`Failed to run ${scriptName} using command: ${
|
|
2972
|
+
console.warn(`Failed to run ${scriptName} using command: ${command2}`, error);
|
|
2981
2973
|
}
|
|
2982
2974
|
}
|
|
2983
2975
|
}
|
|
@@ -3018,24 +3010,24 @@ var MultiAgent = class {
|
|
|
3018
3010
|
switch (exitReason.type) {
|
|
3019
3011
|
case "HandOver" /* HandOver */: {
|
|
3020
3012
|
this.#agents.pop();
|
|
3021
|
-
const
|
|
3013
|
+
const prompt5 = await this.#config.getPrompt?.(
|
|
3022
3014
|
exitReason.agentName,
|
|
3023
3015
|
exitReason.task,
|
|
3024
3016
|
exitReason.context,
|
|
3025
3017
|
exitReason.files,
|
|
3026
3018
|
this.#originalTask
|
|
3027
3019
|
) ?? exitReason.task;
|
|
3028
|
-
return await this.#startTask(exitReason.agentName,
|
|
3020
|
+
return await this.#startTask(exitReason.agentName, prompt5);
|
|
3029
3021
|
}
|
|
3030
3022
|
case "Delegate" /* Delegate */: {
|
|
3031
|
-
const
|
|
3023
|
+
const prompt5 = await this.#config.getPrompt?.(
|
|
3032
3024
|
exitReason.agentName,
|
|
3033
3025
|
exitReason.task,
|
|
3034
3026
|
exitReason.context,
|
|
3035
3027
|
exitReason.files,
|
|
3036
3028
|
this.#originalTask
|
|
3037
3029
|
) ?? exitReason.task;
|
|
3038
|
-
const delegateResult = await this.#startTask(exitReason.agentName,
|
|
3030
|
+
const delegateResult = await this.#startTask(exitReason.agentName, prompt5);
|
|
3039
3031
|
switch (delegateResult.type) {
|
|
3040
3032
|
case "HandOver" /* HandOver */:
|
|
3041
3033
|
case "Delegate" /* Delegate */:
|
|
@@ -3094,6 +3086,28 @@ var MultiAgent = class {
|
|
|
3094
3086
|
}
|
|
3095
3087
|
};
|
|
3096
3088
|
|
|
3089
|
+
// src/Agent/parseJsonFromMarkdown.ts
|
|
3090
|
+
var parseJsonFromMarkdown = (markdown) => {
|
|
3091
|
+
const jsonRegex = /```(?:json)?\n([\s\S]*?)\n```/;
|
|
3092
|
+
const match = markdown.match(jsonRegex);
|
|
3093
|
+
if (match?.[1]) {
|
|
3094
|
+
const content = match[1].trim();
|
|
3095
|
+
let parsed = JSON.parse(content);
|
|
3096
|
+
if (typeof parsed === "string") {
|
|
3097
|
+
try {
|
|
3098
|
+
parsed = JSON.parse(parsed);
|
|
3099
|
+
} catch {
|
|
3100
|
+
}
|
|
3101
|
+
}
|
|
3102
|
+
return parsed;
|
|
3103
|
+
}
|
|
3104
|
+
try {
|
|
3105
|
+
return JSON.parse(markdown);
|
|
3106
|
+
} catch (_error) {
|
|
3107
|
+
}
|
|
3108
|
+
return markdown;
|
|
3109
|
+
};
|
|
3110
|
+
|
|
3097
3111
|
// src/config.ts
|
|
3098
3112
|
import { z as z16 } from "zod";
|
|
3099
3113
|
var toolFormatSchema = z16.enum(["native", "polka-codes"]).optional();
|
|
@@ -3659,304 +3673,6 @@ var generateProjectConfig_default = {
|
|
|
3659
3673
|
agent: "analyzer"
|
|
3660
3674
|
};
|
|
3661
3675
|
|
|
3662
|
-
// src/AiTool/tools/gitDiff.ts
|
|
3663
|
-
import { z as z17 } from "zod";
|
|
3664
|
-
|
|
3665
|
-
// src/AiTool/tools/utils/diffLineNumbers.ts
|
|
3666
|
-
function parseHunkHeader(header) {
|
|
3667
|
-
const match = header.match(/^@@ -(\d+)(?:,(\d+))? \+(\d+)(?:,(\d+))? @@/);
|
|
3668
|
-
if (!match) return null;
|
|
3669
|
-
return {
|
|
3670
|
-
oldStart: parseInt(match[1], 10),
|
|
3671
|
-
oldCount: match[2] ? parseInt(match[2], 10) : 1,
|
|
3672
|
-
newStart: parseInt(match[3], 10),
|
|
3673
|
-
newCount: match[4] ? parseInt(match[4], 10) : 1,
|
|
3674
|
-
header
|
|
3675
|
-
};
|
|
3676
|
-
}
|
|
3677
|
-
function annotateDiffWithLineNumbers(diff) {
|
|
3678
|
-
const lines = diff.split("\n");
|
|
3679
|
-
const annotatedLines = [];
|
|
3680
|
-
let currentNewLine = 0;
|
|
3681
|
-
let currentOldLine = 0;
|
|
3682
|
-
let inHunk = false;
|
|
3683
|
-
for (const line of lines) {
|
|
3684
|
-
if (line.startsWith("@@")) {
|
|
3685
|
-
const hunk = parseHunkHeader(line);
|
|
3686
|
-
if (hunk) {
|
|
3687
|
-
currentOldLine = hunk.oldStart;
|
|
3688
|
-
currentNewLine = hunk.newStart;
|
|
3689
|
-
inHunk = true;
|
|
3690
|
-
}
|
|
3691
|
-
annotatedLines.push(line);
|
|
3692
|
-
continue;
|
|
3693
|
-
}
|
|
3694
|
-
if (line.startsWith("diff --git") || line.startsWith("index ") || line.startsWith("---") || line.startsWith("+++")) {
|
|
3695
|
-
annotatedLines.push(line);
|
|
3696
|
-
inHunk = false;
|
|
3697
|
-
continue;
|
|
3698
|
-
}
|
|
3699
|
-
if (!inHunk) {
|
|
3700
|
-
annotatedLines.push(line);
|
|
3701
|
-
continue;
|
|
3702
|
-
}
|
|
3703
|
-
if (line.startsWith("+") && !line.startsWith("+++")) {
|
|
3704
|
-
annotatedLines.push(`${line} [Line ${currentNewLine}]`);
|
|
3705
|
-
currentNewLine++;
|
|
3706
|
-
} else if (line.startsWith("-") && !line.startsWith("---")) {
|
|
3707
|
-
annotatedLines.push(`${line} [Line ${currentOldLine} removed]`);
|
|
3708
|
-
currentOldLine++;
|
|
3709
|
-
} else if (line.startsWith(" ")) {
|
|
3710
|
-
annotatedLines.push(line);
|
|
3711
|
-
currentOldLine++;
|
|
3712
|
-
currentNewLine++;
|
|
3713
|
-
} else {
|
|
3714
|
-
annotatedLines.push(line);
|
|
3715
|
-
}
|
|
3716
|
-
}
|
|
3717
|
-
return annotatedLines.join("\n");
|
|
3718
|
-
}
|
|
3719
|
-
|
|
3720
|
-
// src/AiTool/tools/gitDiff.ts
|
|
3721
|
-
var toolInfo15 = {
|
|
3722
|
-
name: "git_diff",
|
|
3723
|
-
description: "Get the git diff for the current repository. Can be used to get staged changes, unstaged changes, or changes between commits. By default, it returns unstaged changes.",
|
|
3724
|
-
parameters: z17.object({
|
|
3725
|
-
staged: z17.preprocess((val) => {
|
|
3726
|
-
if (typeof val === "string") {
|
|
3727
|
-
const lower = val.toLowerCase();
|
|
3728
|
-
if (lower === "false") return false;
|
|
3729
|
-
if (lower === "true") return true;
|
|
3730
|
-
}
|
|
3731
|
-
return val;
|
|
3732
|
-
}, z17.boolean().optional().default(false)).describe("Get staged changes instead of unstaged changes."),
|
|
3733
|
-
commitRange: z17.string().optional().describe('The commit range to get the diff for (e.g., "main...HEAD").'),
|
|
3734
|
-
file: z17.string().describe("Get the diff for a specific file."),
|
|
3735
|
-
contextLines: z17.coerce.number().optional().default(5).describe("Number of context lines to include around changes."),
|
|
3736
|
-
includeLineNumbers: z17.preprocess((val) => {
|
|
3737
|
-
if (typeof val === "string") {
|
|
3738
|
-
const lower = val.toLowerCase();
|
|
3739
|
-
if (lower === "false") return false;
|
|
3740
|
-
if (lower === "true") return true;
|
|
3741
|
-
}
|
|
3742
|
-
return val;
|
|
3743
|
-
}, z17.boolean().optional().default(false)).describe("Annotate the diff with line numbers for additions and deletions.")
|
|
3744
|
-
})
|
|
3745
|
-
};
|
|
3746
|
-
var handler15 = async (provider, args) => {
|
|
3747
|
-
if (!provider.executeCommand) {
|
|
3748
|
-
return {
|
|
3749
|
-
type: "Error" /* Error */,
|
|
3750
|
-
message: {
|
|
3751
|
-
type: "error-text",
|
|
3752
|
-
value: "Not possible to execute command. Abort."
|
|
3753
|
-
}
|
|
3754
|
-
};
|
|
3755
|
-
}
|
|
3756
|
-
const { staged, file, commitRange, contextLines, includeLineNumbers } = toolInfo15.parameters.parse(args);
|
|
3757
|
-
const commandParts = ["git", "diff", "--no-color", `-U${contextLines}`];
|
|
3758
|
-
if (staged) {
|
|
3759
|
-
commandParts.push("--staged");
|
|
3760
|
-
}
|
|
3761
|
-
if (commitRange) {
|
|
3762
|
-
commandParts.push(commitRange);
|
|
3763
|
-
}
|
|
3764
|
-
if (file) {
|
|
3765
|
-
commandParts.push("--", `'${file}'`);
|
|
3766
|
-
}
|
|
3767
|
-
const command = commandParts.join(" ");
|
|
3768
|
-
try {
|
|
3769
|
-
const result = await provider.executeCommand(command, false);
|
|
3770
|
-
if (result.exitCode === 0) {
|
|
3771
|
-
if (!result.stdout.trim()) {
|
|
3772
|
-
return {
|
|
3773
|
-
type: "Reply" /* Reply */,
|
|
3774
|
-
message: {
|
|
3775
|
-
type: "text",
|
|
3776
|
-
value: "No diff found."
|
|
3777
|
-
}
|
|
3778
|
-
};
|
|
3779
|
-
}
|
|
3780
|
-
let diffOutput = result.stdout;
|
|
3781
|
-
if (includeLineNumbers) {
|
|
3782
|
-
diffOutput = annotateDiffWithLineNumbers(diffOutput);
|
|
3783
|
-
}
|
|
3784
|
-
return {
|
|
3785
|
-
type: "Reply" /* Reply */,
|
|
3786
|
-
message: {
|
|
3787
|
-
type: "text",
|
|
3788
|
-
value: `<diff file="${file ?? "all"}">
|
|
3789
|
-
${diffOutput}
|
|
3790
|
-
</diff>`
|
|
3791
|
-
}
|
|
3792
|
-
};
|
|
3793
|
-
}
|
|
3794
|
-
return {
|
|
3795
|
-
type: "Error" /* Error */,
|
|
3796
|
-
message: {
|
|
3797
|
-
type: "error-text",
|
|
3798
|
-
value: `\`${command}\` exited with code ${result.exitCode}:
|
|
3799
|
-
${result.stderr}`
|
|
3800
|
-
}
|
|
3801
|
-
};
|
|
3802
|
-
} catch (error) {
|
|
3803
|
-
return {
|
|
3804
|
-
type: "Error" /* Error */,
|
|
3805
|
-
message: {
|
|
3806
|
-
type: "error-text",
|
|
3807
|
-
value: error instanceof Error ? error.message : String(error)
|
|
3808
|
-
}
|
|
3809
|
-
};
|
|
3810
|
-
}
|
|
3811
|
-
};
|
|
3812
|
-
var isAvailable15 = (provider) => {
|
|
3813
|
-
return !!provider.executeCommand;
|
|
3814
|
-
};
|
|
3815
|
-
var gitDiff_default = {
|
|
3816
|
-
...toolInfo15,
|
|
3817
|
-
handler: handler15,
|
|
3818
|
-
isAvailable: isAvailable15
|
|
3819
|
-
};
|
|
3820
|
-
|
|
3821
|
-
// src/AiTool/reviewDiff.ts
|
|
3822
|
-
var prompt5 = `
|
|
3823
|
-
# Code Review Prompt
|
|
3824
|
-
|
|
3825
|
-
You are a senior software engineer reviewing code changes.
|
|
3826
|
-
|
|
3827
|
-
## Critical Instructions
|
|
3828
|
-
- **ONLY review the actual changes shown in the diff.** Do not comment on existing code that wasn't modified.
|
|
3829
|
-
- **ONLY run git_diff on files that are reviewable source/config files** per the "File Selection for git_diff" rules below. Do not pass excluded files to git_diff.
|
|
3830
|
-
|
|
3831
|
-
## File Selection for git_diff
|
|
3832
|
-
Use <file_status> to decide which files to diff. Include only files likely to contain human-authored source or meaningful configuration.
|
|
3833
|
-
|
|
3834
|
-
Include (run git_diff):
|
|
3835
|
-
- Application/source code
|
|
3836
|
-
- UI/templates/assets code
|
|
3837
|
-
- Infra/config that affects behavior
|
|
3838
|
-
|
|
3839
|
-
Exclude (do NOT run git_diff; do not review):
|
|
3840
|
-
- Lockfiles
|
|
3841
|
-
- Generated/build artifacts & deps
|
|
3842
|
-
- Test artifacts/snapshots
|
|
3843
|
-
- Data and fixtures
|
|
3844
|
-
- Binary/media/minified/maps
|
|
3845
|
-
|
|
3846
|
-
## Viewing Changes
|
|
3847
|
-
- For each included file, **use git_diff** to inspect the actual code changes:
|
|
3848
|
-
- **Pull request:** use the provided commit range for the git_diff tool with contextLines: 5 and includeLineNumbers: true, but only surface and review the included files.
|
|
3849
|
-
- **Local changes:** diff staged or unstaged included files using git_diff with contextLines: 5 and includeLineNumbers: true.
|
|
3850
|
-
- The diff will include line number annotations: [Line N] for additions and [Line N removed] for deletions.
|
|
3851
|
-
- You may receive:
|
|
3852
|
-
- <pr_title>
|
|
3853
|
-
- <pr_description>
|
|
3854
|
-
- <commit_messages>
|
|
3855
|
-
- A <review_instructions> tag tells you the focus of the review.
|
|
3856
|
-
- Use <file_status> to understand which files were modified, added, deleted, or renamed and to apply the inclusion/exclusion rules above.
|
|
3857
|
-
|
|
3858
|
-
## Line Number Reporting
|
|
3859
|
-
- Use the line numbers from the annotations in the diff output.
|
|
3860
|
-
- For additions: use the number from the [Line N] annotation after the + line.
|
|
3861
|
-
- For deletions: use the number from the [Line N removed] annotation after the - line.
|
|
3862
|
-
- For modifications: report the line number of the new/current code (from [Line N]).
|
|
3863
|
-
- Report single lines as "N" and ranges as "N-M".
|
|
3864
|
-
|
|
3865
|
-
## Review Guidelines
|
|
3866
|
-
Focus exclusively on the changed lines (+ additions, - deletions, modified lines):
|
|
3867
|
-
- **Specific issues:** Point to exact problems in the changed code with accurate line references from the annotations.
|
|
3868
|
-
- **Actionable fixes:** Provide concrete solutions, not vague suggestions.
|
|
3869
|
-
- **Clear reasoning:** Explain why each issue matters and how to fix it.
|
|
3870
|
-
- **Avoid generic advice** unless directly tied to a specific problem visible in the diff.
|
|
3871
|
-
|
|
3872
|
-
## What NOT to review
|
|
3873
|
-
- Files excluded by the "File Selection for git_diff" rules (do not diff or comment on them).
|
|
3874
|
-
- Existing unchanged code.
|
|
3875
|
-
- Overall project structure/architecture unless directly impacted by the changes.
|
|
3876
|
-
- Missing features or functionality not part of this diff.
|
|
3877
|
-
|
|
3878
|
-
## Output Format
|
|
3879
|
-
Do not include praise or positive feedback.
|
|
3880
|
-
Only include reviews for actual issues found in the changed code.
|
|
3881
|
-
|
|
3882
|
-
Return your review as a JSON object inside a \`\`\`json block, wrapped like:
|
|
3883
|
-
\`\`\`json
|
|
3884
|
-
{
|
|
3885
|
-
"overview": "Summary of specific issues found in the diff changes, 'No issues found', or 'No reviewable changes' if all modified files were excluded.",
|
|
3886
|
-
"specificReviews": [
|
|
3887
|
-
{
|
|
3888
|
-
"file": "path/filename.ext",
|
|
3889
|
-
"lines": "N or N-M",
|
|
3890
|
-
"review": "Specific issue with the changed code and exact actionable fix."
|
|
3891
|
-
}
|
|
3892
|
-
]
|
|
3893
|
-
}
|
|
3894
|
-
\`\`\`
|
|
3895
|
-
`;
|
|
3896
|
-
var reviewDiff_default = {
|
|
3897
|
-
name: "reviewDiff",
|
|
3898
|
-
description: "Reviews a git diff",
|
|
3899
|
-
prompt: prompt5,
|
|
3900
|
-
formatInput: (params) => {
|
|
3901
|
-
const parts = [];
|
|
3902
|
-
if (params.pullRequestTitle) {
|
|
3903
|
-
parts.push(`<pr_title>
|
|
3904
|
-
${params.pullRequestTitle}
|
|
3905
|
-
</pr_title>`);
|
|
3906
|
-
}
|
|
3907
|
-
if (params.pullRequestDescription) {
|
|
3908
|
-
parts.push(`<pr_description>
|
|
3909
|
-
${params.pullRequestDescription}
|
|
3910
|
-
</pr_description>`);
|
|
3911
|
-
}
|
|
3912
|
-
if (params.commitMessages) {
|
|
3913
|
-
parts.push(`<commit_messages>
|
|
3914
|
-
${params.commitMessages}
|
|
3915
|
-
</commit_messages>`);
|
|
3916
|
-
}
|
|
3917
|
-
if (params.changedFiles && params.changedFiles.length > 0) {
|
|
3918
|
-
const fileList = params.changedFiles.map((file) => `${file.status}: ${file.path}`).join("\n");
|
|
3919
|
-
parts.push(`<file_status>
|
|
3920
|
-
${fileList}
|
|
3921
|
-
</file_status>`);
|
|
3922
|
-
}
|
|
3923
|
-
let instructions = "";
|
|
3924
|
-
if (params.commitRange) {
|
|
3925
|
-
instructions = `Review the pull request. Use the git_diff tool with commit range '${params.commitRange}', contextLines: 5, and includeLineNumbers: true to inspect the actual code changes. The diff will include line number annotations to help you report accurate line numbers. File status information is already provided above.`;
|
|
3926
|
-
} else if (params.staged) {
|
|
3927
|
-
instructions = "Review the staged changes. Use the git_diff tool with staged: true, contextLines: 5, and includeLineNumbers: true to inspect the actual code changes. The diff will include line number annotations to help you report accurate line numbers. File status information is already provided above.";
|
|
3928
|
-
} else {
|
|
3929
|
-
instructions = "Review the unstaged changes. Use the git_diff tool with contextLines: 5 and includeLineNumbers: true to inspect the actual code changes. The diff will include line number annotations to help you report accurate line numbers. File status information is already provided above.";
|
|
3930
|
-
}
|
|
3931
|
-
parts.push(`<review_instructions>
|
|
3932
|
-
${instructions}
|
|
3933
|
-
</review_instructions>`);
|
|
3934
|
-
return parts.join("\n");
|
|
3935
|
-
},
|
|
3936
|
-
parseOutput: (output) => {
|
|
3937
|
-
const jsonBlockRegex = /```json\n([\s\S]*?)\n```/;
|
|
3938
|
-
const match = output.match(jsonBlockRegex);
|
|
3939
|
-
const content = match ? match[1] : output;
|
|
3940
|
-
try {
|
|
3941
|
-
return JSON.parse(content);
|
|
3942
|
-
} catch (error) {
|
|
3943
|
-
console.error("Error parsing JSON output:", error);
|
|
3944
|
-
return {
|
|
3945
|
-
overview: `Could not parse review output. Raw output:
|
|
3946
|
-
${output}`,
|
|
3947
|
-
specificReviews: []
|
|
3948
|
-
};
|
|
3949
|
-
}
|
|
3950
|
-
},
|
|
3951
|
-
agent: (options) => {
|
|
3952
|
-
return new AnalyzerAgent({
|
|
3953
|
-
...options,
|
|
3954
|
-
additionalTools: [gitDiff_default],
|
|
3955
|
-
requireToolUse: false
|
|
3956
|
-
});
|
|
3957
|
-
}
|
|
3958
|
-
};
|
|
3959
|
-
|
|
3960
3676
|
// src/AiTool/index.ts
|
|
3961
3677
|
var executeTool = async (definition, ai, params, usageMeter) => {
|
|
3962
3678
|
const resp = await generateText({
|
|
@@ -4011,7 +3727,6 @@ var makeAgentTool = (definition) => {
|
|
|
4011
3727
|
};
|
|
4012
3728
|
var generateGitCommitMessage = makeTool(generateGitCommitMessage_default);
|
|
4013
3729
|
var generateGithubPullRequestDetails = makeTool(generateGithubPullRequestDetails_default);
|
|
4014
|
-
var reviewDiff = makeAgentTool(reviewDiff_default);
|
|
4015
3730
|
var generateProjectConfig = makeMultiAgentTool(generateProjectConfig_default);
|
|
4016
3731
|
var createNewProject = makeMultiAgentTool(createNewProject_default);
|
|
4017
3732
|
|
|
@@ -4041,50 +3756,62 @@ var WorkflowAgent = class extends AgentBase {
|
|
|
4041
3756
|
};
|
|
4042
3757
|
var makeAgentStepSpecHandler = (getModelFn) => ({
|
|
4043
3758
|
type: "agent",
|
|
4044
|
-
handler(
|
|
3759
|
+
handler(step2) {
|
|
4045
3760
|
return {
|
|
4046
|
-
...
|
|
3761
|
+
...step2,
|
|
4047
3762
|
async run(input, context, resumedState) {
|
|
3763
|
+
const logger = context.logger ?? console;
|
|
3764
|
+
if (context.verbose && context.verbose >= 1) {
|
|
3765
|
+
logger.log(`[agent-step] Running agent step '${step2.id}' with input:`, input);
|
|
3766
|
+
}
|
|
4048
3767
|
try {
|
|
4049
|
-
const model = await getModelFn(
|
|
3768
|
+
const model = await getModelFn(step2, context);
|
|
4050
3769
|
const parameters = context.parameters ?? {};
|
|
4051
|
-
const budget =
|
|
4052
|
-
const maxMessages =
|
|
3770
|
+
const budget = step2.budget ?? parameters?.budget;
|
|
3771
|
+
const maxMessages = step2.maxMessages ?? parameters?.maxMessages;
|
|
4053
3772
|
const usageMeter = new UsageMeter({}, { maxMessages, maxCost: budget });
|
|
4054
|
-
const toolFormat =
|
|
3773
|
+
const toolFormat = step2.toolFormat ?? parameters.toolFormat ?? "native";
|
|
4055
3774
|
const policies = parameters.policies ?? [];
|
|
4056
|
-
const modelParameters = { ...parameters.modelParameters ?? {}, ...
|
|
3775
|
+
const modelParameters = { ...parameters.modelParameters ?? {}, ...step2.modelParameters ?? {} };
|
|
4057
3776
|
if (resumedState?.usage) {
|
|
4058
3777
|
usageMeter.setUsage(resumedState.usage);
|
|
4059
3778
|
}
|
|
4060
3779
|
const getAgent = () => {
|
|
4061
|
-
if (
|
|
4062
|
-
const agentName =
|
|
3780
|
+
if (step2.agent) {
|
|
3781
|
+
const agentName = step2.agent;
|
|
4063
3782
|
const AgentClass = agentRegistry[agentName];
|
|
4064
3783
|
if (!AgentClass) {
|
|
4065
3784
|
throw new Error(`Unknown agent: ${agentName}`);
|
|
4066
3785
|
}
|
|
3786
|
+
if (context.verbose && context.verbose >= 1) {
|
|
3787
|
+
logger.log(`[agent-step] Using agent: ${agentName}`);
|
|
3788
|
+
}
|
|
4067
3789
|
const agentOptions = {
|
|
4068
3790
|
ai: model,
|
|
4069
3791
|
os: parameters.os ?? "linux",
|
|
4070
3792
|
provider: context.provider,
|
|
4071
|
-
interactive: false,
|
|
4072
3793
|
toolFormat,
|
|
4073
|
-
additionalTools:
|
|
3794
|
+
additionalTools: step2.tools,
|
|
4074
3795
|
policies,
|
|
4075
3796
|
usageMeter,
|
|
4076
3797
|
parameters: modelParameters,
|
|
4077
|
-
scripts: parameters.scripts
|
|
3798
|
+
scripts: parameters.scripts,
|
|
3799
|
+
callback: context.agentCallback
|
|
4078
3800
|
};
|
|
4079
3801
|
return new AgentClass(agentOptions);
|
|
4080
3802
|
} else {
|
|
4081
|
-
|
|
3803
|
+
if (!step2.systemPrompt) {
|
|
3804
|
+
throw new Error("No system prompt specified for the agent step.");
|
|
3805
|
+
}
|
|
3806
|
+
if (context.verbose && context.verbose >= 1) {
|
|
3807
|
+
logger.log(`[agent-step] Using generic WorkflowAgent`);
|
|
3808
|
+
}
|
|
3809
|
+
const systemPrompt = resolveTemplatedString(step2.systemPrompt, input);
|
|
4082
3810
|
return new WorkflowAgent("agent", model, {
|
|
4083
3811
|
systemPrompt,
|
|
4084
|
-
tools:
|
|
3812
|
+
tools: step2.tools ?? [],
|
|
4085
3813
|
toolNamePrefix: toolFormat === "native" ? "" : "tool_",
|
|
4086
3814
|
provider: context.provider,
|
|
4087
|
-
interactive: false,
|
|
4088
3815
|
agents: [],
|
|
4089
3816
|
scripts: parameters.scripts,
|
|
4090
3817
|
callback: void 0,
|
|
@@ -4100,7 +3827,7 @@ var makeAgentStepSpecHandler = (getModelFn) => ({
|
|
|
4100
3827
|
if (resumedState?.messages) {
|
|
4101
3828
|
agent.setMessages(resumedState.messages);
|
|
4102
3829
|
}
|
|
4103
|
-
const userContentArray =
|
|
3830
|
+
const userContentArray = step2.messages.map((message) => {
|
|
4104
3831
|
if (typeof message === "string" || Array.isArray(message)) {
|
|
4105
3832
|
return message;
|
|
4106
3833
|
}
|
|
@@ -4114,14 +3841,59 @@ var makeAgentStepSpecHandler = (getModelFn) => ({
|
|
|
4114
3841
|
combinedContentParts.push(...content);
|
|
4115
3842
|
}
|
|
4116
3843
|
}
|
|
3844
|
+
if (context.verbose && context.verbose >= 1) {
|
|
3845
|
+
logger.log(`[agent-step] Starting agent with content:`, JSON.stringify(combinedContentParts, null, 2));
|
|
3846
|
+
}
|
|
4117
3847
|
const exitReason = await agent.start(combinedContentParts);
|
|
3848
|
+
if (context.verbose && context.verbose >= 1) {
|
|
3849
|
+
logger.log(`[agent-step] Agent exited with reason:`, exitReason);
|
|
3850
|
+
}
|
|
4118
3851
|
const handleExitReason = (reason) => {
|
|
4119
3852
|
switch (reason.type) {
|
|
4120
3853
|
case "Pause":
|
|
4121
3854
|
return { type: "paused", state: { messages: agent.messages } };
|
|
4122
3855
|
case "UsageExceeded":
|
|
4123
3856
|
return { type: "error", error: new Error("Usage limit exceeded") };
|
|
4124
|
-
case "Exit" /* Exit */:
|
|
3857
|
+
case "Exit" /* Exit */: {
|
|
3858
|
+
const raw = reason.message;
|
|
3859
|
+
if (step2.parseOutput) {
|
|
3860
|
+
try {
|
|
3861
|
+
const parsed = step2.parseOutput(raw);
|
|
3862
|
+
if (step2.outputSchema) {
|
|
3863
|
+
const validationResult = step2.outputSchema.safeParse(parsed);
|
|
3864
|
+
if (validationResult.success) {
|
|
3865
|
+
return { type: "success", output: validationResult.data };
|
|
3866
|
+
}
|
|
3867
|
+
return {
|
|
3868
|
+
type: "error",
|
|
3869
|
+
error: new Error(`Output validation failed: ${validationResult.error.toString()}`)
|
|
3870
|
+
};
|
|
3871
|
+
}
|
|
3872
|
+
return { type: "success", output: parsed };
|
|
3873
|
+
} catch (e) {
|
|
3874
|
+
const error = e instanceof Error ? e : new Error(String(e));
|
|
3875
|
+
error.message = `Failed to parse agent output with parseOutput: ${error.message}`;
|
|
3876
|
+
return { type: "error", error };
|
|
3877
|
+
}
|
|
3878
|
+
} else if (step2.outputSchema) {
|
|
3879
|
+
try {
|
|
3880
|
+
const output = JSON.parse(raw);
|
|
3881
|
+
const validationResult = step2.outputSchema.safeParse(output);
|
|
3882
|
+
if (validationResult.success) {
|
|
3883
|
+
return { type: "success", output: validationResult.data };
|
|
3884
|
+
}
|
|
3885
|
+
return {
|
|
3886
|
+
type: "error",
|
|
3887
|
+
error: new Error(`Output validation failed: ${validationResult.error.toString()}`)
|
|
3888
|
+
};
|
|
3889
|
+
} catch (e) {
|
|
3890
|
+
const error = e instanceof Error ? e : new Error(String(e));
|
|
3891
|
+
error.message = `Failed to parse agent output as JSON: ${error.message}`;
|
|
3892
|
+
return { type: "error", error };
|
|
3893
|
+
}
|
|
3894
|
+
}
|
|
3895
|
+
return { type: "success", output: reason };
|
|
3896
|
+
}
|
|
4125
3897
|
case "HandOver" /* HandOver */:
|
|
4126
3898
|
case "Delegate" /* Delegate */:
|
|
4127
3899
|
return { type: "success", output: reason };
|
|
@@ -4160,23 +3932,29 @@ var StepsBuilder = class {
|
|
|
4160
3932
|
} : this.#steps[0];
|
|
4161
3933
|
return rootStep;
|
|
4162
3934
|
}
|
|
4163
|
-
step(
|
|
4164
|
-
this.#steps.push(
|
|
3935
|
+
step(step2) {
|
|
3936
|
+
this.#steps.push(step2);
|
|
4165
3937
|
return this;
|
|
4166
3938
|
}
|
|
4167
|
-
parallel(id,
|
|
3939
|
+
parallel(id, step2) {
|
|
4168
3940
|
return this.step({
|
|
4169
3941
|
id,
|
|
4170
3942
|
type: "parallel",
|
|
4171
|
-
step
|
|
3943
|
+
step: step2
|
|
4172
3944
|
});
|
|
4173
3945
|
}
|
|
4174
|
-
custom(
|
|
4175
|
-
|
|
4176
|
-
|
|
4177
|
-
|
|
4178
|
-
|
|
4179
|
-
|
|
3946
|
+
custom(idOrSpec, run2) {
|
|
3947
|
+
if (typeof idOrSpec === "string") {
|
|
3948
|
+
if (!run2) {
|
|
3949
|
+
throw new Error('Custom step "run" function must be provided.');
|
|
3950
|
+
}
|
|
3951
|
+
return this.step({
|
|
3952
|
+
id: idOrSpec,
|
|
3953
|
+
type: "custom",
|
|
3954
|
+
run: run2
|
|
3955
|
+
});
|
|
3956
|
+
}
|
|
3957
|
+
return this.step(idOrSpec);
|
|
4180
3958
|
}
|
|
4181
3959
|
agent(id, spec) {
|
|
4182
3960
|
return this.step({
|
|
@@ -4193,16 +3971,16 @@ var builder = () => {
|
|
|
4193
3971
|
// src/workflow/command.ts
|
|
4194
3972
|
var commandStepSpecHandler = {
|
|
4195
3973
|
type: "command",
|
|
4196
|
-
handler(
|
|
3974
|
+
handler(step2) {
|
|
4197
3975
|
return {
|
|
4198
|
-
...
|
|
3976
|
+
...step2,
|
|
4199
3977
|
async run(input, context) {
|
|
4200
3978
|
try {
|
|
4201
3979
|
if (!context.provider.executeCommand) {
|
|
4202
3980
|
throw new Error("executeCommand is not available in the provider");
|
|
4203
3981
|
}
|
|
4204
|
-
const
|
|
4205
|
-
const commandString =
|
|
3982
|
+
const command2 = step2.command.map((arg) => resolveTemplatedString(arg, input));
|
|
3983
|
+
const commandString = command2.map((arg) => {
|
|
4206
3984
|
if (/[^A-Za-z0-9_/:=-]/.test(arg)) {
|
|
4207
3985
|
return `"${arg.replace(/\\/g, "\\\\").replace(/"/g, '\\"')}"`;
|
|
4208
3986
|
}
|
|
@@ -4210,13 +3988,13 @@ var commandStepSpecHandler = {
|
|
|
4210
3988
|
}).join(" ");
|
|
4211
3989
|
const result = await context.provider.executeCommand(commandString, false);
|
|
4212
3990
|
const output = {};
|
|
4213
|
-
if (
|
|
3991
|
+
if (step2.outputs?.includes("stdout")) {
|
|
4214
3992
|
output.stdout = result.stdout;
|
|
4215
3993
|
}
|
|
4216
|
-
if (
|
|
3994
|
+
if (step2.outputs?.includes("stderr")) {
|
|
4217
3995
|
output.stderr = result.stderr;
|
|
4218
3996
|
}
|
|
4219
|
-
if (
|
|
3997
|
+
if (step2.outputs?.includes("exitCode")) {
|
|
4220
3998
|
output.exitCode = result.exitCode;
|
|
4221
3999
|
}
|
|
4222
4000
|
return { type: "success", output };
|
|
@@ -4227,14 +4005,27 @@ var commandStepSpecHandler = {
|
|
|
4227
4005
|
};
|
|
4228
4006
|
}
|
|
4229
4007
|
};
|
|
4008
|
+
var command = (id, ...command2) => ({
|
|
4009
|
+
id,
|
|
4010
|
+
type: "command",
|
|
4011
|
+
command: command2
|
|
4012
|
+
});
|
|
4230
4013
|
|
|
4231
4014
|
// src/workflow/runStep.ts
|
|
4232
|
-
var runStep = async (
|
|
4015
|
+
var runStep = async (step2, input, context, resumedState, allOutputs) => {
|
|
4016
|
+
const logger = context.logger ?? console;
|
|
4017
|
+
if (context.verbose && context.verbose >= 1) {
|
|
4018
|
+
logger.log(`[workflow] running step: ${step2.id}`);
|
|
4019
|
+
logger.log(`[workflow] input: ${JSON.stringify(input, null, 2)}`);
|
|
4020
|
+
}
|
|
4233
4021
|
try {
|
|
4234
|
-
const validatedInput =
|
|
4235
|
-
const result = await
|
|
4022
|
+
const validatedInput = step2.inputSchema?.parse(input) ?? input;
|
|
4023
|
+
const result = await step2.run({ ...validatedInput, $: allOutputs }, context, resumedState);
|
|
4024
|
+
if (context.verbose && context.verbose >= 1) {
|
|
4025
|
+
logger.log(`[workflow] step result: ${step2.id}`, JSON.stringify(result, null, 2));
|
|
4026
|
+
}
|
|
4236
4027
|
if (result.type === "success") {
|
|
4237
|
-
const validatedOutput =
|
|
4028
|
+
const validatedOutput = step2.outputSchema?.parse(result.output) ?? result.output;
|
|
4238
4029
|
return {
|
|
4239
4030
|
...result,
|
|
4240
4031
|
output: validatedOutput
|
|
@@ -4242,6 +4033,9 @@ var runStep = async (step, input, context, resumedState, allOutputs) => {
|
|
|
4242
4033
|
}
|
|
4243
4034
|
return result;
|
|
4244
4035
|
} catch (error) {
|
|
4036
|
+
if (context.verbose && context.verbose >= 1) {
|
|
4037
|
+
logger.error(`[workflow] step error: ${step2.id}`, error);
|
|
4038
|
+
}
|
|
4245
4039
|
return { type: "error", error };
|
|
4246
4040
|
}
|
|
4247
4041
|
};
|
|
@@ -4249,26 +4043,26 @@ var runStep = async (step, input, context, resumedState, allOutputs) => {
|
|
|
4249
4043
|
// src/workflow/steps.ts
|
|
4250
4044
|
var combineHandlers = (...handlers) => {
|
|
4251
4045
|
const allHandlers = {};
|
|
4252
|
-
for (const
|
|
4253
|
-
if (allHandlers[
|
|
4254
|
-
throw new Error(`Handler with type ${
|
|
4046
|
+
for (const handler15 of handlers) {
|
|
4047
|
+
if (allHandlers[handler15.type]) {
|
|
4048
|
+
throw new Error(`Handler with type ${handler15.type} already exists`);
|
|
4255
4049
|
}
|
|
4256
|
-
allHandlers[
|
|
4050
|
+
allHandlers[handler15.type] = handler15;
|
|
4257
4051
|
}
|
|
4258
|
-
return (
|
|
4259
|
-
const
|
|
4260
|
-
if (!
|
|
4261
|
-
throw new Error(`No handler found for step type: ${
|
|
4052
|
+
return (step2, rootHandler) => {
|
|
4053
|
+
const handler15 = allHandlers[step2.type];
|
|
4054
|
+
if (!handler15) {
|
|
4055
|
+
throw new Error(`No handler found for step type: ${step2.type}`);
|
|
4262
4056
|
}
|
|
4263
|
-
return
|
|
4057
|
+
return handler15.handler(step2, rootHandler);
|
|
4264
4058
|
};
|
|
4265
4059
|
};
|
|
4266
4060
|
var sequentialStepSpecHandler = {
|
|
4267
4061
|
type: "sequential",
|
|
4268
|
-
handler(
|
|
4269
|
-
const steps =
|
|
4062
|
+
handler(step2, rootHandler) {
|
|
4063
|
+
const steps = step2.steps.map((subStep) => rootHandler(subStep, rootHandler));
|
|
4270
4064
|
return {
|
|
4271
|
-
...
|
|
4065
|
+
...step2,
|
|
4272
4066
|
async run(input, context, resumedState) {
|
|
4273
4067
|
let currentInput = input;
|
|
4274
4068
|
const allOutputs = resumedState ? resumedState.allOutputs : {};
|
|
@@ -4301,10 +4095,10 @@ var sequentialStepSpecHandler = {
|
|
|
4301
4095
|
};
|
|
4302
4096
|
var parallelStepSpecHandler = {
|
|
4303
4097
|
type: "parallel",
|
|
4304
|
-
handler(
|
|
4305
|
-
const innerStep = rootHandler(
|
|
4098
|
+
handler(step2, rootHandler) {
|
|
4099
|
+
const innerStep = rootHandler(step2.step, rootHandler);
|
|
4306
4100
|
return {
|
|
4307
|
-
...
|
|
4101
|
+
...step2,
|
|
4308
4102
|
async run(input, context, resumedState) {
|
|
4309
4103
|
const items = input.items ?? [];
|
|
4310
4104
|
const promises = items.map((innerInput, index) => {
|
|
@@ -4337,19 +4131,22 @@ var parallelStepSpecHandler = {
|
|
|
4337
4131
|
};
|
|
4338
4132
|
var customStepSpecHandler = {
|
|
4339
4133
|
type: "custom",
|
|
4340
|
-
handler(
|
|
4134
|
+
handler(step2) {
|
|
4341
4135
|
return {
|
|
4342
|
-
...
|
|
4136
|
+
...step2,
|
|
4343
4137
|
async run(input, context, resumedState) {
|
|
4344
|
-
return
|
|
4138
|
+
return step2.run(input, context, resumedState);
|
|
4345
4139
|
}
|
|
4346
4140
|
};
|
|
4347
4141
|
}
|
|
4348
4142
|
};
|
|
4143
|
+
function step(id, run2) {
|
|
4144
|
+
return { id, type: "custom", run: run2 };
|
|
4145
|
+
}
|
|
4349
4146
|
|
|
4350
4147
|
// src/workflow/workflow.ts
|
|
4351
|
-
var run = async (workflow, context,
|
|
4352
|
-
const rootStep =
|
|
4148
|
+
var run = async (workflow, context, handler15, input) => {
|
|
4149
|
+
const rootStep = handler15(workflow.step, handler15);
|
|
4353
4150
|
const result = await runStep(rootStep, input, context, void 0, {});
|
|
4354
4151
|
switch (result.type) {
|
|
4355
4152
|
case "paused":
|
|
@@ -4369,8 +4166,8 @@ var run = async (workflow, context, handler16, input) => {
|
|
|
4369
4166
|
};
|
|
4370
4167
|
}
|
|
4371
4168
|
};
|
|
4372
|
-
var resume = async (workflow, context,
|
|
4373
|
-
const rootStep =
|
|
4169
|
+
var resume = async (workflow, context, handler15, state, input) => {
|
|
4170
|
+
const rootStep = handler15(workflow.step, handler15);
|
|
4374
4171
|
const result = await runStep(rootStep, input, context, state, {});
|
|
4375
4172
|
switch (result.type) {
|
|
4376
4173
|
case "paused":
|
|
@@ -4406,7 +4203,6 @@ export {
|
|
|
4406
4203
|
UsageMeter,
|
|
4407
4204
|
agentsPrompt,
|
|
4408
4205
|
allAgents,
|
|
4409
|
-
allTools_exports as allTools,
|
|
4410
4206
|
analyzerAgentInfo,
|
|
4411
4207
|
architectAgentInfo,
|
|
4412
4208
|
askFollowupQuestion_default as askFollowupQuestion,
|
|
@@ -4416,6 +4212,7 @@ export {
|
|
|
4416
4212
|
codeFixerAgentInfo,
|
|
4417
4213
|
coderAgentInfo,
|
|
4418
4214
|
combineHandlers,
|
|
4215
|
+
command,
|
|
4419
4216
|
commandStepSpecHandler,
|
|
4420
4217
|
configSchema,
|
|
4421
4218
|
createNewProject,
|
|
@@ -4440,6 +4237,7 @@ export {
|
|
|
4440
4237
|
makeTool,
|
|
4441
4238
|
parallelStepSpecHandler,
|
|
4442
4239
|
parseAssistantMessage,
|
|
4240
|
+
parseJsonFromMarkdown,
|
|
4443
4241
|
readBinaryFile_default as readBinaryFile,
|
|
4444
4242
|
readFile_default as readFile,
|
|
4445
4243
|
removeFile_default as removeFile,
|
|
@@ -4448,10 +4246,10 @@ export {
|
|
|
4448
4246
|
replaceInFile as replaceInFileHelper,
|
|
4449
4247
|
responsePrompts,
|
|
4450
4248
|
resume,
|
|
4451
|
-
reviewDiff,
|
|
4452
4249
|
run,
|
|
4453
4250
|
searchFiles_default as searchFiles,
|
|
4454
4251
|
sequentialStepSpecHandler,
|
|
4252
|
+
step,
|
|
4455
4253
|
systemInformation,
|
|
4456
4254
|
toolUsePrompt,
|
|
4457
4255
|
writeToFile_default as writeToFile
|