@redhat-cloud-services/ai-client-common
Advanced tools
Comparing version
{ | ||
"name": "@redhat-cloud-services/ai-client-common", | ||
"version": "0.12.0", | ||
"version": "0.13.0", | ||
"type": "commonjs", | ||
@@ -5,0 +5,0 @@ "main": "./src/index.js", |
@@ -31,4 +31,4 @@ # @redhat-cloud-services/ai-client-common | ||
declare class IAIClient<AP extends Record<string, unknown> = Record<string, unknown>, TChunk = unknown> { | ||
constructor(config: IBaseClientConfig<TChunk>); | ||
declare class IAIClient<AP extends Record<string, unknown> = Record<string, unknown>> { | ||
constructor(config: IBaseClientConfig); | ||
@@ -58,4 +58,2 @@ init(): Promise<{ | ||
getDefaultStreamingHandler<TChunk = unknown>(): IStreamingHandler<TChunk> | undefined; | ||
getConversationHistory(conversationId: string, options?: IRequestOptions): Promise<IConversationHistoryResponse<AP>>; | ||
@@ -102,4 +100,3 @@ | ||
baseUrl: 'https://your-ai-service.com', | ||
fetchFunction: customFetch, // Optional - defaults to native fetch | ||
defaultStreamingHandler: new CustomStreamingHandler() // Optional | ||
fetchFunction: customFetch // Optional - defaults to native fetch | ||
}; | ||
@@ -149,3 +146,3 @@ ``` | ||
The streaming interface has been updated to standardize chunk handling across all AI clients. The `afterChunk` callback now receives an `IStreamChunk` object with standardized structure. | ||
The streaming interface has been updated to standardize chunk handling across all AI clients. The `handleChunk` callback now receives an `IStreamChunk` object with standardized structure. | ||
@@ -160,2 +157,3 @@ #### IStreamChunk Interface | ||
messageId: string; | ||
conversationId: string; | ||
additionalAttributes: T; | ||
@@ -168,11 +166,11 @@ } | ||
```typescript | ||
import { IStreamingHandler, AfterChunkCallback, IStreamChunk } from '@redhat-cloud-services/ai-client-common'; | ||
import { IStreamingHandler, HandleChunkCallback, IStreamChunk } from '@redhat-cloud-services/ai-client-common'; | ||
class CustomStreamingHandler<TChunk = unknown> implements IStreamingHandler<TChunk> { | ||
onChunk(chunk: TChunk, afterChunk?: AfterChunkCallback): void { | ||
onChunk(chunk: TChunk, handleChunk?: HandleChunkCallback): void { | ||
console.log('Received chunk:', chunk); | ||
// Process the chunk and call afterChunk with standardized format | ||
if (afterChunk) { | ||
afterChunk({ | ||
// Process the chunk and call handleChunk with standardized format | ||
if (handleChunk) { | ||
handleChunk({ | ||
answer: extractAnswer(chunk), // Extract answer from chunk | ||
@@ -212,5 +210,7 @@ additionalAttributes: extractAttributes(chunk) // Extract additional data | ||
signal: abortController.signal, | ||
afterChunk: (chunk: IStreamChunk) => { | ||
handleChunk: (chunk: IStreamChunk) => { | ||
// Process each standardized chunk as it arrives | ||
console.log('Answer:', chunk.answer); | ||
console.log('Message ID:', chunk.messageId); | ||
console.log('Conversation ID:', chunk.conversationId); | ||
console.log('Additional data:', chunk.additionalAttributes); | ||
@@ -244,3 +244,3 @@ updateUI(chunk.answer); | ||
stream?: boolean; | ||
afterChunk?: AfterChunkCallback<T>; | ||
handleChunk?: HandleChunkCallback<T>; | ||
requestPayload?: R extends never ? never : R; | ||
@@ -247,0 +247,0 @@ } |
@@ -11,3 +11,3 @@ /** | ||
*/ | ||
export interface IBaseClientConfig<TChunk = unknown> { | ||
export interface IBaseClientConfig { | ||
/** | ||
@@ -22,8 +22,2 @@ * The base URL for the API | ||
fetchFunction?: IFetchFunction; | ||
/** | ||
* Default streaming handler for the client | ||
* Used when sendMessage is called with stream=true but no streamingHandler provided | ||
* Individual requests can override this by providing their own streamingHandler | ||
*/ | ||
defaultStreamingHandler?: IStreamingHandler<TChunk>; | ||
} | ||
@@ -56,4 +50,4 @@ /** | ||
*/ | ||
declare class IAIClient<AP extends Record<string, unknown> = Record<string, unknown>, TChunk = unknown> { | ||
constructor(config: IBaseClientConfig<TChunk>); | ||
declare class IAIClient<AP extends Record<string, unknown> = Record<string, unknown>> { | ||
constructor(config: IBaseClientConfig); | ||
/** | ||
@@ -86,8 +80,2 @@ * Initialize the client and return existing conversations | ||
/** | ||
* Get the default streaming handler for this client | ||
* All AI clients must implement this method to provide consistent streaming behavior | ||
* @returns The default streaming handler or undefined if not configured | ||
*/ | ||
getDefaultStreamingHandler<TChunk = unknown>(): IStreamingHandler<TChunk> | undefined; | ||
/** | ||
* Get the conversation history for a specific conversation | ||
@@ -158,3 +146,3 @@ * @param conversationId - The conversation ID to retrieve history for | ||
*/ | ||
onChunk(chunk: TChunk, afterChunk?: (chunk: IStreamChunk) => void): void; | ||
onChunk(chunk: TChunk, handleChunk?: (chunk: IStreamChunk) => void): void; | ||
/** | ||
@@ -178,2 +166,26 @@ * Called when the stream starts | ||
/** | ||
* Simplified streaming handler interface for cleaner implementation | ||
* This will eventually replace IStreamingHandler across all clients | ||
* | ||
* The simplified approach focuses on: | ||
* - Processing chunks and building message buffer | ||
* - Calling callback with current complete message | ||
* - Simple error handling | ||
*/ | ||
declare class ISimpleStreamingHandler<TChunk = unknown> { | ||
/** | ||
* Process a chunk and return updated message buffer | ||
* @param chunk - The chunk data (text string or parsed JSON object) | ||
* @param currentBuffer - The current accumulated message content | ||
* @param handleChunk - Mandatory callback to execute with current complete message | ||
* @returns Updated message buffer after processing this chunk | ||
*/ | ||
processChunk(chunk: TChunk, currentBuffer: string, handleChunk: HandleChunkCallback): string; | ||
/** | ||
* Called when an error occurs during streaming | ||
*/ | ||
onError?(error: Error): void; | ||
} | ||
export { ISimpleStreamingHandler }; | ||
/** | ||
* Streaming request options for clients that support streaming | ||
@@ -260,3 +272,3 @@ */ | ||
} | ||
export type AfterChunkCallback<T extends Record<string, unknown> = Record<string, unknown>> = (chunk: IStreamChunk<T>) => void; | ||
export type HandleChunkCallback<T extends Record<string, unknown> = Record<string, unknown>> = (chunk: IStreamChunk<T>) => void; | ||
/** | ||
@@ -271,3 +283,3 @@ * Options for sending messages, supporting both streaming and non-streaming modes | ||
stream?: boolean; | ||
afterChunk?: AfterChunkCallback<T>; | ||
handleChunk?: HandleChunkCallback<T>; | ||
/** | ||
@@ -274,0 +286,0 @@ * Additional request payload data specific to the client implementation |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.AIClientValidationError = exports.AIClientError = exports.IAIClient = void 0; | ||
exports.ISimpleStreamingHandler = exports.AIClientValidationError = exports.AIClientError = exports.IAIClient = void 0; | ||
exports.isInitErrorResponse = isInitErrorResponse; | ||
@@ -5,0 +5,0 @@ function isInitErrorResponse(obj) { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
29931
1.22%333
3.74%