@qelos/sdk 3.11.3 → 3.11.5

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,170 @@
1
+ // Example usage of the new Qelos AI SDK with sub-SDKs
2
+
3
+ import QelosSDK from '../src';
4
+ // Alternatively, you can import specific sub-SDKs:
5
+ // import { ThreadsSDK, ChatSDK, RAGSDK } from '../src/ai';
6
+
7
+ // Initialize the SDK
8
+ const sdk = new QelosSDK({
9
+ appUrl: 'https://your-qelos-instance.com',
10
+ fetch: globalThis.fetch || require('node-fetch'),
11
+ accessToken: 'your-access-token'
12
+ });
13
+
14
+ // ===== THREADS SUB-SDK =====
15
+ async function threadExamples() {
16
+ // Create a new thread
17
+ const thread = await sdk.ai.threads.create({
18
+ integration: 'integration-id',
19
+ title: 'Customer Support Chat'
20
+ });
21
+
22
+ // Get a specific thread
23
+ const retrievedThread = await sdk.ai.threads.getOne(thread._id!);
24
+
25
+ // List threads with filters
26
+ const threads = await sdk.ai.threads.list({
27
+ integration: 'integration-id',
28
+ limit: 10,
29
+ page: 1
30
+ });
31
+
32
+ // Delete a thread
33
+ await sdk.ai.threads.delete(thread._id!);
34
+ }
35
+
36
+ // ===== CHAT SUB-SDK =====
37
+ async function chatExamples() {
38
+ const integrationId = 'your-integration-id';
39
+ const messages = [
40
+ { role: 'user', content: 'Hello, how can you help me?' }
41
+ ];
42
+
43
+ // Non-streaming chat
44
+ const response = await sdk.ai.chat.chat(integrationId, {
45
+ messages,
46
+ model: 'gpt-4',
47
+ temperature: 0.7,
48
+ queryParams: { bypassAdmin: true } // Optional query params
49
+ });
50
+
51
+ console.log(response.choices[0].message.content);
52
+
53
+ // Streaming chat
54
+ const stream = await sdk.ai.chat.stream(integrationId, {
55
+ messages,
56
+ stream: true
57
+ });
58
+
59
+ // Parse the SSE stream
60
+ const processor = sdk.ai.chat.parseSSEStream(stream);
61
+ for await (const chunk of processor) {
62
+ if (chunk.choices?.[0]?.delta?.content) {
63
+ console.log(chunk.choices[0].delta.content);
64
+ }
65
+ }
66
+
67
+ // Chat in a thread
68
+ const threadId = 'thread-id';
69
+ const threadResponse = await sdk.ai.chat.chatInThread(integrationId, threadId, {
70
+ messages
71
+ });
72
+
73
+ // Streaming chat in a thread
74
+ const threadStream = await sdk.ai.chat.streamInThread(integrationId, threadId, {
75
+ messages,
76
+ stream: true
77
+ });
78
+
79
+ for await (const chunk of sdk.ai.chat.parseSSEStream(threadStream)) {
80
+ console.log(chunk);
81
+ }
82
+ }
83
+
84
+ // ===== RAG SUB-SDK =====
85
+ async function ragExamples() {
86
+ const sourceId = 'your-ai-source-id';
87
+
88
+ // Create vector storage
89
+ const storage = await sdk.ai.rag.createStorage(sourceId, {
90
+ integrationId: 'integration-id',
91
+ scope: 'thread', // 'thread' | 'user' | 'workspace' | 'tenant'
92
+ subjectId: 'thread-id', // Required for non-tenant scopes
93
+ expirationAfterDays: 30
94
+ });
95
+
96
+ // Upload content to storage
97
+ const uploadResult = await sdk.ai.rag.uploadContent(sourceId, {
98
+ integrationId: 'integration-id',
99
+ content: 'This is the content to be indexed for RAG.',
100
+ fileName: 'document.txt',
101
+ metadata: { type: 'documentation', category: 'help' }
102
+ });
103
+
104
+ // Upload JSON content
105
+ const jsonUpload = await sdk.ai.rag.uploadContent(sourceId, {
106
+ integrationId: 'integration-id',
107
+ content: { title: 'Product Info', description: '...' },
108
+ fileName: 'product-info.json'
109
+ });
110
+
111
+ // Clear specific files
112
+ const clearResult = await sdk.ai.rag.clearStorage(sourceId, {
113
+ integrationId: 'integration-id',
114
+ fileIds: [uploadResult.fileId]
115
+ });
116
+
117
+ // Clear all files (if no fileIds provided)
118
+ await sdk.ai.rag.clearStorage(sourceId, {
119
+ integrationId: 'integrationId'
120
+ });
121
+
122
+ // Get vector stores (internal API - server-side only)
123
+ const stores = await sdk.ai.rag.getVectorStores({
124
+ scope: 'thread',
125
+ subjectId: 'thread-id'
126
+ });
127
+ }
128
+
129
+ // ===== COMBINED EXAMPLE =====
130
+ async function combinedExample() {
131
+ // 1. Create a thread
132
+ const thread = await sdk.ai.threads.create({
133
+ integration: 'integration-id',
134
+ title: 'RAG-enabled Chat'
135
+ });
136
+
137
+ // 2. Upload knowledge base content
138
+ await sdk.ai.rag.uploadContent('source-id', {
139
+ integrationId: 'integration-id',
140
+ content: 'Your knowledge base content here...',
141
+ fileName: 'knowledge-base.txt'
142
+ });
143
+
144
+ // 3. Chat with RAG
145
+ const stream = await sdk.ai.chat.streamInThread(
146
+ 'integration-id',
147
+ thread._id!,
148
+ {
149
+ messages: [
150
+ { role: 'user', content: 'What do you know about the topic?' }
151
+ ],
152
+ stream: true
153
+ }
154
+ );
155
+
156
+ // 4. Process the response
157
+ for await (const chunk of sdk.ai.chat.parseSSEStream(stream)) {
158
+ if (chunk.choices?.[0]?.delta?.content) {
159
+ console.log(chunk.choices[0].delta.content);
160
+ }
161
+ }
162
+ }
163
+
164
+ // Export for reference
165
+ export {
166
+ threadExamples,
167
+ chatExamples,
168
+ ragExamples,
169
+ combinedExample
170
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@qelos/sdk",
3
- "version": "3.11.3",
3
+ "version": "3.11.5",
4
4
  "description": "SDK for Qelos API",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",
@@ -12,6 +12,9 @@
12
12
  "administrator/*": [
13
13
  "./dist/administrator/*.d.ts"
14
14
  ],
15
+ "ai/*": [
16
+ "./dist/ai/*.d.ts"
17
+ ],
15
18
  "workspaces": [
16
19
  "./dist/workspaces.d.ts"
17
20
  ],
@@ -45,6 +48,18 @@
45
48
  "require": "./dist/administrator/*.js",
46
49
  "default": "./dist/administrator/*.js"
47
50
  },
51
+ "./ai": {
52
+ "types": "./dist/ai/index.d.ts",
53
+ "import": "./dist/ai/index.js",
54
+ "require": "./dist/ai/index.js",
55
+ "default": "./dist/ai/index.js"
56
+ },
57
+ "./ai/*": {
58
+ "types": "./dist/ai/*.d.ts",
59
+ "import": "./dist/ai/*.js",
60
+ "require": "./dist/ai/*.js",
61
+ "default": "./dist/ai/*.js"
62
+ },
48
63
  "./workspaces": {
49
64
  "types": "./dist/workspaces.d.ts",
50
65
  "import": "./dist/workspaces.js",
@@ -87,10 +102,10 @@
87
102
  "access": "public"
88
103
  },
89
104
  "dependencies": {
90
- "@qelos/global-types": "^3.9.0"
105
+ "@qelos/global-types": "^3.11.4"
91
106
  },
92
107
  "devDependencies": {
93
- "tsx": "^4.20.4",
108
+ "tsx": "^4.21.0",
94
109
  "typescript": "^4.9.5"
95
110
  },
96
111
  "gitHead": "13468bc51557291935b96b1aeaa837b8b52861e8",
@@ -1,130 +1,18 @@
1
- import { QelosSDKOptions } from './types';
2
- import BaseSDK from './base-sdk';
3
-
4
- export interface IMessage {
5
- role: string;
6
- content: string;
7
- timestamp?: Date | string;
8
- tool_calls?: any[];
9
- name?: string;
10
- tool_call_id?: string;
11
- function_call?: any;
12
- message_id?: string;
13
- }
14
-
15
- export interface IMessageSummary {
16
- fromIndex: number;
17
- toIndex: number;
18
- summary: string;
19
- }
20
-
21
- export interface IThread {
22
- _id?: string;
23
- integration: string;
24
- title?: string;
25
- messages: IMessage[];
26
- messageSummaries: IMessageSummary[];
27
- created?: Date;
28
- updated?: Date;
29
- user?: string;
30
- workspace?: string;
31
- }
32
-
33
- export interface IChatCompletionOptions {
34
- messages: IMessage[];
35
- model?: string;
36
- temperature?: number;
37
- top_p?: number;
38
- frequency_penalty?: number;
39
- presence_penalty?: number;
40
- stop?: string | string[];
41
- max_tokens?: number;
42
- response_format?: any;
43
- context?: Record<string, any>;
44
- stream?: boolean;
45
- }
46
-
47
- export interface ISSEStreamProcessor extends AsyncIterable<any> {
48
- processManually(onData: (data: any) => void | boolean): Promise<void>;
49
- }
50
-
51
- export interface IChatCompletionResponse {
52
- id: string;
53
- object: string;
54
- created: number;
55
- model: string;
56
- choices: Array<{
57
- index: number;
58
- message: IMessage;
59
- finish_reason: string;
60
- }>;
61
- usage?: {
62
- prompt_tokens: number;
63
- completion_tokens: number;
64
- total_tokens: number;
65
- };
66
- }
67
-
68
- export interface ICreateThreadRequest {
69
- integration: string;
70
- title?: string;
71
- }
72
-
73
- export default class QlAI extends BaseSDK {
1
+ import BaseSDK from '../base-sdk';
2
+ import { QelosSDKOptions } from '../types';
3
+ import {
4
+ IChatCompletionOptions,
5
+ IChatCompletionResponse,
6
+ ISSEStreamProcessor
7
+ } from '.';
8
+
9
+ export default class ChatSDK extends BaseSDK {
74
10
  private relativePath = '/api/ai';
11
+
75
12
  constructor(options: QelosSDKOptions) {
76
13
  super(options);
77
14
  }
78
15
 
79
- // Thread CRUD Operations
80
-
81
- /**
82
- * Create a new thread
83
- */
84
- createThread(data: ICreateThreadRequest): Promise<IThread> {
85
- return this.callJsonApi<IThread>(this.relativePath + '/threads', {
86
- method: 'POST',
87
- headers: { 'content-type': 'application/json' },
88
- body: JSON.stringify(data)
89
- });
90
- }
91
-
92
- /**
93
- * Get a specific thread by ID
94
- */
95
- getThread(threadId: string): Promise<IThread> {
96
- return this.callJsonApi<IThread>(`${this.relativePath}/threads/${threadId}${this.getQueryParams({})}`);
97
- }
98
-
99
- /**
100
- * List threads with optional filters
101
- */
102
- listThreads(options?: {
103
- integration?: string;
104
- limit?: number;
105
- page?: number;
106
- sort?: string;
107
- user?: string;
108
- workspace?: string;
109
- [key: string]: string | number;
110
- }): Promise<IThread[]> {
111
- const queryParams = this.getQueryParams(options);
112
- return this.callJsonApi<IThread[]>(
113
- `${this.relativePath}/threads${queryParams}`
114
- );
115
- }
116
-
117
- /**
118
- * Delete a thread
119
- */
120
- deleteThread(threadId: string): Promise<{ success: boolean }> {
121
- return this.callJsonApi<{ success: boolean }>(`${this.relativePath}/threads/${threadId}${this.getQueryParams({})}`, {
122
- method: 'DELETE'
123
- });
124
- }
125
-
126
- // Chat Completion Operations
127
-
128
16
  /**
129
17
  * Create a chat completion without a thread
130
18
  */
@@ -132,7 +20,8 @@ export default class QlAI extends BaseSDK {
132
20
  integrationId: string,
133
21
  options: IChatCompletionOptions
134
22
  ): Promise<IChatCompletionResponse> {
135
- return this.callJsonApi<IChatCompletionResponse>(`${this.relativePath}/${integrationId}/chat-completion${this.getQueryParams({})}`, {
23
+ const queryParams = this.getQueryParams(options.queryParams || {});
24
+ return this.callJsonApi<IChatCompletionResponse>(`${this.relativePath}/${integrationId}/chat-completion${queryParams}`, {
136
25
  method: 'POST',
137
26
  headers: { 'content-type': 'application/json' },
138
27
  body: JSON.stringify(options)
@@ -147,8 +36,9 @@ export default class QlAI extends BaseSDK {
147
36
  threadId: string,
148
37
  options: IChatCompletionOptions
149
38
  ): Promise<IChatCompletionResponse> {
39
+ const queryParams = this.getQueryParams(options.queryParams || {});
150
40
  return this.callJsonApi<IChatCompletionResponse>(
151
- `${this.relativePath}/${integrationId}/chat-completion/${threadId}${this.getQueryParams({})}`,
41
+ `${this.relativePath}/${integrationId}/chat-completion/${threadId}${queryParams}`,
152
42
  {
153
43
  method: 'POST',
154
44
  headers: { 'content-type': 'application/json' },
@@ -161,11 +51,12 @@ export default class QlAI extends BaseSDK {
161
51
  * Create a streaming chat completion without a thread
162
52
  * Returns a ReadableStream for Server-Sent Events
163
53
  */
164
- async streamChat(
54
+ async stream(
165
55
  integrationId: string,
166
56
  options: IChatCompletionOptions
167
57
  ): Promise<ReadableStream<Uint8Array>> {
168
- const response = await this.callApi(`${this.relativePath}/${integrationId}/chat-completion${this.getQueryParams({ stream: true })}`, {
58
+ const queryParams = this.getQueryParams({ ...options.queryParams, stream: true });
59
+ const response = await this.callApi(`${this.relativePath}/${integrationId}/chat-completion${queryParams}`, {
169
60
  method: 'POST',
170
61
  headers: {
171
62
  'content-type': 'application/json',
@@ -185,13 +76,14 @@ export default class QlAI extends BaseSDK {
185
76
  * Create a streaming chat completion with a thread
186
77
  * Returns a ReadableStream for Server-Sent Events
187
78
  */
188
- async streamChatInThread(
79
+ async streamInThread(
189
80
  integrationId: string,
190
81
  threadId: string,
191
82
  options: IChatCompletionOptions
192
83
  ): Promise<ReadableStream<Uint8Array>> {
84
+ const queryParams = this.getQueryParams({ ...options.queryParams, stream: true });
193
85
  const response = await this.callApi(
194
- `${this.relativePath}/${integrationId}/chat-completion/${threadId}${this.getQueryParams({ stream: true })}`,
86
+ `${this.relativePath}/${integrationId}/chat-completion/${threadId}${queryParams}`,
195
87
  {
196
88
  method: 'POST',
197
89
  headers: {
@@ -0,0 +1,181 @@
1
+ import { QelosSDKOptions } from '../types';
2
+ import BaseSDK from '../base-sdk';
3
+ import ThreadsSDK from './threads';
4
+ import ChatSDK from './chat';
5
+ import RagSDK from './rag';
6
+ import {
7
+ IMessage,
8
+ IMessageSummary,
9
+ IThread,
10
+ IChatCompletionOptions,
11
+ ISSEStreamProcessor,
12
+ IChatCompletionResponse,
13
+ ICreateThreadRequest,
14
+ ICreateVectorStorageRequest,
15
+ IUploadContentRequest,
16
+ IClearStorageRequest,
17
+ IVectorStore
18
+ } from './types';
19
+
20
+ export * from './types';
21
+
22
+ // Re-export types for backward compatibility
23
+ export {
24
+ IMessage,
25
+ IMessageSummary,
26
+ IThread,
27
+ IChatCompletionOptions,
28
+ ISSEStreamProcessor,
29
+ IChatCompletionResponse,
30
+ ICreateThreadRequest,
31
+ ICreateVectorStorageRequest,
32
+ IUploadContentRequest,
33
+ IClearStorageRequest,
34
+ IVectorStore
35
+ };
36
+
37
+ /**
38
+ * Qelos AI SDK - Main SDK class with three sub-SDKs
39
+ *
40
+ * This class provides access to AI functionality through three sub-SDKs:
41
+ * - `threads`: Thread CRUD operations for managing conversation threads
42
+ * - `chat`: Chat completion operations (streaming and non-streaming)
43
+ * - `rag`: Vector storage management for Retrieval-Augmented Generation
44
+ *
45
+ * @example
46
+ * ```typescript
47
+ * const sdk = new QelosSDK({
48
+ * appUrl: 'https://your-qelos-instance.com',
49
+ * fetch: globalThis.fetch,
50
+ * accessToken: 'your-token'
51
+ * });
52
+ *
53
+ * // Create a thread
54
+ * const thread = await sdk.ai.threads.create({ integration: 'id' });
55
+ *
56
+ * // Stream chat
57
+ * const stream = await sdk.ai.chat.stream('id', { messages: [...] });
58
+ * for await (const chunk of sdk.ai.chat.parseSSEStream(stream)) {
59
+ * console.log(chunk);
60
+ * }
61
+ *
62
+ * // Upload RAG content
63
+ * await sdk.ai.rag.uploadContent('sourceId', { content: '...' });
64
+ * ```
65
+ */
66
+ // Main AI SDK with sub-SDKs
67
+ export default class QlAI extends BaseSDK {
68
+ /**
69
+ * Threads sub-SDK for managing conversation threads
70
+ */
71
+ public threads: ThreadsSDK;
72
+
73
+ /**
74
+ * Chat sub-SDK for chat completion operations
75
+ */
76
+ public chat: ChatSDK;
77
+
78
+ /**
79
+ * RAG sub-SDK for vector storage management
80
+ */
81
+ public rag: RagSDK;
82
+
83
+ /**
84
+ * Initialize the AI SDK with sub-SDKs
85
+ *
86
+ * @param options - SDK configuration options
87
+ */
88
+ constructor(options: QelosSDKOptions) {
89
+ super(options);
90
+ this.threads = new ThreadsSDK(options);
91
+ this.chat = new ChatSDK(options);
92
+ this.rag = new RagSDK(options);
93
+ }
94
+
95
+ // Legacy methods for backward compatibility
96
+
97
+ /**
98
+ * @deprecated Use sdk.threads.create() instead
99
+ */
100
+ createThread(data: ICreateThreadRequest): Promise<IThread> {
101
+ return this.threads.create(data);
102
+ }
103
+
104
+ /**
105
+ * @deprecated Use sdk.threads.getOne() instead
106
+ */
107
+ getThread(threadId: string): Promise<IThread> {
108
+ return this.threads.getOne(threadId);
109
+ }
110
+
111
+ /**
112
+ * @deprecated Use sdk.threads.list() instead
113
+ */
114
+ listThreads(options?: {
115
+ integration?: string;
116
+ limit?: number;
117
+ page?: number;
118
+ sort?: string;
119
+ user?: string;
120
+ workspace?: string;
121
+ [key: string]: string | number;
122
+ }): Promise<IThread[]> {
123
+ return this.threads.list(options);
124
+ }
125
+
126
+ /**
127
+ * @deprecated Use sdk.threads.delete() instead
128
+ */
129
+ deleteThread(threadId: string): Promise<{ success: boolean }> {
130
+ return this.threads.delete(threadId);
131
+ }
132
+
133
+ /**
134
+ * @deprecated Use sdk.chat.chat() instead
135
+ */
136
+ chatCompletion(
137
+ integrationId: string,
138
+ options: IChatCompletionOptions
139
+ ): Promise<IChatCompletionResponse> {
140
+ return this.chat.chat(integrationId, options);
141
+ }
142
+
143
+ /**
144
+ * @deprecated Use sdk.chat.chatInThread() instead
145
+ */
146
+ chatCompletionInThread(
147
+ integrationId: string,
148
+ threadId: string,
149
+ options: IChatCompletionOptions
150
+ ): Promise<IChatCompletionResponse> {
151
+ return this.chat.chatInThread(integrationId, threadId, options);
152
+ }
153
+
154
+ /**
155
+ * @deprecated Use sdk.chat.stream() instead
156
+ */
157
+ async streamChat(
158
+ integrationId: string,
159
+ options: IChatCompletionOptions
160
+ ): Promise<ReadableStream<Uint8Array>> {
161
+ return this.chat.stream(integrationId, options);
162
+ }
163
+
164
+ /**
165
+ * @deprecated Use sdk.chat.streamInThread() instead
166
+ */
167
+ async streamChatInThread(
168
+ integrationId: string,
169
+ threadId: string,
170
+ options: IChatCompletionOptions
171
+ ): Promise<ReadableStream<Uint8Array>> {
172
+ return this.chat.streamInThread(integrationId, threadId, options);
173
+ }
174
+
175
+ /**
176
+ * @deprecated Use sdk.chat.parseSSEStream() instead
177
+ */
178
+ parseSSEStream(stream: ReadableStream<Uint8Array>): ISSEStreamProcessor {
179
+ return this.chat.parseSSEStream(stream);
180
+ }
181
+ }
package/src/ai/rag.ts ADDED
@@ -0,0 +1,72 @@
1
+ import BaseSDK from '../base-sdk';
2
+ import { QelosSDKOptions } from '../types';
3
+ import {
4
+ ICreateVectorStorageRequest,
5
+ IUploadContentRequest,
6
+ IClearStorageRequest,
7
+ IVectorStore
8
+ } from '.';
9
+
10
+ export default class RagSDK extends BaseSDK {
11
+ private relativePath = '/api/ai';
12
+
13
+ constructor(options: QelosSDKOptions) {
14
+ super(options);
15
+ }
16
+
17
+ /**
18
+ * Create a vector storage for RAG
19
+ */
20
+ createStorage(
21
+ sourceId: string,
22
+ data: ICreateVectorStorageRequest
23
+ ): Promise<{
24
+ success: boolean;
25
+ message: string;
26
+ vectorStore: IVectorStore;
27
+ }> {
28
+ return this.callJsonApi<any>(`${this.relativePath}/sources/${sourceId}/storage${this.getQueryParams({})}`, {
29
+ method: 'POST',
30
+ headers: { 'content-type': 'application/json' },
31
+ body: JSON.stringify(data)
32
+ });
33
+ }
34
+
35
+ /**
36
+ * Upload content to vector storage
37
+ */
38
+ uploadContent(
39
+ sourceId: string,
40
+ data: IUploadContentRequest
41
+ ): Promise<{
42
+ success: boolean;
43
+ message: string;
44
+ fileId: string;
45
+ vectorStoreId: string;
46
+ }> {
47
+ return this.callJsonApi<any>(`${this.relativePath}/sources/${sourceId}/storage/upload${this.getQueryParams({})}`, {
48
+ method: 'POST',
49
+ headers: { 'content-type': 'application/json' },
50
+ body: JSON.stringify(data)
51
+ });
52
+ }
53
+
54
+ /**
55
+ * Clear files from vector storage
56
+ */
57
+ clearStorage(
58
+ sourceId: string,
59
+ data: IClearStorageRequest
60
+ ): Promise<{
61
+ success: boolean;
62
+ message: string;
63
+ clearedCount: number;
64
+ vectorStoreId: string;
65
+ }> {
66
+ return this.callJsonApi<any>(`${this.relativePath}/sources/${sourceId}/storage/clear${this.getQueryParams({})}`, {
67
+ method: 'POST',
68
+ headers: { 'content-type': 'application/json' },
69
+ body: JSON.stringify(data)
70
+ });
71
+ }
72
+ }