Latestv1.0.60
AiProvider Global Config
AiProvider is the core configuration component of the AI component library, used to inject global settings at the top level of the application. It uses Vue's provide/inject mechanism to provide essential configurations such as base API URLs, authentication info, avatars, and typewriter effects to all downstream AI components (e.g., AiChat, AiBubble).
Basic Usage
It is recommended to wrap AiProvider at the root component of your application or at the outer-most level of an AI feature module.
Basic Usage
Global Identity Configuration
You can centrally set user and assistant avatars and names using AiProvider.
Global Identity Configuration
API
Props
| Name | Description | Type | Default |
|---|---|---|---|
base-url | Global AI base URL | string | - |
token | Authentication Token (Bearer) | string | - |
headers | Global request headers | Record<string, string> | - |
user-avatar | User avatar URL | string | - |
assistant-avatar | Assistant avatar URL | string | - |
user-name | User display name | string | - |
assistant-name | Assistant display name | string | - |
system-prompt | Global system prompt | string | - |
typewriter | Typewriter effect configuration | object | { enabled: true, charsPerFrame: 2 } |
typewriter property definition
| Name | Description | Type | Default |
|---|---|---|---|
enabled | Whether to enable typewriter effect | boolean | true |
chars_per_frame | Characters per frame to render | number | 2 |
Request/Response Interceptor Props
| Property Name | Description | Type | Default |
|---|---|---|---|
timeout | Request timeout (ms) | number | 30000 |
withCredentials | Allow credentials | boolean | false |
mode | Request mode | 'cors' | 'no-cors' | 'same-origin' | 'cors' |
cache | Cache mode | RequestCache | See below |
Methods
| Method Name | Description | Parameters | Returns |
|---|---|---|---|
addRequestInterceptor | Add request interceptor | (interceptor: AiRequestInterceptor) => number | Interceptor ID |
addResponseInterceptor | Add response interceptor | (interceptor: AiResponseInterceptor) => number | Interceptor ID |
removeRequestInterceptor | Remove request interceptor | (id: number) => void | — |
removeResponseInterceptor | Remove response interceptor | (id: number) => void | — |
clearInterceptors | Clear all interceptors | () => void | — |
Interceptor Usage Examples
Basic Interceptor
Add Request/Response Interceptors
Request Interceptor - Add Token
Request Interceptor - Auto Add Token
Response Interceptor - Error Handling
Response Interceptor - Unified Error Handling
Logging Interceptor
Logging Interceptor
Type Definitions
typescript
import type { AiRequestConfig, AiResponse } from '@yh-ui/components'
// Request Interceptor
interface AiRequestInterceptor {
onRequest?: (config: AiRequestConfig) => AiRequestConfig | void | Promise<AiRequestConfig | void>
onRequestError?: (error: Error) => void
}
// Response Interceptor
interface AiResponseInterceptor {
onResponse?: (response: Response) => Response | void | Promise<Response | void>
onResponseError?: (error: Error) => void
}Recommendations
- Vite/Nuxt Projects: Typically used once in
App.vueorlayouts/default.vue. - Scope Isolation: If your application has multiple distinct AI modules (e.g., a support bot and a code assistant), you can wrap them in separate
AiProviderinstances for isolation.