| 1 |
import { makeId } from '@agent/lib/util'; |
| 2 |
|
| 3 |
// Providers reject a tool/function name with characters outside this set, and |
| 4 |
// ability ids are namespaced with a slash (e.g. woocommerce/products-query). |
| 5 |
export const toolName = (id) => id.replace(/[^a-zA-Z0-9_.-]/g, '_'); |
| 6 |
|
| 7 |
// The provider rejects a tool result unless it's paired with an assistant |
| 8 |
// tool-call sharing its id, so emit both. |
| 9 |
export const buildToolMessages = ({ id, inputs, result }) => { |
| 10 |
const toolCallId = makeId(); |
| 11 |
const name = toolName(id); |
| 12 |
return [ |
| 13 |
{ |
| 14 |
role: 'assistant', |
| 15 |
content: [ |
| 16 |
{ type: 'tool-call', toolCallId, toolName: name, input: inputs ?? {} }, |
| 17 |
], |
| 18 |
}, |
| 19 |
{ |
| 20 |
role: 'tool', |
| 21 |
content: [ |
| 22 |
{ |
| 23 |
type: 'tool-result', |
| 24 |
toolCallId, |
| 25 |
toolName: name, |
| 26 |
output: { type: 'json', value: result ?? null }, |
| 27 |
}, |
| 28 |
], |
| 29 |
}, |
| 30 |
]; |
| 31 |
}; |
| 32 |
|