imtoagent 0.3.12 → 0.3.14
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.
|
@@ -10,6 +10,10 @@ import type { AgentAdapter, AgentInput, AgentOutput } from '../core/types';
|
|
|
10
10
|
import { buildAttachmentHint } from '../core/types';
|
|
11
11
|
import { buildSystemPrompt } from '../prompt-builder';
|
|
12
12
|
import { getDataDir } from '../utils/paths';
|
|
13
|
+
import { getNpmGlobalBin } from '../utils/backend-check';
|
|
14
|
+
import * as fs from 'fs';
|
|
15
|
+
import * as os from 'os';
|
|
16
|
+
import * as path from 'path';
|
|
13
17
|
|
|
14
18
|
// ================================================================
|
|
15
19
|
// OpenCodeAdapter 上下文
|
|
@@ -248,8 +252,21 @@ export async function startOpenCodeServer(): Promise<void> {
|
|
|
248
252
|
} catch {}
|
|
249
253
|
|
|
250
254
|
console.log('[OpenCodeAdapter] starting opencode serve...');
|
|
255
|
+
|
|
256
|
+
// Resolve opencode binary with fallbacks (matches checkOne() logic)
|
|
257
|
+
const home = os.homedir();
|
|
258
|
+
const customPath = path.join(home, '.opencode', 'bin', 'opencode');
|
|
259
|
+
const npmBin = getNpmGlobalBin();
|
|
260
|
+
const npmBinPath = npmBin ? path.join(npmBin, 'opencode') : null;
|
|
261
|
+
const ocBinPath =
|
|
262
|
+
(npmBinPath && fs.existsSync(npmBinPath)) ? npmBinPath :
|
|
263
|
+
fs.existsSync(customPath) ? customPath :
|
|
264
|
+
'opencode'; // fallback to PATH lookup
|
|
265
|
+
|
|
266
|
+
console.log(`[OpenCodeAdapter] Using opencode binary: ${ocBinPath}`);
|
|
267
|
+
|
|
251
268
|
const child = Bun.spawn(
|
|
252
|
-
[
|
|
269
|
+
[ocBinPath, 'serve', '--port', String(OC_PORT), '--hostname', '127.0.0.1'],
|
|
253
270
|
{
|
|
254
271
|
cwd: getDataDir(),
|
|
255
272
|
env: {
|
|
@@ -28,7 +28,7 @@ const BACKEND_DEFS: Omit<BackendInfo, 'installed' | 'version'>[] = [
|
|
|
28
28
|
|
|
29
29
|
let _cachedNpmBin: string | null | undefined = undefined;
|
|
30
30
|
|
|
31
|
-
function getNpmGlobalBin(): string | null {
|
|
31
|
+
export function getNpmGlobalBin(): string | null {
|
|
32
32
|
if (_cachedNpmBin !== undefined) return _cachedNpmBin;
|
|
33
33
|
try {
|
|
34
34
|
const prefix = execSync('npm get prefix', { encoding: 'utf-8', timeout: 5000 }).trim();
|
|
@@ -111,6 +111,38 @@ export function formatBackendStatus(backends: BackendInfo[]): string {
|
|
|
111
111
|
}).join('\n');
|
|
112
112
|
}
|
|
113
113
|
|
|
114
|
+
// ================================================================
|
|
115
|
+
// Shell config helpers — auto-add paths for user
|
|
116
|
+
// ================================================================
|
|
117
|
+
|
|
118
|
+
function getShellConfigFile(): string | null {
|
|
119
|
+
const candidates = ['.zshrc', '.bashrc', '.bash_profile', '.profile'];
|
|
120
|
+
const home = os.homedir();
|
|
121
|
+
for (const name of candidates) {
|
|
122
|
+
const p = path.join(home, name);
|
|
123
|
+
if (fs.existsSync(p)) return p;
|
|
124
|
+
}
|
|
125
|
+
// fallback: create .zshrc on macOS
|
|
126
|
+
if (process.platform === 'darwin') {
|
|
127
|
+
return path.join(home, '.zshrc');
|
|
128
|
+
}
|
|
129
|
+
return null;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
function ensurePathInConfig(configPath: string, binDir: string): void {
|
|
133
|
+
const exportLine = `export PATH="${binDir}:$PATH"`;
|
|
134
|
+
try {
|
|
135
|
+
if (fs.existsSync(configPath)) {
|
|
136
|
+
const content = fs.readFileSync(configPath, 'utf-8');
|
|
137
|
+
// Skip if already present
|
|
138
|
+
if (content.includes(binDir)) return;
|
|
139
|
+
}
|
|
140
|
+
fs.appendFileSync(configPath, `\n# Added by imtoagent setup\n${exportLine}\n`);
|
|
141
|
+
// Also update current process.env for immediate detection
|
|
142
|
+
process.env.PATH = `${binDir}:${process.env.PATH}`;
|
|
143
|
+
} catch {}
|
|
144
|
+
}
|
|
145
|
+
|
|
114
146
|
// ================================================================
|
|
115
147
|
// 自动安装后端 CLI
|
|
116
148
|
// ================================================================
|
|
@@ -171,7 +203,8 @@ export async function installBackend(
|
|
|
171
203
|
return false;
|
|
172
204
|
}
|
|
173
205
|
|
|
174
|
-
// 安装完成后验证 —
|
|
206
|
+
// 安装完成后验证 — 按优先级依次检查
|
|
207
|
+
// 1) npm global bin
|
|
175
208
|
if (npmBinDir) {
|
|
176
209
|
const binPath = path.join(npmBinDir, b.type);
|
|
177
210
|
try {
|
|
@@ -183,18 +216,25 @@ export async function installBackend(
|
|
|
183
216
|
} catch {}
|
|
184
217
|
}
|
|
185
218
|
|
|
186
|
-
//
|
|
219
|
+
// 2) OpenCode custom install path
|
|
187
220
|
if (type === 'opencode') {
|
|
188
|
-
const
|
|
221
|
+
const opencodeBinDir = path.join(os.homedir(), '.opencode', 'bin');
|
|
222
|
+
const opencodePath = path.join(opencodeBinDir, 'opencode');
|
|
189
223
|
if (fs.existsSync(opencodePath)) {
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
224
|
+
try {
|
|
225
|
+
const version = execSync(`"${opencodePath}" version`, { encoding: 'utf-8', timeout: 5000 }).trim();
|
|
226
|
+
// 自动配置 PATH(如果 shell 配置文件存在且未包含该行)
|
|
227
|
+
const shellConfig = getShellConfigFile();
|
|
228
|
+
if (shellConfig) {
|
|
229
|
+
ensurePathInConfig(shellConfig, opencodeBinDir);
|
|
230
|
+
}
|
|
231
|
+
console.log(`\n✅ ${b.label} installed successfully! Version: ${version}`);
|
|
232
|
+
return true;
|
|
233
|
+
} catch {}
|
|
194
234
|
}
|
|
195
235
|
}
|
|
196
236
|
|
|
197
|
-
//
|
|
237
|
+
// 3) via PATH
|
|
198
238
|
const info = checkOne(b);
|
|
199
239
|
if (info.installed) {
|
|
200
240
|
console.log(`\n✅ ${b.label} installed successfully! Version: ${info.version}`);
|