@cleocode/adapters 2026.3.72 → 2026.3.73

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.
Files changed (96) hide show
  1. package/dist/index.d.ts +3 -0
  2. package/dist/index.d.ts.map +1 -1
  3. package/dist/index.js +1227 -101
  4. package/dist/index.js.map +4 -4
  5. package/dist/providers/claude-code/adapter.d.ts.map +1 -1
  6. package/dist/providers/claude-code/adapter.js +16 -5
  7. package/dist/providers/claude-code/adapter.js.map +1 -1
  8. package/dist/providers/claude-code/hooks.d.ts +89 -25
  9. package/dist/providers/claude-code/hooks.d.ts.map +1 -1
  10. package/dist/providers/claude-code/hooks.js +230 -28
  11. package/dist/providers/claude-code/hooks.js.map +1 -1
  12. package/dist/providers/codex/adapter.d.ts +70 -0
  13. package/dist/providers/codex/adapter.d.ts.map +1 -0
  14. package/dist/providers/codex/adapter.js +134 -0
  15. package/dist/providers/codex/adapter.js.map +1 -0
  16. package/dist/providers/codex/hooks.d.ts +85 -0
  17. package/dist/providers/codex/hooks.d.ts.map +1 -0
  18. package/dist/providers/codex/hooks.js +155 -0
  19. package/dist/providers/codex/hooks.js.map +1 -0
  20. package/dist/providers/codex/index.d.ts +22 -0
  21. package/dist/providers/codex/index.d.ts.map +1 -0
  22. package/dist/providers/codex/index.js +24 -0
  23. package/dist/providers/codex/index.js.map +1 -0
  24. package/dist/providers/codex/install.d.ts +74 -0
  25. package/dist/providers/codex/install.d.ts.map +1 -0
  26. package/dist/providers/codex/install.js +183 -0
  27. package/dist/providers/codex/install.js.map +1 -0
  28. package/dist/providers/cursor/adapter.d.ts.map +1 -1
  29. package/dist/providers/cursor/adapter.js +16 -2
  30. package/dist/providers/cursor/adapter.js.map +1 -1
  31. package/dist/providers/cursor/hooks.d.ts +102 -17
  32. package/dist/providers/cursor/hooks.d.ts.map +1 -1
  33. package/dist/providers/cursor/hooks.js +164 -18
  34. package/dist/providers/cursor/hooks.js.map +1 -1
  35. package/dist/providers/gemini-cli/adapter.d.ts +70 -0
  36. package/dist/providers/gemini-cli/adapter.d.ts.map +1 -0
  37. package/dist/providers/gemini-cli/adapter.js +145 -0
  38. package/dist/providers/gemini-cli/adapter.js.map +1 -0
  39. package/dist/providers/gemini-cli/hooks.d.ts +92 -0
  40. package/dist/providers/gemini-cli/hooks.d.ts.map +1 -0
  41. package/dist/providers/gemini-cli/hooks.js +169 -0
  42. package/dist/providers/gemini-cli/hooks.js.map +1 -0
  43. package/dist/providers/gemini-cli/index.d.ts +22 -0
  44. package/dist/providers/gemini-cli/index.d.ts.map +1 -0
  45. package/dist/providers/gemini-cli/index.js +24 -0
  46. package/dist/providers/gemini-cli/index.js.map +1 -0
  47. package/dist/providers/gemini-cli/install.d.ts +74 -0
  48. package/dist/providers/gemini-cli/install.d.ts.map +1 -0
  49. package/dist/providers/gemini-cli/install.js +183 -0
  50. package/dist/providers/gemini-cli/install.js.map +1 -0
  51. package/dist/providers/kimi/adapter.d.ts +72 -0
  52. package/dist/providers/kimi/adapter.d.ts.map +1 -0
  53. package/dist/providers/kimi/adapter.js +133 -0
  54. package/dist/providers/kimi/adapter.js.map +1 -0
  55. package/dist/providers/kimi/hooks.d.ts +64 -0
  56. package/dist/providers/kimi/hooks.d.ts.map +1 -0
  57. package/dist/providers/kimi/hooks.js +73 -0
  58. package/dist/providers/kimi/hooks.js.map +1 -0
  59. package/dist/providers/kimi/index.d.ts +22 -0
  60. package/dist/providers/kimi/index.d.ts.map +1 -0
  61. package/dist/providers/kimi/index.js +24 -0
  62. package/dist/providers/kimi/index.js.map +1 -0
  63. package/dist/providers/kimi/install.d.ts +80 -0
  64. package/dist/providers/kimi/install.d.ts.map +1 -0
  65. package/dist/providers/kimi/install.js +189 -0
  66. package/dist/providers/kimi/install.js.map +1 -0
  67. package/dist/providers/opencode/adapter.d.ts.map +1 -1
  68. package/dist/providers/opencode/adapter.js +13 -6
  69. package/dist/providers/opencode/adapter.js.map +1 -1
  70. package/dist/providers/opencode/hooks.d.ts +89 -28
  71. package/dist/providers/opencode/hooks.d.ts.map +1 -1
  72. package/dist/providers/opencode/hooks.js +145 -37
  73. package/dist/providers/opencode/hooks.js.map +1 -1
  74. package/package.json +3 -2
  75. package/src/index.ts +18 -0
  76. package/src/providers/claude-code/adapter.ts +16 -5
  77. package/src/providers/claude-code/hooks.ts +154 -30
  78. package/src/providers/codex/adapter.ts +154 -0
  79. package/src/providers/codex/hooks.ts +163 -0
  80. package/src/providers/codex/index.ts +27 -0
  81. package/src/providers/codex/install.ts +203 -0
  82. package/src/providers/codex/manifest.json +28 -0
  83. package/src/providers/cursor/adapter.ts +16 -2
  84. package/src/providers/cursor/hooks.ts +167 -18
  85. package/src/providers/gemini-cli/adapter.ts +165 -0
  86. package/src/providers/gemini-cli/hooks.ts +177 -0
  87. package/src/providers/gemini-cli/index.ts +27 -0
  88. package/src/providers/gemini-cli/install.ts +203 -0
  89. package/src/providers/gemini-cli/manifest.json +35 -0
  90. package/src/providers/kimi/adapter.ts +153 -0
  91. package/src/providers/kimi/hooks.ts +80 -0
  92. package/src/providers/kimi/index.ts +27 -0
  93. package/src/providers/kimi/install.ts +209 -0
  94. package/src/providers/kimi/manifest.json +24 -0
  95. package/src/providers/opencode/adapter.ts +13 -6
  96. package/src/providers/opencode/hooks.ts +146 -37
@@ -0,0 +1,203 @@
1
+ /**
2
+ * Gemini CLI Install Provider
3
+ *
4
+ * Handles CLEO installation into Gemini CLI environments:
5
+ * - Registers CLEO MCP server in ~/.gemini/settings.json
6
+ * - Ensures AGENTS.md has CLEO @-references
7
+ *
8
+ * @task T161
9
+ * @epic T134
10
+ */
11
+
12
+ import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs';
13
+ import { homedir } from 'node:os';
14
+ import { join } from 'node:path';
15
+ import type { AdapterInstallProvider, InstallOptions, InstallResult } from '@cleocode/contracts';
16
+
17
+ /** Lines that should appear in AGENTS.md to reference CLEO. */
18
+ const INSTRUCTION_REFERENCES = ['@~/.cleo/templates/CLEO-INJECTION.md', '@.cleo/memory-bridge.md'];
19
+
20
+ /** MCP server registration key used in Gemini CLI settings. */
21
+ const MCP_SERVER_KEY = 'cleo';
22
+
23
+ /**
24
+ * Install provider for Gemini CLI.
25
+ *
26
+ * Manages CLEO's integration with Gemini CLI by:
27
+ * 1. Registering the CLEO MCP server in ~/.gemini/settings.json
28
+ * 2. Ensuring AGENTS.md contains @-references to CLEO instruction files
29
+ *
30
+ * @task T161
31
+ * @epic T134
32
+ */
33
+ export class GeminiCliInstallProvider implements AdapterInstallProvider {
34
+ /**
35
+ * Install CLEO into a Gemini CLI environment.
36
+ *
37
+ * @param options - Installation options including project directory and MCP server path
38
+ * @returns Result describing what was installed
39
+ * @task T161
40
+ */
41
+ async install(options: InstallOptions): Promise<InstallResult> {
42
+ const { projectDir, mcpServerPath } = options;
43
+ const installedAt = new Date().toISOString();
44
+ let instructionFileUpdated = false;
45
+ let mcpRegistered = false;
46
+ const details: Record<string, unknown> = {};
47
+
48
+ // Step 1: Register MCP server in ~/.gemini/settings.json
49
+ if (mcpServerPath) {
50
+ mcpRegistered = this.registerMcpServer(mcpServerPath);
51
+ if (mcpRegistered) {
52
+ details.mcpConfigPath = join(homedir(), '.gemini', 'settings.json');
53
+ }
54
+ }
55
+
56
+ // Step 2: Ensure AGENTS.md has @-references
57
+ instructionFileUpdated = this.updateInstructionFile(projectDir);
58
+ if (instructionFileUpdated) {
59
+ details.instructionFile = join(projectDir, 'AGENTS.md');
60
+ }
61
+
62
+ return {
63
+ success: true,
64
+ installedAt,
65
+ instructionFileUpdated,
66
+ mcpRegistered,
67
+ details,
68
+ };
69
+ }
70
+
71
+ /**
72
+ * Uninstall CLEO from the Gemini CLI environment.
73
+ *
74
+ * Removes the MCP server registration from ~/.gemini/settings.json.
75
+ * Does not remove AGENTS.md references (they are harmless if CLEO is not present).
76
+ * @task T161
77
+ */
78
+ async uninstall(): Promise<void> {
79
+ const settingsPath = join(homedir(), '.gemini', 'settings.json');
80
+ if (existsSync(settingsPath)) {
81
+ try {
82
+ const raw = readFileSync(settingsPath, 'utf-8');
83
+ const config = JSON.parse(raw) as Record<string, unknown>;
84
+ const mcpServers = config.mcpServers as Record<string, unknown> | undefined;
85
+ if (mcpServers && MCP_SERVER_KEY in mcpServers) {
86
+ delete mcpServers[MCP_SERVER_KEY];
87
+ writeFileSync(settingsPath, JSON.stringify(config, null, 2) + '\n', 'utf-8');
88
+ }
89
+ } catch {
90
+ // Ignore errors during uninstall
91
+ }
92
+ }
93
+ }
94
+
95
+ /**
96
+ * Check whether CLEO is installed in the Gemini CLI environment.
97
+ *
98
+ * Checks for MCP server registered in ~/.gemini/settings.json.
99
+ * Returns true if the CLEO MCP server entry is found.
100
+ * @task T161
101
+ */
102
+ async isInstalled(): Promise<boolean> {
103
+ const settingsPath = join(homedir(), '.gemini', 'settings.json');
104
+ if (existsSync(settingsPath)) {
105
+ try {
106
+ const config = JSON.parse(readFileSync(settingsPath, 'utf-8'));
107
+ const mcpServers = config.mcpServers as Record<string, unknown> | undefined;
108
+ if (mcpServers && MCP_SERVER_KEY in mcpServers) {
109
+ return true;
110
+ }
111
+ } catch {
112
+ // Fall through
113
+ }
114
+ }
115
+
116
+ return false;
117
+ }
118
+
119
+ /**
120
+ * Ensure AGENTS.md contains @-references to CLEO instruction files.
121
+ *
122
+ * Creates AGENTS.md if it does not exist. Appends any missing references.
123
+ *
124
+ * @param projectDir - Project root directory
125
+ * @task T161
126
+ */
127
+ async ensureInstructionReferences(projectDir: string): Promise<void> {
128
+ this.updateInstructionFile(projectDir);
129
+ }
130
+
131
+ /**
132
+ * Register the CLEO MCP server in ~/.gemini/settings.json.
133
+ *
134
+ * Gemini CLI stores its MCP server configuration in ~/.gemini/settings.json
135
+ * under the mcpServers key.
136
+ *
137
+ * @param mcpServerPath - Absolute path to the MCP server entry point
138
+ * @returns true if registration was performed or updated
139
+ */
140
+ private registerMcpServer(mcpServerPath: string): boolean {
141
+ const geminiDir = join(homedir(), '.gemini');
142
+ const settingsPath = join(geminiDir, 'settings.json');
143
+ let config: Record<string, unknown> = {};
144
+
145
+ mkdirSync(geminiDir, { recursive: true });
146
+
147
+ if (existsSync(settingsPath)) {
148
+ try {
149
+ config = JSON.parse(readFileSync(settingsPath, 'utf-8'));
150
+ } catch {
151
+ // Start fresh on parse error
152
+ }
153
+ }
154
+
155
+ if (!config.mcpServers || typeof config.mcpServers !== 'object') {
156
+ config.mcpServers = {};
157
+ }
158
+
159
+ const mcpServers = config.mcpServers as Record<string, unknown>;
160
+ mcpServers[MCP_SERVER_KEY] = {
161
+ command: 'node',
162
+ args: [mcpServerPath],
163
+ };
164
+
165
+ writeFileSync(settingsPath, JSON.stringify(config, null, 2) + '\n', 'utf-8');
166
+ return true;
167
+ }
168
+
169
+ /**
170
+ * Update AGENTS.md with CLEO @-references.
171
+ *
172
+ * @param projectDir - Project root directory
173
+ * @returns true if the file was created or modified
174
+ */
175
+ private updateInstructionFile(projectDir: string): boolean {
176
+ const agentsMdPath = join(projectDir, 'AGENTS.md');
177
+ let content = '';
178
+ let existed = false;
179
+
180
+ if (existsSync(agentsMdPath)) {
181
+ content = readFileSync(agentsMdPath, 'utf-8');
182
+ existed = true;
183
+ }
184
+
185
+ const missingRefs = INSTRUCTION_REFERENCES.filter((ref) => !content.includes(ref));
186
+
187
+ if (missingRefs.length === 0) {
188
+ return false;
189
+ }
190
+
191
+ const refsBlock = missingRefs.join('\n');
192
+
193
+ if (existed) {
194
+ const separator = content.endsWith('\n') ? '' : '\n';
195
+ content = content + separator + refsBlock + '\n';
196
+ } else {
197
+ content = refsBlock + '\n';
198
+ }
199
+
200
+ writeFileSync(agentsMdPath, content, 'utf-8');
201
+ return true;
202
+ }
203
+ }
@@ -0,0 +1,35 @@
1
+ {
2
+ "id": "gemini-cli",
3
+ "name": "Gemini CLI Adapter",
4
+ "version": "1.0.0",
5
+ "description": "CLEO adapter for Google Gemini CLI",
6
+ "provider": "gemini-cli",
7
+ "entryPoint": "src/index.ts",
8
+ "capabilities": {
9
+ "supportsHooks": true,
10
+ "supportedHookEvents": [
11
+ "SessionStart",
12
+ "SessionEnd",
13
+ "BeforeAgent",
14
+ "AfterAgent",
15
+ "BeforeTool",
16
+ "AfterTool",
17
+ "BeforeModel",
18
+ "AfterModel",
19
+ "PreCompress",
20
+ "Notification"
21
+ ],
22
+ "supportsSpawn": false,
23
+ "supportsInstall": true,
24
+ "supportsMcp": true,
25
+ "supportsInstructionFiles": false,
26
+ "supportsContextMonitor": false,
27
+ "supportsStatusline": false,
28
+ "supportsProviderPaths": false,
29
+ "supportsTransport": false
30
+ },
31
+ "detectionPatterns": [
32
+ { "type": "cli", "pattern": "gemini", "description": "Gemini CLI binary available in PATH" },
33
+ { "type": "directory", "pattern": "~/.gemini", "description": "Gemini CLI config directory" }
34
+ ]
35
+ }
@@ -0,0 +1,153 @@
1
+ /**
2
+ * Kimi Adapter
3
+ *
4
+ * Main CLEOProviderAdapter implementation for Moonshot AI Kimi.
5
+ * Provides install-only capabilities for CLEO integration.
6
+ * Kimi has no native hook system, so integration is via MCP only.
7
+ *
8
+ * @task T163
9
+ * @epic T134
10
+ */
11
+
12
+ import { exec } from 'node:child_process';
13
+ import { existsSync } from 'node:fs';
14
+ import { homedir } from 'node:os';
15
+ import { join } from 'node:path';
16
+ import { promisify } from 'node:util';
17
+ import type {
18
+ AdapterCapabilities,
19
+ AdapterHealthStatus,
20
+ CLEOProviderAdapter,
21
+ } from '@cleocode/contracts';
22
+ import { KimiHookProvider } from './hooks.js';
23
+ import { KimiInstallProvider } from './install.js';
24
+
25
+ const execAsync = promisify(exec);
26
+
27
+ /**
28
+ * CLEO provider adapter for Moonshot AI Kimi.
29
+ *
30
+ * Bridges CLEO's adapter system with Kimi's integration surface:
31
+ * - Hooks: No-op (Kimi has no native hook system)
32
+ * - Install: Registers MCP server in ~/.kimi/mcp.json and ensures AGENTS.md references
33
+ *
34
+ * @task T163
35
+ * @epic T134
36
+ */
37
+ export class KimiAdapter implements CLEOProviderAdapter {
38
+ readonly id = 'kimi';
39
+ readonly name = 'Kimi';
40
+ readonly version = '1.0.0';
41
+
42
+ capabilities: AdapterCapabilities = {
43
+ supportsHooks: false,
44
+ supportedHookEvents: [],
45
+ supportsSpawn: false,
46
+ supportsInstall: true,
47
+ supportsMcp: true,
48
+ supportsInstructionFiles: false,
49
+ supportsContextMonitor: false,
50
+ supportsStatusline: false,
51
+ supportsProviderPaths: false,
52
+ supportsTransport: false,
53
+ supportsTaskSync: false,
54
+ };
55
+
56
+ hooks: KimiHookProvider;
57
+ install: KimiInstallProvider;
58
+
59
+ private projectDir: string | null = null;
60
+ private initialized = false;
61
+
62
+ constructor() {
63
+ this.hooks = new KimiHookProvider();
64
+ this.install = new KimiInstallProvider();
65
+ }
66
+
67
+ /**
68
+ * Initialize the adapter for a given project directory.
69
+ *
70
+ * @param projectDir - Root directory of the project
71
+ * @task T163
72
+ */
73
+ async initialize(projectDir: string): Promise<void> {
74
+ this.projectDir = projectDir;
75
+ this.initialized = true;
76
+ }
77
+
78
+ /**
79
+ * Dispose the adapter and clean up resources.
80
+ *
81
+ * Releases tracked state. No hooks to unregister since Kimi
82
+ * has no native hook system.
83
+ * @task T163
84
+ */
85
+ async dispose(): Promise<void> {
86
+ this.initialized = false;
87
+ this.projectDir = null;
88
+ }
89
+
90
+ /**
91
+ * Run a health check to verify Kimi is accessible.
92
+ *
93
+ * Checks:
94
+ * 1. Adapter has been initialized
95
+ * 2. Kimi CLI binary is available in PATH
96
+ * 3. ~/.kimi/ configuration directory exists
97
+ *
98
+ * @returns Health status with details about each check
99
+ * @task T163
100
+ */
101
+ async healthCheck(): Promise<AdapterHealthStatus> {
102
+ const details: Record<string, unknown> = {};
103
+
104
+ if (!this.initialized) {
105
+ return {
106
+ healthy: false,
107
+ provider: this.id,
108
+ details: { error: 'Adapter not initialized' },
109
+ };
110
+ }
111
+
112
+ // Check Kimi CLI availability
113
+ let cliAvailable = false;
114
+ try {
115
+ const { stdout } = await execAsync('which kimi');
116
+ cliAvailable = stdout.trim().length > 0;
117
+ details.cliPath = stdout.trim();
118
+ } catch {
119
+ details.cliAvailable = false;
120
+ }
121
+
122
+ // Check for Kimi config directory
123
+ const kimiConfigDir = join(homedir(), '.kimi');
124
+ const configExists = existsSync(kimiConfigDir);
125
+ details.configDirExists = configExists;
126
+
127
+ // Healthy if CLI is available (primary requirement)
128
+ const healthy = cliAvailable;
129
+ details.cliAvailable = cliAvailable;
130
+
131
+ return {
132
+ healthy,
133
+ provider: this.id,
134
+ details,
135
+ };
136
+ }
137
+
138
+ /**
139
+ * Check whether the adapter has been initialized.
140
+ * @task T163
141
+ */
142
+ isInitialized(): boolean {
143
+ return this.initialized;
144
+ }
145
+
146
+ /**
147
+ * Get the project directory this adapter was initialized with.
148
+ * @task T163
149
+ */
150
+ getProjectDir(): string | null {
151
+ return this.projectDir;
152
+ }
153
+ }
@@ -0,0 +1,80 @@
1
+ /**
2
+ * Kimi Hook Provider
3
+ *
4
+ * Kimi has no native hook system (hookSystem is "none").
5
+ * This provider implements the minimal AdapterHookProvider interface
6
+ * with a no-op mapProviderEvent that always returns null.
7
+ *
8
+ * @task T163
9
+ * @epic T134
10
+ */
11
+
12
+ import type { AdapterHookProvider } from '@cleocode/contracts';
13
+
14
+ /**
15
+ * Hook provider for Kimi.
16
+ *
17
+ * Kimi does not expose a native hook or event system.
18
+ * All hook-related methods are no-ops; mapProviderEvent always
19
+ * returns null since there are no events to map.
20
+ *
21
+ * @task T163
22
+ * @epic T134
23
+ */
24
+ export class KimiHookProvider implements AdapterHookProvider {
25
+ private registered = false;
26
+
27
+ /**
28
+ * Map a Kimi native event name to a CAAMP hook event name.
29
+ *
30
+ * Kimi has no hook system, so this always returns null.
31
+ *
32
+ * @param _providerEvent - Unused; Kimi emits no hookable events
33
+ * @returns Always null
34
+ * @task T163
35
+ */
36
+ mapProviderEvent(_providerEvent: string): string | null {
37
+ return null;
38
+ }
39
+
40
+ /**
41
+ * Register native hooks for a project.
42
+ *
43
+ * Kimi has no hook system. This method is a no-op and only
44
+ * tracks registration state for interface compliance.
45
+ *
46
+ * @param _projectDir - Project directory (unused)
47
+ * @task T163
48
+ */
49
+ async registerNativeHooks(_projectDir: string): Promise<void> {
50
+ this.registered = true;
51
+ }
52
+
53
+ /**
54
+ * Unregister native hooks.
55
+ *
56
+ * Kimi has no hook system. This method is a no-op.
57
+ * @task T163
58
+ */
59
+ async unregisterNativeHooks(): Promise<void> {
60
+ this.registered = false;
61
+ }
62
+
63
+ /**
64
+ * Check whether hooks have been registered via registerNativeHooks.
65
+ * @task T163
66
+ */
67
+ isRegistered(): boolean {
68
+ return this.registered;
69
+ }
70
+
71
+ /**
72
+ * Get the full event mapping for introspection/debugging.
73
+ *
74
+ * Returns an empty map since Kimi has no hookable events.
75
+ * @task T163
76
+ */
77
+ getEventMap(): Readonly<Record<string, string>> {
78
+ return {};
79
+ }
80
+ }
@@ -0,0 +1,27 @@
1
+ /**
2
+ * Kimi provider adapter.
3
+ *
4
+ * CLEO provider adapter for Moonshot AI Kimi.
5
+ * Default export is the adapter class for dynamic loading by AdapterManager.
6
+ *
7
+ * @task T163
8
+ * @epic T134
9
+ */
10
+
11
+ import { KimiAdapter } from './adapter.js';
12
+
13
+ export { KimiAdapter } from './adapter.js';
14
+ export { KimiHookProvider } from './hooks.js';
15
+ export { KimiInstallProvider } from './install.js';
16
+
17
+ export default KimiAdapter;
18
+
19
+ /**
20
+ * Factory function for creating adapter instances.
21
+ * Used by AdapterManager's dynamic import fallback.
22
+ *
23
+ * @task T163
24
+ */
25
+ export function createAdapter(): KimiAdapter {
26
+ return new KimiAdapter();
27
+ }
@@ -0,0 +1,209 @@
1
+ /**
2
+ * Kimi Install Provider
3
+ *
4
+ * Handles CLEO installation into Kimi environments:
5
+ * - Registers CLEO MCP server in ~/.kimi/mcp.json
6
+ * - Ensures AGENTS.md has CLEO @-references
7
+ *
8
+ * Kimi has no native hook system, so this is the primary integration
9
+ * mechanism: registering the MCP server so CLEO can be accessed as a tool.
10
+ *
11
+ * @task T163
12
+ * @epic T134
13
+ */
14
+
15
+ import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs';
16
+ import { homedir } from 'node:os';
17
+ import { join } from 'node:path';
18
+ import type { AdapterInstallProvider, InstallOptions, InstallResult } from '@cleocode/contracts';
19
+
20
+ /** Lines that should appear in AGENTS.md to reference CLEO. */
21
+ const INSTRUCTION_REFERENCES = ['@~/.cleo/templates/CLEO-INJECTION.md', '@.cleo/memory-bridge.md'];
22
+
23
+ /** MCP server registration key used in Kimi MCP config. */
24
+ const MCP_SERVER_KEY = 'cleo';
25
+
26
+ /**
27
+ * Install provider for Kimi.
28
+ *
29
+ * Manages CLEO's integration with Kimi by:
30
+ * 1. Registering the CLEO MCP server in ~/.kimi/mcp.json
31
+ * 2. Ensuring AGENTS.md contains @-references to CLEO instruction files
32
+ *
33
+ * Since Kimi has no native hook system, MCP registration is the only
34
+ * mechanism for surfacing CLEO capabilities inside a Kimi session.
35
+ *
36
+ * @task T163
37
+ * @epic T134
38
+ */
39
+ export class KimiInstallProvider implements AdapterInstallProvider {
40
+ /**
41
+ * Install CLEO into a Kimi environment.
42
+ *
43
+ * @param options - Installation options including project directory and MCP server path
44
+ * @returns Result describing what was installed
45
+ * @task T163
46
+ */
47
+ async install(options: InstallOptions): Promise<InstallResult> {
48
+ const { projectDir, mcpServerPath } = options;
49
+ const installedAt = new Date().toISOString();
50
+ let instructionFileUpdated = false;
51
+ let mcpRegistered = false;
52
+ const details: Record<string, unknown> = {};
53
+
54
+ // Step 1: Register MCP server in ~/.kimi/mcp.json
55
+ if (mcpServerPath) {
56
+ mcpRegistered = this.registerMcpServer(mcpServerPath);
57
+ if (mcpRegistered) {
58
+ details.mcpConfigPath = join(homedir(), '.kimi', 'mcp.json');
59
+ }
60
+ }
61
+
62
+ // Step 2: Ensure AGENTS.md has @-references
63
+ instructionFileUpdated = this.updateInstructionFile(projectDir);
64
+ if (instructionFileUpdated) {
65
+ details.instructionFile = join(projectDir, 'AGENTS.md');
66
+ }
67
+
68
+ return {
69
+ success: true,
70
+ installedAt,
71
+ instructionFileUpdated,
72
+ mcpRegistered,
73
+ details,
74
+ };
75
+ }
76
+
77
+ /**
78
+ * Uninstall CLEO from the Kimi environment.
79
+ *
80
+ * Removes the MCP server registration from ~/.kimi/mcp.json.
81
+ * Does not remove AGENTS.md references (they are harmless if CLEO is not present).
82
+ * @task T163
83
+ */
84
+ async uninstall(): Promise<void> {
85
+ const mcpPath = join(homedir(), '.kimi', 'mcp.json');
86
+ if (existsSync(mcpPath)) {
87
+ try {
88
+ const raw = readFileSync(mcpPath, 'utf-8');
89
+ const config = JSON.parse(raw) as Record<string, unknown>;
90
+ const mcpServers = config.mcpServers as Record<string, unknown> | undefined;
91
+ if (mcpServers && MCP_SERVER_KEY in mcpServers) {
92
+ delete mcpServers[MCP_SERVER_KEY];
93
+ writeFileSync(mcpPath, JSON.stringify(config, null, 2) + '\n', 'utf-8');
94
+ }
95
+ } catch {
96
+ // Ignore errors during uninstall
97
+ }
98
+ }
99
+ }
100
+
101
+ /**
102
+ * Check whether CLEO is installed in the Kimi environment.
103
+ *
104
+ * Checks for MCP server registered in ~/.kimi/mcp.json.
105
+ * Returns true if the CLEO MCP server entry is found.
106
+ * @task T163
107
+ */
108
+ async isInstalled(): Promise<boolean> {
109
+ const mcpPath = join(homedir(), '.kimi', 'mcp.json');
110
+ if (existsSync(mcpPath)) {
111
+ try {
112
+ const config = JSON.parse(readFileSync(mcpPath, 'utf-8'));
113
+ const mcpServers = config.mcpServers as Record<string, unknown> | undefined;
114
+ if (mcpServers && MCP_SERVER_KEY in mcpServers) {
115
+ return true;
116
+ }
117
+ } catch {
118
+ // Fall through
119
+ }
120
+ }
121
+
122
+ return false;
123
+ }
124
+
125
+ /**
126
+ * Ensure AGENTS.md contains @-references to CLEO instruction files.
127
+ *
128
+ * Creates AGENTS.md if it does not exist. Appends any missing references.
129
+ *
130
+ * @param projectDir - Project root directory
131
+ * @task T163
132
+ */
133
+ async ensureInstructionReferences(projectDir: string): Promise<void> {
134
+ this.updateInstructionFile(projectDir);
135
+ }
136
+
137
+ /**
138
+ * Register the CLEO MCP server in ~/.kimi/mcp.json.
139
+ *
140
+ * Kimi stores its MCP server configuration in ~/.kimi/mcp.json
141
+ * under the mcpServers key.
142
+ *
143
+ * @param mcpServerPath - Absolute path to the MCP server entry point
144
+ * @returns true if registration was performed or updated
145
+ */
146
+ private registerMcpServer(mcpServerPath: string): boolean {
147
+ const kimiDir = join(homedir(), '.kimi');
148
+ const mcpPath = join(kimiDir, 'mcp.json');
149
+ let config: Record<string, unknown> = {};
150
+
151
+ mkdirSync(kimiDir, { recursive: true });
152
+
153
+ if (existsSync(mcpPath)) {
154
+ try {
155
+ config = JSON.parse(readFileSync(mcpPath, 'utf-8'));
156
+ } catch {
157
+ // Start fresh on parse error
158
+ }
159
+ }
160
+
161
+ if (!config.mcpServers || typeof config.mcpServers !== 'object') {
162
+ config.mcpServers = {};
163
+ }
164
+
165
+ const mcpServers = config.mcpServers as Record<string, unknown>;
166
+ mcpServers[MCP_SERVER_KEY] = {
167
+ command: 'node',
168
+ args: [mcpServerPath],
169
+ };
170
+
171
+ writeFileSync(mcpPath, JSON.stringify(config, null, 2) + '\n', 'utf-8');
172
+ return true;
173
+ }
174
+
175
+ /**
176
+ * Update AGENTS.md with CLEO @-references.
177
+ *
178
+ * @param projectDir - Project root directory
179
+ * @returns true if the file was created or modified
180
+ */
181
+ private updateInstructionFile(projectDir: string): boolean {
182
+ const agentsMdPath = join(projectDir, 'AGENTS.md');
183
+ let content = '';
184
+ let existed = false;
185
+
186
+ if (existsSync(agentsMdPath)) {
187
+ content = readFileSync(agentsMdPath, 'utf-8');
188
+ existed = true;
189
+ }
190
+
191
+ const missingRefs = INSTRUCTION_REFERENCES.filter((ref) => !content.includes(ref));
192
+
193
+ if (missingRefs.length === 0) {
194
+ return false;
195
+ }
196
+
197
+ const refsBlock = missingRefs.join('\n');
198
+
199
+ if (existed) {
200
+ const separator = content.endsWith('\n') ? '' : '\n';
201
+ content = content + separator + refsBlock + '\n';
202
+ } else {
203
+ content = refsBlock + '\n';
204
+ }
205
+
206
+ writeFileSync(agentsMdPath, content, 'utf-8');
207
+ return true;
208
+ }
209
+ }