# Phase 5: Backend Integration - Progress Report ## ๐ŸŽ‰ What We've Accomplished ### 1. Document Context Structures โœ… **File**: `crates/goose-server/src/routes/reply.rs` Added three new structs to handle document context: ```rust #[derive(Debug, Clone, Deserialize, Serialize)] #[serde(rename_all = "camelCase")] struct DocumentSelection { from: usize, to: usize, text: String, } #[derive(Debug, Clone, Deserialize, Serialize)] #[serde(rename_all = "camelCase")] struct DocumentContext { doc_id: String, content: String, selection: Option, timestamp: i64, } ``` ### 2. ChatRequest Enhancement โœ… Extended the `ChatRequest` struct to include optional document context: ```rust #[derive(Debug, Deserialize, Serialize)] #[serde(rename_all = "camelCase")] struct ChatRequest { messages: Vec, session_id: String, recipe_name: Option, recipe_version: Option, document_context: Option, // NEW! } ``` ### 3. Document Context Logging โœ… Added logging when document context is received: ```rust // Log document context if present if let Some(ref doc_ctx) = document_context { tracing::info!( doc_id = %doc_ctx.doc_id, content_length = doc_ctx.content.len(), has_selection = doc_ctx.selection.is_some(), "Document context received" ); } ``` ### 4. AI Prompt Enhancement โœ… Inject document context as an agent-only system message: ```rust // If document context is present, inject it as a system message if let Some(doc_ctx) = document_context.clone() { let mut context_message = format!( "You are assisting the user with a document they are editing.\n\n\ Document ID: {}\n\ Current Content:\n```\n{}\n```\n", doc_ctx.doc_id, doc_ctx.content ); if let Some(selection) = doc_ctx.selection { context_message.push_str(&format!( "\nUser has selected text from position {} to {}:\n\"{}\"\n", selection.from, selection.to, selection.text )); } context_message.push_str( "\nYou can edit this document using the edit_document tool with these actions:\n\ - insertText(text, position?) - Insert text at a position (or at cursor)\n\ - replaceText(from, to, text) - Replace text in a range\n\ - appendText(text) - Add text to the end\n\ - formatText(from, to, format) - Apply formatting to a range\n\ - clear() - Clear all content\n\n\ Always explain what you're doing before making edits." ); // Add as an agent-only system message let system_message = Message::user() .with_text(context_message) .agent_only(); messages.push(system_message); } ``` ## ๐Ÿ“Š Data Flow (Current State) ``` Frontend (Complete) โœ… โ†“ ChatInput dispatches message with documentContext โ†“ Axios POST to /reply endpoint โ†“ Backend (Partial) ๐Ÿ”„ โ†“ reply_handler receives ChatRequest โ†“ Parses documentContext โœ… โ†“ Logs document context โœ… โ†“ Injects as system message โœ… โ†“ AI receives enhanced prompt โœ… โ†“ AI calls edit_document tool โณ (TO BE IMPLEMENTED) โ†“ Tool handler โณ (TO BE IMPLEMENTED) โ†“ IPC to Electron โณ (TO BE IMPLEMENTED) โ†“ Execute window.gooseEditors[docId].method() โณ (TO BE IMPLEMENTED) โ†“ Document updates โณ (TO BE IMPLEMENTED) ``` ## ๐Ÿงช Testing the Current Implementation ### Test 1: Verify Backend Compiles ```bash cd /Users/spencermartin/Desktop/goose source bin/activate-hermit cargo check -p goose-server # Should compile successfully โœ… ``` ### Test 2: Verify Document Context is Received ```bash # 1. Start the backend cargo run --bin goose-server # 2. In the frontend: # - Open a document # - Click "Ask Goose" # - Send a message # 3. Check backend logs for: # "Document context received" with doc_id, content_length, has_selection ``` ### Test 3: Verify System Message Injection ```bash # The AI should now receive the document context in its prompt # You can verify this by checking the conversation messages # The system message should be agent_visible=true, user_visible=false ``` ## ๐Ÿ”œ What's Next ### Step 1: Create edit_document Tool We need to create a tool that the AI can call to edit documents. This tool should: 1. Accept parameters: `doc_id`, `action`, `text`, `position`, `from`, `to`, `format` 2. Validate the parameters 3. Call the IPC bridge to execute the edit **Location**: Need to find where tools are registered (likely in `crates/goose/src/agents/`) ### Step 2: Create IPC Bridge We need to create an IPC channel in Electron that: 1. Receives edit requests from the Rust backend 2. Executes JavaScript in the renderer process 3. Calls `window.gooseEditors[docId].method(...args)` 4. Returns the result to the backend **Location**: `ui/desktop/src/main/index.ts` or similar ### Step 3: Register the Tool The `edit_document` tool needs to be registered with the agent so the AI knows it's available. ### Step 4: End-to-End Testing Once all pieces are in place: 1. User opens document 2. User clicks "Ask Goose" 3. User sends: "Make this text bold" 4. AI receives document context 5. AI calls `edit_document` tool 6. Tool executes via IPC 7. Document updates in real-time 8. Visual feedback shows "Goose is editing..." ## ๐Ÿ“ Files Modified ### Backend โœ… - `crates/goose-server/src/routes/reply.rs` - Added `DocumentSelection` struct - Added `DocumentContext` struct - Modified `ChatRequest` to include `document_context` - Added document context logging - Added system message injection ### Frontend โœ… (from previous phases) - `ui/desktop/src/components/ChatInput.tsx` - Added `documentContext` state - Enhanced message submission with context ### To Be Modified ๐Ÿ”„ - Tool registration (Rust) - IPC bridge (Electron/TypeScript) - Tool implementation (Rust) ## ๐ŸŽฏ Success Criteria - [x] Backend can receive document context - [x] Backend logs document context - [x] AI receives enhanced prompt with document info - [ ] AI can call edit_document tool - [ ] Tool executes via IPC - [ ] Document updates in real-time - [ ] Visual feedback works - [ ] Multi-turn conversations maintain context ## ๐Ÿ’ก Key Insights 1. **Backward Compatibility**: The `document_context` field is optional, so existing messages work normally. 2. **Agent-Only Messages**: We use `.agent_only()` to inject the document context as a system message that's visible to the AI but not shown to the user in the UI. 3. **Serialization**: Using `#[serde(rename_all = "camelCase")]` ensures the Rust structs match the JavaScript camelCase naming convention. 4. **Logging**: Comprehensive logging helps debug the data flow and verify that document context is being received correctly. ## ๐Ÿš€ Next Developer Actions 1. **Find Tool Registration System** ```bash cd crates/goose rg "register.*tool" -A 5 ``` 2. **Create edit_document Tool** - Define tool schema - Implement tool handler - Register with agent 3. **Create IPC Bridge** - Add IPC handler in Electron main process - Execute JavaScript in renderer - Return results to backend 4. **Test Integration** - Verify tool is called - Verify IPC communication - Verify document updates ## ๐Ÿ“š Related Documentation - `PHASE_5_COMPLETE_SUMMARY.md` - Frontend implementation - `PHASE_5_BACKEND_INTEGRATION.md` - Original backend plan - `GOOSE_DOCUMENT_COLLABORATION.md` - API reference - `COMPLETE_FEATURE_SUMMARY.md` - Full feature overview ## ๐ŸŽŠ Current Status **Backend**: 50% Complete - โœ… Document context parsing - โœ… Logging - โœ… AI prompt enhancement - โณ Tool creation - โณ IPC bridge - โณ Tool registration **Overall Project**: 75% Complete - โœ… Frontend (100%) - ๐Ÿ”„ Backend (50%) The foundation is solid! We've successfully connected the frontend to the backend and enhanced the AI's prompt with document context. The next step is to create the tool that allows the AI to actually edit the document.