@pagelines/sdk 1.0.661 → 1.0.662

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/sdkClient.js CHANGED
@@ -1,38 +1,38 @@
1
1
  var Q = Object.defineProperty;
2
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 l = (i, e) => Q(i, "name", { value: e, configurable: !0 });
4
- var c = (i, e, t) => ee(i, typeof e != "symbol" ? e + "" : e, t);
5
- import { APP_PORT as te, APP_PROD_URL as re, cookieUtil as S, createLogger as oe, shortId as se, runAuthenticatedChatTurnStream as ie, zChatEvent as ae, objectId as P, SettingsObject as ne } from "@pagelines/core";
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, APP_PROD_URL as oe, cookieUtil as S, createLogger as re, shortId as se, runAuthenticatedChatTurnStream as ie, zChatEvent as ae, objectId as P, SettingsObject as ne } from "@pagelines/core";
6
6
  import { watch as B, ref as b, computed as V } from "vue";
7
7
  function p(i, e) {
8
- return i || (e ? `http://localhost:${te}` : re);
8
+ return i || (e ? `http://localhost:${te}` : oe);
9
9
  }
10
- l(p, "resolveApiBase");
10
+ u(p, "resolveApiBase");
11
11
  function ce(i, e, t) {
12
- const r = p(i.apiBase, i.isDev);
13
- return async (o, s) => {
14
- const n = S.getAuthToken(), d = new Headers(s?.headers);
15
- d.has("Content-Type") || d.set("Content-Type", "application/json"), n && !d.has("Authorization") && d.set("Authorization", `Bearer ${n}`);
16
- const a = typeof o == "string" && o.startsWith("/") ? `${r}${o}` : o, u = await fetch(a, {
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
17
  ...s,
18
- headers: d,
18
+ headers: h,
19
19
  credentials: "include"
20
20
  });
21
- if (t && u.status === 401 && (await u.clone().json().catch(() => {
21
+ if (t && c.status === 401 && (await c.clone().json().catch(() => {
22
22
  }))?.code === "TOKEN_ERROR" && t(), e) {
23
- const g = u.json.bind(u);
24
- u.json = async () => {
25
- const f = await g();
23
+ const d = c.json.bind(c);
24
+ c.json = async () => {
25
+ const f = await d();
26
26
  return typeof f == "object" && f !== null && "ok" in f && ("user" in f || "token" in f) && e(f), f;
27
27
  };
28
28
  }
29
- return u;
29
+ return c;
30
30
  };
31
31
  }
32
- l(ce, "createApiFetch");
32
+ u(ce, "createApiFetch");
33
33
  const le = /* @__PURE__ */ Symbol("clean");
34
34
  let v = [], w = 0;
35
- const U = 4, ue = globalThis.nanostoresGlobal || (globalThis.nanostoresGlobal = { epoch: 0 }), N = /* @__PURE__ */ l(/* @__NO_SIDE_EFFECTS__ */ (i) => {
35
+ const T = 4, ue = globalThis.nanostoresGlobal || (globalThis.nanostoresGlobal = { epoch: 0 }), N = /* @__PURE__ */ u(/* @__NO_SIDE_EFFECTS__ */ (i) => {
36
36
  let e = [], t = {
37
37
  get() {
38
38
  return t.lc || t.listen(() => {
@@ -40,21 +40,21 @@ const U = 4, ue = globalThis.nanostoresGlobal || (globalThis.nanostoresGlobal =
40
40
  },
41
41
  init: i,
42
42
  lc: 0,
43
- listen(r) {
44
- return t.lc = e.push(r), () => {
45
- for (let s = w + U; s < v.length; )
46
- v[s] === r ? v.splice(s, U) : s += U;
47
- let o = e.indexOf(r);
48
- ~o && (e.splice(o, 1), --t.lc || t.off());
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
49
  };
50
50
  },
51
- notify(r, o) {
51
+ notify(o, r) {
52
52
  ue.epoch++;
53
53
  let s = !v.length;
54
54
  for (let n of e)
55
- v.push(n, t.value, r, o);
55
+ v.push(n, t.value, o, r);
56
56
  if (s) {
57
- for (w = 0; w < v.length; w += U)
57
+ for (w = 0; w < v.length; w += T)
58
58
  v[w](
59
59
  v[w + 1],
60
60
  v[w + 2],
@@ -67,22 +67,22 @@ const U = 4, ue = globalThis.nanostoresGlobal || (globalThis.nanostoresGlobal =
67
67
  We will redefine it in onMount and onStop. */
68
68
  off() {
69
69
  },
70
- set(r) {
71
- let o = t.value;
72
- o !== r && (t.value = r, t.notify(o));
70
+ set(o) {
71
+ let r = t.value;
72
+ r !== o && (t.value = o, t.notify(r));
73
73
  },
74
- subscribe(r) {
75
- let o = t.listen(r);
76
- return r(t.value), o;
74
+ subscribe(o) {
75
+ let r = t.listen(o);
76
+ return o(t.value), r;
77
77
  },
78
78
  value: i
79
79
  };
80
80
  return process.env.NODE_ENV !== "production" && (t[le] = () => {
81
81
  e = [], t.lc = 0, t.off();
82
82
  }), t;
83
- }, "atom"), y = globalThis, E = y.__PL_AUTH_USER__ ?? (y.__PL_AUTH_USER__ = /* @__PURE__ */ N(void 0)), T = y.__PL_AUTH_TOKEN__ ?? (y.__PL_AUTH_TOKEN__ = /* @__PURE__ */ N(null)), A = y.__PL_AUTH_DEVICE_ID__ ?? (y.__PL_AUTH_DEVICE_ID__ = /* @__PURE__ */ N(null)), z = class z {
83
+ }, "atom"), y = globalThis, U = y.__PL_AUTH_USER__ ?? (y.__PL_AUTH_USER__ = /* @__PURE__ */ N(void 0)), E = y.__PL_AUTH_TOKEN__ ?? (y.__PL_AUTH_TOKEN__ = /* @__PURE__ */ N(null)), A = y.__PL_AUTH_DEVICE_ID__ ?? (y.__PL_AUTH_DEVICE_ID__ = /* @__PURE__ */ N(null)), z = class z {
84
84
  constructor() {
85
- c(this, "logger", oe("SDKStorage"));
85
+ l(this, "logger", re("SDKStorage"));
86
86
  }
87
87
  /**
88
88
  * Check if localStorage is fully functional (some test environments provide partial mocks)
@@ -94,13 +94,13 @@ const U = 4, ue = globalThis.nanostoresGlobal || (globalThis.nanostoresGlobal =
94
94
  * Sync Vue reactive refs with global nanostores for cross-bundle persistence.
95
95
  */
96
96
  syncWithGlobalStores(e) {
97
- const { activeUser: t, token: r, deviceId: o } = e, s = E.get(), n = T.get(), d = A.get();
98
- s && (t.value = s), n && (r.value = n), d && (o.value = d), B(t, (a) => E.set(a), { immediate: !0 }), B(r, (a) => T.set(a), { immediate: !0 }), B(o, (a) => A.set(a), { immediate: !0 }), E.subscribe((a) => {
97
+ const { activeUser: t, token: o, deviceId: r } = e, s = U.get(), n = E.get(), h = A.get();
98
+ s && (t.value = s), n && (o.value = n), h && (r.value = h), B(t, (a) => U.set(a), { immediate: !0 }), B(o, (a) => E.set(a), { immediate: !0 }), B(r, (a) => A.set(a), { immediate: !0 }), U.subscribe((a) => {
99
99
  a !== t.value && (t.value = a);
100
- }), T.subscribe((a) => {
101
- a !== r.value && (r.value = a);
102
- }), A.subscribe((a) => {
100
+ }), E.subscribe((a) => {
103
101
  a !== o.value && (o.value = a);
102
+ }), A.subscribe((a) => {
103
+ a !== r.value && (r.value = a);
104
104
  });
105
105
  }
106
106
  /**
@@ -112,13 +112,13 @@ const U = 4, ue = globalThis.nanostoresGlobal || (globalThis.nanostoresGlobal =
112
112
  return;
113
113
  const t = S.getAuthToken();
114
114
  t && (e.token.value = t);
115
- const r = S.getDeviceId();
116
- if (r && (e.deviceId.value = r), this.isLocalStorageFunctional())
115
+ const o = S.getDeviceId();
116
+ if (o && (e.deviceId.value = o), this.isLocalStorageFunctional())
117
117
  try {
118
- const o = localStorage.getItem("pagelines-user");
119
- o && (e.activeUser.value = JSON.parse(o));
120
- } catch (o) {
121
- this.logger.error("Failed to load user from localStorage", { data: o });
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
122
  }
123
123
  }
124
124
  /**
@@ -146,7 +146,7 @@ const U = 4, ue = globalThis.nanostoresGlobal || (globalThis.nanostoresGlobal =
146
146
  } catch (e) {
147
147
  this.logger.error("Failed to clear user from localStorage", { data: e });
148
148
  }
149
- E.set(void 0), T.set(null), A.set(null);
149
+ U.set(void 0), E.set(null), A.set(null);
150
150
  }
151
151
  }
152
152
  /**
@@ -168,20 +168,20 @@ const U = 4, ue = globalThis.nanostoresGlobal || (globalThis.nanostoresGlobal =
168
168
  if (this.isLocalStorageFunctional())
169
169
  try {
170
170
  localStorage.setItem(e, t);
171
- } catch (r) {
172
- this.logger.error("Failed to set item in localStorage", { key: e, error: r });
171
+ } catch (o) {
172
+ this.logger.error("Failed to set item in localStorage", { key: e, error: o });
173
173
  }
174
174
  }
175
175
  };
176
- l(z, "SDKStorage");
176
+ u(z, "SDKStorage");
177
177
  let D = z;
178
178
  const J = class J {
179
179
  constructor(e) {
180
- c(this, "ctx");
180
+ l(this, "ctx");
181
181
  this.ctx = e;
182
182
  }
183
183
  async create(e) {
184
- const t = e.orgId || this.resolveOrgId(), r = p(this.ctx.apiBase, this.ctx.isDev), s = await (await fetch(`${r}/api/agents`, {
184
+ const t = e.orgId || this.resolveOrgId(), o = p(this.ctx.apiBase, this.ctx.isDev), s = await (await fetch(`${o}/api/agents`, {
185
185
  method: "POST",
186
186
  headers: {
187
187
  "Content-Type": "application/json",
@@ -204,16 +204,16 @@ const J = class J {
204
204
  async getLiveStatus(e) {
205
205
  const t = p(this.ctx.apiBase, this.ctx.isDev);
206
206
  try {
207
- const o = await (await fetch(`${t}/api/agents/state?ids=${encodeURIComponent(e.agentId)}`, {
207
+ const r = await (await fetch(`${t}/api/agents/state?ids=${encodeURIComponent(e.agentId)}`, {
208
208
  headers: {
209
209
  ...this.ctx.token.value && { Authorization: `Bearer ${this.ctx.token.value}` }
210
210
  }
211
211
  })).json();
212
- return o.ok ? o.data?.[e.agentId] ?? { lifecycle: "unknown" } : { lifecycle: "unknown" };
213
- } catch (r) {
212
+ return r.ok ? r.data?.[e.agentId] ?? { lifecycle: "unknown" } : { lifecycle: "unknown" };
213
+ } catch (o) {
214
214
  return this.ctx.logger.warn("agent.getLiveStatus failed", {
215
215
  agentId: e.agentId,
216
- error: r instanceof Error ? r.message : String(r)
216
+ error: o instanceof Error ? o.message : String(o)
217
217
  }), { lifecycle: "unknown" };
218
218
  }
219
219
  }
@@ -225,28 +225,28 @@ const J = class J {
225
225
  * — no magic". Map lifecycle values to user copy at the call site.
226
226
  */
227
227
  async waitUntilReady(e) {
228
- const { agentId: t, onStatus: r, timeoutMs: o = 3e5, pollIntervalMs: s = 3e3 } = e, n = Date.now();
229
- let d;
230
- for (; Date.now() - n < o; ) {
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
231
  const a = await this.getLiveStatus({ agentId: t });
232
232
  if (a.lifecycle === "running") {
233
- r?.("running");
233
+ o?.("running");
234
234
  return;
235
235
  }
236
236
  if (a.lifecycle === "error")
237
237
  throw new Error(a.detail || "Your assistant hit a problem starting");
238
- a.lifecycle !== d && (r?.(a.lifecycle), d = a.lifecycle), await new Promise((u) => setTimeout(u, s));
238
+ a.lifecycle !== h && (o?.(a.lifecycle), h = a.lifecycle), await new Promise((c) => setTimeout(c, s));
239
239
  }
240
240
  throw new Error("Your assistant is taking longer than expected — please try again");
241
241
  }
242
242
  /** Send a typed webhook event to a bot agent. No auth required — the proxy is public. */
243
243
  async webhook(e) {
244
- const r = `${p(this.ctx.apiBase, this.ctx.isDev)}/api/bot-api/webhooks/agents/${e.agentId}`, o = { type: e.type, data: e.data };
245
- e.meta && (o.meta = e.meta);
246
- const s = await fetch(r, {
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
247
  method: "POST",
248
248
  headers: { "Content-Type": "application/json" },
249
- body: JSON.stringify(o)
249
+ body: JSON.stringify(r)
250
250
  });
251
251
  if (!s.ok) {
252
252
  const n = await s.text().catch(() => "Unknown error");
@@ -261,8 +261,8 @@ const J = class J {
261
261
  return t;
262
262
  }
263
263
  };
264
- l(J, "AgentClient");
265
- let R = J;
264
+ u(J, "AgentClient");
265
+ let F = J;
266
266
  async function k(i, e) {
267
267
  i.loading.value = !0, i.error.value = null;
268
268
  try {
@@ -271,41 +271,41 @@ async function k(i, e) {
271
271
  i.loading.value = !1;
272
272
  }
273
273
  }
274
- l(k, "withLoadingState");
274
+ u(k, "withLoadingState");
275
275
  const W = class W {
276
276
  constructor(e) {
277
- c(this, "ctx");
277
+ l(this, "ctx");
278
278
  this.ctx = e;
279
279
  }
280
280
  async sendCode(e) {
281
281
  return k(this.ctx, async () => {
282
282
  try {
283
- const r = await (await this.ctx.apiFetch("/api/auth/check-email", {
283
+ const o = await (await this.ctx.apiFetch("/api/auth/check-email", {
284
284
  method: "POST",
285
285
  body: JSON.stringify({ email: e })
286
286
  })).json();
287
- if (!r.ok)
288
- throw this.ctx.error.value = r.error, new Error(r.error);
287
+ if (!o.ok)
288
+ throw this.ctx.error.value = o.error, new Error(o.error);
289
289
  } catch (t) {
290
290
  this.ctx.logger.error("Send code error", { data: t });
291
- const r = t instanceof Error ? t.message : "Failed to send verification code";
292
- throw this.ctx.error.value = r, t;
291
+ const o = t instanceof Error ? t.message : "Failed to send verification code";
292
+ throw this.ctx.error.value = o, t;
293
293
  }
294
294
  });
295
295
  }
296
296
  async verifyCode(e, t) {
297
297
  return k(this.ctx, async () => {
298
298
  try {
299
- const o = await (await this.ctx.apiFetch("/api/auth/verify-code", {
299
+ const r = await (await this.ctx.apiFetch("/api/auth/verify-code", {
300
300
  method: "POST",
301
301
  body: JSON.stringify({ email: e, code: t })
302
302
  })).json();
303
- if (!o.ok)
304
- throw this.ctx.error.value = o.error, new Error(o.error);
305
- } catch (r) {
306
- this.ctx.logger.error("Verify code error", { data: r });
307
- const o = r instanceof Error ? r.message : "Failed to verify code";
308
- throw this.ctx.error.value = o, r;
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
309
  }
310
310
  });
311
311
  }
@@ -340,11 +340,11 @@ const W = class W {
340
340
  async requestAuthCode(e) {
341
341
  return k(this.ctx, async () => {
342
342
  try {
343
- const r = await (await this.ctx.apiFetch("/api/auth/check-email", {
343
+ const o = await (await this.ctx.apiFetch("/api/auth/check-email", {
344
344
  method: "POST",
345
345
  body: JSON.stringify({ email: e.email })
346
346
  })).json();
347
- return r.ok ? (this.ctx.logger.info("Auth code requested successfully", { email: e.email }), !0) : (this.ctx.error.value = r.error, this.ctx.logger.error("Failed to request auth code", { email: e.email, error: r.error }), !1);
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
348
  } catch (t) {
349
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
350
  }
@@ -353,11 +353,11 @@ const W = class W {
353
353
  async loginWithCode(e) {
354
354
  return k(this.ctx, async () => {
355
355
  try {
356
- const r = await (await this.ctx.apiFetch("/api/auth/verify-code", {
356
+ const o = await (await this.ctx.apiFetch("/api/auth/verify-code", {
357
357
  method: "POST",
358
358
  body: JSON.stringify({ email: e.email, code: e.code })
359
359
  })).json();
360
- return r.ok ? (this.ctx.logger.info("Login successful", { email: e.email }), !0) : (this.ctx.error.value = r.error, this.ctx.logger.error("Login failed", { email: e.email, error: r.error }), !1);
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
361
  } catch (t) {
362
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
363
  }
@@ -368,45 +368,45 @@ const W = class W {
368
368
  this.ctx.logger.error("loginWithGoogle: Only available in browser");
369
369
  return;
370
370
  }
371
- const t = p(this.ctx.apiBase, this.ctx.isDev), r = window.location.origin, o = `${t}/auth/google-popup?origin=${encodeURIComponent(r)}`, s = 500, n = 600, d = window.screenX + (window.outerWidth - s) / 2, a = window.screenY + (window.outerHeight - n) / 2, u = window.open(
372
- o,
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
373
  "google-auth",
374
- `width=${s},height=${n},left=${d},top=${a},popup=yes`
374
+ `width=${s},height=${n},left=${h},top=${a},popup=yes`
375
375
  );
376
- if (!u) {
376
+ if (!c) {
377
377
  const m = "Popup was blocked. Please allow popups for this site.";
378
378
  this.ctx.error.value = m, e?.onError?.(m);
379
379
  return;
380
380
  }
381
- const g = /* @__PURE__ */ l((m) => {
381
+ const d = /* @__PURE__ */ u((m) => {
382
382
  const I = new URL(t).origin;
383
383
  if (m.origin === I) {
384
384
  if (m.data?.type === "auth-success") {
385
- window.removeEventListener("message", g);
386
- const { token: x, user: h } = m.data;
387
- this.ctx.processApiResponse({ ok: !0, data: {}, token: x, user: h }), this.ctx.logger.info("Google login successful"), e?.onSuccess?.(h);
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
388
  } else if (m.data?.type === "auth-error") {
389
- window.removeEventListener("message", g);
389
+ window.removeEventListener("message", d);
390
390
  const x = m.data.error || "Google authentication failed";
391
391
  this.ctx.error.value = x, this.ctx.logger.error("Google login failed", { error: x }), e?.onError?.(x);
392
392
  }
393
393
  }
394
394
  }, "handler");
395
- window.addEventListener("message", g);
395
+ window.addEventListener("message", d);
396
396
  const f = setInterval(() => {
397
- u.closed && (clearInterval(f), window.removeEventListener("message", g));
397
+ c.closed && (clearInterval(f), window.removeEventListener("message", d));
398
398
  }, 500);
399
399
  }
400
400
  };
401
- l(W, "AuthClient");
402
- let F = W;
401
+ u(W, "AuthClient");
402
+ let R = W;
403
403
  const G = class G {
404
404
  constructor(e) {
405
- c(this, "ctx");
405
+ l(this, "ctx");
406
406
  this.ctx = e;
407
407
  }
408
408
  async startTrial(e) {
409
- const t = this.resolveOrgId(), r = p(this.ctx.apiBase, this.ctx.isDev), s = await (await fetch(`${r}/api/billing/checkout/create`, {
409
+ const t = this.resolveOrgId(), o = p(this.ctx.apiBase, this.ctx.isDev), s = await (await fetch(`${o}/api/billing/checkout/create`, {
410
410
  method: "POST",
411
411
  headers: {
412
412
  "Content-Type": "application/json",
@@ -425,7 +425,7 @@ const G = class G {
425
425
  }
426
426
  async confirmCheckout(e) {
427
427
  if (e?.sessionId) {
428
- const t = p(this.ctx.apiBase, this.ctx.isDev), o = await (await fetch(`${t}/api/billing/complete-checkout`, {
428
+ const t = p(this.ctx.apiBase, this.ctx.isDev), r = await (await fetch(`${t}/api/billing/complete-checkout`, {
429
429
  method: "POST",
430
430
  headers: {
431
431
  "Content-Type": "application/json",
@@ -433,20 +433,20 @@ const G = class G {
433
433
  },
434
434
  body: JSON.stringify({ sessionId: e.sessionId })
435
435
  })).json();
436
- if (!o.ok) {
437
- const s = o.error || "Failed to complete checkout";
436
+ if (!r.ok) {
437
+ const s = r.error || "Failed to complete checkout";
438
438
  throw this.ctx.error.value = s, new Error(s);
439
439
  }
440
440
  }
441
441
  return this.getStatus();
442
442
  }
443
443
  async getStatus() {
444
- const e = this.resolveOrgId(), t = p(this.ctx.apiBase, this.ctx.isDev), o = await (await fetch(`${t}/api/billing/details/${e}`, {
444
+ const e = this.resolveOrgId(), t = p(this.ctx.apiBase, this.ctx.isDev), r = await (await fetch(`${t}/api/billing/details/${e}`, {
445
445
  headers: {
446
446
  ...this.ctx.token.value && { Authorization: `Bearer ${this.ctx.token.value}` }
447
447
  }
448
448
  })).json();
449
- if (!o.ok || !o.data)
449
+ if (!r.ok || !r.data)
450
450
  return {
451
451
  plan: void 0,
452
452
  status: "none",
@@ -455,12 +455,12 @@ const G = class G {
455
455
  };
456
456
  const s = ["active", "trial", "canceling"];
457
457
  return {
458
- plan: o.data.plan,
459
- status: o.data.status,
460
- hasActiveBilling: s.includes(o.data.status),
461
- maxAgents: o.data.maxAgents,
462
- ...o.data.trialEnd && { trialEnd: o.data.trialEnd },
463
- ...o.data.trialDaysRemaining !== void 0 && { trialDaysRemaining: o.data.trialDaysRemaining }
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
464
  };
465
465
  }
466
466
  resolveOrgId() {
@@ -470,24 +470,24 @@ const G = class G {
470
470
  return t;
471
471
  }
472
472
  };
473
- l(G, "BillingClient");
473
+ u(G, "BillingClient");
474
474
  let L = G;
475
475
  function de(i) {
476
476
  const e = i.split(`
477
- `).filter((o) => o.length > 0);
478
- let t, r;
479
- for (const o of e)
480
- o.startsWith("event:") ? t = o.slice(6).trim() : o.startsWith("data:") && (r = o.slice(5).trim());
481
- if (!t || r === void 0)
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
482
  return null;
483
483
  try {
484
- const o = ae.safeParse({ event: t, data: JSON.parse(r) });
485
- return o.success ? o.data : null;
484
+ const r = ae.safeParse({ event: t, data: JSON.parse(o) });
485
+ return r.success ? r.data : null;
486
486
  } catch {
487
487
  return null;
488
488
  }
489
489
  }
490
- l(de, "parseChatEvent");
490
+ u(de, "parseChatEvent");
491
491
  function Y(i) {
492
492
  return {
493
493
  code: i.code,
@@ -498,11 +498,19 @@ function Y(i) {
498
498
  ...i.help ? { help: i.help } : {}
499
499
  };
500
500
  }
501
- l(Y, "toChatStreamError");
501
+ u(Y, "toChatStreamError");
502
502
  function he(i) {
503
503
  return i.stage === "stream-open" && i.message.startsWith("HTTP ") ? `Stream failed: ${i.message}` : i.message;
504
504
  }
505
- l(he, "toAuthenticatedTransportError");
505
+ u(he, "toAuthenticatedTransportError");
506
+ function ge() {
507
+ try {
508
+ return Intl.DateTimeFormat().resolvedOptions().timeZone || void 0;
509
+ } catch {
510
+ return;
511
+ }
512
+ }
513
+ u(ge, "detectBrowserTimezone");
506
514
  function X(i) {
507
515
  if (!(i.role !== "assistant" && i.role !== "system"))
508
516
  return {
@@ -519,20 +527,20 @@ function X(i) {
519
527
  ...i.metadata?.issue ? { issue: i.metadata.issue } : {}
520
528
  };
521
529
  }
522
- l(X, "toChatMessage");
530
+ u(X, "toChatMessage");
523
531
  const q = class q {
524
532
  constructor(e) {
525
- c(this, "ctx");
533
+ l(this, "ctx");
526
534
  this.ctx = e;
527
535
  }
528
536
  async cancelConversationTurn(e) {
529
- const t = p(this.ctx.apiBase, this.ctx.isDev), r = this.ctx.token.value ? { Authorization: `Bearer ${this.ctx.token.value}` } : {}, o = await fetch(`${t}/api/conversations/${encodeURIComponent(e.conversationId)}/cancel`, {
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`, {
530
538
  method: "POST",
531
- headers: { "Content-Type": "application/json", ...r },
539
+ headers: { "Content-Type": "application/json", ...o },
532
540
  body: JSON.stringify(e.turnId ? { turnId: e.turnId } : {})
533
- }), s = await o.json();
541
+ }), s = await r.json();
534
542
  if (!s.ok)
535
- throw new Error(s.error || `Cancel failed: HTTP ${o.status}`);
543
+ throw new Error(s.error || `Cancel failed: HTTP ${r.status}`);
536
544
  return s.data;
537
545
  }
538
546
  /**
@@ -546,10 +554,10 @@ const q = class q {
546
554
  * which stores a stable ID in localStorage).
547
555
  */
548
556
  async chatStreamPublic(e) {
549
- const r = `${p(this.ctx.apiBase, this.ctx.isDev)}/api/agents/public/${encodeURIComponent(e.handle)}/chat/v2/stream`;
550
- let o = "", s = !1, n = !1, d;
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;
551
559
  try {
552
- const a = await fetch(r, {
560
+ const a = await fetch(o, {
553
561
  method: "POST",
554
562
  headers: { "Content-Type": "application/json" },
555
563
  body: JSON.stringify({
@@ -564,54 +572,54 @@ const q = class q {
564
572
  e.onError(f.error || `HTTP ${a.status}`);
565
573
  return;
566
574
  }
567
- if (o = a.headers.get("X-Conversation-Id") || "", d = a.body?.getReader(), !d) {
575
+ if (r = a.headers.get("X-Conversation-Id") || "", h = a.body?.getReader(), !h) {
568
576
  e.onError("No response stream");
569
577
  return;
570
578
  }
571
- const u = new TextDecoder();
572
- let g = "";
579
+ const c = new TextDecoder();
580
+ let d = "";
573
581
  e: for (; ; ) {
574
- const { done: f, value: m } = await d.read();
582
+ const { done: f, value: m } = await h.read();
575
583
  if (f)
576
584
  break;
577
- g += u.decode(m, { stream: !0 });
578
- let I = g.indexOf(`
585
+ d += c.decode(m, { stream: !0 });
586
+ let I = d.indexOf(`
579
587
 
580
588
  `);
581
589
  for (; I !== -1; ) {
582
- const x = g.slice(0, I);
583
- g = g.slice(I + 2);
584
- const h = de(x);
585
- if (h) {
586
- if (h.event === "working_state")
587
- e.onStatus?.(h.data.description);
588
- else if (h.event === "tool_activity")
589
- e.onToolActivity?.(h.data);
590
- else if (h.event === "working_end")
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")
591
599
  e.onWorkingEnd?.();
592
- else if (h.event === "message_delta")
593
- e.onDelta(h.data.delta, h.data.role);
594
- else if (h.event === "error") {
595
- s = !0, e.onError(Y(h.data));
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));
596
604
  break e;
597
- } else if (h.event === "message" && (h.data.message.role === "assistant" || h.data.message.role === "system")) {
605
+ } else if (g.event === "message" && (g.data.message.role === "assistant" || g.data.message.role === "system")) {
598
606
  n = !0;
599
- const K = X(h.data.message);
607
+ const K = X(g.data.message);
600
608
  K && e.onMessage?.(K);
601
- } else if (h.event === "stream_end")
609
+ } else if (g.event === "stream_end")
602
610
  break e;
603
611
  }
604
- I = g.indexOf(`
612
+ I = d.indexOf(`
605
613
 
606
614
  `);
607
615
  }
608
616
  }
609
- !s && n ? e.onDone(o) : s || e.onError("Stream ended before reply arrived");
617
+ !s && n ? e.onDone(r) : s || e.onError("Stream ended before reply arrived");
610
618
  } catch (a) {
611
619
  e.onError(a instanceof Error ? a.message : "Stream failed");
612
620
  } finally {
613
621
  try {
614
- await d?.cancel();
622
+ await h?.cancel();
615
623
  } catch {
616
624
  }
617
625
  }
@@ -625,67 +633,68 @@ const q = class q {
625
633
  * Mirrors `src/modules/agent/client.ts → sendChatMessageStream`.
626
634
  */
627
635
  async chatStreamAuthenticated(e) {
628
- const t = p(this.ctx.apiBase, this.ctx.isDev), r = this.ctx.token.value ? { Authorization: `Bearer ${this.ctx.token.value}` } : {};
629
- let o, s = null;
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;
630
638
  try {
631
- const a = await fetch(`${t}/api/conversations`, {
639
+ const c = await fetch(`${t}/api/conversations`, {
632
640
  method: "POST",
633
- headers: { "Content-Type": "application/json", ...r },
641
+ headers: { "Content-Type": "application/json", ...o },
634
642
  body: JSON.stringify({ agentId: e.agentId })
635
- }), u = await a.json();
636
- if (!u.ok) {
637
- e.onError(u.error || `Conversation failed: HTTP ${a.status}`);
643
+ }), d = await c.json();
644
+ if (!d.ok) {
645
+ e.onError(d.error || `Conversation failed: HTTP ${c.status}`);
638
646
  return;
639
647
  }
640
- o = u.data.conversationId, s = u.data.latestSequence ?? null;
641
- } catch (a) {
642
- e.onError(a instanceof Error ? a.message : "Conversation failed");
648
+ r = d.data.conversationId, s = d.data.latestSequence ?? null;
649
+ } catch (c) {
650
+ e.onError(c instanceof Error ? c.message : "Conversation failed");
643
651
  return;
644
652
  }
645
- const n = s ? `${t}/api/conversations/${encodeURIComponent(o)}/stream?since=${encodeURIComponent(s)}` : `${t}/api/conversations/${encodeURIComponent(o)}/stream`, d = `nonce_${se(16)}`;
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();
646
654
  await ie({
647
655
  streamUrl: n,
648
656
  sendUrl: `${t}/api/messages`,
649
- headers: r,
657
+ headers: o,
650
658
  sendBody: {
651
- conversationId: o,
659
+ conversationId: r,
652
660
  content: e.message,
653
- clientNonce: d,
661
+ clientNonce: h,
662
+ ...a ? { viewerTimezone: a } : {},
654
663
  ...e.attachments && e.attachments.length > 0 ? { attachments: e.attachments } : {}
655
664
  },
656
665
  onStatus: e.onStatus,
657
666
  onDelta: e.onDelta,
658
667
  onToolActivity: e.onToolActivity,
659
668
  onWorkingEnd: e.onWorkingEnd,
660
- onMessage: /* @__PURE__ */ l((a) => {
661
- const u = X(a);
662
- u && e.onMessage?.(u);
669
+ onMessage: /* @__PURE__ */ u((c) => {
670
+ const d = X(c);
671
+ d && e.onMessage?.(d);
663
672
  }, "onMessage"),
664
- onDone: /* @__PURE__ */ l(() => e.onDone(o), "onDone"),
665
- onChatError: /* @__PURE__ */ l((a) => e.onError(Y(a)), "onChatError"),
673
+ onDone: /* @__PURE__ */ u(() => e.onDone(r), "onDone"),
674
+ onChatError: /* @__PURE__ */ u((c) => e.onError(Y(c)), "onChatError"),
666
675
  // A coded rejection (e.g. TURN_ACTIVE) stays structured so the
667
676
  // controller can switch on it; only uncoded transport failures
668
677
  // flatten to the legacy string path.
669
- onTransportError: /* @__PURE__ */ l((a) => e.onError(a.code ? { code: a.code, error: a.message } : he(a)), "onTransportError")
678
+ onTransportError: /* @__PURE__ */ u((c) => e.onError(c.code ? { code: c.code, error: c.message } : he(c)), "onTransportError")
670
679
  });
671
680
  }
672
681
  };
673
- l(q, "ChatClient");
682
+ u(q, "ChatClient");
674
683
  let j = q;
675
684
  const M = class M {
676
685
  constructor(e) {
677
- c(this, "ctx");
686
+ l(this, "ctx");
678
687
  this.ctx = e;
679
688
  }
680
689
  async getPublicAgent(e) {
681
690
  this.ctx.loading.value = !0, this.ctx.error.value = null;
682
691
  try {
683
- const r = await (await this.ctx.apiFetch(`/api/agents/public/${encodeURIComponent(e.handle)}`)).json();
684
- if (!r.ok) {
685
- this.ctx.error.value = r.error, this.ctx.logger.error("Failed to fetch public agent", { handle: e.handle, error: r.error });
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 });
686
695
  return;
687
696
  }
688
- return r.data;
697
+ return o.data;
689
698
  } catch (t) {
690
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";
691
700
  return;
@@ -696,12 +705,12 @@ const M = class M {
696
705
  async getAgentByEmail(e) {
697
706
  this.ctx.loading.value = !0, this.ctx.error.value = null;
698
707
  try {
699
- const r = await (await this.ctx.apiFetch(`/api/agents/by-email/${encodeURIComponent(e.email)}`)).json();
700
- if (!r.ok) {
701
- this.ctx.error.value = r.error, this.ctx.logger.error("Failed to fetch agent by email", { email: e.email, error: r.error });
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 });
702
711
  return;
703
712
  }
704
- return r.data;
713
+ return o.data;
705
714
  } catch (t) {
706
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";
707
716
  return;
@@ -727,11 +736,11 @@ const M = class M {
727
736
  }
728
737
  }
729
738
  };
730
- l(M, "UserClient");
739
+ u(M, "UserClient");
731
740
  let C = M;
732
741
  const H = class H {
733
742
  constructor() {
734
- c(this, "key", "__PAGELINES_SDK__");
743
+ l(this, "key", "__PAGELINES_SDK__");
735
744
  }
736
745
  get() {
737
746
  if (!(typeof window > "u"))
@@ -744,53 +753,53 @@ const H = class H {
744
753
  typeof window < "u" && delete globalThis[this.key];
745
754
  }
746
755
  };
747
- l(H, "SDKGlobal");
756
+ u(H, "SDKGlobal");
748
757
  let _ = H;
749
758
  const $ = new _(), O = class O extends ne {
750
759
  constructor(t = {}) {
751
- const r = $.get();
752
- if (r)
753
- return console.debug("[PageLinesSDK] Returning existing singleton instance"), r;
760
+ const o = $.get();
761
+ if (o)
762
+ return console.debug("[PageLinesSDK] Returning existing singleton instance"), o;
754
763
  super("PageLinesSDK", t);
755
764
  // Vue reactive state
756
- c(this, "activeUser", b());
757
- c(this, "token", b(null));
765
+ l(this, "activeUser", b());
766
+ l(this, "token", b(null));
758
767
  // Opaque session token for Bearer clients. Web may also have the same
759
768
  // credential in the HttpOnly pl-session cookie, which JS cannot read.
760
- c(this, "deviceId", b(null));
761
- c(this, "loading", b(!1));
762
- c(this, "error", b(null));
763
- c(this, "apiBase", this.settings.apiBase || void 0);
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);
764
773
  // Storage handler for persistence
765
- c(this, "storage", new D());
774
+ l(this, "storage", new D());
766
775
  // Auto-managed session ID for usage tracking
767
- c(this, "sessionId", P({ prefix: "ses" }));
776
+ l(this, "sessionId", P({ prefix: "ses" }));
768
777
  // Computed properties derived from activeUser (matches UserClient pattern)
769
- c(this, "currentAgent", V(() => {
778
+ l(this, "currentAgent", V(() => {
770
779
  const t = this.activeUser.value;
771
780
  if (!t?.agents)
772
781
  return;
773
- const r = t.primaryAgentId || t.agents[0]?.agentId;
774
- if (r)
775
- return t.agents.find((o) => o.agentId === r);
782
+ const o = t.primaryAgentId || t.agents[0]?.agentId;
783
+ if (o)
784
+ return t.agents.find((r) => r.agentId === o);
776
785
  }));
777
- c(this, "currentOrg", V(() => {
786
+ l(this, "currentOrg", V(() => {
778
787
  const t = this.currentAgent.value;
779
788
  if (!t?.orgId)
780
789
  return;
781
- const r = this.activeUser.value;
782
- if (r?.orgs)
783
- return r.orgs.find((o) => o.orgId === t.orgId);
790
+ const o = this.activeUser.value;
791
+ if (o?.orgs)
792
+ return o.orgs.find((r) => r.orgId === t.orgId);
784
793
  }));
785
794
  // Initialization state
786
- c(this, "initialized");
787
- c(this, "resolveUser");
795
+ l(this, "initialized");
796
+ l(this, "resolveUser");
788
797
  // Sub-clients (new preferred API surface)
789
- c(this, "agent");
790
- c(this, "auth");
791
- c(this, "billing");
792
- c(this, "chat");
793
- c(this, "user");
798
+ l(this, "agent");
799
+ l(this, "auth");
800
+ l(this, "billing");
801
+ l(this, "chat");
802
+ l(this, "user");
794
803
  $.set(this);
795
804
  const s = {
796
805
  apiFetch: ce(
@@ -810,7 +819,7 @@ const $ = new _(), O = class O extends ne {
810
819
  logger: this.logger,
811
820
  processApiResponse: this.processApiResponse.bind(this)
812
821
  };
813
- this.agent = new R(s), this.auth = new F(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) => {
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) => {
814
823
  this.resolveUser = n;
815
824
  }), this.token.value && !this.activeUser.value ? this.auth.getCurrentUser().then((n) => {
816
825
  this.resolveUser && (this.resolveUser(n), this.resolveUser = void 0);
@@ -832,10 +841,10 @@ const $ = new _(), O = class O extends ne {
832
841
  // Process ApiResponse for automatic user/token updates - public for dependency injection
833
842
  processApiResponse(t) {
834
843
  if (!t.ok) return;
835
- let r = !1;
836
- t.user && (this.activeUser.value = t.user, this.logger.info("User updated from API response", { data: t.user }), r = !0), t.token && (this.token.value = t.token, this.logger.info("Token updated from API response"), r = !0);
837
- const o = t.data?.deviceId;
838
- o && (this.deviceId.value = o, r = !0), r && this.persistAuth(), this.resolveUser && (this.resolveUser(t.user), this.resolveUser = void 0);
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);
839
848
  }
840
849
  authRefs() {
841
850
  return {
@@ -856,15 +865,15 @@ const $ = new _(), O = class O extends ne {
856
865
  this.logger.info("Clearing SDK completely"), this.clearSession(), this.loading.value = !1, this.sessionId = P({ prefix: "ses" }), $.delete();
857
866
  }
858
867
  };
859
- l(O, "PageLinesSDK");
868
+ u(O, "PageLinesSDK");
860
869
  let Z = O;
861
870
  export {
862
- R as A,
871
+ F as A,
863
872
  L as B,
864
873
  j as C,
865
874
  Z as P,
866
875
  C as U,
867
- F as a,
876
+ R as a,
868
877
  p as r
869
878
  };
870
879
  //# sourceMappingURL=sdkClient.js.map