1
0

mock-adapter.ts 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import type { GenerateOptions, 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. export function toolCallResponse(rawCallId: string, name: string, args: object, text?: string): StreamChunk[] {
  14. const callId = CallId(rawCallId)
  15. const argumentsJson = JSON.stringify(args)
  16. const chunks: StreamChunk[] = []
  17. let index = 0
  18. if (text) {
  19. chunks.push(
  20. { type: 'block-start', index, blockType: 'text' },
  21. { type: 'text-delta', index, text },
  22. { type: 'block-end', index, block: { type: 'text', text } },
  23. )
  24. index += 1
  25. }
  26. chunks.push(
  27. { type: 'block-start', index, blockType: 'tool-call' },
  28. { type: 'tool-call-delta', index, id: callId, name, argumentsDelta: argumentsJson.slice(0, 5) },
  29. { type: 'tool-call-delta', index, id: callId, argumentsDelta: argumentsJson.slice(5) },
  30. {
  31. type: 'block-end',
  32. index,
  33. block: { type: 'tool-call', id: callId, name, arguments: argumentsJson },
  34. },
  35. { type: 'usage', usage: { inputTokens: 10, outputTokens: 5 } },
  36. { type: 'finish', reason: { kind: 'tool-calls' } },
  37. )
  38. return chunks
  39. }
  40. /**
  41. * Mock adapter driven by a script: each model call consumes the next entry.
  42. * Records every request it receives for assertions. An entry may be a
  43. * function to compute chunks from the request, or a 'hang' marker that
  44. * streams one chunk then waits until aborted.
  45. */
  46. export class MockAdapter extends LlmAdapter {
  47. requests: GenerateOptions[] = []
  48. constructor(private script: (StreamChunk[] | ((options: GenerateOptions) => StreamChunk[]) | 'hang')[]) {
  49. super()
  50. }
  51. async * stream(options: GenerateOptions): AsyncIterable<StreamChunk> {
  52. this.requests.push(options)
  53. const entry = this.script.shift()
  54. if (!entry) throw new Error('MockAdapter: script exhausted')
  55. if (entry === 'hang') {
  56. yield { type: 'block-start', index: 0, blockType: 'text' }
  57. yield { type: 'text-delta', index: 0, text: 'partial' }
  58. await new Promise<void>((_resolve, reject) => {
  59. if (options.signal?.aborted) { reject(new Error('aborted')); return }
  60. options.signal?.addEventListener('abort', () => { reject(new Error('aborted')) }, { once: true })
  61. })
  62. return
  63. }
  64. const chunks = typeof entry === 'function' ? entry(options) : entry
  65. for (const chunk of chunks) {
  66. if (options.signal?.aborted) throw new Error('aborted')
  67. yield chunk
  68. }
  69. }
  70. }