nolo-cli 0.1.9 → 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 -8
  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 -1075
  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,238 +0,0 @@
1
- // packages/ai/agent/runAgentBackground.ts
2
- // 后台运行 agent 并通过 SSE 监听结果
3
- //
4
- // 使用方式(在组件/thunk 中):
5
- // dispatch(runAgentBackground({
6
- // agentKey: "agent-xxx",
7
- // userInput: "帮我搜索...",
8
- // spaceId: "space-yyy", // 可选,写入 space 索引
9
- // onStatusChange: (s) => ..., // 可选,实时更新 UI 状态
10
- // onDone: (result) => ..., // 可选,成功回调
11
- // onFailed: (error) => ..., // 可选,失败回调
12
- // }));
13
-
14
- import { createAsyncThunk } from "@reduxjs/toolkit";
15
- import type { RootState } from "app/store";
16
- import { selectCurrentServer } from "app/settings/settingSlice";
17
- import { selectCurrentToken } from "auth/authSlice";
18
- import { createSSEParser } from "../chat/parseMultilineSSE";
19
-
20
- export type DialogStatus = "pending" | "running" | "done" | "failed" | "cancelled" | "reconnecting";
21
-
22
- export interface RunAgentBackgroundResult {
23
- dialogId: string;
24
- content?: string;
25
- usage?: unknown;
26
- }
27
-
28
- export interface RunAgentBackgroundArgs {
29
- agentKey: string;
30
- userInput: string;
31
- serverBase?: string;
32
- spaceId?: string;
33
- /** 任务状态变化时触发(pending → running → done/failed) */
34
- onStatusChange?: (status: DialogStatus) => void;
35
- /** agent 成功完成时触发 */
36
- onDone?: (result: { dialogId: string; content?: string; usage?: unknown }) => void;
37
- /** agent 失败时触发 */
38
- onFailed?: (error: string) => void;
39
- /** 外部取消信号 */
40
- signal?: AbortSignal;
41
- }
42
-
43
- const MAX_SSE_RETRIES = 3;
44
- const SSE_RETRY_DELAY_MS = 1500;
45
-
46
- type RetryableError = Error & { retryable?: boolean };
47
-
48
- function createRetryableError(message: string): RetryableError {
49
- return Object.assign(new Error(message), { retryable: true });
50
- }
51
-
52
- function createSubscriptionError(status: number, hasBody: boolean): RetryableError {
53
- const message = !hasBody
54
- ? `事件流响应缺少 body (${status})`
55
- : `无法订阅事件流 (${status})`;
56
-
57
- if (status === 401 || status === 403) {
58
- return new Error(message);
59
- }
60
-
61
- return createRetryableError(message);
62
- }
63
-
64
- /**
65
- * 订阅单次 dialog 事件流,收到 done/failed 则 resolve/reject,
66
- * 流意外关闭时 reject 并标记 retryable=true 供上层重试。
67
- */
68
- async function listenToDialogEvents(
69
- dialogId: string,
70
- currentServer: string,
71
- authHeader: string,
72
- signal: AbortSignal,
73
- onStatusChange?: (status: DialogStatus) => void,
74
- onDone?: (result: { dialogId: string; content?: string; usage?: unknown }) => void,
75
- onFailed?: (error: string) => void,
76
- ): Promise<RunAgentBackgroundResult> {
77
- let eventsRes: Response;
78
- try {
79
- eventsRes = await fetch(
80
- `${currentServer}/api/events/dialog-${dialogId}`,
81
- {
82
- method: "GET",
83
- headers: {
84
- Accept: "text/event-stream",
85
- ...(authHeader && { Authorization: authHeader }),
86
- },
87
- signal,
88
- }
89
- );
90
- } catch (e) {
91
- if (e instanceof Error && e.name === "AbortError") throw e;
92
- throw createRetryableError("事件流连接失败");
93
- }
94
-
95
- if (!eventsRes.ok || !eventsRes.body) {
96
- throw createSubscriptionError(eventsRes.status, !!eventsRes.body);
97
- }
98
-
99
- const reader = eventsRes.body.getReader();
100
- const decoder = new TextDecoder();
101
- const parseSSE = createSSEParser();
102
-
103
- return new Promise<RunAgentBackgroundResult>((resolve, reject) => {
104
- const processStream = async () => {
105
- try {
106
- while (true) {
107
- const { done, value } = await reader.read();
108
- if (done) break;
109
-
110
- const chunk = decoder.decode(value, { stream: true });
111
- const events = parseSSE(chunk);
112
-
113
- for (const event of events) {
114
- const type = event.type as string;
115
-
116
- if (type === "status") {
117
- onStatusChange?.(event.status as DialogStatus);
118
- } else if (type === "done") {
119
- const result = {
120
- dialogId,
121
- content: event.content as string | undefined,
122
- usage: event.usage,
123
- };
124
- onStatusChange?.("done");
125
- onDone?.(result);
126
- reader.cancel().catch(() => { });
127
- resolve(result);
128
- return;
129
- } else if (type === "failed") {
130
- const errMsg = (event.error as string) ?? "未知错误";
131
- onStatusChange?.("failed");
132
- onFailed?.(errMsg);
133
- reader.cancel().catch(() => { });
134
- reject(new Error(errMsg));
135
- return;
136
- }
137
- }
138
- }
139
- // 流正常关闭但未收到 done/failed(服务重启等),标记可重试
140
- const err = createRetryableError("事件流意外关闭");
141
- reject(err);
142
- } catch (e: unknown) {
143
- if (e instanceof Error && e.name === "AbortError") {
144
- resolve({ dialogId });
145
- } else {
146
- reject(e);
147
- }
148
- }
149
- };
150
-
151
- processStream();
152
- });
153
- }
154
-
155
- export const runAgentBackground = createAsyncThunk<
156
- RunAgentBackgroundResult,
157
- RunAgentBackgroundArgs,
158
- { state: RootState }
159
- >("agent/runBackground", async (args, { getState, signal: thunkSignal }) => {
160
- const { agentKey, userInput, serverBase, spaceId, onStatusChange, onDone, onFailed } = args;
161
-
162
- const state = getState();
163
- const currentServer = serverBase?.trim().replace(/\/+$/, "") || selectCurrentServer(state);
164
- const token = selectCurrentToken(state);
165
- if (!currentServer) throw new Error("未配置服务器地址");
166
-
167
- const authHeader = token ? `Bearer ${token}` : "";
168
-
169
- // ── Step 1: 触发后台运行,获取 dialogId ──────────────────────────────────
170
- const runRes = await fetch(`${currentServer}/api/agent/run`, {
171
- method: "POST",
172
- headers: {
173
- "Content-Type": "application/json",
174
- ...(authHeader && { Authorization: authHeader }),
175
- },
176
- body: JSON.stringify({
177
- agentKey,
178
- userInput,
179
- spaceId,
180
- background: true,
181
- runtimeContext: {
182
- surface: "web",
183
- host: "browser",
184
- runtime: "react",
185
- entrypoint: "background-agent-run",
186
- capabilities: ["background", "sse-events"],
187
- },
188
- }),
189
- signal: args.signal ?? thunkSignal,
190
- });
191
-
192
- if (!runRes.ok) {
193
- const err = await runRes.text();
194
- throw new Error(`启动后台任务失败 (${runRes.status}): ${err}`);
195
- }
196
-
197
- const {
198
- dialogId,
199
- serverBase: routedServerBase,
200
- } = (await runRes.json()) as {
201
- dialogId: string;
202
- status: string;
203
- serverBase?: string;
204
- };
205
- onStatusChange?.("pending");
206
-
207
- // ── Step 2: 订阅 SSE 事件流(含断线重连,最多 3 次)────────────────────────
208
- const effectiveSignal = args.signal ?? thunkSignal;
209
- let lastError: Error | undefined;
210
-
211
- const eventServer =
212
- typeof routedServerBase === "string" && routedServerBase.trim()
213
- ? routedServerBase.trim().replace(/\/+$/, "")
214
- : currentServer;
215
-
216
- for (let attempt = 0; attempt <= MAX_SSE_RETRIES; attempt++) {
217
- if (attempt > 0) {
218
- if (effectiveSignal.aborted) throw new DOMException("Aborted", "AbortError");
219
- onStatusChange?.("reconnecting");
220
- await new Promise(r => setTimeout(r, SSE_RETRY_DELAY_MS));
221
- if (effectiveSignal.aborted) throw new DOMException("Aborted", "AbortError");
222
- }
223
-
224
- try {
225
- return await listenToDialogEvents(
226
- dialogId, eventServer, authHeader, effectiveSignal,
227
- onStatusChange, onDone, onFailed,
228
- );
229
- } catch (e: any) {
230
- if (e?.name === "AbortError") throw e; // 用户主动取消,不重试
231
- if (!e?.retryable || attempt >= MAX_SSE_RETRIES) throw e; // 非可重试错误或超限
232
- lastError = e;
233
- // retryable(流意外关闭)→ 继续重连
234
- }
235
- }
236
-
237
- throw lastError ?? new Error("事件流重连失败");
238
- });
@@ -1,138 +0,0 @@
1
- // packages/ai/agent/runAgentClientLoop.ts
2
- //
3
- // 客户端多轮 Agent 循环:复现服务端 runAgentLoop 的逻辑
4
- // - 不支持 Response API(当前全部走 OpenAI Completions 格式)
5
- // - 工具执行通过 executeToolCall 在客户端完成
6
- // - 每轮:调 LLM → 有 tool_calls → 执行工具 → 追加 tool 消息 → 继续
7
-
8
- import { RootState } from "app/store";
9
- import { Message } from "app/types";
10
- import { read } from "database/dbSlice";
11
- import { fetchAgentContexts } from "ai/agent/fetchAgentContexts";
12
- import { generateRequestBody } from "ai/llm/generateRequestBody";
13
- import { getApiEndpoint } from "ai/llm/providers";
14
- import { selectCurrentServer } from "app/settings/settingSlice";
15
- import { selectCurrentToken } from "auth/authSlice";
16
- import { performFetchRequest } from "ai/chat/fetchUtils";
17
- import { executeToolCall } from "ai/agent/executeToolCall";
18
- import { extractCustomId } from "core/prefix";
19
- import { updateTokensAction } from "chat/dialog/actions/updateTokensAction";
20
-
21
- export interface RunAgentClientLoopArgs {
22
- agentKey: string;
23
- content: any;
24
- parentMessageId?: string;
25
- billingDialogKey?: string;
26
- }
27
-
28
- export interface RunAgentClientLoopResult {
29
- content: string;
30
- toolCallCount: number;
31
- }
32
-
33
- /**
34
- * 客户端多轮 Agent 执行循环。
35
- *
36
- * 使用场景:callAgentTool 的 client 模式、runAgent thunk。
37
- * 不适合需要流式输出的场景(请用 runStreamingAgent)。
38
- */
39
- export async function runAgentClientLoop(
40
- args: RunAgentClientLoopArgs,
41
- thunkApi: any
42
- ): Promise<RunAgentClientLoopResult> {
43
- const { agentKey, content, parentMessageId, billingDialogKey } = args;
44
- const { getState, dispatch } = thunkApi;
45
- const state = getState() as RootState;
46
-
47
- // 1. 加载 Agent 配置
48
- const agentConfig = await dispatch(read({ dbKey: agentKey })).unwrap();
49
-
50
- // 2. 加载 Agent 上下文(知识库 / references)
51
- const agentContexts = await fetchAgentContexts(
52
- agentConfig.references,
53
- dispatch
54
- );
55
-
56
- // 3. 构造首轮 body(generateRequestBody 会注入 system prompt)
57
- const initialMessages: Message[] = [{ role: "user", content }];
58
- const body = generateRequestBody({
59
- agentConfig,
60
- messages: initialMessages,
61
- userInput: typeof content === "string" ? content : JSON.stringify(content),
62
- contexts: agentContexts,
63
- });
64
- body.stream = false; // 多轮模式不用流式
65
-
66
- // body.messages 包含了 system 消息 + 用户消息,后续累积 tool 结果入此数组
67
- const messages: Message[] = body.messages as Message[];
68
-
69
- const api = getApiEndpoint(agentConfig);
70
- const currentServer = selectCurrentServer(state);
71
- const token = selectCurrentToken(state);
72
-
73
- let finalContent = "";
74
- let toolCallCount = 0;
75
-
76
- // 4. 多轮循环
77
- for (;;) {
78
- // 更新 messages(含累积的 tool 结果)
79
- body.messages = messages;
80
-
81
- const response = await performFetchRequest({
82
- agentConfig,
83
- api,
84
- bodyData: body as any,
85
- currentServer,
86
- token,
87
- });
88
-
89
- const data = await response.json();
90
- const choice = data.choices?.[0];
91
-
92
- if (billingDialogKey && data?.usage) {
93
- await updateTokensAction(
94
- {
95
- dialogId: extractCustomId(billingDialogKey),
96
- dialogKey: billingDialogKey,
97
- usage: data.usage,
98
- agentConfig,
99
- },
100
- thunkApi
101
- );
102
- }
103
-
104
- if (!choice) {
105
- console.warn("[runAgentClientLoop] 响应中找不到 choices[0],停止");
106
- break;
107
- }
108
-
109
- const assistantMsg = choice.message;
110
- const finishReason: string = choice.finish_reason ?? "";
111
-
112
- // 追加 assistant 消息
113
- messages.push(assistantMsg);
114
-
115
- // 记录文本内容
116
- if (typeof assistantMsg.content === "string" && assistantMsg.content) {
117
- finalContent = assistantMsg.content;
118
- }
119
-
120
- // 无工具调用 or 模型明确 stop → 结束
121
- if (!assistantMsg.tool_calls?.length || finishReason === "stop") {
122
- break;
123
- }
124
-
125
- // 5. 执行工具调用
126
- for (const tc of assistantMsg.tool_calls) {
127
- toolCallCount++;
128
- const toolResultContent = await executeToolCall(tc, thunkApi, parentMessageId);
129
- messages.push({
130
- role: "tool",
131
- tool_call_id: tc.id,
132
- content: toolResultContent,
133
- } as any);
134
- }
135
- }
136
-
137
- return { content: finalContent, toolCallCount };
138
- }
@@ -1,97 +0,0 @@
1
- import { canonicalizeToolNames } from "ai/tools/toolNameAliases";
2
- import { buildContextLayerContractBlock } from "./contextLayerContract";
3
- import { buildStartupProtocolBlock } from "./startupProtocol";
4
-
5
- export type RuntimeGuidanceToolOptions = {
6
- hasCheckEnvTool: boolean;
7
- hasExecShellTool: boolean;
8
- hasRememberMemoryTool: boolean;
9
- hasDocTools: boolean;
10
- hasBrowserTools: boolean;
11
- hasEmailRegistrationTools: boolean;
12
- hasEmailRegistrationWorkflow: boolean;
13
- };
14
-
15
- const normalizeToolName = (name: string): string =>
16
- name.replace(/[-_]/g, "").toLowerCase();
17
-
18
- const hasAnyTool = (normalizedTools: Set<string>, candidates: string[]): boolean =>
19
- candidates.some((candidate) => normalizedTools.has(normalizeToolName(candidate)));
20
-
21
- const hasAllTools = (normalizedTools: Set<string>, candidates: string[]): boolean =>
22
- candidates.every((candidate) => normalizedTools.has(normalizeToolName(candidate)));
23
-
24
- const buildEmailRegistrationWorkflowBlock = (
25
- enabled: boolean
26
- ): string => {
27
- if (!enabled) return "";
28
-
29
- return [
30
- "--- 邮箱验证码注册流程 ---",
31
- "当用户要求你注册网站账号时,只允许处理用户明确指定的当前目标网站,不要自行扩展到其他网站、批量注册或规避平台风控。",
32
- "",
33
- "分阶段协议:discover before acting -> assess supportability -> register -> verify -> closeout。",
34
- "",
35
- "推荐流程:",
36
- "1. discover before acting:先用 browser_openSession / browser_readContent 阅读页面,确认目标注册页 URL、账号用途、必填项和停止条件;不要一打开页面就盲点按钮。如果缺少目标网站,先询问用户。",
37
- "2. assess supportability:先判断该流程是否支持当前受控自动化。遇到 CAPTCHA、手机号验证、支付、身份/KYC、OAuth-only、服务条款确认、或任何看起来像规避风控的步骤时,必须立即停止并向用户说明 blockingReason;不要硬闯。",
38
- "3. register:只有在确认支持后,才使用 email_provision_identity 为当前 agent 生成受控域名邮箱身份,再使用 browser_openSession / browser_typeText / browser_click / browser_readContent 填写并提交注册表单。",
39
- "4. verify:提交后使用 email_wait_for 等待该 agent 收件箱里的验证邮件,再用 email_extract_verification 提取验证码或验证链接,回填验证码或打开验证链接完成验证。",
40
- "5. closeout:无论成功还是失败都要 always close sessions,主动清理浏览器会话(例如 browser_closeSession)。如果流程失败,必须明确 failedStage 与 blockingReason,并说明可恢复选项;不要盲目尝试无关网站或绕过验证。",
41
- "6. 最终只在对话中返回账号、邮箱和一次性生成的密码;不要持久化密码,不要写入 agent metadata、数据库、文档或记忆。",
42
- "",
43
- "必须暂停并询问用户的情况:CAPTCHA、手机号验证、支付、身份/KYC、OAuth 授权、OAuth-only、服务条款确认、或任何看起来像规避风控的步骤。",
44
- "如果流程失败,说明 failedStage、blockingReason 和可恢复选项,不要盲目尝试无关网站或绕过验证。",
45
- ].join("\n");
46
- };
47
-
48
- export const resolveRuntimeGuidanceToolOptions = (
49
- tools: string[] = []
50
- ): RuntimeGuidanceToolOptions => {
51
- const normalizedTools = canonicalizeToolNames(tools);
52
- const normalizedToolSet = new Set(normalizedTools.map(normalizeToolName));
53
- const hasBrowserTools = hasAllTools(normalizedToolSet, [
54
- "browser_openSession",
55
- "browser_readContent",
56
- "browser_typeText",
57
- "browser_click",
58
- "browser_closeSession",
59
- ]);
60
- const hasBrowserProbe = hasAnyTool(normalizedToolSet, ["browser_probePage", "browserProbePage"]);
61
- const hasEmailRegistrationTools = hasAllTools(normalizedToolSet, [
62
- "email_provision_identity",
63
- "email_wait_for",
64
- "email_extract_verification",
65
- ]);
66
-
67
- return {
68
- hasCheckEnvTool: normalizedTools.includes("checkEnv"),
69
- hasExecShellTool: normalizedTools.includes("execShell"),
70
- hasRememberMemoryTool: normalizedTools.includes("rememberMemory"),
71
- hasDocTools: normalizedTools.some((tool) =>
72
- ["read", "readDoc", "readPage", "createDoc", "updateDoc"].includes(tool)
73
- ),
74
- hasBrowserTools,
75
- hasEmailRegistrationTools,
76
- // Require browser_probePage to be present before enabling email registration workflow guidance.
77
- hasEmailRegistrationWorkflow: hasBrowserTools && hasBrowserProbe && hasEmailRegistrationTools,
78
- };
79
- };
80
-
81
- export const buildRuntimeGuidanceBlocks = (tools: string[] = []) => {
82
- const options = resolveRuntimeGuidanceToolOptions(tools);
83
-
84
- return {
85
- startupProtocol: buildStartupProtocolBlock({
86
- hasCheckEnvTool: options.hasCheckEnvTool,
87
- hasExecShellTool: options.hasExecShellTool,
88
- }),
89
- contextLayerContract: buildContextLayerContractBlock({
90
- hasRememberMemoryTool: options.hasRememberMemoryTool,
91
- hasDocTools: options.hasDocTools,
92
- }),
93
- emailRegistrationWorkflow: buildEmailRegistrationWorkflowBlock(
94
- options.hasEmailRegistrationWorkflow
95
- ),
96
- };
97
- };
@@ -1,37 +0,0 @@
1
- export const normalizeServerOrigin = (base: string): string | null => {
2
- try {
3
- return new URL(base).origin.replace(/\/+$/, "");
4
- } catch {
5
- return null;
6
- }
7
- };
8
-
9
- export const extractAgentRuntimeServerBase = (agentRecord: any): string | null => {
10
- const candidates = [
11
- agentRecord?.delegation?.serverBase,
12
- agentRecord?.runtime?.serverBase,
13
- agentRecord?.runtimeServerBase,
14
- ];
15
-
16
- if (Array.isArray(agentRecord?.runtimes)) {
17
- const runtimes = agentRecord.runtimes
18
- .filter((runtime: any) => runtime && typeof runtime === "object")
19
- .slice()
20
- .sort((a: any, b: any) => {
21
- const priorityA =
22
- typeof a.priority === "number" ? a.priority : Number.MAX_SAFE_INTEGER;
23
- const priorityB =
24
- typeof b.priority === "number" ? b.priority : Number.MAX_SAFE_INTEGER;
25
- return priorityA - priorityB;
26
- });
27
- candidates.push(...runtimes.map((runtime: any) => runtime.serverBase));
28
- }
29
-
30
- for (const candidate of candidates) {
31
- if (typeof candidate === "string" && candidate.trim()) {
32
- return candidate.trim();
33
- }
34
- }
35
-
36
- return null;
37
- };
@@ -1,128 +0,0 @@
1
- import serverDb from "database/server/db";
2
- import { pubAgentKeys } from "database/keys";
3
- import { Agent } from "app/types";
4
- import { sortAgents } from "ai/agent/utils/sortUtils";
5
- import { supportsImageGeneration } from "ai/agent/utils/imageOutput";
6
- import { isTombstoneRecord } from "database/tombstones";
7
-
8
- export interface FetchPublicAgentsOptions {
9
- limit?: number;
10
- sortBy?:
11
- | "recommended"
12
- | "newest"
13
- | "popular"
14
- | "rating"
15
- | "outputPriceAsc"
16
- | "outputPriceDesc"
17
- | "favorite"; // 新增:按收藏排序
18
- searchName?: string;
19
- userId?: string;
20
- imageOutputOnly?: boolean;
21
- /** Filter agents that include this tool in their tools array */
22
- toolName?: string;
23
- }
24
-
25
- export interface FetchPublicAgentsResult {
26
- data: Agent[];
27
- total: number;
28
- hasMore: boolean;
29
- }
30
-
31
- // 局部 helpers 已移除,使用 shared utils
32
-
33
- async function dbList<T>(
34
- gte: string,
35
- lte: string,
36
- filter?: (v: T) => boolean
37
- ): Promise<T[]> {
38
- const res: T[] = [];
39
- for await (const [, value] of serverDb.iterator({ gte, lte })) {
40
- if (!filter || filter(value as T)) res.push(value as T);
41
- }
42
- return res;
43
- }
44
-
45
- // 轻量优化:对 public agents 做一个短 TTL 的内存缓存,减少频繁迭代 DB
46
- const PUBLIC_AGENTS_CACHE_TTL_MS = 5 * 1000; // 5 秒
47
- let cachedPublicAgents: Agent[] | null = null;
48
- let cachedPublicAgentsAt = 0;
49
-
50
- export function invalidatePublicAgentsCache() {
51
- cachedPublicAgents = null;
52
- cachedPublicAgentsAt = 0;
53
- }
54
-
55
- async function getPublicAgents(): Promise<Agent[]> {
56
- const now = Date.now();
57
- if (cachedPublicAgents && now - cachedPublicAgentsAt < PUBLIC_AGENTS_CACHE_TTL_MS) {
58
- return cachedPublicAgents;
59
- }
60
-
61
- const ranges = pubAgentKeys.allPublicRanges();
62
- let list: Agent[] = [];
63
-
64
- for (const { start, end } of ranges) {
65
- const res = await dbList<Agent>(
66
- start,
67
- end,
68
- (v) => (v as any).isPublic && !isTombstoneRecord(v)
69
- );
70
- list = list.concat(res);
71
- }
72
-
73
- cachedPublicAgents = list;
74
- cachedPublicAgentsAt = now;
75
-
76
- return list;
77
- }
78
-
79
- // 轻量优化:限制单次请求的最大返回条数,避免未来被滥用
80
- const DEFAULT_LIMIT = 20;
81
- const MAX_LIMIT = 100;
82
-
83
- export async function fetchPublicAgents(
84
- options: FetchPublicAgentsOptions = {}
85
- ): Promise<FetchPublicAgentsResult> {
86
- const {
87
- limit: rawLimit = DEFAULT_LIMIT,
88
- sortBy = "recommended",
89
- searchName,
90
- userId,
91
- imageOutputOnly = false,
92
- toolName,
93
- } = options;
94
-
95
- const limit = Math.min(Math.max(rawLimit, 1), MAX_LIMIT);
96
-
97
- // 使用带缓存的获取函数
98
- const baseList = await getPublicAgents();
99
-
100
- // 注意不要修改缓存引用,后续操作都在拷贝/派生数组上做
101
- let list = baseList;
102
-
103
- if (searchName) {
104
- const kw = searchName.toLowerCase();
105
- list = list.filter((agent) => agent.name?.toLowerCase().includes(kw));
106
- }
107
-
108
- if (userId) {
109
- list = list.filter((agent) => agent.userId === userId);
110
- }
111
-
112
- if (imageOutputOnly) {
113
- list = list.filter((agent) => supportsImageGeneration(agent));
114
- }
115
-
116
- if (toolName) {
117
- const kw = toolName.toLowerCase();
118
- list = list.filter((agent) =>
119
- agent.tools?.some((t) => t.toLowerCase().includes(kw))
120
- );
121
- }
122
-
123
- // 使用统一的排序逻辑
124
- const sorted = sortAgents(list, sortBy);
125
-
126
- const data = sorted.slice(0, limit);
127
- return { data, total: list.length, hasMore: list.length > limit };
128
- }