nolo-cli 0.1.13 → 0.1.15

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 (321) hide show
  1. package/README.md +9 -2
  2. package/agent-runtime/hostAdapter.ts +53 -0
  3. package/agent-runtime/index.ts +28 -0
  4. package/agent-runtime/localLoop.ts +62 -0
  5. package/agent-runtime/runtimeDecision.ts +70 -0
  6. package/agent-runtime/types.ts +87 -0
  7. package/agentRunCommand.ts +104 -0
  8. package/agentRuntimeCommands.ts +139 -22
  9. package/agentRuntimeLocal.ts +7 -0
  10. package/ai/agent/_executeModel.ts +118 -0
  11. package/ai/agent/agentSlice.ts +544 -1
  12. package/ai/agent/appWorkingMemory.ts +126 -0
  13. package/ai/agent/avatarUtils.ts +24 -0
  14. package/ai/agent/buildEditingContext.ts +373 -0
  15. package/ai/agent/buildSystemPrompt.ts +532 -0
  16. package/ai/agent/cleanAgentMessages.ts +140 -0
  17. package/ai/agent/cliChatClient.ts +119 -0
  18. package/ai/agent/contextCompiler.ts +107 -0
  19. package/ai/agent/contextLayerContract.ts +44 -0
  20. package/ai/agent/createAgentSchema.ts +234 -0
  21. package/ai/agent/executeToolCall.ts +58 -0
  22. package/ai/agent/fetchAgentContexts.ts +42 -0
  23. package/ai/agent/generatePrompt.ts +3 -0
  24. package/ai/agent/getFullChatContextKeys.ts +168 -0
  25. package/ai/agent/hooks/fetchPublicAgents.ts +133 -0
  26. package/ai/agent/hooks/useAgentConfig.ts +61 -0
  27. package/ai/agent/hooks/useAgentDialog.ts +35 -0
  28. package/ai/agent/hooks/useAgentFormValidation.ts +202 -0
  29. package/ai/agent/hooks/usePublicAgents.ts +473 -0
  30. package/ai/agent/persistMessageWithFixedId.ts +37 -0
  31. package/ai/agent/planSlice.ts +259 -0
  32. package/ai/agent/referenceUtils.ts +229 -0
  33. package/ai/agent/runAgentBackground.ts +238 -0
  34. package/ai/agent/runAgentClientLoop.ts +138 -0
  35. package/ai/agent/runtimeGuidance.ts +97 -0
  36. package/ai/agent/runtimeServerBase.ts +37 -0
  37. package/ai/agent/server/fetchPublicAgents.ts +128 -0
  38. package/ai/agent/startParallelAgentStreams.ts +424 -0
  39. package/ai/agent/startupProtocol.ts +53 -0
  40. package/ai/agent/streamAgentChatTurn.ts +1299 -0
  41. package/ai/agent/streamAgentChatTurnUtils.ts +738 -0
  42. package/ai/agent/types.ts +71 -0
  43. package/ai/agent/utils/imageOutput.ts +39 -0
  44. package/ai/agent/utils/publicImageAgentMode.ts +26 -0
  45. package/ai/agent/utils/sortUtils.ts +250 -0
  46. package/ai/agent/web/referencePickerUtils.ts +146 -0
  47. package/ai/ai.locale.ts +1083 -0
  48. package/ai/chat/accumulateToolCallChunks.ts +95 -0
  49. package/ai/chat/fetchUtils.native.ts +276 -0
  50. package/ai/chat/fetchUtils.ts +153 -0
  51. package/ai/chat/inlineImageUrlsForCustomProvider.ts +117 -0
  52. package/ai/chat/parseApiError.ts +64 -0
  53. package/ai/chat/parseMultilineSSE.ts +95 -0
  54. package/ai/chat/sendOpenAICompletionsRequest.native.ts +682 -0
  55. package/ai/chat/sendOpenAICompletionsRequest.ts +712 -0
  56. package/ai/chat/sendOpenAIResponseRequest.ts +512 -0
  57. package/ai/chat/shouldUseServerProxy.ts +18 -0
  58. package/ai/chat/sseClient.native.ts +91 -0
  59. package/ai/chat/sseClient.ts +67 -0
  60. package/ai/chat/streamReader.native.ts +31 -0
  61. package/ai/chat/streamReader.ts +62 -0
  62. package/ai/chat/updateTotalUsage.ts +72 -0
  63. package/ai/context/buildReferenceContext.ts +437 -0
  64. package/ai/context/calculateContextUsage.ts +133 -0
  65. package/ai/context/retention.ts +165 -0
  66. package/ai/context/tokenUtils.ts +78 -0
  67. package/ai/index.ts +1 -1
  68. package/ai/llm/agentCapabilities.ts +74 -0
  69. package/ai/llm/calculateGeminiImageTokens.ts +57 -0
  70. package/ai/llm/deepinfra.ts +28 -0
  71. package/ai/llm/fireworks.ts +68 -0
  72. package/ai/llm/generateRequestBody.ts +165 -0
  73. package/ai/llm/getModelContextWindow.ts +84 -0
  74. package/ai/llm/getNoloKey.ts +37 -0
  75. package/ai/llm/getPricing.ts +232 -0
  76. package/ai/llm/hooks/useModelPricing.ts +75 -0
  77. package/ai/llm/imagePricing.ts +66 -0
  78. package/ai/llm/isResponseAPIModel.ts +13 -0
  79. package/ai/llm/kimi.ts +18 -0
  80. package/ai/llm/mimo.ts +71 -0
  81. package/ai/llm/mistral.ts +22 -0
  82. package/ai/llm/modelAvatar.ts +427 -0
  83. package/ai/llm/models.ts +45 -0
  84. package/ai/llm/openrouterModels.ts +141 -0
  85. package/ai/llm/providers.ts +307 -0
  86. package/ai/llm/reasoningModels.ts +28 -0
  87. package/ai/llm/types.ts +59 -0
  88. package/ai/llm/usageRequestOptions.ts +59 -0
  89. package/ai/memory/capture.ts +148 -0
  90. package/ai/memory/consolidate.ts +104 -0
  91. package/ai/memory/delete.ts +147 -0
  92. package/ai/memory/overlay.ts +84 -0
  93. package/ai/memory/query.ts +38 -0
  94. package/ai/memory/queryShared.ts +160 -0
  95. package/ai/memory/rank.ts +105 -0
  96. package/ai/memory/recentRelationshipRecap.ts +247 -0
  97. package/ai/memory/remember.ts +167 -0
  98. package/ai/memory/runtime.ts +76 -0
  99. package/ai/memory/store.ts +20 -0
  100. package/ai/memory/storeShared.ts +76 -0
  101. package/ai/memory/types.ts +46 -0
  102. package/ai/memory/understanding.ts +349 -0
  103. package/ai/memory/understandingGreeting.ts +264 -0
  104. package/ai/messages/type.ts +20 -0
  105. package/ai/policy/personalizationDialog.ts +333 -0
  106. package/ai/policy/runtimePolicy.ts +440 -0
  107. package/ai/policy/selfUpdateFields.ts +48 -0
  108. package/ai/policy/types.ts +64 -0
  109. package/ai/skills/referenceRuntime.ts +274 -0
  110. package/ai/skills/skillDiagnostics.ts +251 -0
  111. package/ai/skills/skillDocBuilder.ts +139 -0
  112. package/ai/skills/skillDocProtocol.ts +434 -0
  113. package/ai/skills/skillReferenceSummary.ts +63 -0
  114. package/ai/skills/skillSummaryMarker.ts +26 -0
  115. package/ai/token/calculatePrice.ts +546 -0
  116. package/ai/token/db.ts +98 -0
  117. package/ai/token/externalToolCost.ts +321 -0
  118. package/ai/token/hooks/useRecords.ts +65 -0
  119. package/ai/token/missingUsageEstimate.ts +42 -0
  120. package/ai/token/modelUsageQuery.ts +252 -0
  121. package/ai/token/normalizeUsage.ts +84 -0
  122. package/ai/token/openaiImageGenerationUsage.ts +56 -0
  123. package/ai/token/prepareTokenUsageData.ts +88 -0
  124. package/ai/token/query.ts +88 -0
  125. package/ai/token/queryUserTokens.ts +59 -0
  126. package/ai/token/resolveBillingTarget.ts +52 -0
  127. package/ai/token/saveTokenRecord.ts +53 -0
  128. package/ai/token/serverDialogProjection.ts +78 -0
  129. package/ai/token/serverTokenWriter.ts +143 -0
  130. package/ai/token/stats.ts +21 -0
  131. package/ai/token/tokenThunks.ts +24 -0
  132. package/ai/token/types.ts +93 -0
  133. package/ai/tools/agent/agentTools.ts +176 -0
  134. package/ai/tools/agent/agentUpdateShared.ts +311 -0
  135. package/ai/tools/agent/callAgentTool.ts +139 -0
  136. package/ai/tools/agent/createAgentTool.ts +512 -0
  137. package/ai/tools/agent/createDialogTool.ts +69 -0
  138. package/ai/tools/agent/createSkillAgentTool.ts +62 -0
  139. package/ai/tools/agent/parallelBudget.ts +221 -0
  140. package/ai/tools/agent/presets/appBuilderPreset.ts +147 -0
  141. package/ai/tools/agent/runLlmTool.ts +96 -0
  142. package/ai/tools/agent/runStreamingAgentTool.ts +73 -0
  143. package/ai/tools/agent/skillAgentArgs.ts +106 -0
  144. package/ai/tools/agent/skillAgentPreset.ts +89 -0
  145. package/ai/tools/agent/streamParallelAgentsTool.ts +122 -0
  146. package/ai/tools/agent/updateAgentTool.ts +96 -0
  147. package/ai/tools/agent/updateSelfTool.ts +113 -0
  148. package/ai/tools/amazonProductScraperTool.ts +86 -0
  149. package/ai/tools/apifyActorClient.ts +45 -0
  150. package/ai/tools/appEditGuard.ts +372 -0
  151. package/ai/tools/appReadSnapshot.ts +153 -0
  152. package/ai/tools/appTools.ts +1549 -0
  153. package/ai/tools/applyEditTool.ts +256 -0
  154. package/ai/tools/applyLineEditsTool.ts +312 -0
  155. package/ai/tools/browserTools/click.ts +33 -0
  156. package/ai/tools/browserTools/closeSession.ts +29 -0
  157. package/ai/tools/browserTools/common.ts +27 -0
  158. package/ai/tools/browserTools/openSession.ts +48 -0
  159. package/ai/tools/browserTools/readContent.ts +38 -0
  160. package/ai/tools/browserTools/selectOption.ts +46 -0
  161. package/ai/tools/browserTools/typeText.ts +42 -0
  162. package/ai/tools/category/createCategoryTool.ts +66 -0
  163. package/ai/tools/category/queryContentsByCategoryTool.ts +69 -0
  164. package/ai/tools/category/updateContentCategoryTool.ts +75 -0
  165. package/ai/tools/cfBrowserTools.ts +319 -0
  166. package/ai/tools/cfSpeechToTextTool.ts +49 -0
  167. package/ai/tools/checkEnvTool.ts +65 -0
  168. package/ai/tools/cloudflareCrawlTool.ts +289 -0
  169. package/ai/tools/codeSearchTool.ts +111 -0
  170. package/ai/tools/codeTools.ts +101 -0
  171. package/ai/tools/createDocTool.ts +132 -0
  172. package/ai/tools/createPlanTool.ts +999 -0
  173. package/ai/tools/createSkillDocTool.ts +155 -0
  174. package/ai/tools/createWorkflowTool.ts +154 -0
  175. package/ai/tools/deepseekOcrTool.ts +34 -0
  176. package/ai/tools/delayTool.ts +31 -0
  177. package/ai/tools/deleteSpacesTool.ts +325 -0
  178. package/ai/tools/deleteSpacesToolModel.ts +159 -0
  179. package/ai/tools/devReloadUtils.ts +29 -0
  180. package/ai/tools/dialogMessageSearch.ts +137 -0
  181. package/ai/tools/doctorSkillTool.ts +72 -0
  182. package/ai/tools/ecommerceScraperTool.ts +86 -0
  183. package/ai/tools/emailTools.ts +549 -0
  184. package/ai/tools/evalSkillTool.ts +92 -0
  185. package/ai/tools/exaSearchTool.ts +64 -0
  186. package/ai/tools/execBashTool.ts +379 -0
  187. package/ai/tools/executeSqlTool.ts +192 -0
  188. package/ai/tools/fetchWebpageSupport.ts +309 -0
  189. package/ai/tools/fetchWebpageTool.ts +84 -0
  190. package/ai/tools/geminiImagePreviewTool.ts +361 -0
  191. package/ai/tools/generateDocxTool.ts +215 -0
  192. package/ai/tools/googleSearchScraperTool.ts +106 -0
  193. package/ai/tools/importDataTool.ts +133 -0
  194. package/ai/tools/importSkillTool.ts +162 -0
  195. package/ai/tools/index.ts +1927 -0
  196. package/ai/tools/listFilesTool.ts +82 -0
  197. package/ai/tools/listUserSpacesTool.ts +113 -0
  198. package/ai/tools/modelUsageTools.ts +199 -0
  199. package/ai/tools/olmOcrTool.ts +34 -0
  200. package/ai/tools/openaiImageTool.ts +267 -0
  201. package/ai/tools/prepareTools.ts +23 -0
  202. package/ai/tools/readDocTool.ts +84 -0
  203. package/ai/tools/readFileTool.ts +211 -0
  204. package/ai/tools/readTool.ts +163 -0
  205. package/ai/tools/readXPostTool.ts +233 -0
  206. package/ai/tools/rememberMemoryTool.ts +84 -0
  207. package/ai/tools/remotionVideoTool.ts +151 -0
  208. package/ai/tools/searchDialogMessagesTool.ts +222 -0
  209. package/ai/tools/searchRepoTool.ts +115 -0
  210. package/ai/tools/searchWorkspaceTool.ts +259 -0
  211. package/ai/tools/skillFollowup.ts +86 -0
  212. package/ai/tools/surfWeatherTool.ts +169 -0
  213. package/ai/tools/table/addTableRowTool.ts +217 -0
  214. package/ai/tools/table/createTableTool.ts +315 -0
  215. package/ai/tools/table/rowTools.ts +366 -0
  216. package/ai/tools/table/schemaTools.ts +244 -0
  217. package/ai/tools/table/shareTableTool.ts +148 -0
  218. package/ai/tools/table/toolShared.ts +129 -0
  219. package/ai/tools/toolApiClient.ts +198 -0
  220. package/ai/tools/toolNameAliases.ts +57 -0
  221. package/ai/tools/toolResultError.ts +42 -0
  222. package/ai/tools/toolRunSlice.ts +303 -0
  223. package/ai/tools/toolSchemaCompatibility.ts +53 -0
  224. package/ai/tools/toolVisibility.ts +4 -0
  225. package/ai/tools/types.ts +20 -0
  226. package/ai/tools/uiAskChoiceTool.ts +104 -0
  227. package/ai/tools/updateContentTitleTool.ts +84 -0
  228. package/ai/tools/updateDocTool.ts +105 -0
  229. package/ai/tools/updateUserPreferenceProfileTool.ts +145 -0
  230. package/ai/tools/whisperTool.ts +77 -0
  231. package/ai/tools/writeFileTool.ts +210 -0
  232. package/ai/tools/youtubeScraperTool.ts +116 -0
  233. package/ai/tools/ziweiChartTool.ts +678 -0
  234. package/ai/types.ts +55 -0
  235. package/ai/workflow/workflowExecutor.ts +323 -0
  236. package/ai/workflow/workflowSlice.ts +73 -0
  237. package/ai/workflow/workflowTypes.ts +106 -0
  238. package/client/agentRun.test.ts +240 -0
  239. package/client/agentRun.ts +182 -19
  240. package/client/compactDialog.test.ts +238 -0
  241. package/client/localRuntimeAdapter.test.ts +135 -0
  242. package/client/localRuntimeAdapter.ts +244 -0
  243. package/client/profileConfig.test.ts +40 -0
  244. package/client/streamingOutput.test.ts +22 -0
  245. package/client/streamingOutput.ts +38 -0
  246. package/commandRegistry.ts +11 -2
  247. package/connector-experimental/index.ts +5 -0
  248. package/database/actions/cacheMergedUserData.ts +64 -0
  249. package/database/actions/common.ts +242 -0
  250. package/database/actions/deleteFile.ts +40 -0
  251. package/database/actions/fetchUserData.ts +16 -0
  252. package/database/actions/fileContent.ts +125 -0
  253. package/database/actions/patch.ts +155 -0
  254. package/database/actions/read.ts +337 -0
  255. package/database/actions/readAndWait.ts +224 -0
  256. package/database/actions/readRequestManager.ts +120 -0
  257. package/database/actions/remove.ts +94 -0
  258. package/database/actions/replication.ts +366 -0
  259. package/database/actions/upload.ts +174 -0
  260. package/database/actions/upsert.ts +56 -0
  261. package/database/actions/write.ts +126 -0
  262. package/database/client/db.native.ts +73 -0
  263. package/database/client/db.ts +51 -0
  264. package/database/client/fetchUserData.ts +61 -0
  265. package/database/client/handleError.ts +19 -0
  266. package/database/client/queryRequest.ts +21 -0
  267. package/database/config.ts +21 -0
  268. package/database/dbActionThunks.ts +1 -0
  269. package/database/dbSlice.ts +149 -0
  270. package/database/email.ts +42 -0
  271. package/database/fileRing.ts +51 -0
  272. package/database/fileSharding.ts +70 -0
  273. package/database/fileStorage.native.ts +92 -0
  274. package/database/fileStorage.ts +232 -0
  275. package/database/fileUrl.ts +34 -0
  276. package/database/hooks/useUserData.ts +489 -0
  277. package/database/index.ts +1 -0
  278. package/database/keys.ts +765 -0
  279. package/database/queryPrefixes.ts +14 -0
  280. package/database/requests.ts +443 -0
  281. package/database/runtimeServerContext.ts +35 -0
  282. package/database/server/MemoryDB.ts +76 -0
  283. package/database/server/actorAccess.ts +76 -0
  284. package/database/server/agentDelegation.ts +124 -0
  285. package/database/server/coreDataOwnership.ts +13 -0
  286. package/database/server/coreDataProxy.ts +76 -0
  287. package/database/server/cybotReadonly.ts +18 -0
  288. package/database/server/dataHandlers.ts +111 -0
  289. package/database/server/db.ts +118 -0
  290. package/database/server/dbPath.ts +20 -0
  291. package/database/server/delete.ts +499 -0
  292. package/database/server/emailRepository.ts +1480 -0
  293. package/database/server/ensureDbOpen.ts +12 -0
  294. package/database/server/fileRead.ts +337 -0
  295. package/database/server/fileService.ts +436 -0
  296. package/database/server/handleTransaction.ts +86 -0
  297. package/database/server/patch.ts +282 -0
  298. package/database/server/query.ts +138 -0
  299. package/database/server/read.ts +325 -0
  300. package/database/server/resourceAccess.ts +211 -0
  301. package/database/server/routes.ts +110 -0
  302. package/database/server/spaceMemberAuthority.ts +67 -0
  303. package/database/server/upload.ts +159 -0
  304. package/database/server/write.ts +494 -0
  305. package/database/server/writeAuthority.ts +133 -0
  306. package/database/sqliteDb.ts +46 -0
  307. package/database/table/deleteTable.ts +120 -0
  308. package/database/tenantPlacement.ts +57 -0
  309. package/database/tombstones.ts +52 -0
  310. package/database/userDataLoadDecision.ts +17 -0
  311. package/database/userDataMerge.ts +95 -0
  312. package/database/userPreferenceRegister.ts +108 -0
  313. package/database/utils/dbPath.ts +47 -0
  314. package/database/utils/ulid.native.ts +6 -0
  315. package/database/utils/ulid.ts +1 -0
  316. package/index.ts +37 -19
  317. package/localRuntimeDb.ts +28 -0
  318. package/package.json +17 -4
  319. package/runtimeModeArgs.ts +33 -0
  320. package/tui/readlineWorkspace.ts +1 -0
  321. package/tui/session.ts +22 -0
@@ -0,0 +1,361 @@
1
+ // 文件路径: ai/tools/geminiImagePreviewTool.ts
2
+
3
+ import { callToolApi } from "./toolApiClient";
4
+ import { selectCurrentSpaceId } from "create/space/spaceSlice";
5
+ import { selectUserId } from "auth/authSlice";
6
+ import { selectCurrentServer } from "app/settings/settingSlice";
7
+ import { selectCurrentDialogKey } from "chat/dialog/dialogSlice";
8
+ import { addContentAction } from "create/space/content/addContentAction";
9
+ import { ContentType } from "app/types";
10
+ import { fileKey } from "database/keys";
11
+ import { buildDatabaseFileContentUrl } from "database/fileUrl";
12
+ import { extractCustomId } from "core/prefix";
13
+
14
+ const DEFAULT_GEMINI_IMAGE_MODEL = "gemini-2.5-flash-image" as const;
15
+
16
+ type GeminiImageModel =
17
+ | "gemini-2.5-flash-image"
18
+ | "gemini-3-pro-image-preview";
19
+
20
+ type GeminiImageArg = {
21
+ /**
22
+ * data 可以是:
23
+ * - 纯 Base64(无 data: 前缀)
24
+ * - 完整 dataURL(data:image/png;base64,xxx)
25
+ * - http(s) URL(由服务端拉取并转 Base64)
26
+ *
27
+ * 对于“用户在对话中上传的图片”,推荐直接使用对应 image_url.url。
28
+ */
29
+ data: string;
30
+ mimeType?: string;
31
+ };
32
+
33
+ type GeminiImageCommonArgs = {
34
+ prompt: string;
35
+ images?: GeminiImageArg[];
36
+ aspectRatio?: string;
37
+ imageSize?: "1K" | "2K" | "4K";
38
+ };
39
+
40
+ const buildGeminiImageLlmContext = ({
41
+ result,
42
+ userId,
43
+ currentServer,
44
+ }: {
45
+ result: any;
46
+ userId?: string | null;
47
+ currentServer?: string | null;
48
+ }): string | undefined => {
49
+ const files = Array.isArray(result?.files) ? result.files : [];
50
+ if (files.length === 0) return undefined;
51
+
52
+ const lines = [
53
+ "The image generation tool produced the following reusable images.",
54
+ "If you mention, embed, or tabulate these images in a later reply, reuse these exact references and never invent placeholder/example URLs.",
55
+ ];
56
+
57
+ for (let index = 0; index < files.length; index += 1) {
58
+ const file = files[index];
59
+ const bareFileId =
60
+ typeof file?.fileId === "string" && file.fileId.trim()
61
+ ? file.fileId.trim()
62
+ : "";
63
+ const fileDbKey =
64
+ userId && bareFileId ? fileKey.single(userId, bareFileId) : "";
65
+ const fileUrl = buildDatabaseFileContentUrl(
66
+ currentServer,
67
+ fileDbKey || bareFileId
68
+ );
69
+ const originalName =
70
+ typeof file?.metadata?.originalName === "string"
71
+ ? file.metadata.originalName.trim()
72
+ : "";
73
+ const prompt =
74
+ typeof file?.metadata?.prompt === "string"
75
+ ? file.metadata.prompt.trim()
76
+ : "";
77
+
78
+ lines.push(`image ${index + 1}:`);
79
+ if (bareFileId) lines.push(`- fileId: ${bareFileId}`);
80
+ if (fileDbKey) lines.push(`- fileDbKey: ${fileDbKey}`);
81
+ if (fileUrl) lines.push(`- url: ${fileUrl}`);
82
+ if (originalName) lines.push(`- name: ${originalName}`);
83
+ if (prompt) lines.push(`- prompt: ${prompt}`);
84
+ }
85
+
86
+ return lines.join("\n");
87
+ };
88
+
89
+ /* ============================================================================
90
+ * 工具 Schema(供 LLM 调用)
91
+ * - geminiFlashImage: 默认的 2.5 文生图工具
92
+ * - geminiProImagePreview: 3 Pro 图像编辑/合成工具
93
+ *
94
+ * 决策逻辑放在提示词层,而不是工具内部逻辑:
95
+ * - 提示 LLM:简单“生成新图片”用 geminiFlashImage
96
+ * - 基于现有图片的复杂编辑 / 多图合成用 geminiProImagePreview
97
+ * ========================================================================== */
98
+
99
+ /**
100
+ * 2.5 文生图 / 轻量编辑:
101
+ * - 适用于「根据文字生成新图片」或「对单张图片做简单修改」场景。
102
+ * - 当用户只提供文字、或只是简单风格/颜色调整优先使用本工具。
103
+ * - 如果需要参考当前对话中用户上传的图片,请在调用时显式传入 images:
104
+ * - 对每张相关图片,把对应的 image_url.url 或 Base64/dataURL 写入 images[i].data。
105
+ */
106
+ export const geminiFlashImageFunctionSchema = {
107
+ name: "geminiFlashImage",
108
+ description: [
109
+ "使用 Gemini 2.5 Flash 图片模型,根据文字说明和可选的输入图片生成图像。",
110
+ "",
111
+ "使用建议(面向模型):",
112
+ "1. 当用户只是描述希望生成怎样的图片(当前轮没有图片或不需要基于现有图片做复杂编辑)时,优先使用本工具。",
113
+ "2. 当用户上传了一张或多张图片,只需要做简单的风格调整、颜色修改、加一些小元素时,也可以使用本工具,并在 images 中显式传入这些图片。",
114
+ "3. 如果用户明确要进行复杂编辑、精细修图或多图合成,再考虑使用 geminiProImagePreview。",
115
+ "",
116
+ "关于 images:",
117
+ "- 如果是纯文生图:可以不传 images 字段,或传空数组。",
118
+ "- 如果要参考当前对话中用户上传的图片:请在调用时显式构造 images 数组,",
119
+ " 对每一张图片,将该图片在消息中的 image_url.url(或对应的 Base64/dataURL)填入 images[*].data。",
120
+ ].join("\n"),
121
+ parameters: {
122
+ type: "object",
123
+ properties: {
124
+ prompt: {
125
+ type: "string",
126
+ description: [
127
+ "描述要生成图片的文字提示。",
128
+ "示例:",
129
+ ' - "生成一只坐在沙发上的可爱橘猫,卡通风格"',
130
+ ' - "给用户上传的这张头像加一个圣诞帽,并做成卡通风格"',
131
+ ].join("\n"),
132
+ },
133
+ images: {
134
+ type: "array",
135
+ description: [
136
+ "可选的输入图片数组。",
137
+ "",
138
+ "用法:",
139
+ "1. 纯文生图:不传 images,或传空数组。",
140
+ "2. 需要参考当前轮用户上传的图片时:",
141
+ " - 遍历当前用户消息中的相关 image_url,",
142
+ " - 对每张图片构造元素 { data: 该图片的 URL 或 Base64/dataURL },",
143
+ " - 通常直接将 image_url.url 原样填入 data 即可。",
144
+ "",
145
+ "data 字段支持三种形式:",
146
+ " - 不带 data: 前缀的 Base64;",
147
+ " - 完整 dataURL(例如 data:image/png;base64,AAAA...);",
148
+ " - http(s) 图片 URL(例如对话消息中的 image_url.url)。",
149
+ ].join("\n"),
150
+ items: {
151
+ type: "object",
152
+ properties: {
153
+ data: {
154
+ type: "string",
155
+ description: [
156
+ "图片数据字符串,可为以下之一:",
157
+ "1) 纯 Base64(不带 data: 前缀);",
158
+ "2) 完整 dataURL,例如 \"data:image/png;base64,AAAA...\";",
159
+ "3) http(s) 图片 URL,例如对话消息中的 image_url.url。",
160
+ ].join("\n"),
161
+ },
162
+ mimeType: {
163
+ type: "string",
164
+ description: [
165
+ "可选的 MIME 类型,例如 image/png 或 image/jpeg。",
166
+ "如果 data 是 http(s) URL 或完整 dataURL,可不填。",
167
+ ].join("\n"),
168
+ },
169
+ },
170
+ required: ["data"],
171
+ },
172
+ },
173
+ aspectRatio: {
174
+ type: "string",
175
+ description:
176
+ '生成图片的宽高比,例如 "5:4"、"16:9"、"1:1"。不指定则默认 "5:4"。',
177
+ },
178
+ imageSize: {
179
+ type: "string",
180
+ description:
181
+ '生成图片的分辨率大小,支持 "1K" | "2K" | "4K"。不指定则默认 "2K"。',
182
+ },
183
+ },
184
+ required: ["prompt"],
185
+ },
186
+ };
187
+
188
+ /**
189
+ * 3 Pro 图像编辑 / 多图合成:
190
+ * - 适用于「基于一张或多张输入图片进行复杂编辑或合成」场景。
191
+ * - 当用户明确要“修改这张图”、“把几张图合成一张”、“精细修图”等,请优先使用本工具。
192
+ * - 调用时应显式传入 images,并确保至少包含一张与用户意图相关的图片。
193
+ */
194
+ export const geminiProImagePreviewFunctionSchema = {
195
+ name: "geminiProImagePreview",
196
+ description: [
197
+ "使用 Gemini 3 Pro Image Preview 模型,对一张或多张输入图片进行复杂编辑、合成或高级图像处理。",
198
+ "",
199
+ "使用建议(面向模型):",
200
+ "1. 当用户希望“修改现有图片”或“把多张图片合成一张”等复杂场景时,优先使用本工具。",
201
+ "2. 调用时应在 images 数组中显式传入本轮用户消息中相关的图片:",
202
+ " - 对每一张需要参与编辑/合成的图片,将其 image_url.url(或对应的 Base64/dataURL)写入 images[*].data。",
203
+ "3. 如果用户只是在文字上描述想要什么图片、且不依赖现有图片,请优先使用 geminiFlashImage。",
204
+ "",
205
+ "注意:",
206
+ "- 这个模型设计为“编辑 / 合成”场景,通常需要至少一张输入图片。",
207
+ "- 如果没有 images 而尝试调用,后端可能会返回错误。",
208
+ ].join("\n"),
209
+ parameters: {
210
+ type: "object",
211
+ properties: {
212
+ prompt: {
213
+ type: "string",
214
+ description: [
215
+ "描述如何基于输入图片进行编辑或合成的文字说明。",
216
+ "示例:",
217
+ ' - "把这只黄色的猫咪的毛色改成蓝色,保持背景不变"',
218
+ ' - "把这几张合照中的人合成到同一张办公室场景里,他们在一起合影"',
219
+ ].join("\n"),
220
+ },
221
+ images: {
222
+ type: "array",
223
+ description: [
224
+ "用于编辑或合成的输入图片数组(强烈建议至少包含一张图片)。",
225
+ "",
226
+ "用法:",
227
+ "1. 遍历当前用户消息中与本次编辑需求相关的 image_url,",
228
+ "2. 对每一张图片构造元素 { data: 该图片的 URL 或 Base64/dataURL },",
229
+ "3. 通常直接使用 image_url.url 作为 data 即可。",
230
+ "",
231
+ "data 字段支持:",
232
+ " - 纯 Base64;",
233
+ " - 完整 dataURL;",
234
+ " - http(s) 图片 URL。",
235
+ ].join("\n"),
236
+ items: {
237
+ type: "object",
238
+ properties: {
239
+ data: {
240
+ type: "string",
241
+ description: [
242
+ "图片数据字符串,可为以下之一:",
243
+ "1) 纯 Base64(不带 data: 前缀);",
244
+ "2) 完整 dataURL,例如 \"data:image/png;base64,AAAA...\";",
245
+ "3) http(s) 图片 URL,例如对话消息中的 image_url.url。",
246
+ ].join("\n"),
247
+ },
248
+ mimeType: {
249
+ type: "string",
250
+ description:
251
+ "可选的 MIME 类型,例如 image/png 或 image/jpeg。",
252
+ },
253
+ },
254
+ required: ["data"],
255
+ },
256
+ },
257
+ aspectRatio: {
258
+ type: "string",
259
+ description:
260
+ '生成图片的宽高比,例如 "5:4"、"16:9"、"1:1"。不指定则默认 "5:4"。',
261
+ },
262
+ imageSize: {
263
+ type: "string",
264
+ description:
265
+ '生成图片的分辨率大小,支持 "1K" | "2K" | "4K"。不指定则默认 "2K"。',
266
+ },
267
+ },
268
+ required: ["prompt"],
269
+ },
270
+ };
271
+
272
+
273
+ /* ============================================================================
274
+ * Executor 工厂:根据固定模型生成对应的工具函数
275
+ * ========================================================================== */
276
+
277
+ const createGeminiImageExecutor =
278
+ (model: GeminiImageModel) =>
279
+ async (
280
+ args: GeminiImageCommonArgs,
281
+ thunkApi: any
282
+ ): Promise<{ rawData: any; displayData: string }> => {
283
+ const { prompt, images = [], aspectRatio, imageSize } = args;
284
+
285
+ const trimmedPrompt = prompt?.trim();
286
+ if (!trimmedPrompt) {
287
+ throw new Error("prompt 不能为空");
288
+ }
289
+
290
+ const state = thunkApi.getState();
291
+ const currentDialogKey = selectCurrentDialogKey(state);
292
+ const dialogId = currentDialogKey
293
+ ? extractCustomId(currentDialogKey)
294
+ : undefined;
295
+
296
+ const result = await callToolApi(
297
+ thunkApi,
298
+ "/api/gemini-image-preview",
299
+ { prompt: trimmedPrompt, images, aspectRatio, imageSize, model, dialogId },
300
+ { withAuth: true }
301
+ );
302
+
303
+ // 将生成的图片自动加入当前 space
304
+ const spaceId = selectCurrentSpaceId(state);
305
+ const userId = selectUserId(state);
306
+ const currentServer = selectCurrentServer(state);
307
+
308
+ if (spaceId && userId && result?.files?.length) {
309
+ for (let i = 0; i < result.files.length; i++) {
310
+ const f = result.files[i];
311
+ if (!f.fileId) continue;
312
+ const contentKey = fileKey.single(userId, f.fileId);
313
+ const title = f.metadata?.originalName || `AI Image ${i + 1}`;
314
+ try {
315
+ await addContentAction(
316
+ { spaceId, contentKey, title, type: ContentType.IMAGE },
317
+ { dispatch: thunkApi.dispatch, getState: thunkApi.getState }
318
+ );
319
+ } catch (err) {
320
+ console.warn("[geminiImageExecutor] Failed to add image to space:", err);
321
+ }
322
+ }
323
+ }
324
+
325
+ const displayText: string =
326
+ result?.text ||
327
+ "已根据提供的文字(以及可选的输入图片)生成新的图像。";
328
+ const llmContext = buildGeminiImageLlmContext({
329
+ result,
330
+ userId,
331
+ currentServer,
332
+ });
333
+
334
+ return {
335
+ rawData: result,
336
+ displayData: displayText,
337
+ llmContext,
338
+ };
339
+ };
340
+
341
+ /* ============================================================================
342
+ * 具体工具执行函数(供工具注册使用)
343
+ * ========================================================================== */
344
+
345
+ /**
346
+ * geminiFlashImage:
347
+ * - 使用 gemini-2.5-flash-image
348
+ * - 适用于文生图或轻量级基于图片的修改
349
+ */
350
+ export const geminiFlashImageFunc = createGeminiImageExecutor(
351
+ "gemini-2.5-flash-image"
352
+ );
353
+
354
+ /**
355
+ * geminiProImagePreview:
356
+ * - 使用 gemini-3-pro-image-preview
357
+ * - 适用于基于一张或多张图片的复杂编辑、合成
358
+ */
359
+ export const geminiProImagePreviewFunc = createGeminiImageExecutor(
360
+ "gemini-3-pro-image-preview"
361
+ );
@@ -0,0 +1,215 @@
1
+ // /ai/tools/generateDocxTool.ts
2
+
3
+ /**
4
+ * 供调用方 / LLM 使用的入参类型
5
+ */
6
+ export interface GenerateDocxArgs {
7
+ templateUrl: string;
8
+ fileName?: string;
9
+ variables?: Record<string, any>;
10
+ /**
11
+ * 是否把变量字符串中的 \n 转成 Word 里的换行。
12
+ * - 默认为 true,保持和之前行为一致
13
+ * - 如果希望表格尽量由 Word 自己控制自动换行 / 调整宽度,可以在调用时传 false
14
+ */
15
+ respectLineBreaks?: boolean;
16
+
17
+ /**
18
+ * 是否对备注类字段(remark)做一层“去换行、去多余空白”的清洗。
19
+ * - 默认为 true,有利于启用“根据内容调整表格大小”的表格自动拉宽备注列
20
+ */
21
+ normalizeRemark?: boolean;
22
+ }
23
+
24
+ /**
25
+ * LLM 用的 schema:告诉它有哪些参数可以传
26
+ * 约定:模板里的占位符使用 [[name]] 这种形式,而不是 {{name}}
27
+ */
28
+ export const generateDocxFunctionSchema = {
29
+ name: "generateDocx",
30
+ description:
31
+ "在浏览器中根据指定的 DOCX 模板 URL 和变量,生成并下载一个新的 DOCX 文档。模板占位符使用 [[name]] 语法。",
32
+ parameters: {
33
+ type: "object",
34
+ properties: {
35
+ templateUrl: {
36
+ type: "string",
37
+ description: "DOCX 模板文件的 URL,例如 /templates/contract.docx。",
38
+ },
39
+ fileName: {
40
+ type: "string",
41
+ description: "生成文档的文件名(不含 .docx 后缀,可选,默认:文档)。",
42
+ },
43
+ variables: {
44
+ type: "object",
45
+ description:
46
+ "用于替换模板占位符的键值对,例如 { contract_no: 'PO2501', product_name: '中号转盘款拼图板' }。",
47
+ additionalProperties: true,
48
+ },
49
+ respectLineBreaks: {
50
+ type: "boolean",
51
+ description:
52
+ "是否保留变量字符串中的换行符 (\\n) 为 Word 中的换行。默认 true。若设为 false,则更有利于让 Word 自己控制表格列宽与自动换行。",
53
+ },
54
+ normalizeRemark: {
55
+ type: "boolean",
56
+ description:
57
+ "是否对备注字段(如 remark)做清洗:去掉内部换行和多余空白,提升“根据内容调整表格大小”时备注列的表现。默认 true。",
58
+ },
59
+ },
60
+ required: ["templateUrl"],
61
+ },
62
+ };
63
+
64
+ /**
65
+ * 针对 remark 这类长文本字段的清洗:
66
+ * - 统一换行符格式
67
+ * - 把换行整体压成一个空格(避免变成硬换行)
68
+ * - 收敛多余空白,保留语义
69
+ */
70
+ function normalizeRemarkText(raw: string): string {
71
+ let text = raw;
72
+
73
+ // 统一换行符为 \n
74
+ text = text.replace(/\r\n/g, "\n").replace(/\r/g, "\n");
75
+
76
+ // 将若干个换行(两边可能带空白)压成一个空格
77
+ text = text.replace(/\s*\n+\s*/g, " ");
78
+
79
+ // 将多个连续空格(含不可见空格)压成单个普通空格
80
+ text = text.replace(/[ \t\u00A0]+/g, " ");
81
+
82
+ return text.trim();
83
+ }
84
+
85
+ /**
86
+ * 通用变量清洗:
87
+ * - 统一字符串里的换行符
88
+ * - 去掉末尾多余空白
89
+ * - 针对 remark 字段做额外处理(去换行、去多余空白)
90
+ */
91
+ function normalizeVariables(
92
+ vars: Record<string, any> | undefined,
93
+ options: { normalizeRemark: boolean }
94
+ ): Record<string, any> {
95
+ if (!vars) return {};
96
+
97
+ const { normalizeRemark } = options;
98
+ const result: Record<string, any> = {};
99
+
100
+ for (const [key, value] of Object.entries(vars)) {
101
+ if (typeof value === "string") {
102
+ const unified = value
103
+ .replace(/\r\n/g, "\n")
104
+ .replace(/\r/g, "\n")
105
+ .trimEnd();
106
+
107
+ if (normalizeRemark && key === "remark") {
108
+ result[key] = normalizeRemarkText(unified);
109
+ } else {
110
+ result[key] = unified;
111
+ }
112
+ } else {
113
+ // 非字符串保持原样(数字、数组、对象等)
114
+ result[key] = value;
115
+ }
116
+ }
117
+
118
+ return result;
119
+ }
120
+
121
+ /**
122
+ * 在前端用 docxtemplater 生成 docx 并触发浏览器下载
123
+ * 模板中请使用 [[variable_name]] 作为占位符,避免和已有的 {{ }} 冲突
124
+ */
125
+ export async function generateDocxFunc(
126
+ args: GenerateDocxArgs,
127
+ _thunkApi: any
128
+ ): Promise<{ rawData: object; displayData: string }> {
129
+ const templateUrl = args.templateUrl?.trim();
130
+ if (!templateUrl) {
131
+ throw new Error("生成文档失败:templateUrl 不能为空。");
132
+ }
133
+
134
+ const fileName = (args.fileName?.trim() || "文档") + ".docx";
135
+
136
+ // 默认:保留换行;对 remark 做清洗
137
+ const respectLineBreaks = args.respectLineBreaks ?? true;
138
+ const normalizeRemark = args.normalizeRemark ?? true;
139
+
140
+ // 在传给 docxtemplater 之前,先做一层变量清洗
141
+ const variables = normalizeVariables(args.variables, { normalizeRemark });
142
+
143
+ try {
144
+ const [{ default: PizZip }, { default: Docxtemplater }] = await Promise.all([
145
+ import("pizzip"),
146
+ import("docxtemplater"),
147
+ ]);
148
+
149
+ // 1. 拉取模板
150
+ const res = await fetch(templateUrl);
151
+ if (!res.ok) {
152
+ throw new Error(`无法加载模板:${templateUrl}`);
153
+ }
154
+ const arrayBuffer = await res.arrayBuffer();
155
+
156
+ // 2. 用 PizZip 打开 docx
157
+ const zip = new PizZip(new Uint8Array(arrayBuffer));
158
+
159
+ // 3. 创建 docxtemplater 实例
160
+ // 这里显式改用 [[ ]] 作为分隔符,避免解析到历史留下的 {{ }} 残骸
161
+ const doc = new Docxtemplater(zip, {
162
+ paragraphLoop: true,
163
+ // 是否把字符串里的 \n 转成 Word 中的换行(<w:br/>)
164
+ linebreaks: respectLineBreaks,
165
+ delimiters: {
166
+ start: "[[",
167
+ end: "]]",
168
+ },
169
+ });
170
+
171
+ // 4. 设置数据并渲染
172
+ doc.setData(variables);
173
+
174
+ try {
175
+ doc.render();
176
+ } catch (e: any) {
177
+ console.error("docxtemplater 渲染错误:", e);
178
+ if (e?.properties?.errors) {
179
+ e.properties.errors.forEach((err: any) =>
180
+ console.error("template error:", err)
181
+ );
182
+ }
183
+ throw new Error("模板渲染失败,请检查占位符和 variables 是否匹配。");
184
+ }
185
+
186
+ // 5. 生成 Blob 并触发浏览器下载
187
+ const blob = doc.getZip().generate({
188
+ type: "blob",
189
+ mimeType:
190
+ "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
191
+ });
192
+
193
+ const url = URL.createObjectURL(blob);
194
+ const a = document.createElement("a");
195
+ a.href = url;
196
+ a.download = fileName;
197
+ document.body.appendChild(a);
198
+ a.click();
199
+ document.body.removeChild(a);
200
+ URL.revokeObjectURL(url);
201
+
202
+ // 6. 返回给 LLM / 前端展示的信息
203
+ const rawData = {
204
+ success: true,
205
+ fileName,
206
+ persisted: false, // 仅本地下载,未持久化到服务端
207
+ };
208
+ const displayData = `文档《${fileName}》已生成并开始下载。`;
209
+
210
+ return { rawData, displayData };
211
+ } catch (error: any) {
212
+ const msg = error?.message || JSON.stringify(error) || "未知错误";
213
+ throw new Error(`生成文档时出错: ${msg}`);
214
+ }
215
+ }
@@ -0,0 +1,106 @@
1
+ // /ai/tools/googleSearchScraperTool.ts
2
+ // Apify Google Search Results Scraper
3
+ // Actor: https://apify.com/apify/google-search-scraper
4
+
5
+ import { callApifyActor } from "./apifyActorClient";
6
+
7
+ export const googleSearchScraperFunctionSchema = {
8
+ name: "googleSearchScraper",
9
+ description:
10
+ "通过 Apify 的 Google Search Scraper 抓取 Google 搜索结果(SERP)," +
11
+ "返回自然结果、付费广告、People Also Ask、相关搜索等结构化数据。" +
12
+ "适合需要获取真实 Google 搜索排名和摘要的场景。" +
13
+ "与 exa_search 的区别:此工具返回的是真实 Google SERP 数据,exa_search 是语义搜索引擎。",
14
+ parameters: {
15
+ type: "object",
16
+ properties: {
17
+ queries: {
18
+ type: "array",
19
+ items: { type: "string" },
20
+ description: "要搜索的关键词列表,每个元素对应一次 Google 搜索。",
21
+ },
22
+ maxPagesPerQuery: {
23
+ type: "integer",
24
+ description: "每个关键词抓取的搜索结果页数(默认 1,建议不超过 3)。",
25
+ default: 1,
26
+ },
27
+ resultsPerPage: {
28
+ type: "integer",
29
+ description: "每页结果数量(默认 10,最大 100)。",
30
+ default: 10,
31
+ },
32
+ languageCode: {
33
+ type: "string",
34
+ description: "搜索语言代码,如 'zh-CN'(中文)、'en'(英文)、'ja'(日文)等。",
35
+ },
36
+ countryCode: {
37
+ type: "string",
38
+ description: "搜索国家/地区代码,如 'cn'(中国)、'us'(美国)、'jp'(日本)等。",
39
+ },
40
+ mobileResults: {
41
+ type: "boolean",
42
+ description: "是否模拟移动端搜索(默认 false)。",
43
+ default: false,
44
+ },
45
+ includeUnfilteredResults: {
46
+ type: "boolean",
47
+ description: "是否包含未过滤的补充结果(默认 false)。",
48
+ default: false,
49
+ },
50
+ saveHtml: {
51
+ type: "boolean",
52
+ description: "是否同时保存原始 HTML(默认 false)。",
53
+ default: false,
54
+ },
55
+ },
56
+ required: ["queries"],
57
+ },
58
+ };
59
+
60
+ export async function googleSearchScraperFunc(
61
+ args: {
62
+ queries: string[];
63
+ maxPagesPerQuery?: number;
64
+ resultsPerPage?: number;
65
+ languageCode?: string;
66
+ countryCode?: string;
67
+ mobileResults?: boolean;
68
+ includeUnfilteredResults?: boolean;
69
+ saveHtml?: boolean;
70
+ },
71
+ thunkApi: any
72
+ ) {
73
+ const {
74
+ queries,
75
+ maxPagesPerQuery = 1,
76
+ resultsPerPage = 10,
77
+ languageCode,
78
+ countryCode,
79
+ mobileResults = false,
80
+ includeUnfilteredResults = false,
81
+ saveHtml = false,
82
+ } = args;
83
+
84
+ if (!queries || queries.length === 0) {
85
+ throw new Error("Google 搜索:必须提供至少一个搜索关键词(queries)。");
86
+ }
87
+
88
+ const input: Record<string, any> = {
89
+ queries: queries.join("\n"),
90
+ maxPagesPerQuery,
91
+ resultsPerPage,
92
+ mobileResults,
93
+ includeUnfilteredResults,
94
+ saveHtml,
95
+ };
96
+
97
+ if (languageCode) input.languageCode = languageCode;
98
+ if (countryCode) input.countryCode = countryCode;
99
+
100
+ return callApifyActor(thunkApi, {
101
+ actorId: "apify/google-search-scraper",
102
+ input,
103
+ resultType: "datasetItems",
104
+ displayName: "Google Search Scraper",
105
+ });
106
+ }