@nanocollective/nanocoder
Version:
A local-first CLI coding agent that brings the power of agentic coding tools like Claude Code and Gemini CLI to local models or controlled APIs like OpenRouter
57 lines • 2.17 kB
JavaScript
import { createAnthropic } from '@ai-sdk/anthropic';
import { createGoogleGenerativeAI, } from '@ai-sdk/google';
import { createOpenAICompatible, } from '@ai-sdk/openai-compatible';
import { fetch as undiciFetch } from 'undici';
import { getLogger } from '../../utils/logging/index.js';
/**
* Creates an AI SDK provider based on the sdkProvider configuration.
* Defaults to 'openai-compatible' if not specified.
*/
export function createProvider(providerConfig, undiciAgent) {
const logger = getLogger();
const { config, sdkProvider } = providerConfig;
// Use explicit sdkProvider if set, otherwise default to 'openai-compatible'
if (sdkProvider === 'anthropic') {
logger.info('Using @ai-sdk/anthropic provider', {
provider: providerConfig.name,
sdkProvider,
});
return createAnthropic({
baseURL: config.baseURL || undefined,
apiKey: config.apiKey ?? '',
headers: config.headers,
});
}
if (sdkProvider === 'google') {
logger.info('Using @ai-sdk/google provider', {
provider: providerConfig.name,
sdkProvider,
});
return createGoogleGenerativeAI({
apiKey: config.apiKey ?? '',
});
}
// Custom fetch using undici
const customFetch = (url, options) => {
// Type cast to string | URL since undici's fetch accepts these types
// Request objects are converted to URL internally by the fetch spec
return undiciFetch(url, {
...options,
dispatcher: undiciAgent,
});
};
// Add OpenRouter-specific headers for app attribution
const headers = config.headers ?? {};
if (providerConfig.name.toLowerCase() === 'openrouter') {
headers['HTTP-Referer'] = 'https://github.com/Nano-Collective/nanocoder';
headers['X-Title'] = 'Nanocoder';
}
return createOpenAICompatible({
name: providerConfig.name,
baseURL: config.baseURL ?? '',
apiKey: config.apiKey ?? 'dummy-key',
fetch: customFetch,
headers,
});
}
//# sourceMappingURL=provider-factory.js.map