nolo-cli 0.1.13 → 0.1.15
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +9 -2
- package/agent-runtime/hostAdapter.ts +53 -0
- package/agent-runtime/index.ts +28 -0
- package/agent-runtime/localLoop.ts +62 -0
- package/agent-runtime/runtimeDecision.ts +70 -0
- package/agent-runtime/types.ts +87 -0
- package/agentRunCommand.ts +104 -0
- package/agentRuntimeCommands.ts +139 -22
- package/agentRuntimeLocal.ts +7 -0
- package/ai/agent/_executeModel.ts +118 -0
- package/ai/agent/agentSlice.ts +544 -1
- package/ai/agent/appWorkingMemory.ts +126 -0
- package/ai/agent/avatarUtils.ts +24 -0
- package/ai/agent/buildEditingContext.ts +373 -0
- package/ai/agent/buildSystemPrompt.ts +532 -0
- package/ai/agent/cleanAgentMessages.ts +140 -0
- package/ai/agent/cliChatClient.ts +119 -0
- package/ai/agent/contextCompiler.ts +107 -0
- package/ai/agent/contextLayerContract.ts +44 -0
- package/ai/agent/createAgentSchema.ts +234 -0
- package/ai/agent/executeToolCall.ts +58 -0
- package/ai/agent/fetchAgentContexts.ts +42 -0
- package/ai/agent/generatePrompt.ts +3 -0
- package/ai/agent/getFullChatContextKeys.ts +168 -0
- package/ai/agent/hooks/fetchPublicAgents.ts +133 -0
- package/ai/agent/hooks/useAgentConfig.ts +61 -0
- package/ai/agent/hooks/useAgentDialog.ts +35 -0
- package/ai/agent/hooks/useAgentFormValidation.ts +202 -0
- package/ai/agent/hooks/usePublicAgents.ts +473 -0
- package/ai/agent/persistMessageWithFixedId.ts +37 -0
- package/ai/agent/planSlice.ts +259 -0
- package/ai/agent/referenceUtils.ts +229 -0
- package/ai/agent/runAgentBackground.ts +238 -0
- package/ai/agent/runAgentClientLoop.ts +138 -0
- package/ai/agent/runtimeGuidance.ts +97 -0
- package/ai/agent/runtimeServerBase.ts +37 -0
- package/ai/agent/server/fetchPublicAgents.ts +128 -0
- package/ai/agent/startParallelAgentStreams.ts +424 -0
- package/ai/agent/startupProtocol.ts +53 -0
- package/ai/agent/streamAgentChatTurn.ts +1299 -0
- package/ai/agent/streamAgentChatTurnUtils.ts +738 -0
- package/ai/agent/types.ts +71 -0
- package/ai/agent/utils/imageOutput.ts +39 -0
- package/ai/agent/utils/publicImageAgentMode.ts +26 -0
- package/ai/agent/utils/sortUtils.ts +250 -0
- package/ai/agent/web/referencePickerUtils.ts +146 -0
- package/ai/ai.locale.ts +1083 -0
- package/ai/chat/accumulateToolCallChunks.ts +95 -0
- package/ai/chat/fetchUtils.native.ts +276 -0
- package/ai/chat/fetchUtils.ts +153 -0
- package/ai/chat/inlineImageUrlsForCustomProvider.ts +117 -0
- package/ai/chat/parseApiError.ts +64 -0
- package/ai/chat/parseMultilineSSE.ts +95 -0
- package/ai/chat/sendOpenAICompletionsRequest.native.ts +682 -0
- package/ai/chat/sendOpenAICompletionsRequest.ts +712 -0
- package/ai/chat/sendOpenAIResponseRequest.ts +512 -0
- package/ai/chat/shouldUseServerProxy.ts +18 -0
- package/ai/chat/sseClient.native.ts +91 -0
- package/ai/chat/sseClient.ts +67 -0
- package/ai/chat/streamReader.native.ts +31 -0
- package/ai/chat/streamReader.ts +62 -0
- package/ai/chat/updateTotalUsage.ts +72 -0
- package/ai/context/buildReferenceContext.ts +437 -0
- package/ai/context/calculateContextUsage.ts +133 -0
- package/ai/context/retention.ts +165 -0
- package/ai/context/tokenUtils.ts +78 -0
- package/ai/index.ts +1 -1
- package/ai/llm/agentCapabilities.ts +74 -0
- package/ai/llm/calculateGeminiImageTokens.ts +57 -0
- package/ai/llm/deepinfra.ts +28 -0
- package/ai/llm/fireworks.ts +68 -0
- package/ai/llm/generateRequestBody.ts +165 -0
- package/ai/llm/getModelContextWindow.ts +84 -0
- package/ai/llm/getNoloKey.ts +37 -0
- package/ai/llm/getPricing.ts +232 -0
- package/ai/llm/hooks/useModelPricing.ts +75 -0
- package/ai/llm/imagePricing.ts +66 -0
- package/ai/llm/isResponseAPIModel.ts +13 -0
- package/ai/llm/kimi.ts +18 -0
- package/ai/llm/mimo.ts +71 -0
- package/ai/llm/mistral.ts +22 -0
- package/ai/llm/modelAvatar.ts +427 -0
- package/ai/llm/models.ts +45 -0
- package/ai/llm/openrouterModels.ts +141 -0
- package/ai/llm/providers.ts +307 -0
- package/ai/llm/reasoningModels.ts +28 -0
- package/ai/llm/types.ts +59 -0
- package/ai/llm/usageRequestOptions.ts +59 -0
- package/ai/memory/capture.ts +148 -0
- package/ai/memory/consolidate.ts +104 -0
- package/ai/memory/delete.ts +147 -0
- package/ai/memory/overlay.ts +84 -0
- package/ai/memory/query.ts +38 -0
- package/ai/memory/queryShared.ts +160 -0
- package/ai/memory/rank.ts +105 -0
- package/ai/memory/recentRelationshipRecap.ts +247 -0
- package/ai/memory/remember.ts +167 -0
- package/ai/memory/runtime.ts +76 -0
- package/ai/memory/store.ts +20 -0
- package/ai/memory/storeShared.ts +76 -0
- package/ai/memory/types.ts +46 -0
- package/ai/memory/understanding.ts +349 -0
- package/ai/memory/understandingGreeting.ts +264 -0
- package/ai/messages/type.ts +20 -0
- package/ai/policy/personalizationDialog.ts +333 -0
- package/ai/policy/runtimePolicy.ts +440 -0
- package/ai/policy/selfUpdateFields.ts +48 -0
- package/ai/policy/types.ts +64 -0
- package/ai/skills/referenceRuntime.ts +274 -0
- package/ai/skills/skillDiagnostics.ts +251 -0
- package/ai/skills/skillDocBuilder.ts +139 -0
- package/ai/skills/skillDocProtocol.ts +434 -0
- package/ai/skills/skillReferenceSummary.ts +63 -0
- package/ai/skills/skillSummaryMarker.ts +26 -0
- package/ai/token/calculatePrice.ts +546 -0
- package/ai/token/db.ts +98 -0
- package/ai/token/externalToolCost.ts +321 -0
- package/ai/token/hooks/useRecords.ts +65 -0
- package/ai/token/missingUsageEstimate.ts +42 -0
- package/ai/token/modelUsageQuery.ts +252 -0
- package/ai/token/normalizeUsage.ts +84 -0
- package/ai/token/openaiImageGenerationUsage.ts +56 -0
- package/ai/token/prepareTokenUsageData.ts +88 -0
- package/ai/token/query.ts +88 -0
- package/ai/token/queryUserTokens.ts +59 -0
- package/ai/token/resolveBillingTarget.ts +52 -0
- package/ai/token/saveTokenRecord.ts +53 -0
- package/ai/token/serverDialogProjection.ts +78 -0
- package/ai/token/serverTokenWriter.ts +143 -0
- package/ai/token/stats.ts +21 -0
- package/ai/token/tokenThunks.ts +24 -0
- package/ai/token/types.ts +93 -0
- package/ai/tools/agent/agentTools.ts +176 -0
- package/ai/tools/agent/agentUpdateShared.ts +311 -0
- package/ai/tools/agent/callAgentTool.ts +139 -0
- package/ai/tools/agent/createAgentTool.ts +512 -0
- package/ai/tools/agent/createDialogTool.ts +69 -0
- package/ai/tools/agent/createSkillAgentTool.ts +62 -0
- package/ai/tools/agent/parallelBudget.ts +221 -0
- package/ai/tools/agent/presets/appBuilderPreset.ts +147 -0
- package/ai/tools/agent/runLlmTool.ts +96 -0
- package/ai/tools/agent/runStreamingAgentTool.ts +73 -0
- package/ai/tools/agent/skillAgentArgs.ts +106 -0
- package/ai/tools/agent/skillAgentPreset.ts +89 -0
- package/ai/tools/agent/streamParallelAgentsTool.ts +122 -0
- package/ai/tools/agent/updateAgentTool.ts +96 -0
- package/ai/tools/agent/updateSelfTool.ts +113 -0
- package/ai/tools/amazonProductScraperTool.ts +86 -0
- package/ai/tools/apifyActorClient.ts +45 -0
- package/ai/tools/appEditGuard.ts +372 -0
- package/ai/tools/appReadSnapshot.ts +153 -0
- package/ai/tools/appTools.ts +1549 -0
- package/ai/tools/applyEditTool.ts +256 -0
- package/ai/tools/applyLineEditsTool.ts +312 -0
- package/ai/tools/browserTools/click.ts +33 -0
- package/ai/tools/browserTools/closeSession.ts +29 -0
- package/ai/tools/browserTools/common.ts +27 -0
- package/ai/tools/browserTools/openSession.ts +48 -0
- package/ai/tools/browserTools/readContent.ts +38 -0
- package/ai/tools/browserTools/selectOption.ts +46 -0
- package/ai/tools/browserTools/typeText.ts +42 -0
- package/ai/tools/category/createCategoryTool.ts +66 -0
- package/ai/tools/category/queryContentsByCategoryTool.ts +69 -0
- package/ai/tools/category/updateContentCategoryTool.ts +75 -0
- package/ai/tools/cfBrowserTools.ts +319 -0
- package/ai/tools/cfSpeechToTextTool.ts +49 -0
- package/ai/tools/checkEnvTool.ts +65 -0
- package/ai/tools/cloudflareCrawlTool.ts +289 -0
- package/ai/tools/codeSearchTool.ts +111 -0
- package/ai/tools/codeTools.ts +101 -0
- package/ai/tools/createDocTool.ts +132 -0
- package/ai/tools/createPlanTool.ts +999 -0
- package/ai/tools/createSkillDocTool.ts +155 -0
- package/ai/tools/createWorkflowTool.ts +154 -0
- package/ai/tools/deepseekOcrTool.ts +34 -0
- package/ai/tools/delayTool.ts +31 -0
- package/ai/tools/deleteSpacesTool.ts +325 -0
- package/ai/tools/deleteSpacesToolModel.ts +159 -0
- package/ai/tools/devReloadUtils.ts +29 -0
- package/ai/tools/dialogMessageSearch.ts +137 -0
- package/ai/tools/doctorSkillTool.ts +72 -0
- package/ai/tools/ecommerceScraperTool.ts +86 -0
- package/ai/tools/emailTools.ts +549 -0
- package/ai/tools/evalSkillTool.ts +92 -0
- package/ai/tools/exaSearchTool.ts +64 -0
- package/ai/tools/execBashTool.ts +379 -0
- package/ai/tools/executeSqlTool.ts +192 -0
- package/ai/tools/fetchWebpageSupport.ts +309 -0
- package/ai/tools/fetchWebpageTool.ts +84 -0
- package/ai/tools/geminiImagePreviewTool.ts +361 -0
- package/ai/tools/generateDocxTool.ts +215 -0
- package/ai/tools/googleSearchScraperTool.ts +106 -0
- package/ai/tools/importDataTool.ts +133 -0
- package/ai/tools/importSkillTool.ts +162 -0
- package/ai/tools/index.ts +1927 -0
- package/ai/tools/listFilesTool.ts +82 -0
- package/ai/tools/listUserSpacesTool.ts +113 -0
- package/ai/tools/modelUsageTools.ts +199 -0
- package/ai/tools/olmOcrTool.ts +34 -0
- package/ai/tools/openaiImageTool.ts +267 -0
- package/ai/tools/prepareTools.ts +23 -0
- package/ai/tools/readDocTool.ts +84 -0
- package/ai/tools/readFileTool.ts +211 -0
- package/ai/tools/readTool.ts +163 -0
- package/ai/tools/readXPostTool.ts +233 -0
- package/ai/tools/rememberMemoryTool.ts +84 -0
- package/ai/tools/remotionVideoTool.ts +151 -0
- package/ai/tools/searchDialogMessagesTool.ts +222 -0
- package/ai/tools/searchRepoTool.ts +115 -0
- package/ai/tools/searchWorkspaceTool.ts +259 -0
- package/ai/tools/skillFollowup.ts +86 -0
- package/ai/tools/surfWeatherTool.ts +169 -0
- package/ai/tools/table/addTableRowTool.ts +217 -0
- package/ai/tools/table/createTableTool.ts +315 -0
- package/ai/tools/table/rowTools.ts +366 -0
- package/ai/tools/table/schemaTools.ts +244 -0
- package/ai/tools/table/shareTableTool.ts +148 -0
- package/ai/tools/table/toolShared.ts +129 -0
- package/ai/tools/toolApiClient.ts +198 -0
- package/ai/tools/toolNameAliases.ts +57 -0
- package/ai/tools/toolResultError.ts +42 -0
- package/ai/tools/toolRunSlice.ts +303 -0
- package/ai/tools/toolSchemaCompatibility.ts +53 -0
- package/ai/tools/toolVisibility.ts +4 -0
- package/ai/tools/types.ts +20 -0
- package/ai/tools/uiAskChoiceTool.ts +104 -0
- package/ai/tools/updateContentTitleTool.ts +84 -0
- package/ai/tools/updateDocTool.ts +105 -0
- package/ai/tools/updateUserPreferenceProfileTool.ts +145 -0
- package/ai/tools/whisperTool.ts +77 -0
- package/ai/tools/writeFileTool.ts +210 -0
- package/ai/tools/youtubeScraperTool.ts +116 -0
- package/ai/tools/ziweiChartTool.ts +678 -0
- package/ai/types.ts +55 -0
- package/ai/workflow/workflowExecutor.ts +323 -0
- package/ai/workflow/workflowSlice.ts +73 -0
- package/ai/workflow/workflowTypes.ts +106 -0
- package/client/agentRun.test.ts +240 -0
- package/client/agentRun.ts +182 -19
- package/client/compactDialog.test.ts +238 -0
- package/client/localRuntimeAdapter.test.ts +135 -0
- package/client/localRuntimeAdapter.ts +244 -0
- package/client/profileConfig.test.ts +40 -0
- package/client/streamingOutput.test.ts +22 -0
- package/client/streamingOutput.ts +38 -0
- package/commandRegistry.ts +11 -2
- package/connector-experimental/index.ts +5 -0
- package/database/actions/cacheMergedUserData.ts +64 -0
- package/database/actions/common.ts +242 -0
- package/database/actions/deleteFile.ts +40 -0
- package/database/actions/fetchUserData.ts +16 -0
- package/database/actions/fileContent.ts +125 -0
- package/database/actions/patch.ts +155 -0
- package/database/actions/read.ts +337 -0
- package/database/actions/readAndWait.ts +224 -0
- package/database/actions/readRequestManager.ts +120 -0
- package/database/actions/remove.ts +94 -0
- package/database/actions/replication.ts +366 -0
- package/database/actions/upload.ts +174 -0
- package/database/actions/upsert.ts +56 -0
- package/database/actions/write.ts +126 -0
- package/database/client/db.native.ts +73 -0
- package/database/client/db.ts +51 -0
- package/database/client/fetchUserData.ts +61 -0
- package/database/client/handleError.ts +19 -0
- package/database/client/queryRequest.ts +21 -0
- package/database/config.ts +21 -0
- package/database/dbActionThunks.ts +1 -0
- package/database/dbSlice.ts +149 -0
- package/database/email.ts +42 -0
- package/database/fileRing.ts +51 -0
- package/database/fileSharding.ts +70 -0
- package/database/fileStorage.native.ts +92 -0
- package/database/fileStorage.ts +232 -0
- package/database/fileUrl.ts +34 -0
- package/database/hooks/useUserData.ts +489 -0
- package/database/index.ts +1 -0
- package/database/keys.ts +765 -0
- package/database/queryPrefixes.ts +14 -0
- package/database/requests.ts +443 -0
- package/database/runtimeServerContext.ts +35 -0
- package/database/server/MemoryDB.ts +76 -0
- package/database/server/actorAccess.ts +76 -0
- package/database/server/agentDelegation.ts +124 -0
- package/database/server/coreDataOwnership.ts +13 -0
- package/database/server/coreDataProxy.ts +76 -0
- package/database/server/cybotReadonly.ts +18 -0
- package/database/server/dataHandlers.ts +111 -0
- package/database/server/db.ts +118 -0
- package/database/server/dbPath.ts +20 -0
- package/database/server/delete.ts +499 -0
- package/database/server/emailRepository.ts +1480 -0
- package/database/server/ensureDbOpen.ts +12 -0
- package/database/server/fileRead.ts +337 -0
- package/database/server/fileService.ts +436 -0
- package/database/server/handleTransaction.ts +86 -0
- package/database/server/patch.ts +282 -0
- package/database/server/query.ts +138 -0
- package/database/server/read.ts +325 -0
- package/database/server/resourceAccess.ts +211 -0
- package/database/server/routes.ts +110 -0
- package/database/server/spaceMemberAuthority.ts +67 -0
- package/database/server/upload.ts +159 -0
- package/database/server/write.ts +494 -0
- package/database/server/writeAuthority.ts +133 -0
- package/database/sqliteDb.ts +46 -0
- package/database/table/deleteTable.ts +120 -0
- package/database/tenantPlacement.ts +57 -0
- package/database/tombstones.ts +52 -0
- package/database/userDataLoadDecision.ts +17 -0
- package/database/userDataMerge.ts +95 -0
- package/database/userPreferenceRegister.ts +108 -0
- package/database/utils/dbPath.ts +47 -0
- package/database/utils/ulid.native.ts +6 -0
- package/database/utils/ulid.ts +1 -0
- package/index.ts +37 -19
- package/localRuntimeDb.ts +28 -0
- package/package.json +17 -4
- package/runtimeModeArgs.ts +33 -0
- package/tui/readlineWorkspace.ts +1 -0
- package/tui/session.ts +22 -0
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
// 文件路径: database/actions/patch.ts
|
|
2
|
+
|
|
3
|
+
import type { AppThunkApi } from "app/store";
|
|
4
|
+
import { getRuntimeServerContext } from "database/runtimeServerContext";
|
|
5
|
+
import { toast } from "app/utils/toast";
|
|
6
|
+
import {
|
|
7
|
+
scheduleConfiguredPatchReplication,
|
|
8
|
+
} from "./replication";
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* 深度合并两个对象。源对象中的 null 值会删除目标对象中对应的键。
|
|
12
|
+
* @param target - 目标对象。
|
|
13
|
+
* @param source - 源对象,包含要应用的更改。
|
|
14
|
+
* @returns {any} - 合并后的新对象。
|
|
15
|
+
*/
|
|
16
|
+
const deepMerge = (target: any, source: any): any => {
|
|
17
|
+
const output = { ...target };
|
|
18
|
+
for (const key in source) {
|
|
19
|
+
if (Object.prototype.hasOwnProperty.call(source, key)) {
|
|
20
|
+
if (source[key] === null && key in output) {
|
|
21
|
+
delete output[key]; // null 值用于删除键
|
|
22
|
+
} else if (
|
|
23
|
+
source[key] &&
|
|
24
|
+
typeof source[key] === "object" &&
|
|
25
|
+
!Array.isArray(source[key])
|
|
26
|
+
) {
|
|
27
|
+
output[key] = deepMerge(output[key] || {}, source[key]); // 递归合并
|
|
28
|
+
} else {
|
|
29
|
+
output[key] = source[key]; // 直接赋值
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
return output;
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
const toTimestamp = (value: unknown): number => {
|
|
37
|
+
if (typeof value === "number" && Number.isFinite(value)) return value;
|
|
38
|
+
if (typeof value === "string") {
|
|
39
|
+
const parsed = Date.parse(value);
|
|
40
|
+
return Number.isFinite(parsed) ? parsed : 0;
|
|
41
|
+
}
|
|
42
|
+
return 0;
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
const inferNextUpdatedAt = (currentData: any): number | string | undefined => {
|
|
46
|
+
const previousUpdatedAt = currentData?.updatedAt;
|
|
47
|
+
const previousCreatedAt = currentData?.createdAt;
|
|
48
|
+
const previousMetaCreatedAt = currentData?.meta?.createdAt;
|
|
49
|
+
const previousTimestamp = Math.max(
|
|
50
|
+
toTimestamp(previousUpdatedAt),
|
|
51
|
+
toTimestamp(previousCreatedAt),
|
|
52
|
+
toTimestamp(previousMetaCreatedAt)
|
|
53
|
+
);
|
|
54
|
+
const nextTimestamp = Math.max(Date.now(), previousTimestamp + 1);
|
|
55
|
+
|
|
56
|
+
if (
|
|
57
|
+
typeof previousUpdatedAt === "number" ||
|
|
58
|
+
typeof previousCreatedAt === "number" ||
|
|
59
|
+
typeof previousMetaCreatedAt === "number"
|
|
60
|
+
) {
|
|
61
|
+
return nextTimestamp;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
if (
|
|
65
|
+
typeof previousUpdatedAt === "string" ||
|
|
66
|
+
typeof previousCreatedAt === "string"
|
|
67
|
+
) {
|
|
68
|
+
return new Date(nextTimestamp).toISOString();
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
return undefined;
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Patch Action: 对现有数据项应用增量更新。
|
|
76
|
+
* 1. 从本地数据库读取现有数据。
|
|
77
|
+
* 2. 将传入的 'changes' 对象与现有数据进行深度合并。
|
|
78
|
+
* 3. 将合并后的新数据写回本地数据库。
|
|
79
|
+
* 4. 异步地将 'changes' 对象同步到所有相关服务器。
|
|
80
|
+
* @param payload - 包含 dbKey 和 changes 的对象。
|
|
81
|
+
* @param {string} payload.dbKey - 要更新的数据的键。
|
|
82
|
+
* @param {object} payload.changes - 要应用的更改。
|
|
83
|
+
* @param thunkApi - Redux Thunk API,包含 state 和 extra arugments。
|
|
84
|
+
* @returns {Promise<any>} 更新后的完整数据对象。
|
|
85
|
+
* @throws 如果本地数据不存在或更新过程中发生任何错误,则抛出异常。
|
|
86
|
+
*/
|
|
87
|
+
export const patchAction = async (
|
|
88
|
+
{
|
|
89
|
+
dbKey,
|
|
90
|
+
changes,
|
|
91
|
+
preferredServerOrigin,
|
|
92
|
+
}: { dbKey: string; changes: any; preferredServerOrigin?: string | null },
|
|
93
|
+
thunkApi: AppThunkApi
|
|
94
|
+
): Promise<any> => {
|
|
95
|
+
// 1. 从 thunkApi.extra 中获取数据库实例
|
|
96
|
+
const { db } = thunkApi.extra;
|
|
97
|
+
if (!db) {
|
|
98
|
+
const errorMsg = "Database instance is not available.";
|
|
99
|
+
toast.error(errorMsg);
|
|
100
|
+
throw new Error(errorMsg);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
// 2. 验证输入参数
|
|
104
|
+
if (!dbKey || !changes || typeof changes !== "object") {
|
|
105
|
+
const errorMsg = "Patch action requires a valid dbKey and changes object.";
|
|
106
|
+
toast.error(errorMsg);
|
|
107
|
+
throw new Error(errorMsg);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
const state = thunkApi.getState();
|
|
111
|
+
const { currentServer, syncServers: configuredSyncServers } =
|
|
112
|
+
getRuntimeServerContext(state);
|
|
113
|
+
|
|
114
|
+
try {
|
|
115
|
+
// 3. 使用注入的 db 实例读取当前数据
|
|
116
|
+
const currentData = await db.get(dbKey);
|
|
117
|
+
if (!currentData) {
|
|
118
|
+
throw new Error(
|
|
119
|
+
`Cannot apply patch: Data not found locally for key: ${dbKey}.`
|
|
120
|
+
);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
const patchChanges = Object.prototype.hasOwnProperty.call(changes, "updatedAt")
|
|
124
|
+
? changes
|
|
125
|
+
: {
|
|
126
|
+
...changes,
|
|
127
|
+
...(inferNextUpdatedAt(currentData) !== undefined
|
|
128
|
+
? { updatedAt: inferNextUpdatedAt(currentData) }
|
|
129
|
+
: {}),
|
|
130
|
+
};
|
|
131
|
+
|
|
132
|
+
// 4. 合并数据并写回本地数据库
|
|
133
|
+
const newData = deepMerge(currentData, patchChanges);
|
|
134
|
+
const persistedData =
|
|
135
|
+
newData && typeof newData === "object" ? { ...newData, dbKey } : { dbKey };
|
|
136
|
+
await db.put(dbKey, persistedData);
|
|
137
|
+
|
|
138
|
+
// 5. 异步触发对远程服务器的同步(即发即忘)
|
|
139
|
+
scheduleConfiguredPatchReplication({
|
|
140
|
+
currentServer,
|
|
141
|
+
syncServers: configuredSyncServers,
|
|
142
|
+
preferredServerOrigin,
|
|
143
|
+
dbKey,
|
|
144
|
+
changes: patchChanges,
|
|
145
|
+
state,
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
// 6. 乐观地返回更新后的数据
|
|
149
|
+
return persistedData;
|
|
150
|
+
} catch (error: any) {
|
|
151
|
+
const errorMessage = `Failed to update data for ${dbKey}.`;
|
|
152
|
+
toast.error(errorMessage);
|
|
153
|
+
throw new Error(error.message || errorMessage);
|
|
154
|
+
}
|
|
155
|
+
};
|
|
@@ -0,0 +1,337 @@
|
|
|
1
|
+
// 文件路径: database/actions/read.ts
|
|
2
|
+
|
|
3
|
+
import type { AppThunkApi } from "app/store";
|
|
4
|
+
import { getRuntimeServerContext } from "database/runtimeServerContext";
|
|
5
|
+
import {
|
|
6
|
+
fetchFromClientDb,
|
|
7
|
+
fetchFromServer,
|
|
8
|
+
isReadTimeoutError,
|
|
9
|
+
logger,
|
|
10
|
+
} from "./common";
|
|
11
|
+
import { readRequestManager } from "./readRequestManager";
|
|
12
|
+
import { shouldReplaceWithNextRecord } from "../tombstones";
|
|
13
|
+
import { scheduleExistingRecordReplication } from "./replication";
|
|
14
|
+
|
|
15
|
+
// --- 辅助函数 ---
|
|
16
|
+
|
|
17
|
+
const updateClientDbIfNewer = async (
|
|
18
|
+
clientDb: any,
|
|
19
|
+
dbKey: string,
|
|
20
|
+
remoteData: any,
|
|
21
|
+
localData: any
|
|
22
|
+
): Promise<void> => {
|
|
23
|
+
if (!clientDb) return;
|
|
24
|
+
try {
|
|
25
|
+
if (isRemoteDataNewer(remoteData, localData)) {
|
|
26
|
+
await clientDb.put(dbKey, remoteData);
|
|
27
|
+
}
|
|
28
|
+
} catch (err) {
|
|
29
|
+
throw err;
|
|
30
|
+
}
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
const isRemoteDataNewer = (remoteData: any, localData: any): boolean => {
|
|
34
|
+
if (!remoteData || typeof remoteData !== "object") return false;
|
|
35
|
+
if (!localData || typeof localData !== "object") return true;
|
|
36
|
+
return shouldReplaceWithNextRecord(remoteData, localData);
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
const shouldSyncLocalToServer = (localData: any, remoteData: any): boolean => {
|
|
40
|
+
return !!localData && !remoteData;
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
const syncLocalDataToServer = async (
|
|
44
|
+
thunkApi: AppThunkApi,
|
|
45
|
+
dbKey: string,
|
|
46
|
+
localData: any
|
|
47
|
+
): Promise<void> => {
|
|
48
|
+
try {
|
|
49
|
+
const state = thunkApi.getState();
|
|
50
|
+
const { currentServer, syncServers } = getRuntimeServerContext(state);
|
|
51
|
+
scheduleExistingRecordReplication({
|
|
52
|
+
currentServer,
|
|
53
|
+
syncServers,
|
|
54
|
+
dbKey,
|
|
55
|
+
localData,
|
|
56
|
+
state,
|
|
57
|
+
});
|
|
58
|
+
} catch (err) {
|
|
59
|
+
// Error ignored
|
|
60
|
+
}
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
const saveRemoteDataToClientDb = async (
|
|
64
|
+
clientDb: any,
|
|
65
|
+
dbKey: string,
|
|
66
|
+
remoteData: any,
|
|
67
|
+
serverOrigin?: string | null
|
|
68
|
+
): Promise<void> => {
|
|
69
|
+
if (!clientDb) return;
|
|
70
|
+
try {
|
|
71
|
+
await clientDb.put(
|
|
72
|
+
dbKey,
|
|
73
|
+
serverOrigin
|
|
74
|
+
? {
|
|
75
|
+
...remoteData,
|
|
76
|
+
serverOrigin,
|
|
77
|
+
}
|
|
78
|
+
: remoteData
|
|
79
|
+
);
|
|
80
|
+
} catch (err) {
|
|
81
|
+
// Error ignored
|
|
82
|
+
}
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
const getValidRemoteData = (
|
|
86
|
+
dbKey: string,
|
|
87
|
+
settledResults: PromiseSettledResult<any>[],
|
|
88
|
+
localData?: any
|
|
89
|
+
): { data: any; index: number } | null => {
|
|
90
|
+
const validResults = settledResults
|
|
91
|
+
.map((result, index) => ({
|
|
92
|
+
data: result.status === "fulfilled" ? result.value : null,
|
|
93
|
+
index,
|
|
94
|
+
}))
|
|
95
|
+
.filter((item) => item.data !== null && typeof item.data === "object");
|
|
96
|
+
|
|
97
|
+
if (validResults.length === 0) return null;
|
|
98
|
+
|
|
99
|
+
const latest = validResults.reduce((latest, current) => {
|
|
100
|
+
return shouldReplaceWithNextRecord(current.data, latest.data) ? current : latest;
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
return {
|
|
104
|
+
...latest,
|
|
105
|
+
data: latest.data,
|
|
106
|
+
};
|
|
107
|
+
};
|
|
108
|
+
|
|
109
|
+
const processRemoteDataInBackground = async (
|
|
110
|
+
clientDb: any,
|
|
111
|
+
dbKey: string,
|
|
112
|
+
remotePromises: Promise<any>[],
|
|
113
|
+
remoteServers: string[],
|
|
114
|
+
localData: any,
|
|
115
|
+
thunkApi: AppThunkApi
|
|
116
|
+
): Promise<void> => {
|
|
117
|
+
if (!clientDb) return;
|
|
118
|
+
try {
|
|
119
|
+
const settledResults = await Promise.allSettled(remotePromises);
|
|
120
|
+
const remoteResult = getValidRemoteData(dbKey, settledResults, localData);
|
|
121
|
+
const validRemoteData = remoteResult ? remoteResult.data : null;
|
|
122
|
+
const serverOrigin =
|
|
123
|
+
remoteResult && remoteServers[remoteResult.index]
|
|
124
|
+
? remoteServers[remoteResult.index]
|
|
125
|
+
: undefined;
|
|
126
|
+
|
|
127
|
+
if (validRemoteData && localData) {
|
|
128
|
+
await updateClientDbIfNewer(
|
|
129
|
+
clientDb,
|
|
130
|
+
dbKey,
|
|
131
|
+
serverOrigin ? { ...validRemoteData, serverOrigin } : validRemoteData,
|
|
132
|
+
localData
|
|
133
|
+
);
|
|
134
|
+
}
|
|
135
|
+
if (shouldSyncLocalToServer(localData, validRemoteData)) {
|
|
136
|
+
await syncLocalDataToServer(thunkApi, dbKey, localData);
|
|
137
|
+
}
|
|
138
|
+
} catch (err) {
|
|
139
|
+
// Background sync errors ignored
|
|
140
|
+
}
|
|
141
|
+
};
|
|
142
|
+
|
|
143
|
+
// --- 主函数 ---
|
|
144
|
+
|
|
145
|
+
export const readAction = async (
|
|
146
|
+
payload: {
|
|
147
|
+
dbKey: string;
|
|
148
|
+
signal?: AbortSignal;
|
|
149
|
+
preferredServerOrigin?: string | null;
|
|
150
|
+
},
|
|
151
|
+
thunkApi: AppThunkApi
|
|
152
|
+
): Promise<any> => {
|
|
153
|
+
const dbKey = payload.dbKey;
|
|
154
|
+
const signal = payload.signal;
|
|
155
|
+
const preferredServerOrigin = payload.preferredServerOrigin;
|
|
156
|
+
|
|
157
|
+
if (!dbKey || typeof dbKey !== "string") {
|
|
158
|
+
throw new Error("readAction requires a non-empty dbKey.");
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
// 2. 尽早检查中止信号,快速退出
|
|
162
|
+
if (signal?.aborted) {
|
|
163
|
+
throw new DOMException("Aborted", "AbortError");
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
const { db: clientDb } = thunkApi.extra;
|
|
167
|
+
if (!clientDb) {
|
|
168
|
+
throw new Error("Client database is not available.");
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
const executeRead = async (): Promise<any> => {
|
|
172
|
+
const state = thunkApi.getState();
|
|
173
|
+
const { currentToken, remoteServers: allServers } =
|
|
174
|
+
getRuntimeServerContext(state, preferredServerOrigin);
|
|
175
|
+
const isLoggedIn = !!currentToken;
|
|
176
|
+
const now = Date.now();
|
|
177
|
+
const localData = await fetchFromClientDb(clientDb, dbKey);
|
|
178
|
+
readRequestManager.cleanupMisses(now);
|
|
179
|
+
readRequestManager.cleanupLocalHitRevalidations(now);
|
|
180
|
+
|
|
181
|
+
if (localData) {
|
|
182
|
+
readRequestManager.clearMiss(dbKey);
|
|
183
|
+
} else {
|
|
184
|
+
const retryInMs = readRequestManager.getRetryInMs(dbKey, now);
|
|
185
|
+
if (typeof retryInMs === "number" && retryInMs > 0) {
|
|
186
|
+
logger.debug(
|
|
187
|
+
{ dbKey, retryInMs },
|
|
188
|
+
"[readAction] Suppressing repeated miss read"
|
|
189
|
+
);
|
|
190
|
+
throw new Error(`Read temporarily suppressed for missing key "${dbKey}".`);
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
// 离线 / 无可用远程服务器:只看本地
|
|
195
|
+
if (allServers.length === 0) {
|
|
196
|
+
if (localData) {
|
|
197
|
+
return { ...localData, dbKey };
|
|
198
|
+
}
|
|
199
|
+
readRequestManager.markMiss(dbKey, now);
|
|
200
|
+
throw new Error(
|
|
201
|
+
`Failed to fetch data for key "${dbKey}" because network is offline and no local data is available.`
|
|
202
|
+
);
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
const preferredServer =
|
|
206
|
+
typeof preferredServerOrigin === "string" && preferredServerOrigin.trim().length > 0
|
|
207
|
+
? preferredServerOrigin.trim().replace(/\/+$/, "")
|
|
208
|
+
: null;
|
|
209
|
+
const remainingServers = preferredServer
|
|
210
|
+
? allServers.filter((server) => server.replace(/\/+$/, "") !== preferredServer)
|
|
211
|
+
: allServers;
|
|
212
|
+
|
|
213
|
+
if (localData) {
|
|
214
|
+
// Local-first: return durable local data immediately and only revalidate
|
|
215
|
+
// against remote servers in the background. This avoids turning a
|
|
216
|
+
// preferred-server timeout into a visible read failure for data we
|
|
217
|
+
// already have locally (for example createDialog -> initDialog).
|
|
218
|
+
if (!signal?.aborted) {
|
|
219
|
+
const retryInMs = readRequestManager.getLocalHitRevalidateInMs(
|
|
220
|
+
dbKey,
|
|
221
|
+
now
|
|
222
|
+
);
|
|
223
|
+
if (retryInMs === null) {
|
|
224
|
+
readRequestManager.markLocalHitRevalidated(dbKey, now);
|
|
225
|
+
const revalidationServers = preferredServer
|
|
226
|
+
? [preferredServer, ...remainingServers]
|
|
227
|
+
: remainingServers;
|
|
228
|
+
const remotePromises = revalidationServers.map((server) =>
|
|
229
|
+
fetchFromServer(server, dbKey, isLoggedIn ? currentToken : undefined, signal)
|
|
230
|
+
);
|
|
231
|
+
void (async () => {
|
|
232
|
+
await processRemoteDataInBackground(
|
|
233
|
+
clientDb,
|
|
234
|
+
dbKey,
|
|
235
|
+
remotePromises,
|
|
236
|
+
revalidationServers,
|
|
237
|
+
localData,
|
|
238
|
+
thunkApi
|
|
239
|
+
);
|
|
240
|
+
})();
|
|
241
|
+
} else {
|
|
242
|
+
logger.debug(
|
|
243
|
+
{ dbKey, retryInMs },
|
|
244
|
+
"[readAction] Skipping frequent local-hit revalidation"
|
|
245
|
+
);
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
return { ...localData, dbKey };
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
if (preferredServer) {
|
|
252
|
+
try {
|
|
253
|
+
const preferredRemoteData = await fetchFromServer(
|
|
254
|
+
preferredServer,
|
|
255
|
+
dbKey,
|
|
256
|
+
isLoggedIn ? currentToken : undefined,
|
|
257
|
+
signal
|
|
258
|
+
);
|
|
259
|
+
|
|
260
|
+
if (preferredRemoteData) {
|
|
261
|
+
await saveRemoteDataToClientDb(
|
|
262
|
+
clientDb,
|
|
263
|
+
dbKey,
|
|
264
|
+
preferredRemoteData,
|
|
265
|
+
preferredServer
|
|
266
|
+
);
|
|
267
|
+
readRequestManager.clearMiss(dbKey);
|
|
268
|
+
return { ...preferredRemoteData, dbKey, serverOrigin: preferredServer };
|
|
269
|
+
}
|
|
270
|
+
} catch (error) {
|
|
271
|
+
if (signal?.aborted || (error as { name?: string } | null)?.name === "AbortError") {
|
|
272
|
+
throw error;
|
|
273
|
+
}
|
|
274
|
+
if (isReadTimeoutError(error)) {
|
|
275
|
+
logger.warn(
|
|
276
|
+
{ dbKey, preferredServer, error: String((error as Error).message) },
|
|
277
|
+
"[readAction] Preferred server timed out; falling back to remaining servers"
|
|
278
|
+
);
|
|
279
|
+
} else {
|
|
280
|
+
logger.warn(
|
|
281
|
+
{ dbKey, preferredServer, error: String(error) },
|
|
282
|
+
"[readAction] Preferred server read failed; falling back to remaining servers"
|
|
283
|
+
);
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
// 3. 将 signal 传递给所有网络请求
|
|
289
|
+
const remotePromises = remainingServers.map((server) =>
|
|
290
|
+
fetchFromServer(server, dbKey, isLoggedIn ? currentToken : undefined, signal)
|
|
291
|
+
);
|
|
292
|
+
|
|
293
|
+
// 如果本地没有数据,则等待网络请求结果
|
|
294
|
+
const settledResults = await Promise.allSettled(remotePromises);
|
|
295
|
+
|
|
296
|
+
// 4. 在处理网络结果前,再次检查中止信号
|
|
297
|
+
if (signal?.aborted) {
|
|
298
|
+
throw new DOMException("Aborted", "AbortError");
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
const remoteResult = getValidRemoteData(dbKey, settledResults, localData);
|
|
302
|
+
if (remoteResult) {
|
|
303
|
+
const { data: validRemoteData } = remoteResult;
|
|
304
|
+
const serverOrigin = remainingServers[remoteResult.index];
|
|
305
|
+
if (!signal?.aborted) {
|
|
306
|
+
await saveRemoteDataToClientDb(
|
|
307
|
+
clientDb,
|
|
308
|
+
dbKey,
|
|
309
|
+
validRemoteData,
|
|
310
|
+
serverOrigin
|
|
311
|
+
);
|
|
312
|
+
}
|
|
313
|
+
readRequestManager.clearMiss(dbKey);
|
|
314
|
+
return serverOrigin
|
|
315
|
+
? { ...validRemoteData, dbKey, serverOrigin }
|
|
316
|
+
: { ...validRemoteData, dbKey };
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
readRequestManager.markMiss(dbKey, Date.now());
|
|
320
|
+
throw new Error(`Failed to fetch data for key "${dbKey}" from all sources.`);
|
|
321
|
+
};
|
|
322
|
+
|
|
323
|
+
const canDedup = !signal;
|
|
324
|
+
if (canDedup) {
|
|
325
|
+
const existing = readRequestManager.getInFlight(dbKey);
|
|
326
|
+
if (existing) return existing;
|
|
327
|
+
|
|
328
|
+
let inFlightPromise: Promise<any>;
|
|
329
|
+
inFlightPromise = executeRead().finally(() => {
|
|
330
|
+
readRequestManager.clearInFlight(dbKey, inFlightPromise);
|
|
331
|
+
});
|
|
332
|
+
readRequestManager.setInFlight(dbKey, inFlightPromise);
|
|
333
|
+
return inFlightPromise;
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
return executeRead();
|
|
337
|
+
};
|
|
@@ -0,0 +1,224 @@
|
|
|
1
|
+
// 文件路径: database/actions/readAndWait.ts
|
|
2
|
+
|
|
3
|
+
import type { AppThunkApi } from "app/store";
|
|
4
|
+
import {
|
|
5
|
+
fetchFromClientDb,
|
|
6
|
+
fetchFromServer,
|
|
7
|
+
} from "./common";
|
|
8
|
+
import { readRequestManager } from "./readRequestManager";
|
|
9
|
+
import { scheduleExistingRecordReplication } from "./replication";
|
|
10
|
+
import { getRuntimeServerContext } from "database/runtimeServerContext";
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* 比较远程数据和本地数据的时间戳,判断远程数据是否更新。
|
|
14
|
+
* @param remoteData - 从服务器获取的数据。
|
|
15
|
+
* @param localData - 从本地数据库获取的数据。
|
|
16
|
+
* @returns 如果远程数据更新,则返回 true。
|
|
17
|
+
*/
|
|
18
|
+
const isRemoteDataNewer = (remoteData: any, localData: any): boolean => {
|
|
19
|
+
const toComparableTimestamp = (data: any): number => {
|
|
20
|
+
if (!data || typeof data !== "object") return 0;
|
|
21
|
+
|
|
22
|
+
const updatedAtMs = new Date(data.updatedAt).getTime();
|
|
23
|
+
if (Number.isFinite(updatedAtMs) && updatedAtMs > 0) return updatedAtMs;
|
|
24
|
+
|
|
25
|
+
const createdAtMs = new Date(data.createdAt).getTime();
|
|
26
|
+
if (Number.isFinite(createdAtMs) && createdAtMs > 0) return createdAtMs;
|
|
27
|
+
|
|
28
|
+
const metaCreatedAt = Number(data?.meta?.createdAt);
|
|
29
|
+
if (Number.isFinite(metaCreatedAt) && metaCreatedAt > 0) return metaCreatedAt;
|
|
30
|
+
|
|
31
|
+
return 0;
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
const remoteTs = toComparableTimestamp(remoteData);
|
|
35
|
+
const localTs = toComparableTimestamp(localData);
|
|
36
|
+
if (remoteTs <= 0) return false;
|
|
37
|
+
if (localTs <= 0) return true;
|
|
38
|
+
return remoteTs > localTs;
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* 触发一个“即发即忘”的异步任务,将本地数据上传(写入)到服务器。
|
|
43
|
+
* 通常在发现本地存在数据而所有远程服务器上都不存在该数据时调用。
|
|
44
|
+
*/
|
|
45
|
+
const syncLocalDataToServer = async (
|
|
46
|
+
thunkApi: AppThunkApi,
|
|
47
|
+
dbKey: string,
|
|
48
|
+
localData: any
|
|
49
|
+
): Promise<void> => {
|
|
50
|
+
try {
|
|
51
|
+
const state = thunkApi.getState();
|
|
52
|
+
const { currentServer, syncServers } = getRuntimeServerContext(state);
|
|
53
|
+
scheduleExistingRecordReplication({
|
|
54
|
+
currentServer,
|
|
55
|
+
syncServers,
|
|
56
|
+
dbKey,
|
|
57
|
+
localData,
|
|
58
|
+
state,
|
|
59
|
+
});
|
|
60
|
+
} catch {
|
|
61
|
+
// 后台同步失败不阻塞主流程,静默处理即可
|
|
62
|
+
}
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* 从所有远程服务器的请求结果中,筛选出有效的、时间戳最新的那一份数据。
|
|
67
|
+
*/
|
|
68
|
+
const getValidRemoteData = (
|
|
69
|
+
dbKey: string,
|
|
70
|
+
settledResults: PromiseSettledResult<any>[],
|
|
71
|
+
localData?: any
|
|
72
|
+
): { data: any; index: number } | null => {
|
|
73
|
+
const toComparableTimestamp = (data: any): number => {
|
|
74
|
+
if (!data || typeof data !== "object") return 0;
|
|
75
|
+
|
|
76
|
+
const updatedAtMs = new Date(data.updatedAt).getTime();
|
|
77
|
+
if (Number.isFinite(updatedAtMs) && updatedAtMs > 0) return updatedAtMs;
|
|
78
|
+
|
|
79
|
+
const createdAtMs = new Date(data.createdAt).getTime();
|
|
80
|
+
if (Number.isFinite(createdAtMs) && createdAtMs > 0) return createdAtMs;
|
|
81
|
+
|
|
82
|
+
const metaCreatedAt = Number(data?.meta?.createdAt);
|
|
83
|
+
if (Number.isFinite(metaCreatedAt) && metaCreatedAt > 0) return metaCreatedAt;
|
|
84
|
+
|
|
85
|
+
return 0;
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
const validResults = settledResults
|
|
89
|
+
.map((result, index) => ({
|
|
90
|
+
data: result.status === "fulfilled" ? result.value : null,
|
|
91
|
+
index,
|
|
92
|
+
}))
|
|
93
|
+
.filter((item) => item.data !== null && typeof item.data === "object");
|
|
94
|
+
|
|
95
|
+
if (validResults.length === 0) return null;
|
|
96
|
+
|
|
97
|
+
// 从所有有效的远程数据中,选出时间戳最新的那一个。
|
|
98
|
+
const latest = validResults.reduce((latest, current) => {
|
|
99
|
+
const latestTimestamp = toComparableTimestamp(latest.data);
|
|
100
|
+
const currentTimestamp = toComparableTimestamp(current.data);
|
|
101
|
+
return currentTimestamp > latestTimestamp ? current : latest;
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
return {
|
|
105
|
+
...latest,
|
|
106
|
+
data: latest.data,
|
|
107
|
+
};
|
|
108
|
+
};
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* 核心处理函数:协调本地和远程数据,决定最终返回哪个版本的数据,并处理同步逻辑。
|
|
112
|
+
*/
|
|
113
|
+
const processRemoteData = async (
|
|
114
|
+
db: any,
|
|
115
|
+
dbKey: string,
|
|
116
|
+
remotePromises: Promise<any>[],
|
|
117
|
+
localData: any,
|
|
118
|
+
thunkApi: AppThunkApi
|
|
119
|
+
): Promise<any> => {
|
|
120
|
+
try {
|
|
121
|
+
// 并行执行所有远程请求,并等待它们全部完成(无论成功或失败)。
|
|
122
|
+
const settledResults = await Promise.allSettled(remotePromises);
|
|
123
|
+
// 从所有结果中,找出最“权威”的远程版本(时间戳最新)。
|
|
124
|
+
const remoteResult = getValidRemoteData(dbKey, settledResults, localData);
|
|
125
|
+
const validRemoteData = remoteResult ? remoteResult.data : null;
|
|
126
|
+
|
|
127
|
+
// --- 数据决策核心逻辑 ---
|
|
128
|
+
|
|
129
|
+
// 场景 1: 至少一个远程服务器返回了有效数据。
|
|
130
|
+
if (validRemoteData) {
|
|
131
|
+
// 如果本地没有数据,或者远程更新更新,则用远程覆盖本地缓存。
|
|
132
|
+
if (!localData || isRemoteDataNewer(validRemoteData, localData)) {
|
|
133
|
+
await db.put(dbKey, validRemoteData);
|
|
134
|
+
}
|
|
135
|
+
// 最终决策:返回权威的远程数据。
|
|
136
|
+
return validRemoteData;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
// 场景 2: 所有远程服务器都没有返回有效数据,但我们本地数据库中存在数据。
|
|
140
|
+
if (localData) {
|
|
141
|
+
// 仅当“确实有远程目标”时,才尝试上传本地数据到服务器。
|
|
142
|
+
if (remotePromises.length > 0) {
|
|
143
|
+
void syncLocalDataToServer(thunkApi, dbKey, localData);
|
|
144
|
+
}
|
|
145
|
+
// 最终决策:返回本地数据。
|
|
146
|
+
return localData;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
// 场景 3: 远程和本地都找不到任何数据。
|
|
150
|
+
throw new Error("Failed to fetch data from all sources");
|
|
151
|
+
} catch (err) {
|
|
152
|
+
// 如果在上述过程中发生错误,且有本地数据,则优先返回本地数据,避免崩溃。
|
|
153
|
+
if (localData) {
|
|
154
|
+
return localData;
|
|
155
|
+
}
|
|
156
|
+
throw err;
|
|
157
|
+
}
|
|
158
|
+
};
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* 主函数:读取数据,并等待远程和本地操作都完成后才返回最合适的数据。
|
|
162
|
+
* - 本地优先:有本地数据时,远程只用于更新缓存或回填云端。
|
|
163
|
+
* - 远程优先:若拿到了有效远程数据,以其为准。
|
|
164
|
+
*/
|
|
165
|
+
export const readAndWaitAction = async (
|
|
166
|
+
dbKey: string,
|
|
167
|
+
thunkApi: AppThunkApi
|
|
168
|
+
): Promise<any> => {
|
|
169
|
+
const { db } = thunkApi.extra;
|
|
170
|
+
|
|
171
|
+
if (!db) {
|
|
172
|
+
throw new Error(
|
|
173
|
+
"Database instance is not available in thunk extra argument."
|
|
174
|
+
);
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
const state = thunkApi.getState();
|
|
178
|
+
const { currentToken, remoteServers: allServers } =
|
|
179
|
+
getRuntimeServerContext(state);
|
|
180
|
+
const isLoggedIn = !!currentToken;
|
|
181
|
+
|
|
182
|
+
const executeReadAndWait = async (): Promise<any> => {
|
|
183
|
+
// 1. 准备所有需要访问的远程服务器(带去重 + 离线检测)
|
|
184
|
+
// 2. 首先,尝试从本地数据库获取数据(可能为 null)
|
|
185
|
+
const localData = await fetchFromClientDb(db, dbKey);
|
|
186
|
+
|
|
187
|
+
// 如果离线或没有任何可用远程服务器:
|
|
188
|
+
if (allServers.length === 0) {
|
|
189
|
+
if (localData) {
|
|
190
|
+
return { ...localData, dbKey };
|
|
191
|
+
}
|
|
192
|
+
throw new Error(
|
|
193
|
+
`Failed to fetch data for key "${dbKey}" because network is offline and no local data is available.`
|
|
194
|
+
);
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
// 3. 创建所有到远程服务器的并行请求
|
|
198
|
+
const remotePromises = allServers.map((server) =>
|
|
199
|
+
fetchFromServer(server, dbKey, isLoggedIn ? currentToken : undefined)
|
|
200
|
+
);
|
|
201
|
+
|
|
202
|
+
// 4. 将所有信息交给核心处理函数去做最终决策
|
|
203
|
+
const chosenData = await processRemoteData(
|
|
204
|
+
db,
|
|
205
|
+
dbKey,
|
|
206
|
+
remotePromises,
|
|
207
|
+
localData,
|
|
208
|
+
thunkApi
|
|
209
|
+
);
|
|
210
|
+
|
|
211
|
+
// ⭐ 统一:不论返回的是本地还是远程,最终都附加 dbKey 字段
|
|
212
|
+
return { ...chosenData, dbKey };
|
|
213
|
+
};
|
|
214
|
+
|
|
215
|
+
const existing = readRequestManager.getInFlight(dbKey);
|
|
216
|
+
if (existing) return existing;
|
|
217
|
+
|
|
218
|
+
let inFlightPromise: Promise<any>;
|
|
219
|
+
inFlightPromise = executeReadAndWait().finally(() => {
|
|
220
|
+
readRequestManager.clearInFlight(dbKey, inFlightPromise);
|
|
221
|
+
});
|
|
222
|
+
readRequestManager.setInFlight(dbKey, inFlightPromise);
|
|
223
|
+
return inFlightPromise;
|
|
224
|
+
};
|