nolo-cli 0.1.32 → 0.1.33

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 (230) hide show
  1. package/README.md +0 -6
  2. package/agent-runtime/agentRecordConfig.ts +2 -0
  3. package/agent-runtime/hostAdapter.ts +1 -0
  4. package/agent-runtime/index.ts +0 -21
  5. package/agent-runtime/localToolPolicy.ts +6 -13
  6. package/agent-runtime/localWorkspaceTools.ts +30 -333
  7. package/agent-runtime/platformChatProvider.ts +1 -4
  8. package/agent-runtime/runtimeToolPolicy.ts +47 -32
  9. package/agentAliases.ts +14 -83
  10. package/agentCommandSupport.ts +3 -6
  11. package/agentInternalCommandEntries.ts +2 -13
  12. package/agentListCommands.ts +19 -28
  13. package/agentMachineCommands.ts +40 -336
  14. package/agentPullCommand.ts +36 -56
  15. package/agentRecordCommands.ts +3 -5
  16. package/agentRecordHelpers.ts +22 -189
  17. package/agentRunCommand.ts +438 -107
  18. package/agentRuntimeLocal.ts +0 -10
  19. package/ai/agent/cliExecutor.ts +19 -323
  20. package/authCommands.ts +2 -36
  21. package/cli/agentAliases.ts +14 -83
  22. package/cli/agentCommandSupport.ts +3 -6
  23. package/cli/agentInternalCommandEntries.ts +2 -13
  24. package/cli/agentListCommands.ts +19 -28
  25. package/cli/agentMachineCommands.ts +40 -336
  26. package/cli/agentPullCommand.ts +36 -56
  27. package/cli/agentRecordCommands.ts +3 -5
  28. package/cli/agentRecordHelpers.ts +22 -189
  29. package/cli/agentRunCommand.ts +438 -107
  30. package/cli/agentRuntimeLocal.ts +0 -10
  31. package/cli/authCommands.ts +2 -36
  32. package/cli/cliEnvHelpers.ts +21 -162
  33. package/cli/client/agentRun.ts +853 -57
  34. package/cli/client/localRuntimeAdapter.ts +22 -159
  35. package/cli/client/streamingOutput.ts +3 -1
  36. package/cli/client/taskRunPrompt.ts +24 -7
  37. package/cli/commandRegistry.ts +2 -3
  38. package/cli/connectorRunArtifact.ts +10 -34
  39. package/cli/connectorWebSocketTarget.ts +6 -10
  40. package/cli/internalCommandEntries.ts +0 -8
  41. package/cli/localRuntimeDb.ts +2 -39
  42. package/cli/machineCommands.ts +189 -190
  43. package/cli/machineDaemonCommands.ts +165 -9
  44. package/cli/machineHeartbeatCommands.ts +2 -1
  45. package/cli/machineStatusCommands.ts +13 -5
  46. package/cli/machineWatchCommands.ts +1 -1
  47. package/cli/machineWsRunDispatch.ts +14 -48
  48. package/cli/machineWsSession.ts +0 -2
  49. package/cli/offlineMarxistsAgentCommand.ts +314 -0
  50. package/cli/runtimeDoctorCommands.ts +69 -18
  51. package/cli/scriptCommandEntries.ts +45 -5
  52. package/cli/tableCommands.ts +133 -33
  53. package/cli/taskRunCommand.ts +211 -16
  54. package/cli/tui/session.ts +372 -43
  55. package/cli/workflowInternalCommandEntries.ts +0 -20
  56. package/cliEnvHelpers.ts +21 -162
  57. package/client/agentConfigResolver.test.ts +2 -0
  58. package/client/agentRun.test.ts +12 -208
  59. package/client/agentRun.ts +853 -57
  60. package/client/localRuntimeAdapter.test.ts +9 -168
  61. package/client/localRuntimeAdapter.ts +22 -159
  62. package/client/localToolPolicy.test.ts +4 -28
  63. package/client/streamingOutput.ts +3 -1
  64. package/client/taskRunPrompt.ts +24 -7
  65. package/commandRegistry.ts +2 -3
  66. package/connectorRunArtifact.ts +10 -34
  67. package/connectorWebSocketTarget.ts +6 -10
  68. package/database/server/db.ts +34 -15
  69. package/internalCommandEntries.ts +0 -8
  70. package/localRuntimeDb.ts +2 -39
  71. package/machineCommands.ts +189 -190
  72. package/machineDaemonCommands.ts +165 -9
  73. package/machineHeartbeatCommands.ts +2 -1
  74. package/machineStatusCommands.ts +13 -5
  75. package/machineWatchCommands.ts +1 -1
  76. package/machineWsRunDispatch.ts +14 -48
  77. package/machineWsSession.ts +0 -2
  78. package/offlineMarxistsAgentCommand.ts +314 -0
  79. package/package.json +49 -193
  80. package/runtimeDoctorCommands.ts +69 -18
  81. package/scriptCommandEntries.ts +45 -5
  82. package/tableCommands.ts +133 -33
  83. package/taskRunCommand.ts +211 -16
  84. package/tui/session.ts +372 -43
  85. package/workflowInternalCommandEntries.ts +0 -20
  86. package/agent-runtime/dialogStatus.ts +0 -139
  87. package/agent-runtime/hostRuntimeConfig.ts +0 -86
  88. package/agentCreateCustomCommands.ts +0 -278
  89. package/agentDialogsCommands.ts +0 -227
  90. package/agentDoctorCommands.ts +0 -203
  91. package/agentRunLocalWorkspace.ts +0 -182
  92. package/agentRunParsing.ts +0 -298
  93. package/agentSuperviseCommands.ts +0 -8
  94. package/agentUnpublishCommands.ts +0 -113
  95. package/ai/agent/agentRunDialogLineage.ts +0 -56
  96. package/ai/policy/types.ts +0 -64
  97. package/ai/skills/skillSummaryMarker.ts +0 -26
  98. package/app/types.ts +0 -358
  99. package/build.ts +0 -16
  100. package/buildPublish.ts +0 -138
  101. package/buildPublishArtifactAssembly.ts +0 -724
  102. package/buildPublishImportAnalysis.ts +0 -242
  103. package/chat/messages/fetchMessages.ts +0 -84
  104. package/chat/messages/types.ts +0 -185
  105. package/cli/agentCreateCustomCommands.ts +0 -278
  106. package/cli/agentDialogsCommands.ts +0 -227
  107. package/cli/agentDoctorCommands.ts +0 -203
  108. package/cli/agentRunLocalWorkspace.ts +0 -182
  109. package/cli/agentRunParsing.ts +0 -298
  110. package/cli/agentSuperviseCommands.ts +0 -8
  111. package/cli/agentUnpublishCommands.ts +0 -113
  112. package/cli/buildPublish.ts +0 -138
  113. package/cli/buildPublishArtifactAssembly.ts +0 -724
  114. package/cli/buildPublishImportAnalysis.ts +0 -242
  115. package/cli/cliIoTypes.ts +0 -3
  116. package/cli/cliRuntimeDoctorReport.ts +0 -193
  117. package/cli/client/agentRunInput.ts +0 -10
  118. package/cli/client/agentRunLocalExecution.ts +0 -124
  119. package/cli/client/agentRunLocalExecutor.ts +0 -179
  120. package/cli/client/agentRunRuntimeSelection.ts +0 -145
  121. package/cli/client/agentRunServerTransport.ts +0 -397
  122. package/cli/client/agentRunTaskRunRecorder.ts +0 -176
  123. package/cli/customCodingAgentFlow.ts +0 -208
  124. package/cli/customCodingAgentRecord.ts +0 -179
  125. package/cli/dialogDataAccess.ts +0 -186
  126. package/cli/dialogInternalCommandEntries.ts +0 -11
  127. package/cli/dialogReadCommands.ts +0 -180
  128. package/cli/dialogReadHelpers.ts +0 -226
  129. package/cli/dialogStatusCommands.ts +0 -123
  130. package/cli/docInternalCommandEntries.ts +0 -20
  131. package/cli/hybridReadHelpers.ts +0 -109
  132. package/cli/machineCommandPrompts.ts +0 -79
  133. package/cli/machineCommandResolution.ts +0 -43
  134. package/cli/noloReadBridgePrompt.ts +0 -16
  135. package/cli/pageCreateCommands.ts +0 -219
  136. package/cli/pageDeleteCommands.ts +0 -167
  137. package/cli/pageReadCommands.ts +0 -151
  138. package/cli/pageUpdateCommands.ts +0 -249
  139. package/cli/pageWriteHelpers.ts +0 -209
  140. package/cli/publishPackage.ts +0 -36
  141. package/cli/runtimeInternalCommandEntries.ts +0 -15
  142. package/cli/runtimePassthroughCommands.ts +0 -22
  143. package/cli/scriptPassthroughHelpers.ts +0 -28
  144. package/cli/secretScanHelpers.ts +0 -31
  145. package/cli/spaceAttachCommands.ts +0 -134
  146. package/cli/spaceCategoryCommands.ts +0 -263
  147. package/cli/spaceCommands.ts +0 -295
  148. package/cli/spaceDeleteCommands.ts +0 -312
  149. package/cli/spaceInternalCommandEntries.ts +0 -21
  150. package/cli/spaceMutationHelpers.ts +0 -135
  151. package/cli/spaceReadCommands.ts +0 -323
  152. package/cli/tableDataOutput.ts +0 -41
  153. package/cli/tableDataSafety.ts +0 -26
  154. package/cli/tableMetaCommands.ts +0 -307
  155. package/cli/tableMutationCommands.ts +0 -266
  156. package/cli/tableQueryParsing.ts +0 -90
  157. package/cli/tableQueryRequest.ts +0 -53
  158. package/cli/taskRunActions.ts +0 -122
  159. package/cli/taskRunParsing.ts +0 -86
  160. package/cli/taskRunPromptShared.ts +0 -55
  161. package/cli/tui/tuiCatalog.ts +0 -22
  162. package/cli/tui/tuiCommandInterpreter.ts +0 -253
  163. package/cli/tui/tuiPresentation.ts +0 -146
  164. package/cliIoTypes.ts +0 -3
  165. package/cliRuntimeDoctorReport.ts +0 -193
  166. package/client/agentRunInput.ts +0 -10
  167. package/client/agentRunLocalExecution.test.ts +0 -123
  168. package/client/agentRunLocalExecution.ts +0 -124
  169. package/client/agentRunLocalExecutor.ts +0 -179
  170. package/client/agentRunRuntimeSelection.test.ts +0 -159
  171. package/client/agentRunRuntimeSelection.ts +0 -145
  172. package/client/agentRunServerTransport.ts +0 -397
  173. package/client/agentRunTaskRunRecorder.ts +0 -176
  174. package/connector-experimental/daemonLauncher.ts +0 -223
  175. package/create/space/spaceKeys.ts +0 -65
  176. package/create/types.ts +0 -21
  177. package/customCodingAgentFlow.ts +0 -208
  178. package/customCodingAgentRecord.ts +0 -179
  179. package/database/keys.ts +0 -765
  180. package/database/server/authorityStoreTypes.ts +0 -31
  181. package/database/server/legacyServerDb.ts +0 -58
  182. package/database/server/levelAuthorityStore.ts +0 -75
  183. package/database/server/memoryAuthorityStore.ts +0 -133
  184. package/database/server/serverStoreFactory.ts +0 -124
  185. package/database/tombstones.ts +0 -52
  186. package/database/utils/ulid.ts +0 -1
  187. package/dialogDataAccess.ts +0 -186
  188. package/dialogInternalCommandEntries.ts +0 -11
  189. package/dialogReadCommands.ts +0 -180
  190. package/dialogReadHelpers.ts +0 -226
  191. package/dialogStatusCommands.ts +0 -123
  192. package/docInternalCommandEntries.ts +0 -20
  193. package/hybridReadHelpers.ts +0 -109
  194. package/machineCommandPrompts.ts +0 -79
  195. package/machineCommandResolution.ts +0 -43
  196. package/noloReadBridgePrompt.ts +0 -16
  197. package/pageCreateCommands.ts +0 -219
  198. package/pageDeleteCommands.ts +0 -167
  199. package/pageReadCommands.ts +0 -151
  200. package/pageUpdateCommands.ts +0 -249
  201. package/pageWriteHelpers.ts +0 -209
  202. package/publishPackage.ts +0 -36
  203. package/runtimeInternalCommandEntries.ts +0 -15
  204. package/runtimePassthroughCommands.ts +0 -22
  205. package/scriptPassthroughHelpers.ts +0 -28
  206. package/secretScanHelpers.ts +0 -31
  207. package/share/helpers.ts +0 -302
  208. package/share/keys.ts +0 -243
  209. package/share/types.ts +0 -100
  210. package/spaceAttachCommands.ts +0 -134
  211. package/spaceCategoryCommands.ts +0 -263
  212. package/spaceCommands.ts +0 -295
  213. package/spaceDeleteCommands.ts +0 -312
  214. package/spaceInternalCommandEntries.ts +0 -21
  215. package/spaceMutationHelpers.ts +0 -135
  216. package/spaceReadCommands.ts +0 -323
  217. package/tableDataOutput.ts +0 -41
  218. package/tableDataSafety.ts +0 -26
  219. package/tableMetaCommands.ts +0 -307
  220. package/tableMutationCommands.ts +0 -266
  221. package/tableQueryParsing.ts +0 -90
  222. package/tableQueryRequest.ts +0 -53
  223. package/taskRunActions.ts +0 -122
  224. package/taskRunParsing.ts +0 -86
  225. package/taskRunPromptShared.ts +0 -55
  226. package/tui/tuiCatalog.ts +0 -22
  227. package/tui/tuiCommandInterpreter.test.ts +0 -169
  228. package/tui/tuiCommandInterpreter.ts +0 -253
  229. package/tui/tuiPresentation.test.ts +0 -154
  230. package/tui/tuiPresentation.ts +0 -146
package/share/helpers.ts DELETED
@@ -1,302 +0,0 @@
1
- import {
2
- createAgentKey,
3
- createCybotKey,
4
- isAgentKey,
5
- splitKey,
6
- } from "../database/keys";
7
- import { DataType } from "../create/types";
8
- import type { ShareType } from "./types";
9
-
10
- // ── String normalization ────────────────────────────────────────────
11
-
12
- export const toSafeString = (value: unknown): string =>
13
- typeof value === "string" ? value.trim() : "";
14
-
15
- export const toNonEmptyString = (value: unknown): string | undefined => {
16
- const text = toSafeString(value);
17
- return text || undefined;
18
- };
19
-
20
- export const normalizeUserId = (value: unknown): string => {
21
- const text = toSafeString(value);
22
- return text.startsWith("user:") ? text.slice(5) : text;
23
- };
24
-
25
- export const normalizeAuthorName = (value: unknown): string => {
26
- const text = toSafeString(value);
27
- if (!text) return "";
28
- const lower = text.toLowerCase();
29
- if (lower === "unknown" || lower === "unknown user") return "";
30
- return text;
31
- };
32
-
33
- export const toSafeAgentKey = (value: unknown): string => {
34
- const text = toSafeString(value);
35
- return text && isAgentKey(text) ? text : "";
36
- };
37
-
38
- // ── Time helpers ────────────────────────────────────────────────────
39
-
40
- export const toSafeTimestamp = (value: unknown): number => {
41
- const num = Number(value);
42
- if (Number.isFinite(num) && num > 0) return num;
43
- if (typeof value !== "string") return 0;
44
- const parsed = Date.parse(value);
45
- return Number.isFinite(parsed) && parsed > 0 ? parsed : 0;
46
- };
47
-
48
- const SHARE_TIME_FORMATTER = new Intl.DateTimeFormat("zh-CN", {
49
- timeZone: "Asia/Shanghai",
50
- year: "numeric",
51
- month: "2-digit",
52
- day: "2-digit",
53
- hour: "2-digit",
54
- minute: "2-digit",
55
- second: "2-digit",
56
- hour12: false,
57
- });
58
-
59
- export const formatShareTime = (timestamp: number): string => {
60
- if (!timestamp) return "时间未知";
61
- const date = new Date(timestamp);
62
- return Number.isFinite(date.getTime()) ? SHARE_TIME_FORMATTER.format(date) : "时间未知";
63
- };
64
-
65
- // ── Agent extraction ────────────────────────────────────────────────
66
-
67
- const findAgentKeyInMessages = (messages: unknown[]): string => {
68
- for (const raw of messages) {
69
- if (!raw || typeof raw !== "object") continue;
70
- const key = toSafeAgentKey((raw as Record<string, unknown>).cybotKey);
71
- if (key) return key;
72
- }
73
- return "";
74
- };
75
-
76
- const findAgentKeyInArray = (items: unknown[]): string => {
77
- for (const raw of items) {
78
- const direct = toSafeAgentKey(raw);
79
- if (direct) return direct;
80
- if (!raw || typeof raw !== "object") continue;
81
- const item = raw as Record<string, unknown>;
82
- const key = toSafeAgentKey(item.dbKey) || toSafeAgentKey(item.id);
83
- if (key) return key;
84
- }
85
- return "";
86
- };
87
-
88
- const findAgentNameInMessages = (messages: unknown[]): string => {
89
- for (const raw of messages) {
90
- if (!raw || typeof raw !== "object") continue;
91
- const msg = raw as Record<string, unknown>;
92
- const name =
93
- toSafeString(msg.agentName) ||
94
- toSafeString(msg.cybotName) ||
95
- toSafeString(msg.sourceAgentName);
96
- if (name) return name;
97
- }
98
- return "";
99
- };
100
-
101
- const findAgentNameInArray = (items: unknown[]): string => {
102
- for (const raw of items) {
103
- if (!raw || typeof raw !== "object") continue;
104
- const item = raw as Record<string, unknown>;
105
- const name =
106
- toSafeString(item.name) ||
107
- toSafeString(item.agentName) ||
108
- toSafeString(item.cybotName) ||
109
- toSafeString(item.sourceAgentName);
110
- if (name) return name;
111
- }
112
- return "";
113
- };
114
-
115
- export const extractAgentInfo = (
116
- type: ShareType,
117
- data: Record<string, unknown>
118
- ): { sourceAgentKey?: string; sourceAgentName?: string } => {
119
- const meta = data.meta as Record<string, unknown> | undefined;
120
-
121
- // Candidate keys in priority order
122
- const keyCandidates: unknown[] = [
123
- meta?.sourceAgentKey,
124
- meta?.agentKey,
125
- data.sourceAgentKey,
126
- data.agentKey,
127
- data.cybotKey,
128
- type === DataType.CYBOT ? (data.dbKey ?? data.id) : undefined,
129
- ];
130
-
131
- let agentKey = "";
132
- for (const candidate of keyCandidates) {
133
- agentKey = toSafeAgentKey(candidate);
134
- if (agentKey) break;
135
- }
136
-
137
- // Fallback: search in arrays
138
- if (!agentKey && Array.isArray(data.cybots)) {
139
- agentKey = findAgentKeyInArray(data.cybots);
140
- }
141
- if (!agentKey && Array.isArray(data.messages)) {
142
- agentKey = findAgentKeyInMessages(data.messages as unknown[]);
143
- }
144
- if (!agentKey && Array.isArray(data.history)) {
145
- agentKey = findAgentKeyInMessages(data.history as unknown[]);
146
- }
147
-
148
- // Agent name
149
- const nameCandidates: unknown[] = [
150
- meta?.sourceAgentName,
151
- meta?.agentName,
152
- meta?.cybotName,
153
- data.sourceAgentName,
154
- data.agentName,
155
- data.cybotName,
156
- type === DataType.CYBOT ? data.name : undefined,
157
- ];
158
-
159
- let agentName = "";
160
- for (const candidate of nameCandidates) {
161
- agentName = toSafeString(candidate);
162
- if (agentName) break;
163
- }
164
- if (!agentName && Array.isArray(data.cybots)) {
165
- agentName = findAgentNameInArray(data.cybots);
166
- }
167
- if (!agentName && Array.isArray(data.messages)) {
168
- agentName = findAgentNameInMessages(data.messages as unknown[]);
169
- }
170
- if (!agentName && Array.isArray(data.history)) {
171
- agentName = findAgentNameInMessages(data.history as unknown[]);
172
- }
173
-
174
- return {
175
- ...(agentKey ? { sourceAgentKey: agentKey } : {}),
176
- ...(agentName ? { sourceAgentName: agentName } : {}),
177
- };
178
- };
179
-
180
- // ── Cover image extraction ──────────────────────────────────────────
181
-
182
- const extractImageFromContent = (content: unknown): string | undefined => {
183
- if (Array.isArray(content)) {
184
- const part = content.find(
185
- (p: any) => p?.type === "image_url" && typeof p?.image_url?.url === "string"
186
- );
187
- if (part?.image_url?.url) return part.image_url.url;
188
- }
189
- if (typeof content === "string") {
190
- const match = content.match(/!\[.*?\]\((.*?)\)/);
191
- if (match?.[1]) return match[1];
192
- }
193
- return undefined;
194
- };
195
-
196
- export const extractCoverImage = (
197
- type: ShareType,
198
- data: Record<string, unknown>
199
- ): string | undefined => {
200
- if (type === DataType.IMAGE) {
201
- return toNonEmptyString(data.url);
202
- }
203
- if (type === DataType.APP) {
204
- return toNonEmptyString(data.coverImage);
205
- }
206
- if (type !== DataType.DIALOG) return undefined;
207
-
208
- const messages = (Array.isArray(data.messages)
209
- ? data.messages
210
- : Array.isArray(data.history)
211
- ? data.history
212
- : []) as Record<string, unknown>[];
213
-
214
- for (const msg of messages) {
215
- const fromContent = extractImageFromContent(msg.content);
216
- if (fromContent) return fromContent;
217
-
218
- const image = toNonEmptyString(msg.image);
219
- if (image) return image;
220
-
221
- if (Array.isArray(msg.images)) {
222
- const first = (msg.images as unknown[]).find(
223
- (u) => typeof u === "string" && u.trim().length > 0
224
- );
225
- if (first) return first as string;
226
- }
227
- }
228
- return undefined;
229
- };
230
-
231
- // ── Author extraction ────────────────────────────────────────────────
232
-
233
- export const toPublicAgentKey = (value: unknown): string => {
234
- const key = toSafeAgentKey(value);
235
- if (!key) return "";
236
-
237
- const parts = splitKey(key);
238
- if (parts.length < 3) return "";
239
-
240
- const [type, owner] = parts;
241
- const agentId = parts[parts.length - 1];
242
- if (!agentId) return "";
243
- if (owner === "pub") return key;
244
-
245
- if (type === DataType.AGENT) {
246
- return createAgentKey.public(agentId);
247
- }
248
- if (type === DataType.CYBOT) {
249
- return createCybotKey.public(agentId);
250
- }
251
- return "";
252
- };
253
-
254
- export const resolveShareAuthorIdentity = (args: {
255
- user?: Record<string, unknown> | null;
256
- profile?: Record<string, unknown> | null;
257
- fallbackName?: unknown;
258
- fallbackAvatar?: unknown;
259
- }): { authorName?: string; authorAvatar?: string } => {
260
- const authorName =
261
- toNonEmptyString(args.profile?.nickname) ??
262
- toNonEmptyString(args.user?.name) ??
263
- toNonEmptyString(args.user?.nickname) ??
264
- toNonEmptyString(args.user?.username) ??
265
- toNonEmptyString(args.fallbackName);
266
- const authorAvatar =
267
- toNonEmptyString(args.profile?.avatar) ??
268
- toNonEmptyString(args.user?.avatar) ??
269
- toNonEmptyString(args.fallbackAvatar);
270
-
271
- return {
272
- ...(authorName ? { authorName } : {}),
273
- ...(authorAvatar ? { authorAvatar } : {}),
274
- };
275
- };
276
-
277
- // ── Error helpers ───────────────────────────────────────────────────
278
-
279
- export const isLevelNotFoundError = (error: unknown): boolean => {
280
- if (!error || typeof error !== "object") return false;
281
- const e = error as Record<string, unknown>;
282
- return (
283
- e.notFound === true ||
284
- e.name === "NotFoundError" ||
285
- e.code === "LEVEL_NOT_FOUND" ||
286
- e.code === "LEVEL_NOT_FOUND_ERROR"
287
- );
288
- };
289
-
290
- // ── Sanitization ────────────────────────────────────────────────────
291
-
292
- const SENSITIVE_FIELDS = ["apiKey", "secret", "password"] as const;
293
-
294
- export const sanitizeShareData = (
295
- data: Record<string, unknown>
296
- ): Record<string, unknown> => {
297
- const snapshot = { ...data };
298
- for (const field of SENSITIVE_FIELDS) {
299
- delete snapshot[field];
300
- }
301
- return snapshot;
302
- };
package/share/keys.ts DELETED
@@ -1,243 +0,0 @@
1
- import { createKey, splitKey, isAgentKey } from "../database/keys";
2
- import { DataType } from "../create/types";
3
- import { toSafeAgentKey } from "./helpers";
4
-
5
- const SHARE_PREFIX = "share";
6
- const SHARE_INDEX_PREFIX = "shareidx";
7
- const TIMESTAMP_MAX = 9_999_999_999_999;
8
-
9
- // ── Normalization ───────────────────────────────────────────────────
10
-
11
- const normalizeUserId = (value: unknown): string => {
12
- const text = String(value || "").trim();
13
- return text.startsWith("user:") ? text.slice(5) : text;
14
- };
15
-
16
- const normalizeToken = (value: unknown): string =>
17
- String(value || "").trim();
18
-
19
- const normalizeCreatedAt = (value: unknown): number => {
20
- const parsed = Math.floor(Number(value));
21
- if (!Number.isFinite(parsed) || parsed < 0) return 0;
22
- return Math.min(parsed, TIMESTAMP_MAX);
23
- };
24
-
25
- const toInvertedTimestamp = (value: unknown): string =>
26
- String(TIMESTAMP_MAX - normalizeCreatedAt(value)).padStart(13, "0");
27
-
28
- // ── Community index builders ────────────────────────────────────────
29
-
30
- type CommunityDimension = "creator" | "agent";
31
-
32
- const normalizeDimensionValue = (dim: CommunityDimension, raw: unknown): string =>
33
- dim === "creator" ? normalizeUserId(raw) : toSafeAgentKey(raw);
34
-
35
- const createCommunityIndex = (
36
- dim: CommunityDimension,
37
- rawValue: unknown,
38
- createdAt: unknown,
39
- token: unknown
40
- ): string => {
41
- const value = normalizeDimensionValue(dim, rawValue);
42
- const normalizedToken = normalizeToken(token);
43
- if (!value || !normalizedToken) return "";
44
- return createKey(SHARE_INDEX_PREFIX, "community", dim, value, toInvertedTimestamp(createdAt), normalizedToken);
45
- };
46
-
47
- const createCommunityRange = (dim: CommunityDimension, rawValue: unknown) => {
48
- const value = normalizeDimensionValue(dim, rawValue);
49
- if (!value) return { start: "", end: "" };
50
- const start = createKey(SHARE_INDEX_PREFIX, "community", dim, value, "");
51
- return { start, end: start + "\uffff" };
52
- };
53
-
54
- // ── Global community index (all community shares, time-ordered) ─────
55
-
56
- const createCommunityAllIndex = (
57
- createdAt: unknown,
58
- token: unknown
59
- ): string => {
60
- const normalizedToken = normalizeToken(token);
61
- if (!normalizedToken) return "";
62
- return createKey(SHARE_INDEX_PREFIX, "community", "all", toInvertedTimestamp(createdAt), normalizedToken);
63
- };
64
-
65
- const createCommunityAllRange = () => {
66
- const start = createKey(SHARE_INDEX_PREFIX, "community", "all", "");
67
- return { start, end: start + "\uffff" };
68
- };
69
-
70
- // ── Owner index (all shares regardless of visibility) ───────────────
71
-
72
- const createOwnerIndex = (
73
- rawUserId: unknown,
74
- createdAt: unknown,
75
- token: unknown
76
- ): string => {
77
- const userId = normalizeUserId(rawUserId);
78
- const normalizedToken = normalizeToken(token);
79
- if (!userId || !normalizedToken) return "";
80
- return createKey(SHARE_INDEX_PREFIX, "owner", userId, toInvertedTimestamp(createdAt), normalizedToken);
81
- };
82
-
83
- const createOwnerRange = (rawUserId: unknown) => {
84
- const userId = normalizeUserId(rawUserId);
85
- if (!userId) return { start: "", end: "" };
86
- const start = createKey(SHARE_INDEX_PREFIX, "owner", userId, "");
87
- return { start, end: start + "\uffff" };
88
- };
89
-
90
- // ── Agent key resolution from payload ───────────────────────────────
91
-
92
- const findAgentKeyInArray = (arr: unknown[]): string => {
93
- for (const item of arr) {
94
- const key = toSafeAgentKey(item);
95
- if (key) return key;
96
- }
97
- return "";
98
- };
99
-
100
- const findAgentKeyInMessages = (messages: unknown[]): string => {
101
- for (const raw of messages) {
102
- if (!raw || typeof raw !== "object") continue;
103
- const key = toSafeAgentKey((raw as Record<string, unknown>).cybotKey);
104
- if (key) return key;
105
- }
106
- return "";
107
- };
108
-
109
- const resolveAgentKeyFromPayload = (data: any): string => {
110
- if (!data || typeof data !== "object") return "";
111
-
112
- const meta = data?.meta;
113
- const payload = data?.data;
114
-
115
- const directCandidates: unknown[] = [
116
- meta?.sourceAgentKey, meta?.agentKey,
117
- data?.sourceAgentKey, data?.agentKey, data?.cybotKey,
118
- data?.type === DataType.CYBOT ? (data?.dbKey ?? data?.id) : "",
119
- ];
120
-
121
- for (const c of directCandidates) {
122
- const key = toSafeAgentKey(c);
123
- if (key) return key;
124
- }
125
-
126
- if (payload && typeof payload === "object") {
127
- const payloadCandidates: unknown[] = [
128
- payload?.sourceAgentKey, payload?.agentKey, payload?.cybotKey,
129
- payload?.meta?.sourceAgentKey, payload?.meta?.agentKey,
130
- data?.type === DataType.CYBOT ? (payload?.dbKey ?? payload?.id) : "",
131
- ];
132
- for (const c of payloadCandidates) {
133
- const key = toSafeAgentKey(c);
134
- if (key) return key;
135
- }
136
-
137
- if (Array.isArray(payload?.cybots)) {
138
- const key = findAgentKeyInArray(payload.cybots);
139
- if (key) return key;
140
- }
141
- if (Array.isArray(payload?.messages)) {
142
- const key = findAgentKeyInMessages(payload.messages);
143
- if (key) return key;
144
- }
145
- if (Array.isArray(payload?.history)) {
146
- const key = findAgentKeyInMessages(payload.history);
147
- if (key) return key;
148
- }
149
- }
150
-
151
- return "";
152
- };
153
-
154
- // ── Public API ──────────────────────────────────────────────────────
155
-
156
- export const shareKey = {
157
- create: (token: string) => createKey(SHARE_PREFIX, token),
158
-
159
- range: () => {
160
- const start = createKey(SHARE_PREFIX, "");
161
- return { start, end: start + "\uffff" };
162
- },
163
-
164
- isShareKey: (key: string) => {
165
- const parts = splitKey(key);
166
- return parts.length >= 2 && parts[0] === SHARE_PREFIX;
167
- },
168
-
169
- tokenFromKey: (key: string) => {
170
- if (!shareKey.isShareKey(key)) return "";
171
- return key.slice(`${SHARE_PREFIX}-`.length).trim();
172
- },
173
-
174
- // Creator dimension
175
- communityCreatorIndex: (authorId: string, createdAt: number, token: string) =>
176
- createCommunityIndex("creator", authorId, createdAt, token),
177
-
178
- communityCreatorRange: (authorId: string) =>
179
- createCommunityRange("creator", authorId),
180
-
181
- // Agent dimension
182
- communityAgentIndex: (agentKey: string, createdAt: number, token: string) =>
183
- createCommunityIndex("agent", agentKey, createdAt, token),
184
-
185
- communityAgentRange: (agentKey: string) =>
186
- createCommunityRange("agent", agentKey),
187
-
188
- // Owner dimension (all shares, for "My Shares")
189
- ownerIndex: (userId: string, createdAt: number, token: string) =>
190
- createOwnerIndex(userId, createdAt, token),
191
-
192
- ownerRange: (userId: string) =>
193
- createOwnerRange(userId),
194
-
195
- // Global community feed (all community shares, time-ordered)
196
- communityAllIndex: (createdAt: number, token: string) =>
197
- createCommunityAllIndex(createdAt, token),
198
-
199
- communityAllRange: () =>
200
- createCommunityAllRange(),
201
-
202
- // From existing share data → index keys
203
- communityCreatorIndexFromShare: (dbKey: string, data: any) => {
204
- if (!shareKey.isShareKey(dbKey)) return "";
205
- if (data?.meta?.visibility !== "community") return "";
206
- const token = shareKey.tokenFromKey(dbKey);
207
- return shareKey.communityCreatorIndex(data?.meta?.authorId, data?.meta?.createdAt, token);
208
- },
209
-
210
- communityAgentIndexFromShare: (dbKey: string, data: any) => {
211
- if (!shareKey.isShareKey(dbKey)) return "";
212
- if (data?.meta?.visibility !== "community") return "";
213
- const token = shareKey.tokenFromKey(dbKey);
214
- const agentKey = resolveAgentKeyFromPayload(data);
215
- return shareKey.communityAgentIndex(agentKey, data?.meta?.createdAt, token);
216
- },
217
-
218
- ownerIndexFromShare: (dbKey: string, data: any) => {
219
- if (!shareKey.isShareKey(dbKey)) return "";
220
- const token = shareKey.tokenFromKey(dbKey);
221
- return shareKey.ownerIndex(data?.meta?.authorId, data?.meta?.createdAt, token);
222
- },
223
-
224
- communityAllIndexFromShare: (dbKey: string, data: any) => {
225
- if (!shareKey.isShareKey(dbKey)) return "";
226
- if (data?.meta?.visibility !== "community") return "";
227
- const token = shareKey.tokenFromKey(dbKey);
228
- return shareKey.communityAllIndex(data?.meta?.createdAt, token);
229
- },
230
-
231
- communityIndexKeysFromShare: (dbKey: string, data: any): string[] => {
232
- const all = shareKey.communityAllIndexFromShare(dbKey, data);
233
- const creator = shareKey.communityCreatorIndexFromShare(dbKey, data);
234
- const agent = shareKey.communityAgentIndexFromShare(dbKey, data);
235
- return Array.from(new Set([all, creator, agent].filter(Boolean)));
236
- },
237
-
238
- allIndexKeysFromShare: (dbKey: string, data: any): string[] => {
239
- const owner = shareKey.ownerIndexFromShare(dbKey, data);
240
- const community = shareKey.communityIndexKeysFromShare(dbKey, data);
241
- return Array.from(new Set([owner, ...community].filter(Boolean)));
242
- },
243
- };
package/share/types.ts DELETED
@@ -1,100 +0,0 @@
1
- import { DataType } from "../create/types";
2
-
3
- export type ShareType =
4
- | DataType.DOC
5
- | DataType.DIALOG
6
- | DataType.CYBOT
7
- | DataType.IMAGE
8
- | DataType.APP
9
- | DataType.TABLE;
10
-
11
- export interface ShareMeta {
12
- authorId: string;
13
- authorName?: string;
14
- authorAvatar?: string;
15
- createdAt: number;
16
- updatedAt?: number | string;
17
- visibility: "private" | "community";
18
- title: string;
19
- description?: string;
20
- originalId?: string;
21
- mode?: "live";
22
- coverImage?: string;
23
- sourceAgentKey?: string;
24
- sourceAgentName?: string;
25
- /** Product entitlement source of truth for paid access */
26
- productId?: string;
27
- /** 被导入次数 */
28
- importCount?: number;
29
- /** 当前返回为锁定预览,未包含原始正文 */
30
- previewLocked?: boolean;
31
- /** 当前用户需要购买后才能获取原始内容 */
32
- requiresPurchase?: boolean;
33
- originServer?: string;
34
- tableDbKey?: string;
35
- tableOwnerId?: string;
36
- replicaServers?: string[];
37
- lastReplicationAt?: number;
38
- replicationDirtyAt?: number;
39
- lastReplicationError?: string;
40
- }
41
-
42
- export interface SharedObject {
43
- type: ShareType;
44
- version: number;
45
- data: Record<string, unknown>;
46
- meta: ShareMeta;
47
- createdAt?: number | string;
48
- updatedAt?: number | string;
49
- }
50
-
51
- export interface ShareSummary {
52
- token: string;
53
- type: ShareType;
54
- title: string;
55
- description?: string;
56
- createdAt: number;
57
- authorId: string;
58
- authorName?: string;
59
- authorAvatar?: string;
60
- coverImage?: string;
61
- coverImageUrl?: string;
62
- agentKey?: string;
63
- agentName?: string;
64
- visibility?: "private" | "community";
65
- originalId?: string;
66
- mode?: "live";
67
- originServer?: string;
68
- tableDbKey?: string;
69
- tableOwnerId?: string;
70
- replicaServers?: string[];
71
- lastReplicationAt?: number;
72
- replicationDirtyAt?: number;
73
- lastReplicationError?: string;
74
- /** App URL — populated only for DataType.APP shares */
75
- url?: string;
76
- /** 被导入次数 */
77
- importCount?: number;
78
- /** 更新时间(毫秒时间戳) */
79
- updatedAt?: number;
80
- }
81
-
82
- export const isShareType = (type: unknown): type is ShareType =>
83
- type === DataType.DOC ||
84
- type === DataType.DIALOG ||
85
- type === DataType.CYBOT ||
86
- type === DataType.IMAGE ||
87
- type === DataType.APP ||
88
- type === DataType.TABLE;
89
-
90
- export const SHARE_TYPE_LABELS: Record<ShareType, string> = {
91
- [DataType.DOC]: "文章",
92
- [DataType.DIALOG]: "对话",
93
- [DataType.IMAGE]: "图片",
94
- [DataType.CYBOT]: "AI",
95
- [DataType.APP]: "应用",
96
- [DataType.TABLE]: "表格",
97
- };
98
-
99
- export const getShareTypeLabel = (type: ShareType): string =>
100
- SHARE_TYPE_LABELS[type] ?? "未知";