@pagelines/sdk 1.0.488 → 1.0.490

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,28 +1,28 @@
1
1
  var V = Object.defineProperty;
2
- var ee = (l, e, t) => e in l ? V(l, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : l[e] = t;
3
- var g = (l, e) => V(l, "name", { value: e, configurable: !0 });
4
- var n = (l, e, t) => ee(l, typeof e != "symbol" ? e + "" : e, t);
5
- import { APP_PORT as te, APP_PROD_URL as re, createRefreshingFetch as se, cookieUtil as b, createLogger as oe, shortId as ae, objectId as R, SettingsObject as ie } from "@pagelines/core";
6
- import { watch as P, ref as A, computed as Q } from "vue";
7
- import { hc as ne } from "hono/client";
8
- function w(l, e) {
9
- return l || (e ? `http://localhost:${te}` : re);
2
+ var Z = (l, e, t) => e in l ? V(l, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : l[e] = t;
3
+ var h = (l, e) => V(l, "name", { value: e, configurable: !0 });
4
+ var n = (l, e, t) => Z(l, typeof e != "symbol" ? e + "" : e, t);
5
+ import { APP_PORT as ee, APP_PROD_URL as te, createRefreshingFetch as re, cookieUtil as b, createLogger as se, shortId as oe, objectId as O, SettingsObject as ae } from "@pagelines/core";
6
+ import { watch as R, ref as E, computed as Q } from "vue";
7
+ import { hc as ie } from "hono/client";
8
+ function x(l, e) {
9
+ return l || (e ? `http://localhost:${ee}` : te);
10
10
  }
11
- g(w, "resolveApiBase");
12
- function ce(l, e, t) {
13
- const r = w(l.apiBase, l.isDev), s = t ? se({
11
+ h(x, "resolveApiBase");
12
+ function ne(l, e, t) {
13
+ const r = x(l.apiBase, l.isDev), s = t ? re({
14
14
  fetch: globalThis.fetch.bind(globalThis),
15
15
  getDeviceId: t.getDeviceId,
16
16
  refreshUrl: `${r}/api/auth/refresh`,
17
17
  onAccessTokenRefreshed: t.onAccessTokenRefreshed,
18
18
  onTerminalFailure: t.onTerminalFailure
19
19
  }) : globalThis.fetch.bind(globalThis);
20
- return ne(r, {
21
- fetch: /* @__PURE__ */ g(async (i, d) => {
20
+ return ie(r, {
21
+ fetch: /* @__PURE__ */ h(async (i, g) => {
22
22
  const a = b.getAuthToken(), c = await s(i, {
23
- ...d,
23
+ ...g,
24
24
  headers: {
25
- ...d?.headers,
25
+ ...g?.headers,
26
26
  "Content-Type": "application/json",
27
27
  // Only include Authorization header if token exists
28
28
  ...a && { Authorization: `Bearer ${a}` }
@@ -34,20 +34,20 @@ function ce(l, e, t) {
34
34
  credentials: "include"
35
35
  });
36
36
  if (e) {
37
- const h = c.json.bind(c);
37
+ const u = c.json.bind(c);
38
38
  c.json = async () => {
39
- const u = await h();
40
- return typeof u == "object" && u !== null && "ok" in u && ("user" in u || "token" in u) && e(u), u;
39
+ const d = await u();
40
+ return typeof d == "object" && d !== null && "ok" in d && ("user" in d || "token" in d) && e(d), d;
41
41
  };
42
42
  }
43
43
  return c;
44
44
  }, "fetch")
45
45
  });
46
46
  }
47
- g(ce, "createApiClient");
48
- const le = /* @__PURE__ */ Symbol("clean");
49
- let m = [], k = 0;
50
- const U = 4, ue = globalThis.nanostoresGlobal || (globalThis.nanostoresGlobal = { epoch: 0 }), G = /* @__PURE__ */ g(/* @__NO_SIDE_EFFECTS__ */ (l) => {
47
+ h(ne, "createApiClient");
48
+ const ce = /* @__PURE__ */ Symbol("clean");
49
+ let m = [], I = 0;
50
+ const U = 4, le = globalThis.nanostoresGlobal || (globalThis.nanostoresGlobal = { epoch: 0 }), G = /* @__PURE__ */ h(/* @__NO_SIDE_EFFECTS__ */ (l) => {
51
51
  let e = [], t = {
52
52
  get() {
53
53
  return t.lc || t.listen(() => {
@@ -57,23 +57,23 @@ const U = 4, ue = globalThis.nanostoresGlobal || (globalThis.nanostoresGlobal =
57
57
  lc: 0,
58
58
  listen(r) {
59
59
  return t.lc = e.push(r), () => {
60
- for (let o = k + U; o < m.length; )
60
+ for (let o = I + U; o < m.length; )
61
61
  m[o] === r ? m.splice(o, U) : o += U;
62
62
  let s = e.indexOf(r);
63
63
  ~s && (e.splice(s, 1), --t.lc || t.off());
64
64
  };
65
65
  },
66
66
  notify(r, s) {
67
- ue.epoch++;
67
+ le.epoch++;
68
68
  let o = !m.length;
69
69
  for (let i of e)
70
70
  m.push(i, t.value, r, s);
71
71
  if (o) {
72
- for (k = 0; k < m.length; k += U)
73
- m[k](
74
- m[k + 1],
75
- m[k + 2],
76
- m[k + 3]
72
+ for (I = 0; I < m.length; I += U)
73
+ m[I](
74
+ m[I + 1],
75
+ m[I + 2],
76
+ m[I + 3]
77
77
  );
78
78
  m.length = 0;
79
79
  }
@@ -92,12 +92,12 @@ const U = 4, ue = globalThis.nanostoresGlobal || (globalThis.nanostoresGlobal =
92
92
  },
93
93
  value: l
94
94
  };
95
- return process.env.NODE_ENV !== "production" && (t[le] = () => {
95
+ return process.env.NODE_ENV !== "production" && (t[ce] = () => {
96
96
  e = [], t.lc = 0, t.off();
97
97
  }), t;
98
- }, "atom"), x = globalThis, E = x.__PL_AUTH_USER__ ?? (x.__PL_AUTH_USER__ = /* @__PURE__ */ G(void 0)), T = x.__PL_AUTH_TOKEN__ ?? (x.__PL_AUTH_TOKEN__ = /* @__PURE__ */ G(null)), $ = x.__PL_AUTH_DEVICE_ID__ ?? (x.__PL_AUTH_DEVICE_ID__ = /* @__PURE__ */ G(null)), W = class W {
98
+ }, "atom"), y = globalThis, T = y.__PL_AUTH_USER__ ?? (y.__PL_AUTH_USER__ = /* @__PURE__ */ G(void 0)), A = y.__PL_AUTH_TOKEN__ ?? (y.__PL_AUTH_TOKEN__ = /* @__PURE__ */ G(null)), $ = y.__PL_AUTH_DEVICE_ID__ ?? (y.__PL_AUTH_DEVICE_ID__ = /* @__PURE__ */ G(null)), z = class z {
99
99
  constructor() {
100
- n(this, "logger", oe("SDKStorage"));
100
+ n(this, "logger", se("SDKStorage"));
101
101
  }
102
102
  /**
103
103
  * Check if localStorage is fully functional (some test environments provide partial mocks)
@@ -109,10 +109,10 @@ const U = 4, ue = globalThis.nanostoresGlobal || (globalThis.nanostoresGlobal =
109
109
  * Sync Vue reactive refs with global nanostores for cross-bundle persistence.
110
110
  */
111
111
  syncWithGlobalStores(e) {
112
- const { activeUser: t, token: r, deviceId: s } = e, o = E.get(), i = T.get(), d = $.get();
113
- o && (t.value = o), i && (r.value = i), d && (s.value = d), P(t, (a) => E.set(a), { immediate: !0 }), P(r, (a) => T.set(a), { immediate: !0 }), P(s, (a) => $.set(a), { immediate: !0 }), E.subscribe((a) => {
112
+ const { activeUser: t, token: r, deviceId: s } = e, o = T.get(), i = A.get(), g = $.get();
113
+ o && (t.value = o), i && (r.value = i), g && (s.value = g), R(t, (a) => T.set(a), { immediate: !0 }), R(r, (a) => A.set(a), { immediate: !0 }), R(s, (a) => $.set(a), { immediate: !0 }), T.subscribe((a) => {
114
114
  a !== t.value && (t.value = a);
115
- }), T.subscribe((a) => {
115
+ }), A.subscribe((a) => {
116
116
  a !== r.value && (r.value = a);
117
117
  }), $.subscribe((a) => {
118
118
  a !== s.value && (s.value = a);
@@ -161,7 +161,7 @@ const U = 4, ue = globalThis.nanostoresGlobal || (globalThis.nanostoresGlobal =
161
161
  } catch (e) {
162
162
  this.logger.error("Failed to clear user from localStorage", { data: e });
163
163
  }
164
- E.set(void 0), T.set(null), $.set(null);
164
+ T.set(void 0), A.set(null), $.set(null);
165
165
  }
166
166
  }
167
167
  /**
@@ -188,20 +188,15 @@ const U = 4, ue = globalThis.nanostoresGlobal || (globalThis.nanostoresGlobal =
188
188
  }
189
189
  }
190
190
  };
191
- g(W, "SDKStorage");
192
- let O = W;
193
- const X = [
194
- "Setting up your assistant...",
195
- "Preparing your workspace...",
196
- "Connecting tools...",
197
- "Almost ready..."
198
- ], M = class M {
191
+ h(z, "SDKStorage");
192
+ let P = z;
193
+ const W = class W {
199
194
  constructor(e) {
200
195
  n(this, "ctx");
201
196
  this.ctx = e;
202
197
  }
203
198
  async create(e) {
204
- const t = e.orgId || this.resolveOrgId(), r = w(this.ctx.apiBase, this.ctx.isDev), o = await (await fetch(`${r}/api/agent`, {
199
+ const t = e.orgId || this.resolveOrgId(), r = x(this.ctx.apiBase, this.ctx.isDev), o = await (await fetch(`${r}/api/agents`, {
205
200
  method: "POST",
206
201
  headers: {
207
202
  "Content-Type": "application/json",
@@ -216,31 +211,40 @@ const X = [
216
211
  return this.ctx.processApiResponse(o), o.data[0];
217
212
  }
218
213
  async getStatus(e) {
219
- const t = w(this.ctx.apiBase, this.ctx.isDev), s = await (await fetch(`${t}/api/agent/${e.agentId}/status`, {
214
+ const t = x(this.ctx.apiBase, this.ctx.isDev), s = await (await fetch(`${t}/api/agents/${e.agentId}/status`, {
220
215
  headers: {
221
216
  ...this.ctx.token.value && { Authorization: `Bearer ${this.ctx.token.value}` }
222
217
  }
223
218
  })).json();
224
219
  return !s.ok || !s.data ? { status: "unknown", ready: !1, error: "Failed to check status" } : s.data;
225
220
  }
221
+ /**
222
+ * Poll until the agent is ready. `onStatus` receives the server-reported
223
+ * status string verbatim ('starting', 'provisioning', 'running', …) so
224
+ * the caller renders feedback tied to actual provisioner state, not a
225
+ * rotating set of decorative messages decoupled from reality. Per
226
+ * first-principles → "Always give feedback — no magic": a spinner that
227
+ * emits unrelated copy is closer to magic than to feedback. Map status
228
+ * names to user copy at the call site.
229
+ */
226
230
  async waitUntilReady(e) {
227
231
  const { agentId: t, onStatus: r, timeoutMs: s = 3e5, pollIntervalMs: o = 3e3 } = e, i = Date.now();
228
- let d = 0;
232
+ let g;
229
233
  for (; Date.now() - i < s; ) {
230
234
  const a = await this.getStatus({ agentId: t });
231
235
  if (a.ready) {
232
- r?.("Ready!");
236
+ r?.("running");
233
237
  return;
234
238
  }
235
239
  if (a.status === "error")
236
240
  throw new Error(a.error || "Provisioning failed");
237
- r && d < X.length && (r(X[d]), d++), await new Promise((c) => setTimeout(c, o));
241
+ a.status !== g && (r?.(a.status), g = a.status), await new Promise((c) => setTimeout(c, o));
238
242
  }
239
243
  throw new Error("Agent provisioning timed out — please try again");
240
244
  }
241
245
  /** Send a typed webhook event to a bot agent. No auth required — the proxy is public. */
242
246
  async webhook(e) {
243
- const r = `${w(this.ctx.apiBase, this.ctx.isDev)}/api/bot-api/agents/${e.agentId}/webhook`, s = { type: e.type, data: e.data };
247
+ const r = `${x(this.ctx.apiBase, this.ctx.isDev)}/api/bot-api/agents/${e.agentId}/webhook`, s = { type: e.type, data: e.data };
244
248
  e.meta && (s.meta = e.meta);
245
249
  const o = await fetch(r, {
246
250
  method: "POST",
@@ -260,8 +264,8 @@ const X = [
260
264
  return t;
261
265
  }
262
266
  };
263
- g(M, "AgentClient");
264
- let _ = M;
267
+ h(W, "AgentClient");
268
+ let _ = W;
265
269
  async function S(l, e) {
266
270
  l.loading.value = !0, l.error.value = null;
267
271
  try {
@@ -270,8 +274,8 @@ async function S(l, e) {
270
274
  l.loading.value = !1;
271
275
  }
272
276
  }
273
- g(S, "withLoadingState");
274
- const z = class z {
277
+ h(S, "withLoadingState");
278
+ const J = class J {
275
279
  constructor(e) {
276
280
  n(this, "ctx");
277
281
  this.ctx = e;
@@ -320,12 +324,15 @@ const z = class z {
320
324
  if (this.ctx.token.value)
321
325
  return S(this.ctx, async () => {
322
326
  try {
323
- const t = await (await this.ctx.api.auth.me.$get()).json();
324
- if (!t.ok) {
325
- 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());
327
+ const e = x(this.ctx.apiBase, this.ctx.isDev), r = await (await fetch(`${e}/api/users/me`, {
328
+ method: "GET",
329
+ headers: { Authorization: `Bearer ${this.ctx.token.value}` }
330
+ })).json();
331
+ if (!r.ok) {
332
+ this.ctx.error.value = r.error, r.error.includes("Authentication") && (this.ctx.activeUser.value = void 0, this.ctx.token.value = null, this.ctx.storage.clearStorage());
326
333
  return;
327
334
  }
328
- return t.user;
335
+ return this.ctx.token.value && (this.ctx.activeUser.value = r.data), r.data;
329
336
  } catch (e) {
330
337
  this.ctx.logger.error("Get current user error", { data: e }), this.ctx.error.value = e instanceof Error ? e.message : "Failed to get user info";
331
338
  return;
@@ -361,45 +368,45 @@ const z = class z {
361
368
  this.ctx.logger.error("loginWithGoogle: Only available in browser");
362
369
  return;
363
370
  }
364
- const t = w(this.ctx.apiBase, this.ctx.isDev), r = window.location.origin, s = `${t}/auth/google-popup?origin=${encodeURIComponent(r)}`, o = 500, i = 600, d = window.screenX + (window.outerWidth - o) / 2, a = window.screenY + (window.outerHeight - i) / 2, c = window.open(
371
+ const t = x(this.ctx.apiBase, this.ctx.isDev), r = window.location.origin, s = `${t}/auth/google-popup?origin=${encodeURIComponent(r)}`, o = 500, i = 600, g = window.screenX + (window.outerWidth - o) / 2, a = window.screenY + (window.outerHeight - i) / 2, c = window.open(
365
372
  s,
366
373
  "google-auth",
367
- `width=${o},height=${i},left=${d},top=${a},popup=yes`
374
+ `width=${o},height=${i},left=${g},top=${a},popup=yes`
368
375
  );
369
376
  if (!c) {
370
377
  const v = "Popup was blocked. Please allow popups for this site.";
371
378
  this.ctx.error.value = v, e?.onError?.(v);
372
379
  return;
373
380
  }
374
- const h = /* @__PURE__ */ g((v) => {
375
- const I = new URL(t).origin;
376
- if (v.origin === I) {
381
+ const u = /* @__PURE__ */ h((v) => {
382
+ const k = new URL(t).origin;
383
+ if (v.origin === k) {
377
384
  if (v.data?.type === "auth-success") {
378
- window.removeEventListener("message", h);
385
+ window.removeEventListener("message", u);
379
386
  const { token: p, user: f } = v.data;
380
387
  this.ctx.processApiResponse({ ok: !0, data: {}, token: p, user: f }), this.ctx.logger.info("Google login successful"), e?.onSuccess?.(f);
381
388
  } else if (v.data?.type === "auth-error") {
382
- window.removeEventListener("message", h);
389
+ window.removeEventListener("message", u);
383
390
  const p = v.data.error || "Google authentication failed";
384
391
  this.ctx.error.value = p, this.ctx.logger.error("Google login failed", { error: p }), e?.onError?.(p);
385
392
  }
386
393
  }
387
394
  }, "handler");
388
- window.addEventListener("message", h);
389
- const u = setInterval(() => {
390
- c.closed && (clearInterval(u), window.removeEventListener("message", h));
395
+ window.addEventListener("message", u);
396
+ const d = setInterval(() => {
397
+ c.closed && (clearInterval(d), window.removeEventListener("message", u));
391
398
  }, 500);
392
399
  }
393
400
  };
394
- g(z, "AuthClient");
395
- let j = z;
401
+ h(J, "AuthClient");
402
+ let F = J;
396
403
  const q = class q {
397
404
  constructor(e) {
398
405
  n(this, "ctx");
399
406
  this.ctx = e;
400
407
  }
401
408
  async startTrial(e) {
402
- const t = this.resolveOrgId(), r = w(this.ctx.apiBase, this.ctx.isDev), o = await (await fetch(`${r}/api/billing/checkout/create`, {
409
+ const t = this.resolveOrgId(), r = x(this.ctx.apiBase, this.ctx.isDev), o = await (await fetch(`${r}/api/billing/checkout/create`, {
403
410
  method: "POST",
404
411
  headers: {
405
412
  "Content-Type": "application/json",
@@ -421,7 +428,7 @@ const q = class q {
421
428
  return this.getStatus();
422
429
  }
423
430
  async getStatus() {
424
- const e = this.resolveOrgId(), t = w(this.ctx.apiBase, this.ctx.isDev), s = await (await fetch(`${t}/api/billing/details/${e}`, {
431
+ const e = this.resolveOrgId(), t = x(this.ctx.apiBase, this.ctx.isDev), s = await (await fetch(`${t}/api/billing/details/${e}`, {
425
432
  headers: {
426
433
  ...this.ctx.token.value && { Authorization: `Bearer ${this.ctx.token.value}` }
427
434
  }
@@ -450,10 +457,10 @@ const q = class q {
450
457
  return t;
451
458
  }
452
459
  };
453
- g(q, "BillingClient");
454
- let C = q;
460
+ h(q, "BillingClient");
461
+ let j = q;
455
462
  const de = /* @__PURE__ */ new Set(["message", "message_delta", "working_state", "working_end", "error", "stream_end"]);
456
- function Y(l) {
463
+ function X(l) {
457
464
  const e = l.split(`
458
465
  `).filter((s) => s.length > 0);
459
466
  let t, r;
@@ -467,8 +474,8 @@ function Y(l) {
467
474
  return null;
468
475
  }
469
476
  }
470
- g(Y, "parseChatEvent");
471
- const J = class J {
477
+ h(X, "parseChatEvent");
478
+ const M = class M {
472
479
  constructor(e) {
473
480
  n(this, "ctx");
474
481
  this.ctx = e;
@@ -480,12 +487,12 @@ const J = class J {
480
487
  * handle-based lookup, anonymousId instead of userId).
481
488
  *
482
489
  * The visitor identity is `anonymousId` — caller is responsible for
483
- * generating + persisting it (typically `sdk.generateAnonId()` which
484
- * stores a stable ID in localStorage).
490
+ * generating + persisting it (typically `sdk.user.generateAnonId()`
491
+ * which stores a stable ID in localStorage).
485
492
  */
486
493
  async chatStreamPublic(e) {
487
- const r = `${w(this.ctx.apiBase, this.ctx.isDev)}/api/agent/public/${encodeURIComponent(e.handle)}/chat/v2/stream`;
488
- let s = "", o = !1, i = !1, d;
494
+ const r = `${x(this.ctx.apiBase, this.ctx.isDev)}/api/agents/public/${encodeURIComponent(e.handle)}/chat/v2/stream`;
495
+ let s = "", o = !1, i = !1, g;
489
496
  try {
490
497
  const a = await fetch(r, {
491
498
  method: "POST",
@@ -498,28 +505,28 @@ const J = class J {
498
505
  })
499
506
  });
500
507
  if (!a.ok) {
501
- const u = await a.json().catch(() => ({ error: "Request failed" }));
502
- e.onError(u.error || `HTTP ${a.status}`);
508
+ const d = await a.json().catch(() => ({ error: "Request failed" }));
509
+ e.onError(d.error || `HTTP ${a.status}`);
503
510
  return;
504
511
  }
505
- if (s = a.headers.get("X-Conversation-Id") || "", d = a.body?.getReader(), !d) {
512
+ if (s = a.headers.get("X-Conversation-Id") || "", g = a.body?.getReader(), !g) {
506
513
  e.onError("No response stream");
507
514
  return;
508
515
  }
509
516
  const c = new TextDecoder();
510
- let h = "";
517
+ let u = "";
511
518
  e: for (; ; ) {
512
- const { done: u, value: v } = await d.read();
513
- if (u)
519
+ const { done: d, value: v } = await g.read();
520
+ if (d)
514
521
  break;
515
- h += c.decode(v, { stream: !0 });
516
- let I = h.indexOf(`
522
+ u += c.decode(v, { stream: !0 });
523
+ let k = u.indexOf(`
517
524
 
518
525
  `);
519
- for (; I !== -1; ) {
520
- const p = h.slice(0, I);
521
- h = h.slice(I + 2);
522
- const f = Y(p);
526
+ for (; k !== -1; ) {
527
+ const p = u.slice(0, k);
528
+ u = u.slice(k + 2);
529
+ const f = X(p);
523
530
  if (f) {
524
531
  if (f.event === "working_state")
525
532
  e.onStatus?.(f.data.description);
@@ -533,7 +540,7 @@ const J = class J {
533
540
  else if (f.event === "stream_end")
534
541
  break e;
535
542
  }
536
- I = h.indexOf(`
543
+ k = u.indexOf(`
537
544
 
538
545
  `);
539
546
  }
@@ -543,7 +550,7 @@ const J = class J {
543
550
  e.onError(a instanceof Error ? a.message : "Stream failed");
544
551
  } finally {
545
552
  try {
546
- await d?.cancel();
553
+ await g?.cancel();
547
554
  } catch {
548
555
  }
549
556
  }
@@ -555,24 +562,24 @@ const J = class J {
555
562
  * Mirrors `src/modules/agent/client.ts → sendChatMessageStream`.
556
563
  */
557
564
  async chatStreamAuthenticated(e) {
558
- const t = w(this.ctx.apiBase, this.ctx.isDev), r = this.ctx.token.value ? { Authorization: `Bearer ${this.ctx.token.value}` } : {}, s = `nonce_${ae(16)}`;
565
+ const t = x(this.ctx.apiBase, this.ctx.isDev), r = this.ctx.token.value ? { Authorization: `Bearer ${this.ctx.token.value}` } : {}, s = `nonce_${oe(16)}`;
559
566
  let o;
560
567
  try {
561
568
  const c = await fetch(`${t}/api/messages`, {
562
569
  method: "POST",
563
570
  headers: { "Content-Type": "application/json", ...r },
564
571
  body: JSON.stringify({ agentId: e.agentId, content: e.message, clientNonce: s })
565
- }), h = await c.json();
566
- if (!h.ok) {
567
- e.onError(h.error || `Send failed: HTTP ${c.status}`);
572
+ }), u = await c.json();
573
+ if (!u.ok) {
574
+ e.onError(u.error || `Send failed: HTTP ${c.status}`);
568
575
  return;
569
576
  }
570
- o = h.data;
577
+ o = u.data;
571
578
  } catch (c) {
572
579
  e.onError(c instanceof Error ? c.message : "Send failed");
573
580
  return;
574
581
  }
575
- let i = !1, d = !1, a;
582
+ let i = !1, g = !1, a;
576
583
  try {
577
584
  const c = await fetch(`${t}/api/chat/${encodeURIComponent(e.agentId)}/stream?since=${encodeURIComponent(o.sequence)}`, {
578
585
  headers: r
@@ -585,40 +592,40 @@ const J = class J {
585
592
  e.onError("No response stream");
586
593
  return;
587
594
  }
588
- const h = new TextDecoder();
589
- let u = "";
595
+ const u = new TextDecoder();
596
+ let d = "";
590
597
  e: for (; ; ) {
591
- const { done: v, value: I } = await a.read();
598
+ const { done: v, value: k } = await a.read();
592
599
  if (v)
593
600
  break;
594
- u += h.decode(I, { stream: !0 });
595
- let p = u.indexOf(`
601
+ d += u.decode(k, { stream: !0 });
602
+ let p = d.indexOf(`
596
603
 
597
604
  `);
598
605
  for (; p !== -1; ) {
599
- const f = u.slice(0, p);
600
- u = u.slice(p + 2);
601
- const y = Y(f);
602
- if (y) {
603
- if (y.event === "working_state")
604
- e.onStatus?.(y.data.description);
605
- else if (y.event === "message_delta")
606
- e.onDelta(y.data.delta);
607
- else if (y.event === "error") {
608
- i = !0, e.onError(y.data.message);
606
+ const f = d.slice(0, p);
607
+ d = d.slice(p + 2);
608
+ const w = X(f);
609
+ if (w) {
610
+ if (w.event === "working_state")
611
+ e.onStatus?.(w.data.description);
612
+ else if (w.event === "message_delta")
613
+ e.onDelta(w.data.delta);
614
+ else if (w.event === "error") {
615
+ i = !0, e.onError(w.data.message);
609
616
  break e;
610
- } else if (y.event === "message" && y.data.message.role === "assistant") {
611
- d = !0;
617
+ } else if (w.event === "message" && w.data.message.role === "assistant") {
618
+ g = !0;
612
619
  break e;
613
- } else if (y.event === "stream_end")
620
+ } else if (w.event === "stream_end")
614
621
  break e;
615
622
  }
616
- p = u.indexOf(`
623
+ p = d.indexOf(`
617
624
 
618
625
  `);
619
626
  }
620
627
  }
621
- !i && d ? e.onDone(o.conversationId) : i || e.onError("Stream ended before reply arrived");
628
+ !i && g ? e.onDone(o.conversationId) : i || e.onError("Stream ended before reply arrived");
622
629
  } catch (c) {
623
630
  e.onError(c instanceof Error ? c.message : "Stream failed");
624
631
  } finally {
@@ -628,35 +635,9 @@ const J = class J {
628
635
  }
629
636
  }
630
637
  }
631
- async chatMessage(e) {
632
- const r = `${w(this.ctx.apiBase, this.ctx.isDev)}/api/agent/public/${encodeURIComponent(e.handle)}/chat`;
633
- try {
634
- const o = await (await fetch(r, {
635
- method: "POST",
636
- headers: { "Content-Type": "application/json" },
637
- body: JSON.stringify({
638
- message: e.message,
639
- conversationId: e.conversationId,
640
- visitorId: e.visitorId,
641
- context: e.context
642
- })
643
- })).json();
644
- if (!o.ok || !o.data) {
645
- this.ctx.logger.error("chatMessage failed", { error: !o.ok && "error" in o ? o.error : "Unknown" });
646
- return;
647
- }
648
- return {
649
- conversationId: o.data.conversationId,
650
- content: o.data.message.content
651
- };
652
- } catch (s) {
653
- this.ctx.logger.error("chatMessage error", { error: s });
654
- return;
655
- }
656
- }
657
638
  };
658
- g(J, "ChatClient");
659
- let F = J;
639
+ h(M, "ChatClient");
640
+ let L = M;
660
641
  const K = class K {
661
642
  constructor(e) {
662
643
  n(this, "ctx");
@@ -665,7 +646,7 @@ const K = class K {
665
646
  async getPublicAgent(e) {
666
647
  this.ctx.loading.value = !0, this.ctx.error.value = null;
667
648
  try {
668
- const r = await (await this.ctx.api.agent.public[":handle"].$get({
649
+ const r = await (await this.ctx.api.agents.public[":handle"].$get({
669
650
  param: { handle: e.handle }
670
651
  })).json();
671
652
  if (!r.ok) {
@@ -683,7 +664,7 @@ const K = class K {
683
664
  async getAgentByEmail(e) {
684
665
  this.ctx.loading.value = !0, this.ctx.error.value = null;
685
666
  try {
686
- const r = await (await this.ctx.api.agent["by-email"][":email"].$get({
667
+ const r = await (await this.ctx.api.agents["by-email"][":email"].$get({
687
668
  param: { email: e.email }
688
669
  })).json();
689
670
  if (!r.ok) {
@@ -701,7 +682,7 @@ const K = class K {
701
682
  generateAnonId() {
702
683
  const e = "pagelinesAnonId";
703
684
  let t = this.ctx.storage.getItem(e);
704
- return t || (t = R({ prefix: "anon" }), this.ctx.storage.setItem(e, t)), t;
685
+ return t || (t = O({ prefix: "anon" }), this.ctx.storage.setItem(e, t)), t;
705
686
  }
706
687
  track(e) {
707
688
  try {
@@ -716,8 +697,8 @@ const K = class K {
716
697
  }
717
698
  }
718
699
  };
719
- g(K, "UserClient");
720
- let L = K;
700
+ h(K, "UserClient");
701
+ let N = K;
721
702
  const H = class H {
722
703
  constructor() {
723
704
  n(this, "key", "__PAGELINES_SDK__");
@@ -733,28 +714,28 @@ const H = class H {
733
714
  typeof window < "u" && delete globalThis[this.key];
734
715
  }
735
716
  };
736
- g(H, "SDKGlobal");
737
- let N = H;
738
- const D = new N(), B = class B extends ie {
717
+ h(H, "SDKGlobal");
718
+ let C = H;
719
+ const D = new C(), B = class B extends ae {
739
720
  constructor(t = {}) {
740
721
  const r = D.get();
741
722
  if (r)
742
723
  return console.debug("[PageLinesSDK] Returning existing singleton instance"), r;
743
724
  super("PageLinesSDK", t);
744
725
  // Vue reactive state
745
- n(this, "activeUser", A());
746
- n(this, "token", A(null));
726
+ n(this, "activeUser", E());
727
+ n(this, "token", E(null));
747
728
  // Refresh credential lives in HttpOnly pl-refresh-token cookie — JS never
748
729
  // holds it, so no reactive ref. /api/auth/refresh reads the cookie via the
749
730
  // browser's automatic credentials: 'include' attach.
750
- n(this, "deviceId", A(null));
751
- n(this, "loading", A(!1));
752
- n(this, "error", A(null));
731
+ n(this, "deviceId", E(null));
732
+ n(this, "loading", E(!1));
733
+ n(this, "error", E(null));
753
734
  n(this, "apiBase", this.settings.apiBase || void 0);
754
735
  // Storage handler for persistence
755
- n(this, "storage", new O());
736
+ n(this, "storage", new P());
756
737
  // Auto-managed session ID for usage tracking
757
- n(this, "sessionId", R({ prefix: "ses" }));
738
+ n(this, "sessionId", O({ prefix: "ses" }));
758
739
  // Typed Hono RPC client - uses SDKAppType (auth/self/usage routes only)
759
740
  // Initialized in constructor (definite assignment via singleton pattern)
760
741
  n(this, "apiClient");
@@ -784,15 +765,15 @@ const D = new N(), B = class B extends ie {
784
765
  n(this, "billing");
785
766
  n(this, "chat");
786
767
  n(this, "user");
787
- D.set(this), this.apiClient = ce(
768
+ D.set(this), this.apiClient = ne(
788
769
  { isDev: this.isDev, apiBase: this.apiBase },
789
770
  this.processApiResponse.bind(this),
790
771
  {
791
- getDeviceId: /* @__PURE__ */ g(() => this.deviceId.value, "getDeviceId"),
792
- onAccessTokenRefreshed: /* @__PURE__ */ g((o) => {
772
+ getDeviceId: /* @__PURE__ */ h(() => this.deviceId.value, "getDeviceId"),
773
+ onAccessTokenRefreshed: /* @__PURE__ */ h((o) => {
793
774
  this.token.value = o, this.persistAuth();
794
775
  }, "onAccessTokenRefreshed"),
795
- onTerminalFailure: /* @__PURE__ */ g(() => {
776
+ onTerminalFailure: /* @__PURE__ */ h(() => {
796
777
  this.clearSession();
797
778
  }, "onTerminalFailure")
798
779
  }
@@ -811,9 +792,9 @@ const D = new N(), B = class B extends ie {
811
792
  logger: this.logger,
812
793
  processApiResponse: this.processApiResponse.bind(this)
813
794
  };
814
- this.agent = new _(s), this.auth = new j(s), this.billing = new C(s), this.chat = new F(s), this.user = new L(s), this.logger.info("PageLinesSDK initialized"), this.storage.loadFromStorage(this.authRefs()), this.storage.syncWithGlobalStores(this.authRefs()), this.initialized = new Promise((o) => {
795
+ this.agent = new _(s), this.auth = new F(s), this.billing = new j(s), this.chat = new L(s), this.user = new N(s), this.logger.info("PageLinesSDK initialized"), this.storage.loadFromStorage(this.authRefs()), this.storage.syncWithGlobalStores(this.authRefs()), this.initialized = new Promise((o) => {
815
796
  this.resolveUser = o;
816
- }), this.token.value && !this.activeUser.value ? this.getCurrentUser().then((o) => {
797
+ }), this.token.value && !this.activeUser.value ? this.auth.getCurrentUser().then((o) => {
817
798
  this.resolveUser && (this.resolveUser(o), this.resolveUser = void 0);
818
799
  }).catch((o) => {
819
800
  this.logger.error("Auto user fetch failed", { data: o }), this.resolveUser && (this.resolveUser(void 0), this.resolveUser = void 0);
@@ -830,8 +811,9 @@ const D = new N(), B = class B extends ie {
830
811
  get isDev() {
831
812
  return this.settings.isDev ?? (typeof window < "u" ? window.location.hostname === "localhost" || window.location.hostname.includes("127.0.0.1") : !1);
832
813
  }
833
- // Bypass stub typing for route access (production builds use SDKAppType stub)
834
- // Runtime types are validated via Zod at API boundaries
814
+ // The stub `Hono<any, any, any>` makes `hc<SDKAppType>` resolve to `any`
815
+ // for prod-build speed; in dev the alias points at the real SDKAppType
816
+ // and we get full route typing. Either way `.api` is a valid access.
835
817
  get api() {
836
818
  return this.apiClient.api;
837
819
  }
@@ -859,58 +841,18 @@ const D = new N(), B = class B extends ie {
859
841
  }
860
842
  // Full reset - clears session + state + destroys singleton
861
843
  clear() {
862
- this.logger.info("Clearing SDK completely"), this.clearSession(), this.loading.value = !1, this.sessionId = R({ prefix: "ses" }), D.delete();
863
- }
864
- // Backward-compatible delegates — these forward to sub-clients
865
- // Can be removed in a future breaking change
866
- async sendCode(t) {
867
- return this.auth.sendCode(t);
868
- }
869
- async verifyCode(t, r) {
870
- return this.auth.verifyCode(t, r);
871
- }
872
- async logout() {
873
- return this.auth.logout();
874
- }
875
- async getCurrentUser() {
876
- return this.auth.getCurrentUser();
877
- }
878
- async requestAuthCode(t) {
879
- return this.auth.requestAuthCode(t);
880
- }
881
- async loginWithCode(t) {
882
- return this.auth.loginWithCode(t);
883
- }
884
- loginWithGoogle(t) {
885
- return this.auth.loginWithGoogle(t);
886
- }
887
- async chatStreamPublic(t) {
888
- return this.chat.chatStreamPublic(t);
889
- }
890
- async chatStreamAuthenticated(t) {
891
- return this.chat.chatStreamAuthenticated(t);
892
- }
893
- async getPublicAgent(t) {
894
- return this.user.getPublicAgent(t);
895
- }
896
- async getAgentByEmail(t) {
897
- return this.user.getAgentByEmail(t);
898
- }
899
- generateAnonId() {
900
- return this.user.generateAnonId();
901
- }
902
- track(t) {
903
- return this.user.track(t);
844
+ this.logger.info("Clearing SDK completely"), this.clearSession(), this.loading.value = !1, this.sessionId = O({ prefix: "ses" }), D.delete();
904
845
  }
905
846
  };
906
- g(B, "PageLinesSDK");
907
- let Z = B;
847
+ h(B, "PageLinesSDK");
848
+ let Y = B;
908
849
  export {
909
850
  _ as A,
910
- F as C,
911
- Z as P,
912
- L as U,
913
- j as a,
914
- w as r
851
+ j as B,
852
+ L as C,
853
+ Y as P,
854
+ N as U,
855
+ F as a,
856
+ x as r
915
857
  };
916
858
  //# sourceMappingURL=sdkClient.js.map