|
1 | 1 | const { expect } = require('chai'); |
2 | 2 | const Messages = require('./Messages.js'); |
| 3 | +const OpenAIUtil = require('./OpenAIUtil.js'); |
3 | 4 |
|
4 | 5 | describe('Messages', () => { |
5 | 6 | describe('normalize_single_message', () => { |
@@ -70,4 +71,114 @@ describe('Messages', () => { |
70 | 71 | }); |
71 | 72 | } |
72 | 73 | }); |
| 74 | + describe('normalize OpenAI tool calls', () => { |
| 75 | + const cases = [ |
| 76 | + { |
| 77 | + name: 'string message', |
| 78 | + input: { |
| 79 | + role: 'assistant', |
| 80 | + tool_calls: [ |
| 81 | + { |
| 82 | + id: 'tool-1', |
| 83 | + type: 'function', |
| 84 | + function: { |
| 85 | + name: 'tool-1-function', |
| 86 | + arguments: {}, |
| 87 | + } |
| 88 | + } |
| 89 | + ] |
| 90 | + }, |
| 91 | + output: { |
| 92 | + role: 'assistant', |
| 93 | + content: [ |
| 94 | + { |
| 95 | + type: 'tool_use', |
| 96 | + id: 'tool-1', |
| 97 | + name: 'tool-1-function', |
| 98 | + input: {}, |
| 99 | + } |
| 100 | + ] |
| 101 | + } |
| 102 | + } |
| 103 | + ]; |
| 104 | + for ( const tc of cases ) { |
| 105 | + it(`should normalize ${tc.name}`, () => { |
| 106 | + const output = Messages.normalize_single_message(tc.input); |
| 107 | + expect(output).to.deep.equal(tc.output); |
| 108 | + }); |
| 109 | + } |
| 110 | + }); |
| 111 | + describe('normalize Claude tool calls', () => { |
| 112 | + const cases = [ |
| 113 | + { |
| 114 | + name: 'string message', |
| 115 | + input: { |
| 116 | + role: 'assistant', |
| 117 | + content: [ |
| 118 | + { |
| 119 | + type: 'tool_use', |
| 120 | + id: 'tool-1', |
| 121 | + name: 'tool-1-function', |
| 122 | + input: "{}", |
| 123 | + } |
| 124 | + ] |
| 125 | + }, |
| 126 | + output: { |
| 127 | + role: 'assistant', |
| 128 | + content: [ |
| 129 | + { |
| 130 | + type: 'tool_use', |
| 131 | + id: 'tool-1', |
| 132 | + name: 'tool-1-function', |
| 133 | + input: "{}", |
| 134 | + } |
| 135 | + ] |
| 136 | + } |
| 137 | + } |
| 138 | + ]; |
| 139 | + for ( const tc of cases ) { |
| 140 | + it(`should normalize ${tc.name}`, () => { |
| 141 | + const output = Messages.normalize_single_message(tc.input); |
| 142 | + expect(output).to.deep.equal(tc.output); |
| 143 | + }); |
| 144 | + } |
| 145 | + }); |
| 146 | + describe('OpenAI-ify normalized tool calls', () => { |
| 147 | + const cases = [ |
| 148 | + { |
| 149 | + name: 'string message', |
| 150 | + input: [{ |
| 151 | + role: 'assistant', |
| 152 | + content: [ |
| 153 | + { |
| 154 | + type: 'tool_use', |
| 155 | + id: 'tool-1', |
| 156 | + name: 'tool-1-function', |
| 157 | + input: {}, |
| 158 | + } |
| 159 | + ] |
| 160 | + }], |
| 161 | + output: [{ |
| 162 | + role: 'assistant', |
| 163 | + content: null, |
| 164 | + tool_calls: [ |
| 165 | + { |
| 166 | + id: 'tool-1', |
| 167 | + type: 'function', |
| 168 | + function: { |
| 169 | + name: 'tool-1-function', |
| 170 | + arguments: '{}', |
| 171 | + } |
| 172 | + } |
| 173 | + ] |
| 174 | + }] |
| 175 | + } |
| 176 | + ]; |
| 177 | + for ( const tc of cases ) { |
| 178 | + it(`should normalize ${tc.name}`, async () => { |
| 179 | + const output = await OpenAIUtil.process_input_messages(tc.input); |
| 180 | + expect(output).to.deep.equal(tc.output); |
| 181 | + }); |
| 182 | + } |
| 183 | + }); |
73 | 184 | }); |
0 commit comments