-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathconfig.ts
More file actions
87 lines (75 loc) · 2.35 KB
/
config.ts
File metadata and controls
87 lines (75 loc) · 2.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import { join } from 'path';
import { homedir } from 'os';
import { z } from 'zod';
import { getDataDir } from './paths';
import { MCConfigSchema, PartialMCConfigSchema } from './schemas';
import { PermissionPolicy } from './permission-policy';
import { atomicWrite } from './utils';
export type WorktreeSetup = {
copyFiles?: string[];
symlinkDirs?: string[];
commands?: string[];
};
export type MCConfig = z.infer<typeof MCConfigSchema>;
const DEFAULT_CONFIG: MCConfig = {
defaultPlacement: 'session',
pollInterval: 10000,
idleThreshold: 300000,
worktreeBasePath: join(homedir(), '.local', 'share', 'opencode-mission-control'),
maxParallel: 3,
autoCommit: true,
testTimeout: 600000,
mergeStrategy: 'squash',
useServeMode: true,
portRangeStart: 14100,
portRangeEnd: 14199,
fixBeforeRollbackTimeout: 120000,
defaultPermissionPolicy: PermissionPolicy.getDefaultPolicy(),
omo: {
enabled: false,
defaultMode: 'vanilla',
},
};
const CONFIG_FILE = 'config.json';
export async function getConfigPath(): Promise<string> {
const dataDir = await getDataDir();
return join(dataDir, CONFIG_FILE);
}
export async function loadConfig(): Promise<MCConfig> {
const filePath = await getConfigPath();
const file = Bun.file(filePath);
const exists = await file.exists();
if (!exists) {
return { ...DEFAULT_CONFIG };
}
try {
const content = await file.text();
const fileConfig = PartialMCConfigSchema.parse(JSON.parse(content));
const result: MCConfig = {
...DEFAULT_CONFIG,
...fileConfig,
omo: {
...DEFAULT_CONFIG.omo,
...(fileConfig.omo || {}),
},
};
return result;
} catch (error) {
if (error instanceof z.ZodError) {
throw new Error(`Invalid config in ${filePath}: ${error.issues.map(e => `${e.path.join('.')}: ${e.message}`).join(', ')}`);
}
throw new Error(`Failed to load config from ${filePath}: ${error}`);
}
}
export async function saveConfig(config: MCConfig): Promise<void> {
const filePath = await getConfigPath();
try {
const configToSave = Object.fromEntries(
Object.entries(config).filter(([, value]) => value !== undefined)
) as MCConfig;
const data = JSON.stringify(configToSave, null, 2);
await atomicWrite(filePath, data);
} catch (error) {
throw new Error(`Failed to save config to ${filePath}: ${error}`);
}
}