index.ts 40 KB

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