request-header.ts 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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 { 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 and empty tool
  14. * list become absent fields, matching how requests are built. Logging, folding,
  15. * 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. const adapterDefaults = header.adapterDefaults
  21. return {
  22. config: header.config,
  23. ...adapterDefaults?.reasoningEffort === true || adapterDefaults?.maxTokens === true
  24. ? { adapterDefaults }
  25. : {},
  26. ...header.system !== undefined && header.system.length > 0 ? { system: header.system } : {},
  27. ...header.tools !== undefined && header.tools.length > 0 ? { tools: header.tools } : {},
  28. }
  29. }
  30. /** Canonical JSON equality for tool schemas assembled through the same path. */
  31. function sameSchema(a: ToolSchema, b: ToolSchema): boolean {
  32. return JSON.stringify(a) === JSON.stringify(b)
  33. }
  34. /**
  35. * Field-wise equality over canonical headers. Tool schemas compare in order.
  36. * @param a - one canonical header.
  37. * @param b - the other.
  38. * @returns whether config, system, and tools all match.
  39. */
  40. export function headerEquals(a: EpochHeader, b: EpochHeader): boolean {
  41. if (
  42. !callConfigEquals(a.config, b.config)
  43. || a.adapterDefaults?.reasoningEffort !== b.adapterDefaults?.reasoningEffort
  44. || a.adapterDefaults?.maxTokens !== b.adapterDefaults?.maxTokens
  45. || a.system !== b.system
  46. ) return false
  47. const at = a.tools ?? []
  48. const bt = b.tools ?? []
  49. return at.length === bt.length && at.every((tool, i) => sameSchema(tool, bt[i] as ToolSchema))
  50. }
  51. /**
  52. * Fold the header events of a log (or any prefix) into the
  53. * {@link EpochHeader} in force after the last snapshot. Non-header events are
  54. * skipped. This is the pure offline reconstruction path; the live session
  55. * tracks the same fold incrementally.
  56. * @param events - session events in log order.
  57. * @param from - a previously folded state to continue from.
  58. * @returns the latest canonical header, or undefined when none exists yet.
  59. */
  60. export function foldRequestHeader(events: readonly SessionEvent[], from?: EpochHeader): EpochHeader | undefined {
  61. let state = from
  62. for (const event of events) {
  63. if (event.type === 'request/header') state = canonicalHeader(event.data.header)
  64. }
  65. return state
  66. }