@pagelines/sdk 1.0.682 → 1.0.683
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/AgentProvider.js +701 -0
- package/dist/AgentProvider.js.map +1 -0
- package/dist/AgentWidgetInline.js +44 -32
- package/dist/AgentWidgetInline.js.map +1 -1
- package/dist/AgentWidgetModal.js +75 -64
- package/dist/AgentWidgetModal.js.map +1 -1
- package/dist/AgentWrap.js +5180 -0
- package/dist/AgentWrap.js.map +1 -0
- package/dist/FModal.js +165 -0
- package/dist/FModal.js.map +1 -0
- package/dist/agent/ui/AgentChat.vue.d.ts +3 -4
- package/dist/agent/ui/AgentInputEmail.vue.d.ts +3 -2
- package/dist/agent/ui/AgentInputOneTimeCode.vue.d.ts +3 -4
- package/dist/agent/ui/AgentModal.vue.d.ts +2 -1
- package/dist/agent/ui/AgentProvider.vue.d.ts +2 -1
- package/dist/agent/ui/AgentSidebarClose.vue.d.ts +3 -2
- package/dist/agent/ui/AgentToolActivityGroup.vue.d.ts +2 -1
- package/dist/agent/ui/AgentWidget.vue.d.ts +3 -2
- package/dist/agent/ui/AgentWrap.vue.d.ts +14 -29
- package/dist/agent/ui/ChatRichText.vue.d.ts +2 -1
- package/dist/agent/ui/ChatScroller.vue.d.ts +8 -18
- package/dist/agent/ui/ChatVisualBlock.vue.d.ts +2 -3
- package/dist/agent/ui/ElAgentAbout.vue.d.ts +2 -1
- package/dist/agent/ui/ElAgentButton.vue.d.ts +7 -11
- package/dist/agent/ui/ElAgentChat.vue.d.ts +2 -53
- package/dist/agent/ui/ElAgentChatPreview.vue.d.ts +2 -1
- package/dist/agent/ui/ElAgentHeader.vue.d.ts +2 -1
- package/dist/agent/ui/ElAgentModeSidebar.vue.d.ts +2 -1
- package/dist/agent/ui/ElAgentSidebar.vue.d.ts +2 -1
- package/dist/agent/ui/ElAuthGate.vue.d.ts +2 -1
- package/dist/agent/ui/ElAuthPanel.vue.d.ts +2 -1
- package/dist/agent/ui/ElCreateAgent.vue.d.ts +3 -2
- package/dist/agent/ui/ElModeHeader.vue.d.ts +2 -1
- package/dist/agent/ui/ElProvisioningStatus.vue.d.ts +3 -2
- package/dist/agent/ui/ElSidebar.vue.d.ts +9 -12
- package/dist/agent/ui/ElTrialGate.vue.d.ts +2 -1
- package/dist/agent.js +157 -186
- package/dist/agent.js.map +1 -1
- package/dist/constants.js +80 -0
- package/dist/constants.js.map +1 -0
- package/dist/contract.js +6284 -0
- package/dist/contract.js.map +1 -0
- package/dist/demo.js +242 -259
- package/dist/demo.js.map +1 -1
- package/dist/rolldown-runtime.js +14 -0
- package/dist/sdk.css +2 -1
- package/dist/sdk.js +4 -14
- package/dist/sdkClient.js +775 -867
- package/dist/sdkClient.js.map +1 -1
- package/dist/ui/EffectTransitionList.vue.d.ts +7 -11
- package/dist/ui/FClose.vue.d.ts +3 -2
- package/dist/ui/FModal.vue.d.ts +7 -5
- package/dist/ui/FSpinner.vue.d.ts +3 -2
- package/dist/useWidgetState.js +21 -0
- package/dist/useWidgetState.js.map +1 -0
- package/dist/widget/ui/AgentWidgetInline.vue.d.ts +2 -1
- package/dist/widget/ui/AgentWidgetModal.vue.d.ts +2 -1
- package/dist/widget/ui/AgentWidgetOnboard.vue.d.ts +2 -1
- package/dist/widget/ui/AgentWidgetPopup.vue.d.ts +3 -2
- package/dist/widget.js +717 -7
- package/dist/widget.js.map +1 -1
- package/package.json +25 -25
- package/dist/AgentProvider.vue_vue_type_script_setup_true_lang.js +0 -842
- package/dist/AgentProvider.vue_vue_type_script_setup_true_lang.js.map +0 -1
- package/dist/AgentWidgetOnboard.js +0 -5
- package/dist/AgentWidgetOnboard.js.map +0 -1
- package/dist/AgentWrap.vue_vue_type_script_setup_true_lang.js +0 -4952
- package/dist/AgentWrap.vue_vue_type_script_setup_true_lang.js.map +0 -1
- package/dist/FModal.vue_vue_type_script_setup_true_lang.js +0 -181
- package/dist/FModal.vue_vue_type_script_setup_true_lang.js.map +0 -1
- package/dist/agent/test/AgentController.test.d.ts +0 -1
- package/dist/agent/test/AgentToolActivityGroup.client.test.d.ts +0 -1
- package/dist/agent/test/ChatRichText.client.test.d.ts +0 -1
- package/dist/agent/test/ChatVisualBlock.client.test.d.ts +0 -1
- package/dist/agent/test/ElAgentChat.client.test.d.ts +0 -1
- package/dist/agent/test/ElAgentChatPreview.client.test.d.ts +0 -1
- package/dist/agent/test/VoiceRecorderController.unit.test.d.ts +0 -1
- package/dist/agent/test/auth-inputs.test.d.ts +0 -1
- package/dist/agent/test/chat-visual-format.unit.test.d.ts +0 -1
- package/dist/agent/test/utils.test.d.ts +0 -1
- package/dist/agent/test/webhook.test.d.ts +0 -1
- package/dist/contract/regenerate.d.ts +0 -2
- package/dist/contract/test/build.unit.test.d.ts +0 -1
- package/dist/contract/test/conversation-wire-shapes.unit.test.d.ts +0 -14
- package/dist/contract/test/snapshot.unit.test.d.ts +0 -1
- package/dist/index.js +0 -871
- package/dist/index.js.map +0 -1
- package/dist/sdk.js.map +0 -1
- package/dist/socialPlatforms.js +0 -82
- package/dist/socialPlatforms.js.map +0 -1
- package/dist/test/agent-client.test.d.ts +0 -4
- package/dist/test/api.test.d.ts +0 -1
- package/dist/test/billing-client.test.d.ts +0 -4
- package/dist/test/build.test.d.ts +0 -1
- package/dist/test/chat-authenticated.test.d.ts +0 -1
- package/dist/test/chat-issue-presentation.test.d.ts +0 -1
- package/dist/test/derive-mode.test.d.ts +0 -1
- package/dist/test/sdk-token-capture.test.d.ts +0 -13
- package/dist/vite.config.sdk.d.ts +0 -2
- package/dist/vitest.config.d.ts +0 -2
package/dist/sdkClient.js
CHANGED
|
@@ -1,879 +1,787 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
function p(i, e) {
|
|
8
|
-
return i || (e ? `http://localhost:${te}` : oe.app);
|
|
1
|
+
import "./rolldown-runtime.js";
|
|
2
|
+
import { APP_PORT as e, PAGELINES_URLS as t, SettingsObject as n, cookieUtil as r, createLogger as i, objectId as a, runAuthenticatedChatTurnStream as o, shortId as s, zChatEvent as c } from "@pagelines/core";
|
|
3
|
+
import { computed as l, ref as u, watch as d } from "vue";
|
|
4
|
+
//#region api.ts
|
|
5
|
+
function resolveApiBase(n, r) {
|
|
6
|
+
return n || (r ? `http://localhost:${e}` : t.app);
|
|
9
7
|
}
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
return c;
|
|
30
|
-
};
|
|
8
|
+
function createApiFetch(e, t, n) {
|
|
9
|
+
let i = resolveApiBase(e.apiBase, e.isDev);
|
|
10
|
+
return async (e, a) => {
|
|
11
|
+
let o = r.getAuthToken(), s = new Headers(a?.headers);
|
|
12
|
+
s.has("Content-Type") || s.set("Content-Type", "application/json"), o && !s.has("Authorization") && s.set("Authorization", `Bearer ${o}`);
|
|
13
|
+
let c = typeof e == "string" && e.startsWith("/") ? `${i}${e}` : e, l = await fetch(c, {
|
|
14
|
+
...a,
|
|
15
|
+
headers: s,
|
|
16
|
+
credentials: "include"
|
|
17
|
+
});
|
|
18
|
+
if (n && l.status === 401 && (await l.clone().json().catch(() => void 0))?.code === "TOKEN_ERROR" && n(), t) {
|
|
19
|
+
let e = l.json.bind(l);
|
|
20
|
+
l.json = async () => {
|
|
21
|
+
let n = await e();
|
|
22
|
+
return typeof n == "object" && n && "ok" in n && ("user" in n || "token" in n) && t(n), n;
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
return l;
|
|
26
|
+
};
|
|
31
27
|
}
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
let r = t.value;
|
|
72
|
-
r !== o && (t.value = o, t.notify(r));
|
|
73
|
-
},
|
|
74
|
-
subscribe(o) {
|
|
75
|
-
let r = t.listen(o);
|
|
76
|
-
return o(t.value), r;
|
|
77
|
-
},
|
|
78
|
-
value: i
|
|
79
|
-
};
|
|
80
|
-
return process.env.NODE_ENV !== "production" && (t[le] = () => {
|
|
81
|
-
e = [], t.lc = 0, t.off();
|
|
82
|
-
}), t;
|
|
83
|
-
}, "atom"), y = globalThis, E = y.__PL_AUTH_USER__ ?? (y.__PL_AUTH_USER__ = /* @__PURE__ */ _(void 0)), U = y.__PL_AUTH_TOKEN__ ?? (y.__PL_AUTH_TOKEN__ = /* @__PURE__ */ _(null)), A = y.__PL_AUTH_DEVICE_ID__ ?? (y.__PL_AUTH_DEVICE_ID__ = /* @__PURE__ */ _(null)), z = class z {
|
|
84
|
-
constructor() {
|
|
85
|
-
l(this, "logger", re("SDKStorage"));
|
|
86
|
-
}
|
|
87
|
-
/**
|
|
88
|
-
* Check if localStorage is fully functional (some test environments provide partial mocks)
|
|
89
|
-
*/
|
|
90
|
-
isLocalStorageFunctional() {
|
|
91
|
-
return typeof window < "u" && typeof localStorage?.getItem == "function" && typeof localStorage?.setItem == "function" && typeof localStorage?.removeItem == "function";
|
|
92
|
-
}
|
|
93
|
-
/**
|
|
94
|
-
* Sync Vue reactive refs with global nanostores for cross-bundle persistence.
|
|
95
|
-
*/
|
|
96
|
-
syncWithGlobalStores(e) {
|
|
97
|
-
const { activeUser: t, token: o, deviceId: r } = e, s = E.get(), n = U.get(), h = A.get();
|
|
98
|
-
s && (t.value = s), n && (o.value = n), h && (r.value = h), B(t, (a) => E.set(a), { immediate: !0 }), B(o, (a) => U.set(a), { immediate: !0 }), B(r, (a) => A.set(a), { immediate: !0 }), E.subscribe((a) => {
|
|
99
|
-
a !== t.value && (t.value = a);
|
|
100
|
-
}), U.subscribe((a) => {
|
|
101
|
-
a !== o.value && (o.value = a);
|
|
102
|
-
}), A.subscribe((a) => {
|
|
103
|
-
a !== r.value && (r.value = a);
|
|
104
|
-
});
|
|
105
|
-
}
|
|
106
|
-
/**
|
|
107
|
-
* Load user and token from browser storage on initialization.
|
|
108
|
-
* deviceId comes from a server-set non-HttpOnly cookie (read-only for JS).
|
|
109
|
-
*/
|
|
110
|
-
loadFromStorage(e) {
|
|
111
|
-
if (typeof window > "u")
|
|
112
|
-
return;
|
|
113
|
-
const t = S.getAuthToken();
|
|
114
|
-
t && (e.token.value = t);
|
|
115
|
-
const o = S.getDeviceId();
|
|
116
|
-
if (o && (e.deviceId.value = o), this.isLocalStorageFunctional())
|
|
117
|
-
try {
|
|
118
|
-
const r = localStorage.getItem("pagelines-user");
|
|
119
|
-
r && (e.activeUser.value = JSON.parse(r));
|
|
120
|
-
} catch (r) {
|
|
121
|
-
this.logger.error("Failed to load user from localStorage", { data: r });
|
|
122
|
-
}
|
|
123
|
-
}
|
|
124
|
-
/**
|
|
125
|
-
* Save current user and the transitional JS session-token mirror to browser storage.
|
|
126
|
-
* deviceId is server-owned (Set-Cookie) — JS doesn't write it.
|
|
127
|
-
*/
|
|
128
|
-
saveToStorage(e) {
|
|
129
|
-
if (!(typeof window > "u") && (e.token.value ? S.setAuthToken(e.token.value) : S.removeAuthToken(), this.isLocalStorageFunctional()))
|
|
130
|
-
try {
|
|
131
|
-
e.activeUser.value ? localStorage.setItem("pagelines-user", JSON.stringify(e.activeUser.value)) : localStorage.removeItem("pagelines-user");
|
|
132
|
-
} catch (t) {
|
|
133
|
-
this.logger.error("Failed to save user to localStorage", { data: t });
|
|
134
|
-
}
|
|
135
|
-
}
|
|
136
|
-
/**
|
|
137
|
-
* Clear local stored user data and JS-owned tokens. Server-owned cookies
|
|
138
|
-
* (pl-session, pl-device-id) are cleared by /api/auth/logout via
|
|
139
|
-
* Set-Cookie max-age=0 — JS can't touch them.
|
|
140
|
-
*/
|
|
141
|
-
clearStorage() {
|
|
142
|
-
if (!(typeof window > "u")) {
|
|
143
|
-
if (S.removeAuthToken(), this.isLocalStorageFunctional())
|
|
144
|
-
try {
|
|
145
|
-
localStorage.removeItem("pagelines-user");
|
|
146
|
-
} catch (e) {
|
|
147
|
-
this.logger.error("Failed to clear user from localStorage", { data: e });
|
|
148
|
-
}
|
|
149
|
-
E.set(void 0), U.set(null), A.set(null);
|
|
150
|
-
}
|
|
151
|
-
}
|
|
152
|
-
/**
|
|
153
|
-
* Generic localStorage getter
|
|
154
|
-
*/
|
|
155
|
-
getItem(e) {
|
|
156
|
-
if (!this.isLocalStorageFunctional())
|
|
157
|
-
return null;
|
|
158
|
-
try {
|
|
159
|
-
return localStorage.getItem(e);
|
|
160
|
-
} catch (t) {
|
|
161
|
-
return this.logger.error("Failed to get item from localStorage", { key: e, error: t }), null;
|
|
162
|
-
}
|
|
163
|
-
}
|
|
164
|
-
/**
|
|
165
|
-
* Generic localStorage setter
|
|
166
|
-
*/
|
|
167
|
-
setItem(e, t) {
|
|
168
|
-
if (this.isLocalStorageFunctional())
|
|
169
|
-
try {
|
|
170
|
-
localStorage.setItem(e, t);
|
|
171
|
-
} catch (o) {
|
|
172
|
-
this.logger.error("Failed to set item in localStorage", { key: e, error: o });
|
|
173
|
-
}
|
|
174
|
-
}
|
|
28
|
+
//#endregion
|
|
29
|
+
//#region ../../node_modules/.pnpm/nanostores@1.4.0/node_modules/nanostores/clean-stores/index.js
|
|
30
|
+
var f = Symbol("clean"), p, m = [], h = 0, g = null, _ = 4, v = (p = globalThis).nanostoresGlobal || (p.nanostoresGlobal = { epoch: 0 }), drainQueue = () => {
|
|
31
|
+
for (h = 0; h < m.length; h += _) m[h](m[h + 1].value, m[h + 2], m[h + 3]);
|
|
32
|
+
m.length = 0;
|
|
33
|
+
}, atom = /* @__NO_SIDE_EFFECTS__ */ (e) => {
|
|
34
|
+
let t = [], n = {
|
|
35
|
+
get() {
|
|
36
|
+
return n.lc || n.listen(() => {})(), n.value;
|
|
37
|
+
},
|
|
38
|
+
init: e,
|
|
39
|
+
lc: 0,
|
|
40
|
+
listen(e) {
|
|
41
|
+
return n.lc = t.push(e), () => {
|
|
42
|
+
for (let t = h + _; t < m.length;) m[t] === e ? m.splice(t, _) : t += _;
|
|
43
|
+
let r = t.indexOf(e);
|
|
44
|
+
~r && (t.splice(r, 1), --n.lc || n.off());
|
|
45
|
+
};
|
|
46
|
+
},
|
|
47
|
+
notify(e, r) {
|
|
48
|
+
v.epoch++;
|
|
49
|
+
let i = !m.length && !0;
|
|
50
|
+
for (let i of t) g?.has(i) || (g?.add(i), m.push(i, n, e, r));
|
|
51
|
+
i && drainQueue();
|
|
52
|
+
},
|
|
53
|
+
off() {},
|
|
54
|
+
set(e) {
|
|
55
|
+
let t = n.value;
|
|
56
|
+
t !== e && (n.value = e, n.notify(t));
|
|
57
|
+
},
|
|
58
|
+
subscribe(e) {
|
|
59
|
+
let t = n.listen(e);
|
|
60
|
+
return e(n.value), t;
|
|
61
|
+
},
|
|
62
|
+
value: e
|
|
63
|
+
};
|
|
64
|
+
return process.env.NODE_ENV !== "production" && (n[f] = () => {
|
|
65
|
+
t = [], n.lc = 0, n.off();
|
|
66
|
+
}), n;
|
|
175
67
|
};
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
*/
|
|
204
|
-
async getLiveStatus(e) {
|
|
205
|
-
const t = p(this.ctx.apiBase, this.ctx.isDev);
|
|
206
|
-
try {
|
|
207
|
-
const r = await (await fetch(`${t}/api/agents/state?ids=${encodeURIComponent(e.agentId)}`, {
|
|
208
|
-
headers: {
|
|
209
|
-
...this.ctx.token.value && { Authorization: `Bearer ${this.ctx.token.value}` }
|
|
210
|
-
}
|
|
211
|
-
})).json();
|
|
212
|
-
return r.ok ? r.data?.[e.agentId] ?? { lifecycle: "unknown" } : { lifecycle: "unknown" };
|
|
213
|
-
} catch (o) {
|
|
214
|
-
return this.ctx.logger.warn("agent.getLiveStatus failed", {
|
|
215
|
-
agentId: e.agentId,
|
|
216
|
-
error: o instanceof Error ? o.message : String(o)
|
|
217
|
-
}), { lifecycle: "unknown" };
|
|
218
|
-
}
|
|
219
|
-
}
|
|
220
|
-
/**
|
|
221
|
-
* Poll until the agent's lifecycle reaches `running`. `onStatus` receives
|
|
222
|
-
* the server-reported lifecycle verbatim ('starting', 'running', …) so the
|
|
223
|
-
* caller renders feedback tied to actual provisioner state, not a rotating
|
|
224
|
-
* set of decorative messages. Per first-principles → "Always give feedback
|
|
225
|
-
* — no magic". Map lifecycle values to user copy at the call site.
|
|
226
|
-
*/
|
|
227
|
-
async waitUntilReady(e) {
|
|
228
|
-
const { agentId: t, onStatus: o, timeoutMs: r = 3e5, pollIntervalMs: s = 3e3 } = e, n = Date.now();
|
|
229
|
-
let h;
|
|
230
|
-
for (; Date.now() - n < r; ) {
|
|
231
|
-
const a = await this.getLiveStatus({ agentId: t });
|
|
232
|
-
if (a.lifecycle === "running") {
|
|
233
|
-
o?.("running");
|
|
234
|
-
return;
|
|
235
|
-
}
|
|
236
|
-
if (a.lifecycle === "error")
|
|
237
|
-
throw new Error(a.detail || "Your assistant hit a problem starting");
|
|
238
|
-
a.lifecycle !== h && (o?.(a.lifecycle), h = a.lifecycle), await new Promise((c) => setTimeout(c, s));
|
|
239
|
-
}
|
|
240
|
-
throw new Error("Your assistant is taking longer than expected — please try again");
|
|
241
|
-
}
|
|
242
|
-
/** Send a typed webhook event to a bot agent. No auth required — the proxy is public. */
|
|
243
|
-
async webhook(e) {
|
|
244
|
-
const o = `${p(this.ctx.apiBase, this.ctx.isDev)}/api/bot-api/webhooks/agents/${e.agentId}`, r = { type: e.type, data: e.data };
|
|
245
|
-
e.meta && (r.meta = e.meta);
|
|
246
|
-
const s = await fetch(o, {
|
|
247
|
-
method: "POST",
|
|
248
|
-
headers: { "Content-Type": "application/json" },
|
|
249
|
-
body: JSON.stringify(r)
|
|
250
|
-
});
|
|
251
|
-
if (!s.ok) {
|
|
252
|
-
const n = await s.text().catch(() => "Unknown error");
|
|
253
|
-
throw new Error(`Webhook delivery failed (${s.status}): ${n.slice(0, 200)}`);
|
|
254
|
-
}
|
|
255
|
-
return s.json();
|
|
256
|
-
}
|
|
257
|
-
resolveOrgId() {
|
|
258
|
-
const t = this.ctx.activeUser.value?.orgs?.[0]?.orgId;
|
|
259
|
-
if (!t)
|
|
260
|
-
throw new Error("No organization found. Please sign in first.");
|
|
261
|
-
return t;
|
|
262
|
-
}
|
|
263
|
-
};
|
|
264
|
-
u(J, "AgentClient");
|
|
265
|
-
let F = J;
|
|
266
|
-
async function k(i, e) {
|
|
267
|
-
i.loading.value = !0, i.error.value = null;
|
|
268
|
-
try {
|
|
269
|
-
return await e();
|
|
270
|
-
} finally {
|
|
271
|
-
i.loading.value = !1;
|
|
272
|
-
}
|
|
68
|
+
//#endregion
|
|
69
|
+
//#region \0@oxc-project+runtime@0.139.0/helpers/esm/typeof.js
|
|
70
|
+
function _typeof(e) {
|
|
71
|
+
"@babel/helpers - typeof";
|
|
72
|
+
return _typeof = typeof Symbol == "function" && typeof Symbol.iterator == "symbol" ? function(e) {
|
|
73
|
+
return typeof e;
|
|
74
|
+
} : function(e) {
|
|
75
|
+
return e && typeof Symbol == "function" && e.constructor === Symbol && e !== Symbol.prototype ? "symbol" : typeof e;
|
|
76
|
+
}, _typeof(e);
|
|
77
|
+
}
|
|
78
|
+
//#endregion
|
|
79
|
+
//#region \0@oxc-project+runtime@0.139.0/helpers/esm/toPrimitive.js
|
|
80
|
+
function toPrimitive(e, t) {
|
|
81
|
+
if (_typeof(e) != "object" || !e) return e;
|
|
82
|
+
var n = e[Symbol.toPrimitive];
|
|
83
|
+
if (n !== void 0) {
|
|
84
|
+
var r = n.call(e, t || "default");
|
|
85
|
+
if (_typeof(r) != "object") return r;
|
|
86
|
+
throw TypeError("@@toPrimitive must return a primitive value.");
|
|
87
|
+
}
|
|
88
|
+
return (t === "string" ? String : Number)(e);
|
|
89
|
+
}
|
|
90
|
+
//#endregion
|
|
91
|
+
//#region \0@oxc-project+runtime@0.139.0/helpers/esm/toPropertyKey.js
|
|
92
|
+
function toPropertyKey(e) {
|
|
93
|
+
var t = toPrimitive(e, "string");
|
|
94
|
+
return _typeof(t) == "symbol" ? t : t + "";
|
|
273
95
|
}
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
96
|
+
//#endregion
|
|
97
|
+
//#region \0@oxc-project+runtime@0.139.0/helpers/esm/defineProperty.js
|
|
98
|
+
function _defineProperty(e, t, n) {
|
|
99
|
+
return (t = toPropertyKey(t)) in e ? Object.defineProperty(e, t, {
|
|
100
|
+
value: n,
|
|
101
|
+
enumerable: !0,
|
|
102
|
+
configurable: !0,
|
|
103
|
+
writable: !0
|
|
104
|
+
}) : e[t] = n, e;
|
|
105
|
+
}
|
|
106
|
+
//#endregion
|
|
107
|
+
//#region sdkStorage.ts
|
|
108
|
+
var y = globalThis, b = y.__PL_AUTH_USER__ ?? (y.__PL_AUTH_USER__ = /* @__PURE__ */ atom(void 0)), x = y.__PL_AUTH_TOKEN__ ?? (y.__PL_AUTH_TOKEN__ = /* @__PURE__ */ atom(null)), S = y.__PL_AUTH_DEVICE_ID__ ?? (y.__PL_AUTH_DEVICE_ID__ = /* @__PURE__ */ atom(null)), SDKStorage = class {
|
|
109
|
+
constructor() {
|
|
110
|
+
_defineProperty(this, "logger", i("SDKStorage"));
|
|
111
|
+
}
|
|
112
|
+
isLocalStorageFunctional() {
|
|
113
|
+
return typeof window < "u" && typeof localStorage?.getItem == "function" && typeof localStorage?.setItem == "function" && typeof localStorage?.removeItem == "function";
|
|
114
|
+
}
|
|
115
|
+
syncWithGlobalStores(e) {
|
|
116
|
+
let { activeUser: t, token: n, deviceId: r } = e, i = b.get(), a = x.get(), o = S.get();
|
|
117
|
+
i && (t.value = i), a && (n.value = a), o && (r.value = o), d(t, (e) => b.set(e), { immediate: !0 }), d(n, (e) => x.set(e), { immediate: !0 }), d(r, (e) => S.set(e), { immediate: !0 }), b.subscribe((e) => {
|
|
118
|
+
e !== t.value && (t.value = e);
|
|
119
|
+
}), x.subscribe((e) => {
|
|
120
|
+
e !== n.value && (n.value = e);
|
|
121
|
+
}), S.subscribe((e) => {
|
|
122
|
+
e !== r.value && (r.value = e);
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
loadFromStorage(e) {
|
|
126
|
+
if (typeof window > "u") return;
|
|
127
|
+
let t = r.getAuthToken();
|
|
128
|
+
t && (e.token.value = t);
|
|
129
|
+
let n = r.getDeviceId();
|
|
130
|
+
if (n && (e.deviceId.value = n), this.isLocalStorageFunctional()) try {
|
|
131
|
+
let t = localStorage.getItem("pagelines-user");
|
|
132
|
+
t && (e.activeUser.value = JSON.parse(t));
|
|
133
|
+
} catch (e) {
|
|
134
|
+
this.logger.error("Failed to load user from localStorage", { data: e });
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
saveToStorage(e) {
|
|
138
|
+
if (!(typeof window > "u") && (e.token.value ? r.setAuthToken(e.token.value) : r.removeAuthToken(), this.isLocalStorageFunctional())) try {
|
|
139
|
+
e.activeUser.value ? localStorage.setItem("pagelines-user", JSON.stringify(e.activeUser.value)) : localStorage.removeItem("pagelines-user");
|
|
140
|
+
} catch (e) {
|
|
141
|
+
this.logger.error("Failed to save user to localStorage", { data: e });
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
clearStorage() {
|
|
145
|
+
if (!(typeof window > "u")) {
|
|
146
|
+
if (r.removeAuthToken(), this.isLocalStorageFunctional()) try {
|
|
147
|
+
localStorage.removeItem("pagelines-user");
|
|
148
|
+
} catch (e) {
|
|
149
|
+
this.logger.error("Failed to clear user from localStorage", { data: e });
|
|
150
|
+
}
|
|
151
|
+
b.set(void 0), x.set(null), S.set(null);
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
getItem(e) {
|
|
155
|
+
if (!this.isLocalStorageFunctional()) return null;
|
|
156
|
+
try {
|
|
157
|
+
return localStorage.getItem(e);
|
|
158
|
+
} catch (t) {
|
|
159
|
+
return this.logger.error("Failed to get item from localStorage", {
|
|
160
|
+
key: e,
|
|
161
|
+
error: t
|
|
162
|
+
}), null;
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
setItem(e, t) {
|
|
166
|
+
if (this.isLocalStorageFunctional()) try {
|
|
167
|
+
localStorage.setItem(e, t);
|
|
168
|
+
} catch (t) {
|
|
169
|
+
this.logger.error("Failed to set item in localStorage", {
|
|
170
|
+
key: e,
|
|
171
|
+
error: t
|
|
172
|
+
});
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
}, AgentClient = class {
|
|
176
|
+
constructor(e) {
|
|
177
|
+
_defineProperty(this, "ctx", void 0), this.ctx = e;
|
|
178
|
+
}
|
|
179
|
+
async create(e) {
|
|
180
|
+
let t = e.orgId || this.resolveOrgId(), n = resolveApiBase(this.ctx.apiBase, this.ctx.isDev), r = await (await fetch(`${n}/api/agents`, {
|
|
181
|
+
method: "POST",
|
|
182
|
+
headers: {
|
|
183
|
+
"Content-Type": "application/json",
|
|
184
|
+
...this.ctx.token.value && { Authorization: `Bearer ${this.ctx.token.value}` }
|
|
185
|
+
},
|
|
186
|
+
body: JSON.stringify({
|
|
187
|
+
name: e.name,
|
|
188
|
+
orgId: t
|
|
189
|
+
})
|
|
190
|
+
})).json();
|
|
191
|
+
if (!r.ok || !r.data?.[0]) {
|
|
192
|
+
let e = r.error || "Failed to create agent";
|
|
193
|
+
throw this.ctx.error.value = e, Error(e);
|
|
194
|
+
}
|
|
195
|
+
return this.ctx.processApiResponse(r), r.data[0];
|
|
196
|
+
}
|
|
197
|
+
async getLiveStatus(e) {
|
|
198
|
+
let t = resolveApiBase(this.ctx.apiBase, this.ctx.isDev);
|
|
199
|
+
try {
|
|
200
|
+
let n = await (await fetch(`${t}/api/agents/state?ids=${encodeURIComponent(e.agentId)}`, { headers: { ...this.ctx.token.value && { Authorization: `Bearer ${this.ctx.token.value}` } } })).json();
|
|
201
|
+
return n.ok ? n.data?.[e.agentId] ?? { lifecycle: "unknown" } : { lifecycle: "unknown" };
|
|
202
|
+
} catch (t) {
|
|
203
|
+
return this.ctx.logger.warn("agent.getLiveStatus failed", {
|
|
204
|
+
agentId: e.agentId,
|
|
205
|
+
error: t instanceof Error ? t.message : String(t)
|
|
206
|
+
}), { lifecycle: "unknown" };
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
async waitUntilReady(e) {
|
|
210
|
+
let { agentId: t, onStatus: n, timeoutMs: r = 3e5, pollIntervalMs: i = 3e3 } = e, a = Date.now(), o;
|
|
211
|
+
for (; Date.now() - a < r;) {
|
|
212
|
+
let e = await this.getLiveStatus({ agentId: t });
|
|
213
|
+
if (e.lifecycle === "running") {
|
|
214
|
+
n?.("running");
|
|
215
|
+
return;
|
|
216
|
+
}
|
|
217
|
+
if (e.lifecycle === "error") throw Error(e.detail || "Your assistant hit a problem starting");
|
|
218
|
+
e.lifecycle !== o && (n?.(e.lifecycle), o = e.lifecycle), await new Promise((e) => setTimeout(e, i));
|
|
219
|
+
}
|
|
220
|
+
throw Error("Your assistant is taking longer than expected — please try again");
|
|
221
|
+
}
|
|
222
|
+
async webhook(e) {
|
|
223
|
+
let t = `${resolveApiBase(this.ctx.apiBase, this.ctx.isDev)}/api/bot-api/webhooks/agents/${e.agentId}`, n = {
|
|
224
|
+
type: e.type,
|
|
225
|
+
data: e.data
|
|
226
|
+
};
|
|
227
|
+
e.meta && (n.meta = e.meta);
|
|
228
|
+
let r = await fetch(t, {
|
|
229
|
+
method: "POST",
|
|
230
|
+
headers: { "Content-Type": "application/json" },
|
|
231
|
+
body: JSON.stringify(n)
|
|
232
|
+
});
|
|
233
|
+
if (!r.ok) {
|
|
234
|
+
let e = await r.text().catch(() => "Unknown error");
|
|
235
|
+
throw Error(`Webhook delivery failed (${r.status}): ${e.slice(0, 200)}`);
|
|
236
|
+
}
|
|
237
|
+
return r.json();
|
|
238
|
+
}
|
|
239
|
+
resolveOrgId() {
|
|
240
|
+
let e = this.ctx.activeUser.value?.orgs?.[0]?.orgId;
|
|
241
|
+
if (!e) throw Error("No organization found. Please sign in first.");
|
|
242
|
+
return e;
|
|
243
|
+
}
|
|
400
244
|
};
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
245
|
+
//#endregion
|
|
246
|
+
//#region clients/AuthClient.ts
|
|
247
|
+
async function withLoadingState(e, t) {
|
|
248
|
+
e.loading.value = !0, e.error.value = null;
|
|
249
|
+
try {
|
|
250
|
+
return await t();
|
|
251
|
+
} finally {
|
|
252
|
+
e.loading.value = !1;
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
var AuthClient = class {
|
|
256
|
+
constructor(e) {
|
|
257
|
+
_defineProperty(this, "ctx", void 0), this.ctx = e;
|
|
258
|
+
}
|
|
259
|
+
async sendCode(e) {
|
|
260
|
+
return withLoadingState(this.ctx, async () => {
|
|
261
|
+
try {
|
|
262
|
+
let t = await (await this.ctx.apiFetch("/api/auth/check-email", {
|
|
263
|
+
method: "POST",
|
|
264
|
+
body: JSON.stringify({ email: e })
|
|
265
|
+
})).json();
|
|
266
|
+
if (!t.ok) throw this.ctx.error.value = t.error, Error(t.error);
|
|
267
|
+
} catch (e) {
|
|
268
|
+
this.ctx.logger.error("Send code error", { data: e });
|
|
269
|
+
let t = e instanceof Error ? e.message : "Failed to send verification code";
|
|
270
|
+
throw this.ctx.error.value = t, e;
|
|
271
|
+
}
|
|
272
|
+
});
|
|
273
|
+
}
|
|
274
|
+
async verifyCode(e, t) {
|
|
275
|
+
return withLoadingState(this.ctx, async () => {
|
|
276
|
+
try {
|
|
277
|
+
let n = await (await this.ctx.apiFetch("/api/auth/verify-code", {
|
|
278
|
+
method: "POST",
|
|
279
|
+
body: JSON.stringify({
|
|
280
|
+
email: e,
|
|
281
|
+
code: t
|
|
282
|
+
})
|
|
283
|
+
})).json();
|
|
284
|
+
if (!n.ok) throw this.ctx.error.value = n.error, Error(n.error);
|
|
285
|
+
} catch (e) {
|
|
286
|
+
this.ctx.logger.error("Verify code error", { data: e });
|
|
287
|
+
let t = e instanceof Error ? e.message : "Failed to verify code";
|
|
288
|
+
throw this.ctx.error.value = t, e;
|
|
289
|
+
}
|
|
290
|
+
});
|
|
291
|
+
}
|
|
292
|
+
async logout() {
|
|
293
|
+
return withLoadingState(this.ctx, async () => {
|
|
294
|
+
try {
|
|
295
|
+
await this.ctx.apiFetch("/api/auth/logout", { method: "POST" });
|
|
296
|
+
} catch (e) {
|
|
297
|
+
this.ctx.logger.error("Logout error (user still logged out locally)", { data: e });
|
|
298
|
+
}
|
|
299
|
+
this.ctx.activeUser.value = void 0, this.ctx.token.value = null, this.ctx.storage.clearStorage();
|
|
300
|
+
});
|
|
301
|
+
}
|
|
302
|
+
async getCurrentUser() {
|
|
303
|
+
if (this.ctx.token.value) return withLoadingState(this.ctx, async () => {
|
|
304
|
+
try {
|
|
305
|
+
let e = await (await this.ctx.apiFetch("/api/users/me", { method: "GET" })).json();
|
|
306
|
+
if (!e.ok) {
|
|
307
|
+
this.ctx.error.value = e.error, e.error.includes("Authentication") && (this.ctx.activeUser.value = void 0, this.ctx.token.value = null, this.ctx.storage.clearStorage());
|
|
308
|
+
return;
|
|
309
|
+
}
|
|
310
|
+
return this.ctx.token.value && (this.ctx.activeUser.value = e.data), e.data;
|
|
311
|
+
} catch (e) {
|
|
312
|
+
this.ctx.logger.error("Get current user error", { data: e }), this.ctx.error.value = e instanceof Error ? e.message : "Failed to get user info";
|
|
313
|
+
return;
|
|
314
|
+
}
|
|
315
|
+
});
|
|
316
|
+
}
|
|
317
|
+
async requestAuthCode(e) {
|
|
318
|
+
return withLoadingState(this.ctx, async () => {
|
|
319
|
+
try {
|
|
320
|
+
let t = await (await this.ctx.apiFetch("/api/auth/check-email", {
|
|
321
|
+
method: "POST",
|
|
322
|
+
body: JSON.stringify({ email: e.email })
|
|
323
|
+
})).json();
|
|
324
|
+
return t.ok ? (this.ctx.logger.info("Auth code requested successfully", { email: e.email }), !0) : (this.ctx.error.value = t.error, this.ctx.logger.error("Failed to request auth code", {
|
|
325
|
+
email: e.email,
|
|
326
|
+
error: t.error
|
|
327
|
+
}), !1);
|
|
328
|
+
} catch (t) {
|
|
329
|
+
return this.ctx.logger.error("Request auth code error", {
|
|
330
|
+
email: e.email,
|
|
331
|
+
error: t
|
|
332
|
+
}), this.ctx.error.value = t instanceof Error ? t.message : "Failed to request auth code", !1;
|
|
333
|
+
}
|
|
334
|
+
});
|
|
335
|
+
}
|
|
336
|
+
async loginWithCode(e) {
|
|
337
|
+
return withLoadingState(this.ctx, async () => {
|
|
338
|
+
try {
|
|
339
|
+
let t = await (await this.ctx.apiFetch("/api/auth/verify-code", {
|
|
340
|
+
method: "POST",
|
|
341
|
+
body: JSON.stringify({
|
|
342
|
+
email: e.email,
|
|
343
|
+
code: e.code
|
|
344
|
+
})
|
|
345
|
+
})).json();
|
|
346
|
+
return t.ok ? (this.ctx.logger.info("Login successful", { email: e.email }), !0) : (this.ctx.error.value = t.error, this.ctx.logger.error("Login failed", {
|
|
347
|
+
email: e.email,
|
|
348
|
+
error: t.error
|
|
349
|
+
}), !1);
|
|
350
|
+
} catch (t) {
|
|
351
|
+
return this.ctx.logger.error("Login with code error", {
|
|
352
|
+
email: e.email,
|
|
353
|
+
error: t
|
|
354
|
+
}), this.ctx.error.value = t instanceof Error ? t.message : "Login failed", !1;
|
|
355
|
+
}
|
|
356
|
+
});
|
|
357
|
+
}
|
|
358
|
+
loginWithGoogle(e) {
|
|
359
|
+
if (typeof window > "u") {
|
|
360
|
+
this.ctx.logger.error("loginWithGoogle: Only available in browser");
|
|
361
|
+
return;
|
|
362
|
+
}
|
|
363
|
+
let t = resolveApiBase(this.ctx.apiBase, this.ctx.isDev), n = window.location.origin, r = `${t}/auth/google-popup?origin=${encodeURIComponent(n)}`, i = window.screenX + (window.outerWidth - 500) / 2, a = window.screenY + (window.outerHeight - 600) / 2, o = window.open(r, "google-auth", `width=500,height=600,left=${i},top=${a},popup=yes`);
|
|
364
|
+
if (!o) {
|
|
365
|
+
let t = "Popup was blocked. Please allow popups for this site.";
|
|
366
|
+
this.ctx.error.value = t, e?.onError?.(t);
|
|
367
|
+
return;
|
|
368
|
+
}
|
|
369
|
+
let handler = (n) => {
|
|
370
|
+
let r = new URL(t).origin;
|
|
371
|
+
if (n.origin === r) {
|
|
372
|
+
if (n.data?.type === "auth-success") {
|
|
373
|
+
window.removeEventListener("message", handler);
|
|
374
|
+
let { token: t, user: r } = n.data;
|
|
375
|
+
this.ctx.processApiResponse({
|
|
376
|
+
ok: !0,
|
|
377
|
+
data: {},
|
|
378
|
+
token: t,
|
|
379
|
+
user: r
|
|
380
|
+
}), this.ctx.logger.info("Google login successful"), e?.onSuccess?.(r);
|
|
381
|
+
} else if (n.data?.type === "auth-error") {
|
|
382
|
+
window.removeEventListener("message", handler);
|
|
383
|
+
let t = n.data.error || "Google authentication failed";
|
|
384
|
+
this.ctx.error.value = t, this.ctx.logger.error("Google login failed", { error: t }), e?.onError?.(t);
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
};
|
|
388
|
+
window.addEventListener("message", handler);
|
|
389
|
+
let s = setInterval(() => {
|
|
390
|
+
o.closed && (clearInterval(s), window.removeEventListener("message", handler));
|
|
391
|
+
}, 500);
|
|
392
|
+
}
|
|
393
|
+
}, BillingClient = class {
|
|
394
|
+
constructor(e) {
|
|
395
|
+
_defineProperty(this, "ctx", void 0), this.ctx = e;
|
|
396
|
+
}
|
|
397
|
+
async startTrial(e) {
|
|
398
|
+
let t = this.resolveOrgId(), n = resolveApiBase(this.ctx.apiBase, this.ctx.isDev), r = await (await fetch(`${n}/api/billing/checkout/create`, {
|
|
399
|
+
method: "POST",
|
|
400
|
+
headers: {
|
|
401
|
+
"Content-Type": "application/json",
|
|
402
|
+
...this.ctx.token.value && { Authorization: `Bearer ${this.ctx.token.value}` }
|
|
403
|
+
},
|
|
404
|
+
body: JSON.stringify({
|
|
405
|
+
orgId: t,
|
|
406
|
+
...e?.returnUrl && { returnUrl: e.returnUrl }
|
|
407
|
+
})
|
|
408
|
+
})).json();
|
|
409
|
+
if (!r.ok || !r.data?.url) {
|
|
410
|
+
let e = r.error || "Failed to create checkout session";
|
|
411
|
+
throw this.ctx.error.value = e, Error(e);
|
|
412
|
+
}
|
|
413
|
+
window.location.href = r.data.url;
|
|
414
|
+
}
|
|
415
|
+
async confirmCheckout(e) {
|
|
416
|
+
if (e?.sessionId) {
|
|
417
|
+
let t = resolveApiBase(this.ctx.apiBase, this.ctx.isDev), n = await (await fetch(`${t}/api/billing/complete-checkout`, {
|
|
418
|
+
method: "POST",
|
|
419
|
+
headers: {
|
|
420
|
+
"Content-Type": "application/json",
|
|
421
|
+
...this.ctx.token.value && { Authorization: `Bearer ${this.ctx.token.value}` }
|
|
422
|
+
},
|
|
423
|
+
body: JSON.stringify({ sessionId: e.sessionId })
|
|
424
|
+
})).json();
|
|
425
|
+
if (!n.ok) {
|
|
426
|
+
let e = n.error || "Failed to complete checkout";
|
|
427
|
+
throw this.ctx.error.value = e, Error(e);
|
|
428
|
+
}
|
|
429
|
+
}
|
|
430
|
+
return this.getStatus();
|
|
431
|
+
}
|
|
432
|
+
async getStatus() {
|
|
433
|
+
let e = this.resolveOrgId(), t = resolveApiBase(this.ctx.apiBase, this.ctx.isDev), n = await (await fetch(`${t}/api/billing/details/${e}`, { headers: { ...this.ctx.token.value && { Authorization: `Bearer ${this.ctx.token.value}` } } })).json();
|
|
434
|
+
return !n.ok || !n.data ? {
|
|
435
|
+
plan: void 0,
|
|
436
|
+
status: "none",
|
|
437
|
+
hasActiveBilling: !1,
|
|
438
|
+
maxAgents: 0
|
|
439
|
+
} : {
|
|
440
|
+
plan: n.data.plan,
|
|
441
|
+
status: n.data.status,
|
|
442
|
+
hasActiveBilling: [
|
|
443
|
+
"active",
|
|
444
|
+
"trial",
|
|
445
|
+
"canceling"
|
|
446
|
+
].includes(n.data.status),
|
|
447
|
+
maxAgents: n.data.maxAgents,
|
|
448
|
+
...n.data.trialEnd && { trialEnd: n.data.trialEnd },
|
|
449
|
+
...n.data.trialDaysRemaining !== void 0 && { trialDaysRemaining: n.data.trialDaysRemaining }
|
|
450
|
+
};
|
|
451
|
+
}
|
|
452
|
+
resolveOrgId() {
|
|
453
|
+
let e = this.ctx.activeUser.value?.orgs?.[0]?.orgId;
|
|
454
|
+
if (!e) throw Error("No organization found. Please sign in first.");
|
|
455
|
+
return e;
|
|
456
|
+
}
|
|
472
457
|
};
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
function
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
}
|
|
458
|
+
//#endregion
|
|
459
|
+
//#region clients/ChatClient.ts
|
|
460
|
+
function parseChatEvent(e) {
|
|
461
|
+
let t = e.split("\n").filter((e) => e.length > 0), n, r;
|
|
462
|
+
for (let e of t) e.startsWith("event:") ? n = e.slice(6).trim() : e.startsWith("data:") && (r = e.slice(5).trim());
|
|
463
|
+
if (!n || r === void 0) return null;
|
|
464
|
+
try {
|
|
465
|
+
let e = c.safeParse({
|
|
466
|
+
event: n,
|
|
467
|
+
data: JSON.parse(r)
|
|
468
|
+
});
|
|
469
|
+
return e.success ? e.data : null;
|
|
470
|
+
} catch {
|
|
471
|
+
return null;
|
|
472
|
+
}
|
|
489
473
|
}
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
};
|
|
474
|
+
function toChatStreamError(e) {
|
|
475
|
+
return {
|
|
476
|
+
code: e.code,
|
|
477
|
+
error: e.message,
|
|
478
|
+
...e.bucket ? { bucket: e.bucket } : {},
|
|
479
|
+
...e.actionLabel ? { actionLabel: e.actionLabel } : {},
|
|
480
|
+
...e.actionUrl ? { actionUrl: e.actionUrl } : {},
|
|
481
|
+
...e.help ? { help: e.help } : {}
|
|
482
|
+
};
|
|
500
483
|
}
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
return i.stage === "stream-open" && i.message.startsWith("HTTP ") ? `Stream failed: ${i.message}` : i.message;
|
|
484
|
+
function toAuthenticatedTransportError(e) {
|
|
485
|
+
return e.stage === "stream-open" && e.message.startsWith("HTTP ") ? `Stream failed: ${e.message}` : e.message;
|
|
504
486
|
}
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
}
|
|
487
|
+
function detectBrowserTimezone() {
|
|
488
|
+
try {
|
|
489
|
+
return Intl.DateTimeFormat().resolvedOptions().timeZone || void 0;
|
|
490
|
+
} catch {
|
|
491
|
+
return;
|
|
492
|
+
}
|
|
512
493
|
}
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
...i.metadata?.issue ? { issue: i.metadata.issue } : {}
|
|
528
|
-
};
|
|
494
|
+
function toChatMessage(e) {
|
|
495
|
+
if (!(e.role !== "assistant" && e.role !== "system")) return {
|
|
496
|
+
id: e.messageId,
|
|
497
|
+
text: e.content,
|
|
498
|
+
sender: e.role === "system" ? "system" : "agent",
|
|
499
|
+
timestamp: e.createdAt,
|
|
500
|
+
conversationId: e.conversationId,
|
|
501
|
+
sequence: e.sequence,
|
|
502
|
+
systemKind: e.systemKind,
|
|
503
|
+
...e.metadata?.turnOutcome ? { turnOutcome: e.metadata.turnOutcome } : {},
|
|
504
|
+
...e.metadata?.attachments?.length ? { attachments: e.metadata.attachments } : {},
|
|
505
|
+
...e.metadata?.toolActivities?.length ? { toolActivities: e.metadata.toolActivities } : {},
|
|
506
|
+
...e.metadata?.issue ? { issue: e.metadata.issue } : {}
|
|
507
|
+
};
|
|
529
508
|
}
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
};
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
}
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
509
|
+
var ChatClient = class {
|
|
510
|
+
constructor(e) {
|
|
511
|
+
_defineProperty(this, "ctx", void 0), this.ctx = e;
|
|
512
|
+
}
|
|
513
|
+
async cancelConversationTurn(e) {
|
|
514
|
+
let t = resolveApiBase(this.ctx.apiBase, this.ctx.isDev), n = this.ctx.token.value ? { Authorization: `Bearer ${this.ctx.token.value}` } : {}, r = await fetch(`${t}/api/conversations/${encodeURIComponent(e.conversationId)}/cancel`, {
|
|
515
|
+
method: "POST",
|
|
516
|
+
headers: {
|
|
517
|
+
"Content-Type": "application/json",
|
|
518
|
+
...n
|
|
519
|
+
},
|
|
520
|
+
body: JSON.stringify(e.turnId ? { turnId: e.turnId } : {})
|
|
521
|
+
}), i = await r.json();
|
|
522
|
+
if (!i.ok) throw Error(i.error || `Cancel failed: HTTP ${r.status}`);
|
|
523
|
+
return i.data;
|
|
524
|
+
}
|
|
525
|
+
async chatStreamPublic(e) {
|
|
526
|
+
let t = `${resolveApiBase(this.ctx.apiBase, this.ctx.isDev)}/api/agents/public/${encodeURIComponent(e.handle)}/chat/v2/stream`, n = "", r = !1, i = !1, a;
|
|
527
|
+
try {
|
|
528
|
+
let o = await fetch(t, {
|
|
529
|
+
method: "POST",
|
|
530
|
+
headers: { "Content-Type": "application/json" },
|
|
531
|
+
body: JSON.stringify({
|
|
532
|
+
message: e.message,
|
|
533
|
+
anonymousId: e.anonymousId,
|
|
534
|
+
...e.attachments?.length ? { attachments: e.attachments } : {},
|
|
535
|
+
...e.context ? { context: e.context } : {}
|
|
536
|
+
})
|
|
537
|
+
});
|
|
538
|
+
if (!o.ok) {
|
|
539
|
+
let t = await o.json().catch(() => ({ error: "Request failed" }));
|
|
540
|
+
e.onError(t.error || `HTTP ${o.status}`);
|
|
541
|
+
return;
|
|
542
|
+
}
|
|
543
|
+
if (n = o.headers.get("X-Conversation-Id") || "", a = o.body?.getReader(), !a) {
|
|
544
|
+
e.onError("No response stream");
|
|
545
|
+
return;
|
|
546
|
+
}
|
|
547
|
+
let s = new TextDecoder(), c = "";
|
|
548
|
+
outer: for (;;) {
|
|
549
|
+
let { done: t, value: n } = await a.read();
|
|
550
|
+
if (t) break;
|
|
551
|
+
c += s.decode(n, { stream: !0 });
|
|
552
|
+
let o = c.indexOf("\n\n");
|
|
553
|
+
for (; o !== -1;) {
|
|
554
|
+
let t = c.slice(0, o);
|
|
555
|
+
c = c.slice(o + 2);
|
|
556
|
+
let n = parseChatEvent(t);
|
|
557
|
+
if (n) {
|
|
558
|
+
if (n.event === "working_state") e.onStatus?.(n.data.description);
|
|
559
|
+
else if (n.event === "tool_activity") e.onToolActivity?.(n.data);
|
|
560
|
+
else if (n.event === "working_end") e.onWorkingEnd?.();
|
|
561
|
+
else if (n.event === "message_delta") e.onDelta(n.data.delta, n.data.role);
|
|
562
|
+
else if (n.event === "error") {
|
|
563
|
+
r = !0, e.onError(toChatStreamError(n.data));
|
|
564
|
+
break outer;
|
|
565
|
+
} else if (n.event === "message" && (n.data.message.role === "assistant" || n.data.message.role === "system")) {
|
|
566
|
+
i = !0;
|
|
567
|
+
let t = toChatMessage(n.data.message);
|
|
568
|
+
t && e.onMessage?.(t);
|
|
569
|
+
} else if (n.event === "stream_end") break outer;
|
|
570
|
+
}
|
|
571
|
+
o = c.indexOf("\n\n");
|
|
572
|
+
}
|
|
573
|
+
}
|
|
574
|
+
!r && i ? e.onDone(n) : r || e.onError("Stream ended before reply arrived");
|
|
575
|
+
} catch (t) {
|
|
576
|
+
e.onError(t instanceof Error ? t.message : "Stream failed");
|
|
577
|
+
} finally {
|
|
578
|
+
try {
|
|
579
|
+
await a?.cancel();
|
|
580
|
+
} catch {}
|
|
581
|
+
}
|
|
582
|
+
}
|
|
583
|
+
async chatStreamAuthenticated(e) {
|
|
584
|
+
let t = resolveApiBase(this.ctx.apiBase, this.ctx.isDev), n = this.ctx.token.value ? { Authorization: `Bearer ${this.ctx.token.value}` } : {}, r, i = null;
|
|
585
|
+
try {
|
|
586
|
+
let a = await fetch(`${t}/api/conversations`, {
|
|
587
|
+
method: "POST",
|
|
588
|
+
headers: {
|
|
589
|
+
"Content-Type": "application/json",
|
|
590
|
+
...n
|
|
591
|
+
},
|
|
592
|
+
body: JSON.stringify({ agentId: e.agentId })
|
|
593
|
+
}), o = await a.json();
|
|
594
|
+
if (!o.ok) {
|
|
595
|
+
e.onError(o.error || `Conversation failed: HTTP ${a.status}`);
|
|
596
|
+
return;
|
|
597
|
+
}
|
|
598
|
+
r = o.data.conversationId, i = o.data.latestSequence ?? null;
|
|
599
|
+
} catch (t) {
|
|
600
|
+
e.onError(t instanceof Error ? t.message : "Conversation failed");
|
|
601
|
+
return;
|
|
602
|
+
}
|
|
603
|
+
let a = i ? `${t}/api/conversations/${encodeURIComponent(r)}/stream?since=${encodeURIComponent(i)}` : `${t}/api/conversations/${encodeURIComponent(r)}/stream`, c = `nonce_${s(16)}`, l = detectBrowserTimezone();
|
|
604
|
+
await o({
|
|
605
|
+
streamUrl: a,
|
|
606
|
+
sendUrl: `${t}/api/messages`,
|
|
607
|
+
headers: n,
|
|
608
|
+
sendBody: {
|
|
609
|
+
conversationId: r,
|
|
610
|
+
content: e.message,
|
|
611
|
+
clientNonce: c,
|
|
612
|
+
...l ? { viewerTimezone: l } : {},
|
|
613
|
+
...e.attachments && e.attachments.length > 0 ? { attachments: e.attachments } : {}
|
|
614
|
+
},
|
|
615
|
+
onStatus: e.onStatus,
|
|
616
|
+
onDelta: e.onDelta,
|
|
617
|
+
onToolActivity: e.onToolActivity,
|
|
618
|
+
onWorkingEnd: e.onWorkingEnd,
|
|
619
|
+
onMessage: (t) => {
|
|
620
|
+
let n = toChatMessage(t);
|
|
621
|
+
n && e.onMessage?.(n);
|
|
622
|
+
},
|
|
623
|
+
onDone: () => e.onDone(r),
|
|
624
|
+
onChatError: (t) => e.onError(toChatStreamError(t)),
|
|
625
|
+
onTransportError: (t) => e.onError(t.code ? {
|
|
626
|
+
code: t.code,
|
|
627
|
+
error: t.message
|
|
628
|
+
} : toAuthenticatedTransportError(t))
|
|
629
|
+
});
|
|
630
|
+
}
|
|
631
|
+
}, UserClient = class {
|
|
632
|
+
constructor(e) {
|
|
633
|
+
_defineProperty(this, "ctx", void 0), this.ctx = e;
|
|
634
|
+
}
|
|
635
|
+
async getPublicAgent(e) {
|
|
636
|
+
this.ctx.loading.value = !0, this.ctx.error.value = null;
|
|
637
|
+
try {
|
|
638
|
+
let t = await (await this.ctx.apiFetch(`/api/agents/public/${encodeURIComponent(e.handle)}`)).json();
|
|
639
|
+
if (!t.ok) {
|
|
640
|
+
this.ctx.error.value = t.error, this.ctx.logger.error("Failed to fetch public agent", {
|
|
641
|
+
handle: e.handle,
|
|
642
|
+
error: t.error
|
|
643
|
+
});
|
|
644
|
+
return;
|
|
645
|
+
}
|
|
646
|
+
return t.data;
|
|
647
|
+
} catch (t) {
|
|
648
|
+
this.ctx.logger.error("Get public agent error", {
|
|
649
|
+
handle: e.handle,
|
|
650
|
+
error: t
|
|
651
|
+
}), this.ctx.error.value = t instanceof Error ? t.message : "Failed to fetch agent";
|
|
652
|
+
return;
|
|
653
|
+
} finally {
|
|
654
|
+
this.ctx.loading.value = !1;
|
|
655
|
+
}
|
|
656
|
+
}
|
|
657
|
+
async getAgentByEmail(e) {
|
|
658
|
+
this.ctx.loading.value = !0, this.ctx.error.value = null;
|
|
659
|
+
try {
|
|
660
|
+
let t = await (await this.ctx.apiFetch(`/api/agents/by-email/${encodeURIComponent(e.email)}`)).json();
|
|
661
|
+
if (!t.ok) {
|
|
662
|
+
this.ctx.error.value = t.error, this.ctx.logger.error("Failed to fetch agent by email", {
|
|
663
|
+
email: e.email,
|
|
664
|
+
error: t.error
|
|
665
|
+
});
|
|
666
|
+
return;
|
|
667
|
+
}
|
|
668
|
+
return t.data;
|
|
669
|
+
} catch (t) {
|
|
670
|
+
this.ctx.logger.error("Get agent by email error", {
|
|
671
|
+
email: e.email,
|
|
672
|
+
error: t
|
|
673
|
+
}), this.ctx.error.value = t instanceof Error ? t.message : "Failed to fetch agent";
|
|
674
|
+
return;
|
|
675
|
+
} finally {
|
|
676
|
+
this.ctx.loading.value = !1;
|
|
677
|
+
}
|
|
678
|
+
}
|
|
679
|
+
generateAnonId() {
|
|
680
|
+
let e = "pagelinesAnonId", t = this.ctx.storage.getItem(e);
|
|
681
|
+
return t || (t = a({ prefix: "anon" }), this.ctx.storage.setItem(e, t)), t;
|
|
682
|
+
}
|
|
683
|
+
track(e) {
|
|
684
|
+
try {
|
|
685
|
+
typeof window < "u" && window.parent !== window && window.parent.postMessage({
|
|
686
|
+
type: "pagelines_track",
|
|
687
|
+
event: e.event,
|
|
688
|
+
agentId: e.agentId,
|
|
689
|
+
properties: e.properties || {}
|
|
690
|
+
}, "*"), this.ctx.logger.info("SDK event tracked", {
|
|
691
|
+
event: e.event,
|
|
692
|
+
agentId: e.agentId
|
|
693
|
+
});
|
|
694
|
+
} catch (t) {
|
|
695
|
+
this.ctx.logger.error("Track event error", {
|
|
696
|
+
error: t,
|
|
697
|
+
args: e
|
|
698
|
+
});
|
|
699
|
+
}
|
|
700
|
+
}
|
|
701
|
+
}, SDKGlobal = class {
|
|
702
|
+
constructor() {
|
|
703
|
+
_defineProperty(this, "key", "__PAGELINES_SDK__");
|
|
704
|
+
}
|
|
705
|
+
get() {
|
|
706
|
+
if (!(typeof window > "u")) return globalThis[this.key];
|
|
707
|
+
}
|
|
708
|
+
set(e) {
|
|
709
|
+
typeof window < "u" && (globalThis[this.key] = e);
|
|
710
|
+
}
|
|
711
|
+
delete() {
|
|
712
|
+
typeof window < "u" && delete globalThis[this.key];
|
|
713
|
+
}
|
|
714
|
+
}, C = new SDKGlobal(), w = class PageLinesSDK extends n {
|
|
715
|
+
static getInstance(e = {}) {
|
|
716
|
+
return C.get() || new PageLinesSDK(e);
|
|
717
|
+
}
|
|
718
|
+
get isDev() {
|
|
719
|
+
return this.settings.isDev ?? (typeof window < "u" ? window.location.hostname === "localhost" || window.location.hostname.includes("127.0.0.1") : !1);
|
|
720
|
+
}
|
|
721
|
+
constructor(e = {}) {
|
|
722
|
+
let t = C.get();
|
|
723
|
+
if (t) return console.debug("[PageLinesSDK] Returning existing singleton instance"), t;
|
|
724
|
+
super("PageLinesSDK", e), _defineProperty(this, "activeUser", u()), _defineProperty(this, "token", u(null)), _defineProperty(this, "deviceId", u(null)), _defineProperty(this, "loading", u(!1)), _defineProperty(this, "error", u(null)), _defineProperty(this, "apiBase", this.settings.apiBase || void 0), _defineProperty(this, "storage", new SDKStorage()), _defineProperty(this, "sessionId", a({ prefix: "ses" })), _defineProperty(this, "currentAgent", l(() => {
|
|
725
|
+
let e = this.activeUser.value;
|
|
726
|
+
if (!e?.agents) return;
|
|
727
|
+
let t = e.primaryAgentId || e.agents[0]?.agentId;
|
|
728
|
+
if (t) return e.agents.find((e) => e.agentId === t);
|
|
729
|
+
})), _defineProperty(this, "currentOrg", l(() => {
|
|
730
|
+
let e = this.currentAgent.value;
|
|
731
|
+
if (!e?.orgId) return;
|
|
732
|
+
let t = this.activeUser.value;
|
|
733
|
+
if (t?.orgs) return t.orgs.find((t) => t.orgId === e.orgId);
|
|
734
|
+
})), _defineProperty(this, "initialized", void 0), _defineProperty(this, "resolveUser", void 0), _defineProperty(this, "agent", void 0), _defineProperty(this, "auth", void 0), _defineProperty(this, "billing", void 0), _defineProperty(this, "chat", void 0), _defineProperty(this, "user", void 0), C.set(this);
|
|
735
|
+
let n = {
|
|
736
|
+
apiFetch: createApiFetch({
|
|
737
|
+
isDev: this.isDev,
|
|
738
|
+
apiBase: this.apiBase
|
|
739
|
+
}, this.processApiResponse.bind(this), () => this.clearSession()),
|
|
740
|
+
apiBase: this.apiBase,
|
|
741
|
+
isDev: this.isDev,
|
|
742
|
+
activeUser: this.activeUser,
|
|
743
|
+
token: this.token,
|
|
744
|
+
loading: this.loading,
|
|
745
|
+
error: this.error,
|
|
746
|
+
currentAgent: this.currentAgent,
|
|
747
|
+
storage: this.storage,
|
|
748
|
+
sessionId: this.sessionId,
|
|
749
|
+
logger: this.logger,
|
|
750
|
+
processApiResponse: this.processApiResponse.bind(this)
|
|
751
|
+
};
|
|
752
|
+
this.agent = new AgentClient(n), this.auth = new AuthClient(n), this.billing = new BillingClient(n), this.chat = new ChatClient(n), this.user = new UserClient(n), this.logger.info("PageLinesSDK initialized"), this.storage.loadFromStorage(this.authRefs()), this.storage.syncWithGlobalStores(this.authRefs()), this.initialized = new Promise((e) => {
|
|
753
|
+
this.resolveUser = e;
|
|
754
|
+
}), this.token.value && !this.activeUser.value ? this.auth.getCurrentUser().then((e) => {
|
|
755
|
+
this.resolveUser && (this.resolveUser(e), this.resolveUser = void 0);
|
|
756
|
+
}).catch((e) => {
|
|
757
|
+
this.logger.error("Auto user fetch failed", { data: e }), this.resolveUser && (this.resolveUser(void 0), this.resolveUser = void 0);
|
|
758
|
+
}) : this.activeUser.value ? this.resolveUser?.(this.activeUser.value) : this.resolveUser?.(void 0);
|
|
759
|
+
}
|
|
760
|
+
processApiResponse(e) {
|
|
761
|
+
if (!e.ok) return;
|
|
762
|
+
let t = !1;
|
|
763
|
+
e.user && (this.activeUser.value = e.user, this.logger.info("User updated from API response", { data: e.user }), t = !0), e.token && (this.token.value = e.token, this.logger.info("Token updated from API response"), t = !0);
|
|
764
|
+
let n = e.data?.deviceId;
|
|
765
|
+
n && (this.deviceId.value = n, t = !0), t && this.persistAuth(), this.resolveUser && (this.resolveUser(e.user), this.resolveUser = void 0);
|
|
766
|
+
}
|
|
767
|
+
authRefs() {
|
|
768
|
+
return {
|
|
769
|
+
activeUser: this.activeUser,
|
|
770
|
+
token: this.token,
|
|
771
|
+
deviceId: this.deviceId
|
|
772
|
+
};
|
|
773
|
+
}
|
|
774
|
+
persistAuth() {
|
|
775
|
+
this.storage.saveToStorage(this.authRefs());
|
|
776
|
+
}
|
|
777
|
+
clearSession() {
|
|
778
|
+
this.logger.info("Clearing session"), this.activeUser.value = void 0, this.token.value = null, this.deviceId.value = null, this.error.value = null, this.storage.clearStorage();
|
|
779
|
+
}
|
|
780
|
+
clear() {
|
|
781
|
+
this.logger.info("Clearing SDK completely"), this.clearSession(), this.loading.value = !1, this.sessionId = a({ prefix: "ses" }), C.delete();
|
|
782
|
+
}
|
|
755
783
|
};
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
const o = $.get();
|
|
761
|
-
if (o)
|
|
762
|
-
return console.debug("[PageLinesSDK] Returning existing singleton instance"), o;
|
|
763
|
-
super("PageLinesSDK", t);
|
|
764
|
-
// Vue reactive state
|
|
765
|
-
l(this, "activeUser", b());
|
|
766
|
-
l(this, "token", b(null));
|
|
767
|
-
// Opaque session token for Bearer clients. Web may also have the same
|
|
768
|
-
// credential in the HttpOnly pl-session cookie, which JS cannot read.
|
|
769
|
-
l(this, "deviceId", b(null));
|
|
770
|
-
l(this, "loading", b(!1));
|
|
771
|
-
l(this, "error", b(null));
|
|
772
|
-
l(this, "apiBase", this.settings.apiBase || void 0);
|
|
773
|
-
// Storage handler for persistence
|
|
774
|
-
l(this, "storage", new P());
|
|
775
|
-
// Auto-managed session ID for usage tracking
|
|
776
|
-
l(this, "sessionId", D({ prefix: "ses" }));
|
|
777
|
-
// Computed properties derived from activeUser (matches UserClient pattern)
|
|
778
|
-
l(this, "currentAgent", V(() => {
|
|
779
|
-
const t = this.activeUser.value;
|
|
780
|
-
if (!t?.agents)
|
|
781
|
-
return;
|
|
782
|
-
const o = t.primaryAgentId || t.agents[0]?.agentId;
|
|
783
|
-
if (o)
|
|
784
|
-
return t.agents.find((r) => r.agentId === o);
|
|
785
|
-
}));
|
|
786
|
-
l(this, "currentOrg", V(() => {
|
|
787
|
-
const t = this.currentAgent.value;
|
|
788
|
-
if (!t?.orgId)
|
|
789
|
-
return;
|
|
790
|
-
const o = this.activeUser.value;
|
|
791
|
-
if (o?.orgs)
|
|
792
|
-
return o.orgs.find((r) => r.orgId === t.orgId);
|
|
793
|
-
}));
|
|
794
|
-
// Initialization state
|
|
795
|
-
l(this, "initialized");
|
|
796
|
-
l(this, "resolveUser");
|
|
797
|
-
// Sub-clients (new preferred API surface)
|
|
798
|
-
l(this, "agent");
|
|
799
|
-
l(this, "auth");
|
|
800
|
-
l(this, "billing");
|
|
801
|
-
l(this, "chat");
|
|
802
|
-
l(this, "user");
|
|
803
|
-
$.set(this);
|
|
804
|
-
const s = {
|
|
805
|
-
apiFetch: ce(
|
|
806
|
-
{ isDev: this.isDev, apiBase: this.apiBase },
|
|
807
|
-
this.processApiResponse.bind(this),
|
|
808
|
-
() => this.clearSession()
|
|
809
|
-
),
|
|
810
|
-
apiBase: this.apiBase,
|
|
811
|
-
isDev: this.isDev,
|
|
812
|
-
activeUser: this.activeUser,
|
|
813
|
-
token: this.token,
|
|
814
|
-
loading: this.loading,
|
|
815
|
-
error: this.error,
|
|
816
|
-
currentAgent: this.currentAgent,
|
|
817
|
-
storage: this.storage,
|
|
818
|
-
sessionId: this.sessionId,
|
|
819
|
-
logger: this.logger,
|
|
820
|
-
processApiResponse: this.processApiResponse.bind(this)
|
|
821
|
-
};
|
|
822
|
-
this.agent = new F(s), this.auth = new R(s), this.billing = new L(s), this.chat = new j(s), this.user = new C(s), this.logger.info("PageLinesSDK initialized"), this.storage.loadFromStorage(this.authRefs()), this.storage.syncWithGlobalStores(this.authRefs()), this.initialized = new Promise((n) => {
|
|
823
|
-
this.resolveUser = n;
|
|
824
|
-
}), this.token.value && !this.activeUser.value ? this.auth.getCurrentUser().then((n) => {
|
|
825
|
-
this.resolveUser && (this.resolveUser(n), this.resolveUser = void 0);
|
|
826
|
-
}).catch((n) => {
|
|
827
|
-
this.logger.error("Auto user fetch failed", { data: n }), this.resolveUser && (this.resolveUser(void 0), this.resolveUser = void 0);
|
|
828
|
-
}) : this.activeUser.value ? this.resolveUser?.(this.activeUser.value) : this.resolveUser?.(void 0);
|
|
829
|
-
}
|
|
830
|
-
/**
|
|
831
|
-
* Get singleton instance in browser (creates if doesn't exist)
|
|
832
|
-
* Node.js: Always creates new instance (no singleton)
|
|
833
|
-
* Recommended: Use this instead of constructor for shared state
|
|
834
|
-
*/
|
|
835
|
-
static getInstance(t = {}) {
|
|
836
|
-
return $.get() || new O(t);
|
|
837
|
-
}
|
|
838
|
-
get isDev() {
|
|
839
|
-
return this.settings.isDev ?? (typeof window < "u" ? window.location.hostname === "localhost" || window.location.hostname.includes("127.0.0.1") : !1);
|
|
840
|
-
}
|
|
841
|
-
// Process ApiResponse for automatic user/token updates - public for dependency injection
|
|
842
|
-
processApiResponse(t) {
|
|
843
|
-
if (!t.ok) return;
|
|
844
|
-
let o = !1;
|
|
845
|
-
t.user && (this.activeUser.value = t.user, this.logger.info("User updated from API response", { data: t.user }), o = !0), t.token && (this.token.value = t.token, this.logger.info("Token updated from API response"), o = !0);
|
|
846
|
-
const r = t.data?.deviceId;
|
|
847
|
-
r && (this.deviceId.value = r, o = !0), o && this.persistAuth(), this.resolveUser && (this.resolveUser(t.user), this.resolveUser = void 0);
|
|
848
|
-
}
|
|
849
|
-
authRefs() {
|
|
850
|
-
return {
|
|
851
|
-
activeUser: this.activeUser,
|
|
852
|
-
token: this.token,
|
|
853
|
-
deviceId: this.deviceId
|
|
854
|
-
};
|
|
855
|
-
}
|
|
856
|
-
persistAuth() {
|
|
857
|
-
this.storage.saveToStorage(this.authRefs());
|
|
858
|
-
}
|
|
859
|
-
// Clear all user data and tokens
|
|
860
|
-
clearSession() {
|
|
861
|
-
this.logger.info("Clearing session"), this.activeUser.value = void 0, this.token.value = null, this.deviceId.value = null, this.error.value = null, this.storage.clearStorage();
|
|
862
|
-
}
|
|
863
|
-
// Full reset - clears session + state + destroys singleton
|
|
864
|
-
clear() {
|
|
865
|
-
this.logger.info("Clearing SDK completely"), this.clearSession(), this.loading.value = !1, this.sessionId = D({ prefix: "ses" }), $.delete();
|
|
866
|
-
}
|
|
867
|
-
};
|
|
868
|
-
u(O, "PageLinesSDK");
|
|
869
|
-
let Z = O;
|
|
870
|
-
export {
|
|
871
|
-
F as A,
|
|
872
|
-
L as B,
|
|
873
|
-
j as C,
|
|
874
|
-
Z as P,
|
|
875
|
-
C as U,
|
|
876
|
-
R as a,
|
|
877
|
-
p as r
|
|
878
|
-
};
|
|
879
|
-
//# sourceMappingURL=sdkClient.js.map
|
|
784
|
+
//#endregion
|
|
785
|
+
export { AuthClient as a, resolveApiBase as c, BillingClient as i, UserClient as n, AgentClient as o, ChatClient as r, _defineProperty as s, w as t };
|
|
786
|
+
|
|
787
|
+
//# sourceMappingURL=sdkClient.js.map
|