|
1 | | -import { readFileSync, existsSync } from 'fs' |
2 | | -import { homedir } from 'os' |
| 1 | +import { existsSync } from 'fs' |
| 2 | +import { readFileSync } from 'fs' |
3 | 3 | import { join } from 'path' |
| 4 | +import { getClaudeConfigHomeDir } from './envUtils.js' |
| 5 | +import { parseYaml } from './yaml.js' |
4 | 6 |
|
5 | 7 | let cachedURL: string | null = null |
6 | 8 |
|
7 | | -function parseYAMLSection(text: string, section: string): Record<string, string> { |
8 | | - const result: Record<string, string> = {} |
9 | | - const sectionRegex = new RegExp(`^${section}:\\s*$`, 'm') |
10 | | - const match = sectionRegex.exec(text) |
11 | | - if (!match) return result |
12 | | - const afterSection = text.slice(match.index + match[0].length) |
13 | | - const lines = afterSection.split('\n') |
14 | | - for (const line of lines) { |
15 | | - const trimmed = line.match(/^(\s{2,})\S/) |
16 | | - if (!trimmed) break |
17 | | - const kvMatch = line.match(/^\s+(\w+):\s*["']?(.*?)["']?\s*$/) |
18 | | - if (kvMatch) { |
19 | | - result[kvMatch[1]] = kvMatch[2] |
20 | | - } |
21 | | - } |
22 | | - return result |
23 | | -} |
24 | | - |
25 | 9 | export function getArtifactEngineURL(): string { |
26 | | - if (cachedURL) return cachedURL |
| 10 | + if (cachedURL !== null) return cachedURL |
27 | 11 |
|
28 | 12 | if (process.env.ARTIFACT_ENGINE_URL) { |
29 | 13 | cachedURL = process.env.ARTIFACT_ENGINE_URL |
30 | 14 | return cachedURL |
31 | 15 | } |
32 | 16 |
|
33 | 17 | const configPath = process.env.FUSION_ARTIFACTS_CONFIG || |
34 | | - join(homedir(), '.fusion', 'artifacts', 'config.yaml') |
| 18 | + join(getClaudeConfigHomeDir(), 'artifacts', 'config.yaml') |
35 | 19 |
|
36 | 20 | try { |
37 | 21 | if (existsSync(configPath)) { |
38 | 22 | const content = readFileSync(configPath, 'utf-8') |
39 | | - const server = parseYAMLSection(content, 'server') |
40 | | - const host = server.host || '127.0.0.1' |
41 | | - const port = server.port || '8892' |
| 23 | + const parsed = parseYaml(content) as Record<string, Record<string, string>> | null |
| 24 | + const server = parsed?.server |
| 25 | + const host = server?.host || '127.0.0.1' |
| 26 | + const port = server?.port || '8892' |
42 | 27 | cachedURL = `http://${host}:${port}` |
| 28 | + console.log(`[artifactConfig] loaded from ${configPath}: ${cachedURL}`) |
43 | 29 | return cachedURL |
44 | 30 | } |
45 | | - } catch { |
46 | | - // Fall through to default |
| 31 | + } catch (e) { |
| 32 | + console.log(`[artifactConfig] failed to read ${configPath}: ${e}`) |
47 | 33 | } |
48 | 34 |
|
49 | 35 | cachedURL = 'http://127.0.0.1:8892' |
|
0 commit comments