| @@ -3,11 +3,33 @@ | ||
| 3 | 3 | // Providers reject a tool/function name with characters outside this set, and |
| 4 | 4 | // ability ids are namespaced with a slash (e.g. woocommerce/products-query). |
| 5 | 5 | export const toolName = (id) => id.replace(/[^a-zA-Z0-9_.-]/g, '_'); |
| 6 | 6 | |
| 7 | +// blockSchemas reaches the backend via workflowData; in the transcript it's static | |
| 8 | +// metadata re-read every turn. Serialize-time so persisted events come out clean too. | |
| 9 | +const trimResult = (result) => { | |
| 10 | + if (!result || typeof result !== 'object') return result ?? null; | |
| 11 | + const { blockSchemas: _, ...rest } = result; | |
| 12 | + return rest; | |
| 13 | +}; | |
| 14 | + | |
| 15 | +// Values go stale between runs; ids don't. Keep error or stripFailedToolRuns | |
| 16 | +// can't spot a failure. | |
| 17 | +const summarizeResult = (result) => { | |
| 18 | + if (!result || typeof result !== 'object') return result ?? null; | |
| 19 | + return Object.fromEntries( | |
| 20 | + Object.entries(result).filter( | |
| 21 | + ([key]) => key === 'error' || /^id$|_id$|Id$/.test(key), | |
| 22 | + ), | |
| 23 | + ); | |
| 24 | +}; | |
| 25 | + | |
| 7 | 26 | // The provider rejects a tool result unless it's paired with an assistant |
| 8 | 27 | // tool-call sharing its id, so emit both. |
| 9 | -export const buildToolMessages = ({ id, inputs, result }) => { | |
| 28 | +export const buildToolMessages = ( | |
| 29 | + { id, inputs, result }, | |
| 30 | + { summarize = false } = {}, | |
| 31 | +) => { | |
| 10 | 32 | const toolCallId = makeId(); |
| 11 | 33 | const name = toolName(id); |
| 12 | 34 | return [ |
| 13 | 35 | { |
| @@ -22,9 +44,12 @@ | ||
| 22 | 44 | { |
| 23 | 45 | type: 'tool-result', |
| 24 | 46 | toolCallId, |
| 25 | 47 | toolName: name, |
| 26 | - output: { type: 'json', value: result ?? null }, | |
| 48 | + output: { | |
| 49 | + type: 'json', | |
| 50 | + value: summarize ? summarizeResult(result) : trimResult(result), | |
| 51 | + }, | |
| 27 | 52 | }, |
| 28 | 53 | ], |
| 29 | 54 | }, |
| 30 | 55 | ]; |