refactor(plugin): scope context hook to session (#37175)

Co-authored-by: Dax Raad <d@ironbay.co>
This commit is contained in:
opencode-agent[bot] 2026-07-15 16:32:00 -04:00 committed by GitHub
commit f92d84746b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
19 changed files with 85 additions and 97 deletions

View file

@ -1,15 +1,15 @@
export * as PluginHooks from "./hooks"
import type { AIHooks } from "@opencode-ai/plugin/v2/effect/ai"
import type { AISDKHooks } from "@opencode-ai/plugin/v2/effect/aisdk"
import type { SessionHooks } from "@opencode-ai/plugin/v2/effect/session"
import type { ToolHooks } from "@opencode-ai/plugin/v2/effect/tool"
import { Context, Effect, Layer, Scope } from "effect"
import { makeLocationNode } from "../effect/app-node"
import { State } from "../state"
export interface Domains {
readonly ai: AIHooks
readonly aisdk: AISDKHooks
readonly session: SessionHooks
readonly tool: ToolHooks
}

View file

@ -80,9 +80,6 @@ export const make = Effect.fn("PluginHost.make")(function* (plugin: PluginV2.Int
})
}),
},
ai: {
hook: (name, callback) => hooks.register("ai", name, callback),
},
aisdk: {
hook: (name, callback) => {
if (name === "sdk") {
@ -370,6 +367,7 @@ export const make = Effect.fn("PluginHost.make")(function* (plugin: PluginV2.Int
},
},
session: {
hook: (name, callback) => hooks.register("session", name, callback),
create: (input) =>
runtime.session.create({
id: input?.id,

View file

@ -63,10 +63,6 @@ export function fromPromise(plugin: Plugin) {
transform: transform(host.agent),
reload: () => run(host.agent.reload()),
},
ai: {
hook: (name, callback) =>
register(host.ai.hook(name, (event) => Effect.promise(() => Promise.resolve(callback(event))))),
},
aisdk: {
hook: (name, callback) =>
register(host.aisdk.hook(name, (event) => Effect.promise(() => Promise.resolve(callback(event))))),
@ -166,6 +162,8 @@ export function fromPromise(plugin: Plugin) {
register(host.tool.hook(name, (event) => Effect.promise(() => Promise.resolve(callback(event))))),
},
session: {
hook: (name, callback) =>
register(host.session.hook(name, (event) => Effect.promise(() => Promise.resolve(callback(event))))),
create: (input) =>
run(
host.session.create(

View file

@ -10,7 +10,7 @@ import {
isContextOverflowFailure,
type ProviderErrorEvent,
} from "@opencode-ai/ai"
import type { AIHooks } from "@opencode-ai/plugin/v2/effect/ai"
import type { SessionHooks } from "@opencode-ai/plugin/v2/effect/session"
import { SessionError } from "@opencode-ai/schema/session-error"
import { Money } from "@opencode-ai/schema/money"
import { Cause, Effect, Exit, Fiber, FiberSet, Layer, Option, Semaphore, Stream } from "effect"
@ -216,7 +216,7 @@ const layer = Layer.effect(
toolChoice: isLastStep ? "none" : undefined,
})
const availableTools = new Map(request.tools.map((tool) => [tool.name, tool]))
const requestEvent: AIHooks["request"] = {
const contextEvent: SessionHooks["context"] = {
sessionID: session.id,
agent: agent.id,
model: resolved.ref,
@ -228,11 +228,11 @@ const layer = Layer.effect(
}
// Plugins may reshape the draft but cannot advertise tools excluded by
// permissions, registration state, or the selected agent's step limit.
yield* hooks.trigger("ai", "request", requestEvent)
yield* hooks.trigger("session", "context", contextEvent)
const hookedRequest = LLM.updateRequest(request, {
system: requestEvent.system,
messages: requestEvent.messages,
tools: Object.entries(requestEvent.tools).flatMap(([name, tool]) => {
system: contextEvent.system,
messages: contextEvent.messages,
tools: Object.entries(contextEvent.tools).flatMap(([name, tool]) => {
const registered = availableTools.get(name)
if (!registered) return []
return [{ ...registered, description: tool.description, inputSchema: tool.input }]

View file

@ -196,7 +196,7 @@ export const Plugin = {
)
.pipe(Effect.orDie)
yield* ctx.ai.hook("request", (event) =>
yield* ctx.session.hook("context", (event) =>
Effect.sync(() => {
const usePatch =
event.model.providerID.toLowerCase() === "openai" || event.model.id.toLowerCase().includes("gpt")

View file

@ -216,7 +216,7 @@ export const Plugin = {
)
.pipe(Effect.orDie)
yield* ctx.ai.hook("request", (event) =>
yield* ctx.session.hook("context", (event) =>
Effect.gen(function* () {
const tool = event.tools[name]
if (!tool) return