@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,191 @@
|
|
|
1
|
+
import type { AgentConfig, Tool } from "@clinebot/agents";
|
|
2
|
+
import YAML from "yaml";
|
|
3
|
+
import { z } from "zod";
|
|
4
|
+
import { ALL_DEFAULT_TOOL_NAMES, type DefaultToolName } from "../default-tools";
|
|
5
|
+
|
|
6
|
+
const AgentConfigFrontmatterSchema = z.object({
|
|
7
|
+
name: z.string().trim().min(1),
|
|
8
|
+
description: z.string().trim().min(1),
|
|
9
|
+
modelId: z.string().trim().min(1).optional(),
|
|
10
|
+
tools: z.union([z.string(), z.array(z.string())]).optional(),
|
|
11
|
+
skills: z.union([z.string(), z.array(z.string())]).optional(),
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
const allowedToolNames = new Set<string>(ALL_DEFAULT_TOOL_NAMES);
|
|
15
|
+
|
|
16
|
+
export interface AgentYamlConfig {
|
|
17
|
+
name: string;
|
|
18
|
+
description: string;
|
|
19
|
+
modelId?: string;
|
|
20
|
+
tools: DefaultToolName[];
|
|
21
|
+
skills?: string[];
|
|
22
|
+
systemPrompt: string;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export interface ParseYamlFrontmatterResult {
|
|
26
|
+
data: Record<string, unknown>;
|
|
27
|
+
body: string;
|
|
28
|
+
hadFrontmatter: boolean;
|
|
29
|
+
parseError?: string;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export interface BuildAgentConfigOverridesOptions {
|
|
33
|
+
availableTools?: ReadonlyArray<Tool>;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export function isAgentConfigYamlFile(fileName: string): boolean {
|
|
37
|
+
return /\.(yaml|yml)$/i.test(fileName);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export function normalizeAgentConfigName(name: string): string {
|
|
41
|
+
return name.trim().toLowerCase();
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function parseYamlFrontmatter(markdown: string): ParseYamlFrontmatterResult {
|
|
45
|
+
const frontmatterRegex = /^---\r?\n([\s\S]*?)\r?\n---\r?\n?([\s\S]*)$/;
|
|
46
|
+
const match = markdown.match(frontmatterRegex);
|
|
47
|
+
if (!match) {
|
|
48
|
+
return { data: {}, body: markdown, hadFrontmatter: false };
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const [, yamlContent, body] = match;
|
|
52
|
+
try {
|
|
53
|
+
const parsed = YAML.parse(yamlContent);
|
|
54
|
+
const data =
|
|
55
|
+
parsed && typeof parsed === "object" && !Array.isArray(parsed)
|
|
56
|
+
? (parsed as Record<string, unknown>)
|
|
57
|
+
: {};
|
|
58
|
+
return { data, body, hadFrontmatter: true };
|
|
59
|
+
} catch (error) {
|
|
60
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
61
|
+
return {
|
|
62
|
+
data: {},
|
|
63
|
+
body: markdown,
|
|
64
|
+
hadFrontmatter: true,
|
|
65
|
+
parseError: message,
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function normalizeToolName(toolName: string): DefaultToolName {
|
|
71
|
+
const trimmed = toolName.trim();
|
|
72
|
+
if (!trimmed) {
|
|
73
|
+
throw new Error("Tool name cannot be empty.");
|
|
74
|
+
}
|
|
75
|
+
if (!allowedToolNames.has(trimmed)) {
|
|
76
|
+
throw new Error(
|
|
77
|
+
`Unknown tool '${trimmed}'. Expected one of: ${ALL_DEFAULT_TOOL_NAMES.join(", ")}.`,
|
|
78
|
+
);
|
|
79
|
+
}
|
|
80
|
+
return trimmed as DefaultToolName;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
function parseToolNames(
|
|
84
|
+
tools: string | string[] | undefined,
|
|
85
|
+
): DefaultToolName[] {
|
|
86
|
+
if (!tools) {
|
|
87
|
+
return [];
|
|
88
|
+
}
|
|
89
|
+
const rawTools = Array.isArray(tools) ? tools : tools.split(",");
|
|
90
|
+
return Array.from(new Set(rawTools.map(normalizeToolName)));
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
function normalizeSkillName(skillName: string): string {
|
|
94
|
+
const trimmed = skillName.trim();
|
|
95
|
+
if (!trimmed) {
|
|
96
|
+
throw new Error("Skill name cannot be empty.");
|
|
97
|
+
}
|
|
98
|
+
return trimmed;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
function parseSkills(
|
|
102
|
+
skills: string | string[] | undefined,
|
|
103
|
+
): string[] | undefined {
|
|
104
|
+
if (skills === undefined) {
|
|
105
|
+
return undefined;
|
|
106
|
+
}
|
|
107
|
+
const rawSkills = Array.isArray(skills) ? skills : skills.split(",");
|
|
108
|
+
return Array.from(new Set(rawSkills.map(normalizeSkillName)));
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
export function parseAgentConfigFromYaml(content: string): AgentYamlConfig {
|
|
112
|
+
const { data, body, hadFrontmatter, parseError } =
|
|
113
|
+
parseYamlFrontmatter(content);
|
|
114
|
+
if (parseError) {
|
|
115
|
+
throw new Error(`Failed to parse YAML frontmatter: ${parseError}`);
|
|
116
|
+
}
|
|
117
|
+
if (!hadFrontmatter) {
|
|
118
|
+
throw new Error("Missing YAML frontmatter block in agent config file.");
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
const parsedFrontmatter = AgentConfigFrontmatterSchema.parse(data);
|
|
122
|
+
const systemPrompt = body.trim();
|
|
123
|
+
if (!systemPrompt) {
|
|
124
|
+
throw new Error("Missing system prompt body in agent config file.");
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
return {
|
|
128
|
+
name: parsedFrontmatter.name,
|
|
129
|
+
description: parsedFrontmatter.description,
|
|
130
|
+
modelId: parsedFrontmatter.modelId,
|
|
131
|
+
tools: parseToolNames(parsedFrontmatter.tools),
|
|
132
|
+
skills: parseSkills(parsedFrontmatter.skills),
|
|
133
|
+
systemPrompt,
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
export function resolveAgentTools(
|
|
138
|
+
toolNames: ReadonlyArray<DefaultToolName>,
|
|
139
|
+
availableTools: ReadonlyArray<Tool>,
|
|
140
|
+
): Tool[] {
|
|
141
|
+
if (toolNames.length === 0) {
|
|
142
|
+
return [];
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
const toolIndex = new Map<string, Tool>(
|
|
146
|
+
availableTools.map((tool) => [tool.name, tool]),
|
|
147
|
+
);
|
|
148
|
+
return toolNames.map((toolName) => {
|
|
149
|
+
const resolved = toolIndex.get(toolName);
|
|
150
|
+
if (!resolved) {
|
|
151
|
+
throw new Error(
|
|
152
|
+
`Configured tool '${toolName}' is unavailable. Available tools: ${availableTools.map((tool) => tool.name).join(", ")}.`,
|
|
153
|
+
);
|
|
154
|
+
}
|
|
155
|
+
return resolved;
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
export function toPartialAgentConfig(
|
|
160
|
+
config: AgentYamlConfig,
|
|
161
|
+
options?: BuildAgentConfigOverridesOptions,
|
|
162
|
+
): Partial<Pick<AgentConfig, "modelId" | "systemPrompt" | "tools">> {
|
|
163
|
+
const partial: Partial<
|
|
164
|
+
Pick<AgentConfig, "modelId" | "systemPrompt" | "tools">
|
|
165
|
+
> = {
|
|
166
|
+
systemPrompt: config.systemPrompt,
|
|
167
|
+
};
|
|
168
|
+
|
|
169
|
+
if (config.modelId) {
|
|
170
|
+
partial.modelId = config.modelId;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
if (config.tools.length > 0) {
|
|
174
|
+
if (!options?.availableTools) {
|
|
175
|
+
throw new Error(
|
|
176
|
+
"Configured tools cannot be converted into AgentConfig.tools without availableTools.",
|
|
177
|
+
);
|
|
178
|
+
}
|
|
179
|
+
partial.tools = resolveAgentTools(config.tools, options.availableTools);
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
return partial;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
export function parsePartialAgentConfigFromYaml(
|
|
186
|
+
content: string,
|
|
187
|
+
options?: BuildAgentConfigOverridesOptions,
|
|
188
|
+
): Partial<Pick<AgentConfig, "modelId" | "systemPrompt" | "tools">> {
|
|
189
|
+
const parsed = parseAgentConfigFromYaml(content);
|
|
190
|
+
return toPartialAgentConfig(parsed, options);
|
|
191
|
+
}
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import { existsSync, readdirSync } from "node:fs";
|
|
2
|
+
import { basename, extname, join } from "node:path";
|
|
3
|
+
import type { HookEventName } from "@clinebot/agents";
|
|
4
|
+
import {
|
|
5
|
+
HOOKS_CONFIG_DIRECTORY_NAME,
|
|
6
|
+
resolveDocumentsHooksDirectoryPath,
|
|
7
|
+
resolveHooksConfigSearchPaths as resolveHooksConfigSearchPathsFromShared,
|
|
8
|
+
} from "@clinebot/shared/storage";
|
|
9
|
+
|
|
10
|
+
export { HOOKS_CONFIG_DIRECTORY_NAME, resolveDocumentsHooksDirectoryPath };
|
|
11
|
+
|
|
12
|
+
export function resolveHooksConfigSearchPaths(
|
|
13
|
+
workspacePath?: string,
|
|
14
|
+
): string[] {
|
|
15
|
+
return resolveHooksConfigSearchPathsFromShared(workspacePath);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export enum HookConfigFileName {
|
|
19
|
+
TaskStart = "TaskStart",
|
|
20
|
+
TaskResume = "TaskResume",
|
|
21
|
+
TaskCancel = "TaskCancel",
|
|
22
|
+
TaskComplete = "TaskComplete",
|
|
23
|
+
PreToolUse = "PreToolUse",
|
|
24
|
+
PostToolUse = "PostToolUse",
|
|
25
|
+
UserPromptSubmit = "UserPromptSubmit",
|
|
26
|
+
PreCompact = "PreCompact",
|
|
27
|
+
SessionShutdown = "SessionShutdown",
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export const HOOK_CONFIG_FILE_EVENT_MAP: Readonly<
|
|
31
|
+
Record<HookConfigFileName, HookEventName | undefined>
|
|
32
|
+
> = {
|
|
33
|
+
[HookConfigFileName.TaskStart]: "agent_start",
|
|
34
|
+
[HookConfigFileName.TaskResume]: "agent_resume",
|
|
35
|
+
[HookConfigFileName.TaskCancel]: "agent_abort",
|
|
36
|
+
[HookConfigFileName.TaskComplete]: "agent_end",
|
|
37
|
+
[HookConfigFileName.PreToolUse]: "tool_call",
|
|
38
|
+
[HookConfigFileName.PostToolUse]: "tool_result",
|
|
39
|
+
[HookConfigFileName.UserPromptSubmit]: "prompt_submit",
|
|
40
|
+
[HookConfigFileName.PreCompact]: undefined,
|
|
41
|
+
[HookConfigFileName.SessionShutdown]: "session_shutdown",
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
const HOOK_CONFIG_FILE_LOOKUP = new Map<string, HookConfigFileName>(
|
|
45
|
+
Object.values(HookConfigFileName).map((name) => [name.toLowerCase(), name]),
|
|
46
|
+
);
|
|
47
|
+
|
|
48
|
+
export function toHookConfigFileName(
|
|
49
|
+
fileName: string,
|
|
50
|
+
): HookConfigFileName | undefined {
|
|
51
|
+
const key = basename(fileName, extname(fileName)).trim().toLowerCase();
|
|
52
|
+
return HOOK_CONFIG_FILE_LOOKUP.get(key);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export interface HookConfigFileEntry {
|
|
56
|
+
fileName: HookConfigFileName;
|
|
57
|
+
hookEventName?: HookEventName;
|
|
58
|
+
path: string;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export function listHookConfigFiles(
|
|
62
|
+
workspacePath?: string,
|
|
63
|
+
): HookConfigFileEntry[] {
|
|
64
|
+
const entries: HookConfigFileEntry[] = [];
|
|
65
|
+
const seen = new Set<string>();
|
|
66
|
+
const directories = resolveHooksConfigSearchPaths(workspacePath).filter(
|
|
67
|
+
(directory) => existsSync(directory),
|
|
68
|
+
);
|
|
69
|
+
|
|
70
|
+
for (const directory of directories) {
|
|
71
|
+
try {
|
|
72
|
+
for (const entry of readdirSync(directory, { withFileTypes: true })) {
|
|
73
|
+
if (!entry.isFile()) {
|
|
74
|
+
continue;
|
|
75
|
+
}
|
|
76
|
+
const fileName = toHookConfigFileName(entry.name);
|
|
77
|
+
if (!fileName) {
|
|
78
|
+
continue;
|
|
79
|
+
}
|
|
80
|
+
const path = join(directory, entry.name);
|
|
81
|
+
if (seen.has(path)) {
|
|
82
|
+
continue;
|
|
83
|
+
}
|
|
84
|
+
seen.add(path);
|
|
85
|
+
entries.push({
|
|
86
|
+
fileName,
|
|
87
|
+
hookEventName: HOOK_CONFIG_FILE_EVENT_MAP[fileName],
|
|
88
|
+
path,
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
} catch {
|
|
92
|
+
// Best-effort listing across config roots.
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
return entries.sort((a, b) => a.path.localeCompare(b.path));
|
|
97
|
+
}
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
export type {
|
|
2
|
+
AgentConfigWatcher,
|
|
3
|
+
AgentConfigWatcherEvent,
|
|
4
|
+
AgentYamlConfig,
|
|
5
|
+
BuildAgentConfigOverridesOptions,
|
|
6
|
+
CreateAgentConfigWatcherOptions,
|
|
7
|
+
ParseYamlFrontmatterResult,
|
|
8
|
+
} from "./agent-config-loader";
|
|
9
|
+
export {
|
|
10
|
+
AGENT_CONFIG_DIRECTORY_NAME,
|
|
11
|
+
createAgentConfigDefinition,
|
|
12
|
+
createAgentConfigWatcher,
|
|
13
|
+
parseAgentConfigFromYaml,
|
|
14
|
+
parsePartialAgentConfigFromYaml,
|
|
15
|
+
readAgentConfigsFromDisk,
|
|
16
|
+
resolveAgentConfigSearchPaths,
|
|
17
|
+
resolveAgentsConfigDirPath,
|
|
18
|
+
resolveAgentTools,
|
|
19
|
+
resolveDocumentsAgentConfigDirectoryPath,
|
|
20
|
+
toPartialAgentConfig,
|
|
21
|
+
} from "./agent-config-loader";
|
|
22
|
+
export {
|
|
23
|
+
HOOK_CONFIG_FILE_EVENT_MAP,
|
|
24
|
+
HOOKS_CONFIG_DIRECTORY_NAME,
|
|
25
|
+
type HookConfigFileEntry,
|
|
26
|
+
HookConfigFileName,
|
|
27
|
+
listHookConfigFiles,
|
|
28
|
+
resolveDocumentsHooksDirectoryPath,
|
|
29
|
+
resolveHooksConfigSearchPaths,
|
|
30
|
+
toHookConfigFileName,
|
|
31
|
+
} from "./hooks-config-loader";
|
|
32
|
+
export type { ResolveAgentPluginPathsOptions } from "./plugin-config-loader";
|
|
33
|
+
export {
|
|
34
|
+
discoverPluginModulePaths,
|
|
35
|
+
resolveAgentPluginPaths,
|
|
36
|
+
resolveAndLoadAgentPlugins,
|
|
37
|
+
resolvePluginConfigSearchPaths,
|
|
38
|
+
} from "./plugin-config-loader";
|
|
39
|
+
export type { LoadAgentPluginFromPathOptions } from "./plugin-loader";
|
|
40
|
+
export {
|
|
41
|
+
loadAgentPluginFromPath,
|
|
42
|
+
loadAgentPluginsFromPaths,
|
|
43
|
+
} from "./plugin-loader";
|
|
44
|
+
export type {
|
|
45
|
+
UnifiedConfigDefinition,
|
|
46
|
+
UnifiedConfigFileCandidate,
|
|
47
|
+
UnifiedConfigFileContext,
|
|
48
|
+
UnifiedConfigRecord,
|
|
49
|
+
UnifiedConfigWatcherEvent,
|
|
50
|
+
UnifiedConfigWatcherOptions,
|
|
51
|
+
} from "./unified-config-file-watcher";
|
|
52
|
+
export { UnifiedConfigFileWatcher } from "./unified-config-file-watcher";
|
|
53
|
+
export type {
|
|
54
|
+
CreateInstructionWatcherOptions,
|
|
55
|
+
CreateRulesConfigDefinitionOptions,
|
|
56
|
+
CreateSkillsConfigDefinitionOptions,
|
|
57
|
+
CreateUserInstructionConfigWatcherOptions,
|
|
58
|
+
CreateWorkflowsConfigDefinitionOptions,
|
|
59
|
+
ParseMarkdownFrontmatterResult,
|
|
60
|
+
RuleConfig,
|
|
61
|
+
SkillConfig,
|
|
62
|
+
UserInstructionConfig,
|
|
63
|
+
UserInstructionConfigType,
|
|
64
|
+
UserInstructionConfigWatcher,
|
|
65
|
+
UserInstructionConfigWatcherEvent,
|
|
66
|
+
WorkflowConfig,
|
|
67
|
+
} from "./user-instruction-config-loader";
|
|
68
|
+
export {
|
|
69
|
+
createRulesConfigDefinition,
|
|
70
|
+
createSkillsConfigDefinition,
|
|
71
|
+
createUserInstructionConfigWatcher,
|
|
72
|
+
createWorkflowsConfigDefinition,
|
|
73
|
+
parseRuleConfigFromMarkdown,
|
|
74
|
+
parseSkillConfigFromMarkdown,
|
|
75
|
+
parseWorkflowConfigFromMarkdown,
|
|
76
|
+
RULES_CONFIG_DIRECTORY_NAME,
|
|
77
|
+
resolveDocumentsRulesDirectoryPath,
|
|
78
|
+
resolveDocumentsWorkflowsDirectoryPath,
|
|
79
|
+
resolveRulesConfigSearchPaths,
|
|
80
|
+
resolveSkillsConfigSearchPaths,
|
|
81
|
+
resolveWorkflowsConfigSearchPaths,
|
|
82
|
+
SKILLS_CONFIG_DIRECTORY_NAME,
|
|
83
|
+
WORKFLOWS_CONFIG_DIRECTORY_NAME,
|
|
84
|
+
} from "./user-instruction-config-loader";
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import { mkdir, mkdtemp, rm, writeFile } from "node:fs/promises";
|
|
2
|
+
import { tmpdir } from "node:os";
|
|
3
|
+
import { join } from "node:path";
|
|
4
|
+
import { setHomeDir } from "@clinebot/shared/storage";
|
|
5
|
+
import { afterEach, describe, expect, it } from "vitest";
|
|
6
|
+
import {
|
|
7
|
+
discoverPluginModulePaths,
|
|
8
|
+
resolveAgentPluginPaths,
|
|
9
|
+
resolvePluginConfigSearchPaths,
|
|
10
|
+
} from "./plugin-config-loader";
|
|
11
|
+
|
|
12
|
+
describe("plugin-config-loader", () => {
|
|
13
|
+
const envSnapshot = {
|
|
14
|
+
HOME: process.env.HOME,
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
afterEach(() => {
|
|
18
|
+
process.env.HOME = envSnapshot.HOME;
|
|
19
|
+
setHomeDir(envSnapshot.HOME ?? "~");
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
it("discovers plugin modules recursively", async () => {
|
|
23
|
+
const root = await mkdtemp(join(tmpdir(), "core-plugin-config-loader-"));
|
|
24
|
+
try {
|
|
25
|
+
const nested = join(root, "nested");
|
|
26
|
+
await mkdir(nested, { recursive: true });
|
|
27
|
+
await writeFile(join(root, "a.mjs"), "export default {}", "utf8");
|
|
28
|
+
await writeFile(join(nested, "b.ts"), "export default {}", "utf8");
|
|
29
|
+
await writeFile(join(root, "ignore.txt"), "noop", "utf8");
|
|
30
|
+
|
|
31
|
+
const discovered = discoverPluginModulePaths(root);
|
|
32
|
+
expect(discovered).toEqual([join(root, "a.mjs"), join(nested, "b.ts")]);
|
|
33
|
+
} finally {
|
|
34
|
+
await rm(root, { recursive: true, force: true });
|
|
35
|
+
}
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
it("resolves plugin paths from explicit files/directories", async () => {
|
|
39
|
+
const root = await mkdtemp(join(tmpdir(), "core-plugin-config-loader-"));
|
|
40
|
+
try {
|
|
41
|
+
const pluginsDir = join(root, "plugins");
|
|
42
|
+
await mkdir(pluginsDir, { recursive: true });
|
|
43
|
+
const filePath = join(root, "direct.mjs");
|
|
44
|
+
const dirPluginPath = join(pluginsDir, "dir-plugin.mjs");
|
|
45
|
+
await writeFile(filePath, "export default {}", "utf8");
|
|
46
|
+
await writeFile(dirPluginPath, "export default {}", "utf8");
|
|
47
|
+
|
|
48
|
+
const resolved = resolveAgentPluginPaths({
|
|
49
|
+
pluginPaths: ["./direct.mjs", "./plugins"],
|
|
50
|
+
workspacePath: join(root, "workspace"),
|
|
51
|
+
cwd: root,
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
expect(resolved).toEqual([filePath, dirPluginPath]);
|
|
55
|
+
} finally {
|
|
56
|
+
await rm(root, { recursive: true, force: true });
|
|
57
|
+
}
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
it("includes shared search-path plugins", async () => {
|
|
61
|
+
const home = await mkdtemp(
|
|
62
|
+
join(tmpdir(), "core-plugin-config-loader-home-"),
|
|
63
|
+
);
|
|
64
|
+
const workspace = await mkdtemp(
|
|
65
|
+
join(tmpdir(), "core-plugin-config-loader-workspace-"),
|
|
66
|
+
);
|
|
67
|
+
try {
|
|
68
|
+
process.env.HOME = home;
|
|
69
|
+
setHomeDir(home);
|
|
70
|
+
const workspacePlugins = join(workspace, ".clinerules", "plugins");
|
|
71
|
+
const userPlugins = join(home, ".cline", "plugins");
|
|
72
|
+
await mkdir(workspacePlugins, { recursive: true });
|
|
73
|
+
await mkdir(userPlugins, { recursive: true });
|
|
74
|
+
const workspacePlugin = join(workspacePlugins, "workspace.mjs");
|
|
75
|
+
const userPlugin = join(userPlugins, "user.mjs");
|
|
76
|
+
await writeFile(workspacePlugin, "export default {}", "utf8");
|
|
77
|
+
await writeFile(userPlugin, "export default {}", "utf8");
|
|
78
|
+
|
|
79
|
+
const searchPaths = resolvePluginConfigSearchPaths(workspace);
|
|
80
|
+
expect(searchPaths).toContain(workspacePlugins);
|
|
81
|
+
expect(searchPaths).toContain(userPlugins);
|
|
82
|
+
|
|
83
|
+
const resolved = resolveAgentPluginPaths({ workspacePath: workspace });
|
|
84
|
+
expect(resolved).toContain(workspacePlugin);
|
|
85
|
+
expect(resolved).toContain(userPlugin);
|
|
86
|
+
} finally {
|
|
87
|
+
await rm(home, { recursive: true, force: true });
|
|
88
|
+
await rm(workspace, { recursive: true, force: true });
|
|
89
|
+
}
|
|
90
|
+
});
|
|
91
|
+
});
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
import { existsSync, readdirSync, statSync } from "node:fs";
|
|
2
|
+
import { join, resolve } from "node:path";
|
|
3
|
+
import type { AgentConfig } from "@clinebot/agents";
|
|
4
|
+
import { resolvePluginConfigSearchPaths as resolvePluginConfigSearchPathsFromShared } from "@clinebot/shared/storage";
|
|
5
|
+
import { loadAgentPluginsFromPaths } from "./plugin-loader";
|
|
6
|
+
import { loadSandboxedPlugins } from "./plugin-sandbox";
|
|
7
|
+
|
|
8
|
+
const PLUGIN_MODULE_EXTENSIONS = new Set([
|
|
9
|
+
".js",
|
|
10
|
+
".mjs",
|
|
11
|
+
".cjs",
|
|
12
|
+
".ts",
|
|
13
|
+
".mts",
|
|
14
|
+
".cts",
|
|
15
|
+
]);
|
|
16
|
+
|
|
17
|
+
type AgentPlugin = NonNullable<AgentConfig["extensions"]>[number];
|
|
18
|
+
|
|
19
|
+
export function resolvePluginConfigSearchPaths(
|
|
20
|
+
workspacePath?: string,
|
|
21
|
+
): string[] {
|
|
22
|
+
return resolvePluginConfigSearchPathsFromShared(workspacePath);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function hasPluginModuleExtension(path: string): boolean {
|
|
26
|
+
const dot = path.lastIndexOf(".");
|
|
27
|
+
if (dot === -1) {
|
|
28
|
+
return false;
|
|
29
|
+
}
|
|
30
|
+
return PLUGIN_MODULE_EXTENSIONS.has(path.slice(dot));
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export function discoverPluginModulePaths(directoryPath: string): string[] {
|
|
34
|
+
const root = resolve(directoryPath);
|
|
35
|
+
if (!existsSync(root)) {
|
|
36
|
+
return [];
|
|
37
|
+
}
|
|
38
|
+
const discovered: string[] = [];
|
|
39
|
+
const stack = [root];
|
|
40
|
+
while (stack.length > 0) {
|
|
41
|
+
const current = stack.pop();
|
|
42
|
+
if (!current) {
|
|
43
|
+
continue;
|
|
44
|
+
}
|
|
45
|
+
for (const entry of readdirSync(current, { withFileTypes: true })) {
|
|
46
|
+
const candidate = join(current, entry.name);
|
|
47
|
+
if (entry.isDirectory()) {
|
|
48
|
+
stack.push(candidate);
|
|
49
|
+
continue;
|
|
50
|
+
}
|
|
51
|
+
if (entry.isFile() && hasPluginModuleExtension(candidate)) {
|
|
52
|
+
discovered.push(candidate);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
return discovered.sort((a, b) => a.localeCompare(b));
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function resolveConfiguredPluginPaths(
|
|
60
|
+
pluginPaths: ReadonlyArray<string>,
|
|
61
|
+
cwd: string,
|
|
62
|
+
): string[] {
|
|
63
|
+
const resolvedPaths: string[] = [];
|
|
64
|
+
for (const pluginPath of pluginPaths) {
|
|
65
|
+
const trimmed = pluginPath.trim();
|
|
66
|
+
if (!trimmed) {
|
|
67
|
+
continue;
|
|
68
|
+
}
|
|
69
|
+
const absolutePath = resolve(cwd, trimmed);
|
|
70
|
+
if (!existsSync(absolutePath)) {
|
|
71
|
+
throw new Error(`Plugin path does not exist: ${absolutePath}`);
|
|
72
|
+
}
|
|
73
|
+
const stats = statSync(absolutePath);
|
|
74
|
+
if (stats.isDirectory()) {
|
|
75
|
+
resolvedPaths.push(...discoverPluginModulePaths(absolutePath));
|
|
76
|
+
continue;
|
|
77
|
+
}
|
|
78
|
+
if (!hasPluginModuleExtension(absolutePath)) {
|
|
79
|
+
throw new Error(
|
|
80
|
+
`Plugin file must use a supported extension (${[...PLUGIN_MODULE_EXTENSIONS].join(", ")}): ${absolutePath}`,
|
|
81
|
+
);
|
|
82
|
+
}
|
|
83
|
+
resolvedPaths.push(absolutePath);
|
|
84
|
+
}
|
|
85
|
+
return resolvedPaths;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
export interface ResolveAgentPluginPathsOptions {
|
|
89
|
+
pluginPaths?: ReadonlyArray<string>;
|
|
90
|
+
workspacePath?: string;
|
|
91
|
+
cwd?: string;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
export function resolveAgentPluginPaths(
|
|
95
|
+
options: ResolveAgentPluginPathsOptions = {},
|
|
96
|
+
): string[] {
|
|
97
|
+
const cwd = options.cwd ?? process.cwd();
|
|
98
|
+
const discoveredFromSearchPaths = resolvePluginConfigSearchPaths(
|
|
99
|
+
options.workspacePath,
|
|
100
|
+
)
|
|
101
|
+
.flatMap((directoryPath) => discoverPluginModulePaths(directoryPath))
|
|
102
|
+
.filter((path) => existsSync(path));
|
|
103
|
+
const configuredPaths = resolveConfiguredPluginPaths(
|
|
104
|
+
options.pluginPaths ?? [],
|
|
105
|
+
cwd,
|
|
106
|
+
);
|
|
107
|
+
|
|
108
|
+
const deduped: string[] = [];
|
|
109
|
+
const seen = new Set<string>();
|
|
110
|
+
for (const path of [...configuredPaths, ...discoveredFromSearchPaths]) {
|
|
111
|
+
if (seen.has(path)) {
|
|
112
|
+
continue;
|
|
113
|
+
}
|
|
114
|
+
seen.add(path);
|
|
115
|
+
deduped.push(path);
|
|
116
|
+
}
|
|
117
|
+
return deduped;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
export interface ResolveAndLoadAgentPluginsOptions
|
|
121
|
+
extends ResolveAgentPluginPathsOptions {
|
|
122
|
+
mode?: "sandbox" | "in_process";
|
|
123
|
+
exportName?: string;
|
|
124
|
+
importTimeoutMs?: number;
|
|
125
|
+
hookTimeoutMs?: number;
|
|
126
|
+
contributionTimeoutMs?: number;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
export async function resolveAndLoadAgentPlugins(
|
|
130
|
+
options: ResolveAndLoadAgentPluginsOptions = {},
|
|
131
|
+
): Promise<{
|
|
132
|
+
extensions: AgentPlugin[];
|
|
133
|
+
shutdown?: () => Promise<void>;
|
|
134
|
+
}> {
|
|
135
|
+
const paths = resolveAgentPluginPaths(options);
|
|
136
|
+
if (paths.length === 0) {
|
|
137
|
+
return { extensions: [] };
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
if (options.mode === "in_process") {
|
|
141
|
+
return {
|
|
142
|
+
extensions: await loadAgentPluginsFromPaths(paths, {
|
|
143
|
+
cwd: options.cwd,
|
|
144
|
+
exportName: options.exportName,
|
|
145
|
+
}),
|
|
146
|
+
};
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
const sandboxed = await loadSandboxedPlugins({
|
|
150
|
+
pluginPaths: paths,
|
|
151
|
+
exportName: options.exportName,
|
|
152
|
+
importTimeoutMs: options.importTimeoutMs,
|
|
153
|
+
hookTimeoutMs: options.hookTimeoutMs,
|
|
154
|
+
contributionTimeoutMs: options.contributionTimeoutMs,
|
|
155
|
+
});
|
|
156
|
+
return {
|
|
157
|
+
extensions: sandboxed.extensions ?? [],
|
|
158
|
+
shutdown: sandboxed.shutdown,
|
|
159
|
+
};
|
|
160
|
+
}
|