@clinebot/core 0.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +88 -0
- package/dist/account/cline-account-service.d.ts +34 -0
- package/dist/account/index.d.ts +3 -0
- package/dist/account/rpc.d.ts +38 -0
- package/dist/account/types.d.ts +74 -0
- package/dist/agents/agent-config-loader.d.ts +18 -0
- package/dist/agents/agent-config-parser.d.ts +25 -0
- package/dist/agents/hooks-config-loader.d.ts +23 -0
- package/dist/agents/index.d.ts +11 -0
- package/dist/agents/plugin-config-loader.d.ts +22 -0
- package/dist/agents/plugin-loader.d.ts +9 -0
- package/dist/agents/plugin-sandbox.d.ts +12 -0
- package/dist/agents/unified-config-file-watcher.d.ts +77 -0
- package/dist/agents/user-instruction-config-loader.d.ts +63 -0
- package/dist/auth/client.d.ts +11 -0
- package/dist/auth/cline.d.ts +41 -0
- package/dist/auth/codex.d.ts +39 -0
- package/dist/auth/oca.d.ts +22 -0
- package/dist/auth/server.d.ts +22 -0
- package/dist/auth/types.d.ts +72 -0
- package/dist/auth/utils.d.ts +32 -0
- package/dist/chat/chat-schema.d.ts +145 -0
- package/dist/default-tools/constants.d.ts +23 -0
- package/dist/default-tools/definitions.d.ts +96 -0
- package/dist/default-tools/executors/apply-patch-parser.d.ts +68 -0
- package/dist/default-tools/executors/apply-patch.d.ts +26 -0
- package/dist/default-tools/executors/bash.d.ts +49 -0
- package/dist/default-tools/executors/editor.d.ts +31 -0
- package/dist/default-tools/executors/file-read.d.ts +40 -0
- package/dist/default-tools/executors/index.d.ts +44 -0
- package/dist/default-tools/executors/search.d.ts +50 -0
- package/dist/default-tools/executors/web-fetch.d.ts +58 -0
- package/dist/default-tools/index.d.ts +57 -0
- package/dist/default-tools/presets.d.ts +124 -0
- package/dist/default-tools/schemas.d.ts +121 -0
- package/dist/default-tools/types.d.ts +237 -0
- package/dist/index.d.ts +23 -0
- package/dist/index.js +220 -0
- package/dist/input/file-indexer.d.ts +5 -0
- package/dist/input/index.d.ts +4 -0
- package/dist/input/mention-enricher.d.ts +12 -0
- package/dist/mcp/config-loader.d.ts +15 -0
- package/dist/mcp/index.d.ts +4 -0
- package/dist/mcp/manager.d.ts +24 -0
- package/dist/mcp/types.d.ts +66 -0
- package/dist/runtime/hook-file-hooks.d.ts +18 -0
- package/dist/runtime/rules.d.ts +5 -0
- package/dist/runtime/runtime-builder.d.ts +5 -0
- package/dist/runtime/sandbox/subprocess-sandbox.d.ts +19 -0
- package/dist/runtime/session-runtime.d.ts +36 -0
- package/dist/runtime/tool-approval.d.ts +9 -0
- package/dist/runtime/workflows.d.ts +13 -0
- package/dist/server/index.d.ts +47 -0
- package/dist/server/index.js +641 -0
- package/dist/session/default-session-manager.d.ts +77 -0
- package/dist/session/rpc-session-service.d.ts +12 -0
- package/dist/session/runtime-oauth-token-manager.d.ts +28 -0
- package/dist/session/session-artifacts.d.ts +19 -0
- package/dist/session/session-graph.d.ts +15 -0
- package/dist/session/session-host.d.ts +21 -0
- package/dist/session/session-manager.d.ts +50 -0
- package/dist/session/session-manifest.d.ts +30 -0
- package/dist/session/session-service.d.ts +113 -0
- package/dist/session/sqlite-rpc-session-backend.d.ts +30 -0
- package/dist/session/unified-session-persistence-service.d.ts +93 -0
- package/dist/session/workspace-manager.d.ts +28 -0
- package/dist/session/workspace-manifest.d.ts +25 -0
- package/dist/storage/provider-settings-legacy-migration.d.ts +13 -0
- package/dist/storage/provider-settings-manager.d.ts +20 -0
- package/dist/storage/sqlite-session-store.d.ts +29 -0
- package/dist/storage/sqlite-team-store.d.ts +31 -0
- package/dist/storage/team-store.d.ts +2 -0
- package/dist/team/index.d.ts +1 -0
- package/dist/team/projections.d.ts +8 -0
- package/dist/types/common.d.ts +10 -0
- package/dist/types/config.d.ts +37 -0
- package/dist/types/events.d.ts +54 -0
- package/dist/types/provider-settings.d.ts +20 -0
- package/dist/types/sessions.d.ts +9 -0
- package/dist/types/storage.d.ts +37 -0
- package/dist/types/workspace.d.ts +7 -0
- package/dist/types.d.ts +26 -0
- package/package.json +63 -0
- package/src/account/cline-account-service.test.ts +101 -0
- package/src/account/cline-account-service.ts +267 -0
- package/src/account/index.ts +20 -0
- package/src/account/rpc.test.ts +62 -0
- package/src/account/rpc.ts +172 -0
- package/src/account/types.ts +80 -0
- package/src/agents/agent-config-loader.test.ts +234 -0
- package/src/agents/agent-config-loader.ts +107 -0
- package/src/agents/agent-config-parser.ts +191 -0
- package/src/agents/hooks-config-loader.ts +97 -0
- package/src/agents/index.ts +84 -0
- package/src/agents/plugin-config-loader.test.ts +91 -0
- package/src/agents/plugin-config-loader.ts +160 -0
- package/src/agents/plugin-loader.test.ts +102 -0
- package/src/agents/plugin-loader.ts +105 -0
- package/src/agents/plugin-sandbox.test.ts +120 -0
- package/src/agents/plugin-sandbox.ts +471 -0
- package/src/agents/unified-config-file-watcher.test.ts +196 -0
- package/src/agents/unified-config-file-watcher.ts +483 -0
- package/src/agents/user-instruction-config-loader.test.ts +158 -0
- package/src/agents/user-instruction-config-loader.ts +438 -0
- package/src/auth/client.test.ts +40 -0
- package/src/auth/client.ts +25 -0
- package/src/auth/cline.test.ts +130 -0
- package/src/auth/cline.ts +414 -0
- package/src/auth/codex.test.ts +170 -0
- package/src/auth/codex.ts +466 -0
- package/src/auth/oca.test.ts +215 -0
- package/src/auth/oca.ts +546 -0
- package/src/auth/server.ts +216 -0
- package/src/auth/types.ts +78 -0
- package/src/auth/utils.test.ts +128 -0
- package/src/auth/utils.ts +247 -0
- package/src/chat/chat-schema.ts +82 -0
- package/src/default-tools/constants.ts +35 -0
- package/src/default-tools/definitions.test.ts +233 -0
- package/src/default-tools/definitions.ts +632 -0
- package/src/default-tools/executors/apply-patch-parser.ts +520 -0
- package/src/default-tools/executors/apply-patch.ts +359 -0
- package/src/default-tools/executors/bash.ts +205 -0
- package/src/default-tools/executors/editor.ts +231 -0
- package/src/default-tools/executors/file-read.test.ts +25 -0
- package/src/default-tools/executors/file-read.ts +94 -0
- package/src/default-tools/executors/index.ts +75 -0
- package/src/default-tools/executors/search.ts +278 -0
- package/src/default-tools/executors/web-fetch.ts +259 -0
- package/src/default-tools/index.ts +161 -0
- package/src/default-tools/presets.test.ts +63 -0
- package/src/default-tools/presets.ts +168 -0
- package/src/default-tools/schemas.ts +228 -0
- package/src/default-tools/types.ts +324 -0
- package/src/index.ts +119 -0
- package/src/input/file-indexer.d.ts +11 -0
- package/src/input/file-indexer.test.ts +87 -0
- package/src/input/file-indexer.ts +280 -0
- package/src/input/index.ts +7 -0
- package/src/input/mention-enricher.test.ts +82 -0
- package/src/input/mention-enricher.ts +119 -0
- package/src/mcp/config-loader.test.ts +238 -0
- package/src/mcp/config-loader.ts +219 -0
- package/src/mcp/index.ts +26 -0
- package/src/mcp/manager.test.ts +106 -0
- package/src/mcp/manager.ts +262 -0
- package/src/mcp/types.ts +88 -0
- package/src/runtime/hook-file-hooks.test.ts +106 -0
- package/src/runtime/hook-file-hooks.ts +736 -0
- package/src/runtime/index.ts +27 -0
- package/src/runtime/rules.ts +34 -0
- package/src/runtime/runtime-builder.team-persistence.test.ts +203 -0
- package/src/runtime/runtime-builder.test.ts +215 -0
- package/src/runtime/runtime-builder.ts +515 -0
- package/src/runtime/runtime-parity.test.ts +132 -0
- package/src/runtime/sandbox/subprocess-sandbox.ts +207 -0
- package/src/runtime/session-runtime.ts +44 -0
- package/src/runtime/tool-approval.ts +104 -0
- package/src/runtime/workflows.test.ts +119 -0
- package/src/runtime/workflows.ts +54 -0
- package/src/server/index.ts +282 -0
- package/src/session/default-session-manager.e2e.test.ts +354 -0
- package/src/session/default-session-manager.test.ts +816 -0
- package/src/session/default-session-manager.ts +1286 -0
- package/src/session/index.ts +37 -0
- package/src/session/rpc-session-service.ts +189 -0
- package/src/session/runtime-oauth-token-manager.test.ts +137 -0
- package/src/session/runtime-oauth-token-manager.ts +265 -0
- package/src/session/session-artifacts.ts +106 -0
- package/src/session/session-graph.ts +90 -0
- package/src/session/session-host.ts +190 -0
- package/src/session/session-manager.ts +56 -0
- package/src/session/session-manifest.ts +29 -0
- package/src/session/session-service.team-persistence.test.ts +48 -0
- package/src/session/session-service.ts +610 -0
- package/src/session/sqlite-rpc-session-backend.ts +303 -0
- package/src/session/unified-session-persistence-service.ts +781 -0
- package/src/session/workspace-manager.ts +98 -0
- package/src/session/workspace-manifest.ts +100 -0
- package/src/storage/artifact-store.ts +1 -0
- package/src/storage/index.ts +11 -0
- package/src/storage/provider-settings-legacy-migration.test.ts +175 -0
- package/src/storage/provider-settings-legacy-migration.ts +637 -0
- package/src/storage/provider-settings-manager.test.ts +111 -0
- package/src/storage/provider-settings-manager.ts +129 -0
- package/src/storage/session-store.ts +1 -0
- package/src/storage/sqlite-session-store.ts +270 -0
- package/src/storage/sqlite-team-store.ts +443 -0
- package/src/storage/team-store.ts +5 -0
- package/src/team/index.ts +4 -0
- package/src/team/projections.ts +285 -0
- package/src/types/common.ts +14 -0
- package/src/types/config.ts +64 -0
- package/src/types/events.ts +46 -0
- package/src/types/index.ts +24 -0
- package/src/types/provider-settings.ts +43 -0
- package/src/types/sessions.ts +16 -0
- package/src/types/storage.ts +64 -0
- package/src/types/workspace.ts +7 -0
- package/src/types.ts +127 -0
|
@@ -0,0 +1,483 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import { type FSWatcher, watch } from "node:fs";
|
|
3
|
+
import { readdir, readFile } from "node:fs/promises";
|
|
4
|
+
import { join } from "node:path";
|
|
5
|
+
|
|
6
|
+
export interface UnifiedConfigFileContext<TType extends string = string> {
|
|
7
|
+
type: TType;
|
|
8
|
+
directoryPath: string;
|
|
9
|
+
fileName: string;
|
|
10
|
+
filePath: string;
|
|
11
|
+
content: string;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export interface UnifiedConfigFileCandidate {
|
|
15
|
+
directoryPath: string;
|
|
16
|
+
fileName: string;
|
|
17
|
+
filePath: string;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export interface UnifiedConfigDefinition<
|
|
21
|
+
TType extends string = string,
|
|
22
|
+
TItem = unknown,
|
|
23
|
+
> {
|
|
24
|
+
type: TType;
|
|
25
|
+
directories: ReadonlyArray<string>;
|
|
26
|
+
discoverFiles?: (
|
|
27
|
+
directoryPath: string,
|
|
28
|
+
) => Promise<ReadonlyArray<UnifiedConfigFileCandidate>>;
|
|
29
|
+
includeFile?: (fileName: string, filePath: string) => boolean;
|
|
30
|
+
parseFile: (context: UnifiedConfigFileContext<TType>) => TItem;
|
|
31
|
+
resolveId: (item: TItem, context: UnifiedConfigFileContext<TType>) => string;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export interface UnifiedConfigWatcherOptions {
|
|
35
|
+
debounceMs?: number;
|
|
36
|
+
emitParseErrors?: boolean;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export interface UnifiedConfigRecord<
|
|
40
|
+
TType extends string = string,
|
|
41
|
+
TItem = unknown,
|
|
42
|
+
> {
|
|
43
|
+
type: TType;
|
|
44
|
+
id: string;
|
|
45
|
+
item: TItem;
|
|
46
|
+
filePath: string;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export type UnifiedConfigWatcherEvent<
|
|
50
|
+
TType extends string = string,
|
|
51
|
+
TItem = unknown,
|
|
52
|
+
> =
|
|
53
|
+
| {
|
|
54
|
+
kind: "upsert";
|
|
55
|
+
record: UnifiedConfigRecord<TType, TItem>;
|
|
56
|
+
}
|
|
57
|
+
| {
|
|
58
|
+
kind: "remove";
|
|
59
|
+
type: TType;
|
|
60
|
+
id: string;
|
|
61
|
+
filePath: string;
|
|
62
|
+
}
|
|
63
|
+
| {
|
|
64
|
+
kind: "error";
|
|
65
|
+
type: TType;
|
|
66
|
+
error: unknown;
|
|
67
|
+
filePath?: string;
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
interface InternalRecord<TType extends string, TItem>
|
|
71
|
+
extends UnifiedConfigRecord<TType, TItem> {
|
|
72
|
+
fingerprint: string;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
function toFingerprint(content: string): string {
|
|
76
|
+
return createHash("sha1").update(content).digest("hex");
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
function isErrnoException(error: unknown): error is NodeJS.ErrnoException {
|
|
80
|
+
return Boolean(error && typeof error === "object" && "code" in error);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
function isMissingDirectoryError(error: unknown): boolean {
|
|
84
|
+
return isErrnoException(error) && error.code === "ENOENT";
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
export class UnifiedConfigFileWatcher<
|
|
88
|
+
TType extends string = string,
|
|
89
|
+
TItem = unknown,
|
|
90
|
+
> {
|
|
91
|
+
private readonly definitions: ReadonlyArray<
|
|
92
|
+
UnifiedConfigDefinition<TType, TItem>
|
|
93
|
+
>;
|
|
94
|
+
private readonly debounceMs: number;
|
|
95
|
+
private readonly emitParseErrors: boolean;
|
|
96
|
+
private readonly listeners = new Set<
|
|
97
|
+
(event: UnifiedConfigWatcherEvent<TType, TItem>) => void
|
|
98
|
+
>();
|
|
99
|
+
private readonly recordsByType = new Map<
|
|
100
|
+
TType,
|
|
101
|
+
Map<string, InternalRecord<TType, TItem>>
|
|
102
|
+
>();
|
|
103
|
+
private readonly watchersByDirectory = new Map<string, FSWatcher>();
|
|
104
|
+
private readonly baseTypesByDirectory = new Map<string, Set<TType>>();
|
|
105
|
+
private watchedTypesByDirectory = new Map<string, Set<TType>>();
|
|
106
|
+
private readonly discoveredDirectoriesByType = new Map<TType, Set<string>>();
|
|
107
|
+
private readonly definitionsByType = new Map<
|
|
108
|
+
TType,
|
|
109
|
+
UnifiedConfigDefinition<TType, TItem>
|
|
110
|
+
>();
|
|
111
|
+
private readonly pendingTypes = new Set<TType>();
|
|
112
|
+
private flushTimer: NodeJS.Timeout | undefined;
|
|
113
|
+
private refreshQueue: Promise<void> = Promise.resolve();
|
|
114
|
+
private started = false;
|
|
115
|
+
|
|
116
|
+
constructor(
|
|
117
|
+
definitions: ReadonlyArray<UnifiedConfigDefinition<TType, TItem>>,
|
|
118
|
+
options?: UnifiedConfigWatcherOptions,
|
|
119
|
+
) {
|
|
120
|
+
if (definitions.length === 0) {
|
|
121
|
+
throw new Error(
|
|
122
|
+
"UnifiedConfigFileWatcher requires at least one definition.",
|
|
123
|
+
);
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
this.definitions = definitions;
|
|
127
|
+
this.debounceMs = options?.debounceMs ?? 75;
|
|
128
|
+
this.emitParseErrors = options?.emitParseErrors ?? false;
|
|
129
|
+
|
|
130
|
+
for (const definition of definitions) {
|
|
131
|
+
if (this.definitionsByType.has(definition.type)) {
|
|
132
|
+
throw new Error(
|
|
133
|
+
`Duplicate unified config definition type '${definition.type}'.`,
|
|
134
|
+
);
|
|
135
|
+
}
|
|
136
|
+
this.definitionsByType.set(definition.type, definition);
|
|
137
|
+
this.recordsByType.set(definition.type, new Map());
|
|
138
|
+
this.discoveredDirectoriesByType.set(definition.type, new Set());
|
|
139
|
+
for (const directoryPath of definition.directories) {
|
|
140
|
+
const existing = this.baseTypesByDirectory.get(directoryPath);
|
|
141
|
+
if (existing) {
|
|
142
|
+
existing.add(definition.type);
|
|
143
|
+
} else {
|
|
144
|
+
this.baseTypesByDirectory.set(
|
|
145
|
+
directoryPath,
|
|
146
|
+
new Set([definition.type]),
|
|
147
|
+
);
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
subscribe(
|
|
154
|
+
listener: (event: UnifiedConfigWatcherEvent<TType, TItem>) => void,
|
|
155
|
+
): () => void {
|
|
156
|
+
this.listeners.add(listener);
|
|
157
|
+
return () => {
|
|
158
|
+
this.listeners.delete(listener);
|
|
159
|
+
};
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
async start(): Promise<void> {
|
|
163
|
+
if (this.started) {
|
|
164
|
+
return;
|
|
165
|
+
}
|
|
166
|
+
this.started = true;
|
|
167
|
+
await this.refreshAll();
|
|
168
|
+
this.startDirectoryWatchers();
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
stop(): void {
|
|
172
|
+
this.started = false;
|
|
173
|
+
if (this.flushTimer) {
|
|
174
|
+
clearTimeout(this.flushTimer);
|
|
175
|
+
this.flushTimer = undefined;
|
|
176
|
+
}
|
|
177
|
+
this.pendingTypes.clear();
|
|
178
|
+
for (const watcher of this.watchersByDirectory.values()) {
|
|
179
|
+
watcher.close();
|
|
180
|
+
}
|
|
181
|
+
this.watchersByDirectory.clear();
|
|
182
|
+
this.watchedTypesByDirectory = new Map();
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
async refreshAll(): Promise<void> {
|
|
186
|
+
await this.enqueueRefresh(async () => {
|
|
187
|
+
for (const definition of this.definitions) {
|
|
188
|
+
await this.refreshTypeInternal(definition);
|
|
189
|
+
}
|
|
190
|
+
});
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
async refreshType(type: TType): Promise<void> {
|
|
194
|
+
const definition = this.definitionsByType.get(type);
|
|
195
|
+
if (!definition) {
|
|
196
|
+
throw new Error(`Unknown unified config type '${type}'.`);
|
|
197
|
+
}
|
|
198
|
+
await this.enqueueRefresh(async () => {
|
|
199
|
+
await this.refreshTypeInternal(definition);
|
|
200
|
+
});
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
getSnapshot(type: TType): Map<string, UnifiedConfigRecord<TType, TItem>> {
|
|
204
|
+
const records = this.recordsByType.get(type);
|
|
205
|
+
return new Map(
|
|
206
|
+
[...(records?.entries() ?? [])].map(([id, record]) => [
|
|
207
|
+
id,
|
|
208
|
+
{ ...record },
|
|
209
|
+
]),
|
|
210
|
+
);
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
getAllSnapshots(): Map<
|
|
214
|
+
TType,
|
|
215
|
+
Map<string, UnifiedConfigRecord<TType, TItem>>
|
|
216
|
+
> {
|
|
217
|
+
const snapshot = new Map<
|
|
218
|
+
TType,
|
|
219
|
+
Map<string, UnifiedConfigRecord<TType, TItem>>
|
|
220
|
+
>();
|
|
221
|
+
for (const [type, records] of this.recordsByType.entries()) {
|
|
222
|
+
snapshot.set(
|
|
223
|
+
type,
|
|
224
|
+
new Map(
|
|
225
|
+
[...records.entries()].map(([id, record]) => [id, { ...record }]),
|
|
226
|
+
),
|
|
227
|
+
);
|
|
228
|
+
}
|
|
229
|
+
return snapshot;
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
private emit(event: UnifiedConfigWatcherEvent<TType, TItem>): void {
|
|
233
|
+
for (const listener of this.listeners) {
|
|
234
|
+
listener(event);
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
private enqueueRefresh(action: () => Promise<void>): Promise<void> {
|
|
239
|
+
this.refreshQueue = this.refreshQueue.then(action, action);
|
|
240
|
+
return this.refreshQueue;
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
private startDirectoryWatchers(): void {
|
|
244
|
+
this.syncDirectoryWatchers();
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
private syncDirectoryWatchers(): void {
|
|
248
|
+
const desiredTypesByDirectory = this.buildDesiredTypesByDirectory();
|
|
249
|
+
|
|
250
|
+
for (const [directoryPath, watcher] of this.watchersByDirectory.entries()) {
|
|
251
|
+
if (desiredTypesByDirectory.has(directoryPath)) {
|
|
252
|
+
continue;
|
|
253
|
+
}
|
|
254
|
+
watcher.close();
|
|
255
|
+
this.watchersByDirectory.delete(directoryPath);
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
this.watchedTypesByDirectory = desiredTypesByDirectory;
|
|
259
|
+
|
|
260
|
+
for (const directoryPath of desiredTypesByDirectory.keys()) {
|
|
261
|
+
if (this.watchersByDirectory.has(directoryPath)) {
|
|
262
|
+
continue;
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
try {
|
|
266
|
+
const watcher = watch(directoryPath, () => {
|
|
267
|
+
const types = this.watchedTypesByDirectory.get(directoryPath);
|
|
268
|
+
if (!types) {
|
|
269
|
+
return;
|
|
270
|
+
}
|
|
271
|
+
for (const type of types) {
|
|
272
|
+
this.pendingTypes.add(type);
|
|
273
|
+
}
|
|
274
|
+
this.scheduleFlush();
|
|
275
|
+
});
|
|
276
|
+
this.watchersByDirectory.set(directoryPath, watcher);
|
|
277
|
+
watcher.on("error", (error) => {
|
|
278
|
+
const types = this.watchedTypesByDirectory.get(directoryPath);
|
|
279
|
+
if (!types) {
|
|
280
|
+
return;
|
|
281
|
+
}
|
|
282
|
+
for (const type of types) {
|
|
283
|
+
this.emit({
|
|
284
|
+
kind: "error",
|
|
285
|
+
type,
|
|
286
|
+
error,
|
|
287
|
+
filePath: directoryPath,
|
|
288
|
+
});
|
|
289
|
+
}
|
|
290
|
+
});
|
|
291
|
+
} catch (error) {
|
|
292
|
+
if (!isMissingDirectoryError(error)) {
|
|
293
|
+
const types = desiredTypesByDirectory.get(directoryPath);
|
|
294
|
+
if (!types) {
|
|
295
|
+
continue;
|
|
296
|
+
}
|
|
297
|
+
for (const type of types) {
|
|
298
|
+
this.emit({
|
|
299
|
+
kind: "error",
|
|
300
|
+
type,
|
|
301
|
+
error,
|
|
302
|
+
filePath: directoryPath,
|
|
303
|
+
});
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
private scheduleFlush(): void {
|
|
311
|
+
if (this.flushTimer) {
|
|
312
|
+
clearTimeout(this.flushTimer);
|
|
313
|
+
}
|
|
314
|
+
this.flushTimer = setTimeout(() => {
|
|
315
|
+
this.flushTimer = undefined;
|
|
316
|
+
const types = [...this.pendingTypes];
|
|
317
|
+
this.pendingTypes.clear();
|
|
318
|
+
void this.enqueueRefresh(async () => {
|
|
319
|
+
for (const type of types) {
|
|
320
|
+
const definition = this.definitionsByType.get(type);
|
|
321
|
+
if (!definition) {
|
|
322
|
+
continue;
|
|
323
|
+
}
|
|
324
|
+
await this.refreshTypeInternal(definition);
|
|
325
|
+
}
|
|
326
|
+
});
|
|
327
|
+
}, this.debounceMs);
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
private async refreshTypeInternal(
|
|
331
|
+
definition: UnifiedConfigDefinition<TType, TItem>,
|
|
332
|
+
): Promise<void> {
|
|
333
|
+
const { records: nextRecords, discoveredDirectories } =
|
|
334
|
+
await this.loadDefinition(definition);
|
|
335
|
+
const previousRecords =
|
|
336
|
+
this.recordsByType.get(definition.type) ??
|
|
337
|
+
new Map<string, InternalRecord<TType, TItem>>();
|
|
338
|
+
|
|
339
|
+
for (const [id, previousRecord] of previousRecords.entries()) {
|
|
340
|
+
if (nextRecords.has(id)) {
|
|
341
|
+
continue;
|
|
342
|
+
}
|
|
343
|
+
this.emit({
|
|
344
|
+
kind: "remove",
|
|
345
|
+
type: definition.type,
|
|
346
|
+
id,
|
|
347
|
+
filePath: previousRecord.filePath,
|
|
348
|
+
});
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
for (const [id, nextRecord] of nextRecords.entries()) {
|
|
352
|
+
const previousRecord = previousRecords.get(id);
|
|
353
|
+
if (
|
|
354
|
+
previousRecord &&
|
|
355
|
+
previousRecord.filePath === nextRecord.filePath &&
|
|
356
|
+
previousRecord.fingerprint === nextRecord.fingerprint
|
|
357
|
+
) {
|
|
358
|
+
continue;
|
|
359
|
+
}
|
|
360
|
+
this.emit({
|
|
361
|
+
kind: "upsert",
|
|
362
|
+
record: {
|
|
363
|
+
type: nextRecord.type,
|
|
364
|
+
id,
|
|
365
|
+
item: nextRecord.item,
|
|
366
|
+
filePath: nextRecord.filePath,
|
|
367
|
+
},
|
|
368
|
+
});
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
this.recordsByType.set(definition.type, nextRecords);
|
|
372
|
+
this.discoveredDirectoriesByType.set(
|
|
373
|
+
definition.type,
|
|
374
|
+
discoveredDirectories,
|
|
375
|
+
);
|
|
376
|
+
if (this.started) {
|
|
377
|
+
this.syncDirectoryWatchers();
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
private async loadDefinition(
|
|
382
|
+
definition: UnifiedConfigDefinition<TType, TItem>,
|
|
383
|
+
): Promise<{
|
|
384
|
+
records: Map<string, InternalRecord<TType, TItem>>;
|
|
385
|
+
discoveredDirectories: Set<string>;
|
|
386
|
+
}> {
|
|
387
|
+
const records = new Map<string, InternalRecord<TType, TItem>>();
|
|
388
|
+
const discoveredDirectories = new Set<string>();
|
|
389
|
+
|
|
390
|
+
for (const directoryPath of definition.directories) {
|
|
391
|
+
discoveredDirectories.add(directoryPath);
|
|
392
|
+
const fileCandidates = definition.discoverFiles
|
|
393
|
+
? await definition.discoverFiles(directoryPath)
|
|
394
|
+
: await this.readDirectoryFileCandidates(directoryPath);
|
|
395
|
+
|
|
396
|
+
for (const candidate of fileCandidates) {
|
|
397
|
+
const fileName = candidate.fileName;
|
|
398
|
+
const filePath = candidate.filePath;
|
|
399
|
+
discoveredDirectories.add(candidate.directoryPath);
|
|
400
|
+
if (
|
|
401
|
+
definition.includeFile &&
|
|
402
|
+
!definition.includeFile(fileName, filePath)
|
|
403
|
+
) {
|
|
404
|
+
continue;
|
|
405
|
+
}
|
|
406
|
+
try {
|
|
407
|
+
const content = await readFile(filePath, "utf8");
|
|
408
|
+
const context: UnifiedConfigFileContext<TType> = {
|
|
409
|
+
type: definition.type,
|
|
410
|
+
directoryPath: candidate.directoryPath,
|
|
411
|
+
fileName,
|
|
412
|
+
filePath,
|
|
413
|
+
content,
|
|
414
|
+
};
|
|
415
|
+
const parsed = definition.parseFile(context);
|
|
416
|
+
const id = definition.resolveId(parsed, context).trim();
|
|
417
|
+
if (!id) {
|
|
418
|
+
continue;
|
|
419
|
+
}
|
|
420
|
+
records.set(id, {
|
|
421
|
+
type: definition.type,
|
|
422
|
+
id,
|
|
423
|
+
item: parsed,
|
|
424
|
+
filePath,
|
|
425
|
+
fingerprint: toFingerprint(content),
|
|
426
|
+
});
|
|
427
|
+
} catch (error) {
|
|
428
|
+
if (this.emitParseErrors) {
|
|
429
|
+
this.emit({
|
|
430
|
+
kind: "error",
|
|
431
|
+
type: definition.type,
|
|
432
|
+
error,
|
|
433
|
+
filePath,
|
|
434
|
+
});
|
|
435
|
+
}
|
|
436
|
+
}
|
|
437
|
+
}
|
|
438
|
+
}
|
|
439
|
+
return { records, discoveredDirectories };
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
private buildDesiredTypesByDirectory(): Map<string, Set<TType>> {
|
|
443
|
+
const desired = new Map<string, Set<TType>>();
|
|
444
|
+
for (const [directoryPath, types] of this.baseTypesByDirectory.entries()) {
|
|
445
|
+
desired.set(directoryPath, new Set(types));
|
|
446
|
+
}
|
|
447
|
+
for (const [
|
|
448
|
+
type,
|
|
449
|
+
directories,
|
|
450
|
+
] of this.discoveredDirectoriesByType.entries()) {
|
|
451
|
+
for (const directoryPath of directories) {
|
|
452
|
+
const existing = desired.get(directoryPath);
|
|
453
|
+
if (existing) {
|
|
454
|
+
existing.add(type);
|
|
455
|
+
} else {
|
|
456
|
+
desired.set(directoryPath, new Set([type]));
|
|
457
|
+
}
|
|
458
|
+
}
|
|
459
|
+
}
|
|
460
|
+
return desired;
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
private async readDirectoryFileCandidates(
|
|
464
|
+
directoryPath: string,
|
|
465
|
+
): Promise<UnifiedConfigFileCandidate[]> {
|
|
466
|
+
try {
|
|
467
|
+
const entries = await readdir(directoryPath, { withFileTypes: true });
|
|
468
|
+
return entries
|
|
469
|
+
.filter((entry) => entry.isFile())
|
|
470
|
+
.map((entry) => ({
|
|
471
|
+
directoryPath,
|
|
472
|
+
fileName: entry.name,
|
|
473
|
+
filePath: join(directoryPath, entry.name),
|
|
474
|
+
}))
|
|
475
|
+
.sort((a, b) => a.fileName.localeCompare(b.fileName));
|
|
476
|
+
} catch (error) {
|
|
477
|
+
if (isMissingDirectoryError(error)) {
|
|
478
|
+
return [];
|
|
479
|
+
}
|
|
480
|
+
throw error;
|
|
481
|
+
}
|
|
482
|
+
}
|
|
483
|
+
}
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
import { mkdir, mkdtemp, rm, writeFile } from "node:fs/promises";
|
|
2
|
+
import { tmpdir } from "node:os";
|
|
3
|
+
import { join } from "node:path";
|
|
4
|
+
import { afterEach, describe, expect, it } from "vitest";
|
|
5
|
+
import {
|
|
6
|
+
createUserInstructionConfigWatcher,
|
|
7
|
+
parseRuleConfigFromMarkdown,
|
|
8
|
+
parseSkillConfigFromMarkdown,
|
|
9
|
+
parseWorkflowConfigFromMarkdown,
|
|
10
|
+
resolveRulesConfigSearchPaths,
|
|
11
|
+
resolveSkillsConfigSearchPaths,
|
|
12
|
+
resolveWorkflowsConfigSearchPaths,
|
|
13
|
+
type UserInstructionConfigWatcherEvent,
|
|
14
|
+
} from "./user-instruction-config-loader";
|
|
15
|
+
|
|
16
|
+
const WAIT_TIMEOUT_MS = 4_000;
|
|
17
|
+
const WAIT_INTERVAL_MS = 25;
|
|
18
|
+
|
|
19
|
+
async function waitForEvent(
|
|
20
|
+
events: Array<UserInstructionConfigWatcherEvent>,
|
|
21
|
+
predicate: (event: UserInstructionConfigWatcherEvent) => boolean,
|
|
22
|
+
timeoutMs = WAIT_TIMEOUT_MS,
|
|
23
|
+
): Promise<UserInstructionConfigWatcherEvent> {
|
|
24
|
+
const startedAt = Date.now();
|
|
25
|
+
while (Date.now() - startedAt < timeoutMs) {
|
|
26
|
+
const match = events.find(predicate);
|
|
27
|
+
if (match) {
|
|
28
|
+
return match;
|
|
29
|
+
}
|
|
30
|
+
await new Promise((resolve) => setTimeout(resolve, WAIT_INTERVAL_MS));
|
|
31
|
+
}
|
|
32
|
+
throw new Error("Timed out waiting for watcher event.");
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
describe("user instruction config loader", () => {
|
|
36
|
+
const tempRoots: string[] = [];
|
|
37
|
+
|
|
38
|
+
afterEach(async () => {
|
|
39
|
+
await Promise.all(
|
|
40
|
+
tempRoots.map((dir) => rm(dir, { recursive: true, force: true })),
|
|
41
|
+
);
|
|
42
|
+
tempRoots.length = 0;
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
it("resolves legacy-compatible default search paths", () => {
|
|
46
|
+
const workspacePath = "/repo/demo";
|
|
47
|
+
expect(resolveSkillsConfigSearchPaths(workspacePath)).toEqual(
|
|
48
|
+
expect.arrayContaining([
|
|
49
|
+
"/repo/demo/.clinerules/skills",
|
|
50
|
+
"/repo/demo/.cline/skills",
|
|
51
|
+
"/repo/demo/.claude/skills",
|
|
52
|
+
"/repo/demo/.agents/skills",
|
|
53
|
+
]),
|
|
54
|
+
);
|
|
55
|
+
expect(resolveRulesConfigSearchPaths(workspacePath)).toEqual(
|
|
56
|
+
expect.arrayContaining(["/repo/demo/.clinerules"]),
|
|
57
|
+
);
|
|
58
|
+
expect(resolveWorkflowsConfigSearchPaths(workspacePath)).toEqual(
|
|
59
|
+
expect.arrayContaining(["/repo/demo/.clinerules/workflows"]),
|
|
60
|
+
);
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
it("parses markdown frontmatter for skill, rule, and workflow configs", () => {
|
|
64
|
+
const skill = parseSkillConfigFromMarkdown(
|
|
65
|
+
`---
|
|
66
|
+
name: debugging
|
|
67
|
+
description: Use structured debugging
|
|
68
|
+
disabled: true
|
|
69
|
+
---
|
|
70
|
+
Follow the debugging checklist.`,
|
|
71
|
+
"fallback",
|
|
72
|
+
);
|
|
73
|
+
expect(skill.name).toBe("debugging");
|
|
74
|
+
expect(skill.description).toBe("Use structured debugging");
|
|
75
|
+
expect(skill.disabled).toBe(true);
|
|
76
|
+
expect(skill.instructions).toBe("Follow the debugging checklist.");
|
|
77
|
+
|
|
78
|
+
const rule = parseRuleConfigFromMarkdown(
|
|
79
|
+
`---
|
|
80
|
+
name: rule-a
|
|
81
|
+
disabled: true
|
|
82
|
+
---
|
|
83
|
+
Always run tests before merge.`,
|
|
84
|
+
"rule-a",
|
|
85
|
+
);
|
|
86
|
+
expect(rule.name).toBe("rule-a");
|
|
87
|
+
expect(rule.disabled).toBe(true);
|
|
88
|
+
|
|
89
|
+
const workflow = parseWorkflowConfigFromMarkdown(
|
|
90
|
+
`---
|
|
91
|
+
name: release
|
|
92
|
+
disabled: true
|
|
93
|
+
---
|
|
94
|
+
Document rollout and rollback steps.`,
|
|
95
|
+
"release",
|
|
96
|
+
);
|
|
97
|
+
expect(workflow.name).toBe("release");
|
|
98
|
+
expect(workflow.disabled).toBe(true);
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
it("emits typed events for skills, rules, and workflows in one watcher", async () => {
|
|
102
|
+
const tempRoot = await mkdtemp(
|
|
103
|
+
join(tmpdir(), "core-user-instructions-loader-"),
|
|
104
|
+
);
|
|
105
|
+
tempRoots.push(tempRoot);
|
|
106
|
+
const skillsDir = join(tempRoot, "skills");
|
|
107
|
+
const rulesDir = join(tempRoot, "rules");
|
|
108
|
+
const workflowsDir = join(tempRoot, "workflows");
|
|
109
|
+
await mkdir(join(skillsDir, "incident-response"), { recursive: true });
|
|
110
|
+
await mkdir(rulesDir, { recursive: true });
|
|
111
|
+
await mkdir(workflowsDir, { recursive: true });
|
|
112
|
+
|
|
113
|
+
await writeFile(
|
|
114
|
+
join(skillsDir, "incident-response", "SKILL.md"),
|
|
115
|
+
`---
|
|
116
|
+
name: incident-response
|
|
117
|
+
description: Handle incidents fast
|
|
118
|
+
---
|
|
119
|
+
Escalation runbook`,
|
|
120
|
+
);
|
|
121
|
+
await writeFile(
|
|
122
|
+
join(rulesDir, "default.md"),
|
|
123
|
+
"Keep changes minimal and tested.",
|
|
124
|
+
);
|
|
125
|
+
await writeFile(
|
|
126
|
+
join(workflowsDir, "release.md"),
|
|
127
|
+
"Ship with release checklist.",
|
|
128
|
+
);
|
|
129
|
+
|
|
130
|
+
const watcher = createUserInstructionConfigWatcher({
|
|
131
|
+
skills: { directories: [skillsDir] },
|
|
132
|
+
rules: { directories: [rulesDir] },
|
|
133
|
+
workflows: { directories: [workflowsDir] },
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
const events: Array<UserInstructionConfigWatcherEvent> = [];
|
|
137
|
+
const unsubscribe = watcher.subscribe((event) => events.push(event));
|
|
138
|
+
|
|
139
|
+
try {
|
|
140
|
+
await watcher.start();
|
|
141
|
+
await waitForEvent(
|
|
142
|
+
events,
|
|
143
|
+
(event) => event.kind === "upsert" && event.record.type === "skill",
|
|
144
|
+
);
|
|
145
|
+
await waitForEvent(
|
|
146
|
+
events,
|
|
147
|
+
(event) => event.kind === "upsert" && event.record.type === "rule",
|
|
148
|
+
);
|
|
149
|
+
await waitForEvent(
|
|
150
|
+
events,
|
|
151
|
+
(event) => event.kind === "upsert" && event.record.type === "workflow",
|
|
152
|
+
);
|
|
153
|
+
} finally {
|
|
154
|
+
unsubscribe();
|
|
155
|
+
watcher.stop();
|
|
156
|
+
}
|
|
157
|
+
});
|
|
158
|
+
});
|