Use OpenAI messages format

This commit is contained in:
Alex Gleason committed 2025-08-24 16:12:53 -05:00
1 parent 5d11f51e05
commit 558044db25
6 files changed
+345 -42

No files matched your search

+77 -4
View File
@@ -1,6 +1,6 @@
import { memo } from 'react';
import { Avatar, AvatarFallback, AvatarImage } from '@/components/ui/avatar';
import { Bot, User, Square } from 'lucide-react';
import { Bot, User, Square, Wrench } from 'lucide-react';
import { Button } from '@/components/ui/button';
import { AIMessage } from '@/hooks/useAIChat';
@@ -19,6 +19,57 @@ export const AIMessageItem = memo(({
isCurrentlyLoading = false,
onStopGeneration
}: AIMessageItemProps) => {
// Get display name based on role
const getDisplayName = () => {
switch (message.role) {
case 'user':
return userDisplayName;
case 'assistant':
return 'Assistant';
case 'system':
return 'System';
case 'tool':
return 'Tool';
default:
return 'Unknown';
}
};
// Get role label for display
const getRoleLabel = () => {
switch (message.role) {
case 'user':
return 'User';
case 'assistant':
return 'AI';
case 'system':
return 'System';
case 'tool':
return 'Tool';
default:
return message.role;
}
};
// Get content to display
const getContent = () => {
if (typeof message.content === 'string') {
return message.content;
}
if (Array.isArray(message.content)) {
// Handle array content (like images, text blocks)
return message.content
.map(item => {
if (typeof item === 'string') return item;
if (item.type === 'text') return item.text;
if (item.type === 'image_url') return `[Image: ${item.image_url.url}]`;
return '[Unknown content]';
})
.join('\n');
}
return message.content || '';
};
return (
<div className="flex gap-3">
<Avatar className="h-8 w-8 flex-shrink-0">
@@ -29,6 +80,12 @@ export const AIMessageItem = memo(({
<User className="h-4 w-4" />
</AvatarFallback>
</>
) : message.role === 'tool' ? (
<>
<AvatarFallback>
<Wrench className="h-4 w-4" />
</AvatarFallback>
</>
) : (
<>
<AvatarFallback>
@@ -40,10 +97,10 @@ export const AIMessageItem = memo(({
<div className="flex-1 min-w-0">
<div className="flex items-center gap-2 mb-1">
<span className="font-medium text-sm">
{message.role === 'user' ? userDisplayName : message.role === 'assistant' ? 'Assistant' : 'System'}
{getDisplayName()}
</span>
<span className="text-xs text-muted-foreground">
{message.role === 'user' ? 'User' : message.role === 'assistant' ? 'AI' : 'System'}
{getRoleLabel()}
</span>
{isCurrentlyLoading && onStopGeneration && (
<Button
@@ -59,8 +116,24 @@ export const AIMessageItem = memo(({
</div>
<div className="text-sm">
<div className="whitespace-pre-wrap break-words">
{message.content}
{getContent()}
</div>
{/* Display tool calls if present */}
{'tool_calls' in message && message.tool_calls && (
<div className="mt-2 space-y-2">
{message.tool_calls.map((toolCall, index) => (
<div key={index} className="bg-muted p-2 rounded text-xs">
<div className="font-medium">
Tool Call: {toolCall.type === 'function' ? toolCall.function.name : 'Unknown'}
</div>
<div className="text-muted-foreground">
{toolCall.type === 'function' ? toolCall.function.arguments : 'N/A'}
</div>
</div>
))}
</div>
)}
</div>
</div>
</div>
+13 -3
View File
@@ -18,6 +18,9 @@ import { TextEditorStrReplaceTool } from '@/lib/tools/TextEditorStrReplaceTool';
import { NpmAddPackageTool } from '@/lib/tools/NpmAddPackageTool';
import { NpmRemovePackageTool } from '@/lib/tools/NpmRemovePackageTool';
import { GitCommitTool } from '@/lib/tools/GitCommitTool';
import { toolToOpenAI } from '@/lib/tools/openai-adapter';
import { Tool } from '@/lib/tools/Tool';
import OpenAI from 'openai';
import { buildProject } from "@/lib/build";
interface ChatPaneProps {
@@ -36,7 +39,7 @@ export function ChatPane({ projectId, projectName }: ChatPaneProps) {
// Initialize AI chat with tools
const cwd = `/projects/${projectId}`;
const tools = {
const customTools = {
git_commit: new GitCommitTool(browserFS, cwd),
text_editor_view: new TextEditorViewTool(browserFS, cwd),
text_editor_write: new TextEditorWriteTool(browserFS, cwd),
@@ -45,6 +48,12 @@ export function ChatPane({ projectId, projectName }: ChatPaneProps) {
npm_remove_package: new NpmRemovePackageTool(browserFS, cwd),
};
// Convert tools to OpenAI format
const tools: Record<string, OpenAI.Chat.Completions.ChatCompletionTool> = {};
for (const [name, tool] of Object.entries(customTools)) {
tools[name] = toolToOpenAI(name, tool as Tool<unknown>);
}
const systemPrompt = `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 and manage npm packages.
Key capabilities:
@@ -84,6 +93,7 @@ When creating new components or pages, follow the existing patterns in the codeb
projectId,
projectName,
tools,
customTools,
systemPrompt,
onUpdateMetadata: (title, description) => {
updateMetadata(title, description);
@@ -330,9 +340,9 @@ BASE_DOMAIN=nostrdeploy.com`);
<div className="flex-1 overflow-y-scroll overflow-x-hidden" ref={scrollAreaRef}>
<div className="p-4 space-y-4">
{messages.map((message) => (
{messages.map((message, index) => (
<AIMessageItem
key={message.id}
key={`${index}-${message.role}-${typeof message.content === 'string' ? message.content.slice(0, 50) : 'content'}`}
message={message}
userDisplayName="You"
isCurrentlyLoading={isLoading && message.role === 'assistant'}