Skip to main content

TypeScript SDK: Spaces & Clusters

Organize memories into isolated spaces and logical clusters.

Overview

Spaces provide isolated knowledge bases for separating different projects, users, or contexts. Clusters group related memories together within spaces for better organization.

Use spaces for multi-tenant applications where each user or organization needs their own isolated memory store.

Spaces have human-readable slugs (e.g., project-alpha) that you can use instead of IDs. Slugs are auto-generated from names or can be customized.

Spaces

Spaces provide isolated knowledge bases. Use them to separate different projects, users, or contexts.

Create Space

create-space.ts
const space = await trix.spaces.create({
  name: 'Project Alpha',
  slug: 'alpha',  // Optional - auto-generated if not provided
  description: 'Knowledge base for Project Alpha'
});

console.log('Space ID:', space.id);
console.log('Slug:', space.slug);  // alpha

List Spaces

list-spaces.ts
// List all spaces (returns Space[])
const spaces = await trix.spaces.list();

for (const space of spaces) {
  console.log(`${space.name} (${space.slug}): ${space.description ?? ''}`);
}

Get Space

get-space.ts
// Get by slug (recommended - human-friendly)
const space = await trix.spaces.get('alpha');

// Or by slug method
const space = await trix.spaces.getBySlug('alpha');

Update Space

update-space.ts
await trix.spaces.update('alpha', {
  name: 'Project Alpha v2',
  slug: 'alpha-v2',  // Old slug will redirect to this
  description: 'Updated knowledge base'
});

Delete Space

delete-space.ts
await trix.spaces.delete('space_old');

Deleting a space will also delete all memories, clusters, and relationships within that space. This action cannot be undone.

Working with Space Memories

Create and query memories within a specific space:

space-memories.ts
// Create memory in a space
const memory = await trix.memories.create({
  content: 'Project Alpha requirements document',
  spaceId: 'space_alpha',
  tags: ['requirements', 'documentation']
});

// List memories in a space
const { data: memories } = await trix.memories.list({
  spaceId: 'space_alpha',
  limit: 20
});

// Search within a space
const results = await trix.memories.list({
  q: 'requirements',
  spaceId: 'space_alpha',
  mode: 'hybrid'
});

Clusters

Clusters group related memories together. Use them to organize knowledge into logical collections.

Create Cluster

create-cluster.ts
const cluster = await trix.clusters.create({
  name: 'TypeScript Best Practices',
  description: 'Collection of TypeScript patterns and tips',
  memoryIds: ['mem_1', 'mem_2', 'mem_3'],
  spaceId: 'space_dev',
  metadata: {
    category: 'programming',
    priority: 'high'
  }
});

console.log('Cluster ID:', cluster.id);
console.log('Memory count:', cluster.memoryIds?.length ?? 0);

Add Memory to Cluster

add-to-cluster.ts
// Add memory to cluster with optional relevance score
await trix.clusters.addMemory('cluster_abc', 'mem_new', 0.85);

// Add multiple memories
for (const memoryId of ['mem_4', 'mem_5', 'mem_6']) {
  await trix.clusters.addMemory('cluster_abc', memoryId);
}

Remove Memory from Cluster

remove-from-cluster.ts
await trix.clusters.removeMemory('cluster_abc', 'mem_old');

Expand Cluster

Automatically find and add similar memories to a cluster:

expand-cluster.ts
const expansion = await trix.clusters.expand('cluster_abc', {
  limit: 10,
  threshold: 0.7                          // Minimum similarity
});

console.log('Cluster:', expansion.clusterId);
console.log('Suggestions:', expansion.newMemories.length);

for (const suggestion of expansion.newMemories) {
  console.log(`  ${suggestion.memoryId} (confidence: ${suggestion.confidence.toFixed(2)})`);
}

List Clusters

list-clusters.ts
// Paginated list
const { data, pagination } = await trix.clusters.list({
  limit: 20,
  spaceId: 'space_dev'
});

// Async iteration
for await (const cluster of trix.clusters.listAll()) {
  console.log(cluster.name, cluster.memoryIds?.length ?? 0);
}

Get Cluster

get-cluster.ts
const cluster = await trix.clusters.get('cluster_abc');

console.log('Name:', cluster.name);
console.log('Description:', cluster.description);
console.log('Memories:', cluster.memoryIds?.length ?? 0);

// Get cluster with full memory objects
const clusterWithMemories = await trix.clusters.get('cluster_abc', {
  includeMemories: true
});

for (const memory of clusterWithMemories.memories ?? []) {
  console.log('-', memory.content.slice(0, 50));
}

Update Cluster

update-cluster.ts
await trix.clusters.update('cluster_abc', {
  name: 'TypeScript Advanced Patterns',
  description: 'Updated collection of advanced patterns',
  metadata: {
    category: 'programming',
    level: 'advanced'
  }
});

Delete Cluster

delete-cluster.ts
// Delete cluster (memories are preserved)
await trix.clusters.delete('cluster_abc');

Deleting a cluster only removes the cluster itself. The memories within the cluster are preserved and remain accessible.

Automatic Clustering

Trix can automatically discover clusters based on semantic similarity:

auto-clustering.ts
// List clusters (paginated)
const { data: clusters } = await trix.clusters.list();

for (const cluster of clusters) {
  console.log(`${cluster.name}: ${cluster.memoryIds?.length ?? 0} memories`);
}

// Get details for a specific cluster
const cluster = await trix.clusters.get('cluster_abc123');
console.log('Cluster:', cluster.name);

Complete Example

Here's a complete example showing how to organize a multi-tenant application:

workspace-example.ts
1import { Trix } from '@trixdb/client';
2
3async function setupUserWorkspace(userId: string) {
4  const trix = Trix.fromEnv();
5
6  // Create a space for the user
7  const space = await trix.spaces.create({
8    name: `User ${userId} Workspace`,
9    metadata: { userId, tier: 'premium' }
10  });
11
12  // Create initial clusters
13  const workCluster = await trix.clusters.create({
14    name: 'Work Notes',
15    spaceId: space.id,
16    metadata: { category: 'work' }
17  });
18
19  const personalCluster = await trix.clusters.create({
20    name: 'Personal Notes',
21    spaceId: space.id,
22    metadata: { category: 'personal' }
23  });
24
25  // Add some memories
26  const workMemory = await trix.memories.create({
27    content: 'Meeting notes from project kickoff',
28    spaceId: space.id,
29    tags: ['meeting', 'project']
30  });
31
32  await trix.clusters.addMemory(workCluster.id, workMemory.id);
33
34  console.log(`Workspace setup complete for user ${userId}`);
35  console.log(`Space: ${space.id}`);
36  console.log(`Clusters: ${workCluster.id}, ${personalCluster.id}`);
37
38  return { space, clusters: [workCluster, personalCluster] };
39}
40
41// Query within a user's workspace
42async function searchUserMemories(
43  userId: string,
44  query: string
45) {
46  const trix = Trix.fromEnv();
47
48  // Find user's space
49  const { data: spaces } = await trix.spaces.list();
50  const userSpace = spaces.find(
51    s => s.metadata?.userId === userId
52  );
53
54  if (!userSpace) {
55    throw new Error('User workspace not found');
56  }
57
58  // Search within the space
59  const results = await trix.memories.list({
60    q: query,
61    spaceId: userSpace.id,
62    mode: 'hybrid',
63    limit: 10
64  });
65
66  return results.data;
67}