@ragion/sdk 0.1.0-alpha.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 +41 -0
- package/dist/chat.d.ts +49 -0
- package/dist/chat.js +126 -0
- package/dist/client.d.ts +17 -0
- package/dist/client.js +239 -0
- package/dist/esm/chat.js +122 -0
- package/dist/esm/client.js +235 -0
- package/dist/esm/index.js +4 -0
- package/dist/esm/sdk.js +37 -0
- package/dist/esm/streaming.js +143 -0
- package/dist/esm/types.js +1 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.js +12 -0
- package/dist/sdk.d.ts +34 -0
- package/dist/sdk.js +41 -0
- package/dist/streaming.d.ts +28 -0
- package/dist/streaming.js +148 -0
- package/dist/types.d.ts +45 -0
- package/dist/types.js +2 -0
- package/package.json +51 -0
package/README.md
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
# @ragion/sdk
|
|
2
|
+
|
|
3
|
+
SDK oficial do Ragion para JavaScript e TypeScript.
|
|
4
|
+
|
|
5
|
+
## Instalação
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @ragion/sdk
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Uso Básico
|
|
12
|
+
|
|
13
|
+
```typescript
|
|
14
|
+
import { RagionSDK } from '@ragion/sdk'
|
|
15
|
+
|
|
16
|
+
const ragion = new RagionSDK({
|
|
17
|
+
apiKey: 'sk_...',
|
|
18
|
+
organizationId: 'org_123', // Opcional
|
|
19
|
+
})
|
|
20
|
+
|
|
21
|
+
// Chat com streaming
|
|
22
|
+
ragion.chat.ask({
|
|
23
|
+
message: 'Hello',
|
|
24
|
+
onChunk: (chunk) => console.log(chunk),
|
|
25
|
+
})
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
## API
|
|
29
|
+
|
|
30
|
+
Veja a [documentação completa](../../README.md) para detalhes da API.
|
|
31
|
+
|
|
32
|
+
## Build
|
|
33
|
+
|
|
34
|
+
```bash
|
|
35
|
+
npm run build
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
Gera:
|
|
39
|
+
- `dist/index.js` (CommonJS)
|
|
40
|
+
- `dist/index.esm.js` (ES Modules)
|
|
41
|
+
- `dist/index.d.ts` (TypeScript definitions)
|
package/dist/chat.d.ts
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import type { RagionClient } from "./client";
|
|
2
|
+
import type { ChatOptions, StreamingController } from "./types";
|
|
3
|
+
export declare class ChatModule {
|
|
4
|
+
private client;
|
|
5
|
+
constructor(client: RagionClient);
|
|
6
|
+
private getHeaders;
|
|
7
|
+
private getBaseUrl;
|
|
8
|
+
private validateMessage;
|
|
9
|
+
private buildChatBody;
|
|
10
|
+
/**
|
|
11
|
+
* Sends a chat message and streams the response in real-time
|
|
12
|
+
* @param options - Chat options configuration
|
|
13
|
+
* @param options.message - The message to send (required)
|
|
14
|
+
* @param options.sessionId - Session ID for conversation continuity (optional)
|
|
15
|
+
* @param options.onChunk - Callback function called for each chunk of the response
|
|
16
|
+
* @param options.onComplete - Callback function called when streaming is complete
|
|
17
|
+
* @param options.onError - Callback function called if an error occurs
|
|
18
|
+
* @param options.signal - AbortSignal to cancel the request (optional)
|
|
19
|
+
* @returns StreamingController object with abort method to cancel the stream
|
|
20
|
+
* @example
|
|
21
|
+
* ```typescript
|
|
22
|
+
* const controller = sdk.chat.ask({
|
|
23
|
+
* message: 'What is AI?',
|
|
24
|
+
* onChunk: (chunk) => console.log(chunk),
|
|
25
|
+
* onComplete: () => console.log('Done'),
|
|
26
|
+
* onError: (error) => console.error(error)
|
|
27
|
+
* });
|
|
28
|
+
* // To cancel: controller.abort();
|
|
29
|
+
* ```
|
|
30
|
+
*/
|
|
31
|
+
ask: (options: ChatOptions) => StreamingController;
|
|
32
|
+
/**
|
|
33
|
+
* Sends a chat message and returns the complete response as a Promise
|
|
34
|
+
* @param options - Chat options configuration (without streaming callbacks)
|
|
35
|
+
* @param options.message - The message to send (required)
|
|
36
|
+
* @param options.sessionId - Session ID for conversation continuity (optional)
|
|
37
|
+
* @param options.signal - AbortSignal to cancel the request (optional)
|
|
38
|
+
* @returns Promise that resolves with the complete response string
|
|
39
|
+
* @throws {Error} If the request fails or is aborted
|
|
40
|
+
* @example
|
|
41
|
+
* ```typescript
|
|
42
|
+
* const response = await sdk.chat.askComplete({
|
|
43
|
+
* message: 'Explain quantum computing'
|
|
44
|
+
* });
|
|
45
|
+
* console.log(response);
|
|
46
|
+
* ```
|
|
47
|
+
*/
|
|
48
|
+
askComplete: (options: Omit<ChatOptions, "onChunk" | "onComplete" | "onError">) => Promise<string>;
|
|
49
|
+
}
|
package/dist/chat.js
ADDED
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.ChatModule = void 0;
|
|
13
|
+
const streaming_1 = require("./streaming");
|
|
14
|
+
class ChatModule {
|
|
15
|
+
constructor(client) {
|
|
16
|
+
this.client = client;
|
|
17
|
+
this.getHeaders = () => {
|
|
18
|
+
const headers = this.client.createHeaders();
|
|
19
|
+
const headersObj = {};
|
|
20
|
+
Object.entries(headers).forEach(([key, value]) => {
|
|
21
|
+
headersObj[key] = String(value);
|
|
22
|
+
});
|
|
23
|
+
return headersObj;
|
|
24
|
+
};
|
|
25
|
+
this.getBaseUrl = () => {
|
|
26
|
+
return this.client.getBaseUrl();
|
|
27
|
+
};
|
|
28
|
+
this.validateMessage = (message) => {
|
|
29
|
+
return !!message && message.trim().length > 0;
|
|
30
|
+
};
|
|
31
|
+
this.buildChatBody = (options) => {
|
|
32
|
+
const body = {
|
|
33
|
+
message: options.message.trim(),
|
|
34
|
+
};
|
|
35
|
+
if (options.sessionId) {
|
|
36
|
+
body.sessionId = options.sessionId;
|
|
37
|
+
}
|
|
38
|
+
return body;
|
|
39
|
+
};
|
|
40
|
+
/**
|
|
41
|
+
* Sends a chat message and streams the response in real-time
|
|
42
|
+
* @param options - Chat options configuration
|
|
43
|
+
* @param options.message - The message to send (required)
|
|
44
|
+
* @param options.sessionId - Session ID for conversation continuity (optional)
|
|
45
|
+
* @param options.onChunk - Callback function called for each chunk of the response
|
|
46
|
+
* @param options.onComplete - Callback function called when streaming is complete
|
|
47
|
+
* @param options.onError - Callback function called if an error occurs
|
|
48
|
+
* @param options.signal - AbortSignal to cancel the request (optional)
|
|
49
|
+
* @returns StreamingController object with abort method to cancel the stream
|
|
50
|
+
* @example
|
|
51
|
+
* ```typescript
|
|
52
|
+
* const controller = sdk.chat.ask({
|
|
53
|
+
* message: 'What is AI?',
|
|
54
|
+
* onChunk: (chunk) => console.log(chunk),
|
|
55
|
+
* onComplete: () => console.log('Done'),
|
|
56
|
+
* onError: (error) => console.error(error)
|
|
57
|
+
* });
|
|
58
|
+
* // To cancel: controller.abort();
|
|
59
|
+
* ```
|
|
60
|
+
*/
|
|
61
|
+
this.ask = (options) => {
|
|
62
|
+
const { message, sessionId, onChunk, onComplete, onError, signal } = options;
|
|
63
|
+
if (!this.validateMessage(message)) {
|
|
64
|
+
const error = new Error("Message is required");
|
|
65
|
+
onError === null || onError === void 0 ? void 0 : onError(error);
|
|
66
|
+
return {
|
|
67
|
+
abort: () => { },
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
const url = `${this.getBaseUrl()}/ai/ask`;
|
|
71
|
+
const body = this.buildChatBody({ message, sessionId });
|
|
72
|
+
const controller = (0, streaming_1.createStreamingRequest)(url, {
|
|
73
|
+
method: "POST",
|
|
74
|
+
headers: this.getHeaders(),
|
|
75
|
+
body,
|
|
76
|
+
signal,
|
|
77
|
+
timeout: 30000,
|
|
78
|
+
onChunk: (chunk) => {
|
|
79
|
+
onChunk === null || onChunk === void 0 ? void 0 : onChunk(chunk);
|
|
80
|
+
},
|
|
81
|
+
onComplete: () => {
|
|
82
|
+
onComplete === null || onComplete === void 0 ? void 0 : onComplete();
|
|
83
|
+
},
|
|
84
|
+
onError: (error) => {
|
|
85
|
+
onError === null || onError === void 0 ? void 0 : onError(error);
|
|
86
|
+
},
|
|
87
|
+
});
|
|
88
|
+
return controller;
|
|
89
|
+
};
|
|
90
|
+
/**
|
|
91
|
+
* Sends a chat message and returns the complete response as a Promise
|
|
92
|
+
* @param options - Chat options configuration (without streaming callbacks)
|
|
93
|
+
* @param options.message - The message to send (required)
|
|
94
|
+
* @param options.sessionId - Session ID for conversation continuity (optional)
|
|
95
|
+
* @param options.signal - AbortSignal to cancel the request (optional)
|
|
96
|
+
* @returns Promise that resolves with the complete response string
|
|
97
|
+
* @throws {Error} If the request fails or is aborted
|
|
98
|
+
* @example
|
|
99
|
+
* ```typescript
|
|
100
|
+
* const response = await sdk.chat.askComplete({
|
|
101
|
+
* message: 'Explain quantum computing'
|
|
102
|
+
* });
|
|
103
|
+
* console.log(response);
|
|
104
|
+
* ```
|
|
105
|
+
*/
|
|
106
|
+
this.askComplete = (options) => __awaiter(this, void 0, void 0, function* () {
|
|
107
|
+
return new Promise((resolve, reject) => {
|
|
108
|
+
let fullResponse = "";
|
|
109
|
+
const controller = this.ask(Object.assign(Object.assign({}, options), { onChunk: chunk => {
|
|
110
|
+
fullResponse += chunk;
|
|
111
|
+
}, onComplete: () => {
|
|
112
|
+
resolve(fullResponse);
|
|
113
|
+
}, onError: error => {
|
|
114
|
+
reject(error);
|
|
115
|
+
} }));
|
|
116
|
+
if (options.signal) {
|
|
117
|
+
options.signal.addEventListener("abort", () => {
|
|
118
|
+
controller.abort();
|
|
119
|
+
reject(new Error("Request was aborted"));
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
});
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
exports.ChatModule = ChatModule;
|
package/dist/client.d.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { ApiResponse, RagionConfig, RequestOptions } from "./types";
|
|
2
|
+
export declare class RagionClient {
|
|
3
|
+
private config;
|
|
4
|
+
constructor(config: RagionConfig);
|
|
5
|
+
createHeaders: (customHeaders?: Record<string, string>) => HeadersInit;
|
|
6
|
+
getBaseUrl: () => string;
|
|
7
|
+
private fetchWithTimeout;
|
|
8
|
+
private prepareHeaders;
|
|
9
|
+
private prepareBody;
|
|
10
|
+
private handleErrorResponse;
|
|
11
|
+
request: <T>(endpoint: string, options?: RequestOptions) => Promise<ApiResponse<T>>;
|
|
12
|
+
get: <T>(endpoint: string, options?: Omit<RequestOptions, "method" | "body">) => Promise<ApiResponse<T>>;
|
|
13
|
+
post: <T>(endpoint: string, body?: unknown, options?: Omit<RequestOptions, "method" | "body">) => Promise<ApiResponse<T>>;
|
|
14
|
+
patch: <T>(endpoint: string, body?: unknown, options?: Omit<RequestOptions, "method" | "body">) => Promise<ApiResponse<T>>;
|
|
15
|
+
delete: <T>(endpoint: string, options?: Omit<RequestOptions, "method" | "body">) => Promise<ApiResponse<T>>;
|
|
16
|
+
updateConfig: (config: Partial<RagionConfig>) => void;
|
|
17
|
+
}
|
package/dist/client.js
ADDED
|
@@ -0,0 +1,239 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.RagionClient = void 0;
|
|
13
|
+
const DEFAULT_CONFIG = {
|
|
14
|
+
baseUrl: "https://api.ragion.com.br",
|
|
15
|
+
timeout: 30000,
|
|
16
|
+
retryAttempts: 3,
|
|
17
|
+
retryDelay: 1000,
|
|
18
|
+
};
|
|
19
|
+
const generateRequestId = () => {
|
|
20
|
+
return `${Date.now()}-${Math.random().toString(36).substring(2, 11)}`;
|
|
21
|
+
};
|
|
22
|
+
const sleep = (ms) => {
|
|
23
|
+
return new Promise(resolve => setTimeout(resolve, ms));
|
|
24
|
+
};
|
|
25
|
+
const isRetryableError = (status) => {
|
|
26
|
+
return status >= 500 || status === 429;
|
|
27
|
+
};
|
|
28
|
+
const safeJsonParse = (response) => __awaiter(void 0, void 0, void 0, function* () {
|
|
29
|
+
const text = yield response.text();
|
|
30
|
+
if (!text)
|
|
31
|
+
return null;
|
|
32
|
+
try {
|
|
33
|
+
return JSON.parse(text);
|
|
34
|
+
}
|
|
35
|
+
catch (_a) {
|
|
36
|
+
return null;
|
|
37
|
+
}
|
|
38
|
+
});
|
|
39
|
+
const buildErrorResponse = (error, requestId, status) => {
|
|
40
|
+
if (error instanceof Error) {
|
|
41
|
+
return {
|
|
42
|
+
data: null,
|
|
43
|
+
status: status || 0,
|
|
44
|
+
ok: false,
|
|
45
|
+
error: {
|
|
46
|
+
error: "NetworkError",
|
|
47
|
+
message: error.message,
|
|
48
|
+
statusCode: status || 0,
|
|
49
|
+
requestId,
|
|
50
|
+
},
|
|
51
|
+
requestId,
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
return {
|
|
55
|
+
data: null,
|
|
56
|
+
status: status || 500,
|
|
57
|
+
ok: false,
|
|
58
|
+
error: {
|
|
59
|
+
error: "UnknownError",
|
|
60
|
+
message: "An unknown error occurred",
|
|
61
|
+
statusCode: status || 500,
|
|
62
|
+
requestId,
|
|
63
|
+
},
|
|
64
|
+
requestId,
|
|
65
|
+
};
|
|
66
|
+
};
|
|
67
|
+
const combineAbortSignals = (signal1, signal2) => {
|
|
68
|
+
const combinedController = new AbortController();
|
|
69
|
+
signal1.addEventListener("abort", () => combinedController.abort());
|
|
70
|
+
signal2.addEventListener("abort", () => combinedController.abort());
|
|
71
|
+
return combinedController.signal;
|
|
72
|
+
};
|
|
73
|
+
class RagionClient {
|
|
74
|
+
constructor(config) {
|
|
75
|
+
var _a;
|
|
76
|
+
this.createHeaders = (customHeaders) => {
|
|
77
|
+
const headers = Object.assign({ "Content-Type": "application/json", "x-Api-Key": this.config.apiKey }, customHeaders);
|
|
78
|
+
return headers;
|
|
79
|
+
};
|
|
80
|
+
this.getBaseUrl = () => {
|
|
81
|
+
return this.config.baseUrl;
|
|
82
|
+
};
|
|
83
|
+
this.fetchWithTimeout = (url, options, timeout) => __awaiter(this, void 0, void 0, function* () {
|
|
84
|
+
const controller = new AbortController();
|
|
85
|
+
const timeoutId = setTimeout(() => controller.abort(), timeout);
|
|
86
|
+
try {
|
|
87
|
+
const signal = options.signal ? combineAbortSignals(options.signal, controller.signal) : controller.signal;
|
|
88
|
+
const response = yield fetch(url, Object.assign(Object.assign({}, options), { signal }));
|
|
89
|
+
clearTimeout(timeoutId);
|
|
90
|
+
return response;
|
|
91
|
+
}
|
|
92
|
+
catch (error) {
|
|
93
|
+
clearTimeout(timeoutId);
|
|
94
|
+
if (error instanceof Error && error.name === "AbortError") {
|
|
95
|
+
throw new Error(`Request timeout after ${timeout}ms`);
|
|
96
|
+
}
|
|
97
|
+
throw error;
|
|
98
|
+
}
|
|
99
|
+
});
|
|
100
|
+
this.prepareHeaders = (options) => {
|
|
101
|
+
const isMultipart = options.body instanceof FormData;
|
|
102
|
+
const headers = {};
|
|
103
|
+
if (!isMultipart) {
|
|
104
|
+
Object.assign(headers, this.createHeaders());
|
|
105
|
+
}
|
|
106
|
+
else {
|
|
107
|
+
const authHeaders = this.createHeaders();
|
|
108
|
+
Object.entries(authHeaders).forEach(([key, value]) => {
|
|
109
|
+
if (key.toLowerCase() !== "content-type") {
|
|
110
|
+
headers[key] = value;
|
|
111
|
+
}
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
if (options.headers) {
|
|
115
|
+
Object.assign(headers, options.headers);
|
|
116
|
+
}
|
|
117
|
+
return headers;
|
|
118
|
+
};
|
|
119
|
+
this.prepareBody = (body) => {
|
|
120
|
+
if (!body)
|
|
121
|
+
return undefined;
|
|
122
|
+
if (body instanceof FormData) {
|
|
123
|
+
return body;
|
|
124
|
+
}
|
|
125
|
+
return JSON.stringify(body);
|
|
126
|
+
};
|
|
127
|
+
this.handleErrorResponse = (response, data, requestId, attempt) => {
|
|
128
|
+
const errorData = data || {
|
|
129
|
+
error: "HTTPError",
|
|
130
|
+
message: response.statusText,
|
|
131
|
+
statusCode: response.status,
|
|
132
|
+
};
|
|
133
|
+
if (response.status < 500 && response.status !== 429) {
|
|
134
|
+
return {
|
|
135
|
+
data: null,
|
|
136
|
+
status: response.status,
|
|
137
|
+
ok: false,
|
|
138
|
+
error: Object.assign(Object.assign({}, errorData), { requestId }),
|
|
139
|
+
requestId,
|
|
140
|
+
};
|
|
141
|
+
}
|
|
142
|
+
if (attempt < this.config.retryAttempts && isRetryableError(response.status)) {
|
|
143
|
+
return null;
|
|
144
|
+
}
|
|
145
|
+
return {
|
|
146
|
+
data: null,
|
|
147
|
+
status: response.status,
|
|
148
|
+
ok: false,
|
|
149
|
+
error: Object.assign(Object.assign({}, errorData), { requestId }),
|
|
150
|
+
requestId,
|
|
151
|
+
};
|
|
152
|
+
};
|
|
153
|
+
this.request = (endpoint_1, ...args_1) => __awaiter(this, [endpoint_1, ...args_1], void 0, function* (endpoint, options = {}) {
|
|
154
|
+
var _a;
|
|
155
|
+
const requestId = generateRequestId();
|
|
156
|
+
const url = `${this.config.baseUrl}${endpoint}`;
|
|
157
|
+
const method = options.method || "GET";
|
|
158
|
+
const timeout = options.timeout || this.config.timeout;
|
|
159
|
+
const headers = this.prepareHeaders(options);
|
|
160
|
+
const body = this.prepareBody(options.body);
|
|
161
|
+
let lastError = null;
|
|
162
|
+
let lastStatus;
|
|
163
|
+
for (let attempt = 0; attempt <= this.config.retryAttempts; attempt++) {
|
|
164
|
+
try {
|
|
165
|
+
const response = yield this.fetchWithTimeout(url, {
|
|
166
|
+
method,
|
|
167
|
+
headers,
|
|
168
|
+
body,
|
|
169
|
+
signal: options.signal,
|
|
170
|
+
}, timeout);
|
|
171
|
+
lastStatus = response.status;
|
|
172
|
+
const data = yield safeJsonParse(response);
|
|
173
|
+
if (!response.ok) {
|
|
174
|
+
const errorResponse = this.handleErrorResponse(response, data, requestId, attempt);
|
|
175
|
+
if (errorResponse === null) {
|
|
176
|
+
yield sleep(this.config.retryDelay * (attempt + 1));
|
|
177
|
+
continue;
|
|
178
|
+
}
|
|
179
|
+
return errorResponse;
|
|
180
|
+
}
|
|
181
|
+
return {
|
|
182
|
+
data: data,
|
|
183
|
+
status: response.status,
|
|
184
|
+
ok: true,
|
|
185
|
+
requestId,
|
|
186
|
+
};
|
|
187
|
+
}
|
|
188
|
+
catch (error) {
|
|
189
|
+
lastError = error instanceof Error ? error : new Error(String(error));
|
|
190
|
+
if ((_a = options.signal) === null || _a === void 0 ? void 0 : _a.aborted) {
|
|
191
|
+
return buildErrorResponse(lastError, requestId, 0);
|
|
192
|
+
}
|
|
193
|
+
if (attempt < this.config.retryAttempts) {
|
|
194
|
+
yield sleep(this.config.retryDelay * (attempt + 1));
|
|
195
|
+
continue;
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
return buildErrorResponse(lastError || new Error("Request failed"), requestId, lastStatus);
|
|
200
|
+
});
|
|
201
|
+
this.get = (endpoint, options) => __awaiter(this, void 0, void 0, function* () {
|
|
202
|
+
return this.request(endpoint, Object.assign(Object.assign({}, options), { method: "GET" }));
|
|
203
|
+
});
|
|
204
|
+
this.post = (endpoint, body, options) => __awaiter(this, void 0, void 0, function* () {
|
|
205
|
+
return this.request(endpoint, Object.assign(Object.assign({}, options), { method: "POST", body }));
|
|
206
|
+
});
|
|
207
|
+
this.patch = (endpoint, body, options) => __awaiter(this, void 0, void 0, function* () {
|
|
208
|
+
return this.request(endpoint, Object.assign(Object.assign({}, options), { method: "PATCH", body }));
|
|
209
|
+
});
|
|
210
|
+
this.delete = (endpoint, options) => __awaiter(this, void 0, void 0, function* () {
|
|
211
|
+
return this.request(endpoint, Object.assign(Object.assign({}, options), { method: "DELETE" }));
|
|
212
|
+
});
|
|
213
|
+
this.updateConfig = (config) => {
|
|
214
|
+
if (config.apiKey) {
|
|
215
|
+
this.config.apiKey = config.apiKey;
|
|
216
|
+
}
|
|
217
|
+
if (config.timeout !== undefined) {
|
|
218
|
+
this.config.timeout = config.timeout;
|
|
219
|
+
}
|
|
220
|
+
if (config.retryAttempts !== undefined) {
|
|
221
|
+
this.config.retryAttempts = config.retryAttempts;
|
|
222
|
+
}
|
|
223
|
+
if (config.retryDelay !== undefined) {
|
|
224
|
+
this.config.retryDelay = config.retryDelay;
|
|
225
|
+
}
|
|
226
|
+
};
|
|
227
|
+
if (!config.apiKey) {
|
|
228
|
+
throw new Error("API Key is required");
|
|
229
|
+
}
|
|
230
|
+
this.config = {
|
|
231
|
+
apiKey: config.apiKey,
|
|
232
|
+
baseUrl: DEFAULT_CONFIG.baseUrl,
|
|
233
|
+
timeout: config.timeout || DEFAULT_CONFIG.timeout,
|
|
234
|
+
retryAttempts: (_a = config.retryAttempts) !== null && _a !== void 0 ? _a : DEFAULT_CONFIG.retryAttempts,
|
|
235
|
+
retryDelay: config.retryDelay || DEFAULT_CONFIG.retryDelay,
|
|
236
|
+
};
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
exports.RagionClient = RagionClient;
|
package/dist/esm/chat.js
ADDED
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
import { createStreamingRequest } from "./streaming";
|
|
11
|
+
export class ChatModule {
|
|
12
|
+
constructor(client) {
|
|
13
|
+
this.client = client;
|
|
14
|
+
this.getHeaders = () => {
|
|
15
|
+
const headers = this.client.createHeaders();
|
|
16
|
+
const headersObj = {};
|
|
17
|
+
Object.entries(headers).forEach(([key, value]) => {
|
|
18
|
+
headersObj[key] = String(value);
|
|
19
|
+
});
|
|
20
|
+
return headersObj;
|
|
21
|
+
};
|
|
22
|
+
this.getBaseUrl = () => {
|
|
23
|
+
return this.client.getBaseUrl();
|
|
24
|
+
};
|
|
25
|
+
this.validateMessage = (message) => {
|
|
26
|
+
return !!message && message.trim().length > 0;
|
|
27
|
+
};
|
|
28
|
+
this.buildChatBody = (options) => {
|
|
29
|
+
const body = {
|
|
30
|
+
message: options.message.trim(),
|
|
31
|
+
};
|
|
32
|
+
if (options.sessionId) {
|
|
33
|
+
body.sessionId = options.sessionId;
|
|
34
|
+
}
|
|
35
|
+
return body;
|
|
36
|
+
};
|
|
37
|
+
/**
|
|
38
|
+
* Sends a chat message and streams the response in real-time
|
|
39
|
+
* @param options - Chat options configuration
|
|
40
|
+
* @param options.message - The message to send (required)
|
|
41
|
+
* @param options.sessionId - Session ID for conversation continuity (optional)
|
|
42
|
+
* @param options.onChunk - Callback function called for each chunk of the response
|
|
43
|
+
* @param options.onComplete - Callback function called when streaming is complete
|
|
44
|
+
* @param options.onError - Callback function called if an error occurs
|
|
45
|
+
* @param options.signal - AbortSignal to cancel the request (optional)
|
|
46
|
+
* @returns StreamingController object with abort method to cancel the stream
|
|
47
|
+
* @example
|
|
48
|
+
* ```typescript
|
|
49
|
+
* const controller = sdk.chat.ask({
|
|
50
|
+
* message: 'What is AI?',
|
|
51
|
+
* onChunk: (chunk) => console.log(chunk),
|
|
52
|
+
* onComplete: () => console.log('Done'),
|
|
53
|
+
* onError: (error) => console.error(error)
|
|
54
|
+
* });
|
|
55
|
+
* // To cancel: controller.abort();
|
|
56
|
+
* ```
|
|
57
|
+
*/
|
|
58
|
+
this.ask = (options) => {
|
|
59
|
+
const { message, sessionId, onChunk, onComplete, onError, signal } = options;
|
|
60
|
+
if (!this.validateMessage(message)) {
|
|
61
|
+
const error = new Error("Message is required");
|
|
62
|
+
onError === null || onError === void 0 ? void 0 : onError(error);
|
|
63
|
+
return {
|
|
64
|
+
abort: () => { },
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
const url = `${this.getBaseUrl()}/ai/ask`;
|
|
68
|
+
const body = this.buildChatBody({ message, sessionId });
|
|
69
|
+
const controller = createStreamingRequest(url, {
|
|
70
|
+
method: "POST",
|
|
71
|
+
headers: this.getHeaders(),
|
|
72
|
+
body,
|
|
73
|
+
signal,
|
|
74
|
+
timeout: 30000,
|
|
75
|
+
onChunk: (chunk) => {
|
|
76
|
+
onChunk === null || onChunk === void 0 ? void 0 : onChunk(chunk);
|
|
77
|
+
},
|
|
78
|
+
onComplete: () => {
|
|
79
|
+
onComplete === null || onComplete === void 0 ? void 0 : onComplete();
|
|
80
|
+
},
|
|
81
|
+
onError: (error) => {
|
|
82
|
+
onError === null || onError === void 0 ? void 0 : onError(error);
|
|
83
|
+
},
|
|
84
|
+
});
|
|
85
|
+
return controller;
|
|
86
|
+
};
|
|
87
|
+
/**
|
|
88
|
+
* Sends a chat message and returns the complete response as a Promise
|
|
89
|
+
* @param options - Chat options configuration (without streaming callbacks)
|
|
90
|
+
* @param options.message - The message to send (required)
|
|
91
|
+
* @param options.sessionId - Session ID for conversation continuity (optional)
|
|
92
|
+
* @param options.signal - AbortSignal to cancel the request (optional)
|
|
93
|
+
* @returns Promise that resolves with the complete response string
|
|
94
|
+
* @throws {Error} If the request fails or is aborted
|
|
95
|
+
* @example
|
|
96
|
+
* ```typescript
|
|
97
|
+
* const response = await sdk.chat.askComplete({
|
|
98
|
+
* message: 'Explain quantum computing'
|
|
99
|
+
* });
|
|
100
|
+
* console.log(response);
|
|
101
|
+
* ```
|
|
102
|
+
*/
|
|
103
|
+
this.askComplete = (options) => __awaiter(this, void 0, void 0, function* () {
|
|
104
|
+
return new Promise((resolve, reject) => {
|
|
105
|
+
let fullResponse = "";
|
|
106
|
+
const controller = this.ask(Object.assign(Object.assign({}, options), { onChunk: chunk => {
|
|
107
|
+
fullResponse += chunk;
|
|
108
|
+
}, onComplete: () => {
|
|
109
|
+
resolve(fullResponse);
|
|
110
|
+
}, onError: error => {
|
|
111
|
+
reject(error);
|
|
112
|
+
} }));
|
|
113
|
+
if (options.signal) {
|
|
114
|
+
options.signal.addEventListener("abort", () => {
|
|
115
|
+
controller.abort();
|
|
116
|
+
reject(new Error("Request was aborted"));
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
});
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
}
|