index.ts 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  1. import { Slug } from "@opencode-ai/util/slug"
  2. import path from "path"
  3. import { BusEvent } from "@/bus/bus-event"
  4. import { Bus } from "@/bus"
  5. import { Decimal } from "decimal.js"
  6. import z from "zod"
  7. import { type ProviderMetadata } from "ai"
  8. import { Config } from "../config/config"
  9. import { Flag } from "../flag/flag"
  10. import { Identifier } from "../id/id"
  11. import { Installation } from "../installation"
  12. import { Storage } from "../storage/storage"
  13. import { Log } from "../util/log"
  14. import { MessageV2 } from "./message-v2"
  15. import { Instance } from "../project/instance"
  16. import { SessionPrompt } from "./prompt"
  17. import { fn } from "@/util/fn"
  18. import { Command } from "../command"
  19. import { Snapshot } from "@/snapshot"
  20. import type { Provider } from "@/provider/provider"
  21. import { PermissionNext } from "@/permission/next"
  22. import { Global } from "@/global"
  23. import type { LanguageModelV2Usage } from "@ai-sdk/provider"
  24. import { iife } from "@/util/iife"
  25. export namespace Session {
  26. const log = Log.create({ service: "session" })
  27. const parentTitlePrefix = "New session - "
  28. const childTitlePrefix = "Child session - "
  29. function createDefaultTitle(isChild = false) {
  30. return (isChild ? childTitlePrefix : parentTitlePrefix) + new Date().toISOString()
  31. }
  32. export function isDefaultTitle(title: string) {
  33. return new RegExp(
  34. `^(${parentTitlePrefix}|${childTitlePrefix})\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}\\.\\d{3}Z$`,
  35. ).test(title)
  36. }
  37. function getForkedTitle(title: string): string {
  38. const match = title.match(/^(.+) \(fork #(\d+)\)$/)
  39. if (match) {
  40. const base = match[1]
  41. const num = parseInt(match[2], 10)
  42. return `${base} (fork #${num + 1})`
  43. }
  44. return `${title} (fork #1)`
  45. }
  46. export const Info = z
  47. .object({
  48. id: Identifier.schema("session"),
  49. slug: z.string(),
  50. projectID: z.string(),
  51. directory: z.string(),
  52. parentID: Identifier.schema("session").optional(),
  53. summary: z
  54. .object({
  55. additions: z.number(),
  56. deletions: z.number(),
  57. files: z.number(),
  58. diffs: Snapshot.FileDiff.array().optional(),
  59. })
  60. .optional(),
  61. share: z
  62. .object({
  63. url: z.string(),
  64. })
  65. .optional(),
  66. title: z.string(),
  67. version: z.string(),
  68. time: z.object({
  69. created: z.number(),
  70. updated: z.number(),
  71. compacting: z.number().optional(),
  72. archived: z.number().optional(),
  73. }),
  74. permission: PermissionNext.Ruleset.optional(),
  75. revert: z
  76. .object({
  77. messageID: z.string(),
  78. partID: z.string().optional(),
  79. snapshot: z.string().optional(),
  80. diff: z.string().optional(),
  81. })
  82. .optional(),
  83. })
  84. .meta({
  85. ref: "Session",
  86. })
  87. export type Info = z.output<typeof Info>
  88. export const ShareInfo = z
  89. .object({
  90. secret: z.string(),
  91. url: z.string(),
  92. })
  93. .meta({
  94. ref: "SessionShare",
  95. })
  96. export type ShareInfo = z.output<typeof ShareInfo>
  97. export const Event = {
  98. Created: BusEvent.define(
  99. "session.created",
  100. z.object({
  101. info: Info,
  102. }),
  103. ),
  104. Updated: BusEvent.define(
  105. "session.updated",
  106. z.object({
  107. info: Info,
  108. }),
  109. ),
  110. Deleted: BusEvent.define(
  111. "session.deleted",
  112. z.object({
  113. info: Info,
  114. }),
  115. ),
  116. Diff: BusEvent.define(
  117. "session.diff",
  118. z.object({
  119. sessionID: z.string(),
  120. diff: Snapshot.FileDiff.array(),
  121. }),
  122. ),
  123. Error: BusEvent.define(
  124. "session.error",
  125. z.object({
  126. sessionID: z.string().optional(),
  127. error: MessageV2.Assistant.shape.error,
  128. }),
  129. ),
  130. }
  131. export const create = fn(
  132. z
  133. .object({
  134. parentID: Identifier.schema("session").optional(),
  135. title: z.string().optional(),
  136. permission: Info.shape.permission,
  137. })
  138. .optional(),
  139. async (input) => {
  140. return createNext({
  141. parentID: input?.parentID,
  142. directory: Instance.directory,
  143. title: input?.title,
  144. permission: input?.permission,
  145. })
  146. },
  147. )
  148. export const fork = fn(
  149. z.object({
  150. sessionID: Identifier.schema("session"),
  151. messageID: Identifier.schema("message").optional(),
  152. }),
  153. async (input) => {
  154. const original = await get(input.sessionID)
  155. if (!original) throw new Error("session not found")
  156. const title = getForkedTitle(original.title)
  157. const session = await createNext({
  158. directory: Instance.directory,
  159. title,
  160. })
  161. const msgs = await messages({ sessionID: input.sessionID })
  162. const idMap = new Map<string, string>()
  163. for (const msg of msgs) {
  164. if (input.messageID && msg.info.id >= input.messageID) break
  165. const newID = Identifier.ascending("message")
  166. idMap.set(msg.info.id, newID)
  167. const parentID = msg.info.role === "assistant" && msg.info.parentID ? idMap.get(msg.info.parentID) : undefined
  168. const cloned = await updateMessage({
  169. ...msg.info,
  170. sessionID: session.id,
  171. id: newID,
  172. ...(parentID && { parentID }),
  173. })
  174. for (const part of msg.parts) {
  175. await updatePart({
  176. ...part,
  177. id: Identifier.ascending("part"),
  178. messageID: cloned.id,
  179. sessionID: session.id,
  180. })
  181. }
  182. }
  183. return session
  184. },
  185. )
  186. export const touch = fn(Identifier.schema("session"), async (sessionID) => {
  187. await update(sessionID, (draft) => {
  188. draft.time.updated = Date.now()
  189. })
  190. })
  191. export async function createNext(input: {
  192. id?: string
  193. title?: string
  194. parentID?: string
  195. directory: string
  196. permission?: PermissionNext.Ruleset
  197. }) {
  198. const result: Info = {
  199. id: Identifier.descending("session", input.id),
  200. slug: Slug.create(),
  201. version: Installation.VERSION,
  202. projectID: Instance.project.id,
  203. directory: input.directory,
  204. parentID: input.parentID,
  205. title: input.title ?? createDefaultTitle(!!input.parentID),
  206. permission: input.permission,
  207. time: {
  208. created: Date.now(),
  209. updated: Date.now(),
  210. },
  211. }
  212. log.info("created", result)
  213. await Storage.write(["session", Instance.project.id, result.id], result)
  214. Bus.publish(Event.Created, {
  215. info: result,
  216. })
  217. const cfg = await Config.get()
  218. if (!result.parentID && (Flag.OPENCODE_AUTO_SHARE || cfg.share === "auto"))
  219. share(result.id)
  220. .then((share) => {
  221. update(result.id, (draft) => {
  222. draft.share = share
  223. })
  224. })
  225. .catch(() => {
  226. // Silently ignore sharing errors during session creation
  227. })
  228. Bus.publish(Event.Updated, {
  229. info: result,
  230. })
  231. return result
  232. }
  233. export function plan(input: { slug: string; time: { created: number } }) {
  234. const base = Instance.project.vcs
  235. ? path.join(Instance.worktree, ".opencode", "plans")
  236. : path.join(Global.Path.data, "plans")
  237. return path.join(base, [input.time.created, input.slug].join("-") + ".md")
  238. }
  239. export const get = fn(Identifier.schema("session"), async (id) => {
  240. const read = await Storage.read<Info>(["session", Instance.project.id, id])
  241. return read as Info
  242. })
  243. export const getShare = fn(Identifier.schema("session"), async (id) => {
  244. return Storage.read<ShareInfo>(["share", id])
  245. })
  246. export const share = fn(Identifier.schema("session"), async (id) => {
  247. const cfg = await Config.get()
  248. if (cfg.share === "disabled") {
  249. throw new Error("Sharing is disabled in configuration")
  250. }
  251. const { ShareNext } = await import("@/share/share-next")
  252. const share = await ShareNext.create(id)
  253. await update(
  254. id,
  255. (draft) => {
  256. draft.share = {
  257. url: share.url,
  258. }
  259. },
  260. { touch: false },
  261. )
  262. return share
  263. })
  264. export const unshare = fn(Identifier.schema("session"), async (id) => {
  265. // Use ShareNext to remove the share (same as share function uses ShareNext to create)
  266. const { ShareNext } = await import("@/share/share-next")
  267. await ShareNext.remove(id)
  268. await update(
  269. id,
  270. (draft) => {
  271. draft.share = undefined
  272. },
  273. { touch: false },
  274. )
  275. })
  276. export async function update(id: string, editor: (session: Info) => void, options?: { touch?: boolean }) {
  277. const project = Instance.project
  278. const result = await Storage.update<Info>(["session", project.id, id], (draft) => {
  279. editor(draft)
  280. if (options?.touch !== false) {
  281. draft.time.updated = Date.now()
  282. }
  283. })
  284. Bus.publish(Event.Updated, {
  285. info: result,
  286. })
  287. return result
  288. }
  289. export const diff = fn(Identifier.schema("session"), async (sessionID) => {
  290. const diffs = await Storage.read<Snapshot.FileDiff[]>(["session_diff", sessionID])
  291. return diffs ?? []
  292. })
  293. export const messages = fn(
  294. z.object({
  295. sessionID: Identifier.schema("session"),
  296. limit: z.number().optional(),
  297. }),
  298. async (input) => {
  299. const result = [] as MessageV2.WithParts[]
  300. for await (const msg of MessageV2.stream(input.sessionID)) {
  301. if (input.limit && result.length >= input.limit) break
  302. result.push(msg)
  303. }
  304. result.reverse()
  305. return result
  306. },
  307. )
  308. export async function* list() {
  309. const project = Instance.project
  310. for (const item of await Storage.list(["session", project.id])) {
  311. const session = await Storage.read<Info>(item).catch(() => undefined)
  312. if (!session) continue
  313. yield session
  314. }
  315. }
  316. export const children = fn(Identifier.schema("session"), async (parentID) => {
  317. const project = Instance.project
  318. const result = [] as Session.Info[]
  319. for (const item of await Storage.list(["session", project.id])) {
  320. const session = await Storage.read<Info>(item).catch(() => undefined)
  321. if (!session) continue
  322. if (session.parentID !== parentID) continue
  323. result.push(session)
  324. }
  325. return result
  326. })
  327. export const remove = fn(Identifier.schema("session"), async (sessionID) => {
  328. const project = Instance.project
  329. try {
  330. const session = await get(sessionID)
  331. for (const child of await children(sessionID)) {
  332. await remove(child.id)
  333. }
  334. await unshare(sessionID).catch(() => {})
  335. for (const msg of await Storage.list(["message", sessionID])) {
  336. for (const part of await Storage.list(["part", msg.at(-1)!])) {
  337. await Storage.remove(part)
  338. }
  339. await Storage.remove(msg)
  340. }
  341. await Storage.remove(["session", project.id, sessionID])
  342. Bus.publish(Event.Deleted, {
  343. info: session,
  344. })
  345. } catch (e) {
  346. log.error(e)
  347. }
  348. })
  349. export const updateMessage = fn(MessageV2.Info, async (msg) => {
  350. await Storage.write(["message", msg.sessionID, msg.id], msg)
  351. Bus.publish(MessageV2.Event.Updated, {
  352. info: msg,
  353. })
  354. return msg
  355. })
  356. export const removeMessage = fn(
  357. z.object({
  358. sessionID: Identifier.schema("session"),
  359. messageID: Identifier.schema("message"),
  360. }),
  361. async (input) => {
  362. await Storage.remove(["message", input.sessionID, input.messageID])
  363. Bus.publish(MessageV2.Event.Removed, {
  364. sessionID: input.sessionID,
  365. messageID: input.messageID,
  366. })
  367. return input.messageID
  368. },
  369. )
  370. export const removePart = fn(
  371. z.object({
  372. sessionID: Identifier.schema("session"),
  373. messageID: Identifier.schema("message"),
  374. partID: Identifier.schema("part"),
  375. }),
  376. async (input) => {
  377. await Storage.remove(["part", input.messageID, input.partID])
  378. Bus.publish(MessageV2.Event.PartRemoved, {
  379. sessionID: input.sessionID,
  380. messageID: input.messageID,
  381. partID: input.partID,
  382. })
  383. return input.partID
  384. },
  385. )
  386. const UpdatePartInput = z.union([
  387. MessageV2.Part,
  388. z.object({
  389. part: MessageV2.TextPart,
  390. delta: z.string(),
  391. }),
  392. z.object({
  393. part: MessageV2.ReasoningPart,
  394. delta: z.string(),
  395. }),
  396. ])
  397. export const updatePart = fn(UpdatePartInput, async (input) => {
  398. const part = "delta" in input ? input.part : input
  399. const delta = "delta" in input ? input.delta : undefined
  400. await Storage.write(["part", part.messageID, part.id], part)
  401. Bus.publish(MessageV2.Event.PartUpdated, {
  402. part,
  403. delta,
  404. })
  405. return part
  406. })
  407. export const getUsage = fn(
  408. z.object({
  409. model: z.custom<Provider.Model>(),
  410. usage: z.custom<LanguageModelV2Usage>(),
  411. metadata: z.custom<ProviderMetadata>().optional(),
  412. }),
  413. (input) => {
  414. const safe = (value: number) => {
  415. if (!Number.isFinite(value)) return 0
  416. return value
  417. }
  418. const inputTokens = safe(input.usage.inputTokens ?? 0)
  419. const outputTokens = safe(input.usage.outputTokens ?? 0)
  420. const reasoningTokens = safe(input.usage.reasoningTokens ?? 0)
  421. const cacheReadInputTokens = safe(input.usage.cachedInputTokens ?? 0)
  422. const cacheWriteInputTokens = safe(
  423. (input.metadata?.["anthropic"]?.["cacheCreationInputTokens"] ??
  424. // @ts-expect-error
  425. input.metadata?.["bedrock"]?.["usage"]?.["cacheWriteInputTokens"] ??
  426. // @ts-expect-error
  427. input.metadata?.["venice"]?.["usage"]?.["cacheCreationInputTokens"] ??
  428. 0) as number,
  429. )
  430. // OpenRouter provides inputTokens as the total count of input tokens (including cached).
  431. // AFAIK other providers (OpenRouter/OpenAI/Gemini etc.) do it the same way e.g. vercel/ai#8794 (comment)
  432. // Anthropic does it differently though - inputTokens doesn't include cached tokens.
  433. // It looks like OpenCode's cost calculation assumes all providers return inputTokens the same way Anthropic does (I'm guessing getUsage logic was originally implemented with anthropic), so it's causing incorrect cost calculation for OpenRouter and others.
  434. const excludesCachedTokens = !!(input.metadata?.["anthropic"] || input.metadata?.["bedrock"])
  435. const adjustedInputTokens = safe(
  436. excludesCachedTokens ? inputTokens : inputTokens - cacheReadInputTokens - cacheWriteInputTokens,
  437. )
  438. const total = iife(() => {
  439. // Anthropic doesn't provide total_tokens, also ai sdk will vastly undercount if we
  440. // don't compute from components
  441. if (
  442. input.model.api.npm === "@ai-sdk/anthropic" ||
  443. input.model.api.npm === "@ai-sdk/amazon-bedrock" ||
  444. input.model.api.npm === "@ai-sdk/google-vertex/anthropic"
  445. ) {
  446. return adjustedInputTokens + outputTokens + cacheReadInputTokens + cacheWriteInputTokens
  447. }
  448. return input.usage.totalTokens
  449. })
  450. const tokens = {
  451. total,
  452. input: adjustedInputTokens,
  453. output: outputTokens,
  454. reasoning: reasoningTokens,
  455. cache: {
  456. write: cacheWriteInputTokens,
  457. read: cacheReadInputTokens,
  458. },
  459. }
  460. const costInfo =
  461. input.model.cost?.experimentalOver200K && tokens.input + tokens.cache.read > 200_000
  462. ? input.model.cost.experimentalOver200K
  463. : input.model.cost
  464. return {
  465. cost: safe(
  466. new Decimal(0)
  467. .add(new Decimal(tokens.input).mul(costInfo?.input ?? 0).div(1_000_000))
  468. .add(new Decimal(tokens.output).mul(costInfo?.output ?? 0).div(1_000_000))
  469. .add(new Decimal(tokens.cache.read).mul(costInfo?.cache?.read ?? 0).div(1_000_000))
  470. .add(new Decimal(tokens.cache.write).mul(costInfo?.cache?.write ?? 0).div(1_000_000))
  471. // TODO: update models.dev to have better pricing model, for now:
  472. // charge reasoning tokens at the same rate as output tokens
  473. .add(new Decimal(tokens.reasoning).mul(costInfo?.output ?? 0).div(1_000_000))
  474. .toNumber(),
  475. ),
  476. tokens,
  477. }
  478. },
  479. )
  480. export class BusyError extends Error {
  481. constructor(public readonly sessionID: string) {
  482. super(`Session ${sessionID} is busy`)
  483. }
  484. }
  485. export const initialize = fn(
  486. z.object({
  487. sessionID: Identifier.schema("session"),
  488. modelID: z.string(),
  489. providerID: z.string(),
  490. messageID: Identifier.schema("message"),
  491. }),
  492. async (input) => {
  493. await SessionPrompt.command({
  494. sessionID: input.sessionID,
  495. messageID: input.messageID,
  496. model: input.providerID + "/" + input.modelID,
  497. command: Command.Default.INIT,
  498. arguments: "",
  499. })
  500. },
  501. )
  502. }