Compare commits

..
15 changed files with 200 additions and 533 deletions
+58 -48
View File
@@ -1,7 +1,7 @@
export * as Catalog from "./catalog"
import { makeLocationNode } from "@opencode-ai/util/effect/app-node"
import { Array, Context, Effect, Layer, Option, Order, pipe } from "effect"
import { Array, Context, Effect, Layer, Order, pipe } from "effect"
import { Catalog } from "@opencode-ai/schema/catalog"
import { ModelV2 } from "./model"
import { ProviderV2 } from "./provider"
@@ -208,57 +208,24 @@ const layer = Layer.effect(
}),
small: Effect.fn("CatalogV2.model.small")(function* (providerID) {
const record = state.get().providers.get(providerID)
if (!record) return
const provider = record.provider
const catalog = state.get().providers.get(providerID)
if (!catalog) return
// TODO: Remove these provider-specific assumptions once model syncing reliably reports available deployments.
if (providerID === ProviderV2.ID.azure || providerID === ProviderV2.ID.make("azure-cognitive-services")) {
return
}
const cannotDiscoverAvailableModels =
providerID === ProviderV2.ID.azure || providerID === ProviderV2.ID.make("azure-cognitive-services")
if (cannotDiscoverAvailableModels) return
if (providerID === ProviderV2.ID.opencode) {
const gpt5Nano = record.models.get(ModelV2.ID.make("gpt-5-nano"))
if (gpt5Nano?.enabled && gpt5Nano.status === "active") return projectModel(gpt5Nano, provider)
}
const designated =
providerID === ProviderV2.ID.opencode ? catalog.models.get(ModelV2.ID.make("gpt-5-nano")) : undefined
if (designated && isActive(designated)) return projectModel(designated, catalog.provider)
const candidates = pipe(
Array.fromIterable(record.models.values()),
Array.filter(
(model) =>
model.providerID === providerID &&
model.enabled &&
model.status === "active" &&
model.capabilities.input.some((item) => item.startsWith("text")) &&
model.capabilities.output.some((item) => item.startsWith("text")),
),
Array.map((model) => ({
model,
cost: model.cost[0] ? model.cost[0].input + model.cost[0].output : 999,
age: (Date.now() - model.time.released) / (1000 * 60 * 60 * 24 * 30),
small: SMALL_MODEL_RE.test(`${model.id} ${model.family ?? ""} ${model.name}`.toLowerCase()),
})),
Array.filter((item) => item.cost > 0 && item.age <= 18),
)
const pick = (items: typeof candidates) => {
const maxCost = Math.max(...items.map((item) => item.cost), 0.01)
const maxAge = Math.max(...items.map((item) => item.age), 0.01)
return pipe(
items,
Array.sortWith((item) => (item.cost / maxCost) * 0.8 + (item.age / maxAge) * 0.2, Order.Number),
Array.map((item) => projectModel(item.model, provider)),
Array.head,
)
}
return Option.getOrUndefined(
pipe(
candidates,
Array.filter((item) => item.small),
(items) => (items.length > 0 ? pick(items) : pick(candidates)),
),
)
const candidates = modelCandidates(catalog.models.values(), providerID)
const small = candidates.filter((candidate) => candidate.looksSmall)
const pool = small.length > 0 ? small : candidates
const selected = bestValue(pool)
if (!selected) return
return projectModel(selected.model, catalog.provider)
}),
},
}
@@ -268,5 +235,48 @@ const layer = Layer.effect(
)
const SMALL_MODEL_RE = /\b(nano|flash|lite|mini|haiku|small|fast)\b/
const MONTH_MS = 1000 * 60 * 60 * 24 * 30
const isActive = (model: ModelV2.Info) => model.enabled && model.status === "active"
const modelCandidates = (models: Iterable<ModelV2.Info>, providerID: ProviderV2.ID) => {
const now = Date.now()
return Array.fromIterable(models).flatMap((model) => {
if (
model.providerID !== providerID ||
!isActive(model) ||
!model.capabilities.input.some((item) => item.startsWith("text")) ||
!model.capabilities.output.some((item) => item.startsWith("text"))
)
return []
const cost = model.cost[0] ? model.cost[0].input + model.cost[0].output : 999
const age = (now - model.time.released) / MONTH_MS
if (cost <= 0 || age > 18) return []
return [
{
model,
cost,
age,
looksSmall: SMALL_MODEL_RE.test(`${model.id} ${model.family ?? ""} ${model.name}`.toLowerCase()),
},
]
})
}
type Candidate = ReturnType<typeof modelCandidates>[number]
const bestValue = (candidates: readonly Candidate[]) => {
if (!Array.isReadonlyArrayNonEmpty(candidates)) return
const maxCost = Math.max(...candidates.map((candidate) => candidate.cost), 0.01)
const maxAge = Math.max(...candidates.map((candidate) => candidate.age), 0.01)
return Array.min(
candidates,
Order.mapInput(
Order.Number,
(candidate: Candidate) => (candidate.cost / maxCost) * 0.8 + (candidate.age / maxAge) * 0.2,
),
)
}
export const node = makeLocationNode({ service: Service, layer, deps: [EventV2.node, Integration.node] })
+18 -39
View File
@@ -1,45 +1,24 @@
export * as CodeModeInstructions from "./instructions"
import { Context, Effect, Layer, Schema } from "effect"
import { makeLocationNode } from "@opencode-ai/util/effect/app-node"
import { AgentV2 } from "../agent"
import { CodeMode } from "../codemode"
import { Effect, Schema } from "effect"
import { Instructions } from "../instructions/index"
export interface Interface {
readonly load: (agent: AgentV2.Selection) => Effect.Effect<Instructions.Instructions>
const key = Instructions.Key.make("core/codemode")
const codec = Schema.toCodecJson(Schema.String)
const render = {
initial: (current: string) => current,
changed: (_previous: string, current: string) =>
[
"The Code Mode tool catalog has changed. This catalog supersedes the previous Code Mode tool catalog.",
current,
].join("\n\n"),
removed: () => "Code Mode tools are no longer available. Do not use any previously listed Code Mode tools.",
}
export class Service extends Context.Service<Service, Interface>()("@opencode/v2/CodeModeInstructions") {}
const layer = Layer.effect(
Service,
Effect.gen(function* () {
const codeMode = yield* CodeMode.Service
return Service.of({
load: Effect.fn("CodeModeInstructions.load")(function* (selection) {
const instructions = selection.info
? (yield* codeMode.materialize(selection.info.permissions)).instructions
: undefined
return Instructions.make({
key: Instructions.Key.make("core/codemode"),
codec: Schema.toCodecJson(Schema.String),
read: Effect.succeed(instructions ?? Instructions.removed),
render: {
initial: (current) => current,
changed: (_previous, current) =>
[
"The Code Mode tool catalog has changed. This catalog supersedes the previous Code Mode tool catalog.",
current,
].join("\n\n"),
removed: () =>
"Code Mode tools are no longer available. Do not use any previously listed Code Mode tools.",
},
})
}),
})
}),
)
export const node = makeLocationNode({ service: Service, layer, deps: [CodeMode.node] })
export const make = (content?: string): Instructions.Instructions =>
Instructions.make({
key,
codec,
read: Effect.succeed(content ?? Instructions.removed),
render,
})
-2
View File
@@ -3,7 +3,6 @@ import { AgentV2 } from "./agent"
import { AISDK } from "./aisdk"
import { Catalog } from "./catalog"
import { CodeMode } from "./codemode"
import { CodeModeInstructions } from "./codemode/instructions"
import { CommandV2 } from "./command"
import { Config } from "./config"
import { LayerNode } from "@opencode-ai/util/effect/layer-node"
@@ -82,7 +81,6 @@ const locationServiceNodes = [
ToolRegistry.toolsNode,
Image.node,
SkillInstructions.node,
CodeModeInstructions.node,
ReferenceInstructions.node,
InstructionEntry.node,
Form.node,
-2
View File
@@ -15,7 +15,6 @@ import { GooglePlugin } from "./provider/google"
import { GoogleVertexAnthropicPlugin, GoogleVertexPlugin } from "./provider/google-vertex"
import { GroqPlugin } from "./provider/groq"
import { KiloPlugin } from "./provider/kilo"
import { KimiForCodingPlugin } from "./provider/kimi-for-coding"
import { LLMGatewayPlugin } from "./provider/llmgateway"
import { MistralPlugin } from "./provider/mistral"
import { NvidiaPlugin } from "./provider/nvidia"
@@ -36,7 +35,6 @@ import type { PluginInternal } from "./internal"
export const ProviderPlugins: PluginInternal.InternalPlugin[] = [
AlibabaPlugin,
AmazonBedrockPlugin,
KimiForCodingPlugin,
AnthropicPlugin,
AzureCognitiveServicesPlugin,
AzurePlugin,
@@ -1,254 +0,0 @@
import { arch, hostname, platform, release } from "node:os"
import path from "node:path"
import type { IntegrationOAuthMethodRegistration } from "@opencode-ai/plugin/v2/effect/integration"
import { define } from "@opencode-ai/plugin/v2/effect/plugin"
import { Clock, Effect, Option, Schema, Semaphore, Stream } from "effect"
import { Global } from "@opencode-ai/util/global"
import { App } from "../../app"
import { Credential } from "../../credential"
import { EventV2 } from "../../event"
import { Integration } from "../../integration"
import { ProviderV2 } from "../../provider"
import type { PluginInternal } from "../internal"
const clientID = "17e5f671-d194-4dfb-9706-5516cb48c098"
const issuer = "https://auth.kimi.com"
const deviceGrant = "urn:ietf:params:oauth:grant-type:device_code"
const pollingSafetyMargin = 3000
const methodID = Integration.MethodID.make("device")
const providerID = ProviderV2.ID.make("kimi-for-coding")
const Device = Schema.Struct({
device_code: Schema.String,
user_code: Schema.String,
verification_uri: Schema.optional(Schema.String),
verification_uri_complete: Schema.String,
expires_in: Schema.optional(Schema.Number),
interval: Schema.optional(Schema.Number),
})
const Token = Schema.Struct({
access_token: Schema.String,
refresh_token: Schema.String,
expires_in: Schema.Number,
})
type Token = typeof Token.Type
const TokenError = Schema.Struct({
error: Schema.optional(Schema.String),
error_description: Schema.optional(Schema.String),
})
const decodeTokenError = Schema.decodeUnknownOption(Schema.fromJsonString(TokenError))
const oauth = (headers: Record<string, string>) =>
({
integrationID: Integration.ID.make("kimi-for-coding"),
method: {
id: methodID,
type: "oauth",
label: "Kimi Code subscription (OAuth)",
},
authorize: () =>
request(
`${issuer}/api/oauth/device_authorization`,
{
method: "POST",
headers: formHeaders(headers),
body: new URLSearchParams({ client_id: clientID }).toString(),
},
Device,
).pipe(
Effect.flatMap((device) =>
Clock.currentTimeMillis.pipe(
Effect.map((created) => {
const lifetime = positiveSeconds(device.expires_in, 0)
return {
mode: "auto" as const,
url: device.verification_uri_complete,
instructions: device.verification_uri
? `Open ${device.verification_uri} on any device and enter code: ${device.user_code}`
: `Enter code: ${device.user_code}`,
...(lifetime ? { expiresAt: created + lifetime * 1000 } : {}),
callback: poll(device, headers).pipe(Effect.map(credential)),
}
}),
),
),
),
refresh: (value) => refresh(Credential.OAuth.make({ ...value, methodID }), headers),
}) satisfies IntegrationOAuthMethodRegistration
export const KimiForCodingPlugin = define({
id: "opencode.provider.kimi-for-coding",
effect: Effect.fn(function* (ctx) {
const events = yield* EventV2.Service
const loading = Semaphore.makeUnsafe(1)
const headers = identityHeaders(ctx.app, yield* deviceID())
let connected = false
const load = Effect.fn("KimiForCodingPlugin.load")(function* () {
const connection = yield* ctx.integration.connection.active("kimi-for-coding")
const value = connection
? yield* ctx.integration.connection.resolve(connection).pipe(Effect.catch(() => Effect.succeed(undefined)))
: undefined
connected = value?.type === "oauth" && value.methodID === methodID
})
yield* ctx.integration.transform((draft) => {
draft.method.update(oauth(headers))
})
yield* load()
yield* ctx.catalog.transform((evt) => {
if (!connected) return
const item = evt.provider.get(providerID)
if (!item) return
evt.provider.update(providerID, (provider) => {
// Kimi's OAuth token is a bearer credential on the managed OpenAI-compatible API.
// The API-key connection remains on the catalog's Anthropic transport.
provider.package = ProviderV2.aisdk("@ai-sdk/openai-compatible")
provider.headers = ProviderV2.mergeHeaders(provider.headers, headers)
})
})
const reload = () => loading.withPermit(load().pipe(Effect.andThen(ctx.catalog.reload())))
yield* events.subscribe(Integration.Event.ConnectionUpdated).pipe(
Stream.filter((event) => event.data.integrationID === Integration.ID.make("kimi-for-coding")),
Stream.runForEach(reload),
Effect.forkScoped({ startImmediately: true }),
)
}),
} satisfies PluginInternal.InternalPlugin)
function poll(device: typeof Device.Type, headers: Record<string, string>): Effect.Effect<Token, unknown> {
return Effect.gen(function* () {
const started = yield* Clock.currentTimeMillis
const expires = started + positiveSeconds(device.expires_in, 300) * 1000
const loop = (interval: number): Effect.Effect<Token, unknown> =>
Effect.gen(function* () {
if ((yield* Clock.currentTimeMillis) >= expires) {
return yield* Effect.fail(new Error("Kimi Code device authorization timed out"))
}
const response = yield* send(`${issuer}/api/oauth/token`, {
method: "POST",
headers: formHeaders(headers),
body: new URLSearchParams({
client_id: clientID,
device_code: device.device_code,
grant_type: deviceGrant,
}).toString(),
})
if (response.ok) return yield* decode(response, Token)
const error = yield* Effect.promise(() => response.text()).pipe(
Effect.map((body) => Option.getOrUndefined(decodeTokenError(body))),
Effect.catch(() => Effect.succeed(undefined)),
)
if (error?.error === "authorization_pending") {
return yield* Effect.sleep(interval + pollingSafetyMargin).pipe(Effect.andThen(loop(interval)))
}
if (error?.error === "slow_down") {
const next = interval + 5000
return yield* Effect.sleep(next + pollingSafetyMargin).pipe(Effect.andThen(loop(next)))
}
if (error?.error === "expired_token") {
return yield* Effect.fail(new Error("Kimi Code device code expired - please re-run login"))
}
if (error?.error === "access_denied") {
return yield* Effect.fail(new Error("Kimi Code device authorization was denied"))
}
const detail = error?.error_description ?? error?.error
return yield* Effect.fail(
new Error(`Kimi Code token exchange failed (${response.status})${detail ? `: ${detail}` : ""}`),
)
})
return yield* loop(Math.max(positiveSeconds(device.interval, 5) * 1000, 1000))
})
}
function refresh(value: Credential.OAuth, headers: Record<string, string>) {
return request(
`${issuer}/api/oauth/token`,
{
method: "POST",
headers: formHeaders(headers),
body: new URLSearchParams({
client_id: clientID,
grant_type: "refresh_token",
refresh_token: value.refresh,
}).toString(),
},
Token,
).pipe(Effect.map((token) => credential(token, value.metadata)))
}
function credential(token: Token, metadata?: Readonly<Record<string, unknown>>) {
return Credential.OAuth.make({
type: "oauth",
methodID,
access: token.access_token,
refresh: token.refresh_token,
expires: Date.now() + positiveSeconds(token.expires_in, 3600) * 1000,
metadata,
})
}
function request<S extends Schema.Decoder<unknown>>(url: string, init: RequestInit, schema: S) {
return send(url, init).pipe(
Effect.flatMap((response) => {
if (response.ok) return decode(response, schema)
return Effect.promise(() => response.text()).pipe(
Effect.flatMap((detail) =>
Effect.fail(new Error(`Kimi Code request failed (${response.status})${detail ? `: ${detail}` : ""}`)),
),
)
}),
)
}
function send(url: string, init: RequestInit) {
return Effect.tryPromise({
try: (signal) => fetch(url, { ...init, signal }),
catch: (cause) => cause,
})
}
function decode<S extends Schema.Decoder<unknown>>(response: Response, schema: S) {
return Effect.promise(() => response.json()).pipe(Effect.map(Schema.decodeUnknownSync(schema)))
}
function identityHeaders(app: App.Info, id: string) {
return {
"User-Agent": `opencode/${app.version}`,
"X-Msh-Platform": "kimi_code_cli",
"X-Msh-Version": app.version,
"X-Msh-Device-Name": ascii(hostname()),
"X-Msh-Device-Model": ascii(`${platform()} ${release()} ${arch()}`),
"X-Msh-Os-Version": ascii(release()),
"X-Msh-Device-Id": id,
}
}
function deviceID() {
return Effect.gen(function* () {
const file = Bun.file(path.join(Global.Path.data, "kimi-code-device-id"))
if (yield* Effect.promise(() => file.exists())) {
const value = (yield* Effect.promise(() => file.text())).trim()
if (value) return value
}
const id = crypto.randomUUID()
yield* Effect.promise(() => Bun.write(file, id, { mode: 0o600 }))
return id
})
}
function formHeaders(headers: Record<string, string>) {
return { ...headers, "Content-Type": "application/x-www-form-urlencoded", Accept: "application/json" }
}
function ascii(value: string) {
return value.replaceAll(/[^\u0020-\u007E]/g, "").trim() || "unknown"
}
function positiveSeconds(value: unknown, fallback: number) {
const seconds = Number(value)
return Number.isFinite(seconds) && seconds > 0 ? seconds : fallback
}
+36 -18
View File
@@ -2,6 +2,7 @@ export * as SessionContext from "./context"
import { Context, Effect, Layer } from "effect"
import { AgentV2 } from "../agent"
import { CodeModeInstructions } from "../codemode/instructions"
import { Database } from "../database/database"
import { makeLocationNode } from "@opencode-ai/util/effect/app-node"
import { InstructionDiscovery } from "../instruction-discovery"
@@ -12,7 +13,7 @@ import { McpInstructions } from "../mcp/instructions"
import { PluginSupervisor } from "../plugin/supervisor"
import { ReferenceInstructions } from "../reference/instructions"
import { SkillInstructions } from "../skill/instructions"
import { CodeModeInstructions } from "../codemode/instructions"
import { ToolRegistry } from "../tool/registry"
import { AgentNotFoundError } from "./error"
import { SessionHistory } from "./history"
import { InstructionEntry } from "./instruction-entry"
@@ -25,6 +26,7 @@ export interface Selection {
readonly session: SessionSchema.Info
readonly agent: AgentV2.Selection & { readonly info: AgentV2.Info }
readonly instructions: Instructions.Instructions
readonly toolSet: ToolRegistry.ToolSet
}
export interface Loaded {
@@ -33,15 +35,17 @@ export interface Loaded {
readonly model: SessionRunnerModel.Resolved
readonly initial: string
readonly messages: ReadonlyArray<SessionMessage.Info>
readonly toolSet: ToolRegistry.ToolSet
}
/**
* Resolves model-request state in two phases: `select` fixes the Session,
* agent, and instruction sources; `load` adds the model and active history for
* that selection. This module does not build or execute the model request.
* agent, instruction sources, and tool snapshot; `load` adds the model and
* active history for that selection. This module does not build or execute the
* model request.
*/
export interface Interface {
/** Selects the Session, agent, and instruction sources used by subsequent work. */
/** Selects the Session, agent, instructions, and tools used by subsequent work. */
readonly select: (sessionID: SessionSchema.ID) => Effect.Effect<Selection, AgentNotFoundError>
/** Resolves the model and active history for that selection. */
readonly load: (selection: Selection) => Effect.Effect<Loaded, SessionRunnerModel.Error>
@@ -55,7 +59,6 @@ const layer = Layer.effect(
Effect.gen(function* () {
const agents = yield* AgentV2.Service
const builtins = yield* InstructionBuiltIns.Service
const codeModeInstructions = yield* CodeModeInstructions.Service
const db = (yield* Database.Service).db
const discovery = yield* InstructionDiscovery.Service
const entries = yield* InstructionEntry.Service
@@ -66,6 +69,7 @@ const layer = Layer.effect(
const referenceInstructions = yield* ReferenceInstructions.Service
const skillInstructions = yield* SkillInstructions.Service
const store = yield* SessionStore.Service
const registry = yield* ToolRegistry.Service
const select = Effect.fn("SessionContext.select")(function* (sessionID: SessionSchema.ID) {
const session = yield* store.get(sessionID)
@@ -76,19 +80,32 @@ const layer = Layer.effect(
yield* plugins.flush
const agent = yield* agents.select(session.agent)
if (!agent.info) return yield* new AgentNotFoundError({ sessionID: session.id, agent: session.agent ?? agent.id })
const instructions = yield* Effect.all(
[
builtins.load(sessionID),
codeModeInstructions.load(agent),
discovery.load(),
skillInstructions.load(agent),
referenceInstructions.load(),
mcpInstructions.load(agent),
entries.load(sessionID),
],
const loaded = yield* Effect.all(
{
toolSet: registry.snapshot(agent.info.permissions),
builtins: builtins.load(sessionID),
discovery: discovery.load(),
skills: skillInstructions.load(agent),
references: referenceInstructions.load(),
mcp: mcpInstructions.load(agent),
entries: entries.load(sessionID),
},
{ concurrency: "unbounded" },
).pipe(Effect.map(Instructions.combine))
return { session, agent: { ...agent, info: agent.info }, instructions }
)
return {
session,
agent: { ...agent, info: agent.info },
instructions: Instructions.combine([
loaded.builtins,
CodeModeInstructions.make(loaded.toolSet.codeModeInstructions),
loaded.discovery,
loaded.skills,
loaded.references,
loaded.mcp,
loaded.entries,
]),
toolSet: loaded.toolSet,
}
})
const load = Effect.fn("SessionContext.load")(function* (selection: Selection) {
@@ -100,6 +117,7 @@ const layer = Layer.effect(
model,
initial: history.initial,
messages: history.entries.map((entry) => entry.message),
toolSet: selection.toolSet,
}
})
@@ -112,7 +130,6 @@ export const node = makeLocationNode({
layer,
deps: [
AgentV2.node,
CodeModeInstructions.node,
Database.node,
InstructionBuiltIns.node,
InstructionDiscovery.node,
@@ -124,5 +141,6 @@ export const node = makeLocationNode({
SessionRunnerModel.node,
SessionStore.node,
SkillInstructions.node,
ToolRegistry.node,
],
})
+2 -12
View File
@@ -12,7 +12,6 @@ import { SessionGenerate } from "./generate"
import { SessionHistory } from "./history"
import { SessionModelHeaders } from "./model-headers"
import { SessionRunnerModel } from "./runner/model"
import { ToolRegistry } from "../tool/registry"
import PROMPT_DEFAULT from "./runner/prompt/base.txt"
import { toLLMMessages } from "./runner/to-llm-message"
@@ -24,7 +23,6 @@ export const layer = Layer.effect(
const hooks = yield* PluginHooks.Service
const llm = yield* LLMClient.Service
const models = yield* SessionRunnerModel.Service
const registry = yield* ToolRegistry.Service
const app = yield* App.Metadata
return SessionGenerate.Service.of({
@@ -36,7 +34,7 @@ export const layer = Layer.effect(
const promptCacheKey = /^ses_[0-9a-f]{64}$/.test(selection.session.id)
? selection.session.id.slice(4)
: selection.session.id
const toolSet = yield* registry.snapshot(selection.agent.info.permissions)
const toolSet = selection.toolSet
const toolDefinitions = toolSet.definitions
const toolsByName = new Map(toolDefinitions.map((tool) => [tool.name, tool]))
const contextEvent = yield* hooks.trigger("session", "context", {
@@ -89,13 +87,5 @@ export const layer = Layer.effect(
export const node = makeLocationNode({
service: SessionGenerate.Service,
layer,
deps: [
SessionContext.node,
Database.node,
PluginHooks.node,
SessionRunnerModel.node,
ToolRegistry.node,
App.node,
llmClient,
],
deps: [SessionContext.node, Database.node, PluginHooks.node, SessionRunnerModel.node, App.node, llmClient],
})
+2 -3
View File
@@ -86,7 +86,6 @@ export const layer = Layer.effect(
Service,
Effect.gen(function* () {
const hooks = yield* PluginHooks.Service
const registry = yield* ToolRegistry.Service
const app = yield* App.Metadata
const prepare = Effect.fn("SessionModelRequest.prepare")(function* (input: PrepareInput) {
@@ -98,7 +97,7 @@ export const layer = Layer.effect(
const stepLimitReached = agent.info.steps !== undefined && input.step >= agent.info.steps
// The final Step keeps definitions available to protocols with native "none",
// preserving their prompt cache prefix. Calls are still rejected at execution.
const toolSet = yield* registry.snapshot(agent.info.permissions)
const toolSet = input.context.toolSet
const promptCacheKey = /^ses_[0-9a-f]{64}$/.test(session.id) ? session.id.slice(4) : session.id
const system = [agent.info.system ? agent.info.system : PROMPT_DEFAULT, input.context.initial]
.filter((part) => part.length > 0)
@@ -162,5 +161,5 @@ export const layer = Layer.effect(
export const node = makeLocationNode({
service: Service,
layer,
deps: [PluginHooks.node, ToolRegistry.node, App.node],
deps: [PluginHooks.node, App.node],
})
+9 -4
View File
@@ -44,12 +44,13 @@ export interface Interface {
}
/**
* One request-scoped snapshot pairing advertised definitions with captured
* tools. A model request executes exactly the tool values it advertised
* even if registration changes while the request is in flight.
* One request-scoped snapshot pairing Code Mode instructions and advertised
* definitions with captured tools. A model request executes exactly the tool
* values it advertised even if registration changes while it is in flight.
*/
export interface ToolSet {
readonly definitions: ReadonlyArray<ToolDefinition>
readonly codeModeInstructions?: string
readonly execute: (input: ExecuteInput) => Effect.Effect<ToolOutcome, ToolOutputStore.Error>
}
@@ -320,8 +321,12 @@ const registryLayer = Layer.effect(
if (whollyDisabled(registration.permission, rules)) continue
direct.set(name, registration)
}
const codemodeTool = (yield* codeMode.materialize(permissions)).tool
const codeModeMaterialization = yield* codeMode.materialize(permissions)
const codemodeTool = codeModeMaterialization.tool
return {
...(codeModeMaterialization.instructions === undefined
? {}
: { codeModeInstructions: codeModeMaterialization.instructions }),
definitions: [
// Definitions are prompt-cache prefix bytes, so order only after effective registrations settle.
...Array.from(direct)
@@ -1,15 +1,12 @@
import { describe, expect } from "bun:test"
import { AgentV2 } from "@opencode-ai/core/agent"
import { CodeMode } from "@opencode-ai/core/codemode"
import { CodeModeInstructions } from "@opencode-ai/core/codemode/instructions"
import { AppNodeBuilder } from "@opencode-ai/core/effect/app-node-builder"
import { Tool } from "@opencode-ai/core/tool/tool"
import { Effect, Layer, Schema } from "effect"
import { Effect, Schema } from "effect"
import { it } from "../lib/effect"
import { readInitial, readUpdate } from "../lib/instructions"
const agent = AgentV2.Info.make(AgentV2.Info.empty(AgentV2.ID.make("build")))
describe("CodeModeInstructions", () => {
it.effect("treats equivalent registration orders as an instruction no-op", () => {
const alpha = Tool.make({
@@ -24,70 +21,46 @@ describe("CodeModeInstructions", () => {
output: Schema.String,
execute: () => Effect.succeed({ output: "zeta" }),
})
const codeModeLayer = AppNodeBuilder.build(CodeMode.node)
const layer = Layer.merge(
codeModeLayer,
AppNodeBuilder.build(CodeModeInstructions.node, [[CodeMode.node, codeModeLayer]]),
)
return Effect.gen(function* () {
const codeMode = yield* CodeMode.Service
const instructions = yield* CodeModeInstructions.Service
const initialized = yield* Effect.scoped(
Effect.gen(function* () {
yield* codeMode.register(Tool.registrationEntries({ zeta, alpha }, { namespace: "tools" }))
return yield* instructions.load({ id: agent.id, info: agent }).pipe(Effect.flatMap(readInitial))
const materialization = yield* codeMode.materialize()
return yield* readInitial(CodeModeInstructions.make(materialization.instructions))
}),
)
const reordered = yield* Effect.scoped(
Effect.gen(function* () {
yield* codeMode.register(Tool.registrationEntries({ alpha, zeta }, { namespace: "tools" }))
return yield* instructions
.load({ id: agent.id, info: agent })
.pipe(Effect.flatMap((context) => readUpdate(context, initialized)))
const materialization = yield* codeMode.materialize()
return yield* readUpdate(CodeModeInstructions.make(materialization.instructions), initialized)
}),
)
expect(reordered.changed).toBe(false)
expect(reordered.text).toBe("")
}).pipe(Effect.provide(layer))
}).pipe(Effect.provide(codeModeLayer))
})
it.effect("renders catalog changes and removal", () => {
let catalog: string | undefined = "Initial Code Mode catalog"
const layer = AppNodeBuilder.build(CodeModeInstructions.node, [
[
CodeMode.node,
Layer.mock(CodeMode.Service, {
materialize: () => Effect.succeed({ ...(catalog === undefined ? {} : { instructions: catalog }) }),
register: () => Effect.void,
}),
],
])
return Effect.gen(function* () {
const instructions = yield* CodeModeInstructions.Service
const initialized = yield* instructions.load({ id: agent.id, info: agent }).pipe(Effect.flatMap(readInitial))
const initialized = yield* readInitial(CodeModeInstructions.make(catalog))
expect(initialized.text).toBe("Initial Code Mode catalog")
catalog = "Updated Code Mode catalog"
expect(
yield* instructions
.load({ id: agent.id, info: agent })
.pipe(Effect.flatMap((context) => readUpdate(context, initialized))),
).toMatchObject({
expect(yield* readUpdate(CodeModeInstructions.make(catalog), initialized)).toMatchObject({
text: "The Code Mode tool catalog has changed. This catalog supersedes the previous Code Mode tool catalog.\n\nUpdated Code Mode catalog",
})
catalog = undefined
expect(
yield* instructions
.load({ id: agent.id, info: agent })
.pipe(Effect.flatMap((context) => readUpdate(context, initialized))),
).toMatchObject({
expect(yield* readUpdate(CodeModeInstructions.make(catalog), initialized)).toMatchObject({
text: "Code Mode tools are no longer available. Do not use any previously listed Code Mode tools.",
})
}).pipe(Effect.provide(layer))
})
})
})
+5 -2
View File
@@ -578,7 +578,8 @@ describe("LocationServiceMap", () => {
const blockedState = yield* update(blocked.path, blockedID)
expect(blockedState.providers.some((provider) => provider.id === blockedID)).toBe(true)
expect(blockedState.providers.some((provider) => provider.id === allowedID)).toBe(false)
expect(blockedState.tools.map((tool) => tool.name).sort()).toEqual([
const blockedTools = blockedState.tools.map((tool) => tool.name)
expect(blockedTools.filter((name) => name !== "execute").sort()).toEqual([
"edit",
"glob",
"grep",
@@ -595,7 +596,9 @@ describe("LocationServiceMap", () => {
const allowedState = yield* update(allowed.path, allowedID)
expect(allowedState.providers.some((provider) => provider.id === allowedID)).toBe(true)
expect(allowedState.providers.some((provider) => provider.id === blockedID)).toBe(false)
expect(allowedState.tools.map((tool) => tool.name).sort()).toEqual([
const allowedTools = allowedState.tools.map((tool) => tool.name)
expect(blockedTools.includes("execute")).toBe(allowedTools.includes("execute"))
expect(allowedTools.filter((name) => name !== "execute").sort()).toEqual([
"edit",
"glob",
"grep",
@@ -1,105 +0,0 @@
import { Catalog } from "@opencode-ai/core/catalog"
import { Credential } from "@opencode-ai/core/credential"
import { Integration } from "@opencode-ai/core/integration"
import { ModelV2 } from "@opencode-ai/core/model"
import { PluginV2 } from "@opencode-ai/core/plugin"
import { PluginHost } from "@opencode-ai/core/plugin/host"
import { KimiForCodingPlugin } from "@opencode-ai/core/plugin/provider/kimi-for-coding"
import { ProviderPlugins } from "@opencode-ai/core/plugin/provider"
import { ProviderV2 } from "@opencode-ai/core/provider"
import { describe, expect } from "bun:test"
import { Effect } from "effect"
import { testEffect } from "../lib/effect"
import { PluginTestLayer } from "./fixture"
const it = testEffect(PluginTestLayer)
const providerID = ProviderV2.ID.make("kimi-for-coding")
const methodID = Integration.MethodID.make("device")
const addPlugin = Effect.fn(function* () {
const plugin = yield* PluginV2.Service
const host = yield* PluginHost.make(plugin)
yield* KimiForCodingPlugin.effect(host)
})
const addProvider = Effect.fn(function* () {
const catalog = yield* Catalog.Service
yield* catalog.transform((draft) => {
draft.provider.update(providerID, (provider) => {
provider.name = "Kimi For Coding"
provider.package = ProviderV2.aisdk("@ai-sdk/anthropic")
provider.settings = { baseURL: "https://api.kimi.com/coding/v1" }
provider.headers = { "X-Custom": "preserved" }
})
draft.model.update(providerID, ModelV2.ID.make("k3"), () => {})
})
})
describe("KimiForCodingPlugin", () => {
it.effect("runs before generic Anthropic and OpenAI-compatible transforms", () =>
Effect.sync(() => {
const ids = ProviderPlugins.map((plugin) => plugin.id)
const index = ids.indexOf("opencode.provider.kimi-for-coding")
expect(index).toBeGreaterThanOrEqual(0)
expect(index).toBeLessThan(ids.indexOf("opencode.provider.anthropic"))
expect(index).toBeLessThan(ids.indexOf("opencode.provider.openai-compatible"))
}),
)
it.effect("registers Kimi Code subscription OAuth", () =>
Effect.gen(function* () {
yield* addPlugin()
expect((yield* (yield* Integration.Service).get(Integration.ID.make("kimi-for-coding")))?.methods).toEqual([
{
id: methodID,
type: "oauth",
label: "Kimi Code subscription (OAuth)",
},
])
}),
)
it.effect("adds Kimi device identity headers for OAuth connections", () =>
Effect.gen(function* () {
const credentials = yield* Credential.Service
const catalog = yield* Catalog.Service
yield* addProvider()
yield* credentials.create({
integrationID: Integration.ID.make("kimi-for-coding"),
value: Credential.OAuth.make({
type: "oauth",
methodID,
access: "access",
refresh: "refresh",
expires: Date.now() + 60_000,
}),
})
yield* addPlugin()
const provider = yield* catalog.provider.get(providerID)
expect(provider?.package).toBe(ProviderV2.aisdk("@ai-sdk/openai-compatible"))
expect(provider?.headers?.["X-Custom"]).toBe("preserved")
expect(provider?.headers?.["X-Msh-Platform"]).toBe("kimi_code_cli")
expect(provider?.headers?.["X-Msh-Device-Id"]).toMatch(/^[0-9a-f-]+$/)
expect(provider?.headers?.["User-Agent"]).toStartWith("opencode/")
expect(provider?.headers?.["Content-Type"]).toBeUndefined()
}),
)
it.effect("leaves API-key connections unchanged", () =>
Effect.gen(function* () {
const credentials = yield* Credential.Service
const catalog = yield* Catalog.Service
yield* addProvider()
yield* credentials.create({
integrationID: Integration.ID.make("kimi-for-coding"),
value: Credential.Key.make({ type: "key", key: "sk-kimi" }),
})
yield* addPlugin()
const provider = yield* catalog.provider.get(providerID)
expect(provider?.package).toBe(ProviderV2.aisdk("@ai-sdk/anthropic"))
expect(provider?.headers).toEqual({ "X-Custom": "preserved" })
}),
)
})
+9 -7
View File
@@ -97,6 +97,7 @@ const plugins = Layer.mock(PluginSupervisor.Service, { flush: Effect.void })
const tools = Layer.mock(ToolRegistry.Service, {
snapshot: () =>
Effect.succeed({
codeModeInstructions: "Captured Code Mode catalog",
definitions: [ToolDefinition.make({ name: "lookup", description: "Lookup", inputSchema: { type: "object" } })],
execute: () => Effect.die(new Error("unused")),
}),
@@ -285,13 +286,14 @@ it.effect("generates from fresh settled Session context without durable mutation
expect(requests[0]?.system.map((part) => part.text)).toContain("Initial context")
expect(requests[0]?.http?.headers).toMatchObject({ "X-Session-Id": sessionID })
expect(requests[0]?.providerOptions).toMatchObject({ openai: { promptCacheKey: sessionID } })
expect(
requests[0]?.messages.flatMap((message) =>
message.role === "system"
? message.content.flatMap((content) => (content.type === "text" ? [content.text] : []))
: [],
),
).toEqual(["Changed context"])
const instructionUpdates = requests[0]?.messages.flatMap((message) =>
message.role === "system"
? message.content.flatMap((content) => (content.type === "text" ? [content.text] : []))
: [],
)
expect(instructionUpdates).toHaveLength(1)
expect(instructionUpdates?.[0]).toContain("Changed context")
expect(instructionUpdates?.[0]).toContain("Captured Code Mode catalog")
expect(userTexts(requests[0])).toEqual(["Existing durable context", "Summarize privately"])
expect(
requests[0]?.messages.flatMap((message) =>
@@ -533,6 +533,7 @@ describe("ToolRegistry", () => {
.pipe(Scope.provide(scope))
const toolSet = yield* service.snapshot()
const execute = toolSet.definitions.find((tool) => tool.name === "execute")
expect(toolSet.codeModeInstructions).toContain("tools.echo")
expect(execute?.description).toContain("confined Code Mode runtime")
expect(execute?.description).not.toContain("Echo text")
yield* Scope.close(scope, Exit.void)
+50
View File
@@ -43,6 +43,7 @@ import * as SessionRunnerLLM from "@opencode-ai/core/session/runner/llm"
import { SessionRunnerModel } from "@opencode-ai/core/session/runner/model"
import { SessionUsage } from "@opencode-ai/core/session/usage"
import { ToolRegistry } from "@opencode-ai/core/tool/registry"
import { CodeMode } from "@opencode-ai/core/codemode"
import { PluginSupervisor } from "@opencode-ai/core/plugin/supervisor"
import { PluginHooks } from "@opencode-ai/core/plugin/hooks"
import { SystemPromptPlugin } from "@opencode-ai/core/plugin/system-prompt"
@@ -368,6 +369,12 @@ const pluginSupervisor = Layer.succeed(
flush: Effect.suspend(() => pluginFlushHook),
}),
)
let codeModeMaterializations: ReadonlyArray<CodeMode.Materialization> = []
let codeModeMaterializationCount = 0
const codeMode = Layer.mock(CodeMode.Service, {
register: () => Effect.void,
materialize: () => Effect.sync(() => codeModeMaterializations[codeModeMaterializationCount++] ?? {}),
})
const promptCatalog = Layer.mock(Catalog.Service, {
provider: {
get: () => Effect.succeed(undefined),
@@ -405,6 +412,7 @@ const runnerLayer = AppNodeBuilder.build(SessionRunnerLLM.node, [
[McpInstructions.node, mcpInstructions],
[ToolOutputStore.node, toolOutputStore],
[PluginSupervisor.node, pluginSupervisor],
[CodeMode.node, codeMode],
])
const execution = Layer.effect(
SessionExecution.Service,
@@ -464,6 +472,7 @@ const it = testEffect(
[Config.node, config],
[ToolOutputStore.node, toolOutputStore],
[PluginSupervisor.node, pluginSupervisor],
[CodeMode.node, codeMode],
],
),
)
@@ -512,6 +521,8 @@ const setup = Effect.gen(function* () {
systemLoadHook = Effect.void
modelResolveHook = Effect.void
pluginFlushHook = Effect.void
codeModeMaterializations = []
codeModeMaterializationCount = 0
currentModel = model
skillBaselines.clear()
responses = undefined
@@ -823,6 +834,45 @@ const verifyPartialFlushOnInterruption = (kind: FragmentKind) =>
})
describe("SessionRunnerLLM", () => {
it.effect("uses one Code Mode materialization per request for instructions and execution", () =>
Effect.gen(function* () {
const executed: string[] = []
const execute = (name: string) =>
Tool.make({
description: `Execute ${name}`,
input: Schema.Struct({}),
output: Schema.String,
execute: () => Effect.sync(() => executed.push(name)).pipe(Effect.as({ output: name })),
})
const session = yield* setup
codeModeMaterializations = [
{ instructions: "Code Mode catalog A", tool: execute("A") },
{ instructions: "Code Mode catalog B", tool: execute("B") },
{ instructions: "Code Mode catalog C", tool: execute("C") },
{ instructions: "Code Mode catalog D", tool: execute("D") },
]
yield* admit(session, "Use Code Mode")
responses = [reply.tool("call-execute", "execute", {}), reply.stop()]
yield* session.resume(sessionID)
expect(requests).toHaveLength(2)
expect(codeModeMaterializationCount).toBe(2)
expect(requests[0]?.system.some((part) => part.text.includes("Code Mode catalog A"))).toBe(true)
expect(requests[0]?.system.some((part) => part.text.includes("Code Mode catalog B"))).toBe(false)
expect(requests[0]?.tools.find((tool) => tool.name === "execute")?.description).toBe("Execute A")
expect(executed).toEqual(["A"])
expect(requests[1]?.tools.find((tool) => tool.name === "execute")?.description).toBe("Execute B")
expect(
requests[1]?.messages.some(
(message) =>
message.role === "system" &&
message.content.some((part) => part.type === "text" && part.text.includes("Code Mode catalog B")),
),
).toBe(true)
}),
)
it.effect("applies session context hooks without exposing unavailable tools", () =>
Effect.gen(function* () {
const session = yield* setup