prompt.ts 46 KB

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