| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501 |
- import path from "path"
- import { Decimal } from "decimal.js"
- import { z, ZodSchema } from "zod"
- import {
- generateText,
- LoadAPIKeyError,
- streamText,
- tool,
- wrapLanguageModel,
- type Tool as AITool,
- type LanguageModelUsage,
- type ProviderMetadata,
- type ModelMessage,
- type StreamTextResult,
- } from "ai"
- import PROMPT_INITIALIZE from "../session/prompt/initialize.txt"
- import PROMPT_PLAN from "../session/prompt/plan.txt"
- import { App } from "../app/app"
- import { Bus } from "../bus"
- import { Config } from "../config/config"
- import { Flag } from "../flag/flag"
- import { Identifier } from "../id/id"
- import { Installation } from "../installation"
- import { MCP } from "../mcp"
- import { Provider } from "../provider/provider"
- import { ProviderTransform } from "../provider/transform"
- import type { ModelsDev } from "../provider/models"
- import { Share } from "../share/share"
- import { Snapshot } from "../snapshot"
- import { Storage } from "../storage/storage"
- import { Log } from "../util/log"
- import { NamedError } from "../util/error"
- import { SystemPrompt } from "./system"
- import { FileTime } from "../file/time"
- import { MessageV2 } from "./message-v2"
- import { LSP } from "../lsp"
- import { ReadTool } from "../tool/read"
- import { mergeDeep, pipe, splitWhen } from "remeda"
- import { ToolRegistry } from "../tool/registry"
- import { Plugin } from "../plugin"
- import { Agent } from "../agent/agent"
- import { Permission } from "../permission"
- import { Wildcard } from "../util/wildcard"
- export namespace Session {
- const log = Log.create({ service: "session" })
- const OUTPUT_TOKEN_MAX = 32_000
- const parentSessionTitlePrefix = "New session - "
- const childSessionTitlePrefix = "Child session - "
- function createDefaultTitle(isChild = false) {
- return (isChild ? childSessionTitlePrefix : parentSessionTitlePrefix) + new Date().toISOString()
- }
- function isDefaultTitle(title: string) {
- return title.startsWith(parentSessionTitlePrefix)
- }
- export const Info = z
- .object({
- id: Identifier.schema("session"),
- parentID: Identifier.schema("session").optional(),
- share: z
- .object({
- url: z.string(),
- })
- .optional(),
- title: z.string(),
- version: z.string(),
- time: z.object({
- created: z.number(),
- updated: z.number(),
- }),
- revert: z
- .object({
- messageID: z.string(),
- partID: z.string().optional(),
- snapshot: z.string().optional(),
- diff: z.string().optional(),
- })
- .optional(),
- })
- .openapi({
- ref: "Session",
- })
- export type Info = z.output<typeof Info>
- export const ShareInfo = z
- .object({
- secret: z.string(),
- url: z.string(),
- })
- .openapi({
- ref: "SessionShare",
- })
- export type ShareInfo = z.output<typeof ShareInfo>
- export const Event = {
- Updated: Bus.event(
- "session.updated",
- z.object({
- info: Info,
- }),
- ),
- Deleted: Bus.event(
- "session.deleted",
- z.object({
- info: Info,
- }),
- ),
- Idle: Bus.event(
- "session.idle",
- z.object({
- sessionID: z.string(),
- }),
- ),
- Error: Bus.event(
- "session.error",
- z.object({
- sessionID: z.string().optional(),
- error: MessageV2.Assistant.shape.error,
- }),
- ),
- }
- const state = App.state(
- "session",
- () => {
- const sessions = new Map<string, Info>()
- const messages = new Map<string, MessageV2.Info[]>()
- const pending = new Map<string, AbortController>()
- const autoCompacting = new Map<string, boolean>()
- const queued = new Map<
- string,
- {
- input: ChatInput
- message: MessageV2.User
- parts: MessageV2.Part[]
- processed: boolean
- callback: (input: { info: MessageV2.Assistant; parts: MessageV2.Part[] }) => void
- }[]
- >()
- return {
- sessions,
- messages,
- pending,
- autoCompacting,
- queued,
- }
- },
- async (state) => {
- for (const [_, controller] of state.pending) {
- controller.abort()
- }
- },
- )
- export async function create(parentID?: string) {
- const result: Info = {
- id: Identifier.descending("session"),
- version: Installation.VERSION,
- parentID,
- title: createDefaultTitle(!!parentID),
- time: {
- created: Date.now(),
- updated: Date.now(),
- },
- }
- log.info("created", result)
- state().sessions.set(result.id, result)
- await Storage.writeJSON("session/info/" + result.id, result)
- const cfg = await Config.get()
- if (!result.parentID && (Flag.OPENCODE_AUTO_SHARE || cfg.share === "auto"))
- share(result.id)
- .then((share) => {
- update(result.id, (draft) => {
- draft.share = share
- })
- })
- .catch(() => {
- // Silently ignore sharing errors during session creation
- })
- Bus.publish(Event.Updated, {
- info: result,
- })
- return result
- }
- export async function get(id: string) {
- const result = state().sessions.get(id)
- if (result) {
- return result
- }
- const read = await Storage.readJSON<Info>("session/info/" + id)
- state().sessions.set(id, read)
- return read as Info
- }
- export async function getShare(id: string) {
- return Storage.readJSON<ShareInfo>("session/share/" + id)
- }
- export async function share(id: string) {
- const cfg = await Config.get()
- if (cfg.share === "disabled") {
- throw new Error("Sharing is disabled in configuration")
- }
- const session = await get(id)
- if (session.share) return session.share
- const share = await Share.create(id)
- await update(id, (draft) => {
- draft.share = {
- url: share.url,
- }
- })
- await Storage.writeJSON<ShareInfo>("session/share/" + id, share)
- await Share.sync("session/info/" + id, session)
- for (const msg of await messages(id)) {
- await Share.sync("session/message/" + id + "/" + msg.info.id, msg.info)
- for (const part of msg.parts) {
- await Share.sync("session/part/" + id + "/" + msg.info.id + "/" + part.id, part)
- }
- }
- return share
- }
- export async function unshare(id: string) {
- const share = await getShare(id)
- if (!share) return
- await Storage.remove("session/share/" + id)
- await update(id, (draft) => {
- draft.share = undefined
- })
- await Share.remove(id, share.secret)
- }
- export async function update(id: string, editor: (session: Info) => void) {
- const { sessions } = state()
- const session = await get(id)
- if (!session) return
- editor(session)
- session.time.updated = Date.now()
- sessions.set(id, session)
- await Storage.writeJSON("session/info/" + id, session)
- Bus.publish(Event.Updated, {
- info: session,
- })
- return session
- }
- export async function messages(sessionID: string) {
- const result = [] as {
- info: MessageV2.Info
- parts: MessageV2.Part[]
- }[]
- for (const p of await Storage.list("session/message/" + sessionID)) {
- const read = await Storage.readJSON<MessageV2.Info>(p)
- result.push({
- info: read,
- parts: await getParts(sessionID, read.id),
- })
- }
- result.sort((a, b) => (a.info.id > b.info.id ? 1 : -1))
- return result
- }
- export async function getMessage(sessionID: string, messageID: string) {
- return {
- info: await Storage.readJSON<MessageV2.Info>("session/message/" + sessionID + "/" + messageID),
- parts: await getParts(sessionID, messageID),
- }
- }
- export async function getParts(sessionID: string, messageID: string) {
- const result = [] as MessageV2.Part[]
- for (const item of await Storage.list("session/part/" + sessionID + "/" + messageID)) {
- const read = await Storage.readJSON<MessageV2.Part>(item)
- result.push(read)
- }
- result.sort((a, b) => (a.id > b.id ? 1 : -1))
- return result
- }
- export async function* list() {
- for (const item of await Storage.list("session/info")) {
- const sessionID = path.basename(item, ".json")
- yield get(sessionID)
- }
- }
- export async function children(parentID: string) {
- const result = [] as Session.Info[]
- for (const item of await Storage.list("session/info")) {
- const sessionID = path.basename(item, ".json")
- const session = await get(sessionID)
- if (session.parentID !== parentID) continue
- result.push(session)
- }
- return result
- }
- export function abort(sessionID: string) {
- const controller = state().pending.get(sessionID)
- if (!controller) return false
- log.info("aborting", {
- sessionID,
- })
- controller.abort()
- state().pending.delete(sessionID)
- return true
- }
- export async function remove(sessionID: string, emitEvent = true) {
- try {
- abort(sessionID)
- const session = await get(sessionID)
- for (const child of await children(sessionID)) {
- await remove(child.id, false)
- }
- await unshare(sessionID).catch(() => {})
- await Storage.remove(`session/info/${sessionID}`).catch(() => {})
- await Storage.removeDir(`session/message/${sessionID}/`).catch(() => {})
- state().sessions.delete(sessionID)
- state().messages.delete(sessionID)
- if (emitEvent) {
- Bus.publish(Event.Deleted, {
- info: session,
- })
- }
- } catch (e) {
- log.error(e)
- }
- }
- async function updateMessage(msg: MessageV2.Info) {
- await Storage.writeJSON("session/message/" + msg.sessionID + "/" + msg.id, msg)
- Bus.publish(MessageV2.Event.Updated, {
- info: msg,
- })
- }
- async function updatePart(part: MessageV2.Part) {
- await Storage.writeJSON(["session", "part", part.sessionID, part.messageID, part.id].join("/"), part)
- Bus.publish(MessageV2.Event.PartUpdated, {
- part,
- })
- return part
- }
- export const ChatInput = z.object({
- sessionID: Identifier.schema("session"),
- messageID: Identifier.schema("message").optional(),
- providerID: z.string(),
- modelID: z.string(),
- agent: z.string().optional(),
- system: z.string().optional(),
- tools: z.record(z.boolean()).optional(),
- parts: z.array(
- z.discriminatedUnion("type", [
- MessageV2.TextPart.omit({
- messageID: true,
- sessionID: true,
- })
- .partial({
- id: true,
- })
- .openapi({
- ref: "TextPartInput",
- }),
- MessageV2.FilePart.omit({
- messageID: true,
- sessionID: true,
- })
- .partial({
- id: true,
- })
- .openapi({
- ref: "FilePartInput",
- }),
- MessageV2.AgentPart.omit({
- messageID: true,
- sessionID: true,
- })
- .partial({
- id: true,
- })
- .openapi({
- ref: "AgentPartInput",
- }),
- ]),
- ),
- })
- export type ChatInput = z.infer<typeof ChatInput>
- export async function chat(
- input: z.infer<typeof ChatInput>,
- ): Promise<{ info: MessageV2.Assistant; parts: MessageV2.Part[] }> {
- const l = log.clone().tag("session", input.sessionID)
- l.info("chatting")
- const inputAgent = input.agent ?? "build"
- // Process revert cleanup first, before creating new messages
- const session = await get(input.sessionID)
- if (session.revert) {
- let msgs = await messages(input.sessionID)
- const messageID = session.revert.messageID
- const [preserve, remove] = splitWhen(msgs, (x) => x.info.id === messageID)
- msgs = preserve
- for (const msg of remove) {
- await Storage.remove(`session/message/${input.sessionID}/${msg.info.id}`)
- await Bus.publish(MessageV2.Event.Removed, { sessionID: input.sessionID, messageID: msg.info.id })
- }
- const last = preserve.at(-1)
- if (session.revert.partID && last) {
- const partID = session.revert.partID
- const [preserveParts, removeParts] = splitWhen(last.parts, (x) => x.id === partID)
- last.parts = preserveParts
- for (const part of removeParts) {
- await Storage.remove(`session/part/${input.sessionID}/${last.info.id}/${part.id}`)
- await Bus.publish(MessageV2.Event.PartRemoved, {
- sessionID: input.sessionID,
- messageID: last.info.id,
- partID: part.id,
- })
- }
- }
- await update(input.sessionID, (draft) => {
- draft.revert = undefined
- })
- }
- const userMsg: MessageV2.Info = {
- id: input.messageID ?? Identifier.ascending("message"),
- role: "user",
- sessionID: input.sessionID,
- time: {
- created: Date.now(),
- },
- }
- const app = App.info()
- const userParts = await Promise.all(
- input.parts.map(async (part): Promise<MessageV2.Part[]> => {
- if (part.type === "file") {
- const url = new URL(part.url)
- switch (url.protocol) {
- case "data:":
- if (part.mime === "text/plain") {
- return [
- {
- id: Identifier.ascending("part"),
- messageID: userMsg.id,
- sessionID: input.sessionID,
- type: "text",
- synthetic: true,
- text: `Called the Read tool with the following input: ${JSON.stringify({ filePath: part.filename })}`,
- },
- {
- id: Identifier.ascending("part"),
- messageID: userMsg.id,
- sessionID: input.sessionID,
- type: "text",
- synthetic: true,
- text: Buffer.from(part.url, "base64url").toString(),
- },
- {
- ...part,
- id: part.id ?? Identifier.ascending("part"),
- messageID: userMsg.id,
- sessionID: input.sessionID,
- },
- ]
- }
- break
- case "file:":
- // have to normalize, symbol search returns absolute paths
- // Decode the pathname since URL constructor doesn't automatically decode it
- const filePath = decodeURIComponent(url.pathname)
- if (part.mime === "text/plain") {
- let offset: number | undefined = undefined
- let limit: number | undefined = undefined
- const range = {
- start: url.searchParams.get("start"),
- end: url.searchParams.get("end"),
- }
- if (range.start != null) {
- const filePath = part.url.split("?")[0]
- let start = parseInt(range.start)
- let end = range.end ? parseInt(range.end) : undefined
- // some LSP servers (eg, gopls) don't give full range in
- // workspace/symbol searches, so we'll try to find the
- // symbol in the document to get the full range
- if (start === end) {
- const symbols = await LSP.documentSymbol(filePath)
- for (const symbol of symbols) {
- let range: LSP.Range | undefined
- if ("range" in symbol) {
- range = symbol.range
- } else if ("location" in symbol) {
- range = symbol.location.range
- }
- if (range?.start?.line && range?.start?.line === start) {
- start = range.start.line
- end = range?.end?.line ?? start
- break
- }
- }
- offset = Math.max(start - 2, 0)
- if (end) {
- limit = end - offset + 2
- }
- }
- }
- const args = { filePath, offset, limit }
- const result = await ReadTool.init().then((t) =>
- t.execute(args, {
- sessionID: input.sessionID,
- abort: new AbortController().signal,
- messageID: userMsg.id,
- metadata: async () => {},
- }),
- )
- return [
- {
- id: Identifier.ascending("part"),
- messageID: userMsg.id,
- sessionID: input.sessionID,
- type: "text",
- synthetic: true,
- text: `Called the Read tool with the following input: ${JSON.stringify(args)}`,
- },
- {
- id: Identifier.ascending("part"),
- messageID: userMsg.id,
- sessionID: input.sessionID,
- type: "text",
- synthetic: true,
- text: result.output,
- },
- {
- ...part,
- id: part.id ?? Identifier.ascending("part"),
- messageID: userMsg.id,
- sessionID: input.sessionID,
- },
- ]
- }
- let file = Bun.file(filePath)
- FileTime.read(input.sessionID, filePath)
- return [
- {
- id: Identifier.ascending("part"),
- messageID: userMsg.id,
- sessionID: input.sessionID,
- type: "text",
- text: `Called the Read tool with the following input: {\"filePath\":\"${filePath}\"}`,
- synthetic: true,
- },
- {
- id: part.id ?? Identifier.ascending("part"),
- messageID: userMsg.id,
- sessionID: input.sessionID,
- type: "file",
- url: `data:${part.mime};base64,` + Buffer.from(await file.bytes()).toString("base64"),
- mime: part.mime,
- filename: part.filename!,
- source: part.source,
- },
- ]
- }
- }
- if (part.type === "agent") {
- return [
- {
- id: Identifier.ascending("part"),
- ...part,
- messageID: userMsg.id,
- sessionID: input.sessionID,
- },
- {
- id: Identifier.ascending("part"),
- messageID: userMsg.id,
- sessionID: input.sessionID,
- type: "text",
- synthetic: true,
- text:
- "Use the above message and context to generate a prompt and call the task tool with subagent: " +
- part.name,
- },
- ]
- }
- return [
- {
- id: Identifier.ascending("part"),
- ...part,
- messageID: userMsg.id,
- sessionID: input.sessionID,
- },
- ]
- }),
- ).then((x) => x.flat())
- await Plugin.trigger(
- "chat.message",
- {},
- {
- message: userMsg,
- parts: userParts,
- },
- )
- await updateMessage(userMsg)
- for (const part of userParts) {
- await updatePart(part)
- }
- // mark session as updated
- // used for session list sorting (indicates when session was most recently interacted with)
- await update(input.sessionID, (_draft) => {})
- if (isLocked(input.sessionID)) {
- return new Promise((resolve) => {
- const queue = state().queued.get(input.sessionID) ?? []
- queue.push({
- input: input,
- message: userMsg,
- parts: userParts,
- processed: false,
- callback: resolve,
- })
- state().queued.set(input.sessionID, queue)
- })
- }
- const model = await Provider.getModel(input.providerID, input.modelID)
- let msgs = await messages(input.sessionID)
- const previous = msgs.filter((x) => x.info.role === "assistant").at(-1)?.info as MessageV2.Assistant
- const outputLimit = Math.min(model.info.limit.output, OUTPUT_TOKEN_MAX) || OUTPUT_TOKEN_MAX
- // auto summarize if too long
- if (previous && previous.tokens) {
- const tokens =
- previous.tokens.input + previous.tokens.cache.read + previous.tokens.cache.write + previous.tokens.output
- if (model.info.limit.context && tokens > Math.max((model.info.limit.context - outputLimit) * 0.9, 0)) {
- state().autoCompacting.set(input.sessionID, true)
- await summarize({
- sessionID: input.sessionID,
- providerID: input.providerID,
- modelID: input.modelID,
- })
- return chat(input)
- }
- }
- using abort = lock(input.sessionID)
- const lastSummary = msgs.findLast((msg) => msg.info.role === "assistant" && msg.info.summary === true)
- if (lastSummary) msgs = msgs.filter((msg) => msg.info.id >= lastSummary.info.id)
- if (msgs.length === 1 && !session.parentID && isDefaultTitle(session.title)) {
- const small = (await Provider.getSmallModel(input.providerID)) ?? model
- generateText({
- maxOutputTokens: small.info.reasoning ? 1024 : 20,
- providerOptions: {
- [input.providerID]: {
- ...small.info.options,
- ...ProviderTransform.options(input.providerID, small.info.id),
- },
- },
- messages: [
- ...SystemPrompt.title(input.providerID).map(
- (x): ModelMessage => ({
- role: "system",
- content: x,
- }),
- ),
- ...MessageV2.toModelMessage([
- {
- info: {
- id: Identifier.ascending("message"),
- role: "user",
- sessionID: input.sessionID,
- time: {
- created: Date.now(),
- },
- },
- parts: userParts,
- },
- ]),
- ],
- model: small.language,
- })
- .then((result) => {
- if (result.text)
- return Session.update(input.sessionID, (draft) => {
- const cleaned = result.text.replace(/<think>[\s\S]*?<\/think>\s*/g, "")
- const title = cleaned.length > 100 ? cleaned.substring(0, 97) + "..." : cleaned
- draft.title = title.trim()
- })
- })
- .catch(() => {})
- }
- const agent = await Agent.get(inputAgent)
- if (agent.name === "plan") {
- msgs.at(-1)?.parts.push({
- id: Identifier.ascending("part"),
- messageID: userMsg.id,
- sessionID: input.sessionID,
- type: "text",
- text: PROMPT_PLAN,
- synthetic: true,
- })
- }
- let system = SystemPrompt.header(input.providerID)
- system.push(
- ...(() => {
- if (input.system) return [input.system]
- if (agent.prompt) return [agent.prompt]
- return SystemPrompt.provider(input.modelID)
- })(),
- )
- system.push(...(await SystemPrompt.environment()))
- system.push(...(await SystemPrompt.custom()))
- // max 2 system prompt messages for caching purposes
- const [first, ...rest] = system
- system = [first, rest.join("\n")]
- const assistantMsg: MessageV2.Info = {
- id: Identifier.ascending("message"),
- role: "assistant",
- system,
- mode: inputAgent,
- path: {
- cwd: app.path.cwd,
- root: app.path.root,
- },
- cost: 0,
- tokens: {
- input: 0,
- output: 0,
- reasoning: 0,
- cache: { read: 0, write: 0 },
- },
- modelID: input.modelID,
- providerID: input.providerID,
- time: {
- created: Date.now(),
- },
- sessionID: input.sessionID,
- }
- await updateMessage(assistantMsg)
- const tools: Record<string, AITool> = {}
- const processor = createProcessor(assistantMsg, model.info)
- const enabledTools = pipe(
- agent.tools,
- mergeDeep(await ToolRegistry.enabled(input.providerID, input.modelID)),
- mergeDeep(input.tools ?? {}),
- )
- for (const item of await ToolRegistry.tools(input.providerID, input.modelID)) {
- if (Wildcard.all(item.id, enabledTools) === false) continue
- tools[item.id] = tool({
- id: item.id as any,
- description: item.description,
- inputSchema: item.parameters as ZodSchema,
- async execute(args, options) {
- await Plugin.trigger(
- "tool.execute.before",
- {
- tool: item.id,
- sessionID: input.sessionID,
- callID: options.toolCallId,
- },
- {
- args,
- },
- )
- const result = await item.execute(args, {
- sessionID: input.sessionID,
- abort: options.abortSignal!,
- messageID: assistantMsg.id,
- callID: options.toolCallId,
- metadata: async (val) => {
- const match = processor.partFromToolCall(options.toolCallId)
- if (match && match.state.status === "running") {
- await updatePart({
- ...match,
- state: {
- title: val.title,
- metadata: val.metadata,
- status: "running",
- input: args,
- time: {
- start: Date.now(),
- },
- },
- })
- }
- },
- })
- await Plugin.trigger(
- "tool.execute.after",
- {
- tool: item.id,
- sessionID: input.sessionID,
- callID: options.toolCallId,
- },
- result,
- )
- return result
- },
- toModelOutput(result) {
- return {
- type: "text",
- value: result.output,
- }
- },
- })
- }
- for (const [key, item] of Object.entries(await MCP.tools())) {
- if (Wildcard.all(key, enabledTools) === false) continue
- const execute = item.execute
- if (!execute) continue
- item.execute = async (args, opts) => {
- const result = await execute(args, opts)
- const output = result.content
- .filter((x: any) => x.type === "text")
- .map((x: any) => x.text)
- .join("\n\n")
- return {
- output,
- }
- }
- item.toModelOutput = (result) => {
- return {
- type: "text",
- value: result.output,
- }
- }
- tools[key] = item
- }
- const params = await Plugin.trigger(
- "chat.params",
- {
- model: model.info,
- provider: await Provider.getProvider(input.providerID),
- message: userMsg,
- },
- {
- temperature: model.info.temperature
- ? (agent.temperature ?? ProviderTransform.temperature(input.providerID, input.modelID))
- : undefined,
- topP: agent.topP ?? ProviderTransform.topP(input.providerID, input.modelID),
- options: {
- ...ProviderTransform.options(input.providerID, input.modelID),
- ...model.info.options,
- ...agent.options,
- },
- },
- )
- const stream = streamText({
- onError(e) {
- log.error("streamText error", {
- error: e,
- })
- },
- async prepareStep({ messages }) {
- const queue = (state().queued.get(input.sessionID) ?? []).filter((x) => !x.processed)
- if (queue.length) {
- for (const item of queue) {
- if (item.processed) continue
- messages.push(
- ...MessageV2.toModelMessage([
- {
- info: item.message,
- parts: item.parts,
- },
- ]),
- )
- item.processed = true
- }
- assistantMsg.time.completed = Date.now()
- await updateMessage(assistantMsg)
- Object.assign(assistantMsg, {
- id: Identifier.ascending("message"),
- role: "assistant",
- system,
- path: {
- cwd: app.path.cwd,
- root: app.path.root,
- },
- cost: 0,
- tokens: {
- input: 0,
- output: 0,
- reasoning: 0,
- cache: { read: 0, write: 0 },
- },
- modelID: input.modelID,
- providerID: input.providerID,
- mode: inputAgent,
- time: {
- created: Date.now(),
- },
- sessionID: input.sessionID,
- })
- await updateMessage(assistantMsg)
- }
- return {
- messages,
- }
- },
- async experimental_repairToolCall(input) {
- return {
- ...input.toolCall,
- input: JSON.stringify({
- tool: input.toolCall.toolName,
- error: input.error.message,
- }),
- toolName: "invalid",
- }
- },
- maxRetries: 3,
- activeTools: Object.keys(tools).filter((x) => x !== "invalid"),
- maxOutputTokens: outputLimit,
- abortSignal: abort.signal,
- stopWhen: async ({ steps }) => {
- if (steps.length >= 1000) {
- return true
- }
- // Check if processor flagged that we should stop
- if (processor.getShouldStop()) {
- return true
- }
- return false
- },
- providerOptions: {
- [input.providerID]: params.options,
- },
- temperature: params.temperature,
- topP: params.topP,
- messages: [
- ...system.map(
- (x): ModelMessage => ({
- role: "system",
- content: x,
- }),
- ),
- ...MessageV2.toModelMessage(msgs),
- ],
- tools: model.info.tool_call === false ? undefined : tools,
- model: wrapLanguageModel({
- model: model.language,
- middleware: [
- {
- async transformParams(args) {
- if (args.type === "stream") {
- // @ts-expect-error
- args.params.prompt = ProviderTransform.message(args.params.prompt, input.providerID, input.modelID)
- }
- return args.params
- },
- },
- ],
- }),
- })
- const result = await processor.process(stream)
- const queued = state().queued.get(input.sessionID) ?? []
- const unprocessed = queued.find((x) => !x.processed)
- if (unprocessed) {
- unprocessed.processed = true
- return chat(unprocessed.input)
- }
- for (const item of queued) {
- item.callback(result)
- }
- state().queued.delete(input.sessionID)
- return result
- }
- function createProcessor(assistantMsg: MessageV2.Assistant, model: ModelsDev.Model) {
- const toolcalls: Record<string, MessageV2.ToolPart> = {}
- let snapshot: string | undefined
- let shouldStop = false
- return {
- partFromToolCall(toolCallID: string) {
- return toolcalls[toolCallID]
- },
- getShouldStop() {
- return shouldStop
- },
- async process(stream: StreamTextResult<Record<string, AITool>, never>) {
- try {
- let currentText: MessageV2.TextPart | undefined
- let reasoningMap: Record<string, MessageV2.ReasoningPart> = {}
- for await (const value of stream.fullStream) {
- log.info("part", {
- type: value.type,
- })
- switch (value.type) {
- case "start":
- break
- case "reasoning-start":
- if (value.id in reasoningMap) {
- continue
- }
- reasoningMap[value.id] = {
- id: Identifier.ascending("part"),
- messageID: assistantMsg.id,
- sessionID: assistantMsg.sessionID,
- type: "reasoning",
- text: "",
- time: {
- start: Date.now(),
- },
- }
- break
- case "reasoning-delta":
- if (value.id in reasoningMap) {
- const part = reasoningMap[value.id]
- part.text += value.text
- if (part.text) await updatePart(part)
- }
- break
- case "reasoning-end":
- if (value.id in reasoningMap) {
- const part = reasoningMap[value.id]
- part.text = part.text.trimEnd()
- part.metadata = value.providerMetadata
- part.time = {
- ...part.time,
- end: Date.now(),
- }
- await updatePart(part)
- delete reasoningMap[value.id]
- }
- break
- case "tool-input-start":
- const part = await updatePart({
- id: toolcalls[value.id]?.id ?? Identifier.ascending("part"),
- messageID: assistantMsg.id,
- sessionID: assistantMsg.sessionID,
- type: "tool",
- tool: value.toolName,
- callID: value.id,
- state: {
- status: "pending",
- },
- })
- toolcalls[value.id] = part as MessageV2.ToolPart
- break
- case "tool-input-delta":
- break
- case "tool-input-end":
- break
- case "tool-call": {
- const match = toolcalls[value.toolCallId]
- if (match) {
- const part = await updatePart({
- ...match,
- tool: value.toolName,
- state: {
- status: "running",
- input: value.input,
- time: {
- start: Date.now(),
- },
- },
- })
- toolcalls[value.toolCallId] = part as MessageV2.ToolPart
- }
- break
- }
- case "tool-result": {
- const match = toolcalls[value.toolCallId]
- if (match && match.state.status === "running") {
- await updatePart({
- ...match,
- state: {
- status: "completed",
- input: value.input,
- output: value.output.output,
- metadata: value.output.metadata,
- title: value.output.title,
- time: {
- start: match.state.time.start,
- end: Date.now(),
- },
- },
- })
- delete toolcalls[value.toolCallId]
- }
- break
- }
- case "tool-error": {
- const match = toolcalls[value.toolCallId]
- if (match && match.state.status === "running") {
- if (value.error instanceof Permission.RejectedError) {
- shouldStop = true
- }
- await updatePart({
- ...match,
- state: {
- status: "error",
- input: value.input,
- error: (value.error as any).toString(),
- time: {
- start: match.state.time.start,
- end: Date.now(),
- },
- },
- })
- delete toolcalls[value.toolCallId]
- }
- break
- }
- case "error":
- throw value.error
- case "start-step":
- await updatePart({
- id: Identifier.ascending("part"),
- messageID: assistantMsg.id,
- sessionID: assistantMsg.sessionID,
- type: "step-start",
- })
- snapshot = await Snapshot.track()
- break
- case "finish-step":
- const usage = getUsage(model, value.usage, value.providerMetadata)
- assistantMsg.cost += usage.cost
- assistantMsg.tokens = usage.tokens
- await updatePart({
- id: Identifier.ascending("part"),
- messageID: assistantMsg.id,
- sessionID: assistantMsg.sessionID,
- type: "step-finish",
- tokens: usage.tokens,
- cost: usage.cost,
- })
- await updateMessage(assistantMsg)
- if (snapshot) {
- const patch = await Snapshot.patch(snapshot)
- if (patch.files.length) {
- await updatePart({
- id: Identifier.ascending("part"),
- messageID: assistantMsg.id,
- sessionID: assistantMsg.sessionID,
- type: "patch",
- hash: patch.hash,
- files: patch.files,
- })
- }
- snapshot = undefined
- }
- break
- case "text-start":
- currentText = {
- id: Identifier.ascending("part"),
- messageID: assistantMsg.id,
- sessionID: assistantMsg.sessionID,
- type: "text",
- text: "",
- time: {
- start: Date.now(),
- },
- }
- break
- case "text-delta":
- if (currentText) {
- currentText.text += value.text
- if (currentText.text) await updatePart(currentText)
- }
- break
- case "text-end":
- if (currentText) {
- currentText.text = currentText.text.trimEnd()
- currentText.time = {
- start: Date.now(),
- end: Date.now(),
- }
- await updatePart(currentText)
- }
- currentText = undefined
- break
- case "finish":
- assistantMsg.time.completed = Date.now()
- await updateMessage(assistantMsg)
- break
- default:
- log.info("unhandled", {
- ...value,
- })
- continue
- }
- }
- } catch (e) {
- log.error("", {
- error: e,
- })
- switch (true) {
- case e instanceof DOMException && e.name === "AbortError":
- assistantMsg.error = new MessageV2.AbortedError(
- { message: e.message },
- {
- cause: e,
- },
- ).toObject()
- break
- case MessageV2.OutputLengthError.isInstance(e):
- assistantMsg.error = e
- break
- case LoadAPIKeyError.isInstance(e):
- assistantMsg.error = new MessageV2.AuthError(
- {
- providerID: model.id,
- message: e.message,
- },
- { cause: e },
- ).toObject()
- break
- case e instanceof Error:
- assistantMsg.error = new NamedError.Unknown({ message: e.toString() }, { cause: e }).toObject()
- break
- default:
- assistantMsg.error = new NamedError.Unknown({ message: JSON.stringify(e) }, { cause: e })
- }
- Bus.publish(Event.Error, {
- sessionID: assistantMsg.sessionID,
- error: assistantMsg.error,
- })
- }
- const p = await getParts(assistantMsg.sessionID, assistantMsg.id)
- for (const part of p) {
- if (part.type === "tool" && part.state.status !== "completed" && part.state.status !== "error") {
- updatePart({
- ...part,
- state: {
- status: "error",
- error: "Tool execution aborted",
- time: {
- start: Date.now(),
- end: Date.now(),
- },
- input: {},
- },
- })
- }
- }
- assistantMsg.time.completed = Date.now()
- await updateMessage(assistantMsg)
- return { info: assistantMsg, parts: p }
- },
- }
- }
- export const RevertInput = z.object({
- sessionID: Identifier.schema("session"),
- messageID: Identifier.schema("message"),
- partID: Identifier.schema("part").optional(),
- })
- export type RevertInput = z.infer<typeof RevertInput>
- export async function revert(input: RevertInput) {
- const all = await messages(input.sessionID)
- let lastUser: MessageV2.User | undefined
- const session = await get(input.sessionID)
- let revert: Info["revert"]
- const patches: Snapshot.Patch[] = []
- for (const msg of all) {
- if (msg.info.role === "user") lastUser = msg.info
- const remaining = []
- for (const part of msg.parts) {
- if (revert) {
- if (part.type === "patch") {
- patches.push(part)
- }
- continue
- }
- if (!revert) {
- if ((msg.info.id === input.messageID && !input.partID) || part.id === input.partID) {
- // if no useful parts left in message, same as reverting whole message
- const partID = remaining.some((item) => ["text", "tool"].includes(item.type)) ? input.partID : undefined
- revert = {
- messageID: !partID && lastUser ? lastUser.id : msg.info.id,
- partID,
- }
- }
- remaining.push(part)
- }
- }
- }
- if (revert) {
- const session = await get(input.sessionID)
- revert.snapshot = session.revert?.snapshot ?? (await Snapshot.track())
- await Snapshot.revert(patches)
- if (revert.snapshot) revert.diff = await Snapshot.diff(revert.snapshot)
- return update(input.sessionID, (draft) => {
- draft.revert = revert
- })
- }
- return session
- }
- export async function unrevert(input: { sessionID: string }) {
- log.info("unreverting", input)
- const session = await get(input.sessionID)
- if (!session.revert) return session
- if (session.revert.snapshot) await Snapshot.restore(session.revert.snapshot)
- const next = await update(input.sessionID, (draft) => {
- draft.revert = undefined
- })
- return next
- }
- export async function summarize(input: { sessionID: string; providerID: string; modelID: string }) {
- using abort = lock(input.sessionID)
- const msgs = await messages(input.sessionID)
- const lastSummary = msgs.findLast((msg) => msg.info.role === "assistant" && msg.info.summary === true)
- const filtered = msgs.filter((msg) => !lastSummary || msg.info.id >= lastSummary.info.id)
- const model = await Provider.getModel(input.providerID, input.modelID)
- const app = App.info()
- const system = [
- ...SystemPrompt.summarize(input.providerID),
- ...(await SystemPrompt.environment()),
- ...(await SystemPrompt.custom()),
- ]
- const next: MessageV2.Info = {
- id: Identifier.ascending("message"),
- role: "assistant",
- sessionID: input.sessionID,
- system,
- mode: "build",
- path: {
- cwd: app.path.cwd,
- root: app.path.root,
- },
- summary: true,
- cost: 0,
- modelID: input.modelID,
- providerID: input.providerID,
- tokens: {
- input: 0,
- output: 0,
- reasoning: 0,
- cache: { read: 0, write: 0 },
- },
- time: {
- created: Date.now(),
- },
- }
- await updateMessage(next)
- const processor = createProcessor(next, model.info)
- const stream = streamText({
- maxRetries: 10,
- abortSignal: abort.signal,
- model: model.language,
- messages: [
- ...system.map(
- (x): ModelMessage => ({
- role: "system",
- content: x,
- }),
- ),
- ...MessageV2.toModelMessage(filtered),
- {
- role: "user",
- content: [
- {
- type: "text",
- text: "Provide a detailed but concise summary of our conversation above. Focus on information that would be helpful for continuing the conversation, including what we did, what we're doing, which files we're working on, and what we're going to do next.",
- },
- ],
- },
- ],
- })
- const result = await processor.process(stream)
- return result
- }
- function isLocked(sessionID: string) {
- return state().pending.has(sessionID)
- }
- function lock(sessionID: string) {
- log.info("locking", { sessionID })
- if (state().pending.has(sessionID)) throw new BusyError(sessionID)
- const controller = new AbortController()
- state().pending.set(sessionID, controller)
- return {
- signal: controller.signal,
- async [Symbol.dispose]() {
- log.info("unlocking", { sessionID })
- state().pending.delete(sessionID)
- const isAutoCompacting = state().autoCompacting.get(sessionID) ?? false
- if (isAutoCompacting) {
- state().autoCompacting.delete(sessionID)
- return
- }
- const session = await get(sessionID)
- if (session.parentID) return
- Bus.publish(Event.Idle, {
- sessionID,
- })
- },
- }
- }
- function getUsage(model: ModelsDev.Model, usage: LanguageModelUsage, metadata?: ProviderMetadata) {
- const tokens = {
- input: usage.inputTokens ?? 0,
- output: usage.outputTokens ?? 0,
- reasoning: 0,
- cache: {
- write: (metadata?.["anthropic"]?.["cacheCreationInputTokens"] ??
- // @ts-expect-error
- metadata?.["bedrock"]?.["usage"]?.["cacheWriteInputTokens"] ??
- 0) as number,
- read: usage.cachedInputTokens ?? 0,
- },
- }
- return {
- cost: new Decimal(0)
- .add(new Decimal(tokens.input).mul(model.cost?.input ?? 0).div(1_000_000))
- .add(new Decimal(tokens.output).mul(model.cost?.output ?? 0).div(1_000_000))
- .add(new Decimal(tokens.cache.read).mul(model.cost?.cache_read ?? 0).div(1_000_000))
- .add(new Decimal(tokens.cache.write).mul(model.cost?.cache_write ?? 0).div(1_000_000))
- .toNumber(),
- tokens,
- }
- }
- export class BusyError extends Error {
- constructor(public readonly sessionID: string) {
- super(`Session ${sessionID} is busy`)
- }
- }
- export async function initialize(input: {
- sessionID: string
- modelID: string
- providerID: string
- messageID: string
- }) {
- const app = App.info()
- await Session.chat({
- sessionID: input.sessionID,
- messageID: input.messageID,
- providerID: input.providerID,
- modelID: input.modelID,
- parts: [
- {
- id: Identifier.ascending("part"),
- type: "text",
- text: PROMPT_INITIALIZE.replace("${path}", app.path.root),
- },
- ],
- })
- await App.initialize()
- }
- }
|