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