Skip to content
Latestv1.0.63

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 ​

NameDescriptionTypeDefault
base-urlGlobal AI base URLstring-
tokenAuthentication Token (Bearer)string-
headersGlobal request headersRecord<string, string>-
user-avatarUser avatar URLstring-
assistant-avatarAssistant avatar URLstring-
user-nameUser display namestring-
assistant-nameAssistant display namestring-
system-promptGlobal system promptstring-
typewriterTypewriter effect configurationobject{ enabled: true, charsPerFrame: 2 }

typewriter property definition ​

NameDescriptionTypeDefault
enabledWhether to enable typewriter effectbooleantrue
chars_per_frameCharacters per frame to rendernumber2

Request/Response Interceptor Props ​

Property NameDescriptionTypeDefault
timeoutRequest timeout (ms)number30000
withCredentialsAllow credentialsbooleanfalse
modeRequest mode'cors' | 'no-cors' | 'same-origin''cors'
cacheCache modeRequestCacheSee below

Methods ​

Method NameDescriptionParametersReturns
addRequestInterceptorAdd request interceptor(interceptor: AiRequestInterceptor) => numberInterceptor ID
addResponseInterceptorAdd response interceptor(interceptor: AiResponseInterceptor) => numberInterceptor ID
removeRequestInterceptorRemove request interceptor(id: number) => void—
removeResponseInterceptorRemove response interceptor(id: number) => void—
clearInterceptorsClear 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.vue or layouts/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 AiProvider instances for isolation.

Released under the MIT License.