feat: persist thread documents

This commit is contained in:
Vincent Koc 2026-04-26 23:29:39 -07:00
parent df97b7c591
commit fc40530eeb
No known key found for this signature in database
2 changed files with 81 additions and 0 deletions

View File

@ -0,0 +1,35 @@
package store
import (
"context"
"fmt"
)
type Document struct {
ID int64 `json:"id"`
ThreadID int64 `json:"thread_id"`
Title string `json:"title"`
Body string `json:"body,omitempty"`
RawText string `json:"raw_text"`
DedupeText string `json:"dedupe_text"`
UpdatedAt string `json:"updated_at"`
}
func (s *Store) UpsertDocument(ctx context.Context, doc Document) (int64, error) {
var id int64
err := s.db.QueryRowContext(ctx, `
insert into documents(thread_id, title, body, raw_text, dedupe_text, updated_at)
values(?, ?, ?, ?, ?, ?)
on conflict(thread_id) do update set
title=excluded.title,
body=excluded.body,
raw_text=excluded.raw_text,
dedupe_text=excluded.dedupe_text,
updated_at=excluded.updated_at
returning id
`, doc.ThreadID, doc.Title, nullString(doc.Body), doc.RawText, doc.DedupeText, doc.UpdatedAt).Scan(&id)
if err != nil {
return 0, fmt.Errorf("upsert document: %w", err)
}
return id, nil
}

View File

@ -0,0 +1,46 @@
package store
import (
"context"
"path/filepath"
"testing"
)
func TestUpsertDocumentIndexesFTS(t *testing.T) {
ctx := context.Background()
st, err := Open(ctx, filepath.Join(t.TempDir(), "gitcrawl.db"))
if err != nil {
t.Fatalf("open store: %v", err)
}
defer st.Close()
repoID, err := st.UpsertRepository(ctx, Repository{Owner: "openclaw", Name: "gitcrawl", FullName: "openclaw/gitcrawl", RawJSON: "{}", UpdatedAt: "2026-04-26T00:00:00Z"})
if err != nil {
t.Fatalf("repo: %v", err)
}
threadID, err := st.UpsertThread(ctx, Thread{
RepoID: repoID, GitHubID: "1", Number: 1, Kind: "issue", State: "open",
Title: "download stalls", HTMLURL: "https://github.com/openclaw/gitcrawl/issues/1",
LabelsJSON: "[]", AssigneesJSON: "[]", RawJSON: "{}", ContentHash: "hash", UpdatedAt: "2026-04-26T00:00:00Z",
})
if err != nil {
t.Fatalf("thread: %v", err)
}
if _, err := st.UpsertDocument(ctx, Document{
ThreadID: threadID,
Title: "download stalls",
RawText: "download stalls on large artifacts",
DedupeText: "download stalls large artifacts",
UpdatedAt: "2026-04-26T00:00:00Z",
}); err != nil {
t.Fatalf("document: %v", err)
}
var count int
if err := st.DB().QueryRowContext(ctx, `select count(*) from documents_fts where documents_fts match 'artifacts'`).Scan(&count); err != nil {
t.Fatalf("fts query: %v", err)
}
if count != 1 {
t.Fatalf("fts count: got %d want 1", count)
}
}