@fluxbase/sdk 2026.2.3-rc.9 → 2026.2.3

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/index.js CHANGED
@@ -3643,8 +3643,43 @@ var FluxbaseFunctions = class {
3643
3643
 
3644
3644
  // src/jobs.ts
3645
3645
  var FluxbaseJobs = class {
3646
- constructor(fetch2) {
3646
+ constructor(fetch2, isServiceRole = false) {
3647
+ this.cachedRole = null;
3648
+ this.ROLE_CACHE_TTL = 5 * 60 * 1e3;
3647
3649
  this.fetch = fetch2;
3650
+ this.isServiceRole = isServiceRole;
3651
+ }
3652
+ /**
3653
+ * Get the current user's role from user_profiles table
3654
+ * This is used to auto-populate the onBehalfOf parameter when submitting jobs
3655
+ *
3656
+ * @returns Promise resolving to the user's role or null if not found
3657
+ */
3658
+ async getCurrentUserRole(userId) {
3659
+ try {
3660
+ const now = Date.now();
3661
+ if (this.cachedRole && this.cachedRole.userId === userId && this.cachedRole.expiresAt > now) {
3662
+ return this.cachedRole.role;
3663
+ }
3664
+ const profileResult = await this.fetch.get(
3665
+ `/rest/v1/user_profiles?id=eq.${userId}&select=role`
3666
+ );
3667
+ if (!profileResult || !profileResult[0]) {
3668
+ return null;
3669
+ }
3670
+ const role = profileResult[0].role;
3671
+ if (role) {
3672
+ this.cachedRole = {
3673
+ userId,
3674
+ role,
3675
+ expiresAt: now + this.ROLE_CACHE_TTL
3676
+ };
3677
+ }
3678
+ return role;
3679
+ } catch (error) {
3680
+ console.warn("[FluxbaseJobs] Failed to fetch user role:", error);
3681
+ return null;
3682
+ }
3648
3683
  }
3649
3684
  /**
3650
3685
  * Submit a new job for execution
@@ -3689,13 +3724,27 @@ var FluxbaseJobs = class {
3689
3724
  */
3690
3725
  async submit(jobName, payload, options) {
3691
3726
  try {
3727
+ let finalOnBehalfOf = options?.onBehalfOf;
3728
+ if (this.isServiceRole && !finalOnBehalfOf) {
3729
+ const userResponse = await this.fetch.get("/api/v1/auth/user");
3730
+ if (userResponse?.user) {
3731
+ const user = userResponse.user;
3732
+ const role = await this.getCurrentUserRole(user.id);
3733
+ finalOnBehalfOf = {
3734
+ user_id: user.id,
3735
+ user_email: user.email,
3736
+ user_role: role || void 0
3737
+ // Include role if found
3738
+ };
3739
+ }
3740
+ }
3692
3741
  const request = {
3693
3742
  job_name: jobName,
3694
3743
  payload,
3695
3744
  priority: options?.priority,
3696
3745
  namespace: options?.namespace,
3697
3746
  scheduled: options?.scheduled,
3698
- on_behalf_of: options?.onBehalfOf
3747
+ on_behalf_of: finalOnBehalfOf
3699
3748
  };
3700
3749
  const data = await this.fetch.post("/api/v1/jobs/submit", request);
3701
3750
  return { data, error: null };
@@ -8512,6 +8561,454 @@ var FluxbaseAdminAI = class {
8512
8561
  return { data: null, error };
8513
8562
  }
8514
8563
  }
8564
+ // ============================================================================
8565
+ // DOCUMENT UPDATE AND BULK DELETE
8566
+ // ============================================================================
8567
+ /**
8568
+ * Update a document in a knowledge base
8569
+ *
8570
+ * @param knowledgeBaseId - Knowledge base ID
8571
+ * @param documentId - Document ID
8572
+ * @param updates - Fields to update
8573
+ * @returns Promise resolving to { data, error } tuple with updated document
8574
+ *
8575
+ * @example
8576
+ * ```typescript
8577
+ * const { data, error } = await client.admin.ai.updateDocument('kb-uuid', 'doc-uuid', {
8578
+ * title: 'Updated Title',
8579
+ * tags: ['updated', 'tag'],
8580
+ * metadata: { category: 'updated' },
8581
+ * })
8582
+ * ```
8583
+ */
8584
+ async updateDocument(knowledgeBaseId, documentId, updates) {
8585
+ try {
8586
+ const data = await this.fetch.patch(
8587
+ `/api/v1/admin/ai/knowledge-bases/${knowledgeBaseId}/documents/${documentId}`,
8588
+ updates
8589
+ );
8590
+ return { data, error: null };
8591
+ } catch (error) {
8592
+ return { data: null, error };
8593
+ }
8594
+ }
8595
+ /**
8596
+ * Delete documents from a knowledge base by filter
8597
+ *
8598
+ * @param knowledgeBaseId - Knowledge base ID
8599
+ * @param filter - Filter criteria for deletion
8600
+ * @returns Promise resolving to { data, error } tuple with deletion count
8601
+ *
8602
+ * @example
8603
+ * ```typescript
8604
+ * // Delete by tags
8605
+ * const { data, error } = await client.admin.ai.deleteDocumentsByFilter('kb-uuid', {
8606
+ * tags: ['deprecated', 'archive'],
8607
+ * })
8608
+ *
8609
+ * // Delete by metadata
8610
+ * const { data, error } = await client.admin.ai.deleteDocumentsByFilter('kb-uuid', {
8611
+ * metadata: { source: 'legacy-system' },
8612
+ * })
8613
+ *
8614
+ * if (data) {
8615
+ * console.log(`Deleted ${data.deleted_count} documents`)
8616
+ * }
8617
+ * ```
8618
+ */
8619
+ async deleteDocumentsByFilter(knowledgeBaseId, filter) {
8620
+ try {
8621
+ const data = await this.fetch.post(
8622
+ `/api/v1/admin/ai/knowledge-bases/${knowledgeBaseId}/documents/delete-by-filter`,
8623
+ filter
8624
+ );
8625
+ return { data, error: null };
8626
+ } catch (error) {
8627
+ return { data: null, error };
8628
+ }
8629
+ }
8630
+ // ============================================================================
8631
+ // KNOWLEDGE BASE CAPABILITIES
8632
+ // ============================================================================
8633
+ /**
8634
+ * Get knowledge base system capabilities
8635
+ *
8636
+ * Returns information about OCR support, supported file types, etc.
8637
+ *
8638
+ * @returns Promise resolving to { data, error } tuple with capabilities
8639
+ *
8640
+ * @example
8641
+ * ```typescript
8642
+ * const { data, error } = await client.admin.ai.getCapabilities()
8643
+ * if (data) {
8644
+ * console.log('OCR available:', data.ocr_available)
8645
+ * console.log('Supported types:', data.supported_file_types)
8646
+ * }
8647
+ * ```
8648
+ */
8649
+ async getCapabilities() {
8650
+ try {
8651
+ const data = await this.fetch.get(
8652
+ "/api/v1/admin/ai/knowledge-bases/capabilities"
8653
+ );
8654
+ return { data, error: null };
8655
+ } catch (error) {
8656
+ return { data: null, error };
8657
+ }
8658
+ }
8659
+ // ============================================================================
8660
+ // KNOWLEDGE GRAPH / ENTITIES
8661
+ // ============================================================================
8662
+ /**
8663
+ * List entities in a knowledge base
8664
+ *
8665
+ * @param knowledgeBaseId - Knowledge base ID
8666
+ * @param entityType - Optional entity type filter
8667
+ * @returns Promise resolving to { data, error } tuple with array of entities
8668
+ *
8669
+ * @example
8670
+ * ```typescript
8671
+ * // List all entities
8672
+ * const { data, error } = await client.admin.ai.listEntities('kb-uuid')
8673
+ *
8674
+ * // Filter by type
8675
+ * const { data, error } = await client.admin.ai.listEntities('kb-uuid', 'person')
8676
+ *
8677
+ * if (data) {
8678
+ * console.log('Entities:', data.map(e => e.name))
8679
+ * }
8680
+ * ```
8681
+ */
8682
+ async listEntities(knowledgeBaseId, entityType) {
8683
+ try {
8684
+ const params = entityType ? `?entity_type=${entityType}` : "";
8685
+ const response = await this.fetch.get(
8686
+ `/api/v1/admin/ai/knowledge-bases/${knowledgeBaseId}/entities${params}`
8687
+ );
8688
+ return { data: response.entities || [], error: null };
8689
+ } catch (error) {
8690
+ return { data: null, error };
8691
+ }
8692
+ }
8693
+ /**
8694
+ * Search for entities in a knowledge base
8695
+ *
8696
+ * @param knowledgeBaseId - Knowledge base ID
8697
+ * @param query - Search query
8698
+ * @param types - Optional entity type filters
8699
+ * @returns Promise resolving to { data, error } tuple with matching entities
8700
+ *
8701
+ * @example
8702
+ * ```typescript
8703
+ * // Search all entity types
8704
+ * const { data, error } = await client.admin.ai.searchEntities('kb-uuid', 'John')
8705
+ *
8706
+ * // Search specific types
8707
+ * const { data, error } = await client.admin.ai.searchEntities('kb-uuid', 'Apple', ['organization', 'product'])
8708
+ *
8709
+ * if (data) {
8710
+ * console.log('Found entities:', data.map(e => `${e.name} (${e.entity_type})`))
8711
+ * }
8712
+ * ```
8713
+ */
8714
+ async searchEntities(knowledgeBaseId, query, types) {
8715
+ try {
8716
+ const params = new URLSearchParams({ query });
8717
+ if (types && types.length > 0) {
8718
+ params.append("types", types.join(","));
8719
+ }
8720
+ const response = await this.fetch.get(
8721
+ `/api/v1/admin/ai/knowledge-bases/${knowledgeBaseId}/entities/search?${params.toString()}`
8722
+ );
8723
+ return { data: response.entities || [], error: null };
8724
+ } catch (error) {
8725
+ return { data: null, error };
8726
+ }
8727
+ }
8728
+ /**
8729
+ * Get relationships for a specific entity
8730
+ *
8731
+ * @param knowledgeBaseId - Knowledge base ID
8732
+ * @param entityId - Entity ID
8733
+ * @returns Promise resolving to { data, error } tuple with entity relationships
8734
+ *
8735
+ * @example
8736
+ * ```typescript
8737
+ * const { data, error } = await client.admin.ai.getEntityRelationships('kb-uuid', 'entity-uuid')
8738
+ * if (data) {
8739
+ * console.log('Relationships:', data.map(r => `${r.relationship_type} -> ${r.target_entity?.name}`))
8740
+ * }
8741
+ * ```
8742
+ */
8743
+ async getEntityRelationships(knowledgeBaseId, entityId) {
8744
+ try {
8745
+ const response = await this.fetch.get(
8746
+ `/api/v1/admin/ai/knowledge-bases/${knowledgeBaseId}/entities/${entityId}/relationships`
8747
+ );
8748
+ return { data: response.relationships || [], error: null };
8749
+ } catch (error) {
8750
+ return { data: null, error };
8751
+ }
8752
+ }
8753
+ /**
8754
+ * Get the knowledge graph for a knowledge base
8755
+ *
8756
+ * Returns all entities and relationships for visualization.
8757
+ *
8758
+ * @param knowledgeBaseId - Knowledge base ID
8759
+ * @returns Promise resolving to { data, error } tuple with graph data
8760
+ *
8761
+ * @example
8762
+ * ```typescript
8763
+ * const { data, error } = await client.admin.ai.getKnowledgeGraph('kb-uuid')
8764
+ * if (data) {
8765
+ * console.log('Graph:', data.entity_count, 'entities,', data.relationship_count, 'relationships')
8766
+ * // Use with visualization libraries like D3.js, Cytoscape.js, etc.
8767
+ * }
8768
+ * ```
8769
+ */
8770
+ async getKnowledgeGraph(knowledgeBaseId) {
8771
+ try {
8772
+ const data = await this.fetch.get(
8773
+ `/api/v1/admin/ai/knowledge-bases/${knowledgeBaseId}/graph`
8774
+ );
8775
+ return { data, error: null };
8776
+ } catch (error) {
8777
+ return { data: null, error };
8778
+ }
8779
+ }
8780
+ // ============================================================================
8781
+ // KNOWLEDGE BASE REVERSE LOOKUP
8782
+ // ============================================================================
8783
+ /**
8784
+ * List all chatbots that use a specific knowledge base
8785
+ *
8786
+ * Reverse lookup to find which chatbots are linked to a knowledge base.
8787
+ *
8788
+ * @param knowledgeBaseId - Knowledge base ID
8789
+ * @returns Promise resolving to { data, error } tuple with array of chatbot summaries
8790
+ *
8791
+ * @example
8792
+ * ```typescript
8793
+ * const { data, error } = await client.admin.ai.listChatbotsUsingKB('kb-uuid')
8794
+ * if (data) {
8795
+ * console.log('Used by chatbots:', data.map(c => c.name))
8796
+ * }
8797
+ * ```
8798
+ */
8799
+ async listChatbotsUsingKB(knowledgeBaseId) {
8800
+ try {
8801
+ const response = await this.fetch.get(`/api/v1/admin/ai/knowledge-bases/${knowledgeBaseId}/chatbots`);
8802
+ return { data: response.chatbots || [], error: null };
8803
+ } catch (error) {
8804
+ return { data: null, error };
8805
+ }
8806
+ }
8807
+ // ============================================================================
8808
+ // TABLE EXPORT
8809
+ // ============================================================================
8810
+ /**
8811
+ * Export a database table to a knowledge base
8812
+ *
8813
+ * The table schema will be exported as a markdown document and indexed.
8814
+ * Optionally filter which columns to export for security or relevance.
8815
+ *
8816
+ * @param knowledgeBaseId - Knowledge base ID
8817
+ * @param options - Export options including column selection
8818
+ * @returns Promise resolving to { data, error } tuple with export result
8819
+ *
8820
+ * @example
8821
+ * ```typescript
8822
+ * // Export all columns
8823
+ * const { data, error } = await client.admin.ai.exportTable('kb-uuid', {
8824
+ * schema: 'public',
8825
+ * table: 'users',
8826
+ * include_foreign_keys: true,
8827
+ * })
8828
+ *
8829
+ * // Export specific columns (recommended for sensitive data)
8830
+ * const { data, error } = await client.admin.ai.exportTable('kb-uuid', {
8831
+ * schema: 'public',
8832
+ * table: 'users',
8833
+ * columns: ['id', 'name', 'email', 'created_at'],
8834
+ * })
8835
+ * ```
8836
+ */
8837
+ async exportTable(knowledgeBaseId, options) {
8838
+ try {
8839
+ const data = await this.fetch.post(
8840
+ `/api/v1/admin/ai/knowledge-bases/${knowledgeBaseId}/tables/export`,
8841
+ options
8842
+ );
8843
+ return { data, error: null };
8844
+ } catch (error) {
8845
+ return { data: null, error };
8846
+ }
8847
+ }
8848
+ /**
8849
+ * Get detailed table information including columns
8850
+ *
8851
+ * Use this to discover available columns before exporting.
8852
+ *
8853
+ * @param schema - Schema name (e.g., 'public')
8854
+ * @param table - Table name
8855
+ * @returns Promise resolving to { data, error } tuple with table details
8856
+ *
8857
+ * @example
8858
+ * ```typescript
8859
+ * const { data, error } = await client.admin.ai.getTableDetails('public', 'users')
8860
+ * if (data) {
8861
+ * console.log('Columns:', data.columns.map(c => c.name))
8862
+ * console.log('Primary key:', data.primary_key)
8863
+ * }
8864
+ * ```
8865
+ */
8866
+ async getTableDetails(schema, table) {
8867
+ try {
8868
+ const data = await this.fetch.get(
8869
+ `/api/v1/admin/ai/tables/${schema}/${table}`
8870
+ );
8871
+ return { data, error: null };
8872
+ } catch (error) {
8873
+ return { data: null, error };
8874
+ }
8875
+ }
8876
+ // ============================================================================
8877
+ // TABLE EXPORT PRESETS
8878
+ // ============================================================================
8879
+ /**
8880
+ * Create a table export preset
8881
+ *
8882
+ * Saves a table export configuration for easy re-export. Use triggerTableExportSync
8883
+ * to re-export when the schema changes.
8884
+ *
8885
+ * @param knowledgeBaseId - Knowledge base ID
8886
+ * @param config - Export preset configuration
8887
+ * @returns Promise resolving to { data, error } tuple with created preset
8888
+ *
8889
+ * @example
8890
+ * ```typescript
8891
+ * const { data, error } = await client.admin.ai.createTableExportSync('kb-uuid', {
8892
+ * schema_name: 'public',
8893
+ * table_name: 'products',
8894
+ * columns: ['id', 'name', 'description', 'price'],
8895
+ * include_foreign_keys: true,
8896
+ * export_now: true, // Trigger initial export
8897
+ * })
8898
+ * ```
8899
+ */
8900
+ async createTableExportSync(knowledgeBaseId, config) {
8901
+ try {
8902
+ const data = await this.fetch.post(
8903
+ `/api/v1/admin/ai/knowledge-bases/${knowledgeBaseId}/sync-configs`,
8904
+ config
8905
+ );
8906
+ return { data, error: null };
8907
+ } catch (error) {
8908
+ return { data: null, error };
8909
+ }
8910
+ }
8911
+ /**
8912
+ * List table export presets for a knowledge base
8913
+ *
8914
+ * @param knowledgeBaseId - Knowledge base ID
8915
+ * @returns Promise resolving to { data, error } tuple with array of presets
8916
+ *
8917
+ * @example
8918
+ * ```typescript
8919
+ * const { data, error } = await client.admin.ai.listTableExportSyncs('kb-uuid')
8920
+ * if (data) {
8921
+ * data.forEach(config => {
8922
+ * console.log(`${config.schema_name}.${config.table_name}`)
8923
+ * })
8924
+ * }
8925
+ * ```
8926
+ */
8927
+ async listTableExportSyncs(knowledgeBaseId) {
8928
+ try {
8929
+ const response = await this.fetch.get(`/api/v1/admin/ai/knowledge-bases/${knowledgeBaseId}/sync-configs`);
8930
+ return { data: response.sync_configs || [], error: null };
8931
+ } catch (error) {
8932
+ return { data: null, error };
8933
+ }
8934
+ }
8935
+ /**
8936
+ * Update a table export preset
8937
+ *
8938
+ * @param knowledgeBaseId - Knowledge base ID
8939
+ * @param syncId - Preset ID
8940
+ * @param updates - Fields to update
8941
+ * @returns Promise resolving to { data, error } tuple with updated preset
8942
+ *
8943
+ * @example
8944
+ * ```typescript
8945
+ * const { data, error } = await client.admin.ai.updateTableExportSync('kb-uuid', 'sync-id', {
8946
+ * columns: ['id', 'name', 'email', 'updated_at'],
8947
+ * })
8948
+ * ```
8949
+ */
8950
+ async updateTableExportSync(knowledgeBaseId, syncId, updates) {
8951
+ try {
8952
+ const data = await this.fetch.patch(
8953
+ `/api/v1/admin/ai/knowledge-bases/${knowledgeBaseId}/sync-configs/${syncId}`,
8954
+ updates
8955
+ );
8956
+ return { data, error: null };
8957
+ } catch (error) {
8958
+ return { data: null, error };
8959
+ }
8960
+ }
8961
+ /**
8962
+ * Delete a table export sync configuration
8963
+ *
8964
+ * @param knowledgeBaseId - Knowledge base ID
8965
+ * @param syncId - Sync config ID
8966
+ * @returns Promise resolving to { data, error } tuple
8967
+ *
8968
+ * @example
8969
+ * ```typescript
8970
+ * const { data, error } = await client.admin.ai.deleteTableExportSync('kb-uuid', 'sync-id')
8971
+ * ```
8972
+ */
8973
+ async deleteTableExportSync(knowledgeBaseId, syncId) {
8974
+ try {
8975
+ await this.fetch.delete(
8976
+ `/api/v1/admin/ai/knowledge-bases/${knowledgeBaseId}/sync-configs/${syncId}`
8977
+ );
8978
+ return { data: null, error: null };
8979
+ } catch (error) {
8980
+ return { data: null, error };
8981
+ }
8982
+ }
8983
+ /**
8984
+ * Manually trigger a table export sync
8985
+ *
8986
+ * Immediately re-exports the table to the knowledge base,
8987
+ * regardless of the sync mode.
8988
+ *
8989
+ * @param knowledgeBaseId - Knowledge base ID
8990
+ * @param syncId - Sync config ID
8991
+ * @returns Promise resolving to { data, error } tuple with export result
8992
+ *
8993
+ * @example
8994
+ * ```typescript
8995
+ * const { data, error } = await client.admin.ai.triggerTableExportSync('kb-uuid', 'sync-id')
8996
+ * if (data) {
8997
+ * console.log('Exported document:', data.document_id)
8998
+ * }
8999
+ * ```
9000
+ */
9001
+ async triggerTableExportSync(knowledgeBaseId, syncId) {
9002
+ try {
9003
+ const data = await this.fetch.post(
9004
+ `/api/v1/admin/ai/knowledge-bases/${knowledgeBaseId}/sync-configs/${syncId}/trigger`,
9005
+ {}
9006
+ );
9007
+ return { data, error: null };
9008
+ } catch (error) {
9009
+ return { data: null, error };
9010
+ }
9011
+ }
8515
9012
  };
8516
9013
 
8517
9014
  // src/admin-rpc.ts
@@ -12088,7 +12585,7 @@ var FluxbaseClient = class {
12088
12585
  );
12089
12586
  this.storage = new FluxbaseStorage(this.fetch);
12090
12587
  this.functions = new FluxbaseFunctions(this.fetch);
12091
- this.jobs = new FluxbaseJobs(this.fetch);
12588
+ this.jobs = new FluxbaseJobs(this.fetch, options?.serviceRole ?? false);
12092
12589
  this.admin = new FluxbaseAdmin(this.fetch);
12093
12590
  this.management = new FluxbaseManagement(this.fetch);
12094
12591
  this.settings = new SettingsClient(this.fetch);
@@ -12290,18 +12787,28 @@ function getEnvVar(name) {
12290
12787
  }
12291
12788
  function createClient(fluxbaseUrl, fluxbaseKey, options) {
12292
12789
  const url = fluxbaseUrl || getEnvVar("FLUXBASE_URL") || getEnvVar("NEXT_PUBLIC_FLUXBASE_URL") || getEnvVar("VITE_FLUXBASE_URL");
12293
- const key = fluxbaseKey || getEnvVar("FLUXBASE_ANON_KEY") || getEnvVar("FLUXBASE_SERVICE_TOKEN") || getEnvVar("FLUXBASE_JOB_TOKEN") || getEnvVar("NEXT_PUBLIC_FLUXBASE_ANON_KEY") || getEnvVar("VITE_FLUXBASE_ANON_KEY");
12790
+ const resolvedKey = fluxbaseKey || getEnvVar("FLUXBASE_SERVICE_TOKEN") || getEnvVar("FLUXBASE_ANON_KEY") || getEnvVar("FLUXBASE_JOB_TOKEN") || getEnvVar("NEXT_PUBLIC_FLUXBASE_ANON_KEY") || getEnvVar("VITE_FLUXBASE_ANON_KEY");
12791
+ let isServiceRole = options?.serviceRole ?? false;
12792
+ if (!fluxbaseKey && getEnvVar("FLUXBASE_SERVICE_TOKEN")) {
12793
+ isServiceRole = true;
12794
+ }
12795
+ if (options?.serviceRole !== void 0) {
12796
+ isServiceRole = options.serviceRole;
12797
+ }
12294
12798
  if (!url) {
12295
12799
  throw new Error(
12296
12800
  "Fluxbase URL is required. Pass it as the first argument or set FLUXBASE_URL environment variable."
12297
12801
  );
12298
12802
  }
12299
- if (!key) {
12803
+ if (!resolvedKey) {
12300
12804
  throw new Error(
12301
12805
  "Fluxbase key is required. Pass it as the second argument or set FLUXBASE_ANON_KEY environment variable."
12302
12806
  );
12303
12807
  }
12304
- return new FluxbaseClient(url, key, options);
12808
+ return new FluxbaseClient(url, resolvedKey, {
12809
+ ...options,
12810
+ serviceRole: isServiceRole
12811
+ });
12305
12812
  }
12306
12813
 
12307
12814
  // src/type-guards.ts