@clinebot/core 0.0.22 → 0.0.23
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/dist/ClineCore.d.ts +110 -0
- package/dist/ClineCore.d.ts.map +1 -0
- package/dist/account/cline-account-service.d.ts +2 -1
- package/dist/account/cline-account-service.d.ts.map +1 -1
- package/dist/account/index.d.ts +1 -1
- package/dist/account/index.d.ts.map +1 -1
- package/dist/account/rpc.d.ts +3 -1
- package/dist/account/rpc.d.ts.map +1 -1
- package/dist/account/types.d.ts +3 -0
- package/dist/account/types.d.ts.map +1 -1
- package/dist/agents/plugin-loader.d.ts.map +1 -1
- package/dist/agents/plugin-sandbox-bootstrap.js +17 -17
- package/dist/auth/client.d.ts +1 -1
- package/dist/auth/client.d.ts.map +1 -1
- package/dist/auth/cline.d.ts +1 -1
- package/dist/auth/cline.d.ts.map +1 -1
- package/dist/auth/codex.d.ts +1 -1
- package/dist/auth/codex.d.ts.map +1 -1
- package/dist/auth/oca.d.ts +1 -1
- package/dist/auth/oca.d.ts.map +1 -1
- package/dist/auth/utils.d.ts +2 -2
- package/dist/auth/utils.d.ts.map +1 -1
- package/dist/index.d.ts +50 -5
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +949 -0
- package/dist/providers/local-provider-service.d.ts +4 -4
- package/dist/providers/local-provider-service.d.ts.map +1 -1
- package/dist/runtime/runtime-builder.d.ts +1 -0
- package/dist/runtime/runtime-builder.d.ts.map +1 -1
- package/dist/runtime/session-runtime.d.ts +2 -1
- package/dist/runtime/session-runtime.d.ts.map +1 -1
- package/dist/runtime/team-runtime-registry.d.ts +13 -0
- package/dist/runtime/team-runtime-registry.d.ts.map +1 -0
- package/dist/session/default-session-manager.d.ts +2 -2
- package/dist/session/default-session-manager.d.ts.map +1 -1
- package/dist/session/rpc-runtime-ensure.d.ts +53 -0
- package/dist/session/rpc-runtime-ensure.d.ts.map +1 -0
- package/dist/session/session-config-builder.d.ts +2 -3
- package/dist/session/session-config-builder.d.ts.map +1 -1
- package/dist/session/session-host.d.ts +8 -18
- package/dist/session/session-host.d.ts.map +1 -1
- package/dist/session/session-manager.d.ts +1 -1
- package/dist/session/session-manager.d.ts.map +1 -1
- package/dist/session/session-manifest.d.ts +1 -2
- package/dist/session/session-manifest.d.ts.map +1 -1
- package/dist/session/unified-session-persistence-service.d.ts +2 -2
- package/dist/session/unified-session-persistence-service.d.ts.map +1 -1
- package/dist/session/utils/helpers.d.ts +1 -1
- package/dist/session/utils/helpers.d.ts.map +1 -1
- package/dist/session/utils/types.d.ts +1 -1
- package/dist/session/utils/types.d.ts.map +1 -1
- package/dist/storage/provider-settings-legacy-migration.d.ts.map +1 -1
- package/dist/telemetry/OpenTelemetryProvider.d.ts.map +1 -1
- package/dist/telemetry/distinct-id.d.ts +2 -0
- package/dist/telemetry/distinct-id.d.ts.map +1 -0
- package/dist/telemetry/{opentelemetry.d.ts → index.d.ts} +1 -1
- package/dist/telemetry/index.d.ts.map +1 -0
- package/dist/telemetry/index.js +28 -0
- package/dist/tools/constants.d.ts +1 -1
- package/dist/tools/constants.d.ts.map +1 -1
- package/dist/tools/definitions.d.ts +3 -3
- package/dist/tools/definitions.d.ts.map +1 -1
- package/dist/tools/executors/apply-patch.d.ts +1 -1
- package/dist/tools/executors/apply-patch.d.ts.map +1 -1
- package/dist/tools/executors/bash.d.ts +1 -1
- package/dist/tools/executors/bash.d.ts.map +1 -1
- package/dist/tools/executors/editor.d.ts +1 -1
- package/dist/tools/executors/editor.d.ts.map +1 -1
- package/dist/tools/executors/file-read.d.ts +1 -1
- package/dist/tools/executors/file-read.d.ts.map +1 -1
- package/dist/tools/executors/index.d.ts +14 -14
- package/dist/tools/executors/index.d.ts.map +1 -1
- package/dist/tools/executors/search.d.ts +1 -1
- package/dist/tools/executors/search.d.ts.map +1 -1
- package/dist/tools/executors/web-fetch.d.ts +1 -1
- package/dist/tools/executors/web-fetch.d.ts.map +1 -1
- package/dist/tools/helpers.d.ts +1 -1
- package/dist/tools/helpers.d.ts.map +1 -1
- package/dist/tools/index.d.ts +10 -10
- package/dist/tools/index.d.ts.map +1 -1
- package/dist/tools/model-tool-routing.d.ts +1 -1
- package/dist/tools/model-tool-routing.d.ts.map +1 -1
- package/dist/tools/presets.d.ts +1 -1
- package/dist/tools/presets.d.ts.map +1 -1
- package/dist/types/common.d.ts +17 -8
- package/dist/types/common.d.ts.map +1 -1
- package/dist/types/config.d.ts +4 -3
- package/dist/types/config.d.ts.map +1 -1
- package/dist/types/provider-settings.d.ts +1 -1
- package/dist/types/provider-settings.d.ts.map +1 -1
- package/dist/types.d.ts +5 -2
- package/dist/types.d.ts.map +1 -1
- package/dist/version.d.ts +2 -0
- package/dist/version.d.ts.map +1 -0
- package/package.json +44 -38
- package/src/ClineCore.ts +137 -0
- package/src/account/cline-account-service.test.ts +101 -0
- package/src/account/cline-account-service.ts +300 -0
- package/src/account/featurebase-token.test.ts +175 -0
- package/src/account/index.ts +23 -0
- package/src/account/rpc.test.ts +63 -0
- package/src/account/rpc.ts +185 -0
- package/src/account/types.ts +102 -0
- package/src/agents/agent-config-loader.test.ts +236 -0
- package/src/agents/agent-config-loader.ts +108 -0
- package/src/agents/agent-config-parser.ts +198 -0
- package/src/agents/hooks-config-loader.test.ts +20 -0
- package/src/agents/hooks-config-loader.ts +118 -0
- package/src/agents/index.ts +85 -0
- package/src/agents/plugin-config-loader.test.ts +140 -0
- package/src/agents/plugin-config-loader.ts +97 -0
- package/src/agents/plugin-loader.test.ts +210 -0
- package/src/agents/plugin-loader.ts +175 -0
- package/src/agents/plugin-sandbox-bootstrap.ts +448 -0
- package/src/agents/plugin-sandbox.test.ts +296 -0
- package/src/agents/plugin-sandbox.ts +341 -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 +420 -0
- package/src/auth/codex.test.ts +170 -0
- package/src/auth/codex.ts +491 -0
- package/src/auth/oca.test.ts +215 -0
- package/src/auth/oca.ts +573 -0
- package/src/auth/server.ts +216 -0
- package/src/auth/types.ts +81 -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/index.ts +479 -0
- package/src/input/file-indexer.d.ts +11 -0
- package/src/input/file-indexer.test.ts +127 -0
- package/src/input/file-indexer.ts +327 -0
- package/src/input/index.ts +7 -0
- package/src/input/mention-enricher.test.ts +85 -0
- package/src/input/mention-enricher.ts +122 -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/providers/local-provider-registry.ts +232 -0
- package/src/providers/local-provider-service.test.ts +783 -0
- package/src/providers/local-provider-service.ts +471 -0
- package/src/runtime/commands.test.ts +98 -0
- package/src/runtime/commands.ts +83 -0
- package/src/runtime/hook-file-hooks.test.ts +237 -0
- package/src/runtime/hook-file-hooks.ts +859 -0
- package/src/runtime/index.ts +37 -0
- package/src/runtime/rules.ts +34 -0
- package/src/runtime/runtime-builder.team-persistence.test.ts +245 -0
- package/src/runtime/runtime-builder.test.ts +371 -0
- package/src/runtime/runtime-builder.ts +631 -0
- package/src/runtime/runtime-parity.test.ts +143 -0
- package/src/runtime/sandbox/subprocess-sandbox.ts +231 -0
- package/src/runtime/session-runtime.ts +49 -0
- package/src/runtime/skills.ts +44 -0
- package/src/runtime/team-runtime-registry.ts +46 -0
- package/src/runtime/tool-approval.ts +104 -0
- package/src/runtime/workflows.test.ts +119 -0
- package/src/runtime/workflows.ts +45 -0
- package/src/session/default-session-manager.e2e.test.ts +384 -0
- package/src/session/default-session-manager.test.ts +1931 -0
- package/src/session/default-session-manager.ts +1422 -0
- package/src/session/file-session-service.ts +280 -0
- package/src/session/index.ts +45 -0
- package/src/session/rpc-runtime-ensure.ts +521 -0
- package/src/session/rpc-session-service.ts +107 -0
- package/src/session/rpc-spawn-lease.test.ts +49 -0
- package/src/session/rpc-spawn-lease.ts +122 -0
- package/src/session/runtime-oauth-token-manager.test.ts +137 -0
- package/src/session/runtime-oauth-token-manager.ts +272 -0
- package/src/session/session-agent-events.ts +248 -0
- package/src/session/session-artifacts.ts +106 -0
- package/src/session/session-config-builder.ts +113 -0
- package/src/session/session-graph.ts +92 -0
- package/src/session/session-host.test.ts +89 -0
- package/src/session/session-host.ts +205 -0
- package/src/session/session-manager.ts +69 -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 +673 -0
- package/src/session/session-team-coordination.ts +229 -0
- package/src/session/session-telemetry.ts +100 -0
- package/src/session/sqlite-rpc-session-backend.ts +303 -0
- package/src/session/unified-session-persistence-service.test.ts +85 -0
- package/src/session/unified-session-persistence-service.ts +994 -0
- package/src/session/utils/helpers.ts +139 -0
- package/src/session/utils/types.ts +57 -0
- package/src/session/utils/usage.ts +32 -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/file-team-store.ts +257 -0
- package/src/storage/index.ts +11 -0
- package/src/storage/provider-settings-legacy-migration.test.ts +424 -0
- package/src/storage/provider-settings-legacy-migration.ts +826 -0
- package/src/storage/provider-settings-manager.test.ts +191 -0
- package/src/storage/provider-settings-manager.ts +152 -0
- package/src/storage/session-store.ts +1 -0
- package/src/storage/sqlite-session-store.ts +275 -0
- package/src/storage/sqlite-team-store.ts +454 -0
- package/src/storage/team-store.ts +40 -0
- package/src/team/index.ts +4 -0
- package/src/team/projections.ts +285 -0
- package/src/telemetry/ITelemetryAdapter.ts +94 -0
- package/src/telemetry/LoggerTelemetryAdapter.test.ts +42 -0
- package/src/telemetry/LoggerTelemetryAdapter.ts +114 -0
- package/src/telemetry/OpenTelemetryAdapter.test.ts +157 -0
- package/src/telemetry/OpenTelemetryAdapter.ts +348 -0
- package/src/telemetry/OpenTelemetryProvider.test.ts +113 -0
- package/src/telemetry/OpenTelemetryProvider.ts +325 -0
- package/src/telemetry/TelemetryService.test.ts +134 -0
- package/src/telemetry/TelemetryService.ts +141 -0
- package/src/telemetry/core-events.ts +400 -0
- package/src/telemetry/distinct-id.test.ts +57 -0
- package/src/telemetry/distinct-id.ts +58 -0
- package/src/telemetry/index.ts +20 -0
- package/src/tools/constants.ts +35 -0
- package/src/tools/definitions.test.ts +704 -0
- package/src/tools/definitions.ts +709 -0
- package/src/tools/executors/apply-patch-parser.ts +520 -0
- package/src/tools/executors/apply-patch.ts +359 -0
- package/src/tools/executors/bash.test.ts +87 -0
- package/src/tools/executors/bash.ts +207 -0
- package/src/tools/executors/editor.test.ts +35 -0
- package/src/tools/executors/editor.ts +219 -0
- package/src/tools/executors/file-read.test.ts +49 -0
- package/src/tools/executors/file-read.ts +110 -0
- package/src/tools/executors/index.ts +87 -0
- package/src/tools/executors/search.ts +278 -0
- package/src/tools/executors/web-fetch.ts +259 -0
- package/src/tools/helpers.ts +130 -0
- package/src/tools/index.ts +169 -0
- package/src/tools/model-tool-routing.test.ts +86 -0
- package/src/tools/model-tool-routing.ts +132 -0
- package/src/tools/presets.test.ts +62 -0
- package/src/tools/presets.ts +168 -0
- package/src/tools/schemas.ts +327 -0
- package/src/tools/types.ts +329 -0
- package/src/types/common.ts +26 -0
- package/src/types/config.ts +86 -0
- package/src/types/events.ts +74 -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 +132 -0
- package/src/version.ts +3 -0
- package/dist/index.node.d.ts +0 -47
- package/dist/index.node.d.ts.map +0 -1
- package/dist/index.node.js +0 -948
- package/dist/telemetry/opentelemetry.d.ts.map +0 -1
- package/dist/telemetry/opentelemetry.js +0 -27
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
import { mkdir, mkdtemp, rm, unlink, writeFile } from "node:fs/promises";
|
|
2
|
+
import { tmpdir } from "node:os";
|
|
3
|
+
import { basename, join } from "node:path";
|
|
4
|
+
import { afterEach, describe, expect, it } from "vitest";
|
|
5
|
+
import {
|
|
6
|
+
type AgentYamlConfig,
|
|
7
|
+
parseAgentConfigFromYaml,
|
|
8
|
+
} from "./agent-config-loader";
|
|
9
|
+
import {
|
|
10
|
+
UnifiedConfigFileWatcher,
|
|
11
|
+
type UnifiedConfigWatcherEvent,
|
|
12
|
+
} from "./unified-config-file-watcher";
|
|
13
|
+
|
|
14
|
+
const WAIT_TIMEOUT_MS = 8_000;
|
|
15
|
+
const WAIT_INTERVAL_MS = 25;
|
|
16
|
+
|
|
17
|
+
async function waitForEvent<TType extends string, TItem>(
|
|
18
|
+
events: Array<UnifiedConfigWatcherEvent<TType, TItem>>,
|
|
19
|
+
predicate: (event: UnifiedConfigWatcherEvent<TType, TItem>) => boolean,
|
|
20
|
+
timeoutMs = WAIT_TIMEOUT_MS,
|
|
21
|
+
): Promise<UnifiedConfigWatcherEvent<TType, TItem>> {
|
|
22
|
+
const startedAt = Date.now();
|
|
23
|
+
while (Date.now() - startedAt < timeoutMs) {
|
|
24
|
+
const match = events.find(predicate);
|
|
25
|
+
if (match) {
|
|
26
|
+
return match;
|
|
27
|
+
}
|
|
28
|
+
await new Promise((resolve) => setTimeout(resolve, WAIT_INTERVAL_MS));
|
|
29
|
+
}
|
|
30
|
+
throw new Error("Timed out waiting for watcher event.");
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
describe("UnifiedConfigFileWatcher", () => {
|
|
34
|
+
const tempRoots: string[] = [];
|
|
35
|
+
|
|
36
|
+
afterEach(async () => {
|
|
37
|
+
await Promise.all(
|
|
38
|
+
tempRoots.map((dir) => rm(dir, { recursive: true, force: true })),
|
|
39
|
+
);
|
|
40
|
+
tempRoots.length = 0;
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
it("emits upsert and remove events with config type for agent configs", async () => {
|
|
44
|
+
const tempRoot = await mkdtemp(
|
|
45
|
+
join(tmpdir(), "core-unified-config-watcher-"),
|
|
46
|
+
);
|
|
47
|
+
tempRoots.push(tempRoot);
|
|
48
|
+
const agentsDir = join(tempRoot, "agents");
|
|
49
|
+
await mkdir(agentsDir, { recursive: true });
|
|
50
|
+
const agentFilePath = join(agentsDir, "reviewer.yaml");
|
|
51
|
+
await writeFile(
|
|
52
|
+
agentFilePath,
|
|
53
|
+
`---
|
|
54
|
+
name: Reviewer
|
|
55
|
+
description: Reviews patches
|
|
56
|
+
tools: read_files
|
|
57
|
+
---
|
|
58
|
+
Review code carefully.`,
|
|
59
|
+
);
|
|
60
|
+
|
|
61
|
+
const watcher = new UnifiedConfigFileWatcher([
|
|
62
|
+
{
|
|
63
|
+
type: "agent" as const,
|
|
64
|
+
directories: [agentsDir],
|
|
65
|
+
includeFile: (fileName) => /\.(yaml|yml)$/i.test(fileName),
|
|
66
|
+
parseFile: (context) => parseAgentConfigFromYaml(context.content),
|
|
67
|
+
resolveId: (config) => config.name.toLowerCase(),
|
|
68
|
+
},
|
|
69
|
+
]);
|
|
70
|
+
|
|
71
|
+
const events: Array<
|
|
72
|
+
UnifiedConfigWatcherEvent<
|
|
73
|
+
"agent",
|
|
74
|
+
ReturnType<typeof parseAgentConfigFromYaml>
|
|
75
|
+
>
|
|
76
|
+
> = [];
|
|
77
|
+
const unsubscribe = watcher.subscribe((event) => events.push(event));
|
|
78
|
+
|
|
79
|
+
try {
|
|
80
|
+
await watcher.start();
|
|
81
|
+
await waitForEvent(
|
|
82
|
+
events,
|
|
83
|
+
(event) => event.kind === "upsert" && event.record.id === "reviewer",
|
|
84
|
+
);
|
|
85
|
+
|
|
86
|
+
events.length = 0;
|
|
87
|
+
await writeFile(
|
|
88
|
+
agentFilePath,
|
|
89
|
+
`---
|
|
90
|
+
name: Reviewer
|
|
91
|
+
description: Reviews patches
|
|
92
|
+
---
|
|
93
|
+
Review code with strictness.`,
|
|
94
|
+
);
|
|
95
|
+
await watcher.refreshType("agent");
|
|
96
|
+
await waitForEvent(
|
|
97
|
+
events,
|
|
98
|
+
(event) => event.kind === "upsert" && event.record.type === "agent",
|
|
99
|
+
);
|
|
100
|
+
|
|
101
|
+
events.length = 0;
|
|
102
|
+
await unlink(agentFilePath);
|
|
103
|
+
await watcher.refreshType("agent");
|
|
104
|
+
await waitForEvent(
|
|
105
|
+
events,
|
|
106
|
+
(event) => event.kind === "remove" && event.type === "agent",
|
|
107
|
+
);
|
|
108
|
+
} finally {
|
|
109
|
+
unsubscribe();
|
|
110
|
+
watcher.stop();
|
|
111
|
+
}
|
|
112
|
+
}, 15_000);
|
|
113
|
+
|
|
114
|
+
it("supports one watcher instance for multiple config types", async () => {
|
|
115
|
+
const tempRoot = await mkdtemp(
|
|
116
|
+
join(tmpdir(), "core-unified-config-watcher-"),
|
|
117
|
+
);
|
|
118
|
+
tempRoots.push(tempRoot);
|
|
119
|
+
const agentsDir = join(tempRoot, "agents");
|
|
120
|
+
const skillsDir = join(tempRoot, "skills");
|
|
121
|
+
await mkdir(agentsDir, { recursive: true });
|
|
122
|
+
await mkdir(skillsDir, { recursive: true });
|
|
123
|
+
|
|
124
|
+
await writeFile(
|
|
125
|
+
join(agentsDir, "researcher.yaml"),
|
|
126
|
+
`---
|
|
127
|
+
name: Researcher
|
|
128
|
+
description: Finds context
|
|
129
|
+
---
|
|
130
|
+
Investigate related code paths.`,
|
|
131
|
+
);
|
|
132
|
+
await writeFile(
|
|
133
|
+
join(skillsDir, "SKILL.md"),
|
|
134
|
+
`---
|
|
135
|
+
name: incident-response
|
|
136
|
+
description: Handle incidents
|
|
137
|
+
---
|
|
138
|
+
Escalation playbook`,
|
|
139
|
+
);
|
|
140
|
+
|
|
141
|
+
const watcher = new UnifiedConfigFileWatcher<
|
|
142
|
+
"agent" | "skill",
|
|
143
|
+
AgentYamlConfig | { path: string }
|
|
144
|
+
>([
|
|
145
|
+
{
|
|
146
|
+
type: "agent" as const,
|
|
147
|
+
directories: [agentsDir],
|
|
148
|
+
includeFile: (fileName) => /\.(yaml|yml)$/i.test(fileName),
|
|
149
|
+
parseFile: (context) =>
|
|
150
|
+
parseAgentConfigFromYaml(context.content) as
|
|
151
|
+
| AgentYamlConfig
|
|
152
|
+
| { path: string },
|
|
153
|
+
resolveId: (config) =>
|
|
154
|
+
"name" in config ? config.name.toLowerCase() : "invalid-agent-config",
|
|
155
|
+
},
|
|
156
|
+
{
|
|
157
|
+
type: "skill" as const,
|
|
158
|
+
directories: [skillsDir],
|
|
159
|
+
includeFile: (fileName) => fileName === "SKILL.md",
|
|
160
|
+
parseFile: (context) => ({ path: context.filePath }),
|
|
161
|
+
resolveId: (_parsed, context) => basename(context.directoryPath),
|
|
162
|
+
},
|
|
163
|
+
]);
|
|
164
|
+
|
|
165
|
+
const events: Array<
|
|
166
|
+
UnifiedConfigWatcherEvent<
|
|
167
|
+
"agent" | "skill",
|
|
168
|
+
AgentYamlConfig | { path: string }
|
|
169
|
+
>
|
|
170
|
+
> = [];
|
|
171
|
+
const unsubscribe = watcher.subscribe((event) => events.push(event));
|
|
172
|
+
|
|
173
|
+
try {
|
|
174
|
+
await watcher.start();
|
|
175
|
+
await waitForEvent(
|
|
176
|
+
events,
|
|
177
|
+
(event) =>
|
|
178
|
+
event.kind === "upsert" &&
|
|
179
|
+
(event.record.type === "agent" || event.record.type === "skill"),
|
|
180
|
+
);
|
|
181
|
+
expect(
|
|
182
|
+
events.some(
|
|
183
|
+
(event) => event.kind === "upsert" && event.record.type === "agent",
|
|
184
|
+
),
|
|
185
|
+
).toBe(true);
|
|
186
|
+
expect(
|
|
187
|
+
events.some(
|
|
188
|
+
(event) => event.kind === "upsert" && event.record.type === "skill",
|
|
189
|
+
),
|
|
190
|
+
).toBe(true);
|
|
191
|
+
} finally {
|
|
192
|
+
unsubscribe();
|
|
193
|
+
watcher.stop();
|
|
194
|
+
}
|
|
195
|
+
});
|
|
196
|
+
});
|
|
@@ -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
|
+
}
|