nolo-cli 0.1.10 → 0.1.12

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 (232) hide show
  1. package/README.md +0 -32
  2. package/agentRuntimeCommands.ts +1 -1
  3. package/client/compactDialog.ts +2 -5
  4. package/commandRegistry.ts +2 -2
  5. package/machineCommands.ts +28 -3
  6. package/package.json +5 -25
  7. package/ai/agent/_executeModel.ts +0 -118
  8. package/ai/agent/agentSlice.ts +0 -525
  9. package/ai/agent/appWorkingMemory.ts +0 -126
  10. package/ai/agent/avatarUtils.ts +0 -24
  11. package/ai/agent/buildEditingContext.ts +0 -373
  12. package/ai/agent/buildSystemPrompt.ts +0 -532
  13. package/ai/agent/cleanAgentMessages.ts +0 -140
  14. package/ai/agent/cliChatClient.ts +0 -119
  15. package/ai/agent/contextCompiler.ts +0 -107
  16. package/ai/agent/contextLayerContract.ts +0 -44
  17. package/ai/agent/createAgentSchema.ts +0 -234
  18. package/ai/agent/executeToolCall.ts +0 -58
  19. package/ai/agent/fetchAgentContexts.ts +0 -42
  20. package/ai/agent/generatePrompt.ts +0 -3
  21. package/ai/agent/getFullChatContextKeys.ts +0 -168
  22. package/ai/agent/hooks/fetchPublicAgents.ts +0 -133
  23. package/ai/agent/hooks/useAgentConfig.ts +0 -61
  24. package/ai/agent/hooks/useAgentDialog.ts +0 -35
  25. package/ai/agent/hooks/useAgentFormValidation.ts +0 -202
  26. package/ai/agent/hooks/usePublicAgents.ts +0 -473
  27. package/ai/agent/persistMessageWithFixedId.ts +0 -37
  28. package/ai/agent/planSlice.ts +0 -259
  29. package/ai/agent/referenceUtils.ts +0 -229
  30. package/ai/agent/runAgentBackground.ts +0 -238
  31. package/ai/agent/runAgentClientLoop.ts +0 -138
  32. package/ai/agent/runtimeGuidance.ts +0 -97
  33. package/ai/agent/runtimeServerBase.ts +0 -37
  34. package/ai/agent/server/fetchPublicAgents.ts +0 -128
  35. package/ai/agent/startParallelAgentStreams.ts +0 -424
  36. package/ai/agent/startupProtocol.ts +0 -53
  37. package/ai/agent/streamAgentChatTurn.ts +0 -1278
  38. package/ai/agent/streamAgentChatTurnUtils.ts +0 -738
  39. package/ai/agent/types.ts +0 -71
  40. package/ai/agent/utils/imageOutput.ts +0 -33
  41. package/ai/agent/utils/sortUtils.ts +0 -250
  42. package/ai/agent/web/referencePickerUtils.ts +0 -146
  43. package/ai/ai.locale.ts +0 -1079
  44. package/ai/chat/accumulateToolCallChunks.ts +0 -95
  45. package/ai/chat/fetchUtils.native.ts +0 -276
  46. package/ai/chat/fetchUtils.ts +0 -153
  47. package/ai/chat/parseApiError.ts +0 -64
  48. package/ai/chat/parseMultilineSSE.ts +0 -95
  49. package/ai/chat/sendOpenAICompletionsRequest.native.ts +0 -682
  50. package/ai/chat/sendOpenAICompletionsRequest.ts +0 -703
  51. package/ai/chat/sendOpenAIResponseRequest.ts +0 -491
  52. package/ai/chat/shouldUseServerProxy.ts +0 -18
  53. package/ai/chat/sseClient.native.ts +0 -91
  54. package/ai/chat/sseClient.ts +0 -67
  55. package/ai/chat/streamReader.native.ts +0 -31
  56. package/ai/chat/streamReader.ts +0 -62
  57. package/ai/chat/updateTotalUsage.ts +0 -72
  58. package/ai/context/buildReferenceContext.ts +0 -437
  59. package/ai/context/calculateContextUsage.ts +0 -133
  60. package/ai/context/retention.ts +0 -165
  61. package/ai/context/tokenUtils.ts +0 -78
  62. package/ai/index.ts +0 -1
  63. package/ai/llm/calculateGeminiImageTokens.ts +0 -57
  64. package/ai/llm/deepinfra.ts +0 -28
  65. package/ai/llm/fireworks.ts +0 -50
  66. package/ai/llm/generateRequestBody.ts +0 -165
  67. package/ai/llm/getModelContextWindow.ts +0 -84
  68. package/ai/llm/getNoloKey.ts +0 -31
  69. package/ai/llm/getPricing.ts +0 -199
  70. package/ai/llm/hooks/useModelPricing.ts +0 -75
  71. package/ai/llm/imagePricing.ts +0 -40
  72. package/ai/llm/isResponseAPIModel.ts +0 -13
  73. package/ai/llm/mimo.ts +0 -71
  74. package/ai/llm/mistral.ts +0 -22
  75. package/ai/llm/modelAvatar.ts +0 -427
  76. package/ai/llm/models.ts +0 -45
  77. package/ai/llm/openrouterModels.ts +0 -269
  78. package/ai/llm/providers.ts +0 -306
  79. package/ai/llm/reasoningModels.ts +0 -28
  80. package/ai/llm/types.ts +0 -59
  81. package/ai/llm/usageRequestOptions.ts +0 -59
  82. package/ai/memory/capture.ts +0 -148
  83. package/ai/memory/consolidate.ts +0 -104
  84. package/ai/memory/delete.ts +0 -147
  85. package/ai/memory/overlay.ts +0 -84
  86. package/ai/memory/query.ts +0 -38
  87. package/ai/memory/queryShared.ts +0 -160
  88. package/ai/memory/rank.ts +0 -105
  89. package/ai/memory/recentRelationshipRecap.ts +0 -249
  90. package/ai/memory/remember.ts +0 -167
  91. package/ai/memory/runtime.ts +0 -76
  92. package/ai/memory/store.ts +0 -20
  93. package/ai/memory/storeShared.ts +0 -76
  94. package/ai/memory/types.ts +0 -46
  95. package/ai/memory/understanding.ts +0 -349
  96. package/ai/memory/understandingGreeting.ts +0 -264
  97. package/ai/messages/type.ts +0 -20
  98. package/ai/policy/personalizationDialog.ts +0 -333
  99. package/ai/policy/runtimePolicy.ts +0 -440
  100. package/ai/policy/selfUpdateFields.ts +0 -48
  101. package/ai/policy/types.ts +0 -64
  102. package/ai/skills/referenceRuntime.ts +0 -274
  103. package/ai/skills/skillDiagnostics.ts +0 -251
  104. package/ai/skills/skillDocBuilder.ts +0 -139
  105. package/ai/skills/skillDocProtocol.ts +0 -434
  106. package/ai/skills/skillReferenceSummary.ts +0 -63
  107. package/ai/skills/skillSummaryMarker.ts +0 -26
  108. package/ai/token/calculatePrice.ts +0 -544
  109. package/ai/token/db.ts +0 -98
  110. package/ai/token/externalToolCost.ts +0 -330
  111. package/ai/token/hooks/useRecords.ts +0 -65
  112. package/ai/token/missingUsageEstimate.ts +0 -42
  113. package/ai/token/modelUsageQuery.ts +0 -252
  114. package/ai/token/normalizeUsage.ts +0 -84
  115. package/ai/token/openaiImageGenerationUsage.ts +0 -56
  116. package/ai/token/prepareTokenUsageData.ts +0 -88
  117. package/ai/token/query.ts +0 -88
  118. package/ai/token/queryUserTokens.ts +0 -59
  119. package/ai/token/resolveBillingTarget.ts +0 -52
  120. package/ai/token/saveTokenRecord.ts +0 -53
  121. package/ai/token/serverDialogProjection.ts +0 -78
  122. package/ai/token/serverTokenWriter.ts +0 -143
  123. package/ai/token/stats.ts +0 -21
  124. package/ai/token/tokenThunks.ts +0 -24
  125. package/ai/token/types.ts +0 -93
  126. package/ai/tools/agent/agentTools.ts +0 -176
  127. package/ai/tools/agent/agentUpdateShared.ts +0 -311
  128. package/ai/tools/agent/callAgentTool.ts +0 -139
  129. package/ai/tools/agent/createAgentTool.ts +0 -512
  130. package/ai/tools/agent/createDialogTool.ts +0 -69
  131. package/ai/tools/agent/createSkillAgentTool.ts +0 -62
  132. package/ai/tools/agent/parallelBudget.ts +0 -221
  133. package/ai/tools/agent/presets/appBuilderPreset.ts +0 -145
  134. package/ai/tools/agent/runLlmTool.ts +0 -96
  135. package/ai/tools/agent/runStreamingAgentTool.ts +0 -73
  136. package/ai/tools/agent/skillAgentArgs.ts +0 -106
  137. package/ai/tools/agent/skillAgentPreset.ts +0 -89
  138. package/ai/tools/agent/streamParallelAgentsTool.ts +0 -122
  139. package/ai/tools/agent/updateAgentTool.ts +0 -96
  140. package/ai/tools/agent/updateSelfTool.ts +0 -113
  141. package/ai/tools/amazonProductScraperTool.ts +0 -86
  142. package/ai/tools/apifyActorClient.ts +0 -45
  143. package/ai/tools/appEditGuard.ts +0 -372
  144. package/ai/tools/appReadSnapshot.ts +0 -153
  145. package/ai/tools/appTools.ts +0 -1549
  146. package/ai/tools/applyEditTool.ts +0 -256
  147. package/ai/tools/applyLineEditsTool.ts +0 -312
  148. package/ai/tools/browserTools/click.ts +0 -33
  149. package/ai/tools/browserTools/closeSession.ts +0 -29
  150. package/ai/tools/browserTools/common.ts +0 -27
  151. package/ai/tools/browserTools/openSession.ts +0 -48
  152. package/ai/tools/browserTools/readContent.ts +0 -38
  153. package/ai/tools/browserTools/selectOption.ts +0 -46
  154. package/ai/tools/browserTools/typeText.ts +0 -42
  155. package/ai/tools/category/createCategoryTool.ts +0 -66
  156. package/ai/tools/category/queryContentsByCategoryTool.ts +0 -69
  157. package/ai/tools/category/updateContentCategoryTool.ts +0 -75
  158. package/ai/tools/cfBrowserTools.ts +0 -319
  159. package/ai/tools/cfSpeechToTextTool.ts +0 -49
  160. package/ai/tools/checkEnvTool.ts +0 -65
  161. package/ai/tools/cloudflareCrawlTool.ts +0 -289
  162. package/ai/tools/codeSearchTool.ts +0 -111
  163. package/ai/tools/codeTools.ts +0 -101
  164. package/ai/tools/createDocTool.ts +0 -132
  165. package/ai/tools/createPlanTool.ts +0 -999
  166. package/ai/tools/createSkillDocTool.ts +0 -155
  167. package/ai/tools/createWorkflowTool.ts +0 -154
  168. package/ai/tools/deepseekOcrTool.ts +0 -34
  169. package/ai/tools/delayTool.ts +0 -31
  170. package/ai/tools/deleteSpacesTool.ts +0 -325
  171. package/ai/tools/deleteSpacesToolModel.ts +0 -159
  172. package/ai/tools/devReloadUtils.ts +0 -29
  173. package/ai/tools/dialogMessageSearch.ts +0 -137
  174. package/ai/tools/doctorSkillTool.ts +0 -72
  175. package/ai/tools/ecommerceScraperTool.ts +0 -86
  176. package/ai/tools/emailTools.ts +0 -549
  177. package/ai/tools/evalSkillTool.ts +0 -92
  178. package/ai/tools/exaSearchTool.ts +0 -64
  179. package/ai/tools/execBashTool.ts +0 -379
  180. package/ai/tools/executeSqlTool.ts +0 -192
  181. package/ai/tools/fetchWebpageSupport.ts +0 -309
  182. package/ai/tools/fetchWebpageTool.ts +0 -84
  183. package/ai/tools/geminiImagePreviewTool.ts +0 -361
  184. package/ai/tools/generateDocxTool.ts +0 -215
  185. package/ai/tools/googleSearchScraperTool.ts +0 -106
  186. package/ai/tools/importDataTool.ts +0 -133
  187. package/ai/tools/importSkillTool.ts +0 -162
  188. package/ai/tools/index.ts +0 -1858
  189. package/ai/tools/listFilesTool.ts +0 -82
  190. package/ai/tools/listUserSpacesTool.ts +0 -113
  191. package/ai/tools/modelUsageTools.ts +0 -142
  192. package/ai/tools/olmOcrTool.ts +0 -34
  193. package/ai/tools/openaiImageTool.ts +0 -218
  194. package/ai/tools/paddleOcrTool.ts +0 -34
  195. package/ai/tools/prepareTools.ts +0 -23
  196. package/ai/tools/readDocTool.ts +0 -84
  197. package/ai/tools/readFileTool.ts +0 -211
  198. package/ai/tools/readTool.ts +0 -163
  199. package/ai/tools/readXPostTool.ts +0 -233
  200. package/ai/tools/rememberMemoryTool.ts +0 -84
  201. package/ai/tools/remotionVideoTool.ts +0 -151
  202. package/ai/tools/searchDialogMessagesTool.ts +0 -222
  203. package/ai/tools/searchRepoTool.ts +0 -115
  204. package/ai/tools/searchWorkspaceTool.ts +0 -259
  205. package/ai/tools/skillFollowup.ts +0 -86
  206. package/ai/tools/surfWeatherTool.ts +0 -169
  207. package/ai/tools/table/addTableRowTool.ts +0 -217
  208. package/ai/tools/table/createTableTool.ts +0 -315
  209. package/ai/tools/table/rowTools.ts +0 -366
  210. package/ai/tools/table/schemaTools.ts +0 -244
  211. package/ai/tools/table/shareTableTool.ts +0 -148
  212. package/ai/tools/table/toolShared.ts +0 -129
  213. package/ai/tools/toolApiClient.ts +0 -198
  214. package/ai/tools/toolNameAliases.ts +0 -57
  215. package/ai/tools/toolResultError.ts +0 -42
  216. package/ai/tools/toolRunSlice.ts +0 -303
  217. package/ai/tools/toolSchemaCompatibility.ts +0 -53
  218. package/ai/tools/toolVisibility.ts +0 -4
  219. package/ai/tools/types.ts +0 -20
  220. package/ai/tools/uiAskChoiceTool.ts +0 -104
  221. package/ai/tools/updateContentTitleTool.ts +0 -84
  222. package/ai/tools/updateDocTool.ts +0 -105
  223. package/ai/tools/updateUserPreferenceProfileTool.ts +0 -145
  224. package/ai/tools/whisperTool.ts +0 -77
  225. package/ai/tools/writeFileTool.ts +0 -210
  226. package/ai/tools/youtubeScraperTool.ts +0 -116
  227. package/ai/tools/ziweiChartTool.ts +0 -678
  228. package/ai/types.ts +0 -55
  229. package/ai/workflow/workflowExecutor.ts +0 -323
  230. package/ai/workflow/workflowSlice.ts +0 -73
  231. package/ai/workflow/workflowTypes.ts +0 -106
  232. package/connector-experimental/index.ts +0 -5
@@ -1,379 +0,0 @@
1
- import { DEFAULT_LOCAL_API_ORIGIN } from "core/localOrigins";
2
- import { callToolApi } from "./toolApiClient";
3
-
4
- type ExecShellKind = "auto" | "bash" | "powershell";
5
-
6
- type ExecBashRequestArgs = {
7
- command?: string;
8
- cwd?: string;
9
- interactive?: boolean;
10
- pty?: boolean;
11
- shell?: ExecShellKind;
12
- sessionId?: string;
13
- input?: string;
14
- close?: boolean;
15
- };
16
-
17
- export const execBashFunctionSchema = {
18
- name: "execBash",
19
- description:
20
- "兼容旧 agent 的 shell 执行工具。底层会调用后端 shell 执行接口;在 Windows 上可能自动回退到 PowerShell。环境不明确时,先调用 checkEnv({check:'context'}) 判断当前平台与可用 shell。支持交互式 session:先用 interactive=true 启动,再通过 sessionId + input 继续写入 stdin,或用 sessionId 单独轮询输出。仅用于本地开发环境。对 rm 等危险命令默认拦截,需显式传 unsafe: true 才会执行。不要用它执行 rg/grep 代码搜索;代码搜索请改用 codeSearch。",
21
- parameters: {
22
- type: "object",
23
- properties: {
24
- command: {
25
- type: "string",
26
- description:
27
- "要执行的 shell 命令,例如: 'git status --short', 'bun test --watch=false'。",
28
- },
29
- cwd: {
30
- type: "string",
31
- description:
32
- "可选:命令执行的工作目录(相对于 diff-server 所在机器),默认使用后端的 process.cwd()。",
33
- },
34
- unsafe: {
35
- type: "boolean",
36
- description:
37
- "可选:是否允许执行被判定为危险的命令(例如 rm -rf)。默认 false;当为 false 且命令被识别为危险时,会直接拦截而不真正执行。",
38
- },
39
- interactive: {
40
- type: "boolean",
41
- description:
42
- "可选:是否以交互式 session 启动命令。为 true 时接口会立即返回 sessionId,后续可继续写 stdin 或轮询输出。",
43
- },
44
- pty: {
45
- type: "boolean",
46
- description:
47
- "可选:是否为交互式 session 分配伪终端(PTY)。适合 python -i、bash、node 等真正依赖终端语义的 REPL。",
48
- },
49
- sessionId: {
50
- type: "string",
51
- description:
52
- "可选:已存在的交互式 sessionId。传入后可继续写入 input、查询最新输出或关闭该 session。",
53
- },
54
- input: {
55
- type: "string",
56
- description:
57
- "可选:要写入 session stdin 的文本。通常需要自己补换行,例如 'y\\n' 或 'exit\\n'。",
58
- },
59
- close: {
60
- type: "boolean",
61
- description:
62
- "可选:是否关闭指定 session。通常与 sessionId 搭配使用。",
63
- },
64
- },
65
- },
66
- };
67
-
68
- export const execShellFunctionSchema = {
69
- name: "execShell",
70
- description:
71
- "跨平台 shell 执行工具。会在 Windows 上优先使用 PowerShell,在 Linux/macOS 上使用 bash。环境不明确时,先调用 checkEnv({check:'context'}) 判断当前平台与可用 shell。支持交互式 session:先用 interactive=true 启动,再通过 sessionId + input 继续写入 stdin,或用 sessionId 单独轮询输出。仅用于本地开发环境。对 rm 等危险命令默认拦截,需显式传 unsafe: true 才会执行。不要用它执行 rg/grep 代码搜索;代码搜索请改用 codeSearch。",
72
- parameters: {
73
- type: "object",
74
- properties: {
75
- command: {
76
- type: "string",
77
- description:
78
- "要执行的 shell 命令,例如: 'git status --short', 'Get-Location', 'bun test --watch=false'。",
79
- },
80
- cwd: {
81
- type: "string",
82
- description:
83
- "可选:命令执行的工作目录(相对于 diff-server 所在机器),默认使用后端的 process.cwd()。",
84
- },
85
- shell: {
86
- type: "string",
87
- enum: ["auto", "bash", "powershell"],
88
- description:
89
- "可选:显式指定 shell。默认 auto:Windows 优先 PowerShell,其他系统使用 bash。",
90
- },
91
- unsafe: {
92
- type: "boolean",
93
- description:
94
- "可选:是否允许执行被判定为危险的命令(例如 rm -rf)。默认 false;当为 false 且命令被识别为危险时,会直接拦截而不真正执行。",
95
- },
96
- interactive: {
97
- type: "boolean",
98
- description:
99
- "可选:是否以交互式 session 启动命令。为 true 时接口会立即返回 sessionId,后续可继续写 stdin 或轮询输出。",
100
- },
101
- pty: {
102
- type: "boolean",
103
- description:
104
- "可选:是否为交互式 session 分配伪终端(PTY)。当前仅 bash 路径支持;Windows PowerShell 会自动降级为非 PTY 交互。",
105
- },
106
- sessionId: {
107
- type: "string",
108
- description:
109
- "可选:已存在的交互式 sessionId。传入后可继续写入 input、查询最新输出或关闭该 session。",
110
- },
111
- input: {
112
- type: "string",
113
- description:
114
- "可选:要写入 session stdin 的文本。通常需要自己补换行,例如 'y\\n' 或 'exit\\n'。",
115
- },
116
- close: {
117
- type: "boolean",
118
- description:
119
- "可选:是否关闭指定 session。通常与 sessionId 搭配使用。",
120
- },
121
- },
122
- },
123
- };
124
-
125
- function isDangerousCommand(rawCommand: string): boolean {
126
- const command = rawCommand.trim();
127
- const normalized = command.replace(/\s+/g, " ");
128
-
129
- if (
130
- /^rm\b/i.test(normalized) ||
131
- /\bsudo\s+rm\b/i.test(normalized) ||
132
- /\brm\s+-[rf]+\b/i.test(normalized) ||
133
- /\brm\s+-[^\s]*r[^\s]*f[^\s]*/i.test(normalized)
134
- ) {
135
- return true;
136
- }
137
-
138
- if (/\brm\s+-[^\s]*f[^\s]*/i.test(normalized)) {
139
- return true;
140
- }
141
-
142
- return false;
143
- }
144
-
145
- async function requestExec(
146
- args: ExecBashRequestArgs,
147
- thunkApi?: any,
148
- endpoint = "/api/exec-bash",
149
- ): Promise<any> {
150
- const payload = Object.fromEntries(
151
- Object.entries(args).filter(([, value]) => value !== undefined)
152
- );
153
-
154
- if (thunkApi?.getState) {
155
- return callToolApi(thunkApi, endpoint, payload);
156
- }
157
-
158
- const baseUrl =
159
- (typeof process !== "undefined" &&
160
- (process as any).env?.DIFF_SERVER_ORIGIN) ||
161
- (typeof window !== "undefined" ? window.location.origin : DEFAULT_LOCAL_API_ORIGIN);
162
- const url = `${baseUrl}${endpoint}`;
163
- const res = await fetch(url, {
164
- method: "POST",
165
- headers: { "Content-Type": "application/json" },
166
- body: JSON.stringify(payload),
167
- });
168
-
169
- const text = await res.text();
170
- try {
171
- return text ? JSON.parse(text) : {};
172
- } catch {
173
- return { raw: text, ok: res.ok, status: res.status };
174
- }
175
- }
176
-
177
- function formatExecResult(prefixLabel: string, args: any, json: any, trimmedCommand: string, sessionId?: string) {
178
- const prefix = json?.ok === false || json?.error ? `[${prefixLabel} 失败]` : `[${prefixLabel} 成功]`;
179
- const stdout = json.stdout ?? "";
180
- const stderr = json.stderr ?? "";
181
- const exitCode = json.exitCode ?? json.code ?? null;
182
- const responseSessionId = json.sessionId ?? sessionId ?? null;
183
- const responseInteractive = json.interactive === true;
184
- const responseRunning = json.running;
185
-
186
- const displayParts: string[] = [];
187
- displayParts.push(`${prefix} command: ${json.command ?? trimmedCommand ?? "(session follow-up)"}`);
188
- if (json.shell ?? args.shell) {
189
- displayParts.push(`shell: ${json.shell ?? args.shell}`);
190
- }
191
- if (args.cwd) {
192
- displayParts.push(`cwd: ${args.cwd}`);
193
- }
194
- if (responseSessionId) {
195
- displayParts.push(`sessionId: ${responseSessionId}`);
196
- }
197
- if (responseInteractive) {
198
- displayParts.push(`running: ${responseRunning === false ? "false" : "true"}`);
199
- }
200
- if (exitCode !== null && exitCode !== undefined) {
201
- displayParts.push(`exitCode: ${exitCode}`);
202
- }
203
- if (stdout) {
204
- displayParts.push("\n[stdout]\n" + stdout.trim());
205
- }
206
- if (stderr) {
207
- displayParts.push("\n[stderr]\n" + stderr.trim());
208
- }
209
-
210
- return {
211
- rawData: {
212
- command: trimmedCommand || null,
213
- cwd: args.cwd ?? null,
214
- shell: args.shell ?? "auto",
215
- ...json,
216
- },
217
- displayData: displayParts.join("\n"),
218
- };
219
- }
220
-
221
- function buildBlockedResponse(toolName: "execBash" | "execShell", trimmedCommand: string, cwd?: string, shell?: ExecShellKind) {
222
- const lines: string[] = [];
223
- lines.push(`[${toolName} 拦截] 检测到潜在危险命令,默认不执行。`);
224
- lines.push(`command: ${trimmedCommand}`);
225
- if (cwd) {
226
- lines.push(`cwd: ${cwd}`);
227
- }
228
- lines.push(
229
- "如果你确实确认需要执行,请在界面中手动确认或在工具参数中显式传入 unsafe: true(仍建议先检查命令是否有误)。"
230
- );
231
-
232
- return {
233
- rawData: {
234
- applied: false,
235
- blocked: true,
236
- reason: "dangerous_command",
237
- requireUnsafe: true,
238
- command: trimmedCommand,
239
- cwd: cwd ?? null,
240
- shell: shell ?? "auto",
241
- },
242
- displayData: lines.join("\n"),
243
- };
244
- }
245
-
246
- export const startExecBashSession = (
247
- thunkApi: any,
248
- args: { command: string; cwd?: string; pty?: boolean }
249
- ) =>
250
- requestExec(
251
- {
252
- command: args.command,
253
- cwd: args.cwd,
254
- interactive: true,
255
- pty: args.pty,
256
- },
257
- thunkApi,
258
- );
259
-
260
- export const writeExecBashSession = (
261
- thunkApi: any,
262
- args: { sessionId: string; input: string }
263
- ) =>
264
- requestExec(
265
- {
266
- sessionId: args.sessionId,
267
- input: args.input,
268
- },
269
- thunkApi,
270
- );
271
-
272
- export const readExecBashSession = (
273
- thunkApi: any,
274
- args: { sessionId: string }
275
- ) =>
276
- requestExec(
277
- {
278
- sessionId: args.sessionId,
279
- },
280
- thunkApi,
281
- );
282
-
283
- export const closeExecBashSession = (
284
- thunkApi: any,
285
- args: { sessionId: string; input?: string }
286
- ) =>
287
- requestExec(
288
- {
289
- sessionId: args.sessionId,
290
- input: args.input,
291
- close: true,
292
- },
293
- thunkApi,
294
- );
295
-
296
- export const startExecShellSession = (
297
- thunkApi: any,
298
- args: { command: string; cwd?: string; pty?: boolean; shell?: ExecShellKind }
299
- ) =>
300
- requestExec(
301
- {
302
- command: args.command,
303
- cwd: args.cwd,
304
- interactive: true,
305
- pty: args.pty,
306
- shell: args.shell,
307
- },
308
- thunkApi,
309
- "/api/exec-shell",
310
- );
311
-
312
- export async function execBashFunc(args: any): Promise<{
313
- rawData: any;
314
- displayData: string;
315
- }> {
316
- const { command, cwd, unsafe, interactive, pty, sessionId, input, close } = args || {};
317
- const isSessionFollowup = typeof sessionId === "string" && sessionId.trim().length > 0;
318
-
319
- if (!isSessionFollowup && (!command || typeof command !== "string")) {
320
- const msg = "execBash 需要提供 command,或提供有效的 sessionId 来继续已有交互式 session。";
321
- return { rawData: { error: msg }, displayData: msg };
322
- }
323
-
324
- const trimmedCommand = typeof command === "string" ? command.trim() : "";
325
- if (!isSessionFollowup && !unsafe && isDangerousCommand(trimmedCommand)) {
326
- return buildBlockedResponse("execBash", trimmedCommand, cwd);
327
- }
328
-
329
- try {
330
- const json = await requestExec({
331
- ...(trimmedCommand ? { command: trimmedCommand } : {}),
332
- ...(cwd ? { cwd } : {}),
333
- ...(interactive ? { interactive: true } : {}),
334
- ...(pty ? { pty: true } : {}),
335
- ...(isSessionFollowup ? { sessionId } : {}),
336
- ...(typeof input === "string" ? { input } : {}),
337
- ...(close ? { close: true } : {}),
338
- });
339
- return formatExecResult("execBash", { cwd }, json, trimmedCommand, sessionId);
340
- } catch (error: any) {
341
- const msg = `execBash 调用失败:${error?.message || String(error)}`;
342
- return { rawData: { error: msg }, displayData: msg };
343
- }
344
- }
345
-
346
- export async function execShellFunc(args: any): Promise<{
347
- rawData: any;
348
- displayData: string;
349
- }> {
350
- const { command, cwd, shell, unsafe, interactive, pty, sessionId, input, close } = args || {};
351
- const isSessionFollowup = typeof sessionId === "string" && sessionId.trim().length > 0;
352
-
353
- if (!isSessionFollowup && (!command || typeof command !== "string")) {
354
- const msg = "execShell 需要提供 command,或提供有效的 sessionId 来继续已有交互式 session。";
355
- return { rawData: { error: msg }, displayData: msg };
356
- }
357
-
358
- const trimmedCommand = typeof command === "string" ? command.trim() : "";
359
- if (!isSessionFollowup && !unsafe && isDangerousCommand(trimmedCommand)) {
360
- return buildBlockedResponse("execShell", trimmedCommand, cwd, shell);
361
- }
362
-
363
- try {
364
- const json = await requestExec({
365
- ...(trimmedCommand ? { command: trimmedCommand } : {}),
366
- ...(cwd ? { cwd } : {}),
367
- ...(shell ? { shell } : {}),
368
- ...(interactive ? { interactive: true } : {}),
369
- ...(pty ? { pty: true } : {}),
370
- ...(isSessionFollowup ? { sessionId } : {}),
371
- ...(typeof input === "string" ? { input } : {}),
372
- ...(close ? { close: true } : {}),
373
- }, undefined, "/api/exec-shell");
374
- return formatExecResult("execShell", { cwd, shell }, json, trimmedCommand, sessionId);
375
- } catch (error: any) {
376
- const msg = `execShell 调用失败:${error?.message || String(error)}`;
377
- return { rawData: { error: msg }, displayData: msg };
378
- }
379
- }
@@ -1,192 +0,0 @@
1
- // /ai/tools/executeSqlTool.ts
2
-
3
- import { getToolRequestContext, callToolApi } from "./toolApiClient";
4
- import { API_ENDPOINTS } from "database/config";
5
-
6
- /**
7
- * [Schema] 定义了 'executeSql' 工具的结构,供 LLM 调用。
8
- * [修改] Schema 增加对参数化查询的描述,但对 LLM 保持简单,不暴露 params 字段。
9
- * LLM 应该总是生成完整的、人类可读的 SQL。参数化是由内部工具(如 importData)调用的。
10
- */
11
- export const executeSqlFunctionSchema = {
12
- name: "executeSql",
13
- description:
14
- "直接在 SQLite 数据库中执行任意 SQL 语句,包括查询、插入、更新、删除和表结构操作。内部支持参数化查询以防止SQL注入。",
15
- parameters: {
16
- type: "object",
17
- properties: {
18
- sqlQuery: {
19
- type: "string",
20
- description:
21
- "要执行的完整 SQL 语句。例如:SELECT * FROM users; INSERT INTO products (name, price) VALUES ('Milk', 2.5);",
22
- },
23
- // 注意:params 字段不向 LLM 暴露,这是内部实现细节
24
- },
25
- required: ["sqlQuery"],
26
- },
27
- };
28
-
29
- // buildRequestHeaders 和 postRequest / handleApiResponse 已统一至 toolApiClient
30
-
31
- const buildRequestHeaders = (token: string | null): Record<string, string> => {
32
- const headers: Record<string, string> = { "Content-Type": "application/json" };
33
- if (token) headers["Authorization"] = `Bearer ${token}`;
34
- return headers;
35
- };
36
-
37
- const handleApiResponse = async (response: Response): Promise<any> => {
38
- if (!response.ok) {
39
- let errorMessage = `API 错误!状态码: ${response.status}`;
40
- try {
41
- const errorData = await response.json() as Record<string, any>;
42
- if (errorData?.error) {
43
- errorMessage += `: ${errorData.error.message || JSON.stringify(errorData.error)}`;
44
- }
45
- } catch (e) {
46
- /* 忽略json解析错误 */
47
- }
48
- throw new Error(errorMessage);
49
- }
50
- return response.json();
51
- };
52
-
53
- const formatSqlResultText = (
54
- data: any,
55
- sqlQuery: string,
56
- params?: any[]
57
- ): string => {
58
- // ... (此函数大部分逻辑保持不变)
59
- if (!data.success) return `SQL 执行失败:${data.error || "未知错误"}`;
60
- const result = data.result;
61
-
62
- // [新增] 如果是参数化 INSERT,提供更精确的反馈
63
- if (
64
- params &&
65
- params.length > 0 &&
66
- sqlQuery.trim().toLowerCase().startsWith("insert")
67
- ) {
68
- const changes = result?.changes ?? 0;
69
- const match = sqlQuery.match(/insert\s+into\s+['"]?(\w+)['"]?/i);
70
- const tableName = match ? ` "${match[1]}"` : "";
71
- if (changes > 0) {
72
- return `✅ 成功向表${tableName}中批量插入了 ${changes} 条记录。`;
73
- }
74
- }
75
-
76
- // ... (其余格式化逻辑与之前相同)
77
- const lowerCaseQuery = sqlQuery.trim().toLowerCase();
78
-
79
- if (lowerCaseQuery.startsWith("create table")) {
80
- const match = sqlQuery.match(
81
- /create\s+table\s+(?:if\s+not\s+exists\s+)?['"]?(\w+)['"]?/i
82
- );
83
- const tableName = match ? match[1] : "新";
84
- return `✅ 表 "${tableName}" 已成功创建。`;
85
- }
86
-
87
- if (Array.isArray(result)) {
88
- const isListTablesQuery =
89
- lowerCaseQuery.includes("select") &&
90
- lowerCaseQuery.includes("name") &&
91
- lowerCaseQuery.includes("sqlite_master") &&
92
- lowerCaseQuery.includes("type") &&
93
- lowerCaseQuery.includes("'table'");
94
- if (
95
- isListTablesQuery &&
96
- result.length > 0 &&
97
- result[0].name !== undefined
98
- ) {
99
- const tableNames = result
100
- .map((row: any) => `- \`${row.name}\``)
101
- .join("\n");
102
- return `查询成功,数据库中共有 ${result.length} 个表:\n\n${tableNames}`;
103
- }
104
- if (lowerCaseQuery.startsWith("pragma table_info(")) {
105
- const match = sqlQuery.match(/\(\s*['"]?(\w+)['"]?\s*\)/);
106
- const tableName = match ? ` "${match[1]}"` : "";
107
- if (result.length > 0 && result[0].cid !== undefined) {
108
- let resultText = `表${tableName} 的结构信息 (${result.length} 列):\n\n\`\`\`\n| 列名 | 类型 | 非空 | 主键 |\n|-------------|-----------|------|------|\n`;
109
- result.forEach((col: any) => {
110
- resultText += `| ${String(col.name).padEnd(11)} | ${String(col.type).padEnd(9)} | ${(col.notnull ? "是" : "否").padEnd(4)} | ${(col.pk ? "是" : "否").padEnd(4)} |\n`;
111
- });
112
- resultText += "```";
113
- return resultText;
114
- } else {
115
- return `表${tableName} 不存在或没有列信息。`;
116
- }
117
- }
118
- if (
119
- lowerCaseQuery.includes("sqlite_master") &&
120
- lowerCaseQuery.includes("select sql") &&
121
- result.length > 0 &&
122
- result[0].sql !== undefined
123
- ) {
124
- return `表创建 SQL 语句:\n\n\`\`\`sql\n${result[0].sql}\n\`\`\``;
125
- }
126
- let resultText = `SQL 查询成功,返回 ${result.length} 条记录。\n\n`;
127
- if (result.length > 0) {
128
- resultText +=
129
- "```json\n" + JSON.stringify(result.slice(0, 5), null, 2) + "\n```";
130
- if (result.length > 5)
131
- resultText += `\n... 更多 ${result.length - 5} 条记录未显示。`;
132
- } else {
133
- resultText += "没有找到匹配的记录。";
134
- }
135
- return resultText;
136
- } else if (typeof result === "object" && result !== null) {
137
- if (result.message) return `SQL 命令执行成功:${result.message}`;
138
- if (result.changes !== undefined || result.lastInsertRowid !== undefined) {
139
- let resultText = `SQL 命令执行成功。`;
140
- if (result.changes > 0) resultText += ` 影响行数:${result.changes}。`;
141
- if (result.lastInsertRowid)
142
- resultText += ` 最后插入ID:${result.lastInsertRowid}。`;
143
- return resultText;
144
- }
145
- return `SQL 命令执行成功,原始响应:${JSON.stringify(result)}`;
146
- }
147
- return `SQL 命令执行成功,原始响应:${JSON.stringify(result)}`;
148
- };
149
-
150
- /**
151
- * [Executor] 'executeSql' 工具的执行函数。
152
- * [修改] 函数签名和请求体以支持可选的参数化查询。
153
- * @param args - LLM 或内部工具提供的参数: { sqlQuery: string, params?: any[] }
154
- * @param thunkApi - Redux Thunk API
155
- * @returns {Promise<{rawData: any, displayData: string}>} - 返回标准化的结果对象
156
- */
157
- export async function executeSqlFunc(
158
- args: { sqlQuery: string; params?: any[] }, // <--- [修改] 签名接受可选的 params
159
- thunkApi: any
160
- ): Promise<{ rawData: any; displayData: string }> {
161
- const { sqlQuery, params } = args;
162
-
163
- if (!sqlQuery || typeof sqlQuery !== "string" || sqlQuery.trim() === "") {
164
- throw new Error("SQL 执行失败:'sqlQuery' 必须为非空字符串。");
165
- }
166
-
167
- try {
168
- const { baseUrl, token } = getToolRequestContext(thunkApi);
169
- const apiUrl = `${baseUrl}${API_ENDPOINTS.EXECUTE_SQL}`;
170
- const requestHeaders = buildRequestHeaders(token);
171
-
172
- const requestBody: { sql_query: string; params?: any[] } = {
173
- sql_query: sqlQuery,
174
- };
175
- if (params && Array.isArray(params)) {
176
- requestBody.params = params;
177
- }
178
-
179
- const response = await fetch(apiUrl, { method: "POST", headers: requestHeaders, body: JSON.stringify(requestBody) });
180
- const responseData = await handleApiResponse(response);
181
-
182
- // 生成用于UI展示的文本和用于后续步骤的原始数据
183
- const displayData = formatSqlResultText(responseData, sqlQuery, params);
184
- const rawData = responseData; // 完整的后端响应作为原始数据
185
-
186
- return { rawData, displayData };
187
- } catch (error: any) {
188
- console.error("Execute SQL tool error:", error);
189
- // 重新抛出格式化的错误,以便上层捕获并显示
190
- throw new Error(`SQL 执行失败:${error.message}`);
191
- }
192
- }