Repository navigation
feat(core): add session-store backed todo core tool #283
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
kskarthik
wants to merge
13
commits into
mark3labs:master
Choose a base branch
from
kskarthik:feat/core-todo-tool
base: master
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.
Open
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
6d148b9
feat(core): add session-store backed todo core tool
kskarthik eeca159
style(test): fix golangci-lint modernize and errcheck issues
kskarthik 9f32c96
fix(core): reject duplicate explicit todo IDs and avoid collision on …
kskarthik e4b81d9
fix(session): record branch provenance in CompactionEntry for branch-…
kskarthik f0b71c7
fix(kit): respect explicit WithTodoStore over session store and docum…
kskarthik 2fa8892
fix(kit): bind sessionTodoStore to owner branch to reject writes afte…
kskarthik 7092504
fix(session): preserve pre-compaction extension history and map PreCo…
kskarthik 9eb3582
fix(core): treat empty todos as read, add explicit action clear, and …
kskarthik 2d27aae
fix(kit): preserve sessionTodoStore ownership across context compaction
kskarthik 5624f42
fix(kit): use jobScope().leafID for effectiveTodoStore branch binding
kskarthik beb6f8d
fix(kit): support chained compaction traversal in isBranchActive and …
kskarthik b3039e9
fix(kit): rebind job todo store to owner branch and propagate CoreToo…
kskarthik 4e63ec8
test(kit): assert completed job status in TestBackgroundJob_RebindsTo…
kskarthik 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,316 @@ | ||
| package core | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "strings" | ||
| "sync" | ||
|
|
||
| "charm.land/fantasy" | ||
| ) | ||
|
|
||
| // Todo status values. | ||
| const ( | ||
| TodoStatusPending = "pending" | ||
| TodoStatusInProgress = "in_progress" | ||
| TodoStatusCompleted = "completed" | ||
| TodoStatusCancelled = "cancelled" | ||
| ) | ||
|
|
||
| // Todo priority values. | ||
| const ( | ||
| TodoPriorityLow = "low" | ||
| TodoPriorityMedium = "medium" | ||
| TodoPriorityHigh = "high" | ||
| ) | ||
|
|
||
| // TodoItem represents a single task in a structured checklist. | ||
| type TodoItem struct { | ||
| ID string `json:"id"` | ||
| Content string `json:"content"` | ||
| Status string `json:"status"` | ||
| Priority string `json:"priority,omitempty"` | ||
| } | ||
|
|
||
| // TodoStore provides storage and retrieval for todo items. | ||
| type TodoStore interface { | ||
| GetTodos(ctx context.Context) ([]TodoItem, error) | ||
| SaveTodos(ctx context.Context, todos []TodoItem) error | ||
| } | ||
|
|
||
| type memoryTodoStore struct { | ||
| mu sync.RWMutex | ||
| todos []TodoItem | ||
| } | ||
|
|
||
| // NewMemoryTodoStore returns a thread-safe in-memory TodoStore. | ||
| func NewMemoryTodoStore() TodoStore { | ||
| return &memoryTodoStore{} | ||
| } | ||
|
|
||
| func (s *memoryTodoStore) GetTodos(ctx context.Context) ([]TodoItem, error) { | ||
| s.mu.RLock() | ||
| defer s.mu.RUnlock() | ||
| res := make([]TodoItem, len(s.todos)) | ||
| copy(res, s.todos) | ||
| return res, nil | ||
| } | ||
|
|
||
| func (s *memoryTodoStore) SaveTodos(ctx context.Context, todos []TodoItem) error { | ||
| s.mu.Lock() | ||
| defer s.mu.Unlock() | ||
| s.todos = make([]TodoItem, len(todos)) | ||
| copy(s.todos, todos) | ||
| return nil | ||
| } | ||
|
|
||
| type todoStoreKey struct{} | ||
|
|
||
| // ContextWithTodoStore stores the TodoStore in ctx for use by the todo tool. | ||
| func ContextWithTodoStore(ctx context.Context, store TodoStore) context.Context { | ||
| return context.WithValue(ctx, todoStoreKey{}, store) | ||
| } | ||
|
|
||
| // TodoStoreFromContext returns the TodoStore stored in ctx, or nil if none is set. | ||
| func TodoStoreFromContext(ctx context.Context) TodoStore { | ||
| if s, ok := ctx.Value(todoStoreKey{}).(TodoStore); ok { | ||
| return s | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| type todoArgs struct { | ||
| Action string `json:"action,omitempty"` | ||
| Todos *[]TodoItem `json:"todos,omitempty"` | ||
| } | ||
|
|
||
| // NewTodoTool creates the todo core tool for managing structured task checklists. | ||
| func NewTodoTool(opts ...ToolOption) fantasy.AgentTool { | ||
| cfg := ApplyOptions(opts) | ||
| defaultStore := cfg.TodoStore | ||
| if defaultStore == nil { | ||
| defaultStore = NewMemoryTodoStore() | ||
| } | ||
|
|
||
| return &coreTool{ | ||
| info: fantasy.ToolInfo{ | ||
| Name: "todo", | ||
| Description: `Track and manage structured task checklists for complex multi-step workflows. | ||
|
|
||
| Use this tool to plan multi-step tasks, track progress, and maintain focus across long workflows. | ||
| - To view existing tasks: call with no arguments, {"action": "read"}, or {"todos": []}. | ||
| - To update or replace tasks: call with {"todos": [...]}. Each task must have: | ||
| - id: unique string identifier (e.g. "1", "2") | ||
| - content: clear description of the step | ||
| - status: "pending", "in_progress", "completed", or "cancelled" | ||
| - priority: optional "high", "medium", or "low" (default: "medium") | ||
| - To clear all tasks: call with {"action": "clear"}. | ||
|
|
||
| Tasks are persisted in the session store and survive context compaction and session restarts.`, | ||
| Parameters: map[string]any{ | ||
| "action": map[string]any{ | ||
| "type": "string", | ||
| "enum": []string{"read", "write", "clear"}, | ||
| "description": "Optional action. If non-empty 'todos' is provided, 'write' is assumed. 'clear' removes all tasks. Defaults to 'read'.", | ||
| }, | ||
| "todos": map[string]any{ | ||
| "type": "array", | ||
| "description": "The updated list of todo items.", | ||
| "items": map[string]any{ | ||
| "type": "object", | ||
| "properties": map[string]any{ | ||
| "id": map[string]any{ | ||
| "type": "string", | ||
| "description": "Unique identifier for the task.", | ||
| }, | ||
| "content": map[string]any{ | ||
| "type": "string", | ||
| "description": "Detailed description of the task.", | ||
| }, | ||
| "status": map[string]any{ | ||
| "type": "string", | ||
| "enum": []string{"pending", "in_progress", "completed", "cancelled"}, | ||
| "description": "Current status of the task.", | ||
| }, | ||
| "priority": map[string]any{ | ||
| "type": "string", | ||
| "enum": []string{"high", "medium", "low"}, | ||
| "description": "Priority level of the task.", | ||
| }, | ||
| }, | ||
| "required": []string{"content", "status"}, | ||
| }, | ||
| }, | ||
| }, | ||
| Required: []string{}, | ||
| Parallel: false, | ||
| }, | ||
| handler: func(ctx context.Context, call fantasy.ToolCall) (fantasy.ToolResponse, error) { | ||
| return executeTodo(ctx, call, defaultStore) | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| func executeTodo(ctx context.Context, call fantasy.ToolCall, defaultStore TodoStore) (fantasy.ToolResponse, error) { | ||
| if err := ctx.Err(); err != nil { | ||
| return fantasy.ToolResponse{}, err | ||
| } | ||
|
|
||
| store := TodoStoreFromContext(ctx) | ||
| if store == nil { | ||
| store = defaultStore | ||
| } | ||
|
|
||
| var args todoArgs | ||
| if err := parseArgs(call.Input, &args); err != nil { | ||
| return fantasy.NewTextErrorResponse(fmt.Sprintf("invalid arguments: %v", err)), nil | ||
| } | ||
|
|
||
| if args.Action == "clear" { | ||
| if err := store.SaveTodos(ctx, []TodoItem{}); err != nil { | ||
| return fantasy.NewTextErrorResponse(fmt.Sprintf("failed to clear todos: %v", err)), nil | ||
| } | ||
| return fantasy.NewTextResponse("Todos cleared."), nil | ||
| } | ||
|
|
||
| // If non-empty todos are provided, treat as write action. | ||
| if args.Todos != nil && len(*args.Todos) > 0 { | ||
| items := *args.Todos | ||
|
|
||
| // Reserve all non-empty explicit IDs and reject duplicate explicit IDs. | ||
| usedIDs := make(map[string]bool, len(items)) | ||
| for i, item := range items { | ||
| id := strings.TrimSpace(item.ID) | ||
| if id != "" { | ||
| if usedIDs[id] { | ||
| return fantasy.NewTextErrorResponse(fmt.Sprintf("item %d: duplicate todo id %q", i+1, id)), nil | ||
| } | ||
| usedIDs[id] = true | ||
| } | ||
| } | ||
|
|
||
| nextID := 1 | ||
| normalized := make([]TodoItem, len(items)) | ||
| for i, item := range items { | ||
| content := strings.TrimSpace(item.Content) | ||
| if content == "" { | ||
| return fantasy.NewTextErrorResponse(fmt.Sprintf("item %d: content cannot be empty", i+1)), nil | ||
| } | ||
|
|
||
| status := strings.ToLower(strings.TrimSpace(item.Status)) | ||
| switch status { | ||
| case "done": | ||
| status = TodoStatusCompleted | ||
| case "in-progress": | ||
| status = TodoStatusInProgress | ||
| case TodoStatusPending, TodoStatusInProgress, TodoStatusCompleted, TodoStatusCancelled: | ||
| // valid | ||
| case "": | ||
| status = TodoStatusPending | ||
| default: | ||
| return fantasy.NewTextErrorResponse(fmt.Sprintf("item %d: invalid status %q (must be pending, in_progress, completed, or cancelled)", i+1, item.Status)), nil | ||
| } | ||
|
|
||
| priority := strings.ToLower(strings.TrimSpace(item.Priority)) | ||
| switch priority { | ||
| case TodoPriorityHigh, TodoPriorityMedium, TodoPriorityLow: | ||
| // valid | ||
| default: | ||
| priority = TodoPriorityMedium | ||
| } | ||
|
|
||
| id := strings.TrimSpace(item.ID) | ||
| if id == "" { | ||
| for { | ||
| cand := fmt.Sprintf("%d", nextID) | ||
| nextID++ | ||
| if !usedIDs[cand] { | ||
| id = cand | ||
| usedIDs[id] = true | ||
| break | ||
| } | ||
| } | ||
| } | ||
|
|
||
| normalized[i] = TodoItem{ | ||
| ID: id, | ||
| Content: content, | ||
| Status: status, | ||
| Priority: priority, | ||
| } | ||
| } | ||
|
|
||
| if err := store.SaveTodos(ctx, normalized); err != nil { | ||
| return fantasy.NewTextErrorResponse(fmt.Sprintf("failed to save todos: %v", err)), nil | ||
| } | ||
|
|
||
| return fantasy.NewTextResponse(FormatTodoList(normalized)), nil | ||
| } | ||
|
|
||
| if args.Action == "write" { | ||
| return fantasy.NewTextErrorResponse("action 'write' requires 'todos' list"), nil | ||
| } | ||
|
|
||
| // Read action | ||
| todos, err := store.GetTodos(ctx) | ||
| if err != nil { | ||
| return fantasy.NewTextErrorResponse(fmt.Sprintf("failed to read todos: %v", err)), nil | ||
| } | ||
|
|
||
| if len(todos) == 0 { | ||
| return fantasy.NewTextResponse("No todos found. Call todo with a list of tasks in \"todos\" to create some."), nil | ||
| } | ||
|
|
||
| return fantasy.NewTextResponse(FormatTodoList(todos)), nil | ||
| } | ||
|
|
||
| // FormatTodoList renders a list of TodoItems into a formatted human-readable checklist. | ||
| func FormatTodoList(todos []TodoItem) string { | ||
| if len(todos) == 0 { | ||
| return "No todos found." | ||
| } | ||
|
|
||
| var completed, inProgress, cancelled int | ||
| for _, t := range todos { | ||
| switch t.Status { | ||
| case TodoStatusCompleted: | ||
| completed++ | ||
| case TodoStatusInProgress: | ||
| inProgress++ | ||
| case TodoStatusCancelled: | ||
| cancelled++ | ||
| } | ||
| } | ||
|
|
||
| var sb strings.Builder | ||
| fmt.Fprintf(&sb, "Todos (%d/%d completed", completed, len(todos)) | ||
| if inProgress > 0 { | ||
| fmt.Fprintf(&sb, ", %d in progress", inProgress) | ||
| } | ||
| if cancelled > 0 { | ||
| fmt.Fprintf(&sb, ", %d cancelled", cancelled) | ||
| } | ||
| sb.WriteString("):\n") | ||
|
|
||
| for _, t := range todos { | ||
| var marker string | ||
| switch t.Status { | ||
| case TodoStatusCompleted: | ||
| marker = "[x]" | ||
| case TodoStatusInProgress: | ||
| marker = "[~]" | ||
| case TodoStatusCancelled: | ||
| marker = "[-]" | ||
| default: | ||
| marker = "[ ]" | ||
| } | ||
|
|
||
| prio := "" | ||
| if t.Priority != "" && t.Priority != TodoPriorityMedium { | ||
| prio = fmt.Sprintf(" [%s]", t.Priority) | ||
| } | ||
| fmt.Fprintf(&sb, "%s %s: %s%s\n", marker, t.ID, t.Content, prio) | ||
| } | ||
|
|
||
| return strings.TrimRight(sb.String(), "\n") | ||
| } | ||
Oops, something went wrong.
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.