reconnect.worker.client.ts 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. /** Compiled production Client fold for a reconnect during a long Assistant attempt. */
  2. import { performance } from 'node:perf_hooks'
  3. import { AssistantStreamAccumulator } from '@deepseek-ai/dsh-llm/assistant-stream'
  4. import { LlmAttemptId } from '@deepseek-ai/dsh-llm/brand'
  5. import type { SessionAssistantStreamBaseline } from '@deepseek-ai/dsh-api-session-controller/types'
  6. // The Client implementation has no plain-Node export; only this adapter is bundled.
  7. import { ClientAssistantStream } from '../../packages/api/session-controller/src/client/sessions/assistant-stream.ts'
  8. import { assertBuiltBenchmarkRuntime } from '../support/built-worker.ts'
  9. /** Measurements of replace() only; fixture construction and forced GC are excluded. */
  10. export interface ReconnectReport {
  11. readonly deltas: number
  12. readonly records: number
  13. readonly entries: number
  14. readonly replaceMs: number
  15. readonly retainedMb: number
  16. readonly nextFrame: string | undefined
  17. }
  18. assertBuiltBenchmarkRuntime(import.meta.url, {
  19. '@deepseek-ai/dsh-llm/assistant-stream': import.meta.resolve('@deepseek-ai/dsh-llm/assistant-stream'),
  20. })
  21. const deltas = 100000
  22. const accumulator = new AssistantStreamAccumulator()
  23. accumulator.push({ time: 1700000000000, chunk: { type: 'block-start', index: 0, blockType: 'reasoning' } })
  24. for (let index = 0; index < deltas; index++) {
  25. accumulator.push({ time: 1700000000001 + index, chunk: { type: 'reasoning-delta', index: 0, text: 'token ' } })
  26. }
  27. const attemptId = LlmAttemptId('synthetic-reconnect')
  28. const nextIndex = deltas + 1
  29. const baseline: SessionAssistantStreamBaseline = {
  30. revision: nextIndex + 1,
  31. activeAttempt: {
  32. attemptId, startedAfterSeq: -1, turn: 1, step: 1, nextIndex,
  33. stream: JSON.parse(JSON.stringify(accumulator.snapshot())) as NonNullable<SessionAssistantStreamBaseline['activeAttempt']>['stream'],
  34. },
  35. }
  36. if (globalThis.gc === undefined) throw new Error('reconnect benchmark requires --expose-gc')
  37. globalThis.gc()
  38. const before = process.memoryUsage().heapUsed
  39. const client = new ClientAssistantStream()
  40. const start = performance.now()
  41. const visible = client.replace([], baseline)
  42. const replaceMs = performance.now() - start
  43. globalThis.gc()
  44. const retainedMb = (process.memoryUsage().heapUsed - before) / 1048576
  45. const next = client.acceptFrame({ type: 'chunk', attemptId, revision: nextIndex + 2, index: nextIndex, time: 1700000000001 + deltas, chunk: { type: 'reasoning-delta', index: 0, text: 'suffix' } })
  46. const report: ReconnectReport = { deltas, records: baseline.activeAttempt!.stream.length, entries: visible.length, replaceMs, retainedMb, nextFrame: next?.type }
  47. process.stdout.write(JSON.stringify(report) + '\n')