Add ToolRenderResult interface for custom tool rendering
- Changed ToolRenderer return type from TemplateResult to ToolRenderResult
- ToolRenderResult = { content: TemplateResult, isCustom: boolean }
- isCustom: true = no card wrapper, false = wrap in card
- Updated all existing tool renderers to return new format
- Updated Messages.ts to handle custom rendering
This enables tools to render without default card chrome when needed.
This commit is contained in:
@@ -1,9 +1,9 @@
|
||||
import { html, type TemplateResult } from "@mariozechner/mini-lit";
|
||||
import { html } from "@mariozechner/mini-lit";
|
||||
import type { ToolResultMessage } from "@mariozechner/pi-ai";
|
||||
import { Calculator } from "lucide";
|
||||
import { i18n } from "../../utils/i18n.js";
|
||||
import { renderHeader } from "../renderer-registry.js";
|
||||
import type { ToolRenderer } from "../types.js";
|
||||
import type { ToolRenderer, ToolRenderResult } from "../types.js";
|
||||
|
||||
interface CalculateParams {
|
||||
expression: string;
|
||||
@@ -11,7 +11,7 @@ interface CalculateParams {
|
||||
|
||||
// Calculate tool has undefined details (only uses output)
|
||||
export class CalculateRenderer implements ToolRenderer<CalculateParams, undefined> {
|
||||
render(params: CalculateParams | undefined, result: ToolResultMessage<undefined> | undefined): TemplateResult {
|
||||
render(params: CalculateParams | undefined, result: ToolResultMessage<undefined> | undefined): ToolRenderResult {
|
||||
const state = result ? (result.isError ? "error" : "complete") : "inprogress";
|
||||
|
||||
// Full params + full result
|
||||
@@ -20,29 +20,35 @@ export class CalculateRenderer implements ToolRenderer<CalculateParams, undefine
|
||||
|
||||
// Error: show expression in header, error below
|
||||
if (result.isError) {
|
||||
return html`
|
||||
<div class="space-y-3">
|
||||
${renderHeader(state, Calculator, params.expression)}
|
||||
<div class="text-sm text-destructive">${output}</div>
|
||||
</div>
|
||||
`;
|
||||
return {
|
||||
content: html`
|
||||
<div class="space-y-3">
|
||||
${renderHeader(state, Calculator, params.expression)}
|
||||
<div class="text-sm text-destructive">${output}</div>
|
||||
</div>
|
||||
`,
|
||||
isCustom: false,
|
||||
};
|
||||
}
|
||||
|
||||
// Success: show expression = result in header
|
||||
return renderHeader(state, Calculator, `${params.expression} = ${output}`);
|
||||
return { content: renderHeader(state, Calculator, `${params.expression} = ${output}`), isCustom: false };
|
||||
}
|
||||
|
||||
// Full params, no result: just show header with expression in it
|
||||
if (params?.expression) {
|
||||
return renderHeader(state, Calculator, `${i18n("Calculating")} ${params.expression}`);
|
||||
return {
|
||||
content: renderHeader(state, Calculator, `${i18n("Calculating")} ${params.expression}`),
|
||||
isCustom: false,
|
||||
};
|
||||
}
|
||||
|
||||
// Partial params (empty expression), no result
|
||||
if (params && !params.expression) {
|
||||
return renderHeader(state, Calculator, i18n("Writing expression..."));
|
||||
return { content: renderHeader(state, Calculator, i18n("Writing expression...")), isCustom: false };
|
||||
}
|
||||
|
||||
// No params, no result
|
||||
return renderHeader(state, Calculator, i18n("Waiting for expression..."));
|
||||
return { content: renderHeader(state, Calculator, i18n("Waiting for expression...")), isCustom: false };
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user