1
0

history.client.ts 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. /** Pure history responses shared by the local Remote fake and assembled Session tests. */
  2. import { SESSION_FORMAT_VERSION } from '@deepseek-ai/dsh-session/types'
  3. import type {
  4. SessionAssistantStreamBaseline, SessionFollowFrame, SessionFollowRequest, SessionPage, SessionProjectionBaseline,
  5. } from '../../src/types.ts'
  6. import { historyRecordLastSeq } from '../../src/client/sessions/history-records.ts'
  7. /**
  8. * Cut a history page at the Host cursor, preserving its other fields.
  9. * @param page - scripted history page.
  10. * @param throughSeq - inclusive final sequence.
  11. * @returns the page without records beyond the cursor.
  12. */
  13. export function pageThrough(page: SessionPage, throughSeq: number): SessionPage {
  14. return { ...page, records: page.records.filter(record => historyRecordLastSeq(record) <= throughSeq) }
  15. }
  16. /**
  17. * Build the opening follow frame for a Session or addressed child.
  18. * @param page - history and optional projection baseline.
  19. * @param request - addressed follow request.
  20. * @param cursor - opening cursor; defaults to the history tail, or -1 when empty.
  21. * @param assistantStream - baseline included only when the request opts in.
  22. * @returns the opening frame.
  23. */
  24. export function followSnapshot(
  25. page: SessionPage & { readonly projections?: SessionProjectionBaseline },
  26. request: SessionFollowRequest,
  27. cursor = page.records.length === 0 ? -1 : historyRecordLastSeq(page.records.at(-1)!),
  28. assistantStream: SessionAssistantStreamBaseline = { revision: 0 },
  29. ): Extract<SessionFollowFrame, { type: 'snapshot' }> {
  30. const { address } = request
  31. return {
  32. type: 'snapshot',
  33. header: {
  34. version: SESSION_FORMAT_VERSION,
  35. id: address.kind === 'session' ? address.sessionId : address.childSessionId,
  36. createdAt: 0,
  37. isSeeded: false,
  38. ...(address.kind === 'subagent' ? { origin: 'subagent', parentSession: address.parentSessionId } : {}),
  39. },
  40. cursor,
  41. records: pageThrough(page, cursor).records,
  42. hasMore: page.hasMore,
  43. projections: page.projections ?? { asOfSeq: cursor, values: {} },
  44. ...(request.assistantStream === true ? { assistantStream } : {}),
  45. }
  46. }