mock-adapter.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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, or a 'hang' marker that
  58. * streams one chunk then waits until aborted.
  59. */
  60. export class MockAdapter extends LlmAdapter {
  61. requests: GenerateOptions[] = []
  62. constructor(
  63. private script: (StreamChunk[] | ((options: GenerateOptions) => StreamChunk[]) | 'hang')[],
  64. private readonly reasoning?: LlmModelReasoningInfo,
  65. ) {
  66. super()
  67. }
  68. override resolveModel(
  69. provider: string,
  70. model: string,
  71. ): Promise<LlmResolvedModelInfo> {
  72. return Promise.resolve({
  73. provider,
  74. id: model,
  75. name: model,
  76. ...this.reasoning === undefined ? {} : { reasoning: this.reasoning },
  77. })
  78. }
  79. async * stream(options: GenerateOptions): AsyncIterable<StreamChunk> {
  80. this.requests.push(options)
  81. const entry = this.script.shift()
  82. if (!entry) throw new Error('MockAdapter: script exhausted')
  83. if (entry === 'hang') {
  84. yield { type: 'block-start', index: 0, blockType: 'text' }
  85. yield { type: 'text-delta', index: 0, text: 'partial' }
  86. await new Promise<void>((_resolve, reject) => {
  87. if (options.signal?.aborted) { reject(new Error('aborted')); return }
  88. options.signal?.addEventListener('abort', () => { reject(new Error('aborted')) }, { once: true })
  89. })
  90. return
  91. }
  92. const chunks = typeof entry === 'function' ? entry(options) : entry
  93. for (const chunk of chunks) {
  94. if (options.signal?.aborted) throw new Error('aborted')
  95. yield chunk
  96. }
  97. }
  98. }