@ragbits/api-client 1.4.0-dev.202512021005 → 1.4.0-dev.202512050236
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/__tests__/mocks/handlers.ts +9 -5
- package/dist/autogen.types.d.ts +28 -1
- package/dist/index.cjs +26 -6
- package/dist/index.d.ts +4 -4
- package/dist/index.js +26 -6
- package/dist/types.d.ts +65 -11
- package/package.json +1 -1
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { http, HttpResponse } from 'msw'
|
|
2
2
|
import { defaultConfigResponse } from '../utils'
|
|
3
|
+
import { ChatResponse } from '../../src'
|
|
3
4
|
|
|
4
5
|
export const handlers = [
|
|
5
6
|
// Config endpoint with conditional error handling
|
|
@@ -65,11 +66,14 @@ export const handlers = [
|
|
|
65
66
|
|
|
66
67
|
const stream = new ReadableStream({
|
|
67
68
|
start(controller) {
|
|
68
|
-
const messages = [
|
|
69
|
-
{ type: 'text', content: 'Hello there!' },
|
|
70
|
-
{ type: 'text', content: 'How can I help you?' },
|
|
71
|
-
{ type: 'message_id', content: 'msg-123' },
|
|
72
|
-
{
|
|
69
|
+
const messages: ChatResponse[] = [
|
|
70
|
+
{ type: 'text', content: { text: 'Hello there!' } },
|
|
71
|
+
{ type: 'text', content: { text: 'How can I help you?' } },
|
|
72
|
+
{ type: 'message_id', content: { message_id: 'msg-123' } },
|
|
73
|
+
{
|
|
74
|
+
type: 'conversation_id',
|
|
75
|
+
content: { conversation_id: 'conv-456' },
|
|
76
|
+
},
|
|
73
77
|
]
|
|
74
78
|
|
|
75
79
|
messages.forEach((message, index) => {
|
package/dist/autogen.types.d.ts
CHANGED
|
@@ -60,6 +60,12 @@ export interface ChatContext {
|
|
|
60
60
|
};
|
|
61
61
|
user: User | null;
|
|
62
62
|
session_id: string | null;
|
|
63
|
+
/**
|
|
64
|
+
* List of confirmed/declined tools from the frontend
|
|
65
|
+
*/
|
|
66
|
+
confirmed_tools: {
|
|
67
|
+
[k: string]: unknown;
|
|
68
|
+
}[] | null;
|
|
63
69
|
[k: string]: unknown;
|
|
64
70
|
}
|
|
65
71
|
/**
|
|
@@ -164,6 +170,17 @@ export interface Task {
|
|
|
164
170
|
full_response: string | null;
|
|
165
171
|
dependencies: string[];
|
|
166
172
|
}
|
|
173
|
+
/**
|
|
174
|
+
* Represents a tool confirmation request sent to the user.
|
|
175
|
+
*/
|
|
176
|
+
export interface ConfirmationRequest {
|
|
177
|
+
confirmation_id: string;
|
|
178
|
+
tool_name: string;
|
|
179
|
+
tool_description: string;
|
|
180
|
+
arguments: {
|
|
181
|
+
[k: string]: unknown;
|
|
182
|
+
};
|
|
183
|
+
}
|
|
167
184
|
/**
|
|
168
185
|
* Text content wrapper.
|
|
169
186
|
*/
|
|
@@ -208,6 +225,12 @@ export interface UsageContent {
|
|
|
208
225
|
export interface TodoItemContent {
|
|
209
226
|
task: Task;
|
|
210
227
|
}
|
|
228
|
+
/**
|
|
229
|
+
* Confirmation request content wrapper.
|
|
230
|
+
*/
|
|
231
|
+
export interface ConfirmationRequestContent {
|
|
232
|
+
confirmation_request: ConfirmationRequest;
|
|
233
|
+
}
|
|
211
234
|
/**
|
|
212
235
|
* Customization for the header section of the UI.
|
|
213
236
|
*/
|
|
@@ -485,6 +508,10 @@ export interface ConversationSummaryResponse {
|
|
|
485
508
|
type: 'conversation_summary';
|
|
486
509
|
content: ConversationSummaryContent;
|
|
487
510
|
}
|
|
511
|
+
export interface ConfirmationRequestChatResponse {
|
|
512
|
+
type: 'confirmation_request';
|
|
513
|
+
content: ConfirmationRequestContent;
|
|
514
|
+
}
|
|
488
515
|
export interface ChunkedChatResponse {
|
|
489
516
|
type: 'chunked_content';
|
|
490
517
|
content: ChunkedContent;
|
|
@@ -492,4 +519,4 @@ export interface ChunkedChatResponse {
|
|
|
492
519
|
/**
|
|
493
520
|
* Typed chat response union
|
|
494
521
|
*/
|
|
495
|
-
export type ChatResponse = TextChatResponse | ReferenceChatResponse | MessageIdChatResponse | ConversationIdChatResponse | StateUpdateChatResponse | LiveUpdateChatResponse | FollowupMessagesChatResponse | ImageChatResponse | MessageUsageChatResponse | ClearMessageChatResponse | TodoItemChatResonse | ConversationSummaryResponse;
|
|
522
|
+
export type ChatResponse = TextChatResponse | ReferenceChatResponse | MessageIdChatResponse | ConversationIdChatResponse | StateUpdateChatResponse | LiveUpdateChatResponse | FollowupMessagesChatResponse | ImageChatResponse | MessageUsageChatResponse | ClearMessageChatResponse | TodoItemChatResonse | ConversationSummaryResponse | ConfirmationRequestChatResponse;
|
package/dist/index.cjs
CHANGED
|
@@ -120,10 +120,13 @@ var RagbitsClient = class {
|
|
|
120
120
|
* @param endpoint - API endpoint path
|
|
121
121
|
* @param options - Typed request options for the specific endpoint
|
|
122
122
|
*/
|
|
123
|
-
async makeRequest(endpoint,
|
|
123
|
+
async makeRequest(endpoint, ...args) {
|
|
124
|
+
const options = args[0];
|
|
124
125
|
const {
|
|
125
126
|
method = "GET",
|
|
126
127
|
body,
|
|
128
|
+
pathParams,
|
|
129
|
+
queryParams,
|
|
127
130
|
headers = {},
|
|
128
131
|
...restOptions
|
|
129
132
|
} = options || {};
|
|
@@ -131,15 +134,32 @@ var RagbitsClient = class {
|
|
|
131
134
|
method,
|
|
132
135
|
headers,
|
|
133
136
|
...restOptions
|
|
134
|
-
// This will include signal and other fetch options
|
|
135
137
|
};
|
|
136
138
|
if (body && method !== "GET") {
|
|
137
139
|
requestOptions.body = typeof body === "string" ? body : JSON.stringify(body);
|
|
138
140
|
}
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
141
|
+
let url = endpoint.toString();
|
|
142
|
+
if (pathParams) {
|
|
143
|
+
url = url.replace(/:([^/]+)/g, (_, paramName) => {
|
|
144
|
+
if (paramName in pathParams) {
|
|
145
|
+
const value = pathParams[paramName];
|
|
146
|
+
return encodeURIComponent(String(value));
|
|
147
|
+
} else {
|
|
148
|
+
throw new Error(`Path parameter ${paramName} is required`);
|
|
149
|
+
}
|
|
150
|
+
});
|
|
151
|
+
}
|
|
152
|
+
if (queryParams && Object.keys(queryParams).length > 0) {
|
|
153
|
+
const searchParams = new URLSearchParams();
|
|
154
|
+
for (const [key, value] of Object.entries(queryParams)) {
|
|
155
|
+
if (value !== void 0 && value !== null) {
|
|
156
|
+
searchParams.append(key, String(value));
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
url += `?${searchParams.toString()}`;
|
|
160
|
+
}
|
|
161
|
+
url = this._buildApiUrl(url);
|
|
162
|
+
const response = await this._makeRequest(url, requestOptions);
|
|
143
163
|
return response.json();
|
|
144
164
|
}
|
|
145
165
|
/**
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { ClientConfig, StreamCallbacks, BaseApiEndpoints, EndpointDefinition, EndpointResponse,
|
|
1
|
+
import type { ClientConfig, StreamCallbacks, BaseApiEndpoints, EndpointDefinition, EndpointResponse, BaseStreamingEndpoints, EndpointRequest, MakeRequestOptions } from './types';
|
|
2
2
|
/**
|
|
3
3
|
* Client for communicating with the Ragbits API
|
|
4
4
|
*/
|
|
@@ -30,8 +30,8 @@ export declare class RagbitsClient {
|
|
|
30
30
|
* @param options - Typed request options for the specific endpoint
|
|
31
31
|
*/
|
|
32
32
|
makeRequest<Endpoints extends {
|
|
33
|
-
[K in keyof Endpoints]: EndpointDefinition
|
|
34
|
-
} = BaseApiEndpoints, URL extends keyof Endpoints = keyof Endpoints>(endpoint: URL,
|
|
33
|
+
[K in keyof Endpoints]: EndpointDefinition<any, any, any, any>;
|
|
34
|
+
} = BaseApiEndpoints, URL extends keyof Endpoints = keyof Endpoints>(endpoint: URL, ...args: MakeRequestOptions<URL, Endpoints>): Promise<EndpointResponse<URL, Endpoints>>;
|
|
35
35
|
/**
|
|
36
36
|
* Method for streaming requests to known endpoints only
|
|
37
37
|
* @param endpoint - Streaming endpoint path
|
|
@@ -40,7 +40,7 @@ export declare class RagbitsClient {
|
|
|
40
40
|
* @param signal - Optional AbortSignal for cancelling the request
|
|
41
41
|
*/
|
|
42
42
|
makeStreamRequest<Endpoints extends {
|
|
43
|
-
[K in keyof Endpoints]: EndpointDefinition
|
|
43
|
+
[K in keyof Endpoints]: EndpointDefinition<any, any, any, any>;
|
|
44
44
|
} = BaseStreamingEndpoints, URL extends keyof Endpoints = keyof Endpoints>(endpoint: URL, data: EndpointRequest<URL, Endpoints>, callbacks: StreamCallbacks<EndpointResponse<URL, Endpoints>>, signal?: AbortSignal, customHeaders?: Record<string, string>): () => void;
|
|
45
45
|
private normalizeHeaders;
|
|
46
46
|
private handleChunkedContent;
|
package/dist/index.js
CHANGED
|
@@ -89,10 +89,13 @@ var RagbitsClient = class {
|
|
|
89
89
|
* @param endpoint - API endpoint path
|
|
90
90
|
* @param options - Typed request options for the specific endpoint
|
|
91
91
|
*/
|
|
92
|
-
async makeRequest(endpoint,
|
|
92
|
+
async makeRequest(endpoint, ...args) {
|
|
93
|
+
const options = args[0];
|
|
93
94
|
const {
|
|
94
95
|
method = "GET",
|
|
95
96
|
body,
|
|
97
|
+
pathParams,
|
|
98
|
+
queryParams,
|
|
96
99
|
headers = {},
|
|
97
100
|
...restOptions
|
|
98
101
|
} = options || {};
|
|
@@ -100,15 +103,32 @@ var RagbitsClient = class {
|
|
|
100
103
|
method,
|
|
101
104
|
headers,
|
|
102
105
|
...restOptions
|
|
103
|
-
// This will include signal and other fetch options
|
|
104
106
|
};
|
|
105
107
|
if (body && method !== "GET") {
|
|
106
108
|
requestOptions.body = typeof body === "string" ? body : JSON.stringify(body);
|
|
107
109
|
}
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
110
|
+
let url = endpoint.toString();
|
|
111
|
+
if (pathParams) {
|
|
112
|
+
url = url.replace(/:([^/]+)/g, (_, paramName) => {
|
|
113
|
+
if (paramName in pathParams) {
|
|
114
|
+
const value = pathParams[paramName];
|
|
115
|
+
return encodeURIComponent(String(value));
|
|
116
|
+
} else {
|
|
117
|
+
throw new Error(`Path parameter ${paramName} is required`);
|
|
118
|
+
}
|
|
119
|
+
});
|
|
120
|
+
}
|
|
121
|
+
if (queryParams && Object.keys(queryParams).length > 0) {
|
|
122
|
+
const searchParams = new URLSearchParams();
|
|
123
|
+
for (const [key, value] of Object.entries(queryParams)) {
|
|
124
|
+
if (value !== void 0 && value !== null) {
|
|
125
|
+
searchParams.append(key, String(value));
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
url += `?${searchParams.toString()}`;
|
|
129
|
+
}
|
|
130
|
+
url = this._buildApiUrl(url);
|
|
131
|
+
const response = await this._makeRequest(url, requestOptions);
|
|
112
132
|
return response.json();
|
|
113
133
|
}
|
|
114
134
|
/**
|
package/dist/types.d.ts
CHANGED
|
@@ -20,10 +20,12 @@ export interface StreamCallbacks<T, E = Error> {
|
|
|
20
20
|
onError: (error: E) => void | Promise<void>;
|
|
21
21
|
onClose?: () => void | Promise<void>;
|
|
22
22
|
}
|
|
23
|
-
export interface EndpointDefinition<Req = any, Res = any> {
|
|
23
|
+
export interface EndpointDefinition<Req = any, Res = any, PathParams = never, QueryParams = never> {
|
|
24
24
|
method: string;
|
|
25
25
|
request: Req;
|
|
26
26
|
response: Res;
|
|
27
|
+
pathParams: PathParams;
|
|
28
|
+
queryParams: QueryParams;
|
|
27
29
|
}
|
|
28
30
|
/**
|
|
29
31
|
* Base predefined API endpoint definitions with their request/response types
|
|
@@ -45,34 +47,86 @@ export interface BaseStreamingEndpoints {
|
|
|
45
47
|
* Extract endpoint paths as a union type
|
|
46
48
|
*/
|
|
47
49
|
export type EndpointPath<Endpoints extends {
|
|
48
|
-
[K in keyof Endpoints]: EndpointDefinition
|
|
50
|
+
[K in keyof Endpoints]: EndpointDefinition<any, any, any, any>;
|
|
49
51
|
}> = keyof Endpoints;
|
|
50
52
|
/**
|
|
51
53
|
* Extract request type for a specific API endpoint
|
|
52
54
|
*/
|
|
53
55
|
export type EndpointRequest<URL extends keyof Endpoints, Endpoints extends {
|
|
54
|
-
[K in keyof Endpoints]: EndpointDefinition
|
|
56
|
+
[K in keyof Endpoints]: EndpointDefinition<any, any, any, any>;
|
|
55
57
|
}> = Endpoints[URL]['request'];
|
|
56
58
|
/**
|
|
57
59
|
* Extract response type for a specific API endpoint
|
|
58
60
|
*/
|
|
59
61
|
export type EndpointResponse<URL extends keyof Endpoints, Endpoints extends {
|
|
60
|
-
[K in keyof Endpoints]: EndpointDefinition
|
|
62
|
+
[K in keyof Endpoints]: EndpointDefinition<any, any, any, any>;
|
|
61
63
|
}> = Endpoints[URL]['response'];
|
|
62
64
|
/**
|
|
63
65
|
* Extract HTTP method for a specific API endpoint
|
|
64
66
|
*/
|
|
65
67
|
export type EndpointMethod<URL extends keyof Endpoints, Endpoints extends {
|
|
66
|
-
[K in keyof Endpoints]: EndpointDefinition
|
|
68
|
+
[K in keyof Endpoints]: EndpointDefinition<any, any, any, any>;
|
|
67
69
|
}> = Endpoints[URL]['method'];
|
|
68
70
|
/**
|
|
69
|
-
*
|
|
71
|
+
* Check if an object type has any required properties
|
|
72
|
+
* - {} extends T means all properties are optional → false
|
|
73
|
+
* - {} doesn't extend T means at least one property is required → true
|
|
70
74
|
*/
|
|
71
|
-
export
|
|
72
|
-
|
|
73
|
-
|
|
75
|
+
export type HasRequiredKeys<T> = [T] extends [never] ? false : {} extends T ? false : true;
|
|
76
|
+
/**
|
|
77
|
+
* Generic request options for API endpoints with typed methods, path params, query params, and body
|
|
78
|
+
* - pathParams is REQUIRED when PathParams is not never
|
|
79
|
+
* - queryParams is REQUIRED when it has required keys inside
|
|
80
|
+
* - queryParams is OPTIONAL when all keys are optional or it's never
|
|
81
|
+
* - body is REQUIRED when it's a specific type (not never, not undefined)
|
|
82
|
+
* - body is OPTIONAL when it's never or undefined
|
|
83
|
+
*/
|
|
84
|
+
export type RequestOptions<URL extends keyof Endpoints, Endpoints extends {
|
|
85
|
+
[K in keyof Endpoints]: EndpointDefinition<any, any, any, any>;
|
|
86
|
+
}> = {
|
|
74
87
|
method?: Endpoints[URL]['method'];
|
|
75
|
-
body?: Endpoints[URL]['request'] extends never ? undefined : Endpoints[URL]['request'];
|
|
76
88
|
headers?: Record<string, string>;
|
|
77
89
|
signal?: AbortSignal;
|
|
78
|
-
}
|
|
90
|
+
} & (Endpoints[URL]['pathParams'] extends never ? {
|
|
91
|
+
pathParams?: never;
|
|
92
|
+
} : {
|
|
93
|
+
pathParams: Endpoints[URL]['pathParams'];
|
|
94
|
+
}) & (Endpoints[URL]['queryParams'] extends never ? {
|
|
95
|
+
queryParams?: never;
|
|
96
|
+
} : HasRequiredKeys<Endpoints[URL]['queryParams']> extends true ? {
|
|
97
|
+
queryParams: Endpoints[URL]['queryParams'];
|
|
98
|
+
} : {
|
|
99
|
+
queryParams?: Endpoints[URL]['queryParams'];
|
|
100
|
+
}) & // All properties optional
|
|
101
|
+
(Endpoints[URL]['request'] extends never ? {
|
|
102
|
+
body?: never;
|
|
103
|
+
} : Endpoints[URL]['request'] extends undefined ? {
|
|
104
|
+
body?: Endpoints[URL]['request'];
|
|
105
|
+
} : {
|
|
106
|
+
body: Endpoints[URL]['request'];
|
|
107
|
+
});
|
|
108
|
+
/**
|
|
109
|
+
* Check if a type is not never and not undefined
|
|
110
|
+
*/
|
|
111
|
+
export type IsRequired<T> = [T] extends [never] ? false : T extends undefined ? false : true;
|
|
112
|
+
/**
|
|
113
|
+
* Check if an endpoint has any required parameters
|
|
114
|
+
* Returns true if any of these conditions are met:
|
|
115
|
+
* - pathParams is defined (not never), OR
|
|
116
|
+
* - queryParams has any required keys inside, OR
|
|
117
|
+
* - body/request is required (not never, not undefined)
|
|
118
|
+
*/
|
|
119
|
+
export type HasRequiredParams<URL extends keyof Endpoints, Endpoints extends {
|
|
120
|
+
[K in keyof Endpoints]: EndpointDefinition<any, any, any, any>;
|
|
121
|
+
}> = Endpoints[URL]['pathParams'] extends never ? HasRequiredKeys<Endpoints[URL]['queryParams']> extends true ? true : IsRequired<Endpoints[URL]['request']> : true;
|
|
122
|
+
/**
|
|
123
|
+
* Conditional options parameter for makeRequest
|
|
124
|
+
* - Required if endpoint has:
|
|
125
|
+
* - pathParams, OR
|
|
126
|
+
* - queryParams with specific type (not undefined), OR
|
|
127
|
+
* - body with specific type (not undefined)
|
|
128
|
+
* - Optional otherwise
|
|
129
|
+
*/
|
|
130
|
+
export type MakeRequestOptions<URL extends keyof Endpoints, Endpoints extends {
|
|
131
|
+
[K in keyof Endpoints]: EndpointDefinition<any, any, any, any>;
|
|
132
|
+
}> = HasRequiredParams<URL, Endpoints> extends true ? [options: RequestOptions<URL, Endpoints>] : [options?: RequestOptions<URL, Endpoints>];
|