index.ts 46 KB

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