refactor(core): run session HTTP hooks inside model HTTP hooks

This commit is contained in:
Aiden Cline 2026-11-11 15:19:57 -06:00
parent 71a220e0c6
commit 24bd1663a7
6 changed files with 41 additions and 79 deletions

View file

@ -7,19 +7,32 @@ import { HttpClientRequest, HttpClientResponse } from "effect/http"
import type { PluginHooks } from "./plugin/hooks.js"
/**
* Per-call middleware that runs the model `experimental.http.*` hooks, which include the session `http.*` hooks.
* Undefined when no hook applies.
* 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 any =
(yield* hooks.has("model", "experimental.http.request", providerID)) ||
(yield* hooks.has("model", "experimental.http.response", 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 (!any) return undefined
const http: HttpMiddleware = (req, handler) =>
Effect.gen(function* () {
const request = yield* HttpClientRequest.toWeb(req)
const before = yield* hooks.trigger("model", "experimental.http.request", { ...scope, request })
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 =
@ -37,10 +50,13 @@ export const middleware = Effect.fnUntraced(function* (hooks: PluginHooks.Interf
[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,
response: inner.response,
})
return HttpClientResponse.fromWeb(sent, after.response)
})

View file

@ -1,8 +1,8 @@
export * as PluginHooks from "./hooks.js"
import type { AISDKHooks } from "@opencode/plugin/effect/aisdk"
import type { ModelHttpRequest, ModelHttpResponse, ModelRequestHooks } from "@opencode/plugin/effect/model"
import type { SessionHooks, SessionHttpRequest, SessionHttpResponse } from "@opencode/plugin/effect/session"
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"
import type { ModelHookOptions } from "@opencode/plugin/effect/registration"
@ -42,17 +42,10 @@ const eventProviderID = (event: unknown) => {
return typeof model.providerID === "string" ? model.providerID : undefined
}
type Name<Domain extends keyof Domains> = keyof Domains[Domain] & keyof Failures[Domain]
// The deprecated session HTTP hooks register into the model HTTP hooks, so only the model names are triggered.
type Triggered<Domain extends keyof Domains> = Exclude<
Name<Domain>,
Domain extends "session" ? "http.request" | "http.response" : never
>
export interface Interface {
readonly has: <Domain extends keyof Domains>(
domain: Domain,
name: Triggered<Domain>,
name: keyof Domains[Domain] & keyof Failures[Domain],
providerID?: string,
) => Effect.Effect<boolean>
readonly register: <Domain extends keyof Domains, Name extends keyof Domains[Domain] & keyof Failures[Domain]>(
@ -61,11 +54,11 @@ export interface Interface {
callback: Callback<Domains[Domain][Name], Failures[Domain][Name]>,
options?: ModelHookOptions,
) => Effect.Effect<State.Registration, never, Scope.Scope>
readonly trigger: <Domain extends keyof Domains, Hook extends Triggered<Domain>>(
readonly trigger: <Domain extends keyof Domains, Name extends keyof Domains[Domain] & keyof Failures[Domain]>(
domain: Domain,
name: Hook,
event: Domains[Domain][Hook],
) => Effect.Effect<Domains[Domain][Hook], Failures[Domain][Hook]>
name: Name,
event: Domains[Domain][Name],
) => Effect.Effect<Domains[Domain][Name], Failures[Domain][Name]>
}
export class Service extends Context.Service<Service, Interface>()("@opencode/PluginHooks") {}
@ -79,16 +72,9 @@ const layer = Layer.effect(
const register: Interface["register"] = Effect.fn("PluginHooks.register")(
function* (domain, name, callback, options) {
const scope = yield* Scope.Scope
// The session HTTP hooks are model HTTP hooks that skip calls made outside a Session.
const legacy =
domain === "session" && name === "http.request"
? sessionHttp.request
: domain === "session" && name === "http.response"
? sessionHttp.response
: undefined
const id = legacy ? legacy.id : key(domain, name)
const id = key(domain, name)
let active = true
const entry = { callback: legacy ? legacy.adapt(callback) : callback, options }
const entry = { callback, options }
callbacks.set(id, [...(callbacks.get(id) ?? []), entry])
const dispose = Effect.sync(() => {
if (!active) return
@ -122,42 +108,4 @@ const layer = Layer.effect(
}),
)
const sessionHttp = {
request: { id: "model.experimental.http.request", adapt: sessionRequest },
response: { id: "model.experimental.http.response", adapt: sessionResponse },
}
function sessionRequest(callback: Function) {
return (event: ModelHttpRequest): Effect.Effect<void> => {
if (event.session === undefined) return Effect.void
const legacy: SessionHttpRequest = {
sessionID: event.session.id,
agent: event.session.agent,
model: event.model,
kind: event.kind,
request: event.request,
}
return Effect.map(callback(legacy), () => {
event.request = legacy.request
})
}
}
function sessionResponse(callback: Function) {
return (event: ModelHttpResponse): Effect.Effect<void> => {
if (event.session === undefined) return Effect.void
const legacy: SessionHttpResponse = {
sessionID: event.session.id,
agent: event.session.agent,
model: event.model,
kind: event.kind,
request: event.request,
response: event.response,
}
return Effect.map(callback(legacy), () => {
event.response = legacy.response
})
}
}
export const node = makeLocationNode({ service: Service, layer, deps: [] })

View file

@ -206,14 +206,13 @@ export interface SessionHooks {
readonly "experimental.model.resolve": SessionModelResolve
readonly "model.request": SessionModelRequest
/**
* Runs for HTTP requests a Session makes. Registered as a `ctx.model.hook("experimental.http.request")` that skips
* calls without a Session. Prefer the model hook, which also sees Generate calls; `event.session` holds the Session.
* Runs for HTTP requests a Session makes, inside `ctx.model.hook("experimental.http.request")`. Prefer the model hook,
* which also sees Generate calls; `event.session` holds the Session.
*/
readonly "http.request": SessionHttpRequest
/**
* Runs for HTTP responses to a Session's requests. Registered as a `ctx.model.hook("experimental.http.response")`
* that skips calls without a Session. Prefer the model hook, which also sees Generate calls; `event.session` holds the
* Session.
* Runs for HTTP responses to a Session's requests, before `ctx.model.hook("experimental.http.response")`. Prefer the
* model hook, which also sees Generate calls; `event.session` holds the Session.
*/
readonly "http.response": SessionHttpResponse
readonly "experimental.ws.handshake": SessionWebSocketHandshake

View file

@ -206,14 +206,13 @@ export interface SessionHooks {
readonly "experimental.model.resolve": SessionModelResolve
readonly "model.request": SessionModelRequest
/**
* Runs for HTTP requests a Session makes. Registered as a `ctx.model.hook("experimental.http.request")` that skips
* calls without a Session. Prefer the model hook, which also sees Generate calls; `event.session` holds the Session.
* Runs for HTTP requests a Session makes, inside `ctx.model.hook("experimental.http.request")`. Prefer the model hook,
* which also sees Generate calls; `event.session` holds the Session.
*/
readonly "http.request": SessionHttpRequest
/**
* Runs for HTTP responses to a Session's requests. Registered as a `ctx.model.hook("experimental.http.response")`
* that skips calls without a Session. Prefer the model hook, which also sees Generate calls; `event.session` holds the
* Session.
* Runs for HTTP responses to a Session's requests, before `ctx.model.hook("experimental.http.response")`. Prefer the
* model hook, which also sees Generate calls; `event.session` holds the Session.
*/
readonly "http.response": SessionHttpResponse
readonly "experimental.ws.handshake": SessionWebSocketHandshake

View file

@ -1311,7 +1311,7 @@ Modify native provider requests or responses with the experimental model HTTP ho
to a model provider, including Generate text, image and decision calls made outside a Session. Bodies are one-shot
streams; clone or replace a body before reading it. `event.kind` says why the call was made, and `event.session` is set
for a Session's own model requests. The session `http.request` and `http.response` hooks still
work, as model hooks that skip calls without a Session.
work for Session requests, inside the model hooks.
```ts
effect: (ctx) =>

View file

@ -1505,8 +1505,8 @@ await ctx.session.hook(
#### Native HTTP
Prefer the [model HTTP hooks](#model-http-experimental), which run for the same requests and for calls made outside a
Session. `ctx.session.hook("http.request")` and `"http.response"` still work: each one is registered as a model hook
that skips calls without a Session, so both kinds run in plugin order.
Session. `ctx.session.hook("http.request")` and `"http.response"` still work for Session requests. The model hooks wrap
them: a model request hook runs before the session one, and a model response hook runs after it.
#### Native WebSocket (experimental)