* Validates the history contains the correct roles. * * @throws Error if the history does not start with a user turn. * @throws Error if the history contains an invalid role.
(history: Content[])
| 61 | * @throws Error if the history contains an invalid role. |
| 62 | */ |
| 63 | function validateHistory(history: Content[]) { |
| 64 | for (const content of history) { |
| 65 | if (content.role !== 'user' && content.role !== 'model') { |
| 66 | throw new Error(`Role must be user or model, but got ${content.role}.`); |
| 67 | } |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Extracts the curated (valid) history from a comprehensive history. |