index.ts 51 KB

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