deepfish-ai 2.0.3 → 2.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.js +454 -429
- package/dist/serve/pm2-server.js +312 -315
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -9001,7 +9001,7 @@ var import_path18 = __toESM(require("path"));
|
|
|
9001
9001
|
var import_crypto4 = require("crypto");
|
|
9002
9002
|
|
|
9003
9003
|
// src/agent/AIAgent/index.ts
|
|
9004
|
-
var
|
|
9004
|
+
var import_langchain16 = require("langchain");
|
|
9005
9005
|
var import_deepagents = require("deepagents");
|
|
9006
9006
|
|
|
9007
9007
|
// src/agent/AIAgent/utils/langgraph-checkpoint-filesystem/filesystem-saver.ts
|
|
@@ -24428,7 +24428,7 @@ var import_child_process2 = require("child_process");
|
|
|
24428
24428
|
var import_chardet = __toESM(require_lib());
|
|
24429
24429
|
var import_os2 = __toESM(require("os"));
|
|
24430
24430
|
var import_iconv_lite = __toESM(require("iconv-lite"));
|
|
24431
|
-
var
|
|
24431
|
+
var import_langchain3 = require("langchain");
|
|
24432
24432
|
|
|
24433
24433
|
// src/cli/cli-utils/getGlobalData.ts
|
|
24434
24434
|
var import_path6 = __toESM(require("path"));
|
|
@@ -24445,6 +24445,267 @@ function getEncoding() {
|
|
|
24445
24445
|
return (getConfig()?.encoding || "auto").toLowerCase();
|
|
24446
24446
|
}
|
|
24447
24447
|
|
|
24448
|
+
// src/agent/tools/utils.ts
|
|
24449
|
+
var import_langchain2 = require("langchain");
|
|
24450
|
+
var import_path8 = __toESM(require("path"));
|
|
24451
|
+
var import_fs_extra8 = __toESM(require("fs-extra"));
|
|
24452
|
+
var import_crypto = require("crypto");
|
|
24453
|
+
|
|
24454
|
+
// src/agent/tools/fileTools.ts
|
|
24455
|
+
var import_fs_extra7 = __toESM(require("fs-extra"));
|
|
24456
|
+
var import_path7 = __toESM(require("path"));
|
|
24457
|
+
var DEFAULT_MAX_OUTPUT = 6e4;
|
|
24458
|
+
var TEXT_FILE_EXTENSIONS = /* @__PURE__ */ new Set([
|
|
24459
|
+
".txt",
|
|
24460
|
+
".md",
|
|
24461
|
+
".json",
|
|
24462
|
+
".json5",
|
|
24463
|
+
".js",
|
|
24464
|
+
".jsx",
|
|
24465
|
+
".ts",
|
|
24466
|
+
".tsx",
|
|
24467
|
+
".mjs",
|
|
24468
|
+
".cjs",
|
|
24469
|
+
".css",
|
|
24470
|
+
".less",
|
|
24471
|
+
".scss",
|
|
24472
|
+
".html",
|
|
24473
|
+
".xml",
|
|
24474
|
+
".yaml",
|
|
24475
|
+
".yml",
|
|
24476
|
+
".toml",
|
|
24477
|
+
".ini",
|
|
24478
|
+
".env",
|
|
24479
|
+
".gitignore",
|
|
24480
|
+
".sql",
|
|
24481
|
+
".py",
|
|
24482
|
+
".java",
|
|
24483
|
+
".c",
|
|
24484
|
+
".cpp",
|
|
24485
|
+
".h",
|
|
24486
|
+
".hpp",
|
|
24487
|
+
".cs",
|
|
24488
|
+
".go",
|
|
24489
|
+
".rs",
|
|
24490
|
+
".php",
|
|
24491
|
+
".rb",
|
|
24492
|
+
".sh",
|
|
24493
|
+
".bat",
|
|
24494
|
+
".ps1",
|
|
24495
|
+
".vue",
|
|
24496
|
+
".svelte",
|
|
24497
|
+
".log",
|
|
24498
|
+
".csv"
|
|
24499
|
+
]);
|
|
24500
|
+
function resolveWorkspacePath(inputPath, cwd = getTrueCwd()) {
|
|
24501
|
+
if (!inputPath?.trim()) {
|
|
24502
|
+
throw new Error("\u8DEF\u5F84\u4E0D\u80FD\u4E3A\u7A7A");
|
|
24503
|
+
}
|
|
24504
|
+
return import_path7.default.resolve(cwd, inputPath);
|
|
24505
|
+
}
|
|
24506
|
+
function normalizePathForMatch(filePath) {
|
|
24507
|
+
return filePath.replace(/\\/g, "/");
|
|
24508
|
+
}
|
|
24509
|
+
function truncateOutput(content, maxLength = DEFAULT_MAX_OUTPUT) {
|
|
24510
|
+
if (content.length <= maxLength) {
|
|
24511
|
+
return content;
|
|
24512
|
+
}
|
|
24513
|
+
return `${content.slice(0, maxLength)}
|
|
24514
|
+
|
|
24515
|
+
[\u5185\u5BB9\u5DF2\u622A\u65AD\uFF0C\u4EC5\u663E\u793A\u524D ${maxLength} \u4E2A\u5B57\u7B26\uFF0C\u603B\u957F\u5EA6 ${content.length} \u4E2A\u5B57\u7B26]`;
|
|
24516
|
+
}
|
|
24517
|
+
function isProbablyTextFile(filePath) {
|
|
24518
|
+
const ext = import_path7.default.extname(filePath).toLowerCase();
|
|
24519
|
+
const base = import_path7.default.basename(filePath).toLowerCase();
|
|
24520
|
+
return TEXT_FILE_EXTENSIONS.has(ext) || TEXT_FILE_EXTENSIONS.has(base) || base.startsWith(".env");
|
|
24521
|
+
}
|
|
24522
|
+
function globToRegExp(pattern) {
|
|
24523
|
+
const normalized = normalizePathForMatch(pattern);
|
|
24524
|
+
let regex = "^";
|
|
24525
|
+
for (let i = 0; i < normalized.length; i++) {
|
|
24526
|
+
const char = normalized[i];
|
|
24527
|
+
const next = normalized[i + 1];
|
|
24528
|
+
if (char === "*") {
|
|
24529
|
+
if (next === "*") {
|
|
24530
|
+
const after = normalized[i + 2];
|
|
24531
|
+
if (after === "/") {
|
|
24532
|
+
regex += "(?:.*/)?";
|
|
24533
|
+
i += 2;
|
|
24534
|
+
} else {
|
|
24535
|
+
regex += ".*";
|
|
24536
|
+
i += 1;
|
|
24537
|
+
}
|
|
24538
|
+
} else {
|
|
24539
|
+
regex += "[^/]*";
|
|
24540
|
+
}
|
|
24541
|
+
} else if (char === "?") {
|
|
24542
|
+
regex += "[^/]";
|
|
24543
|
+
} else {
|
|
24544
|
+
regex += char.replace(/[|\\{}()[\]^$+?.]/g, "\\$&");
|
|
24545
|
+
}
|
|
24546
|
+
}
|
|
24547
|
+
regex += "$";
|
|
24548
|
+
return new RegExp(regex, "i");
|
|
24549
|
+
}
|
|
24550
|
+
function matchesGlob(relativePath, pattern) {
|
|
24551
|
+
return globToRegExp(pattern).test(normalizePathForMatch(relativePath));
|
|
24552
|
+
}
|
|
24553
|
+
async function walkFiles(rootDir, options = {}) {
|
|
24554
|
+
const files = [];
|
|
24555
|
+
const includeHidden = options.includeHidden ?? false;
|
|
24556
|
+
const maxFiles = options.maxFiles ?? 5e3;
|
|
24557
|
+
async function walk(currentDir) {
|
|
24558
|
+
if (files.length >= maxFiles) {
|
|
24559
|
+
return;
|
|
24560
|
+
}
|
|
24561
|
+
const entries = await import_fs_extra7.default.readdir(currentDir, { withFileTypes: true });
|
|
24562
|
+
for (const entry of entries) {
|
|
24563
|
+
if (files.length >= maxFiles) {
|
|
24564
|
+
return;
|
|
24565
|
+
}
|
|
24566
|
+
if (!includeHidden && entry.name.startsWith(".")) {
|
|
24567
|
+
continue;
|
|
24568
|
+
}
|
|
24569
|
+
if (entry.name === "node_modules" || entry.name === "dist" || entry.name === ".git") {
|
|
24570
|
+
continue;
|
|
24571
|
+
}
|
|
24572
|
+
const fullPath = import_path7.default.join(currentDir, entry.name);
|
|
24573
|
+
if (entry.isDirectory()) {
|
|
24574
|
+
await walk(fullPath);
|
|
24575
|
+
} else if (entry.isFile()) {
|
|
24576
|
+
files.push(fullPath);
|
|
24577
|
+
}
|
|
24578
|
+
}
|
|
24579
|
+
}
|
|
24580
|
+
await walk(rootDir);
|
|
24581
|
+
return files;
|
|
24582
|
+
}
|
|
24583
|
+
function formatJson(data) {
|
|
24584
|
+
return JSON.stringify(data, null, 2);
|
|
24585
|
+
}
|
|
24586
|
+
|
|
24587
|
+
// src/agent/tools/utils.ts
|
|
24588
|
+
function successResult(data) {
|
|
24589
|
+
return { success: true, data };
|
|
24590
|
+
}
|
|
24591
|
+
function errorResult(error51, data) {
|
|
24592
|
+
const message = error51 instanceof Error ? error51.message : String(error51);
|
|
24593
|
+
return data === void 0 ? { success: false, error: message } : { success: false, error: message, data };
|
|
24594
|
+
}
|
|
24595
|
+
function serializeToolResult(result) {
|
|
24596
|
+
return truncateOutput(JSON.stringify(result, null, 2));
|
|
24597
|
+
}
|
|
24598
|
+
async function safeTool(handler) {
|
|
24599
|
+
try {
|
|
24600
|
+
const data = await handler();
|
|
24601
|
+
return serializeToolResult(successResult(data));
|
|
24602
|
+
} catch (error51) {
|
|
24603
|
+
return serializeToolResult(errorResult(error51));
|
|
24604
|
+
}
|
|
24605
|
+
}
|
|
24606
|
+
function toLangChainTool(func, description) {
|
|
24607
|
+
const { name, description: desc, parameters } = description.function;
|
|
24608
|
+
const { properties } = parameters;
|
|
24609
|
+
const zodProperties = {};
|
|
24610
|
+
for (const [key, value] of Object.entries(properties)) {
|
|
24611
|
+
const { type, description: desc2 } = value;
|
|
24612
|
+
let zodType;
|
|
24613
|
+
switch (type) {
|
|
24614
|
+
case "string":
|
|
24615
|
+
zodType = desc2 ? external_exports.string().describe(desc2) : external_exports.string();
|
|
24616
|
+
break;
|
|
24617
|
+
case "number":
|
|
24618
|
+
zodType = desc2 ? external_exports.number().describe(desc2) : external_exports.number();
|
|
24619
|
+
break;
|
|
24620
|
+
case "boolean":
|
|
24621
|
+
zodType = desc2 ? external_exports.boolean().describe(desc2) : external_exports.boolean();
|
|
24622
|
+
break;
|
|
24623
|
+
case "object":
|
|
24624
|
+
zodType = desc2 ? external_exports.object({}).describe(desc2) : external_exports.object({});
|
|
24625
|
+
break;
|
|
24626
|
+
case "array":
|
|
24627
|
+
zodType = desc2 ? external_exports.array(external_exports.string()).describe(desc2) : external_exports.array(external_exports.string());
|
|
24628
|
+
break;
|
|
24629
|
+
default:
|
|
24630
|
+
zodType = external_exports.any();
|
|
24631
|
+
}
|
|
24632
|
+
zodProperties[key] = zodType;
|
|
24633
|
+
}
|
|
24634
|
+
const schema = external_exports.object(zodProperties);
|
|
24635
|
+
const wrappedFunc = async (args) => {
|
|
24636
|
+
try {
|
|
24637
|
+
const result = await func(...Object.values(args));
|
|
24638
|
+
if (typeof result === "object" && result !== null && "success" in result) {
|
|
24639
|
+
return serializeToolResult(result);
|
|
24640
|
+
}
|
|
24641
|
+
return serializeToolResult(successResult(result));
|
|
24642
|
+
} catch (error51) {
|
|
24643
|
+
return serializeToolResult(errorResult(error51));
|
|
24644
|
+
}
|
|
24645
|
+
};
|
|
24646
|
+
return (0, import_langchain2.tool)(wrappedFunc, {
|
|
24647
|
+
name: `${name}_${(0, import_crypto.randomUUID)().slice(0, 3)}`,
|
|
24648
|
+
description: desc,
|
|
24649
|
+
schema
|
|
24650
|
+
});
|
|
24651
|
+
}
|
|
24652
|
+
function scanUserTools() {
|
|
24653
|
+
const tools = [];
|
|
24654
|
+
const scanPaths = getScanDirPaths();
|
|
24655
|
+
scanPaths.forEach((scanPath) => {
|
|
24656
|
+
const toolsDir = import_path8.default.resolve(scanPath, "tools");
|
|
24657
|
+
if (import_fs_extra8.default.pathExistsSync(toolsDir)) {
|
|
24658
|
+
const files = import_fs_extra8.default.readdirSync(toolsDir);
|
|
24659
|
+
files.forEach((file2) => {
|
|
24660
|
+
if (import_fs_extra8.default.statSync(import_path8.default.resolve(toolsDir, file2)).isDirectory()) {
|
|
24661
|
+
const subDirPath = import_path8.default.resolve(toolsDir, file2);
|
|
24662
|
+
const subFiles = import_fs_extra8.default.readdirSync(subDirPath);
|
|
24663
|
+
const indexFile = subFiles.find((f) => f === "index" || f === "index.cjs");
|
|
24664
|
+
if (indexFile) {
|
|
24665
|
+
const filePath = _scanDeepFishJsFile(import_path8.default.resolve(subDirPath, indexFile), indexFile);
|
|
24666
|
+
if (filePath) {
|
|
24667
|
+
_loadToolsFromFile(filePath, tools);
|
|
24668
|
+
}
|
|
24669
|
+
} else {
|
|
24670
|
+
subFiles.forEach((subFile) => {
|
|
24671
|
+
const filePath = _scanDeepFishJsFile(import_path8.default.resolve(subDirPath, subFile), subFile);
|
|
24672
|
+
if (filePath) {
|
|
24673
|
+
_loadToolsFromFile(filePath, tools);
|
|
24674
|
+
}
|
|
24675
|
+
});
|
|
24676
|
+
}
|
|
24677
|
+
} else {
|
|
24678
|
+
const filePath = _scanDeepFishJsFile(toolsDir, file2);
|
|
24679
|
+
if (filePath) {
|
|
24680
|
+
_loadToolsFromFile(filePath, tools);
|
|
24681
|
+
}
|
|
24682
|
+
}
|
|
24683
|
+
});
|
|
24684
|
+
}
|
|
24685
|
+
});
|
|
24686
|
+
return tools;
|
|
24687
|
+
}
|
|
24688
|
+
function _scanDeepFishJsFile(filePath, fileName) {
|
|
24689
|
+
if (fileName.endsWith(".js") || fileName.endsWith(".cjs")) {
|
|
24690
|
+
const fileContent = import_fs_extra8.default.readFileSync(filePath, "utf-8");
|
|
24691
|
+
if (fileContent.includes("module.exports") && fileContent.includes("descriptions") && fileContent.includes("functions")) {
|
|
24692
|
+
return filePath;
|
|
24693
|
+
}
|
|
24694
|
+
}
|
|
24695
|
+
return null;
|
|
24696
|
+
}
|
|
24697
|
+
function _loadToolsFromFile(filePath, tools) {
|
|
24698
|
+
const toolModule = require(filePath);
|
|
24699
|
+
const { functions, descriptions } = toolModule;
|
|
24700
|
+
descriptions.forEach((desc) => {
|
|
24701
|
+
const func = functions[desc.function.name];
|
|
24702
|
+
if (func) {
|
|
24703
|
+
const langChainTool = toLangChainTool(func, desc);
|
|
24704
|
+
tools.push(langChainTool);
|
|
24705
|
+
}
|
|
24706
|
+
});
|
|
24707
|
+
}
|
|
24708
|
+
|
|
24448
24709
|
// src/agent/tools/executeCommand.ts
|
|
24449
24710
|
function executeCommand(command, timeout = -1, cwd) {
|
|
24450
24711
|
logSuccess(`Executing system command: ${command}; ${timeout > 0 ? `Timeout: ${timeout}ms` : "No timeout limit"}`);
|
|
@@ -24465,16 +24726,14 @@ function executeCommand(command, timeout = -1, cwd) {
|
|
|
24465
24726
|
const stderr = import_iconv_lite.default.decode(result.stderr, targetEncoding);
|
|
24466
24727
|
const code = result.status;
|
|
24467
24728
|
if (stderr && !stderr.trim().startsWith("WARNING")) {
|
|
24468
|
-
|
|
24469
|
-
logError(`Execute error: ${error51.message}`);
|
|
24470
|
-
return `Execute error: ${error51.message}`;
|
|
24729
|
+
throw new Error(`Command failed (code ${code}): ${stderr.trim()}`);
|
|
24471
24730
|
}
|
|
24472
24731
|
logSuccess(`${stdout}
|
|
24473
24732
|
Command executed successfully`);
|
|
24474
24733
|
return stdout || "Command executed successfully";
|
|
24475
|
-
} catch (
|
|
24476
|
-
logError(`
|
|
24477
|
-
|
|
24734
|
+
} catch (error51) {
|
|
24735
|
+
logError(`Execute error: ${error51.message}`);
|
|
24736
|
+
throw error51;
|
|
24478
24737
|
}
|
|
24479
24738
|
}
|
|
24480
24739
|
function detectEncoding(buffer) {
|
|
@@ -24491,24 +24750,19 @@ function detectEncoding(buffer) {
|
|
|
24491
24750
|
}
|
|
24492
24751
|
return import_os2.default.platform() === "win32" ? "gbk" : "utf-8";
|
|
24493
24752
|
}
|
|
24494
|
-
var executeCommandTool = (0,
|
|
24495
|
-
|
|
24496
|
-
|
|
24497
|
-
|
|
24498
|
-
|
|
24499
|
-
|
|
24500
|
-
|
|
24501
|
-
|
|
24502
|
-
command: external_exports.string().describe("\u8981\u6267\u884C\u7684 shell \u547D\u4EE4"),
|
|
24503
|
-
timeout: external_exports.number().default(-1).describe("\u8D85\u65F6\u65F6\u95F4\uFF08\u6BEB\u79D2\uFF09\uFF0C-1 \u8868\u793A\u4E0D\u9650\u5236")
|
|
24504
|
-
})
|
|
24505
|
-
}
|
|
24506
|
-
);
|
|
24753
|
+
var executeCommandTool = (0, import_langchain3.tool)(({ command, timeout }) => safeTool(() => executeCommand(command, timeout)), {
|
|
24754
|
+
name: "execute_command",
|
|
24755
|
+
description: `\u5728\u672C\u5730\u7CFB\u7EDF(${import_os2.default.platform()})\u4E0A\u6267\u884C\u4E00\u6761 shell \u547D\u4EE4\u5E76\u8FD4\u56DE\u8F93\u51FA\u7ED3\u679C\u3002\u9002\u7528\u4E8Erunning\u811A\u672C\u3001\u64CD\u4F5C\u6587\u4EF6\u7CFB\u7EDF\u3001\u542F\u52A8\u8FDB\u7A0B\u7B49\u573A\u666F\u3002\u5FC5\u987B\u6CE8\u610F\u64CD\u4F5C\u7CFB\u7EDF\u7684\u517C\u5BB9\u6027\uFF0C\u5F53\u524D\u64CD\u4F5C\u7CFB\u7EDF\u4E3A ${import_os2.default.platform()}\u3002`,
|
|
24756
|
+
schema: external_exports.object({
|
|
24757
|
+
command: external_exports.string().describe("\u8981\u6267\u884C\u7684 shell \u547D\u4EE4"),
|
|
24758
|
+
timeout: external_exports.number().default(-1).describe("\u8D85\u65F6\u65F6\u95F4\uFF08\u6BEB\u79D2\uFF09\uFF0C-1 \u8868\u793A\u4E0D\u9650\u5236")
|
|
24759
|
+
})
|
|
24760
|
+
});
|
|
24507
24761
|
|
|
24508
24762
|
// src/agent/tools/executeJSCode.ts
|
|
24509
|
-
var
|
|
24510
|
-
var
|
|
24511
|
-
var
|
|
24763
|
+
var import_langchain4 = require("langchain");
|
|
24764
|
+
var import_path9 = __toESM(require("path"));
|
|
24765
|
+
var import_fs_extra9 = __toESM(require("fs-extra"));
|
|
24512
24766
|
var _require = require;
|
|
24513
24767
|
async function executeJSCode(code) {
|
|
24514
24768
|
logInfo("Executing JavaScript code: ");
|
|
@@ -24527,25 +24781,25 @@ async function executeJSCode(code) {
|
|
|
24527
24781
|
throw error51;
|
|
24528
24782
|
}
|
|
24529
24783
|
}
|
|
24530
|
-
var getInstalledPackagesTool = (0,
|
|
24531
|
-
async () => {
|
|
24532
|
-
const packageJson =
|
|
24533
|
-
const pkg =
|
|
24534
|
-
return
|
|
24535
|
-
},
|
|
24784
|
+
var getInstalledPackagesTool = (0, import_langchain4.tool)(
|
|
24785
|
+
async () => safeTool(() => {
|
|
24786
|
+
const packageJson = import_path9.default.join(getCodePath(), "./package.json");
|
|
24787
|
+
const pkg = import_fs_extra9.default.readJsonSync(packageJson);
|
|
24788
|
+
return pkg.dependencies;
|
|
24789
|
+
}),
|
|
24536
24790
|
{
|
|
24537
24791
|
name: "get_installed_packages",
|
|
24538
24792
|
description: "\u83B7\u53D6 deepfish-ai CLI \u5DE5\u5177\u81EA\u8EAB\u5DF2\u5B89\u88C5\u7684 npm \u4F9D\u8D56\u5305\u5217\u8868",
|
|
24539
24793
|
schema: external_exports.object({})
|
|
24540
24794
|
}
|
|
24541
24795
|
);
|
|
24542
|
-
var checkPackageInstalledTool = (0,
|
|
24543
|
-
async ({ packageName }) => {
|
|
24544
|
-
const packageJson =
|
|
24545
|
-
const pkg =
|
|
24796
|
+
var checkPackageInstalledTool = (0, import_langchain4.tool)(
|
|
24797
|
+
async ({ packageName }) => safeTool(() => {
|
|
24798
|
+
const packageJson = import_path9.default.join(getCodePath(), "./package.json");
|
|
24799
|
+
const pkg = import_fs_extra9.default.readJsonSync(packageJson);
|
|
24546
24800
|
const installed = Object.prototype.hasOwnProperty.call(pkg.dependencies, packageName);
|
|
24547
|
-
return
|
|
24548
|
-
},
|
|
24801
|
+
return { packageName, installed };
|
|
24802
|
+
}),
|
|
24549
24803
|
{
|
|
24550
24804
|
name: "check_package_installed",
|
|
24551
24805
|
description: "\u68C0\u67E5\u6307\u5B9A\u7684 npm \u5305\u662F\u5426\u5DF2\u5728 deepfish-ai CLI \u5DE5\u5177\u4E2D\u5B89\u88C5",
|
|
@@ -24554,15 +24808,15 @@ var checkPackageInstalledTool = (0, import_langchain3.tool)(
|
|
|
24554
24808
|
})
|
|
24555
24809
|
}
|
|
24556
24810
|
);
|
|
24557
|
-
var installPackageTool = (0,
|
|
24558
|
-
async ({ packageName }) => {
|
|
24559
|
-
const packageJson =
|
|
24560
|
-
const pkg =
|
|
24811
|
+
var installPackageTool = (0, import_langchain4.tool)(
|
|
24812
|
+
async ({ packageName }) => safeTool(() => {
|
|
24813
|
+
const packageJson = import_path9.default.join(getCodePath(), "./package.json");
|
|
24814
|
+
const pkg = import_fs_extra9.default.readJsonSync(packageJson);
|
|
24561
24815
|
if (Object.prototype.hasOwnProperty.call(pkg.dependencies, packageName)) {
|
|
24562
24816
|
return `Package "${packageName}" is already installed.`;
|
|
24563
24817
|
}
|
|
24564
24818
|
return executeCommand(`npm install ${packageName}`, 12e4, getCodePath());
|
|
24565
|
-
},
|
|
24819
|
+
}),
|
|
24566
24820
|
{
|
|
24567
24821
|
name: "install_package",
|
|
24568
24822
|
description: "\u5728 deepfish-ai CLI \u5DE5\u5177\u4E2D\u5B89\u88C5\u6307\u5B9A\u7684 npm \u5305\uFF0C\u4F7F execute_js_code \u53EF\u4EE5 require \u8BE5\u5305",
|
|
@@ -24571,53 +24825,47 @@ var installPackageTool = (0, import_langchain3.tool)(
|
|
|
24571
24825
|
})
|
|
24572
24826
|
}
|
|
24573
24827
|
);
|
|
24574
|
-
var executeJSCodeTool = (0,
|
|
24575
|
-
|
|
24576
|
-
|
|
24577
|
-
return result;
|
|
24578
|
-
},
|
|
24579
|
-
{
|
|
24580
|
-
name: "execute_js_code",
|
|
24581
|
-
description: `\u6267\u884C\u4E00\u6BB5Node.js\u4EE3\u7801\u5E76\u8FD4\u56DE\u6267\u884C\u7ED3\u679C\u3002\u6CE8\u610F\uFF1A\u4EE3\u7801\u5FC5\u987B\u5305\u542B\u4E00\u4E2A__main()\u51FD\u6570\u4F5C\u4E3A\u6267\u884C\u5165\u53E3\uFF0C__main()\u51FD\u6570\u5185\u5FC5\u987B\u662F\u4E00\u4E2A\u4F7F\u7528async\u524D\u7F00\u7684\u51FD\u6570\u3002\u793A\u4F8B\u4EE3\u7801\uFF1A
|
|
24828
|
+
var executeJSCodeTool = (0, import_langchain4.tool)(async ({ code }) => safeTool(() => executeJSCode(code)), {
|
|
24829
|
+
name: "execute_js_code",
|
|
24830
|
+
description: `\u6267\u884C\u4E00\u6BB5Node.js\u4EE3\u7801\u5E76\u8FD4\u56DE\u6267\u884C\u7ED3\u679C\u3002\u6CE8\u610F\uFF1A\u4EE3\u7801\u5FC5\u987B\u5305\u542B\u4E00\u4E2A__main()\u51FD\u6570\u4F5C\u4E3A\u6267\u884C\u5165\u53E3\uFF0C__main()\u51FD\u6570\u5185\u5FC5\u987B\u662F\u4E00\u4E2A\u4F7F\u7528async\u524D\u7F00\u7684\u51FD\u6570\u3002\u793A\u4F8B\u4EE3\u7801\uFF1A
|
|
24582
24831
|
async function __main() {
|
|
24583
24832
|
const data = await fs.readFile("data.txt", "utf-8")
|
|
24584
24833
|
return data
|
|
24585
24834
|
}
|
|
24586
24835
|
`,
|
|
24587
|
-
|
|
24588
|
-
|
|
24589
|
-
|
|
24590
|
-
|
|
24591
|
-
);
|
|
24836
|
+
schema: external_exports.object({
|
|
24837
|
+
code: external_exports.string().describe("\u8981\u6267\u884C\u7684Node.js\u4EE3\u7801")
|
|
24838
|
+
})
|
|
24839
|
+
});
|
|
24592
24840
|
var packageTools = [getInstalledPackagesTool, checkPackageInstalledTool, installPackageTool, executeJSCodeTool];
|
|
24593
24841
|
|
|
24594
24842
|
// src/cli/cli-utils/UserCache.ts
|
|
24595
|
-
var
|
|
24596
|
-
var
|
|
24597
|
-
var
|
|
24843
|
+
var import_path10 = __toESM(require("path"));
|
|
24844
|
+
var import_fs_extra10 = __toESM(require("fs-extra"));
|
|
24845
|
+
var import_crypto2 = require("crypto");
|
|
24598
24846
|
var UserCache = class {
|
|
24599
24847
|
cacheDir;
|
|
24600
24848
|
catalogPath;
|
|
24601
24849
|
constructor() {
|
|
24602
24850
|
const cacheDir = getUserStorePath();
|
|
24603
|
-
const catalog =
|
|
24604
|
-
|
|
24851
|
+
const catalog = import_path10.default.join(cacheDir, "catalog.json");
|
|
24852
|
+
import_fs_extra10.default.ensureFileSync(catalog);
|
|
24605
24853
|
this.cacheDir = cacheDir;
|
|
24606
24854
|
this.catalogPath = catalog;
|
|
24607
24855
|
}
|
|
24608
24856
|
getCatalog() {
|
|
24609
|
-
return
|
|
24857
|
+
return import_fs_extra10.default.readJSONSync(this.catalogPath, { throws: false }) || [];
|
|
24610
24858
|
}
|
|
24611
24859
|
getCatalogFilePath() {
|
|
24612
24860
|
return this.catalogPath;
|
|
24613
24861
|
}
|
|
24614
24862
|
updateCatalog(catalog) {
|
|
24615
|
-
|
|
24863
|
+
import_fs_extra10.default.writeJSONSync(this.catalogPath, catalog);
|
|
24616
24864
|
}
|
|
24617
24865
|
add(description, content) {
|
|
24618
|
-
const id = (0,
|
|
24619
|
-
const filePath =
|
|
24620
|
-
|
|
24866
|
+
const id = (0, import_crypto2.randomUUID)();
|
|
24867
|
+
const filePath = import_path10.default.join(this.cacheDir, `${id}.md`);
|
|
24868
|
+
import_fs_extra10.default.writeFileSync(filePath, content, "utf-8");
|
|
24621
24869
|
const catalog = this.getCatalog();
|
|
24622
24870
|
catalog.push({ id, description });
|
|
24623
24871
|
this.updateCatalog(catalog);
|
|
@@ -24626,9 +24874,9 @@ var UserCache = class {
|
|
|
24626
24874
|
const catalog = this.getCatalog();
|
|
24627
24875
|
const item = catalog.find((item2) => item2.id === id);
|
|
24628
24876
|
if (item) {
|
|
24629
|
-
const filePath =
|
|
24630
|
-
if (
|
|
24631
|
-
return { description: item.description, content:
|
|
24877
|
+
const filePath = import_path10.default.join(this.cacheDir, `${id}.md`);
|
|
24878
|
+
if (import_fs_extra10.default.existsSync(filePath)) {
|
|
24879
|
+
return { description: item.description, content: import_fs_extra10.default.readFileSync(filePath, "utf-8") };
|
|
24632
24880
|
}
|
|
24633
24881
|
}
|
|
24634
24882
|
return { description: "", content: "" };
|
|
@@ -24651,9 +24899,9 @@ var UserCache = class {
|
|
|
24651
24899
|
}
|
|
24652
24900
|
catalog.splice(idx, 1);
|
|
24653
24901
|
this.updateCatalog(catalog);
|
|
24654
|
-
const filePath =
|
|
24655
|
-
if (
|
|
24656
|
-
|
|
24902
|
+
const filePath = import_path10.default.join(this.cacheDir, `${id}.md`);
|
|
24903
|
+
if (import_fs_extra10.default.existsSync(filePath)) {
|
|
24904
|
+
import_fs_extra10.default.removeSync(filePath);
|
|
24657
24905
|
}
|
|
24658
24906
|
return true;
|
|
24659
24907
|
}
|
|
@@ -24671,20 +24919,20 @@ var UserCache = class {
|
|
|
24671
24919
|
if (!item) {
|
|
24672
24920
|
return false;
|
|
24673
24921
|
}
|
|
24674
|
-
const filePath =
|
|
24675
|
-
|
|
24922
|
+
const filePath = import_path10.default.join(this.cacheDir, `${id}.md`);
|
|
24923
|
+
import_fs_extra10.default.writeFileSync(filePath, content, "utf-8");
|
|
24676
24924
|
return true;
|
|
24677
24925
|
}
|
|
24678
24926
|
};
|
|
24679
24927
|
|
|
24680
24928
|
// src/agent/tools/learn-self.ts
|
|
24681
|
-
var
|
|
24929
|
+
var import_langchain5 = require("langchain");
|
|
24682
24930
|
var userCache = new UserCache();
|
|
24683
|
-
var learnSelfTool = (0,
|
|
24684
|
-
({ description, content }) => {
|
|
24931
|
+
var learnSelfTool = (0, import_langchain5.tool)(
|
|
24932
|
+
({ description, content }) => safeTool(() => {
|
|
24685
24933
|
userCache.add(description, content);
|
|
24686
24934
|
return `\u5DF2\u7F13\u5B58\u89E3\u51B3\u65B9\u6848: ${description}`;
|
|
24687
|
-
},
|
|
24935
|
+
}),
|
|
24688
24936
|
{
|
|
24689
24937
|
name: "learn_self",
|
|
24690
24938
|
description: "\u5C06\u89E3\u51B3\u590D\u6742\u95EE\u9898\u7684\u65B9\u6848\u7F13\u5B58\u5230\u672C\u5730\u77E5\u8BC6\u5E93\u3002\u5F53\u4F60\u6210\u529F\u89E3\u51B3\u4E86\u4E00\u4E2A\u590D\u6742\u95EE\u9898\u540E\uFF0C\u8C03\u7528\u6B64\u5DE5\u5177\u4FDD\u5B58\u89E3\u51B3\u65B9\u6848\uFF0C\u4EE5\u4FBF\u5C06\u6765\u9047\u5230\u7C7B\u4F3C\u95EE\u9898\u65F6\u53EF\u4EE5\u590D\u7528\u3002description \u5FC5\u987B\u7B80\u6D01\uFF0C\u4E0D\u8D85\u8FC750\u5B57\uFF1Bcontent \u5FC5\u987B\u4F7F\u7528 Markdown \u683C\u5F0F\u7F16\u5199\uFF0C\u5305\u542B\u5B8C\u6574\u7684\u89E3\u51B3\u6B65\u9AA4\u548C\u5173\u952E\u4FE1\u606F\u3002",
|
|
@@ -24694,27 +24942,27 @@ var learnSelfTool = (0, import_langchain4.tool)(
|
|
|
24694
24942
|
})
|
|
24695
24943
|
}
|
|
24696
24944
|
);
|
|
24697
|
-
var getLearnedDetailTool = (0,
|
|
24698
|
-
({ indexOrId }) => {
|
|
24945
|
+
var getLearnedDetailTool = (0, import_langchain5.tool)(
|
|
24946
|
+
({ indexOrId }) => safeTool(() => {
|
|
24699
24947
|
const index = Number(indexOrId);
|
|
24700
24948
|
let item;
|
|
24701
24949
|
if (!isNaN(index) && Number.isInteger(index)) {
|
|
24702
24950
|
item = userCache.getByIndex(index);
|
|
24703
24951
|
if (!item) {
|
|
24704
|
-
|
|
24952
|
+
throw new Error(`\u65E0\u6548\u7684\u7D22\u5F15: ${indexOrId}\uFF0C\u8BF7\u8BFB\u53D6 catalog.json \u67E5\u770B\u6709\u6548\u7D22\u5F15\u3002`);
|
|
24705
24953
|
}
|
|
24706
24954
|
} else {
|
|
24707
24955
|
const catalog = userCache.list();
|
|
24708
24956
|
item = catalog.find((c) => c.id === indexOrId);
|
|
24709
24957
|
if (!item) {
|
|
24710
|
-
|
|
24958
|
+
throw new Error(`\u672A\u627E\u5230 id \u4E3A "${indexOrId}" \u7684\u7F13\u5B58\u9879\uFF0C\u8BF7\u8BFB\u53D6 catalog.json \u67E5\u770B\u6240\u6709\u7F13\u5B58\u3002`);
|
|
24711
24959
|
}
|
|
24712
24960
|
}
|
|
24713
24961
|
const detail = userCache.getContentById(item.id);
|
|
24714
24962
|
return `## ${detail.description}
|
|
24715
24963
|
|
|
24716
24964
|
${detail.content}`;
|
|
24717
|
-
},
|
|
24965
|
+
}),
|
|
24718
24966
|
{
|
|
24719
24967
|
name: "get_learned_detail",
|
|
24720
24968
|
description: "\u8BFB\u53D6\u6307\u5B9A\u7F13\u5B58\u65B9\u6848\u7684\u5B8C\u6574\u8BE6\u7EC6\u4FE1\u606F\u3002\u4F20\u5165\u7D22\u5F15\u53F7\uFF08\u6570\u5B57\uFF09\u6216 id\uFF08uuid \u5B57\u7B26\u4E32\uFF09\u6765\u83B7\u53D6\u65B9\u6848\u7684\u5B8C\u6574\u5185\u5BB9\u3002",
|
|
@@ -24723,14 +24971,14 @@ ${detail.content}`;
|
|
|
24723
24971
|
})
|
|
24724
24972
|
}
|
|
24725
24973
|
);
|
|
24726
|
-
var updateLearnContentTool = (0,
|
|
24727
|
-
({ id, content }) => {
|
|
24974
|
+
var updateLearnContentTool = (0, import_langchain5.tool)(
|
|
24975
|
+
({ id, content }) => safeTool(() => {
|
|
24728
24976
|
const success2 = userCache.update(id, content);
|
|
24729
24977
|
if (!success2) {
|
|
24730
|
-
|
|
24978
|
+
throw new Error(`\u66F4\u65B0\u5931\u8D25: \u672A\u627E\u5230 id \u4E3A "${id}" \u7684\u7F13\u5B58\u9879\uFF0C\u8BF7\u8BFB\u53D6 catalog.json \u67E5\u770B\u6240\u6709\u7F13\u5B58\u3002`);
|
|
24731
24979
|
}
|
|
24732
24980
|
return `\u5DF2\u66F4\u65B0\u7F13\u5B58\u65B9\u6848: ${id}`;
|
|
24733
|
-
},
|
|
24981
|
+
}),
|
|
24734
24982
|
{
|
|
24735
24983
|
name: "update_learned_content",
|
|
24736
24984
|
description: "\u66F4\u65B0\u5DF2\u6709\u7F13\u5B58\u65B9\u6848\u7684\u5185\u5BB9\u3002\u4F20\u5165\u7F13\u5B58\u9879\u7684 id\uFF08uuid \u5B57\u7B26\u4E32\uFF09\u548C\u65B0\u7684 Markdown \u5185\u5BB9\uFF0C\u8986\u76D6\u539F\u6709\u6587\u4EF6\u3002\u9002\u7528\u4E8E\u8865\u5145\u66F4\u591A\u7EC6\u8282\u6216\u4FEE\u6B63\u4E4B\u524D\u7684\u65B9\u6848\u3002",
|
|
@@ -24740,24 +24988,47 @@ var updateLearnContentTool = (0, import_langchain4.tool)(
|
|
|
24740
24988
|
})
|
|
24741
24989
|
}
|
|
24742
24990
|
);
|
|
24743
|
-
var getCatalogFilePathTool = (0,
|
|
24744
|
-
|
|
24745
|
-
|
|
24746
|
-
}
|
|
24747
|
-
|
|
24748
|
-
name: "get_catalog_file_path",
|
|
24749
|
-
description: "\u83B7\u53D6\u7F13\u5B58\u7D22\u5F15\u6587\u4EF6 catalog.json \u7684\u7EDD\u5BF9\u8DEF\u5F84\u3002\u8BE5\u6587\u4EF6\u5305\u542B\u6240\u6709\u5DF2\u7F13\u5B58\u65B9\u6848\u7684 id \u548C description \u5217\u8868\u3002\u83B7\u53D6\u8DEF\u5F84\u540E\uFF0C\u4F7F\u7528 read_file \u5DE5\u5177\u8BFB\u53D6\u8BE5\u6587\u4EF6\u5373\u53EF\u67E5\u770B\u6240\u6709\u7F13\u5B58\u9879\u3002",
|
|
24750
|
-
schema: external_exports.object({})
|
|
24751
|
-
}
|
|
24752
|
-
);
|
|
24991
|
+
var getCatalogFilePathTool = (0, import_langchain5.tool)(() => safeTool(() => userCache.getCatalogFilePath()), {
|
|
24992
|
+
name: "get_catalog_file_path",
|
|
24993
|
+
description: "\u83B7\u53D6\u7F13\u5B58\u7D22\u5F15\u6587\u4EF6 catalog.json \u7684\u7EDD\u5BF9\u8DEF\u5F84\u3002\u8BE5\u6587\u4EF6\u5305\u542B\u6240\u6709\u5DF2\u7F13\u5B58\u65B9\u6848\u7684 id \u548C description \u5217\u8868\u3002\u83B7\u53D6\u8DEF\u5F84\u540E\uFF0C\u4F7F\u7528 read_file \u5DE5\u5177\u8BFB\u53D6\u8BE5\u6587\u4EF6\u5373\u53EF\u67E5\u770B\u6240\u6709\u7F13\u5B58\u9879\u3002",
|
|
24994
|
+
schema: external_exports.object({})
|
|
24995
|
+
});
|
|
24753
24996
|
var learnTools = [learnSelfTool, getLearnedDetailTool, updateLearnContentTool, getCatalogFilePathTool];
|
|
24754
24997
|
|
|
24755
24998
|
// src/agent/tools/mcp.ts
|
|
24756
24999
|
var import_mcp_adapters = require("@langchain/mcp-adapters");
|
|
24757
|
-
var
|
|
24758
|
-
var
|
|
25000
|
+
var import_fs_extra11 = __toESM(require("fs-extra"));
|
|
25001
|
+
var import_langchain6 = require("langchain");
|
|
25002
|
+
var import_path11 = __toESM(require("path"));
|
|
25003
|
+
function normalizeMcpToolResult(result) {
|
|
25004
|
+
if (Array.isArray(result)) {
|
|
25005
|
+
return result.map((item) => normalizeMcpToolResult(item));
|
|
25006
|
+
}
|
|
25007
|
+
if (result && typeof result === "object" && "content" in result) {
|
|
25008
|
+
return result;
|
|
25009
|
+
}
|
|
25010
|
+
return result;
|
|
25011
|
+
}
|
|
25012
|
+
function wrapMcpTool(mcpTool) {
|
|
25013
|
+
const wrapped = (0, import_langchain6.tool)(
|
|
25014
|
+
async (input, runtime) => {
|
|
25015
|
+
try {
|
|
25016
|
+
const result = await mcpTool.invoke(input, runtime);
|
|
25017
|
+
return serializeToolResult(successResult(normalizeMcpToolResult(result)));
|
|
25018
|
+
} catch (error51) {
|
|
25019
|
+
return serializeToolResult(errorResult(error51));
|
|
25020
|
+
}
|
|
25021
|
+
},
|
|
25022
|
+
{
|
|
25023
|
+
name: mcpTool.name,
|
|
25024
|
+
description: mcpTool.description,
|
|
25025
|
+
schema: mcpTool.schema ?? external_exports.object({})
|
|
25026
|
+
}
|
|
25027
|
+
);
|
|
25028
|
+
return wrapped;
|
|
25029
|
+
}
|
|
24759
25030
|
async function loadMcpToolsFromConfigPath(mcpFilePath) {
|
|
24760
|
-
const jsonContent =
|
|
25031
|
+
const jsonContent = import_fs_extra11.default.readJSONSync(mcpFilePath);
|
|
24761
25032
|
const { mcpServers } = jsonContent;
|
|
24762
25033
|
if (!mcpServers || Object.keys(mcpServers).length === 0) {
|
|
24763
25034
|
return [];
|
|
@@ -24785,7 +25056,7 @@ async function loadMcpToolsFromConfigPath(mcpFilePath) {
|
|
|
24785
25056
|
const client = new import_mcp_adapters.MultiServerMCPClient(jsonContent.mcpServers);
|
|
24786
25057
|
const tools = await client.getTools();
|
|
24787
25058
|
logInfo(`Loaded ${tools.length} tools from MCP config.`);
|
|
24788
|
-
return tools;
|
|
25059
|
+
return tools.map(wrapMcpTool);
|
|
24789
25060
|
} catch (error51) {
|
|
24790
25061
|
console.log("Error loading MCP tools:", error51);
|
|
24791
25062
|
return [];
|
|
@@ -24795,8 +25066,8 @@ async function scanUserMcp() {
|
|
|
24795
25066
|
const tools = [];
|
|
24796
25067
|
const scanPaths = getScanDirPaths();
|
|
24797
25068
|
for (const scanPath of scanPaths) {
|
|
24798
|
-
const mcpFilePath =
|
|
24799
|
-
if (
|
|
25069
|
+
const mcpFilePath = import_path11.default.join(scanPath, "mcp.json");
|
|
25070
|
+
if (import_fs_extra11.default.existsSync(mcpFilePath)) {
|
|
24800
25071
|
const mcpTools = await loadMcpToolsFromConfigPath(mcpFilePath);
|
|
24801
25072
|
tools.push(...mcpTools);
|
|
24802
25073
|
}
|
|
@@ -24804,271 +25075,31 @@ async function scanUserMcp() {
|
|
|
24804
25075
|
return tools;
|
|
24805
25076
|
}
|
|
24806
25077
|
|
|
24807
|
-
// src/agent/tools/utils.ts
|
|
24808
|
-
var import_langchain5 = require("langchain");
|
|
24809
|
-
var import_path10 = __toESM(require("path"));
|
|
24810
|
-
var import_fs_extra10 = __toESM(require("fs-extra"));
|
|
24811
|
-
var import_crypto2 = require("crypto");
|
|
24812
|
-
function toLangChainTool(func, description) {
|
|
24813
|
-
const { name, description: desc, parameters } = description.function;
|
|
24814
|
-
const { properties, required: required2 } = parameters;
|
|
24815
|
-
const zodProperties = {};
|
|
24816
|
-
for (const [key, value] of Object.entries(properties)) {
|
|
24817
|
-
const { type, description: desc2, ...rest } = value;
|
|
24818
|
-
let zodType;
|
|
24819
|
-
switch (type) {
|
|
24820
|
-
case "string":
|
|
24821
|
-
zodType = desc2 ? external_exports.string().describe(desc2) : external_exports.string();
|
|
24822
|
-
break;
|
|
24823
|
-
case "number":
|
|
24824
|
-
zodType = desc2 ? external_exports.number().describe(desc2) : external_exports.number();
|
|
24825
|
-
break;
|
|
24826
|
-
case "boolean":
|
|
24827
|
-
zodType = desc2 ? external_exports.boolean().describe(desc2) : external_exports.boolean();
|
|
24828
|
-
break;
|
|
24829
|
-
case "object":
|
|
24830
|
-
zodType = desc2 ? external_exports.object({}).describe(desc2) : external_exports.object({});
|
|
24831
|
-
break;
|
|
24832
|
-
case "array":
|
|
24833
|
-
zodType = desc2 ? external_exports.array(external_exports.string()).describe(desc2) : external_exports.array(external_exports.string());
|
|
24834
|
-
break;
|
|
24835
|
-
default:
|
|
24836
|
-
zodType = external_exports.any();
|
|
24837
|
-
}
|
|
24838
|
-
zodProperties[key] = zodType;
|
|
24839
|
-
}
|
|
24840
|
-
const schema = external_exports.object(zodProperties);
|
|
24841
|
-
const wrappedFunc = (args) => {
|
|
24842
|
-
const result = func(...Object.values(args));
|
|
24843
|
-
if (typeof result === "object") {
|
|
24844
|
-
return JSON.stringify(result);
|
|
24845
|
-
} else {
|
|
24846
|
-
return result;
|
|
24847
|
-
}
|
|
24848
|
-
};
|
|
24849
|
-
return (0, import_langchain5.tool)(wrappedFunc, {
|
|
24850
|
-
name: `${name}_${(0, import_crypto2.randomUUID)().slice(0, 3)}`,
|
|
24851
|
-
description: desc,
|
|
24852
|
-
schema
|
|
24853
|
-
});
|
|
24854
|
-
}
|
|
24855
|
-
function scanUserTools() {
|
|
24856
|
-
const tools = [];
|
|
24857
|
-
const scanPaths = getScanDirPaths();
|
|
24858
|
-
scanPaths.forEach((scanPath) => {
|
|
24859
|
-
const toolsDir = import_path10.default.resolve(scanPath, "tools");
|
|
24860
|
-
if (import_fs_extra10.default.pathExistsSync(toolsDir)) {
|
|
24861
|
-
const files = import_fs_extra10.default.readdirSync(toolsDir);
|
|
24862
|
-
files.forEach((file2) => {
|
|
24863
|
-
if (import_fs_extra10.default.statSync(import_path10.default.resolve(toolsDir, file2)).isDirectory()) {
|
|
24864
|
-
const subDirPath = import_path10.default.resolve(toolsDir, file2);
|
|
24865
|
-
const subFiles = import_fs_extra10.default.readdirSync(subDirPath);
|
|
24866
|
-
const indexFile = subFiles.find((f) => f === "index" || f === "index.cjs");
|
|
24867
|
-
if (indexFile) {
|
|
24868
|
-
const filePath = _scanDeepFishJsFile(import_path10.default.resolve(subDirPath, indexFile), indexFile);
|
|
24869
|
-
if (filePath) {
|
|
24870
|
-
_loadToolsFromFile(filePath, tools);
|
|
24871
|
-
}
|
|
24872
|
-
} else {
|
|
24873
|
-
subFiles.forEach((subFile) => {
|
|
24874
|
-
const filePath = _scanDeepFishJsFile(import_path10.default.resolve(subDirPath, subFile), subFile);
|
|
24875
|
-
if (filePath) {
|
|
24876
|
-
_loadToolsFromFile(filePath, tools);
|
|
24877
|
-
}
|
|
24878
|
-
});
|
|
24879
|
-
}
|
|
24880
|
-
} else {
|
|
24881
|
-
const filePath = _scanDeepFishJsFile(toolsDir, file2);
|
|
24882
|
-
if (filePath) {
|
|
24883
|
-
_loadToolsFromFile(filePath, tools);
|
|
24884
|
-
}
|
|
24885
|
-
}
|
|
24886
|
-
});
|
|
24887
|
-
}
|
|
24888
|
-
});
|
|
24889
|
-
return tools;
|
|
24890
|
-
}
|
|
24891
|
-
function _scanDeepFishJsFile(filePath, fileName) {
|
|
24892
|
-
if (fileName.endsWith(".js") || fileName.endsWith(".cjs")) {
|
|
24893
|
-
const fileContent = import_fs_extra10.default.readFileSync(filePath, "utf-8");
|
|
24894
|
-
if (fileContent.includes("module.exports") && fileContent.includes("descriptions") && fileContent.includes("functions")) {
|
|
24895
|
-
return filePath;
|
|
24896
|
-
}
|
|
24897
|
-
}
|
|
24898
|
-
return null;
|
|
24899
|
-
}
|
|
24900
|
-
function _loadToolsFromFile(filePath, tools) {
|
|
24901
|
-
const toolModule = require(filePath);
|
|
24902
|
-
const { functions, descriptions } = toolModule;
|
|
24903
|
-
descriptions.forEach((desc) => {
|
|
24904
|
-
const func = functions[desc.function.name];
|
|
24905
|
-
if (func) {
|
|
24906
|
-
const langChainTool = toLangChainTool(func, desc);
|
|
24907
|
-
tools.push(langChainTool);
|
|
24908
|
-
}
|
|
24909
|
-
});
|
|
24910
|
-
}
|
|
24911
|
-
|
|
24912
25078
|
// src/agent/tools/edit.ts
|
|
24913
25079
|
var import_fs_extra12 = __toESM(require("fs-extra"));
|
|
24914
|
-
var
|
|
24915
|
-
|
|
24916
|
-
// src/agent/tools/fileTools.ts
|
|
24917
|
-
var import_fs_extra11 = __toESM(require("fs-extra"));
|
|
24918
|
-
var import_path11 = __toESM(require("path"));
|
|
24919
|
-
var DEFAULT_MAX_OUTPUT = 6e4;
|
|
24920
|
-
var TEXT_FILE_EXTENSIONS = /* @__PURE__ */ new Set([
|
|
24921
|
-
".txt",
|
|
24922
|
-
".md",
|
|
24923
|
-
".json",
|
|
24924
|
-
".json5",
|
|
24925
|
-
".js",
|
|
24926
|
-
".jsx",
|
|
24927
|
-
".ts",
|
|
24928
|
-
".tsx",
|
|
24929
|
-
".mjs",
|
|
24930
|
-
".cjs",
|
|
24931
|
-
".css",
|
|
24932
|
-
".less",
|
|
24933
|
-
".scss",
|
|
24934
|
-
".html",
|
|
24935
|
-
".xml",
|
|
24936
|
-
".yaml",
|
|
24937
|
-
".yml",
|
|
24938
|
-
".toml",
|
|
24939
|
-
".ini",
|
|
24940
|
-
".env",
|
|
24941
|
-
".gitignore",
|
|
24942
|
-
".sql",
|
|
24943
|
-
".py",
|
|
24944
|
-
".java",
|
|
24945
|
-
".c",
|
|
24946
|
-
".cpp",
|
|
24947
|
-
".h",
|
|
24948
|
-
".hpp",
|
|
24949
|
-
".cs",
|
|
24950
|
-
".go",
|
|
24951
|
-
".rs",
|
|
24952
|
-
".php",
|
|
24953
|
-
".rb",
|
|
24954
|
-
".sh",
|
|
24955
|
-
".bat",
|
|
24956
|
-
".ps1",
|
|
24957
|
-
".vue",
|
|
24958
|
-
".svelte",
|
|
24959
|
-
".log",
|
|
24960
|
-
".csv"
|
|
24961
|
-
]);
|
|
24962
|
-
function resolveWorkspacePath(inputPath, cwd = getTrueCwd()) {
|
|
24963
|
-
if (!inputPath?.trim()) {
|
|
24964
|
-
throw new Error("\u8DEF\u5F84\u4E0D\u80FD\u4E3A\u7A7A");
|
|
24965
|
-
}
|
|
24966
|
-
return import_path11.default.resolve(cwd, inputPath);
|
|
24967
|
-
}
|
|
24968
|
-
function normalizePathForMatch(filePath) {
|
|
24969
|
-
return filePath.replace(/\\/g, "/");
|
|
24970
|
-
}
|
|
24971
|
-
function truncateOutput(content, maxLength = DEFAULT_MAX_OUTPUT) {
|
|
24972
|
-
if (content.length <= maxLength) {
|
|
24973
|
-
return content;
|
|
24974
|
-
}
|
|
24975
|
-
return `${content.slice(0, maxLength)}
|
|
24976
|
-
|
|
24977
|
-
[\u5185\u5BB9\u5DF2\u622A\u65AD\uFF0C\u4EC5\u663E\u793A\u524D ${maxLength} \u4E2A\u5B57\u7B26\uFF0C\u603B\u957F\u5EA6 ${content.length} \u4E2A\u5B57\u7B26]`;
|
|
24978
|
-
}
|
|
24979
|
-
function isProbablyTextFile(filePath) {
|
|
24980
|
-
const ext = import_path11.default.extname(filePath).toLowerCase();
|
|
24981
|
-
const base = import_path11.default.basename(filePath).toLowerCase();
|
|
24982
|
-
return TEXT_FILE_EXTENSIONS.has(ext) || TEXT_FILE_EXTENSIONS.has(base) || base.startsWith(".env");
|
|
24983
|
-
}
|
|
24984
|
-
function globToRegExp(pattern) {
|
|
24985
|
-
const normalized = normalizePathForMatch(pattern);
|
|
24986
|
-
let regex = "^";
|
|
24987
|
-
for (let i = 0; i < normalized.length; i++) {
|
|
24988
|
-
const char = normalized[i];
|
|
24989
|
-
const next = normalized[i + 1];
|
|
24990
|
-
if (char === "*") {
|
|
24991
|
-
if (next === "*") {
|
|
24992
|
-
const after = normalized[i + 2];
|
|
24993
|
-
if (after === "/") {
|
|
24994
|
-
regex += "(?:.*/)?";
|
|
24995
|
-
i += 2;
|
|
24996
|
-
} else {
|
|
24997
|
-
regex += ".*";
|
|
24998
|
-
i += 1;
|
|
24999
|
-
}
|
|
25000
|
-
} else {
|
|
25001
|
-
regex += "[^/]*";
|
|
25002
|
-
}
|
|
25003
|
-
} else if (char === "?") {
|
|
25004
|
-
regex += "[^/]";
|
|
25005
|
-
} else {
|
|
25006
|
-
regex += char.replace(/[|\\{}()[\]^$+?.]/g, "\\$&");
|
|
25007
|
-
}
|
|
25008
|
-
}
|
|
25009
|
-
regex += "$";
|
|
25010
|
-
return new RegExp(regex, "i");
|
|
25011
|
-
}
|
|
25012
|
-
function matchesGlob(relativePath, pattern) {
|
|
25013
|
-
return globToRegExp(pattern).test(normalizePathForMatch(relativePath));
|
|
25014
|
-
}
|
|
25015
|
-
async function walkFiles(rootDir, options = {}) {
|
|
25016
|
-
const files = [];
|
|
25017
|
-
const includeHidden = options.includeHidden ?? false;
|
|
25018
|
-
const maxFiles = options.maxFiles ?? 5e3;
|
|
25019
|
-
async function walk(currentDir) {
|
|
25020
|
-
if (files.length >= maxFiles) {
|
|
25021
|
-
return;
|
|
25022
|
-
}
|
|
25023
|
-
const entries = await import_fs_extra11.default.readdir(currentDir, { withFileTypes: true });
|
|
25024
|
-
for (const entry of entries) {
|
|
25025
|
-
if (files.length >= maxFiles) {
|
|
25026
|
-
return;
|
|
25027
|
-
}
|
|
25028
|
-
if (!includeHidden && entry.name.startsWith(".")) {
|
|
25029
|
-
continue;
|
|
25030
|
-
}
|
|
25031
|
-
if (entry.name === "node_modules" || entry.name === "dist" || entry.name === ".git") {
|
|
25032
|
-
continue;
|
|
25033
|
-
}
|
|
25034
|
-
const fullPath = import_path11.default.join(currentDir, entry.name);
|
|
25035
|
-
if (entry.isDirectory()) {
|
|
25036
|
-
await walk(fullPath);
|
|
25037
|
-
} else if (entry.isFile()) {
|
|
25038
|
-
files.push(fullPath);
|
|
25039
|
-
}
|
|
25040
|
-
}
|
|
25041
|
-
}
|
|
25042
|
-
await walk(rootDir);
|
|
25043
|
-
return files;
|
|
25044
|
-
}
|
|
25045
|
-
function formatJson(data) {
|
|
25046
|
-
return JSON.stringify(data, null, 2);
|
|
25047
|
-
}
|
|
25048
|
-
|
|
25049
|
-
// src/agent/tools/edit.ts
|
|
25080
|
+
var import_langchain7 = require("langchain");
|
|
25050
25081
|
async function editFileByReplace(filePath, oldString, newString, replaceAll = false) {
|
|
25051
25082
|
const absPath = resolveWorkspacePath(filePath);
|
|
25052
25083
|
if (!await import_fs_extra12.default.pathExists(absPath)) {
|
|
25053
|
-
|
|
25084
|
+
throw new Error(`\u6587\u4EF6\u4E0D\u5B58\u5728 ${absPath}`);
|
|
25054
25085
|
}
|
|
25055
25086
|
const content = await import_fs_extra12.default.readFile(absPath, "utf-8");
|
|
25056
25087
|
const matches = content.split(oldString).length - 1;
|
|
25057
25088
|
if (!oldString) {
|
|
25058
|
-
|
|
25089
|
+
throw new Error("oldString \u4E0D\u80FD\u4E3A\u7A7A");
|
|
25059
25090
|
}
|
|
25060
25091
|
if (matches === 0) {
|
|
25061
|
-
|
|
25092
|
+
throw new Error("\u672A\u627E\u5230 oldString\uFF0C\u8BF7\u5148\u8BFB\u53D6\u6587\u4EF6\u786E\u8BA4\u7CBE\u786E\u5185\u5BB9");
|
|
25062
25093
|
}
|
|
25063
25094
|
if (!replaceAll && matches > 1) {
|
|
25064
|
-
|
|
25095
|
+
throw new Error(`oldString \u5339\u914D\u5230 ${matches} \u5904\u3002\u8BF7\u63D0\u4F9B\u66F4\u591A\u4E0A\u4E0B\u6587\u4F7F\u5176\u552F\u4E00\uFF0C\u6216\u8BBE\u7F6E replaceAll=true`);
|
|
25065
25096
|
}
|
|
25066
25097
|
const nextContent = replaceAll ? content.split(oldString).join(newString) : content.replace(oldString, newString);
|
|
25067
25098
|
await import_fs_extra12.default.writeFile(absPath, nextContent, "utf-8");
|
|
25068
25099
|
return `\u5DF2\u7F16\u8F91\u6587\u4EF6: ${absPath}\uFF0C\u66FF\u6362 ${replaceAll ? matches : 1} \u5904`;
|
|
25069
25100
|
}
|
|
25070
|
-
var editFileTool = (0,
|
|
25071
|
-
async ({ filePath, oldString, newString, replaceAll }) => editFileByReplace(filePath, oldString, newString, replaceAll),
|
|
25101
|
+
var editFileTool = (0, import_langchain7.tool)(
|
|
25102
|
+
async ({ filePath, oldString, newString, replaceAll }) => safeTool(() => editFileByReplace(filePath, oldString, newString, replaceAll)),
|
|
25072
25103
|
{
|
|
25073
25104
|
name: "edit_file",
|
|
25074
25105
|
description: "\u7F16\u8F91\u5DF2\u6709\u6587\u672C\u6587\u4EF6\uFF1A\u4F7F\u7528\u7CBE\u786E oldString \u66FF\u6362\u4E3A newString\u3002\u9ED8\u8BA4\u8981\u6C42 oldString \u5728\u6587\u4EF6\u4E2D\u552F\u4E00\uFF0C\u4EE5\u907F\u514D\u8BEF\u6539\u3002",
|
|
@@ -25083,7 +25114,7 @@ var editFileTool = (0, import_langchain6.tool)(
|
|
|
25083
25114
|
|
|
25084
25115
|
// src/agent/tools/glob.ts
|
|
25085
25116
|
var import_path12 = __toESM(require("path"));
|
|
25086
|
-
var
|
|
25117
|
+
var import_langchain8 = require("langchain");
|
|
25087
25118
|
async function globFiles(pattern, cwd, maxResults = 200, includeHidden = false) {
|
|
25088
25119
|
const rootDir = resolveWorkspacePath(cwd || ".");
|
|
25089
25120
|
const allFiles = await walkFiles(rootDir, { includeHidden, maxFiles: Math.max(maxResults * 20, 1e3) });
|
|
@@ -25091,8 +25122,8 @@ async function globFiles(pattern, cwd, maxResults = 200, includeHidden = false)
|
|
|
25091
25122
|
const matches = allFiles.map((file2) => normalizePathForMatch(import_path12.default.relative(rootDir, file2))).filter((relativePath) => matchesGlob(relativePath, normalizedPattern)).slice(0, maxResults);
|
|
25092
25123
|
return truncateOutput(formatJson({ cwd: rootDir, pattern, count: matches.length, files: matches }));
|
|
25093
25124
|
}
|
|
25094
|
-
var globTool = (0,
|
|
25095
|
-
async ({ pattern, cwd, maxResults, includeHidden }) => globFiles(pattern, cwd, maxResults, includeHidden),
|
|
25125
|
+
var globTool = (0, import_langchain8.tool)(
|
|
25126
|
+
async ({ pattern, cwd, maxResults, includeHidden }) => safeTool(() => globFiles(pattern, cwd, maxResults, includeHidden)),
|
|
25096
25127
|
{
|
|
25097
25128
|
name: "glob_files",
|
|
25098
25129
|
description: "\u6309 glob \u6A21\u5F0F\u67E5\u627E\u5F53\u524D\u5DE5\u4F5C\u76EE\u5F55\u4E0B\u7684\u6587\u4EF6\uFF0C\u4F8B\u5982 **/*.ts\u3001src/**/*.tsx\u3002\u9ED8\u8BA4\u5FFD\u7565\u9690\u85CF\u76EE\u5F55\u3001node_modules\u3001dist\u3001.git\u3002",
|
|
@@ -25108,7 +25139,7 @@ var globTool = (0, import_langchain7.tool)(
|
|
|
25108
25139
|
// src/agent/tools/grep.ts
|
|
25109
25140
|
var import_fs_extra13 = __toESM(require("fs-extra"));
|
|
25110
25141
|
var import_path13 = __toESM(require("path"));
|
|
25111
|
-
var
|
|
25142
|
+
var import_langchain9 = require("langchain");
|
|
25112
25143
|
async function grepFiles(query, cwd, includePattern = "**/*", isRegexp = false, maxResults = 100, includeHidden = false) {
|
|
25113
25144
|
const rootDir = resolveWorkspacePath(cwd || ".");
|
|
25114
25145
|
const matcher = isRegexp ? new RegExp(query, "i") : null;
|
|
@@ -25133,25 +25164,35 @@ async function grepFiles(query, cwd, includePattern = "**/*", isRegexp = false,
|
|
|
25133
25164
|
}
|
|
25134
25165
|
return truncateOutput(formatJson({ cwd: rootDir, query, includePattern, count: matches.length, matches }));
|
|
25135
25166
|
}
|
|
25136
|
-
var grepTool = (0,
|
|
25137
|
-
async ({ query, cwd, includePattern, isRegexp, maxResults, includeHidden }) =>
|
|
25167
|
+
var grepTool = (0, import_langchain9.tool)(
|
|
25168
|
+
async ({ query, pattern, cwd, includePattern, isRegexp, maxResults, includeHidden }) => safeTool(() => {
|
|
25169
|
+
const searchQuery = query || pattern;
|
|
25170
|
+
if (!searchQuery) {
|
|
25171
|
+
throw new Error("query \u6216 pattern \u81F3\u5C11\u9700\u8981\u63D0\u4F9B\u4E00\u4E2A");
|
|
25172
|
+
}
|
|
25173
|
+
return grepFiles(searchQuery, cwd, includePattern, isRegexp, maxResults, includeHidden);
|
|
25174
|
+
}),
|
|
25138
25175
|
{
|
|
25139
25176
|
name: "grep_files",
|
|
25140
|
-
description: "\u5728\u6587\u672C\u6587\u4EF6\u4E2D\u641C\u7D22\u5185\u5BB9\uFF0C\u652F\u6301\u666E\u901A\u5B57\u7B26\u4E32\u6216\u6B63\u5219\u8868\u8FBE\u5F0F\uFF0C\u53EF\u7528 includePattern \u9650\u5B9A\u6587\u4EF6\u8303\u56F4\u3002",
|
|
25177
|
+
description: "\u5728\u6587\u672C\u6587\u4EF6\u4E2D\u641C\u7D22\u5185\u5BB9\uFF0C\u652F\u6301\u666E\u901A\u5B57\u7B26\u4E32\u6216\u6B63\u5219\u8868\u8FBE\u5F0F\uFF0C\u53EF\u7528 includePattern \u9650\u5B9A\u6587\u4EF6\u8303\u56F4\u3002query \u4E3A\u641C\u7D22\u5185\u5BB9\uFF1B\u517C\u5BB9 pattern \u4F5C\u4E3A query \u7684\u522B\u540D\u3002",
|
|
25141
25178
|
schema: external_exports.object({
|
|
25142
|
-
query: external_exports.string().describe("\u8981\u641C\u7D22\u7684\u5B57\u7B26\u4E32\u6216\u6B63\u5219\u8868\u8FBE\u5F0F"),
|
|
25179
|
+
query: external_exports.string().optional().describe("\u8981\u641C\u7D22\u7684\u5B57\u7B26\u4E32\u6216\u6B63\u5219\u8868\u8FBE\u5F0F"),
|
|
25180
|
+
pattern: external_exports.string().optional().describe("query \u7684\u517C\u5BB9\u522B\u540D\uFF0C\u8981\u641C\u7D22\u7684\u5B57\u7B26\u4E32\u6216\u6B63\u5219\u8868\u8FBE\u5F0F"),
|
|
25143
25181
|
cwd: external_exports.string().optional().describe("\u641C\u7D22\u6839\u76EE\u5F55\uFF0C\u9ED8\u8BA4\u5F53\u524D\u5DE5\u4F5C\u76EE\u5F55"),
|
|
25144
25182
|
includePattern: external_exports.string().default("**/*").describe("\u9650\u5B9A\u641C\u7D22\u6587\u4EF6\u7684 glob \u6A21\u5F0F\uFF0C\u4F8B\u5982 src/**/*.ts"),
|
|
25145
|
-
isRegexp: external_exports.boolean().default(false).describe("query \u662F\u5426\u4E3A\u6B63\u5219\u8868\u8FBE\u5F0F"),
|
|
25183
|
+
isRegexp: external_exports.boolean().default(false).describe("query/pattern \u662F\u5426\u4E3A\u6B63\u5219\u8868\u8FBE\u5F0F"),
|
|
25146
25184
|
maxResults: external_exports.number().default(100).describe("\u6700\u5927\u8FD4\u56DE\u5339\u914D\u6570\u91CF"),
|
|
25147
25185
|
includeHidden: external_exports.boolean().default(false).describe("\u662F\u5426\u5305\u542B\u9690\u85CF\u6587\u4EF6\u6216\u9690\u85CF\u76EE\u5F55")
|
|
25186
|
+
}).refine((input) => input.query || input.pattern, {
|
|
25187
|
+
message: "query \u6216 pattern \u81F3\u5C11\u9700\u8981\u63D0\u4F9B\u4E00\u4E2A",
|
|
25188
|
+
path: ["query"]
|
|
25148
25189
|
})
|
|
25149
25190
|
}
|
|
25150
25191
|
);
|
|
25151
25192
|
|
|
25152
25193
|
// src/agent/tools/question.ts
|
|
25153
25194
|
var import_inquirer2 = __toESM(require("inquirer"));
|
|
25154
|
-
var
|
|
25195
|
+
var import_langchain10 = require("langchain");
|
|
25155
25196
|
async function askQuestion(question, type = "input", choices = []) {
|
|
25156
25197
|
const promptType = type === "select" ? "list" : type;
|
|
25157
25198
|
const answer = await import_inquirer2.default.prompt([
|
|
@@ -25165,31 +25206,28 @@ async function askQuestion(question, type = "input", choices = []) {
|
|
|
25165
25206
|
const value = answer["value"];
|
|
25166
25207
|
return typeof value === "string" ? value : JSON.stringify(value);
|
|
25167
25208
|
}
|
|
25168
|
-
var questionTool = (0,
|
|
25169
|
-
|
|
25170
|
-
|
|
25171
|
-
|
|
25172
|
-
|
|
25173
|
-
|
|
25174
|
-
|
|
25175
|
-
|
|
25176
|
-
|
|
25177
|
-
})
|
|
25178
|
-
}
|
|
25179
|
-
);
|
|
25209
|
+
var questionTool = (0, import_langchain10.tool)(async ({ question, type, choices }) => safeTool(() => askQuestion(question, type, choices)), {
|
|
25210
|
+
name: "ask_question",
|
|
25211
|
+
description: "\u5F53\u7EE7\u7EED\u4EFB\u52A1\u5FC5\u987B\u83B7\u53D6\u7528\u6237\u6F84\u6E05\u3001\u786E\u8BA4\u6216\u9009\u62E9\u65F6\uFF0C\u5411\u7528\u6237\u53D1\u8D77\u547D\u4EE4\u884C\u63D0\u95EE\u5E76\u8FD4\u56DE\u7B54\u6848\u3002\u4E0D\u8981\u8BE2\u95EE\u5BC6\u7801\u3001\u5BC6\u94A5\u7B49\u654F\u611F\u4FE1\u606F\u3002",
|
|
25212
|
+
schema: external_exports.object({
|
|
25213
|
+
question: external_exports.string().describe("\u8981\u8BE2\u95EE\u7528\u6237\u7684\u95EE\u9898"),
|
|
25214
|
+
type: external_exports.enum(["input", "confirm", "select"]).default("input").describe("\u95EE\u9898\u7C7B\u578B\uFF1Ainput \u6587\u672C\u8F93\u5165\u3001confirm \u786E\u8BA4\u3001select \u5355\u9009"),
|
|
25215
|
+
choices: external_exports.array(external_exports.string()).default([]).describe("select \u5355\u9009\u9879\u5217\u8868\uFF1B\u975E select \u7C7B\u578B\u53EF\u4E3A\u7A7A")
|
|
25216
|
+
})
|
|
25217
|
+
});
|
|
25180
25218
|
|
|
25181
25219
|
// src/agent/tools/read.ts
|
|
25182
25220
|
var import_fs_extra14 = __toESM(require("fs-extra"));
|
|
25183
25221
|
var import_iconv_lite2 = __toESM(require("iconv-lite"));
|
|
25184
|
-
var
|
|
25222
|
+
var import_langchain11 = require("langchain");
|
|
25185
25223
|
async function readFile2(filePath, startLine = 1, endLine = -1, encoding) {
|
|
25186
25224
|
const absPath = resolveWorkspacePath(filePath);
|
|
25187
25225
|
if (!await import_fs_extra14.default.pathExists(absPath)) {
|
|
25188
|
-
|
|
25226
|
+
throw new Error(`\u6587\u4EF6\u4E0D\u5B58\u5728 ${absPath}`);
|
|
25189
25227
|
}
|
|
25190
25228
|
const stat = await import_fs_extra14.default.stat(absPath);
|
|
25191
25229
|
if (!stat.isFile()) {
|
|
25192
|
-
|
|
25230
|
+
throw new Error(`\u8DEF\u5F84\u4E0D\u662F\u6587\u4EF6 ${absPath}`);
|
|
25193
25231
|
}
|
|
25194
25232
|
const buffer = await import_fs_extra14.default.readFile(absPath);
|
|
25195
25233
|
const targetEncoding = encoding || getEncoding() || "utf-8";
|
|
@@ -25198,13 +25236,13 @@ async function readFile2(filePath, startLine = 1, endLine = -1, encoding) {
|
|
|
25198
25236
|
const safeStart = Math.max(1, startLine || 1);
|
|
25199
25237
|
const safeEnd = endLine && endLine > 0 ? Math.min(endLine, lines.length) : lines.length;
|
|
25200
25238
|
if (safeStart > safeEnd) {
|
|
25201
|
-
|
|
25239
|
+
throw new Error(`\u884C\u53F7\u8303\u56F4\u65E0\u6548\uFF0C\u6587\u4EF6\u5171 ${lines.length} \u884C`);
|
|
25202
25240
|
}
|
|
25203
25241
|
const selected = lines.slice(safeStart - 1, safeEnd).map((line, index) => `${safeStart + index}: ${line}`).join("\n");
|
|
25204
25242
|
return truncateOutput(formatJson({ filePath: absPath, totalLines: lines.length, startLine: safeStart, endLine: safeEnd, content: selected }));
|
|
25205
25243
|
}
|
|
25206
|
-
var readFileTool = (0,
|
|
25207
|
-
async ({ filePath, startLine, endLine, encoding }) => readFile2(filePath, startLine, endLine, encoding),
|
|
25244
|
+
var readFileTool = (0, import_langchain11.tool)(
|
|
25245
|
+
async ({ filePath, startLine, endLine, encoding }) => safeTool(() => readFile2(filePath, startLine, endLine, encoding)),
|
|
25208
25246
|
{
|
|
25209
25247
|
name: "read_file",
|
|
25210
25248
|
description: "\u8BFB\u53D6\u672C\u5730\u6587\u672C\u6587\u4EF6\u5185\u5BB9\u3002\u652F\u6301\u7EDD\u5BF9\u8DEF\u5F84\u6216\u76F8\u5BF9\u5F53\u524D\u5DE5\u4F5C\u76EE\u5F55\u7684\u8DEF\u5F84\uFF0C\u53EF\u6307\u5B9A\u8D77\u6B62\u884C\u53F7\uFF1B\u8FD4\u56DE\u5E26\u884C\u53F7\u7684\u5185\u5BB9\u3002",
|
|
@@ -25220,7 +25258,7 @@ var readFileTool = (0, import_langchain10.tool)(
|
|
|
25220
25258
|
// src/agent/tools/semanticMemory.ts
|
|
25221
25259
|
var import_fs_extra15 = __toESM(require("fs-extra"));
|
|
25222
25260
|
var import_path14 = __toESM(require("path"));
|
|
25223
|
-
var
|
|
25261
|
+
var import_langchain12 = require("langchain");
|
|
25224
25262
|
var DEFAULT_MEMORY_MARKDOWN = `# \u7528\u6237\u8BED\u4E49\u8BB0\u5FC6
|
|
25225
25263
|
|
|
25226
25264
|
> \u8BE5\u6587\u4EF6\u7531\u8BED\u4E49\u8BB0\u5FC6\u5DE5\u5177\u7EF4\u62A4\u3002\u4EC5\u8BB0\u5F55\u53EF\u957F\u671F\u590D\u7528\u7684\u7528\u6237\u4FE1\u606F\u3001\u4E60\u60EF\u548C\u504F\u597D\uFF1B\u4E0D\u8981\u8BB0\u5F55\u5BC6\u7801\u3001\u5BC6\u94A5\u3001\u4EE4\u724C\u7B49\u654F\u611F\u4FE1\u606F\u3002
|
|
@@ -25240,7 +25278,7 @@ function getMemoryFilePath(runtimeMemoryFilePath) {
|
|
|
25240
25278
|
}
|
|
25241
25279
|
async function readSemanticMemory(memoryFilePath) {
|
|
25242
25280
|
if (!memoryFilePath) {
|
|
25243
|
-
|
|
25281
|
+
throw new Error("\u5F53\u524D\u8FD0\u884C\u4E0A\u4E0B\u6587\u672A\u63D0\u4F9B memoryFilePath\uFF0C\u65E0\u6CD5\u8BFB\u53D6\u7528\u6237\u8BED\u4E49\u8BB0\u5FC6");
|
|
25244
25282
|
}
|
|
25245
25283
|
if (!await import_fs_extra15.default.pathExists(memoryFilePath)) {
|
|
25246
25284
|
return DEFAULT_MEMORY_MARKDOWN;
|
|
@@ -25249,27 +25287,23 @@ async function readSemanticMemory(memoryFilePath) {
|
|
|
25249
25287
|
}
|
|
25250
25288
|
async function updateSemanticMemory(memoryFilePath, content) {
|
|
25251
25289
|
if (!memoryFilePath) {
|
|
25252
|
-
|
|
25290
|
+
throw new Error("\u5F53\u524D\u8FD0\u884C\u4E0A\u4E0B\u6587\u672A\u63D0\u4F9B memoryFilePath\uFF0C\u65E0\u6CD5\u66F4\u65B0\u7528\u6237\u8BED\u4E49\u8BB0\u5FC6");
|
|
25253
25291
|
}
|
|
25254
25292
|
await import_fs_extra15.default.ensureDir(import_path14.default.dirname(memoryFilePath));
|
|
25255
25293
|
await import_fs_extra15.default.writeFile(memoryFilePath, `${content.trim()}
|
|
25256
25294
|
`, "utf-8");
|
|
25257
25295
|
return `\u5DF2\u66F4\u65B0\u7528\u6237\u8BED\u4E49\u8BB0\u5FC6: ${memoryFilePath}`;
|
|
25258
25296
|
}
|
|
25259
|
-
var readSemanticMemoryTool = (0,
|
|
25260
|
-
async (_input, runtime) =>
|
|
25261
|
-
return readSemanticMemory(getMemoryFilePath(runtime.context?.memoryFilePath));
|
|
25262
|
-
},
|
|
25297
|
+
var readSemanticMemoryTool = (0, import_langchain12.tool)(
|
|
25298
|
+
async (_input, runtime) => safeTool(() => readSemanticMemory(getMemoryFilePath(runtime.context?.memoryFilePath))),
|
|
25263
25299
|
{
|
|
25264
25300
|
name: "read_user_semantic_memory",
|
|
25265
25301
|
description: "\u8BFB\u53D6\u5F53\u524D\u7528\u6237\u8BED\u4E49\u8BB0\u5FC6 Markdown \u6587\u4EF6\u3002\u51C6\u5907\u65B0\u589E\u3001\u4FEE\u6B63\u6216\u5408\u5E76\u7528\u6237\u957F\u671F\u504F\u597D\u524D\uFF0C\u5E94\u5148\u8C03\u7528\u672C\u5DE5\u5177\u8BFB\u53D6\u73B0\u6709\u5185\u5BB9\uFF0C\u907F\u514D\u91CD\u590D\u548C\u8986\u76D6\u5DF2\u6709\u4FE1\u606F\u3002\u82E5\u6587\u4EF6\u4E0D\u5B58\u5728\uFF0C\u4F1A\u8FD4\u56DE\u63A8\u8350\u7684 Markdown \u6A21\u677F\u3002",
|
|
25266
25302
|
schema: external_exports.object({})
|
|
25267
25303
|
}
|
|
25268
25304
|
);
|
|
25269
|
-
var updateSemanticMemoryTool = (0,
|
|
25270
|
-
async ({ content }, runtime) =>
|
|
25271
|
-
return updateSemanticMemory(getMemoryFilePath(runtime.context?.memoryFilePath), content);
|
|
25272
|
-
},
|
|
25305
|
+
var updateSemanticMemoryTool = (0, import_langchain12.tool)(
|
|
25306
|
+
async ({ content }, runtime) => safeTool(() => updateSemanticMemory(getMemoryFilePath(runtime.context?.memoryFilePath), content)),
|
|
25273
25307
|
{
|
|
25274
25308
|
name: "update_user_semantic_memory",
|
|
25275
25309
|
description: "\u8986\u76D6\u66F4\u65B0\u5F53\u524D\u7528\u6237\u8BED\u4E49\u8BB0\u5FC6 Markdown \u6587\u4EF6\u3002content \u5FC5\u987B\u662F\u5B8C\u6574\u7684 Markdown \u6587\u4EF6\u5185\u5BB9\uFF0C\u800C\u4E0D\u662F\u7247\u6BB5\u3002\u4EC5\u8BB0\u5F55\u660E\u786E\u3001\u7A33\u5B9A\u3001\u53EF\u957F\u671F\u590D\u7528\u7684\u4FE1\u606F\uFF0C\u4F8B\u5982\u7528\u6237\u79F0\u547C\u3001\u64CD\u4F5C\u4E60\u60EF\u3001\u7F16\u7801\u4E60\u60EF\u3001\u4E2A\u4EBA\u504F\u597D\u7B49\uFF1B\u4E0D\u8981\u8BB0\u5F55\u4E00\u6B21\u6027\u4EFB\u52A1\u4FE1\u606F\u3001\u4E34\u65F6\u4E0A\u4E0B\u6587\u3001\u63A8\u6D4B\u5185\u5BB9\u3001\u5BC6\u7801\u3001\u5BC6\u94A5\u3001\u4EE4\u724C\u3001\u9690\u79C1\u654F\u611F\u4FE1\u606F\u3002\u66F4\u65B0\u524D\u5E94\u5148\u8C03\u7528 read_user_semantic_memory \u8BFB\u53D6\u73B0\u6709\u5185\u5BB9\uFF0C\u7136\u540E\u5728\u4FDD\u7559\u5DF2\u6709\u6709\u6548\u8BB0\u5FC6\u7684\u57FA\u7840\u4E0A\u5408\u5E76\u65B0\u4FE1\u606F\uFF0C\u6309 Markdown \u6807\u9898\u548C\u5217\u8868\u6574\u7406\uFF0C\u5E76\u81EA\u884C\u53BB\u91CD\u3002",
|
|
@@ -25328,21 +25362,21 @@ var TaskQueue = class {
|
|
|
25328
25362
|
};
|
|
25329
25363
|
|
|
25330
25364
|
// src/agent/tools/task.ts
|
|
25331
|
-
var
|
|
25365
|
+
var import_langchain13 = require("langchain");
|
|
25332
25366
|
function getCurrentTaskQueue(agentId) {
|
|
25333
25367
|
return agentId ? new TaskQueue(agentId) : null;
|
|
25334
25368
|
}
|
|
25335
25369
|
function manageTaskQueue(agentId, action, task, index) {
|
|
25336
25370
|
const queue = getCurrentTaskQueue(agentId);
|
|
25337
25371
|
if (!queue) {
|
|
25338
|
-
|
|
25372
|
+
throw new Error("\u672A\u627E\u5230\u5F53\u524D\u4F1A\u8BDD\uFF0C\u8BF7\u5148\u521B\u5EFA agent \u4F1A\u8BDD");
|
|
25339
25373
|
}
|
|
25340
25374
|
if (action === "list") {
|
|
25341
25375
|
return formatJson({ tasks: queue.loadTasks() });
|
|
25342
25376
|
}
|
|
25343
25377
|
if (action === "add") {
|
|
25344
25378
|
if (!task?.trim()) {
|
|
25345
|
-
|
|
25379
|
+
throw new Error("\u6DFB\u52A0\u4EFB\u52A1\u65F6 task \u4E0D\u80FD\u4E3A\u7A7A");
|
|
25346
25380
|
}
|
|
25347
25381
|
queue.pushTask(task.trim());
|
|
25348
25382
|
return `\u5DF2\u6DFB\u52A0\u4EFB\u52A1: ${task.trim()}`;
|
|
@@ -25351,7 +25385,7 @@ function manageTaskQueue(agentId, action, task, index) {
|
|
|
25351
25385
|
const tasks = queue.loadTasks();
|
|
25352
25386
|
const safeIndex = Number(index);
|
|
25353
25387
|
if (!Number.isInteger(safeIndex) || safeIndex < 1 || safeIndex > tasks.length) {
|
|
25354
|
-
|
|
25388
|
+
throw new Error(`\u4EFB\u52A1\u5E8F\u53F7\u65E0\u6548\uFF0C\u5F53\u524D\u4EFB\u52A1\u6570 ${tasks.length}`);
|
|
25355
25389
|
}
|
|
25356
25390
|
const removed = tasks[safeIndex - 1];
|
|
25357
25391
|
queue.delTask(safeIndex - 1);
|
|
@@ -25360,11 +25394,11 @@ function manageTaskQueue(agentId, action, task, index) {
|
|
|
25360
25394
|
queue.clearTasks();
|
|
25361
25395
|
return "\u5DF2\u6E05\u7A7A\u4EFB\u52A1\u961F\u5217";
|
|
25362
25396
|
}
|
|
25363
|
-
var taskTool = (0,
|
|
25364
|
-
async ({ action, task, index }, runtime) => {
|
|
25397
|
+
var taskTool = (0, import_langchain13.tool)(
|
|
25398
|
+
async ({ action, task, index }, runtime) => safeTool(() => {
|
|
25365
25399
|
const agentId = runtime.context?.agentId || getAgentId();
|
|
25366
25400
|
return manageTaskQueue(agentId, action, task, index);
|
|
25367
|
-
},
|
|
25401
|
+
}),
|
|
25368
25402
|
{
|
|
25369
25403
|
name: "manage_task_queue",
|
|
25370
25404
|
description: "\u7BA1\u7406\u5F53\u524D agent \u4F1A\u8BDD\u4EFB\u52A1\u961F\u5217\uFF1A\u67E5\u770B\u3001\u6DFB\u52A0\u540E\u7EED\u4EFB\u52A1\u3001\u5220\u9664\u6307\u5B9A\u4EFB\u52A1\u6216\u6E05\u7A7A\u4EFB\u52A1\u3002\u6DFB\u52A0\u7684\u4EFB\u52A1\u4F1A\u5728\u5F53\u524D\u4EFB\u52A1\u7ED3\u675F\u540E\u7EE7\u7EED\u6267\u884C\u3002",
|
|
@@ -25377,7 +25411,7 @@ var taskTool = (0, import_langchain12.tool)(
|
|
|
25377
25411
|
);
|
|
25378
25412
|
|
|
25379
25413
|
// src/agent/tools/webfetch.ts
|
|
25380
|
-
var
|
|
25414
|
+
var import_langchain14 = require("langchain");
|
|
25381
25415
|
async function webFetch(url2, timeout = 3e4, maxLength = 6e4) {
|
|
25382
25416
|
const controller = new AbortController();
|
|
25383
25417
|
const timer = setTimeout(() => controller.abort(), timeout);
|
|
@@ -25406,35 +25440,29 @@ async function webFetch(url2, timeout = 3e4, maxLength = 6e4) {
|
|
|
25406
25440
|
),
|
|
25407
25441
|
maxLength
|
|
25408
25442
|
);
|
|
25409
|
-
} catch (error51) {
|
|
25410
|
-
const message = error51 instanceof Error ? error51.message : String(error51);
|
|
25411
|
-
return `Web fetch error: ${message}`;
|
|
25412
25443
|
} finally {
|
|
25413
25444
|
clearTimeout(timer);
|
|
25414
25445
|
}
|
|
25415
25446
|
}
|
|
25416
|
-
var webFetchTool = (0,
|
|
25417
|
-
|
|
25418
|
-
|
|
25419
|
-
|
|
25420
|
-
|
|
25421
|
-
|
|
25422
|
-
|
|
25423
|
-
|
|
25424
|
-
|
|
25425
|
-
})
|
|
25426
|
-
}
|
|
25427
|
-
);
|
|
25447
|
+
var webFetchTool = (0, import_langchain14.tool)(async ({ url: url2, timeout, maxLength }) => safeTool(() => webFetch(url2, timeout, maxLength)), {
|
|
25448
|
+
name: "web_fetch",
|
|
25449
|
+
description: "\u901A\u8FC7 HTTP GET \u83B7\u53D6\u7F51\u9875\u3001\u63A5\u53E3\u6216\u8FDC\u7A0B\u6587\u672C\u5185\u5BB9\uFF0C\u5E76\u8FD4\u56DE\u72B6\u6001\u7801\u3001content-type \u548C\u54CD\u5E94\u6B63\u6587\u3002",
|
|
25450
|
+
schema: external_exports.object({
|
|
25451
|
+
url: external_exports.string().url().describe("\u8981\u8BF7\u6C42\u7684\u5B8C\u6574 URL\uFF0C\u4F8B\u5982 https://example.com"),
|
|
25452
|
+
timeout: external_exports.number().default(3e4).describe("\u8BF7\u6C42\u8D85\u65F6\u65F6\u95F4\uFF08\u6BEB\u79D2\uFF09"),
|
|
25453
|
+
maxLength: external_exports.number().default(6e4).describe("\u6700\u5927\u8FD4\u56DE\u5B57\u7B26\u6570\uFF0C\u8D85\u51FA\u4F1A\u622A\u65AD")
|
|
25454
|
+
})
|
|
25455
|
+
});
|
|
25428
25456
|
|
|
25429
25457
|
// src/agent/tools/write.ts
|
|
25430
25458
|
var import_fs_extra17 = __toESM(require("fs-extra"));
|
|
25431
25459
|
var import_path15 = __toESM(require("path"));
|
|
25432
|
-
var
|
|
25460
|
+
var import_langchain15 = require("langchain");
|
|
25433
25461
|
async function writeFile2(filePath, content, mode = "overwrite") {
|
|
25434
25462
|
const absPath = resolveWorkspacePath(filePath);
|
|
25435
25463
|
const exists = await import_fs_extra17.default.pathExists(absPath);
|
|
25436
25464
|
if (mode === "create" && exists) {
|
|
25437
|
-
|
|
25465
|
+
throw new Error(`\u6587\u4EF6\u5DF2\u5B58\u5728\uFF0Ccreate \u6A21\u5F0F\u4E0D\u4F1A\u8986\u76D6 ${absPath}`);
|
|
25438
25466
|
}
|
|
25439
25467
|
await import_fs_extra17.default.ensureDir(import_path15.default.dirname(absPath));
|
|
25440
25468
|
if (mode === "append") {
|
|
@@ -25444,18 +25472,15 @@ async function writeFile2(filePath, content, mode = "overwrite") {
|
|
|
25444
25472
|
await import_fs_extra17.default.writeFile(absPath, content, "utf-8");
|
|
25445
25473
|
return `${exists ? "\u5DF2\u8986\u76D6\u5199\u5165\u6587\u4EF6" : "\u5DF2\u521B\u5EFA\u6587\u4EF6"}: ${absPath}`;
|
|
25446
25474
|
}
|
|
25447
|
-
var writeFileTool = (0,
|
|
25448
|
-
|
|
25449
|
-
|
|
25450
|
-
|
|
25451
|
-
|
|
25452
|
-
|
|
25453
|
-
|
|
25454
|
-
|
|
25455
|
-
|
|
25456
|
-
})
|
|
25457
|
-
}
|
|
25458
|
-
);
|
|
25475
|
+
var writeFileTool = (0, import_langchain15.tool)(async ({ filePath, content, mode }) => safeTool(() => writeFile2(filePath, content, mode)), {
|
|
25476
|
+
name: "write_file",
|
|
25477
|
+
description: "\u5411\u672C\u5730\u6587\u4EF6\u5199\u5165\u6587\u672C\u5185\u5BB9\u3002\u53EF\u521B\u5EFA\u65B0\u6587\u4EF6\u3001\u8986\u76D6\u5DF2\u6709\u6587\u4EF6\u6216\u8FFD\u52A0\u5185\u5BB9\uFF0C\u4F1A\u81EA\u52A8\u521B\u5EFA\u7236\u76EE\u5F55\u3002",
|
|
25478
|
+
schema: external_exports.object({
|
|
25479
|
+
filePath: external_exports.string().describe("\u76EE\u6807\u6587\u4EF6\u8DEF\u5F84\uFF0C\u652F\u6301\u7EDD\u5BF9\u8DEF\u5F84\u6216\u76F8\u5BF9\u5F53\u524D\u5DE5\u4F5C\u76EE\u5F55\u7684\u8DEF\u5F84"),
|
|
25480
|
+
content: external_exports.string().describe("\u8981\u5199\u5165\u7684\u5B8C\u6574\u6587\u672C\u5185\u5BB9"),
|
|
25481
|
+
mode: external_exports.enum(["overwrite", "append", "create"]).default("overwrite").describe("\u5199\u5165\u6A21\u5F0F\uFF1Aoverwrite \u8986\u76D6\u3001append \u8FFD\u52A0\u3001create \u4EC5\u65B0\u5EFA")
|
|
25482
|
+
})
|
|
25483
|
+
});
|
|
25459
25484
|
|
|
25460
25485
|
// src/agent/tools/index.ts
|
|
25461
25486
|
var builtinTools = [
|
|
@@ -25529,19 +25554,19 @@ var AIAgent = class extends import_eventemitter_super.EventEmitterSuper {
|
|
|
25529
25554
|
memoryFilePath: external_exports.string().optional(),
|
|
25530
25555
|
agentId: external_exports.string().optional()
|
|
25531
25556
|
});
|
|
25532
|
-
const agent = (0,
|
|
25557
|
+
const agent = (0, import_langchain16.createAgent)({
|
|
25533
25558
|
model,
|
|
25534
25559
|
checkpointer,
|
|
25535
25560
|
tools: this.tools,
|
|
25536
25561
|
contextSchema,
|
|
25537
25562
|
middleware: [
|
|
25538
25563
|
createAgentEventMiddleware(this),
|
|
25539
|
-
(0,
|
|
25564
|
+
(0, import_langchain16.summarizationMiddleware)({
|
|
25540
25565
|
model,
|
|
25541
25566
|
trigger: { tokens: this.opt.modelOpt.maxContextLength || 1e5 },
|
|
25542
25567
|
keep: { messages: 50 }
|
|
25543
25568
|
}),
|
|
25544
|
-
(0,
|
|
25569
|
+
(0, import_langchain16.humanInTheLoopMiddleware)({
|
|
25545
25570
|
interruptOn: {
|
|
25546
25571
|
install_package: {
|
|
25547
25572
|
allowedDecisions: ["approve", "reject"]
|
|
@@ -25549,7 +25574,7 @@ var AIAgent = class extends import_eventemitter_super.EventEmitterSuper {
|
|
|
25549
25574
|
readEmailTool: false
|
|
25550
25575
|
}
|
|
25551
25576
|
}),
|
|
25552
|
-
(0,
|
|
25577
|
+
(0, import_langchain16.todoListMiddleware)(),
|
|
25553
25578
|
(0, import_deepagents.createPatchToolCallsMiddleware)(),
|
|
25554
25579
|
(0, import_deepagents.createSubAgentMiddleware)({
|
|
25555
25580
|
defaultModel: model,
|
|
@@ -25572,7 +25597,7 @@ var AIAgent = class extends import_eventemitter_super.EventEmitterSuper {
|
|
|
25572
25597
|
this.initEvents();
|
|
25573
25598
|
}
|
|
25574
25599
|
async execute(input) {
|
|
25575
|
-
const humanMessage = new
|
|
25600
|
+
const humanMessage = new import_langchain16.HumanMessage(input);
|
|
25576
25601
|
this.messages.push(humanMessage);
|
|
25577
25602
|
const stream = await this.agent.stream(
|
|
25578
25603
|
{ messages: this.messages },
|