146 lines
4.0 KiB
Go
146 lines
4.0 KiB
Go
package playlist
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"io"
|
|
"log/slog"
|
|
|
|
"github.com/eat-pray-ai/yutu/cmd"
|
|
"github.com/eat-pray-ai/yutu/pkg/playlist"
|
|
"github.com/mark3labs/mcp-go/mcp"
|
|
"github.com/spf13/cobra"
|
|
)
|
|
|
|
const (
|
|
updateShort = "Update an existing playlist"
|
|
updateLong = "Update an existing playlist, with the specified title, description, tags, etc"
|
|
updateIdUsage = "ID of the playlist to update"
|
|
)
|
|
|
|
func init() {
|
|
cmd.MCP.AddTool(updateTool, updateHandler)
|
|
playlistCmd.AddCommand(updateCmd)
|
|
|
|
updateCmd.Flags().StringSliceVarP(&ids, "id", "i", []string{}, updateIdUsage)
|
|
updateCmd.Flags().StringVarP(&title, "title", "t", "", titleUsage)
|
|
updateCmd.Flags().StringVarP(&description, "description", "d", "", descUsage)
|
|
updateCmd.Flags().StringSliceVarP(&tags, "tags", "a", []string{}, tagsUsage)
|
|
updateCmd.Flags().StringVarP(&language, "language", "l", "", languageUsage)
|
|
updateCmd.Flags().StringVarP(&privacy, "privacy", "p", "", privacyUsage)
|
|
updateCmd.Flags().StringVarP(&output, "output", "o", "", cmd.SilentUsage)
|
|
updateCmd.Flags().StringVarP(&jpath, "jsonPath", "j", "", cmd.JPUsage)
|
|
|
|
_ = updateCmd.MarkFlagRequired("id")
|
|
}
|
|
|
|
var updateCmd = &cobra.Command{
|
|
Use: "update",
|
|
Short: updateShort,
|
|
Long: updateLong,
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
err := update(cmd.OutOrStdout())
|
|
if err != nil {
|
|
_ = cmd.Help()
|
|
cmd.PrintErrf("Error: %v\n", err)
|
|
}
|
|
},
|
|
}
|
|
|
|
var updateTool = mcp.NewTool(
|
|
"playlist-update",
|
|
mcp.WithTitleAnnotation(updateShort),
|
|
mcp.WithDescription(updateLong),
|
|
mcp.WithDestructiveHintAnnotation(false),
|
|
mcp.WithOpenWorldHintAnnotation(true),
|
|
mcp.WithReadOnlyHintAnnotation(false),
|
|
mcp.WithArray(
|
|
"ids", mcp.DefaultArray([]string{}),
|
|
mcp.Items(map[string]any{"type": "string"}),
|
|
mcp.Description(updateIdUsage), mcp.Required(),
|
|
),
|
|
mcp.WithString(
|
|
"title", mcp.DefaultString(""),
|
|
mcp.Description(titleUsage), mcp.Required(),
|
|
),
|
|
mcp.WithString(
|
|
"description", mcp.DefaultString(""),
|
|
mcp.Description(descUsage), mcp.Required(),
|
|
),
|
|
mcp.WithArray(
|
|
"tags", mcp.DefaultArray([]string{}),
|
|
mcp.Items(map[string]any{"type": "string"}),
|
|
mcp.Description(tagsUsage), mcp.Required(),
|
|
),
|
|
mcp.WithString(
|
|
"language", mcp.DefaultString(""),
|
|
mcp.Description(languageUsage), mcp.Required(),
|
|
),
|
|
mcp.WithString(
|
|
"privacy", mcp.Enum("public", "private", "unlisted"),
|
|
mcp.DefaultString(""), mcp.Description(privacyUsage), mcp.Required(),
|
|
),
|
|
mcp.WithString(
|
|
"output", mcp.Enum("json", "yaml", "silent", ""),
|
|
mcp.DefaultString("yaml"), mcp.Description(cmd.SilentUsage), mcp.Required(),
|
|
),
|
|
mcp.WithString(
|
|
"jsonpath", mcp.DefaultString(""),
|
|
mcp.Description(cmd.JPUsage), mcp.Required(),
|
|
),
|
|
)
|
|
|
|
func updateHandler(
|
|
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)
|
|
}
|
|
title, _ = args["title"].(string)
|
|
description, _ = args["description"].(string)
|
|
tagsRaw, _ := args["tags"].([]any)
|
|
tags = make([]string, len(tagsRaw))
|
|
for i, tag := range tagsRaw {
|
|
tags[i] = tag.(string)
|
|
}
|
|
language, _ = args["language"].(string)
|
|
privacy, _ = args["privacy"].(string)
|
|
output, _ = args["output"].(string)
|
|
jpath, _ = args["jsonpath"].(string)
|
|
|
|
slog.InfoContext(ctx, "playlist update started")
|
|
|
|
var writer bytes.Buffer
|
|
err := update(&writer)
|
|
if err != nil {
|
|
slog.ErrorContext(
|
|
ctx, "playlist update failed",
|
|
"error", err,
|
|
"args", args,
|
|
)
|
|
return mcp.NewToolResultError(err.Error()), err
|
|
}
|
|
slog.InfoContext(
|
|
ctx, "playlist update completed successfully",
|
|
"resultSize", writer.Len(),
|
|
)
|
|
return mcp.NewToolResultText(writer.String()), nil
|
|
}
|
|
|
|
func update(writer io.Writer) error {
|
|
p := playlist.NewPlaylist(
|
|
playlist.WithIDs(ids),
|
|
playlist.WithTitle(title),
|
|
playlist.WithDescription(description),
|
|
playlist.WithTags(tags),
|
|
playlist.WithLanguage(language),
|
|
playlist.WithPrivacy(privacy),
|
|
playlist.WithService(nil),
|
|
)
|
|
|
|
return p.Update(output, jpath, writer)
|
|
}
|