copilot-api / src /lib /config.ts
imspsycho's picture
Initial upload from Google Colab
98c9143 verified
Raw
History Blame Contribute Delete
15.3 kB
import consola from "consola"
import { randomBytes } from "node:crypto"
import fs from "node:fs"
import { PATHS } from "./paths"
export interface AppConfig {
auth?: {
apiKeys?: Array<string>
adminApiKey?: string
}
providers?: Record<string, ProviderConfig>
modelMappings?: Record<string, string>
extraPrompts?: Record<string, string>
smallModel?: string
responsesApiContextManagementModels?: Array<string>
modelReasoningEfforts?: Record<
string,
"none" | "minimal" | "low" | "medium" | "high" | "xhigh"
>
useMessagesApi?: boolean
useResponsesApiWebSocket?: boolean
anthropicApiKey?: string
useResponsesApiWebSearch?: boolean
claudeTokenMultiplier?: number
}
export interface ModelConfig {
temperature?: number
topP?: number
topK?: number
extraBody?: Record<string, unknown>
contextCache?: boolean
supportPdf?: boolean
toolContentSupportType?: Array<ToolContentSupportType>
}
export type ProviderAuthType = "authorization" | "x-api-key"
export type ProviderType = "anthropic" | "openai-compatible"
export type ToolContentSupportType = "array" | "image" | "pdf"
export interface ProviderConfig {
type?: string
enabled?: boolean
baseUrl?: string
apiKey?: string
authType?: ProviderAuthType
models?: Record<string, ModelConfig>
adjustInputTokens?: boolean
}
export interface ResolvedProviderConfig {
name: string
type: ProviderType
baseUrl: string
apiKey: string
authType: ProviderAuthType
models?: Record<string, ModelConfig>
adjustInputTokens?: boolean
}
const gpt5ExplorationPrompt = `## Exploration and reading files
- **Think first.** Before any tool call, decide ALL files/resources you will need.
- **Batch everything.** If you need multiple files (even from different places), read them together.
- **multi_tool_use.parallel** Use multi_tool_use.parallel to parallelize tool calls and only this.
- **Only make sequential calls if you truly cannot know the next file without seeing a result first.**
- **Workflow:** (a) plan all needed reads → (b) issue one parallel batch → (c) analyze results → (d) repeat if new, unpredictable reads arise.`
const gpt5CommentaryPrompt = `# Working with the user
You interact with the user through a terminal. You have 2 ways of communicating with the users:
- Share intermediary updates in \`commentary\` channel.
- After you have completed all your work, send a message to the \`final\` channel.
## Intermediary updates
- Intermediary updates go to the \`commentary\` channel.
- User updates are short updates while you are working, they are NOT final answers.
- You use 1-2 sentence user updates to communicate progress and new information to the user as you are doing work.
- Do not begin responses with conversational interjections or meta commentary. Avoid openers such as acknowledgements (“Done —”, “Got it”, “Great question, ”) or framing phrases.
- You provide user updates frequently, every 20s.
- Before exploring or doing substantial work, you start with a user update acknowledging the request and explaining your first step. You should include your understanding of the user request and explain what you will do. Avoid commenting on the request or using starters such as "Got it -" or "Understood -" etc.
- When exploring, e.g. searching, reading files, you provide user updates as you go, every 20s, explaining what context you are gathering and what you've learned. Vary your sentence structure when providing these updates to avoid sounding repetitive - in particular, don't start each sentence the same way.
- After you have sufficient context, and the work is substantial, you provide a longer plan (this is the only user update that may be longer than 2 sentences and can contain formatting).
- Before performing file edits of any kind, you provide updates explaining what edits you are making.
- As you are thinking, you very frequently provide updates even if not taking any actions, informing the user of your progress. You interrupt your thinking and send multiple updates in a row if thinking for more than 100 words.
- Tone of your updates MUST match your personality.`
const defaultConfig: AppConfig = {
auth: {
apiKeys: [],
},
providers: {},
modelMappings: {},
extraPrompts: {
"gpt-5-mini": gpt5ExplorationPrompt,
"gpt-5.3-codex": gpt5CommentaryPrompt,
"gpt-5.4-mini": gpt5CommentaryPrompt,
"gpt-5.4": gpt5CommentaryPrompt,
"gpt-5.5": gpt5CommentaryPrompt,
},
smallModel: "gpt-5-mini",
responsesApiContextManagementModels: [],
modelReasoningEfforts: {
"gpt-5-mini": "low",
"gpt-5.3-codex": "xhigh",
"gpt-5.4-mini": "xhigh",
"gpt-5.4": "xhigh",
"gpt-5.5": "xhigh",
},
useMessagesApi: true,
useResponsesApiWebSocket: true,
useResponsesApiWebSearch: true,
}
let cachedConfig: AppConfig | null = null
function normalizeAdminApiKey(adminApiKey: unknown): string | null {
if (typeof adminApiKey !== "string") {
if (adminApiKey !== undefined) {
consola.warn(
"Invalid auth.adminApiKey config. Expected a non-empty string.",
)
}
return null
}
const normalizedAdminApiKey = adminApiKey.trim()
if (!normalizedAdminApiKey) {
consola.warn(
"Invalid auth.adminApiKey config. Expected a non-empty string.",
)
return null
}
return normalizedAdminApiKey
}
function generateAdminApiKey(): string {
return randomBytes(32).toString("hex")
}
function isNodeError(error: unknown): error is NodeJS.ErrnoException {
return error instanceof Error && "code" in error
}
function ensureConfigFile(): void {
try {
fs.accessSync(PATHS.CONFIG_PATH, fs.constants.R_OK | fs.constants.W_OK)
} catch {
fs.mkdirSync(PATHS.APP_DIR, { recursive: true })
fs.writeFileSync(
PATHS.CONFIG_PATH,
`${JSON.stringify(defaultConfig, null, 2)}\n`,
"utf8",
)
try {
fs.chmodSync(PATHS.CONFIG_PATH, 0o600)
} catch {
return
}
}
}
function readConfigFromDisk(): AppConfig {
ensureConfigFile()
try {
const raw = fs.readFileSync(PATHS.CONFIG_PATH, "utf8")
if (!raw.trim()) {
fs.writeFileSync(
PATHS.CONFIG_PATH,
`${JSON.stringify(defaultConfig, null, 2)}\n`,
"utf8",
)
return defaultConfig
}
return JSON.parse(raw) as AppConfig
} catch (error) {
consola.error("Failed to read config file, using default config", error)
return defaultConfig
}
}
function readEditableConfigFromDisk(): AppConfig {
try {
const raw = fs.readFileSync(PATHS.CONFIG_PATH, "utf8")
if (!raw.trim()) {
return {}
}
return JSON.parse(raw) as AppConfig
} catch (error) {
if (isNodeError(error) && error.code === "ENOENT") {
return {}
}
if (error instanceof SyntaxError) {
throw new Error(`Config file is not valid JSON: ${PATHS.CONFIG_PATH}`)
}
throw error
}
}
function writeConfigToDisk(config: AppConfig): void {
fs.mkdirSync(PATHS.APP_DIR, { recursive: true })
fs.writeFileSync(
PATHS.CONFIG_PATH,
`${JSON.stringify(config, null, 2)}\n`,
"utf8",
)
}
function mergeDefaultConfig(config: AppConfig): {
mergedConfig: AppConfig
changed: boolean
} {
const extraPrompts = config.extraPrompts ?? {}
const defaultExtraPrompts = defaultConfig.extraPrompts ?? {}
const modelReasoningEfforts = config.modelReasoningEfforts ?? {}
const defaultModelReasoningEfforts = defaultConfig.modelReasoningEfforts ?? {}
const missingExtraPromptModels = Object.keys(defaultExtraPrompts).filter(
(model) => !Object.hasOwn(extraPrompts, model),
)
const missingReasoningEffortModels = Object.keys(
defaultModelReasoningEfforts,
).filter((model) => !Object.hasOwn(modelReasoningEfforts, model))
const hasExtraPromptChanges = missingExtraPromptModels.length > 0
const hasReasoningEffortChanges = missingReasoningEffortModels.length > 0
if (!hasExtraPromptChanges && !hasReasoningEffortChanges) {
return { mergedConfig: config, changed: false }
}
return {
mergedConfig: {
...config,
extraPrompts: {
...defaultExtraPrompts,
...extraPrompts,
},
modelReasoningEfforts: {
...defaultModelReasoningEfforts,
...modelReasoningEfforts,
},
},
changed: true,
}
}
function ensureAdminApiKey(config: AppConfig): {
mergedConfig: AppConfig
changed: boolean
} {
const normalizedAdminApiKey = normalizeAdminApiKey(config.auth?.adminApiKey)
if (normalizedAdminApiKey) {
if (config.auth?.adminApiKey === normalizedAdminApiKey) {
return { mergedConfig: config, changed: false }
}
return {
mergedConfig: {
...config,
auth: {
...(config.auth ?? {}),
adminApiKey: normalizedAdminApiKey,
},
},
changed: true,
}
}
const editableConfig = readEditableConfigFromDisk()
const { mergedConfig } = mergeDefaultConfig({
...editableConfig,
auth: {
...(editableConfig.auth ?? {}),
adminApiKey: generateAdminApiKey(),
},
})
return { mergedConfig, changed: true }
}
export function mergeConfigWithDefaults(): AppConfig {
const config = readConfigFromDisk()
const { mergedConfig, changed } = mergeDefaultConfig(config)
const {
mergedConfig: mergedConfigWithAdminApiKey,
changed: adminApiKeyChanged,
} = ensureAdminApiKey(mergedConfig)
const shouldPersistConfig = changed || adminApiKeyChanged
if (shouldPersistConfig) {
try {
writeConfigToDisk(mergedConfigWithAdminApiKey)
} catch (writeError) {
if (adminApiKeyChanged) {
throw writeError
}
consola.warn(
"Failed to write merged extraPrompts to config file",
writeError,
)
}
}
cachedConfig = mergedConfigWithAdminApiKey
return mergedConfigWithAdminApiKey
}
export function getConfig(): AppConfig {
cachedConfig ??= mergeDefaultConfig(readConfigFromDisk()).mergedConfig
return cachedConfig
}
export function reloadConfig(): AppConfig {
return mergeConfigWithDefaults()
}
export function getExtraPromptForModel(model: string): string {
const config = getConfig()
return config.extraPrompts?.[model] ?? ""
}
export function getModelMappings(): Record<string, string> {
const config = getConfig()
const modelMappings = config.modelMappings
if (!modelMappings) {
return { ...(defaultConfig.modelMappings ?? {}) }
}
const validMappings: Record<string, string> = {}
for (const [sourceModel, targetModel] of Object.entries(modelMappings)) {
if (
!sourceModel
|| typeof targetModel !== "string"
|| targetModel.length === 0
) {
continue
}
validMappings[sourceModel] = targetModel
}
return validMappings
}
function validateModelMappings(
modelMappings: Record<string, string>,
): Record<string, string> {
const validatedMappings: Record<string, string> = {}
for (const [sourceModel, targetModel] of Object.entries(modelMappings)) {
if (!sourceModel || !targetModel) {
throw new Error(
"Each model mapping must use non-empty source and target values.",
)
}
validatedMappings[sourceModel] = targetModel
}
return validatedMappings
}
export function setModelMappings(
modelMappings: Record<string, string>,
): Record<string, string> {
const nextConfig = {
...readEditableConfigFromDisk(),
modelMappings: validateModelMappings(modelMappings),
}
writeConfigToDisk(nextConfig)
cachedConfig = reloadConfig()
return getModelMappings()
}
export function resolveMappedModel(model: string): string {
return getModelMappings()[model] ?? model
}
export function getSmallModel(): string {
const config = getConfig()
return config.smallModel ?? "gpt-5-mini"
}
export function getResponsesApiContextManagementModels(): Array<string> {
const config = getConfig()
return (
config.responsesApiContextManagementModels
?? defaultConfig.responsesApiContextManagementModels
?? []
)
}
export function isResponsesApiContextManagementModel(model: string): boolean {
return getResponsesApiContextManagementModels().includes(model)
}
export function getReasoningEffortForModel(
model: string,
): "none" | "minimal" | "low" | "medium" | "high" | "xhigh" {
const config = getConfig()
return config.modelReasoningEfforts?.[model] ?? "high"
}
export function normalizeProviderBaseUrl(url: string): string {
return url.trim().replace(/\/+$/u, "")
}
function getDefaultProviderAuthType(
providerType: ProviderType,
): ProviderAuthType {
return providerType === "openai-compatible" ? "authorization" : "x-api-key"
}
export function resolveProviderAuthType(
providerName: string,
authType: string | undefined,
providerType: ProviderType,
): ProviderAuthType {
if (authType === undefined) {
return getDefaultProviderAuthType(providerType)
}
if (authType === "x-api-key") {
return "x-api-key"
}
if (authType === "authorization") {
return authType
}
consola.warn(
`Provider ${providerName} has invalid authType '${authType}', falling back to ${getDefaultProviderAuthType(providerType)}`,
)
return getDefaultProviderAuthType(providerType)
}
export function getProviderConfig(name: string): ResolvedProviderConfig | null {
const providerName = name.trim()
if (!providerName) {
return null
}
const config = getConfig()
const provider = config.providers?.[providerName]
if (!provider) {
return null
}
if (provider.enabled === false) {
return null
}
const type = provider.type ?? "anthropic"
if (type !== "anthropic" && type !== "openai-compatible") {
consola.warn(
`Provider ${providerName} is ignored because type '${type}' is not supported`,
)
return null
}
const baseUrl = normalizeProviderBaseUrl(provider.baseUrl ?? "")
const apiKey = (provider.apiKey ?? "").trim()
const authType = resolveProviderAuthType(
providerName,
provider.authType,
type,
)
if (!baseUrl || !apiKey) {
consola.warn(
`Provider ${providerName} is enabled but missing baseUrl or apiKey`,
)
return null
}
return {
name: providerName,
type,
baseUrl,
apiKey,
authType,
models: provider.models,
adjustInputTokens: provider.adjustInputTokens,
}
}
export function listEnabledProviders(): Array<string> {
const config = getConfig()
const providerNames = Object.keys(config.providers ?? {})
return providerNames.filter((name) => getProviderConfig(name) !== null)
}
export function isMessagesApiEnabled(): boolean {
const config = getConfig()
return config.useMessagesApi ?? true
}
export function isResponsesApiWebSocketEnabled(): boolean {
const config = getConfig()
return config.useResponsesApiWebSocket ?? true
}
export function getAnthropicApiKey(): string | undefined {
const config = getConfig()
return config.anthropicApiKey ?? process.env.ANTHROPIC_API_KEY ?? undefined
}
export function isResponsesApiWebSearchEnabled(): boolean {
const config = getConfig()
return config.useResponsesApiWebSearch ?? true
}
export function getClaudeTokenMultiplier(): number {
const config = getConfig()
return config.claudeTokenMultiplier ?? 1.15
}