Skip to content
2 changes: 1 addition & 1 deletion AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ const table = sqliteTable("session", {
- Temporary exception: endpoints that list or use providers managed policy can deny (`model.list`, `model.default`, `provider.list`, `generate.*`, `voice.*` and `websearch.query`) wait through `awaitPlugins` in `packages/server/src/location.ts`, because policy is applied by the last plugin to activate and is missing until then. Don't add new waits; this goes away once a plugin batch becomes visible only when it ends.
- The server answers with what is loaded now: lists may be empty or partial on a cold Location, and an action whose model, provider or RPC hasn't loaded yet fails with not-found. That is expected, not a bug to fix on the server.
- Clients are responsible for making sure what they need is loaded before they call. Long-lived clients refresh on the matching `*.updated` event (`model.updated`, `agent.updated`, `skill.updated`, `integration.updated`, `plugin.updated`, ...). One-shot clients (CLI commands, ACP, SDK callers) wait on their side, for example by polling `plugin.list` or the entry they need. See `packages/cli/src/services/plugin-startup.ts`.
- Running the hooks that are registered and waiting for their result is fine (for example `hooks.trigger("session", "http.request", ...)`). What is not allowed is waiting for plugins to load first: if the plugin that adds a hook hasn't loaded yet, the call runs without that hook.
- Running the hooks that are registered and waiting for their result is fine (for example `hooks.trigger("model", "experimental.http.request", ...)`). What is not allowed is waiting for plugins to load first: if the plugin that adds a hook hasn't loaded yet, the call runs without that hook.

## Session Core

Expand Down
7 changes: 5 additions & 2 deletions packages/core/src/aisdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -710,13 +710,16 @@ function metadataProviderOptions(input: ProviderMetadata | undefined): SharedV3P
return Object.fromEntries(Object.entries(input).map(([key, value]) => [key, jsonObject(value)]))
}

function streamLanguage(language: LanguageModelV3, options: LanguageModelV3CallOptions, http?: HttpMiddleware) {
function streamLanguage(language: LanguageModelV3, options: LanguageModelV3CallOptions, perCall?: HttpMiddleware) {
const state: StreamState = { step: 0, toolNames: {}, open: {} }
return Stream.concat(
Stream.make(LLMEvent.stepStart({ index: state.step })),
Stream.unwrap(
Effect.gen(function* () {
const context = yield* Effect.context<never>()
// The SDK sends through its own fetch, not the executor, so scoped middleware is applied here, outside the
// per-call one, and cleared for the hooks so a request a hook makes isn't wrapped again.
const http = RequestExecutor.composeMiddleware(yield* RequestExecutor.CurrentMiddleware, perCall)
const context = Context.add(yield* Effect.context<never>(), RequestExecutor.CurrentMiddleware, undefined)
return yield* Effect.tryPromise({
try: () =>
http ? httpMiddleware.run({ http, context }, () => language.doStream(options)) : language.doStream(options),
Expand Down
9 changes: 8 additions & 1 deletion packages/core/src/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import { llmClient, requestExecutor } from "./effect/app-node-platform.js"
import { Integration } from "./integration.js"
import { ModelResolver } from "./model-resolver.js"
import { Model } from "./model.js"
import { ModelHttp } from "./model-http.js"
import { PluginHooks } from "./plugin/hooks.js"
import { ModelRetry } from "./model-retry.js"

export interface TextInput {
Expand Down Expand Up @@ -129,6 +131,7 @@ export const layer = Layer.effect(
const images = yield* ImageClient.Service
const decisionClient = yield* DecisionClient.Service
const videos = yield* VideoClient.Service
const hooks = yield* PluginHooks.Service

const runText = Effect.fn("Generate.text")(function* (input: TextInput) {
const resolved = yield* resolver
Expand Down Expand Up @@ -164,6 +167,7 @@ export const layer = Layer.effect(
}),
)
.pipe(
ModelHttp.scoped(hooks, { model: resolved.ref, kind: "text" }),
ModelRetry.retryOneShot,
Effect.mapError((error) => providerError(resolved.ref, error)),
)
Expand Down Expand Up @@ -206,6 +210,7 @@ export const layer = Layer.effect(
})
.pipe(
Effect.provideService(ImageClient.Service, images),
ModelHttp.scoped(hooks, { model: input.model, kind: "image" }),
ModelRetry.retryOneShot,
Effect.mapError((error) => providerError(input.model, error)),
)
Expand Down Expand Up @@ -247,6 +252,7 @@ export const layer = Layer.effect(
})
.pipe(
Effect.provideService(DecisionClient.Service, decisionClient),
ModelHttp.scoped(hooks, { model: input.model, kind: "decision" }),
ModelRetry.retryOneShot,
Effect.mapError((error) => providerError(input.model, error)),
)
Expand All @@ -271,6 +277,7 @@ export const layer = Layer.effect(
if (!model) return yield* missing(ref, "video")
return yield* run(model).pipe(
Effect.provideService(VideoClient.Service, videos),
ModelHttp.scoped(hooks, { model: ref, kind: "video" }),
Effect.mapError((error) => providerError(ref, error)),
)
})
Expand Down Expand Up @@ -374,5 +381,5 @@ function failureReason(tag: AIError["reason"]["_tag"], status: number | undefine
export const node = makeLocationNode({
service: Service,
layer,
deps: [ModelResolver.node, Model.node, llmClient, requestExecutor],
deps: [ModelResolver.node, Model.node, PluginHooks.node, llmClient, requestExecutor],
})
72 changes: 72 additions & 0 deletions packages/core/src/model-http.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
export * as ModelHttp from "./model-http.js"

import { RequestExecutor, type HttpMiddleware } from "@opencode/ai/route"
import type { ModelRequestScope } from "@opencode/plugin/effect/model"
import { Effect, Stream } from "effect"
import { HttpClientRequest, HttpClientResponse } from "effect/http"
import type { PluginHooks } from "./plugin/hooks.js"

/**
* Per-call middleware that runs the model `experimental.http.*` hooks, and the session `http.*` hooks when a Session
* makes the request. Model hooks wrap session hooks. Undefined when no hook applies.
*/
export const middleware = Effect.fnUntraced(function* (hooks: PluginHooks.Interface, scope: ModelRequestScope) {
const providerID = scope.model.providerID
// The session hooks' own event shape, for requests a Session makes.
const session = scope.session && {
sessionID: scope.session.id,
agent: scope.session.agent,
model: scope.model,
kind: scope.kind,
}
const hasHooks =
(yield* hooks.has("model", "experimental.http.request", providerID)) ||
(yield* hooks.has("model", "experimental.http.response", providerID)) ||
(session !== undefined &&
((yield* hooks.has("session", "http.request", providerID)) ||
(yield* hooks.has("session", "http.response", providerID))))
if (!hasHooks) return undefined
const http: HttpMiddleware = (req, handler) =>
Effect.gen(function* () {
const request = yield* HttpClientRequest.toWeb(req)
const outer = yield* hooks.trigger("model", "experimental.http.request", { ...scope, request })
const before = session
? yield* hooks.trigger("session", "http.request", { ...session, request: outer.request })
: outer
// A hook that kept the request object cannot have replaced its body, so the original body is sent as is.
// Reading it back would hold a second full copy of the request, screenshots included, for the whole call.
const sent =
before.request === request
? HttpClientRequest.setBody(HttpClientRequest.fromWeb(request), req.body)
: before.request.body
? HttpClientRequest.bodyUint8Array(
HttpClientRequest.fromWeb(before.request),
new Uint8Array(yield* Effect.promise(() => before.request.clone().arrayBuffer())),
before.request.headers.get("content-type") ?? undefined,
)
: HttpClientRequest.fromWeb(before.request)
const res = yield* handler(sent)
const response = new Response(
[204, 205, 304].includes(res.status) ? null : yield* Stream.toReadableStreamEffect(res.stream),
{ status: res.status, headers: res.headers },
)
const inner = session
? yield* hooks.trigger("session", "http.response", { ...session, request: before.request, response })
: { response }
const after = yield* hooks.trigger("model", "experimental.http.response", {
...scope,
request: before.request,
response: inner.response,
})
return HttpClientResponse.fromWeb(sent, after.response)
})
return http
})

/** Runs `effect` with the model hooks on every provider request it makes, its polls and provider downloads included. */
export const scoped =
(hooks: PluginHooks.Interface, scope: ModelRequestScope) =>
<A, E, R>(effect: Effect.Effect<A, E, R>) =>
middleware(hooks, scope).pipe(
Effect.flatMap((http) => (http ? effect.pipe(RequestExecutor.withMiddleware(http)) : effect)),
)
3 changes: 3 additions & 0 deletions packages/core/src/plugin/hooks.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export * as PluginHooks from "./hooks.js"

import type { AISDKHooks } from "@opencode/plugin/effect/aisdk"
import type { ModelRequestHooks } from "@opencode/plugin/effect/model"
import type { SessionHooks } from "@opencode/plugin/effect/session"
import type { ShellHooks } from "@opencode/plugin/effect/shell"
import type { ToolFailures, ToolHooks } from "@opencode/plugin/effect/tool"
Expand All @@ -12,6 +13,7 @@ import { State } from "../state.js"

export interface Domains {
readonly aisdk: AISDKHooks
readonly model: ModelRequestHooks
readonly session: SessionHooks
readonly permission: PermissionHooks
readonly shell: ShellHooks
Expand All @@ -23,6 +25,7 @@ type NoFailures<Spec> = { readonly [Name in keyof Spec]: never }
// Failure channel for each hook event. Only tool execute.before may fail: a Tool.Error rejects the call before it runs.
interface Failures extends Record<keyof Domains, unknown> {
readonly aisdk: NoFailures<AISDKHooks>
readonly model: NoFailures<ModelRequestHooks>
readonly session: NoFailures<SessionHooks>
readonly permission: NoFailures<PermissionHooks>
readonly shell: NoFailures<ShellHooks>
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/plugin/host.ts
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@ export const make = Effect.fn("PluginHost.make")(function* (plugin: Pick<Interfa
}),
},
model: {
hook: (name, callback, options) => hooks.register("model", name, callback, options),
list: (input) => response(models.available(input?.types)),
default: () => response(models.default()),
reload: models.reload,
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/plugin/provider/azure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -444,8 +444,8 @@ export function make(
const current = yield* token(scope).pipe(Effect.orDie)
return `Bearer ${current.access}`
})
yield* ctx.session.hook(
"http.request",
yield* ctx.model.hook(
"experimental.http.request",
(evt) =>
Effect.gen(function* () {
const authorization = yield* bearer(evt.request.url)
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/plugin/provider/github-copilot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -415,8 +415,8 @@ export const GithubCopilotPlugin = define({
}),
{ providerID: Provider.ID.githubCopilot },
)
yield* ctx.session.hook(
"http.request",
yield* ctx.model.hook(
"experimental.http.request",
(evt) =>
Effect.gen(function* () {
// The Messages route sends the token as `x-api-key`, the OpenAI routes as a bearer token.
Expand Down
6 changes: 3 additions & 3 deletions packages/core/src/plugin/provider/opencode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { isDeepStrictEqual } from "node:util"
import { Duration, Effect, Equal, FiberHandle, Option, Schema, Scope, Semaphore, Stream } from "effect"
import type { IntegrationOAuthMethodRegistration } from "@opencode/plugin/effect/integration"
import { define } from "@opencode/plugin/effect/plugin"
import type { SessionHttpResponse } from "@opencode/plugin/effect/session"
import type { ModelHttpResponse } from "@opencode/plugin/effect/model"
import {
FetchHttpClient,
HttpBody,
Expand Down Expand Up @@ -541,7 +541,7 @@ export const OpencodePlugin = define<
// Console inference rejects a missing SSO sign-in with a generic 401/403. Re-check the Console config, which
// reports the reason, and replace the response with instructions when the connection needs the user.
// A client retrying rejected inference would otherwise send one config request per failed response.
const explain = (event: SessionHttpResponse) =>
const explain = (event: ModelHttpResponse) =>
Effect.gen(function* () {
if (event.response.status !== 401 && event.response.status !== 403) return
yield* check(Duration.seconds(30))
Expand All @@ -563,7 +563,7 @@ export const OpencodePlugin = define<
yield* Effect.forEach(explainers, (registration) => registration.dispose, { discard: true })
explainers = yield* Effect.forEach(
next.connection ? new Set([Provider.ID.opencode, ...Object.keys(next.remote?.providers ?? {})]) : [],
(providerID) => ctx.session.hook("http.response", explain, { providerID }).pipe(Scope.provide(scope)),
(providerID) => ctx.model.hook("experimental.http.response", explain, { providerID }).pipe(Scope.provide(scope)),
)
})
yield* guard(snapshot)
Expand Down
8 changes: 4 additions & 4 deletions packages/core/src/plugin/provider/snowflake-cortex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,8 +203,8 @@ export const SnowflakeCortexPlugin = define({
}
})
yield* ProviderRefresh.onSwitch(integrationID, ProviderRefresh.reloadIfChanged(load, ctx.provider.reload()))
yield* ctx.session.hook(
"http.request",
yield* ctx.model.hook(
"experimental.http.request",
(event) =>
Effect.sync(() => {
// Model resolution already supplies and refreshes stored credentials on each attempt.
Expand All @@ -214,8 +214,8 @@ export const SnowflakeCortexPlugin = define({
}),
{ providerID },
)
yield* ctx.session.hook(
"http.response",
yield* ctx.model.hook(
"experimental.http.response",
Effect.fn(function* (event) {
if (event.response.status !== 400) return
const error = Option.getOrUndefined(decodeError(yield* Effect.promise(() => event.response.clone().text())))
Expand Down
41 changes: 7 additions & 34 deletions packages/core/src/session/model-request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ import type {
import type { Agent } from "@opencode/schema/agent"
import type { Model } from "@opencode/schema/model"
import type { Content } from "@opencode/schema/tool"
import { Cause, Context, Effect, Layer, Result, Stream } from "effect"
import { HttpClientRequest, HttpClientResponse } from "effect/http"
import { Cause, Context, Effect, Layer, Result } from "effect"
import { makeLocationNode } from "@opencode/util/effect/app-node"
import { App } from "../app.js"
import { ModelHttp } from "../model-http.js"
import { Instructions } from "../instructions/index.js"
import { Permission } from "../permission.js"
import { PluginHooks } from "../plugin/hooks.js"
Expand Down Expand Up @@ -372,38 +372,11 @@ export const layer = Layer.effect(
model: route === base.model.route ? base.model : LanguageModel.update(base.model, { route }),
headers: Object.keys(modelHook.headers).length === 0 ? undefined : modelHook.headers,
}
const hasHttpHooks =
(yield* hooks.has("session", "http.request", model.ref.providerID)) ||
(yield* hooks.has("session", "http.response", model.ref.providerID))
const http: StreamOptions["http"] = hasHttpHooks
? (req, handler) =>
Effect.gen(function* () {
const request = yield* HttpClientRequest.toWeb(req)
const before = yield* hooks.trigger("session", "http.request", { ...scope, request })
// A hook that kept the request object cannot have replaced its body, so the original body is sent as is.
// Reading it back would hold a second full copy of the request, screenshots included, for the whole call.
const sent =
before.request === request
? HttpClientRequest.setBody(HttpClientRequest.fromWeb(request), req.body)
: before.request.body
? HttpClientRequest.bodyUint8Array(
HttpClientRequest.fromWeb(before.request),
new Uint8Array(yield* Effect.promise(() => before.request.clone().arrayBuffer())),
before.request.headers.get("content-type") ?? undefined,
)
: HttpClientRequest.fromWeb(before.request)
const res = yield* handler(sent)
const after = yield* hooks.trigger("session", "http.response", {
...scope,
request: before.request,
response: new Response(
[204, 205, 304].includes(res.status) ? null : yield* Stream.toReadableStreamEffect(res.stream),
{ status: res.status, headers: res.headers },
),
})
return HttpClientResponse.fromWeb(sent, after.response)
})
: undefined
const http = yield* ModelHttp.middleware(hooks, {
model: model.ref,
kind,
session: { id: session.id, agent: input.agent },
})
// HTTP hooks wrap every HTTP request, including the WebSocket fallback path. The route decides
// which transport actually carries the request, so both hook families are always offered.
const webSocket =
Expand Down
11 changes: 10 additions & 1 deletion packages/core/src/voice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,14 @@ import {
} from "@opencode/ai/experimental"
import { RequestExecutor } from "@opencode/ai/route"
import { makeLocationNode } from "@opencode/util/effect/app-node"
import { Model } from "@opencode/schema/model"
import { Provider } from "@opencode/schema/provider"
import { Context, Effect, Layer, Redacted, Schema } from "effect"
import { Credential } from "./credential.js"
import { requestExecutor } from "./effect/app-node-platform.js"
import { Integration } from "./integration.js"
import { ModelHttp } from "./model-http.js"
import { PluginHooks } from "./plugin/hooks.js"

export interface ModelInfo {
readonly model: string
Expand Down Expand Up @@ -56,6 +60,7 @@ export const layer = Layer.effect(
Effect.gen(function* () {
const executor = yield* RequestExecutor.Service
const integrations = yield* Integration.Service
const hooks = yield* PluginHooks.Service

// Integration lists the active credential first, then newer stored credentials, then non-empty env vars.
const findConnection = (provider: string, methods: readonly string[] = ["key"]) =>
Expand Down Expand Up @@ -114,6 +119,10 @@ export const layer = Layer.effect(
...access,
}).pipe(
Effect.provideService(RequestExecutor.Service, executor),
ModelHttp.scoped(hooks, {
model: Model.Ref.make({ providerID: Provider.ID.make(found.provider), id: Model.ID.make(input.model) }),
kind: "voice",
}),
Effect.mapError(
(error: AIError): Error =>
error.reason._tag === "InvalidRequest" || error.reason._tag === "UnsupportedOperation"
Expand All @@ -130,7 +139,7 @@ export const layer = Layer.effect(
export const node = makeLocationNode({
service: Service,
layer,
deps: [Integration.node, requestExecutor],
deps: [Integration.node, PluginHooks.node, requestExecutor],
})

/** OpenCode hosts GPT-Live on its OpenAI account, billed to the workspace whose credential starts a session. */
Expand Down
Loading
Loading