gaunt-sloth-assistant
Version:
[](https://github.com/Galvanized-Pukeko/gaunt-sloth-assistant/actions/workflows/unit-tests.yml) [ {
const { ChatOpenAI } = await import('@langchain/openai');
// Use environment variable if available, otherwise use the config value
const openRouterApiKey = getApiKey(llmConfig);
if (!openRouterApiKey) {
throw new Error('You need to define OPEN_ROUTER_API_KEY environment variable, or set apiKey in your config file.');
}
const configFields = {
...llmConfig,
apiKey: openRouterApiKey,
model: llmConfig.model || 'moonshotai/kimi-k2',
configuration: {
baseURL: 'https://openrouter.ai/api/v1',
...(llmConfig.configuration || {}),
defaultHeaders: {
'HTTP-Referer': 'https://gaunt-sloth-assistant.github.io/',
'X-Title': 'Gaunt Sloth Assistant',
},
},
};
// eslint-disable-next-line @typescript-eslint/no-explicit-any
delete configFields.type;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
delete configFields.apiKeyEnvironmentVariable;
return new ChatOpenAI(configFields);
}
function getApiKey(llmConfig) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const conf = llmConfig;
if (conf.apiKeyEnvironmentVariable && env[conf.apiKeyEnvironmentVariable]) {
return env[conf.apiKeyEnvironmentVariable];
}
else {
return llmConfig.apiKey || env.OPEN_ROUTER_API_KEY || env.OPENROUTER_API_KEY;
}
}
const jsonContent = `{
"llm": {
"type": "openrouter",
"model": "moonshotai/kimi-k2"
}
}`;
export function init(configFileName) {
// Determine which content to use based on file extension
if (!configFileName.endsWith('.json')) {
throw new Error('Only JSON config is supported.');
}
writeFileIfNotExistsWithMessages(configFileName, jsonContent);
displayWarning(`You need to edit your ${configFileName} to configure model, ` +
'or define OPEN_ROUTER_API_KEY environment variable.');
}
//# sourceMappingURL=openrouter.js.map