-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathsources-config.ts
More file actions
421 lines (379 loc) · 14.7 KB
/
Copy pathsources-config.ts
File metadata and controls
421 lines (379 loc) · 14.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
import { createHash, randomUUID } from "node:crypto";
import { existsSync, mkdirSync, readFileSync, renameSync, rmSync, statSync, writeFileSync } from "node:fs";
import { homedir } from "node:os";
import { dirname, resolve } from "node:path";
export type SignetSourceKind = "obsidian" | "github";
export type SignetSourceMode = "read-only";
export interface GitHubSourceSettings {
readonly repos: readonly string[];
readonly tokenRef?: string;
readonly resourceTypes: readonly ("issues" | "pulls" | "discussions" | "docs")[];
readonly state?: "open" | "closed" | "all";
readonly includeComments?: boolean;
readonly labels?: readonly string[];
readonly docPaths?: readonly string[];
readonly maxItemsPerRepo?: number;
}
export const DEFAULT_GITHUB_RESOURCE_TYPES = ["issues", "pulls", "discussions", "docs"] as const;
export const DEFAULT_GITHUB_DOC_PATHS = ["README.md", "CHANGELOG.md"] as const;
export interface SignetSourceEntry {
readonly id: string;
readonly kind: SignetSourceKind;
readonly name: string;
readonly root: string;
readonly enabled: boolean;
readonly mode: SignetSourceMode;
readonly createdAt: string;
readonly updatedAt: string;
readonly lastIndexedAt?: string;
readonly excludeGlobs?: readonly string[];
readonly settings?: Readonly<Record<string, unknown>>;
}
export const DEFAULT_OBSIDIAN_EXCLUDE_GLOBS = [
"**/.obsidian/**",
"**/.trash/**",
"**/.hermes/**",
"**/.*/**",
"**/.*",
] as const;
export interface SignetSourcesConfig {
readonly version: 1;
readonly sources: readonly SignetSourceEntry[];
}
export interface AddObsidianSourceInput {
readonly root: string;
readonly name?: string;
readonly excludeGlobs?: readonly string[];
readonly now?: string;
}
export interface AddGitHubSourceInput {
readonly repos: readonly string[];
readonly name?: string;
readonly tokenRef?: string;
readonly resourceTypes?: readonly ("issues" | "pulls" | "discussions" | "docs")[];
readonly state?: "open" | "closed" | "all";
readonly includeComments?: boolean;
readonly labels?: readonly string[];
readonly docPaths?: readonly string[];
readonly maxItemsPerRepo?: number;
readonly now?: string;
}
export type AddSourceResult =
| { readonly ok: true; readonly source: SignetSourceEntry; readonly created: boolean }
| { readonly ok: false; readonly error: string };
export type RemoveSourceResult =
| { readonly ok: true; readonly source: SignetSourceEntry }
| { readonly ok: false; readonly error: string };
const SOURCES_CONFIG_VERSION = 1;
export function getAgentsDir(): string {
return process.env.SIGNET_PATH || `${homedir()}/.agents`;
}
export function getSourcesConfigPath(agentsDir = getAgentsDir()): string {
return `${agentsDir.replace(/\/$/, "")}/sources.json`;
}
export function loadSourcesConfig(agentsDir = getAgentsDir()): SignetSourcesConfig {
const path = getSourcesConfigPath(agentsDir);
if (!existsSync(path)) return emptyConfig();
try {
const parsed = JSON.parse(readFileSync(path, "utf8")) as unknown;
if (!isRecord(parsed) || parsed.version !== SOURCES_CONFIG_VERSION || !Array.isArray(parsed.sources)) {
return emptyConfig();
}
return {
version: SOURCES_CONFIG_VERSION,
sources: parsed.sources.filter(isSourceEntry),
};
} catch {
return emptyConfig();
}
}
export function saveSourcesConfig(config: SignetSourcesConfig, agentsDir = getAgentsDir()): void {
const path = getSourcesConfigPath(agentsDir);
mkdirSync(dirname(path), { recursive: true });
const tmp = `${path}.tmp-${process.pid}-${randomUUID()}`;
writeFileSync(tmp, `${JSON.stringify(config, null, 2)}\n`, "utf8");
renameSync(tmp, path);
}
function loadSourcesConfigForWrite(agentsDir = getAgentsDir()): SignetSourcesConfig {
const path = getSourcesConfigPath(agentsDir);
if (!existsSync(path)) return emptyConfig();
let parsed: unknown;
try {
parsed = JSON.parse(readFileSync(path, "utf8")) as unknown;
} catch (err) {
const detail = err instanceof Error ? err.message : String(err);
throw new Error(`Sources config is not readable JSON; refusing to overwrite ${path}: ${detail}`);
}
if (!isRecord(parsed) || parsed.version !== SOURCES_CONFIG_VERSION || !Array.isArray(parsed.sources)) {
throw new Error(`Sources config is invalid; refusing to overwrite ${path}`);
}
if (!parsed.sources.every(isSourceEntry)) {
throw new Error(`Sources config contains invalid source entries; refusing to overwrite ${path}`);
}
return { version: SOURCES_CONFIG_VERSION, sources: parsed.sources };
}
export function addObsidianSource(input: AddObsidianSourceInput, agentsDir = getAgentsDir()): AddSourceResult {
return withSourcesConfigLock(agentsDir, () => addObsidianSourceUnlocked(input, agentsDir));
}
function addObsidianSourceUnlocked(input: AddObsidianSourceInput, agentsDir = getAgentsDir()): AddSourceResult {
try {
return addObsidianSourceChecked(input, agentsDir);
} catch (err) {
const detail = err instanceof Error ? err.message : String(err);
return { ok: false, error: detail };
}
}
function addObsidianSourceChecked(input: AddObsidianSourceInput, agentsDir = getAgentsDir()): AddSourceResult {
const trimmedRoot = input.root.trim();
if (!trimmedRoot) return { ok: false, error: "Obsidian vault path is required" };
const root = resolve(trimmedRoot);
if (!existsSync(root)) return { ok: false, error: `Obsidian vault path does not exist: ${root}` };
try {
if (!statSync(root).isDirectory()) return { ok: false, error: `Obsidian vault path must be a directory: ${root}` };
} catch {
return { ok: false, error: `Obsidian vault path is not accessible: ${root}` };
}
const now = input.now ?? new Date().toISOString();
const cfg = loadSourcesConfigForWrite(agentsDir);
const existing = cfg.sources.find((source) => source.kind === "obsidian" && source.root === root);
if (existing) {
const updated = {
...existing,
name: cleanName(input.name) ?? existing.name,
excludeGlobs: input.excludeGlobs
? mergeDefaultObsidianExcludeGlobs(input.excludeGlobs)
: (existing.excludeGlobs ?? [...DEFAULT_OBSIDIAN_EXCLUDE_GLOBS]),
enabled: true,
updatedAt: now,
};
saveSourcesConfig(
{
version: SOURCES_CONFIG_VERSION,
sources: cfg.sources.map((source) => (source.id === existing.id ? updated : source)),
},
agentsDir,
);
return { ok: true, source: updated, created: false };
}
const source: SignetSourceEntry = {
id: `obsidian:${createHash("sha256").update(root).digest("hex").slice(0, 16)}`,
kind: "obsidian",
name: cleanName(input.name) ?? "Obsidian Vault",
root,
enabled: true,
mode: "read-only",
createdAt: now,
updatedAt: now,
excludeGlobs: mergeDefaultObsidianExcludeGlobs(input.excludeGlobs),
};
saveSourcesConfig({ version: SOURCES_CONFIG_VERSION, sources: [...cfg.sources, source] }, agentsDir);
return { ok: true, source, created: true };
}
export function addGitHubSource(input: AddGitHubSourceInput, agentsDir = getAgentsDir()): AddSourceResult {
return withSourcesConfigLock(agentsDir, () => addGitHubSourceUnlocked(input, agentsDir));
}
function addGitHubSourceUnlocked(input: AddGitHubSourceInput, agentsDir = getAgentsDir()): AddSourceResult {
try {
return addGitHubSourceChecked(input, agentsDir);
} catch (err) {
const detail = err instanceof Error ? err.message : String(err);
return { ok: false, error: detail };
}
}
function addGitHubSourceChecked(input: AddGitHubSourceInput, agentsDir = getAgentsDir()): AddSourceResult {
const repos = input.repos.map((r) => r.trim()).filter(Boolean);
if (repos.length === 0) return { ok: false, error: "At least one repo (owner/repo or owner/*) is required" };
for (const repo of repos) {
if (!/^[a-zA-Z0-9_.-]+\/[a-zA-Z0-9_*.-]+$/.test(repo)) {
return { ok: false, error: `Invalid repo pattern: ${repo}. Expected owner/repo or owner/*` };
}
}
const now = input.now ?? new Date().toISOString();
const cfg = loadSourcesConfigForWrite(agentsDir);
const settingsKey = repos.sort().join(",");
const existing = cfg.sources.find(
(source) => source.kind === "github" && (source.settings?.repos as string[])?.sort().join(",") === settingsKey,
);
if (existing) {
const updated: SignetSourceEntry = {
...existing,
name: cleanName(input.name) ?? existing.name,
enabled: true,
updatedAt: now,
settings: buildGitHubSettings(input),
};
saveSourcesConfig(
{
version: SOURCES_CONFIG_VERSION,
sources: cfg.sources.map((source) => (source.id === existing.id ? updated : source)),
},
agentsDir,
);
return { ok: true, source: updated, created: false };
}
const source: SignetSourceEntry = {
id: `github:${createHash("sha256").update(settingsKey).digest("hex").slice(0, 16)}`,
kind: "github",
name: cleanName(input.name) ?? repos[0],
root: "",
enabled: true,
mode: "read-only",
createdAt: now,
updatedAt: now,
settings: buildGitHubSettings(input),
};
saveSourcesConfig({ version: SOURCES_CONFIG_VERSION, sources: [...cfg.sources, source] }, agentsDir);
return { ok: true, source, created: true };
}
function buildGitHubSettings(input: AddGitHubSourceInput): Readonly<Record<string, unknown>> {
return {
repos: input.repos,
tokenRef: input.tokenRef,
resourceTypes: input.resourceTypes ?? [...DEFAULT_GITHUB_RESOURCE_TYPES],
state: input.state ?? "all",
includeComments: input.includeComments ?? true,
labels: input.labels,
docPaths: input.docPaths ?? [...DEFAULT_GITHUB_DOC_PATHS],
maxItemsPerRepo: input.maxItemsPerRepo ?? 500,
};
}
export function parseGitHubSettings(raw: Readonly<Record<string, unknown>> | undefined): GitHubSourceSettings {
if (!raw) {
return { repos: [], resourceTypes: [...DEFAULT_GITHUB_RESOURCE_TYPES] };
}
const repos =
Array.isArray(raw.repos) && raw.repos.every((r) => typeof r === "string") ? (raw.repos as string[]) : [];
const resourceTypes =
Array.isArray(raw.resourceTypes) && raw.resourceTypes.every((t) => typeof t === "string")
? (raw.resourceTypes as string[]).filter((t): t is "issues" | "pulls" | "discussions" | "docs" =>
["issues", "pulls", "discussions", "docs"].includes(t),
)
: [...DEFAULT_GITHUB_RESOURCE_TYPES];
return {
repos,
tokenRef: typeof raw.tokenRef === "string" ? raw.tokenRef : undefined,
resourceTypes,
state: raw.state === "open" || raw.state === "closed" || raw.state === "all" ? raw.state : "all",
includeComments: typeof raw.includeComments === "boolean" ? raw.includeComments : true,
labels:
Array.isArray(raw.labels) && raw.labels.every((l) => typeof l === "string")
? (raw.labels as string[])
: undefined,
docPaths:
Array.isArray(raw.docPaths) && raw.docPaths.every((p) => typeof p === "string")
? (raw.docPaths as string[])
: [...DEFAULT_GITHUB_DOC_PATHS],
maxItemsPerRepo: typeof raw.maxItemsPerRepo === "number" && raw.maxItemsPerRepo > 0 ? raw.maxItemsPerRepo : 500,
};
}
export function markSourceIndexed(
sourceId: string,
indexedAt = new Date().toISOString(),
agentsDir = getAgentsDir(),
): void {
withSourcesConfigLock(agentsDir, () => markSourceIndexedUnlocked(sourceId, indexedAt, agentsDir));
}
function markSourceIndexedUnlocked(
sourceId: string,
indexedAt = new Date().toISOString(),
agentsDir = getAgentsDir(),
): void {
const cfg = loadSourcesConfigForWrite(agentsDir);
saveSourcesConfig(
{
version: SOURCES_CONFIG_VERSION,
sources: cfg.sources.map((source) =>
source.id === sourceId ? { ...source, lastIndexedAt: indexedAt, updatedAt: indexedAt } : source,
),
},
agentsDir,
);
}
export function removeSource(sourceId: string, agentsDir = getAgentsDir()): RemoveSourceResult {
return withSourcesConfigLock(agentsDir, () => removeSourceUnlocked(sourceId, agentsDir));
}
function removeSourceUnlocked(sourceId: string, agentsDir = getAgentsDir()): RemoveSourceResult {
try {
return removeSourceChecked(sourceId, agentsDir);
} catch (err) {
const detail = err instanceof Error ? err.message : String(err);
return { ok: false, error: detail };
}
}
function removeSourceChecked(sourceId: string, agentsDir = getAgentsDir()): RemoveSourceResult {
const id = sourceId.trim();
if (!id) return { ok: false, error: "Source id is required" };
const cfg = loadSourcesConfigForWrite(agentsDir);
const source = cfg.sources.find((entry) => entry.id === id);
if (!source) return { ok: false, error: `Source not found: ${id}` };
saveSourcesConfig(
{
version: SOURCES_CONFIG_VERSION,
sources: cfg.sources.filter((entry) => entry.id !== id),
},
agentsDir,
);
return { ok: true, source };
}
function emptyConfig(): SignetSourcesConfig {
return { version: SOURCES_CONFIG_VERSION, sources: [] };
}
function withSourcesConfigLock<T>(agentsDir: string, fn: () => T): T {
const configPath = getSourcesConfigPath(agentsDir);
mkdirSync(dirname(configPath), { recursive: true });
const lockDir = `${configPath}.lock`;
let locked = false;
for (let attempt = 0; attempt < 500; attempt++) {
try {
mkdirSync(lockDir);
locked = true;
break;
} catch (err) {
if (!isFileExistsError(err)) throw err;
Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, 10);
}
}
if (!locked) throw new Error(`Timed out waiting for Sources config lock: ${lockDir}`);
try {
return fn();
} finally {
rmSync(lockDir, { recursive: true, force: true });
}
}
function isFileExistsError(err: unknown): boolean {
return typeof err === "object" && err !== null && "code" in err && (err as { code?: unknown }).code === "EEXIST";
}
function cleanName(value: string | undefined): string | null {
const trimmed = value?.trim();
return trimmed && trimmed.length > 0 ? trimmed : null;
}
function cleanExcludeGlobs(values: readonly string[] | undefined): readonly string[] | null {
if (!values) return null;
const cleaned = Array.from(new Set(values.map((value) => value.trim()).filter(Boolean)));
return cleaned.length > 0 ? cleaned : [];
}
function mergeDefaultObsidianExcludeGlobs(values: readonly string[] | undefined): readonly string[] {
return [...DEFAULT_OBSIDIAN_EXCLUDE_GLOBS, ...(cleanExcludeGlobs(values) ?? [])].filter(
(value, index, all) => all.indexOf(value) === index,
);
}
function isRecord(value: unknown): value is Record<string, unknown> {
return typeof value === "object" && value !== null && !Array.isArray(value);
}
function isSourceEntry(value: unknown): value is SignetSourceEntry {
return (
isRecord(value) &&
(value.kind === "obsidian" || value.kind === "github") &&
typeof value.id === "string" &&
typeof value.name === "string" &&
typeof value.root === "string" &&
typeof value.enabled === "boolean" &&
value.mode === "read-only" &&
typeof value.createdAt === "string" &&
typeof value.updatedAt === "string" &&
(value.lastIndexedAt === undefined || typeof value.lastIndexedAt === "string") &&
(value.excludeGlobs === undefined ||
(Array.isArray(value.excludeGlobs) && value.excludeGlobs.every((entry) => typeof entry === "string"))) &&
(value.settings === undefined || isRecord(value.settings))
);
}