-
Notifications
You must be signed in to change notification settings - Fork 1
Implement Structured Event System with SOLID Principles for Browser.AI #13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Copilot
wants to merge
7
commits into
feat/browser-extention
Choose a base branch
from
copilot/create-event-emission-module
base: feat/browser-extention
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
ad5a0b4
Initial plan
Copilot 6db9ee1
Implement structured event system with SOLID principles
Copilot 49c95a8
Add demo and documentation for structured event system
Copilot f6c003f
Add TypeScript definitions and frontend integration guide
Copilot 909328d
Add implementation summary and finalize structured event system
Copilot 0e57432
Add events module README and finalize documentation
Copilot c9b6c6c
Merge branch 'feat/browser-extention' into copilot/create-event-emiss…
Sathursan-S File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
313 changes: 313 additions & 0 deletions
313
browser_ai_extension/browse_ai/src/types/structured-events.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,313 @@ | ||
| /** | ||
| * Structured Event System - TypeScript Definitions | ||
| * | ||
| * Type definitions for the new structured event system. | ||
| * These match the Python event schemas in browser_ai_gui/events/schemas.py | ||
| */ | ||
|
|
||
| // ============================================================================ | ||
| // Enums | ||
| // ============================================================================ | ||
|
|
||
| export enum EventCategory { | ||
| AGENT = 'agent', | ||
| TASK = 'task', | ||
| LLM = 'llm', | ||
| BROWSER = 'browser', | ||
| PROGRESS = 'progress', | ||
| SYSTEM = 'system', | ||
| } | ||
|
|
||
| export enum EventSeverity { | ||
| DEBUG = 'debug', | ||
| INFO = 'info', | ||
| WARNING = 'warning', | ||
| ERROR = 'error', | ||
| CRITICAL = 'critical', | ||
| } | ||
|
|
||
| // ============================================================================ | ||
| // Base Event | ||
| // ============================================================================ | ||
|
|
||
| export interface BaseEvent { | ||
| event_id: string | ||
| event_type: string | ||
| category: EventCategory | ||
| timestamp: string // ISO 8601 format | ||
| severity: EventSeverity | ||
| session_id?: string | ||
| task_id?: string | ||
| metadata?: Record<string, any> | ||
| } | ||
|
|
||
| // ============================================================================ | ||
| // Agent Events | ||
| // ============================================================================ | ||
|
|
||
| export interface AgentStartEvent extends BaseEvent { | ||
| event_type: 'agent.start' | ||
| category: EventCategory.AGENT | ||
| task_description: string | ||
| agent_id: string | ||
| configuration: Record<string, any> | ||
| } | ||
|
|
||
| export interface AgentStepEvent extends BaseEvent { | ||
| event_type: 'agent.step' | ||
| category: EventCategory.AGENT | ||
| step_number: number | ||
| step_description: string | ||
| agent_id: string | ||
| total_steps?: number | ||
| } | ||
|
|
||
| export interface AgentActionEvent extends BaseEvent { | ||
| event_type: 'agent.action' | ||
| category: EventCategory.AGENT | ||
| action_type: string | ||
| action_description: string | ||
| agent_id: string | ||
| action_parameters: Record<string, any> | ||
| action_result?: string | ||
| } | ||
|
|
||
| export interface AgentProgressEvent extends BaseEvent { | ||
| event_type: 'agent.progress' | ||
| category: EventCategory.PROGRESS | ||
| agent_id: string | ||
| progress_percentage: number // 0.0 to 100.0 | ||
| current_step: number | ||
| total_steps?: number | ||
| status_message?: string | ||
| } | ||
|
|
||
| export interface AgentStateEvent extends BaseEvent { | ||
| event_type: 'agent.state_change' | ||
| category: EventCategory.AGENT | ||
| agent_id: string | ||
| old_state: string | ||
| new_state: string | ||
| state_data: Record<string, any> | ||
| } | ||
|
|
||
| export interface AgentCompleteEvent extends BaseEvent { | ||
| event_type: 'agent.complete' | ||
| category: EventCategory.AGENT | ||
| agent_id: string | ||
| success: boolean | ||
| result?: string | ||
| execution_time_ms?: number | ||
| steps_executed?: number | ||
| } | ||
|
|
||
| export interface AgentErrorEvent extends BaseEvent { | ||
| event_type: 'agent.error' | ||
| category: EventCategory.AGENT | ||
| severity: EventSeverity.ERROR | ||
| agent_id: string | ||
| error_type: string | ||
| error_message: string | ||
| error_details?: string | ||
| recoverable: boolean | ||
| } | ||
|
|
||
| // ============================================================================ | ||
| // LLM Events | ||
| // ============================================================================ | ||
|
|
||
| export interface LLMOutputEvent extends BaseEvent { | ||
| event_type: 'llm.output' | ||
| category: EventCategory.LLM | ||
| agent_id: string | ||
| llm_provider: string | ||
| prompt_tokens?: number | ||
| completion_tokens?: number | ||
| total_tokens?: number | ||
| model_name?: string | ||
| response_preview?: string // First 200 chars | ||
| latency_ms?: number | ||
| } | ||
|
|
||
| // ============================================================================ | ||
| // Task Events | ||
| // ============================================================================ | ||
|
|
||
| export interface TaskStateChangeEvent extends BaseEvent { | ||
| event_type: 'task.state_change' | ||
| category: EventCategory.TASK | ||
| task_description: string | ||
| old_state: string | ||
| new_state: string | ||
| agent_id?: string | ||
| } | ||
|
|
||
| // ============================================================================ | ||
| // Union Type for All Events | ||
| // ============================================================================ | ||
|
|
||
| export type StructuredEvent = | ||
| | AgentStartEvent | ||
| | AgentStepEvent | ||
| | AgentActionEvent | ||
| | AgentProgressEvent | ||
| | AgentStateEvent | ||
| | AgentCompleteEvent | ||
| | AgentErrorEvent | ||
| | LLMOutputEvent | ||
| | TaskStateChangeEvent | ||
|
|
||
| // ============================================================================ | ||
| // Event Handlers | ||
| // ============================================================================ | ||
|
|
||
| export type EventHandler<T extends StructuredEvent = StructuredEvent> = (event: T) => void | ||
|
|
||
| export interface EventSubscription { | ||
| id: string | ||
| handler: EventHandler | ||
| filter?: string // Event type filter | ||
| categoryFilter?: EventCategory // Category filter | ||
| } | ||
|
|
||
| // ============================================================================ | ||
| // Type Guards | ||
| // ============================================================================ | ||
|
|
||
| export function isStructuredEvent(obj: any): obj is StructuredEvent { | ||
| return ( | ||
| obj && | ||
| typeof obj.event_id === 'string' && | ||
| typeof obj.event_type === 'string' && | ||
| typeof obj.category === 'string' && | ||
| typeof obj.timestamp === 'string' && | ||
| typeof obj.severity === 'string' | ||
| ) | ||
| } | ||
|
|
||
| export function isAgentStartEvent(event: StructuredEvent): event is AgentStartEvent { | ||
| return event.event_type === 'agent.start' | ||
| } | ||
|
|
||
| export function isAgentStepEvent(event: StructuredEvent): event is AgentStepEvent { | ||
| return event.event_type === 'agent.step' | ||
| } | ||
|
|
||
| export function isAgentActionEvent(event: StructuredEvent): event is AgentActionEvent { | ||
| return event.event_type === 'agent.action' | ||
| } | ||
|
|
||
| export function isAgentProgressEvent(event: StructuredEvent): event is AgentProgressEvent { | ||
| return event.event_type === 'agent.progress' | ||
| } | ||
|
|
||
| export function isAgentCompleteEvent(event: StructuredEvent): event is AgentCompleteEvent { | ||
| return event.event_type === 'agent.complete' | ||
| } | ||
|
|
||
| export function isAgentErrorEvent(event: StructuredEvent): event is AgentErrorEvent { | ||
| return event.event_type === 'agent.error' | ||
| } | ||
|
|
||
| export function isLLMOutputEvent(event: StructuredEvent): event is LLMOutputEvent { | ||
| return event.event_type === 'llm.output' | ||
| } | ||
|
|
||
| export function isTaskStateChangeEvent(event: StructuredEvent): event is TaskStateChangeEvent { | ||
| return event.event_type === 'task.state_change' | ||
| } | ||
|
|
||
| // ============================================================================ | ||
| // Event Filtering Helpers | ||
| // ============================================================================ | ||
|
|
||
| export function filterByCategory(events: StructuredEvent[], category: EventCategory): StructuredEvent[] { | ||
| return events.filter(event => event.category === category) | ||
| } | ||
|
|
||
| export function filterByType(events: StructuredEvent[], eventType: string): StructuredEvent[] { | ||
| return events.filter(event => event.event_type === eventType) | ||
| } | ||
|
|
||
| export function filterBySeverity(events: StructuredEvent[], severity: EventSeverity): StructuredEvent[] { | ||
| return events.filter(event => event.severity === severity) | ||
| } | ||
|
|
||
| export function filterByAgentId(events: StructuredEvent[], agentId: string): StructuredEvent[] { | ||
| return events.filter(event => { | ||
| return 'agent_id' in event && event.agent_id === agentId | ||
| }) | ||
| } | ||
|
|
||
| // ============================================================================ | ||
| // Event Formatting Helpers | ||
| // ============================================================================ | ||
|
|
||
| export function formatEventTimestamp(event: StructuredEvent): string { | ||
| return new Date(event.timestamp).toLocaleString() | ||
| } | ||
|
|
||
| export function getEventIcon(event: StructuredEvent): string { | ||
| if (isAgentStartEvent(event)) return '🚀' | ||
| if (isAgentStepEvent(event)) return '📍' | ||
| if (isAgentActionEvent(event)) return '🔧' | ||
| if (isAgentProgressEvent(event)) return '📊' | ||
| if (isAgentCompleteEvent(event)) return event.success ? '✅' : '⚠️' | ||
| if (isAgentErrorEvent(event)) return '❌' | ||
| if (isLLMOutputEvent(event)) return '🤖' | ||
| if (isTaskStateChangeEvent(event)) return '🔄' | ||
| return '📡' | ||
| } | ||
|
|
||
| export function getSeverityColor(severity: EventSeverity): string { | ||
| switch (severity) { | ||
| case EventSeverity.DEBUG: | ||
| return '#888888' | ||
| case EventSeverity.INFO: | ||
| return '#0066cc' | ||
| case EventSeverity.WARNING: | ||
| return '#ff9900' | ||
| case EventSeverity.ERROR: | ||
| return '#cc0000' | ||
| case EventSeverity.CRITICAL: | ||
| return '#990000' | ||
| default: | ||
| return '#000000' | ||
| } | ||
| } | ||
|
|
||
| // ============================================================================ | ||
| // Example Usage | ||
| // ============================================================================ | ||
|
|
||
| /* | ||
| // Subscribe to structured events | ||
| socket.on('structured_event', (event: StructuredEvent) => { | ||
| console.log(`Event: ${event.event_type}`) | ||
|
|
||
| if (isAgentProgressEvent(event)) { | ||
| updateProgressBar(event.progress_percentage) | ||
| } | ||
|
|
||
| if (isAgentCompleteEvent(event)) { | ||
| if (event.success) { | ||
| showNotification('Task completed successfully!') | ||
| } | ||
| } | ||
|
|
||
| if (isAgentErrorEvent(event)) { | ||
| console.error(`Error: ${event.error_message}`) | ||
| } | ||
| }) | ||
|
|
||
| // Filter events by category | ||
| const agentEvents = filterByCategory(allEvents, EventCategory.AGENT) | ||
|
|
||
| // Filter events by agent ID | ||
| const myAgentEvents = filterByAgentId(allEvents, 'agent-123') | ||
|
|
||
| // Display with icons | ||
| events.forEach(event => { | ||
| console.log(`${getEventIcon(event)} ${event.event_type}`) | ||
| }) | ||
| */ |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion | 🟠 Major
Use the StructuredEvent type instead of
anyfor type safety.Line 211 defines
structured_eventwith ananytype, which defeats the purpose of the type-safe structured event system. TheStructuredEventtype is defined instructured-events.tsand should be imported and used here.Apply this diff:
🤖 Prompt for AI Agents