codec.spec.ts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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 { SessionId } from '@deepseek-ai/dsh-session'
  5. import { encodeSessionReferenceUri, formatSessionReferenceMention } from '@deepseek-ai/dsh-session-reference'
  6. import type { ContentBlock as AcpContentBlock } from '@agentclientprotocol/sdk'
  7. import {
  8. acpPromptToReferencedPrompt,
  9. acpPromptToText,
  10. harnessBlockToAcpContent,
  11. promptHasUnsupportedContent,
  12. turnEndToStopReason,
  13. } from '../src/codec.ts'
  14. describe('turnEndToStopReason', () => {
  15. // The SDK rejects an unknown stopReason, so this must be total over every
  16. // TurnEndReason kind and always produce a legal wire value.
  17. it('maps every known TurnEndReason kind to a legal StopReason', () => {
  18. expect(turnEndToStopReason({ kind: 'completed' })).toBe('end_turn')
  19. expect(turnEndToStopReason({ kind: 'max-tokens' })).toBe('max_tokens')
  20. expect(turnEndToStopReason({ kind: 'aborted' })).toBe('cancelled')
  21. expect(turnEndToStopReason({ kind: 'disposed' })).toBe('cancelled')
  22. expect(turnEndToStopReason({ kind: 'error', step: 1, message: 'boom' })).toBe('end_turn')
  23. })
  24. it('falls back to end_turn for an unknown (merge-extensible) future kind', () => {
  25. // A plugin-added TurnEndReason variant the bridge does not yet know about
  26. // must still produce a legal wire value, not throw into the SDK.
  27. const future = { kind: 'refusal' } as unknown as TurnEndReason
  28. expect(turnEndToStopReason(future)).toBe('end_turn')
  29. })
  30. })
  31. describe('harnessBlockToAcpContent', () => {
  32. it('maps a text block to ACP text content', () => {
  33. expect(harnessBlockToAcpContent({ type: 'text', text: 'hi' })).toEqual({ type: 'text', text: 'hi' })
  34. })
  35. it('returns undefined for non-text blocks (reasoning / plugin-added)', () => {
  36. expect(harnessBlockToAcpContent({ type: 'reasoning', text: 'think' })).toBeUndefined()
  37. expect(harnessBlockToAcpContent({ type: 'chart', data: 'x' } as unknown as ContentBlock)).toBeUndefined()
  38. })
  39. })
  40. describe('acpPromptToText', () => {
  41. it('concatenates text blocks and renders resource links explicitly', () => {
  42. const prompt: AcpContentBlock[] = [
  43. { type: 'text', text: 'hello ' },
  44. { type: 'resource_link', uri: 'file:///x', name: 'x' },
  45. { type: 'text', text: 'world' },
  46. ]
  47. expect(acpPromptToText(prompt)).toBe('hello \n[resource_link name="x" uri="file:///x"]\nworld')
  48. })
  49. it('returns empty string for a prompt with no text blocks', () => {
  50. expect(acpPromptToText([{ type: 'image', mimeType: 'image/png', data: 'AA==' }])).toBe('')
  51. })
  52. })
  53. describe('acpPromptToReferencedPrompt', () => {
  54. it('extracts resource links and inline mentions while preserving ordinary links', () => {
  55. const sessionId = SessionId('source/会话')
  56. const prompt: AcpContentBlock[] = [
  57. { type: 'text', text: `compare ${formatSessionReferenceMention({ sessionId, label: 'inline' })} with ` },
  58. { type: 'resource_link', uri: encodeSessionReferenceUri(sessionId), name: 'linked' },
  59. { type: 'resource_link', uri: 'file:///x', name: 'x' },
  60. ]
  61. expect(acpPromptToReferencedPrompt(prompt)).toEqual({
  62. text: 'compare @inline with @linked\n[resource_link name="x" uri="file:///x"]\n',
  63. references: [{ sessionId, label: 'inline' }, { sessionId, label: 'linked' }],
  64. })
  65. })
  66. it('rejects malformed session resource links', () => {
  67. expect(() => acpPromptToReferencedPrompt([
  68. { type: 'resource_link', uri: 'dsh-session:%%%', name: 'bad' },
  69. ])).toThrow(/invalid session reference URI/)
  70. })
  71. it('uses the decoded id for an empty resource name and ignores unsupported direct inputs', () => {
  72. const sessionId = SessionId('source')
  73. expect(acpPromptToReferencedPrompt([
  74. { type: 'resource_link', uri: encodeSessionReferenceUri(sessionId), name: '' },
  75. { type: 'image', mimeType: 'image/png', data: 'AA==' },
  76. ])).toEqual({ text: '@source', references: [{ sessionId, label: 'source' }] })
  77. })
  78. })
  79. describe('promptHasUnsupportedContent', () => {
  80. it('detects image, audio, and embedded resource blocks', () => {
  81. expect(promptHasUnsupportedContent([{ type: 'image', mimeType: 'image/png', data: 'AA==' }])).toBe(true)
  82. expect(promptHasUnsupportedContent([{ type: 'audio', mimeType: 'audio/wav', data: 'AA==' }])).toBe(true)
  83. expect(promptHasUnsupportedContent([{ type: 'resource', resource: { uri: 'file:///x', text: 'x' } }])).toBe(true)
  84. })
  85. it('passes baseline text and resource_link prompt blocks', () => {
  86. expect(promptHasUnsupportedContent([{ type: 'text', text: 'hi' }])).toBe(false)
  87. expect(promptHasUnsupportedContent([{ type: 'resource_link', uri: 'file:///x', name: 'x' }])).toBe(false)
  88. })
  89. })