@x-tech-laboratories/x-app 0.129.0 → 0.130.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.
- package/README.md +11 -2
- package/dist/agent/claude_stream.d.ts +1 -22
- package/dist/agent/claude_stream.js +2 -85
- package/dist/agent/client.d.ts +1 -35
- package/dist/agent/client.js +2 -169
- package/dist/agent/commands.d.ts +1 -22
- package/dist/agent/commands.js +2 -80
- package/dist/agent/desktop_notify.d.ts +1 -54
- package/dist/agent/desktop_notify.js +2 -229
- package/dist/agent/firefox_bookmarks.d.ts +1 -29
- package/dist/agent/firefox_bookmarks.js +2 -147
- package/dist/agent/git.d.ts +1 -37
- package/dist/agent/git.js +2 -121
- package/dist/agent/github.d.ts +1 -81
- package/dist/agent/github.js +2 -185
- package/dist/agent/inhibit.d.ts +1 -32
- package/dist/agent/inhibit.js +2 -141
- package/dist/agent/keys.d.ts +1 -26
- package/dist/agent/keys.js +2 -161
- package/dist/agent/local_socket.d.ts +1 -18
- package/dist/agent/local_socket.js +2 -106
- package/dist/agent/long_runs.d.ts +1 -45
- package/dist/agent/long_runs.js +2 -419
- package/dist/agent/programs.d.ts +1 -60
- package/dist/agent/programs.js +2 -237
- package/dist/agent/protocol.d.ts +1 -172
- package/dist/agent/protocol.js +2 -249
- package/dist/agent/scheduled_notices.d.ts +1 -21
- package/dist/agent/scheduled_notices.js +2 -38
- package/dist/agent/session.d.ts +1 -61
- package/dist/agent/session.js +2 -335
- package/dist/agent/ssh_config.d.ts +1 -12
- package/dist/agent/ssh_config.js +2 -72
- package/dist/agent/stalls.d.ts +1 -44
- package/dist/agent/stalls.js +2 -176
- package/dist/agent/tasks.d.ts +1 -17
- package/dist/agent/tasks.js +2 -15
- package/dist/agent/wake.d.ts +1 -14
- package/dist/agent/wake.js +2 -88
- package/dist/agent/worktree_files.d.ts +1 -20
- package/dist/agent/worktree_files.js +2 -69
- package/dist/app/desktop.js +21 -8
- package/dist/daemon_link.d.ts +2 -1
- package/dist/daemon_link.js +4 -2
- package/dist/durations.d.ts +1 -15
- package/dist/durations.js +2 -52
- package/package.json +5 -3
|
@@ -1,229 +1,2 @@
|
|
|
1
|
-
//
|
|
2
|
-
|
|
3
|
-
// reach: an agent running as a system service cannot show one.
|
|
4
|
-
//
|
|
5
|
-
// Every notification has a Dismiss button, and when it carries a link, clicking the notification opens it with
|
|
6
|
-
// xdg-open. A notification given a timeout is closed by the agent when it runs out, whatever its urgency, since
|
|
7
|
-
// desktops keep critical ones on screen. Each notification can belong to a key, such as the item it is about: a newer
|
|
8
|
-
// one for the same key replaces it, and dismissing the key closes it.
|
|
9
|
-
// ── Imports ──────────────────────────────────────────────────────────────────
|
|
10
|
-
import { spawn } from "node:child_process";
|
|
11
|
-
// ── Settings ─────────────────────────────────────────────────────────────────
|
|
12
|
-
export const URGENCIES = ["low", "normal", "critical"];
|
|
13
|
-
// How long to keep waiting for a click before giving up on a notification.
|
|
14
|
-
export const WAIT_SECONDS = 12 * 3600;
|
|
15
|
-
// How long notify-send may take to report the new notification's id.
|
|
16
|
-
export const ID_SECONDS = 10.0;
|
|
17
|
-
// The notify-send argument list for one notification.
|
|
18
|
-
export function notify_command(title, body, urgency, url, options) {
|
|
19
|
-
const { app_name, icon = null, replace_id = null, timeout = 0 } = options;
|
|
20
|
-
const args = ["notify-send", `--app-name=${app_name}`, `--urgency=${urgency}`, "--print-id"];
|
|
21
|
-
if (timeout)
|
|
22
|
-
args.push(`--expire-time=${Math.trunc(timeout * 1000)}`);
|
|
23
|
-
if (icon)
|
|
24
|
-
args.push(`--icon=${icon}`);
|
|
25
|
-
if (replace_id !== null)
|
|
26
|
-
args.push(`--replace-id=${replace_id}`);
|
|
27
|
-
if (url)
|
|
28
|
-
args.push("--action=default=Open");
|
|
29
|
-
// --wait keeps notify-send running until the notification closes, and prints the action's name if one was clicked.
|
|
30
|
-
args.push("--action=dismiss=Dismiss", "--wait");
|
|
31
|
-
return [...args, "--", title, body];
|
|
32
|
-
}
|
|
33
|
-
// The call that closes one notification through the desktop's notification service.
|
|
34
|
-
export function close_command(notification_id) {
|
|
35
|
-
return ["busctl", "--user", "call", "org.freedesktop.Notifications", "/org/freedesktop/Notifications", "org.freedesktop.Notifications", "CloseNotification", "u", String(notification_id)];
|
|
36
|
-
}
|
|
37
|
-
export const start_process = (args) => new Promise((resolve, reject) => {
|
|
38
|
-
const child = spawn(args[0], args.slice(1), { stdio: ["ignore", "pipe", "pipe"] });
|
|
39
|
-
let stdout = "";
|
|
40
|
-
let stderr = "";
|
|
41
|
-
let line_waiters = [];
|
|
42
|
-
let first;
|
|
43
|
-
child.stdout.setEncoding("utf8").on("data", (chunk) => {
|
|
44
|
-
stdout += chunk;
|
|
45
|
-
if (first === undefined && stdout.includes("\n")) {
|
|
46
|
-
const cut = stdout.indexOf("\n") + 1;
|
|
47
|
-
first = stdout.slice(0, cut);
|
|
48
|
-
stdout = stdout.slice(cut);
|
|
49
|
-
for (const waiter of line_waiters)
|
|
50
|
-
waiter(first);
|
|
51
|
-
line_waiters = [];
|
|
52
|
-
}
|
|
53
|
-
});
|
|
54
|
-
child.stderr.setEncoding("utf8").on("data", (chunk) => (stderr += chunk));
|
|
55
|
-
const ended = new Promise((settle) => child.on("close", () => {
|
|
56
|
-
if (first === undefined) {
|
|
57
|
-
first = null;
|
|
58
|
-
for (const waiter of line_waiters)
|
|
59
|
-
waiter(null);
|
|
60
|
-
}
|
|
61
|
-
settle([stdout, stderr]);
|
|
62
|
-
}));
|
|
63
|
-
child.on("error", reject);
|
|
64
|
-
child.on("spawn", () => resolve({
|
|
65
|
-
first_line: () => (first !== undefined ? Promise.resolve(first) : new Promise((settle) => line_waiters.push(settle))),
|
|
66
|
-
finished: () => ended,
|
|
67
|
-
exit_code: () => child.exitCode,
|
|
68
|
-
kill: () => void child.kill("SIGKILL"),
|
|
69
|
-
}));
|
|
70
|
-
});
|
|
71
|
-
function within(promise, seconds) {
|
|
72
|
-
return new Promise((resolve, reject) => {
|
|
73
|
-
const timer = setTimeout(() => resolve(TIMED_OUT), seconds * 1000);
|
|
74
|
-
promise.then((value) => {
|
|
75
|
-
clearTimeout(timer);
|
|
76
|
-
resolve(value);
|
|
77
|
-
}, (error) => {
|
|
78
|
-
clearTimeout(timer);
|
|
79
|
-
reject(error);
|
|
80
|
-
});
|
|
81
|
-
});
|
|
82
|
-
}
|
|
83
|
-
const TIMED_OUT = Symbol("timed out");
|
|
84
|
-
// Shows notifications and remembers which one each key has on screen.
|
|
85
|
-
export class Notifier {
|
|
86
|
-
shown = new Map();
|
|
87
|
-
// Messages for one key are handled in arrival order, or a dismiss could overtake the notification it closes.
|
|
88
|
-
queues = new Map();
|
|
89
|
-
start;
|
|
90
|
-
app_name;
|
|
91
|
-
default_title;
|
|
92
|
-
icons;
|
|
93
|
-
report;
|
|
94
|
-
constructor(options) {
|
|
95
|
-
this.app_name = options.app_name;
|
|
96
|
-
this.default_title = options.default_title ?? options.app_name;
|
|
97
|
-
this.icons = options.icons ?? {};
|
|
98
|
-
this.start = options.start ?? start_process;
|
|
99
|
-
this.report = options.on_error ?? (() => { });
|
|
100
|
-
}
|
|
101
|
-
// The notify-send arguments this notifier uses for one notification.
|
|
102
|
-
command(title, body, urgency, url, icon = null, replace_id = null, timeout = 0) {
|
|
103
|
-
const file = icon !== null && Object.hasOwn(this.icons, icon) ? this.icons[icon] : icon;
|
|
104
|
-
return notify_command(title, body, urgency, url, { app_name: this.app_name, icon: file, replace_id, timeout });
|
|
105
|
-
}
|
|
106
|
-
locked(key, work) {
|
|
107
|
-
const name = key ?? "";
|
|
108
|
-
const before = this.queues.get(name) ?? Promise.resolve();
|
|
109
|
-
const next = before.then(work, work);
|
|
110
|
-
this.queues.set(name, next.catch(() => undefined));
|
|
111
|
-
return next;
|
|
112
|
-
}
|
|
113
|
-
// Shows the notification described, and opens its link if it is clicked; resolves once it is closed
|
|
114
|
-
// or replaced.
|
|
115
|
-
async show(frame) {
|
|
116
|
-
const title = String(frame.title || this.default_title);
|
|
117
|
-
const body = String(frame.body || "");
|
|
118
|
-
const urgency = URGENCIES.includes(frame.urgency) ? String(frame.urgency) : "normal";
|
|
119
|
-
const url = frame.url ? String(frame.url) : null;
|
|
120
|
-
const icon = frame.icon ? String(frame.icon) : null;
|
|
121
|
-
const key = frame.key ? String(frame.key) : null;
|
|
122
|
-
const timeout = Math.max(0, Number(frame.timeout || 0) || 0);
|
|
123
|
-
let replaced = false;
|
|
124
|
-
let mine = null;
|
|
125
|
-
const started = await this.locked(key, async () => {
|
|
126
|
-
let replace_id = null;
|
|
127
|
-
const previous = key ? this.shown.get(key) : undefined;
|
|
128
|
-
if (key && previous) {
|
|
129
|
-
this.shown.delete(key);
|
|
130
|
-
replace_id = previous.id;
|
|
131
|
-
previous.replace();
|
|
132
|
-
}
|
|
133
|
-
let process;
|
|
134
|
-
try {
|
|
135
|
-
process = await this.start(this.command(title, body, urgency, url, icon, replace_id, timeout));
|
|
136
|
-
}
|
|
137
|
-
catch (error) {
|
|
138
|
-
this.report(`could not show a desktop notification (is notify-send installed?): ${error.message}`);
|
|
139
|
-
return null;
|
|
140
|
-
}
|
|
141
|
-
const first = await within(process.first_line(), ID_SECONDS);
|
|
142
|
-
if (first === TIMED_OUT) {
|
|
143
|
-
process.kill();
|
|
144
|
-
await process.finished();
|
|
145
|
-
return null;
|
|
146
|
-
}
|
|
147
|
-
const notification_id = first !== null && /^\s*\d+\s*$/.test(first) ? Number(first.trim()) : null;
|
|
148
|
-
if (key && notification_id !== null) {
|
|
149
|
-
mine = { id: notification_id, replace: () => { replaced = true; process.kill(); } };
|
|
150
|
-
this.shown.set(key, mine);
|
|
151
|
-
}
|
|
152
|
-
return { process, notification_id };
|
|
153
|
-
});
|
|
154
|
-
if (started === null)
|
|
155
|
-
return;
|
|
156
|
-
const { process, notification_id } = started;
|
|
157
|
-
try {
|
|
158
|
-
const ended = await within(process.finished(), timeout || WAIT_SECONDS);
|
|
159
|
-
if (replaced)
|
|
160
|
-
return;
|
|
161
|
-
if (ended === TIMED_OUT) {
|
|
162
|
-
// Out of time: close it here, because a desktop keeps a critical notification regardless.
|
|
163
|
-
if (timeout && notification_id !== null)
|
|
164
|
-
await this.close(notification_id);
|
|
165
|
-
if ((await within(process.finished(), 5)) === TIMED_OUT) {
|
|
166
|
-
process.kill();
|
|
167
|
-
await process.finished();
|
|
168
|
-
}
|
|
169
|
-
return;
|
|
170
|
-
}
|
|
171
|
-
const [rest, stderr] = ended;
|
|
172
|
-
if (process.exit_code() !== 0) {
|
|
173
|
-
this.report(`notify-send failed (${process.exit_code()}): ${stderr.trim()}`);
|
|
174
|
-
return;
|
|
175
|
-
}
|
|
176
|
-
if (url && rest.trim() === "default")
|
|
177
|
-
await this.open(url);
|
|
178
|
-
}
|
|
179
|
-
finally {
|
|
180
|
-
if (key && mine !== null && this.shown.get(key) === mine)
|
|
181
|
-
this.shown.delete(key);
|
|
182
|
-
}
|
|
183
|
-
}
|
|
184
|
-
// Closes the notification a key has on screen, if any.
|
|
185
|
-
async dismiss(frame) {
|
|
186
|
-
const key = String(frame.key || "");
|
|
187
|
-
const shown = await this.locked(key, async () => {
|
|
188
|
-
const found = this.shown.get(key);
|
|
189
|
-
this.shown.delete(key);
|
|
190
|
-
return found;
|
|
191
|
-
});
|
|
192
|
-
if (shown !== undefined)
|
|
193
|
-
await this.close(shown.id);
|
|
194
|
-
}
|
|
195
|
-
// Stops waiting on every notification, as the agent shuts down.
|
|
196
|
-
cancel_all() {
|
|
197
|
-
for (const shown of this.shown.values())
|
|
198
|
-
shown.replace();
|
|
199
|
-
this.shown.clear();
|
|
200
|
-
}
|
|
201
|
-
// Closes one notification through the desktop's notification service.
|
|
202
|
-
async close(notification_id) {
|
|
203
|
-
let process;
|
|
204
|
-
try {
|
|
205
|
-
process = await this.start(close_command(notification_id));
|
|
206
|
-
}
|
|
207
|
-
catch (error) {
|
|
208
|
-
this.report(`could not close a desktop notification (is busctl installed?): ${error.message}`);
|
|
209
|
-
return;
|
|
210
|
-
}
|
|
211
|
-
const [, stderr] = await process.finished();
|
|
212
|
-
if (process.exit_code() !== 0)
|
|
213
|
-
this.report(`closing notification ${notification_id} failed: ${stderr.trim()}`);
|
|
214
|
-
}
|
|
215
|
-
// Opens a link in the user's browser.
|
|
216
|
-
async open(url) {
|
|
217
|
-
let process;
|
|
218
|
-
try {
|
|
219
|
-
process = await this.start(["xdg-open", url]);
|
|
220
|
-
}
|
|
221
|
-
catch (error) {
|
|
222
|
-
this.report(`could not open ${url} (is xdg-open installed?): ${error.message}`);
|
|
223
|
-
return;
|
|
224
|
-
}
|
|
225
|
-
const [, stderr] = await process.finished();
|
|
226
|
-
if (process.exit_code() !== 0)
|
|
227
|
-
this.report(`xdg-open ${url} failed: ${stderr.trim()}`);
|
|
228
|
-
}
|
|
229
|
-
}
|
|
1
|
+
// Moved to @x-tech-laboratories/x-app-agent, which a host agent installs without the rest of X-App.
|
|
2
|
+
export * from "@x-tech-laboratories/x-app-agent/desktop_notify";
|
|
@@ -1,29 +1 @@
|
|
|
1
|
-
export
|
|
2
|
-
export declare function default_profile_of(ini: string): {
|
|
3
|
-
path: string;
|
|
4
|
-
is_relative: boolean;
|
|
5
|
-
} | null;
|
|
6
|
-
export declare function firefox_profile(set_folder?: string, folders?: string[]): Promise<string>;
|
|
7
|
-
export interface FirefoxBookmark {
|
|
8
|
-
id: number;
|
|
9
|
-
parent: number;
|
|
10
|
-
type: number;
|
|
11
|
-
title: string | null;
|
|
12
|
-
guid: string;
|
|
13
|
-
url: string | null;
|
|
14
|
-
}
|
|
15
|
-
export interface FirefoxBookmarks {
|
|
16
|
-
rows: FirefoxBookmark[];
|
|
17
|
-
by_id: Map<number, FirefoxBookmark>;
|
|
18
|
-
}
|
|
19
|
-
export declare const root_names: Record<string, string>;
|
|
20
|
-
export declare function read_firefox_bookmarks(profile: string): Promise<FirefoxBookmarks>;
|
|
21
|
-
export declare function bookmark_folder_path(bookmarks: FirefoxBookmarks, id: number): string;
|
|
22
|
-
export declare function is_page_bookmark(row: FirefoxBookmark): boolean;
|
|
23
|
-
export declare function search_bookmarks(bookmarks: FirefoxBookmarks, query: string, limit?: number): FirefoxBookmark[];
|
|
24
|
-
export declare function bookmarks_by_guid(bookmarks: FirefoxBookmarks, guids: string[]): Record<string, FirefoxBookmark>;
|
|
25
|
-
export declare function bookmark_folders(bookmarks: FirefoxBookmarks): {
|
|
26
|
-
guid: string;
|
|
27
|
-
path: string;
|
|
28
|
-
}[];
|
|
29
|
-
export declare function bookmarks_in_folder(bookmarks: FirefoxBookmarks, guid: string): FirefoxBookmark[];
|
|
1
|
+
export * from "@x-tech-laboratories/x-app-agent/firefox_bookmarks";
|
|
@@ -1,147 +1,2 @@
|
|
|
1
|
-
//
|
|
2
|
-
|
|
3
|
-
// while it runs. Tags, which Firefox keeps as folders of their own, are left out.
|
|
4
|
-
// ── Imports ──────────────────────────────────────────────────────────────────
|
|
5
|
-
import { copyFile, mkdtemp, readFile, rm, stat } from "node:fs/promises";
|
|
6
|
-
import { homedir, tmpdir } from "node:os";
|
|
7
|
-
import { join, resolve } from "node:path";
|
|
8
|
-
import { DatabaseSync } from "node:sqlite";
|
|
9
|
-
// ── Profile ──────────────────────────────────────────────────────────────────
|
|
10
|
-
// Where Firefox keeps its profiles: the XDG folder newer versions use, then the older one.
|
|
11
|
-
export function firefox_folders(home = homedir()) {
|
|
12
|
-
return [join(home, ".config", "mozilla", "firefox"), join(home, ".mozilla", "firefox")];
|
|
13
|
-
}
|
|
14
|
-
// The default profile named in a profiles.ini, as a folder relative to it or absolute.
|
|
15
|
-
export function default_profile_of(ini) {
|
|
16
|
-
const sections = ini.split(/^\[/m).map((section) => {
|
|
17
|
-
const [head = "", ...lines] = section.split("\n");
|
|
18
|
-
const values = Object.fromEntries(lines.map((line) => line.split(/=(.*)/s)).filter((pair) => pair.length >= 2).map(([key, value]) => [key.trim(), value.trim()]));
|
|
19
|
-
return { head: head.replace(/\].*$/, ""), values };
|
|
20
|
-
});
|
|
21
|
-
const install = sections.find((section) => section.head.startsWith("Install") && section.values.Default);
|
|
22
|
-
if (install)
|
|
23
|
-
return { path: install.values.Default, is_relative: true };
|
|
24
|
-
const profiles = sections.filter((section) => section.head.startsWith("Profile") && section.values.Path);
|
|
25
|
-
const chosen = profiles.find((section) => section.values.Default === "1") ?? profiles[0];
|
|
26
|
-
return chosen ? { path: chosen.values.Path, is_relative: chosen.values.IsRelative !== "0" } : null;
|
|
27
|
-
}
|
|
28
|
-
// The profile folder set, with ~ as the home folder, or else Firefox's default profile in the folders given.
|
|
29
|
-
export async function firefox_profile(set_folder = "", folders = firefox_folders()) {
|
|
30
|
-
const set = set_folder.trim();
|
|
31
|
-
if (set)
|
|
32
|
-
return resolve(set.replace(/^~(?=$|\/)/, homedir()));
|
|
33
|
-
for (const folder of folders) {
|
|
34
|
-
let ini;
|
|
35
|
-
try {
|
|
36
|
-
ini = await readFile(join(folder, "profiles.ini"), "utf8");
|
|
37
|
-
}
|
|
38
|
-
catch {
|
|
39
|
-
continue;
|
|
40
|
-
}
|
|
41
|
-
const found = default_profile_of(ini);
|
|
42
|
-
if (found)
|
|
43
|
-
return found.is_relative ? join(folder, found.path) : found.path;
|
|
44
|
-
}
|
|
45
|
-
throw new Error("Firefox's profile was not found.");
|
|
46
|
-
}
|
|
47
|
-
const bookmark_type = 1;
|
|
48
|
-
const folder_type = 2;
|
|
49
|
-
// Firefox's own root folders, by guid, with the names its window shows.
|
|
50
|
-
export const root_names = { menu________: "Menu", toolbar_____: "Toolbar", unfiled_____: "Other Bookmarks", mobile______: "Mobile" };
|
|
51
|
-
let kept = null;
|
|
52
|
-
// The bookmarks in the profile's places.sqlite; read again only once it or its write-ahead log changes.
|
|
53
|
-
export async function read_firefox_bookmarks(profile) {
|
|
54
|
-
const places = join(profile, "places.sqlite");
|
|
55
|
-
const sizes = await Promise.all([places, `${places}-wal`].map((path) => stat(path).then((info) => `${info.mtimeMs}:${info.size}`, () => "")));
|
|
56
|
-
if (!sizes[0])
|
|
57
|
-
throw new Error(`Firefox's bookmarks were not found in ${profile}.`);
|
|
58
|
-
const key = `${places}|${sizes.join("|")}`;
|
|
59
|
-
if (kept?.key === key)
|
|
60
|
-
return kept.bookmarks;
|
|
61
|
-
const folder = await mkdtemp(join(tmpdir(), "firefox-bookmarks-"));
|
|
62
|
-
try {
|
|
63
|
-
const copy = join(folder, "places.sqlite");
|
|
64
|
-
await copyFile(places, copy);
|
|
65
|
-
if (sizes[1])
|
|
66
|
-
await copyFile(`${places}-wal`, `${copy}-wal`);
|
|
67
|
-
const database = new DatabaseSync(copy);
|
|
68
|
-
try {
|
|
69
|
-
const rows = database
|
|
70
|
-
.prepare("SELECT b.id, b.parent, b.type, b.title, b.guid, p.url FROM moz_bookmarks b LEFT JOIN moz_places p ON p.id = b.fk ORDER BY b.parent, b.position")
|
|
71
|
-
.all();
|
|
72
|
-
const tags = rows.find((row) => row.guid === "tags________")?.id;
|
|
73
|
-
const by_id = new Map(rows.map((row) => [row.id, row]));
|
|
74
|
-
const shown = rows.filter((row) => {
|
|
75
|
-
let parent = row.parent;
|
|
76
|
-
for (let step = 0; step < 50 && parent; step++) {
|
|
77
|
-
if (parent === tags)
|
|
78
|
-
return false;
|
|
79
|
-
parent = by_id.get(parent)?.parent ?? 0;
|
|
80
|
-
}
|
|
81
|
-
return row.id !== tags;
|
|
82
|
-
});
|
|
83
|
-
const bookmarks = { rows: shown, by_id: new Map(shown.map((row) => [row.id, row])) };
|
|
84
|
-
kept = { key, bookmarks };
|
|
85
|
-
return bookmarks;
|
|
86
|
-
}
|
|
87
|
-
finally {
|
|
88
|
-
database.close();
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
finally {
|
|
92
|
-
await rm(folder, { recursive: true, force: true });
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
// ── Finding ──────────────────────────────────────────────────────────────────
|
|
96
|
-
// A folder's names from its root down, such as "Other Bookmarks/1. Career/5. Job Hunting".
|
|
97
|
-
export function bookmark_folder_path(bookmarks, id) {
|
|
98
|
-
const names = [];
|
|
99
|
-
let row = bookmarks.by_id.get(id);
|
|
100
|
-
for (let step = 0; step < 50 && row && row.parent !== 0; step++) {
|
|
101
|
-
names.unshift(root_names[row.guid] ?? (row.title ?? "").trim());
|
|
102
|
-
row = bookmarks.by_id.get(row.parent);
|
|
103
|
-
}
|
|
104
|
-
return names.join("/");
|
|
105
|
-
}
|
|
106
|
-
// A bookmark of a web page, not a folder, a separator or a saved query.
|
|
107
|
-
export function is_page_bookmark(row) {
|
|
108
|
-
return row.type === bookmark_type && Boolean(row.url) && !row.url.startsWith("place:");
|
|
109
|
-
}
|
|
110
|
-
// Bookmarks whose title or address holds every word typed, the first `limit` of them.
|
|
111
|
-
export function search_bookmarks(bookmarks, query, limit = 30) {
|
|
112
|
-
const words = query.toLowerCase().split(/\s+/).filter(Boolean);
|
|
113
|
-
if (words.length === 0)
|
|
114
|
-
return [];
|
|
115
|
-
return bookmarks.rows.filter((row) => is_page_bookmark(row) && words.every((word) => `${row.title ?? ""} ${row.url}`.toLowerCase().includes(word))).slice(0, limit);
|
|
116
|
-
}
|
|
117
|
-
// Each bookmark still there, by guid; one deleted is left out.
|
|
118
|
-
export function bookmarks_by_guid(bookmarks, guids) {
|
|
119
|
-
const wanted = new Set(guids);
|
|
120
|
-
return Object.fromEntries(bookmarks.rows.filter((row) => is_page_bookmark(row) && wanted.has(row.guid)).map((row) => [row.guid, row]));
|
|
121
|
-
}
|
|
122
|
-
// Every folder that is not one of Firefox's roots, with its path, in the order of their paths.
|
|
123
|
-
export function bookmark_folders(bookmarks) {
|
|
124
|
-
return bookmarks.rows
|
|
125
|
-
.filter((row) => row.type === folder_type && row.parent !== 0 && !root_names[row.guid] && bookmarks.by_id.get(row.parent))
|
|
126
|
-
.map((row) => ({ guid: row.guid, path: bookmark_folder_path(bookmarks, row.id) }))
|
|
127
|
-
.sort((first, second) => first.path.localeCompare(second.path, undefined, { numeric: true }));
|
|
128
|
-
}
|
|
129
|
-
// The bookmarks in a folder and every folder inside it; throws when there is no such folder.
|
|
130
|
-
export function bookmarks_in_folder(bookmarks, guid) {
|
|
131
|
-
const folder = bookmarks.rows.find((row) => row.guid === guid && row.type === folder_type);
|
|
132
|
-
if (!folder)
|
|
133
|
-
throw new Error(`Firefox has no bookmark folder ${guid}.`);
|
|
134
|
-
const inside = new Set([folder.id]);
|
|
135
|
-
// Rows come by parent, so a folder can come before the one holding it; go again until nothing new is found.
|
|
136
|
-
let grew = true;
|
|
137
|
-
while (grew) {
|
|
138
|
-
grew = false;
|
|
139
|
-
for (const row of bookmarks.rows) {
|
|
140
|
-
if (row.type === folder_type && inside.has(row.parent) && !inside.has(row.id)) {
|
|
141
|
-
inside.add(row.id);
|
|
142
|
-
grew = true;
|
|
143
|
-
}
|
|
144
|
-
}
|
|
145
|
-
}
|
|
146
|
-
return bookmarks.rows.filter((row) => is_page_bookmark(row) && inside.has(row.parent));
|
|
147
|
-
}
|
|
1
|
+
// Moved to @x-tech-laboratories/x-app-agent, which a host agent installs without the rest of X-App.
|
|
2
|
+
export * from "@x-tech-laboratories/x-app-agent/firefox_bookmarks";
|
package/dist/agent/git.d.ts
CHANGED
|
@@ -1,37 +1 @@
|
|
|
1
|
-
|
|
2
|
-
export declare function git(args: string[], cwd: string, allowed_exit_codes?: number[]): Promise<string>;
|
|
3
|
-
export declare function default_branch(repository: string): Promise<string>;
|
|
4
|
-
export interface WorktreeRequest {
|
|
5
|
-
path: string;
|
|
6
|
-
branch: string;
|
|
7
|
-
base?: string;
|
|
8
|
-
}
|
|
9
|
-
export interface Worktree {
|
|
10
|
-
path: string;
|
|
11
|
-
head: string;
|
|
12
|
-
branch: string;
|
|
13
|
-
main: boolean;
|
|
14
|
-
}
|
|
15
|
-
export declare function add_worktree(repository: string, { path, branch, base }: WorktreeRequest): Promise<string>;
|
|
16
|
-
export declare function list_worktrees(repository: string): Promise<Worktree[]>;
|
|
17
|
-
export declare function reset_worktree(path: string, commit: string): Promise<void>;
|
|
18
|
-
export declare function remove_worktree(repository: string, path: string, options?: {
|
|
19
|
-
force?: boolean;
|
|
20
|
-
delete_branch?: string;
|
|
21
|
-
}): Promise<void>;
|
|
22
|
-
export interface RawChange {
|
|
23
|
-
path: string;
|
|
24
|
-
old_path: string;
|
|
25
|
-
status: DiffFileStatus;
|
|
26
|
-
blob: string;
|
|
27
|
-
}
|
|
28
|
-
export declare function read_raw_changes(text: string): RawChange[];
|
|
29
|
-
export declare function read_numstat(text: string): Map<string, {
|
|
30
|
-
additions: number;
|
|
31
|
-
deletions: number;
|
|
32
|
-
} | null>;
|
|
33
|
-
export declare function sum_numstat(text: string): {
|
|
34
|
-
additions: number;
|
|
35
|
-
deletions: number;
|
|
36
|
-
};
|
|
37
|
-
export declare function untracked_files(worktree: string): Promise<string[]>;
|
|
1
|
+
export * from "@x-tech-laboratories/x-app-agent/git";
|
package/dist/agent/git.js
CHANGED
|
@@ -1,121 +1,2 @@
|
|
|
1
|
-
//
|
|
2
|
-
|
|
3
|
-
// `--numstat` print, so an app can list a branch's changed files with their status and line counts.
|
|
4
|
-
// ── Imports ──────────────────────────────────────────────────────────────────
|
|
5
|
-
import { existsSync } from "node:fs";
|
|
6
|
-
import { run_program } from "./programs.js";
|
|
7
|
-
// ── Running ──────────────────────────────────────────────────────────────────
|
|
8
|
-
// Runs git in a folder and gives back what it printed, throwing its last words when it ends with an exit code not allowed.
|
|
9
|
-
export async function git(args, cwd, allowed_exit_codes = [0]) {
|
|
10
|
-
const result = await run_program(["git", ...args], { cwd, stdout: "keep", stderr: "keep" });
|
|
11
|
-
if (!allowed_exit_codes.includes(result.exit_code))
|
|
12
|
-
throw new Error(`git ${args[0]} failed: ${result.stderr.trim() || `exit ${result.exit_code}`}`);
|
|
13
|
-
return result.stdout;
|
|
14
|
-
}
|
|
15
|
-
// The branch origin's HEAD names, such as "origin/main", or "HEAD" when the repository has no origin.
|
|
16
|
-
export async function default_branch(repository) {
|
|
17
|
-
try {
|
|
18
|
-
return (await git(["symbolic-ref", "--short", "refs/remotes/origin/HEAD"], repository)).trim();
|
|
19
|
-
}
|
|
20
|
-
catch {
|
|
21
|
-
return "HEAD";
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
// The worktree at the path, made the first time on its own branch, the branch taken when it already exists.
|
|
25
|
-
export async function add_worktree(repository, { path, branch, base }) {
|
|
26
|
-
if (existsSync(path))
|
|
27
|
-
return path;
|
|
28
|
-
if (base === undefined)
|
|
29
|
-
await git(["fetch", "--quiet", "origin"], repository).catch(() => undefined);
|
|
30
|
-
const has_branch = await git(["rev-parse", "--verify", "--quiet", `refs/heads/${branch}`], repository).then(() => true, () => false);
|
|
31
|
-
if (has_branch)
|
|
32
|
-
await git(["worktree", "add", path, branch], repository);
|
|
33
|
-
else
|
|
34
|
-
await git(["worktree", "add", "-b", branch, path, base ?? (await default_branch(repository))], repository);
|
|
35
|
-
return path;
|
|
36
|
-
}
|
|
37
|
-
// The repository's worktrees, its main checkout first, as `git worktree list --porcelain` names them.
|
|
38
|
-
export async function list_worktrees(repository) {
|
|
39
|
-
const text = await git(["worktree", "list", "--porcelain", "-z"], repository);
|
|
40
|
-
const worktrees = [];
|
|
41
|
-
let current = null;
|
|
42
|
-
for (const field of text.split("\0")) {
|
|
43
|
-
if (field.startsWith("worktree ")) {
|
|
44
|
-
current = { path: field.slice("worktree ".length), head: "", branch: "", main: worktrees.length === 0 };
|
|
45
|
-
worktrees.push(current);
|
|
46
|
-
}
|
|
47
|
-
else if (current && field.startsWith("HEAD "))
|
|
48
|
-
current.head = field.slice("HEAD ".length);
|
|
49
|
-
else if (current && field.startsWith("branch "))
|
|
50
|
-
current.branch = field.slice("branch ".length).replace(/^refs\/heads\//, "");
|
|
51
|
-
}
|
|
52
|
-
return worktrees;
|
|
53
|
-
}
|
|
54
|
-
// Puts the worktree back to a commit, throwing away every change and new file since, ignored files kept.
|
|
55
|
-
export async function reset_worktree(path, commit) {
|
|
56
|
-
await git(["reset", "--hard", commit], path);
|
|
57
|
-
await git(["clean", "-fd"], path);
|
|
58
|
-
}
|
|
59
|
-
// Removes a worktree, even with changes in it when `force`, and its branch too when `delete_branch` and it is merged or forced.
|
|
60
|
-
export async function remove_worktree(repository, path, options = {}) {
|
|
61
|
-
await git(["worktree", "remove", ...(options.force ? ["--force"] : []), path], repository);
|
|
62
|
-
if (options.delete_branch)
|
|
63
|
-
await git(["branch", options.force ? "-D" : "-d", options.delete_branch], repository);
|
|
64
|
-
}
|
|
65
|
-
// ── Reading changes ──────────────────────────────────────────────────────────
|
|
66
|
-
const status_letters = { A: "added", D: "removed", R: "renamed", C: "added", M: "changed", T: "changed" };
|
|
67
|
-
// `git diff --raw --no-abbrev -z` output read into changes; a rename or copy names its old path first.
|
|
68
|
-
export function read_raw_changes(text) {
|
|
69
|
-
const parts = text.split("\0");
|
|
70
|
-
const changes = [];
|
|
71
|
-
for (let index = 0; index < parts.length - 1;) {
|
|
72
|
-
const fields = parts[index].replace(/^:/, "").split(" ");
|
|
73
|
-
const letter = fields[4]?.[0] ?? "M";
|
|
74
|
-
const blob = fields[3] ?? "";
|
|
75
|
-
if (letter === "R" || letter === "C") {
|
|
76
|
-
changes.push({ old_path: parts[index + 1], path: parts[index + 2], status: status_letters[letter], blob });
|
|
77
|
-
index += 3;
|
|
78
|
-
}
|
|
79
|
-
else {
|
|
80
|
-
changes.push({ old_path: parts[index + 1], path: parts[index + 1], status: status_letters[letter] ?? "changed", blob });
|
|
81
|
-
index += 2;
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
return changes;
|
|
85
|
-
}
|
|
86
|
-
// `git diff --numstat -z` output as each path's added and removed lines, null for a binary file.
|
|
87
|
-
export function read_numstat(text) {
|
|
88
|
-
const parts = text.split("\0");
|
|
89
|
-
const counts = new Map();
|
|
90
|
-
for (let index = 0; index < parts.length - 1;) {
|
|
91
|
-
const [added, removed, path] = parts[index].split("\t");
|
|
92
|
-
const value = added === "-" ? null : { additions: Number(added) || 0, deletions: Number(removed) || 0 };
|
|
93
|
-
// A rename leaves the path empty and names the old and new paths after it.
|
|
94
|
-
if (path === "") {
|
|
95
|
-
counts.set(parts[index + 2], value);
|
|
96
|
-
index += 3;
|
|
97
|
-
}
|
|
98
|
-
else {
|
|
99
|
-
counts.set(path, value);
|
|
100
|
-
index += 1;
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
|
-
return counts;
|
|
104
|
-
}
|
|
105
|
-
// The lines plain `git diff --numstat` output counts as added and removed, binary files left out.
|
|
106
|
-
export function sum_numstat(text) {
|
|
107
|
-
let additions = 0;
|
|
108
|
-
let deletions = 0;
|
|
109
|
-
for (const line of text.split("\n")) {
|
|
110
|
-
const [added, removed] = line.split("\t");
|
|
111
|
-
if (!added || added === "-")
|
|
112
|
-
continue;
|
|
113
|
-
additions += Number(added) || 0;
|
|
114
|
-
deletions += Number(removed) || 0;
|
|
115
|
-
}
|
|
116
|
-
return { additions, deletions };
|
|
117
|
-
}
|
|
118
|
-
// Files git does not track yet and does not ignore, which a diff against a commit leaves out.
|
|
119
|
-
export async function untracked_files(worktree) {
|
|
120
|
-
return (await git(["ls-files", "--others", "--exclude-standard", "-z"], worktree)).split("\0").filter(Boolean);
|
|
121
|
-
}
|
|
1
|
+
// Moved to @x-tech-laboratories/x-app-agent, which a host agent installs without the rest of X-App.
|
|
2
|
+
export * from "@x-tech-laboratories/x-app-agent/git";
|
package/dist/agent/github.d.ts
CHANGED
|
@@ -1,81 +1 @@
|
|
|
1
|
-
export
|
|
2
|
-
signed_in: boolean;
|
|
3
|
-
account: string;
|
|
4
|
-
problem: string;
|
|
5
|
-
}
|
|
6
|
-
export interface GitHubSignIn {
|
|
7
|
-
code: string;
|
|
8
|
-
url: string;
|
|
9
|
-
}
|
|
10
|
-
export interface IssueRequest {
|
|
11
|
-
repository: string;
|
|
12
|
-
title: string;
|
|
13
|
-
body: string;
|
|
14
|
-
}
|
|
15
|
-
export interface IssueLink {
|
|
16
|
-
number: number;
|
|
17
|
-
opened: boolean;
|
|
18
|
-
}
|
|
19
|
-
export interface IssueState {
|
|
20
|
-
open: boolean;
|
|
21
|
-
labels: string[];
|
|
22
|
-
body: string;
|
|
23
|
-
}
|
|
24
|
-
export interface IssueEdit {
|
|
25
|
-
add_labels?: string[];
|
|
26
|
-
remove_labels?: string[];
|
|
27
|
-
body?: string;
|
|
28
|
-
}
|
|
29
|
-
export interface LabelStyle {
|
|
30
|
-
search?: string;
|
|
31
|
-
colour?: string;
|
|
32
|
-
description?: string;
|
|
33
|
-
}
|
|
34
|
-
export type PullRequestState = "open" | "closed" | "merged";
|
|
35
|
-
export interface PullRequestFound {
|
|
36
|
-
number: number;
|
|
37
|
-
url: string;
|
|
38
|
-
state: PullRequestState;
|
|
39
|
-
draft: boolean;
|
|
40
|
-
}
|
|
41
|
-
export declare const check_buckets: readonly ["pass", "fail", "pending", "skipping", "cancel"];
|
|
42
|
-
export type CheckBucket = (typeof check_buckets)[number];
|
|
43
|
-
export interface PullRequestCheck {
|
|
44
|
-
name: string;
|
|
45
|
-
workflow: string;
|
|
46
|
-
bucket: CheckBucket;
|
|
47
|
-
link: string;
|
|
48
|
-
}
|
|
49
|
-
export interface HookConfig {
|
|
50
|
-
url: string;
|
|
51
|
-
secret: string;
|
|
52
|
-
events: readonly string[];
|
|
53
|
-
}
|
|
54
|
-
export interface GitHubCli {
|
|
55
|
-
account: () => Promise<GitHubAccount>;
|
|
56
|
-
start_sign_in: (on_done: (signed_in: boolean) => void) => Promise<GitHubSignIn>;
|
|
57
|
-
sign_out: (account: string) => Promise<void>;
|
|
58
|
-
find_or_open_issue: (request: IssueRequest) => Promise<IssueLink>;
|
|
59
|
-
view_issue: (repository: string, number: number) => Promise<IssueState>;
|
|
60
|
-
ensure_labels: (repository: string, names: string[], style?: LabelStyle) => Promise<void>;
|
|
61
|
-
edit_issue: (repository: string, number: number, edit: IssueEdit) => Promise<void>;
|
|
62
|
-
post_comment: (repository: string, number: number, body: string) => Promise<number>;
|
|
63
|
-
update_comment: (repository: string, id: number, body: string) => Promise<boolean>;
|
|
64
|
-
close_issue: (repository: string, number: number) => Promise<void>;
|
|
65
|
-
find_pull_request: (repository: string, branch: string) => Promise<PullRequestFound | null>;
|
|
66
|
-
view_pull_request: (repository: string, number: number) => Promise<PullRequestFound>;
|
|
67
|
-
read_checks: (repository: string, number: number) => Promise<PullRequestCheck[]>;
|
|
68
|
-
register_hook: (repository: string, hook: HookConfig) => Promise<"added" | "updated">;
|
|
69
|
-
}
|
|
70
|
-
export interface GitHubCliOptions {
|
|
71
|
-
program?: string;
|
|
72
|
-
host?: string;
|
|
73
|
-
code_wait_in_milliseconds?: number;
|
|
74
|
-
}
|
|
75
|
-
export declare function title_likeness(first: string, second: string): number;
|
|
76
|
-
export declare const same_issue_likeness = 0.6;
|
|
77
|
-
export declare function best_title_match(title: string, issues: {
|
|
78
|
-
number: number;
|
|
79
|
-
title: string;
|
|
80
|
-
}[], least?: number): number | null;
|
|
81
|
-
export declare function github_cli({ program, host, code_wait_in_milliseconds }?: GitHubCliOptions): GitHubCli;
|
|
1
|
+
export * from "@x-tech-laboratories/x-app-agent/github";
|