opencloud-platform-sdk 1.1.0 → 1.3.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/dist/index.d.mts +5 -1
- package/dist/index.d.ts +5 -1
- package/dist/index.js +57 -10
- package/dist/index.mjs +57 -10
- package/package.json +1 -1
package/dist/index.d.mts
CHANGED
|
@@ -1,11 +1,13 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
2
|
+
* opencloud-platform-sdk v1.3.0
|
|
3
3
|
* Official SDK for OpenCloud - AI App Marketplace
|
|
4
4
|
*
|
|
5
5
|
* Features:
|
|
6
6
|
* - Automatic preview/production mode detection
|
|
7
7
|
* - Works in bolt.diy/WebContainers preview
|
|
8
|
+
* - Auto-detects API keys from bolt.diy builder
|
|
8
9
|
* - Seamless transition to production with credits
|
|
10
|
+
* - Safe for Vite/Rollup production builds (no side effects at import time)
|
|
9
11
|
*/
|
|
10
12
|
interface OpenCloudConfig {
|
|
11
13
|
appId: string;
|
|
@@ -51,10 +53,12 @@ declare class OpenCloudSDK {
|
|
|
51
53
|
init(options: OpenCloudConfig): void;
|
|
52
54
|
/**
|
|
53
55
|
* Set API key for preview mode
|
|
56
|
+
* Safe to call during build - will be a no-op if not in browser
|
|
54
57
|
*/
|
|
55
58
|
setPreviewApiKey(key: string, provider?: 'openrouter' | 'anthropic' | 'openai'): void;
|
|
56
59
|
/**
|
|
57
60
|
* Get API key for preview mode
|
|
61
|
+
* Checks in order: config, localStorage, bolt.diy cookies
|
|
58
62
|
*/
|
|
59
63
|
private getPreviewApiKey;
|
|
60
64
|
/**
|
package/dist/index.d.ts
CHANGED
|
@@ -1,11 +1,13 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
2
|
+
* opencloud-platform-sdk v1.3.0
|
|
3
3
|
* Official SDK for OpenCloud - AI App Marketplace
|
|
4
4
|
*
|
|
5
5
|
* Features:
|
|
6
6
|
* - Automatic preview/production mode detection
|
|
7
7
|
* - Works in bolt.diy/WebContainers preview
|
|
8
|
+
* - Auto-detects API keys from bolt.diy builder
|
|
8
9
|
* - Seamless transition to production with credits
|
|
10
|
+
* - Safe for Vite/Rollup production builds (no side effects at import time)
|
|
9
11
|
*/
|
|
10
12
|
interface OpenCloudConfig {
|
|
11
13
|
appId: string;
|
|
@@ -51,10 +53,12 @@ declare class OpenCloudSDK {
|
|
|
51
53
|
init(options: OpenCloudConfig): void;
|
|
52
54
|
/**
|
|
53
55
|
* Set API key for preview mode
|
|
56
|
+
* Safe to call during build - will be a no-op if not in browser
|
|
54
57
|
*/
|
|
55
58
|
setPreviewApiKey(key: string, provider?: 'openrouter' | 'anthropic' | 'openai'): void;
|
|
56
59
|
/**
|
|
57
60
|
* Get API key for preview mode
|
|
61
|
+
* Checks in order: config, localStorage, bolt.diy cookies
|
|
58
62
|
*/
|
|
59
63
|
private getPreviewApiKey;
|
|
60
64
|
/**
|
package/dist/index.js
CHANGED
|
@@ -24,21 +24,47 @@ __export(index_exports, {
|
|
|
24
24
|
opencloud: () => opencloud
|
|
25
25
|
});
|
|
26
26
|
module.exports = __toCommonJS(index_exports);
|
|
27
|
+
var isBrowser = typeof window !== "undefined" && typeof document !== "undefined";
|
|
27
28
|
var STORAGE_KEYS = {
|
|
28
29
|
OPENROUTER_KEY: "openrouter_api_key",
|
|
29
30
|
ANTHROPIC_KEY: "anthropic_api_key",
|
|
30
31
|
OPENAI_KEY: "openai_api_key",
|
|
31
32
|
PREVIEW_PROVIDER: "opencloud_preview_provider"
|
|
32
33
|
};
|
|
34
|
+
function getCookie(name) {
|
|
35
|
+
if (!isBrowser) return null;
|
|
36
|
+
const value = `; ${document.cookie}`;
|
|
37
|
+
const parts = value.split(`; ${name}=`);
|
|
38
|
+
if (parts.length === 2) {
|
|
39
|
+
const cookieValue = parts.pop()?.split(";").shift();
|
|
40
|
+
if (cookieValue) {
|
|
41
|
+
try {
|
|
42
|
+
return decodeURIComponent(cookieValue);
|
|
43
|
+
} catch {
|
|
44
|
+
return cookieValue;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
return null;
|
|
49
|
+
}
|
|
50
|
+
function getBoltApiKeys() {
|
|
51
|
+
const apiKeysCookie = getCookie("apiKeys");
|
|
52
|
+
if (!apiKeysCookie) return null;
|
|
53
|
+
try {
|
|
54
|
+
return JSON.parse(apiKeysCookie);
|
|
55
|
+
} catch {
|
|
56
|
+
return null;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
33
59
|
var OpenCloudSDK = class {
|
|
34
60
|
constructor() {
|
|
35
61
|
this._isPreviewMode = false;
|
|
36
62
|
this.config = {
|
|
37
63
|
appId: "",
|
|
38
|
-
apiUrl:
|
|
64
|
+
apiUrl: isBrowser ? window.location.origin : "http://localhost:3000",
|
|
39
65
|
previewApiKey: void 0
|
|
40
66
|
};
|
|
41
|
-
if (
|
|
67
|
+
if (isBrowser) {
|
|
42
68
|
this._isPreviewMode = this.detectPreviewMode();
|
|
43
69
|
}
|
|
44
70
|
}
|
|
@@ -46,7 +72,7 @@ var OpenCloudSDK = class {
|
|
|
46
72
|
* Detect if running in preview/development mode
|
|
47
73
|
*/
|
|
48
74
|
detectPreviewMode() {
|
|
49
|
-
if (
|
|
75
|
+
if (!isBrowser) return false;
|
|
50
76
|
const hostname = window.location.hostname;
|
|
51
77
|
return hostname.includes("webcontainer") || hostname.includes("localhost") || hostname.includes("127.0.0.1") || hostname.includes(".local") || hostname.includes("stackblitz") || hostname.includes("codesandbox");
|
|
52
78
|
}
|
|
@@ -76,9 +102,10 @@ var OpenCloudSDK = class {
|
|
|
76
102
|
}
|
|
77
103
|
/**
|
|
78
104
|
* Set API key for preview mode
|
|
105
|
+
* Safe to call during build - will be a no-op if not in browser
|
|
79
106
|
*/
|
|
80
107
|
setPreviewApiKey(key, provider = "openrouter") {
|
|
81
|
-
if (
|
|
108
|
+
if (!isBrowser) return;
|
|
82
109
|
const storageKey = provider === "openrouter" ? STORAGE_KEYS.OPENROUTER_KEY : provider === "anthropic" ? STORAGE_KEYS.ANTHROPIC_KEY : STORAGE_KEYS.OPENAI_KEY;
|
|
83
110
|
localStorage.setItem(storageKey, key);
|
|
84
111
|
localStorage.setItem(STORAGE_KEYS.PREVIEW_PROVIDER, provider);
|
|
@@ -86,9 +113,10 @@ var OpenCloudSDK = class {
|
|
|
86
113
|
}
|
|
87
114
|
/**
|
|
88
115
|
* Get API key for preview mode
|
|
116
|
+
* Checks in order: config, localStorage, bolt.diy cookies
|
|
89
117
|
*/
|
|
90
118
|
getPreviewApiKey() {
|
|
91
|
-
if (
|
|
119
|
+
if (!isBrowser) return null;
|
|
92
120
|
if (this.config.previewApiKey) {
|
|
93
121
|
return { key: this.config.previewApiKey, provider: "openrouter" };
|
|
94
122
|
}
|
|
@@ -105,6 +133,25 @@ var OpenCloudSDK = class {
|
|
|
105
133
|
return { key: key2, provider: name.replace("_KEY", "").toLowerCase() };
|
|
106
134
|
}
|
|
107
135
|
}
|
|
136
|
+
const boltApiKeys = getBoltApiKeys();
|
|
137
|
+
if (boltApiKeys) {
|
|
138
|
+
const providerMappings = [
|
|
139
|
+
{ boltName: "OpenRouter", provider: "openrouter" },
|
|
140
|
+
{ boltName: "Anthropic", provider: "anthropic" },
|
|
141
|
+
{ boltName: "OpenAI", provider: "openai" },
|
|
142
|
+
// Also check with _API_KEY suffix (older format)
|
|
143
|
+
{ boltName: "OpenRouter_API_KEY", provider: "openrouter" },
|
|
144
|
+
{ boltName: "Anthropic_API_KEY", provider: "anthropic" },
|
|
145
|
+
{ boltName: "OpenAI_API_KEY", provider: "openai" }
|
|
146
|
+
];
|
|
147
|
+
for (const { boltName, provider: provider2 } of providerMappings) {
|
|
148
|
+
const apiKey = boltApiKeys[boltName];
|
|
149
|
+
if (apiKey && apiKey.length > 10 && !apiKey.includes("your_")) {
|
|
150
|
+
console.log(`[OpenCloudSDK] Using API key from bolt.diy (${provider2})`);
|
|
151
|
+
return { key: apiKey, provider: provider2 };
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
}
|
|
108
155
|
return null;
|
|
109
156
|
}
|
|
110
157
|
/**
|
|
@@ -301,7 +348,7 @@ var OpenCloudSDK = class {
|
|
|
301
348
|
* Request user to purchase more tokens
|
|
302
349
|
*/
|
|
303
350
|
async requestTopUp() {
|
|
304
|
-
if (
|
|
351
|
+
if (!isBrowser) return;
|
|
305
352
|
if (this._isPreviewMode) {
|
|
306
353
|
console.warn("[OpenCloudSDK] Top-up not available in preview mode");
|
|
307
354
|
return;
|
|
@@ -319,7 +366,7 @@ var OpenCloudSDK = class {
|
|
|
319
366
|
* Start sending heartbeats
|
|
320
367
|
*/
|
|
321
368
|
startHeartbeat() {
|
|
322
|
-
if (
|
|
369
|
+
if (!isBrowser) return;
|
|
323
370
|
this.sendHeartbeat();
|
|
324
371
|
this.heartbeatInterval = setInterval(() => this.sendHeartbeat(), 3e4);
|
|
325
372
|
}
|
|
@@ -327,7 +374,7 @@ var OpenCloudSDK = class {
|
|
|
327
374
|
* Send heartbeat to platform
|
|
328
375
|
*/
|
|
329
376
|
sendHeartbeat() {
|
|
330
|
-
if (!this.config.appId ||
|
|
377
|
+
if (!this.config.appId || !isBrowser) return;
|
|
331
378
|
const data = JSON.stringify({
|
|
332
379
|
appId: this.config.appId,
|
|
333
380
|
timestamp: Date.now(),
|
|
@@ -342,7 +389,7 @@ var OpenCloudSDK = class {
|
|
|
342
389
|
* Get user session token
|
|
343
390
|
*/
|
|
344
391
|
async getUserSession() {
|
|
345
|
-
if (
|
|
392
|
+
if (!isBrowser) {
|
|
346
393
|
throw new Error("getUserSession can only be called in browser");
|
|
347
394
|
}
|
|
348
395
|
return new Promise((resolve, reject) => {
|
|
@@ -386,7 +433,7 @@ var OpenCloudSDK = class {
|
|
|
386
433
|
}
|
|
387
434
|
};
|
|
388
435
|
var opencloud = new OpenCloudSDK();
|
|
389
|
-
if (
|
|
436
|
+
if (isBrowser) {
|
|
390
437
|
window.OpenCloudSDK = opencloud;
|
|
391
438
|
window.PlatformSDK = opencloud;
|
|
392
439
|
}
|
package/dist/index.mjs
CHANGED
|
@@ -1,19 +1,45 @@
|
|
|
1
1
|
// src/index.ts
|
|
2
|
+
var isBrowser = typeof window !== "undefined" && typeof document !== "undefined";
|
|
2
3
|
var STORAGE_KEYS = {
|
|
3
4
|
OPENROUTER_KEY: "openrouter_api_key",
|
|
4
5
|
ANTHROPIC_KEY: "anthropic_api_key",
|
|
5
6
|
OPENAI_KEY: "openai_api_key",
|
|
6
7
|
PREVIEW_PROVIDER: "opencloud_preview_provider"
|
|
7
8
|
};
|
|
9
|
+
function getCookie(name) {
|
|
10
|
+
if (!isBrowser) return null;
|
|
11
|
+
const value = `; ${document.cookie}`;
|
|
12
|
+
const parts = value.split(`; ${name}=`);
|
|
13
|
+
if (parts.length === 2) {
|
|
14
|
+
const cookieValue = parts.pop()?.split(";").shift();
|
|
15
|
+
if (cookieValue) {
|
|
16
|
+
try {
|
|
17
|
+
return decodeURIComponent(cookieValue);
|
|
18
|
+
} catch {
|
|
19
|
+
return cookieValue;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
return null;
|
|
24
|
+
}
|
|
25
|
+
function getBoltApiKeys() {
|
|
26
|
+
const apiKeysCookie = getCookie("apiKeys");
|
|
27
|
+
if (!apiKeysCookie) return null;
|
|
28
|
+
try {
|
|
29
|
+
return JSON.parse(apiKeysCookie);
|
|
30
|
+
} catch {
|
|
31
|
+
return null;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
8
34
|
var OpenCloudSDK = class {
|
|
9
35
|
constructor() {
|
|
10
36
|
this._isPreviewMode = false;
|
|
11
37
|
this.config = {
|
|
12
38
|
appId: "",
|
|
13
|
-
apiUrl:
|
|
39
|
+
apiUrl: isBrowser ? window.location.origin : "http://localhost:3000",
|
|
14
40
|
previewApiKey: void 0
|
|
15
41
|
};
|
|
16
|
-
if (
|
|
42
|
+
if (isBrowser) {
|
|
17
43
|
this._isPreviewMode = this.detectPreviewMode();
|
|
18
44
|
}
|
|
19
45
|
}
|
|
@@ -21,7 +47,7 @@ var OpenCloudSDK = class {
|
|
|
21
47
|
* Detect if running in preview/development mode
|
|
22
48
|
*/
|
|
23
49
|
detectPreviewMode() {
|
|
24
|
-
if (
|
|
50
|
+
if (!isBrowser) return false;
|
|
25
51
|
const hostname = window.location.hostname;
|
|
26
52
|
return hostname.includes("webcontainer") || hostname.includes("localhost") || hostname.includes("127.0.0.1") || hostname.includes(".local") || hostname.includes("stackblitz") || hostname.includes("codesandbox");
|
|
27
53
|
}
|
|
@@ -51,9 +77,10 @@ var OpenCloudSDK = class {
|
|
|
51
77
|
}
|
|
52
78
|
/**
|
|
53
79
|
* Set API key for preview mode
|
|
80
|
+
* Safe to call during build - will be a no-op if not in browser
|
|
54
81
|
*/
|
|
55
82
|
setPreviewApiKey(key, provider = "openrouter") {
|
|
56
|
-
if (
|
|
83
|
+
if (!isBrowser) return;
|
|
57
84
|
const storageKey = provider === "openrouter" ? STORAGE_KEYS.OPENROUTER_KEY : provider === "anthropic" ? STORAGE_KEYS.ANTHROPIC_KEY : STORAGE_KEYS.OPENAI_KEY;
|
|
58
85
|
localStorage.setItem(storageKey, key);
|
|
59
86
|
localStorage.setItem(STORAGE_KEYS.PREVIEW_PROVIDER, provider);
|
|
@@ -61,9 +88,10 @@ var OpenCloudSDK = class {
|
|
|
61
88
|
}
|
|
62
89
|
/**
|
|
63
90
|
* Get API key for preview mode
|
|
91
|
+
* Checks in order: config, localStorage, bolt.diy cookies
|
|
64
92
|
*/
|
|
65
93
|
getPreviewApiKey() {
|
|
66
|
-
if (
|
|
94
|
+
if (!isBrowser) return null;
|
|
67
95
|
if (this.config.previewApiKey) {
|
|
68
96
|
return { key: this.config.previewApiKey, provider: "openrouter" };
|
|
69
97
|
}
|
|
@@ -80,6 +108,25 @@ var OpenCloudSDK = class {
|
|
|
80
108
|
return { key: key2, provider: name.replace("_KEY", "").toLowerCase() };
|
|
81
109
|
}
|
|
82
110
|
}
|
|
111
|
+
const boltApiKeys = getBoltApiKeys();
|
|
112
|
+
if (boltApiKeys) {
|
|
113
|
+
const providerMappings = [
|
|
114
|
+
{ boltName: "OpenRouter", provider: "openrouter" },
|
|
115
|
+
{ boltName: "Anthropic", provider: "anthropic" },
|
|
116
|
+
{ boltName: "OpenAI", provider: "openai" },
|
|
117
|
+
// Also check with _API_KEY suffix (older format)
|
|
118
|
+
{ boltName: "OpenRouter_API_KEY", provider: "openrouter" },
|
|
119
|
+
{ boltName: "Anthropic_API_KEY", provider: "anthropic" },
|
|
120
|
+
{ boltName: "OpenAI_API_KEY", provider: "openai" }
|
|
121
|
+
];
|
|
122
|
+
for (const { boltName, provider: provider2 } of providerMappings) {
|
|
123
|
+
const apiKey = boltApiKeys[boltName];
|
|
124
|
+
if (apiKey && apiKey.length > 10 && !apiKey.includes("your_")) {
|
|
125
|
+
console.log(`[OpenCloudSDK] Using API key from bolt.diy (${provider2})`);
|
|
126
|
+
return { key: apiKey, provider: provider2 };
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
}
|
|
83
130
|
return null;
|
|
84
131
|
}
|
|
85
132
|
/**
|
|
@@ -276,7 +323,7 @@ var OpenCloudSDK = class {
|
|
|
276
323
|
* Request user to purchase more tokens
|
|
277
324
|
*/
|
|
278
325
|
async requestTopUp() {
|
|
279
|
-
if (
|
|
326
|
+
if (!isBrowser) return;
|
|
280
327
|
if (this._isPreviewMode) {
|
|
281
328
|
console.warn("[OpenCloudSDK] Top-up not available in preview mode");
|
|
282
329
|
return;
|
|
@@ -294,7 +341,7 @@ var OpenCloudSDK = class {
|
|
|
294
341
|
* Start sending heartbeats
|
|
295
342
|
*/
|
|
296
343
|
startHeartbeat() {
|
|
297
|
-
if (
|
|
344
|
+
if (!isBrowser) return;
|
|
298
345
|
this.sendHeartbeat();
|
|
299
346
|
this.heartbeatInterval = setInterval(() => this.sendHeartbeat(), 3e4);
|
|
300
347
|
}
|
|
@@ -302,7 +349,7 @@ var OpenCloudSDK = class {
|
|
|
302
349
|
* Send heartbeat to platform
|
|
303
350
|
*/
|
|
304
351
|
sendHeartbeat() {
|
|
305
|
-
if (!this.config.appId ||
|
|
352
|
+
if (!this.config.appId || !isBrowser) return;
|
|
306
353
|
const data = JSON.stringify({
|
|
307
354
|
appId: this.config.appId,
|
|
308
355
|
timestamp: Date.now(),
|
|
@@ -317,7 +364,7 @@ var OpenCloudSDK = class {
|
|
|
317
364
|
* Get user session token
|
|
318
365
|
*/
|
|
319
366
|
async getUserSession() {
|
|
320
|
-
if (
|
|
367
|
+
if (!isBrowser) {
|
|
321
368
|
throw new Error("getUserSession can only be called in browser");
|
|
322
369
|
}
|
|
323
370
|
return new Promise((resolve, reject) => {
|
|
@@ -361,7 +408,7 @@ var OpenCloudSDK = class {
|
|
|
361
408
|
}
|
|
362
409
|
};
|
|
363
410
|
var opencloud = new OpenCloudSDK();
|
|
364
|
-
if (
|
|
411
|
+
if (isBrowser) {
|
|
365
412
|
window.OpenCloudSDK = opencloud;
|
|
366
413
|
window.PlatformSDK = opencloud;
|
|
367
414
|
}
|