|
1 | 1 | import { define } from "@opencode/plugin/effect/plugin" |
2 | 2 | import type { Entry } from "@opencode/schema/config" |
3 | | -import { Duration, Effect, Schedule, type Schema, Semaphore, Stream } from "effect" |
| 3 | +import { Duration, Effect, Option, Schedule, type Schema, Semaphore, Stream } from "effect" |
4 | 4 | import { HttpClient, HttpClientRequest, HttpClientResponse } from "effect/http" |
| 5 | +import { shellParserWasm } from "#shell-parser-wasm" |
5 | 6 | import { Config } from "../../config.js" |
| 7 | +import { Environment } from "../../environment/index.js" |
| 8 | +import { EnvironmentUnavailable } from "../../environment/unavailable.js" |
| 9 | +import { Location } from "../../location.js" |
6 | 10 | import { Model } from "../../model.js" |
7 | 11 | import type { PluginInternal } from "../internal.js" |
8 | 12 | import { foldSettings } from "./configured.js" |
@@ -31,13 +35,22 @@ export function createLocalProviderPlugin<Discovered>(input: { |
31 | 35 | const discoveryLock = Semaphore.makeUnsafe(1) |
32 | 36 | const suffix = input.stripPathSuffix ?? /\/v1$/ |
33 | 37 |
|
34 | | - return function make(origin = input.origin, interval: Duration.Input = "30 seconds") { |
| 38 | + return function make(origin?: string, interval: Duration.Input = "30 seconds") { |
35 | 39 | return define({ |
36 | 40 | id: input.id, |
37 | 41 | effect: Effect.fn(function* (ctx) { |
38 | 42 | const http = HttpClient.filterStatusOk(yield* HttpClient.HttpClient) |
39 | 43 | const config = yield* Config.Service |
40 | | - const source = { current: configured(yield* config.entries(), input.providerID, origin, suffix) } |
| 44 | + const environment = yield* Effect.serviceOption(Environment.Service) |
| 45 | + const location = yield* Effect.serviceOption(Location.Service) |
| 46 | + const defaultOrigin = |
| 47 | + origin ?? |
| 48 | + (shellParserWasm.bash === "" || |
| 49 | + (Option.isSome(location) && location.value.workspaceID !== undefined) || |
| 50 | + (Option.isSome(environment) && environment.value.spawner === EnvironmentUnavailable.spawner) |
| 51 | + ? undefined |
| 52 | + : input.origin) |
| 53 | + const source = { current: configured(yield* config.entries(), input.providerID, defaultOrigin, suffix) } |
41 | 54 | const loaded = { models: [] as readonly Discovered[], hash: "[]" } |
42 | 55 |
|
43 | 56 | yield* ctx.integration.transform((integrations) => { |
@@ -115,7 +128,7 @@ export function createLocalProviderPlugin<Discovered>(input: { |
115 | 128 | // Keep the last successful inventory through transient outages instead of flickering model availability. |
116 | 129 | yield* refresh().pipe(Effect.ignore, Effect.repeat(Schedule.spaced(interval)), Effect.forkScoped) |
117 | 130 | const reload = Effect.fn("LocalProviderPlugin.reload")(function* () { |
118 | | - const next = configured(yield* config.entries(), input.providerID, origin, suffix) |
| 131 | + const next = configured(yield* config.entries(), input.providerID, defaultOrigin, suffix) |
119 | 132 | if ( |
120 | 133 | next.baseURL === source.current.baseURL && |
121 | 134 | next.apiKey === source.current.apiKey && |
@@ -172,10 +185,14 @@ function createClient( |
172 | 185 | } |
173 | 186 | } |
174 | 187 |
|
175 | | -function configured(entries: readonly Entry[], providerID: string, origin: string, suffix: RegExp) { |
| 188 | +function configured(entries: readonly Entry[], providerID: string, origin: string | undefined, suffix: RegExp) { |
176 | 189 | const settings = foldSettings(entries, providerID, undefined) |
177 | 190 | const baseURL = ( |
178 | | - typeof settings?.baseURL === "string" ? settings.baseURL : `${origin.replace(/\/+$/, "")}/v1` |
| 191 | + typeof settings?.baseURL === "string" |
| 192 | + ? settings.baseURL |
| 193 | + : origin !== undefined |
| 194 | + ? `${origin.replace(/\/+$/, "")}/v1` |
| 195 | + : "" |
179 | 196 | ).replace(/\/+$/, "") |
180 | 197 | const apiKey = typeof settings?.apiKey === "string" ? settings.apiKey : undefined |
181 | 198 | if (!URL.canParse(baseURL)) return { baseURL, apiKey } |
|
0 commit comments