Files
yutu/cmd/liveStream/insert.go
T

135 lines
4.5 KiB
Go

// Copyright 2026 eat-pray-ai & OpenWaygate
// SPDX-License-Identifier: Apache-2.0
package liveStream
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/liveStream"
"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 = "liveStream-insert"
insertConfirm = "Create live stream: %s"
insertShort = "Create a live stream"
insertLong = "Create a live stream. Use this tool to create a live stream."
insertExample = `# Create a live stream with RTMP at 1080p
yutu liveStream insert --title "My Stream" --ingestionType rtmp --resolution 1080p --frameRate 60fps
# Create a live stream with description
yutu liveStream insert --title "Gaming Stream" --description "Live gaming session" --ingestionType rtmp --resolution 720p`
)
var insertInSchema = &jsonschema.Schema{
Type: "object",
Required: []string{"title", "ingestion_type", "resolution"},
Properties: map[string]*jsonschema.Schema{
"title": {Type: "string", Description: titleUsage},
"description": {Type: "string", Description: descUsage},
"frame_rate": {
Type: "string", Description: frUsage,
Enum: []any{"30fps", "60fps", "variable"},
},
"ingestion_type": {
Type: "string", Description: itUsage,
Enum: []any{"rtmp", "dash", "webrtc", "hls"},
},
"resolution": {
Type: "string", Description: resUsage,
Enum: []any{"240p", "360p", "480p", "720p", "1080p", "1440p", "2160p", "variable"},
},
"on_behalf_of_content_owner": {Type: "string", Description: pkg.OBOCOUsage},
"on_behalf_of_content_owner_channel": {
Type: "string", Description: obococUsage,
},
"parts": {
Type: "array", Description: pkg.PartsUsage,
Items: &jsonschema.Schema{Type: "string"},
Default: jsontext.Value(`["snippet","cdn","status"]`),
},
"output": {
Type: "string", Enum: []any{"json", "yaml", "silent"},
Description: pkg.SilentUsage, Default: jsontext.Value(`"yaml"`),
},
},
}
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 liveStream.LiveStream) string {
return fmt.Sprintf(insertConfirm, input.Title)
},
func(input liveStream.LiveStream, w io.Writer) error {
return input.Insert(w)
},
),
),
)
liveStreamCmd.AddCommand(insertCmd)
insertCmd.Flags().StringVarP(&title, "title", "t", "", titleUsage)
insertCmd.Flags().StringVarP(&description, "description", "d", "", descUsage)
insertCmd.Flags().StringVarP(&frameRate, "frameRate", "f", "", frUsage)
insertCmd.Flags().StringVarP(&ingestionType, "ingestionType", "I", "", itUsage)
insertCmd.Flags().StringVarP(&resolution, "resolution", "r", "", resUsage)
insertCmd.Flags().StringVarP(
&onBehalfOfContentOwner, "onBehalfOfContentOwner", "b", "", pkg.OBOCOUsage,
)
insertCmd.Flags().StringVarP(
&onBehalfOfContentOwnerChannel, "onBehalfOfContentOwnerChannel", "B", "",
obococUsage,
)
insertCmd.Flags().StringSliceVarP(
&parts, "parts", "p", []string{"snippet", "cdn", "status"}, pkg.PartsUsage,
)
insertCmd.Flags().StringP("output", "o", "", pkg.SilentUsage)
insertCmd.Flags().Bool("yes", false, pkg.ConfirmedUsage)
_ = insertCmd.MarkFlagRequired("title")
_ = insertCmd.MarkFlagRequired("ingestionType")
_ = insertCmd.MarkFlagRequired("resolution")
}
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 := liveStream.NewLiveStream(
liveStream.WithTitle(title),
liveStream.WithDescription(description),
liveStream.WithFrameRate(frameRate),
liveStream.WithIngestionType(ingestionType),
liveStream.WithResolution(resolution),
liveStream.WithOnBehalfOfContentOwner(onBehalfOfContentOwner),
liveStream.WithOnBehalfOfContentOwnerChannel(onBehalfOfContentOwnerChannel),
liveStream.WithParts(parts),
liveStream.WithOutput(output),
)
utils.HandleCmdError(input.Insert(c.OutOrStdout()), c)
},
}