mirror of
https://gitlab.com/soapbox-pub/shakespeare.git
synced 2026-12-03 07:57:22 +00:00
Expose console messages via tool call.
This commit is contained in:
1 parent
8108407478
commit
804efb4d23
5 files changed
+120
-7
No files matched your search
@@ -51,6 +51,49 @@ Shakespeare provides AI agents with specialized tools for project development (n
|
||||
- **NostrReadNipsIndexTool**: Read the full list of NIPs, kinds, and tags
|
||||
- **NostrFetchEventTool**: Fetch Nostr events using NIP-19 identifiers
|
||||
- **NostrGenerateKindTool**: Generate unused Nostr event kind numbers
|
||||
- **ReadConsoleMessagesTool**: Read console messages from project preview with filtering capabilities
|
||||
|
||||
#### Console Message Reading
|
||||
|
||||
The `ReadConsoleMessagesTool` allows you to access console messages from the project preview iframe, enabling proactive error detection and debugging assistance.
|
||||
|
||||
**Usage Examples:**
|
||||
|
||||
```typescript
|
||||
// Read all console messages
|
||||
const allMessages = await read_console_messages({});
|
||||
|
||||
// Read only error messages
|
||||
const errorMessages = await read_console_messages({ filter: 'error' });
|
||||
|
||||
// Read last 10 messages of any type
|
||||
const recentMessages = await read_console_messages({ limit: 10 });
|
||||
|
||||
// Read last 5 warning messages
|
||||
const recentWarnings = await read_console_messages({ filter: 'warn', limit: 5 });
|
||||
```
|
||||
|
||||
**Parameters:**
|
||||
- `filter` (optional): `'error' | 'warn' | 'info' | 'debug' | 'log' | 'all'` - defaults to `'all'`
|
||||
- `limit` (optional): `number` - maximum number of messages to return, defaults to all messages
|
||||
|
||||
**Use Cases:**
|
||||
- **Proactive Error Detection**: Automatically detect and fix console errors
|
||||
- **Debugging Assistance**: Correlate console errors with code changes
|
||||
- **Build Validation**: Check for runtime errors after building
|
||||
- **Performance Monitoring**: Identify performance-related warnings
|
||||
|
||||
**Example Workflow:**
|
||||
```
|
||||
User: Fix the errors in my app
|
||||
AI: Let me check the console for errors first...
|
||||
read_console_messages({ filter: 'error' })
|
||||
// Returns: Found 2 errors (level: error):
|
||||
// [14:32:15] [ERROR] Failed to load module 'react'
|
||||
// [14:32:16] [ERROR] Network timeout at https://api.example.com/data
|
||||
|
||||
AI: I can see there are 2 errors in the console. Let me fix the module loading issue first...
|
||||
```
|
||||
|
||||
#### Shell Commands
|
||||
|
||||
|
||||
@@ -33,6 +33,7 @@ import { NostrReadNipsIndexTool } from '@/lib/tools/NostrReadNipsIndexTool';
|
||||
import { NostrGenerateKindTool } from '@/lib/tools/NostrGenerateKindTool';
|
||||
import { ShellTool } from '@/lib/tools/ShellTool';
|
||||
import { TypecheckTool } from '@/lib/tools/TypecheckTool';
|
||||
import { ReadConsoleMessagesTool } from '@/lib/tools/ReadConsoleMessagesTool';
|
||||
import { toolToOpenAI } from '@/lib/tools/openai-adapter';
|
||||
import { Tool } from '@/lib/tools/Tool';
|
||||
import OpenAI from 'openai';
|
||||
@@ -48,6 +49,7 @@ interface ChatPaneProps {
|
||||
onLoadingChange?: (isLoading: boolean) => void;
|
||||
isLoading?: boolean;
|
||||
isBuildLoading?: boolean;
|
||||
consoleMessages?: import('@/pages/ProjectView').ConsoleMessage[];
|
||||
}
|
||||
|
||||
export interface ChatPaneRef {
|
||||
@@ -62,6 +64,7 @@ export const ChatPane = forwardRef<ChatPaneRef, ChatPaneProps>(({
|
||||
onLoadingChange,
|
||||
isLoading: externalIsLoading,
|
||||
isBuildLoading: externalIsBuildLoading,
|
||||
consoleMessages,
|
||||
}, ref) => {
|
||||
const { t } = useTranslation();
|
||||
const [input, setInput] = useState('');
|
||||
@@ -112,6 +115,7 @@ export const ChatPane = forwardRef<ChatPaneRef, ChatPaneProps>(({
|
||||
nostr_read_nips_index: new NostrReadNipsIndexTool(),
|
||||
nostr_generate_kind: new NostrGenerateKindTool(),
|
||||
shell: new ShellTool(fs, cwd, git),
|
||||
read_console_messages: new ReadConsoleMessagesTool(consoleMessages || []),
|
||||
};
|
||||
|
||||
// Add deploy tool only if user is logged in
|
||||
@@ -123,7 +127,7 @@ export const ChatPane = forwardRef<ChatPaneRef, ChatPaneProps>(({
|
||||
}
|
||||
|
||||
return baseTools;
|
||||
}, [fs, git, cwd, user, projectId]);
|
||||
}, [fs, git, cwd, user, projectId, consoleMessages]);
|
||||
|
||||
// Convert tools to OpenAI format
|
||||
const tools = useMemo(() => {
|
||||
|
||||
@@ -33,6 +33,8 @@ interface PreviewPaneProps {
|
||||
projectName?: string;
|
||||
onFirstInteraction?: () => void;
|
||||
isPreviewable?: boolean;
|
||||
consoleMessages?: ConsoleMessage[];
|
||||
setConsoleMessages?: React.Dispatch<React.SetStateAction<ConsoleMessage[]>>;
|
||||
}
|
||||
|
||||
interface JSONRPCRequest {
|
||||
@@ -65,13 +67,14 @@ interface JSONRPCResponse {
|
||||
id: number;
|
||||
}
|
||||
|
||||
interface ConsoleMessage {
|
||||
// Console message type (now defined in ProjectView for sharing)
|
||||
export interface ConsoleMessage {
|
||||
id: number;
|
||||
level: 'log' | 'warn' | 'error' | 'info' | 'debug';
|
||||
message: string;
|
||||
}
|
||||
|
||||
export function PreviewPane({ projectId, activeTab, onToggleView, projectName, onFirstInteraction, isPreviewable = true }: PreviewPaneProps) {
|
||||
export function PreviewPane({ projectId, activeTab, onToggleView, projectName, onFirstInteraction, isPreviewable = true, consoleMessages: externalConsoleMessages = [], setConsoleMessages: externalSetConsoleMessages }: PreviewPaneProps) {
|
||||
const { t } = useTranslation();
|
||||
const [selectedFile, setSelectedFile] = useState<string | null>(null);
|
||||
const [fileContent, setFileContent] = useState<string>('');
|
||||
@@ -82,9 +85,14 @@ export function PreviewPane({ projectId, activeTab, onToggleView, projectName, o
|
||||
const [currentPath, setCurrentPath] = useState('/');
|
||||
const [navigationHistory, setNavigationHistory] = useState<string[]>(['/']);
|
||||
const [historyIndex, setHistoryIndex] = useState(0);
|
||||
const [consoleMessages, setConsoleMessages] = useState<ConsoleMessage[]>([]);
|
||||
const [copiedMessageId, setCopiedMessageId] = useState<number | null>(null);
|
||||
const [deployDialogOpen, setDeployDialogOpen] = useState(false);
|
||||
|
||||
// Use external state if provided, otherwise use internal state
|
||||
const [internalConsoleMessages, setInternalConsoleMessages] = useState<ConsoleMessage[]>([]);
|
||||
const consoleMessages = externalConsoleMessages || internalConsoleMessages;
|
||||
const setConsoleMessages = externalSetConsoleMessages || setInternalConsoleMessages;
|
||||
const handleSetConsoleMessages = setConsoleMessages;
|
||||
const iframeRef = useRef<HTMLIFrameElement>(null);
|
||||
const { fs } = useFS();
|
||||
const projectsManager = useProjectsManager();
|
||||
@@ -242,11 +250,11 @@ export function PreviewPane({ projectId, activeTab, onToggleView, projectName, o
|
||||
message: params.message,
|
||||
};
|
||||
|
||||
setConsoleMessages(prev => [...prev, newConsoleMessage]);
|
||||
handleSetConsoleMessages(prev => [...prev, newConsoleMessage]);
|
||||
|
||||
// Log to parent console for debugging with appropriate level
|
||||
console[normalizedLevel](`[IFRAME ${params.level.toUpperCase()}] ${params.message}`);
|
||||
}, []);
|
||||
}, [handleSetConsoleMessages]);
|
||||
|
||||
const handleFetch = useCallback(async (request: JSONRPCRequest) => {
|
||||
const { params, id } = request;
|
||||
@@ -447,7 +455,7 @@ export function PreviewPane({ projectId, activeTab, onToggleView, projectName, o
|
||||
};
|
||||
|
||||
const clearConsole = () => {
|
||||
setConsoleMessages([]);
|
||||
handleSetConsoleMessages([]);
|
||||
};
|
||||
|
||||
const copyMessageToClipboard = async (msg: ConsoleMessage) => {
|
||||
|
||||
@@ -0,0 +1,45 @@
|
||||
import { Tool } from './Tool';
|
||||
import type { ConsoleMessage } from '@/pages/ProjectView';
|
||||
|
||||
export interface ReadConsoleMessagesParams {
|
||||
filter?: 'error' | 'warn' | 'info' | 'debug' | 'log' | 'all';
|
||||
limit?: number;
|
||||
}
|
||||
|
||||
export class ReadConsoleMessagesTool implements Tool<ReadConsoleMessagesParams> {
|
||||
private consoleMessages: ConsoleMessage[];
|
||||
|
||||
constructor(consoleMessages: ConsoleMessage[]) {
|
||||
this.consoleMessages = consoleMessages;
|
||||
}
|
||||
|
||||
async execute(params: ReadConsoleMessagesParams): Promise<string> {
|
||||
const { filter = 'all', limit } = params;
|
||||
|
||||
let filteredMessages = this.consoleMessages;
|
||||
|
||||
// Apply filter if specified
|
||||
if (filter !== 'all') {
|
||||
filteredMessages = this.consoleMessages.filter(msg => msg.level === filter);
|
||||
}
|
||||
|
||||
// Apply limit if specified
|
||||
if (limit && limit > 0) {
|
||||
filteredMessages = filteredMessages.slice(-limit);
|
||||
}
|
||||
|
||||
if (filteredMessages.length === 0) {
|
||||
return `No console messages found${filter !== 'all' ? ` for level: ${filter}` : ''}.`;
|
||||
}
|
||||
|
||||
// Format the messages
|
||||
const formattedMessages = filteredMessages.map(msg => {
|
||||
const timestamp = new Date(msg.id).toLocaleTimeString();
|
||||
return `[${timestamp}] [${msg.level.toUpperCase()}] ${msg.message}`;
|
||||
}).join('\n');
|
||||
|
||||
return `Found ${filteredMessages.length} console message${filteredMessages.length !== 1 ? 's' : ''}${filter !== 'all' ? ` (level: ${filter})` : ''}:\n\n${formattedMessages}`;
|
||||
}
|
||||
|
||||
description = 'Read console messages from the project preview. Can filter by level and limit results.';
|
||||
}
|
||||
@@ -21,6 +21,13 @@ import { StarButton } from '@/components/StarButton';
|
||||
import { useBuildProject } from '@/hooks/useBuildProject';
|
||||
import { useIsProjectPreviewable } from '@/hooks/useIsProjectPreviewable';
|
||||
|
||||
// Console message type (copied from PreviewPane for minimal change)
|
||||
export interface ConsoleMessage {
|
||||
id: number;
|
||||
level: 'log' | 'warn' | 'error' | 'info' | 'debug';
|
||||
message: string;
|
||||
}
|
||||
|
||||
export function ProjectView() {
|
||||
const { projectId } = useParams<{ projectId: string }>();
|
||||
const { t } = useTranslation();
|
||||
@@ -30,6 +37,7 @@ export function ProjectView() {
|
||||
const [mobileView, setMobileView] = useState<'chat' | 'preview' | 'code'>('chat');
|
||||
const [isAILoading, setIsAILoading] = useState(false);
|
||||
const [isProjectInfoOpen, setIsProjectInfoOpen] = useState(false);
|
||||
const [consoleMessages, setConsoleMessages] = useState<ConsoleMessage[]>([]);
|
||||
const projectsManager = useProjectsManager();
|
||||
const chatPaneRef = useRef<ChatPaneRef>(null);
|
||||
const navigate = useNavigate();
|
||||
@@ -245,6 +253,8 @@ export function ProjectView() {
|
||||
activeTab={mobileView}
|
||||
projectName={project.name}
|
||||
onFirstInteraction={handleFirstInteraction}
|
||||
consoleMessages={consoleMessages}
|
||||
setConsoleMessages={setConsoleMessages}
|
||||
/>
|
||||
) : (
|
||||
<div className="h-full p-4 space-y-4">
|
||||
@@ -407,6 +417,7 @@ export function ProjectView() {
|
||||
onLoadingChange={handleAILoadingChange}
|
||||
isLoading={isAILoading}
|
||||
isBuildLoading={build.isPending}
|
||||
consoleMessages={consoleMessages}
|
||||
/>
|
||||
) : (
|
||||
<div className="h-full p-4 space-y-4">
|
||||
@@ -434,6 +445,8 @@ export function ProjectView() {
|
||||
projectName={project.name}
|
||||
onFirstInteraction={handleFirstInteraction}
|
||||
isPreviewable={isPreviewable}
|
||||
consoleMessages={consoleMessages}
|
||||
setConsoleMessages={setConsoleMessages}
|
||||
/>
|
||||
) : (
|
||||
<div className="h-full p-4 space-y-4">
|
||||
|
||||
Reference in new issue
Block a user