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

@ -44,7 +44,7 @@ export const registerToolPlugin = <R>(plugin: {
Effect.gen(function* () {
const tools = yield* Tools.Service
const context = host({
ai: {
session: {
hook: () => Effect.succeed({ dispose: Effect.void }),
},
tool: {

View file

@ -12,17 +12,17 @@ const layer = PluginHooks.node.implementation as Layer.Layer<PluginHooks.Service
const it = testEffect(layer)
describe("PluginHooks", () => {
it.effect("registers scoped AI hooks and triggers them sequentially", () =>
it.effect("registers scoped session hooks and triggers them sequentially", () =>
Effect.gen(function* () {
const hooks = yield* PluginHooks.Service
const seen: string[] = []
yield* hooks.register("ai", "request", (event) =>
yield* hooks.register("session", "context", (event) =>
Effect.sync(() => {
seen.push("first")
event.system.push(SystemPart.make("second"))
}),
)
yield* hooks.register("ai", "request", (event) =>
yield* hooks.register("session", "context", (event) =>
Effect.sync(() => {
seen.push(event.system[1]?.text ?? "missing")
event.messages = [Message.user("changed")]
@ -37,7 +37,7 @@ describe("PluginHooks", () => {
tools: {},
}
expect(yield* hooks.trigger("ai", "request", event)).toBe(event)
expect(yield* hooks.trigger("session", "context", event)).toBe(event)
expect(seen).toEqual(["first", "second"])
expect(event.messages).toEqual([Message.user("changed")])
}),

View file

@ -8,7 +8,9 @@ import { ProviderV2 } from "@opencode-ai/core/provider"
import type { IntegrationEnvMethod, IntegrationKeyMethod, IntegrationOAuthMethod } from "@opencode-ai/sdk/v2/types"
import { Effect, Stream } from "effect"
type Overrides = Partial<Omit<PluginContext, "options">>
type Overrides = Partial<Omit<PluginContext, "options" | "session">> & {
readonly session?: Partial<PluginContext["session"]>
}
export function host(overrides: Overrides = {}): PluginContext {
return {
@ -18,9 +20,6 @@ export function host(overrides: Overrides = {}): PluginContext {
transform: () => Effect.die("unused agent.transform"),
reload: () => Effect.die("unused agent.reload"),
},
ai: overrides.ai ?? {
hook: () => Effect.die("unused ai.hook"),
},
aisdk: overrides.aisdk ?? {
hook: () => Effect.die("unused aisdk.hook"),
},
@ -80,12 +79,13 @@ export function host(overrides: Overrides = {}): PluginContext {
transform: () => Effect.die("unused tool.transform"),
hook: () => Effect.die("unused tool.hook"),
},
session: overrides.session ?? {
create: () => Effect.die("unused session.create"),
get: () => Effect.die("unused session.get"),
prompt: () => Effect.die("unused session.prompt"),
command: () => Effect.die("unused session.command"),
interrupt: () => Effect.die("unused session.interrupt"),
session: {
hook: overrides.session?.hook ?? (() => Effect.die("unused session.hook")),
create: overrides.session?.create ?? (() => Effect.die("unused session.create")),
get: overrides.session?.get ?? (() => Effect.die("unused session.get")),
prompt: overrides.session?.prompt ?? (() => Effect.die("unused session.prompt")),
command: overrides.session?.command ?? (() => Effect.die("unused session.command")),
interrupt: overrides.session?.interrupt ?? (() => Effect.die("unused session.interrupt")),
},
}
}

View file

@ -10,7 +10,7 @@ import { SessionV2 } from "@opencode-ai/core/session"
import { SessionMessage } from "@opencode-ai/core/session/message"
import { ToolRegistry } from "@opencode-ai/core/tool/registry"
import { Plugin } from "@opencode-ai/plugin/v2"
import type { AIHooks } from "@opencode-ai/plugin/v2/effect/ai"
import type { SessionHooks } from "@opencode-ai/plugin/v2/effect/session"
import { Model } from "@opencode-ai/schema/model"
import { Provider } from "@opencode-ai/schema/provider"
import { testEffect } from "../lib/effect"
@ -77,24 +77,24 @@ describe("fromPromise", () => {
}),
)
it.effect("forwards AI request hooks", () =>
it.effect("forwards session context hooks", () =>
Effect.gen(function* () {
const plugin = yield* PluginV2.Service
const hooks = yield* PluginHooks.Service
const host = yield* PluginHost.make(plugin)
yield* PluginPromise.fromPromise(
Plugin.define({
id: "promise-ai-request",
id: "promise-session-context",
setup: async (ctx) => {
await ctx.ai.hook("request", (event) => {
await ctx.session.hook("context", (event) => {
event.system.push(SystemPart.make("Promise hook"))
delete event.tools.echo
})
},
}),
).effect(host)
const event: AIHooks["request"] = {
sessionID: SessionV2.ID.make("ses_promise_ai_request"),
const event: SessionHooks["context"] = {
sessionID: SessionV2.ID.make("ses_promise_session_context"),
agent: AgentV2.ID.make("build"),
model: Model.Ref.make({ providerID: Provider.ID.make("test"), id: Model.ID.make("model") }),
system: [SystemPart.make("Initial")],
@ -102,7 +102,7 @@ describe("fromPromise", () => {
tools: { echo: { description: "Echo", input: { type: "object" } } },
}
yield* hooks.trigger("ai", "request", event)
yield* hooks.trigger("session", "context", event)
expect(event.system.map((part) => part.text)).toEqual(["Initial", "Promise hook"])
expect(event.tools).toEqual({})

View file

@ -777,11 +777,11 @@ const verifyPartialFlushOnInterruption = (kind: FragmentKind) =>
})
describe("SessionRunnerLLM", () => {
it.effect("applies AI request hooks without exposing unavailable tools", () =>
it.effect("applies session context hooks without exposing unavailable tools", () =>
Effect.gen(function* () {
const session = yield* setup
const hooks = yield* PluginHooks.Service
yield* hooks.register("ai", "request", (event) =>
yield* hooks.register("session", "context", (event) =>
Effect.sync(() => {
event.system = [SystemPart.make("Hooked system")]
event.messages = [Message.user("Hooked message")]