@wirecat/cli-core 0.702.0 → 0.704.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.
Files changed (36) hide show
  1. package/CHANGELOG.md +68 -0
  2. package/dist/background/daily-archive-timer.d.ts +1 -1
  3. package/dist/cli/messenger/conversation-answers.d.ts +1 -1
  4. package/dist/cli/messenger/conversation-answers.js +4 -1
  5. package/dist/cli/messenger/conversations-command.js +2 -1
  6. package/dist/crm/cli/index.js +8 -5
  7. package/dist/crm/manifest/source.js +2 -2
  8. package/dist/crm/testing/manifest-fake.js +18 -0
  9. package/dist/documents/file-mirror-model.d.ts +88 -0
  10. package/dist/documents/file-mirror-model.js +1 -0
  11. package/dist/documents/index.d.ts +5 -2
  12. package/dist/documents/index.js +2 -0
  13. package/dist/documents/push-model.d.ts +3 -0
  14. package/dist/embeddings/onnx-runtime.d.ts +1 -1
  15. package/dist/embeddings/onnx-runtime.js +42 -61
  16. package/dist/errors/reasons.js +8 -0
  17. package/dist/ports/wirecat-tools.js +7 -0
  18. package/dist/sends/upload.js +0 -1
  19. package/dist/services/documents/mirror-watch.d.ts +12 -4
  20. package/dist/services/documents/mirror-watch.js +2 -0
  21. package/dist/services/documents/mirrors.d.ts +55 -35
  22. package/dist/services/documents/mirrors.js +63 -66
  23. package/dist/services/documents/push.d.ts +56 -4
  24. package/dist/services/documents/push.js +131 -24
  25. package/dist/services/embeddings-search.js +35 -10
  26. package/dist/services/embeddings.d.ts +2 -0
  27. package/dist/services/files/mirror.d.ts +145 -0
  28. package/dist/services/files/mirror.js +603 -0
  29. package/dist/services/files/versions.d.ts +50 -0
  30. package/dist/services/files/versions.js +142 -0
  31. package/dist/services/metadata.d.ts +1 -1
  32. package/dist/store/sqlite/vectors.d.ts +1 -0
  33. package/dist/store/sqlite/vectors.js +25 -2
  34. package/dist/store/vectors.d.ts +2 -0
  35. package/dist/store/vectors.js +4 -2
  36. package/package.json +1 -1
@@ -15,6 +15,8 @@ import { refreshSearch } from "./search-refresh.js";
15
15
  const CANDIDATES = 50;
16
16
  /** Conversations the meaning half reads across chats before it gives each chat its share. */
17
17
  const ACROSS_CHATS = 500;
18
+ /** The owner's own chunks shown beside the hits: a result list ranks them low, and a question is often about them. */
19
+ const OWN_BLOCK = 5;
18
20
  /**
19
21
  * What one chat brings to each list when several are searched, so a large chat cannot fill them. Its meaning hits
20
22
  * count by how far they lead its 20th best: a large chat holds some text near any question, and a small chat's
@@ -190,6 +192,7 @@ export const embeddingsSearch = (deps) => {
190
192
  };
191
193
  });
192
194
  const meaning = [];
195
+ const own = [];
193
196
  const sources = words.sources;
194
197
  if (target.installed && prepared.scopeAccounts.length && eligibility?.conversations.length !== 0) {
195
198
  await withEmbedder(deps, target, choice, {}, async (embedder) => {
@@ -219,8 +222,21 @@ export const embeddingsSearch = (deps) => {
219
222
  ...hits,
220
223
  ...images.filter((hit) => !eligibility || eligibility.conversations.includes(hit.summary.id)),
221
224
  ].sort((a, b) => b.score - a.score);
225
+ const mine = await store.vectors.nearestConversations(one, {
226
+ ...scope,
227
+ ...(since === undefined ? {} : { since }),
228
+ model: target.key,
229
+ limit: OWN_BLOCK,
230
+ query: vector,
231
+ fromOwner: true,
232
+ ...(eligibility ? { conversations: eligibility.conversations } : {}),
233
+ });
222
234
  for (const hit of found)
223
235
  sources.set(hit.summary.id, one);
236
+ for (const hit of mine) {
237
+ sources.set(hit.summary.id, one);
238
+ own.push(hit);
239
+ }
224
240
  meaning.push(...(whole ? found.map((hit) => ({ hit, lead: hit.score })) : shared(found, limit)));
225
241
  }
226
242
  });
@@ -252,22 +268,31 @@ export const embeddingsSearch = (deps) => {
252
268
  ].sort();
253
269
  if (scoped.length > 1)
254
270
  readiness.scopes = scoped;
271
+ const located = (hit) => {
272
+ const source = sources.get(hit.summary.id);
273
+ return {
274
+ ...hit,
275
+ source,
276
+ locator: formatLocator({ ...source, chat: hit.summary.chatId, message: hit.chunk.firstMessageId }),
277
+ };
278
+ };
279
+ const hits = fused(meaning.map(({ hit }) => hit), words.hits, meaningFloor(target.key))
280
+ .slice(0, limit)
281
+ .map(located);
282
+ const shown = new Set(hits.map((hit) => hit.summary.id));
283
+ const ownHits = own
284
+ .filter((hit) => !shown.has(hit.summary.id))
285
+ .sort((a, b) => b.score - a.score)
286
+ .slice(0, OWN_BLOCK)
287
+ .map((hit) => located({ ...hit, by: ["meaning"] }));
255
288
  return {
256
289
  accounts: prepared.scopeAccounts,
257
290
  model: target.id,
258
291
  meaning: target.installed ? "searched" : "unavailable",
259
292
  ...(wordsOutOfTime ? { words: "out-of-time" } : {}),
260
293
  ...(words.respellings.size ? { respelled: Object.fromEntries(words.respellings) } : {}),
261
- hits: fused(meaning.map(({ hit }) => hit), words.hits, meaningFloor(target.key))
262
- .slice(0, limit)
263
- .map((hit) => {
264
- const source = sources.get(hit.summary.id);
265
- return {
266
- ...hit,
267
- source,
268
- locator: formatLocator({ ...source, chat: hit.summary.chatId, message: hit.chunk.firstMessageId }),
269
- };
270
- }),
294
+ hits,
295
+ ...(ownHits.length ? { own: ownHits } : {}),
271
296
  readiness,
272
297
  ...(locallyRefreshed ? { prepared: locallyRefreshed } : {}),
273
298
  ...(refreshed
@@ -148,6 +148,8 @@ export interface FoundConversations {
148
148
  title: string | null;
149
149
  };
150
150
  hits: FoundConversation[];
151
+ /** The owner's own conversations nearest in meaning that `hits` left out. */
152
+ own?: FoundConversation[];
151
153
  readiness: SearchReadiness;
152
154
  /** Kept for callers of 0.141.0 and before; `readiness.wordsOnly` says more. */
153
155
  embeddedOnlyElsewhere: Id[];
@@ -0,0 +1,145 @@
1
+ import { CliError } from "@wirecat/cli-base";
2
+ import type { FileMirrorStatePort, FileStepKind, FileTarget } from "../../documents/file-mirror-model.js";
3
+ import type { Keep } from "../../documents/push-model.js";
4
+ import { type Mirror } from "../documents/mirrors.js";
5
+ import { type MirrorAction, type PushConflict } from "../documents/push.js";
6
+ import type { FileVersions } from "./versions.js";
7
+ type Side = "local" | "remote";
8
+ export interface FileMirrorOptions {
9
+ /** Absolute path of the local folder. */
10
+ root: string;
11
+ /** The remote folder, as the target addresses it. */
12
+ folder: string;
13
+ target: FileTarget;
14
+ state: FileMirrorStatePort;
15
+ /** Keeps every local file's content before the sync overwrites or deletes it, and logs each change. */
16
+ versions: FileVersions;
17
+ principal: string;
18
+ /** Settles conflicts; without it they are reported and left. */
19
+ keep?: Keep;
20
+ dryRun?: boolean;
21
+ /** Lets a run delete this many files although the deletion guard would stop it. */
22
+ allowDeletions?: number;
23
+ /**
24
+ * The owner confirmed that a root reported as replaced is the right folder now: its identity is recorded again and
25
+ * the run is a first sync (identical files adopted, different ones conflicts), deleting nothing.
26
+ */
27
+ repin?: boolean;
28
+ /** How the app names the remote side; it ends a conflict copy's name. */
29
+ remote: string;
30
+ /** Runs one write behind the app's permission checks; `side` says which side it changes. */
31
+ guard<T>(attempt: {
32
+ action: MirrorAction;
33
+ side: Side;
34
+ path: string;
35
+ operationId: string;
36
+ }, act: () => Promise<T>): Promise<T>;
37
+ operationId(): string;
38
+ errorOf(error: unknown): CliError;
39
+ assertHeld(): void;
40
+ signal?: AbortSignal;
41
+ maxFiles?: number;
42
+ now?(): number;
43
+ }
44
+ export interface FileStep {
45
+ kind: FileStepKind;
46
+ path: string;
47
+ /** A rename's new path, or a kept copy's name. */
48
+ to?: string;
49
+ }
50
+ export interface FileMirrorResult {
51
+ uploaded: string[];
52
+ downloaded: string[];
53
+ trashed: {
54
+ path: string;
55
+ side: Side;
56
+ }[];
57
+ renamed: {
58
+ from: string;
59
+ to: string;
60
+ }[];
61
+ adopted: string[];
62
+ unchanged: number;
63
+ conflicts: PushConflict[];
64
+ errors: {
65
+ path: string;
66
+ code: string;
67
+ message: string;
68
+ }[];
69
+ /**
70
+ * Files of the last sync missing from a side that cannot prove a deletion: its listing was incomplete, or its root
71
+ * is gone or is another folder. Nothing is deleted for them.
72
+ */
73
+ unknown: {
74
+ path: string;
75
+ side: Side;
76
+ reason: string;
77
+ }[];
78
+ /** Every step but the unchanged files, on a dry run. */
79
+ plan?: FileStep[];
80
+ /** On a dry run, the deletions that would stop the real run without `allowDeletions`. */
81
+ tooManyDeletions?: number;
82
+ stopped?: true;
83
+ }
84
+ /**
85
+ * Syncs a local folder and a remote folder of plain files both ways, whole files at a time. Each path is compared
86
+ * three ways (local now, remote now, last sync): a change on one side is copied to the other, the same content on
87
+ * both is adopted, and different changes on both are a conflict unless `keep` settles it. Deletions go to the
88
+ * remote's trash and to `<root>/.wirecat-trash/`. A remote rename, found by the file's id, renames the local file.
89
+ * Every write is conditional on what the listing saw, so a crash at any point is settled by the next run.
90
+ */
91
+ export declare const syncFileMirror: (options: FileMirrorOptions) => Promise<FileMirrorResult>;
92
+ /** A file mirror's state in `<dir>/<id>.json`, refused rather than reset when invalid. */
93
+ export declare const fileSyncState: (dir: string, mirror: Mirror, principal: string) => FileMirrorStatePort;
94
+ /** What each mirror's next sync would send from local changes alone; the remote is not asked. */
95
+ export declare const fileMirrorStatus: (mirrors: Mirror[], stateOf: (mirror: Mirror) => FileMirrorStatePort) => Promise<{
96
+ id: string;
97
+ path: string;
98
+ folder: string;
99
+ upload: string[];
100
+ delete: string[];
101
+ unchanged: number;
102
+ remote_checked: boolean;
103
+ }[]>;
104
+ /**
105
+ * A watch's `remoteChanges` for a file target: every mirror when the remote's revision moved since the last poll (or
106
+ * at the first poll), none when it did not, and every mirror at each poll when the target has no revision.
107
+ */
108
+ export declare const fileRemoteChanges: (target: FileTarget) => (signal: AbortSignal) => Promise<string[] | "all">;
109
+ export interface FileMirrorSyncOptions extends Pick<FileMirrorOptions, "target" | "principal" | "keep" | "dryRun" | "allowDeletions" | "repin" | "guard" | "operationId" | "errorOf" | "signal"> {
110
+ mirrors: Mirror[];
111
+ stateOf(mirror: Mirror): FileMirrorStatePort;
112
+ versionsOf(mirror: Mirror): FileVersions;
113
+ lock(): Promise<{
114
+ assertHeld(): void;
115
+ release(): Promise<void> | void;
116
+ }>;
117
+ /** How the app names the remote side in reports and conflict copies. */
118
+ remote: string;
119
+ }
120
+ /** Syncs each file mirror under one lock, with one `mirror.partial` issue per mirror left with conflicts or errors. */
121
+ export declare const syncFileMirrors: (options: FileMirrorSyncOptions) => Promise<{
122
+ items: (Omit<{
123
+ id: string;
124
+ path: string;
125
+ } & FileMirrorResult, "conflicts"> & {
126
+ conflicts: {
127
+ path: string;
128
+ url?: string | undefined;
129
+ reason: string;
130
+ parts?: {
131
+ [x: string]: string;
132
+ last_sync: string;
133
+ local: string;
134
+ }[] | undefined;
135
+ local_changed_at?: string | undefined;
136
+ local_size?: number | undefined;
137
+ }[];
138
+ })[];
139
+ issues: {
140
+ code: "provider_error";
141
+ reason: string;
142
+ source: string;
143
+ }[];
144
+ }>;
145
+ export {};