index.ts 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643
  1. import path from "path"
  2. import { spawn } from "child_process"
  3. import { Decimal } from "decimal.js"
  4. import { z, ZodSchema } from "zod"
  5. import {
  6. generateText,
  7. LoadAPIKeyError,
  8. streamText,
  9. tool,
  10. wrapLanguageModel,
  11. type Tool as AITool,
  12. type LanguageModelUsage,
  13. type ProviderMetadata,
  14. type ModelMessage,
  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 { LSP } from "../lsp"
  38. import { ReadTool } from "../tool/read"
  39. import { mergeDeep, pipe, splitWhen } from "remeda"
  40. import { ToolRegistry } from "../tool/registry"
  41. import { Plugin } from "../plugin"
  42. import { Agent } from "../agent/agent"
  43. import { Permission } from "../permission"
  44. import { Wildcard } from "../util/wildcard"
  45. import { ulid } from "ulid"
  46. import { defer } from "../util/defer"
  47. export namespace Session {
  48. const log = Log.create({ service: "session" })
  49. const OUTPUT_TOKEN_MAX = 32_000
  50. const parentSessionTitlePrefix = "New session - "
  51. const childSessionTitlePrefix = "Child session - "
  52. function createDefaultTitle(isChild = false) {
  53. return (isChild ? childSessionTitlePrefix : parentSessionTitlePrefix) + new Date().toISOString()
  54. }
  55. function isDefaultTitle(title: string) {
  56. return title.startsWith(parentSessionTitlePrefix)
  57. }
  58. export const Info = z
  59. .object({
  60. id: Identifier.schema("session"),
  61. parentID: Identifier.schema("session").optional(),
  62. share: z
  63. .object({
  64. url: z.string(),
  65. })
  66. .optional(),
  67. title: z.string(),
  68. version: z.string(),
  69. time: z.object({
  70. created: z.number(),
  71. updated: z.number(),
  72. }),
  73. revert: z
  74. .object({
  75. messageID: z.string(),
  76. partID: z.string().optional(),
  77. snapshot: z.string().optional(),
  78. diff: z.string().optional(),
  79. })
  80. .optional(),
  81. })
  82. .openapi({
  83. ref: "Session",
  84. })
  85. export type Info = z.output<typeof Info>
  86. export const ShareInfo = z
  87. .object({
  88. secret: z.string(),
  89. url: z.string(),
  90. })
  91. .openapi({
  92. ref: "SessionShare",
  93. })
  94. export type ShareInfo = z.output<typeof ShareInfo>
  95. export const Event = {
  96. Updated: Bus.event(
  97. "session.updated",
  98. z.object({
  99. info: Info,
  100. }),
  101. ),
  102. Deleted: Bus.event(
  103. "session.deleted",
  104. z.object({
  105. info: Info,
  106. }),
  107. ),
  108. Idle: Bus.event(
  109. "session.idle",
  110. z.object({
  111. sessionID: z.string(),
  112. }),
  113. ),
  114. Error: Bus.event(
  115. "session.error",
  116. z.object({
  117. sessionID: z.string().optional(),
  118. error: MessageV2.Assistant.shape.error,
  119. }),
  120. ),
  121. }
  122. const state = App.state(
  123. "session",
  124. () => {
  125. const sessions = new Map<string, Info>()
  126. const messages = new Map<string, MessageV2.Info[]>()
  127. const pending = new Map<string, AbortController>()
  128. const autoCompacting = new Map<string, boolean>()
  129. const queued = new Map<
  130. string,
  131. {
  132. input: ChatInput
  133. message: MessageV2.User
  134. parts: MessageV2.Part[]
  135. processed: boolean
  136. callback: (input: { info: MessageV2.Assistant; parts: MessageV2.Part[] }) => void
  137. }[]
  138. >()
  139. return {
  140. sessions,
  141. messages,
  142. pending,
  143. autoCompacting,
  144. queued,
  145. }
  146. },
  147. async (state) => {
  148. for (const [_, controller] of state.pending) {
  149. controller.abort()
  150. }
  151. },
  152. )
  153. export async function create(parentID?: string, title?: string) {
  154. const result: Info = {
  155. id: Identifier.descending("session"),
  156. version: Installation.VERSION,
  157. parentID,
  158. title: title ?? createDefaultTitle(!!parentID),
  159. time: {
  160. created: Date.now(),
  161. updated: Date.now(),
  162. },
  163. }
  164. log.info("created", result)
  165. state().sessions.set(result.id, result)
  166. await Storage.writeJSON("session/info/" + result.id, result)
  167. const cfg = await Config.get()
  168. if (!result.parentID && (Flag.OPENCODE_AUTO_SHARE || cfg.share === "auto"))
  169. share(result.id)
  170. .then((share) => {
  171. update(result.id, (draft) => {
  172. draft.share = share
  173. })
  174. })
  175. .catch(() => {
  176. // Silently ignore sharing errors during session creation
  177. })
  178. Bus.publish(Event.Updated, {
  179. info: result,
  180. })
  181. return result
  182. }
  183. export async function get(id: string) {
  184. const result = state().sessions.get(id)
  185. if (result) {
  186. return result
  187. }
  188. const read = await Storage.readJSON<Info>("session/info/" + id)
  189. state().sessions.set(id, read)
  190. return read as Info
  191. }
  192. export async function getShare(id: string) {
  193. return Storage.readJSON<ShareInfo>("session/share/" + id)
  194. }
  195. export async function share(id: string) {
  196. const cfg = await Config.get()
  197. if (cfg.share === "disabled") {
  198. throw new Error("Sharing is disabled in configuration")
  199. }
  200. const session = await get(id)
  201. if (session.share) return session.share
  202. const share = await Share.create(id)
  203. await update(id, (draft) => {
  204. draft.share = {
  205. url: share.url,
  206. }
  207. })
  208. await Storage.writeJSON<ShareInfo>("session/share/" + id, share)
  209. await Share.sync("session/info/" + id, session)
  210. for (const msg of await messages(id)) {
  211. await Share.sync("session/message/" + id + "/" + msg.info.id, msg.info)
  212. for (const part of msg.parts) {
  213. await Share.sync("session/part/" + id + "/" + msg.info.id + "/" + part.id, part)
  214. }
  215. }
  216. return share
  217. }
  218. export async function unshare(id: string) {
  219. const share = await getShare(id)
  220. if (!share) return
  221. await Storage.remove("session/share/" + id)
  222. await update(id, (draft) => {
  223. draft.share = undefined
  224. })
  225. await Share.remove(id, share.secret)
  226. }
  227. export async function update(id: string, editor: (session: Info) => void) {
  228. const { sessions } = state()
  229. const session = await get(id)
  230. if (!session) return
  231. editor(session)
  232. session.time.updated = Date.now()
  233. sessions.set(id, session)
  234. await Storage.writeJSON("session/info/" + id, session)
  235. Bus.publish(Event.Updated, {
  236. info: session,
  237. })
  238. return session
  239. }
  240. export async function messages(sessionID: string) {
  241. const result = [] as {
  242. info: MessageV2.Info
  243. parts: MessageV2.Part[]
  244. }[]
  245. for (const p of await Storage.list("session/message/" + sessionID)) {
  246. const read = await Storage.readJSON<MessageV2.Info>(p)
  247. result.push({
  248. info: read,
  249. parts: await getParts(sessionID, read.id),
  250. })
  251. }
  252. result.sort((a, b) => (a.info.id > b.info.id ? 1 : -1))
  253. return result
  254. }
  255. export async function getMessage(sessionID: string, messageID: string) {
  256. return {
  257. info: await Storage.readJSON<MessageV2.Info>("session/message/" + sessionID + "/" + messageID),
  258. parts: await getParts(sessionID, messageID),
  259. }
  260. }
  261. export async function getParts(sessionID: string, messageID: string) {
  262. const result = [] as MessageV2.Part[]
  263. for (const item of await Storage.list("session/part/" + sessionID + "/" + messageID)) {
  264. const read = await Storage.readJSON<MessageV2.Part>(item)
  265. result.push(read)
  266. }
  267. result.sort((a, b) => (a.id > b.id ? 1 : -1))
  268. return result
  269. }
  270. export async function* list() {
  271. for (const item of await Storage.list("session/info")) {
  272. const sessionID = path.basename(item, ".json")
  273. yield get(sessionID)
  274. }
  275. }
  276. export async function children(parentID: string) {
  277. const result = [] as Session.Info[]
  278. for (const item of await Storage.list("session/info")) {
  279. const sessionID = path.basename(item, ".json")
  280. const session = await get(sessionID)
  281. if (session.parentID !== parentID) continue
  282. result.push(session)
  283. }
  284. return result
  285. }
  286. export function abort(sessionID: string) {
  287. const controller = state().pending.get(sessionID)
  288. if (!controller) return false
  289. log.info("aborting", {
  290. sessionID,
  291. })
  292. controller.abort()
  293. state().pending.delete(sessionID)
  294. return true
  295. }
  296. export async function remove(sessionID: string, emitEvent = true) {
  297. try {
  298. abort(sessionID)
  299. const session = await get(sessionID)
  300. for (const child of await children(sessionID)) {
  301. await remove(child.id, false)
  302. }
  303. await unshare(sessionID).catch(() => {})
  304. await Storage.remove(`session/info/${sessionID}`).catch(() => {})
  305. await Storage.removeDir(`session/message/${sessionID}/`).catch(() => {})
  306. state().sessions.delete(sessionID)
  307. state().messages.delete(sessionID)
  308. if (emitEvent) {
  309. Bus.publish(Event.Deleted, {
  310. info: session,
  311. })
  312. }
  313. } catch (e) {
  314. log.error(e)
  315. }
  316. }
  317. async function updateMessage(msg: MessageV2.Info) {
  318. await Storage.writeJSON("session/message/" + msg.sessionID + "/" + msg.id, msg)
  319. Bus.publish(MessageV2.Event.Updated, {
  320. info: msg,
  321. })
  322. }
  323. async function updatePart(part: MessageV2.Part) {
  324. await Storage.writeJSON(["session", "part", part.sessionID, part.messageID, part.id].join("/"), part)
  325. Bus.publish(MessageV2.Event.PartUpdated, {
  326. part,
  327. })
  328. return part
  329. }
  330. export const ChatInput = z.object({
  331. sessionID: Identifier.schema("session"),
  332. messageID: Identifier.schema("message").optional(),
  333. providerID: z.string(),
  334. modelID: z.string(),
  335. agent: z.string().optional(),
  336. system: z.string().optional(),
  337. tools: z.record(z.boolean()).optional(),
  338. parts: z.array(
  339. z.discriminatedUnion("type", [
  340. MessageV2.TextPart.omit({
  341. messageID: true,
  342. sessionID: true,
  343. })
  344. .partial({
  345. id: true,
  346. })
  347. .openapi({
  348. ref: "TextPartInput",
  349. }),
  350. MessageV2.FilePart.omit({
  351. messageID: true,
  352. sessionID: true,
  353. })
  354. .partial({
  355. id: true,
  356. })
  357. .openapi({
  358. ref: "FilePartInput",
  359. }),
  360. MessageV2.AgentPart.omit({
  361. messageID: true,
  362. sessionID: true,
  363. })
  364. .partial({
  365. id: true,
  366. })
  367. .openapi({
  368. ref: "AgentPartInput",
  369. }),
  370. ]),
  371. ),
  372. })
  373. export type ChatInput = z.infer<typeof ChatInput>
  374. export async function chat(
  375. input: z.infer<typeof ChatInput>,
  376. ): Promise<{ info: MessageV2.Assistant; parts: MessageV2.Part[] }> {
  377. const l = log.clone().tag("session", input.sessionID)
  378. l.info("chatting")
  379. const inputAgent = input.agent ?? "build"
  380. // Process revert cleanup first, before creating new messages
  381. const session = await get(input.sessionID)
  382. if (session.revert) {
  383. let msgs = await messages(input.sessionID)
  384. const messageID = session.revert.messageID
  385. const [preserve, remove] = splitWhen(msgs, (x) => x.info.id === messageID)
  386. msgs = preserve
  387. for (const msg of remove) {
  388. await Storage.remove(`session/message/${input.sessionID}/${msg.info.id}`)
  389. await Bus.publish(MessageV2.Event.Removed, { sessionID: input.sessionID, messageID: msg.info.id })
  390. }
  391. const last = preserve.at(-1)
  392. if (session.revert.partID && last) {
  393. const partID = session.revert.partID
  394. const [preserveParts, removeParts] = splitWhen(last.parts, (x) => x.id === partID)
  395. last.parts = preserveParts
  396. for (const part of removeParts) {
  397. await Storage.remove(`session/part/${input.sessionID}/${last.info.id}/${part.id}`)
  398. await Bus.publish(MessageV2.Event.PartRemoved, {
  399. sessionID: input.sessionID,
  400. messageID: last.info.id,
  401. partID: part.id,
  402. })
  403. }
  404. }
  405. await update(input.sessionID, (draft) => {
  406. draft.revert = undefined
  407. })
  408. }
  409. const userMsg: MessageV2.Info = {
  410. id: input.messageID ?? Identifier.ascending("message"),
  411. role: "user",
  412. sessionID: input.sessionID,
  413. time: {
  414. created: Date.now(),
  415. },
  416. }
  417. const app = App.info()
  418. const userParts = await Promise.all(
  419. input.parts.map(async (part): Promise<MessageV2.Part[]> => {
  420. if (part.type === "file") {
  421. const url = new URL(part.url)
  422. switch (url.protocol) {
  423. case "data:":
  424. if (part.mime === "text/plain") {
  425. return [
  426. {
  427. id: Identifier.ascending("part"),
  428. messageID: userMsg.id,
  429. sessionID: input.sessionID,
  430. type: "text",
  431. synthetic: true,
  432. text: `Called the Read tool with the following input: ${JSON.stringify({ filePath: part.filename })}`,
  433. },
  434. {
  435. id: Identifier.ascending("part"),
  436. messageID: userMsg.id,
  437. sessionID: input.sessionID,
  438. type: "text",
  439. synthetic: true,
  440. text: Buffer.from(part.url, "base64url").toString(),
  441. },
  442. {
  443. ...part,
  444. id: part.id ?? Identifier.ascending("part"),
  445. messageID: userMsg.id,
  446. sessionID: input.sessionID,
  447. },
  448. ]
  449. }
  450. break
  451. case "file:":
  452. // have to normalize, symbol search returns absolute paths
  453. // Decode the pathname since URL constructor doesn't automatically decode it
  454. const filePath = decodeURIComponent(url.pathname)
  455. if (part.mime === "text/plain") {
  456. let offset: number | undefined = undefined
  457. let limit: number | undefined = undefined
  458. const range = {
  459. start: url.searchParams.get("start"),
  460. end: url.searchParams.get("end"),
  461. }
  462. if (range.start != null) {
  463. const filePath = part.url.split("?")[0]
  464. let start = parseInt(range.start)
  465. let end = range.end ? parseInt(range.end) : undefined
  466. // some LSP servers (eg, gopls) don't give full range in
  467. // workspace/symbol searches, so we'll try to find the
  468. // symbol in the document to get the full range
  469. if (start === end) {
  470. const symbols = await LSP.documentSymbol(filePath)
  471. for (const symbol of symbols) {
  472. let range: LSP.Range | undefined
  473. if ("range" in symbol) {
  474. range = symbol.range
  475. } else if ("location" in symbol) {
  476. range = symbol.location.range
  477. }
  478. if (range?.start?.line && range?.start?.line === start) {
  479. start = range.start.line
  480. end = range?.end?.line ?? start
  481. break
  482. }
  483. }
  484. offset = Math.max(start - 2, 0)
  485. if (end) {
  486. limit = end - offset + 2
  487. }
  488. }
  489. }
  490. const args = { filePath, offset, limit }
  491. const result = await ReadTool.init().then((t) =>
  492. t.execute(args, {
  493. sessionID: input.sessionID,
  494. abort: new AbortController().signal,
  495. agent: input.agent!,
  496. messageID: userMsg.id,
  497. extra: { bypassCwdCheck: true },
  498. metadata: async () => {},
  499. }),
  500. )
  501. return [
  502. {
  503. id: Identifier.ascending("part"),
  504. messageID: userMsg.id,
  505. sessionID: input.sessionID,
  506. type: "text",
  507. synthetic: true,
  508. text: `Called the Read tool with the following input: ${JSON.stringify(args)}`,
  509. },
  510. {
  511. id: Identifier.ascending("part"),
  512. messageID: userMsg.id,
  513. sessionID: input.sessionID,
  514. type: "text",
  515. synthetic: true,
  516. text: result.output,
  517. },
  518. {
  519. ...part,
  520. id: part.id ?? Identifier.ascending("part"),
  521. messageID: userMsg.id,
  522. sessionID: input.sessionID,
  523. },
  524. ]
  525. }
  526. let file = Bun.file(filePath)
  527. FileTime.read(input.sessionID, filePath)
  528. return [
  529. {
  530. id: Identifier.ascending("part"),
  531. messageID: userMsg.id,
  532. sessionID: input.sessionID,
  533. type: "text",
  534. text: `Called the Read tool with the following input: {\"filePath\":\"${filePath}\"}`,
  535. synthetic: true,
  536. },
  537. {
  538. id: part.id ?? Identifier.ascending("part"),
  539. messageID: userMsg.id,
  540. sessionID: input.sessionID,
  541. type: "file",
  542. url: `data:${part.mime};base64,` + Buffer.from(await file.bytes()).toString("base64"),
  543. mime: part.mime,
  544. filename: part.filename!,
  545. source: part.source,
  546. },
  547. ]
  548. }
  549. }
  550. if (part.type === "agent") {
  551. return [
  552. {
  553. id: Identifier.ascending("part"),
  554. ...part,
  555. messageID: userMsg.id,
  556. sessionID: input.sessionID,
  557. },
  558. {
  559. id: Identifier.ascending("part"),
  560. messageID: userMsg.id,
  561. sessionID: input.sessionID,
  562. type: "text",
  563. synthetic: true,
  564. text:
  565. "Use the above message and context to generate a prompt and call the task tool with subagent: " +
  566. part.name,
  567. },
  568. ]
  569. }
  570. return [
  571. {
  572. id: Identifier.ascending("part"),
  573. ...part,
  574. messageID: userMsg.id,
  575. sessionID: input.sessionID,
  576. },
  577. ]
  578. }),
  579. ).then((x) => x.flat())
  580. await Plugin.trigger(
  581. "chat.message",
  582. {},
  583. {
  584. message: userMsg,
  585. parts: userParts,
  586. },
  587. )
  588. await updateMessage(userMsg)
  589. for (const part of userParts) {
  590. await updatePart(part)
  591. }
  592. // mark session as updated
  593. // used for session list sorting (indicates when session was most recently interacted with)
  594. await update(input.sessionID, (_draft) => {})
  595. if (isLocked(input.sessionID)) {
  596. return new Promise((resolve) => {
  597. const queue = state().queued.get(input.sessionID) ?? []
  598. queue.push({
  599. input: input,
  600. message: userMsg,
  601. parts: userParts,
  602. processed: false,
  603. callback: resolve,
  604. })
  605. state().queued.set(input.sessionID, queue)
  606. })
  607. }
  608. const model = await Provider.getModel(input.providerID, input.modelID)
  609. let msgs = await messages(input.sessionID)
  610. const previous = msgs.filter((x) => x.info.role === "assistant").at(-1)?.info as MessageV2.Assistant
  611. const outputLimit = Math.min(model.info.limit.output, OUTPUT_TOKEN_MAX) || OUTPUT_TOKEN_MAX
  612. // auto summarize if too long
  613. if (previous && previous.tokens) {
  614. const tokens =
  615. previous.tokens.input + previous.tokens.cache.read + previous.tokens.cache.write + previous.tokens.output
  616. if (model.info.limit.context && tokens > Math.max((model.info.limit.context - outputLimit) * 0.9, 0)) {
  617. state().autoCompacting.set(input.sessionID, true)
  618. await summarize({
  619. sessionID: input.sessionID,
  620. providerID: input.providerID,
  621. modelID: input.modelID,
  622. })
  623. return chat(input)
  624. }
  625. }
  626. using abort = lock(input.sessionID)
  627. const lastSummary = msgs.findLast((msg) => msg.info.role === "assistant" && msg.info.summary === true)
  628. if (lastSummary) msgs = msgs.filter((msg) => msg.info.id >= lastSummary.info.id)
  629. if (msgs.filter((m) => m.info.role === "user").length === 1 && !session.parentID && isDefaultTitle(session.title)) {
  630. const small = (await Provider.getSmallModel(input.providerID)) ?? model
  631. generateText({
  632. maxOutputTokens: small.info.reasoning ? 1024 : 20,
  633. providerOptions: {
  634. [input.providerID]: {
  635. ...small.info.options,
  636. ...ProviderTransform.options(input.providerID, small.info.id, input.sessionID),
  637. },
  638. },
  639. messages: [
  640. ...SystemPrompt.title(input.providerID).map(
  641. (x): ModelMessage => ({
  642. role: "system",
  643. content: x,
  644. }),
  645. ),
  646. ...MessageV2.toModelMessage([
  647. {
  648. info: {
  649. id: Identifier.ascending("message"),
  650. role: "user",
  651. sessionID: input.sessionID,
  652. time: {
  653. created: Date.now(),
  654. },
  655. },
  656. parts: userParts,
  657. },
  658. ]),
  659. ],
  660. model: small.language,
  661. })
  662. .then((result) => {
  663. if (result.text)
  664. return Session.update(input.sessionID, (draft) => {
  665. const cleaned = result.text.replace(/<think>[\s\S]*?<\/think>\s*/g, "")
  666. const title = cleaned.length > 100 ? cleaned.substring(0, 97) + "..." : cleaned
  667. draft.title = title.trim()
  668. })
  669. })
  670. .catch(() => {})
  671. }
  672. const agent = await Agent.get(inputAgent)
  673. if (agent.name === "plan") {
  674. msgs.at(-1)?.parts.push({
  675. id: Identifier.ascending("part"),
  676. messageID: userMsg.id,
  677. sessionID: input.sessionID,
  678. type: "text",
  679. text: PROMPT_PLAN,
  680. synthetic: true,
  681. })
  682. }
  683. let system = SystemPrompt.header(input.providerID)
  684. system.push(
  685. ...(() => {
  686. if (input.system) return [input.system]
  687. if (agent.prompt) return [agent.prompt]
  688. return SystemPrompt.provider(input.modelID)
  689. })(),
  690. )
  691. system.push(...(await SystemPrompt.environment()))
  692. system.push(...(await SystemPrompt.custom()))
  693. // max 2 system prompt messages for caching purposes
  694. const [first, ...rest] = system
  695. system = [first, rest.join("\n")]
  696. const assistantMsg: MessageV2.Info = {
  697. id: Identifier.ascending("message"),
  698. role: "assistant",
  699. system,
  700. mode: inputAgent,
  701. path: {
  702. cwd: app.path.cwd,
  703. root: app.path.root,
  704. },
  705. cost: 0,
  706. tokens: {
  707. input: 0,
  708. output: 0,
  709. reasoning: 0,
  710. cache: { read: 0, write: 0 },
  711. },
  712. modelID: input.modelID,
  713. providerID: input.providerID,
  714. time: {
  715. created: Date.now(),
  716. },
  717. sessionID: input.sessionID,
  718. }
  719. await updateMessage(assistantMsg)
  720. await using _ = defer(async () => {
  721. if (assistantMsg.time.completed) return
  722. await Storage.remove(`session/message/${input.sessionID}/${assistantMsg.id}`)
  723. await Bus.publish(MessageV2.Event.Removed, { sessionID: input.sessionID, messageID: assistantMsg.id })
  724. })
  725. const tools: Record<string, AITool> = {}
  726. const processor = createProcessor(assistantMsg, model.info)
  727. const enabledTools = pipe(
  728. agent.tools,
  729. mergeDeep(await ToolRegistry.enabled(input.providerID, input.modelID, agent)),
  730. mergeDeep(input.tools ?? {}),
  731. )
  732. for (const item of await ToolRegistry.tools(input.providerID, input.modelID)) {
  733. if (Wildcard.all(item.id, enabledTools) === false) continue
  734. tools[item.id] = tool({
  735. id: item.id as any,
  736. description: item.description,
  737. inputSchema: item.parameters as ZodSchema,
  738. async execute(args, options) {
  739. await Plugin.trigger(
  740. "tool.execute.before",
  741. {
  742. tool: item.id,
  743. sessionID: input.sessionID,
  744. callID: options.toolCallId,
  745. },
  746. {
  747. args,
  748. },
  749. )
  750. const result = await item.execute(args, {
  751. sessionID: input.sessionID,
  752. abort: options.abortSignal!,
  753. messageID: assistantMsg.id,
  754. callID: options.toolCallId,
  755. agent: agent.name,
  756. metadata: async (val) => {
  757. const match = processor.partFromToolCall(options.toolCallId)
  758. if (match && match.state.status === "running") {
  759. await updatePart({
  760. ...match,
  761. state: {
  762. title: val.title,
  763. metadata: val.metadata,
  764. status: "running",
  765. input: args,
  766. time: {
  767. start: Date.now(),
  768. },
  769. },
  770. })
  771. }
  772. },
  773. })
  774. await Plugin.trigger(
  775. "tool.execute.after",
  776. {
  777. tool: item.id,
  778. sessionID: input.sessionID,
  779. callID: options.toolCallId,
  780. },
  781. result,
  782. )
  783. return result
  784. },
  785. toModelOutput(result) {
  786. return {
  787. type: "text",
  788. value: result.output,
  789. }
  790. },
  791. })
  792. }
  793. for (const [key, item] of Object.entries(await MCP.tools())) {
  794. if (Wildcard.all(key, enabledTools) === false) continue
  795. const execute = item.execute
  796. if (!execute) continue
  797. item.execute = async (args, opts) => {
  798. const result = await execute(args, opts)
  799. const output = result.content
  800. .filter((x: any) => x.type === "text")
  801. .map((x: any) => x.text)
  802. .join("\n\n")
  803. return {
  804. output,
  805. }
  806. }
  807. item.toModelOutput = (result) => {
  808. return {
  809. type: "text",
  810. value: result.output,
  811. }
  812. }
  813. tools[key] = item
  814. }
  815. const params = await Plugin.trigger(
  816. "chat.params",
  817. {
  818. model: model.info,
  819. provider: await Provider.getProvider(input.providerID),
  820. message: userMsg,
  821. },
  822. {
  823. temperature: model.info.temperature
  824. ? (agent.temperature ?? ProviderTransform.temperature(input.providerID, input.modelID))
  825. : undefined,
  826. topP: agent.topP ?? ProviderTransform.topP(input.providerID, input.modelID),
  827. options: {
  828. ...ProviderTransform.options(input.providerID, input.modelID, input.sessionID),
  829. ...model.info.options,
  830. ...agent.options,
  831. },
  832. },
  833. )
  834. const stream = streamText({
  835. onError(e) {
  836. log.error("streamText error", {
  837. error: e,
  838. })
  839. },
  840. async prepareStep({ messages }) {
  841. const queue = (state().queued.get(input.sessionID) ?? []).filter((x) => !x.processed)
  842. if (queue.length) {
  843. for (const item of queue) {
  844. if (item.processed) continue
  845. messages.push(
  846. ...MessageV2.toModelMessage([
  847. {
  848. info: item.message,
  849. parts: item.parts,
  850. },
  851. ]),
  852. )
  853. item.processed = true
  854. }
  855. assistantMsg.time.completed = Date.now()
  856. await updateMessage(assistantMsg)
  857. Object.assign(assistantMsg, {
  858. id: Identifier.ascending("message"),
  859. role: "assistant",
  860. system,
  861. path: {
  862. cwd: app.path.cwd,
  863. root: app.path.root,
  864. },
  865. cost: 0,
  866. tokens: {
  867. input: 0,
  868. output: 0,
  869. reasoning: 0,
  870. cache: { read: 0, write: 0 },
  871. },
  872. modelID: input.modelID,
  873. providerID: input.providerID,
  874. mode: inputAgent,
  875. time: {
  876. created: Date.now(),
  877. },
  878. sessionID: input.sessionID,
  879. })
  880. await updateMessage(assistantMsg)
  881. }
  882. return {
  883. messages,
  884. }
  885. },
  886. async experimental_repairToolCall(input) {
  887. return {
  888. ...input.toolCall,
  889. input: JSON.stringify({
  890. tool: input.toolCall.toolName,
  891. error: input.error.message,
  892. }),
  893. toolName: "invalid",
  894. }
  895. },
  896. maxRetries: 3,
  897. activeTools: Object.keys(tools).filter((x) => x !== "invalid"),
  898. maxOutputTokens: outputLimit,
  899. abortSignal: abort.signal,
  900. stopWhen: async ({ steps }) => {
  901. if (steps.length >= 1000) {
  902. return true
  903. }
  904. // Check if processor flagged that we should stop
  905. if (processor.getShouldStop()) {
  906. return true
  907. }
  908. return false
  909. },
  910. providerOptions: {
  911. [input.providerID]: params.options,
  912. },
  913. temperature: params.temperature,
  914. topP: params.topP,
  915. messages: [
  916. ...system.map(
  917. (x): ModelMessage => ({
  918. role: "system",
  919. content: x,
  920. }),
  921. ),
  922. ...MessageV2.toModelMessage(msgs.filter((m) => !(m.info.role === "assistant" && m.info.error))),
  923. ],
  924. tools: model.info.tool_call === false ? undefined : tools,
  925. model: wrapLanguageModel({
  926. model: model.language,
  927. middleware: [
  928. {
  929. async transformParams(args) {
  930. if (args.type === "stream") {
  931. // @ts-expect-error
  932. args.params.prompt = ProviderTransform.message(args.params.prompt, input.providerID, input.modelID)
  933. }
  934. return args.params
  935. },
  936. },
  937. ],
  938. }),
  939. })
  940. const result = await processor.process(stream)
  941. const queued = state().queued.get(input.sessionID) ?? []
  942. const unprocessed = queued.find((x) => !x.processed)
  943. if (unprocessed) {
  944. unprocessed.processed = true
  945. return chat(unprocessed.input)
  946. }
  947. for (const item of queued) {
  948. item.callback(result)
  949. }
  950. state().queued.delete(input.sessionID)
  951. return result
  952. }
  953. export const CommandInput = z.object({
  954. sessionID: Identifier.schema("session"),
  955. agent: z.string(),
  956. command: z.string(),
  957. })
  958. export type CommandInput = z.infer<typeof CommandInput>
  959. export async function shell(input: CommandInput) {
  960. using abort = lock(input.sessionID)
  961. const msg: MessageV2.Assistant = {
  962. id: Identifier.ascending("message"),
  963. sessionID: input.sessionID,
  964. system: [],
  965. mode: input.agent,
  966. cost: 0,
  967. path: {
  968. cwd: App.info().path.cwd,
  969. root: App.info().path.root,
  970. },
  971. time: {
  972. created: Date.now(),
  973. },
  974. role: "assistant",
  975. tokens: {
  976. input: 0,
  977. output: 0,
  978. reasoning: 0,
  979. cache: { read: 0, write: 0 },
  980. },
  981. modelID: "",
  982. providerID: "",
  983. }
  984. await updateMessage(msg)
  985. const part: MessageV2.Part = {
  986. type: "tool",
  987. id: Identifier.ascending("part"),
  988. messageID: msg.id,
  989. sessionID: input.sessionID,
  990. tool: "bash",
  991. callID: ulid(),
  992. state: {
  993. status: "running",
  994. time: {
  995. start: Date.now(),
  996. },
  997. input: {
  998. command: input.command,
  999. },
  1000. },
  1001. }
  1002. await updatePart(part)
  1003. const app = App.info()
  1004. const shell = process.env["SHELL"] ?? "bash"
  1005. const shellName = path.basename(shell)
  1006. const scripts: Record<string, string> = {
  1007. nu: input.command,
  1008. fish: `eval "${input.command}"`,
  1009. }
  1010. const script =
  1011. scripts[shellName] ??
  1012. `[[ -f ~/.zshenv ]] && source ~/.zshenv >/dev/null 2>&1 || true
  1013. [[ -f "\${ZDOTDIR:-$HOME}/.zshrc" ]] && source "\${ZDOTDIR:-$HOME}/.zshrc" >/dev/null 2>&1 || true
  1014. [[ -f ~/.bashrc ]] && source ~/.bashrc >/dev/null 2>&1 || true
  1015. eval "${input.command}"`
  1016. const isFishOrNu = shellName === "fish" || shellName === "nu"
  1017. const args = isFishOrNu ? ["-c", script] : ["-c", "-l", script]
  1018. const proc = spawn(shell, args, {
  1019. cwd: app.path.cwd,
  1020. signal: abort.signal,
  1021. stdio: ["ignore", "pipe", "pipe"],
  1022. env: {
  1023. ...process.env,
  1024. TERM: "dumb",
  1025. },
  1026. })
  1027. let output = ""
  1028. proc.stdout?.on("data", (chunk) => {
  1029. output += chunk.toString()
  1030. if (part.state.status === "running") {
  1031. part.state.metadata = {
  1032. output: output,
  1033. description: "",
  1034. }
  1035. updatePart(part)
  1036. }
  1037. })
  1038. proc.stderr?.on("data", (chunk) => {
  1039. output += chunk.toString()
  1040. if (part.state.status === "running") {
  1041. part.state.metadata = {
  1042. output: output,
  1043. description: "",
  1044. }
  1045. updatePart(part)
  1046. }
  1047. })
  1048. await new Promise<void>((resolve) => {
  1049. proc.on("close", () => {
  1050. resolve()
  1051. })
  1052. })
  1053. msg.time.completed = Date.now()
  1054. await updateMessage(msg)
  1055. if (part.state.status === "running") {
  1056. part.state = {
  1057. status: "completed",
  1058. time: {
  1059. ...part.state.time,
  1060. end: Date.now(),
  1061. },
  1062. input: part.state.input,
  1063. title: "",
  1064. metadata: {
  1065. output,
  1066. description: "",
  1067. },
  1068. output,
  1069. }
  1070. await updatePart(part)
  1071. }
  1072. return { info: msg, parts: [part] }
  1073. }
  1074. function createProcessor(assistantMsg: MessageV2.Assistant, model: ModelsDev.Model) {
  1075. const toolcalls: Record<string, MessageV2.ToolPart> = {}
  1076. let snapshot: string | undefined
  1077. let shouldStop = false
  1078. return {
  1079. partFromToolCall(toolCallID: string) {
  1080. return toolcalls[toolCallID]
  1081. },
  1082. getShouldStop() {
  1083. return shouldStop
  1084. },
  1085. async process(stream: StreamTextResult<Record<string, AITool>, never>) {
  1086. try {
  1087. let currentText: MessageV2.TextPart | undefined
  1088. let reasoningMap: Record<string, MessageV2.ReasoningPart> = {}
  1089. for await (const value of stream.fullStream) {
  1090. log.info("part", {
  1091. type: value.type,
  1092. })
  1093. switch (value.type) {
  1094. case "start":
  1095. break
  1096. case "reasoning-start":
  1097. if (value.id in reasoningMap) {
  1098. continue
  1099. }
  1100. reasoningMap[value.id] = {
  1101. id: Identifier.ascending("part"),
  1102. messageID: assistantMsg.id,
  1103. sessionID: assistantMsg.sessionID,
  1104. type: "reasoning",
  1105. text: "",
  1106. time: {
  1107. start: Date.now(),
  1108. },
  1109. }
  1110. break
  1111. case "reasoning-delta":
  1112. if (value.id in reasoningMap) {
  1113. const part = reasoningMap[value.id]
  1114. part.text += value.text
  1115. if (part.text) await updatePart(part)
  1116. }
  1117. break
  1118. case "reasoning-end":
  1119. if (value.id in reasoningMap) {
  1120. const part = reasoningMap[value.id]
  1121. part.text = part.text.trimEnd()
  1122. part.metadata = value.providerMetadata
  1123. part.time = {
  1124. ...part.time,
  1125. end: Date.now(),
  1126. }
  1127. await updatePart(part)
  1128. delete reasoningMap[value.id]
  1129. }
  1130. break
  1131. case "tool-input-start":
  1132. const part = await updatePart({
  1133. id: toolcalls[value.id]?.id ?? Identifier.ascending("part"),
  1134. messageID: assistantMsg.id,
  1135. sessionID: assistantMsg.sessionID,
  1136. type: "tool",
  1137. tool: value.toolName,
  1138. callID: value.id,
  1139. state: {
  1140. status: "pending",
  1141. },
  1142. })
  1143. toolcalls[value.id] = part as MessageV2.ToolPart
  1144. break
  1145. case "tool-input-delta":
  1146. break
  1147. case "tool-input-end":
  1148. break
  1149. case "tool-call": {
  1150. const match = toolcalls[value.toolCallId]
  1151. if (match) {
  1152. const part = await updatePart({
  1153. ...match,
  1154. tool: value.toolName,
  1155. state: {
  1156. status: "running",
  1157. input: value.input,
  1158. time: {
  1159. start: Date.now(),
  1160. },
  1161. },
  1162. })
  1163. toolcalls[value.toolCallId] = part as MessageV2.ToolPart
  1164. }
  1165. break
  1166. }
  1167. case "tool-result": {
  1168. const match = toolcalls[value.toolCallId]
  1169. if (match && match.state.status === "running") {
  1170. await updatePart({
  1171. ...match,
  1172. state: {
  1173. status: "completed",
  1174. input: value.input,
  1175. output: value.output.output,
  1176. metadata: value.output.metadata,
  1177. title: value.output.title,
  1178. time: {
  1179. start: match.state.time.start,
  1180. end: Date.now(),
  1181. },
  1182. },
  1183. })
  1184. delete toolcalls[value.toolCallId]
  1185. }
  1186. break
  1187. }
  1188. case "tool-error": {
  1189. const match = toolcalls[value.toolCallId]
  1190. if (match && match.state.status === "running") {
  1191. if (value.error instanceof Permission.RejectedError) {
  1192. shouldStop = true
  1193. }
  1194. await updatePart({
  1195. ...match,
  1196. state: {
  1197. status: "error",
  1198. input: value.input,
  1199. error: (value.error as any).toString(),
  1200. metadata: value.error instanceof Permission.RejectedError ? value.error.metadata : undefined,
  1201. time: {
  1202. start: match.state.time.start,
  1203. end: Date.now(),
  1204. },
  1205. },
  1206. })
  1207. delete toolcalls[value.toolCallId]
  1208. }
  1209. break
  1210. }
  1211. case "error":
  1212. throw value.error
  1213. case "start-step":
  1214. await updatePart({
  1215. id: Identifier.ascending("part"),
  1216. messageID: assistantMsg.id,
  1217. sessionID: assistantMsg.sessionID,
  1218. type: "step-start",
  1219. })
  1220. snapshot = await Snapshot.track()
  1221. break
  1222. case "finish-step":
  1223. const usage = getUsage(model, value.usage, value.providerMetadata)
  1224. assistantMsg.cost += usage.cost
  1225. assistantMsg.tokens = usage.tokens
  1226. await updatePart({
  1227. id: Identifier.ascending("part"),
  1228. messageID: assistantMsg.id,
  1229. sessionID: assistantMsg.sessionID,
  1230. type: "step-finish",
  1231. tokens: usage.tokens,
  1232. cost: usage.cost,
  1233. })
  1234. await updateMessage(assistantMsg)
  1235. if (snapshot) {
  1236. const patch = await Snapshot.patch(snapshot)
  1237. if (patch.files.length) {
  1238. await updatePart({
  1239. id: Identifier.ascending("part"),
  1240. messageID: assistantMsg.id,
  1241. sessionID: assistantMsg.sessionID,
  1242. type: "patch",
  1243. hash: patch.hash,
  1244. files: patch.files,
  1245. })
  1246. }
  1247. snapshot = undefined
  1248. }
  1249. break
  1250. case "text-start":
  1251. currentText = {
  1252. id: Identifier.ascending("part"),
  1253. messageID: assistantMsg.id,
  1254. sessionID: assistantMsg.sessionID,
  1255. type: "text",
  1256. text: "",
  1257. time: {
  1258. start: Date.now(),
  1259. },
  1260. }
  1261. break
  1262. case "text-delta":
  1263. if (currentText) {
  1264. currentText.text += value.text
  1265. if (currentText.text) await updatePart(currentText)
  1266. }
  1267. break
  1268. case "text-end":
  1269. if (currentText) {
  1270. currentText.text = currentText.text.trimEnd()
  1271. currentText.time = {
  1272. start: Date.now(),
  1273. end: Date.now(),
  1274. }
  1275. await updatePart(currentText)
  1276. }
  1277. currentText = undefined
  1278. break
  1279. case "finish":
  1280. assistantMsg.time.completed = Date.now()
  1281. await updateMessage(assistantMsg)
  1282. break
  1283. default:
  1284. log.info("unhandled", {
  1285. ...value,
  1286. })
  1287. continue
  1288. }
  1289. }
  1290. } catch (e) {
  1291. log.error("", {
  1292. error: e,
  1293. })
  1294. switch (true) {
  1295. case e instanceof DOMException && e.name === "AbortError":
  1296. assistantMsg.error = new MessageV2.AbortedError(
  1297. { message: e.message },
  1298. {
  1299. cause: e,
  1300. },
  1301. ).toObject()
  1302. break
  1303. case MessageV2.OutputLengthError.isInstance(e):
  1304. assistantMsg.error = e
  1305. break
  1306. case LoadAPIKeyError.isInstance(e):
  1307. assistantMsg.error = new MessageV2.AuthError(
  1308. {
  1309. providerID: model.id,
  1310. message: e.message,
  1311. },
  1312. { cause: e },
  1313. ).toObject()
  1314. break
  1315. case e instanceof Error:
  1316. assistantMsg.error = new NamedError.Unknown({ message: e.toString() }, { cause: e }).toObject()
  1317. break
  1318. default:
  1319. assistantMsg.error = new NamedError.Unknown({ message: JSON.stringify(e) }, { cause: e })
  1320. }
  1321. Bus.publish(Event.Error, {
  1322. sessionID: assistantMsg.sessionID,
  1323. error: assistantMsg.error,
  1324. })
  1325. }
  1326. const p = await getParts(assistantMsg.sessionID, assistantMsg.id)
  1327. for (const part of p) {
  1328. if (part.type === "tool" && part.state.status !== "completed" && part.state.status !== "error") {
  1329. updatePart({
  1330. ...part,
  1331. state: {
  1332. status: "error",
  1333. error: "Tool execution aborted",
  1334. time: {
  1335. start: Date.now(),
  1336. end: Date.now(),
  1337. },
  1338. input: {},
  1339. },
  1340. })
  1341. }
  1342. }
  1343. assistantMsg.time.completed = Date.now()
  1344. await updateMessage(assistantMsg)
  1345. return { info: assistantMsg, parts: p }
  1346. },
  1347. }
  1348. }
  1349. export const RevertInput = z.object({
  1350. sessionID: Identifier.schema("session"),
  1351. messageID: Identifier.schema("message"),
  1352. partID: Identifier.schema("part").optional(),
  1353. })
  1354. export type RevertInput = z.infer<typeof RevertInput>
  1355. export async function revert(input: RevertInput) {
  1356. const all = await messages(input.sessionID)
  1357. let lastUser: MessageV2.User | undefined
  1358. const session = await get(input.sessionID)
  1359. let revert: Info["revert"]
  1360. const patches: Snapshot.Patch[] = []
  1361. for (const msg of all) {
  1362. if (msg.info.role === "user") lastUser = msg.info
  1363. const remaining = []
  1364. for (const part of msg.parts) {
  1365. if (revert) {
  1366. if (part.type === "patch") {
  1367. patches.push(part)
  1368. }
  1369. continue
  1370. }
  1371. if (!revert) {
  1372. if ((msg.info.id === input.messageID && !input.partID) || part.id === input.partID) {
  1373. // if no useful parts left in message, same as reverting whole message
  1374. const partID = remaining.some((item) => ["text", "tool"].includes(item.type)) ? input.partID : undefined
  1375. revert = {
  1376. messageID: !partID && lastUser ? lastUser.id : msg.info.id,
  1377. partID,
  1378. }
  1379. }
  1380. remaining.push(part)
  1381. }
  1382. }
  1383. }
  1384. if (revert) {
  1385. const session = await get(input.sessionID)
  1386. revert.snapshot = session.revert?.snapshot ?? (await Snapshot.track())
  1387. await Snapshot.revert(patches)
  1388. if (revert.snapshot) revert.diff = await Snapshot.diff(revert.snapshot)
  1389. return update(input.sessionID, (draft) => {
  1390. draft.revert = revert
  1391. })
  1392. }
  1393. return session
  1394. }
  1395. export async function unrevert(input: { sessionID: string }) {
  1396. log.info("unreverting", input)
  1397. const session = await get(input.sessionID)
  1398. if (!session.revert) return session
  1399. if (session.revert.snapshot) await Snapshot.restore(session.revert.snapshot)
  1400. const next = await update(input.sessionID, (draft) => {
  1401. draft.revert = undefined
  1402. })
  1403. return next
  1404. }
  1405. export async function summarize(input: { sessionID: string; providerID: string; modelID: string }) {
  1406. using abort = lock(input.sessionID)
  1407. const msgs = await messages(input.sessionID)
  1408. const lastSummary = msgs.findLast((msg) => msg.info.role === "assistant" && msg.info.summary === true)
  1409. const filtered = msgs.filter((msg) => !lastSummary || msg.info.id >= lastSummary.info.id)
  1410. const model = await Provider.getModel(input.providerID, input.modelID)
  1411. const app = App.info()
  1412. const system = [
  1413. ...SystemPrompt.summarize(input.providerID),
  1414. ...(await SystemPrompt.environment()),
  1415. ...(await SystemPrompt.custom()),
  1416. ]
  1417. const next: MessageV2.Info = {
  1418. id: Identifier.ascending("message"),
  1419. role: "assistant",
  1420. sessionID: input.sessionID,
  1421. system,
  1422. mode: "build",
  1423. path: {
  1424. cwd: app.path.cwd,
  1425. root: app.path.root,
  1426. },
  1427. summary: true,
  1428. cost: 0,
  1429. modelID: input.modelID,
  1430. providerID: input.providerID,
  1431. tokens: {
  1432. input: 0,
  1433. output: 0,
  1434. reasoning: 0,
  1435. cache: { read: 0, write: 0 },
  1436. },
  1437. time: {
  1438. created: Date.now(),
  1439. },
  1440. }
  1441. await updateMessage(next)
  1442. const processor = createProcessor(next, model.info)
  1443. const stream = streamText({
  1444. maxRetries: 10,
  1445. abortSignal: abort.signal,
  1446. model: model.language,
  1447. messages: [
  1448. ...system.map(
  1449. (x): ModelMessage => ({
  1450. role: "system",
  1451. content: x,
  1452. }),
  1453. ),
  1454. ...MessageV2.toModelMessage(filtered),
  1455. {
  1456. role: "user",
  1457. content: [
  1458. {
  1459. type: "text",
  1460. 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.",
  1461. },
  1462. ],
  1463. },
  1464. ],
  1465. })
  1466. const result = await processor.process(stream)
  1467. return result
  1468. }
  1469. function isLocked(sessionID: string) {
  1470. return state().pending.has(sessionID)
  1471. }
  1472. function lock(sessionID: string) {
  1473. log.info("locking", { sessionID })
  1474. if (state().pending.has(sessionID)) throw new BusyError(sessionID)
  1475. const controller = new AbortController()
  1476. state().pending.set(sessionID, controller)
  1477. return {
  1478. signal: controller.signal,
  1479. async [Symbol.dispose]() {
  1480. log.info("unlocking", { sessionID })
  1481. state().pending.delete(sessionID)
  1482. const isAutoCompacting = state().autoCompacting.get(sessionID) ?? false
  1483. if (isAutoCompacting) {
  1484. state().autoCompacting.delete(sessionID)
  1485. return
  1486. }
  1487. const session = await get(sessionID)
  1488. if (session.parentID) return
  1489. Bus.publish(Event.Idle, {
  1490. sessionID,
  1491. })
  1492. },
  1493. }
  1494. }
  1495. function getUsage(model: ModelsDev.Model, usage: LanguageModelUsage, metadata?: ProviderMetadata) {
  1496. const tokens = {
  1497. input: usage.inputTokens ?? 0,
  1498. output: usage.outputTokens ?? 0,
  1499. reasoning: 0,
  1500. cache: {
  1501. write: (metadata?.["anthropic"]?.["cacheCreationInputTokens"] ??
  1502. // @ts-expect-error
  1503. metadata?.["bedrock"]?.["usage"]?.["cacheWriteInputTokens"] ??
  1504. 0) as number,
  1505. read: usage.cachedInputTokens ?? 0,
  1506. },
  1507. }
  1508. return {
  1509. cost: new Decimal(0)
  1510. .add(new Decimal(tokens.input).mul(model.cost?.input ?? 0).div(1_000_000))
  1511. .add(new Decimal(tokens.output).mul(model.cost?.output ?? 0).div(1_000_000))
  1512. .add(new Decimal(tokens.cache.read).mul(model.cost?.cache_read ?? 0).div(1_000_000))
  1513. .add(new Decimal(tokens.cache.write).mul(model.cost?.cache_write ?? 0).div(1_000_000))
  1514. .toNumber(),
  1515. tokens,
  1516. }
  1517. }
  1518. export class BusyError extends Error {
  1519. constructor(public readonly sessionID: string) {
  1520. super(`Session ${sessionID} is busy`)
  1521. }
  1522. }
  1523. export async function initialize(input: {
  1524. sessionID: string
  1525. modelID: string
  1526. providerID: string
  1527. messageID: string
  1528. }) {
  1529. const app = App.info()
  1530. await Session.chat({
  1531. sessionID: input.sessionID,
  1532. messageID: input.messageID,
  1533. providerID: input.providerID,
  1534. modelID: input.modelID,
  1535. parts: [
  1536. {
  1537. id: Identifier.ascending("part"),
  1538. type: "text",
  1539. text: PROMPT_INITIALIZE.replace("${path}", app.path.root),
  1540. },
  1541. ],
  1542. })
  1543. await App.initialize()
  1544. }
  1545. }