nolo-cli 0.1.40 → 0.1.42
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 +27 -12
- package/agent-runtime/agentThread.ts +222 -0
- package/agent-runtime/agentThreadAdmission.ts +74 -0
- package/agent-runtime/dialogWritePlan.ts +42 -0
- package/agent-runtime/hostAdapter.ts +1 -0
- package/agent-runtime/hybridRecordStore.ts +5 -3
- package/agent-runtime/index.ts +43 -0
- package/agent-runtime/localLoop.ts +31 -2
- package/agent-runtime/localToolPolicy.ts +1 -0
- package/agent-runtime/localWorkspaceTools.ts +1258 -78
- package/agent-runtime/noloWorkspaceTools.ts +20 -1
- package/agent-runtime/runtimeToolPolicy.ts +1 -5
- package/agent-runtime/runtimeToolSurface.ts +1 -0
- package/agentAliases.ts +29 -9
- package/agentInternalCommandEntries.ts +11 -3
- package/agentMachineCommands.ts +1 -0
- package/agentRecordCommands.ts +2 -2
- package/agentRecordHelpers.ts +59 -6
- package/agentRunCommand.ts +84 -19
- package/ai/agent/_executeModel.ts +146 -0
- package/ai/agent/agentSlice.ts +556 -1
- package/ai/agent/appWorkingMemory.ts +126 -0
- package/ai/agent/buildEditingContext.ts +378 -0
- package/ai/agent/buildSystemPrompt.ts +599 -0
- package/ai/agent/canvasEditingContext.ts +49 -0
- package/ai/agent/cleanAgentMessages.ts +140 -0
- package/ai/agent/cliChatClient.ts +153 -0
- package/ai/agent/cliExecutor.ts +264 -46
- package/ai/agent/contextCompiler.ts +107 -0
- package/ai/agent/contextLayerContract.ts +44 -0
- package/ai/agent/createAgentSchema.ts +272 -0
- package/ai/agent/currentTimeContext.ts +65 -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/guidedCreation/capabilities.ts +84 -0
- package/ai/agent/guidedCreation/types.ts +38 -0
- package/ai/agent/isLiveAudioOnlyAgent.ts +26 -0
- package/ai/agent/liveAudioModel.ts +2 -0
- package/ai/agent/pageBuilderHandoffRules.ts +75 -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 +310 -0
- package/ai/agent/runAgentClientLoop.ts +164 -0
- package/ai/agent/runtimeGuidance.ts +116 -0
- package/ai/agent/serverOrigin.ts +7 -0
- package/ai/agent/startParallelAgentStreams.ts +424 -0
- package/ai/agent/startupProtocol.ts +53 -0
- package/ai/agent/streamAgentChatTurn.ts +2135 -0
- package/ai/agent/streamAgentChatTurnUtils.ts +923 -0
- package/ai/agent/toolFailureGuard.ts +99 -0
- package/ai/agent/types.ts +73 -0
- package/ai/agent/utils/imageOutput.ts +37 -0
- package/ai/agent/utils/publicImageAgentMode.ts +26 -0
- package/ai/ai.locale.ts +1165 -0
- package/ai/chat/accumulateToolCallChunks.ts +126 -0
- package/ai/chat/fetchUtils.ts +143 -0
- package/ai/chat/inlineImageUrlsForCustomProvider.ts +122 -0
- package/ai/chat/parseApiError.ts +67 -0
- package/ai/chat/parseMultilineSSE.ts +95 -0
- package/ai/chat/sendOpenAICompletionsRequest.ts +921 -0
- package/ai/chat/sendOpenAIResponseRequest.ts +720 -0
- package/ai/chat/serverProxyRetry.ts +102 -0
- package/ai/chat/shouldUseServerProxy.ts +18 -0
- package/ai/chat/streamRetry.ts +46 -0
- package/ai/chat/updateTotalUsage.ts +89 -0
- package/ai/context/buildReferenceContext.ts +437 -0
- package/ai/context/retention.ts +165 -0
- package/ai/context/tokenUtils.ts +78 -0
- package/ai/llm/agentCapabilities.ts +74 -0
- package/ai/llm/deepinfra.ts +79 -0
- package/ai/llm/fireworks.ts +68 -0
- package/ai/llm/generateRequestBody.ts +165 -0
- package/ai/llm/getModelContextWindow.ts +93 -0
- package/ai/llm/getPricing.ts +252 -0
- package/ai/llm/imagePricing.ts +66 -0
- package/ai/llm/isResponseAPIModel.ts +13 -0
- package/ai/llm/kimi.ts +22 -0
- package/ai/llm/mimo.ts +99 -0
- package/ai/llm/mistral.ts +21 -0
- package/ai/llm/models.ts +45 -0
- package/ai/llm/openrouterModels.ts +31 -0
- package/ai/llm/providers.ts +313 -0
- package/ai/llm/reasoningModels.ts +27 -0
- package/ai/llm/types.ts +82 -0
- package/ai/llm/usageRequestOptions.ts +59 -0
- package/ai/memory/policy.ts +49 -0
- package/ai/memory/queryShared.ts +167 -0
- package/ai/memory/recentRelationshipRecap.ts +251 -0
- package/ai/memory/remember.ts +111 -0
- package/ai/memory/scope.ts +74 -0
- package/ai/memory/store.ts +20 -0
- package/ai/memory/storeShared.ts +76 -0
- package/ai/memory/types.ts +52 -0
- package/ai/memory/understanding.ts +341 -0
- package/ai/memory/understandingGreeting.ts +264 -0
- package/ai/policy/personalizationDialog.ts +333 -0
- package/ai/policy/runtimePolicy.ts +526 -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/skillSummaryMarker.ts +26 -0
- package/ai/token/calculatePrice.ts +576 -0
- package/ai/token/missingUsageEstimate.ts +42 -0
- package/ai/token/normalizeUsage.ts +124 -0
- package/ai/token/openaiImageGenerationUsage.ts +56 -0
- package/ai/token/prepareTokenUsageData.ts +93 -0
- package/ai/token/resolveBillingTarget.ts +52 -0
- package/ai/token/saveTokenRecord.ts +53 -0
- package/ai/token/types.ts +103 -0
- package/ai/tools/agent/agentTools.ts +214 -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/prepareAgentDraftTool.ts +159 -0
- package/ai/tools/agent/presets/appBuilderPreset.ts +154 -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/startAgentDialogTool.ts +54 -0
- package/ai/tools/agent/streamParallelAgentsTool.ts +123 -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 +259 -0
- package/ai/tools/applyLineEditsTool.ts +315 -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 +102 -0
- package/ai/tools/cloudflareCrawlTool.ts +289 -0
- package/ai/tools/codeSearchTool.ts +123 -0
- package/ai/tools/codeTools.ts +101 -0
- package/ai/tools/convertMarxistsBookTool.ts +103 -0
- package/ai/tools/createDocTool.ts +132 -0
- package/ai/tools/createSkillDocTool.ts +155 -0
- package/ai/tools/createWorkflowTool.ts +154 -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 +63 -0
- package/ai/tools/execShellTool.ts +266 -0
- package/ai/tools/executeSqlTool.ts +192 -0
- package/ai/tools/fetchWebpageSupport.ts +333 -0
- package/ai/tools/fetchWebpageTool.ts +97 -0
- package/ai/tools/geminiImagePreviewTool.ts +386 -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 +2290 -0
- package/ai/tools/jdProductScraperTool.ts +821 -0
- package/ai/tools/listUserSpacesTool.ts +113 -0
- package/ai/tools/localFilesTool.ts +276 -0
- package/ai/tools/marxistsOfflineBook.ts +253 -0
- package/ai/tools/modelUsageTools.ts +206 -0
- package/ai/tools/noloWorkspaceReadTools.ts +467 -0
- package/ai/tools/olmOcrTool.ts +33 -0
- package/ai/tools/openaiImageTool.ts +267 -0
- package/ai/tools/prepareTools.ts +193 -0
- package/ai/tools/readDocTool.ts +88 -0
- package/ai/tools/readFileTool.ts +214 -0
- package/ai/tools/readTool.ts +194 -0
- package/ai/tools/readXPostTool.ts +229 -0
- package/ai/tools/readXhsProfileTool.ts +638 -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/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 +326 -0
- package/ai/tools/table/rowTools.ts +386 -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/taobaoTmallProductScraperTool.ts +49 -0
- package/ai/tools/toolApiClient.ts +279 -0
- package/ai/tools/toolPacks.ts +19 -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/uiAskChoiceTool.ts +104 -0
- package/ai/tools/updateContentTitleTool.ts +84 -0
- package/ai/tools/updateDocTool.ts +105 -0
- package/ai/tools/updateUserPreferenceProfileTool.ts +153 -0
- package/ai/tools/wereadGatewayTool.ts +152 -0
- package/ai/tools/whisperTool.ts +76 -0
- package/ai/tools/writeFileTool.ts +213 -0
- package/ai/tools/youtubeScraperTool.ts +116 -0
- package/ai/tools/ziweiChartTool.ts +678 -0
- package/ai/types.ts +58 -0
- package/ai/workflow/workflowExecutor.ts +323 -0
- package/ai/workflow/workflowSlice.ts +73 -0
- package/ai/workflow/workflowTypes.ts +106 -0
- package/app/actions/syncAppRecord.ts +73 -0
- package/app/constants/animationSets.ts +21 -0
- package/app/constants/appEditor.ts +99 -0
- package/app/favorite/deletedFavoriteProjection.ts +31 -0
- package/app/favorite/favoriteSlice.ts +690 -0
- package/app/favorite/type.ts +43 -0
- package/app/hooks/deleteDbKey.ts +254 -0
- package/app/i18n/client.ts +13 -0
- package/app/i18n/i18n.base.ts +14 -0
- package/app/i18n/i18n.config.ts +77 -0
- package/app/i18n/index.ts +15 -0
- package/app/i18n/mapLanguage.ts +11 -0
- package/app/i18n/translations/appBuilder.locale.ts +453 -0
- package/app/i18n/translations/error.locale.ts +246 -0
- package/app/i18n/translations/interface.locale.ts +2206 -0
- package/app/i18n/translations/pricing.locale.ts +397 -0
- package/app/i18n/translations/privacy.locale.ts +168 -0
- package/app/i18n/translations/seo.locale.ts +129 -0
- package/app/i18n/translations/terms.locale.ts +124 -0
- package/app/i18n/types.ts +6 -0
- package/app/notifications/model.ts +77 -0
- package/app/notifications/notificationSlice.ts +141 -0
- package/app/reducer.ts +36 -0
- package/app/settings/dbActionThunks.ts +34 -0
- package/app/settings/defaultSpacePreference.ts +87 -0
- package/app/settings/settingSlice.tsx +1212 -0
- package/app/settings/settingsRecord.ts +8 -0
- package/app/stateViews/runtime.ts +90 -0
- package/app/store.ts +55 -0
- package/app/theme/colors.ts +280 -0
- package/app/theme/fontPreference.ts +77 -0
- package/app/theme/theme.config.ts +120 -0
- package/app/theme/themeModeBootstrap.ts +123 -0
- package/app/types/appSummary.ts +118 -0
- package/app/types.ts +358 -0
- package/app/utils/appKeys.ts +55 -0
- package/app/utils/async.ts +28 -0
- package/app/utils/desktopAgentRuntimeTurnClient.ts +75 -0
- package/app/utils/env.ts +17 -0
- package/app/utils/fileUtils.ts +229 -0
- package/app/utils/imageUtils.ts +274 -0
- package/app/utils/myContentItems.ts +289 -0
- package/app/utils/retryAfter.ts +36 -0
- package/app/utils/retryFetch.ts +61 -0
- package/app/utils/toast.ts +3 -0
- package/auth/action/signUpAction.ts +227 -0
- package/auth/adminPermissions.ts +78 -0
- package/auth/authSlice.ts +531 -0
- package/auth/client/loginRequest.ts +14 -0
- package/auth/resetAuthScopedClientState.ts +27 -0
- package/auth/routes.ts +183 -0
- package/auth/token.ts +63 -0
- package/auth/types.ts +40 -0
- package/chat/chat.locale.ts +817 -0
- package/chat/dialog/actions/addCybotAction.ts +52 -0
- package/chat/dialog/actions/addReferenceKeysAction.ts +62 -0
- package/chat/dialog/actions/builtinDialogLlm.ts +35 -0
- package/chat/dialog/actions/cleanupCliSession.ts +40 -0
- package/chat/dialog/actions/createDialogAction.ts +379 -0
- package/chat/dialog/actions/createScheduledTaskAction.ts +78 -0
- package/chat/dialog/actions/handleSendMessageAction.ts +202 -0
- package/chat/dialog/actions/handleSendMessageResolver.ts +27 -0
- package/chat/dialog/actions/removeCybotAction.ts +6 -0
- package/chat/dialog/actions/removeDialogAgentAction.ts +44 -0
- package/chat/dialog/actions/replacePrimaryCybotAction.ts +6 -0
- package/chat/dialog/actions/setPrimaryDialogAgentAction.ts +52 -0
- package/chat/dialog/actions/updateDialogSummaryAction.ts +430 -0
- package/chat/dialog/actions/updateDialogTitleAction.ts +151 -0
- package/chat/dialog/actions/updateDialogTitlePolicy.ts +30 -0
- package/chat/dialog/actions/updateTokensAction.ts +233 -0
- package/chat/dialog/dialogAgents.ts +72 -0
- package/chat/dialog/dialogAttachmentCleanup.ts +135 -0
- package/chat/dialog/dialogSlice.ts +985 -0
- package/chat/dialog/dialogTitle.ts +81 -0
- package/chat/dialog/dialogTokenStats.ts +12 -0
- package/chat/dialog/dialogUrl.ts +6 -0
- package/chat/dialog/goal.ts +108 -0
- package/chat/dialog/objectAssistantRegistry.ts +286 -0
- package/chat/dialog/schedule.ts +17 -0
- package/chat/messages/activityCompletion.ts +97 -0
- package/chat/messages/fetchAndCacheMessages.ts +119 -0
- package/chat/messages/fetchConvMsgs.ts +51 -0
- package/chat/messages/fileUrl.ts +13 -0
- package/chat/messages/imagePayloadPersistence.ts +22 -0
- package/chat/messages/messageAgent.ts +8 -0
- package/chat/messages/messageContent.ts +203 -0
- package/chat/messages/messageContract.ts +88 -0
- package/chat/messages/messageSlice.ts +1386 -0
- package/chat/messages/toolThunks.ts +646 -0
- package/chat/messages/types.ts +8 -1
- package/chat/messages/web/toolDisplayName.ts +686 -0
- package/cli/agentAliases.ts +29 -9
- package/cli/agentInternalCommandEntries.ts +11 -3
- package/cli/agentMachineCommands.ts +1 -0
- package/cli/agentRecordCommands.ts +2 -2
- package/cli/agentRecordHelpers.ts +59 -6
- package/cli/agentRunCommand.ts +84 -19
- package/cli/client/agentRun.ts +92 -261
- package/cli/client/localRuntimeAdapter.ts +292 -50
- package/cli/commandRegistry.ts +7 -4
- package/cli/dialogAttachmentCleanup.ts +151 -0
- package/cli/dialogCommands.ts +163 -61
- package/cli/dialogInternalCommandEntries.ts +1 -1
- package/cli/docCreateCommands.ts +345 -0
- package/cli/docInternalCommandEntries.ts +11 -0
- package/cli/docPageHelpers.ts +80 -0
- package/cli/docSkillHelpers.ts +67 -0
- package/cli/docSpaceHelpers.ts +46 -0
- package/cli/internalCommandEntries.ts +4 -0
- package/cli/machineCommands.ts +18 -34
- package/cli/memoryCommands.ts +253 -0
- package/cli/memoryInternalCommandEntries.ts +9 -0
- package/cli/qoderUsageProbe.ts +252 -0
- package/cli/scriptCommandEntries.ts +0 -3
- package/cli/secretScan.ts +31 -0
- package/cli/tableCommands.ts +248 -13
- package/cli/workflowInternalCommandEntries.ts +2 -3
- package/client/agentRun.test.ts +119 -83
- package/client/agentRun.ts +92 -261
- package/client/localRuntimeAdapter.test.ts +894 -32
- package/client/localRuntimeAdapter.ts +292 -50
- package/commandRegistry.ts +7 -4
- package/connector-experimental/capabilities.ts +10 -9
- package/core/builtinAgents.ts +209 -0
- package/core/config.ts +15 -0
- package/core/crypto.ts +51 -0
- package/core/generateKeyPairFromSeedV1.ts +15 -0
- package/core/generateMainKey.ts +22 -0
- package/core/init.ts +41 -0
- package/core/localOrigins.ts +8 -0
- package/core/password.ts +13 -0
- package/core/prefix.ts +23 -0
- package/create/editor/transforms/fromMarkdown/index.ts +260 -0
- package/create/editor/transforms/fromMarkdown/inline.ts +285 -0
- package/create/editor/transforms/fromMarkdown/table.ts +130 -0
- package/create/editor/transforms/markdownToSlate.ts +24 -0
- package/create/editor/transforms/slateToRenderMarkdown.ts +218 -0
- package/create/editor/transforms/slateToSimplifiedMarkdown.ts +224 -0
- package/create/editor/transforms/slateToText.ts +33 -0
- package/create/editor/utils/slateUtils.ts +233 -0
- package/create/space/addSpaceAction.ts +193 -0
- package/create/space/category/categoryActions.ts +427 -0
- package/create/space/changeSpaceAction.ts +36 -0
- package/create/space/constants.ts +8 -0
- package/create/space/content/addContentAction.ts +116 -0
- package/create/space/content/contentThunks.ts +90 -0
- package/create/space/content/deleteContentFromSpaceAction.ts +187 -0
- package/create/space/content/deleteMultipleContentAction.ts +112 -0
- package/create/space/content/moveContentAction.ts +285 -0
- package/create/space/content/updateContentCategoryAction.ts +154 -0
- package/create/space/content/updateContentTitleAction.ts +151 -0
- package/create/space/content/uploadAndAddFileToSpaceAction.ts +98 -0
- package/create/space/contentKeyUtils.ts +117 -0
- package/create/space/deleteSpaceAction.ts +276 -0
- package/create/space/fetchSpaceAction.ts +70 -0
- package/create/space/fetchSpaceSidebarStateAction.ts +26 -0
- package/create/space/loadDefaultSpaceAction.ts +57 -0
- package/create/space/member/addMemberAction.ts +79 -0
- package/create/space/member/fetchUserSpaceMembershipsAction.ts +386 -0
- package/create/space/member/memberThunks.ts +47 -0
- package/create/space/member/removeMemberAction.ts +67 -0
- package/create/space/resolvePreferredSpaceId.ts +113 -0
- package/create/space/sidebarVisibleTypes.ts +260 -0
- package/create/space/space.locale.ts +1087 -0
- package/create/space/spaceAccess.ts +182 -0
- package/create/space/spaceCollapsedState.ts +54 -0
- package/create/space/spaceKeys.ts +65 -0
- package/create/space/spaceSlice.ts +328 -0
- package/create/space/spaceThunks.ts +410 -0
- package/create/space/types.ts +27 -0
- package/create/space/updateSpaceAction.ts +107 -0
- package/create/space/utils/permissions.ts +47 -0
- package/create/types.ts +21 -0
- package/database/actions/agentReadResolution.ts +33 -0
- package/database/actions/common.ts +243 -0
- package/database/actions/deleteFile.ts +40 -0
- package/database/actions/fileContent.ts +125 -0
- package/database/actions/patch.ts +155 -0
- package/database/actions/read.ts +358 -0
- package/database/actions/readAndWait.ts +260 -0
- package/database/actions/readRequestManager.ts +120 -0
- package/database/actions/readResolution.ts +125 -0
- package/database/actions/remove.ts +94 -0
- package/database/actions/replication.ts +477 -0
- package/database/actions/upload.ts +186 -0
- package/database/actions/upsert.ts +56 -0
- package/database/actions/write.ts +146 -0
- package/database/authority/ownerKey.ts +80 -0
- package/database/authority/recordAuthority.ts +67 -0
- package/database/authority/userAuthorityRegistry.ts +54 -0
- package/database/client/fetchUserData.ts +61 -0
- package/database/dbActionThunks.ts +1 -0
- package/database/dbSlice.ts +149 -0
- package/database/fileRing.ts +51 -0
- package/database/fileStorage.ts +232 -0
- package/database/fileUrl.ts +34 -0
- package/database/keys.ts +767 -0
- package/database/queryPrefixes.ts +14 -0
- package/database/requests.ts +443 -0
- package/database/runtimeServerContext.ts +54 -0
- package/database/tenantPlacement.ts +57 -0
- package/database/userPreferenceRegister.ts +131 -0
- package/database/utils/ulid.ts +1 -0
- package/dialogAttachmentCleanup.ts +151 -0
- package/dialogCommands.ts +163 -61
- package/dialogInternalCommandEntries.ts +1 -1
- package/docCreateCommands.ts +345 -0
- package/docInternalCommandEntries.ts +11 -0
- package/docPageHelpers.ts +80 -0
- package/docSkillHelpers.ts +67 -0
- package/docSpaceHelpers.ts +46 -0
- package/integrations/anthropic/anthropicModels.ts +47 -0
- package/integrations/deepseek/models.ts +37 -0
- package/integrations/google/models.ts +341 -0
- package/integrations/openai/chatCompletionStreamMode.ts +10 -0
- package/integrations/openai/filterAndCleanMessages.ts +284 -0
- package/integrations/openai/flexTier.ts +102 -0
- package/integrations/openai/generateOpenAIRequestBody.ts +271 -0
- package/integrations/openai/generateResponseRequestBody.ts +77 -0
- package/integrations/openai/models.ts +271 -0
- package/integrations/openai/providerBodyCompatibility.ts +23 -0
- package/integrations/openai/responsesHelpers.ts +297 -0
- package/integrations/x-reader/types.ts +109 -0
- package/integrations/xai/models.ts +13 -0
- package/integrations/xhs-reader/redaction.ts +68 -0
- package/integrations/xhs-reader/types.ts +277 -0
- package/integrations/xhs-reader/url.ts +80 -0
- package/internalCommandEntries.ts +4 -0
- package/machineCommands.ts +18 -34
- package/memoryCommands.ts +253 -0
- package/memoryInternalCommandEntries.ts +9 -0
- package/package.json +454 -9
- package/qoderUsageProbe.ts +252 -0
- package/render/contentIcon/types.ts +167 -0
- package/render/page/createPageAction.ts +183 -0
- package/render/page/docSlice.ts +495 -0
- package/render/page/types.ts +19 -0
- package/render/table/createTableAction.ts +274 -0
- package/render/table/deleteTableAction.ts +193 -0
- package/render/table/fetchAndCacheTableRows.ts +174 -0
- package/render/table/tableSlice.ts +1167 -0
- package/render/table/toolValueUtils.ts +363 -0
- package/render/table/types.ts +262 -0
- package/render/table/utils/tableSerialization.ts +50 -0
- package/scriptCommandEntries.ts +0 -3
- package/secretScan.ts +31 -0
- package/server/handlers/agentRun/types.ts +105 -0
- package/share/action.ts +225 -0
- package/share/helpers.ts +302 -0
- package/share/keys.ts +243 -0
- package/share/publicSharePolicy.ts +27 -0
- package/share/shareSlice.ts +53 -0
- package/share/types.ts +100 -0
- package/tableCommands.ts +248 -13
- package/workflowInternalCommandEntries.ts +2 -3
- package/ai/tools/agent/taskRunTool.ts +0 -112
- package/cli/client/taskRunPrompt.ts +0 -27
- package/cli/taskRunCommand.ts +0 -265
- package/client/taskRunPrompt.ts +0 -27
- package/taskRunCommand.ts +0 -265
|
@@ -0,0 +1,430 @@
|
|
|
1
|
+
// 文件路径: packages/chat/dialog/actions/updateDialogSummaryAction.ts
|
|
2
|
+
|
|
3
|
+
import type { RootState } from "../../../app/store";
|
|
4
|
+
import { runLlm } from "../../../ai/agent/agentSlice";
|
|
5
|
+
import {
|
|
6
|
+
buildBuiltinSummaryContent,
|
|
7
|
+
BUILTIN_SUMMARY_LLM_CONFIG,
|
|
8
|
+
} from "./builtinDialogLlm";
|
|
9
|
+
import { patch, selectById } from "../../../database/dbSlice";
|
|
10
|
+
// import { selectAllMsgs, selectMsgsByDialogId } from "../../messages/messageSlice"; // CIRCULAR
|
|
11
|
+
import { serializeMessageContent } from "../../messages/messageContent";
|
|
12
|
+
import { DialogConfig, Agent } from "../../../app/types";
|
|
13
|
+
import { selectContextRetention } from "../../../app/settings/settingSlice";
|
|
14
|
+
import { getModelContextWindow } from "../../../ai/llm/getModelContextWindow";
|
|
15
|
+
import { estimateTokenCount } from "../../../ai/context/tokenUtils";
|
|
16
|
+
import { extractCustomId } from "../../../core/prefix";
|
|
17
|
+
import type { Message } from "../../messages/types";
|
|
18
|
+
import {
|
|
19
|
+
ConversationLoad,
|
|
20
|
+
planContextUsage,
|
|
21
|
+
} from "../../../ai/context/retention";
|
|
22
|
+
|
|
23
|
+
// --- 常量 ---
|
|
24
|
+
const MIN_COMPRESS_COUNT = 5; // 至少压缩5条以上才有意义
|
|
25
|
+
const MIN_PROACTIVE_MESSAGE_COUNT = 6;
|
|
26
|
+
const MIN_PROACTIVE_TOKENS = 1800;
|
|
27
|
+
const PROACTIVE_SUMMARY_MAX_INPUT_TOKENS = 12000;
|
|
28
|
+
const ACTIVE_SUMMARY_TAIL_KEEP_COUNT = 2;
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
// --- 辅助函数 ---
|
|
32
|
+
|
|
33
|
+
const getMessageTokenCount = (msg: any): number => {
|
|
34
|
+
if (msg.usage?.completion_tokens) {
|
|
35
|
+
return msg.usage.completion_tokens;
|
|
36
|
+
}
|
|
37
|
+
const content = serializeMessageContent(msg.content) || "";
|
|
38
|
+
return estimateTokenCount(content);
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
const getMessagesForDialogFromState = (
|
|
42
|
+
state: RootState,
|
|
43
|
+
dialogId: string
|
|
44
|
+
): Message[] => {
|
|
45
|
+
const msgsState = state.message.dialogStateById[dialogId]?.msgs;
|
|
46
|
+
if (!msgsState || !msgsState.ids) return [];
|
|
47
|
+
|
|
48
|
+
return (msgsState.ids as string[])
|
|
49
|
+
.map(id => msgsState.entities[id])
|
|
50
|
+
.filter(Boolean) as Message[];
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
const formatMessagesForSummary = (msgs: Message[]): string =>
|
|
54
|
+
msgs
|
|
55
|
+
.map(m => {
|
|
56
|
+
const content = serializeMessageContent(m.content) || "[非文本内容]";
|
|
57
|
+
return `${m.role}: ${content}`;
|
|
58
|
+
})
|
|
59
|
+
.join("\n");
|
|
60
|
+
|
|
61
|
+
const findMessageIndexById = (msgs: Message[], id?: string): number =>
|
|
62
|
+
id ? msgs.findIndex(m => m.id === id) : -1;
|
|
63
|
+
|
|
64
|
+
const hasOpenEndedToolCall = (msg: Message | undefined): boolean =>
|
|
65
|
+
!!msg && Array.isArray((msg as any).tool_calls) && (msg as any).tool_calls.length > 0;
|
|
66
|
+
|
|
67
|
+
const truncateMessagesForSummary = (msgs: Message[]): Message[] => {
|
|
68
|
+
let total = 0;
|
|
69
|
+
let start = msgs.length;
|
|
70
|
+
|
|
71
|
+
for (let i = msgs.length - 1; i >= 0; i--) {
|
|
72
|
+
const t = getMessageTokenCount(msgs[i]);
|
|
73
|
+
if (total + t > PROACTIVE_SUMMARY_MAX_INPUT_TOKENS) break;
|
|
74
|
+
total += t;
|
|
75
|
+
start = i;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
return msgs.slice(start);
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
const buildPreviousSummaryWithProactive = (
|
|
82
|
+
dialogConfig: DialogConfig,
|
|
83
|
+
shouldConsumeProactive: boolean
|
|
84
|
+
): string => {
|
|
85
|
+
const base = dialogConfig.summary || "";
|
|
86
|
+
const proactive = shouldConsumeProactive ? dialogConfig.proactiveSummary || "" : "";
|
|
87
|
+
|
|
88
|
+
if (!base && !proactive) return "";
|
|
89
|
+
if (!proactive) return base;
|
|
90
|
+
if (!base) return `【主动工作摘要】\n${proactive}`;
|
|
91
|
+
|
|
92
|
+
return `${base}\n\n【主动工作摘要】\n${proactive}`;
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
const isActiveSummaryWorthDoing = (
|
|
96
|
+
pendingTokens: number,
|
|
97
|
+
contextWindow: number
|
|
98
|
+
): boolean => {
|
|
99
|
+
const minTokens = Math.min(
|
|
100
|
+
40_000,
|
|
101
|
+
Math.max(10_000, Math.floor(contextWindow * 0.05))
|
|
102
|
+
);
|
|
103
|
+
return pendingTokens >= minTokens;
|
|
104
|
+
};
|
|
105
|
+
|
|
106
|
+
// --- Action ---
|
|
107
|
+
const classifyConversationLoad = (msgs: Message[]): ConversationLoad => {
|
|
108
|
+
const N = 20;
|
|
109
|
+
if (!Array.isArray(msgs) || msgs.length === 0) return "light";
|
|
110
|
+
|
|
111
|
+
const tail = msgs.slice(-N);
|
|
112
|
+
const tokenSamples = tail.map(getMessageTokenCount);
|
|
113
|
+
if (tokenSamples.length === 0) return "light";
|
|
114
|
+
|
|
115
|
+
const sum = tokenSamples.reduce((acc, v) => acc + v, 0);
|
|
116
|
+
const avg = sum / tokenSamples.length;
|
|
117
|
+
const sorted = [...tokenSamples].sort((a, b) => a - b);
|
|
118
|
+
const p95 = sorted[Math.floor((sorted.length - 1) * 0.95)];
|
|
119
|
+
|
|
120
|
+
if (p95 < 200 && avg < 120) {
|
|
121
|
+
return "light";
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
if (p95 > 2000 || avg > 1200) {
|
|
125
|
+
return "heavy";
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
return "medium";
|
|
129
|
+
};
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
const summarizingDialogs = new Set<string>();
|
|
133
|
+
|
|
134
|
+
export const updateDialogSummaryAction = async (
|
|
135
|
+
args: {
|
|
136
|
+
dialogKey: string;
|
|
137
|
+
preFetchedMessages?: Message[];
|
|
138
|
+
force?: boolean;
|
|
139
|
+
reason?: "task_completed" | "context_budget";
|
|
140
|
+
},
|
|
141
|
+
thunkApi: any
|
|
142
|
+
) => {
|
|
143
|
+
const { dialogKey, preFetchedMessages, force = false } = args;
|
|
144
|
+
|
|
145
|
+
// 0. 并发锁:避免同一个 Dialog 同时进行多个摘要任务
|
|
146
|
+
if (summarizingDialogs.has(dialogKey)) return;
|
|
147
|
+
summarizingDialogs.add(dialogKey);
|
|
148
|
+
|
|
149
|
+
const { dispatch, getState } = thunkApi;
|
|
150
|
+
|
|
151
|
+
try {
|
|
152
|
+
const state = getState() as RootState;
|
|
153
|
+
const dialogId = extractCustomId(dialogKey);
|
|
154
|
+
|
|
155
|
+
const dialogConfig = selectById(state, dialogKey) as DialogConfig;
|
|
156
|
+
if (!dialogConfig) return;
|
|
157
|
+
|
|
158
|
+
// 1. 获取配置
|
|
159
|
+
const retention = selectContextRetention(state);
|
|
160
|
+
|
|
161
|
+
let contextWindow = 128000;
|
|
162
|
+
if (dialogConfig.cybots && dialogConfig.cybots.length > 0) {
|
|
163
|
+
const agentId = dialogConfig.cybots[0];
|
|
164
|
+
const agent = selectById(state, agentId) as Agent;
|
|
165
|
+
if (agent?.model) {
|
|
166
|
+
contextWindow = getModelContextWindow(agent.model);
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
// 2. 获取消息
|
|
171
|
+
const allMsgs =
|
|
172
|
+
preFetchedMessages || getMessagesForDialogFromState(state, dialogId);
|
|
173
|
+
|
|
174
|
+
|
|
175
|
+
|
|
176
|
+
|
|
177
|
+
// 3. 找到最后一次压缩的位置
|
|
178
|
+
const lastSummarizedId = dialogConfig.summarizedBeforeId;
|
|
179
|
+
let startIndex = 0;
|
|
180
|
+
if (lastSummarizedId) {
|
|
181
|
+
const found = allMsgs.findIndex(m => m.id === lastSummarizedId);
|
|
182
|
+
if (found !== -1) {
|
|
183
|
+
startIndex = found + 1;
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
// 待处理的消息(尚未被压缩进 summary)
|
|
188
|
+
const pendingMsgs = allMsgs.slice(startIndex);
|
|
189
|
+
if (pendingMsgs.length === 0) return;
|
|
190
|
+
|
|
191
|
+
// 4. 计算当前总开销 = 已有 Summary + 待处理消息
|
|
192
|
+
const summaryTokens = estimateTokenCount(dialogConfig.summary || "");
|
|
193
|
+
const pendingTokens = pendingMsgs.reduce((sum, msg) => sum + getMessageTokenCount(msg), 0);
|
|
194
|
+
const totalUsed = summaryTokens + pendingTokens;
|
|
195
|
+
|
|
196
|
+
// 5. 基于模型窗口 / slider / 近期负载,规划历史预算
|
|
197
|
+
const adjustedSummaryTokens = Math.max(summaryTokens, 1000);
|
|
198
|
+
const recentLoad = classifyConversationLoad(pendingMsgs);
|
|
199
|
+
|
|
200
|
+
const { historyBudget, rawMessageBudget } = planContextUsage({
|
|
201
|
+
contextWindow,
|
|
202
|
+
retentionSlider: retention,
|
|
203
|
+
summaryTokens: adjustedSummaryTokens,
|
|
204
|
+
recentLoad,
|
|
205
|
+
});
|
|
206
|
+
|
|
207
|
+
const shouldRunActiveSummary =
|
|
208
|
+
force &&
|
|
209
|
+
!hasOpenEndedToolCall(pendingMsgs[pendingMsgs.length - 1]) &&
|
|
210
|
+
isActiveSummaryWorthDoing(pendingTokens, contextWindow);
|
|
211
|
+
|
|
212
|
+
// 历史 + 待处理总开销未达到预算,且没有明确的主动归档信号,不触发压缩
|
|
213
|
+
if (totalUsed < historyBudget && !shouldRunActiveSummary) {
|
|
214
|
+
return;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
// 6. 需要压缩:决定把哪些消息压进去
|
|
218
|
+
// 目标:压缩后,summary + 保留的原始消息 ≈ historyBudget,
|
|
219
|
+
// 且尽量保留最近的若干条消息。
|
|
220
|
+
|
|
221
|
+
let tokensToKeep = 0;
|
|
222
|
+
let keepCount = 0;
|
|
223
|
+
|
|
224
|
+
// 从后往前数,保留最近的消息直到填满 rawMessageBudget
|
|
225
|
+
for (let i = pendingMsgs.length - 1; i >= 0; i--) {
|
|
226
|
+
const t = getMessageTokenCount(pendingMsgs[i]);
|
|
227
|
+
if (tokensToKeep + t > rawMessageBudget) break;
|
|
228
|
+
tokensToKeep += t;
|
|
229
|
+
keepCount++;
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
// 主动归档时保留最后两条原文,避免刚给用户的结论立刻被折叠进 summary。
|
|
233
|
+
let compressCount =
|
|
234
|
+
shouldRunActiveSummary && totalUsed < historyBudget
|
|
235
|
+
? Math.max(0, pendingMsgs.length - ACTIVE_SUMMARY_TAIL_KEEP_COUNT)
|
|
236
|
+
: pendingMsgs.length - keepCount;
|
|
237
|
+
|
|
238
|
+
// Guard: Prevent breaking tool chains or compressing open-ended tool calls
|
|
239
|
+
// OpenAI requires: Assistant(tool_calls) -> Tool(output) must be contiguous.
|
|
240
|
+
|
|
241
|
+
// 1. Ensure we do not cut immediately before a 'tool' message.
|
|
242
|
+
// If pendingMsgs[compressCount] (the first kept message) is 'tool',
|
|
243
|
+
// it means we are summarizing its parent 'assistant'. This is illegal.
|
|
244
|
+
while (compressCount > 0 && compressCount < pendingMsgs.length && pendingMsgs[compressCount].role === 'tool') {
|
|
245
|
+
compressCount--;
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
// 2. Ensure the last compressed message is not an assistant with tool_calls that needs a future tool output
|
|
249
|
+
// (This is implicitly covered by #1 if tool output exists, but if tool output hasn't arrived yet,
|
|
250
|
+
// we must check the last message itself).
|
|
251
|
+
// Actually, if tool output hasn't arrived, 'updateDialogSummaryAction' is usually called after 'messageStreamEnd'.
|
|
252
|
+
// If the last message is Assistant(tool_calls), the NEXT message (not yet in list) will be Tool.
|
|
253
|
+
// So we should NOT compress the last message if it has tool_calls.
|
|
254
|
+
if (compressCount > 0) {
|
|
255
|
+
const lastCompressed = pendingMsgs[compressCount - 1];
|
|
256
|
+
const hasToolCalls = Array.isArray((lastCompressed as any).tool_calls) && (lastCompressed as any).tool_calls.length > 0;
|
|
257
|
+
if (hasToolCalls) {
|
|
258
|
+
// If we are chopping off the end of the conversation, and it ends with a tool call,
|
|
259
|
+
// we must preserve it until tool output arrives.
|
|
260
|
+
// Actually, just to be safe, never compress an active tool call.
|
|
261
|
+
compressCount--;
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
|
|
266
|
+
// 2. 可压缩的消息太少,不值得压缩
|
|
267
|
+
if (compressCount < MIN_COMPRESS_COUNT) {
|
|
268
|
+
return;
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
const msgsToCompress = pendingMsgs.slice(0, compressCount);
|
|
272
|
+
|
|
273
|
+
|
|
274
|
+
// 最后一条被压缩的消息
|
|
275
|
+
const newSummarizedBeforeId = msgsToCompress[msgsToCompress.length - 1].id;
|
|
276
|
+
|
|
277
|
+
// 7. 提取引用 Keys (pageKey/dialogKey) 并保存
|
|
278
|
+
// 这一步非常重要:因为这些消息即将从 context 中消失,必须把 key 留下来
|
|
279
|
+
// 我们复用 addReferenceKeysAction 的逻辑,或者通过 content 提取
|
|
280
|
+
// 这里直接提取然后 patch dialog
|
|
281
|
+
const extractedKeys = new Set(dialogConfig.referenceKeys || []);
|
|
282
|
+
msgsToCompress.forEach(msg => {
|
|
283
|
+
// 简单遍历 content
|
|
284
|
+
if (Array.isArray(msg.content)) {
|
|
285
|
+
msg.content.forEach((p: any) => {
|
|
286
|
+
if (p?.pageKey) extractedKeys.add(p.pageKey);
|
|
287
|
+
if (p?.dialogKey) extractedKeys.add(p.dialogKey);
|
|
288
|
+
});
|
|
289
|
+
} else if (msg.content && typeof msg.content === 'object') {
|
|
290
|
+
const c: any = msg.content;
|
|
291
|
+
if (c.pageKey) extractedKeys.add(c.pageKey);
|
|
292
|
+
if (c.dialogKey) extractedKeys.add(c.dialogKey);
|
|
293
|
+
}
|
|
294
|
+
});
|
|
295
|
+
|
|
296
|
+
// 8. 生成新 Summary (结构化记忆升级版)
|
|
297
|
+
// 目标:将线性摘要升级为 "事实 (Facts) + 剧情 (Narrative)" 双轨记忆。
|
|
298
|
+
// 这能显著提升 Agent 的 "智商",防止关键配置在长对话中被遗忘。
|
|
299
|
+
|
|
300
|
+
const proactiveBeforeIndex = findMessageIndexById(
|
|
301
|
+
allMsgs,
|
|
302
|
+
dialogConfig.proactiveSummaryBeforeId
|
|
303
|
+
);
|
|
304
|
+
const shouldConsumeProactive =
|
|
305
|
+
!!dialogConfig.proactiveSummary &&
|
|
306
|
+
proactiveBeforeIndex !== -1 &&
|
|
307
|
+
proactiveBeforeIndex < startIndex + compressCount;
|
|
308
|
+
|
|
309
|
+
const previousSummary = buildPreviousSummaryWithProactive(
|
|
310
|
+
dialogConfig,
|
|
311
|
+
shouldConsumeProactive
|
|
312
|
+
);
|
|
313
|
+
const messagesText = formatMessagesForSummary(msgsToCompress);
|
|
314
|
+
|
|
315
|
+
const promptContent = buildBuiltinSummaryContent(
|
|
316
|
+
previousSummary,
|
|
317
|
+
messagesText
|
|
318
|
+
);
|
|
319
|
+
|
|
320
|
+
try {
|
|
321
|
+
// 调用内置 Summary LLM
|
|
322
|
+
const newSummary = await dispatch(
|
|
323
|
+
runLlm({
|
|
324
|
+
llmConfig: BUILTIN_SUMMARY_LLM_CONFIG,
|
|
325
|
+
content: promptContent,
|
|
326
|
+
billingDialogKey: dialogKey,
|
|
327
|
+
})
|
|
328
|
+
).unwrap();
|
|
329
|
+
|
|
330
|
+
if (newSummary && typeof newSummary === "string" && newSummary.trim()) {
|
|
331
|
+
const currentCount = dialogConfig.compressionCount || 0;
|
|
332
|
+
|
|
333
|
+
await dispatch(
|
|
334
|
+
patch({
|
|
335
|
+
dbKey: dialogKey,
|
|
336
|
+
changes: {
|
|
337
|
+
summary: newSummary.trim(),
|
|
338
|
+
summarizedBeforeId: newSummarizedBeforeId,
|
|
339
|
+
referenceKeys: Array.from(extractedKeys),
|
|
340
|
+
compressionCount: currentCount + 1,
|
|
341
|
+
summaryPending: false, // Explicitly clear pending flag
|
|
342
|
+
...(shouldConsumeProactive
|
|
343
|
+
? {
|
|
344
|
+
proactiveSummary: "",
|
|
345
|
+
proactiveSummaryBeforeId: undefined,
|
|
346
|
+
}
|
|
347
|
+
: {}),
|
|
348
|
+
},
|
|
349
|
+
})
|
|
350
|
+
).unwrap();
|
|
351
|
+
|
|
352
|
+
console.log(`[ContextCompression] Compressed ${compressCount} messages. New summary len: ${newSummary.length}`);
|
|
353
|
+
}
|
|
354
|
+
} catch (err) {
|
|
355
|
+
console.error("[ContextCompression] Failed:", err);
|
|
356
|
+
}
|
|
357
|
+
} finally {
|
|
358
|
+
summarizingDialogs.delete(dialogKey);
|
|
359
|
+
}
|
|
360
|
+
};
|
|
361
|
+
|
|
362
|
+
export const updateDialogProactiveSummaryAction = async (
|
|
363
|
+
args: { dialogKey: string; preFetchedMessages?: Message[] },
|
|
364
|
+
thunkApi: any
|
|
365
|
+
) => {
|
|
366
|
+
const { dialogKey, preFetchedMessages } = args;
|
|
367
|
+
if (summarizingDialogs.has(dialogKey)) return;
|
|
368
|
+
if (summarizingDialogs.has(`proactive:${dialogKey}`)) return;
|
|
369
|
+
summarizingDialogs.add(`proactive:${dialogKey}`);
|
|
370
|
+
|
|
371
|
+
const { dispatch, getState } = thunkApi;
|
|
372
|
+
|
|
373
|
+
try {
|
|
374
|
+
const state = getState() as RootState;
|
|
375
|
+
const dialogId = extractCustomId(dialogKey);
|
|
376
|
+
const dialogConfig = selectById(state, dialogKey) as DialogConfig;
|
|
377
|
+
if (!dialogConfig) return;
|
|
378
|
+
|
|
379
|
+
const allMsgs = preFetchedMessages || getMessagesForDialogFromState(state, dialogId);
|
|
380
|
+
if (!allMsgs.length) return;
|
|
381
|
+
|
|
382
|
+
const hardSummaryIndex = findMessageIndexById(allMsgs, dialogConfig.summarizedBeforeId);
|
|
383
|
+
const proactiveIndex = findMessageIndexById(allMsgs, dialogConfig.proactiveSummaryBeforeId);
|
|
384
|
+
const startIndex = Math.max(hardSummaryIndex, proactiveIndex) + 1;
|
|
385
|
+
const pendingMsgs = allMsgs.slice(startIndex);
|
|
386
|
+
if (pendingMsgs.length < MIN_PROACTIVE_MESSAGE_COUNT) return;
|
|
387
|
+
|
|
388
|
+
if (hasOpenEndedToolCall(pendingMsgs[pendingMsgs.length - 1])) return;
|
|
389
|
+
|
|
390
|
+
const pendingTokens = pendingMsgs.reduce((sum, msg) => sum + getMessageTokenCount(msg), 0);
|
|
391
|
+
const hasToolOutput = pendingMsgs.some(msg => msg.role === "tool");
|
|
392
|
+
if (!hasToolOutput && pendingTokens < MIN_PROACTIVE_TOKENS) return;
|
|
393
|
+
|
|
394
|
+
const msgsToSummarize = truncateMessagesForSummary(pendingMsgs);
|
|
395
|
+
if (msgsToSummarize.length < MIN_PROACTIVE_MESSAGE_COUNT && !hasToolOutput) return;
|
|
396
|
+
|
|
397
|
+
const newProactiveBeforeId = pendingMsgs[pendingMsgs.length - 1].id;
|
|
398
|
+
const previousSummary = dialogConfig.proactiveSummary || "";
|
|
399
|
+
const messagesText = formatMessagesForSummary(msgsToSummarize);
|
|
400
|
+
const promptContent = buildBuiltinSummaryContent(previousSummary, messagesText);
|
|
401
|
+
|
|
402
|
+
try {
|
|
403
|
+
const newSummary = await dispatch(
|
|
404
|
+
runLlm({
|
|
405
|
+
llmConfig: BUILTIN_SUMMARY_LLM_CONFIG,
|
|
406
|
+
content: promptContent,
|
|
407
|
+
billingDialogKey: dialogKey,
|
|
408
|
+
})
|
|
409
|
+
).unwrap();
|
|
410
|
+
|
|
411
|
+
if (newSummary && typeof newSummary === "string" && newSummary.trim()) {
|
|
412
|
+
await dispatch(
|
|
413
|
+
patch({
|
|
414
|
+
dbKey: dialogKey,
|
|
415
|
+
changes: {
|
|
416
|
+
proactiveSummary: newSummary.trim(),
|
|
417
|
+
proactiveSummaryBeforeId: newProactiveBeforeId,
|
|
418
|
+
},
|
|
419
|
+
})
|
|
420
|
+
).unwrap();
|
|
421
|
+
|
|
422
|
+
console.log(`[ContextCompression] Proactive summary updated. len: ${newSummary.length}`);
|
|
423
|
+
}
|
|
424
|
+
} catch (err) {
|
|
425
|
+
console.error("[ContextCompression] Proactive summary failed:", err);
|
|
426
|
+
}
|
|
427
|
+
} finally {
|
|
428
|
+
summarizingDialogs.delete(`proactive:${dialogKey}`);
|
|
429
|
+
}
|
|
430
|
+
};
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
// chat/dialogs/updateDialogTitleAction.ts
|
|
2
|
+
|
|
3
|
+
import { runLlm } from "../../../ai/agent/agentSlice";
|
|
4
|
+
import type { RootState } from "../../../app/store";
|
|
5
|
+
import {
|
|
6
|
+
updateContentTitle,
|
|
7
|
+
} from "../../../create/space/spaceSlice";
|
|
8
|
+
import { normalizeSpaceId } from "../../../create/space/spaceKeys";
|
|
9
|
+
import { patch, selectById } from "../../../database/dbSlice";
|
|
10
|
+
import { extractCustomId } from "../../../core/prefix";
|
|
11
|
+
import { format } from "date-fns";
|
|
12
|
+
import { selectAllMsgs } from "../../messages/messageSlice";
|
|
13
|
+
import { serializeMessageContent } from "../../messages/messageContent";
|
|
14
|
+
import {
|
|
15
|
+
BUILTIN_TITLE_LLM_CONFIG,
|
|
16
|
+
} from "./builtinDialogLlm";
|
|
17
|
+
import {
|
|
18
|
+
shouldUpdateTitle,
|
|
19
|
+
} from "./updateDialogTitlePolicy";
|
|
20
|
+
import {
|
|
21
|
+
buildDialogFallbackTitleFromMessages,
|
|
22
|
+
resolveDialogTitle,
|
|
23
|
+
} from "../dialogTitle";
|
|
24
|
+
|
|
25
|
+
// --- 常量 ---
|
|
26
|
+
const MAX_MESSAGES_FOR_CONTEXT = 20;
|
|
27
|
+
|
|
28
|
+
const isAssistantToolStub = (msg: any) =>
|
|
29
|
+
msg?.role === "assistant" &&
|
|
30
|
+
(msg.content == null ||
|
|
31
|
+
(typeof msg.content === "string" && msg.content.trim().length === 0) ||
|
|
32
|
+
(Array.isArray(msg.content) && msg.content.length === 0)) &&
|
|
33
|
+
Array.isArray(msg?.tool_calls) &&
|
|
34
|
+
msg.tool_calls.length > 0;
|
|
35
|
+
|
|
36
|
+
const dedupeById = <T extends { id?: string }>(messages: T[]) => {
|
|
37
|
+
const seen = new Set<string>();
|
|
38
|
+
return messages.filter((message) => {
|
|
39
|
+
const id = typeof message.id === "string" ? message.id : "";
|
|
40
|
+
if (!id || seen.has(id)) return false;
|
|
41
|
+
seen.add(id);
|
|
42
|
+
return true;
|
|
43
|
+
});
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
const getMessageContextForTitle = (
|
|
47
|
+
state: RootState,
|
|
48
|
+
dialogKey: string,
|
|
49
|
+
selectAllMessages: typeof selectAllMsgs = selectAllMsgs
|
|
50
|
+
) => {
|
|
51
|
+
const allMsgs = selectAllMessages(state, extractCustomId(dialogKey));
|
|
52
|
+
|
|
53
|
+
const flattened = Array.isArray(allMsgs)
|
|
54
|
+
? typeof (allMsgs as any).flat === "function"
|
|
55
|
+
? (allMsgs as any).flat()
|
|
56
|
+
: (allMsgs as any).reduce(
|
|
57
|
+
(acc: any[], cur: any) => acc.concat(cur),
|
|
58
|
+
[]
|
|
59
|
+
)
|
|
60
|
+
: [];
|
|
61
|
+
|
|
62
|
+
const normalized = flattened
|
|
63
|
+
.filter((msg: any) => msg?.role !== "tool" && !isAssistantToolStub(msg))
|
|
64
|
+
.map((msg: any) => {
|
|
65
|
+
const textContent = serializeMessageContent(msg?.content, "这里是一个图片");
|
|
66
|
+
if (!textContent) return null;
|
|
67
|
+
|
|
68
|
+
return {
|
|
69
|
+
...msg,
|
|
70
|
+
content: textContent as string, // 之后只会用到 role + content
|
|
71
|
+
};
|
|
72
|
+
})
|
|
73
|
+
.filter(Boolean) as Array<{ role: any; content: string }>;
|
|
74
|
+
|
|
75
|
+
const earlyUserTurns = normalized.filter((msg) => msg.role === "user").slice(0, 3);
|
|
76
|
+
const recentTurns = normalized.slice(-12);
|
|
77
|
+
|
|
78
|
+
return dedupeById([...earlyUserTurns, ...recentTurns]).slice(
|
|
79
|
+
0,
|
|
80
|
+
MAX_MESSAGES_FOR_CONTEXT
|
|
81
|
+
);
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
type UpdateDialogTitleDeps = {
|
|
85
|
+
runLlmAction?: typeof runLlm;
|
|
86
|
+
patchAction?: typeof patch;
|
|
87
|
+
selectDialogById?: typeof selectById;
|
|
88
|
+
selectAllMessages?: typeof selectAllMsgs;
|
|
89
|
+
updateSpaceContentTitle?: typeof updateContentTitle;
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
// --- 异步 Thunk Action ---
|
|
93
|
+
export const updateDialogTitleActionWithDeps = async (
|
|
94
|
+
args,
|
|
95
|
+
thunkApi,
|
|
96
|
+
deps: UpdateDialogTitleDeps = {}
|
|
97
|
+
) => {
|
|
98
|
+
const {
|
|
99
|
+
runLlmAction = runLlm,
|
|
100
|
+
patchAction = patch,
|
|
101
|
+
selectDialogById = selectById,
|
|
102
|
+
selectAllMessages = selectAllMsgs,
|
|
103
|
+
updateSpaceContentTitle = updateContentTitle,
|
|
104
|
+
} = deps;
|
|
105
|
+
const { dialogKey } = args;
|
|
106
|
+
const { dispatch, getState } = thunkApi;
|
|
107
|
+
const state = getState() as RootState;
|
|
108
|
+
|
|
109
|
+
const dialogConfig = selectDialogById(state, dialogKey);
|
|
110
|
+
if (
|
|
111
|
+
!dialogConfig ||
|
|
112
|
+
!shouldUpdateTitle(dialogConfig.createdAt, dialogConfig.updatedAt)
|
|
113
|
+
) {
|
|
114
|
+
return dialogConfig;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
const messageContext = getMessageContextForTitle(state, dialogKey, selectAllMessages);
|
|
118
|
+
if (messageContext.length === 0) {
|
|
119
|
+
return dialogConfig;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
const content = JSON.stringify(
|
|
123
|
+
messageContext.map((msg) => ({ role: msg.role, content: msg.content }))
|
|
124
|
+
);
|
|
125
|
+
|
|
126
|
+
const generatedTitle = await dispatch(
|
|
127
|
+
runLlmAction({
|
|
128
|
+
llmConfig: BUILTIN_TITLE_LLM_CONFIG,
|
|
129
|
+
content,
|
|
130
|
+
billingDialogKey: dialogKey,
|
|
131
|
+
})
|
|
132
|
+
).unwrap();
|
|
133
|
+
|
|
134
|
+
const fallbackTitle =
|
|
135
|
+
buildDialogFallbackTitleFromMessages(messageContext) ||
|
|
136
|
+
`Conversation on ${format(new Date(), "MMM d")}`;
|
|
137
|
+
const title = resolveDialogTitle(generatedTitle, fallbackTitle);
|
|
138
|
+
|
|
139
|
+
const spaceId =
|
|
140
|
+
dialogConfig?.spaceId && normalizeSpaceId(dialogConfig.spaceId);
|
|
141
|
+
if (spaceId) {
|
|
142
|
+
dispatch(updateSpaceContentTitle({ spaceId, contentKey: dialogKey, title }));
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
return await dispatch(
|
|
146
|
+
patchAction({ dbKey: dialogKey, changes: { title } })
|
|
147
|
+
).unwrap();
|
|
148
|
+
};
|
|
149
|
+
|
|
150
|
+
export const updateDialogTitleAction = async (args, thunkApi) =>
|
|
151
|
+
updateDialogTitleActionWithDeps(args, thunkApi);
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { differenceInMinutes } from "date-fns";
|
|
2
|
+
|
|
3
|
+
export const TITLE_UPDATE_INTERVAL_MINUTES = 30;
|
|
4
|
+
|
|
5
|
+
const FORCE_UPDATE_FOR_TEST = false;
|
|
6
|
+
|
|
7
|
+
export const shouldUpdateTitle = (
|
|
8
|
+
createdAt?: string,
|
|
9
|
+
lastUpdatedAt?: string,
|
|
10
|
+
now = new Date()
|
|
11
|
+
): boolean => {
|
|
12
|
+
if (FORCE_UPDATE_FOR_TEST) return true;
|
|
13
|
+
|
|
14
|
+
const lastUpdate = lastUpdatedAt ? new Date(lastUpdatedAt) : null;
|
|
15
|
+
const creation = createdAt ? new Date(createdAt) : null;
|
|
16
|
+
|
|
17
|
+
if (
|
|
18
|
+
!lastUpdate ||
|
|
19
|
+
!creation ||
|
|
20
|
+
Number.isNaN(lastUpdate.getTime()) ||
|
|
21
|
+
Number.isNaN(creation.getTime())
|
|
22
|
+
) {
|
|
23
|
+
return true;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
return (
|
|
27
|
+
differenceInMinutes(now, creation) <= TITLE_UPDATE_INTERVAL_MINUTES ||
|
|
28
|
+
differenceInMinutes(now, lastUpdate) >= TITLE_UPDATE_INTERVAL_MINUTES
|
|
29
|
+
);
|
|
30
|
+
};
|