Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions packages/server/src/handlers/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export const GenerateHandler = HttpApiBuilder.group(Api, "server.generate", (han
const services = locations.get(Location.Ref.make({ directory: AbsolutePath.make(global.config) }))
const text = Effect.fn("server.generate.text")(
function* (request: { readonly payload: { readonly prompt: string; readonly model?: Model.Ref } }) {
yield* awaitPlugins
yield* awaitPlugins()
const generate = yield* Generate.Service
return { data: { text: yield* generate.text(request.payload).pipe(Effect.mapError(generateError)) } }
},
Expand All @@ -33,7 +33,7 @@ export const GenerateHandler = HttpApiBuilder.group(Api, "server.generate", (han
)
const image = Effect.fn("server.generate.image")(
function* (request: { readonly payload: ImageRequest }) {
yield* awaitPlugins
yield* awaitPlugins()
const generate = yield* Generate.Service
return { data: yield* generate.image(request.payload).pipe(Effect.mapError(generateError)) }
},
Expand All @@ -48,7 +48,7 @@ export const GenerateHandler = HttpApiBuilder.group(Api, "server.generate", (han
message: "Decision question names must be unique",
field: "questions",
})
yield* awaitPlugins
yield* awaitPlugins()
const generate = yield* Generate.Service
return { data: yield* generate.decisions(request.payload).pipe(Effect.mapError(generateError)) }
},
Expand Down
4 changes: 2 additions & 2 deletions packages/server/src/handlers/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ export const ModelHandler = HttpApiBuilder.group(Api, "server.model", (handlers)
.handle(
"model.list",
Effect.fn(function* (ctx) {
yield* awaitPlugins
yield* awaitPlugins()
const models = yield* Model.Service
return yield* response(models.available(ctx.query.types))
}),
)
.handle(
"model.default",
Effect.fn(function* () {
yield* awaitPlugins
yield* awaitPlugins()
const models = yield* Model.Service
return yield* response(models.default())
}),
Expand Down
2 changes: 1 addition & 1 deletion packages/server/src/handlers/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export const ProviderHandler = HttpApiBuilder.group(Api, "server.provider", (han
.handle(
"provider.list",
Effect.fn(function* () {
yield* awaitPlugins
yield* awaitPlugins()
const providers = yield* Provider.Service
return yield* response(providers.available())
}),
Expand Down
4 changes: 2 additions & 2 deletions packages/server/src/handlers/voice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export const VoiceHandler = HttpApiBuilder.group(Api, "server.voice", (handlers)
"voice.models",
Effect.fn("server.voice.models")(
function* () {
yield* awaitPlugins
yield* awaitPlugins()
const voice = yield* Voice.Service
return { data: yield* voice.models() }
},
Expand All @@ -31,7 +31,7 @@ export const VoiceHandler = HttpApiBuilder.group(Api, "server.voice", (handlers)
"voice.session",
Effect.fn("server.voice.session")(
function* (request) {
yield* awaitPlugins
yield* awaitPlugins()
const voice = yield* Voice.Service
const transport = yield* voice
.session(request.payload)
Expand Down
2 changes: 1 addition & 1 deletion packages/server/src/handlers/websearch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export const WebSearchHandler = HttpApiBuilder.group(Api, "server.websearch", (h
"websearch.query",
Effect.fn("server.websearch.query")(function* (request) {
// A cold Location must finish activating plugins before their providers can be resolved.
yield* awaitPlugins
yield* awaitPlugins()
const websearch = yield* WebSearch.Service
return yield* response(
websearch.query(request.payload).pipe(
Expand Down
55 changes: 43 additions & 12 deletions packages/server/src/location.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,49 @@ export function locationErrors<A, E, R>(effect: Effect.Effect<A, E, R>) {
// Temporary: managed policy is applied by the last internal plugin, and State shows each registration as soon as it is
// made, so until activation finishes a provider the policy denies is still listed and usable. Only the endpoints that
// list or use providers wait. Remove once a plugin batch becomes visible only when it ends.
export const awaitPlugins = Plugin.awaitActivation.pipe(
Effect.timeoutOrElse({
duration: "5 seconds",
orElse: () =>
Effect.fail(
new ServiceUnavailableError({
message: "Location plugins did not finish activating",
service: "plugin",
}),
),
}),
)

const DEFAULT_PLUGIN_ACTIVATION_TIMEOUT_MS = 5_000

const DURATION_UNITS: Record<string, number> = {
ms: 1,
millisecond: 1,
milliseconds: 1,
s: 1_000,
sec: 1_000,
secs: 1_000,
second: 1_000,
seconds: 1_000,
m: 60_000,
min: 60_000,
mins: 60_000,
minute: 60_000,
minutes: 60_000,
}

export function pluginActivationTimeoutMs() {
const raw = process.env.OPENCODE_PLUGIN_ACTIVATION_TIMEOUT
if (!raw) return DEFAULT_PLUGIN_ACTIVATION_TIMEOUT_MS
const match = /^\s*(\d+(?:\.\d+)?)\s*([a-z]+)?\s*$/i.exec(raw)
const scale = DURATION_UNITS[(match?.[2] ?? "ms").toLowerCase()]
const value = match && scale !== undefined ? Number(match[1]) * scale : undefined
// An unreadable or non-positive value keeps the default rather than failing every request.
return value !== undefined && Number.isFinite(value) && value > 0 ? value : DEFAULT_PLUGIN_ACTIVATION_TIMEOUT_MS
}

// Read per request so a server that inherits the variable from its parent picks it up without a restart.
export const awaitPlugins = () =>
Plugin.awaitActivation.pipe(
Effect.timeoutOrElse({
duration: pluginActivationTimeoutMs(),
orElse: () =>
Effect.fail(
new ServiceUnavailableError({
message: "Location plugins did not finish activating",
service: "plugin",
}),
),
}),
)

export function response<A, E, R>(data: Effect.Effect<A, E, R>) {
return Effect.gen(function* () {
Expand Down
36 changes: 36 additions & 0 deletions packages/server/test/plugin-activation-timeout.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { afterEach, describe, expect, it } from "bun:test"
import { pluginActivationTimeoutMs } from "../src/location"

const VARIABLE = "OPENCODE_PLUGIN_ACTIVATION_TIMEOUT"
const original = process.env[VARIABLE]

afterEach(() => {
if (original === undefined) delete process.env[VARIABLE]
else process.env[VARIABLE] = original
})

describe("pluginActivationTimeoutMs", () => {
it("defaults to five seconds when the variable is unset", () => {
delete process.env[VARIABLE]
expect(pluginActivationTimeoutMs()).toBe(5_000)
})

it.each([
["30 seconds", 30_000],
["10s", 10_000],
["500ms", 500],
["2 minutes", 120_000],
["750", 750],
] as const)("parses %p as milliseconds", (raw, expected) => {
process.env[VARIABLE] = raw
expect(pluginActivationTimeoutMs()).toBe(expected)
})

it.each([["soon"], ["0 seconds"], ["-5 seconds"], ["5 fortune cookies"]] as const)(
"keeps the default for the unreadable value %p",
(raw) => {
process.env[VARIABLE] = raw
expect(pluginActivationTimeoutMs()).toBe(5_000)
},
)
})
16 changes: 16 additions & 0 deletions services/site/src/docs/content/cli/config.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,22 @@ OPENCODE_CLI_CONFIG_CONTENT='{"tabs":{"mode":"off"}}' opencode
OpenCode merges the inline settings over the global `cli.json`. Nested objects are merged, while arrays and scalar
values from the environment replace global values. Inline settings remain authoritative while the variable is set.

## Plugin activation timeout

Endpoints that need the model catalog wait for a location's plugins to activate, and answer `503` when activation
exceeds the budget. A `--standalone` call starts a private server, so every call pays the cold activation cost, and
slow plugin setups can exceed the default budget of five seconds.

Set `OPENCODE_PLUGIN_ACTIVATION_TIMEOUT` to raise the budget:

```sh
OPENCODE_PLUGIN_ACTIVATION_TIMEOUT="30 seconds" opencode serve
```

The value is a duration such as `500ms`, `10 seconds`, or `2 minutes`; a bare number counts as milliseconds. The
variable is read per request, so a server that inherits it picks it up without a restart. An unreadable or
non-positive value keeps the five-second default.

## Appearance
Choose a theme, control interface motion, and override the terminal cursor:

Expand Down
Loading