@sybil-studio-devs/sdk 0.1.0 → 0.1.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.
@@ -0,0 +1,348 @@
1
+ // src/client.ts
2
+ var DEFAULT_BASE_URL = "https://app.sybil.studio/api/sdk/v1";
3
+ var DEFAULT_TIMEOUT = 3e4;
4
+ var SybilSDK = class {
5
+ constructor(config) {
6
+ if (typeof config === "string") {
7
+ this.apiKey = config;
8
+ this.baseUrl = DEFAULT_BASE_URL;
9
+ this.timeout = DEFAULT_TIMEOUT;
10
+ } else {
11
+ this.apiKey = config.apiKey;
12
+ this.baseUrl = config.baseUrl || DEFAULT_BASE_URL;
13
+ this.timeout = config.timeout || DEFAULT_TIMEOUT;
14
+ }
15
+ if (!this.apiKey) {
16
+ throw new Error("API key is required");
17
+ }
18
+ if (!this.apiKey.startsWith("sk_live_") && !this.apiKey.startsWith("sk_test_")) {
19
+ throw new Error("Invalid API key format. Must start with sk_live_ or sk_test_");
20
+ }
21
+ this.pages = new PagesAPI(this);
22
+ this.documents = new DocumentsAPI(this);
23
+ this.youtube = new YouTubeAPI(this);
24
+ this.chat = new ChatAPI(this);
25
+ this.analyze = new AnalyzeAPI(this);
26
+ }
27
+ async request(endpoint, options = {}) {
28
+ const url = `${this.baseUrl}${endpoint}`;
29
+ const headers = {
30
+ "x-api-key": this.apiKey,
31
+ ...options.headers
32
+ };
33
+ if (options.body && typeof options.body === "string") {
34
+ headers["Content-Type"] = "application/json";
35
+ }
36
+ const controller = new AbortController();
37
+ const timeoutId = setTimeout(() => controller.abort(), this.timeout);
38
+ try {
39
+ const response = await fetch(url, {
40
+ ...options,
41
+ headers,
42
+ signal: controller.signal
43
+ });
44
+ clearTimeout(timeoutId);
45
+ if (!response.ok) {
46
+ const errorData = await response.json().catch(() => ({}));
47
+ const message = errorData.error?.message || `Request failed with status ${response.status}`;
48
+ throw new SybilError(message, response.status, errorData.error?.code);
49
+ }
50
+ return response.json();
51
+ } catch (error) {
52
+ clearTimeout(timeoutId);
53
+ if (error instanceof SybilError) {
54
+ throw error;
55
+ }
56
+ if (error instanceof Error && error.name === "AbortError") {
57
+ throw new SybilError("Request timeout", 408, "TIMEOUT");
58
+ }
59
+ throw new SybilError(
60
+ error instanceof Error ? error.message : "Unknown error",
61
+ 500,
62
+ "UNKNOWN"
63
+ );
64
+ }
65
+ }
66
+ };
67
+ var SybilError = class extends Error {
68
+ constructor(message, status, code) {
69
+ super(message);
70
+ this.name = "SybilError";
71
+ this.status = status;
72
+ this.code = code;
73
+ }
74
+ };
75
+ var PagesAPI = class {
76
+ constructor(client) {
77
+ this.client = client;
78
+ }
79
+ async list(params = {}) {
80
+ const searchParams = new URLSearchParams();
81
+ if (params.limit) searchParams.set("limit", params.limit.toString());
82
+ if (params.offset) searchParams.set("offset", params.offset.toString());
83
+ if (params.parent_id) searchParams.set("parent_id", params.parent_id);
84
+ if (params.is_published !== void 0) searchParams.set("is_published", params.is_published.toString());
85
+ const query = searchParams.toString();
86
+ return this.client.request(`/pages${query ? `?${query}` : ""}`);
87
+ }
88
+ async get(pageId) {
89
+ return this.client.request(`/pages/${pageId}`);
90
+ }
91
+ async create(params = {}) {
92
+ return this.client.request("/pages", {
93
+ method: "POST",
94
+ body: JSON.stringify(params)
95
+ });
96
+ }
97
+ async update(pageId, params) {
98
+ return this.client.request(`/pages/${pageId}`, {
99
+ method: "PUT",
100
+ body: JSON.stringify(params)
101
+ });
102
+ }
103
+ async delete(pageId) {
104
+ return this.client.request(`/pages/${pageId}`, {
105
+ method: "DELETE"
106
+ });
107
+ }
108
+ async upload(file, fileName) {
109
+ const formData = new FormData();
110
+ formData.append("file", file, fileName);
111
+ return this.client.request("/pages/upload", {
112
+ method: "POST",
113
+ body: formData
114
+ });
115
+ }
116
+ async deleteFile(filePath) {
117
+ return this.client.request("/pages/upload", {
118
+ method: "DELETE",
119
+ body: JSON.stringify({ filePath })
120
+ });
121
+ }
122
+ };
123
+ var DocumentsAPI = class {
124
+ constructor(client) {
125
+ this.client = client;
126
+ }
127
+ async list(params = {}) {
128
+ const searchParams = new URLSearchParams();
129
+ if (params.limit) searchParams.set("limit", params.limit.toString());
130
+ if (params.offset) searchParams.set("offset", params.offset.toString());
131
+ if (params.status) searchParams.set("status", params.status);
132
+ if (params.fileType) searchParams.set("fileType", params.fileType);
133
+ if (params.search) searchParams.set("search", params.search);
134
+ const query = searchParams.toString();
135
+ return this.client.request(`/documents${query ? `?${query}` : ""}`);
136
+ }
137
+ async get(documentId, params = {}) {
138
+ const searchParams = new URLSearchParams();
139
+ if (params.includeContent) searchParams.set("includeContent", "true");
140
+ if (params.includeInsights === false) searchParams.set("includeInsights", "false");
141
+ if (params.includeMetadata === false) searchParams.set("includeMetadata", "false");
142
+ const query = searchParams.toString();
143
+ return this.client.request(`/documents/${documentId}${query ? `?${query}` : ""}`);
144
+ }
145
+ async process(file, options = {}) {
146
+ const formData = new FormData();
147
+ formData.append("file", file, options.fileName);
148
+ if (options.metadata) {
149
+ formData.append("metadata", JSON.stringify(options.metadata));
150
+ }
151
+ if (options.collectionId) {
152
+ formData.append("collectionId", options.collectionId);
153
+ }
154
+ return this.client.request("/documents", {
155
+ method: "POST",
156
+ body: formData
157
+ });
158
+ }
159
+ async status(documentId) {
160
+ return this.client.request(`/documents/${documentId}/status`);
161
+ }
162
+ async insights(documentId, type) {
163
+ const query = type ? `?type=${type}` : "";
164
+ return this.client.request(`/documents/${documentId}/insights${query}`);
165
+ }
166
+ async delete(documentId) {
167
+ return this.client.request(`/documents/${documentId}`, {
168
+ method: "DELETE"
169
+ });
170
+ }
171
+ async waitForProcessing(documentId, options = {}) {
172
+ const maxAttempts = options.maxAttempts || 60;
173
+ const interval = options.interval || 3e3;
174
+ for (let i = 0; i < maxAttempts; i++) {
175
+ const status = await this.status(documentId);
176
+ if (status.status === "processed" || status.status === "completed") {
177
+ const { document: document2 } = await this.get(documentId);
178
+ return document2;
179
+ }
180
+ if (status.status === "failed") {
181
+ throw new SybilError(status.error?.message || "Processing failed", 500, "PROCESSING_FAILED");
182
+ }
183
+ await new Promise((resolve) => setTimeout(resolve, interval));
184
+ }
185
+ throw new SybilError("Processing timeout", 408, "TIMEOUT");
186
+ }
187
+ };
188
+ var YouTubeAPI = class {
189
+ constructor(client) {
190
+ this.client = client;
191
+ }
192
+ async list(params = {}) {
193
+ const searchParams = new URLSearchParams();
194
+ if (params.limit) searchParams.set("limit", params.limit.toString());
195
+ if (params.offset) searchParams.set("offset", params.offset.toString());
196
+ const query = searchParams.toString();
197
+ return this.client.request(`/youtube${query ? `?${query}` : ""}`);
198
+ }
199
+ async process(url, options) {
200
+ return this.client.request("/youtube", {
201
+ method: "POST",
202
+ body: JSON.stringify({
203
+ url,
204
+ collectionId: options?.collectionId,
205
+ force: options?.force
206
+ })
207
+ });
208
+ }
209
+ async get(videoId, params = {}) {
210
+ const searchParams = new URLSearchParams();
211
+ if (params.includeTranscript) searchParams.set("includeTranscript", "true");
212
+ if (params.includeChunks) searchParams.set("includeChunks", "true");
213
+ if (params.includeInsights === false) searchParams.set("includeInsights", "false");
214
+ const query = searchParams.toString();
215
+ return this.client.request(`/youtube/${videoId}${query ? `?${query}` : ""}`);
216
+ }
217
+ async delete(videoId) {
218
+ return this.client.request(`/youtube/${videoId}`, {
219
+ method: "DELETE"
220
+ });
221
+ }
222
+ };
223
+ var ChatAPI = class {
224
+ constructor(client) {
225
+ this.client = client;
226
+ }
227
+ async send(paramsOrDocId, query) {
228
+ const params = typeof paramsOrDocId === "string" ? { documentId: paramsOrDocId, query } : paramsOrDocId;
229
+ return this.client.request("/chat", {
230
+ method: "POST",
231
+ body: JSON.stringify(params)
232
+ });
233
+ }
234
+ async history(documentId, limit) {
235
+ const searchParams = new URLSearchParams();
236
+ searchParams.set("documentId", documentId);
237
+ if (limit) searchParams.set("limit", limit.toString());
238
+ return this.client.request(`/chat?${searchParams.toString()}`);
239
+ }
240
+ };
241
+ var AnalyzeAPI = class {
242
+ constructor(client) {
243
+ this.client = client;
244
+ }
245
+ async run(paramsOrDocId, forceRefresh) {
246
+ const params = typeof paramsOrDocId === "string" ? { documentId: paramsOrDocId, forceRefresh } : paramsOrDocId;
247
+ return this.client.request("/analyze", {
248
+ method: "POST",
249
+ body: JSON.stringify(params)
250
+ });
251
+ }
252
+ async get(documentId) {
253
+ return this.client.request(`/analyze?documentId=${documentId}`);
254
+ }
255
+ };
256
+ function embedPage(options) {
257
+ const {
258
+ pageId,
259
+ apiKey,
260
+ container,
261
+ baseUrl = "https://app.sybil.studio",
262
+ theme = "dark",
263
+ readOnly = false,
264
+ toolbar = true,
265
+ minHeight = "400px",
266
+ maxHeight,
267
+ onReady,
268
+ onChange,
269
+ onSave,
270
+ onError
271
+ } = options;
272
+ const containerEl = typeof container === "string" ? document.querySelector(container) : container;
273
+ if (!containerEl) {
274
+ throw new Error("Container element not found");
275
+ }
276
+ const params = new URLSearchParams({
277
+ apiKey,
278
+ readOnly: readOnly.toString(),
279
+ toolbar: toolbar.toString(),
280
+ minHeight
281
+ });
282
+ if (typeof theme === "string") {
283
+ params.set("theme", theme);
284
+ } else {
285
+ if (theme.background) params.set("bg", theme.background);
286
+ if (theme.backgroundSecondary) params.set("bgSecondary", theme.backgroundSecondary);
287
+ if (theme.text) params.set("textColor", theme.text);
288
+ if (theme.textSecondary) params.set("textSecondary", theme.textSecondary);
289
+ if (theme.border) params.set("borderColor", theme.border);
290
+ if (theme.accent) params.set("accent", theme.accent);
291
+ }
292
+ const iframe = document.createElement("iframe");
293
+ iframe.src = `${baseUrl}/embed/pages/${pageId}?${params.toString()}`;
294
+ iframe.style.width = "100%";
295
+ iframe.style.minHeight = minHeight;
296
+ if (maxHeight) iframe.style.maxHeight = maxHeight;
297
+ iframe.style.border = "none";
298
+ iframe.style.borderRadius = "8px";
299
+ iframe.allow = "clipboard-write";
300
+ const handleMessage = (event) => {
301
+ if (event.origin !== new URL(baseUrl).origin) return;
302
+ switch (event.data?.type) {
303
+ case "sybil:page:ready":
304
+ onReady?.();
305
+ break;
306
+ case "sybil:page:changed":
307
+ onChange?.(event.data.blocks);
308
+ break;
309
+ case "sybil:page:saved":
310
+ onSave?.();
311
+ break;
312
+ case "sybil:page:error":
313
+ onError?.(new Error(event.data.error));
314
+ break;
315
+ case "sybil:content":
316
+ break;
317
+ }
318
+ };
319
+ window.addEventListener("message", handleMessage);
320
+ containerEl.appendChild(iframe);
321
+ return {
322
+ getContent: () => {
323
+ return new Promise((resolve) => {
324
+ const handler = (event) => {
325
+ if (event.data?.type === "sybil:content") {
326
+ window.removeEventListener("message", handler);
327
+ resolve(event.data.blocks);
328
+ }
329
+ };
330
+ window.addEventListener("message", handler);
331
+ iframe.contentWindow?.postMessage({ type: "sybil:getContent" }, baseUrl);
332
+ });
333
+ },
334
+ setContent: (blocks) => {
335
+ iframe.contentWindow?.postMessage({ type: "sybil:setContent", blocks }, baseUrl);
336
+ },
337
+ destroy: () => {
338
+ window.removeEventListener("message", handleMessage);
339
+ iframe.remove();
340
+ }
341
+ };
342
+ }
343
+
344
+ export {
345
+ SybilSDK,
346
+ SybilError,
347
+ embedPage
348
+ };