codec.spec.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. import { describe, expect, it } from 'vitest'
  2. import type { ContentBlock } from '@deepseek-ai/dsh-llm'
  3. import type { TurnEndReason } from '@deepseek-ai/dsh-session'
  4. import type { ContentBlock as AcpContentBlock } from '@agentclientprotocol/sdk'
  5. import {
  6. acpPromptToText,
  7. harnessBlockToAcpContent,
  8. promptHasUnsupportedContent,
  9. turnEndToStopReason,
  10. } from '../src/codec.ts'
  11. describe('turnEndToStopReason', () => {
  12. // The SDK rejects an unknown stopReason, so this must be total over every
  13. // TurnEndReason kind and always produce a legal wire value.
  14. it('maps every known TurnEndReason kind to a legal StopReason', () => {
  15. expect(turnEndToStopReason({ kind: 'completed' })).toBe('end_turn')
  16. expect(turnEndToStopReason({ kind: 'max-tokens' })).toBe('max_tokens')
  17. expect(turnEndToStopReason({ kind: 'aborted', reason: 'x' })).toBe('cancelled')
  18. expect(turnEndToStopReason({ kind: 'disposed' })).toBe('cancelled')
  19. expect(turnEndToStopReason({ kind: 'rejected', reason: 'blocked by hook' })).toBe('cancelled')
  20. expect(turnEndToStopReason({ kind: 'error', step: 1, message: 'boom' })).toBe('end_turn')
  21. })
  22. it('falls back to end_turn for an unknown (merge-extensible) future kind', () => {
  23. // A plugin-added TurnEndReason variant the bridge does not yet know about
  24. // must still produce a legal wire value, not throw into the SDK.
  25. const future = { kind: 'refusal' } as unknown as TurnEndReason
  26. expect(turnEndToStopReason(future)).toBe('end_turn')
  27. })
  28. })
  29. describe('harnessBlockToAcpContent', () => {
  30. it('maps a text block to ACP text content', () => {
  31. expect(harnessBlockToAcpContent({ type: 'text', text: 'hi' })).toEqual({ type: 'text', text: 'hi' })
  32. })
  33. it('returns undefined for non-text blocks (reasoning / plugin-added)', () => {
  34. expect(harnessBlockToAcpContent({ type: 'reasoning', text: 'think' })).toBeUndefined()
  35. expect(harnessBlockToAcpContent({ type: 'chart', data: 'x' } as unknown as ContentBlock)).toBeUndefined()
  36. })
  37. })
  38. describe('acpPromptToText', () => {
  39. it('concatenates text blocks and renders resource links explicitly', () => {
  40. const prompt: AcpContentBlock[] = [
  41. { type: 'text', text: 'hello ' },
  42. { type: 'resource_link', uri: 'file:///x', name: 'x' },
  43. { type: 'text', text: 'world' },
  44. ]
  45. expect(acpPromptToText(prompt)).toBe('hello \n[resource_link name="x" uri="file:///x"]\nworld')
  46. })
  47. it('returns empty string for a prompt with no text blocks', () => {
  48. expect(acpPromptToText([{ type: 'image', mimeType: 'image/png', data: 'AA==' }])).toBe('')
  49. })
  50. })
  51. describe('promptHasUnsupportedContent', () => {
  52. it('detects image, audio, and embedded resource blocks', () => {
  53. expect(promptHasUnsupportedContent([{ type: 'image', mimeType: 'image/png', data: 'AA==' }])).toBe(true)
  54. expect(promptHasUnsupportedContent([{ type: 'audio', mimeType: 'audio/wav', data: 'AA==' }])).toBe(true)
  55. expect(promptHasUnsupportedContent([{ type: 'resource', resource: { uri: 'file:///x', text: 'x' } }])).toBe(true)
  56. })
  57. it('passes baseline text and resource_link prompt blocks', () => {
  58. expect(promptHasUnsupportedContent([{ type: 'text', text: 'hi' }])).toBe(false)
  59. expect(promptHasUnsupportedContent([{ type: 'resource_link', uri: 'file:///x', name: 'x' }])).toBe(false)
  60. })
  61. })