Files
wiki/convex/embeddingsQueries.ts
Wayne Sutton 5a8df46681 feat: Add semantic search with vector embeddings
Add vector-based semantic search to complement keyword search.
  Users can toggle between "Keyword" and "Semantic" modes in the
  search modal (Cmd+K, then Tab to switch).

  Semantic search:
  - Uses OpenAI text-embedding-ada-002 (1536 dimensions)
  - Finds content by meaning, not exact words
  - Shows similarity scores as percentages
  - ~300ms latency, ~$0.0001/query
  - Graceful fallback if OPENAI_API_KEY not set

  New files:
  - convex/embeddings.ts - Embedding generation actions
  - convex/embeddingsQueries.ts - Queries/mutations for embeddings
  - convex/semanticSearch.ts - Vector search action
  - convex/semanticSearchQueries.ts - Result hydration queries
  - content/pages/docs-search.md - Keyword search docs
  - content/pages/docs-semantic-search.md - Semantic search docs

  Changes:
  - convex/schema.ts: Add embedding field and by_embedding vectorIndex
  - SearchModal.tsx: Add mode toggle (TextAa/Brain icons)
  - sync-posts.ts: Generate embeddings after content sync
  - global.css: Search mode toggle styles

  Documentation updated:
  - changelog.md, TASK.md, files.md, about.md, home.md

  Configuration:
  npx convex env set OPENAI_API_KEY sk-your-key

  Generated with [Claude Code](https://claude.com/claude-code)

  Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>

  Status: Ready to commit. All semantic search files are staged. The TypeScript warnings are pre-existing (unused variables) and don't affect the build.
2026-01-05 18:30:48 -08:00

106 lines
2.5 KiB
TypeScript

import { v } from "convex/values";
import { internalMutation, internalQuery } from "./_generated/server";
// Internal query to get posts without embeddings
export const getPostsWithoutEmbeddings = internalQuery({
args: { limit: v.number() },
returns: v.array(
v.object({
_id: v.id("posts"),
title: v.string(),
content: v.string(),
})
),
handler: async (ctx, args) => {
const posts = await ctx.db
.query("posts")
.withIndex("by_published", (q) => q.eq("published", true))
.collect();
return posts
.filter((post) => !post.embedding)
.slice(0, args.limit)
.map((post) => ({
_id: post._id,
title: post.title,
content: post.content,
}));
},
});
// Internal query to get pages without embeddings
export const getPagesWithoutEmbeddings = internalQuery({
args: { limit: v.number() },
returns: v.array(
v.object({
_id: v.id("pages"),
title: v.string(),
content: v.string(),
})
),
handler: async (ctx, args) => {
const pages = await ctx.db
.query("pages")
.withIndex("by_published", (q) => q.eq("published", true))
.collect();
return pages
.filter((page) => !page.embedding)
.slice(0, args.limit)
.map((page) => ({
_id: page._id,
title: page.title,
content: page.content,
}));
},
});
// Internal mutation to save embedding for a post
export const savePostEmbedding = internalMutation({
args: {
id: v.id("posts"),
embedding: v.array(v.float64()),
},
handler: async (ctx, args) => {
await ctx.db.patch(args.id, { embedding: args.embedding });
},
});
// Internal mutation to save embedding for a page
export const savePageEmbedding = internalMutation({
args: {
id: v.id("pages"),
embedding: v.array(v.float64()),
},
handler: async (ctx, args) => {
await ctx.db.patch(args.id, { embedding: args.embedding });
},
});
// Internal query to get post by slug
export const getPostBySlug = internalQuery({
args: { slug: v.string() },
returns: v.union(
v.object({
_id: v.id("posts"),
title: v.string(),
content: v.string(),
}),
v.null()
),
handler: async (ctx, args) => {
const post = await ctx.db
.query("posts")
.withIndex("by_slug", (q) => q.eq("slug", args.slug))
.first();
if (!post) return null;
return {
_id: post._id,
title: post.title,
content: post.content,
};
},
});