@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.
@@ -0,0 +1,148 @@
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.createStreamingRequest = exports.SSEParser = void 0;
13
+ class SSEParser {
14
+ constructor() {
15
+ this.buffer = "";
16
+ this.processChunk = (chunk, callbacks) => {
17
+ var _a;
18
+ this.buffer += this.decoder.decode(chunk, { stream: true });
19
+ const lines = this.buffer.split("\n");
20
+ this.buffer = lines.pop() || "";
21
+ for (const line of lines) {
22
+ const trimmedLine = line.trim();
23
+ if (!trimmedLine)
24
+ continue;
25
+ if (trimmedLine.startsWith("data: ")) {
26
+ const data = trimmedLine.slice(6);
27
+ if (data === "[DONE]") {
28
+ (_a = callbacks.onDone) === null || _a === void 0 ? void 0 : _a.call(callbacks);
29
+ return;
30
+ }
31
+ this.processDataLine(data, callbacks);
32
+ }
33
+ }
34
+ };
35
+ this.processDataLine = (data, callbacks) => {
36
+ var _a, _b, _c;
37
+ try {
38
+ const parsedData = JSON.parse(data);
39
+ if (parsedData.type === "error" || parsedData.error) {
40
+ const error = new Error(parsedData.message || parsedData.error || "Unknown error occurred");
41
+ (_a = callbacks.onError) === null || _a === void 0 ? void 0 : _a.call(callbacks, error);
42
+ return;
43
+ }
44
+ (_b = callbacks.onData) === null || _b === void 0 ? void 0 : _b.call(callbacks, data);
45
+ }
46
+ catch (_d) {
47
+ (_c = callbacks.onData) === null || _c === void 0 ? void 0 : _c.call(callbacks, data);
48
+ }
49
+ };
50
+ this.processFinal = (callbacks) => {
51
+ var _a, _b;
52
+ if (this.buffer.trim()) {
53
+ const trimmedLine = this.buffer.trim();
54
+ if (trimmedLine.startsWith("data: ")) {
55
+ const data = trimmedLine.slice(6);
56
+ if (data !== "[DONE]") {
57
+ (_a = callbacks.onData) === null || _a === void 0 ? void 0 : _a.call(callbacks, data);
58
+ }
59
+ }
60
+ }
61
+ (_b = callbacks.onDone) === null || _b === void 0 ? void 0 : _b.call(callbacks);
62
+ };
63
+ this.reset = () => {
64
+ this.buffer = "";
65
+ };
66
+ this.decoder = new TextDecoder();
67
+ }
68
+ }
69
+ exports.SSEParser = SSEParser;
70
+ const createStreamingRequest = (url, options) => {
71
+ const { method = "POST", headers = {}, body, signal, timeout = 30000, onChunk, onComplete, onError } = options;
72
+ const abortController = new AbortController();
73
+ const combinedSignal = signal
74
+ ? (() => {
75
+ const combined = new AbortController();
76
+ signal.addEventListener("abort", () => combined.abort());
77
+ abortController.signal.addEventListener("abort", () => combined.abort());
78
+ return combined.signal;
79
+ })()
80
+ : abortController.signal;
81
+ const timeoutId = setTimeout(() => {
82
+ abortController.abort();
83
+ }, timeout);
84
+ const parser = new SSEParser();
85
+ const performStream = () => __awaiter(void 0, void 0, void 0, function* () {
86
+ var _a;
87
+ try {
88
+ const requestHeaders = new Headers(headers);
89
+ requestHeaders.set("Accept", "text/event-stream");
90
+ const response = yield fetch(url, {
91
+ method,
92
+ headers: requestHeaders,
93
+ body: body ? JSON.stringify(body) : undefined,
94
+ signal: combinedSignal,
95
+ });
96
+ clearTimeout(timeoutId);
97
+ if (!response.ok) {
98
+ const errorText = yield response.text().catch(() => "Unknown error");
99
+ throw new Error(`HTTP ${response.status}: ${response.statusText} - ${errorText}`);
100
+ }
101
+ const reader = (_a = response.body) === null || _a === void 0 ? void 0 : _a.getReader();
102
+ if (!reader) {
103
+ throw new Error("Response body is not readable");
104
+ }
105
+ while (true) {
106
+ const { done, value } = yield reader.read();
107
+ if (done) {
108
+ parser.processFinal({
109
+ onData: onChunk,
110
+ onDone: onComplete,
111
+ });
112
+ break;
113
+ }
114
+ parser.processChunk(value, {
115
+ onData: onChunk,
116
+ onError: error => {
117
+ reader.cancel();
118
+ onError === null || onError === void 0 ? void 0 : onError(error);
119
+ },
120
+ onDone: () => {
121
+ reader.cancel();
122
+ onComplete === null || onComplete === void 0 ? void 0 : onComplete();
123
+ },
124
+ });
125
+ }
126
+ }
127
+ catch (error) {
128
+ clearTimeout(timeoutId);
129
+ if (error instanceof Error && error.name === "AbortError") {
130
+ onError === null || onError === void 0 ? void 0 : onError(new Error("Request aborted"));
131
+ }
132
+ else if (error instanceof Error) {
133
+ onError === null || onError === void 0 ? void 0 : onError(error);
134
+ }
135
+ else {
136
+ onError === null || onError === void 0 ? void 0 : onError(new Error(String(error)));
137
+ }
138
+ }
139
+ });
140
+ performStream();
141
+ return {
142
+ abort: () => {
143
+ clearTimeout(timeoutId);
144
+ abortController.abort();
145
+ },
146
+ };
147
+ };
148
+ exports.createStreamingRequest = createStreamingRequest;
@@ -0,0 +1,45 @@
1
+ export interface RagionConfig {
2
+ apiKey: string;
3
+ timeout?: number;
4
+ retryAttempts?: number;
5
+ retryDelay?: number;
6
+ }
7
+ export interface RequestOptions {
8
+ method?: string;
9
+ headers?: Record<string, string>;
10
+ body?: unknown;
11
+ signal?: AbortSignal;
12
+ timeout?: number;
13
+ }
14
+ export interface ApiResponse<T> {
15
+ data: T | null;
16
+ status: number;
17
+ ok: boolean;
18
+ error?: ApiError;
19
+ requestId?: string;
20
+ }
21
+ export interface ApiError {
22
+ error: string;
23
+ message: string;
24
+ statusCode: number;
25
+ requestId?: string;
26
+ timestamp?: string;
27
+ details?: unknown;
28
+ }
29
+ export interface ChatMessage {
30
+ id: string;
31
+ role: "user" | "assistant";
32
+ content: string;
33
+ timestamp: Date;
34
+ }
35
+ export interface ChatOptions {
36
+ message: string;
37
+ sessionId?: string;
38
+ onChunk?: (chunk: string) => void;
39
+ onComplete?: () => void;
40
+ onError?: (error: Error) => void;
41
+ signal?: AbortSignal;
42
+ }
43
+ export interface StreamingController {
44
+ abort: () => void;
45
+ }
package/dist/types.js ADDED
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
package/package.json ADDED
@@ -0,0 +1,51 @@
1
+ {
2
+ "name": "@ragion/sdk",
3
+ "version": "0.1.0-alpha.0",
4
+ "description": "Official Ragion SDK for JavaScript and TypeScript - External integrations with x-Api-Key",
5
+ "main": "./dist/index.js",
6
+ "module": "./dist/esm/index.js",
7
+ "types": "./dist/index.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "import": "./dist/esm/index.js",
11
+ "require": "./dist/index.js",
12
+ "types": "./dist/index.d.ts"
13
+ }
14
+ },
15
+ "files": [
16
+ "dist"
17
+ ],
18
+ "scripts": {
19
+ "build": "rimraf dist && tsc && tsc --project tsconfig.esm.json",
20
+ "dev": "tsc --watch",
21
+ "clean": "rimraf dist"
22
+ },
23
+ "keywords": [
24
+ "ragion",
25
+ "rag",
26
+ "ai",
27
+ "chat",
28
+ "knowledge-base",
29
+ "sdk"
30
+ ],
31
+ "author": "Ragion",
32
+ "license": "MIT",
33
+ "repository": {
34
+ "type": "git",
35
+ "url": "https://github.com/ragion/ragion-app"
36
+ },
37
+ "engines": {
38
+ "node": ">=18.0.0"
39
+ },
40
+ "devDependencies": {
41
+ "@types/node": "^25.0.0",
42
+ "eslint": "^9.18.0",
43
+ "eslint-config-prettier": "^10.0.1",
44
+ "eslint-plugin-prettier": "^5.2.2",
45
+ "prettier": "^3.4.2",
46
+ "rimraf": "^5.0.0",
47
+ "ts-node": "^10.9.2",
48
+ "typescript": "^5.6.3",
49
+ "typescript-eslint": "^8.20.0"
50
+ }
51
+ }