deepfish-ai 1.0.22 → 1.0.23
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/package.json +1 -1
- package/src/AgentRobot/AgentRobotFactory/MainAgentRobot.js +4 -7
- package/src/AgentRobot/AgentRobotFactory/SubAgentRobot.js +3 -8
- package/src/AgentRobot/AgentRobotFactory/SubSkillAgentRobot.js +3 -8
- package/src/AgentRobot/BaseAgentRobot/Brain.js +8 -8
- package/src/AgentRobot/BaseAgentRobot/Hand.js +3 -3
- package/src/AgentRobot/BaseAgentRobot/index.js +25 -89
- package/src/AgentRobot/BaseAgentRobot/lazy-tools/doc-transform.js +1 -0
- package/src/AgentRobot/BaseAgentRobot/lazy-tools/docx.js +1 -0
- package/src/AgentRobot/BaseAgentRobot/lazy-tools/embedding.js +1 -0
- package/src/AgentRobot/BaseAgentRobot/lazy-tools/img.js +1 -0
- package/src/AgentRobot/BaseAgentRobot/lazy-tools/pdf.js +1 -0
- package/src/AgentRobot/BaseAgentRobot/lazy-tools/pptx.js +1 -0
- package/src/AgentRobot/BaseAgentRobot/lazy-tools/xlsx.js +1 -0
- package/src/AgentRobot/BaseAgentRobot/tools/BaseTools.js +1 -0
- package/src/AgentRobot/BaseAgentRobot/tools/CreateAgentTools.js +3 -2
- package/src/AgentRobot/BaseAgentRobot/tools/FileTools.js +1 -0
- package/src/AgentRobot/BaseAgentRobot/tools/GenerateTools.js +2 -1
- package/src/AgentRobot/BaseAgentRobot/tools/InquirerTools.js +1 -0
- package/src/AgentRobot/BaseAgentRobot/tools/SystemTools.js +3 -4
- package/src/AgentRobot/BaseAgentRobot/tools/TaskTools.js +1 -0
- package/src/AgentRobot/BaseAgentRobot/tools/TestTools.js +1 -0
- package/src/AgentRobot/BaseAgentRobot/tools/UserTool.js +87 -0
- package/src/AgentRobot/BaseAgentRobot/tools/WebTools.js +1 -0
- package/src/AgentRobot/BaseAgentRobot/utils/AIRequest.js +9 -2
- package/src/AgentRobot/BaseAgentRobot/utils/AIToolManager.js +128 -0
- package/src/cli/DefaultConfig.js +1 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "deepfish-ai",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.23",
|
|
4
4
|
"description": "This is an AI-driven command-line tool built on Node.js, equipped with AI agent and workflow capabilities. It is compatible with a wide range of AI models, can convert natural language into cross-system terminal and file operation commands, and features high extensibility. It supports complex tasks such as translation, content creation, and format conversion, while allowing custom extensions to be automatically generated via AI.",
|
|
5
5
|
"main": "src/index.js",
|
|
6
6
|
"type": "commonjs",
|
|
@@ -18,9 +18,12 @@ class MainAgentRobot extends BaseAgentRobot {
|
|
|
18
18
|
_initFiles(opt) {
|
|
19
19
|
this.workspace = opt.workspace || process.cwd() // 工作空间,目录
|
|
20
20
|
this.basespace = opt.basespace || path.join(os.homedir(), '.deepfish-ai') // 记忆空间,目录
|
|
21
|
+
this.userspace = path.join(this.basespace, 'user-info') // 用户空间,目录
|
|
22
|
+
this.userInfoFilePath = path.join(this.userspace, 'user.md')
|
|
21
23
|
this.memorySpace = path.join(this.basespace, 'memory') // 记忆空间,目录
|
|
22
24
|
this.agentRecordFilePath = path.join(this.memorySpace, 'agentRecord.json')
|
|
23
25
|
fs.ensureDirSync(this.memorySpace)
|
|
26
|
+
fs.ensureDirSync(this.userspace)
|
|
24
27
|
// 查看agentRecord.json文件是否存在,不存在则创建
|
|
25
28
|
if (!fs.pathExistsSync(this.agentRecordFilePath)) {
|
|
26
29
|
fs.writeJsonSync(this.agentRecordFilePath, [], { spaces: 2 })
|
|
@@ -68,12 +71,6 @@ class MainAgentRobot extends BaseAgentRobot {
|
|
|
68
71
|
fs.writeJsonSync(this.agentRecordFilePath, agentRecord, { spaces: 2 })
|
|
69
72
|
this.logger = new Logger(this) // 初始化日志系统
|
|
70
73
|
this.logger.clearAllLogs()
|
|
71
|
-
this.toolCollection = AttachmentToolScanner.getToolCollection(
|
|
72
|
-
this.workspace,
|
|
73
|
-
) // 加载工具集合
|
|
74
|
-
this.clawSkillCollection = AttachmentToolScanner.getClawSkillCollection(
|
|
75
|
-
this.basespace,
|
|
76
|
-
) // 加载Claw技能集合
|
|
77
74
|
}
|
|
78
75
|
|
|
79
76
|
_getDefaultSystemPrompt(opt) {
|
|
@@ -82,7 +79,7 @@ class MainAgentRobot extends BaseAgentRobot {
|
|
|
82
79
|
${systemPrompt}
|
|
83
80
|
### 工具调用
|
|
84
81
|
对于复杂的任务,先从可以使用的Skills中查找并使用合适的Skill,如果没有合适的Skill,再使用内置工具函数,使用时请严格按照工具函数的调用方式进行调用。
|
|
85
|
-
${AttachmentToolScanner.getAttachToolPrompt(this.toolCollection, this.clawSkillCollection)}
|
|
82
|
+
${AttachmentToolScanner.getAttachToolPrompt(this.toolManager.toolCollection, this.toolManager.clawSkillCollection)}
|
|
86
83
|
`
|
|
87
84
|
}
|
|
88
85
|
}
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
const path = require('path')
|
|
2
2
|
const os = require('os')
|
|
3
|
-
const fs = require('fs-extra')
|
|
4
3
|
const BaseAgentRobot = require('../BaseAgentRobot/index.js')
|
|
5
4
|
const Logger = require('../BaseAgentRobot/Logger.js')
|
|
6
5
|
const {AttachmentToolScanner} = require('../BaseAgentRobot/utils/AttachmentToolScanner.js')
|
|
@@ -20,6 +19,8 @@ class SubAgentRobot extends BaseAgentRobot {
|
|
|
20
19
|
this.attachTools = opt.attachTools || []
|
|
21
20
|
this.workspace = opt.workspace || process.cwd() // 工作空间,目录
|
|
22
21
|
this.basespace = opt.basespace || path.join(os.homedir(), '.deepfish-ai') // 记忆空间,目录
|
|
22
|
+
this.userspace = path.join(this.basespace, 'user-info') // 用户空间,目录
|
|
23
|
+
this.userInfoFilePath = path.join(this.userspace, 'user.md')
|
|
23
24
|
this.memorySpace = path.join(this.basespace, 'memory') // 记忆空间,目录
|
|
24
25
|
this.agentRecordFilePath = path.join(this.memorySpace, 'agentRecord.json')
|
|
25
26
|
this.agentSpace = path.join(this.memorySpace, this.root.id) // Agent空间,目录
|
|
@@ -28,12 +29,6 @@ class SubAgentRobot extends BaseAgentRobot {
|
|
|
28
29
|
this.logger = new Logger(this) // 初始化日志系统
|
|
29
30
|
this.agentTree = new AgentTree(this)
|
|
30
31
|
this.agentTree.init()
|
|
31
|
-
this.toolCollection = AttachmentToolScanner.getToolCollection(
|
|
32
|
-
this.workspace,
|
|
33
|
-
) // 加载工具集合
|
|
34
|
-
this.clawSkillCollection = AttachmentToolScanner.getClawSkillCollection(
|
|
35
|
-
this.basespace,
|
|
36
|
-
) // 加载Claw技能集合
|
|
37
32
|
}
|
|
38
33
|
|
|
39
34
|
_getDefaultSystemPrompt(opt) {
|
|
@@ -42,7 +37,7 @@ class SubAgentRobot extends BaseAgentRobot {
|
|
|
42
37
|
${systemPrompt}
|
|
43
38
|
### 工具调用
|
|
44
39
|
对于复杂的任务,先从可以使用的Skills中查找并使用合适的Skill,如果没有合适的Skill,再使用内置工具函数,使用时请严格按照工具函数的调用方式进行调用。
|
|
45
|
-
${AttachmentToolScanner.getAttachToolPrompt(this.toolCollection, this.clawSkillCollection)}
|
|
40
|
+
${AttachmentToolScanner.getAttachToolPrompt(this.toolManager.toolCollection, this.toolManager.clawSkillCollection)}
|
|
46
41
|
`
|
|
47
42
|
}
|
|
48
43
|
}
|
|
@@ -20,6 +20,8 @@ class SubSkillAgentRobot extends BaseAgentRobot {
|
|
|
20
20
|
this.attachTools = opt.attachTools || []
|
|
21
21
|
this.workspace = opt.workspace || process.cwd() // 工作空间,目录
|
|
22
22
|
this.basespace = opt.basespace || path.join(os.homedir(), '.deepfish-ai') // 记忆空间,目录
|
|
23
|
+
this.userspace = path.join(this.basespace, 'user-info') // 用户空间,目录
|
|
24
|
+
this.userInfoFilePath = path.join(this.userspace, 'user.md')
|
|
23
25
|
this.memorySpace = path.join(this.basespace, 'memory') // 记忆空间,目录
|
|
24
26
|
this.agentRecordFilePath = path.join(this.memorySpace, 'agentRecord.json')
|
|
25
27
|
this.agentSpace = path.join(this.memorySpace, this.root.id) // Agent空间,目录
|
|
@@ -29,20 +31,13 @@ class SubSkillAgentRobot extends BaseAgentRobot {
|
|
|
29
31
|
this.logger = new Logger(this) // 初始化日志系统
|
|
30
32
|
this.agentTree = new AgentTree(this)
|
|
31
33
|
this.agentTree.init()
|
|
32
|
-
|
|
33
|
-
this.toolCollection = AttachmentToolScanner.getToolCollection(
|
|
34
|
-
this.workspace,
|
|
35
|
-
) // 加载工具集合
|
|
36
|
-
this.clawSkillCollection = AttachmentToolScanner.getClawSkillCollection(
|
|
37
|
-
this.basespace,
|
|
38
|
-
) // 加载Claw技能集合
|
|
39
34
|
}
|
|
40
35
|
|
|
41
36
|
_getDefaultSystemPrompt(opt) {
|
|
42
37
|
const clawSkills = opt.clawSkills || []
|
|
43
38
|
let systemPrompt = super._getDefaultSystemPrompt(opt)
|
|
44
39
|
systemPrompt =
|
|
45
|
-
systemPrompt + '\n' + AttachmentToolScanner.getClawSkillPrompt(clawSkills, this.toolCollection, this.clawSkillCollection)
|
|
40
|
+
systemPrompt + '\n' + AttachmentToolScanner.getClawSkillPrompt(clawSkills, this.toolManager.toolCollection, this.toolManager.clawSkillCollection)
|
|
46
41
|
return systemPrompt
|
|
47
42
|
}
|
|
48
43
|
}
|
|
@@ -70,10 +70,17 @@ class Brain extends EventEmitterSuper {
|
|
|
70
70
|
if (maxIterations === -1) {
|
|
71
71
|
maxIterations = Infinity
|
|
72
72
|
}
|
|
73
|
-
const skillDescriptions = this.agentRobot.
|
|
73
|
+
const skillDescriptions = this.agentRobot.toolManager.descriptions
|
|
74
74
|
this.emit(BrainEvent.THINK_BEFORE, messages)
|
|
75
75
|
while (maxIterations-- > 0) {
|
|
76
76
|
try {
|
|
77
|
+
// 更新系统提示词
|
|
78
|
+
if (messages[0].role === 'system') {
|
|
79
|
+
messages[0] = {
|
|
80
|
+
role: 'system',
|
|
81
|
+
content: this.agentRobot.systemPrompt,
|
|
82
|
+
}
|
|
83
|
+
}
|
|
77
84
|
// 压缩上下文
|
|
78
85
|
await this.messageCompresser.compress(messages)
|
|
79
86
|
const { message, content, tool_calls } = await thinkByTool(
|
|
@@ -180,13 +187,6 @@ class Brain extends EventEmitterSuper {
|
|
|
180
187
|
}
|
|
181
188
|
|
|
182
189
|
_initMessages(messages) {
|
|
183
|
-
let firstMessage = messages[0]
|
|
184
|
-
if (firstMessage.role === 'system') {
|
|
185
|
-
messages[0] = {
|
|
186
|
-
role: 'system',
|
|
187
|
-
content: this.agentRobot.systemPrompt,
|
|
188
|
-
}
|
|
189
|
-
}
|
|
190
190
|
let lastMessage = messages[messages.length - 1]
|
|
191
191
|
while (
|
|
192
192
|
messages.length > 1 &&
|
|
@@ -12,7 +12,7 @@ class Hand extends EventEmitterSuper {
|
|
|
12
12
|
super()
|
|
13
13
|
this.agentRobot = agentRobot
|
|
14
14
|
this.maxBlockFileSize = agentRobot.opt.maxBlockFileSize || 20 // KB
|
|
15
|
-
this.tools = agentRobot.
|
|
15
|
+
this.tools = agentRobot.toolManager.functions
|
|
16
16
|
}
|
|
17
17
|
|
|
18
18
|
_parseToolCalls(tool_call) {
|
|
@@ -57,7 +57,7 @@ class Hand extends EventEmitterSuper {
|
|
|
57
57
|
}
|
|
58
58
|
|
|
59
59
|
_getRequiredParamNames(funcName) {
|
|
60
|
-
const descriptions = this.agentRobot.
|
|
60
|
+
const descriptions = this.agentRobot.toolManager.descriptions
|
|
61
61
|
const current = descriptions.find((item) => item?.function?.name === funcName)
|
|
62
62
|
return current?.function?.parameters?.required || []
|
|
63
63
|
}
|
|
@@ -89,7 +89,7 @@ class Hand extends EventEmitterSuper {
|
|
|
89
89
|
toolContent = {
|
|
90
90
|
truncated: true,
|
|
91
91
|
message:
|
|
92
|
-
'文件内容过大,请使用executeJSCode
|
|
92
|
+
'文件内容过大,请使用executeJSCode工具编写脚本分块读取和处理文件,避免一次性读取整个文件内容到对话中。如果不是本地文件,建议创建或下载成本地文件后再进行分块读取。',
|
|
93
93
|
preview: toolContent.substring(0, 1000) + '...',
|
|
94
94
|
}
|
|
95
95
|
} else {
|
|
@@ -1,17 +1,10 @@
|
|
|
1
1
|
const path = require('path')
|
|
2
2
|
const os = require('os')
|
|
3
|
-
const fs = require('fs-extra')
|
|
4
|
-
const lodash = require('lodash')
|
|
5
3
|
const { Brain } = require('./Brain.js')
|
|
6
4
|
const BrainEvent = require('./BrainEvent.js')
|
|
7
5
|
const ScreenPrinter = require('./ScreenPrinter.js')
|
|
8
6
|
const { HandEvent, Hand } = require('./Hand.js')
|
|
9
|
-
const
|
|
10
|
-
const axios = require('axios')
|
|
11
|
-
const echarts = require('echarts')
|
|
12
|
-
const canvas = require('canvas')
|
|
13
|
-
const cheerio = require('cheerio')
|
|
14
|
-
const puppeteer = require('puppeteer')
|
|
7
|
+
const AIToolManager = require('./utils/AIToolManager.js')
|
|
15
8
|
|
|
16
9
|
class BaseAgentRobot {
|
|
17
10
|
id = '' // Agentid
|
|
@@ -19,8 +12,7 @@ class BaseAgentRobot {
|
|
|
19
12
|
|
|
20
13
|
brain = null // 大脑,负责思考、记忆、决策
|
|
21
14
|
hand = null // 手,负责使用工具
|
|
22
|
-
|
|
23
|
-
attachTools = null // 附加工具, Agent后续安装的工具函数
|
|
15
|
+
|
|
24
16
|
heart = null // 心脏,负责心跳、连接
|
|
25
17
|
sender = null // 发送器,负责发送消息
|
|
26
18
|
receiver = null // 接收器,负责接收消息
|
|
@@ -34,12 +26,14 @@ class BaseAgentRobot {
|
|
|
34
26
|
|
|
35
27
|
workspace = null
|
|
36
28
|
basespace = null
|
|
29
|
+
userspace = null
|
|
37
30
|
memorySpace = null
|
|
38
31
|
agentRecordFilePath = null
|
|
39
32
|
agentSpace = null
|
|
40
33
|
agentTree = null
|
|
41
34
|
memoryFilePath = null
|
|
42
35
|
logDirPath = null
|
|
36
|
+
toolManager = null
|
|
43
37
|
|
|
44
38
|
constructor(
|
|
45
39
|
opt = {
|
|
@@ -54,6 +48,7 @@ class BaseAgentRobot {
|
|
|
54
48
|
maxBlockFileSize: 20, // 大文件分块阈值,单位KB;超过该大小的文件需要分块处理
|
|
55
49
|
systemPrompt: '', // 系统提示语
|
|
56
50
|
encoding: 'auto', // 命令行编码格式, 可设置为utf-8、gbk等, 也可以设置成auto或空值自动判断
|
|
51
|
+
isThinkPrint: true, // 是否打印思考过程
|
|
57
52
|
aiConfig: {
|
|
58
53
|
name: 'deepseek',
|
|
59
54
|
type: 'deepseek',
|
|
@@ -73,9 +68,7 @@ class BaseAgentRobot {
|
|
|
73
68
|
this.name = opt.name || 'AgentRobot'
|
|
74
69
|
this.screenPrinter = new ScreenPrinter() // 屏幕打印机
|
|
75
70
|
this._initFiles(opt) // 初始化文件
|
|
76
|
-
|
|
77
|
-
this.originalTools = this._getOriginalTools() // 天赋技能
|
|
78
|
-
this.attachTools = opt.attachTools || [] // 附加工具, Agent后续安装的工具函数
|
|
71
|
+
this.toolManager = new AIToolManager(this)
|
|
79
72
|
this.systemPrompt = opt.systemPrompt || this._getDefaultSystemPrompt(opt) // 系统提示语
|
|
80
73
|
this.brain = new Brain(this) // 初始化大脑
|
|
81
74
|
this.hand = new Hand(this) // 初始化手
|
|
@@ -87,10 +80,11 @@ class BaseAgentRobot {
|
|
|
87
80
|
|
|
88
81
|
_initEvents() {
|
|
89
82
|
const aiConfig = this.opt.aiConfig
|
|
83
|
+
const isThinkPrint = this.opt.isThinkPrint
|
|
90
84
|
let stopLoading = null
|
|
91
85
|
this.brain.on(BrainEvent.THINK_BEFORE, () => {})
|
|
92
86
|
this.brain.on(BrainEvent.SUB_THINK_BEFORE, (messages) => {
|
|
93
|
-
if (!aiConfig.stream) {
|
|
87
|
+
if (!aiConfig.stream || !isThinkPrint) {
|
|
94
88
|
if (stopLoading) {
|
|
95
89
|
stopLoading('I have finished thinking.')
|
|
96
90
|
}
|
|
@@ -98,27 +92,27 @@ class BaseAgentRobot {
|
|
|
98
92
|
}
|
|
99
93
|
})
|
|
100
94
|
this.brain.on(BrainEvent.SUB_THINK_AFTER, (messages) => {
|
|
101
|
-
if (!aiConfig.stream && stopLoading) {
|
|
95
|
+
if ((!aiConfig.stream && stopLoading) || !isThinkPrint) {
|
|
102
96
|
stopLoading('I have finished thinking.')
|
|
103
97
|
stopLoading = null
|
|
104
|
-
const lastMessage = messages[messages.length - 1]
|
|
105
|
-
this.screenPrinter.logInfo(lastMessage?.content)
|
|
98
|
+
// const lastMessage = messages[messages.length - 1]
|
|
99
|
+
// this.screenPrinter.logInfo(lastMessage?.content)
|
|
106
100
|
}
|
|
107
101
|
})
|
|
108
102
|
this.brain.on(BrainEvent.SUB_STREAM_THINK_OUTPUT, (messages, output) => {
|
|
109
|
-
this.screenPrinter.streamOutput(output, '#47854a')
|
|
103
|
+
isThinkPrint && this.screenPrinter.streamOutput(output, '#47854a')
|
|
110
104
|
})
|
|
111
105
|
this.brain.on(BrainEvent.SUB_STREAM_CONTENT_OUTPUT, (messages, content) => {
|
|
112
|
-
this.screenPrinter.streamOutput(content, '#c2a654')
|
|
106
|
+
isThinkPrint && this.screenPrinter.streamOutput(content, '#c2a654')
|
|
113
107
|
})
|
|
114
108
|
this.brain.on(
|
|
115
109
|
BrainEvent.SUB_STREAM_TOOL_CALLS_OUTPUT,
|
|
116
110
|
(messages, toolCalls) => {
|
|
117
|
-
this.screenPrinter.streamOutput(toolCalls, '#47854a')
|
|
111
|
+
isThinkPrint && this.screenPrinter.streamOutput(toolCalls, '#47854a')
|
|
118
112
|
},
|
|
119
113
|
)
|
|
120
114
|
this.brain.on(BrainEvent.SUB_STREAM_END, () => {
|
|
121
|
-
this.screenPrinter.streamLineBreak()
|
|
115
|
+
isThinkPrint && this.screenPrinter.streamLineBreak()
|
|
122
116
|
})
|
|
123
117
|
this.brain.on(BrainEvent.SUB_USE_TOOL, async (toolCalls) => {
|
|
124
118
|
await this.hand.useTools(toolCalls)
|
|
@@ -181,73 +175,6 @@ class BaseAgentRobot {
|
|
|
181
175
|
})
|
|
182
176
|
}
|
|
183
177
|
|
|
184
|
-
loadAttachTool(toolName) {
|
|
185
|
-
let tool = this.attachTools.find((t) => t.name === toolName)
|
|
186
|
-
if (!tool) {
|
|
187
|
-
tool = this.toolCollection.find((t) => t.name === toolName)
|
|
188
|
-
this.attachTools.push(tool)
|
|
189
|
-
}
|
|
190
|
-
}
|
|
191
|
-
|
|
192
|
-
getTools() {
|
|
193
|
-
const tools = [...this.originalTools, ...this.attachTools]
|
|
194
|
-
const toolFunctions = {
|
|
195
|
-
fs,
|
|
196
|
-
axios,
|
|
197
|
-
dayjs,
|
|
198
|
-
lodash,
|
|
199
|
-
canvas,
|
|
200
|
-
echarts,
|
|
201
|
-
cheerio,
|
|
202
|
-
puppeteer
|
|
203
|
-
}
|
|
204
|
-
|
|
205
|
-
tools.forEach((tool) => {
|
|
206
|
-
Object.assign(toolFunctions, tool.functions)
|
|
207
|
-
})
|
|
208
|
-
toolFunctions.agentRobot = this
|
|
209
|
-
toolFunctions.Tools = toolFunctions
|
|
210
|
-
// 兼容老版本
|
|
211
|
-
toolFunctions.aiCli = {
|
|
212
|
-
Tools: toolFunctions,
|
|
213
|
-
}
|
|
214
|
-
return toolFunctions
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
getToolDescriptions() {
|
|
218
|
-
const tools = [...this.originalTools, ...this.attachTools]
|
|
219
|
-
const toolDescriptions = []
|
|
220
|
-
tools.forEach((tool) => {
|
|
221
|
-
const descriptions = tool.descriptions.map((item) => {
|
|
222
|
-
if (!item.type) {
|
|
223
|
-
return {
|
|
224
|
-
type: 'function',
|
|
225
|
-
function: item,
|
|
226
|
-
}
|
|
227
|
-
} else {
|
|
228
|
-
return item
|
|
229
|
-
}
|
|
230
|
-
})
|
|
231
|
-
toolDescriptions.push(...descriptions)
|
|
232
|
-
})
|
|
233
|
-
return toolDescriptions
|
|
234
|
-
}
|
|
235
|
-
|
|
236
|
-
// 获取原装工具
|
|
237
|
-
_getOriginalTools() {
|
|
238
|
-
// 自动扫描tools目录
|
|
239
|
-
const toolsPath = path.join(__dirname, './tools')
|
|
240
|
-
const toolFiles = fs.readdirSync(toolsPath).filter((file) => {
|
|
241
|
-
return file.endsWith('.js') || file.endsWith('.mjs')
|
|
242
|
-
})
|
|
243
|
-
const tools = []
|
|
244
|
-
toolFiles.forEach((file) => {
|
|
245
|
-
const tool = require(path.join(toolsPath, file))
|
|
246
|
-
tools.push(tool)
|
|
247
|
-
})
|
|
248
|
-
return tools
|
|
249
|
-
}
|
|
250
|
-
|
|
251
178
|
_getDefaultSystemPrompt(opt) {
|
|
252
179
|
const osType = process.platform
|
|
253
180
|
const workspace = this.workspace
|
|
@@ -262,7 +189,7 @@ class BaseAgentRobot {
|
|
|
262
189
|
语言类型: 与用户输入语言一致
|
|
263
190
|
|
|
264
191
|
### 工具使用
|
|
265
|
-
执行任务前,应仔细阅读工具描述以及可以使用的Skills
|
|
192
|
+
执行任务前,应仔细阅读工具描述以及可以使用的Skills的描述内容,优先使用匹配到的工具或技能,避免自己发挥。
|
|
266
193
|
|
|
267
194
|
### 大文本文件处理规则(分步执行)
|
|
268
195
|
处理长文档等大文件(单文件>${maxBlockFileSize}KB)时,必须按以下步骤分块处理:
|
|
@@ -277,6 +204,15 @@ class BaseAgentRobot {
|
|
|
277
204
|
3. 结果校验:任务完成后,需简单校验结果是否符合用户目标(如文件是否生成、内容是否完整),并向用户反馈校验结果。
|
|
278
205
|
4. 如果执行任务过程中需要安装依赖、软件或工具,必须通过调用用户交互函数与用户交互,等待用户确认后再执行安装,除非用户明确说明执行过程中使用静默模式。
|
|
279
206
|
5. 任务执行过程中,产生的所有临时文件(如分块文件、测试文件等)必须以"tmp_"为前缀命名,如"tmp_block_filename.txt、tmp_test_filename.txt、tmp_bak_filename.txt",并在任务完成后删除这些临时文件,确保工作目录整洁。
|
|
207
|
+
|
|
208
|
+
### 用户信息
|
|
209
|
+
#### 用户信息记录规则
|
|
210
|
+
当对话中出现用户信息时,如个人基础信息(如姓名、年龄、职业、兴趣、性格特征)、操作习惯、代码习惯、阅读习惯、常用目录、文档收藏夹目录等,必须使用用户信息读写函数进行记录。
|
|
211
|
+
|
|
212
|
+
#### 当前用户信息
|
|
213
|
+
----user info start----
|
|
214
|
+
${this.toolManager.functions.readUserInfo()}
|
|
215
|
+
----user info end----
|
|
280
216
|
`
|
|
281
217
|
}
|
|
282
218
|
|
|
@@ -67,7 +67,7 @@ async function createSubAgent(workGoal) {
|
|
|
67
67
|
|
|
68
68
|
function loadAttachTool(toolName) {
|
|
69
69
|
try {
|
|
70
|
-
this.agentRobot.
|
|
70
|
+
this.agentRobot.toolManager.addTool(toolName)
|
|
71
71
|
return `Tool ${toolName} loaded successfully`
|
|
72
72
|
} catch (error) {
|
|
73
73
|
return `Failed to load tool ${toolName}: ${error.message}`
|
|
@@ -107,7 +107,7 @@ const descriptions = [
|
|
|
107
107
|
function: {
|
|
108
108
|
name: 'createSubAgent',
|
|
109
109
|
description:
|
|
110
|
-
'
|
|
110
|
+
'创建一个子agent,并让其执行给定工作目标。可以进行复杂的任务分配,如知识库查询、多源搜索结果整合、多步骤任务处理等,返回执行结果与状态信息。',
|
|
111
111
|
parameters: {
|
|
112
112
|
type: 'object',
|
|
113
113
|
properties: {
|
|
@@ -151,6 +151,7 @@ const CreateAgentTool = {
|
|
|
151
151
|
description: '提供子agent创建与任务分发能力,可按技能选择子agent执行目标任务',
|
|
152
152
|
descriptions,
|
|
153
153
|
functions,
|
|
154
|
+
isSystem: true
|
|
154
155
|
}
|
|
155
156
|
|
|
156
157
|
module.exports = CreateAgentTool
|
|
@@ -202,7 +202,7 @@ module.exports = functions
|
|
|
202
202
|
3. 测试文件:如果引入了requestAI,必须确保函数可正确使用 this.Tools 上下文。
|
|
203
203
|
- 环境创建方式:
|
|
204
204
|
"const { DeepFishAI } = require('${packagePath}')\nconst deepfishAI = new DeepFishAI();"
|
|
205
|
-
- 调用方式:为模块导出的functions绑定Tools上下文,示例:functions.Tools = deepfishAI.agentRobot.
|
|
205
|
+
- 调用方式:为模块导出的functions绑定Tools上下文,示例:functions.Tools = deepfishAI.agentRobot.toolManager.functions;
|
|
206
206
|
4. 断言与输出规范:每个用例需打印“用例名称、输入、期望、实际、是否通过(PASS/FAIL)”;全部执行后输出汇总(总数、通过数、失败数)。
|
|
207
207
|
5. 失败处理:出现异常时不得静默吞错,需捕获并输出可定位信息(错误消息、对应用例、关键参数)。
|
|
208
208
|
6. 副作用控制:测试过程中创建的临时文件必须使用 tmp_test_ 前缀,并在测试结束后清理。
|
|
@@ -423,6 +423,7 @@ const GenerateTools = {
|
|
|
423
423
|
description: '提供扩展工具与Skill工具包生成规则能力,用于辅助AI构建标准化扩展项目模板',
|
|
424
424
|
descriptions,
|
|
425
425
|
functions,
|
|
426
|
+
isSystem: true
|
|
426
427
|
}
|
|
427
428
|
|
|
428
429
|
module.exports = GenerateTools
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
* @Author: Roman 306863030@qq.com
|
|
3
3
|
* @Date: 2026-03-17 11:59:19
|
|
4
4
|
* @LastEditors: Roman 306863030@qq.com
|
|
5
|
-
* @LastEditTime: 2026-04-
|
|
5
|
+
* @LastEditTime: 2026-04-23 18:53:35
|
|
6
6
|
* @FilePath: \deepfish\src\AgentRobot\BaseAgentRobot\tools\SystemTools.js
|
|
7
7
|
* @Description: 默认扩展函数
|
|
8
8
|
* @
|
|
@@ -65,8 +65,6 @@ async function requestAI(
|
|
|
65
65
|
systemDescription = systemDescription.systemDescription || ''
|
|
66
66
|
}
|
|
67
67
|
try {
|
|
68
|
-
aiConsole.logInfo(`aiSystem: ${systemDescription}`)
|
|
69
|
-
aiConsole.logInfo(`aiPrompt: ${prompt}`)
|
|
70
68
|
const response = await this.agentRobot.brain.think(
|
|
71
69
|
systemDescription,
|
|
72
70
|
prompt,
|
|
@@ -91,7 +89,7 @@ async function executeJSCode(code) {
|
|
|
91
89
|
throw error
|
|
92
90
|
}
|
|
93
91
|
try {
|
|
94
|
-
const functions = this.agentRobot.
|
|
92
|
+
const functions = this.agentRobot.toolManager.functions
|
|
95
93
|
const Func = new Function(
|
|
96
94
|
'Tools',
|
|
97
95
|
'require',
|
|
@@ -237,6 +235,7 @@ const SystemTool = {
|
|
|
237
235
|
'提供系统命令执行、AI请求、JS代码执行、扩展文件生成规则、AI配置管理、Tool加载执行等核心系统功能',
|
|
238
236
|
descriptions,
|
|
239
237
|
functions,
|
|
238
|
+
isSystem: true
|
|
240
239
|
}
|
|
241
240
|
|
|
242
241
|
module.exports = SystemTool
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
const fs = require('fs-extra')
|
|
2
|
+
|
|
3
|
+
function readUserInfo() {
|
|
4
|
+
const userInfoFilePath = this.agentRobot.userInfoFilePath
|
|
5
|
+
if (!fs.existsSync(userInfoFilePath)) {
|
|
6
|
+
return '暂无用户信息记录';
|
|
7
|
+
}
|
|
8
|
+
return fs.readFileSync(userInfoFilePath, 'utf-8') || '暂无用户信息记录'
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
async function writeUserInfo(info) {
|
|
12
|
+
const userInfoFilePath = this.agentRobot.userInfoFilePath
|
|
13
|
+
const oldUserInfo = this.Tools.readUserInfo()
|
|
14
|
+
const normalizedOldUserInfo = oldUserInfo === '暂无用户信息记录' ? '' : oldUserInfo
|
|
15
|
+
let mergedInfo = (info || '').trim()
|
|
16
|
+
|
|
17
|
+
const systemDescription = `你是用户信息整理助手。请将已有用户信息与新增用户信息合并为一份Markdown文本,只保留非敏感信息并去重,优先保留更新、更准确的信息。记录内容应简洁、可检索、可复用。`
|
|
18
|
+
const prompt = `请整合以下两部分用户信息,输出最终Markdown内容(仅输出正文,不要解释):\n\n【已有用户信息】\n${normalizedOldUserInfo || '(空)'}\n\n【新增用户信息】\n${mergedInfo || '(空)'}\n\n要求:\n1. 仅保留非敏感信息。\n2. 相同信息去重,冲突时以新增信息为准。\n3. 不确定内容标注“待确认”。\n4. 按“信息类型: 信息内容”格式整理。`
|
|
19
|
+
|
|
20
|
+
if (this.Tools.requestAI && mergedInfo) {
|
|
21
|
+
try {
|
|
22
|
+
const aiMerged = await this.Tools.requestAI(systemDescription, prompt, 0.2)
|
|
23
|
+
if (typeof aiMerged === 'string' && aiMerged.trim()) {
|
|
24
|
+
mergedInfo = aiMerged.trim()
|
|
25
|
+
}
|
|
26
|
+
} catch (error) {
|
|
27
|
+
// AI合并失败时回退为直接写入新增信息,保证写入流程可用
|
|
28
|
+
mergedInfo = normalizedOldUserInfo + '\n' + info
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
fs.writeFileSync(userInfoFilePath, mergedInfo, 'utf-8')
|
|
32
|
+
// 更新系统提示词中的用户信息区块
|
|
33
|
+
this.agentRobot.systemPrompt = this.agentRobot.systemPrompt.replace(
|
|
34
|
+
/----user info start----([\s\S]*?)----user info end----/,
|
|
35
|
+
`----user info start----\n${mergedInfo}\n----user info end----`
|
|
36
|
+
)
|
|
37
|
+
return true
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const descriptions = [
|
|
41
|
+
{
|
|
42
|
+
type: 'function',
|
|
43
|
+
function: {
|
|
44
|
+
name: 'writeUserInfo',
|
|
45
|
+
description: '写入用户信息内容。记录规则:1. 仅记录非敏感信息:个人基础信息(如姓名、年龄、职业、兴趣、性格特征)、操作习惯、代码习惯、阅读习惯、常用目录、文档收藏夹目录等。2. 禁止记录敏感信息:密码、密钥、令牌、身份证号、银行卡号、详细住址、联系方式、精确定位、财务/医疗等个人隐私数据。3. 已有同类信息时优先更新,不重复堆叠;存在不确定信息时需标注“待确认”,不得臆测补全。4. 记录内容应简洁、可检索、可复用,避免写入一次性上下文和与任务无关的噪音信息。5. 无需询问,自动写入文件。6. 使用markdown文件格式记录,按照“信息类型: 信息内容”的格式进行记录,如“兴趣: 阅读、旅行、编程”。7. 只记录当前用户信息中没有的内容,避免重复记录相同信息。',
|
|
46
|
+
parameters: {
|
|
47
|
+
type: 'object',
|
|
48
|
+
properties: {
|
|
49
|
+
info: {
|
|
50
|
+
type: 'string',
|
|
51
|
+
description: '当前用户信息中没有的内容',
|
|
52
|
+
},
|
|
53
|
+
},
|
|
54
|
+
required: ['info'],
|
|
55
|
+
},
|
|
56
|
+
},
|
|
57
|
+
},
|
|
58
|
+
{
|
|
59
|
+
type: 'function',
|
|
60
|
+
function: {
|
|
61
|
+
name: 'readUserInfo',
|
|
62
|
+
description: '读取已记录的用户信息内容。如果文件不存在,则返回 "暂无用户信息记录"。',
|
|
63
|
+
parameters: {
|
|
64
|
+
type: 'object',
|
|
65
|
+
properties: {},
|
|
66
|
+
required: [],
|
|
67
|
+
},
|
|
68
|
+
},
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
]
|
|
72
|
+
|
|
73
|
+
const functions = {
|
|
74
|
+
readUserInfo,
|
|
75
|
+
writeUserInfo,
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
const UserTool = {
|
|
79
|
+
name: 'UserTool',
|
|
80
|
+
description: '提供用户信息读写功能,用于维护用户偏好、习惯和常用目录信息',
|
|
81
|
+
platform: 'all',
|
|
82
|
+
descriptions,
|
|
83
|
+
functions,
|
|
84
|
+
isSystem: true
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
module.exports = UserTool
|
|
@@ -44,10 +44,10 @@ async function think(
|
|
|
44
44
|
streamEnd,
|
|
45
45
|
)
|
|
46
46
|
await thinkAfter()
|
|
47
|
-
return messageRes.choices[0].message.content
|
|
47
|
+
return clearThinkTag(messageRes.choices[0].message.content)
|
|
48
48
|
}
|
|
49
49
|
await thinkAfter()
|
|
50
|
-
return response.choices[0].message.content
|
|
50
|
+
return clearThinkTag(response.choices[0].message.content)
|
|
51
51
|
} catch (error) {
|
|
52
52
|
throw new Error(`AI response error: ${error.message}`)
|
|
53
53
|
}
|
|
@@ -90,6 +90,7 @@ async function thinkByTool(
|
|
|
90
90
|
streamEnd,
|
|
91
91
|
)
|
|
92
92
|
await thinkAfter()
|
|
93
|
+
messageRes.choices[0].message.content = clearThinkTag(messageRes.choices[0].message.content)
|
|
93
94
|
return {
|
|
94
95
|
content: messageRes.choices[0].message.content,
|
|
95
96
|
tool_calls: messageRes.choices[0].message.tool_calls,
|
|
@@ -97,6 +98,7 @@ async function thinkByTool(
|
|
|
97
98
|
}
|
|
98
99
|
}
|
|
99
100
|
await thinkAfter()
|
|
101
|
+
response.choices[0].message.content = clearThinkTag(response.choices[0].message.content)
|
|
100
102
|
return {
|
|
101
103
|
content: response.choices[0].message.content,
|
|
102
104
|
tool_calls: response.choices[0].message.tool_calls,
|
|
@@ -107,6 +109,11 @@ async function thinkByTool(
|
|
|
107
109
|
}
|
|
108
110
|
}
|
|
109
111
|
|
|
112
|
+
// 清除字符串中<think></think>标签以及之间的内容
|
|
113
|
+
function clearThinkTag(content) {
|
|
114
|
+
return content.replace(/<think>[\s\S]*?<\/think>/g, '')
|
|
115
|
+
}
|
|
116
|
+
|
|
110
117
|
// 流式输出结果转非流式输出
|
|
111
118
|
async function _streamToNonStream(
|
|
112
119
|
stream,
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
const path = require('path')
|
|
2
|
+
const os = require('os')
|
|
3
|
+
const fs = require('fs-extra')
|
|
4
|
+
const dayjs = require('dayjs')
|
|
5
|
+
const axios = require('axios')
|
|
6
|
+
const echarts = require('echarts')
|
|
7
|
+
const canvas = require('canvas')
|
|
8
|
+
const cheerio = require('cheerio')
|
|
9
|
+
const puppeteer = require('puppeteer')
|
|
10
|
+
const lodash = require('lodash')
|
|
11
|
+
const { AttachmentToolScanner } = require('./AttachmentToolScanner')
|
|
12
|
+
|
|
13
|
+
class AIToolManager {
|
|
14
|
+
originalTools = null // 原装工具
|
|
15
|
+
attachTools = null // 附加工具, Agent后续安装的工具函数
|
|
16
|
+
functions = {} // key为函数名称,value为方法体
|
|
17
|
+
descriptions = [] // openai能识别的描述
|
|
18
|
+
tools = [] // 工具列表
|
|
19
|
+
toolCollection = null // 工具集合
|
|
20
|
+
clawSkillCollection = null // Claw技能集合
|
|
21
|
+
constructor(agentRobot) {
|
|
22
|
+
this.agentRobot = agentRobot
|
|
23
|
+
this.initTools(agentRobot.opt)
|
|
24
|
+
}
|
|
25
|
+
initTools(opt) {
|
|
26
|
+
this.originalTools = this._getOriginalTools() // 天赋技能
|
|
27
|
+
this.attachTools = opt.attachTools || [] // 附加工具, Agent后续安装的工具函数
|
|
28
|
+
const tools = [...this.originalTools, ...this.attachTools]
|
|
29
|
+
tools.forEach((tool) => {
|
|
30
|
+
this._addTool(tool)
|
|
31
|
+
})
|
|
32
|
+
Object.assign(this.functions, {
|
|
33
|
+
fs,
|
|
34
|
+
axios,
|
|
35
|
+
dayjs,
|
|
36
|
+
lodash,
|
|
37
|
+
canvas,
|
|
38
|
+
echarts,
|
|
39
|
+
cheerio,
|
|
40
|
+
puppeteer,
|
|
41
|
+
})
|
|
42
|
+
|
|
43
|
+
this.functions.agentRobot = this.agentRobot
|
|
44
|
+
this.functions.Tools = this.functions
|
|
45
|
+
// 兼容老版本
|
|
46
|
+
this.functions.aiCli = {
|
|
47
|
+
Tools: this.functions,
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
// 外部工具扫描
|
|
51
|
+
this.toolCollection = AttachmentToolScanner.getToolCollection(
|
|
52
|
+
this.agentRobot.workspace,
|
|
53
|
+
) // 加载工具集合
|
|
54
|
+
this.clawSkillCollection = AttachmentToolScanner.getClawSkillCollection(
|
|
55
|
+
this.agentRobot.basespace,
|
|
56
|
+
) // 加载Claw技能集合
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
// 动态添加工具
|
|
60
|
+
addTool(toolName) {
|
|
61
|
+
let tool = this.tools.find((t) => t.name === toolName)
|
|
62
|
+
if (!tool) {
|
|
63
|
+
tool = this.toolCollection.find((t) => t.name === toolName)
|
|
64
|
+
if (tool) {
|
|
65
|
+
this._addTool(tool)
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
_addTool(tool) {
|
|
71
|
+
const platform = tool.platform || 'all'
|
|
72
|
+
if (platform === 'all' || platform === process.platform) {
|
|
73
|
+
tool.descriptions = tool.descriptions.map((item) => {
|
|
74
|
+
if (!item.type) {
|
|
75
|
+
item = {
|
|
76
|
+
type: 'function',
|
|
77
|
+
function: item,
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
return item
|
|
81
|
+
})
|
|
82
|
+
if (tool.name && !tool.isSystem) {
|
|
83
|
+
for (const funcName in tool.functions) {
|
|
84
|
+
if (!funcName.includes('_')) {
|
|
85
|
+
this.functions[`${tool.name}_${funcName}`] =
|
|
86
|
+
tool.functions[funcName]
|
|
87
|
+
} else {
|
|
88
|
+
this.functions[funcName] = tool.functions[funcName]
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
const descriptions = tool.descriptions.map((item) => {
|
|
92
|
+
if (
|
|
93
|
+
tool.name &&
|
|
94
|
+
item.function.name &&
|
|
95
|
+
!item.function.name.includes('_')
|
|
96
|
+
) {
|
|
97
|
+
item.function.name = `${tool.name}_${item.function.name}`
|
|
98
|
+
return item
|
|
99
|
+
} else {
|
|
100
|
+
return item
|
|
101
|
+
}
|
|
102
|
+
})
|
|
103
|
+
this.descriptions.push(...descriptions)
|
|
104
|
+
} else {
|
|
105
|
+
Object.assign(this.functions, tool.functions)
|
|
106
|
+
this.descriptions.push(...tool.descriptions)
|
|
107
|
+
}
|
|
108
|
+
this.tools.push(tool)
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
// 获取原装工具
|
|
113
|
+
_getOriginalTools() {
|
|
114
|
+
// 自动扫描tools目录
|
|
115
|
+
const toolsPath = path.join(__dirname, '../tools')
|
|
116
|
+
const toolFiles = fs.readdirSync(toolsPath).filter((file) => {
|
|
117
|
+
return file.endsWith('.js') || file.endsWith('.cjs')
|
|
118
|
+
})
|
|
119
|
+
const tools = []
|
|
120
|
+
toolFiles.forEach((file) => {
|
|
121
|
+
const tool = require(path.join(toolsPath, file))
|
|
122
|
+
tools.push(tool)
|
|
123
|
+
})
|
|
124
|
+
return tools
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
module.exports = AIToolManager
|
package/src/cli/DefaultConfig.js
CHANGED
|
@@ -8,6 +8,7 @@ const defaultConfig = {
|
|
|
8
8
|
maxLogExpireTime: 3, // 日志过期时间,单位天,-1表示无限制,0表示不记录
|
|
9
9
|
maxBlockFileSize: 20, // 最大分块文件大小,单位KB;超过该大小的文件需要分块处理
|
|
10
10
|
encoding: 'auto', // 命令行编码格式, 可设置为utf-8、gbk等, 也可以设置成auto或空值自动判断
|
|
11
|
+
isThinkPrint: true, // 是否打印思考过程
|
|
11
12
|
EMBEDDING_API: '', // 向量化接口地址
|
|
12
13
|
EMBEDDING_API_KEY: '' // 向量化接口密钥
|
|
13
14
|
}
|