|
| 1 | +import { |
| 2 | + MessagesResponse, |
| 3 | + TextBlock, |
| 4 | + TextCitation, |
| 5 | + ThinkingBlock, |
| 6 | + ToolUseBlock, |
| 7 | +} from '../../../types/messagesResponse'; |
| 8 | + |
| 9 | +const getMessageStartEvent = (response: MessagesResponse): string => { |
| 10 | + const message = { ...response, content: [], type: 'message_start' }; |
| 11 | + return `event: message_start\ndata: ${JSON.stringify({ |
| 12 | + type: 'message_start', |
| 13 | + message, |
| 14 | + })}\n\n`; |
| 15 | +}; |
| 16 | + |
| 17 | +const getMessageDeltaEvent = (response: MessagesResponse): string => { |
| 18 | + const messageDeltaEvent = { |
| 19 | + type: 'message_delta', |
| 20 | + delta: { |
| 21 | + stop_reason: response.stop_reason, |
| 22 | + stop_sequence: response.stop_sequence, |
| 23 | + }, |
| 24 | + usage: response.usage, |
| 25 | + }; |
| 26 | + return `event: message_delta\ndata: ${JSON.stringify(messageDeltaEvent)}\n\n`; |
| 27 | +}; |
| 28 | + |
| 29 | +const MESSAGE_STOP_EVENT = `event: message_stop\ndata: {type: 'message_stop'}\n\n`; |
| 30 | + |
| 31 | +const textContentBlockStartEvent = (index: number): string => { |
| 32 | + return `event: content_block_start\ndata: ${JSON.stringify({ |
| 33 | + type: 'content_block_start', |
| 34 | + index, |
| 35 | + content_block: { |
| 36 | + type: 'text', |
| 37 | + text: '', |
| 38 | + }, |
| 39 | + })}\n\n`; |
| 40 | +}; |
| 41 | + |
| 42 | +const textContentBlockDeltaEvent = ( |
| 43 | + index: number, |
| 44 | + textBlock: TextBlock |
| 45 | +): string => { |
| 46 | + return `event: content_block_delta\ndata: ${JSON.stringify({ |
| 47 | + type: 'content_block_delta', |
| 48 | + index, |
| 49 | + delta: { |
| 50 | + type: 'text_delta', |
| 51 | + text: textBlock.text, |
| 52 | + }, |
| 53 | + })}\n\n`; |
| 54 | +}; |
| 55 | + |
| 56 | +const toolUseContentBlockStartEvent = ( |
| 57 | + index: number, |
| 58 | + toolUseBlock: ToolUseBlock |
| 59 | +): string => { |
| 60 | + return `event: content_block_start\ndata: ${JSON.stringify({ |
| 61 | + type: 'content_block_start', |
| 62 | + index, |
| 63 | + content_block: { |
| 64 | + type: 'tool_use', |
| 65 | + tool_use: { ...toolUseBlock, input: {} }, |
| 66 | + }, |
| 67 | + })}\n\n`; |
| 68 | +}; |
| 69 | + |
| 70 | +const toolUseContentBlockDeltaEvent = ( |
| 71 | + index: number, |
| 72 | + toolUseBlock: ToolUseBlock |
| 73 | +): string => { |
| 74 | + return `event: content_block_delta\ndata: ${JSON.stringify({ |
| 75 | + type: 'content_block_delta', |
| 76 | + index, |
| 77 | + delta: { |
| 78 | + type: 'input_json_delta', |
| 79 | + partial_json: JSON.stringify(toolUseBlock.input), |
| 80 | + }, |
| 81 | + })}\n\n`; |
| 82 | +}; |
| 83 | + |
| 84 | +const thinkingContentBlockStartEvent = (index: number): string => { |
| 85 | + return `event: content_block_start\ndata: ${JSON.stringify({ |
| 86 | + type: 'content_block_start', |
| 87 | + index, |
| 88 | + content_block: { |
| 89 | + type: 'thinking', |
| 90 | + thinking: '', |
| 91 | + signature: '', |
| 92 | + }, |
| 93 | + })}\n\n`; |
| 94 | +}; |
| 95 | + |
| 96 | +const thinkingContentBlockDeltaEvent = ( |
| 97 | + index: number, |
| 98 | + thinkingBlock: ThinkingBlock |
| 99 | +): string => { |
| 100 | + return `event: content_block_delta\ndata: ${JSON.stringify({ |
| 101 | + type: 'content_block_delta', |
| 102 | + index, |
| 103 | + delta: { |
| 104 | + type: 'thinking_delta', |
| 105 | + thinking: thinkingBlock.thinking, |
| 106 | + }, |
| 107 | + })}\n\n`; |
| 108 | +}; |
| 109 | + |
| 110 | +const signatureContentBlockDeltaEvent = ( |
| 111 | + index: number, |
| 112 | + thinkingBlock: ThinkingBlock |
| 113 | +): string => { |
| 114 | + return `event: content_block_delta\ndata: ${JSON.stringify({ |
| 115 | + type: 'content_block_delta', |
| 116 | + index, |
| 117 | + delta: { |
| 118 | + type: 'signature_delta', |
| 119 | + signature: thinkingBlock.signature, |
| 120 | + }, |
| 121 | + })}\n\n`; |
| 122 | +}; |
| 123 | + |
| 124 | +const citationContentBlockDeltaEvent = ( |
| 125 | + index: number, |
| 126 | + citation: TextCitation |
| 127 | +): string => { |
| 128 | + return `event: content_block_delta\ndata: ${JSON.stringify({ |
| 129 | + type: 'content_block_delta', |
| 130 | + index, |
| 131 | + delta: { |
| 132 | + type: 'citations_delta', |
| 133 | + citation, |
| 134 | + }, |
| 135 | + })}\n\n`; |
| 136 | +}; |
| 137 | + |
| 138 | +const contentBlockStopEvent = (index: number): string => { |
| 139 | + return `event: content_block_stop\ndata: ${JSON.stringify({ |
| 140 | + type: 'content_block_stop', |
| 141 | + index, |
| 142 | + })}\n\n`; |
| 143 | +}; |
| 144 | + |
| 145 | +export function* anthropicMessagesJsonToStreamGenerator( |
| 146 | + response: MessagesResponse |
| 147 | +): Generator<string, void, unknown> { |
| 148 | + yield getMessageStartEvent(response); |
| 149 | + |
| 150 | + for (const [index, contentBlock] of response.content.entries()) { |
| 151 | + switch (contentBlock.type) { |
| 152 | + case 'text': |
| 153 | + yield textContentBlockStartEvent(index); |
| 154 | + yield textContentBlockDeltaEvent(index, contentBlock); |
| 155 | + if (contentBlock.citations) { |
| 156 | + for (const citation of contentBlock.citations) { |
| 157 | + yield citationContentBlockDeltaEvent(index, citation); |
| 158 | + } |
| 159 | + } |
| 160 | + break; |
| 161 | + case 'tool_use': |
| 162 | + yield toolUseContentBlockStartEvent(index, contentBlock); |
| 163 | + yield toolUseContentBlockDeltaEvent(index, contentBlock); |
| 164 | + break; |
| 165 | + case 'thinking': |
| 166 | + yield thinkingContentBlockStartEvent(index); |
| 167 | + yield thinkingContentBlockDeltaEvent(index, contentBlock); |
| 168 | + yield signatureContentBlockDeltaEvent(index, contentBlock); |
| 169 | + break; |
| 170 | + } |
| 171 | + yield contentBlockStopEvent(index); |
| 172 | + } |
| 173 | + |
| 174 | + yield getMessageDeltaEvent(response); |
| 175 | + |
| 176 | + yield MESSAGE_STOP_EVENT; |
| 177 | +} |
| 178 | +``; |
0 commit comments