116 lines
3.9 KiB
Go
116 lines
3.9 KiB
Go
// Copyright 2025 eat-pray-ai & OpenWaygate
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
package playlist
|
|
|
|
import (
|
|
"encoding/json/jsontext"
|
|
"fmt"
|
|
"io"
|
|
|
|
cobramcp "github.com/eat-pray-ai/cobra-mcp"
|
|
"github.com/eat-pray-ai/yutu/cmd"
|
|
"github.com/eat-pray-ai/yutu/pkg"
|
|
"github.com/eat-pray-ai/yutu/pkg/playlist"
|
|
"github.com/eat-pray-ai/yutu/pkg/utils"
|
|
"github.com/google/jsonschema-go/jsonschema"
|
|
"github.com/modelcontextprotocol/go-sdk/mcp"
|
|
"github.com/spf13/cobra"
|
|
)
|
|
|
|
const (
|
|
insertTool = "playlist-insert"
|
|
insertCidUsage = "Channel id of the playlist"
|
|
insertConfirm = "Create playlist: %s"
|
|
insertShort = "Create a playlist"
|
|
insertLong = "Create a playlist. Use this tool to create a playlist."
|
|
insertExample = `# Create a public playlist
|
|
yutu playlist insert --title 'My Playlist' --channelId UC_x5XG1OV2P6uZZ5FSM9Ttw --privacy public
|
|
# Create a private playlist with description
|
|
yutu playlist insert --title 'Tutorial Series' --channelId UC_x5X --privacy private --description 'My tutorials'
|
|
# Create an unlisted playlist with tags
|
|
yutu playlist insert --title 'Music' --channelId UC_x5X --privacy unlisted --tags 'music,pop'`
|
|
)
|
|
|
|
var insertInSchema = &jsonschema.Schema{
|
|
Type: "object",
|
|
Required: []string{"title", "channel_id", "privacy"},
|
|
Properties: map[string]*jsonschema.Schema{
|
|
"title": {Type: "string", Description: titleUsage},
|
|
"description": {Type: "string", Description: descUsage},
|
|
"tags": {
|
|
Type: "array", Description: tagsUsage,
|
|
Items: &jsonschema.Schema{Type: "string"},
|
|
},
|
|
"language": {Type: "string", Description: languageUsage},
|
|
"channel_id": {Type: "string", Description: insertCidUsage},
|
|
"privacy": {
|
|
Type: "string", Description: privacyUsage,
|
|
Enum: []any{"public", "private", "unlisted"},
|
|
},
|
|
"output": {
|
|
Type: "string", Enum: []any{"json", "yaml", "silent"},
|
|
Description: pkg.SilentUsage, Default: jsontext.Value(`"json"`),
|
|
},
|
|
},
|
|
}
|
|
|
|
func init() {
|
|
mcp.AddTool(
|
|
cmd.Server, &mcp.Tool{
|
|
Name: insertTool, Title: insertShort, Description: insertLong,
|
|
InputSchema: insertInSchema, Annotations: &mcp.ToolAnnotations{
|
|
DestructiveHint: new(false),
|
|
IdempotentHint: false,
|
|
OpenWorldHint: new(true),
|
|
ReadOnlyHint: false,
|
|
},
|
|
}, cobramcp.GenToolHandlerWithMRTR(
|
|
insertTool, cobramcp.ConfirmThen(
|
|
func(input playlist.Playlist) string {
|
|
return fmt.Sprintf(insertConfirm, input.Title)
|
|
},
|
|
func(input playlist.Playlist, w io.Writer) error {
|
|
return input.Insert(w)
|
|
},
|
|
),
|
|
),
|
|
)
|
|
playlistCmd.AddCommand(insertCmd)
|
|
|
|
insertCmd.Flags().StringVarP(&title, "title", "t", "", titleUsage)
|
|
insertCmd.Flags().StringVarP(description, "description", "d", "", descUsage)
|
|
insertCmd.Flags().StringSliceVarP(&tags, "tags", "a", []string{}, tagsUsage)
|
|
insertCmd.Flags().StringVarP(language, "language", "l", "", languageUsage)
|
|
insertCmd.Flags().StringVarP(&channelId, "channelId", "c", "", insertCidUsage)
|
|
insertCmd.Flags().StringVarP(&privacy, "privacy", "p", "", privacyUsage)
|
|
insertCmd.Flags().StringP("output", "o", "", pkg.SilentUsage)
|
|
insertCmd.Flags().Bool("yes", false, pkg.ConfirmedUsage)
|
|
_ = insertCmd.MarkFlagRequired("title")
|
|
_ = insertCmd.MarkFlagRequired("channelId")
|
|
_ = insertCmd.MarkFlagRequired("privacy")
|
|
}
|
|
|
|
var insertCmd = &cobra.Command{
|
|
Use: "insert",
|
|
Short: insertShort,
|
|
Long: insertLong,
|
|
Example: insertExample,
|
|
PreRunE: func(c *cobra.Command, _ []string) error {
|
|
return utils.ConfirmPreRun(c, fmt.Sprintf(insertConfirm, title))
|
|
},
|
|
Run: func(c *cobra.Command, _ []string) {
|
|
output, _ := c.Flags().GetString("output")
|
|
input := playlist.NewPlaylist(
|
|
playlist.WithTitle(title),
|
|
playlist.WithDescription(description),
|
|
playlist.WithTags(tags),
|
|
playlist.WithLanguage(language),
|
|
playlist.WithChannelId(channelId),
|
|
playlist.WithPrivacy(privacy),
|
|
playlist.WithOutput(output),
|
|
)
|
|
utils.HandleCmdError(input.Insert(c.OutOrStdout()), c)
|
|
},
|
|
}
|