nolo-cli 0.1.13 → 0.1.15
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +9 -2
- package/agent-runtime/hostAdapter.ts +53 -0
- package/agent-runtime/index.ts +28 -0
- package/agent-runtime/localLoop.ts +62 -0
- package/agent-runtime/runtimeDecision.ts +70 -0
- package/agent-runtime/types.ts +87 -0
- package/agentRunCommand.ts +104 -0
- package/agentRuntimeCommands.ts +139 -22
- package/agentRuntimeLocal.ts +7 -0
- package/ai/agent/_executeModel.ts +118 -0
- package/ai/agent/agentSlice.ts +544 -1
- package/ai/agent/appWorkingMemory.ts +126 -0
- package/ai/agent/avatarUtils.ts +24 -0
- package/ai/agent/buildEditingContext.ts +373 -0
- package/ai/agent/buildSystemPrompt.ts +532 -0
- package/ai/agent/cleanAgentMessages.ts +140 -0
- package/ai/agent/cliChatClient.ts +119 -0
- package/ai/agent/contextCompiler.ts +107 -0
- package/ai/agent/contextLayerContract.ts +44 -0
- package/ai/agent/createAgentSchema.ts +234 -0
- package/ai/agent/executeToolCall.ts +58 -0
- package/ai/agent/fetchAgentContexts.ts +42 -0
- package/ai/agent/generatePrompt.ts +3 -0
- package/ai/agent/getFullChatContextKeys.ts +168 -0
- package/ai/agent/hooks/fetchPublicAgents.ts +133 -0
- package/ai/agent/hooks/useAgentConfig.ts +61 -0
- package/ai/agent/hooks/useAgentDialog.ts +35 -0
- package/ai/agent/hooks/useAgentFormValidation.ts +202 -0
- package/ai/agent/hooks/usePublicAgents.ts +473 -0
- package/ai/agent/persistMessageWithFixedId.ts +37 -0
- package/ai/agent/planSlice.ts +259 -0
- package/ai/agent/referenceUtils.ts +229 -0
- package/ai/agent/runAgentBackground.ts +238 -0
- package/ai/agent/runAgentClientLoop.ts +138 -0
- package/ai/agent/runtimeGuidance.ts +97 -0
- package/ai/agent/runtimeServerBase.ts +37 -0
- package/ai/agent/server/fetchPublicAgents.ts +128 -0
- package/ai/agent/startParallelAgentStreams.ts +424 -0
- package/ai/agent/startupProtocol.ts +53 -0
- package/ai/agent/streamAgentChatTurn.ts +1299 -0
- package/ai/agent/streamAgentChatTurnUtils.ts +738 -0
- package/ai/agent/types.ts +71 -0
- package/ai/agent/utils/imageOutput.ts +39 -0
- package/ai/agent/utils/publicImageAgentMode.ts +26 -0
- package/ai/agent/utils/sortUtils.ts +250 -0
- package/ai/agent/web/referencePickerUtils.ts +146 -0
- package/ai/ai.locale.ts +1083 -0
- package/ai/chat/accumulateToolCallChunks.ts +95 -0
- package/ai/chat/fetchUtils.native.ts +276 -0
- package/ai/chat/fetchUtils.ts +153 -0
- package/ai/chat/inlineImageUrlsForCustomProvider.ts +117 -0
- package/ai/chat/parseApiError.ts +64 -0
- package/ai/chat/parseMultilineSSE.ts +95 -0
- package/ai/chat/sendOpenAICompletionsRequest.native.ts +682 -0
- package/ai/chat/sendOpenAICompletionsRequest.ts +712 -0
- package/ai/chat/sendOpenAIResponseRequest.ts +512 -0
- package/ai/chat/shouldUseServerProxy.ts +18 -0
- package/ai/chat/sseClient.native.ts +91 -0
- package/ai/chat/sseClient.ts +67 -0
- package/ai/chat/streamReader.native.ts +31 -0
- package/ai/chat/streamReader.ts +62 -0
- package/ai/chat/updateTotalUsage.ts +72 -0
- package/ai/context/buildReferenceContext.ts +437 -0
- package/ai/context/calculateContextUsage.ts +133 -0
- package/ai/context/retention.ts +165 -0
- package/ai/context/tokenUtils.ts +78 -0
- package/ai/index.ts +1 -1
- package/ai/llm/agentCapabilities.ts +74 -0
- package/ai/llm/calculateGeminiImageTokens.ts +57 -0
- package/ai/llm/deepinfra.ts +28 -0
- package/ai/llm/fireworks.ts +68 -0
- package/ai/llm/generateRequestBody.ts +165 -0
- package/ai/llm/getModelContextWindow.ts +84 -0
- package/ai/llm/getNoloKey.ts +37 -0
- package/ai/llm/getPricing.ts +232 -0
- package/ai/llm/hooks/useModelPricing.ts +75 -0
- package/ai/llm/imagePricing.ts +66 -0
- package/ai/llm/isResponseAPIModel.ts +13 -0
- package/ai/llm/kimi.ts +18 -0
- package/ai/llm/mimo.ts +71 -0
- package/ai/llm/mistral.ts +22 -0
- package/ai/llm/modelAvatar.ts +427 -0
- package/ai/llm/models.ts +45 -0
- package/ai/llm/openrouterModels.ts +141 -0
- package/ai/llm/providers.ts +307 -0
- package/ai/llm/reasoningModels.ts +28 -0
- package/ai/llm/types.ts +59 -0
- package/ai/llm/usageRequestOptions.ts +59 -0
- package/ai/memory/capture.ts +148 -0
- package/ai/memory/consolidate.ts +104 -0
- package/ai/memory/delete.ts +147 -0
- package/ai/memory/overlay.ts +84 -0
- package/ai/memory/query.ts +38 -0
- package/ai/memory/queryShared.ts +160 -0
- package/ai/memory/rank.ts +105 -0
- package/ai/memory/recentRelationshipRecap.ts +247 -0
- package/ai/memory/remember.ts +167 -0
- package/ai/memory/runtime.ts +76 -0
- package/ai/memory/store.ts +20 -0
- package/ai/memory/storeShared.ts +76 -0
- package/ai/memory/types.ts +46 -0
- package/ai/memory/understanding.ts +349 -0
- package/ai/memory/understandingGreeting.ts +264 -0
- package/ai/messages/type.ts +20 -0
- package/ai/policy/personalizationDialog.ts +333 -0
- package/ai/policy/runtimePolicy.ts +440 -0
- package/ai/policy/selfUpdateFields.ts +48 -0
- package/ai/policy/types.ts +64 -0
- package/ai/skills/referenceRuntime.ts +274 -0
- package/ai/skills/skillDiagnostics.ts +251 -0
- package/ai/skills/skillDocBuilder.ts +139 -0
- package/ai/skills/skillDocProtocol.ts +434 -0
- package/ai/skills/skillReferenceSummary.ts +63 -0
- package/ai/skills/skillSummaryMarker.ts +26 -0
- package/ai/token/calculatePrice.ts +546 -0
- package/ai/token/db.ts +98 -0
- package/ai/token/externalToolCost.ts +321 -0
- package/ai/token/hooks/useRecords.ts +65 -0
- package/ai/token/missingUsageEstimate.ts +42 -0
- package/ai/token/modelUsageQuery.ts +252 -0
- package/ai/token/normalizeUsage.ts +84 -0
- package/ai/token/openaiImageGenerationUsage.ts +56 -0
- package/ai/token/prepareTokenUsageData.ts +88 -0
- package/ai/token/query.ts +88 -0
- package/ai/token/queryUserTokens.ts +59 -0
- package/ai/token/resolveBillingTarget.ts +52 -0
- package/ai/token/saveTokenRecord.ts +53 -0
- package/ai/token/serverDialogProjection.ts +78 -0
- package/ai/token/serverTokenWriter.ts +143 -0
- package/ai/token/stats.ts +21 -0
- package/ai/token/tokenThunks.ts +24 -0
- package/ai/token/types.ts +93 -0
- package/ai/tools/agent/agentTools.ts +176 -0
- package/ai/tools/agent/agentUpdateShared.ts +311 -0
- package/ai/tools/agent/callAgentTool.ts +139 -0
- package/ai/tools/agent/createAgentTool.ts +512 -0
- package/ai/tools/agent/createDialogTool.ts +69 -0
- package/ai/tools/agent/createSkillAgentTool.ts +62 -0
- package/ai/tools/agent/parallelBudget.ts +221 -0
- package/ai/tools/agent/presets/appBuilderPreset.ts +147 -0
- package/ai/tools/agent/runLlmTool.ts +96 -0
- package/ai/tools/agent/runStreamingAgentTool.ts +73 -0
- package/ai/tools/agent/skillAgentArgs.ts +106 -0
- package/ai/tools/agent/skillAgentPreset.ts +89 -0
- package/ai/tools/agent/streamParallelAgentsTool.ts +122 -0
- package/ai/tools/agent/updateAgentTool.ts +96 -0
- package/ai/tools/agent/updateSelfTool.ts +113 -0
- package/ai/tools/amazonProductScraperTool.ts +86 -0
- package/ai/tools/apifyActorClient.ts +45 -0
- package/ai/tools/appEditGuard.ts +372 -0
- package/ai/tools/appReadSnapshot.ts +153 -0
- package/ai/tools/appTools.ts +1549 -0
- package/ai/tools/applyEditTool.ts +256 -0
- package/ai/tools/applyLineEditsTool.ts +312 -0
- package/ai/tools/browserTools/click.ts +33 -0
- package/ai/tools/browserTools/closeSession.ts +29 -0
- package/ai/tools/browserTools/common.ts +27 -0
- package/ai/tools/browserTools/openSession.ts +48 -0
- package/ai/tools/browserTools/readContent.ts +38 -0
- package/ai/tools/browserTools/selectOption.ts +46 -0
- package/ai/tools/browserTools/typeText.ts +42 -0
- package/ai/tools/category/createCategoryTool.ts +66 -0
- package/ai/tools/category/queryContentsByCategoryTool.ts +69 -0
- package/ai/tools/category/updateContentCategoryTool.ts +75 -0
- package/ai/tools/cfBrowserTools.ts +319 -0
- package/ai/tools/cfSpeechToTextTool.ts +49 -0
- package/ai/tools/checkEnvTool.ts +65 -0
- package/ai/tools/cloudflareCrawlTool.ts +289 -0
- package/ai/tools/codeSearchTool.ts +111 -0
- package/ai/tools/codeTools.ts +101 -0
- package/ai/tools/createDocTool.ts +132 -0
- package/ai/tools/createPlanTool.ts +999 -0
- package/ai/tools/createSkillDocTool.ts +155 -0
- package/ai/tools/createWorkflowTool.ts +154 -0
- package/ai/tools/deepseekOcrTool.ts +34 -0
- package/ai/tools/delayTool.ts +31 -0
- package/ai/tools/deleteSpacesTool.ts +325 -0
- package/ai/tools/deleteSpacesToolModel.ts +159 -0
- package/ai/tools/devReloadUtils.ts +29 -0
- package/ai/tools/dialogMessageSearch.ts +137 -0
- package/ai/tools/doctorSkillTool.ts +72 -0
- package/ai/tools/ecommerceScraperTool.ts +86 -0
- package/ai/tools/emailTools.ts +549 -0
- package/ai/tools/evalSkillTool.ts +92 -0
- package/ai/tools/exaSearchTool.ts +64 -0
- package/ai/tools/execBashTool.ts +379 -0
- package/ai/tools/executeSqlTool.ts +192 -0
- package/ai/tools/fetchWebpageSupport.ts +309 -0
- package/ai/tools/fetchWebpageTool.ts +84 -0
- package/ai/tools/geminiImagePreviewTool.ts +361 -0
- package/ai/tools/generateDocxTool.ts +215 -0
- package/ai/tools/googleSearchScraperTool.ts +106 -0
- package/ai/tools/importDataTool.ts +133 -0
- package/ai/tools/importSkillTool.ts +162 -0
- package/ai/tools/index.ts +1927 -0
- package/ai/tools/listFilesTool.ts +82 -0
- package/ai/tools/listUserSpacesTool.ts +113 -0
- package/ai/tools/modelUsageTools.ts +199 -0
- package/ai/tools/olmOcrTool.ts +34 -0
- package/ai/tools/openaiImageTool.ts +267 -0
- package/ai/tools/prepareTools.ts +23 -0
- package/ai/tools/readDocTool.ts +84 -0
- package/ai/tools/readFileTool.ts +211 -0
- package/ai/tools/readTool.ts +163 -0
- package/ai/tools/readXPostTool.ts +233 -0
- package/ai/tools/rememberMemoryTool.ts +84 -0
- package/ai/tools/remotionVideoTool.ts +151 -0
- package/ai/tools/searchDialogMessagesTool.ts +222 -0
- package/ai/tools/searchRepoTool.ts +115 -0
- package/ai/tools/searchWorkspaceTool.ts +259 -0
- package/ai/tools/skillFollowup.ts +86 -0
- package/ai/tools/surfWeatherTool.ts +169 -0
- package/ai/tools/table/addTableRowTool.ts +217 -0
- package/ai/tools/table/createTableTool.ts +315 -0
- package/ai/tools/table/rowTools.ts +366 -0
- package/ai/tools/table/schemaTools.ts +244 -0
- package/ai/tools/table/shareTableTool.ts +148 -0
- package/ai/tools/table/toolShared.ts +129 -0
- package/ai/tools/toolApiClient.ts +198 -0
- package/ai/tools/toolNameAliases.ts +57 -0
- package/ai/tools/toolResultError.ts +42 -0
- package/ai/tools/toolRunSlice.ts +303 -0
- package/ai/tools/toolSchemaCompatibility.ts +53 -0
- package/ai/tools/toolVisibility.ts +4 -0
- package/ai/tools/types.ts +20 -0
- package/ai/tools/uiAskChoiceTool.ts +104 -0
- package/ai/tools/updateContentTitleTool.ts +84 -0
- package/ai/tools/updateDocTool.ts +105 -0
- package/ai/tools/updateUserPreferenceProfileTool.ts +145 -0
- package/ai/tools/whisperTool.ts +77 -0
- package/ai/tools/writeFileTool.ts +210 -0
- package/ai/tools/youtubeScraperTool.ts +116 -0
- package/ai/tools/ziweiChartTool.ts +678 -0
- package/ai/types.ts +55 -0
- package/ai/workflow/workflowExecutor.ts +323 -0
- package/ai/workflow/workflowSlice.ts +73 -0
- package/ai/workflow/workflowTypes.ts +106 -0
- package/client/agentRun.test.ts +240 -0
- package/client/agentRun.ts +182 -19
- package/client/compactDialog.test.ts +238 -0
- package/client/localRuntimeAdapter.test.ts +135 -0
- package/client/localRuntimeAdapter.ts +244 -0
- package/client/profileConfig.test.ts +40 -0
- package/client/streamingOutput.test.ts +22 -0
- package/client/streamingOutput.ts +38 -0
- package/commandRegistry.ts +11 -2
- package/connector-experimental/index.ts +5 -0
- package/database/actions/cacheMergedUserData.ts +64 -0
- package/database/actions/common.ts +242 -0
- package/database/actions/deleteFile.ts +40 -0
- package/database/actions/fetchUserData.ts +16 -0
- package/database/actions/fileContent.ts +125 -0
- package/database/actions/patch.ts +155 -0
- package/database/actions/read.ts +337 -0
- package/database/actions/readAndWait.ts +224 -0
- package/database/actions/readRequestManager.ts +120 -0
- package/database/actions/remove.ts +94 -0
- package/database/actions/replication.ts +366 -0
- package/database/actions/upload.ts +174 -0
- package/database/actions/upsert.ts +56 -0
- package/database/actions/write.ts +126 -0
- package/database/client/db.native.ts +73 -0
- package/database/client/db.ts +51 -0
- package/database/client/fetchUserData.ts +61 -0
- package/database/client/handleError.ts +19 -0
- package/database/client/queryRequest.ts +21 -0
- package/database/config.ts +21 -0
- package/database/dbActionThunks.ts +1 -0
- package/database/dbSlice.ts +149 -0
- package/database/email.ts +42 -0
- package/database/fileRing.ts +51 -0
- package/database/fileSharding.ts +70 -0
- package/database/fileStorage.native.ts +92 -0
- package/database/fileStorage.ts +232 -0
- package/database/fileUrl.ts +34 -0
- package/database/hooks/useUserData.ts +489 -0
- package/database/index.ts +1 -0
- package/database/keys.ts +765 -0
- package/database/queryPrefixes.ts +14 -0
- package/database/requests.ts +443 -0
- package/database/runtimeServerContext.ts +35 -0
- package/database/server/MemoryDB.ts +76 -0
- package/database/server/actorAccess.ts +76 -0
- package/database/server/agentDelegation.ts +124 -0
- package/database/server/coreDataOwnership.ts +13 -0
- package/database/server/coreDataProxy.ts +76 -0
- package/database/server/cybotReadonly.ts +18 -0
- package/database/server/dataHandlers.ts +111 -0
- package/database/server/db.ts +118 -0
- package/database/server/dbPath.ts +20 -0
- package/database/server/delete.ts +499 -0
- package/database/server/emailRepository.ts +1480 -0
- package/database/server/ensureDbOpen.ts +12 -0
- package/database/server/fileRead.ts +337 -0
- package/database/server/fileService.ts +436 -0
- package/database/server/handleTransaction.ts +86 -0
- package/database/server/patch.ts +282 -0
- package/database/server/query.ts +138 -0
- package/database/server/read.ts +325 -0
- package/database/server/resourceAccess.ts +211 -0
- package/database/server/routes.ts +110 -0
- package/database/server/spaceMemberAuthority.ts +67 -0
- package/database/server/upload.ts +159 -0
- package/database/server/write.ts +494 -0
- package/database/server/writeAuthority.ts +133 -0
- package/database/sqliteDb.ts +46 -0
- package/database/table/deleteTable.ts +120 -0
- package/database/tenantPlacement.ts +57 -0
- package/database/tombstones.ts +52 -0
- package/database/userDataLoadDecision.ts +17 -0
- package/database/userDataMerge.ts +95 -0
- package/database/userPreferenceRegister.ts +108 -0
- package/database/utils/dbPath.ts +47 -0
- package/database/utils/ulid.native.ts +6 -0
- package/database/utils/ulid.ts +1 -0
- package/index.ts +37 -19
- package/localRuntimeDb.ts +28 -0
- package/package.json +17 -4
- package/runtimeModeArgs.ts +33 -0
- package/tui/readlineWorkspace.ts +1 -0
- package/tui/session.ts +22 -0
|
@@ -0,0 +1,238 @@
|
|
|
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
|
+
});
|
|
@@ -0,0 +1,138 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,97 @@
|
|
|
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
|
+
};
|
|
@@ -0,0 +1,37 @@
|
|
|
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
|
+
};
|
|
@@ -0,0 +1,128 @@
|
|
|
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
|
+
}
|