mirror of
https://gitlab.com/soapbox-pub/shakespeare.git
synced 2026-12-05 16:23:36 +00:00
Add text_editor tools
This commit is contained in:
11 files changed
+1090
-333
No files matched your search
@@ -5,13 +5,15 @@ import { Button } from '@/components/ui/button';
|
||||
import { Textarea } from '@/components/ui/textarea';
|
||||
import { Send, Settings, Play, CloudUpload } from 'lucide-react';
|
||||
import { useAISettings } from '@/hooks/useAISettings';
|
||||
import { FsToolSet } from '@/lib/FsToolSet';
|
||||
import { useFS } from '@/hooks/useFS';
|
||||
import { useJSRuntime } from '@/hooks/useJSRuntime';
|
||||
import { copyDirectory } from '@/lib/copyFiles';
|
||||
import { generateSecretKey, getPublicKey, nip19 } from 'nostr-tools';
|
||||
import { bytesToHex } from 'nostr-tools/utils';
|
||||
import { MessageItem } from '@/components/ai/MessageItem';
|
||||
import { TextEditorViewTool } from '@/lib/tools/TextEditorViewTool';
|
||||
import { TextEditorWriteTool } from '@/lib/tools/TextEditorWriteTool';
|
||||
import { TextEditorStrReplaceTool } from '@/lib/tools/TextEditorStrReplaceTool';
|
||||
|
||||
interface ChatPaneProps {
|
||||
projectId: string;
|
||||
@@ -165,18 +167,19 @@ BASE_DOMAIN=nostrdeploy.com`);
|
||||
});
|
||||
|
||||
const provider = openai(settings.model);
|
||||
const toolSet = new FsToolSet(browserFS, `/projects/${projectId}`);
|
||||
|
||||
return generateText({
|
||||
model: provider,
|
||||
messages,
|
||||
maxSteps: 100,
|
||||
onStepFinish(stepResult) {
|
||||
console.log(stepResult);
|
||||
addMessages(stepResult.response.messages);
|
||||
},
|
||||
tools: {
|
||||
readFile: toolSet.readFile,
|
||||
writeFile: toolSet.writeFile,
|
||||
text_editor_view: new TextEditorViewTool(browserFS, `/projects/${projectId}`),
|
||||
text_editor_write: new TextEditorWriteTool(browserFS, `/projects/${projectId}`),
|
||||
text_editor_str_replace: new TextEditorStrReplaceTool(browserFS, `/projects/${projectId}`),
|
||||
},
|
||||
system: `You are an AI assistant helping users build custom Nostr websites. You have access to tools that allow you to read, write, and manage files in the project, as well as build the project.
|
||||
|
||||
@@ -332,7 +335,6 @@ When creating new components or pages, follow the existing patterns in the codeb
|
||||
key={index}
|
||||
message={message}
|
||||
userDisplayName="You"
|
||||
allMessages={messages}
|
||||
/>
|
||||
))}
|
||||
|
||||
|
||||
@@ -9,34 +9,13 @@ 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, CoreToolMessage } from 'ai';
|
||||
import type { CoreAssistantMessage } from 'ai';
|
||||
|
||||
interface AssistantContentProps {
|
||||
content: CoreAssistantMessage['content'];
|
||||
allMessages?: unknown[];
|
||||
}
|
||||
|
||||
export const AssistantContent = memo(({ content, allMessages = [] }: AssistantContentProps) => {
|
||||
// Helper function to find tool result for a given tool call
|
||||
const findToolResult = (toolCallId: string) => {
|
||||
// Look through all tool messages to find the one with our toolCallId
|
||||
for (const msg of allMessages) {
|
||||
if ((msg as { role: string }).role === 'tool') {
|
||||
const toolMessage = msg as CoreToolMessage;
|
||||
if (Array.isArray(toolMessage.content)) {
|
||||
const toolResultPart = toolMessage.content.find(
|
||||
part => part.type === 'tool-result' && part.toolCallId === toolCallId
|
||||
);
|
||||
if (toolResultPart) {
|
||||
return toolResultPart;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return undefined;
|
||||
};
|
||||
|
||||
|
||||
export const AssistantContent = memo(({ content }: AssistantContentProps) => {
|
||||
// Handle string content (simple text messages)
|
||||
if (typeof content === 'string') {
|
||||
return (
|
||||
@@ -136,8 +115,8 @@ export const AssistantContent = memo(({ content, allMessages = [] }: AssistantCo
|
||||
</div>
|
||||
);
|
||||
} else if (item.type === 'tool-call') {
|
||||
// Special rendering for js-dev__text_editor_str_replace tool
|
||||
if (item.toolName === 'js-dev__text_editor_str_replace' && item.args) {
|
||||
// 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 };
|
||||
return (
|
||||
<DiffRenderer
|
||||
@@ -149,8 +128,8 @@ export const AssistantContent = memo(({ content, allMessages = [] }: AssistantCo
|
||||
);
|
||||
}
|
||||
|
||||
// Special rendering for js-dev__run_script tool
|
||||
if (item.toolName === 'js-dev__run_script' && item.args) {
|
||||
// Special rendering for run_script tool
|
||||
if (item.toolName === 'run_script' && item.args) {
|
||||
const { script } = item.args as { script: string };
|
||||
return (
|
||||
<ScriptRunner
|
||||
@@ -160,71 +139,8 @@ export const AssistantContent = memo(({ content, allMessages = [] }: AssistantCo
|
||||
);
|
||||
}
|
||||
|
||||
// Special rendering for readFile tool
|
||||
if (item.toolName === 'readFile' && item.args) {
|
||||
const { filePath } = item.args as { filePath: string };
|
||||
const toolResultPart = findToolResult(item.toolCallId);
|
||||
let result: string | undefined;
|
||||
const isError = toolResultPart?.isError || false;
|
||||
|
||||
if (toolResultPart && toolResultPart.result) {
|
||||
if (typeof toolResultPart.result === 'string') {
|
||||
result = toolResultPart.result;
|
||||
} else if (toolResultPart.result && typeof toolResultPart.result === 'object') {
|
||||
// Handle FsToolSet readFile result format: { content, success }
|
||||
const resultObj = toolResultPart.result as Record<string, unknown>;
|
||||
if (resultObj.content && typeof resultObj.content === 'string') {
|
||||
result = resultObj.content;
|
||||
} else {
|
||||
result = JSON.stringify(toolResultPart.result);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<FileViewer
|
||||
key={index}
|
||||
path={filePath}
|
||||
result={result}
|
||||
isError={isError}
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
// Special rendering for writeFile tool
|
||||
if (item.toolName === 'writeFile' && item.args) {
|
||||
const { filePath, content } = item.args as { filePath: string; content: string };
|
||||
const toolResultPart = findToolResult(item.toolCallId);
|
||||
let result: string | undefined;
|
||||
const isError = toolResultPart?.isError || false;
|
||||
|
||||
if (toolResultPart && toolResultPart.result) {
|
||||
if (typeof toolResultPart.result === 'string') {
|
||||
result = toolResultPart.result;
|
||||
} else if (toolResultPart.result && typeof toolResultPart.result === 'object') {
|
||||
// Handle FsToolSet writeFile result format: { success, message }
|
||||
const resultObj = toolResultPart.result as Record<string, unknown>;
|
||||
if (resultObj.message && typeof resultObj.message === 'string') {
|
||||
result = resultObj.message;
|
||||
} else {
|
||||
result = JSON.stringify(toolResultPart.result);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<FileWriter
|
||||
key={index}
|
||||
path={filePath}
|
||||
fileText={content}
|
||||
result={result}
|
||||
isError={isError}
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
// Special rendering for js-dev__text_editor_view tool
|
||||
if (item.toolName === 'js-dev__text_editor_view' && item.args) {
|
||||
// Special rendering for text_editor_view tool
|
||||
if (item.toolName === 'text_editor_view' && item.args) {
|
||||
const { path } = item.args as { path: string };
|
||||
return (
|
||||
<FileViewer
|
||||
@@ -234,8 +150,8 @@ export const AssistantContent = memo(({ content, allMessages = [] }: AssistantCo
|
||||
);
|
||||
}
|
||||
|
||||
// Special rendering for js-dev__text_editor_write tool
|
||||
if (item.toolName === 'js-dev__text_editor_write' && item.args) {
|
||||
// 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 };
|
||||
return (
|
||||
<FileWriter
|
||||
@@ -246,8 +162,8 @@ export const AssistantContent = memo(({ content, allMessages = [] }: AssistantCo
|
||||
);
|
||||
}
|
||||
|
||||
// Special rendering for js-dev__shell tool
|
||||
if (item.toolName === 'js-dev__shell' && item.args) {
|
||||
// Special rendering for shell tool
|
||||
if (item.toolName === 'shell' && item.args) {
|
||||
const { command } = item.args as { command: string };
|
||||
return (
|
||||
<ShellCommand
|
||||
@@ -257,8 +173,8 @@ export const AssistantContent = memo(({ content, allMessages = [] }: AssistantCo
|
||||
);
|
||||
}
|
||||
|
||||
// Special rendering for js-dev__npm_add_package tool
|
||||
if (item.toolName === 'js-dev__npm_add_package' && item.args) {
|
||||
// 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 };
|
||||
return (
|
||||
<PackageManager
|
||||
@@ -272,8 +188,8 @@ export const AssistantContent = memo(({ content, allMessages = [] }: AssistantCo
|
||||
);
|
||||
}
|
||||
|
||||
// Special rendering for js-dev__npm_remove_package tool
|
||||
if (item.toolName === 'js-dev__npm_remove_package' && item.args) {
|
||||
// Special rendering for npm_remove_package tool
|
||||
if (item.toolName === 'npm_remove_package' && item.args) {
|
||||
const { name } = item.args as { name: string };
|
||||
return (
|
||||
<PackageManager
|
||||
@@ -285,8 +201,8 @@ export const AssistantContent = memo(({ content, allMessages = [] }: AssistantCo
|
||||
);
|
||||
}
|
||||
|
||||
// Special rendering for js-dev__jsr_add_package tool
|
||||
if (item.toolName === 'js-dev__jsr_add_package' && item.args) {
|
||||
// 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 };
|
||||
return (
|
||||
<PackageManager
|
||||
@@ -300,8 +216,8 @@ export const AssistantContent = memo(({ content, allMessages = [] }: AssistantCo
|
||||
);
|
||||
}
|
||||
|
||||
// Special rendering for js-dev__jsr_remove_package tool
|
||||
if (item.toolName === 'js-dev__jsr_remove_package' && item.args) {
|
||||
// Special rendering for jsr_remove_package tool
|
||||
if (item.toolName === 'jsr_remove_package' && item.args) {
|
||||
const { name } = item.args as { name: string };
|
||||
return (
|
||||
<PackageManager
|
||||
|
||||
@@ -8,14 +8,12 @@ interface MessageItemProps {
|
||||
message: (CoreUserMessage | CoreAssistantMessage | CoreToolMessage) & { id: string };
|
||||
userDisplayName?: string;
|
||||
userProfileImage?: string;
|
||||
allMessages?: ((CoreUserMessage | CoreAssistantMessage | CoreToolMessage) & { id: string })[];
|
||||
}
|
||||
|
||||
export const MessageItem = memo(({
|
||||
message,
|
||||
userDisplayName = 'You',
|
||||
userProfileImage,
|
||||
allMessages = []
|
||||
userProfileImage
|
||||
}: MessageItemProps) => {
|
||||
// Don't render tool messages as separate items - they're integrated into assistant messages
|
||||
if (message.role === 'tool') {
|
||||
@@ -62,7 +60,6 @@ export const MessageItem = memo(({
|
||||
) : message.role === 'assistant' ? (
|
||||
<AssistantContent
|
||||
content={(message as CoreAssistantMessage).content}
|
||||
allMessages={allMessages}
|
||||
/>
|
||||
) : null}
|
||||
</div>
|
||||
|
||||
Reference in new issue
Block a user