mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-16 05:01:03 +03:00
refactor(core): resolve prompt hook model once per step (#58601)
This commit is contained in:
committed by
GitHub
Unverified
parent
228d232e5f
commit
6436011a52
@@ -5,6 +5,7 @@ import { define } from "@opencode/plugin/effect/plugin"
|
||||
import type { SessionHooks } from "@opencode/plugin/effect/session"
|
||||
import { Model } from "@opencode/schema/model"
|
||||
import { Effect } from "effect"
|
||||
import { OptimizePlugin } from "./optimize.js"
|
||||
|
||||
export function identity(model: { readonly name: string; readonly ref: Model.Ref }) {
|
||||
return [
|
||||
@@ -20,10 +21,7 @@ export const Plugin = define({
|
||||
effect: Effect.fn("IdentityPlugin")(function* (ctx) {
|
||||
const hook = (event: SessionHooks["context"]) =>
|
||||
Effect.gen(function* () {
|
||||
const model =
|
||||
(yield* ctx.model.list()).data.find(
|
||||
(model) => model.providerID === event.model.providerID && model.id === event.model.id,
|
||||
) ?? Model.Info.default(event.model.providerID, event.model.id)
|
||||
const model = yield* OptimizePlugin.resolveModel(ctx, event)
|
||||
event.system.splice(1, 0, SystemPart.make(identity({ name: model.name, ref: event.model })))
|
||||
}).pipe(Effect.catch(() => Effect.void))
|
||||
yield* ctx.session.hook("context", hook)
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
export * as OptimizePlugin from "./optimize.js"
|
||||
|
||||
import { define } from "@opencode/plugin/effect/plugin"
|
||||
import { define, type Context } from "@opencode/plugin/effect/plugin"
|
||||
import type { SessionHooks } from "@opencode/plugin/effect/session"
|
||||
import { Model } from "@opencode/schema/model"
|
||||
import { Effect } from "effect"
|
||||
@@ -61,6 +61,20 @@ export const MetaPlugin = make("opencode.prompt.meta", (model) => {
|
||||
|
||||
export const Plugins = [OpenAIPlugin, AnthropicPlugin, KimiPlugin, ArceePlugin, MetaPlugin] as const
|
||||
|
||||
const models = new WeakMap<SessionHooks["context"], Model.Info>()
|
||||
|
||||
export const resolveModel = (ctx: Context, event: SessionHooks["context"]) =>
|
||||
Effect.gen(function* () {
|
||||
const cached = models.get(event)
|
||||
if (cached?.providerID === event.model.providerID && cached.id === event.model.id) return cached
|
||||
const model =
|
||||
(yield* ctx.model.list()).data.find(
|
||||
(model) => model.providerID === event.model.providerID && model.id === event.model.id,
|
||||
) ?? Model.Info.default(event.model.providerID, event.model.id)
|
||||
models.set(event, model)
|
||||
return model
|
||||
})
|
||||
|
||||
function make(
|
||||
id: string,
|
||||
optimize: (model: Model.Info, tools: SessionHooks["context"]["tools"]) => string | undefined,
|
||||
@@ -71,10 +85,7 @@ function make(
|
||||
effect: Effect.fn(`OptimizePlugin.${id}`)(function* (ctx) {
|
||||
const hook = (event: SessionHooks["context"]) =>
|
||||
Effect.gen(function* () {
|
||||
const model =
|
||||
(yield* ctx.model.list()).data.find(
|
||||
(model) => model.providerID === event.model.providerID && model.id === event.model.id,
|
||||
) ?? Model.Info.default(event.model.providerID, event.model.id)
|
||||
const model = yield* resolveModel(ctx, event)
|
||||
// Curate tools before rendering their guidance, including for agents with a custom system prompt.
|
||||
const template = optimize(model, event.tools)
|
||||
if (!template) return
|
||||
|
||||
@@ -4,6 +4,7 @@ import { Agent } from "@opencode/core/agent"
|
||||
import { Plugin } from "@opencode/core/plugin"
|
||||
import { PluginHooks } from "@opencode/core/plugin/hooks"
|
||||
import { PluginHost } from "@opencode/core/plugin/host"
|
||||
import { IdentityPlugin } from "@opencode/core/plugin/identity"
|
||||
import { OptimizePlugin } from "@opencode/core/plugin/optimize"
|
||||
import { Session } from "@opencode/core/session"
|
||||
import { SessionSystemPrompt } from "@opencode/core/session/system-prompt"
|
||||
@@ -352,4 +353,46 @@ describe("OptimizePlugin", () => {
|
||||
)
|
||||
}),
|
||||
)
|
||||
|
||||
it.effect("lists models once per step across prompt and identity plugins", () =>
|
||||
Effect.gen(function* () {
|
||||
const catalog = yield* Provider.Service
|
||||
const hooks = yield* PluginHooks.Service
|
||||
const baseHost = yield* makeHost
|
||||
let listCalls = 0
|
||||
const pluginHost = {
|
||||
...baseHost,
|
||||
model: {
|
||||
...baseHost.model,
|
||||
list: (input?: Parameters<typeof baseHost.model.list>[0]) =>
|
||||
Effect.sync(() => {
|
||||
listCalls += 1
|
||||
}).pipe(Effect.andThen(baseHost.model.list(input))),
|
||||
},
|
||||
}
|
||||
yield* catalog.transform((editor) => {
|
||||
editor.models.update(Provider.ID.make("test"), Model.ID.make("meta/muse-spark-1.1"), (model) => {
|
||||
model.name = "Muse Spark"
|
||||
})
|
||||
})
|
||||
yield* Effect.forEach(OptimizePlugin.Plugins, (plugin) => plugin.effect(pluginHost), { discard: true })
|
||||
yield* IdentityPlugin.Plugin.effect(pluginHost)
|
||||
|
||||
const named = context("meta/muse-spark-1.1")
|
||||
yield* hooks.trigger("session", "context", named)
|
||||
expect(listCalls).toBe(1)
|
||||
expect(named.system.map((part) => part.text)).toEqual([
|
||||
SessionSystemPrompt.render(PROMPT_META.replaceAll("{{MODEL_NAME}}", "Muse Spark"), Object.keys(named.tools)),
|
||||
IdentityPlugin.identity({ name: "Muse Spark", ref: named.model }),
|
||||
])
|
||||
|
||||
const unknown = context("unknown-gpt-5")
|
||||
yield* hooks.trigger("session", "compaction", unknown)
|
||||
expect(listCalls).toBe(2)
|
||||
expect(unknown.system.map((part) => part.text)).toEqual([
|
||||
SessionSystemPrompt.render(PROMPT_GPT, Object.keys(unknown.tools)),
|
||||
IdentityPlugin.identity({ name: "unknown-gpt-5", ref: unknown.model }),
|
||||
])
|
||||
}),
|
||||
)
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user