chatCache.ts 88 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467
  1. import { projectChatEvent } from './chatEventProjection.js'
  2. import { isFinalChatResult } from './chatUnread.js'
  3. import { HttpError } from './errors.js'
  4. import { createHash, randomUUID } from 'node:crypto'
  5. import { chmodSync, createReadStream, existsSync, mkdirSync, renameSync, statSync, unlinkSync, writeFileSync } from 'node:fs'
  6. import { join } from 'node:path'
  7. import { DatabaseSync } from 'node:sqlite'
  8. import type { UpstreamResponse } from './upstream.js'
  9. import type { UpstreamServiceSession } from './localAuth.js'
  10. export type ChatCacheMode = 'upstream-only' | 'shadow' | 'prefer-local'
  11. export type ChatCacheState = 'current' | 'stale' | 'syncing' | 'gap'
  12. export type ChatCacheSource = 'local' | 'upstream'
  13. interface StoredSnapshot {
  14. response: UpstreamResponse
  15. state: ChatCacheState
  16. }
  17. interface MessagePage {
  18. response: UpstreamResponse
  19. state: ChatCacheState
  20. }
  21. function object(value: unknown): Record<string, unknown> | undefined {
  22. return value && typeof value === 'object' && !Array.isArray(value)
  23. ? value as Record<string, unknown> : undefined
  24. }
  25. function parseResponse(response: UpstreamResponse): Record<string, unknown> | undefined {
  26. if (response.status < 200 || response.status >= 300) return undefined
  27. try { return object(JSON.parse(response.body.toString('utf8'))) } catch { return undefined }
  28. }
  29. function headerRecord(headers: Headers): string {
  30. return JSON.stringify(Object.fromEntries([...headers.entries()].filter(([name]) => name.toLowerCase() !== 'set-cookie')))
  31. }
  32. function responseFromRow(row: Record<string, unknown>): UpstreamResponse {
  33. return {
  34. status: Number(row.status),
  35. headers: new Headers(JSON.parse(String(row.headers)) as Record<string, string>),
  36. body: Buffer.from(String(row.body), 'base64'),
  37. }
  38. }
  39. function profileOf(value: Record<string, unknown>, fallback = ''): string {
  40. return String(value.profile ?? value.profile_name ?? fallback).trim()
  41. }
  42. function sessionIDOf(value: Record<string, unknown>): string {
  43. return String(value.id ?? value.session_id ?? value.sessionId ?? '').trim()
  44. }
  45. function sessionSource(value: Record<string, unknown>): string {
  46. return String(value.source ?? '').trim()
  47. }
  48. function sessionTitle(value: Record<string, unknown>): string {
  49. return String(value.title ?? '').trim()
  50. }
  51. // Hermes uses Unix seconds; local route registration uses milliseconds. Keep
  52. // the public summary in seconds so Web and iOS compare the same activity time.
  53. function timestampSeconds(value: unknown): number | undefined {
  54. if (typeof value !== 'number' && typeof value !== 'string') return undefined
  55. const numeric = Number(value)
  56. const seconds = Number.isFinite(numeric)
  57. ? numeric > 10_000_000_000 ? numeric / 1_000 : numeric
  58. : typeof value === 'string' ? Date.parse(value) / 1_000 : NaN
  59. return Number.isFinite(seconds) && seconds > 0 ? seconds : undefined
  60. }
  61. function firstTimestamp(value: Record<string, unknown>, keys: string[]): number | undefined {
  62. for (const key of keys) {
  63. const timestamp = timestampSeconds(value[key])
  64. if (timestamp !== undefined) return timestamp
  65. }
  66. return undefined
  67. }
  68. const START_TIME_KEYS = ['started_at', 'startedAt', 'session_started', 'created_at', 'createdAt']
  69. const ACTIVITY_TIME_KEYS = ['last_active', 'last_active_at', 'last_activity_at', 'lastActive', 'updated_at', 'updatedAt']
  70. function mergeSessionMetadata(previous: Record<string, unknown>, incoming: Record<string, unknown>): Record<string, unknown> {
  71. const summary = { ...previous, ...incoming }
  72. // A messages response can carry title/model metadata, but its envelope is
  73. // not a replacement for the session detail.
  74. delete summary.messages
  75. delete summary.pagination
  76. const started = firstTimestamp(incoming, START_TIME_KEYS) ?? firstTimestamp(previous, START_TIME_KEYS)
  77. const activity = firstTimestamp(incoming, ACTIVITY_TIME_KEYS) ?? firstTimestamp(previous, ACTIVITY_TIME_KEYS)
  78. if (started !== undefined) summary.started_at = started
  79. if (activity !== undefined) summary.last_active = activity
  80. return summary
  81. }
  82. function messageTimeBounds(messages: Record<string, unknown>[]): { first?: number; last?: number } {
  83. let first: number | undefined
  84. let last: number | undefined
  85. for (const message of messages) {
  86. const time = firstTimestamp(message, ['timestamp', 'created_at', 'createdAt'])
  87. if (time === undefined) continue
  88. first = Math.min(first ?? time, time)
  89. last = Math.max(last ?? time, time)
  90. }
  91. return { first, last }
  92. }
  93. function isPlaceholderTitle(value: string): boolean {
  94. return new Set([
  95. '', '新对话', '新会话', '未命名对话', '未命名会话', 'new conversation', 'new session', 'untitled',
  96. ]).has(value.trim().toLowerCase())
  97. }
  98. function messageID(value: Record<string, unknown>, fallback: string): string {
  99. const id = String(value.id ?? value.row_id ?? value.message_id ?? value.messageId ?? '').trim()
  100. return id || createHash('sha256').update(JSON.stringify(value) + fallback).digest('hex')
  101. }
  102. function bool(value: unknown): boolean {
  103. return value === true || value === 1 || value === '1' || value === 'true'
  104. }
  105. const PATH_PATTERN = /(?:file:\/\/)?(\/(?:Users|private|var|tmp)\/[^\s<>'"`)\]}]+\.[A-Za-z0-9]{1,12})/g
  106. function referencedPaths(value: unknown, output = new Set<string>()): Set<string> {
  107. if (typeof value === 'string') {
  108. for (const match of value.matchAll(PATH_PATTERN)) output.add(match[1]!)
  109. } else if (Array.isArray(value)) {
  110. for (const child of value) referencedPaths(child, output)
  111. } else {
  112. const record = object(value)
  113. if (record) for (const child of Object.values(record)) referencedPaths(child, output)
  114. }
  115. return output
  116. }
  117. export class ChatCacheStore {
  118. readonly db: DatabaseSync
  119. readonly assetsRoot: string
  120. private closed = false
  121. get isClosed(): boolean { return this.closed }
  122. constructor(home: string, legacySingleUserID?: string) {
  123. mkdirSync(home, { recursive: true, mode: 0o700 })
  124. const path = join(home, 'chat-cache.sqlite3')
  125. this.db = new DatabaseSync(path)
  126. chmodSync(path, 0o600)
  127. this.assetsRoot = join(home, 'chat-cache-assets')
  128. mkdirSync(this.assetsRoot, { recursive: true, mode: 0o700 })
  129. this.db.exec(`PRAGMA journal_mode=WAL; PRAGMA foreign_keys=ON; PRAGMA busy_timeout=5000;
  130. CREATE TABLE IF NOT EXISTS chat_sessions(
  131. owner TEXT NOT NULL, profile TEXT NOT NULL, session_id TEXT NOT NULL,
  132. runtime_id TEXT, source TEXT NOT NULL, data TEXT NOT NULL, authoritative_title TEXT,
  133. authoritative_title_at INTEGER NOT NULL DEFAULT 0,
  134. owned_at INTEGER, ownership_evidence TEXT,
  135. sync_state TEXT NOT NULL DEFAULT 'stale', complete INTEGER NOT NULL DEFAULT 0,
  136. last_event_seq INTEGER NOT NULL DEFAULT 0, last_synced_at INTEGER NOT NULL,
  137. PRIMARY KEY(owner,profile,session_id));
  138. CREATE TABLE IF NOT EXISTS chat_messages(
  139. owner TEXT NOT NULL, profile TEXT NOT NULL, session_id TEXT NOT NULL,
  140. message_id TEXT NOT NULL, position INTEGER NOT NULL, data TEXT NOT NULL,
  141. content_hash TEXT NOT NULL, PRIMARY KEY(owner,profile,session_id,message_id));
  142. CREATE INDEX IF NOT EXISTS chat_messages_position ON chat_messages(owner,profile,session_id,position);
  143. CREATE TABLE IF NOT EXISTS chat_events(
  144. owner TEXT NOT NULL, profile TEXT NOT NULL, session_id TEXT NOT NULL,
  145. event_seq INTEGER NOT NULL, event_type TEXT NOT NULL, payload TEXT NOT NULL,
  146. received_at INTEGER NOT NULL, PRIMARY KEY(owner,profile,session_id,event_seq,event_type));
  147. CREATE TABLE IF NOT EXISTS chat_attachments(
  148. owner TEXT NOT NULL, profile TEXT NOT NULL, session_id TEXT NOT NULL,
  149. source_path TEXT NOT NULL, sha256 TEXT, mime_type TEXT, size INTEGER,
  150. local_path TEXT, state TEXT NOT NULL DEFAULT 'pending', updated_at INTEGER NOT NULL,
  151. PRIMARY KEY(owner,profile,session_id,source_path));
  152. CREATE INDEX IF NOT EXISTS chat_attachments_source ON chat_attachments(owner,source_path);
  153. CREATE TABLE IF NOT EXISTS chat_sync_cursors(
  154. owner TEXT NOT NULL, profile TEXT NOT NULL, generation TEXT,
  155. event_cursor INTEGER NOT NULL DEFAULT 0, sync_state TEXT NOT NULL DEFAULT 'stale',
  156. last_synced_at INTEGER NOT NULL, PRIMARY KEY(owner,profile));
  157. CREATE TABLE IF NOT EXISTS chat_snapshots(
  158. owner TEXT NOT NULL, cache_key TEXT NOT NULL, kind TEXT NOT NULL,
  159. profile TEXT NOT NULL, session_id TEXT, status INTEGER NOT NULL,
  160. headers TEXT NOT NULL, body TEXT NOT NULL, sync_state TEXT NOT NULL,
  161. saved_at INTEGER NOT NULL, PRIMARY KEY(owner,cache_key));
  162. CREATE TABLE IF NOT EXISTS chat_meta(
  163. key TEXT PRIMARY KEY, value TEXT NOT NULL);`)
  164. this.db.exec(`CREATE TABLE IF NOT EXISTS chat_local_state(
  165. owner TEXT NOT NULL, profile TEXT NOT NULL, session_id TEXT NOT NULL,
  166. pinned INTEGER, read_count INTEGER NOT NULL DEFAULT 0,
  167. assistant_id TEXT, PRIMARY KEY(owner,profile,session_id));
  168. CREATE TABLE IF NOT EXISTS chat_session_aliases(owner TEXT NOT NULL,profile TEXT NOT NULL,alias_id TEXT NOT NULL,session_id TEXT NOT NULL,PRIMARY KEY(owner,profile,alias_id));
  169. CREATE TABLE IF NOT EXISTS chat_recovery_jobs(owner TEXT NOT NULL,profile TEXT NOT NULL,session_id TEXT NOT NULL,force INTEGER NOT NULL DEFAULT 0,PRIMARY KEY(owner,profile,session_id));
  170. CREATE TABLE IF NOT EXISTS chat_event_receipts(
  171. owner TEXT NOT NULL, profile TEXT NOT NULL, session_id TEXT NOT NULL, event_id TEXT NOT NULL,
  172. PRIMARY KEY(owner,profile,session_id,event_id));`)
  173. const localColumns=this.db.prepare('PRAGMA table_info(chat_local_state)').all() as {name:string}[]
  174. for(const [name,type] of [['title','TEXT'],['archived','INTEGER'],['read_initialized','INTEGER NOT NULL DEFAULT 0'],['final_read_count','INTEGER']]){
  175. if(!localColumns.some(c=>c.name===name))this.db.exec(`ALTER TABLE chat_local_state ADD COLUMN ${name} ${type}`)
  176. }
  177. const sessionColumns = this.db.prepare('PRAGMA table_info(chat_sessions)').all() as Array<{ name: string }>
  178. this.db.exec('BEGIN IMMEDIATE')
  179. try {
  180. if (!sessionColumns.some(column => column.name === 'sync_revision')) {
  181. this.db.exec('ALTER TABLE chat_sessions ADD COLUMN sync_revision INTEGER NOT NULL DEFAULT 0')
  182. }
  183. if (!sessionColumns.some(column => column.name === 'message_total')) {
  184. this.db.exec('ALTER TABLE chat_sessions ADD COLUMN message_total INTEGER')
  185. }
  186. if (!sessionColumns.some(column => column.name === 'metadata_complete')) {
  187. this.db.exec('ALTER TABLE chat_sessions ADD COLUMN metadata_complete INTEGER NOT NULL DEFAULT 0')
  188. this.db.exec("UPDATE chat_sessions SET metadata_complete=1 WHERE json_valid(data) AND (json_type(data,'$.message_count') IS NOT NULL OR json_type(data,'$.model') IS NOT NULL)")
  189. }
  190. if (!sessionColumns.some(column => column.name === 'authoritative_title')) {
  191. this.db.exec('ALTER TABLE chat_sessions ADD COLUMN authoritative_title TEXT')
  192. }
  193. if (!sessionColumns.some(column => column.name === 'authoritative_title_at')) {
  194. this.db.exec('ALTER TABLE chat_sessions ADD COLUMN authoritative_title_at INTEGER NOT NULL DEFAULT 0')
  195. }
  196. if (!sessionColumns.some(column => column.name === 'owned_at')) {
  197. this.db.exec('ALTER TABLE chat_sessions ADD COLUMN owned_at INTEGER')
  198. }
  199. if (!sessionColumns.some(column => column.name === 'ownership_evidence')) {
  200. this.db.exec('ALTER TABLE chat_sessions ADD COLUMN ownership_evidence TEXT')
  201. }
  202. this.db.exec(`CREATE INDEX IF NOT EXISTS chat_sessions_owned
  203. ON chat_sessions(owner,profile,owned_at,last_synced_at)`)
  204. const ownershipMigration = this.db.prepare(
  205. "SELECT value FROM chat_meta WHERE key='ownership-registry-v1'",
  206. ).get() as { value?: string } | undefined
  207. if (!ownershipMigration) {
  208. // A single cached owner is trusted only when the persisted local user
  209. // database independently confirms the same sole identity.
  210. const legacyOwners = this.db.prepare(
  211. 'SELECT DISTINCT owner FROM chat_sessions ORDER BY owner',
  212. ).all() as Array<{ owner: string }>
  213. if (legacyOwners.length === 1 && legacyOwners[0]!.owner === legacySingleUserID) {
  214. this.db.exec(`UPDATE chat_sessions SET
  215. owned_at=COALESCE(owned_at,last_synced_at),
  216. ownership_evidence=COALESCE(ownership_evidence,'legacy-single-user')`)
  217. }
  218. }
  219. // Recover interrupted/new-schema writes only from explicit command proof.
  220. this.db.exec(`UPDATE chat_sessions SET
  221. owned_at=COALESCE(owned_at,(
  222. SELECT MIN(chat_events.received_at) FROM chat_events
  223. WHERE chat_events.owner=chat_sessions.owner
  224. AND chat_events.profile=chat_sessions.profile
  225. AND chat_events.session_id=chat_sessions.session_id
  226. AND chat_events.event_type IN ('command:session.create','command:session.branch')
  227. )),
  228. ownership_evidence=COALESCE(ownership_evidence,'command-migration')
  229. WHERE EXISTS (
  230. SELECT 1 FROM chat_events
  231. WHERE chat_events.owner=chat_sessions.owner
  232. AND chat_events.profile=chat_sessions.profile
  233. AND chat_events.session_id=chat_sessions.session_id
  234. AND chat_events.event_type IN ('command:session.create','command:session.branch')
  235. )`)
  236. const legacyTitles = this.db.prepare(`SELECT owner,profile,session_id,data FROM chat_sessions
  237. WHERE authoritative_title IS NULL`).all() as Array<{
  238. owner: string; profile: string; session_id: string; data: string
  239. }>
  240. const backfillTitle = this.db.prepare(`UPDATE chat_sessions SET authoritative_title=?
  241. WHERE owner=? AND profile=? AND session_id=? AND authoritative_title IS NULL`)
  242. for (const row of legacyTitles) {
  243. let summary: Record<string, unknown> = {}
  244. try { summary = object(JSON.parse(row.data)) ?? {} } catch { continue }
  245. const title = sessionTitle(summary)
  246. if (!isPlaceholderTitle(title)) backfillTitle.run(title, row.owner, row.profile, row.session_id)
  247. }
  248. if (!this.db.prepare("SELECT 1 FROM chat_meta WHERE key='session-activity-v1'").get()) {
  249. const rows = this.db.prepare(`SELECT owner,profile,session_id,source,data,owned_at,message_total
  250. FROM chat_sessions WHERE owned_at IS NOT NULL`).all() as Array<{
  251. owner: string; profile: string; session_id: string; source: string; data: string
  252. owned_at: number; message_total: number | null
  253. }>
  254. const update = this.db.prepare(`UPDATE chat_sessions SET data=? WHERE owner=? AND profile=? AND session_id=?`)
  255. for (const row of rows) {
  256. let stored: Record<string, unknown>
  257. try { stored = object(JSON.parse(row.data)) ?? {} } catch { continue }
  258. const summary = mergeSessionMetadata({}, stored)
  259. if (Array.isArray(stored.messages) || !firstTimestamp(summary, START_TIME_KEYS)
  260. || !firstTimestamp(summary, ACTIVITY_TIME_KEYS)) {
  261. const messages = this.db.prepare(`SELECT data FROM chat_messages WHERE owner=? AND profile=? AND session_id=?`)
  262. .all(row.owner, row.profile, row.session_id) as Array<{ data: string }>
  263. const bounds = messageTimeBounds(messages.flatMap(message => {
  264. try { return [object(JSON.parse(message.data)) ?? {}] } catch { return [] }
  265. }))
  266. summary.started_at = firstTimestamp(summary, START_TIME_KEYS) ?? bounds.first ?? timestampSeconds(row.owned_at)
  267. summary.last_active = Math.max(firstTimestamp(summary, ACTIVITY_TIME_KEYS) ?? 0,
  268. bounds.last ?? 0) || summary.started_at
  269. if (row.message_total !== null) summary.message_count = row.message_total
  270. }
  271. summary.id = row.session_id
  272. summary.profile = row.profile
  273. summary.source = sessionSource(summary) || row.source
  274. update.run(JSON.stringify(summary), row.owner, row.profile, row.session_id)
  275. }
  276. this.db.prepare("INSERT INTO chat_meta(key,value) VALUES('session-activity-v1','complete')").run()
  277. }
  278. // List bodies written before ownership became an explicit projection do
  279. // not carry `owned` and may have been filtered by `source`. Rebuild them on
  280. // first use after every restart instead of serving a semantically old page.
  281. this.db.prepare("UPDATE chat_snapshots SET sync_state='stale' WHERE kind='list'").run()
  282. this.db.prepare(`INSERT INTO chat_meta(key,value) VALUES('ownership-registry-v1','complete')
  283. ON CONFLICT(key) DO UPDATE SET value=excluded.value`).run()
  284. this.db.prepare(`INSERT OR IGNORE INTO chat_local_state(owner,profile,session_id)
  285. SELECT owner,profile,session_id FROM chat_sessions WHERE owned_at IS NOT NULL`).run()
  286. this.db.exec('COMMIT')
  287. } catch (error) {
  288. this.db.exec('ROLLBACK')
  289. throw error
  290. }
  291. }
  292. close(): void { this.closed = true; this.db.close() }
  293. stats(): Record<string, number> {
  294. const count = (table: string) => Number((this.db.prepare(`SELECT COUNT(*) count FROM ${table}`).get() as { count: number }).count)
  295. const bytes = Number((this.db.prepare(`SELECT COALESCE(SUM(size),0) bytes FROM chat_attachments WHERE state='cached'`).get() as { bytes: number }).bytes)
  296. return {
  297. sessions: Number((this.db.prepare(`SELECT COUNT(*) count FROM chat_sessions
  298. WHERE owned_at IS NOT NULL`).get() as { count: number }).count),
  299. messages: count('chat_messages'),
  300. events: count('chat_events'),
  301. attachments: count('chat_attachments'),
  302. attachmentBytes: bytes,
  303. }
  304. }
  305. snapshot(owner: string, key: string): StoredSnapshot | undefined {
  306. const row = this.db.prepare('SELECT status,headers,body,sync_state FROM chat_snapshots WHERE owner=? AND cache_key=?')
  307. .get(owner, key) as Record<string, unknown> | undefined
  308. return row ? { response: responseFromRow(row), state: String(row.sync_state) as ChatCacheState } : undefined
  309. }
  310. localList(owner: string, profile: string, page: { offset?: number; limit?: number; archived?: string }): UpstreamResponse {
  311. const offset = Math.max(0, Math.trunc(page.offset ?? 0)), limit = Math.max(1, Math.min(200, Math.trunc(page.limit ?? 100)))
  312. const where = `s.owner=? AND s.owned_at IS NOT NULL AND (?='' OR s.profile=?)
  313. AND s.source NOT IN ('cron','ios_group','yaoyao_workspace')
  314. AND (?='include' OR COALESCE(l.archived,json_extract(s.data,'$.archived'),json_extract(s.data,'$.is_archived'),0)=CASE WHEN ?='only' THEN 1 ELSE 0 END)`
  315. const args = [owner, profile, profile, page.archived ?? 'exclude', page.archived ?? 'exclude']
  316. const total = Number((this.db.prepare(`SELECT count(*) n FROM chat_sessions s LEFT JOIN chat_local_state l USING(owner,profile,session_id) WHERE ${where}`).get(...args) as {n:number}).n)
  317. const rows = this.db.prepare(`SELECT s.profile,s.session_id,s.data,s.sync_state,s.complete,l.title local_title,
  318. COALESCE(l.pinned,json_extract(s.data,'$.pinned'),json_extract(s.data,'$.is_pinned'),0) pin
  319. FROM chat_sessions s LEFT JOIN chat_local_state l USING(owner,profile,session_id) WHERE ${where}
  320. ORDER BY pin DESC, COALESCE(json_extract(s.data,'$.last_active'),0) DESC, s.session_id
  321. LIMIT ? OFFSET ?`).all(...args, limit, offset) as Array<Record<string,any>>
  322. const unread = new Map(this.unread(owner, profile).sessions.map(row => [JSON.stringify([row.profile, row.session_id]), row.final_unread_count]))
  323. return {status:200,headers:new Headers({'content-type':'application/json'}),body:Buffer.from(JSON.stringify({
  324. sessions:rows.map(r=>({...JSON.parse(r.data),...(r.local_title!=null?{title:r.local_title}:{}),id:r.session_id,profile:r.profile,owned:true,pinned:!!r.pin,
  325. final_unread_count:unread.get(JSON.stringify([r.profile,r.session_id]))??0,
  326. sync_state:r.sync_state,history_complete:!!r.complete})), total,offset,limit,
  327. sync_state:this.needsListMetadata(owner,profile)?'syncing':'current'
  328. }))}
  329. }
  330. missingSessions(owner: string, profile: string): Array<{profile:string;session_id:string}> {
  331. return (this.db.prepare(`SELECT profile,session_id FROM chat_sessions WHERE owner=? AND owned_at IS NOT NULL
  332. AND (?='' OR profile=?) AND (metadata_complete=0 OR complete=0) LIMIT 100`)
  333. .all(owner,profile,profile) as {profile:string;session_id:string}[])
  334. }
  335. pins(owner: string, profile: string): string[] {
  336. return (this.db.prepare(`SELECT s.session_id FROM chat_sessions s LEFT JOIN chat_local_state l USING(owner,profile,session_id)
  337. WHERE s.owner=? AND s.owned_at IS NOT NULL AND (?='' OR s.profile=?)
  338. AND COALESCE(l.pinned,json_extract(s.data,'$.pinned'),json_extract(s.data,'$.is_pinned'),0)=1 ORDER BY s.last_synced_at DESC`)
  339. .all(owner,profile,profile) as {session_id:string}[]).map(r=>r.session_id)
  340. }
  341. unread(owner: string, profile: string) {
  342. const sessions = (this.db.prepare(`SELECT s.profile,s.session_id,COALESCE(s.message_total,0) message_count,
  343. CASE WHEN COALESCE(l.read_initialized,0)=1 THEN COALESCE(l.final_read_count,l.read_count) ELSE COALESCE(s.message_total,0) END final_read_count,
  344. CASE WHEN COALESCE(l.read_initialized,0)=1 THEN l.read_count ELSE COALESCE(s.message_total,0) END read_message_count FROM chat_sessions s LEFT JOIN chat_local_state l USING(owner,profile,session_id)
  345. WHERE s.owner=? AND s.owned_at IS NOT NULL AND (?='' OR s.profile=?)`).all(owner,profile,profile) as any[])
  346. .map(({final_read_count,...r})=>{
  347. const messages = this.db.prepare(`SELECT data FROM chat_messages
  348. WHERE owner=? AND profile=? AND session_id=? AND position>=?
  349. AND json_extract(data,'$.role')='assistant'`)
  350. .all(owner,r.profile,r.session_id,final_read_count) as {data:string}[]
  351. return {...r,updated_at:Date.now(),unread_count:Math.max(0,r.message_count-r.read_message_count),
  352. final_unread_count:messages.filter(row=>isFinalChatResult(JSON.parse(row.data))).length}
  353. })
  354. return {profile,supported:true,total_unread:sessions.reduce((n,r)=>n+r.unread_count,0),sessions}
  355. }
  356. markRead(owner: string, profile: string, id: string, count?: number) {
  357. id=this.canonicalSessionID(owner,profile,id)
  358. this.requireOwned(owner,profile,id)
  359. const total = Number((this.db.prepare('SELECT message_total FROM chat_sessions WHERE owner=? AND profile=? AND session_id=?')
  360. .get(owner,profile,id) as any).message_total ?? 0)
  361. const read = Math.max(0,Math.min(total, count ?? total))
  362. // Reading a streaming placeholder must not consume its future final answer.
  363. // Keep the original cursor untouched for existing consumers.
  364. const pending = this.db.prepare(`SELECT MIN(position) position FROM chat_messages
  365. WHERE owner=? AND profile=? AND session_id=? AND position<?
  366. AND message_id=(SELECT assistant_id FROM chat_local_state WHERE owner=? AND profile=? AND session_id=?)
  367. AND json_extract(data,'$.role')='assistant' AND json_extract(data,'$.status')='streaming'`)
  368. .get(owner,profile,id,read,owner,profile,id) as {position:number|null}
  369. const finalRead = pending.position ?? read
  370. this.db.prepare(`INSERT INTO chat_local_state(owner,profile,session_id,read_count,final_read_count,read_initialized) VALUES(?,?,?,?,?,1)
  371. ON CONFLICT(owner,profile,session_id) DO UPDATE SET read_count=MAX(read_count,excluded.read_count),
  372. final_read_count=MAX(COALESCE(final_read_count,read_count),excluded.final_read_count),read_initialized=1`).run(owner,profile,id,read,finalRead)
  373. return this.unread(owner,profile).sessions.find(r=>r.session_id===id)!
  374. }
  375. requireOwned(owner: string, profile: string, id: string): void {
  376. if (!this.ownsSession(owner,profile,id)) throw new HttpError(404,'会话不存在','session_not_found')
  377. }
  378. patchLocal(owner: string, profile: string, id: string, patch: Record<string,unknown>): void {
  379. id=this.canonicalSessionID(owner,profile,id)
  380. this.requireOwned(owner,profile,id)
  381. this.db.exec('SAVEPOINT local_chat_patch')
  382. try {
  383. if (typeof patch.pinned==='boolean') this.db.prepare(`INSERT INTO chat_local_state(owner,profile,session_id,pinned) VALUES(?,?,?,?)
  384. ON CONFLICT(owner,profile,session_id) DO UPDATE SET pinned=excluded.pinned`).run(owner,profile,id,patch.pinned?1:0)
  385. const row = this.localDetail(owner,profile,id)!
  386. const data = {...JSON.parse(row.response.body.toString()),...patch}
  387. this.db.prepare('UPDATE chat_sessions SET data=? WHERE owner=? AND profile=? AND session_id=?').run(JSON.stringify(data),owner,profile,id)
  388. for(const key of ['title','archived'] as const)if(patch[key]!==undefined){
  389. const value=key==='archived'?(patch[key]?1:0):String(patch[key])
  390. this.db.prepare(`INSERT INTO chat_local_state(owner,profile,session_id,${key}) VALUES(?,?,?,?) ON CONFLICT(owner,profile,session_id) DO UPDATE SET ${key}=excluded.${key}`).run(owner,profile,id,value)
  391. }
  392. if(typeof patch.title==='string') this.recordAuthoritativeTitle(owner,profile,id,patch.title)
  393. this.db.prepare("UPDATE chat_sessions SET sync_state=CASE WHEN complete=1 THEN 'current' ELSE sync_state END WHERE owner=? AND profile=? AND session_id=?").run(owner,profile,id)
  394. this.db.exec('RELEASE local_chat_patch')
  395. } catch(e) {this.db.exec('ROLLBACK TO local_chat_patch; RELEASE local_chat_patch');throw e}
  396. }
  397. private writeLiveMessage(owner:string,profile:string,id:string,message:Record<string,any>): void {
  398. const prior = this.db.prepare('SELECT position FROM chat_messages WHERE owner=? AND profile=? AND session_id=? AND message_id=?').get(owner,profile,id,message.id) as {position:number}|undefined
  399. const row = this.db.prepare('SELECT message_total,complete,data FROM chat_sessions WHERE owner=? AND profile=? AND session_id=?').get(owner,profile,id) as any
  400. const extent = Number((this.db.prepare('SELECT COALESCE(MAX(position)+1,0) n FROM chat_messages WHERE owner=? AND profile=? AND session_id=?').get(owner,profile,id) as any).n)
  401. const position = prior?.position ?? Math.max(extent,Number(row.message_total??0))
  402. const data=JSON.stringify(message)
  403. this.db.prepare(`INSERT INTO chat_messages(owner,profile,session_id,message_id,position,data,content_hash) VALUES(?,?,?,?,?,?,?)
  404. ON CONFLICT(owner,profile,session_id,message_id) DO UPDATE SET data=excluded.data,content_hash=excluded.content_hash`).run(owner,profile,id,message.id,position,data,createHash('sha256').update(data).digest('hex'))
  405. const total=Math.max(Number(row.message_total??0),position+1), summary=JSON.parse(row.data)
  406. const now=Date.now()
  407. summary.message_count=total; summary.last_active=now/1000
  408. if (typeof message.content==='string' && message.content.trim()) summary.preview=message.content.slice(0,160)
  409. this.db.prepare('UPDATE chat_sessions SET data=?,message_total=?,last_synced_at=? WHERE owner=? AND profile=? AND session_id=?').run(JSON.stringify(summary),total,now,owner,profile,id)
  410. for(const path of referencedPaths(message)) this.db.prepare(`INSERT OR IGNORE INTO chat_attachments(owner,profile,session_id,source_path,state,updated_at) VALUES(?,?,?,?,'pending',?)`).run(owner,profile,id,path,now)
  411. }
  412. projectedMessageID(owner:string,profile:string,id:string):string|undefined {
  413. return (this.db.prepare(`SELECT message_id FROM chat_messages WHERE owner=? AND profile=? AND session_id=?
  414. AND json_extract(data,'$.role')='assistant' ORDER BY position DESC LIMIT 1`).get(owner,profile,id) as {message_id:string}|undefined)?.message_id
  415. }
  416. private projectEvent(owner:string,profile:string,id:string,type:string,p:Record<string,any>,eventID:string): void {
  417. if(!this.ownsSession(owner,profile,id))return
  418. if(type==='command.confirmed'||type==='command.rejected'){
  419. const userID=`user:${p.delivery_id}`
  420. const row=this.db.prepare('SELECT data FROM chat_messages WHERE owner=? AND profile=? AND session_id=? AND message_id=?').get(owner,profile,id,userID) as any
  421. if(row)this.writeLiveMessage(owner,profile,id,{...JSON.parse(row.data),status:type==='command.confirmed'?'complete':'failed',...(p.error?{error:p.error}:{})})
  422. this.db.prepare("UPDATE chat_sessions SET sync_state=CASE WHEN sync_state='gap' THEN 'gap' WHEN complete=1 THEN 'current' ELSE 'stale' END WHERE owner=? AND profile=? AND session_id=?").run(owner,profile,id)
  423. return
  424. }
  425. if (type==='message.user' || type==='peer_user_message') {
  426. this.writeLiveMessage(owner,profile,id,{...p,id:String(p.message_id??p.id??eventID),role:'user',content:p.text??p.content??'',timestamp:Date.now()/1000});return
  427. }
  428. const state=this.db.prepare('SELECT assistant_id FROM chat_local_state WHERE owner=? AND profile=? AND session_id=?').get(owner,profile,id) as any
  429. const last=state?.assistant_id?this.db.prepare('SELECT data FROM chat_messages WHERE owner=? AND profile=? AND session_id=? AND message_id=?').get(owner,profile,id,state.assistant_id) as any:undefined
  430. const previous=last?JSON.parse(last.data):undefined
  431. // A new run must never append to the assistant row from the preceding turn.
  432. if(['message.complete','run.completed'].includes(type) && !previous && !p.text && !p.content && !p.message){
  433. this.db.prepare("UPDATE chat_sessions SET sync_state='gap' WHERE owner=? AND profile=? AND session_id=?").run(owner,profile,id);return
  434. }
  435. const startsNew=type==='message.start' && previous?.status!=='streaming'
  436. const message=projectChatEvent(type,p,startsNew?undefined:previous,`event:${eventID}`,Date.now())
  437. if(message){
  438. if(previous && previous.id !== message.id && !startsNew){
  439. const exists=this.db.prepare('SELECT 1 FROM chat_messages WHERE owner=? AND profile=? AND session_id=? AND message_id=?').get(owner,profile,id,message.id)
  440. if(!exists)this.db.prepare('UPDATE chat_messages SET message_id=? WHERE owner=? AND profile=? AND session_id=? AND message_id=?').run(message.id,owner,profile,id,previous.id)
  441. else {this.db.prepare("UPDATE chat_sessions SET sync_state='gap' WHERE owner=? AND profile=? AND session_id=?").run(owner,profile,id);return}
  442. }
  443. this.writeLiveMessage(owner,profile,id,message)
  444. this.db.prepare(`INSERT INTO chat_local_state(owner,profile,session_id,assistant_id) VALUES(?,?,?,?)
  445. ON CONFLICT(owner,profile,session_id) DO UPDATE SET assistant_id=excluded.assistant_id`).run(owner,profile,id,type==='message.interim'?null:message.id)
  446. this.db.prepare(`UPDATE chat_sessions SET sync_state=CASE WHEN sync_state='gap' THEN 'gap' WHEN complete=1 THEN 'current' ELSE 'stale' END
  447. WHERE owner=? AND profile=? AND session_id=?`).run(owner,profile,id)
  448. }
  449. }
  450. needsListMetadata(owner: string, profile: string): boolean {
  451. return Boolean(this.db.prepare(`SELECT 1 FROM chat_sessions WHERE owner=? AND owned_at IS NOT NULL
  452. AND (?='' OR profile=?) AND metadata_complete=0 LIMIT 1`).get(owner, profile, profile))
  453. }
  454. localDetail(owner: string, profile: string, sessionID: string): StoredSnapshot | undefined {
  455. const row = this.db.prepare(`SELECT data,sync_state FROM chat_sessions
  456. WHERE owner=? AND profile=? AND session_id=? AND owned_at IS NOT NULL`)
  457. .get(owner, profile, sessionID) as { data: string; sync_state: ChatCacheState } | undefined
  458. if (!row) return undefined
  459. const local=this.db.prepare('SELECT title,archived,pinned FROM chat_local_state WHERE owner=? AND profile=? AND session_id=?').get(owner,profile,sessionID) as any
  460. return { response: { status: 200, headers: new Headers({ 'content-type': 'application/json' }),
  461. body: Buffer.from(JSON.stringify({...JSON.parse(row.data),...(local?.title!=null?{title:local.title}:{}),...(local?.archived!=null?{archived:!!local.archived}:{}),owned:true,pinned:local?.pinned!=null?!!local.pinned:bool(JSON.parse(row.data).pinned),sync_state:row.sync_state})) }, state: row.sync_state }
  462. }
  463. revision(owner: string, profile: string, sessionID: string): number {
  464. return Number((this.db.prepare('SELECT sync_revision FROM chat_sessions WHERE owner=? AND profile=? AND session_id=?')
  465. .get(owner, profile, sessionID) as { sync_revision?: number } | undefined)?.sync_revision ?? 0)
  466. }
  467. hasMessage(owner: string, profile: string, sessionID: string, message: Record<string, unknown>, position: number): boolean {
  468. const id = messageID(message, String(position))
  469. const row = this.db.prepare('SELECT position FROM chat_messages WHERE owner=? AND profile=? AND session_id=? AND message_id=?')
  470. .get(owner, profile, sessionID, id) as { position: number } | undefined
  471. if (row && row.position !== position) throw new Error('Stored history changed; explicit refresh is required')
  472. return Boolean(row)
  473. }
  474. putSnapshot(owner: string, key: string, kind: string, profile: string, sessionID: string | undefined,
  475. response: UpstreamResponse, ownedList = false, requestStartedAt = Date.now(),
  476. metadataResponse: UpstreamResponse = response): boolean {
  477. const payload = parseResponse(response)
  478. if (!payload) return false
  479. if (kind === 'list' && !ownedList) return false
  480. const isOwnedChat = ownedList || this.payloadBelongsToOwnedChat(owner, profile, sessionID, payload)
  481. if (!isOwnedChat) return false
  482. const now = Date.now()
  483. this.db.exec('SAVEPOINT chat_snapshot')
  484. try {
  485. // Hydrate all registered sessions from the unsliced upstream list, while
  486. // persisting only the owner-filtered page as the public response snapshot.
  487. this.ingestPayload(owner, profile, sessionID, parseResponse(metadataResponse) ?? payload, now, kind, requestStartedAt)
  488. this.db.prepare(`INSERT INTO chat_snapshots(owner,cache_key,kind,profile,session_id,status,headers,body,sync_state,saved_at)
  489. VALUES(?,?,?,?,?,?,?,?,?,?) ON CONFLICT(owner,cache_key) DO UPDATE SET status=excluded.status,headers=excluded.headers,
  490. body=excluded.body,sync_state='current',saved_at=excluded.saved_at`)
  491. .run(owner, key, kind, profile, sessionID ?? null, response.status, headerRecord(response.headers),
  492. response.body.toString('base64'), 'current', now)
  493. this.db.exec('RELEASE chat_snapshot')
  494. return true
  495. } catch (error) {
  496. this.db.exec('ROLLBACK TO chat_snapshot; RELEASE chat_snapshot')
  497. throw error
  498. }
  499. }
  500. applySync(owner: string, profile: string, sessionID: string, revision: number,
  501. pages: Array<{ key: string; kind: string; response: UpstreamResponse; startedAt: number }>, force: boolean): boolean {
  502. if (this.revision(owner, profile, sessionID) !== revision) return false
  503. this.db.exec('SAVEPOINT history_sync')
  504. try {
  505. if (force) this.db.prepare('DELETE FROM chat_messages WHERE owner=? AND profile=? AND session_id=?').run(owner, profile, sessionID)
  506. // Recovery promotes locally generated stream identities to upstream row IDs.
  507. // Remove only replaced rows inside the verified window; keep older coverage.
  508. if(!force){
  509. const windows=pages.filter(p=>p.kind==='messages').map(p=>parseResponse(p.response)).filter(Boolean) as Record<string,any>[]
  510. for(const window of windows){
  511. const pagination=window.pagination??{},messages=window.messages as Record<string,any>[]
  512. if(!Array.isArray(messages)||!Number.isSafeInteger(pagination.total))continue
  513. const end=pagination.total-Number(pagination.offset??0),start=end-messages.length
  514. const ids=new Set(messages.map((m,index)=>messageID(m,String(start+index))))
  515. const existing=this.db.prepare('SELECT message_id,data FROM chat_messages WHERE owner=? AND profile=? AND session_id=? AND position>=? AND position<?').all(owner,profile,sessionID,start,end) as {message_id:string;data:string}[]
  516. for(const row of existing){
  517. if(ids.has(row.message_id))continue
  518. const value=JSON.parse(row.data)
  519. if(value.role==='user'&&value.status==='pending'&&!messages.some(m=>m.role==='user'&&m.content===value.content))
  520. throw new Error('Unconfirmed submission must be reconciled before history replacement')
  521. this.db.prepare('DELETE FROM chat_messages WHERE owner=? AND profile=? AND session_id=? AND message_id=?').run(owner,profile,sessionID,row.message_id)
  522. }
  523. }
  524. }
  525. this.db.prepare("DELETE FROM chat_snapshots WHERE owner=? AND profile=? AND session_id=? AND kind='messages'").run(owner, profile, sessionID)
  526. for (const page of pages) this.putSnapshot(owner, page.key, page.kind, profile, sessionID, page.response, false, page.startedAt)
  527. const latest=this.db.prepare("SELECT message_id FROM chat_messages WHERE owner=? AND profile=? AND session_id=? AND json_extract(data,'$.role')='assistant' ORDER BY position DESC LIMIT 1").get(owner,profile,sessionID) as {message_id:string}|undefined
  528. if(latest)this.db.prepare('UPDATE chat_local_state SET assistant_id=? WHERE owner=? AND profile=? AND session_id=?').run(latest.message_id,owner,profile,sessionID)
  529. this.db.exec('RELEASE history_sync')
  530. return true
  531. } catch (error) {
  532. this.db.exec('ROLLBACK TO history_sync; RELEASE history_sync')
  533. throw error
  534. }
  535. }
  536. messagePage(owner: string, profile: string, sessionID: string, offset: number, limit: number): MessagePage | undefined {
  537. const session = this.db.prepare(`SELECT data,sync_state,complete,message_total FROM chat_sessions
  538. WHERE owner=? AND profile=? AND session_id=?`).get(owner, profile, sessionID) as Record<string, unknown> | undefined
  539. if (!session) return undefined
  540. const totalRow = this.db.prepare('SELECT COUNT(*) count, MAX(position)+1 extent FROM chat_messages WHERE owner=? AND profile=? AND session_id=?')
  541. .get(owner, profile, sessionID) as { count: number; extent: number | null }
  542. if (session.message_total == null && !bool(session.complete)) return undefined
  543. const total = Number(session.message_total ?? totalRow.extent ?? 0)
  544. const end = Math.max(0, total - offset)
  545. const start = Math.max(0, end - limit)
  546. const rows = this.db.prepare(`SELECT data FROM chat_messages WHERE owner=? AND profile=? AND session_id=?
  547. AND position>=? AND position<? ORDER BY position`).all(owner, profile, sessionID, start, end) as Array<{ data: string }>
  548. const messages = rows.map(row => JSON.parse(row.data))
  549. if (messages.length !== end - start) return undefined // Fetch only an actually missing page.
  550. const body = Buffer.from(JSON.stringify({
  551. session_id: sessionID,
  552. session: JSON.parse(this.localDetail(owner,profile,sessionID)!.response.body.toString()),
  553. messages,
  554. pagination: {
  555. total,
  556. returned: messages.length,
  557. offset,
  558. limit,
  559. has_more: start > 0,
  560. },
  561. }))
  562. return {
  563. response: { status: 200, headers: new Headers({ 'content-type': 'application/json; charset=utf-8' }), body },
  564. state: String(session.sync_state) as ChatCacheState,
  565. }
  566. }
  567. recordEvent(owner: string, profile: string, sessionID: string, frame: Record<string, unknown>): boolean {
  568. const rawSeq = Number(frame.seq ?? frame.sequence ?? 0)
  569. const type = String(frame.type ?? 'unknown')
  570. const payload = JSON.stringify(frame.payload ?? {})
  571. const eventID = String(frame.delivery_id ?? (rawSeq > 0 ? `${frame.epoch ?? ''}:${rawSeq}:${type}` : randomUUID()))
  572. const seq = Number.parseInt(createHash('sha256').update(eventID).digest('hex').slice(0,12),16)
  573. const now = Date.now()
  574. this.db.exec('SAVEPOINT live_event')
  575. try {
  576. const receipt=this.db.prepare('INSERT OR IGNORE INTO chat_event_receipts VALUES(?,?,?,?)').run(owner,profile,sessionID,eventID)
  577. if(!receipt.changes){this.db.exec('RELEASE live_event');return false}
  578. const previous = this.db.prepare(`SELECT last_event_seq FROM chat_sessions
  579. WHERE owner=? AND profile=? AND session_id=?`).get(owner, profile, sessionID) as { last_event_seq?: number } | undefined
  580. const lastSeen = Number(previous?.last_event_seq ?? 0)
  581. const inserted = this.db.prepare(`INSERT OR IGNORE INTO chat_events(owner,profile,session_id,event_seq,event_type,payload,received_at)
  582. VALUES(?,?,?,?,?,?,?)`).run(owner, profile, sessionID, Number.isSafeInteger(seq) ? seq : 0, type, payload, now)
  583. if (!inserted.changes) {this.db.exec('RELEASE live_event');return false}
  584. const state: ChatCacheState = type === 'gateway.reset' || type.includes('reset')
  585. || (!frame.epoch && rawSeq > 0 && lastSeen > 0 && rawSeq > lastSeen + 1) ? 'gap' : 'stale'
  586. const metadataOnly = ['session.info', 'session.title', 'session.title.updated', 'session.usage', 'gateway.ready'].includes(type) && state !== 'gap'
  587. this.db.prepare(`UPDATE chat_sessions SET sync_state=CASE WHEN ? THEN sync_state WHEN sync_state='gap' THEN 'gap' ELSE ? END,
  588. sync_revision=sync_revision+?,last_event_seq=MAX(last_event_seq,?),last_synced_at=?
  589. WHERE owner=? AND profile=? AND session_id=?`).run(metadataOnly ? 1 : 0, state, metadataOnly ? 0 : 1, rawSeq > 0 ? rawSeq : lastSeen, now, owner, profile, sessionID)
  590. if (type === 'session.info') {
  591. const payload = object(frame.payload) ?? {}
  592. const info = object(payload.info) ?? payload
  593. const row = this.db.prepare('SELECT data FROM chat_sessions WHERE owner=? AND profile=? AND session_id=?')
  594. .get(owner, profile, sessionID) as { data: string } | undefined
  595. if (row) {
  596. const summary = object(JSON.parse(row.data)) ?? {}
  597. for (const field of ['model', 'provider', 'fast']) if (info[field] !== undefined) summary[field] = info[field]
  598. this.db.prepare('UPDATE chat_sessions SET data=? WHERE owner=? AND profile=? AND session_id=?')
  599. .run(JSON.stringify(summary), owner, profile, sessionID)
  600. }
  601. }
  602. if (type === 'session.title' || type === 'session.title.updated') {
  603. const title = String(object(frame.payload)?.title ?? frame.title ?? '').trim()
  604. if (title) this.updateOwnedSessionTitle(owner, profile, sessionID, title, now)
  605. }
  606. this.db.prepare(`UPDATE chat_snapshots SET sync_state=? WHERE owner=? AND (session_id=? OR kind='list')
  607. AND (?=0 OR kind<>'messages')`).run(state, owner, sessionID, metadataOnly ? 1 : 0)
  608. this.db.prepare(`INSERT INTO chat_sync_cursors(owner,profile,event_cursor,sync_state,last_synced_at)
  609. VALUES(?,?,?,?,?) ON CONFLICT(owner,profile) DO UPDATE SET event_cursor=MAX(event_cursor,excluded.event_cursor),
  610. sync_state=excluded.sync_state,last_synced_at=excluded.last_synced_at`)
  611. .run(owner, profile, rawSeq > 0 ? rawSeq : lastSeen, state, now)
  612. this.projectEvent(owner,profile,sessionID,type,object(frame.payload)??{},eventID)
  613. this.db.exec('RELEASE live_event')
  614. return true
  615. }catch(e){this.db.exec('ROLLBACK TO live_event; RELEASE live_event');throw e}
  616. }
  617. canonicalSessionID(owner:string,profile:string,id:string):string {
  618. let current=id
  619. const seen=new Set<string>()
  620. for(let n=0;n<32&&!seen.has(current);n++){
  621. seen.add(current)
  622. const row=this.db.prepare('SELECT session_id FROM chat_session_aliases WHERE owner=? AND profile=? AND alias_id=?').get(owner,profile,current) as {session_id:string}|undefined
  623. if(!row)break
  624. current=row.session_id
  625. }
  626. return current
  627. }
  628. private migrateRuntimeSession(owner:string,profile:string,id:string,runtime:string):void {
  629. const old=this.db.prepare('SELECT session_id FROM chat_sessions WHERE owner=? AND profile=? AND runtime_id=? AND session_id<>? AND owned_at IS NOT NULL').get(owner,profile,runtime,id) as {session_id:string}|undefined
  630. if(!old||this.ownsSession(owner,profile,id))return
  631. this.db.exec('SAVEPOINT migrate_live_route')
  632. try{
  633. for(const table of ['chat_sessions','chat_messages','chat_events','chat_snapshots','chat_attachments','chat_local_state','chat_event_receipts','chat_recovery_jobs'])
  634. this.db.prepare(`UPDATE ${table} SET session_id=? WHERE owner=? AND profile=? AND session_id=?`).run(id,owner,profile,old.session_id)
  635. this.db.prepare('INSERT OR REPLACE INTO chat_session_aliases VALUES(?,?,?,?)').run(owner,profile,old.session_id,id)
  636. this.db.prepare('UPDATE chat_session_aliases SET session_id=? WHERE owner=? AND profile=? AND session_id=?').run(id,owner,profile,old.session_id)
  637. this.db.prepare("UPDATE chat_sessions SET data=json_set(data,'$.id',?) WHERE owner=? AND profile=? AND session_id=?").run(id,owner,profile,id)
  638. this.db.exec('RELEASE migrate_live_route')
  639. }catch(e){this.db.exec('ROLLBACK TO migrate_live_route; RELEASE migrate_live_route');throw e}
  640. }
  641. recordRoute(owner: string, profile: string, sessionID: string, runtimeID: string): void {
  642. this.migrateRuntimeSession(owner,profile,sessionID,runtimeID)
  643. const now = Date.now()
  644. const summary = { id: sessionID, profile, source: 'unknown', title: '新对话', started_at: now, last_active: now }
  645. this.db.prepare(`INSERT INTO chat_sessions(owner,profile,session_id,runtime_id,source,data,sync_state,complete,last_synced_at,owned_at,ownership_evidence)
  646. VALUES(?,?,?,?,?,?,?,?,?,?,?) ON CONFLICT(owner,profile,session_id) DO UPDATE SET runtime_id=excluded.runtime_id,
  647. last_synced_at=excluded.last_synced_at,owned_at=COALESCE(chat_sessions.owned_at,excluded.owned_at),
  648. ownership_evidence=COALESCE(chat_sessions.ownership_evidence,excluded.ownership_evidence)`)
  649. .run(owner, profile, sessionID, runtimeID, 'unknown', JSON.stringify(summary), 'stale', 0, now, now, 'route-confirmed')
  650. this.markListsStale(owner)
  651. }
  652. recordCommand(owner: string, profile: string, sessionID: string, method: string,
  653. params: Record<string, unknown>): void {
  654. const now = Date.now()
  655. const source = sessionSource(params) || 'unknown'
  656. const establishesOwnership = method === 'session.create' || method === 'session.branch'
  657. const summary = { id: sessionID, profile, source, title: '新对话', started_at: now, last_active: now }
  658. this.db.prepare(`INSERT INTO chat_sessions(owner,profile,session_id,source,data,sync_state,complete,last_synced_at,owned_at,ownership_evidence)
  659. VALUES(?,?,?,?,?,?,?,?,?,?) ON CONFLICT(owner,profile,session_id) DO UPDATE SET
  660. source=CASE WHEN excluded.source='unknown' THEN chat_sessions.source ELSE excluded.source END,
  661. sync_state=CASE WHEN chat_sessions.sync_state='gap' THEN 'gap' ELSE 'stale' END,sync_revision=chat_sessions.sync_revision+1,last_synced_at=excluded.last_synced_at,
  662. owned_at=COALESCE(chat_sessions.owned_at,excluded.owned_at),
  663. ownership_evidence=COALESCE(chat_sessions.ownership_evidence,excluded.ownership_evidence)`)
  664. .run(owner, profile, sessionID, source, JSON.stringify(summary), 'stale', 0, now,
  665. establishesOwnership ? now : null, establishesOwnership ? `command:${method}` : null)
  666. if(method==='session.create') this.db.prepare(`INSERT INTO chat_local_state(owner,profile,session_id,read_initialized) VALUES(?,?,?,1) ON CONFLICT(owner,profile,session_id) DO UPDATE SET read_initialized=1`).run(owner,profile,sessionID)
  667. if(method==='session.create') this.db.prepare(`UPDATE chat_sessions SET complete=1,metadata_complete=1,message_total=0,sync_state='current' WHERE owner=? AND profile=? AND session_id=? AND message_total IS NULL`).run(owner,profile,sessionID)
  668. if((method==='prompt.submit'||method==='session.steer') && params._delivery_id){
  669. const userID=`user:${params._delivery_id}`
  670. this.writeLiveMessage(owner,profile,sessionID,{id:userID,role:'user',content:params.text??params.input??'',timestamp:now/1000,status:'pending',client_message_id:String(params._delivery_id).replace(/^(web|ios):prompt:/,'')})
  671. this.db.prepare(`INSERT INTO chat_local_state(owner,profile,session_id,assistant_id) VALUES(?,?,?,NULL) ON CONFLICT(owner,profile,session_id) DO UPDATE SET assistant_id=NULL`).run(owner,profile,sessionID)
  672. }
  673. const payload = JSON.stringify(params)
  674. const seq = Number.parseInt(createHash('sha256').update(`${method}:${payload}`).digest('hex').slice(0, 12), 16)
  675. this.db.prepare(`INSERT OR IGNORE INTO chat_events(owner,profile,session_id,event_seq,event_type,payload,received_at)
  676. VALUES(?,?,?,?,?,?,?)`).run(owner, profile, sessionID, seq, `command:${method}`, payload, now)
  677. this.db.prepare("UPDATE chat_snapshots SET sync_state='stale' WHERE owner=? AND (session_id=? OR kind='list')")
  678. .run(owner, sessionID)
  679. }
  680. fileSourceMessage(owner: string, profile: string, sessionID: string, messageID: string): Record<string, unknown> | undefined {
  681. const row = this.db.prepare('SELECT data FROM chat_messages WHERE owner=? AND profile=? AND session_id=? AND message_id=?')
  682. .get(owner, profile, sessionID, messageID)
  683. return row ? JSON.parse(String(row.data)) : undefined
  684. }
  685. markListsStale(owner: string): void {
  686. this.db.prepare("UPDATE chat_snapshots SET sync_state='stale' WHERE owner=? AND kind='list'").run(owner)
  687. }
  688. invalidateOwnedMetadata(owner: string): Array<{profile:string;session_id:string}> {
  689. return this.db.prepare(`UPDATE chat_sessions SET metadata_complete=0,sync_revision=sync_revision+1
  690. WHERE owner=? AND owned_at IS NOT NULL AND source NOT IN ('cron','ios_group','yaoyao_workspace')
  691. RETURNING profile,session_id`).all(owner) as Array<{profile:string;session_id:string}>
  692. }
  693. refreshMetadata(owner:string,profile:string,id:string,revision:number,response:UpstreamResponse,startedAt:number):boolean {
  694. const payload=parseResponse(response),summary=object(payload?.session)??payload
  695. const total=summary?.message_count??summary?.messageCount
  696. const row=this.db.prepare('SELECT metadata_complete,complete,message_total,sync_state FROM chat_sessions WHERE owner=? AND profile=? AND session_id=?')
  697. .get(owner,profile,id) as {metadata_complete:number;complete:number;message_total:number;sync_state:string}|undefined
  698. if(!row||row.metadata_complete||!row.complete||total==null||Number(total)!==row.message_total||this.revision(owner,profile,id)!==revision)return false
  699. this.db.exec('SAVEPOINT metadata_refresh')
  700. try {
  701. const metadata={...summary};delete metadata.messages
  702. const headers=new Headers(response.headers);headers.delete('content-length')
  703. const metadataResponse={...response,headers,body:Buffer.from(JSON.stringify(metadata))}
  704. if(!this.putSnapshot(owner,`detail:${profile}:${id}`,'detail',profile,id,metadataResponse,false,startedAt))throw new Error('Invalid conversation metadata')
  705. // A metadata invalidation must not replace or downgrade the durable live tail.
  706. this.db.prepare('UPDATE chat_sessions SET sync_state=? WHERE owner=? AND profile=? AND session_id=?').run(row.sync_state,owner,profile,id)
  707. this.db.exec('RELEASE metadata_refresh');return true
  708. }catch(error){this.db.exec('ROLLBACK TO metadata_refresh; RELEASE metadata_refresh');throw error}
  709. }
  710. recordAuthoritativeTitle(owner: string, profile: string, sessionID: string, title: string): void {
  711. const normalized = title.trim()
  712. if (!normalized) return
  713. this.updateOwnedSessionTitle(owner, profile, sessionID, normalized, Date.now())
  714. this.db.prepare(`UPDATE chat_snapshots SET sync_state='stale'
  715. WHERE owner=? AND (session_id=? OR kind='list')`).run(owner, sessionID)
  716. }
  717. deleteSession(owner: string, profile: string, sessionID: string): void {
  718. const assets = this.db.prepare(`SELECT DISTINCT local_path FROM chat_attachments
  719. WHERE owner=? AND profile=? AND session_id=? AND local_path IS NOT NULL`)
  720. .all(owner, profile, sessionID) as Array<{ local_path: string }>
  721. this.db.exec('BEGIN IMMEDIATE')
  722. try {
  723. for (const table of ['chat_messages', 'chat_events', 'chat_attachments', 'chat_local_state', 'chat_event_receipts', 'chat_recovery_jobs', 'chat_session_aliases', 'chat_sessions']) {
  724. this.db.prepare(`DELETE FROM ${table} WHERE owner=? AND profile=? AND session_id=?`).run(owner, profile, sessionID)
  725. }
  726. this.db.prepare("DELETE FROM chat_snapshots WHERE owner=? AND ((profile=? AND session_id=?) OR kind='list')")
  727. .run(owner, profile, sessionID)
  728. this.db.exec('COMMIT')
  729. for (const asset of assets) {
  730. const referenced = this.db.prepare('SELECT 1 ok FROM chat_attachments WHERE local_path=? LIMIT 1').get(asset.local_path)
  731. if (!referenced) try { unlinkSync(asset.local_path) } catch { /* Already absent. */ }
  732. }
  733. } catch (error) { this.db.exec('ROLLBACK'); throw error }
  734. }
  735. attachment(owner: string, sourcePath: string): { localPath: string; mimeType?: string } | undefined {
  736. const row = this.db.prepare(`SELECT local_path,mime_type FROM chat_attachments
  737. WHERE owner=? AND source_path=? AND state='cached' ORDER BY updated_at DESC LIMIT 1`).get(owner, sourcePath) as Record<string, unknown> | undefined
  738. const localPath = row?.local_path ? String(row.local_path) : ''
  739. return localPath && existsSync(localPath) ? { localPath, ...(row?.mime_type ? { mimeType: String(row.mime_type) } : {}) } : undefined
  740. }
  741. knowsAttachment(owner: string, sourcePath: string): boolean {
  742. return Boolean(this.db.prepare('SELECT 1 ok FROM chat_attachments WHERE owner=? AND source_path=? LIMIT 1').get(owner, sourcePath))
  743. }
  744. ownsSession(owner: string, profile: string, sessionID: string): boolean {
  745. sessionID=this.canonicalSessionID(owner,profile,sessionID)
  746. return Boolean(this.db.prepare(
  747. 'SELECT 1 ok FROM chat_sessions WHERE owner=? AND profile=? AND session_id=? AND owned_at IS NOT NULL',
  748. ).get(owner, profile, sessionID))
  749. }
  750. ownedSessionMetadata(owner: string, profile: string, sessionID: string): {
  751. owned: boolean
  752. authoritativeTitle?: string
  753. } {
  754. const row = this.db.prepare(`SELECT authoritative_title FROM chat_sessions
  755. WHERE owner=? AND profile=? AND session_id=? AND owned_at IS NOT NULL`)
  756. .get(owner, profile, sessionID) as { authoritative_title?: string | null } | undefined
  757. const local=this.db.prepare('SELECT title FROM chat_local_state WHERE owner=? AND profile=? AND session_id=?').get(owner,profile,sessionID) as {title?:string}|undefined
  758. const authoritativeTitle = String(local?.title ?? row?.authoritative_title ?? '').trim()
  759. return {
  760. owned: Boolean(row),
  761. ...(authoritativeTitle ? { authoritativeTitle } : {}),
  762. }
  763. }
  764. mergeOwnedSessionsIntoList(
  765. owner: string,
  766. fallbackProfile: string,
  767. response: UpstreamResponse,
  768. page: { offset?: number; limit?: number; archived?: string } = {},
  769. ): UpstreamResponse {
  770. const payload = parseResponse(response)
  771. if (!payload) return response
  772. const collectionKey = Array.isArray(payload.items)
  773. ? 'items'
  774. : Array.isArray(payload.sessions) ? 'sessions' : undefined
  775. if (!collectionKey) return response
  776. const rows = (fallbackProfile
  777. ? this.db.prepare(`SELECT profile,session_id,source,data,authoritative_title,last_synced_at FROM chat_sessions
  778. WHERE owner=? AND profile=? AND owned_at IS NOT NULL`)
  779. .all(owner, fallbackProfile)
  780. : this.db.prepare(`SELECT profile,session_id,source,data,authoritative_title,last_synced_at FROM chat_sessions
  781. WHERE owner=? AND owned_at IS NOT NULL`).all(owner)) as Array<{
  782. profile: string
  783. session_id: string
  784. source: string
  785. data: string
  786. authoritative_title: string | null
  787. last_synced_at: number
  788. }>
  789. const owned = new Map(rows.map(row => [
  790. `${row.profile}\u0000${row.session_id}`,
  791. row,
  792. ]))
  793. const merged = new Map<string, Record<string, unknown>>()
  794. for (const [key, row] of owned) {
  795. let stored: Record<string, unknown> = {}
  796. try { stored = object(JSON.parse(row.data)) ?? {} } catch { /* Ignore damaged display metadata. */ }
  797. const storedSource = sessionSource(stored)
  798. merged.set(key, {
  799. ...stored,
  800. id: row.session_id,
  801. profile: row.profile,
  802. source: storedSource && storedSource !== 'unknown' ? storedSource : row.source,
  803. ...(row.authoritative_title?.trim() ? { title: row.authoritative_title.trim() } : {}),
  804. _yaoyao_registry_activity: row.last_synced_at,
  805. owned: true,
  806. })
  807. }
  808. for (const value of payload[collectionKey] as unknown[]) {
  809. const session = object(value)
  810. if (!session) continue
  811. const id = sessionIDOf(session)
  812. const profile = profileOf(session, fallbackProfile) || 'default'
  813. if (!id) continue
  814. const key = `${profile}\u0000${id}`
  815. const row = owned.get(key)
  816. if (row) {
  817. const title = row.authoritative_title?.trim()
  818. merged.set(key, {
  819. ...mergeSessionMetadata(merged.get(key) ?? {}, session),
  820. ...(title ? { title } : {}),
  821. _yaoyao_registry_activity: row.last_synced_at,
  822. owned: true,
  823. })
  824. }
  825. }
  826. const activity = (session: Record<string, unknown>): number =>
  827. firstTimestamp(session, ACTIVITY_TIME_KEYS) ?? firstTimestamp(session, START_TIME_KEYS) ?? 0
  828. const archived = (session: Record<string, unknown>): boolean => bool(
  829. session.is_archived ?? session.isArchived ?? session.archived,
  830. )
  831. const pinned = (session: Record<string, unknown>): boolean => bool(
  832. session.is_pinned ?? session.isPinned ?? session.pinned,
  833. )
  834. const archivedMode = page.archived ?? 'exclude'
  835. const allSessions = [...merged.values()]
  836. .filter(session => archivedMode === 'include'
  837. || (archivedMode === 'only' ? archived(session) : !archived(session)))
  838. .sort((left, right) => Number(pinned(right)) - Number(pinned(left))
  839. || activity(right) - activity(left))
  840. const offset = Math.max(0, Math.trunc(page.offset ?? 0))
  841. const limit = Math.max(1, Math.min(500, Math.trunc(page.limit ?? 100)))
  842. const sessions = allSessions.slice(offset, offset + limit).map((session) => {
  843. const publicSession = mergeSessionMetadata({}, session)
  844. delete publicSession._yaoyao_registry_activity
  845. return publicSession
  846. })
  847. const body = Buffer.from(JSON.stringify({
  848. ...payload,
  849. [collectionKey]: sessions,
  850. total: allSessions.length,
  851. offset,
  852. limit,
  853. }))
  854. const headers = new Headers(response.headers)
  855. headers.delete('content-length')
  856. return { ...response, headers, body }
  857. }
  858. excludeOwnedSessionsFromList(
  859. owner: string,
  860. fallbackProfile: string,
  861. response: UpstreamResponse,
  862. page: { offset?: number; limit?: number } = {},
  863. ): UpstreamResponse {
  864. const payload = parseResponse(response)
  865. if (!payload) return response
  866. const collectionKey = Array.isArray(payload.items)
  867. ? 'items'
  868. : Array.isArray(payload.sessions) ? 'sessions' : undefined
  869. if (!collectionKey) return response
  870. const allSessions = (payload[collectionKey] as unknown[]).filter((value) => {
  871. const session = object(value)
  872. if (!session) return true
  873. const id = sessionIDOf(session)
  874. const profile = profileOf(session, fallbackProfile) || 'default'
  875. return !id || !this.ownsSession(owner, profile, id)
  876. })
  877. const offset = Math.max(0, Math.trunc(page.offset ?? 0))
  878. const limit = Math.max(1, Math.min(500, Math.trunc(page.limit ?? 100)))
  879. const sessions = allSessions.slice(offset, offset + limit)
  880. const headers = new Headers(response.headers)
  881. headers.delete('content-length')
  882. return {
  883. ...response,
  884. headers,
  885. body: Buffer.from(JSON.stringify({
  886. ...payload,
  887. [collectionKey]: sessions,
  888. total: allSessions.length,
  889. offset,
  890. limit,
  891. })),
  892. }
  893. }
  894. markSessionOwnership(
  895. owner: string,
  896. fallbackProfile: string,
  897. sessionID: string,
  898. response: UpstreamResponse,
  899. ): UpstreamResponse {
  900. const payload = parseResponse(response)
  901. if (!payload) return response
  902. const session = object(payload.session)
  903. const candidate = session ?? payload
  904. const id = sessionIDOf(candidate) || sessionID
  905. const profile = profileOf(candidate, fallbackProfile) || fallbackProfile || 'default'
  906. const row = this.db.prepare(`SELECT authoritative_title FROM chat_sessions
  907. WHERE owner=? AND profile=? AND session_id=? AND owned_at IS NOT NULL`).get(owner, profile, id) as { authoritative_title?: string | null } | undefined
  908. const owned = Boolean(row)
  909. const local=this.db.prepare('SELECT title FROM chat_local_state WHERE owner=? AND profile=? AND session_id=?').get(owner,profile,id) as {title?:string}|undefined
  910. const title = String(local?.title ?? row?.authoritative_title ?? '').trim()
  911. const body = session
  912. ? Buffer.from(JSON.stringify({
  913. ...payload,
  914. session: { ...session, ...(title ? { title } : {}), owned },
  915. owned,
  916. }))
  917. : Buffer.from(JSON.stringify({ ...payload, ...(title ? { title } : {}), owned }))
  918. const headers = new Headers(response.headers)
  919. headers.delete('content-length')
  920. return { ...response, headers, body }
  921. }
  922. markOwnedSearchResults(
  923. owner: string,
  924. fallbackProfile: string,
  925. response: UpstreamResponse,
  926. view?: 'chat' | 'history',
  927. limit = 100,
  928. ): UpstreamResponse {
  929. const payload = parseResponse(response)
  930. if (!payload || !Array.isArray(payload.results)) return response
  931. const marked = payload.results.map((value) => {
  932. const session = object(value)
  933. if (!session) return value
  934. const id = sessionIDOf(session)
  935. const profile = profileOf(session, fallbackProfile) || fallbackProfile || 'default'
  936. const metadata = id
  937. ? this.ownedSessionMetadata(owner, profile, id)
  938. : { owned: false }
  939. return {
  940. ...session,
  941. ...(metadata.authoritativeTitle ? { title: metadata.authoritativeTitle } : {}),
  942. owned: metadata.owned,
  943. }
  944. })
  945. const projected = marked.filter((value) => {
  946. if (!view) return true
  947. const session = object(value)
  948. if (!session) return false
  949. return view === 'chat' ? session.owned === true : session.owned !== true
  950. })
  951. const results = projected.slice(0, Math.max(1, Math.min(100, Math.trunc(limit))))
  952. const headers = new Headers(response.headers)
  953. headers.delete('content-length')
  954. return {
  955. ...response,
  956. headers,
  957. body: Buffer.from(JSON.stringify({ ...payload, results, total: projected.length })),
  958. }
  959. }
  960. storeAttachment(owner: string, sourcePath: string, response: UpstreamResponse): string | undefined {
  961. if (response.status !== 200 || !response.body.length) return undefined
  962. const sha256 = createHash('sha256').update(response.body).digest('hex')
  963. const directory = join(this.assetsRoot, sha256.slice(0, 2))
  964. mkdirSync(directory, { recursive: true, mode: 0o700 })
  965. const target = join(directory, sha256)
  966. if (!existsSync(target)) {
  967. const temporary = `${target}.${process.pid}.tmp`
  968. writeFileSync(temporary, response.body, { mode: 0o600 })
  969. renameSync(temporary, target)
  970. }
  971. const mimeType = response.headers.get('content-type')?.split(';', 1)[0]
  972. this.db.prepare(`UPDATE chat_attachments SET sha256=?,mime_type=?,size=?,local_path=?,state='cached',updated_at=?
  973. WHERE owner=? AND source_path=?`).run(sha256, mimeType ?? null, response.body.length, target, Date.now(), owner, sourcePath)
  974. return target
  975. }
  976. assetStream(path: string): { stream: ReturnType<typeof createReadStream>; size: number } {
  977. return { stream: createReadStream(path), size: statSync(path).size }
  978. }
  979. private payloadBelongsToOwnedChat(owner: string, profile: string, sessionID: string | undefined,
  980. payload: Record<string, unknown>): boolean {
  981. const session = object(payload.session) ?? (sessionID ? payload : undefined)
  982. if (session) {
  983. const id = sessionIDOf(session) || sessionID || ''
  984. const sessionProfile = profileOf(session, profile) || profile || 'default'
  985. if (id && this.ownsSession(owner, sessionProfile, id)) return true
  986. }
  987. const sessions = Array.isArray(payload.items) ? payload.items : Array.isArray(payload.sessions) ? payload.sessions : []
  988. if (sessions.some(value => {
  989. const candidate = object(value)
  990. if (!candidate) return false
  991. const id = sessionIDOf(candidate)
  992. const sessionProfile = profileOf(candidate, profile) || profile || 'default'
  993. return Boolean(id && this.ownsSession(owner, sessionProfile, id))
  994. })) return true
  995. return Boolean(sessionID && this.ownsSession(owner, profile || 'default', sessionID))
  996. }
  997. private ingestPayload(owner: string, fallbackProfile: string, sessionID: string | undefined,
  998. payload: Record<string, unknown>, now: number, kind: string, requestStartedAt: number): void {
  999. const candidates: Record<string, unknown>[] = []
  1000. const direct = object(payload.session)
  1001. if (direct) candidates.push(direct)
  1002. const listed = Array.isArray(payload.items) ? payload.items : Array.isArray(payload.sessions) ? payload.sessions : []
  1003. for (const value of listed) if (object(value)) candidates.push(object(value)!)
  1004. if (sessionID && !direct && (sessionIDOf(payload) || sessionSource(payload) || typeof payload.title === 'string')) {
  1005. candidates.push(payload)
  1006. }
  1007. for (const session of candidates) {
  1008. const id = sessionIDOf(session) || sessionID || ''
  1009. if (!id) continue
  1010. const profile = profileOf(session, fallbackProfile) || fallbackProfile || 'default'
  1011. const registered = this.ownsSession(owner, profile, id)
  1012. const source = sessionSource(session)
  1013. if (!registered) continue
  1014. const titleRow = this.db.prepare(`SELECT data,authoritative_title,authoritative_title_at FROM chat_sessions
  1015. WHERE owner=? AND profile=? AND session_id=?`).get(owner, profile, id) as {
  1016. data: string; authoritative_title?: string | null; authoritative_title_at?: number
  1017. } | undefined
  1018. const authoritativeTitle = String(titleRow?.authoritative_title ?? '').trim()
  1019. const authoritativeTitleAt = Number(titleRow?.authoritative_title_at ?? 0)
  1020. const incomingTitle = sessionTitle(session)
  1021. const preservesAuthoritativeTitle = Boolean(authoritativeTitle)
  1022. && (kind !== 'detail' || isPlaceholderTitle(incomingTitle)
  1023. || requestStartedAt <= authoritativeTitleAt)
  1024. let previous: Record<string, unknown> = {}
  1025. try { previous = object(JSON.parse(titleRow?.data ?? '{}')) ?? {} } catch { /* Recover from incoming metadata. */ }
  1026. const storedSession = mergeSessionMetadata(previous, session)
  1027. if (preservesAuthoritativeTitle) storedSession.title = authoritativeTitle
  1028. const metadataComplete = kind !== 'messages' || direct ? 1 : 0
  1029. this.db.prepare(`INSERT INTO chat_sessions(owner,profile,session_id,runtime_id,source,data,sync_state,complete,last_synced_at,metadata_complete)
  1030. VALUES(?,?,?,?,?,?,?,?,?,?) ON CONFLICT(owner,profile,session_id) DO UPDATE SET data=excluded.data,
  1031. metadata_complete=MAX(chat_sessions.metadata_complete,excluded.metadata_complete),
  1032. source=CASE WHEN excluded.source='unknown' THEN chat_sessions.source ELSE excluded.source END,
  1033. sync_state='current',last_synced_at=excluded.last_synced_at`)
  1034. .run(owner, profile, id, null, source || 'unknown', JSON.stringify(storedSession), 'current', 0, now, metadataComplete)
  1035. const acceptedTitle = sessionTitle(storedSession)
  1036. if (acceptedTitle && !isPlaceholderTitle(acceptedTitle)
  1037. && (!authoritativeTitle || kind === 'detail')) {
  1038. this.db.prepare(`UPDATE chat_sessions SET authoritative_title=?,authoritative_title_at=?
  1039. WHERE owner=? AND profile=? AND session_id=?`).run(
  1040. acceptedTitle,
  1041. Math.max(requestStartedAt, authoritativeTitleAt),
  1042. owner,
  1043. profile,
  1044. id,
  1045. )
  1046. }
  1047. }
  1048. const messages = Array.isArray(payload.messages) ? payload.messages.flatMap(value => object(value) ? [object(value)!] : []) : []
  1049. if (!Array.isArray(payload.messages) || !sessionID) return
  1050. const profile = fallbackProfile || profileOf(direct ?? {}, 'default') || 'default'
  1051. const pagination = object(payload.pagination) ?? {}
  1052. const offset = Math.max(0, Number(pagination.offset ?? payload.offset ?? 0) || 0)
  1053. const storedDetail = this.localDetail(owner, profile, sessionID)
  1054. const storedSummary = storedDetail ? parseResponse(storedDetail.response) ?? {} : {}
  1055. const total = Math.max(offset + messages.length,
  1056. Number(pagination.total ?? payload.total ?? storedSummary.message_count ?? storedSummary.messageCount ?? 0) || 0)
  1057. const start = Math.max(0, total - offset - messages.length)
  1058. messages.forEach((message, index) => {
  1059. const data = JSON.stringify(message)
  1060. this.db.prepare(`INSERT INTO chat_messages(owner,profile,session_id,message_id,position,data,content_hash)
  1061. VALUES(?,?,?,?,?,?,?) ON CONFLICT(owner,profile,session_id,message_id) DO UPDATE SET position=excluded.position,
  1062. data=excluded.data,content_hash=excluded.content_hash
  1063. WHERE chat_messages.content_hash<>excluded.content_hash OR chat_messages.position<>excluded.position`)
  1064. .run(owner, profile, sessionID, messageID(message, String(start + index)), start + index, data,
  1065. createHash('sha256').update(data).digest('hex'))
  1066. for (const sourcePath of referencedPaths(message)) {
  1067. this.db.prepare(`INSERT INTO chat_attachments(owner,profile,session_id,source_path,state,updated_at)
  1068. VALUES(?,?,?,?,?,?) ON CONFLICT(owner,profile,session_id,source_path) DO UPDATE SET updated_at=excluded.updated_at`)
  1069. .run(owner, profile, sessionID, sourcePath, 'pending', now)
  1070. }
  1071. })
  1072. const coverage = this.db.prepare(`SELECT COUNT(*) count, COUNT(DISTINCT position) positions, MIN(position) first, MAX(position) last
  1073. FROM chat_messages WHERE owner=? AND profile=? AND session_id=?`).get(owner, profile, sessionID) as Record<string, number>
  1074. const complete = total === 0 ? coverage.count === 0
  1075. : coverage.count === total && coverage.positions === total && coverage.first === 0 && coverage.last === total - 1
  1076. const row = this.db.prepare('SELECT data FROM chat_sessions WHERE owner=? AND profile=? AND session_id=?')
  1077. .get(owner, profile, sessionID) as { data: string }
  1078. const summary = mergeSessionMetadata({}, object(JSON.parse(row.data)) ?? {})
  1079. const bounds = messageTimeBounds(messages)
  1080. if (!firstTimestamp(summary, START_TIME_KEYS) && bounds.first !== undefined) summary.started_at = bounds.first
  1081. if (bounds.last !== undefined) {
  1082. summary.last_active = Math.max(firstTimestamp(summary, ACTIVITY_TIME_KEYS) ?? 0, bounds.last)
  1083. }
  1084. summary.message_count = total
  1085. this.db.prepare('UPDATE chat_sessions SET data=? WHERE owner=? AND profile=? AND session_id=?')
  1086. .run(JSON.stringify(summary), owner, profile, sessionID)
  1087. this.db.prepare(`UPDATE chat_sessions SET complete=?,message_total=?,sync_state='current',last_synced_at=?
  1088. WHERE owner=? AND profile=? AND session_id=?`).run(complete ? 1 : 0, total, now, owner, profile, sessionID)
  1089. }
  1090. private updateOwnedSessionTitle(owner: string, profile: string, sessionID: string, title: string, now: number): void {
  1091. const row = this.db.prepare(`SELECT source,data FROM chat_sessions
  1092. WHERE owner=? AND profile=? AND session_id=? AND owned_at IS NOT NULL`).get(owner, profile, sessionID) as Record<string, unknown> | undefined
  1093. if (!row) return
  1094. let summary: Record<string, unknown> = {}
  1095. try { summary = object(JSON.parse(String(row.data))) ?? {} } catch { /* Rebuild damaged display metadata below. */ }
  1096. const storedSource = sessionSource(summary)
  1097. const source = storedSource && storedSource !== 'unknown'
  1098. ? storedSource
  1099. : String(row.source || 'unknown')
  1100. this.db.prepare(`UPDATE chat_sessions SET data=?,authoritative_title=?,authoritative_title_at=?,sync_state='stale',last_synced_at=?
  1101. WHERE owner=? AND profile=? AND session_id=?`).run(JSON.stringify({
  1102. ...summary,
  1103. id: sessionID,
  1104. profile,
  1105. source,
  1106. title,
  1107. }), title, now, now, owner, profile, sessionID)
  1108. }
  1109. }
  1110. export class ChatCacheCoordinator {
  1111. private reconciling = new Map<string, Promise<void>>()
  1112. onSynchronized: (owner: string, profile: string, sessionID: string) => void = () => {}
  1113. constructor(readonly store: ChatCacheStore, readonly upstream: UpstreamServiceSession,
  1114. readonly mode: ChatCacheMode = 'prefer-local') {
  1115. const timer=setTimeout(()=>{
  1116. this.retryTimers.delete(timer)
  1117. if(this.store.isClosed||this.stopped)return
  1118. void this.restoreReadPositions()
  1119. for(const row of this.store.db.prepare('SELECT * FROM chat_recovery_jobs').all() as any[])this.schedule(row.owner,row.profile,row.session_id,!!row.force)
  1120. },0);timer.unref();this.retryTimers.add(timer)
  1121. }
  1122. readonly diagnostics = {localReads:0, recoveryAttempts:0, recoveryFailures:0, recoveryCompleted:0}
  1123. private async restoreReadPositions():Promise<void>{
  1124. if(this.stopped||this.store.isClosed)return
  1125. const scopes=this.store.db.prepare('SELECT DISTINCT owner,profile FROM chat_local_state WHERE read_initialized=0').all() as {owner:string;profile:string}[]
  1126. let failed=false
  1127. for(let i=0;i<scopes.length;i+=2)await Promise.all(scopes.slice(i,i+2).map(async({owner,profile})=>{
  1128. if(this.stopped||this.store.isClosed)return
  1129. try{
  1130. const response=await this.upstream.request('/api/session-unread',{search:new URLSearchParams({profile})})
  1131. if(this.stopped||this.store.isClosed)return
  1132. if(response.status>=500)throw new Error('Read-position import unavailable')
  1133. const payload=parseResponse(response),rows=Array.isArray(payload?.sessions)?payload.sessions:[]
  1134. this.store.db.exec('SAVEPOINT read_position_import')
  1135. try{
  1136. for(const raw of rows){const r=object(raw);if(!r)continue;const id=String(r.session_id??r.sessionId??'')
  1137. const count=Math.max(0,Number(r.read_message_count??r.readMessageCount??0)||0)
  1138. this.store.db.prepare('UPDATE chat_local_state SET read_count=?,read_initialized=1 WHERE owner=? AND profile=? AND session_id=? AND read_initialized=0').run(count,owner,profile,id)
  1139. }
  1140. this.store.db.prepare(`UPDATE chat_local_state SET read_count=COALESCE((SELECT message_total FROM chat_sessions s WHERE s.owner=chat_local_state.owner AND s.profile=chat_local_state.profile AND s.session_id=chat_local_state.session_id),0),read_initialized=1 WHERE owner=? AND profile=? AND read_initialized=0`).run(owner,profile)
  1141. this.store.db.exec('RELEASE read_position_import')
  1142. }catch(e){this.store.db.exec('ROLLBACK TO read_position_import; RELEASE read_position_import');throw e}
  1143. }catch{failed=true}
  1144. }))
  1145. if(failed&&!this.stopped&&!this.store.isClosed){const timer=setTimeout(()=>{this.retryTimers.delete(timer);void this.restoreReadPositions()},30_000);timer.unref();this.retryTimers.add(timer)}
  1146. }
  1147. private jobs = new Map<string,{owner:string;profile:string;id:string;force:boolean;attempt:number}>()
  1148. private activeJobs = new Set<string>()
  1149. private retryTimers = new Set<ReturnType<typeof setTimeout>>()
  1150. private stopped = false
  1151. close(): void {this.stopped=true;for(const timer of this.retryTimers)clearTimeout(timer);this.retryTimers.clear();this.jobs.clear()}
  1152. schedule(owner:string,profile:string,id:string,force=false): void {
  1153. if(this.stopped||this.store.isClosed||!this.store.ownsSession(owner,profile,id))return
  1154. const key=JSON.stringify([owner,profile,id]),previous=this.jobs.get(key)
  1155. if(previous){previous.force ||= force;if(force)this.store.db.prepare('UPDATE chat_recovery_jobs SET force=1 WHERE owner=? AND profile=? AND session_id=?').run(owner,profile,id);return}
  1156. this.store.db.prepare('INSERT INTO chat_recovery_jobs VALUES(?,?,?,?) ON CONFLICT(owner,profile,session_id) DO UPDATE SET force=MAX(force,excluded.force)').run(owner,profile,id,force?1:0)
  1157. this.jobs.set(key,{owner,profile,id,force,attempt:0})
  1158. const timer=setTimeout(()=>{this.retryTimers.delete(timer);this.drain()},0);timer.unref();this.retryTimers.add(timer)
  1159. }
  1160. private drain(): void {
  1161. if(this.stopped||this.store.isClosed)return
  1162. for(const [key,job] of this.jobs){
  1163. if(this.activeJobs.size>=2)break
  1164. if(this.activeJobs.has(key)||job.attempt<0)continue
  1165. this.activeJobs.add(key)
  1166. this.diagnostics.recoveryAttempts++
  1167. const force=job.force;job.force=false
  1168. void this.reconcile(job.owner,job.profile,job.id,force).then(()=>{
  1169. if(this.stopped||this.store.isClosed)return
  1170. if(job.force){job.attempt=0;return}
  1171. this.diagnostics.recoveryCompleted++
  1172. this.jobs.delete(key)
  1173. this.store.db.prepare('DELETE FROM chat_recovery_jobs WHERE owner=? AND profile=? AND session_id=?').run(job.owner,job.profile,job.id)
  1174. }).catch(()=>{
  1175. if(this.stopped||this.store.isClosed)return
  1176. this.diagnostics.recoveryFailures++
  1177. job.force ||= force
  1178. const attempt=Math.abs(job.attempt)+1;job.attempt=-attempt
  1179. const timer=setTimeout(()=>{this.retryTimers.delete(timer);job.attempt=attempt;this.drain()},Math.min(30_000,1000*2**Math.min(attempt-1,5)))
  1180. timer.unref();this.retryTimers.add(timer)
  1181. }).finally(()=>{this.activeJobs.delete(key);this.drain()})
  1182. }
  1183. }
  1184. readLocal(owner:string,kind:'list'|'detail'|'messages',profile:string,id:string|undefined,
  1185. page:{offset?:number;limit?:number;archived?:string},force=false): {response:UpstreamResponse;source:ChatCacheSource;state:ChatCacheState} {
  1186. this.diagnostics.localReads++
  1187. if(id)id=this.store.canonicalSessionID(owner,profile,id)
  1188. if(kind==='list'){
  1189. for(const row of this.store.missingSessions(owner,profile)) this.schedule(owner,row.profile,row.session_id,force)
  1190. return {response:this.store.localList(owner,profile,page),source:'local',state:this.store.needsListMetadata(owner,profile)?'syncing':'current'}
  1191. }
  1192. this.store.requireOwned(owner,profile,id!)
  1193. const local=kind==='detail'?this.store.localDetail(owner,profile,id!):this.store.messagePage(owner,profile,id!,page.offset??0,page.limit??100)
  1194. if(force||!local||local.state==='gap')this.schedule(owner,profile,id!,force)
  1195. if(!local)throw new HttpError(409,'历史正在后台补齐,请稍后重试','CHAT_HISTORY_SYNC_PENDING')
  1196. return {response:local.response,source:'local',state:this.jobs.has(JSON.stringify([owner,profile,id]))?'syncing':local.state}
  1197. }
  1198. async read(owner: string, key: string, kind: string, profile: string, sessionID: string | undefined,
  1199. load: () => Promise<UpstreamResponse>, ownedList = false,
  1200. listPage: { offset?: number; limit?: number; archived?: string } = {}): Promise<{ response: UpstreamResponse; source: ChatCacheSource; state: ChatCacheState }> {
  1201. if (this.mode === 'upstream-only') {
  1202. const upstreamResponse = await load()
  1203. return {
  1204. response: kind === 'list' && ownedList
  1205. ? this.store.mergeOwnedSessionsIntoList(owner, profile, upstreamResponse, listPage)
  1206. : upstreamResponse,
  1207. source: 'upstream',
  1208. state: 'current',
  1209. }
  1210. }
  1211. if (this.mode === 'prefer-local' && kind === 'list' && ownedList && !this.store.needsListMetadata(owner, profile)) {
  1212. return { response: this.store.localList(owner, profile, listPage), source: 'local', state: 'current' }
  1213. }
  1214. const local = kind === 'list' && ownedList && this.store.needsListMetadata(owner, profile) ? undefined : kind === 'detail' && sessionID
  1215. ? this.store.localDetail(owner, profile, sessionID) ?? this.store.snapshot(owner, key)
  1216. : this.store.snapshot(owner, key)
  1217. const projectedLocal = local && kind === 'list' && ownedList
  1218. ? {
  1219. ...local,
  1220. response: this.store.mergeOwnedSessionsIntoList(owner, profile, local.response, listPage),
  1221. }
  1222. : local
  1223. if (this.mode === 'prefer-local' && local) {
  1224. return { response: projectedLocal!.response, source: 'local', state: local.state }
  1225. }
  1226. try {
  1227. const requestStartedAt = Date.now()
  1228. const upstreamResponse = await load()
  1229. if (this.mode === 'prefer-local' && local && upstreamResponse.status >= 500) {
  1230. return { response: projectedLocal!.response, source: 'local', state: 'stale' }
  1231. }
  1232. const response = kind === 'list' && ownedList
  1233. ? this.store.mergeOwnedSessionsIntoList(
  1234. owner,
  1235. profile,
  1236. upstreamResponse,
  1237. listPage,
  1238. )
  1239. : upstreamResponse
  1240. this.store.putSnapshot(owner, key, kind, profile, sessionID, response, ownedList, requestStartedAt, upstreamResponse)
  1241. return { response, source: 'upstream', state: 'current' }
  1242. } catch (error) {
  1243. if (this.mode === 'prefer-local' && local) return { response: projectedLocal!.response, source: 'local', state: 'stale' }
  1244. throw error
  1245. }
  1246. }
  1247. async messages(owner: string, key: string, profile: string, sessionID: string, offset: number, limit: number,
  1248. load: () => Promise<UpstreamResponse>): Promise<{ response: UpstreamResponse; source: ChatCacheSource; state: ChatCacheState }> {
  1249. if (this.mode === 'prefer-local') {
  1250. // A completion may still be committing its authoritative tail. Join it for
  1251. // the latest page; older, already stored pages never wait for the network.
  1252. if (offset === 0) await this.reconciling.get(JSON.stringify([owner, profile, sessionID]))?.catch(() => {})
  1253. const page = this.store.messagePage(owner, profile, sessionID, offset, limit)
  1254. if (page) return { response: page.response, source: 'local', state: page.state }
  1255. }
  1256. return this.read(owner, key, 'messages', profile, sessionID, load)
  1257. }
  1258. observe(owner: string, profile: string, sessionID: string, frame: Record<string, unknown>): void {
  1259. if (!this.store.recordEvent(owner, profile, sessionID, frame)) return
  1260. if (String(frame.type).includes('reset') || this.store.localDetail(owner,profile,sessionID)?.state==='gap'
  1261. || (['message.complete','run.completed'].includes(String(frame.type)) && this.store.localDetail(owner,profile,sessionID)?.state!=='current')) {
  1262. this.schedule(owner, profile, sessionID)
  1263. }
  1264. }
  1265. observeGlobal(owner: string, type: string, upstreamInvalidation=false): void {
  1266. if (type !== 'sessions.changed' || this.stopped || this.store.isClosed) return
  1267. this.store.markListsStale(owner)
  1268. if(upstreamInvalidation)for(const row of this.store.invalidateOwnedMetadata(owner))this.schedule(owner,row.profile,row.session_id)
  1269. }
  1270. command(owner: string, profile: string, sessionID: string, method: string,
  1271. params: Record<string, unknown>): void {
  1272. this.store.recordCommand(owner, profile, sessionID, method, params)
  1273. }
  1274. route(owner: string, profile: string, sessionID: string, runtimeID: string): void {
  1275. this.store.recordRoute(owner, profile, sessionID, runtimeID)
  1276. }
  1277. async reconcile(owner: string, profile: string, sessionID: string, force = false): Promise<void> {
  1278. if(this.store.isClosed)return
  1279. sessionID=this.store.canonicalSessionID(owner,profile,sessionID)
  1280. if (this.stopped || this.store.isClosed || !this.store.ownsSession(owner, profile, sessionID)) return
  1281. const key = JSON.stringify([owner, profile, sessionID])
  1282. const pending = this.reconciling.get(key)
  1283. if (pending) {
  1284. await pending.catch(() => {})
  1285. if (force || this.store.localDetail(owner, profile, sessionID)?.state !== 'current') {
  1286. return this.reconcile(owner, profile, sessionID, force)
  1287. }
  1288. return
  1289. }
  1290. const task = (async () => {
  1291. const revision = this.store.revision(owner, profile, sessionID)
  1292. const previous = this.store.messagePage(owner, profile, sessionID, 0, 1)
  1293. const previousTotal = previous ? Number(object(parseResponse(previous.response)?.pagination)?.total ?? 0) : 0
  1294. const detailPath = `/api/sessions/${encodeURIComponent(sessionID)}`
  1295. const detailStartedAt = Date.now()
  1296. const detail = await this.upstream.request(detailPath, { search: new URLSearchParams({ profile }), cache: 'reload' })
  1297. if (this.stopped || this.store.isClosed) return
  1298. if (detail.status !== 200) throw new Error('Unable to synchronize conversation metadata')
  1299. if(!force&&this.store.refreshMetadata(owner,profile,sessionID,revision,detail,detailStartedAt)) {
  1300. this.onSynchronized(owner,profile,sessionID)
  1301. return
  1302. }
  1303. const detailPayload = parseResponse(detail) ?? {}
  1304. const detailSummary = object(detailPayload.session) ?? detailPayload
  1305. const summaryTotal = Math.max(0, Number(detailSummary.message_count ?? detailSummary.messageCount ?? previousTotal) || 0)
  1306. const pages = [{ key: `detail:${profile}:${sessionID}`, kind: 'detail', response: detail, startedAt: detailStartedAt, offset: 0 }]
  1307. let offset = 0
  1308. let done = false
  1309. let expectedTotal: number | undefined
  1310. let resolvedTotal = summaryTotal
  1311. // Stop as soon as the authoritative tail overlaps durable messages. Only
  1312. // explicit refresh (or uncached history) walks all the way to the beginning.
  1313. for (let page = 0; page < 1_000; page++) {
  1314. const search = new URLSearchParams({ profile, offset: String(offset), limit: '100', order: 'latest', include_compacted: 'true' })
  1315. const startedAt = Date.now()
  1316. const response = await this.upstream.request(`${detailPath}/messages`, { search, cache: 'reload' })
  1317. if (this.stopped || this.store.isClosed) return
  1318. const payload = parseResponse(response)
  1319. if (response.status !== 200 || !Array.isArray(payload?.messages)) throw new Error('Unable to synchronize conversation messages')
  1320. const messages = payload.messages.flatMap(value => object(value) ? [object(value)!] : [])
  1321. const pagination = object(payload.pagination) ?? {}
  1322. const explicitTotal = pagination.total ?? payload.total
  1323. const total = Math.max(offset + messages.length, Number(explicitTotal ?? summaryTotal) || 0)
  1324. if (!force && total < previousTotal) throw new Error('Stored history changed; explicit refresh is required')
  1325. if (explicitTotal !== undefined) {
  1326. if (expectedTotal !== undefined && expectedTotal !== total) throw new Error('Conversation changed during synchronization')
  1327. expectedTotal = total
  1328. }
  1329. const anchors = !force ? messages.map((message, index) =>
  1330. this.store.hasMessage(owner, profile, sessionID, message, total - offset - messages.length + index)) : []
  1331. const overlap = anchors.some(Boolean)
  1332. pages.push({ key: `messages:${profile}:${sessionID}:${offset}:100`, kind: 'messages', response, startedAt, offset })
  1333. const more = pagination.hasMore ?? pagination.has_more ?? payload.hasMore ?? payload.has_more
  1334. const hasMore = more === undefined ? messages.length >= 100 : bool(more)
  1335. if (overlap || !hasMore) {
  1336. if (!overlap && expectedTotal !== undefined && expectedTotal !== offset + messages.length) throw new Error('Conversation pagination ended before its declared total')
  1337. resolvedTotal = overlap ? total : offset + messages.length
  1338. done = true
  1339. break
  1340. }
  1341. if (!messages.length) break
  1342. offset += messages.length
  1343. }
  1344. if (!done) throw new Error('Conversation synchronization is incomplete')
  1345. // Newer Hermes omits total/has_more. Once the end is known, project the
  1346. // same verified total onto every page before assigning absolute positions.
  1347. for (let index = 0; index < pages.length; index++) {
  1348. const entry = pages[index]!
  1349. const payload = parseResponse(entry.response)!
  1350. if (entry.kind === 'detail') {
  1351. const summary = object(payload.session) ?? payload
  1352. summary.message_count = resolvedTotal
  1353. } else {
  1354. const pagination = object(payload.pagination) ?? {}
  1355. const pageOffset = entry.offset
  1356. payload.pagination = { ...pagination, total: resolvedTotal, offset: pageOffset,
  1357. has_more: pageOffset + (payload.messages as unknown[]).length < resolvedTotal }
  1358. }
  1359. const headers = new Headers(entry.response.headers)
  1360. headers.delete('content-length')
  1361. entry.response = { ...entry.response, headers, body: Buffer.from(JSON.stringify(payload)) }
  1362. }
  1363. if (!this.store.applySync(owner, profile, sessionID, revision, pages, force)) {
  1364. // Preserve the invalidation when a newer event arrived during the read.
  1365. // Its completion joins this flight and schedules the next tail read.
  1366. throw new Error('Conversation changed during refresh; retry after the reply completes')
  1367. } else {
  1368. this.onSynchronized(owner, profile, sessionID)
  1369. }
  1370. })().finally(() => { if (this.reconciling.get(key) === task) this.reconciling.delete(key) })
  1371. this.reconciling.set(key, task)
  1372. return task
  1373. }
  1374. async cacheAttachment(owner: string, sourcePath: string): Promise<void> {
  1375. if (this.mode === 'upstream-only' || !this.store.knowsAttachment(owner, sourcePath) || this.store.attachment(owner, sourcePath)) return
  1376. try {
  1377. const response = await this.upstream.request('/api/files/download', {
  1378. search: new URLSearchParams({ path: sourcePath }), maxResponseBytes: 100 * 1_024 * 1_024,
  1379. })
  1380. this.store.storeAttachment(owner, sourcePath, response)
  1381. } catch { /* The next authorized request can retry. */ }
  1382. }
  1383. }
  1384. export function chatCacheKey(kind: string, profile: string, sessionID: string | undefined, search: URLSearchParams): string {
  1385. const normalized = new URLSearchParams(search)
  1386. normalized.sort()
  1387. return [kind, profile, sessionID ?? '', normalized.toString()].join(':')
  1388. }