nolo-cli 0.1.10 → 0.1.11

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 (240) hide show
  1. package/README.md +0 -32
  2. package/agentRuntimeCommands.ts +3 -3
  3. package/commandRegistry.ts +2 -2
  4. package/machineCommands.ts +31 -6
  5. package/package.json +6 -22
  6. package/ai/agent/_executeModel.ts +0 -118
  7. package/ai/agent/agentSlice.ts +0 -525
  8. package/ai/agent/appWorkingMemory.ts +0 -126
  9. package/ai/agent/avatarUtils.ts +0 -24
  10. package/ai/agent/buildEditingContext.ts +0 -373
  11. package/ai/agent/buildSystemPrompt.ts +0 -532
  12. package/ai/agent/cleanAgentMessages.ts +0 -140
  13. package/ai/agent/cliChatClient.ts +0 -119
  14. package/ai/agent/cliExecutor.ts +0 -733
  15. package/ai/agent/cliPrompt.ts +0 -10
  16. package/ai/agent/contextCompiler.ts +0 -107
  17. package/ai/agent/contextLayerContract.ts +0 -44
  18. package/ai/agent/createAgentSchema.ts +0 -234
  19. package/ai/agent/executeToolCall.ts +0 -58
  20. package/ai/agent/fetchAgentContexts.ts +0 -42
  21. package/ai/agent/generatePrompt.ts +0 -3
  22. package/ai/agent/getFullChatContextKeys.ts +0 -168
  23. package/ai/agent/hooks/fetchPublicAgents.ts +0 -133
  24. package/ai/agent/hooks/useAgentConfig.ts +0 -61
  25. package/ai/agent/hooks/useAgentDialog.ts +0 -35
  26. package/ai/agent/hooks/useAgentFormValidation.ts +0 -202
  27. package/ai/agent/hooks/usePublicAgents.ts +0 -473
  28. package/ai/agent/machineRunPermissions.ts +0 -95
  29. package/ai/agent/persistMessageWithFixedId.ts +0 -37
  30. package/ai/agent/planSlice.ts +0 -259
  31. package/ai/agent/referenceUtils.ts +0 -229
  32. package/ai/agent/runAgentBackground.ts +0 -238
  33. package/ai/agent/runAgentClientLoop.ts +0 -138
  34. package/ai/agent/runtimeGuidance.ts +0 -97
  35. package/ai/agent/runtimeServerBase.ts +0 -37
  36. package/ai/agent/server/fetchPublicAgents.ts +0 -128
  37. package/ai/agent/startParallelAgentStreams.ts +0 -424
  38. package/ai/agent/startupProtocol.ts +0 -53
  39. package/ai/agent/streamAgentChatTurn.ts +0 -1278
  40. package/ai/agent/streamAgentChatTurnUtils.ts +0 -738
  41. package/ai/agent/types.ts +0 -71
  42. package/ai/agent/utils/imageOutput.ts +0 -33
  43. package/ai/agent/utils/sortUtils.ts +0 -250
  44. package/ai/agent/web/referencePickerUtils.ts +0 -146
  45. package/ai/ai.locale.ts +0 -1079
  46. package/ai/chat/accumulateToolCallChunks.ts +0 -95
  47. package/ai/chat/fetchUtils.native.ts +0 -276
  48. package/ai/chat/fetchUtils.ts +0 -153
  49. package/ai/chat/parseApiError.ts +0 -64
  50. package/ai/chat/parseMultilineSSE.ts +0 -95
  51. package/ai/chat/sendOpenAICompletionsRequest.native.ts +0 -682
  52. package/ai/chat/sendOpenAICompletionsRequest.ts +0 -703
  53. package/ai/chat/sendOpenAIResponseRequest.ts +0 -491
  54. package/ai/chat/shouldUseServerProxy.ts +0 -18
  55. package/ai/chat/sseClient.native.ts +0 -91
  56. package/ai/chat/sseClient.ts +0 -67
  57. package/ai/chat/streamReader.native.ts +0 -31
  58. package/ai/chat/streamReader.ts +0 -62
  59. package/ai/chat/updateTotalUsage.ts +0 -72
  60. package/ai/context/buildReferenceContext.ts +0 -437
  61. package/ai/context/calculateContextUsage.ts +0 -133
  62. package/ai/context/retention.ts +0 -165
  63. package/ai/context/tokenUtils.ts +0 -78
  64. package/ai/index.ts +0 -1
  65. package/ai/llm/calculateGeminiImageTokens.ts +0 -57
  66. package/ai/llm/deepinfra.ts +0 -28
  67. package/ai/llm/fireworks.ts +0 -50
  68. package/ai/llm/generateRequestBody.ts +0 -165
  69. package/ai/llm/getModelContextWindow.ts +0 -84
  70. package/ai/llm/getNoloKey.ts +0 -31
  71. package/ai/llm/getPricing.ts +0 -199
  72. package/ai/llm/hooks/useModelPricing.ts +0 -75
  73. package/ai/llm/imagePricing.ts +0 -40
  74. package/ai/llm/isResponseAPIModel.ts +0 -13
  75. package/ai/llm/mimo.ts +0 -71
  76. package/ai/llm/mistral.ts +0 -22
  77. package/ai/llm/modelAvatar.ts +0 -427
  78. package/ai/llm/models.ts +0 -45
  79. package/ai/llm/openrouterModels.ts +0 -269
  80. package/ai/llm/providers.ts +0 -306
  81. package/ai/llm/reasoningModels.ts +0 -28
  82. package/ai/llm/types.ts +0 -59
  83. package/ai/llm/usageRequestOptions.ts +0 -59
  84. package/ai/memory/capture.ts +0 -148
  85. package/ai/memory/consolidate.ts +0 -104
  86. package/ai/memory/delete.ts +0 -147
  87. package/ai/memory/overlay.ts +0 -84
  88. package/ai/memory/query.ts +0 -38
  89. package/ai/memory/queryShared.ts +0 -160
  90. package/ai/memory/rank.ts +0 -105
  91. package/ai/memory/recentRelationshipRecap.ts +0 -249
  92. package/ai/memory/remember.ts +0 -167
  93. package/ai/memory/runtime.ts +0 -76
  94. package/ai/memory/store.ts +0 -20
  95. package/ai/memory/storeShared.ts +0 -76
  96. package/ai/memory/types.ts +0 -46
  97. package/ai/memory/understanding.ts +0 -349
  98. package/ai/memory/understandingGreeting.ts +0 -264
  99. package/ai/messages/type.ts +0 -20
  100. package/ai/policy/personalizationDialog.ts +0 -333
  101. package/ai/policy/runtimePolicy.ts +0 -440
  102. package/ai/policy/selfUpdateFields.ts +0 -48
  103. package/ai/policy/types.ts +0 -64
  104. package/ai/skills/referenceRuntime.ts +0 -274
  105. package/ai/skills/skillDiagnostics.ts +0 -251
  106. package/ai/skills/skillDocBuilder.ts +0 -139
  107. package/ai/skills/skillDocProtocol.ts +0 -434
  108. package/ai/skills/skillReferenceSummary.ts +0 -63
  109. package/ai/skills/skillSummaryMarker.ts +0 -26
  110. package/ai/token/calculatePrice.ts +0 -544
  111. package/ai/token/db.ts +0 -98
  112. package/ai/token/externalToolCost.ts +0 -330
  113. package/ai/token/hooks/useRecords.ts +0 -65
  114. package/ai/token/missingUsageEstimate.ts +0 -42
  115. package/ai/token/modelUsageQuery.ts +0 -252
  116. package/ai/token/normalizeUsage.ts +0 -84
  117. package/ai/token/openaiImageGenerationUsage.ts +0 -56
  118. package/ai/token/prepareTokenUsageData.ts +0 -88
  119. package/ai/token/query.ts +0 -88
  120. package/ai/token/queryUserTokens.ts +0 -59
  121. package/ai/token/resolveBillingTarget.ts +0 -52
  122. package/ai/token/saveTokenRecord.ts +0 -53
  123. package/ai/token/serverDialogProjection.ts +0 -78
  124. package/ai/token/serverTokenWriter.ts +0 -143
  125. package/ai/token/stats.ts +0 -21
  126. package/ai/token/tokenThunks.ts +0 -24
  127. package/ai/token/types.ts +0 -93
  128. package/ai/tools/agent/agentTools.ts +0 -176
  129. package/ai/tools/agent/agentUpdateShared.ts +0 -311
  130. package/ai/tools/agent/callAgentTool.ts +0 -139
  131. package/ai/tools/agent/createAgentTool.ts +0 -512
  132. package/ai/tools/agent/createDialogTool.ts +0 -69
  133. package/ai/tools/agent/createSkillAgentTool.ts +0 -62
  134. package/ai/tools/agent/parallelBudget.ts +0 -221
  135. package/ai/tools/agent/presets/appBuilderPreset.ts +0 -145
  136. package/ai/tools/agent/runLlmTool.ts +0 -96
  137. package/ai/tools/agent/runStreamingAgentTool.ts +0 -73
  138. package/ai/tools/agent/skillAgentArgs.ts +0 -106
  139. package/ai/tools/agent/skillAgentPreset.ts +0 -89
  140. package/ai/tools/agent/streamParallelAgentsTool.ts +0 -122
  141. package/ai/tools/agent/updateAgentTool.ts +0 -96
  142. package/ai/tools/agent/updateSelfTool.ts +0 -113
  143. package/ai/tools/amazonProductScraperTool.ts +0 -86
  144. package/ai/tools/apifyActorClient.ts +0 -45
  145. package/ai/tools/appEditGuard.ts +0 -372
  146. package/ai/tools/appReadSnapshot.ts +0 -153
  147. package/ai/tools/appTools.ts +0 -1549
  148. package/ai/tools/applyEditTool.ts +0 -256
  149. package/ai/tools/applyLineEditsTool.ts +0 -312
  150. package/ai/tools/browserTools/click.ts +0 -33
  151. package/ai/tools/browserTools/closeSession.ts +0 -29
  152. package/ai/tools/browserTools/common.ts +0 -27
  153. package/ai/tools/browserTools/openSession.ts +0 -48
  154. package/ai/tools/browserTools/readContent.ts +0 -38
  155. package/ai/tools/browserTools/selectOption.ts +0 -46
  156. package/ai/tools/browserTools/typeText.ts +0 -42
  157. package/ai/tools/category/createCategoryTool.ts +0 -66
  158. package/ai/tools/category/queryContentsByCategoryTool.ts +0 -69
  159. package/ai/tools/category/updateContentCategoryTool.ts +0 -75
  160. package/ai/tools/cfBrowserTools.ts +0 -319
  161. package/ai/tools/cfSpeechToTextTool.ts +0 -49
  162. package/ai/tools/checkEnvTool.ts +0 -65
  163. package/ai/tools/cloudflareCrawlTool.ts +0 -289
  164. package/ai/tools/codeSearchTool.ts +0 -111
  165. package/ai/tools/codeTools.ts +0 -101
  166. package/ai/tools/createDocTool.ts +0 -132
  167. package/ai/tools/createPlanTool.ts +0 -999
  168. package/ai/tools/createSkillDocTool.ts +0 -155
  169. package/ai/tools/createWorkflowTool.ts +0 -154
  170. package/ai/tools/deepseekOcrTool.ts +0 -34
  171. package/ai/tools/delayTool.ts +0 -31
  172. package/ai/tools/deleteSpacesTool.ts +0 -325
  173. package/ai/tools/deleteSpacesToolModel.ts +0 -159
  174. package/ai/tools/devReloadUtils.ts +0 -29
  175. package/ai/tools/dialogMessageSearch.ts +0 -137
  176. package/ai/tools/doctorSkillTool.ts +0 -72
  177. package/ai/tools/ecommerceScraperTool.ts +0 -86
  178. package/ai/tools/emailTools.ts +0 -549
  179. package/ai/tools/evalSkillTool.ts +0 -92
  180. package/ai/tools/exaSearchTool.ts +0 -64
  181. package/ai/tools/execBashTool.ts +0 -379
  182. package/ai/tools/executeSqlTool.ts +0 -192
  183. package/ai/tools/fetchWebpageSupport.ts +0 -309
  184. package/ai/tools/fetchWebpageTool.ts +0 -84
  185. package/ai/tools/geminiImagePreviewTool.ts +0 -361
  186. package/ai/tools/generateDocxTool.ts +0 -215
  187. package/ai/tools/googleSearchScraperTool.ts +0 -106
  188. package/ai/tools/importDataTool.ts +0 -133
  189. package/ai/tools/importSkillTool.ts +0 -162
  190. package/ai/tools/index.ts +0 -1858
  191. package/ai/tools/listFilesTool.ts +0 -82
  192. package/ai/tools/listUserSpacesTool.ts +0 -113
  193. package/ai/tools/modelUsageTools.ts +0 -142
  194. package/ai/tools/olmOcrTool.ts +0 -34
  195. package/ai/tools/openaiImageTool.ts +0 -218
  196. package/ai/tools/paddleOcrTool.ts +0 -34
  197. package/ai/tools/prepareTools.ts +0 -23
  198. package/ai/tools/readDocTool.ts +0 -84
  199. package/ai/tools/readFileTool.ts +0 -211
  200. package/ai/tools/readTool.ts +0 -163
  201. package/ai/tools/readXPostTool.ts +0 -233
  202. package/ai/tools/rememberMemoryTool.ts +0 -84
  203. package/ai/tools/remotionVideoTool.ts +0 -151
  204. package/ai/tools/searchDialogMessagesTool.ts +0 -222
  205. package/ai/tools/searchRepoTool.ts +0 -115
  206. package/ai/tools/searchWorkspaceTool.ts +0 -259
  207. package/ai/tools/skillFollowup.ts +0 -86
  208. package/ai/tools/surfWeatherTool.ts +0 -169
  209. package/ai/tools/table/addTableRowTool.ts +0 -217
  210. package/ai/tools/table/createTableTool.ts +0 -315
  211. package/ai/tools/table/rowTools.ts +0 -366
  212. package/ai/tools/table/schemaTools.ts +0 -244
  213. package/ai/tools/table/shareTableTool.ts +0 -148
  214. package/ai/tools/table/toolShared.ts +0 -129
  215. package/ai/tools/toolApiClient.ts +0 -198
  216. package/ai/tools/toolNameAliases.ts +0 -57
  217. package/ai/tools/toolResultError.ts +0 -42
  218. package/ai/tools/toolRunSlice.ts +0 -303
  219. package/ai/tools/toolSchemaCompatibility.ts +0 -53
  220. package/ai/tools/toolVisibility.ts +0 -4
  221. package/ai/tools/types.ts +0 -20
  222. package/ai/tools/uiAskChoiceTool.ts +0 -104
  223. package/ai/tools/updateContentTitleTool.ts +0 -84
  224. package/ai/tools/updateDocTool.ts +0 -105
  225. package/ai/tools/updateUserPreferenceProfileTool.ts +0 -145
  226. package/ai/tools/whisperTool.ts +0 -77
  227. package/ai/tools/writeFileTool.ts +0 -210
  228. package/ai/tools/youtubeScraperTool.ts +0 -116
  229. package/ai/tools/ziweiChartTool.ts +0 -678
  230. package/ai/types.ts +0 -55
  231. package/ai/workflow/workflowExecutor.ts +0 -323
  232. package/ai/workflow/workflowSlice.ts +0 -73
  233. package/ai/workflow/workflowTypes.ts +0 -106
  234. package/client/compactDialog.ts +0 -222
  235. package/connector-experimental/capabilities.ts +0 -73
  236. package/connector-experimental/codexBinary.ts +0 -41
  237. package/connector-experimental/heartbeatLoop.ts +0 -22
  238. package/connector-experimental/index.ts +0 -5
  239. package/connector-experimental/machineInfo.ts +0 -46
  240. package/connector-experimental/protocol.ts +0 -54
@@ -1,738 +0,0 @@
1
- // 文件路径: packages/ai/agent/streamAgentChatTurnUtils.ts
2
-
3
- import type { RootState } from "app/store";
4
- import { read, selectById } from "database/dbSlice";
5
- import { fetchReferenceContents } from "ai/context/buildReferenceContext";
6
- import {
7
- selectPendingFiles,
8
- type PendingFile,
9
- } from "chat/dialog/dialogSlice";
10
- import { selectAllMsgs } from "chat/messages/messageSlice";
11
- import { selectCurrentSpace, selectViewMode } from "create/space/spaceSlice";
12
- import { createSpaceKey } from "create/space/spaceKeys";
13
- import {
14
- selectAiRecentContentLimit,
15
- selectGlobalPrompt,
16
- selectKnowledgeCaptureLevel,
17
- selectSpaceContextLevel,
18
- selectUserTonePreset,
19
- } from "app/settings/settingSlice";
20
- import {
21
- getFullChatContextKeys,
22
- deduplicateContextKeys,
23
- } from "ai/agent/getFullChatContextKeys";
24
- import type { Agent, Category, SpaceContent, DialogConfig } from "app/types";
25
- import type { Contexts } from "ai/types";
26
- import { getModelContextWindow } from "ai/llm/getModelContextWindow";
27
- import {
28
- selectCurrentUserBalance,
29
- selectUserId,
30
- } from "auth/authSlice";
31
- import { getModelPricing, getPrices, getFinalPrice } from "ai/llm/getPricing";
32
- import {
33
- buildStaticUserPolicyContext,
34
- resolveSpaceContextPreloadPlan,
35
- } from "ai/policy/runtimePolicy";
36
- import {
37
- PERSONALIZATION_DIALOG_CATEGORY,
38
- buildPersonalizationDialogPolicyContext,
39
- } from "ai/policy/personalizationDialog";
40
- import { buildEditingContextSummary } from "./buildEditingContext";
41
- import { estimateTokenCount } from "ai/context/tokenUtils";
42
- import type { OpenAIMessage } from "integrations/openai/filterAndCleanMessages";
43
- import {
44
- ConversationLoad,
45
- planContextUsage,
46
- } from "ai/context/retention";
47
- import { TOOL_PACKS } from "ai/tools";
48
- import { canonicalizeToolNames, prioritizeToolNames } from "ai/tools/toolNameAliases";
49
- import {
50
- selectAllToolRuns,
51
- } from "ai/tools/toolRunSlice";
52
- import { buildRecentAppToolMemory } from "./appWorkingMemory";
53
- import type { AgentRuntimeOptions } from "./types";
54
-
55
- /**
56
- * 估算单条 OpenAI 消息的 token 数(包括 tool_calls)。
57
- */
58
- export const estimateTokensOfMessage = (msg: OpenAIMessage): number => {
59
- let content = "";
60
-
61
- if (typeof msg.content === "string") {
62
- content = msg.content;
63
- } else if (Array.isArray(msg.content)) {
64
- content = msg.content
65
- .map((p: any) => {
66
- if (p.type === "text") return p.text || "";
67
- if (p.type === "image_url") return "[image]";
68
- return "[non-text]";
69
- })
70
- .join("");
71
- } else if (msg.content && typeof msg.content === "object") {
72
- content = JSON.stringify(msg.content);
73
- }
74
-
75
- let extraTokens = 0;
76
- if (Array.isArray((msg as any).tool_calls)) {
77
- const toolsStr = JSON.stringify((msg as any).tool_calls);
78
- extraTokens = estimateTokenCount(toolsStr);
79
- }
80
-
81
- return estimateTokenCount(content) + extraTokens;
82
- };
83
-
84
- /**
85
- * 基于最近 N 条消息的 token 分布,粗略评估会话负载等级。
86
- */
87
- export const classifyConversationLoad = (
88
- messages: OpenAIMessage[],
89
- ): ConversationLoad => {
90
- const N = 20;
91
- if (!Array.isArray(messages) || messages.length === 0) return "light";
92
-
93
- const tail = messages.slice(-N);
94
- const tokenSamples = tail.map(estimateTokensOfMessage);
95
- if (tokenSamples.length === 0) return "light";
96
-
97
- const sum = tokenSamples.reduce((acc, v) => acc + v, 0);
98
- const avg = sum / tokenSamples.length;
99
- const sorted = [...tokenSamples].sort((a, b) => a - b);
100
- const p95 = sorted[Math.floor((sorted.length - 1) * 0.95)];
101
-
102
- if (p95 < 200 && avg < 120) {
103
- return "light";
104
- }
105
-
106
- if (p95 > 2000 || avg > 1200) {
107
- return "heavy";
108
- }
109
-
110
- return "medium";
111
- };
112
-
113
- /**
114
- * 根据上下文窗口和对话摘要,对消息历史做截断。
115
- */
116
- /**
117
- * 压缩历史 tool_result 内容,防止大体积工具返回值撑爆上下文。
118
- *
119
- * 策略:
120
- * - 最近一个 tool 轮次(最后一条 assistant tool_calls + 对应 tool 结果):完整保留
121
- * - 更早的 tool 消息:内容截断到 MAX_CHARS,并追加截断标记
122
- *
123
- * 在 filterAndCleanMessages 之后、trimMessagesWithSummary 之前调用。
124
- */
125
- export const compressOldToolResults = (
126
- messages: OpenAIMessage[],
127
- maxChars = 800,
128
- ): OpenAIMessage[] => {
129
- // 找到最后一条 assistant 消息(含 tool_calls)的索引
130
- let lastToolCallAssistantIdx = -1;
131
- for (let i = messages.length - 1; i >= 0; i--) {
132
- if (
133
- messages[i].role === "assistant" &&
134
- Array.isArray((messages[i] as any).tool_calls) &&
135
- (messages[i] as any).tool_calls.length > 0
136
- ) {
137
- lastToolCallAssistantIdx = i;
138
- break;
139
- }
140
- }
141
-
142
- return messages.map((msg, idx) => {
143
- // 只处理 tool 消息,且不是最近轮次的
144
- if (msg.role !== "tool") return msg;
145
- if (idx > lastToolCallAssistantIdx) return msg; // 最近轮次,保留完整
146
-
147
- const content = typeof msg.content === "string" ? msg.content : JSON.stringify(msg.content);
148
- if (content.length <= maxChars) return msg;
149
-
150
- return {
151
- ...msg,
152
- content: content.slice(0, maxChars) + `\n…[截断,原始长度 ${content.length} 字符]`,
153
- };
154
- });
155
- };
156
-
157
-
158
- export const trimMessagesWithSummary = (
159
- messages: OpenAIMessage[],
160
- contextWindow: number,
161
- summaryTokenCount: number,
162
- retention: number = 50,
163
- ): OpenAIMessage[] => {
164
- const recentLoad = classifyConversationLoad(messages);
165
-
166
- const { rawMessageBudget, minTailTokens } = planContextUsage({
167
- contextWindow,
168
- retentionSlider: retention,
169
- summaryTokens: summaryTokenCount,
170
- recentLoad,
171
- });
172
-
173
- if (!Array.isArray(messages) || messages.length === 0) return messages;
174
-
175
- let totalTokens = 0;
176
- let keepCount = 0;
177
-
178
- // 从后往前保留,直到填满 rawMessageBudget
179
- for (let i = messages.length - 1; i >= 0; i--) {
180
- const t = estimateTokensOfMessage(messages[i]);
181
- if (totalTokens + t > rawMessageBudget) break;
182
- totalTokens += t;
183
- keepCount++;
184
- }
185
-
186
- if (keepCount >= messages.length) {
187
- return messages;
188
- }
189
-
190
- // 二次兜底:如果保留下来的 token 太少,尝试多保留几条,
191
- // 直到达到 minTailTokens,允许轻微超过 rawMessageBudget(最多 20%)。
192
- if (totalTokens < minTailTokens) {
193
- const maxBudgetWithSlack = rawMessageBudget * 1.2;
194
- for (let i = messages.length - 1 - keepCount; i >= 0; i--) {
195
- const t = estimateTokensOfMessage(messages[i]);
196
- if (totalTokens + t > maxBudgetWithSlack) break;
197
- totalTokens += t;
198
- keepCount++;
199
- if (totalTokens >= minTailTokens) break;
200
- }
201
- }
202
-
203
- // 再兜一层底:至少保留 2 条消息(如果存在)
204
- if (keepCount === 0 && messages.length > 0) {
205
- keepCount = Math.min(2, messages.length);
206
- }
207
-
208
- let cutIndex = messages.length - keepCount;
209
- if (cutIndex < 0) cutIndex = 0;
210
-
211
- // 避免从 tool 消息中间开始,丢失其调用方 assistant
212
- while (cutIndex < messages.length && messages[cutIndex].role === "tool") {
213
- cutIndex++;
214
- }
215
-
216
- return messages.slice(cutIndex);
217
- };
218
-
219
- /**
220
- * 安全读取数据库记录,失败时返回 null。
221
- */
222
- export const readSafe = async <T>(
223
- dispatch: any,
224
- dbKey: string,
225
- ): Promise<T | null> => {
226
- try {
227
- return (await dispatch(read({
228
- dbKey: dbKey
229
- })).unwrap()) as T;
230
- } catch {
231
- return null;
232
- }
233
- };
234
-
235
- /** 将 Map 的所有 value 拼接为一个字符串 */
236
- export const joinMapValues = (map: Map<string, string>): string =>
237
- Array.from(map.values()).join("");
238
-
239
- /** 检查 OpenAI 消息数组中是否包含 image_url 片段 */
240
- export const hasImageInMessages = (messages: any[]): boolean => {
241
- if (!Array.isArray(messages)) return false;
242
-
243
- return messages.some((msg) => {
244
- const content = (msg as any).content;
245
- if (!Array.isArray(content)) return false;
246
-
247
- return content.some(
248
- (part: any) =>
249
- part &&
250
- typeof part === "object" &&
251
- part.type === "image_url" &&
252
- part.image_url &&
253
- typeof part.image_url.url === "string" &&
254
- part.image_url.url.trim() !== "",
255
- );
256
- });
257
- };
258
-
259
- /** 根据 pendingFiles 和 currentInputMap 构造“当前输入上下文”字符串 */
260
- export const formatCurrentInputContext = (
261
- pendingFiles: PendingFile[],
262
- currentInputMap: Map<string, string>,
263
- ): string => {
264
- if (pendingFiles.length === 0 || currentInputMap.size === 0) {
265
- return joinMapValues(currentInputMap);
266
- }
267
-
268
- const relevantPendingFiles = pendingFiles.filter((file) => {
269
- const key = file.sourceDialogKey || file.dialogKey || file.pageKey;
270
- return key && currentInputMap.has(key);
271
- });
272
-
273
- if (relevantPendingFiles.length === 0) {
274
- return joinMapValues(currentInputMap);
275
- }
276
-
277
- const filesByGroup = new Map<string, PendingFile[]>();
278
- for (const file of relevantPendingFiles) {
279
- const groupKey = file.groupId || file.id;
280
- const group = filesByGroup.get(groupKey);
281
- if (group) {
282
- group.push(file);
283
- } else {
284
- filesByGroup.set(groupKey, [file]);
285
- }
286
- }
287
-
288
- let sourceCounter = 1;
289
- let output = "";
290
-
291
- filesByGroup.forEach((filesInGroup) => {
292
- const isGroup = filesInGroup.length > 1;
293
- const sourceName = isGroup
294
- ? filesInGroup[0].name.split(" (")[0]
295
- : filesInGroup[0].name;
296
-
297
- output += `--- Source ${sourceCounter}: "${sourceName}" ---\n`;
298
-
299
- filesInGroup.forEach((file) => {
300
- const key = file.sourceDialogKey || file.dialogKey || file.pageKey;
301
- if (!key) return; // Skip if no key
302
- const content = currentInputMap.get(key);
303
- if (!content) return;
304
-
305
- if (isGroup) {
306
- output += `### Document: "${file.name}"\n${content}\n`;
307
- } else {
308
- output += `${content}\n`;
309
- }
310
- });
311
-
312
- output += `--- End of Source ${sourceCounter} ---\n\n`;
313
- sourceCounter++;
314
- });
315
-
316
- return output;
317
- };
318
-
319
- /** 校验当前用户是否有权限使用该 Agent,并且余额是否充足(每轮调用) */
320
- export const validateAccessAndBalance = (
321
- agentConfig: Agent,
322
- state: RootState,
323
- ): string | null => {
324
- const userBalance = selectCurrentUserBalance(state);
325
- const currentUserId = selectUserId(state);
326
-
327
- if (typeof userBalance !== "number") {
328
- return "正在获取用户余额,请稍候...";
329
- }
330
-
331
- const isOwner = Boolean(currentUserId) && agentConfig.userId === currentUserId;
332
-
333
- if (!isOwner) {
334
- const hasWhitelist =
335
- Array.isArray(agentConfig.whitelist) &&
336
- agentConfig.whitelist.length > 0;
337
-
338
- if (hasWhitelist) {
339
- const isUserInWhitelist =
340
- !!currentUserId && agentConfig.whitelist?.includes(currentUserId);
341
-
342
- if (!isUserInWhitelist) {
343
- return "您不在该应用的白名单中,无法使用。";
344
- }
345
- }
346
- }
347
-
348
- const isCustomApi = agentConfig.apiSource === "custom";
349
- const isCliApi = agentConfig.apiSource === "cli";
350
- if (isCustomApi || isCliApi) {
351
- return null;
352
- }
353
-
354
- const serverPrices = getModelPricing(agentConfig.provider || "", agentConfig.model);
355
-
356
- if (!serverPrices) {
357
- return "无法获取模型定价信息,请稍后重试。";
358
- }
359
-
360
- const prices = getPrices(agentConfig, serverPrices);
361
- const maxPrice = getFinalPrice(prices);
362
-
363
- if (userBalance < maxPrice) {
364
- return "余额不足,请充值后再试。";
365
- }
366
-
367
- return null;
368
- };
369
-
370
- /** 静态上下文:Loop 期间稳定不变的部分 */
371
- export interface StaticContexts {
372
- botInstructionsContext: string;
373
- botKnowledgeContext: string;
374
- spaceContext: string | null;
375
- userGlobalPrompt: string | null;
376
- userPolicyContext: string | null;
377
- }
378
-
379
- /** 动态上下文:每轮 Loop 需要更新的部分 */
380
- export interface DynamicContexts {
381
- currentInputContext: string | null;
382
- historyContext: string;
383
- editingContext: string | null;
384
- appWorkingMemory: string | null;
385
- dialogSummary: string | null;
386
- proactiveSummary: string | null;
387
- referenceKeys: string[];
388
- }
389
-
390
-
391
- /**
392
- * 构建静态上下文(Loop 外调用一次)
393
- * 包含:botInstructions、botKnowledge、spaceContext、userGlobalPrompt
394
- * 这些内容在 Agent Loop 期间是稳定的,不需要每轮重新构建
395
- */
396
- export const buildStaticContexts = async (
397
- state: RootState,
398
- dispatch: any,
399
- agentConfig: Agent,
400
- dialogConfig?: DialogConfig,
401
- referenceContentCache?: Map<string, any>,
402
- ): Promise<StaticContexts> => {
403
- // 获取上下文 keys
404
- const keySets = await getFullChatContextKeys(
405
- state,
406
- dispatch,
407
- agentConfig,
408
- "", // 静态上下文不需要 userInput
409
- undefined, // 静态上下文不需要 dialogConfig
410
- );
411
- const finalKeys = deduplicateContextKeys(keySets);
412
-
413
- const fetchReferences = (keys: string[]) =>
414
- fetchReferenceContents(keys, dispatch, {
415
- format: "simplified_markdown",
416
- inlineMentionMeta: true,
417
- preloaded: referenceContentCache,
418
- });
419
-
420
- // 并行获取静态引用内容
421
- const [botInstructionsMap, botKnowledgeMap] = await Promise.all([
422
- fetchReferences(finalKeys.botInstructionsContext),
423
- fetchReferences(finalKeys.botKnowledgeContext),
424
- ]);
425
-
426
- // 构建 spaceContext
427
- const globalPrompt = selectGlobalPrompt(state);
428
- const userTonePreset = selectUserTonePreset(state);
429
- const knowledgeCaptureLevel = selectKnowledgeCaptureLevel(state);
430
- const spaceContextLevel = selectSpaceContextLevel(state);
431
- const currentSpace = selectCurrentSpace(state);
432
- const userRecentLimit = selectAiRecentContentLimit(state);
433
- const contextWindow = getModelContextWindow(agentConfig.model) || 128000;
434
- const preloadPlan = resolveSpaceContextPreloadPlan(spaceContextLevel);
435
- const dynamicLimit = Math.floor((contextWindow * preloadPlan.preloadBudgetRatio) / 150);
436
- const recentLimit = preloadPlan.includeRecentContent
437
- ? Math.max(3, Math.min(userRecentLimit, Math.max(3, dynamicLimit)))
438
- : 0;
439
-
440
- let spaceContext: string | null = null;
441
-
442
- if (currentSpace && spaceContextLevel > 1) {
443
- const { categories, contents } = currentSpace;
444
- const catEntries = Object.entries(categories || {}) as Array<
445
- [string, Category | null]
446
- >;
447
- const validCatList = catEntries
448
- .filter((entry) => entry[1] !== null)
449
- .map((entry) => [entry[0], entry[1] as Category] as const)
450
- .sort((a, b) => (a[1].order || 0) - (b[1].order || 0));
451
-
452
- const catMap = new Map<string, string>();
453
- let struct = "Directory Structure (Categories):\n";
454
- if (validCatList.length === 0) struct += "(No categories defined)\n";
455
-
456
- validCatList.forEach(([id, cat]) => {
457
- catMap.set(id, cat.name);
458
- struct += `- ${cat.name} (ID: ${id})\n`;
459
- });
460
-
461
- const contentEntries = Object.entries(contents || {}) as Array<
462
- [string, SpaceContent | null]
463
- >;
464
- const contentList = recentLimit > 0
465
- ? contentEntries
466
- .filter((entry) => entry[1] !== null)
467
- .map((entry) => entry[1] as SpaceContent)
468
- .sort((a, b) => b.updatedAt - a.updatedAt)
469
- .slice(0, recentLimit)
470
- : [];
471
-
472
- if (contentList.length > 0) {
473
- struct += `\nRecent Contents (Top ${recentLimit}):\n`;
474
- contentList.forEach((c) => {
475
- const item = c as SpaceContent;
476
- const catName = item.categoryId
477
- ? catMap.get(item.categoryId) || "Unknown"
478
- : "Uncategorized";
479
- struct += `- [${item.type}] ${item.title} (Category: ${catName}, dbKey: ${item.contentKey})\n`;
480
- });
481
- }
482
-
483
- spaceContext = `Space Title: ${currentSpace.name}\nSpace ID: ${currentSpace.id}\nDescription: ${currentSpace.description || "N/A"}\n\n${struct}`;
484
- }
485
-
486
- // 处理 linkedSpaces
487
- const linkedSpaceIds = agentConfig.linkedSpaces || [];
488
- if (linkedSpaceIds.length > 0 && spaceContextLevel > 1) {
489
- const linkedSpacesInfo: string[] = [];
490
-
491
- for (const spaceId of linkedSpaceIds) {
492
- const spaceKey = createSpaceKey.space(spaceId);
493
- const spaceData = await readSafe<any>(dispatch, spaceKey);
494
- if (spaceData) {
495
- const name = spaceData.name || spaceId;
496
- const desc = spaceData.description || "";
497
- linkedSpacesInfo.push(
498
- `- ${name} (ID: ${spaceId})${desc ? `: ${desc}` : ""}`,
499
- );
500
- } else {
501
- linkedSpacesInfo.push(`- [无法访问] ${spaceId}`);
502
- }
503
- }
504
-
505
- if (linkedSpacesInfo.length > 0) {
506
- const linkedSection =
507
- `\n\n--- 关联空间 (Linked Spaces) ---\n` +
508
- `以下是 Agent 可访问的其他工作空间(粗略上下文):\n` +
509
- linkedSpacesInfo.join("\n") +
510
- `\n\n提示:如需查询这些空间的详细内容,可使用 read 工具配合对应的 dbKey。`;
511
-
512
- spaceContext = (spaceContext || "") + linkedSection;
513
- }
514
- }
515
-
516
- // 当前仓库里只有 personalization 这一类“特殊流程对话”需要在普通 agent 之外
517
- // 额外追加工具/策略约束,因此先按 dialog category 做最小分流。
518
- // 如果未来出现多个稳定存在的流程型入口(例如 onboarding / agent-authoring / skill-authoring),
519
- // 再考虑把这层升级成统一的 dialog mode 抽象。
520
- const dialogPolicyContext =
521
- dialogConfig?.category === PERSONALIZATION_DIALOG_CATEGORY
522
- ? buildPersonalizationDialogPolicyContext()
523
- : null;
524
-
525
- return {
526
- botInstructionsContext: joinMapValues(botInstructionsMap),
527
- botKnowledgeContext: joinMapValues(botKnowledgeMap),
528
- spaceContext,
529
- userGlobalPrompt: globalPrompt,
530
- userPolicyContext: [
531
- buildStaticUserPolicyContext({
532
- agentConfig,
533
- settingsRecord: {
534
- userTonePreset,
535
- knowledgeCaptureLevel,
536
- spaceContextLevel,
537
- enableReadCurrentSpace: spaceContextLevel > 1,
538
- },
539
- }),
540
- dialogPolicyContext,
541
- ]
542
- .filter(Boolean)
543
- .join("\n"),
544
- };
545
- };
546
-
547
- /**
548
- * 构建动态上下文(每轮 Loop 调用)
549
- * 包含:currentInput、history、editingContext、dialogSummary
550
- * 这些内容可能每轮变化,需要实时更新
551
- */
552
- export const buildDynamicContexts = async (
553
- state: RootState,
554
- dispatch: any,
555
- agentConfig: Agent,
556
- userInput: string | any[],
557
- runtimeOptions?: AgentRuntimeOptions,
558
- referenceContentCache?: Map<string, any>,
559
- dialogKey?: string,
560
- ): Promise<DynamicContexts> => {
561
- // 读取 dialog summary 和 config
562
- let dialogSummary: string | null = null;
563
- let dialogConfig: DialogConfig | undefined;
564
- if (dialogKey) {
565
- dialogConfig = selectById(state, dialogKey) as DialogConfig | undefined;
566
- if (dialogConfig?.summary) {
567
- dialogSummary = dialogConfig.summary;
568
- }
569
- }
570
-
571
- const keySets = await getFullChatContextKeys(
572
- state,
573
- dispatch,
574
- agentConfig,
575
- userInput,
576
- dialogConfig,
577
- );
578
- const finalKeys = deduplicateContextKeys(keySets);
579
-
580
- const fetchReferences = (keys: string[]) =>
581
- fetchReferenceContents(keys, dispatch, {
582
- format: "simplified_markdown",
583
- inlineMentionMeta: true,
584
- preloaded: referenceContentCache,
585
- });
586
-
587
- // 只获取动态引用内容
588
- const [currentInputMap, historyMap] = await Promise.all([
589
- fetchReferences(finalKeys.currentInputContext),
590
- fetchReferences(finalKeys.historyContext),
591
- ]);
592
-
593
- const pendingFiles = selectPendingFiles(state);
594
- const formattedCurrentInputContext = formatCurrentInputContext(
595
- pendingFiles,
596
- currentInputMap,
597
- );
598
-
599
- const dialogId = dialogConfig?.id ?? null;
600
- const currentDialogMessages = selectAllMsgs(state, dialogId);
601
- const currentDialogMessageIds = new Set(currentDialogMessages.map((msg) => msg.id));
602
- const currentDialogToolRuns = selectAllToolRuns(state).filter((run) =>
603
- currentDialogMessageIds.has(run.messageId)
604
- );
605
-
606
- const editingContext = buildEditingContextSummary(state, runtimeOptions);
607
- const appWorkingMemory = buildRecentAppToolMemory(
608
- currentDialogMessages,
609
- currentDialogToolRuns,
610
- );
611
-
612
- return {
613
- currentInputContext: formattedCurrentInputContext.trim() || null,
614
- historyContext: joinMapValues(historyMap),
615
- editingContext,
616
- appWorkingMemory,
617
- dialogSummary,
618
- proactiveSummary: dialogConfig?.proactiveSummary || null,
619
- referenceKeys: dialogConfig?.referenceKeys || [],
620
- };
621
- };
622
-
623
- /**
624
- * 合并静态和动态上下文为完整的 Contexts 对象
625
- */
626
- export const mergeContexts = (
627
- staticCtx: StaticContexts,
628
- dynamicCtx: DynamicContexts,
629
- ): Contexts => ({
630
- botInstructionsContext: staticCtx.botInstructionsContext || undefined,
631
- botKnowledgeContext: staticCtx.botKnowledgeContext || undefined,
632
- spaceContext: staticCtx.spaceContext || undefined,
633
- userGlobalPrompt: staticCtx.userGlobalPrompt || undefined,
634
- userPolicyContext: staticCtx.userPolicyContext || undefined,
635
- currentInputContext: dynamicCtx.currentInputContext,
636
- historyContext: dynamicCtx.historyContext || undefined,
637
- editingContext: dynamicCtx.editingContext,
638
- appWorkingMemory: dynamicCtx.appWorkingMemory,
639
- dialogSummary: dynamicCtx.dialogSummary,
640
- proactiveSummary: dynamicCtx.proactiveSummary,
641
- referenceKeys: dynamicCtx.referenceKeys,
642
- });
643
-
644
- /** 合并 Agent.tools + Context Page Tools + Mentioned Tools + runtimeOptions.extraTools */
645
- export const mergeAgentToolsWithRuntime = (
646
- agentConfig: Agent,
647
- referencedTools: string[],
648
- mentionedTools: string[],
649
- runtimeOptions?: AgentRuntimeOptions,
650
- state?: RootState,
651
- ): Agent => {
652
- const rawBaseTools = Array.isArray((agentConfig as any).tools)
653
- ? ((agentConfig as any).tools as string[])
654
- : [];
655
- const baseTools = canonicalizeToolNames(rawBaseTools);
656
- const requiredSkillTools = canonicalizeToolNames(
657
- (agentConfig as any).referencedTools ?? []
658
- );
659
- const recommendedSkillTools = canonicalizeToolNames(
660
- (agentConfig as any).recommendedSkillTools ?? []
661
- );
662
- const recommendedSkillHints = Array.from(
663
- new Set(
664
- ((agentConfig as any).recommendedSkillHints ?? []).filter(
665
- (item: unknown): item is string =>
666
- typeof item === "string" && item.trim().length > 0,
667
- ),
668
- ),
669
- );
670
- const skillPromptPatches = Array.from(
671
- new Set(
672
- ((agentConfig as any).skillPromptPatches ?? []).filter(
673
- (item: unknown): item is string =>
674
- typeof item === "string" && item.trim().length > 0,
675
- ),
676
- ),
677
- );
678
- const enhancedTools = new Set<string>([
679
- ...baseTools,
680
- ...TOOL_PACKS.CORE,
681
- ...(baseTools.length > 0 ? TOOL_PACKS.LIGHT_WEB : []),
682
- ...requiredSkillTools,
683
- ...canonicalizeToolNames(referencedTools),
684
- ...canonicalizeToolNames(mentionedTools),
685
- ]);
686
-
687
- // Intelligence: If user explicitly added ANY browser tool, auto-inject the FULL browser pack
688
- const hasAnyBrowserTool = baseTools.some((t) => t.startsWith("browser_"));
689
- if (hasAnyBrowserTool) {
690
- TOOL_PACKS.FULL_BROWSER.forEach((t) => enhancedTools.add(t));
691
- }
692
-
693
- const extraTools = canonicalizeToolNames(runtimeOptions?.extraTools ?? []);
694
- for (const t of extraTools) {
695
- enhancedTools.add(t);
696
- }
697
-
698
- const viewMode = state ? selectViewMode(state) : "categories";
699
- if (viewMode === "all") {
700
- enhancedTools.delete("search_workspace");
701
- enhancedTools.add("search_all_spaces");
702
- } else {
703
- enhancedTools.delete("search_all_spaces");
704
- enhancedTools.add("search_workspace");
705
- }
706
-
707
- return {
708
- ...agentConfig,
709
- tools: prioritizeToolNames(
710
- Array.from(enhancedTools),
711
- recommendedSkillTools,
712
- ),
713
- recommendedSkillTools,
714
- recommendedSkillHints,
715
- skillPromptPatches,
716
- };
717
- };
718
-
719
- /** 应用本轮图片配置 override */
720
- export const applyImageConfigRuntimeOverride = (
721
- agentConfig: Agent,
722
- runtimeOptions?: AgentRuntimeOptions,
723
- ): Agent => {
724
- const override = runtimeOptions?.imageConfigOverride;
725
- if (!override) {
726
- return agentConfig;
727
- }
728
-
729
- const baseImageConfig = (agentConfig as any).imageConfig ?? {};
730
-
731
- return {
732
- ...agentConfig,
733
- imageConfig: {
734
- ...baseImageConfig,
735
- ...override,
736
- },
737
- };
738
- };