|
| 1 | +import { CoreAssistantMessage, CoreToolMessage } from '../prompt'; |
1 | 2 | import { CallSettings } from '../prompt/call-settings';
|
2 | 3 | import { convertToLanguageModelPrompt } from '../prompt/convert-to-language-model-prompt';
|
3 | 4 | import { getValidatedPrompt } from '../prompt/get-validated-prompt';
|
@@ -184,6 +185,15 @@ Warnings from the model provider (e.g. unsupported settings)
|
184 | 185 | */
|
185 | 186 | readonly warnings: CallWarning[] | undefined;
|
186 | 187 |
|
| 188 | + /** |
| 189 | +The response messages that were generated during the call. It consists of an assistant message, |
| 190 | +potentially containing tool calls. |
| 191 | +When there are tool results, there is an additional tool message with the tool results that are available. |
| 192 | +If there are tools that do not have execute functions, they are not included in the tool results and |
| 193 | +need to be added separately. |
| 194 | + */ |
| 195 | + readonly responseMessages: Array<CoreAssistantMessage | CoreToolMessage>; |
| 196 | + |
187 | 197 | /**
|
188 | 198 | Optional raw response data.
|
189 | 199 | */
|
@@ -220,7 +230,42 @@ Logprobs for the completion.
|
220 | 230 | this.warnings = options.warnings;
|
221 | 231 | this.rawResponse = options.rawResponse;
|
222 | 232 | this.logprobs = options.logprobs;
|
| 233 | + this.responseMessages = toResponseMessages(options); |
| 234 | + } |
| 235 | +} |
| 236 | + |
| 237 | +/** |
| 238 | +Converts the result of a `generateText` call to a list of response messages. |
| 239 | + */ |
| 240 | +function toResponseMessages<TOOLS extends Record<string, CoreTool>>({ |
| 241 | + text, |
| 242 | + toolCalls, |
| 243 | + toolResults, |
| 244 | +}: { |
| 245 | + text: string; |
| 246 | + toolCalls: ToToolCallArray<TOOLS>; |
| 247 | + toolResults: ToToolResultArray<TOOLS>; |
| 248 | +}): Array<CoreAssistantMessage | CoreToolMessage> { |
| 249 | + const responseMessages: Array<CoreAssistantMessage | CoreToolMessage> = []; |
| 250 | + |
| 251 | + responseMessages.push({ |
| 252 | + role: 'assistant', |
| 253 | + content: [{ type: 'text', text }, ...toolCalls], |
| 254 | + }); |
| 255 | + |
| 256 | + if (toolResults.length > 0) { |
| 257 | + responseMessages.push({ |
| 258 | + role: 'tool', |
| 259 | + content: toolResults.map(result => ({ |
| 260 | + type: 'tool-result', |
| 261 | + toolCallId: result.toolCallId, |
| 262 | + toolName: result.toolName, |
| 263 | + result: result.result, |
| 264 | + })), |
| 265 | + }); |
223 | 266 | }
|
| 267 | + |
| 268 | + return responseMessages; |
224 | 269 | }
|
225 | 270 |
|
226 | 271 | /**
|
|
0 commit comments