1package tools
 2
 3import (
 4	"context"
 5	"encoding/json"
 6)
 7
 8type ToolInfo struct {
 9	Name        string
10	Description string
11	Parameters  map[string]any
12	Required    []string
13}
14
15type toolResponseType string
16
17type (
18	sessionIDContextKey string
19	messageIDContextKey string
20)
21
22const (
23	ToolResponseTypeText  toolResponseType = "text"
24	ToolResponseTypeImage toolResponseType = "image"
25
26	SessionIDContextKey sessionIDContextKey = "session_id"
27	MessageIDContextKey messageIDContextKey = "message_id"
28)
29
30type ToolResponse struct {
31	Type     toolResponseType `json:"type"`
32	Content  string           `json:"content"`
33	Metadata string           `json:"metadata,omitempty"`
34	IsError  bool             `json:"is_error"`
35}
36
37func NewTextResponse(content string) ToolResponse {
38	return ToolResponse{
39		Type:    ToolResponseTypeText,
40		Content: content,
41	}
42}
43
44func WithResponseMetadata(response ToolResponse, metadata any) ToolResponse {
45	if metadata != nil {
46		metadataBytes, err := json.Marshal(metadata)
47		if err != nil {
48			return response
49		}
50		response.Metadata = string(metadataBytes)
51	}
52	return response
53}
54
55func NewTextErrorResponse(content string) ToolResponse {
56	return ToolResponse{
57		Type:    ToolResponseTypeText,
58		Content: content,
59		IsError: true,
60	}
61}
62
63type ToolCall struct {
64	ID    string `json:"id"`
65	Name  string `json:"name"`
66	Input string `json:"input"`
67}
68
69type BaseTool interface {
70	Info() ToolInfo
71	Name() string
72	Run(ctx context.Context, params ToolCall) (ToolResponse, error)
73}
74
75func GetContextValues(ctx context.Context) (string, string) {
76	sessionID := ctx.Value(SessionIDContextKey)
77	messageID := ctx.Value(MessageIDContextKey)
78	if sessionID == nil {
79		return "", ""
80	}
81	if messageID == nil {
82		return sessionID.(string), ""
83	}
84	return sessionID.(string), messageID.(string)
85}