@wirecat/cli-core 0.667.0 → 0.669.0
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/CHANGELOG.md +92 -0
- package/dist/calendar/mcp/index.d.ts +2909 -186
- package/dist/calendar/mcp/index.js +20 -7
- package/dist/calendar/testing/writer-contract.js +33 -0
- package/dist/cli/documents/catalog.js +3 -14
- package/dist/cli/documents/sync.d.ts +21 -5
- package/dist/cli/documents/sync.js +39 -8
- package/dist/cli/documents/testing/source-contract.d.ts +9 -0
- package/dist/cli/documents/testing/source-contract.js +12 -0
- package/dist/cli/meeting-batch.js +2 -2
- package/dist/documents/model.d.ts +4 -0
- package/dist/entities/index.d.ts +1 -0
- package/dist/errors/reasons.js +1 -0
- package/dist/meetings/answers.d.ts +2 -0
- package/dist/meetings/answers.js +3 -0
- package/dist/meetings/cli/index.d.ts +2 -0
- package/dist/meetings/cli/index.js +4 -1
- package/dist/meetings/cli/management.d.ts +2 -0
- package/dist/meetings/cli/management.js +6 -1
- package/dist/meetings/cli/passage.js +2 -2
- package/dist/meetings/index.d.ts +2 -2
- package/dist/meetings/index.js +2 -2
- package/dist/meetings/mcp/passage.js +2 -2
- package/dist/meetings/passage.d.ts +5 -0
- package/dist/meetings/passage.js +10 -0
- package/dist/meetings/read-answers.d.ts +2 -0
- package/dist/meetings/read-answers.js +2 -0
- package/dist/meetings/read-through.d.ts +1 -1
- package/dist/meetings/read-through.js +4 -0
- package/dist/meetings/reads.d.ts +3 -17
- package/dist/meetings/reads.js +10 -1
- package/dist/meetings/store.d.ts +6 -0
- package/dist/meetings/store.js +13 -0
- package/dist/services/documents/ingestion.d.ts +3 -0
- package/dist/services/documents/ingestion.js +9 -2
- package/dist/services/documents/plan.d.ts +16 -1
- package/dist/services/documents/plan.js +163 -103
- package/dist/services/documents/ports.d.ts +13 -0
- package/dist/services/documents/sync.d.ts +11 -0
- package/dist/services/documents/sync.js +41 -3
- package/dist/services/metadata.d.ts +1 -1
- package/dist/store/sqlite/catalog.d.ts +2 -1
- package/dist/store/sqlite/catalog.js +4 -1
- package/dist/store/sqlite/meeting-listing.js +28 -7
- package/dist/store/sqlite/note-search.d.ts +10 -1
- package/dist/store/sqlite/note-search.js +35 -1
- package/package.json +3 -3
package/dist/meetings/reads.d.ts
CHANGED
|
@@ -3,7 +3,7 @@ import { type QueryAst } from "@wirecat/cli-base/query";
|
|
|
3
3
|
import { type ListEnvelope } from "@wirecat/cli-base/standards";
|
|
4
4
|
import { type MeetingDetailsItem, type MeetingItemBody, type MeetingListBody, type MeetingSummaryItem, type MeetingTranscriptItem } from "./answers.js";
|
|
5
5
|
import { type MeetingItem } from "./item.js";
|
|
6
|
-
import { type MeetingParticipantSearchOptions, type MeetingParticipantSearchStore } from "./participant-search.js";
|
|
6
|
+
import { type MeetingParticipantCandidate, type MeetingParticipantSearchOptions, type MeetingParticipantSearchStore } from "./participant-search.js";
|
|
7
7
|
import type { MeetingDetails, MeetingFilter, MeetingStore } from "./store.js";
|
|
8
8
|
export declare class MeetingError extends CliError {
|
|
9
9
|
constructor(code: "validation_error" | "not_found" | "configuration_error", message: string);
|
|
@@ -23,23 +23,9 @@ export declare const listed: <T>(items: T[], page?: {
|
|
|
23
23
|
export declare const meetingPeople: (store: MeetingStore & Partial<MeetingParticipantSearchStore>, query: string, accountId?: number, options?: Omit<MeetingParticipantSearchOptions, "accountId">) => Promise<{
|
|
24
24
|
limit: number;
|
|
25
25
|
hasMore: boolean;
|
|
26
|
-
items: {
|
|
27
|
-
createdAt: number;
|
|
28
|
-
updatedAt: number;
|
|
29
|
-
id: number;
|
|
30
|
-
meetingId: number;
|
|
31
|
-
identityId: number;
|
|
32
|
-
displayName: string | null;
|
|
33
|
-
email: string | null;
|
|
34
|
-
role: string | null;
|
|
35
|
-
joinedAt: number | null;
|
|
36
|
-
leftAt: number | null;
|
|
37
|
-
durationMs: number | null;
|
|
38
|
-
sessions: import("./model.js").JsonValue;
|
|
39
|
-
externalId: string | null;
|
|
40
|
-
metadata: import("./model.js").Metadata;
|
|
26
|
+
items: (Omit<MeetingParticipantCandidate, "person"> & {
|
|
41
27
|
untrusted: string[];
|
|
42
|
-
}[];
|
|
28
|
+
})[];
|
|
43
29
|
page: number;
|
|
44
30
|
} | {
|
|
45
31
|
schema_version: 1;
|
package/dist/meetings/reads.js
CHANGED
|
@@ -75,7 +75,16 @@ export const meetingPeople = async (store, query, accountId, options = {}) => {
|
|
|
75
75
|
}
|
|
76
76
|
if (Object.values(options).some((value) => value !== undefined))
|
|
77
77
|
throw new MeetingError("configuration_error", "bounded participant lookup needs a selected account and capable store");
|
|
78
|
-
|
|
78
|
+
// The bounded path's row, so `untrusted` names keys that are there. This store cannot tell a person link, so no
|
|
79
|
+
// `person`: its null would say the identity is linked to no one.
|
|
80
|
+
return listed((await store.participants(query, accountId)).map((one) => ({
|
|
81
|
+
participant_id: one.id,
|
|
82
|
+
meeting_id: one.meetingId,
|
|
83
|
+
identity_id: one.identityId,
|
|
84
|
+
display_name: one.displayName,
|
|
85
|
+
email: one.email,
|
|
86
|
+
untrusted: [...PARTICIPANT_UNTRUSTED],
|
|
87
|
+
})));
|
|
79
88
|
};
|
|
80
89
|
const PAGE_NEXT = STEPS.find((step) => step.id === "page.next");
|
|
81
90
|
/** Newest first; `filter.query` is a list query `meetingsQuery` validated. The answer pages by `limit`. */
|
package/dist/meetings/store.d.ts
CHANGED
|
@@ -29,6 +29,12 @@ export interface TranscriptInput extends Omit<NewRecord<Transcript>, "meetingId"
|
|
|
29
29
|
driveFileId?: string;
|
|
30
30
|
}
|
|
31
31
|
export declare const MEETING_DOCUMENT_ROLES: readonly ["transcript", "recording", "notes"];
|
|
32
|
+
/**
|
|
33
|
+
* What Meet and Gemini put in the name of the Google Doc they save, the words drive finds them by. The English names
|
|
34
|
+
* are the ones in a real Drive; the Russian and Spanish ones are Google's localized forms as best known, not yet seen
|
|
35
|
+
* in one. Drive's files have no field that marks a document as Meet's, so the name is all there is.
|
|
36
|
+
*/
|
|
37
|
+
export declare const TRANSCRIPT_WORDS: readonly ["Transcript", "Notes by Gemini", "Транскрипт", "Заметки Gemini", "Transcripción", "Notas de Gemini"];
|
|
32
38
|
export type MeetingDocumentRole = (typeof MEETING_DOCUMENT_ROLES)[number];
|
|
33
39
|
/**
|
|
34
40
|
* A Drive file the meeting service saved for the meeting: its transcript, its recording or its notes (Meet's
|
package/dist/meetings/store.js
CHANGED
|
@@ -1,3 +1,16 @@
|
|
|
1
1
|
export const MEETING_DOCUMENT_ROLES = ["transcript", "recording", "notes"];
|
|
2
|
+
/**
|
|
3
|
+
* What Meet and Gemini put in the name of the Google Doc they save, the words drive finds them by. The English names
|
|
4
|
+
* are the ones in a real Drive; the Russian and Spanish ones are Google's localized forms as best known, not yet seen
|
|
5
|
+
* in one. Drive's files have no field that marks a document as Meet's, so the name is all there is.
|
|
6
|
+
*/
|
|
7
|
+
export const TRANSCRIPT_WORDS = [
|
|
8
|
+
"Transcript",
|
|
9
|
+
"Notes by Gemini",
|
|
10
|
+
"Транскрипт",
|
|
11
|
+
"Заметки Gemini",
|
|
12
|
+
"Transcripción",
|
|
13
|
+
"Notas de Gemini",
|
|
14
|
+
];
|
|
2
15
|
/** `searchHits`' refusal of a page whose stored text passes `maxReadBytes`; a reader may ask for fewer rows. */
|
|
3
16
|
export const READ_BUDGET_EXCEEDED = "Meeting search exceeds its stored text byte budget";
|
|
@@ -28,6 +28,9 @@ export declare const documentIngestion: <M extends SourceFile>(store: Pick<Messa
|
|
|
28
28
|
}>;
|
|
29
29
|
};
|
|
30
30
|
collection: (principal: string, create: boolean) => Promise<import("../../store/index.js").NoteFolder | undefined>;
|
|
31
|
+
settle: ({ until }: {
|
|
32
|
+
until: () => boolean;
|
|
33
|
+
}) => Promise<number>;
|
|
31
34
|
save: (principal: string, content: SourceContent<M>, hash: string) => Promise<string>;
|
|
32
35
|
remove: (principal: string, id: string) => Promise<void>;
|
|
33
36
|
};
|
|
@@ -10,6 +10,8 @@ export const documentIngestion = (store, options) => {
|
|
|
10
10
|
const found = (await store.notes.folders()).find((item) => item.name === name);
|
|
11
11
|
return found ?? (create ? await store.notes.addFolder({ name, format: "markdown" }) : undefined);
|
|
12
12
|
};
|
|
13
|
+
// The catalog answers external ids (`<id>.md`), so an id given back as it was answered is found too.
|
|
14
|
+
const both = (id) => [...new Set([options.externalId(id), id])];
|
|
13
15
|
const stale = async (ref) => {
|
|
14
16
|
for (const status of ["proposed", "confirmed"]) {
|
|
15
17
|
for (let batch = 0;; batch++) {
|
|
@@ -44,7 +46,10 @@ export const documentIngestion = (store, options) => {
|
|
|
44
46
|
},
|
|
45
47
|
get: async (principal, id) => {
|
|
46
48
|
const selected = await folder(principal, false);
|
|
47
|
-
|
|
49
|
+
if (!selected)
|
|
50
|
+
return undefined;
|
|
51
|
+
const entry = await store.catalog.get(selected.id, options.externalId(id));
|
|
52
|
+
return entry ?? (options.externalId(id) === id ? undefined : store.catalog.get(selected.id, id));
|
|
48
53
|
},
|
|
49
54
|
list: async (principal, query = {}) => {
|
|
50
55
|
const selected = await folder(principal, false);
|
|
@@ -57,12 +62,14 @@ export const documentIngestion = (store, options) => {
|
|
|
57
62
|
? store.catalog.list(selected.id, {
|
|
58
63
|
...query,
|
|
59
64
|
...(ast ? { ast } : {}),
|
|
60
|
-
...(query.parent === undefined ? {} : { parent:
|
|
65
|
+
...(query.parent === undefined ? {} : { parent: [query.parent].flat().flatMap(both) }),
|
|
61
66
|
})
|
|
62
67
|
: { items: [], hasMore: false };
|
|
63
68
|
},
|
|
64
69
|
},
|
|
65
70
|
collection: folder,
|
|
71
|
+
// A store fake that predates `indexWritten` leaves the text for the next search to index.
|
|
72
|
+
settle: async ({ until }) => store.notes.indexWritten?.({ until }),
|
|
66
73
|
save: async (principal, content, hash) => {
|
|
67
74
|
const selected = await folder(principal, true);
|
|
68
75
|
if (!selected)
|
|
@@ -7,5 +7,20 @@ interface WalkStore<M extends SourceFile> {
|
|
|
7
7
|
read: (scope: string) => SourceWalk<M> | undefined;
|
|
8
8
|
save: (walk: SourceWalk<M>) => void;
|
|
9
9
|
}
|
|
10
|
-
export
|
|
10
|
+
export type WalkStop = "entries" | "requests" | "deadline" | "cancelled";
|
|
11
|
+
/** A folder walk this call stopped before its end, saved for the next sync to go on from. */
|
|
12
|
+
export interface WalkProgress {
|
|
13
|
+
folders_left: number;
|
|
14
|
+
/** Entries the walk has found over every call so far. */
|
|
15
|
+
entries: number;
|
|
16
|
+
/** Folders this call listed: 0 means it moved nothing on. */
|
|
17
|
+
listed: number;
|
|
18
|
+
stopped: WalkStop;
|
|
19
|
+
}
|
|
20
|
+
export type Plan<M extends SourceFile> = {
|
|
21
|
+
pending: NonNullable<SourceState<M>["pending"]>;
|
|
22
|
+
} | {
|
|
23
|
+
walk: WalkProgress;
|
|
24
|
+
};
|
|
25
|
+
export declare const planDocuments: <M extends SourceFile>(options: SyncOptions<M>, source: DocumentSource<M> | undefined, state: SourceState<M>, roots: string[], limit: number, pictures: boolean, walks?: WalkStore<M>) => Promise<Plan<M>>;
|
|
11
26
|
export {};
|
|
@@ -12,134 +12,78 @@ export const changedSince = (previous, file) => previous.version !== file.versio
|
|
|
12
12
|
previous.canDownload !== file.canDownload ||
|
|
13
13
|
JSON.stringify(previous.parents) !== JSON.stringify(file.parents);
|
|
14
14
|
const SAVE_EVERY_MS = 2000;
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
15
|
+
// A round of changes is saved under its own scope: a CLI that validates the walk file keeps only the fields it knows,
|
|
16
|
+
// and these say both what the walk is and which cursor it started from.
|
|
17
|
+
const changesScope = (scope, cursor) => `${scope}:changes:${cursor}`;
|
|
18
|
+
export const planDocuments = async (options, source, state, roots, limit, pictures, walks) => {
|
|
19
|
+
const { isFolder, skipReason, retryable, maxEntries, errorOf, query, budget } = options;
|
|
18
20
|
const scope = scopeHash(roots, query, options.isDocument !== undefined);
|
|
19
21
|
if (!roots.length && query?.mode !== "include")
|
|
20
22
|
return {
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
23
|
+
pending: {
|
|
24
|
+
scope,
|
|
25
|
+
cursor: state.cursor ?? "",
|
|
26
|
+
folders: {},
|
|
27
|
+
skipped: [],
|
|
28
|
+
jobs: Object.keys(state.documents).map((id) => ({ id, kind: "remove" })),
|
|
29
|
+
},
|
|
26
30
|
};
|
|
27
31
|
if (!source)
|
|
28
32
|
throw new CliError("configuration_error", "selected folders or a saved query require an authenticated source");
|
|
29
33
|
if (query?.mode === "include" && !query.list)
|
|
30
34
|
throw new CliError("configuration_error", "a query that includes files must list them");
|
|
31
|
-
// Listed before the walk: a failure here then costs no folder already walked.
|
|
32
|
-
const found = query?.mode === "include" && query.list ? await query.list() : [];
|
|
33
35
|
const initial = !state.cursor || scope !== state.scope;
|
|
34
|
-
const resumed = initial ?
|
|
35
|
-
const cursor = initial ? (resumed?.cursor ?? (await source.startToken())) : state.cursor;
|
|
36
|
+
const resumed = walks?.read(initial ? scope : changesScope(scope, state.cursor));
|
|
36
37
|
const folders = initial ? {} : structuredClone(state.folders);
|
|
37
38
|
const files = new Map(Object.values(state.documents).map((doc) => [doc.metadata.id, doc.metadata]));
|
|
38
39
|
const changed = new Set();
|
|
40
|
+
// What the walk saves: every file of a full walk, or what a round of changes touched, a removed file as trashed.
|
|
41
|
+
const delta = new Map();
|
|
39
42
|
for (const skipped of state.skipped)
|
|
40
43
|
if (skipped.file)
|
|
41
44
|
files.set(skipped.id, skipped.file);
|
|
45
|
+
if (initial)
|
|
46
|
+
files.clear();
|
|
42
47
|
const keep = (file) => {
|
|
43
48
|
files.set(file.id, file);
|
|
44
49
|
changed.add(file.id);
|
|
50
|
+
delta.set(file.id, file);
|
|
51
|
+
};
|
|
52
|
+
const drop = (id) => {
|
|
53
|
+
const known = files.get(id);
|
|
54
|
+
if (known)
|
|
55
|
+
delta.set(id, { ...known, trashed: true });
|
|
56
|
+
files.delete(id);
|
|
45
57
|
};
|
|
46
58
|
const container = (file) => {
|
|
47
59
|
if (options.isDocument?.(file))
|
|
48
60
|
keep(file);
|
|
49
61
|
};
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
const
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
folders[folder.id] = folder;
|
|
59
|
-
for (const file of await source.children(folder.id)) {
|
|
60
|
-
if (++discovered > MAX_ENTRIES)
|
|
61
|
-
throw tooManyEntries();
|
|
62
|
-
if (isFolder(file)) {
|
|
63
|
-
container(file);
|
|
64
|
-
await crawl(file, new Set([...ancestry, folder.id]));
|
|
65
|
-
}
|
|
66
|
-
else
|
|
67
|
-
keep(file);
|
|
68
|
-
}
|
|
69
|
-
};
|
|
70
|
-
let finalCursor = cursor;
|
|
71
|
-
if (initial) {
|
|
72
|
-
files.clear();
|
|
73
|
-
const walk = resumed ?? {
|
|
62
|
+
// A folder the last finished pass walked keeps its subtree: a round lists only its direct children, and the
|
|
63
|
+
// source's changes name what changed below them. A new, restored or moved-in folder is walked in full.
|
|
64
|
+
const walked = (id) => !initial && state.folders[id] !== undefined && !state.folders[id]?.trashed;
|
|
65
|
+
let walk = resumed;
|
|
66
|
+
let cursor = state.cursor ?? "";
|
|
67
|
+
if (!walk && initial) {
|
|
68
|
+
const start = await source.startToken();
|
|
69
|
+
walk = {
|
|
74
70
|
scope,
|
|
75
|
-
cursor,
|
|
71
|
+
cursor: start,
|
|
76
72
|
stack: [...roots].reverse().map((id) => ({ id, ancestry: [] })),
|
|
77
73
|
visited: [],
|
|
78
74
|
folders: {},
|
|
79
75
|
files: [],
|
|
80
76
|
discovered: 0,
|
|
81
77
|
};
|
|
82
|
-
for (const id of walk.visited)
|
|
83
|
-
visited.add(id);
|
|
84
|
-
Object.assign(folders, walk.folders);
|
|
85
|
-
for (const file of walk.files) {
|
|
86
|
-
files.set(file.id, file);
|
|
87
|
-
changed.add(file.id);
|
|
88
|
-
}
|
|
89
|
-
discovered = walk.discovered;
|
|
90
|
-
let savedAt = (options.now ?? Date.now)();
|
|
91
|
-
const record = () => {
|
|
92
|
-
walks?.save({ ...walk, visited: [...visited], folders, files: [...files.values()], discovered });
|
|
93
|
-
savedAt = (options.now ?? Date.now)();
|
|
94
|
-
};
|
|
95
|
-
try {
|
|
96
|
-
for (let step = walk.stack.at(-1); step; step = walk.stack.at(-1)) {
|
|
97
|
-
if (step.ancestry.includes(step.id))
|
|
98
|
-
throw new CliError("invalid_response", "folder traversal contains a cycle");
|
|
99
|
-
if (visited.has(step.id)) {
|
|
100
|
-
walk.stack.pop();
|
|
101
|
-
continue;
|
|
102
|
-
}
|
|
103
|
-
const folder = step.folder ?? (await source.metadata(step.id));
|
|
104
|
-
if (!step.folder && (!isFolder(folder) || folder.trashed))
|
|
105
|
-
throw new CliError("validation_error", "configured root is not an accessible folder");
|
|
106
|
-
if (!step.folder)
|
|
107
|
-
container(folder);
|
|
108
|
-
const children = await source.children(folder.id);
|
|
109
|
-
if (discovered + children.length > MAX_ENTRIES)
|
|
110
|
-
throw tooManyEntries();
|
|
111
|
-
discovered += children.length;
|
|
112
|
-
walk.stack.pop();
|
|
113
|
-
visited.add(folder.id);
|
|
114
|
-
folders[folder.id] = folder;
|
|
115
|
-
const nested = [];
|
|
116
|
-
for (const file of children)
|
|
117
|
-
if (isFolder(file)) {
|
|
118
|
-
container(file);
|
|
119
|
-
nested.push({ id: file.id, ancestry: [...step.ancestry, folder.id], folder: file });
|
|
120
|
-
}
|
|
121
|
-
else
|
|
122
|
-
keep(file);
|
|
123
|
-
for (const entry of nested.reverse())
|
|
124
|
-
walk.stack.push(entry);
|
|
125
|
-
if ((options.now ?? Date.now)() - savedAt > SAVE_EVERY_MS)
|
|
126
|
-
record();
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
catch (error) {
|
|
130
|
-
record();
|
|
131
|
-
throw error;
|
|
132
|
-
}
|
|
133
78
|
}
|
|
134
|
-
else {
|
|
79
|
+
else if (!walk) {
|
|
135
80
|
const page = await source.changes(cursor);
|
|
136
|
-
finalCursor = page.cursor;
|
|
137
81
|
const latest = new Map(page.items.map((change) => [change.id, change]));
|
|
138
82
|
const entering = [];
|
|
139
83
|
// Apply all ancestry changes before classifying document scope.
|
|
140
84
|
for (const change of latest.values()) {
|
|
141
85
|
if (change.removed || change.file?.trashed) {
|
|
142
|
-
|
|
86
|
+
drop(change.id);
|
|
143
87
|
if (folders[change.id])
|
|
144
88
|
folders[change.id] = { ...folders[change.id], trashed: true };
|
|
145
89
|
}
|
|
@@ -171,10 +115,126 @@ export const planDocuments = async (options, source, state, roots, limit, pictur
|
|
|
171
115
|
folders[root] = { ...previous, trashed: true };
|
|
172
116
|
}
|
|
173
117
|
}
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
118
|
+
const stack = entering
|
|
119
|
+
.filter((folder) => within(folder, roots, folders))
|
|
120
|
+
.reverse()
|
|
121
|
+
.map((folder) => ({ id: folder.id, ancestry: [], folder }));
|
|
122
|
+
if (stack.length)
|
|
123
|
+
walk = {
|
|
124
|
+
scope: changesScope(scope, cursor),
|
|
125
|
+
cursor: page.cursor,
|
|
126
|
+
stack,
|
|
127
|
+
visited: [],
|
|
128
|
+
folders: {},
|
|
129
|
+
files: [],
|
|
130
|
+
discovered: 0,
|
|
131
|
+
};
|
|
132
|
+
else
|
|
133
|
+
cursor = page.cursor;
|
|
177
134
|
}
|
|
135
|
+
const vanished = async (id) => {
|
|
136
|
+
try {
|
|
137
|
+
return (await source.metadata(id)).trashed;
|
|
138
|
+
}
|
|
139
|
+
catch (error) {
|
|
140
|
+
return errorOf(error).code === "not_found";
|
|
141
|
+
}
|
|
142
|
+
};
|
|
143
|
+
let found = [];
|
|
144
|
+
if (walk) {
|
|
145
|
+
const visited = new Set(walk.visited);
|
|
146
|
+
Object.assign(folders, walk.folders);
|
|
147
|
+
for (const file of walk.files) {
|
|
148
|
+
if (file.trashed)
|
|
149
|
+
drop(file.id);
|
|
150
|
+
else
|
|
151
|
+
keep(file);
|
|
152
|
+
}
|
|
153
|
+
cursor = walk.cursor;
|
|
154
|
+
const now = options.now ?? Date.now;
|
|
155
|
+
let savedAt = now();
|
|
156
|
+
const current = walk;
|
|
157
|
+
const record = () => {
|
|
158
|
+
walks?.save({ ...current, visited: [...visited], folders, files: [...delta.values()] });
|
|
159
|
+
savedAt = now();
|
|
160
|
+
};
|
|
161
|
+
let spent = 0;
|
|
162
|
+
let listed = 0;
|
|
163
|
+
let stopped = null;
|
|
164
|
+
try {
|
|
165
|
+
for (let step = walk.stack.at(-1); step; step = walk.stack.at(-1)) {
|
|
166
|
+
if (step.ancestry.includes(step.id))
|
|
167
|
+
throw new CliError("invalid_response", "folder traversal contains a cycle");
|
|
168
|
+
if (visited.has(step.id)) {
|
|
169
|
+
walk.stack.pop();
|
|
170
|
+
continue;
|
|
171
|
+
}
|
|
172
|
+
// One folder at least per call, so a folder with more children than the bound still gets listed.
|
|
173
|
+
stopped = listed > 0 && spent >= maxEntries ? "entries" : (budget?.take() ?? null);
|
|
174
|
+
if (stopped)
|
|
175
|
+
break;
|
|
176
|
+
const folder = step.folder ?? (await source.metadata(step.id));
|
|
177
|
+
if (!step.folder && (!isFolder(folder) || folder.trashed))
|
|
178
|
+
throw new CliError("validation_error", "configured root is not an accessible folder");
|
|
179
|
+
if (!step.folder)
|
|
180
|
+
container(folder);
|
|
181
|
+
let children;
|
|
182
|
+
try {
|
|
183
|
+
children = await source.children(folder.id);
|
|
184
|
+
}
|
|
185
|
+
catch (error) {
|
|
186
|
+
// Deleted or unshared since the walk saved it: gone, with what is below it, instead of failing every sync.
|
|
187
|
+
// A listing's 404 alone can be passing, so the folder is read again and is gone only when that read is a
|
|
188
|
+
// 404 too or finds it trashed; on any doubt the call fails and the next sync lists it again.
|
|
189
|
+
if (errorOf(error).code !== "not_found" || !(await vanished(folder.id)))
|
|
190
|
+
throw error;
|
|
191
|
+
walk.stack.pop();
|
|
192
|
+
visited.add(folder.id);
|
|
193
|
+
folders[folder.id] = { ...folder, trashed: true };
|
|
194
|
+
drop(folder.id);
|
|
195
|
+
spent++;
|
|
196
|
+
listed++;
|
|
197
|
+
continue;
|
|
198
|
+
}
|
|
199
|
+
spent += 1 + children.length;
|
|
200
|
+
listed++;
|
|
201
|
+
walk.discovered += children.length;
|
|
202
|
+
walk.stack.pop();
|
|
203
|
+
visited.add(folder.id);
|
|
204
|
+
folders[folder.id] = folder;
|
|
205
|
+
const nested = [];
|
|
206
|
+
for (const file of children)
|
|
207
|
+
if (isFolder(file)) {
|
|
208
|
+
container(file);
|
|
209
|
+
if (walked(file.id))
|
|
210
|
+
folders[file.id] = file;
|
|
211
|
+
else
|
|
212
|
+
nested.push({ id: file.id, ancestry: [...step.ancestry, folder.id], folder: file });
|
|
213
|
+
}
|
|
214
|
+
else
|
|
215
|
+
keep(file);
|
|
216
|
+
for (const entry of nested.reverse())
|
|
217
|
+
walk.stack.push(entry);
|
|
218
|
+
if (now() - savedAt > SAVE_EVERY_MS)
|
|
219
|
+
record();
|
|
220
|
+
}
|
|
221
|
+
if (stopped) {
|
|
222
|
+
record();
|
|
223
|
+
return {
|
|
224
|
+
walk: { folders_left: walk.stack.length, entries: walk.discovered, listed, stopped },
|
|
225
|
+
};
|
|
226
|
+
}
|
|
227
|
+
// Listed once the walk is done: a call the walk does not finish would read it for nothing.
|
|
228
|
+
if (query?.mode === "include" && query.list)
|
|
229
|
+
found = await query.list();
|
|
230
|
+
}
|
|
231
|
+
catch (error) {
|
|
232
|
+
record();
|
|
233
|
+
throw error;
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
else if (query?.mode === "include" && query.list)
|
|
237
|
+
found = await query.list();
|
|
178
238
|
const matched = new Set();
|
|
179
239
|
for (const file of found) {
|
|
180
240
|
if ((isFolder(file) && !options.isDocument?.(file)) ||
|
|
@@ -182,8 +242,6 @@ export const planDocuments = async (options, source, state, roots, limit, pictur
|
|
|
182
242
|
!query?.matches(file) ||
|
|
183
243
|
matched.has(file.id))
|
|
184
244
|
continue;
|
|
185
|
-
if (!files.has(file.id) && ++discovered > MAX_ENTRIES)
|
|
186
|
-
throw tooManyEntries();
|
|
187
245
|
// Listed again on every sync, so only a new or changed file is work; a skipped one keeps its skip.
|
|
188
246
|
const prior = state.documents[file.id]?.metadata ?? state.skipped.find((item) => item.id === file.id)?.file;
|
|
189
247
|
if (initial || !prior || changedSince(prior, file))
|
|
@@ -219,7 +277,7 @@ export const planDocuments = async (options, source, state, roots, limit, pictur
|
|
|
219
277
|
jobs.push({ id, kind: "save", file });
|
|
220
278
|
}
|
|
221
279
|
}
|
|
222
|
-
for (const id of
|
|
280
|
+
for (const id of Object.keys(state.documents)) {
|
|
223
281
|
const file = files.get(id);
|
|
224
282
|
if (!file || !inScope(file, roots, folders, matched, query))
|
|
225
283
|
jobs.push({ id, kind: "remove" });
|
|
@@ -227,11 +285,13 @@ export const planDocuments = async (options, source, state, roots, limit, pictur
|
|
|
227
285
|
// Only retain metadata for selected ancestry; the account-wide feed includes unrelated folders.
|
|
228
286
|
const scopedFolders = Object.fromEntries(Object.entries(folders).filter(([, file]) => within(file, roots, folders) || roots.includes(file.id)));
|
|
229
287
|
return {
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
288
|
+
pending: {
|
|
289
|
+
scope,
|
|
290
|
+
cursor,
|
|
291
|
+
folders: scopedFolders,
|
|
292
|
+
jobs,
|
|
293
|
+
skipped,
|
|
294
|
+
...(query?.mode === "include" ? { matched: [...matched] } : {}),
|
|
295
|
+
},
|
|
236
296
|
};
|
|
237
297
|
};
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { CliError } from "@wirecat/cli-base";
|
|
2
|
+
import type { Deadline } from "../../background/budget.js";
|
|
2
3
|
import type { DocumentSource, SourceFile, SourceIndex, SourceSkip, SourceStatePort } from "../../documents/model.js";
|
|
3
4
|
import type { SourceQuery } from "../../documents/scope.js";
|
|
4
5
|
export interface SyncOptions<M extends SourceFile> {
|
|
@@ -10,8 +11,20 @@ export interface SyncOptions<M extends SourceFile> {
|
|
|
10
11
|
query?: SourceQuery<M>;
|
|
11
12
|
textLimit: number;
|
|
12
13
|
max: number;
|
|
14
|
+
/**
|
|
15
|
+
* The entries one call walks, each folder listing counted as one too; the walk saves where it stopped and the next
|
|
16
|
+
* sync goes on from there, so a tree of any size is synced over several calls.
|
|
17
|
+
*/
|
|
13
18
|
maxEntries: number;
|
|
19
|
+
/** @deprecated No longer used: a walk past `maxEntries` stops and goes on in the next sync instead of failing. */
|
|
14
20
|
limitError?(): CliError;
|
|
21
|
+
/** Asked before each folder listing and each file read: a stop ends the call with its work saved. */
|
|
22
|
+
budget?: {
|
|
23
|
+
take(): "requests" | "deadline" | "cancelled" | null;
|
|
24
|
+
readonly stopped?: string | null;
|
|
25
|
+
};
|
|
26
|
+
/** The run's hard limit: the word index at the end of a sync stops before it, less its margin. */
|
|
27
|
+
deadline?: Deadline;
|
|
15
28
|
isFolder(file: M): boolean;
|
|
16
29
|
/**
|
|
17
30
|
* A folder that is a document too, such as a wiki page with sub-pages: walked for its children and imported
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { SourceFile } from "../../documents/model.js";
|
|
2
|
+
import { type WalkStop } from "./plan.js";
|
|
2
3
|
import type { SyncOptions } from "./ports.js";
|
|
3
4
|
export interface SyncResult {
|
|
4
5
|
saved: number;
|
|
@@ -19,5 +20,15 @@ export interface SyncResult {
|
|
|
19
20
|
message: string;
|
|
20
21
|
}[];
|
|
21
22
|
coverage: string;
|
|
23
|
+
/** The pass is done: the folder walk finished and no operation is left for another sync. */
|
|
24
|
+
complete: boolean;
|
|
25
|
+
/** Why this call stopped before the pass's end: its entry bound (`maxEntries`) or its request budget. */
|
|
26
|
+
stopped?: WalkStop;
|
|
27
|
+
/** A folder walk still under way; the next sync goes on from where this one stopped. */
|
|
28
|
+
walk?: {
|
|
29
|
+
folders_left: number;
|
|
30
|
+
entries: number;
|
|
31
|
+
listed: number;
|
|
32
|
+
};
|
|
22
33
|
}
|
|
23
34
|
export declare const syncDocuments: <M extends SourceFile>(options: SyncOptions<M>) => Promise<SyncResult>;
|
|
@@ -1,11 +1,13 @@
|
|
|
1
1
|
import { createHash } from "node:crypto";
|
|
2
2
|
import { CliError, isCliError } from "@wirecat/cli-base";
|
|
3
|
+
import { deadlineMargin } from "../../background/budget.js";
|
|
3
4
|
import { catalogFiles } from "../../documents/catalog.js";
|
|
4
5
|
import { inScope } from "../../documents/scope.js";
|
|
5
6
|
import { changedSince, planDocuments, scopeHash } from "./plan.js";
|
|
6
7
|
const MAX_PICTURE_BYTES = 25 * 1024 * 1024;
|
|
7
8
|
const SAVE_EVERY_MS = 2000;
|
|
8
9
|
const SAVE_EVERY_ITEMS = 25;
|
|
10
|
+
const SETTLE_MS = 10_000;
|
|
9
11
|
const isPicture = (file) => file.mime.startsWith("image/") && file.canDownload;
|
|
10
12
|
const hashText = (text) => createHash("sha256").update(text).digest("hex");
|
|
11
13
|
export const syncDocuments = async (options) => {
|
|
@@ -25,6 +27,7 @@ export const syncDocuments = async (options) => {
|
|
|
25
27
|
pending: 0,
|
|
26
28
|
skipped: [],
|
|
27
29
|
errors: [],
|
|
30
|
+
complete: true,
|
|
28
31
|
coverage: query?.mode === "include"
|
|
29
32
|
? "selected folders and the files a saved query finds; unsupported files are reported"
|
|
30
33
|
: query?.mode === "exclude"
|
|
@@ -44,18 +47,37 @@ export const syncDocuments = async (options) => {
|
|
|
44
47
|
if (state.principal !== principal)
|
|
45
48
|
throw new CliError("configuration_error", "sync state belongs to another source account");
|
|
46
49
|
const currentScope = scopeHash(profile.roots, query, options.isDocument !== undefined);
|
|
47
|
-
|
|
48
|
-
|
|
50
|
+
if (state.pending && (state.pending.scope !== currentScope || rescan)) {
|
|
51
|
+
// Work the new plan cannot see: a file saved to the index before its state was.
|
|
52
|
+
for (const job of state.pending.jobs)
|
|
53
|
+
if (!state.documents[job.id]) {
|
|
54
|
+
await index.remove(profile.principal, job.id);
|
|
55
|
+
statePort.removeSnapshot(job.id);
|
|
56
|
+
}
|
|
49
57
|
delete state.pending;
|
|
58
|
+
// Saved at once: a call the walk cuts short would otherwise find the old batch again and repeat these removals.
|
|
59
|
+
statePort.save(state);
|
|
60
|
+
}
|
|
61
|
+
// A rescan starts the walk over, a saved one included; the cursor is dropped on disk, so the syncs after it
|
|
62
|
+
// continue the walk until it ends.
|
|
50
63
|
if (rescan) {
|
|
51
64
|
delete state.cursor;
|
|
52
65
|
statePort.removeWalk();
|
|
66
|
+
statePort.save(state);
|
|
53
67
|
}
|
|
54
68
|
if (!state.pending) {
|
|
55
|
-
|
|
69
|
+
const plan = await planDocuments(options, source, state, profile.roots, limit, profile.picture_search ?? false, {
|
|
56
70
|
read: (scope) => statePort.readWalk(scope),
|
|
57
71
|
save: (walk) => statePort.saveWalk(walk),
|
|
58
72
|
});
|
|
73
|
+
if ("walk" in plan) {
|
|
74
|
+
result.complete = false;
|
|
75
|
+
result.stopped = plan.walk.stopped;
|
|
76
|
+
result.walk = { folders_left: plan.walk.folders_left, entries: plan.walk.entries, listed: plan.walk.listed };
|
|
77
|
+
result.skipped = state.skipped.map(({ id, reason }) => ({ id, reason }));
|
|
78
|
+
return result;
|
|
79
|
+
}
|
|
80
|
+
state.pending = plan.pending;
|
|
59
81
|
state.skipped = state.pending.skipped;
|
|
60
82
|
statePort.save(state);
|
|
61
83
|
statePort.removeWalk();
|
|
@@ -144,6 +166,11 @@ export const syncDocuments = async (options) => {
|
|
|
144
166
|
else {
|
|
145
167
|
if (!source)
|
|
146
168
|
throw new CliError("configuration_error", "a pending save requires source sign-in");
|
|
169
|
+
const stop = options.budget?.take();
|
|
170
|
+
if (stop) {
|
|
171
|
+
result.stopped = stop;
|
|
172
|
+
break;
|
|
173
|
+
}
|
|
147
174
|
const content = await source.content(job.id, limit);
|
|
148
175
|
const listed = query?.mode === "include" && (pending.matched?.includes(job.id) ?? query.matches(content.metadata));
|
|
149
176
|
if (!listed && !inScope(content.metadata, profile.roots, pending.folders, new Set(), query)) {
|
|
@@ -205,6 +232,7 @@ export const syncDocuments = async (options) => {
|
|
|
205
232
|
}
|
|
206
233
|
flush();
|
|
207
234
|
result.pending = pending.jobs.length;
|
|
235
|
+
result.complete = !pending.jobs.length;
|
|
208
236
|
if (!pending.jobs.length) {
|
|
209
237
|
state.cursor = pending.cursor;
|
|
210
238
|
state.scope = pending.scope;
|
|
@@ -217,6 +245,16 @@ export const syncDocuments = async (options) => {
|
|
|
217
245
|
state.lastSync = new Date(now()).toISOString();
|
|
218
246
|
statePort.save(state);
|
|
219
247
|
}
|
|
248
|
+
// A reader that cannot write finds the text by its words only once it is indexed; a search that can write does it
|
|
249
|
+
// itself, so what this bound leaves, or a failure here, is indexed by the next one. Skipped after a stop, and kept
|
|
250
|
+
// inside the run's deadline less its margin, so the partial answer and its next step still reach the caller.
|
|
251
|
+
// Real time: `now` may be fixed.
|
|
252
|
+
const settleFrom = Date.now();
|
|
253
|
+
const settleUntil = Math.min(settleFrom + SETTLE_MS, options.deadline ? options.deadline.endsAt - deadlineMargin(options.deadline) : Number.POSITIVE_INFINITY);
|
|
254
|
+
if (!result.stopped && !options.budget?.stopped && settleUntil > settleFrom)
|
|
255
|
+
await index
|
|
256
|
+
.settle?.({ until: () => signal?.aborted === true || Date.now() >= settleUntil })
|
|
257
|
+
.catch(() => undefined);
|
|
220
258
|
return result;
|
|
221
259
|
}
|
|
222
260
|
finally {
|
|
@@ -14,12 +14,12 @@ export declare const metadataService: (deps: ServiceDeps) => {
|
|
|
14
14
|
dryRun?: boolean;
|
|
15
15
|
}) => Promise<{
|
|
16
16
|
items: ({
|
|
17
|
+
error?: undefined;
|
|
17
18
|
chatId: string;
|
|
18
19
|
tags: import("../domain/channel-tags.js").ChannelTagMatch[];
|
|
19
20
|
algorithm: string;
|
|
20
21
|
fetchedAt: string | null;
|
|
21
22
|
source: string;
|
|
22
|
-
error?: undefined;
|
|
23
23
|
} | {
|
|
24
24
|
tags?: undefined;
|
|
25
25
|
algorithm?: undefined;
|