@jive-sh/skill-sync 0.4.16-latest-darwin-x64 → 0.4.16
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 +121 -0
- package/bin/package.json +1 -0
- package/bin/skill-sync +576 -0
- package/bin/skill-sync-daemon +576 -0
- package/dist/daemons/skill-sync-daemon.js +500 -0
- package/dist/daemons/skill-sync-daemon.js.map +1 -0
- package/dist/entrypoints/skill-sync.js +153 -0
- package/dist/entrypoints/skill-sync.js.map +1 -0
- package/dist/index.js +10 -0
- package/dist/index.js.map +1 -0
- package/dist/internal/config.js +159 -0
- package/dist/internal/config.js.map +1 -0
- package/dist/internal/control.js +101 -0
- package/dist/internal/control.js.map +1 -0
- package/dist/internal/folders.js +46 -0
- package/dist/internal/folders.js.map +1 -0
- package/dist/internal/path.js +36 -0
- package/dist/internal/path.js.map +1 -0
- package/dist/internal/server.js +32 -0
- package/dist/internal/server.js.map +1 -0
- package/dist/internal/skill-check.js +83 -0
- package/dist/internal/skill-check.js.map +1 -0
- package/dist/internal/sync.js +162 -0
- package/dist/internal/sync.js.map +1 -0
- package/dist/internal/usage.js +419 -0
- package/dist/internal/usage.js.map +1 -0
- package/dist/internal/wire.js +24 -0
- package/dist/internal/wire.js.map +1 -0
- package/install.cjs +576 -0
- package/package.json +39 -11
- package/src/daemons/skill-sync-daemon.ts +493 -0
- package/src/entrypoints/skill-sync.ts +156 -0
- package/src/index.ts +15 -0
- package/src/internal/config.ts +200 -0
- package/src/internal/control.ts +140 -0
- package/src/internal/folders.ts +49 -0
- package/src/internal/path.ts +32 -0
- package/src/internal/server.ts +34 -0
- package/src/internal/skill-check.ts +97 -0
- package/src/internal/sync.ts +197 -0
- package/src/internal/usage.ts +457 -0
- package/src/internal/wire.ts +241 -0
- package/multi-call-binary +0 -4
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
import { spawn, spawnSync } from "node:child_process";
|
|
2
|
+
import { Command } from "commander";
|
|
3
|
+
import { Effect } from "effect";
|
|
4
|
+
import { daemonStatuses, upgrade } from "jive-abstract-library";
|
|
5
|
+
import { name, version } from "../../package.json";
|
|
6
|
+
import { DaemonUnreachable, request } from "../internal/control";
|
|
7
|
+
/**
|
|
8
|
+
* The CLI that accompanies the daemon: `login`, `logout`, `list`, `add`, `upgrade` and `version`. Which folders
|
|
9
|
+
* sync is chosen on the dashboard; skills are ticked there too, or added with `add` by the agent that wrote one.
|
|
10
|
+
*
|
|
11
|
+
* The daemon owns the sync token and keeps it in `~/.jive/skill-sync-token.json`. `login`, `logout`, `list`
|
|
12
|
+
* and `add` are requests to it over the control socket; the token goes from the browser straight to the daemon
|
|
13
|
+
* and never through here.
|
|
14
|
+
*/
|
|
15
|
+
const program = new Command("skill-sync")
|
|
16
|
+
.description("keep the skills you choose identical across every Claude, Codex and Grok skills folder on every machine; choose them at https://skill-sync.jive.sh/");
|
|
17
|
+
program
|
|
18
|
+
.command("login")
|
|
19
|
+
.description("sign this machine in with your passkey in the browser; it stays signed in until you log out")
|
|
20
|
+
.action(async () => {
|
|
21
|
+
const result = await withDaemon({ type: "login" }, (event) => {
|
|
22
|
+
if (event.type !== "open")
|
|
23
|
+
return;
|
|
24
|
+
console.log(`Opening ${event.url}`);
|
|
25
|
+
openBrowser(event.url);
|
|
26
|
+
});
|
|
27
|
+
if (result.type === "signedIn") {
|
|
28
|
+
console.log(`Signed in. ${result.machine} is syncing; choose what syncs at https://skill-sync.jive.sh/`);
|
|
29
|
+
}
|
|
30
|
+
else {
|
|
31
|
+
fail(result);
|
|
32
|
+
}
|
|
33
|
+
});
|
|
34
|
+
program
|
|
35
|
+
.command("logout")
|
|
36
|
+
.description("revoke this machine's token and stop syncing until the next login")
|
|
37
|
+
.action(async () => {
|
|
38
|
+
const result = await withDaemon({ type: "logout" }, () => undefined);
|
|
39
|
+
if (result.type === "signedOut") {
|
|
40
|
+
console.log(result.revoked ? "Signed out; the token is revoked." : "Signed out locally; the server could not be reached to revoke the token.");
|
|
41
|
+
}
|
|
42
|
+
else {
|
|
43
|
+
fail(result);
|
|
44
|
+
}
|
|
45
|
+
});
|
|
46
|
+
program
|
|
47
|
+
.command("list")
|
|
48
|
+
.description("list the skills that sync, as the server holds them; needs this machine signed in")
|
|
49
|
+
.action(async () => {
|
|
50
|
+
const result = await withDaemon({ type: "list" }, () => undefined);
|
|
51
|
+
if (result.type !== "list")
|
|
52
|
+
fail(result);
|
|
53
|
+
if (result.skills.length === 0) {
|
|
54
|
+
console.log("No skills sync yet; tick them at https://skill-sync.jive.sh/");
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
const width = Math.max(...result.skills.map((skill) => skill.name.length));
|
|
58
|
+
for (const skill of result.skills) {
|
|
59
|
+
const detail = skill.editedAt === null
|
|
60
|
+
? "no files yet"
|
|
61
|
+
: `${skill.files} ${skill.files === 1 ? "file" : "files"}, last edited ${new Date(skill.editedAt).toLocaleString()} on ${skill.editedBy}`;
|
|
62
|
+
console.log(`${skill.name.padEnd(width)} ${detail}`);
|
|
63
|
+
}
|
|
64
|
+
});
|
|
65
|
+
program
|
|
66
|
+
.command("add")
|
|
67
|
+
.argument("<skill>", "the skill's folder name, which must match the name in its SKILL.md")
|
|
68
|
+
.description("put a skill meant for every project on the synced list; it must be in a folder skill-sync syncs, with a valid SKILL.md. Repeating it changes nothing")
|
|
69
|
+
.action(async (skill) => {
|
|
70
|
+
const result = await withDaemon({ type: "add", skill }, () => undefined);
|
|
71
|
+
if (result.type !== "added")
|
|
72
|
+
fail(result);
|
|
73
|
+
console.log(result.added ? `Added ${skill} to the synced list.` : `${skill} was already on the synced list.`);
|
|
74
|
+
if (result.syncingFolders.length === 0) {
|
|
75
|
+
console.log(`None of the folders holding it on this machine (${result.folders.join(", ")}) is ticked at https://skill-sync.jive.sh/, so its files upload once one is.`);
|
|
76
|
+
}
|
|
77
|
+
});
|
|
78
|
+
program
|
|
79
|
+
.command("upgrade")
|
|
80
|
+
.description("upgrade skill-sync to the latest version with the package manager that installed it")
|
|
81
|
+
.action(async () => {
|
|
82
|
+
await Effect.runPromise(upgrade({ packageName: name })).catch((cause) => {
|
|
83
|
+
console.error(`Upgrade failed: ${cause instanceof Error ? cause.message : String(cause)}`);
|
|
84
|
+
process.exit(1);
|
|
85
|
+
});
|
|
86
|
+
});
|
|
87
|
+
program
|
|
88
|
+
.command("version")
|
|
89
|
+
.description("print the version of skill-sync installed here")
|
|
90
|
+
.action(() => {
|
|
91
|
+
console.log(version);
|
|
92
|
+
});
|
|
93
|
+
program.parse();
|
|
94
|
+
if (process.argv.length <= 2)
|
|
95
|
+
program.help();
|
|
96
|
+
/**
|
|
97
|
+
* Sends a request to the running daemon. If none is listening — stopped, or never started since the
|
|
98
|
+
* install — this package's registered daemon is started through its service manager and the request is
|
|
99
|
+
* retried for a few seconds.
|
|
100
|
+
*/
|
|
101
|
+
async function withDaemon(message, onEvent) {
|
|
102
|
+
try {
|
|
103
|
+
return await request(message, onEvent);
|
|
104
|
+
}
|
|
105
|
+
catch (cause) {
|
|
106
|
+
if (!(cause instanceof DaemonUnreachable))
|
|
107
|
+
throw cause;
|
|
108
|
+
}
|
|
109
|
+
await startDaemons();
|
|
110
|
+
for (let attempt = 0;; attempt++) {
|
|
111
|
+
await new Promise((resolve) => setTimeout(resolve, 500));
|
|
112
|
+
try {
|
|
113
|
+
return await request(message, onEvent);
|
|
114
|
+
}
|
|
115
|
+
catch (cause) {
|
|
116
|
+
if (!(cause instanceof DaemonUnreachable) || attempt >= 20) {
|
|
117
|
+
console.error(`${cause instanceof Error ? cause.message : String(cause)}. Reinstall skill-sync to register its daemon.`);
|
|
118
|
+
process.exit(1);
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
function fail(event) {
|
|
124
|
+
console.error(event.type === "error" ? event.message : `unexpected answer from the daemon: ${JSON.stringify(event)}`);
|
|
125
|
+
process.exit(1);
|
|
126
|
+
}
|
|
127
|
+
/** Starts this package's registered daemons through their service manager. */
|
|
128
|
+
async function startDaemons() {
|
|
129
|
+
const statuses = await Effect.runPromise(daemonStatuses).catch(() => []);
|
|
130
|
+
const ours = statuses.filter((status) => status.registration.packageName === name && status.executableExists);
|
|
131
|
+
if (ours.length === 0) {
|
|
132
|
+
return;
|
|
133
|
+
}
|
|
134
|
+
for (const { registration } of ours) {
|
|
135
|
+
const command = registration.kind === "launchd"
|
|
136
|
+
? ["launchctl", "kickstart", "-k", `gui/${process.getuid?.() ?? 0}/${registration.unit}`]
|
|
137
|
+
: registration.kind === "systemd"
|
|
138
|
+
? ["systemctl", "--user", "restart", registration.unit]
|
|
139
|
+
: ["schtasks", "/Run", "/TN", registration.unit];
|
|
140
|
+
const result = spawnSync(command[0], command.slice(1), { stdio: ["ignore", "ignore", "pipe"] });
|
|
141
|
+
if (result.status !== 0) {
|
|
142
|
+
console.error(`Could not start ${registration.unit}: ${String(result.stderr ?? "").trim() || `exit ${result.status}`}`);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
function openBrowser(url) {
|
|
147
|
+
const command = process.platform === "darwin" ? "open"
|
|
148
|
+
: process.platform === "win32" ? "cmd"
|
|
149
|
+
: "xdg-open";
|
|
150
|
+
const args = process.platform === "win32" ? ["/c", "start", "", url] : [url];
|
|
151
|
+
spawn(command, args, { stdio: "ignore", detached: true }).unref();
|
|
152
|
+
}
|
|
153
|
+
//# sourceMappingURL=skill-sync.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"skill-sync.js","sourceRoot":"","sources":["../../../../src/entrypoints/skill-sync.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,SAAS,EAAE,MAAM,oBAAoB,CAAC;AACtD,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AACpC,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAChC,OAAO,EAAE,cAAc,EAAE,OAAO,EAAE,MAAM,uBAAuB,CAAC;AA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,EAAE,OAAO,CAAC,KAAK,CAAC,CAAC,CAAC,EAAE,EAAE,KAAK,EAAE,CAAC,QAAQ,EAAE,QAAQ,EAAE,MAAM,CAAC,EAAE,CAAC,CAAC;QACjG,IAAI,MAAM,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;YACxB,OAAO,CAAC,KAAK,CAAC,mBAAmB,YAAY,CAAC,IAAI,KAAK,MAAM,CAAC,MAAM,CAAC,MAAM,IAAI,EAAE,CAAC,CAAC,IAAI,EAAE,IAAI,QAAQ,MAAM,CAAC,MAAM,EAAE,EAAE,CAAC,CAAC;QAC1H,CAAC;IACH,CAAC;AACH,CAAC;AAED,SAAS,WAAW,CAAC,GAAW;IAC9B,MAAM,OAAO,GAAG,OAAO,CAAC,QAAQ,KAAK,QAAQ,CAAC,CAAC,CAAC,MAAM;QACpD,CAAC,CAAC,OAAO,CAAC,QAAQ,KAAK,OAAO,CAAC,CAAC,CAAC,KAAK;YACtC,CAAC,CAAC,UAAU,CAAC;IACf,MAAM,IAAI,GAAG,OAAO,CAAC,QAAQ,KAAK,OAAO,CAAC,CAAC,CAAC,CAAC,IAAI,EAAE,OAAO,EAAE,EAAE,EAAE,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC;IAC7E,KAAK,CAAC,OAAO,EAAE,IAAI,EAAE,EAAE,KAAK,EAAE,QAAQ,EAAE,QAAQ,EAAE,IAAI,EAAE,CAAC,CAAC,KAAK,EAAE,CAAC;AACpE,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What the service needs from this package.
|
|
3
|
+
*
|
|
4
|
+
* The daemon and CLI are the product; this entrypoint exists so the service can share the path rules
|
|
5
|
+
* and wire types rather than restate them. A rule restated is a rule that drifts, and these two are
|
|
6
|
+
* exactly the pair that must not: one validates what the other writes to disk.
|
|
7
|
+
*/
|
|
8
|
+
export { invalidPathReason, isValidPath } from "./internal/path";
|
|
9
|
+
export { DAEMON_VERSION_HEADER, isSkillName, skillOf } from "./internal/wire";
|
|
10
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,EAAE,iBAAiB,EAAE,WAAW,EAAE,MAAM,iBAAiB,CAAC;AACjE,OAAO,EAAE,qBAAqB,EAAE,WAAW,EAAE,OAAO,EAAE,MAAM,iBAAiB,CAAC"}
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import { mkdirSync, readFileSync, writeFileSync, existsSync, readdirSync, rmSync, statSync } from "node:fs";
|
|
3
|
+
import { homedir, hostname, userInfo } from "node:os";
|
|
4
|
+
import { join, relative, sep } from "node:path";
|
|
5
|
+
/**
|
|
6
|
+
* Per-machine settings, sync state and the sync token, all under `~/.jive/`.
|
|
7
|
+
*
|
|
8
|
+
* A file rather than environment variables: the endpoint and which machine this is differ per computer
|
|
9
|
+
* and have to survive a login, which is exactly what a config file is for and exactly what an environment
|
|
10
|
+
* variable is not. Which folders to sync is not in here: they are detected, and the choice to leave one
|
|
11
|
+
* out is kept on the server so the dashboard can change it and a reinstalled daemon still remembers it.
|
|
12
|
+
*/
|
|
13
|
+
const JIVE_HOME = join(homedir(), ".jive");
|
|
14
|
+
export const CONFIG_PATH = join(JIVE_HOME, "skill-sync.json");
|
|
15
|
+
const STATE_PATH = join(JIVE_HOME, "skill-sync-state.json");
|
|
16
|
+
/**
|
|
17
|
+
* The sync token, written at sign-in so the daemon is still signed in after a restart, a reboot or an
|
|
18
|
+
* upgrade. An ordinary file: anything that can read the home directory can copy the token, and the
|
|
19
|
+
* dashboard can revoke it.
|
|
20
|
+
*/
|
|
21
|
+
export const TOKEN_PATH = join(JIVE_HOME, "skill-sync-token.json");
|
|
22
|
+
/**
|
|
23
|
+
* How the CLI reaches the running daemon: a Unix socket in `~/.jive/`, or a named pipe on Windows. Only
|
|
24
|
+
* this user can open either, which is what lets the CLI hand the daemon work that involves the token.
|
|
25
|
+
*/
|
|
26
|
+
export const CONTROL_SOCKET = process.platform === "win32"
|
|
27
|
+
? `\\\\.\\pipe\\jive-skill-sync-${userInfo().username}`
|
|
28
|
+
: join(JIVE_HOME, "skill-sync.sock");
|
|
29
|
+
/** Managed by Claude's own cloud sync; see `Settings.exclude`. */
|
|
30
|
+
const DEFAULT_EXCLUDE = ["synced"];
|
|
31
|
+
/**
|
|
32
|
+
* The settings, writing a template on first run. Undefined when the file lacks an endpoint or a machine
|
|
33
|
+
* name, which only happens if someone edited it.
|
|
34
|
+
*/
|
|
35
|
+
export function loadSettings() {
|
|
36
|
+
if (!existsSync(CONFIG_PATH)) {
|
|
37
|
+
mkdirSync(JIVE_HOME, { recursive: true });
|
|
38
|
+
writeFileSync(CONFIG_PATH, `${JSON.stringify({
|
|
39
|
+
endpoint: "https://skill-sync.jive.sh",
|
|
40
|
+
machine: machineName(),
|
|
41
|
+
exclude: DEFAULT_EXCLUDE,
|
|
42
|
+
}, null, 2)}\n`);
|
|
43
|
+
}
|
|
44
|
+
const settings = readJson(CONFIG_PATH);
|
|
45
|
+
if (!settings.endpoint || !settings.machine)
|
|
46
|
+
return undefined;
|
|
47
|
+
return { endpoint: settings.endpoint, machine: settings.machine, exclude: settings.exclude ?? DEFAULT_EXCLUDE };
|
|
48
|
+
}
|
|
49
|
+
function readJson(path) {
|
|
50
|
+
try {
|
|
51
|
+
return JSON.parse(readFileSync(path, "utf-8"));
|
|
52
|
+
}
|
|
53
|
+
catch {
|
|
54
|
+
return {};
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Every folder's sync state.
|
|
59
|
+
*
|
|
60
|
+
* Before folders were detected the file held one folder's `{ cursor, known }`, for the folder then named
|
|
61
|
+
* by `skillsDir` in the settings. That state is carried over to that folder: dropping it would make the
|
|
62
|
+
* folder look never-synced, and every file in it that differs from the server would become a conflict.
|
|
63
|
+
*/
|
|
64
|
+
export function loadStates() {
|
|
65
|
+
const stored = readJson(STATE_PATH);
|
|
66
|
+
if (stored.folders !== undefined)
|
|
67
|
+
return stored.folders;
|
|
68
|
+
const legacyFolder = readJson(CONFIG_PATH).skillsDir;
|
|
69
|
+
if (legacyFolder !== undefined && stored.cursor !== undefined && stored.known !== undefined) {
|
|
70
|
+
return { [legacyFolder]: { cursor: stored.cursor, known: stored.known } };
|
|
71
|
+
}
|
|
72
|
+
return {};
|
|
73
|
+
}
|
|
74
|
+
/** The saved token, or undefined when this machine is signed out. */
|
|
75
|
+
export function loadToken() {
|
|
76
|
+
return readJson(TOKEN_PATH).token;
|
|
77
|
+
}
|
|
78
|
+
export function saveToken(token) {
|
|
79
|
+
mkdirSync(JIVE_HOME, { recursive: true });
|
|
80
|
+
writeFileSync(TOKEN_PATH, `${JSON.stringify({ token }, null, 2)}\n`);
|
|
81
|
+
}
|
|
82
|
+
export function deleteToken() {
|
|
83
|
+
rmSync(TOKEN_PATH, { force: true });
|
|
84
|
+
}
|
|
85
|
+
export function stateFor(states, folder) {
|
|
86
|
+
states[folder] ??= { cursor: 0, known: {} };
|
|
87
|
+
return states[folder];
|
|
88
|
+
}
|
|
89
|
+
export function saveStates(states) {
|
|
90
|
+
mkdirSync(JIVE_HOME, { recursive: true });
|
|
91
|
+
writeFileSync(STATE_PATH, `${JSON.stringify({ folders: states }, null, 2)}\n`);
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* A name ending in `.lock` is another program's lock, never an agent home, a skill or a skill's file.
|
|
95
|
+
* Claude Code locks a path by creating an empty `<path>.lock` folder beside it and removing it when done;
|
|
96
|
+
* counted as an agent home, `~/.claude-<name>.lock` was filled with skills and the lock could not be released.
|
|
97
|
+
*/
|
|
98
|
+
export const isLock = (name) => name.endsWith(".lock");
|
|
99
|
+
/** `HOSTNAME` is not set on macOS, so the template was writing "unknown" on exactly this machine. */
|
|
100
|
+
function machineName() {
|
|
101
|
+
return hostname().replace(/\.local$/, "").replace(/[^A-Za-z0-9-]+/g, "-") || "unknown";
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* The `skills` folder of every agent home in `home`: Claude's `~/.claude` and each `~/.claude-<name>`
|
|
105
|
+
* (each a `CLAUDE_CONFIG_DIR`), Grok's `~/.grok` and each `~/.grok-<name>` (each a `GROK_HOME`; Grok's
|
|
106
|
+
* docs name `~/.grok/skills` as where it reads a user's skills), and Codex's `~/.agents/skills`, where
|
|
107
|
+
* Codex reads a user's skills. Codex counts when either `~/.agents` or Codex's own home `~/.codex`
|
|
108
|
+
* exists, because a machine with Codex installed may have no `~/.agents` yet.
|
|
109
|
+
*
|
|
110
|
+
* Grok also reads `~/.agents/skills`, undocumented; a skill in both folders loads once, from
|
|
111
|
+
* `~/.grok/skills` (checked with `grok inspect`, Grok 1.0.70), so syncing both is harmless.
|
|
112
|
+
*
|
|
113
|
+
* Every home is a candidate, whether or not it has a `skills` folder yet, so an account added later
|
|
114
|
+
* joins without anyone listing it. Which of them actually sync is the user's choice on the dashboard; a
|
|
115
|
+
* folder that is synced and missing is created.
|
|
116
|
+
*/
|
|
117
|
+
export function detectFolders(home = homedir()) {
|
|
118
|
+
return [...new Set(readdirSync(home)
|
|
119
|
+
.filter((entry) => !isLock(entry))
|
|
120
|
+
.filter((entry) => statSync(join(home, entry)).isDirectory())
|
|
121
|
+
.flatMap((entry) => {
|
|
122
|
+
if (entry === ".claude" || entry.startsWith(".claude-") || entry === ".grok" || entry.startsWith(".grok-")) {
|
|
123
|
+
return [join(home, entry, "skills")];
|
|
124
|
+
}
|
|
125
|
+
if (entry === ".agents" || entry === ".codex")
|
|
126
|
+
return [join(home, ".agents", "skills")];
|
|
127
|
+
return [];
|
|
128
|
+
}))]
|
|
129
|
+
.sort();
|
|
130
|
+
}
|
|
131
|
+
export function hashBytes(bytes) {
|
|
132
|
+
return createHash("sha256").update(bytes).digest("hex");
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* Every file under a synced folder, as paths relative to it with forward slashes.
|
|
136
|
+
*
|
|
137
|
+
* Normalised because the same skill synced from Windows and macOS must be one row, not two.
|
|
138
|
+
*/
|
|
139
|
+
export function listLocalFiles(folder, exclude = DEFAULT_EXCLUDE) {
|
|
140
|
+
if (!existsSync(folder))
|
|
141
|
+
return [];
|
|
142
|
+
const found = [];
|
|
143
|
+
const walk = (dir, top) => {
|
|
144
|
+
for (const entry of readdirSync(dir)) {
|
|
145
|
+
if (entry.startsWith(".") || isLock(entry))
|
|
146
|
+
continue;
|
|
147
|
+
if (top && exclude.includes(entry))
|
|
148
|
+
continue;
|
|
149
|
+
const full = join(dir, entry);
|
|
150
|
+
if (statSync(full).isDirectory())
|
|
151
|
+
walk(full, false);
|
|
152
|
+
else
|
|
153
|
+
found.push(relative(folder, full).split(sep).join("/"));
|
|
154
|
+
}
|
|
155
|
+
};
|
|
156
|
+
walk(folder, true);
|
|
157
|
+
return found.sort();
|
|
158
|
+
}
|
|
159
|
+
//# sourceMappingURL=config.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"config.js","sourceRoot":"","sources":["../../../../src/internal/config.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AACzC,OAAO,EAAE,SAAS,EAAE,YAAY,EAAE,aAAa,EAAE,UAAU,EAAE,WAAW,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,SAAS,CAAC;AAC5G,OAAO,EAAE,OAAO,EAAE,QAAQ,EAAE,QAAQ,EAAE,MAAM,SAAS,CAAC;AACtD,OAAO,EAAE,IAAI,EAAE,QAAQ,EAAE,GAAG,EAAE,MAAM,WAAW,CAAC;AAEhD;;;;;;;GAOG;AAEH,MAAM,SAAS,GAAG,IAAI,CAAC,OAAO,EAAE,EAAE,OAAO,CAAC,CAAC;AAC3C,MAAM,CAAC,MAAM,WAAW,GAAG,IAAI,CAAC,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C/D,CAAC;IACH,CAAC,CAAC;IACF,IAAI,CAAC,MAAM,EAAE,IAAI,CAAC,CAAC;IACnB,OAAO,KAAK,CAAC,IAAI,EAAE,CAAC;AACtB,CAAC"}
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import { createConnection, createServer } from "node:net";
|
|
2
|
+
import { chmodSync, rmSync } from "node:fs";
|
|
3
|
+
import { CONTROL_SOCKET } from "./config";
|
|
4
|
+
const isFinal = (event) => event.type !== "open";
|
|
5
|
+
/** The daemon is not running, as opposed to running and refusing the request. */
|
|
6
|
+
export class DaemonUnreachable extends Error {
|
|
7
|
+
}
|
|
8
|
+
/** Sends one request and calls `onEvent` for each event until the final one, which it resolves with. */
|
|
9
|
+
export function request(message, onEvent) {
|
|
10
|
+
return new Promise((resolve, reject) => {
|
|
11
|
+
let settled = false;
|
|
12
|
+
const socket = createConnection(CONTROL_SOCKET);
|
|
13
|
+
socket.on("connect", () => socket.write(`${JSON.stringify(message)}\n`));
|
|
14
|
+
socket.on("error", (cause) => {
|
|
15
|
+
if (settled)
|
|
16
|
+
return;
|
|
17
|
+
settled = true;
|
|
18
|
+
reject(cause.code === "ENOENT" || cause.code === "ECONNREFUSED"
|
|
19
|
+
? new DaemonUnreachable(`the skill-sync daemon is not running (${cause.code} on ${CONTROL_SOCKET})`)
|
|
20
|
+
: cause);
|
|
21
|
+
});
|
|
22
|
+
readLines(socket, (line) => {
|
|
23
|
+
const event = JSON.parse(line);
|
|
24
|
+
onEvent(event);
|
|
25
|
+
if (isFinal(event) && !settled) {
|
|
26
|
+
settled = true;
|
|
27
|
+
socket.end();
|
|
28
|
+
resolve(event);
|
|
29
|
+
}
|
|
30
|
+
});
|
|
31
|
+
socket.on("close", () => {
|
|
32
|
+
if (!settled) {
|
|
33
|
+
settled = true;
|
|
34
|
+
reject(new Error("the daemon closed the connection without answering"));
|
|
35
|
+
}
|
|
36
|
+
});
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Listens on the control socket. Resolves false, without listening, when another live daemon already
|
|
41
|
+
* holds it: two installs of skill-sync mean two daemons, and the second stands down rather than fight.
|
|
42
|
+
*/
|
|
43
|
+
export async function serve(handle) {
|
|
44
|
+
if (await answers())
|
|
45
|
+
return false;
|
|
46
|
+
// Nothing answered, so a socket file left here is from a daemon that died; it would block the listen.
|
|
47
|
+
if (process.platform !== "win32")
|
|
48
|
+
rmSync(CONTROL_SOCKET, { force: true });
|
|
49
|
+
const server = createServer((socket) => {
|
|
50
|
+
const reply = {
|
|
51
|
+
send: (event) => {
|
|
52
|
+
if (socket.writable)
|
|
53
|
+
socket.write(`${JSON.stringify(event)}\n`);
|
|
54
|
+
if (isFinal(event))
|
|
55
|
+
socket.end();
|
|
56
|
+
},
|
|
57
|
+
};
|
|
58
|
+
readLines(socket, (line) => {
|
|
59
|
+
try {
|
|
60
|
+
handle(JSON.parse(line), reply);
|
|
61
|
+
}
|
|
62
|
+
catch (cause) {
|
|
63
|
+
reply.send({ type: "error", message: cause instanceof Error ? cause.message : String(cause) });
|
|
64
|
+
}
|
|
65
|
+
});
|
|
66
|
+
socket.on("error", () => undefined); // A CLI that went away mid-request; nothing to tell it.
|
|
67
|
+
});
|
|
68
|
+
await new Promise((resolve, reject) => {
|
|
69
|
+
server.once("error", reject);
|
|
70
|
+
server.listen(CONTROL_SOCKET, () => resolve());
|
|
71
|
+
});
|
|
72
|
+
// Only this user may talk to the daemon that holds their token.
|
|
73
|
+
if (process.platform !== "win32")
|
|
74
|
+
chmodSync(CONTROL_SOCKET, 0o600);
|
|
75
|
+
return server;
|
|
76
|
+
}
|
|
77
|
+
/** Whether a live daemon is listening on the control socket. */
|
|
78
|
+
function answers() {
|
|
79
|
+
return new Promise((resolve) => {
|
|
80
|
+
const socket = createConnection(CONTROL_SOCKET);
|
|
81
|
+
socket.on("connect", () => {
|
|
82
|
+
socket.end();
|
|
83
|
+
resolve(true);
|
|
84
|
+
});
|
|
85
|
+
socket.on("error", () => resolve(false));
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
function readLines(socket, onLine) {
|
|
89
|
+
let buffer = "";
|
|
90
|
+
socket.on("data", (chunk) => {
|
|
91
|
+
buffer += chunk.toString();
|
|
92
|
+
let newline;
|
|
93
|
+
while ((newline = buffer.indexOf("\n")) >= 0) {
|
|
94
|
+
const line = buffer.slice(0, newline).trim();
|
|
95
|
+
buffer = buffer.slice(newline + 1);
|
|
96
|
+
if (line !== "")
|
|
97
|
+
onLine(line);
|
|
98
|
+
}
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
//# sourceMappingURL=control.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"control.js","sourceRoot":"","sources":["../../../../src/internal/control.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,YAAY,EAA4B,MAAM,UAAU,CAAC;AACpF,OAAO,EAAE,SAAS,EAAE,MAAM,EAAE,MAAM,SAAS,CAAC;AAC5C,OAAO,EAAE,cAAc,EAAE,MAAM,UAAU,CAAC;AAuC1C,MAAM,OAAO,GAAG,CAAC,KAAmB,EAAW,EAAE,CAAC,KAAK,CAAC,IAAI,KAAK,MAAM,CAAC;AAExE,iFAAiF;AACjF,MAAM,OAAO,iBAAkB,SAAQ,KAAK;CAAG;AAE/C,wGAAwG;AACxG,MAAM,UAAU,OAAO,CAAC,OAAuB,EAAE,OAAsC;IACrF,OAAO,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE;QACrC,IAAI,OAAO,GAAG,KAAK,CAAC;QACpB,MAAM,MAAM,GAAG,gBAAgB,CAAC,cAAc,CAAC,CAAC;QAChD,MAAM,CAAC,EAAE,CAAC,SAAS,EAAE,GAAG,EAAE,CAAC,MAAM,CAAC,KAAK,CAAC,GAAG,IAAI,CAAC,SAAS,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC;QACzE,MAAM,CAAC,EAAE,CAAC,OAAO,EAAE,CA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O;IACd,OAAO,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,EAAE;QAC7B,MAAM,MAAM,GAAG,gBAAgB,CAAC,cAAc,CAAC,CAAC;QAChD,MAAM,CAAC,EAAE,CAAC,SAAS,EAAE,GAAG,EAAE;YACxB,MAAM,CAAC,GAAG,EAAE,CAAC;YACb,OAAO,CAAC,IAAI,CAAC,CAAC;QAChB,CAAC,CAAC,CAAC;QACH,MAAM,CAAC,EAAE,CAAC,OAAO,EAAE,GAAG,EAAE,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC,CAAC;IAC3C,CAAC,CAAC,CAAC;AACL,CAAC;AAED,SAAS,SAAS,CAAC,MAAc,EAAE,MAA8B;IAC/D,IAAI,MAAM,GAAG,EAAE,CAAC;IAChB,MAAM,CAAC,EAAE,CAAC,MAAM,EAAE,CAAC,KAAK,EAAE,EAAE;QAC1B,MAAM,IAAI,KAAK,CAAC,QAAQ,EAAE,CAAC;QAC3B,IAAI,OAAe,CAAC;QACpB,OAAO,CAAC,OAAO,GAAG,MAAM,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC,IAAI,CAAC,EAAE,CAAC;YAC7C,MAAM,IAAI,GAAG,MAAM,CAAC,KAAK,CAAC,CAAC,EAAE,OAAO,CAAC,CAAC,IAAI,EAAE,CAAC;YAC7C,MAAM,GAAG,MAAM,CAAC,KAAK,CAAC,OAAO,GAAG,CAAC,CAAC,CAAC;YACnC,IAAI,IAAI,KAAK,EAAE;gBAAE,MAAM,CAAC,IAAI,CAAC,CAAC;QAChC,CAAC;IACH,CAAC,CAAC,CAAC;AACL,CAAC"}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { readFileSync } from "node:fs";
|
|
2
|
+
import { join } from "node:path";
|
|
3
|
+
import { detectFolders, hashBytes, listLocalFiles } from "./config";
|
|
4
|
+
import { fetchServer } from "./server";
|
|
5
|
+
import { skillOf, } from "./wire";
|
|
6
|
+
/**
|
|
7
|
+
* What this machine tells the service about its folders, and what it hears back.
|
|
8
|
+
*
|
|
9
|
+
* The daemon and the CLI both register, so this lives in one place: they must describe a folder the same
|
|
10
|
+
* way, or the dashboard would show whichever of them spoke last.
|
|
11
|
+
*/
|
|
12
|
+
/**
|
|
13
|
+
* The skills on disk in one folder, with each file's hash. Hashing every file on each check is cheap at
|
|
14
|
+
* skills' size, and it is what lets the dashboard show whether this folder's copy is the synced one.
|
|
15
|
+
*/
|
|
16
|
+
export function localSkills(folder, config) {
|
|
17
|
+
const byName = new Map();
|
|
18
|
+
for (const path of listLocalFiles(folder, config.exclude)) {
|
|
19
|
+
const name = skillOf(path);
|
|
20
|
+
let hash;
|
|
21
|
+
try {
|
|
22
|
+
hash = hashBytes(readFileSync(join(folder, path)));
|
|
23
|
+
}
|
|
24
|
+
catch {
|
|
25
|
+
continue; // Deleted between listing and reading; the next check reports the folder as it is.
|
|
26
|
+
}
|
|
27
|
+
byName.set(name, [...(byName.get(name) ?? []), { path, hash }]);
|
|
28
|
+
}
|
|
29
|
+
return [...byName].map(([name, files]) => ({ name, files }));
|
|
30
|
+
}
|
|
31
|
+
/** Everything registration sends, so a caller can tell when it has changed and is worth re-sending. */
|
|
32
|
+
export function describe(config) {
|
|
33
|
+
return { folders: detectFolders().map((path) => ({ path, skills: localSkills(path, config) })) };
|
|
34
|
+
}
|
|
35
|
+
/** Registers this machine's folders and skills; returns the folders to sync and the skills in each. */
|
|
36
|
+
export async function register(config, request) {
|
|
37
|
+
const response = await fetchServer(config, "/folders", {
|
|
38
|
+
method: "POST",
|
|
39
|
+
headers: { "content-type": "application/json" },
|
|
40
|
+
body: JSON.stringify(request),
|
|
41
|
+
});
|
|
42
|
+
if (!response.ok)
|
|
43
|
+
throw new Error(`POST /folders: ${response.status} ${await response.text()}`);
|
|
44
|
+
return (await response.json()).folders;
|
|
45
|
+
}
|
|
46
|
+
//# sourceMappingURL=folders.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"folders.js","sourceRoot":"","sources":["../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AG,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,WAAW,CAAC,IAAI,EAAE,MAAM,CAAC,EAAE,CAAC,CAAC,EAAE,CAAC;AACnG,CAAC;AAED,uGAAuG;AACvG,MAAM,CAAC,KAAK,UAAU,QAAQ,CAAC,MAAc,EAAE,OAAuB;IACpE,MAAM,QAAQ,GAAG,MAAM,WAAW,CAAC,MAAM,EAAE,UAAU,EAAE;QACrD,MAAM,EAAE,MAAM;QACd,OAAO,EAAE,EAAE,cAAc,EAAE,kBAAkB,EAAE;QAC/C,IAAI,EAAE,IAAI,CAAC,SAAS,CAAC,OAAO,CAAC;KAC9B,CAAC,CAAC;IACH,IAAI,CAAC,QAAQ,CAAC,EAAE;QAAE,MAAM,IAAI,KAAK,CAAC,kBAAkB,QAAQ,CAAC,MAAM,IAAI,MAAM,QAAQ,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC;IAChG,OAAO,CAAC,MAAM,QAAQ,CAAC,IAAI,EAAsB,CAAA,CAAC,OAAO,CAAC;AAC5D,CAAC"}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Validation for the one piece of untrusted input that becomes a filesystem path.
|
|
3
|
+
*
|
|
4
|
+
* Shared by the Worker and the daemon on purpose, and deliberately free of any Node import so the Worker
|
|
5
|
+
* can bundle it. Both ends check: the server so one compromised client cannot poison what every machine
|
|
6
|
+
* pulls, and the daemon because a server it does not control is not a thing to take paths from either.
|
|
7
|
+
*
|
|
8
|
+
* Without this, a pushed path of `../../.ssh/authorized_keys` is joined to the skills directory and
|
|
9
|
+
* written wherever the daemon's user can write.
|
|
10
|
+
*/
|
|
11
|
+
/** Long enough for any real nesting, short enough that nothing can be used to exhaust anything. */
|
|
12
|
+
const MAX_LENGTH = 512;
|
|
13
|
+
export function invalidPathReason(path) {
|
|
14
|
+
if (path.length === 0 || path.length > MAX_LENGTH)
|
|
15
|
+
return "path is empty or too long";
|
|
16
|
+
if (path.includes("\0"))
|
|
17
|
+
return "path contains a null byte";
|
|
18
|
+
if (path.includes("\\"))
|
|
19
|
+
return "path must use forward slashes";
|
|
20
|
+
if (path.startsWith("/"))
|
|
21
|
+
return "path must be relative";
|
|
22
|
+
// A Windows drive letter is absolute on the machine that matters even though it has no leading slash.
|
|
23
|
+
if (/^[A-Za-z]:/.test(path))
|
|
24
|
+
return "path must be relative";
|
|
25
|
+
for (const segment of path.split("/")) {
|
|
26
|
+
if (segment === "")
|
|
27
|
+
return "path has an empty segment";
|
|
28
|
+
if (segment === "." || segment === "..")
|
|
29
|
+
return "path must not contain . or .. segments";
|
|
30
|
+
}
|
|
31
|
+
return undefined;
|
|
32
|
+
}
|
|
33
|
+
export function isValidPath(path) {
|
|
34
|
+
return invalidPathReason(path) === undefined;
|
|
35
|
+
}
|
|
36
|
+
//# sourceMappingURL=path.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"path.js","sourceRoot":"","sources":["../../../../src/internal/path.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAEH,mGAAmG;AACnG,MAAM,UAAU,GAAG,GAAG,CAAC;AAEvB,MAAM,UAAU,iBAAiB,CAAC,IAAY;IAC5C,IAAI,IAAI,CAAC,MAAM,KAAK,CAAC,IAAI,IAAI,CAAC,MAAM,GAAG,UAAU;QAAE,OAAO,2BAA2B,CAAC;IACtF,IAAI,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC;QAAE,OAAO,2BAA2B,CAAC;IAC5D,IAAI,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC;QAAE,OAAO,+BAA+B,CAAC;IAChE,IAAI,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC;QAAE,OAAO,uBAAuB,CAAC;IACzD,sGAAsG;IACtG,IAAI,YAAY,CAAC,IAAI,CAAC,IAAI,CAAC;QAAE,OAAO,uBAAuB,CAAC;IAE5D,KAAK,MAAM,OAAO,IAAI,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,EAAE,CAAC;QACtC,IAAI,OAAO,KAAK,EAAE;YAAE,OAAO,2BAA2B,CAAC;QACvD,IAAI,OAAO,KAAK,GAAG,IAAI,OAAO,KAAK,IAAI;YAAE,OAAO,wCAAwC,CAAC;IAC3F,CAAC;IACD,OAAO,SAAS,CAAC;AACnB,CAAC;AAED,MAAM,UAAU,WAAW,CAAC,IAAY;IACtC,OAAO,iBAAiB,CAAC,IAAI,CAAC,KAAK,SAAS,CAAC;AAC/C,CAAC"}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Requests to the skill-sync service, signed with this machine's token.
|
|
3
|
+
*
|
|
4
|
+
* Every request goes through `fetchServer`, so a token the server no longer accepts is noticed the same
|
|
5
|
+
* way whichever request hits it first.
|
|
6
|
+
*/
|
|
7
|
+
/** The server answered 401: the token was revoked or is unknown, so no retry with it can succeed. */
|
|
8
|
+
export class TokenRejected extends Error {
|
|
9
|
+
token;
|
|
10
|
+
constructor(message, token) {
|
|
11
|
+
super(message);
|
|
12
|
+
this.token = token;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
export function authHeaders(config, extra) {
|
|
16
|
+
return { authorization: `Bearer ${config.token}`, "x-skill-sync-machine": config.machine, ...extra };
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Sends one request to the server with the token. Throws `TokenRejected` on 401; every other status is
|
|
20
|
+
* returned for the caller to judge, since some (a 409 from `PUT /files`) are answers rather than failures.
|
|
21
|
+
*/
|
|
22
|
+
export async function fetchServer(config, path, init = {}) {
|
|
23
|
+
const response = await fetch(`${config.endpoint}${path}`, {
|
|
24
|
+
...init,
|
|
25
|
+
headers: authHeaders(config, (init.headers ?? {})),
|
|
26
|
+
});
|
|
27
|
+
if (response.status === 401) {
|
|
28
|
+
throw new TokenRejected(`${init.method ?? "GET"} ${path}: the server rejected this machine's token`, config.token);
|
|
29
|
+
}
|
|
30
|
+
return response;
|
|
31
|
+
}
|
|
32
|
+
//# sourceMappingURL=server.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"server.js","sourceRoot":"","sources":["../../../../src/internal/server.ts"],"names":[],"mappings":"AAEA;;;;;GAKG;AAEH,qGAAqG;AACrG,MAAM,OAAO,aAAc,SAAQ,KAAK;IACA;IAAtC,YAAY,OAAe,EAAW,KAAa;QACjD,KAAK,CAAC,OAAO,CAAC,CAAC;QADqB,UAAK,GAAL,KAAK,CAAQ;IAEnD,CAAC;CACF;AAED,MAAM,UAAU,WAAW,CAAC,MAAc,EAAE,KAA6B;IACvE,OAAO,EAAE,aAAa,EAAE,UAAU,MAAM,CAAC,KAAK,EAAE,EAAE,sBAAsB,EAAE,MAAM,CAAC,OAAO,EAAE,GAAG,KAAK,EAAE,CAAC;AACvG,CAAC;AAED;;;GAGG;AACH,MAAM,CAAC,KAAK,UAAU,WAAW,CAAC,MAAc,EAAE,IAAY,EAAE,OAAoB,EAAE;IACpF,MAAM,QAAQ,GAAG,MAAM,KAAK,CAAC,GAAG,MAAM,CAAC,QAAQ,GAAG,IAAI,EAAE,EAAE;QACxD,GAAG,IAAI;QACP,OAAO,EAAE,WAAW,CAAC,MAAM,EAAE,CAAC,IAAI,CAAC,OAAO,IAAI,EAAE,CAA2B,CAAC;KAC7E,CAAC,CAAC;IACH,IAAI,QAAQ,CAAC,MAAM,KAAK,GAAG,EAAE,CAAC;QAC5B,MAAM,IAAI,aAAa,CAAC,GAAG,IAAI,CAAC,MAAM,IAAI,KAAK,IAAI,IAAI,4CAA4C,EAAE,MAAM,CAAC,KAAK,CAAC,CAAC;IACrH,CAAC;IACD,OAAO,QAAQ,CAAC;AAClB,CAAC"}
|