Skip to content

Commit 1038fc2

Browse files
committed
refactor(provider): generalize model discovery loop for all providers
Replace the hardcoded GitLab discovery block with a generic loop over all discoveryLoaders entries. Refactor Maple to use the existing discoverModels hook instead of inline fetching.
1 parent 02d1bb2 commit 1038fc2

1 file changed

Lines changed: 47 additions & 55 deletions

File tree

packages/opencode/src/provider/provider.ts

Lines changed: 47 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -813,7 +813,7 @@ function custom(dep: CustomDep): Record<string, CustomLoader> {
813813
},
814814
},
815815
}),
816-
maple: Effect.fnUntraced(function* (input: Info) {
816+
maple: Effect.fnUntraced(function* () {
817817
const cfg = yield* dep.config()
818818
const env = yield* dep.env()
819819
const baseURL = cfg.provider?.["maple"]?.options?.baseURL ?? "http://127.0.0.1:8080/v1"
@@ -823,59 +823,49 @@ function custom(dep: CustomDep): Record<string, CustomLoader> {
823823

824824
if (!apiKey) return { autoload: false }
825825

826-
const models = yield* Effect.tryPromise({
827-
try: async () => {
826+
return {
827+
autoload: true,
828+
options: {
829+
baseURL,
830+
apiKey,
831+
},
832+
async discoverModels(): Promise<Record<string, Model>> {
828833
const response = await fetch(`${baseURL}/models`, {
829834
headers: { Authorization: `Bearer ${apiKey}` },
830835
signal: AbortSignal.timeout(5000),
831836
})
832-
if (!response.ok) {
833-
log.warn("Failed to fetch Maple models", { status: response.status })
834-
return []
837+
if (!response.ok) return {}
838+
const data = (await response.json()) as { data?: Array<{ id: string }> }
839+
const models: Record<string, Model> = {}
840+
for (const m of data.data ?? []) {
841+
models[m.id] = {
842+
id: ModelID.make(m.id),
843+
providerID: ProviderID.make("maple"),
844+
name: m.id,
845+
api: {
846+
id: m.id,
847+
url: baseURL,
848+
npm: "@ai-sdk/openai-compatible",
849+
},
850+
status: "active",
851+
headers: {},
852+
options: {},
853+
cost: { input: 0, output: 0, cache: { read: 0, write: 0 } },
854+
limit: { context: 128000, output: 8192 },
855+
capabilities: {
856+
temperature: true,
857+
reasoning: false,
858+
attachment: false,
859+
toolcall: true,
860+
input: { text: true, audio: false, image: false, video: false, pdf: false },
861+
output: { text: true, audio: false, image: false, video: false, pdf: false },
862+
interleaved: false,
863+
},
864+
release_date: "",
865+
variants: {},
866+
}
835867
}
836-
const data = await response.json() as { data?: Array<{ id: string }> }
837-
return data.data ?? []
838-
},
839-
catch: (e) => {
840-
log.warn("Failed to connect to Maple proxy", { error: e })
841-
return new Error(String(e))
842-
},
843-
}).pipe(Effect.orElseSucceed(() => [] as Array<{ id: string }>))
844-
845-
for (const model of models) {
846-
input.models[model.id] = {
847-
id: ModelID.make(model.id),
848-
providerID: ProviderID.make("maple"),
849-
name: model.id,
850-
api: {
851-
id: model.id,
852-
url: baseURL,
853-
npm: "@ai-sdk/openai-compatible",
854-
},
855-
status: "active",
856-
headers: {},
857-
options: {},
858-
cost: { input: 0, output: 0, cache: { read: 0, write: 0 } },
859-
limit: { context: 128000, output: 8192 },
860-
capabilities: {
861-
temperature: true,
862-
reasoning: false,
863-
attachment: false,
864-
toolcall: true,
865-
input: { text: true, audio: false, image: false, video: false, pdf: false },
866-
output: { text: true, audio: false, image: false, video: false, pdf: false },
867-
interleaved: false,
868-
},
869-
release_date: "",
870-
variants: {},
871-
}
872-
}
873-
874-
return {
875-
autoload: Object.keys(input.models).length > 0,
876-
options: {
877-
baseURL,
878-
apiKey,
868+
return models
879869
},
880870
}
881871
}),
@@ -1360,18 +1350,20 @@ const layer: Layer.Layer<
13601350
mergeProvider(providerID, partial)
13611351
}
13621352

1363-
const gitlab = ProviderID.make("gitlab")
1364-
if (discoveryLoaders[gitlab] && providers[gitlab] && isProviderAllowed(gitlab)) {
1353+
for (const [id, discover] of Object.entries(discoveryLoaders)) {
1354+
const providerID = ProviderID.make(id)
1355+
if (!providers[providerID] || !isProviderAllowed(providerID)) continue
13651356
yield* Effect.promise(async () => {
13661357
try {
1367-
const discovered = await discoveryLoaders[gitlab]()
1358+
const discovered = await discover()
13681359
for (const [modelID, model] of Object.entries(discovered)) {
1369-
if (!providers[gitlab].models[modelID]) {
1370-
providers[gitlab].models[modelID] = model
1360+
if (!providers[providerID].models[modelID]) {
1361+
model.providerID = providerID
1362+
providers[providerID].models[modelID] = model
13711363
}
13721364
}
13731365
} catch (e) {
1374-
log.warn("state discovery error", { id: "gitlab", error: e })
1366+
log.warn("state discovery error", { id, error: e })
13751367
}
13761368
})
13771369
}

0 commit comments

Comments
 (0)