index.ts 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833
  1. /**
  2. * Log-backed session title service, deterministic fallback, and provider contract.
  3. * @module @deepseek-ai/dsh-session-title
  4. */
  5. import { Context, FiberState, Service, type Fiber } from '@deepseek-ai/cordis'
  6. import z from '@deepseek-ai/schemastery'
  7. import { z as zod } from 'zod'
  8. import type { ZodType } from 'zod'
  9. import type { Branded } from '@deepseek-ai/dsh-brand'
  10. import { isAgentLoopRequest } from '@deepseek-ai/dsh-llm'
  11. import type { GenerateOptions } from '@deepseek-ai/dsh-llm'
  12. import { assertNever, deepFreeze } from '@deepseek-ai/dsh-util-values'
  13. import type {
  14. Session,
  15. SessionEvent,
  16. } from '@deepseek-ai/dsh-session'
  17. import { SessionSeq } from '@deepseek-ai/dsh-session'
  18. import type {} from '@deepseek-ai/dsh-session-projection'
  19. import type { ProjectionDefinition } from '@deepseek-ai/dsh-session-projection'
  20. import type {} from '@deepseek-ai/dsh-agent'
  21. export type {
  22. SessionTitleEventData,
  23. SessionTitleModelProvenance,
  24. SessionTitleSnapshot,
  25. SessionTitleSource,
  26. SessionTitleUserMessage,
  27. TitleProjection,
  28. } from './types.ts'
  29. import { fallbackSessionTitle, normalizeSessionTitle } from './normalize.ts'
  30. import type {
  31. SessionTitleEventData,
  32. SessionTitleModelProvenance,
  33. SessionTitleSnapshot,
  34. SessionTitleSource,
  35. SessionTitleUserMessage,
  36. TitleInputState,
  37. TitleProjection,
  38. } from './types.ts'
  39. /** Identifies one session-title provider registration. */
  40. export type SessionTitleProviderId = Branded<'SessionTitleProviderId'>
  41. /**
  42. * Brand a raw provider id.
  43. * @param id - stable non-empty provider identifier supplied by a plugin.
  44. * @returns the same string with the session-title provider brand.
  45. */
  46. export function SessionTitleProviderId(id: string): SessionTitleProviderId {
  47. return id as SessionTitleProviderId
  48. }
  49. export { fallbackSessionTitle, normalizeSessionTitle, truncateTitleUtf8 } from './normalize.ts'
  50. /** Required deterministic fallback and accepted-title limits. */
  51. export interface Config {
  52. /** Maximum whitespace-delimited words in the built-in fallback. */
  53. readonly fallbackMaxWords: number
  54. /** Maximum UTF-8 bytes in the built-in fallback. */
  55. readonly fallbackMaxBytes: number
  56. /** Maximum UTF-8 bytes in any accepted title. */
  57. readonly maxTitleBytes: number
  58. }
  59. declare module '@deepseek-ai/cordis' {
  60. interface Context {
  61. sessionTitle: SessionTitleService
  62. }
  63. }
  64. declare module '@deepseek-ai/dsh-session/types' {
  65. interface SessionEventMap {
  66. /**
  67. * Latest-wins session title snapshot. Log-only: it never enters the model
  68. * surface or derived history.
  69. */
  70. 'session/title': SessionTitleEventData
  71. }
  72. }
  73. /**
  74. * Rejection of an explicit user title whose text normalizes to empty — the
  75. * one {@link SessionTitleService.rename} failure that blames the input.
  76. * Callers translating rename failures onto a wire (`title-invalid`) narrow on
  77. * this class; liveness and disposal failures stay plain `Error`s.
  78. */
  79. export class SessionTitleInvalidError extends Error {
  80. override readonly name = 'SessionTitleInvalidError'
  81. }
  82. /** Automatic generation cadence owned by a registered provider. */
  83. export type SessionTitleAutomaticMode = 'first-prompt' | 'all-prompts'
  84. /** Immutable input supplied to one title-provider call. */
  85. export interface SessionTitleProviderRequest {
  86. /** Live session being titled. */
  87. readonly session: Session
  88. /** All eligible human messages through this generation revision. */
  89. readonly messages: readonly SessionTitleUserMessage[]
  90. /** Exact current logged main-request route, when one has been recorded. */
  91. readonly route?: SessionTitleModelProvenance
  92. /** Cancellation for supersession, disposal, timeout composition, or the explicit caller. */
  93. readonly signal: AbortSignal
  94. }
  95. /** Provider output before service-owned normalization and log acceptance. */
  96. export interface SessionTitleProviderResult {
  97. /** Proposed title text. */
  98. readonly title: string
  99. /** Exact seqs from `request.messages` used by this result. */
  100. readonly messageSeqs: readonly SessionSeq[]
  101. /** Auxiliary LLM route, when generation used a model. */
  102. readonly model?: SessionTitleModelProvenance
  103. }
  104. /** One optional asynchronous title implementation registered with the service. */
  105. export interface SessionTitleProvider {
  106. /** Stable id of the provider recorded with the title. */
  107. readonly id: SessionTitleProviderId
  108. /** When new human prompts start automatic generation. */
  109. readonly automatic: SessionTitleAutomaticMode
  110. /**
  111. * Produce one title revision.
  112. * @param request - message snapshot, current route, session, and cancellation.
  113. * @returns proposed title plus exact input seqs and the optional provider/model route used to generate it.
  114. */
  115. generate(request: SessionTitleProviderRequest): Promise<SessionTitleProviderResult>
  116. }
  117. /** Extract one eligible human text message from a session event. */
  118. function sessionTitleUserMessageOf(event: SessionEvent): SessionTitleUserMessage | undefined {
  119. if (event.type !== 'user/message' || event.data.source.kind !== 'user') return undefined
  120. const content = event.data.content
  121. const text = content
  122. .filter((block): block is Extract<(typeof content)[number], { type: 'text' }> => block.type === 'text')
  123. .map(block => block.text)
  124. .join('\n')
  125. if (normalizeSessionTitle(text, Number.MAX_SAFE_INTEGER).length === 0) return undefined
  126. return { seq: event.seq, text }
  127. }
  128. /** Defensive copy of a logged title source (the snapshot must not alias log-owned objects). */
  129. function copySessionTitleSource(source: SessionTitleSource): SessionTitleSource {
  130. switch (source.kind) {
  131. case 'fallback': return { kind: 'fallback' }
  132. case 'provider': return {
  133. kind: 'provider',
  134. provider: source.provider,
  135. ...(source.model === undefined ? {} : { model: { ...source.model } }),
  136. }
  137. case 'user': return { kind: 'user' }
  138. /* v8 ignore next -- closed-union exhaustiveness guard */
  139. default: return assertNever(source, 'SessionTitleSource')
  140. }
  141. }
  142. /** Service-owned resolved limits. */
  143. interface ResolvedConfig {
  144. readonly fallbackMaxWords: number
  145. readonly fallbackMaxBytes: number
  146. readonly maxTitleBytes: number
  147. }
  148. /** One exact provider registration generation. */
  149. interface ProviderRegistration {
  150. readonly provider: SessionTitleProvider
  151. readonly active: Set<Promise<unknown>>
  152. closing: boolean
  153. }
  154. /** Automatic work waiting for the matching main-request header. */
  155. interface PendingAutomaticWork {
  156. readonly registration: ProviderRegistration
  157. readonly revision: number
  158. readonly throughSeq: SessionSeq
  159. }
  160. /** Provider call currently allowed to commit for one session. */
  161. interface ActiveProviderWork extends PendingAutomaticWork {
  162. readonly controller: AbortController
  163. readonly signal: AbortSignal
  164. }
  165. /** Mutable concurrency state scoped to one live session. */
  166. interface SessionTitleWorkState {
  167. revision: number
  168. fallback?: Promise<SessionTitleSnapshot | undefined>
  169. pending?: PendingAutomaticWork
  170. active?: ActiveProviderWork
  171. }
  172. /** Validate one positive integer configuration field. */
  173. function assertPositiveInteger(name: keyof Config, value: number): void {
  174. if (!Number.isInteger(value) || value <= 0) {
  175. throw new Error(`session-title: ${name} must be a positive integer`)
  176. }
  177. }
  178. /**
  179. * Convert title projection state into an immutable snapshot.
  180. * @param state - the title unit's folded state.
  181. * @returns the immutable snapshot.
  182. */
  183. function titleSnapshotFromState(state: TitleProjection): SessionTitleSnapshot {
  184. return deepFreeze({
  185. title: state.title,
  186. messageSeqs: [...state.messageSeqs],
  187. source: copySessionTitleSource(state.source),
  188. eventSeq: state.eventSeq,
  189. updatedAt: state.updatedAt,
  190. })
  191. }
  192. const EMPTY_TITLE_INPUT: TitleInputState = { first: null, count: 0, lastSeq: null }
  193. const sessionTitleUserMessageSchema: ZodType<SessionTitleUserMessage> = zod.object({
  194. seq: zod.number().int().nonnegative().max(Number.MAX_SAFE_INTEGER).transform(SessionSeq),
  195. text: zod.string(),
  196. }).strict()
  197. const titleInputStateSchema: ZodType<TitleInputState> = zod.object({
  198. first: sessionTitleUserMessageSchema.nullable(),
  199. count: zod.number().int().nonnegative(),
  200. lastSeq: zod.number().int().nonnegative().max(Number.MAX_SAFE_INTEGER).transform(SessionSeq).nullable(),
  201. }).strict().superRefine((state, context) => {
  202. const empty = state.first === null && state.lastSeq === null && state.count === 0
  203. const populated = state.first !== null
  204. && state.lastSeq !== null
  205. && state.count > 0
  206. && state.first.seq <= state.lastSeq
  207. if (!empty && !populated) {
  208. context.addIssue({
  209. code: 'custom',
  210. message: 'title input state must pair its count with first and last message seqs',
  211. })
  212. }
  213. })
  214. /**
  215. * Collect eligible human text messages from a session log, in seq order.
  216. * The full eligible prefix is only materialized for one provider generation,
  217. * so it is scanned from the log at execution time rather than retained by
  218. * the O(1) `titleInput` projection.
  219. * @param events - the session event log.
  220. * @param throughSeq - optional inclusive upper seq bound.
  221. * @returns eligible messages with exact source seqs.
  222. */
  223. function collectSessionTitleMessages(
  224. events: readonly SessionEvent[],
  225. throughSeq?: SessionSeq,
  226. ): SessionTitleUserMessage[] {
  227. const messages: SessionTitleUserMessage[] = []
  228. for (const event of events) {
  229. if (throughSeq !== undefined && event.seq > throughSeq) break
  230. const message = sessionTitleUserMessageOf(event)
  231. if (message !== undefined) messages.push(message)
  232. }
  233. return messages
  234. }
  235. const titleViewSchema: ZodType<string | null> = zod.string().min(1).nullable()
  236. /** Latest logged title text and its client view. */
  237. export const titleProjectionDefinition = {
  238. key: 'title',
  239. stateVersion: 1,
  240. stateSchema: titleViewSchema,
  241. init: () => null,
  242. apply: (state, event) => (event.type === 'session/title'
  243. ? event.data.title
  244. : state),
  245. wire: {
  246. viewSchema: titleViewSchema,
  247. view: state => state,
  248. },
  249. } satisfies ProjectionDefinition<'title', string | null>
  250. /**
  251. * Fold the latest logged title without consulting mutable metadata.
  252. * @param events - live or persisted session log.
  253. * @returns the latest immutable title snapshot, or `undefined`.
  254. */
  255. export function foldSessionTitle(events: readonly SessionEvent[]): SessionTitleSnapshot | undefined {
  256. const event = events.findLast(item => item.type === 'session/title')
  257. if (event === undefined) return undefined
  258. return titleSnapshotFromState({
  259. title: event.data.title,
  260. messageSeqs: event.data.messageSeqs,
  261. source: event.data.source,
  262. eventSeq: event.seq,
  263. updatedAt: event.time,
  264. })
  265. }
  266. /** Log-backed title fold plus asynchronous fallback generation. */
  267. export class SessionTitleService extends Service {
  268. static inject = ['sessions', 'sessionProjections']
  269. static Config: z<Config> = z.object({
  270. fallbackMaxWords: z.number().step(1).min(1).required(),
  271. fallbackMaxBytes: z.number().step(1).min(1).required(),
  272. maxTitleBytes: z.number().step(1).min(1).required(),
  273. })
  274. private readonly config: ResolvedConfig
  275. private readonly ownerFiber: Fiber
  276. private registration: ProviderRegistration | undefined
  277. private readonly work = new Map<Session, SessionTitleWorkState>()
  278. private readonly lifetime = new AbortController()
  279. private readonly inFlight = new Set<Promise<unknown>>()
  280. constructor(ctx: Context, config: Config) {
  281. super(ctx, 'sessionTitle')
  282. this.ownerFiber = ctx.fiber
  283. const candidate: unknown = config
  284. if (candidate === null || typeof candidate !== 'object') {
  285. throw new Error('session-title: configuration is required')
  286. }
  287. const value = candidate as Config
  288. assertPositiveInteger('fallbackMaxWords', value.fallbackMaxWords)
  289. assertPositiveInteger('fallbackMaxBytes', value.fallbackMaxBytes)
  290. assertPositiveInteger('maxTitleBytes', value.maxTitleBytes)
  291. if (value.fallbackMaxBytes > value.maxTitleBytes) {
  292. throw new Error('session-title: fallbackMaxBytes must not exceed maxTitleBytes')
  293. }
  294. this.config = deepFreeze({ ...value })
  295. ctx.effect(() => async () => {
  296. this.lifetime.abort(new Error('session-title service disposed'))
  297. if (this.registration !== undefined) this.registration.closing = true
  298. this.registration = undefined
  299. for (const state of this.work.values()) {
  300. delete state.pending
  301. state.active?.controller.abort(new Error('session-title service disposed'))
  302. }
  303. await this.drain(this.inFlight)
  304. this.work.clear()
  305. }, 'sessionTitle lifecycle')
  306. ctx.sessionProjections.register(titleProjectionDefinition)
  307. ctx.sessionProjections.register<'titleInput', TitleInputState>({
  308. key: 'titleInput',
  309. stateVersion: 3,
  310. stateSchema: titleInputStateSchema,
  311. init: () => EMPTY_TITLE_INPUT,
  312. apply: (state, event) => {
  313. const message = sessionTitleUserMessageOf(event)
  314. if (message === undefined) return state
  315. return {
  316. first: state.first ?? message,
  317. count: state.count + 1,
  318. lastSeq: message.seq,
  319. }
  320. },
  321. })
  322. ctx.on('session/event', (session, event) => {
  323. switch (event.type) {
  324. case 'user/message':
  325. this.onUserMessage(session, event)
  326. break
  327. case 'request/header':
  328. this.onRequestHeader(session, event)
  329. break
  330. default:
  331. break
  332. }
  333. })
  334. ctx.on('llm/stream', (options, next) => {
  335. this.onMainRequest(options)
  336. return next()
  337. }, { global: true, prepend: true })
  338. ctx.on('session/disposed', (session) => {
  339. const state = this.work.get(session)
  340. if (state === undefined) return
  341. state.active?.controller.abort(new Error('session disposed during title generation'))
  342. this.work.delete(session)
  343. })
  344. }
  345. /**
  346. * Read the latest folded title from one live or replayed session.
  347. * @param session - session whose log is the title source of truth.
  348. * @returns latest title snapshot, or `undefined` before eligible input.
  349. */
  350. get(session: Session): SessionTitleSnapshot | undefined {
  351. return foldSessionTitle(session.snapshotEvents())
  352. }
  353. /**
  354. * Accept an explicit user title. Appends a `session/title` event with the
  355. * `user` source, which pins the title: in-flight automatic generation is
  356. * superseded and later user messages schedule none (an explicit
  357. * {@link SessionTitleService.refresh} remains the deliberate unpin).
  358. * @param session - exact live session to rename.
  359. * @param title - raw user input; normalized before acceptance.
  360. * @returns the accepted title snapshot.
  361. * @throws {SessionTitleInvalidError} when the title normalizes to empty.
  362. * @throws {Error} when the session is not live or the service is disposed.
  363. */
  364. rename(session: Session, title: string): SessionTitleSnapshot {
  365. this.assertServiceActive()
  366. if (this.ctx.sessions.get(session.id) !== session) {
  367. throw new Error(`session "${session.id}" is not live in this store`)
  368. }
  369. const normalized = normalizeSessionTitle(title, this.config.maxTitleBytes)
  370. if (normalized.length === 0) {
  371. throw new SessionTitleInvalidError('session title must contain visible characters')
  372. }
  373. const state = this.stateFor(session)
  374. this.supersede(state, 'user rename superseded automatic title generation')
  375. session.append('session/title', {
  376. title: normalized,
  377. messageSeqs: [],
  378. source: { kind: 'user' },
  379. })
  380. const snapshot = this.get(session)
  381. /* v8 ignore next -- unreachable: the append above just committed a session/title event. */
  382. if (snapshot === undefined) throw new Error('renamed title failed to fold')
  383. return snapshot
  384. }
  385. /**
  386. * Explicitly retry the registered provider, or materialize the built-in
  387. * fallback when no provider is registered.
  388. * @param session - exact live session to refresh.
  389. * @param signal - optional caller cancellation.
  390. * @returns latest accepted title, or `undefined` when no eligible text exists.
  391. */
  392. async refresh(session: Session, signal?: AbortSignal): Promise<SessionTitleSnapshot | undefined> {
  393. signal?.throwIfAborted()
  394. this.assertServiceActive()
  395. if (this.ctx.sessions.get(session.id) !== session) {
  396. throw new Error(`session "${session.id}" is not live in this store`)
  397. }
  398. const registration = this.registration
  399. const input = this.titleInputOf(session)
  400. if (registration === undefined || registration.closing || input.lastSeq === null) {
  401. // Explicit refresh is the unpin even without a provider: a standing
  402. // user title must not short-circuit ensureFallback into a no-op, so
  403. // re-derive and append the fallback over it when one is derivable.
  404. const current = this.get(session)
  405. const first = input.first
  406. if (current?.source.kind === 'user' && first !== null) {
  407. this.appendFallback(session, first)
  408. signal?.throwIfAborted()
  409. return this.get(session)
  410. }
  411. const fallback = await this.ensureFallback(session)
  412. signal?.throwIfAborted()
  413. return fallback
  414. }
  415. const state = this.stateFor(session)
  416. const revision = this.supersede(state, 'explicit title refresh superseded older generation')
  417. const work = this.activate({
  418. registration,
  419. revision,
  420. throughSeq: input.lastSeq,
  421. }, state, signal)
  422. const config = session.requestHeader()?.config
  423. const route = config === undefined ? undefined : { provider: config.provider, model: config.model }
  424. return this.startProvider(session, work, route)
  425. }
  426. /**
  427. * Register the sole optional title provider. Disposal aborts its pending and
  428. * active work before another provider may register.
  429. * @param provider - provider identity, cadence, and generation function.
  430. * @returns exact Cordis effect disposer, which settles after active calls quiesce.
  431. */
  432. register(provider: SessionTitleProvider): () => Promise<void> {
  433. this.validateProvider(provider)
  434. if (this.registration !== undefined) {
  435. throw new Error(`session-title provider "${this.registration.provider.id}" is already registered`)
  436. }
  437. const registration: ProviderRegistration = {
  438. provider,
  439. active: new Set(),
  440. closing: false,
  441. }
  442. const dispose = this.ctx.effect(function* (this: SessionTitleService) {
  443. this.registration = registration
  444. yield async () => {
  445. registration.closing = true
  446. for (const state of this.work.values()) {
  447. if (state.pending?.registration === registration) delete state.pending
  448. if (state.active?.registration === registration) {
  449. state.active.controller.abort(new Error(`session-title provider "${provider.id}" was disposed`))
  450. }
  451. }
  452. await this.drain(registration.active)
  453. if (this.registration === registration) this.registration = undefined
  454. }
  455. }.bind(this), 'sessionTitle.register()')
  456. return dispose
  457. }
  458. /** Schedule fallback creation and any provider cadence for one eligible event. */
  459. private onUserMessage(session: Session, event: Extract<SessionEvent, { type: 'user/message' }>): void {
  460. if (!this.serviceActive()) return
  461. if (event.data.source.kind !== 'user' || sessionTitleUserMessageOf(event) === undefined) return
  462. // A user rename pins the title: no automatic revision may override it.
  463. if (this.get(session)?.source.kind === 'user') return
  464. const registration = this.registration
  465. if (registration !== undefined && !registration.closing) {
  466. const count = this.titleInputOf(session).count
  467. const shouldSchedule = registration.provider.automatic === 'all-prompts'
  468. || (session.header.parentSession === undefined && count === 1 && this.get(session) === undefined)
  469. if (shouldSchedule) {
  470. const state = this.stateFor(session)
  471. const revision = this.supersede(state, 'newer user message superseded title generation')
  472. state.pending = { registration, revision, throughSeq: event.seq }
  473. }
  474. }
  475. this.defer(async () => {
  476. try {
  477. await this.ensureFallback(session)
  478. } catch (error: unknown) {
  479. if (!this.serviceActive()) return
  480. this.ctx.logger.warn(`session "${session.id}": fallback title update failed: ${String(error)}`)
  481. }
  482. })
  483. }
  484. /** Start pending automatic work only after its exact main-request route is logged. */
  485. private onRequestHeader(session: Session, event: Extract<SessionEvent, { type: 'request/header' }>): void {
  486. if (!this.serviceActive()) return
  487. const state = this.work.get(session)
  488. const pending = state?.pending
  489. if (state === undefined || pending === undefined || pending.throughSeq >= event.seq) return
  490. const route = {
  491. provider: event.data.header.config.provider,
  492. model: event.data.header.config.model,
  493. }
  494. this.startPending(session, state, pending, route)
  495. }
  496. /** Start unchanged-route work from the marked loop request after its header fold is current. */
  497. private onMainRequest(options: GenerateOptions): void {
  498. if (!this.serviceActive() || options.sessionId === undefined || !isAgentLoopRequest(options)) return
  499. const session = this.ctx.sessions.get(options.sessionId)
  500. const state = session === undefined ? undefined : this.work.get(session)
  501. const pending = state?.pending
  502. if (session === undefined || state === undefined || pending === undefined) return
  503. const boundary = this.ctx.sessionProjections.stateOf(session, 'turnBoundary')?.lastStepBoundary
  504. const route = session.requestHeader()?.config
  505. if (boundary?.kind !== 'start'
  506. || boundary.seq <= pending.throughSeq
  507. || route?.provider !== options.provider
  508. || route.model !== options.model) return
  509. this.startPending(session, state, pending, { provider: options.provider, model: options.model })
  510. }
  511. /** Consume one pending revision and schedule its non-blocking provider call. */
  512. private startPending(
  513. session: Session,
  514. state: SessionTitleWorkState,
  515. pending: PendingAutomaticWork,
  516. route: SessionTitleModelProvenance,
  517. ): void {
  518. delete state.pending
  519. this.defer(async () => {
  520. if (this.registration !== pending.registration
  521. || pending.registration.closing
  522. || this.work.get(session) !== state
  523. || state.revision !== pending.revision) return
  524. const work = this.activate(pending, state)
  525. try {
  526. await this.startProvider(session, work, route)
  527. } catch (error: unknown) {
  528. if (work.signal.aborted || !this.serviceActive()) return
  529. this.ctx.logger.warn(`session "${session.id}": automatic title generation failed: ${String(error)}`)
  530. }
  531. })
  532. }
  533. /** Start one tracked provider call after publishing its active revision. */
  534. private startProvider(
  535. session: Session,
  536. work: ActiveProviderWork,
  537. route?: SessionTitleModelProvenance,
  538. ): Promise<SessionTitleSnapshot | undefined> {
  539. const run = Promise.resolve().then(() => this.runProvider(session, work, route))
  540. return this.track(run, work.registration)
  541. }
  542. /** Execute and accept one current provider revision. */
  543. private async runProvider(
  544. session: Session,
  545. work: ActiveProviderWork,
  546. route?: SessionTitleModelProvenance,
  547. ): Promise<SessionTitleSnapshot | undefined> {
  548. try {
  549. this.assertCurrent(session, work)
  550. await this.ensureFallback(session)
  551. this.assertCurrent(session, work)
  552. const messages = collectSessionTitleMessages(session.snapshotEvents(), work.throughSeq)
  553. const result = await work.registration.provider.generate({
  554. session,
  555. messages,
  556. ...route === undefined ? {} : { route },
  557. signal: work.signal,
  558. })
  559. this.assertCurrent(session, work)
  560. const accepted = this.validateResult(result, messages)
  561. session.append('session/title', {
  562. title: accepted.title,
  563. messageSeqs: [...accepted.messageSeqs],
  564. source: {
  565. kind: 'provider',
  566. provider: work.registration.provider.id,
  567. ...accepted.model === undefined ? {} : { model: accepted.model },
  568. },
  569. })
  570. return this.get(session)
  571. } finally {
  572. const state = this.work.get(session)
  573. if (state?.active === work) delete state.active
  574. }
  575. }
  576. /** Validate and normalize provider output against the supplied message snapshot. */
  577. private validateResult(
  578. result: unknown,
  579. messages: readonly SessionTitleUserMessage[],
  580. ): SessionTitleProviderResult {
  581. if (result === null || typeof result !== 'object') {
  582. throw new Error('session-title provider returned an invalid result')
  583. }
  584. const candidate = result as Record<string, unknown>
  585. if (typeof candidate.title !== 'string') throw new Error('session-title provider title must be a string')
  586. const title = normalizeSessionTitle(candidate.title, this.config.maxTitleBytes)
  587. if (title.length === 0) throw new Error('session-title provider returned an empty title')
  588. if (!Array.isArray(candidate.messageSeqs) || candidate.messageSeqs.length === 0) {
  589. throw new Error('session-title provider must identify at least one source message seq')
  590. }
  591. const messageSeqs: SessionSeq[] = []
  592. const order = new Map(messages.map((message, index) => [message.seq, index]))
  593. let previous = -1
  594. for (const seq of candidate.messageSeqs as unknown[]) {
  595. if (typeof seq !== 'number' || !Number.isSafeInteger(seq) || seq < 0) {
  596. throw new Error('session-title provider messageSeqs must be unique, ordered seqs from the request')
  597. }
  598. const sessionSeq = SessionSeq(seq)
  599. const index = order.get(sessionSeq)
  600. if (index === undefined || index <= previous) {
  601. throw new Error('session-title provider messageSeqs must be unique, ordered seqs from the request')
  602. }
  603. messageSeqs.push(sessionSeq)
  604. previous = index
  605. }
  606. const modelCandidate = candidate.model
  607. let model: SessionTitleModelProvenance | undefined
  608. if (modelCandidate !== undefined) {
  609. if (modelCandidate === null || typeof modelCandidate !== 'object') {
  610. throw new Error('session-title provider result model must contain non-empty provider and model strings')
  611. }
  612. const record = modelCandidate as Record<string, unknown>
  613. if (typeof record.provider !== 'string' || record.provider.length === 0
  614. || typeof record.model !== 'string' || record.model.length === 0) {
  615. throw new Error('session-title provider result model must contain non-empty provider and model strings')
  616. }
  617. model = { provider: record.provider, model: record.model }
  618. }
  619. return {
  620. title,
  621. messageSeqs,
  622. ...(model === undefined ? {} : { model }),
  623. }
  624. }
  625. /** Fail a completion whose provider, revision, session, or signal is stale. */
  626. private assertCurrent(session: Session, work: ActiveProviderWork): void {
  627. this.assertServiceActive()
  628. work.signal.throwIfAborted()
  629. const state = this.work.get(session)
  630. /* v8 ignore next -- every supported supersession, provider disposal, and session disposal aborts
  631. * the work signal before changing this state. */
  632. if (this.registration !== work.registration
  633. || state?.active !== work
  634. || state.revision !== work.revision
  635. || this.ctx.sessions.get(session.id) !== session) {
  636. throw new Error('session title generation state changed without cancellation')
  637. }
  638. }
  639. /** Create and publish an active provider call from one fixed revision. */
  640. private activate(
  641. pending: PendingAutomaticWork,
  642. state: SessionTitleWorkState,
  643. upstream?: AbortSignal,
  644. ): ActiveProviderWork {
  645. const controller = new AbortController()
  646. const signal = upstream === undefined
  647. ? AbortSignal.any([controller.signal, this.lifetime.signal])
  648. : AbortSignal.any([controller.signal, this.lifetime.signal, upstream])
  649. const work: ActiveProviderWork = { ...pending, controller, signal }
  650. state.active = work
  651. return work
  652. }
  653. /** Abort older active work and reserve the next session-local revision. */
  654. private supersede(state: SessionTitleWorkState, reason: string): number {
  655. state.active?.controller.abort(new Error(reason))
  656. delete state.pending
  657. state.revision += 1
  658. return state.revision
  659. }
  660. /** Return mutable work state for one session. */
  661. private stateFor(session: Session): SessionTitleWorkState {
  662. let state = this.work.get(session)
  663. if (state === undefined) {
  664. state = { revision: 0 }
  665. this.work.set(session, state)
  666. }
  667. return state
  668. }
  669. private titleInputOf(session: Session): TitleInputState {
  670. return this.ctx.sessionProjections.stateOf(session, 'titleInput') as TitleInputState
  671. }
  672. /** Queue detached service work and retain it through service disposal. */
  673. private defer(task: () => Promise<void>): void {
  674. const run = Promise.resolve().then(async () => {
  675. if (!this.serviceActive()) return
  676. await task()
  677. })
  678. void this.track(run)
  679. }
  680. /** Retain one promise until settlement for service and optional provider teardown. */
  681. private track<T>(run: Promise<T>, registration?: ProviderRegistration): Promise<T> {
  682. this.inFlight.add(run)
  683. registration?.active.add(run)
  684. const settled = (): void => {
  685. this.inFlight.delete(run)
  686. registration?.active.delete(run)
  687. }
  688. void run.then(settled, settled)
  689. return run
  690. }
  691. /** Await every current and settling promise in one lifecycle registry. */
  692. private async drain(active: Set<Promise<unknown>>): Promise<void> {
  693. while (active.size > 0) await Promise.allSettled([...active])
  694. }
  695. /** Whether the owning plugin fiber can still start or commit title work. */
  696. private serviceActive(): boolean {
  697. return !this.lifetime.signal.aborted
  698. && this.ownerFiber.uid !== null
  699. && this.ownerFiber.state === FiberState.ACTIVE
  700. }
  701. /** Reject work once the owning plugin fiber has begun unloading. */
  702. private assertServiceActive(): void {
  703. if (!this.serviceActive()) throw new Error('session-title service disposed')
  704. }
  705. /** Reject malformed provider registrations before publishing an effect. */
  706. private validateProvider(provider: unknown): asserts provider is SessionTitleProvider {
  707. if (provider === null || typeof provider !== 'object') {
  708. throw new Error('session-title provider must be an object')
  709. }
  710. const candidate = provider as Record<string, unknown>
  711. if (typeof candidate.id !== 'string' || candidate.id.length === 0) {
  712. throw new Error('session-title provider id must be a non-empty string')
  713. }
  714. if (candidate.automatic !== 'first-prompt' && candidate.automatic !== 'all-prompts') {
  715. throw new Error('session-title provider automatic mode is invalid')
  716. }
  717. if (typeof candidate.generate !== 'function') {
  718. throw new Error(`session-title provider "${candidate.id}" requires generate()`)
  719. }
  720. }
  721. /**
  722. * Derive and append the deterministic fallback title over whatever stands
  723. * (the refresh unpin path: overwriting a pinned user title is the point).
  724. * Synchronous on purpose — no await may separate derivation from append, so
  725. * it needs neither ensureFallback's in-flight dedup nor its liveness
  726. * re-check. An underivable fallback (empty after the caps) appends nothing.
  727. */
  728. private appendFallback(session: Session, first: SessionTitleUserMessage): void {
  729. const title = fallbackSessionTitle(first.text, this.config.fallbackMaxWords, this.config.fallbackMaxBytes)
  730. if (title.length === 0) return
  731. session.append('session/title', {
  732. title,
  733. messageSeqs: [first.seq],
  734. source: { kind: 'fallback' },
  735. })
  736. }
  737. /** Create the first deterministic fallback if the session still lacks a title. */
  738. private async ensureFallback(session: Session): Promise<SessionTitleSnapshot | undefined> {
  739. this.assertServiceActive()
  740. const current = this.get(session)
  741. if (current !== undefined) return current
  742. const first = this.titleInputOf(session).first
  743. if (first === null) return undefined
  744. const title = fallbackSessionTitle(
  745. first.text,
  746. this.config.fallbackMaxWords,
  747. this.config.fallbackMaxBytes,
  748. )
  749. if (title.length === 0) return undefined
  750. const state = this.stateFor(session)
  751. if (state.fallback !== undefined) return state.fallback
  752. const fallback = Promise.resolve().then(() => {
  753. this.assertServiceActive()
  754. if (this.ctx.sessions.get(session.id) !== session) {
  755. throw new Error(`session "${session.id}" is not live in this store`)
  756. }
  757. const accepted = this.get(session)
  758. if (accepted !== undefined) return accepted
  759. session.append('session/title', {
  760. title,
  761. messageSeqs: [first.seq],
  762. source: { kind: 'fallback' },
  763. })
  764. return this.get(session)
  765. })
  766. state.fallback = fallback
  767. try {
  768. return await fallback
  769. } finally {
  770. delete state.fallback
  771. }
  772. }
  773. }
  774. export default SessionTitleService