140 lines
3.6 KiB
Go
140 lines
3.6 KiB
Go
package caption
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"io"
|
|
"log/slog"
|
|
|
|
"github.com/eat-pray-ai/yutu/cmd"
|
|
"github.com/eat-pray-ai/yutu/pkg/caption"
|
|
"github.com/mark3labs/mcp-go/mcp"
|
|
"github.com/spf13/cobra"
|
|
)
|
|
|
|
const (
|
|
listShort = "List captions"
|
|
listLong = "List captions of a video"
|
|
listIdsUsage = "IDs of the captions to list"
|
|
)
|
|
|
|
func init() {
|
|
cmd.MCP.AddTool(listTool, listHandler)
|
|
captionCmd.AddCommand(listCmd)
|
|
|
|
listCmd.Flags().StringSliceVarP(&ids, "ids", "i", []string{}, listIdsUsage)
|
|
listCmd.Flags().StringVarP(&videoId, "videoId", "v", "", vidUsage)
|
|
listCmd.Flags().StringVarP(&onBehalfOf, "onBehalfOf", "b", "", "")
|
|
listCmd.Flags().StringVarP(
|
|
&onBehalfOfContentOwner, "onBehalfOfContentOwner", "B", "", "",
|
|
)
|
|
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: listShort,
|
|
Long: listLong,
|
|
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(
|
|
"caption-list",
|
|
mcp.WithTitleAnnotation(listShort),
|
|
mcp.WithDescription(listLong),
|
|
mcp.WithDestructiveHintAnnotation(false),
|
|
mcp.WithOpenWorldHintAnnotation(true),
|
|
mcp.WithReadOnlyHintAnnotation(true),
|
|
mcp.WithArray(
|
|
"ids", mcp.DefaultArray([]string{}),
|
|
mcp.Items(map[string]any{"type": "string"}),
|
|
mcp.Description(listIdsUsage), mcp.Required(),
|
|
),
|
|
mcp.WithString(
|
|
"videoId", mcp.DefaultString(""),
|
|
mcp.Description(vidUsage), mcp.Required(),
|
|
),
|
|
mcp.WithString(
|
|
"onBehalfOf", mcp.DefaultString(""),
|
|
mcp.Description(""), mcp.Required(),
|
|
),
|
|
mcp.WithString(
|
|
"onBehalfOfContentOwner", mcp.DefaultString(""),
|
|
mcp.Description(""), 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()
|
|
idsRaw, _ := args["ids"].([]any)
|
|
ids := make([]string, len(idsRaw))
|
|
for i, id := range idsRaw {
|
|
ids[i] = id.(string)
|
|
}
|
|
videoId, _ = args["videoId"].(string)
|
|
onBehalfOf, _ = args["onBehalfOf"].(string)
|
|
onBehalfOfContentOwner, _ = args["onBehalfOfContentOwner"].(string)
|
|
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, "caption list started")
|
|
|
|
var writer bytes.Buffer
|
|
err := list(&writer)
|
|
if err != nil {
|
|
slog.ErrorContext(
|
|
ctx, "caption list failed",
|
|
"error", err,
|
|
"args", args,
|
|
)
|
|
return mcp.NewToolResultError(err.Error()), err
|
|
}
|
|
slog.InfoContext(
|
|
ctx, "caption list completed successfully",
|
|
"resultSize", writer.Len(),
|
|
)
|
|
return mcp.NewToolResultText(writer.String()), nil
|
|
}
|
|
|
|
func list(writer io.Writer) error {
|
|
c := caption.NewCation(
|
|
caption.WithIDs(ids),
|
|
caption.WithVideoId(videoId),
|
|
caption.WithOnBehalfOf(onBehalfOf),
|
|
caption.WithOnBehalfOfContentOwner(onBehalfOfContentOwner),
|
|
caption.WithService(nil),
|
|
)
|
|
|
|
return c.List(parts, output, jpath, writer)
|
|
}
|