@operad-io/memory 0.3.1 → 0.4.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.
@@ -1,5 +1,7 @@
1
1
  /** The shapes the full-text index and the search tool share. */
2
2
 
3
+ import type { ObjectStore } from "@operad-io/engine";
4
+
3
5
  /** What a search may cover. */
4
6
  export const SEARCH_SCOPES = ["wiki", "memory", "chats"] as const;
5
7
 
@@ -149,6 +151,10 @@ export interface BackendQuery {
149
151
  readonly owner: string;
150
152
  readonly fields?: Readonly<Record<string, string | readonly string[]>> | undefined;
151
153
  readonly limit: number;
154
+ /** Reach only this document. A caller that names one has already decided who may. */
155
+ readonly document?: string | undefined;
156
+ /** Return every matching chunk of a document, not only the best one. */
157
+ readonly chunks?: boolean | undefined;
152
158
  }
153
159
 
154
160
  /**
@@ -184,6 +190,13 @@ export interface SearchRequest {
184
190
  readonly scopes?: readonly SearchScope[] | undefined;
185
191
  readonly fields?: Readonly<Record<string, string | readonly string[]>> | undefined;
186
192
  readonly limit?: number | undefined;
193
+ /**
194
+ * Search inside this one chat, whoever the caller is: every matching place in
195
+ * it comes back, not only the best one, each with the message it is in.
196
+ */
197
+ readonly chat?: string | undefined;
198
+ /** The record the chat's messages are read from, to say which message a hit is in. */
199
+ readonly store?: ObjectStore | undefined;
187
200
  }
188
201
 
189
202
  /** One search result. */
@@ -197,6 +210,8 @@ export interface SearchHit {
197
210
  readonly snippet: string;
198
211
  readonly score: number;
199
212
  readonly fields: DocumentFields;
213
+ /** In a search inside one chat: the id of the message the match is in. */
214
+ readonly message?: string | undefined;
200
215
  }
201
216
 
202
217
  /** A directory's description, returned next to the hits found inside it. */
package/src/tidy.ts CHANGED
@@ -4,7 +4,7 @@
4
4
  * drops too many existing index entries and keeps only additions instead.
5
5
  */
6
6
 
7
- import { existsSync, mkdirSync, readFileSync, readdirSync, writeFileSync } from "node:fs";
7
+ import { chmodSync, existsSync, mkdirSync, readFileSync, readdirSync, writeFileSync } from "node:fs";
8
8
  import { dirname, join, relative, sep } from "node:path";
9
9
  import {
10
10
  MEMORY_INDEX_MAX,
@@ -340,3 +340,43 @@ export function tidyCommitSummary(
340
340
  summary,
341
341
  };
342
342
  }
343
+
344
+ const INSTRUCTIONS_FILE = "instructions.md";
345
+
346
+ /**
347
+ * Makes the person's instructions read-only for the length of a tidy run. A
348
+ * tidy never changes them; the run's commit puts them back anyway, in case the
349
+ * run got past the mode.
350
+ */
351
+ export function lockInstructions(memoryDir: string): void {
352
+ const file = join(memoryDir, INSTRUCTIONS_FILE);
353
+ if (existsSync(file)) chmodSync(file, 0o444);
354
+ }
355
+
356
+ /** Ends the read-only mode of the instructions. */
357
+ export function unlockInstructions(memoryDir: string): void {
358
+ const file = join(memoryDir, INSTRUCTIONS_FILE);
359
+ if (existsSync(file)) chmodSync(file, 0o644);
360
+ }
361
+
362
+ /**
363
+ * Ends the read-only mode and puts the instructions back as the last commit
364
+ * has them, whatever the run did to the file (changed it, deleted it, or made
365
+ * it writable and rewrote it). A file no commit has yet is only made writable.
366
+ * Returns true when it had to rewrite the file.
367
+ */
368
+ export function restoreInstructions(repository: Repository, directory: string): boolean {
369
+ const file = join(directory, INSTRUCTIONS_FILE);
370
+ const path = relative(repository.root, file).split(sep).join("/");
371
+ // The file is tracked and differs from the last commit: check it out again.
372
+ // Git writes the bytes as they were, which a trimmed `show` would not.
373
+ const tracked = memoryGit(repository.root, ["ls-tree", "--name-only", "HEAD", "--", path]);
374
+ const changed = memoryGit(repository.root, ["status", "--porcelain", "--", path]);
375
+ let restored = false;
376
+ if (tracked.ok && tracked.out.length > 0 && changed.out.length > 0) {
377
+ if (existsSync(file)) chmodSync(file, 0o644);
378
+ restored = memoryGit(repository.root, ["checkout", "HEAD", "--", path]).ok;
379
+ }
380
+ if (existsSync(file)) chmodSync(file, 0o644);
381
+ return restored;
382
+ }