-
Notifications
You must be signed in to change notification settings - Fork 7
feat: PGVector-backed log indexing and semantic search (#24) #80
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
khat190
wants to merge
2
commits into
deekshithgowda85:prod
Choose a base branch
from
khat190:feat/pgvector-log-search
base: prod
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+635
−5
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,160 @@ | ||
| /** | ||
| * app/api/logs/search/route.ts | ||
| * | ||
| * GET /api/logs/search?q=<query>[&sandboxId=<id>][&limit=<n>] | ||
| * Returns semantically similar log chunks for the authenticated user. | ||
| * | ||
| * POST /api/logs/search | ||
| * Body: { q: string; sandboxId?: string; limit?: number } | ||
| * Same as GET but accepts a JSON body (useful for longer queries). | ||
| * | ||
| * POST /api/logs/search?action=trigger | ||
| * Body: { sandboxId: string; ttlDays?: number } | ||
| * Manually trigger indexing for a sandbox (fires the Inngest event). | ||
| */ | ||
|
|
||
| import { NextRequest, NextResponse } from "next/server"; | ||
| import { auth } from "@/lib/auth"; | ||
| import { searchLogVectors } from "@/lib/db"; | ||
| import { inngest } from "@/lib/inngest"; | ||
| import { embedQuery } from "@/lib/vector-indexer"; | ||
|
|
||
| // ── Helpers ─────────────────────────────────────────────────────────────────── | ||
|
|
||
| function sanitiseLimit(raw: string | null | undefined): number { | ||
| const n = parseInt(raw ?? "10", 10); | ||
| if (isNaN(n) || n < 1) return 10; | ||
| if (n > 50) return 50; | ||
| return n; | ||
| } | ||
|
|
||
| // ── Shared search handler ───────────────────────────────────────────────────── | ||
|
|
||
| async function handleSearch(opts: { | ||
| userId: string; | ||
| query: string; | ||
| sandboxId?: string; | ||
| limit: number; | ||
| }) { | ||
| const { userId, query, sandboxId, limit } = opts; | ||
|
|
||
| if (!query || query.trim().length === 0) { | ||
| return NextResponse.json( | ||
| { error: "Query parameter 'q' is required" }, | ||
| { status: 400 } | ||
| ); | ||
| } | ||
|
|
||
| // Embed the user's natural-language query using Cohere | ||
| const queryEmbedding = await embedQuery(query.trim()); | ||
|
|
||
| // Run the cosine-similarity search | ||
| const results = await searchLogVectors({ | ||
| userId, | ||
| queryEmbedding, | ||
| sandboxId, | ||
| limit, | ||
| }); | ||
|
|
||
| return NextResponse.json({ | ||
| query, | ||
| sandboxId: sandboxId ?? null, | ||
| count: results.length, | ||
| results: results.map((r) => ({ | ||
| id: r.id, | ||
| sandboxId: r.sandboxId, | ||
| logIdRange: { start: r.logIdStart, end: r.logIdEnd }, | ||
| chunkText: r.chunkText, | ||
| createdAt: r.createdAt, | ||
| })), | ||
| }); | ||
| } | ||
|
|
||
| // ── GET /api/logs/search ────────────────────────────────────────────────────── | ||
|
|
||
| export async function GET(req: NextRequest) { | ||
| const session = await auth(); | ||
| if (!session?.user?.id) { | ||
| return NextResponse.json({ error: "Unauthorized" }, { status: 401 }); | ||
| } | ||
|
|
||
| const { searchParams } = new URL(req.url); | ||
| const query = searchParams.get("q") ?? ""; | ||
| const sandboxId = searchParams.get("sandboxId") ?? undefined; | ||
| const limit = sanitiseLimit(searchParams.get("limit")); | ||
|
|
||
| try { | ||
| return await handleSearch({ userId: session.user.id, query, sandboxId, limit }); | ||
| } catch (err) { | ||
| console.error("[logs/search] GET error:", err); | ||
| return NextResponse.json( | ||
| { error: "Search failed", detail: String(err) }, | ||
| { status: 500 } | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| // ── POST /api/logs/search ───────────────────────────────────────────────────── | ||
|
|
||
| export async function POST(req: NextRequest) { | ||
| const session = await auth(); | ||
| if (!session?.user?.id) { | ||
| return NextResponse.json({ error: "Unauthorized" }, { status: 401 }); | ||
| } | ||
|
|
||
| // Handle manual index trigger via ?action=trigger | ||
| const { searchParams } = new URL(req.url); | ||
| if (searchParams.get("action") === "trigger") { | ||
| return handleTrigger(req, session.user.id); | ||
| } | ||
|
|
||
| let body: { q?: string; sandboxId?: string; limit?: number }; | ||
| try { | ||
| body = await req.json(); | ||
| } catch { | ||
| return NextResponse.json({ error: "Invalid JSON body" }, { status: 400 }); | ||
| } | ||
|
|
||
| const query = body.q ?? ""; | ||
| const sandboxId = body.sandboxId ?? undefined; | ||
| const limit = sanitiseLimit(String(body.limit ?? 10)); | ||
|
|
||
| try { | ||
| return await handleSearch({ userId: session.user.id, query, sandboxId, limit }); | ||
| } catch (err) { | ||
| console.error("[logs/search] POST error:", err); | ||
| return NextResponse.json( | ||
| { error: "Search failed", detail: String(err) }, | ||
| { status: 500 } | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| // ── Manual index trigger ────────────────────────────────────────────────────── | ||
|
|
||
| async function handleTrigger(req: NextRequest, userId: string) { | ||
| let body: { sandboxId?: string; ttlDays?: number }; | ||
| try { | ||
| body = await req.json(); | ||
| } catch { | ||
| return NextResponse.json({ error: "Invalid JSON body" }, { status: 400 }); | ||
| } | ||
|
|
||
| const { sandboxId, ttlDays = 30 } = body; | ||
| if (!sandboxId) { | ||
| return NextResponse.json( | ||
| { error: "sandboxId is required" }, | ||
| { status: 400 } | ||
| ); | ||
| } | ||
|
|
||
| await inngest.send({ | ||
| name: "log/index.requested", | ||
| data: { sandboxId, userId, ttlDays }, | ||
| }); | ||
|
|
||
| return NextResponse.json({ | ||
| ok: true, | ||
| message: `Indexing triggered for sandbox ${sandboxId}`, | ||
| }); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.