@blinkdotnew/dev-sdk 2.2.0 → 2.2.3
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 +291 -9
- package/dist/index.d.ts +291 -9
- package/dist/index.js +442 -139
- package/dist/index.mjs +440 -140
- package/package.json +5 -3
package/dist/index.js
CHANGED
|
@@ -7,6 +7,33 @@ var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require
|
|
|
7
7
|
throw Error('Dynamic require of "' + x + '" is not supported');
|
|
8
8
|
});
|
|
9
9
|
|
|
10
|
+
// ../core/src/platform.ts
|
|
11
|
+
function detectPlatform() {
|
|
12
|
+
if (typeof Deno !== "undefined") {
|
|
13
|
+
return "deno";
|
|
14
|
+
}
|
|
15
|
+
if (typeof process !== "undefined" && process.versions?.node) {
|
|
16
|
+
if (typeof navigator !== "undefined" && navigator.product === "ReactNative") {
|
|
17
|
+
return "react-native";
|
|
18
|
+
}
|
|
19
|
+
return "node";
|
|
20
|
+
}
|
|
21
|
+
if (typeof navigator !== "undefined" && navigator.product === "ReactNative") {
|
|
22
|
+
return "react-native";
|
|
23
|
+
}
|
|
24
|
+
if (typeof window !== "undefined" && typeof document !== "undefined") {
|
|
25
|
+
return "web";
|
|
26
|
+
}
|
|
27
|
+
return "node";
|
|
28
|
+
}
|
|
29
|
+
var platform = detectPlatform();
|
|
30
|
+
var isWeb = platform === "web";
|
|
31
|
+
var isReactNative = platform === "react-native";
|
|
32
|
+
var isNode = platform === "node";
|
|
33
|
+
var isDeno = platform === "deno";
|
|
34
|
+
var isBrowser = isWeb || isReactNative;
|
|
35
|
+
var isServer = isNode || isDeno;
|
|
36
|
+
|
|
10
37
|
// ../core/src/storage-adapter.ts
|
|
11
38
|
var WebStorageAdapter = class {
|
|
12
39
|
getItem(key) {
|
|
@@ -92,6 +119,9 @@ var NoOpStorageAdapter = class {
|
|
|
92
119
|
}
|
|
93
120
|
};
|
|
94
121
|
function getDefaultStorageAdapter() {
|
|
122
|
+
if (isDeno) {
|
|
123
|
+
return new NoOpStorageAdapter();
|
|
124
|
+
}
|
|
95
125
|
if (typeof window !== "undefined" && typeof localStorage !== "undefined") {
|
|
96
126
|
try {
|
|
97
127
|
localStorage.setItem("__test__", "test");
|
|
@@ -103,28 +133,6 @@ function getDefaultStorageAdapter() {
|
|
|
103
133
|
return new NoOpStorageAdapter();
|
|
104
134
|
}
|
|
105
135
|
|
|
106
|
-
// ../core/src/platform.ts
|
|
107
|
-
function detectPlatform() {
|
|
108
|
-
if (typeof process !== "undefined" && process.versions?.node) {
|
|
109
|
-
if (typeof navigator !== "undefined" && navigator.product === "ReactNative") {
|
|
110
|
-
return "react-native";
|
|
111
|
-
}
|
|
112
|
-
return "node";
|
|
113
|
-
}
|
|
114
|
-
if (typeof navigator !== "undefined" && navigator.product === "ReactNative") {
|
|
115
|
-
return "react-native";
|
|
116
|
-
}
|
|
117
|
-
if (typeof window !== "undefined" && typeof document !== "undefined") {
|
|
118
|
-
return "web";
|
|
119
|
-
}
|
|
120
|
-
return "node";
|
|
121
|
-
}
|
|
122
|
-
var platform = detectPlatform();
|
|
123
|
-
var isWeb = platform === "web";
|
|
124
|
-
var isReactNative = platform === "react-native";
|
|
125
|
-
var isNode = platform === "node";
|
|
126
|
-
var isBrowser = isWeb || isReactNative;
|
|
127
|
-
|
|
128
136
|
// ../core/src/types.ts
|
|
129
137
|
var BlinkError = class extends Error {
|
|
130
138
|
constructor(message, code, status, details) {
|
|
@@ -388,17 +396,44 @@ function convertKeysToCamelCase(obj) {
|
|
|
388
396
|
return converted;
|
|
389
397
|
}
|
|
390
398
|
var HttpClient = class {
|
|
391
|
-
authUrl;
|
|
392
|
-
coreUrl;
|
|
399
|
+
authUrl = "https://blink.new";
|
|
400
|
+
coreUrl = "https://core.blink.new";
|
|
393
401
|
projectId;
|
|
402
|
+
publishableKey;
|
|
403
|
+
secretKey;
|
|
404
|
+
// Permanent, non-expiring key (like Stripe's sk_live_...)
|
|
394
405
|
getToken;
|
|
395
406
|
getValidToken;
|
|
396
407
|
constructor(config, getToken, getValidToken) {
|
|
397
408
|
this.projectId = config.projectId;
|
|
409
|
+
this.publishableKey = config.publishableKey;
|
|
410
|
+
this.secretKey = config.secretKey || config.serviceToken;
|
|
398
411
|
this.getToken = getToken;
|
|
399
412
|
this.getValidToken = getValidToken;
|
|
400
|
-
|
|
401
|
-
|
|
413
|
+
}
|
|
414
|
+
shouldAttachPublishableKey(path, method) {
|
|
415
|
+
if (method !== "GET" && method !== "POST") return false;
|
|
416
|
+
if (path.includes("/api/analytics/")) return true;
|
|
417
|
+
if (path.includes("/api/storage/")) return true;
|
|
418
|
+
if (path.includes("/api/db/") && path.includes("/rest/v1/")) return method === "GET";
|
|
419
|
+
return false;
|
|
420
|
+
}
|
|
421
|
+
shouldSkipSecretKey(url) {
|
|
422
|
+
try {
|
|
423
|
+
const parsed = new URL(url);
|
|
424
|
+
return parsed.hostname.endsWith(".functions.blink.new");
|
|
425
|
+
} catch {
|
|
426
|
+
return false;
|
|
427
|
+
}
|
|
428
|
+
}
|
|
429
|
+
getAuthorizationHeader(url, token) {
|
|
430
|
+
if (this.secretKey && !this.shouldSkipSecretKey(url)) {
|
|
431
|
+
return `Bearer ${this.secretKey}`;
|
|
432
|
+
}
|
|
433
|
+
if (token) {
|
|
434
|
+
return `Bearer ${token}`;
|
|
435
|
+
}
|
|
436
|
+
return null;
|
|
402
437
|
}
|
|
403
438
|
/**
|
|
404
439
|
* Make an authenticated request to the Blink API
|
|
@@ -406,19 +441,23 @@ var HttpClient = class {
|
|
|
406
441
|
async request(path, options = {}) {
|
|
407
442
|
const url = this.buildUrl(path, options.searchParams);
|
|
408
443
|
const token = this.getValidToken ? await this.getValidToken() : this.getToken();
|
|
444
|
+
const method = options.method || "GET";
|
|
409
445
|
const headers = {
|
|
410
446
|
"Content-Type": "application/json",
|
|
411
447
|
...options.headers
|
|
412
448
|
};
|
|
413
|
-
|
|
414
|
-
|
|
449
|
+
const auth = this.getAuthorizationHeader(url, token);
|
|
450
|
+
if (auth) {
|
|
451
|
+
headers.Authorization = auth;
|
|
452
|
+
} else if (this.publishableKey && !headers["x-blink-publishable-key"] && this.shouldAttachPublishableKey(path, method)) {
|
|
453
|
+
headers["x-blink-publishable-key"] = this.publishableKey;
|
|
415
454
|
}
|
|
416
455
|
const requestInit = {
|
|
417
|
-
method
|
|
456
|
+
method,
|
|
418
457
|
headers,
|
|
419
458
|
signal: options.signal
|
|
420
459
|
};
|
|
421
|
-
if (options.body &&
|
|
460
|
+
if (options.body && method !== "GET") {
|
|
422
461
|
requestInit.body = typeof options.body === "string" ? options.body : JSON.stringify(options.body);
|
|
423
462
|
}
|
|
424
463
|
try {
|
|
@@ -572,12 +611,12 @@ var HttpClient = class {
|
|
|
572
611
|
throw new BlinkValidationError("Unsupported file type");
|
|
573
612
|
}
|
|
574
613
|
formData.append("path", filePath);
|
|
575
|
-
if (options.upsert !== void 0) {
|
|
576
|
-
formData.append("options", JSON.stringify({ upsert: options.upsert }));
|
|
577
|
-
}
|
|
578
614
|
const headers = {};
|
|
579
|
-
|
|
580
|
-
|
|
615
|
+
const auth = this.getAuthorizationHeader(url, token);
|
|
616
|
+
if (auth) {
|
|
617
|
+
headers.Authorization = auth;
|
|
618
|
+
} else if (this.publishableKey && path.includes("/api/storage/") && !headers["x-blink-publishable-key"]) {
|
|
619
|
+
headers["x-blink-publishable-key"] = this.publishableKey;
|
|
581
620
|
}
|
|
582
621
|
try {
|
|
583
622
|
if (typeof XMLHttpRequest !== "undefined" && options.onProgress) {
|
|
@@ -678,7 +717,7 @@ var HttpClient = class {
|
|
|
678
717
|
});
|
|
679
718
|
}
|
|
680
719
|
/**
|
|
681
|
-
* Stream AI text generation
|
|
720
|
+
* Stream AI text generation - uses Vercel AI SDK's pipeUIMessageStreamToResponse (Data Stream Protocol)
|
|
682
721
|
*/
|
|
683
722
|
async streamAiText(prompt, options = {}, onChunk) {
|
|
684
723
|
const url = this.buildUrl(`/api/ai/${this.projectId}/text`);
|
|
@@ -686,9 +725,8 @@ var HttpClient = class {
|
|
|
686
725
|
const headers = {
|
|
687
726
|
"Content-Type": "application/json"
|
|
688
727
|
};
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
}
|
|
728
|
+
const auth = this.getAuthorizationHeader(url, token);
|
|
729
|
+
if (auth) headers.Authorization = auth;
|
|
692
730
|
const body = {
|
|
693
731
|
prompt,
|
|
694
732
|
stream: true,
|
|
@@ -708,7 +746,7 @@ var HttpClient = class {
|
|
|
708
746
|
if (!response.body) {
|
|
709
747
|
throw new BlinkNetworkError("No response body for streaming");
|
|
710
748
|
}
|
|
711
|
-
return this.
|
|
749
|
+
return this.parseDataStreamProtocol(response.body, onChunk);
|
|
712
750
|
} catch (error) {
|
|
713
751
|
if (error instanceof BlinkError) {
|
|
714
752
|
throw error;
|
|
@@ -733,7 +771,7 @@ var HttpClient = class {
|
|
|
733
771
|
});
|
|
734
772
|
}
|
|
735
773
|
/**
|
|
736
|
-
* Stream AI object generation
|
|
774
|
+
* Stream AI object generation - uses Vercel AI SDK's pipeTextStreamToResponse
|
|
737
775
|
*/
|
|
738
776
|
async streamAiObject(prompt, options = {}, onPartial) {
|
|
739
777
|
const url = this.buildUrl(`/api/ai/${this.projectId}/object`);
|
|
@@ -741,9 +779,8 @@ var HttpClient = class {
|
|
|
741
779
|
const headers = {
|
|
742
780
|
"Content-Type": "application/json"
|
|
743
781
|
};
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
}
|
|
782
|
+
const auth = this.getAuthorizationHeader(url, token);
|
|
783
|
+
if (auth) headers.Authorization = auth;
|
|
747
784
|
const body = {
|
|
748
785
|
prompt,
|
|
749
786
|
stream: true,
|
|
@@ -763,7 +800,35 @@ var HttpClient = class {
|
|
|
763
800
|
if (!response.body) {
|
|
764
801
|
throw new BlinkNetworkError("No response body for streaming");
|
|
765
802
|
}
|
|
766
|
-
|
|
803
|
+
const reader = response.body.getReader();
|
|
804
|
+
const decoder = new TextDecoder();
|
|
805
|
+
let buffer = "";
|
|
806
|
+
let latestObject = {};
|
|
807
|
+
try {
|
|
808
|
+
while (true) {
|
|
809
|
+
const { done, value } = await reader.read();
|
|
810
|
+
if (done) break;
|
|
811
|
+
const chunk = decoder.decode(value, { stream: true });
|
|
812
|
+
buffer += chunk;
|
|
813
|
+
try {
|
|
814
|
+
const parsed = JSON.parse(buffer);
|
|
815
|
+
latestObject = parsed;
|
|
816
|
+
if (onPartial) {
|
|
817
|
+
onPartial(parsed);
|
|
818
|
+
}
|
|
819
|
+
} catch {
|
|
820
|
+
}
|
|
821
|
+
}
|
|
822
|
+
if (buffer) {
|
|
823
|
+
try {
|
|
824
|
+
latestObject = JSON.parse(buffer);
|
|
825
|
+
} catch {
|
|
826
|
+
}
|
|
827
|
+
}
|
|
828
|
+
return { object: latestObject };
|
|
829
|
+
} finally {
|
|
830
|
+
reader.releaseLock();
|
|
831
|
+
}
|
|
767
832
|
} catch (error) {
|
|
768
833
|
if (error instanceof BlinkError) {
|
|
769
834
|
throw error;
|
|
@@ -820,6 +885,17 @@ var HttpClient = class {
|
|
|
820
885
|
signal
|
|
821
886
|
});
|
|
822
887
|
}
|
|
888
|
+
async aiVideo(prompt, options = {}) {
|
|
889
|
+
const { signal, ...body } = options;
|
|
890
|
+
return this.request(`/api/ai/${this.projectId}/video`, {
|
|
891
|
+
method: "POST",
|
|
892
|
+
body: {
|
|
893
|
+
prompt,
|
|
894
|
+
...body
|
|
895
|
+
},
|
|
896
|
+
signal
|
|
897
|
+
});
|
|
898
|
+
}
|
|
823
899
|
/**
|
|
824
900
|
* Data-specific requests
|
|
825
901
|
*/
|
|
@@ -861,6 +937,38 @@ var HttpClient = class {
|
|
|
861
937
|
async dataSearch(projectId, request) {
|
|
862
938
|
return this.post(`/api/data/${projectId}/search`, request);
|
|
863
939
|
}
|
|
940
|
+
/**
|
|
941
|
+
* Connector requests
|
|
942
|
+
*/
|
|
943
|
+
formatProviderForPath(provider) {
|
|
944
|
+
return provider.replace("_", "-");
|
|
945
|
+
}
|
|
946
|
+
async connectorStatus(provider) {
|
|
947
|
+
return this.request(`/api/connectors/${this.formatProviderForPath(provider)}/${this.projectId}/status`, {
|
|
948
|
+
method: "GET"
|
|
949
|
+
});
|
|
950
|
+
}
|
|
951
|
+
async connectorExecute(provider, request) {
|
|
952
|
+
const path = request.method.startsWith("/") ? request.method : `/${request.method}`;
|
|
953
|
+
const url = `/api/connectors/${this.formatProviderForPath(provider)}/${this.projectId}${path}`;
|
|
954
|
+
const method = (request.http_method || "GET").toUpperCase();
|
|
955
|
+
if (method === "GET") {
|
|
956
|
+
return this.request(url, {
|
|
957
|
+
method: "GET",
|
|
958
|
+
searchParams: request.params
|
|
959
|
+
});
|
|
960
|
+
}
|
|
961
|
+
return this.request(url, {
|
|
962
|
+
method,
|
|
963
|
+
body: request.params || {}
|
|
964
|
+
});
|
|
965
|
+
}
|
|
966
|
+
async connectorSaveApiKey(provider, request) {
|
|
967
|
+
return this.request(`/api/connectors/${this.formatProviderForPath(provider)}/${this.projectId}/api-key`, {
|
|
968
|
+
method: "POST",
|
|
969
|
+
body: request
|
|
970
|
+
});
|
|
971
|
+
}
|
|
864
972
|
/**
|
|
865
973
|
* Realtime-specific requests
|
|
866
974
|
*/
|
|
@@ -926,93 +1034,94 @@ var HttpClient = class {
|
|
|
926
1034
|
}
|
|
927
1035
|
}
|
|
928
1036
|
/**
|
|
929
|
-
* Parse Vercel AI SDK
|
|
930
|
-
*
|
|
1037
|
+
* Parse Vercel AI SDK v5 Data Stream Protocol (Server-Sent Events)
|
|
1038
|
+
* Supports all event types from the UI Message Stream protocol
|
|
931
1039
|
*/
|
|
932
|
-
async
|
|
1040
|
+
async parseDataStreamProtocol(body, onChunk) {
|
|
933
1041
|
const reader = body.getReader();
|
|
934
1042
|
const decoder = new TextDecoder();
|
|
1043
|
+
const finalResult = {
|
|
1044
|
+
text: "",
|
|
1045
|
+
toolCalls: [],
|
|
1046
|
+
toolResults: [],
|
|
1047
|
+
sources: [],
|
|
1048
|
+
files: [],
|
|
1049
|
+
reasoning: []
|
|
1050
|
+
};
|
|
935
1051
|
let buffer = "";
|
|
936
|
-
let finalResult = {};
|
|
937
1052
|
try {
|
|
938
1053
|
while (true) {
|
|
939
1054
|
const { done, value } = await reader.read();
|
|
940
1055
|
if (done) break;
|
|
941
1056
|
buffer += decoder.decode(value, { stream: true });
|
|
942
|
-
const lines = buffer.split(
|
|
1057
|
+
const lines = buffer.split("\n");
|
|
943
1058
|
buffer = lines.pop() || "";
|
|
944
1059
|
for (const line of lines) {
|
|
945
1060
|
if (!line.trim()) continue;
|
|
1061
|
+
if (line === "[DONE]") {
|
|
1062
|
+
continue;
|
|
1063
|
+
}
|
|
1064
|
+
if (!line.startsWith("data: ")) continue;
|
|
946
1065
|
try {
|
|
947
|
-
|
|
948
|
-
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
} else if (line.startsWith("2:")) {
|
|
957
|
-
const data = JSON.parse(line.slice(2));
|
|
958
|
-
if (Array.isArray(data) && data.length > 0) {
|
|
959
|
-
const item = data[0];
|
|
960
|
-
if (typeof item === "string") {
|
|
961
|
-
finalResult.status = item;
|
|
962
|
-
} else if (typeof item === "object") {
|
|
963
|
-
if (onPartial) {
|
|
964
|
-
onPartial(item);
|
|
965
|
-
}
|
|
966
|
-
finalResult.object = item;
|
|
1066
|
+
const jsonStr = line.slice(6);
|
|
1067
|
+
const part = JSON.parse(jsonStr);
|
|
1068
|
+
switch (part.type) {
|
|
1069
|
+
case "text-start":
|
|
1070
|
+
break;
|
|
1071
|
+
case "text-delta":
|
|
1072
|
+
if (part.delta) {
|
|
1073
|
+
finalResult.text += part.delta;
|
|
1074
|
+
if (onChunk) onChunk(part.delta);
|
|
967
1075
|
}
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
if (metadata.usage) {
|
|
972
|
-
finalResult.usage = metadata.usage;
|
|
973
|
-
}
|
|
974
|
-
if (metadata.finishReason) {
|
|
975
|
-
finalResult.finishReason = metadata.finishReason;
|
|
976
|
-
}
|
|
977
|
-
} else if (line.startsWith("e:")) {
|
|
978
|
-
const errorData = JSON.parse(line.slice(2));
|
|
979
|
-
finalResult.error = errorData;
|
|
980
|
-
}
|
|
981
|
-
} catch (error) {
|
|
982
|
-
console.warn("Failed to parse stream line:", line, error);
|
|
983
|
-
}
|
|
984
|
-
}
|
|
985
|
-
}
|
|
986
|
-
if (buffer.trim()) {
|
|
987
|
-
try {
|
|
988
|
-
if (buffer.startsWith("0:")) {
|
|
989
|
-
const textChunk = JSON.parse(buffer.slice(2));
|
|
990
|
-
if (onChunk) {
|
|
991
|
-
onChunk(textChunk);
|
|
992
|
-
}
|
|
993
|
-
finalResult.text = (finalResult.text || "") + textChunk;
|
|
994
|
-
} else if (buffer.startsWith("2:")) {
|
|
995
|
-
const data = JSON.parse(buffer.slice(2));
|
|
996
|
-
if (Array.isArray(data) && data.length > 0) {
|
|
997
|
-
const item = data[0];
|
|
998
|
-
if (typeof item === "object") {
|
|
999
|
-
if (onPartial) {
|
|
1000
|
-
onPartial(item);
|
|
1076
|
+
if (part.textDelta) {
|
|
1077
|
+
finalResult.text += part.textDelta;
|
|
1078
|
+
if (onChunk) onChunk(part.textDelta);
|
|
1001
1079
|
}
|
|
1002
|
-
|
|
1003
|
-
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
-
|
|
1008
|
-
|
|
1009
|
-
|
|
1010
|
-
|
|
1011
|
-
|
|
1080
|
+
break;
|
|
1081
|
+
case "text-end":
|
|
1082
|
+
break;
|
|
1083
|
+
case "tool-call":
|
|
1084
|
+
finalResult.toolCalls.push({
|
|
1085
|
+
toolCallId: part.toolCallId,
|
|
1086
|
+
toolName: part.toolName,
|
|
1087
|
+
args: part.args
|
|
1088
|
+
});
|
|
1089
|
+
break;
|
|
1090
|
+
case "tool-result":
|
|
1091
|
+
finalResult.toolResults.push({
|
|
1092
|
+
toolCallId: part.toolCallId,
|
|
1093
|
+
toolName: part.toolName,
|
|
1094
|
+
result: part.result
|
|
1095
|
+
});
|
|
1096
|
+
break;
|
|
1097
|
+
case "source-url":
|
|
1098
|
+
finalResult.sources.push({
|
|
1099
|
+
id: part.id,
|
|
1100
|
+
url: part.url,
|
|
1101
|
+
title: part.title
|
|
1102
|
+
});
|
|
1103
|
+
break;
|
|
1104
|
+
case "file":
|
|
1105
|
+
finalResult.files.push(part.file);
|
|
1106
|
+
break;
|
|
1107
|
+
case "reasoning":
|
|
1108
|
+
finalResult.reasoning.push(part.content);
|
|
1109
|
+
break;
|
|
1110
|
+
case "finish":
|
|
1111
|
+
finalResult.finishReason = part.finishReason;
|
|
1112
|
+
finalResult.usage = part.usage;
|
|
1113
|
+
if (part.response) finalResult.response = part.response;
|
|
1114
|
+
break;
|
|
1115
|
+
case "error":
|
|
1116
|
+
finalResult.error = part.error;
|
|
1117
|
+
throw new Error(part.error);
|
|
1118
|
+
case "data":
|
|
1119
|
+
if (!finalResult.customData) finalResult.customData = [];
|
|
1120
|
+
finalResult.customData.push(part.value);
|
|
1121
|
+
break;
|
|
1012
1122
|
}
|
|
1123
|
+
} catch (e) {
|
|
1013
1124
|
}
|
|
1014
|
-
} catch (error) {
|
|
1015
|
-
console.warn("Failed to parse final buffer:", buffer, error);
|
|
1016
1125
|
}
|
|
1017
1126
|
}
|
|
1018
1127
|
return finalResult;
|
|
@@ -1179,13 +1288,13 @@ var BlinkAuth = class {
|
|
|
1179
1288
|
this.authConfig = {
|
|
1180
1289
|
mode: "managed",
|
|
1181
1290
|
// Default mode
|
|
1182
|
-
authUrl: "https://
|
|
1291
|
+
authUrl: "https://blink.new",
|
|
1183
1292
|
coreUrl: "https://core.blink.new",
|
|
1184
1293
|
detectSessionInUrl: true,
|
|
1185
1294
|
// Default to true for web compatibility
|
|
1186
1295
|
...config.auth
|
|
1187
1296
|
};
|
|
1188
|
-
this.authUrl = "https://
|
|
1297
|
+
this.authUrl = this.authConfig.authUrl || "https://blink.new";
|
|
1189
1298
|
this.coreUrl = this.authConfig.coreUrl || "https://core.blink.new";
|
|
1190
1299
|
const hostname = getLocationHostname();
|
|
1191
1300
|
if (hostname && this.authUrl === "https://blink.new" && (hostname === "localhost" || hostname === "127.0.0.1")) {
|
|
@@ -2939,6 +3048,11 @@ var BlinkAuth = class {
|
|
|
2939
3048
|
};
|
|
2940
3049
|
|
|
2941
3050
|
// src/database.ts
|
|
3051
|
+
function assertServerOnly(methodName) {
|
|
3052
|
+
if (typeof window !== "undefined") {
|
|
3053
|
+
throw new Error(`${methodName} is server-only. Use Blink CRUD methods (blink.db.<table>.*) instead.`);
|
|
3054
|
+
}
|
|
3055
|
+
}
|
|
2942
3056
|
function camelToSnake3(str) {
|
|
2943
3057
|
return str.replace(/[A-Z]/g, (letter) => `_${letter.toLowerCase()}`);
|
|
2944
3058
|
}
|
|
@@ -3157,6 +3271,7 @@ var BlinkTable = class {
|
|
|
3157
3271
|
* Raw SQL query on this table (for advanced use cases)
|
|
3158
3272
|
*/
|
|
3159
3273
|
async sql(query, params) {
|
|
3274
|
+
assertServerOnly("blink.db.<table>.sql");
|
|
3160
3275
|
const response = await this.httpClient.dbSql(query, params);
|
|
3161
3276
|
return response.data;
|
|
3162
3277
|
}
|
|
@@ -3205,6 +3320,7 @@ var BlinkDatabase = class {
|
|
|
3205
3320
|
* Execute raw SQL query
|
|
3206
3321
|
*/
|
|
3207
3322
|
async sql(query, params) {
|
|
3323
|
+
assertServerOnly("blink.db.sql");
|
|
3208
3324
|
const response = await this.httpClient.dbSql(query, params);
|
|
3209
3325
|
return response.data;
|
|
3210
3326
|
}
|
|
@@ -3212,6 +3328,7 @@ var BlinkDatabase = class {
|
|
|
3212
3328
|
* Execute batch SQL operations
|
|
3213
3329
|
*/
|
|
3214
3330
|
async batch(statements, mode = "write") {
|
|
3331
|
+
assertServerOnly("blink.db.batch");
|
|
3215
3332
|
const response = await this.httpClient.dbBatch(statements, mode);
|
|
3216
3333
|
return response.data;
|
|
3217
3334
|
}
|
|
@@ -3274,7 +3391,6 @@ var BlinkStorageImpl = class {
|
|
|
3274
3391
|
correctedPath,
|
|
3275
3392
|
// Use corrected path with proper extension
|
|
3276
3393
|
{
|
|
3277
|
-
upsert: options.upsert,
|
|
3278
3394
|
onProgress: options.onProgress,
|
|
3279
3395
|
contentType: detectedContentType
|
|
3280
3396
|
// Pass detected content type
|
|
@@ -3294,7 +3410,7 @@ var BlinkStorageImpl = class {
|
|
|
3294
3410
|
if (error instanceof Error && "status" in error) {
|
|
3295
3411
|
const status = error.status;
|
|
3296
3412
|
if (status === 409) {
|
|
3297
|
-
throw new BlinkStorageError("File already exists.
|
|
3413
|
+
throw new BlinkStorageError("File already exists.", 409);
|
|
3298
3414
|
}
|
|
3299
3415
|
if (status === 400) {
|
|
3300
3416
|
throw new BlinkStorageError("Invalid request parameters", 400);
|
|
@@ -3339,7 +3455,6 @@ var BlinkStorageImpl = class {
|
|
|
3339
3455
|
detectedContentType
|
|
3340
3456
|
};
|
|
3341
3457
|
} catch (error) {
|
|
3342
|
-
console.warn("File type detection failed, using original path:", error);
|
|
3343
3458
|
return {
|
|
3344
3459
|
correctedPath: originalPath,
|
|
3345
3460
|
detectedContentType: "application/octet-stream"
|
|
@@ -3729,13 +3844,7 @@ var BlinkAIImpl = class {
|
|
|
3729
3844
|
options.prompt || "",
|
|
3730
3845
|
requestBody
|
|
3731
3846
|
);
|
|
3732
|
-
|
|
3733
|
-
return response.data.result;
|
|
3734
|
-
} else if (response.data?.text) {
|
|
3735
|
-
return response.data;
|
|
3736
|
-
} else {
|
|
3737
|
-
throw new BlinkAIError("Invalid response format: missing text");
|
|
3738
|
-
}
|
|
3847
|
+
return response.data;
|
|
3739
3848
|
} catch (error) {
|
|
3740
3849
|
if (error instanceof BlinkAIError) {
|
|
3741
3850
|
throw error;
|
|
@@ -3804,9 +3913,14 @@ var BlinkAIImpl = class {
|
|
|
3804
3913
|
);
|
|
3805
3914
|
return {
|
|
3806
3915
|
text: result.text || "",
|
|
3807
|
-
finishReason: "stop",
|
|
3916
|
+
finishReason: result.finishReason || "stop",
|
|
3808
3917
|
usage: result.usage,
|
|
3809
|
-
|
|
3918
|
+
toolCalls: result.toolCalls,
|
|
3919
|
+
toolResults: result.toolResults,
|
|
3920
|
+
sources: result.sources,
|
|
3921
|
+
files: result.files,
|
|
3922
|
+
reasoningDetails: result.reasoning,
|
|
3923
|
+
response: result.response
|
|
3810
3924
|
};
|
|
3811
3925
|
} catch (error) {
|
|
3812
3926
|
if (error instanceof BlinkAIError) {
|
|
@@ -3885,13 +3999,7 @@ var BlinkAIImpl = class {
|
|
|
3885
3999
|
signal: options.signal
|
|
3886
4000
|
}
|
|
3887
4001
|
);
|
|
3888
|
-
|
|
3889
|
-
return response.data.result;
|
|
3890
|
-
} else if (response.data?.object) {
|
|
3891
|
-
return response.data;
|
|
3892
|
-
} else {
|
|
3893
|
-
throw new BlinkAIError("Invalid response format: missing object");
|
|
3894
|
-
}
|
|
4002
|
+
return response.data;
|
|
3895
4003
|
} catch (error) {
|
|
3896
4004
|
if (error instanceof BlinkAIError) {
|
|
3897
4005
|
throw error;
|
|
@@ -3953,8 +4061,7 @@ var BlinkAIImpl = class {
|
|
|
3953
4061
|
return {
|
|
3954
4062
|
object: result.object || {},
|
|
3955
4063
|
finishReason: "stop",
|
|
3956
|
-
usage: result.usage
|
|
3957
|
-
...result
|
|
4064
|
+
usage: result.usage
|
|
3958
4065
|
};
|
|
3959
4066
|
} catch (error) {
|
|
3960
4067
|
if (error instanceof BlinkAIError) {
|
|
@@ -4215,6 +4322,131 @@ var BlinkAIImpl = class {
|
|
|
4215
4322
|
);
|
|
4216
4323
|
}
|
|
4217
4324
|
}
|
|
4325
|
+
/**
|
|
4326
|
+
* Generates videos from text prompts or images using AI video generation models.
|
|
4327
|
+
*
|
|
4328
|
+
* @param options - Object containing:
|
|
4329
|
+
* - `prompt`: Text description of the video to generate (required)
|
|
4330
|
+
* - `model`: Video model to use (optional). Available models:
|
|
4331
|
+
* **Text-to-Video Models:**
|
|
4332
|
+
* - `"fal-ai/veo3.1"` - Google Veo 3.1 (best quality)
|
|
4333
|
+
* - `"fal-ai/veo3.1/fast"` (default) - Veo 3.1 fast mode (faster, cheaper)
|
|
4334
|
+
* - `"fal-ai/sora-2/text-to-video/pro"` - OpenAI Sora 2
|
|
4335
|
+
* - `"fal-ai/kling-video/v2.6/pro/text-to-video"` - Kling 2.6
|
|
4336
|
+
* **Image-to-Video Models:**
|
|
4337
|
+
* - `"fal-ai/veo3.1/image-to-video"` - Veo 3.1 I2V
|
|
4338
|
+
* - `"fal-ai/veo3.1/fast/image-to-video"` - Veo 3.1 fast I2V
|
|
4339
|
+
* - `"fal-ai/sora-2/image-to-video/pro"` - Sora 2 I2V
|
|
4340
|
+
* - `"fal-ai/kling-video/v2.6/pro/image-to-video"` - Kling 2.6 I2V
|
|
4341
|
+
* - `image_url`: Source image URL for image-to-video (required for I2V models)
|
|
4342
|
+
* - `duration`: Video duration ("4s", "5s", "6s", "8s", "10s", "12s")
|
|
4343
|
+
* - `aspect_ratio`: Aspect ratio ("16:9", "9:16", "1:1")
|
|
4344
|
+
* - `resolution`: Resolution ("720p", "1080p") - Veo/Sora only
|
|
4345
|
+
* - `negative_prompt`: What to avoid in generation - Veo/Kling only
|
|
4346
|
+
* - `generate_audio`: Generate audio with video (default: true)
|
|
4347
|
+
* - `seed`: For reproducibility - Veo only
|
|
4348
|
+
* - `cfg_scale`: Guidance scale (0-1) - Kling only
|
|
4349
|
+
* - Plus optional signal parameter
|
|
4350
|
+
*
|
|
4351
|
+
* @example
|
|
4352
|
+
* ```ts
|
|
4353
|
+
* // Basic text-to-video generation (uses default fast model)
|
|
4354
|
+
* const { result } = await blink.ai.generateVideo({
|
|
4355
|
+
* prompt: "A serene sunset over the ocean with gentle waves"
|
|
4356
|
+
* });
|
|
4357
|
+
* console.log("Video URL:", result.video.url);
|
|
4358
|
+
*
|
|
4359
|
+
* // High quality with Veo 3.1
|
|
4360
|
+
* const { result } = await blink.ai.generateVideo({
|
|
4361
|
+
* prompt: "A cinematic shot of a futuristic city at night",
|
|
4362
|
+
* model: "fal-ai/veo3.1",
|
|
4363
|
+
* resolution: "1080p",
|
|
4364
|
+
* aspect_ratio: "16:9"
|
|
4365
|
+
* });
|
|
4366
|
+
*
|
|
4367
|
+
* // Image-to-video animation
|
|
4368
|
+
* const { result } = await blink.ai.generateVideo({
|
|
4369
|
+
* prompt: "Animate this image with gentle camera movement",
|
|
4370
|
+
* model: "fal-ai/veo3.1/fast/image-to-video",
|
|
4371
|
+
* image_url: "https://example.com/my-image.jpg",
|
|
4372
|
+
* duration: "5s"
|
|
4373
|
+
* });
|
|
4374
|
+
*
|
|
4375
|
+
* // Using Sora 2 for creative videos
|
|
4376
|
+
* const { result } = await blink.ai.generateVideo({
|
|
4377
|
+
* prompt: "A magical forest with glowing fireflies",
|
|
4378
|
+
* model: "fal-ai/sora-2/text-to-video/pro",
|
|
4379
|
+
* duration: "8s"
|
|
4380
|
+
* });
|
|
4381
|
+
*
|
|
4382
|
+
* // Using Kling for detailed videos
|
|
4383
|
+
* const { result, usage } = await blink.ai.generateVideo({
|
|
4384
|
+
* prompt: "A professional cooking tutorial scene",
|
|
4385
|
+
* model: "fal-ai/kling-video/v2.6/pro/text-to-video",
|
|
4386
|
+
* negative_prompt: "blur, distort, low quality",
|
|
4387
|
+
* cfg_scale: 0.7
|
|
4388
|
+
* });
|
|
4389
|
+
* console.log("Credits charged:", usage?.creditsCharged);
|
|
4390
|
+
* ```
|
|
4391
|
+
*
|
|
4392
|
+
* @returns Promise<VideoGenerationResponse> - Object containing:
|
|
4393
|
+
* - `result.video.url`: URL to the generated video
|
|
4394
|
+
* - `result.video.content_type`: MIME type (video/mp4)
|
|
4395
|
+
* - `result.video.file_name`: Generated filename
|
|
4396
|
+
* - `result.video.file_size`: File size in bytes
|
|
4397
|
+
* - `metadata`: Generation metadata (projectId, timestamp, model)
|
|
4398
|
+
* - `usage`: Credits charged and cost information
|
|
4399
|
+
*/
|
|
4400
|
+
async generateVideo(options) {
|
|
4401
|
+
try {
|
|
4402
|
+
if (!options.prompt) {
|
|
4403
|
+
throw new BlinkAIError("Prompt is required");
|
|
4404
|
+
}
|
|
4405
|
+
const i2vModels = [
|
|
4406
|
+
"fal-ai/veo3.1/image-to-video",
|
|
4407
|
+
"fal-ai/veo3.1/fast/image-to-video",
|
|
4408
|
+
"fal-ai/sora-2/image-to-video/pro",
|
|
4409
|
+
"fal-ai/kling-video/v2.6/pro/image-to-video"
|
|
4410
|
+
];
|
|
4411
|
+
if (options.model && i2vModels.includes(options.model) && !options.image_url) {
|
|
4412
|
+
throw new BlinkAIError("image_url is required for image-to-video models");
|
|
4413
|
+
}
|
|
4414
|
+
if (options.image_url) {
|
|
4415
|
+
const validation = this.validateImageUrl(options.image_url);
|
|
4416
|
+
if (!validation.isValid) {
|
|
4417
|
+
throw new BlinkAIError(`Invalid image_url: ${validation.error}`);
|
|
4418
|
+
}
|
|
4419
|
+
}
|
|
4420
|
+
const response = await this.httpClient.aiVideo(
|
|
4421
|
+
options.prompt,
|
|
4422
|
+
{
|
|
4423
|
+
model: options.model,
|
|
4424
|
+
image_url: options.image_url,
|
|
4425
|
+
duration: options.duration,
|
|
4426
|
+
aspect_ratio: options.aspect_ratio,
|
|
4427
|
+
resolution: options.resolution,
|
|
4428
|
+
negative_prompt: options.negative_prompt,
|
|
4429
|
+
generate_audio: options.generate_audio,
|
|
4430
|
+
seed: options.seed,
|
|
4431
|
+
cfg_scale: options.cfg_scale,
|
|
4432
|
+
signal: options.signal
|
|
4433
|
+
}
|
|
4434
|
+
);
|
|
4435
|
+
if (!response.data?.result?.video?.url) {
|
|
4436
|
+
throw new BlinkAIError("Invalid response format: missing video URL");
|
|
4437
|
+
}
|
|
4438
|
+
return response.data;
|
|
4439
|
+
} catch (error) {
|
|
4440
|
+
if (error instanceof BlinkAIError) {
|
|
4441
|
+
throw error;
|
|
4442
|
+
}
|
|
4443
|
+
throw new BlinkAIError(
|
|
4444
|
+
`Video generation failed: ${error instanceof Error ? error.message : "Unknown error"}`,
|
|
4445
|
+
void 0,
|
|
4446
|
+
{ originalError: error }
|
|
4447
|
+
);
|
|
4448
|
+
}
|
|
4449
|
+
}
|
|
4218
4450
|
/**
|
|
4219
4451
|
* Converts text to speech using AI voice synthesis models.
|
|
4220
4452
|
*
|
|
@@ -5273,7 +5505,6 @@ var BlinkAnalyticsImpl = class {
|
|
|
5273
5505
|
} catch (error) {
|
|
5274
5506
|
this.queue = [...events, ...this.queue];
|
|
5275
5507
|
this.persistQueue();
|
|
5276
|
-
console.error("Failed to send analytics events:", error);
|
|
5277
5508
|
}
|
|
5278
5509
|
if (this.queue.length > 0) {
|
|
5279
5510
|
this.timer = setTimeout(() => this.flush(), BATCH_TIMEOUT);
|
|
@@ -5464,6 +5695,64 @@ var BlinkAnalyticsImpl = class {
|
|
|
5464
5695
|
}
|
|
5465
5696
|
};
|
|
5466
5697
|
|
|
5698
|
+
// src/connectors.ts
|
|
5699
|
+
var BlinkConnectorsImpl = class {
|
|
5700
|
+
constructor(httpClient) {
|
|
5701
|
+
this.httpClient = httpClient;
|
|
5702
|
+
}
|
|
5703
|
+
async status(provider, options) {
|
|
5704
|
+
const response = await this.httpClient.connectorStatus(provider);
|
|
5705
|
+
return response.data;
|
|
5706
|
+
}
|
|
5707
|
+
async execute(provider, request) {
|
|
5708
|
+
const response = await this.httpClient.connectorExecute(provider, request);
|
|
5709
|
+
return response.data;
|
|
5710
|
+
}
|
|
5711
|
+
async saveApiKey(provider, request) {
|
|
5712
|
+
const response = await this.httpClient.connectorSaveApiKey(provider, request);
|
|
5713
|
+
return response.data;
|
|
5714
|
+
}
|
|
5715
|
+
};
|
|
5716
|
+
|
|
5717
|
+
// src/functions.ts
|
|
5718
|
+
var BlinkFunctionsImpl = class {
|
|
5719
|
+
httpClient;
|
|
5720
|
+
projectId;
|
|
5721
|
+
constructor(httpClient, projectId, _getToken) {
|
|
5722
|
+
this.httpClient = httpClient;
|
|
5723
|
+
this.projectId = projectId;
|
|
5724
|
+
}
|
|
5725
|
+
/**
|
|
5726
|
+
* Get the project suffix from the full project ID.
|
|
5727
|
+
* Project IDs are formatted as: prj_xxxxx
|
|
5728
|
+
* The suffix is the last 8 characters used in function URLs.
|
|
5729
|
+
*/
|
|
5730
|
+
getProjectSuffix() {
|
|
5731
|
+
return this.projectId.slice(-8);
|
|
5732
|
+
}
|
|
5733
|
+
/**
|
|
5734
|
+
* Build the full function URL
|
|
5735
|
+
*/
|
|
5736
|
+
buildFunctionUrl(functionSlug, searchParams) {
|
|
5737
|
+
const suffix = this.getProjectSuffix();
|
|
5738
|
+
const baseUrl = `https://${suffix}--${functionSlug}.functions.blink.new`;
|
|
5739
|
+
if (!searchParams || Object.keys(searchParams).length === 0) {
|
|
5740
|
+
return baseUrl;
|
|
5741
|
+
}
|
|
5742
|
+
const url = new URL(baseUrl);
|
|
5743
|
+
Object.entries(searchParams).forEach(([key, value]) => {
|
|
5744
|
+
url.searchParams.set(key, value);
|
|
5745
|
+
});
|
|
5746
|
+
return url.toString();
|
|
5747
|
+
}
|
|
5748
|
+
async invoke(functionSlug, options = {}) {
|
|
5749
|
+
const { method = "POST", body, headers = {}, searchParams } = options;
|
|
5750
|
+
const url = this.buildFunctionUrl(functionSlug, searchParams);
|
|
5751
|
+
const res = await this.httpClient.request(url, { method, body, headers });
|
|
5752
|
+
return { data: res.data, status: res.status, headers: res.headers };
|
|
5753
|
+
}
|
|
5754
|
+
};
|
|
5755
|
+
|
|
5467
5756
|
// src/client.ts
|
|
5468
5757
|
var BlinkClientImpl = class {
|
|
5469
5758
|
auth;
|
|
@@ -5474,8 +5763,13 @@ var BlinkClientImpl = class {
|
|
|
5474
5763
|
realtime;
|
|
5475
5764
|
notifications;
|
|
5476
5765
|
analytics;
|
|
5766
|
+
connectors;
|
|
5767
|
+
functions;
|
|
5477
5768
|
httpClient;
|
|
5478
5769
|
constructor(config) {
|
|
5770
|
+
if ((config.secretKey || config.serviceToken) && isBrowser) {
|
|
5771
|
+
throw new Error("secretKey/serviceToken is server-only. Do not provide it in browser/React Native clients.");
|
|
5772
|
+
}
|
|
5479
5773
|
this.auth = new BlinkAuth(config);
|
|
5480
5774
|
this.httpClient = new HttpClient(
|
|
5481
5775
|
config,
|
|
@@ -5489,6 +5783,12 @@ var BlinkClientImpl = class {
|
|
|
5489
5783
|
this.realtime = new BlinkRealtimeImpl(this.httpClient, config.projectId);
|
|
5490
5784
|
this.notifications = new BlinkNotificationsImpl(this.httpClient);
|
|
5491
5785
|
this.analytics = new BlinkAnalyticsImpl(this.httpClient, config.projectId);
|
|
5786
|
+
this.connectors = new BlinkConnectorsImpl(this.httpClient);
|
|
5787
|
+
this.functions = new BlinkFunctionsImpl(
|
|
5788
|
+
this.httpClient,
|
|
5789
|
+
config.projectId,
|
|
5790
|
+
() => this.auth.getValidToken()
|
|
5791
|
+
);
|
|
5492
5792
|
this.auth.onAuthStateChanged((state) => {
|
|
5493
5793
|
if (state.isAuthenticated && state.user) {
|
|
5494
5794
|
this.analytics.setUserId(state.user.id);
|
|
@@ -5510,6 +5810,7 @@ function createClient(config) {
|
|
|
5510
5810
|
exports.AsyncStorageAdapter = AsyncStorageAdapter;
|
|
5511
5811
|
exports.BlinkAIImpl = BlinkAIImpl;
|
|
5512
5812
|
exports.BlinkAnalyticsImpl = BlinkAnalyticsImpl;
|
|
5813
|
+
exports.BlinkConnectorsImpl = BlinkConnectorsImpl;
|
|
5513
5814
|
exports.BlinkDataImpl = BlinkDataImpl;
|
|
5514
5815
|
exports.BlinkDatabase = BlinkDatabase;
|
|
5515
5816
|
exports.BlinkRealtimeChannel = BlinkRealtimeChannel;
|
|
@@ -5521,8 +5822,10 @@ exports.WebStorageAdapter = WebStorageAdapter;
|
|
|
5521
5822
|
exports.createClient = createClient;
|
|
5522
5823
|
exports.getDefaultStorageAdapter = getDefaultStorageAdapter;
|
|
5523
5824
|
exports.isBrowser = isBrowser;
|
|
5825
|
+
exports.isDeno = isDeno;
|
|
5524
5826
|
exports.isNode = isNode;
|
|
5525
5827
|
exports.isReactNative = isReactNative;
|
|
5828
|
+
exports.isServer = isServer;
|
|
5526
5829
|
exports.isWeb = isWeb;
|
|
5527
5830
|
exports.platform = platform;
|
|
5528
5831
|
//# sourceMappingURL=index.js.map
|