xtrace_sdk.integrations¶
Submodules¶
Classes¶
Async HTTP client for the XTrace encrypted vector database API. |
Package Contents¶
- class xtrace_sdk.integrations.XTraceIntegration(org_id, api_key=None, admin_key=None, api_url='https://api.production.xtrace.ai', admin_api_url=None)¶
Async HTTP client for the XTrace encrypted vector database API.
Handles all network communication with XTrace: uploading encrypted chunks, querying Hamming distances, metadata search, and execution context management.
Supports use as an async context manager for automatic session cleanup:
async with XTraceIntegration(org_id="your_org_id") as xtrace: await xtrace.store_db(...)
- Parameters:
- org_id¶
- api_url = 'https://api.production.xtrace.ai'¶
- admin_api_url¶
- batch_size = 500¶
- _loop: asyncio.AbstractEventLoop | None = None¶
- _lock¶
- api_key = None¶
- async init_session()¶
Ensure a live
aiohttp.ClientSessionexists for the current event loop.Creates or recreates the session if it is closed or belongs to a different loop. Called automatically before every API request.
- Returns:
The active client session.
- Return type:
aiohttp.ClientSession
- async close()¶
Close the underlying HTTP session and release all connections.
- Return type:
None
- async __aenter__()¶
- Return type:
- async __aexit__(exc_type, exc, tb)¶
- Parameters:
exc_type (Any)
exc (Any)
tb (Any)
- Return type:
None
- _preprocess_chunks(db, index, update=False)¶
- async get_chunk_by_id(chunk_ids, kb_id, projection=None)¶
Fetch one or more chunks by their IDs.
- Parameters:
- Returns:
List of chunk dicts. The
indexfield, if present, is base64-decoded to bytes.- Return type:
- async store_db(db, index, kb_id, context_id, concurrent=False)¶
Upload an encrypted database to XTrace, automatically batching into groups of 500.
- Parameters:
db (EncryptedDB) – Encrypted document collection produced by
DataLoader.index (EncryptedIndex) – Encrypted embedding vectors, one list per chunk.
kb_id (str) – Destination knowledge-base ID.
context_id (str) – Execution context ID to associate with the data.
concurrent (bool, optional) – If
True, upload batches concurrently withasyncio.gather. Defaults to sequential upload.
- Returns:
List of server responses, one per batch.
- Return type:
- async insert_chunks(kb_id, chunks, context_id)¶
Insert a pre-batched list of chunks. Retries up to 3 times on server errors.
Prefer
store_db()for most use cases — it handles batching automatically.
- async update_chunks(index_updates, chunk_updates, context_id, kb_id)¶
Replace existing chunks with updated content and re-encrypted vectors.
Each entry in
chunk_updatesmust include achunk_idfield.- Parameters:
- Returns:
List of server responses, one per batch.
- Return type:
- async delete_chunks(chunk_ids, kb_id)¶
Delete chunks by ID from a knowledge base.
- async patch_chunks(kb_id, context_id, chunk_patches)¶
Apply partial updates to existing chunks (e.g. metadata only).
- async compute_hamming_distances(query, context_id, kb_id, meta_filter=None, range=None)¶
Submit an encrypted query vector and retrieve encrypted Hamming distances.
The server computes distances between
queryand all stored vectors without decrypting either side. Results must be decrypted client-side with the homomorphic secret key.- Parameters:
query (list[gmpy2.mpz]) – Encrypted query vector produced by the homomorphic client.
context_id (str) – Execution context ID that matches the stored data.
kb_id (str) – Knowledge-base ID to search.
meta_filter (str | dict | None) – Optional metadata filter expression (dict or JSON string).
range (list[int] | None) – Optional
[min, max]range to limit which chunks are searched.
- Returns:
List of
(chunk_id, encrypted_distance)tuples.- Return type:
- async meta_search(kb_id, meta_filter, context_id, return_content=False, projection=None)¶
Search chunks by metadata filter expression, returning all matching results.
- Parameters:
kb_id (str) – Knowledge-base ID to search.
meta_filter (str or dict) – Filter expression as a dict or JSON string. Uses MongoDB-style operators (
$eq,$in,$and, etc.).context_id (str) – Execution context ID.
return_content (bool, optional) – If
True, include the (AES-encrypted) chunk content in results. Defaults toFalse.projection (list[str], optional) – Optional list of fields to include in each result.
- Returns:
List of matching chunk dicts.
- Return type:
- async meta_search_paginated(kb_id, context_id, meta_filter=None, projection=None, limit=None, offset=0, sort_order='desc', sort_by='chunk_id', return_content=False)¶
Search chunks by metadata filter with pagination support.
- Parameters:
kb_id (str) – Knowledge-base ID to search.
context_id (str) – Execution context ID.
meta_filter (str or dict, optional) – Optional filter expression as a dict or JSON string.
projection (list[str], optional) – Optional list of fields to include in each result.
limit (int, optional) – Maximum number of results to return. Returns all if omitted.
offset (int, optional) – Number of results to skip (for pagination), defaults to
0.sort_order (str, optional) –
"asc"or"desc", defaults to"desc".sort_by (str, optional) – Field to sort by, defaults to
"chunk_id".return_content (bool, optional) – If
True, include the (AES-encrypted) chunk content in results. Defaults toFalse.
- Returns:
Dict containing
results(list of chunk dicts) and pagination metadata.- Return type:
- async delete_chunks_by_meta(kb_id, context_id, meta_filter, dry_run=False)¶
Delete all chunks matching a metadata filter.
- Parameters:
- Returns:
Server response dict (includes
deleted_countorwould_delete_count).- Return type:
- async patch_chunks_by_meta(kb_id, context_id, meta_filter, patch, dry_run=False)¶
Apply a metadata patch to all chunks matching a filter expression.
- Parameters:
kb_id (str) – Knowledge-base ID to update.
context_id (str) – Execution context ID.
meta_filter (str or dict) – Filter expression as a dict or JSON string.
patch (dict) – Metadata fields to set on each matching chunk.
dry_run (bool, optional) – If
True, return a count of chunks that would be updated without modifying anything. Defaults toFalse.
- Returns:
Server response dict.
- Return type:
- async store_exec_context(exec_context_dict, context_id)¶
Upload a serialised execution context to XTrace remote storage.
The secret key inside
exec_context_dictmust already be AES-encrypted before calling this method. Usesave_to_remote()instead of calling this directly.
- async get_serialized_exec_context(context_id)¶
Fetch a serialised execution context from XTrace remote storage.
- async list_exec_contexts()¶
List all execution context IDs stored under this organisation.
- async delete_exec_context(context_id)¶
Delete an execution context from XTrace remote storage.
- async get_ctx_for_kb(kb_id)¶
Return the execution context IDs bound to a knowledge base.
- async get_kbs_for_ctx(ctx_id)¶
Return the knowledge-base IDs bound to an execution context.
- async register_kb_binding(kb_id, ctx_id)¶
Bind a knowledge base to an execution context on the server.
- async list_kbs()¶
List all knowledge bases for the organisation.
Requires an admin key (
admin_keyconstructor arg orXTRACE_ADMIN_KEYenv var).
- async get_kb(kb_id)¶
Fetch metadata for a single knowledge base.
Requires an admin key (
admin_keyconstructor arg orXTRACE_ADMIN_KEYenv var).
- async list_api_keys()¶
List all API keys for the organisation.
Requires an admin key (
admin_keyconstructor arg orXTRACE_ADMIN_KEYenv var).
- async get_key_permissions(key_hash)¶
Get KB permissions for an API key.
Requires an admin key (
admin_keyconstructor arg orXTRACE_ADMIN_KEYenv var).
- async set_key_permission(key_hash, kb_id, permission)¶
Set KB permission for an API key.
Requires an admin key (
admin_keyconstructor arg orXTRACE_ADMIN_KEYenv var).
- async create_kb(name, description='')¶
Create a new knowledge base.
Requires an admin key (
admin_keyconstructor arg orXTRACE_ADMIN_KEYenv var).
- async delete_kb(kb_id)¶
Permanently delete a knowledge base and all its chunks.
Requires an admin key (
admin_keyconstructor arg orXTRACE_ADMIN_KEYenv var).- Parameters:
kb_id (str) – ID of the knowledge base to delete.
- Return type:
None
- async create_api_key(name, description='')¶
Create a new API key for the organisation.
Requires an admin key (
admin_keyconstructor arg orXTRACE_ADMIN_KEYenv var).
- async revoke_api_key(key_hash)¶
Revoke an API key by its hash.
Requires an admin key (
admin_keyconstructor arg orXTRACE_ADMIN_KEYenv var).- Parameters:
key_hash (str) – Hash of the API key to revoke (from
create_api_key()).- Return type:
None