nolo-cli 0.8.0-alpha.6 → 0.8.0-alpha.7

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.
Files changed (174) hide show
  1. package/_executeModel-XIGO7HW5.js +198 -0
  2. package/action-PAXRPJJF.js +27 -0
  3. package/addContentAction-CZVWF3QZ.js +25 -0
  4. package/addDialogAgentAction-CCIDTPHC.js +67 -0
  5. package/agentDeleteCommand-47B24JYG.js +323 -0
  6. package/agentEmailCommands-D4FC6OZJ.js +621 -0
  7. package/agentListCommands-UFET7CC4.js +219 -0
  8. package/agentMachineCommands-LH7QDQPP.js +421 -0
  9. package/agentPullCommand-O2MTUAFR.js +122 -0
  10. package/agentRecordCommands-CWS2B5IA.js +89 -0
  11. package/agentRunCommand-3FRPISLG.js +102 -0
  12. package/agentRunControl-K4VKNEHF.js +57 -0
  13. package/agentRuntimeLocal-JNQQC2WG.js +99 -0
  14. package/agentSlice-XDFSELFJ.js +41 -0
  15. package/authCommand-WXMD3IPR.js +574 -0
  16. package/authCommands-X74AFDTC.js +26 -0
  17. package/chunk-2EISRLRC.js +238 -0
  18. package/chunk-2FTTDCD4.js +675 -0
  19. package/chunk-2JHWPW5B.js +2127 -0
  20. package/chunk-2LMT6K6N.js +171 -0
  21. package/chunk-2MALU52L.js +51 -0
  22. package/chunk-364VYAX2.js +19 -0
  23. package/chunk-3ASDTTEI.js +1774 -0
  24. package/chunk-3EUW3WEO.js +8 -0
  25. package/chunk-3LY4F7SV.js +9293 -0
  26. package/chunk-4CQLVGRV.js +50 -0
  27. package/chunk-4IJYA27C.js +342 -0
  28. package/chunk-4SN6J65J.js +38 -0
  29. package/chunk-4VNS5WPM.js +42 -0
  30. package/chunk-543C7RDK.js +562 -0
  31. package/chunk-572EKYLF.js +792 -0
  32. package/chunk-5MIDPSSK.js +206 -0
  33. package/chunk-5MMAK75Q.js +15 -0
  34. package/chunk-5XH3NZ4T.js +3631 -0
  35. package/chunk-63W5N6P3.js +150 -0
  36. package/chunk-6HYVJV7B.js +102 -0
  37. package/chunk-6PWWWMI7.js +628 -0
  38. package/chunk-6WD3OOJS.js +20 -0
  39. package/chunk-A3X4Y7MO.js +4294 -0
  40. package/chunk-AR7ZSGDI.js +929 -0
  41. package/chunk-ATNTHIZI.js +13 -0
  42. package/chunk-AUGHVUFE.js +69 -0
  43. package/chunk-AVMKZHBC.js +85 -0
  44. package/chunk-AZPCASRE.js +36 -0
  45. package/chunk-B76D3JJK.js +1447 -0
  46. package/chunk-BR2WP3DF.js +548 -0
  47. package/chunk-BSTNFPHD.js +231 -0
  48. package/chunk-CF2WZF4N.js +134 -0
  49. package/chunk-CF7PE763.js +10 -0
  50. package/chunk-CKOJQDKR.js +198 -0
  51. package/chunk-CM54RSSA.js +206 -0
  52. package/chunk-CWMSB67P.js +34 -0
  53. package/chunk-DA2OEKS6.js +27 -0
  54. package/chunk-DFFIZ25G.js +30 -0
  55. package/chunk-DWLEOOGR.js +8 -0
  56. package/chunk-DZ3XJVET.js +231 -0
  57. package/chunk-E5HMHIKL.js +222 -0
  58. package/chunk-EHLHSX7R.js +1433 -0
  59. package/chunk-ELRRLDWI.js +478 -0
  60. package/chunk-EYEEHCU4.js +3066 -0
  61. package/chunk-G3SUCT4W.js +8 -0
  62. package/chunk-G5H7RNFL.js +807 -0
  63. package/chunk-GAKEBQD6.js +30 -0
  64. package/chunk-GBRG75BQ.js +86 -0
  65. package/chunk-GEKCOPTN.js +190 -0
  66. package/chunk-H2RMRZJE.js +113 -0
  67. package/chunk-H33743XH.js +25 -0
  68. package/chunk-HEIIIZBX.js +104 -0
  69. package/chunk-HNMXYYJR.js +12 -0
  70. package/chunk-IAXBCTTE.js +175 -0
  71. package/chunk-IDZTNRP2.js +85 -0
  72. package/chunk-IECI4YHR.js +421 -0
  73. package/chunk-ISNCLGUZ.js +146 -0
  74. package/chunk-IYQV26RA.js +138 -0
  75. package/chunk-JBTJAPUF.js +35 -0
  76. package/chunk-JZPBVBIY.js +94 -0
  77. package/chunk-KK6SWS5B.js +1078 -0
  78. package/chunk-KQHGZWSV.js +63 -0
  79. package/chunk-KTI2WDDG.js +24674 -0
  80. package/chunk-KUG52IAP.js +8 -0
  81. package/chunk-KX6YBIHG.js +8 -0
  82. package/chunk-LCBAMY2M.js +60 -0
  83. package/chunk-LM25IZBJ.js +902 -0
  84. package/chunk-LM6IYQ3F.js +16 -0
  85. package/chunk-M76ECQRD.js +13658 -0
  86. package/chunk-MRC4Q34M.js +188 -0
  87. package/chunk-NE3E3XJ2.js +936 -0
  88. package/chunk-NXZGF7F7.js +2878 -0
  89. package/chunk-OF5H3QWS.js +300 -0
  90. package/chunk-OLZBQWZU.js +6 -0
  91. package/chunk-OVGXXUVI.js +91 -0
  92. package/chunk-OWOHNGCN.js +50 -0
  93. package/chunk-PN7AFLOK.js +3401 -0
  94. package/chunk-QSJ3HFEF.js +8 -0
  95. package/chunk-QYXLLLGL.js +4519 -0
  96. package/chunk-ROWQEJRP.js +139 -0
  97. package/chunk-S7Z2HMCN.js +25 -0
  98. package/chunk-SL3EPWB4.js +1949 -0
  99. package/chunk-TDIQFT2F.js +301 -0
  100. package/chunk-TILFE6LV.js +381 -0
  101. package/chunk-U4TRT5V4.js +246 -0
  102. package/chunk-U6VC4MU4.js +8 -0
  103. package/chunk-UAD2WT54.js +64 -0
  104. package/chunk-USZDUWOV.js +191 -0
  105. package/chunk-USZMK5UT.js +105 -0
  106. package/chunk-UVPF33PW.js +46995 -0
  107. package/chunk-V2F4OGYM.js +9013 -0
  108. package/chunk-VEJLAVTC.js +2052 -0
  109. package/chunk-VF4K2QE6.js +21 -0
  110. package/chunk-VHBWHNVT.js +59 -0
  111. package/chunk-VIDPVFP3.js +20 -0
  112. package/chunk-VP4WJWKW.js +8882 -0
  113. package/chunk-VQ2AJRGD.js +148 -0
  114. package/chunk-W4NVOSFF.js +37 -0
  115. package/chunk-WWC7I6TA.js +8 -0
  116. package/chunk-X7W4TNB4.js +1550 -0
  117. package/chunk-XDVAYP26.js +537 -0
  118. package/chunk-XKIMRZO2.js +221 -0
  119. package/chunk-Y5GCAMGP.js +9 -0
  120. package/chunk-YD43KPCK.js +360 -0
  121. package/chunk-YIJFUVKA.js +34 -0
  122. package/chunk-Z4NR32XR.js +50 -0
  123. package/chunk-ZNFJWEN4.js +56 -0
  124. package/cliExecutor-DNE7TCC4.js +36 -0
  125. package/clipboardy-BVE3LVEH.js +12001 -0
  126. package/createAgentAutomationAction-3QTGNYJC.js +864 -0
  127. package/createDialogAction-5YX3ARBK.js +52 -0
  128. package/createPageAction-EQNQQRCW.js +169 -0
  129. package/db-HCJ5PRB5.js +271 -0
  130. package/dbSlice-C4UJLRZP.js +59 -0
  131. package/deleteFile-4SKGPHOZ.js +41 -0
  132. package/dialogCommands-AOQS23FV.js +59 -0
  133. package/dialogSlice-GZE3NCMC.js +146 -0
  134. package/docCreateCommands-GVOQVJON.js +215 -0
  135. package/docDeleteCommands-GEB7YAUZ.js +176 -0
  136. package/docReadCommands-XYKI2DF2.js +129 -0
  137. package/docStore-MLM5AME5.js +96 -0
  138. package/docUpdateCommands-4L3RAMHZ.js +313 -0
  139. package/fetchMessages-JOCYIFKL.js +16 -0
  140. package/getPricing-PWPJ6MNV.js +30 -0
  141. package/handleSendMessageAction-WG2GKTGZ.js +183 -0
  142. package/index.js +188 -113035
  143. package/localDialogRead-D3BELSTF.js +26 -0
  144. package/localFirstLog-DHNOY2NA.js +19 -0
  145. package/localRuntimeAuthority-TEGCOWX4.js +471 -0
  146. package/localRuntimeDb-LGDF6FQP.js +12 -0
  147. package/machineCommands-ZNXKLH6Q.js +774 -0
  148. package/machineInfo-M6KB6HZN.js +10 -0
  149. package/machineWsRunDispatch-7WRG6HNV.js +105 -0
  150. package/memoryCommands-O4MIPIWQ.js +476 -0
  151. package/models-XLEOET3E.js +9 -0
  152. package/offlineMarxistsAgentCommand-BCPJITJL.js +242 -0
  153. package/package.json +3 -25
  154. package/read-RIJCIVKX.js +17 -0
  155. package/readDocTool-GZD2N3LO.js +16 -0
  156. package/readlineWorkspace-AQURUDN4.js +5357 -0
  157. package/removeDialogAgentAction-CXLST3N6.js +72 -0
  158. package/runAgentClientLoop-VQUEKGA5.js +288 -0
  159. package/runtimeDoctorCommands-LRHLPDRC.js +289 -0
  160. package/setDialogExtraReferencesAction-LP6VYGU7.js +59 -0
  161. package/setPrimaryDialogAgentAction-D3GGH23T.js +79 -0
  162. package/settingSlice-J4I3Z5H7.js +198 -0
  163. package/skillSummaryMarker-42HKSXYY.js +9 -0
  164. package/slateToRenderMarkdown-Y5IYN73U.js +7 -0
  165. package/slateUtils-2M7MUTWX.js +19 -0
  166. package/spaceCommands-R2RXD5UY.js +450 -0
  167. package/spaceSlice-5UT4JNIN.js +145 -0
  168. package/streamAgentChatTurn-NRWSQKFN.js +4503 -0
  169. package/tableCommands-66XEWHAK.js +1286 -0
  170. package/toolApiClient-QI3YBNWB.js +26 -0
  171. package/tools-TNMUDDRK.js +83 -0
  172. package/understanding-HCSFPE7R.js +318 -0
  173. package/updateCommands-VOSPKXFF.js +31 -0
  174. package/updateDialogTitleAction-IXOKAVX3.js +185 -0
@@ -0,0 +1,478 @@
1
+ import {
2
+ getRuntimeServerContext,
3
+ partitionReadServers,
4
+ pickBestSettledRemoteRecord,
5
+ planAuthorityReadServers,
6
+ resolveRecordAuthority,
7
+ scheduleExistingRecordReplication,
8
+ shouldReplaceLocalWithRemoteRecord,
9
+ shouldReplicateLocalRecord
10
+ } from "./chunk-EYEEHCU4.js";
11
+ import {
12
+ fetchFromClientDb,
13
+ fetchFromServer,
14
+ isAbortError,
15
+ isReadTimeoutError,
16
+ logger
17
+ } from "./chunk-QYXLLLGL.js";
18
+ import {
19
+ shouldReplaceWithNextRecord
20
+ } from "./chunk-GBRG75BQ.js";
21
+ import {
22
+ NOLO_CLUSTER_SERVERS,
23
+ normalizeKnownServerOrigin
24
+ } from "./chunk-LCBAMY2M.js";
25
+ import {
26
+ normalizeServerOrigin
27
+ } from "./chunk-CF7PE763.js";
28
+
29
+ // packages/core/builtinAgents.ts
30
+ var BUILTIN_NOLO_AGENT_KEY = "agent-pub-01NOLOAPPBLD000000019KCKT0";
31
+ var BUILTIN_APP_BUILDER_AGENT_KEY = "agent-pub-01APPBUILDER00000001YAII3I";
32
+ var BUILTIN_ECOMMERCE_AGENT_KEY = "agent-pub-01ECOMMERCEAG00000001PYQ2J";
33
+ var BUILTIN_AGENT_CREATOR_AGENT_KEY = "agent-pub-01NOLOAGENTCRT000000000001";
34
+ var BUILTIN_FEEDBACK_AGENT_KEY = "agent-pub-01NOLOFEEDBACKA000000000R2";
35
+ var BUILTIN_CHROME_OPERATOR_AGENT_KEY = "agent-pub-01CHROMEOPR000000000001";
36
+ var PUBLIC_DEEPSEEK_V4_FLASH_AGENT_ID = "01DSV4FLASHPB00000000JFPFD";
37
+ var PUBLIC_DEEPSEEK_V4_FLASH_AGENT_KEY = `agent-pub-${PUBLIC_DEEPSEEK_V4_FLASH_AGENT_ID}`;
38
+ var PUBLIC_DEEPSEEK_V4_PRO_AGENT_ID = "01DSV4PROPUB00000001A9OLZN";
39
+ var PUBLIC_DEEPSEEK_V4_PRO_AGENT_KEY = `agent-pub-${PUBLIC_DEEPSEEK_V4_PRO_AGENT_ID}`;
40
+ var PUBLIC_KIMI_K27_CODING_AGENT_ID = "01KIMIK27CODEOL000000001";
41
+ var PUBLIC_KIMI_K27_CODING_AGENT_KEY = `agent-pub-${PUBLIC_KIMI_K27_CODING_AGENT_ID}`;
42
+ var PUBLIC_GLM_52_AGENT_ID = "01GLM52CHAT00000000001U721";
43
+ var PUBLIC_GLM_52_AGENT_KEY = `agent-pub-${PUBLIC_GLM_52_AGENT_ID}`;
44
+ var PUBLIC_KIMI_K26_IMAGE_AGENT_ID = "01KIMIK26OLLAMA0000000001";
45
+ var PUBLIC_KIMI_K26_IMAGE_AGENT_KEY = `agent-pub-${PUBLIC_KIMI_K26_IMAGE_AGENT_ID}`;
46
+ var BUILTIN_PLATFORM_AGENT_KEYS = [
47
+ BUILTIN_NOLO_AGENT_KEY,
48
+ BUILTIN_APP_BUILDER_AGENT_KEY,
49
+ BUILTIN_ECOMMERCE_AGENT_KEY,
50
+ BUILTIN_AGENT_CREATOR_AGENT_KEY,
51
+ BUILTIN_FEEDBACK_AGENT_KEY,
52
+ BUILTIN_CHROME_OPERATOR_AGENT_KEY
53
+ ];
54
+ var BUILTIN_PLATFORM_AGENT_KEY_SET = new Set(BUILTIN_PLATFORM_AGENT_KEYS);
55
+
56
+ // packages/database/actions/agentReadResolution.ts
57
+ var BUILTIN_PLATFORM_AGENT_KEYS2 = [
58
+ ...BUILTIN_PLATFORM_AGENT_KEYS
59
+ ];
60
+ var BUILTIN_PLATFORM_AGENT_KEY_SET2 = new Set(
61
+ BUILTIN_PLATFORM_AGENT_KEYS2
62
+ );
63
+ var normalizeServer = (server) => normalizeKnownServerOrigin(server) ?? normalizeServerOrigin(server);
64
+ var isBuiltinPlatformAgentKey = (dbKey) => typeof dbKey === "string" && BUILTIN_PLATFORM_AGENT_KEY_SET2.has(dbKey);
65
+ var resolveAgentReadServers = ({
66
+ dbKey,
67
+ configuredServers
68
+ }) => {
69
+ const normalized = configuredServers.map(normalizeServer);
70
+ if (!isBuiltinPlatformAgentKey(dbKey)) {
71
+ return Array.from(new Set(normalized));
72
+ }
73
+ return Array.from(/* @__PURE__ */ new Set([...normalized, ...NOLO_CLUSTER_SERVERS]));
74
+ };
75
+
76
+ // packages/database/actions/readRequestManager.ts
77
+ var DEFAULT_MISS_COOLDOWN_MS = 2e3;
78
+ var DEFAULT_LOCAL_HIT_REVALIDATE_COOLDOWN_MS = 1500;
79
+ var DEFAULT_MISS_CACHE_MAX_SIZE = 1e3;
80
+ var ReadRequestManager = class {
81
+ constructor(options = {}) {
82
+ this.options = options;
83
+ }
84
+ inFlightReads = /* @__PURE__ */ new Map();
85
+ recentMisses = /* @__PURE__ */ new Map();
86
+ recentLocalHitRevalidations = /* @__PURE__ */ new Map();
87
+ get missCooldownMs() {
88
+ return this.options.missCooldownMs ?? DEFAULT_MISS_COOLDOWN_MS;
89
+ }
90
+ get missCacheMaxSize() {
91
+ return this.options.missCacheMaxSize ?? DEFAULT_MISS_CACHE_MAX_SIZE;
92
+ }
93
+ get localHitRevalidateCooldownMs() {
94
+ return this.options.localHitRevalidateCooldownMs ?? DEFAULT_LOCAL_HIT_REVALIDATE_COOLDOWN_MS;
95
+ }
96
+ getInFlight(dbKey) {
97
+ return this.inFlightReads.get(dbKey);
98
+ }
99
+ setInFlight(dbKey, promise) {
100
+ this.inFlightReads.set(dbKey, promise);
101
+ }
102
+ clearInFlight(dbKey, promise) {
103
+ if (this.inFlightReads.get(dbKey) === promise) {
104
+ this.inFlightReads.delete(dbKey);
105
+ }
106
+ }
107
+ clearMiss(dbKey) {
108
+ this.recentMisses.delete(dbKey);
109
+ }
110
+ getRetryInMs(dbKey, now) {
111
+ const missUntil = this.recentMisses.get(dbKey);
112
+ if (typeof missUntil !== "number") return null;
113
+ if (missUntil <= now) {
114
+ this.recentMisses.delete(dbKey);
115
+ return null;
116
+ }
117
+ return missUntil - now;
118
+ }
119
+ markMiss(dbKey, now, cooldownMs = this.missCooldownMs) {
120
+ this.recentMisses.set(dbKey, now + cooldownMs);
121
+ this.cleanupMisses(now);
122
+ }
123
+ getLocalHitRevalidateInMs(dbKey, now) {
124
+ const nextAllowedAt = this.recentLocalHitRevalidations.get(dbKey);
125
+ if (typeof nextAllowedAt !== "number") return null;
126
+ if (nextAllowedAt <= now) {
127
+ this.recentLocalHitRevalidations.delete(dbKey);
128
+ return null;
129
+ }
130
+ return nextAllowedAt - now;
131
+ }
132
+ markLocalHitRevalidated(dbKey, now, cooldownMs = this.localHitRevalidateCooldownMs) {
133
+ this.recentLocalHitRevalidations.set(dbKey, now + cooldownMs);
134
+ this.cleanupLocalHitRevalidations(now);
135
+ }
136
+ cleanupExpiringMap(map, now) {
137
+ for (const [key, expiresAt] of Array.from(map.entries())) {
138
+ if (expiresAt <= now) {
139
+ map.delete(key);
140
+ }
141
+ }
142
+ if (map.size <= this.missCacheMaxSize) return;
143
+ const overflow = map.size - this.missCacheMaxSize;
144
+ const keys = Array.from(map.keys());
145
+ for (let i = 0; i < overflow; i += 1) {
146
+ const key = keys[i];
147
+ if (key) map.delete(key);
148
+ }
149
+ }
150
+ cleanupMisses(now) {
151
+ this.cleanupExpiringMap(this.recentMisses, now);
152
+ }
153
+ cleanupLocalHitRevalidations(now) {
154
+ this.cleanupExpiringMap(this.recentLocalHitRevalidations, now);
155
+ }
156
+ // test helper
157
+ getMissCacheSize() {
158
+ return this.recentMisses.size;
159
+ }
160
+ // test helper
161
+ getLocalHitRevalidationCacheSize() {
162
+ return this.recentLocalHitRevalidations.size;
163
+ }
164
+ };
165
+ var readRequestManager = new ReadRequestManager();
166
+
167
+ // packages/database/actions/read.ts
168
+ var updateClientDbIfNewer = async (clientDb, dbKey, remoteData, localData) => {
169
+ if (!clientDb) return;
170
+ try {
171
+ if (isRemoteDataNewer(remoteData, localData)) {
172
+ await clientDb.put(
173
+ dbKey,
174
+ normalizeReadRecord(dbKey, remoteData, { forCache: true })
175
+ );
176
+ }
177
+ } catch (err) {
178
+ throw err;
179
+ }
180
+ };
181
+ var normalizeReadRecord = (dbKey, data, options = {}) => {
182
+ if (!data || typeof data !== "object") return data;
183
+ const baseRecord = options.forCache ? data : { ...data, dbKey };
184
+ return baseRecord;
185
+ };
186
+ var isRemoteDataNewer = (remoteData, localData) => {
187
+ if (!remoteData || typeof remoteData !== "object") return false;
188
+ if (!localData || typeof localData !== "object") return true;
189
+ return shouldReplaceWithNextRecord(remoteData, localData);
190
+ };
191
+ var syncLocalDataToServer = async (replicationContext, dbKey, localData) => {
192
+ try {
193
+ scheduleExistingRecordReplication({
194
+ currentServer: replicationContext.currentServer,
195
+ syncServers: replicationContext.syncServers,
196
+ dbKey,
197
+ localData,
198
+ state: replicationContext.state
199
+ });
200
+ } catch (err) {
201
+ }
202
+ };
203
+ var saveRemoteDataToClientDb = async (clientDb, dbKey, remoteData, serverOrigin) => {
204
+ if (!clientDb) return;
205
+ try {
206
+ const normalizedRemoteData = normalizeReadRecord(dbKey, remoteData, {
207
+ forCache: true
208
+ });
209
+ await clientDb.put(
210
+ dbKey,
211
+ serverOrigin ? {
212
+ ...normalizedRemoteData,
213
+ serverOrigin
214
+ } : normalizedRemoteData
215
+ );
216
+ } catch (err) {
217
+ }
218
+ };
219
+ var processRemoteDataInBackground = async (clientDb, dbKey, remotePromises, remoteServers, localData, replicationContext) => {
220
+ if (!clientDb) return;
221
+ try {
222
+ const settledResults = await Promise.allSettled(remotePromises);
223
+ const remoteResult = pickBestSettledRemoteRecord({
224
+ settledResults,
225
+ isBetterCandidate: (current, latest) => shouldReplaceWithNextRecord(current, latest)
226
+ });
227
+ const validRemoteData = remoteResult ? remoteResult.data : null;
228
+ const serverOrigin = remoteResult && remoteServers[remoteResult.index] ? remoteServers[remoteResult.index] : void 0;
229
+ if (shouldReplaceLocalWithRemoteRecord({
230
+ localData,
231
+ remoteData: validRemoteData,
232
+ isRemoteNewer: isRemoteDataNewer
233
+ })) {
234
+ await updateClientDbIfNewer(
235
+ clientDb,
236
+ dbKey,
237
+ serverOrigin ? { ...validRemoteData, serverOrigin } : validRemoteData,
238
+ localData
239
+ );
240
+ }
241
+ if (shouldReplicateLocalRecord({
242
+ localData,
243
+ remoteData: validRemoteData,
244
+ remoteTargetCount: remotePromises.length
245
+ })) {
246
+ await syncLocalDataToServer(replicationContext, dbKey, localData);
247
+ }
248
+ } catch (err) {
249
+ }
250
+ };
251
+ var readAction = async (payload, thunkApi) => {
252
+ const dbKey = payload.dbKey;
253
+ const signal = payload.signal;
254
+ const preferredServerOrigin = payload.preferredServerOrigin;
255
+ if (!dbKey || typeof dbKey !== "string") {
256
+ throw new Error("readAction requires a non-empty dbKey.");
257
+ }
258
+ if (signal?.aborted) {
259
+ throw new DOMException("Aborted", "AbortError");
260
+ }
261
+ const { db: clientDb } = thunkApi.extra;
262
+ if (!clientDb) {
263
+ throw new Error("Client database is not available.");
264
+ }
265
+ const executeRead = async () => {
266
+ const isDialogKey = dbKey.startsWith("dialog-") && !dbKey.includes("-msg-");
267
+ const readStartedAt = isDialogKey ? Date.now() : 0;
268
+ const state = thunkApi.getState();
269
+ const {
270
+ currentToken,
271
+ remoteServers: configuredServers,
272
+ currentServer,
273
+ currentUserId,
274
+ syncServers,
275
+ userAuthorityRegistry
276
+ } = getRuntimeServerContext(state, preferredServerOrigin);
277
+ const allServers = resolveAgentReadServers({ dbKey, configuredServers });
278
+ const isLoggedIn = !!currentToken;
279
+ const now = Date.now();
280
+ const localData = await fetchFromClientDb(clientDb, dbKey);
281
+ if (isDialogKey) {
282
+ console.info("[readAction-perf] dialog local-read", {
283
+ dbKey,
284
+ localHit: !!localData,
285
+ localReadMs: Date.now() - readStartedAt,
286
+ serverCount: allServers.length
287
+ });
288
+ }
289
+ const authority = resolveRecordAuthority({
290
+ dbKey,
291
+ record: localData,
292
+ currentUserId,
293
+ currentServer,
294
+ userAuthorityRegistry
295
+ });
296
+ const authorityPlannedServers = planAuthorityReadServers({
297
+ allServers,
298
+ authorityServer: preferredServerOrigin ?? authority.authorityServer,
299
+ serverOrigin: authority.serverOrigin
300
+ });
301
+ const { preferredServer, fallbackServers, orderedServersForLocalHit } = partitionReadServers({
302
+ allServers: authorityPlannedServers,
303
+ preferredServerOrigin: preferredServerOrigin ?? authority.authorityServer
304
+ });
305
+ readRequestManager.cleanupMisses(now);
306
+ readRequestManager.cleanupLocalHitRevalidations(now);
307
+ if (localData) {
308
+ readRequestManager.clearMiss(dbKey);
309
+ } else {
310
+ const retryInMs = readRequestManager.getRetryInMs(dbKey, now);
311
+ if (typeof retryInMs === "number" && retryInMs > 0) {
312
+ logger.debug(
313
+ { dbKey, retryInMs },
314
+ "[readAction] Suppressing repeated miss read"
315
+ );
316
+ throw new Error(
317
+ `Read temporarily suppressed for missing key "${dbKey}".`
318
+ );
319
+ }
320
+ }
321
+ if (authorityPlannedServers.length === 0) {
322
+ if (localData) {
323
+ if (isDialogKey) {
324
+ console.info("[readAction-perf] dialog offline-local-hit", {
325
+ dbKey,
326
+ totalMs: Date.now() - readStartedAt
327
+ });
328
+ }
329
+ return normalizeReadRecord(dbKey, localData);
330
+ }
331
+ readRequestManager.markMiss(dbKey, now);
332
+ throw new Error(
333
+ `Failed to fetch data for key "${dbKey}" because network is offline and no local data is available.`
334
+ );
335
+ }
336
+ if (localData) {
337
+ const retryInMs = signal?.aborted ? void 0 : readRequestManager.getLocalHitRevalidateInMs(dbKey, now);
338
+ if (!signal?.aborted) {
339
+ if (retryInMs === null) {
340
+ readRequestManager.markLocalHitRevalidated(dbKey, now);
341
+ const remotePromises2 = orderedServersForLocalHit.map(
342
+ (server) => fetchFromServer(
343
+ server,
344
+ dbKey,
345
+ isLoggedIn ? currentToken : void 0,
346
+ signal
347
+ )
348
+ );
349
+ void processRemoteDataInBackground(
350
+ clientDb,
351
+ dbKey,
352
+ remotePromises2,
353
+ orderedServersForLocalHit,
354
+ localData,
355
+ { currentServer, syncServers, state }
356
+ );
357
+ } else {
358
+ logger.debug(
359
+ { dbKey, retryInMs },
360
+ "[readAction] Skipping frequent local-hit revalidation"
361
+ );
362
+ }
363
+ }
364
+ if (isDialogKey) {
365
+ console.info("[readAction-perf] dialog local-hit-return", {
366
+ dbKey,
367
+ totalMs: Date.now() - readStartedAt,
368
+ revalidating: retryInMs === null
369
+ });
370
+ }
371
+ return normalizeReadRecord(dbKey, localData);
372
+ }
373
+ if (preferredServer) {
374
+ try {
375
+ const preferredRemoteData = await fetchFromServer(
376
+ preferredServer,
377
+ dbKey,
378
+ isLoggedIn ? currentToken : void 0,
379
+ signal
380
+ );
381
+ if (preferredRemoteData) {
382
+ await saveRemoteDataToClientDb(
383
+ clientDb,
384
+ dbKey,
385
+ preferredRemoteData,
386
+ preferredServer
387
+ );
388
+ readRequestManager.clearMiss(dbKey);
389
+ if (isDialogKey) {
390
+ console.info("[readAction-perf] dialog preferred-remote-hit", {
391
+ dbKey,
392
+ totalMs: Date.now() - readStartedAt,
393
+ server: preferredServer
394
+ });
395
+ }
396
+ return normalizeReadRecord(dbKey, {
397
+ ...preferredRemoteData,
398
+ serverOrigin: preferredServer
399
+ });
400
+ }
401
+ } catch (error) {
402
+ if (signal?.aborted || isAbortError(error)) {
403
+ throw error;
404
+ }
405
+ if (isReadTimeoutError(error)) {
406
+ logger.warn(
407
+ { dbKey, preferredServer, error: String(error.message) },
408
+ "[readAction] Preferred server timed out; falling back to remaining servers"
409
+ );
410
+ } else {
411
+ logger.warn(
412
+ { dbKey, preferredServer, error: String(error) },
413
+ "[readAction] Preferred server read failed; falling back to remaining servers"
414
+ );
415
+ }
416
+ }
417
+ }
418
+ const remotePromises = fallbackServers.map(
419
+ (server) => fetchFromServer(
420
+ server,
421
+ dbKey,
422
+ isLoggedIn ? currentToken : void 0,
423
+ signal
424
+ )
425
+ );
426
+ const settledResults = await Promise.allSettled(remotePromises);
427
+ if (signal?.aborted) {
428
+ throw new DOMException("Aborted", "AbortError");
429
+ }
430
+ const remoteResult = pickBestSettledRemoteRecord({
431
+ settledResults,
432
+ isBetterCandidate: (current, latest) => shouldReplaceWithNextRecord(current, latest)
433
+ });
434
+ if (remoteResult) {
435
+ const { data: validRemoteData } = remoteResult;
436
+ const serverOrigin = fallbackServers[remoteResult.index];
437
+ if (!signal?.aborted) {
438
+ await saveRemoteDataToClientDb(
439
+ clientDb,
440
+ dbKey,
441
+ validRemoteData,
442
+ serverOrigin
443
+ );
444
+ }
445
+ readRequestManager.clearMiss(dbKey);
446
+ const remoteData = serverOrigin ? { ...validRemoteData, dbKey, serverOrigin } : { ...validRemoteData, dbKey };
447
+ return normalizeReadRecord(dbKey, remoteData);
448
+ }
449
+ readRequestManager.markMiss(dbKey, Date.now());
450
+ throw new Error(
451
+ `Failed to fetch data for key "${dbKey}" from all sources.`
452
+ );
453
+ };
454
+ const canDedup = !signal;
455
+ if (canDedup) {
456
+ const existing = readRequestManager.getInFlight(dbKey);
457
+ if (existing) return existing;
458
+ let inFlightPromise;
459
+ inFlightPromise = executeRead().finally(() => {
460
+ readRequestManager.clearInFlight(dbKey, inFlightPromise);
461
+ });
462
+ readRequestManager.setInFlight(dbKey, inFlightPromise);
463
+ return inFlightPromise;
464
+ }
465
+ return executeRead();
466
+ };
467
+
468
+ export {
469
+ BUILTIN_NOLO_AGENT_KEY,
470
+ PUBLIC_DEEPSEEK_V4_FLASH_AGENT_KEY,
471
+ PUBLIC_DEEPSEEK_V4_PRO_AGENT_KEY,
472
+ PUBLIC_KIMI_K27_CODING_AGENT_KEY,
473
+ PUBLIC_GLM_52_AGENT_KEY,
474
+ PUBLIC_KIMI_K26_IMAGE_AGENT_KEY,
475
+ readRequestManager,
476
+ resolveAgentReadServers,
477
+ readAction
478
+ };