@ragbits/api-client 0.0.1

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/src/types.ts ADDED
@@ -0,0 +1,259 @@
1
+ import { RJSFSchema } from '@rjsf/utils'
2
+
3
+ /**
4
+ * Message roles for chat conversations
5
+ */
6
+ export enum MessageRole {
7
+ USER = 'user',
8
+ ASSISTANT = 'assistant',
9
+ SYSTEM = 'system',
10
+ }
11
+
12
+ /**
13
+ * Message structure for chat conversations
14
+ */
15
+ export interface Message {
16
+ role: MessageRole
17
+ content: string
18
+ id?: string
19
+ }
20
+
21
+ /**
22
+ * Reference structure for document references
23
+ */
24
+ export interface Reference {
25
+ title: string
26
+ content: string
27
+ url?: string
28
+ }
29
+
30
+ /**
31
+ * Response types from the API
32
+ */
33
+ export enum ChatResponseType {
34
+ MESSAGE = 'message',
35
+ REFERENCE = 'reference',
36
+ STATE_UPDATE = 'state_update',
37
+ TEXT = 'text',
38
+ MESSAGE_ID = 'message_id',
39
+ CONVERSATION_ID = 'conversation_id',
40
+ }
41
+
42
+ /**
43
+ * Feedback types for user feedback
44
+ */
45
+ export enum FeedbackType {
46
+ LIKE = 'like',
47
+ DISLIKE = 'dislike',
48
+ }
49
+
50
+ /**
51
+ * Server state interface for state updates
52
+ */
53
+ export interface ServerState {
54
+ state: Record<string, unknown>
55
+ signature: string
56
+ }
57
+
58
+ /**
59
+ * Specific chat response types
60
+ */
61
+ interface MessageIdChatResponse {
62
+ type: ChatResponseType.MESSAGE_ID
63
+ content: string
64
+ }
65
+
66
+ interface TextChatResponse {
67
+ type: ChatResponseType.TEXT
68
+ content: string
69
+ }
70
+
71
+ interface ReferenceChatResponse {
72
+ type: ChatResponseType.REFERENCE
73
+ content: Reference
74
+ }
75
+
76
+ interface ConversationIdChatResponse {
77
+ type: ChatResponseType.CONVERSATION_ID
78
+ content: string
79
+ }
80
+
81
+ interface StateUpdateChatResponse {
82
+ type: ChatResponseType.STATE_UPDATE
83
+ content: ServerState
84
+ }
85
+
86
+ /**
87
+ * Typed chat response union
88
+ */
89
+ export type TypedChatResponse =
90
+ | TextChatResponse
91
+ | ReferenceChatResponse
92
+ | MessageIdChatResponse
93
+ | ConversationIdChatResponse
94
+ | StateUpdateChatResponse
95
+
96
+ /**
97
+ * Base chat request to the API
98
+ */
99
+ export interface ChatRequest {
100
+ message: string
101
+ history: Message[]
102
+ context?: Record<string, unknown>
103
+ }
104
+
105
+ /**
106
+ * Feedback request to the API
107
+ */
108
+ export interface FeedbackRequest {
109
+ message_id: string
110
+ feedback: FeedbackType
111
+ payload: Record<string, unknown> | null
112
+ }
113
+
114
+ /**
115
+ * Feedback response from the API
116
+ */
117
+ export interface FeedbackResponse {
118
+ status: string
119
+ }
120
+
121
+ /**
122
+ * UI customization configuration
123
+ */
124
+ export interface UICustomization {
125
+ header: {
126
+ title?: string
127
+ subtitle?: string
128
+ logo?: string
129
+ }
130
+ welcome_message?: string
131
+ }
132
+
133
+ /**
134
+ * Configuration response from the API
135
+ */
136
+ export interface ConfigResponse {
137
+ feedback: {
138
+ like: {
139
+ enabled: boolean
140
+ form: RJSFSchema | null
141
+ }
142
+ dislike: {
143
+ enabled: boolean
144
+ form: RJSFSchema | null
145
+ }
146
+ }
147
+ customization: UICustomization | null
148
+ }
149
+
150
+ /**
151
+ * Configuration for the client
152
+ */
153
+ export interface ClientConfig {
154
+ baseUrl?: string
155
+ }
156
+
157
+ /**
158
+ * Callbacks for handling streaming responses
159
+ */
160
+ export interface StreamCallbacks<T, E = Error> {
161
+ onMessage: (data: T) => void | Promise<void>
162
+ onError: (error: E) => void | Promise<void>
163
+ onClose?: () => void | Promise<void>
164
+ }
165
+
166
+ /**
167
+ * Regular API endpoint definitions with their request/response types
168
+ */
169
+ export interface ApiEndpoints {
170
+ '/api/config': {
171
+ method: 'GET'
172
+ request: never
173
+ response: ConfigResponse
174
+ }
175
+ '/api/feedback': {
176
+ method: 'POST'
177
+ request: FeedbackRequest
178
+ response: FeedbackResponse
179
+ }
180
+ }
181
+
182
+ /**
183
+ * Streaming API endpoint definitions with their request/stream response types
184
+ */
185
+ export interface StreamingEndpoints {
186
+ '/api/chat': {
187
+ method: 'POST'
188
+ request: ChatRequest
189
+ stream: TypedChatResponse
190
+ }
191
+ }
192
+
193
+ /**
194
+ * Extract endpoint paths as a union type
195
+ */
196
+ export type ApiEndpointPath = keyof ApiEndpoints
197
+
198
+ /**
199
+ * Extract streaming endpoint paths as a union type
200
+ */
201
+ export type StreamingEndpointPath = keyof StreamingEndpoints
202
+
203
+ /**
204
+ * Extract request type for a specific API endpoint
205
+ */
206
+ export type ApiEndpointRequest<T extends ApiEndpointPath> =
207
+ ApiEndpoints[T]['request']
208
+
209
+ /**
210
+ * Extract response type for a specific API endpoint
211
+ */
212
+ export type ApiEndpointResponse<T extends ApiEndpointPath> =
213
+ ApiEndpoints[T]['response']
214
+
215
+ /**
216
+ * Extract HTTP method for a specific API endpoint
217
+ */
218
+ export type ApiEndpointMethod<T extends ApiEndpointPath> =
219
+ ApiEndpoints[T]['method']
220
+
221
+ /**
222
+ * Extract request type for a specific streaming endpoint
223
+ */
224
+ export type StreamingEndpointRequest<T extends StreamingEndpointPath> =
225
+ StreamingEndpoints[T]['request']
226
+
227
+ /**
228
+ * Extract stream response type for a specific streaming endpoint
229
+ */
230
+ export type StreamingEndpointStream<T extends StreamingEndpointPath> =
231
+ StreamingEndpoints[T]['stream']
232
+
233
+ /**
234
+ * Extract HTTP method for a specific streaming endpoint
235
+ */
236
+ export type StreamingEndpointMethod<T extends StreamingEndpointPath> =
237
+ StreamingEndpoints[T]['method']
238
+
239
+ /**
240
+ * Generic request options for API endpoints with typed methods and body
241
+ */
242
+ export interface TypedApiRequestOptions<T extends ApiEndpointPath> {
243
+ method?: ApiEndpointMethod<T>
244
+ body?: ApiEndpointRequest<T> extends never
245
+ ? undefined
246
+ : ApiEndpointRequest<T>
247
+ headers?: Record<string, string>
248
+ signal?: AbortSignal
249
+ }
250
+
251
+ /**
252
+ * Typed request options for specific streaming endpoints
253
+ */
254
+ export interface TypedStreamRequestOptions<T extends StreamingEndpointPath> {
255
+ method?: StreamingEndpointMethod<T>
256
+ body?: StreamingEndpointRequest<T>
257
+ headers?: Record<string, string>
258
+ signal?: AbortSignal
259
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,16 @@
1
+ {
2
+ "compilerOptions": {
3
+ "target": "ES2020",
4
+ "module": "ESNext",
5
+ "lib": ["DOM", "DOM.Iterable", "ESNext"],
6
+ "moduleResolution": "node",
7
+ "strict": true,
8
+ "declaration": true,
9
+ "esModuleInterop": true,
10
+ "skipLibCheck": true,
11
+ "forceConsistentCasingInFileNames": true,
12
+ "outDir": "dist"
13
+ },
14
+ "include": ["src"],
15
+ "exclude": ["node_modules", "dist"]
16
+ }
@@ -0,0 +1,20 @@
1
+ import { defineConfig } from 'vitest/config'
2
+
3
+ export default defineConfig({
4
+ test: {
5
+ globals: true,
6
+ environment: 'node',
7
+ setupFiles: ['./src/test/setup.ts'],
8
+ coverage: {
9
+ provider: 'v8',
10
+ reporter: ['text', 'json', 'html'],
11
+ exclude: [
12
+ 'node_modules/',
13
+ 'dist/',
14
+ 'src/test/',
15
+ '**/*.d.ts',
16
+ '**/*.config.*',
17
+ ],
18
+ },
19
+ },
20
+ })