* feat: 实现小红书标签输入和自动关联功能 - 在 PublishImageContent 结构体中添加 Tags 字段 - 实现 inputTags 函数处理多个标签输入 - 实现 inputTag 函数自动点击标签联想下拉框 - 通过 #creator-editor-topic-container 选择器定位标签下拉框 - 自动点击第一个 .item 元素完成标签关联 - 添加错误处理和日志记录 * feat: 为 MCP 和 HTTP API 添加标签(tags)支持 - 在 PublishRequest 结构体中添加 Tags 字段 - 更新 MCP handler 处理标签参数 - 更新 MCP 工具定义,添加 tags 参数说明 - HTTP API 自动支持 tags 字段(通过 PublishRequest) - 保持向后兼容,tags 为可选参数
297 lines
6.6 KiB
Go
297 lines
6.6 KiB
Go
package main
|
||
|
||
import (
|
||
"context"
|
||
"encoding/json"
|
||
"fmt"
|
||
|
||
"github.com/sirupsen/logrus"
|
||
)
|
||
|
||
// MCP 工具处理函数
|
||
|
||
// handleCheckLoginStatus 处理检查登录状态
|
||
func (s *AppServer) handleCheckLoginStatus(ctx context.Context) *MCPToolResult {
|
||
logrus.Info("MCP: 检查登录状态")
|
||
|
||
status, err := s.xiaohongshuService.CheckLoginStatus(ctx)
|
||
if err != nil {
|
||
return &MCPToolResult{
|
||
Content: []MCPContent{{
|
||
Type: "text",
|
||
Text: "检查登录状态失败: " + err.Error(),
|
||
}},
|
||
IsError: true,
|
||
}
|
||
}
|
||
|
||
resultText := fmt.Sprintf("登录状态检查成功: %+v", status)
|
||
return &MCPToolResult{
|
||
Content: []MCPContent{{
|
||
Type: "text",
|
||
Text: resultText,
|
||
}},
|
||
}
|
||
}
|
||
|
||
// handlePublishContent 处理发布内容
|
||
func (s *AppServer) handlePublishContent(ctx context.Context, args map[string]interface{}) *MCPToolResult {
|
||
logrus.Info("MCP: 发布内容")
|
||
|
||
// 解析参数
|
||
title, _ := args["title"].(string)
|
||
content, _ := args["content"].(string)
|
||
imagePathsInterface, _ := args["images"].([]interface{})
|
||
tagsInterface, _ := args["tags"].([]interface{})
|
||
|
||
var imagePaths []string
|
||
for _, path := range imagePathsInterface {
|
||
if pathStr, ok := path.(string); ok {
|
||
imagePaths = append(imagePaths, pathStr)
|
||
}
|
||
}
|
||
|
||
var tags []string
|
||
for _, tag := range tagsInterface {
|
||
if tagStr, ok := tag.(string); ok {
|
||
tags = append(tags, tagStr)
|
||
}
|
||
}
|
||
|
||
logrus.Infof("MCP: 发布内容 - 标题: %s, 图片数量: %d, 标签数量: %d", title, len(imagePaths), len(tags))
|
||
|
||
// 构建发布请求
|
||
req := &PublishRequest{
|
||
Title: title,
|
||
Content: content,
|
||
Images: imagePaths,
|
||
Tags: tags,
|
||
}
|
||
|
||
// 执行发布
|
||
result, err := s.xiaohongshuService.PublishContent(ctx, req)
|
||
if err != nil {
|
||
return &MCPToolResult{
|
||
Content: []MCPContent{{
|
||
Type: "text",
|
||
Text: "发布失败: " + err.Error(),
|
||
}},
|
||
IsError: true,
|
||
}
|
||
}
|
||
|
||
resultText := fmt.Sprintf("内容发布成功: %+v", result)
|
||
return &MCPToolResult{
|
||
Content: []MCPContent{{
|
||
Type: "text",
|
||
Text: resultText,
|
||
}},
|
||
}
|
||
}
|
||
|
||
// handleListFeeds 处理获取Feeds列表
|
||
func (s *AppServer) handleListFeeds(ctx context.Context) *MCPToolResult {
|
||
logrus.Info("MCP: 获取Feeds列表")
|
||
|
||
result, err := s.xiaohongshuService.ListFeeds(ctx)
|
||
if err != nil {
|
||
return &MCPToolResult{
|
||
Content: []MCPContent{{
|
||
Type: "text",
|
||
Text: "获取Feeds列表失败: " + err.Error(),
|
||
}},
|
||
IsError: true,
|
||
}
|
||
}
|
||
|
||
// 格式化输出,转换为JSON字符串
|
||
jsonData, err := json.MarshalIndent(result, "", " ")
|
||
if err != nil {
|
||
return &MCPToolResult{
|
||
Content: []MCPContent{{
|
||
Type: "text",
|
||
Text: fmt.Sprintf("获取Feeds列表成功,但序列化失败: %v", err),
|
||
}},
|
||
IsError: true,
|
||
}
|
||
}
|
||
|
||
return &MCPToolResult{
|
||
Content: []MCPContent{{
|
||
Type: "text",
|
||
Text: string(jsonData),
|
||
}},
|
||
}
|
||
}
|
||
|
||
// handleSearchFeeds 处理搜索Feeds
|
||
func (s *AppServer) handleSearchFeeds(ctx context.Context, args map[string]interface{}) *MCPToolResult {
|
||
logrus.Info("MCP: 搜索Feeds")
|
||
|
||
// 解析参数
|
||
keyword, ok := args["keyword"].(string)
|
||
if !ok || keyword == "" {
|
||
return &MCPToolResult{
|
||
Content: []MCPContent{{
|
||
Type: "text",
|
||
Text: "搜索Feeds失败: 缺少关键词参数",
|
||
}},
|
||
IsError: true,
|
||
}
|
||
}
|
||
|
||
logrus.Infof("MCP: 搜索Feeds - 关键词: %s", keyword)
|
||
|
||
result, err := s.xiaohongshuService.SearchFeeds(ctx, keyword)
|
||
if err != nil {
|
||
return &MCPToolResult{
|
||
Content: []MCPContent{{
|
||
Type: "text",
|
||
Text: "搜索Feeds失败: " + err.Error(),
|
||
}},
|
||
IsError: true,
|
||
}
|
||
}
|
||
|
||
// 格式化输出,转换为JSON字符串
|
||
jsonData, err := json.MarshalIndent(result, "", " ")
|
||
if err != nil {
|
||
return &MCPToolResult{
|
||
Content: []MCPContent{{
|
||
Type: "text",
|
||
Text: fmt.Sprintf("搜索Feeds成功,但序列化失败: %v", err),
|
||
}},
|
||
IsError: true,
|
||
}
|
||
}
|
||
|
||
return &MCPToolResult{
|
||
Content: []MCPContent{{
|
||
Type: "text",
|
||
Text: string(jsonData),
|
||
}},
|
||
}
|
||
}
|
||
|
||
// handleGetFeedDetail 处理获取Feed详情
|
||
func (s *AppServer) handleGetFeedDetail(ctx context.Context, args map[string]any) *MCPToolResult {
|
||
logrus.Info("MCP: 获取Feed详情")
|
||
|
||
// 解析参数
|
||
feedID, ok := args["feed_id"].(string)
|
||
if !ok || feedID == "" {
|
||
return &MCPToolResult{
|
||
Content: []MCPContent{{
|
||
Type: "text",
|
||
Text: "获取Feed详情失败: 缺少feed_id参数",
|
||
}},
|
||
IsError: true,
|
||
}
|
||
}
|
||
|
||
xsecToken, ok := args["xsec_token"].(string)
|
||
if !ok || xsecToken == "" {
|
||
return &MCPToolResult{
|
||
Content: []MCPContent{{
|
||
Type: "text",
|
||
Text: "获取Feed详情失败: 缺少xsec_token参数",
|
||
}},
|
||
IsError: true,
|
||
}
|
||
}
|
||
|
||
logrus.Infof("MCP: 获取Feed详情 - Feed ID: %s", feedID)
|
||
|
||
result, err := s.xiaohongshuService.GetFeedDetail(ctx, feedID, xsecToken)
|
||
if err != nil {
|
||
return &MCPToolResult{
|
||
Content: []MCPContent{{
|
||
Type: "text",
|
||
Text: "获取Feed详情失败: " + err.Error(),
|
||
}},
|
||
IsError: true,
|
||
}
|
||
}
|
||
|
||
// 格式化输出,转换为JSON字符串
|
||
jsonData, err := json.MarshalIndent(result, "", " ")
|
||
if err != nil {
|
||
return &MCPToolResult{
|
||
Content: []MCPContent{{
|
||
Type: "text",
|
||
Text: fmt.Sprintf("获取Feed详情成功,但序列化失败: %v", err),
|
||
}},
|
||
IsError: true,
|
||
}
|
||
}
|
||
|
||
return &MCPToolResult{
|
||
Content: []MCPContent{{
|
||
Type: "text",
|
||
Text: string(jsonData),
|
||
}},
|
||
}
|
||
}
|
||
|
||
// handlePostComment 处理发表评论到Feed
|
||
func (s *AppServer) handlePostComment(ctx context.Context, args map[string]interface{}) *MCPToolResult {
|
||
logrus.Info("MCP: 发表评论到Feed")
|
||
|
||
// 解析参数
|
||
feedID, ok := args["feed_id"].(string)
|
||
if !ok || feedID == "" {
|
||
return &MCPToolResult{
|
||
Content: []MCPContent{{
|
||
Type: "text",
|
||
Text: "发表评论失败: 缺少feed_id参数",
|
||
}},
|
||
IsError: true,
|
||
}
|
||
}
|
||
|
||
xsecToken, ok := args["xsec_token"].(string)
|
||
if !ok || xsecToken == "" {
|
||
return &MCPToolResult{
|
||
Content: []MCPContent{{
|
||
Type: "text",
|
||
Text: "发表评论失败: 缺少xsec_token参数",
|
||
}},
|
||
IsError: true,
|
||
}
|
||
}
|
||
|
||
content, ok := args["content"].(string)
|
||
if !ok || content == "" {
|
||
return &MCPToolResult{
|
||
Content: []MCPContent{{
|
||
Type: "text",
|
||
Text: "发表评论失败: 缺少content参数",
|
||
}},
|
||
IsError: true,
|
||
}
|
||
}
|
||
|
||
logrus.Infof("MCP: 发表评论 - Feed ID: %s, 内容长度: %d", feedID, len(content))
|
||
|
||
// 发表评论
|
||
result, err := s.xiaohongshuService.PostCommentToFeed(ctx, feedID, xsecToken, content)
|
||
if err != nil {
|
||
return &MCPToolResult{
|
||
Content: []MCPContent{{
|
||
Type: "text",
|
||
Text: "发表评论失败: " + err.Error(),
|
||
}},
|
||
IsError: true,
|
||
}
|
||
}
|
||
|
||
// 返回成功结果,只包含feed_id
|
||
resultText := fmt.Sprintf("评论发表成功 - Feed ID: %s", result.FeedID)
|
||
return &MCPToolResult{
|
||
Content: []MCPContent{{
|
||
Type: "text",
|
||
Text: resultText,
|
||
}},
|
||
}
|
||
}
|