Files

115 lines
3.3 KiB
Go

// Copyright 2025 eat-pray-ai & OpenWaygate
// SPDX-License-Identifier: Apache-2.0
package comment
import (
"encoding/json/jsontext"
"fmt"
"io"
"strings"
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/comment"
"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 (
updateTool = "comment-update"
updateIdUsage = "ID of the comment"
updateConfirm = "Update comment: %s"
updateShort = "Update a comment"
updateLong = "Update a comment. Use this tool to update a comment."
updateExample = `# Update comment text
yutu comment update --id abc123 --textOriginal 'Updated comment'
# Like a comment
yutu comment update --id abc123 --viewerRating like
# Update comment text with rating enabled
yutu comment update --id abc123 --textOriginal 'New text' --canRate`
)
var updateInSchema = &jsonschema.Schema{
Type: "object",
Required: []string{"ids"},
Properties: map[string]*jsonschema.Schema{
"ids": {
Type: "array", Description: updateIdUsage,
Items: &jsonschema.Schema{Type: "string"},
},
"can_rate": {Type: "boolean", Description: crUsage},
"text_original": {Type: "string", Description: toUsage},
"viewer_rating": {
Type: "string", Description: vrUsage,
Enum: []any{"none", "like", "dislike"},
},
"output": {
Type: "string", Enum: []any{"json", "yaml", "silent"},
Description: pkg.SilentUsage, Default: jsontext.Value(`"json"`),
},
},
}
func init() {
mcp.AddTool(
cmd.Server, &mcp.Tool{
Name: updateTool, Title: updateShort, Description: updateLong,
InputSchema: updateInSchema, Annotations: &mcp.ToolAnnotations{
DestructiveHint: new(false),
IdempotentHint: false,
OpenWorldHint: new(true),
ReadOnlyHint: false,
},
}, cobramcp.GenToolHandlerWithMRTR(
updateTool, cobramcp.ConfirmThen(
func(input comment.Comment) string {
return fmt.Sprintf(updateConfirm, strings.Join(input.Ids, ", "))
},
func(input comment.Comment, w io.Writer) error {
return input.Update(w)
},
),
),
)
commentCmd.AddCommand(updateCmd)
updateCmd.Flags().StringSliceVarP(&ids, "id", "i", []string{}, updateIdUsage)
updateCmd.Flags().BoolVarP(canRate, "canRate", "R", false, crUsage)
updateCmd.Flags().StringVarP(
&textOriginal, "textOriginal", "t", "", toUsage,
)
updateCmd.Flags().StringVarP(
&viewerRating, "viewerRating", "r", "", vrUsage,
)
updateCmd.Flags().StringP("output", "o", "", pkg.SilentUsage)
updateCmd.Flags().Bool("yes", false, pkg.ConfirmedUsage)
_ = updateCmd.MarkFlagRequired("id")
}
var updateCmd = &cobra.Command{
Use: "update",
Short: updateShort,
Long: updateLong,
Example: updateExample,
PreRunE: func(c *cobra.Command, _ []string) error {
return utils.ConfirmPreRun(
c, fmt.Sprintf(updateConfirm, strings.Join(ids, ", ")),
)
},
Run: func(c *cobra.Command, _ []string) {
output, _ := c.Flags().GetString("output")
input := comment.NewComment(
comment.WithIds(ids),
comment.WithCanRate(canRate),
comment.WithTextOriginal(textOriginal),
comment.WithViewerRating(viewerRating),
comment.WithOutput(output),
)
utils.HandleCmdError(input.Update(c.OutOrStdout()), c)
},
}