deepfish-ai 1.0.28 → 2.0.2

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.
Files changed (62) hide show
  1. package/README.md +126 -368
  2. package/dist/index.js +23320 -0
  3. package/dist/serve/client/assets/index-DUHYC91l.js +1 -0
  4. package/dist/serve/client/assets/index-zcrdNLZs.css +1 -0
  5. package/dist/serve/client/index.html +14 -0
  6. package/dist/serve/pm2-server.js +21015 -0
  7. package/dist/serve/server/entry-server.mjs +224 -0
  8. package/package.json +50 -70
  9. package/LICENSE +0 -21
  10. package/README_CN.md +0 -460
  11. package/src/AgentRobot/AgentRobotFactory/MainAgentRobot.js +0 -88
  12. package/src/AgentRobot/AgentRobotFactory/SubAgentRobot.js +0 -45
  13. package/src/AgentRobot/AgentRobotFactory/SubSkillAgentRobot.js +0 -45
  14. package/src/AgentRobot/AgentRobotFactory/index.js +0 -48
  15. package/src/AgentRobot/BaseAgentRobot/Brain.js +0 -210
  16. package/src/AgentRobot/BaseAgentRobot/BrainEvent.js +0 -17
  17. package/src/AgentRobot/BaseAgentRobot/Hand.js +0 -119
  18. package/src/AgentRobot/BaseAgentRobot/Heart.js +0 -0
  19. package/src/AgentRobot/BaseAgentRobot/Logger.js +0 -114
  20. package/src/AgentRobot/BaseAgentRobot/ScreenPrinter.js +0 -49
  21. package/src/AgentRobot/BaseAgentRobot/index.js +0 -266
  22. package/src/AgentRobot/BaseAgentRobot/lazy-tools/AliBailian.js +0 -997
  23. package/src/AgentRobot/BaseAgentRobot/lazy-tools/doc-transform.js +0 -204
  24. package/src/AgentRobot/BaseAgentRobot/lazy-tools/docx.js +0 -1286
  25. package/src/AgentRobot/BaseAgentRobot/lazy-tools/img.js +0 -765
  26. package/src/AgentRobot/BaseAgentRobot/lazy-tools/pdf.js +0 -736
  27. package/src/AgentRobot/BaseAgentRobot/lazy-tools/pptx.js +0 -567
  28. package/src/AgentRobot/BaseAgentRobot/lazy-tools/video.js +0 -36
  29. package/src/AgentRobot/BaseAgentRobot/lazy-tools/xlsx.js +0 -611
  30. package/src/AgentRobot/BaseAgentRobot/tools/BaseTools.js +0 -13
  31. package/src/AgentRobot/BaseAgentRobot/tools/CreateAgentTools.js +0 -158
  32. package/src/AgentRobot/BaseAgentRobot/tools/FileTools.js +0 -768
  33. package/src/AgentRobot/BaseAgentRobot/tools/GenerateTools.js +0 -441
  34. package/src/AgentRobot/BaseAgentRobot/tools/InquirerTools.js +0 -244
  35. package/src/AgentRobot/BaseAgentRobot/tools/SystemTools.js +0 -263
  36. package/src/AgentRobot/BaseAgentRobot/tools/TaskTools.js +0 -211
  37. package/src/AgentRobot/BaseAgentRobot/tools/TestTools.js +0 -100
  38. package/src/AgentRobot/BaseAgentRobot/tools/UserTool.js +0 -87
  39. package/src/AgentRobot/BaseAgentRobot/tools/WebTools.js +0 -257
  40. package/src/AgentRobot/BaseAgentRobot/utils/AIRequest.js +0 -297
  41. package/src/AgentRobot/BaseAgentRobot/utils/AIToolManager.js +0 -131
  42. package/src/AgentRobot/BaseAgentRobot/utils/AgentTree.js +0 -66
  43. package/src/AgentRobot/BaseAgentRobot/utils/AttachmentToolScanner.js +0 -235
  44. package/src/AgentRobot/BaseAgentRobot/utils/MessageCompresser.js +0 -236
  45. package/src/AgentRobot/BaseAgentRobot/utils/aiConsole.js +0 -74
  46. package/src/AgentRobot/BaseAgentRobot/utils/aiInquirer.js +0 -78
  47. package/src/AgentRobot/BaseAgentRobot/utils/analyzeReturn.test.js +0 -104
  48. package/src/AgentRobot/BaseAgentRobot/utils/copilot.js +0 -117
  49. package/src/AgentRobot/BaseAgentRobot/utils/node-root.js +0 -138
  50. package/src/AgentRobot/BaseAgentRobot/utils/normal.js +0 -399
  51. package/src/cli/ConfigManager.js +0 -283
  52. package/src/cli/DefaultConfig.js +0 -98
  53. package/src/cli/GlobalVariable.js +0 -8
  54. package/src/cli/MemoryManager.js +0 -53
  55. package/src/cli/SkillConfigManager.js +0 -343
  56. package/src/cli/SkillParser.js +0 -58
  57. package/src/cli/ai-auth.js +0 -231
  58. package/src/cli/ai-config.js +0 -225
  59. package/src/cli/ai-memory.js +0 -22
  60. package/src/cli/ai-skill.js +0 -65
  61. package/src/cli/index.js +0 -77
  62. package/src/index.js +0 -54
@@ -1,210 +0,0 @@
1
- const fs = require('fs-extra')
2
- const { EventEmitterSuper } = require('eventemitter-super')
3
- const BrainEvent = require('./BrainEvent.js')
4
- const MessageCompresser = require('./utils/MessageCompresser.js')
5
- const { creatClient, think, thinkByTool } = require('./utils/AIRequest.js')
6
- const lodash = require('lodash')
7
-
8
- class Brain extends EventEmitterSuper {
9
- constructor(agentRobot) {
10
- super()
11
- this.messages = []
12
- this.maxIterations = agentRobot.opt.maxIterations
13
- this.maxContextLength = agentRobot.opt.aiConfig.maxContextLength
14
- this.memoryFilePath = agentRobot.memoryFilePath
15
- this.messageCompresser = new MessageCompresser(this)
16
- this.aiConfig = agentRobot.opt.aiConfig
17
- this.aiClient = creatClient(agentRobot.opt.aiConfig)
18
- this.agentRobot = agentRobot
19
- this.restoreMemory()
20
- }
21
- // 恢复记忆
22
- restoreMemory() {
23
- if (fs.existsSync(this.memoryFilePath)) {
24
- const messages = fs.readJsonSync(this.memoryFilePath) || []
25
- // 预处理
26
- this.messages = this._initMessages(messages)
27
- }
28
- }
29
- // 写入记忆
30
- storeMemory(message) {
31
- this.messages.push(message)
32
- fs.writeJsonSync(this.memoryFilePath, this.messages, { spaces: 2 })
33
- this.emit(BrainEvent.NEW_MESSAGE, message)
34
- }
35
- storeToolReport(toolId, toolReport) {
36
- if (typeof toolReport === 'object') {
37
- toolReport = JSON.stringify(toolReport)
38
- }
39
- const message = {
40
- role: 'tool',
41
- tool_call_id: toolId,
42
- content: toolReport,
43
- }
44
- this.storeMemory(message)
45
- }
46
-
47
- clearMemory() {
48
- fs.removeSync(this.memoryFilePath)
49
- }
50
- // 循环思考
51
- async thinkLoop(goal) {
52
- let maxIterations = this.maxIterations
53
- if (this.messages.length === 0) {
54
- // 初始化message
55
- this.storeMemory({
56
- role: 'system',
57
- content: this.agentRobot.systemPrompt,
58
- })
59
- this.storeMemory({
60
- role: 'user',
61
- content: goal,
62
- })
63
- } else {
64
- this.storeMemory({
65
- role: 'user',
66
- content: goal,
67
- })
68
- }
69
- let messages = this.messages
70
- if (maxIterations === -1) {
71
- maxIterations = Infinity
72
- }
73
- const skillDescriptions = this.agentRobot.toolManager.descriptions
74
- this.emit(BrainEvent.THINK_BEFORE, messages)
75
- while (maxIterations-- > 0) {
76
- try {
77
- // 更新系统提示词
78
- if (messages[0].role === 'system') {
79
- this.agentRobot.updateSessionTime()
80
- messages[0] = {
81
- role: 'system',
82
- content: this.agentRobot.systemPrompt,
83
- }
84
- }
85
- // 压缩上下文
86
- await this.messageCompresser.compress(messages)
87
- const { message, content, tool_calls } = await thinkByTool(
88
- this.aiClient,
89
- this.aiConfig,
90
- messages,
91
- skillDescriptions,
92
- () => {
93
- this.emit(BrainEvent.SUB_THINK_BEFORE, messages)
94
- },
95
- () => {
96
- this.emit(BrainEvent.SUB_THINK_AFTER, messages)
97
- },
98
- (thinkOutput) => {
99
- this.emit(BrainEvent.SUB_STREAM_THINK_OUTPUT, messages, thinkOutput)
100
- },
101
- (contentOutput) => {
102
- this.emit(
103
- BrainEvent.SUB_STREAM_CONTENT_OUTPUT,
104
- messages,
105
- contentOutput,
106
- )
107
- },
108
- (toolCallsOutput) => {
109
- this.emit(
110
- BrainEvent.SUB_STREAM_TOOL_CALLS_OUTPUT,
111
- messages,
112
- toolCallsOutput,
113
- )
114
- },
115
- () => {
116
- this.emit(BrainEvent.SUB_STREAM_END, messages)
117
- },
118
- )
119
- this.storeMemory(message)
120
- // 检查是否是任务完成的总结响应(没有工具调用且有内容)
121
- if (tool_calls) {
122
- await this.emitPromise(BrainEvent.SUB_USE_TOOL, tool_calls)
123
- } else {
124
- break
125
- }
126
- } catch (error) {
127
- this.emit(BrainEvent.SUB_THINK_ERROR, messages, error)
128
- return `AI response error: ${error.message}`
129
- }
130
- }
131
- const lastMessageContent = messages[messages.length - 1]?.content || ''
132
- this.emit(BrainEvent.THINK_AFTER, lastMessageContent)
133
- return lastMessageContent
134
- }
135
-
136
- async think(systemPrompt, prompt, temperature) {
137
- const messages = [
138
- {
139
- role: 'system',
140
- content: systemPrompt,
141
- },
142
- {
143
- role: 'user',
144
- content: prompt,
145
- },
146
- ]
147
- try {
148
- const aiConfig = lodash.cloneDeep(this.aiConfig)
149
- if (temperature) {
150
- aiConfig.temperature = temperature
151
- }
152
- const result = await think(
153
- this.aiClient,
154
- aiConfig,
155
- messages,
156
- () => {
157
- this.emit(BrainEvent.SUB_THINK_BEFORE, messages)
158
- },
159
- () => {
160
- this.emit(BrainEvent.SUB_THINK_AFTER, messages)
161
- },
162
- (thinkOutput) => {
163
- this.emit(BrainEvent.SUB_STREAM_THINK_OUTPUT, messages, thinkOutput)
164
- },
165
- (contentOutput) => {
166
- this.emit(
167
- BrainEvent.SUB_STREAM_CONTENT_OUTPUT,
168
- messages,
169
- contentOutput,
170
- )
171
- },
172
- (toolCallsOutput) => {
173
- this.emit(
174
- BrainEvent.SUB_STREAM_TOOL_CALLS_OUTPUT,
175
- messages,
176
- toolCallsOutput,
177
- )
178
- },
179
- () => {
180
- this.emit(BrainEvent.SUB_STREAM_END, messages)
181
- },
182
- )
183
- return result
184
- } catch (error) {
185
- this.emit(BrainEvent.SUB_THINK_ERROR, messages, error)
186
- return `AI response error: ${error.message}`
187
- }
188
- }
189
-
190
- _initMessages(messages) {
191
- let lastMessage = messages[messages.length - 1]
192
- while (
193
- messages.length > 1 &&
194
- !(
195
- lastMessage.role === 'assistant' &&
196
- !lastMessage.tool_calls &&
197
- lastMessage.content
198
- )
199
- ) {
200
- messages.pop()
201
- lastMessage = messages[messages.length - 1]
202
- }
203
- return messages
204
- }
205
- }
206
-
207
- module.exports = {
208
- Brain,
209
- BrainEvent
210
- }
@@ -1,17 +0,0 @@
1
- class BrainEvent {
2
- static THINK_BEFORE = '1'
3
- static SUB_THINK_BEFORE = '1.1'
4
- static SUB_THINK_AFTER = '1.2'
5
- static SUB_STREAM_THINK_OUTPUT = '1.3'
6
- static SUB_STREAM_CONTENT_OUTPUT = '1.4'
7
- static SUB_STREAM_TOOL_CALLS_OUTPUT = '1.5'
8
- static SUB_STREAM_END = '1.6'
9
- static SUB_USE_TOOL = '1.7'
10
- static SUB_THINK_ERROR = '1.8'
11
- static COMPRESS_MESSAGES_BEFORE = '1.9'
12
- static COMPRESS_MESSAGES_AFTER = '1.10'
13
- static NEW_MESSAGE = '1.11'
14
- static THINK_AFTER = '2'
15
- }
16
-
17
- module.exports = BrainEvent
@@ -1,119 +0,0 @@
1
- const { EventEmitterSuper } = require('eventemitter-super')
2
-
3
- const HandEvent = {
4
- USE_TOOL_BEFORE: '1',
5
- USE_TOOL_REPORT: '1.1',
6
- USE_TOOL_ERROR: '1.2',
7
- USE_TOOL_AFTER: '2',
8
- }
9
-
10
- class Hand extends EventEmitterSuper {
11
- constructor(agentRobot) {
12
- super()
13
- this.agentRobot = agentRobot
14
- this.maxBlockFileSize = agentRobot.opt.maxBlockFileSize || 20 // KB
15
- this.tools = agentRobot.toolManager.functions
16
- }
17
-
18
- _parseToolCalls(tool_call) {
19
- const { id, function: func } = tool_call
20
- const { name, arguments: args } = func
21
- let parsedArgs
22
- try {
23
- parsedArgs = typeof args === 'string' ? JSON.parse(args) : args
24
- } catch (error) {
25
- if (typeof args === 'string') {
26
- parsedArgs = {
27
- value: args,
28
- }
29
- }
30
- }
31
- return {
32
- toolId: id,
33
- funcArgs: parsedArgs,
34
- funcName: name,
35
- }
36
- }
37
-
38
- _getFunctionParamNames(fn) {
39
- if (typeof fn !== 'function') return []
40
- const src = fn.toString()
41
- const match = src.match(/^[\s\S]*?\(([^)]*)\)/)
42
- if (!match) return []
43
- return match[1]
44
- .split(',')
45
- .map((item) => item.trim())
46
- .filter(Boolean)
47
- .map((item) => item.replace(/\s*=.*$/, '').trim())
48
- }
49
-
50
- _buildOrderedArgs(fn, funcArgs) {
51
- if (funcArgs == null) return []
52
- if (Array.isArray(funcArgs)) return funcArgs
53
- if (typeof funcArgs !== 'object') return [funcArgs]
54
- const paramNames = this._getFunctionParamNames(fn)
55
- if (paramNames.length === 0) return Object.values(funcArgs)
56
- return paramNames.map((name) => funcArgs[name])
57
- }
58
-
59
- _getRequiredParamNames(funcName) {
60
- const descriptions = this.agentRobot.toolManager.descriptions
61
- const current = descriptions.find((item) => item?.function?.name === funcName)
62
- return current?.function?.parameters?.required || []
63
- }
64
-
65
- async useTools(tool_calls) {
66
- for (const toolCall of tool_calls) {
67
- const { toolId, funcArgs, funcName } = this._parseToolCalls(toolCall)
68
- const toolFunctions = this.tools
69
- this.emit(HandEvent.USE_TOOL_BEFORE, toolId, funcName, funcArgs)
70
- if (toolFunctions[funcName]) {
71
- try {
72
- const requiredParams = this._getRequiredParamNames(funcName)
73
- if (funcArgs && typeof funcArgs === 'object' && !Array.isArray(funcArgs)) {
74
- const missingParams = requiredParams.filter((name) => funcArgs[name] === undefined)
75
- if (missingParams.length > 0) {
76
- throw new Error(`Missing required tool arguments for ${funcName}: ${missingParams.join(', ')}`)
77
- }
78
- }
79
- const orderedArgs = this._buildOrderedArgs(toolFunctions[funcName], funcArgs)
80
- let result = await toolFunctions[funcName](...orderedArgs)
81
- let toolContent = JSON.stringify(result)
82
- if (funcName !== 'requestAI') {
83
- const MAX_CONTENT_SIZE = this.maxBlockFileSize * 1024
84
- if (toolContent.length > MAX_CONTENT_SIZE) {
85
- if (
86
- typeof result === 'string' &&
87
- result.length > MAX_CONTENT_SIZE
88
- ) {
89
- toolContent = {
90
- truncated: true,
91
- message:
92
- '文件内容过大,请使用executeJSCode工具编写脚本分块读取和处理文件,避免一次性读取整个文件内容到对话中。如果不是本地文件,建议创建或下载成本地文件后再进行分块读取。',
93
- preview: toolContent.substring(0, 1000) + '...',
94
- }
95
- } else {
96
- toolContent = {
97
- truncated: true,
98
- message: '结果数据量过大,请使用更具体的查询或分块处理。',
99
- preview: toolContent.substring(0, 1000) + '...',
100
- }
101
- }
102
- }
103
- }
104
- this.emit(HandEvent.USE_TOOL_REPORT, toolId, funcName, toolContent)
105
- } catch (error) {
106
- this.emit(HandEvent.USE_TOOL_ERROR, toolId, funcName, { error: true, message: error.message, stack: error.stack })
107
- }
108
- this.emit(HandEvent.USE_TOOL_AFTER, toolId, funcName, funcArgs)
109
- } else {
110
- this.emit(HandEvent.USE_TOOL_ERROR, toolId, funcName, { error: true, message: `Tool ${funcName} not found` })
111
- }
112
- }
113
- }
114
- }
115
-
116
- module.exports = {
117
- Hand,
118
- HandEvent
119
- }
File without changes
@@ -1,114 +0,0 @@
1
- const dayjs = require('dayjs')
2
- const path = require('path')
3
- const fs = require('fs-extra')
4
-
5
- class Logger {
6
- constructor(agentRobot) {
7
- this.logDirPath = agentRobot.logDirPath
8
- this.screenPrinter = agentRobot.screenPrinter
9
- this.maxLogExpireTime = agentRobot.config.maxLogExpireTime
10
- this.memorySpace = agentRobot.memorySpace
11
- this.logTimeMap = new Map()
12
- this.logId = agentRobot.logId || agentRobot.root.logId
13
- }
14
- clearAllLogs() {
15
- const fileNames = fs.readdirSync(this.memorySpace)
16
- const currentDate = dayjs()
17
- fileNames.forEach((fileName) => {
18
- // 如果是目录则进入logs目录,清除日志文件
19
- const filePath = path.join(this.memorySpace, fileName)
20
- if (fs.statSync(filePath).isDirectory()) {
21
- const logDir = path.join(filePath, 'logs')
22
- if (fs.existsSync(logDir) && fs.statSync(logDir).isDirectory()) {
23
- const logFiles = fs.readdirSync(logDir)
24
- logFiles.forEach((logFile) => {
25
- if (logFile.startsWith('log-') && logFile.endsWith('.txt')) {
26
- const logDate = dayjs(logFile.slice(4, -4))
27
- if (currentDate.diff(logDate, 'day') > this.maxLogExpireTime) {
28
- fs.removeSync(path.join(logDir, logFile))
29
- }
30
- }
31
- })
32
- }
33
- }
34
- })
35
- }
36
-
37
- logExecTime(id, description = '') {
38
- if (this.maxLogExpireTime === 0) {
39
- return false
40
- }
41
- if (this.logTimeMap.has(id)) {
42
- const startTime = this.logTimeMap.get(id)
43
- const duration = dayjs().diff(startTime, 'second')
44
- this.logTimeMap.delete(id)
45
- this.screenPrinter.logInfo(
46
- `${description} Execution time: ${duration} seconds`,
47
- )
48
- this.logInfo(`${description} Execution time: ${duration} seconds`)
49
- } else {
50
- this.logTimeMap.set(id, dayjs())
51
- }
52
- }
53
- logMessage(message) {
54
- if (this.maxLogExpireTime === 0) {
55
- return false
56
- }
57
- const logFile = path.join(
58
- this.logDirPath,
59
- `log-${this.logId}-${dayjs().format('YYYY-MM-DD HH')}.txt`,
60
- )
61
- const logFile2 = path.join(
62
- this.logDirPath,
63
- `log-messeage-${this.logId}.txt`,
64
- )
65
- try {
66
- let logEntry = `[${new Date().toISOString()}][${message.role}] ${message.content}\n`
67
- fs.appendFileSync(logFile, logEntry)
68
- fs.appendFileSync(logFile2, logEntry)
69
- return true
70
- } catch (error) {
71
- console.error('Failed to log message:', error.message)
72
- return false
73
- }
74
- }
75
- logCompress(messages) {
76
- if (this.maxLogExpireTime === 0) {
77
- return false
78
- }
79
- const logFile = path.join(
80
- this.logDirPath,
81
- `log-${this.logId}-${dayjs().format('YYYY-MM-DD HH')}.txt`,
82
- )
83
- try {
84
- let logEntry = `[${new Date().toISOString()}][***COMPRESS START***]
85
- ${messages.map(m => `[${m.role}] ${m.content}`).join('\n')}
86
- [***COMPRESS END***]
87
- `
88
- fs.appendFileSync(logFile, logEntry)
89
- return true
90
- } catch (error) {
91
- console.error('Failed to log compress:', error.message)
92
- return false
93
- }
94
- }
95
- logInfo(message) {
96
- if (this.maxLogExpireTime === 0) {
97
- return false
98
- }
99
- const logFile = path.join(
100
- this.logDirPath,
101
- `log-${this.logId}-${dayjs().format('YYYY-MM-DD HH')}.txt`,
102
- )
103
- try {
104
- let logEntry = `[${new Date().toISOString()}][###############] ${message}\n`
105
- fs.appendFileSync(logFile, logEntry)
106
- return true
107
- } catch (error) {
108
- console.error('Failed to log info:', error.message)
109
- return false
110
- }
111
- }
112
- }
113
-
114
- module.exports = Logger
@@ -1,49 +0,0 @@
1
- const aiConsole = require("./utils/aiConsole.js")
2
-
3
- class ScreenPrinter {
4
- // 日志相关工具函数
5
- logInfo(message) {
6
- aiConsole.logInfo(message)
7
- }
8
-
9
- logSuccess(message) {
10
- aiConsole.logSuccess(message)
11
- }
12
-
13
- logError(message) {
14
- aiConsole.logError(message)
15
- }
16
-
17
- logWarning(message) {
18
- aiConsole.logWarning(message)
19
- }
20
-
21
- logDisabled(message) {
22
- aiConsole.logDisabled(message)
23
- }
24
-
25
- writeLine(msg1, msg2 = '', color = 'blue') {
26
- aiConsole.writeLine(msg1, msg2, color)
27
- }
28
-
29
- // 流式输出
30
- async streamOutput(text, color = '#9bed7f') {
31
- return aiConsole.streamOutput(text, color)
32
- }
33
-
34
- // 流式换行
35
- async streamLineBreak() {
36
- return aiConsole.streamLineBreak()
37
- }
38
-
39
-
40
- loading(label = 'Thinking...') {
41
- return aiConsole.loading(label)
42
- }
43
-
44
- log(msg, color) {
45
- aiConsole.log(msg, color)
46
- }
47
- }
48
-
49
- module.exports = ScreenPrinter