@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.
Files changed (100) hide show
  1. package/dist/AgentProvider.js +701 -0
  2. package/dist/AgentProvider.js.map +1 -0
  3. package/dist/AgentWidgetInline.js +44 -32
  4. package/dist/AgentWidgetInline.js.map +1 -1
  5. package/dist/AgentWidgetModal.js +75 -64
  6. package/dist/AgentWidgetModal.js.map +1 -1
  7. package/dist/AgentWrap.js +5180 -0
  8. package/dist/AgentWrap.js.map +1 -0
  9. package/dist/FModal.js +165 -0
  10. package/dist/FModal.js.map +1 -0
  11. package/dist/agent/ui/AgentChat.vue.d.ts +3 -4
  12. package/dist/agent/ui/AgentInputEmail.vue.d.ts +3 -2
  13. package/dist/agent/ui/AgentInputOneTimeCode.vue.d.ts +3 -4
  14. package/dist/agent/ui/AgentModal.vue.d.ts +2 -1
  15. package/dist/agent/ui/AgentProvider.vue.d.ts +2 -1
  16. package/dist/agent/ui/AgentSidebarClose.vue.d.ts +3 -2
  17. package/dist/agent/ui/AgentToolActivityGroup.vue.d.ts +2 -1
  18. package/dist/agent/ui/AgentWidget.vue.d.ts +3 -2
  19. package/dist/agent/ui/AgentWrap.vue.d.ts +14 -29
  20. package/dist/agent/ui/ChatRichText.vue.d.ts +2 -1
  21. package/dist/agent/ui/ChatScroller.vue.d.ts +8 -18
  22. package/dist/agent/ui/ChatVisualBlock.vue.d.ts +2 -3
  23. package/dist/agent/ui/ElAgentAbout.vue.d.ts +2 -1
  24. package/dist/agent/ui/ElAgentButton.vue.d.ts +7 -11
  25. package/dist/agent/ui/ElAgentChat.vue.d.ts +2 -53
  26. package/dist/agent/ui/ElAgentChatPreview.vue.d.ts +2 -1
  27. package/dist/agent/ui/ElAgentHeader.vue.d.ts +2 -1
  28. package/dist/agent/ui/ElAgentModeSidebar.vue.d.ts +2 -1
  29. package/dist/agent/ui/ElAgentSidebar.vue.d.ts +2 -1
  30. package/dist/agent/ui/ElAuthGate.vue.d.ts +2 -1
  31. package/dist/agent/ui/ElAuthPanel.vue.d.ts +2 -1
  32. package/dist/agent/ui/ElCreateAgent.vue.d.ts +3 -2
  33. package/dist/agent/ui/ElModeHeader.vue.d.ts +2 -1
  34. package/dist/agent/ui/ElProvisioningStatus.vue.d.ts +3 -2
  35. package/dist/agent/ui/ElSidebar.vue.d.ts +9 -12
  36. package/dist/agent/ui/ElTrialGate.vue.d.ts +2 -1
  37. package/dist/agent.js +157 -186
  38. package/dist/agent.js.map +1 -1
  39. package/dist/constants.js +80 -0
  40. package/dist/constants.js.map +1 -0
  41. package/dist/contract.js +6284 -0
  42. package/dist/contract.js.map +1 -0
  43. package/dist/demo.js +242 -259
  44. package/dist/demo.js.map +1 -1
  45. package/dist/rolldown-runtime.js +14 -0
  46. package/dist/sdk.css +2 -1
  47. package/dist/sdk.js +4 -14
  48. package/dist/sdkClient.js +775 -867
  49. package/dist/sdkClient.js.map +1 -1
  50. package/dist/ui/EffectTransitionList.vue.d.ts +7 -11
  51. package/dist/ui/FClose.vue.d.ts +3 -2
  52. package/dist/ui/FModal.vue.d.ts +7 -5
  53. package/dist/ui/FSpinner.vue.d.ts +3 -2
  54. package/dist/useWidgetState.js +21 -0
  55. package/dist/useWidgetState.js.map +1 -0
  56. package/dist/widget/ui/AgentWidgetInline.vue.d.ts +2 -1
  57. package/dist/widget/ui/AgentWidgetModal.vue.d.ts +2 -1
  58. package/dist/widget/ui/AgentWidgetOnboard.vue.d.ts +2 -1
  59. package/dist/widget/ui/AgentWidgetPopup.vue.d.ts +3 -2
  60. package/dist/widget.js +717 -7
  61. package/dist/widget.js.map +1 -1
  62. package/package.json +25 -25
  63. package/dist/AgentProvider.vue_vue_type_script_setup_true_lang.js +0 -842
  64. package/dist/AgentProvider.vue_vue_type_script_setup_true_lang.js.map +0 -1
  65. package/dist/AgentWidgetOnboard.js +0 -5
  66. package/dist/AgentWidgetOnboard.js.map +0 -1
  67. package/dist/AgentWrap.vue_vue_type_script_setup_true_lang.js +0 -4952
  68. package/dist/AgentWrap.vue_vue_type_script_setup_true_lang.js.map +0 -1
  69. package/dist/FModal.vue_vue_type_script_setup_true_lang.js +0 -181
  70. package/dist/FModal.vue_vue_type_script_setup_true_lang.js.map +0 -1
  71. package/dist/agent/test/AgentController.test.d.ts +0 -1
  72. package/dist/agent/test/AgentToolActivityGroup.client.test.d.ts +0 -1
  73. package/dist/agent/test/ChatRichText.client.test.d.ts +0 -1
  74. package/dist/agent/test/ChatVisualBlock.client.test.d.ts +0 -1
  75. package/dist/agent/test/ElAgentChat.client.test.d.ts +0 -1
  76. package/dist/agent/test/ElAgentChatPreview.client.test.d.ts +0 -1
  77. package/dist/agent/test/VoiceRecorderController.unit.test.d.ts +0 -1
  78. package/dist/agent/test/auth-inputs.test.d.ts +0 -1
  79. package/dist/agent/test/chat-visual-format.unit.test.d.ts +0 -1
  80. package/dist/agent/test/utils.test.d.ts +0 -1
  81. package/dist/agent/test/webhook.test.d.ts +0 -1
  82. package/dist/contract/regenerate.d.ts +0 -2
  83. package/dist/contract/test/build.unit.test.d.ts +0 -1
  84. package/dist/contract/test/conversation-wire-shapes.unit.test.d.ts +0 -14
  85. package/dist/contract/test/snapshot.unit.test.d.ts +0 -1
  86. package/dist/index.js +0 -871
  87. package/dist/index.js.map +0 -1
  88. package/dist/sdk.js.map +0 -1
  89. package/dist/socialPlatforms.js +0 -82
  90. package/dist/socialPlatforms.js.map +0 -1
  91. package/dist/test/agent-client.test.d.ts +0 -4
  92. package/dist/test/api.test.d.ts +0 -1
  93. package/dist/test/billing-client.test.d.ts +0 -4
  94. package/dist/test/build.test.d.ts +0 -1
  95. package/dist/test/chat-authenticated.test.d.ts +0 -1
  96. package/dist/test/chat-issue-presentation.test.d.ts +0 -1
  97. package/dist/test/derive-mode.test.d.ts +0 -1
  98. package/dist/test/sdk-token-capture.test.d.ts +0 -13
  99. package/dist/vite.config.sdk.d.ts +0 -2
  100. package/dist/vitest.config.d.ts +0 -2
package/dist/sdkClient.js CHANGED
@@ -1,879 +1,787 @@
1
- var Q = Object.defineProperty;
2
- var ee = (i, e, t) => e in i ? Q(i, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : i[e] = t;
3
- var u = (i, e) => Q(i, "name", { value: e, configurable: !0 });
4
- var l = (i, e, t) => ee(i, typeof e != "symbol" ? e + "" : e, t);
5
- import { APP_PORT as te, PAGELINES_URLS as oe, cookieUtil as S, createLogger as re, shortId as se, runAuthenticatedChatTurnStream as ie, zChatEvent as ae, objectId as D, SettingsObject as ne } from "@pagelines/core";
6
- import { watch as B, ref as b, computed as V } from "vue";
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
- u(p, "resolveApiBase");
11
- function ce(i, e, t) {
12
- const o = p(i.apiBase, i.isDev);
13
- return async (r, s) => {
14
- const n = S.getAuthToken(), h = new Headers(s?.headers);
15
- h.has("Content-Type") || h.set("Content-Type", "application/json"), n && !h.has("Authorization") && h.set("Authorization", `Bearer ${n}`);
16
- const a = typeof r == "string" && r.startsWith("/") ? `${o}${r}` : r, c = await fetch(a, {
17
- ...s,
18
- headers: h,
19
- credentials: "include"
20
- });
21
- if (t && c.status === 401 && (await c.clone().json().catch(() => {
22
- }))?.code === "TOKEN_ERROR" && t(), e) {
23
- const d = c.json.bind(c);
24
- c.json = async () => {
25
- const f = await d();
26
- return typeof f == "object" && f !== null && "ok" in f && ("user" in f || "token" in f) && e(f), f;
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
- u(ce, "createApiFetch");
33
- const le = /* @__PURE__ */ Symbol("clean");
34
- let v = [], w = 0;
35
- const T = 4, ue = globalThis.nanostoresGlobal || (globalThis.nanostoresGlobal = { epoch: 0 }), _ = /* @__PURE__ */ u(/* @__NO_SIDE_EFFECTS__ */ (i) => {
36
- let e = [], t = {
37
- get() {
38
- return t.lc || t.listen(() => {
39
- })(), t.value;
40
- },
41
- init: i,
42
- lc: 0,
43
- listen(o) {
44
- return t.lc = e.push(o), () => {
45
- for (let s = w + T; s < v.length; )
46
- v[s] === o ? v.splice(s, T) : s += T;
47
- let r = e.indexOf(o);
48
- ~r && (e.splice(r, 1), --t.lc || t.off());
49
- };
50
- },
51
- notify(o, r) {
52
- ue.epoch++;
53
- let s = !v.length;
54
- for (let n of e)
55
- v.push(n, t.value, o, r);
56
- if (s) {
57
- for (w = 0; w < v.length; w += T)
58
- v[w](
59
- v[w + 1],
60
- v[w + 2],
61
- v[w + 3]
62
- );
63
- v.length = 0;
64
- }
65
- },
66
- /* It will be called on last listener unsubscribing.
67
- We will redefine it in onMount and onStop. */
68
- off() {
69
- },
70
- set(o) {
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
- u(z, "SDKStorage");
177
- let P = z;
178
- const J = class J {
179
- constructor(e) {
180
- l(this, "ctx");
181
- this.ctx = e;
182
- }
183
- async create(e) {
184
- const t = e.orgId || this.resolveOrgId(), o = p(this.ctx.apiBase, this.ctx.isDev), s = await (await fetch(`${o}/api/agents`, {
185
- method: "POST",
186
- headers: {
187
- "Content-Type": "application/json",
188
- ...this.ctx.token.value && { Authorization: `Bearer ${this.ctx.token.value}` }
189
- },
190
- body: JSON.stringify({ name: e.name, orgId: t })
191
- })).json();
192
- if (!s.ok || !s.data?.[0]) {
193
- const n = s.error || "Failed to create agent";
194
- throw this.ctx.error.value = n, new Error(n);
195
- }
196
- return this.ctx.processApiResponse(s), s.data[0];
197
- }
198
- /**
199
- * Live runtime status — the same canonical `LiveStatus` shape every other
200
- * status producer emits (`GET /agents` rows, SSE status frames, lifecycle
201
- * action responses). See plans/bots/bot-state-management.md.
202
- * `{ lifecycle: 'unknown' }` is the graceful degrade on any fetch failure.
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
- u(k, "withLoadingState");
275
- const G = class G {
276
- constructor(e) {
277
- l(this, "ctx");
278
- this.ctx = e;
279
- }
280
- async sendCode(e) {
281
- return k(this.ctx, async () => {
282
- try {
283
- const o = await (await this.ctx.apiFetch("/api/auth/check-email", {
284
- method: "POST",
285
- body: JSON.stringify({ email: e })
286
- })).json();
287
- if (!o.ok)
288
- throw this.ctx.error.value = o.error, new Error(o.error);
289
- } catch (t) {
290
- this.ctx.logger.error("Send code error", { data: t });
291
- const o = t instanceof Error ? t.message : "Failed to send verification code";
292
- throw this.ctx.error.value = o, t;
293
- }
294
- });
295
- }
296
- async verifyCode(e, t) {
297
- return k(this.ctx, async () => {
298
- try {
299
- const r = await (await this.ctx.apiFetch("/api/auth/verify-code", {
300
- method: "POST",
301
- body: JSON.stringify({ email: e, code: t })
302
- })).json();
303
- if (!r.ok)
304
- throw this.ctx.error.value = r.error, new Error(r.error);
305
- } catch (o) {
306
- this.ctx.logger.error("Verify code error", { data: o });
307
- const r = o instanceof Error ? o.message : "Failed to verify code";
308
- throw this.ctx.error.value = r, o;
309
- }
310
- });
311
- }
312
- async logout() {
313
- return k(this.ctx, async () => {
314
- try {
315
- await this.ctx.apiFetch("/api/auth/logout", { method: "POST" });
316
- } catch (e) {
317
- this.ctx.logger.error("Logout error (user still logged out locally)", { data: e });
318
- }
319
- this.ctx.activeUser.value = void 0, this.ctx.token.value = null, this.ctx.storage.clearStorage();
320
- });
321
- }
322
- async getCurrentUser() {
323
- if (this.ctx.token.value)
324
- return k(this.ctx, async () => {
325
- try {
326
- const t = await (await this.ctx.apiFetch("/api/users/me", {
327
- method: "GET"
328
- })).json();
329
- if (!t.ok) {
330
- this.ctx.error.value = t.error, t.error.includes("Authentication") && (this.ctx.activeUser.value = void 0, this.ctx.token.value = null, this.ctx.storage.clearStorage());
331
- return;
332
- }
333
- return this.ctx.token.value && (this.ctx.activeUser.value = t.data), t.data;
334
- } catch (e) {
335
- this.ctx.logger.error("Get current user error", { data: e }), this.ctx.error.value = e instanceof Error ? e.message : "Failed to get user info";
336
- return;
337
- }
338
- });
339
- }
340
- async requestAuthCode(e) {
341
- return k(this.ctx, async () => {
342
- try {
343
- const o = await (await this.ctx.apiFetch("/api/auth/check-email", {
344
- method: "POST",
345
- body: JSON.stringify({ email: e.email })
346
- })).json();
347
- return o.ok ? (this.ctx.logger.info("Auth code requested successfully", { email: e.email }), !0) : (this.ctx.error.value = o.error, this.ctx.logger.error("Failed to request auth code", { email: e.email, error: o.error }), !1);
348
- } catch (t) {
349
- return this.ctx.logger.error("Request auth code error", { email: e.email, error: t }), this.ctx.error.value = t instanceof Error ? t.message : "Failed to request auth code", !1;
350
- }
351
- });
352
- }
353
- async loginWithCode(e) {
354
- return k(this.ctx, async () => {
355
- try {
356
- const o = await (await this.ctx.apiFetch("/api/auth/verify-code", {
357
- method: "POST",
358
- body: JSON.stringify({ email: e.email, code: e.code })
359
- })).json();
360
- return o.ok ? (this.ctx.logger.info("Login successful", { email: e.email }), !0) : (this.ctx.error.value = o.error, this.ctx.logger.error("Login failed", { email: e.email, error: o.error }), !1);
361
- } catch (t) {
362
- return this.ctx.logger.error("Login with code error", { email: e.email, error: t }), this.ctx.error.value = t instanceof Error ? t.message : "Login failed", !1;
363
- }
364
- });
365
- }
366
- loginWithGoogle(e) {
367
- if (typeof window > "u") {
368
- this.ctx.logger.error("loginWithGoogle: Only available in browser");
369
- return;
370
- }
371
- const t = p(this.ctx.apiBase, this.ctx.isDev), o = window.location.origin, r = `${t}/auth/google-popup?origin=${encodeURIComponent(o)}`, s = 500, n = 600, h = window.screenX + (window.outerWidth - s) / 2, a = window.screenY + (window.outerHeight - n) / 2, c = window.open(
372
- r,
373
- "google-auth",
374
- `width=${s},height=${n},left=${h},top=${a},popup=yes`
375
- );
376
- if (!c) {
377
- const m = "Popup was blocked. Please allow popups for this site.";
378
- this.ctx.error.value = m, e?.onError?.(m);
379
- return;
380
- }
381
- const d = /* @__PURE__ */ u((m) => {
382
- const I = new URL(t).origin;
383
- if (m.origin === I) {
384
- if (m.data?.type === "auth-success") {
385
- window.removeEventListener("message", d);
386
- const { token: x, user: g } = m.data;
387
- this.ctx.processApiResponse({ ok: !0, data: {}, token: x, user: g }), this.ctx.logger.info("Google login successful"), e?.onSuccess?.(g);
388
- } else if (m.data?.type === "auth-error") {
389
- window.removeEventListener("message", d);
390
- const x = m.data.error || "Google authentication failed";
391
- this.ctx.error.value = x, this.ctx.logger.error("Google login failed", { error: x }), e?.onError?.(x);
392
- }
393
- }
394
- }, "handler");
395
- window.addEventListener("message", d);
396
- const f = setInterval(() => {
397
- c.closed && (clearInterval(f), window.removeEventListener("message", d));
398
- }, 500);
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
- u(G, "AuthClient");
402
- let R = G;
403
- const W = class W {
404
- constructor(e) {
405
- l(this, "ctx");
406
- this.ctx = e;
407
- }
408
- async startTrial(e) {
409
- const t = this.resolveOrgId(), o = p(this.ctx.apiBase, this.ctx.isDev), s = await (await fetch(`${o}/api/billing/checkout/create`, {
410
- method: "POST",
411
- headers: {
412
- "Content-Type": "application/json",
413
- ...this.ctx.token.value && { Authorization: `Bearer ${this.ctx.token.value}` }
414
- },
415
- body: JSON.stringify({
416
- orgId: t,
417
- ...e?.returnUrl && { returnUrl: e.returnUrl }
418
- })
419
- })).json();
420
- if (!s.ok || !s.data?.url) {
421
- const n = s.error || "Failed to create checkout session";
422
- throw this.ctx.error.value = n, new Error(n);
423
- }
424
- window.location.href = s.data.url;
425
- }
426
- async confirmCheckout(e) {
427
- if (e?.sessionId) {
428
- const t = p(this.ctx.apiBase, this.ctx.isDev), r = await (await fetch(`${t}/api/billing/complete-checkout`, {
429
- method: "POST",
430
- headers: {
431
- "Content-Type": "application/json",
432
- ...this.ctx.token.value && { Authorization: `Bearer ${this.ctx.token.value}` }
433
- },
434
- body: JSON.stringify({ sessionId: e.sessionId })
435
- })).json();
436
- if (!r.ok) {
437
- const s = r.error || "Failed to complete checkout";
438
- throw this.ctx.error.value = s, new Error(s);
439
- }
440
- }
441
- return this.getStatus();
442
- }
443
- async getStatus() {
444
- const e = this.resolveOrgId(), t = p(this.ctx.apiBase, this.ctx.isDev), r = await (await fetch(`${t}/api/billing/details/${e}`, {
445
- headers: {
446
- ...this.ctx.token.value && { Authorization: `Bearer ${this.ctx.token.value}` }
447
- }
448
- })).json();
449
- if (!r.ok || !r.data)
450
- return {
451
- plan: void 0,
452
- status: "none",
453
- hasActiveBilling: !1,
454
- maxAgents: 0
455
- };
456
- const s = ["active", "trial", "canceling"];
457
- return {
458
- plan: r.data.plan,
459
- status: r.data.status,
460
- hasActiveBilling: s.includes(r.data.status),
461
- maxAgents: r.data.maxAgents,
462
- ...r.data.trialEnd && { trialEnd: r.data.trialEnd },
463
- ...r.data.trialDaysRemaining !== void 0 && { trialDaysRemaining: r.data.trialDaysRemaining }
464
- };
465
- }
466
- resolveOrgId() {
467
- const t = this.ctx.activeUser.value?.orgs?.[0]?.orgId;
468
- if (!t)
469
- throw new Error("No organization found. Please sign in first.");
470
- return t;
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
- u(W, "BillingClient");
474
- let L = W;
475
- function de(i) {
476
- const e = i.split(`
477
- `).filter((r) => r.length > 0);
478
- let t, o;
479
- for (const r of e)
480
- r.startsWith("event:") ? t = r.slice(6).trim() : r.startsWith("data:") && (o = r.slice(5).trim());
481
- if (!t || o === void 0)
482
- return null;
483
- try {
484
- const r = ae.safeParse({ event: t, data: JSON.parse(o) });
485
- return r.success ? r.data : null;
486
- } catch {
487
- return null;
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
- u(de, "parseChatEvent");
491
- function Y(i) {
492
- return {
493
- code: i.code,
494
- error: i.message,
495
- ...i.bucket ? { bucket: i.bucket } : {},
496
- ...i.actionLabel ? { actionLabel: i.actionLabel } : {},
497
- ...i.actionUrl ? { actionUrl: i.actionUrl } : {},
498
- ...i.help ? { help: i.help } : {}
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
- u(Y, "toChatStreamError");
502
- function he(i) {
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
- u(he, "toAuthenticatedTransportError");
506
- function ge() {
507
- try {
508
- return Intl.DateTimeFormat().resolvedOptions().timeZone || void 0;
509
- } catch {
510
- return;
511
- }
487
+ function detectBrowserTimezone() {
488
+ try {
489
+ return Intl.DateTimeFormat().resolvedOptions().timeZone || void 0;
490
+ } catch {
491
+ return;
492
+ }
512
493
  }
513
- u(ge, "detectBrowserTimezone");
514
- function X(i) {
515
- if (!(i.role !== "assistant" && i.role !== "system"))
516
- return {
517
- id: i.messageId,
518
- text: i.content,
519
- sender: i.role === "system" ? "system" : "agent",
520
- timestamp: i.createdAt,
521
- conversationId: i.conversationId,
522
- sequence: i.sequence,
523
- systemKind: i.systemKind,
524
- ...i.metadata?.turnOutcome ? { turnOutcome: i.metadata.turnOutcome } : {},
525
- ...i.metadata?.attachments?.length ? { attachments: i.metadata.attachments } : {},
526
- ...i.metadata?.toolActivities?.length ? { toolActivities: i.metadata.toolActivities } : {},
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
- u(X, "toChatMessage");
531
- const q = class q {
532
- constructor(e) {
533
- l(this, "ctx");
534
- this.ctx = e;
535
- }
536
- async cancelConversationTurn(e) {
537
- const t = p(this.ctx.apiBase, this.ctx.isDev), o = this.ctx.token.value ? { Authorization: `Bearer ${this.ctx.token.value}` } : {}, r = await fetch(`${t}/api/conversations/${encodeURIComponent(e.conversationId)}/cancel`, {
538
- method: "POST",
539
- headers: { "Content-Type": "application/json", ...o },
540
- body: JSON.stringify(e.turnId ? { turnId: e.turnId } : {})
541
- }), s = await r.json();
542
- if (!s.ok)
543
- throw new Error(s.error || `Cancel failed: HTTP ${r.status}`);
544
- return s.data;
545
- }
546
- /**
547
- * Public/visitor chat anonymous chat against an agent's public handle,
548
- * routed through the messaging substrate. Same SSE wire format as
549
- * `chatStreamAuthenticated` the only difference is the route (no auth,
550
- * handle-based lookup, anonymousId instead of userId).
551
- *
552
- * The visitor identity is `anonymousId` — caller is responsible for
553
- * generating + persisting it (typically `sdk.user.generateAnonId()`
554
- * which stores a stable ID in localStorage).
555
- */
556
- async chatStreamPublic(e) {
557
- const o = `${p(this.ctx.apiBase, this.ctx.isDev)}/api/agents/public/${encodeURIComponent(e.handle)}/chat/v2/stream`;
558
- let r = "", s = !1, n = !1, h;
559
- try {
560
- const a = await fetch(o, {
561
- method: "POST",
562
- headers: { "Content-Type": "application/json" },
563
- body: JSON.stringify({
564
- message: e.message,
565
- anonymousId: e.anonymousId,
566
- ...e.attachments?.length ? { attachments: e.attachments } : {},
567
- ...e.context ? { context: e.context } : {}
568
- })
569
- });
570
- if (!a.ok) {
571
- const f = await a.json().catch(() => ({ error: "Request failed" }));
572
- e.onError(f.error || `HTTP ${a.status}`);
573
- return;
574
- }
575
- if (r = a.headers.get("X-Conversation-Id") || "", h = a.body?.getReader(), !h) {
576
- e.onError("No response stream");
577
- return;
578
- }
579
- const c = new TextDecoder();
580
- let d = "";
581
- e: for (; ; ) {
582
- const { done: f, value: m } = await h.read();
583
- if (f)
584
- break;
585
- d += c.decode(m, { stream: !0 });
586
- let I = d.indexOf(`
587
-
588
- `);
589
- for (; I !== -1; ) {
590
- const x = d.slice(0, I);
591
- d = d.slice(I + 2);
592
- const g = de(x);
593
- if (g) {
594
- if (g.event === "working_state")
595
- e.onStatus?.(g.data.description);
596
- else if (g.event === "tool_activity")
597
- e.onToolActivity?.(g.data);
598
- else if (g.event === "working_end")
599
- e.onWorkingEnd?.();
600
- else if (g.event === "message_delta")
601
- e.onDelta(g.data.delta, g.data.role);
602
- else if (g.event === "error") {
603
- s = !0, e.onError(Y(g.data));
604
- break e;
605
- } else if (g.event === "message" && (g.data.message.role === "assistant" || g.data.message.role === "system")) {
606
- n = !0;
607
- const K = X(g.data.message);
608
- K && e.onMessage?.(K);
609
- } else if (g.event === "stream_end")
610
- break e;
611
- }
612
- I = d.indexOf(`
613
-
614
- `);
615
- }
616
- }
617
- !s && n ? e.onDone(r) : s || e.onError("Stream ended before reply arrived");
618
- } catch (a) {
619
- e.onError(a instanceof Error ? a.message : "Stream failed");
620
- } finally {
621
- try {
622
- await h?.cancel();
623
- } catch {
624
- }
625
- }
626
- }
627
- /**
628
- * Authenticated chat — substrate-backed:
629
- * POST /api/conversations, open SSE from that conversation's latest
630
- * sequence, then POST /api/messages. Opening the stream before send keeps
631
- * fast working_state / message_delta frames from outrunning the subscriber.
632
- * Same callback contract as `chatStream` so callers don't need transport details.
633
- * Mirrors `src/modules/agent/client.ts sendChatMessageStream`.
634
- */
635
- async chatStreamAuthenticated(e) {
636
- const t = p(this.ctx.apiBase, this.ctx.isDev), o = this.ctx.token.value ? { Authorization: `Bearer ${this.ctx.token.value}` } : {};
637
- let r, s = null;
638
- try {
639
- const c = await fetch(`${t}/api/conversations`, {
640
- method: "POST",
641
- headers: { "Content-Type": "application/json", ...o },
642
- body: JSON.stringify({ agentId: e.agentId })
643
- }), d = await c.json();
644
- if (!d.ok) {
645
- e.onError(d.error || `Conversation failed: HTTP ${c.status}`);
646
- return;
647
- }
648
- r = d.data.conversationId, s = d.data.latestSequence ?? null;
649
- } catch (c) {
650
- e.onError(c instanceof Error ? c.message : "Conversation failed");
651
- return;
652
- }
653
- const n = s ? `${t}/api/conversations/${encodeURIComponent(r)}/stream?since=${encodeURIComponent(s)}` : `${t}/api/conversations/${encodeURIComponent(r)}/stream`, h = `nonce_${se(16)}`, a = ge();
654
- await ie({
655
- streamUrl: n,
656
- sendUrl: `${t}/api/messages`,
657
- headers: o,
658
- sendBody: {
659
- conversationId: r,
660
- content: e.message,
661
- clientNonce: h,
662
- ...a ? { viewerTimezone: a } : {},
663
- ...e.attachments && e.attachments.length > 0 ? { attachments: e.attachments } : {}
664
- },
665
- onStatus: e.onStatus,
666
- onDelta: e.onDelta,
667
- onToolActivity: e.onToolActivity,
668
- onWorkingEnd: e.onWorkingEnd,
669
- onMessage: /* @__PURE__ */ u((c) => {
670
- const d = X(c);
671
- d && e.onMessage?.(d);
672
- }, "onMessage"),
673
- onDone: /* @__PURE__ */ u(() => e.onDone(r), "onDone"),
674
- onChatError: /* @__PURE__ */ u((c) => e.onError(Y(c)), "onChatError"),
675
- // A coded rejection (e.g. TURN_ACTIVE) stays structured so the
676
- // controller can switch on it; only uncoded transport failures
677
- // flatten to the legacy string path.
678
- onTransportError: /* @__PURE__ */ u((c) => e.onError(c.code ? { code: c.code, error: c.message } : he(c)), "onTransportError")
679
- });
680
- }
681
- };
682
- u(q, "ChatClient");
683
- let j = q;
684
- const M = class M {
685
- constructor(e) {
686
- l(this, "ctx");
687
- this.ctx = e;
688
- }
689
- async getPublicAgent(e) {
690
- this.ctx.loading.value = !0, this.ctx.error.value = null;
691
- try {
692
- const o = await (await this.ctx.apiFetch(`/api/agents/public/${encodeURIComponent(e.handle)}`)).json();
693
- if (!o.ok) {
694
- this.ctx.error.value = o.error, this.ctx.logger.error("Failed to fetch public agent", { handle: e.handle, error: o.error });
695
- return;
696
- }
697
- return o.data;
698
- } catch (t) {
699
- this.ctx.logger.error("Get public agent error", { handle: e.handle, error: t }), this.ctx.error.value = t instanceof Error ? t.message : "Failed to fetch agent";
700
- return;
701
- } finally {
702
- this.ctx.loading.value = !1;
703
- }
704
- }
705
- async getAgentByEmail(e) {
706
- this.ctx.loading.value = !0, this.ctx.error.value = null;
707
- try {
708
- const o = await (await this.ctx.apiFetch(`/api/agents/by-email/${encodeURIComponent(e.email)}`)).json();
709
- if (!o.ok) {
710
- this.ctx.error.value = o.error, this.ctx.logger.error("Failed to fetch agent by email", { email: e.email, error: o.error });
711
- return;
712
- }
713
- return o.data;
714
- } catch (t) {
715
- this.ctx.logger.error("Get agent by email error", { email: e.email, error: t }), this.ctx.error.value = t instanceof Error ? t.message : "Failed to fetch agent";
716
- return;
717
- } finally {
718
- this.ctx.loading.value = !1;
719
- }
720
- }
721
- generateAnonId() {
722
- const e = "pagelinesAnonId";
723
- let t = this.ctx.storage.getItem(e);
724
- return t || (t = D({ prefix: "anon" }), this.ctx.storage.setItem(e, t)), t;
725
- }
726
- track(e) {
727
- try {
728
- typeof window < "u" && window.parent !== window && window.parent.postMessage({
729
- type: "pagelines_track",
730
- event: e.event,
731
- agentId: e.agentId,
732
- properties: e.properties || {}
733
- }, "*"), this.ctx.logger.info("SDK event tracked", { event: e.event, agentId: e.agentId });
734
- } catch (t) {
735
- this.ctx.logger.error("Track event error", { error: t, args: e });
736
- }
737
- }
738
- };
739
- u(M, "UserClient");
740
- let C = M;
741
- const H = class H {
742
- constructor() {
743
- l(this, "key", "__PAGELINES_SDK__");
744
- }
745
- get() {
746
- if (!(typeof window > "u"))
747
- return globalThis[this.key];
748
- }
749
- set(e) {
750
- typeof window < "u" && (globalThis[this.key] = e);
751
- }
752
- delete() {
753
- typeof window < "u" && delete globalThis[this.key];
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
- u(H, "SDKGlobal");
757
- let N = H;
758
- const $ = new N(), O = class O extends ne {
759
- constructor(t = {}) {
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