Primitives.org.ai

artifacts

Cached embeddings and computed content

Artifacts store computed content with automatic cache invalidation.

Import

import { DB } from 'ai-database'

const { db, artifacts } = DB({
  Post: { title: 'string', content: 'markdown' },
})

What Are Artifacts?

Artifacts are cached outputs associated with entities:

UseExample
EmbeddingsVector representations for search
Rendered HTMLMarkdown to HTML conversion
BundlesCompiled JavaScript/CSS
ASTParsed code/content

Automatic Embeddings

When you create entities, embeddings are auto-generated:

const post = await db.Post.create({
  title: 'Hello',
  content: 'This is my post...',
})

// Embedding artifact auto-created
const embedding = await artifacts.get(post.$id, 'embedding')
// { vectors: Float32Array, model: '...', dimensions: 3072 }

Operations

get

Get an artifact:

const embedding = await artifacts.get(entityId, 'embedding')
const html = await artifacts.get(entityId, 'html')

set

Store an artifact:

await artifacts.set(entityId, 'html', {
  content: renderedHtml,
  sourceHash: hash(content),
})

delete

Delete artifacts:

await artifacts.delete(entityId, 'html')  // specific type
await artifacts.delete(entityId)          // all for entity

list

List artifacts for an entity:

const all = await artifacts.list(entityId)
// ['embedding', 'html', 'ast']

Cache Invalidation

When source content changes, artifacts invalidate:

await db.Post.update(post.$id, {
  content: 'New content...',
})

// Embedding is regenerated automatically
// Custom artifacts return null until rebuilt

Artifact Types

TypeDescription
embeddingVector embeddings (auto-generated)
chunksChunked content for search
astParsed abstract syntax tree
htmlRendered HTML
bundleBundled JavaScript/CSS
CustomAny string type you define

Custom Artifacts

// Compute and store
const html = await renderMarkdown(post.content)
await artifacts.set(post.$id, 'html', {
  content: html,
  sourceHash: hash(post.content),
})

// Retrieve later
const cached = await artifacts.get(post.$id, 'html')
if (cached && cached.sourceHash === hash(post.content)) {
  return cached.content  // Cache hit
}
Was this page helpful?

On this page