2
0

index.ts 45 KB

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