feat: add gt mol attach-from-mail command (gt-h6eq.7)
Allows agents to self-pin work from mail messages. The command: 1. Reads a mail message by ID 2. Extracts molecule ID from the body (attached_molecule:, molecule_id:, etc.) 3. Attaches the molecule to the agent's pinned bead (hook) 4. Marks the mail as read Includes unit tests for the molecule ID extraction logic. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
146
internal/cmd/molecule_attach_from_mail.go
Normal file
146
internal/cmd/molecule_attach_from_mail.go
Normal file
@@ -0,0 +1,146 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/steveyegge/gastown/internal/beads"
|
||||
"github.com/steveyegge/gastown/internal/mail"
|
||||
"github.com/steveyegge/gastown/internal/style"
|
||||
"github.com/steveyegge/gastown/internal/workspace"
|
||||
)
|
||||
|
||||
// runMoleculeAttachFromMail handles the "gt mol attach-from-mail <mail-id>" command.
|
||||
// It reads a mail message, extracts the molecule ID from the body, and attaches
|
||||
// it to the current agent's hook (pinned bead).
|
||||
func runMoleculeAttachFromMail(cmd *cobra.Command, args []string) error {
|
||||
mailID := args[0]
|
||||
|
||||
// Get current working directory and town root
|
||||
cwd, err := os.Getwd()
|
||||
if err != nil {
|
||||
return fmt.Errorf("getting current directory: %w", err)
|
||||
}
|
||||
|
||||
townRoot, err := workspace.FindFromCwd()
|
||||
if err != nil || townRoot == "" {
|
||||
return fmt.Errorf("not in a Gas Town workspace")
|
||||
}
|
||||
|
||||
// Detect agent role and identity
|
||||
roleCtx := detectRole(cwd, townRoot)
|
||||
agentIdentity := buildAgentIdentity(roleCtx)
|
||||
if agentIdentity == "" {
|
||||
return fmt.Errorf("cannot determine agent identity from current directory (role: %s)", roleCtx.Role)
|
||||
}
|
||||
|
||||
// Get the agent's mailbox
|
||||
mailWorkDir, err := findMailWorkDir()
|
||||
if err != nil {
|
||||
return fmt.Errorf("finding mail workspace: %w", err)
|
||||
}
|
||||
|
||||
router := mail.NewRouter(mailWorkDir)
|
||||
mailbox, err := router.GetMailbox(agentIdentity)
|
||||
if err != nil {
|
||||
return fmt.Errorf("getting mailbox: %w", err)
|
||||
}
|
||||
|
||||
// Read the mail message
|
||||
msg, err := mailbox.Get(mailID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("reading mail message: %w", err)
|
||||
}
|
||||
|
||||
// Extract molecule ID from mail body
|
||||
moleculeID := extractMoleculeIDFromMail(msg.Body)
|
||||
if moleculeID == "" {
|
||||
return fmt.Errorf("no attached_molecule field found in mail body")
|
||||
}
|
||||
|
||||
// Find local beads directory
|
||||
workDir, err := findLocalBeadsDir()
|
||||
if err != nil {
|
||||
return fmt.Errorf("not in a beads workspace: %w", err)
|
||||
}
|
||||
|
||||
b := beads.New(workDir)
|
||||
|
||||
// Find the agent's pinned bead (hook)
|
||||
pinnedBeads, err := b.List(beads.ListOptions{
|
||||
Status: beads.StatusPinned,
|
||||
Assignee: agentIdentity,
|
||||
Priority: -1,
|
||||
})
|
||||
if err != nil {
|
||||
return fmt.Errorf("listing pinned beads: %w", err)
|
||||
}
|
||||
|
||||
if len(pinnedBeads) == 0 {
|
||||
return fmt.Errorf("no pinned bead found for agent %s - create one first", agentIdentity)
|
||||
}
|
||||
|
||||
// Use the first pinned bead as the hook
|
||||
hookBead := pinnedBeads[0]
|
||||
|
||||
// Check if molecule exists
|
||||
_, err = b.Show(moleculeID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("molecule %s not found: %w", moleculeID, err)
|
||||
}
|
||||
|
||||
// Attach the molecule to the hook
|
||||
issue, err := b.AttachMolecule(hookBead.ID, moleculeID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("attaching molecule: %w", err)
|
||||
}
|
||||
|
||||
// Mark mail as read
|
||||
if err := mailbox.MarkRead(mailID); err != nil {
|
||||
// Non-fatal: log warning but don't fail
|
||||
fmt.Fprintf(os.Stderr, "Warning: could not mark mail as read: %v\n", err)
|
||||
}
|
||||
|
||||
// Output success
|
||||
attachment := beads.ParseAttachmentFields(issue)
|
||||
fmt.Printf("%s Attached molecule from mail\n", style.Bold.Render("✓"))
|
||||
fmt.Printf(" Mail: %s\n", mailID)
|
||||
fmt.Printf(" Hook: %s\n", hookBead.ID)
|
||||
fmt.Printf(" Molecule: %s\n", moleculeID)
|
||||
if attachment != nil && attachment.AttachedAt != "" {
|
||||
fmt.Printf(" Attached at: %s\n", attachment.AttachedAt)
|
||||
}
|
||||
fmt.Printf("\n%s Run 'gt mol status' to see progress\n", style.Dim.Render("Hint:"))
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// extractMoleculeIDFromMail extracts a molecule ID from a mail message body.
|
||||
// It looks for patterns like:
|
||||
// - attached_molecule: <id>
|
||||
// - molecule_id: <id>
|
||||
// - molecule: <id>
|
||||
//
|
||||
// The ID is expected to be on the same line after the colon.
|
||||
func extractMoleculeIDFromMail(body string) string {
|
||||
// Try various patterns for molecule ID in mail body (case-insensitive)
|
||||
patterns := []string{
|
||||
`(?i)attached_molecule:\s*(\S+)`,
|
||||
`(?i)molecule_id:\s*(\S+)`,
|
||||
`(?i)molecule:\s*(\S+)`,
|
||||
`(?i)mol:\s*(\S+)`,
|
||||
}
|
||||
|
||||
for _, pattern := range patterns {
|
||||
re := regexp.MustCompile(pattern)
|
||||
matches := re.FindStringSubmatch(body)
|
||||
if len(matches) >= 2 {
|
||||
return strings.TrimSpace(matches[1])
|
||||
}
|
||||
}
|
||||
|
||||
return ""
|
||||
}
|
||||
Reference in New Issue
Block a user