@x-tech-laboratories/x-app 0.117.0 → 0.119.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 (63) hide show
  1. package/README.md +21 -8
  2. package/dist/agent/claude_stream.d.ts +22 -0
  3. package/dist/agent/claude_stream.js +85 -0
  4. package/dist/agent/git.d.ts +37 -0
  5. package/dist/agent/git.js +121 -0
  6. package/dist/agent/github.d.ts +81 -0
  7. package/dist/agent/github.js +185 -0
  8. package/dist/agent/programs.d.ts +17 -0
  9. package/dist/agent/programs.js +62 -0
  10. package/dist/agent/worktree_files.d.ts +20 -0
  11. package/dist/agent/worktree_files.js +69 -0
  12. package/dist/agent_transcript.d.ts +83 -0
  13. package/dist/agent_transcript.js +267 -0
  14. package/dist/app/daemon.js +2 -2
  15. package/dist/app/define.d.ts +7 -2
  16. package/dist/app/define.js +13 -0
  17. package/dist/app/desktop.js +2 -2
  18. package/dist/app/protocol.d.ts +3 -1
  19. package/dist/app/protocol.js +1 -0
  20. package/dist/app/server.d.ts +1 -0
  21. package/dist/app/server.js +13 -2
  22. package/dist/app/web/android.js +8 -1
  23. package/dist/app/web/hosts.d.ts +1 -1
  24. package/dist/app/web/hosts.js +5 -4
  25. package/dist/connection_protocol.d.ts +1 -0
  26. package/dist/connection_protocol.js +11 -0
  27. package/dist/daemon_router.d.ts +2 -2
  28. package/dist/daemon_router.js +3 -2
  29. package/dist/diff_rows.d.ts +78 -0
  30. package/dist/diff_rows.js +158 -0
  31. package/dist/frontmatter.d.ts +10 -0
  32. package/dist/frontmatter.js +57 -0
  33. package/dist/frontmatter_folder.d.ts +24 -0
  34. package/dist/frontmatter_folder.js +54 -0
  35. package/dist/history.js +14 -1
  36. package/dist/server/config.d.ts +2 -0
  37. package/dist/server/config.js +15 -1
  38. package/dist/server/events.d.ts +2 -0
  39. package/dist/server/events.js +3 -1
  40. package/dist/server/github_webhook.d.ts +30 -0
  41. package/dist/server/github_webhook.js +146 -0
  42. package/dist/sync_engine.d.ts +5 -0
  43. package/dist/sync_engine.js +63 -9
  44. package/dist/sync_protocol.d.ts +4 -0
  45. package/dist/sync_protocol.js +11 -0
  46. package/dist/test_helpers.d.ts +7 -0
  47. package/dist/test_helpers.js +44 -0
  48. package/dist/web/react/accounts.js +2 -1
  49. package/dist/web/react/controls.d.ts +3 -2
  50. package/dist/web/react/controls.js +6 -4
  51. package/dist/web/react/copy_button.d.ts +11 -0
  52. package/dist/web/react/copy_button.js +44 -0
  53. package/dist/web/react/dialog.js +2 -2
  54. package/dist/web/react/diff.d.ts +26 -0
  55. package/dist/web/react/diff.js +79 -0
  56. package/dist/web/react/file_tree.d.ts +26 -0
  57. package/dist/web/react/file_tree.js +46 -0
  58. package/dist/web/react/local_mcp_settings.js +2 -1
  59. package/dist/web/react/transcript.d.ts +24 -0
  60. package/dist/web/react/transcript.js +100 -0
  61. package/dist/web/tree.d.ts +3 -1
  62. package/dist/web/tree.js +38 -10
  63. package/package.json +5 -3
@@ -0,0 +1,267 @@
1
+ // An agent session's transcript as steps a page draws: thinking, the agent's text, each tool call with its input and
2
+ // result, permission requests and the user's own messages, folded from Claude Code's stream-json lines as they arrive,
3
+ // partial blocks merged as they end, and kept within a size so a finished session replays the same view. It runs alike
4
+ // in a daemon, a page and a phone, so the daemon can store it and every UI can draw it.
5
+ // The most of a transcript kept, as JSON characters; the earliest steps go first.
6
+ export const transcript_limit = 400_000;
7
+ // The most of one tool's result or one input field kept.
8
+ export const tool_text_limit = 8_000;
9
+ export function empty_transcript() {
10
+ return { entries: [], model: "", mode: "", activity: "working", dropped: 0 };
11
+ }
12
+ // A stored transcript, or an empty one when there is none or it is damaged.
13
+ export function read_transcript(text) {
14
+ if (!text)
15
+ return empty_transcript();
16
+ try {
17
+ const saved = JSON.parse(text);
18
+ return { ...empty_transcript(), ...saved, entries: Array.isArray(saved.entries) ? saved.entries : [] };
19
+ }
20
+ catch {
21
+ return empty_transcript();
22
+ }
23
+ }
24
+ function cut(text, limit = tool_text_limit) {
25
+ return text.length > limit ? `${text.slice(0, limit)}\n… ${text.length - limit} more characters left out` : text;
26
+ }
27
+ // Long text fields of a tool's input cut to a length, so a large write does not fill the transcript.
28
+ function cut_input(input) {
29
+ if (!input || typeof input !== "object" || Array.isArray(input))
30
+ return {};
31
+ return Object.fromEntries(Object.entries(input).map(([key, value]) => [key, typeof value === "string" ? cut(value) : value]));
32
+ }
33
+ function result_text(content) {
34
+ if (typeof content === "string")
35
+ return content;
36
+ if (Array.isArray(content))
37
+ return content.map((part) => (part && typeof part === "object" && part.type === "text" ? String(part.text ?? "") : "")).join("\n");
38
+ return "";
39
+ }
40
+ // The earliest entry of a message and kind whose whole message has not arrived yet, matched by tool id for tools.
41
+ function open_entry(transcript, message_id, kind, tool_id) {
42
+ return transcript.entries.find((entry) => entry.kind === kind && entry.message_id === message_id && (kind !== "tool" || entry.id === tool_id));
43
+ }
44
+ function streaming_entry(transcript, message_id) {
45
+ for (let index = transcript.entries.length - 1; index >= 0; index -= 1) {
46
+ const entry = transcript.entries[index];
47
+ if ((entry.kind === "thinking" || entry.kind === "text" || entry.kind === "tool") && entry.message_id === message_id && entry.streaming)
48
+ return entry;
49
+ }
50
+ return undefined;
51
+ }
52
+ function read_partial(transcript, message) {
53
+ const event = (message.event ?? {});
54
+ const message_id = String(message.api_message_id ?? "");
55
+ if (event.type === "content_block_start") {
56
+ const block = (event.content_block ?? {});
57
+ if (block.type === "thinking")
58
+ transcript.entries.push({ kind: "thinking", text: String(block.thinking ?? ""), message_id, streaming: true });
59
+ else if (block.type === "text")
60
+ transcript.entries.push({ kind: "text", text: String(block.text ?? ""), message_id, streaming: true });
61
+ else if (block.type === "tool_use")
62
+ transcript.entries.push({ kind: "tool", id: String(block.id ?? ""), name: String(block.name ?? ""), input: {}, input_text: "", message_id, streaming: true });
63
+ else
64
+ return false;
65
+ return true;
66
+ }
67
+ if (event.type === "content_block_delta") {
68
+ const entry = streaming_entry(transcript, message_id);
69
+ const delta = (event.delta ?? {});
70
+ if (!entry)
71
+ return false;
72
+ if (entry.kind === "thinking" && delta.type === "thinking_delta")
73
+ entry.text += String(delta.thinking ?? "");
74
+ else if (entry.kind === "text" && delta.type === "text_delta")
75
+ entry.text += String(delta.text ?? "");
76
+ else if (entry.kind === "tool" && delta.type === "input_json_delta")
77
+ entry.input_text = `${entry.input_text ?? ""}${String(delta.partial_json ?? "")}`;
78
+ else
79
+ return false;
80
+ return true;
81
+ }
82
+ if (event.type === "content_block_stop") {
83
+ const entry = streaming_entry(transcript, message_id);
84
+ if (!entry)
85
+ return false;
86
+ entry.streaming = false;
87
+ if (entry.kind === "tool" && entry.input_text) {
88
+ try {
89
+ entry.input = cut_input(JSON.parse(entry.input_text));
90
+ }
91
+ catch {
92
+ // The whole message brings the input again.
93
+ }
94
+ }
95
+ if (entry.kind === "tool")
96
+ delete entry.input_text;
97
+ return true;
98
+ }
99
+ return false;
100
+ }
101
+ function read_assistant(transcript, message) {
102
+ const body = (message.message ?? {});
103
+ const message_id = String(body.id ?? "");
104
+ const content = Array.isArray(body.content) ? body.content : [];
105
+ let changed = false;
106
+ for (const part of content) {
107
+ if (part.type !== "thinking" && part.type !== "text" && part.type !== "tool_use")
108
+ continue;
109
+ const kind = part.type === "tool_use" ? "tool" : part.type;
110
+ const found = message_id ? open_entry(transcript, message_id, kind, String(part.id ?? "")) : undefined;
111
+ const text = String((part.type === "thinking" ? part.thinking : part.text) ?? "");
112
+ if (found?.kind === "tool") {
113
+ found.input = cut_input(part.input);
114
+ delete found.input_text;
115
+ }
116
+ else if (found && (found.kind === "text" || found.kind === "thinking")) {
117
+ if (text)
118
+ found.text = text;
119
+ }
120
+ else if (kind === "tool") {
121
+ transcript.entries.push({ kind: "tool", id: String(part.id ?? ""), name: String(part.name ?? ""), input: cut_input(part.input), streaming: false });
122
+ }
123
+ else {
124
+ transcript.entries.push({ kind, text, streaming: false });
125
+ }
126
+ // A whole block is no longer matched by its message.
127
+ if (found) {
128
+ found.streaming = false;
129
+ delete found.message_id;
130
+ }
131
+ changed = true;
132
+ }
133
+ return changed;
134
+ }
135
+ function read_user(transcript, message) {
136
+ const body = (message.message ?? {});
137
+ const content = typeof body.content === "string" ? [{ type: "text", text: body.content }] : Array.isArray(body.content) ? body.content : [];
138
+ let changed = false;
139
+ for (const part of content) {
140
+ if (part.type === "tool_result") {
141
+ const tool = transcript.entries.find((entry) => entry.kind === "tool" && entry.id === part.tool_use_id);
142
+ if (!tool)
143
+ continue;
144
+ tool.result = cut(result_text(part.content));
145
+ tool.is_error = part.is_error === true;
146
+ changed = true;
147
+ }
148
+ }
149
+ const text = content.filter((part) => part.type === "text").map((part) => String(part.text ?? "")).join("\n");
150
+ if (!text)
151
+ return changed;
152
+ const own = transcript.entries.find((entry) => entry.kind === "user" && entry.id === message.uuid);
153
+ if (own) {
154
+ if (own.pending) {
155
+ own.pending = false;
156
+ return true;
157
+ }
158
+ return changed;
159
+ }
160
+ if (text.startsWith("[Request interrupted")) {
161
+ transcript.entries.push({ kind: "notice", tone: "warn", text: "Interrupted. The session waits for your message." });
162
+ return true;
163
+ }
164
+ return changed;
165
+ }
166
+ function read_result(transcript, message) {
167
+ const reason = String(message.terminal_reason ?? "");
168
+ if (message.is_error !== true || reason.startsWith("aborted"))
169
+ return false;
170
+ transcript.entries.push({ kind: "notice", tone: "bad", text: String(message.result ?? "") || `The turn ended with an error (${String(message.subtype ?? "unknown")}).` });
171
+ return true;
172
+ }
173
+ // Folds one parsed stream-json line into the transcript and says whether anything shown changed.
174
+ export function fold_stream_message(transcript, message) {
175
+ if (message.type === "stream_event")
176
+ return read_partial(transcript, message);
177
+ if (message.type === "assistant")
178
+ return read_assistant(transcript, message);
179
+ if (message.type === "user")
180
+ return read_user(transcript, message);
181
+ if (message.type === "result")
182
+ return read_result(transcript, message);
183
+ if (message.type === "system" && message.subtype === "init") {
184
+ transcript.model = String(message.model ?? transcript.model);
185
+ transcript.mode = String(message.permissionMode ?? transcript.mode);
186
+ return true;
187
+ }
188
+ return false;
189
+ }
190
+ // ── Writing ──────────────────────────────────────────────────────────────────
191
+ // A permission request added to wait for the user, its long input fields cut.
192
+ export function add_permission(transcript, request) {
193
+ const entry = { kind: "permission", id: request.id, tool: request.tool, input: cut_input(request.input), reason: request.reason };
194
+ transcript.entries.push(entry);
195
+ return entry;
196
+ }
197
+ // A message from the user added as pending until the stream echoes it.
198
+ export function add_user_message(transcript, role, id, text, at) {
199
+ transcript.entries.push({ kind: "user", role, id, text: cut(text, transcript_limit / 4), pending: true, ...(at ? { at } : {}) });
200
+ }
201
+ // The transcript as it is stored: streaming blocks marked whole, the earliest steps dropped past the size limit.
202
+ export function stored_transcript(transcript, limit = transcript_limit) {
203
+ let text = JSON.stringify(transcript);
204
+ while (text.length > limit && transcript.entries.length > 1) {
205
+ // The starting input stays first, so the reader still sees what the session was given.
206
+ const first_kept = transcript.entries[0]?.kind === "user" && transcript.entries[0].role === "input" ? 1 : 0;
207
+ const drop = Math.max(1, Math.ceil((transcript.entries.length - first_kept) / 10));
208
+ transcript.entries.splice(first_kept, drop);
209
+ transcript.dropped += drop;
210
+ text = JSON.stringify(transcript);
211
+ }
212
+ return text;
213
+ }
214
+ // Every block still streaming marked whole and every waiting request denied, as when the session ended part-way.
215
+ export function close_open_blocks(transcript) {
216
+ for (const entry of transcript.entries) {
217
+ if ((entry.kind === "thinking" || entry.kind === "text" || entry.kind === "tool") && entry.streaming)
218
+ entry.streaming = false;
219
+ if (entry.kind === "permission" && !entry.decision)
220
+ Object.assign(entry, { decision: "denied", message: "The session ended first." });
221
+ }
222
+ }
223
+ // ── Describing ───────────────────────────────────────────────────────────────
224
+ const summary_limit = 160;
225
+ // One line saying what a tool call works on: its file, command, pattern, address or description.
226
+ export function tool_summary(input) {
227
+ const main = input.file_path ?? input.notebook_path ?? input.command ?? input.pattern ?? input.url ?? input.query ?? input.description ?? input.prompt ?? "";
228
+ const text = String(typeof main === "string" ? main : JSON.stringify(main)).replace(/\s+/g, " ").trim();
229
+ return text.length > summary_limit ? `${text.slice(0, summary_limit)}…` : text;
230
+ }
231
+ // The change a file-editing tool call makes, read from its input, or null for any other tool.
232
+ export function edit_of(name, input) {
233
+ const path = String(input.file_path ?? input.notebook_path ?? "");
234
+ if (name === "Edit")
235
+ return [{ path, before: String(input.old_string ?? ""), after: String(input.new_string ?? "") }];
236
+ if (name === "Write")
237
+ return [{ path, before: "", after: String(input.content ?? "") }];
238
+ if (name === "MultiEdit" && Array.isArray(input.edits)) {
239
+ return input.edits.map((edit) => ({ path, before: String(edit.old_string ?? ""), after: String(edit.new_string ?? "") }));
240
+ }
241
+ return null;
242
+ }
243
+ // The entries a user message is waiting on, so the page can show it as not yet read.
244
+ export function pending_messages(transcript) {
245
+ return transcript.entries.filter((entry) => entry.kind === "user" && entry.pending === true);
246
+ }
247
+ // The permission requests still waiting on the user.
248
+ export function waiting_permissions(transcript) {
249
+ return transcript.entries.filter((entry) => entry.kind === "permission" && !entry.decision);
250
+ }
251
+ // ── Allowing for the session ─────────────────────────────────────────────────
252
+ // What allowing a tool for the rest of the session covers: the tool, and for a command the program it starts.
253
+ export function permission_rule(tool, input) {
254
+ if (tool !== "Bash" || typeof input.command !== "string")
255
+ return tool;
256
+ const program = input.command.trim().split(/\s+/)[0] ?? "";
257
+ return program ? `Bash(${program})` : tool;
258
+ }
259
+ // Whether a rule covers a tool call, such as Bash(git) covering every git command.
260
+ export function rule_covers(rule, tool, input) {
261
+ return rule === tool || rule === permission_rule(tool, input);
262
+ }
263
+ // The rule in words for the button that sets it, such as "git commands".
264
+ export function rule_words(rule) {
265
+ const program = /^Bash\((.+)\)$/.exec(rule)?.[1];
266
+ return program ? `${program} commands` : rule;
267
+ }
@@ -17,7 +17,7 @@ import { create_sync_peer, sync_device_methods } from "../sync_peer.js";
17
17
  import { keep_listening_for_peers } from "../sync_peer_listening.js";
18
18
  import { declared_file_of, no_services, side_databases } from "./services.js";
19
19
  import { read_desktop_settings } from "./protocol.js";
20
- import { call_body_limit, open_app_database, register_app_calls, watch_outside_changes } from "./server.js";
20
+ import { call_body_limit, data_change_text, open_app_database, register_app_calls, watch_outside_changes } from "./server.js";
21
21
  // ── Daemon ───────────────────────────────────────────────────────────────────
22
22
  // Answers on the socket and starts sync; null when another daemon on the same database already answers.
23
23
  export async function start_daemon(app, options) {
@@ -28,7 +28,7 @@ export async function start_daemon(app, options) {
28
28
  // Backed up before an upgrade, before another copy replaces it, and daily, beside the database.
29
29
  const opened = open_app_database(app, database_path, join(profile, "backups"));
30
30
  // Every window's event stream, told when the data changed or sync's status moved.
31
- const changes = new EventStream();
31
+ const changes = new EventStream({ change_data: data_change_text(app, opened.database) });
32
32
  // Server certificates the user trusted, such as a home server's self-signed one, handed to Node's own fetch.
33
33
  const certificates = create_certificate_trust({ file: join(profile, "trusted-certificates.json"), use_in_node: true });
34
34
  // Off until "Let other devices connect" is turned on; then other devices reach this computer's sync on its port.
@@ -12,7 +12,7 @@ import { type SettingSpec, type SettingsMethods, type SettingsStore, type Settin
12
12
  import { type MigrationStep, type SqlDatabase } from "../sql.ts";
13
13
  import { type SyncedTable } from "../sync_engine.ts";
14
14
  import { type PeerListeningMethods, type SyncDeviceMethods, type SyncStatus } from "../sync_protocol.ts";
15
- import { type DesktopMethods, type DesktopSettingSpec } from "./protocol.ts";
15
+ import { type DataChange, type DesktopMethods, type DesktopSettingSpec } from "./protocol.ts";
16
16
  export type StoreMethod = (database: SqlDatabase, ...args: never[]) => unknown;
17
17
  export type KeptHistory = Pick<History<unknown>, "synced_table" | "install" | "forget_touched" | "record" | "prune"> & Partial<Pick<History<unknown>, "drop_triggers">>;
18
18
  export interface Feature<Methods extends Record<string, StoreMethod> = Record<string, StoreMethod>> {
@@ -79,7 +79,7 @@ export interface Restorable extends Backups {
79
79
  restore: (name: string) => Promise<void>;
80
80
  }
81
81
  export type AppEvents = {
82
- changed: null;
82
+ changed: DataChange | null;
83
83
  sync_status: SyncStatus;
84
84
  app_event: {
85
85
  event: string;
@@ -148,10 +148,15 @@ export declare function define_app<const Features extends readonly Feature[]>(op
148
148
  has_unsent_changes: (database: SqlDatabase, device: string) => boolean;
149
149
  count_unsent_rows: (database: SqlDatabase, devices: readonly string[]) => number;
150
150
  collect_changes_since: (database: SqlDatabase, cursor: number, except_site: string) => import("../sync_engine.ts").CollectedChanges;
151
+ changed_tables_since: (database: SqlDatabase, cursor: number) => {
152
+ tables: string[];
153
+ up_to_seq: number;
154
+ };
151
155
  collect_snapshot: (database: SqlDatabase) => import("../sync_engine.ts").CollectedChanges;
152
156
  apply_changes: (database: SqlDatabase, changes: import("../sync_protocol.ts").SyncChange[]) => number;
153
157
  claim_everything: (database: SqlDatabase, theirs: import("../sync_protocol.ts").SyncChange[]) => void;
154
158
  };
159
+ take_data_change: (database: SqlDatabase) => DataChange | null;
155
160
  needs_upgrade: (database: SqlDatabase) => boolean;
156
161
  open_store: (database: SqlDatabase) => void;
157
162
  method_names: ((keyof UnionToIntersection<Features[number]["methods"]> | keyof SettingsMethods) & string)[];
@@ -62,6 +62,18 @@ export function define_app(options) {
62
62
  tables: [...features.flatMap((feature) => feature.tables), ...histories.map((history) => history.synced_table)],
63
63
  after_apply: (database) => histories.forEach((history) => history.forget_touched(database)),
64
64
  });
65
+ // Where each open connection's last announced change reached, so the next one names only the tables written since.
66
+ const announced = new WeakMap();
67
+ // The synced tables changed since the last call for this connection; the first call, and any after a restore, cannot tell.
68
+ function take_data_change(database) {
69
+ const { generation, last_seq } = sync_engine.read_state(database);
70
+ const since = announced.get(database);
71
+ const { tables, up_to_seq } = sync_engine.changed_tables_since(database, since?.seq ?? last_seq);
72
+ announced.set(database, { generation, seq: up_to_seq });
73
+ if (!since || since.generation !== generation || since.seq > last_seq)
74
+ return null;
75
+ return { tables };
76
+ }
65
77
  // ── Opening ──────────────────────────────────────────────────────────
66
78
  // Whether a database already holding data has steps to run, so it can be backed up before its schema is upgraded.
67
79
  function needs_upgrade(database) {
@@ -224,6 +236,7 @@ export function define_app(options) {
224
236
  settings_store,
225
237
  setting_defaults: settings_store.defaults,
226
238
  sync_engine,
239
+ take_data_change,
227
240
  needs_upgrade,
228
241
  open_store,
229
242
  method_names,
@@ -87,8 +87,8 @@ export function run_desktop(app, electron, options) {
87
87
  restart_local: () => ensure_daemon(),
88
88
  local_tables: [...default_local_tables, "peer_listening"],
89
89
  is_read_only: app.is_read_only,
90
- on_change: () => {
91
- app.bridges.events.emit(windows(), "changed", null);
90
+ on_change: (change) => {
91
+ app.bridges.events.emit(windows(), "changed", change);
92
92
  void apply_window_settings().catch(() => undefined);
93
93
  void tray?.refresh();
94
94
  },
@@ -4,6 +4,8 @@ import type { ConnectionClient } from "../connection_protocol.ts";
4
4
  import type { LocalMcpClient } from "../local_mcp_protocol.ts";
5
5
  import type { Client } from "../electron/bridge.ts";
6
6
  import type { PeerListeningClient, SyncDeviceClient } from "../sync_protocol.ts";
7
+ import type { DataChange } from "../sync_protocol.ts";
8
+ export { data_change_of, type DataChange } from "../sync_protocol.ts";
7
9
  export interface DesktopSettings {
8
10
  show_tray_icon: boolean;
9
11
  keep_running_in_background: boolean;
@@ -54,6 +56,6 @@ export interface PageApi<Methods extends Record<keyof Methods, (...args: never[]
54
56
  peer_listening?: PeerListeningClient;
55
57
  desktop?: Client<DesktopMethods>;
56
58
  local_mcp?: LocalMcpClient;
57
- on_change: (listener: () => void) => () => void;
59
+ on_change: (listener: (change: DataChange | null) => void) => () => void;
58
60
  on_event?: (event: string, listener: (data: unknown) => void) => () => void;
59
61
  }
@@ -1,5 +1,6 @@
1
1
  // What the base app's hosts and processes say to each other: the page's api on every host, and the desktop app's own
2
2
  // settings, which its daemon keeps beside the database rather than syncing, since they belong to the computer.
3
+ export { data_change_of } from "../sync_protocol.js";
3
4
  export const desktop_setting_defaults = {
4
5
  show_tray_icon: true,
5
6
  keep_running_in_background: true,
@@ -18,6 +18,7 @@ export interface AppDatabase {
18
18
  close: () => void;
19
19
  }
20
20
  export declare function open_app_database(app: App, path: string, backups_folder: string): AppDatabase;
21
+ export declare function data_change_text(app: Pick<App, "take_data_change">, database: () => SqlDatabase): () => string;
21
22
  export declare function watch_outside_changes(database: () => SqlDatabase, on_change: () => void, every_ms?: number): () => void;
22
23
  export declare const call_body_limit: number;
23
24
  export declare function call_args(app: App, method: string, body: unknown): unknown[];
@@ -89,6 +89,17 @@ function adopt_schedule_file(app, database, file) {
89
89
  app.adopt_backup_schedule(database, saved);
90
90
  rmSync(file, { force: true });
91
91
  }
92
+ // What a change event carries: the tables written since the last one, or nothing when that cannot be told, as mid-restore.
93
+ export function data_change_text(app, database) {
94
+ return () => {
95
+ try {
96
+ return JSON.stringify(app.take_data_change(database()));
97
+ }
98
+ catch {
99
+ return "";
100
+ }
101
+ };
102
+ }
92
103
  // Another program, such as an older build of the app, may write the database file; SQLite moves data_version only
93
104
  // for another connection's commits, so this one's own writes never count. Checked every second; returns how to stop.
94
105
  export function watch_outside_changes(database, on_change, every_ms = 1000) {
@@ -244,8 +255,8 @@ export async function run_server(app, options) {
244
255
  ...(app.token_prefix ? { token_prefix: app.token_prefix } : {}),
245
256
  declared: load_accounts(`${prefix}_PASSWORD_HASH_FILE`, server_config.values.password_hash_file),
246
257
  });
247
- // Every page hears of a change, whether a page or another device made it.
248
- const changes = new EventStream();
258
+ // Every page hears of a change, whether a page or another device made it, and which tables it touched.
259
+ const changes = new EventStream({ change_data: data_change_text(app, opened.database) });
249
260
  // The server is one sync peer among the devices, kept beside the data so a restore leaves its pairings be.
250
261
  const sync = create_sync_peer({
251
262
  app: app.id,
@@ -60,8 +60,15 @@ export async function android_api(app, options = {}) {
60
60
  const services = options.services ? await options.services({ database: () => database.connection }) : {};
61
61
  const listeners = new Set();
62
62
  const announce = () => {
63
+ let change = null;
64
+ try {
65
+ change = app.take_data_change(database.connection);
66
+ }
67
+ catch {
68
+ // A database closed mid-restore cannot tell, so the page fetches everything.
69
+ }
63
70
  for (const listener of listeners)
64
- listener();
71
+ listener(change);
65
72
  services.changed?.();
66
73
  };
67
74
  // The paired devices and keys stay in the app's private storage; before another copy replaces this one the data is
@@ -1,5 +1,5 @@
1
1
  import type { App } from "../define.ts";
2
- import type { PageApi } from "../protocol.ts";
2
+ import { type PageApi } from "../protocol.ts";
3
3
  export declare function web_api(app: Pick<App, "bridges">): PageApi;
4
4
  export declare function is_desktop(app: Pick<App, "bridges">): boolean;
5
5
  export declare function desktop_api(app: Pick<App, "bridges"> & Partial<Pick<App, "local_mcp">>): Promise<PageApi>;
@@ -3,6 +3,7 @@
3
3
  // this computer's daemon or to the server Settings → Connections chose.
4
4
  import { LOCAL_CONNECTION_ID } from "../../connection_protocol.js";
5
5
  import { LoginNeeded } from "../../web/states.js";
6
+ import { data_change_of } from "../protocol.js";
6
7
  // ── Web ──────────────────────────────────────────────────────────────────────
7
8
  // Relative, so a server behind a proxy under a folder keeps it.
8
9
  const call_path = "api/call/";
@@ -31,10 +32,10 @@ function follow_changes(on_change, on_client_id, named) {
31
32
  events.addEventListener("hello", (event) => {
32
33
  on_client_id(event.data);
33
34
  if (has_connected)
34
- on_change();
35
+ on_change(null);
35
36
  has_connected = true;
36
37
  });
37
- events.addEventListener("changed", on_change);
38
+ events.addEventListener("changed", (message) => on_change(data_change_of(message.data)));
38
39
  // The browser retries a dropped stream by itself but gives up on a refused one, so the page tries again later.
39
40
  events.addEventListener("error", () => {
40
41
  if (events.readyState === EventSource.CLOSED)
@@ -66,9 +67,9 @@ export function web_api(app) {
66
67
  }
67
68
  const listeners = new Set();
68
69
  const named = new Map();
69
- const listen_for = follow_changes(() => {
70
+ const listen_for = follow_changes((change) => {
70
71
  for (const listener of listeners)
71
- listener();
72
+ listener(change);
72
73
  }, (given) => (client_id = given), named);
73
74
  const { bridges } = app;
74
75
  return {
@@ -41,3 +41,4 @@ export interface ConnectionMethods {
41
41
  export type ConnectionClient = ConnectionMethods;
42
42
  export declare const connection_method_names: readonly ["list_connections", "add_connection", "rename_connection", "remove_connection", "choose_connection"];
43
43
  export declare function connection_address(typed: string): string;
44
+ export declare function memory_connection_storage(): ConnectionStorage;
@@ -27,3 +27,14 @@ export function connection_address(typed) {
27
27
  throw new Error(`"${text}" is not a daemon's address.`);
28
28
  return `${scheme}://${parts[1].toLowerCase()}${parts[2].replace(/\/+$/, "")}`;
29
29
  }
30
+ // ── Storage in memory ────────────────────────────────────────────────────────
31
+ // The daemons a device knows, kept in memory alone, such as for a test or a page that forgets them on reload.
32
+ export function memory_connection_storage() {
33
+ let saved = null;
34
+ return {
35
+ read: () => saved,
36
+ write: (next) => {
37
+ saved = next;
38
+ },
39
+ };
40
+ }
@@ -1,6 +1,6 @@
1
1
  import type { Connections } from "./connections.ts";
2
2
  import type { DaemonLink } from "./daemon_link.ts";
3
- import type { SyncStatus } from "./sync_protocol.ts";
3
+ import { type DataChange, type SyncStatus } from "./sync_protocol.ts";
4
4
  export interface DaemonRouterOptions {
5
5
  connections: Pick<Connections, "current" | "subscribe">;
6
6
  link: Pick<DaemonLink, "call" | "follow_events">;
@@ -10,7 +10,7 @@ export interface DaemonRouterOptions {
10
10
  is_read_only?: (name: string) => boolean;
11
11
  events_path?: string;
12
12
  api_prefix?: string;
13
- on_change: () => void;
13
+ on_change: (change: DataChange | null) => void;
14
14
  on_sync_status?: (status: SyncStatus) => void;
15
15
  on_event?: (event: string, data: string) => void;
16
16
  on_switch: () => void;
@@ -3,6 +3,7 @@
3
3
  // such as its sync and trusted certificates. It follows the chosen daemon's events, starts this computer's daemon
4
4
  // again when it went away, and tells the app when another daemon was chosen, so the page never knows where its daemon
5
5
  // runs. It holds no Electron, so the app gives it the windows' side as callbacks.
6
+ import { data_change_of } from "./sync_protocol.js";
6
7
  // What stays on this computer in every app: its sync, its trusted certificates, its MCP endpoint and its own settings.
7
8
  export const default_local_tables = ["device", "certificates", "local_mcp", "daemon"];
8
9
  // ── Router ───────────────────────────────────────────────────────────────────
@@ -46,13 +47,13 @@ export function create_daemon_router(options) {
46
47
  if (event === "hello")
47
48
  client_id = data;
48
49
  else if (event === "changed")
49
- options.on_change();
50
+ options.on_change(data_change_of(data));
50
51
  else if (event === "sync_status")
51
52
  options.on_sync_status?.(JSON.parse(data));
52
53
  else
53
54
  options.on_event?.(event, data);
54
55
  }, {
55
- on_connect: options.on_change,
56
+ on_connect: () => options.on_change(null),
56
57
  on_disconnect: () => {
57
58
  if (!is_quitting && is_here() && options.restart_local)
58
59
  void options.restart_local().catch(on_error);
@@ -0,0 +1,78 @@
1
+ export type DiffLineKind = "added" | "removed" | "context";
2
+ export interface DiffLine {
3
+ kind: DiffLineKind;
4
+ text: string;
5
+ old_number: number | null;
6
+ new_number: number | null;
7
+ }
8
+ export interface DiffHunk {
9
+ header: string;
10
+ lines: DiffLine[];
11
+ }
12
+ export type DiffFileStatus = "added" | "removed" | "renamed" | "changed";
13
+ export interface DiffFile {
14
+ path: string;
15
+ old_path: string;
16
+ status: DiffFileStatus;
17
+ additions: number;
18
+ deletions: number;
19
+ hunks: DiffHunk[];
20
+ }
21
+ export interface WordPart {
22
+ text: string;
23
+ changed: boolean;
24
+ }
25
+ export interface DiffCell {
26
+ number: number;
27
+ parts: WordPart[];
28
+ }
29
+ export type SplitRow = {
30
+ kind: "same";
31
+ left: DiffCell;
32
+ right: DiffCell;
33
+ } | {
34
+ kind: "change";
35
+ left: DiffCell | null;
36
+ right: DiffCell | null;
37
+ } | {
38
+ kind: "hunk";
39
+ header: string;
40
+ };
41
+ export type UnifiedRow = {
42
+ kind: "same" | "removed" | "added";
43
+ old_number: number | null;
44
+ new_number: number | null;
45
+ parts: WordPart[];
46
+ } | {
47
+ kind: "hunk";
48
+ header: string;
49
+ };
50
+ export interface DiffFold {
51
+ kind: "fold";
52
+ start: number;
53
+ count: number;
54
+ }
55
+ export interface EditLine {
56
+ kind: DiffLineKind;
57
+ text: string;
58
+ }
59
+ export declare const context_lines = 3;
60
+ export declare function parse_unified_diff(text: string): DiffFile[];
61
+ export declare function diff_totals(files: Pick<DiffFile, "additions" | "deletions">[]): {
62
+ additions: number;
63
+ deletions: number;
64
+ };
65
+ export declare function diff_within_limit(text: string, limit: number): {
66
+ text: string;
67
+ truncated: boolean;
68
+ };
69
+ export declare function word_parts(old_text: string, new_text: string): {
70
+ old: WordPart[];
71
+ new: WordPart[];
72
+ };
73
+ export declare function split_rows(file: DiffFile): SplitRow[];
74
+ export declare function unified_rows(file: DiffFile): UnifiedRow[];
75
+ export declare function fold_rows<Row extends {
76
+ kind: string;
77
+ }>(rows: Row[], opened: ReadonlySet<number>, context?: number): (Row | DiffFold)[];
78
+ export declare function edit_lines(before: string, after: string): EditLine[];