improvement(copilot): improve context inputs and fix some bugs#1216
improvement(copilot): improve context inputs and fix some bugs#1216
Conversation
There was a problem hiding this comment.
Greptile Summary
This PR significantly enhances the Copilot system by introducing a comprehensive context system and improving user experience. The changes implement four new context types: current_workflow (auto-added for workflow awareness), workflow_block (specific block references), logs (execution history), and docs (documentation search). The system now automatically includes current workflow context in conversations while allowing per-chat disabling, and supports @mention syntax for referencing different context types.
Key improvements include: changing the default agent depth from 0 to 1 to enable more conversational interactions with tool calling capabilities; implementing optimistic background message saving when switching between chats to prevent data loss; adding sophisticated message sanitization for documentation searches; and enhanced error handling with user-friendly messages. The handleNewChat function was optimized to provide immediate UI feedback by removing blocking waits on data loading operations.
The implementation spans across multiple layers - from frontend type definitions and UI components to backend API processing and database queries. The system maintains backward compatibility while adding granular context awareness that allows the Copilot to provide more targeted assistance based on the user's current working context.
Confidence score: 4/5
- This PR introduces significant new functionality with generally solid implementation patterns, though the complexity increases risk
- Score reflects comprehensive context system additions with proper error handling, but some complex state management logic could benefit from additional testing
- Pay close attention to the user-input component and context processing logic due to their complexity
9 files reviewed, 7 comments
|
|
||
| const results = await Promise.all(tasks) | ||
| return results.filter((r): r is AgentContext => !!r) | ||
| return results.filter((r): r is AgentContext => !!r) as AgentContext[] |
There was a problem hiding this comment.
style: Redundant type assertion - the filter already ensures the correct type
| return results.filter((r): r is AgentContext => !!r) as AgentContext[] | |
| return results.filter((r): r is AgentContext => !!r) |
| kind: c?.kind, | ||
| chatId: c?.chatId, | ||
| workflowId: c?.workflowId, | ||
| executionId: (c as any)?.executionId, |
There was a problem hiding this comment.
style: Using (c as any)?.executionId bypasses TypeScript safety. Consider updating the ChatContext type to include executionId properly.
Context Used: Context - Avoid using type assertions to 'any' in TypeScript. Instead, ensure proper type definitions are used to maintain type safety. (link)
| // Clear @mention contexts after submission, but preserve current_workflow if not disabled | ||
| setSelectedContexts((prev) => { | ||
| // Keep current_workflow context if it's not disabled | ||
| const currentWorkflowCtx = prev.find( | ||
| (ctx) => ctx.kind === 'current_workflow' && !workflowAutoAddDisabled | ||
| ) | ||
| return currentWorkflowCtx ? [currentWorkflowCtx] : [] | ||
| }) |
There was a problem hiding this comment.
style: The context preservation logic after submission has complex conditional behavior. Consider extracting this into a separate function for better testability and clarity.
Context Used: Context - If a switch statement is large and handles multiple cases, extract each case into separate functions for better maintainability. (link)
| setSelectedContexts((prev) => { | ||
| // Keep contexts that are mentioned in text OR are current_workflow (unless disabled) | ||
| const filteredContexts = prev.filter((c) => { | ||
| // Always preserve current_workflow context if it's not disabled | ||
| // It should only be removable via the X button | ||
| if (c.kind === 'current_workflow' && !workflowAutoAddDisabled) { | ||
| return true | ||
| } | ||
| // For other contexts, check if they're mentioned in text | ||
| return !!c.label && presentLabels.has(c.label!) | ||
| }) | ||
|
|
||
| return filteredContexts | ||
| }) |
There was a problem hiding this comment.
style: This context filtering logic is duplicated in multiple places (lines 690-697, 1670-1675). Consider extracting into a utility function to reduce duplication.
| } | ||
|
|
||
| addWorkflowContext() | ||
| }, [workflowId, workflowAutoAddDisabled, workflows.length, message, justSentMessage]) // Re-run when message changes |
There was a problem hiding this comment.
style: The dependency array includes workflows.length which could cause unnecessary re-runs. Consider using workflows directly or implementing a more stable dependency.
| (result as any).execution?.error || (result as any).execution?.output?.error | ||
| } | ||
| } | ||
| } catch {} |
There was a problem hiding this comment.
style: Empty catch block silently swallows all errors during result inspection, which could hide important debugging information
| if (result && typeof result === 'object' && 'success' in (result as any)) { | ||
| succeeded = Boolean((result as any).success) | ||
| if (!succeeded) { | ||
| errorMessage = (result as any)?.error || (result as any)?.output?.error | ||
| } | ||
| } else if ( | ||
| result && | ||
| typeof result === 'object' && | ||
| 'execution' in (result as any) && | ||
| (result as any).execution && | ||
| typeof (result as any).execution === 'object' | ||
| ) { | ||
| succeeded = Boolean((result as any).execution.success) | ||
| if (!succeeded) { | ||
| errorMessage = | ||
| (result as any).execution?.error || (result as any).execution?.output?.error | ||
| } | ||
| } |
There was a problem hiding this comment.
style: Multiple type assertions to any bypass TypeScript's type safety. Consider defining proper interfaces for the expected result structures
| if (result && typeof result === 'object' && 'success' in (result as any)) { | |
| succeeded = Boolean((result as any).success) | |
| if (!succeeded) { | |
| errorMessage = (result as any)?.error || (result as any)?.output?.error | |
| } | |
| } else if ( | |
| result && | |
| typeof result === 'object' && | |
| 'execution' in (result as any) && | |
| (result as any).execution && | |
| typeof (result as any).execution === 'object' | |
| ) { | |
| succeeded = Boolean((result as any).execution.success) | |
| if (!succeeded) { | |
| errorMessage = | |
| (result as any).execution?.error || (result as any).execution?.output?.error | |
| } | |
| } | |
| interface WorkflowResult { | |
| success: boolean; | |
| error?: string; | |
| output?: { error?: string }; | |
| } | |
| interface StreamingResult { | |
| execution: WorkflowResult; | |
| } | |
| if (result && typeof result === 'object' && 'success' in result) { | |
| const workflowResult = result as WorkflowResult | |
| succeeded = Boolean(workflowResult.success) | |
| if (!succeeded) { | |
| errorMessage = workflowResult.error || workflowResult.output?.error | |
| } | |
| } else if ( | |
| result && | |
| typeof result === 'object' && | |
| 'execution' in result && | |
| result.execution && | |
| typeof result.execution === 'object' | |
| ) { | |
| const streamingResult = result as StreamingResult | |
| succeeded = Boolean(streamingResult.execution.success) | |
| if (!succeeded) { | |
| errorMessage = | |
| streamingResult.execution.error || streamingResult.execution.output?.error | |
| } | |
| } |
Context Used: Context - Avoid using type assertions to 'any' in TypeScript. Instead, ensure proper type definitions are used to maintain type safety. (link)
* Add logs v1 * Update * Updates * Updates * Fixes * Fix current workflow in context * Fix mentions * Error handling * Fix chat loading * Hide current workflow from context * Run workflow fix * Lint
…ode surface area and removed redundant code (#1217) * improvement(invitations): consolidate invite-error and invite pages, made API endpoints more restful and reduced code surface area for invitations by 50% * refactored logs API routes * refactor rate limit api route, consolidate usage check api endpoint * refactored chat page and invitations page * consolidate ollama and openrouter stores to just providers store * removed unused route * removed legacy envvar methods * remove dead, legacy routes for invitations PUT and workflow SYNC * improvement(copilot): improve context inputs and fix some bugs (#1216) * Add logs v1 * Update * Updates * Updates * Fixes * Fix current workflow in context * Fix mentions * Error handling * Fix chat loading * Hide current workflow from context * Run workflow fix * Lint * updated invitation log * styling for invitation pages --------- Co-authored-by: Siddharth Ganesan <33737564+Sg312@users.noreply.github.com>
* Add logs v1 * Update * Updates * Updates * Fixes * Fix current workflow in context * Fix mentions * Error handling * Fix chat loading * Hide current workflow from context * Run workflow fix * Lint
…ode surface area and removed redundant code (#1217) * improvement(invitations): consolidate invite-error and invite pages, made API endpoints more restful and reduced code surface area for invitations by 50% * refactored logs API routes * refactor rate limit api route, consolidate usage check api endpoint * refactored chat page and invitations page * consolidate ollama and openrouter stores to just providers store * removed unused route * removed legacy envvar methods * remove dead, legacy routes for invitations PUT and workflow SYNC * improvement(copilot): improve context inputs and fix some bugs (#1216) * Add logs v1 * Update * Updates * Updates * Fixes * Fix current workflow in context * Fix mentions * Error handling * Fix chat loading * Hide current workflow from context * Run workflow fix * Lint * updated invitation log * styling for invitation pages --------- Co-authored-by: Siddharth Ganesan <33737564+Sg312@users.noreply.github.com>
…udioai#1216) * Add logs v1 * Update * Updates * Updates * Fixes * Fix current workflow in context * Fix mentions * Error handling * Fix chat loading * Hide current workflow from context * Run workflow fix * Lint
…ode surface area and removed redundant code (simstudioai#1217) * improvement(invitations): consolidate invite-error and invite pages, made API endpoints more restful and reduced code surface area for invitations by 50% * refactored logs API routes * refactor rate limit api route, consolidate usage check api endpoint * refactored chat page and invitations page * consolidate ollama and openrouter stores to just providers store * removed unused route * removed legacy envvar methods * remove dead, legacy routes for invitations PUT and workflow SYNC * improvement(copilot): improve context inputs and fix some bugs (simstudioai#1216) * Add logs v1 * Update * Updates * Updates * Fixes * Fix current workflow in context * Fix mentions * Error handling * Fix chat loading * Hide current workflow from context * Run workflow fix * Lint * updated invitation log * styling for invitation pages --------- Co-authored-by: Siddharth Ganesan <33737564+Sg312@users.noreply.github.com>
Summary
Fixes #(issue)
Type of Change
Testing
Manual
Checklist