@devvibex/aiflow 1.6.0 → 1.7.0

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@devvibex/aiflow",
3
- "version": "1.6.0",
3
+ "version": "1.7.0",
4
4
  "description": "First-party LLM gateway SDK for apps generated on the VibeX platform — chat, streaming, image, video, music (Lyria), sound effects (ElevenLabs), embeddings and real-time speech translation (Gemini Live) without shipping any third-party API key.",
5
5
  "keywords": [
6
6
  "ai",
package/src/index.d.ts CHANGED
@@ -508,9 +508,16 @@ export interface AIFlowClientOptions {
508
508
  defaultSfxModel?: string | null;
509
509
  /**
510
510
  * Current end-user id (the app's logged-in user). Scopes the per-user
511
- * knowledge base; sent as `x-end-user-id`. Set/clear later with `setEndUser`.
511
+ * knowledge base. Set/clear later with `setEndUser`.
512
512
  */
513
513
  endUserId?: string | null;
514
+ /**
515
+ * The logged-in end user's access token (from your app's
516
+ * `POST /:projectKey/auth/login`). REQUIRED for the knowledge base: the
517
+ * gateway verifies it and derives a TRUSTED end-user id (sent as
518
+ * `x-app-user-token`). Set/clear later with `setEndUser(id, token)`.
519
+ */
520
+ appUserToken?: string | null;
514
521
  timeout?: number;
515
522
  maxRetries?: number;
516
523
  fetch?: typeof fetch;
@@ -604,6 +611,8 @@ export class AIFlowClient {
604
611
  readonly maxRetries: number;
605
612
  /** Current end-user id (or null). Set via `setEndUser`. */
606
613
  endUserId: string | null;
614
+ /** Current end-user access token (or null), sent as `x-app-user-token`. Set via `setEndUser`. */
615
+ appUserToken: string | null;
607
616
  /** Per-user knowledge base — upload / list / delete the current user's docs. */
608
617
  readonly knowledge: KnowledgeNamespace;
609
618
  /** Admin namespace — manage the app's AIFlow module config (needs `setAdminToken`). */
@@ -613,11 +622,13 @@ export class AIFlowClient {
613
622
 
614
623
  /**
615
624
  * Set (or clear with `null`) the current end user. Call after your app
616
- * authenticates a user — every subsequent knowledge AND chat call then
617
- * carries `x-end-user-id`, so the user's own uploaded documents ground their
618
- * answers. Returns `this` for chaining.
625
+ * authenticates a user, passing the user's id AND their access token (from
626
+ * your app's `POST /:projectKey/auth/login`). Every subsequent knowledge AND
627
+ * chat call then carries the verified `x-app-user-token`, so the gateway
628
+ * derives a TRUSTED end-user id and grounds answers in that user's own
629
+ * documents. The token is REQUIRED for the knowledge base. Returns `this`.
619
630
  */
620
- setEndUser(endUserId: string | null): this;
631
+ setEndUser(endUserId: string | null, appUserToken?: string | null): this;
621
632
 
622
633
  /**
623
634
  * Set a session-level default model for one generation kind. Wins over the
package/src/index.js CHANGED
@@ -28,7 +28,8 @@ export class AIFlowClient {
28
28
  * @param {string} options.baseUrl VibeX gateway base URL (e.g. "https://api.vibe-x.io/aiflow").
29
29
  * @param {string} [options.defaultModel]
30
30
  * @param {string} [options.defaultImageModel]
31
- * @param {string} [options.endUserId] Current end-user id (the app's logged-in user). Scopes the per-user knowledge base; sent as `x-end-user-id`. Set/clear later with `setEndUser()`.
31
+ * @param {string} [options.endUserId] Current end-user id (the app's logged-in user). Scopes the per-user knowledge base. Set/clear later with `setEndUser()`.
32
+ * @param {string} [options.appUserToken] The logged-in end user's access token (from your app's `POST /:projectKey/auth/login`). REQUIRED for the knowledge base: the gateway verifies it and derives a TRUSTED end-user id (sent as `x-app-user-token`). Set/clear later with `setEndUser(id, token)`.
32
33
  * @param {number} [options.timeout] Per-request timeout (ms). Default 60000.
33
34
  * @param {number} [options.maxRetries] Retries on 429/5xx / network errors. Default 2.
34
35
  * @param {typeof fetch} [options.fetch] Custom fetch implementation.
@@ -43,6 +44,7 @@ export class AIFlowClient {
43
44
  defaultAudioModel = null,
44
45
  defaultSfxModel = null,
45
46
  endUserId = null,
47
+ appUserToken = null,
46
48
  timeout = DEFAULT_TIMEOUT_MS,
47
49
  maxRetries = DEFAULT_MAX_RETRIES,
48
50
  fetch: customFetch,
@@ -55,6 +57,10 @@ export class AIFlowClient {
55
57
  // Current end user (the app's logged-in user). Scopes the knowledge base
56
58
  // so each user only ever sees/retrieves their own uploaded documents.
57
59
  this.endUserId = endUserId ? String(endUserId) : null;
60
+ // The end user's access token (issued by your app's own login). The
61
+ // gateway VERIFIES it and derives a trusted end-user id from it — this is
62
+ // what secures the per-user knowledge base against a spoofed id header.
63
+ this.appUserToken = appUserToken ? String(appUserToken) : null;
58
64
  // Caller-provided overrides — win over server-fetched config.
59
65
  this._userDefaults = {
60
66
  chat: defaultModel,
@@ -114,13 +120,23 @@ export class AIFlowClient {
114
120
 
115
121
  /**
116
122
  * Set (or clear) the current end user. Call after your app authenticates a
117
- * user — every subsequent knowledge AND chat call carries `x-end-user-id`,
118
- * so the user's own uploaded documents ground their answers. Pass `null` on
119
- * logout. Returns `this` for chaining.
123
+ * user, passing the user's id AND their access token (the one your app got
124
+ * from `POST /:projectKey/auth/login`). Every subsequent knowledge AND chat
125
+ * call then carries the verified `x-app-user-token`, so the gateway can
126
+ * derive a TRUSTED end-user id and ground answers in that user's own
127
+ * documents. Pass `null` (or no args) on logout to clear both. Returns
128
+ * `this` for chaining.
129
+ *
130
+ * The token is REQUIRED for the knowledge base to work securely: without it
131
+ * the gateway has no authenticated identity, so uploads are rejected and
132
+ * chat is not grounded in the user's documents (the legacy id-only mode is
133
+ * off by default because a raw id header is spoofable).
120
134
  * @param {string|null} endUserId
135
+ * @param {string|null} [appUserToken] The user's access token from your app's login.
121
136
  */
122
- setEndUser(endUserId) {
137
+ setEndUser(endUserId, appUserToken = null) {
123
138
  this.endUserId = endUserId ? String(endUserId) : null;
139
+ this.appUserToken = appUserToken ? String(appUserToken) : null;
124
140
  return this;
125
141
  }
126
142
 
@@ -717,11 +733,15 @@ export class AIFlowClient {
717
733
  // ────────────────────────────────────────────────────────────────────────
718
734
 
719
735
  /**
720
- * Auth/identity headers attached to every gateway call: the public app id
721
- * and (when set) the current end-user id that scopes the knowledge base.
736
+ * Auth/identity headers attached to every gateway call: the public app id,
737
+ * the current end-user's VERIFIED access token (`x-app-user-token`, the
738
+ * secure identity the gateway trusts), and the end-user id (`x-end-user-id`,
739
+ * legacy/diagnostic only — the gateway ignores it unless an operator opts
740
+ * into the unverified mode).
722
741
  */
723
742
  _authHeaders() {
724
743
  const h = { 'x-app-id': this.appId };
744
+ if (this.appUserToken) h['x-app-user-token'] = this.appUserToken;
725
745
  if (this.endUserId) h['x-end-user-id'] = this.endUserId;
726
746
  return h;
727
747
  }
@@ -863,9 +883,11 @@ export class AIFlowClient {
863
883
  if (!file) {
864
884
  throw new AIFlowError('a File/Blob is required to ingest knowledge');
865
885
  }
866
- if (!this.endUserId) {
886
+ if (!this.appUserToken) {
867
887
  throw new AIFlowError(
868
- 'setEndUser(id) must be called before uploading knowledge',
888
+ 'setEndUser(id, appUserToken) must be called with the logged-in ' +
889
+ "user's access token before uploading knowledge — the gateway " +
890
+ 'verifies it to scope uploads to that user.',
869
891
  );
870
892
  }
871
893
  if (typeof FormData === 'undefined') {