nolo-cli 0.1.39 → 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/README.md +4 -4
- package/agent-runtime/dialogWritePlan.ts +39 -0
- package/agent-runtime/hostAdapter.ts +4 -0
- package/agent-runtime/hybridRecordStore.ts +5 -3
- package/agent-runtime/index.ts +15 -0
- package/agent-runtime/localLoop.ts +87 -7
- package/agent-runtime/localToolPolicy.ts +2 -13
- package/agent-runtime/localWorkspaceTools.ts +42 -110
- package/agent-runtime/runtimeToolPolicy.ts +2 -11
- package/agent-runtime/runtimeToolSurface.ts +198 -0
- package/agent-runtime/types.ts +1 -0
- package/agentAliases.ts +2 -1
- package/agentRunCommand.ts +10 -32
- package/agentRuntimeLocal.ts +1 -0
- 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 +314 -40
- 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/agentAliases.ts +2 -1
- package/cli/agentRunCommand.ts +10 -32
- package/cli/agentRuntimeLocal.ts +1 -0
- package/cli/client/agentRun.ts +67 -95
- package/cli/client/localRuntimeAdapter.ts +323 -54
- package/cli/commandRegistry.ts +2 -2
- package/cli/machineWsRunDispatch.ts +51 -6
- package/cli/offlineMarxistsAgentCommand.ts +6 -4
- package/client/agentConfigResolver.test.ts +2 -2
- package/client/agentRun.test.ts +175 -57
- package/client/agentRun.ts +67 -95
- package/client/localRuntimeAdapter.test.ts +682 -37
- package/client/localRuntimeAdapter.ts +323 -54
- package/client/localRuntimeDryRun.test.ts +23 -9
- package/client/localToolPolicy.test.ts +4 -4
- package/commandRegistry.ts +2 -2
- 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/server/levelAuthorityStore.ts +20 -1
- 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/machineWsRunDispatch.ts +51 -6
- package/offlineMarxistsAgentCommand.ts +6 -4
- package/package.json +430 -3
- 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,477 @@
|
|
|
1
|
+
import { getAllServers } from "./common";
|
|
2
|
+
import {
|
|
3
|
+
noloDeleteRequest,
|
|
4
|
+
noloPatchRequest,
|
|
5
|
+
noloUploadRequest,
|
|
6
|
+
noloWriteRequest,
|
|
7
|
+
syncWithServers,
|
|
8
|
+
} from "../requests";
|
|
9
|
+
import { planServersForTenant } from "../tenantPlacement";
|
|
10
|
+
import { resolveRecordAuthority } from "../authority/recordAuthority";
|
|
11
|
+
import { planAuthorityReadServers } from "./readResolution";
|
|
12
|
+
|
|
13
|
+
const isReadonlyPublicRecordKey = (dbKey: string): boolean =>
|
|
14
|
+
dbKey.startsWith("agent-pub-") || dbKey.startsWith("cybot-pub-");
|
|
15
|
+
|
|
16
|
+
const normalizeCurrentUserId = (state: any): string | null => {
|
|
17
|
+
const userId = state?.auth?.currentUser?.userId;
|
|
18
|
+
return typeof userId === "string" && userId.trim().length > 0 ? userId.trim() : null;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
export const resolveReplicationServers = (
|
|
22
|
+
currentServer: string | undefined,
|
|
23
|
+
syncServers: string[] | undefined,
|
|
24
|
+
preferredServerOrigin?: string | null
|
|
25
|
+
): string[] => getAllServers(currentServer, syncServers, preferredServerOrigin);
|
|
26
|
+
|
|
27
|
+
export const resolveAuthorityReplicationServers = ({
|
|
28
|
+
currentServer,
|
|
29
|
+
syncServers,
|
|
30
|
+
preferredServerOrigin,
|
|
31
|
+
dbKey,
|
|
32
|
+
record,
|
|
33
|
+
state,
|
|
34
|
+
}: {
|
|
35
|
+
currentServer: string | undefined;
|
|
36
|
+
syncServers: string[] | undefined;
|
|
37
|
+
preferredServerOrigin?: string | null;
|
|
38
|
+
dbKey: string;
|
|
39
|
+
record?: any;
|
|
40
|
+
state: any;
|
|
41
|
+
}): string[] => {
|
|
42
|
+
const allServers = resolveReplicationServers(
|
|
43
|
+
currentServer,
|
|
44
|
+
syncServers,
|
|
45
|
+
preferredServerOrigin
|
|
46
|
+
);
|
|
47
|
+
const authority = resolveRecordAuthority({
|
|
48
|
+
dbKey,
|
|
49
|
+
record,
|
|
50
|
+
currentUserId: state?.auth?.currentUser?.userId,
|
|
51
|
+
currentServer,
|
|
52
|
+
userAuthorityRegistry:
|
|
53
|
+
state?.settings?.userAuthorityRegistry ??
|
|
54
|
+
state?.auth?.currentUser?.authorityRegistry,
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
return planAuthorityReadServers({
|
|
58
|
+
allServers,
|
|
59
|
+
authorityServer: preferredServerOrigin ?? authority.authorityServer,
|
|
60
|
+
serverOrigin: authority.serverOrigin,
|
|
61
|
+
});
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
export const scheduleWriteReplication = (
|
|
65
|
+
servers: string[],
|
|
66
|
+
request: { data: any; customKey: string; userId?: string },
|
|
67
|
+
state: any
|
|
68
|
+
) => {
|
|
69
|
+
if (servers.length === 0) return;
|
|
70
|
+
Promise.resolve().then(async () => {
|
|
71
|
+
const [primaryServer, ...backupServers] = servers;
|
|
72
|
+
const primarySucceeded = await noloWriteRequest(primaryServer, request, state);
|
|
73
|
+
|
|
74
|
+
if (!primarySucceeded) {
|
|
75
|
+
console.warn(`Primary write sync failed for ${request.customKey} on ${primaryServer}`);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
if (backupServers.length === 0) {
|
|
79
|
+
return;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
syncWithServers(
|
|
83
|
+
backupServers,
|
|
84
|
+
(server, requestConfig, requestState, signal) =>
|
|
85
|
+
noloWriteRequest(server, requestConfig, requestState, signal, {
|
|
86
|
+
failureLogLevel: "info",
|
|
87
|
+
}),
|
|
88
|
+
`Backup write sync failed for ${request.customKey} on`,
|
|
89
|
+
request,
|
|
90
|
+
state
|
|
91
|
+
);
|
|
92
|
+
});
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
export const resolveTenantReplicationServers = ({
|
|
96
|
+
currentServer,
|
|
97
|
+
syncServers,
|
|
98
|
+
tenantId,
|
|
99
|
+
}: {
|
|
100
|
+
currentServer: string | undefined;
|
|
101
|
+
syncServers: string[] | undefined;
|
|
102
|
+
tenantId: string | null | undefined;
|
|
103
|
+
}): string[] => {
|
|
104
|
+
const allServers = resolveReplicationServers(currentServer, syncServers);
|
|
105
|
+
if (allServers.length === 0) {
|
|
106
|
+
return [];
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
return planServersForTenant(allServers, currentServer, tenantId);
|
|
110
|
+
};
|
|
111
|
+
|
|
112
|
+
export const resolveUploadReplicationServers = ({
|
|
113
|
+
currentServer,
|
|
114
|
+
syncServers,
|
|
115
|
+
tenantId,
|
|
116
|
+
uploadConfig,
|
|
117
|
+
state,
|
|
118
|
+
}: {
|
|
119
|
+
currentServer: string | undefined;
|
|
120
|
+
syncServers: string[] | undefined;
|
|
121
|
+
tenantId: string | null | undefined;
|
|
122
|
+
uploadConfig: {
|
|
123
|
+
metadata: any;
|
|
124
|
+
customKey: string;
|
|
125
|
+
userId?: string;
|
|
126
|
+
};
|
|
127
|
+
state: any;
|
|
128
|
+
}): string[] => {
|
|
129
|
+
const allServers = resolveReplicationServers(currentServer, syncServers);
|
|
130
|
+
const authority = resolveRecordAuthority({
|
|
131
|
+
dbKey: uploadConfig.customKey,
|
|
132
|
+
record: uploadConfig.metadata,
|
|
133
|
+
currentUserId: state?.auth?.currentUser?.userId,
|
|
134
|
+
currentServer,
|
|
135
|
+
userAuthorityRegistry:
|
|
136
|
+
state?.settings?.userAuthorityRegistry ??
|
|
137
|
+
state?.auth?.currentUser?.authorityRegistry,
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
if (authority.ownerUserId || authority.authorityServer) {
|
|
141
|
+
return planAuthorityReadServers({
|
|
142
|
+
allServers,
|
|
143
|
+
authorityServer: authority.authorityServer,
|
|
144
|
+
serverOrigin: authority.serverOrigin,
|
|
145
|
+
});
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
return resolveTenantReplicationServers({
|
|
149
|
+
currentServer,
|
|
150
|
+
syncServers,
|
|
151
|
+
tenantId,
|
|
152
|
+
});
|
|
153
|
+
};
|
|
154
|
+
|
|
155
|
+
export const scheduleExistingRecordReplication = ({
|
|
156
|
+
currentServer,
|
|
157
|
+
syncServers,
|
|
158
|
+
preferredServerOrigin,
|
|
159
|
+
dbKey,
|
|
160
|
+
localData,
|
|
161
|
+
state,
|
|
162
|
+
}: {
|
|
163
|
+
currentServer: string | undefined;
|
|
164
|
+
syncServers: string[] | undefined;
|
|
165
|
+
preferredServerOrigin?: string | null;
|
|
166
|
+
dbKey: string;
|
|
167
|
+
localData: any;
|
|
168
|
+
state: any;
|
|
169
|
+
}): string[] => {
|
|
170
|
+
if (isReadonlyPublicRecordKey(dbKey)) {
|
|
171
|
+
return [];
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
const currentUserId = normalizeCurrentUserId(state);
|
|
175
|
+
const authority = resolveRecordAuthority({
|
|
176
|
+
dbKey,
|
|
177
|
+
record: localData,
|
|
178
|
+
currentUserId,
|
|
179
|
+
currentServer,
|
|
180
|
+
userAuthorityRegistry:
|
|
181
|
+
state?.settings?.userAuthorityRegistry ??
|
|
182
|
+
state?.auth?.currentUser?.authorityRegistry,
|
|
183
|
+
});
|
|
184
|
+
if (authority.ownerUserId && currentUserId && authority.ownerUserId !== currentUserId) {
|
|
185
|
+
return [];
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
const servers = resolveAuthorityReplicationServers({
|
|
189
|
+
currentServer,
|
|
190
|
+
syncServers,
|
|
191
|
+
preferredServerOrigin,
|
|
192
|
+
dbKey,
|
|
193
|
+
record: localData,
|
|
194
|
+
state,
|
|
195
|
+
});
|
|
196
|
+
if (servers.length === 0) {
|
|
197
|
+
return [];
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
scheduleWriteReplication(
|
|
201
|
+
servers,
|
|
202
|
+
{
|
|
203
|
+
data: localData,
|
|
204
|
+
customKey: dbKey,
|
|
205
|
+
userId:
|
|
206
|
+
typeof localData?.userId === "string"
|
|
207
|
+
? localData.userId
|
|
208
|
+
: state?.auth?.currentUser?.userId,
|
|
209
|
+
},
|
|
210
|
+
state
|
|
211
|
+
);
|
|
212
|
+
|
|
213
|
+
return servers;
|
|
214
|
+
};
|
|
215
|
+
|
|
216
|
+
export const schedulePatchReplication = ({
|
|
217
|
+
servers,
|
|
218
|
+
dbKey,
|
|
219
|
+
changes,
|
|
220
|
+
state,
|
|
221
|
+
preferredServerOrigin,
|
|
222
|
+
}: {
|
|
223
|
+
servers: string[];
|
|
224
|
+
dbKey: string;
|
|
225
|
+
changes: any;
|
|
226
|
+
state: any;
|
|
227
|
+
preferredServerOrigin?: string | null;
|
|
228
|
+
}) => {
|
|
229
|
+
if (servers.length === 0) return;
|
|
230
|
+
|
|
231
|
+
Promise.resolve().then(async () => {
|
|
232
|
+
const primaryServer =
|
|
233
|
+
typeof preferredServerOrigin === "string" && preferredServerOrigin.trim().length > 0
|
|
234
|
+
? preferredServerOrigin.trim().replace(/\/+$/, "")
|
|
235
|
+
: servers[0];
|
|
236
|
+
const backupServers = servers.filter(
|
|
237
|
+
(server) => server.replace(/\/+$/, "") !== primaryServer
|
|
238
|
+
);
|
|
239
|
+
|
|
240
|
+
const primarySucceeded = await noloPatchRequest(primaryServer, dbKey, changes, state, undefined, {
|
|
241
|
+
failureLogLevel: "warn",
|
|
242
|
+
});
|
|
243
|
+
if (!primarySucceeded) {
|
|
244
|
+
console.warn(`Primary patch sync failed for ${dbKey} on ${primaryServer}`);
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
if (backupServers.length > 0) {
|
|
248
|
+
syncWithServers(
|
|
249
|
+
backupServers,
|
|
250
|
+
(server, targetDbKey, nextChanges, requestState, signal) =>
|
|
251
|
+
noloPatchRequest(server, targetDbKey, nextChanges, requestState, signal, {
|
|
252
|
+
failureLogLevel: "info",
|
|
253
|
+
}),
|
|
254
|
+
`Backup patch sync failed for ${dbKey} on`,
|
|
255
|
+
dbKey,
|
|
256
|
+
changes,
|
|
257
|
+
state
|
|
258
|
+
);
|
|
259
|
+
}
|
|
260
|
+
});
|
|
261
|
+
};
|
|
262
|
+
|
|
263
|
+
export const scheduleConfiguredPatchReplication = ({
|
|
264
|
+
currentServer,
|
|
265
|
+
syncServers,
|
|
266
|
+
preferredServerOrigin,
|
|
267
|
+
dbKey,
|
|
268
|
+
changes,
|
|
269
|
+
state,
|
|
270
|
+
}: {
|
|
271
|
+
currentServer: string | undefined;
|
|
272
|
+
syncServers: string[] | undefined;
|
|
273
|
+
preferredServerOrigin?: string | null;
|
|
274
|
+
dbKey: string;
|
|
275
|
+
changes: any;
|
|
276
|
+
state: any;
|
|
277
|
+
}): string[] => {
|
|
278
|
+
const servers = resolveAuthorityReplicationServers({
|
|
279
|
+
currentServer,
|
|
280
|
+
syncServers,
|
|
281
|
+
preferredServerOrigin,
|
|
282
|
+
dbKey,
|
|
283
|
+
record: changes,
|
|
284
|
+
state,
|
|
285
|
+
});
|
|
286
|
+
|
|
287
|
+
if (servers.length === 0) {
|
|
288
|
+
return [];
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
schedulePatchReplication({
|
|
292
|
+
servers,
|
|
293
|
+
dbKey,
|
|
294
|
+
changes,
|
|
295
|
+
state,
|
|
296
|
+
preferredServerOrigin,
|
|
297
|
+
});
|
|
298
|
+
|
|
299
|
+
return servers;
|
|
300
|
+
};
|
|
301
|
+
|
|
302
|
+
export const scheduleUploadReplication = ({
|
|
303
|
+
currentServer,
|
|
304
|
+
syncServers,
|
|
305
|
+
tenantId,
|
|
306
|
+
uploadConfig,
|
|
307
|
+
state,
|
|
308
|
+
excludeServers = [],
|
|
309
|
+
}: {
|
|
310
|
+
currentServer: string | undefined;
|
|
311
|
+
syncServers: string[] | undefined;
|
|
312
|
+
tenantId: string | null | undefined;
|
|
313
|
+
uploadConfig: {
|
|
314
|
+
file: File;
|
|
315
|
+
metadata: any;
|
|
316
|
+
customKey: string;
|
|
317
|
+
userId?: string;
|
|
318
|
+
};
|
|
319
|
+
state: any;
|
|
320
|
+
excludeServers?: string[];
|
|
321
|
+
}): string[] => {
|
|
322
|
+
const servers = resolveUploadReplicationServers({
|
|
323
|
+
currentServer,
|
|
324
|
+
syncServers,
|
|
325
|
+
tenantId,
|
|
326
|
+
uploadConfig,
|
|
327
|
+
state,
|
|
328
|
+
});
|
|
329
|
+
const excluded = new Set(
|
|
330
|
+
excludeServers
|
|
331
|
+
.filter((server): server is string => typeof server === "string")
|
|
332
|
+
.map((server) => server.trim().replace(/\/+$/, ""))
|
|
333
|
+
);
|
|
334
|
+
const remainingServers = servers.filter(
|
|
335
|
+
(server) => !excluded.has(server.trim().replace(/\/+$/, ""))
|
|
336
|
+
);
|
|
337
|
+
|
|
338
|
+
if (remainingServers.length === 0) {
|
|
339
|
+
return [];
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
Promise.resolve().then(() => {
|
|
343
|
+
syncWithServers(
|
|
344
|
+
remainingServers,
|
|
345
|
+
noloUploadRequest,
|
|
346
|
+
`Upload sync failed for ${uploadConfig.customKey} on`,
|
|
347
|
+
uploadConfig,
|
|
348
|
+
state
|
|
349
|
+
);
|
|
350
|
+
});
|
|
351
|
+
|
|
352
|
+
return remainingServers;
|
|
353
|
+
};
|
|
354
|
+
|
|
355
|
+
export const uploadToCurrentServer = async ({
|
|
356
|
+
currentServer,
|
|
357
|
+
uploadConfig,
|
|
358
|
+
state,
|
|
359
|
+
}: {
|
|
360
|
+
currentServer: string | undefined;
|
|
361
|
+
uploadConfig: {
|
|
362
|
+
file: File;
|
|
363
|
+
metadata: any;
|
|
364
|
+
customKey: string;
|
|
365
|
+
userId?: string;
|
|
366
|
+
};
|
|
367
|
+
state: any;
|
|
368
|
+
}): Promise<boolean> => {
|
|
369
|
+
if (!currentServer) {
|
|
370
|
+
return false;
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
return noloUploadRequest(currentServer, uploadConfig, state);
|
|
374
|
+
};
|
|
375
|
+
|
|
376
|
+
export const deleteFromReplicationServers = async ({
|
|
377
|
+
servers,
|
|
378
|
+
dbKey,
|
|
379
|
+
deleteOptions = { type: "single" as const },
|
|
380
|
+
state,
|
|
381
|
+
preferredServerOrigin,
|
|
382
|
+
}: {
|
|
383
|
+
servers: string[];
|
|
384
|
+
dbKey: string;
|
|
385
|
+
deleteOptions?: { type: "single" | "table" | "messages" };
|
|
386
|
+
state: any;
|
|
387
|
+
preferredServerOrigin?: string | null;
|
|
388
|
+
}): Promise<{ succeeded: string[]; failed: string[] }> => {
|
|
389
|
+
if (!servers.length) {
|
|
390
|
+
return { succeeded: [], failed: [] };
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
const preferredServer =
|
|
394
|
+
typeof preferredServerOrigin === "string" && preferredServerOrigin.trim().length > 0
|
|
395
|
+
? preferredServerOrigin.trim().replace(/\/+$/, "")
|
|
396
|
+
: null;
|
|
397
|
+
const remainingServers = preferredServer
|
|
398
|
+
? servers.filter((server) => server.replace(/\/+$/, "") !== preferredServer)
|
|
399
|
+
: servers;
|
|
400
|
+
const succeeded: string[] = [];
|
|
401
|
+
const failed: string[] = [];
|
|
402
|
+
|
|
403
|
+
if (preferredServer) {
|
|
404
|
+
const ok = await noloDeleteRequest(preferredServer, dbKey, deleteOptions, state);
|
|
405
|
+
if (ok) {
|
|
406
|
+
succeeded.push(preferredServer);
|
|
407
|
+
} else {
|
|
408
|
+
failed.push(preferredServer);
|
|
409
|
+
}
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
if (remainingServers.length > 0) {
|
|
413
|
+
const results = await Promise.all(
|
|
414
|
+
remainingServers.map(async (server) => ({
|
|
415
|
+
server,
|
|
416
|
+
ok: await noloDeleteRequest(server, dbKey, deleteOptions, state),
|
|
417
|
+
}))
|
|
418
|
+
);
|
|
419
|
+
results.forEach(({ server, ok }) => {
|
|
420
|
+
if (ok) succeeded.push(server);
|
|
421
|
+
else failed.push(server);
|
|
422
|
+
});
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
return { succeeded, failed };
|
|
426
|
+
};
|
|
427
|
+
|
|
428
|
+
export const scheduleDeleteReplication = ({
|
|
429
|
+
currentServer,
|
|
430
|
+
syncServers,
|
|
431
|
+
preferredServerOrigin,
|
|
432
|
+
dbKey,
|
|
433
|
+
deleteOptions,
|
|
434
|
+
state,
|
|
435
|
+
onResult,
|
|
436
|
+
onError,
|
|
437
|
+
}: {
|
|
438
|
+
currentServer: string | undefined;
|
|
439
|
+
syncServers: string[] | undefined;
|
|
440
|
+
preferredServerOrigin?: string | null;
|
|
441
|
+
dbKey: string;
|
|
442
|
+
deleteOptions?: { type: "single" | "table" | "messages" };
|
|
443
|
+
state: any;
|
|
444
|
+
onResult?: (result: { succeeded: string[]; failed: string[] }) => void;
|
|
445
|
+
onError?: (error: unknown) => void;
|
|
446
|
+
}): string[] => {
|
|
447
|
+
const servers = resolveAuthorityReplicationServers({
|
|
448
|
+
currentServer,
|
|
449
|
+
syncServers,
|
|
450
|
+
preferredServerOrigin,
|
|
451
|
+
dbKey,
|
|
452
|
+
state,
|
|
453
|
+
});
|
|
454
|
+
|
|
455
|
+
if (servers.length === 0) {
|
|
456
|
+
return [];
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
void Promise.resolve()
|
|
460
|
+
.then(() =>
|
|
461
|
+
deleteFromReplicationServers({
|
|
462
|
+
servers,
|
|
463
|
+
dbKey,
|
|
464
|
+
deleteOptions,
|
|
465
|
+
state,
|
|
466
|
+
preferredServerOrigin,
|
|
467
|
+
})
|
|
468
|
+
)
|
|
469
|
+
.then((result) => {
|
|
470
|
+
onResult?.(result);
|
|
471
|
+
})
|
|
472
|
+
.catch((error) => {
|
|
473
|
+
onError?.(error);
|
|
474
|
+
});
|
|
475
|
+
|
|
476
|
+
return servers;
|
|
477
|
+
};
|
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
// 文件路径: database/actions/upload.ts
|
|
2
|
+
|
|
3
|
+
import { getRuntimeServerContext } from "../runtimeServerContext";
|
|
4
|
+
import { ulid } from "../utils/ulid";
|
|
5
|
+
import { normalizeTimeFields, logger } from "./common";
|
|
6
|
+
import { toast } from "../../app/utils/toast";
|
|
7
|
+
import { saveFileToIndexedDb } from "../fileStorage";
|
|
8
|
+
import { fileKey } from "../keys";
|
|
9
|
+
import {
|
|
10
|
+
resolveUploadReplicationServers,
|
|
11
|
+
scheduleUploadReplication,
|
|
12
|
+
uploadToCurrentServer,
|
|
13
|
+
} from "./replication";
|
|
14
|
+
import { DataType } from "../../create/types";
|
|
15
|
+
import { resolveFileCategory } from "../../app/utils/fileUtils";
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* 辅助函数:保存文件元数据到客户端数据库
|
|
19
|
+
*/
|
|
20
|
+
const saveToClientDb = async (
|
|
21
|
+
clientDb: any,
|
|
22
|
+
dbKey: string,
|
|
23
|
+
metadata: any
|
|
24
|
+
): Promise<void> => {
|
|
25
|
+
if (!clientDb) {
|
|
26
|
+
logger.error({ dbKey }, "Client database is undefined in saveToClientDb");
|
|
27
|
+
throw new Error("Client database instance is required");
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
try {
|
|
31
|
+
await clientDb.put(dbKey, metadata);
|
|
32
|
+
logger.debug(
|
|
33
|
+
{ dbKey },
|
|
34
|
+
"File metadata saved successfully to local database."
|
|
35
|
+
);
|
|
36
|
+
} catch (err: any) {
|
|
37
|
+
logger.error(
|
|
38
|
+
{ err, dbKey },
|
|
39
|
+
"Failed to save file metadata to local database"
|
|
40
|
+
);
|
|
41
|
+
throw new Error(`Local database put failed for ${dbKey}: ${err.message}`);
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Upload File Action: 上传文件并保存元数据。
|
|
47
|
+
*
|
|
48
|
+
* 设计要点:
|
|
49
|
+
* - 以 fileId 作为文件唯一 ID;
|
|
50
|
+
* - 本地 IndexedDB 完整缓存一份(离线可用);
|
|
51
|
+
* - 按 tenantId(通常为 userId)通过 hash ring 选择若干服务器写入完整副本;
|
|
52
|
+
* - 服务器只需跑单机 fileService,不感知 ring/tenant。
|
|
53
|
+
*
|
|
54
|
+
* 将来扩容到几十台服务器:
|
|
55
|
+
* - 只需要在设置里增加/调整 syncServers 列表;
|
|
56
|
+
* - getAllServers + planServersForTenant 会自动把新节点纳入分布;
|
|
57
|
+
* - 无需修改业务调用代码。
|
|
58
|
+
*/
|
|
59
|
+
export const uploadFileAction = async (
|
|
60
|
+
uploadConfig: { file: File; customKey?: string; userId?: string },
|
|
61
|
+
thunkApi: any
|
|
62
|
+
): Promise<any> => {
|
|
63
|
+
const { db: clientDb } = thunkApi.extra;
|
|
64
|
+
const state = thunkApi.getState();
|
|
65
|
+
const { currentServer, syncServers, currentUserId } =
|
|
66
|
+
getRuntimeServerContext(state);
|
|
67
|
+
|
|
68
|
+
const { file, customKey } = uploadConfig;
|
|
69
|
+
const userId = uploadConfig.userId || currentUserId;
|
|
70
|
+
// 1. 验证参数
|
|
71
|
+
if (!file) {
|
|
72
|
+
const errorMsg =
|
|
73
|
+
"Invalid arguments for uploadFileAction: file is required.";
|
|
74
|
+
logger.error(errorMsg, { uploadConfig });
|
|
75
|
+
toast.error(errorMsg);
|
|
76
|
+
throw new Error(errorMsg);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
try {
|
|
80
|
+
// 2. 生成文件 ID 和文件名(fileId 将作为逻辑 ID,服务端会沿用)
|
|
81
|
+
const fileId = ulid();
|
|
82
|
+
const fileExtension = file.name.split(".").pop() || "";
|
|
83
|
+
const fileName = `${fileId}${fileExtension ? "." + fileExtension : ""}`;
|
|
84
|
+
|
|
85
|
+
// 决定最终的 dbKey (强制使用 file-userId-ulid 模式)
|
|
86
|
+
let finalDbKey = customKey;
|
|
87
|
+
if (!finalDbKey || !finalDbKey.startsWith("file-")) {
|
|
88
|
+
const actualUserId = userId || "unknown";
|
|
89
|
+
if (actualUserId === "unknown") {
|
|
90
|
+
console.warn("[uploadFileAction] User ID is unknown during upload. Key will be file-unknown.");
|
|
91
|
+
}
|
|
92
|
+
finalDbKey = fileKey.single(actualUserId, fileId);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
// 3. 准备文件元数据(添加时间戳、dbKey、userId 等)
|
|
96
|
+
const fileMetadata = normalizeTimeFields({
|
|
97
|
+
id: fileId,
|
|
98
|
+
title: file.name,
|
|
99
|
+
originalName: file.name,
|
|
100
|
+
fileName,
|
|
101
|
+
filePath: "",
|
|
102
|
+
size: file.size,
|
|
103
|
+
mimeType: file.type || "application/octet-stream",
|
|
104
|
+
type: DataType.FILE,
|
|
105
|
+
fileCategory: resolveFileCategory({
|
|
106
|
+
mimeType: file.type,
|
|
107
|
+
fileName: file.name,
|
|
108
|
+
}),
|
|
109
|
+
dbKey: finalDbKey,
|
|
110
|
+
userId,
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
// 本地结构化元数据:key = finalDbKey(供 dbSlice 等使用)
|
|
114
|
+
await saveToClientDb(clientDb, finalDbKey, fileMetadata);
|
|
115
|
+
|
|
116
|
+
// 4. 将原始文件存入 IndexedDB / Native Storage
|
|
117
|
+
// 本地以 fileId 为 key 缓存内容(离线使用)
|
|
118
|
+
// 在 RN 环境下,saveFileToIndexedDb 实际上是存储文件路径引用
|
|
119
|
+
try {
|
|
120
|
+
await saveFileToIndexedDb(fileId, file);
|
|
121
|
+
} catch (err) {
|
|
122
|
+
logger.warn(
|
|
123
|
+
{ err, fileId },
|
|
124
|
+
"[uploadFileAction] Failed to cache file locally."
|
|
125
|
+
);
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
// 5. 基于用户 authority 选择 primary;无 authority 时退回 tenant placement。
|
|
129
|
+
// - 用户搬家或自建服务器时,文件元数据和内容先写入 owner authority
|
|
130
|
+
// - 副本仍由 syncServers 提供冗余
|
|
131
|
+
const tenantId = userId || "default";
|
|
132
|
+
const uploadReplicationConfig = {
|
|
133
|
+
file,
|
|
134
|
+
metadata: fileMetadata,
|
|
135
|
+
customKey: finalDbKey,
|
|
136
|
+
userId,
|
|
137
|
+
};
|
|
138
|
+
|
|
139
|
+
const uploadServers = resolveUploadReplicationServers({
|
|
140
|
+
currentServer,
|
|
141
|
+
syncServers,
|
|
142
|
+
tenantId,
|
|
143
|
+
uploadConfig: uploadReplicationConfig,
|
|
144
|
+
state,
|
|
145
|
+
});
|
|
146
|
+
const primaryUploadServer = uploadServers[0] ?? currentServer;
|
|
147
|
+
const primaryUploadSucceeded = await uploadToCurrentServer({
|
|
148
|
+
currentServer: primaryUploadServer,
|
|
149
|
+
uploadConfig: uploadReplicationConfig,
|
|
150
|
+
state,
|
|
151
|
+
});
|
|
152
|
+
if (primaryUploadServer && !primaryUploadSucceeded) {
|
|
153
|
+
throw new Error(`Primary upload failed on authority server ${primaryUploadServer}`);
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
const serversToUse = scheduleUploadReplication({
|
|
157
|
+
currentServer,
|
|
158
|
+
syncServers,
|
|
159
|
+
tenantId,
|
|
160
|
+
uploadConfig: uploadReplicationConfig,
|
|
161
|
+
state,
|
|
162
|
+
excludeServers: primaryUploadServer ? [primaryUploadServer] : [],
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
if (!primaryUploadServer && !serversToUse.length) {
|
|
166
|
+
logger.warn(
|
|
167
|
+
"[uploadFileAction] No replication servers available, file metadata only saved locally.",
|
|
168
|
+
{ finalDbKey, fileName, tenantId }
|
|
169
|
+
);
|
|
170
|
+
return fileMetadata;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
logger.debug(
|
|
174
|
+
`[uploadFileAction] Uploaded primary copy for ${fileName} and scheduled background sync to ${serversToUse.length} additional servers.`,
|
|
175
|
+
{ primaryUploadServer, currentServer, serversToUse, tenantId }
|
|
176
|
+
);
|
|
177
|
+
// 8. 返回本地保存的元数据
|
|
178
|
+
return fileMetadata;
|
|
179
|
+
} catch (error: any) {
|
|
180
|
+
const errorMessage = `Upload action failed for ${customKey}: ${error?.message || "Unknown error"
|
|
181
|
+
}`;
|
|
182
|
+
logger.error({ error }, "[uploadFileAction] Error");
|
|
183
|
+
toast.error(`Failed to upload file for ${customKey}.`);
|
|
184
|
+
throw new Error(errorMessage);
|
|
185
|
+
}
|
|
186
|
+
};
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
// src/database/actions/upsert.ts
|
|
2
|
+
import { toast } from "../../app/utils/toast";
|
|
3
|
+
import type { AppThunkApi } from "../../app/store";
|
|
4
|
+
|
|
5
|
+
import { patchAction } from "./patch";
|
|
6
|
+
import { readAction } from "./read";
|
|
7
|
+
import { writeAction } from "./write";
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Upsert 数据协调器:根据数据是否存在,调度 patch 或 write 操作。
|
|
11
|
+
* 此操作本身不执行数据库读写或网络请求,而是委托给其他 async thunks。
|
|
12
|
+
*
|
|
13
|
+
* @param upsertConfig 包含 dbKey(必需)和 data(必需)的对象。
|
|
14
|
+
* @param thunkApi Redux Thunk API,包含 dispatch 和 getState。
|
|
15
|
+
* @returns Promise<any> 来自 patch或write操作成功后的数据对象。
|
|
16
|
+
* @throws 如果参数无效或调度的操作失败,则抛出错误。
|
|
17
|
+
*/
|
|
18
|
+
export const upsertAction = async (
|
|
19
|
+
upsertConfig: { dbKey: string; data: any },
|
|
20
|
+
thunkApi: AppThunkApi
|
|
21
|
+
): Promise<any> => {
|
|
22
|
+
const { dbKey, data } = upsertConfig;
|
|
23
|
+
|
|
24
|
+
// 1. 参数验证
|
|
25
|
+
if (!dbKey || !data || typeof data !== "object") {
|
|
26
|
+
const errorMsg = "upsertAction 参数无效:dbKey 和 data 对象是必需的。";
|
|
27
|
+
toast.error(errorMsg);
|
|
28
|
+
throw new Error(errorMsg);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
try {
|
|
32
|
+
// 2. 调用 readAction 检查数据是否存在于本地(通过 Redux Store 或 DB)
|
|
33
|
+
// 我们不关心 readAction 是否真的从网络读取,只关心它最终返回的结果。
|
|
34
|
+
const existingData = await readAction({ dbKey }, thunkApi);
|
|
35
|
+
let finalResult;
|
|
36
|
+
|
|
37
|
+
// 3. 根据是否存在数据,决定调度 patch 还是 write
|
|
38
|
+
if (existingData && Object.keys(existingData).length > 0) {
|
|
39
|
+
// **更新路径**:数据已存在,调度 patch action
|
|
40
|
+
// patch action 内部会处理 updatedAt、userId、本地写入和服务器同步
|
|
41
|
+
finalResult = await patchAction({ dbKey, changes: data }, thunkApi);
|
|
42
|
+
} else {
|
|
43
|
+
// **插入路径**:数据不存在,调度 write action
|
|
44
|
+
// write action 内部会处理 createdAt/updatedAt、userId、本地写入和服务器同步
|
|
45
|
+
finalResult = await writeAction({ data, customKey: dbKey }, thunkApi);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// 4. 返回最终执行成功的数据
|
|
49
|
+
return finalResult;
|
|
50
|
+
} catch (error: any) {
|
|
51
|
+
const errorMessage = `Upsert 协调操作失败 (dbKey: ${dbKey}): ${error.message || "未知错误"}`;
|
|
52
|
+
toast.error("数据保存失败,请稍后重试。");
|
|
53
|
+
// 重新抛出错误,以便 create.asyncThunk 能捕获到 rejected 状态
|
|
54
|
+
throw new Error(errorMessage, { cause: error });
|
|
55
|
+
}
|
|
56
|
+
};
|