From 296074aab29138dd94af7d82e70b3513e28746ea Mon Sep 17 00:00:00 2001 From: Aiden Cline <63023139+rekram1-node@users.noreply.github.com> Date: Sat, 31 Oct 2026 21:05:18 -0500 Subject: [PATCH] feat(core): configure session warming per provider and model (#60025) --- .../client/src/promise/generated/types.ts | 4 ++- packages/core/src/plugin/warming.ts | 31 +++++++++++++------ packages/protocol/openapi.json | 30 ++++++++++++++++++ packages/schema/src/provider.ts | 14 +++++++++ 4 files changed, 68 insertions(+), 11 deletions(-) diff --git a/packages/client/src/promise/generated/types.ts b/packages/client/src/promise/generated/types.ts index 238da3f357b..7650139b5e7 100644 --- a/packages/client/src/promise/generated/types.ts +++ b/packages/client/src/promise/generated/types.ts @@ -25,6 +25,8 @@ export type ProviderCompaction = { type: "summary" } | { type: "native" } export type ProviderCacheTTL = `${number}${"ms" | "s" | "m" | "h" | "d"}` +export type ProviderCacheWarming = boolean | { interval?: "auto" | (string & {}); duration?: string } + export type ProviderTransport = "http" | "websocket" export type ProviderEncoding = "zstd" | "gzip" | "none" @@ -393,7 +395,7 @@ export type LocationPublicInfo = { export type ModelRef = { id: string; providerID: ProviderID; variant?: string } -export type ProviderCache = { ttl?: ProviderCacheTTL } +export type ProviderCache = { ttl?: ProviderCacheTTL; warming?: ProviderCacheWarming } export type ModelVariantSettings = { compaction?: ProviderCompaction; encoding?: ProviderEncoding } & { [x: string]: any diff --git a/packages/core/src/plugin/warming.ts b/packages/core/src/plugin/warming.ts index 780e6654beb..ff00c9bf3ca 100644 --- a/packages/core/src/plugin/warming.ts +++ b/packages/core/src/plugin/warming.ts @@ -4,10 +4,13 @@ import { define } from "@opencode/plugin/effect/plugin" import type { SessionHooks } from "@opencode/plugin/effect/session" import type { Provider } from "@opencode/schema/provider" import type { Session } from "@opencode/schema/session" -import { Clock, Duration, Effect, Scope, Stream } from "effect" +import { ConfigWarming } from "@opencode/schema/config/warming" +import { Clock, Duration, Effect, Option, Schema, Scope, Stream } from "effect" import { Config } from "../config.js" import { resolveModel } from "./optimize.js" +const decodeWarming = Schema.decodeUnknownOption(ConfigWarming.Warming) + const defaults = { prompt: "This is a keep-alive request. Do not perform any work or use tools. Reply with exactly: OK", interval: Duration.minutes(4), @@ -18,16 +21,24 @@ export const Plugin = define({ id: "opencode.warming", effect: Effect.fn(function* (ctx) { const config = yield* Config.Service - const modelTTL = (event: SessionHooks["context"]) => - resolveModel(ctx, event).pipe( - Effect.map((model) => model.settings?.cache?.ttl), - Effect.orElseSucceed(() => undefined), - ) + // A model's or provider's `cache.warming` overrides the top-level `warming`. const loadSettings = Effect.fn("WarmingPlugin.loadSettings")(function* (event: SessionHooks["context"]) { - const warming = Config.latest(yield* config.entries(), "warming") - if (!warming) return - const settings = warming === true ? defaults : { ...defaults, ...warming } - const interval = settings.interval === "auto" ? autoInterval(yield* modelTTL(event)) : settings.interval + // Warming must never fail the request it hooks, so an unreadable catalog falls back to the top-level setting. + const cache = (yield* resolveModel(ctx, event).pipe(Effect.orElseSucceed(() => undefined)))?.settings?.cache + const local = cache?.warming === undefined ? undefined : Option.getOrUndefined(decodeWarming(cache.warming)) + if (cache?.warming !== undefined && local === undefined) + yield* Effect.logWarning("invalid cache.warming setting; using the top-level warming setting", { + model: event.model, + }) + const global = Config.latest(yield* config.entries(), "warming") + if (!(local ?? global)) return + // The keep-alive prompt is only set at the top level; a model's setting changes timing. + const settings = { + ...defaults, + ...(typeof global === "object" ? global : {}), + ...(typeof local === "object" ? local : {}), + } + const interval = settings.interval === "auto" ? autoInterval(cache?.ttl) : settings.interval // "auto" doesn't warm a model without a cache.ttl. if (interval === undefined) return const { duration } = settings diff --git a/packages/protocol/openapi.json b/packages/protocol/openapi.json index 8a9f64ad2f5..c54cc9ebe00 100644 --- a/packages/protocol/openapi.json +++ b/packages/protocol/openapi.json @@ -21507,6 +21507,9 @@ "properties": { "ttl": { "$ref": "#/components/schemas/Provider.CacheTTL" + }, + "warming": { + "$ref": "#/components/schemas/Provider.CacheWarming" } }, "additionalProperties": false @@ -21515,6 +21518,33 @@ "type": "string", "pattern": "^[+-]?(?:\\d+(?:\\.\\d+)?|\\.\\d+)(?:[Ee][+-]?\\d+)?(?:ms|s|m|h|d)$" }, + "Provider.CacheWarming": { + "anyOf": [ + { + "type": "boolean" + }, + { + "type": "object", + "properties": { + "interval": { + "anyOf": [ + { + "type": "string", + "enum": ["auto"] + }, + { + "type": "string" + } + ] + }, + "duration": { + "type": "string" + } + }, + "additionalProperties": false + } + ] + }, "Provider.Compaction": { "anyOf": [ { diff --git a/packages/schema/src/provider.ts b/packages/schema/src/provider.ts index 9563aad5c14..9985e7402cf 100644 --- a/packages/schema/src/provider.ts +++ b/packages/schema/src/provider.ts @@ -53,9 +53,23 @@ export const CacheTTL = Schema.TemplateLiteral([Schema.Number, Schema.Literals([ .annotate({ identifier: "Provider.CacheTTL" }) export type CacheTTL = typeof CacheTTL.Type +/** Keep-alive requests for this provider's or model's sessions; overrides the top-level `warming`. */ +export const CacheWarming = Schema.Union([ + Schema.Boolean, + Schema.Struct({ + // Settings hold JSON, so durations stay strings here; the warming plugin parses them. + interval: Schema.Union([Schema.Literal("auto"), Schema.String]) + .pipe(optional) + .annotate({ description: 'Duration such as "4 minutes", or "auto" for 90% of the model\'s cache.ttl' }), + duration: Schema.String.pipe(optional).annotate({ description: 'Duration such as "30 minutes"' }), + }), +]).annotate({ identifier: "Provider.CacheWarming" }) +export type CacheWarming = typeof CacheWarming.Type + /** Prompt-cache settings. `ttl` is sent to the provider as given; each provider accepts its own set. */ export const Cache = Schema.Struct({ ttl: CacheTTL.pipe(optional), + warming: CacheWarming.pipe(optional), }).annotate({ identifier: "Provider.Cache" }) export type Cache = typeof Cache.Type