xonecas.io
60255
Zn

Zoea-Nova / files

Zoea-Nova / internal / mcp / tools.go

4.7 KB
1package mcp
2
3import (
4	"context"
5	"encoding/json"
6	"fmt"
7)
8
9// SearchResult represents a search result from memory.
10type SearchResult struct {
11	Role      string
12	Source    string
13	Content   string
14	CreatedAt string
15}
16
17type ReasoningResult struct {
18	Role      string
19	Source    string
20	Content   string
21	Reasoning string
22	CreatedAt string
23}
24
25// Orchestrator defines the interface for swarm orchestration.
26// This interface breaks the import cycle between mcp and core packages.
27type Orchestrator interface {
28	MysisCount() int
29	MaxMyses() int
30	GetStateCounts() map[string]int
31	SendMessageAsync(mysisID, message string) error
32	BroadcastAsync(message string) error
33	BroadcastFrom(senderID, message string) error
34	SearchMessages(mysisID, query string, limit int) ([]SearchResult, error)
35	SearchReasoning(mysisID, query string, limit int) ([]ReasoningResult, error)
36}
37
38// RegisterOrchestratorTools registers the internal orchestration tools with the proxy.
39func RegisterOrchestratorTools(proxy *Proxy, orchestrator Orchestrator) {
40	// Search messages tool
41	proxy.RegisterTool(
42		Tool{
43			Name:        "zoea_search_messages",
44			Description: "Search your past messages (direct messages, responses, tool results) by text content",
45			InputSchema: json.RawMessage(`{
46				"type": "object",
47				"properties": {
48					"mysis_id": {"type": "string", "description": "The ID of the mysis whose messages to search"},
49					"query": {"type": "string", "description": "Text to search for in message content"},
50					"limit": {"type": "integer", "description": "Maximum results to return (default 20, max 100)"}
51				},
52				"required": ["mysis_id", "query"]
53			}`),
54		},
55		func(ctx context.Context, args json.RawMessage) (*ToolResult, error) {
56			var params struct {
57				MysisID string `json:"mysis_id"`
58				Query   string `json:"query"`
59				Limit   int    `json:"limit"`
60			}
61			if err := json.Unmarshal(args, &params); err != nil {
62				return &ToolResult{
63					Content: []ContentBlock{{Type: "text", Text: fmt.Sprintf("invalid arguments: %v", err)}},
64					IsError: true,
65				}, nil
66			}
67
68			if params.Query == "" {
69				return &ToolResult{
70					Content: []ContentBlock{{Type: "text", Text: "query cannot be empty"}},
71					IsError: true,
72				}, nil
73			}
74
75			limit := params.Limit
76			if limit <= 0 {
77				limit = 20
78			} else if limit > 100 {
79				limit = 100
80			}
81
82			results, err := orchestrator.SearchMessages(params.MysisID, params.Query, limit)
83			if err != nil {
84				return &ToolResult{
85					Content: []ContentBlock{{Type: "text", Text: fmt.Sprintf("search failed: %v", err)}},
86					IsError: true,
87				}, nil
88			}
89
90			if len(results) == 0 {
91				return &ToolResult{
92					Content: []ContentBlock{{Type: "text", Text: fmt.Sprintf("no messages found matching '%s'", params.Query)}},
93				}, nil
94			}
95
96			data, _ := json.MarshalIndent(results, "", "  ")
97			return &ToolResult{
98				Content: []ContentBlock{{Type: "text", Text: string(data)}},
99			}, nil
100		},
101	)
102
103	proxy.RegisterTool(
104		Tool{
105			Name:        "zoea_search_reasoning",
106			Description: "Search your past reasoning content by text",
107			InputSchema: json.RawMessage(`{
108				"type": "object",
109				"properties": {
110					"mysis_id": {"type": "string", "description": "The ID of the mysis whose reasoning to search"},
111					"query": {"type": "string", "description": "Text to search for in reasoning content"},
112					"limit": {"type": "integer", "description": "Maximum results to return (default 20, max 100)"}
113				},
114				"required": ["mysis_id", "query"]
115			}`),
116		},
117		func(ctx context.Context, args json.RawMessage) (*ToolResult, error) {
118			var params struct {
119				MysisID string `json:"mysis_id"`
120				Query   string `json:"query"`
121				Limit   int    `json:"limit"`
122			}
123			if err := json.Unmarshal(args, &params); err != nil {
124				return &ToolResult{
125					Content: []ContentBlock{{Type: "text", Text: fmt.Sprintf("invalid arguments: %v", err)}},
126					IsError: true,
127				}, nil
128			}
129
130			if params.Query == "" {
131				return &ToolResult{
132					Content: []ContentBlock{{Type: "text", Text: "query cannot be empty"}},
133					IsError: true,
134				}, nil
135			}
136
137			limit := params.Limit
138			if limit <= 0 {
139				limit = 20
140			} else if limit > 100 {
141				limit = 100
142			}
143
144			results, err := orchestrator.SearchReasoning(params.MysisID, params.Query, limit)
145			if err != nil {
146				return &ToolResult{
147					Content: []ContentBlock{{Type: "text", Text: fmt.Sprintf("search failed: %v", err)}},
148					IsError: true,
149				}, nil
150			}
151
152			if len(results) == 0 {
153				return &ToolResult{
154					Content: []ContentBlock{{Type: "text", Text: fmt.Sprintf("no reasoning found matching '%s'", params.Query)}},
155				}, nil
156			}
157
158			data, _ := json.MarshalIndent(results, "", "  ")
159			return &ToolResult{
160				Content: []ContentBlock{{Type: "text", Text: string(data)}},
161			}, nil
162		},
163	)
164}