Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@ POST https://api.githubcopilot.com/auto
model and mints a 24-hour session token. The conversation stays on that model, so its
prompt cache stays warm; every request carries `Copilot-Session-Token`, which bills it
as Auto (10% discount on paid plans).
- **Across reloads.** opencode reloads plugins when the config changes and recycles idle
instances. The route survives that: it is kept, with its session token, in
`routes.json` next to the plugin log (owner-only), until the token expires. A restored
route the auto can no longer send (another anchor or catalog) is routed again.
- **Routing again** happens, as in VS Code, on the next turn after compaction, when the
token is within 5 minutes of expiring, when a turn attaches an image the model cannot
read, and when you switch tier.
Expand Down
151 changes: 142 additions & 9 deletions src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// call, which autos to inject and which server pick an auto can actually send.
// Mirrors VS Code's Copilot Auto (extensions/copilot/src/platform/endpoint in
// microsoft/vscode: automodeService.ts, autoV2Fetcher.ts, autoModeTiers.ts).
import { appendFile, mkdir } from "node:fs/promises"
import { appendFile, mkdir, readFile, rename, rm, writeFile } from "node:fs/promises"
import { homedir } from "node:os"
import path from "node:path"

Expand Down Expand Up @@ -93,18 +93,20 @@ export type Route = {
expiresAtMs: number
}

// --- state directory --------------------------------------------------------------------
// ~/.local/state/opencode-copilot-auto (or $XDG_STATE_HOME, %LOCALAPPDATA% on Windows):
// the log and the persisted routes. Resolved per call, so tests can move it.
function stateDir() {
if (process.platform === "win32")
return path.join(process.env.LOCALAPPDATA ?? path.join(homedir(), "AppData", "Local"), PLUGIN_ID)
return path.join(process.env.XDG_STATE_HOME ?? path.join(homedir(), ".local", "state"), PLUGIN_ID)
}

// --- logging ----------------------------------------------------------------------------
// File logger — keeps the TUI clean.
// Tail ~/.local/state/opencode-copilot-auto/plugin.log to observe.
function logFile() {
if (process.platform === "win32")
return path.join(
process.env.LOCALAPPDATA ?? path.join(homedir(), "AppData", "Local"),
PLUGIN_ID,
"plugin.log",
)
const xdg = process.env.XDG_STATE_HOME
return path.join(xdg ?? path.join(homedir(), ".local", "state"), PLUGIN_ID, "plugin.log")
return path.join(stateDir(), "plugin.log")
}

let logReady: Promise<void> | undefined
Expand Down Expand Up @@ -278,3 +280,134 @@ export async function fetchAutoDecision(
clearTimeout(timer)
}
}

// --- conversations ----------------------------------------------------------------------
// Everything tracked per opencode session and auto: the pinned route plus the inputs
// for the next routing round.
export type ConvState = {
route?: Route
routing?: Promise<Route>
needsReEval?: boolean
// The prompt a routing round last failed for (no Auto session); the next attempt
// waits for the next user prompt instead of stalling every tool step.
failedFor?: object
}

// What survives a reload: the route (with its session token, so the conversation
// stays billed against its Auto session) and a pending re-evaluation.
type StoredConv = Route & { needsReEval?: boolean }
type StoredSessions = Record<string, Record<string, StoredConv>>

// opencode reloads plugins whenever the config changes and recycles idle instances,
// which used to drop every pinned route: the next turn routed again on whatever the
// user typed last and could switch the model mid-conversation, losing its prompt
// cache. VS Code keeps the route per conversation; so do we, across reloads, in
// routes.json next to the log. Entries live as long as their session token (24h).
function routesFile() {
return path.join(stateDir(), "routes.json")
}

function durable(route: Route | undefined, now = Date.now()): route is Route {
return !!route && route.expiresAtMs > now
}

async function readRoutes(file: string): Promise<StoredSessions> {
try {
const data = JSON.parse(await readFile(file, "utf8")) as { sessions?: unknown }
return data.sessions && typeof data.sessions === "object" ? (data.sessions as StoredSessions) : {}
} catch {
return {}
}
}

// Writes from this process run one at a time; each re-reads the file, so instances
// for other directories (or other opencode processes) keep their entries.
let routeWrites: Promise<void> = Promise.resolve()

function writeRoute(sessionID: string, autoID: string, entry: StoredConv | undefined): Promise<void> {
const file = routesFile()
const write = async () => {
const sessions = await readRoutes(file)
const now = Date.now()
if (entry) (sessions[sessionID] ??= {})[autoID] = entry
else if (sessions[sessionID]) delete sessions[sessionID][autoID]
for (const [id, autos] of Object.entries(sessions)) {
for (const [auto, stored] of Object.entries(autos)) if (!durable(stored, now)) delete autos[auto]
if (!Object.keys(autos).length) delete sessions[id]
}
await mkdir(path.dirname(file), { recursive: true })
// Holds session tokens: owner-only, replaced atomically.
const tmp = `${file}.${process.pid}.${Math.random().toString(36).slice(2)}.tmp`
try {
await writeFile(tmp, JSON.stringify({ version: 1, sessions }), { mode: 0o600 })
await rename(tmp, file)
} catch (error) {
await rm(tmp, { force: true }).catch(() => undefined)
throw error
}
}
routeWrites = routeWrites.then(write).catch((error) => log(`route store write failed: ${String(error)}`))
return routeWrites
}

export class Conversations {
private live = new Map<string, Map<string, ConvState>>()
private stored: StoredSessions = {}

// Reads the routes persisted by earlier instances. Never throws.
async load() {
this.stored = await readRoutes(routesFile())
}

// The conversation of `auto` in a session. A route persisted before a reload comes
// back while its token is valid and the auto can still send its model (the anchor
// or catalog may have changed since).
get(sessionID: string, auto: AutoSpec, catalog: CatalogModel[]): ConvState {
let sessionAutos = this.live.get(sessionID)
if (!sessionAutos) {
sessionAutos = new Map()
this.live.set(sessionID, sessionAutos)
}
let conv = sessionAutos.get(auto.id)
if (conv) return conv
conv = {}
const stored = this.stored[sessionID]?.[auto.id]
if (stored) {
delete this.stored[sessionID][auto.id]
const { needsReEval, ...route } = stored
if (durable(route) && servable(auto, route.modelID, catalog)) {
conv.route = route
conv.needsReEval = needsReEval
log(`${auto.id}: restored ${route.modelID} for ${sessionID}`)
}
}
sessionAutos.set(auto.id, conv)
return conv
}

// Persists a conversation after a routing round or an invalidation.
save(sessionID: string, autoID: string, conv: ConvState): Promise<void> {
const route = conv.route
return writeRoute(
sessionID,
autoID,
durable(route) ? { ...route, ...(conv.needsReEval ? { needsReEval: true } : {}) } : undefined,
)
}

// Marks every auto of a session for re-routing on its next agent turn, including
// routes restored from disk but not used yet. Returns whether there was any.
async invalidate(sessionID: string): Promise<boolean> {
const writes: Promise<void>[] = []
for (const [autoID, conv] of this.live.get(sessionID) ?? []) {
conv.needsReEval = true
writes.push(this.save(sessionID, autoID, conv))
}
for (const [autoID, stored] of Object.entries(this.stored[sessionID] ?? {})) {
stored.needsReEval = true
writes.push(writeRoute(sessionID, autoID, stored))
}
await Promise.all(writes)
return writes.length > 0
}
}
42 changes: 10 additions & 32 deletions src/v1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type { Model } from "@opencode-ai/sdk/v2"
import {
type AutoSpec,
type CatalogModel,
Conversations,
type Options,
PLUGIN_ID,
PROVIDER_ID,
Expand All @@ -18,17 +19,6 @@ import {
routeUsable,
} from "./core"

// Everything tracked per opencode session and auto: the pinned route plus whether the
// next agent turn has to route again.
type ConvState = {
route?: Route
routing?: Promise<Route>
needsReEval?: boolean
// The prompt a routing round last failed for (no Auto session); the next attempt
// waits for the next user prompt instead of stalling every tool step.
failedFor?: object
}

// opencode's github-copilot provider sends GPT-5+ (except gpt-5-mini) over the
// Responses API and everything else over Chat Completions; Claude goes through the
// Anthropic SDK.
Expand Down Expand Up @@ -107,23 +97,12 @@ export const server: Plugin = async ({ client }, options) => {
let catalog: CatalogModel[] = []
let autos = new Map<string, AutoSpec>()
const prompts = new Map<string, { text: string; hasImage: boolean }>()
const conversations = new Map<string, Map<string, ConvState>>()
const conversations = new Conversations()
await conversations.load()

const modelByAPIID = (apiID: string) => models.find((m) => m.api.id === apiID)

function getConv(sessionID: string, autoID: string): ConvState {
let sessionAutos = conversations.get(sessionID)
if (!sessionAutos) {
sessionAutos = new Map()
conversations.set(sessionID, sessionAutos)
}
let conv = sessionAutos.get(autoID)
if (!conv) {
conv = {}
sessionAutos.set(autoID, conv)
}
return conv
}
const getConv = (sessionID: string, auto: AutoSpec) => conversations.get(sessionID, auto, catalog)

// One routing round through POST /auto.
async function resolveRoute(sessionID: string, auto: AutoSpec, hasImage: boolean): Promise<Route> {
Expand All @@ -135,7 +114,7 @@ export const server: Plugin = async ({ client }, options) => {
: undefined
// As VS Code: when routing fails, keep the last route while it can still serve
// the turn, and route again on the next one.
const previous = getConv(sessionID, auto.id).route
const previous = getConv(sessionID, auto).route
if (!session && previous?.sessionToken && routeUsable(previous, catalog, hasImage)) {
log(`${auto.id}: routing failed, keeping ${previous.modelID} for ${sessionID}`)
return previous
Expand All @@ -160,10 +139,8 @@ export const server: Plugin = async ({ client }, options) => {
// turn (mirrors VS Code's invalidateRouterCache).
event: async ({ event }) => {
if (event.type !== "session.compacted") return
const sessionAutos = conversations.get(event.properties.sessionID)
if (!sessionAutos?.size) return
for (const conv of sessionAutos.values()) conv.needsReEval = true
log(`route invalidated by compaction for ${event.properties.sessionID}`)
const sessionID = event.properties.sessionID
if (await conversations.invalidate(sessionID)) log(`route invalidated by compaction for ${sessionID}`)
},
// Capture what the user typed; only POST /auto sees it. Synthetic parts (file
// contents, reminders) are opencode's additions, not the user's prompt.
Expand All @@ -181,7 +158,7 @@ export const server: Plugin = async ({ client }, options) => {
if (incoming.model.providerID !== PROVIDER_ID) return
const auto = autos.get(incoming.model.id)
if (!auto) return
const conv = getConv(incoming.sessionID, auto.id)
const conv = getConv(incoming.sessionID, auto)
// Route once per conversation and stick with it so the model — and its prompt
// KV cache — stays stable, as VS Code does. Route again after compaction, when
// the token is about to expire, when the turn attaches an image the model
Expand All @@ -204,6 +181,7 @@ export const server: Plugin = async ({ client }, options) => {
if (next !== route) conv.needsReEval = false
conv.failedFor = next.sessionToken && next !== route ? undefined : prompt
conv.route = next
await conversations.save(incoming.sessionID, auto.id, conv)
}

// The model itself is swapped in by the fetch wrapper (see chat.headers).
Expand All @@ -221,7 +199,7 @@ export const server: Plugin = async ({ client }, options) => {
if (incoming.model.providerID !== PROVIDER_ID) return
const auto = autos.get(incoming.model.id)
if (!auto) return
const route = getConv(incoming.sessionID, auto.id).route
const route = getConv(incoming.sessionID, auto).route
if (!route) return
output.headers[ROUTE_HEADER] = route.modelID
if (route.sessionToken) output.headers["Copilot-Session-Token"] = route.sessionToken
Expand Down
41 changes: 10 additions & 31 deletions src/v2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
type AutoSpec,
type CatalogModel,
type ConvKind,
Conversations,
DEFAULT_BASE_URL,
type Options,
PROVIDER_ID,
Expand Down Expand Up @@ -32,17 +33,6 @@ type CopilotModel = {
capabilities?: { input?: readonly string[] }
}

// Everything tracked per opencode session and auto: the pinned route plus the
// inputs for the next routing round.
type ConvState = {
route?: Route
routing?: Promise<Route>
needsReEval?: boolean
// The prompt a routing round last failed for (no Auto session); the next attempt
// waits for the next user prompt instead of stalling every tool step.
failedFor?: object
}

// opencode's Copilot provider sends GPT-5+ (except gpt-5-mini), Grok and MAI Code
// over the Responses API and the rest over Chat Completions, unless the live model
// list advertised an endpoint (settings.endpoint).
Expand Down Expand Up @@ -104,21 +94,10 @@ export const setup: Plugin.Plugin["setup"] = async (ctx) => {
let autos = new Map<string, AutoSpec>()
let injectedSignature = ""
const prompts = new Map<string, { text: string; hasImage: boolean }>()
const conversations = new Map<string, Map<string, ConvState>>()
const conversations = new Conversations()
await conversations.load()

function getConv(sessionID: string, autoID: string): ConvState {
let sessionAutos = conversations.get(sessionID)
if (!sessionAutos) {
sessionAutos = new Map()
conversations.set(sessionID, sessionAutos)
}
let conv = sessionAutos.get(autoID)
if (!conv) {
conv = {}
sessionAutos.set(autoID, conv)
}
return conv
}
const getConv = (sessionID: string, auto: AutoSpec) => conversations.get(sessionID, auto, catalog)

// One routing round through POST /auto.
async function resolveRoute(sessionID: string, auto: AutoSpec, hasImage: boolean): Promise<Route> {
Expand All @@ -130,7 +109,7 @@ export const setup: Plugin.Plugin["setup"] = async (ctx) => {
: undefined
// As VS Code: when routing fails, keep the last route while it can still serve
// the turn, and route again on the next one.
const previous = getConv(sessionID, auto.id).route
const previous = getConv(sessionID, auto).route
if (!session && previous?.sessionToken && routeUsable(previous, catalog, hasImage)) {
log(`${auto.id}: routing failed, keeping ${previous.modelID} for ${sessionID}`)
return previous
Expand All @@ -146,7 +125,7 @@ export const setup: Plugin.Plugin["setup"] = async (ctx) => {
// compaction, when the token is about to expire, when the turn attaches an image
// the model cannot read, or after a failed round.
async function ensureRoute(sessionID: string, auto: AutoSpec, kind: ConvKind): Promise<Route> {
const conv = getConv(sessionID, auto.id)
const conv = getConv(sessionID, auto)
const prompt = prompts.get(sessionID)
const hasImage = kind === "primary" && !!prompt?.hasImage
const route = conv.route
Expand All @@ -163,6 +142,7 @@ export const setup: Plugin.Plugin["setup"] = async (ctx) => {
if (next !== route && kind === "primary") conv.needsReEval = false
conv.failedFor = next.sessionToken && next !== route ? undefined : prompt
conv.route = next
await conversations.save(sessionID, auto.id, conv)
return next
}

Expand Down Expand Up @@ -209,10 +189,9 @@ export const setup: Plugin.Plugin["setup"] = async (ctx) => {
// keeps the current route.
await ctx.session.hook(
"compaction",
(event) => {
async (event) => {
if (!autos.has(event.model.id)) return
for (const conv of conversations.get(event.sessionID)?.values() ?? []) conv.needsReEval = true
log(`route invalidated by compaction for ${event.sessionID}`)
if (await conversations.invalidate(event.sessionID)) log(`route invalidated by compaction for ${event.sessionID}`)
},
{ providerID: PROVIDER_ID },
)
Expand All @@ -236,7 +215,7 @@ export const setup: Plugin.Plugin["setup"] = async (ctx) => {
async (event) => {
const auto = autos.get(event.model.id)
if (!auto) return
const modelID = conversations.get(event.sessionID)?.get(auto.id)?.route?.modelID
const modelID = getConv(event.sessionID, auto).route?.modelID
if (!modelID) return
const text = await event.request.clone().text().catch(() => "")
let body: Record<string, unknown>
Expand Down
Loading
Loading