@x-tech-laboratories/x-app 0.116.0 → 0.118.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/README.md +19 -6
- package/dist/agent/claude_stream.d.ts +22 -0
- package/dist/agent/claude_stream.js +85 -0
- package/dist/agent/git.d.ts +37 -0
- package/dist/agent/git.js +121 -0
- package/dist/agent/github.d.ts +81 -0
- package/dist/agent/github.js +185 -0
- package/dist/agent/programs.d.ts +17 -0
- package/dist/agent/programs.js +62 -0
- package/dist/agent/worktree_files.d.ts +20 -0
- package/dist/agent/worktree_files.js +69 -0
- package/dist/agent_transcript.d.ts +83 -0
- package/dist/agent_transcript.js +267 -0
- package/dist/app_android_key.d.ts +3 -0
- package/dist/app_android_key.js +34 -11
- package/dist/connection_protocol.d.ts +1 -0
- package/dist/connection_protocol.js +11 -0
- package/dist/diff_rows.d.ts +78 -0
- package/dist/diff_rows.js +158 -0
- package/dist/frontmatter.d.ts +10 -0
- package/dist/frontmatter.js +57 -0
- package/dist/frontmatter_folder.d.ts +24 -0
- package/dist/frontmatter_folder.js +54 -0
- package/dist/server/config.d.ts +2 -0
- package/dist/server/config.js +15 -1
- package/dist/server/github_webhook.d.ts +30 -0
- package/dist/server/github_webhook.js +146 -0
- package/dist/test_helpers.d.ts +7 -0
- package/dist/test_helpers.js +44 -0
- package/dist/web/react/accounts.js +2 -1
- package/dist/web/react/controls.d.ts +3 -2
- package/dist/web/react/controls.js +6 -4
- package/dist/web/react/copy_button.d.ts +11 -0
- package/dist/web/react/copy_button.js +44 -0
- package/dist/web/react/dialog.js +2 -2
- package/dist/web/react/diff.d.ts +26 -0
- package/dist/web/react/diff.js +79 -0
- package/dist/web/react/file_tree.d.ts +26 -0
- package/dist/web/react/file_tree.js +46 -0
- package/dist/web/react/local_mcp_settings.js +2 -1
- package/dist/web/react/transcript.d.ts +24 -0
- package/dist/web/react/transcript.js +100 -0
- 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
|
+
}
|
|
@@ -5,6 +5,9 @@ export interface KeyPlan {
|
|
|
5
5
|
keystore: string;
|
|
6
6
|
password_file: string;
|
|
7
7
|
alias: string;
|
|
8
|
+
org?: string;
|
|
8
9
|
}
|
|
9
10
|
export declare function key_plan(app_folder: string, folder?: string, home?: string): KeyPlan;
|
|
11
|
+
export declare function org_key_plan(org: string, folder?: string, home?: string): KeyPlan;
|
|
10
12
|
export declare function make_release_key(plan: KeyPlan, run?: RunProgram): void;
|
|
13
|
+
export declare function plan_of_args(args: readonly string[]): KeyPlan;
|
package/dist/app_android_key.js
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
// The `x-app-android-key [app folder]`
|
|
3
|
-
// secrets
|
|
4
|
-
//
|
|
5
|
-
//
|
|
2
|
+
// The `x-app-android-key [app folder] [key folder]` and `x-app-android-key --org <org> [key folder]` commands: an
|
|
3
|
+
// Android release key, made once and stored as the secrets X-App's `app-android` action signs with, without ever
|
|
4
|
+
// printing it. With --org the key is the organization's, one for every app, stored as organization secrets every
|
|
5
|
+
// repository reads; a repository's own secrets of the same names still win. The key lives in ~/.local/share/<name>/signing
|
|
6
|
+
// unless a folder is given, and is never replaced, since Android installs an update only when it carries the same key.
|
|
7
|
+
// Run it where keytool is (the app's Android shell), with gh signed in.
|
|
6
8
|
// ── Imports ──────────────────────────────────────────────────────────────────
|
|
7
9
|
import { spawnSync } from "node:child_process";
|
|
8
10
|
import { randomBytes } from "node:crypto";
|
|
@@ -16,12 +18,24 @@ const run_program = (program, args, input) => {
|
|
|
16
18
|
if (result.status !== 0)
|
|
17
19
|
throw new Error(`${program} ${args[0] ?? ""} failed`);
|
|
18
20
|
};
|
|
19
|
-
// Where
|
|
20
|
-
|
|
21
|
-
const { name } = JSON.parse(readFileSync(join(app_folder, "package.json"), "utf8"));
|
|
21
|
+
// Where a key named after `name` goes: the user's data folder, unless a folder is given.
|
|
22
|
+
function plan_for(name, folder, home) {
|
|
22
23
|
const signing = folder ?? join(home, ".local", "share", name, "signing");
|
|
23
24
|
return { folder: signing, keystore: join(signing, `${name}-release.p12`), password_file: join(signing, "keystore-password.txt"), alias: name };
|
|
24
25
|
}
|
|
26
|
+
// Where the app's key goes: named after its package.
|
|
27
|
+
export function key_plan(app_folder, folder, home = homedir()) {
|
|
28
|
+
const { name } = JSON.parse(readFileSync(join(app_folder, "package.json"), "utf8"));
|
|
29
|
+
return plan_for(name, folder, home);
|
|
30
|
+
}
|
|
31
|
+
// Where an organization's key goes: named after the organization, its secrets the organization's.
|
|
32
|
+
export function org_key_plan(org, folder, home = homedir()) {
|
|
33
|
+
return { ...plan_for(org, folder, home), org };
|
|
34
|
+
}
|
|
35
|
+
// Where gh stores a secret: the organization's, read by every repository, or the current repository's.
|
|
36
|
+
function secret_args(plan, name) {
|
|
37
|
+
return plan.org ? ["secret", "set", name, "--org", plan.org, "--visibility", "all"] : ["secret", "set", name];
|
|
38
|
+
}
|
|
25
39
|
// Makes the key with one random password for the keystore and the key, then sets the three secrets with gh; refuses a
|
|
26
40
|
// key already there, which is the app's for good.
|
|
27
41
|
export function make_release_key(plan, run = run_program) {
|
|
@@ -35,13 +49,22 @@ export function make_release_key(plan, run = run_program) {
|
|
|
35
49
|
"-dname", "CN=X-Tech Laboratories, O=X-Tech Laboratories",
|
|
36
50
|
"-storepass:file", plan.password_file, "-keypass:file", plan.password_file,
|
|
37
51
|
]);
|
|
38
|
-
run("gh",
|
|
39
|
-
run("gh",
|
|
40
|
-
run("gh",
|
|
52
|
+
run("gh", secret_args(plan, "ANDROID_KEYSTORE_BASE64"), readFileSync(plan.keystore).toString("base64"));
|
|
53
|
+
run("gh", secret_args(plan, "ANDROID_KEYSTORE_PASSWORD"), readFileSync(plan.password_file));
|
|
54
|
+
run("gh", secret_args(plan, "ANDROID_KEY_ALIAS"), plan.alias);
|
|
55
|
+
}
|
|
56
|
+
// The plan the command line asks for: `--org <org> [folder]`, or `[app folder] [folder]`.
|
|
57
|
+
export function plan_of_args(args) {
|
|
58
|
+
if (args[0] === "--org") {
|
|
59
|
+
if (!args[1])
|
|
60
|
+
throw new Error("--org needs the organization's name");
|
|
61
|
+
return org_key_plan(args[1], args[2]);
|
|
62
|
+
}
|
|
63
|
+
return key_plan(resolve(args[0] ?? "."), args[1]);
|
|
41
64
|
}
|
|
42
65
|
if (import.meta.main) {
|
|
43
66
|
try {
|
|
44
|
-
const plan =
|
|
67
|
+
const plan = plan_of_args(process.argv.slice(2));
|
|
45
68
|
make_release_key(plan);
|
|
46
69
|
console.info(`Saved the key in ${plan.folder} and uploaded it to GitHub.`);
|
|
47
70
|
console.info("Back up both files in that folder; losing them means no later build can install over this app.");
|
|
@@ -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
|
+
}
|
|
@@ -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[];
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
// A change to text files laid out for reading. A unified diff, as git diff prints it, is read into files, hunks and
|
|
2
|
+
// numbered lines through parse-diff; each file is laid out side by side, old on the left and new on the right, or as
|
|
3
|
+
// one column; removed and added lines are paired so the words that changed within them are marked, through jsdiff; and
|
|
4
|
+
// long unchanged stretches fold down to a few lines on each side of a change. `edit_lines` diffs a before and after
|
|
5
|
+
// text, such as one edit an agent made. parse-diff (MIT) and jsdiff (BSD-3-Clause) were chosen because they read git's
|
|
6
|
+
// format and mark words exactly as the hand-written readers did, and work alike in a page, a phone and Node.
|
|
7
|
+
// ── Imports ──────────────────────────────────────────────────────────────────
|
|
8
|
+
import { diffLines, diffWordsWithSpace } from "diff";
|
|
9
|
+
import parse_diff from "parse-diff";
|
|
10
|
+
// Lines kept on each side of a change when the rest of an unchanged stretch folds.
|
|
11
|
+
export const context_lines = 3;
|
|
12
|
+
// ── Reading ──────────────────────────────────────────────────────────────────
|
|
13
|
+
const no_file = "/dev/null";
|
|
14
|
+
function status_of(file, path, old_path) {
|
|
15
|
+
if (file.new || file.from === no_file)
|
|
16
|
+
return "added";
|
|
17
|
+
if (file.deleted || file.to === no_file)
|
|
18
|
+
return "removed";
|
|
19
|
+
return path === old_path ? "changed" : "renamed";
|
|
20
|
+
}
|
|
21
|
+
function line_of(change) {
|
|
22
|
+
// parse-diff keeps the leading +, - or space, which the view draws apart.
|
|
23
|
+
const text = change.content.slice(1);
|
|
24
|
+
if (change.type === "add")
|
|
25
|
+
return { kind: "added", text, old_number: null, new_number: change.ln };
|
|
26
|
+
if (change.type === "del")
|
|
27
|
+
return { kind: "removed", text, old_number: change.ln, new_number: null };
|
|
28
|
+
return { kind: "context", text, old_number: change.ln1, new_number: change.ln2 };
|
|
29
|
+
}
|
|
30
|
+
// The files a unified diff changes, in the order it lists them.
|
|
31
|
+
export function parse_unified_diff(text) {
|
|
32
|
+
return parse_diff(text).map((file) => {
|
|
33
|
+
const path = file.to && file.to !== no_file ? file.to : (file.from ?? "");
|
|
34
|
+
const old_path = file.from && file.from !== no_file ? file.from : path;
|
|
35
|
+
const hunks = file.chunks.map((chunk) => ({ header: chunk.content, lines: chunk.changes.filter((change) => !change.content.startsWith("\\")).map(line_of) }));
|
|
36
|
+
return { path, old_path, status: status_of(file, path, old_path), additions: file.additions, deletions: file.deletions, hunks };
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
// The whole change's added and removed line counts.
|
|
40
|
+
export function diff_totals(files) {
|
|
41
|
+
return files.reduce((total, file) => ({ additions: total.additions + file.additions, deletions: total.deletions + file.deletions }), { additions: 0, deletions: 0 });
|
|
42
|
+
}
|
|
43
|
+
// A diff cut at the last whole file within a size, and whether it was cut.
|
|
44
|
+
export function diff_within_limit(text, limit) {
|
|
45
|
+
if (text.length <= limit)
|
|
46
|
+
return { text, truncated: false };
|
|
47
|
+
const cut = text.lastIndexOf("\ndiff --git ", limit);
|
|
48
|
+
return { text: cut > 0 ? text.slice(0, cut + 1) : "", truncated: true };
|
|
49
|
+
}
|
|
50
|
+
// ── Words ────────────────────────────────────────────────────────────────────
|
|
51
|
+
const whole = (text) => [{ text, changed: false }];
|
|
52
|
+
// The words of an old and a new line, each side marking what it lost or gained; a line changed through is left whole.
|
|
53
|
+
export function word_parts(old_text, new_text) {
|
|
54
|
+
const parts = diffWordsWithSpace(old_text, new_text);
|
|
55
|
+
const kept = parts.filter((part) => !part.added && !part.removed).reduce((total, part) => total + part.value.trim().length, 0);
|
|
56
|
+
if (kept === 0)
|
|
57
|
+
return { old: whole(old_text), new: whole(new_text) };
|
|
58
|
+
return {
|
|
59
|
+
old: parts.filter((part) => !part.added).map((part) => ({ text: part.value, changed: Boolean(part.removed) })),
|
|
60
|
+
new: parts.filter((part) => !part.removed).map((part) => ({ text: part.value, changed: Boolean(part.added) })),
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
// Each hunk's lines walked as unchanged lines and blocks of removed lines followed by added ones.
|
|
64
|
+
function walk(file, on_header, on_same, on_block) {
|
|
65
|
+
for (const hunk of file.hunks) {
|
|
66
|
+
if (file.hunks.length > 1)
|
|
67
|
+
on_header(hunk.header);
|
|
68
|
+
let block = { removed: [], added: [] };
|
|
69
|
+
const flush = () => {
|
|
70
|
+
if (block.removed.length || block.added.length)
|
|
71
|
+
on_block(block);
|
|
72
|
+
block = { removed: [], added: [] };
|
|
73
|
+
};
|
|
74
|
+
for (const line of hunk.lines) {
|
|
75
|
+
if (line.kind === "context") {
|
|
76
|
+
flush();
|
|
77
|
+
on_same(line);
|
|
78
|
+
}
|
|
79
|
+
else if (line.kind === "removed") {
|
|
80
|
+
if (block.added.length)
|
|
81
|
+
flush();
|
|
82
|
+
block.removed.push(line);
|
|
83
|
+
}
|
|
84
|
+
else
|
|
85
|
+
block.added.push(line);
|
|
86
|
+
}
|
|
87
|
+
flush();
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
// The paired lines' words, a removed line matched with the added line at the same place in its block.
|
|
91
|
+
function paired(block) {
|
|
92
|
+
const old = block.removed.map((line) => whole(line.text));
|
|
93
|
+
const fresh = block.added.map((line) => whole(line.text));
|
|
94
|
+
for (let index = 0; index < Math.min(old.length, fresh.length); index += 1) {
|
|
95
|
+
const words = word_parts(block.removed[index].text, block.added[index].text);
|
|
96
|
+
old[index] = words.old;
|
|
97
|
+
fresh[index] = words.new;
|
|
98
|
+
}
|
|
99
|
+
return { old, new: fresh };
|
|
100
|
+
}
|
|
101
|
+
// The file as rows of old and new side by side.
|
|
102
|
+
export function split_rows(file) {
|
|
103
|
+
const rows = [];
|
|
104
|
+
walk(file, (header) => rows.push({ kind: "hunk", header }), (line) => rows.push({ kind: "same", left: { number: line.old_number, parts: whole(line.text) }, right: { number: line.new_number, parts: whole(line.text) } }), (block) => {
|
|
105
|
+
const words = paired(block);
|
|
106
|
+
for (let index = 0; index < Math.max(block.removed.length, block.added.length); index += 1) {
|
|
107
|
+
const old_line = block.removed[index];
|
|
108
|
+
const new_line = block.added[index];
|
|
109
|
+
rows.push({ kind: "change", left: old_line ? { number: old_line.old_number, parts: words.old[index] } : null, right: new_line ? { number: new_line.new_number, parts: words.new[index] } : null });
|
|
110
|
+
}
|
|
111
|
+
});
|
|
112
|
+
return rows;
|
|
113
|
+
}
|
|
114
|
+
// The file as one column, each block's removed lines before its added ones.
|
|
115
|
+
export function unified_rows(file) {
|
|
116
|
+
const rows = [];
|
|
117
|
+
walk(file, (header) => rows.push({ kind: "hunk", header }), (line) => rows.push({ kind: "same", old_number: line.old_number, new_number: line.new_number, parts: whole(line.text) }), (block) => {
|
|
118
|
+
const words = paired(block);
|
|
119
|
+
block.removed.forEach((line, index) => rows.push({ kind: "removed", old_number: line.old_number, new_number: null, parts: words.old[index] }));
|
|
120
|
+
block.added.forEach((line, index) => rows.push({ kind: "added", old_number: null, new_number: line.new_number, parts: words.new[index] }));
|
|
121
|
+
});
|
|
122
|
+
return rows;
|
|
123
|
+
}
|
|
124
|
+
// ── Folding ──────────────────────────────────────────────────────────────────
|
|
125
|
+
// The rows with each long unchanged stretch cut to a few lines beside its changes, the rest a fold, unless opened.
|
|
126
|
+
export function fold_rows(rows, opened, context = context_lines) {
|
|
127
|
+
const shown = [];
|
|
128
|
+
let index = 0;
|
|
129
|
+
while (index < rows.length) {
|
|
130
|
+
if (rows[index].kind !== "same") {
|
|
131
|
+
shown.push(rows[index]);
|
|
132
|
+
index += 1;
|
|
133
|
+
continue;
|
|
134
|
+
}
|
|
135
|
+
let end = index;
|
|
136
|
+
while (end < rows.length && rows[end].kind === "same")
|
|
137
|
+
end += 1;
|
|
138
|
+
const keep_before = index === 0 ? 0 : context;
|
|
139
|
+
const keep_after = end === rows.length ? 0 : context;
|
|
140
|
+
const hidden = end - index - keep_before - keep_after;
|
|
141
|
+
if (hidden > 1 && !opened.has(index)) {
|
|
142
|
+
shown.push(...rows.slice(index, index + keep_before), { kind: "fold", start: index, count: hidden }, ...rows.slice(end - keep_after, end));
|
|
143
|
+
}
|
|
144
|
+
else {
|
|
145
|
+
shown.push(...rows.slice(index, end));
|
|
146
|
+
}
|
|
147
|
+
index = end;
|
|
148
|
+
}
|
|
149
|
+
return shown;
|
|
150
|
+
}
|
|
151
|
+
// ── Before and after ─────────────────────────────────────────────────────────
|
|
152
|
+
// A before and after text as lines marked added, removed or kept.
|
|
153
|
+
export function edit_lines(before, after) {
|
|
154
|
+
return diffLines(before, after).flatMap((part) => {
|
|
155
|
+
const kind = part.added ? "added" : part.removed ? "removed" : "context";
|
|
156
|
+
return part.value.replace(/\n$/, "").split("\n").map((text) => ({ kind, text }));
|
|
157
|
+
});
|
|
158
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { Document } from "yaml";
|
|
2
|
+
export interface FrontmatterFile {
|
|
3
|
+
frontmatter: Document;
|
|
4
|
+
body: string;
|
|
5
|
+
}
|
|
6
|
+
export declare function parse_frontmatter(text: string): FrontmatterFile;
|
|
7
|
+
export declare function frontmatter_values(file: FrontmatterFile): Record<string, unknown>;
|
|
8
|
+
export declare function frontmatter_text(value: unknown): string;
|
|
9
|
+
export declare function frontmatter_list(value: unknown): string[];
|
|
10
|
+
export declare function write_frontmatter(values: Record<string, unknown>, body: string, previous?: Document | null): string;
|