| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135 |
- import path from "node:path"
- import { App } from "../app/app"
- import { Identifier } from "../id/id"
- import { Storage } from "../storage/storage"
- import { Log } from "../util/log"
- import {
- generateText,
- LoadAPIKeyError,
- convertToCoreMessages,
- streamText,
- tool,
- type Tool as AITool,
- type LanguageModelUsage,
- type CoreMessage,
- type UIMessage,
- type ProviderMetadata,
- wrapLanguageModel,
- type Attachment,
- } from "ai"
- import { z, ZodSchema } from "zod"
- import { Decimal } from "decimal.js"
- import PROMPT_INITIALIZE from "../session/prompt/initialize.txt"
- import { Share } from "../share/share"
- import { Message } from "./message"
- import { Bus } from "../bus"
- import { Provider } from "../provider/provider"
- import { MCP } from "../mcp"
- import { NamedError } from "../util/error"
- import type { Tool } from "../tool/tool"
- import { SystemPrompt } from "./system"
- import { Flag } from "../flag/flag"
- import type { ModelsDev } from "../provider/models"
- import { Installation } from "../installation"
- import { Config } from "../config/config"
- import { ProviderTransform } from "../provider/transform"
- import { Snapshot } from "../snapshot"
- export namespace Session {
- const log = Log.create({ service: "session" })
- export const Info = z
- .object({
- id: Identifier.schema("session"),
- parentID: Identifier.schema("session").optional(),
- share: z
- .object({
- url: z.string(),
- })
- .optional(),
- title: z.string(),
- version: z.string(),
- time: z.object({
- created: z.number(),
- updated: z.number(),
- }),
- revert: z
- .object({
- messageID: z.string(),
- part: z.number(),
- snapshot: z.string().optional(),
- })
- .optional(),
- })
- .openapi({
- ref: "Session",
- })
- export type Info = z.output<typeof Info>
- export const ShareInfo = z
- .object({
- secret: z.string(),
- url: z.string(),
- })
- .openapi({
- ref: "SessionShare",
- })
- export type ShareInfo = z.output<typeof ShareInfo>
- export const Event = {
- Updated: Bus.event(
- "session.updated",
- z.object({
- info: Info,
- }),
- ),
- Deleted: Bus.event(
- "session.deleted",
- z.object({
- info: Info,
- }),
- ),
- Idle: Bus.event(
- "session.idle",
- z.object({
- sessionID: z.string(),
- }),
- ),
- Error: Bus.event(
- "session.error",
- z.object({
- error: Message.Info.shape.metadata.shape.error,
- }),
- ),
- }
- const state = App.state(
- "session",
- () => {
- const sessions = new Map<string, Info>()
- const messages = new Map<string, Message.Info[]>()
- const pending = new Map<string, AbortController>()
- return {
- sessions,
- messages,
- pending,
- }
- },
- async (state) => {
- for (const [_, controller] of state.pending) {
- controller.abort()
- }
- },
- )
- export async function create(parentID?: string) {
- const result: Info = {
- id: Identifier.descending("session"),
- version: Installation.VERSION,
- parentID,
- title:
- (parentID ? "Child session - " : "New Session - ") +
- new Date().toISOString(),
- time: {
- created: Date.now(),
- updated: Date.now(),
- },
- }
- log.info("created", result)
- state().sessions.set(result.id, result)
- await Storage.writeJSON("session/info/" + result.id, result)
- const cfg = await Config.get()
- if (!result.parentID && (Flag.OPENCODE_AUTO_SHARE || cfg.autoshare))
- share(result.id).then((share) => {
- update(result.id, (draft) => {
- draft.share = share
- })
- })
- Bus.publish(Event.Updated, {
- info: result,
- })
- return result
- }
- export async function get(id: string) {
- const result = state().sessions.get(id)
- if (result) {
- return result
- }
- const read = await Storage.readJSON<Info>("session/info/" + id)
- state().sessions.set(id, read)
- return read as Info
- }
- export async function getShare(id: string) {
- return Storage.readJSON<ShareInfo>("session/share/" + id)
- }
- export async function share(id: string) {
- const session = await get(id)
- if (session.share) return session.share
- const share = await Share.create(id)
- await update(id, (draft) => {
- draft.share = {
- url: share.url,
- }
- })
- await Storage.writeJSON<ShareInfo>("session/share/" + id, share)
- await Share.sync("session/info/" + id, session)
- for (const msg of await messages(id)) {
- await Share.sync("session/message/" + id + "/" + msg.id, msg)
- }
- return share
- }
- export async function unshare(id: string) {
- const share = await getShare(id)
- if (!share) return
- await Storage.remove("session/share/" + id)
- await update(id, (draft) => {
- draft.share = undefined
- })
- await Share.remove(id, share.secret)
- }
- export async function update(id: string, editor: (session: Info) => void) {
- const { sessions } = state()
- const session = await get(id)
- if (!session) return
- editor(session)
- session.time.updated = Date.now()
- sessions.set(id, session)
- await Storage.writeJSON("session/info/" + id, session)
- Bus.publish(Event.Updated, {
- info: session,
- })
- return session
- }
- export async function messages(sessionID: string) {
- const result = [] as Message.Info[]
- const list = Storage.list("session/message/" + sessionID)
- for await (const p of list) {
- const read = await Storage.readJSON<Message.Info>(p)
- result.push(read)
- }
- result.sort((a, b) => (a.id > b.id ? 1 : -1))
- return result
- }
- export async function getMessage(sessionID: string, messageID: string) {
- return Storage.readJSON<Message.Info>(
- "session/message/" + sessionID + "/" + messageID,
- )
- }
- export async function* list() {
- for await (const item of Storage.list("session/info")) {
- const sessionID = path.basename(item, ".json")
- yield get(sessionID)
- }
- }
- export async function children(parentID: string) {
- const result = [] as Session.Info[]
- for await (const item of Storage.list("session/info")) {
- const sessionID = path.basename(item, ".json")
- const session = await get(sessionID)
- if (session.parentID !== parentID) continue
- result.push(session)
- }
- return result
- }
- export function abort(sessionID: string) {
- const controller = state().pending.get(sessionID)
- if (!controller) return false
- controller.abort()
- state().pending.delete(sessionID)
- return true
- }
- export async function remove(sessionID: string, emitEvent = true) {
- try {
- abort(sessionID)
- const session = await get(sessionID)
- for (const child of await children(sessionID)) {
- await remove(child.id, false)
- }
- await unshare(sessionID).catch(() => {})
- await Storage.remove(`session/info/${sessionID}`).catch(() => {})
- await Storage.removeDir(`session/message/${sessionID}/`).catch(() => {})
- state().sessions.delete(sessionID)
- state().messages.delete(sessionID)
- if (emitEvent) {
- Bus.publish(Event.Deleted, {
- info: session,
- })
- }
- } catch (e) {
- log.error(e)
- }
- }
- async function updateMessage(msg: Message.Info) {
- await Storage.writeJSON(
- "session/message/" + msg.metadata.sessionID + "/" + msg.id,
- msg,
- )
- Bus.publish(Message.Event.Updated, {
- info: msg,
- })
- }
- export async function chat(input: {
- sessionID: string
- providerID: string
- modelID: string
- parts: Message.MessagePart[]
- system?: string[]
- tools?: Tool.Info[]
- }) {
- const l = log.clone().tag("session", input.sessionID)
- l.info("chatting")
- const model = await Provider.getModel(input.providerID, input.modelID)
- let msgs = await messages(input.sessionID)
- const session = await get(input.sessionID)
- if (session.revert) {
- const trimmed = []
- for (const msg of msgs) {
- if (
- msg.id > session.revert.messageID ||
- (msg.id === session.revert.messageID && session.revert.part === 0)
- ) {
- await Storage.remove(
- "session/message/" + input.sessionID + "/" + msg.id,
- )
- await Bus.publish(Message.Event.Removed, {
- sessionID: input.sessionID,
- messageID: msg.id,
- })
- continue
- }
- if (msg.id === session.revert.messageID) {
- if (session.revert.part === 0) break
- msg.parts = msg.parts.slice(0, session.revert.part)
- }
- trimmed.push(msg)
- }
- msgs = trimmed
- await update(input.sessionID, (draft) => {
- draft.revert = undefined
- })
- }
- const previous = msgs.at(-1)
- // auto summarize if too long
- if (previous?.metadata.assistant) {
- const tokens =
- previous.metadata.assistant.tokens.input +
- previous.metadata.assistant.tokens.cache.read +
- previous.metadata.assistant.tokens.cache.write +
- previous.metadata.assistant.tokens.output
- if (
- model.info.limit.context &&
- tokens >
- Math.max(
- (model.info.limit.context - (model.info.limit.output ?? 0)) * 0.9,
- 0,
- )
- ) {
- await summarize({
- sessionID: input.sessionID,
- providerID: input.providerID,
- modelID: input.modelID,
- })
- return chat(input)
- }
- }
- using abort = lock(input.sessionID)
- const lastSummary = msgs.findLast(
- (msg) => msg.metadata.assistant?.summary === true,
- )
- if (lastSummary) msgs = msgs.filter((msg) => msg.id >= lastSummary.id)
- const app = App.info()
- input.parts = await Promise.all(
- input.parts.map(async (part): Promise<Message.MessagePart[]> => {
- if (part.type === "file") {
- const url = new URL(part.url)
- switch (url.protocol) {
- case "file:":
- let content = Bun.file(path.join(app.path.cwd, url.pathname))
- if (part.mediaType === "text/plain") {
- let text = await content.text()
- const range = {
- start: url.searchParams.get("start"),
- end: url.searchParams.get("end"),
- }
- if (range.start != null && part.mediaType === "text/plain") {
- const lines = text.split("\n")
- const start = parseInt(range.start)
- const end = range.end ? parseInt(range.end) : lines.length
- text = lines.slice(start, end).join("\n")
- }
- return [
- {
- type: "text",
- text: [
- "Called the Read tool on " + url.pathname,
- "<results>",
- text,
- "</results>",
- ].join("\n"),
- },
- ]
- }
- return [
- {
- type: "text",
- text: ["Called the Read tool on " + url.pathname].join("\n"),
- },
- {
- type: "file",
- url:
- `data:${part.mediaType};base64,` +
- Buffer.from(await content.bytes()).toString("base64url"),
- mediaType: part.mediaType,
- filename: path.basename(part.filename!),
- },
- ]
- }
- }
- return [part]
- }),
- ).then((x) => x.flat())
- if (msgs.length === 0 && !session.parentID) {
- generateText({
- maxTokens: input.providerID === "google" ? 1024 : 20,
- providerOptions: model.info.options,
- messages: [
- ...SystemPrompt.title(input.providerID).map(
- (x): CoreMessage => ({
- role: "system",
- content: x,
- }),
- ),
- ...convertToCoreMessages([
- {
- role: "user",
- content: "",
- parts: toParts(input.parts).parts,
- },
- ]),
- ],
- model: model.language,
- })
- .then((result) => {
- if (result.text)
- return Session.update(input.sessionID, (draft) => {
- draft.title = result.text
- })
- })
- .catch(() => {})
- }
- const snapshot = await Snapshot.create(input.sessionID)
- const msg: Message.Info = {
- role: "user",
- id: Identifier.ascending("message"),
- parts: input.parts,
- metadata: {
- time: {
- created: Date.now(),
- },
- sessionID: input.sessionID,
- tool: {},
- snapshot,
- },
- }
- await updateMessage(msg)
- msgs.push(msg)
- const system = input.system ?? SystemPrompt.provider(input.providerID)
- system.push(...(await SystemPrompt.environment()))
- system.push(...(await SystemPrompt.custom()))
- const next: Message.Info = {
- id: Identifier.ascending("message"),
- role: "assistant",
- parts: [],
- metadata: {
- snapshot,
- assistant: {
- system,
- path: {
- cwd: app.path.cwd,
- root: app.path.root,
- },
- cost: 0,
- tokens: {
- input: 0,
- output: 0,
- reasoning: 0,
- cache: { read: 0, write: 0 },
- },
- modelID: input.modelID,
- providerID: input.providerID,
- },
- time: {
- created: Date.now(),
- },
- sessionID: input.sessionID,
- tool: {},
- },
- }
- await updateMessage(next)
- const tools: Record<string, AITool> = {}
- for (const item of await Provider.tools(input.providerID)) {
- tools[item.id.replaceAll(".", "_")] = tool({
- id: item.id as any,
- description: item.description,
- parameters: item.parameters as ZodSchema,
- async execute(args, opts) {
- const start = Date.now()
- try {
- const result = await item.execute(args, {
- sessionID: input.sessionID,
- abort: abort.signal,
- messageID: next.id,
- metadata: async (val) => {
- next.metadata.tool[opts.toolCallId] = {
- ...val,
- time: {
- start: 0,
- end: 0,
- },
- }
- await updateMessage(next)
- },
- })
- next.metadata!.tool![opts.toolCallId] = {
- ...result.metadata,
- snapshot: await Snapshot.create(input.sessionID),
- time: {
- start,
- end: Date.now(),
- },
- }
- await updateMessage(next)
- return result.output
- } catch (e: any) {
- next.metadata!.tool![opts.toolCallId] = {
- error: true,
- message: e.toString(),
- title: e.toString(),
- snapshot: await Snapshot.create(input.sessionID),
- time: {
- start,
- end: Date.now(),
- },
- }
- await updateMessage(next)
- return e.toString()
- }
- },
- })
- }
- for (const [key, item] of Object.entries(await MCP.tools())) {
- const execute = item.execute
- if (!execute) continue
- item.execute = async (args, opts) => {
- const start = Date.now()
- try {
- const result = await execute(args, opts)
- next.metadata!.tool![opts.toolCallId] = {
- ...result.metadata,
- snapshot: await Snapshot.create(input.sessionID),
- time: {
- start,
- end: Date.now(),
- },
- }
- await updateMessage(next)
- return result.content
- .filter((x: any) => x.type === "text")
- .map((x: any) => x.text)
- .join("\n\n")
- } catch (e: any) {
- next.metadata!.tool![opts.toolCallId] = {
- error: true,
- message: e.toString(),
- snapshot: await Snapshot.create(input.sessionID),
- title: "mcp",
- time: {
- start,
- end: Date.now(),
- },
- }
- await updateMessage(next)
- return e.toString()
- }
- }
- tools[key] = item
- }
- let text: Message.TextPart | undefined
- const result = streamText({
- onStepFinish: async (step) => {
- log.info("step finish", { finishReason: step.finishReason })
- const assistant = next.metadata!.assistant!
- const usage = getUsage(model.info, step.usage, step.providerMetadata)
- assistant.cost += usage.cost
- assistant.tokens = usage.tokens
- await updateMessage(next)
- if (text) {
- Bus.publish(Message.Event.PartUpdated, {
- part: text,
- messageID: next.id,
- sessionID: next.metadata.sessionID,
- })
- }
- text = undefined
- },
- onError(err) {
- log.error("callback error", err)
- switch (true) {
- case LoadAPIKeyError.isInstance(err.error):
- next.metadata.error = new Provider.AuthError(
- {
- providerID: input.providerID,
- message: err.error.message,
- },
- { cause: err.error },
- ).toObject()
- break
- case err.error instanceof Error:
- next.metadata.error = new NamedError.Unknown(
- { message: err.error.toString() },
- { cause: err.error },
- ).toObject()
- break
- default:
- next.metadata.error = new NamedError.Unknown(
- { message: JSON.stringify(err.error) },
- { cause: err.error },
- )
- }
- Bus.publish(Event.Error, {
- error: next.metadata.error,
- })
- },
- // async prepareStep(step) {
- // next.parts.push({
- // type: "step-start",
- // })
- // await updateMessage(next)
- // return step
- // },
- toolCallStreaming: true,
- maxRetries: 10,
- maxTokens: Math.max(0, model.info.limit.output) || undefined,
- abortSignal: abort.signal,
- maxSteps: 1000,
- providerOptions: model.info.options,
- messages: [
- ...system.map(
- (x): CoreMessage => ({
- role: "system",
- content: x,
- }),
- ),
- ...convertToCoreMessages(
- msgs.map(toUIMessage).filter((x) => x.parts.length > 0),
- ),
- ],
- temperature: model.info.temperature ? 0 : undefined,
- tools: model.info.tool_call === false ? undefined : tools,
- model: wrapLanguageModel({
- model: model.language,
- middleware: [
- {
- async transformParams(args) {
- if (args.type === "stream") {
- args.params.prompt = ProviderTransform.message(
- args.params.prompt,
- input.providerID,
- input.modelID,
- )
- }
- return args.params
- },
- },
- ],
- }),
- })
- try {
- for await (const value of result.fullStream) {
- l.info("part", {
- type: value.type,
- })
- switch (value.type) {
- case "step-start":
- next.parts.push({
- type: "step-start",
- })
- break
- case "text-delta":
- if (!text) {
- text = {
- type: "text",
- text: value.textDelta,
- }
- next.parts.push(text)
- break
- } else text.text += value.textDelta
- break
- case "tool-call": {
- const [match] = next.parts.flatMap((p) =>
- p.type === "tool-invocation" &&
- p.toolInvocation.toolCallId === value.toolCallId
- ? [p]
- : [],
- )
- if (!match) break
- match.toolInvocation.args = value.args
- match.toolInvocation.state = "call"
- Bus.publish(Message.Event.PartUpdated, {
- part: match,
- messageID: next.id,
- sessionID: next.metadata.sessionID,
- })
- break
- }
- case "tool-call-streaming-start":
- next.parts.push({
- type: "tool-invocation",
- toolInvocation: {
- state: "partial-call",
- toolName: value.toolName,
- toolCallId: value.toolCallId,
- args: {},
- },
- })
- Bus.publish(Message.Event.PartUpdated, {
- part: next.parts[next.parts.length - 1],
- messageID: next.id,
- sessionID: next.metadata.sessionID,
- })
- break
- case "tool-call-delta":
- continue
- // for some reason ai sdk claims to not send this part but it does
- // @ts-expect-error
- case "tool-result":
- const match = next.parts.find(
- (p) =>
- p.type === "tool-invocation" &&
- // @ts-expect-error
- p.toolInvocation.toolCallId === value.toolCallId,
- )
- if (match && match.type === "tool-invocation") {
- match.toolInvocation = {
- // @ts-expect-error
- args: value.args,
- // @ts-expect-error
- toolCallId: value.toolCallId,
- // @ts-expect-error
- toolName: value.toolName,
- state: "result",
- // @ts-expect-error
- result: value.result as string,
- }
- Bus.publish(Message.Event.PartUpdated, {
- part: match,
- messageID: next.id,
- sessionID: next.metadata.sessionID,
- })
- }
- break
- case "finish":
- log.info("message finish", {
- reason: value.finishReason,
- })
- const assistant = next.metadata!.assistant!
- const usage = getUsage(
- model.info,
- value.usage,
- value.providerMetadata,
- )
- assistant.cost += usage.cost
- await updateMessage(next)
- if (value.finishReason === "length")
- throw new Message.OutputLengthError({})
- break
- default:
- l.info("unhandled", {
- type: value.type,
- })
- continue
- }
- await updateMessage(next)
- }
- } catch (e: any) {
- log.error("stream error", {
- error: e,
- })
- switch (true) {
- case Message.OutputLengthError.isInstance(e):
- next.metadata.error = e
- break
- case LoadAPIKeyError.isInstance(e):
- next.metadata.error = new Provider.AuthError(
- {
- providerID: input.providerID,
- message: e.message,
- },
- { cause: e },
- ).toObject()
- break
- case e instanceof Error:
- next.metadata.error = new NamedError.Unknown(
- { message: e.toString() },
- { cause: e },
- ).toObject()
- break
- default:
- next.metadata.error = new NamedError.Unknown(
- { message: JSON.stringify(e) },
- { cause: e },
- )
- }
- Bus.publish(Event.Error, {
- error: next.metadata.error,
- })
- }
- next.metadata!.time.completed = Date.now()
- for (const part of next.parts) {
- if (
- part.type === "tool-invocation" &&
- part.toolInvocation.state !== "result"
- ) {
- part.toolInvocation = {
- ...part.toolInvocation,
- state: "result",
- result: "request was aborted",
- }
- }
- }
- await updateMessage(next)
- return next
- }
- export async function revert(input: {
- sessionID: string
- messageID: string
- part: number
- }) {
- const message = await getMessage(input.sessionID, input.messageID)
- if (!message) return
- const part = message.parts[input.part]
- if (!part) return
- const session = await get(input.sessionID)
- const snapshot =
- session.revert?.snapshot ?? (await Snapshot.create(input.sessionID))
- const old = (() => {
- if (message.role === "assistant") {
- const lastTool = message.parts.findLast(
- (part, index) =>
- part.type === "tool-invocation" && index < input.part,
- )
- if (lastTool && lastTool.type === "tool-invocation")
- return message.metadata.tool[lastTool.toolInvocation.toolCallId]
- .snapshot
- }
- return message.metadata.snapshot
- })()
- if (old) await Snapshot.restore(input.sessionID, old)
- await update(input.sessionID, (draft) => {
- draft.revert = {
- messageID: input.messageID,
- part: input.part,
- snapshot,
- }
- })
- }
- export async function unrevert(sessionID: string) {
- const session = await get(sessionID)
- if (!session) return
- if (!session.revert) return
- if (session.revert.snapshot)
- await Snapshot.restore(sessionID, session.revert.snapshot)
- update(sessionID, (draft) => {
- draft.revert = undefined
- })
- }
- export async function summarize(input: {
- sessionID: string
- providerID: string
- modelID: string
- }) {
- using abort = lock(input.sessionID)
- const msgs = await messages(input.sessionID)
- const lastSummary = msgs.findLast(
- (msg) => msg.metadata.assistant?.summary === true,
- )?.id
- const filtered = msgs.filter((msg) => !lastSummary || msg.id >= lastSummary)
- const model = await Provider.getModel(input.providerID, input.modelID)
- const app = App.info()
- const system = SystemPrompt.summarize(input.providerID)
- const next: Message.Info = {
- id: Identifier.ascending("message"),
- role: "assistant",
- parts: [],
- metadata: {
- tool: {},
- sessionID: input.sessionID,
- assistant: {
- system,
- path: {
- cwd: app.path.cwd,
- root: app.path.root,
- },
- summary: true,
- cost: 0,
- modelID: input.modelID,
- providerID: input.providerID,
- tokens: {
- input: 0,
- output: 0,
- reasoning: 0,
- cache: { read: 0, write: 0 },
- },
- },
- time: {
- created: Date.now(),
- },
- },
- }
- await updateMessage(next)
- let text: Message.TextPart | undefined
- const result = streamText({
- abortSignal: abort.signal,
- model: model.language,
- messages: [
- ...system.map(
- (x): CoreMessage => ({
- role: "system",
- content: x,
- }),
- ),
- ...convertToCoreMessages(filtered.map(toUIMessage)),
- {
- role: "user",
- content: [
- {
- type: "text",
- 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.",
- },
- ],
- },
- ],
- onStepFinish: async (step) => {
- const assistant = next.metadata!.assistant!
- const usage = getUsage(model.info, step.usage, step.providerMetadata)
- assistant.cost += usage.cost
- assistant.tokens = usage.tokens
- await updateMessage(next)
- if (text) {
- Bus.publish(Message.Event.PartUpdated, {
- part: text,
- messageID: next.id,
- sessionID: next.metadata.sessionID,
- })
- }
- text = undefined
- },
- async onFinish(input) {
- const assistant = next.metadata!.assistant!
- const usage = getUsage(model.info, input.usage, input.providerMetadata)
- assistant.cost += usage.cost
- assistant.tokens = usage.tokens
- next.metadata!.time.completed = Date.now()
- await updateMessage(next)
- },
- })
- for await (const value of result.fullStream) {
- switch (value.type) {
- case "text-delta":
- if (!text) {
- text = {
- type: "text",
- text: value.textDelta,
- }
- next.parts.push(text)
- } else text.text += value.textDelta
- await updateMessage(next)
- break
- }
- }
- }
- function lock(sessionID: string) {
- log.info("locking", { sessionID })
- if (state().pending.has(sessionID)) throw new BusyError(sessionID)
- const controller = new AbortController()
- state().pending.set(sessionID, controller)
- return {
- signal: controller.signal,
- [Symbol.dispose]() {
- log.info("unlocking", { sessionID })
- state().pending.delete(sessionID)
- Bus.publish(Event.Idle, {
- sessionID,
- })
- },
- }
- }
- function getUsage(
- model: ModelsDev.Model,
- usage: LanguageModelUsage,
- metadata?: ProviderMetadata,
- ) {
- const tokens = {
- input: usage.promptTokens ?? 0,
- output: usage.completionTokens ?? 0,
- reasoning: 0,
- cache: {
- write: (metadata?.["anthropic"]?.["cacheCreationInputTokens"] ??
- // @ts-expect-error
- metadata?.["bedrock"]?.["usage"]?.["cacheWriteInputTokens"] ??
- 0) as number,
- read: (metadata?.["anthropic"]?.["cacheReadInputTokens"] ??
- // @ts-expect-error
- metadata?.["bedrock"]?.["usage"]?.["cacheReadInputTokens"] ??
- 0) as number,
- },
- }
- return {
- cost: new Decimal(0)
- .add(new Decimal(tokens.input).mul(model.cost.input).div(1_000_000))
- .add(new Decimal(tokens.output).mul(model.cost.output).div(1_000_000))
- .add(
- new Decimal(tokens.cache.read)
- .mul(model.cost.cache_read ?? 0)
- .div(1_000_000),
- )
- .add(
- new Decimal(tokens.cache.write)
- .mul(model.cost.cache_write ?? 0)
- .div(1_000_000),
- )
- .toNumber(),
- tokens,
- }
- }
- export class BusyError extends Error {
- constructor(public readonly sessionID: string) {
- super(`Session ${sessionID} is busy`)
- }
- }
- export async function initialize(input: {
- sessionID: string
- modelID: string
- providerID: string
- }) {
- const app = App.info()
- await Session.chat({
- sessionID: input.sessionID,
- providerID: input.providerID,
- modelID: input.modelID,
- parts: [
- {
- type: "text",
- text: PROMPT_INITIALIZE.replace("${path}", app.path.root),
- },
- ],
- })
- await App.initialize()
- }
- }
- function toUIMessage(msg: Message.Info): UIMessage {
- if (msg.role === "assistant") {
- return {
- id: msg.id,
- role: "assistant",
- content: "",
- ...toParts(msg.parts),
- }
- }
- if (msg.role === "user") {
- return {
- id: msg.id,
- role: "user",
- content: "",
- ...toParts(msg.parts),
- }
- }
- throw new Error("not implemented")
- }
- function toParts(parts: Message.MessagePart[]) {
- const result: {
- parts: UIMessage["parts"]
- experimental_attachments: Attachment[]
- } = {
- parts: [],
- experimental_attachments: [],
- }
- for (const part of parts) {
- switch (part.type) {
- case "text":
- result.parts.push({ type: "text", text: part.text })
- break
- case "file":
- result.experimental_attachments.push({
- url: part.url,
- contentType: part.mediaType,
- name: part.filename,
- })
- break
- case "tool-invocation":
- result.parts.push({
- type: "tool-invocation",
- toolInvocation: part.toolInvocation,
- })
- break
- case "step-start":
- result.parts.push({
- type: "step-start",
- })
- break
- default:
- break
- }
- }
- return result
- }
|