MCPcopy Create free account
hub / github.com/tiann/hapi / listOpencodeModelsForCwd

Function listOpencodeModelsForCwd

cli/src/modules/common/opencodeModels.ts:136–176  ·  view source on GitHub ↗
(
    cwd: string
)

Source from the content-addressed store, hash-verified

134 * one probe at a time per cwd.
135 */
136export async function listOpencodeModelsForCwd(
137 cwd: string
138): Promise<ListOpencodeModelsForCwdResponse> {
139 const trimmed = cwd?.trim();
140 if (!trimmed) {
141 return { success: false, error: 'cwd is required' };
142 }
143
144 const cached = cache.get(trimmed);
145 if (cached && cached.expiresAt > Date.now()) {
146 return cached.response;
147 }
148
149 const existing = inflight.get(trimmed);
150 if (existing) {
151 return existing;
152 }
153
154 const promise = (async () => {
155 try {
156 const response = await runOpencodeProbe(trimmed);
157 if (response.success) {
158 cache.set(trimmed, {
159 expiresAt: Date.now() + CACHE_TTL_MS,
160 response
161 });
162 }
163 return response;
164 } catch (error) {
165 return {
166 success: false,
167 error: getErrorMessage(error, 'Failed to discover OpenCode models')
168 } satisfies ListOpencodeModelsForCwdResponse;
169 } finally {
170 inflight.delete(trimmed);
171 }
172 })();
173
174 inflight.set(trimmed, promise);
175 return promise;
176}
177
178/**
179 * Clear the in-process cache. Exposed for tests.

Callers 3

constructorMethod · 0.90

Calls 3

getErrorMessageFunction · 0.90
runOpencodeProbeFunction · 0.85
getMethod · 0.80

Tested by

no test coverage detected