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