xonecas.io
64875
Mc

mini-coder / commit 5cf366a

refactor(llm-api): simplify provider construction with lazy factories

xonecas2026-03-14 5cf366adda469d653b13caf2dd87d03192ad2abe parent b3d248f

1 file changed

src/llm-api/providers.ts+64−84
@@ -26,10 +26,16 @@ type ProviderName = (typeof SUPPORTED_PROVIDERS)[number];
2626
2727 const ZEN_BASE = "https://opencode.ai/zen/v1";
2828
29-function getFetchWithLogging(): typeof fetch {
29+type ProviderFetch = typeof fetch;
30+type AnthropicProvider = ReturnType<typeof createAnthropic>;
31+type OpenAIProvider = ReturnType<typeof createOpenAI>;
32+type GoogleProvider = ReturnType<typeof createGoogleGenerativeAI>;
33+type OpenAICompatProvider = ReturnType<typeof createOpenAICompatible>;
34+
35+function createFetchWithLogging(): ProviderFetch {
3036 const customFetch = async (
31- input: Parameters<typeof fetch>[0],
32- init?: Parameters<typeof fetch>[1],
37+ input: Parameters<ProviderFetch>[0],
38+ init?: Parameters<ProviderFetch>[1],
3339 ): Promise<Response> => {
3440 if (init?.body) {
3541 try {
@@ -52,10 +58,10 @@ function getFetchWithLogging(): typeof fetch {
5258 }
5359 return fetch(input, init);
5460 };
55- return customFetch as unknown as typeof fetch;
61+ return customFetch as ProviderFetch;
5662 }
5763
58-const fetchWithLogging = getFetchWithLogging();
64+const fetchWithLogging = createFetchWithLogging();
5965
6066 function requireEnv(name: string): string {
6167 const value = process.env[name];
@@ -71,131 +77,105 @@ function requireAnyEnv(names: string[]): string {
7177 throw new Error(`${names.join(" or ")} is not set`);
7278 }
7379
74-let _zenAnthropic: ReturnType<typeof createAnthropic> | null = null;
75-let _zenOpenAI: ReturnType<typeof createOpenAI> | null = null;
76-let _zenGoogle: ReturnType<typeof createGoogleGenerativeAI> | null = null;
77-let _zenCompat: ReturnType<typeof createOpenAICompatible> | null = null;
80+function lazy<T>(factory: () => T): () => T {
81+ let instance: T | null = null;
82+ return () => {
83+ if (instance === null) {
84+ instance = factory();
85+ }
86+ return instance;
87+ };
88+}
7889
79-function zenAnthropic() {
80- if (!_zenAnthropic) {
81- _zenAnthropic = createAnthropic({
90+const zenProviders = {
91+ anthropic: lazy<AnthropicProvider>(() =>
92+ createAnthropic({
8293 fetch: fetchWithLogging,
8394 apiKey: requireEnv("OPENCODE_API_KEY"),
8495 baseURL: ZEN_BASE,
85- });
86- }
87- return _zenAnthropic;
88-}
89-
90-function zenOpenAI() {
91- if (!_zenOpenAI) {
92- _zenOpenAI = createOpenAI({
96+ }),
97+ ),
98+ openai: lazy<OpenAIProvider>(() =>
99+ createOpenAI({
93100 fetch: fetchWithLogging,
94101 apiKey: requireEnv("OPENCODE_API_KEY"),
95102 baseURL: ZEN_BASE,
96- });
97- }
98- return _zenOpenAI;
99-}
100-
101-function zenGoogle() {
102- if (!_zenGoogle) {
103- _zenGoogle = createGoogleGenerativeAI({
103+ }),
104+ ),
105+ google: lazy<GoogleProvider>(() =>
106+ createGoogleGenerativeAI({
104107 fetch: fetchWithLogging,
105108 apiKey: requireEnv("OPENCODE_API_KEY"),
106109 baseURL: ZEN_BASE,
107- });
108- }
109- return _zenGoogle;
110-}
111-
112-function zenCompat() {
113- if (!_zenCompat) {
114- _zenCompat = createOpenAICompatible({
110+ }),
111+ ),
112+ compat: lazy<OpenAICompatProvider>(() =>
113+ createOpenAICompatible({
115114 fetch: fetchWithLogging,
116115 name: "zen-compat",
117116 apiKey: requireEnv("OPENCODE_API_KEY"),
118117 baseURL: ZEN_BASE,
119- });
120- }
121- return _zenCompat;
122-}
123-
124-let _directAnthropic: ReturnType<typeof createAnthropic> | null = null;
125-let _directOpenAI: ReturnType<typeof createOpenAI> | null = null;
126-let _directGoogle: ReturnType<typeof createGoogleGenerativeAI> | null = null;
127-let _ollama: ReturnType<typeof createOpenAICompatible> | null = null;
118+ }),
119+ ),
120+};
128121
129-function directAnthropic() {
130- if (!_directAnthropic) {
131- _directAnthropic = createAnthropic({
122+const directProviders = {
123+ anthropic: lazy<AnthropicProvider>(() =>
124+ createAnthropic({
132125 fetch: fetchWithLogging,
133126 apiKey: requireEnv("ANTHROPIC_API_KEY"),
134- });
135- }
136- return _directAnthropic;
137-}
138-
139-function directOpenAI() {
140- if (!_directOpenAI) {
141- _directOpenAI = createOpenAI({
127+ }),
128+ ),
129+ openai: lazy<OpenAIProvider>(() =>
130+ createOpenAI({
142131 fetch: fetchWithLogging,
143132 apiKey: requireEnv("OPENAI_API_KEY"),
144- });
145- }
146- return _directOpenAI;
147-}
148-
149-function directGoogle() {
150- if (!_directGoogle) {
151- _directGoogle = createGoogleGenerativeAI({
133+ }),
134+ ),
135+ google: lazy<GoogleProvider>(() =>
136+ createGoogleGenerativeAI({
152137 fetch: fetchWithLogging,
153138 apiKey: requireAnyEnv(["GOOGLE_API_KEY", "GEMINI_API_KEY"]),
154- });
155- }
156- return _directGoogle;
157-}
158-
159-function ollamaProvider() {
160- if (!_ollama) {
139+ }),
140+ ),
141+ ollama: lazy<OpenAICompatProvider>(() => {
161142 const baseURL = process.env.OLLAMA_BASE_URL ?? "http://localhost:11434";
162- _ollama = createOpenAICompatible({
143+ return createOpenAICompatible({
163144 name: "ollama",
164145 baseURL: `${baseURL}/v1`,
165146 apiKey: "ollama",
166147 fetch: fetchWithLogging,
167148 });
168- }
169- return _ollama;
170-}
149+ }),
150+};
171151
172152 function resolveZenModel(modelId: string): LanguageModel {
173153 switch (getZenBackend(modelId)) {
174154 case "anthropic":
175- return zenAnthropic()(modelId);
155+ return zenProviders.anthropic()(modelId);
176156 case "openai":
177- return zenOpenAI().responses(modelId);
157+ return zenProviders.openai().responses(modelId);
178158 case "google":
179- return zenGoogle()(modelId);
159+ return zenProviders.google()(modelId);
180160 case "compat":
181- return zenCompat()(modelId);
161+ return zenProviders.compat()(modelId);
182162 }
183163 }
184164
185165 function resolveOpenAIModel(modelId: string): LanguageModel {
186166 return modelId.startsWith("gpt-")
187- ? directOpenAI().responses(modelId)
188- : directOpenAI()(modelId);
167+ ? directProviders.openai().responses(modelId)
168+ : directProviders.openai()(modelId);
189169 }
190170
191171 const PROVIDER_MODEL_RESOLVERS: Readonly<
192172 Record<ProviderName, (modelId: string) => LanguageModel>
193173 > = {
194174 zen: resolveZenModel,
195- anthropic: (modelId) => directAnthropic()(modelId),
175+ anthropic: (modelId) => directProviders.anthropic()(modelId),
196176 openai: resolveOpenAIModel,
197- google: (modelId) => directGoogle()(modelId),
198- ollama: (modelId) => ollamaProvider().chatModel(modelId),
177+ google: (modelId) => directProviders.google()(modelId),
178+ ollama: (modelId) => directProviders.ollama().chatModel(modelId),
199179 };
200180
201181 function isProviderName(provider: string): provider is ProviderName {