deepfish-ai 1.0.29 → 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 +61 -83
  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 -424
  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 -354
  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,297 +0,0 @@
1
- const { OpenAI } = require('openai')
2
- const {
3
- refreshGithubModelsTokenIfNeeded,
4
- buildDefaultHeaders,
5
- } = require('./copilot.js')
6
-
7
- function normalizeAiRequestConfig(aiConfig = {}) {
8
- const max_tokens = (!aiConfig.maxTokens || aiConfig.maxTokens === -1)
9
- ? undefined
10
- : aiConfig.maxTokens * 1024 // 兼容旧配置,按 1KB ~= 1024 tokens 估算
11
-
12
- const requestConfig = {
13
- model: aiConfig.model,
14
- temperature: aiConfig.temperature,
15
- stream: aiConfig.stream,
16
- max_tokens,
17
- }
18
-
19
- // 仅 DeepSeek 兼容端注入扩展字段,避免其它网关因未知参数报错
20
- if (aiConfig.type === 'deepseek') {
21
- requestConfig.extra_body = { reasoning_split: true }
22
- }
23
-
24
- return requestConfig
25
- }
26
-
27
- function creatClient(aiConfig) {
28
- if (aiConfig.type === 'github-models') {
29
- if (!aiConfig.apiKey || !String(aiConfig.apiKey).trim()) {
30
- throw new Error(
31
- 'GitHub Models requires apiKey. Please set a GitHub token with models:read permission in your current AI config.',
32
- )
33
- }
34
- }
35
- if (aiConfig.type === 'copilot') {
36
- if (!aiConfig.apiKey || !String(aiConfig.apiKey).trim()) {
37
- throw new Error(
38
- 'GitHub Copilot requires apiKey. Please set a GitHub Copilot access token in your current AI config.',
39
- )
40
- }
41
- }
42
-
43
- return new OpenAI({
44
- baseURL: aiConfig.baseUrl,
45
- apiKey: aiConfig.apiKey || '',
46
- defaultHeaders: buildDefaultHeaders(aiConfig),
47
- })
48
- }
49
-
50
- /**
51
- * Ai单轮问答
52
- * @param {*} openAiClient OpenAI客户端
53
- * @param {*} aiConfig {model, temperature, maxTokens, stream}
54
- * @param {*} systemDescription
55
- * @param {*} prompt
56
- * @param {*} temperature
57
- * @returns
58
- */
59
- async function think(
60
- openAiClient,
61
- aiConfig,
62
- messages,
63
- thinkBefore = () => {},
64
- thinkAfter = () => {},
65
- streamThinkOutput,
66
- streamContentOutput,
67
- streamToolCallsOutput,
68
- streamEnd,
69
- ) {
70
- try {
71
- await refreshGithubModelsTokenIfNeeded(aiConfig)
72
- const requestClient = creatClient(aiConfig)
73
- const requestConfig = normalizeAiRequestConfig(aiConfig)
74
- const opt = {
75
- messages: messages,
76
- ...requestConfig,
77
- }
78
- await thinkBefore()
79
- const extraHeaders = aiConfig.type === 'copilot' ? { 'x-initiator': 'user' } : undefined
80
- const response = await requestClient.chat.completions.create(opt, extraHeaders ? { headers: extraHeaders } : undefined)
81
- if (aiConfig.stream) {
82
- const messageRes = await _streamToNonStream(
83
- response,
84
- streamThinkOutput,
85
- streamContentOutput,
86
- streamToolCallsOutput,
87
- streamEnd,
88
- )
89
- await thinkAfter()
90
- return clearThinkTag(messageRes.choices[0].message.content)
91
- }
92
- await thinkAfter()
93
- return clearThinkTag(response.choices[0].message.content)
94
- } catch (error) {
95
- throw new Error(`AI response error: ${error.message}`)
96
- }
97
- }
98
- /**
99
- * Ai携带工具请求
100
- * @param {*} openAiClient
101
- * @param {*} aiConfig {model, temperature, maxTokens}
102
- * @param {*} messages
103
- * @param {*} functionDescriptions
104
- * @returns
105
- */
106
- async function thinkByTool(
107
- openAiClient,
108
- aiConfig,
109
- messages,
110
- functionDescriptions,
111
- thinkBefore = () => {},
112
- thinkAfter = () => {},
113
- streamThinkOutput,
114
- streamContentOutput,
115
- streamToolCallsOutput,
116
- streamEnd,
117
- ) {
118
- try {
119
- await refreshGithubModelsTokenIfNeeded(aiConfig)
120
- const requestClient = creatClient(aiConfig)
121
- const requestConfig = normalizeAiRequestConfig(aiConfig)
122
- await thinkBefore()
123
- const extraHeaders = aiConfig.type === 'copilot' ? { 'x-initiator': 'agent' } : undefined
124
- const response = await requestClient.chat.completions.create(
125
- {
126
- messages: messages,
127
- tools: functionDescriptions,
128
- tool_choice: 'auto',
129
- ...requestConfig,
130
- },
131
- extraHeaders ? { headers: extraHeaders } : undefined,
132
- )
133
- if (aiConfig.stream) {
134
- const messageRes = await _streamToNonStream(
135
- response,
136
- streamThinkOutput,
137
- streamContentOutput,
138
- streamToolCallsOutput,
139
- streamEnd,
140
- )
141
- await thinkAfter()
142
- messageRes.choices[0].message.content = clearThinkTag(messageRes.choices[0].message.content)
143
- return {
144
- content: messageRes.choices[0].message.content,
145
- tool_calls: messageRes.choices[0].message.tool_calls,
146
- message: messageRes.choices[0].message,
147
- }
148
- }
149
- await thinkAfter()
150
- response.choices[0].message.content = clearThinkTag(response.choices[0].message.content)
151
- return {
152
- content: response.choices[0].message.content,
153
- tool_calls: response.choices[0].message.tool_calls,
154
- message: response.choices[0].message,
155
- }
156
- } catch (error) {
157
- throw new Error(`AI response error: ${error.message}`)
158
- }
159
- }
160
-
161
- // 清除字符串中<think></think>标签以及之间的内容
162
- function clearThinkTag(content) {
163
- return content.replace(/<think>[\s\S]*?<\/think>/g, '')
164
- }
165
-
166
- // 流式输出结果转非流式输出
167
- async function _streamToNonStream(
168
- stream,
169
- streamThinkOutput = () => {},
170
- streamContentOutput = () => {},
171
- streamToolCallsOutput = () => {},
172
- streamEnd = () => {},
173
- ) {
174
- // 初始化最终响应结构(对齐 OpenAI 非流式响应格式)
175
- const finalResponse = {
176
- id: '',
177
- object: 'chat.completion',
178
- created: Math.floor(Date.now() / 1000), // 生成时间戳
179
- model: '',
180
- choices: [
181
- {
182
- index: 0,
183
- message: {
184
- role: 'assistant',
185
- content: '',
186
- reasoning_content: '',
187
- tool_calls: [], // 存储完整的工具调用列表
188
- },
189
- finish_reason: null,
190
- logprobs: null,
191
- },
192
- ],
193
- usage: {
194
- prompt_tokens: 0,
195
- completion_tokens: 0,
196
- total_tokens: 0,
197
- },
198
- }
199
-
200
- // 工具调用缓冲区:处理多工具调用 + 分段参数拼接
201
- const toolCallBuffers = new Map() // key: tool_call_id, value: toolCall object
202
- const toolCallIndexMap = new Map() // key: index, value: tool_call_id
203
- try {
204
- // 遍历所有流式数据块
205
- for await (const chunk of stream) {
206
- // 1. 填充全局信息(仅首次获取)
207
- if (!finalResponse.id) {
208
- finalResponse.id = chunk.id || `chatcmpl-${Date.now()}`
209
- }
210
- if (!finalResponse.model) {
211
- finalResponse.model = chunk.model || 'deepseek-reasoner'
212
- }
213
- const choice = chunk.choices[0]
214
- if (!choice) {
215
- continue
216
- }
217
- const delta = choice.delta
218
- if (!delta) {
219
- continue
220
- }
221
- // 2. 处理普通文本内容
222
- const reasoning_content = delta.reasoning_content
223
- if (reasoning_content) {
224
- if (finalResponse.choices[0].message.reasoning_content.length === 0) {
225
- streamThinkOutput('[Thinking...]')
226
- }
227
- finalResponse.choices[0].message.reasoning_content += reasoning_content
228
- // 流式输出
229
- streamThinkOutput(reasoning_content)
230
- }
231
- const content = delta.content
232
- if (content) {
233
- if (finalResponse.choices[0].message.content.length === 0) {
234
- streamContentOutput('\r\n')
235
- }
236
- finalResponse.choices[0].message.content += content
237
- // 流式输出
238
- streamContentOutput(content)
239
- }
240
- // 3. 处理工具调用(核心逻辑)
241
- if (delta.tool_calls && delta.tool_calls.length > 0) {
242
- delta.tool_calls.forEach((toolCallChunk) => {
243
- const index = toolCallChunk.index
244
- if (toolCallChunk.id) {
245
- const id = toolCallChunk.id
246
- toolCallIndexMap.set(index, id)
247
- let toolCall = toolCallBuffers.get(id)
248
- if (!toolCall) {
249
- toolCall = {
250
- id: id,
251
- type: toolCallChunk.type || 'function',
252
- function: {
253
- name: toolCallChunk.function.name,
254
- arguments: '',
255
- },
256
- }
257
- toolCallBuffers.set(id, toolCall)
258
- }
259
- } else {
260
- const id = toolCallIndexMap.get(index)
261
- const toolCall = toolCallBuffers.get(id)
262
- if (toolCall && toolCallChunk.function?.arguments) {
263
- if (toolCall.function.arguments.length === 0) {
264
- streamToolCallsOutput(`\r\n[ToolCalls...][${toolCall.function.name}]`)
265
- }
266
- toolCall.function.arguments += toolCallChunk.function.arguments
267
- streamToolCallsOutput(toolCallChunk.function.arguments)
268
- }
269
- }
270
- })
271
- }
272
-
273
- // 4. 处理结束标记
274
- if (choice.finish_reason) {
275
- finalResponse.choices[0].finish_reason = choice.finish_reason
276
-
277
- // 工具调用结束:将缓冲区数据写入最终响应
278
- if (choice.finish_reason === 'tool_calls' && toolCallBuffers.size > 0) {
279
- finalResponse.choices[0].message.content = '' // 工具调用时 content 为 null
280
- finalResponse.choices[0].message.tool_calls = Array.from(
281
- toolCallBuffers.values(),
282
- )
283
- } else {
284
- finalResponse.choices[0].message.tool_calls = undefined
285
- }
286
- }
287
- }
288
- streamEnd()
289
- return finalResponse
290
- } catch (error) {
291
- console.error('流式数据转换失败:', error.message)
292
- throw error // 抛出错误让上层处理
293
- }
294
- }
295
-
296
-
297
- module.exports = { creatClient, think, thinkByTool }
@@ -1,131 +0,0 @@
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
- this.toolCollection = AttachmentToolScanner.getToolCollection(
51
- this.agentRobot.workspace,
52
- this.agentRobot.basespace,
53
- ) // 加载工具集合
54
- this.clawSkillCollection = AttachmentToolScanner.getClawSkillCollection(
55
- this.agentRobot.basespace,
56
- ) // 加载Claw技能集合
57
- this.agentRobot.toolCollection = this.toolCollection
58
- this.agentRobot.clawSkillCollection = this.clawSkillCollection
59
- }
60
-
61
- // 动态添加工具
62
- addTool(toolName) {
63
- let tool = this.tools.find((t) => t.name === toolName)
64
- if (!tool) {
65
- tool = this.toolCollection.find((t) => t.name === toolName)
66
- if (tool) {
67
- this._addTool(tool)
68
- }
69
- }
70
- }
71
-
72
- _addTool(tool) {
73
- const platform = tool.platform || 'all'
74
- if (platform === 'all' || platform === process.platform) {
75
- tool.descriptions = tool.descriptions.map((item) => {
76
- if (!item.type) {
77
- item = {
78
- type: 'function',
79
- function: item,
80
- }
81
- }
82
- return item
83
- })
84
- tool.isSystem = true
85
- if (tool.name && !tool.isSystem) {
86
- for (const funcName in tool.functions) {
87
- if (!funcName.includes('_')) {
88
- this.functions[`${tool.name}_${funcName}`] =
89
- tool.functions[funcName]
90
- } else {
91
- this.functions[funcName] = tool.functions[funcName]
92
- }
93
- }
94
- const descriptions = tool.descriptions.map((item) => {
95
- if (
96
- tool.name &&
97
- item.function.name &&
98
- !item.function.name.includes('_')
99
- ) {
100
- item.function.name = `${tool.name}_${item.function.name}`
101
- return item
102
- } else {
103
- return item
104
- }
105
- })
106
- this.descriptions.push(...descriptions)
107
- } else {
108
- Object.assign(this.functions, tool.functions)
109
- this.descriptions.push(...tool.descriptions)
110
- }
111
- this.tools.push(tool)
112
- }
113
- }
114
-
115
- // 获取原装工具
116
- _getOriginalTools() {
117
- // 自动扫描tools目录
118
- const toolsPath = path.join(__dirname, '../tools')
119
- const toolFiles = fs.readdirSync(toolsPath).filter((file) => {
120
- return file.endsWith('.js') || file.endsWith('.cjs')
121
- })
122
- const tools = []
123
- toolFiles.forEach((file) => {
124
- const tool = require(path.join(toolsPath, file))
125
- tools.push(tool)
126
- })
127
- return tools
128
- }
129
- }
130
-
131
- module.exports = AIToolManager
@@ -1,66 +0,0 @@
1
- const path = require('path')
2
- const fs = require('fs-extra')
3
-
4
- class AgentTree {
5
- content = null
6
- constructor(agentRobot) {
7
- this.agentRobot = agentRobot
8
- this.agentTreeFilePath = path.join(
9
- this.agentRobot.agentSpace,
10
- 'agentTree.json',
11
- )
12
- this.parentAgentTree = this.agentRobot.parent?.agentTree
13
- this.rootAgentTree = this.agentRobot.root?.agentTree
14
- }
15
- init() {
16
- if (!this.rootAgentTree && !fs.pathExistsSync(this.agentTreeFilePath)) {
17
- fs.writeJsonSync(
18
- this.agentTreeFilePath,
19
- { agentId: this.agentRobot.id, children: [] },
20
- { spaces: 2 },
21
- )
22
- }
23
- if (!this.rootAgentTree) {
24
- this.content = fs.readJsonSync(this.agentTreeFilePath)
25
- } else {
26
- if (this.parentAgentTree && this.parentAgentTree.content.children) {
27
- this.content = this.parentAgentTree.content.children.find(
28
- (child) => child.agentId === this.agentRobot.id,
29
- )
30
- if (!this.content) {
31
- this.content = {
32
- agentId: this.agentRobot.id,
33
- children: [],
34
- type: this.agentRobot.type,
35
- }
36
- this.parentAgentTree.content.children.push(this.content)
37
- fs.writeJsonSync(this.agentTreeFilePath, this.rootAgentTree.content, {
38
- spaces: 2,
39
- })
40
- }
41
- }
42
- }
43
- }
44
-
45
- clear() {
46
- if (this.rootAgentTree) {
47
- if (this.parentAgentTree && this.parentAgentTree.content.children) {
48
- const currentNode = this.parentAgentTree.content.children.findIndex(
49
- (child) => child.agentId === this.agentRobot.id,
50
- )
51
- if (currentNode !== -1) {
52
- this.parentAgentTree.content.children.splice(currentNode, 1)
53
- fs.writeJsonSync(this.agentTreeFilePath, this.rootAgentTree.content, {
54
- spaces: 2,
55
- })
56
- }
57
- }
58
- // 清除子memory
59
- this.agentRobot.memoryFilePath &&
60
- fs.removeSync(this.agentRobot.memoryFilePath)
61
- }
62
- }
63
- }
64
-
65
- module.exports = AgentTree
66
-