@dreamlake/dreamlake-cli 0.104.8 → 0.105.1

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 (110) hide show
  1. package/dist/api/index.d.ts +27 -17
  2. package/dist/api/index.js +25 -16
  3. package/dist/api/memo-character-plan.d.ts +2 -0
  4. package/dist/api/memo-character-plan.js +2 -0
  5. package/dist/api/memo-editing.d.ts +2 -0
  6. package/dist/api/memo-editing.js +2 -0
  7. package/dist/api/memo-edits.d.ts +63 -0
  8. package/dist/api/memo-edits.js +206 -0
  9. package/dist/api/memo-events.d.ts +44 -0
  10. package/dist/api/memo-events.js +65 -0
  11. package/dist/api/memo-facade.d.ts +294 -0
  12. package/dist/api/memo-facade.js +382 -0
  13. package/dist/api/memo-files.d.ts +111 -0
  14. package/dist/api/memo-files.js +110 -0
  15. package/dist/api/memo-html-read.d.ts +2 -0
  16. package/dist/api/memo-html-read.js +2 -0
  17. package/dist/api/memo-html.d.ts +2 -0
  18. package/dist/api/memo-html.js +2 -0
  19. package/dist/api/memo-linger.d.ts +85 -0
  20. package/dist/api/memo-linger.js +271 -0
  21. package/dist/api/memo-presence.d.ts +109 -0
  22. package/dist/api/memo-presence.js +75 -0
  23. package/dist/api/memo-reading.d.ts +55 -0
  24. package/dist/api/memo-reading.js +43 -0
  25. package/dist/api/memo-toc.d.ts +2 -0
  26. package/dist/api/memo-toc.js +2 -0
  27. package/dist/api/memo-write.d.ts +119 -0
  28. package/dist/api/memo-write.js +221 -0
  29. package/dist/api/memos.d.ts +344 -0
  30. package/dist/api/memos.js +231 -0
  31. package/dist/api/note-character-plan.d.ts +2 -2
  32. package/dist/api/note-character-plan.js +2 -2
  33. package/dist/api/note-editing.d.ts +2 -2
  34. package/dist/api/note-editing.js +2 -2
  35. package/dist/api/note-edits.d.ts +2 -59
  36. package/dist/api/note-edits.js +2 -205
  37. package/dist/api/note-events.d.ts +2 -41
  38. package/dist/api/note-events.js +2 -64
  39. package/dist/api/note-facade.d.ts +2 -268
  40. package/dist/api/note-facade.js +2 -376
  41. package/dist/api/note-files.d.ts +2 -102
  42. package/dist/api/note-files.js +2 -107
  43. package/dist/api/note-html-read.d.ts +2 -2
  44. package/dist/api/note-html-read.js +2 -2
  45. package/dist/api/note-html.d.ts +2 -2
  46. package/dist/api/note-html.js +2 -2
  47. package/dist/api/note-linger.d.ts +2 -83
  48. package/dist/api/note-linger.js +2 -270
  49. package/dist/api/note-presence.d.ts +2 -100
  50. package/dist/api/note-presence.js +2 -73
  51. package/dist/api/note-reading.d.ts +2 -51
  52. package/dist/api/note-reading.js +2 -42
  53. package/dist/api/note-toc.d.ts +2 -2
  54. package/dist/api/note-toc.js +2 -2
  55. package/dist/api/note-write.d.ts +2 -110
  56. package/dist/api/note-write.js +2 -217
  57. package/dist/api/notes.d.ts +2 -335
  58. package/dist/api/notes.js +2 -230
  59. package/dist/api/open-resources.js +3 -3
  60. package/dist/cli/folders/index.d.ts +1 -1
  61. package/dist/cli/folders/index.js +4 -4
  62. package/dist/cli/help-examples.generated.js +6 -6
  63. package/dist/cli/index.js +2 -2
  64. package/dist/cli/{notes → memos}/api-transport.d.ts +1 -1
  65. package/dist/cli/{notes → memos}/api-transport.js +1 -1
  66. package/dist/cli/{notes → memos}/archive-command.d.ts +1 -1
  67. package/dist/cli/{notes → memos}/archive-command.js +5 -5
  68. package/dist/cli/{notes → memos}/character-plan.d.ts +3 -1
  69. package/dist/cli/{notes → memos}/character-plan.js +7 -5
  70. package/dist/cli/{notes → memos}/document.d.ts +9 -9
  71. package/dist/cli/{notes → memos}/document.js +12 -12
  72. package/dist/cli/{notes → memos}/edit-commands.d.ts +8 -8
  73. package/dist/cli/{notes → memos}/edit-commands.js +14 -14
  74. package/dist/cli/{notes → memos}/events.d.ts +3 -3
  75. package/dist/cli/{notes → memos}/events.js +2 -2
  76. package/dist/cli/{notes → memos}/files-commands.d.ts +2 -2
  77. package/dist/cli/{notes → memos}/files-commands.js +5 -5
  78. package/dist/cli/{notes → memos}/index.d.ts +16 -16
  79. package/dist/cli/{notes → memos}/index.js +53 -53
  80. package/dist/cli/{notes → memos}/linger.d.ts +3 -3
  81. package/dist/cli/{notes → memos}/linger.js +5 -5
  82. package/dist/cli/{notes → memos}/move-command.d.ts +5 -5
  83. package/dist/cli/{notes → memos}/move-command.js +6 -6
  84. package/dist/cli/{notes → memos}/rm-command.d.ts +2 -2
  85. package/dist/cli/{notes → memos}/rm-command.js +2 -2
  86. package/dist/cli/{notes → memos}/select-command.d.ts +1 -1
  87. package/dist/cli/{notes → memos}/select-command.js +4 -4
  88. package/dist/cli/{notes → memos}/suggest-diff.d.ts +1 -1
  89. package/dist/cli/{notes → memos}/suggest-diff.js +2 -2
  90. package/dist/cli/{notes → memos}/transfer-command.d.ts +2 -2
  91. package/dist/cli/{notes → memos}/transfer-command.js +2 -2
  92. package/dist/cli/share.d.ts +1 -1
  93. package/dist/cli/share.js +1 -1
  94. package/dist/cli/skill/index.js +1 -1
  95. package/dist/cli/update/index.js +3 -3
  96. package/dist/cli/version.d.ts +1 -1
  97. package/dist/cli/version.js +1 -1
  98. package/package.json +9 -9
  99. /package/dist/cli/{notes → memos}/editing.d.ts +0 -0
  100. /package/dist/cli/{notes → memos}/editing.js +0 -0
  101. /package/dist/cli/{notes → memos}/html-read.d.ts +0 -0
  102. /package/dist/cli/{notes → memos}/html-read.js +0 -0
  103. /package/dist/cli/{notes → memos}/html.d.ts +0 -0
  104. /package/dist/cli/{notes → memos}/html.js +0 -0
  105. /package/dist/cli/{notes → memos}/media-commands.d.ts +0 -0
  106. /package/dist/cli/{notes → memos}/media-commands.js +0 -0
  107. /package/dist/cli/{notes → memos}/toc.d.ts +0 -0
  108. /package/dist/cli/{notes → memos}/toc.js +0 -0
  109. /package/dist/cli/{notes → memos}/version-commands.d.ts +0 -0
  110. /package/dist/cli/{notes → memos}/version-commands.js +0 -0
@@ -17,15 +17,15 @@ export * from "./workspace.js";
17
17
  export * from "./content.js";
18
18
  export * from "./execution.js";
19
19
  export * from "./ownership.js";
20
- export * from "./note-files.js";
21
- export * from "./note-presence.js";
22
- export * from "./note-linger.js";
20
+ export * from "./memo-files.js";
21
+ export * from "./memo-presence.js";
22
+ export * from "./memo-linger.js";
23
23
  export * from "./jobs.js";
24
24
  export * from "./infra.js";
25
25
  import { type ClientOptions, type Transport } from "./client.js";
26
26
  export * from "./client.js";
27
27
  export * from "./sessions.js";
28
- export * from "./notes.js";
28
+ export * from "./memos.js";
29
29
  export * from "./agent.js";
30
30
  export * from "./skills.js";
31
31
  /** TypeScript operations use an explicit transport; importing does not execute CLI startup. */
@@ -568,7 +568,9 @@ export declare function createApi(transport: Transport): {
568
568
  }) => Promise<unknown>;
569
569
  managed: (id: string, action: "status" | "start" | "reconnect" | "stop", input: Record<string, unknown>) => Promise<unknown>;
570
570
  };
571
- notes: import("./note-facade.js").NotesFacade;
571
+ memos: import("./memo-facade.js").MemosFacade;
572
+ /** @deprecated Use memos. */
573
+ notes: import("./memo-facade.js").MemosFacade;
572
574
  skills: {
573
575
  list: () => Promise<import("./skills.js").SkillsList>;
574
576
  skill: (name: string) => Promise<import("./skills.js").SkillDetail>;
@@ -1659,7 +1661,9 @@ export declare function createClient(options: ClientOptions): {
1659
1661
  }) => Promise<unknown>;
1660
1662
  managed: (id: string, action: "status" | "start" | "reconnect" | "stop", input: Record<string, unknown>) => Promise<unknown>;
1661
1663
  };
1662
- notes: import("./note-facade.js").NotesFacade;
1664
+ memos: import("./memo-facade.js").MemosFacade;
1665
+ /** @deprecated Use memos. */
1666
+ notes: import("./memo-facade.js").MemosFacade;
1663
1667
  skills: {
1664
1668
  list: () => Promise<import("./skills.js").SkillsList>;
1665
1669
  skill: (name: string) => Promise<import("./skills.js").SkillDetail>;
@@ -2138,16 +2142,22 @@ export declare function createClient(options: ClientOptions): {
2138
2142
  };
2139
2143
  };
2140
2144
  export * from "./layout.js";
2141
- export * as noteEditing from "./note-editing.js";
2142
- export * as noteOutline from "./note-toc.js";
2143
- export * as noteHtml from "./note-html.js";
2144
- export * from "./note-html-read.js";
2145
- export { characterDiff, NotePatchError, PATCH_LIMITS, } from "./note-character-plan.js";
2145
+ export * as memoEditing from "./memo-editing.js";
2146
+ /** @deprecated Use memoEditing. */
2147
+ export * as noteEditing from "./memo-editing.js";
2148
+ export * as memoOutline from "./memo-toc.js";
2149
+ /** @deprecated Use memoOutline. */
2150
+ export * as noteOutline from "./memo-toc.js";
2151
+ export * as memoHtml from "./memo-html.js";
2152
+ /** @deprecated Use memoHtml. */
2153
+ export * as noteHtml from "./memo-html.js";
2154
+ export * from "./memo-html-read.js";
2155
+ export { characterDiff, MemoPatchError, NotePatchError, PATCH_LIMITS, } from "./memo-character-plan.js";
2146
2156
  export * from "./presigned-transfer.js";
2147
- export * from "./note-events.js";
2148
- export * from "./note-write.js";
2149
- export { documentEdits, documentInsertion } from "./note-edits.js";
2150
- export * from "./note-facade.js";
2151
- export type { NoteReadingOptions, NoteReadingPage, NoteFocusedRead } from "./note-reading.js";
2152
- export { lineRange, focusedRead, InvalidLineRange } from "./note-reading.js";
2157
+ export * from "./memo-events.js";
2158
+ export * from "./memo-write.js";
2159
+ export { documentEdits, documentInsertion } from "./memo-edits.js";
2160
+ export * from "./memo-facade.js";
2161
+ export type { MemoReadingOptions, MemoReadingPage, MemoFocusedRead, NoteReadingOptions, NoteReadingPage, NoteFocusedRead } from "./memo-reading.js";
2162
+ export { lineRange, focusedRead, InvalidLineRange } from "./memo-reading.js";
2153
2163
  export type { HostMovePreview, HostMoveRequest, HostMoveReceipt, HostMoveRetainedSessions, HostMoveRetainedRuns } from "./host-moves.js";
package/dist/api/index.js CHANGED
@@ -30,9 +30,9 @@ export * from "./workspace.js";
30
30
  export * from "./content.js";
31
31
  export * from "./execution.js";
32
32
  export * from "./ownership.js";
33
- export * from "./note-files.js";
34
- export * from "./note-presence.js";
35
- export * from "./note-linger.js";
33
+ export * from "./memo-files.js";
34
+ export * from "./memo-presence.js";
35
+ export * from "./memo-linger.js";
36
36
  export * from "./jobs.js";
37
37
  export * from "./infra.js";
38
38
  /** `@dreamlake/dreamlake-cli/api` — the DreamLake TypeScript API.
@@ -48,16 +48,17 @@ export * from "./infra.js";
48
48
  */
49
49
  import { agentApi, projectToolsApi } from "./agent.js";
50
50
  import { createTransport, } from "./client.js";
51
- import { notesFacade } from "./note-facade.js";
51
+ import { memosFacade } from "./memo-facade.js";
52
52
  import { sessionsApi } from "./sessions.js";
53
53
  import { skillsApi } from "./skills.js";
54
54
  export * from "./client.js";
55
55
  export * from "./sessions.js";
56
- export * from "./notes.js";
56
+ export * from "./memos.js";
57
57
  export * from "./agent.js";
58
58
  export * from "./skills.js";
59
59
  /** TypeScript operations use an explicit transport; importing does not execute CLI startup. */
60
60
  export function createApi(transport) {
61
+ const memos = memosFacade(transport);
61
62
  return {
62
63
  transport,
63
64
  openResources: openResourcesApi(transport),
@@ -77,7 +78,9 @@ export function createApi(transport) {
77
78
  teams: teamsApi(transport),
78
79
  sessions: (namespace) => sessionsApi(transport, namespace),
79
80
  terminal: (namespace) => terminalApi(transport, namespace),
80
- notes: notesFacade(transport),
81
+ memos,
82
+ /** @deprecated Use memos. */
83
+ notes: memos,
81
84
  skills: skillsApi(transport),
82
85
  projects: (namespace) => projectsApi(transport, namespace),
83
86
  nodes: nodesApi(transport),
@@ -113,14 +116,20 @@ export function createClient(options) {
113
116
  };
114
117
  }
115
118
  export * from "./layout.js";
116
- export * as noteEditing from "./note-editing.js";
117
- export * as noteOutline from "./note-toc.js";
118
- export * as noteHtml from "./note-html.js";
119
- export * from "./note-html-read.js";
120
- export { characterDiff, NotePatchError, PATCH_LIMITS, } from "./note-character-plan.js";
119
+ export * as memoEditing from "./memo-editing.js";
120
+ /** @deprecated Use memoEditing. */
121
+ export * as noteEditing from "./memo-editing.js";
122
+ export * as memoOutline from "./memo-toc.js";
123
+ /** @deprecated Use memoOutline. */
124
+ export * as noteOutline from "./memo-toc.js";
125
+ export * as memoHtml from "./memo-html.js";
126
+ /** @deprecated Use memoHtml. */
127
+ export * as noteHtml from "./memo-html.js";
128
+ export * from "./memo-html-read.js";
129
+ export { characterDiff, MemoPatchError, NotePatchError, PATCH_LIMITS, } from "./memo-character-plan.js";
121
130
  export * from "./presigned-transfer.js";
122
- export * from "./note-events.js";
123
- export * from "./note-write.js";
124
- export { documentEdits, documentInsertion } from "./note-edits.js";
125
- export * from "./note-facade.js";
126
- export { lineRange, focusedRead, InvalidLineRange } from "./note-reading.js";
131
+ export * from "./memo-events.js";
132
+ export * from "./memo-write.js";
133
+ export { documentEdits, documentInsertion } from "./memo-edits.js";
134
+ export * from "./memo-facade.js";
135
+ export { lineRange, focusedRead, InvalidLineRange } from "./memo-reading.js";
@@ -0,0 +1,2 @@
1
+ /** Public API export; the existing implementation stays at its original location. */
2
+ export * from "../cli/memos/character-plan.js";
@@ -0,0 +1,2 @@
1
+ /** Public API export; the existing implementation stays at its original location. */
2
+ export * from "../cli/memos/character-plan.js";
@@ -0,0 +1,2 @@
1
+ /** Public API export; the existing implementation stays at its original location. */
2
+ export * from "../cli/memos/editing.js";
@@ -0,0 +1,2 @@
1
+ /** Public API export; the existing implementation stays at its original location. */
2
+ export * from "../cli/memos/editing.js";
@@ -0,0 +1,63 @@
1
+ /** Unicode code-point edits against one retained Notes v2 baseline. */
2
+ export interface MemoSource {
3
+ note: string;
4
+ content: string;
5
+ hash: string;
6
+ revision: string;
7
+ }
8
+ export interface SourceEdit {
9
+ start: number;
10
+ end: number;
11
+ text: string;
12
+ }
13
+ export interface MemoEdit extends SourceEdit {
14
+ expected?: string;
15
+ }
16
+ export interface MemoPatch {
17
+ format: "inline-dff" | "diff";
18
+ patch: string;
19
+ baseRevision: string;
20
+ mode: "merge" | "exact";
21
+ }
22
+ /** The CLI's names for the same shapes; `src/cli/memos/document.ts` re-exports them. */
23
+ export type SourceSnapshot = MemoSource;
24
+ export type DocumentPatchPayload = MemoPatch;
25
+ /** Generated reading-view annotations are never saved as source. What is
26
+ * forbidden is NEW text that carries the generated header line
27
+ * (`<!-- notes-reading-view revision=… hash=…`). Deleting such a line, keeping
28
+ * one the note already has, or mentioning the marker in prose or a code block
29
+ * is allowed. Checked on raw inserted text before compilation (escaping would
30
+ * hide it) and on the insertion payloads of a caller-supplied patch. */
31
+ export declare const READING_VIEW_MARKER = "<!-- notes-reading-view";
32
+ export declare const READING_VIEW_HEADER: RegExp;
33
+ export declare class ReadingViewMarkerRefused extends Error {
34
+ constructor(what: string);
35
+ }
36
+ /** Inserted or replacement text only, never deletions or unchanged context. */
37
+ export declare function refuseReadingView(text: string, what: string): void;
38
+ /** Whole-body replacement: only lines that are new relative to the baseline count, so a header the note already holds survives a write. */
39
+ export declare function refuseNewReadingViewLines(baseline: string, content: string, what: string): void;
40
+ /** Apply the inline-dff subset this client emits (`@@ chars s:e @@` + one `~` line of `[-old-]`/`{+new+}`
41
+ * segments) to a baseline. null when the patch is outside that subset or its old text does not match. */
42
+ export declare function applyInlineDff(baseline: string, patch: string): string | null;
43
+ /** Apply a unified diff to a baseline, verifying context and removed lines. null when it does not apply cleanly. */
44
+ export declare function applyUnifiedDiff(baseline: string, patch: string): string | null;
45
+ /** A caller-supplied patch. With the baseline it was written against, the target is reconstructed and
46
+ * checked like a whole-body write (so a header split across hunks is seen assembled, and one the note
47
+ * already has may be re-added). Without a baseline, or when the patch does not apply to it, the check is
48
+ * BEST-EFFORT on the insertion payloads: inline-dff `{+…+}` (escapes decoded), concatenated across hunks
49
+ * that are adjacent in the original (one's end offset is the next's start, so no baseline text lies
50
+ * between them), or unified-diff `+` lines. Authoritative enforcement belongs to the server. */
51
+ export declare function refuseReadingViewPatch(patch: string, format: "inline-dff" | "diff", baseline?: string): void;
52
+ /** Exact original-source spans, with untouched gaps omitted from the PATCH. The target the edits
53
+ * produce is reconstructed and checked against the baseline, so a generated header assembled from
54
+ * several spans is refused exactly as a whole-body write would be. */
55
+ export declare function documentEdits(source: SourceSnapshot, edits: SourceEdit[], exact?: boolean): DocumentPatchPayload;
56
+ /** One literal insertion at an original-source Unicode code-point boundary. */
57
+ export declare function documentInsertion(source: SourceSnapshot, at: number, text: string, exact?: boolean): DocumentPatchPayload;
58
+ /** Validate optional expected text, then use the same source-span compiler. */
59
+ export declare function memoEdits(source: MemoSource, edits: MemoEdit[], mode?: "merge" | "exact"): MemoPatch;
60
+ export type { MemoSource as NoteSource };
61
+ export type { MemoEdit as NoteEdit };
62
+ export type { MemoPatch as NotePatch };
63
+ export { memoEdits as noteEdits };
@@ -0,0 +1,206 @@
1
+ /** Generated reading-view annotations are never saved as source. What is
2
+ * forbidden is NEW text that carries the generated header line
3
+ * (`<!-- notes-reading-view revision=… hash=…`). Deleting such a line, keeping
4
+ * one the note already has, or mentioning the marker in prose or a code block
5
+ * is allowed. Checked on raw inserted text before compilation (escaping would
6
+ * hide it) and on the insertion payloads of a caller-supplied patch. */
7
+ export const READING_VIEW_MARKER = "<!-- notes-reading-view";
8
+ export const READING_VIEW_HEADER = /^\s*<!-- notes-reading-view revision=/m;
9
+ export class ReadingViewMarkerRefused extends Error {
10
+ constructor(what) {
11
+ super(`${what} adds a generated reading-view marker header ("${READING_VIEW_MARKER} revision=…"); generated annotations are never saved as source`);
12
+ this.name = "ReadingViewMarkerRefused";
13
+ }
14
+ }
15
+ /** Inserted or replacement text only, never deletions or unchanged context. */
16
+ export function refuseReadingView(text, what) {
17
+ if (READING_VIEW_HEADER.test(text))
18
+ throw new ReadingViewMarkerRefused(what);
19
+ }
20
+ /** Whole-body replacement: only lines that are new relative to the baseline count, so a header the note already holds survives a write. */
21
+ export function refuseNewReadingViewLines(baseline, content, what) {
22
+ const kept = new Set(baseline.split("\n"));
23
+ for (const line of content.split("\n"))
24
+ if (READING_VIEW_HEADER.test(line) && !kept.has(line))
25
+ throw new ReadingViewMarkerRefused(what);
26
+ }
27
+ /** inline-dff escapes only (`\\ \n \r \t \[ \] \{ \} \+ \-`); anything else keeps its backslash. Never applied to unified-diff lines. */
28
+ const INLINE_ESCAPES = { "\\": "\\", n: "\n", r: "\r", t: "\t", "[": "[", "]": "]", "{": "{", "}": "}", "+": "+", "-": "-" };
29
+ const decodeInline = (text) => text.replace(/\\(.)/g, (whole, char) => INLINE_ESCAPES[char] ?? whole);
30
+ /** Apply the inline-dff subset this client emits (`@@ chars s:e @@` + one `~` line of `[-old-]`/`{+new+}`
31
+ * segments) to a baseline. null when the patch is outside that subset or its old text does not match. */
32
+ export function applyInlineDff(baseline, patch) {
33
+ const points = Array.from(baseline), lines = patch.split("\n");
34
+ let out = "", cursor = 0;
35
+ for (let i = 0; i < lines.length;) {
36
+ if (lines[i] === "") {
37
+ i++;
38
+ continue;
39
+ }
40
+ const header = /^@@ chars (\d+):(\d+) @@$/.exec(lines[i]), body = lines[i + 1];
41
+ if (!header || body === undefined || !body.startsWith("~ "))
42
+ return null;
43
+ const start = Number(header[1]), end = Number(header[2]);
44
+ if (start < cursor || end < start || end > points.length)
45
+ return null;
46
+ let rest = body.slice(2), old = "", added = "";
47
+ while (rest) {
48
+ const segment = /^(?:\[-((?:\\.|[^\\])*?)-\]|\{\+((?:\\.|[^\\])*?)\+\})/.exec(rest);
49
+ if (!segment)
50
+ return null;
51
+ if (segment[1] !== undefined)
52
+ old += decodeInline(segment[1]);
53
+ else
54
+ added += decodeInline(segment[2]);
55
+ rest = rest.slice(segment[0].length);
56
+ }
57
+ if (points.slice(start, end).join("") !== old)
58
+ return null;
59
+ out += points.slice(cursor, start).join("") + added;
60
+ cursor = end;
61
+ i += 2;
62
+ }
63
+ return out + points.slice(cursor).join("");
64
+ }
65
+ /** Apply a unified diff to a baseline, verifying context and removed lines. null when it does not apply cleanly. */
66
+ export function applyUnifiedDiff(baseline, patch) {
67
+ const src = baseline.split("\n"), lines = patch.split("\n"), out = [];
68
+ let at = 0, i = 0;
69
+ while (i < lines.length && (lines[i].startsWith("--- ") || lines[i].startsWith("+++ ")))
70
+ i++;
71
+ while (i < lines.length) {
72
+ if (lines[i] === "" && i === lines.length - 1)
73
+ break;
74
+ const hunk = /^@@ -(\d+)(?:,(\d+))? \+\d+(?:,\d+)? @@/.exec(lines[i]);
75
+ if (!hunk)
76
+ return null;
77
+ const index = hunk[2] === "0" ? Number(hunk[1]) : Number(hunk[1]) - 1;
78
+ if (index < at || index > src.length)
79
+ return null;
80
+ out.push(...src.slice(at, index));
81
+ at = index;
82
+ i++;
83
+ while (i < lines.length && !lines[i].startsWith("@@")) {
84
+ const line = lines[i++];
85
+ if (line.startsWith("\\"))
86
+ continue;
87
+ if (line === "") {
88
+ if (i === lines.length)
89
+ break;
90
+ return null;
91
+ }
92
+ const tag = line[0], text = line.slice(1);
93
+ if (tag === " " || tag === "-") {
94
+ if (src[at] !== text)
95
+ return null;
96
+ if (tag === " ")
97
+ out.push(text);
98
+ at++;
99
+ }
100
+ else if (tag === "+")
101
+ out.push(text);
102
+ else
103
+ return null;
104
+ }
105
+ }
106
+ out.push(...src.slice(at));
107
+ return out.join("\n");
108
+ }
109
+ /** Insertion payloads of an inline-dff patch, hunk by hunk with their original spans; null outside the parseable subset. */
110
+ function inlineInsertions(patch) {
111
+ const lines = patch.split("\n"), hunks = [];
112
+ for (let i = 0; i < lines.length;) {
113
+ if (lines[i] === "") {
114
+ i++;
115
+ continue;
116
+ }
117
+ const header = /^@@ chars (\d+):(\d+) @@$/.exec(lines[i]), body = lines[i + 1];
118
+ if (!header || body === undefined || !body.startsWith("~ "))
119
+ return null;
120
+ let rest = body.slice(2), added = "";
121
+ while (rest) {
122
+ const segment = /^(?:\[-((?:\\.|[^\\])*?)-\]|\{\+((?:\\.|[^\\])*?)\+\})/.exec(rest);
123
+ if (!segment)
124
+ return null;
125
+ if (segment[2] !== undefined)
126
+ added += decodeInline(segment[2]);
127
+ rest = rest.slice(segment[0].length);
128
+ }
129
+ hunks.push({ start: Number(header[1]), end: Number(header[2]), added });
130
+ i += 2;
131
+ }
132
+ return hunks;
133
+ }
134
+ /** A caller-supplied patch. With the baseline it was written against, the target is reconstructed and
135
+ * checked like a whole-body write (so a header split across hunks is seen assembled, and one the note
136
+ * already has may be re-added). Without a baseline, or when the patch does not apply to it, the check is
137
+ * BEST-EFFORT on the insertion payloads: inline-dff `{+…+}` (escapes decoded), concatenated across hunks
138
+ * that are adjacent in the original (one's end offset is the next's start, so no baseline text lies
139
+ * between them), or unified-diff `+` lines. Authoritative enforcement belongs to the server. */
140
+ export function refuseReadingViewPatch(patch, format, baseline) {
141
+ if (baseline !== undefined) {
142
+ const target = format === "diff" ? applyUnifiedDiff(baseline, patch) : applyInlineDff(baseline, patch);
143
+ if (target !== null) {
144
+ refuseNewReadingViewLines(baseline, target, "the patch");
145
+ return;
146
+ }
147
+ }
148
+ if (format === "diff") {
149
+ for (const line of patch.split("\n"))
150
+ if (line.startsWith("+") && !line.startsWith("+++ "))
151
+ refuseReadingView(line.slice(1), "the patch");
152
+ return;
153
+ }
154
+ const hunks = inlineInsertions(patch);
155
+ if (hunks === null) {
156
+ for (const match of patch.matchAll(/\{\+((?:\\[\s\S]|[^\\])*?)\+\}/g))
157
+ refuseReadingView(decodeInline(match[1]), "the patch");
158
+ return;
159
+ }
160
+ let run = "", previousEnd = -1;
161
+ for (const hunk of hunks) {
162
+ run = hunk.start === previousEnd ? run + hunk.added : hunk.added;
163
+ previousEnd = hunk.end;
164
+ refuseReadingView(run, "the patch");
165
+ }
166
+ }
167
+ function inlineLiteral(text) {
168
+ return text.replace(/[\\\n\r\t\[\]{}+\-]/g, char => char === "\n" ? "\\n" : char === "\r" ? "\\r" : char === "\t" ? "\\t" : `\\${char}`);
169
+ }
170
+ /** Exact original-source spans, with untouched gaps omitted from the PATCH. The target the edits
171
+ * produce is reconstructed and checked against the baseline, so a generated header assembled from
172
+ * several spans is refused exactly as a whole-body write would be. */
173
+ export function documentEdits(source, edits, exact = false) {
174
+ const points = Array.from(source.content);
175
+ let lastEnd = 0, lastStart = -1, target = "";
176
+ const hunks = [];
177
+ for (const edit of edits) {
178
+ if (!Number.isSafeInteger(edit.start) || !Number.isSafeInteger(edit.end) || edit.start < lastEnd
179
+ || edit.start <= lastStart || edit.end < edit.start || edit.end > points.length) {
180
+ throw new Error("Edit spans must be ordered and disjoint within the original source");
181
+ }
182
+ target += points.slice(lastEnd, edit.start).join("") + edit.text;
183
+ lastStart = edit.start;
184
+ lastEnd = edit.end;
185
+ const old = points.slice(edit.start, edit.end).join("");
186
+ if (old === edit.text)
187
+ continue;
188
+ hunks.push(`@@ chars ${edit.start}:${edit.end} @@\n~ ${old ? `[-${inlineLiteral(old)}-]` : ""}${edit.text ? `{+${inlineLiteral(edit.text)}+}` : ""}\n`);
189
+ }
190
+ refuseNewReadingViewLines(source.content, target + points.slice(lastEnd).join(""), "the edit");
191
+ return { format: "inline-dff", patch: hunks.join(""), baseRevision: source.revision, mode: exact ? "exact" : "merge" };
192
+ }
193
+ /** One literal insertion at an original-source Unicode code-point boundary. */
194
+ export function documentInsertion(source, at, text, exact = false) {
195
+ return documentEdits(source, [{ start: at, end: at, text }], exact);
196
+ }
197
+ /** Validate optional expected text, then use the same source-span compiler. */
198
+ export function memoEdits(source, edits, mode = "exact") {
199
+ const points = Array.from(source.content);
200
+ for (const edit of edits) {
201
+ if (edit.expected !== undefined && edit.expected !== points.slice(edit.start, edit.end).join(""))
202
+ throw new Error("Edit does not match the original source");
203
+ }
204
+ return documentEdits(source, edits, mode === "exact");
205
+ }
206
+ export { memoEdits as noteEdits };
@@ -0,0 +1,44 @@
1
+ export type MemoEventRequest = (url: string, options: {
2
+ token?: string | null;
3
+ headers?: Record<string, string>;
4
+ signal?: AbortSignal;
5
+ followRedirects?: boolean;
6
+ }) => Promise<Response>;
7
+ export type Selection = null | {
8
+ status: 'unresolved';
9
+ } | {
10
+ status: 'resolved';
11
+ anchor: number;
12
+ head: number;
13
+ start: number;
14
+ end: number;
15
+ unit: 'unicode-code-point';
16
+ text: string;
17
+ truncated: boolean;
18
+ };
19
+ export interface Participant {
20
+ client: string;
21
+ user: {
22
+ id: string;
23
+ name: string;
24
+ kind: 'human' | 'agent';
25
+ owner?: {
26
+ id: string;
27
+ name: string;
28
+ };
29
+ };
30
+ selection?: Selection;
31
+ }
32
+ export interface MemoEvent {
33
+ type: 'observation';
34
+ note: string;
35
+ hash: string;
36
+ observedAt: number;
37
+ participants: Participant[];
38
+ activityVersion: string;
39
+ }
40
+ /** Explicit-token adapter: no login files, telemetry or process state. */
41
+ export declare function readMemoEvents(url: string, token: string, note: string, signal: AbortSignal, receive: (event: MemoEvent) => void, request?: MemoEventRequest): Promise<void>;
42
+ export type { MemoEventRequest as NoteEventRequest };
43
+ export type { MemoEvent as NoteEvent };
44
+ export { readMemoEvents as readNoteEvents };
@@ -0,0 +1,65 @@
1
+ /** Notes SSE parser over an injected request; the CLI supplies its own `requestRaw`. */
2
+ import { httpResponse, HttpError } from "./http.js";
3
+ /** Fetch SSE with auth headers, abort/idle bounds, and no automatic replay across gaps. */
4
+ async function readEvents(url, token, note, signal, receive, request) {
5
+ const idle = new AbortController();
6
+ let timer;
7
+ const touch = () => { clearTimeout(timer); timer = setTimeout(() => idle.abort(new Error('Memos event stream timed out')), 45000); };
8
+ touch();
9
+ let reader;
10
+ try {
11
+ const res = await request(url, { token, headers: { Accept: 'text/event-stream' },
12
+ signal: AbortSignal.any([signal, idle.signal]), followRedirects: false });
13
+ if (!/^text\/event-stream(?:;|$)/i.test(res.headers.get('content-type') ?? '') || !res.body)
14
+ throw new Error('server does not support Memos events; upgrade the server');
15
+ reader = res.body.getReader();
16
+ const decoder = new TextDecoder('utf-8', { fatal: true });
17
+ let buffer = '', data = [], frameSize = 0;
18
+ while (!signal.aborted) {
19
+ const { done, value } = await reader.read();
20
+ if (done)
21
+ throw new Error('Memos event stream disconnected; run --linger again for a fresh snapshot');
22
+ touch();
23
+ buffer += decoder.decode(value, { stream: true });
24
+ if (buffer.length > 4_000_000)
25
+ throw new Error('Memos event frame exceeds limit');
26
+ let newline;
27
+ while ((newline = buffer.indexOf('\n')) >= 0) {
28
+ const line = buffer.slice(0, newline).replace(/\r$/, '');
29
+ buffer = buffer.slice(newline + 1);
30
+ if (line.startsWith('data:')) {
31
+ const part = line.slice(5).replace(/^ /, '');
32
+ frameSize += part.length;
33
+ if (frameSize > 4_000_000)
34
+ throw new Error('Memos event frame exceeds limit');
35
+ data.push(part);
36
+ }
37
+ else if (!line && data.length) {
38
+ const event = JSON.parse(data.join('\n'));
39
+ data = [];
40
+ frameSize = 0;
41
+ if (event.type === 'error')
42
+ throw new Error(`Memos event stream ended (${String(event.error)})`);
43
+ if (event.type !== 'observation' || event.note !== note || !/^sha256:[a-f0-9]{64}$/.test(event.hash) ||
44
+ !Array.isArray(event.participants) || typeof event.activityVersion !== 'string')
45
+ throw new Error('invalid Memos event response');
46
+ receive(event);
47
+ }
48
+ }
49
+ }
50
+ }
51
+ finally {
52
+ clearTimeout(timer);
53
+ await reader?.cancel().catch(() => { });
54
+ }
55
+ }
56
+ /** Explicit-token adapter: no login files, telemetry or process state. */
57
+ export function readMemoEvents(url, token, note, signal, receive, request) {
58
+ return readEvents(url, token, note, signal, receive, request ?? (async (url, options = {}) => {
59
+ const response = await httpResponse(url, { token: options.token, headers: options.headers, signal: options.signal, redirect: "manual" });
60
+ if (!response.ok)
61
+ throw new HttpError(response.status, await response.text());
62
+ return response;
63
+ }));
64
+ }
65
+ export { readMemoEvents as readNoteEvents };