codec.spec.ts 3.2 KB

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