|
1 | 1 | import type { RuntimeKind } from '../../runtime/types.ts'; |
2 | | -import type { NextStep } from '../../types/common.ts'; |
| 2 | +import type { NextStep, OutputStyle, ToolResponse } from '../../types/common.ts'; |
3 | 3 | import { toKebabCase } from '../../runtime/naming.ts'; |
4 | 4 |
|
5 | 5 | function resolveLabel(step: NextStep): string { |
@@ -90,3 +90,31 @@ export function renderNextStepsSection(steps: NextStep[], runtime: RuntimeKind): |
90 | 90 |
|
91 | 91 | return `Next steps:\n${lines.join('\n')}`; |
92 | 92 | } |
| 93 | + |
| 94 | +export function processToolResponse( |
| 95 | + response: ToolResponse, |
| 96 | + runtime: RuntimeKind, |
| 97 | + style: OutputStyle = 'normal', |
| 98 | +): ToolResponse { |
| 99 | + const { nextSteps, ...rest } = response; |
| 100 | + |
| 101 | + if (!nextSteps || nextSteps.length === 0 || style === 'minimal') { |
| 102 | + return { ...rest }; |
| 103 | + } |
| 104 | + |
| 105 | + const nextStepsSection = renderNextStepsSection(nextSteps, runtime); |
| 106 | + |
| 107 | + const processedContent = response.content.map((item, index) => { |
| 108 | + if (item.type === 'text' && index === response.content.length - 1) { |
| 109 | + return { ...item, text: item.text + '\n\n' + nextStepsSection }; |
| 110 | + } |
| 111 | + return item; |
| 112 | + }); |
| 113 | + |
| 114 | + const hasTextContent = response.content.some((item) => item.type === 'text'); |
| 115 | + if (!hasTextContent && nextStepsSection) { |
| 116 | + processedContent.push({ type: 'text', text: nextStepsSection.trim() }); |
| 117 | + } |
| 118 | + |
| 119 | + return { ...rest, content: processedContent }; |
| 120 | +} |
0 commit comments