mock-adapter.ts 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. import type { GenerateOptions, LlmModelReasoningInfo, LlmResolvedModelInfo, StreamChunk } from '@deepseek-ai/dsh-llm'
  2. import { CallId, LlmAdapter } from '@deepseek-ai/dsh-llm'
  3. /** Helpers to write scripted responses tersely. */
  4. export function textResponse(text: string): StreamChunk[] {
  5. return [
  6. { type: 'block-start', index: 0, blockType: 'text' },
  7. ...Array.from(text, (char): StreamChunk => ({ type: 'text-delta', index: 0, text: char })),
  8. { type: 'block-end', index: 0, block: { type: 'text', text } },
  9. { type: 'usage', usage: { inputTokens: 10, outputTokens: text.length } },
  10. { type: 'finish', reason: { kind: 'stop' } },
  11. ]
  12. }
  13. /**
  14. * Like {@link textResponse} but the stream ends with a `max-tokens` finish —
  15. * the model was cut off at the output-token ceiling (DeepSeek's `length`).
  16. * Used to exercise the turn-end `max-tokens` surfacing rule.
  17. */
  18. export function maxTokensResponse(text: string): StreamChunk[] {
  19. return [
  20. { type: 'block-start', index: 0, blockType: 'text' },
  21. ...Array.from(text, (char): StreamChunk => ({ type: 'text-delta', index: 0, text: char })),
  22. { type: 'block-end', index: 0, block: { type: 'text', text } },
  23. { type: 'usage', usage: { inputTokens: 10, outputTokens: text.length } },
  24. { type: 'finish', reason: { kind: 'max-tokens' } },
  25. ]
  26. }
  27. export function toolCallResponse(rawCallId: string, name: string, args: object, text?: string): StreamChunk[] {
  28. const callId = CallId(rawCallId)
  29. const argumentsJson = JSON.stringify(args)
  30. const chunks: StreamChunk[] = []
  31. let index = 0
  32. if (text) {
  33. chunks.push(
  34. { type: 'block-start', index, blockType: 'text' },
  35. { type: 'text-delta', index, text },
  36. { type: 'block-end', index, block: { type: 'text', text } },
  37. )
  38. index += 1
  39. }
  40. chunks.push(
  41. { type: 'block-start', index, blockType: 'tool-call' },
  42. { type: 'tool-call-delta', index, id: callId, name, argumentsDelta: argumentsJson.slice(0, 5) },
  43. { type: 'tool-call-delta', index, id: callId, argumentsDelta: argumentsJson.slice(5) },
  44. {
  45. type: 'block-end',
  46. index,
  47. block: { type: 'tool-call', id: callId, name, arguments: argumentsJson },
  48. },
  49. { type: 'usage', usage: { inputTokens: 10, outputTokens: 5 } },
  50. { type: 'finish', reason: { kind: 'tool-calls' } },
  51. )
  52. return chunks
  53. }
  54. /**
  55. * Mock adapter driven by a script: each model call consumes the next entry.
  56. * Records every request it receives for assertions. An entry may be a
  57. * function to compute chunks from the request, a 'hang' marker that
  58. * streams one chunk then waits until aborted, or 'hang-slow' which takes
  59. * 50ms to notice the abort — a stand-in for slow real-world teardown
  60. * (LLM stream cancellation, tool unwinding).
  61. */
  62. export class MockAdapter extends LlmAdapter {
  63. requests: GenerateOptions[] = []
  64. constructor(
  65. private script: (StreamChunk[] | ((options: GenerateOptions) => StreamChunk[]) | 'hang' | 'hang-slow')[],
  66. private readonly reasoning?: LlmModelReasoningInfo,
  67. private readonly defaultMaxTokens?: number,
  68. ) {
  69. super()
  70. }
  71. override resolveModel(
  72. provider: string,
  73. model: string,
  74. ): Promise<LlmResolvedModelInfo> {
  75. return Promise.resolve({
  76. provider,
  77. id: model,
  78. name: model,
  79. ...this.reasoning === undefined ? {} : { reasoning: this.reasoning },
  80. ...this.defaultMaxTokens === undefined ? {} : { defaultMaxTokens: this.defaultMaxTokens },
  81. })
  82. }
  83. async * stream(options: GenerateOptions): AsyncIterable<StreamChunk> {
  84. this.requests.push(options)
  85. const entry = this.script.shift()
  86. if (!entry) throw new Error('MockAdapter: script exhausted')
  87. if (entry === 'hang') {
  88. yield { type: 'block-start', index: 0, blockType: 'text' }
  89. yield { type: 'text-delta', index: 0, text: 'partial' }
  90. await new Promise<void>((_resolve, reject) => {
  91. if (options.signal?.aborted) { reject(new Error('aborted')); return }
  92. options.signal?.addEventListener('abort', () => { reject(new Error('aborted')) }, { once: true })
  93. })
  94. return
  95. }
  96. if (entry === 'hang-slow') {
  97. yield { type: 'block-start', index: 0, blockType: 'text' }
  98. yield { type: 'text-delta', index: 0, text: 'partial' }
  99. await new Promise<void>((_resolve, reject) => {
  100. const fail = (): void => { reject(new Error('aborted')) }
  101. if (options.signal?.aborted) { setTimeout(fail, 50); return }
  102. options.signal?.addEventListener('abort', () => { setTimeout(fail, 50) }, { once: true })
  103. })
  104. return
  105. }
  106. const chunks = typeof entry === 'function' ? entry(options) : entry
  107. for (const chunk of chunks) {
  108. if (options.signal?.aborted) throw new Error('aborted')
  109. yield chunk
  110. }
  111. }
  112. }