prompt.ts 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389
  1. import path from "path"
  2. import os from "os"
  3. import fs from "fs/promises"
  4. import z from "zod"
  5. import { Identifier } from "../id/id"
  6. import { MessageV2 } from "./message-v2"
  7. import { Log } from "../util/log"
  8. import { SessionRevert } from "./revert"
  9. import { Session } from "."
  10. import { Agent } from "../agent/agent"
  11. import { Provider } from "../provider/provider"
  12. import { type Tool as AITool, tool, jsonSchema } from "ai"
  13. import { SessionCompaction } from "./compaction"
  14. import { Instance } from "../project/instance"
  15. import { Bus } from "../bus"
  16. import { ProviderTransform } from "../provider/transform"
  17. import { SystemPrompt } from "./system"
  18. import { Plugin } from "../plugin"
  19. import PROMPT_PLAN from "../session/prompt/plan.txt"
  20. import BUILD_SWITCH from "../session/prompt/build-switch.txt"
  21. import MAX_STEPS from "../session/prompt/max-steps.txt"
  22. import { defer } from "../util/defer"
  23. import { mergeDeep, pipe } from "remeda"
  24. import { ToolRegistry } from "../tool/registry"
  25. import { Wildcard } from "../util/wildcard"
  26. import { MCP } from "../mcp"
  27. import { LSP } from "../lsp"
  28. import { ReadTool } from "../tool/read"
  29. import { ListTool } from "../tool/ls"
  30. import { FileTime } from "../file/time"
  31. import { ulid } from "ulid"
  32. import { spawn } from "child_process"
  33. import { Command } from "../command"
  34. import { $, fileURLToPath } from "bun"
  35. import { ConfigMarkdown } from "../config/markdown"
  36. import { SessionSummary } from "./summary"
  37. import { NamedError } from "@opencode-ai/util/error"
  38. import { fn } from "@/util/fn"
  39. import { SessionProcessor } from "./processor"
  40. import { TaskTool } from "@/tool/task"
  41. import { SessionStatus } from "./status"
  42. import { LLM } from "./llm"
  43. import { iife } from "@/util/iife"
  44. import { Shell } from "@/shell/shell"
  45. // @ts-ignore
  46. globalThis.AI_SDK_LOG_WARNINGS = false
  47. export namespace SessionPrompt {
  48. const log = Log.create({ service: "session.prompt" })
  49. export const OUTPUT_TOKEN_MAX = 32_000
  50. const state = Instance.state(
  51. () => {
  52. const data: Record<
  53. string,
  54. {
  55. abort: AbortController
  56. callbacks: {
  57. resolve(input: MessageV2.WithParts): void
  58. reject(): void
  59. }[]
  60. }
  61. > = {}
  62. return data
  63. },
  64. async (current) => {
  65. for (const item of Object.values(current)) {
  66. item.abort.abort()
  67. }
  68. },
  69. )
  70. export function assertNotBusy(sessionID: string) {
  71. const match = state()[sessionID]
  72. if (match) throw new Session.BusyError(sessionID)
  73. }
  74. export const PromptInput = z.object({
  75. sessionID: Identifier.schema("session"),
  76. messageID: Identifier.schema("message").optional(),
  77. model: z
  78. .object({
  79. providerID: z.string(),
  80. modelID: z.string(),
  81. })
  82. .optional(),
  83. agent: z.string().optional(),
  84. noReply: z.boolean().optional(),
  85. tools: z.record(z.string(), z.boolean()).optional(),
  86. system: z.string().optional(),
  87. parts: z.array(
  88. z.discriminatedUnion("type", [
  89. MessageV2.TextPart.omit({
  90. messageID: true,
  91. sessionID: true,
  92. })
  93. .partial({
  94. id: true,
  95. })
  96. .meta({
  97. ref: "TextPartInput",
  98. }),
  99. MessageV2.FilePart.omit({
  100. messageID: true,
  101. sessionID: true,
  102. })
  103. .partial({
  104. id: true,
  105. })
  106. .meta({
  107. ref: "FilePartInput",
  108. }),
  109. MessageV2.AgentPart.omit({
  110. messageID: true,
  111. sessionID: true,
  112. })
  113. .partial({
  114. id: true,
  115. })
  116. .meta({
  117. ref: "AgentPartInput",
  118. }),
  119. MessageV2.SubtaskPart.omit({
  120. messageID: true,
  121. sessionID: true,
  122. })
  123. .partial({
  124. id: true,
  125. })
  126. .meta({
  127. ref: "SubtaskPartInput",
  128. }),
  129. ]),
  130. ),
  131. })
  132. export type PromptInput = z.infer<typeof PromptInput>
  133. export const prompt = fn(PromptInput, async (input) => {
  134. const session = await Session.get(input.sessionID)
  135. await SessionRevert.cleanup(session)
  136. const message = await createUserMessage(input)
  137. await Session.touch(input.sessionID)
  138. if (input.noReply === true) {
  139. return message
  140. }
  141. return loop(input.sessionID)
  142. })
  143. export async function resolvePromptParts(template: string): Promise<PromptInput["parts"]> {
  144. const parts: PromptInput["parts"] = [
  145. {
  146. type: "text",
  147. text: template,
  148. },
  149. ]
  150. const files = ConfigMarkdown.files(template)
  151. const seen = new Set<string>()
  152. await Promise.all(
  153. files.map(async (match) => {
  154. const name = match[1]
  155. if (seen.has(name)) return
  156. seen.add(name)
  157. const filepath = name.startsWith("~/")
  158. ? path.join(os.homedir(), name.slice(2))
  159. : path.resolve(Instance.worktree, name)
  160. const stats = await fs.stat(filepath).catch(() => undefined)
  161. if (!stats) {
  162. const agent = await Agent.get(name)
  163. if (agent) {
  164. parts.push({
  165. type: "agent",
  166. name: agent.name,
  167. })
  168. }
  169. return
  170. }
  171. if (stats.isDirectory()) {
  172. parts.push({
  173. type: "file",
  174. url: `file://${filepath}`,
  175. filename: name,
  176. mime: "application/x-directory",
  177. })
  178. return
  179. }
  180. parts.push({
  181. type: "file",
  182. url: `file://${filepath}`,
  183. filename: name,
  184. mime: "text/plain",
  185. })
  186. }),
  187. )
  188. return parts
  189. }
  190. function start(sessionID: string) {
  191. const s = state()
  192. if (s[sessionID]) return
  193. const controller = new AbortController()
  194. s[sessionID] = {
  195. abort: controller,
  196. callbacks: [],
  197. }
  198. return controller.signal
  199. }
  200. export function cancel(sessionID: string) {
  201. log.info("cancel", { sessionID })
  202. const s = state()
  203. const match = s[sessionID]
  204. if (!match) return
  205. match.abort.abort()
  206. for (const item of match.callbacks) {
  207. item.reject()
  208. }
  209. delete s[sessionID]
  210. SessionStatus.set(sessionID, { type: "idle" })
  211. return
  212. }
  213. export const loop = fn(Identifier.schema("session"), async (sessionID) => {
  214. const abort = start(sessionID)
  215. if (!abort) {
  216. return new Promise<MessageV2.WithParts>((resolve, reject) => {
  217. const callbacks = state()[sessionID].callbacks
  218. callbacks.push({ resolve, reject })
  219. })
  220. }
  221. using _ = defer(() => cancel(sessionID))
  222. let step = 0
  223. while (true) {
  224. SessionStatus.set(sessionID, { type: "busy" })
  225. log.info("loop", { step, sessionID })
  226. if (abort.aborted) break
  227. let msgs = await MessageV2.filterCompacted(MessageV2.stream(sessionID))
  228. let lastUser: MessageV2.User | undefined
  229. let lastAssistant: MessageV2.Assistant | undefined
  230. let lastFinished: MessageV2.Assistant | undefined
  231. let tasks: (MessageV2.CompactionPart | MessageV2.SubtaskPart)[] = []
  232. for (let i = msgs.length - 1; i >= 0; i--) {
  233. const msg = msgs[i]
  234. if (!lastUser && msg.info.role === "user") lastUser = msg.info as MessageV2.User
  235. if (!lastAssistant && msg.info.role === "assistant") lastAssistant = msg.info as MessageV2.Assistant
  236. if (!lastFinished && msg.info.role === "assistant" && msg.info.finish)
  237. lastFinished = msg.info as MessageV2.Assistant
  238. if (lastUser && lastFinished) break
  239. const task = msg.parts.filter((part) => part.type === "compaction" || part.type === "subtask")
  240. if (task && !lastFinished) {
  241. tasks.push(...task)
  242. }
  243. }
  244. if (!lastUser) throw new Error("No user message found in stream. This should never happen.")
  245. if (
  246. lastAssistant?.finish &&
  247. !["tool-calls", "unknown"].includes(lastAssistant.finish) &&
  248. lastUser.id < lastAssistant.id
  249. ) {
  250. log.info("exiting loop", { sessionID })
  251. break
  252. }
  253. step++
  254. if (step === 1)
  255. ensureTitle({
  256. session: await Session.get(sessionID),
  257. modelID: lastUser.model.modelID,
  258. providerID: lastUser.model.providerID,
  259. message: msgs.find((m) => m.info.role === "user")!,
  260. history: msgs,
  261. })
  262. const model = await Provider.getModel(lastUser.model.providerID, lastUser.model.modelID)
  263. const task = tasks.pop()
  264. // pending subtask
  265. // TODO: centralize "invoke tool" logic
  266. if (task?.type === "subtask") {
  267. const taskTool = await TaskTool.init()
  268. const assistantMessage = (await Session.updateMessage({
  269. id: Identifier.ascending("message"),
  270. role: "assistant",
  271. parentID: lastUser.id,
  272. sessionID,
  273. mode: task.agent,
  274. agent: task.agent,
  275. path: {
  276. cwd: Instance.directory,
  277. root: Instance.worktree,
  278. },
  279. cost: 0,
  280. tokens: {
  281. input: 0,
  282. output: 0,
  283. reasoning: 0,
  284. cache: { read: 0, write: 0 },
  285. },
  286. modelID: model.id,
  287. providerID: model.providerID,
  288. time: {
  289. created: Date.now(),
  290. },
  291. })) as MessageV2.Assistant
  292. let part = (await Session.updatePart({
  293. id: Identifier.ascending("part"),
  294. messageID: assistantMessage.id,
  295. sessionID: assistantMessage.sessionID,
  296. type: "tool",
  297. callID: ulid(),
  298. tool: TaskTool.id,
  299. state: {
  300. status: "running",
  301. input: {
  302. prompt: task.prompt,
  303. description: task.description,
  304. subagent_type: task.agent,
  305. },
  306. time: {
  307. start: Date.now(),
  308. },
  309. },
  310. })) as MessageV2.ToolPart
  311. let executionError: Error | undefined
  312. const result = await taskTool
  313. .execute(
  314. {
  315. prompt: task.prompt,
  316. description: task.description,
  317. subagent_type: task.agent,
  318. },
  319. {
  320. agent: task.agent,
  321. messageID: assistantMessage.id,
  322. sessionID: sessionID,
  323. abort,
  324. async metadata(input) {
  325. await Session.updatePart({
  326. ...part,
  327. type: "tool",
  328. state: {
  329. ...part.state,
  330. ...input,
  331. },
  332. } satisfies MessageV2.ToolPart)
  333. },
  334. },
  335. )
  336. .catch((error) => {
  337. executionError = error
  338. log.error("subtask execution failed", { error, agent: task.agent, description: task.description })
  339. return undefined
  340. })
  341. assistantMessage.finish = "tool-calls"
  342. assistantMessage.time.completed = Date.now()
  343. await Session.updateMessage(assistantMessage)
  344. if (result && part.state.status === "running") {
  345. await Session.updatePart({
  346. ...part,
  347. state: {
  348. status: "completed",
  349. input: part.state.input,
  350. title: result.title,
  351. metadata: result.metadata,
  352. output: result.output,
  353. attachments: result.attachments,
  354. time: {
  355. ...part.state.time,
  356. end: Date.now(),
  357. },
  358. },
  359. } satisfies MessageV2.ToolPart)
  360. }
  361. if (!result) {
  362. await Session.updatePart({
  363. ...part,
  364. state: {
  365. status: "error",
  366. error: executionError ? `Tool execution failed: ${executionError.message}` : "Tool execution failed",
  367. time: {
  368. start: part.state.status === "running" ? part.state.time.start : Date.now(),
  369. end: Date.now(),
  370. },
  371. metadata: part.metadata,
  372. input: part.state.input,
  373. },
  374. } satisfies MessageV2.ToolPart)
  375. }
  376. continue
  377. }
  378. // pending compaction
  379. if (task?.type === "compaction") {
  380. const result = await SessionCompaction.process({
  381. messages: msgs,
  382. parentID: lastUser.id,
  383. abort,
  384. sessionID,
  385. auto: task.auto,
  386. })
  387. if (result === "stop") break
  388. continue
  389. }
  390. // context overflow, needs compaction
  391. if (
  392. lastFinished &&
  393. lastFinished.summary !== true &&
  394. SessionCompaction.isOverflow({ tokens: lastFinished.tokens, model })
  395. ) {
  396. await SessionCompaction.create({
  397. sessionID,
  398. agent: lastUser.agent,
  399. model: lastUser.model,
  400. auto: true,
  401. })
  402. continue
  403. }
  404. // normal processing
  405. const agent = await Agent.get(lastUser.agent)
  406. const maxSteps = agent.maxSteps ?? Infinity
  407. const isLastStep = step >= maxSteps
  408. msgs = insertReminders({
  409. messages: msgs,
  410. agent,
  411. })
  412. const processor = SessionProcessor.create({
  413. assistantMessage: (await Session.updateMessage({
  414. id: Identifier.ascending("message"),
  415. parentID: lastUser.id,
  416. role: "assistant",
  417. mode: agent.name,
  418. agent: agent.name,
  419. path: {
  420. cwd: Instance.directory,
  421. root: Instance.worktree,
  422. },
  423. cost: 0,
  424. tokens: {
  425. input: 0,
  426. output: 0,
  427. reasoning: 0,
  428. cache: { read: 0, write: 0 },
  429. },
  430. modelID: model.id,
  431. providerID: model.providerID,
  432. time: {
  433. created: Date.now(),
  434. },
  435. sessionID,
  436. })) as MessageV2.Assistant,
  437. sessionID: sessionID,
  438. model,
  439. abort,
  440. })
  441. const tools = await resolveTools({
  442. agent,
  443. sessionID,
  444. model,
  445. tools: lastUser.tools,
  446. processor,
  447. })
  448. if (step === 1) {
  449. SessionSummary.summarize({
  450. sessionID: sessionID,
  451. messageID: lastUser.id,
  452. })
  453. }
  454. const result = await processor.process({
  455. user: lastUser,
  456. agent,
  457. abort,
  458. sessionID,
  459. system: [...(await SystemPrompt.environment()), ...(await SystemPrompt.custom())],
  460. messages: [
  461. ...MessageV2.toModelMessage(msgs),
  462. ...(isLastStep
  463. ? [
  464. {
  465. role: "assistant" as const,
  466. content: MAX_STEPS,
  467. },
  468. ]
  469. : []),
  470. ],
  471. tools,
  472. model,
  473. })
  474. if (result === "stop") break
  475. continue
  476. }
  477. SessionCompaction.prune({ sessionID })
  478. for await (const item of MessageV2.stream(sessionID)) {
  479. if (item.info.role === "user") continue
  480. const queued = state()[sessionID]?.callbacks ?? []
  481. for (const q of queued) {
  482. q.resolve(item)
  483. }
  484. return item
  485. }
  486. throw new Error("Impossible")
  487. })
  488. async function lastModel(sessionID: string) {
  489. for await (const item of MessageV2.stream(sessionID)) {
  490. if (item.info.role === "user" && item.info.model) return item.info.model
  491. }
  492. return Provider.defaultModel()
  493. }
  494. async function resolveTools(input: {
  495. agent: Agent.Info
  496. model: Provider.Model
  497. sessionID: string
  498. tools?: Record<string, boolean>
  499. processor: SessionProcessor.Info
  500. }) {
  501. using _ = log.time("resolveTools")
  502. const tools: Record<string, AITool> = {}
  503. const enabledTools = pipe(
  504. input.agent.tools,
  505. mergeDeep(await ToolRegistry.enabled(input.agent)),
  506. mergeDeep(input.tools ?? {}),
  507. )
  508. for (const item of await ToolRegistry.tools(input.model.providerID)) {
  509. if (Wildcard.all(item.id, enabledTools) === false) continue
  510. const schema = ProviderTransform.schema(input.model, z.toJSONSchema(item.parameters))
  511. tools[item.id] = tool({
  512. id: item.id as any,
  513. description: item.description,
  514. inputSchema: jsonSchema(schema as any),
  515. async execute(args, options) {
  516. await Plugin.trigger(
  517. "tool.execute.before",
  518. {
  519. tool: item.id,
  520. sessionID: input.sessionID,
  521. callID: options.toolCallId,
  522. },
  523. {
  524. args,
  525. },
  526. )
  527. const result = await item.execute(args, {
  528. sessionID: input.sessionID,
  529. abort: options.abortSignal!,
  530. messageID: input.processor.message.id,
  531. callID: options.toolCallId,
  532. extra: { model: input.model },
  533. agent: input.agent.name,
  534. metadata: async (val) => {
  535. const match = input.processor.partFromToolCall(options.toolCallId)
  536. if (match && match.state.status === "running") {
  537. await Session.updatePart({
  538. ...match,
  539. state: {
  540. title: val.title,
  541. metadata: val.metadata,
  542. status: "running",
  543. input: args,
  544. time: {
  545. start: Date.now(),
  546. },
  547. },
  548. })
  549. }
  550. },
  551. })
  552. await Plugin.trigger(
  553. "tool.execute.after",
  554. {
  555. tool: item.id,
  556. sessionID: input.sessionID,
  557. callID: options.toolCallId,
  558. },
  559. result,
  560. )
  561. return result
  562. },
  563. toModelOutput(result) {
  564. return {
  565. type: "text",
  566. value: result.output,
  567. }
  568. },
  569. })
  570. }
  571. for (const [key, item] of Object.entries(await MCP.tools())) {
  572. if (Wildcard.all(key, enabledTools) === false) continue
  573. const execute = item.execute
  574. if (!execute) continue
  575. // Wrap execute to add plugin hooks and format output
  576. item.execute = async (args, opts) => {
  577. await Plugin.trigger(
  578. "tool.execute.before",
  579. {
  580. tool: key,
  581. sessionID: input.sessionID,
  582. callID: opts.toolCallId,
  583. },
  584. {
  585. args,
  586. },
  587. )
  588. const result = await execute(args, opts)
  589. await Plugin.trigger(
  590. "tool.execute.after",
  591. {
  592. tool: key,
  593. sessionID: input.sessionID,
  594. callID: opts.toolCallId,
  595. },
  596. result,
  597. )
  598. const textParts: string[] = []
  599. const attachments: MessageV2.FilePart[] = []
  600. for (const contentItem of result.content) {
  601. if (contentItem.type === "text") {
  602. textParts.push(contentItem.text)
  603. } else if (contentItem.type === "image") {
  604. attachments.push({
  605. id: Identifier.ascending("part"),
  606. sessionID: input.sessionID,
  607. messageID: input.processor.message.id,
  608. type: "file",
  609. mime: contentItem.mimeType,
  610. url: `data:${contentItem.mimeType};base64,${contentItem.data}`,
  611. })
  612. }
  613. // Add support for other types if needed
  614. }
  615. return {
  616. title: "",
  617. metadata: result.metadata ?? {},
  618. output: textParts.join("\n\n"),
  619. attachments,
  620. content: result.content, // directly return content to preserve ordering when outputting to model
  621. }
  622. }
  623. item.toModelOutput = (result) => {
  624. return {
  625. type: "text",
  626. value: result.output,
  627. }
  628. }
  629. tools[key] = item
  630. }
  631. return tools
  632. }
  633. async function createUserMessage(input: PromptInput) {
  634. const agent = await Agent.get(input.agent ?? "build")
  635. const info: MessageV2.Info = {
  636. id: input.messageID ?? Identifier.ascending("message"),
  637. role: "user",
  638. sessionID: input.sessionID,
  639. time: {
  640. created: Date.now(),
  641. },
  642. tools: input.tools,
  643. agent: agent.name,
  644. model: input.model ?? agent.model ?? (await lastModel(input.sessionID)),
  645. }
  646. const parts = await Promise.all(
  647. input.parts.map(async (part): Promise<MessageV2.Part[]> => {
  648. if (part.type === "file") {
  649. const url = new URL(part.url)
  650. switch (url.protocol) {
  651. case "data:":
  652. if (part.mime === "text/plain") {
  653. return [
  654. {
  655. id: Identifier.ascending("part"),
  656. messageID: info.id,
  657. sessionID: input.sessionID,
  658. type: "text",
  659. synthetic: true,
  660. text: `Called the Read tool with the following input: ${JSON.stringify({ filePath: part.filename })}`,
  661. },
  662. {
  663. id: Identifier.ascending("part"),
  664. messageID: info.id,
  665. sessionID: input.sessionID,
  666. type: "text",
  667. synthetic: true,
  668. text: Buffer.from(part.url, "base64url").toString(),
  669. },
  670. {
  671. ...part,
  672. id: part.id ?? Identifier.ascending("part"),
  673. messageID: info.id,
  674. sessionID: input.sessionID,
  675. },
  676. ]
  677. }
  678. break
  679. case "file:":
  680. log.info("file", { mime: part.mime })
  681. // have to normalize, symbol search returns absolute paths
  682. // Decode the pathname since URL constructor doesn't automatically decode it
  683. const filepath = fileURLToPath(part.url)
  684. const stat = await Bun.file(filepath).stat()
  685. if (stat.isDirectory()) {
  686. part.mime = "application/x-directory"
  687. }
  688. if (part.mime === "text/plain") {
  689. let offset: number | undefined = undefined
  690. let limit: number | undefined = undefined
  691. const range = {
  692. start: url.searchParams.get("start"),
  693. end: url.searchParams.get("end"),
  694. }
  695. if (range.start != null) {
  696. const filePathURI = part.url.split("?")[0]
  697. let start = parseInt(range.start)
  698. let end = range.end ? parseInt(range.end) : undefined
  699. // some LSP servers (eg, gopls) don't give full range in
  700. // workspace/symbol searches, so we'll try to find the
  701. // symbol in the document to get the full range
  702. if (start === end) {
  703. const symbols = await LSP.documentSymbol(filePathURI)
  704. for (const symbol of symbols) {
  705. let range: LSP.Range | undefined
  706. if ("range" in symbol) {
  707. range = symbol.range
  708. } else if ("location" in symbol) {
  709. range = symbol.location.range
  710. }
  711. if (range?.start?.line && range?.start?.line === start) {
  712. start = range.start.line
  713. end = range?.end?.line ?? start
  714. break
  715. }
  716. }
  717. }
  718. offset = Math.max(start - 1, 0)
  719. if (end) {
  720. limit = end - offset
  721. }
  722. }
  723. const args = { filePath: filepath, offset, limit }
  724. const pieces: MessageV2.Part[] = [
  725. {
  726. id: Identifier.ascending("part"),
  727. messageID: info.id,
  728. sessionID: input.sessionID,
  729. type: "text",
  730. synthetic: true,
  731. text: `Called the Read tool with the following input: ${JSON.stringify(args)}`,
  732. },
  733. ]
  734. await ReadTool.init()
  735. .then(async (t) => {
  736. const model = await Provider.getModel(info.model.providerID, info.model.modelID)
  737. const result = await t.execute(args, {
  738. sessionID: input.sessionID,
  739. abort: new AbortController().signal,
  740. agent: input.agent!,
  741. messageID: info.id,
  742. extra: { bypassCwdCheck: true, model },
  743. metadata: async () => {},
  744. })
  745. pieces.push({
  746. id: Identifier.ascending("part"),
  747. messageID: info.id,
  748. sessionID: input.sessionID,
  749. type: "text",
  750. synthetic: true,
  751. text: result.output,
  752. })
  753. if (result.attachments?.length) {
  754. pieces.push(
  755. ...result.attachments.map((attachment) => ({
  756. ...attachment,
  757. synthetic: true,
  758. filename: attachment.filename ?? part.filename,
  759. messageID: info.id,
  760. sessionID: input.sessionID,
  761. })),
  762. )
  763. } else {
  764. pieces.push({
  765. ...part,
  766. id: part.id ?? Identifier.ascending("part"),
  767. messageID: info.id,
  768. sessionID: input.sessionID,
  769. })
  770. }
  771. })
  772. .catch((error) => {
  773. log.error("failed to read file", { error })
  774. const message = error instanceof Error ? error.message : error.toString()
  775. Bus.publish(Session.Event.Error, {
  776. sessionID: input.sessionID,
  777. error: new NamedError.Unknown({
  778. message,
  779. }).toObject(),
  780. })
  781. pieces.push({
  782. id: Identifier.ascending("part"),
  783. messageID: info.id,
  784. sessionID: input.sessionID,
  785. type: "text",
  786. synthetic: true,
  787. text: `Read tool failed to read ${filepath} with the following error: ${message}`,
  788. })
  789. })
  790. return pieces
  791. }
  792. if (part.mime === "application/x-directory") {
  793. const args = { path: filepath }
  794. const result = await ListTool.init().then((t) =>
  795. t.execute(args, {
  796. sessionID: input.sessionID,
  797. abort: new AbortController().signal,
  798. agent: input.agent!,
  799. messageID: info.id,
  800. extra: { bypassCwdCheck: true },
  801. metadata: async () => {},
  802. }),
  803. )
  804. return [
  805. {
  806. id: Identifier.ascending("part"),
  807. messageID: info.id,
  808. sessionID: input.sessionID,
  809. type: "text",
  810. synthetic: true,
  811. text: `Called the list tool with the following input: ${JSON.stringify(args)}`,
  812. },
  813. {
  814. id: Identifier.ascending("part"),
  815. messageID: info.id,
  816. sessionID: input.sessionID,
  817. type: "text",
  818. synthetic: true,
  819. text: result.output,
  820. },
  821. {
  822. ...part,
  823. id: part.id ?? Identifier.ascending("part"),
  824. messageID: info.id,
  825. sessionID: input.sessionID,
  826. },
  827. ]
  828. }
  829. const file = Bun.file(filepath)
  830. FileTime.read(input.sessionID, filepath)
  831. return [
  832. {
  833. id: Identifier.ascending("part"),
  834. messageID: info.id,
  835. sessionID: input.sessionID,
  836. type: "text",
  837. text: `Called the Read tool with the following input: {\"filePath\":\"${filepath}\"}`,
  838. synthetic: true,
  839. },
  840. {
  841. id: part.id ?? Identifier.ascending("part"),
  842. messageID: info.id,
  843. sessionID: input.sessionID,
  844. type: "file",
  845. url: `data:${part.mime};base64,` + Buffer.from(await file.bytes()).toString("base64"),
  846. mime: part.mime,
  847. filename: part.filename!,
  848. source: part.source,
  849. },
  850. ]
  851. }
  852. }
  853. if (part.type === "agent") {
  854. return [
  855. {
  856. id: Identifier.ascending("part"),
  857. ...part,
  858. messageID: info.id,
  859. sessionID: input.sessionID,
  860. },
  861. {
  862. id: Identifier.ascending("part"),
  863. messageID: info.id,
  864. sessionID: input.sessionID,
  865. type: "text",
  866. synthetic: true,
  867. text:
  868. "Use the above message and context to generate a prompt and call the task tool with subagent: " +
  869. part.name,
  870. },
  871. ]
  872. }
  873. return [
  874. {
  875. id: Identifier.ascending("part"),
  876. ...part,
  877. messageID: info.id,
  878. sessionID: input.sessionID,
  879. },
  880. ]
  881. }),
  882. ).then((x) => x.flat())
  883. await Plugin.trigger(
  884. "chat.message",
  885. {
  886. sessionID: input.sessionID,
  887. agent: input.agent,
  888. model: input.model,
  889. messageID: input.messageID,
  890. },
  891. {
  892. message: info,
  893. parts,
  894. },
  895. )
  896. await Session.updateMessage(info)
  897. for (const part of parts) {
  898. await Session.updatePart(part)
  899. }
  900. return {
  901. info,
  902. parts,
  903. }
  904. }
  905. function insertReminders(input: { messages: MessageV2.WithParts[]; agent: Agent.Info }) {
  906. const userMessage = input.messages.findLast((msg) => msg.info.role === "user")
  907. if (!userMessage) return input.messages
  908. if (input.agent.name === "plan") {
  909. userMessage.parts.push({
  910. id: Identifier.ascending("part"),
  911. messageID: userMessage.info.id,
  912. sessionID: userMessage.info.sessionID,
  913. type: "text",
  914. // TODO (for mr dax): update to use the anthropic full fledged one (see plan-reminder-anthropic.txt)
  915. text: PROMPT_PLAN,
  916. synthetic: true,
  917. })
  918. }
  919. const wasPlan = input.messages.some((msg) => msg.info.role === "assistant" && msg.info.agent === "plan")
  920. if (wasPlan && input.agent.name === "build") {
  921. userMessage.parts.push({
  922. id: Identifier.ascending("part"),
  923. messageID: userMessage.info.id,
  924. sessionID: userMessage.info.sessionID,
  925. type: "text",
  926. text: BUILD_SWITCH,
  927. synthetic: true,
  928. })
  929. }
  930. return input.messages
  931. }
  932. export const ShellInput = z.object({
  933. sessionID: Identifier.schema("session"),
  934. agent: z.string(),
  935. model: z
  936. .object({
  937. providerID: z.string(),
  938. modelID: z.string(),
  939. })
  940. .optional(),
  941. command: z.string(),
  942. })
  943. export type ShellInput = z.infer<typeof ShellInput>
  944. export async function shell(input: ShellInput) {
  945. const abort = start(input.sessionID)
  946. if (!abort) {
  947. throw new Session.BusyError(input.sessionID)
  948. }
  949. using _ = defer(() => cancel(input.sessionID))
  950. const session = await Session.get(input.sessionID)
  951. if (session.revert) {
  952. SessionRevert.cleanup(session)
  953. }
  954. const agent = await Agent.get(input.agent)
  955. const model = input.model ?? agent.model ?? (await lastModel(input.sessionID))
  956. const userMsg: MessageV2.User = {
  957. id: Identifier.ascending("message"),
  958. sessionID: input.sessionID,
  959. time: {
  960. created: Date.now(),
  961. },
  962. role: "user",
  963. agent: input.agent,
  964. model: {
  965. providerID: model.providerID,
  966. modelID: model.modelID,
  967. },
  968. }
  969. await Session.updateMessage(userMsg)
  970. const userPart: MessageV2.Part = {
  971. type: "text",
  972. id: Identifier.ascending("part"),
  973. messageID: userMsg.id,
  974. sessionID: input.sessionID,
  975. text: "The following tool was executed by the user",
  976. synthetic: true,
  977. }
  978. await Session.updatePart(userPart)
  979. const msg: MessageV2.Assistant = {
  980. id: Identifier.ascending("message"),
  981. sessionID: input.sessionID,
  982. parentID: userMsg.id,
  983. mode: input.agent,
  984. agent: input.agent,
  985. cost: 0,
  986. path: {
  987. cwd: Instance.directory,
  988. root: Instance.worktree,
  989. },
  990. time: {
  991. created: Date.now(),
  992. },
  993. role: "assistant",
  994. tokens: {
  995. input: 0,
  996. output: 0,
  997. reasoning: 0,
  998. cache: { read: 0, write: 0 },
  999. },
  1000. modelID: model.modelID,
  1001. providerID: model.providerID,
  1002. }
  1003. await Session.updateMessage(msg)
  1004. const part: MessageV2.Part = {
  1005. type: "tool",
  1006. id: Identifier.ascending("part"),
  1007. messageID: msg.id,
  1008. sessionID: input.sessionID,
  1009. tool: "bash",
  1010. callID: ulid(),
  1011. state: {
  1012. status: "running",
  1013. time: {
  1014. start: Date.now(),
  1015. },
  1016. input: {
  1017. command: input.command,
  1018. },
  1019. },
  1020. }
  1021. await Session.updatePart(part)
  1022. const shell = Shell.preferred()
  1023. const shellName = (
  1024. process.platform === "win32" ? path.win32.basename(shell, ".exe") : path.basename(shell)
  1025. ).toLowerCase()
  1026. const invocations: Record<string, { args: string[] }> = {
  1027. nu: {
  1028. args: ["-c", input.command],
  1029. },
  1030. fish: {
  1031. args: ["-c", input.command],
  1032. },
  1033. zsh: {
  1034. args: [
  1035. "-c",
  1036. "-l",
  1037. `
  1038. [[ -f ~/.zshenv ]] && source ~/.zshenv >/dev/null 2>&1 || true
  1039. [[ -f "\${ZDOTDIR:-$HOME}/.zshrc" ]] && source "\${ZDOTDIR:-$HOME}/.zshrc" >/dev/null 2>&1 || true
  1040. ${input.command}
  1041. `,
  1042. ],
  1043. },
  1044. bash: {
  1045. args: [
  1046. "-c",
  1047. "-l",
  1048. `
  1049. [[ -f ~/.bashrc ]] && source ~/.bashrc >/dev/null 2>&1 || true
  1050. ${input.command}
  1051. `,
  1052. ],
  1053. },
  1054. // Windows cmd
  1055. cmd: {
  1056. args: ["/c", input.command],
  1057. },
  1058. // Windows PowerShell
  1059. powershell: {
  1060. args: ["-NoProfile", "-Command", input.command],
  1061. },
  1062. pwsh: {
  1063. args: ["-NoProfile", "-Command", input.command],
  1064. },
  1065. // Fallback: any shell that doesn't match those above
  1066. // - No -l, for max compatibility
  1067. "": {
  1068. args: ["-c", `${input.command}`],
  1069. },
  1070. }
  1071. const matchingInvocation = invocations[shellName] ?? invocations[""]
  1072. const args = matchingInvocation?.args
  1073. const proc = spawn(shell, args, {
  1074. cwd: Instance.directory,
  1075. detached: process.platform !== "win32",
  1076. stdio: ["ignore", "pipe", "pipe"],
  1077. env: {
  1078. ...process.env,
  1079. TERM: "dumb",
  1080. },
  1081. })
  1082. let output = ""
  1083. proc.stdout?.on("data", (chunk) => {
  1084. output += chunk.toString()
  1085. if (part.state.status === "running") {
  1086. part.state.metadata = {
  1087. output: output,
  1088. description: "",
  1089. }
  1090. Session.updatePart(part)
  1091. }
  1092. })
  1093. proc.stderr?.on("data", (chunk) => {
  1094. output += chunk.toString()
  1095. if (part.state.status === "running") {
  1096. part.state.metadata = {
  1097. output: output,
  1098. description: "",
  1099. }
  1100. Session.updatePart(part)
  1101. }
  1102. })
  1103. let aborted = false
  1104. let exited = false
  1105. const kill = () => Shell.killTree(proc, { exited: () => exited })
  1106. if (abort.aborted) {
  1107. aborted = true
  1108. await kill()
  1109. }
  1110. const abortHandler = () => {
  1111. aborted = true
  1112. void kill()
  1113. }
  1114. abort.addEventListener("abort", abortHandler, { once: true })
  1115. await new Promise<void>((resolve) => {
  1116. proc.on("close", () => {
  1117. exited = true
  1118. abort.removeEventListener("abort", abortHandler)
  1119. resolve()
  1120. })
  1121. })
  1122. if (aborted) {
  1123. output += "\n\n" + ["<metadata>", "User aborted the command", "</metadata>"].join("\n")
  1124. }
  1125. msg.time.completed = Date.now()
  1126. await Session.updateMessage(msg)
  1127. if (part.state.status === "running") {
  1128. part.state = {
  1129. status: "completed",
  1130. time: {
  1131. ...part.state.time,
  1132. end: Date.now(),
  1133. },
  1134. input: part.state.input,
  1135. title: "",
  1136. metadata: {
  1137. output,
  1138. description: "",
  1139. },
  1140. output,
  1141. }
  1142. await Session.updatePart(part)
  1143. }
  1144. return { info: msg, parts: [part] }
  1145. }
  1146. export const CommandInput = z.object({
  1147. messageID: Identifier.schema("message").optional(),
  1148. sessionID: Identifier.schema("session"),
  1149. agent: z.string().optional(),
  1150. model: z.string().optional(),
  1151. arguments: z.string(),
  1152. command: z.string(),
  1153. })
  1154. export type CommandInput = z.infer<typeof CommandInput>
  1155. const bashRegex = /!`([^`]+)`/g
  1156. const argsRegex = /(?:[^\s"']+|"[^"]*"|'[^']*')+/g
  1157. const placeholderRegex = /\$(\d+)/g
  1158. const quoteTrimRegex = /^["']|["']$/g
  1159. /**
  1160. * Regular expression to match @ file references in text
  1161. * Matches @ followed by file paths, excluding commas, periods at end of sentences, and backticks
  1162. * Does not match when preceded by word characters or backticks (to avoid email addresses and quoted references)
  1163. */
  1164. export async function command(input: CommandInput) {
  1165. log.info("command", input)
  1166. const command = await Command.get(input.command)
  1167. const agentName = command.agent ?? input.agent ?? "build"
  1168. const raw = input.arguments.match(argsRegex) ?? []
  1169. const args = raw.map((arg) => arg.replace(quoteTrimRegex, ""))
  1170. const placeholders = command.template.match(placeholderRegex) ?? []
  1171. let last = 0
  1172. for (const item of placeholders) {
  1173. const value = Number(item.slice(1))
  1174. if (value > last) last = value
  1175. }
  1176. // Let the final placeholder swallow any extra arguments so prompts read naturally
  1177. const withArgs = command.template.replaceAll(placeholderRegex, (_, index) => {
  1178. const position = Number(index)
  1179. const argIndex = position - 1
  1180. if (argIndex >= args.length) return ""
  1181. if (position === last) return args.slice(argIndex).join(" ")
  1182. return args[argIndex]
  1183. })
  1184. let template = withArgs.replaceAll("$ARGUMENTS", input.arguments)
  1185. const shell = ConfigMarkdown.shell(template)
  1186. if (shell.length > 0) {
  1187. const results = await Promise.all(
  1188. shell.map(async ([, cmd]) => {
  1189. try {
  1190. return await $`${{ raw: cmd }}`.nothrow().text()
  1191. } catch (error) {
  1192. return `Error executing command: ${error instanceof Error ? error.message : String(error)}`
  1193. }
  1194. }),
  1195. )
  1196. let index = 0
  1197. template = template.replace(bashRegex, () => results[index++])
  1198. }
  1199. template = template.trim()
  1200. const model = await (async () => {
  1201. if (command.model) {
  1202. return Provider.parseModel(command.model)
  1203. }
  1204. if (command.agent) {
  1205. const cmdAgent = await Agent.get(command.agent)
  1206. if (cmdAgent.model) {
  1207. return cmdAgent.model
  1208. }
  1209. }
  1210. if (input.model) return Provider.parseModel(input.model)
  1211. return await lastModel(input.sessionID)
  1212. })()
  1213. const agent = await Agent.get(agentName)
  1214. const parts =
  1215. (agent.mode === "subagent" && command.subtask !== false) || command.subtask === true
  1216. ? [
  1217. {
  1218. type: "subtask" as const,
  1219. agent: agent.name,
  1220. description: command.description ?? "",
  1221. // TODO: how can we make task tool accept a more complex input?
  1222. prompt: await resolvePromptParts(template).then((x) => x.find((y) => y.type === "text")?.text ?? ""),
  1223. },
  1224. ]
  1225. : await resolvePromptParts(template)
  1226. const result = (await prompt({
  1227. sessionID: input.sessionID,
  1228. messageID: input.messageID,
  1229. model,
  1230. agent: agentName,
  1231. parts,
  1232. })) as MessageV2.WithParts
  1233. Bus.publish(Command.Event.Executed, {
  1234. name: input.command,
  1235. sessionID: input.sessionID,
  1236. arguments: input.arguments,
  1237. messageID: result.info.id,
  1238. })
  1239. return result
  1240. }
  1241. async function ensureTitle(input: {
  1242. session: Session.Info
  1243. message: MessageV2.WithParts
  1244. history: MessageV2.WithParts[]
  1245. providerID: string
  1246. modelID: string
  1247. }) {
  1248. if (input.session.parentID) return
  1249. if (!Session.isDefaultTitle(input.session.title)) return
  1250. const isFirst =
  1251. input.history.filter((m) => m.info.role === "user" && !m.parts.every((p) => "synthetic" in p && p.synthetic))
  1252. .length === 1
  1253. if (!isFirst) return
  1254. const agent = await Agent.get("title")
  1255. if (!agent) return
  1256. const result = await LLM.stream({
  1257. agent,
  1258. user: input.message.info as MessageV2.User,
  1259. system: [],
  1260. small: true,
  1261. tools: {},
  1262. model: await iife(async () => {
  1263. if (agent.model) return await Provider.getModel(agent.model.providerID, agent.model.modelID)
  1264. return (
  1265. (await Provider.getSmallModel(input.providerID)) ?? (await Provider.getModel(input.providerID, input.modelID))
  1266. )
  1267. }),
  1268. abort: new AbortController().signal,
  1269. sessionID: input.session.id,
  1270. retries: 2,
  1271. messages: [
  1272. {
  1273. role: "user",
  1274. content: "Generate a title for this conversation:\n",
  1275. },
  1276. ...MessageV2.toModelMessage([
  1277. {
  1278. info: {
  1279. id: Identifier.ascending("message"),
  1280. role: "user",
  1281. sessionID: input.session.id,
  1282. time: {
  1283. created: Date.now(),
  1284. },
  1285. agent: input.message.info.role === "user" ? input.message.info.agent : "build",
  1286. model: {
  1287. providerID: input.providerID,
  1288. modelID: input.modelID,
  1289. },
  1290. },
  1291. parts: input.message.parts,
  1292. },
  1293. ]),
  1294. ],
  1295. })
  1296. const text = await result.text.catch((err) => log.error("failed to generate title", { error: err }))
  1297. if (text)
  1298. return Session.update(input.session.id, (draft) => {
  1299. const cleaned = text
  1300. .replace(/<think>[\s\S]*?<\/think>\s*/g, "")
  1301. .split("\n")
  1302. .map((line) => line.trim())
  1303. .find((line) => line.length > 0)
  1304. if (!cleaned) return
  1305. const title = cleaned.length > 100 ? cleaned.substring(0, 97) + "..." : cleaned
  1306. draft.title = title
  1307. })
  1308. }
  1309. }