@pagelines/sdk 1.0.511 → 1.0.513

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,25 +1,25 @@
1
- var V = Object.defineProperty;
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);
1
+ var Q = Object.defineProperty;
2
+ var te = (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 c = (i, e, t) => te(i, typeof e != "symbol" ? e + "" : e, t);
5
+ import { APP_PORT as re, APP_PROD_URL as se, createRefreshingFetch as oe, cookieUtil as b, createLogger as ae, shortId as ie, objectId as O, SettingsObject as ne } from "@pagelines/core";
6
+ import { watch as R, ref as E, computed as X } from "vue";
7
+ import { hc as ce } from "hono/client";
8
+ function y(i, e) {
9
+ return i || (e ? `http://localhost:${re}` : se);
10
10
  }
11
- h(x, "resolveApiBase");
12
- function ne(l, e, t) {
13
- const r = x(l.apiBase, l.isDev), s = t ? re({
11
+ u(y, "resolveApiBase");
12
+ function le(i, e, t) {
13
+ const r = y(i.apiBase, i.isDev), s = t ? oe({
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 ie(r, {
21
- fetch: /* @__PURE__ */ h(async (i, g) => {
22
- const a = b.getAuthToken(), c = await s(i, {
20
+ return ce(r, {
21
+ fetch: /* @__PURE__ */ u(async (n, g) => {
22
+ const a = b.getAuthToken(), l = await s(n, {
23
23
  ...g,
24
24
  headers: {
25
25
  ...g?.headers,
@@ -34,26 +34,26 @@ function ne(l, e, t) {
34
34
  credentials: "include"
35
35
  });
36
36
  if (e) {
37
- const u = c.json.bind(c);
38
- c.json = async () => {
39
- const d = await u();
37
+ const h = l.json.bind(l);
38
+ l.json = async () => {
39
+ const d = await h();
40
40
  return typeof d == "object" && d !== null && "ok" in d && ("user" in d || "token" in d) && e(d), d;
41
41
  };
42
42
  }
43
- return c;
43
+ return l;
44
44
  }, "fetch")
45
45
  });
46
46
  }
47
- h(ne, "createApiClient");
48
- const ce = /* @__PURE__ */ Symbol("clean");
47
+ u(le, "createApiClient");
48
+ const de = /* @__PURE__ */ Symbol("clean");
49
49
  let w = [], I = 0;
50
- const U = 4, le = globalThis.nanostoresGlobal || (globalThis.nanostoresGlobal = { epoch: 0 }), G = /* @__PURE__ */ h(/* @__NO_SIDE_EFFECTS__ */ (l) => {
50
+ const U = 4, ue = globalThis.nanostoresGlobal || (globalThis.nanostoresGlobal = { epoch: 0 }), G = /* @__PURE__ */ u(/* @__NO_SIDE_EFFECTS__ */ (i) => {
51
51
  let e = [], t = {
52
52
  get() {
53
53
  return t.lc || t.listen(() => {
54
54
  })(), t.value;
55
55
  },
56
- init: l,
56
+ init: i,
57
57
  lc: 0,
58
58
  listen(r) {
59
59
  return t.lc = e.push(r), () => {
@@ -64,10 +64,10 @@ const U = 4, le = globalThis.nanostoresGlobal || (globalThis.nanostoresGlobal =
64
64
  };
65
65
  },
66
66
  notify(r, s) {
67
- le.epoch++;
67
+ ue.epoch++;
68
68
  let o = !w.length;
69
- for (let i of e)
70
- w.push(i, t.value, r, s);
69
+ for (let n of e)
70
+ w.push(n, t.value, r, s);
71
71
  if (o) {
72
72
  for (I = 0; I < w.length; I += U)
73
73
  w[I](
@@ -90,14 +90,14 @@ const U = 4, le = globalThis.nanostoresGlobal || (globalThis.nanostoresGlobal =
90
90
  let s = t.listen(r);
91
91
  return r(t.value), s;
92
92
  },
93
- value: l
93
+ value: i
94
94
  };
95
- return process.env.NODE_ENV !== "production" && (t[ce] = () => {
95
+ return process.env.NODE_ENV !== "production" && (t[de] = () => {
96
96
  e = [], t.lc = 0, t.off();
97
97
  }), t;
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 {
98
+ }, "atom"), x = globalThis, T = x.__PL_AUTH_USER__ ?? (x.__PL_AUTH_USER__ = /* @__PURE__ */ G(void 0)), A = x.__PL_AUTH_TOKEN__ ?? (x.__PL_AUTH_TOKEN__ = /* @__PURE__ */ G(null)), $ = x.__PL_AUTH_DEVICE_ID__ ?? (x.__PL_AUTH_DEVICE_ID__ = /* @__PURE__ */ G(null)), z = class z {
99
99
  constructor() {
100
- n(this, "logger", se("SDKStorage"));
100
+ c(this, "logger", ae("SDKStorage"));
101
101
  }
102
102
  /**
103
103
  * Check if localStorage is fully functional (some test environments provide partial mocks)
@@ -109,8 +109,8 @@ const U = 4, le = 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 = 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) => {
112
+ const { activeUser: t, token: r, deviceId: s } = e, o = T.get(), n = A.get(), g = $.get();
113
+ o && (t.value = o), n && (r.value = n), 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
115
  }), A.subscribe((a) => {
116
116
  a !== r.value && (r.value = a);
@@ -188,15 +188,15 @@ const U = 4, le = globalThis.nanostoresGlobal || (globalThis.nanostoresGlobal =
188
188
  }
189
189
  }
190
190
  };
191
- h(z, "SDKStorage");
191
+ u(z, "SDKStorage");
192
192
  let P = z;
193
- const W = class W {
193
+ const M = class M {
194
194
  constructor(e) {
195
- n(this, "ctx");
195
+ c(this, "ctx");
196
196
  this.ctx = e;
197
197
  }
198
198
  async create(e) {
199
- const t = e.orgId || this.resolveOrgId(), r = x(this.ctx.apiBase, this.ctx.isDev), o = await (await fetch(`${r}/api/agents`, {
199
+ const t = e.orgId || this.resolveOrgId(), r = y(this.ctx.apiBase, this.ctx.isDev), o = await (await fetch(`${r}/api/agents`, {
200
200
  method: "POST",
201
201
  headers: {
202
202
  "Content-Type": "application/json",
@@ -205,13 +205,13 @@ const W = class W {
205
205
  body: JSON.stringify({ name: e.name, orgId: t })
206
206
  })).json();
207
207
  if (!o.ok || !o.data?.[0]) {
208
- const i = o.error || "Failed to create agent";
209
- throw this.ctx.error.value = i, new Error(i);
208
+ const n = o.error || "Failed to create agent";
209
+ throw this.ctx.error.value = n, new Error(n);
210
210
  }
211
211
  return this.ctx.processApiResponse(o), o.data[0];
212
212
  }
213
213
  async getStatus(e) {
214
- const t = x(this.ctx.apiBase, this.ctx.isDev), s = await (await fetch(`${t}/api/agents/${e.agentId}/status`, {
214
+ const t = y(this.ctx.apiBase, this.ctx.isDev), s = await (await fetch(`${t}/api/agents/${e.agentId}/status`, {
215
215
  headers: {
216
216
  ...this.ctx.token.value && { Authorization: `Bearer ${this.ctx.token.value}` }
217
217
  }
@@ -228,9 +228,9 @@ const W = class W {
228
228
  * names to user copy at the call site.
229
229
  */
230
230
  async waitUntilReady(e) {
231
- const { agentId: t, onStatus: r, timeoutMs: s = 3e5, pollIntervalMs: o = 3e3 } = e, i = Date.now();
231
+ const { agentId: t, onStatus: r, timeoutMs: s = 3e5, pollIntervalMs: o = 3e3 } = e, n = Date.now();
232
232
  let g;
233
- for (; Date.now() - i < s; ) {
233
+ for (; Date.now() - n < s; ) {
234
234
  const a = await this.getStatus({ agentId: t });
235
235
  if (a.ready) {
236
236
  r?.("running");
@@ -238,13 +238,13 @@ const W = class W {
238
238
  }
239
239
  if (a.status === "error")
240
240
  throw new Error(a.error || "Provisioning failed");
241
- a.status !== g && (r?.(a.status), g = a.status), await new Promise((c) => setTimeout(c, o));
241
+ a.status !== g && (r?.(a.status), g = a.status), await new Promise((l) => setTimeout(l, o));
242
242
  }
243
243
  throw new Error("Agent provisioning timed out — please try again");
244
244
  }
245
245
  /** Send a typed webhook event to a bot agent. No auth required — the proxy is public. */
246
246
  async webhook(e) {
247
- const r = `${x(this.ctx.apiBase, this.ctx.isDev)}/api/bot-api/agents/${e.agentId}/webhook`, s = { type: e.type, data: e.data };
247
+ const r = `${y(this.ctx.apiBase, this.ctx.isDev)}/api/bot-api/agents/${e.agentId}/webhook`, s = { type: e.type, data: e.data };
248
248
  e.meta && (s.meta = e.meta);
249
249
  const o = await fetch(r, {
250
250
  method: "POST",
@@ -252,8 +252,8 @@ const W = class W {
252
252
  body: JSON.stringify(s)
253
253
  });
254
254
  if (!o.ok) {
255
- const i = await o.text().catch(() => "Unknown error");
256
- throw new Error(`Webhook delivery failed (${o.status}): ${i.slice(0, 200)}`);
255
+ const n = await o.text().catch(() => "Unknown error");
256
+ throw new Error(`Webhook delivery failed (${o.status}): ${n.slice(0, 200)}`);
257
257
  }
258
258
  return o.json();
259
259
  }
@@ -264,20 +264,20 @@ const W = class W {
264
264
  return t;
265
265
  }
266
266
  };
267
- h(W, "AgentClient");
268
- let _ = W;
269
- async function S(l, e) {
270
- l.loading.value = !0, l.error.value = null;
267
+ u(M, "AgentClient");
268
+ let _ = M;
269
+ async function S(i, e) {
270
+ i.loading.value = !0, i.error.value = null;
271
271
  try {
272
272
  return await e();
273
273
  } finally {
274
- l.loading.value = !1;
274
+ i.loading.value = !1;
275
275
  }
276
276
  }
277
- h(S, "withLoadingState");
278
- const J = class J {
277
+ u(S, "withLoadingState");
278
+ const W = class W {
279
279
  constructor(e) {
280
- n(this, "ctx");
280
+ c(this, "ctx");
281
281
  this.ctx = e;
282
282
  }
283
283
  async sendCode(e) {
@@ -324,7 +324,7 @@ const J = class J {
324
324
  if (this.ctx.token.value)
325
325
  return S(this.ctx, async () => {
326
326
  try {
327
- const e = x(this.ctx.apiBase, this.ctx.isDev), r = await (await fetch(`${e}/api/users/me`, {
327
+ const e = y(this.ctx.apiBase, this.ctx.isDev), r = await (await fetch(`${e}/api/users/me`, {
328
328
  method: "GET",
329
329
  headers: { Authorization: `Bearer ${this.ctx.token.value}` }
330
330
  })).json();
@@ -368,45 +368,45 @@ const J = class J {
368
368
  this.ctx.logger.error("loginWithGoogle: Only available in browser");
369
369
  return;
370
370
  }
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(
371
+ const t = y(this.ctx.apiBase, this.ctx.isDev), r = window.location.origin, s = `${t}/auth/google-popup?origin=${encodeURIComponent(r)}`, o = 500, n = 600, g = window.screenX + (window.outerWidth - o) / 2, a = window.screenY + (window.outerHeight - n) / 2, l = window.open(
372
372
  s,
373
373
  "google-auth",
374
- `width=${o},height=${i},left=${g},top=${a},popup=yes`
374
+ `width=${o},height=${n},left=${g},top=${a},popup=yes`
375
375
  );
376
- if (!c) {
377
- const v = "Popup was blocked. Please allow popups for this site.";
378
- this.ctx.error.value = v, e?.onError?.(v);
376
+ if (!l) {
377
+ const p = "Popup was blocked. Please allow popups for this site.";
378
+ this.ctx.error.value = p, e?.onError?.(p);
379
379
  return;
380
380
  }
381
- const u = /* @__PURE__ */ h((v) => {
381
+ const h = /* @__PURE__ */ u((p) => {
382
382
  const k = new URL(t).origin;
383
- if (v.origin === k) {
384
- if (v.data?.type === "auth-success") {
385
- window.removeEventListener("message", u);
386
- const { token: p, user: f } = v.data;
387
- this.ctx.processApiResponse({ ok: !0, data: {}, token: p, user: f }), this.ctx.logger.info("Google login successful"), e?.onSuccess?.(f);
388
- } else if (v.data?.type === "auth-error") {
389
- window.removeEventListener("message", u);
390
- const p = v.data.error || "Google authentication failed";
391
- this.ctx.error.value = p, this.ctx.logger.error("Google login failed", { error: p }), e?.onError?.(p);
383
+ if (p.origin === k) {
384
+ if (p.data?.type === "auth-success") {
385
+ window.removeEventListener("message", h);
386
+ const { token: m, user: f } = p.data;
387
+ this.ctx.processApiResponse({ ok: !0, data: {}, token: m, user: f }), this.ctx.logger.info("Google login successful"), e?.onSuccess?.(f);
388
+ } else if (p.data?.type === "auth-error") {
389
+ window.removeEventListener("message", h);
390
+ const m = p.data.error || "Google authentication failed";
391
+ this.ctx.error.value = m, this.ctx.logger.error("Google login failed", { error: m }), e?.onError?.(m);
392
392
  }
393
393
  }
394
394
  }, "handler");
395
- window.addEventListener("message", u);
395
+ window.addEventListener("message", h);
396
396
  const d = setInterval(() => {
397
- c.closed && (clearInterval(d), window.removeEventListener("message", u));
397
+ l.closed && (clearInterval(d), window.removeEventListener("message", h));
398
398
  }, 500);
399
399
  }
400
400
  };
401
- h(J, "AuthClient");
402
- let F = J;
403
- const q = class q {
401
+ u(W, "AuthClient");
402
+ let F = W;
403
+ const J = class J {
404
404
  constructor(e) {
405
- n(this, "ctx");
405
+ c(this, "ctx");
406
406
  this.ctx = e;
407
407
  }
408
408
  async startTrial(e) {
409
- const t = this.resolveOrgId(), r = x(this.ctx.apiBase, this.ctx.isDev), o = await (await fetch(`${r}/api/billing/checkout/create`, {
409
+ const t = this.resolveOrgId(), r = y(this.ctx.apiBase, this.ctx.isDev), o = await (await fetch(`${r}/api/billing/checkout/create`, {
410
410
  method: "POST",
411
411
  headers: {
412
412
  "Content-Type": "application/json",
@@ -419,8 +419,8 @@ const q = class q {
419
419
  })
420
420
  })).json();
421
421
  if (!o.ok || !o.data?.url) {
422
- const i = o.error || "Failed to create checkout session";
423
- throw this.ctx.error.value = i, new Error(i);
422
+ const n = o.error || "Failed to create checkout session";
423
+ throw this.ctx.error.value = n, new Error(n);
424
424
  }
425
425
  window.location.href = o.data.url;
426
426
  }
@@ -428,7 +428,7 @@ const q = class q {
428
428
  return this.getStatus();
429
429
  }
430
430
  async getStatus() {
431
- const e = this.resolveOrgId(), t = x(this.ctx.apiBase, this.ctx.isDev), s = await (await fetch(`${t}/api/billing/details/${e}`, {
431
+ const e = this.resolveOrgId(), t = y(this.ctx.apiBase, this.ctx.isDev), s = await (await fetch(`${t}/api/billing/details/${e}`, {
432
432
  headers: {
433
433
  ...this.ctx.token.value && { Authorization: `Bearer ${this.ctx.token.value}` }
434
434
  }
@@ -457,16 +457,16 @@ const q = class q {
457
457
  return t;
458
458
  }
459
459
  };
460
- h(q, "BillingClient");
461
- let j = q;
462
- const de = /* @__PURE__ */ new Set(["message", "message_delta", "working_state", "working_end", "error", "stream_end"]);
463
- function X(l) {
464
- const e = l.split(`
460
+ u(J, "BillingClient");
461
+ let j = J;
462
+ const he = /* @__PURE__ */ new Set(["message", "message_delta", "working_state", "working_end", "error", "stream_end"]);
463
+ function Y(i) {
464
+ const e = i.split(`
465
465
  `).filter((s) => s.length > 0);
466
466
  let t, r;
467
467
  for (const s of e)
468
468
  s.startsWith("event:") ? t = s.slice(6).trim() : s.startsWith("data:") && (r = s.slice(5).trim());
469
- if (!t || r === void 0 || !de.has(t))
469
+ if (!t || r === void 0 || !he.has(t))
470
470
  return null;
471
471
  try {
472
472
  return { event: t, data: JSON.parse(r) };
@@ -474,10 +474,22 @@ function X(l) {
474
474
  return null;
475
475
  }
476
476
  }
477
- h(X, "parseChatEvent");
478
- const M = class M {
477
+ u(Y, "parseChatEvent");
478
+ function Z(i) {
479
+ if (!(i.role !== "assistant" && i.role !== "system"))
480
+ return {
481
+ id: i.messageId,
482
+ text: i.content,
483
+ sender: i.role === "system" ? "system" : "agent",
484
+ timestamp: i.createdAt,
485
+ ...i.metadata?.attachments?.length ? { attachments: i.metadata.attachments } : {},
486
+ ...i.metadata?.issue ? { issue: i.metadata.issue } : {}
487
+ };
488
+ }
489
+ u(Z, "toChatMessage");
490
+ const q = class q {
479
491
  constructor(e) {
480
- n(this, "ctx");
492
+ c(this, "ctx");
481
493
  this.ctx = e;
482
494
  }
483
495
  /**
@@ -491,8 +503,8 @@ const M = class M {
491
503
  * which stores a stable ID in localStorage).
492
504
  */
493
505
  async chatStreamPublic(e) {
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;
506
+ const r = `${y(this.ctx.apiBase, this.ctx.isDev)}/api/agents/public/${encodeURIComponent(e.handle)}/chat/v2/stream`;
507
+ let s = "", o = !1, n = !1, g;
496
508
  try {
497
509
  const a = await fetch(r, {
498
510
  method: "POST",
@@ -513,20 +525,20 @@ const M = class M {
513
525
  e.onError("No response stream");
514
526
  return;
515
527
  }
516
- const c = new TextDecoder();
517
- let u = "";
528
+ const l = new TextDecoder();
529
+ let h = "";
518
530
  e: for (; ; ) {
519
- const { done: d, value: v } = await g.read();
531
+ const { done: d, value: p } = await g.read();
520
532
  if (d)
521
533
  break;
522
- u += c.decode(v, { stream: !0 });
523
- let k = u.indexOf(`
534
+ h += l.decode(p, { stream: !0 });
535
+ let k = h.indexOf(`
524
536
 
525
537
  `);
526
538
  for (; k !== -1; ) {
527
- const p = u.slice(0, k);
528
- u = u.slice(k + 2);
529
- const f = X(p);
539
+ const m = h.slice(0, k);
540
+ h = h.slice(k + 2);
541
+ const f = Y(m);
530
542
  if (f) {
531
543
  if (f.event === "working_state")
532
544
  e.onStatus?.(f.data.description);
@@ -535,17 +547,19 @@ const M = class M {
535
547
  else if (f.event === "error") {
536
548
  o = !0, e.onError(f.data.message);
537
549
  break e;
538
- } else if (f.event === "message" && (f.data.message.role === "assistant" || f.data.message.role === "system"))
539
- i = !0;
540
- else if (f.event === "stream_end")
550
+ } else if (f.event === "message" && (f.data.message.role === "assistant" || f.data.message.role === "system")) {
551
+ n = !0;
552
+ const v = Z(f.data.message);
553
+ v && e.onMessage?.(v);
554
+ } else if (f.event === "stream_end")
541
555
  break e;
542
556
  }
543
- k = u.indexOf(`
557
+ k = h.indexOf(`
544
558
 
545
559
  `);
546
560
  }
547
561
  }
548
- !o && i ? e.onDone(s) : o || e.onError("Stream ended before reply arrived");
562
+ !o && n ? e.onDone(s) : o || e.onError("Stream ended before reply arrived");
549
563
  } catch (a) {
550
564
  e.onError(a instanceof Error ? a.message : "Stream failed");
551
565
  } finally {
@@ -562,72 +576,74 @@ const M = class M {
562
576
  * Mirrors `src/modules/agent/client.ts → sendChatMessageStream`.
563
577
  */
564
578
  async chatStreamAuthenticated(e) {
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)}`;
579
+ const t = y(this.ctx.apiBase, this.ctx.isDev), r = this.ctx.token.value ? { Authorization: `Bearer ${this.ctx.token.value}` } : {}, s = `nonce_${ie(16)}`;
566
580
  let o;
567
581
  try {
568
- const c = await fetch(`${t}/api/messages`, {
582
+ const l = await fetch(`${t}/api/messages`, {
569
583
  method: "POST",
570
584
  headers: { "Content-Type": "application/json", ...r },
571
585
  body: JSON.stringify({ agentId: e.agentId, content: e.message, clientNonce: s })
572
- }), u = await c.json();
573
- if (!u.ok) {
574
- e.onError(u.error || `Send failed: HTTP ${c.status}`);
586
+ }), h = await l.json();
587
+ if (!h.ok) {
588
+ e.onError(h.error || `Send failed: HTTP ${l.status}`);
575
589
  return;
576
590
  }
577
- o = u.data;
578
- } catch (c) {
579
- e.onError(c instanceof Error ? c.message : "Send failed");
591
+ o = h.data;
592
+ } catch (l) {
593
+ e.onError(l instanceof Error ? l.message : "Send failed");
580
594
  return;
581
595
  }
582
- let i = !1, g = !1, a;
596
+ let n = !1, g = !1, a;
583
597
  try {
584
- const c = await fetch(`${t}/api/chat/${encodeURIComponent(e.agentId)}/stream?since=${encodeURIComponent(o.sequence)}`, {
598
+ const l = await fetch(`${t}/api/chat/${encodeURIComponent(e.agentId)}/stream?since=${encodeURIComponent(o.sequence)}`, {
585
599
  headers: r
586
600
  });
587
- if (!c.ok) {
588
- e.onError(`Stream failed: HTTP ${c.status}`);
601
+ if (!l.ok) {
602
+ e.onError(`Stream failed: HTTP ${l.status}`);
589
603
  return;
590
604
  }
591
- if (a = c.body?.getReader(), !a) {
605
+ if (a = l.body?.getReader(), !a) {
592
606
  e.onError("No response stream");
593
607
  return;
594
608
  }
595
- const u = new TextDecoder();
609
+ const h = new TextDecoder();
596
610
  let d = "";
597
611
  e: for (; ; ) {
598
- const { done: v, value: k } = await a.read();
599
- if (v)
612
+ const { done: p, value: k } = await a.read();
613
+ if (p)
600
614
  break;
601
- d += u.decode(k, { stream: !0 });
602
- let p = d.indexOf(`
615
+ d += h.decode(k, { stream: !0 });
616
+ let m = d.indexOf(`
603
617
 
604
618
  `);
605
- for (; p !== -1; ) {
606
- const f = d.slice(0, p);
607
- d = d.slice(p + 2);
608
- const m = X(f);
609
- if (m) {
610
- if (m.event === "working_state")
611
- e.onStatus?.(m.data.description);
612
- else if (m.event === "message_delta")
613
- e.onDelta(m.data.delta, m.data.role);
614
- else if (m.event === "error") {
615
- i = !0, e.onError(m.data.message);
619
+ for (; m !== -1; ) {
620
+ const f = d.slice(0, m);
621
+ d = d.slice(m + 2);
622
+ const v = Y(f);
623
+ if (v) {
624
+ if (v.event === "working_state")
625
+ e.onStatus?.(v.data.description);
626
+ else if (v.event === "message_delta")
627
+ e.onDelta(v.data.delta, v.data.role);
628
+ else if (v.event === "error") {
629
+ n = !0, e.onError(v.data.message);
616
630
  break e;
617
- } else if (m.event === "message" && (m.data.message.role === "assistant" || m.data.message.role === "system")) {
631
+ } else if (v.event === "message" && (v.data.message.role === "assistant" || v.data.message.role === "system")) {
618
632
  g = !0;
633
+ const V = Z(v.data.message);
634
+ V && e.onMessage?.(V);
619
635
  break e;
620
- } else if (m.event === "stream_end")
636
+ } else if (v.event === "stream_end")
621
637
  break e;
622
638
  }
623
- p = d.indexOf(`
639
+ m = d.indexOf(`
624
640
 
625
641
  `);
626
642
  }
627
643
  }
628
- !i && g ? e.onDone(o.conversationId) : i || e.onError("Stream ended before reply arrived");
629
- } catch (c) {
630
- e.onError(c instanceof Error ? c.message : "Stream failed");
644
+ !n && g ? e.onDone(o.conversationId) : n || e.onError("Stream ended before reply arrived");
645
+ } catch (l) {
646
+ e.onError(l instanceof Error ? l.message : "Stream failed");
631
647
  } finally {
632
648
  try {
633
649
  await a?.cancel();
@@ -636,11 +652,11 @@ const M = class M {
636
652
  }
637
653
  }
638
654
  };
639
- h(M, "ChatClient");
640
- let L = M;
655
+ u(q, "ChatClient");
656
+ let L = q;
641
657
  const K = class K {
642
658
  constructor(e) {
643
- n(this, "ctx");
659
+ c(this, "ctx");
644
660
  this.ctx = e;
645
661
  }
646
662
  async getPublicAgent(e) {
@@ -697,11 +713,11 @@ const K = class K {
697
713
  }
698
714
  }
699
715
  };
700
- h(K, "UserClient");
716
+ u(K, "UserClient");
701
717
  let N = K;
702
718
  const H = class H {
703
719
  constructor() {
704
- n(this, "key", "__PAGELINES_SDK__");
720
+ c(this, "key", "__PAGELINES_SDK__");
705
721
  }
706
722
  get() {
707
723
  if (!(typeof window > "u"))
@@ -714,33 +730,33 @@ const H = class H {
714
730
  typeof window < "u" && delete globalThis[this.key];
715
731
  }
716
732
  };
717
- h(H, "SDKGlobal");
733
+ u(H, "SDKGlobal");
718
734
  let C = H;
719
- const D = new C(), B = class B extends ae {
735
+ const D = new C(), B = class B extends ne {
720
736
  constructor(t = {}) {
721
737
  const r = D.get();
722
738
  if (r)
723
739
  return console.debug("[PageLinesSDK] Returning existing singleton instance"), r;
724
740
  super("PageLinesSDK", t);
725
741
  // Vue reactive state
726
- n(this, "activeUser", E());
727
- n(this, "token", E(null));
742
+ c(this, "activeUser", E());
743
+ c(this, "token", E(null));
728
744
  // Refresh credential lives in HttpOnly pl-refresh-token cookie — JS never
729
745
  // holds it, so no reactive ref. /api/auth/refresh reads the cookie via the
730
746
  // browser's automatic credentials: 'include' attach.
731
- n(this, "deviceId", E(null));
732
- n(this, "loading", E(!1));
733
- n(this, "error", E(null));
734
- n(this, "apiBase", this.settings.apiBase || void 0);
747
+ c(this, "deviceId", E(null));
748
+ c(this, "loading", E(!1));
749
+ c(this, "error", E(null));
750
+ c(this, "apiBase", this.settings.apiBase || void 0);
735
751
  // Storage handler for persistence
736
- n(this, "storage", new P());
752
+ c(this, "storage", new P());
737
753
  // Auto-managed session ID for usage tracking
738
- n(this, "sessionId", O({ prefix: "ses" }));
754
+ c(this, "sessionId", O({ prefix: "ses" }));
739
755
  // Typed Hono RPC client - uses SDKAppType (auth/self/usage routes only)
740
756
  // Initialized in constructor (definite assignment via singleton pattern)
741
- n(this, "apiClient");
757
+ c(this, "apiClient");
742
758
  // Computed properties derived from activeUser (matches UserClient pattern)
743
- n(this, "currentAgent", Q(() => {
759
+ c(this, "currentAgent", X(() => {
744
760
  const t = this.activeUser.value;
745
761
  if (!t?.agents)
746
762
  return;
@@ -748,7 +764,7 @@ const D = new C(), B = class B extends ae {
748
764
  if (r)
749
765
  return t.agents.find((s) => s.agentId === r);
750
766
  }));
751
- n(this, "currentOrg", Q(() => {
767
+ c(this, "currentOrg", X(() => {
752
768
  const t = this.currentAgent.value;
753
769
  if (!t?.orgId)
754
770
  return;
@@ -757,23 +773,23 @@ const D = new C(), B = class B extends ae {
757
773
  return r.orgs.find((s) => s.orgId === t.orgId);
758
774
  }));
759
775
  // Initialization state
760
- n(this, "initialized");
761
- n(this, "resolveUser");
776
+ c(this, "initialized");
777
+ c(this, "resolveUser");
762
778
  // Sub-clients (new preferred API surface)
763
- n(this, "agent");
764
- n(this, "auth");
765
- n(this, "billing");
766
- n(this, "chat");
767
- n(this, "user");
768
- D.set(this), this.apiClient = ne(
779
+ c(this, "agent");
780
+ c(this, "auth");
781
+ c(this, "billing");
782
+ c(this, "chat");
783
+ c(this, "user");
784
+ D.set(this), this.apiClient = le(
769
785
  { isDev: this.isDev, apiBase: this.apiBase },
770
786
  this.processApiResponse.bind(this),
771
787
  {
772
- getDeviceId: /* @__PURE__ */ h(() => this.deviceId.value, "getDeviceId"),
773
- onAccessTokenRefreshed: /* @__PURE__ */ h((o) => {
788
+ getDeviceId: /* @__PURE__ */ u(() => this.deviceId.value, "getDeviceId"),
789
+ onAccessTokenRefreshed: /* @__PURE__ */ u((o) => {
774
790
  this.token.value = o, this.persistAuth();
775
791
  }, "onAccessTokenRefreshed"),
776
- onTerminalFailure: /* @__PURE__ */ h(() => {
792
+ onTerminalFailure: /* @__PURE__ */ u(() => {
777
793
  this.clearSession();
778
794
  }, "onTerminalFailure")
779
795
  }
@@ -844,15 +860,15 @@ const D = new C(), B = class B extends ae {
844
860
  this.logger.info("Clearing SDK completely"), this.clearSession(), this.loading.value = !1, this.sessionId = O({ prefix: "ses" }), D.delete();
845
861
  }
846
862
  };
847
- h(B, "PageLinesSDK");
848
- let Y = B;
863
+ u(B, "PageLinesSDK");
864
+ let ee = B;
849
865
  export {
850
866
  _ as A,
851
867
  j as B,
852
868
  L as C,
853
- Y as P,
869
+ ee as P,
854
870
  N as U,
855
871
  F as a,
856
- x as r
872
+ y as r
857
873
  };
858
874
  //# sourceMappingURL=sdkClient.js.map