nolo-cli 0.1.13 → 0.1.14

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 (320) 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/agentRuntimeCommands.ts +139 -22
  8. package/agentRuntimeLocal.ts +7 -0
  9. package/ai/agent/_executeModel.ts +118 -0
  10. package/ai/agent/agentSlice.ts +544 -1
  11. package/ai/agent/appWorkingMemory.ts +126 -0
  12. package/ai/agent/avatarUtils.ts +24 -0
  13. package/ai/agent/buildEditingContext.ts +373 -0
  14. package/ai/agent/buildSystemPrompt.ts +532 -0
  15. package/ai/agent/cleanAgentMessages.ts +140 -0
  16. package/ai/agent/cliChatClient.ts +119 -0
  17. package/ai/agent/contextCompiler.ts +107 -0
  18. package/ai/agent/contextLayerContract.ts +44 -0
  19. package/ai/agent/createAgentSchema.ts +234 -0
  20. package/ai/agent/executeToolCall.ts +58 -0
  21. package/ai/agent/fetchAgentContexts.ts +42 -0
  22. package/ai/agent/generatePrompt.ts +3 -0
  23. package/ai/agent/getFullChatContextKeys.ts +168 -0
  24. package/ai/agent/hooks/fetchPublicAgents.ts +133 -0
  25. package/ai/agent/hooks/useAgentConfig.ts +61 -0
  26. package/ai/agent/hooks/useAgentDialog.ts +35 -0
  27. package/ai/agent/hooks/useAgentFormValidation.ts +202 -0
  28. package/ai/agent/hooks/usePublicAgents.ts +473 -0
  29. package/ai/agent/persistMessageWithFixedId.ts +37 -0
  30. package/ai/agent/planSlice.ts +259 -0
  31. package/ai/agent/referenceUtils.ts +229 -0
  32. package/ai/agent/runAgentBackground.ts +238 -0
  33. package/ai/agent/runAgentClientLoop.ts +138 -0
  34. package/ai/agent/runtimeGuidance.ts +97 -0
  35. package/ai/agent/runtimeServerBase.ts +37 -0
  36. package/ai/agent/server/fetchPublicAgents.ts +128 -0
  37. package/ai/agent/startParallelAgentStreams.ts +424 -0
  38. package/ai/agent/startupProtocol.ts +53 -0
  39. package/ai/agent/streamAgentChatTurn.ts +1299 -0
  40. package/ai/agent/streamAgentChatTurnUtils.ts +738 -0
  41. package/ai/agent/types.ts +71 -0
  42. package/ai/agent/utils/imageOutput.ts +39 -0
  43. package/ai/agent/utils/publicImageAgentMode.ts +26 -0
  44. package/ai/agent/utils/sortUtils.ts +250 -0
  45. package/ai/agent/web/referencePickerUtils.ts +146 -0
  46. package/ai/ai.locale.ts +1083 -0
  47. package/ai/chat/accumulateToolCallChunks.ts +95 -0
  48. package/ai/chat/fetchUtils.native.ts +276 -0
  49. package/ai/chat/fetchUtils.ts +153 -0
  50. package/ai/chat/inlineImageUrlsForCustomProvider.ts +117 -0
  51. package/ai/chat/parseApiError.ts +64 -0
  52. package/ai/chat/parseMultilineSSE.ts +95 -0
  53. package/ai/chat/sendOpenAICompletionsRequest.native.ts +682 -0
  54. package/ai/chat/sendOpenAICompletionsRequest.ts +712 -0
  55. package/ai/chat/sendOpenAIResponseRequest.ts +512 -0
  56. package/ai/chat/shouldUseServerProxy.ts +18 -0
  57. package/ai/chat/sseClient.native.ts +91 -0
  58. package/ai/chat/sseClient.ts +67 -0
  59. package/ai/chat/streamReader.native.ts +31 -0
  60. package/ai/chat/streamReader.ts +62 -0
  61. package/ai/chat/updateTotalUsage.ts +72 -0
  62. package/ai/context/buildReferenceContext.ts +437 -0
  63. package/ai/context/calculateContextUsage.ts +133 -0
  64. package/ai/context/retention.ts +165 -0
  65. package/ai/context/tokenUtils.ts +78 -0
  66. package/ai/index.ts +1 -1
  67. package/ai/llm/agentCapabilities.ts +74 -0
  68. package/ai/llm/calculateGeminiImageTokens.ts +57 -0
  69. package/ai/llm/deepinfra.ts +28 -0
  70. package/ai/llm/fireworks.ts +68 -0
  71. package/ai/llm/generateRequestBody.ts +165 -0
  72. package/ai/llm/getModelContextWindow.ts +84 -0
  73. package/ai/llm/getNoloKey.ts +37 -0
  74. package/ai/llm/getPricing.ts +232 -0
  75. package/ai/llm/hooks/useModelPricing.ts +75 -0
  76. package/ai/llm/imagePricing.ts +66 -0
  77. package/ai/llm/isResponseAPIModel.ts +13 -0
  78. package/ai/llm/kimi.ts +18 -0
  79. package/ai/llm/mimo.ts +71 -0
  80. package/ai/llm/mistral.ts +22 -0
  81. package/ai/llm/modelAvatar.ts +427 -0
  82. package/ai/llm/models.ts +45 -0
  83. package/ai/llm/openrouterModels.ts +141 -0
  84. package/ai/llm/providers.ts +307 -0
  85. package/ai/llm/reasoningModels.ts +28 -0
  86. package/ai/llm/types.ts +59 -0
  87. package/ai/llm/usageRequestOptions.ts +59 -0
  88. package/ai/memory/capture.ts +148 -0
  89. package/ai/memory/consolidate.ts +104 -0
  90. package/ai/memory/delete.ts +147 -0
  91. package/ai/memory/overlay.ts +84 -0
  92. package/ai/memory/query.ts +38 -0
  93. package/ai/memory/queryShared.ts +160 -0
  94. package/ai/memory/rank.ts +105 -0
  95. package/ai/memory/recentRelationshipRecap.ts +247 -0
  96. package/ai/memory/remember.ts +167 -0
  97. package/ai/memory/runtime.ts +76 -0
  98. package/ai/memory/store.ts +20 -0
  99. package/ai/memory/storeShared.ts +76 -0
  100. package/ai/memory/types.ts +46 -0
  101. package/ai/memory/understanding.ts +349 -0
  102. package/ai/memory/understandingGreeting.ts +264 -0
  103. package/ai/messages/type.ts +20 -0
  104. package/ai/policy/personalizationDialog.ts +333 -0
  105. package/ai/policy/runtimePolicy.ts +440 -0
  106. package/ai/policy/selfUpdateFields.ts +48 -0
  107. package/ai/policy/types.ts +64 -0
  108. package/ai/skills/referenceRuntime.ts +274 -0
  109. package/ai/skills/skillDiagnostics.ts +251 -0
  110. package/ai/skills/skillDocBuilder.ts +139 -0
  111. package/ai/skills/skillDocProtocol.ts +434 -0
  112. package/ai/skills/skillReferenceSummary.ts +63 -0
  113. package/ai/skills/skillSummaryMarker.ts +26 -0
  114. package/ai/token/calculatePrice.ts +546 -0
  115. package/ai/token/db.ts +98 -0
  116. package/ai/token/externalToolCost.ts +321 -0
  117. package/ai/token/hooks/useRecords.ts +65 -0
  118. package/ai/token/missingUsageEstimate.ts +42 -0
  119. package/ai/token/modelUsageQuery.ts +252 -0
  120. package/ai/token/normalizeUsage.ts +84 -0
  121. package/ai/token/openaiImageGenerationUsage.ts +56 -0
  122. package/ai/token/prepareTokenUsageData.ts +88 -0
  123. package/ai/token/query.ts +88 -0
  124. package/ai/token/queryUserTokens.ts +59 -0
  125. package/ai/token/resolveBillingTarget.ts +52 -0
  126. package/ai/token/saveTokenRecord.ts +53 -0
  127. package/ai/token/serverDialogProjection.ts +78 -0
  128. package/ai/token/serverTokenWriter.ts +143 -0
  129. package/ai/token/stats.ts +21 -0
  130. package/ai/token/tokenThunks.ts +24 -0
  131. package/ai/token/types.ts +93 -0
  132. package/ai/tools/agent/agentTools.ts +176 -0
  133. package/ai/tools/agent/agentUpdateShared.ts +311 -0
  134. package/ai/tools/agent/callAgentTool.ts +139 -0
  135. package/ai/tools/agent/createAgentTool.ts +512 -0
  136. package/ai/tools/agent/createDialogTool.ts +69 -0
  137. package/ai/tools/agent/createSkillAgentTool.ts +62 -0
  138. package/ai/tools/agent/parallelBudget.ts +221 -0
  139. package/ai/tools/agent/presets/appBuilderPreset.ts +147 -0
  140. package/ai/tools/agent/runLlmTool.ts +96 -0
  141. package/ai/tools/agent/runStreamingAgentTool.ts +73 -0
  142. package/ai/tools/agent/skillAgentArgs.ts +106 -0
  143. package/ai/tools/agent/skillAgentPreset.ts +89 -0
  144. package/ai/tools/agent/streamParallelAgentsTool.ts +122 -0
  145. package/ai/tools/agent/updateAgentTool.ts +96 -0
  146. package/ai/tools/agent/updateSelfTool.ts +113 -0
  147. package/ai/tools/amazonProductScraperTool.ts +86 -0
  148. package/ai/tools/apifyActorClient.ts +45 -0
  149. package/ai/tools/appEditGuard.ts +372 -0
  150. package/ai/tools/appReadSnapshot.ts +153 -0
  151. package/ai/tools/appTools.ts +1549 -0
  152. package/ai/tools/applyEditTool.ts +256 -0
  153. package/ai/tools/applyLineEditsTool.ts +312 -0
  154. package/ai/tools/browserTools/click.ts +33 -0
  155. package/ai/tools/browserTools/closeSession.ts +29 -0
  156. package/ai/tools/browserTools/common.ts +27 -0
  157. package/ai/tools/browserTools/openSession.ts +48 -0
  158. package/ai/tools/browserTools/readContent.ts +38 -0
  159. package/ai/tools/browserTools/selectOption.ts +46 -0
  160. package/ai/tools/browserTools/typeText.ts +42 -0
  161. package/ai/tools/category/createCategoryTool.ts +66 -0
  162. package/ai/tools/category/queryContentsByCategoryTool.ts +69 -0
  163. package/ai/tools/category/updateContentCategoryTool.ts +75 -0
  164. package/ai/tools/cfBrowserTools.ts +319 -0
  165. package/ai/tools/cfSpeechToTextTool.ts +49 -0
  166. package/ai/tools/checkEnvTool.ts +65 -0
  167. package/ai/tools/cloudflareCrawlTool.ts +289 -0
  168. package/ai/tools/codeSearchTool.ts +111 -0
  169. package/ai/tools/codeTools.ts +101 -0
  170. package/ai/tools/createDocTool.ts +132 -0
  171. package/ai/tools/createPlanTool.ts +999 -0
  172. package/ai/tools/createSkillDocTool.ts +155 -0
  173. package/ai/tools/createWorkflowTool.ts +154 -0
  174. package/ai/tools/deepseekOcrTool.ts +34 -0
  175. package/ai/tools/delayTool.ts +31 -0
  176. package/ai/tools/deleteSpacesTool.ts +325 -0
  177. package/ai/tools/deleteSpacesToolModel.ts +159 -0
  178. package/ai/tools/devReloadUtils.ts +29 -0
  179. package/ai/tools/dialogMessageSearch.ts +137 -0
  180. package/ai/tools/doctorSkillTool.ts +72 -0
  181. package/ai/tools/ecommerceScraperTool.ts +86 -0
  182. package/ai/tools/emailTools.ts +549 -0
  183. package/ai/tools/evalSkillTool.ts +92 -0
  184. package/ai/tools/exaSearchTool.ts +64 -0
  185. package/ai/tools/execBashTool.ts +379 -0
  186. package/ai/tools/executeSqlTool.ts +192 -0
  187. package/ai/tools/fetchWebpageSupport.ts +309 -0
  188. package/ai/tools/fetchWebpageTool.ts +84 -0
  189. package/ai/tools/geminiImagePreviewTool.ts +361 -0
  190. package/ai/tools/generateDocxTool.ts +215 -0
  191. package/ai/tools/googleSearchScraperTool.ts +106 -0
  192. package/ai/tools/importDataTool.ts +133 -0
  193. package/ai/tools/importSkillTool.ts +162 -0
  194. package/ai/tools/index.ts +1927 -0
  195. package/ai/tools/listFilesTool.ts +82 -0
  196. package/ai/tools/listUserSpacesTool.ts +113 -0
  197. package/ai/tools/modelUsageTools.ts +199 -0
  198. package/ai/tools/olmOcrTool.ts +34 -0
  199. package/ai/tools/openaiImageTool.ts +267 -0
  200. package/ai/tools/prepareTools.ts +23 -0
  201. package/ai/tools/readDocTool.ts +84 -0
  202. package/ai/tools/readFileTool.ts +211 -0
  203. package/ai/tools/readTool.ts +163 -0
  204. package/ai/tools/readXPostTool.ts +233 -0
  205. package/ai/tools/rememberMemoryTool.ts +84 -0
  206. package/ai/tools/remotionVideoTool.ts +151 -0
  207. package/ai/tools/searchDialogMessagesTool.ts +222 -0
  208. package/ai/tools/searchRepoTool.ts +115 -0
  209. package/ai/tools/searchWorkspaceTool.ts +259 -0
  210. package/ai/tools/skillFollowup.ts +86 -0
  211. package/ai/tools/surfWeatherTool.ts +169 -0
  212. package/ai/tools/table/addTableRowTool.ts +217 -0
  213. package/ai/tools/table/createTableTool.ts +315 -0
  214. package/ai/tools/table/rowTools.ts +366 -0
  215. package/ai/tools/table/schemaTools.ts +244 -0
  216. package/ai/tools/table/shareTableTool.ts +148 -0
  217. package/ai/tools/table/toolShared.ts +129 -0
  218. package/ai/tools/toolApiClient.ts +198 -0
  219. package/ai/tools/toolNameAliases.ts +57 -0
  220. package/ai/tools/toolResultError.ts +42 -0
  221. package/ai/tools/toolRunSlice.ts +303 -0
  222. package/ai/tools/toolSchemaCompatibility.ts +53 -0
  223. package/ai/tools/toolVisibility.ts +4 -0
  224. package/ai/tools/types.ts +20 -0
  225. package/ai/tools/uiAskChoiceTool.ts +104 -0
  226. package/ai/tools/updateContentTitleTool.ts +84 -0
  227. package/ai/tools/updateDocTool.ts +105 -0
  228. package/ai/tools/updateUserPreferenceProfileTool.ts +145 -0
  229. package/ai/tools/whisperTool.ts +77 -0
  230. package/ai/tools/writeFileTool.ts +210 -0
  231. package/ai/tools/youtubeScraperTool.ts +116 -0
  232. package/ai/tools/ziweiChartTool.ts +678 -0
  233. package/ai/types.ts +55 -0
  234. package/ai/workflow/workflowExecutor.ts +323 -0
  235. package/ai/workflow/workflowSlice.ts +73 -0
  236. package/ai/workflow/workflowTypes.ts +106 -0
  237. package/client/agentRun.test.ts +240 -0
  238. package/client/agentRun.ts +182 -19
  239. package/client/compactDialog.test.ts +238 -0
  240. package/client/localRuntimeAdapter.test.ts +135 -0
  241. package/client/localRuntimeAdapter.ts +244 -0
  242. package/client/profileConfig.test.ts +40 -0
  243. package/client/streamingOutput.test.ts +22 -0
  244. package/client/streamingOutput.ts +38 -0
  245. package/commandRegistry.ts +9 -2
  246. package/connector-experimental/index.ts +5 -0
  247. package/database/actions/cacheMergedUserData.ts +64 -0
  248. package/database/actions/common.ts +242 -0
  249. package/database/actions/deleteFile.ts +40 -0
  250. package/database/actions/fetchUserData.ts +16 -0
  251. package/database/actions/fileContent.ts +125 -0
  252. package/database/actions/patch.ts +155 -0
  253. package/database/actions/read.ts +337 -0
  254. package/database/actions/readAndWait.ts +224 -0
  255. package/database/actions/readRequestManager.ts +120 -0
  256. package/database/actions/remove.ts +94 -0
  257. package/database/actions/replication.ts +366 -0
  258. package/database/actions/upload.ts +174 -0
  259. package/database/actions/upsert.ts +56 -0
  260. package/database/actions/write.ts +126 -0
  261. package/database/client/db.native.ts +73 -0
  262. package/database/client/db.ts +51 -0
  263. package/database/client/fetchUserData.ts +61 -0
  264. package/database/client/handleError.ts +19 -0
  265. package/database/client/queryRequest.ts +21 -0
  266. package/database/config.ts +21 -0
  267. package/database/dbActionThunks.ts +1 -0
  268. package/database/dbSlice.ts +149 -0
  269. package/database/email.ts +42 -0
  270. package/database/fileRing.ts +51 -0
  271. package/database/fileSharding.ts +70 -0
  272. package/database/fileStorage.native.ts +92 -0
  273. package/database/fileStorage.ts +232 -0
  274. package/database/fileUrl.ts +34 -0
  275. package/database/hooks/useUserData.ts +489 -0
  276. package/database/index.ts +1 -0
  277. package/database/keys.ts +765 -0
  278. package/database/queryPrefixes.ts +14 -0
  279. package/database/requests.ts +443 -0
  280. package/database/runtimeServerContext.ts +35 -0
  281. package/database/server/MemoryDB.ts +76 -0
  282. package/database/server/actorAccess.ts +76 -0
  283. package/database/server/agentDelegation.ts +124 -0
  284. package/database/server/coreDataOwnership.ts +13 -0
  285. package/database/server/coreDataProxy.ts +76 -0
  286. package/database/server/cybotReadonly.ts +18 -0
  287. package/database/server/dataHandlers.ts +111 -0
  288. package/database/server/db.ts +118 -0
  289. package/database/server/dbPath.ts +20 -0
  290. package/database/server/delete.ts +499 -0
  291. package/database/server/emailRepository.ts +1480 -0
  292. package/database/server/ensureDbOpen.ts +12 -0
  293. package/database/server/fileRead.ts +337 -0
  294. package/database/server/fileService.ts +436 -0
  295. package/database/server/handleTransaction.ts +86 -0
  296. package/database/server/patch.ts +282 -0
  297. package/database/server/query.ts +138 -0
  298. package/database/server/read.ts +325 -0
  299. package/database/server/resourceAccess.ts +211 -0
  300. package/database/server/routes.ts +110 -0
  301. package/database/server/spaceMemberAuthority.ts +67 -0
  302. package/database/server/upload.ts +159 -0
  303. package/database/server/write.ts +494 -0
  304. package/database/server/writeAuthority.ts +133 -0
  305. package/database/sqliteDb.ts +46 -0
  306. package/database/table/deleteTable.ts +120 -0
  307. package/database/tenantPlacement.ts +57 -0
  308. package/database/tombstones.ts +52 -0
  309. package/database/userDataLoadDecision.ts +17 -0
  310. package/database/userDataMerge.ts +95 -0
  311. package/database/userPreferenceRegister.ts +108 -0
  312. package/database/utils/dbPath.ts +47 -0
  313. package/database/utils/ulid.native.ts +6 -0
  314. package/database/utils/ulid.ts +1 -0
  315. package/index.ts +25 -15
  316. package/localRuntimeDb.ts +28 -0
  317. package/package.json +16 -4
  318. package/runtimeModeArgs.ts +33 -0
  319. package/tui/readlineWorkspace.ts +1 -0
  320. package/tui/session.ts +22 -0
@@ -0,0 +1,1299 @@
1
+ // 文件路径: packages/ai/agent/streamAgentChatTurn.ts
2
+ import { extractCustomId } from "core/prefix";
3
+ import { createDialogMessageKeyAndId } from "database/keys";
4
+ import { DataType } from "create/types";
5
+
6
+ import type { RootState } from "app/store";
7
+ import { patch, read } from "database/dbSlice";
8
+ import { generateRequestBody } from "ai/llm/generateRequestBody";
9
+ import {
10
+ selectCurrentDialogConfig,
11
+ selectDialogConfigByKey,
12
+ addActiveController,
13
+ removeActiveController,
14
+ selectPendingUserInputQueue,
15
+ dequeueUserInput,
16
+ clearPendingUserInputQueue,
17
+ } from "chat/dialog/dialogSlice";
18
+ import { removeTransientMessage, selectAllMsgs } from "chat/messages/messageSlice";
19
+ import {
20
+ selectContextRetention,
21
+ selectMaxExecutionTime,
22
+ selectCurrentServer,
23
+ } from "app/settings/settingSlice";
24
+ import { filterAndCleanMessages } from "integrations/openai/filterAndCleanMessages";
25
+ import {
26
+ getFullChatContextKeys,
27
+ deduplicateContextKeys,
28
+ } from "ai/agent/getFullChatContextKeys";
29
+ import type { Agent } from "app/types";
30
+ import { isResponseAPIModel } from "ai/llm/isResponseAPIModel";
31
+ import { getModelContextWindow } from "ai/llm/getModelContextWindow";
32
+ import { resolveAgentImageInputSupport } from "ai/llm/agentCapabilities";
33
+
34
+ import {
35
+ sendOpenAICompletionsRequest,
36
+ type CompletionMeta,
37
+ } from "../chat/sendOpenAICompletionsRequest";
38
+ import { sendOpenAIResponseRequest } from "../chat/sendOpenAIResponseRequest";
39
+
40
+ import type { AgentRuntimeOptions } from "./types";
41
+ import { buildAgentViewMessages } from "./cleanAgentMessages";
42
+ import { extractCategorizedMentions, type CategorizedMentions } from "create/editor/utils/slateUtils";
43
+ import { resolveReferenceAssets, resolveToolsFromKeys } from "./referenceUtils";
44
+ import { estimateTokenCount } from "ai/context/tokenUtils";
45
+ import {
46
+ applyImageConfigRuntimeOverride,
47
+ buildStaticContexts,
48
+ compressOldToolResults,
49
+ buildDynamicContexts,
50
+ mergeContexts,
51
+ hasImageInMessages,
52
+ mergeAgentToolsWithRuntime,
53
+ trimMessagesWithSummary,
54
+ validateAccessAndBalance,
55
+ } from "./streamAgentChatTurnUtils";
56
+ import { buildCliPrompt } from "./cliPrompt";
57
+ import { createCliChatTurnStream } from "./cliChatClient";
58
+ import { getCliChatSession, startCliChatSession } from "./cliChatClient";
59
+ import { messageStreaming, prepareAndPersistUserMessage } from "chat/messages/messageSlice";
60
+ import { selectCurrentToken, selectUserId } from "auth/authSlice";
61
+ import { persistMessageWithFixedId } from "./persistMessageWithFixedId";
62
+ import { updateTotalUsage } from "../chat/updateTotalUsage";
63
+ import { createSSEParser } from "../chat/parseMultilineSSE";
64
+ import {
65
+ extractAgentRuntimeServerBase,
66
+ normalizeServerOrigin,
67
+ } from "./runtimeServerBase";
68
+
69
+ const buildParallelMessageMetadata = (
70
+ agentConfig: Pick<Agent, "dbKey" | "name">,
71
+ runtimeOptions?: AgentRuntimeOptions,
72
+ ) => {
73
+ const rawName =
74
+ typeof agentConfig?.name === "string" ? agentConfig.name.trim() : "";
75
+ return {
76
+ agentKey: agentConfig.dbKey,
77
+ cybotKey: agentConfig.dbKey,
78
+ ...(rawName ? { agentName: rawName } : {}),
79
+ ...(runtimeOptions?.parallelSessionId
80
+ ? { parallelSessionId: runtimeOptions.parallelSessionId }
81
+ : {}),
82
+ ...(runtimeOptions?.parallelBranchId
83
+ ? { parallelBranchId: runtimeOptions.parallelBranchId }
84
+ : {}),
85
+ ...(runtimeOptions?.parallelLabel
86
+ ? { parallelLabel: runtimeOptions.parallelLabel }
87
+ : {}),
88
+ ...(runtimeOptions?.parallelIndex !== undefined
89
+ ? { parallelIndex: runtimeOptions.parallelIndex }
90
+ : {}),
91
+ };
92
+ };
93
+
94
+ const filterMessagesForParallelBranch = (
95
+ messages: any[],
96
+ runtimeOptions?: AgentRuntimeOptions,
97
+ ) => {
98
+ if (!runtimeOptions?.parallelSessionId) return messages;
99
+
100
+ return messages.filter((message: any) => {
101
+ const sessionId = message?.parallelSessionId;
102
+ if (sessionId !== runtimeOptions.parallelSessionId) {
103
+ return true;
104
+ }
105
+ return message?.parallelBranchId === runtimeOptions.parallelBranchId;
106
+ });
107
+ };
108
+
109
+ const formatMachineAgentRunError = async (response: Response): Promise<string> => {
110
+ const errorText = await response.text();
111
+ let payload: any = null;
112
+ try {
113
+ payload = errorText ? JSON.parse(errorText) : null;
114
+ } catch {
115
+ payload = null;
116
+ }
117
+
118
+ const reason = typeof payload?.reason === "string" ? payload.reason : "";
119
+ if (response.status === 409) {
120
+ if (reason === "bound_machine_unavailable") {
121
+ return "绑定的电脑不在线。请确认这台电脑已开机并重新运行连接命令。";
122
+ }
123
+ if (reason === "connector_offline") {
124
+ return "电脑在线,但连接器未连接。请在这台电脑上重新运行连接命令后再试。";
125
+ }
126
+ if (reason === "missing_capability") {
127
+ return "这台电脑没有对应的 CLI 能力。请安装对应 CLI,或把 Agent 绑定到另一台电脑。";
128
+ }
129
+ }
130
+
131
+ const message =
132
+ typeof payload?.message === "string" && payload.message.trim()
133
+ ? payload.message.trim()
134
+ : typeof payload?.error === "string" && payload.error.trim()
135
+ ? payload.error.trim()
136
+ : errorText.trim();
137
+ return message || `Machine agent run failed (${response.status})`;
138
+ };
139
+
140
+ /** streamAgentChatTurn 参数(聊天轮次专用) */
141
+ export interface StreamAgentChatTurnArgs {
142
+ agentKey: string;
143
+ userInput: string | any[];
144
+ serverBase?: string;
145
+ dialogKey?: string; // 可选。显式指定目标对话,不传则使用当前活跃对话。
146
+ isStreaming?: boolean;
147
+ parentMessageId?: string;
148
+ runtimeOptions?: AgentRuntimeOptions;
149
+ }
150
+
151
+ const normalizeAgentRunUserInput = (userInput: string | any[]) => {
152
+ if (typeof userInput === "string") {
153
+ return userInput;
154
+ }
155
+ if (!Array.isArray(userInput)) {
156
+ return "";
157
+ }
158
+ return userInput.filter((part) => {
159
+ if (!part || typeof part !== "object") return false;
160
+ if (part.type === "text") return typeof part.text === "string";
161
+ return (
162
+ part.type === "image_url" &&
163
+ typeof part.image_url?.url === "string" &&
164
+ !!part.image_url.url.trim()
165
+ );
166
+ });
167
+ };
168
+
169
+ const extractAgentRunUserText = (userInput: string | any[]) => {
170
+ if (typeof userInput === "string") {
171
+ return userInput;
172
+ }
173
+ if (!Array.isArray(userInput)) {
174
+ return "";
175
+ }
176
+ return userInput
177
+ .filter((part) => part?.type === "text" && typeof part.text === "string")
178
+ .map((part) => part.text)
179
+ .join("\n")
180
+ .trim();
181
+ };
182
+
183
+ const hasAgentRunUserInputContent = (userInput: string | any[]) => {
184
+ if (typeof userInput === "string") {
185
+ return userInput.trim().length > 0;
186
+ }
187
+ return Array.isArray(userInput) && userInput.length > 0;
188
+ };
189
+
190
+ /**
191
+ * 真正用于“聊天轮次”的流式 Agent 调用(带 Agent Loop):
192
+ * - 每轮检查权限 & 余额
193
+ * - 每轮重建上下文 & 消息
194
+ * - 调用 Completions/Response API
195
+ * - 对 Completions 模型,基于 tool_calls / handoff / pending 决定是否继续
196
+ */
197
+ export const streamAgentChatTurnHandler = async (
198
+ args: StreamAgentChatTurnArgs,
199
+ thunkApi: any,
200
+ ) => {
201
+ const { agentKey, userInput, dialogKey: explicitDialogKey, parentMessageId, runtimeOptions } = args;
202
+ const { getState, dispatch, rejectWithValue } = thunkApi;
203
+ const state = getState() as RootState;
204
+
205
+ // 🚀 额外引入一个 Loop 控制器,用于中止整个 Agent 循环
206
+ const loopController = new AbortController();
207
+ const isAbortError = (error?: { name?: string } | null) =>
208
+ error?.name === "AbortError" || loopController.signal.aborted || thunkApi.signal.aborted;
209
+ const onAbort = () => loopController.abort();
210
+ thunkApi.signal.addEventListener("abort", onAbort);
211
+ let loopKey: string | null = null;
212
+ let runtimeDialogKey: string | null = explicitDialogKey ?? null;
213
+ let remoteTransientMessageId: string | null = null;
214
+ let remoteTransientMessageFinalized = false;
215
+
216
+ try {
217
+ let totalTurnUsage: any = null;
218
+ const agentRunUserInput = normalizeAgentRunUserInput(userInput);
219
+ // 1. 读取 Agent 配置
220
+ const agentConfig = (await dispatch(read({ dbKey: agentKey })).unwrap()) as Agent;
221
+ if (!agentConfig) {
222
+ return rejectWithValue(`Agent config not found for ID: ${agentKey}`);
223
+ }
224
+
225
+ // ── CLI Agent 专用路由 ────────────────────────────────────────────────
226
+ // CLI 共享 prompt / model 这些入口能力,但不复用本地 tool-call 循环。
227
+ if (agentConfig.apiSource === "cli") {
228
+ const currentState = getState() as RootState;
229
+ const w =
230
+ typeof globalThis !== "undefined" && (globalThis as any).window
231
+ ? (globalThis as any).window
232
+ : null;
233
+ if (w) w.__LOOP_STOP_REASON__ = null;
234
+
235
+ const userText = extractAgentRunUserText(userInput);
236
+
237
+ const prompt = buildCliPrompt(agentConfig.prompt, userText);
238
+
239
+ // 生成消息 key
240
+ const dialogConfig =
241
+ selectDialogConfigByKey(currentState, explicitDialogKey) ??
242
+ selectCurrentDialogConfig(currentState);
243
+ if (!dialogConfig) {
244
+ return rejectWithValue("Dialog config not found");
245
+ }
246
+
247
+ const dialogKey = explicitDialogKey || dialogConfig.dbKey;
248
+ if (!dialogKey) {
249
+ return rejectWithValue("当前对话不存在,无法发送消息。");
250
+ }
251
+ runtimeDialogKey = dialogKey;
252
+ const dialogId = extractCustomId(dialogKey);
253
+ loopKey = `loop:${dialogId}`;
254
+ dispatch(addActiveController({ messageId: loopKey, controller: loopController, dialogKey }));
255
+
256
+ const { key: msgKey, messageId } = createDialogMessageKeyAndId(dialogId);
257
+ const cliMessageMetadata = buildParallelMessageMetadata(
258
+ agentConfig,
259
+ runtimeOptions,
260
+ );
261
+ const boundMachineId =
262
+ typeof (agentConfig as any).runtimeBinding?.machineId === "string"
263
+ ? (agentConfig as any).runtimeBinding.machineId.trim()
264
+ : "";
265
+
266
+ if (boundMachineId) {
267
+ const token = selectCurrentToken(currentState);
268
+ const authHeader = token ? `Bearer ${token}` : "";
269
+ const rawMessages = filterMessagesForParallelBranch(
270
+ selectAllMsgs(currentState, dialogId),
271
+ runtimeOptions,
272
+ );
273
+ const visibleMessages = buildAgentViewMessages(
274
+ rawMessages as any,
275
+ agentConfig.dbKey,
276
+ );
277
+ const cleanedMessages = filterAndCleanMessages(visibleMessages);
278
+ const currentServer = selectCurrentServer(currentState);
279
+ remoteTransientMessageId = messageId;
280
+ let accumulated = "";
281
+ let totalTurnUsage: any = undefined;
282
+ const buildMachineAssistantMessage = () => ({
283
+ id: messageId,
284
+ dbKey: msgKey,
285
+ role: "assistant" as const,
286
+ content: accumulated,
287
+ ...cliMessageMetadata,
288
+ userId: selectUserId(getState() as RootState),
289
+ });
290
+
291
+ dispatch(messageStreaming({
292
+ id: messageId,
293
+ dialogId,
294
+ dbKey: msgKey,
295
+ content: "",
296
+ role: "assistant",
297
+ ...cliMessageMetadata,
298
+ }));
299
+
300
+ const rejectMachineStream = async (message: string) => {
301
+ if (accumulated.length > 0) {
302
+ await persistMessageWithFixedId(dispatch, buildMachineAssistantMessage());
303
+ } else {
304
+ dispatch(removeTransientMessage(messageId));
305
+ }
306
+ remoteTransientMessageFinalized = true;
307
+ return rejectWithValue(message);
308
+ };
309
+
310
+ const machineResponse = await fetch(`${currentServer.replace(/\/+$/, "")}/api/agent/run`, {
311
+ method: "POST",
312
+ headers: {
313
+ "Content-Type": "application/json",
314
+ Accept: "text/event-stream",
315
+ ...(authHeader ? { Authorization: authHeader } : {}),
316
+ },
317
+ body: JSON.stringify({
318
+ agentKey,
319
+ userInput: agentRunUserInput,
320
+ messages: cleanedMessages,
321
+ stream: true,
322
+ runtimeContext: {
323
+ surface: "web",
324
+ host: "browser",
325
+ runtime: "react",
326
+ entrypoint: "chat-dialog",
327
+ capabilities: ["streaming", "dialog-ui", "machine-bound-cli"],
328
+ },
329
+ ...((dialogConfig as any)?.spaceId ? { spaceId: (dialogConfig as any).spaceId } : {}),
330
+ }),
331
+ signal: loopController.signal,
332
+ });
333
+
334
+ if (!machineResponse.ok) {
335
+ return await rejectMachineStream(
336
+ await formatMachineAgentRunError(machineResponse),
337
+ );
338
+ }
339
+
340
+ const reader = machineResponse.body?.getReader();
341
+ if (!reader) {
342
+ return await rejectMachineStream("无法读取电脑端 Agent 流式响应");
343
+ }
344
+
345
+ const decoder = new TextDecoder();
346
+ const parseSSE = createSSEParser();
347
+ const abortMachineStream = async () => {
348
+ if (w) w.__LOOP_STOP_REASON__ = "aborted";
349
+ if (accumulated.length <= 0) return;
350
+ await persistMessageWithFixedId(dispatch, buildMachineAssistantMessage());
351
+ };
352
+
353
+ try {
354
+ while (true) {
355
+ const { done, value } = await reader.read();
356
+ if (done) break;
357
+ if (loopController.signal.aborted || thunkApi.signal.aborted) {
358
+ await abortMachineStream();
359
+ return;
360
+ }
361
+
362
+ const payloads = parseSSE(
363
+ decoder.decode(value, { stream: true }),
364
+ );
365
+ for (const payload of payloads) {
366
+ if (loopController.signal.aborted || thunkApi.signal.aborted) {
367
+ await abortMachineStream();
368
+ return;
369
+ }
370
+ if (payload.type === "error") {
371
+ return await rejectMachineStream(
372
+ payload.message || "电脑端 Agent 执行失败",
373
+ );
374
+ }
375
+ if (payload.type === "text" && typeof payload.content === "string") {
376
+ accumulated += payload.content;
377
+ dispatch(messageStreaming({
378
+ id: messageId,
379
+ dialogId,
380
+ dbKey: msgKey,
381
+ content: accumulated,
382
+ role: "assistant",
383
+ ...cliMessageMetadata,
384
+ }));
385
+ }
386
+ if (payload.type === "done") {
387
+ totalTurnUsage = payload.usage;
388
+ }
389
+ }
390
+ }
391
+
392
+ if (loopController.signal.aborted || thunkApi.signal.aborted) {
393
+ await abortMachineStream();
394
+ return;
395
+ }
396
+
397
+ await persistMessageWithFixedId(dispatch, buildMachineAssistantMessage());
398
+ remoteTransientMessageFinalized = true;
399
+ } finally {
400
+ try {
401
+ await reader.cancel();
402
+ } catch {
403
+ // ignore
404
+ }
405
+ }
406
+
407
+ return {
408
+ usage: totalTurnUsage ?? undefined,
409
+ };
410
+ }
411
+
412
+ let cliSessionId = dialogConfig.cliSessionId ?? null;
413
+
414
+ // 先创建一条空的流式消息(让用户立刻看到 loading 状态)
415
+ dispatch(messageStreaming({
416
+ id: messageId,
417
+ dialogId,
418
+ dbKey: msgKey,
419
+ content: "",
420
+ role: "assistant",
421
+ ...cliMessageMetadata,
422
+ }));
423
+
424
+ const ensureCliSession = async () => {
425
+ if (cliSessionId) {
426
+ const existing = await getCliChatSession(
427
+ { getState },
428
+ { sessionId: cliSessionId },
429
+ ).catch(() => null);
430
+ if (existing?.ok && existing?.session?.sessionId) {
431
+ return cliSessionId;
432
+ }
433
+ }
434
+
435
+ const started = await startCliChatSession(
436
+ { getState },
437
+ {
438
+ cliProvider: agentConfig.cliProvider || "copilot",
439
+ model: agentConfig.model || undefined,
440
+ systemPrompt: agentConfig.prompt || undefined,
441
+ },
442
+ );
443
+
444
+ const newSessionId =
445
+ typeof started?.sessionId === "string" ? started.sessionId : null;
446
+ if (!newSessionId) {
447
+ throw new Error("无法创建 CLI session。");
448
+ }
449
+
450
+ cliSessionId = newSessionId;
451
+ const patchResult = dispatch(
452
+ patch({
453
+ dbKey: dialogKey,
454
+ changes: {
455
+ cliSessionId: newSessionId,
456
+ },
457
+ })
458
+ ) as any;
459
+ try {
460
+ if (typeof patchResult?.unwrap === "function") {
461
+ await patchResult.unwrap();
462
+ } else {
463
+ await patchResult;
464
+ }
465
+ } catch {
466
+ // Best effort only. Session still exists server-side even if dialog persistence fails.
467
+ }
468
+ return newSessionId;
469
+ };
470
+
471
+ const initialSessionId = await ensureCliSession();
472
+ let resp = await createCliChatTurnStream(
473
+ {
474
+ getState,
475
+ },
476
+ {
477
+ sessionId: initialSessionId,
478
+ prompt,
479
+ model: agentConfig.model || undefined,
480
+ },
481
+ loopController.signal,
482
+ );
483
+
484
+ if (!resp.ok && resp.status === 404) {
485
+ cliSessionId = null;
486
+ const renewedSessionId = await ensureCliSession();
487
+ resp = await createCliChatTurnStream(
488
+ {
489
+ getState,
490
+ },
491
+ {
492
+ sessionId: renewedSessionId,
493
+ prompt,
494
+ model: agentConfig.model || undefined,
495
+ },
496
+ loopController.signal,
497
+ );
498
+ }
499
+
500
+ if (!resp.ok) {
501
+ const err = await resp.json().catch(() => ({ error: resp.statusText }));
502
+ return rejectWithValue(err.error || "CLI 执行失败");
503
+ }
504
+
505
+ // 读取 SSE 流并逐步更新消息内容
506
+ const reader = resp.body?.getReader();
507
+ if (!reader) {
508
+ return rejectWithValue("无法读取流式响应");
509
+ }
510
+
511
+ let accumulated = "";
512
+ const decoder = new TextDecoder();
513
+ const buildCliAssistantMessage = () => ({
514
+ id: messageId,
515
+ dbKey: msgKey,
516
+ role: "assistant" as const,
517
+ content: accumulated,
518
+ ...cliMessageMetadata,
519
+ userId: selectUserId(getState() as RootState),
520
+ });
521
+ const abortCliStream = async () => {
522
+ if (w) w.__LOOP_STOP_REASON__ = "aborted";
523
+ if (accumulated.length <= 0) {
524
+ return;
525
+ }
526
+ await persistMessageWithFixedId(dispatch, buildCliAssistantMessage());
527
+ };
528
+
529
+ try {
530
+ while (true) {
531
+ const { done, value } = await reader.read();
532
+ if (done) break;
533
+ if (loopController.signal.aborted || thunkApi.signal.aborted) {
534
+ await abortCliStream();
535
+ return;
536
+ }
537
+
538
+ const raw = decoder.decode(value, { stream: true });
539
+ // 解析 SSE 格式 "data: {...}\n\n"
540
+ const lines = raw.split("\n");
541
+ for (const line of lines) {
542
+ if (!line.startsWith("data: ")) continue;
543
+ if (loopController.signal.aborted || thunkApi.signal.aborted) {
544
+ await abortCliStream();
545
+ return;
546
+ }
547
+ try {
548
+ const payload = JSON.parse(line.slice(6));
549
+ if (payload.error) {
550
+ return rejectWithValue(payload.error);
551
+ }
552
+ if (payload.chunk) {
553
+ if (loopController.signal.aborted || thunkApi.signal.aborted) {
554
+ await abortCliStream();
555
+ return;
556
+ }
557
+ accumulated += payload.chunk;
558
+ if (loopController.signal.aborted || thunkApi.signal.aborted) {
559
+ await abortCliStream();
560
+ return;
561
+ }
562
+ dispatch(messageStreaming({
563
+ id: messageId,
564
+ dialogId,
565
+ dbKey: msgKey,
566
+ content: accumulated,
567
+ role: "assistant",
568
+ ...cliMessageMetadata,
569
+ }));
570
+ }
571
+ // payload.done 时 stream 自然关闭,while loop 会退出
572
+ } catch {
573
+ // 忽略解析失败的行
574
+ }
575
+ }
576
+ }
577
+
578
+ if (loopController.signal.aborted || thunkApi.signal.aborted) {
579
+ await abortCliStream();
580
+ return;
581
+ }
582
+
583
+ // 持久化最终消息:用已有 ID,避免 prepareAndPersistMessage 重新生成 ID 导致重复
584
+ await persistMessageWithFixedId(dispatch, buildCliAssistantMessage());
585
+ } finally {
586
+ try {
587
+ await reader.cancel();
588
+ } catch {
589
+ // ignore
590
+ }
591
+ }
592
+
593
+ return;
594
+ }
595
+ // ─────────────────────────────────────────────────────────────────────
596
+
597
+ const currentDialog =
598
+ selectDialogConfigByKey(state, explicitDialogKey) ??
599
+ selectCurrentDialogConfig(state);
600
+ const activeDialogKey = currentDialog?.dbKey;
601
+ const dialogKey = explicitDialogKey || activeDialogKey;
602
+
603
+ if (!dialogKey) {
604
+ return rejectWithValue("当前对话不存在,无法发送消息。");
605
+ }
606
+ runtimeDialogKey = dialogKey;
607
+ const dialogId = extractCustomId(dialogKey);
608
+
609
+ const userInputText = extractAgentRunUserText(userInput);
610
+
611
+ const explicitServerBase =
612
+ typeof args.serverBase === "string" && args.serverBase.trim()
613
+ ? args.serverBase.trim()
614
+ : null;
615
+ const declaredRuntimeServerBase = extractAgentRuntimeServerBase(agentConfig);
616
+ const requestedServerBase = explicitServerBase ?? declaredRuntimeServerBase;
617
+ const normalizedRequestedServerBase =
618
+ requestedServerBase && normalizeServerOrigin(requestedServerBase);
619
+ const normalizedCurrentServer = normalizeServerOrigin(
620
+ selectCurrentServer(state),
621
+ );
622
+ const canAutoRouteRemotely =
623
+ !Array.isArray(userInput) &&
624
+ !runtimeOptions?.extraTools?.length &&
625
+ !runtimeOptions?.editingTarget &&
626
+ !runtimeOptions?.imageConfigOverride;
627
+ if (requestedServerBase && canAutoRouteRemotely) {
628
+ if (
629
+ normalizedRequestedServerBase &&
630
+ normalizedCurrentServer &&
631
+ normalizedRequestedServerBase === normalizedCurrentServer
632
+ ) {
633
+ // same server as current workspace; keep local flow
634
+ } else {
635
+ const token = selectCurrentToken(state);
636
+ const authHeader = token ? `Bearer ${token}` : "";
637
+ const rawMessages = filterMessagesForParallelBranch(
638
+ selectAllMsgs(state, dialogId),
639
+ runtimeOptions,
640
+ );
641
+ const visibleMessages = buildAgentViewMessages(
642
+ rawMessages as any,
643
+ agentConfig.dbKey,
644
+ );
645
+ const cleanedMessages = filterAndCleanMessages(visibleMessages);
646
+ const { key: msgKey, messageId } = createDialogMessageKeyAndId(dialogId);
647
+ remoteTransientMessageId = messageId;
648
+ const remoteMessageMetadata = buildParallelMessageMetadata(
649
+ agentConfig,
650
+ runtimeOptions,
651
+ );
652
+ let accumulated = "";
653
+ let totalTurnUsage: any = undefined;
654
+ const buildRemoteAssistantMessage = () => ({
655
+ id: messageId,
656
+ dbKey: msgKey,
657
+ role: "assistant" as const,
658
+ content: accumulated,
659
+ ...remoteMessageMetadata,
660
+ userId: selectUserId(getState() as RootState),
661
+ });
662
+
663
+ loopKey = `loop:${dialogId}`;
664
+ dispatch(addActiveController({ messageId: loopKey, controller: loopController, dialogKey }));
665
+ dispatch(messageStreaming({
666
+ id: messageId,
667
+ dialogId,
668
+ dbKey: msgKey,
669
+ content: "",
670
+ role: "assistant",
671
+ ...remoteMessageMetadata,
672
+ }));
673
+
674
+ const rejectRemoteStream = async (message: string) => {
675
+ if (accumulated.length > 0) {
676
+ await persistMessageWithFixedId(dispatch, buildRemoteAssistantMessage());
677
+ } else {
678
+ dispatch(removeTransientMessage(messageId));
679
+ }
680
+ remoteTransientMessageFinalized = true;
681
+ return rejectWithValue(message);
682
+ };
683
+
684
+ const remoteResponse = await fetch(`${requestedServerBase.replace(/\/+$/, "")}/api/agent/run`, {
685
+ method: "POST",
686
+ headers: {
687
+ "Content-Type": "application/json",
688
+ Accept: "text/event-stream",
689
+ ...(authHeader ? { Authorization: authHeader } : {}),
690
+ },
691
+ body: JSON.stringify({
692
+ agentKey,
693
+ userInput: agentRunUserInput,
694
+ messages: cleanedMessages,
695
+ stream: true,
696
+ runtimeContext: {
697
+ surface: "web",
698
+ host: "browser",
699
+ runtime: "react",
700
+ entrypoint: "chat-dialog",
701
+ capabilities: ["streaming", "dialog-ui", "tool-cards"],
702
+ },
703
+ ...(currentDialog?.spaceId ? { spaceId: currentDialog.spaceId } : {}),
704
+ }),
705
+ signal: loopController.signal,
706
+ });
707
+
708
+ if (!remoteResponse.ok) {
709
+ const errorText = await remoteResponse.text();
710
+ return await rejectRemoteStream(
711
+ errorText || `Remote agent run failed (${remoteResponse.status})`,
712
+ );
713
+ }
714
+
715
+ const reader = remoteResponse.body?.getReader();
716
+ if (!reader) {
717
+ return await rejectRemoteStream("无法读取远端流式响应");
718
+ }
719
+
720
+ const decoder = new TextDecoder();
721
+ const parseSSE = createSSEParser();
722
+ const abortRemoteStream = async () => {
723
+ if (accumulated.length <= 0) return;
724
+ await persistMessageWithFixedId(dispatch, buildRemoteAssistantMessage());
725
+ };
726
+
727
+ try {
728
+ while (true) {
729
+ const { done, value } = await reader.read();
730
+ if (done) break;
731
+ if (loopController.signal.aborted || thunkApi.signal.aborted) {
732
+ await abortRemoteStream();
733
+ return;
734
+ }
735
+
736
+ const payloads = parseSSE(
737
+ decoder.decode(value, { stream: true }),
738
+ );
739
+ for (const payload of payloads) {
740
+ if (payload.type === "error") {
741
+ return await rejectRemoteStream(
742
+ payload.message || "远端 Agent 执行失败",
743
+ );
744
+ }
745
+ if (payload.type === "text" && typeof payload.content === "string") {
746
+ accumulated += payload.content;
747
+ dispatch(messageStreaming({
748
+ id: messageId,
749
+ dialogId,
750
+ dbKey: msgKey,
751
+ content: accumulated,
752
+ role: "assistant",
753
+ ...remoteMessageMetadata,
754
+ }));
755
+ }
756
+ if (payload.type === "done") {
757
+ totalTurnUsage = payload.usage;
758
+ }
759
+ }
760
+ }
761
+ } finally {
762
+ try {
763
+ await reader.cancel();
764
+ } catch {
765
+ // ignore
766
+ }
767
+ }
768
+
769
+ await persistMessageWithFixedId(dispatch, buildRemoteAssistantMessage());
770
+ remoteTransientMessageFinalized = true;
771
+ return {
772
+ usage: totalTurnUsage ?? undefined,
773
+ };
774
+ }
775
+ }
776
+
777
+ // Extract Mentions from userInput if it's potentially Slate content
778
+ let extractedMentions: CategorizedMentions | undefined;
779
+ if (Array.isArray(userInput)) {
780
+ // Basic check if it looks like Slate nodes (has children) or just assume safe to traverse
781
+ // extractCategorizedMentions handles traversal safely.
782
+ extractedMentions = extractCategorizedMentions(userInput as any);
783
+ }
784
+
785
+ const mentionedTools = extractedMentions?.tools ?? [];
786
+
787
+ // 2. 解析引用:包含 tools 的页面自动升级为 instruction
788
+ const {
789
+ references: normalizedReferences,
790
+ contentByKey: referenceContentCache,
791
+ referencedTools: referenceTools,
792
+ recommendedSkillTools: referenceRecommendedSkillTools,
793
+ recommendedSkillHints: referenceRecommendedSkillHints,
794
+ skillPromptPatches: referenceSkillPromptPatches,
795
+ } = await resolveReferenceAssets(agentConfig.references, dispatch);
796
+
797
+ const agentConfigWithReferences = {
798
+ ...agentConfig,
799
+ references: normalizedReferences,
800
+ referencedTools: referenceTools,
801
+ recommendedSkillTools: referenceRecommendedSkillTools,
802
+ recommendedSkillHints: referenceRecommendedSkillHints,
803
+ skillPromptPatches: referenceSkillPromptPatches,
804
+ };
805
+
806
+ // --- [新增] 提取本次 Handler 启动前的稳定历史消息 ID 集合 ---
807
+ const initialRawMsgs = selectAllMsgs(state, dialogId);
808
+ const initialHistoryIds = new Set(initialRawMsgs.map((m: any) => m.id));
809
+
810
+ const keySets = await getFullChatContextKeys(
811
+ state,
812
+ dispatch,
813
+ agentConfigWithReferences,
814
+ userInput,
815
+ currentDialog ?? undefined,
816
+ );
817
+ const finalKeys = deduplicateContextKeys(keySets);
818
+ const allContextKeys = new Set<string>([
819
+ ...finalKeys.botInstructionsContext,
820
+ ...finalKeys.currentInputContext,
821
+ ...finalKeys.historyContext,
822
+ ...finalKeys.botKnowledgeContext,
823
+ ]);
824
+
825
+ // 4. 上下文页面里提取 tools 并缓存内容
826
+ const {
827
+ tools: contextTools,
828
+ contentByKey: contextContentCache,
829
+ recommendedSkillTools: contextRecommendedSkillTools = [],
830
+ recommendedSkillHints: contextRecommendedSkillHints = [],
831
+ skillPromptPatches: contextSkillPromptPatches = [],
832
+ } = await resolveToolsFromKeys(
833
+ Array.from(allContextKeys),
834
+ dispatch,
835
+ referenceContentCache,
836
+ );
837
+
838
+ const mergedContentCache = new Map<string, any>([
839
+ ...referenceContentCache,
840
+ ...contextContentCache,
841
+ ]);
842
+
843
+ // 4. 合并工具 (Base + Default + Context + Mentioned + Runtime) + 图片配置
844
+ const agentConfigWithTools = mergeAgentToolsWithRuntime(
845
+ {
846
+ ...agentConfigWithReferences,
847
+ recommendedSkillTools: [
848
+ ...(((agentConfigWithReferences as any).recommendedSkillTools ?? []) as string[]),
849
+ ...contextRecommendedSkillTools,
850
+ ],
851
+ recommendedSkillHints: [
852
+ ...(((agentConfigWithReferences as any).recommendedSkillHints ?? []) as string[]),
853
+ ...contextRecommendedSkillHints,
854
+ ],
855
+ skillPromptPatches: [
856
+ ...(((agentConfigWithReferences as any).skillPromptPatches ?? []) as string[]),
857
+ ...contextSkillPromptPatches,
858
+ ],
859
+ },
860
+ contextTools,
861
+ mentionedTools,
862
+ runtimeOptions,
863
+ state,
864
+ );
865
+ const agentConfigForCall = applyImageConfigRuntimeOverride(
866
+ agentConfigWithTools,
867
+ runtimeOptions,
868
+ );
869
+
870
+ // 如果对话设置了 maxTokens,覆盖 agent 的 max_tokens
871
+ const dialogMaxTokens = currentDialog?.maxTokens;
872
+ const effectiveAgentConfig = dialogMaxTokens
873
+ ? { ...agentConfigForCall, max_tokens: dialogMaxTokens }
874
+ : agentConfigForCall;
875
+
876
+ const isRespModel = isResponseAPIModel(agentConfigForCall);
877
+
878
+ // 🔹 Response-style 模型:与 completions 一样走完整 Agent Loop
879
+ if (isRespModel) {
880
+ const maxExecutionTime = selectMaxExecutionTime(state);
881
+ const MAX_TIME_MS = maxExecutionTime > 0 ? maxExecutionTime : 240_000;
882
+ const startTime = Date.now();
883
+
884
+ const staticContexts = await buildStaticContexts(
885
+ state,
886
+ dispatch,
887
+ agentConfigForCall,
888
+ currentDialog ?? undefined,
889
+ mergedContentCache,
890
+ );
891
+
892
+ let appendTempUserInput = true;
893
+ let currentParentMessageId = parentMessageId ?? undefined;
894
+
895
+ const w = typeof globalThis !== "undefined" && (globalThis as any).window ? (globalThis as any).window : null;
896
+ if (w) w.__LOOP_STOP_REASON__ = null;
897
+
898
+ loopKey = `loop:${dialogId}`;
899
+ dispatch(addActiveController({ messageId: loopKey, controller: loopController, dialogKey }));
900
+
901
+ for (;;) {
902
+ const requestParentMessageId = currentParentMessageId;
903
+ if (loopController.signal.aborted || thunkApi.signal.aborted) {
904
+ if (w) w.__LOOP_STOP_REASON__ = "aborted";
905
+ break;
906
+ }
907
+
908
+ const loopState = getState() as RootState;
909
+ const now = Date.now();
910
+ if (now - startTime > MAX_TIME_MS) {
911
+ if (w) w.__LOOP_STOP_REASON__ = "timeout";
912
+ break;
913
+ }
914
+
915
+ const accessError = validateAccessAndBalance(
916
+ agentConfigForCall,
917
+ loopState,
918
+ );
919
+ if (accessError) {
920
+ return rejectWithValue(accessError);
921
+ }
922
+
923
+ const dynamicContexts = await buildDynamicContexts(
924
+ loopState,
925
+ dispatch,
926
+ agentConfigForCall,
927
+ userInput,
928
+ runtimeOptions,
929
+ mergedContentCache,
930
+ dialogKey,
931
+ );
932
+ const contexts = mergeContexts(staticContexts, dynamicContexts);
933
+
934
+ const rawMessages = filterMessagesForParallelBranch(
935
+ selectAllMsgs(loopState, dialogId),
936
+ runtimeOptions,
937
+ );
938
+ let visibleMessages = buildAgentViewMessages(
939
+ rawMessages as any,
940
+ agentConfigForCall.dbKey,
941
+ );
942
+
943
+ if (appendTempUserInput && hasAgentRunUserInputContent(agentRunUserInput)) {
944
+ visibleMessages = [
945
+ ...visibleMessages,
946
+ {
947
+ id: `__tmp_user_${Date.now()}`,
948
+ dbKey: "",
949
+ role: "user",
950
+ content: agentRunUserInput,
951
+ thinkContent: "",
952
+ cybotKey: agentConfigForCall.dbKey,
953
+ isStreaming: false,
954
+ } as any,
955
+ ];
956
+ }
957
+
958
+ const cleanedMessages = filterAndCleanMessages(visibleMessages);
959
+ const ctxWindow =
960
+ getModelContextWindow(agentConfigForCall.model) || 128000;
961
+ const retention = selectContextRetention(loopState);
962
+ const summaryTokenCount = contexts.dialogSummary
963
+ ? estimateTokenCount(contexts.dialogSummary)
964
+ : 0;
965
+ const processedMessages = trimMessagesWithSummary(
966
+ compressOldToolResults(cleanedMessages),
967
+ ctxWindow,
968
+ summaryTokenCount,
969
+ retention,
970
+ );
971
+
972
+ let firstDynamicIdx = processedMessages.findIndex(
973
+ (m) => m.id && !initialHistoryIds.has(m.id),
974
+ );
975
+ if (firstDynamicIdx === -1) firstDynamicIdx = processedMessages.length;
976
+
977
+ const stableMessages = processedMessages.slice(0, firstDynamicIdx);
978
+ const dynamicMessages = processedMessages.slice(firstDynamicIdx);
979
+
980
+ if (appendTempUserInput) {
981
+ const agentHasVision = resolveAgentImageInputSupport(
982
+ agentConfigForCall as any,
983
+ );
984
+
985
+ if (!agentHasVision && hasImageInMessages(processedMessages)) {
986
+ return rejectWithValue(
987
+ "当前 Agent 不支持图片输入,请改用文本或文档。",
988
+ );
989
+ }
990
+ }
991
+
992
+ const bodyData = generateRequestBody({
993
+ agentConfig: effectiveAgentConfig,
994
+ messages: dynamicMessages,
995
+ stableMessages,
996
+ userInput: userInputText,
997
+ contexts,
998
+ });
999
+
1000
+ const meta: CompletionMeta = await sendOpenAIResponseRequest({
1001
+ bodyData,
1002
+ agentConfig: agentConfigForCall,
1003
+ thunkApi,
1004
+ dialogKey,
1005
+ parentMessageId: currentParentMessageId,
1006
+ messageMetadata: buildParallelMessageMetadata(
1007
+ agentConfigForCall,
1008
+ runtimeOptions,
1009
+ ),
1010
+ });
1011
+
1012
+ appendTempUserInput = false;
1013
+ currentParentMessageId = undefined;
1014
+ totalTurnUsage = updateTotalUsage(totalTurnUsage, meta.usage);
1015
+
1016
+ if (meta.hasHandedOff) {
1017
+ if (!requestParentMessageId && meta.messageId) {
1018
+ dispatch(removeTransientMessage(meta.messageId));
1019
+ }
1020
+ if (w) w.__LOOP_STOP_REASON__ = "handoff";
1021
+ break;
1022
+ }
1023
+
1024
+ if (meta.hasPendingInteraction) {
1025
+ if (w) w.__LOOP_STOP_REASON__ = "pending";
1026
+ break;
1027
+ }
1028
+
1029
+ const afterTurnState = getState() as RootState;
1030
+ const queuedMessages = selectPendingUserInputQueue(afterTurnState, dialogKey);
1031
+ if (queuedMessages.length > 0) {
1032
+ const queuedText = queuedMessages[0];
1033
+
1034
+ const currentDialogConfig =
1035
+ selectDialogConfigByKey(afterTurnState, dialogKey) ??
1036
+ selectCurrentDialogConfig(afterTurnState);
1037
+ if (!currentDialogConfig) {
1038
+ dispatch(clearPendingUserInputQueue({ dialogKey }));
1039
+ break;
1040
+ }
1041
+ await dispatch(
1042
+ prepareAndPersistUserMessage({
1043
+ userInput: queuedText,
1044
+ dialogConfig: currentDialogConfig,
1045
+ })
1046
+ ).unwrap();
1047
+ dispatch(dequeueUserInput({ dialogKey }));
1048
+ continue;
1049
+ }
1050
+
1051
+ if (!meta.hasToolCalls) {
1052
+ if (w) w.__LOOP_STOP_REASON__ = "done";
1053
+ break;
1054
+ }
1055
+ }
1056
+
1057
+ return {
1058
+ usage: totalTurnUsage ?? undefined,
1059
+ };
1060
+ }
1061
+
1062
+ // 🔹 Completions-style 模型:Agent Loop
1063
+ const maxExecutionTime = selectMaxExecutionTime(state);
1064
+
1065
+ const MAX_TIME_MS = maxExecutionTime > 0 ? maxExecutionTime : 240_000;
1066
+ const startTime = Date.now();
1067
+
1068
+ // 🚀 优化:在 Loop 外构建静态上下文(只执行一次)
1069
+ // 静态上下文包含:botInstructions、botKnowledge、spaceContext、userGlobalPrompt
1070
+ // 这些内容在 Loop 期间是稳定的,不需要每轮重新构建
1071
+ const staticContexts = await buildStaticContexts(
1072
+ state,
1073
+ dispatch,
1074
+ agentConfigForCall,
1075
+ currentDialog ?? undefined,
1076
+ mergedContentCache,
1077
+ );
1078
+
1079
+ let appendTempUserInput = true;
1080
+ let currentParentMessageId = parentMessageId ?? undefined;
1081
+
1082
+ const w = typeof globalThis !== "undefined" && (globalThis as any).window ? (globalThis as any).window : null;
1083
+ if (w) w.__LOOP_STOP_REASON__ = null;
1084
+
1085
+ if (!isRespModel) {
1086
+ loopKey = `loop:${dialogId}`;
1087
+ dispatch(addActiveController({ messageId: loopKey, controller: loopController, dialogKey }));
1088
+ }
1089
+
1090
+ for (;;) {
1091
+ const requestParentMessageId = currentParentMessageId;
1092
+ // 每轮开始前检查是否已中止
1093
+ if (loopController.signal.aborted || thunkApi.signal.aborted) {
1094
+ if (w) w.__LOOP_STOP_REASON__ = "aborted";
1095
+ break;
1096
+ }
1097
+
1098
+ const loopState = getState() as RootState;
1099
+ const now = Date.now();
1100
+ if (now - startTime > MAX_TIME_MS) {
1101
+ if (w) w.__LOOP_STOP_REASON__ = "timeout";
1102
+ break;
1103
+ }
1104
+
1105
+ // 每轮检查权限 & 余额
1106
+ const accessError = validateAccessAndBalance(
1107
+ agentConfigForCall,
1108
+ loopState,
1109
+ );
1110
+ if (accessError) {
1111
+ return rejectWithValue(accessError);
1112
+ }
1113
+
1114
+ // 🚀 优化:每轮只构建动态上下文(currentInput、history、editingContext、dialogSummary)
1115
+ const dynamicContexts = await buildDynamicContexts(
1116
+ loopState,
1117
+ dispatch,
1118
+ agentConfigForCall,
1119
+ userInput,
1120
+ runtimeOptions,
1121
+ mergedContentCache,
1122
+ dialogKey,
1123
+ );
1124
+
1125
+ // 合并静态和动态上下文
1126
+ const contexts = mergeContexts(staticContexts, dynamicContexts);
1127
+
1128
+ const rawMessages = filterMessagesForParallelBranch(
1129
+ selectAllMsgs(loopState, dialogId),
1130
+ runtimeOptions,
1131
+ );
1132
+ let visibleMessages = buildAgentViewMessages(
1133
+ rawMessages as any,
1134
+ agentConfigForCall.dbKey,
1135
+ );
1136
+
1137
+ if (appendTempUserInput && hasAgentRunUserInputContent(agentRunUserInput)) {
1138
+ visibleMessages = [
1139
+ ...visibleMessages,
1140
+ {
1141
+ id: `__tmp_user_${Date.now()}`,
1142
+ dbKey: "",
1143
+ role: "user",
1144
+ content: agentRunUserInput,
1145
+ thinkContent: "",
1146
+ cybotKey: agentConfigForCall.dbKey,
1147
+ isStreaming: false,
1148
+ } as any,
1149
+ ];
1150
+ }
1151
+
1152
+ const cleanedMessages = filterAndCleanMessages(visibleMessages);
1153
+ const ctxWindow =
1154
+ getModelContextWindow(agentConfigForCall.model) || 128000;
1155
+ const retention = selectContextRetention(loopState);
1156
+ const summaryTokenCount = contexts.dialogSummary
1157
+ ? estimateTokenCount(contexts.dialogSummary)
1158
+ : 0;
1159
+ const processedMessages = trimMessagesWithSummary(
1160
+ compressOldToolResults(cleanedMessages),
1161
+ ctxWindow,
1162
+ summaryTokenCount,
1163
+ retention,
1164
+ );
1165
+
1166
+ // --- [优化 P1] 使用 findIndex + slice 确保顺序和无 ID 稳定消息的保留 ---
1167
+ let firstDynamicIdx = processedMessages.findIndex(
1168
+ (m) => m.id && !initialHistoryIds.has(m.id),
1169
+ );
1170
+ if (firstDynamicIdx === -1) firstDynamicIdx = processedMessages.length;
1171
+
1172
+ const stableMessages = processedMessages.slice(0, firstDynamicIdx);
1173
+ const dynamicMessages = processedMessages.slice(firstDynamicIdx);
1174
+
1175
+ if (appendTempUserInput) {
1176
+ const agentHasVision = resolveAgentImageInputSupport(
1177
+ agentConfigForCall as any,
1178
+ );
1179
+
1180
+ if (!agentHasVision && hasImageInMessages(processedMessages)) {
1181
+ return rejectWithValue(
1182
+ "当前 Agent 不支持图片输入,请改用文本或文档。",
1183
+ );
1184
+ }
1185
+ }
1186
+
1187
+ const bodyData = generateRequestBody({
1188
+ agentConfig: effectiveAgentConfig,
1189
+ messages: dynamicMessages,
1190
+ stableMessages,
1191
+ userInput: userInputText,
1192
+ contexts,
1193
+ });
1194
+
1195
+ const meta: CompletionMeta = await sendOpenAICompletionsRequest({
1196
+ bodyData,
1197
+ agentConfig: agentConfigForCall,
1198
+ thunkApi,
1199
+ dialogKey,
1200
+ parentMessageId: currentParentMessageId,
1201
+ messageMetadata: buildParallelMessageMetadata(
1202
+ agentConfigForCall,
1203
+ runtimeOptions,
1204
+ ),
1205
+ });
1206
+
1207
+ appendTempUserInput = false;
1208
+ currentParentMessageId = undefined;
1209
+ totalTurnUsage = updateTotalUsage(totalTurnUsage, meta.usage);
1210
+
1211
+ // handoff(例如 runStreamingAgent):当前 Agent 停止,后续由子 Agent 自动续跑
1212
+ if (meta.hasHandedOff) {
1213
+ if (!requestParentMessageId && meta.messageId) {
1214
+ dispatch(removeTransientMessage(meta.messageId));
1215
+ }
1216
+ if (w) w.__LOOP_STOP_REASON__ = "handoff";
1217
+ break;
1218
+ }
1219
+
1220
+ if (meta.hasPendingInteraction) {
1221
+ if (w) w.__LOOP_STOP_REASON__ = "pending";
1222
+ break;
1223
+ }
1224
+
1225
+ // 检查是否有用户在 loop 期间发送的排队消息
1226
+ const afterTurnState = getState() as RootState;
1227
+ const queuedMessages = selectPendingUserInputQueue(afterTurnState, dialogKey);
1228
+ if (queuedMessages.length > 0) {
1229
+ const queuedText = queuedMessages[0];
1230
+
1231
+ const currentDialogConfig =
1232
+ selectDialogConfigByKey(afterTurnState, dialogKey) ??
1233
+ selectCurrentDialogConfig(afterTurnState);
1234
+ if (!currentDialogConfig) {
1235
+ // 对话已切换/销毁,无法持久化;清空队列并终止 loop,避免死循环重试
1236
+ dispatch(clearPendingUserInputQueue({ dialogKey }));
1237
+ break;
1238
+ }
1239
+ await dispatch(
1240
+ prepareAndPersistUserMessage({
1241
+ userInput: queuedText,
1242
+ dialogConfig: currentDialogConfig,
1243
+ })
1244
+ ).unwrap();
1245
+ // 持久化成功后再出队,避免 persist 失败时丢消息
1246
+ dispatch(dequeueUserInput({ dialogKey }));
1247
+ // 用户消息已持久化到 DB,下一轮 selectAllMsgs 会自动包含它
1248
+ // 不设置 appendTempUserInput,直接继续下一轮
1249
+ continue;
1250
+ }
1251
+
1252
+ if (!meta.hasToolCalls) {
1253
+ if (w) w.__LOOP_STOP_REASON__ = "done";
1254
+ break;
1255
+ }
1256
+
1257
+ // 否则:存在 tool_calls 且没有 handoff / pending,基于新的 history 继续下一轮
1258
+ }
1259
+
1260
+ return {
1261
+ usage: totalTurnUsage ?? undefined,
1262
+ };
1263
+ } catch (error: any) {
1264
+ if (isAbortError(error)) {
1265
+ if (remoteTransientMessageId && !remoteTransientMessageFinalized) {
1266
+ dispatch(removeTransientMessage(remoteTransientMessageId));
1267
+ remoteTransientMessageFinalized = true;
1268
+ }
1269
+ const w =
1270
+ typeof globalThis !== "undefined" && (globalThis as any).window
1271
+ ? (globalThis as any).window
1272
+ : null;
1273
+ if (w) w.__LOOP_STOP_REASON__ = "aborted";
1274
+ return;
1275
+ }
1276
+ console.error(
1277
+ `Error in streamAgentChatTurn for [${agentKey}]:`,
1278
+ error,
1279
+ );
1280
+ if (remoteTransientMessageId && !remoteTransientMessageFinalized) {
1281
+ dispatch(removeTransientMessage(remoteTransientMessageId));
1282
+ remoteTransientMessageFinalized = true;
1283
+ }
1284
+
1285
+ return rejectWithValue(
1286
+ error?.message ||
1287
+ "An unexpected error occurred in streamAgentChatTurn.",
1288
+ );
1289
+ } finally {
1290
+ if (loopKey && runtimeDialogKey) {
1291
+ dispatch(removeActiveController({ messageId: loopKey, dialogKey: runtimeDialogKey }));
1292
+ } else if (loopKey) {
1293
+ dispatch(removeActiveController(loopKey));
1294
+ }
1295
+ // 清空排队的用户消息(loop 结束或异常时,未消费的消息不应继续保留)
1296
+ dispatch(clearPendingUserInputQueue(runtimeDialogKey ? { dialogKey: runtimeDialogKey } : undefined));
1297
+ thunkApi.signal.removeEventListener("abort", onAbort);
1298
+ }
1299
+ };