mirror of
https://gitlab.com/soapbox-pub/shakespeare.git
synced 2026-12-06 00:27:24 +00:00
Fix display of tool results
This commit is contained in:
1 parent
bae0ece813
commit
4c33d2ca02
4 files changed
+134
-16
No files matched your search
@@ -9,13 +9,60 @@ import { FileViewer } from '@/components/ai/FileViewer';
|
||||
import { FileWriter } from '@/components/ai/FileWriter';
|
||||
import { ShellCommand } from '@/components/ai/ShellCommand';
|
||||
import { PackageManager } from '@/components/ai/PackageManager';
|
||||
import type { CoreAssistantMessage } from 'ai';
|
||||
import type { CoreAssistantMessage, CoreToolMessage } from 'ai';
|
||||
|
||||
interface AssistantContentProps {
|
||||
content: CoreAssistantMessage['content'];
|
||||
toolResults?: CoreToolMessage[];
|
||||
}
|
||||
|
||||
export const AssistantContent = memo(({ content }: AssistantContentProps) => {
|
||||
export const AssistantContent = memo(({ content, toolResults = [] }: AssistantContentProps) => {
|
||||
// Helper function to find tool result by call ID
|
||||
const findToolResult = (toolCallId: string) => {
|
||||
for (const toolMessage of toolResults) {
|
||||
if (toolMessage.content) {
|
||||
for (const resultPart of toolMessage.content) {
|
||||
if (resultPart.toolCallId === toolCallId) {
|
||||
return resultPart;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
};
|
||||
|
||||
// Helper function to extract result text and error status
|
||||
const extractToolResult = (toolCallId: string): { result?: string; isError: boolean } => {
|
||||
const toolResult = findToolResult(toolCallId);
|
||||
|
||||
if (!toolResult) {
|
||||
return { isError: false };
|
||||
}
|
||||
|
||||
// Handle direct string results
|
||||
if (typeof toolResult.result === 'string') {
|
||||
return {
|
||||
result: toolResult.result,
|
||||
isError: toolResult.isError || false
|
||||
};
|
||||
}
|
||||
|
||||
// Handle CallToolResult structure
|
||||
if (toolResult.result && typeof toolResult.result === 'object') {
|
||||
const callResult = toolResult.result as {
|
||||
content?: Array<{ type: string; text: string }>;
|
||||
isError?: boolean;
|
||||
};
|
||||
if (callResult.content && Array.isArray(callResult.content) && callResult.content[0]?.text) {
|
||||
return {
|
||||
result: callResult.content[0].text,
|
||||
isError: callResult.isError || false
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
return { isError: toolResult.isError || false };
|
||||
};
|
||||
// Handle string content (simple text messages)
|
||||
if (typeof content === 'string') {
|
||||
return (
|
||||
@@ -118,12 +165,16 @@ export const AssistantContent = memo(({ content }: AssistantContentProps) => {
|
||||
// Special rendering for text_editor_str_replace tool
|
||||
if (item.toolName === 'text_editor_str_replace' && item.args) {
|
||||
const { path, old_str, new_str } = item.args as { path: string; old_str: string; new_str: string };
|
||||
const { result, isError } = extractToolResult(item.toolCallId);
|
||||
|
||||
return (
|
||||
<DiffRenderer
|
||||
key={index}
|
||||
path={path}
|
||||
oldStr={old_str}
|
||||
newStr={new_str}
|
||||
result={result}
|
||||
isError={isError}
|
||||
/>
|
||||
);
|
||||
}
|
||||
@@ -131,10 +182,14 @@ export const AssistantContent = memo(({ content }: AssistantContentProps) => {
|
||||
// Special rendering for run_script tool
|
||||
if (item.toolName === 'run_script' && item.args) {
|
||||
const { script } = item.args as { script: string };
|
||||
const { result, isError } = extractToolResult(item.toolCallId);
|
||||
|
||||
return (
|
||||
<ScriptRunner
|
||||
key={index}
|
||||
script={script}
|
||||
result={result}
|
||||
isError={isError}
|
||||
/>
|
||||
);
|
||||
}
|
||||
@@ -142,10 +197,14 @@ export const AssistantContent = memo(({ content }: AssistantContentProps) => {
|
||||
// Special rendering for text_editor_view tool
|
||||
if (item.toolName === 'text_editor_view' && item.args) {
|
||||
const { path } = item.args as { path: string };
|
||||
const { result, isError } = extractToolResult(item.toolCallId);
|
||||
|
||||
return (
|
||||
<FileViewer
|
||||
key={index}
|
||||
path={path}
|
||||
result={result}
|
||||
isError={isError}
|
||||
/>
|
||||
);
|
||||
}
|
||||
@@ -153,11 +212,15 @@ export const AssistantContent = memo(({ content }: AssistantContentProps) => {
|
||||
// Special rendering for text_editor_write tool
|
||||
if (item.toolName === 'text_editor_write' && item.args) {
|
||||
const { path, file_text } = item.args as { path: string; file_text: string };
|
||||
const { result, isError } = extractToolResult(item.toolCallId);
|
||||
|
||||
return (
|
||||
<FileWriter
|
||||
key={index}
|
||||
path={path}
|
||||
fileText={file_text}
|
||||
result={result}
|
||||
isError={isError}
|
||||
/>
|
||||
);
|
||||
}
|
||||
@@ -165,10 +228,14 @@ export const AssistantContent = memo(({ content }: AssistantContentProps) => {
|
||||
// Special rendering for shell tool
|
||||
if (item.toolName === 'shell' && item.args) {
|
||||
const { command } = item.args as { command: string };
|
||||
const { result, isError } = extractToolResult(item.toolCallId);
|
||||
|
||||
return (
|
||||
<ShellCommand
|
||||
key={index}
|
||||
command={command}
|
||||
result={result}
|
||||
isError={isError}
|
||||
/>
|
||||
);
|
||||
}
|
||||
@@ -176,6 +243,8 @@ export const AssistantContent = memo(({ content }: AssistantContentProps) => {
|
||||
// Special rendering for npm_add_package tool
|
||||
if (item.toolName === 'npm_add_package' && item.args) {
|
||||
const { name, version, dev } = item.args as { name: string; version?: string; dev?: boolean };
|
||||
const { result, isError } = extractToolResult(item.toolCallId);
|
||||
|
||||
return (
|
||||
<PackageManager
|
||||
key={index}
|
||||
@@ -184,6 +253,8 @@ export const AssistantContent = memo(({ content }: AssistantContentProps) => {
|
||||
packageName={name}
|
||||
version={version}
|
||||
dev={dev}
|
||||
result={result}
|
||||
isError={isError}
|
||||
/>
|
||||
);
|
||||
}
|
||||
@@ -191,12 +262,16 @@ export const AssistantContent = memo(({ content }: AssistantContentProps) => {
|
||||
// Special rendering for npm_remove_package tool
|
||||
if (item.toolName === 'npm_remove_package' && item.args) {
|
||||
const { name } = item.args as { name: string };
|
||||
const { result, isError } = extractToolResult(item.toolCallId);
|
||||
|
||||
return (
|
||||
<PackageManager
|
||||
key={index}
|
||||
action="remove"
|
||||
registry="npm"
|
||||
packageName={name}
|
||||
result={result}
|
||||
isError={isError}
|
||||
/>
|
||||
);
|
||||
}
|
||||
@@ -204,6 +279,8 @@ export const AssistantContent = memo(({ content }: AssistantContentProps) => {
|
||||
// Special rendering for jsr_add_package tool
|
||||
if (item.toolName === 'jsr_add_package' && item.args) {
|
||||
const { name, version, dev } = item.args as { name: string; version?: string; dev?: boolean };
|
||||
const { result, isError } = extractToolResult(item.toolCallId);
|
||||
|
||||
return (
|
||||
<PackageManager
|
||||
key={index}
|
||||
@@ -212,6 +289,8 @@ export const AssistantContent = memo(({ content }: AssistantContentProps) => {
|
||||
packageName={name}
|
||||
version={version}
|
||||
dev={dev}
|
||||
result={result}
|
||||
isError={isError}
|
||||
/>
|
||||
);
|
||||
}
|
||||
@@ -219,12 +298,16 @@ export const AssistantContent = memo(({ content }: AssistantContentProps) => {
|
||||
// Special rendering for jsr_remove_package tool
|
||||
if (item.toolName === 'jsr_remove_package' && item.args) {
|
||||
const { name } = item.args as { name: string };
|
||||
const { result, isError } = extractToolResult(item.toolCallId);
|
||||
|
||||
return (
|
||||
<PackageManager
|
||||
key={index}
|
||||
action="remove"
|
||||
registry="jsr"
|
||||
packageName={name}
|
||||
result={result}
|
||||
isError={isError}
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
@@ -7,11 +7,12 @@ interface DiffRendererProps {
|
||||
path: string;
|
||||
oldStr: string;
|
||||
newStr: string;
|
||||
result?: string;
|
||||
isError?: boolean;
|
||||
className?: string;
|
||||
}
|
||||
|
||||
export function DiffRenderer({ path, oldStr, newStr, isError = false, className }: DiffRendererProps) {
|
||||
export function DiffRenderer({ path, oldStr, newStr, result, isError = false, className }: DiffRendererProps) {
|
||||
// Split strings into lines for diff rendering
|
||||
const oldLines = oldStr.split('\n');
|
||||
const newLines = newStr.split('\n');
|
||||
@@ -145,6 +146,18 @@ export function DiffRenderer({ path, oldStr, newStr, isError = false, className
|
||||
</span>
|
||||
)}
|
||||
</div>
|
||||
|
||||
{/* Tool Result */}
|
||||
{result && (
|
||||
<div className={cn(
|
||||
"mt-3 p-3 rounded-md text-sm",
|
||||
isError
|
||||
? "bg-destructive/10 text-destructive border border-destructive/20"
|
||||
: "bg-muted/30 text-muted-foreground"
|
||||
)}>
|
||||
<pre className="whitespace-pre-wrap break-words">{result}</pre>
|
||||
</div>
|
||||
)}
|
||||
</CardContent>
|
||||
</Card>
|
||||
);
|
||||
|
||||
@@ -8,12 +8,14 @@ interface MessageItemProps {
|
||||
message: (CoreUserMessage | CoreAssistantMessage | CoreToolMessage) & { id: string };
|
||||
userDisplayName?: string;
|
||||
userProfileImage?: string;
|
||||
toolResults?: CoreToolMessage[];
|
||||
}
|
||||
|
||||
export const MessageItem = memo(({
|
||||
message,
|
||||
userDisplayName = 'You',
|
||||
userProfileImage
|
||||
userProfileImage,
|
||||
toolResults = []
|
||||
}: MessageItemProps) => {
|
||||
// Don't render tool messages as separate items - they're integrated into assistant messages
|
||||
if (message.role === 'tool') {
|
||||
@@ -60,6 +62,7 @@ export const MessageItem = memo(({
|
||||
) : message.role === 'assistant' ? (
|
||||
<AssistantContent
|
||||
content={(message as CoreAssistantMessage).content}
|
||||
toolResults={toolResults}
|
||||
/>
|
||||
) : null}
|
||||
</div>
|
||||
|
||||
Reference in new issue
Block a user