@ragbits/api-client 0.0.1 → 0.0.2
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/dist/index.cjs +211 -0
- package/dist/index.d.cts +257 -0
- package/dist/index.d.ts +257 -0
- package/dist/index.js +181 -0
- package/package.json +1 -1
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var index_exports = {};
|
|
22
|
+
__export(index_exports, {
|
|
23
|
+
ChatResponseType: () => ChatResponseType,
|
|
24
|
+
FeedbackType: () => FeedbackType,
|
|
25
|
+
MessageRole: () => MessageRole,
|
|
26
|
+
RagbitsClient: () => RagbitsClient
|
|
27
|
+
});
|
|
28
|
+
module.exports = __toCommonJS(index_exports);
|
|
29
|
+
|
|
30
|
+
// src/types.ts
|
|
31
|
+
var MessageRole = /* @__PURE__ */ ((MessageRole2) => {
|
|
32
|
+
MessageRole2["USER"] = "user";
|
|
33
|
+
MessageRole2["ASSISTANT"] = "assistant";
|
|
34
|
+
MessageRole2["SYSTEM"] = "system";
|
|
35
|
+
return MessageRole2;
|
|
36
|
+
})(MessageRole || {});
|
|
37
|
+
var ChatResponseType = /* @__PURE__ */ ((ChatResponseType2) => {
|
|
38
|
+
ChatResponseType2["MESSAGE"] = "message";
|
|
39
|
+
ChatResponseType2["REFERENCE"] = "reference";
|
|
40
|
+
ChatResponseType2["STATE_UPDATE"] = "state_update";
|
|
41
|
+
ChatResponseType2["TEXT"] = "text";
|
|
42
|
+
ChatResponseType2["MESSAGE_ID"] = "message_id";
|
|
43
|
+
ChatResponseType2["CONVERSATION_ID"] = "conversation_id";
|
|
44
|
+
return ChatResponseType2;
|
|
45
|
+
})(ChatResponseType || {});
|
|
46
|
+
var FeedbackType = /* @__PURE__ */ ((FeedbackType2) => {
|
|
47
|
+
FeedbackType2["LIKE"] = "like";
|
|
48
|
+
FeedbackType2["DISLIKE"] = "dislike";
|
|
49
|
+
return FeedbackType2;
|
|
50
|
+
})(FeedbackType || {});
|
|
51
|
+
|
|
52
|
+
// src/index.ts
|
|
53
|
+
var RagbitsClient = class {
|
|
54
|
+
/**
|
|
55
|
+
* @param config - Configuration object
|
|
56
|
+
*/
|
|
57
|
+
constructor(config = {}) {
|
|
58
|
+
this.baseUrl = config.baseUrl || "http://127.0.0.1:8000";
|
|
59
|
+
try {
|
|
60
|
+
new URL(this.baseUrl);
|
|
61
|
+
} catch {
|
|
62
|
+
throw new Error(
|
|
63
|
+
`Invalid base URL: ${this.baseUrl}. Please provide a valid URL.`
|
|
64
|
+
);
|
|
65
|
+
}
|
|
66
|
+
if (this.baseUrl.endsWith("/")) {
|
|
67
|
+
this.baseUrl = this.baseUrl.slice(0, -1);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Get the base URL used by this client
|
|
72
|
+
*/
|
|
73
|
+
getBaseUrl() {
|
|
74
|
+
return this.baseUrl;
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Build full API URL from path
|
|
78
|
+
* @private
|
|
79
|
+
*/
|
|
80
|
+
_buildApiUrl(path) {
|
|
81
|
+
return `${this.baseUrl}${path}`;
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Make a request to the API
|
|
85
|
+
* @private
|
|
86
|
+
*/
|
|
87
|
+
async _makeRequest(url, options = {}) {
|
|
88
|
+
const defaultOptions = {
|
|
89
|
+
headers: {
|
|
90
|
+
"Content-Type": "application/json"
|
|
91
|
+
}
|
|
92
|
+
};
|
|
93
|
+
const response = await fetch(url, { ...defaultOptions, ...options });
|
|
94
|
+
if (!response.ok) {
|
|
95
|
+
throw new Error(`HTTP error! status: ${response.status}`);
|
|
96
|
+
}
|
|
97
|
+
return response;
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* Method to make API requests to known endpoints only
|
|
101
|
+
* @param endpoint - API endpoint path (must be predefined)
|
|
102
|
+
* @param options - Typed request options for the specific endpoint
|
|
103
|
+
*/
|
|
104
|
+
async makeRequest(endpoint, options) {
|
|
105
|
+
const {
|
|
106
|
+
method = "GET",
|
|
107
|
+
body,
|
|
108
|
+
headers = {},
|
|
109
|
+
...restOptions
|
|
110
|
+
} = options || {};
|
|
111
|
+
const requestOptions = {
|
|
112
|
+
method,
|
|
113
|
+
headers,
|
|
114
|
+
...restOptions
|
|
115
|
+
// This will include signal and other fetch options
|
|
116
|
+
};
|
|
117
|
+
if (body && method !== "GET") {
|
|
118
|
+
requestOptions.body = typeof body === "string" ? body : JSON.stringify(body);
|
|
119
|
+
}
|
|
120
|
+
const response = await this._makeRequest(
|
|
121
|
+
this._buildApiUrl(endpoint),
|
|
122
|
+
requestOptions
|
|
123
|
+
);
|
|
124
|
+
return response.json();
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* Method for streaming requests to known endpoints only
|
|
128
|
+
* @param endpoint - Streaming endpoint path (must be predefined)
|
|
129
|
+
* @param data - Request data
|
|
130
|
+
* @param callbacks - Stream callbacks
|
|
131
|
+
* @param signal - Optional AbortSignal for cancelling the request
|
|
132
|
+
*/
|
|
133
|
+
makeStreamRequest(endpoint, data, callbacks, signal) {
|
|
134
|
+
let isCancelled = false;
|
|
135
|
+
const processStream = async (response) => {
|
|
136
|
+
const reader = response.body?.pipeThrough(new TextDecoderStream()).getReader();
|
|
137
|
+
if (!reader) {
|
|
138
|
+
throw new Error("Response body is null");
|
|
139
|
+
}
|
|
140
|
+
while (!isCancelled && !signal?.aborted) {
|
|
141
|
+
try {
|
|
142
|
+
const { value, done } = await reader.read();
|
|
143
|
+
if (done) {
|
|
144
|
+
callbacks.onClose?.();
|
|
145
|
+
break;
|
|
146
|
+
}
|
|
147
|
+
const lines = value.split("\n");
|
|
148
|
+
for (const line of lines) {
|
|
149
|
+
if (!line.startsWith("data: ")) continue;
|
|
150
|
+
try {
|
|
151
|
+
const jsonString = line.replace("data: ", "").trim();
|
|
152
|
+
const parsedData = JSON.parse(
|
|
153
|
+
jsonString
|
|
154
|
+
);
|
|
155
|
+
await callbacks.onMessage(parsedData);
|
|
156
|
+
} catch (parseError) {
|
|
157
|
+
console.error("Error parsing JSON:", parseError);
|
|
158
|
+
await callbacks.onError(
|
|
159
|
+
new Error("Error processing server response")
|
|
160
|
+
);
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
} catch (streamError) {
|
|
164
|
+
console.error("Stream error:", streamError);
|
|
165
|
+
await callbacks.onError(new Error("Error reading stream"));
|
|
166
|
+
break;
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
};
|
|
170
|
+
const startStream = async () => {
|
|
171
|
+
try {
|
|
172
|
+
const response = await fetch(this._buildApiUrl(endpoint), {
|
|
173
|
+
method: "POST",
|
|
174
|
+
headers: {
|
|
175
|
+
"Content-Type": "application/json",
|
|
176
|
+
Accept: "text/event-stream"
|
|
177
|
+
},
|
|
178
|
+
body: JSON.stringify(data),
|
|
179
|
+
signal
|
|
180
|
+
});
|
|
181
|
+
if (!response.ok) {
|
|
182
|
+
throw new Error(`HTTP error! status: ${response.status}`);
|
|
183
|
+
}
|
|
184
|
+
await processStream(response);
|
|
185
|
+
} catch (error) {
|
|
186
|
+
if (signal?.aborted) {
|
|
187
|
+
return;
|
|
188
|
+
}
|
|
189
|
+
console.error("Request error:", error);
|
|
190
|
+
const errorMessage = error instanceof Error ? error.message : "Error connecting to server";
|
|
191
|
+
await callbacks.onError(new Error(errorMessage));
|
|
192
|
+
}
|
|
193
|
+
};
|
|
194
|
+
try {
|
|
195
|
+
startStream();
|
|
196
|
+
} catch (error) {
|
|
197
|
+
const errorMessage = error instanceof Error ? error.message : "Failed to start stream";
|
|
198
|
+
callbacks.onError(new Error(errorMessage));
|
|
199
|
+
}
|
|
200
|
+
return () => {
|
|
201
|
+
isCancelled = true;
|
|
202
|
+
};
|
|
203
|
+
}
|
|
204
|
+
};
|
|
205
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
206
|
+
0 && (module.exports = {
|
|
207
|
+
ChatResponseType,
|
|
208
|
+
FeedbackType,
|
|
209
|
+
MessageRole,
|
|
210
|
+
RagbitsClient
|
|
211
|
+
});
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,257 @@
|
|
|
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/dist/index.d.ts
ADDED
|
@@ -0,0 +1,257 @@
|
|
|
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/dist/index.js
ADDED
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
// src/types.ts
|
|
2
|
+
var MessageRole = /* @__PURE__ */ ((MessageRole2) => {
|
|
3
|
+
MessageRole2["USER"] = "user";
|
|
4
|
+
MessageRole2["ASSISTANT"] = "assistant";
|
|
5
|
+
MessageRole2["SYSTEM"] = "system";
|
|
6
|
+
return MessageRole2;
|
|
7
|
+
})(MessageRole || {});
|
|
8
|
+
var ChatResponseType = /* @__PURE__ */ ((ChatResponseType2) => {
|
|
9
|
+
ChatResponseType2["MESSAGE"] = "message";
|
|
10
|
+
ChatResponseType2["REFERENCE"] = "reference";
|
|
11
|
+
ChatResponseType2["STATE_UPDATE"] = "state_update";
|
|
12
|
+
ChatResponseType2["TEXT"] = "text";
|
|
13
|
+
ChatResponseType2["MESSAGE_ID"] = "message_id";
|
|
14
|
+
ChatResponseType2["CONVERSATION_ID"] = "conversation_id";
|
|
15
|
+
return ChatResponseType2;
|
|
16
|
+
})(ChatResponseType || {});
|
|
17
|
+
var FeedbackType = /* @__PURE__ */ ((FeedbackType2) => {
|
|
18
|
+
FeedbackType2["LIKE"] = "like";
|
|
19
|
+
FeedbackType2["DISLIKE"] = "dislike";
|
|
20
|
+
return FeedbackType2;
|
|
21
|
+
})(FeedbackType || {});
|
|
22
|
+
|
|
23
|
+
// src/index.ts
|
|
24
|
+
var RagbitsClient = class {
|
|
25
|
+
/**
|
|
26
|
+
* @param config - Configuration object
|
|
27
|
+
*/
|
|
28
|
+
constructor(config = {}) {
|
|
29
|
+
this.baseUrl = config.baseUrl || "http://127.0.0.1:8000";
|
|
30
|
+
try {
|
|
31
|
+
new URL(this.baseUrl);
|
|
32
|
+
} catch {
|
|
33
|
+
throw new Error(
|
|
34
|
+
`Invalid base URL: ${this.baseUrl}. Please provide a valid URL.`
|
|
35
|
+
);
|
|
36
|
+
}
|
|
37
|
+
if (this.baseUrl.endsWith("/")) {
|
|
38
|
+
this.baseUrl = this.baseUrl.slice(0, -1);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Get the base URL used by this client
|
|
43
|
+
*/
|
|
44
|
+
getBaseUrl() {
|
|
45
|
+
return this.baseUrl;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Build full API URL from path
|
|
49
|
+
* @private
|
|
50
|
+
*/
|
|
51
|
+
_buildApiUrl(path) {
|
|
52
|
+
return `${this.baseUrl}${path}`;
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Make a request to the API
|
|
56
|
+
* @private
|
|
57
|
+
*/
|
|
58
|
+
async _makeRequest(url, options = {}) {
|
|
59
|
+
const defaultOptions = {
|
|
60
|
+
headers: {
|
|
61
|
+
"Content-Type": "application/json"
|
|
62
|
+
}
|
|
63
|
+
};
|
|
64
|
+
const response = await fetch(url, { ...defaultOptions, ...options });
|
|
65
|
+
if (!response.ok) {
|
|
66
|
+
throw new Error(`HTTP error! status: ${response.status}`);
|
|
67
|
+
}
|
|
68
|
+
return response;
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Method to make API requests to known endpoints only
|
|
72
|
+
* @param endpoint - API endpoint path (must be predefined)
|
|
73
|
+
* @param options - Typed request options for the specific endpoint
|
|
74
|
+
*/
|
|
75
|
+
async makeRequest(endpoint, options) {
|
|
76
|
+
const {
|
|
77
|
+
method = "GET",
|
|
78
|
+
body,
|
|
79
|
+
headers = {},
|
|
80
|
+
...restOptions
|
|
81
|
+
} = options || {};
|
|
82
|
+
const requestOptions = {
|
|
83
|
+
method,
|
|
84
|
+
headers,
|
|
85
|
+
...restOptions
|
|
86
|
+
// This will include signal and other fetch options
|
|
87
|
+
};
|
|
88
|
+
if (body && method !== "GET") {
|
|
89
|
+
requestOptions.body = typeof body === "string" ? body : JSON.stringify(body);
|
|
90
|
+
}
|
|
91
|
+
const response = await this._makeRequest(
|
|
92
|
+
this._buildApiUrl(endpoint),
|
|
93
|
+
requestOptions
|
|
94
|
+
);
|
|
95
|
+
return response.json();
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Method for streaming requests to known endpoints only
|
|
99
|
+
* @param endpoint - Streaming endpoint path (must be predefined)
|
|
100
|
+
* @param data - Request data
|
|
101
|
+
* @param callbacks - Stream callbacks
|
|
102
|
+
* @param signal - Optional AbortSignal for cancelling the request
|
|
103
|
+
*/
|
|
104
|
+
makeStreamRequest(endpoint, data, callbacks, signal) {
|
|
105
|
+
let isCancelled = false;
|
|
106
|
+
const processStream = async (response) => {
|
|
107
|
+
const reader = response.body?.pipeThrough(new TextDecoderStream()).getReader();
|
|
108
|
+
if (!reader) {
|
|
109
|
+
throw new Error("Response body is null");
|
|
110
|
+
}
|
|
111
|
+
while (!isCancelled && !signal?.aborted) {
|
|
112
|
+
try {
|
|
113
|
+
const { value, done } = await reader.read();
|
|
114
|
+
if (done) {
|
|
115
|
+
callbacks.onClose?.();
|
|
116
|
+
break;
|
|
117
|
+
}
|
|
118
|
+
const lines = value.split("\n");
|
|
119
|
+
for (const line of lines) {
|
|
120
|
+
if (!line.startsWith("data: ")) continue;
|
|
121
|
+
try {
|
|
122
|
+
const jsonString = line.replace("data: ", "").trim();
|
|
123
|
+
const parsedData = JSON.parse(
|
|
124
|
+
jsonString
|
|
125
|
+
);
|
|
126
|
+
await callbacks.onMessage(parsedData);
|
|
127
|
+
} catch (parseError) {
|
|
128
|
+
console.error("Error parsing JSON:", parseError);
|
|
129
|
+
await callbacks.onError(
|
|
130
|
+
new Error("Error processing server response")
|
|
131
|
+
);
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
} catch (streamError) {
|
|
135
|
+
console.error("Stream error:", streamError);
|
|
136
|
+
await callbacks.onError(new Error("Error reading stream"));
|
|
137
|
+
break;
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
};
|
|
141
|
+
const startStream = async () => {
|
|
142
|
+
try {
|
|
143
|
+
const response = await fetch(this._buildApiUrl(endpoint), {
|
|
144
|
+
method: "POST",
|
|
145
|
+
headers: {
|
|
146
|
+
"Content-Type": "application/json",
|
|
147
|
+
Accept: "text/event-stream"
|
|
148
|
+
},
|
|
149
|
+
body: JSON.stringify(data),
|
|
150
|
+
signal
|
|
151
|
+
});
|
|
152
|
+
if (!response.ok) {
|
|
153
|
+
throw new Error(`HTTP error! status: ${response.status}`);
|
|
154
|
+
}
|
|
155
|
+
await processStream(response);
|
|
156
|
+
} catch (error) {
|
|
157
|
+
if (signal?.aborted) {
|
|
158
|
+
return;
|
|
159
|
+
}
|
|
160
|
+
console.error("Request error:", error);
|
|
161
|
+
const errorMessage = error instanceof Error ? error.message : "Error connecting to server";
|
|
162
|
+
await callbacks.onError(new Error(errorMessage));
|
|
163
|
+
}
|
|
164
|
+
};
|
|
165
|
+
try {
|
|
166
|
+
startStream();
|
|
167
|
+
} catch (error) {
|
|
168
|
+
const errorMessage = error instanceof Error ? error.message : "Failed to start stream";
|
|
169
|
+
callbacks.onError(new Error(errorMessage));
|
|
170
|
+
}
|
|
171
|
+
return () => {
|
|
172
|
+
isCancelled = true;
|
|
173
|
+
};
|
|
174
|
+
}
|
|
175
|
+
};
|
|
176
|
+
export {
|
|
177
|
+
ChatResponseType,
|
|
178
|
+
FeedbackType,
|
|
179
|
+
MessageRole,
|
|
180
|
+
RagbitsClient
|
|
181
|
+
};
|