mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-18 07:12:47 +03:00
feat(core): configure session warming per provider and model (#60025)
This commit is contained in:
committed by
GitHub
Unverified
parent
9a611d36b9
commit
296074aab2
+3
-1
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
Generated
+30
@@ -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": [
|
||||
{
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
Reference in New Issue
Block a user