@drsamonline/cli 0.0.0-stage → 0.12.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/tools.js ADDED
@@ -0,0 +1,235 @@
1
+ /**
2
+ * Coding-tool detection and one-click configuration.
3
+ *
4
+ * Where a tool has a config file we can merge into safely, we write it. Where it
5
+ * stores settings inside an app (Cursor, Cline, Roo) we do not pretend otherwise:
6
+ * we print the two fields to paste, because silently editing an app's internal
7
+ * state is how you break someone's editor.
8
+ */
9
+
10
+ import { existsSync, mkdirSync, readdirSync, readFileSync, writeFileSync } from 'node:fs';
11
+ import { homedir } from 'node:os';
12
+ import { join } from 'node:path';
13
+
14
+ /**
15
+ * @typedef {{
16
+ * id: string, name: string, detected: boolean, evidence: string|null,
17
+ * setup: 'file'|'paste', configPath: string|null,
18
+ * fields?: string[], note?: string
19
+ * }} Tool
20
+ */
21
+
22
+ /** @param {{home?: string, env?: NodeJS.ProcessEnv}} [opts] @returns {Tool[]} */
23
+ export function detectTools(opts = {}) {
24
+ const home = opts.home ?? homedir();
25
+ const env = opts.env ?? process.env;
26
+ const has = (p) => existsSync(p);
27
+ const firstExisting = (...paths) => paths.find(has) ?? null;
28
+
29
+ /** @type {Tool[]} */
30
+ const tools = [];
31
+
32
+ // Continue (VS Code / JetBrains)
33
+ const continuePath = firstExisting(join(home, '.continue', 'config.json'), join(home, '.continue', 'config.yaml'));
34
+ tools.push({
35
+ id: 'continue',
36
+ name: 'Continue',
37
+ detected: Boolean(continuePath),
38
+ evidence: continuePath,
39
+ setup: continuePath?.endsWith('.json') ? 'file' : 'paste',
40
+ configPath: continuePath,
41
+ fields: ['models[].provider = openai', 'models[].apiBase = <endpoint>', 'models[].model = <combo>'],
42
+ });
43
+
44
+ // Aider
45
+ const aiderBin = which('aider', env) ?? firstExisting(join(home, '.aider.conf.yml'), join(home, '.aider.conf.yaml'));
46
+ tools.push({
47
+ id: 'aider',
48
+ name: 'Aider',
49
+ detected: Boolean(aiderBin),
50
+ evidence: aiderBin,
51
+ setup: 'file',
52
+ configPath: join(home, '.aider.conf.yml'),
53
+ fields: ['openai-api-base', 'openai-api-key', 'model'],
54
+ });
55
+
56
+ // Zed
57
+ const zedPath = firstExisting(join(home, '.config', 'zed', 'settings.json'), join(home, '.zed', 'settings.json'));
58
+ tools.push({
59
+ id: 'zed',
60
+ name: 'Zed',
61
+ detected: Boolean(zedPath) || has(join(home, '.local', 'share', 'zed')),
62
+ evidence: zedPath,
63
+ setup: zedPath ? 'file' : 'paste',
64
+ configPath: zedPath,
65
+ fields: ['language_models.openai.api_url', 'language_models.openai.available_models'],
66
+ });
67
+
68
+ // Cursor
69
+ const cursorPath = firstExisting(join(home, '.cursor'), '/Applications/Cursor.app', join(home, '.config', 'Cursor'));
70
+ tools.push({
71
+ id: 'cursor',
72
+ name: 'Cursor',
73
+ detected: Boolean(cursorPath),
74
+ evidence: cursorPath,
75
+ setup: 'paste',
76
+ configPath: null,
77
+ fields: ['Settings → Models → OpenAI API Key', 'Settings → Models → Override OpenAI Base URL'],
78
+ note: 'Cursor keeps this in its own database, so it has to be pasted in the UI.',
79
+ });
80
+
81
+ // Cline / Roo Code (VS Code extensions)
82
+ const extRoots = [
83
+ join(home, '.vscode', 'extensions'),
84
+ join(home, '.vscode-server', 'extensions'),
85
+ join(home, '.cursor', 'extensions'),
86
+ ];
87
+ const extHit = extRoots.find((dir) => {
88
+ if (!has(dir)) return false;
89
+ try {
90
+ return readDirSafe(dir).some((n) => /claude-dev|cline|roo-cline|roocode/i.test(n));
91
+ } catch {
92
+ return false;
93
+ }
94
+ });
95
+ tools.push({
96
+ id: 'cline',
97
+ name: 'Cline / Roo Code',
98
+ detected: Boolean(extHit),
99
+ evidence: extHit ?? null,
100
+ setup: 'paste',
101
+ configPath: null,
102
+ fields: ['API Provider = OpenAI Compatible', 'Base URL = <endpoint>', 'Model ID = <combo>'],
103
+ note: 'The extension stores settings in VS Code global state; paste these three fields.',
104
+ });
105
+
106
+ // Codex CLI
107
+ const codexPath = firstExisting(join(home, '.codex', 'config.toml'), which('codex', env));
108
+ tools.push({
109
+ id: 'codex',
110
+ name: 'Codex CLI',
111
+ detected: Boolean(codexPath),
112
+ evidence: codexPath,
113
+ setup: 'paste',
114
+ configPath: join(home, '.codex', 'config.toml'),
115
+ fields: ['model_provider', 'base_url = "<endpoint>"'],
116
+ });
117
+
118
+ // Anything that honours OPENAI_BASE_URL
119
+ tools.push({
120
+ id: 'env',
121
+ name: 'Any OpenAI-compatible tool (env vars)',
122
+ detected: true,
123
+ evidence: 'always available',
124
+ setup: 'paste',
125
+ configPath: null,
126
+ fields: ['OPENAI_BASE_URL=<endpoint>', 'OPENAI_API_KEY=<gateway key or any non-empty string>'],
127
+ });
128
+
129
+ return tools;
130
+ }
131
+
132
+ /**
133
+ * Write a tool's config, merging rather than replacing.
134
+ * @param {string} toolId
135
+ * @param {{baseUrl: string, combo: string, apiKey: string, home?: string, dryRun?: boolean}} opts
136
+ * @returns {{ok: boolean, path: string|null, message: string, snippet?: string}}
137
+ */
138
+ export function writeToolConfig(toolId, opts) {
139
+ const home = opts.home ?? homedir();
140
+ const baseUrl = opts.baseUrl.replace(/\/+$/, '');
141
+ const envSnippet = `export OPENAI_BASE_URL=${baseUrl}/v1\nexport OPENAI_API_KEY=${opts.apiKey}`;
142
+
143
+ if (toolId === 'aider') {
144
+ const path = join(home, '.aider.conf.yml');
145
+ const block = [
146
+ `# added by sui-router`,
147
+ `openai-api-base: ${baseUrl}/v1`,
148
+ `openai-api-key: ${opts.apiKey}`,
149
+ `model: openai/${opts.combo}`,
150
+ ].join('\n');
151
+ const existing = existsSync(path) ? readFileSync(path, 'utf8') : '';
152
+ const next = existing.includes('openai-api-base:')
153
+ ? existing.replace(/openai-api-base:.*/g, `openai-api-base: ${baseUrl}/v1`)
154
+ : `${existing.trimEnd()}${existing.trim() ? '\n' : ''}${block}\n`;
155
+ if (!opts.dryRun) writeFileSync(path, next);
156
+ return { ok: true, path, message: `Wrote ${path}. Aider will use ${opts.combo} through sui.` };
157
+ }
158
+
159
+ if (toolId === 'continue') {
160
+ const path = join(home, '.continue', 'config.json');
161
+ if (!existsSync(path)) {
162
+ return {
163
+ ok: false,
164
+ path,
165
+ message: 'Continue is installed but has no config.json yet. Open Continue once, then run this again.',
166
+ snippet: JSON.stringify({ models: [{ title: 'sui', provider: 'openai', model: opts.combo, apiBase: `${baseUrl}/v1`, apiKey: opts.apiKey }] }, null, 2),
167
+ };
168
+ }
169
+ let parsed;
170
+ try {
171
+ parsed = JSON.parse(readFileSync(path, 'utf8'));
172
+ } catch {
173
+ return { ok: false, path, message: `${path} is not valid JSON, so I left it alone. Fix it, then re-run.` };
174
+ }
175
+ parsed.models = Array.isArray(parsed.models) ? parsed.models : [];
176
+ const entry = {
177
+ title: `sui — ${opts.combo}`,
178
+ provider: 'openai',
179
+ model: opts.combo,
180
+ apiBase: `${baseUrl}/v1`,
181
+ apiKey: opts.apiKey,
182
+ };
183
+ const idx = parsed.models.findIndex((m) => m && m.apiBase === `${baseUrl}/v1`);
184
+ if (idx >= 0) parsed.models[idx] = entry;
185
+ else parsed.models.push(entry);
186
+ if (!opts.dryRun) writeFileSync(path, JSON.stringify(parsed, null, 2));
187
+ return { ok: true, path, message: `Added "${opts.combo}" to ${path}.` };
188
+ }
189
+
190
+ if (toolId === 'zed') {
191
+ const path = existsSync(join(home, '.config', 'zed', 'settings.json'))
192
+ ? join(home, '.config', 'zed', 'settings.json')
193
+ : join(home, '.zed', 'settings.json');
194
+ let parsed = {};
195
+ if (existsSync(path)) {
196
+ try {
197
+ parsed = JSON.parse(readFileSync(path, 'utf8'));
198
+ } catch {
199
+ return { ok: false, path, message: `${path} is not valid JSON, so I left it alone.` };
200
+ }
201
+ }
202
+ parsed.language_models = parsed.language_models ?? {};
203
+ parsed.language_models.openai = {
204
+ ...(parsed.language_models.openai ?? {}),
205
+ api_url: `${baseUrl}/v1`,
206
+ available_models: [...new Set([...(parsed.language_models.openai?.available_models ?? []), opts.combo])],
207
+ };
208
+ if (!opts.dryRun) {
209
+ mkdirSync(join(path, '..'), { recursive: true });
210
+ writeFileSync(path, JSON.stringify(parsed, null, 2));
211
+ }
212
+ return { ok: true, path, message: `Pointed Zed at sui in ${path}.` };
213
+ }
214
+
215
+ return {
216
+ ok: false,
217
+ path: null,
218
+ message: 'Paste these two lines into the tool:',
219
+ snippet: envSnippet,
220
+ };
221
+ }
222
+
223
+ function which(bin, env = process.env) {
224
+ const path = String(env.PATH ?? '');
225
+ for (const dir of path.split(':')) {
226
+ if (!dir) continue;
227
+ const candidate = join(dir, bin);
228
+ if (existsSync(candidate)) return candidate;
229
+ }
230
+ return null;
231
+ }
232
+
233
+ function readDirSafe(dir) {
234
+ return readdirSync(dir);
235
+ }
package/README.md DELETED
@@ -1,3 +0,0 @@
1
- # Temporary Holding Version
2
-
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.