From 24bd1663a7a6509c0f737c7e7b34487a5a17b568 Mon Sep 17 00:00:00 2001 From: Aiden Cline Date: Wed, 11 Nov 2026 15:19:57 -0600 Subject: [PATCH] refactor(core): run session HTTP hooks inside model HTTP hooks --- packages/core/src/model-http.ts | 26 +++++-- packages/core/src/plugin/hooks.ts | 70 +++---------------- packages/plugin/src/effect/session.ts | 9 ++- packages/plugin/src/promise/session.ts | 9 ++- .../src/docs/content/build/plugins/effect.mdx | 2 +- .../src/docs/content/build/plugins/server.mdx | 4 +- 6 files changed, 41 insertions(+), 79 deletions(-) diff --git a/packages/core/src/model-http.ts b/packages/core/src/model-http.ts index 3d55cc666ce..248e6eaa7b5 100644 --- a/packages/core/src/model-http.ts +++ b/packages/core/src/model-http.ts @@ -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) }) diff --git a/packages/core/src/plugin/hooks.ts b/packages/core/src/plugin/hooks.ts index f67f03ea8e8..783aefdadb3 100644 --- a/packages/core/src/plugin/hooks.ts +++ b/packages/core/src/plugin/hooks.ts @@ -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 = 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 = Exclude< - Name, - Domain extends "session" ? "http.request" | "http.response" : never -> - export interface Interface { readonly has: ( domain: Domain, - name: Triggered, + name: keyof Domains[Domain] & keyof Failures[Domain], providerID?: string, ) => Effect.Effect readonly register: ( @@ -61,11 +54,11 @@ export interface Interface { callback: Callback, options?: ModelHookOptions, ) => Effect.Effect - readonly trigger: >( + readonly trigger: ( domain: Domain, - name: Hook, - event: Domains[Domain][Hook], - ) => Effect.Effect + name: Name, + event: Domains[Domain][Name], + ) => Effect.Effect } export class Service extends Context.Service()("@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 => { - 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 => { - 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: [] }) diff --git a/packages/plugin/src/effect/session.ts b/packages/plugin/src/effect/session.ts index ce6ebb8d3eb..c3db2731901 100644 --- a/packages/plugin/src/effect/session.ts +++ b/packages/plugin/src/effect/session.ts @@ -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 diff --git a/packages/plugin/src/promise/session.ts b/packages/plugin/src/promise/session.ts index 9c53dd3b9ae..f460bf5021f 100644 --- a/packages/plugin/src/promise/session.ts +++ b/packages/plugin/src/promise/session.ts @@ -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 diff --git a/services/site/src/docs/content/build/plugins/effect.mdx b/services/site/src/docs/content/build/plugins/effect.mdx index 0ead3c9b031..bc998e589ff 100644 --- a/services/site/src/docs/content/build/plugins/effect.mdx +++ b/services/site/src/docs/content/build/plugins/effect.mdx @@ -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) => diff --git a/services/site/src/docs/content/build/plugins/server.mdx b/services/site/src/docs/content/build/plugins/server.mdx index 819ac121b8b..8e5338ebfca 100644 --- a/services/site/src/docs/content/build/plugins/server.mdx +++ b/services/site/src/docs/content/build/plugins/server.mdx @@ -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)