Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 302
feat: improve the event system to emit more data and conventionalize …#239
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
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -17,7 +17,7 @@ export abstract class ChatClientEventEmitter { | ||
| */ | ||
| protected abstract emitEvent( | ||
| eventName: string, | ||
| data?: Record<string, any>, | ||
| data?: Record<string, unknown>, | ||
| ): void | ||
| /** | ||
| @@ -33,14 +33,14 @@ export abstract class ChatClientEventEmitter { | ||
| * Emit loading state changed event | ||
| */ | ||
| loadingChanged(isLoading: boolean): void { | ||
| this.emitEvent('client:loading-changed', { isLoading }) | ||
| this.emitEvent('client:loading:changed', { isLoading }) | ||
| } | ||
| /** | ||
| * Emit error state changed event | ||
| */ | ||
| errorChanged(error: string | null): void { | ||
| this.emitEvent('client:error-changed', { | ||
| this.emitEvent('client:error:changed', { | ||
| error, | ||
| }) | ||
| } | ||
| @@ -49,12 +49,8 @@ export abstract class ChatClientEventEmitter { | ||
| * Emit text update events (combines processor and client events) | ||
| */ | ||
| textUpdated(streamId: string, messageId: string, content: string): void { | ||
| this.emitEvent('processor:text-updated', { | ||
| this.emitEvent('text:chunk:content', { | ||
| streamId, | ||
| content, | ||
| }) | ||
| this.emitEvent('client:assistant-message-updated', { | ||
| messageId, | ||
| content, | ||
| }) | ||
| @@ -71,15 +67,8 @@ export abstract class ChatClientEventEmitter { | ||
| state: string, | ||
| args: string, | ||
| ): void { | ||
| this.emitEvent('processor:tool-call-state-changed', { | ||
| this.emitEvent('tools:call:updated', { | ||
| streamId, | ||
| toolCallId, | ||
| toolName, | ||
| state, | ||
| arguments: args, | ||
| }) | ||
| this.emitEvent('client:tool-call-updated', { | ||
| messageId, | ||
| toolCallId, | ||
| toolName, | ||
| @@ -91,22 +80,6 @@ export abstract class ChatClientEventEmitter { | ||
| /** | ||
| * Emit tool result state change event | ||
| */ | ||
| toolResultStateChanged( | ||
| streamId: string, | ||
| toolCallId: string, | ||
| content: string, | ||
| state: string, | ||
| error?: string, | ||
| ): void { | ||
| this.emitEvent('processor:tool-result-state-changed', { | ||
| streamId, | ||
| toolCallId, | ||
| content, | ||
| state, | ||
| error, | ||
| }) | ||
| } | ||
| /** | ||
| * Emit thinking update event | ||
| */ | ||
| @@ -116,7 +89,7 @@ export abstract class ChatClientEventEmitter { | ||
| content: string, | ||
| delta?: string, | ||
| ): void { | ||
| this.emitEvent('stream:chunk:thinking', { | ||
| this.emitEvent('text:chunk:thinking', { | ||
| streamId, | ||
| messageId, | ||
| content, | ||
| @@ -128,13 +101,15 @@ export abstract class ChatClientEventEmitter { | ||
| * Emit approval requested event | ||
| */ | ||
| approvalRequested( | ||
| streamId: string, | ||
| messageId: string, | ||
| toolCallId: string, | ||
| toolName: string, | ||
| input: any, | ||
| input: unknown, | ||
| approvalId: string, | ||
| ): void { | ||
| this.emitEvent('client:approval-requested', { | ||
| this.emitEvent('tools:approval:requested', { | ||
| streamId, | ||
| messageId, | ||
| toolCallId, | ||
| toolName, | ||
| @@ -146,26 +121,34 @@ export abstract class ChatClientEventEmitter { | ||
| /** | ||
| * Emit message appended event | ||
| */ | ||
| messageAppended(uiMessage: UIMessage): void { | ||
| const contentPreview = uiMessage.parts | ||
| .filter((p) => p.type === 'text') | ||
| .map((p) => (p as any).content) | ||
| messageAppended(uiMessage: UIMessage, streamId?: string): void { | ||
| const content = uiMessage.parts | ||
| .filter((part) => part.type === 'text') | ||
| .map((part) => part.content) | ||
| .join(' ') | ||
| .substring(0, 100) | ||
| this.emitEvent('client:message-appended', { | ||
| this.emitEvent('text:message:created', { | ||
| streamId, | ||
| messageId: uiMessage.id, | ||
| role: uiMessage.role, | ||
| contentPreview, | ||
| content, | ||
| parts: uiMessage.parts, | ||
| }) | ||
| } | ||
| /** | ||
| * Emit message sent event | ||
| */ | ||
| messageSent(messageId: string, content: string): void { | ||
| this.emitEvent('client:message-sent', { | ||
| this.emitEvent('text:message:created', { | ||
| messageId, | ||
| role: 'user', | ||
| content, | ||
| }) | ||
| this.emitEvent('text:message:user', { | ||
| messageId, | ||
| role: 'user', | ||
| content, | ||
| }) | ||
| } | ||
| @@ -190,7 +173,7 @@ export abstract class ChatClientEventEmitter { | ||
| * Emit messages cleared event | ||
| */ | ||
| messagesCleared(): void { | ||
| this.emitEvent('client:messages-cleared') | ||
| this.emitEvent('client:messages:cleared') | ||
| } | ||
| /** | ||
| @@ -199,10 +182,10 @@ export abstract class ChatClientEventEmitter { | ||
| toolResultAdded( | ||
| toolCallId: string, | ||
| toolName: string, | ||
| output: any, | ||
| output: unknown, | ||
| state: string, | ||
| ): void { | ||
| this.emitEvent('tool:result-added', { | ||
| this.emitEvent('tools:result:added', { | ||
| toolCallId, | ||
| toolName, | ||
| output, | ||
| @@ -218,7 +201,7 @@ export abstract class ChatClientEventEmitter { | ||
| toolCallId: string, | ||
| approved: boolean, | ||
| ): void { | ||
| this.emitEvent('tool:approval-responded', { | ||
| this.emitEvent('tools:approval:responded', { | ||
| approvalId, | ||
| toolCallId, | ||
| approved, | ||
| @@ -235,14 +218,19 @@ export class DefaultChatClientEventEmitter extends ChatClientEventEmitter { | ||
| */ | ||
| protected emitEvent(eventName: string, data?: Record<string, any>): void { | ||
AlemTuzlak marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| // For client:* and tool:* events, automatically add clientId and timestamp | ||
| if (eventName.startsWith('client:') || eventName.startsWith('tool:')) { | ||
| if ( | ||
| eventName.startsWith('client:') || | ||
| eventName.startsWith('tools:') || | ||
| eventName.startsWith('text:') | ||
| ) { | ||
| aiEventClient.emit(eventName as any, { | ||
| ...data, | ||
| clientId: this.clientId, | ||
| source: 'client', | ||
| timestamp: Date.now(), | ||
| }) | ||
| } else { | ||
| // For other events (e.g., processor:*), just add timestamp | ||
| // For other events, just add timestamp | ||
| aiEventClient.emit(eventName as any, { | ||
| ...data, | ||
| timestamp: Date.now(), | ||
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.