index.ts 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286
  1. import path from "path"
  2. import { Decimal } from "decimal.js"
  3. import { z, ZodSchema } from "zod"
  4. import {
  5. generateText,
  6. LoadAPIKeyError,
  7. streamText,
  8. tool,
  9. wrapLanguageModel,
  10. type Tool as AITool,
  11. type LanguageModelUsage,
  12. type ProviderMetadata,
  13. type ModelMessage,
  14. stepCountIs,
  15. type StreamTextResult,
  16. } from "ai"
  17. import PROMPT_INITIALIZE from "../session/prompt/initialize.txt"
  18. import PROMPT_PLAN from "../session/prompt/plan.txt"
  19. import { App } from "../app/app"
  20. import { Bus } from "../bus"
  21. import { Config } from "../config/config"
  22. import { Flag } from "../flag/flag"
  23. import { Identifier } from "../id/id"
  24. import { Installation } from "../installation"
  25. import { MCP } from "../mcp"
  26. import { Provider } from "../provider/provider"
  27. import { ProviderTransform } from "../provider/transform"
  28. import type { ModelsDev } from "../provider/models"
  29. import { Share } from "../share/share"
  30. import { Snapshot } from "../snapshot"
  31. import { Storage } from "../storage/storage"
  32. import { Log } from "../util/log"
  33. import { NamedError } from "../util/error"
  34. import { SystemPrompt } from "./system"
  35. import { FileTime } from "../file/time"
  36. import { MessageV2 } from "./message-v2"
  37. import { Mode } from "./mode"
  38. import { LSP } from "../lsp"
  39. import { ReadTool } from "../tool/read"
  40. import { splitWhen } from "remeda"
  41. export namespace Session {
  42. const log = Log.create({ service: "session" })
  43. const OUTPUT_TOKEN_MAX = 32_000
  44. export const Info = z
  45. .object({
  46. id: Identifier.schema("session"),
  47. parentID: Identifier.schema("session").optional(),
  48. share: z
  49. .object({
  50. url: z.string(),
  51. })
  52. .optional(),
  53. title: z.string(),
  54. version: z.string(),
  55. time: z.object({
  56. created: z.number(),
  57. updated: z.number(),
  58. }),
  59. revert: z
  60. .object({
  61. messageID: z.string(),
  62. partID: z.string().optional(),
  63. snapshot: z.string().optional(),
  64. })
  65. .optional(),
  66. })
  67. .openapi({
  68. ref: "Session",
  69. })
  70. export type Info = z.output<typeof Info>
  71. export const ShareInfo = z
  72. .object({
  73. secret: z.string(),
  74. url: z.string(),
  75. })
  76. .openapi({
  77. ref: "SessionShare",
  78. })
  79. export type ShareInfo = z.output<typeof ShareInfo>
  80. export const Event = {
  81. Updated: Bus.event(
  82. "session.updated",
  83. z.object({
  84. info: Info,
  85. }),
  86. ),
  87. Deleted: Bus.event(
  88. "session.deleted",
  89. z.object({
  90. info: Info,
  91. }),
  92. ),
  93. Idle: Bus.event(
  94. "session.idle",
  95. z.object({
  96. sessionID: z.string(),
  97. }),
  98. ),
  99. Error: Bus.event(
  100. "session.error",
  101. z.object({
  102. sessionID: z.string().optional(),
  103. error: MessageV2.Assistant.shape.error,
  104. }),
  105. ),
  106. }
  107. const state = App.state(
  108. "session",
  109. () => {
  110. const sessions = new Map<string, Info>()
  111. const messages = new Map<string, MessageV2.Info[]>()
  112. const pending = new Map<string, AbortController>()
  113. const queued = new Map<
  114. string,
  115. {
  116. input: ChatInput
  117. message: MessageV2.User
  118. parts: MessageV2.Part[]
  119. processed: boolean
  120. callback: (input: { info: MessageV2.Assistant; parts: MessageV2.Part[] }) => void
  121. }[]
  122. >()
  123. return {
  124. sessions,
  125. messages,
  126. pending,
  127. queued,
  128. }
  129. },
  130. async (state) => {
  131. for (const [_, controller] of state.pending) {
  132. controller.abort()
  133. }
  134. },
  135. )
  136. export async function create(parentID?: string) {
  137. const result: Info = {
  138. id: Identifier.descending("session"),
  139. version: Installation.VERSION,
  140. parentID,
  141. title: (parentID ? "Child session - " : "New Session - ") + new Date().toISOString(),
  142. time: {
  143. created: Date.now(),
  144. updated: Date.now(),
  145. },
  146. }
  147. log.info("created", result)
  148. state().sessions.set(result.id, result)
  149. await Storage.writeJSON("session/info/" + result.id, result)
  150. const cfg = await Config.get()
  151. if (!result.parentID && (Flag.OPENCODE_AUTO_SHARE || cfg.share === "auto"))
  152. share(result.id)
  153. .then((share) => {
  154. update(result.id, (draft) => {
  155. draft.share = share
  156. })
  157. })
  158. .catch(() => {
  159. // Silently ignore sharing errors during session creation
  160. })
  161. Bus.publish(Event.Updated, {
  162. info: result,
  163. })
  164. return result
  165. }
  166. export async function get(id: string) {
  167. const result = state().sessions.get(id)
  168. if (result) {
  169. return result
  170. }
  171. const read = await Storage.readJSON<Info>("session/info/" + id)
  172. state().sessions.set(id, read)
  173. return read as Info
  174. }
  175. export async function getShare(id: string) {
  176. return Storage.readJSON<ShareInfo>("session/share/" + id)
  177. }
  178. export async function share(id: string) {
  179. const cfg = await Config.get()
  180. if (cfg.share === "disabled") {
  181. throw new Error("Sharing is disabled in configuration")
  182. }
  183. const session = await get(id)
  184. if (session.share) return session.share
  185. const share = await Share.create(id)
  186. await update(id, (draft) => {
  187. draft.share = {
  188. url: share.url,
  189. }
  190. })
  191. await Storage.writeJSON<ShareInfo>("session/share/" + id, share)
  192. await Share.sync("session/info/" + id, session)
  193. for (const msg of await messages(id)) {
  194. await Share.sync("session/message/" + id + "/" + msg.info.id, msg.info)
  195. for (const part of msg.parts) {
  196. await Share.sync("session/part/" + id + "/" + msg.info.id + "/" + part.id, part)
  197. }
  198. }
  199. return share
  200. }
  201. export async function unshare(id: string) {
  202. const share = await getShare(id)
  203. if (!share) return
  204. await Storage.remove("session/share/" + id)
  205. await update(id, (draft) => {
  206. draft.share = undefined
  207. })
  208. await Share.remove(id, share.secret)
  209. }
  210. export async function update(id: string, editor: (session: Info) => void) {
  211. const { sessions } = state()
  212. const session = await get(id)
  213. if (!session) return
  214. editor(session)
  215. session.time.updated = Date.now()
  216. sessions.set(id, session)
  217. await Storage.writeJSON("session/info/" + id, session)
  218. Bus.publish(Event.Updated, {
  219. info: session,
  220. })
  221. return session
  222. }
  223. export async function messages(sessionID: string) {
  224. const result = [] as {
  225. info: MessageV2.Info
  226. parts: MessageV2.Part[]
  227. }[]
  228. for (const p of await Storage.list("session/message/" + sessionID)) {
  229. const read = await Storage.readJSON<MessageV2.Info>(p)
  230. result.push({
  231. info: read,
  232. parts: await getParts(sessionID, read.id),
  233. })
  234. }
  235. result.sort((a, b) => (a.info.id > b.info.id ? 1 : -1))
  236. return result
  237. }
  238. export async function getMessage(sessionID: string, messageID: string) {
  239. return Storage.readJSON<MessageV2.Info>("session/message/" + sessionID + "/" + messageID)
  240. }
  241. export async function getParts(sessionID: string, messageID: string) {
  242. const result = [] as MessageV2.Part[]
  243. for (const item of await Storage.list("session/part/" + sessionID + "/" + messageID)) {
  244. const read = await Storage.readJSON<MessageV2.Part>(item)
  245. result.push(read)
  246. }
  247. result.sort((a, b) => (a.id > b.id ? 1 : -1))
  248. return result
  249. }
  250. export async function* list() {
  251. for (const item of await Storage.list("session/info")) {
  252. const sessionID = path.basename(item, ".json")
  253. yield get(sessionID)
  254. }
  255. }
  256. export async function children(parentID: string) {
  257. const result = [] as Session.Info[]
  258. for (const item of await Storage.list("session/info")) {
  259. const sessionID = path.basename(item, ".json")
  260. const session = await get(sessionID)
  261. if (session.parentID !== parentID) continue
  262. result.push(session)
  263. }
  264. return result
  265. }
  266. export function abort(sessionID: string) {
  267. const controller = state().pending.get(sessionID)
  268. if (!controller) return false
  269. controller.abort()
  270. state().pending.delete(sessionID)
  271. return true
  272. }
  273. export async function remove(sessionID: string, emitEvent = true) {
  274. try {
  275. abort(sessionID)
  276. const session = await get(sessionID)
  277. for (const child of await children(sessionID)) {
  278. await remove(child.id, false)
  279. }
  280. await unshare(sessionID).catch(() => {})
  281. await Storage.remove(`session/info/${sessionID}`).catch(() => {})
  282. await Storage.removeDir(`session/message/${sessionID}/`).catch(() => {})
  283. state().sessions.delete(sessionID)
  284. state().messages.delete(sessionID)
  285. if (emitEvent) {
  286. Bus.publish(Event.Deleted, {
  287. info: session,
  288. })
  289. }
  290. } catch (e) {
  291. log.error(e)
  292. }
  293. }
  294. async function updateMessage(msg: MessageV2.Info) {
  295. await Storage.writeJSON("session/message/" + msg.sessionID + "/" + msg.id, msg)
  296. Bus.publish(MessageV2.Event.Updated, {
  297. info: msg,
  298. })
  299. }
  300. async function updatePart(part: MessageV2.Part) {
  301. await Storage.writeJSON(["session", "part", part.sessionID, part.messageID, part.id].join("/"), part)
  302. Bus.publish(MessageV2.Event.PartUpdated, {
  303. part,
  304. })
  305. return part
  306. }
  307. export const ChatInput = z.object({
  308. sessionID: Identifier.schema("session"),
  309. messageID: Identifier.schema("message").optional(),
  310. providerID: z.string(),
  311. modelID: z.string(),
  312. mode: z.string().optional(),
  313. system: z.string().optional(),
  314. tools: z.record(z.boolean()).optional(),
  315. parts: z.array(
  316. z.discriminatedUnion("type", [
  317. MessageV2.TextPart.omit({
  318. messageID: true,
  319. sessionID: true,
  320. })
  321. .partial({
  322. id: true,
  323. })
  324. .openapi({
  325. ref: "TextPartInput",
  326. }),
  327. MessageV2.FilePart.omit({
  328. messageID: true,
  329. sessionID: true,
  330. })
  331. .partial({
  332. id: true,
  333. })
  334. .openapi({
  335. ref: "FilePartInput",
  336. }),
  337. ]),
  338. ),
  339. })
  340. export type ChatInput = z.infer<typeof ChatInput>
  341. export async function chat(
  342. input: z.infer<typeof ChatInput>,
  343. ): Promise<{ info: MessageV2.Assistant; parts: MessageV2.Part[] }> {
  344. const l = log.clone().tag("session", input.sessionID)
  345. l.info("chatting")
  346. const inputMode = input.mode ?? "build"
  347. const userMsg: MessageV2.Info = {
  348. id: input.messageID ?? Identifier.ascending("message"),
  349. role: "user",
  350. sessionID: input.sessionID,
  351. time: {
  352. created: Date.now(),
  353. },
  354. }
  355. const app = App.info()
  356. const userParts = await Promise.all(
  357. input.parts.map(async (part): Promise<MessageV2.Part[]> => {
  358. if (part.type === "file") {
  359. const url = new URL(part.url)
  360. switch (url.protocol) {
  361. case "file:":
  362. // have to normalize, symbol search returns absolute paths
  363. // Decode the pathname since URL constructor doesn't automatically decode it
  364. const pathname = decodeURIComponent(url.pathname)
  365. const relativePath = pathname.replace(app.path.cwd, ".")
  366. const filePath = path.join(app.path.cwd, relativePath)
  367. if (part.mime === "text/plain") {
  368. let offset: number | undefined = undefined
  369. let limit: number | undefined = undefined
  370. const range = {
  371. start: url.searchParams.get("start"),
  372. end: url.searchParams.get("end"),
  373. }
  374. if (range.start != null) {
  375. const filePath = part.url.split("?")[0]
  376. let start = parseInt(range.start)
  377. let end = range.end ? parseInt(range.end) : undefined
  378. // some LSP servers (eg, gopls) don't give full range in
  379. // workspace/symbol searches, so we'll try to find the
  380. // symbol in the document to get the full range
  381. if (start === end) {
  382. const symbols = await LSP.documentSymbol(filePath)
  383. for (const symbol of symbols) {
  384. let range: LSP.Range | undefined
  385. if ("range" in symbol) {
  386. range = symbol.range
  387. } else if ("location" in symbol) {
  388. range = symbol.location.range
  389. }
  390. if (range?.start?.line && range?.start?.line === start) {
  391. start = range.start.line
  392. end = range?.end?.line ?? start
  393. break
  394. }
  395. }
  396. offset = Math.max(start - 2, 0)
  397. if (end) {
  398. limit = end - offset + 2
  399. }
  400. }
  401. }
  402. const args = { filePath, offset, limit }
  403. const result = await ReadTool().then((t) =>
  404. t.execute(args, {
  405. sessionID: input.sessionID,
  406. abort: new AbortController().signal,
  407. messageID: userMsg.id,
  408. metadata: async () => {},
  409. }),
  410. )
  411. return [
  412. {
  413. id: Identifier.ascending("part"),
  414. messageID: userMsg.id,
  415. sessionID: input.sessionID,
  416. type: "text",
  417. synthetic: true,
  418. text: `Called the Read tool with the following input: ${JSON.stringify(args)}`,
  419. },
  420. {
  421. id: Identifier.ascending("part"),
  422. messageID: userMsg.id,
  423. sessionID: input.sessionID,
  424. type: "text",
  425. synthetic: true,
  426. text: result.output,
  427. },
  428. {
  429. ...part,
  430. id: part.id ?? Identifier.ascending("part"),
  431. messageID: userMsg.id,
  432. sessionID: input.sessionID,
  433. },
  434. ]
  435. }
  436. let file = Bun.file(filePath)
  437. FileTime.read(input.sessionID, filePath)
  438. return [
  439. {
  440. id: Identifier.ascending("part"),
  441. messageID: userMsg.id,
  442. sessionID: input.sessionID,
  443. type: "text",
  444. text: `Called the Read tool with the following input: {\"filePath\":\"${pathname}\"}`,
  445. synthetic: true,
  446. },
  447. {
  448. id: part.id ?? Identifier.ascending("part"),
  449. messageID: userMsg.id,
  450. sessionID: input.sessionID,
  451. type: "file",
  452. url: `data:${part.mime};base64,` + Buffer.from(await file.bytes()).toString("base64"),
  453. mime: part.mime,
  454. filename: part.filename!,
  455. source: part.source,
  456. },
  457. ]
  458. }
  459. }
  460. return [
  461. {
  462. id: Identifier.ascending("part"),
  463. ...part,
  464. messageID: userMsg.id,
  465. sessionID: input.sessionID,
  466. },
  467. ]
  468. }),
  469. ).then((x) => x.flat())
  470. if (inputMode === "plan")
  471. userParts.push({
  472. id: Identifier.ascending("part"),
  473. messageID: userMsg.id,
  474. sessionID: input.sessionID,
  475. type: "text",
  476. text: PROMPT_PLAN,
  477. synthetic: true,
  478. })
  479. await updateMessage(userMsg)
  480. for (const part of userParts) {
  481. await updatePart(part)
  482. }
  483. // mark session as updated since a message has been added to it
  484. await update(input.sessionID, (_draft) => {})
  485. if (isLocked(input.sessionID)) {
  486. return new Promise((resolve) => {
  487. const queue = state().queued.get(input.sessionID) ?? []
  488. queue.push({
  489. input: input,
  490. message: userMsg,
  491. parts: userParts,
  492. processed: false,
  493. callback: resolve,
  494. })
  495. state().queued.set(input.sessionID, queue)
  496. })
  497. }
  498. const model = await Provider.getModel(input.providerID, input.modelID)
  499. let msgs = await messages(input.sessionID)
  500. const session = await get(input.sessionID)
  501. if (session.revert) {
  502. const messageID = session.revert.messageID
  503. const [preserve, remove] = splitWhen(msgs, (x) => x.info.id === messageID)
  504. msgs = preserve
  505. for (const msg of remove) {
  506. await Storage.remove(`session/message/${input.sessionID}/${msg.info.id}`)
  507. await Bus.publish(MessageV2.Event.Removed, { sessionID: input.sessionID, messageID: msg.info.id })
  508. }
  509. const last = preserve.at(-1)
  510. if (session.revert.partID && last) {
  511. const partID = session.revert.partID
  512. const [preserveParts, removeParts] = splitWhen(last.parts, (x) => x.id === partID)
  513. last.parts = preserveParts
  514. for (const part of removeParts) {
  515. await Storage.remove(`session/part/${input.sessionID}/${last.info.id}/${part.id}`)
  516. await Bus.publish(MessageV2.Event.PartRemoved, {
  517. messageID: last.info.id,
  518. partID: part.id,
  519. })
  520. }
  521. }
  522. }
  523. const previous = msgs.filter((x) => x.info.role === "assistant").at(-1)?.info as MessageV2.Assistant
  524. const outputLimit = Math.min(model.info.limit.output, OUTPUT_TOKEN_MAX) || OUTPUT_TOKEN_MAX
  525. // auto summarize if too long
  526. if (previous && previous.tokens) {
  527. const tokens =
  528. previous.tokens.input + previous.tokens.cache.read + previous.tokens.cache.write + previous.tokens.output
  529. if (model.info.limit.context && tokens > Math.max((model.info.limit.context - outputLimit) * 0.9, 0)) {
  530. await summarize({
  531. sessionID: input.sessionID,
  532. providerID: input.providerID,
  533. modelID: input.modelID,
  534. })
  535. return chat(input)
  536. }
  537. }
  538. using abort = lock(input.sessionID)
  539. const lastSummary = msgs.findLast((msg) => msg.info.role === "assistant" && msg.info.summary === true)
  540. if (lastSummary) msgs = msgs.filter((msg) => msg.info.id >= lastSummary.info.id)
  541. if (msgs.length === 1 && !session.parentID) {
  542. const small = (await Provider.getSmallModel(input.providerID)) ?? model
  543. generateText({
  544. maxOutputTokens: small.info.reasoning ? 1024 : 20,
  545. providerOptions: {
  546. [input.providerID]: small.info.options,
  547. },
  548. messages: [
  549. ...SystemPrompt.title(input.providerID).map(
  550. (x): ModelMessage => ({
  551. role: "system",
  552. content: x,
  553. }),
  554. ),
  555. ...MessageV2.toModelMessage([
  556. {
  557. info: {
  558. id: Identifier.ascending("message"),
  559. role: "user",
  560. sessionID: input.sessionID,
  561. time: {
  562. created: Date.now(),
  563. },
  564. },
  565. parts: userParts,
  566. },
  567. ]),
  568. ],
  569. model: small.language,
  570. })
  571. .then((result) => {
  572. if (result.text)
  573. return Session.update(input.sessionID, (draft) => {
  574. draft.title = result.text
  575. })
  576. })
  577. .catch(() => {})
  578. }
  579. const mode = await Mode.get(inputMode)
  580. let system = SystemPrompt.header(input.providerID)
  581. system.push(
  582. ...(() => {
  583. if (input.system) return [input.system]
  584. if (mode.prompt) return [mode.prompt]
  585. return SystemPrompt.provider(input.modelID)
  586. })(),
  587. )
  588. system.push(...(await SystemPrompt.environment()))
  589. system.push(...(await SystemPrompt.custom()))
  590. // max 2 system prompt messages for caching purposes
  591. const [first, ...rest] = system
  592. system = [first, rest.join("\n")]
  593. const assistantMsg: MessageV2.Info = {
  594. id: Identifier.ascending("message"),
  595. role: "assistant",
  596. system,
  597. mode: inputMode,
  598. path: {
  599. cwd: app.path.cwd,
  600. root: app.path.root,
  601. },
  602. cost: 0,
  603. tokens: {
  604. input: 0,
  605. output: 0,
  606. reasoning: 0,
  607. cache: { read: 0, write: 0 },
  608. },
  609. modelID: input.modelID,
  610. providerID: input.providerID,
  611. time: {
  612. created: Date.now(),
  613. },
  614. sessionID: input.sessionID,
  615. }
  616. await updateMessage(assistantMsg)
  617. const tools: Record<string, AITool> = {}
  618. const processor = createProcessor(assistantMsg, model.info)
  619. for (const item of await Provider.tools(input.providerID)) {
  620. if (mode.tools[item.id] === false) continue
  621. if (input.tools?.[item.id] === false) continue
  622. if (session.parentID && item.id === "task") continue
  623. tools[item.id] = tool({
  624. id: item.id as any,
  625. description: item.description,
  626. inputSchema: item.parameters as ZodSchema,
  627. async execute(args, options) {
  628. await processor.track(options.toolCallId)
  629. const result = await item.execute(args, {
  630. sessionID: input.sessionID,
  631. abort: abort.signal,
  632. messageID: assistantMsg.id,
  633. metadata: async (val) => {
  634. const match = processor.partFromToolCall(options.toolCallId)
  635. if (match && match.state.status === "running") {
  636. await updatePart({
  637. ...match,
  638. state: {
  639. title: val.title,
  640. metadata: val.metadata,
  641. status: "running",
  642. input: args,
  643. time: {
  644. start: Date.now(),
  645. },
  646. },
  647. })
  648. }
  649. },
  650. })
  651. return result
  652. },
  653. toModelOutput(result) {
  654. return {
  655. type: "text",
  656. value: result.output,
  657. }
  658. },
  659. })
  660. }
  661. for (const [key, item] of Object.entries(await MCP.tools())) {
  662. if (mode.tools[key] === false) continue
  663. const execute = item.execute
  664. if (!execute) continue
  665. item.execute = async (args, opts) => {
  666. await processor.track(opts.toolCallId)
  667. const result = await execute(args, opts)
  668. const output = result.content
  669. .filter((x: any) => x.type === "text")
  670. .map((x: any) => x.text)
  671. .join("\n\n")
  672. return {
  673. output,
  674. }
  675. }
  676. item.toModelOutput = (result) => {
  677. return {
  678. type: "text",
  679. value: result.output,
  680. }
  681. }
  682. tools[key] = item
  683. }
  684. const stream = streamText({
  685. onError() {},
  686. async prepareStep({ messages }) {
  687. const queue = (state().queued.get(input.sessionID) ?? []).filter((x) => !x.processed)
  688. if (queue.length) {
  689. for (const item of queue) {
  690. if (item.processed) continue
  691. messages.push(
  692. ...MessageV2.toModelMessage([
  693. {
  694. info: item.message,
  695. parts: item.parts,
  696. },
  697. ]),
  698. )
  699. item.processed = true
  700. }
  701. assistantMsg.time.completed = Date.now()
  702. await updateMessage(assistantMsg)
  703. Object.assign(assistantMsg, {
  704. id: Identifier.ascending("message"),
  705. role: "assistant",
  706. system,
  707. path: {
  708. cwd: app.path.cwd,
  709. root: app.path.root,
  710. },
  711. cost: 0,
  712. tokens: {
  713. input: 0,
  714. output: 0,
  715. reasoning: 0,
  716. cache: { read: 0, write: 0 },
  717. },
  718. modelID: input.modelID,
  719. providerID: input.providerID,
  720. time: {
  721. created: Date.now(),
  722. },
  723. sessionID: input.sessionID,
  724. })
  725. await updateMessage(assistantMsg)
  726. }
  727. return {
  728. messages,
  729. }
  730. },
  731. maxRetries: 10,
  732. maxOutputTokens: outputLimit,
  733. abortSignal: abort.signal,
  734. stopWhen: stepCountIs(1000),
  735. providerOptions: {
  736. [input.providerID]: model.info.options,
  737. },
  738. messages: [
  739. ...system.map(
  740. (x): ModelMessage => ({
  741. role: "system",
  742. content: x,
  743. }),
  744. ),
  745. ...MessageV2.toModelMessage(msgs),
  746. ],
  747. temperature: model.info.temperature ? 0 : undefined,
  748. tools: model.info.tool_call === false ? undefined : tools,
  749. model: wrapLanguageModel({
  750. model: model.language,
  751. middleware: [
  752. {
  753. async transformParams(args) {
  754. if (args.type === "stream") {
  755. // @ts-expect-error
  756. args.params.prompt = ProviderTransform.message(args.params.prompt, input.providerID, input.modelID)
  757. }
  758. return args.params
  759. },
  760. },
  761. ],
  762. }),
  763. })
  764. const result = await processor.process(stream)
  765. const queued = state().queued.get(input.sessionID) ?? []
  766. const unprocessed = queued.find((x) => !x.processed)
  767. if (unprocessed) {
  768. unprocessed.processed = true
  769. return chat(unprocessed.input)
  770. }
  771. for (const item of queued) {
  772. item.callback(result)
  773. }
  774. state().queued.delete(input.sessionID)
  775. return result
  776. }
  777. function createProcessor(assistantMsg: MessageV2.Assistant, model: ModelsDev.Model) {
  778. const toolCalls: Record<string, MessageV2.ToolPart> = {}
  779. const snapshots: Record<string, string> = {}
  780. return {
  781. async track(toolCallID: string) {
  782. const hash = await Snapshot.track()
  783. if (hash) snapshots[toolCallID] = hash
  784. },
  785. partFromToolCall(toolCallID: string) {
  786. return toolCalls[toolCallID]
  787. },
  788. async process(stream: StreamTextResult<Record<string, AITool>, never>) {
  789. try {
  790. let currentText: MessageV2.TextPart | undefined
  791. for await (const value of stream.fullStream) {
  792. log.info("part", {
  793. type: value.type,
  794. })
  795. switch (value.type) {
  796. case "start":
  797. break
  798. case "tool-input-start":
  799. const part = await updatePart({
  800. id: Identifier.ascending("part"),
  801. messageID: assistantMsg.id,
  802. sessionID: assistantMsg.sessionID,
  803. type: "tool",
  804. tool: value.toolName,
  805. callID: value.id,
  806. state: {
  807. status: "pending",
  808. },
  809. })
  810. toolCalls[value.id] = part as MessageV2.ToolPart
  811. break
  812. case "tool-input-delta":
  813. break
  814. case "tool-input-end":
  815. break
  816. case "tool-call": {
  817. const match = toolCalls[value.toolCallId]
  818. if (match) {
  819. const part = await updatePart({
  820. ...match,
  821. state: {
  822. status: "running",
  823. input: value.input,
  824. time: {
  825. start: Date.now(),
  826. },
  827. },
  828. })
  829. toolCalls[value.toolCallId] = part as MessageV2.ToolPart
  830. }
  831. break
  832. }
  833. case "tool-result": {
  834. const match = toolCalls[value.toolCallId]
  835. if (match && match.state.status === "running") {
  836. await updatePart({
  837. ...match,
  838. state: {
  839. status: "completed",
  840. input: value.input,
  841. output: value.output.output,
  842. metadata: value.output.metadata,
  843. title: value.output.title,
  844. time: {
  845. start: match.state.time.start,
  846. end: Date.now(),
  847. },
  848. },
  849. })
  850. delete toolCalls[value.toolCallId]
  851. const snapshot = snapshots[value.toolCallId]
  852. if (snapshot) {
  853. const patch = await Snapshot.patch(snapshot)
  854. if (patch.files.length) {
  855. await updatePart({
  856. id: Identifier.ascending("part"),
  857. messageID: assistantMsg.id,
  858. sessionID: assistantMsg.sessionID,
  859. type: "patch",
  860. hash: patch.hash,
  861. files: patch.files,
  862. })
  863. }
  864. }
  865. }
  866. break
  867. }
  868. case "tool-error": {
  869. const match = toolCalls[value.toolCallId]
  870. if (match && match.state.status === "running") {
  871. await updatePart({
  872. ...match,
  873. state: {
  874. status: "error",
  875. input: value.input,
  876. error: (value.error as any).toString(),
  877. time: {
  878. start: match.state.time.start,
  879. end: Date.now(),
  880. },
  881. },
  882. })
  883. delete toolCalls[value.toolCallId]
  884. const snapshot = snapshots[value.toolCallId]
  885. if (snapshot) {
  886. const patch = await Snapshot.patch(snapshot)
  887. await updatePart({
  888. id: Identifier.ascending("part"),
  889. messageID: assistantMsg.id,
  890. sessionID: assistantMsg.sessionID,
  891. type: "patch",
  892. hash: patch.hash,
  893. files: patch.files,
  894. })
  895. }
  896. }
  897. break
  898. }
  899. case "error":
  900. throw value.error
  901. case "start-step":
  902. await updatePart({
  903. id: Identifier.ascending("part"),
  904. messageID: assistantMsg.id,
  905. sessionID: assistantMsg.sessionID,
  906. type: "step-start",
  907. })
  908. break
  909. case "finish-step":
  910. const usage = getUsage(model, value.usage, value.providerMetadata)
  911. assistantMsg.cost += usage.cost
  912. assistantMsg.tokens = usage.tokens
  913. await updatePart({
  914. id: Identifier.ascending("part"),
  915. messageID: assistantMsg.id,
  916. sessionID: assistantMsg.sessionID,
  917. type: "step-finish",
  918. tokens: usage.tokens,
  919. cost: usage.cost,
  920. })
  921. await updateMessage(assistantMsg)
  922. break
  923. case "text-start":
  924. currentText = {
  925. id: Identifier.ascending("part"),
  926. messageID: assistantMsg.id,
  927. sessionID: assistantMsg.sessionID,
  928. type: "text",
  929. text: "",
  930. time: {
  931. start: Date.now(),
  932. },
  933. }
  934. break
  935. case "text":
  936. if (currentText) {
  937. currentText.text += value.text
  938. await updatePart(currentText)
  939. }
  940. break
  941. case "text-end":
  942. if (currentText && currentText.text) {
  943. currentText.time = {
  944. start: Date.now(),
  945. end: Date.now(),
  946. }
  947. await updatePart(currentText)
  948. }
  949. currentText = undefined
  950. break
  951. case "finish":
  952. assistantMsg.time.completed = Date.now()
  953. await updateMessage(assistantMsg)
  954. break
  955. default:
  956. log.info("unhandled", {
  957. ...value,
  958. })
  959. continue
  960. }
  961. }
  962. } catch (e) {
  963. log.error("", {
  964. error: e,
  965. })
  966. switch (true) {
  967. case e instanceof DOMException && e.name === "AbortError":
  968. assistantMsg.error = new MessageV2.AbortedError(
  969. { message: e.message },
  970. {
  971. cause: e,
  972. },
  973. ).toObject()
  974. break
  975. case MessageV2.OutputLengthError.isInstance(e):
  976. assistantMsg.error = e
  977. break
  978. case LoadAPIKeyError.isInstance(e):
  979. assistantMsg.error = new MessageV2.AuthError(
  980. {
  981. providerID: model.id,
  982. message: e.message,
  983. },
  984. { cause: e },
  985. ).toObject()
  986. break
  987. case e instanceof Error:
  988. assistantMsg.error = new NamedError.Unknown({ message: e.toString() }, { cause: e }).toObject()
  989. break
  990. default:
  991. assistantMsg.error = new NamedError.Unknown({ message: JSON.stringify(e) }, { cause: e })
  992. }
  993. Bus.publish(Event.Error, {
  994. sessionID: assistantMsg.sessionID,
  995. error: assistantMsg.error,
  996. })
  997. }
  998. const p = await getParts(assistantMsg.sessionID, assistantMsg.id)
  999. for (const part of p) {
  1000. if (part.type === "tool" && part.state.status !== "completed") {
  1001. updatePart({
  1002. ...part,
  1003. state: {
  1004. status: "error",
  1005. error: "Tool execution aborted",
  1006. time: {
  1007. start: Date.now(),
  1008. end: Date.now(),
  1009. },
  1010. input: {},
  1011. },
  1012. })
  1013. }
  1014. }
  1015. assistantMsg.time.completed = Date.now()
  1016. await updateMessage(assistantMsg)
  1017. return { info: assistantMsg, parts: p }
  1018. },
  1019. }
  1020. }
  1021. export const RevertInput = z.object({
  1022. sessionID: Identifier.schema("session"),
  1023. messageID: Identifier.schema("message"),
  1024. partID: Identifier.schema("part").optional(),
  1025. })
  1026. export type RevertInput = z.infer<typeof RevertInput>
  1027. export async function revert(input: RevertInput) {
  1028. const all = await messages(input.sessionID)
  1029. let lastUser: MessageV2.User | undefined
  1030. const session = await get(input.sessionID)
  1031. let revert: Info["revert"]
  1032. const patches: Snapshot.Patch[] = []
  1033. for (const msg of all) {
  1034. if (msg.info.role === "user") lastUser = msg.info
  1035. const remaining = []
  1036. for (const part of msg.parts) {
  1037. if (revert) {
  1038. if (part.type === "patch") {
  1039. patches.push(part)
  1040. }
  1041. continue
  1042. }
  1043. if (!revert) {
  1044. if ((msg.info.id === input.messageID && !input.partID) || part.id === input.partID) {
  1045. // if no useful parts left in message, same as reverting whole message
  1046. const partID = remaining.some((item) => ["text", "tool"].includes(item.type)) ? input.partID : undefined
  1047. revert = {
  1048. messageID: !partID && lastUser ? lastUser.id : msg.info.id,
  1049. partID,
  1050. }
  1051. }
  1052. remaining.push(part)
  1053. }
  1054. }
  1055. }
  1056. if (revert) {
  1057. const session = await get(input.sessionID)
  1058. revert.snapshot = session.revert?.snapshot ?? (await Snapshot.track())
  1059. await Snapshot.revert(patches)
  1060. return update(input.sessionID, (draft) => {
  1061. draft.revert = revert
  1062. })
  1063. }
  1064. return session
  1065. }
  1066. export async function unrevert(input: { sessionID: string }) {
  1067. log.info("unreverting", input)
  1068. const session = await get(input.sessionID)
  1069. if (!session.revert) return session
  1070. if (session.revert.snapshot) await Snapshot.restore(session.revert.snapshot)
  1071. const next = await update(input.sessionID, (draft) => {
  1072. draft.revert = undefined
  1073. })
  1074. return next
  1075. }
  1076. export async function summarize(input: { sessionID: string; providerID: string; modelID: string }) {
  1077. using abort = lock(input.sessionID)
  1078. const msgs = await messages(input.sessionID)
  1079. const lastSummary = msgs.findLast((msg) => msg.info.role === "assistant" && msg.info.summary === true)
  1080. const filtered = msgs.filter((msg) => !lastSummary || msg.info.id >= lastSummary.info.id)
  1081. const model = await Provider.getModel(input.providerID, input.modelID)
  1082. const app = App.info()
  1083. const system = [
  1084. ...SystemPrompt.summarize(input.providerID),
  1085. ...(await SystemPrompt.environment()),
  1086. ...(await SystemPrompt.custom()),
  1087. ]
  1088. const next: MessageV2.Info = {
  1089. id: Identifier.ascending("message"),
  1090. role: "assistant",
  1091. sessionID: input.sessionID,
  1092. system,
  1093. mode: "build",
  1094. path: {
  1095. cwd: app.path.cwd,
  1096. root: app.path.root,
  1097. },
  1098. summary: true,
  1099. cost: 0,
  1100. modelID: input.modelID,
  1101. providerID: input.providerID,
  1102. tokens: {
  1103. input: 0,
  1104. output: 0,
  1105. reasoning: 0,
  1106. cache: { read: 0, write: 0 },
  1107. },
  1108. time: {
  1109. created: Date.now(),
  1110. },
  1111. }
  1112. await updateMessage(next)
  1113. const processor = createProcessor(next, model.info)
  1114. const stream = streamText({
  1115. maxRetries: 10,
  1116. abortSignal: abort.signal,
  1117. model: model.language,
  1118. messages: [
  1119. ...system.map(
  1120. (x): ModelMessage => ({
  1121. role: "system",
  1122. content: x,
  1123. }),
  1124. ),
  1125. ...MessageV2.toModelMessage(filtered),
  1126. {
  1127. role: "user",
  1128. content: [
  1129. {
  1130. type: "text",
  1131. text: "Provide a detailed but concise summary of our conversation above. Focus on information that would be helpful for continuing the conversation, including what we did, what we're doing, which files we're working on, and what we're going to do next.",
  1132. },
  1133. ],
  1134. },
  1135. ],
  1136. })
  1137. const result = await processor.process(stream)
  1138. return result
  1139. }
  1140. function isLocked(sessionID: string) {
  1141. return state().pending.has(sessionID)
  1142. }
  1143. function lock(sessionID: string) {
  1144. log.info("locking", { sessionID })
  1145. if (state().pending.has(sessionID)) throw new BusyError(sessionID)
  1146. const controller = new AbortController()
  1147. state().pending.set(sessionID, controller)
  1148. return {
  1149. signal: controller.signal,
  1150. [Symbol.dispose]() {
  1151. log.info("unlocking", { sessionID })
  1152. state().pending.delete(sessionID)
  1153. Bus.publish(Event.Idle, {
  1154. sessionID,
  1155. })
  1156. },
  1157. }
  1158. }
  1159. function getUsage(model: ModelsDev.Model, usage: LanguageModelUsage, metadata?: ProviderMetadata) {
  1160. const tokens = {
  1161. input: usage.inputTokens ?? 0,
  1162. output: usage.outputTokens ?? 0,
  1163. reasoning: 0,
  1164. cache: {
  1165. write: (metadata?.["anthropic"]?.["cacheCreationInputTokens"] ??
  1166. // @ts-expect-error
  1167. metadata?.["bedrock"]?.["usage"]?.["cacheWriteInputTokens"] ??
  1168. 0) as number,
  1169. read: usage.cachedInputTokens ?? 0,
  1170. },
  1171. }
  1172. return {
  1173. cost: new Decimal(0)
  1174. .add(new Decimal(tokens.input).mul(model.cost.input).div(1_000_000))
  1175. .add(new Decimal(tokens.output).mul(model.cost.output).div(1_000_000))
  1176. .add(new Decimal(tokens.cache.read).mul(model.cost.cache_read ?? 0).div(1_000_000))
  1177. .add(new Decimal(tokens.cache.write).mul(model.cost.cache_write ?? 0).div(1_000_000))
  1178. .toNumber(),
  1179. tokens,
  1180. }
  1181. }
  1182. export class BusyError extends Error {
  1183. constructor(public readonly sessionID: string) {
  1184. super(`Session ${sessionID} is busy`)
  1185. }
  1186. }
  1187. export async function initialize(input: {
  1188. sessionID: string
  1189. modelID: string
  1190. providerID: string
  1191. messageID: string
  1192. }) {
  1193. const app = App.info()
  1194. await Session.chat({
  1195. sessionID: input.sessionID,
  1196. messageID: input.messageID,
  1197. providerID: input.providerID,
  1198. modelID: input.modelID,
  1199. parts: [
  1200. {
  1201. id: Identifier.ascending("part"),
  1202. type: "text",
  1203. text: PROMPT_INITIALIZE.replace("${path}", app.path.root),
  1204. },
  1205. ],
  1206. })
  1207. await App.initialize()
  1208. }
  1209. }