- Remove unused setIsGoosehintsModalOpen parameter in AppLayout - Add eslint-disable comments for insights and error in SessionsInsights - Remove unused TabsList and TabsTrigger imports in SettingsView
9.2 KiB
Phase 5: Backend Integration Plan
Overview
This phase connects the collaborative document editor with Goose's AI backend, enabling Goose to receive document context from chat messages and programmatically edit documents.
Current Status ✅
Frontend (Completed)
- CollaborativeDocEditor - Exposes programmatic API via
window.gooseEditors[docId] - Chat Input Listener - Already listening for
populate-chat-inputevents (ChatInput.tsx lines 1171-1188) - Event System - Two custom events implemented:
populate-chat-input- Pre-fills chat with document contextgoose-doc-assist- Signals document assistance request
API Methods Available
window.gooseEditors[docId] = {
insertText(text, position?),
replaceText(from, to, text),
appendText(text),
formatText(from, to, format),
getContent(),
getText(),
getSelection(),
clear()
}
What's Needed
1. Message Metadata Enhancement
Location: ui/desktop/src/components/ChatInput.tsx
Currently, when the user sends a message, we need to attach document context metadata if it came from a document editor.
Implementation:
// In ChatInput.tsx, add state to track document context
const [documentContext, setDocumentContext] = useState<{
docId: string;
content: string;
selection?: { from: number; to: number; text: string };
timestamp: number;
} | null>(null);
// Update the populate-chat-input listener (lines 1171-1188)
const handlePopulateChatInput = (event: CustomEvent) => {
const { message, docId, metadata } = event.detail;
setDisplayValue(message);
setValue(message);
// Store the document context
setDocumentContext({
docId,
content: metadata.content,
selection: metadata.selection,
timestamp: Date.now()
});
textAreaRef.current?.focus();
};
// In performSubmit function, include document context in message
const performSubmit = useCallback((text?: string) => {
// ... existing code ...
// Create message object with document context
const messageData = {
value: textToSend,
documentContext: documentContext ? {
docId: documentContext.docId,
content: documentContext.content,
selection: documentContext.selection,
timestamp: documentContext.timestamp
} : undefined
};
handleSubmit(
new CustomEvent('submit', { detail: messageData }) as unknown as React.FormEvent
);
// Clear document context after sending
setDocumentContext(null);
// ... rest of existing code ...
}, [documentContext, /* other deps */]);
2. Backend Message Handler
Location: Rust backend (likely crates/goose-server/ or crates/goose-cli/)
The backend needs to:
- Receive messages with optional
documentContextmetadata - Pass this context to the AI agent
- Provide a tool/function for the AI to call
window.gooseEditorsmethods
Conceptual Implementation (Rust):
// Message structure
struct Message {
content: String,
document_context: Option<DocumentContext>,
}
struct DocumentContext {
doc_id: String,
content: String,
selection: Option<Selection>,
timestamp: i64,
}
// Tool definition for AI
fn register_document_tools() {
// Tool: edit_document
// Description: Edit a collaborative document that the user is working on
// Parameters:
// - doc_id: string (required)
// - action: "insert" | "replace" | "append" | "format" | "clear"
// - text: string (for insert/replace/append)
// - position: number (optional, for insert)
// - from: number (optional, for replace/format)
// - to: number (optional, for replace/format)
// - format: object (optional, for format)
register_tool("edit_document", |params| {
// Execute JavaScript in the renderer process
execute_js_in_renderer(&format!(
"window.gooseEditors['{}'].{}({})",
params.doc_id,
params.action,
serialize_params(params)
))
});
}
3. AI Prompt Enhancement
When a message includes document context, the system prompt should be enhanced:
You are assisting the user with a document they are editing.
Document ID: {docId}
Current Content:
{content}
{if selection exists}
User has selected text from position {from} to {to}:
"{selectedText}"
{endif}
The user's request: {userMessage}
You can edit this document using the edit_document tool with these actions:
- insertText(text, position?) - Insert text at a position (or at cursor)
- replaceText(from, to, text) - Replace text in a range
- appendText(text) - Add text to the end
- formatText(from, to, format) - Apply formatting to a range
- clear() - Clear all content
Always explain what you're doing before making edits.
4. Renderer IPC Bridge
Location: ui/desktop/src/main/ (Electron main process)
Create an IPC channel for the backend to execute document edits:
// In main process
ipcMain.handle('execute-document-edit', async (event, docId, method, args) => {
// Send to renderer process
const window = BrowserWindow.getFocusedWindow();
if (window) {
return window.webContents.executeJavaScript(`
(function() {
const editor = window.gooseEditors['${docId}'];
if (editor && typeof editor.${method} === 'function') {
return editor.${method}(${JSON.stringify(args)});
}
return { error: 'Editor not found or method not available' };
})()
`);
}
});
5. Visual Feedback System
Already Implemented ✅
gooseIsTypingstate in CollaborativeDocEditor- Visual badge showing "Goose is editing..."
- Toggle to enable/disable Goose collaboration
Testing Strategy
Phase 5A: Message Metadata (Frontend Only)
- Open a document editor
- Click "Ask Goose" button
- Verify chat input is populated
- Open browser console
- Before sending, check that
documentContextstate is set - Send message
- Verify message includes document context in the submit event
Phase 5B: Backend Tool Registration
- Start Goose with document tool enabled
- Send a message: "Add a heading that says 'Hello World'"
- Backend should recognize document context
- AI should call
edit_documenttool - Verify tool execution reaches the IPC bridge
Phase 5C: End-to-End Integration
- Open document, type some content
- Select text
- Click "Ask Goose", ask: "Make this text bold"
- Goose should:
- Receive document context with selection
- Call
formatText(from, to, { bold: true }) - Update the document
- Show "Goose is editing..." indicator
Phase 5D: Multi-Turn Conversation
- Open document
- Ask Goose: "Write a short poem about coding"
- Goose inserts poem
- Ask: "Make the first line bold"
- Goose should remember document context and format correctly
- Ask: "Add a new stanza about debugging"
- Goose should append new content
Implementation Order
- ✅ Frontend API - Already complete
- ✅ Event System - Already complete
- ✅ Chat Input Listener - Already complete
- Message Metadata - Add document context to messages (Frontend)
- IPC Bridge - Create Electron IPC channel for document edits
- Backend Tool - Register
edit_documenttool in Rust backend - AI Integration - Enhance prompts with document context
- Testing - End-to-end testing with real documents
Next Steps
To continue Phase 5, we need to:
- Modify ChatInput.tsx to store and pass document context with messages
- Identify the correct Rust backend files to add the document editing tool
- Create the IPC bridge in Electron main process
- Test the integration with console commands first, then with AI
Files to Modify
Frontend
- ✅
ui/desktop/src/components/CollaborativeDocEditor.tsx- Complete - 🔄
ui/desktop/src/components/ChatInput.tsx- Add document context state and metadata - 🔄
ui/desktop/src/main/index.ts(or similar) - Add IPC handler for document edits
Backend (To be determined)
- 🔄
crates/goose-server/src/???- Add document editing tool - 🔄
crates/goose/src/???- Enhance message handling with document context
Questions to Resolve
-
Where is the message handling code in the Rust backend?
- Need to find the file that processes incoming chat messages
- Need to understand the tool/function calling system
-
How does the Electron app communicate with the Rust backend?
- Is it HTTP? IPC? WebSocket?
- Need to trace the path from
handleSubmitto backend
-
What is the tool registration system?
- How are tools/functions exposed to the AI?
- What's the format for tool definitions?
Success Criteria
- User can click "Ask Goose" in document editor
- Chat input is populated with document context
- User sends message with document-related request
- Goose receives document context in backend
- Goose can call document editing methods
- Document updates in real-time
- Visual feedback shows "Goose is editing..."
- Multi-turn conversations maintain document context
Documentation
Once complete, create:
GOOSE_DOCUMENT_INTEGRATION_GUIDE.md- For developersDOCUMENT_COLLABORATION_USER_GUIDE.md- For end users- Update
README_DOCEDITOR.mdwith collaboration features