prompt.ts 48 KB

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