@ragbits/api-client 0.0.2 → 1.3.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 +207 -63
- package/{src/test → __tests__}/utils.ts +18 -1
- package/dist/autogen.types.d.ts +426 -0
- package/dist/index.cjs +149 -44
- package/dist/index.d.ts +17 -225
- package/dist/index.js +147 -44
- package/dist/types.d.ts +77 -0
- package/package.json +9 -4
- package/dist/index.d.cts +0 -257
- package/eslint.config.js +0 -21
- package/prettier.config.js +0 -6
- package/src/index.ts +0 -213
- package/src/test/RagbitsClient.test.ts +0 -484
- package/src/test/types.test.ts +0 -35
- package/src/types.ts +0 -259
- package/tsconfig.json +0 -16
- package/vitest.config.ts +0 -20
- /package/{src/test → __tests__}/mocks/handlers.ts +0 -0
- /package/{src/test → __tests__}/setup.ts +0 -0
package/dist/index.js
CHANGED
|
@@ -1,24 +1,33 @@
|
|
|
1
|
-
// src/types.ts
|
|
2
|
-
var
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
1
|
+
// src/autogen.types.ts
|
|
2
|
+
var ChatResponseType = {
|
|
3
|
+
Text: "text",
|
|
4
|
+
Reference: "reference",
|
|
5
|
+
StateUpdate: "state_update",
|
|
6
|
+
MessageId: "message_id",
|
|
7
|
+
ConversationId: "conversation_id",
|
|
8
|
+
LiveUpdate: "live_update",
|
|
9
|
+
FollowupMessages: "followup_messages",
|
|
10
|
+
Image: "image",
|
|
11
|
+
ChunkedContent: "chunked_content",
|
|
12
|
+
ClearMessage: "clear_message",
|
|
13
|
+
Usage: "usage"
|
|
14
|
+
};
|
|
15
|
+
var FeedbackType = {
|
|
16
|
+
Like: "like",
|
|
17
|
+
Dislike: "dislike"
|
|
18
|
+
};
|
|
19
|
+
var LiveUpdateType = {
|
|
20
|
+
Start: "START",
|
|
21
|
+
Finish: "FINISH"
|
|
22
|
+
};
|
|
23
|
+
var MessageRole = {
|
|
24
|
+
User: "user",
|
|
25
|
+
Assistant: "assistant",
|
|
26
|
+
System: "system"
|
|
27
|
+
};
|
|
28
|
+
var AuthType = {
|
|
29
|
+
Credentials: "credentials"
|
|
30
|
+
};
|
|
22
31
|
|
|
23
32
|
// src/index.ts
|
|
24
33
|
var RagbitsClient = class {
|
|
@@ -26,7 +35,15 @@ var RagbitsClient = class {
|
|
|
26
35
|
* @param config - Configuration object
|
|
27
36
|
*/
|
|
28
37
|
constructor(config = {}) {
|
|
29
|
-
this.
|
|
38
|
+
this.chunkQueue = /* @__PURE__ */ new Map();
|
|
39
|
+
this.baseUrl = config.baseUrl ?? "";
|
|
40
|
+
this.auth = config.auth;
|
|
41
|
+
if (this.baseUrl.endsWith("/")) {
|
|
42
|
+
this.baseUrl = this.baseUrl.slice(0, -1);
|
|
43
|
+
}
|
|
44
|
+
if (!this.baseUrl) {
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
30
47
|
try {
|
|
31
48
|
new URL(this.baseUrl);
|
|
32
49
|
} catch {
|
|
@@ -34,9 +51,6 @@ var RagbitsClient = class {
|
|
|
34
51
|
`Invalid base URL: ${this.baseUrl}. Please provide a valid URL.`
|
|
35
52
|
);
|
|
36
53
|
}
|
|
37
|
-
if (this.baseUrl.endsWith("/")) {
|
|
38
|
-
this.baseUrl = this.baseUrl.slice(0, -1);
|
|
39
|
-
}
|
|
40
54
|
}
|
|
41
55
|
/**
|
|
42
56
|
* Get the base URL used by this client
|
|
@@ -56,12 +70,20 @@ var RagbitsClient = class {
|
|
|
56
70
|
* @private
|
|
57
71
|
*/
|
|
58
72
|
async _makeRequest(url, options = {}) {
|
|
59
|
-
const
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
73
|
+
const defaultHeaders = {
|
|
74
|
+
"Content-Type": "application/json"
|
|
75
|
+
};
|
|
76
|
+
const headers = {
|
|
77
|
+
...defaultHeaders,
|
|
78
|
+
...this.normalizeHeaders(options.headers)
|
|
63
79
|
};
|
|
64
|
-
|
|
80
|
+
if (this.auth?.getToken) {
|
|
81
|
+
headers["Authorization"] = `Bearer ${this.auth.getToken()}`;
|
|
82
|
+
}
|
|
83
|
+
const response = await fetch(url, { ...options, headers });
|
|
84
|
+
if (response.status === 401) {
|
|
85
|
+
this.auth?.onUnauthorized?.();
|
|
86
|
+
}
|
|
65
87
|
if (!response.ok) {
|
|
66
88
|
throw new Error(`HTTP error! status: ${response.status}`);
|
|
67
89
|
}
|
|
@@ -69,7 +91,7 @@ var RagbitsClient = class {
|
|
|
69
91
|
}
|
|
70
92
|
/**
|
|
71
93
|
* Method to make API requests to known endpoints only
|
|
72
|
-
* @param endpoint - API endpoint path
|
|
94
|
+
* @param endpoint - API endpoint path
|
|
73
95
|
* @param options - Typed request options for the specific endpoint
|
|
74
96
|
*/
|
|
75
97
|
async makeRequest(endpoint, options) {
|
|
@@ -89,25 +111,26 @@ var RagbitsClient = class {
|
|
|
89
111
|
requestOptions.body = typeof body === "string" ? body : JSON.stringify(body);
|
|
90
112
|
}
|
|
91
113
|
const response = await this._makeRequest(
|
|
92
|
-
this._buildApiUrl(endpoint),
|
|
114
|
+
this._buildApiUrl(endpoint.toString()),
|
|
93
115
|
requestOptions
|
|
94
116
|
);
|
|
95
117
|
return response.json();
|
|
96
118
|
}
|
|
97
119
|
/**
|
|
98
120
|
* Method for streaming requests to known endpoints only
|
|
99
|
-
* @param endpoint - Streaming endpoint path
|
|
121
|
+
* @param endpoint - Streaming endpoint path
|
|
100
122
|
* @param data - Request data
|
|
101
123
|
* @param callbacks - Stream callbacks
|
|
102
124
|
* @param signal - Optional AbortSignal for cancelling the request
|
|
103
125
|
*/
|
|
104
|
-
makeStreamRequest(endpoint, data, callbacks, signal) {
|
|
126
|
+
makeStreamRequest(endpoint, data, callbacks, signal, customHeaders) {
|
|
105
127
|
let isCancelled = false;
|
|
106
128
|
const processStream = async (response) => {
|
|
107
129
|
const reader = response.body?.pipeThrough(new TextDecoderStream()).getReader();
|
|
108
130
|
if (!reader) {
|
|
109
131
|
throw new Error("Response body is null");
|
|
110
132
|
}
|
|
133
|
+
let buffer = "";
|
|
111
134
|
while (!isCancelled && !signal?.aborted) {
|
|
112
135
|
try {
|
|
113
136
|
const { value, done } = await reader.read();
|
|
@@ -115,7 +138,9 @@ var RagbitsClient = class {
|
|
|
115
138
|
callbacks.onClose?.();
|
|
116
139
|
break;
|
|
117
140
|
}
|
|
118
|
-
|
|
141
|
+
buffer += value;
|
|
142
|
+
const lines = buffer.split("\n");
|
|
143
|
+
buffer = lines.pop() ?? "";
|
|
119
144
|
for (const line of lines) {
|
|
120
145
|
if (!line.startsWith("data: ")) continue;
|
|
121
146
|
try {
|
|
@@ -123,6 +148,10 @@ var RagbitsClient = class {
|
|
|
123
148
|
const parsedData = JSON.parse(
|
|
124
149
|
jsonString
|
|
125
150
|
);
|
|
151
|
+
if (parsedData.type === ChatResponseType.ChunkedContent) {
|
|
152
|
+
this.handleChunkedContent(parsedData, callbacks);
|
|
153
|
+
continue;
|
|
154
|
+
}
|
|
126
155
|
await callbacks.onMessage(parsedData);
|
|
127
156
|
} catch (parseError) {
|
|
128
157
|
console.error("Error parsing JSON:", parseError);
|
|
@@ -132,6 +161,9 @@ var RagbitsClient = class {
|
|
|
132
161
|
}
|
|
133
162
|
}
|
|
134
163
|
} catch (streamError) {
|
|
164
|
+
if (signal?.aborted) {
|
|
165
|
+
return;
|
|
166
|
+
}
|
|
135
167
|
console.error("Stream error:", streamError);
|
|
136
168
|
await callbacks.onError(new Error("Error reading stream"));
|
|
137
169
|
break;
|
|
@@ -140,15 +172,29 @@ var RagbitsClient = class {
|
|
|
140
172
|
};
|
|
141
173
|
const startStream = async () => {
|
|
142
174
|
try {
|
|
143
|
-
const
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
175
|
+
const defaultHeaders = {
|
|
176
|
+
"Content-Type": "application/json",
|
|
177
|
+
Accept: "text/event-stream"
|
|
178
|
+
};
|
|
179
|
+
const headers = {
|
|
180
|
+
...defaultHeaders,
|
|
181
|
+
...customHeaders
|
|
182
|
+
};
|
|
183
|
+
if (this.auth?.getToken) {
|
|
184
|
+
headers["Authorization"] = `Bearer ${this.auth.getToken()}`;
|
|
185
|
+
}
|
|
186
|
+
const response = await fetch(
|
|
187
|
+
this._buildApiUrl(endpoint.toString()),
|
|
188
|
+
{
|
|
189
|
+
method: "POST",
|
|
190
|
+
headers,
|
|
191
|
+
body: JSON.stringify(data),
|
|
192
|
+
signal
|
|
193
|
+
}
|
|
194
|
+
);
|
|
195
|
+
if (response.status === 401) {
|
|
196
|
+
this.auth?.onUnauthorized?.();
|
|
197
|
+
}
|
|
152
198
|
if (!response.ok) {
|
|
153
199
|
throw new Error(`HTTP error! status: ${response.status}`);
|
|
154
200
|
}
|
|
@@ -172,10 +218,67 @@ var RagbitsClient = class {
|
|
|
172
218
|
isCancelled = true;
|
|
173
219
|
};
|
|
174
220
|
}
|
|
221
|
+
normalizeHeaders(init) {
|
|
222
|
+
if (!init) return {};
|
|
223
|
+
if (init instanceof Headers) {
|
|
224
|
+
return Object.fromEntries(init.entries());
|
|
225
|
+
}
|
|
226
|
+
if (Array.isArray(init)) {
|
|
227
|
+
return Object.fromEntries(init);
|
|
228
|
+
}
|
|
229
|
+
return init;
|
|
230
|
+
}
|
|
231
|
+
async handleChunkedContent(data, callbacks) {
|
|
232
|
+
const response = data;
|
|
233
|
+
const content = response.content;
|
|
234
|
+
const {
|
|
235
|
+
content_type: contentType,
|
|
236
|
+
id,
|
|
237
|
+
chunk_index: chunkIndex,
|
|
238
|
+
total_chunks: totalChunks,
|
|
239
|
+
mime_type: mimeType,
|
|
240
|
+
data: chunkData
|
|
241
|
+
} = content;
|
|
242
|
+
if (!this.chunkQueue.has(id)) {
|
|
243
|
+
this.chunkQueue.set(id, {
|
|
244
|
+
chunks: /* @__PURE__ */ new Map(),
|
|
245
|
+
totalChunks,
|
|
246
|
+
mimeType
|
|
247
|
+
});
|
|
248
|
+
}
|
|
249
|
+
const imageInfo = this.chunkQueue.get(id);
|
|
250
|
+
imageInfo.chunks.set(chunkIndex, chunkData);
|
|
251
|
+
if (imageInfo.chunks.size !== totalChunks) return;
|
|
252
|
+
const sortedChunks = Array.from(
|
|
253
|
+
{ length: totalChunks },
|
|
254
|
+
(_, i) => imageInfo.chunks.get(i)
|
|
255
|
+
);
|
|
256
|
+
const completeBase64 = sortedChunks.join("");
|
|
257
|
+
try {
|
|
258
|
+
atob(completeBase64);
|
|
259
|
+
} catch (e) {
|
|
260
|
+
this.chunkQueue.delete(id);
|
|
261
|
+
console.error("\u274C Invalid base64 data: ", e);
|
|
262
|
+
await callbacks.onError(new Error("Error reading stream"));
|
|
263
|
+
}
|
|
264
|
+
if (contentType === ChatResponseType.Image) {
|
|
265
|
+
const completeImageResponse = {
|
|
266
|
+
type: ChatResponseType.Image,
|
|
267
|
+
content: {
|
|
268
|
+
id,
|
|
269
|
+
url: `${imageInfo.mimeType},${completeBase64}`
|
|
270
|
+
}
|
|
271
|
+
};
|
|
272
|
+
await callbacks.onMessage(completeImageResponse);
|
|
273
|
+
}
|
|
274
|
+
this.chunkQueue.delete(id);
|
|
275
|
+
}
|
|
175
276
|
};
|
|
176
277
|
export {
|
|
278
|
+
AuthType,
|
|
177
279
|
ChatResponseType,
|
|
178
280
|
FeedbackType,
|
|
281
|
+
LiveUpdateType,
|
|
179
282
|
MessageRole,
|
|
180
283
|
RagbitsClient
|
|
181
284
|
};
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { ConfigResponse, FeedbackRequest, FeedbackResponse, ChatRequest, ChatResponse, LogoutRequest, LoginRequest, LoginResponse } from './autogen.types';
|
|
2
|
+
export interface GenericResponse {
|
|
3
|
+
success: boolean;
|
|
4
|
+
}
|
|
5
|
+
/**
|
|
6
|
+
* Configuration for the client
|
|
7
|
+
*/
|
|
8
|
+
export interface ClientConfig {
|
|
9
|
+
baseUrl?: string;
|
|
10
|
+
auth?: {
|
|
11
|
+
getToken?: () => string;
|
|
12
|
+
onUnauthorized?: () => Promise<void> | void;
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Callbacks for handling streaming responses
|
|
17
|
+
*/
|
|
18
|
+
export interface StreamCallbacks<T, E = Error> {
|
|
19
|
+
onMessage: (data: T) => void | Promise<void>;
|
|
20
|
+
onError: (error: E) => void | Promise<void>;
|
|
21
|
+
onClose?: () => void | Promise<void>;
|
|
22
|
+
}
|
|
23
|
+
export interface EndpointDefinition<Req = any, Res = any> {
|
|
24
|
+
method: string;
|
|
25
|
+
request: Req;
|
|
26
|
+
response: Res;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Base predefined API endpoint definitions with their request/response types
|
|
30
|
+
*/
|
|
31
|
+
export interface BaseApiEndpoints {
|
|
32
|
+
'/api/config': EndpointDefinition<never, ConfigResponse>;
|
|
33
|
+
'/api/feedback': EndpointDefinition<FeedbackRequest, FeedbackResponse>;
|
|
34
|
+
'/api/auth/login': EndpointDefinition<LoginRequest, LoginResponse>;
|
|
35
|
+
'/api/auth/logout': EndpointDefinition<LogoutRequest, GenericResponse>;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Streaming API endpoint definitions with their request/stream response types
|
|
39
|
+
*/
|
|
40
|
+
export interface BaseStreamingEndpoints {
|
|
41
|
+
'/api/chat': EndpointDefinition<ChatRequest, ChatResponse>;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Extract endpoint paths as a union type
|
|
45
|
+
*/
|
|
46
|
+
export type EndpointPath<Endpoints extends {
|
|
47
|
+
[K in keyof Endpoints]: EndpointDefinition;
|
|
48
|
+
}> = keyof Endpoints;
|
|
49
|
+
/**
|
|
50
|
+
* Extract request type for a specific API endpoint
|
|
51
|
+
*/
|
|
52
|
+
export type EndpointRequest<URL extends keyof Endpoints, Endpoints extends {
|
|
53
|
+
[K in keyof Endpoints]: EndpointDefinition;
|
|
54
|
+
}> = Endpoints[URL]['request'];
|
|
55
|
+
/**
|
|
56
|
+
* Extract response type for a specific API endpoint
|
|
57
|
+
*/
|
|
58
|
+
export type EndpointResponse<URL extends keyof Endpoints, Endpoints extends {
|
|
59
|
+
[K in keyof Endpoints]: EndpointDefinition;
|
|
60
|
+
}> = Endpoints[URL]['response'];
|
|
61
|
+
/**
|
|
62
|
+
* Extract HTTP method for a specific API endpoint
|
|
63
|
+
*/
|
|
64
|
+
export type EndpointMethod<URL extends keyof Endpoints, Endpoints extends {
|
|
65
|
+
[K in keyof Endpoints]: EndpointDefinition;
|
|
66
|
+
}> = Endpoints[URL]['method'];
|
|
67
|
+
/**
|
|
68
|
+
* Generic request options for API endpoints with typed methods and body
|
|
69
|
+
*/
|
|
70
|
+
export interface RequestOptions<URL extends keyof Endpoints, Endpoints extends {
|
|
71
|
+
[K in keyof Endpoints]: EndpointDefinition;
|
|
72
|
+
}> {
|
|
73
|
+
method?: Endpoints[URL]['method'];
|
|
74
|
+
body?: Endpoints[URL]['request'] extends never ? undefined : Endpoints[URL]['request'];
|
|
75
|
+
headers?: Record<string, string>;
|
|
76
|
+
signal?: AbortSignal;
|
|
77
|
+
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ragbits/api-client",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "1.3.0",
|
|
4
4
|
"description": "JavaScript client for the Ragbits API",
|
|
5
|
+
"repository": {
|
|
6
|
+
"type": "git",
|
|
7
|
+
"url": "https://github.com/deepsense-ai/ragbits"
|
|
8
|
+
},
|
|
5
9
|
"main": "dist/index.cjs",
|
|
6
10
|
"module": "dist/index.js",
|
|
7
11
|
"types": "dist/index.d.ts",
|
|
@@ -14,14 +18,15 @@
|
|
|
14
18
|
}
|
|
15
19
|
},
|
|
16
20
|
"scripts": {
|
|
17
|
-
"build": "tsup src/index.ts --format cjs,esm --
|
|
18
|
-
"dev": "
|
|
21
|
+
"build": "npm run clean && tsup src/index.ts --format cjs,esm && tsc --emitDeclarationOnly --declaration --project tsconfig.json",
|
|
22
|
+
"dev": "tsc -b . --watch",
|
|
19
23
|
"test": "vitest",
|
|
20
24
|
"test:run": "vitest run",
|
|
21
25
|
"test:coverage": "vitest run --coverage",
|
|
22
26
|
"lint": "eslint .",
|
|
23
27
|
"format": "prettier --write .",
|
|
24
|
-
"format:check": "prettier --check ."
|
|
28
|
+
"format:check": "prettier --check .",
|
|
29
|
+
"clean": "rm -rf ./dist && rm -f ./tsconfig.tsbuildinfo"
|
|
25
30
|
},
|
|
26
31
|
"keywords": [
|
|
27
32
|
"ragbits",
|
package/dist/index.d.cts
DELETED
|
@@ -1,257 +0,0 @@
|
|
|
1
|
-
import { RJSFSchema } from '@rjsf/utils';
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Message roles for chat conversations
|
|
5
|
-
*/
|
|
6
|
-
declare enum MessageRole {
|
|
7
|
-
USER = "user",
|
|
8
|
-
ASSISTANT = "assistant",
|
|
9
|
-
SYSTEM = "system"
|
|
10
|
-
}
|
|
11
|
-
/**
|
|
12
|
-
* Message structure for chat conversations
|
|
13
|
-
*/
|
|
14
|
-
interface Message {
|
|
15
|
-
role: MessageRole;
|
|
16
|
-
content: string;
|
|
17
|
-
id?: string;
|
|
18
|
-
}
|
|
19
|
-
/**
|
|
20
|
-
* Reference structure for document references
|
|
21
|
-
*/
|
|
22
|
-
interface Reference {
|
|
23
|
-
title: string;
|
|
24
|
-
content: string;
|
|
25
|
-
url?: string;
|
|
26
|
-
}
|
|
27
|
-
/**
|
|
28
|
-
* Response types from the API
|
|
29
|
-
*/
|
|
30
|
-
declare enum ChatResponseType {
|
|
31
|
-
MESSAGE = "message",
|
|
32
|
-
REFERENCE = "reference",
|
|
33
|
-
STATE_UPDATE = "state_update",
|
|
34
|
-
TEXT = "text",
|
|
35
|
-
MESSAGE_ID = "message_id",
|
|
36
|
-
CONVERSATION_ID = "conversation_id"
|
|
37
|
-
}
|
|
38
|
-
/**
|
|
39
|
-
* Feedback types for user feedback
|
|
40
|
-
*/
|
|
41
|
-
declare enum FeedbackType {
|
|
42
|
-
LIKE = "like",
|
|
43
|
-
DISLIKE = "dislike"
|
|
44
|
-
}
|
|
45
|
-
/**
|
|
46
|
-
* Server state interface for state updates
|
|
47
|
-
*/
|
|
48
|
-
interface ServerState {
|
|
49
|
-
state: Record<string, unknown>;
|
|
50
|
-
signature: string;
|
|
51
|
-
}
|
|
52
|
-
/**
|
|
53
|
-
* Specific chat response types
|
|
54
|
-
*/
|
|
55
|
-
interface MessageIdChatResponse {
|
|
56
|
-
type: ChatResponseType.MESSAGE_ID;
|
|
57
|
-
content: string;
|
|
58
|
-
}
|
|
59
|
-
interface TextChatResponse {
|
|
60
|
-
type: ChatResponseType.TEXT;
|
|
61
|
-
content: string;
|
|
62
|
-
}
|
|
63
|
-
interface ReferenceChatResponse {
|
|
64
|
-
type: ChatResponseType.REFERENCE;
|
|
65
|
-
content: Reference;
|
|
66
|
-
}
|
|
67
|
-
interface ConversationIdChatResponse {
|
|
68
|
-
type: ChatResponseType.CONVERSATION_ID;
|
|
69
|
-
content: string;
|
|
70
|
-
}
|
|
71
|
-
interface StateUpdateChatResponse {
|
|
72
|
-
type: ChatResponseType.STATE_UPDATE;
|
|
73
|
-
content: ServerState;
|
|
74
|
-
}
|
|
75
|
-
/**
|
|
76
|
-
* Typed chat response union
|
|
77
|
-
*/
|
|
78
|
-
type TypedChatResponse = TextChatResponse | ReferenceChatResponse | MessageIdChatResponse | ConversationIdChatResponse | StateUpdateChatResponse;
|
|
79
|
-
/**
|
|
80
|
-
* Base chat request to the API
|
|
81
|
-
*/
|
|
82
|
-
interface ChatRequest {
|
|
83
|
-
message: string;
|
|
84
|
-
history: Message[];
|
|
85
|
-
context?: Record<string, unknown>;
|
|
86
|
-
}
|
|
87
|
-
/**
|
|
88
|
-
* Feedback request to the API
|
|
89
|
-
*/
|
|
90
|
-
interface FeedbackRequest {
|
|
91
|
-
message_id: string;
|
|
92
|
-
feedback: FeedbackType;
|
|
93
|
-
payload: Record<string, unknown> | null;
|
|
94
|
-
}
|
|
95
|
-
/**
|
|
96
|
-
* Feedback response from the API
|
|
97
|
-
*/
|
|
98
|
-
interface FeedbackResponse {
|
|
99
|
-
status: string;
|
|
100
|
-
}
|
|
101
|
-
/**
|
|
102
|
-
* UI customization configuration
|
|
103
|
-
*/
|
|
104
|
-
interface UICustomization {
|
|
105
|
-
header: {
|
|
106
|
-
title?: string;
|
|
107
|
-
subtitle?: string;
|
|
108
|
-
logo?: string;
|
|
109
|
-
};
|
|
110
|
-
welcome_message?: string;
|
|
111
|
-
}
|
|
112
|
-
/**
|
|
113
|
-
* Configuration response from the API
|
|
114
|
-
*/
|
|
115
|
-
interface ConfigResponse {
|
|
116
|
-
feedback: {
|
|
117
|
-
like: {
|
|
118
|
-
enabled: boolean;
|
|
119
|
-
form: RJSFSchema | null;
|
|
120
|
-
};
|
|
121
|
-
dislike: {
|
|
122
|
-
enabled: boolean;
|
|
123
|
-
form: RJSFSchema | null;
|
|
124
|
-
};
|
|
125
|
-
};
|
|
126
|
-
customization: UICustomization | null;
|
|
127
|
-
}
|
|
128
|
-
/**
|
|
129
|
-
* Configuration for the client
|
|
130
|
-
*/
|
|
131
|
-
interface ClientConfig {
|
|
132
|
-
baseUrl?: string;
|
|
133
|
-
}
|
|
134
|
-
/**
|
|
135
|
-
* Callbacks for handling streaming responses
|
|
136
|
-
*/
|
|
137
|
-
interface StreamCallbacks<T, E = Error> {
|
|
138
|
-
onMessage: (data: T) => void | Promise<void>;
|
|
139
|
-
onError: (error: E) => void | Promise<void>;
|
|
140
|
-
onClose?: () => void | Promise<void>;
|
|
141
|
-
}
|
|
142
|
-
/**
|
|
143
|
-
* Regular API endpoint definitions with their request/response types
|
|
144
|
-
*/
|
|
145
|
-
interface ApiEndpoints {
|
|
146
|
-
'/api/config': {
|
|
147
|
-
method: 'GET';
|
|
148
|
-
request: never;
|
|
149
|
-
response: ConfigResponse;
|
|
150
|
-
};
|
|
151
|
-
'/api/feedback': {
|
|
152
|
-
method: 'POST';
|
|
153
|
-
request: FeedbackRequest;
|
|
154
|
-
response: FeedbackResponse;
|
|
155
|
-
};
|
|
156
|
-
}
|
|
157
|
-
/**
|
|
158
|
-
* Streaming API endpoint definitions with their request/stream response types
|
|
159
|
-
*/
|
|
160
|
-
interface StreamingEndpoints {
|
|
161
|
-
'/api/chat': {
|
|
162
|
-
method: 'POST';
|
|
163
|
-
request: ChatRequest;
|
|
164
|
-
stream: TypedChatResponse;
|
|
165
|
-
};
|
|
166
|
-
}
|
|
167
|
-
/**
|
|
168
|
-
* Extract endpoint paths as a union type
|
|
169
|
-
*/
|
|
170
|
-
type ApiEndpointPath = keyof ApiEndpoints;
|
|
171
|
-
/**
|
|
172
|
-
* Extract streaming endpoint paths as a union type
|
|
173
|
-
*/
|
|
174
|
-
type StreamingEndpointPath = keyof StreamingEndpoints;
|
|
175
|
-
/**
|
|
176
|
-
* Extract request type for a specific API endpoint
|
|
177
|
-
*/
|
|
178
|
-
type ApiEndpointRequest<T extends ApiEndpointPath> = ApiEndpoints[T]['request'];
|
|
179
|
-
/**
|
|
180
|
-
* Extract response type for a specific API endpoint
|
|
181
|
-
*/
|
|
182
|
-
type ApiEndpointResponse<T extends ApiEndpointPath> = ApiEndpoints[T]['response'];
|
|
183
|
-
/**
|
|
184
|
-
* Extract HTTP method for a specific API endpoint
|
|
185
|
-
*/
|
|
186
|
-
type ApiEndpointMethod<T extends ApiEndpointPath> = ApiEndpoints[T]['method'];
|
|
187
|
-
/**
|
|
188
|
-
* Extract request type for a specific streaming endpoint
|
|
189
|
-
*/
|
|
190
|
-
type StreamingEndpointRequest<T extends StreamingEndpointPath> = StreamingEndpoints[T]['request'];
|
|
191
|
-
/**
|
|
192
|
-
* Extract stream response type for a specific streaming endpoint
|
|
193
|
-
*/
|
|
194
|
-
type StreamingEndpointStream<T extends StreamingEndpointPath> = StreamingEndpoints[T]['stream'];
|
|
195
|
-
/**
|
|
196
|
-
* Extract HTTP method for a specific streaming endpoint
|
|
197
|
-
*/
|
|
198
|
-
type StreamingEndpointMethod<T extends StreamingEndpointPath> = StreamingEndpoints[T]['method'];
|
|
199
|
-
/**
|
|
200
|
-
* Generic request options for API endpoints with typed methods and body
|
|
201
|
-
*/
|
|
202
|
-
interface TypedApiRequestOptions<T extends ApiEndpointPath> {
|
|
203
|
-
method?: ApiEndpointMethod<T>;
|
|
204
|
-
body?: ApiEndpointRequest<T> extends never ? undefined : ApiEndpointRequest<T>;
|
|
205
|
-
headers?: Record<string, string>;
|
|
206
|
-
signal?: AbortSignal;
|
|
207
|
-
}
|
|
208
|
-
/**
|
|
209
|
-
* Typed request options for specific streaming endpoints
|
|
210
|
-
*/
|
|
211
|
-
interface TypedStreamRequestOptions<T extends StreamingEndpointPath> {
|
|
212
|
-
method?: StreamingEndpointMethod<T>;
|
|
213
|
-
body?: StreamingEndpointRequest<T>;
|
|
214
|
-
headers?: Record<string, string>;
|
|
215
|
-
signal?: AbortSignal;
|
|
216
|
-
}
|
|
217
|
-
|
|
218
|
-
/**
|
|
219
|
-
* Client for communicating with the Ragbits API
|
|
220
|
-
*/
|
|
221
|
-
declare class RagbitsClient {
|
|
222
|
-
private readonly baseUrl;
|
|
223
|
-
/**
|
|
224
|
-
* @param config - Configuration object
|
|
225
|
-
*/
|
|
226
|
-
constructor(config?: ClientConfig);
|
|
227
|
-
/**
|
|
228
|
-
* Get the base URL used by this client
|
|
229
|
-
*/
|
|
230
|
-
getBaseUrl(): string;
|
|
231
|
-
/**
|
|
232
|
-
* Build full API URL from path
|
|
233
|
-
* @private
|
|
234
|
-
*/
|
|
235
|
-
private _buildApiUrl;
|
|
236
|
-
/**
|
|
237
|
-
* Make a request to the API
|
|
238
|
-
* @private
|
|
239
|
-
*/
|
|
240
|
-
private _makeRequest;
|
|
241
|
-
/**
|
|
242
|
-
* Method to make API requests to known endpoints only
|
|
243
|
-
* @param endpoint - API endpoint path (must be predefined)
|
|
244
|
-
* @param options - Typed request options for the specific endpoint
|
|
245
|
-
*/
|
|
246
|
-
makeRequest<T extends ApiEndpointPath>(endpoint: T, options?: TypedApiRequestOptions<T>): Promise<ApiEndpointResponse<T>>;
|
|
247
|
-
/**
|
|
248
|
-
* Method for streaming requests to known endpoints only
|
|
249
|
-
* @param endpoint - Streaming endpoint path (must be predefined)
|
|
250
|
-
* @param data - Request data
|
|
251
|
-
* @param callbacks - Stream callbacks
|
|
252
|
-
* @param signal - Optional AbortSignal for cancelling the request
|
|
253
|
-
*/
|
|
254
|
-
makeStreamRequest<T extends StreamingEndpointPath>(endpoint: T, data: StreamingEndpointRequest<T>, callbacks: StreamCallbacks<StreamingEndpointStream<T>>, signal?: AbortSignal): () => void;
|
|
255
|
-
}
|
|
256
|
-
|
|
257
|
-
export { type ApiEndpointMethod, type ApiEndpointPath, type ApiEndpointRequest, type ApiEndpointResponse, type ApiEndpoints, type ChatRequest, ChatResponseType, type ClientConfig, type ConfigResponse, type FeedbackRequest, type FeedbackResponse, FeedbackType, type Message, MessageRole, RagbitsClient, type Reference, type ServerState, type StreamCallbacks, type StreamingEndpointMethod, type StreamingEndpointPath, type StreamingEndpointRequest, type StreamingEndpointStream, type StreamingEndpoints, type TypedApiRequestOptions, type TypedChatResponse, type TypedStreamRequestOptions, type UICustomization };
|
package/eslint.config.js
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
import js from '@eslint/js'
|
|
2
|
-
import globals from 'globals'
|
|
3
|
-
import tseslint from 'typescript-eslint'
|
|
4
|
-
|
|
5
|
-
export default tseslint.config(
|
|
6
|
-
{ ignores: ['dist', 'coverage'] },
|
|
7
|
-
{
|
|
8
|
-
extends: [js.configs.recommended, ...tseslint.configs.recommended],
|
|
9
|
-
files: ['**/*.{ts,tsx}'],
|
|
10
|
-
languageOptions: {
|
|
11
|
-
ecmaVersion: 2020,
|
|
12
|
-
globals: globals.node,
|
|
13
|
-
},
|
|
14
|
-
rules: {
|
|
15
|
-
'@typescript-eslint/no-unused-vars': [
|
|
16
|
-
'error',
|
|
17
|
-
{ argsIgnorePattern: '^_', varsIgnorePattern: '^_' },
|
|
18
|
-
],
|
|
19
|
-
},
|
|
20
|
-
}
|
|
21
|
-
)
|