| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912 |
- import path from "path"
- import os from "os"
- import fs from "fs/promises"
- import z from "zod/v4"
- import { Identifier } from "../id/id"
- import { MessageV2 } from "./message-v2"
- import { Log } from "../util/log"
- import { SessionRevert } from "./revert"
- import { Session } from "."
- import { Agent } from "../agent/agent"
- import { Provider } from "../provider/provider"
- import {
- generateText,
- streamText,
- type ModelMessage,
- type Tool as AITool,
- tool,
- wrapLanguageModel,
- type StreamTextResult,
- stepCountIs,
- jsonSchema,
- } from "ai"
- import { SessionCompaction } from "./compaction"
- import { SessionLock } from "./lock"
- import { Instance } from "../project/instance"
- import { Bus } from "../bus"
- import { ProviderTransform } from "../provider/transform"
- import { SystemPrompt } from "./system"
- import { Plugin } from "../plugin"
- import { SessionRetry } from "./retry"
- import PROMPT_PLAN from "../session/prompt/plan.txt"
- import BUILD_SWITCH from "../session/prompt/build-switch.txt"
- import { ModelsDev } from "../provider/models"
- import { defer } from "../util/defer"
- import { mergeDeep, pipe } from "remeda"
- import { ToolRegistry } from "../tool/registry"
- import { Wildcard } from "../util/wildcard"
- import { MCP } from "../mcp"
- import { LSP } from "../lsp"
- import { ReadTool } from "../tool/read"
- import { ListTool } from "../tool/ls"
- import { TaskTool } from "../tool/task"
- import { FileTime } from "../file/time"
- import { Permission } from "../permission"
- import { Snapshot } from "../snapshot"
- import { ulid } from "ulid"
- import { spawn } from "child_process"
- import { Command } from "../command"
- import { $, fileURLToPath } from "bun"
- import { ConfigMarkdown } from "../config/markdown"
- import { MessageSummary } from "./summary"
- export namespace SessionPrompt {
- const log = Log.create({ service: "session.prompt" })
- export const OUTPUT_TOKEN_MAX = 32_000
- const MAX_RETRIES = 10
- export const Event = {
- Idle: Bus.event(
- "session.idle",
- z.object({
- sessionID: z.string(),
- }),
- ),
- }
- const state = Instance.state(
- () => {
- const queued = new Map<
- string,
- {
- messageID: string
- callback: (input: MessageV2.WithParts) => void
- }[]
- >()
- return {
- queued,
- }
- },
- async (current) => {
- current.queued.clear()
- },
- )
- export const PromptInput = z.object({
- sessionID: Identifier.schema("session"),
- messageID: Identifier.schema("message").optional(),
- model: z
- .object({
- providerID: z.string(),
- modelID: z.string(),
- })
- .optional(),
- agent: z.string().optional(),
- system: z.string().optional(),
- tools: z.record(z.string(), z.boolean()).optional(),
- /**
- * ACP (Agent Client Protocol) connection details for streaming responses.
- * When provided, enables real-time streaming and tool execution visibility.
- */
- acpConnection: z
- .object({
- connection: z.any(), // AgentSideConnection - using any to avoid circular deps
- sessionId: z.string(), // ACP session ID (different from opencode sessionID)
- })
- .optional(),
- parts: z.array(
- z.discriminatedUnion("type", [
- MessageV2.TextPart.omit({
- messageID: true,
- sessionID: true,
- })
- .partial({
- id: true,
- })
- .meta({
- ref: "TextPartInput",
- }),
- MessageV2.FilePart.omit({
- messageID: true,
- sessionID: true,
- })
- .partial({
- id: true,
- })
- .meta({
- ref: "FilePartInput",
- }),
- MessageV2.AgentPart.omit({
- messageID: true,
- sessionID: true,
- })
- .partial({
- id: true,
- })
- .meta({
- ref: "AgentPartInput",
- }),
- ]),
- ),
- })
- export type PromptInput = z.infer<typeof PromptInput>
- export async function prompt(input: PromptInput): Promise<MessageV2.WithParts> {
- const l = log.clone().tag("session", input.sessionID)
- l.info("prompt")
- const session = await Session.get(input.sessionID)
- await SessionRevert.cleanup(session)
- const userMsg = await createUserMessage(input)
- await Session.touch(input.sessionID)
- if (isBusy(input.sessionID)) {
- return new Promise((resolve) => {
- const queue = state().queued.get(input.sessionID) ?? []
- queue.push({
- messageID: userMsg.info.id,
- callback: resolve,
- })
- state().queued.set(input.sessionID, queue)
- })
- }
- const agent = await Agent.get(input.agent ?? "build")
- const model = await resolveModel({
- agent,
- model: input.model,
- }).then((x) => Provider.getModel(x.providerID, x.modelID))
- using abort = lock(input.sessionID)
- const system = await resolveSystemPrompt({
- providerID: model.providerID,
- modelID: model.info.id,
- agent,
- system: input.system,
- })
- const processor = await createProcessor({
- sessionID: input.sessionID,
- model: model.info,
- providerID: model.providerID,
- agent: agent.name,
- system,
- abort: abort.signal,
- acpConnection: input.acpConnection,
- })
- const tools = await resolveTools({
- agent,
- sessionID: input.sessionID,
- modelID: model.modelID,
- providerID: model.providerID,
- tools: input.tools,
- processor,
- })
- const params = await Plugin.trigger(
- "chat.params",
- {
- model: model.info,
- provider: await Provider.getProvider(model.providerID),
- message: userMsg,
- },
- {
- temperature: model.info.temperature
- ? (agent.temperature ?? ProviderTransform.temperature(model.providerID, model.modelID))
- : undefined,
- topP: agent.topP ?? ProviderTransform.topP(model.providerID, model.modelID),
- options: {
- ...ProviderTransform.options(model.providerID, model.modelID, input.sessionID),
- ...model.info.options,
- ...agent.options,
- },
- },
- )
- let step = 0
- while (true) {
- const msgs: MessageV2.WithParts[] = pipe(
- await getMessages({
- sessionID: input.sessionID,
- model: model.info,
- providerID: model.providerID,
- signal: abort.signal,
- }),
- (messages) => insertReminders({ messages, agent }),
- )
- if (step === 0)
- ensureTitle({
- session,
- history: msgs,
- message: userMsg,
- providerID: model.providerID,
- modelID: model.info.id,
- })
- step++
- await processor.next(msgs.findLast((m) => m.info.role === "user")?.info.id!)
- await using _ = defer(async () => {
- await processor.end()
- })
- const doStream = () =>
- streamText({
- onError(error) {
- log.error("stream error", {
- error,
- })
- },
- async experimental_repairToolCall(input) {
- const lower = input.toolCall.toolName.toLowerCase()
- if (lower !== input.toolCall.toolName && tools[lower]) {
- log.info("repairing tool call", {
- tool: input.toolCall.toolName,
- repaired: lower,
- })
- return {
- ...input.toolCall,
- toolName: lower,
- }
- }
- return {
- ...input.toolCall,
- input: JSON.stringify({
- tool: input.toolCall.toolName,
- error: input.error.message,
- }),
- toolName: "invalid",
- }
- },
- headers:
- model.providerID === "opencode"
- ? {
- "x-opencode-session": input.sessionID,
- "x-opencode-request": userMsg.info.id,
- }
- : undefined,
- // set to 0, we handle loop
- maxRetries: 0,
- activeTools: Object.keys(tools).filter((x) => x !== "invalid"),
- maxOutputTokens: ProviderTransform.maxOutputTokens(
- model.providerID,
- params.options,
- model.info.limit.output,
- OUTPUT_TOKEN_MAX,
- ),
- abortSignal: abort.signal,
- providerOptions: ProviderTransform.providerOptions(model.npm, model.providerID, params.options),
- stopWhen: stepCountIs(1),
- temperature: params.temperature,
- topP: params.topP,
- messages: [
- ...system.map(
- (x): ModelMessage => ({
- role: "system",
- content: x,
- }),
- ),
- ...MessageV2.toModelMessage(
- msgs.filter((m) => {
- if (m.info.role !== "assistant" || m.info.error === undefined) {
- return true
- }
- if (
- MessageV2.AbortedError.isInstance(m.info.error) &&
- m.parts.some((part) => part.type !== "step-start" && part.type !== "reasoning")
- ) {
- return true
- }
- return false
- }),
- ),
- ],
- 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, model.providerID, model.modelID)
- }
- return args.params
- },
- },
- ],
- }),
- })
- let stream = doStream()
- let result = await processor.process(stream, {
- count: 0,
- max: MAX_RETRIES,
- })
- if (result.shouldRetry) {
- for (let retry = 1; retry < MAX_RETRIES; retry++) {
- const lastRetryPart = result.parts.findLast((p) => p.type === "retry")
- if (lastRetryPart) {
- const delayMs = SessionRetry.getRetryDelayInMs(lastRetryPart.error, retry)
- log.info("retrying with backoff", {
- attempt: retry,
- delayMs,
- })
- const stop = await SessionRetry.sleep(delayMs, abort.signal)
- .then(() => false)
- .catch((error) => {
- if (error instanceof DOMException && error.name === "AbortError") {
- const err = new MessageV2.AbortedError(
- { message: error.message },
- {
- cause: error,
- },
- ).toObject()
- result.info.error = err
- Bus.publish(Session.Event.Error, {
- sessionID: result.info.sessionID,
- error: result.info.error,
- })
- return true
- }
- throw error
- })
- if (stop) break
- }
- stream = doStream()
- result = await processor.process(stream, {
- count: retry,
- max: MAX_RETRIES,
- })
- if (!result.shouldRetry) {
- break
- }
- }
- }
- await processor.end()
- const queued = state().queued.get(input.sessionID) ?? []
- if (!result.blocked && !result.info.error) {
- if ((await stream.finishReason) === "tool-calls") {
- continue
- }
- const unprocessed = queued.filter((x) => x.messageID > result.info.id)
- if (unprocessed.length) {
- continue
- }
- }
- for (const item of queued) {
- item.callback(result)
- }
- state().queued.delete(input.sessionID)
- SessionCompaction.prune(input)
- MessageSummary.summarize({
- sessionID: input.sessionID,
- messageID: result.info.parentID,
- providerID: model.providerID,
- })
- return result
- }
- }
- async function getMessages(input: {
- sessionID: string
- model: ModelsDev.Model
- providerID: string
- signal: AbortSignal
- }) {
- let msgs = await Session.messages(input.sessionID).then(MessageV2.filterCompacted)
- const lastAssistant = msgs.findLast((msg) => msg.info.role === "assistant")
- if (
- lastAssistant?.info.role === "assistant" &&
- SessionCompaction.isOverflow({
- tokens: lastAssistant.info.tokens,
- model: input.model,
- })
- ) {
- const summaryMsg = await SessionCompaction.run({
- sessionID: input.sessionID,
- providerID: input.providerID,
- modelID: input.model.id,
- signal: input.signal,
- })
- const resumeMsgID = Identifier.ascending("message")
- const resumeMsg = {
- info: await Session.updateMessage({
- id: resumeMsgID,
- role: "user",
- sessionID: input.sessionID,
- time: {
- created: Date.now(),
- },
- }),
- parts: [
- await Session.updatePart({
- type: "text",
- sessionID: input.sessionID,
- messageID: resumeMsgID,
- id: Identifier.ascending("part"),
- text: "Use the above summary generated from your last session to resume from where you left off.",
- time: {
- start: Date.now(),
- end: Date.now(),
- },
- synthetic: true,
- }),
- ],
- }
- msgs = [summaryMsg, resumeMsg]
- }
- return msgs
- }
- async function resolveModel(input: { model: PromptInput["model"]; agent: Agent.Info }) {
- if (input.model) {
- return input.model
- }
- if (input.agent.model) {
- return input.agent.model
- }
- return Provider.defaultModel()
- }
- async function resolveSystemPrompt(input: {
- system?: string
- agent: Agent.Info
- providerID: string
- modelID: string
- }) {
- let system = SystemPrompt.header(input.providerID)
- system.push(
- ...(() => {
- if (input.system) return [input.system]
- if (input.agent.prompt) return [input.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")]
- return system
- }
- async function resolveTools(input: {
- agent: Agent.Info
- sessionID: string
- modelID: string
- providerID: string
- tools?: Record<string, boolean>
- processor: Processor
- }) {
- const tools: Record<string, AITool> = {}
- const enabledTools = pipe(
- input.agent.tools,
- mergeDeep(await ToolRegistry.enabled(input.providerID, input.modelID, input.agent)),
- mergeDeep(input.tools ?? {}),
- )
- for (const item of await ToolRegistry.tools(input.providerID, input.modelID)) {
- if (Wildcard.all(item.id, enabledTools) === false) continue
- const schema = ProviderTransform.schema(input.providerID, input.modelID, z.toJSONSchema(item.parameters))
- tools[item.id] = tool({
- id: item.id as any,
- description: item.description,
- inputSchema: jsonSchema(schema as any),
- 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: input.processor.message.id,
- callID: options.toolCallId,
- extra: {
- modelID: input.modelID,
- providerID: input.providerID,
- },
- agent: input.agent.name,
- metadata: async (val) => {
- const match = input.processor.partFromToolCall(options.toolCallId)
- if (match && match.state.status === "running") {
- await Session.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) => {
- await Plugin.trigger(
- "tool.execute.before",
- {
- tool: key,
- sessionID: input.sessionID,
- callID: opts.toolCallId,
- },
- {
- args,
- },
- )
- const result = await execute(args, opts)
- const output = result.content
- .filter((x: any) => x.type === "text")
- .map((x: any) => x.text)
- .join("\n\n")
- await Plugin.trigger(
- "tool.execute.after",
- {
- tool: key,
- sessionID: input.sessionID,
- callID: opts.toolCallId,
- },
- result,
- )
- return {
- title: "",
- metadata: {},
- output,
- }
- }
- item.toModelOutput = (result) => {
- return {
- type: "text",
- value: result.output,
- }
- }
- tools[key] = item
- }
- return tools
- }
- async function createUserMessage(input: PromptInput) {
- const info: MessageV2.Info = {
- id: input.messageID ?? Identifier.ascending("message"),
- role: "user",
- sessionID: input.sessionID,
- time: {
- created: Date.now(),
- },
- }
- const parts = 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: info.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: info.id,
- sessionID: input.sessionID,
- type: "text",
- synthetic: true,
- text: Buffer.from(part.url, "base64url").toString(),
- },
- {
- ...part,
- id: part.id ?? Identifier.ascending("part"),
- messageID: info.id,
- sessionID: input.sessionID,
- },
- ]
- }
- break
- case "file:":
- log.info("file", { mime: part.mime })
- // have to normalize, symbol search returns absolute paths
- // Decode the pathname since URL constructor doesn't automatically decode it
- const filepath = fileURLToPath(part.url)
- const stat = await Bun.file(filepath).stat()
- if (stat.isDirectory()) {
- part.mime = "application/x-directory"
- }
- 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 filePathURI = 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(filePathURI)
- 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 - 1, 0)
- if (end) {
- limit = end - offset
- }
- }
- const args = { filePath: filepath, offset, limit }
- const result = await ReadTool.init().then((t) =>
- t.execute(args, {
- sessionID: input.sessionID,
- abort: new AbortController().signal,
- agent: input.agent!,
- messageID: info.id,
- extra: { bypassCwdCheck: true },
- metadata: async () => {},
- }),
- )
- return [
- {
- id: Identifier.ascending("part"),
- messageID: info.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: info.id,
- sessionID: input.sessionID,
- type: "text",
- synthetic: true,
- text: result.output,
- },
- {
- ...part,
- id: part.id ?? Identifier.ascending("part"),
- messageID: info.id,
- sessionID: input.sessionID,
- },
- ]
- }
- if (part.mime === "application/x-directory") {
- const args = { path: filepath }
- const result = await ListTool.init().then((t) =>
- t.execute(args, {
- sessionID: input.sessionID,
- abort: new AbortController().signal,
- agent: input.agent!,
- messageID: info.id,
- extra: { bypassCwdCheck: true },
- metadata: async () => {},
- }),
- )
- return [
- {
- id: Identifier.ascending("part"),
- messageID: info.id,
- sessionID: input.sessionID,
- type: "text",
- synthetic: true,
- text: `Called the list tool with the following input: ${JSON.stringify(args)}`,
- },
- {
- id: Identifier.ascending("part"),
- messageID: info.id,
- sessionID: input.sessionID,
- type: "text",
- synthetic: true,
- text: result.output,
- },
- {
- ...part,
- id: part.id ?? Identifier.ascending("part"),
- messageID: info.id,
- sessionID: input.sessionID,
- },
- ]
- }
- const file = Bun.file(filepath)
- FileTime.read(input.sessionID, filepath)
- return [
- {
- id: Identifier.ascending("part"),
- messageID: info.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: info.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: info.id,
- sessionID: input.sessionID,
- },
- {
- id: Identifier.ascending("part"),
- messageID: info.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: info.id,
- sessionID: input.sessionID,
- },
- ]
- }),
- ).then((x) => x.flat())
- await Plugin.trigger(
- "chat.message",
- {},
- {
- message: info,
- parts,
- },
- )
- await Session.updateMessage(info)
- for (const part of parts) {
- await Session.updatePart(part)
- }
- return {
- info,
- parts,
- }
- }
- function insertReminders(input: { messages: MessageV2.WithParts[]; agent: Agent.Info }) {
- const userMessage = input.messages.findLast((msg) => msg.info.role === "user")
- if (!userMessage) return input.messages
- if (input.agent.name === "plan") {
- userMessage.parts.push({
- id: Identifier.ascending("part"),
- messageID: userMessage.info.id,
- sessionID: userMessage.info.sessionID,
- type: "text",
- text: PROMPT_PLAN,
- synthetic: true,
- })
- }
- const wasPlan = input.messages.some((msg) => msg.info.role === "assistant" && msg.info.mode === "plan")
- if (wasPlan && input.agent.name === "build") {
- userMessage.parts.push({
- id: Identifier.ascending("part"),
- messageID: userMessage.info.id,
- sessionID: userMessage.info.sessionID,
- type: "text",
- text: BUILD_SWITCH,
- synthetic: true,
- })
- }
- return input.messages
- }
- /**
- * Maps tool names to ACP tool kinds for consistent categorization.
- * - read: Tools that read data (read, glob, grep, list, webfetch, docs)
- * - edit: Tools that modify state (edit, write, bash)
- * - other: All other tools (MCP tools, task, todowrite, etc.)
- */
- function determineToolKind(toolName: string): "read" | "edit" | "other" {
- const readTools = [
- "read",
- "glob",
- "grep",
- "list",
- "webfetch",
- "context7_resolve_library_id",
- "context7_get_library_docs",
- ]
- const editTools = ["edit", "write", "bash"]
- if (readTools.includes(toolName.toLowerCase())) return "read"
- if (editTools.includes(toolName.toLowerCase())) return "edit"
- return "other"
- }
- /**
- * Extracts file/directory locations from tool inputs for ACP notifications.
- * Returns array of {path} objects that ACP clients can use for navigation.
- *
- * Examples:
- * - read({filePath: "/foo/bar.ts"}) -> [{path: "/foo/bar.ts"}]
- * - glob({pattern: "*.ts", path: "/src"}) -> [{path: "/src"}]
- * - bash({command: "ls"}) -> [] (no file references)
- */
- function extractLocations(toolName: string, input: Record<string, any>): { path: string }[] {
- try {
- switch (toolName.toLowerCase()) {
- case "read":
- case "edit":
- case "write":
- return input["filePath"] ? [{ path: input["filePath"] }] : []
- case "glob":
- case "grep":
- return input["path"] ? [{ path: input["path"] }] : []
- case "bash":
- return []
- case "list":
- return input["path"] ? [{ path: input["path"] }] : []
- default:
- return []
- }
- } catch {
- return []
- }
- }
- export type Processor = Awaited<ReturnType<typeof createProcessor>>
- async function createProcessor(input: {
- sessionID: string
- providerID: string
- model: ModelsDev.Model
- system: string[]
- agent: string
- abort: AbortSignal
- acpConnection?: {
- connection: any
- sessionId: string
- }
- }) {
- const toolcalls: Record<string, MessageV2.ToolPart> = {}
- let snapshot: string | undefined
- let blocked = false
- async function createMessage(parentID: string) {
- const msg: MessageV2.Info = {
- id: Identifier.ascending("message"),
- parentID,
- role: "assistant",
- system: input.system,
- mode: input.agent,
- path: {
- cwd: Instance.directory,
- root: Instance.worktree,
- },
- cost: 0,
- tokens: {
- input: 0,
- output: 0,
- reasoning: 0,
- cache: { read: 0, write: 0 },
- },
- modelID: input.model.id,
- providerID: input.providerID,
- time: {
- created: Date.now(),
- },
- sessionID: input.sessionID,
- }
- await Session.updateMessage(msg)
- return msg
- }
- let assistantMsg: MessageV2.Assistant | undefined
- const result = {
- async end() {
- if (assistantMsg) {
- assistantMsg.time.completed = Date.now()
- await Session.updateMessage(assistantMsg)
- assistantMsg = undefined
- }
- },
- async next(parentID: string) {
- if (assistantMsg) {
- throw new Error("end previous assistant message first")
- }
- assistantMsg = await createMessage(parentID)
- return assistantMsg
- },
- get message() {
- if (!assistantMsg) throw new Error("call next() first before accessing message")
- return assistantMsg
- },
- partFromToolCall(toolCallID: string) {
- return toolcalls[toolCallID]
- },
- async process(stream: StreamTextResult<Record<string, AITool>, never>, retries: { count: number; max: number }) {
- log.info("process")
- if (!assistantMsg) throw new Error("call next() first before processing")
- let shouldRetry = false
- try {
- let currentText: MessageV2.TextPart | undefined
- let reasoningMap: Record<string, MessageV2.ReasoningPart> = {}
- for await (const value of stream.fullStream) {
- input.abort.throwIfAborted()
- 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(),
- },
- metadata: value.providerMetadata,
- }
- break
- case "reasoning-delta":
- if (value.id in reasoningMap) {
- const part = reasoningMap[value.id]
- part.text += value.text
- if (value.providerMetadata) part.metadata = value.providerMetadata
- if (part.text) await Session.updatePart(part)
- }
- break
- case "reasoning-end":
- if (value.id in reasoningMap) {
- const part = reasoningMap[value.id]
- part.text = part.text.trimEnd()
- part.time = {
- ...part.time,
- end: Date.now(),
- }
- if (value.providerMetadata) part.metadata = value.providerMetadata
- await Session.updatePart(part)
- delete reasoningMap[value.id]
- }
- break
- case "tool-input-start":
- const part = await Session.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
- // Notify ACP client of pending tool call
- if (input.acpConnection) {
- await input.acpConnection.connection
- .sessionUpdate({
- sessionId: input.acpConnection.sessionId,
- update: {
- sessionUpdate: "tool_call",
- toolCallId: value.id,
- title: value.toolName,
- kind: determineToolKind(value.toolName),
- status: "pending",
- locations: [], // Will be populated when we have input
- rawInput: {},
- },
- })
- .catch((err: Error) => {
- log.error("failed to send tool pending to ACP", { error: err })
- })
- }
- break
- case "tool-input-delta":
- break
- case "tool-input-end":
- break
- case "tool-call": {
- const match = toolcalls[value.toolCallId]
- if (match) {
- const part = await Session.updatePart({
- ...match,
- tool: value.toolName,
- state: {
- status: "running",
- input: value.input,
- time: {
- start: Date.now(),
- },
- },
- metadata: value.providerMetadata,
- })
- toolcalls[value.toolCallId] = part as MessageV2.ToolPart
- // Notify ACP client that tool is running
- if (input.acpConnection) {
- await input.acpConnection.connection
- .sessionUpdate({
- sessionId: input.acpConnection.sessionId,
- update: {
- sessionUpdate: "tool_call_update",
- toolCallId: value.toolCallId,
- status: "in_progress",
- locations: extractLocations(value.toolName, value.input),
- rawInput: value.input,
- },
- })
- .catch((err: Error) => {
- log.error("failed to send tool in_progress to ACP", { error: err })
- })
- }
- }
- break
- }
- case "tool-result": {
- const match = toolcalls[value.toolCallId]
- if (match && match.state.status === "running") {
- await Session.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(),
- },
- attachments: value.output.attachments,
- },
- })
- // Notify ACP client that tool completed
- if (input.acpConnection) {
- await input.acpConnection.connection
- .sessionUpdate({
- sessionId: input.acpConnection.sessionId,
- update: {
- sessionUpdate: "tool_call_update",
- toolCallId: value.toolCallId,
- status: "completed",
- content: [
- {
- type: "content",
- content: {
- type: "text",
- text: value.output.output,
- },
- },
- ],
- rawOutput: value.output,
- },
- })
- .catch((err: Error) => {
- log.error("failed to send tool completed to ACP", { error: err })
- })
- }
- delete toolcalls[value.toolCallId]
- }
- break
- }
- case "tool-error": {
- const match = toolcalls[value.toolCallId]
- if (match && match.state.status === "running") {
- await Session.updatePart({
- ...match,
- state: {
- status: "error",
- input: value.input,
- error: (value.error as any).toString(),
- metadata: value.error instanceof Permission.RejectedError ? value.error.metadata : undefined,
- time: {
- start: match.state.time.start,
- end: Date.now(),
- },
- },
- })
- // Notify ACP client of tool error
- if (input.acpConnection) {
- await input.acpConnection.connection
- .sessionUpdate({
- sessionId: input.acpConnection.sessionId,
- update: {
- sessionUpdate: "tool_call_update",
- toolCallId: value.toolCallId,
- status: "failed",
- content: [
- {
- type: "content",
- content: {
- type: "text",
- text: `Error: ${(value.error as any).toString()}`,
- },
- },
- ],
- rawOutput: {
- error: (value.error as any).toString(),
- },
- },
- })
- .catch((err: Error) => {
- log.error("failed to send tool error to ACP", { error: err })
- })
- }
- if (value.error instanceof Permission.RejectedError) {
- blocked = true
- }
- delete toolcalls[value.toolCallId]
- }
- break
- }
- case "error":
- throw value.error
- case "start-step":
- snapshot = await Snapshot.track()
- await Session.updatePart({
- id: Identifier.ascending("part"),
- messageID: assistantMsg.id,
- sessionID: assistantMsg.sessionID,
- snapshot,
- type: "step-start",
- })
- break
- case "finish-step":
- const usage = Session.getUsage({
- model: input.model,
- usage: value.usage,
- metadata: value.providerMetadata,
- })
- assistantMsg.cost += usage.cost
- assistantMsg.tokens = usage.tokens
- await Session.updatePart({
- id: Identifier.ascending("part"),
- snapshot: await Snapshot.track(),
- messageID: assistantMsg.id,
- sessionID: assistantMsg.sessionID,
- type: "step-finish",
- tokens: usage.tokens,
- cost: usage.cost,
- })
- await Session.updateMessage(assistantMsg)
- if (snapshot) {
- const patch = await Snapshot.patch(snapshot)
- if (patch.files.length) {
- await Session.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(),
- },
- metadata: value.providerMetadata,
- }
- break
- case "text-delta":
- if (currentText) {
- currentText.text += value.text
- if (value.providerMetadata) currentText.metadata = value.providerMetadata
- if (currentText.text) await Session.updatePart(currentText)
- // Send streaming chunk to ACP client
- if (input.acpConnection && value.text) {
- await input.acpConnection.connection
- .sessionUpdate({
- sessionId: input.acpConnection.sessionId,
- update: {
- sessionUpdate: "agent_message_chunk",
- content: {
- type: "text",
- text: value.text,
- },
- },
- })
- .catch((err: Error) => {
- log.error("failed to send text delta to ACP", { error: err })
- // Don't fail the whole request if ACP notification fails
- })
- }
- }
- break
- case "text-end":
- if (currentText) {
- currentText.text = currentText.text.trimEnd()
- currentText.time = {
- start: Date.now(),
- end: Date.now(),
- }
- if (value.providerMetadata) currentText.metadata = value.providerMetadata
- await Session.updatePart(currentText)
- }
- currentText = undefined
- break
- case "finish":
- assistantMsg.time.completed = Date.now()
- await Session.updateMessage(assistantMsg)
- break
- default:
- log.info("unhandled", {
- ...value,
- })
- continue
- }
- }
- } catch (e) {
- log.error("process", {
- error: e,
- })
- const error = MessageV2.fromError(e, { providerID: input.providerID })
- if (retries.count < retries.max && MessageV2.APIError.isInstance(error) && error.data.isRetryable) {
- shouldRetry = true
- await Session.updatePart({
- id: Identifier.ascending("part"),
- messageID: assistantMsg.id,
- sessionID: assistantMsg.sessionID,
- type: "retry",
- attempt: retries.count + 1,
- time: {
- created: Date.now(),
- },
- error,
- })
- } else {
- assistantMsg.error = error
- Bus.publish(Session.Event.Error, {
- sessionID: assistantMsg.sessionID,
- error: assistantMsg.error,
- })
- }
- }
- const p = await Session.getParts(assistantMsg.id)
- for (const part of p) {
- if (part.type === "tool" && part.state.status !== "completed" && part.state.status !== "error") {
- Session.updatePart({
- ...part,
- state: {
- status: "error",
- error: "Tool execution aborted",
- time: {
- start: Date.now(),
- end: Date.now(),
- },
- input: {},
- },
- })
- }
- }
- if (!shouldRetry) {
- assistantMsg.time.completed = Date.now()
- }
- await Session.updateMessage(assistantMsg)
- return { info: assistantMsg, parts: p, blocked, shouldRetry }
- },
- }
- return result
- }
- function isBusy(sessionID: string) {
- return SessionLock.isLocked(sessionID)
- }
- function lock(sessionID: string) {
- const handle = SessionLock.acquire({
- sessionID,
- })
- log.info("locking", { sessionID })
- return {
- signal: handle.signal,
- abort: handle.abort,
- async [Symbol.dispose]() {
- handle[Symbol.dispose]()
- log.info("unlocking", { sessionID })
- const session = await Session.get(sessionID)
- if (session.parentID) return
- Bus.publish(Event.Idle, {
- sessionID,
- })
- },
- }
- }
- export const ShellInput = z.object({
- sessionID: Identifier.schema("session"),
- agent: z.string(),
- command: z.string(),
- })
- export type ShellInput = z.infer<typeof ShellInput>
- export async function shell(input: ShellInput) {
- using abort = lock(input.sessionID)
- const session = await Session.get(input.sessionID)
- if (session.revert) {
- SessionRevert.cleanup(session)
- }
- const userMsg: MessageV2.User = {
- id: Identifier.ascending("message"),
- sessionID: input.sessionID,
- time: {
- created: Date.now(),
- },
- role: "user",
- }
- await Session.updateMessage(userMsg)
- const userPart: MessageV2.Part = {
- type: "text",
- id: Identifier.ascending("part"),
- messageID: userMsg.id,
- sessionID: input.sessionID,
- text: "The following tool was executed by the user",
- synthetic: true,
- }
- await Session.updatePart(userPart)
- const msg: MessageV2.Assistant = {
- id: Identifier.ascending("message"),
- sessionID: input.sessionID,
- parentID: userMsg.id,
- system: [],
- mode: input.agent,
- cost: 0,
- path: {
- cwd: Instance.directory,
- root: Instance.worktree,
- },
- time: {
- created: Date.now(),
- },
- role: "assistant",
- tokens: {
- input: 0,
- output: 0,
- reasoning: 0,
- cache: { read: 0, write: 0 },
- },
- modelID: "",
- providerID: "",
- }
- await Session.updateMessage(msg)
- const part: MessageV2.Part = {
- type: "tool",
- id: Identifier.ascending("part"),
- messageID: msg.id,
- sessionID: input.sessionID,
- tool: "bash",
- callID: ulid(),
- state: {
- status: "running",
- time: {
- start: Date.now(),
- },
- input: {
- command: input.command,
- },
- },
- }
- await Session.updatePart(part)
- const shell = process.env["SHELL"] ?? "bash"
- const shellName = path.basename(shell)
- const invocations: Record<string, { args: string[] }> = {
- nu: {
- args: ["-c", input.command],
- },
- fish: {
- args: ["-c", input.command],
- },
- zsh: {
- args: [
- "-c",
- "-l",
- `
- [[ -f ~/.zshenv ]] && source ~/.zshenv >/dev/null 2>&1 || true
- [[ -f "\${ZDOTDIR:-$HOME}/.zshrc" ]] && source "\${ZDOTDIR:-$HOME}/.zshrc" >/dev/null 2>&1 || true
- ${input.command}
- `,
- ],
- },
- bash: {
- args: [
- "-c",
- "-l",
- `
- [[ -f ~/.bashrc ]] && source ~/.bashrc >/dev/null 2>&1 || true
- ${input.command}
- `,
- ],
- },
- // Fallback: any shell that doesn't match those above
- "": {
- args: ["-c", "-l", `${input.command}`],
- },
- }
- const matchingInvocation = invocations[shellName] ?? invocations[""]
- const args = matchingInvocation?.args
- const proc = spawn(shell, args, {
- cwd: Instance.directory,
- signal: abort.signal,
- detached: true,
- stdio: ["ignore", "pipe", "pipe"],
- env: {
- ...process.env,
- TERM: "dumb",
- },
- })
- abort.signal.addEventListener("abort", () => {
- if (!proc.pid) return
- process.kill(-proc.pid)
- })
- let output = ""
- proc.stdout?.on("data", (chunk) => {
- output += chunk.toString()
- if (part.state.status === "running") {
- part.state.metadata = {
- output: output,
- description: "",
- }
- Session.updatePart(part)
- }
- })
- proc.stderr?.on("data", (chunk) => {
- output += chunk.toString()
- if (part.state.status === "running") {
- part.state.metadata = {
- output: output,
- description: "",
- }
- Session.updatePart(part)
- }
- })
- await new Promise<void>((resolve) => {
- proc.on("close", () => {
- resolve()
- })
- })
- msg.time.completed = Date.now()
- await Session.updateMessage(msg)
- if (part.state.status === "running") {
- part.state = {
- status: "completed",
- time: {
- ...part.state.time,
- end: Date.now(),
- },
- input: part.state.input,
- title: "",
- metadata: {
- output,
- description: "",
- },
- output,
- }
- await Session.updatePart(part)
- }
- return { info: msg, parts: [part] }
- }
- export const CommandInput = z.object({
- messageID: Identifier.schema("message").optional(),
- sessionID: Identifier.schema("session"),
- agent: z.string().optional(),
- model: z.string().optional(),
- arguments: z.string(),
- command: z.string(),
- })
- export type CommandInput = z.infer<typeof CommandInput>
- const bashRegex = /!`([^`]+)`/g
- /**
- * Regular expression to match @ file references in text
- * Matches @ followed by file paths, excluding commas, periods at end of sentences, and backticks
- * Does not match when preceded by word characters or backticks (to avoid email addresses and quoted references)
- */
- export async function command(input: CommandInput) {
- log.info("command", input)
- const command = await Command.get(input.command)
- const agentName = command.agent ?? input.agent ?? "build"
- let template = command.template.replaceAll("$ARGUMENTS", input.arguments)
- const shell = ConfigMarkdown.shell(template)
- if (shell.length > 0) {
- const results = await Promise.all(
- shell.map(async ([, cmd]) => {
- try {
- return await $`${{ raw: cmd }}`.nothrow().text()
- } catch (error) {
- return `Error executing command: ${error instanceof Error ? error.message : String(error)}`
- }
- }),
- )
- let index = 0
- template = template.replace(bashRegex, () => results[index++])
- }
- const parts = [
- {
- type: "text",
- text: template,
- },
- ] as PromptInput["parts"]
- const files = ConfigMarkdown.files(template)
- await Promise.all(
- files.map(async (match) => {
- const name = match[1]
- const filepath = name.startsWith("~/")
- ? path.join(os.homedir(), name.slice(2))
- : path.resolve(Instance.worktree, name)
- const stats = await fs.stat(filepath).catch(() => undefined)
- if (!stats) {
- const agent = await Agent.get(name)
- if (agent) {
- parts.push({
- type: "agent",
- name: agent.name,
- })
- }
- return
- }
- if (stats.isDirectory()) {
- parts.push({
- type: "file",
- url: `file://${filepath}`,
- filename: name,
- mime: "application/x-directory",
- })
- return
- }
- parts.push({
- type: "file",
- url: `file://${filepath}`,
- filename: name,
- mime: "text/plain",
- })
- }),
- )
- const model = await (async () => {
- if (command.model) {
- return Provider.parseModel(command.model)
- }
- if (command.agent) {
- const cmdAgent = await Agent.get(command.agent)
- if (cmdAgent.model) {
- return cmdAgent.model
- }
- }
- if (input.model) {
- return Provider.parseModel(input.model)
- }
- return await Provider.defaultModel()
- })()
- const agent = await Agent.get(agentName)
- if ((agent.mode === "subagent" && command.subtask !== false) || command.subtask === true) {
- using abort = lock(input.sessionID)
- const userMsg: MessageV2.User = {
- id: Identifier.ascending("message"),
- sessionID: input.sessionID,
- time: {
- created: Date.now(),
- },
- role: "user",
- }
- await Session.updateMessage(userMsg)
- const userPart: MessageV2.Part = {
- type: "text",
- id: Identifier.ascending("part"),
- messageID: userMsg.id,
- sessionID: input.sessionID,
- text: "The following tool was executed by the user",
- synthetic: true,
- }
- await Session.updatePart(userPart)
- const assistantMsg: MessageV2.Assistant = {
- id: Identifier.ascending("message"),
- sessionID: input.sessionID,
- parentID: userMsg.id,
- system: [],
- mode: agentName,
- cost: 0,
- path: {
- cwd: Instance.directory,
- root: Instance.worktree,
- },
- time: {
- created: Date.now(),
- },
- role: "assistant",
- tokens: {
- input: 0,
- output: 0,
- reasoning: 0,
- cache: { read: 0, write: 0 },
- },
- modelID: model.modelID,
- providerID: model.providerID,
- }
- await Session.updateMessage(assistantMsg)
- const args = {
- description: "Consulting " + agent.name,
- subagent_type: agent.name,
- prompt: template,
- }
- const toolPart: MessageV2.ToolPart = {
- type: "tool",
- id: Identifier.ascending("part"),
- messageID: assistantMsg.id,
- sessionID: input.sessionID,
- tool: "task",
- callID: ulid(),
- state: {
- status: "running",
- time: {
- start: Date.now(),
- },
- input: {
- description: args.description,
- subagent_type: args.subagent_type,
- // truncate prompt to preserve context
- prompt: args.prompt.length > 100 ? args.prompt.substring(0, 97) + "..." : args.prompt,
- },
- },
- }
- await Session.updatePart(toolPart)
- const result = await TaskTool.init().then((t) =>
- t.execute(args, {
- sessionID: input.sessionID,
- abort: abort.signal,
- agent: agent.name,
- messageID: assistantMsg.id,
- extra: {},
- metadata: async (metadata) => {
- if (toolPart.state.status === "running") {
- toolPart.state.metadata = metadata.metadata
- toolPart.state.title = metadata.title
- await Session.updatePart(toolPart)
- }
- },
- }),
- )
- assistantMsg.time.completed = Date.now()
- await Session.updateMessage(assistantMsg)
- if (toolPart.state.status === "running") {
- toolPart.state = {
- status: "completed",
- time: {
- ...toolPart.state.time,
- end: Date.now(),
- },
- input: toolPart.state.input,
- title: "",
- metadata: result.metadata,
- output: result.output,
- }
- await Session.updatePart(toolPart)
- }
- return { info: assistantMsg, parts: [toolPart] }
- }
- return prompt({
- sessionID: input.sessionID,
- messageID: input.messageID,
- model,
- agent: agentName,
- parts,
- })
- }
- async function ensureTitle(input: {
- session: Session.Info
- message: MessageV2.WithParts
- history: MessageV2.WithParts[]
- providerID: string
- modelID: string
- }) {
- if (input.session.parentID) return
- const isFirst =
- input.history.filter((m) => m.info.role === "user" && !m.parts.every((p) => "synthetic" in p && p.synthetic))
- .length === 1
- if (!isFirst) return
- const small =
- (await Provider.getSmallModel(input.providerID)) ?? (await Provider.getModel(input.providerID, input.modelID))
- const options = {
- ...ProviderTransform.options(small.providerID, small.modelID, input.session.id),
- ...small.info.options,
- }
- if (small.providerID === "openai" || small.modelID.includes("gpt-5")) {
- options["reasoningEffort"] = "minimal"
- }
- if (small.providerID === "google") {
- options["thinkingConfig"] = {
- thinkingBudget: 0,
- }
- }
- generateText({
- maxOutputTokens: small.info.reasoning ? 1500 : 20,
- providerOptions: ProviderTransform.providerOptions(small.npm, small.providerID, options),
- messages: [
- ...SystemPrompt.title(small.providerID).map(
- (x): ModelMessage => ({
- role: "system",
- content: x,
- }),
- ),
- ...MessageV2.toModelMessage([
- {
- info: {
- id: Identifier.ascending("message"),
- role: "user",
- sessionID: input.session.id,
- time: {
- created: Date.now(),
- },
- },
- parts: input.message.parts,
- },
- ]),
- ],
- model: small.language,
- })
- .then((result) => {
- if (result.text)
- return Session.update(input.session.id, (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((error) => {
- log.error("failed to generate title", { error, model: small.info.id })
- })
- }
- }
|