Merge pull request #24 from KristianJBorgwarth/feat/full-link-completion

feat(link_completion): full link complete
This commit is contained in:
Kristian 2026-04-14 05:44:26 +02:00 committed by GitHub
commit 6e723fce64
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 32 additions and 19 deletions

View file

@ -10,6 +10,7 @@ import (
"github.com/KristianJBorgwarth/dendrite.daemon/core/logging" "github.com/KristianJBorgwarth/dendrite.daemon/core/logging"
"github.com/KristianJBorgwarth/dendrite.daemon/core/server" "github.com/KristianJBorgwarth/dendrite.daemon/core/server"
"github.com/KristianJBorgwarth/dendrite.daemon/core/services" "github.com/KristianJBorgwarth/dendrite.daemon/core/services"
"github.com/KristianJBorgwarth/dendrite.daemon/persistence"
"github.com/KristianJBorgwarth/dendrite.daemon/persistence/repositories" "github.com/KristianJBorgwarth/dendrite.daemon/persistence/repositories"
_ "modernc.org/sqlite" _ "modernc.org/sqlite"
) )
@ -19,7 +20,7 @@ func main() {
server := server.NewServer() server := server.NewServer()
uow := repositories.NewUnitOfWork(); uow := repositories.NewUnitOfWork();
linkRepo := repositories.NewLinkRepository() linkRepo := repositories.NewLinkRepository(*persistence.NewReadContext())
tagRepo := repositories.NewTagRepository() tagRepo := repositories.NewTagRepository()
noteRepo := repositories.NewNoteRepository() noteRepo := repositories.NewNoteRepository()
tagService := services.NewTagService(tagRepo) tagService := services.NewTagService(tagRepo)
@ -29,7 +30,7 @@ func main() {
server.RegisterHandler("vault/init", vault.NewInitializeHandler()) server.RegisterHandler("vault/init", vault.NewInitializeHandler())
server.RegisterHandler("note/create", note.NewCreateNoteHandler(uow, tagService, noteRepo)) server.RegisterHandler("note/create", note.NewCreateNoteHandler(uow, tagService, noteRepo))
server.RegisterHandler("note/save", note.NewSaveNoteHandler(uow, noteRepo, tagService, noteService, linkService)) server.RegisterHandler("note/save", note.NewSaveNoteHandler(uow, noteRepo, tagService, noteService, linkService))
server.RegisterHandler("completion/link", completion.NewCompleteLinkHandler()) server.RegisterHandler("completion/link", completion.NewCompleteLinkHandler(linkRepo))
if err := server.Run(os.Stdin, os.Stdout); err != nil { if err := server.Run(os.Stdin, os.Stdout); err != nil {
slog.Error("server error", "error", err) slog.Error("server error", "error", err)

View file

@ -4,20 +4,25 @@ import (
"context" "context"
"encoding/json" "encoding/json"
"log/slog" "log/slog"
"github.com/KristianJBorgwarth/dendrite.daemon/persistence/repositories"
) )
type completeLinkCommand struct { type completeLinkCommand struct {
Query string `json:"query"` Query string `json:"query"`
} }
type completionItem struct { type completeLinkResult struct {
Slug string `json:"slug"` Slug string `json:"slug"`
Display string `json:"display"`
} }
type CompleteLinkHandler struct{} type CompleteLinkHandler struct {
linkRepo repositories.ILinkRepository
}
func NewCompleteLinkHandler() *CompleteLinkHandler { func NewCompleteLinkHandler(lr repositories.ILinkRepository) *CompleteLinkHandler {
return &CompleteLinkHandler{} return &CompleteLinkHandler{linkRepo: lr}
} }
func (h *CompleteLinkHandler) Handle(ctx context.Context, raw json.RawMessage) (any, error) { func (h *CompleteLinkHandler) Handle(ctx context.Context, raw json.RawMessage) (any, error) {
@ -25,11 +30,16 @@ func (h *CompleteLinkHandler) Handle(ctx context.Context, raw json.RawMessage) (
if err := json.Unmarshal(raw, &cmd); err != nil { if err := json.Unmarshal(raw, &cmd); err != nil {
return nil, err return nil, err
} }
slog.Debug("handling complete link command", "query", cmd.Query) links, err := h.linkRepo.Search(ctx, cmd.Query)
if err != nil {
slog.Error("Failed to search links", "error", err)
return nil, err
}
return []completionItem{ items := make([]completeLinkResult, len(links))
{Slug: "standard-streams"}, for i, link := range links {
{Slug: "unit-of-work"}, items[i] = completeLinkResult{Slug: link.TargetSlug(), Display: link.Display()}
{Slug: "treesitter-basics"}, }
}, nil
return items, nil
} }

View file

@ -12,14 +12,16 @@ type ILinkRepository interface {
Insert(ctx context.Context, dbContext persistence.IDbContext, links []*models.Link) error Insert(ctx context.Context, dbContext persistence.IDbContext, links []*models.Link) error
GetByNoteID(ctx context.Context, dbContext persistence.IDbContext, fromNoteID string) ([]*models.Link, error) GetByNoteID(ctx context.Context, dbContext persistence.IDbContext, fromNoteID string) ([]*models.Link, error)
GetBySlug(ctx context.Context, dbContext persistence.IDbContext, targetSlug string) ([]*models.Link, error) GetBySlug(ctx context.Context, dbContext persistence.IDbContext, targetSlug string) ([]*models.Link, error)
Search(ctx context.Context, dbContext persistence.IDbContext, query string) ([]*models.Link, error) Search(ctx context.Context, query string) ([]*models.Link, error)
Delete(ctx context.Context, dbContext persistence.IDbContext, fromNoteID string) error Delete(ctx context.Context, dbContext persistence.IDbContext, fromNoteID string) error
} }
type linkRepository struct{} type linkRepository struct {
readDBContext persistence.ReadContext
}
func NewLinkRepository() ILinkRepository { func NewLinkRepository(rdb persistence.ReadContext) ILinkRepository {
return &linkRepository{} return &linkRepository{readDBContext: rdb}
} }
func (r *linkRepository) Insert(ctx context.Context, dbContext persistence.IDbContext, links []*models.Link) error { func (r *linkRepository) Insert(ctx context.Context, dbContext persistence.IDbContext, links []*models.Link) error {
@ -81,8 +83,8 @@ func (r *linkRepository) GetBySlug(ctx context.Context, dbContext persistence.ID
return links, nil return links, nil
} }
func (r *linkRepository) Search(ctx context.Context, dbContext persistence.IDbContext, query string) ([]*models.Link, error) { func (r *linkRepository) Search(ctx context.Context, query string) ([]*models.Link, error) {
rows, err := dbContext.QueryContext(ctx, `SELECT id, from_note_id, target_slug, raw, display, line, col FROM link WHERE raw LIKE ?`, "%"+query+"%") rows, err := r.readDBContext.QueryContext(ctx, `SELECT id, from_note_id, target_slug, raw, display, line, col FROM link WHERE raw LIKE ?`, "%"+query+"%")
if err != nil { if err != nil {
return nil, err return nil, err
} }