thoth-agents 0.1.6 → 0.1.7

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.
@@ -0,0 +1,1898 @@
1
+ // src/config/loader.ts
2
+ import * as fs from "fs";
3
+ import * as path from "path";
4
+
5
+ // src/cli/config-io.ts
6
+ import {
7
+ copyFileSync,
8
+ existsSync as existsSync2,
9
+ readFileSync,
10
+ renameSync,
11
+ statSync,
12
+ writeFileSync
13
+ } from "fs";
14
+
15
+ // src/cli/paths.ts
16
+ import { existsSync, mkdirSync } from "fs";
17
+ import { homedir } from "os";
18
+ import { dirname, join } from "path";
19
+ function getDefaultOpenCodeConfigDir() {
20
+ const userConfigDir = process.env.XDG_CONFIG_HOME ? process.env.XDG_CONFIG_HOME : join(homedir(), ".config");
21
+ return join(userConfigDir, "opencode");
22
+ }
23
+ function getCustomOpenCodeConfigDir() {
24
+ const configDir = process.env.OPENCODE_CONFIG_DIR?.trim();
25
+ return configDir || void 0;
26
+ }
27
+ function getConfigDir() {
28
+ const customConfigDir = getCustomOpenCodeConfigDir();
29
+ if (customConfigDir) {
30
+ return customConfigDir;
31
+ }
32
+ return getDefaultOpenCodeConfigDir();
33
+ }
34
+ function getOpenCodeConfigPaths() {
35
+ const configDir = getDefaultOpenCodeConfigDir();
36
+ return [join(configDir, "opencode.json"), join(configDir, "opencode.jsonc")];
37
+ }
38
+ function getConfigJson() {
39
+ return getOpenCodeConfigPaths()[0];
40
+ }
41
+ function getConfigJsonc() {
42
+ return getOpenCodeConfigPaths()[1];
43
+ }
44
+ function getLiteConfig() {
45
+ return join(getConfigDir(), "thoth-agents.json");
46
+ }
47
+ function getLiteConfigJsonc() {
48
+ return join(getConfigDir(), "thoth-agents.jsonc");
49
+ }
50
+ function getExistingLiteConfigPath() {
51
+ const jsonPath = getLiteConfig();
52
+ if (existsSync(jsonPath)) return jsonPath;
53
+ const jsoncPath = getLiteConfigJsonc();
54
+ if (existsSync(jsoncPath)) return jsoncPath;
55
+ return jsonPath;
56
+ }
57
+ function getExistingConfigPath() {
58
+ const jsonPath = getConfigJson();
59
+ if (existsSync(jsonPath)) return jsonPath;
60
+ const jsoncPath = getConfigJsonc();
61
+ if (existsSync(jsoncPath)) return jsoncPath;
62
+ return jsonPath;
63
+ }
64
+ function ensureConfigDir() {
65
+ const configDir = getConfigDir();
66
+ if (!existsSync(configDir)) {
67
+ mkdirSync(configDir, { recursive: true });
68
+ }
69
+ }
70
+ function ensureOpenCodeConfigDir() {
71
+ const configDir = dirname(getConfigJson());
72
+ if (!existsSync(configDir)) {
73
+ mkdirSync(configDir, { recursive: true });
74
+ }
75
+ }
76
+
77
+ // src/cli/providers.ts
78
+ var MODEL_MAPPINGS = {
79
+ openai: {
80
+ orchestrator: { model: "openai/gpt-5.4" },
81
+ oracle: { model: "openai/gpt-5.4", variant: "high" },
82
+ librarian: { model: "openai/gpt-5.4-mini", variant: "low" },
83
+ explorer: { model: "openai/gpt-5.4-mini", variant: "low" },
84
+ designer: { model: "openai/gpt-5.4-mini", variant: "medium" },
85
+ quick: { model: "openai/gpt-5.4-mini", variant: "low" },
86
+ deep: { model: "openai/gpt-5.4", variant: "high" }
87
+ },
88
+ kimi: {
89
+ orchestrator: { model: "kimi-for-coding/k2p5" },
90
+ oracle: { model: "kimi-for-coding/k2p5", variant: "high" },
91
+ librarian: { model: "kimi-for-coding/k2p5", variant: "low" },
92
+ explorer: { model: "kimi-for-coding/k2p5", variant: "low" },
93
+ designer: { model: "kimi-for-coding/k2p5", variant: "medium" },
94
+ quick: { model: "kimi-for-coding/k2p5", variant: "low" },
95
+ deep: { model: "kimi-for-coding/k2p5", variant: "high" }
96
+ },
97
+ copilot: {
98
+ orchestrator: { model: "github-copilot/claude-opus-4.6" },
99
+ oracle: { model: "github-copilot/claude-opus-4.6", variant: "high" },
100
+ librarian: { model: "github-copilot/grok-code-fast-1", variant: "low" },
101
+ explorer: { model: "github-copilot/grok-code-fast-1", variant: "low" },
102
+ designer: {
103
+ model: "github-copilot/gemini-3.1-pro-preview",
104
+ variant: "medium"
105
+ },
106
+ quick: { model: "github-copilot/claude-sonnet-4.6", variant: "low" },
107
+ deep: { model: "github-copilot/claude-opus-4.6", variant: "high" }
108
+ },
109
+ "zai-plan": {
110
+ orchestrator: { model: "zai-coding-plan/glm-5" },
111
+ oracle: { model: "zai-coding-plan/glm-5", variant: "high" },
112
+ librarian: { model: "zai-coding-plan/glm-5", variant: "low" },
113
+ explorer: { model: "zai-coding-plan/glm-5", variant: "low" },
114
+ designer: { model: "zai-coding-plan/glm-5", variant: "medium" },
115
+ quick: { model: "zai-coding-plan/glm-5", variant: "low" },
116
+ deep: { model: "zai-coding-plan/glm-5", variant: "high" }
117
+ }
118
+ };
119
+ function generateLiteConfig(installConfig) {
120
+ const config = {
121
+ preset: "openai",
122
+ presets: {}
123
+ };
124
+ const createAgentConfig = (modelInfo) => {
125
+ return {
126
+ model: modelInfo.model,
127
+ variant: modelInfo.variant
128
+ };
129
+ };
130
+ const buildPreset = (mappingName) => {
131
+ const mapping = MODEL_MAPPINGS[mappingName];
132
+ return Object.fromEntries(
133
+ Object.entries(mapping).map(([agentName, modelInfo]) => [
134
+ agentName,
135
+ createAgentConfig(modelInfo)
136
+ ])
137
+ );
138
+ };
139
+ config.presets.openai = buildPreset("openai");
140
+ if (installConfig.hasTmux) {
141
+ config.tmux = {
142
+ enabled: true,
143
+ layout: "main-vertical",
144
+ main_pane_size: 60
145
+ };
146
+ }
147
+ return config;
148
+ }
149
+
150
+ // src/cli/config-io.ts
151
+ var PACKAGE_NAME = "thoth-agents";
152
+ function stripJsonComments(json) {
153
+ const commentPattern = /\\"|"(?:\\"|[^"])*"|(\/\/.*|\/\*[\s\S]*?\*\/)/g;
154
+ const trailingCommaPattern = /\\"|"(?:\\"|[^"])*"|(,)(\s*[}\]])/g;
155
+ return json.replace(
156
+ commentPattern,
157
+ (match, commentGroup) => commentGroup ? "" : match
158
+ ).replace(
159
+ trailingCommaPattern,
160
+ (match, comma, closing) => comma ? closing : match
161
+ );
162
+ }
163
+ function parseConfigFile(path2) {
164
+ try {
165
+ if (!existsSync2(path2)) return { config: null };
166
+ const stat = statSync(path2);
167
+ if (stat.size === 0) return { config: null };
168
+ const content = readFileSync(path2, "utf-8");
169
+ if (content.trim().length === 0) return { config: null };
170
+ return { config: JSON.parse(stripJsonComments(content)) };
171
+ } catch (err) {
172
+ return { config: null, error: String(err) };
173
+ }
174
+ }
175
+ function parseConfig(path2) {
176
+ const result = parseConfigFile(path2);
177
+ if (result.config || result.error) return result;
178
+ if (path2.endsWith(".json")) {
179
+ const jsoncPath = path2.replace(/\.json$/, ".jsonc");
180
+ return parseConfigFile(jsoncPath);
181
+ }
182
+ return { config: null };
183
+ }
184
+ function writeConfig(configPath, config) {
185
+ if (configPath.endsWith(".jsonc")) {
186
+ console.warn(
187
+ "[config-manager] Writing to .jsonc file - comments will not be preserved"
188
+ );
189
+ }
190
+ const tmpPath = `${configPath}.tmp`;
191
+ const bakPath = `${configPath}.bak`;
192
+ const content = `${JSON.stringify(config, null, 2)}
193
+ `;
194
+ if (existsSync2(configPath)) {
195
+ copyFileSync(configPath, bakPath);
196
+ }
197
+ writeFileSync(tmpPath, content);
198
+ renameSync(tmpPath, configPath);
199
+ }
200
+ async function addPluginToOpenCodeConfig() {
201
+ const configPath = getExistingConfigPath();
202
+ try {
203
+ ensureOpenCodeConfigDir();
204
+ } catch (err) {
205
+ return {
206
+ success: false,
207
+ configPath,
208
+ error: `Failed to create config directory: ${err}`
209
+ };
210
+ }
211
+ try {
212
+ const { config: parsedConfig, error } = parseConfig(configPath);
213
+ if (error) {
214
+ return {
215
+ success: false,
216
+ configPath,
217
+ error: `Failed to parse config: ${error}`
218
+ };
219
+ }
220
+ const config = parsedConfig ?? {};
221
+ const plugins = config.plugin ?? [];
222
+ const filteredPlugins = plugins.filter(
223
+ (p) => p !== PACKAGE_NAME && !p.startsWith(`${PACKAGE_NAME}@`)
224
+ );
225
+ filteredPlugins.push(`${PACKAGE_NAME}@latest`);
226
+ config.plugin = filteredPlugins;
227
+ writeConfig(configPath, config);
228
+ return { success: true, configPath };
229
+ } catch (err) {
230
+ return {
231
+ success: false,
232
+ configPath,
233
+ error: `Failed to update opencode config: ${err}`
234
+ };
235
+ }
236
+ }
237
+ function writeLiteConfig(installConfig, targetPath) {
238
+ const configPath = targetPath ?? getLiteConfig();
239
+ try {
240
+ ensureConfigDir();
241
+ const config = generateLiteConfig(installConfig);
242
+ const tmpPath = `${configPath}.tmp`;
243
+ const bakPath = `${configPath}.bak`;
244
+ const content = `${JSON.stringify(config, null, 2)}
245
+ `;
246
+ if (existsSync2(configPath)) {
247
+ copyFileSync(configPath, bakPath);
248
+ }
249
+ writeFileSync(tmpPath, content);
250
+ renameSync(tmpPath, configPath);
251
+ return { success: true, configPath };
252
+ } catch (err) {
253
+ return {
254
+ success: false,
255
+ configPath,
256
+ error: `Failed to write lite config: ${err}`
257
+ };
258
+ }
259
+ }
260
+ function disableDefaultAgents() {
261
+ const configPath = getExistingConfigPath();
262
+ try {
263
+ ensureOpenCodeConfigDir();
264
+ const { config: parsedConfig, error } = parseConfig(configPath);
265
+ if (error) {
266
+ return {
267
+ success: false,
268
+ configPath,
269
+ error: `Failed to parse config: ${error}`
270
+ };
271
+ }
272
+ const config = parsedConfig ?? {};
273
+ const agent = config.agent ?? {};
274
+ agent.explore = { disable: true };
275
+ agent.general = { disable: true };
276
+ config.agent = agent;
277
+ writeConfig(configPath, config);
278
+ return { success: true, configPath };
279
+ } catch (err) {
280
+ return {
281
+ success: false,
282
+ configPath,
283
+ error: `Failed to disable default agents: ${err}`
284
+ };
285
+ }
286
+ }
287
+ function detectCurrentConfig() {
288
+ const result = {
289
+ isInstalled: false,
290
+ hasKimi: false,
291
+ hasOpenAI: false,
292
+ hasAnthropic: false,
293
+ hasCopilot: false,
294
+ hasZaiPlan: false,
295
+ hasAntigravity: false,
296
+ hasChutes: false,
297
+ hasOpencodeZen: false,
298
+ hasTmux: false
299
+ };
300
+ const { config } = parseConfig(getExistingConfigPath());
301
+ if (!config) return result;
302
+ const plugins = config.plugin ?? [];
303
+ result.isInstalled = plugins.some((p) => p.startsWith(PACKAGE_NAME));
304
+ result.hasAntigravity = plugins.some(
305
+ (p) => p.startsWith("opencode-antigravity-auth")
306
+ );
307
+ const providers = config.provider;
308
+ result.hasKimi = !!providers?.kimi;
309
+ result.hasAnthropic = !!providers?.anthropic;
310
+ result.hasCopilot = !!providers?.["github-copilot"];
311
+ result.hasZaiPlan = !!providers?.["zai-coding-plan"];
312
+ result.hasChutes = !!providers?.chutes;
313
+ if (providers?.google) result.hasAntigravity = true;
314
+ const { config: liteConfig } = parseConfig(getLiteConfig());
315
+ if (liteConfig && typeof liteConfig === "object") {
316
+ const configObj = liteConfig;
317
+ const presetName = configObj.preset;
318
+ const presets = configObj.presets;
319
+ const agents = presets?.[presetName];
320
+ if (agents) {
321
+ const models = Object.values(agents).map((a) => a?.model).filter(Boolean);
322
+ result.hasOpenAI = models.some((m) => m?.startsWith("openai/"));
323
+ result.hasAnthropic = models.some((m) => m?.startsWith("anthropic/"));
324
+ result.hasCopilot = models.some((m) => m?.startsWith("github-copilot/"));
325
+ result.hasZaiPlan = models.some((m) => m?.startsWith("zai-coding-plan/"));
326
+ result.hasOpencodeZen = models.some((m) => m?.startsWith("opencode/"));
327
+ if (models.some((m) => m?.startsWith("google/"))) {
328
+ result.hasAntigravity = true;
329
+ }
330
+ if (models.some((m) => m?.startsWith("chutes/"))) {
331
+ result.hasChutes = true;
332
+ }
333
+ }
334
+ if (configObj.tmux && typeof configObj.tmux === "object") {
335
+ const tmuxConfig = configObj.tmux;
336
+ result.hasTmux = tmuxConfig.enabled === true;
337
+ }
338
+ }
339
+ return result;
340
+ }
341
+
342
+ // src/config/schema.ts
343
+ import { z } from "zod";
344
+ var AGENT_NAMES = [
345
+ "orchestrator",
346
+ "oracle",
347
+ "designer",
348
+ "explorer",
349
+ "librarian",
350
+ "quick",
351
+ "deep"
352
+ ];
353
+ var FALLBACK_AGENT_NAMES = [...AGENT_NAMES];
354
+ var MANUAL_AGENT_NAMES = [...AGENT_NAMES];
355
+ var ProviderModelIdSchema = z.string().regex(
356
+ /^[^/\s]+\/[^\s]+$/,
357
+ "Expected provider/model format (provider/.../model)"
358
+ );
359
+ var ManualAgentPlanSchema = z.object({
360
+ primary: ProviderModelIdSchema,
361
+ fallback1: ProviderModelIdSchema,
362
+ fallback2: ProviderModelIdSchema,
363
+ fallback3: ProviderModelIdSchema
364
+ }).superRefine((value, ctx) => {
365
+ const unique = /* @__PURE__ */ new Set([
366
+ value.primary,
367
+ value.fallback1,
368
+ value.fallback2,
369
+ value.fallback3
370
+ ]);
371
+ if (unique.size !== 4) {
372
+ ctx.addIssue({
373
+ code: z.ZodIssueCode.custom,
374
+ message: "primary and fallbacks must be unique per agent"
375
+ });
376
+ }
377
+ });
378
+ var ManualPlanSchema = z.object({
379
+ orchestrator: ManualAgentPlanSchema,
380
+ oracle: ManualAgentPlanSchema,
381
+ designer: ManualAgentPlanSchema,
382
+ explorer: ManualAgentPlanSchema,
383
+ librarian: ManualAgentPlanSchema,
384
+ quick: ManualAgentPlanSchema,
385
+ deep: ManualAgentPlanSchema
386
+ }).strict();
387
+ var AgentModelChainSchema = z.array(z.string()).min(1);
388
+ var FallbackChainsSchema = z.object({
389
+ orchestrator: AgentModelChainSchema.optional(),
390
+ oracle: AgentModelChainSchema.optional(),
391
+ designer: AgentModelChainSchema.optional(),
392
+ explorer: AgentModelChainSchema.optional(),
393
+ librarian: AgentModelChainSchema.optional(),
394
+ quick: AgentModelChainSchema.optional(),
395
+ deep: AgentModelChainSchema.optional()
396
+ }).catchall(AgentModelChainSchema);
397
+ var AgentOverrideConfigSchema = z.object({
398
+ model: z.union([
399
+ z.string(),
400
+ z.array(
401
+ z.union([
402
+ z.string(),
403
+ z.object({
404
+ id: z.string(),
405
+ variant: z.string().optional()
406
+ })
407
+ ])
408
+ )
409
+ ]).optional(),
410
+ temperature: z.number().min(0).max(2).optional(),
411
+ steps: z.number().int().min(1).optional(),
412
+ variant: z.string().optional().catch(void 0)
413
+ });
414
+ var TmuxLayoutSchema = z.enum([
415
+ "main-horizontal",
416
+ // Main pane on top, agents stacked below
417
+ "main-vertical",
418
+ // Main pane on left, agents stacked on right
419
+ "tiled",
420
+ // All panes equal size grid
421
+ "even-horizontal",
422
+ // All panes side by side
423
+ "even-vertical"
424
+ // All panes stacked vertically
425
+ ]);
426
+ var TmuxConfigSchema = z.object({
427
+ enabled: z.boolean().default(false),
428
+ layout: TmuxLayoutSchema.default("main-vertical"),
429
+ main_pane_size: z.number().min(20).max(80).default(60)
430
+ // percentage for main pane
431
+ });
432
+ var PresetSchema = z.record(z.string(), AgentOverrideConfigSchema);
433
+ var AgentNameSchema = z.enum(AGENT_NAMES);
434
+ var McpNameSchema = z.enum([
435
+ "exa",
436
+ "context7",
437
+ "grep_app",
438
+ "thoth_mem"
439
+ ]);
440
+ var ThothConfigSchema = z.object({
441
+ command: z.array(z.string()).optional(),
442
+ data_dir: z.string().optional(),
443
+ environment: z.record(z.string(), z.string()).optional(),
444
+ timeout: z.number().optional(),
445
+ http_port: z.number().optional()
446
+ });
447
+ var ArtifactStoreModeSchema = z.enum([
448
+ "thoth-mem",
449
+ "openspec",
450
+ "hybrid"
451
+ ]);
452
+ var ArtifactStoreConfigSchema = z.object({
453
+ mode: ArtifactStoreModeSchema.default("hybrid")
454
+ });
455
+ var CodexGenerationConfigSchema = z.object({
456
+ enabled: z.boolean().default(false),
457
+ outputRoot: z.string().optional(),
458
+ dryRun: z.boolean().default(true)
459
+ });
460
+ var FailoverConfigSchema = z.object({
461
+ enabled: z.boolean().default(true),
462
+ timeoutMs: z.number().min(0).default(15e3),
463
+ retryDelayMs: z.number().min(0).default(500),
464
+ chains: FallbackChainsSchema.default({})
465
+ });
466
+ var PluginConfigSchema = z.object({
467
+ preset: z.string().optional(),
468
+ setDefaultAgent: z.boolean().optional(),
469
+ scoringEngineVersion: z.enum(["v1", "v2-shadow", "v2"]).optional(),
470
+ balanceProviderUsage: z.boolean().optional(),
471
+ manualPlan: ManualPlanSchema.optional(),
472
+ presets: z.record(z.string(), PresetSchema).optional(),
473
+ agents: z.record(z.string(), AgentOverrideConfigSchema).optional(),
474
+ disabled_mcps: z.array(z.string()).optional(),
475
+ tmux: TmuxConfigSchema.optional(),
476
+ fallback: FailoverConfigSchema.optional(),
477
+ thoth: ThothConfigSchema.optional(),
478
+ artifactStore: ArtifactStoreConfigSchema.optional(),
479
+ codex: CodexGenerationConfigSchema.optional()
480
+ });
481
+
482
+ // src/config/loader.ts
483
+ var PROMPTS_DIR_NAME = "thoth-agents";
484
+ function loadConfigFromPath(configPath) {
485
+ try {
486
+ const content = fs.readFileSync(configPath, "utf-8");
487
+ const rawConfig = JSON.parse(stripJsonComments(content));
488
+ const result = PluginConfigSchema.safeParse(rawConfig);
489
+ if (!result.success) {
490
+ console.warn(`[thoth-agents] Invalid config at ${configPath}:`);
491
+ console.warn(result.error.format());
492
+ return null;
493
+ }
494
+ return result.data;
495
+ } catch (error) {
496
+ if (error instanceof Error && "code" in error && error.code !== "ENOENT") {
497
+ console.warn(
498
+ `[thoth-agents] Error reading config from ${configPath}:`,
499
+ error.message
500
+ );
501
+ }
502
+ return null;
503
+ }
504
+ }
505
+ function findConfigPath(basePath) {
506
+ const jsoncPath = `${basePath}.jsonc`;
507
+ const jsonPath = `${basePath}.json`;
508
+ if (fs.existsSync(jsoncPath)) {
509
+ return jsoncPath;
510
+ }
511
+ if (fs.existsSync(jsonPath)) {
512
+ return jsonPath;
513
+ }
514
+ return null;
515
+ }
516
+ function deepMerge(base, override) {
517
+ if (!base) return override;
518
+ if (!override) return base;
519
+ const result = { ...base };
520
+ for (const key of Object.keys(override)) {
521
+ const baseVal = base[key];
522
+ const overrideVal = override[key];
523
+ if (typeof baseVal === "object" && baseVal !== null && typeof overrideVal === "object" && overrideVal !== null && !Array.isArray(baseVal) && !Array.isArray(overrideVal)) {
524
+ result[key] = deepMerge(
525
+ baseVal,
526
+ overrideVal
527
+ );
528
+ } else {
529
+ result[key] = overrideVal;
530
+ }
531
+ }
532
+ return result;
533
+ }
534
+ function loadPluginConfig(directory) {
535
+ const userConfigBasePath = path.join(getConfigDir(), "thoth-agents");
536
+ const projectConfigBasePath = path.join(
537
+ directory,
538
+ ".opencode",
539
+ "thoth-agents"
540
+ );
541
+ const userConfigPath = findConfigPath(userConfigBasePath);
542
+ const projectConfigPath = findConfigPath(projectConfigBasePath);
543
+ let config = userConfigPath ? loadConfigFromPath(userConfigPath) ?? {} : {};
544
+ const projectConfig = projectConfigPath ? loadConfigFromPath(projectConfigPath) : null;
545
+ if (projectConfig) {
546
+ config = {
547
+ ...config,
548
+ ...projectConfig,
549
+ agents: deepMerge(config.agents, projectConfig.agents),
550
+ tmux: deepMerge(config.tmux, projectConfig.tmux),
551
+ fallback: deepMerge(config.fallback, projectConfig.fallback),
552
+ thoth: deepMerge(config.thoth, projectConfig.thoth),
553
+ artifactStore: deepMerge(
554
+ config.artifactStore,
555
+ projectConfig.artifactStore
556
+ ),
557
+ codex: deepMerge(config.codex, projectConfig.codex)
558
+ };
559
+ }
560
+ const envPreset = process.env.THOTH_AGENTS_PRESET;
561
+ if (envPreset) {
562
+ config.preset = envPreset;
563
+ }
564
+ if (config.preset) {
565
+ const preset = config.presets?.[config.preset];
566
+ if (preset) {
567
+ config.agents = deepMerge(preset, config.agents);
568
+ } else {
569
+ const presetSource = envPreset === config.preset ? "environment variable" : "config file";
570
+ const availablePresets = config.presets ? Object.keys(config.presets).join(", ") : "none";
571
+ console.warn(
572
+ `[thoth-agents] Preset "${config.preset}" not found (from ${presetSource}). Available presets: ${availablePresets}`
573
+ );
574
+ }
575
+ }
576
+ return config;
577
+ }
578
+ function loadAgentPrompt(agentName, preset) {
579
+ const presetDirName = preset && /^[a-zA-Z0-9_-]+$/.test(preset) ? preset : void 0;
580
+ const promptsDir = path.join(getConfigDir(), PROMPTS_DIR_NAME);
581
+ const promptSearchDirs = presetDirName ? [path.join(promptsDir, presetDirName), promptsDir] : [promptsDir];
582
+ const result = {};
583
+ const readFirstPrompt = (fileName, errorPrefix) => {
584
+ for (const dir of promptSearchDirs) {
585
+ const promptPath = path.join(dir, fileName);
586
+ if (!fs.existsSync(promptPath)) {
587
+ continue;
588
+ }
589
+ try {
590
+ return fs.readFileSync(promptPath, "utf-8");
591
+ } catch (error) {
592
+ console.warn(
593
+ `[thoth-agents] ${errorPrefix} ${promptPath}:`,
594
+ error instanceof Error ? error.message : String(error)
595
+ );
596
+ }
597
+ }
598
+ return void 0;
599
+ };
600
+ result.prompt = readFirstPrompt(
601
+ `${agentName}.md`,
602
+ "Error reading prompt file"
603
+ );
604
+ result.appendPrompt = readFirstPrompt(
605
+ `${agentName}_append.md`,
606
+ "Error reading append prompt file"
607
+ );
608
+ return result;
609
+ }
610
+
611
+ // src/config/constants.ts
612
+ var AGENT_ALIASES = {
613
+ explore: "explorer",
614
+ "frontend-ui-ux-engineer": "designer"
615
+ };
616
+ var SUBAGENT_NAMES = [
617
+ "explorer",
618
+ "librarian",
619
+ "oracle",
620
+ "designer",
621
+ "quick",
622
+ "deep"
623
+ ];
624
+ var ORCHESTRATOR_NAME = "orchestrator";
625
+ var ALL_AGENT_NAMES = [
626
+ ORCHESTRATOR_NAME,
627
+ "explorer",
628
+ "librarian",
629
+ "oracle",
630
+ "designer",
631
+ "quick",
632
+ "deep"
633
+ ];
634
+ var DEFAULT_MODELS = {
635
+ orchestrator: void 0,
636
+ oracle: "openai/gpt-5.4",
637
+ librarian: "openai/gpt-5.4-mini",
638
+ explorer: "openai/gpt-5.4-mini",
639
+ designer: "openai/gpt-5.4-mini",
640
+ quick: "openai/gpt-5.4-mini",
641
+ deep: "openai/gpt-5.4"
642
+ };
643
+ var POLL_INTERVAL_BACKGROUND_MS = 2e3;
644
+ var DEFAULT_TIMEOUT_MS = 2 * 60 * 1e3;
645
+ var MAX_POLL_TIME_MS = 5 * 60 * 1e3;
646
+ var DEFAULT_THOTH_COMMAND = ["npx", "-y", "thoth-mem"];
647
+
648
+ // src/config/utils.ts
649
+ function getAgentOverride(config, name) {
650
+ const overrides = config?.agents ?? {};
651
+ return overrides[name] ?? overrides[Object.keys(AGENT_ALIASES).find((k) => AGENT_ALIASES[k] === name) ?? ""];
652
+ }
653
+
654
+ // src/agents/prompt-dialects.ts
655
+ var OPENCODE_CAPABILITIES = {
656
+ agentDefinitions: "supported",
657
+ delegatedExecution: "supported",
658
+ parallelDelegation: "supported",
659
+ runtimeHooks: "supported",
660
+ mcpConfiguration: "supported",
661
+ skillPackaging: "supported",
662
+ rolePermissions: "supported",
663
+ parentContextInjection: "supported",
664
+ memoryGovernanceEnforcement: "supported"
665
+ };
666
+ var CODEX_PROMPT_CAPABILITIES = {
667
+ agentDefinitions: "supported",
668
+ delegatedExecution: "instruction-only",
669
+ parallelDelegation: "instruction-only",
670
+ runtimeHooks: "unknown",
671
+ mcpConfiguration: "supported",
672
+ skillPackaging: "supported",
673
+ rolePermissions: "instruction-only",
674
+ parentContextInjection: "instruction-only",
675
+ memoryGovernanceEnforcement: "instruction-only"
676
+ };
677
+ function supportedCapabilityProfile(capabilities) {
678
+ return {
679
+ capabilities,
680
+ renderCapabilityDisclosure: () => void 0
681
+ };
682
+ }
683
+ function codexCapabilityDisclosure(capability) {
684
+ const status = CODEX_PROMPT_CAPABILITIES[capability];
685
+ if (status === "supported") {
686
+ return void 0;
687
+ }
688
+ if (status === "unknown") {
689
+ return `${capability}: unknown in Codex; treat related behavior as diagnostic-only unless the active Codex host documents support.`;
690
+ }
691
+ return `${capability}: ${status} in Codex; preserve the role responsibility as prompt guidance because equivalent runtime enforcement is not guaranteed.`;
692
+ }
693
+ var OPENCODE_PROMPT_DIALECT = {
694
+ harness: "opencode",
695
+ tools: {
696
+ delegationTool: "task",
697
+ backgroundDelegationTool: "task(background=true)",
698
+ backgroundStatusTool: "task_status",
699
+ userQuestionTool: "question",
700
+ progressTool: "todowrite",
701
+ hostStatusSurface: "task_status",
702
+ roleReference: (role) => `@${role}`
703
+ },
704
+ capabilities: supportedCapabilityProfile(OPENCODE_CAPABILITIES),
705
+ dispatchLabel(method) {
706
+ switch (method) {
707
+ case "root-coordinator":
708
+ return "root coordinator";
709
+ case "task":
710
+ return "task";
711
+ case "synchronous-task-only":
712
+ return "synchronous task only";
713
+ }
714
+ },
715
+ renderRoleInvocation(role) {
716
+ return `@${role}`;
717
+ }
718
+ };
719
+ var CODEX_PROMPT_DIALECT = {
720
+ harness: "codex",
721
+ tools: {
722
+ delegationTool: "Codex custom-agent task",
723
+ backgroundDelegationTool: "Codex background role-agent run",
724
+ backgroundStatusTool: "Codex host status surface",
725
+ userQuestionTool: "request_user_input",
726
+ progressTool: "Codex progress tracking surface",
727
+ hostStatusSurface: "Codex host status surface",
728
+ roleReference: (role) => `${role} role agent`
729
+ },
730
+ capabilities: {
731
+ capabilities: CODEX_PROMPT_CAPABILITIES,
732
+ renderCapabilityDisclosure: codexCapabilityDisclosure
733
+ },
734
+ dispatchLabel(method) {
735
+ switch (method) {
736
+ case "root-coordinator":
737
+ return "ambient Codex root session coordinator";
738
+ case "task":
739
+ return "Codex custom-agent task";
740
+ case "synchronous-task-only":
741
+ return "synchronous Codex custom-agent task only";
742
+ }
743
+ },
744
+ renderRoleInvocation(role) {
745
+ return role === "orchestrator" ? "orchestrator role agent" : `${role} subagent`;
746
+ }
747
+ };
748
+
749
+ // src/agents/prompt-sections.ts
750
+ function createQuestionProtocolSection() {
751
+ return {
752
+ kind: "question-protocol",
753
+ toolConcept: "userQuestion"
754
+ };
755
+ }
756
+ function createSubagentRulesSection(memoryAccess = "base") {
757
+ return {
758
+ kind: "subagent-rules",
759
+ memoryAccess,
760
+ progressConcept: "progress",
761
+ userQuestionConcept: "userQuestion"
762
+ };
763
+ }
764
+ function createResponseBudgetSection() {
765
+ return { kind: "response-budget" };
766
+ }
767
+ function createStepBudgetSection(steps) {
768
+ if (steps === void 0 || !Number.isInteger(steps) || steps <= 0) {
769
+ return void 0;
770
+ }
771
+ return { kind: "step-budget", steps };
772
+ }
773
+ function detectModelFamilyFromModel(model) {
774
+ const id = getPrimaryModelId(model)?.toLowerCase();
775
+ if (!id) {
776
+ return void 0;
777
+ }
778
+ if (id.includes("claude") || id.startsWith("anthropic/")) {
779
+ return "claude";
780
+ }
781
+ if (id.includes("gpt") || id.startsWith("openai/")) {
782
+ return "openai";
783
+ }
784
+ if (id.includes("gemini") || id.startsWith("google/")) {
785
+ return "gemini";
786
+ }
787
+ if (id.includes("kimi") || id.includes("k2")) {
788
+ return "kimi";
789
+ }
790
+ if (id.includes("glm") || id.startsWith("zai-")) {
791
+ return "glm";
792
+ }
793
+ return void 0;
794
+ }
795
+ function createModelFamilySection(role, model) {
796
+ const family = detectModelFamilyFromModel(model);
797
+ if (!family) {
798
+ return void 0;
799
+ }
800
+ return { kind: "model-family", role, family };
801
+ }
802
+ function roleText(template) {
803
+ return { kind: "role-text", template };
804
+ }
805
+ function specialistSections({
806
+ role,
807
+ mode,
808
+ dispatch,
809
+ scope,
810
+ responsibility,
811
+ rules,
812
+ memoryAccess,
813
+ output
814
+ }) {
815
+ const dispatchLabel = dispatch === "task" ? "{{dispatch.task}}" : "{{dispatch.synchronous-task-only}}";
816
+ return [
817
+ roleText(`<role>
818
+ You are ${role}.
819
+ </role>
820
+
821
+ <mode>
822
+ - Mode: ${mode}
823
+ - Dispatch method: ${dispatchLabel}
824
+ - Scope: ${scope}
825
+ </mode>
826
+
827
+ <responsibility>
828
+ ${responsibility}
829
+ </responsibility>
830
+
831
+ <rules>`),
832
+ createSubagentRulesSection(memoryAccess),
833
+ roleText(`${rules.join("\n")}
834
+ </rules>`),
835
+ createQuestionProtocolSection(),
836
+ roleText(`<output>`),
837
+ createResponseBudgetSection(),
838
+ roleText(`${output}
839
+ </output>`)
840
+ ];
841
+ }
842
+ function createOrchestratorPromptSections() {
843
+ return [
844
+ roleText(`<role>
845
+ You are the delegate-first root coordinator and decision engine for thoth-agents.
846
+ The root agent is the orchestrator/root coordinator for the session.
847
+ Orchestrator-only, root-only, or orchestrator-owned rules still apply even if
848
+ the harness does not name this agent "orchestrator".
849
+ </role>
850
+
851
+ <style>
852
+ Respond in the user's language. Be warm, direct, evidence-led, and concise.
853
+ Push back when context, risk, or assumptions are weak. Avoid verbosity.
854
+ </style>
855
+
856
+ <core-rules>
857
+ - Mode: primary coordinator. Mutation: none.
858
+ - Load \`thoth-mem-agents\` and \`requirements-interview\`.
859
+ - You MUST NOT read or write any file in the workspace except \`openspec/\` coordination artifacts for the SDD pipeline.
860
+ - Delegate all inspection, writing, searching, debugging, and verification.
861
+ - Own the thinking: analyze, choose approach, handle task sequencing, synthesize facts, decide, ask \`{{userQuestionTool}}\` for blocking user input, manage progress, own root-session memory, and write the final report.
862
+ - Use sub-agents for evidence and action, not to outsource architecture or planning.
863
+ - Never request raw file dumps from sub-agents; ask for findings, paths, line anchors, diffs, verification, and blockers.
864
+ - Use openspec/ for coordination artifacts, especially
865
+ openspec/changes/{change-name}/tasks.md.
866
+ - Visual or UX work and screenshots always go to {{role.designer}}.
867
+ - Verify through delegation, not inline.
868
+ - Verification should follow the user's project instructions and use the smallest sufficient delegated checks: typecheck, lint, focused tests, or build when appropriate.
869
+ - When a harness cannot enforce a rule directly, preserve the rule as instruction-only guidance and disclose the enforcement gap instead of weakening the contract.
870
+ </core-rules>
871
+
872
+ <session-bootstrap>
873
+ - At the start of a new root session, when thoth-mem tools are available, load \`thoth-mem-agents\` and \`requirements-interview\`, call \`mem_session_start\` with the current project and session identity, then save the real user prompt with \`mem_save_prompt\`.
874
+ - Save only the real user request with \`mem_save_prompt\`; never save generated sub-agent prompts, handoffs, summaries, or tool scaffolding as user intent.
875
+ - If thoth-mem tools or required session/project identity are unavailable, disclose that memory bootstrap could not run and continue without pretending memory was saved.
876
+ </session-bootstrap>
877
+
878
+ <routing>
879
+ {{role.explorer}}: read-only codebase discovery. Use for broad search, symbols, references, unknown paths, or multiple candidates.
880
+ {{role.librarian}}: read-only external docs/public examples. Use for version-sensitive APIs, official docs, or unfamiliar libraries.
881
+ {{role.oracle}}: read-only review/diagnosis. Use for architecture, security/correctness risk, plan review, persistent bugs, or high-stakes ambiguity.
882
+ {{role.designer}}: write-capable UI/UX owner. Use for user-facing UI, styles, layout, interactions, and all visual QA.
883
+ {{role.quick}}: write-capable narrow implementer. Use for clear, mechanical, low-risk, uniform edits.
884
+ {{role.deep}}: write-capable thorough implementer. Use for backend logic, data flow, APIs, state, refactors, edge cases, or correctness-critical work.
885
+
886
+ Tiebreakers:
887
+ - User-facing UI -> {{role.designer}}. Backend/system logic -> {{role.deep}}. Mechanical pattern -> {{role.quick}}.
888
+ - Discovery first when paths or facts are unknown; implementation agent may read known local context for its own task, but should not redo broad discovery already assigned to {{role.explorer}}/{{role.librarian}}.
889
+ - Do not use {{role.oracle}} for routine synthesis. After {{role.explorer}}/{{role.librarian}} results, you combine facts, inferences, unknowns, confidence, and next step.
890
+ </routing>
891
+
892
+ <subagent-prompts>
893
+ - Every sub-agent prompt you write must be in English, regardless of the user's language.
894
+ - Keep user-facing replies in the user's language, but translate delegated task prompts, internal handoffs, SDD envelopes, and verification requests into English.
895
+ - Prefer 2-3 surgical discovery probes over one broad exploration when independent facts can be gathered in parallel.
896
+ - A surgical probe asks one narrow question and returns only the anchors needed for your decision.
897
+ </subagent-prompts>
898
+
899
+ <internal-handoff>
900
+ Before dispatching {{role.designer}}, {{role.quick}}, or {{role.deep}} after discovery, synthesize a compact internal handoff. This is an implementation detail between you and sub-agents, not a user-facing step or artifact.
901
+
902
+ Internal handoff fields: Goal, Decision, Evidence, Scope, Steps, Verification, and Uncertainty. Include relevant files, symbols, anchors, constraints, non-goals, and what to escalate instead of guessing.
903
+
904
+ Never mention the internal handoff to the user, ask the user to prepare it, or present handoff preparation as the recommended next step. To the user, describe the actual work: discovery, design, implementation, verification, or the concrete decision needed.
905
+
906
+ For {{role.explorer}}/{{role.librarian}}, ask narrow fact-finding questions for likely files, symbols, call sites, constraints, examples, versioned API facts, and verification targets. Require decision-ready findings, not raw context.
907
+ </internal-handoff>
908
+
909
+ <dispatch>
910
+ - If independent delegations are ready, launch them in the same response.
911
+ - Default to normal synchronous \`{{delegationTool}}\` execution.
912
+ - Experimental background \`{{backgroundDelegationTool}}\` is allowed only for {{role.explorer}} and {{role.librarian}} for asynchronous delegation.
913
+ - {{role.oracle}}, {{role.designer}}, {{role.quick}}, and {{role.deep}} always use normal synchronous \`{{delegationTool}}\` execution.
914
+ - When using background \`{{delegationTool}}\`, treat it as conditional and non-portable: if the host does not expose the experimental path, fall back to normal synchronous \`{{delegationTool}}\`.
915
+ - Use \`{{backgroundStatusTool}}\` to wait, poll, and collect background task results before synthesizing or reporting completion.
916
+ - If a result is empty, contradictory, or low-confidence, retry once with a materially sharper prompt; then escalate with evidence via \`{{userQuestionTool}}\`.
917
+ - If a named subagent hits capacity, retry that same role up to 3 attempts.
918
+ - Never switch to \`default\`, \`worker\`, or any other role.
919
+ - After 3 failures, stay on the same role; if a same-role model override exists, use it. Otherwise report a capacity blocker.
920
+ - Write-capable dispatches must include the internal handoff when one exists, so implementers can edit instead of rediscovering the plan.
921
+ - Never tell sub-agents to discard working-tree changes.
922
+ </dispatch>
923
+
924
+ <sdd>
925
+ All work always starts with requirements-interview skill.
926
+
927
+ Routes:
928
+ - Direct implementation for low-complexity work.
929
+ - Accelerated SDD: propose -> tasks.
930
+ - Full SDD: propose -> spec -> design -> tasks.
931
+
932
+ Hard gates:
933
+ - Artifact-producing SDD phases are dispatched to {{role.deep}} or {{role.quick}} with the matching skill loaded.
934
+ - {{role.oracle}} is read-only and only handles plan-reviewer.
935
+ - Never skip artifacts or jump from requirements-interview to implementation when SDD is selected.
936
+ - Before SDD execution, load \`executing-plans\`; then track progress in {{progressTool}} plus the persistent artifact.
937
+ - If openspec persistence is selected and openspec/ is missing, dispatch sdd-init first.
938
+ - During SDD execution, batch compatible implementation work.
939
+ - Group consecutive ready SDD tasks for the same execution agent into one dispatch when dependencies, scope, and verification can be handled together. Keep per-task tracking and evidence; do not split a compatible {{role.designer}}/{{role.quick}}/{{role.deep}} run into one delegation per checkbox.
940
+
941
+ SDD dispatch envelope must include: skill name, persistence mode, pipeline type, change name, project name, needed prior artifact context, verification expectation, and return envelope.
942
+ After each phase, verify the sub-agent reported the openspec path and/or thoth-mem topic_key. Retry once if missing.
943
+
944
+ Artifact governance handoff:
945
+ - After \`sdd-tasks\`, you may surface report-only artifact governance findings before execution preparation starts.
946
+ - Delegate governance inspection; do not inspect repository artifacts inline.
947
+ - Do not treat governance findings as an execution gate.
948
+ - Do not let governance validation replace \`plan-reviewer\` or \`executing-plans\`.
949
+ - Root thoth-mem ownership stays with you; sub-agents may surface findings but must not own session memory, prompts, or progress checkpoints.
950
+
951
+ Plan gate: after tasks, ask with \`{{userQuestionTool}}\`: "Review plan with {{role.oracle}} before executing (Recommended)" or "Proceed to execution".
952
+ If reviewed, the review loop is complete only after [OKAY].
953
+ If {{role.oracle}} returns [OKAY], ask the user with \`{{userQuestionTool}}\` whether to proceed to implementation or stop with the approved plan.
954
+ Do not dispatch \`sdd-apply\` after oracle approval until the user confirms implementation.
955
+ Post-execution: delegate sdd-verify, then sdd-archive when verification passes.
956
+ </sdd>
957
+
958
+ <progress-memory>
959
+ - Keep {{progressTool}} top-level and lean for multi-step work.
960
+ - When SDD is active, update both {{progressTool}} and openspec/changes/{change-name}/tasks.md before dispatch and after results.
961
+ - Root-session memory is yours: search before repeated work; save durable decisions, discoveries, bugs, patterns, constraints, and session summaries.
962
+ - Durable \`mem_save\` guidance: save architecture decisions, accepted or rejected recommendations, bug fixes with root cause, non-obvious discoveries, conventions, configuration changes, and durable user preferences. Use stable topic keys for evolving topics, and keep general observations outside the protected \`sdd/*\` namespace.
963
+ - Targeted 3-layer recall protocol: \`mem_search\` with compact results -> \`mem_timeline\` around promising observations -> \`mem_get_observation\` only for records needed in full. Use preview search only when compact results do not disambiguate.
964
+ - SDD memory artifacts use deterministic topic keys only in thoth-mem or hybrid persistence modes: \`sdd/{change}/{artifact}\`.
965
+ - Before ending the root session, call \`mem_session_summary\` with a concise Goal, Instructions, Discoveries, Accomplished, Next Steps, and Relevant Files summary. Do not claim memory was saved unless the tool call succeeded.
966
+ - After compaction, first preserve the compacted summary with \`mem_session_summary\`, then recover recent context and use the 3-layer recall protocol before continuing work.
967
+ </progress-memory>
968
+
969
+ <communication>
970
+ State the plan briefly, delegate, then summarize outcomes without replaying raw work. Before any tool call or delegation, emit a short user-visible status/preamble that names the next action and target; for parallel dispatches, one compact sentence covering the batch is enough. Keep preambles about next action, evidence, and verification, not private reasoning. Separate evidence, inference, and uncertainty when it matters. Never ask blocking questions in prose.
971
+ </communication>`),
972
+ createQuestionProtocolSection()
973
+ ];
974
+ }
975
+ function createReadOnlySpecialistPromptSections(role) {
976
+ if (role === "explorer") {
977
+ return specialistSections({
978
+ role,
979
+ mode: "read-only",
980
+ dispatch: "task",
981
+ scope: "local repository discovery",
982
+ responsibility: "Find workspace facts fast. Return decision-ready evidence for internal handoffs: paths, lines, symbols, candidate files, constraints, edit targets, verification targets, and conclusions.",
983
+ rules: [
984
+ "- Questions should be rare; exhaust local evidence first.",
985
+ "- Prefer paths, lines, symbols, and concise summaries over dumps.",
986
+ "- Do not implement, edit files, mutate the repository, or own durable session memory.",
987
+ "- When full content is explicitly requested, reproduce it faithfully."
988
+ ],
989
+ memoryAccess: "readonly",
990
+ output: `
991
+ Return exactly these sections, in this order:
992
+
993
+ STATUS: one of CONFIRMED | PARTIAL | INCONCLUSIVE
994
+ - CONFIRMED = direct evidence answers the question with high confidence.
995
+ - PARTIAL = some direct evidence, but gaps remain or multiple candidates exist.
996
+ - INCONCLUSIVE = no sufficient evidence found. Never fabricate a confident answer from naming similarity alone.
997
+
998
+ FINDINGS: bullets with claim, evidence type [direct|inferred|assumed], confidence [high|medium|low], and file:line anchors for concrete claims.
999
+
1000
+ ALTERNATIVES CONSIDERED: ranked candidates when more than one plausible match exists. Omit if only one candidate.
1001
+
1002
+ UNRESOLVED QUESTIONS: ambiguity and what context would unblock it.
1003
+
1004
+ UNCHECKED AREAS: what you did not inspect that could change the answer. Omit if nothing notable.
1005
+
1006
+ SHORT EVIDENCE: at most one 2-line excerpt per key finding.
1007
+
1008
+ Lead with STATUS. Stay under 40 lines total when possible. If the schema forces more lines, exceed the budget rather than drop required fields.`
1009
+ });
1010
+ }
1011
+ if (role === "librarian") {
1012
+ return specialistSections({
1013
+ role,
1014
+ mode: "read-only",
1015
+ dispatch: "task",
1016
+ scope: "external docs and research plus local confirmation when needed",
1017
+ responsibility: "Gather authoritative external evidence that helps the orchestrator make implementation decisions. Prefer official docs first, include version sensitivity, then high-signal public examples. Every substantive claim must carry a source URL.",
1018
+ rules: [
1019
+ "- Questions should be rare; exhaust available sources first.",
1020
+ "- Prefer official documentation over commentary when both answer the same point.",
1021
+ "- Distinguish clearly between official guidance and community examples.",
1022
+ "- Do not mutate the repository, invent undocumented APIs, or perform broad implementation work."
1023
+ ],
1024
+ memoryAccess: "readonly",
1025
+ output: `- Organize by finding. Include a source URL for every claim.
1026
+ - Distinguish official docs from community examples.
1027
+ - Return synthesized findings, not full documentation excerpts.
1028
+ - Target: under 40 lines total.`
1029
+ });
1030
+ }
1031
+ return specialistSections({
1032
+ role,
1033
+ mode: "read-only",
1034
+ dispatch: "synchronous task only",
1035
+ scope: "advice, diagnosis, architecture, code review, and plan review",
1036
+ responsibility: "Provide read-only review and strategic technical guidance anchored to evidence, including findings, risks, assumptions, and decision-ready conclusions. Use systematic-debugging for bugs, plan-reviewer for SDD plans, and web-assisted research when deeper diagnosis needs it.",
1037
+ rules: [
1038
+ "- Cite exact files and lines for local claims.",
1039
+ "- Separate observations, risks, and recommendations.",
1040
+ "- Ask only when tradeoffs, risk tolerance, or approval materially change the recommendation.",
1041
+ "- Do not produce SDD artifacts, implement edits, or mutate the workspace."
1042
+ ],
1043
+ memoryAccess: "readonly",
1044
+ output: `- Cite exact files and lines \u2014 do not quote large code blocks.
1045
+ - Separate observations, risks, and recommendations.
1046
+ - For diagnosis: root cause + fix recommendation, not step-by-step trace.
1047
+ - Target: under 50 lines total.`
1048
+ });
1049
+ }
1050
+ function createWriteCapableSpecialistPromptSections(role) {
1051
+ if (role === "designer") {
1052
+ return specialistSections({
1053
+ role,
1054
+ mode: "write-capable",
1055
+ dispatch: "synchronous task only",
1056
+ scope: "UI/UX decisions, implementation, and visual verification",
1057
+ responsibility: "Own the user-facing solution: choose the UX approach, implement it, and verify it visually through the visual verification surface across responsive states. Capture evidence once in non-blocking single-run mode.\nFor visual QA-only tasks, inspect the UI, summarize what is correct, note issues, recommend fixes.",
1058
+ rules: [
1059
+ "- Treat the orchestrator's internal handoff as the handoff; do not rediscover settled scope or constraints.",
1060
+ "- Own UX decisions unless a real user preference is required.",
1061
+ "- Verify visually and check responsive behavior when feasible; do not stop at code that merely compiles.",
1062
+ "- Delete temporary evidence you generated, such as screenshots, once it is no longer needed; if it is for root/orchestrator review, list it in your final output as garbage to delete after review.",
1063
+ "- Keep changes focused on the user-facing outcome.",
1064
+ "- Preserve unrelated working-tree changes.",
1065
+ "- Avoid interactive or persistent visual verification unless explicitly requested; keep it single-run, evidence-driven."
1066
+ ],
1067
+ memoryAccess: "writable",
1068
+ output: `For SDD tasks: use the Task Result envelope (Status, Task, What was done, Files changed, Verification, Issues).
1069
+ For non-SDD work: state what was implemented, verification status, and remaining caveats.
1070
+ - Include visual verification status when applicable.
1071
+ - Target: under 30 lines total.`
1072
+ });
1073
+ }
1074
+ if (role === "quick") {
1075
+ return specialistSections({
1076
+ role,
1077
+ mode: "write-capable",
1078
+ dispatch: "synchronous task only",
1079
+ scope: "fast bounded implementation",
1080
+ responsibility: "Implement well-defined changes quickly. Favor speed over exhaustive analysis when the task is narrow, low-risk, mechanical, and the path is clear.",
1081
+ rules: [
1082
+ "- Optimize for fast execution on narrow, clear tasks.",
1083
+ "- Treat the orchestrator's internal handoff as the starting point; follow its file anchors, scope, non-goals, and verification target.",
1084
+ "- Preserve unrelated working-tree changes.",
1085
+ "- Read only the context you need.",
1086
+ "- Do not redo broad discovery. If the handoff lacks essential anchors, surface the missing context instead of turning the task into open-ended exploration.",
1087
+ "- Avoid multi-step planning; if the task stops being bounded, surface it.",
1088
+ "- Ask only for implementation-local ambiguity, not orchestrator-level routing.",
1089
+ "- NEVER run git commands that discard changes (`git restore`, `git checkout --`, `git reset`, `git clean`). Files modified by prior tasks are intentional SDD progress, not unintended changes."
1090
+ ],
1091
+ memoryAccess: "writable",
1092
+ output: `For SDD tasks: use the Task Result envelope (Status, Task, What was done, Files changed, Verification, Issues).
1093
+ For non-SDD work: status + summary + files changed + issues. Nothing more.
1094
+ - Target: under 20 lines total.`
1095
+ });
1096
+ }
1097
+ return specialistSections({
1098
+ role,
1099
+ mode: "write-capable",
1100
+ dispatch: "synchronous task only",
1101
+ scope: "thorough implementation and verification",
1102
+ responsibility: "Handle correctness-critical, multi-file, or edge-case-heavy changes with full local context analysis. Use test-driven-development and systematic-debugging when relevant before implementing fixes.",
1103
+ rules: [
1104
+ "- Treat the orchestrator's internal handoff as the architecture handoff; validate it against nearby code, but do not restart upstream discovery unless evidence contradicts it.",
1105
+ "- Do not skip verification \u2014 thoroughness is your value proposition.",
1106
+ "- Investigate related files, types, and call sites before changing shared behavior, prioritizing the anchors and constraints in the handoff.",
1107
+ "- Preserve unrelated working-tree changes.",
1108
+ "- Ask when a real architecture or implementation tradeoff blocks correct execution."
1109
+ ],
1110
+ memoryAccess: "writable",
1111
+ output: `For SDD tasks: use the Task Result envelope (Status, Task, What was done, Files changed, Verification, Issues).
1112
+ For non-SDD work: summary + files changed + verification results + edge cases considered.
1113
+ - Save detailed analysis for follow-up requests; return only actionable conclusions.
1114
+ - Target: under 40 lines total.`
1115
+ });
1116
+ }
1117
+ function getPrimaryModelId(model) {
1118
+ if (Array.isArray(model)) {
1119
+ const first = model[0];
1120
+ return typeof first === "string" ? first : first?.id;
1121
+ }
1122
+ return model;
1123
+ }
1124
+ function renderQuestionProtocol(_section, dialect) {
1125
+ return `<questions>
1126
+ Use \`${dialect.tools.userQuestionTool}\` only for blocking choices: unresolved ambiguity that changes the result, destructive/security-sensitive actions, or missing secrets. Do all non-blocked work first, ask one targeted question with a recommended default first, then stop.
1127
+ </questions>`;
1128
+ }
1129
+ function renderSubagentRules(section, dialect) {
1130
+ const rules = [
1131
+ `- Single-task leaf agent: do not delegate, manage SDD phases, act as orchestrator, or call \`${dialect.tools.progressTool}\`.`,
1132
+ `- Use \`${dialect.tools.userQuestionTool}\` only for local blocking decisions.`,
1133
+ "- Never discard working-tree changes: no `git restore`, `git checkout -- <path>`, `git reset --hard`, `git clean`, or `git stash`.",
1134
+ "- Avoid blocking/watch commands; use terminating checks only."
1135
+ ];
1136
+ if (section.memoryAccess === "readonly") {
1137
+ rules.push(
1138
+ "- Use read-only thoth-mem only when dispatch gives session_id/project: `mem_search` -> `mem_timeline` -> `mem_get_observation`.",
1139
+ "- Never call `mem_session_start`, `mem_session_summary`, or `mem_save_prompt`; those tools are orchestrator-owned.",
1140
+ "- Never write memory; memory writes are orchestrator-owned."
1141
+ );
1142
+ }
1143
+ if (section.memoryAccess === "writable") {
1144
+ rules.push(
1145
+ "- Use delegated thoth-mem tools only (mem_save, mem_search, mem_get_observation, mem_timeline, mem_suggest_topic_key).",
1146
+ "- Never call `mem_session_start`, `mem_session_summary`, or `mem_save_prompt`; those tools are orchestrator-owned.",
1147
+ "- Always use the parent session_id/project from dispatch for every thoth-mem call.",
1148
+ "- If either is missing, do NOT call thoth-mem.",
1149
+ "- For reads, use only `mem_search` -> `mem_timeline` -> `mem_get_observation`.",
1150
+ "- You do not own durable memory of your own; `mem_save` writes under the orchestrator's session/project only."
1151
+ );
1152
+ }
1153
+ return rules.join("\n");
1154
+ }
1155
+ function renderResponseBudget() {
1156
+ return "Return concise structured results: status, summary, files, verification/issues. Never return raw file dumps.";
1157
+ }
1158
+ function renderStepBudget(section) {
1159
+ return `<step-budget>
1160
+ - You have a hard execution budget of ${section.steps} steps.
1161
+ - Plan your tool use before acting, prioritize the highest-signal checks first, and stop once you have enough evidence to answer.
1162
+ - Avoid repeated searches or reads. If the remaining work will exceed the budget, return partial findings with the next best target instead of looping.
1163
+ </step-budget>`;
1164
+ }
1165
+ function getRoleModelProfile(role) {
1166
+ switch (role) {
1167
+ case "orchestrator":
1168
+ return "- Exploit your role by selecting the right specialist category, launching independent tasks together, and synthesizing facts/inferences/unknowns before the next dispatch.";
1169
+ case "explorer":
1170
+ return "- Exploit your role by scanning broadly first, then narrowing to symbol/path evidence with ranked candidates and confidence.";
1171
+ case "librarian":
1172
+ return "- Exploit your role by prioritizing official docs, dates, versions, and source quality before summarizing public examples.";
1173
+ case "oracle":
1174
+ return "- Exploit your role by challenging assumptions, identifying risk, and giving a decision-ready recommendation backed by evidence.";
1175
+ case "designer":
1176
+ return "- Exploit your role by making concrete UX choices, implementing them, and verifying the visible result instead of stopping at code review.";
1177
+ case "quick":
1178
+ return "- Exploit your role by applying the smallest clear edit, avoiding broad exploration, and returning immediately after focused verification.";
1179
+ case "deep":
1180
+ return "- Exploit your role by building a complete mental model of shared behavior, writing tests first when behavior changes, and verifying edge cases.";
1181
+ }
1182
+ }
1183
+ function renderModelFamily(section) {
1184
+ const roleGuidance = getRoleModelProfile(section.role);
1185
+ if (section.family === "claude") {
1186
+ return `<model-profile family="claude">
1187
+ - Use XML-like sections, label uncertainty, and delegate aggressively when agentic.
1188
+ ${roleGuidance}
1189
+ </model-profile>`;
1190
+ }
1191
+ if (section.family === "openai") {
1192
+ return `<model-profile family="openai">
1193
+ - Plan briefly, then act. Keep tool dispatch explicit: action, target, return shape.
1194
+ ${roleGuidance}
1195
+ </model-profile>`;
1196
+ }
1197
+ if (section.family === "gemini") {
1198
+ return `<model-profile family="gemini">
1199
+ - Use long-context breadth deliberately, then ground conclusions in exact anchors.
1200
+ ${roleGuidance}
1201
+ </model-profile>`;
1202
+ }
1203
+ if (section.family === "kimi") {
1204
+ return `<model-profile family="kimi">
1205
+ - Favor repository-scale navigation before edits; keep patches grounded in current file state.
1206
+ ${roleGuidance}
1207
+ </model-profile>`;
1208
+ }
1209
+ return `<model-profile family="glm">
1210
+ - Use compact checklists, conservative steps, clear verification, and concrete blockers.
1211
+ ${roleGuidance}
1212
+ </model-profile>`;
1213
+ }
1214
+ function renderRoleText(section, dialect) {
1215
+ return section.template.replaceAll("{{delegationTool}}", dialect.tools.delegationTool).replaceAll(
1216
+ "{{backgroundDelegationTool}}",
1217
+ dialect.tools.backgroundDelegationTool ?? dialect.tools.delegationTool
1218
+ ).replaceAll(
1219
+ "{{backgroundStatusTool}}",
1220
+ dialect.tools.backgroundStatusTool ?? dialect.tools.hostStatusSurface ?? ""
1221
+ ).replaceAll("{{userQuestionTool}}", dialect.tools.userQuestionTool).replaceAll("{{progressTool}}", dialect.tools.progressTool).replaceAll("{{dispatch.task}}", dialect.dispatchLabel("task")).replaceAll(
1222
+ "{{dispatch.synchronous-task-only}}",
1223
+ dialect.dispatchLabel("synchronous-task-only")
1224
+ ).replace(
1225
+ /\{\{role\.(\w+)\}\}/g,
1226
+ (_match, role) => dialect.renderRoleInvocation(role)
1227
+ );
1228
+ }
1229
+ function renderPromptSection(section, dialect) {
1230
+ switch (section.kind) {
1231
+ case "question-protocol":
1232
+ return renderQuestionProtocol(section, dialect);
1233
+ case "subagent-rules":
1234
+ return renderSubagentRules(section, dialect);
1235
+ case "response-budget":
1236
+ return renderResponseBudget();
1237
+ case "step-budget":
1238
+ return renderStepBudget(section);
1239
+ case "model-family":
1240
+ return renderModelFamily(section);
1241
+ case "role-text":
1242
+ return renderRoleText(section, dialect);
1243
+ }
1244
+ }
1245
+ function renderRolePrompt(sections, dialect) {
1246
+ return sections.map((section) => renderPromptSection(section, dialect).trim()).filter(Boolean).join("\n\n");
1247
+ }
1248
+
1249
+ // src/agents/prompt-utils.ts
1250
+ var QUESTION_PROTOCOL = renderPromptSection(
1251
+ createQuestionProtocolSection(),
1252
+ OPENCODE_PROMPT_DIALECT
1253
+ );
1254
+ var SUBAGENT_RULES = renderPromptSection(
1255
+ createSubagentRulesSection("base"),
1256
+ OPENCODE_PROMPT_DIALECT
1257
+ );
1258
+ var SUBAGENT_RULES_READONLY = renderPromptSection(
1259
+ createSubagentRulesSection("readonly"),
1260
+ OPENCODE_PROMPT_DIALECT
1261
+ );
1262
+ var SUBAGENT_RULES_WRITABLE = renderPromptSection(
1263
+ createSubagentRulesSection("writable"),
1264
+ OPENCODE_PROMPT_DIALECT
1265
+ );
1266
+ var RESPONSE_BUDGET = renderPromptSection(
1267
+ createResponseBudgetSection(),
1268
+ OPENCODE_PROMPT_DIALECT
1269
+ );
1270
+ function trimPromptSection(section) {
1271
+ const trimmed = section?.trim();
1272
+ return trimmed ? trimmed : void 0;
1273
+ }
1274
+ function appendPromptSections(...sections) {
1275
+ return sections.map(trimPromptSection).filter(Boolean).join("\n\n");
1276
+ }
1277
+ function detectModelFamily(model) {
1278
+ return detectModelFamilyFromModel(model);
1279
+ }
1280
+ function getStepBudgetPromptSection(steps) {
1281
+ const section = createStepBudgetSection(steps);
1282
+ if (!section) {
1283
+ return void 0;
1284
+ }
1285
+ return renderPromptSection(section, OPENCODE_PROMPT_DIALECT);
1286
+ }
1287
+ function getModelFamilyPromptSection(role, model) {
1288
+ const section = createModelFamilySection(role, model);
1289
+ if (!section) {
1290
+ return void 0;
1291
+ }
1292
+ return renderPromptSection(section, OPENCODE_PROMPT_DIALECT);
1293
+ }
1294
+ function replacePromptPlaceholders(template, placeholders = {}) {
1295
+ return Object.entries(placeholders).reduce((prompt, [key, value]) => {
1296
+ if (value === void 0) {
1297
+ return prompt;
1298
+ }
1299
+ return prompt.replaceAll(`{{${key}}}`, String(value));
1300
+ }, template);
1301
+ }
1302
+ function composeAgentPrompt({
1303
+ basePrompt,
1304
+ customPrompt,
1305
+ customAppendPrompt,
1306
+ placeholders
1307
+ }) {
1308
+ const resolvedBase = replacePromptPlaceholders(basePrompt, placeholders);
1309
+ if (customPrompt) {
1310
+ return replacePromptPlaceholders(customPrompt, placeholders);
1311
+ }
1312
+ return appendPromptSections(
1313
+ resolvedBase,
1314
+ replacePromptPlaceholders(customAppendPrompt ?? "", placeholders)
1315
+ );
1316
+ }
1317
+
1318
+ // src/cli/custom-skills.ts
1319
+ import {
1320
+ copyFileSync as copyFileSync2,
1321
+ existsSync as existsSync5,
1322
+ mkdirSync as mkdirSync3,
1323
+ readdirSync as readdirSync2,
1324
+ rmSync,
1325
+ statSync as statSync3
1326
+ } from "fs";
1327
+ import { dirname as dirname2, join as join4, parse } from "path";
1328
+ import { fileURLToPath } from "url";
1329
+
1330
+ // src/harness/core/skills.ts
1331
+ var ORCHESTRATOR_ONLY = ["orchestrator"];
1332
+ var SHARED_SKILL_SUPPORT = {
1333
+ name: "_shared",
1334
+ description: "Shared OpenSpec, persistence, and thoth-mem SDD conventions",
1335
+ allowedRoles: [
1336
+ "orchestrator",
1337
+ "explorer",
1338
+ "librarian",
1339
+ "oracle",
1340
+ "designer",
1341
+ "quick",
1342
+ "deep"
1343
+ ],
1344
+ sourcePath: "src/skills/_shared",
1345
+ kind: "shared-support",
1346
+ purpose: "support"
1347
+ };
1348
+ var BUNDLED_SKILL_REGISTRY = [
1349
+ {
1350
+ name: "requirements-interview",
1351
+ description: "Mandatory step-0 discovery interview to understand user intent, clarify scope, and choose the right path before implementation",
1352
+ allowedRoles: ORCHESTRATOR_ONLY,
1353
+ sourcePath: "src/skills/requirements-interview",
1354
+ kind: "skill",
1355
+ purpose: "requirements"
1356
+ },
1357
+ {
1358
+ name: "thoth-mem-agents",
1359
+ description: "Orchestrator/subagent thoth-mem workflow contract for parent session_id/project ownership, prompt-save prohibitions, and safe durable memory usage",
1360
+ allowedRoles: [
1361
+ "orchestrator",
1362
+ "explorer",
1363
+ "librarian",
1364
+ "oracle",
1365
+ "designer",
1366
+ "quick",
1367
+ "deep"
1368
+ ],
1369
+ sourcePath: "src/skills/thoth-mem-agents",
1370
+ kind: "skill",
1371
+ purpose: "memory"
1372
+ },
1373
+ {
1374
+ name: "plan-reviewer",
1375
+ description: "Review SDD task plans for execution blockers and valid references",
1376
+ allowedRoles: ["orchestrator", "oracle"],
1377
+ sourcePath: "src/skills/plan-reviewer",
1378
+ kind: "skill",
1379
+ purpose: "review"
1380
+ },
1381
+ {
1382
+ name: "sdd-init",
1383
+ description: "Initialize OpenSpec structure and SDD project context",
1384
+ allowedRoles: ORCHESTRATOR_ONLY,
1385
+ sourcePath: "src/skills/sdd-init",
1386
+ kind: "skill",
1387
+ purpose: "sdd"
1388
+ },
1389
+ {
1390
+ name: "sdd-propose",
1391
+ description: "Create change proposals for OpenSpec workflows",
1392
+ allowedRoles: ORCHESTRATOR_ONLY,
1393
+ sourcePath: "src/skills/sdd-propose",
1394
+ kind: "skill",
1395
+ purpose: "sdd"
1396
+ },
1397
+ {
1398
+ name: "sdd-spec",
1399
+ description: "Write OpenSpec delta specifications",
1400
+ allowedRoles: ORCHESTRATOR_ONLY,
1401
+ sourcePath: "src/skills/sdd-spec",
1402
+ kind: "skill",
1403
+ purpose: "sdd"
1404
+ },
1405
+ {
1406
+ name: "sdd-design",
1407
+ description: "Create technical solution design artifacts for changes",
1408
+ allowedRoles: ORCHESTRATOR_ONLY,
1409
+ sourcePath: "src/skills/sdd-design",
1410
+ kind: "skill",
1411
+ purpose: "sdd"
1412
+ },
1413
+ {
1414
+ name: "sdd-tasks",
1415
+ description: "Generate phased implementation task checklists",
1416
+ allowedRoles: ORCHESTRATOR_ONLY,
1417
+ sourcePath: "src/skills/sdd-tasks",
1418
+ kind: "skill",
1419
+ purpose: "sdd"
1420
+ },
1421
+ {
1422
+ name: "sdd-apply",
1423
+ description: "Execute tasks and persist implementation progress",
1424
+ allowedRoles: ORCHESTRATOR_ONLY,
1425
+ sourcePath: "src/skills/sdd-apply",
1426
+ kind: "skill",
1427
+ purpose: "sdd"
1428
+ },
1429
+ {
1430
+ name: "executing-plans",
1431
+ description: "Execute SDD task lists with real-time progress tracking, sub-agent dispatch, and verification checkpoints",
1432
+ allowedRoles: ORCHESTRATOR_ONLY,
1433
+ sourcePath: "src/skills/executing-plans",
1434
+ kind: "skill",
1435
+ purpose: "sdd"
1436
+ },
1437
+ {
1438
+ name: "sdd-verify",
1439
+ description: "Build verification reports and compliance matrices",
1440
+ allowedRoles: ORCHESTRATOR_ONLY,
1441
+ sourcePath: "src/skills/sdd-verify",
1442
+ kind: "skill",
1443
+ purpose: "sdd"
1444
+ },
1445
+ {
1446
+ name: "sdd-archive",
1447
+ description: "Archive completed OpenSpec changes with audit trails",
1448
+ allowedRoles: ORCHESTRATOR_ONLY,
1449
+ sourcePath: "src/skills/sdd-archive",
1450
+ kind: "skill",
1451
+ purpose: "sdd"
1452
+ }
1453
+ ];
1454
+ var SKILL_REGISTRY = [
1455
+ ...BUNDLED_SKILL_REGISTRY,
1456
+ SHARED_SKILL_SUPPORT
1457
+ ];
1458
+ function getSkillRegistry() {
1459
+ return SKILL_REGISTRY.map((entry) => ({
1460
+ ...entry,
1461
+ allowedRoles: [...entry.allowedRoles]
1462
+ }));
1463
+ }
1464
+
1465
+ // src/cli/skill-manifest.ts
1466
+ import { createHash } from "crypto";
1467
+ import {
1468
+ existsSync as existsSync4,
1469
+ mkdirSync as mkdirSync2,
1470
+ readdirSync,
1471
+ readFileSync as readFileSync3,
1472
+ statSync as statSync2,
1473
+ writeFileSync as writeFileSync2
1474
+ } from "fs";
1475
+ import { join as join3, relative } from "path";
1476
+ var SHARED_SKILL_DIRECTORY = "_shared";
1477
+ var SKILLS_SOURCE_ROOT = join3("src", "skills");
1478
+ var MANIFEST_FILE_NAME = ".skill-manifest.json";
1479
+ function getManifestPath() {
1480
+ return join3(getCustomSkillsDir(), MANIFEST_FILE_NAME);
1481
+ }
1482
+ function listFilesRecursive(dirPath) {
1483
+ if (!existsSync4(dirPath)) {
1484
+ return [];
1485
+ }
1486
+ const files = [];
1487
+ const entries = readdirSync(dirPath).sort(
1488
+ (left, right) => left.localeCompare(right)
1489
+ );
1490
+ for (const entry of entries) {
1491
+ const entryPath = join3(dirPath, entry);
1492
+ const stat = statSync2(entryPath);
1493
+ if (stat.isDirectory()) {
1494
+ files.push(...listFilesRecursive(entryPath));
1495
+ continue;
1496
+ }
1497
+ files.push(entryPath);
1498
+ }
1499
+ return files;
1500
+ }
1501
+ function readPackageVersion(packageRoot) {
1502
+ const packageJsonPath = join3(packageRoot, "package.json");
1503
+ const packageJson = JSON.parse(readFileSync3(packageJsonPath, "utf-8"));
1504
+ if (typeof packageJson.version !== "string" || packageJson.version.length === 0) {
1505
+ throw new Error(`Invalid package version in ${packageJsonPath}`);
1506
+ }
1507
+ return packageJson.version;
1508
+ }
1509
+ function readManifest() {
1510
+ const manifestPath = getManifestPath();
1511
+ if (!existsSync4(manifestPath)) {
1512
+ return null;
1513
+ }
1514
+ try {
1515
+ return JSON.parse(readFileSync3(manifestPath, "utf-8"));
1516
+ } catch (error) {
1517
+ console.warn(`Failed to read skill manifest: ${manifestPath}`, error);
1518
+ return null;
1519
+ }
1520
+ }
1521
+ function writeManifest(manifest) {
1522
+ const manifestPath = getManifestPath();
1523
+ mkdirSync2(getCustomSkillsDir(), { recursive: true });
1524
+ writeFileSync2(manifestPath, `${JSON.stringify(manifest, null, 2)}
1525
+ `);
1526
+ }
1527
+ function computeSkillHash(skillDirPath) {
1528
+ const hash = createHash("sha256");
1529
+ for (const filePath of listFilesRecursive(skillDirPath)) {
1530
+ hash.update(`${relative(skillDirPath, filePath)}
1531
+ `);
1532
+ hash.update(readFileSync3(filePath));
1533
+ hash.update("\n");
1534
+ }
1535
+ return hash.digest("hex");
1536
+ }
1537
+ function findRemovedSkills(manifest) {
1538
+ const currentSkillNames = new Set(CUSTOM_SKILLS.map((skill) => skill.name));
1539
+ return Object.keys(manifest.skills).filter(
1540
+ (skillName) => !currentSkillNames.has(skillName)
1541
+ );
1542
+ }
1543
+ function checkSkillsNeedUpdate(packageRoot) {
1544
+ const manifest = readManifest();
1545
+ const pluginVersion = readPackageVersion(packageRoot);
1546
+ const sharedHash = computeSkillHash(
1547
+ join3(packageRoot, SKILLS_SOURCE_ROOT, SHARED_SKILL_DIRECTORY)
1548
+ );
1549
+ const versionChanged = manifest !== null && manifest.pluginVersion !== pluginVersion;
1550
+ const sharedChanged = manifest !== null && manifest.sharedHash !== sharedHash;
1551
+ const removedSkills = manifest ? findRemovedSkills(manifest) : [];
1552
+ const skillsNeedingUpdate = CUSTOM_SKILLS.flatMap((skill) => {
1553
+ const sourcePath = join3(packageRoot, skill.sourcePath);
1554
+ const targetPath = join3(getCustomSkillsDir(), skill.name);
1555
+ const sourceHash = computeSkillHash(sourcePath);
1556
+ const manifestEntry = manifest?.skills[skill.name];
1557
+ const reasons = [];
1558
+ if (!manifest) {
1559
+ reasons.push("manifest-missing");
1560
+ }
1561
+ if (versionChanged) {
1562
+ reasons.push("version-change");
1563
+ }
1564
+ if (sharedChanged) {
1565
+ reasons.push("shared-hash-mismatch");
1566
+ }
1567
+ if (!manifestEntry) {
1568
+ reasons.push("new-skill");
1569
+ } else if (manifestEntry.hash !== sourceHash) {
1570
+ reasons.push("hash-mismatch");
1571
+ }
1572
+ if (!existsSync4(targetPath)) {
1573
+ reasons.push("missing-install");
1574
+ }
1575
+ if (reasons.length === 0) {
1576
+ return [];
1577
+ }
1578
+ return [
1579
+ {
1580
+ skill,
1581
+ sourceHash,
1582
+ targetPath,
1583
+ reasons
1584
+ }
1585
+ ];
1586
+ });
1587
+ return {
1588
+ pluginVersion,
1589
+ sharedHash,
1590
+ manifest,
1591
+ versionChanged,
1592
+ sharedChanged,
1593
+ needsUpdate: skillsNeedingUpdate.length > 0 || removedSkills.length > 0,
1594
+ skillsNeedingUpdate,
1595
+ removedSkills
1596
+ };
1597
+ }
1598
+
1599
+ // src/cli/custom-skills.ts
1600
+ var SHARED_SKILL_DIRECTORY2 = "_shared";
1601
+ var SHARED_SKILL_SOURCE_PATH = `src/skills/${SHARED_SKILL_DIRECTORY2}`;
1602
+ var CUSTOM_SKILLS = BUNDLED_SKILL_REGISTRY.map(
1603
+ (skill) => ({
1604
+ name: skill.name,
1605
+ description: skill.description,
1606
+ allowedAgents: [...skill.allowedRoles],
1607
+ sourcePath: skill.sourcePath
1608
+ })
1609
+ );
1610
+ function getCustomSkillsDir() {
1611
+ return join4(getConfigDir(), "skills");
1612
+ }
1613
+ function copyDirRecursive(src, dest) {
1614
+ if (!existsSync5(dest)) {
1615
+ mkdirSync3(dest, { recursive: true });
1616
+ }
1617
+ const entries = readdirSync2(src);
1618
+ for (const entry of entries) {
1619
+ const srcPath = join4(src, entry);
1620
+ const destPath = join4(dest, entry);
1621
+ const stat = statSync3(srcPath);
1622
+ if (stat.isDirectory()) {
1623
+ copyDirRecursive(srcPath, destPath);
1624
+ } else {
1625
+ const destDir = dirname2(destPath);
1626
+ if (!existsSync5(destDir)) {
1627
+ mkdirSync3(destDir, { recursive: true });
1628
+ }
1629
+ copyFileSync2(srcPath, destPath);
1630
+ }
1631
+ }
1632
+ }
1633
+ function installSharedSkillAssets(packageRoot) {
1634
+ const sharedSourcePath = join4(packageRoot, SHARED_SKILL_SOURCE_PATH);
1635
+ const sharedTargetPath = join4(getCustomSkillsDir(), SHARED_SKILL_DIRECTORY2);
1636
+ if (!existsSync5(sharedSourcePath)) {
1637
+ console.error(`Custom skill shared assets not found: ${sharedSourcePath}`);
1638
+ return false;
1639
+ }
1640
+ rmSync(sharedTargetPath, { recursive: true, force: true });
1641
+ copyDirRecursive(sharedSourcePath, sharedTargetPath);
1642
+ return true;
1643
+ }
1644
+ function findPackageRoot(startDir) {
1645
+ let currentDir = startDir;
1646
+ const filesystemRoot = parse(startDir).root;
1647
+ while (true) {
1648
+ if (existsSync5(join4(currentDir, "package.json")) && existsSync5(join4(currentDir, "src", "skills"))) {
1649
+ return currentDir;
1650
+ }
1651
+ if (currentDir === filesystemRoot) {
1652
+ return null;
1653
+ }
1654
+ const parentDir = dirname2(currentDir);
1655
+ if (parentDir === currentDir) {
1656
+ return null;
1657
+ }
1658
+ currentDir = parentDir;
1659
+ }
1660
+ }
1661
+ function resolvePackageRoot(packageRoot) {
1662
+ if (packageRoot) {
1663
+ return packageRoot;
1664
+ }
1665
+ const moduleDir = fileURLToPath(new URL(".", import.meta.url));
1666
+ return findPackageRoot(moduleDir) ?? fileURLToPath(new URL("../..", import.meta.url));
1667
+ }
1668
+ function installCustomSkillFiles(skill, packageRoot) {
1669
+ const sourcePath = join4(packageRoot, skill.sourcePath);
1670
+ const targetPath = join4(getCustomSkillsDir(), skill.name);
1671
+ if (!existsSync5(sourcePath)) {
1672
+ console.error(`Custom skill source not found: ${sourcePath}`);
1673
+ return false;
1674
+ }
1675
+ rmSync(targetPath, { recursive: true, force: true });
1676
+ copyDirRecursive(sourcePath, targetPath);
1677
+ return true;
1678
+ }
1679
+ function removeObsoleteSkills(removedSkillNames) {
1680
+ const removedSkills = [];
1681
+ for (const skillName of removedSkillNames) {
1682
+ const targetPath = join4(getCustomSkillsDir(), skillName);
1683
+ try {
1684
+ console.log(`Removing obsolete bundled skill: ${skillName}`);
1685
+ rmSync(targetPath, { recursive: true, force: true });
1686
+ removedSkills.push(skillName);
1687
+ } catch (error) {
1688
+ console.warn(`Failed to remove obsolete bundled skill: ${skillName}`);
1689
+ console.warn(error);
1690
+ }
1691
+ }
1692
+ return removedSkills;
1693
+ }
1694
+ function buildManifest(packageRoot, updatedSkills, previousManifest, pluginVersion, sharedHash) {
1695
+ const installedAt = (/* @__PURE__ */ new Date()).toISOString();
1696
+ const updatedSkillNames = new Set(
1697
+ updatedSkills.map(({ skill }) => skill.name)
1698
+ );
1699
+ const skills = Object.fromEntries(
1700
+ CUSTOM_SKILLS.map((skill) => {
1701
+ const previousEntry = previousManifest?.skills[skill.name];
1702
+ const nextInstalledAt = updatedSkillNames.has(skill.name) ? installedAt : previousEntry?.installedAt ?? installedAt;
1703
+ return [
1704
+ skill.name,
1705
+ {
1706
+ hash: computeSkillHash(join4(packageRoot, skill.sourcePath)),
1707
+ installedAt: nextInstalledAt
1708
+ }
1709
+ ];
1710
+ })
1711
+ );
1712
+ return {
1713
+ pluginVersion,
1714
+ sharedHash,
1715
+ skills
1716
+ };
1717
+ }
1718
+ function pruneRemovedSkillsFromManifest(manifest, removedSkills) {
1719
+ const removedSkillNames = new Set(removedSkills);
1720
+ return {
1721
+ ...manifest,
1722
+ skills: Object.fromEntries(
1723
+ Object.entries(manifest.skills).filter(
1724
+ ([skillName]) => !removedSkillNames.has(skillName)
1725
+ )
1726
+ )
1727
+ };
1728
+ }
1729
+ function installCustomSkills(packageRoot = resolvePackageRoot()) {
1730
+ const updateCheck = checkSkillsNeedUpdate(packageRoot);
1731
+ if (!updateCheck.needsUpdate) {
1732
+ return {
1733
+ success: true,
1734
+ updatedSkills: [],
1735
+ skippedSkills: [...CUSTOM_SKILLS],
1736
+ failedSkills: [],
1737
+ removedSkills: []
1738
+ };
1739
+ }
1740
+ const removedSkills = removeObsoleteSkills(updateCheck.removedSkills);
1741
+ if (removedSkills.length > 0 && updateCheck.manifest) {
1742
+ writeManifest(
1743
+ pruneRemovedSkillsFromManifest(updateCheck.manifest, removedSkills)
1744
+ );
1745
+ }
1746
+ if (updateCheck.skillsNeedingUpdate.length === 0) {
1747
+ writeManifest(
1748
+ buildManifest(
1749
+ packageRoot,
1750
+ [],
1751
+ updateCheck.manifest,
1752
+ updateCheck.pluginVersion,
1753
+ updateCheck.sharedHash
1754
+ )
1755
+ );
1756
+ return {
1757
+ success: true,
1758
+ updatedSkills: [],
1759
+ skippedSkills: [...CUSTOM_SKILLS],
1760
+ failedSkills: [],
1761
+ removedSkills
1762
+ };
1763
+ }
1764
+ if (!installSharedSkillAssets(packageRoot)) {
1765
+ return {
1766
+ success: false,
1767
+ updatedSkills: [],
1768
+ skippedSkills: [],
1769
+ failedSkills: updateCheck.skillsNeedingUpdate.map(
1770
+ ({ skill, reasons }) => ({
1771
+ skill,
1772
+ reasons
1773
+ })
1774
+ ),
1775
+ removedSkills
1776
+ };
1777
+ }
1778
+ const updatesBySkillName = new Map(
1779
+ updateCheck.skillsNeedingUpdate.map((entry) => [entry.skill.name, entry])
1780
+ );
1781
+ const updatedSkills = [];
1782
+ const skippedSkills = [];
1783
+ const failedSkills = [];
1784
+ for (const skill of CUSTOM_SKILLS) {
1785
+ const pendingUpdate = updatesBySkillName.get(skill.name);
1786
+ if (!pendingUpdate) {
1787
+ skippedSkills.push(skill);
1788
+ continue;
1789
+ }
1790
+ if (installCustomSkillFiles(skill, packageRoot)) {
1791
+ updatedSkills.push({
1792
+ skill,
1793
+ reasons: pendingUpdate.reasons
1794
+ });
1795
+ continue;
1796
+ }
1797
+ failedSkills.push({
1798
+ skill,
1799
+ reasons: pendingUpdate.reasons
1800
+ });
1801
+ }
1802
+ if (failedSkills.length > 0) {
1803
+ return {
1804
+ success: false,
1805
+ updatedSkills,
1806
+ skippedSkills,
1807
+ failedSkills,
1808
+ removedSkills
1809
+ };
1810
+ }
1811
+ writeManifest(
1812
+ buildManifest(
1813
+ packageRoot,
1814
+ updatedSkills,
1815
+ updateCheck.manifest,
1816
+ updateCheck.pluginVersion,
1817
+ updateCheck.sharedHash
1818
+ )
1819
+ );
1820
+ return {
1821
+ success: true,
1822
+ updatedSkills,
1823
+ skippedSkills,
1824
+ failedSkills,
1825
+ removedSkills
1826
+ };
1827
+ }
1828
+
1829
+ // src/mcp/context7.ts
1830
+ var CONTEXT7_MCP_URL = "https://mcp.context7.com/mcp";
1831
+ var context7 = {
1832
+ type: "remote",
1833
+ url: CONTEXT7_MCP_URL,
1834
+ headers: process.env.CONTEXT7_API_KEY ? { CONTEXT7_API_KEY: process.env.CONTEXT7_API_KEY } : void 0,
1835
+ oauth: false
1836
+ };
1837
+
1838
+ // src/mcp/exa.ts
1839
+ var exa = {
1840
+ type: "local",
1841
+ command: ["npx", "-y", "exa-mcp-server"]
1842
+ };
1843
+
1844
+ // src/mcp/grep-app.ts
1845
+ var GREP_APP_MCP_URL = "https://mcp.grep.app";
1846
+ var grep_app = {
1847
+ type: "remote",
1848
+ url: GREP_APP_MCP_URL,
1849
+ oauth: false
1850
+ };
1851
+
1852
+ export {
1853
+ SUBAGENT_NAMES,
1854
+ ALL_AGENT_NAMES,
1855
+ DEFAULT_MODELS,
1856
+ POLL_INTERVAL_BACKGROUND_MS,
1857
+ DEFAULT_THOTH_COMMAND,
1858
+ getOpenCodeConfigPaths,
1859
+ getExistingLiteConfigPath,
1860
+ getExistingConfigPath,
1861
+ ensureConfigDir,
1862
+ ensureOpenCodeConfigDir,
1863
+ generateLiteConfig,
1864
+ stripJsonComments,
1865
+ parseConfig,
1866
+ writeConfig,
1867
+ addPluginToOpenCodeConfig,
1868
+ writeLiteConfig,
1869
+ disableDefaultAgents,
1870
+ detectCurrentConfig,
1871
+ loadPluginConfig,
1872
+ loadAgentPrompt,
1873
+ getAgentOverride,
1874
+ OPENCODE_PROMPT_DIALECT,
1875
+ CODEX_PROMPT_DIALECT,
1876
+ createStepBudgetSection,
1877
+ createModelFamilySection,
1878
+ createOrchestratorPromptSections,
1879
+ createReadOnlySpecialistPromptSections,
1880
+ createWriteCapableSpecialistPromptSections,
1881
+ renderPromptSection,
1882
+ renderRolePrompt,
1883
+ appendPromptSections,
1884
+ detectModelFamily,
1885
+ getStepBudgetPromptSection,
1886
+ getModelFamilyPromptSection,
1887
+ composeAgentPrompt,
1888
+ getSkillRegistry,
1889
+ CUSTOM_SKILLS,
1890
+ getCustomSkillsDir,
1891
+ findPackageRoot,
1892
+ installCustomSkills,
1893
+ CONTEXT7_MCP_URL,
1894
+ context7,
1895
+ exa,
1896
+ GREP_APP_MCP_URL,
1897
+ grep_app
1898
+ };