114 lines
2.9 KiB
Go
114 lines
2.9 KiB
Go
package superChatEvent
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"io"
|
|
"log/slog"
|
|
|
|
"github.com/eat-pray-ai/yutu/cmd"
|
|
"github.com/eat-pray-ai/yutu/pkg/superChatEvent"
|
|
"github.com/mark3labs/mcp-go/mcp"
|
|
"github.com/spf13/cobra"
|
|
)
|
|
|
|
func init() {
|
|
cmd.MCP.AddTool(listTool, listHandler)
|
|
superChatEventCmd.AddCommand(listCmd)
|
|
|
|
listCmd.Flags().StringVarP(&hl, "hl", "l", "", hlUsage)
|
|
listCmd.Flags().Int64VarP(&maxResults, "maxResults", "n", 5, cmd.MRUsage)
|
|
listCmd.Flags().StringSliceVarP(
|
|
&parts, "parts", "p", []string{"id", "snippet"}, cmd.PartsUsage,
|
|
)
|
|
listCmd.Flags().StringVarP(&output, "output", "o", "table", cmd.TableUsage)
|
|
listCmd.Flags().StringVarP(&jpath, "jsonpath", "j", "", cmd.JPUsage)
|
|
}
|
|
|
|
var listCmd = &cobra.Command{
|
|
Use: "list",
|
|
Short: short,
|
|
Long: long,
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
err := list(cmd.OutOrStdout())
|
|
if err != nil {
|
|
_ = cmd.Help()
|
|
cmd.PrintErrf("Error: %v\n", err)
|
|
}
|
|
},
|
|
}
|
|
|
|
var listTool = mcp.NewTool(
|
|
"superChatEvent-list",
|
|
mcp.WithTitleAnnotation(short),
|
|
mcp.WithDescription(long),
|
|
mcp.WithDestructiveHintAnnotation(false),
|
|
mcp.WithOpenWorldHintAnnotation(true),
|
|
mcp.WithReadOnlyHintAnnotation(true),
|
|
mcp.WithString(
|
|
"hl", mcp.DefaultString(""),
|
|
mcp.Description(hlUsage), mcp.Required(),
|
|
),
|
|
mcp.WithNumber(
|
|
"maxResults", mcp.DefaultNumber(5),
|
|
mcp.Description(cmd.MRUsage), mcp.Required(),
|
|
),
|
|
mcp.WithArray(
|
|
"parts", mcp.DefaultArray([]string{"id", "snippet"}),
|
|
mcp.Items(map[string]any{"type": "string"}),
|
|
mcp.Description(cmd.PartsUsage), mcp.Required(),
|
|
),
|
|
mcp.WithString(
|
|
"output", mcp.Enum("json", "yaml", "table"),
|
|
mcp.DefaultString("yaml"), mcp.Description(cmd.TableUsage), mcp.Required(),
|
|
),
|
|
mcp.WithString(
|
|
"jsonpath", mcp.DefaultString(""),
|
|
mcp.Description(cmd.JPUsage), mcp.Required(),
|
|
),
|
|
)
|
|
|
|
func listHandler(
|
|
ctx context.Context, request mcp.CallToolRequest,
|
|
) (*mcp.CallToolResult, error) {
|
|
args := request.GetArguments()
|
|
hl, _ = args["hl"].(string)
|
|
maxResultsRaw, _ := args["maxResults"].(float64)
|
|
maxResults = int64(maxResultsRaw)
|
|
partsRaw, _ := args["parts"].([]any)
|
|
parts = make([]string, len(partsRaw))
|
|
for i, part := range partsRaw {
|
|
parts[i] = part.(string)
|
|
}
|
|
output, _ = args["output"].(string)
|
|
jpath, _ = args["jsonpath"].(string)
|
|
|
|
slog.InfoContext(ctx, "superChatEvent list started")
|
|
|
|
var writer bytes.Buffer
|
|
err := list(&writer)
|
|
if err != nil {
|
|
slog.ErrorContext(
|
|
ctx, "superChatEvent list failed",
|
|
"error", err,
|
|
"args", args,
|
|
)
|
|
return mcp.NewToolResultError(err.Error()), err
|
|
}
|
|
slog.InfoContext(
|
|
ctx, "superChatEvent list completed successfully",
|
|
"resultSize", writer.Len(),
|
|
)
|
|
return mcp.NewToolResultText(writer.String()), nil
|
|
}
|
|
|
|
func list(writer io.Writer) error {
|
|
sc := superChatEvent.NewSuperChatEvent(
|
|
superChatEvent.WithHl(hl),
|
|
superChatEvent.WithMaxResults(maxResults),
|
|
superChatEvent.WithService(nil),
|
|
)
|
|
|
|
return sc.List(parts, output, jpath, writer)
|
|
}
|