Merge branch 'dev' into sqlite2
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"$schema": "https://json.schemastore.org/package.json",
|
||||
"version": "1.1.51",
|
||||
"version": "1.1.52",
|
||||
"name": "opencode",
|
||||
"type": "module",
|
||||
"license": "MIT",
|
||||
@@ -53,7 +53,7 @@
|
||||
"dependencies": {
|
||||
"@actions/core": "1.11.1",
|
||||
"@actions/github": "6.0.1",
|
||||
"@agentclientprotocol/sdk": "0.13.0",
|
||||
"@agentclientprotocol/sdk": "0.14.1",
|
||||
"@ai-sdk/amazon-bedrock": "3.0.74",
|
||||
"@ai-sdk/anthropic": "2.0.58",
|
||||
"@ai-sdk/azure": "2.0.91",
|
||||
|
||||
@@ -25,6 +25,7 @@ import {
|
||||
type SetSessionModeResponse,
|
||||
type ToolCallContent,
|
||||
type ToolKind,
|
||||
type Usage,
|
||||
} from "@agentclientprotocol/sdk"
|
||||
|
||||
import { Log } from "../util/log"
|
||||
@@ -38,7 +39,7 @@ import { Config } from "@/config/config"
|
||||
import { Todo } from "@/session/todo"
|
||||
import { z } from "zod"
|
||||
import { LoadAPIKeyError } from "ai"
|
||||
import type { Event, OpencodeClient, SessionMessageResponse } from "@opencode-ai/sdk/v2"
|
||||
import type { AssistantMessage, Event, OpencodeClient, SessionMessageResponse } from "@opencode-ai/sdk/v2"
|
||||
import { applyPatch } from "diff"
|
||||
|
||||
type ModeOption = { id: string; name: string; description?: string }
|
||||
@@ -49,6 +50,74 @@ const DEFAULT_VARIANT_VALUE = "default"
|
||||
export namespace ACP {
|
||||
const log = Log.create({ service: "acp-agent" })
|
||||
|
||||
async function getContextLimit(
|
||||
sdk: OpencodeClient,
|
||||
providerID: string,
|
||||
modelID: string,
|
||||
directory: string,
|
||||
): Promise<number | null> {
|
||||
const providers = await sdk.config
|
||||
.providers({ directory })
|
||||
.then((x) => x.data?.providers ?? [])
|
||||
.catch((error) => {
|
||||
log.error("failed to get providers for context limit", { error })
|
||||
return []
|
||||
})
|
||||
|
||||
const provider = providers.find((p) => p.id === providerID)
|
||||
const model = provider?.models[modelID]
|
||||
return model?.limit.context ?? null
|
||||
}
|
||||
|
||||
async function sendUsageUpdate(
|
||||
connection: AgentSideConnection,
|
||||
sdk: OpencodeClient,
|
||||
sessionID: string,
|
||||
directory: string,
|
||||
): Promise<void> {
|
||||
const messages = await sdk.session
|
||||
.messages({ sessionID, directory }, { throwOnError: true })
|
||||
.then((x) => x.data)
|
||||
.catch((error) => {
|
||||
log.error("failed to fetch messages for usage update", { error })
|
||||
return undefined
|
||||
})
|
||||
|
||||
if (!messages) return
|
||||
|
||||
const assistantMessages = messages.filter(
|
||||
(m): m is { info: AssistantMessage; parts: SessionMessageResponse["parts"] } => m.info.role === "assistant",
|
||||
)
|
||||
|
||||
const lastAssistant = assistantMessages[assistantMessages.length - 1]
|
||||
if (!lastAssistant) return
|
||||
|
||||
const msg = lastAssistant.info
|
||||
const size = await getContextLimit(sdk, msg.providerID, msg.modelID, directory)
|
||||
|
||||
if (!size) {
|
||||
// Cannot calculate usage without known context size
|
||||
return
|
||||
}
|
||||
|
||||
const used = msg.tokens.input + (msg.tokens.cache?.read ?? 0)
|
||||
const totalCost = assistantMessages.reduce((sum, m) => sum + m.info.cost, 0)
|
||||
|
||||
await connection
|
||||
.sessionUpdate({
|
||||
sessionId: sessionID,
|
||||
update: {
|
||||
sessionUpdate: "usage_update",
|
||||
used,
|
||||
size,
|
||||
cost: { amount: totalCost, currency: "USD" },
|
||||
},
|
||||
})
|
||||
.catch((error) => {
|
||||
log.error("failed to send usage update", { error })
|
||||
})
|
||||
}
|
||||
|
||||
export async function init({ sdk: _sdk }: { sdk: OpencodeClient }) {
|
||||
return {
|
||||
create: (connection: AgentSideConnection, fullConfig: ACPConfig) => {
|
||||
@@ -568,6 +637,8 @@ export namespace ACP {
|
||||
await this.processMessage(msg)
|
||||
}
|
||||
|
||||
await sendUsageUpdate(this.connection, this.sdk, sessionId, directory)
|
||||
|
||||
return result
|
||||
} catch (e) {
|
||||
const error = MessageV2.fromError(e, {
|
||||
@@ -676,6 +747,8 @@ export namespace ACP {
|
||||
await this.processMessage(msg)
|
||||
}
|
||||
|
||||
await sendUsageUpdate(this.connection, this.sdk, sessionId, directory)
|
||||
|
||||
return mode
|
||||
} catch (e) {
|
||||
const error = MessageV2.fromError(e, {
|
||||
@@ -699,11 +772,15 @@ export namespace ACP {
|
||||
|
||||
log.info("resume_session", { sessionId, mcpServers: mcpServers.length })
|
||||
|
||||
return this.loadSessionMode({
|
||||
const result = await this.loadSessionMode({
|
||||
cwd: directory,
|
||||
mcpServers,
|
||||
sessionId,
|
||||
})
|
||||
|
||||
await sendUsageUpdate(this.connection, this.sdk, sessionId, directory)
|
||||
|
||||
return result
|
||||
} catch (e) {
|
||||
const error = MessageV2.fromError(e, {
|
||||
providerID: this.config.defaultModel?.providerID ?? "unknown",
|
||||
@@ -1261,13 +1338,22 @@ export namespace ACP {
|
||||
return { name, args: rest.join(" ").trim() }
|
||||
})()
|
||||
|
||||
const done = {
|
||||
stopReason: "end_turn" as const,
|
||||
_meta: {},
|
||||
}
|
||||
const buildUsage = (msg: AssistantMessage): Usage => ({
|
||||
totalTokens:
|
||||
msg.tokens.input +
|
||||
msg.tokens.output +
|
||||
msg.tokens.reasoning +
|
||||
(msg.tokens.cache?.read ?? 0) +
|
||||
(msg.tokens.cache?.write ?? 0),
|
||||
inputTokens: msg.tokens.input,
|
||||
outputTokens: msg.tokens.output,
|
||||
thoughtTokens: msg.tokens.reasoning || undefined,
|
||||
cachedReadTokens: msg.tokens.cache?.read || undefined,
|
||||
cachedWriteTokens: msg.tokens.cache?.write || undefined,
|
||||
})
|
||||
|
||||
if (!cmd) {
|
||||
await this.sdk.session.prompt({
|
||||
const response = await this.sdk.session.prompt({
|
||||
sessionID,
|
||||
model: {
|
||||
providerID: model.providerID,
|
||||
@@ -1278,14 +1364,22 @@ export namespace ACP {
|
||||
agent,
|
||||
directory,
|
||||
})
|
||||
return done
|
||||
const msg = response.data?.info
|
||||
|
||||
await sendUsageUpdate(this.connection, this.sdk, sessionID, directory)
|
||||
|
||||
return {
|
||||
stopReason: "end_turn" as const,
|
||||
usage: msg ? buildUsage(msg) : undefined,
|
||||
_meta: {},
|
||||
}
|
||||
}
|
||||
|
||||
const command = await this.config.sdk.command
|
||||
.list({ directory }, { throwOnError: true })
|
||||
.then((x) => x.data!.find((c) => c.name === cmd.name))
|
||||
if (command) {
|
||||
await this.sdk.session.command({
|
||||
const response = await this.sdk.session.command({
|
||||
sessionID,
|
||||
command: command.name,
|
||||
arguments: cmd.args,
|
||||
@@ -1293,7 +1387,15 @@ export namespace ACP {
|
||||
agent,
|
||||
directory,
|
||||
})
|
||||
return done
|
||||
const msg = response.data?.info
|
||||
|
||||
await sendUsageUpdate(this.connection, this.sdk, sessionID, directory)
|
||||
|
||||
return {
|
||||
stopReason: "end_turn" as const,
|
||||
usage: msg ? buildUsage(msg) : undefined,
|
||||
_meta: {},
|
||||
}
|
||||
}
|
||||
|
||||
switch (cmd.name) {
|
||||
@@ -1310,7 +1412,12 @@ export namespace ACP {
|
||||
break
|
||||
}
|
||||
|
||||
return done
|
||||
await sendUsageUpdate(this.connection, this.sdk, sessionID, directory)
|
||||
|
||||
return {
|
||||
stopReason: "end_turn" as const,
|
||||
_meta: {},
|
||||
}
|
||||
}
|
||||
|
||||
async cancel(params: CancelNotification) {
|
||||
|
||||
@@ -154,7 +154,9 @@ function AutoMethod(props: AutoMethodProps) {
|
||||
<text attributes={TextAttributes.BOLD} fg={theme.text}>
|
||||
{props.title}
|
||||
</text>
|
||||
<text fg={theme.textMuted}>esc</text>
|
||||
<text fg={theme.textMuted} onMouseUp={() => dialog.clear()}>
|
||||
esc
|
||||
</text>
|
||||
</box>
|
||||
<box gap={1}>
|
||||
<Link href={props.authorization.url} fg={theme.primary} />
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import { TextAttributes } from "@opentui/core"
|
||||
import { useTheme } from "../context/theme"
|
||||
import { useDialog } from "@tui/ui/dialog"
|
||||
import { useSync } from "@tui/context/sync"
|
||||
import { For, Match, Switch, Show, createMemo } from "solid-js"
|
||||
import { Installation } from "@/installation"
|
||||
@@ -9,6 +10,7 @@ export type DialogStatusProps = {}
|
||||
export function DialogStatus() {
|
||||
const sync = useSync()
|
||||
const { theme } = useTheme()
|
||||
const dialog = useDialog()
|
||||
|
||||
const enabledFormatters = createMemo(() => sync.data.formatter.filter((f) => f.enabled))
|
||||
|
||||
@@ -43,7 +45,9 @@ export function DialogStatus() {
|
||||
<text fg={theme.text} attributes={TextAttributes.BOLD}>
|
||||
Status
|
||||
</text>
|
||||
<text fg={theme.textMuted}>esc</text>
|
||||
<text fg={theme.textMuted} onMouseUp={() => dialog.clear()}>
|
||||
esc
|
||||
</text>
|
||||
</box>
|
||||
<text fg={theme.textMuted}>OpenCode v{Installation.VERSION}</text>
|
||||
<Show when={Object.keys(sync.data.mcp).length > 0} fallback={<text fg={theme.text}>No MCP Servers</text>}>
|
||||
|
||||
@@ -1625,6 +1625,7 @@ function BlockTool(props: {
|
||||
function Bash(props: ToolProps<typeof BashTool>) {
|
||||
const { theme } = useTheme()
|
||||
const sync = useSync()
|
||||
const isRunning = createMemo(() => props.part.state.status === "running")
|
||||
const output = createMemo(() => stripAnsi(props.metadata.output?.trim() ?? ""))
|
||||
const [expanded, setExpanded] = createSignal(false)
|
||||
const lines = createMemo(() => output().split("\n"))
|
||||
@@ -1665,6 +1666,7 @@ function Bash(props: ToolProps<typeof BashTool>) {
|
||||
<BlockTool
|
||||
title={title()}
|
||||
part={props.part}
|
||||
spinner={isRunning()}
|
||||
onClick={overflow() ? () => setExpanded((prev) => !prev) : undefined}
|
||||
>
|
||||
<box gap={1}>
|
||||
|
||||
@@ -25,7 +25,9 @@ export function DialogAlert(props: DialogAlertProps) {
|
||||
<text attributes={TextAttributes.BOLD} fg={theme.text}>
|
||||
{props.title}
|
||||
</text>
|
||||
<text fg={theme.textMuted}>esc</text>
|
||||
<text fg={theme.textMuted} onMouseUp={() => dialog.clear()}>
|
||||
esc
|
||||
</text>
|
||||
</box>
|
||||
<box paddingBottom={1}>
|
||||
<text fg={theme.textMuted}>{props.message}</text>
|
||||
|
||||
@@ -37,7 +37,9 @@ export function DialogConfirm(props: DialogConfirmProps) {
|
||||
<text attributes={TextAttributes.BOLD} fg={theme.text}>
|
||||
{props.title}
|
||||
</text>
|
||||
<text fg={theme.textMuted}>esc</text>
|
||||
<text fg={theme.textMuted} onMouseUp={() => dialog.clear()}>
|
||||
esc
|
||||
</text>
|
||||
</box>
|
||||
<box paddingBottom={1}>
|
||||
<text fg={theme.textMuted}>{props.message}</text>
|
||||
|
||||
@@ -80,7 +80,9 @@ export function DialogExportOptions(props: DialogExportOptionsProps) {
|
||||
<text attributes={TextAttributes.BOLD} fg={theme.text}>
|
||||
Export Options
|
||||
</text>
|
||||
<text fg={theme.textMuted}>esc</text>
|
||||
<text fg={theme.textMuted} onMouseUp={() => dialog.clear()}>
|
||||
esc
|
||||
</text>
|
||||
</box>
|
||||
<box gap={1}>
|
||||
<box>
|
||||
|
||||
@@ -21,7 +21,9 @@ export function DialogHelp() {
|
||||
<text attributes={TextAttributes.BOLD} fg={theme.text}>
|
||||
Help
|
||||
</text>
|
||||
<text fg={theme.textMuted}>esc/enter</text>
|
||||
<text fg={theme.textMuted} onMouseUp={() => dialog.clear()}>
|
||||
esc/enter
|
||||
</text>
|
||||
</box>
|
||||
<box paddingBottom={1}>
|
||||
<text fg={theme.textMuted}>
|
||||
|
||||
@@ -39,7 +39,9 @@ export function DialogPrompt(props: DialogPromptProps) {
|
||||
<text attributes={TextAttributes.BOLD} fg={theme.text}>
|
||||
{props.title}
|
||||
</text>
|
||||
<text fg={theme.textMuted}>esc</text>
|
||||
<text fg={theme.textMuted} onMouseUp={() => dialog.clear()}>
|
||||
esc
|
||||
</text>
|
||||
</box>
|
||||
<box gap={1}>
|
||||
{props.description}
|
||||
|
||||
@@ -226,7 +226,9 @@ export function DialogSelect<T>(props: DialogSelectProps<T>) {
|
||||
<text fg={theme.text} attributes={TextAttributes.BOLD}>
|
||||
{props.title}
|
||||
</text>
|
||||
<text fg={theme.textMuted}>esc</text>
|
||||
<text fg={theme.textMuted} onMouseUp={() => dialog.clear()}>
|
||||
esc
|
||||
</text>
|
||||
</box>
|
||||
<box paddingTop={1}>
|
||||
<input
|
||||
|
||||
@@ -3,6 +3,7 @@ import { Log } from "../util/log"
|
||||
import { Installation } from "../installation"
|
||||
import { Auth, OAUTH_DUMMY_KEY } from "../auth"
|
||||
import os from "os"
|
||||
import { ProviderTransform } from "@/provider/transform"
|
||||
|
||||
const log = Log.create({ service: "plugin.codex" })
|
||||
|
||||
@@ -361,6 +362,7 @@ export async function CodexAuthPlugin(input: PluginInput): Promise<Hooks> {
|
||||
"gpt-5.1-codex-mini",
|
||||
"gpt-5.2",
|
||||
"gpt-5.2-codex",
|
||||
"gpt-5.3-codex",
|
||||
"gpt-5.1-codex",
|
||||
])
|
||||
for (const modelId of Object.keys(provider.models)) {
|
||||
@@ -369,6 +371,38 @@ export async function CodexAuthPlugin(input: PluginInput): Promise<Hooks> {
|
||||
}
|
||||
}
|
||||
|
||||
if (!provider.models["gpt-5.3-codex"]) {
|
||||
const model = {
|
||||
id: "gpt-5.3-codex",
|
||||
providerID: "openai",
|
||||
api: {
|
||||
id: "gpt-5.3-codex",
|
||||
url: "https://chatgpt.com/backend-api/codex",
|
||||
npm: "@ai-sdk/openai",
|
||||
},
|
||||
name: "GPT-5.3 Codex",
|
||||
capabilities: {
|
||||
temperature: false,
|
||||
reasoning: true,
|
||||
attachment: true,
|
||||
toolcall: true,
|
||||
input: { text: true, audio: false, image: true, video: false, pdf: false },
|
||||
output: { text: true, audio: false, image: false, video: false, pdf: false },
|
||||
interleaved: false,
|
||||
},
|
||||
cost: { input: 0, output: 0, cache: { read: 0, write: 0 } },
|
||||
limit: { context: 400_000, input: 272_000, output: 128_000 },
|
||||
status: "active" as const,
|
||||
options: {},
|
||||
headers: {},
|
||||
release_date: "2026-02-05",
|
||||
variants: {} as Record<string, Record<string, any>>,
|
||||
family: "gpt-codex",
|
||||
}
|
||||
model.variants = ProviderTransform.variants(model)
|
||||
provider.models["gpt-5.3-codex"] = model
|
||||
}
|
||||
|
||||
// Zero out costs for Codex (included with ChatGPT subscription)
|
||||
for (const model of Object.values(provider.models)) {
|
||||
model.cost = {
|
||||
|
||||
@@ -301,17 +301,20 @@ export async function CopilotAuthPlugin(input: PluginInput): Promise<Hooks> {
|
||||
},
|
||||
],
|
||||
},
|
||||
"chat.headers": async (input, output) => {
|
||||
if (!input.model.providerID.includes("github-copilot")) return
|
||||
"chat.headers": async (incoming, output) => {
|
||||
if (!incoming.model.providerID.includes("github-copilot")) return
|
||||
|
||||
if (input.model.api.npm === "@ai-sdk/anthropic") {
|
||||
if (incoming.model.api.npm === "@ai-sdk/anthropic") {
|
||||
output.headers["anthropic-beta"] = "interleaved-thinking-2025-05-14"
|
||||
}
|
||||
|
||||
const session = await sdk.session
|
||||
.get({
|
||||
path: {
|
||||
id: input.sessionID,
|
||||
id: incoming.sessionID,
|
||||
},
|
||||
query: {
|
||||
directory: input.directory,
|
||||
},
|
||||
throwOnError: true,
|
||||
})
|
||||
|
||||
@@ -6,13 +6,18 @@ import { createOpencodeClient } from "@opencode-ai/sdk"
|
||||
import { Server } from "../server/server"
|
||||
import { BunProc } from "../bun"
|
||||
import { Instance } from "../project/instance"
|
||||
import { Flag } from "../flag/flag"
|
||||
import { CodexAuthPlugin } from "./codex"
|
||||
import { Session } from "../session"
|
||||
import { NamedError } from "@opencode-ai/util/error"
|
||||
import { CopilotAuthPlugin } from "./copilot"
|
||||
import { gitlabAuthPlugin as GitlabAuthPlugin } from "@gitlab/opencode-gitlab-auth"
|
||||
|
||||
export namespace Plugin {
|
||||
const log = Log.create({ service: "plugin" })
|
||||
|
||||
const BUILTIN = ["opencode-anthropic-auth@0.0.13"]
|
||||
|
||||
// Built-in plugins that are directly imported (not installed from npm)
|
||||
const INTERNAL_PLUGINS: PluginInstance[] = [CodexAuthPlugin, CopilotAuthPlugin, GitlabAuthPlugin]
|
||||
|
||||
@@ -39,8 +44,11 @@ export namespace Plugin {
|
||||
hooks.push(init)
|
||||
}
|
||||
|
||||
const plugins = [...(config.plugin ?? [])]
|
||||
let plugins = config.plugin ?? []
|
||||
if (plugins.length) await Config.waitForDependencies()
|
||||
if (!Flag.OPENCODE_DISABLE_DEFAULT_PLUGINS) {
|
||||
plugins = [...BUILTIN, ...plugins]
|
||||
}
|
||||
|
||||
for (let plugin of plugins) {
|
||||
// ignore old codex plugin since it is supported first party now
|
||||
@@ -50,7 +58,24 @@ export namespace Plugin {
|
||||
const lastAtIndex = plugin.lastIndexOf("@")
|
||||
const pkg = lastAtIndex > 0 ? plugin.substring(0, lastAtIndex) : plugin
|
||||
const version = lastAtIndex > 0 ? plugin.substring(lastAtIndex + 1) : "latest"
|
||||
plugin = await BunProc.install(pkg, version)
|
||||
const builtin = BUILTIN.some((x) => x.startsWith(pkg + "@"))
|
||||
plugin = await BunProc.install(pkg, version).catch((err) => {
|
||||
if (!builtin) throw err
|
||||
|
||||
const message = err instanceof Error ? err.message : String(err)
|
||||
log.error("failed to install builtin plugin", {
|
||||
pkg,
|
||||
version,
|
||||
error: message,
|
||||
})
|
||||
Bus.publish(Session.Event.Error, {
|
||||
error: new NamedError.Unknown({
|
||||
message: `Failed to install built-in plugin ${pkg}@${version}: ${message}`,
|
||||
}).toObject(),
|
||||
})
|
||||
|
||||
return ""
|
||||
})
|
||||
if (!plugin) continue
|
||||
}
|
||||
const mod = await import(plugin)
|
||||
|
||||
@@ -375,7 +375,8 @@ export namespace ProviderTransform {
|
||||
}
|
||||
}
|
||||
const copilotEfforts = iife(() => {
|
||||
if (id.includes("5.1-codex-max") || id.includes("5.2")) return [...WIDELY_SUPPORTED_EFFORTS, "xhigh"]
|
||||
if (id.includes("5.1-codex-max") || id.includes("5.2") || id.includes("5.3"))
|
||||
return [...WIDELY_SUPPORTED_EFFORTS, "xhigh"]
|
||||
return WIDELY_SUPPORTED_EFFORTS
|
||||
})
|
||||
return Object.fromEntries(
|
||||
@@ -422,7 +423,7 @@ export namespace ProviderTransform {
|
||||
if (id === "gpt-5-pro") return {}
|
||||
const openaiEfforts = iife(() => {
|
||||
if (id.includes("codex")) {
|
||||
if (id.includes("5.2")) return [...WIDELY_SUPPORTED_EFFORTS, "xhigh"]
|
||||
if (id.includes("5.2") || id.includes("5.3")) return [...WIDELY_SUPPORTED_EFFORTS, "xhigh"]
|
||||
return WIDELY_SUPPORTED_EFFORTS
|
||||
}
|
||||
const arr = [...WIDELY_SUPPORTED_EFFORTS]
|
||||
|
||||
@@ -71,9 +71,6 @@ export namespace SessionPrompt {
|
||||
async (current) => {
|
||||
for (const item of Object.values(current)) {
|
||||
item.abort.abort()
|
||||
for (const callback of item.callbacks) {
|
||||
callback.reject(new DOMException("Aborted", "AbortError"))
|
||||
}
|
||||
}
|
||||
},
|
||||
)
|
||||
@@ -249,9 +246,6 @@ export namespace SessionPrompt {
|
||||
return
|
||||
}
|
||||
match.abort.abort()
|
||||
for (const item of match.callbacks) {
|
||||
item.reject(new DOMException("Aborted", "AbortError"))
|
||||
}
|
||||
delete s[sessionID]
|
||||
SessionStatus.set(sessionID, { type: "idle" })
|
||||
return
|
||||
|
||||
Reference in New Issue
Block a user