@genex-ai/embed-sdk 0.31.0 → 0.33.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.
@@ -1,264 +0,0 @@
1
- // ../embed-protocol/src/constants.ts
2
- var NATIVE_CHANNEL = "genex-native";
3
- var NATIVE_PROTOCOL_VERSION = 1;
4
- var NATIVE_MARKER_PARAM = "genex_native";
5
- var NATIVE_MARKER_VALUE = "1";
6
- var NATIVE_PROTOCOL_PARAM = "genex_protocol";
7
- var NATIVE_ATTEMPT_PARAM = "genex_attempt";
8
- var NATIVE_NONCE_PARAM = "genex_nonce";
9
- var NATIVE_RECEIVER = "__genexNative";
10
- var MAX_BRIDGE_MESSAGE_BYTES = 4096;
11
- var BRIDGE_ID_RE = /^[0-9a-f]{32}$/;
12
- var SLUG_RE = /^[a-z0-9]([a-z0-9-]{0,61}[a-z0-9])?$/;
13
- function isBridgeId(value) {
14
- return typeof value === "string" && BRIDGE_ID_RE.test(value);
15
- }
16
- function isBridgeSlug(value) {
17
- return typeof value === "string" && SLUG_RE.test(value);
18
- }
19
-
20
- // ../embed-protocol/src/base64url.ts
21
- var ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
22
- var LOOKUP = (() => {
23
- const table = new Array(128).fill(-1);
24
- for (let i = 0; i < ALPHABET.length; i++) table[ALPHABET.charCodeAt(i)] = i;
25
- return table;
26
- })();
27
- function utf8Bytes(text) {
28
- const out = [];
29
- for (let i = 0; i < text.length; i++) {
30
- let code = text.charCodeAt(i);
31
- if (code >= 55296 && code <= 56319) {
32
- const next = i + 1 < text.length ? text.charCodeAt(i + 1) : 0;
33
- if (next >= 56320 && next <= 57343) {
34
- code = 65536 + (code - 55296 << 10) + (next - 56320);
35
- i++;
36
- } else {
37
- code = 65533;
38
- }
39
- } else if (code >= 56320 && code <= 57343) {
40
- code = 65533;
41
- }
42
- if (code < 128) out.push(code);
43
- else if (code < 2048) out.push(192 | code >> 6, 128 | code & 63);
44
- else if (code < 65536) {
45
- out.push(224 | code >> 12, 128 | code >> 6 & 63, 128 | code & 63);
46
- } else {
47
- out.push(
48
- 240 | code >> 18,
49
- 128 | code >> 12 & 63,
50
- 128 | code >> 6 & 63,
51
- 128 | code & 63
52
- );
53
- }
54
- }
55
- return out;
56
- }
57
- function utf8Text(bytes) {
58
- let out = "";
59
- for (let i = 0; i < bytes.length; ) {
60
- const b0 = bytes[i];
61
- let code;
62
- let size;
63
- if (b0 < 128) {
64
- code = b0;
65
- size = 1;
66
- } else if ((b0 & 224) === 192) {
67
- code = b0 & 31;
68
- size = 2;
69
- } else if ((b0 & 240) === 224) {
70
- code = b0 & 15;
71
- size = 3;
72
- } else if ((b0 & 248) === 240) {
73
- code = b0 & 7;
74
- size = 4;
75
- } else return null;
76
- if (i + size > bytes.length) return null;
77
- for (let k = 1; k < size; k++) {
78
- const b = bytes[i + k];
79
- if ((b & 192) !== 128) return null;
80
- code = code << 6 | b & 63;
81
- }
82
- if (size === 2 && code < 128) return null;
83
- if (size === 3 && (code < 2048 || code >= 55296 && code <= 57343)) return null;
84
- if (size === 4 && (code < 65536 || code > 1114111)) return null;
85
- if (code > 65535) {
86
- const v = code - 65536;
87
- out += String.fromCharCode(55296 + (v >> 10), 56320 + (v & 1023));
88
- } else {
89
- out += String.fromCharCode(code);
90
- }
91
- i += size;
92
- }
93
- return out;
94
- }
95
- function utf8ByteLength(text) {
96
- return utf8Bytes(text).length;
97
- }
98
- function encodeBase64UrlJson(value) {
99
- const bytes = utf8Bytes(JSON.stringify(value));
100
- let out = "";
101
- for (let i = 0; i < bytes.length; i += 3) {
102
- const b0 = bytes[i];
103
- const b1 = bytes[i + 1];
104
- const b2 = bytes[i + 2];
105
- out += ALPHABET[b0 >> 2];
106
- out += ALPHABET[(b0 & 3) << 4 | (b1 ?? 0) >> 4];
107
- if (b1 === void 0) break;
108
- out += ALPHABET[(b1 & 15) << 2 | (b2 ?? 0) >> 6];
109
- if (b2 === void 0) break;
110
- out += ALPHABET[b2 & 63];
111
- }
112
- return out;
113
- }
114
- function decodeBase64UrlJson(encoded) {
115
- if (encoded.length % 4 === 1) return void 0;
116
- const bytes = [];
117
- let buffer = 0;
118
- let bits = 0;
119
- for (let i = 0; i < encoded.length; i++) {
120
- const code = encoded.charCodeAt(i);
121
- const value = code < 128 ? LOOKUP[code] : -1;
122
- if (value < 0) return void 0;
123
- buffer = buffer << 6 | value;
124
- bits += 6;
125
- if (bits >= 8) {
126
- bits -= 8;
127
- bytes.push(buffer >> bits & 255);
128
- }
129
- }
130
- if (bits > 0 && (buffer & (1 << bits) - 1) !== 0) return void 0;
131
- const text = utf8Text(bytes);
132
- if (text === null) return void 0;
133
- try {
134
- return JSON.parse(text);
135
- } catch {
136
- return void 0;
137
- }
138
- }
139
-
140
- // ../embed-protocol/src/messages.ts
141
- var MAX_TICKET_LENGTH = 2048;
142
- var MAX_EXPIRES_AT_LENGTH = 32;
143
- var ISO_INSTANT_RE = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(\.\d{1,3})?Z$/;
144
- function isRecord(value) {
145
- return typeof value === "object" && value !== null && !Array.isArray(value);
146
- }
147
- function hasExactKeys(value, keys) {
148
- const own = Object.keys(value);
149
- if (own.length !== keys.length) return false;
150
- for (const key of keys) {
151
- if (!Object.prototype.hasOwnProperty.call(value, key)) return false;
152
- }
153
- return true;
154
- }
155
- var BASE_KEYS = ["channel", "v", "type", "slug", "attemptId", "nonce"];
156
- function envelopeBaseIsValid(value) {
157
- return value.channel === NATIVE_CHANNEL && value.v === NATIVE_PROTOCOL_VERSION && isBridgeSlug(value.slug) && isBridgeId(value.attemptId) && isBridgeId(value.nonce);
158
- }
159
- function parseBridgeNativeCommand(value) {
160
- if (!isRecord(value) || !envelopeBaseIsValid(value)) return void 0;
161
- switch (value.type) {
162
- case "ticket":
163
- if (!hasExactKeys(value, [...BASE_KEYS, "ticket", "expiresAt"])) return void 0;
164
- if (typeof value.ticket !== "string" || value.ticket.length === 0 || value.ticket.length > MAX_TICKET_LENGTH) {
165
- return void 0;
166
- }
167
- if (typeof value.expiresAt !== "string" || value.expiresAt.length > MAX_EXPIRES_AT_LENGTH || !ISO_INSTANT_RE.test(value.expiresAt)) {
168
- return void 0;
169
- }
170
- return value;
171
- case "guest":
172
- case "cancel":
173
- if (!hasExactKeys(value, BASE_KEYS)) return void 0;
174
- return value;
175
- case "foreground":
176
- if (!hasExactKeys(value, [...BASE_KEYS, "active"])) return void 0;
177
- if (typeof value.active !== "boolean") return void 0;
178
- return value;
179
- case "network":
180
- if (!hasExactKeys(value, [...BASE_KEYS, "online"])) return void 0;
181
- if (typeof value.online !== "boolean") return void 0;
182
- return value;
183
- default:
184
- return void 0;
185
- }
186
- }
187
-
188
- // ../embed-protocol/src/transport.ts
189
- function read(raw, parse) {
190
- if (typeof raw !== "string" || raw.length === 0) return { ok: false, reason: "undecodable" };
191
- if (utf8ByteLength(raw) > MAX_BRIDGE_MESSAGE_BYTES) return { ok: false, reason: "too-large" };
192
- const decoded = decodeBase64UrlJson(raw);
193
- if (decoded === void 0) return { ok: false, reason: "undecodable" };
194
- const message = parse(decoded);
195
- if (message === void 0) return { ok: false, reason: "invalid" };
196
- return { ok: true, message };
197
- }
198
- function readBridgeNativeCommand(raw) {
199
- return read(raw, parseBridgeNativeCommand);
200
- }
201
- function encodeBridgeMessage(message) {
202
- const encoded = encodeBase64UrlJson(message);
203
- if (encoded.length > MAX_BRIDGE_MESSAGE_BYTES) return void 0;
204
- return encoded;
205
- }
206
-
207
- // ../embed-protocol/src/entry-url.ts
208
- function readParam(search, key) {
209
- const query = search.charAt(0) === "?" ? search.slice(1) : search;
210
- if (query.length === 0) return null;
211
- for (const pair of query.split("&")) {
212
- const eq = pair.indexOf("=");
213
- if (eq === -1) continue;
214
- if (pair.slice(0, eq) === key) return pair.slice(eq + 1);
215
- }
216
- return null;
217
- }
218
- function hasNativeMarker(search) {
219
- return readParam(search, NATIVE_MARKER_PARAM) === NATIVE_MARKER_VALUE;
220
- }
221
- function readNativeEntry(search) {
222
- if (!hasNativeMarker(search)) return null;
223
- const attemptId = readParam(search, NATIVE_ATTEMPT_PARAM);
224
- const nonce = readParam(search, NATIVE_NONCE_PARAM);
225
- const protocol = Number(readParam(search, NATIVE_PROTOCOL_PARAM));
226
- if (!isBridgeId(attemptId) || !isBridgeId(nonce)) return null;
227
- if (protocol !== NATIVE_PROTOCOL_VERSION) return null;
228
- return { attemptId, nonce, protocol };
229
- }
230
-
231
- // ../embed-protocol/src/runtime-generation.ts
232
- function isGenerationId(value) {
233
- return typeof value === "string" && /^[A-Za-z0-9_-]{1,128}$/.test(value);
234
- }
235
- function parseGenerationStreamEvent(event, data) {
236
- let value;
237
- try {
238
- value = JSON.parse(data);
239
- } catch {
240
- return void 0;
241
- }
242
- if (typeof value !== "object" || value === null || Array.isArray(value)) return void 0;
243
- const fields = value;
244
- if (event === "start") return isGenerationId(fields.generationId) ? { event, data: { generationId: fields.generationId } } : void 0;
245
- if (event === "delta") return typeof fields.text === "string" ? { event, data: { text: fields.text } } : void 0;
246
- if (event === "done") return isGenerationId(fields.id) && typeof fields.status === "string" ? { event, data: value } : void 0;
247
- return void 0;
248
- }
249
- function isGenerationTerminal(status) {
250
- return ["succeeded", "failed", "unknown", "canceled", "expired"].includes(status);
251
- }
252
-
253
- export {
254
- NATIVE_CHANNEL,
255
- NATIVE_PROTOCOL_VERSION,
256
- NATIVE_RECEIVER,
257
- readBridgeNativeCommand,
258
- encodeBridgeMessage,
259
- hasNativeMarker,
260
- readNativeEntry,
261
- isGenerationId,
262
- parseGenerationStreamEvent,
263
- isGenerationTerminal
264
- };