index.ts 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113
  1. import path from "node:path"
  2. import { App } from "../app/app"
  3. import { Identifier } from "../id/id"
  4. import { Storage } from "../storage/storage"
  5. import { Log } from "../util/log"
  6. import {
  7. generateText,
  8. LoadAPIKeyError,
  9. convertToCoreMessages,
  10. streamText,
  11. tool,
  12. type Tool as AITool,
  13. type LanguageModelUsage,
  14. type CoreMessage,
  15. type UIMessage,
  16. type ProviderMetadata,
  17. wrapLanguageModel,
  18. type Attachment,
  19. } from "ai"
  20. import { z, ZodSchema } from "zod"
  21. import { Decimal } from "decimal.js"
  22. import PROMPT_INITIALIZE from "../session/prompt/initialize.txt"
  23. import { Share } from "../share/share"
  24. import { Message } from "./message"
  25. import { Bus } from "../bus"
  26. import { Provider } from "../provider/provider"
  27. import { MCP } from "../mcp"
  28. import { NamedError } from "../util/error"
  29. import type { Tool } from "../tool/tool"
  30. import { SystemPrompt } from "./system"
  31. import { Flag } from "../flag/flag"
  32. import type { ModelsDev } from "../provider/models"
  33. import { Installation } from "../installation"
  34. import { Config } from "../config/config"
  35. import { ProviderTransform } from "../provider/transform"
  36. import { Snapshot } from "../snapshot"
  37. export namespace Session {
  38. const log = Log.create({ service: "session" })
  39. export const Info = z
  40. .object({
  41. id: Identifier.schema("session"),
  42. parentID: Identifier.schema("session").optional(),
  43. share: z
  44. .object({
  45. url: z.string(),
  46. })
  47. .optional(),
  48. title: z.string(),
  49. version: z.string(),
  50. time: z.object({
  51. created: z.number(),
  52. updated: z.number(),
  53. }),
  54. revert: z
  55. .object({
  56. messageID: z.string(),
  57. part: z.number(),
  58. snapshot: z.string().optional(),
  59. })
  60. .optional(),
  61. })
  62. .openapi({
  63. ref: "Session",
  64. })
  65. export type Info = z.output<typeof Info>
  66. export const ShareInfo = z
  67. .object({
  68. secret: z.string(),
  69. url: z.string(),
  70. })
  71. .openapi({
  72. ref: "SessionShare",
  73. })
  74. export type ShareInfo = z.output<typeof ShareInfo>
  75. export const Event = {
  76. Updated: Bus.event(
  77. "session.updated",
  78. z.object({
  79. info: Info,
  80. }),
  81. ),
  82. Deleted: Bus.event(
  83. "session.deleted",
  84. z.object({
  85. info: Info,
  86. }),
  87. ),
  88. Idle: Bus.event(
  89. "session.idle",
  90. z.object({
  91. sessionID: z.string(),
  92. }),
  93. ),
  94. Error: Bus.event(
  95. "session.error",
  96. z.object({
  97. error: Message.Info.shape.metadata.shape.error,
  98. }),
  99. ),
  100. }
  101. const state = App.state(
  102. "session",
  103. () => {
  104. const sessions = new Map<string, Info>()
  105. const messages = new Map<string, Message.Info[]>()
  106. const pending = new Map<string, AbortController>()
  107. return {
  108. sessions,
  109. messages,
  110. pending,
  111. }
  112. },
  113. async (state) => {
  114. for (const [_, controller] of state.pending) {
  115. controller.abort()
  116. }
  117. },
  118. )
  119. export async function create(parentID?: string) {
  120. const result: Info = {
  121. id: Identifier.descending("session"),
  122. version: Installation.VERSION,
  123. parentID,
  124. title:
  125. (parentID ? "Child session - " : "New Session - ") +
  126. new Date().toISOString(),
  127. time: {
  128. created: Date.now(),
  129. updated: Date.now(),
  130. },
  131. }
  132. log.info("created", result)
  133. state().sessions.set(result.id, result)
  134. await Storage.writeJSON("session/info/" + result.id, result)
  135. const cfg = await Config.get()
  136. if (!result.parentID && (Flag.OPENCODE_AUTO_SHARE || cfg.autoshare))
  137. share(result.id).then((share) => {
  138. update(result.id, (draft) => {
  139. draft.share = share
  140. })
  141. })
  142. Bus.publish(Event.Updated, {
  143. info: result,
  144. })
  145. return result
  146. }
  147. export async function get(id: string) {
  148. const result = state().sessions.get(id)
  149. if (result) {
  150. return result
  151. }
  152. const read = await Storage.readJSON<Info>("session/info/" + id)
  153. state().sessions.set(id, read)
  154. return read as Info
  155. }
  156. export async function getShare(id: string) {
  157. return Storage.readJSON<ShareInfo>("session/share/" + id)
  158. }
  159. export async function share(id: string) {
  160. const session = await get(id)
  161. if (session.share) return session.share
  162. const share = await Share.create(id)
  163. await update(id, (draft) => {
  164. draft.share = {
  165. url: share.url,
  166. }
  167. })
  168. await Storage.writeJSON<ShareInfo>("session/share/" + id, share)
  169. await Share.sync("session/info/" + id, session)
  170. for (const msg of await messages(id)) {
  171. await Share.sync("session/message/" + id + "/" + msg.id, msg)
  172. }
  173. return share
  174. }
  175. export async function unshare(id: string) {
  176. const share = await getShare(id)
  177. if (!share) return
  178. await Storage.remove("session/share/" + id)
  179. await update(id, (draft) => {
  180. draft.share = undefined
  181. })
  182. await Share.remove(id, share.secret)
  183. }
  184. export async function update(id: string, editor: (session: Info) => void) {
  185. const { sessions } = state()
  186. const session = await get(id)
  187. if (!session) return
  188. editor(session)
  189. session.time.updated = Date.now()
  190. sessions.set(id, session)
  191. await Storage.writeJSON("session/info/" + id, session)
  192. Bus.publish(Event.Updated, {
  193. info: session,
  194. })
  195. return session
  196. }
  197. export async function messages(sessionID: string) {
  198. const result = [] as Message.Info[]
  199. const list = Storage.list("session/message/" + sessionID)
  200. for await (const p of list) {
  201. const read = await Storage.readJSON<Message.Info>(p)
  202. result.push(read)
  203. }
  204. result.sort((a, b) => (a.id > b.id ? 1 : -1))
  205. return result
  206. }
  207. export async function getMessage(sessionID: string, messageID: string) {
  208. return Storage.readJSON<Message.Info>(
  209. "session/message/" + sessionID + "/" + messageID,
  210. )
  211. }
  212. export async function* list() {
  213. for await (const item of Storage.list("session/info")) {
  214. const sessionID = path.basename(item, ".json")
  215. yield get(sessionID)
  216. }
  217. }
  218. export async function children(parentID: string) {
  219. const result = [] as Session.Info[]
  220. for await (const item of Storage.list("session/info")) {
  221. const sessionID = path.basename(item, ".json")
  222. const session = await get(sessionID)
  223. if (session.parentID !== parentID) continue
  224. result.push(session)
  225. }
  226. return result
  227. }
  228. export function abort(sessionID: string) {
  229. const controller = state().pending.get(sessionID)
  230. if (!controller) return false
  231. controller.abort()
  232. state().pending.delete(sessionID)
  233. return true
  234. }
  235. export async function remove(sessionID: string, emitEvent = true) {
  236. try {
  237. abort(sessionID)
  238. const session = await get(sessionID)
  239. for (const child of await children(sessionID)) {
  240. await remove(child.id, false)
  241. }
  242. await unshare(sessionID).catch(() => {})
  243. await Storage.remove(`session/info/${sessionID}`).catch(() => {})
  244. await Storage.removeDir(`session/message/${sessionID}/`).catch(() => {})
  245. state().sessions.delete(sessionID)
  246. state().messages.delete(sessionID)
  247. if (emitEvent) {
  248. Bus.publish(Event.Deleted, {
  249. info: session,
  250. })
  251. }
  252. } catch (e) {
  253. log.error(e)
  254. }
  255. }
  256. async function updateMessage(msg: Message.Info) {
  257. await Storage.writeJSON(
  258. "session/message/" + msg.metadata.sessionID + "/" + msg.id,
  259. msg,
  260. )
  261. Bus.publish(Message.Event.Updated, {
  262. info: msg,
  263. })
  264. }
  265. export async function chat(input: {
  266. sessionID: string
  267. providerID: string
  268. modelID: string
  269. parts: Message.MessagePart[]
  270. system?: string[]
  271. tools?: Tool.Info[]
  272. }) {
  273. const l = log.clone().tag("session", input.sessionID)
  274. l.info("chatting")
  275. const model = await Provider.getModel(input.providerID, input.modelID)
  276. let msgs = await messages(input.sessionID)
  277. const session = await get(input.sessionID)
  278. if (session.revert) {
  279. const trimmed = []
  280. for (const msg of msgs) {
  281. if (
  282. msg.id > session.revert.messageID ||
  283. (msg.id === session.revert.messageID && session.revert.part === 0)
  284. ) {
  285. await Storage.remove(
  286. "session/message/" + input.sessionID + "/" + msg.id,
  287. )
  288. await Bus.publish(Message.Event.Removed, {
  289. sessionID: input.sessionID,
  290. messageID: msg.id,
  291. })
  292. continue
  293. }
  294. if (msg.id === session.revert.messageID) {
  295. if (session.revert.part === 0) break
  296. msg.parts = msg.parts.slice(0, session.revert.part)
  297. }
  298. trimmed.push(msg)
  299. }
  300. msgs = trimmed
  301. await update(input.sessionID, (draft) => {
  302. draft.revert = undefined
  303. })
  304. }
  305. const previous = msgs.at(-1)
  306. // auto summarize if too long
  307. if (previous?.metadata.assistant) {
  308. const tokens =
  309. previous.metadata.assistant.tokens.input +
  310. previous.metadata.assistant.tokens.cache.read +
  311. previous.metadata.assistant.tokens.cache.write +
  312. previous.metadata.assistant.tokens.output
  313. if (
  314. model.info.limit.context &&
  315. tokens >
  316. Math.max(
  317. (model.info.limit.context - (model.info.limit.output ?? 0)) * 0.9,
  318. 0,
  319. )
  320. ) {
  321. await summarize({
  322. sessionID: input.sessionID,
  323. providerID: input.providerID,
  324. modelID: input.modelID,
  325. })
  326. return chat(input)
  327. }
  328. }
  329. using abort = lock(input.sessionID)
  330. const lastSummary = msgs.findLast(
  331. (msg) => msg.metadata.assistant?.summary === true,
  332. )
  333. if (lastSummary) msgs = msgs.filter((msg) => msg.id >= lastSummary.id)
  334. const app = App.info()
  335. input.parts = await Promise.all(
  336. input.parts.map(async (part) => {
  337. if (part.type === "file") {
  338. const url = new URL(part.url)
  339. switch (url.protocol) {
  340. case "file:":
  341. let content = await Bun.file(
  342. path.join(app.path.cwd, url.pathname),
  343. ).text()
  344. const range = {
  345. start: url.searchParams.get("start"),
  346. end: url.searchParams.get("end"),
  347. }
  348. if (range.start != null) {
  349. const lines = content.split("\n")
  350. const start = parseInt(range.start)
  351. const end = range.end ? parseInt(range.end) : lines.length
  352. content = lines.slice(start, end).join("\n")
  353. }
  354. return {
  355. type: "file",
  356. url: "data:text/plain;base64," + btoa(content),
  357. mediaType: "text/plain",
  358. filename: part.filename,
  359. }
  360. }
  361. }
  362. return part
  363. }),
  364. )
  365. if (msgs.length === 0 && !session.parentID) {
  366. generateText({
  367. maxTokens: input.providerID === "google" ? 1024 : 20,
  368. providerOptions: model.info.options,
  369. messages: [
  370. ...SystemPrompt.title(input.providerID).map(
  371. (x): CoreMessage => ({
  372. role: "system",
  373. content: x,
  374. }),
  375. ),
  376. ...convertToCoreMessages([
  377. {
  378. role: "user",
  379. content: "",
  380. ...toParts(input.parts),
  381. },
  382. ]),
  383. ],
  384. model: model.language,
  385. })
  386. .then((result) => {
  387. if (result.text)
  388. return Session.update(input.sessionID, (draft) => {
  389. draft.title = result.text
  390. })
  391. })
  392. .catch(() => {})
  393. }
  394. const snapshot = await Snapshot.create(input.sessionID)
  395. const msg: Message.Info = {
  396. role: "user",
  397. id: Identifier.ascending("message"),
  398. parts: input.parts,
  399. metadata: {
  400. time: {
  401. created: Date.now(),
  402. },
  403. sessionID: input.sessionID,
  404. tool: {},
  405. snapshot,
  406. },
  407. }
  408. await updateMessage(msg)
  409. msgs.push(msg)
  410. const system = input.system ?? SystemPrompt.provider(input.providerID)
  411. system.push(...(await SystemPrompt.environment()))
  412. system.push(...(await SystemPrompt.custom()))
  413. const next: Message.Info = {
  414. id: Identifier.ascending("message"),
  415. role: "assistant",
  416. parts: [],
  417. metadata: {
  418. snapshot,
  419. assistant: {
  420. system,
  421. path: {
  422. cwd: app.path.cwd,
  423. root: app.path.root,
  424. },
  425. cost: 0,
  426. tokens: {
  427. input: 0,
  428. output: 0,
  429. reasoning: 0,
  430. cache: { read: 0, write: 0 },
  431. },
  432. modelID: input.modelID,
  433. providerID: input.providerID,
  434. },
  435. time: {
  436. created: Date.now(),
  437. },
  438. sessionID: input.sessionID,
  439. tool: {},
  440. },
  441. }
  442. await updateMessage(next)
  443. const tools: Record<string, AITool> = {}
  444. for (const item of await Provider.tools(input.providerID)) {
  445. tools[item.id.replaceAll(".", "_")] = tool({
  446. id: item.id as any,
  447. description: item.description,
  448. parameters: item.parameters as ZodSchema,
  449. async execute(args, opts) {
  450. const start = Date.now()
  451. try {
  452. const result = await item.execute(args, {
  453. sessionID: input.sessionID,
  454. abort: abort.signal,
  455. messageID: next.id,
  456. metadata: async (val) => {
  457. next.metadata.tool[opts.toolCallId] = {
  458. ...val,
  459. time: {
  460. start: 0,
  461. end: 0,
  462. },
  463. }
  464. await updateMessage(next)
  465. },
  466. })
  467. next.metadata!.tool![opts.toolCallId] = {
  468. ...result.metadata,
  469. snapshot: await Snapshot.create(input.sessionID),
  470. time: {
  471. start,
  472. end: Date.now(),
  473. },
  474. }
  475. await updateMessage(next)
  476. return result.output
  477. } catch (e: any) {
  478. next.metadata!.tool![opts.toolCallId] = {
  479. error: true,
  480. message: e.toString(),
  481. title: e.toString(),
  482. snapshot: await Snapshot.create(input.sessionID),
  483. time: {
  484. start,
  485. end: Date.now(),
  486. },
  487. }
  488. await updateMessage(next)
  489. return e.toString()
  490. }
  491. },
  492. })
  493. }
  494. for (const [key, item] of Object.entries(await MCP.tools())) {
  495. const execute = item.execute
  496. if (!execute) continue
  497. item.execute = async (args, opts) => {
  498. const start = Date.now()
  499. try {
  500. const result = await execute(args, opts)
  501. next.metadata!.tool![opts.toolCallId] = {
  502. ...result.metadata,
  503. snapshot: await Snapshot.create(input.sessionID),
  504. time: {
  505. start,
  506. end: Date.now(),
  507. },
  508. }
  509. await updateMessage(next)
  510. return result.content
  511. .filter((x: any) => x.type === "text")
  512. .map((x: any) => x.text)
  513. .join("\n\n")
  514. } catch (e: any) {
  515. next.metadata!.tool![opts.toolCallId] = {
  516. error: true,
  517. message: e.toString(),
  518. snapshot: await Snapshot.create(input.sessionID),
  519. title: "mcp",
  520. time: {
  521. start,
  522. end: Date.now(),
  523. },
  524. }
  525. await updateMessage(next)
  526. return e.toString()
  527. }
  528. }
  529. tools[key] = item
  530. }
  531. let text: Message.TextPart | undefined
  532. const result = streamText({
  533. onStepFinish: async (step) => {
  534. log.info("step finish", { finishReason: step.finishReason })
  535. const assistant = next.metadata!.assistant!
  536. const usage = getUsage(model.info, step.usage, step.providerMetadata)
  537. assistant.cost += usage.cost
  538. assistant.tokens = usage.tokens
  539. await updateMessage(next)
  540. if (text) {
  541. Bus.publish(Message.Event.PartUpdated, {
  542. part: text,
  543. messageID: next.id,
  544. sessionID: next.metadata.sessionID,
  545. })
  546. }
  547. text = undefined
  548. },
  549. onError(err) {
  550. log.error("callback error", err)
  551. switch (true) {
  552. case LoadAPIKeyError.isInstance(err.error):
  553. next.metadata.error = new Provider.AuthError(
  554. {
  555. providerID: input.providerID,
  556. message: err.error.message,
  557. },
  558. { cause: err.error },
  559. ).toObject()
  560. break
  561. case err.error instanceof Error:
  562. next.metadata.error = new NamedError.Unknown(
  563. { message: err.error.toString() },
  564. { cause: err.error },
  565. ).toObject()
  566. break
  567. default:
  568. next.metadata.error = new NamedError.Unknown(
  569. { message: JSON.stringify(err.error) },
  570. { cause: err.error },
  571. )
  572. }
  573. Bus.publish(Event.Error, {
  574. error: next.metadata.error,
  575. })
  576. },
  577. // async prepareStep(step) {
  578. // next.parts.push({
  579. // type: "step-start",
  580. // })
  581. // await updateMessage(next)
  582. // return step
  583. // },
  584. toolCallStreaming: true,
  585. maxRetries: 10,
  586. maxTokens: Math.max(0, model.info.limit.output) || undefined,
  587. abortSignal: abort.signal,
  588. maxSteps: 1000,
  589. providerOptions: model.info.options,
  590. messages: [
  591. ...system.map(
  592. (x): CoreMessage => ({
  593. role: "system",
  594. content: x,
  595. }),
  596. ),
  597. ...convertToCoreMessages(
  598. msgs.map(toUIMessage).filter((x) => x.parts.length > 0),
  599. ),
  600. ],
  601. temperature: model.info.temperature ? 0 : undefined,
  602. tools: model.info.tool_call === false ? undefined : tools,
  603. model: wrapLanguageModel({
  604. model: model.language,
  605. middleware: [
  606. {
  607. async transformParams(args) {
  608. if (args.type === "stream") {
  609. args.params.prompt = ProviderTransform.message(
  610. args.params.prompt,
  611. input.providerID,
  612. input.modelID,
  613. )
  614. }
  615. return args.params
  616. },
  617. },
  618. ],
  619. }),
  620. })
  621. try {
  622. for await (const value of result.fullStream) {
  623. l.info("part", {
  624. type: value.type,
  625. })
  626. switch (value.type) {
  627. case "step-start":
  628. next.parts.push({
  629. type: "step-start",
  630. })
  631. break
  632. case "text-delta":
  633. if (!text) {
  634. text = {
  635. type: "text",
  636. text: value.textDelta,
  637. }
  638. next.parts.push(text)
  639. break
  640. } else text.text += value.textDelta
  641. break
  642. case "tool-call": {
  643. const [match] = next.parts.flatMap((p) =>
  644. p.type === "tool-invocation" &&
  645. p.toolInvocation.toolCallId === value.toolCallId
  646. ? [p]
  647. : [],
  648. )
  649. if (!match) break
  650. match.toolInvocation.args = value.args
  651. match.toolInvocation.state = "call"
  652. Bus.publish(Message.Event.PartUpdated, {
  653. part: match,
  654. messageID: next.id,
  655. sessionID: next.metadata.sessionID,
  656. })
  657. break
  658. }
  659. case "tool-call-streaming-start":
  660. next.parts.push({
  661. type: "tool-invocation",
  662. toolInvocation: {
  663. state: "partial-call",
  664. toolName: value.toolName,
  665. toolCallId: value.toolCallId,
  666. args: {},
  667. },
  668. })
  669. Bus.publish(Message.Event.PartUpdated, {
  670. part: next.parts[next.parts.length - 1],
  671. messageID: next.id,
  672. sessionID: next.metadata.sessionID,
  673. })
  674. break
  675. case "tool-call-delta":
  676. continue
  677. // for some reason ai sdk claims to not send this part but it does
  678. // @ts-expect-error
  679. case "tool-result":
  680. const match = next.parts.find(
  681. (p) =>
  682. p.type === "tool-invocation" &&
  683. // @ts-expect-error
  684. p.toolInvocation.toolCallId === value.toolCallId,
  685. )
  686. if (match && match.type === "tool-invocation") {
  687. match.toolInvocation = {
  688. // @ts-expect-error
  689. args: value.args,
  690. // @ts-expect-error
  691. toolCallId: value.toolCallId,
  692. // @ts-expect-error
  693. toolName: value.toolName,
  694. state: "result",
  695. // @ts-expect-error
  696. result: value.result as string,
  697. }
  698. Bus.publish(Message.Event.PartUpdated, {
  699. part: match,
  700. messageID: next.id,
  701. sessionID: next.metadata.sessionID,
  702. })
  703. }
  704. break
  705. case "finish":
  706. log.info("message finish", {
  707. reason: value.finishReason,
  708. })
  709. const assistant = next.metadata!.assistant!
  710. const usage = getUsage(
  711. model.info,
  712. value.usage,
  713. value.providerMetadata,
  714. )
  715. assistant.cost += usage.cost
  716. await updateMessage(next)
  717. if (value.finishReason === "length")
  718. throw new Message.OutputLengthError({})
  719. break
  720. default:
  721. l.info("unhandled", {
  722. type: value.type,
  723. })
  724. continue
  725. }
  726. await updateMessage(next)
  727. }
  728. } catch (e: any) {
  729. log.error("stream error", {
  730. error: e,
  731. })
  732. switch (true) {
  733. case Message.OutputLengthError.isInstance(e):
  734. next.metadata.error = e
  735. break
  736. case LoadAPIKeyError.isInstance(e):
  737. next.metadata.error = new Provider.AuthError(
  738. {
  739. providerID: input.providerID,
  740. message: e.message,
  741. },
  742. { cause: e },
  743. ).toObject()
  744. break
  745. case e instanceof Error:
  746. next.metadata.error = new NamedError.Unknown(
  747. { message: e.toString() },
  748. { cause: e },
  749. ).toObject()
  750. break
  751. default:
  752. next.metadata.error = new NamedError.Unknown(
  753. { message: JSON.stringify(e) },
  754. { cause: e },
  755. )
  756. }
  757. Bus.publish(Event.Error, {
  758. error: next.metadata.error,
  759. })
  760. }
  761. next.metadata!.time.completed = Date.now()
  762. for (const part of next.parts) {
  763. if (
  764. part.type === "tool-invocation" &&
  765. part.toolInvocation.state !== "result"
  766. ) {
  767. part.toolInvocation = {
  768. ...part.toolInvocation,
  769. state: "result",
  770. result: "request was aborted",
  771. }
  772. }
  773. }
  774. await updateMessage(next)
  775. return next
  776. }
  777. export async function revert(input: {
  778. sessionID: string
  779. messageID: string
  780. part: number
  781. }) {
  782. const message = await getMessage(input.sessionID, input.messageID)
  783. if (!message) return
  784. const part = message.parts[input.part]
  785. if (!part) return
  786. const session = await get(input.sessionID)
  787. const snapshot =
  788. session.revert?.snapshot ?? (await Snapshot.create(input.sessionID))
  789. const old = (() => {
  790. if (message.role === "assistant") {
  791. const lastTool = message.parts.findLast(
  792. (part, index) =>
  793. part.type === "tool-invocation" && index < input.part,
  794. )
  795. if (lastTool && lastTool.type === "tool-invocation")
  796. return message.metadata.tool[lastTool.toolInvocation.toolCallId]
  797. .snapshot
  798. }
  799. return message.metadata.snapshot
  800. })()
  801. if (old) await Snapshot.restore(input.sessionID, old)
  802. await update(input.sessionID, (draft) => {
  803. draft.revert = {
  804. messageID: input.messageID,
  805. part: input.part,
  806. snapshot,
  807. }
  808. })
  809. }
  810. export async function unrevert(sessionID: string) {
  811. const session = await get(sessionID)
  812. if (!session) return
  813. if (!session.revert) return
  814. if (session.revert.snapshot)
  815. await Snapshot.restore(sessionID, session.revert.snapshot)
  816. update(sessionID, (draft) => {
  817. draft.revert = undefined
  818. })
  819. }
  820. export async function summarize(input: {
  821. sessionID: string
  822. providerID: string
  823. modelID: string
  824. }) {
  825. using abort = lock(input.sessionID)
  826. const msgs = await messages(input.sessionID)
  827. const lastSummary = msgs.findLast(
  828. (msg) => msg.metadata.assistant?.summary === true,
  829. )?.id
  830. const filtered = msgs.filter((msg) => !lastSummary || msg.id >= lastSummary)
  831. const model = await Provider.getModel(input.providerID, input.modelID)
  832. const app = App.info()
  833. const system = SystemPrompt.summarize(input.providerID)
  834. const next: Message.Info = {
  835. id: Identifier.ascending("message"),
  836. role: "assistant",
  837. parts: [],
  838. metadata: {
  839. tool: {},
  840. sessionID: input.sessionID,
  841. assistant: {
  842. system,
  843. path: {
  844. cwd: app.path.cwd,
  845. root: app.path.root,
  846. },
  847. summary: true,
  848. cost: 0,
  849. modelID: input.modelID,
  850. providerID: input.providerID,
  851. tokens: {
  852. input: 0,
  853. output: 0,
  854. reasoning: 0,
  855. cache: { read: 0, write: 0 },
  856. },
  857. },
  858. time: {
  859. created: Date.now(),
  860. },
  861. },
  862. }
  863. await updateMessage(next)
  864. let text: Message.TextPart | undefined
  865. const result = streamText({
  866. abortSignal: abort.signal,
  867. model: model.language,
  868. messages: [
  869. ...system.map(
  870. (x): CoreMessage => ({
  871. role: "system",
  872. content: x,
  873. }),
  874. ),
  875. ...convertToCoreMessages(filtered.map(toUIMessage)),
  876. {
  877. role: "user",
  878. content: [
  879. {
  880. type: "text",
  881. 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.",
  882. },
  883. ],
  884. },
  885. ],
  886. onStepFinish: async (step) => {
  887. const assistant = next.metadata!.assistant!
  888. const usage = getUsage(model.info, step.usage, step.providerMetadata)
  889. assistant.cost += usage.cost
  890. assistant.tokens = usage.tokens
  891. await updateMessage(next)
  892. if (text) {
  893. Bus.publish(Message.Event.PartUpdated, {
  894. part: text,
  895. messageID: next.id,
  896. sessionID: next.metadata.sessionID,
  897. })
  898. }
  899. text = undefined
  900. },
  901. async onFinish(input) {
  902. const assistant = next.metadata!.assistant!
  903. const usage = getUsage(model.info, input.usage, input.providerMetadata)
  904. assistant.cost += usage.cost
  905. assistant.tokens = usage.tokens
  906. next.metadata!.time.completed = Date.now()
  907. await updateMessage(next)
  908. },
  909. })
  910. for await (const value of result.fullStream) {
  911. switch (value.type) {
  912. case "text-delta":
  913. if (!text) {
  914. text = {
  915. type: "text",
  916. text: value.textDelta,
  917. }
  918. next.parts.push(text)
  919. } else text.text += value.textDelta
  920. await updateMessage(next)
  921. break
  922. }
  923. }
  924. }
  925. function lock(sessionID: string) {
  926. log.info("locking", { sessionID })
  927. if (state().pending.has(sessionID)) throw new BusyError(sessionID)
  928. const controller = new AbortController()
  929. state().pending.set(sessionID, controller)
  930. return {
  931. signal: controller.signal,
  932. [Symbol.dispose]() {
  933. log.info("unlocking", { sessionID })
  934. state().pending.delete(sessionID)
  935. Bus.publish(Event.Idle, {
  936. sessionID,
  937. })
  938. },
  939. }
  940. }
  941. function getUsage(
  942. model: ModelsDev.Model,
  943. usage: LanguageModelUsage,
  944. metadata?: ProviderMetadata,
  945. ) {
  946. const tokens = {
  947. input: usage.promptTokens ?? 0,
  948. output: usage.completionTokens ?? 0,
  949. reasoning: 0,
  950. cache: {
  951. write: (metadata?.["anthropic"]?.["cacheCreationInputTokens"] ??
  952. // @ts-expect-error
  953. metadata?.["bedrock"]?.["usage"]?.["cacheWriteInputTokens"] ??
  954. 0) as number,
  955. read: (metadata?.["anthropic"]?.["cacheReadInputTokens"] ??
  956. // @ts-expect-error
  957. metadata?.["bedrock"]?.["usage"]?.["cacheReadInputTokens"] ??
  958. 0) as number,
  959. },
  960. }
  961. return {
  962. cost: new Decimal(0)
  963. .add(new Decimal(tokens.input).mul(model.cost.input).div(1_000_000))
  964. .add(new Decimal(tokens.output).mul(model.cost.output).div(1_000_000))
  965. .add(
  966. new Decimal(tokens.cache.read)
  967. .mul(model.cost.cache_read ?? 0)
  968. .div(1_000_000),
  969. )
  970. .add(
  971. new Decimal(tokens.cache.write)
  972. .mul(model.cost.cache_write ?? 0)
  973. .div(1_000_000),
  974. )
  975. .toNumber(),
  976. tokens,
  977. }
  978. }
  979. export class BusyError extends Error {
  980. constructor(public readonly sessionID: string) {
  981. super(`Session ${sessionID} is busy`)
  982. }
  983. }
  984. export async function initialize(input: {
  985. sessionID: string
  986. modelID: string
  987. providerID: string
  988. }) {
  989. const app = App.info()
  990. await Session.chat({
  991. sessionID: input.sessionID,
  992. providerID: input.providerID,
  993. modelID: input.modelID,
  994. parts: [
  995. {
  996. type: "text",
  997. text: PROMPT_INITIALIZE.replace("${path}", app.path.root),
  998. },
  999. ],
  1000. })
  1001. await App.initialize()
  1002. }
  1003. }
  1004. function toUIMessage(msg: Message.Info): UIMessage {
  1005. if (msg.role === "assistant") {
  1006. return {
  1007. id: msg.id,
  1008. role: "assistant",
  1009. content: "",
  1010. ...toParts(msg.parts),
  1011. }
  1012. }
  1013. if (msg.role === "user") {
  1014. return {
  1015. id: msg.id,
  1016. role: "user",
  1017. content: "",
  1018. ...toParts(msg.parts),
  1019. }
  1020. }
  1021. throw new Error("not implemented")
  1022. }
  1023. function toParts(parts: Message.MessagePart[]) {
  1024. const result: {
  1025. parts: UIMessage["parts"]
  1026. experimental_attachments: Attachment[]
  1027. } = {
  1028. parts: [],
  1029. experimental_attachments: [],
  1030. }
  1031. for (const part of parts) {
  1032. switch (part.type) {
  1033. case "text":
  1034. result.parts.push({ type: "text", text: part.text })
  1035. break
  1036. case "file":
  1037. result.experimental_attachments.push({
  1038. url: part.url,
  1039. contentType: part.mediaType,
  1040. name: part.filename,
  1041. })
  1042. break
  1043. case "tool-invocation":
  1044. result.parts.push({
  1045. type: "tool-invocation",
  1046. toolInvocation: part.toolInvocation,
  1047. })
  1048. break
  1049. case "step-start":
  1050. result.parts.push({
  1051. type: "step-start",
  1052. })
  1053. break
  1054. default:
  1055. break
  1056. }
  1057. }
  1058. return result
  1059. }