Files
amcs/internal/tools/summarize.go
Hein 66370a7f0e feat(tools): implement CRUD operations for thoughts and projects
* Add tools for creating, retrieving, updating, and deleting thoughts.
* Implement project management tools for creating and listing projects.
* Introduce linking functionality between thoughts.
* Add search and recall capabilities for thoughts based on semantic queries.
* Implement statistics and summarization tools for thought analysis.
* Create database migrations for thoughts, projects, and links.
* Add helper functions for UUID parsing and project resolution.
2026-03-24 15:38:59 +02:00

94 lines
3.0 KiB
Go

package tools
import (
"context"
"strings"
"github.com/google/uuid"
"github.com/modelcontextprotocol/go-sdk/mcp"
"git.warky.dev/wdevs/amcs/internal/ai"
"git.warky.dev/wdevs/amcs/internal/config"
"git.warky.dev/wdevs/amcs/internal/session"
"git.warky.dev/wdevs/amcs/internal/store"
)
type SummarizeTool struct {
store *store.DB
provider ai.Provider
search config.SearchConfig
sessions *session.ActiveProjects
}
type SummarizeInput struct {
Query string `json:"query,omitempty" jsonschema:"optional semantic focus for the summary"`
Project string `json:"project,omitempty" jsonschema:"optional project name or id; falls back to the active session project"`
Days int `json:"days,omitempty" jsonschema:"only include thoughts from the last N days when query is omitted"`
Limit int `json:"limit,omitempty" jsonschema:"maximum number of thoughts to summarize"`
}
type SummarizeOutput struct {
Summary string `json:"summary"`
Count int `json:"count"`
}
func NewSummarizeTool(db *store.DB, provider ai.Provider, search config.SearchConfig, sessions *session.ActiveProjects) *SummarizeTool {
return &SummarizeTool{store: db, provider: provider, search: search, sessions: sessions}
}
func (t *SummarizeTool) Handle(ctx context.Context, req *mcp.CallToolRequest, in SummarizeInput) (*mcp.CallToolResult, SummarizeOutput, error) {
project, err := resolveProject(ctx, t.store, t.sessions, req, in.Project, false)
if err != nil {
return nil, SummarizeOutput{}, err
}
limit := normalizeLimit(in.Limit, t.search)
query := strings.TrimSpace(in.Query)
lines := make([]string, 0, limit)
count := 0
if query != "" {
embedding, err := t.provider.Embed(ctx, query)
if err != nil {
return nil, SummarizeOutput{}, err
}
var projectID *uuid.UUID
if project != nil {
projectID = &project.ID
}
results, err := t.store.SearchSimilarThoughts(ctx, embedding, t.search.DefaultThreshold, limit, projectID, nil)
if err != nil {
return nil, SummarizeOutput{}, err
}
for i, result := range results {
lines = append(lines, thoughtContextLine(i, result.Content, result.Metadata, result.Similarity))
}
count = len(results)
} else {
var projectID *uuid.UUID
if project != nil {
projectID = &project.ID
}
thoughts, err := t.store.RecentThoughts(ctx, projectID, limit, in.Days)
if err != nil {
return nil, SummarizeOutput{}, err
}
for i, thought := range thoughts {
lines = append(lines, thoughtContextLine(i, thought.Content, thought.Metadata, 0))
}
count = len(thoughts)
}
userPrompt := formatContextBlock("Summarize the following thoughts into concise prose with themes, action items, and notable people.", lines)
systemPrompt := "You summarize note collections. Be concise, concrete, and structured in plain prose."
summary, err := t.provider.Summarize(ctx, systemPrompt, userPrompt)
if err != nil {
return nil, SummarizeOutput{}, err
}
if project != nil {
_ = t.store.TouchProject(ctx, project.ID)
}
return nil, SummarizeOutput{Summary: summary, Count: count}, nil
}