config.ts
1.78 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
import { DEFAULT_MODEL_CONFIG } from './default-config.js';
import type { AiModelConfig, DefaultConfigManager } from './types.js';
/**
* The caller-set default, or null when the caller never configured one.
* Deliberately process-local and in-memory: no file IO, no env reads.
*/
let userConfig: AiModelConfig | null = null;
/**
* The module's internal default config (see default-config.ts). Returned when
* no caller-set config exists, so a bare completeChat() still resolves to a
* concrete model identity instead of failing.
*/
export function getBuiltinDefaultConfig(): AiModelConfig {
return DEFAULT_MODEL_CONFIG;
}
const defaultConfigManager: DefaultConfigManager = {
setDefaultModelConfig(cfg: AiModelConfig): void {
if (!cfg || !cfg.modelName || !cfg.apiKey) {
throw new Error('[ai-client] setDefaultModelConfig requires modelName and apiKey');
}
userConfig = cfg;
},
getDefaultModelConfig(): AiModelConfig | null {
return userConfig;
},
clearDefaultModelConfig(): void {
userConfig = null;
},
};
/** Set the process-wide default model config. */
export function setDefaultModelConfig(cfg: AiModelConfig): void {
defaultConfigManager.setDefaultModelConfig(cfg);
}
/**
* Read the caller-set default model config. Returns null until a caller sets
* one via setDefaultModelConfig — when null, completeChat falls back to the
* module's internal config file (default-config.ts) rather than failing.
*/
export function getDefaultModelConfig(): AiModelConfig | null {
return defaultConfigManager.getDefaultModelConfig();
}
/** Forget the process-wide default model config. */
export function clearDefaultModelConfig(): void {
defaultConfigManager.clearDefaultModelConfig();
}
export { defaultConfigManager };