nolo-cli 0.1.40 → 0.1.41
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/agent-runtime/hybridRecordStore.ts +5 -3
- 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 +585 -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 +65 -6
- 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 +2001 -0
- package/ai/agent/streamAgentChatTurnUtils.ts +860 -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 +1095 -0
- package/ai/chat/accumulateToolCallChunks.ts +126 -0
- package/ai/chat/fetchUtils.ts +143 -0
- package/ai/chat/inlineImageUrlsForCustomProvider.ts +117 -0
- package/ai/chat/parseApiError.ts +67 -0
- package/ai/chat/parseMultilineSSE.ts +95 -0
- package/ai/chat/sendOpenAICompletionsRequest.ts +897 -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 +23 -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 +6 -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/queryShared.ts +161 -0
- package/ai/memory/recentRelationshipRecap.ts +247 -0
- package/ai/memory/remember.ts +167 -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/policy/personalizationDialog.ts +333 -0
- package/ai/policy/runtimePolicy.ts +454 -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 +235 -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 +53 -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 +2308 -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 +23 -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 +627 -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 +315 -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/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 +55 -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/favoriteSlice.ts +690 -0
- package/app/favorite/type.ts +43 -0
- package/app/hooks/deleteDbKey.ts +233 -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 +2174 -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 +114 -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 +73 -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 +18 -0
- package/auth/authSlice.ts +531 -0
- package/auth/client/loginRequest.ts +14 -0
- package/auth/resetAuthScopedClientState.ts +27 -0
- package/auth/routes.ts +178 -0
- package/auth/token.ts +63 -0
- package/auth/types.ts +40 -0
- package/chat/chat.locale.ts +813 -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 +197 -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/dialogSlice.ts +872 -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/fetchAndCacheMessages.ts +119 -0
- package/chat/messages/fetchConvMsgs.ts +51 -0
- package/chat/messages/fileUrl.ts +13 -0
- package/chat/messages/messageAgent.ts +8 -0
- package/chat/messages/messageContent.ts +199 -0
- package/chat/messages/messageContract.ts +88 -0
- package/chat/messages/messageSlice.ts +1367 -0
- package/chat/messages/toolThunks.ts +625 -0
- package/cli/client/agentRun.ts +27 -8
- package/cli/client/localRuntimeAdapter.ts +144 -28
- package/client/agentRun.test.ts +52 -0
- package/client/agentRun.ts +27 -8
- package/client/localRuntimeAdapter.test.ts +437 -6
- package/client/localRuntimeAdapter.ts +144 -28
- 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 +241 -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/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 +274 -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/analyze/profileAnalyzer.ts +205 -0
- package/integrations/xhs-reader/backends/playwrightProfileCollector.ts +624 -0
- package/integrations/xhs-reader/bridge/readXhsProfileWithBridge.ts +227 -0
- package/integrations/xhs-reader/client/xhsWebApiClient.ts +229 -0
- package/integrations/xhs-reader/normalize.ts +390 -0
- package/integrations/xhs-reader/orchestrator.ts +178 -0
- package/integrations/xhs-reader/redaction.ts +68 -0
- package/integrations/xhs-reader/types.ts +244 -0
- package/integrations/xhs-reader/url.ts +57 -0
- package/package.json +430 -4
- package/render/page/createPageAction.ts +183 -0
- package/render/page/docSlice.ts +468 -0
- package/render/page/types.ts +17 -0
- package/render/table/createTableAction.ts +270 -0
- package/render/table/deleteTableAction.ts +193 -0
- package/render/table/fetchAndCacheTableRows.ts +174 -0
- package/render/table/tableSlice.ts +1106 -0
- package/render/table/toolValueUtils.ts +363 -0
- package/render/table/types.ts +252 -0
- package/render/table/utils/tableSerialization.ts +50 -0
- package/server/handlers/agentRun/types.ts +104 -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
|
@@ -0,0 +1,872 @@
|
|
|
1
|
+
// 文件路径: chat/dialog/dialogSlice.ts
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
type PayloadAction,
|
|
5
|
+
asyncThunkCreator,
|
|
6
|
+
buildCreateSlice,
|
|
7
|
+
createSelector,
|
|
8
|
+
} from "@reduxjs/toolkit";
|
|
9
|
+
import type { RootState } from "../../app/store";
|
|
10
|
+
import { getRuntimeServerContext } from "../../database/runtimeServerContext";
|
|
11
|
+
import type { Descendant } from "slate";
|
|
12
|
+
import { resetMsgs, clearAllStreaming } from "../messages/messageSlice";
|
|
13
|
+
import { extractCustomId } from "../../core/prefix";
|
|
14
|
+
import { patch, read, remove, selectById } from "../../database/dbSlice";
|
|
15
|
+
|
|
16
|
+
import type { DialogConfig, DialogGoalState } from "../../app/types";
|
|
17
|
+
import { clearPlan } from "../../ai/agent/planSlice";
|
|
18
|
+
import { clearWorkflow } from "../../ai/workflow/workflowSlice";
|
|
19
|
+
|
|
20
|
+
import { scheduleDeleteReplication } from "../../database/actions/replication";
|
|
21
|
+
|
|
22
|
+
// 外部 Actions
|
|
23
|
+
import { updateTokensAction } from "./actions/updateTokensAction";
|
|
24
|
+
import { cleanupCliSessionForDialog } from "./actions/cleanupCliSession";
|
|
25
|
+
|
|
26
|
+
import { createKey } from "../../database/keys";
|
|
27
|
+
import { mergeDialogTokenStats } from "./dialogTokenStats";
|
|
28
|
+
import { getDialogAgentIds, getPrimaryDialogAgentId } from "./dialogAgents";
|
|
29
|
+
import { buildDialogGoal, getDialogGoalReport } from "./goal";
|
|
30
|
+
|
|
31
|
+
const collectKeys = async (prefix: string, db: any): Promise<string[]> => {
|
|
32
|
+
const keys: string[] = [];
|
|
33
|
+
let iterator = db.iterator({
|
|
34
|
+
gte: prefix,
|
|
35
|
+
lte: prefix + "\uffff",
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
if (iterator && typeof iterator.then === 'function') {
|
|
39
|
+
iterator = await iterator;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
for await (const [key] of iterator) {
|
|
43
|
+
keys.push(key as string);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
return keys;
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
const createSliceWithThunks = buildCreateSlice({
|
|
50
|
+
creators: { asyncThunk: asyncThunkCreator },
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
const runCreateDialogAction = async (args: any, thunkApi: any) => {
|
|
54
|
+
const { createDialogAction } = await import("./actions/createDialogAction");
|
|
55
|
+
return createDialogAction(args, thunkApi);
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
const runCreateScheduledTaskAction = async (args: any, thunkApi: any) => {
|
|
59
|
+
const { createScheduledTaskAction } = await import("./actions/createScheduledTaskAction");
|
|
60
|
+
return createScheduledTaskAction(args, thunkApi);
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
const runUpdateDialogTitleAction = async (args: any, thunkApi: any) => {
|
|
64
|
+
const { updateDialogTitleAction } = await import("./actions/updateDialogTitleAction");
|
|
65
|
+
return updateDialogTitleAction(args, thunkApi);
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
const runAddCybotAction = async (args: any, thunkApi: any) => {
|
|
69
|
+
const { addCybotAction } = await import("./actions/addCybotAction");
|
|
70
|
+
return addCybotAction(args, thunkApi);
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
const runRemoveCybotAction = async (args: any, thunkApi: any) => {
|
|
74
|
+
const { removeCybotAction } = await import("./actions/removeCybotAction");
|
|
75
|
+
return removeCybotAction(args, thunkApi);
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
const runReplacePrimaryCybotAction = async (args: any, thunkApi: any) => {
|
|
79
|
+
const { replacePrimaryCybotAction } = await import("./actions/replacePrimaryCybotAction");
|
|
80
|
+
return replacePrimaryCybotAction(args, thunkApi);
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
const runRemoveDialogAgentAction = async (args: any, thunkApi: any) => {
|
|
84
|
+
const { removeDialogAgentAction } = await import("./actions/removeDialogAgentAction");
|
|
85
|
+
return removeDialogAgentAction(args, thunkApi);
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
const runSetPrimaryDialogAgentAction = async (args: any, thunkApi: any) => {
|
|
89
|
+
const { setPrimaryDialogAgentAction } = await import("./actions/setPrimaryDialogAgentAction");
|
|
90
|
+
return setPrimaryDialogAgentAction(args, thunkApi);
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
const runHandleSendMessageAction = async (args: any, thunkApi: any) => {
|
|
94
|
+
const { handleSendMessageAction } = await import("./actions/handleSendMessageAction");
|
|
95
|
+
return handleSendMessageAction(args, thunkApi);
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
// --- Interfaces ---
|
|
99
|
+
|
|
100
|
+
export interface PendingFile {
|
|
101
|
+
id: string;
|
|
102
|
+
name: string;
|
|
103
|
+
/** Source content key included in the message attachment. */
|
|
104
|
+
pageKey?: string;
|
|
105
|
+
/** Source dialog key included in the message attachment. Prefer sourceDialogKey in new code. */
|
|
106
|
+
dialogKey?: string;
|
|
107
|
+
sourceDialogKey?: string;
|
|
108
|
+
/** Target dialog runtime that should receive this pending attachment. */
|
|
109
|
+
targetDialogKey?: string;
|
|
110
|
+
/** @deprecated Use targetDialogKey. Kept for older pending attachment payloads. */
|
|
111
|
+
runtimeDialogKey?: string;
|
|
112
|
+
type:
|
|
113
|
+
| "excel"
|
|
114
|
+
| "docx"
|
|
115
|
+
| "pdf"
|
|
116
|
+
| "page"
|
|
117
|
+
| "txt"
|
|
118
|
+
| "dialog"
|
|
119
|
+
| "table"
|
|
120
|
+
| "image"
|
|
121
|
+
| "file"
|
|
122
|
+
| "agent"
|
|
123
|
+
| "cybot"
|
|
124
|
+
| "app"
|
|
125
|
+
| "ocr_text";
|
|
126
|
+
groupId?: string;
|
|
127
|
+
ocrText?: string; // ← 新增:OCR 识别结果
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
export interface CreatePagePayload {
|
|
131
|
+
slateData: Descendant[];
|
|
132
|
+
jsonData?: Record<string, any>[];
|
|
133
|
+
title: string;
|
|
134
|
+
type: "excel" | "docx" | "pdf" | "txt" | "table";
|
|
135
|
+
fileId: string;
|
|
136
|
+
size: number;
|
|
137
|
+
groupId?: string;
|
|
138
|
+
dialogKey?: string;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
export interface PendingRawData {
|
|
142
|
+
pageKey: string;
|
|
143
|
+
jsonData: Record<string, any>[];
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
export interface TokenStats {
|
|
147
|
+
inputTokens: number;
|
|
148
|
+
outputTokens: number;
|
|
149
|
+
totalCost: number;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
type LiveTokenUsagePayload = {
|
|
153
|
+
input_tokens: number;
|
|
154
|
+
output_tokens: number;
|
|
155
|
+
cost?: number;
|
|
156
|
+
dialogKey?: string;
|
|
157
|
+
};
|
|
158
|
+
|
|
159
|
+
type CreateDialogGoalPayload = {
|
|
160
|
+
objective: string;
|
|
161
|
+
tokenBudget?: number;
|
|
162
|
+
dialogKey?: string;
|
|
163
|
+
now?: number;
|
|
164
|
+
};
|
|
165
|
+
|
|
166
|
+
type CompleteDialogGoalPayload = {
|
|
167
|
+
dialogKey?: string;
|
|
168
|
+
now?: number;
|
|
169
|
+
};
|
|
170
|
+
|
|
171
|
+
export type LoopStopReason =
|
|
172
|
+
| "done" // 模型主动结束,无工具调用
|
|
173
|
+
| "handoff" // 移交给子 Agent
|
|
174
|
+
| "pending" // 等待用户确认
|
|
175
|
+
| "timeout" // 超时
|
|
176
|
+
| "error"; // 异常
|
|
177
|
+
|
|
178
|
+
interface DialogState {
|
|
179
|
+
currentDialogKey: string | null;
|
|
180
|
+
/**
|
|
181
|
+
* 多对话运行态桶:
|
|
182
|
+
* - 现阶段先服务“网页端切换对话不停止”
|
|
183
|
+
* - token / controller / queue /附件等按 dialogKey 隔离
|
|
184
|
+
* - 未来桌面端若支持多窗口/多 tab,还需要补跨窗口 stop 语义与同步策略
|
|
185
|
+
*/
|
|
186
|
+
dialogRuntimeByKey: Record<string, DialogRuntimeState>;
|
|
187
|
+
isUpdatingMode: boolean;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
interface DialogRuntimeState {
|
|
191
|
+
tokens: TokenStats;
|
|
192
|
+
goal?: DialogGoalState;
|
|
193
|
+
pendingFiles: PendingFile[];
|
|
194
|
+
activeControllers: Record<string, AbortController>;
|
|
195
|
+
pendingRawData: Record<string, PendingRawData>;
|
|
196
|
+
loopStopReason: LoopStopReason | null;
|
|
197
|
+
/** 用户在 agent loop 运行期间发送的消息队列,loop 每轮结束后依次注入 */
|
|
198
|
+
pendingUserInputQueue: string[];
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
// --- Initial State ---
|
|
202
|
+
|
|
203
|
+
const GLOBAL_DIALOG_RUNTIME_KEY = "__global__";
|
|
204
|
+
|
|
205
|
+
const createEmptyTokenStats = (): TokenStats => ({
|
|
206
|
+
inputTokens: 0,
|
|
207
|
+
outputTokens: 0,
|
|
208
|
+
totalCost: 0,
|
|
209
|
+
});
|
|
210
|
+
|
|
211
|
+
const createEmptyDialogRuntimeState = (): DialogRuntimeState => ({
|
|
212
|
+
tokens: createEmptyTokenStats(),
|
|
213
|
+
pendingFiles: [],
|
|
214
|
+
activeControllers: {},
|
|
215
|
+
pendingRawData: {},
|
|
216
|
+
loopStopReason: null,
|
|
217
|
+
pendingUserInputQueue: [],
|
|
218
|
+
});
|
|
219
|
+
|
|
220
|
+
const resetDialogRuntimeSessionState = (runtime: DialogRuntimeState) => {
|
|
221
|
+
runtime.tokens = createEmptyTokenStats();
|
|
222
|
+
runtime.loopStopReason = null;
|
|
223
|
+
};
|
|
224
|
+
|
|
225
|
+
const resolveDialogRuntimeKey = (
|
|
226
|
+
state: Pick<DialogState, "currentDialogKey">,
|
|
227
|
+
dialogKey?: string | null
|
|
228
|
+
) => dialogKey ?? state.currentDialogKey ?? GLOBAL_DIALOG_RUNTIME_KEY;
|
|
229
|
+
|
|
230
|
+
const ensureDialogRuntimeState = (
|
|
231
|
+
state: DialogState,
|
|
232
|
+
dialogKey?: string | null
|
|
233
|
+
): DialogRuntimeState => {
|
|
234
|
+
const runtimeKey = resolveDialogRuntimeKey(state, dialogKey);
|
|
235
|
+
if (!state.dialogRuntimeByKey[runtimeKey]) {
|
|
236
|
+
state.dialogRuntimeByKey[runtimeKey] = createEmptyDialogRuntimeState();
|
|
237
|
+
}
|
|
238
|
+
return state.dialogRuntimeByKey[runtimeKey];
|
|
239
|
+
};
|
|
240
|
+
|
|
241
|
+
const getDialogRuntimeState = (
|
|
242
|
+
state: DialogState,
|
|
243
|
+
dialogKey?: string | null
|
|
244
|
+
): DialogRuntimeState =>
|
|
245
|
+
state.dialogRuntimeByKey[resolveDialogRuntimeKey(state, dialogKey)] ??
|
|
246
|
+
createEmptyDialogRuntimeState();
|
|
247
|
+
|
|
248
|
+
const initialState: DialogState = {
|
|
249
|
+
currentDialogKey: null,
|
|
250
|
+
dialogRuntimeByKey: {
|
|
251
|
+
[GLOBAL_DIALOG_RUNTIME_KEY]: createEmptyDialogRuntimeState(),
|
|
252
|
+
},
|
|
253
|
+
isUpdatingMode: false,
|
|
254
|
+
};
|
|
255
|
+
|
|
256
|
+
// --- Slice Definition ---
|
|
257
|
+
|
|
258
|
+
const dialogSlice = createSliceWithThunks({
|
|
259
|
+
name: "dialog",
|
|
260
|
+
initialState,
|
|
261
|
+
reducers: (create) => ({
|
|
262
|
+
// --- Thunks ---
|
|
263
|
+
createPageAndAddReference: create.asyncThunk(
|
|
264
|
+
async (payload: CreatePagePayload, { dispatch, rejectWithValue }) => {
|
|
265
|
+
const { slateData, jsonData, title, type, fileId, groupId, dialogKey } = payload;
|
|
266
|
+
try {
|
|
267
|
+
const { createDoc } = await import("../../render/page/docSlice");
|
|
268
|
+
const pageKey = await (dispatch as any)(
|
|
269
|
+
createDoc({ slateData, title })
|
|
270
|
+
).unwrap();
|
|
271
|
+
|
|
272
|
+
const newReference: PendingFile = {
|
|
273
|
+
id: fileId,
|
|
274
|
+
name: title,
|
|
275
|
+
pageKey,
|
|
276
|
+
dialogKey,
|
|
277
|
+
type,
|
|
278
|
+
groupId,
|
|
279
|
+
};
|
|
280
|
+
const newRawData = jsonData ? { pageKey, jsonData } : null;
|
|
281
|
+
|
|
282
|
+
return { reference: newReference, rawData: newRawData, dialogKey };
|
|
283
|
+
} catch (error) {
|
|
284
|
+
console.error("创建页面或引用失败:", error);
|
|
285
|
+
return rejectWithValue((error as Error).message);
|
|
286
|
+
}
|
|
287
|
+
},
|
|
288
|
+
{
|
|
289
|
+
fulfilled: (state, action) => {
|
|
290
|
+
const runtime = ensureDialogRuntimeState(
|
|
291
|
+
state,
|
|
292
|
+
action.payload.dialogKey ?? action.meta.arg.dialogKey
|
|
293
|
+
);
|
|
294
|
+
runtime.pendingFiles.push(action.payload.reference);
|
|
295
|
+
if (action.payload.rawData && action.payload.rawData.pageKey) {
|
|
296
|
+
runtime.pendingRawData[action.payload.rawData.pageKey] =
|
|
297
|
+
action.payload.rawData;
|
|
298
|
+
}
|
|
299
|
+
},
|
|
300
|
+
}
|
|
301
|
+
),
|
|
302
|
+
|
|
303
|
+
deleteDialog: create.asyncThunk(
|
|
304
|
+
async (dialogKey: string, thunkApi) => {
|
|
305
|
+
const { dispatch, getState, extra } = thunkApi;
|
|
306
|
+
const { db } = extra;
|
|
307
|
+
const state = getState() as RootState;
|
|
308
|
+
const { currentServer, syncServers } = getRuntimeServerContext(state);
|
|
309
|
+
const currentDialogKey = state.dialog.currentDialogKey;
|
|
310
|
+
const currentDialogId = currentDialogKey
|
|
311
|
+
? extractCustomId(currentDialogKey)
|
|
312
|
+
: null;
|
|
313
|
+
const targetDialogId = extractCustomId(dialogKey);
|
|
314
|
+
const targetDialogConfig = selectById(state, dialogKey) as DialogConfig | null;
|
|
315
|
+
|
|
316
|
+
const isCurrentDialog =
|
|
317
|
+
currentDialogId !== null && currentDialogId === targetDialogId;
|
|
318
|
+
await cleanupCliSessionForDialog({ dispatch, getState }, targetDialogConfig);
|
|
319
|
+
// --- 无论是否是当前对话,都从当前空间删除该内容 ---
|
|
320
|
+
await (dispatch as any)(remove(dialogKey));
|
|
321
|
+
const prefix = createKey("dialog", targetDialogId, "msg");
|
|
322
|
+
const deletedIds = await collectKeys(prefix, db);
|
|
323
|
+
if (!deletedIds.length) return { ids: [] };
|
|
324
|
+
|
|
325
|
+
const ops = deletedIds.map((key) => ({ type: "del" as const, key }));
|
|
326
|
+
|
|
327
|
+
await db.batch(ops);
|
|
328
|
+
|
|
329
|
+
scheduleDeleteReplication({
|
|
330
|
+
currentServer,
|
|
331
|
+
syncServers,
|
|
332
|
+
dbKey: targetDialogId,
|
|
333
|
+
deleteOptions: { type: "messages" },
|
|
334
|
+
state,
|
|
335
|
+
});
|
|
336
|
+
// --- 如果是当前对话,清理当前相关状态 ---
|
|
337
|
+
if (isCurrentDialog) {
|
|
338
|
+
await thunkApi.dispatch(resetMsgs());
|
|
339
|
+
dispatch(dialogSlice.actions.clearPendingAttachments());
|
|
340
|
+
dispatch(clearPlan());
|
|
341
|
+
dispatch(clearWorkflow());
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
return { dialogKey, isCurrentDialog };
|
|
345
|
+
},
|
|
346
|
+
{
|
|
347
|
+
fulfilled: (state, action) => {
|
|
348
|
+
delete state.dialogRuntimeByKey[action.payload.dialogKey];
|
|
349
|
+
if (action.payload.isCurrentDialog) {
|
|
350
|
+
state.currentDialogKey = null;
|
|
351
|
+
}
|
|
352
|
+
},
|
|
353
|
+
}
|
|
354
|
+
),
|
|
355
|
+
|
|
356
|
+
initDialog: create.asyncThunk(
|
|
357
|
+
async (id: string, { dispatch, signal, getState }) => {
|
|
358
|
+
dispatch(dialogSlice.actions.clearPendingAttachments());
|
|
359
|
+
dispatch(clearPlan());
|
|
360
|
+
dispatch(clearWorkflow());
|
|
361
|
+
const { currentServer: preferredServerOrigin } = getRuntimeServerContext(
|
|
362
|
+
getState() as RootState
|
|
363
|
+
);
|
|
364
|
+
return await (dispatch as any)(
|
|
365
|
+
read({
|
|
366
|
+
dbKey: id,
|
|
367
|
+
signal,
|
|
368
|
+
preferredServerOrigin,
|
|
369
|
+
})
|
|
370
|
+
).unwrap();
|
|
371
|
+
},
|
|
372
|
+
{
|
|
373
|
+
pending: (state, action) => {
|
|
374
|
+
state.currentDialogKey = action.meta.arg;
|
|
375
|
+
const runtime = ensureDialogRuntimeState(state, action.meta.arg);
|
|
376
|
+
resetDialogRuntimeSessionState(runtime);
|
|
377
|
+
},
|
|
378
|
+
fulfilled: (state, action) => {
|
|
379
|
+
if (state.currentDialogKey === action.meta.arg) {
|
|
380
|
+
// No-op
|
|
381
|
+
}
|
|
382
|
+
},
|
|
383
|
+
rejected: (state, action) => {
|
|
384
|
+
const isAborted =
|
|
385
|
+
action.error.name === "AbortError" ||
|
|
386
|
+
action.error.message === "Aborted";
|
|
387
|
+
const isCurrentDialog = state.currentDialogKey === action.meta.arg;
|
|
388
|
+
|
|
389
|
+
if (!isAborted && isCurrentDialog) {
|
|
390
|
+
// Log the error but keep currentDialogKey set — the dialog key is
|
|
391
|
+
// valid (it came from the space content list), we just couldn't load
|
|
392
|
+
// the config doc (e.g. not synced to server yet or network error).
|
|
393
|
+
// Keeping the key allows MessageInput to send and MessageList to
|
|
394
|
+
// display messages independently.
|
|
395
|
+
console.info("Failed to load dialog config:", action.error.message);
|
|
396
|
+
}
|
|
397
|
+
},
|
|
398
|
+
}
|
|
399
|
+
),
|
|
400
|
+
|
|
401
|
+
handleSendMessage: create.asyncThunk(runHandleSendMessageAction),
|
|
402
|
+
|
|
403
|
+
abortAllMessages: create.asyncThunk(
|
|
404
|
+
async (
|
|
405
|
+
args: { dialogKey?: string; all?: boolean } | undefined,
|
|
406
|
+
{ getState, dispatch }
|
|
407
|
+
) => {
|
|
408
|
+
/**
|
|
409
|
+
* 产品约束说明:
|
|
410
|
+
* - 当前网页端 stop 默认只停“当前对话”,避免切换对话时把后台任务一起杀掉
|
|
411
|
+
* - `all: true` 仅用于 logout / 全局 reset 这类明确的系统级清理
|
|
412
|
+
* - 未来桌面端若要支持“从任务中心停任意 dialog”,应在 UI 层显式传 dialogKey
|
|
413
|
+
*/
|
|
414
|
+
const dialogState = (getState() as RootState).dialog;
|
|
415
|
+
const runtimeStates = args?.all
|
|
416
|
+
? Object.values(dialogState.dialogRuntimeByKey)
|
|
417
|
+
: [getDialogRuntimeState(dialogState, args?.dialogKey)];
|
|
418
|
+
|
|
419
|
+
runtimeStates.forEach((runtimeState) => {
|
|
420
|
+
Object.values(runtimeState.activeControllers).forEach((controller) =>
|
|
421
|
+
controller.abort()
|
|
422
|
+
);
|
|
423
|
+
});
|
|
424
|
+
dispatch(clearAllStreaming(args));
|
|
425
|
+
dispatch(dialogSlice.actions.clearActiveControllers(args));
|
|
426
|
+
}
|
|
427
|
+
),
|
|
428
|
+
|
|
429
|
+
// Passthrough thunks for external actions
|
|
430
|
+
updateTokens: create.asyncThunk(updateTokensAction, {
|
|
431
|
+
fulfilled: (state, action) => {
|
|
432
|
+
const dialogKey = action.meta.arg.dialogKey;
|
|
433
|
+
if (!dialogKey) return;
|
|
434
|
+
|
|
435
|
+
const runtime = state.dialogRuntimeByKey[dialogKey];
|
|
436
|
+
if (!runtime) return;
|
|
437
|
+
|
|
438
|
+
runtime.tokens.inputTokens = Math.max(
|
|
439
|
+
0,
|
|
440
|
+
runtime.tokens.inputTokens - (action.payload.input_tokens ?? 0)
|
|
441
|
+
);
|
|
442
|
+
runtime.tokens.outputTokens = Math.max(
|
|
443
|
+
0,
|
|
444
|
+
runtime.tokens.outputTokens - (action.payload.output_tokens ?? 0)
|
|
445
|
+
);
|
|
446
|
+
runtime.tokens.totalCost = Math.max(
|
|
447
|
+
0,
|
|
448
|
+
runtime.tokens.totalCost - (action.payload.cost ?? 0)
|
|
449
|
+
);
|
|
450
|
+
},
|
|
451
|
+
}),
|
|
452
|
+
saveDialogGoal: create.asyncThunk(
|
|
453
|
+
async (
|
|
454
|
+
payload: CreateDialogGoalPayload,
|
|
455
|
+
{ dispatch, getState }
|
|
456
|
+
) => {
|
|
457
|
+
const state = getState() as RootState;
|
|
458
|
+
const dialogKey = payload.dialogKey ?? state.dialog.currentDialogKey;
|
|
459
|
+
if (!dialogKey) {
|
|
460
|
+
throw new Error("Cannot save dialog goal without a dialog key.");
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
const goal = buildDialogGoal(payload);
|
|
464
|
+
await (dispatch as any)(
|
|
465
|
+
patch({
|
|
466
|
+
dbKey: dialogKey,
|
|
467
|
+
changes: { goal },
|
|
468
|
+
})
|
|
469
|
+
).unwrap();
|
|
470
|
+
|
|
471
|
+
return { dialogKey, goal };
|
|
472
|
+
},
|
|
473
|
+
{
|
|
474
|
+
fulfilled: (state, action) => {
|
|
475
|
+
const runtime = ensureDialogRuntimeState(state, action.payload.dialogKey);
|
|
476
|
+
runtime.goal = action.payload.goal;
|
|
477
|
+
},
|
|
478
|
+
}
|
|
479
|
+
),
|
|
480
|
+
saveCompletedDialogGoal: create.asyncThunk(
|
|
481
|
+
async (
|
|
482
|
+
payload: CompleteDialogGoalPayload | undefined,
|
|
483
|
+
{ dispatch, getState }
|
|
484
|
+
) => {
|
|
485
|
+
const state = getState() as RootState;
|
|
486
|
+
const dialogKey = payload?.dialogKey ?? state.dialog.currentDialogKey;
|
|
487
|
+
if (!dialogKey) {
|
|
488
|
+
throw new Error("Cannot complete dialog goal without a dialog key.");
|
|
489
|
+
}
|
|
490
|
+
|
|
491
|
+
const runtimeGoal =
|
|
492
|
+
state.dialog.dialogRuntimeByKey[dialogKey]?.goal ?? null;
|
|
493
|
+
const persistedDialog = selectById(state, dialogKey) as DialogConfig | null;
|
|
494
|
+
const goal = runtimeGoal ?? persistedDialog?.goal ?? null;
|
|
495
|
+
if (!goal) {
|
|
496
|
+
throw new Error("Cannot complete dialog goal before one is created.");
|
|
497
|
+
}
|
|
498
|
+
|
|
499
|
+
const completedGoal: DialogGoalState = {
|
|
500
|
+
...goal,
|
|
501
|
+
status: "complete",
|
|
502
|
+
completedAt: payload?.now ?? Date.now(),
|
|
503
|
+
};
|
|
504
|
+
await (dispatch as any)(
|
|
505
|
+
patch({
|
|
506
|
+
dbKey: dialogKey,
|
|
507
|
+
changes: { goal: completedGoal },
|
|
508
|
+
})
|
|
509
|
+
).unwrap();
|
|
510
|
+
|
|
511
|
+
return { dialogKey, goal: completedGoal };
|
|
512
|
+
},
|
|
513
|
+
{
|
|
514
|
+
fulfilled: (state, action) => {
|
|
515
|
+
const runtime = ensureDialogRuntimeState(state, action.payload.dialogKey);
|
|
516
|
+
runtime.goal = action.payload.goal;
|
|
517
|
+
},
|
|
518
|
+
}
|
|
519
|
+
),
|
|
520
|
+
createDialog: create.asyncThunk(runCreateDialogAction),
|
|
521
|
+
createScheduledTask: create.asyncThunk(runCreateScheduledTaskAction),
|
|
522
|
+
updateDialogTitle: create.asyncThunk(runUpdateDialogTitleAction),
|
|
523
|
+
addCybot: create.asyncThunk(runAddCybotAction),
|
|
524
|
+
removeCybot: create.asyncThunk(runRemoveCybotAction),
|
|
525
|
+
replacePrimaryCybot: create.asyncThunk(runReplacePrimaryCybotAction),
|
|
526
|
+
addDialogAgent: create.asyncThunk(runAddCybotAction),
|
|
527
|
+
removeDialogAgent: create.asyncThunk(runRemoveDialogAgentAction),
|
|
528
|
+
setPrimaryDialogAgent: create.asyncThunk(runSetPrimaryDialogAgentAction),
|
|
529
|
+
|
|
530
|
+
// --- Reducers ---
|
|
531
|
+
addPendingFile: create.reducer(
|
|
532
|
+
(state, action: PayloadAction<PendingFile>) => {
|
|
533
|
+
const targetRuntimeKey =
|
|
534
|
+
action.payload.targetDialogKey ??
|
|
535
|
+
action.payload.runtimeDialogKey ??
|
|
536
|
+
(action.payload.type === "dialog" ? state.currentDialogKey : action.payload.dialogKey);
|
|
537
|
+
const runtime = ensureDialogRuntimeState(state, targetRuntimeKey);
|
|
538
|
+
if (!runtime.pendingFiles.some((f) => f.id === action.payload.id)) {
|
|
539
|
+
runtime.pendingFiles.push(action.payload);
|
|
540
|
+
}
|
|
541
|
+
}
|
|
542
|
+
),
|
|
543
|
+
|
|
544
|
+
removePendingFile: create.reducer(
|
|
545
|
+
(state, action: PayloadAction<string>) => {
|
|
546
|
+
const runtime = ensureDialogRuntimeState(state);
|
|
547
|
+
const fileToRemove = runtime.pendingFiles.find(
|
|
548
|
+
(f) => f.id === action.payload
|
|
549
|
+
);
|
|
550
|
+
if (fileToRemove) {
|
|
551
|
+
if (fileToRemove.pageKey) {
|
|
552
|
+
delete runtime.pendingRawData[fileToRemove.pageKey];
|
|
553
|
+
}
|
|
554
|
+
runtime.pendingFiles = runtime.pendingFiles.filter(
|
|
555
|
+
(file) => file.id !== action.payload
|
|
556
|
+
);
|
|
557
|
+
}
|
|
558
|
+
}
|
|
559
|
+
),
|
|
560
|
+
|
|
561
|
+
clearPendingAttachments: create.reducer(
|
|
562
|
+
(state, action: PayloadAction<{ dialogKey?: string; all?: boolean } | undefined>) => {
|
|
563
|
+
if (action.payload?.all) {
|
|
564
|
+
Object.values(state.dialogRuntimeByKey).forEach((runtime) => {
|
|
565
|
+
runtime.pendingFiles = [];
|
|
566
|
+
runtime.pendingRawData = {};
|
|
567
|
+
});
|
|
568
|
+
return;
|
|
569
|
+
}
|
|
570
|
+
const runtime = ensureDialogRuntimeState(state, action.payload?.dialogKey);
|
|
571
|
+
runtime.pendingFiles = [];
|
|
572
|
+
runtime.pendingRawData = {};
|
|
573
|
+
}
|
|
574
|
+
),
|
|
575
|
+
|
|
576
|
+
setLoopStopReason: create.reducer(
|
|
577
|
+
(
|
|
578
|
+
state,
|
|
579
|
+
action: PayloadAction<{ reason: LoopStopReason | null; dialogKey?: string }>
|
|
580
|
+
) => {
|
|
581
|
+
const runtime = ensureDialogRuntimeState(state, action.payload.dialogKey);
|
|
582
|
+
runtime.loopStopReason = action.payload.reason;
|
|
583
|
+
}
|
|
584
|
+
),
|
|
585
|
+
|
|
586
|
+
clearDialogRuntimeState: create.reducer(
|
|
587
|
+
(state, action: PayloadAction<{ dialogKey: string }>) => {
|
|
588
|
+
delete state.dialogRuntimeByKey[action.payload.dialogKey];
|
|
589
|
+
}
|
|
590
|
+
),
|
|
591
|
+
|
|
592
|
+
createDialogGoal: create.reducer(
|
|
593
|
+
(state, action: PayloadAction<CreateDialogGoalPayload>) => {
|
|
594
|
+
const objective = action.payload.objective.trim();
|
|
595
|
+
if (!objective) return;
|
|
596
|
+
const runtime = ensureDialogRuntimeState(state, action.payload.dialogKey);
|
|
597
|
+
const tokenBudget =
|
|
598
|
+
typeof action.payload.tokenBudget === "number" &&
|
|
599
|
+
Number.isFinite(action.payload.tokenBudget) &&
|
|
600
|
+
action.payload.tokenBudget > 0
|
|
601
|
+
? Math.floor(action.payload.tokenBudget)
|
|
602
|
+
: undefined;
|
|
603
|
+
runtime.goal = {
|
|
604
|
+
objective,
|
|
605
|
+
status: "active",
|
|
606
|
+
...(tokenBudget ? { tokenBudget } : {}),
|
|
607
|
+
createdAt: action.payload.now ?? Date.now(),
|
|
608
|
+
};
|
|
609
|
+
}
|
|
610
|
+
),
|
|
611
|
+
|
|
612
|
+
completeDialogGoal: create.reducer(
|
|
613
|
+
(state, action: PayloadAction<CompleteDialogGoalPayload | undefined>) => {
|
|
614
|
+
const runtime = ensureDialogRuntimeState(state, action.payload?.dialogKey);
|
|
615
|
+
if (!runtime.goal) return;
|
|
616
|
+
runtime.goal = {
|
|
617
|
+
...runtime.goal,
|
|
618
|
+
status: "complete",
|
|
619
|
+
completedAt: action.payload?.now ?? Date.now(),
|
|
620
|
+
};
|
|
621
|
+
}
|
|
622
|
+
),
|
|
623
|
+
|
|
624
|
+
addActiveController: create.reducer(
|
|
625
|
+
(
|
|
626
|
+
state,
|
|
627
|
+
action: PayloadAction<{
|
|
628
|
+
messageId: string;
|
|
629
|
+
controller: AbortController;
|
|
630
|
+
dialogKey?: string;
|
|
631
|
+
}>
|
|
632
|
+
) => {
|
|
633
|
+
const runtime = ensureDialogRuntimeState(state, action.payload.dialogKey);
|
|
634
|
+
runtime.activeControllers[action.payload.messageId] =
|
|
635
|
+
action.payload.controller;
|
|
636
|
+
}
|
|
637
|
+
),
|
|
638
|
+
|
|
639
|
+
removeActiveController: create.reducer(
|
|
640
|
+
(
|
|
641
|
+
state,
|
|
642
|
+
action: PayloadAction<{ messageId: string; dialogKey?: string } | string>
|
|
643
|
+
) => {
|
|
644
|
+
const payload =
|
|
645
|
+
typeof action.payload === "string"
|
|
646
|
+
? { messageId: action.payload }
|
|
647
|
+
: action.payload;
|
|
648
|
+
const runtime = ensureDialogRuntimeState(state, payload.dialogKey);
|
|
649
|
+
delete runtime.activeControllers[payload.messageId];
|
|
650
|
+
}
|
|
651
|
+
),
|
|
652
|
+
|
|
653
|
+
clearActiveControllers: create.reducer(
|
|
654
|
+
(state, action: PayloadAction<{ dialogKey?: string; all?: boolean } | undefined>) => {
|
|
655
|
+
if (action.payload?.all) {
|
|
656
|
+
Object.values(state.dialogRuntimeByKey).forEach((runtime) => {
|
|
657
|
+
runtime.activeControllers = {};
|
|
658
|
+
});
|
|
659
|
+
return;
|
|
660
|
+
}
|
|
661
|
+
const runtime = ensureDialogRuntimeState(state, action.payload?.dialogKey);
|
|
662
|
+
runtime.activeControllers = {};
|
|
663
|
+
}
|
|
664
|
+
),
|
|
665
|
+
|
|
666
|
+
enqueueUserInput: create.reducer(
|
|
667
|
+
(state, action: PayloadAction<string | { text: string; dialogKey?: string }>) => {
|
|
668
|
+
const payload =
|
|
669
|
+
typeof action.payload === "string"
|
|
670
|
+
? { text: action.payload }
|
|
671
|
+
: action.payload;
|
|
672
|
+
const runtime = ensureDialogRuntimeState(state, payload.dialogKey);
|
|
673
|
+
runtime.pendingUserInputQueue.push(payload.text);
|
|
674
|
+
}
|
|
675
|
+
),
|
|
676
|
+
|
|
677
|
+
dequeueUserInput: create.reducer(
|
|
678
|
+
(state, action: PayloadAction<{ dialogKey?: string } | undefined>) => {
|
|
679
|
+
const runtime = ensureDialogRuntimeState(state, action.payload?.dialogKey);
|
|
680
|
+
runtime.pendingUserInputQueue.shift();
|
|
681
|
+
}
|
|
682
|
+
),
|
|
683
|
+
|
|
684
|
+
clearPendingUserInputQueue: create.reducer(
|
|
685
|
+
(state, action: PayloadAction<{ dialogKey?: string; all?: boolean } | undefined>) => {
|
|
686
|
+
if (action.payload?.all) {
|
|
687
|
+
Object.values(state.dialogRuntimeByKey).forEach((runtime) => {
|
|
688
|
+
runtime.pendingUserInputQueue = [];
|
|
689
|
+
});
|
|
690
|
+
return;
|
|
691
|
+
}
|
|
692
|
+
const runtime = ensureDialogRuntimeState(state, action.payload?.dialogKey);
|
|
693
|
+
runtime.pendingUserInputQueue = [];
|
|
694
|
+
}
|
|
695
|
+
),
|
|
696
|
+
|
|
697
|
+
tokenUsageLiveUpdate: create.reducer(
|
|
698
|
+
(
|
|
699
|
+
state,
|
|
700
|
+
action: PayloadAction<LiveTokenUsagePayload>
|
|
701
|
+
) => {
|
|
702
|
+
const runtime = ensureDialogRuntimeState(state, action.payload.dialogKey);
|
|
703
|
+
runtime.tokens.inputTokens += action.payload.input_tokens;
|
|
704
|
+
runtime.tokens.outputTokens += action.payload.output_tokens;
|
|
705
|
+
runtime.tokens.totalCost += action.payload.cost ?? 0;
|
|
706
|
+
}
|
|
707
|
+
),
|
|
708
|
+
|
|
709
|
+
clearDialogState: create.reducer((state) => {
|
|
710
|
+
const previousDialogKey = state.currentDialogKey;
|
|
711
|
+
const previousRuntime = previousDialogKey
|
|
712
|
+
? state.dialogRuntimeByKey[previousDialogKey]
|
|
713
|
+
: null;
|
|
714
|
+
const globalRuntime = ensureDialogRuntimeState(state, GLOBAL_DIALOG_RUNTIME_KEY);
|
|
715
|
+
|
|
716
|
+
if (previousRuntime) {
|
|
717
|
+
if (previousRuntime.pendingFiles.length > 0) {
|
|
718
|
+
globalRuntime.pendingFiles = previousRuntime.pendingFiles;
|
|
719
|
+
previousRuntime.pendingFiles = [];
|
|
720
|
+
}
|
|
721
|
+
previousRuntime.pendingRawData = {};
|
|
722
|
+
previousRuntime.pendingUserInputQueue = [];
|
|
723
|
+
}
|
|
724
|
+
state.currentDialogKey = null;
|
|
725
|
+
globalRuntime.pendingRawData = {};
|
|
726
|
+
globalRuntime.pendingUserInputQueue = [];
|
|
727
|
+
}),
|
|
728
|
+
}),
|
|
729
|
+
selectors: {
|
|
730
|
+
selectCurrentDialogKey: (state) => state.currentDialogKey,
|
|
731
|
+
selectIsUpdatingMode: (state) => state.isUpdatingMode,
|
|
732
|
+
selectDialogRuntimeByKey: (state, dialogKey?: string) =>
|
|
733
|
+
getDialogRuntimeState(state, dialogKey),
|
|
734
|
+
selectPendingFiles: (state, dialogKey?: string) =>
|
|
735
|
+
getDialogRuntimeState(state, dialogKey).pendingFiles,
|
|
736
|
+
selectActiveControllers: (state, dialogKey?: string) =>
|
|
737
|
+
getDialogRuntimeState(state, dialogKey).activeControllers,
|
|
738
|
+
selectPendingRawData: (state, dialogKey?: string) =>
|
|
739
|
+
getDialogRuntimeState(state, dialogKey).pendingRawData,
|
|
740
|
+
selectDialogRuntimeTokens: (state, dialogKey?: string) =>
|
|
741
|
+
getDialogRuntimeState(state, dialogKey).tokens,
|
|
742
|
+
selectDialogRuntimeGoal: (state, dialogKey?: string) =>
|
|
743
|
+
getDialogRuntimeState(state, dialogKey).goal ?? null,
|
|
744
|
+
selectTotalDialogTokens: (state, dialogKey?: string) =>
|
|
745
|
+
getDialogRuntimeState(state, dialogKey).tokens,
|
|
746
|
+
selectPendingRawDataByPageKey: (state, pageKey: string) =>
|
|
747
|
+
getDialogRuntimeState(state).pendingRawData[pageKey],
|
|
748
|
+
selectPendingUserInputQueue: (state, dialogKey?: string) =>
|
|
749
|
+
getDialogRuntimeState(state, dialogKey).pendingUserInputQueue,
|
|
750
|
+
selectLoopStopReason: (state, dialogKey?: string) =>
|
|
751
|
+
getDialogRuntimeState(state, dialogKey).loopStopReason,
|
|
752
|
+
},
|
|
753
|
+
});
|
|
754
|
+
|
|
755
|
+
// --- Actions and Reducer Exports ---
|
|
756
|
+
export const {
|
|
757
|
+
createPageAndAddReference,
|
|
758
|
+
addPendingFile,
|
|
759
|
+
removePendingFile,
|
|
760
|
+
clearPendingAttachments,
|
|
761
|
+
clearDialogRuntimeState,
|
|
762
|
+
initDialog,
|
|
763
|
+
deleteDialog,
|
|
764
|
+
updateTokens,
|
|
765
|
+
saveDialogGoal,
|
|
766
|
+
saveCompletedDialogGoal,
|
|
767
|
+
clearDialogState,
|
|
768
|
+
createDialog,
|
|
769
|
+
createScheduledTask,
|
|
770
|
+
createDialogGoal,
|
|
771
|
+
completeDialogGoal,
|
|
772
|
+
updateDialogTitle,
|
|
773
|
+
addCybot,
|
|
774
|
+
addDialogAgent,
|
|
775
|
+
removeCybot,
|
|
776
|
+
removeDialogAgent,
|
|
777
|
+
replacePrimaryCybot,
|
|
778
|
+
setPrimaryDialogAgent,
|
|
779
|
+
handleSendMessage,
|
|
780
|
+
addActiveController,
|
|
781
|
+
removeActiveController,
|
|
782
|
+
abortAllMessages,
|
|
783
|
+
clearActiveControllers,
|
|
784
|
+
tokenUsageLiveUpdate,
|
|
785
|
+
setLoopStopReason,
|
|
786
|
+
enqueueUserInput,
|
|
787
|
+
dequeueUserInput,
|
|
788
|
+
clearPendingUserInputQueue,
|
|
789
|
+
} = dialogSlice.actions;
|
|
790
|
+
|
|
791
|
+
export default dialogSlice.reducer;
|
|
792
|
+
|
|
793
|
+
// --- Selectors Exports ---
|
|
794
|
+
export const {
|
|
795
|
+
selectCurrentDialogKey,
|
|
796
|
+
selectIsUpdatingMode,
|
|
797
|
+
selectPendingFiles,
|
|
798
|
+
selectActiveControllers,
|
|
799
|
+
selectDialogRuntimeByKey,
|
|
800
|
+
selectLoopStopReason,
|
|
801
|
+
selectPendingRawData,
|
|
802
|
+
selectDialogRuntimeTokens,
|
|
803
|
+
selectDialogRuntimeGoal,
|
|
804
|
+
selectPendingRawDataByPageKey,
|
|
805
|
+
selectPendingUserInputQueue,
|
|
806
|
+
} = dialogSlice.selectors;
|
|
807
|
+
|
|
808
|
+
export const selectCurrentDialogConfig = createSelector(
|
|
809
|
+
(state: RootState) => state,
|
|
810
|
+
selectCurrentDialogKey,
|
|
811
|
+
(state, currentDialogKey) =>
|
|
812
|
+
currentDialogKey
|
|
813
|
+
? (selectById(state, currentDialogKey) as DialogConfig | null)
|
|
814
|
+
: null
|
|
815
|
+
);
|
|
816
|
+
|
|
817
|
+
export const selectCurrentDialogAgentIds = createSelector(
|
|
818
|
+
selectCurrentDialogConfig,
|
|
819
|
+
(dialogConfig) => getDialogAgentIds(dialogConfig)
|
|
820
|
+
);
|
|
821
|
+
|
|
822
|
+
export const selectCurrentPrimaryAgentId = createSelector(
|
|
823
|
+
selectCurrentDialogConfig,
|
|
824
|
+
(dialogConfig) => getPrimaryDialogAgentId(dialogConfig)
|
|
825
|
+
);
|
|
826
|
+
|
|
827
|
+
export const selectDialogConfigByKey = createSelector(
|
|
828
|
+
(state: RootState) => state,
|
|
829
|
+
(_: RootState, dialogKey?: string | null) => dialogKey,
|
|
830
|
+
(state, dialogKey) =>
|
|
831
|
+
dialogKey ? (selectById(state, dialogKey) as DialogConfig | null) : null
|
|
832
|
+
);
|
|
833
|
+
|
|
834
|
+
export const selectCurrentDialogTokens = createSelector(
|
|
835
|
+
(state: RootState) => state,
|
|
836
|
+
selectCurrentDialogConfig,
|
|
837
|
+
(state: RootState) => selectDialogRuntimeTokens(state),
|
|
838
|
+
(_state: RootState, dialogKey?: string) => dialogKey,
|
|
839
|
+
(state, currentDialog, currentRuntimeTokens, dialogKey) => {
|
|
840
|
+
if (dialogKey) {
|
|
841
|
+
const dialogConfig = selectById(state, dialogKey) as DialogConfig | null;
|
|
842
|
+
const runtimeTokens = selectDialogRuntimeTokens(state, dialogKey);
|
|
843
|
+
return mergeDialogTokenStats(dialogConfig, runtimeTokens);
|
|
844
|
+
}
|
|
845
|
+
|
|
846
|
+
return mergeDialogTokenStats(currentDialog, currentRuntimeTokens);
|
|
847
|
+
}
|
|
848
|
+
);
|
|
849
|
+
|
|
850
|
+
export const selectTotalDialogTokens = selectCurrentDialogTokens;
|
|
851
|
+
|
|
852
|
+
export const selectCurrentDialogGoalReport = createSelector(
|
|
853
|
+
(state: RootState) => state,
|
|
854
|
+
selectCurrentDialogConfig,
|
|
855
|
+
(state: RootState) => selectDialogRuntimeTokens(state),
|
|
856
|
+
(state: RootState) => selectDialogRuntimeGoal(state),
|
|
857
|
+
(_state: RootState, dialogKey?: string) => dialogKey,
|
|
858
|
+
(state, currentDialog, currentRuntimeTokens, currentRuntimeGoal, dialogKey) => {
|
|
859
|
+
if (dialogKey) {
|
|
860
|
+
const dialogConfig = selectById(state, dialogKey) as DialogConfig | null;
|
|
861
|
+
const runtimeTokens = selectDialogRuntimeTokens(state, dialogKey);
|
|
862
|
+
const runtimeGoal = selectDialogRuntimeGoal(state, dialogKey);
|
|
863
|
+
return getDialogGoalReport(dialogConfig, runtimeTokens, runtimeGoal);
|
|
864
|
+
}
|
|
865
|
+
|
|
866
|
+
return getDialogGoalReport(
|
|
867
|
+
currentDialog,
|
|
868
|
+
currentRuntimeTokens,
|
|
869
|
+
currentRuntimeGoal
|
|
870
|
+
);
|
|
871
|
+
}
|
|
872
|
+
);
|