( messages: ChatCompletionsMessage[], )
| 86 | } |
| 87 | |
| 88 | function convertMessages( |
| 89 | messages: ChatCompletionsMessage[], |
| 90 | ): unknown[] { |
| 91 | const input: unknown[] = [] |
| 92 | |
| 93 | for (const msg of messages) { |
| 94 | switch (msg.role) { |
| 95 | case 'system': { |
| 96 | // System messages are extracted to top-level `instructions` field; |
| 97 | // if any slip through, convert to developer role |
| 98 | if (msg.content) { |
| 99 | input.push({ type: 'message', role: 'developer', content: msg.content }) |
| 100 | } |
| 101 | break |
| 102 | } |
| 103 | |
| 104 | case 'user': { |
| 105 | const content = convertUserContentParts(msg.content) |
| 106 | if (content) { |
| 107 | input.push({ type: 'message', role: 'user', content }) |
| 108 | } |
| 109 | break |
| 110 | } |
| 111 | |
| 112 | case 'assistant': { |
| 113 | if (msg.content) { |
| 114 | input.push({ type: 'message', role: 'assistant', content: msg.content }) |
| 115 | } |
| 116 | if (msg.tool_calls) { |
| 117 | for (const tc of msg.tool_calls) { |
| 118 | input.push({ |
| 119 | type: 'function_call', |
| 120 | call_id: tc.id, |
| 121 | name: tc.function.name, |
| 122 | arguments: tc.function.arguments, |
| 123 | }) |
| 124 | } |
| 125 | } |
| 126 | break |
| 127 | } |
| 128 | |
| 129 | case 'tool': { |
| 130 | input.push({ |
| 131 | type: 'function_call_output', |
| 132 | call_id: msg.tool_call_id ?? 'unknown', |
| 133 | output: |
| 134 | typeof msg.content === 'string' |
| 135 | ? msg.content |
| 136 | : JSON.stringify(msg.content), |
| 137 | }) |
| 138 | break |
| 139 | } |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | return input |
| 144 | } |
| 145 |
no test coverage detected