feat(tools): add maintenance and meal planning tools with CRUD operations
- Implement maintenance tool for adding, logging, and retrieving tasks - Create meals tool for managing recipes, meal plans, and shopping lists - Introduce reparse metadata tool for updating thought metadata - Add household knowledge, home maintenance, family calendar, meal planning, and professional CRM database migrations - Grant necessary permissions for new database tables
This commit is contained in:
212
internal/tools/calendar.go
Normal file
212
internal/tools/calendar.go
Normal file
@@ -0,0 +1,212 @@
|
||||
package tools
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/modelcontextprotocol/go-sdk/mcp"
|
||||
|
||||
"git.warky.dev/wdevs/amcs/internal/store"
|
||||
ext "git.warky.dev/wdevs/amcs/internal/types"
|
||||
)
|
||||
|
||||
type CalendarTool struct {
|
||||
store *store.DB
|
||||
}
|
||||
|
||||
func NewCalendarTool(db *store.DB) *CalendarTool {
|
||||
return &CalendarTool{store: db}
|
||||
}
|
||||
|
||||
// add_family_member
|
||||
|
||||
type AddFamilyMemberInput struct {
|
||||
Name string `json:"name" jsonschema:"person's name"`
|
||||
Relationship string `json:"relationship,omitempty" jsonschema:"e.g. self, spouse, child, parent"`
|
||||
BirthDate *time.Time `json:"birth_date,omitempty"`
|
||||
Notes string `json:"notes,omitempty"`
|
||||
}
|
||||
|
||||
type AddFamilyMemberOutput struct {
|
||||
Member ext.FamilyMember `json:"member"`
|
||||
}
|
||||
|
||||
func (t *CalendarTool) AddMember(ctx context.Context, _ *mcp.CallToolRequest, in AddFamilyMemberInput) (*mcp.CallToolResult, AddFamilyMemberOutput, error) {
|
||||
if strings.TrimSpace(in.Name) == "" {
|
||||
return nil, AddFamilyMemberOutput{}, errInvalidInput("name is required")
|
||||
}
|
||||
member, err := t.store.AddFamilyMember(ctx, ext.FamilyMember{
|
||||
Name: strings.TrimSpace(in.Name),
|
||||
Relationship: strings.TrimSpace(in.Relationship),
|
||||
BirthDate: in.BirthDate,
|
||||
Notes: strings.TrimSpace(in.Notes),
|
||||
})
|
||||
if err != nil {
|
||||
return nil, AddFamilyMemberOutput{}, err
|
||||
}
|
||||
return nil, AddFamilyMemberOutput{Member: member}, nil
|
||||
}
|
||||
|
||||
// list_family_members
|
||||
|
||||
type ListFamilyMembersInput struct{}
|
||||
|
||||
type ListFamilyMembersOutput struct {
|
||||
Members []ext.FamilyMember `json:"members"`
|
||||
}
|
||||
|
||||
func (t *CalendarTool) ListMembers(ctx context.Context, _ *mcp.CallToolRequest, _ ListFamilyMembersInput) (*mcp.CallToolResult, ListFamilyMembersOutput, error) {
|
||||
members, err := t.store.ListFamilyMembers(ctx)
|
||||
if err != nil {
|
||||
return nil, ListFamilyMembersOutput{}, err
|
||||
}
|
||||
if members == nil {
|
||||
members = []ext.FamilyMember{}
|
||||
}
|
||||
return nil, ListFamilyMembersOutput{Members: members}, nil
|
||||
}
|
||||
|
||||
// add_activity
|
||||
|
||||
type AddActivityInput struct {
|
||||
Title string `json:"title" jsonschema:"activity title"`
|
||||
ActivityType string `json:"activity_type,omitempty" jsonschema:"e.g. sports, medical, school, social"`
|
||||
FamilyMemberID *uuid.UUID `json:"family_member_id,omitempty" jsonschema:"leave empty for whole-family activities"`
|
||||
DayOfWeek string `json:"day_of_week,omitempty" jsonschema:"for recurring: monday, tuesday, etc."`
|
||||
StartTime string `json:"start_time,omitempty" jsonschema:"HH:MM format"`
|
||||
EndTime string `json:"end_time,omitempty" jsonschema:"HH:MM format"`
|
||||
StartDate *time.Time `json:"start_date,omitempty"`
|
||||
EndDate *time.Time `json:"end_date,omitempty" jsonschema:"for recurring activities, when they end"`
|
||||
Location string `json:"location,omitempty"`
|
||||
Notes string `json:"notes,omitempty"`
|
||||
}
|
||||
|
||||
type AddActivityOutput struct {
|
||||
Activity ext.Activity `json:"activity"`
|
||||
}
|
||||
|
||||
func (t *CalendarTool) AddActivity(ctx context.Context, _ *mcp.CallToolRequest, in AddActivityInput) (*mcp.CallToolResult, AddActivityOutput, error) {
|
||||
if strings.TrimSpace(in.Title) == "" {
|
||||
return nil, AddActivityOutput{}, errInvalidInput("title is required")
|
||||
}
|
||||
activity, err := t.store.AddActivity(ctx, ext.Activity{
|
||||
FamilyMemberID: in.FamilyMemberID,
|
||||
Title: strings.TrimSpace(in.Title),
|
||||
ActivityType: strings.TrimSpace(in.ActivityType),
|
||||
DayOfWeek: strings.ToLower(strings.TrimSpace(in.DayOfWeek)),
|
||||
StartTime: strings.TrimSpace(in.StartTime),
|
||||
EndTime: strings.TrimSpace(in.EndTime),
|
||||
StartDate: in.StartDate,
|
||||
EndDate: in.EndDate,
|
||||
Location: strings.TrimSpace(in.Location),
|
||||
Notes: strings.TrimSpace(in.Notes),
|
||||
})
|
||||
if err != nil {
|
||||
return nil, AddActivityOutput{}, err
|
||||
}
|
||||
return nil, AddActivityOutput{Activity: activity}, nil
|
||||
}
|
||||
|
||||
// get_week_schedule
|
||||
|
||||
type GetWeekScheduleInput struct {
|
||||
WeekStart time.Time `json:"week_start" jsonschema:"start of the week (Monday) to retrieve"`
|
||||
}
|
||||
|
||||
type GetWeekScheduleOutput struct {
|
||||
Activities []ext.Activity `json:"activities"`
|
||||
}
|
||||
|
||||
func (t *CalendarTool) GetWeekSchedule(ctx context.Context, _ *mcp.CallToolRequest, in GetWeekScheduleInput) (*mcp.CallToolResult, GetWeekScheduleOutput, error) {
|
||||
activities, err := t.store.GetWeekSchedule(ctx, in.WeekStart)
|
||||
if err != nil {
|
||||
return nil, GetWeekScheduleOutput{}, err
|
||||
}
|
||||
if activities == nil {
|
||||
activities = []ext.Activity{}
|
||||
}
|
||||
return nil, GetWeekScheduleOutput{Activities: activities}, nil
|
||||
}
|
||||
|
||||
// search_activities
|
||||
|
||||
type SearchActivitiesInput struct {
|
||||
Query string `json:"query,omitempty" jsonschema:"search text matching title or notes"`
|
||||
ActivityType string `json:"activity_type,omitempty" jsonschema:"filter by type"`
|
||||
FamilyMemberID *uuid.UUID `json:"family_member_id,omitempty" jsonschema:"filter by family member"`
|
||||
}
|
||||
|
||||
type SearchActivitiesOutput struct {
|
||||
Activities []ext.Activity `json:"activities"`
|
||||
}
|
||||
|
||||
func (t *CalendarTool) SearchActivities(ctx context.Context, _ *mcp.CallToolRequest, in SearchActivitiesInput) (*mcp.CallToolResult, SearchActivitiesOutput, error) {
|
||||
activities, err := t.store.SearchActivities(ctx, in.Query, in.ActivityType, in.FamilyMemberID)
|
||||
if err != nil {
|
||||
return nil, SearchActivitiesOutput{}, err
|
||||
}
|
||||
if activities == nil {
|
||||
activities = []ext.Activity{}
|
||||
}
|
||||
return nil, SearchActivitiesOutput{Activities: activities}, nil
|
||||
}
|
||||
|
||||
// add_important_date
|
||||
|
||||
type AddImportantDateInput struct {
|
||||
Title string `json:"title" jsonschema:"description of the date"`
|
||||
DateValue time.Time `json:"date_value" jsonschema:"the date"`
|
||||
FamilyMemberID *uuid.UUID `json:"family_member_id,omitempty"`
|
||||
RecurringYearly bool `json:"recurring_yearly,omitempty" jsonschema:"if true, reminds every year"`
|
||||
ReminderDaysBefore int `json:"reminder_days_before,omitempty" jsonschema:"how many days before to remind (default: 7)"`
|
||||
Notes string `json:"notes,omitempty"`
|
||||
}
|
||||
|
||||
type AddImportantDateOutput struct {
|
||||
Date ext.ImportantDate `json:"date"`
|
||||
}
|
||||
|
||||
func (t *CalendarTool) AddImportantDate(ctx context.Context, _ *mcp.CallToolRequest, in AddImportantDateInput) (*mcp.CallToolResult, AddImportantDateOutput, error) {
|
||||
if strings.TrimSpace(in.Title) == "" {
|
||||
return nil, AddImportantDateOutput{}, errInvalidInput("title is required")
|
||||
}
|
||||
reminder := in.ReminderDaysBefore
|
||||
if reminder <= 0 {
|
||||
reminder = 7
|
||||
}
|
||||
d, err := t.store.AddImportantDate(ctx, ext.ImportantDate{
|
||||
FamilyMemberID: in.FamilyMemberID,
|
||||
Title: strings.TrimSpace(in.Title),
|
||||
DateValue: in.DateValue,
|
||||
RecurringYearly: in.RecurringYearly,
|
||||
ReminderDaysBefore: reminder,
|
||||
Notes: strings.TrimSpace(in.Notes),
|
||||
})
|
||||
if err != nil {
|
||||
return nil, AddImportantDateOutput{}, err
|
||||
}
|
||||
return nil, AddImportantDateOutput{Date: d}, nil
|
||||
}
|
||||
|
||||
// get_upcoming_dates
|
||||
|
||||
type GetUpcomingDatesInput struct {
|
||||
DaysAhead int `json:"days_ahead,omitempty" jsonschema:"how many days to look ahead (default: 30)"`
|
||||
}
|
||||
|
||||
type GetUpcomingDatesOutput struct {
|
||||
Dates []ext.ImportantDate `json:"dates"`
|
||||
}
|
||||
|
||||
func (t *CalendarTool) GetUpcomingDates(ctx context.Context, _ *mcp.CallToolRequest, in GetUpcomingDatesInput) (*mcp.CallToolResult, GetUpcomingDatesOutput, error) {
|
||||
dates, err := t.store.GetUpcomingDates(ctx, in.DaysAhead)
|
||||
if err != nil {
|
||||
return nil, GetUpcomingDatesOutput{}, err
|
||||
}
|
||||
if dates == nil {
|
||||
dates = []ext.ImportantDate{}
|
||||
}
|
||||
return nil, GetUpcomingDatesOutput{Dates: dates}, nil
|
||||
}
|
||||
232
internal/tools/crm.go
Normal file
232
internal/tools/crm.go
Normal file
@@ -0,0 +1,232 @@
|
||||
package tools
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/modelcontextprotocol/go-sdk/mcp"
|
||||
|
||||
"git.warky.dev/wdevs/amcs/internal/store"
|
||||
ext "git.warky.dev/wdevs/amcs/internal/types"
|
||||
)
|
||||
|
||||
type CRMTool struct {
|
||||
store *store.DB
|
||||
}
|
||||
|
||||
func NewCRMTool(db *store.DB) *CRMTool {
|
||||
return &CRMTool{store: db}
|
||||
}
|
||||
|
||||
// add_professional_contact
|
||||
|
||||
type AddContactInput struct {
|
||||
Name string `json:"name" jsonschema:"contact's full name"`
|
||||
Company string `json:"company,omitempty"`
|
||||
Title string `json:"title,omitempty" jsonschema:"job title"`
|
||||
Email string `json:"email,omitempty"`
|
||||
Phone string `json:"phone,omitempty"`
|
||||
LinkedInURL string `json:"linkedin_url,omitempty"`
|
||||
HowWeMet string `json:"how_we_met,omitempty"`
|
||||
Tags []string `json:"tags,omitempty"`
|
||||
Notes string `json:"notes,omitempty"`
|
||||
FollowUpDate *time.Time `json:"follow_up_date,omitempty"`
|
||||
}
|
||||
|
||||
type AddContactOutput struct {
|
||||
Contact ext.ProfessionalContact `json:"contact"`
|
||||
}
|
||||
|
||||
func (t *CRMTool) AddContact(ctx context.Context, _ *mcp.CallToolRequest, in AddContactInput) (*mcp.CallToolResult, AddContactOutput, error) {
|
||||
if strings.TrimSpace(in.Name) == "" {
|
||||
return nil, AddContactOutput{}, errInvalidInput("name is required")
|
||||
}
|
||||
if in.Tags == nil {
|
||||
in.Tags = []string{}
|
||||
}
|
||||
contact, err := t.store.AddProfessionalContact(ctx, ext.ProfessionalContact{
|
||||
Name: strings.TrimSpace(in.Name),
|
||||
Company: strings.TrimSpace(in.Company),
|
||||
Title: strings.TrimSpace(in.Title),
|
||||
Email: strings.TrimSpace(in.Email),
|
||||
Phone: strings.TrimSpace(in.Phone),
|
||||
LinkedInURL: strings.TrimSpace(in.LinkedInURL),
|
||||
HowWeMet: strings.TrimSpace(in.HowWeMet),
|
||||
Tags: in.Tags,
|
||||
Notes: strings.TrimSpace(in.Notes),
|
||||
FollowUpDate: in.FollowUpDate,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, AddContactOutput{}, err
|
||||
}
|
||||
return nil, AddContactOutput{Contact: contact}, nil
|
||||
}
|
||||
|
||||
// search_contacts
|
||||
|
||||
type SearchContactsInput struct {
|
||||
Query string `json:"query,omitempty" jsonschema:"search text matching name, company, title, or notes"`
|
||||
Tags []string `json:"tags,omitempty" jsonschema:"filter by tags (all must match)"`
|
||||
}
|
||||
|
||||
type SearchContactsOutput struct {
|
||||
Contacts []ext.ProfessionalContact `json:"contacts"`
|
||||
}
|
||||
|
||||
func (t *CRMTool) SearchContacts(ctx context.Context, _ *mcp.CallToolRequest, in SearchContactsInput) (*mcp.CallToolResult, SearchContactsOutput, error) {
|
||||
contacts, err := t.store.SearchContacts(ctx, in.Query, in.Tags)
|
||||
if err != nil {
|
||||
return nil, SearchContactsOutput{}, err
|
||||
}
|
||||
if contacts == nil {
|
||||
contacts = []ext.ProfessionalContact{}
|
||||
}
|
||||
return nil, SearchContactsOutput{Contacts: contacts}, nil
|
||||
}
|
||||
|
||||
// log_interaction
|
||||
|
||||
type LogInteractionInput struct {
|
||||
ContactID uuid.UUID `json:"contact_id" jsonschema:"id of the contact"`
|
||||
InteractionType string `json:"interaction_type" jsonschema:"one of: meeting, email, call, coffee, event, linkedin, other"`
|
||||
OccurredAt *time.Time `json:"occurred_at,omitempty" jsonschema:"when it happened (defaults to now)"`
|
||||
Summary string `json:"summary" jsonschema:"summary of the interaction"`
|
||||
FollowUpNeeded bool `json:"follow_up_needed,omitempty"`
|
||||
FollowUpNotes string `json:"follow_up_notes,omitempty"`
|
||||
}
|
||||
|
||||
type LogInteractionOutput struct {
|
||||
Interaction ext.ContactInteraction `json:"interaction"`
|
||||
}
|
||||
|
||||
func (t *CRMTool) LogInteraction(ctx context.Context, _ *mcp.CallToolRequest, in LogInteractionInput) (*mcp.CallToolResult, LogInteractionOutput, error) {
|
||||
if strings.TrimSpace(in.Summary) == "" {
|
||||
return nil, LogInteractionOutput{}, errInvalidInput("summary is required")
|
||||
}
|
||||
occurredAt := time.Now()
|
||||
if in.OccurredAt != nil {
|
||||
occurredAt = *in.OccurredAt
|
||||
}
|
||||
interaction, err := t.store.LogInteraction(ctx, ext.ContactInteraction{
|
||||
ContactID: in.ContactID,
|
||||
InteractionType: in.InteractionType,
|
||||
OccurredAt: occurredAt,
|
||||
Summary: strings.TrimSpace(in.Summary),
|
||||
FollowUpNeeded: in.FollowUpNeeded,
|
||||
FollowUpNotes: strings.TrimSpace(in.FollowUpNotes),
|
||||
})
|
||||
if err != nil {
|
||||
return nil, LogInteractionOutput{}, err
|
||||
}
|
||||
return nil, LogInteractionOutput{Interaction: interaction}, nil
|
||||
}
|
||||
|
||||
// get_contact_history
|
||||
|
||||
type GetContactHistoryInput struct {
|
||||
ContactID uuid.UUID `json:"contact_id" jsonschema:"id of the contact"`
|
||||
}
|
||||
|
||||
type GetContactHistoryOutput struct {
|
||||
History ext.ContactHistory `json:"history"`
|
||||
}
|
||||
|
||||
func (t *CRMTool) GetHistory(ctx context.Context, _ *mcp.CallToolRequest, in GetContactHistoryInput) (*mcp.CallToolResult, GetContactHistoryOutput, error) {
|
||||
history, err := t.store.GetContactHistory(ctx, in.ContactID)
|
||||
if err != nil {
|
||||
return nil, GetContactHistoryOutput{}, err
|
||||
}
|
||||
return nil, GetContactHistoryOutput{History: history}, nil
|
||||
}
|
||||
|
||||
// create_opportunity
|
||||
|
||||
type CreateOpportunityInput struct {
|
||||
ContactID *uuid.UUID `json:"contact_id,omitempty"`
|
||||
Title string `json:"title" jsonschema:"opportunity title"`
|
||||
Description string `json:"description,omitempty"`
|
||||
Stage string `json:"stage,omitempty" jsonschema:"one of: identified, in_conversation, proposal, negotiation, won, lost (default: identified)"`
|
||||
Value *float64 `json:"value,omitempty" jsonschema:"monetary value"`
|
||||
ExpectedCloseDate *time.Time `json:"expected_close_date,omitempty"`
|
||||
Notes string `json:"notes,omitempty"`
|
||||
}
|
||||
|
||||
type CreateOpportunityOutput struct {
|
||||
Opportunity ext.Opportunity `json:"opportunity"`
|
||||
}
|
||||
|
||||
func (t *CRMTool) CreateOpportunity(ctx context.Context, _ *mcp.CallToolRequest, in CreateOpportunityInput) (*mcp.CallToolResult, CreateOpportunityOutput, error) {
|
||||
if strings.TrimSpace(in.Title) == "" {
|
||||
return nil, CreateOpportunityOutput{}, errInvalidInput("title is required")
|
||||
}
|
||||
stage := strings.TrimSpace(in.Stage)
|
||||
if stage == "" {
|
||||
stage = "identified"
|
||||
}
|
||||
opp, err := t.store.CreateOpportunity(ctx, ext.Opportunity{
|
||||
ContactID: in.ContactID,
|
||||
Title: strings.TrimSpace(in.Title),
|
||||
Description: strings.TrimSpace(in.Description),
|
||||
Stage: stage,
|
||||
Value: in.Value,
|
||||
ExpectedCloseDate: in.ExpectedCloseDate,
|
||||
Notes: strings.TrimSpace(in.Notes),
|
||||
})
|
||||
if err != nil {
|
||||
return nil, CreateOpportunityOutput{}, err
|
||||
}
|
||||
return nil, CreateOpportunityOutput{Opportunity: opp}, nil
|
||||
}
|
||||
|
||||
// get_follow_ups_due
|
||||
|
||||
type GetFollowUpsDueInput struct {
|
||||
DaysAhead int `json:"days_ahead,omitempty" jsonschema:"look ahead window in days (default: 7)"`
|
||||
}
|
||||
|
||||
type GetFollowUpsDueOutput struct {
|
||||
Contacts []ext.ProfessionalContact `json:"contacts"`
|
||||
}
|
||||
|
||||
func (t *CRMTool) GetFollowUpsDue(ctx context.Context, _ *mcp.CallToolRequest, in GetFollowUpsDueInput) (*mcp.CallToolResult, GetFollowUpsDueOutput, error) {
|
||||
contacts, err := t.store.GetFollowUpsDue(ctx, in.DaysAhead)
|
||||
if err != nil {
|
||||
return nil, GetFollowUpsDueOutput{}, err
|
||||
}
|
||||
if contacts == nil {
|
||||
contacts = []ext.ProfessionalContact{}
|
||||
}
|
||||
return nil, GetFollowUpsDueOutput{Contacts: contacts}, nil
|
||||
}
|
||||
|
||||
// link_thought_to_contact
|
||||
|
||||
type LinkThoughtToContactInput struct {
|
||||
ContactID uuid.UUID `json:"contact_id" jsonschema:"id of the contact"`
|
||||
ThoughtID uuid.UUID `json:"thought_id" jsonschema:"id of the thought to link"`
|
||||
}
|
||||
|
||||
type LinkThoughtToContactOutput struct {
|
||||
Contact ext.ProfessionalContact `json:"contact"`
|
||||
}
|
||||
|
||||
func (t *CRMTool) LinkThought(ctx context.Context, _ *mcp.CallToolRequest, in LinkThoughtToContactInput) (*mcp.CallToolResult, LinkThoughtToContactOutput, error) {
|
||||
thought, err := t.store.GetThought(ctx, in.ThoughtID)
|
||||
if err != nil {
|
||||
return nil, LinkThoughtToContactOutput{}, fmt.Errorf("thought not found: %w", err)
|
||||
}
|
||||
|
||||
appendText := fmt.Sprintf("\n\n[Linked thought %s]: %s", thought.ID, thought.Content)
|
||||
if err := t.store.AppendThoughtToContactNotes(ctx, in.ContactID, appendText); err != nil {
|
||||
return nil, LinkThoughtToContactOutput{}, err
|
||||
}
|
||||
|
||||
contact, err := t.store.GetContact(ctx, in.ContactID)
|
||||
if err != nil {
|
||||
return nil, LinkThoughtToContactOutput{}, err
|
||||
}
|
||||
return nil, LinkThoughtToContactOutput{Contact: contact}, nil
|
||||
}
|
||||
151
internal/tools/household.go
Normal file
151
internal/tools/household.go
Normal file
@@ -0,0 +1,151 @@
|
||||
package tools
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/modelcontextprotocol/go-sdk/mcp"
|
||||
|
||||
"git.warky.dev/wdevs/amcs/internal/store"
|
||||
ext "git.warky.dev/wdevs/amcs/internal/types"
|
||||
)
|
||||
|
||||
type HouseholdTool struct {
|
||||
store *store.DB
|
||||
}
|
||||
|
||||
func NewHouseholdTool(db *store.DB) *HouseholdTool {
|
||||
return &HouseholdTool{store: db}
|
||||
}
|
||||
|
||||
// add_household_item
|
||||
|
||||
type AddHouseholdItemInput struct {
|
||||
Name string `json:"name" jsonschema:"name of the item"`
|
||||
Category string `json:"category,omitempty" jsonschema:"category (e.g. paint, appliance, measurement, document)"`
|
||||
Location string `json:"location,omitempty" jsonschema:"where in the home this item is"`
|
||||
Details map[string]any `json:"details,omitempty" jsonschema:"flexible metadata (model numbers, colors, specs, etc.)"`
|
||||
Notes string `json:"notes,omitempty"`
|
||||
}
|
||||
|
||||
type AddHouseholdItemOutput struct {
|
||||
Item ext.HouseholdItem `json:"item"`
|
||||
}
|
||||
|
||||
func (t *HouseholdTool) AddItem(ctx context.Context, _ *mcp.CallToolRequest, in AddHouseholdItemInput) (*mcp.CallToolResult, AddHouseholdItemOutput, error) {
|
||||
if strings.TrimSpace(in.Name) == "" {
|
||||
return nil, AddHouseholdItemOutput{}, errInvalidInput("name is required")
|
||||
}
|
||||
if in.Details == nil {
|
||||
in.Details = map[string]any{}
|
||||
}
|
||||
item, err := t.store.AddHouseholdItem(ctx, ext.HouseholdItem{
|
||||
Name: strings.TrimSpace(in.Name),
|
||||
Category: strings.TrimSpace(in.Category),
|
||||
Location: strings.TrimSpace(in.Location),
|
||||
Details: in.Details,
|
||||
Notes: strings.TrimSpace(in.Notes),
|
||||
})
|
||||
if err != nil {
|
||||
return nil, AddHouseholdItemOutput{}, err
|
||||
}
|
||||
return nil, AddHouseholdItemOutput{Item: item}, nil
|
||||
}
|
||||
|
||||
// search_household_items
|
||||
|
||||
type SearchHouseholdItemsInput struct {
|
||||
Query string `json:"query,omitempty" jsonschema:"search text matching name or notes"`
|
||||
Category string `json:"category,omitempty" jsonschema:"filter by category"`
|
||||
Location string `json:"location,omitempty" jsonschema:"filter by location"`
|
||||
}
|
||||
|
||||
type SearchHouseholdItemsOutput struct {
|
||||
Items []ext.HouseholdItem `json:"items"`
|
||||
}
|
||||
|
||||
func (t *HouseholdTool) SearchItems(ctx context.Context, _ *mcp.CallToolRequest, in SearchHouseholdItemsInput) (*mcp.CallToolResult, SearchHouseholdItemsOutput, error) {
|
||||
items, err := t.store.SearchHouseholdItems(ctx, in.Query, in.Category, in.Location)
|
||||
if err != nil {
|
||||
return nil, SearchHouseholdItemsOutput{}, err
|
||||
}
|
||||
if items == nil {
|
||||
items = []ext.HouseholdItem{}
|
||||
}
|
||||
return nil, SearchHouseholdItemsOutput{Items: items}, nil
|
||||
}
|
||||
|
||||
// get_household_item
|
||||
|
||||
type GetHouseholdItemInput struct {
|
||||
ID uuid.UUID `json:"id" jsonschema:"item id"`
|
||||
}
|
||||
|
||||
type GetHouseholdItemOutput struct {
|
||||
Item ext.HouseholdItem `json:"item"`
|
||||
}
|
||||
|
||||
func (t *HouseholdTool) GetItem(ctx context.Context, _ *mcp.CallToolRequest, in GetHouseholdItemInput) (*mcp.CallToolResult, GetHouseholdItemOutput, error) {
|
||||
item, err := t.store.GetHouseholdItem(ctx, in.ID)
|
||||
if err != nil {
|
||||
return nil, GetHouseholdItemOutput{}, err
|
||||
}
|
||||
return nil, GetHouseholdItemOutput{Item: item}, nil
|
||||
}
|
||||
|
||||
// add_vendor
|
||||
|
||||
type AddVendorInput struct {
|
||||
Name string `json:"name" jsonschema:"vendor name"`
|
||||
ServiceType string `json:"service_type,omitempty" jsonschema:"type of service (e.g. plumber, electrician, landscaper)"`
|
||||
Phone string `json:"phone,omitempty"`
|
||||
Email string `json:"email,omitempty"`
|
||||
Website string `json:"website,omitempty"`
|
||||
Notes string `json:"notes,omitempty"`
|
||||
Rating *int `json:"rating,omitempty" jsonschema:"1-5 rating"`
|
||||
}
|
||||
|
||||
type AddVendorOutput struct {
|
||||
Vendor ext.HouseholdVendor `json:"vendor"`
|
||||
}
|
||||
|
||||
func (t *HouseholdTool) AddVendor(ctx context.Context, _ *mcp.CallToolRequest, in AddVendorInput) (*mcp.CallToolResult, AddVendorOutput, error) {
|
||||
if strings.TrimSpace(in.Name) == "" {
|
||||
return nil, AddVendorOutput{}, errInvalidInput("name is required")
|
||||
}
|
||||
vendor, err := t.store.AddVendor(ctx, ext.HouseholdVendor{
|
||||
Name: strings.TrimSpace(in.Name),
|
||||
ServiceType: strings.TrimSpace(in.ServiceType),
|
||||
Phone: strings.TrimSpace(in.Phone),
|
||||
Email: strings.TrimSpace(in.Email),
|
||||
Website: strings.TrimSpace(in.Website),
|
||||
Notes: strings.TrimSpace(in.Notes),
|
||||
Rating: in.Rating,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, AddVendorOutput{}, err
|
||||
}
|
||||
return nil, AddVendorOutput{Vendor: vendor}, nil
|
||||
}
|
||||
|
||||
// list_vendors
|
||||
|
||||
type ListVendorsInput struct {
|
||||
ServiceType string `json:"service_type,omitempty" jsonschema:"filter by service type"`
|
||||
}
|
||||
|
||||
type ListVendorsOutput struct {
|
||||
Vendors []ext.HouseholdVendor `json:"vendors"`
|
||||
}
|
||||
|
||||
func (t *HouseholdTool) ListVendors(ctx context.Context, _ *mcp.CallToolRequest, in ListVendorsInput) (*mcp.CallToolResult, ListVendorsOutput, error) {
|
||||
vendors, err := t.store.ListVendors(ctx, in.ServiceType)
|
||||
if err != nil {
|
||||
return nil, ListVendorsOutput{}, err
|
||||
}
|
||||
if vendors == nil {
|
||||
vendors = []ext.HouseholdVendor{}
|
||||
}
|
||||
return nil, ListVendorsOutput{Vendors: vendors}, nil
|
||||
}
|
||||
137
internal/tools/maintenance.go
Normal file
137
internal/tools/maintenance.go
Normal file
@@ -0,0 +1,137 @@
|
||||
package tools
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/modelcontextprotocol/go-sdk/mcp"
|
||||
|
||||
"git.warky.dev/wdevs/amcs/internal/store"
|
||||
ext "git.warky.dev/wdevs/amcs/internal/types"
|
||||
)
|
||||
|
||||
type MaintenanceTool struct {
|
||||
store *store.DB
|
||||
}
|
||||
|
||||
func NewMaintenanceTool(db *store.DB) *MaintenanceTool {
|
||||
return &MaintenanceTool{store: db}
|
||||
}
|
||||
|
||||
// add_maintenance_task
|
||||
|
||||
type AddMaintenanceTaskInput struct {
|
||||
Name string `json:"name" jsonschema:"task name"`
|
||||
Category string `json:"category,omitempty" jsonschema:"e.g. hvac, plumbing, exterior, appliance, landscaping"`
|
||||
FrequencyDays *int `json:"frequency_days,omitempty" jsonschema:"recurrence interval in days; omit for one-time tasks"`
|
||||
NextDue *time.Time `json:"next_due,omitempty" jsonschema:"when the task is next due"`
|
||||
Priority string `json:"priority,omitempty" jsonschema:"low, medium, high, or urgent (default: medium)"`
|
||||
Notes string `json:"notes,omitempty"`
|
||||
}
|
||||
|
||||
type AddMaintenanceTaskOutput struct {
|
||||
Task ext.MaintenanceTask `json:"task"`
|
||||
}
|
||||
|
||||
func (t *MaintenanceTool) AddTask(ctx context.Context, _ *mcp.CallToolRequest, in AddMaintenanceTaskInput) (*mcp.CallToolResult, AddMaintenanceTaskOutput, error) {
|
||||
if strings.TrimSpace(in.Name) == "" {
|
||||
return nil, AddMaintenanceTaskOutput{}, errInvalidInput("name is required")
|
||||
}
|
||||
priority := strings.TrimSpace(in.Priority)
|
||||
if priority == "" {
|
||||
priority = "medium"
|
||||
}
|
||||
task, err := t.store.AddMaintenanceTask(ctx, ext.MaintenanceTask{
|
||||
Name: strings.TrimSpace(in.Name),
|
||||
Category: strings.TrimSpace(in.Category),
|
||||
FrequencyDays: in.FrequencyDays,
|
||||
NextDue: in.NextDue,
|
||||
Priority: priority,
|
||||
Notes: strings.TrimSpace(in.Notes),
|
||||
})
|
||||
if err != nil {
|
||||
return nil, AddMaintenanceTaskOutput{}, err
|
||||
}
|
||||
return nil, AddMaintenanceTaskOutput{Task: task}, nil
|
||||
}
|
||||
|
||||
// log_maintenance
|
||||
|
||||
type LogMaintenanceInput struct {
|
||||
TaskID uuid.UUID `json:"task_id" jsonschema:"id of the maintenance task"`
|
||||
CompletedAt *time.Time `json:"completed_at,omitempty" jsonschema:"when the work was done (defaults to now)"`
|
||||
PerformedBy string `json:"performed_by,omitempty" jsonschema:"who did the work (self, vendor name, etc.)"`
|
||||
Cost *float64 `json:"cost,omitempty" jsonschema:"cost of the work"`
|
||||
Notes string `json:"notes,omitempty"`
|
||||
NextAction string `json:"next_action,omitempty" jsonschema:"recommended follow-up"`
|
||||
}
|
||||
|
||||
type LogMaintenanceOutput struct {
|
||||
Log ext.MaintenanceLog `json:"log"`
|
||||
}
|
||||
|
||||
func (t *MaintenanceTool) LogWork(ctx context.Context, _ *mcp.CallToolRequest, in LogMaintenanceInput) (*mcp.CallToolResult, LogMaintenanceOutput, error) {
|
||||
completedAt := time.Now()
|
||||
if in.CompletedAt != nil {
|
||||
completedAt = *in.CompletedAt
|
||||
}
|
||||
log, err := t.store.LogMaintenance(ctx, ext.MaintenanceLog{
|
||||
TaskID: in.TaskID,
|
||||
CompletedAt: completedAt,
|
||||
PerformedBy: strings.TrimSpace(in.PerformedBy),
|
||||
Cost: in.Cost,
|
||||
Notes: strings.TrimSpace(in.Notes),
|
||||
NextAction: strings.TrimSpace(in.NextAction),
|
||||
})
|
||||
if err != nil {
|
||||
return nil, LogMaintenanceOutput{}, err
|
||||
}
|
||||
return nil, LogMaintenanceOutput{Log: log}, nil
|
||||
}
|
||||
|
||||
// get_upcoming_maintenance
|
||||
|
||||
type GetUpcomingMaintenanceInput struct {
|
||||
DaysAhead int `json:"days_ahead,omitempty" jsonschema:"how many days to look ahead (default: 30)"`
|
||||
}
|
||||
|
||||
type GetUpcomingMaintenanceOutput struct {
|
||||
Tasks []ext.MaintenanceTask `json:"tasks"`
|
||||
}
|
||||
|
||||
func (t *MaintenanceTool) GetUpcoming(ctx context.Context, _ *mcp.CallToolRequest, in GetUpcomingMaintenanceInput) (*mcp.CallToolResult, GetUpcomingMaintenanceOutput, error) {
|
||||
tasks, err := t.store.GetUpcomingMaintenance(ctx, in.DaysAhead)
|
||||
if err != nil {
|
||||
return nil, GetUpcomingMaintenanceOutput{}, err
|
||||
}
|
||||
if tasks == nil {
|
||||
tasks = []ext.MaintenanceTask{}
|
||||
}
|
||||
return nil, GetUpcomingMaintenanceOutput{Tasks: tasks}, nil
|
||||
}
|
||||
|
||||
// search_maintenance_history
|
||||
|
||||
type SearchMaintenanceHistoryInput struct {
|
||||
Query string `json:"query,omitempty" jsonschema:"search text matching task name or notes"`
|
||||
Category string `json:"category,omitempty" jsonschema:"filter by task category"`
|
||||
Start *time.Time `json:"start,omitempty" jsonschema:"filter logs completed on or after this date"`
|
||||
End *time.Time `json:"end,omitempty" jsonschema:"filter logs completed on or before this date"`
|
||||
}
|
||||
|
||||
type SearchMaintenanceHistoryOutput struct {
|
||||
Logs []ext.MaintenanceLogWithTask `json:"logs"`
|
||||
}
|
||||
|
||||
func (t *MaintenanceTool) SearchHistory(ctx context.Context, _ *mcp.CallToolRequest, in SearchMaintenanceHistoryInput) (*mcp.CallToolResult, SearchMaintenanceHistoryOutput, error) {
|
||||
logs, err := t.store.SearchMaintenanceHistory(ctx, in.Query, in.Category, in.Start, in.End)
|
||||
if err != nil {
|
||||
return nil, SearchMaintenanceHistoryOutput{}, err
|
||||
}
|
||||
if logs == nil {
|
||||
logs = []ext.MaintenanceLogWithTask{}
|
||||
}
|
||||
return nil, SearchMaintenanceHistoryOutput{Logs: logs}, nil
|
||||
}
|
||||
210
internal/tools/meals.go
Normal file
210
internal/tools/meals.go
Normal file
@@ -0,0 +1,210 @@
|
||||
package tools
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/modelcontextprotocol/go-sdk/mcp"
|
||||
|
||||
"git.warky.dev/wdevs/amcs/internal/store"
|
||||
ext "git.warky.dev/wdevs/amcs/internal/types"
|
||||
)
|
||||
|
||||
type MealsTool struct {
|
||||
store *store.DB
|
||||
}
|
||||
|
||||
func NewMealsTool(db *store.DB) *MealsTool {
|
||||
return &MealsTool{store: db}
|
||||
}
|
||||
|
||||
// add_recipe
|
||||
|
||||
type AddRecipeInput struct {
|
||||
Name string `json:"name" jsonschema:"recipe name"`
|
||||
Cuisine string `json:"cuisine,omitempty"`
|
||||
PrepTimeMinutes *int `json:"prep_time_minutes,omitempty"`
|
||||
CookTimeMinutes *int `json:"cook_time_minutes,omitempty"`
|
||||
Servings *int `json:"servings,omitempty"`
|
||||
Ingredients []ext.Ingredient `json:"ingredients,omitempty" jsonschema:"list of ingredients with name, quantity, unit"`
|
||||
Instructions []string `json:"instructions,omitempty" jsonschema:"ordered list of steps"`
|
||||
Tags []string `json:"tags,omitempty"`
|
||||
Rating *int `json:"rating,omitempty" jsonschema:"1-5 rating"`
|
||||
Notes string `json:"notes,omitempty"`
|
||||
}
|
||||
|
||||
type AddRecipeOutput struct {
|
||||
Recipe ext.Recipe `json:"recipe"`
|
||||
}
|
||||
|
||||
func (t *MealsTool) AddRecipe(ctx context.Context, _ *mcp.CallToolRequest, in AddRecipeInput) (*mcp.CallToolResult, AddRecipeOutput, error) {
|
||||
if strings.TrimSpace(in.Name) == "" {
|
||||
return nil, AddRecipeOutput{}, errInvalidInput("name is required")
|
||||
}
|
||||
if in.Ingredients == nil {
|
||||
in.Ingredients = []ext.Ingredient{}
|
||||
}
|
||||
if in.Instructions == nil {
|
||||
in.Instructions = []string{}
|
||||
}
|
||||
if in.Tags == nil {
|
||||
in.Tags = []string{}
|
||||
}
|
||||
recipe, err := t.store.AddRecipe(ctx, ext.Recipe{
|
||||
Name: strings.TrimSpace(in.Name),
|
||||
Cuisine: strings.TrimSpace(in.Cuisine),
|
||||
PrepTimeMinutes: in.PrepTimeMinutes,
|
||||
CookTimeMinutes: in.CookTimeMinutes,
|
||||
Servings: in.Servings,
|
||||
Ingredients: in.Ingredients,
|
||||
Instructions: in.Instructions,
|
||||
Tags: in.Tags,
|
||||
Rating: in.Rating,
|
||||
Notes: strings.TrimSpace(in.Notes),
|
||||
})
|
||||
if err != nil {
|
||||
return nil, AddRecipeOutput{}, err
|
||||
}
|
||||
return nil, AddRecipeOutput{Recipe: recipe}, nil
|
||||
}
|
||||
|
||||
// search_recipes
|
||||
|
||||
type SearchRecipesInput struct {
|
||||
Query string `json:"query,omitempty" jsonschema:"search by recipe name"`
|
||||
Cuisine string `json:"cuisine,omitempty"`
|
||||
Tags []string `json:"tags,omitempty" jsonschema:"filter by tags (all must match)"`
|
||||
Ingredient string `json:"ingredient,omitempty" jsonschema:"filter by ingredient name"`
|
||||
}
|
||||
|
||||
type SearchRecipesOutput struct {
|
||||
Recipes []ext.Recipe `json:"recipes"`
|
||||
}
|
||||
|
||||
func (t *MealsTool) SearchRecipes(ctx context.Context, _ *mcp.CallToolRequest, in SearchRecipesInput) (*mcp.CallToolResult, SearchRecipesOutput, error) {
|
||||
recipes, err := t.store.SearchRecipes(ctx, in.Query, in.Cuisine, in.Tags, in.Ingredient)
|
||||
if err != nil {
|
||||
return nil, SearchRecipesOutput{}, err
|
||||
}
|
||||
if recipes == nil {
|
||||
recipes = []ext.Recipe{}
|
||||
}
|
||||
return nil, SearchRecipesOutput{Recipes: recipes}, nil
|
||||
}
|
||||
|
||||
// update_recipe
|
||||
|
||||
type UpdateRecipeInput struct {
|
||||
ID uuid.UUID `json:"id" jsonschema:"recipe id to update"`
|
||||
Name string `json:"name" jsonschema:"recipe name"`
|
||||
Cuisine string `json:"cuisine,omitempty"`
|
||||
PrepTimeMinutes *int `json:"prep_time_minutes,omitempty"`
|
||||
CookTimeMinutes *int `json:"cook_time_minutes,omitempty"`
|
||||
Servings *int `json:"servings,omitempty"`
|
||||
Ingredients []ext.Ingredient `json:"ingredients,omitempty"`
|
||||
Instructions []string `json:"instructions,omitempty"`
|
||||
Tags []string `json:"tags,omitempty"`
|
||||
Rating *int `json:"rating,omitempty"`
|
||||
Notes string `json:"notes,omitempty"`
|
||||
}
|
||||
|
||||
type UpdateRecipeOutput struct {
|
||||
Recipe ext.Recipe `json:"recipe"`
|
||||
}
|
||||
|
||||
func (t *MealsTool) UpdateRecipe(ctx context.Context, _ *mcp.CallToolRequest, in UpdateRecipeInput) (*mcp.CallToolResult, UpdateRecipeOutput, error) {
|
||||
if strings.TrimSpace(in.Name) == "" {
|
||||
return nil, UpdateRecipeOutput{}, errInvalidInput("name is required")
|
||||
}
|
||||
if in.Ingredients == nil {
|
||||
in.Ingredients = []ext.Ingredient{}
|
||||
}
|
||||
if in.Instructions == nil {
|
||||
in.Instructions = []string{}
|
||||
}
|
||||
if in.Tags == nil {
|
||||
in.Tags = []string{}
|
||||
}
|
||||
recipe, err := t.store.UpdateRecipe(ctx, in.ID, ext.Recipe{
|
||||
Name: strings.TrimSpace(in.Name),
|
||||
Cuisine: strings.TrimSpace(in.Cuisine),
|
||||
PrepTimeMinutes: in.PrepTimeMinutes,
|
||||
CookTimeMinutes: in.CookTimeMinutes,
|
||||
Servings: in.Servings,
|
||||
Ingredients: in.Ingredients,
|
||||
Instructions: in.Instructions,
|
||||
Tags: in.Tags,
|
||||
Rating: in.Rating,
|
||||
Notes: strings.TrimSpace(in.Notes),
|
||||
})
|
||||
if err != nil {
|
||||
return nil, UpdateRecipeOutput{}, err
|
||||
}
|
||||
return nil, UpdateRecipeOutput{Recipe: recipe}, nil
|
||||
}
|
||||
|
||||
// create_meal_plan
|
||||
|
||||
type CreateMealPlanInput struct {
|
||||
WeekStart time.Time `json:"week_start" jsonschema:"Monday of the week to plan"`
|
||||
Meals []ext.MealPlanInput `json:"meals" jsonschema:"list of meal entries for the week"`
|
||||
}
|
||||
|
||||
type CreateMealPlanOutput struct {
|
||||
Entries []ext.MealPlanEntry `json:"entries"`
|
||||
}
|
||||
|
||||
func (t *MealsTool) CreateMealPlan(ctx context.Context, _ *mcp.CallToolRequest, in CreateMealPlanInput) (*mcp.CallToolResult, CreateMealPlanOutput, error) {
|
||||
if len(in.Meals) == 0 {
|
||||
return nil, CreateMealPlanOutput{}, errInvalidInput("meals are required")
|
||||
}
|
||||
entries, err := t.store.CreateMealPlan(ctx, in.WeekStart, in.Meals)
|
||||
if err != nil {
|
||||
return nil, CreateMealPlanOutput{}, err
|
||||
}
|
||||
if entries == nil {
|
||||
entries = []ext.MealPlanEntry{}
|
||||
}
|
||||
return nil, CreateMealPlanOutput{Entries: entries}, nil
|
||||
}
|
||||
|
||||
// get_meal_plan
|
||||
|
||||
type GetMealPlanInput struct {
|
||||
WeekStart time.Time `json:"week_start" jsonschema:"Monday of the week to retrieve"`
|
||||
}
|
||||
|
||||
type GetMealPlanOutput struct {
|
||||
Entries []ext.MealPlanEntry `json:"entries"`
|
||||
}
|
||||
|
||||
func (t *MealsTool) GetMealPlan(ctx context.Context, _ *mcp.CallToolRequest, in GetMealPlanInput) (*mcp.CallToolResult, GetMealPlanOutput, error) {
|
||||
entries, err := t.store.GetMealPlan(ctx, in.WeekStart)
|
||||
if err != nil {
|
||||
return nil, GetMealPlanOutput{}, err
|
||||
}
|
||||
if entries == nil {
|
||||
entries = []ext.MealPlanEntry{}
|
||||
}
|
||||
return nil, GetMealPlanOutput{Entries: entries}, nil
|
||||
}
|
||||
|
||||
// generate_shopping_list
|
||||
|
||||
type GenerateShoppingListInput struct {
|
||||
WeekStart time.Time `json:"week_start" jsonschema:"Monday of the week to generate shopping list for"`
|
||||
}
|
||||
|
||||
type GenerateShoppingListOutput struct {
|
||||
ShoppingList ext.ShoppingList `json:"shopping_list"`
|
||||
}
|
||||
|
||||
func (t *MealsTool) GenerateShoppingList(ctx context.Context, _ *mcp.CallToolRequest, in GenerateShoppingListInput) (*mcp.CallToolResult, GenerateShoppingListOutput, error) {
|
||||
list, err := t.store.GenerateShoppingList(ctx, in.WeekStart)
|
||||
if err != nil {
|
||||
return nil, GenerateShoppingListOutput{}, err
|
||||
}
|
||||
return nil, GenerateShoppingListOutput{ShoppingList: list}, nil
|
||||
}
|
||||
163
internal/tools/reparse_metadata.go
Normal file
163
internal/tools/reparse_metadata.go
Normal file
@@ -0,0 +1,163 @@
|
||||
package tools
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log/slog"
|
||||
"reflect"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/modelcontextprotocol/go-sdk/mcp"
|
||||
"golang.org/x/sync/semaphore"
|
||||
|
||||
"git.warky.dev/wdevs/amcs/internal/ai"
|
||||
"git.warky.dev/wdevs/amcs/internal/config"
|
||||
"git.warky.dev/wdevs/amcs/internal/metadata"
|
||||
"git.warky.dev/wdevs/amcs/internal/session"
|
||||
"git.warky.dev/wdevs/amcs/internal/store"
|
||||
thoughttypes "git.warky.dev/wdevs/amcs/internal/types"
|
||||
)
|
||||
|
||||
const metadataReparseConcurrency = 4
|
||||
|
||||
type ReparseMetadataTool struct {
|
||||
store *store.DB
|
||||
provider ai.Provider
|
||||
capture config.CaptureConfig
|
||||
sessions *session.ActiveProjects
|
||||
logger *slog.Logger
|
||||
}
|
||||
|
||||
type ReparseMetadataInput struct {
|
||||
Project string `json:"project,omitempty" jsonschema:"optional project name or id to scope the reparse"`
|
||||
Limit int `json:"limit,omitempty" jsonschema:"maximum number of thoughts to process in one call; defaults to 100"`
|
||||
IncludeArchived bool `json:"include_archived,omitempty" jsonschema:"whether to include archived thoughts; defaults to false"`
|
||||
OlderThanDays int `json:"older_than_days,omitempty" jsonschema:"only reparse thoughts older than N days; 0 means no restriction"`
|
||||
DryRun bool `json:"dry_run,omitempty" jsonschema:"report counts without updating metadata"`
|
||||
}
|
||||
|
||||
type ReparseMetadataFailure struct {
|
||||
ID string `json:"id"`
|
||||
Error string `json:"error"`
|
||||
}
|
||||
|
||||
type ReparseMetadataOutput struct {
|
||||
Scanned int `json:"scanned"`
|
||||
Reparsed int `json:"reparsed"`
|
||||
Normalized int `json:"normalized"`
|
||||
Updated int `json:"updated"`
|
||||
Skipped int `json:"skipped"`
|
||||
Failed int `json:"failed"`
|
||||
DryRun bool `json:"dry_run"`
|
||||
Failures []ReparseMetadataFailure `json:"failures,omitempty"`
|
||||
}
|
||||
|
||||
func NewReparseMetadataTool(db *store.DB, provider ai.Provider, capture config.CaptureConfig, sessions *session.ActiveProjects, logger *slog.Logger) *ReparseMetadataTool {
|
||||
return &ReparseMetadataTool{store: db, provider: provider, capture: capture, sessions: sessions, logger: logger}
|
||||
}
|
||||
|
||||
func (t *ReparseMetadataTool) Handle(ctx context.Context, req *mcp.CallToolRequest, in ReparseMetadataInput) (*mcp.CallToolResult, ReparseMetadataOutput, error) {
|
||||
limit := in.Limit
|
||||
if limit <= 0 {
|
||||
limit = 100
|
||||
}
|
||||
|
||||
project, err := resolveProject(ctx, t.store, t.sessions, req, in.Project, false)
|
||||
if err != nil {
|
||||
return nil, ReparseMetadataOutput{}, err
|
||||
}
|
||||
|
||||
var projectID *uuid.UUID
|
||||
if project != nil {
|
||||
projectID = &project.ID
|
||||
}
|
||||
|
||||
thoughts, err := t.store.ListThoughtsForMetadataReparse(ctx, limit, projectID, in.IncludeArchived, in.OlderThanDays)
|
||||
if err != nil {
|
||||
return nil, ReparseMetadataOutput{}, err
|
||||
}
|
||||
|
||||
out := ReparseMetadataOutput{
|
||||
Scanned: len(thoughts),
|
||||
DryRun: in.DryRun,
|
||||
}
|
||||
|
||||
if in.DryRun || len(thoughts) == 0 {
|
||||
return nil, out, nil
|
||||
}
|
||||
|
||||
start := time.Now()
|
||||
sem := semaphore.NewWeighted(metadataReparseConcurrency)
|
||||
var mu sync.Mutex
|
||||
var wg sync.WaitGroup
|
||||
|
||||
for _, thought := range thoughts {
|
||||
if ctx.Err() != nil {
|
||||
break
|
||||
}
|
||||
if err := sem.Acquire(ctx, 1); err != nil {
|
||||
break
|
||||
}
|
||||
wg.Add(1)
|
||||
go func(thought thoughttypes.Thought) {
|
||||
defer wg.Done()
|
||||
defer sem.Release(1)
|
||||
|
||||
normalizedCurrent := metadata.Normalize(thought.Metadata, t.capture)
|
||||
|
||||
extracted, extractErr := t.provider.ExtractMetadata(ctx, thought.Content)
|
||||
normalizedTarget := normalizedCurrent
|
||||
if extractErr != nil {
|
||||
mu.Lock()
|
||||
out.Normalized++
|
||||
mu.Unlock()
|
||||
t.logger.Warn("metadata reparse extract failed, using normalized existing metadata", slog.String("thought_id", thought.ID.String()), slog.String("error", extractErr.Error()))
|
||||
} else {
|
||||
normalizedTarget = metadata.Normalize(extracted, t.capture)
|
||||
mu.Lock()
|
||||
out.Reparsed++
|
||||
mu.Unlock()
|
||||
}
|
||||
|
||||
if metadataEqual(thought.Metadata, normalizedTarget) {
|
||||
mu.Lock()
|
||||
out.Skipped++
|
||||
mu.Unlock()
|
||||
return
|
||||
}
|
||||
|
||||
if _, updateErr := t.store.UpdateThought(ctx, thought.ID, thought.Content, nil, "", normalizedTarget, thought.ProjectID); updateErr != nil {
|
||||
mu.Lock()
|
||||
out.Failures = append(out.Failures, ReparseMetadataFailure{ID: thought.ID.String(), Error: updateErr.Error()})
|
||||
mu.Unlock()
|
||||
t.logger.Warn("metadata reparse update failed", slog.String("thought_id", thought.ID.String()), slog.String("error", updateErr.Error()))
|
||||
return
|
||||
}
|
||||
|
||||
mu.Lock()
|
||||
out.Updated++
|
||||
mu.Unlock()
|
||||
}(thought)
|
||||
}
|
||||
|
||||
wg.Wait()
|
||||
|
||||
out.Failed = len(out.Failures)
|
||||
|
||||
t.logger.Info("metadata reparse completed",
|
||||
slog.Int("scanned", out.Scanned),
|
||||
slog.Int("reparsed", out.Reparsed),
|
||||
slog.Int("normalized", out.Normalized),
|
||||
slog.Int("updated", out.Updated),
|
||||
slog.Int("skipped", out.Skipped),
|
||||
slog.Int("failed", out.Failed),
|
||||
slog.Duration("duration", time.Since(start)),
|
||||
)
|
||||
|
||||
return nil, out, nil
|
||||
}
|
||||
|
||||
func metadataEqual(a, b thoughttypes.ThoughtMetadata) bool {
|
||||
return reflect.DeepEqual(a, b)
|
||||
}
|
||||
Reference in New Issue
Block a user