nolo-cli 0.1.34 → 0.1.36
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 +11 -1
- package/agent-runtime/index.ts +2 -0
- package/agent-runtime/localToolPolicy.ts +1 -1
- package/agent-runtime/localWorkspaceTools.ts +5 -2
- package/agent-runtime/platformChatProvider.ts +17 -9
- package/agent-runtime/providerResolution.ts +71 -13
- package/agentCommandSupport.ts +4 -0
- package/agentListCommands.ts +73 -11
- package/agentListHelpers.ts +56 -1
- package/agentRecordHelpers.ts +45 -5
- package/cli/agentCommandSupport.ts +4 -0
- package/cli/agentListCommands.ts +73 -11
- package/cli/agentListHelpers.ts +56 -1
- package/cli/agentRecordHelpers.ts +45 -5
- package/cli/cliSpaceHelpers.ts +39 -0
- package/cli/client/localRuntimeAdapter.ts +86 -2
- package/cli/connectorWebSocketTarget.ts +10 -3
- package/cli/dialogCommands.ts +446 -0
- package/cli/dialogInternalCommandEntries.ts +13 -0
- package/cli/globalRecordOperations.ts +258 -0
- package/cli/internalCommandEntries.ts +2 -0
- package/cli/localRuntimeAuthority.ts +182 -0
- package/cli/localRuntimeDb.ts +2 -4
- package/cli/machineWsRunDispatch.ts +42 -3
- package/cli/machineWsSession.ts +2 -0
- package/cli/runtimeDoctorCommands.ts +55 -5
- package/cliSpaceHelpers.ts +39 -0
- package/client/localRuntimeAdapter.ts +86 -2
- package/connectorWebSocketTarget.ts +10 -3
- package/database/server/cliAuthorityBrokerClient.ts +227 -0
- package/database/server/cliAuthorityBrokerServer.ts +283 -0
- package/database/server/cliAuthorityBrokerTypes.ts +43 -0
- package/database/server/cliAuthorityStoreDriver.ts +98 -0
- package/database/server/levelAuthorityStore.ts +46 -26
- package/database/tombstones.ts +52 -0
- package/database/userDataMerge.ts +95 -0
- package/dialogCommands.ts +446 -0
- package/dialogInternalCommandEntries.ts +13 -0
- package/globalRecordOperations.ts +258 -0
- package/internalCommandEntries.ts +2 -0
- package/localRuntimeAuthority.ts +182 -0
- package/localRuntimeDb.ts +2 -4
- package/machineWsRunDispatch.ts +42 -3
- package/machineWsSession.ts +2 -0
- package/package.json +18 -6
- package/runtimeDoctorCommands.ts +55 -5
- package/database/server/db.ts +0 -99
- package/database/server/ensureDbOpen.ts +0 -12
- package/database/server/memoryAuthorityStore.ts +0 -133
- package/database/server/serverStoreFactory.ts +0 -118
|
@@ -0,0 +1,446 @@
|
|
|
1
|
+
import type { AgentCommandDeps } from "./agentCommandSupport";
|
|
2
|
+
import { buildSpaceLookup, getSpaceContentKeys } from "./cliSpaceHelpers";
|
|
3
|
+
import {
|
|
4
|
+
parseUserIdFromAuthToken,
|
|
5
|
+
readOption,
|
|
6
|
+
resolveAuthToken,
|
|
7
|
+
resolveServerCandidates,
|
|
8
|
+
resolveServerUrl,
|
|
9
|
+
} from "./cliEnvHelpers";
|
|
10
|
+
import {
|
|
11
|
+
deleteDbRecordOnServers,
|
|
12
|
+
listUserRecordsFromServers,
|
|
13
|
+
readDbRecordFromServers,
|
|
14
|
+
readLiveDbRecordAfterTombstoneMerge,
|
|
15
|
+
} from "./globalRecordOperations";
|
|
16
|
+
|
|
17
|
+
type ListedDialog = {
|
|
18
|
+
id: string;
|
|
19
|
+
dbKey: string;
|
|
20
|
+
title: string;
|
|
21
|
+
status: string | null;
|
|
22
|
+
updatedAt: string | number | null;
|
|
23
|
+
createdAt: string | number | null;
|
|
24
|
+
spaceId: string | null;
|
|
25
|
+
triggerType: string | null;
|
|
26
|
+
primaryAgentKey: string | null;
|
|
27
|
+
cybots: string[];
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
const DIALOG_ID_RE = /^[0-9A-HJKMNP-TV-Z]{26}$/i;
|
|
31
|
+
const DIALOG_PATH_RE = /^\/(?:space\/([^/]+)\/)?dialog-(.+)-([0-9A-HJKMNP-TV-Z]{26})\/?$/i;
|
|
32
|
+
const VALUE_FLAGS = new Set([
|
|
33
|
+
"--limit",
|
|
34
|
+
"--server",
|
|
35
|
+
"--server-url",
|
|
36
|
+
"--space",
|
|
37
|
+
"--space-id",
|
|
38
|
+
"--token",
|
|
39
|
+
]);
|
|
40
|
+
|
|
41
|
+
function hasFlag(args: string[], flag: string) {
|
|
42
|
+
return args.includes(flag);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function readFirstPositional(args: string[]) {
|
|
46
|
+
for (let index = 0; index < args.length; index += 1) {
|
|
47
|
+
const value = args[index];
|
|
48
|
+
if (VALUE_FLAGS.has(value)) {
|
|
49
|
+
index += 1;
|
|
50
|
+
continue;
|
|
51
|
+
}
|
|
52
|
+
if (!value.startsWith("-")) return value;
|
|
53
|
+
}
|
|
54
|
+
return undefined;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function readLimit(args: string[], fallback: number) {
|
|
58
|
+
const raw = readOption(args, "--limit");
|
|
59
|
+
const parsed = raw ? Number(raw) : fallback;
|
|
60
|
+
return Number.isFinite(parsed) && parsed > 0 ? Math.floor(parsed) : fallback;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function getDialogIdFromKey(dbKey: string) {
|
|
64
|
+
const index = dbKey.lastIndexOf("-");
|
|
65
|
+
return index >= 0 ? dbKey.slice(index + 1) : dbKey;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function resolveDialogInput(rawInput: string, userId: string) {
|
|
69
|
+
const raw = rawInput.trim();
|
|
70
|
+
if (raw.startsWith("http://") || raw.startsWith("https://")) {
|
|
71
|
+
const url = new URL(raw);
|
|
72
|
+
const match = url.pathname.match(DIALOG_PATH_RE);
|
|
73
|
+
if (!match) {
|
|
74
|
+
throw new Error(`Unsupported dialog URL path: ${url.pathname}`);
|
|
75
|
+
}
|
|
76
|
+
const [, spaceId, ownerId, dialogId] = match;
|
|
77
|
+
return {
|
|
78
|
+
dbKey: `dialog-${ownerId}-${dialogId}`,
|
|
79
|
+
dialogId,
|
|
80
|
+
ownerId,
|
|
81
|
+
spaceId: spaceId ? decodeURIComponent(spaceId) : null,
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
if (raw.startsWith("dialog-")) {
|
|
86
|
+
const dialogId = getDialogIdFromKey(raw);
|
|
87
|
+
return {
|
|
88
|
+
dbKey: raw,
|
|
89
|
+
dialogId,
|
|
90
|
+
ownerId: raw.slice("dialog-".length, Math.max("dialog-".length, raw.length - dialogId.length - 1)),
|
|
91
|
+
spaceId: null,
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
if (!DIALOG_ID_RE.test(raw)) {
|
|
96
|
+
throw new Error(`Unsupported dialog id: ${raw}`);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
return {
|
|
100
|
+
dbKey: `dialog-${userId}-${raw}`,
|
|
101
|
+
dialogId: raw,
|
|
102
|
+
ownerId: userId,
|
|
103
|
+
spaceId: null,
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
function normalizeDialogRecord(record: any, fallbackDbKey?: string): ListedDialog | null {
|
|
108
|
+
const dbKey =
|
|
109
|
+
typeof record?.dbKey === "string" && record.dbKey.trim()
|
|
110
|
+
? record.dbKey.trim()
|
|
111
|
+
: fallbackDbKey ?? "";
|
|
112
|
+
const id =
|
|
113
|
+
typeof record?.id === "string" && record.id.trim()
|
|
114
|
+
? record.id.trim()
|
|
115
|
+
: dbKey
|
|
116
|
+
? getDialogIdFromKey(dbKey)
|
|
117
|
+
: "";
|
|
118
|
+
if (!dbKey || !id) return null;
|
|
119
|
+
|
|
120
|
+
return {
|
|
121
|
+
id,
|
|
122
|
+
dbKey,
|
|
123
|
+
title:
|
|
124
|
+
typeof record?.title === "string" && record.title.trim()
|
|
125
|
+
? record.title.trim()
|
|
126
|
+
: typeof record?.taskLabel === "string" && record.taskLabel.trim()
|
|
127
|
+
? record.taskLabel.trim()
|
|
128
|
+
: "(untitled)",
|
|
129
|
+
status: typeof record?.status === "string" ? record.status : null,
|
|
130
|
+
updatedAt:
|
|
131
|
+
typeof record?.updatedAt === "string" || typeof record?.updatedAt === "number"
|
|
132
|
+
? record.updatedAt
|
|
133
|
+
: typeof record?.updated_at === "string" || typeof record?.updated_at === "number"
|
|
134
|
+
? record.updated_at
|
|
135
|
+
: null,
|
|
136
|
+
createdAt:
|
|
137
|
+
typeof record?.createdAt === "string" || typeof record?.createdAt === "number"
|
|
138
|
+
? record.createdAt
|
|
139
|
+
: typeof record?.created === "string" || typeof record?.created === "number"
|
|
140
|
+
? record.created
|
|
141
|
+
: null,
|
|
142
|
+
spaceId: typeof record?.spaceId === "string" && record.spaceId.trim() ? record.spaceId : null,
|
|
143
|
+
triggerType: typeof record?.triggerType === "string" ? record.triggerType : null,
|
|
144
|
+
primaryAgentKey:
|
|
145
|
+
typeof record?.primaryAgentKey === "string" && record.primaryAgentKey.trim()
|
|
146
|
+
? record.primaryAgentKey
|
|
147
|
+
: null,
|
|
148
|
+
cybots: Array.isArray(record?.cybots)
|
|
149
|
+
? record.cybots.filter((agent: unknown): agent is string => typeof agent === "string")
|
|
150
|
+
: [],
|
|
151
|
+
};
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
function sortDialogs(dialogs: ListedDialog[]) {
|
|
155
|
+
return dialogs.sort((a, b) => {
|
|
156
|
+
const ta = a.updatedAt ?? a.createdAt ?? 0;
|
|
157
|
+
const tb = b.updatedAt ?? b.createdAt ?? 0;
|
|
158
|
+
const left = typeof ta === "number" ? ta : Date.parse(String(ta)) || 0;
|
|
159
|
+
const right = typeof tb === "number" ? tb : Date.parse(String(tb)) || 0;
|
|
160
|
+
return right - left;
|
|
161
|
+
});
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
function isScheduledDialog(record: ListedDialog) {
|
|
165
|
+
return record.triggerType === "scheduled_run";
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
function printListUsage(output: { write(chunk: string): unknown }) {
|
|
169
|
+
output.write(`Usage:
|
|
170
|
+
nolo dialog list [--space <spaceId|spaceUrl>] [--limit 100] [--json] [--ids-only]
|
|
171
|
+
|
|
172
|
+
Options:
|
|
173
|
+
--space <space> List current user's dialogs attached to one space.
|
|
174
|
+
--limit <n> Maximum dialogs to return. Default: 100.
|
|
175
|
+
--include-scheduled Include scheduled automation run dialogs.
|
|
176
|
+
--json Print machine-readable JSON.
|
|
177
|
+
--ids-only Print only dialog ids.
|
|
178
|
+
--server <url> Override NOLO_SERVER/BASE_URL.
|
|
179
|
+
--token <jwt> Override AUTH_TOKEN.
|
|
180
|
+
|
|
181
|
+
Lists merge global server candidates and ignore newer tombstones.
|
|
182
|
+
`);
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
function printDeleteUsage(output: { write(chunk: string): unknown }) {
|
|
186
|
+
output.write(`Usage:
|
|
187
|
+
nolo dialog delete <dialogId|dialogKey|dialogUrl> [--yes] [--json]
|
|
188
|
+
|
|
189
|
+
Options:
|
|
190
|
+
--yes Actually delete. Without this, the command is a dry-run.
|
|
191
|
+
--json Print machine-readable JSON.
|
|
192
|
+
--server <url> Prefer this server and include it in global deletion.
|
|
193
|
+
--token <jwt> Override AUTH_TOKEN.
|
|
194
|
+
|
|
195
|
+
Deletes are global across the selected/profile server plus known Nolo cluster peers.
|
|
196
|
+
`);
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
async function readSpaceDialogRecords(args: {
|
|
200
|
+
authToken: string;
|
|
201
|
+
fetchImpl: typeof fetch;
|
|
202
|
+
fallbackFetchImpl?: typeof fetch;
|
|
203
|
+
serverUrls: string[];
|
|
204
|
+
spaceInput: string;
|
|
205
|
+
userId: string;
|
|
206
|
+
}) {
|
|
207
|
+
const { spaceId, spaceKey } = buildSpaceLookup(args.spaceInput);
|
|
208
|
+
const spaceRead = await readLiveDbRecordAfterTombstoneMerge({
|
|
209
|
+
authToken: args.authToken,
|
|
210
|
+
dbKey: spaceKey,
|
|
211
|
+
fallbackFetchImpl: args.fallbackFetchImpl,
|
|
212
|
+
fetchImpl: args.fetchImpl,
|
|
213
|
+
label: "space record",
|
|
214
|
+
serverUrls: args.serverUrls,
|
|
215
|
+
});
|
|
216
|
+
const contentKeys = [...getSpaceContentKeys(spaceRead.record)]
|
|
217
|
+
.filter((key) => key.startsWith(`dialog-${args.userId}-`));
|
|
218
|
+
const contentKeySet = new Set(contentKeys);
|
|
219
|
+
const dialogResult = await listUserRecordsFromServers({
|
|
220
|
+
authToken: args.authToken,
|
|
221
|
+
fallbackFetchImpl: args.fallbackFetchImpl,
|
|
222
|
+
fetchImpl: args.fetchImpl,
|
|
223
|
+
label: "dialog query",
|
|
224
|
+
serverUrls: args.serverUrls,
|
|
225
|
+
type: "dialog",
|
|
226
|
+
userId: args.userId,
|
|
227
|
+
});
|
|
228
|
+
const records = dialogResult.records.filter((record) =>
|
|
229
|
+
typeof record?.dbKey === "string" && contentKeySet.has(record.dbKey)
|
|
230
|
+
);
|
|
231
|
+
const failures = [...spaceRead.failures, ...dialogResult.failures];
|
|
232
|
+
return { spaceId, records, failures };
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
export async function runDialogListCommand(
|
|
236
|
+
args: string[],
|
|
237
|
+
deps: AgentCommandDeps = {}
|
|
238
|
+
) {
|
|
239
|
+
const env = deps.env ?? process.env;
|
|
240
|
+
const output = deps.output ?? process.stdout;
|
|
241
|
+
if (hasFlag(args, "--help") || hasFlag(args, "-h")) {
|
|
242
|
+
printListUsage(output);
|
|
243
|
+
return 0;
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
const authToken = resolveAuthToken(args, env);
|
|
247
|
+
if (!authToken) {
|
|
248
|
+
output.write("[nolo] dialog list requires an auth token. Run `nolo login` or set AUTH_TOKEN.\n");
|
|
249
|
+
return 1;
|
|
250
|
+
}
|
|
251
|
+
const userId = parseUserIdFromAuthToken(authToken);
|
|
252
|
+
if (!userId) {
|
|
253
|
+
output.write("[nolo] dialog list could not read userId from AUTH_TOKEN.\n");
|
|
254
|
+
return 1;
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
const fetchImpl = deps.fetchImpl ?? fetch;
|
|
258
|
+
const fallbackFetchImpl = deps.fallbackFetchImpl;
|
|
259
|
+
const serverUrl = resolveServerUrl(args, env);
|
|
260
|
+
const serverUrls = resolveServerCandidates(args, env, serverUrl);
|
|
261
|
+
const limit = readLimit(args, 100);
|
|
262
|
+
const spaceInput = readOption(args, "--space") ?? readOption(args, "--space-id");
|
|
263
|
+
const includeScheduled = hasFlag(args, "--include-scheduled");
|
|
264
|
+
|
|
265
|
+
try {
|
|
266
|
+
let source: "user-data" | "space" = "user-data";
|
|
267
|
+
let resolvedSpaceId: string | null = null;
|
|
268
|
+
let queryFailures: Array<{ serverUrl: string; error: string }> = [];
|
|
269
|
+
let records: any[];
|
|
270
|
+
if (spaceInput) {
|
|
271
|
+
source = "space";
|
|
272
|
+
const result = await readSpaceDialogRecords({
|
|
273
|
+
authToken,
|
|
274
|
+
fallbackFetchImpl,
|
|
275
|
+
fetchImpl,
|
|
276
|
+
serverUrls,
|
|
277
|
+
spaceInput,
|
|
278
|
+
userId,
|
|
279
|
+
});
|
|
280
|
+
resolvedSpaceId = result.spaceId;
|
|
281
|
+
records = result.records;
|
|
282
|
+
queryFailures = result.failures;
|
|
283
|
+
} else {
|
|
284
|
+
const result = await listUserRecordsFromServers({
|
|
285
|
+
authToken,
|
|
286
|
+
fallbackFetchImpl,
|
|
287
|
+
fetchImpl,
|
|
288
|
+
label: "dialog query",
|
|
289
|
+
serverUrls,
|
|
290
|
+
type: "dialog",
|
|
291
|
+
userId,
|
|
292
|
+
});
|
|
293
|
+
records = result.records;
|
|
294
|
+
queryFailures = result.failures;
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
const dialogs = sortDialogs(
|
|
298
|
+
records
|
|
299
|
+
.map((record) => normalizeDialogRecord(record))
|
|
300
|
+
.filter((dialog): dialog is ListedDialog => dialog != null)
|
|
301
|
+
.filter((dialog) => includeScheduled || !isScheduledDialog(dialog))
|
|
302
|
+
).slice(0, limit);
|
|
303
|
+
|
|
304
|
+
if (hasFlag(args, "--ids-only")) {
|
|
305
|
+
output.write(`${dialogs.map((dialog) => dialog.id).join("\n")}\n`);
|
|
306
|
+
return 0;
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
if (hasFlag(args, "--json")) {
|
|
310
|
+
output.write(JSON.stringify({
|
|
311
|
+
userId,
|
|
312
|
+
...(resolvedSpaceId ? { spaceId: resolvedSpaceId } : {}),
|
|
313
|
+
source,
|
|
314
|
+
targetServers: serverUrls,
|
|
315
|
+
...(queryFailures.length ? { serverFailures: queryFailures } : {}),
|
|
316
|
+
total: dialogs.length,
|
|
317
|
+
dialogs,
|
|
318
|
+
}, null, 2));
|
|
319
|
+
output.write("\n");
|
|
320
|
+
return 0;
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
output.write(`userId: ${userId}\n`);
|
|
324
|
+
if (resolvedSpaceId) output.write(`spaceId: ${resolvedSpaceId}\n`);
|
|
325
|
+
output.write(`source: ${source}\n`);
|
|
326
|
+
output.write(`targetServers: ${serverUrls.join(", ")}\n`);
|
|
327
|
+
if (queryFailures.length) {
|
|
328
|
+
output.write(`serverFailures: ${queryFailures.length}\n`);
|
|
329
|
+
}
|
|
330
|
+
output.write(`total dialogs: ${dialogs.length}\n`);
|
|
331
|
+
for (const dialog of dialogs) {
|
|
332
|
+
output.write(
|
|
333
|
+
`\n${dialog.title}\nid=${dialog.id}\nstatus=${dialog.status ?? "-"}\nupdatedAt=${dialog.updatedAt ?? "-"}\ndbKey=${dialog.dbKey}\n`
|
|
334
|
+
);
|
|
335
|
+
}
|
|
336
|
+
return 0;
|
|
337
|
+
} catch (error) {
|
|
338
|
+
output.write(
|
|
339
|
+
`[nolo] dialog list failed: ${
|
|
340
|
+
error instanceof Error ? error.message : String(error)
|
|
341
|
+
}\n`
|
|
342
|
+
);
|
|
343
|
+
return 1;
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
export async function runDialogDeleteCommand(
|
|
348
|
+
args: string[],
|
|
349
|
+
deps: AgentCommandDeps = {}
|
|
350
|
+
) {
|
|
351
|
+
const env = deps.env ?? process.env;
|
|
352
|
+
const output = deps.output ?? process.stdout;
|
|
353
|
+
if (hasFlag(args, "--help") || hasFlag(args, "-h")) {
|
|
354
|
+
printDeleteUsage(output);
|
|
355
|
+
return 0;
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
const rawDialog = readFirstPositional(args);
|
|
359
|
+
if (!rawDialog) {
|
|
360
|
+
printDeleteUsage(output);
|
|
361
|
+
return 1;
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
const authToken = resolveAuthToken(args, env);
|
|
365
|
+
if (!authToken) {
|
|
366
|
+
output.write("[nolo] dialog delete requires an auth token. Run `nolo login` or set AUTH_TOKEN.\n");
|
|
367
|
+
return 1;
|
|
368
|
+
}
|
|
369
|
+
const userId = parseUserIdFromAuthToken(authToken);
|
|
370
|
+
if (!userId) {
|
|
371
|
+
output.write("[nolo] dialog delete could not read userId from AUTH_TOKEN.\n");
|
|
372
|
+
return 1;
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
const fetchImpl = deps.fetchImpl ?? fetch;
|
|
376
|
+
const fallbackFetchImpl = deps.fallbackFetchImpl;
|
|
377
|
+
const serverUrl = resolveServerUrl(args, env);
|
|
378
|
+
const serverUrls = resolveServerCandidates(args, env, serverUrl);
|
|
379
|
+
const resolved = resolveDialogInput(rawDialog, userId);
|
|
380
|
+
const shouldDelete = hasFlag(args, "--yes");
|
|
381
|
+
|
|
382
|
+
try {
|
|
383
|
+
const readResult = await readDbRecordFromServers({
|
|
384
|
+
authToken,
|
|
385
|
+
dbKey: resolved.dbKey,
|
|
386
|
+
fallbackFetchImpl,
|
|
387
|
+
fetchImpl,
|
|
388
|
+
label: "dialog record",
|
|
389
|
+
serverUrls,
|
|
390
|
+
});
|
|
391
|
+
const dialog = normalizeDialogRecord(readResult.record, resolved.dbKey);
|
|
392
|
+
if (!dialog) {
|
|
393
|
+
throw new Error(`dialog record is unreadable: ${resolved.dbKey}`);
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
let deleteResults: Awaited<ReturnType<typeof deleteDbRecordOnServers>> = [];
|
|
397
|
+
if (shouldDelete) {
|
|
398
|
+
deleteResults = await deleteDbRecordOnServers({
|
|
399
|
+
authToken,
|
|
400
|
+
dbKey: resolved.dbKey,
|
|
401
|
+
fallbackFetchImpl,
|
|
402
|
+
fetchImpl,
|
|
403
|
+
serverUrls,
|
|
404
|
+
});
|
|
405
|
+
}
|
|
406
|
+
const failedDeletes = deleteResults.filter((result) => !result.ok);
|
|
407
|
+
|
|
408
|
+
const payload = {
|
|
409
|
+
dbKey: resolved.dbKey,
|
|
410
|
+
dialogId: resolved.dialogId,
|
|
411
|
+
title: dialog.title,
|
|
412
|
+
readServerUrl: readResult.serverUrl,
|
|
413
|
+
targetServers: serverUrls,
|
|
414
|
+
dryRun: !shouldDelete,
|
|
415
|
+
deleted: shouldDelete,
|
|
416
|
+
...(readResult.failures.length ? { readFailures: readResult.failures } : {}),
|
|
417
|
+
...(shouldDelete ? { deleteResults } : {}),
|
|
418
|
+
};
|
|
419
|
+
|
|
420
|
+
if (hasFlag(args, "--json")) {
|
|
421
|
+
output.write(JSON.stringify(payload, null, 2));
|
|
422
|
+
output.write("\n");
|
|
423
|
+
return failedDeletes.length > 0 ? 1 : 0;
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
output.write(`dialog: ${dialog.title}\n`);
|
|
427
|
+
output.write(`dbKey: ${resolved.dbKey}\n`);
|
|
428
|
+
output.write(`readServer: ${readResult.serverUrl}\n`);
|
|
429
|
+
output.write(`targetServers: ${serverUrls.join(", ")}\n`);
|
|
430
|
+
output.write(
|
|
431
|
+
shouldDelete
|
|
432
|
+
? failedDeletes.length > 0
|
|
433
|
+
? `Deleted dialog on ${deleteResults.length - failedDeletes.length}/${deleteResults.length} servers. Failed: ${failedDeletes.map((result) => `${result.serverUrl} (${result.error})`).join(", ")}\n`
|
|
434
|
+
: "Deleted dialog on all target servers. Server-side delete cascades dialog messages.\n"
|
|
435
|
+
: "Dry-run only. Add --yes to delete this dialog and its messages.\n"
|
|
436
|
+
);
|
|
437
|
+
return failedDeletes.length > 0 ? 1 : 0;
|
|
438
|
+
} catch (error) {
|
|
439
|
+
output.write(
|
|
440
|
+
`[nolo] dialog delete failed: ${
|
|
441
|
+
error instanceof Error ? error.message : String(error)
|
|
442
|
+
}\n`
|
|
443
|
+
);
|
|
444
|
+
return 1;
|
|
445
|
+
}
|
|
446
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { CommandEntry } from "./cliCommandTypes";
|
|
2
|
+
import { createEnvCommand } from "./cliCommandFactories";
|
|
3
|
+
import {
|
|
4
|
+
runDialogDeleteCommand,
|
|
5
|
+
runDialogListCommand,
|
|
6
|
+
} from "./dialogCommands";
|
|
7
|
+
|
|
8
|
+
export function getDialogInternalCommandEntries(): CommandEntry[] {
|
|
9
|
+
return [
|
|
10
|
+
createEnvCommand(["dialog", "list"], "List dialogs", runDialogListCommand),
|
|
11
|
+
createEnvCommand(["dialog", "delete"], "Delete a dialog", runDialogDeleteCommand),
|
|
12
|
+
];
|
|
13
|
+
}
|
|
@@ -0,0 +1,258 @@
|
|
|
1
|
+
import {
|
|
2
|
+
deleteDbRecord,
|
|
3
|
+
queryUserRecords,
|
|
4
|
+
readDbRecord,
|
|
5
|
+
} from "./agentRecordHelpers";
|
|
6
|
+
import { isTombstoneRecord } from "../database/tombstones";
|
|
7
|
+
import { mergeAndDedupUserData } from "../database/userDataMerge";
|
|
8
|
+
|
|
9
|
+
export type GlobalRecordFailure = {
|
|
10
|
+
serverUrl: string;
|
|
11
|
+
error: string;
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
export type GlobalDeleteResult = {
|
|
15
|
+
serverUrl: string;
|
|
16
|
+
ok: boolean;
|
|
17
|
+
result?: any;
|
|
18
|
+
error?: string;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
export type GlobalRecordTarget = {
|
|
22
|
+
serverUrl: string;
|
|
23
|
+
authToken?: string;
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
export type DbDeleteOptions = {
|
|
27
|
+
type?: "single" | "table" | "messages";
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
export async function listUserRecordsFromServers(args: {
|
|
31
|
+
authToken: string;
|
|
32
|
+
fallbackFetchImpl?: typeof fetch;
|
|
33
|
+
fetchImpl: typeof fetch;
|
|
34
|
+
limit?: number;
|
|
35
|
+
serverUrls: string[];
|
|
36
|
+
summary?: boolean;
|
|
37
|
+
type: string | string[];
|
|
38
|
+
userId: string;
|
|
39
|
+
label?: string;
|
|
40
|
+
}) {
|
|
41
|
+
const remoteResults: Array<{ data: { data: any[] } }> = [];
|
|
42
|
+
const failures: GlobalRecordFailure[] = [];
|
|
43
|
+
|
|
44
|
+
for (const serverUrl of args.serverUrls) {
|
|
45
|
+
try {
|
|
46
|
+
const records = await queryUserRecords({
|
|
47
|
+
authToken: args.authToken,
|
|
48
|
+
fallbackFetchImpl: args.fallbackFetchImpl,
|
|
49
|
+
fetchImpl: args.fetchImpl,
|
|
50
|
+
includeDeleted: true,
|
|
51
|
+
limit: args.limit,
|
|
52
|
+
serverUrl,
|
|
53
|
+
summary: args.summary,
|
|
54
|
+
type: args.type,
|
|
55
|
+
userId: args.userId,
|
|
56
|
+
});
|
|
57
|
+
remoteResults.push({
|
|
58
|
+
data: {
|
|
59
|
+
data: records.map((record: any) => ({
|
|
60
|
+
...record,
|
|
61
|
+
serverOrigin:
|
|
62
|
+
typeof record?.serverOrigin === "string" && record.serverOrigin.trim()
|
|
63
|
+
? record.serverOrigin
|
|
64
|
+
: serverUrl,
|
|
65
|
+
})),
|
|
66
|
+
},
|
|
67
|
+
});
|
|
68
|
+
} catch (error) {
|
|
69
|
+
failures.push({
|
|
70
|
+
serverUrl,
|
|
71
|
+
error: error instanceof Error ? error.message : String(error),
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
if (remoteResults.length === 0 && failures.length > 0) {
|
|
77
|
+
const label = args.label ?? "record query";
|
|
78
|
+
throw new Error(
|
|
79
|
+
`${label} failed on all candidate servers: ${failures
|
|
80
|
+
.map((failure) => `${failure.serverUrl}: ${failure.error}`)
|
|
81
|
+
.join("; ")}`
|
|
82
|
+
);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
const merged = mergeAndDedupUserData([], remoteResults, { includeDeleted: true });
|
|
86
|
+
return {
|
|
87
|
+
records: merged.filter((record) => !isTombstoneRecord(record)),
|
|
88
|
+
failures,
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
export async function readDbRecordFromServers(args: {
|
|
93
|
+
authToken: string;
|
|
94
|
+
dbKey: string;
|
|
95
|
+
fallbackFetchImpl?: typeof fetch;
|
|
96
|
+
fetchImpl: typeof fetch;
|
|
97
|
+
includeDeleted?: boolean;
|
|
98
|
+
serverUrls: string[];
|
|
99
|
+
label?: string;
|
|
100
|
+
}) {
|
|
101
|
+
const failures: GlobalRecordFailure[] = [];
|
|
102
|
+
for (const serverUrl of args.serverUrls) {
|
|
103
|
+
try {
|
|
104
|
+
const record = await readDbRecord({
|
|
105
|
+
authToken: args.authToken,
|
|
106
|
+
dbKey: args.dbKey,
|
|
107
|
+
fallbackFetchImpl: args.fallbackFetchImpl,
|
|
108
|
+
fetchImpl: args.fetchImpl,
|
|
109
|
+
includeDeleted: args.includeDeleted,
|
|
110
|
+
serverUrl,
|
|
111
|
+
});
|
|
112
|
+
return { record, serverUrl, failures };
|
|
113
|
+
} catch (error) {
|
|
114
|
+
failures.push({
|
|
115
|
+
serverUrl,
|
|
116
|
+
error: error instanceof Error ? error.message : String(error),
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
const label = args.label ?? `record ${args.dbKey}`;
|
|
122
|
+
throw new Error(
|
|
123
|
+
`${label} is unreadable on all candidate servers: ${failures
|
|
124
|
+
.map((failure) => `${failure.serverUrl}: ${failure.error}`)
|
|
125
|
+
.join("; ")}`
|
|
126
|
+
);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
export async function readDbRecordVersionsFromServers(args: {
|
|
130
|
+
authToken: string;
|
|
131
|
+
dbKey: string;
|
|
132
|
+
fallbackFetchImpl?: typeof fetch;
|
|
133
|
+
fetchImpl: typeof fetch;
|
|
134
|
+
serverUrls: string[];
|
|
135
|
+
}) {
|
|
136
|
+
const records: any[] = [];
|
|
137
|
+
const failures: GlobalRecordFailure[] = [];
|
|
138
|
+
for (const serverUrl of args.serverUrls) {
|
|
139
|
+
try {
|
|
140
|
+
const record = await readDbRecord({
|
|
141
|
+
authToken: args.authToken,
|
|
142
|
+
dbKey: args.dbKey,
|
|
143
|
+
fallbackFetchImpl: args.fallbackFetchImpl,
|
|
144
|
+
fetchImpl: args.fetchImpl,
|
|
145
|
+
includeDeleted: true,
|
|
146
|
+
serverUrl,
|
|
147
|
+
});
|
|
148
|
+
records.push({
|
|
149
|
+
...record,
|
|
150
|
+
dbKey: typeof record?.dbKey === "string" && record.dbKey.trim()
|
|
151
|
+
? record.dbKey
|
|
152
|
+
: args.dbKey,
|
|
153
|
+
serverOrigin:
|
|
154
|
+
typeof record?.serverOrigin === "string" && record.serverOrigin.trim()
|
|
155
|
+
? record.serverOrigin
|
|
156
|
+
: serverUrl,
|
|
157
|
+
});
|
|
158
|
+
} catch (error) {
|
|
159
|
+
failures.push({
|
|
160
|
+
serverUrl,
|
|
161
|
+
error: error instanceof Error ? error.message : String(error),
|
|
162
|
+
});
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
return { records, failures };
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
export async function readLiveDbRecordAfterTombstoneMerge(args: {
|
|
169
|
+
authToken: string;
|
|
170
|
+
dbKey: string;
|
|
171
|
+
fallbackFetchImpl?: typeof fetch;
|
|
172
|
+
fetchImpl: typeof fetch;
|
|
173
|
+
serverUrls: string[];
|
|
174
|
+
label?: string;
|
|
175
|
+
}) {
|
|
176
|
+
const result = await readDbRecordVersionsFromServers(args);
|
|
177
|
+
const merged = mergeAndDedupUserData(
|
|
178
|
+
[],
|
|
179
|
+
[{ data: { data: result.records } }],
|
|
180
|
+
{ includeDeleted: true }
|
|
181
|
+
);
|
|
182
|
+
const record = merged.find((item) => item?.dbKey === args.dbKey);
|
|
183
|
+
if (record && !isTombstoneRecord(record)) {
|
|
184
|
+
return {
|
|
185
|
+
record,
|
|
186
|
+
serverUrl: typeof record.serverOrigin === "string" ? record.serverOrigin : "",
|
|
187
|
+
failures: result.failures,
|
|
188
|
+
};
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
const label = args.label ?? `record ${args.dbKey}`;
|
|
192
|
+
const failureText = result.failures.length
|
|
193
|
+
? ` Failures: ${result.failures
|
|
194
|
+
.map((failure) => `${failure.serverUrl}: ${failure.error}`)
|
|
195
|
+
.join("; ")}`
|
|
196
|
+
: "";
|
|
197
|
+
throw new Error(`${label} is deleted or unreadable after global tombstone merge.${failureText}`);
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
export async function recordExistsAfterTombstoneMerge(args: {
|
|
201
|
+
authToken: string;
|
|
202
|
+
dbKey: string;
|
|
203
|
+
fallbackFetchImpl?: typeof fetch;
|
|
204
|
+
fetchImpl: typeof fetch;
|
|
205
|
+
serverUrls: string[];
|
|
206
|
+
}) {
|
|
207
|
+
try {
|
|
208
|
+
await readLiveDbRecordAfterTombstoneMerge(args);
|
|
209
|
+
return true;
|
|
210
|
+
} catch {
|
|
211
|
+
return false;
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
export async function deleteDbRecordOnServers(args: {
|
|
216
|
+
authToken: string;
|
|
217
|
+
dbKey: string;
|
|
218
|
+
deleteOptions?: DbDeleteOptions;
|
|
219
|
+
fallbackFetchImpl?: typeof fetch;
|
|
220
|
+
fetchImpl: typeof fetch;
|
|
221
|
+
serverUrls: string[];
|
|
222
|
+
}): Promise<GlobalDeleteResult[]> {
|
|
223
|
+
return deleteDbRecordOnTargets({
|
|
224
|
+
...args,
|
|
225
|
+
targets: args.serverUrls.map((serverUrl) => ({ serverUrl })),
|
|
226
|
+
});
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
export async function deleteDbRecordOnTargets(args: {
|
|
230
|
+
authToken: string;
|
|
231
|
+
dbKey: string;
|
|
232
|
+
deleteOptions?: DbDeleteOptions;
|
|
233
|
+
fallbackFetchImpl?: typeof fetch;
|
|
234
|
+
fetchImpl: typeof fetch;
|
|
235
|
+
targets: GlobalRecordTarget[];
|
|
236
|
+
}): Promise<GlobalDeleteResult[]> {
|
|
237
|
+
const results: GlobalDeleteResult[] = [];
|
|
238
|
+
for (const target of args.targets) {
|
|
239
|
+
try {
|
|
240
|
+
const result = await deleteDbRecord({
|
|
241
|
+
authToken: target.authToken ?? args.authToken,
|
|
242
|
+
dbKey: args.dbKey,
|
|
243
|
+
deleteOptions: args.deleteOptions,
|
|
244
|
+
fallbackFetchImpl: args.fallbackFetchImpl,
|
|
245
|
+
fetchImpl: args.fetchImpl,
|
|
246
|
+
serverUrl: target.serverUrl,
|
|
247
|
+
});
|
|
248
|
+
results.push({ serverUrl: target.serverUrl, ok: true, result });
|
|
249
|
+
} catch (error) {
|
|
250
|
+
results.push({
|
|
251
|
+
serverUrl: target.serverUrl,
|
|
252
|
+
ok: false,
|
|
253
|
+
error: error instanceof Error ? error.message : String(error),
|
|
254
|
+
});
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
return results;
|
|
258
|
+
}
|