Skip to content

SmythOS Studio: Open-Source Visual AI Agent Builder and deployable runtime stack from SmythOS. Start with an intuitive drag-and-drop workspace, extend with custom code, and deploy your agents anywhere — local, cloud, or edge — with full governance and control.

Refactor Chat Types for Improved Clarity and Consistency

Salman-Ahamed08d3e8500cd5
diff --git a/packages/app/src/react/features/ai-chat/types/chat.types.ts b/packages/app/src/react/features/ai-chat/types/chat.types.ts
index 5a06fce0..941e73bc 100644
--- a/packages/app/src/react/features/ai-chat/types/chat.types.ts
+++ b/packages/app/src/react/features/ai-chat/types/chat.types.ts
@@ -1,12 +1,3 @@
-/**
- * Professional Chat Type System
- *
- * Clean, minimal, and reusable type definitions for the chat system.
- * Follows DRY principles - no redundant properties.
- *
- * @module chat.types
- */
-
// ============================================================================
// CORE MESSAGE TYPES
// ============================================================================
@@ -33,110 +24,35 @@ export type TMessageType = 'user' | 'system' | 'thinking' | 'loading' | 'error';
* - Optional fields for specific use cases
*/
export interface IChatMessage {
- /** Unique identifier */
- id?: string | number;
-
- /** Message content */
- message: string;
-
- /**
- * Message type - single source of truth for state
- * Replaces: isReplying, isRetrying, isError, me booleans
- */
- type: TMessageType;
-
- // ============================================================================
- // CONVERSATION TRACKING
- // ============================================================================
-
- /**
- * Conversation Turn ID
- * Groups all messages (thinking, function calls, responses) related to
- * a single user query. All AI responses for one user message share the same turnId.
- *
- * @example
- * ```
- * User: "What's the weather?" → turnId: "turn_001"
- * AI: "Thinking..." → turnId: "turn_001" (same)
- * AI: "Using skill: weather" → turnId: "turn_001" (same)
- * AI: "It's sunny!" → turnId: "turn_001" (same)
- *
- * User: "Thanks!" → turnId: "turn_002" (new)
- * AI: "You're welcome!" → turnId: "turn_002" (same)
- * ```
- */
- conversationTurnId?: string;
-
- /**
- * Message ID (future)
- * Unique identifier from backend for this specific message
- * Will be available after SDK integration
- *
- * @example "msg_abc123-def456-ghi789"
- */
- messageId?: string;
-
- // ============================================================================
- // USER MESSAGE PROPERTIES
- // ============================================================================
-
- /** Attached files (user messages only) */
- files?: IMessageFile[];
-
- // ============================================================================
- // SYSTEM MESSAGE PROPERTIES
- // ============================================================================
-
- /** Avatar URL for system/AI messages */
- avatar?: string;
-
- /** Inline thinking/status message during generation */
- thinkingMessage?: string;
-
- // ============================================================================
- // INTERACTIVE PROPERTIES
- // ============================================================================
-
- /** Retry callback for error messages */
- onRetryClick?: () => void;
-
- /** Message timestamp (optional, for future use) */
- timestamp?: number;
+ id?: string | number; // Unique identifier
+ message: string; // Message content
+ type: TMessageType; // Message type - single source of truth for state
+ conversationTurnId?: string; // Conversation Turn ID
+ messageId?: string; // Message ID (future)
+ files?: IMessageFile[]; // Attached files (user messages only)
+ avatar?: string; // Avatar URL for system/AI messages
+ thinkingMessage?: string; // Inline thinking/status message during generation
+ onRetryClick?: () => void; // Retry callback for error messages
+ timestamp?: number; // Message timestamp (optional, for future use)
}
/**
* File attachment structure
* Simplified from previous complex nested structure
*/
-export interface IMessageFile {
- /** The actual File object */
- file: File;
- /** File metadata */
+export interface IMessageFile {
+ id: string; // Required ID for React keys and tracking
+ file: File; // The actual File object
metadata: {
- /** Public URL after upload */
publicUrl?: string;
- /** MIME type */
fileType?: string;
- /** Unique key for tracking */
key?: string;
- /** Upload in progress indicator */
isUploading?: boolean;
- /** Preview URL for images */
previewUrl?: string;
};
-
- /** Required ID for React keys and tracking */
- id: string;
}
-/**
- * Type alias for backward compatibility
- * Same as IMessageFile - fully compatible
- * @deprecated Use IMessageFile instead for new code
- */
-export type FileWithMetadata = IMessageFile;
-
// ============================================================================
// STREAMING TYPES
// ============================================================================
@@ -146,98 +62,35 @@ export type FileWithMetadata = IMessageFile;
* Only includes actually used properties
*/
export interface IStreamChunk {
- /**
- * Conversation Turn ID
- * Groups all AI responses (thinking, function calls, content) for a single user message
- * All responses in one conversation turn share the same turnId
- *
- * @example "turn_1735123456789_abc123xyz"
- */
- conversationTurnId?: string;
-
- /**
- * Message ID (future)
- * Unique identifier from backend for this specific message
- * Will be available after SDK integration
- *
- * @example "msg_abc123-def456-ghi789"
- */
- messageId?: string;
-
- /** Content chunk for streaming responses */
- content?: string;
-
- /** Status/thinking message */
- status_message?: string;
-
- /** Function/tool name */
- function?: string;
-
- /** Function call details */
- function_call?: {
- name?: string;
- arguments?: Record<string, unknown>;
- };
-
- /** Debug information */
- debug?: string;
-
- /** Debug tracking hash */
- hashId?: string;
-
- /** Debug session indicator */
- debugOn?: boolean;
-
- /** Error message */
- error?: string;
-
- /** Error indicator */
- isError?: boolean;
+ conversationTurnId?: string; // Conversation Turn ID
+ messageId?: string; // Message ID (future)
+ content?: string; // Content chunk for streaming responses
+ status_message?: string; // Status/thinking message
+ function?: string; // Function/tool name
+ function_call?: { name?: string; arguments?: Record<string, unknown> };
+ debug?: string; // Debug information
+ hashId?: string; // Debug tracking hash
+ debugOn?: boolean; // Debug session indicator
+ error?: string; // Error message
+ isError?: boolean; // Error indicator
}
-/**
- * Thinking message type discriminator
- */
-export type TThinkingType = 'general' | 'function' | 'status';
+export type TThinkingType = 'general' | 'function' | 'status'; // Thinking message type discriminator
-/**
- * Stream configuration for API calls
- */
export interface IStreamConfig {
- /** Target agent ID */
- agentId: string;
-
- /** Conversation ID */
- chatId: string;
-
- /** User message content */
- message: string;
-
- /** File attachments */
- attachments?: IFileAttachment[];
-
- /** Abort signal for cancellation */
- signal: AbortSignal;
-
- /** Custom headers */
- headers?: Record<string, string>;
+ agentId: string; // Target agent ID
+ chatId: string; // Conversation ID
+ message: string; // User message content
+ attachments?: IFileAttachment[]; // File attachments
+ signal: AbortSignal; // Abort signal for cancellation
+ headers?: Record<string, string>; // Custom headers
}
-/**
- * File attachment for API
- */
export interface IFileAttachment {
- /** Public URL */
- url: string;
-
- /** Filename */
- name?: string;
-
- /** MIME type */
- type?: string;
-
- /** File size */
- size?: number;
+ url: string; // Public URL
+ name?: string; // Filename
+ type?: string; // MIME type
+ size?: number; // File size
}
/**
@@ -245,93 +98,37 @@ export interface IFileAttachment {
*/
/* eslint-disable no-unused-vars */
export interface IStreamCallbacks {
- /** Content chunk received - now includes optional conversationTurnId */
onContent: (content: string, conversationTurnId?: string) => void;
-
- /** Thinking/status update - now includes optional conversationTurnId */
onThinking?: (message: string, type: TThinkingType, conversationTurnId?: string) => void;
-
- /** Tool/function call detected - now includes optional conversationTurnId */
onToolCall?: (
toolName: string,
args: Record<string, unknown>,
conversationTurnId?: string,
) => void;
-
- /** Debug information received */
- onDebug?: (debug: IStreamChunk) => void;
-
- /** Error occurred - error object now includes conversationTurnId */
- onError: (error: IChatError) => void;
-
- /** Stream started */
- onStart?: () => void;
-
- /** Stream completed */
- onComplete: () => void;
+ onDebug?: (debug: IStreamChunk) => void; // Debug information received
+ onError: (error: IChatError) => void; // Error occurred - error object now includes conversationTurnId
+ onStart?: () => void; // Stream started
+ onComplete: () => void; // Stream completed
}
-/* eslint-enable no-unused-vars */
// ============================================================================
// ERROR TYPES
// ============================================================================
-/**
- * Error type classification
- */
export type TErrorType = 'stream' | 'network' | 'abort' | 'system';
-/**
- * Chat error structure
- */
export interface IChatError {
- /** Error message */
- message: string;
-
- /** Error type */
- type: TErrorType;
-
- /** Conversation turn ID for grouping */
- conversationTurnId?: string;
-
- /** Original error object */
- originalError?: Error | unknown;
-
- /** User-initiated abort flag */
- isAborted?: boolean;
+ message: string; // Error message
+ type: TErrorType; // Error type
+ conversationTurnId?: string; // Conversation turn ID for grouping
+ originalError?: Error | unknown; // Original error object
+ isAborted?: boolean; // User-initiated abort flag
}
// ============================================================================
// HOOK INTERFACES
// ============================================================================
-/**
- * Chat hook configuration
- */
-/* eslint-disable no-unused-vars */
-export interface IUseChatConfig {
- /** Agent ID */
- agentId: string;
-
- /** Chat/Conversation ID */
- chatId: string;
-
- /** Avatar URL for AI messages */
- avatar?: string;
-
- /** Custom API client */
- client?: unknown; // Avoid circular dependency
-
- /** Custom headers */
- headers?: Record<string, string>;
-
- /** Chat completion callback */
- onChatComplete?: (message: string) => void;
-
- /** Error callback */
- onError?: (error: Error) => void;
-}
-
/**
* Chat hook return type
*/
@@ -349,7 +146,6 @@ export interface IUseChatReturn {
clearMessages: () => void;
clearError: () => void;
}
-/* eslint-enable no-unused-vars */
// ============================================================================
// API CLIENT TYPES
@@ -359,86 +155,8 @@ export interface IUseChatReturn {
* API client configuration
*/
export interface IAPIConfig {
- /** Base API URL */
- baseUrl?: string;
-
- /** Default headers */
- defaultHeaders?: Record<string, string>;
-
- /** Request timeout */
- timeout?: number;
-
- /** Retry configuration */
- retry?: {
- attempts: number;
- delay: number;
- };
+ baseUrl?: string; // Base API URL
+ defaultHeaders?: Record<string, string>; // Default headers
+ timeout?: number; // Request timeout
+ retry?: { attempts: number; delay: number }; // Retry configuration
}
-
-// ============================================================================
-// UTILITY TYPES
-// ============================================================================
-
-/**
- * Extract user messages
- */
-export type TUserMessage = IChatMessage & { type: 'user' };
-
-/**
- * Extract system messages
- */
-export type TSystemMessage = IChatMessage & { type: 'system' };
-
-/**
- * Extract thinking messages
- */
-export type TThinkingMessage = IChatMessage & { type: 'thinking' };
-
-/**
- * Extract loading messages
- */
-export type TLoadingMessage = IChatMessage & { type: 'loading' };
-
-/**
- * Extract error messages
- */
-export type TErrorMessage = IChatMessage & { type: 'error' };
-
-// ============================================================================
-// TYPE GUARDS
-// ============================================================================
-
-/**
- * Check if message is from user
- */
-export const isUserMessage = (message: IChatMessage): message is TUserMessage => {
- return message.type === 'user';
-};
-
-/**
- * Check if message is from system
- */
-export const isSystemMessage = (message: IChatMessage): message is TSystemMessage => {
- return message.type === 'system';
-};
-
-/**
- * Check if message is thinking indicator
- */
-export const isThinkingMessage = (message: IChatMessage): message is TThinkingMessage => {
- return message.type === 'thinking';
-};
-
-/**
- * Check if message is loading (replying/retrying)
- */
-export const isLoadingMessage = (message: IChatMessage): message is TLoadingMessage => {
- return message.type === 'loading';
-};
-
-/**
- * Check if message is error
- */
-export const isErrorMessage = (message: IChatMessage): message is TErrorMessage => {
- return message.type === 'error';
-};