| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667 |
- import path from "path"
- import { App } from "../app/app"
- import { Identifier } from "../id/id"
- import { Storage } from "../storage/storage"
- import { Log } from "../util/log"
- import {
- convertToModelMessages,
- generateText,
- stepCountIs,
- streamText,
- tool,
- type Tool as AITool,
- type LanguageModelUsage,
- } from "ai"
- import { z, ZodSchema } from "zod"
- import { Decimal } from "decimal.js"
- import PROMPT_ANTHROPIC from "./prompt/anthropic.txt"
- import PROMPT_ANTHROPIC_SPOOF from "./prompt/anthropic_spoof.txt"
- import PROMPT_TITLE from "./prompt/title.txt"
- import PROMPT_SUMMARIZE from "./prompt/summarize.txt"
- 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 { SessionContext } from "./context"
- import { ListTool } from "../tool/ls"
- import { MCP } from "../mcp"
- export namespace Session {
- const log = Log.create({ service: "session" })
- export const Info = z
- .object({
- id: Identifier.schema("session"),
- share: z
- .object({
- secret: z.string(),
- url: z.string(),
- })
- .optional(),
- title: z.string(),
- time: z.object({
- created: z.number(),
- updated: z.number(),
- }),
- })
- .openapi({
- ref: "session.info",
- })
- export type Info = z.output<typeof Info>
- export const Event = {
- Updated: Bus.event(
- "session.updated",
- z.object({
- info: Info,
- }),
- ),
- }
- const state = App.state("session", () => {
- const sessions = new Map<string, Info>()
- const messages = new Map<string, Message.Info[]>()
- return {
- sessions,
- messages,
- }
- })
- export async function create() {
- const result: Info = {
- id: Identifier.descending("session"),
- title: "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)
- 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 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 = share
- })
- for (const msg of await messages(id)) {
- await Share.sync("session/message/" + id + "/" + msg.id, msg)
- }
- return share
- }
- 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).catch(() => {})
- if (!read) continue
- result.push(read)
- }
- result.sort((a, b) => (a.id > b.id ? 1 : -1))
- return result
- }
- export async function* list() {
- for await (const item of Storage.list("session/info")) {
- const sessionID = path.basename(item, ".json")
- yield get(sessionID)
- }
- }
- export function abort(sessionID: string) {
- const controller = pending.get(sessionID)
- if (!controller) return false
- controller.abort()
- pending.delete(sessionID)
- return true
- }
- 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.Part[]
- }) {
- 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 previous = msgs.at(-1)
- if (previous?.metadata.assistant) {
- const tokens =
- previous.metadata.assistant.tokens.input +
- previous.metadata.assistant.tokens.output
- if (
- tokens >
- (model.info.contextWindow - (model.info.maxOutputTokens ?? 0)) * 0.9
- ) {
- 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.role === "system" || msg.id >= lastSummary.id,
- )
- if (msgs.length === 0) {
- const app = App.info()
- if (input.providerID === "anthropic") {
- const claude: Message.Info = {
- id: Identifier.ascending("message"),
- role: "system",
- parts: [
- {
- type: "text",
- text: PROMPT_ANTHROPIC_SPOOF.trim(),
- },
- ],
- metadata: {
- sessionID: input.sessionID,
- time: {
- created: Date.now(),
- },
- tool: {},
- },
- }
- await updateMessage(claude)
- msgs.push(claude)
- }
- const system: Message.Info = {
- id: Identifier.ascending("message"),
- role: "system",
- parts: [
- {
- type: "text",
- text: PROMPT_ANTHROPIC,
- },
- {
- type: "text",
- text: [
- `Here is some useful information about the environment you are running in:`,
- `<env>`,
- `Working directory: ${app.path.cwd}`,
- `Is directory a git repo: ${app.git ? "yes" : "no"}`,
- `Platform: ${process.platform}`,
- `Today's date: ${new Date().toISOString()}`,
- `</env>`,
- `<project>`,
- `${app.git ? await ListTool.execute({ path: app.path.cwd }, { sessionID: input.sessionID }).then((x) => x.output) : ""}`,
- `</project>`,
- ].join("\n"),
- },
- ],
- metadata: {
- sessionID: input.sessionID,
- time: {
- created: Date.now(),
- },
- tool: {},
- },
- }
- const context = await SessionContext.find()
- if (context) {
- system.parts.push({
- type: "text",
- text: context,
- })
- }
- msgs.push(system)
- generateText({
- maxOutputTokens: 80,
- messages: convertToModelMessages([
- {
- role: "system",
- parts: [
- {
- type: "text",
- text: PROMPT_ANTHROPIC_SPOOF.trim(),
- },
- ],
- },
- {
- role: "system",
- parts: [
- {
- type: "text",
- text: PROMPT_TITLE,
- },
- ],
- },
- {
- role: "user",
- parts: input.parts,
- },
- ]),
- model: model.language,
- }).then((result) => {
- return Session.update(input.sessionID, (draft) => {
- draft.title = result.text
- })
- })
- await updateMessage(system)
- }
- const msg: Message.Info = {
- role: "user",
- id: Identifier.ascending("message"),
- parts: input.parts,
- metadata: {
- time: {
- created: Date.now(),
- },
- sessionID: input.sessionID,
- tool: {},
- },
- }
- await updateMessage(msg)
- msgs.push(msg)
- const next: Message.Info = {
- id: Identifier.ascending("message"),
- role: "assistant",
- parts: [],
- metadata: {
- assistant: {
- cost: 0,
- tokens: {
- input: 0,
- output: 0,
- reasoning: 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,
- })
- next.metadata!.tool![opts.toolCallId] = {
- ...result.metadata,
- time: {
- start,
- end: Date.now(),
- },
- }
- return result.output
- } catch (e: any) {
- next.metadata!.tool![opts.toolCallId] = {
- error: true,
- message: e.toString(),
- time: {
- start,
- end: Date.now(),
- },
- }
- 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,
- time: {
- start,
- end: Date.now(),
- },
- }
- 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(),
- time: {
- start,
- end: Date.now(),
- },
- }
- 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(step.usage, model.info)
- assistant.cost += usage.cost
- assistant.tokens = usage.tokens
- await updateMessage(next)
- if (text) {
- Bus.publish(Message.Event.PartUpdated, {
- part: text,
- })
- }
- text = undefined
- },
- async onChunk(input) {
- const value = input.chunk
- l.info("part", {
- type: value.type,
- })
- switch (value.type) {
- case "text":
- if (!text) {
- text = value
- next.parts.push(value)
- break
- } else text.text += value.text
- break
- case "tool-call":
- next.parts.push({
- type: "tool-invocation",
- toolInvocation: {
- state: "call",
- ...value,
- // hack until zod v4
- args: value.args as any,
- },
- })
- Bus.publish(Message.Event.PartUpdated, {
- part: next.parts[next.parts.length - 1],
- })
- 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],
- })
- break
- case "tool-call-delta":
- break
- case "tool-result":
- const match = next.parts.find(
- (p) =>
- p.type === "tool-invocation" &&
- p.toolInvocation.toolCallId === value.toolCallId,
- )
- if (match && match.type === "tool-invocation") {
- match.toolInvocation = {
- args: match.toolInvocation.args,
- toolCallId: match.toolInvocation.toolCallId,
- toolName: match.toolInvocation.toolName,
- state: "result",
- result: value.result as string,
- }
- Bus.publish(Message.Event.PartUpdated, {
- part: match,
- })
- }
- break
- default:
- l.info("unhandled", {
- type: value.type,
- })
- }
- await updateMessage(next)
- },
- async onFinish(input) {
- const assistant = next.metadata!.assistant!
- const usage = getUsage(input.totalUsage, model.info)
- assistant.cost = usage.cost
- await updateMessage(next)
- },
- onError(input) {
- if (input.error instanceof Error) {
- next.metadata.error = input.error.toString()
- }
- },
- async prepareStep(step) {
- next.parts.push({
- type: "step-start",
- })
- await updateMessage(next)
- return step
- },
- toolCallStreaming: false,
- abortSignal: abort.signal,
- maxRetries: 6,
- stopWhen: stepCountIs(1000),
- messages: convertToModelMessages(msgs),
- temperature: 0,
- tools: {
- ...(await MCP.tools()),
- ...tools,
- },
- model: model.language,
- })
- await result.consumeStream()
- next.metadata!.time.completed = Date.now()
- await updateMessage(next)
- return next
- }
- 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) => msg.role !== "system" && (!lastSummary || msg.id >= lastSummary),
- )
- const model = await Provider.getModel(input.providerID, input.modelID)
- const next: Message.Info = {
- id: Identifier.ascending("message"),
- role: "assistant",
- parts: [],
- metadata: {
- tool: {},
- sessionID: input.sessionID,
- assistant: {
- summary: true,
- cost: 0,
- modelID: input.modelID,
- providerID: input.providerID,
- tokens: {
- input: 0,
- output: 0,
- reasoning: 0,
- },
- },
- time: {
- created: Date.now(),
- },
- },
- }
- await updateMessage(next)
- const result = await generateText({
- abortSignal: abort.signal,
- model: model.language,
- messages: convertToModelMessages([
- {
- role: "system",
- parts: [
- {
- type: "text",
- text: PROMPT_SUMMARIZE,
- },
- ],
- },
- ...filtered,
- {
- role: "user",
- parts: [
- {
- 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.",
- },
- ],
- },
- ]),
- })
- next.parts.push({
- type: "text",
- text: result.text,
- })
- const assistant = next.metadata!.assistant!
- const usage = getUsage(result.usage, model.info)
- assistant.cost = usage.cost
- assistant.tokens = usage.tokens
- await updateMessage(next)
- }
- const pending = new Map<string, AbortController>()
- function lock(sessionID: string) {
- log.info("locking", { sessionID })
- if (pending.has(sessionID)) throw new BusyError(sessionID)
- const controller = new AbortController()
- pending.set(sessionID, controller)
- return {
- signal: controller.signal,
- [Symbol.dispose]() {
- log.info("unlocking", { sessionID })
- pending.delete(sessionID)
- },
- }
- }
- function getUsage(usage: LanguageModelUsage, model: Provider.Model) {
- const tokens = {
- input: usage.inputTokens ?? 0,
- output: usage.outputTokens ?? 0,
- reasoning: usage.reasoningTokens ?? 0,
- }
- 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))
- .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
- }) {
- await Session.chat({
- sessionID: input.sessionID,
- providerID: input.providerID,
- modelID: input.modelID,
- parts: [
- {
- type: "text",
- text: PROMPT_INITIALIZE,
- },
- ],
- })
- await App.initialize()
- }
- }
|