request-header.ts 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /**
  2. * Request-header reconstruction utilities over full `request/header` session
  3. * events. Anyone holding a session log reconstructs the {@link EpochHeader}
  4. * any request was built under by taking the latest canonical snapshot; the
  5. * loop uses the same equality helper to avoid logging unchanged headers.
  6. *
  7. * @module dsh-session/request-header
  8. */
  9. import { callConfigEquals } from '@deepseek-ai/dsh-llm'
  10. import type { Message, ToolSchema } from '@deepseek-ai/dsh-llm'
  11. import type { EpochHeader, SessionEvent } from './types.ts'
  12. /**
  13. * Normalize a header to canonical form: an empty system prompt, an empty tool
  14. * list, and an empty session prefix become absent fields, matching how requests
  15. * are built. Logging, folding, and comparison use this one representation.
  16. * @param header - the header to normalize (not mutated).
  17. * @returns the canonical header.
  18. */
  19. export function canonicalHeader(header: EpochHeader): EpochHeader {
  20. return {
  21. config: header.config,
  22. ...header.system !== undefined && header.system.length > 0 ? { system: header.system } : {},
  23. ...header.tools !== undefined && header.tools.length > 0 ? { tools: header.tools } : {},
  24. ...header.messagePrefix !== undefined && header.messagePrefix.length > 0 ? { messagePrefix: header.messagePrefix } : {},
  25. }
  26. }
  27. /** Canonical JSON equality for tool schemas assembled through the same path. */
  28. function sameSchema(a: ToolSchema, b: ToolSchema): boolean {
  29. return JSON.stringify(a) === JSON.stringify(b)
  30. }
  31. /** Canonical JSON equality over session-prefix arrays; absence equals empty. */
  32. function sameMessages(a: readonly Message[] | undefined, b: readonly Message[] | undefined): boolean {
  33. return JSON.stringify(a ?? []) === JSON.stringify(b ?? [])
  34. }
  35. /**
  36. * Field-wise equality over canonical headers. Tool schemas compare in order;
  37. * the session prefix compares as canonical JSON.
  38. * @param a - one canonical header.
  39. * @param b - the other.
  40. * @returns whether config, system, tools, and session prefix all match.
  41. */
  42. export function headerEquals(a: EpochHeader, b: EpochHeader): boolean {
  43. if (!callConfigEquals(a.config, b.config) || a.system !== b.system) return false
  44. if (!sameMessages(a.messagePrefix, b.messagePrefix)) return false
  45. const at = a.tools ?? []
  46. const bt = b.tools ?? []
  47. return at.length === bt.length && at.every((tool, i) => sameSchema(tool, bt[i] as ToolSchema))
  48. }
  49. /**
  50. * Fold the header events of a log (or any prefix) into the
  51. * {@link EpochHeader} in force after the last snapshot. Non-header events are
  52. * skipped. This is the pure offline reconstruction path; the live session
  53. * tracks the same fold incrementally.
  54. * @param events - session events in log order.
  55. * @param from - a previously folded state to continue from.
  56. * @returns the latest canonical header, or undefined when none exists yet.
  57. */
  58. export function foldRequestHeader(events: readonly SessionEvent[], from?: EpochHeader): EpochHeader | undefined {
  59. let state = from
  60. for (const event of events) {
  61. if (event.type === 'request/header') state = canonicalHeader(event.data.header)
  62. }
  63. return state
  64. }