@redhat-cloud-services/ai-client-common 0.4.0 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +35 -14
- package/package.json +1 -1
- package/src/lib/ai-client-common.d.ts +11 -5
- package/src/lib/ai-client-common.d.ts.map +1 -1
package/README.md
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
# @redhat-cloud-services/ai-client-common
|
|
2
2
|
|
|
3
|
-
|
|
4
3
|
Common interfaces and utilities for AI client packages in the Red Hat Cloud Services ecosystem.
|
|
5
4
|
|
|
5
|
+
|
|
6
6
|
## Features
|
|
7
7
|
|
|
8
8
|
- **Standardized AI Client Interface** - Common `IAIClient` interface for all AI services
|
|
9
|
+
- **Conversation Management** - Standard conversation interface with locking support
|
|
9
10
|
- **Dependency Injection** - Interfaces for custom fetch implementations and streaming handlers
|
|
10
11
|
- **TypeScript Support** - Comprehensive type definitions for AI client development
|
|
11
12
|
- **Error Handling** - Base error classes with validation error support
|
|
@@ -32,10 +33,10 @@ interface IAIClient<AP extends Record<string, unknown> = Record<string, unknown>
|
|
|
32
33
|
initialConversationId: string;
|
|
33
34
|
conversations: IConversation[];
|
|
34
35
|
}>;
|
|
35
|
-
sendMessage<TChunk = unknown>(
|
|
36
|
+
sendMessage<TChunk = unknown, T extends Record<string, unknown> = Record<string, unknown>>(
|
|
36
37
|
conversationId: string,
|
|
37
38
|
message: string,
|
|
38
|
-
options?: ISendMessageOptions<
|
|
39
|
+
options?: ISendMessageOptions<T>
|
|
39
40
|
): Promise<TChunk | IMessageResponse<AP> | void>;
|
|
40
41
|
getDefaultStreamingHandler?<TChunk = unknown>(): IStreamingHandler<TChunk> | undefined;
|
|
41
42
|
getConversationHistory(conversationId: string, options?: IRequestOptions): Promise<IConversationHistoryResponse<AP>>;
|
|
@@ -83,17 +84,34 @@ const config: IBaseClientConfig = {
|
|
|
83
84
|
|
|
84
85
|
### Streaming Support
|
|
85
86
|
|
|
87
|
+
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.
|
|
88
|
+
|
|
89
|
+
#### IStreamChunk Interface
|
|
90
|
+
|
|
91
|
+
```typescript
|
|
92
|
+
import { IStreamChunk } from '@redhat-cloud-services/ai-client-common';
|
|
93
|
+
|
|
94
|
+
interface IStreamChunk<T extends Record<string, unknown> = Record<string, unknown>> {
|
|
95
|
+
answer: string;
|
|
96
|
+
additionalAttributes: T;
|
|
97
|
+
}
|
|
98
|
+
```
|
|
99
|
+
|
|
86
100
|
#### Implementing a Custom Streaming Handler
|
|
87
101
|
|
|
88
102
|
```typescript
|
|
89
|
-
import { IStreamingHandler } from '@redhat-cloud-services/ai-client-common';
|
|
103
|
+
import { IStreamingHandler, AfterChunkCallback, IStreamChunk } from '@redhat-cloud-services/ai-client-common';
|
|
90
104
|
|
|
91
|
-
class CustomStreamingHandler implements IStreamingHandler<
|
|
92
|
-
onChunk(chunk:
|
|
105
|
+
class CustomStreamingHandler<TChunk = unknown> implements IStreamingHandler<TChunk> {
|
|
106
|
+
onChunk(chunk: TChunk, afterChunk?: AfterChunkCallback): void {
|
|
93
107
|
console.log('Received chunk:', chunk);
|
|
94
|
-
|
|
108
|
+
|
|
109
|
+
// Process the chunk and call afterChunk with standardized format
|
|
95
110
|
if (afterChunk) {
|
|
96
|
-
afterChunk(
|
|
111
|
+
afterChunk({
|
|
112
|
+
answer: extractAnswer(chunk), // Extract answer from chunk
|
|
113
|
+
additionalAttributes: extractAttributes(chunk) // Extract additional data
|
|
114
|
+
});
|
|
97
115
|
}
|
|
98
116
|
}
|
|
99
117
|
|
|
@@ -101,7 +119,7 @@ class CustomStreamingHandler implements IStreamingHandler<string> {
|
|
|
101
119
|
console.log('Stream started', { conversationId, messageId });
|
|
102
120
|
}
|
|
103
121
|
|
|
104
|
-
onComplete?(finalChunk:
|
|
122
|
+
onComplete?(finalChunk: TChunk): void {
|
|
105
123
|
console.log('Stream completed:', finalChunk);
|
|
106
124
|
}
|
|
107
125
|
|
|
@@ -118,15 +136,17 @@ class CustomStreamingHandler implements IStreamingHandler<string> {
|
|
|
118
136
|
#### Streaming Request Options
|
|
119
137
|
|
|
120
138
|
```typescript
|
|
121
|
-
import { ISendMessageOptions } from '@redhat-cloud-services/ai-client-common';
|
|
139
|
+
import { ISendMessageOptions, IStreamChunk } from '@redhat-cloud-services/ai-client-common';
|
|
122
140
|
|
|
123
|
-
const streamingOptions: ISendMessageOptions
|
|
141
|
+
const streamingOptions: ISendMessageOptions = {
|
|
124
142
|
stream: true,
|
|
125
143
|
headers: { 'Custom-Header': 'value' },
|
|
126
144
|
signal: abortController.signal,
|
|
127
|
-
afterChunk: (chunk) => {
|
|
128
|
-
// Process each chunk as it arrives
|
|
129
|
-
|
|
145
|
+
afterChunk: (chunk: IStreamChunk) => {
|
|
146
|
+
// Process each standardized chunk as it arrives
|
|
147
|
+
console.log('Answer:', chunk.answer);
|
|
148
|
+
console.log('Additional data:', chunk.additionalAttributes);
|
|
149
|
+
updateUI(chunk.answer);
|
|
130
150
|
}
|
|
131
151
|
};
|
|
132
152
|
```
|
|
@@ -191,6 +211,7 @@ interface IMessageResponse<AP = Record<string, unknown>> {
|
|
|
191
211
|
interface IConversation {
|
|
192
212
|
id: string;
|
|
193
213
|
title: string;
|
|
214
|
+
locked: boolean; // Prevents new messages when true
|
|
194
215
|
}
|
|
195
216
|
```
|
|
196
217
|
|
package/package.json
CHANGED
|
@@ -59,7 +59,7 @@ export interface IAIClient<AP extends Record<string, unknown> = Record<string, u
|
|
|
59
59
|
* @param options - Optional configuration for the request
|
|
60
60
|
* @returns Promise that resolves to the AI's response
|
|
61
61
|
*/
|
|
62
|
-
sendMessage<TChunk = unknown>(conversationId: string, message: string, options?: ISendMessageOptions<
|
|
62
|
+
sendMessage<TChunk = unknown, T extends Record<string, unknown> = Record<string, unknown>>(conversationId: string, message: string, options?: ISendMessageOptions<T>): Promise<TChunk | IMessageResponse<AP> | void>;
|
|
63
63
|
/**
|
|
64
64
|
* Get the default streaming handler for this client (if any)
|
|
65
65
|
* @returns The default streaming handler or undefined if not configured
|
|
@@ -130,7 +130,7 @@ export interface IStreamingHandler<TChunk = unknown> {
|
|
|
130
130
|
/**
|
|
131
131
|
* Handle a single chunk of streaming data
|
|
132
132
|
*/
|
|
133
|
-
onChunk(chunk: TChunk, afterChunk?: (chunk:
|
|
133
|
+
onChunk(chunk: TChunk, afterChunk?: (chunk: IStreamChunk) => void): void;
|
|
134
134
|
/**
|
|
135
135
|
* Called when the stream starts
|
|
136
136
|
*/
|
|
@@ -208,7 +208,7 @@ export interface IConversationMessage<T extends Record<string, unknown> = Record
|
|
|
208
208
|
answer: string;
|
|
209
209
|
role: 'user' | 'bot';
|
|
210
210
|
input: string;
|
|
211
|
-
|
|
211
|
+
additionalAttributes?: T;
|
|
212
212
|
}
|
|
213
213
|
/**
|
|
214
214
|
* Response type for conversation history requests
|
|
@@ -217,19 +217,25 @@ export type IConversationHistoryResponse<T extends Record<string, unknown> = Rec
|
|
|
217
217
|
export interface IConversation {
|
|
218
218
|
id: string;
|
|
219
219
|
title: string;
|
|
220
|
+
locked: boolean;
|
|
220
221
|
}
|
|
221
222
|
export interface IErrorMessageResponse {
|
|
222
223
|
error: any;
|
|
223
224
|
}
|
|
225
|
+
export interface IStreamChunk<T extends Record<string, unknown> = Record<string, unknown>> {
|
|
226
|
+
answer: string;
|
|
227
|
+
additionalAttributes: T;
|
|
228
|
+
}
|
|
229
|
+
export type AfterChunkCallback<T extends Record<string, unknown> = Record<string, unknown>> = (chunk: IStreamChunk<T>) => void;
|
|
224
230
|
/**
|
|
225
231
|
* Options for sending messages, supporting both streaming and non-streaming modes
|
|
226
232
|
*/
|
|
227
|
-
export interface ISendMessageOptions<
|
|
233
|
+
export interface ISendMessageOptions<T extends Record<string, unknown> = Record<string, unknown>> extends IRequestOptions {
|
|
228
234
|
/**
|
|
229
235
|
* Whether to use streaming mode for the response
|
|
230
236
|
* When true, the client's default streaming handler will be used
|
|
231
237
|
*/
|
|
232
238
|
stream?: boolean;
|
|
233
|
-
afterChunk?:
|
|
239
|
+
afterChunk?: AfterChunkCallback<T>;
|
|
234
240
|
}
|
|
235
241
|
//# sourceMappingURL=ai-client-common.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ai-client-common.d.ts","sourceRoot":"","sources":["../../../../../packages/ai-client-common/src/lib/ai-client-common.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,MAAM,WAAW,cAAc;IAC7B,CAAC,KAAK,EAAE,WAAW,GAAG,GAAG,EAAE,IAAI,CAAC,EAAE,WAAW,GAAG,OAAO,CAAC,QAAQ,CAAC,CAAC;CACnE;AAED;;GAEG;AACH,MAAM,WAAW,iBAAiB,CAAC,MAAM,GAAG,OAAO;IACjD;;OAEG;IACH,OAAO,EAAE,MAAM,CAAC;IAEhB;;;OAGG;IACH,aAAa,EAAE,cAAc,CAAC;IAE9B;;;;OAIG;IACH,uBAAuB,CAAC,EAAE,iBAAiB,CAAC,MAAM,CAAC,CAAC;CACrD;AAED;;GAEG;AACH,MAAM,WAAW,eAAe;IAC9B;;OAEG;IACH,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IAEjC;;OAEG;IACH,MAAM,CAAC,EAAE,WAAW,CAAC;CACtB;AAED;;;GAGG;AACH,MAAM,WAAW,SAAS,CAAC,EAAE,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC;IACrF;;;;OAIG;IACH,IAAI,IAAI,OAAO,CAAC;QACd,qBAAqB,EAAE,MAAM,CAAC;QAC9B,aAAa,EAAE,aAAa,EAAE,CAAC;KAChC,CAAC,CAAC;IAEH;;;;;;OAMG;IACH,WAAW,CAAC,MAAM,GAAG,OAAO,
|
|
1
|
+
{"version":3,"file":"ai-client-common.d.ts","sourceRoot":"","sources":["../../../../../packages/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