clivly 0.23.1 → 0.24.0-next.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 +28 -0
- package/{adapter-B4v4dwgD.d.mts → adapter--YV7du3i.d.cts} +1 -1
- package/{adapter-BK3gsby5.d.cts → adapter-DD3K3dCW.d.mts} +1 -1
- package/{auth-adapter-BIa-ZyVV.d.mts → auth-adapter-C17dzlzF.d.cts} +1 -1
- package/{auth-adapter-DwtMJb7D.d.cts → auth-adapter-CizFv7oU.d.mts} +1 -1
- package/auth-better-auth.d.cts +1 -1
- package/auth-better-auth.d.mts +1 -1
- package/auth-clerk.d.cts +1 -1
- package/auth-clerk.d.mts +1 -1
- package/auth-supabase.cjs +121 -0
- package/auth-supabase.d.cts +23 -0
- package/auth-supabase.d.mts +23 -0
- package/auth-supabase.mjs +119 -0
- package/auth-workos.d.cts +1 -1
- package/auth-workos.d.mts +1 -1
- package/core-adapter.d.cts +1 -1
- package/core-adapter.d.mts +1 -1
- package/core-auth-adapter.d.cts +1 -1
- package/core-auth-adapter.d.mts +1 -1
- package/core-mapping-score.d.cts +1 -0
- package/core-sync-engine.cjs +1 -1
- package/core-sync-engine.mjs +1 -1
- package/core-types.cjs +5 -1
- package/core-types.d.cts +2 -2
- package/core-types.d.mts +2 -2
- package/core-types.mjs +1 -1
- package/core-view-compiler.cjs +1 -1
- package/core-view-compiler.mjs +1 -1
- package/core.cjs +9 -5
- package/core.d.cts +6 -6
- package/core.d.mts +6 -6
- package/core.mjs +6 -6
- package/discover-BZIPXl23.cjs +200 -0
- package/discover-CFIi43Qn.mjs +195 -0
- package/drizzle.cjs +1 -1
- package/drizzle.d.cts +2 -2
- package/drizzle.d.mts +2 -2
- package/drizzle.mjs +1 -1
- package/executor-CJEWurjZ.mjs +23 -0
- package/executor-CU-jRgpC.cjs +40 -0
- package/index-CZkp8TV4.d.cts +108 -0
- package/index-Cnrz4ywf.d.mts +108 -0
- package/{index-Bh7o2J7K.d.mts → index-DOoX2GgI.d.mts} +3 -3
- package/{index-DRRTE5IJ.d.cts → index-uFDiQEjj.d.cts} +231 -5
- package/index.cjs +903 -30
- package/index.d.cts +27 -0
- package/index.d.mts +26 -0
- package/index.mjs +901 -28
- package/{internal-CdfVkh4O.d.mts → internal-D_2q2BOB.d.mts} +1 -1
- package/internal-adapter.d.cts +1 -1
- package/internal-adapter.d.mts +1 -1
- package/internal-auth-adapter.d.cts +1 -1
- package/internal-auth-adapter.d.mts +1 -1
- package/internal-sync-engine.cjs +1 -1
- package/internal-sync-engine.mjs +1 -1
- package/internal-types.cjs +5 -1
- package/internal-types.d.cts +2 -2
- package/internal-types.d.mts +2 -2
- package/internal-types.mjs +1 -1
- package/internal-view-compiler.cjs +1 -1
- package/internal-view-compiler.mjs +1 -1
- package/internal.cjs +9 -5
- package/internal.d.cts +5 -6
- package/internal.d.mts +6 -6
- package/internal.mjs +6 -6
- package/introspector-BwYgHGcS.cjs +363 -0
- package/introspector-Bz52xOUr.mjs +334 -0
- package/{namespace-probe-JuHriruA.d.mts → namespace-probe--GvxJ06J.d.mts} +8 -1
- package/package.json +31 -1
- package/projection-guard-7AMY0aCk.mjs +389 -0
- package/projection-guard-D8VUhCz7.cjs +508 -0
- package/{projection-sql-DQQdjw9I.mjs → projection-sql-2joBkLz2.mjs} +13 -3
- package/{projection-sql-BkrHR2ea.cjs → projection-sql-DS5sVTY-.cjs} +18 -2
- package/sdk-Bzkr24NW.cjs +207 -0
- package/sdk-D2WwGBzk.mjs +178 -0
- package/sdk.cjs +8 -6
- package/sdk.d.cts +7 -1
- package/sdk.d.mts +7 -1
- package/sdk.mjs +3 -2
- package/sql.cjs +13 -0
- package/sql.d.cts +2 -0
- package/sql.d.mts +2 -0
- package/sql.mjs +4 -0
- package/{sdk-BkMDOfCk.mjs → src-Dq2nY1Sj.mjs} +36 -495
- package/{sdk-BOarpe5I.cjs → src-X0-yIA3O.cjs} +63 -546
- package/supabase.cjs +255 -0
- package/supabase.d.cts +19 -0
- package/supabase.d.mts +19 -0
- package/supabase.mjs +254 -0
- package/{sync-engine-yFLEyId3.mjs → sync-engine-D0mJUeqP.mjs} +1 -1
- package/{sync-engine-DNSoZeo0.cjs → sync-engine-hU041kXO.cjs} +1 -1
- package/types-KMH5D5Lg.mjs +32 -0
- package/{namespace-probe-CcL6Wj8E.d.cts → types-UR-tT4c1.d.cts} +52 -45
- package/{types-BBfwsDxe.d.cts → types-f-Vcv-pl.d.cts} +18 -2
- package/{types-BBfwsDxe.d.mts → types-f-Vcv-pl.d.mts} +18 -2
- package/types-kR1kUlmz.cjs +67 -0
- package/{view-compiler-CSHSqZl7.mjs → view-compiler-BhKiezRH.mjs} +1 -1
- package/{view-compiler-NqiortOA.cjs → view-compiler-DgZadq1F.cjs} +1 -1
- package/vite.d.cts +1 -1
- package/vite.d.mts +1 -1
- package/internal-D7qVRIFb.d.cts +0 -228
- package/types-8KSw2AWg.mjs +0 -8
- package/types-B8m39L1s.cjs +0 -19
package/sdk-Bzkr24NW.cjs
ADDED
|
@@ -0,0 +1,207 @@
|
|
|
1
|
+
// ../sdk/src/chat-session.ts
|
|
2
|
+
const DEFAULT_API_URL = "https://api.clivly.com";
|
|
3
|
+
const JSON_HEADERS = { "content-type": "application/json" };
|
|
4
|
+
const TRAILING_SLASH = /\/$/;
|
|
5
|
+
function jsonResponse(body, status) {
|
|
6
|
+
return new Response(JSON.stringify(body), {
|
|
7
|
+
status,
|
|
8
|
+
headers: JSON_HEADERS
|
|
9
|
+
});
|
|
10
|
+
}
|
|
11
|
+
function normalizeApiUrl(apiUrl) {
|
|
12
|
+
return apiUrl.replace(TRAILING_SLASH, "");
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* The origin *claimed* by the request, for reporting upstream.
|
|
16
|
+
*
|
|
17
|
+
* Falls back to the request URL so Clivly still receives an origin for a
|
|
18
|
+
* non-browser caller. This value is reported, never trusted: only a real
|
|
19
|
+
* `Origin` header is evidence of where a request came from, so the access
|
|
20
|
+
* decision uses {@link trustedOrigin} instead. Keeping the two apart is the
|
|
21
|
+
* point — conflating them let a caller that simply omitted the header be
|
|
22
|
+
* judged against the server's own origin, which is usually in the allowlist.
|
|
23
|
+
*/
|
|
24
|
+
function reportedOrigin(req) {
|
|
25
|
+
const headerOrigin = trustedOrigin(req);
|
|
26
|
+
if (headerOrigin) return headerOrigin;
|
|
27
|
+
try {
|
|
28
|
+
return new URL(req.url).origin;
|
|
29
|
+
} catch {
|
|
30
|
+
return null;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* The only origin worth an access decision: one the browser set itself.
|
|
35
|
+
*
|
|
36
|
+
* A missing header means "not a browser request we can vouch for", which is
|
|
37
|
+
* untrusted — never the server's own origin.
|
|
38
|
+
*/
|
|
39
|
+
function trustedOrigin(req) {
|
|
40
|
+
return req.headers.get("origin");
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* The visitor's address as the host's platform reported it. Clivly throttles
|
|
44
|
+
* new chats per visitor on it; without it the only address Clivly sees is this
|
|
45
|
+
* server's, so every visitor shared one bucket.
|
|
46
|
+
*/
|
|
47
|
+
function defaultVisitorIp(req) {
|
|
48
|
+
const direct = req.headers.get("cf-connecting-ip") ?? req.headers.get("x-real-ip");
|
|
49
|
+
if (direct) return direct.trim();
|
|
50
|
+
return req.headers.get("x-forwarded-for")?.split(",")[0]?.trim() || null;
|
|
51
|
+
}
|
|
52
|
+
function isChatSessionRequestBody(value) {
|
|
53
|
+
if (typeof value !== "object" || value === null) return false;
|
|
54
|
+
const body = value;
|
|
55
|
+
if (typeof body.widgetId !== "string" || body.widgetId.trim().length === 0) return false;
|
|
56
|
+
if (typeof body.visitor !== "object" || body.visitor === null || Array.isArray(body.visitor)) return false;
|
|
57
|
+
const visitor = body.visitor;
|
|
58
|
+
if (typeof visitor.name !== "string" || visitor.name.trim().length === 0) return false;
|
|
59
|
+
if (typeof visitor.email !== "string" || visitor.email.trim().length === 0) return false;
|
|
60
|
+
return body.visitorToken === void 0 || body.visitorToken === null || typeof body.visitorToken === "string";
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Judge a request against the configured allowlist.
|
|
64
|
+
*
|
|
65
|
+
* `origin` must be a *trusted* origin (see {@link trustedOrigin}) — passing
|
|
66
|
+
* the reported one would reinstate the bypass this guards against.
|
|
67
|
+
*
|
|
68
|
+
* An unset allowlist still admits every origin, because requiring it would
|
|
69
|
+
* break every existing deployment on upgrade. The unset case is instead
|
|
70
|
+
* warned about once at construction, so the permissive default is loud
|
|
71
|
+
* rather than silent.
|
|
72
|
+
*/
|
|
73
|
+
function originAllowed(origin, allowedOrigins) {
|
|
74
|
+
if (!allowedOrigins || allowedOrigins.length === 0) return true;
|
|
75
|
+
if (!origin) return false;
|
|
76
|
+
return allowedOrigins.includes(origin);
|
|
77
|
+
}
|
|
78
|
+
async function safeHostUser(resolveUser, request) {
|
|
79
|
+
if (!resolveUser) return null;
|
|
80
|
+
try {
|
|
81
|
+
return await resolveUser(request);
|
|
82
|
+
} catch {
|
|
83
|
+
console.warn("[clivly] resolveUser threw; continuing without host identity");
|
|
84
|
+
return null;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
function safeVisitorIp(resolveVisitorIp, request) {
|
|
88
|
+
try {
|
|
89
|
+
return resolveVisitorIp(request);
|
|
90
|
+
} catch {
|
|
91
|
+
return null;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
function createChatSessionHandler({ allowedOrigins, apiKey, apiUrl = DEFAULT_API_URL, fetchImpl = fetch, resolveUser, resolveVisitorIp = defaultVisitorIp }) {
|
|
95
|
+
const sessionUrl = `${normalizeApiUrl(apiUrl)}/clivly/widget/session`;
|
|
96
|
+
if (!allowedOrigins || allowedOrigins.length === 0) console.warn("[clivly] createChatSessionHandler has no allowedOrigins; every origin can open a chat session against this workspace. Pass allowedOrigins to restrict it.");
|
|
97
|
+
return async (request) => {
|
|
98
|
+
if (request.method !== "POST") return new Response(JSON.stringify({ error: "method_not_allowed" }), {
|
|
99
|
+
status: 405,
|
|
100
|
+
headers: {
|
|
101
|
+
...JSON_HEADERS,
|
|
102
|
+
allow: "POST"
|
|
103
|
+
}
|
|
104
|
+
});
|
|
105
|
+
if (!originAllowed(trustedOrigin(request), allowedOrigins)) return jsonResponse({ error: "origin_not_allowed" }, 403);
|
|
106
|
+
const origin = reportedOrigin(request);
|
|
107
|
+
let body;
|
|
108
|
+
try {
|
|
109
|
+
body = await request.json();
|
|
110
|
+
} catch {
|
|
111
|
+
return jsonResponse({ error: "invalid_json" }, 400);
|
|
112
|
+
}
|
|
113
|
+
if (!isChatSessionRequestBody(body)) return jsonResponse({ error: "invalid_request" }, 400);
|
|
114
|
+
const hostUser = await safeHostUser(resolveUser, request);
|
|
115
|
+
const visitorIp = safeVisitorIp(resolveVisitorIp, request);
|
|
116
|
+
try {
|
|
117
|
+
const upstream = await fetchImpl(sessionUrl, {
|
|
118
|
+
method: "POST",
|
|
119
|
+
headers: {
|
|
120
|
+
authorization: `Bearer ${apiKey}`,
|
|
121
|
+
"content-type": "application/json",
|
|
122
|
+
...origin ? { origin } : {}
|
|
123
|
+
},
|
|
124
|
+
body: JSON.stringify({
|
|
125
|
+
widgetId: body.widgetId,
|
|
126
|
+
visitor: body.visitor,
|
|
127
|
+
visitorToken: body.visitorToken,
|
|
128
|
+
origin,
|
|
129
|
+
...hostUser ? { hostUser } : {},
|
|
130
|
+
...visitorIp ? { visitorIp } : {}
|
|
131
|
+
})
|
|
132
|
+
});
|
|
133
|
+
const responseBody = await upstream.text();
|
|
134
|
+
return new Response(responseBody, {
|
|
135
|
+
status: upstream.status,
|
|
136
|
+
headers: { "content-type": upstream.headers.get("content-type") ?? "application/json" }
|
|
137
|
+
});
|
|
138
|
+
} catch {
|
|
139
|
+
return jsonResponse({ error: "upstream_unreachable" }, 502);
|
|
140
|
+
}
|
|
141
|
+
};
|
|
142
|
+
}
|
|
143
|
+
// ../sdk/src/errors.ts
|
|
144
|
+
var ClivlyError = class ClivlyError extends Error {
|
|
145
|
+
/** HTTP status, or null when the request never completed (network error). */
|
|
146
|
+
status;
|
|
147
|
+
reason;
|
|
148
|
+
/** Whether a retry could plausibly succeed (network, 429, 5xx). */
|
|
149
|
+
retryable;
|
|
150
|
+
constructor(result, message) {
|
|
151
|
+
super(message ?? `Clivly connection failed: ${result.reason}`);
|
|
152
|
+
this.name = new.target.name;
|
|
153
|
+
this.status = result.status;
|
|
154
|
+
this.reason = result.reason;
|
|
155
|
+
this.retryable = result.retryable;
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* Build the error subclass matching a failed result's reason, or `null` when
|
|
159
|
+
* the result is `ok`.
|
|
160
|
+
*/
|
|
161
|
+
static from(result) {
|
|
162
|
+
if (result.ok) return null;
|
|
163
|
+
switch (result.reason) {
|
|
164
|
+
case "invalid_api_key":
|
|
165
|
+
case "forbidden": return new ClivlyAuthError(result);
|
|
166
|
+
case "rate_limited": return new ClivlyRateLimitError(result);
|
|
167
|
+
case "network_error": return new ClivlyNetworkError(result);
|
|
168
|
+
default: return new ClivlyError(result);
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
};
|
|
172
|
+
/** The API key was rejected (401) or lacks access (403). Not retryable. */
|
|
173
|
+
var ClivlyAuthError = class extends ClivlyError {};
|
|
174
|
+
/** Rate limited (429). Retryable after backoff / `Retry-After`. */
|
|
175
|
+
var ClivlyRateLimitError = class extends ClivlyError {};
|
|
176
|
+
/** The request never reached the server (network/DNS/TLS). Retryable. */
|
|
177
|
+
var ClivlyNetworkError = class extends ClivlyError {};
|
|
178
|
+
Object.defineProperty(exports, "ClivlyAuthError", {
|
|
179
|
+
enumerable: true,
|
|
180
|
+
get: function() {
|
|
181
|
+
return ClivlyAuthError;
|
|
182
|
+
}
|
|
183
|
+
});
|
|
184
|
+
Object.defineProperty(exports, "ClivlyError", {
|
|
185
|
+
enumerable: true,
|
|
186
|
+
get: function() {
|
|
187
|
+
return ClivlyError;
|
|
188
|
+
}
|
|
189
|
+
});
|
|
190
|
+
Object.defineProperty(exports, "ClivlyNetworkError", {
|
|
191
|
+
enumerable: true,
|
|
192
|
+
get: function() {
|
|
193
|
+
return ClivlyNetworkError;
|
|
194
|
+
}
|
|
195
|
+
});
|
|
196
|
+
Object.defineProperty(exports, "ClivlyRateLimitError", {
|
|
197
|
+
enumerable: true,
|
|
198
|
+
get: function() {
|
|
199
|
+
return ClivlyRateLimitError;
|
|
200
|
+
}
|
|
201
|
+
});
|
|
202
|
+
Object.defineProperty(exports, "createChatSessionHandler", {
|
|
203
|
+
enumerable: true,
|
|
204
|
+
get: function() {
|
|
205
|
+
return createChatSessionHandler;
|
|
206
|
+
}
|
|
207
|
+
});
|
package/sdk-D2WwGBzk.mjs
ADDED
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
// ../sdk/src/chat-session.ts
|
|
2
|
+
const DEFAULT_API_URL = "https://api.clivly.com";
|
|
3
|
+
const JSON_HEADERS = { "content-type": "application/json" };
|
|
4
|
+
const TRAILING_SLASH = /\/$/;
|
|
5
|
+
function jsonResponse(body, status) {
|
|
6
|
+
return new Response(JSON.stringify(body), {
|
|
7
|
+
status,
|
|
8
|
+
headers: JSON_HEADERS
|
|
9
|
+
});
|
|
10
|
+
}
|
|
11
|
+
function normalizeApiUrl(apiUrl) {
|
|
12
|
+
return apiUrl.replace(TRAILING_SLASH, "");
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* The origin *claimed* by the request, for reporting upstream.
|
|
16
|
+
*
|
|
17
|
+
* Falls back to the request URL so Clivly still receives an origin for a
|
|
18
|
+
* non-browser caller. This value is reported, never trusted: only a real
|
|
19
|
+
* `Origin` header is evidence of where a request came from, so the access
|
|
20
|
+
* decision uses {@link trustedOrigin} instead. Keeping the two apart is the
|
|
21
|
+
* point — conflating them let a caller that simply omitted the header be
|
|
22
|
+
* judged against the server's own origin, which is usually in the allowlist.
|
|
23
|
+
*/
|
|
24
|
+
function reportedOrigin(req) {
|
|
25
|
+
const headerOrigin = trustedOrigin(req);
|
|
26
|
+
if (headerOrigin) return headerOrigin;
|
|
27
|
+
try {
|
|
28
|
+
return new URL(req.url).origin;
|
|
29
|
+
} catch {
|
|
30
|
+
return null;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* The only origin worth an access decision: one the browser set itself.
|
|
35
|
+
*
|
|
36
|
+
* A missing header means "not a browser request we can vouch for", which is
|
|
37
|
+
* untrusted — never the server's own origin.
|
|
38
|
+
*/
|
|
39
|
+
function trustedOrigin(req) {
|
|
40
|
+
return req.headers.get("origin");
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* The visitor's address as the host's platform reported it. Clivly throttles
|
|
44
|
+
* new chats per visitor on it; without it the only address Clivly sees is this
|
|
45
|
+
* server's, so every visitor shared one bucket.
|
|
46
|
+
*/
|
|
47
|
+
function defaultVisitorIp(req) {
|
|
48
|
+
const direct = req.headers.get("cf-connecting-ip") ?? req.headers.get("x-real-ip");
|
|
49
|
+
if (direct) return direct.trim();
|
|
50
|
+
return req.headers.get("x-forwarded-for")?.split(",")[0]?.trim() || null;
|
|
51
|
+
}
|
|
52
|
+
function isChatSessionRequestBody(value) {
|
|
53
|
+
if (typeof value !== "object" || value === null) return false;
|
|
54
|
+
const body = value;
|
|
55
|
+
if (typeof body.widgetId !== "string" || body.widgetId.trim().length === 0) return false;
|
|
56
|
+
if (typeof body.visitor !== "object" || body.visitor === null || Array.isArray(body.visitor)) return false;
|
|
57
|
+
const visitor = body.visitor;
|
|
58
|
+
if (typeof visitor.name !== "string" || visitor.name.trim().length === 0) return false;
|
|
59
|
+
if (typeof visitor.email !== "string" || visitor.email.trim().length === 0) return false;
|
|
60
|
+
return body.visitorToken === void 0 || body.visitorToken === null || typeof body.visitorToken === "string";
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Judge a request against the configured allowlist.
|
|
64
|
+
*
|
|
65
|
+
* `origin` must be a *trusted* origin (see {@link trustedOrigin}) — passing
|
|
66
|
+
* the reported one would reinstate the bypass this guards against.
|
|
67
|
+
*
|
|
68
|
+
* An unset allowlist still admits every origin, because requiring it would
|
|
69
|
+
* break every existing deployment on upgrade. The unset case is instead
|
|
70
|
+
* warned about once at construction, so the permissive default is loud
|
|
71
|
+
* rather than silent.
|
|
72
|
+
*/
|
|
73
|
+
function originAllowed(origin, allowedOrigins) {
|
|
74
|
+
if (!allowedOrigins || allowedOrigins.length === 0) return true;
|
|
75
|
+
if (!origin) return false;
|
|
76
|
+
return allowedOrigins.includes(origin);
|
|
77
|
+
}
|
|
78
|
+
async function safeHostUser(resolveUser, request) {
|
|
79
|
+
if (!resolveUser) return null;
|
|
80
|
+
try {
|
|
81
|
+
return await resolveUser(request);
|
|
82
|
+
} catch {
|
|
83
|
+
console.warn("[clivly] resolveUser threw; continuing without host identity");
|
|
84
|
+
return null;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
function safeVisitorIp(resolveVisitorIp, request) {
|
|
88
|
+
try {
|
|
89
|
+
return resolveVisitorIp(request);
|
|
90
|
+
} catch {
|
|
91
|
+
return null;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
function createChatSessionHandler({ allowedOrigins, apiKey, apiUrl = DEFAULT_API_URL, fetchImpl = fetch, resolveUser, resolveVisitorIp = defaultVisitorIp }) {
|
|
95
|
+
const sessionUrl = `${normalizeApiUrl(apiUrl)}/clivly/widget/session`;
|
|
96
|
+
if (!allowedOrigins || allowedOrigins.length === 0) console.warn("[clivly] createChatSessionHandler has no allowedOrigins; every origin can open a chat session against this workspace. Pass allowedOrigins to restrict it.");
|
|
97
|
+
return async (request) => {
|
|
98
|
+
if (request.method !== "POST") return new Response(JSON.stringify({ error: "method_not_allowed" }), {
|
|
99
|
+
status: 405,
|
|
100
|
+
headers: {
|
|
101
|
+
...JSON_HEADERS,
|
|
102
|
+
allow: "POST"
|
|
103
|
+
}
|
|
104
|
+
});
|
|
105
|
+
if (!originAllowed(trustedOrigin(request), allowedOrigins)) return jsonResponse({ error: "origin_not_allowed" }, 403);
|
|
106
|
+
const origin = reportedOrigin(request);
|
|
107
|
+
let body;
|
|
108
|
+
try {
|
|
109
|
+
body = await request.json();
|
|
110
|
+
} catch {
|
|
111
|
+
return jsonResponse({ error: "invalid_json" }, 400);
|
|
112
|
+
}
|
|
113
|
+
if (!isChatSessionRequestBody(body)) return jsonResponse({ error: "invalid_request" }, 400);
|
|
114
|
+
const hostUser = await safeHostUser(resolveUser, request);
|
|
115
|
+
const visitorIp = safeVisitorIp(resolveVisitorIp, request);
|
|
116
|
+
try {
|
|
117
|
+
const upstream = await fetchImpl(sessionUrl, {
|
|
118
|
+
method: "POST",
|
|
119
|
+
headers: {
|
|
120
|
+
authorization: `Bearer ${apiKey}`,
|
|
121
|
+
"content-type": "application/json",
|
|
122
|
+
...origin ? { origin } : {}
|
|
123
|
+
},
|
|
124
|
+
body: JSON.stringify({
|
|
125
|
+
widgetId: body.widgetId,
|
|
126
|
+
visitor: body.visitor,
|
|
127
|
+
visitorToken: body.visitorToken,
|
|
128
|
+
origin,
|
|
129
|
+
...hostUser ? { hostUser } : {},
|
|
130
|
+
...visitorIp ? { visitorIp } : {}
|
|
131
|
+
})
|
|
132
|
+
});
|
|
133
|
+
const responseBody = await upstream.text();
|
|
134
|
+
return new Response(responseBody, {
|
|
135
|
+
status: upstream.status,
|
|
136
|
+
headers: { "content-type": upstream.headers.get("content-type") ?? "application/json" }
|
|
137
|
+
});
|
|
138
|
+
} catch {
|
|
139
|
+
return jsonResponse({ error: "upstream_unreachable" }, 502);
|
|
140
|
+
}
|
|
141
|
+
};
|
|
142
|
+
}
|
|
143
|
+
// ../sdk/src/errors.ts
|
|
144
|
+
var ClivlyError = class ClivlyError extends Error {
|
|
145
|
+
/** HTTP status, or null when the request never completed (network error). */
|
|
146
|
+
status;
|
|
147
|
+
reason;
|
|
148
|
+
/** Whether a retry could plausibly succeed (network, 429, 5xx). */
|
|
149
|
+
retryable;
|
|
150
|
+
constructor(result, message) {
|
|
151
|
+
super(message ?? `Clivly connection failed: ${result.reason}`);
|
|
152
|
+
this.name = new.target.name;
|
|
153
|
+
this.status = result.status;
|
|
154
|
+
this.reason = result.reason;
|
|
155
|
+
this.retryable = result.retryable;
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* Build the error subclass matching a failed result's reason, or `null` when
|
|
159
|
+
* the result is `ok`.
|
|
160
|
+
*/
|
|
161
|
+
static from(result) {
|
|
162
|
+
if (result.ok) return null;
|
|
163
|
+
switch (result.reason) {
|
|
164
|
+
case "invalid_api_key":
|
|
165
|
+
case "forbidden": return new ClivlyAuthError(result);
|
|
166
|
+
case "rate_limited": return new ClivlyRateLimitError(result);
|
|
167
|
+
case "network_error": return new ClivlyNetworkError(result);
|
|
168
|
+
default: return new ClivlyError(result);
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
};
|
|
172
|
+
/** The API key was rejected (401) or lacks access (403). Not retryable. */
|
|
173
|
+
var ClivlyAuthError = class extends ClivlyError {};
|
|
174
|
+
/** Rate limited (429). Retryable after backoff / `Retry-After`. */
|
|
175
|
+
var ClivlyRateLimitError = class extends ClivlyError {};
|
|
176
|
+
/** The request never reached the server (network/DNS/TLS). Retryable. */
|
|
177
|
+
var ClivlyNetworkError = class extends ClivlyError {};
|
|
178
|
+
export { createChatSessionHandler as a, ClivlyRateLimitError as i, ClivlyError as n, ClivlyNetworkError as r, ClivlyAuthError as t };
|
package/sdk.cjs
CHANGED
|
@@ -1,16 +1,18 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
2
|
const require_entity_config = require("./entity-config-BP8Cc_nC.cjs");
|
|
3
|
-
const
|
|
4
|
-
|
|
3
|
+
const require_src = require("./src-X0-yIA3O.cjs");
|
|
4
|
+
const require_sdk = require("./sdk-Bzkr24NW.cjs");
|
|
5
|
+
exports.CURSOR_KEY_PREFIX = require_src.CURSOR_KEY_PREFIX;
|
|
5
6
|
exports.ClivlyAuthError = require_sdk.ClivlyAuthError;
|
|
6
7
|
exports.ClivlyError = require_sdk.ClivlyError;
|
|
7
8
|
exports.ClivlyNetworkError = require_sdk.ClivlyNetworkError;
|
|
8
9
|
exports.ClivlyRateLimitError = require_sdk.ClivlyRateLimitError;
|
|
9
10
|
exports.createChatSessionHandler = require_sdk.createChatSessionHandler;
|
|
10
|
-
exports.createClivlySDK =
|
|
11
|
+
exports.createClivlySDK = require_src.createClivlySDK;
|
|
11
12
|
exports.customObjectDeclarations = require_entity_config.customObjectDeclarations;
|
|
12
13
|
exports.defineClivlyConfig = require_entity_config.defineClivlyConfig;
|
|
13
|
-
exports.isPlaceholderUrl =
|
|
14
|
-
exports.kvCursorStore =
|
|
15
|
-
exports.memoryCursorStore =
|
|
14
|
+
exports.isPlaceholderUrl = require_src.isPlaceholderUrl;
|
|
15
|
+
exports.kvCursorStore = require_src.kvCursorStore;
|
|
16
|
+
exports.memoryCursorStore = require_src.memoryCursorStore;
|
|
17
|
+
exports.readProcessEnv = require_src.readProcessEnv;
|
|
16
18
|
exports.validateEntitiesConfig = require_entity_config.validateEntitiesConfig;
|
package/sdk.d.cts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { S as ExposurePolicy, _ as EnsuredMappingResult, a as ClivlyConnectResult, c as ClivlySDK, d as CreateChatSessionHandlerOptions, f as CursorStore, g as EnsureSchemaResult, h as DoctorReport, i as ClivlyConnectReason, l as ClivlySDKConfig, m as DoctorCheck, n as ChatSessionRequestBody, o as ClivlyConsentInput, p as DiscoveredTable, r as ChatWidgetVisitor, s as ClivlyConsentResult, t as AppIdStore, u as ClivlyUser, v as EnsuredObjectTypeResult, x as SyncTriggerDescription, y as RemoteProjectionsDescription } from "./types-UR-tT4c1.cjs";
|
|
2
2
|
import { C as customObjectDeclarations, M as validateEntitiesConfig, a as ClivlyEntitiesConfig, f as CustomObjectDeclaration, l as ConfigValidationError, m as DiscoveredSchemaTable, u as ConfigValidationResult, w as defineClivlyConfig } from "./entity-config-CXpGowQa.cjs";
|
|
3
3
|
export declare function isPlaceholderUrl(value: string): boolean;
|
|
4
4
|
export declare function createChatSessionHandler({ allowedOrigins, apiKey, apiUrl, fetchImpl, resolveUser, resolveVisitorIp }: CreateChatSessionHandlerOptions): (request: Request) => Promise<Response>;
|
|
@@ -53,6 +53,12 @@ export declare const CURSOR_KEY_PREFIX = "clivly:cursor:";
|
|
|
53
53
|
export declare function kvCursorStore(kv: CursorKV, options?: {
|
|
54
54
|
prefix?: string;
|
|
55
55
|
}): CursorStore;
|
|
56
|
+
/**
|
|
57
|
+
* The host's environment, or `{}` where there is no `process` (Deno edge
|
|
58
|
+
* functions, bare workers). Read through `globalThis` so a missing global is
|
|
59
|
+
* `undefined`, not a ReferenceError.
|
|
60
|
+
*/
|
|
61
|
+
export declare function readProcessEnv(): Record<string, string | undefined>;
|
|
56
62
|
/**
|
|
57
63
|
* Create the Clivly SDK. The implemented surface — heartbeat/discovery reporting
|
|
58
64
|
* and the auth-verify handler — connects a developer's app to a running Clivly
|
package/sdk.d.mts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { C as ExposurePolicy, S as SyncTriggerDescription, _ as EnsureSchemaResult, a as ClivlyConnectReason, b as RemoteProjectionsDescription, c as ClivlyConsentResult, d as ClivlyUser, f as CreateChatSessionHandlerOptions, g as DoctorReport, h as DoctorCheck, i as ChatWidgetVisitor, l as ClivlySDK, m as DiscoveredTable, n as AppIdStore, o as ClivlyConnectResult, p as CursorStore, r as ChatSessionRequestBody, s as ClivlyConsentInput, u as ClivlySDKConfig, v as EnsuredMappingResult, y as EnsuredObjectTypeResult } from "./namespace-probe
|
|
1
|
+
import { C as ExposurePolicy, S as SyncTriggerDescription, _ as EnsureSchemaResult, a as ClivlyConnectReason, b as RemoteProjectionsDescription, c as ClivlyConsentResult, d as ClivlyUser, f as CreateChatSessionHandlerOptions, g as DoctorReport, h as DoctorCheck, i as ChatWidgetVisitor, l as ClivlySDK, m as DiscoveredTable, n as AppIdStore, o as ClivlyConnectResult, p as CursorStore, r as ChatSessionRequestBody, s as ClivlyConsentInput, u as ClivlySDKConfig, v as EnsuredMappingResult, y as EnsuredObjectTypeResult } from "./namespace-probe--GvxJ06J.mjs";
|
|
2
2
|
import { C as customObjectDeclarations, M as validateEntitiesConfig, a as ClivlyEntitiesConfig, f as CustomObjectDeclaration, l as ConfigValidationError, m as DiscoveredSchemaTable, u as ConfigValidationResult, w as defineClivlyConfig } from "./entity-config-CXpGowQa.mjs";
|
|
3
3
|
export declare function isPlaceholderUrl(value: string): boolean;
|
|
4
4
|
export declare function createChatSessionHandler({ allowedOrigins, apiKey, apiUrl, fetchImpl, resolveUser, resolveVisitorIp }: CreateChatSessionHandlerOptions): (request: Request) => Promise<Response>;
|
|
@@ -53,6 +53,12 @@ export declare const CURSOR_KEY_PREFIX = "clivly:cursor:";
|
|
|
53
53
|
export declare function kvCursorStore(kv: CursorKV, options?: {
|
|
54
54
|
prefix?: string;
|
|
55
55
|
}): CursorStore;
|
|
56
|
+
/**
|
|
57
|
+
* The host's environment, or `{}` where there is no `process` (Deno edge
|
|
58
|
+
* functions, bare workers). Read through `globalThis` so a missing global is
|
|
59
|
+
* `undefined`, not a ReferenceError.
|
|
60
|
+
*/
|
|
61
|
+
export declare function readProcessEnv(): Record<string, string | undefined>;
|
|
56
62
|
/**
|
|
57
63
|
* Create the Clivly SDK. The implemented surface — heartbeat/discovery reporting
|
|
58
64
|
* and the auth-verify handler — connects a developer's app to a running Clivly
|
package/sdk.mjs
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
1
|
import { c as defineClivlyConfig, g as validateEntitiesConfig, s as customObjectDeclarations } from "./entity-config-CI5CGfaN.mjs";
|
|
2
|
-
import { a as
|
|
3
|
-
|
|
2
|
+
import { a as readProcessEnv, i as memoryCursorStore, n as createClivlySDK, o as isPlaceholderUrl, r as kvCursorStore, t as CURSOR_KEY_PREFIX } from "./src-Dq2nY1Sj.mjs";
|
|
3
|
+
import { a as createChatSessionHandler, i as ClivlyRateLimitError, n as ClivlyError, r as ClivlyNetworkError, t as ClivlyAuthError } from "./sdk-D2WwGBzk.mjs";
|
|
4
|
+
export { CURSOR_KEY_PREFIX, ClivlyAuthError, ClivlyError, ClivlyNetworkError, ClivlyRateLimitError, createChatSessionHandler, createClivlySDK, customObjectDeclarations, defineClivlyConfig, isPlaceholderUrl, kvCursorStore, memoryCursorStore, readProcessEnv, validateEntitiesConfig };
|
package/sql.cjs
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
const require_introspector = require("./introspector-BwYgHGcS.cjs");
|
|
3
|
+
const require_discover = require("./discover-BZIPXl23.cjs");
|
|
4
|
+
const require_executor = require("./executor-CU-jRgpC.cjs");
|
|
5
|
+
exports.connectPostgresJs = require_executor.connectPostgresJs;
|
|
6
|
+
exports.discoverFromSql = require_discover.discoverFromSql;
|
|
7
|
+
exports.existsFromSql = require_introspector.existsFromSql;
|
|
8
|
+
exports.fromSql = require_introspector.fromSql;
|
|
9
|
+
exports.fromSqlProjection = require_introspector.fromSqlProjection;
|
|
10
|
+
exports.pgExecutor = require_executor.pgExecutor;
|
|
11
|
+
exports.postgresJsExecutor = require_executor.postgresJsExecutor;
|
|
12
|
+
exports.sqlAppIdStore = require_introspector.sqlAppIdStore;
|
|
13
|
+
exports.sqlIntrospector = require_introspector.sqlIntrospector;
|
package/sql.d.cts
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import { a as discoverFromSql, c as connectPostgresJs, i as fromSql, l as pgExecutor, n as sqlIntrospector, o as sqlAppIdStore, r as existsFromSql, s as SqlExecutor, t as fromSqlProjection, u as postgresJsExecutor } from "./index-CZkp8TV4.cjs";
|
|
2
|
+
export { type SqlExecutor, connectPostgresJs, discoverFromSql, existsFromSql, fromSql, fromSqlProjection, pgExecutor, postgresJsExecutor, sqlAppIdStore, sqlIntrospector };
|
package/sql.d.mts
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import { a as discoverFromSql, c as connectPostgresJs, i as fromSql, l as pgExecutor, n as sqlIntrospector, o as sqlAppIdStore, r as existsFromSql, s as SqlExecutor, t as fromSqlProjection, u as postgresJsExecutor } from "./index-Cnrz4ywf.mjs";
|
|
2
|
+
export { type SqlExecutor, connectPostgresJs, discoverFromSql, existsFromSql, fromSql, fromSqlProjection, pgExecutor, postgresJsExecutor, sqlAppIdStore, sqlIntrospector };
|
package/sql.mjs
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import { a as sqlAppIdStore, i as fromSql, n as fromSqlProjection, r as existsFromSql, t as sqlIntrospector } from "./introspector-Bz52xOUr.mjs";
|
|
2
|
+
import { t as discoverFromSql } from "./discover-CFIi43Qn.mjs";
|
|
3
|
+
import { n as pgExecutor, r as postgresJsExecutor, t as connectPostgresJs } from "./executor-CJEWurjZ.mjs";
|
|
4
|
+
export { connectPostgresJs, discoverFromSql, existsFromSql, fromSql, fromSqlProjection, pgExecutor, postgresJsExecutor, sqlAppIdStore, sqlIntrospector };
|