@beltoinc/slyos-sdk 1.5.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 +434 -0
- package/create-chatbot.sh +634 -0
- package/dist/index.d.ts +278 -0
- package/dist/index.js +1483 -0
- package/package.json +35 -0
- package/src/index.ts +1856 -0
- package/tsconfig.json +15 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,278 @@
|
|
|
1
|
+
interface SlyOSConfig {
|
|
2
|
+
apiKey: string;
|
|
3
|
+
apiUrl?: string;
|
|
4
|
+
onProgress?: ProgressCallback;
|
|
5
|
+
onEvent?: EventCallback;
|
|
6
|
+
}
|
|
7
|
+
interface GenerateOptions {
|
|
8
|
+
temperature?: number;
|
|
9
|
+
maxTokens?: number;
|
|
10
|
+
topP?: number;
|
|
11
|
+
}
|
|
12
|
+
interface TranscribeOptions {
|
|
13
|
+
language?: string;
|
|
14
|
+
returnTimestamps?: boolean;
|
|
15
|
+
}
|
|
16
|
+
type ModelCategory = 'llm' | 'stt';
|
|
17
|
+
type QuantizationLevel = 'q4' | 'q8' | 'fp16' | 'fp32';
|
|
18
|
+
interface DeviceProfile {
|
|
19
|
+
cpuCores: number;
|
|
20
|
+
memoryMB: number;
|
|
21
|
+
estimatedStorageMB: number;
|
|
22
|
+
platform: 'web' | 'nodejs';
|
|
23
|
+
os: string;
|
|
24
|
+
recommendedQuant: QuantizationLevel;
|
|
25
|
+
maxContextWindow: number;
|
|
26
|
+
deviceFingerprint?: string;
|
|
27
|
+
gpuRenderer?: string;
|
|
28
|
+
gpuVramMb?: number;
|
|
29
|
+
screenWidth?: number;
|
|
30
|
+
screenHeight?: number;
|
|
31
|
+
pixelRatio?: number;
|
|
32
|
+
browserName?: string;
|
|
33
|
+
browserVersion?: string;
|
|
34
|
+
networkType?: string;
|
|
35
|
+
latencyToApiMs?: number;
|
|
36
|
+
timezone?: string;
|
|
37
|
+
wasmAvailable?: boolean;
|
|
38
|
+
webgpuAvailable?: boolean;
|
|
39
|
+
}
|
|
40
|
+
interface ProgressEvent {
|
|
41
|
+
stage: 'initializing' | 'profiling' | 'downloading' | 'loading' | 'ready' | 'generating' | 'transcribing' | 'error';
|
|
42
|
+
progress: number;
|
|
43
|
+
message: string;
|
|
44
|
+
detail?: any;
|
|
45
|
+
}
|
|
46
|
+
interface SlyEvent {
|
|
47
|
+
type: 'auth' | 'device_registered' | 'device_profiled' | 'model_download_start' | 'model_download_progress' | 'model_loaded' | 'inference_start' | 'inference_complete' | 'error' | 'fallback_success' | 'fallback_error' | 'telemetry_flushed';
|
|
48
|
+
data?: any;
|
|
49
|
+
timestamp: number;
|
|
50
|
+
}
|
|
51
|
+
type ProgressCallback = (event: ProgressEvent) => void;
|
|
52
|
+
type EventCallback = (event: SlyEvent) => void;
|
|
53
|
+
interface OpenAIMessage {
|
|
54
|
+
role: 'system' | 'user' | 'assistant';
|
|
55
|
+
content: string;
|
|
56
|
+
}
|
|
57
|
+
interface OpenAIChatCompletionRequest {
|
|
58
|
+
messages: OpenAIMessage[];
|
|
59
|
+
temperature?: number;
|
|
60
|
+
top_p?: number;
|
|
61
|
+
max_tokens?: number;
|
|
62
|
+
frequency_penalty?: number;
|
|
63
|
+
presence_penalty?: number;
|
|
64
|
+
stop?: string | string[];
|
|
65
|
+
}
|
|
66
|
+
interface OpenAIChoice {
|
|
67
|
+
index: number;
|
|
68
|
+
message: OpenAIMessage;
|
|
69
|
+
finish_reason: string;
|
|
70
|
+
}
|
|
71
|
+
interface OpenAIUsage {
|
|
72
|
+
prompt_tokens: number;
|
|
73
|
+
completion_tokens: number;
|
|
74
|
+
total_tokens: number;
|
|
75
|
+
}
|
|
76
|
+
interface OpenAIChatCompletionResponse {
|
|
77
|
+
id: string;
|
|
78
|
+
object: 'chat.completion';
|
|
79
|
+
created: number;
|
|
80
|
+
model: string;
|
|
81
|
+
choices: OpenAIChoice[];
|
|
82
|
+
usage: OpenAIUsage;
|
|
83
|
+
}
|
|
84
|
+
interface BedrockTextGenerationConfig {
|
|
85
|
+
maxTokenCount?: number;
|
|
86
|
+
temperature?: number;
|
|
87
|
+
topP?: number;
|
|
88
|
+
topK?: number;
|
|
89
|
+
stopSequences?: string[];
|
|
90
|
+
}
|
|
91
|
+
interface BedrockInvokeRequest {
|
|
92
|
+
inputText: string;
|
|
93
|
+
textGenerationConfig?: BedrockTextGenerationConfig;
|
|
94
|
+
}
|
|
95
|
+
interface BedrockResult {
|
|
96
|
+
outputText: string;
|
|
97
|
+
tokenCount: number;
|
|
98
|
+
}
|
|
99
|
+
interface BedrockInvokeResponse {
|
|
100
|
+
results: BedrockResult[];
|
|
101
|
+
input_text_token_count?: number;
|
|
102
|
+
}
|
|
103
|
+
type FallbackProvider = 'openai' | 'bedrock';
|
|
104
|
+
interface FallbackConfig {
|
|
105
|
+
provider: FallbackProvider;
|
|
106
|
+
apiKey: string;
|
|
107
|
+
model: string;
|
|
108
|
+
region?: string;
|
|
109
|
+
}
|
|
110
|
+
interface SlyOSConfigWithFallback extends SlyOSConfig {
|
|
111
|
+
fallback?: FallbackConfig;
|
|
112
|
+
}
|
|
113
|
+
interface OpenAICompatibleClient {
|
|
114
|
+
chat: {
|
|
115
|
+
completions: {
|
|
116
|
+
create(request: OpenAIChatCompletionRequest & {
|
|
117
|
+
model: string;
|
|
118
|
+
}): Promise<OpenAIChatCompletionResponse>;
|
|
119
|
+
};
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
interface RAGOptions {
|
|
123
|
+
knowledgeBaseId: string;
|
|
124
|
+
query: string;
|
|
125
|
+
topK?: number;
|
|
126
|
+
modelId: string;
|
|
127
|
+
temperature?: number;
|
|
128
|
+
maxTokens?: number;
|
|
129
|
+
}
|
|
130
|
+
interface RAGChunk {
|
|
131
|
+
id: string;
|
|
132
|
+
documentId: string;
|
|
133
|
+
documentName: string;
|
|
134
|
+
content: string;
|
|
135
|
+
similarityScore: number;
|
|
136
|
+
metadata?: Record<string, any>;
|
|
137
|
+
}
|
|
138
|
+
interface RAGResponse {
|
|
139
|
+
query: string;
|
|
140
|
+
retrievedChunks: RAGChunk[];
|
|
141
|
+
generatedResponse: string;
|
|
142
|
+
context: string;
|
|
143
|
+
latencyMs: number;
|
|
144
|
+
tierUsed: 1 | 2 | 3;
|
|
145
|
+
}
|
|
146
|
+
interface OfflineIndex {
|
|
147
|
+
metadata: {
|
|
148
|
+
kb_id: string;
|
|
149
|
+
kb_name: string;
|
|
150
|
+
chunk_size: number;
|
|
151
|
+
embedding_dim: number;
|
|
152
|
+
total_chunks: number;
|
|
153
|
+
synced_at: string;
|
|
154
|
+
expires_at: string;
|
|
155
|
+
sync_token: string;
|
|
156
|
+
};
|
|
157
|
+
chunks: Array<{
|
|
158
|
+
id: string;
|
|
159
|
+
document_id: string;
|
|
160
|
+
document_name: string;
|
|
161
|
+
content: string;
|
|
162
|
+
chunk_index: number;
|
|
163
|
+
embedding: number[] | null;
|
|
164
|
+
metadata: Record<string, any>;
|
|
165
|
+
}>;
|
|
166
|
+
}
|
|
167
|
+
declare class SlyOS {
|
|
168
|
+
private apiKey;
|
|
169
|
+
private apiUrl;
|
|
170
|
+
private deviceId;
|
|
171
|
+
private token;
|
|
172
|
+
private models;
|
|
173
|
+
private deviceProfile;
|
|
174
|
+
private onProgress;
|
|
175
|
+
private onEvent;
|
|
176
|
+
private fallbackConfig;
|
|
177
|
+
private modelContextWindow;
|
|
178
|
+
private telemetryBuffer;
|
|
179
|
+
private telemetryFlushTimer;
|
|
180
|
+
private static readonly TELEMETRY_BATCH_SIZE;
|
|
181
|
+
private static readonly TELEMETRY_FLUSH_INTERVAL;
|
|
182
|
+
constructor(config: SlyOSConfigWithFallback);
|
|
183
|
+
private emitProgress;
|
|
184
|
+
private emitEvent;
|
|
185
|
+
private recordTelemetry;
|
|
186
|
+
private flushTelemetry;
|
|
187
|
+
analyzeDevice(): Promise<DeviceProfile>;
|
|
188
|
+
getDeviceProfile(): DeviceProfile | null;
|
|
189
|
+
getModelContextWindow(): number;
|
|
190
|
+
getDeviceId(): string;
|
|
191
|
+
getSdkVersion(): string;
|
|
192
|
+
destroy(): Promise<void>;
|
|
193
|
+
recommendModel(category?: ModelCategory): {
|
|
194
|
+
modelId: string;
|
|
195
|
+
quant: QuantizationLevel;
|
|
196
|
+
contextWindow: number;
|
|
197
|
+
reason: string;
|
|
198
|
+
} | null;
|
|
199
|
+
initialize(): Promise<DeviceProfile>;
|
|
200
|
+
getAvailableModels(): Record<string, {
|
|
201
|
+
models: {
|
|
202
|
+
id: string;
|
|
203
|
+
sizesMB: Record<string, number>;
|
|
204
|
+
minRAM_MB: Record<string, number>;
|
|
205
|
+
}[];
|
|
206
|
+
}>;
|
|
207
|
+
searchModels(query: string, options?: {
|
|
208
|
+
limit?: number;
|
|
209
|
+
task?: string;
|
|
210
|
+
}): Promise<Array<{
|
|
211
|
+
id: string;
|
|
212
|
+
name: string;
|
|
213
|
+
downloads: number;
|
|
214
|
+
likes: number;
|
|
215
|
+
task: string;
|
|
216
|
+
size_category: string;
|
|
217
|
+
}>>;
|
|
218
|
+
canRunModel(modelId: string, quant?: QuantizationLevel): {
|
|
219
|
+
canRun: boolean;
|
|
220
|
+
reason: string;
|
|
221
|
+
recommendedQuant: QuantizationLevel;
|
|
222
|
+
};
|
|
223
|
+
loadModel(modelId: string, options?: {
|
|
224
|
+
quant?: QuantizationLevel;
|
|
225
|
+
}): Promise<void>;
|
|
226
|
+
generate(modelId: string, prompt: string, options?: GenerateOptions): Promise<string>;
|
|
227
|
+
transcribe(modelId: string, audioInput: any, options?: TranscribeOptions): Promise<string>;
|
|
228
|
+
chatCompletion(modelId: string, request: OpenAIChatCompletionRequest): Promise<OpenAIChatCompletionResponse>;
|
|
229
|
+
bedrockInvoke(modelId: string, request: BedrockInvokeRequest): Promise<BedrockInvokeResponse>;
|
|
230
|
+
private fallbackToOpenAI;
|
|
231
|
+
private fallbackToBedrock;
|
|
232
|
+
private fallbackToOpenAICloud;
|
|
233
|
+
private fallbackToBedrockCloud;
|
|
234
|
+
private invokeBedrockCloud;
|
|
235
|
+
private mapModelToOpenAI;
|
|
236
|
+
private localEmbeddingModel;
|
|
237
|
+
private offlineIndexes;
|
|
238
|
+
/**
|
|
239
|
+
* Tier 2: Cloud-indexed RAG with local inference.
|
|
240
|
+
* Retrieves relevant chunks from server, generates response locally.
|
|
241
|
+
*/
|
|
242
|
+
ragQuery(options: RAGOptions): Promise<RAGResponse>;
|
|
243
|
+
/**
|
|
244
|
+
* Tier 1: Fully local RAG. Zero network calls.
|
|
245
|
+
* Documents are chunked/embedded on-device, retrieval and generation all local.
|
|
246
|
+
*/
|
|
247
|
+
ragQueryLocal(options: RAGOptions & {
|
|
248
|
+
documents: Array<{
|
|
249
|
+
content: string;
|
|
250
|
+
name?: string;
|
|
251
|
+
}>;
|
|
252
|
+
}): Promise<RAGResponse>;
|
|
253
|
+
/**
|
|
254
|
+
* Tier 3: Offline RAG using a synced knowledge base.
|
|
255
|
+
* First call syncKnowledgeBase(), then use this for offline queries.
|
|
256
|
+
*/
|
|
257
|
+
ragQueryOffline(options: RAGOptions): Promise<RAGResponse>;
|
|
258
|
+
/**
|
|
259
|
+
* Sync a knowledge base for offline use (Tier 3).
|
|
260
|
+
* Downloads chunks + embeddings from server, stores locally.
|
|
261
|
+
*/
|
|
262
|
+
syncKnowledgeBase(knowledgeBaseId: string, deviceId?: string): Promise<{
|
|
263
|
+
chunkCount: number;
|
|
264
|
+
sizeMb: number;
|
|
265
|
+
expiresAt: string;
|
|
266
|
+
}>;
|
|
267
|
+
private loadEmbeddingModel;
|
|
268
|
+
private embedTextLocal;
|
|
269
|
+
private cosineSimilarity;
|
|
270
|
+
private chunkTextLocal;
|
|
271
|
+
static openaiCompatible(config: {
|
|
272
|
+
apiKey: string;
|
|
273
|
+
apiUrl?: string;
|
|
274
|
+
fallback?: FallbackConfig;
|
|
275
|
+
}): OpenAICompatibleClient;
|
|
276
|
+
}
|
|
277
|
+
export default SlyOS;
|
|
278
|
+
export type { SlyOSConfig, SlyOSConfigWithFallback, GenerateOptions, TranscribeOptions, DeviceProfile, ProgressEvent, SlyEvent, QuantizationLevel, ModelCategory, OpenAIMessage, OpenAIChatCompletionRequest, OpenAIChatCompletionResponse, OpenAIChoice, OpenAIUsage, BedrockTextGenerationConfig, BedrockInvokeRequest, BedrockInvokeResponse, BedrockResult, FallbackConfig, FallbackProvider, OpenAICompatibleClient, RAGOptions, RAGChunk, RAGResponse, OfflineIndex, };
|