@jive-sh/skill-sync 0.7.2 → 0.7.3-latest-darwin-x64
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/multi-call-binary +4 -0
- package/package.json +11 -36
- package/README.md +0 -196
- package/bin/package.json +0 -1
- package/bin/skill-sync +0 -614
- package/bin/skill-sync-daemon +0 -614
- package/dist/daemons/skill-sync-daemon.js +0 -634
- package/dist/daemons/skill-sync-daemon.js.map +0 -1
- package/dist/entrypoints/skill-sync.js +0 -7
- package/dist/entrypoints/skill-sync.js.map +0 -1
- package/dist/index.js +0 -10
- package/dist/index.js.map +0 -1
- package/dist/internal/agents/agent-tool.js +0 -102
- package/dist/internal/agents/agent-tool.js.map +0 -1
- package/dist/internal/agents/claude.js +0 -424
- package/dist/internal/agents/claude.js.map +0 -1
- package/dist/internal/agents/codex.js +0 -270
- package/dist/internal/agents/codex.js.map +0 -1
- package/dist/internal/agents/command.js +0 -219
- package/dist/internal/agents/command.js.map +0 -1
- package/dist/internal/agents/grok.js +0 -170
- package/dist/internal/agents/grok.js.map +0 -1
- package/dist/internal/agents/index.js +0 -70
- package/dist/internal/agents/index.js.map +0 -1
- package/dist/internal/cli.js +0 -185
- package/dist/internal/cli.js.map +0 -1
- package/dist/internal/config.js +0 -131
- package/dist/internal/config.js.map +0 -1
- package/dist/internal/control.js +0 -113
- package/dist/internal/control.js.map +0 -1
- package/dist/internal/daemon-control/impl.js +0 -68
- package/dist/internal/daemon-control/impl.js.map +0 -1
- package/dist/internal/daemon-control/interface.js +0 -4
- package/dist/internal/daemon-control/interface.js.map +0 -1
- package/dist/internal/folders.js +0 -47
- package/dist/internal/folders.js.map +0 -1
- package/dist/internal/local-skills.js +0 -43
- package/dist/internal/local-skills.js.map +0 -1
- package/dist/internal/path.js +0 -36
- package/dist/internal/path.js.map +0 -1
- package/dist/internal/server.js +0 -80
- package/dist/internal/server.js.map +0 -1
- package/dist/internal/skill-check.js +0 -83
- package/dist/internal/skill-check.js.map +0 -1
- package/dist/internal/skill-refinement/evals/cases.js +0 -143
- package/dist/internal/skill-refinement/evals/cases.js.map +0 -1
- package/dist/internal/skill-refinement/evals/opus-action-judge-original.json +0 -350
- package/dist/internal/skill-refinement/evals/opus-judge-original.json +0 -165
- package/dist/internal/skill-refinement/feedback/intake.js +0 -52
- package/dist/internal/skill-refinement/feedback/intake.js.map +0 -1
- package/dist/internal/skill-refinement/jev/impl.js +0 -51
- package/dist/internal/skill-refinement/jev/impl.js.map +0 -1
- package/dist/internal/skill-refinement/jev/interface.js +0 -14
- package/dist/internal/skill-refinement/jev/interface.js.map +0 -1
- package/dist/internal/skill-refinement/paths.js +0 -62
- package/dist/internal/skill-refinement/paths.js.map +0 -1
- package/dist/internal/skill-refinement/platform.js +0 -68
- package/dist/internal/skill-refinement/platform.js.map +0 -1
- package/dist/internal/skill-refinement/service.js +0 -238
- package/dist/internal/skill-refinement/service.js.map +0 -1
- package/dist/internal/skill-refinement/skills/skill-text.js +0 -217
- package/dist/internal/skill-refinement/skills/skill-text.js.map +0 -1
- package/dist/internal/skill-refinement/workflow/skill-refinement.js +0 -576
- package/dist/internal/skill-refinement/workflow/skill-refinement.js.map +0 -1
- package/dist/internal/skill-refinement/writer/impl.js +0 -69
- package/dist/internal/skill-refinement/writer/impl.js.map +0 -1
- package/dist/internal/skill-refinement/writer/interface.js +0 -10
- package/dist/internal/skill-refinement/writer/interface.js.map +0 -1
- package/dist/internal/sync.js +0 -182
- package/dist/internal/sync.js.map +0 -1
- package/dist/internal/wire.js +0 -31
- package/dist/internal/wire.js.map +0 -1
- package/install.cjs +0 -614
- package/src/daemons/skill-sync-daemon.ts +0 -639
- package/src/entrypoints/skill-sync.ts +0 -12
- package/src/index.ts +0 -15
- package/src/internal/agents/agent-tool.ts +0 -149
- package/src/internal/agents/claude.ts +0 -442
- package/src/internal/agents/codex.ts +0 -288
- package/src/internal/agents/command.ts +0 -257
- package/src/internal/agents/grok.ts +0 -174
- package/src/internal/agents/index.ts +0 -74
- package/src/internal/cli.ts +0 -217
- package/src/internal/config.ts +0 -172
- package/src/internal/control.ts +0 -157
- package/src/internal/daemon-control/impl.ts +0 -80
- package/src/internal/daemon-control/interface.ts +0 -40
- package/src/internal/folders.ts +0 -50
- package/src/internal/local-skills.ts +0 -58
- package/src/internal/path.ts +0 -32
- package/src/internal/server.ts +0 -76
- package/src/internal/skill-check.ts +0 -97
- package/src/internal/skill-refinement/evals/cases.ts +0 -167
- package/src/internal/skill-refinement/evals/opus-action-judge-original.json +0 -350
- package/src/internal/skill-refinement/evals/opus-judge-original.json +0 -165
- package/src/internal/skill-refinement/feedback/intake.ts +0 -66
- package/src/internal/skill-refinement/jev/impl.ts +0 -63
- package/src/internal/skill-refinement/jev/interface.ts +0 -51
- package/src/internal/skill-refinement/paths.ts +0 -87
- package/src/internal/skill-refinement/platform.ts +0 -72
- package/src/internal/skill-refinement/service.ts +0 -283
- package/src/internal/skill-refinement/skills/skill-text.ts +0 -290
- package/src/internal/skill-refinement/workflow/skill-refinement.ts +0 -641
- package/src/internal/skill-refinement/writer/impl.ts +0 -79
- package/src/internal/skill-refinement/writer/interface.ts +0 -29
- package/src/internal/sync.ts +0 -216
- package/src/internal/wire.ts +0 -439
|
@@ -1,79 +0,0 @@
|
|
|
1
|
-
import { Effect, Layer, Schema } from 'effect';
|
|
2
|
-
import fs from 'node:fs';
|
|
3
|
-
import path from 'node:path';
|
|
4
|
-
import { Writer, WriterError, type IWriter, type Written } from './interface';
|
|
5
|
-
|
|
6
|
-
/** Which Claude account and model the calls run on, and where each call's system prompt file is written. */
|
|
7
|
-
export interface WriterConfig {
|
|
8
|
-
/** The `claude` CLI to run: found where its installers put it, since the daemon runs with the service manager's PATH. */
|
|
9
|
-
readonly claudeCommand: string;
|
|
10
|
-
/** The Claude Code config folder whose sign-in the calls use (`CLAUDE_CONFIG_DIR`): it decides which account pays. */
|
|
11
|
-
readonly claudeConfigDir: string;
|
|
12
|
-
readonly model: string;
|
|
13
|
-
readonly scratch: string;
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
/** `claude -p --output-format json`'s result, the fields this module reads. */
|
|
17
|
-
const ClaudeResult = Schema.Struct({
|
|
18
|
-
is_error: Schema.Boolean,
|
|
19
|
-
result: Schema.String,
|
|
20
|
-
total_cost_usd: Schema.Finite,
|
|
21
|
-
usage: Schema.Struct({
|
|
22
|
-
input_tokens: Schema.Finite,
|
|
23
|
-
cache_read_input_tokens: Schema.Finite,
|
|
24
|
-
cache_creation_input_tokens: Schema.Finite,
|
|
25
|
-
output_tokens: Schema.Finite,
|
|
26
|
-
}),
|
|
27
|
-
});
|
|
28
|
-
const decodeResult = Schema.decodeUnknownEffect(Schema.fromJsonString(ClaudeResult));
|
|
29
|
-
|
|
30
|
-
class ClaudeCliWriter implements IWriter {
|
|
31
|
-
constructor(private readonly config: WriterConfig) {}
|
|
32
|
-
|
|
33
|
-
*write(input: { readonly system: string; readonly prompt: string }): Effect.fn.Return<Written, WriterError> {
|
|
34
|
-
const { config } = this;
|
|
35
|
-
// The system prompt goes in a file: a skill is longer than an argument should carry.
|
|
36
|
-
const systemFile = path.join(config.scratch, `system-${Bun.hash(input.system).toString(36)}.md`);
|
|
37
|
-
yield* Effect.try({
|
|
38
|
-
try: () => { fs.mkdirSync(config.scratch, { recursive: true }); if (!fs.existsSync(systemFile)) fs.writeFileSync(systemFile, input.system); },
|
|
39
|
-
catch: (e) => new WriterError({ reason: `the system prompt file could not be written: ${String(e)}` }),
|
|
40
|
-
});
|
|
41
|
-
const out = yield* Effect.tryPromise({
|
|
42
|
-
try: async () => {
|
|
43
|
-
const proc = Bun.spawn(
|
|
44
|
-
[config.claudeCommand, '-p', input.prompt, '--model', config.model, '--setting-sources', '', '--tools', '', '--no-session-persistence', '--output-format', 'json', '--system-prompt-file', systemFile],
|
|
45
|
-
{ cwd: config.scratch, stdout: 'pipe', stderr: 'pipe', env: childEnv(config.claudeConfigDir) },
|
|
46
|
-
);
|
|
47
|
-
const [stdout, stderr, code] = await Promise.all([new Response(proc.stdout).text(), new Response(proc.stderr).text(), proc.exited]);
|
|
48
|
-
return { stdout, stderr, code };
|
|
49
|
-
},
|
|
50
|
-
catch: (e) => new WriterError({ reason: `claude could not start: ${String(e)}` }),
|
|
51
|
-
});
|
|
52
|
-
const parsed = yield* Effect.mapError(decodeResult(out.stdout), () => new WriterError({ reason: `claude exited ${out.code}: ${(out.stdout + out.stderr).slice(0, 600)}` }));
|
|
53
|
-
if (parsed.is_error) return yield* Effect.fail(new WriterError({ reason: `claude failed: ${parsed.result.slice(0, 600)}` }));
|
|
54
|
-
return {
|
|
55
|
-
text: parsed.result,
|
|
56
|
-
spend: {
|
|
57
|
-
model: config.model,
|
|
58
|
-
inputTokens: parsed.usage.input_tokens,
|
|
59
|
-
cacheReadTokens: parsed.usage.cache_read_input_tokens,
|
|
60
|
-
cacheWriteTokens: parsed.usage.cache_creation_input_tokens,
|
|
61
|
-
outputTokens: parsed.usage.output_tokens,
|
|
62
|
-
costUsd: parsed.total_cost_usd,
|
|
63
|
-
},
|
|
64
|
-
};
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
export const WriterLive = (config: WriterConfig) => Layer.succeed(Writer, new ClaudeCliWriter(config));
|
|
69
|
-
|
|
70
|
-
/**
|
|
71
|
-
* The service's own environment with the account set, and none of the variables a running Claude Code session gives the
|
|
72
|
-
* processes it starts: a call made from inside a session would otherwise act as part of it.
|
|
73
|
-
*/
|
|
74
|
-
const childEnv = (claudeConfigDir: string): Record<string, string> => {
|
|
75
|
-
const env: Record<string, string> = {};
|
|
76
|
-
for (const [k, v] of Object.entries(process.env)) if (v !== undefined && !k.startsWith('CLAUDE_CODE') && k !== 'CLAUDECODE') env[k] = v;
|
|
77
|
-
env.CLAUDE_CONFIG_DIR = claudeConfigDir;
|
|
78
|
-
return env;
|
|
79
|
-
};
|
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* THE GENERATIVE MODEL, for the steps that must write text: drafting a skill edit and writing a reply under a skill
|
|
3
|
-
* version. Each call is a fresh, small request with only the text the step gives it: no tools, no settings, no history.
|
|
4
|
-
*/
|
|
5
|
-
import { Context, Data, type Effect } from 'effect';
|
|
6
|
-
|
|
7
|
-
/** What one call spent, as the model's API reported it, and its list price. */
|
|
8
|
-
export interface WriterSpend {
|
|
9
|
-
readonly model: string;
|
|
10
|
-
readonly inputTokens: number;
|
|
11
|
-
readonly cacheReadTokens: number;
|
|
12
|
-
readonly cacheWriteTokens: number;
|
|
13
|
-
readonly outputTokens: number;
|
|
14
|
-
readonly costUsd: number;
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
export interface Written {
|
|
18
|
-
readonly text: string;
|
|
19
|
-
readonly spend: WriterSpend;
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
export class WriterError extends Data.TaggedError('WriterError')<{ readonly reason: string }> {}
|
|
23
|
-
|
|
24
|
-
export interface IWriter {
|
|
25
|
-
/** `system` is the rules the text is written under (a skill version); `prompt` the one request. */
|
|
26
|
-
readonly write: (input: { readonly system: string; readonly prompt: string }) => Effect.fn.Return<Written, WriterError>;
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
export class Writer extends Context.Service<Writer, IWriter>()('Writer') {}
|
package/src/internal/sync.ts
DELETED
|
@@ -1,216 +0,0 @@
|
|
|
1
|
-
import { existsSync, mkdirSync, readdirSync, readFileSync, rmdirSync, rmSync, statSync, utimesSync, writeFileSync } from "node:fs";
|
|
2
|
-
import { dirname, join } from "node:path";
|
|
3
|
-
import { hashBytes, isLock, listLocalFiles, type Config, type State } from "./config";
|
|
4
|
-
import { invalidPathReason } from "./path";
|
|
5
|
-
import { fetchServer } from "./server";
|
|
6
|
-
import { skillOf, type ListFilesResponse, type PutFileRequest } from "./wire";
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* One sync cycle: take every newer edit the server has, then send every newer edit this folder has.
|
|
10
|
-
*
|
|
11
|
-
* The latest edit wins, always. Every file carries the time it was last edited: here, the file's
|
|
12
|
-
* modification time; on the server, the edit time of the version it holds. When this folder and the
|
|
13
|
-
* server disagree about a file, whichever was edited later is kept, on both sides. Nothing is merged and
|
|
14
|
-
* nothing is set aside, so every folder converges on the same bytes.
|
|
15
|
-
*
|
|
16
|
-
* A pulled file is given the server's edit time as its modification time, so the next cycle does not
|
|
17
|
-
* mistake the pull for a local edit.
|
|
18
|
-
*/
|
|
19
|
-
|
|
20
|
-
export interface Report {
|
|
21
|
-
readonly pulled: string[];
|
|
22
|
-
readonly pushed: string[];
|
|
23
|
-
readonly deletedLocally: string[];
|
|
24
|
-
/** Paths the server offered that this machine refused to write. */
|
|
25
|
-
readonly rejected: string[];
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
/** True when a report has nothing in it worth telling anyone. */
|
|
29
|
-
export function isQuiet(report: Report): boolean {
|
|
30
|
-
return [report.pulled, report.pushed, report.deletedLocally, report.rejected].every((list) => list.length === 0);
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
/**
|
|
34
|
-
* What a cycle changed, as the dashboard shows it under the folder: per direction, how many files in
|
|
35
|
-
* which skills ("pulled 64 files: agentic-collaboration 52, senior-swe 12"), never the file paths, which
|
|
36
|
-
* after a large pull fill the page. Empty when nothing changed.
|
|
37
|
-
*/
|
|
38
|
-
export function describeActivity(report: Report): string {
|
|
39
|
-
const direction = (verb: string, paths: ReadonlyArray<string>): string => {
|
|
40
|
-
if (paths.length === 0) return "";
|
|
41
|
-
const perSkill = new Map<string, number>();
|
|
42
|
-
for (const path of paths) perSkill.set(skillOf(path), (perSkill.get(skillOf(path)) ?? 0) + 1);
|
|
43
|
-
const skills = [...perSkill].sort(([a], [b]) => a.localeCompare(b)).map(([skill, files]) => `${skill} ${files}`);
|
|
44
|
-
return `${verb} ${paths.length} ${paths.length === 1 ? "file" : "files"}: ${skills.join(", ")}`;
|
|
45
|
-
};
|
|
46
|
-
return [
|
|
47
|
-
direction("pulled", report.pulled),
|
|
48
|
-
direction("pushed", report.pushed),
|
|
49
|
-
direction("deleted", report.deletedLocally),
|
|
50
|
-
].filter(Boolean).join("; ");
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
/**
|
|
54
|
-
* `skills` is the synced list. Every other skill in the folder is local to it: never pushed, never
|
|
55
|
-
* overwritten, never deleted by a sync.
|
|
56
|
-
*/
|
|
57
|
-
export async function syncOnce(config: Config, folder: string, state: State, skills: ReadonlySet<string>): Promise<Report> {
|
|
58
|
-
const report: Report = { pulled: [], pushed: [], deletedLocally: [], rejected: [] };
|
|
59
|
-
mkdirSync(folder, { recursive: true });
|
|
60
|
-
|
|
61
|
-
// A skill added to the list since last cycle may have server files older than the cursor; re-read from the start.
|
|
62
|
-
if ([...skills].some((skill) => !(state.skills ?? []).includes(skill))) state.cursor = 0;
|
|
63
|
-
state.skills = [...skills].sort();
|
|
64
|
-
// A skill removed from the list stops being tracked here. Left in `known`, its files would look deleted
|
|
65
|
-
// from this folder and the push would delete them on every machine.
|
|
66
|
-
for (const path of Object.keys(state.known)) {
|
|
67
|
-
if (!skills.has(skillOf(path)) || path.split("/").some(isLock)) delete state.known[path];
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
const present = new Set(listLocalFiles(folder, config.exclude).filter((path) => skills.has(skillOf(path))));
|
|
71
|
-
// Files deleted here since the last cycle. When they were deleted is not recorded anywhere, so the
|
|
72
|
-
// deletion counts as an edit made now: the pull leaves these paths alone and the push deletes them.
|
|
73
|
-
const deletedHere = new Set(Object.keys(state.known).filter((path) => !present.has(path)));
|
|
74
|
-
|
|
75
|
-
const ctx: Cycle = { config, folder, state, report, skills };
|
|
76
|
-
await pull(ctx, deletedHere, present);
|
|
77
|
-
await push(ctx, present);
|
|
78
|
-
return report;
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
interface Cycle {
|
|
82
|
-
readonly config: Config;
|
|
83
|
-
readonly folder: string;
|
|
84
|
-
readonly state: State;
|
|
85
|
-
readonly report: Report;
|
|
86
|
-
readonly skills: ReadonlySet<string>;
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
/** When this file was last edited here, to the millisecond the server stores. */
|
|
90
|
-
const editedAt = (file: string): number => Math.floor(statSync(file).mtimeMs);
|
|
91
|
-
|
|
92
|
-
async function pull(ctx: Cycle, deletedHere: ReadonlySet<string>, present: Set<string>): Promise<void> {
|
|
93
|
-
const { config, folder, state, report } = ctx;
|
|
94
|
-
const body = await (await call(config, `/files?since=${state.cursor}`)).json() as ListFilesResponse;
|
|
95
|
-
|
|
96
|
-
for (const file of body.files) {
|
|
97
|
-
// The server is not trusted to supply a path either: one bad row would otherwise write outside the
|
|
98
|
-
// skills directory on every machine that syncs.
|
|
99
|
-
const badPath = invalidPathReason(file.path);
|
|
100
|
-
if (badPath) {
|
|
101
|
-
report.rejected.push(`${file.path} (${badPath})`);
|
|
102
|
-
continue;
|
|
103
|
-
}
|
|
104
|
-
if (!ctx.skills.has(skillOf(file.path))) continue;
|
|
105
|
-
// Never written here: the file listing skips lock names, so a pulled one would read as deleted next cycle.
|
|
106
|
-
if (file.path.split("/").some(isLock)) continue;
|
|
107
|
-
// Deleted here, which is newer than anything the server has; the push sends the deletion.
|
|
108
|
-
if (deletedHere.has(file.path)) continue;
|
|
109
|
-
|
|
110
|
-
const local = join(folder, file.path);
|
|
111
|
-
const exists = existsSync(local);
|
|
112
|
-
const localHash = exists ? hashBytes(readFileSync(local)) : undefined;
|
|
113
|
-
if (localHash === (file.hash ?? undefined)) {
|
|
114
|
-
if (file.hash === null) delete state.known[file.path];
|
|
115
|
-
else state.known[file.path] = file.hash;
|
|
116
|
-
continue;
|
|
117
|
-
}
|
|
118
|
-
// Edited here since this folder last agreed with the server, and later than the server's version:
|
|
119
|
-
// this folder's edit wins, and the push sends it.
|
|
120
|
-
if (exists && localHash !== state.known[file.path] && editedAt(local) > file.editedAt) continue;
|
|
121
|
-
|
|
122
|
-
await apply(ctx, file, present);
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
state.cursor = body.cursor;
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
async function push(ctx: Cycle, present: Set<string>): Promise<void> {
|
|
129
|
-
const { config, folder, state, report } = ctx;
|
|
130
|
-
for (const path of [...present]) {
|
|
131
|
-
const file = join(folder, path);
|
|
132
|
-
const bytes = readFileSync(file);
|
|
133
|
-
const hash = hashBytes(bytes);
|
|
134
|
-
if (state.known[path] === hash) continue;
|
|
135
|
-
const later = await put(config, { path, hash, content: Buffer.from(bytes).toString("base64"), editedAt: editedAt(file) });
|
|
136
|
-
if (later === undefined) {
|
|
137
|
-
state.known[path] = hash;
|
|
138
|
-
report.pushed.push(path);
|
|
139
|
-
} else {
|
|
140
|
-
await apply(ctx, { path, ...later }, present);
|
|
141
|
-
}
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
// A path this folder agreed on that is no longer on disk was deleted here. The server keeps the
|
|
145
|
-
// deletion, or the next machine to sync would upload the file again.
|
|
146
|
-
for (const path of Object.keys(state.known)) {
|
|
147
|
-
if (present.has(path)) continue;
|
|
148
|
-
const later = await put(config, { path, hash: null, content: null, editedAt: Date.now() });
|
|
149
|
-
if (later === undefined) {
|
|
150
|
-
delete state.known[path];
|
|
151
|
-
report.pushed.push(`${path} (deleted)`);
|
|
152
|
-
} else {
|
|
153
|
-
await apply(ctx, { path, ...later }, present);
|
|
154
|
-
}
|
|
155
|
-
}
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
/** The server's version of a file, as it answers a refused write. */
|
|
159
|
-
interface ServerVersion {
|
|
160
|
-
readonly hash: string | null;
|
|
161
|
-
readonly editedAt: number;
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
/**
|
|
165
|
-
* Writes the server's version of a file here: its bytes with its edit time, or a deletion. Used by the
|
|
166
|
-
* pull, and by the push when the server refuses a write because it holds a later edit, so a refused write
|
|
167
|
-
* settles at once rather than waiting for a pull that may never list the file again.
|
|
168
|
-
*/
|
|
169
|
-
async function apply(ctx: Cycle, file: ServerVersion & { readonly path: string }, present: Set<string>): Promise<void> {
|
|
170
|
-
const { config, folder, state, report } = ctx;
|
|
171
|
-
const local = join(folder, file.path);
|
|
172
|
-
if (file.hash === null) {
|
|
173
|
-
rmSync(local, { force: true });
|
|
174
|
-
removeEmptyParents(folder, local);
|
|
175
|
-
present.delete(file.path);
|
|
176
|
-
delete state.known[file.path];
|
|
177
|
-
report.deletedLocally.push(file.path);
|
|
178
|
-
return;
|
|
179
|
-
}
|
|
180
|
-
const content = new Uint8Array(await (await call(config, `/content/${file.hash}`)).arrayBuffer());
|
|
181
|
-
mkdirSync(dirname(local), { recursive: true });
|
|
182
|
-
writeFileSync(local, content);
|
|
183
|
-
utimesSync(local, new Date(), new Date(file.editedAt));
|
|
184
|
-
state.known[file.path] = file.hash;
|
|
185
|
-
present.add(file.path);
|
|
186
|
-
report.pulled.push(file.path);
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
/** A deleted skill leaves no empty directory behind, which would still show up as a skill. */
|
|
190
|
-
function removeEmptyParents(folder: string, file: string): void {
|
|
191
|
-
for (let dir = dirname(file); dir !== folder && dir.startsWith(folder); dir = dirname(dir)) {
|
|
192
|
-
if (readdirSync(dir).length > 0) return;
|
|
193
|
-
rmdirSync(dir);
|
|
194
|
-
}
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
/** Undefined when the server took the write; otherwise the later version it holds instead. */
|
|
198
|
-
async function put(config: Config, request: PutFileRequest): Promise<ServerVersion | undefined> {
|
|
199
|
-
const response = await fetchServer(config, "/files", {
|
|
200
|
-
method: "PUT",
|
|
201
|
-
headers: { "content-type": "application/json" },
|
|
202
|
-
body: JSON.stringify(request),
|
|
203
|
-
});
|
|
204
|
-
if (response.status === 409) {
|
|
205
|
-
const current = await response.json() as { currentHash: string | null; currentEditedAt: number };
|
|
206
|
-
return { hash: current.currentHash, editedAt: current.currentEditedAt };
|
|
207
|
-
}
|
|
208
|
-
if (!response.ok) throw new Error(`PUT /files ${request.path}: ${response.status} ${await response.text()}`);
|
|
209
|
-
return undefined;
|
|
210
|
-
}
|
|
211
|
-
|
|
212
|
-
async function call(config: Config, path: string): Promise<Response> {
|
|
213
|
-
const response = await fetchServer(config, path);
|
|
214
|
-
if (!response.ok) throw new Error(`GET ${path}: ${response.status} ${await response.text()}`);
|
|
215
|
-
return response;
|
|
216
|
-
}
|