index.ts 41 KB

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