Compare commits
85
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7849116a69 | ||
|
|
b082135e6e | ||
|
|
9ae0191270 | ||
|
|
211a5fa393 | ||
|
|
af199e73e2 | ||
|
|
fd24619437 | ||
|
|
6337058d15 | ||
|
|
d38868c8aa | ||
|
|
de8040ecc2 | ||
|
|
96f406be6b | ||
|
|
0d99d18acc | ||
|
|
9377abc5f6 | ||
|
|
e8ca78fe49 | ||
|
|
b923f522d5 | ||
|
|
28bc577e88 | ||
|
|
82dfee7291 | ||
|
|
1d2edbaa9f | ||
|
|
25a5f5b7d2 | ||
|
|
0b43905697 | ||
|
|
28227b19c7 | ||
|
|
622632908e | ||
|
|
2ec25ebb98 | ||
|
|
bccc9eb056 | ||
|
|
2274fd96f0 | ||
|
|
d3e444cb56 | ||
|
|
0148ad1800 | ||
|
|
e38fd9ab93 | ||
|
|
fb33a0b9e0 | ||
|
|
e94c7063ed | ||
|
|
d6b51a04f4 | ||
|
|
cd3a09e153 | ||
|
|
2f925d29fd | ||
|
|
68483f05b2 | ||
|
|
6eb3efa065 | ||
|
|
783e1eeef9 | ||
|
|
9e0a67f728 | ||
|
|
19f9285f8c | ||
|
|
3817ac230d | ||
|
|
75b54a9467 | ||
|
|
851d491d2a | ||
|
|
b55f243780 | ||
|
|
257ac94fb1 | ||
|
|
9834a84888 | ||
|
|
a68c06540c | ||
|
|
44c5ef13b4 | ||
|
|
b70e109e89 | ||
|
|
3fc144a699 | ||
|
|
4d274c9da3 | ||
|
|
7ffb48c9ae | ||
|
|
a24fd542c0 | ||
|
|
b8418b6a5f | ||
|
|
8c19b0048b | ||
|
|
a9751fa74d | ||
|
|
8a0bd34e13 | ||
|
|
5a160cefd8 | ||
|
|
08ee5dc573 | ||
|
|
5c45bd57da | ||
|
|
349537e336 | ||
|
|
b243b38d65 | ||
|
|
819355b31f | ||
|
|
c3d4de52a7 | ||
|
|
f478b7d3e1 | ||
|
|
d84c73e8b2 | ||
|
|
431f64be85 | ||
|
|
d979d86fa3 | ||
|
|
b150911da9 | ||
|
|
37d6a4ea2e | ||
|
|
20c8e0dfec | ||
|
|
cde050f146 | ||
|
|
e02410dae6 | ||
|
|
74d31566ff | ||
|
|
6765a74d83 | ||
|
|
13e5914638 | ||
|
|
8fcc6baee0 | ||
|
|
6774d423b7 | ||
|
|
8156528c05 | ||
|
|
e5a60386c6 | ||
|
|
06b0a9eef3 | ||
|
|
83f72377a7 | ||
|
|
50712d3305 | ||
|
|
f79a6fc707 | ||
|
|
5a09204bf5 | ||
|
|
0d11b2be45 | ||
|
|
effe7c829e | ||
|
|
7c76dfea4b |
@@ -8,11 +8,30 @@ The format is inspired by [Keep a Changelog](https://keepachangelog.com/) and th
|
||||
|
||||
### Added
|
||||
|
||||
- **Aitable workflow execution and history** — adds `dws aitable workflow run` for confirmed asynchronous execution of scheduled or record-triggered workflows, plus `dws aitable workflow history` for status-, time-, and page-filtered execution records. The commands map directly to `aitable/run_workflow` and `aitable/get_flow_record_list`, validate trigger-specific arguments locally, and document the `executionId` / `instanceId` correlation.
|
||||
- **Streaming-card mentions** — `chat +messages-send-card` now accepts
|
||||
`--at-open-dingtalk-ids` and `--at-all` for group cards, passing mention
|
||||
targets to the initial card-creation request and prepending its returned
|
||||
`atTag` to the automatic streaming update.
|
||||
|
||||
### Fixed
|
||||
|
||||
- **Machine-readable export and download receipts** — `dws doc export`,
|
||||
`dws drive download`, and `dws drive download --version` now keep progress
|
||||
logs on stderr under `--format json` and emit one JSON result on stdout after
|
||||
a successful local write. The result includes the saved path and byte size;
|
||||
document exports additionally report the node, requested format, job/task
|
||||
ID, and final status.
|
||||
### Changed
|
||||
|
||||
- **Minutes `permission apply --policy` type** — `--policy` is now declared as
|
||||
an `int` flag and its required check uses `Flags().Changed`, matching the
|
||||
numeric-parameter convention. `--help` reports `int` instead of `string`;
|
||||
accepted values (2/3/4) and gateway behavior are unchanged.
|
||||
- **Minutes skill references** — document `permission apply` in both Minutes
|
||||
skill references: list it in the command trees, describe its policy values and
|
||||
how it differs from `permission add`, and add its intent routing.
|
||||
|
||||
## [1.0.58-beta.2] - 2026-08-10
|
||||
|
||||
### Added
|
||||
|
||||
@@ -0,0 +1,84 @@
|
||||
// Copyright 2026 Alibaba Group
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
|
||||
package app
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/DingTalk-Real-AI/dingtalk-workspace-cli/internal/shortcut/chatmsg"
|
||||
)
|
||||
|
||||
func TestCrossPlatformCoverageChatMessageReceiptActionsBindToRunnableCommands(t *testing.T) {
|
||||
testCases := []struct {
|
||||
name string
|
||||
payload map[string]any
|
||||
}{
|
||||
{
|
||||
name: "send receipt awaiting status",
|
||||
payload: chatmsg.ProjectMessageSendReceipt(map[string]any{
|
||||
"openTaskId": "task-pending",
|
||||
}),
|
||||
},
|
||||
{
|
||||
name: "send receipt ready for message actions",
|
||||
payload: chatmsg.ProjectMessageSendReceipt(map[string]any{
|
||||
"openTaskId": "task-ready",
|
||||
"openMessageId": "message-ready",
|
||||
"openConversationId": "conversation-ready",
|
||||
}),
|
||||
},
|
||||
{
|
||||
name: "send status awaiting message reference",
|
||||
payload: chatmsg.ProjectMessageSendStatus(map[string]any{
|
||||
"status": "PENDING",
|
||||
}, "task-pending"),
|
||||
},
|
||||
{
|
||||
name: "send status ready for message actions",
|
||||
payload: chatmsg.ProjectMessageSendStatus(map[string]any{
|
||||
"openTaskId": "task-ready",
|
||||
"openMessageId": "message-ready",
|
||||
"openConversationId": "conversation-ready",
|
||||
"status": "SUCCESS",
|
||||
}, "task-ready"),
|
||||
},
|
||||
}
|
||||
|
||||
root := NewRootCommand()
|
||||
for _, testCase := range testCases {
|
||||
t.Run(testCase.name, func(t *testing.T) {
|
||||
actions, ok := testCase.payload["nextActions"].([]map[string]any)
|
||||
if !ok || len(actions) == 0 {
|
||||
t.Fatalf("nextActions = %#v, want non-empty []map[string]any", testCase.payload["nextActions"])
|
||||
}
|
||||
|
||||
for index, action := range actions {
|
||||
cliPath, ok := action["cliPath"].(string)
|
||||
if !ok || strings.TrimSpace(cliPath) == "" {
|
||||
t.Fatalf("nextActions[%d].cliPath = %#v, want non-empty string", index, action["cliPath"])
|
||||
}
|
||||
|
||||
command, remaining, err := root.Find(strings.Fields(cliPath))
|
||||
if err != nil {
|
||||
t.Fatalf("nextActions[%d].cliPath %q does not bind: %v", index, cliPath, err)
|
||||
}
|
||||
if command == nil || len(remaining) != 0 || !command.Runnable() {
|
||||
t.Fatalf("nextActions[%d].cliPath %q resolved to command=%v remaining=%v runnable=%v", index, cliPath, command, remaining, command != nil && command.Runnable())
|
||||
}
|
||||
|
||||
arguments, ok := action["arguments"].(map[string]any)
|
||||
if !ok {
|
||||
t.Fatalf("nextActions[%d].arguments = %#v, want map[string]any", index, action["arguments"])
|
||||
}
|
||||
for name := range arguments {
|
||||
if command.Flags().Lookup(name) == nil && command.InheritedFlags().Lookup(name) == nil {
|
||||
t.Errorf("nextActions[%d] argument %q is not a flag of runnable command %q", index, name, cliPath)
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -26,6 +26,14 @@ func TestReviewedMutationSafetyReachesFinalSchema(t *testing.T) {
|
||||
wants := []finalSchemaSafetyWant{
|
||||
{canonical: "aitable.form_field_hide", effect: "write", risk: "medium", confirmation: "not_required", idempotency: "idempotent", provenance: declared},
|
||||
{canonical: "chat.dismiss_group", effect: "destructive", risk: "high", confirmation: "user_required", idempotency: "unknown", provenance: declared},
|
||||
// Card update intentionally layers confirmation: the atomic typed command
|
||||
// preserves its original contract, while the Agent-facing shortcut owns
|
||||
// the outer confirmation boundary.
|
||||
{canonical: "chat.update_streaming_card", effect: "write", risk: "medium", confirmation: "not_required", idempotency: "unknown", provenance: declared},
|
||||
{canonical: "chat.shortcut_messages_send", effect: "write", risk: "medium", confirmation: "user_required", idempotency: "unknown", provenance: declared},
|
||||
{canonical: "chat.shortcut_messages_send_by_webhook", effect: "write", risk: "medium", confirmation: "user_required", idempotency: "unknown", provenance: declared},
|
||||
{canonical: "chat.shortcut_messages_send_card", effect: "write", risk: "medium", confirmation: "user_required", idempotency: "unknown", provenance: declared},
|
||||
{canonical: "chat.shortcut_messages_update_card", effect: "write", risk: "medium", confirmation: "user_required", idempotency: "unknown", provenance: declared},
|
||||
{canonical: "drive.recycle_restore", effect: "write", risk: "medium", confirmation: "not_required", idempotency: "unknown", provenance: declared},
|
||||
{canonical: "minutes.create_speaker_summary", effect: "write", risk: "medium", confirmation: "not_required", idempotency: "unknown", provenance: declared},
|
||||
{canonical: "sheet.clear_range", effect: "write", risk: "medium", confirmation: "user_required", idempotency: "unknown", provenance: declared},
|
||||
|
||||
@@ -81,6 +81,7 @@ func newServerFailureAPIError(
|
||||
apperrors.WithReason(fallbackReason),
|
||||
apperrors.WithServerKey(serverKey),
|
||||
apperrors.WithHint(fallbackHint),
|
||||
apperrors.WithActions("运行 dws doctor 检查登录态、网络和本地环境;持续失败时保留 Trace ID 和 Server Code"),
|
||||
apperrors.WithServerDiag(diag),
|
||||
}
|
||||
if classified, ok := classifyServerFailure(message, diag); ok {
|
||||
|
||||
@@ -95,6 +95,9 @@ func TestCrossPlatformCoverageServerFailureClassifierUnknownFallsBack(t *testing
|
||||
if typed.Reason != "business_error" || typed.Origin != "" || typed.FailureStage != "" || typed.ExecutionStarted != nil {
|
||||
t.Fatalf("unexpected fallback classification: %#v", typed)
|
||||
}
|
||||
if len(typed.Actions) == 0 || !strings.Contains(typed.Actions[0], "dws doctor") {
|
||||
t.Fatalf("fallback error has no stable troubleshooting entry: %#v", typed.Actions)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCrossPlatformCoverageServerFailureReasonUsesTypedClassification(t *testing.T) {
|
||||
|
||||
@@ -256,6 +256,10 @@ var reviewedSchemaParameterMappingExclusions = map[string]string{
|
||||
"chat.list_conversation_message_v2 --open-dingtalk-id": "selects the alternate list_individual_chat_message branch",
|
||||
"chat.list_conversation_message_v2 --user": "selects the alternate list_individual_chat_message branch",
|
||||
"chat.list_message_favorites --cursor": "Reviewed unpinned adapter: chat.list_message_favorites has no singular pinned interface_ref; --cursor is a CLI wrapper input and does not publish a direct interface property.",
|
||||
"chat.list_message_favorites --max-items": "Reviewed helper-only pagination control: --max-items caps aggregated CLI output for chat.list_message_favorites and is not sent to the remote adapter.",
|
||||
"chat.list_message_favorites --page-all": "Reviewed helper-only pagination control: --page-all enables CLI-side looping for chat.list_message_favorites and is not sent to the remote adapter.",
|
||||
"chat.list_message_favorites --page-delay": "Reviewed helper-only pagination control: --page-delay controls local delay between chat.list_message_favorites pages and is not sent to the remote adapter.",
|
||||
"chat.list_message_favorites --page-limit": "Reviewed helper-only pagination control: --page-limit caps CLI-side page fetches for chat.list_message_favorites and is not sent to the remote adapter.",
|
||||
"chat.list_message_favorites --size": "Reviewed unpinned adapter: chat.list_message_favorites has no singular pinned interface_ref; --size is a CLI wrapper input and does not publish a direct interface property.",
|
||||
"chat.query_msg_read_status --user": "conditional wrapper: parseCSVValues + appendChatIDArgs routes each supplied identifier to targetUserIds or targetOpenDingTalkIds according to its runtime ID shape; there is no single RPC property for this flag",
|
||||
"chat.query_msg_read_status --users": "conditional wrapper/alias of --user: parseCSVValues + appendChatIDArgs routes each supplied identifier to targetUserIds or targetOpenDingTalkIds according to its runtime ID shape; there is no single RPC property for this flag",
|
||||
|
||||
@@ -41,7 +41,7 @@ func TestCrossPlatformCoverageDiagnosticsAndErrorRenderingEdges(t *testing.T) {
|
||||
t.Fatalf("PrintJSON friendly diagnostics = %q, %v", out.String(), err)
|
||||
}
|
||||
out.Reset()
|
||||
if err := PrintHumanAt(&out, err, VerbosityVerbose); err != nil || !strings.Contains(out.String(), "开启地址") {
|
||||
if err := PrintHumanAt(&out, err, VerbosityVerbose); err != nil || !strings.Contains(out.String(), "处理入口") {
|
||||
t.Fatalf("PrintHuman friendly diagnostics = %q, %v", out.String(), err)
|
||||
}
|
||||
out.Reset()
|
||||
|
||||
@@ -19,6 +19,7 @@ import (
|
||||
stderrors "errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/url"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
@@ -460,7 +461,7 @@ func PrintHumanAt(w io.Writer, err error, v Verbosity) error {
|
||||
lines = append(lines, tui.Cyan("Hint: "+friendlyHint))
|
||||
}
|
||||
if actionURL != "" {
|
||||
lines = append(lines, tui.White("Action: 开启地址: "+actionURL))
|
||||
lines = append(lines, tui.White("Action: 处理入口: "+actionURL))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -540,7 +541,7 @@ func PrintHumanAt(w io.Writer, err error, v Verbosity) error {
|
||||
|
||||
func serverGuidance(diag ServerDiagnostics) (string, string) {
|
||||
friendlyHint := strings.TrimSpace(diag.FriendlyHint)
|
||||
actionURL := strings.TrimSpace(diag.ActionURL)
|
||||
actionURL := safeServerActionURL(diag.ActionURL)
|
||||
if friendlyHint == "" || actionURL == "" {
|
||||
switch diag.ServerErrorCode {
|
||||
case "TOKEN_VERIFIED_FAILED", "CLI_ORG_NOT_AUTHORIZED":
|
||||
@@ -555,6 +556,19 @@ func serverGuidance(diag ServerDiagnostics) (string, string) {
|
||||
return friendlyHint, actionURL
|
||||
}
|
||||
|
||||
func safeServerActionURL(raw string) string {
|
||||
raw = strings.TrimSpace(raw)
|
||||
if raw == "" {
|
||||
return ""
|
||||
}
|
||||
parsed, err := url.Parse(raw)
|
||||
if err != nil || !strings.EqualFold(parsed.Scheme, "https") ||
|
||||
parsed.Hostname() == "" || parsed.User != nil {
|
||||
return ""
|
||||
}
|
||||
return parsed.String()
|
||||
}
|
||||
|
||||
func category(err error) string {
|
||||
var typed *Error
|
||||
if stderrors.As(err, &typed) {
|
||||
|
||||
@@ -336,11 +336,40 @@ func TestCrossPlatformCoveragePrintHumanIncludesServerGuidance(t *testing.T) {
|
||||
if !strings.Contains(got, "Hint: 请联系管理员开通消息搜索权益") {
|
||||
t.Fatalf("expected server guidance in output, got %q", got)
|
||||
}
|
||||
if !strings.Contains(got, "Action: 开启地址: https://example.test/enable-search") {
|
||||
if !strings.Contains(got, "Action: 处理入口: https://example.test/enable-search") {
|
||||
t.Fatalf("expected server action URL in output, got %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCrossPlatformCoverageServerGuidanceSuppressesUnsafeActionURL(t *testing.T) {
|
||||
t.Parallel()
|
||||
for _, actionURL := range []string{
|
||||
"http://example.test/help",
|
||||
"javascript:alert(1)",
|
||||
"https://user:secret@example.test/help",
|
||||
"not a url",
|
||||
} {
|
||||
var human strings.Builder
|
||||
err := NewAPI("server error", WithServerDiag(ServerDiagnostics{
|
||||
FriendlyHint: "保留 Trace ID 后排查",
|
||||
ActionURL: actionURL,
|
||||
}))
|
||||
if printErr := PrintHuman(&human, err); printErr != nil {
|
||||
t.Fatal(printErr)
|
||||
}
|
||||
if strings.Contains(human.String(), actionURL) || strings.Contains(human.String(), "处理入口") {
|
||||
t.Fatalf("unsafe action URL %q leaked to human output: %q", actionURL, human.String())
|
||||
}
|
||||
var jsonOutput strings.Builder
|
||||
if printErr := PrintJSON(&jsonOutput, err); printErr != nil {
|
||||
t.Fatal(printErr)
|
||||
}
|
||||
if strings.Contains(jsonOutput.String(), `"action_url"`) {
|
||||
t.Fatalf("unsafe action URL %q leaked to JSON output: %q", actionURL, jsonOutput.String())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestCrossPlatformCoveragePrintJSONIncludesRPCCodeAndData(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
|
||||
+178
-2
@@ -16,6 +16,7 @@ import (
|
||||
"github.com/DingTalk-Real-AI/dingtalk-workspace-cli/pkg/paging"
|
||||
"github.com/spf13/cobra"
|
||||
|
||||
"github.com/DingTalk-Real-AI/dingtalk-workspace-cli/internal/corecmd"
|
||||
"github.com/DingTalk-Real-AI/dingtalk-workspace-cli/internal/corecmd/contract"
|
||||
)
|
||||
|
||||
@@ -125,6 +126,70 @@ func resolveWorkflowDSL(cmd *cobra.Command) (map[string]any, error) {
|
||||
return dsl, nil
|
||||
}
|
||||
|
||||
func validateWorkflowRunFlags(cmd *cobra.Command, _ []string) error {
|
||||
tableID, _ := cmd.Flags().GetString("table-id")
|
||||
tableID = strings.TrimSpace(tableID)
|
||||
recordIDs, _ := cmd.Flags().GetStringSlice("record-ids")
|
||||
cleaned := make([]string, 0, len(recordIDs))
|
||||
seen := make(map[string]struct{}, len(recordIDs))
|
||||
for _, recordID := range recordIDs {
|
||||
recordID = strings.TrimSpace(recordID)
|
||||
if recordID == "" {
|
||||
continue
|
||||
}
|
||||
if _, ok := seen[recordID]; ok {
|
||||
return apperrors.NewValidation(fmt.Sprintf("--record-ids 不能包含重复值 %q", recordID))
|
||||
}
|
||||
seen[recordID] = struct{}{}
|
||||
cleaned = append(cleaned, recordID)
|
||||
}
|
||||
if cmd.Flags().Changed("table-id") && tableID == "" {
|
||||
return apperrors.NewValidation("--table-id 不能为空")
|
||||
}
|
||||
if cmd.Flags().Changed("record-ids") && len(cleaned) == 0 {
|
||||
return apperrors.NewValidation("--record-ids 必须包含 1 到 5 个非空记录 ID")
|
||||
}
|
||||
if len(cleaned) > 5 {
|
||||
return apperrors.NewValidation(fmt.Sprintf("--record-ids 最多支持 5 个记录 ID,got %d", len(cleaned)))
|
||||
}
|
||||
if (tableID != "") != (len(cleaned) > 0) {
|
||||
return apperrors.NewValidation("--table-id 与 --record-ids 必须同时提供;定时触发工作流则两者都不传")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func validateWorkflowHistoryFlags(cmd *cobra.Command, _ []string) error {
|
||||
if cmd.Flags().Changed("page") {
|
||||
page, _ := cmd.Flags().GetInt("page")
|
||||
if page < 0 {
|
||||
return apperrors.NewValidation(fmt.Sprintf("--page 必须 >= 0,got %d", page))
|
||||
}
|
||||
}
|
||||
if cmd.Flags().Changed("size") {
|
||||
size, _ := cmd.Flags().GetInt("size")
|
||||
if size < 1 || size > 100 {
|
||||
return apperrors.NewValidation(fmt.Sprintf("--size 必须在 [1, 100] 范围内,got %d", size))
|
||||
}
|
||||
}
|
||||
for _, name := range []string{"after-time", "before-time"} {
|
||||
if !cmd.Flags().Changed(name) {
|
||||
continue
|
||||
}
|
||||
value, _ := cmd.Flags().GetInt(name)
|
||||
if value < 0 {
|
||||
return apperrors.NewValidation(fmt.Sprintf("--%s 必须是 >= 0 的 Unix 毫秒时间戳,got %d", name, value))
|
||||
}
|
||||
}
|
||||
if cmd.Flags().Changed("after-time") && cmd.Flags().Changed("before-time") {
|
||||
afterTime, _ := cmd.Flags().GetInt("after-time")
|
||||
beforeTime, _ := cmd.Flags().GetInt("before-time")
|
||||
if afterTime >= beforeTime {
|
||||
return apperrors.NewValidation(fmt.Sprintf("--after-time 必须小于 --before-time,got %d >= %d", afterTime, beforeTime))
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// recordQueryFetchAll implements --all auto-pagination for record query.
|
||||
// It prints only a complete result. A page limit, empty/invalid response,
|
||||
// transport failure, or cursor cycle returns a non-zero structured error whose
|
||||
@@ -1038,7 +1103,7 @@ func newAitableCommand() *cobra.Command {
|
||||
dws aitable form [list|delete|update] 表单管理
|
||||
dws aitable form field [list|update|hide] 表单字段管理
|
||||
dws aitable form share [get|update|notify] 表单分享管理
|
||||
dws aitable workflow [edit-example|create|update|enable|disable|get|list] 自动化工作流管理
|
||||
dws aitable workflow [edit-example|create|update|enable|disable|run|history|get|list] 自动化工作流管理
|
||||
dws aitable dashboard [get|create|update|delete|config-example] 仪表盘管理
|
||||
dws aitable chart [get|create|update|delete|widgets-example] 图表管理
|
||||
dws aitable export data 数据导出
|
||||
@@ -4878,7 +4943,7 @@ locked 为 true 表示视图已锁定,false 表示未锁定。`,
|
||||
|
||||
workflowCmd := &cobra.Command{
|
||||
Use: "workflow",
|
||||
Short: "自动化工作流管理(创建 / 更新 / 启停 / 查看 / 列表)",
|
||||
Short: "自动化工作流管理(创建 / 更新 / 启停 / 执行 / 历史 / 查询)",
|
||||
RunE: groupRunE,
|
||||
}
|
||||
|
||||
@@ -5223,6 +5288,116 @@ valid=false 仍表示 DSL 校验或发布未通过,必须读取 issues 修正
|
||||
},
|
||||
})
|
||||
|
||||
workflowRunCmd := NewLeafCommand(LeafSpec{
|
||||
Use: "run",
|
||||
Short: "执行指定自动化工作流",
|
||||
Long: `立即执行指定 Base 中的自动化工作流。此命令会启动真实的异步执行,并可能产生该工作流配置的消息发送、记录写入等副作用,因此执行前需要确认;CLI 不自动重试。
|
||||
|
||||
记录类触发器必须同时提供 --table-id 与 --record-ids;--table-id 必须与触发器绑定的数据表一致,--record-ids 接受 1 到 5 个不重复记录 ID。定时触发器不传这两个参数。
|
||||
返回每条记录的提交状态;提交成功项包含 executionId,可用 workflow history 返回项的 instanceId 匹配执行记录。`,
|
||||
Example: ` dws aitable workflow run --base-id BASE_ID --workflow-id WORKFLOW_ID --table-id TABLE_ID --record-ids RECORD_ID_1,RECORD_ID_2
|
||||
dws aitable workflow run --base-id BASE_ID --workflow-id WORKFLOW_ID`,
|
||||
Tool: "run_workflow",
|
||||
Safety: contract.SafetySpec{
|
||||
Effect: "write", Risk: "medium",
|
||||
Confirmation: "user_required", Idempotency: "non_idempotent",
|
||||
},
|
||||
Validate: validateWorkflowRunFlags,
|
||||
Flags: []LeafFlag{
|
||||
{Name: "base-id", Usage: "目标 Base ID (必填)", Bind: "baseId", Trim: true, Required: true, Aliases: []string{"base"}},
|
||||
{Name: "workflow-id", Usage: "目标工作流 ID (必填)", Bind: "workflowId", Trim: true, Required: true},
|
||||
{Name: "table-id", Usage: "记录类触发器绑定的 Table ID;定时触发器不传", Bind: "tableId", Trim: true, OmitEmpty: true, RequiredWhen: "record-ids is provided or the workflow uses a record-based trigger"},
|
||||
{Name: "record-ids", Usage: "触发工作流的记录 ID,逗号分隔;记录类触发器必填,1 到 5 个且不可重复", Kind: LeafStringSlice, Bind: "recordIds", RequiredWhen: "table-id is provided or the workflow uses a record-based trigger"},
|
||||
},
|
||||
Constraints: []LeafConstraint{{
|
||||
Kind: corecmd.Custom,
|
||||
Flags: []string{"table-id", "record-ids"},
|
||||
Description: "记录类触发器必须同时提供 --table-id 与 --record-ids;定时触发器两者都不传",
|
||||
}},
|
||||
Contract: LeafContract{
|
||||
Identity: contract.ToolIdentitySpec{
|
||||
ProductID: "aitable",
|
||||
Name: "workflow_run",
|
||||
CanonicalPath: "aitable.workflow_run",
|
||||
CLIPath: "aitable workflow run",
|
||||
PrimaryCLIPath: "aitable workflow run",
|
||||
},
|
||||
Description: "立即执行 AI 表格自动化工作流,并返回异步执行提交结果。",
|
||||
Interface: aitableMCPInterface("run_workflow"),
|
||||
Selection: contract.SelectionSpec{
|
||||
AgentSummary: "立即执行已知 AI 表格自动化工作流,并获取 executionId。",
|
||||
UseWhen: []string{"用户明确要求立即执行已知工作流,已确认真实 base-id、workflow-id、触发类型及可能产生的业务副作用;记录类触发器还需确认绑定的 table-id 和 1 到 5 个真实 record-id"},
|
||||
AvoidWhen: []string{"仅开启后续自动触发用 workflow enable;查询工作流定义用 workflow get;查询既有执行结果用 workflow history;返回 executionId 后应以 history 的 instanceId 核对,不要在结果不确定时直接重复执行"},
|
||||
Examples: []string{
|
||||
"dws aitable workflow run --base-id <BASE_ID> --workflow-id <WORKFLOW_ID> --table-id <TABLE_ID> --record-ids <RECORD_ID>",
|
||||
"dws aitable workflow run --base-id <BASE_ID> --workflow-id <WORKFLOW_ID>",
|
||||
},
|
||||
},
|
||||
Parameters: []contract.ParamDecl{
|
||||
{Name: "base-id", Property: "baseId", Required: boolPtr(true), InterfaceType: "string"},
|
||||
{Name: "workflow-id", Property: "workflowId", Required: boolPtr(true), InterfaceType: "string"},
|
||||
{Name: "table-id", Property: "tableId", InterfaceType: "string", RequiredWhen: "record-ids is provided or the workflow uses a record-based trigger"},
|
||||
{Name: "record-ids", Property: "recordIds", InterfaceType: "array", RequiredWhen: "table-id is provided or the workflow uses a record-based trigger"},
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
workflowHistoryCmd := NewLeafCommand(LeafSpec{
|
||||
Use: "history",
|
||||
Short: "查询工作流执行历史",
|
||||
Long: `分页查询指定 AI 表格工作流的执行历史。
|
||||
可按状态和 Unix 毫秒时间范围筛选;同时提供 --after-time 与 --before-time 时,前者必须小于后者。--page 从 0 开始,--size 默认 20、最大 100。
|
||||
返回 totalCount 与 list;run 返回的 executionId 可与历史项 instanceId 匹配。`,
|
||||
Example: ` dws aitable workflow history --base-id BASE_ID --workflow-id WORKFLOW_ID
|
||||
dws aitable workflow history --base-id BASE_ID --workflow-id WORKFLOW_ID --status failed --after-time 1786000000000 --before-time 1787000000000 --page 0 --size 50`,
|
||||
Tool: "get_flow_record_list",
|
||||
Safety: aitableSafetyRead(),
|
||||
Validate: validateWorkflowHistoryFlags,
|
||||
Flags: []LeafFlag{
|
||||
{Name: "base-id", Usage: "目标 Base ID (必填)", Bind: "baseId", Trim: true, Required: true, Aliases: []string{"base"}},
|
||||
{Name: "workflow-id", Usage: "目标工作流 ID (必填)", Bind: "flowId", Trim: true, Required: true},
|
||||
{Name: "status", Usage: "执行状态筛选;不传表示全部", Bind: "status", Trim: true, OmitEmpty: true, Enum: []string{"success", "failed", "running", "break", "untrigger"}},
|
||||
{Name: "after-time", Usage: "开始时间(Unix 毫秒)", Kind: LeafInt, Bind: "afterTime"},
|
||||
{Name: "before-time", Usage: "结束时间(Unix 毫秒)", Kind: LeafInt, Bind: "beforeTime"},
|
||||
{Name: "page", Usage: "页码,从 0 开始", Kind: LeafInt, Default: "0", Bind: "page"},
|
||||
{Name: "size", Usage: "每页条数 [1, 100]", Kind: LeafInt, Default: "20", Bind: "size"},
|
||||
},
|
||||
Constraints: []LeafConstraint{{
|
||||
Kind: corecmd.Custom,
|
||||
Flags: []string{"after-time", "before-time"},
|
||||
Description: "同时提供 --after-time 与 --before-time 时,--after-time 必须小于 --before-time",
|
||||
}},
|
||||
Contract: LeafContract{
|
||||
Identity: contract.ToolIdentitySpec{
|
||||
ProductID: "aitable",
|
||||
Name: "workflow_history",
|
||||
CanonicalPath: "aitable.workflow_history",
|
||||
CLIPath: "aitable workflow history",
|
||||
PrimaryCLIPath: "aitable workflow history",
|
||||
},
|
||||
Description: "分页查询 AI 表格自动化工作流执行历史。",
|
||||
Interface: aitableMCPInterface("get_flow_record_list"),
|
||||
Selection: contract.SelectionSpec{
|
||||
AgentSummary: "按状态、时间和分页条件查询工作流执行历史。",
|
||||
UseWhen: []string{"需要核对工作流是否执行、执行结果或定位 run 返回的 executionId 时;executionId 与历史项 instanceId 相同,running 为非终态"},
|
||||
AvoidWhen: []string{"查询工作流定义用 workflow get;列出工作流用 workflow list;立即发起执行用 workflow run"},
|
||||
Examples: []string{
|
||||
"dws aitable workflow history --base-id <BASE_ID> --workflow-id <WORKFLOW_ID>",
|
||||
"dws aitable workflow history --base-id <BASE_ID> --workflow-id <WORKFLOW_ID> --status failed --page 0 --size 50",
|
||||
},
|
||||
},
|
||||
Parameters: []contract.ParamDecl{
|
||||
{Name: "base-id", Property: "baseId", Required: boolPtr(true), InterfaceType: "string"},
|
||||
{Name: "workflow-id", Property: "flowId", Required: boolPtr(true), InterfaceType: "string"},
|
||||
{Name: "status", Property: "status", InterfaceType: "string", Enum: []string{"success", "failed", "running", "break", "untrigger"}},
|
||||
{Name: "after-time", Property: "afterTime", InterfaceType: "number"},
|
||||
{Name: "before-time", Property: "beforeTime", InterfaceType: "number"},
|
||||
{Name: "page", Property: "page", InterfaceType: "number"},
|
||||
{Name: "size", Property: "size", InterfaceType: "number"},
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
// ── dashboard: 仪表盘管理 ────────────────────────────────────
|
||||
|
||||
dashboardCmd := &cobra.Command{Use: "dashboard", Short: "仪表盘管理", RunE: groupRunE}
|
||||
@@ -7424,6 +7599,7 @@ parentSectionId 为空串表示该节点在 Base 根目录下。
|
||||
workflowCmd.AddCommand(
|
||||
workflowEditExampleCmd, workflowCreateCmd, workflowUpdateCmd,
|
||||
workflowEnableCmd, workflowDisableCmd,
|
||||
workflowRunCmd, workflowHistoryCmd,
|
||||
workflowGetCmd, workflowListCmd,
|
||||
)
|
||||
|
||||
|
||||
@@ -12,6 +12,7 @@ import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/DingTalk-Real-AI/dingtalk-workspace-cli/internal/testseam"
|
||||
"github.com/DingTalk-Real-AI/dingtalk-workspace-cli/pkg/edition"
|
||||
)
|
||||
|
||||
@@ -40,26 +41,23 @@ func (*aitableWorkflowCaller) JQ() string { return "" }
|
||||
|
||||
func runAitableWorkflowCommand(t *testing.T, stdin io.Reader, args ...string) (*aitableWorkflowCaller, error) {
|
||||
t.Helper()
|
||||
previousDeps := deps
|
||||
previousArgs := os.Args
|
||||
t.Cleanup(func() {
|
||||
deps = previousDeps
|
||||
os.Args = previousArgs
|
||||
})
|
||||
testseam.Protect(t, &os.Args)
|
||||
|
||||
caller := &aitableWorkflowCaller{}
|
||||
InitDeps(caller)
|
||||
InitDepsForTest(t, caller)
|
||||
deps.Out.w = io.Discard
|
||||
os.Args = append([]string{"dws", "aitable", "workflow"}, args...)
|
||||
|
||||
cmd := newAitableCommand()
|
||||
cmd.PersistentFlags().String("format", "json", "output format")
|
||||
cmd.PersistentFlags().Bool("yes", false, "skip confirmation")
|
||||
cmd.SilenceErrors = true
|
||||
cmd.SilenceUsage = true
|
||||
cmd.SetArgs(append([]string{"workflow"}, args...))
|
||||
if stdin != nil {
|
||||
cmd.SetIn(stdin)
|
||||
if stdin == nil {
|
||||
stdin = strings.NewReader("")
|
||||
}
|
||||
cmd.SetIn(stdin)
|
||||
return caller, cmd.Execute()
|
||||
}
|
||||
|
||||
@@ -197,3 +195,158 @@ func TestAitableWorkflowWriteRejectsInvalidInput(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestCrossPlatformCoverageAitableWorkflowRunMapsRecordTrigger(t *testing.T) {
|
||||
caller, err := runAitableWorkflowCommand(t, nil,
|
||||
"run",
|
||||
"--base-id", "base-run",
|
||||
"--workflow-id", "workflow-run",
|
||||
"--table-id", "table-run",
|
||||
"--record-ids", "record-1,record-2",
|
||||
"--yes",
|
||||
)
|
||||
if err != nil {
|
||||
t.Fatalf("workflow run returned error: %v", err)
|
||||
}
|
||||
if len(caller.calls) != 1 {
|
||||
t.Fatalf("tool call count = %d, want 1", len(caller.calls))
|
||||
}
|
||||
call := caller.calls[0]
|
||||
if call.productID != "aitable" || call.toolName != "run_workflow" {
|
||||
t.Fatalf("tool call = %s/%s, want aitable/run_workflow", call.productID, call.toolName)
|
||||
}
|
||||
wantArgs := map[string]any{
|
||||
"baseId": "base-run",
|
||||
"workflowId": "workflow-run",
|
||||
"tableId": "table-run",
|
||||
"recordIds": []string{"record-1", "record-2"},
|
||||
}
|
||||
if !reflect.DeepEqual(call.args, wantArgs) {
|
||||
t.Fatalf("tool args = %#v, want %#v", call.args, wantArgs)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCrossPlatformCoverageAitableWorkflowRunMapsScheduledTrigger(t *testing.T) {
|
||||
caller, err := runAitableWorkflowCommand(t, nil,
|
||||
"run", "--base", "base-scheduled", "--workflow-id", "workflow-scheduled", "--yes",
|
||||
)
|
||||
if err != nil {
|
||||
t.Fatalf("scheduled workflow run returned error: %v", err)
|
||||
}
|
||||
wantArgs := map[string]any{
|
||||
"baseId": "base-scheduled",
|
||||
"workflowId": "workflow-scheduled",
|
||||
}
|
||||
if len(caller.calls) != 1 || !reflect.DeepEqual(caller.calls[0].args, wantArgs) {
|
||||
t.Fatalf("calls = %#v, want one scheduled invocation %#v", caller.calls, wantArgs)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCrossPlatformCoverageAitableWorkflowRunRejectsUnsafeOrInvalidInput(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
args []string
|
||||
want string
|
||||
}{
|
||||
{name: "confirmation", args: []string{"run", "--base-id", "base", "--workflow-id", "workflow"}, want: "用户确认"},
|
||||
{name: "blank table", args: []string{"run", "--base-id", "base", "--workflow-id", "workflow", "--table-id", " ", "--yes"}, want: "--table-id 不能为空"},
|
||||
{name: "blank records", args: []string{"run", "--base-id", "base", "--workflow-id", "workflow", "--record-ids", " , ", "--yes"}, want: "--record-ids 必须包含"},
|
||||
{name: "table without records", args: []string{"run", "--base-id", "base", "--workflow-id", "workflow", "--table-id", "table", "--yes"}, want: "必须同时提供"},
|
||||
{name: "records without table", args: []string{"run", "--base-id", "base", "--workflow-id", "workflow", "--record-ids", "record", "--yes"}, want: "必须同时提供"},
|
||||
{name: "duplicate records", args: []string{"run", "--base-id", "base", "--workflow-id", "workflow", "--table-id", "table", "--record-ids", "record,record", "--yes"}, want: "不能包含重复值"},
|
||||
{name: "too many records", args: []string{"run", "--base-id", "base", "--workflow-id", "workflow", "--table-id", "table", "--record-ids", "r1,r2,r3,r4,r5,r6", "--yes"}, want: "最多支持 5 个"},
|
||||
}
|
||||
for _, tc := range tests {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
caller, err := runAitableWorkflowCommand(t, nil, tc.args...)
|
||||
if err == nil || !strings.Contains(strings.ToLower(err.Error()), strings.ToLower(tc.want)) {
|
||||
t.Fatalf("error = %v, want substring %q", err, tc.want)
|
||||
}
|
||||
if len(caller.calls) != 0 {
|
||||
t.Fatalf("invalid run reached MCP: %#v", caller.calls)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestCrossPlatformCoverageAitableWorkflowHistoryMapsFilters(t *testing.T) {
|
||||
caller, err := runAitableWorkflowCommand(t, nil,
|
||||
"history",
|
||||
"--base-id", "base-history",
|
||||
"--workflow-id", "workflow-history",
|
||||
"--status", "failed",
|
||||
"--after-time", "1786000000000",
|
||||
"--before-time", "1787000000000",
|
||||
"--page", "2",
|
||||
"--size", "50",
|
||||
)
|
||||
if err != nil {
|
||||
t.Fatalf("workflow history returned error: %v", err)
|
||||
}
|
||||
if len(caller.calls) != 1 {
|
||||
t.Fatalf("tool call count = %d, want 1", len(caller.calls))
|
||||
}
|
||||
call := caller.calls[0]
|
||||
if call.productID != "aitable" || call.toolName != "get_flow_record_list" {
|
||||
t.Fatalf("tool call = %s/%s, want aitable/get_flow_record_list", call.productID, call.toolName)
|
||||
}
|
||||
wantArgs := map[string]any{
|
||||
"baseId": "base-history",
|
||||
"flowId": "workflow-history",
|
||||
"status": "failed",
|
||||
"afterTime": 1786000000000,
|
||||
"beforeTime": 1787000000000,
|
||||
"page": 2,
|
||||
"size": 50,
|
||||
}
|
||||
if !reflect.DeepEqual(call.args, wantArgs) {
|
||||
t.Fatalf("tool args = %#v, want %#v", call.args, wantArgs)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCrossPlatformCoverageAitableWorkflowHistoryMapsSingleTimeFilter(t *testing.T) {
|
||||
caller, err := runAitableWorkflowCommand(t, nil,
|
||||
"history",
|
||||
"--base-id", "base-history",
|
||||
"--workflow-id", "workflow-history",
|
||||
"--after-time", "1786000000000",
|
||||
)
|
||||
if err != nil {
|
||||
t.Fatalf("workflow history returned error: %v", err)
|
||||
}
|
||||
wantArgs := map[string]any{
|
||||
"baseId": "base-history",
|
||||
"flowId": "workflow-history",
|
||||
"afterTime": 1786000000000,
|
||||
"size": 20,
|
||||
}
|
||||
if len(caller.calls) != 1 || !reflect.DeepEqual(caller.calls[0].args, wantArgs) {
|
||||
t.Fatalf("calls = %#v, want one history invocation %#v", caller.calls, wantArgs)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCrossPlatformCoverageAitableWorkflowHistoryRejectsInvalidFilters(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
args []string
|
||||
want string
|
||||
}{
|
||||
{name: "status", args: []string{"history", "--base-id", "base", "--workflow-id", "workflow", "--status", "unknown"}, want: "允许值"},
|
||||
{name: "negative page", args: []string{"history", "--base-id", "base", "--workflow-id", "workflow", "--page", "-1"}, want: "--page 必须 >= 0"},
|
||||
{name: "zero size", args: []string{"history", "--base-id", "base", "--workflow-id", "workflow", "--size", "0"}, want: "--size 必须在"},
|
||||
{name: "large size", args: []string{"history", "--base-id", "base", "--workflow-id", "workflow", "--size", "101"}, want: "--size 必须在"},
|
||||
{name: "negative after", args: []string{"history", "--base-id", "base", "--workflow-id", "workflow", "--after-time", "-1"}, want: "Unix 毫秒时间戳"},
|
||||
{name: "reversed range", args: []string{"history", "--base-id", "base", "--workflow-id", "workflow", "--after-time", "200", "--before-time", "100"}, want: "必须小于"},
|
||||
}
|
||||
for _, tc := range tests {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
caller, err := runAitableWorkflowCommand(t, nil, tc.args...)
|
||||
if err == nil || !strings.Contains(err.Error(), tc.want) {
|
||||
t.Fatalf("error = %v, want substring %q", err, tc.want)
|
||||
}
|
||||
if len(caller.calls) != 0 {
|
||||
t.Fatalf("invalid history query reached MCP: %#v", caller.calls)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
+762
-267
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,163 @@
|
||||
// Copyright 2026 Alibaba Group
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package helpers
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
apperrors "github.com/DingTalk-Real-AI/dingtalk-workspace-cli/internal/errors"
|
||||
)
|
||||
|
||||
func runNativeCardUpdate(t *testing.T, caller *scriptedToolCaller, args ...string) error {
|
||||
t.Helper()
|
||||
installScriptedCaller(t, caller)
|
||||
root := newChatCommand()
|
||||
root.SilenceErrors = true
|
||||
root.SilenceUsage = true
|
||||
if root.PersistentFlags().Lookup("dry-run") == nil {
|
||||
root.PersistentFlags().Bool("dry-run", false, "preview without executing")
|
||||
}
|
||||
if root.PersistentFlags().Lookup("yes") == nil {
|
||||
root.PersistentFlags().Bool("yes", false, "skip confirmation")
|
||||
}
|
||||
root.SetArgs(args)
|
||||
return root.Execute()
|
||||
}
|
||||
|
||||
func TestCrossPlatformCoverageNativeMessageUpdateCardVerifiesWrite(t *testing.T) {
|
||||
t.Run("atomic command preserves no-extra-confirmation contract", func(t *testing.T) {
|
||||
caller := &scriptedToolCaller{steps: []scriptedToolStep{{text: `{"result":{"bizId":"biz-confirm","updated":true}}`}}}
|
||||
if err := runNativeCardUpdate(t, caller,
|
||||
"message", "update-card",
|
||||
"--biz-id", "biz-confirm",
|
||||
"--content", "原子更新",
|
||||
"--flow-status", "3",
|
||||
); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
wantArgs := map[string]any{
|
||||
"bizId": "biz-confirm",
|
||||
"msgContent": "原子更新",
|
||||
"flowStatus": 3,
|
||||
}
|
||||
if caller.calls != 1 || caller.server != "im" || caller.tool != "update_streaming_card" || !reflect.DeepEqual(caller.args, wantArgs) {
|
||||
t.Fatalf("atomic call = count:%d server:%q tool:%q args:%#v", caller.calls, caller.server, caller.tool, caller.args)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("explicit evidence succeeds", func(t *testing.T) {
|
||||
caller := &scriptedToolCaller{steps: []scriptedToolStep{{text: `{"result":{"bizId":"biz-1","updated":true}}`}}}
|
||||
err := runNativeCardUpdate(t, caller,
|
||||
"message", "update-card",
|
||||
"--biz-id", "biz-1",
|
||||
"--content", "完成",
|
||||
"--flow-status", "3",
|
||||
)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if caller.calls != 1 || caller.server != "im" || caller.tool != "update_streaming_card" {
|
||||
t.Fatalf("call = count:%d server:%q tool:%q", caller.calls, caller.server, caller.tool)
|
||||
}
|
||||
if caller.args["bizId"] != "biz-1" {
|
||||
t.Fatalf("args = %#v", caller.args)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("generic success is unverified", func(t *testing.T) {
|
||||
caller := &scriptedToolCaller{steps: []scriptedToolStep{{text: `{"success":true,"errorCode":null}`}}}
|
||||
err := runNativeCardUpdate(t, caller,
|
||||
"message", "update-card",
|
||||
"--biz-id", "not-a-real-card",
|
||||
"--content", "完成",
|
||||
"--flow-status", "3",
|
||||
)
|
||||
var typed *apperrors.Error
|
||||
if !errors.As(err, &typed) || typed.Reason != "streaming_card_update_unverified" {
|
||||
t.Fatalf("error = %#v, want streaming_card_update_unverified", err)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("lower write error is preserved", func(t *testing.T) {
|
||||
caller := &scriptedToolCaller{steps: []scriptedToolStep{{err: errors.New("write unavailable")}}}
|
||||
err := runNativeCardUpdate(t, caller,
|
||||
"message", "update-card",
|
||||
"--biz-id", "biz-1",
|
||||
"--content", "完成",
|
||||
"--flow-status", "3",
|
||||
)
|
||||
if err == nil {
|
||||
t.Fatal("lower write error was ignored")
|
||||
}
|
||||
})
|
||||
|
||||
for _, test := range []struct {
|
||||
name string
|
||||
response string
|
||||
wantReason string
|
||||
}{
|
||||
{name: "empty response", response: "", wantReason: "streaming_card_update_unverified"},
|
||||
{name: "invalid response", response: "{", wantReason: "streaming_card_update_response_invalid"},
|
||||
{name: "not applied", response: `{"result":{"updated":false}}`, wantReason: "streaming_card_update_not_applied"},
|
||||
{name: "biz id drift", response: `{"result":{"bizId":"biz-other","updated":true}}`, wantReason: "streaming_card_update_biz_id_mismatch"},
|
||||
} {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
caller := &scriptedToolCaller{steps: []scriptedToolStep{{text: test.response}}}
|
||||
err := runNativeCardUpdate(t, caller,
|
||||
"message", "update-card",
|
||||
"--biz-id", "biz-1",
|
||||
"--content", "完成",
|
||||
"--flow-status", "3",
|
||||
)
|
||||
var typed *apperrors.Error
|
||||
if !errors.As(err, &typed) || typed.Reason != test.wantReason {
|
||||
t.Fatalf("error = %#v, want reason %q", err, test.wantReason)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
t.Run("invalid arguments make no call", func(t *testing.T) {
|
||||
for _, args := range [][]string{
|
||||
{"message", "update-card", "--biz-id", "<bizId>", "--content", "完成", "--flow-status", "3"},
|
||||
{"message", "update-card", "--biz-id", "biz-1", "--content", "完成", "--flow-status", "6"},
|
||||
} {
|
||||
caller := &scriptedToolCaller{}
|
||||
if err := runNativeCardUpdate(t, caller, args...); err == nil {
|
||||
t.Fatalf("args %v unexpectedly succeeded", args)
|
||||
}
|
||||
if caller.calls != 0 {
|
||||
t.Fatalf("args %v made %d calls", args, caller.calls)
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("dry run publishes unverified plan without write", func(t *testing.T) {
|
||||
caller := &scriptedToolCaller{}
|
||||
err := runNativeCardUpdate(t, caller,
|
||||
"message", "update-card",
|
||||
"--biz-id", "biz-preview",
|
||||
"--content", "完成",
|
||||
"--flow-status", "3",
|
||||
"--dry-run",
|
||||
)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if caller.calls != 0 {
|
||||
t.Fatalf("dry-run made %d calls", caller.calls)
|
||||
}
|
||||
})
|
||||
}
|
||||
@@ -25,7 +25,7 @@ func runChatCoverageCommand(t *testing.T, caller edition.ToolCaller, args ...str
|
||||
root.SilenceUsage = true
|
||||
root.SetOut(io.Discard)
|
||||
root.SetErr(io.Discard)
|
||||
root.SetArgs(args)
|
||||
root.SetArgs(append(append([]string(nil), args...), "--yes"))
|
||||
return root.ExecuteContext(context.Background())
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,267 @@
|
||||
package helpers
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"io"
|
||||
"reflect"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/DingTalk-Real-AI/dingtalk-workspace-cli/pkg/edition"
|
||||
)
|
||||
|
||||
type chatMessagePaginationCaller struct {
|
||||
steps []scriptedToolStep
|
||||
calls []pagedCommandCall
|
||||
}
|
||||
|
||||
func (c *chatMessagePaginationCaller) CallTool(_ context.Context, serverID, toolName string, args map[string]any) (*edition.ToolResult, error) {
|
||||
copied := map[string]any{}
|
||||
for k, v := range args {
|
||||
copied[k] = v
|
||||
}
|
||||
c.calls = append(c.calls, pagedCommandCall{server: serverID, tool: toolName, args: copied})
|
||||
if len(c.steps) == 0 {
|
||||
return textToolResult(`{"result":{"messages":[],"items":[],"hasMore":false,"nextCursor":"0"}}`), nil
|
||||
}
|
||||
step := c.steps[len(c.calls)-1]
|
||||
if step.err != nil {
|
||||
return nil, step.err
|
||||
}
|
||||
return textToolResult(step.text), nil
|
||||
}
|
||||
|
||||
func (*chatMessagePaginationCaller) Format() string { return "json" }
|
||||
func (*chatMessagePaginationCaller) DryRun() bool { return false }
|
||||
func (*chatMessagePaginationCaller) Fields() string { return "" }
|
||||
func (*chatMessagePaginationCaller) JQ() string { return "" }
|
||||
|
||||
func executeChatMessagePaginationCommand(t *testing.T, caller *chatMessagePaginationCaller, args ...string) (map[string]any, error) {
|
||||
t.Helper()
|
||||
oldDeps := deps
|
||||
oldSleep := helperSleep
|
||||
t.Cleanup(func() {
|
||||
deps = oldDeps
|
||||
helperSleep = oldSleep
|
||||
})
|
||||
InitDeps(caller)
|
||||
out := &bytes.Buffer{}
|
||||
deps.Out.w = out
|
||||
deps.Out.errW = io.Discard
|
||||
helperSleep = func(d time.Duration) {}
|
||||
|
||||
root := newChatCommand()
|
||||
root.SilenceErrors = true
|
||||
root.SilenceUsage = true
|
||||
root.SetOut(io.Discard)
|
||||
root.SetErr(io.Discard)
|
||||
root.SetArgs(args)
|
||||
err := root.ExecuteContext(context.Background())
|
||||
if out.Len() == 0 {
|
||||
return nil, err
|
||||
}
|
||||
var parsed map[string]any
|
||||
if unmarshalErr := json.Unmarshal(out.Bytes(), &parsed); unmarshalErr != nil {
|
||||
t.Fatalf("stdout JSON = %q, err = %v", out.String(), unmarshalErr)
|
||||
}
|
||||
return parsed, err
|
||||
}
|
||||
|
||||
func TestChatMessagePaginationDefaultSinglePageUnchanged(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
args []string
|
||||
server string
|
||||
tool string
|
||||
want map[string]any
|
||||
}{
|
||||
{
|
||||
name: "list-all",
|
||||
args: []string{"message", "list-all", "--start", "2026-08-01 00:00:00", "--end", "2026-08-02 00:00:00"},
|
||||
server: "",
|
||||
tool: "search_messages_by_time_range",
|
||||
want: map[string]any{"startTime": "2026-08-01 00:00:00", "endTime": "2026-08-02 00:00:00", "limit": 50, "cursor": "0"},
|
||||
},
|
||||
{
|
||||
name: "list-by-sender",
|
||||
args: []string{"message", "list-by-sender", "--sender-user-id", "u1", "--start", "2026-08-01T00:00:00+08:00", "--end", "2026-08-02T00:00:00+08:00"},
|
||||
server: "",
|
||||
tool: "search_messages_by_sender",
|
||||
want: map[string]any{"senderUserId": "u1", "startTime": float64(1785513600000), "endTime": float64(1785600000000), "limit": 50, "cursor": "0"},
|
||||
},
|
||||
{
|
||||
name: "list-mentions",
|
||||
args: []string{"message", "list-mentions", "--group", "cid1", "--start", "2026-08-01T00:00:00+08:00", "--end", "2026-08-02T00:00:00+08:00"},
|
||||
server: "",
|
||||
tool: "search_at_me_message",
|
||||
want: map[string]any{"openConversationId": "cid1", "startTime": float64(1785513600000), "endTime": float64(1785600000000), "limit": 50, "cursor": "0"},
|
||||
},
|
||||
{
|
||||
name: "list-focused",
|
||||
args: []string{"message", "list-focused"},
|
||||
server: "",
|
||||
tool: "list_special_focus_messages",
|
||||
want: map[string]any{"limit": 50},
|
||||
},
|
||||
{
|
||||
name: "search",
|
||||
args: []string{"message", "search", "--query", "发布", "--start", "2026-08-01T00:00:00+08:00", "--end", "2026-08-02T00:00:00+08:00"},
|
||||
server: "",
|
||||
tool: "search_messages_by_keyword",
|
||||
want: map[string]any{"keyword": "发布", "startTime": float64(1785513600000), "endTime": float64(1785600000000), "limit": 100, "cursor": "0"},
|
||||
},
|
||||
{
|
||||
name: "search-advanced",
|
||||
args: []string{"message", "search-advanced", "--query", "周报", "--start", "2026-08-01T00:00:00+08:00", "--end", "2026-08-02T00:00:00+08:00"},
|
||||
server: "im",
|
||||
tool: "search_messages",
|
||||
want: map[string]any{"keyword": "周报", "startTime": float64(1785513600000), "endTime": float64(1785600000000), "limit": 100, "cursor": "0"},
|
||||
},
|
||||
{
|
||||
name: "list-favorites",
|
||||
args: []string{"message", "list-favorites"},
|
||||
server: "im",
|
||||
tool: "list_message_favorites",
|
||||
want: map[string]any{"cursor": int64(0), "size": "20"},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
caller := &chatMessagePaginationCaller{}
|
||||
args := append([]string{}, tt.args...)
|
||||
args = append(args, "--page-limit", "2", "--max-items", "1", "--page-delay", "0")
|
||||
_, err := executeChatMessagePaginationCommand(t, caller, args...)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(caller.calls) != 1 {
|
||||
t.Fatalf("calls = %#v, want one fallback call", caller.calls)
|
||||
}
|
||||
got := caller.calls[0]
|
||||
if got.server != tt.server || got.tool != tt.tool || !argsEqual(got.args, tt.want) {
|
||||
t.Fatalf("call = %#v, want server=%s tool=%s args=%#v", got, tt.server, tt.tool, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestChatMessagePaginationPageAllAggregatesSevenCommands(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
args []string
|
||||
server string
|
||||
tool string
|
||||
itemPath string
|
||||
cursorOne any
|
||||
cursorTwo any
|
||||
pageOne string
|
||||
pageTwo string
|
||||
}{
|
||||
{
|
||||
name: "list-all", args: []string{"message", "list-all", "--start", "2026-08-01 00:00:00", "--end", "2026-08-02 00:00:00"},
|
||||
server: "chat", tool: "search_messages_by_time_range", itemPath: "conversationMessagesList", cursorOne: "0", cursorTwo: "c2",
|
||||
pageOne: `{"result":{"conversationMessagesList":[{"openConversationId":"cid1","title":"群1","messages":[{"id":"m1"}]}],"hasMore":true,"nextCursor":"c2"}}`,
|
||||
pageTwo: `{"result":{"conversationMessagesList":[{"openConversationId":"cid1","title":"群1","messages":[{"id":"m2"}]}],"hasMore":false,"nextCursor":""}}`,
|
||||
},
|
||||
{
|
||||
name: "list-by-sender", args: []string{"message", "list-by-sender", "--sender-user-id", "u1", "--start", "2026-08-01T00:00:00+08:00", "--end", "2026-08-02T00:00:00+08:00"},
|
||||
server: "chat", tool: "search_messages_by_sender", itemPath: "conversationMessagesList", cursorOne: "0", cursorTwo: "c2",
|
||||
pageOne: `{"result":{"conversationMessagesList":[{"openConversationId":"cid1","title":"洄川","messages":[{"id":"m1"}]}],"hasMore":true,"nextCursor":"c2"}}`,
|
||||
pageTwo: `{"result":{"conversationMessagesList":[{"openConversationId":"cid1","title":"洄川","messages":[{"id":"m2"}]}],"hasMore":false,"nextCursor":""}}`,
|
||||
},
|
||||
{
|
||||
name: "list-mentions", args: []string{"message", "list-mentions", "--start", "2026-08-01T00:00:00+08:00", "--end", "2026-08-02T00:00:00+08:00"},
|
||||
server: "chat", tool: "search_at_me_message", itemPath: "conversationMessagesList", cursorOne: "0", cursorTwo: "c2",
|
||||
pageOne: `{"result":{"conversationMessagesList":[{"openConversationId":"cid1","title":"群1","messages":[{"id":"m1"}]}],"hasMore":true,"nextCursor":"c2"}}`,
|
||||
pageTwo: `{"result":{"conversationMessagesList":[{"openConversationId":"cid1","title":"群1","messages":[{"id":"m2"}]}],"hasMore":false,"nextCursor":""}}`,
|
||||
},
|
||||
{
|
||||
name: "list-focused", args: []string{"message", "list-focused"},
|
||||
server: "chat", tool: "list_special_focus_messages", itemPath: "messages", cursorOne: nil, cursorTwo: int64(2),
|
||||
pageOne: `{"result":{"messages":[{"id":"m1"}],"hasMore":true,"nextCursor":2}}`,
|
||||
pageTwo: `{"result":{"messages":[{"id":"m2"}],"hasMore":false,"nextCursor":0}}`,
|
||||
},
|
||||
{
|
||||
name: "search", args: []string{"message", "search", "--query", "发布", "--start", "2026-08-01T00:00:00+08:00", "--end", "2026-08-02T00:00:00+08:00"},
|
||||
server: "chat", tool: "search_messages_by_keyword", itemPath: "conversationMessagesList", cursorOne: "0", cursorTwo: "c2",
|
||||
pageOne: `{"result":{"conversationMessagesList":[{"openConversationId":"cid1","title":"群1","messages":[{"id":"m1"}]}],"hasMore":true,"nextCursor":"c2"}}`,
|
||||
pageTwo: `{"result":{"conversationMessagesList":[{"openConversationId":"cid1","title":"群1","messages":[{"id":"m2"}]}],"hasMore":false,"nextCursor":""}}`,
|
||||
},
|
||||
{
|
||||
name: "search-advanced", args: []string{"message", "search-advanced", "--query", "周报"},
|
||||
server: "im", tool: "search_messages", itemPath: "conversationMessagesList", cursorOne: "0", cursorTwo: "c2",
|
||||
pageOne: `{"result":{"conversationMessagesList":[{"openConversationId":"cid1","title":"群1","messages":[{"id":"m1"}]}],"hasMore":true,"nextCursor":"c2"}}`,
|
||||
pageTwo: `{"result":{"conversationMessagesList":[{"openConversationId":"cid1","title":"群1","messages":[{"id":"m2"}]}],"hasMore":false,"nextCursor":""}}`,
|
||||
},
|
||||
{
|
||||
name: "list-favorites", args: []string{"message", "list-favorites"},
|
||||
server: "im", tool: "list_message_favorites", itemPath: "items", cursorOne: int64(0), cursorTwo: int64(20),
|
||||
pageOne: `{"result":{"items":[{"id":"f1"}],"hasMore":true,"nextCursor":20}}`,
|
||||
pageTwo: `{"result":{"items":[{"id":"f2"}],"hasMore":false,"nextCursor":0}}`,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
caller := &chatMessagePaginationCaller{steps: []scriptedToolStep{{text: tt.pageOne}, {text: tt.pageTwo}}}
|
||||
args := append([]string{}, tt.args...)
|
||||
args = append(args, "--page-all", "--page-delay", "0")
|
||||
got, err := executeChatMessagePaginationCommand(t, caller, args...)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
items := got["result"].(map[string]any)[tt.itemPath].([]any)
|
||||
if tt.itemPath == "conversationMessagesList" {
|
||||
messages := items[0].(map[string]any)["messages"].([]any)
|
||||
if len(items) != 1 || len(messages) != 2 {
|
||||
t.Fatalf("conversation items = %#v", items)
|
||||
}
|
||||
} else if len(items) != 2 {
|
||||
t.Fatalf("items = %#v", items)
|
||||
}
|
||||
if len(caller.calls) != 2 {
|
||||
t.Fatalf("calls = %#v, want two pages", caller.calls)
|
||||
}
|
||||
if caller.calls[0].server != tt.server || caller.calls[0].tool != tt.tool {
|
||||
t.Fatalf("first call = %#v", caller.calls[0])
|
||||
}
|
||||
if !reflect.DeepEqual(caller.calls[0].args["cursor"], tt.cursorOne) {
|
||||
t.Fatalf("first cursor = %#v, want %#v", caller.calls[0].args["cursor"], tt.cursorOne)
|
||||
}
|
||||
if !reflect.DeepEqual(caller.calls[1].args["cursor"], tt.cursorTwo) {
|
||||
t.Fatalf("second cursor = %#v, want %#v", caller.calls[1].args["cursor"], tt.cursorTwo)
|
||||
}
|
||||
paging := got["paging"].(map[string]any)
|
||||
if paging["pages"].(float64) != 2 || paging["total"].(float64) != 2 || paging["truncated"] != false {
|
||||
t.Fatalf("paging = %#v", paging)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func argsEqual(got, want map[string]any) bool {
|
||||
if len(got) != len(want) {
|
||||
return false
|
||||
}
|
||||
for key, wantValue := range want {
|
||||
gotValue, ok := got[key]
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
switch w := wantValue.(type) {
|
||||
case float64:
|
||||
g, ok := gotValue.(int64)
|
||||
if !ok || float64(g) != w {
|
||||
return false
|
||||
}
|
||||
default:
|
||||
if !reflect.DeepEqual(gotValue, wantValue) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
@@ -16,6 +16,7 @@ package helpers
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"io"
|
||||
"os"
|
||||
"reflect"
|
||||
@@ -23,8 +24,10 @@ import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
apperrors "github.com/DingTalk-Real-AI/dingtalk-workspace-cli/internal/errors"
|
||||
"github.com/DingTalk-Real-AI/dingtalk-workspace-cli/pkg/agentproduct"
|
||||
"github.com/DingTalk-Real-AI/dingtalk-workspace-cli/pkg/edition"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
type chatMessageSearchCall struct {
|
||||
@@ -34,12 +37,41 @@ type chatMessageSearchCall struct {
|
||||
}
|
||||
|
||||
type chatMessageSearchCaller struct {
|
||||
calls []chatMessageSearchCall
|
||||
calls []chatMessageSearchCall
|
||||
searchResponse string
|
||||
searchResponses []string
|
||||
searchCalls int
|
||||
searchError error
|
||||
failPreflight bool
|
||||
preflightError error
|
||||
}
|
||||
|
||||
func (c *chatMessageSearchCaller) CallTool(_ context.Context, productID, toolName string, args map[string]any) (*edition.ToolResult, error) {
|
||||
c.calls = append(c.calls, chatMessageSearchCall{productID: productID, toolName: toolName, args: args})
|
||||
return &edition.ToolResult{Content: []edition.ContentBlock{{Type: "text", Text: `{}`}}}, nil
|
||||
text := `{}`
|
||||
if toolName == "get_conversation_info" {
|
||||
if c.preflightError != nil {
|
||||
return nil, c.preflightError
|
||||
}
|
||||
if c.failPreflight {
|
||||
return nil, errors.New("conversation not found")
|
||||
}
|
||||
text = `{"result":{"openConversationId":"` + args["openConversationId"].(string) + `"}}`
|
||||
}
|
||||
if toolName == "search_messages_by_keyword" || toolName == "search_messages" {
|
||||
if c.searchError != nil {
|
||||
c.searchCalls++
|
||||
return nil, c.searchError
|
||||
}
|
||||
text = `{"result":{"messages":[],"hasMore":false}}`
|
||||
if c.searchCalls < len(c.searchResponses) {
|
||||
text = c.searchResponses[c.searchCalls]
|
||||
} else if c.searchResponse != "" {
|
||||
text = c.searchResponse
|
||||
}
|
||||
c.searchCalls++
|
||||
}
|
||||
return &edition.ToolResult{Content: []edition.ContentBlock{{Type: "text", Text: text}}}, nil
|
||||
}
|
||||
|
||||
func (*chatMessageSearchCaller) Format() string { return "json" }
|
||||
@@ -72,6 +104,7 @@ func TestCrossPlatformCoverageChatMessageSearchUsesMCPContracts(t *testing.T) {
|
||||
productID string
|
||||
toolName string
|
||||
wantToolArg map[string]any
|
||||
preflight []string
|
||||
}{
|
||||
{
|
||||
name: "keyword search",
|
||||
@@ -79,13 +112,13 @@ func TestCrossPlatformCoverageChatMessageSearchUsesMCPContracts(t *testing.T) {
|
||||
productID: "chat",
|
||||
toolName: "search_messages_by_keyword",
|
||||
wantToolArg: map[string]any{
|
||||
"keyword": "categoryName",
|
||||
"openConversationId": "cid-1",
|
||||
"startTime": startTime.UnixMilli(),
|
||||
"endTime": endTime.UnixMilli(),
|
||||
"limit": 100,
|
||||
"cursor": "0",
|
||||
"keyword": "categoryName",
|
||||
"startTime": startTime.UnixMilli(),
|
||||
"endTime": endTime.UnixMilli(),
|
||||
"limit": 100,
|
||||
"cursor": "0",
|
||||
},
|
||||
preflight: []string{"cid-1"},
|
||||
},
|
||||
{
|
||||
name: "advanced search",
|
||||
@@ -93,16 +126,16 @@ func TestCrossPlatformCoverageChatMessageSearchUsesMCPContracts(t *testing.T) {
|
||||
productID: "im",
|
||||
toolName: "search_messages",
|
||||
wantToolArg: map[string]any{
|
||||
"keyword": "categoryName",
|
||||
"openConversationIds": []string{"cid-1", "cid-2"},
|
||||
"messageType": "text",
|
||||
"onlyRobotMessages": true,
|
||||
"searchConvType": "group",
|
||||
"startTime": startTime.UnixMilli(),
|
||||
"endTime": endTime.UnixMilli(),
|
||||
"limit": 100,
|
||||
"cursor": "0",
|
||||
"keyword": "categoryName",
|
||||
"messageType": "text",
|
||||
"onlyRobotMessages": true,
|
||||
"searchConvType": "group",
|
||||
"startTime": startTime.UnixMilli(),
|
||||
"endTime": endTime.UnixMilli(),
|
||||
"limit": 100,
|
||||
"cursor": "0",
|
||||
},
|
||||
preflight: []string{"cid-1", "cid-2"},
|
||||
},
|
||||
}
|
||||
|
||||
@@ -116,14 +149,21 @@ func TestCrossPlatformCoverageChatMessageSearchUsesMCPContracts(t *testing.T) {
|
||||
cmd := newChatCommand()
|
||||
cmd.SilenceErrors = true
|
||||
cmd.SilenceUsage = true
|
||||
cmd.SetOut(io.Discard)
|
||||
cmd.SetArgs(tt.args)
|
||||
if err := cmd.Execute(); err != nil {
|
||||
t.Fatalf("chat search returned error: %v", err)
|
||||
}
|
||||
if len(caller.calls) != 1 {
|
||||
t.Fatalf("tool call count = %d, want 1", len(caller.calls))
|
||||
if len(caller.calls) != len(tt.preflight)+1 {
|
||||
t.Fatalf("tool calls = %#v", caller.calls)
|
||||
}
|
||||
call := caller.calls[0]
|
||||
for index, conversationID := range tt.preflight {
|
||||
call := caller.calls[index]
|
||||
if call.productID != "chat" || call.toolName != "get_conversation_info" || call.args["openConversationId"] != conversationID {
|
||||
t.Fatalf("preflight[%d] = %#v", index, call)
|
||||
}
|
||||
}
|
||||
call := caller.calls[len(caller.calls)-1]
|
||||
if call.productID != tt.productID || call.toolName != tt.toolName {
|
||||
t.Fatalf("tool call = %s/%s, want %s/%s", call.productID, call.toolName, tt.productID, tt.toolName)
|
||||
}
|
||||
@@ -134,6 +174,507 @@ func TestCrossPlatformCoverageChatMessageSearchUsesMCPContracts(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func executeNativeScopedSearch(t *testing.T, caller *chatMessageSearchCaller, args ...string) (map[string]any, error) {
|
||||
t.Helper()
|
||||
previousDeps := deps
|
||||
t.Cleanup(func() { deps = previousDeps })
|
||||
InitDeps(caller)
|
||||
deps.Out.w = io.Discard
|
||||
cmd := newChatCommand()
|
||||
cmd.SilenceErrors = true
|
||||
cmd.SilenceUsage = true
|
||||
var output strings.Builder
|
||||
cmd.SetOut(&output)
|
||||
cmd.SetArgs(args)
|
||||
if err := cmd.Execute(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var payload map[string]any
|
||||
if err := json.Unmarshal([]byte(output.String()), &payload); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return payload, nil
|
||||
}
|
||||
|
||||
func TestNativeScopedSearchFiltersGlobalResultsForBothEntries(t *testing.T) {
|
||||
start := "2026-07-09T00:00:00+08:00"
|
||||
end := "2026-07-11T00:00:00+08:00"
|
||||
for _, tt := range []struct {
|
||||
name string
|
||||
args []string
|
||||
tool string
|
||||
scopeParam string
|
||||
}{
|
||||
{
|
||||
name: "keyword search",
|
||||
args: []string{"message", "search", "--query", "周报", "--group", "cid-target", "--start", start, "--end", end},
|
||||
tool: "search_messages_by_keyword",
|
||||
scopeParam: "openConversationId",
|
||||
},
|
||||
{
|
||||
name: "advanced search",
|
||||
args: []string{"message", "search-advanced", "--query", "周报", "--conversation-ids", "cid-target"},
|
||||
tool: "search_messages",
|
||||
scopeParam: "openConversationIds",
|
||||
},
|
||||
} {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
caller := &chatMessageSearchCaller{searchResponse: `{
|
||||
"result": {
|
||||
"conversationMessagesList": [
|
||||
{"openConversationId":"cid-target","title":"目标群","messages":[{"openMessageId":"m-target","content":"目标"}]},
|
||||
{"openConversationId":"cid-other","title":"其他群","messages":[{"openMessageId":"m-other","content":"越界"}]}
|
||||
],
|
||||
"hasMore": false
|
||||
}
|
||||
}`}
|
||||
payload, err := executeNativeScopedSearch(t, caller, tt.args...)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
result, _ := payload["result"].(map[string]any)
|
||||
groups, _ := result["conversationMessagesList"].([]any)
|
||||
if len(groups) != 1 {
|
||||
t.Fatalf("result = %#v", result)
|
||||
}
|
||||
group, _ := groups[0].(map[string]any)
|
||||
if group["openConversationId"] != "cid-target" {
|
||||
t.Fatalf("group = %#v", group)
|
||||
}
|
||||
scope, _ := payload["scope"].(map[string]any)
|
||||
if scope["targetsValidated"] != true || scope["resultsWithinScope"] != true || scope["filterMode"] != "client" {
|
||||
t.Fatalf("scope = %#v", scope)
|
||||
}
|
||||
searchCall := caller.calls[len(caller.calls)-1]
|
||||
if searchCall.toolName != tt.tool {
|
||||
t.Fatalf("search call = %#v", searchCall)
|
||||
}
|
||||
if _, exists := searchCall.args[tt.scopeParam]; exists {
|
||||
t.Fatalf("global fallback unexpectedly forwarded %s: %#v", tt.scopeParam, searchCall.args)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestNativeScopedSearchInvalidCIDStopsBeforeSearch(t *testing.T) {
|
||||
caller := &chatMessageSearchCaller{failPreflight: true}
|
||||
_, err := executeNativeScopedSearch(t, caller,
|
||||
"message", "search-advanced", "--query", "周报", "--conversation-ids", "cid-invalid")
|
||||
if err == nil {
|
||||
t.Fatal("invalid CID unexpectedly succeeded")
|
||||
}
|
||||
var typed *apperrors.Error
|
||||
if !errors.As(err, &typed) || typed.Reason != "search_conversation_scope_invalid" {
|
||||
t.Fatalf("error = %#v", err)
|
||||
}
|
||||
if len(caller.calls) != 1 || caller.calls[0].toolName != "get_conversation_info" {
|
||||
t.Fatalf("calls = %#v", caller.calls)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNativeScopedSearchPreservesPreflightAuthError(t *testing.T) {
|
||||
want := &CLIError{Code: CodeAuthNotConfigured, Message: "当前未登录"}
|
||||
caller := &chatMessageSearchCaller{preflightError: want}
|
||||
_, err := executeNativeScopedSearch(t, caller,
|
||||
"message", "search-advanced", "--query", "周报", "--conversation-ids", "cid-target")
|
||||
if err == nil {
|
||||
t.Fatal("auth failure unexpectedly succeeded")
|
||||
}
|
||||
var cliErr *CLIError
|
||||
if !errors.As(err, &cliErr) || cliErr.Code != CodeAuthNotConfigured {
|
||||
t.Fatalf("error = %#v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCrossPlatformCoverageNativeScopedSearchPreservesAmbiguousMCPToolErrors(t *testing.T) {
|
||||
for _, test := range []struct {
|
||||
name string
|
||||
want *CLIError
|
||||
}{
|
||||
{
|
||||
name: "rate limited",
|
||||
want: &CLIError{
|
||||
Code: CodeMCPToolError,
|
||||
Message: `{"success":false,"errorCode":"invalidRequest.rateLimited","errorMsg":"slow down"}`,
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "permission denied",
|
||||
want: &CLIError{
|
||||
Code: CodeMCPToolError,
|
||||
Message: `{"success":false,"errorCode":"forbidden.noPermission","errorMsg":"permission denied"}`,
|
||||
},
|
||||
},
|
||||
} {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
caller := &chatMessageSearchCaller{preflightError: test.want}
|
||||
_, err := executeNativeScopedSearch(t, caller,
|
||||
"message", "search-advanced", "--query", "周报", "--conversation-ids", "cid-target")
|
||||
if err != test.want {
|
||||
t.Fatalf("error = %#v, want original %#v", err, test.want)
|
||||
}
|
||||
if len(caller.calls) != 1 || caller.calls[0].toolName != "get_conversation_info" {
|
||||
t.Fatalf("calls = %#v", caller.calls)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestNativeScopedSearchScansUntilTargetConversationAppears(t *testing.T) {
|
||||
caller := &chatMessageSearchCaller{searchResponses: []string{
|
||||
`{"result":{"conversationMessagesList":[{"openConversationId":"cid-other","messages":[{"openMessageId":"m-other"}]}],"hasMore":true,"nextCursor":"c2"}}`,
|
||||
`{"result":{"conversationMessagesList":[{"openConversationId":"cid-target","messages":[{"openMessageId":"m-target"}]}],"hasMore":false}}`,
|
||||
}}
|
||||
payload, err := executeNativeScopedSearch(t, caller,
|
||||
"message", "search-advanced", "--query", "周报", "--conversation-ids", "cid-target")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
result, _ := payload["result"].(map[string]any)
|
||||
if result["pagesFetched"] != float64(2) || result["complete"] != true {
|
||||
t.Fatalf("result = %#v", result)
|
||||
}
|
||||
groups, _ := result["conversationMessagesList"].([]any)
|
||||
if len(groups) != 1 {
|
||||
t.Fatalf("groups = %#v", groups)
|
||||
}
|
||||
searchCalls := make([]chatMessageSearchCall, 0, 2)
|
||||
for _, call := range caller.calls {
|
||||
if call.toolName == "search_messages" {
|
||||
searchCalls = append(searchCalls, call)
|
||||
}
|
||||
}
|
||||
if len(searchCalls) != 2 || searchCalls[1].args["cursor"] != "c2" {
|
||||
t.Fatalf("search calls = %#v", searchCalls)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCrossPlatformCoverageNativeScopedSearchPageAllOptions(t *testing.T) {
|
||||
t.Run("page limit preserves continuation", func(t *testing.T) {
|
||||
caller := &chatMessageSearchCaller{searchResponse: `{
|
||||
"result": {
|
||||
"conversationMessagesList": [
|
||||
{"openConversationId":"cid-target","messages":[{"openMessageId":"m1"}]}
|
||||
],
|
||||
"hasMore": true,
|
||||
"nextCursor": "c2"
|
||||
}
|
||||
}`}
|
||||
payload, err := executeNativeScopedSearch(t, caller,
|
||||
"message", "search-advanced", "--query", "周报", "--conversation-ids", "cid-target",
|
||||
"--page-all", "--page-limit", "1", "--page-delay", "0")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
paging, _ := payload["paging"].(map[string]any)
|
||||
if paging["pages"] != float64(1) || paging["total"] != float64(1) || paging["truncated"] != true {
|
||||
t.Fatalf("paging = %#v", paging)
|
||||
}
|
||||
if caller.searchCalls != 1 {
|
||||
t.Fatalf("search calls = %d, want 1", caller.searchCalls)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("max items truncates within filtered page", func(t *testing.T) {
|
||||
caller := &chatMessageSearchCaller{searchResponse: `{
|
||||
"result": {
|
||||
"conversationMessagesList": [
|
||||
{"openConversationId":"cid-target","messages":[
|
||||
{"openMessageId":"m1"},
|
||||
{"openMessageId":"m2"}
|
||||
]}
|
||||
],
|
||||
"hasMore": false
|
||||
}
|
||||
}`}
|
||||
payload, err := executeNativeScopedSearch(t, caller,
|
||||
"message", "search-advanced", "--query", "周报", "--conversation-ids", "cid-target",
|
||||
"--page-all", "--max-items", "1", "--page-delay", "0")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
result, _ := payload["result"].(map[string]any)
|
||||
groups, _ := result["conversationMessagesList"].([]any)
|
||||
group, _ := groups[0].(map[string]any)
|
||||
messages, _ := group["messages"].([]any)
|
||||
if len(messages) != 1 {
|
||||
t.Fatalf("messages = %#v", messages)
|
||||
}
|
||||
paging, _ := payload["paging"].(map[string]any)
|
||||
if paging["total"] != float64(1) || paging["truncatedWithinPage"] != true || paging["resumeCursorReliable"] != false {
|
||||
t.Fatalf("paging = %#v", paging)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestNativeScopedSearchMissingConversationIdentityFailsClosed(t *testing.T) {
|
||||
caller := &chatMessageSearchCaller{searchResponse: `{"result":{"messages":[{"openMessageId":"m1"}],"hasMore":false}}`}
|
||||
_, err := executeNativeScopedSearch(t, caller,
|
||||
"message", "search-advanced", "--query", "周报", "--conversation-ids", "cid-target")
|
||||
if err == nil {
|
||||
t.Fatal("unverifiable scoped result unexpectedly succeeded")
|
||||
}
|
||||
var typed *apperrors.Error
|
||||
if !errors.As(err, &typed) || typed.Reason != "search_conversation_scope_unverified" {
|
||||
t.Fatalf("error = %#v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNativeScopedSearchValidEmptyResultIsComplete(t *testing.T) {
|
||||
caller := &chatMessageSearchCaller{searchResponse: `{"result":{"messages":[],"hasMore":false}}`}
|
||||
payload, err := executeNativeScopedSearch(t, caller,
|
||||
"message", "search-advanced", "--query", "周报", "--conversation-ids", "cid-empty")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
result, _ := payload["result"].(map[string]any)
|
||||
if result["complete"] != true || result["hasMore"] != false {
|
||||
t.Fatalf("result = %#v", result)
|
||||
}
|
||||
scope, _ := payload["scope"].(map[string]any)
|
||||
if scope["targetsValidated"] != true || scope["sourceComplete"] != true {
|
||||
t.Fatalf("scope = %#v", scope)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCrossPlatformCoverageNativeScopedSearchFailureAndPaginationBranches(t *testing.T) {
|
||||
t.Run("empty scope uses the native search call", func(t *testing.T) {
|
||||
caller := &chatMessageSearchCaller{}
|
||||
previousDeps := deps
|
||||
t.Cleanup(func() { deps = previousDeps })
|
||||
InitDeps(caller)
|
||||
deps.Out.w = io.Discard
|
||||
|
||||
cmd := &cobra.Command{Use: "search"}
|
||||
err := runConversationScopedMessageSearch(
|
||||
cmd,
|
||||
"im",
|
||||
"search_messages",
|
||||
"openConversationIds",
|
||||
map[string]any{"keyword": "周报"},
|
||||
[]string{"", " "},
|
||||
)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(caller.calls) != 1 || caller.calls[0].toolName != "search_messages" {
|
||||
t.Fatalf("calls = %#v", caller.calls)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("invalid page options fail before preflight", func(t *testing.T) {
|
||||
caller := &chatMessageSearchCaller{}
|
||||
previousDeps := deps
|
||||
t.Cleanup(func() { deps = previousDeps })
|
||||
InitDeps(caller)
|
||||
|
||||
cmd := &cobra.Command{Use: "search"}
|
||||
AddPagedMCPFlags(cmd)
|
||||
if err := cmd.Flags().Set("page-all", "true"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := cmd.Flags().Set("page-limit", "0"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
err := runConversationScopedMessageSearch(
|
||||
cmd,
|
||||
"im",
|
||||
"search_messages",
|
||||
"openConversationIds",
|
||||
map[string]any{"keyword": "周报"},
|
||||
[]string{"cid-target"},
|
||||
)
|
||||
if err == nil || !strings.Contains(err.Error(), "--page-limit must be between 1 and 500") {
|
||||
t.Fatalf("error = %v", err)
|
||||
}
|
||||
if len(caller.calls) != 0 {
|
||||
t.Fatalf("invalid paging made calls: %#v", caller.calls)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("cancelled context interrupts page delay", func(t *testing.T) {
|
||||
caller := &chatMessageSearchCaller{searchResponse: `{
|
||||
"result": {
|
||||
"messages": [{"openMessageId":"m1","openConversationId":"cid-target"}],
|
||||
"hasMore": true,
|
||||
"nextCursor": "c2"
|
||||
}
|
||||
}`}
|
||||
previousDeps := deps
|
||||
t.Cleanup(func() { deps = previousDeps })
|
||||
InitDeps(caller)
|
||||
deps.Out.w = io.Discard
|
||||
|
||||
cmd := &cobra.Command{Use: "search"}
|
||||
AddPagedMCPFlags(cmd)
|
||||
if err := cmd.Flags().Set("page-all", "true"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := cmd.Flags().Set("page-delay", "60000"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
cancel()
|
||||
cmd.SetContext(ctx)
|
||||
err := runConversationScopedMessageSearch(
|
||||
cmd,
|
||||
"im",
|
||||
"search_messages",
|
||||
"openConversationIds",
|
||||
map[string]any{"keyword": "周报", "limit": 100, "cursor": "0"},
|
||||
[]string{"cid-target"},
|
||||
)
|
||||
if !errors.Is(err, context.Canceled) {
|
||||
t.Fatalf("error = %v, want context canceled", err)
|
||||
}
|
||||
if caller.searchCalls != 1 {
|
||||
t.Fatalf("search calls = %d, want 1", caller.searchCalls)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("lower search error", func(t *testing.T) {
|
||||
caller := &chatMessageSearchCaller{searchError: errors.New("search unavailable")}
|
||||
_, err := executeNativeScopedSearch(t, caller,
|
||||
"message", "search-advanced", "--query", "周报", "--conversation-ids", "cid-target")
|
||||
if err == nil {
|
||||
t.Fatal("lower search error was ignored")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("stalled cursor fails closed", func(t *testing.T) {
|
||||
caller := &chatMessageSearchCaller{searchResponse: `{
|
||||
"result": {
|
||||
"messages": [{"openMessageId":"m1","openConversationId":"cid-target"}],
|
||||
"hasMore": true
|
||||
}
|
||||
}`}
|
||||
_, err := executeNativeScopedSearch(t, caller,
|
||||
"message", "search-advanced", "--query", "周报", "--conversation-ids", "cid-target")
|
||||
var typed *apperrors.Error
|
||||
if !errors.As(err, &typed) || typed.Reason != "search_conversation_scope_cursor_stalled" {
|
||||
t.Fatalf("error = %#v", err)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("result limit preserves continuation", func(t *testing.T) {
|
||||
caller := &chatMessageSearchCaller{searchResponse: `{
|
||||
"result": {
|
||||
"messages": [{"openMessageId":"m1","openConversationId":"cid-target"}],
|
||||
"hasMore": true,
|
||||
"nextCursor": "c2"
|
||||
}
|
||||
}`}
|
||||
payload, err := executeNativeScopedSearch(t, caller,
|
||||
"message", "search-advanced", "--query", "周报", "--conversation-ids", "cid-target", "--limit", "1")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
result, _ := payload["result"].(map[string]any)
|
||||
if result["complete"] != false || result["hasMore"] != true || result["nextCursor"] != "c2" {
|
||||
t.Fatalf("result = %#v", result)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("duplicate message ids are removed across pages", func(t *testing.T) {
|
||||
caller := &chatMessageSearchCaller{searchResponses: []string{
|
||||
`{"result":{"messages":[{"openMessageId":"m1","openConversationId":"cid-target"}],"hasMore":true,"nextCursor":"c2"}}`,
|
||||
`{"result":{"messages":[{"openMessageId":"m1","openConversationId":"cid-target"},{"openMessageId":"m2","openConversationId":"cid-target"}],"hasMore":false}}`,
|
||||
}}
|
||||
payload, err := executeNativeScopedSearch(t, caller,
|
||||
"message", "search-advanced", "--query", "周报", "--conversation-ids", "cid-target")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
result, _ := payload["result"].(map[string]any)
|
||||
groups, _ := result["conversationMessagesList"].([]any)
|
||||
group, _ := groups[0].(map[string]any)
|
||||
messages, _ := group["messages"].([]any)
|
||||
if len(messages) != 2 {
|
||||
t.Fatalf("deduplicated messages = %#v", messages)
|
||||
}
|
||||
})
|
||||
|
||||
if got := uniqueNonEmptyStrings([]string{" cid ", "", "cid"}); !reflect.DeepEqual(got, []string{"cid"}) {
|
||||
t.Fatalf("uniqueNonEmptyStrings = %#v", got)
|
||||
}
|
||||
for _, test := range []struct {
|
||||
value any
|
||||
want int
|
||||
}{
|
||||
{value: int64(7), want: 7},
|
||||
{value: json.Number("8"), want: 8},
|
||||
{value: float64(9), want: 9},
|
||||
{value: int64(0), want: 11},
|
||||
} {
|
||||
if got := positiveSearchLimit(test.value, 11); got != test.want {
|
||||
t.Errorf("positiveSearchLimit(%#v) = %d, want %d", test.value, got, test.want)
|
||||
}
|
||||
}
|
||||
if cleanSearchCursor(nil) != "" || cleanSearchCursor(" null ") != "" || cleanSearchCursor(" c2 ") != "c2" {
|
||||
t.Fatal("cleanSearchCursor did not normalize sentinel values")
|
||||
}
|
||||
}
|
||||
|
||||
func TestCrossPlatformCoverageNativeScopedSearchDryRunShowsCompositePlanWithoutCallingTools(t *testing.T) {
|
||||
caller := &chatMessageSearchCaller{}
|
||||
previousDeps := deps
|
||||
t.Cleanup(func() { deps = previousDeps })
|
||||
InitDeps(caller)
|
||||
cmd := &cobra.Command{Use: "search"}
|
||||
cmd.Flags().Bool("dry-run", true, "")
|
||||
AddPagedMCPFlags(cmd)
|
||||
for name, value := range map[string]string{
|
||||
"page-all": "true",
|
||||
"page-limit": "7",
|
||||
"max-items": "9",
|
||||
"page-delay": "11",
|
||||
} {
|
||||
if err := cmd.Flags().Set(name, value); err != nil {
|
||||
t.Fatalf("set --%s: %v", name, err)
|
||||
}
|
||||
}
|
||||
var output strings.Builder
|
||||
cmd.SetOut(&output)
|
||||
err := runConversationScopedMessageSearch(
|
||||
cmd,
|
||||
"im",
|
||||
"search_messages",
|
||||
"openConversationIds",
|
||||
map[string]any{
|
||||
"keyword": "周报",
|
||||
"openConversationIds": []string{"cid-target"},
|
||||
"limit": 100,
|
||||
"cursor": "0",
|
||||
},
|
||||
[]string{"cid-target"},
|
||||
)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(caller.calls) != 0 {
|
||||
t.Fatalf("dry-run made calls: %#v", caller.calls)
|
||||
}
|
||||
var payload map[string]any
|
||||
if err := json.Unmarshal([]byte(output.String()), &payload); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
plan, _ := payload["plan"].([]any)
|
||||
if payload["dry_run"] != true || payload["executed"] != false || len(plan) != 3 {
|
||||
t.Fatalf("payload = %#v", payload)
|
||||
}
|
||||
searchStage, _ := plan[1].(map[string]any)
|
||||
arguments, _ := searchStage["arguments"].(map[string]any)
|
||||
if _, exists := arguments["openConversationIds"]; exists {
|
||||
t.Fatalf("dry-run global search still carries scope: %#v", searchStage)
|
||||
}
|
||||
if searchStage["pageAll"] != true ||
|
||||
searchStage["pageLimit"] != float64(7) ||
|
||||
searchStage["maxItems"] != float64(9) ||
|
||||
searchStage["pageDelay"] != float64(11) {
|
||||
t.Fatalf("dry-run paging = %#v", searchStage)
|
||||
}
|
||||
}
|
||||
|
||||
type chatChangedContractCaller struct {
|
||||
calls []chatMessageSearchCall
|
||||
resolveUsers bool
|
||||
@@ -160,9 +701,12 @@ func executeChatChangedContract(t *testing.T, caller *chatChangedContractCaller,
|
||||
InitDeps(caller)
|
||||
deps.Out.w = io.Discard
|
||||
cmd := newChatCommand()
|
||||
if cmd.PersistentFlags().Lookup("yes") == nil {
|
||||
cmd.PersistentFlags().Bool("yes", false, "skip confirmation")
|
||||
}
|
||||
cmd.SilenceErrors = true
|
||||
cmd.SilenceUsage = true
|
||||
cmd.SetArgs(args)
|
||||
cmd.SetArgs(append(append([]string(nil), args...), "--yes"))
|
||||
return cmd.Execute()
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,123 @@
|
||||
// Copyright 2026 Alibaba Group
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package helpers
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
apperrors "github.com/DingTalk-Real-AI/dingtalk-workspace-cli/internal/errors"
|
||||
)
|
||||
|
||||
// NormalizeSearchConversationScopeError maps only errors that prove the
|
||||
// requested conversation does not exist or that openConversationId itself is
|
||||
// invalid. Unknown MCP tool failures must remain unchanged: the legacy
|
||||
// CodeMCPToolError bucket also carries permission, throttling, and transient
|
||||
// backend failures, none of which proves that the caller supplied a bad CID.
|
||||
func NormalizeSearchConversationScopeError(conversationID string, err error) error {
|
||||
if err == nil || !isDefinitiveInvalidSearchConversationError(err) {
|
||||
return err
|
||||
}
|
||||
return apperrors.NewValidation(
|
||||
fmt.Sprintf("无法验证会话 CID %q;已停止搜索,避免过滤失效后返回其他会话消息", conversationID),
|
||||
apperrors.WithReason("search_conversation_scope_invalid"),
|
||||
apperrors.WithDetails(map[string]any{"conversationId": conversationID}),
|
||||
apperrors.WithRetryable(false),
|
||||
apperrors.WithHint("确认 openConversationId 存在且当前账号可访问后重试"),
|
||||
apperrors.WithCause(err),
|
||||
)
|
||||
}
|
||||
|
||||
func isDefinitiveInvalidSearchConversationError(err error) bool {
|
||||
var cliErr *CLIError
|
||||
if errors.As(err, &cliErr) {
|
||||
switch cliErr.Code {
|
||||
case CodeResourceNotFound, CodeInvalidParam:
|
||||
return true
|
||||
}
|
||||
code, message := searchConversationErrorFacts(cliErr.Message)
|
||||
if isExplicitInvalidConversationCode(code) || isConversationParameterError(code, message) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
// Transport errors can survive below a legacy CLIError in the cause chain.
|
||||
// Inspect their structured diagnostics, but require conversation-specific
|
||||
// evidence before treating a generic PARAM_ERROR as an invalid CID.
|
||||
var appErr *apperrors.Error
|
||||
if errors.As(err, &appErr) {
|
||||
code := strings.TrimSpace(appErr.ServerDiag.ServerErrorCode)
|
||||
message := strings.Join([]string{
|
||||
appErr.Message,
|
||||
appErr.ServerDiag.TechnicalDetail,
|
||||
appErr.Reason,
|
||||
appErr.FailureStage,
|
||||
}, " ")
|
||||
return isExplicitInvalidConversationCode(code) || isConversationParameterError(code, message)
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func searchConversationErrorFacts(raw string) (string, string) {
|
||||
var body map[string]any
|
||||
if json.Unmarshal([]byte(raw), &body) != nil {
|
||||
return "", raw
|
||||
}
|
||||
code := firstSearchConversationErrorString(body, "errorCode", "error_code", "code")
|
||||
message := firstSearchConversationErrorString(body, "errorMsg", "error_msg", "message", "error")
|
||||
return code, message
|
||||
}
|
||||
|
||||
func firstSearchConversationErrorString(body map[string]any, keys ...string) string {
|
||||
for _, key := range keys {
|
||||
if value, ok := body[key].(string); ok && strings.TrimSpace(value) != "" {
|
||||
return strings.TrimSpace(value)
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func isExplicitInvalidConversationCode(code string) bool {
|
||||
switch strings.ToUpper(strings.TrimSpace(code)) {
|
||||
case "INVALID_OPEN_CONVERSATION_ID", "OPEN_CONVERSATION_NOT_FOUND", "CONVERSATION_NOT_FOUND":
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
func isConversationParameterError(code, message string) bool {
|
||||
normalizedCode := strings.ToUpper(strings.TrimSpace(code))
|
||||
if normalizedCode != "PARAM_ERROR" && normalizedCode != "PARAMETER_ERROR" && normalizedCode != "INVALID_ARGUMENT" {
|
||||
return false
|
||||
}
|
||||
normalizedMessage := strings.ToLower(strings.TrimSpace(message))
|
||||
mentionsConversationID := strings.Contains(normalizedMessage, "openconversationid") ||
|
||||
strings.Contains(normalizedMessage, "open conversation id") ||
|
||||
strings.Contains(normalizedMessage, "conversation id") ||
|
||||
strings.Contains(normalizedMessage, "cid")
|
||||
if !mentionsConversationID {
|
||||
return false
|
||||
}
|
||||
for _, marker := range []string{
|
||||
"invalid", "illegal", "malformed", "required", "missing", "not found", "不存在", "无效", "非法", "缺少", "必填",
|
||||
} {
|
||||
if strings.Contains(normalizedMessage, marker) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
@@ -0,0 +1,123 @@
|
||||
// Copyright 2026 Alibaba Group
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package helpers
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"testing"
|
||||
|
||||
apperrors "github.com/DingTalk-Real-AI/dingtalk-workspace-cli/internal/errors"
|
||||
)
|
||||
|
||||
func TestCrossPlatformCoverageNormalizeSearchConversationScopeError(t *testing.T) {
|
||||
if got := NormalizeSearchConversationScopeError("cid", nil); got != nil {
|
||||
t.Fatalf("nil error normalized to %#v", got)
|
||||
}
|
||||
|
||||
invalidCases := []struct {
|
||||
name string
|
||||
err error
|
||||
}{
|
||||
{
|
||||
name: "classified resource not found",
|
||||
err: &CLIError{Code: CodeResourceNotFound, Message: "conversation not found"},
|
||||
},
|
||||
{
|
||||
name: "explicit conversation error code",
|
||||
err: &CLIError{
|
||||
Code: CodeMCPToolError,
|
||||
Message: `{"success":false,"errorCode":"INVALID_OPEN_CONVERSATION_ID","errorMsg":"invalid conversation"}`,
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "conversation specific parameter error",
|
||||
err: &CLIError{
|
||||
Code: CodeMCPToolError,
|
||||
Message: `{"success":false,"errorCode":"PARAM_ERROR","errorMsg":"openConversationId is invalid"}`,
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "transport diagnostic proves invalid conversation",
|
||||
err: apperrors.NewAPI(
|
||||
"conversation validation failed",
|
||||
apperrors.WithServerDiag(apperrors.ServerDiagnostics{
|
||||
ServerErrorCode: "INVALID_OPEN_CONVERSATION_ID",
|
||||
}),
|
||||
),
|
||||
},
|
||||
}
|
||||
for _, test := range invalidCases {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
got := NormalizeSearchConversationScopeError("cid-invalid", test.err)
|
||||
var typed *apperrors.Error
|
||||
if !errors.As(got, &typed) || typed.Reason != "search_conversation_scope_invalid" {
|
||||
t.Fatalf("normalized error = %#v", got)
|
||||
}
|
||||
if !typed.RetryableSet || typed.Retryable {
|
||||
t.Fatalf("retryable = (%t, set=%t), want false and set", typed.Retryable, typed.RetryableSet)
|
||||
}
|
||||
if typed.Details["conversationId"] != "cid-invalid" || !errors.Is(got, test.err) {
|
||||
t.Fatalf("normalized error lost details or cause: %#v", typed)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
preservedCases := []struct {
|
||||
name string
|
||||
err error
|
||||
}{
|
||||
{
|
||||
name: "rate limit",
|
||||
err: &CLIError{
|
||||
Code: CodeMCPToolError,
|
||||
Message: `{"success":false,"errorCode":"invalidRequest.rateLimited","errorMsg":"slow down","retryable":true}`,
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "permission denied",
|
||||
err: &CLIError{
|
||||
Code: CodeMCPToolError,
|
||||
Message: `{"success":false,"errorCode":"forbidden.noPermission","errorMsg":"permission denied"}`,
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "generic parameter error without CID evidence",
|
||||
err: &CLIError{
|
||||
Code: CodeMCPToolError,
|
||||
Message: `{"success":false,"errorCode":"PARAM_ERROR","errorMsg":"未找到指定工具"}`,
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "structured error without recognized facts",
|
||||
err: &CLIError{
|
||||
Code: CodeMCPToolError,
|
||||
Message: `{"success":false,"retryable":true}`,
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "parameter error mentions CID without invalid evidence",
|
||||
err: &CLIError{
|
||||
Code: CodeMCPToolError,
|
||||
Message: `{"success":false,"errorCode":"PARAM_ERROR","errorMsg":"openConversationId could not be processed"}`,
|
||||
},
|
||||
},
|
||||
}
|
||||
for _, test := range preservedCases {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
if got := NormalizeSearchConversationScopeError("cid-target", test.err); got != test.err {
|
||||
t.Fatalf("error = %#v, want original %#v", got, test.err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -101,7 +101,7 @@ func TestCrossPlatformCoverageChatMessageSendLocation(t *testing.T) {
|
||||
caller = &guardedMutationCaller{}
|
||||
err = executeGuardedMutationCommand(t, caller, newChatCommand,
|
||||
"message", "send", "--group", "cid1", "--msg-type", "location",
|
||||
"--latitude", "39.9", "--longitude", "116.4", "--location-name", "国贸", "--map-thumbnail-url", "@media1")
|
||||
"--latitude", "39.9", "--longitude", "116.4", "--location-name", "国贸", "--map-thumbnail-url", "@media1", "--yes")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -128,7 +128,7 @@ func TestCrossPlatformCoverageChatMessageSendProfile(t *testing.T) {
|
||||
|
||||
caller = &guardedMutationCaller{}
|
||||
err = executeGuardedMutationCommand(t, caller, newChatCommand,
|
||||
"message", "send", "--group", "cid1", "--msg-type", "profile", "--contact-id", "od123")
|
||||
"message", "send", "--group", "cid1", "--msg-type", "profile", "--contact-id", "od123", "--yes")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
+33
-5
@@ -3668,6 +3668,17 @@ CLI 内部自动完成全部流程:
|
||||
}
|
||||
|
||||
if deps.Caller.DryRun() {
|
||||
if strings.EqualFold(strings.TrimSpace(deps.Caller.Format()), "json") {
|
||||
return deps.Out.PrintJSON(map[string]any{
|
||||
"dry_run": true,
|
||||
"executed": false,
|
||||
"preview_kind": "plan",
|
||||
"operation": "doc_export",
|
||||
"nodeId": node,
|
||||
"exportFormat": format,
|
||||
"savedPath": outputPath,
|
||||
})
|
||||
}
|
||||
deps.Out.PrintKeyValue("操作", "导出文档(提交+轮询+下载)")
|
||||
deps.Out.PrintKeyValue("文档", node)
|
||||
deps.Out.PrintKeyValue("输出", outputPath)
|
||||
@@ -3678,7 +3689,7 @@ CLI 内部自动完成全部流程:
|
||||
ctx := context.Background()
|
||||
|
||||
// ── Step 1: 提交导出任务 ──
|
||||
deps.Out.PrintInfo("[1/3] 提交导出任务...")
|
||||
printJSONSafeInfo("[1/3] 提交导出任务...")
|
||||
submitText, err := callMCPToolReturnText(ctx, "submit_export_job", submitArgs)
|
||||
if err != nil {
|
||||
return fmt.Errorf("提交导出任务失败: %w", err)
|
||||
@@ -3693,10 +3704,10 @@ CLI 内部自动完成全部流程:
|
||||
deps.Out.PrintRaw(submitText)
|
||||
return fmt.Errorf("提交导出任务成功但未返回 jobId")
|
||||
}
|
||||
deps.Out.PrintInfo(fmt.Sprintf(" 任务已提交,jobId: %s", jobID))
|
||||
printJSONSafeInfo(fmt.Sprintf(" 任务已提交,jobId: %s", jobID))
|
||||
|
||||
// ── Step 2: 渐进式退避轮询 ──
|
||||
deps.Out.PrintInfo("[2/3] 等待导出完成...")
|
||||
printJSONSafeInfo("[2/3] 等待导出完成...")
|
||||
downloadURL, err := pollDocExportJob(ctx, jobID)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -3715,11 +3726,28 @@ CLI 内部自动完成全部流程:
|
||||
outputPath = filepath.Join(outputPath, filename)
|
||||
}
|
||||
|
||||
deps.Out.PrintInfo(fmt.Sprintf("[3/3] 下载文件到 %s ...", outputPath))
|
||||
printJSONSafeInfo(fmt.Sprintf("[3/3] 下载文件到 %s ...", outputPath))
|
||||
if err := httpGetFile(ctx, downloadURL, nil, outputPath); err != nil {
|
||||
return fmt.Errorf("文件下载失败 (jobId=%s): %w", jobID, err)
|
||||
}
|
||||
|
||||
if strings.EqualFold(strings.TrimSpace(deps.Caller.Format()), "json") {
|
||||
info, err := os.Stat(outputPath)
|
||||
if err != nil {
|
||||
return fmt.Errorf("读取导出产物信息失败 (jobId=%s): %w", jobID, err)
|
||||
}
|
||||
return deps.Out.PrintJSON(map[string]any{
|
||||
"success": true,
|
||||
"nodeId": node,
|
||||
"exportFormat": format,
|
||||
"jobId": jobID,
|
||||
"taskId": jobID,
|
||||
"status": "SUCCESS",
|
||||
"savedPath": outputPath,
|
||||
"sizeBytes": info.Size(),
|
||||
})
|
||||
}
|
||||
|
||||
deps.Out.PrintInfo(fmt.Sprintf("导出完成: %s", outputPath))
|
||||
return nil
|
||||
},
|
||||
@@ -4657,7 +4685,7 @@ func pollDocExportJob(ctx context.Context, jobID string) (downloadURL string, er
|
||||
|
||||
for attempt := 1; attempt <= maxPolls; attempt++ {
|
||||
interval := pollInterval(attempt)
|
||||
deps.Out.PrintInfo(fmt.Sprintf(" 第 %d/%d 次查询,等待 %v ...", attempt, maxPolls, interval))
|
||||
printJSONSafeInfo(fmt.Sprintf(" 第 %d/%d 次查询,等待 %v ...", attempt, maxPolls, interval))
|
||||
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
|
||||
@@ -619,10 +619,20 @@ func newDriveCommand() *cobra.Command {
|
||||
return err
|
||||
}
|
||||
dlOpts.logf = func(format string, a ...any) {
|
||||
deps.Out.PrintInfo(fmt.Sprintf(format, a...))
|
||||
printJSONSafeInfo(fmt.Sprintf(format, a...))
|
||||
}
|
||||
|
||||
if deps.Caller.DryRun() {
|
||||
if strings.EqualFold(strings.TrimSpace(deps.Caller.Format()), "json") {
|
||||
return deps.Out.PrintJSON(map[string]any{
|
||||
"dry_run": true,
|
||||
"executed": false,
|
||||
"preview_kind": "plan",
|
||||
"operation": "drive_download",
|
||||
"nodeId": fileID,
|
||||
"savedPath": outputPath,
|
||||
})
|
||||
}
|
||||
deps.Out.PrintKeyValue("操作", "下载钉盘文件")
|
||||
deps.Out.PrintKeyValue("文件ID", fileID)
|
||||
deps.Out.PrintKeyValue("输出", outputPath)
|
||||
@@ -632,7 +642,7 @@ func newDriveCommand() *cobra.Command {
|
||||
ctx := cmd.Context()
|
||||
|
||||
// Step 1: 获取下载 URL 和签名请求头
|
||||
deps.Out.PrintInfo("[1/2] 获取下载链接...")
|
||||
printJSONSafeInfo("[1/2] 获取下载链接...")
|
||||
text, err := callMCPToolReturnText(ctx, "download_file", argsMap)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -654,7 +664,7 @@ func newDriveCommand() *cobra.Command {
|
||||
}
|
||||
|
||||
// Step 2: 分片下载(自动分派 + 401/403 凭证刷新重试)
|
||||
deps.Out.PrintInfo(fmt.Sprintf("[2/2] 下载文件到 %s ...", outputPath))
|
||||
printJSONSafeInfo(fmt.Sprintf("[2/2] 下载文件到 %s ...", outputPath))
|
||||
dlOpts.knownSize = parseDownloadFileSize(text)
|
||||
dlOpts.nodeID = fileID
|
||||
dlOpts.version = parseDownloadFileVersion(text)
|
||||
@@ -684,6 +694,19 @@ func newDriveCommand() *cobra.Command {
|
||||
return err
|
||||
}
|
||||
|
||||
if strings.EqualFold(strings.TrimSpace(deps.Caller.Format()), "json") {
|
||||
info, err := os.Stat(outputPath)
|
||||
if err != nil {
|
||||
return fmt.Errorf("读取下载产物信息失败: %w", err)
|
||||
}
|
||||
return deps.Out.PrintJSON(map[string]any{
|
||||
"success": true,
|
||||
"nodeId": fileID,
|
||||
"version": dlOpts.version,
|
||||
"savedPath": outputPath,
|
||||
"sizeBytes": info.Size(),
|
||||
})
|
||||
}
|
||||
deps.Out.PrintInfo(fmt.Sprintf("下载完成: %s", outputPath))
|
||||
return nil
|
||||
},
|
||||
@@ -772,10 +795,21 @@ func newDriveCommand() *cobra.Command {
|
||||
return err
|
||||
}
|
||||
dlOpts.logf = func(format string, a ...any) {
|
||||
deps.Out.PrintInfo(fmt.Sprintf(format, a...))
|
||||
printJSONSafeInfo(fmt.Sprintf(format, a...))
|
||||
}
|
||||
|
||||
if deps.Caller.DryRun() {
|
||||
if strings.EqualFold(strings.TrimSpace(deps.Caller.Format()), "json") {
|
||||
return deps.Out.PrintJSON(map[string]any{
|
||||
"dry_run": true,
|
||||
"executed": false,
|
||||
"preview_kind": "plan",
|
||||
"operation": "drive_download_version",
|
||||
"nodeId": fileID,
|
||||
"version": versionNum,
|
||||
"savedPath": outputPath,
|
||||
})
|
||||
}
|
||||
deps.Out.PrintKeyValue("操作", "下载文件历史版本")
|
||||
deps.Out.PrintKeyValue("节点ID", fileID)
|
||||
deps.Out.PrintKeyValue("版本号", fmt.Sprintf("%d", versionNum))
|
||||
@@ -784,7 +818,7 @@ func newDriveCommand() *cobra.Command {
|
||||
}
|
||||
|
||||
ctx := cmd.Context()
|
||||
deps.Out.PrintInfo("[1/2] 获取历史版本下载链接...")
|
||||
printJSONSafeInfo("[1/2] 获取历史版本下载链接...")
|
||||
dlArgsMap := map[string]any{
|
||||
"nodeId": fileID,
|
||||
"version": versionNum,
|
||||
@@ -804,7 +838,7 @@ func newDriveCommand() *cobra.Command {
|
||||
}
|
||||
outputPath = filepath.Join(outputPath, filename)
|
||||
}
|
||||
deps.Out.PrintInfo(fmt.Sprintf("[2/2] 下载文件到 %s ...", outputPath))
|
||||
printJSONSafeInfo(fmt.Sprintf("[2/2] 下载文件到 %s ...", outputPath))
|
||||
dlOpts.knownSize = parseDownloadFileSize(text)
|
||||
dlOpts.nodeID = fileID
|
||||
dlOpts.version = versionNum
|
||||
@@ -833,6 +867,19 @@ func newDriveCommand() *cobra.Command {
|
||||
}
|
||||
return err
|
||||
}
|
||||
if strings.EqualFold(strings.TrimSpace(deps.Caller.Format()), "json") {
|
||||
info, err := os.Stat(outputPath)
|
||||
if err != nil {
|
||||
return fmt.Errorf("读取下载产物信息失败: %w", err)
|
||||
}
|
||||
return deps.Out.PrintJSON(map[string]any{
|
||||
"success": true,
|
||||
"nodeId": fileID,
|
||||
"version": versionNum,
|
||||
"savedPath": outputPath,
|
||||
"sizeBytes": info.Size(),
|
||||
})
|
||||
}
|
||||
deps.Out.PrintInfo(fmt.Sprintf("下载完成: %s", outputPath))
|
||||
return nil
|
||||
},
|
||||
|
||||
@@ -0,0 +1,198 @@
|
||||
// Copyright 2026 Alibaba Group
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package helpers
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/DingTalk-Real-AI/dingtalk-workspace-cli/internal/testseam"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
func executeJSONOutputContractCommand(t *testing.T, caller *scriptedToolCaller, build func() *cobra.Command, args ...string) (string, string, error) {
|
||||
t.Helper()
|
||||
testseam.Protect(t, &deps)
|
||||
testseam.Protect(t, &os.Args)
|
||||
InitDeps(caller)
|
||||
var stdout, stderr bytes.Buffer
|
||||
deps.Out.w = &stdout
|
||||
deps.Out.errW = &stderr
|
||||
|
||||
root := build()
|
||||
installExampleGlobalFlags(root)
|
||||
os.Args = append([]string{"dws", root.Name()}, args...)
|
||||
root.SilenceErrors = true
|
||||
root.SilenceUsage = true
|
||||
root.SetArgs(args)
|
||||
if err := root.Execute(); err != nil {
|
||||
return stdout.String(), stderr.String(), err
|
||||
}
|
||||
return stdout.String(), stderr.String(), nil
|
||||
}
|
||||
|
||||
func assertJSONOutputPayload(t *testing.T, stdout string) map[string]any {
|
||||
t.Helper()
|
||||
var payload map[string]any
|
||||
if err := json.Unmarshal([]byte(stdout), &payload); err != nil {
|
||||
t.Fatalf("stdout is not JSON: %v\n%s", err, stdout)
|
||||
}
|
||||
return payload
|
||||
}
|
||||
|
||||
func TestCrossPlatformCoverageJSONOutputContractForCompletedFileTransfers(t *testing.T) {
|
||||
testseam.Swap(t, &httpGetFile, func(_ context.Context, _ string, _ map[string]string, destination string) error {
|
||||
return os.WriteFile(destination, []byte("payload"), 0o600)
|
||||
})
|
||||
|
||||
t.Run("drive latest download", func(t *testing.T) {
|
||||
outputPath := filepath.Join(t.TempDir(), "latest.txt")
|
||||
stdout, stderr, err := executeJSONOutputContractCommand(t,
|
||||
&scriptedToolCaller{format: "json", steps: []scriptedToolStep{{text: `{"downloadUrl":"https://example.test/latest.txt","fileSize":7,"version":9}`}}},
|
||||
newDriveCommand,
|
||||
"download", "--node", "node-latest", "--output", outputPath)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
payload := assertJSONOutputPayload(t, stdout)
|
||||
if payload["nodeId"] != "node-latest" || payload["savedPath"] != outputPath || payload["sizeBytes"] != float64(7) || payload["version"] != float64(9) {
|
||||
t.Fatalf("payload = %#v", payload)
|
||||
}
|
||||
if !strings.Contains(stderr, "下载完成") && !strings.Contains(stderr, "下载文件到") {
|
||||
t.Fatalf("expected progress on stderr, got %q", stderr)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("drive historical download through compatibility flag", func(t *testing.T) {
|
||||
outputPath := filepath.Join(t.TempDir(), "versioned.txt")
|
||||
stdout, _, err := executeJSONOutputContractCommand(t,
|
||||
&scriptedToolCaller{format: "json", steps: []scriptedToolStep{{text: `{"downloadUrl":"https://example.test/versioned.txt","fileSize":7}`}}},
|
||||
newDriveCommand,
|
||||
"download", "--node", "node-versioned", "--version", "4", "--output", outputPath)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
payload := assertJSONOutputPayload(t, stdout)
|
||||
if payload["nodeId"] != "node-versioned" || payload["version"] != float64(4) || payload["sizeBytes"] != float64(7) {
|
||||
t.Fatalf("payload = %#v", payload)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("doc export", func(t *testing.T) {
|
||||
testseam.Swap(t, &helperAfter, func(time.Duration) <-chan time.Time {
|
||||
ch := make(chan time.Time, 1)
|
||||
ch <- time.Now()
|
||||
return ch
|
||||
})
|
||||
outputPath := filepath.Join(t.TempDir(), "export.md")
|
||||
stdout, stderr, err := executeJSONOutputContractCommand(t,
|
||||
&scriptedToolCaller{format: "json", steps: []scriptedToolStep{
|
||||
{text: `{"jobId":"export-job-1"}`},
|
||||
{text: `{"status":"SUCCESS","downloadUrl":"https://example.test/export.md"}`},
|
||||
}},
|
||||
newDocCommand,
|
||||
"export", "--node", "doc-node", "--export-format", "markdown", "--output", outputPath)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
payload := assertJSONOutputPayload(t, stdout)
|
||||
if payload["nodeId"] != "doc-node" || payload["exportFormat"] != "markdown" || payload["jobId"] != "export-job-1" || payload["taskId"] != "export-job-1" || payload["status"] != "SUCCESS" || payload["sizeBytes"] != float64(7) {
|
||||
t.Fatalf("payload = %#v", payload)
|
||||
}
|
||||
if !strings.Contains(stderr, "提交导出任务") {
|
||||
t.Fatalf("expected export progress on stderr, got %q", stderr)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestCrossPlatformCoverageJSONOutputContractDryRunIsMachineReadable(t *testing.T) {
|
||||
stdout, _, err := executeJSONOutputContractCommand(t,
|
||||
&scriptedToolCaller{format: "json", dry: true},
|
||||
newDriveCommand,
|
||||
"download", "--node", "node-dry-run", "--output", filepath.Join(t.TempDir(), "out.txt"), "--dry-run")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
payload := assertJSONOutputPayload(t, stdout)
|
||||
if payload["dry_run"] != true || payload["executed"] != false || payload["nodeId"] != "node-dry-run" {
|
||||
t.Fatalf("payload = %#v", payload)
|
||||
}
|
||||
|
||||
stdout, _, err = executeJSONOutputContractCommand(t,
|
||||
&scriptedToolCaller{format: "json", dry: true},
|
||||
newDocCommand,
|
||||
"export", "--node", "doc-dry-run", "--export-format", "markdown", "--output", filepath.Join(t.TempDir(), "export.md"), "--dry-run")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
payload = assertJSONOutputPayload(t, stdout)
|
||||
if payload["dry_run"] != true || payload["executed"] != false || payload["nodeId"] != "doc-dry-run" || payload["operation"] != "doc_export" {
|
||||
t.Fatalf("payload = %#v", payload)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCrossPlatformCoverageJSONOutputContractReportsMissingLocalArtifact(t *testing.T) {
|
||||
testseam.Swap(t, &httpGetFile, func(context.Context, string, map[string]string, string) error {
|
||||
return nil
|
||||
})
|
||||
testseam.Swap(t, &helperAfter, func(time.Duration) <-chan time.Time {
|
||||
ch := make(chan time.Time, 1)
|
||||
ch <- time.Now()
|
||||
return ch
|
||||
})
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
build func() *cobra.Command
|
||||
args []string
|
||||
steps []scriptedToolStep
|
||||
}{
|
||||
{
|
||||
name: "latest drive download",
|
||||
build: newDriveCommand,
|
||||
args: []string{"download", "--node", "node-latest", "--output", filepath.Join(t.TempDir(), "latest.txt")},
|
||||
steps: []scriptedToolStep{{text: `{"downloadUrl":"https://example.test/latest.txt","fileSize":7,"version":9}`}},
|
||||
},
|
||||
{
|
||||
name: "versioned drive download",
|
||||
build: newDriveCommand,
|
||||
args: []string{"download", "--node", "node-versioned", "--version", "4", "--output", filepath.Join(t.TempDir(), "versioned.txt")},
|
||||
steps: []scriptedToolStep{{text: `{"downloadUrl":"https://example.test/versioned.txt","fileSize":7}`}},
|
||||
},
|
||||
{
|
||||
name: "doc export",
|
||||
build: newDocCommand,
|
||||
args: []string{"export", "--node", "doc-node", "--export-format", "markdown", "--output", filepath.Join(t.TempDir(), "export.md")},
|
||||
steps: []scriptedToolStep{
|
||||
{text: `{"jobId":"export-job-1"}`},
|
||||
{text: `{"status":"SUCCESS","downloadUrl":"https://example.test/export.md"}`},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
_, _, err := executeJSONOutputContractCommand(t, &scriptedToolCaller{format: "json", steps: tt.steps}, tt.build, tt.args...)
|
||||
if err == nil || !strings.Contains(err.Error(), "读取") {
|
||||
t.Fatalf("expected missing local artifact error, got %v", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -1346,7 +1346,7 @@ func newMinutesCommand() *cobra.Command {
|
||||
Short: "批量删除个人热词",
|
||||
Long: `批量删除听记个人热词。
|
||||
支持一次删除多个热词(逗号分隔)。删除后对应热词不再参与后续语音识别优化。`,
|
||||
Example: ` dws minutes hot-word delete --words "钉钉"
|
||||
Example: ` dws minutes hot-word delete --words "天气"
|
||||
dws minutes hot-word delete --words "OKR,钉钉,Copilot"`,
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
if err := validateRequiredFlags(cmd, "words"); err != nil {
|
||||
@@ -1384,7 +1384,7 @@ func newMinutesCommand() *cobra.Command {
|
||||
"不确定现有热词时先用 hot-word list",
|
||||
},
|
||||
Examples: []string{
|
||||
"dws minutes hot-word delete --words \"钉钉\"",
|
||||
"dws minutes hot-word delete --words \"天气\"",
|
||||
"dws minutes hot-word delete --words \"OKR,钉钉,Copilot\"",
|
||||
},
|
||||
},
|
||||
@@ -1883,11 +1883,12 @@ func newMinutesCommand() *cobra.Command {
|
||||
if err := validateRequiredFlagWithAliases(cmd, "id", "url", "task-uuid", "uuid"); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := validateRequiredFlags(cmd, "policy"); err != nil {
|
||||
return err
|
||||
// 手动校验必填参数(避免 validateRequiredFlags 对 Int 的误判)
|
||||
if !cmd.Flags().Changed("policy") {
|
||||
return fmt.Errorf("missing required flag --policy")
|
||||
}
|
||||
|
||||
policyID, err := strconv.ParseInt(mustGetFlag(cmd, "policy"), 10, 64)
|
||||
policyID, err := cmd.Flags().GetInt("policy")
|
||||
if err != nil || policyID < 2 || policyID > 4 {
|
||||
return fmt.Errorf("flag --policy must be an integer between 2 and 4 (2=可编辑, 3=可查看/下载, 4=仅查看)")
|
||||
}
|
||||
@@ -1942,7 +1943,7 @@ func newMinutesCommand() *cobra.Command {
|
||||
_ = permissionApplyCmd.Flags().MarkHidden("task-uuid")
|
||||
permissionApplyCmd.Flags().String("uuid", "", "--id 的别名")
|
||||
_ = permissionApplyCmd.Flags().MarkHidden("uuid")
|
||||
permissionApplyCmd.Flags().String("policy", "", "权限类型: 2=可编辑, 3=可查看/下载, 4=仅查看 (必填)")
|
||||
permissionApplyCmd.Flags().Int("policy", 0, "权限类型: 2=可编辑, 3=可查看/下载, 4=仅查看 (必填)")
|
||||
|
||||
permissionCmd.AddCommand(permissionAddCmd, permissionRemoveCmd, permissionApplyCmd)
|
||||
|
||||
|
||||
@@ -151,6 +151,17 @@ func (f *Formatter) PrintInfo(msg string) { fmt.Fprintf(f.w, "[INFO] %s\n",
|
||||
func (f *Formatter) PrintProgress(msg string) { fmt.Fprintf(f.errW, "%s\n", msg) }
|
||||
func (f *Formatter) PrintDim(msg string) { fmt.Fprintf(f.w, " %s\n", msg) }
|
||||
|
||||
// printJSONSafeInfo keeps command progress out of stdout when the caller
|
||||
// requested JSON. A successful JSON command must leave stdout parseable; its
|
||||
// human-readable progress belongs on stderr instead.
|
||||
func printJSONSafeInfo(msg string) {
|
||||
if deps != nil && deps.Caller != nil && strings.EqualFold(strings.TrimSpace(deps.Caller.Format()), "json") {
|
||||
deps.Out.PrintProgress("[INFO] " + msg)
|
||||
return
|
||||
}
|
||||
deps.Out.PrintInfo(msg)
|
||||
}
|
||||
|
||||
func (f *Formatter) PrintKeyValue(key, value string) {
|
||||
fmt.Fprintf(f.w, "%-16s%s\n", key+":", value)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,521 @@
|
||||
package helpers
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
const (
|
||||
defaultPagedCommandPageLimit = 50
|
||||
maxPagedCommandPageLimit = 500
|
||||
defaultPagedCommandDelayMS = 200
|
||||
)
|
||||
|
||||
type PagedCursorKind int
|
||||
|
||||
const (
|
||||
PagedCursorString PagedCursorKind = iota
|
||||
PagedCursorInt64
|
||||
)
|
||||
|
||||
type PagedAggregationMode int
|
||||
|
||||
const (
|
||||
PagedAggregationArray PagedAggregationMode = iota
|
||||
PagedAggregationConversationMessages
|
||||
)
|
||||
|
||||
type PagedMCPCommandConfig struct {
|
||||
ServerID string
|
||||
ToolName string
|
||||
ItemPath string
|
||||
CursorPath string
|
||||
HasMorePath string
|
||||
CursorArg string
|
||||
CursorKind PagedCursorKind
|
||||
AggregationMode PagedAggregationMode
|
||||
BuildArgs func(*cobra.Command) (map[string]any, error)
|
||||
Fallback func(map[string]any) error
|
||||
}
|
||||
|
||||
type pagedCommandOptions struct {
|
||||
pageAll bool
|
||||
pageLimit int
|
||||
maxItems int
|
||||
delayMS int
|
||||
}
|
||||
|
||||
func AddPagedMCPFlags(cmd *cobra.Command) {
|
||||
cmd.Flags().Bool("page-all", false, "自动按 nextCursor 拉取所有分页;未设置时保持单页调用")
|
||||
cmd.Flags().Int("page-limit", defaultPagedCommandPageLimit, "自动翻页最多请求页数(默认 50,范围 1-500;仅 --page-all 生效)")
|
||||
cmd.Flags().Int("max-items", 0, "自动翻页最多返回条数(默认 0 表示不限制;仅 --page-all 生效)")
|
||||
cmd.Flags().Int("page-delay", defaultPagedCommandDelayMS, "自动翻页每页之间等待毫秒数(默认 200;0 表示不等待;仅 --page-all 生效)")
|
||||
}
|
||||
|
||||
func RunPagedMCPCommand(cmd *cobra.Command, cfg PagedMCPCommandConfig) error {
|
||||
args, err := cfg.BuildArgs(cmd)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
opts, err := readPagedCommandOptions(cmd)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !opts.pageAll {
|
||||
return cfg.Fallback(args)
|
||||
}
|
||||
if err := validatePagedConfig(cfg); err != nil {
|
||||
return err
|
||||
}
|
||||
if deps.Caller.DryRun() {
|
||||
return deps.Out.PrintJSON(map[string]any{
|
||||
"dry_run": true,
|
||||
"request": map[string]any{
|
||||
"server": cfg.ServerID,
|
||||
"name": cfg.ToolName,
|
||||
"args": args,
|
||||
},
|
||||
"paging": map[string]any{
|
||||
"pageAll": true,
|
||||
"pageLimit": opts.pageLimit,
|
||||
"maxItems": opts.maxItems,
|
||||
"pageDelay": opts.delayMS,
|
||||
},
|
||||
})
|
||||
}
|
||||
return runPagedMCPCommand(cmd, cfg, opts, args)
|
||||
}
|
||||
|
||||
func readPagedCommandOptions(cmd *cobra.Command) (pagedCommandOptions, error) {
|
||||
pageAll, _ := cmd.Flags().GetBool("page-all")
|
||||
opts := pagedCommandOptions{pageAll: pageAll}
|
||||
if !pageAll {
|
||||
return opts, nil
|
||||
}
|
||||
opts.pageLimit, _ = cmd.Flags().GetInt("page-limit")
|
||||
if opts.pageLimit < 1 || opts.pageLimit > maxPagedCommandPageLimit {
|
||||
return opts, fmt.Errorf("--page-limit must be between 1 and 500")
|
||||
}
|
||||
opts.maxItems, _ = cmd.Flags().GetInt("max-items")
|
||||
if opts.maxItems < 0 {
|
||||
return opts, fmt.Errorf("--max-items must be greater than or equal to 0")
|
||||
}
|
||||
opts.delayMS, _ = cmd.Flags().GetInt("page-delay")
|
||||
if opts.delayMS < 0 {
|
||||
return opts, fmt.Errorf("--page-delay must be greater than or equal to 0")
|
||||
}
|
||||
return opts, nil
|
||||
}
|
||||
|
||||
func validatePagedConfig(cfg PagedMCPCommandConfig) error {
|
||||
switch {
|
||||
case strings.TrimSpace(cfg.ServerID) == "":
|
||||
return fmt.Errorf("paged command server is required")
|
||||
case strings.TrimSpace(cfg.ToolName) == "":
|
||||
return fmt.Errorf("paged command tool is required")
|
||||
case strings.TrimSpace(cfg.ItemPath) == "":
|
||||
return fmt.Errorf("paged command item path is required")
|
||||
case strings.TrimSpace(cfg.CursorPath) == "":
|
||||
return fmt.Errorf("paged command cursor path is required")
|
||||
case strings.TrimSpace(cfg.HasMorePath) == "":
|
||||
return fmt.Errorf("paged command hasMore path is required")
|
||||
case strings.TrimSpace(cfg.CursorArg) == "":
|
||||
return fmt.Errorf("paged command cursor arg is required")
|
||||
case cfg.BuildArgs == nil || cfg.Fallback == nil:
|
||||
return fmt.Errorf("paged command callbacks are required")
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func runPagedMCPCommand(cmd *cobra.Command, cfg PagedMCPCommandConfig, opts pagedCommandOptions, args map[string]any) error {
|
||||
var envelope map[string]any
|
||||
ctx := cmd.Context()
|
||||
items := newPagedCollection(cfg)
|
||||
seenCursors := map[string]bool{}
|
||||
currentCursor := cursorValueKey(args[cfg.CursorArg], cfg.CursorKind)
|
||||
lastCursor := args[cfg.CursorArg]
|
||||
hasMore := true
|
||||
|
||||
for page := 1; page <= opts.pageLimit && hasMore; page++ {
|
||||
pageCursor := args[cfg.CursorArg]
|
||||
seenCursors[currentCursor] = true
|
||||
text, err := callMCPToolReturnTextOnServer(ctx, cfg.ServerID, cfg.ToolName, args)
|
||||
if err != nil {
|
||||
return handlePagedCommandError(cmd, envelope, cfg, items, page, currentCursor, err)
|
||||
}
|
||||
parsed, pageItems, nextCursor, more, err := parsePagedCommandPage(text, cfg)
|
||||
if err != nil {
|
||||
return handlePagedCommandError(cmd, envelope, cfg, items, page, currentCursor, err)
|
||||
}
|
||||
if envelope == nil {
|
||||
envelope = parsed
|
||||
}
|
||||
if err := items.Add(pageItems); err != nil {
|
||||
return handlePagedCommandError(cmd, envelope, cfg, items, page, currentCursor, err)
|
||||
}
|
||||
hasMore = more
|
||||
|
||||
if opts.maxItems > 0 && items.Total() > opts.maxItems {
|
||||
items.Truncate(opts.maxItems)
|
||||
return writePagedCommandResult(envelope, cfg, items, pagingMetadata{
|
||||
Truncated: true,
|
||||
HasMore: true,
|
||||
LastCursor: pageCursor,
|
||||
Pages: page,
|
||||
Total: items.Total(),
|
||||
TruncatedWithinPage: true,
|
||||
})
|
||||
}
|
||||
lastCursor = nextCursor
|
||||
if opts.maxItems > 0 && items.Total() == opts.maxItems && hasMore {
|
||||
return writePagedCommandResult(envelope, cfg, items, pagingMetadata{
|
||||
Truncated: true,
|
||||
HasMore: true,
|
||||
LastCursor: lastCursor,
|
||||
Pages: page,
|
||||
Total: items.Total(),
|
||||
})
|
||||
}
|
||||
if !hasMore {
|
||||
return writePagedCommandResult(envelope, cfg, items, pagingMetadata{
|
||||
Truncated: false,
|
||||
HasMore: false,
|
||||
LastCursor: lastCursor,
|
||||
Pages: page,
|
||||
Total: items.Total(),
|
||||
})
|
||||
}
|
||||
nextKey := cursorValueKey(nextCursor, cfg.CursorKind)
|
||||
if nextKey == "" || nextKey == currentCursor || seenCursors[nextKey] {
|
||||
err := fmt.Errorf("pagination cursor did not advance: %s", nextKey)
|
||||
return handlePagedCommandError(cmd, envelope, cfg, items, page+1, nextKey, err)
|
||||
}
|
||||
normalizedCursor, err := normalizeCursorArg(nextCursor, cfg.CursorKind)
|
||||
if err != nil {
|
||||
return handlePagedCommandError(cmd, envelope, cfg, items, page+1, nextKey, err)
|
||||
}
|
||||
currentCursor = nextKey
|
||||
args[cfg.CursorArg] = normalizedCursor
|
||||
if opts.delayMS > 0 {
|
||||
if err := sleepPagedCommandDelay(ctx, time.Duration(opts.delayMS)*time.Millisecond); err != nil {
|
||||
return handlePagedCommandError(cmd, envelope, cfg, items, page+1, currentCursor, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return writePagedCommandResult(envelope, cfg, items, pagingMetadata{
|
||||
Truncated: hasMore,
|
||||
HasMore: hasMore,
|
||||
LastCursor: lastCursor,
|
||||
Pages: opts.pageLimit,
|
||||
Total: items.Total(),
|
||||
})
|
||||
}
|
||||
|
||||
func parsePagedCommandPage(text string, cfg PagedMCPCommandConfig) (map[string]any, []any, any, bool, error) {
|
||||
var parsed map[string]any
|
||||
if err := json.Unmarshal([]byte(text), &parsed); err != nil {
|
||||
return nil, nil, nil, false, fmt.Errorf("parse paged response JSON: %w", err)
|
||||
}
|
||||
rawHasMore, ok := getJSONPath(parsed, cfg.HasMorePath)
|
||||
if !ok {
|
||||
return nil, nil, nil, false, fmt.Errorf("paged response missing %s", cfg.HasMorePath)
|
||||
}
|
||||
hasMore, ok := rawHasMore.(bool)
|
||||
if !ok {
|
||||
return nil, nil, nil, false, fmt.Errorf("paged response %s must be boolean", cfg.HasMorePath)
|
||||
}
|
||||
rawItems, ok := getJSONPath(parsed, cfg.ItemPath)
|
||||
if !ok && cfg.AggregationMode == PagedAggregationConversationMessages && !hasMore {
|
||||
rawItems = []any{}
|
||||
ok = true
|
||||
}
|
||||
if !ok {
|
||||
return nil, nil, nil, false, fmt.Errorf("paged response missing %s", cfg.ItemPath)
|
||||
}
|
||||
items, ok := rawItems.([]any)
|
||||
if !ok {
|
||||
return nil, nil, nil, false, fmt.Errorf("paged response %s must be array", cfg.ItemPath)
|
||||
}
|
||||
nextCursor, ok := getJSONPath(parsed, cfg.CursorPath)
|
||||
if hasMore && !ok {
|
||||
return nil, nil, nil, false, fmt.Errorf("paged response missing %s", cfg.CursorPath)
|
||||
}
|
||||
return parsed, items, nextCursor, hasMore, nil
|
||||
}
|
||||
|
||||
type pagingMetadata struct {
|
||||
Truncated bool
|
||||
HasMore bool
|
||||
LastCursor any
|
||||
Pages int
|
||||
Total int
|
||||
TruncatedWithinPage bool
|
||||
Partial bool
|
||||
FailedPage int
|
||||
FailedCursor string
|
||||
PagesFetched int
|
||||
ItemsFetched int
|
||||
Error string
|
||||
}
|
||||
|
||||
func handlePagedCommandError(cmd *cobra.Command, envelope map[string]any, cfg PagedMCPCommandConfig, items *pagedCollection, failedPage int, failedCursor string, err error) error {
|
||||
if envelope == nil {
|
||||
return err
|
||||
}
|
||||
fmt.Fprintf(cmd.ErrOrStderr(), "pagination stopped at page %d: %v\n", failedPage, err)
|
||||
if outputErr := writePagedCommandResult(envelope, cfg, items, pagingMetadata{
|
||||
Truncated: true,
|
||||
HasMore: true,
|
||||
LastCursor: failedCursor,
|
||||
Pages: failedPage - 1,
|
||||
Total: items.Total(),
|
||||
Partial: true,
|
||||
FailedPage: failedPage,
|
||||
FailedCursor: failedCursor,
|
||||
PagesFetched: failedPage - 1,
|
||||
ItemsFetched: items.Total(),
|
||||
Error: err.Error(),
|
||||
}); outputErr != nil {
|
||||
return errors.Join(err, outputErr)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func writePagedCommandResult(envelope map[string]any, cfg PagedMCPCommandConfig, items *pagedCollection, meta pagingMetadata) error {
|
||||
_ = setJSONPath(envelope, cfg.ItemPath, items.Values())
|
||||
_ = setJSONPath(envelope, cfg.HasMorePath, meta.HasMore)
|
||||
_ = setJSONPath(envelope, cfg.CursorPath, meta.LastCursor)
|
||||
paging := map[string]any{
|
||||
"truncated": meta.Truncated,
|
||||
"hasMore": meta.HasMore,
|
||||
"lastCursor": meta.LastCursor,
|
||||
"pages": meta.Pages,
|
||||
"total": meta.Total,
|
||||
}
|
||||
if meta.Partial {
|
||||
paging["partial"] = true
|
||||
paging["failedPage"] = meta.FailedPage
|
||||
paging["failedCursor"] = meta.FailedCursor
|
||||
paging["pagesFetched"] = meta.PagesFetched
|
||||
paging["itemsFetched"] = meta.ItemsFetched
|
||||
paging["error"] = meta.Error
|
||||
}
|
||||
if meta.TruncatedWithinPage {
|
||||
paging["truncatedWithinPage"] = true
|
||||
paging["resumeCursorReliable"] = false
|
||||
}
|
||||
envelope["paging"] = paging
|
||||
return deps.Out.PrintJSON(envelope)
|
||||
}
|
||||
|
||||
func sleepPagedCommandDelay(ctx context.Context, delay time.Duration) error {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return ctx.Err()
|
||||
case <-helperAfter(delay):
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
type pagedCollection struct {
|
||||
mode PagedAggregationMode
|
||||
items []any
|
||||
conversationIndex map[string]int
|
||||
total int
|
||||
}
|
||||
|
||||
func newPagedCollection(cfg PagedMCPCommandConfig) *pagedCollection {
|
||||
return &pagedCollection{
|
||||
mode: cfg.AggregationMode,
|
||||
conversationIndex: map[string]int{},
|
||||
}
|
||||
}
|
||||
|
||||
func (c *pagedCollection) Add(items []any) error {
|
||||
if c.mode != PagedAggregationConversationMessages {
|
||||
c.items = append(c.items, items...)
|
||||
c.total = len(c.items)
|
||||
return nil
|
||||
}
|
||||
for _, item := range items {
|
||||
if err := c.addConversation(item); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *pagedCollection) Values() []any {
|
||||
if c.items == nil {
|
||||
return []any{}
|
||||
}
|
||||
return c.items
|
||||
}
|
||||
|
||||
func (c *pagedCollection) Total() int {
|
||||
return c.total
|
||||
}
|
||||
|
||||
func (c *pagedCollection) Truncate(maxItems int) bool {
|
||||
if maxItems <= 0 || c.total <= maxItems {
|
||||
return false
|
||||
}
|
||||
if c.mode != PagedAggregationConversationMessages {
|
||||
c.items = c.items[:maxItems]
|
||||
c.total = len(c.items)
|
||||
return true
|
||||
}
|
||||
c.truncateConversationMessages(maxItems)
|
||||
return true
|
||||
}
|
||||
|
||||
func (c *pagedCollection) addConversation(item any) error {
|
||||
conversation, ok := item.(map[string]any)
|
||||
if !ok {
|
||||
return fmt.Errorf("paged response conversation item must be object")
|
||||
}
|
||||
key, _ := conversation["openConversationId"].(string)
|
||||
if strings.TrimSpace(key) == "" {
|
||||
return fmt.Errorf("paged response conversation item missing openConversationId")
|
||||
}
|
||||
messages, err := conversationMessages(conversation)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if idx, ok := c.conversationIndex[key]; ok {
|
||||
existing := c.items[idx].(map[string]any)
|
||||
existingMessages, err := conversationMessages(existing)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
existing["messages"] = append(existingMessages, messages...)
|
||||
c.total += len(messages)
|
||||
return nil
|
||||
}
|
||||
c.conversationIndex[key] = len(c.items)
|
||||
c.items = append(c.items, conversation)
|
||||
c.total += len(messages)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *pagedCollection) truncateConversationMessages(maxItems int) {
|
||||
remaining := maxItems
|
||||
for i, item := range c.items {
|
||||
conversation := item.(map[string]any)
|
||||
messages, _ := conversationMessages(conversation)
|
||||
if remaining >= len(messages) {
|
||||
remaining -= len(messages)
|
||||
continue
|
||||
}
|
||||
if remaining == 0 {
|
||||
c.items = c.items[:i]
|
||||
c.total = maxItems
|
||||
return
|
||||
}
|
||||
conversation["messages"] = messages[:remaining]
|
||||
c.items = c.items[:i+1]
|
||||
c.total = maxItems
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func conversationMessages(conversation map[string]any) ([]any, error) {
|
||||
raw, ok := conversation["messages"]
|
||||
if !ok {
|
||||
return []any{}, nil
|
||||
}
|
||||
messages, ok := raw.([]any)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("paged response conversation messages must be array")
|
||||
}
|
||||
return messages, nil
|
||||
}
|
||||
|
||||
func getJSONPath(root map[string]any, path string) (any, bool) {
|
||||
var current any = root
|
||||
for _, part := range strings.Split(path, ".") {
|
||||
obj, ok := current.(map[string]any)
|
||||
if !ok {
|
||||
return nil, false
|
||||
}
|
||||
current, ok = obj[part]
|
||||
if !ok {
|
||||
return nil, false
|
||||
}
|
||||
}
|
||||
return current, true
|
||||
}
|
||||
|
||||
func setJSONPath(root map[string]any, path string, value any) bool {
|
||||
parts := strings.Split(path, ".")
|
||||
current := root
|
||||
for _, part := range parts[:len(parts)-1] {
|
||||
next, ok := current[part].(map[string]any)
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
current = next
|
||||
}
|
||||
current[parts[len(parts)-1]] = value
|
||||
return true
|
||||
}
|
||||
|
||||
func cursorValueKey(value any, kind PagedCursorKind) string {
|
||||
switch kind {
|
||||
case PagedCursorInt64:
|
||||
switch v := value.(type) {
|
||||
case int64:
|
||||
return strconv.FormatInt(v, 10)
|
||||
case int:
|
||||
return strconv.Itoa(v)
|
||||
case float64:
|
||||
return strconv.FormatInt(int64(v), 10)
|
||||
case string:
|
||||
return strings.TrimSpace(v)
|
||||
default:
|
||||
return ""
|
||||
}
|
||||
default:
|
||||
if value == nil {
|
||||
return ""
|
||||
}
|
||||
return strings.TrimSpace(fmt.Sprint(value))
|
||||
}
|
||||
}
|
||||
|
||||
func normalizeCursorArg(value any, kind PagedCursorKind) (any, error) {
|
||||
if kind != PagedCursorInt64 {
|
||||
if value == nil {
|
||||
return "", nil
|
||||
}
|
||||
return fmt.Sprint(value), nil
|
||||
}
|
||||
switch v := value.(type) {
|
||||
case int64:
|
||||
return v, nil
|
||||
case int:
|
||||
return int64(v), nil
|
||||
case float64:
|
||||
converted := int64(v)
|
||||
if float64(converted) != v {
|
||||
return nil, fmt.Errorf("paged response cursor must be an integer, got %v", v)
|
||||
}
|
||||
return converted, nil
|
||||
case string:
|
||||
parsed, err := strconv.ParseInt(strings.TrimSpace(v), 10, 64)
|
||||
if err == nil {
|
||||
return parsed, nil
|
||||
}
|
||||
return nil, fmt.Errorf("paged response cursor must be a base-10 int64 string, got %q", v)
|
||||
}
|
||||
return nil, fmt.Errorf("paged response cursor must be int64-compatible, got %T", value)
|
||||
}
|
||||
@@ -0,0 +1,973 @@
|
||||
package helpers
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"io"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/DingTalk-Real-AI/dingtalk-workspace-cli/pkg/edition"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
type pagedCommandCall struct {
|
||||
server string
|
||||
tool string
|
||||
args map[string]any
|
||||
ctx context.Context
|
||||
}
|
||||
|
||||
type pagedCommandCaller struct {
|
||||
steps []scriptedToolStep
|
||||
calls []pagedCommandCall
|
||||
format string
|
||||
dry bool
|
||||
}
|
||||
|
||||
func (c *pagedCommandCaller) CallTool(ctx context.Context, serverID, toolName string, args map[string]any) (*edition.ToolResult, error) {
|
||||
copied := map[string]any{}
|
||||
for k, v := range args {
|
||||
copied[k] = v
|
||||
}
|
||||
c.calls = append(c.calls, pagedCommandCall{server: serverID, tool: toolName, args: copied, ctx: ctx})
|
||||
if len(c.steps) == 0 {
|
||||
return textToolResult(`{"result":{"messages":[],"hasMore":false}}`), nil
|
||||
}
|
||||
step := c.steps[len(c.calls)-1]
|
||||
if step.err != nil {
|
||||
return nil, step.err
|
||||
}
|
||||
return textToolResult(step.text), nil
|
||||
}
|
||||
|
||||
func (c *pagedCommandCaller) Format() string { return c.format }
|
||||
func (c *pagedCommandCaller) DryRun() bool { return c.dry }
|
||||
func (*pagedCommandCaller) Fields() string { return "" }
|
||||
func (*pagedCommandCaller) JQ() string { return "" }
|
||||
|
||||
func runPagedCommandTest(t *testing.T, caller *pagedCommandCaller, cfg PagedMCPCommandConfig, args ...string) (map[string]any, string, error) {
|
||||
t.Helper()
|
||||
return runPagedCommandTestWithSleep(t, caller, cfg, func(time.Duration) {}, args...)
|
||||
}
|
||||
|
||||
func runPagedCommandTestWithSleep(t *testing.T, caller *pagedCommandCaller, cfg PagedMCPCommandConfig, sleep func(time.Duration), args ...string) (map[string]any, string, error) {
|
||||
t.Helper()
|
||||
out, stderr, err := executePagedCommandTest(t, caller, cfg, sleep, &bytes.Buffer{}, args...)
|
||||
if strings.TrimSpace(out) == "" {
|
||||
return nil, stderr, err
|
||||
}
|
||||
var parsed map[string]any
|
||||
if unmarshalErr := json.Unmarshal([]byte(out), &parsed); unmarshalErr != nil {
|
||||
t.Fatalf("stdout JSON = %q, err = %v", out, unmarshalErr)
|
||||
}
|
||||
return parsed, stderr, err
|
||||
}
|
||||
|
||||
func executePagedCommandTest(t *testing.T, caller *pagedCommandCaller, cfg PagedMCPCommandConfig, sleep func(time.Duration), stdout io.Writer, args ...string) (string, string, error) {
|
||||
t.Helper()
|
||||
return executePagedCommandTestWithContext(t, context.Background(), caller, cfg, sleep, stdout, args...)
|
||||
}
|
||||
|
||||
func executePagedCommandTestWithContext(t *testing.T, ctx context.Context, caller *pagedCommandCaller, cfg PagedMCPCommandConfig, sleep func(time.Duration), stdout io.Writer, args ...string) (string, string, error) {
|
||||
t.Helper()
|
||||
oldDeps := deps
|
||||
oldSleep := helperSleep
|
||||
oldAfter := helperAfter
|
||||
t.Cleanup(func() {
|
||||
deps = oldDeps
|
||||
helperSleep = oldSleep
|
||||
helperAfter = oldAfter
|
||||
})
|
||||
InitDeps(caller)
|
||||
out := stdout
|
||||
errOut := &bytes.Buffer{}
|
||||
deps.Out.w = out
|
||||
deps.Out.errW = errOut
|
||||
if sleep != nil {
|
||||
helperSleep = sleep
|
||||
helperAfter = func(d time.Duration) <-chan time.Time {
|
||||
sleep(d)
|
||||
ch := make(chan time.Time, 1)
|
||||
ch <- time.Now()
|
||||
return ch
|
||||
}
|
||||
}
|
||||
|
||||
cmd := &cobra.Command{
|
||||
Use: "paged",
|
||||
SilenceUsage: true,
|
||||
RunE: func(cmd *cobra.Command, _ []string) error {
|
||||
return RunPagedMCPCommand(cmd, cfg)
|
||||
},
|
||||
}
|
||||
cmd.SetContext(ctx)
|
||||
cmd.Flags().String("cursor", "0", "")
|
||||
AddPagedMCPFlags(cmd)
|
||||
cmd.SetErr(errOut)
|
||||
cmd.SetArgs(args)
|
||||
err := cmd.Execute()
|
||||
if buf, ok := out.(*bytes.Buffer); ok {
|
||||
return buf.String(), errOut.String(), err
|
||||
}
|
||||
return "", errOut.String(), err
|
||||
}
|
||||
|
||||
func pagedCommandMessagesConfig(fallback func(map[string]any) error) PagedMCPCommandConfig {
|
||||
if fallback == nil {
|
||||
fallback = func(map[string]any) error { return nil }
|
||||
}
|
||||
return PagedMCPCommandConfig{
|
||||
ServerID: "chat",
|
||||
ToolName: "search_messages_by_time_range",
|
||||
ItemPath: "result.messages",
|
||||
CursorPath: "result.nextCursor",
|
||||
HasMorePath: "result.hasMore",
|
||||
CursorArg: "cursor",
|
||||
CursorKind: PagedCursorString,
|
||||
BuildArgs: func(cmd *cobra.Command) (map[string]any, error) {
|
||||
cursor, _ := cmd.Flags().GetString("cursor")
|
||||
return map[string]any{"cursor": cursor, "limit": 2}, nil
|
||||
},
|
||||
Fallback: fallback,
|
||||
}
|
||||
}
|
||||
|
||||
func pagedCommandConversationMessagesConfig(fallback func(map[string]any) error) PagedMCPCommandConfig {
|
||||
cfg := pagedCommandMessagesConfig(fallback)
|
||||
cfg.ItemPath = "result.conversationMessagesList"
|
||||
cfg.AggregationMode = PagedAggregationConversationMessages
|
||||
return cfg
|
||||
}
|
||||
|
||||
func TestPagedMCPCommandDefaultUsesFallbackOnly(t *testing.T) {
|
||||
caller := &pagedCommandCaller{}
|
||||
fallbackCalls := 0
|
||||
cfg := pagedCommandMessagesConfig(func(args map[string]any) error {
|
||||
fallbackCalls++
|
||||
if args["cursor"] != "0" {
|
||||
t.Fatalf("fallback args = %#v", args)
|
||||
}
|
||||
return nil
|
||||
})
|
||||
_, _, err := runPagedCommandTest(t, caller, cfg, "--page-limit", "2", "--max-items", "1", "--page-delay", "0")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if fallbackCalls != 1 || len(caller.calls) != 0 {
|
||||
t.Fatalf("fallback=%d remote=%d, want fallback only", fallbackCalls, len(caller.calls))
|
||||
}
|
||||
}
|
||||
|
||||
func TestPagedMCPCommandRejectsInvalidConfigWhenPageAll(t *testing.T) {
|
||||
caller := &pagedCommandCaller{}
|
||||
cfg := pagedCommandMessagesConfig(nil)
|
||||
cfg.ServerID = " "
|
||||
|
||||
got, _, err := runPagedCommandTest(t, caller, cfg, "--page-all")
|
||||
if err == nil || !strings.Contains(err.Error(), "server is required") {
|
||||
t.Fatalf("result=%#v err=%v, want config error", got, err)
|
||||
}
|
||||
if got != nil {
|
||||
t.Fatalf("result=%#v, want no stdout", got)
|
||||
}
|
||||
if len(caller.calls) != 0 {
|
||||
t.Fatalf("calls=%#v, want no remote call", caller.calls)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPagedMCPCommandDryRunPrintsRequestAndSkipsRemote(t *testing.T) {
|
||||
caller := &pagedCommandCaller{dry: true}
|
||||
|
||||
got, _, err := runPagedCommandTest(t, caller, pagedCommandMessagesConfig(nil), "--page-all", "--page-limit", "3", "--max-items", "7", "--page-delay", "0")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(caller.calls) != 0 {
|
||||
t.Fatalf("calls=%#v, want no remote call", caller.calls)
|
||||
}
|
||||
if got["dry_run"] != true {
|
||||
t.Fatalf("dry_run=%#v, want true", got["dry_run"])
|
||||
}
|
||||
request := got["request"].(map[string]any)
|
||||
if request["server"] != "chat" || request["name"] != "search_messages_by_time_range" {
|
||||
t.Fatalf("request=%#v", request)
|
||||
}
|
||||
args := request["args"].(map[string]any)
|
||||
if args["cursor"] != "0" || args["limit"].(float64) != 2 {
|
||||
t.Fatalf("args=%#v", args)
|
||||
}
|
||||
paging := got["paging"].(map[string]any)
|
||||
if paging["pageAll"] != true || paging["pageLimit"].(float64) != 3 || paging["maxItems"].(float64) != 7 || paging["pageDelay"].(float64) != 0 {
|
||||
t.Fatalf("paging=%#v", paging)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPagedMCPCommandValidateConfigRejectsMissingRequiredFields(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
edit func(*PagedMCPCommandConfig)
|
||||
want string
|
||||
}{
|
||||
{
|
||||
name: "server",
|
||||
edit: func(cfg *PagedMCPCommandConfig) {
|
||||
cfg.ServerID = ""
|
||||
},
|
||||
want: "server is required",
|
||||
},
|
||||
{
|
||||
name: "tool",
|
||||
edit: func(cfg *PagedMCPCommandConfig) {
|
||||
cfg.ToolName = ""
|
||||
},
|
||||
want: "tool is required",
|
||||
},
|
||||
{
|
||||
name: "item path",
|
||||
edit: func(cfg *PagedMCPCommandConfig) {
|
||||
cfg.ItemPath = ""
|
||||
},
|
||||
want: "item path is required",
|
||||
},
|
||||
{
|
||||
name: "cursor path",
|
||||
edit: func(cfg *PagedMCPCommandConfig) {
|
||||
cfg.CursorPath = ""
|
||||
},
|
||||
want: "cursor path is required",
|
||||
},
|
||||
{
|
||||
name: "hasMore path",
|
||||
edit: func(cfg *PagedMCPCommandConfig) {
|
||||
cfg.HasMorePath = ""
|
||||
},
|
||||
want: "hasMore path is required",
|
||||
},
|
||||
{
|
||||
name: "cursor arg",
|
||||
edit: func(cfg *PagedMCPCommandConfig) {
|
||||
cfg.CursorArg = ""
|
||||
},
|
||||
want: "cursor arg is required",
|
||||
},
|
||||
{
|
||||
name: "build args callback",
|
||||
edit: func(cfg *PagedMCPCommandConfig) {
|
||||
cfg.BuildArgs = nil
|
||||
},
|
||||
want: "callbacks are required",
|
||||
},
|
||||
{
|
||||
name: "fallback callback",
|
||||
edit: func(cfg *PagedMCPCommandConfig) {
|
||||
cfg.Fallback = nil
|
||||
},
|
||||
want: "callbacks are required",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
cfg := pagedCommandMessagesConfig(nil)
|
||||
tt.edit(&cfg)
|
||||
err := validatePagedConfig(cfg)
|
||||
if err == nil || !strings.Contains(err.Error(), tt.want) {
|
||||
t.Fatalf("err=%v, want %q", err, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestPagedMCPCommandStringCursorAggregatesAndPageLimit(t *testing.T) {
|
||||
caller := &pagedCommandCaller{steps: []scriptedToolStep{
|
||||
{text: `{"result":{"messages":[{"id":"m1"}],"hasMore":true,"nextCursor":"c2"}}`},
|
||||
{text: `{"result":{"messages":[{"id":"m2"}],"hasMore":true,"nextCursor":"c3"}}`},
|
||||
}}
|
||||
got, _, err := runPagedCommandTest(t, caller, pagedCommandMessagesConfig(nil), "--page-all", "--page-limit", "2", "--page-delay", "0")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
items := got["result"].(map[string]any)["messages"].([]any)
|
||||
result := got["result"].(map[string]any)
|
||||
paging := got["paging"].(map[string]any)
|
||||
if len(items) != 2 || paging["truncated"] != true || paging["pages"].(float64) != 2 {
|
||||
t.Fatalf("result = %#v", got)
|
||||
}
|
||||
if result["hasMore"] != true || result["nextCursor"] != "c3" {
|
||||
t.Fatalf("result=%#v, want final page-limit cursor state", result)
|
||||
}
|
||||
if caller.calls[0].args["cursor"] != "0" || caller.calls[1].args["cursor"] != "c2" {
|
||||
t.Fatalf("call args = %#v", caller.calls)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPagedMCPCommandStringCursorAggregatesAndSyncsCompletionFields(t *testing.T) {
|
||||
caller := &pagedCommandCaller{steps: []scriptedToolStep{
|
||||
{text: `{"result":{"messages":[{"id":"m1"}],"hasMore":true,"nextCursor":"c2"}}`},
|
||||
{text: `{"result":{"messages":[{"id":"m2"}],"hasMore":false,"nextCursor":""}}`},
|
||||
}}
|
||||
|
||||
got, _, err := runPagedCommandTest(t, caller, pagedCommandMessagesConfig(nil), "--page-all", "--page-delay", "0")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
result := got["result"].(map[string]any)
|
||||
items := result["messages"].([]any)
|
||||
if len(items) != 2 || result["hasMore"] != false || result["nextCursor"] != "" {
|
||||
t.Fatalf("result=%#v, want complete aggregate with final cursor state", result)
|
||||
}
|
||||
paging := got["paging"].(map[string]any)
|
||||
if paging["truncated"] != false || paging["hasMore"] != false || paging["lastCursor"] != "" {
|
||||
t.Fatalf("paging=%#v, want complete pagination metadata", paging)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPagedMCPCommandConversationMessagesMergeSameConversation(t *testing.T) {
|
||||
caller := &pagedCommandCaller{steps: []scriptedToolStep{
|
||||
{text: `{"result":{"conversationMessagesList":[{"openConversationId":"cid1","title":"群1","messages":[{"id":"m1"}]}],"hasMore":true,"nextCursor":"c2"}}`},
|
||||
{text: `{"result":{"conversationMessagesList":[{"openConversationId":"cid1","title":"ignored","messages":[{"id":"m2"}]}],"hasMore":false,"nextCursor":""}}`},
|
||||
}}
|
||||
got, _, err := runPagedCommandTest(t, caller, pagedCommandConversationMessagesConfig(nil), "--page-all", "--page-delay", "0")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
conversations := got["result"].(map[string]any)["conversationMessagesList"].([]any)
|
||||
if len(conversations) != 1 {
|
||||
t.Fatalf("conversations=%#v, want one merged conversation", conversations)
|
||||
}
|
||||
conversation := conversations[0].(map[string]any)
|
||||
messages := conversation["messages"].([]any)
|
||||
if conversation["title"] != "群1" || len(messages) != 2 {
|
||||
t.Fatalf("conversation=%#v, want preserved title and two messages", conversation)
|
||||
}
|
||||
paging := got["paging"].(map[string]any)
|
||||
if paging["total"].(float64) != 2 {
|
||||
t.Fatalf("paging=%#v, want total message count 2", paging)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPagedMCPCommandConversationMessagesPreserveFirstConversationOrder(t *testing.T) {
|
||||
caller := &pagedCommandCaller{steps: []scriptedToolStep{
|
||||
{text: `{"result":{"conversationMessagesList":[{"openConversationId":"cid2","messages":[{"id":"m2"}]},{"openConversationId":"cid1","messages":[{"id":"m1"}]}],"hasMore":true,"nextCursor":"c2"}}`},
|
||||
{text: `{"result":{"conversationMessagesList":[{"openConversationId":"cid3","messages":[{"id":"m3"}]}],"hasMore":false,"nextCursor":""}}`},
|
||||
}}
|
||||
got, _, err := runPagedCommandTest(t, caller, pagedCommandConversationMessagesConfig(nil), "--page-all", "--page-delay", "0")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
conversations := got["result"].(map[string]any)["conversationMessagesList"].([]any)
|
||||
gotIDs := []string{
|
||||
conversations[0].(map[string]any)["openConversationId"].(string),
|
||||
conversations[1].(map[string]any)["openConversationId"].(string),
|
||||
conversations[2].(map[string]any)["openConversationId"].(string),
|
||||
}
|
||||
if strings.Join(gotIDs, ",") != "cid2,cid1,cid3" {
|
||||
t.Fatalf("conversation order=%v", gotIDs)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPagedMCPCommandConversationMessagesMaxItemsTruncatesMessages(t *testing.T) {
|
||||
caller := &pagedCommandCaller{steps: []scriptedToolStep{
|
||||
{text: `{"result":{"conversationMessagesList":[{"openConversationId":"cid1","messages":[{"id":"m1"},{"id":"m2"}]},{"openConversationId":"cid2","messages":[{"id":"m3"},{"id":"m4"}]}],"hasMore":true,"nextCursor":"c2"}}`},
|
||||
}}
|
||||
got, _, err := runPagedCommandTest(t, caller, pagedCommandConversationMessagesConfig(nil), "--page-all", "--max-items", "3", "--page-delay", "0")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
conversations := got["result"].(map[string]any)["conversationMessagesList"].([]any)
|
||||
if len(conversations) != 2 {
|
||||
t.Fatalf("conversations=%#v, want two conversations", conversations)
|
||||
}
|
||||
secondMessages := conversations[1].(map[string]any)["messages"].([]any)
|
||||
paging := got["paging"].(map[string]any)
|
||||
if len(secondMessages) != 1 || paging["total"].(float64) != 3 || paging["truncated"] != true {
|
||||
t.Fatalf("result=%#v", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPagedMCPCommandConversationMessagesMissingListOnFinalPageIsEmpty(t *testing.T) {
|
||||
caller := &pagedCommandCaller{steps: []scriptedToolStep{
|
||||
{text: `{"result":{"hasMore":false,"nextCursor":""}}`},
|
||||
}}
|
||||
got, _, err := runPagedCommandTest(t, caller, pagedCommandConversationMessagesConfig(nil), "--page-all", "--page-delay", "0")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
conversations := got["result"].(map[string]any)["conversationMessagesList"].([]any)
|
||||
if len(conversations) != 0 {
|
||||
t.Fatalf("conversations=%#v, want empty", conversations)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPagedMCPCommandConversationMessagesLaterFailureOutputsPartial(t *testing.T) {
|
||||
caller := &pagedCommandCaller{steps: []scriptedToolStep{
|
||||
{text: `{"result":{"conversationMessagesList":[{"openConversationId":"cid1","messages":[{"id":"m1"}]}],"hasMore":true,"nextCursor":"c2"}}`},
|
||||
{err: errors.New("page failed")},
|
||||
}}
|
||||
got, stderr, err := runPagedCommandTest(t, caller, pagedCommandConversationMessagesConfig(nil), "--page-all", "--page-delay", "0")
|
||||
if err == nil || !strings.Contains(stderr, "pagination stopped") {
|
||||
t.Fatalf("err=%v stderr=%q", err, stderr)
|
||||
}
|
||||
conversations := got["result"].(map[string]any)["conversationMessagesList"].([]any)
|
||||
paging := got["paging"].(map[string]any)
|
||||
if len(conversations) != 1 || paging["partial"] != true || paging["itemsFetched"].(float64) != 1 {
|
||||
t.Fatalf("result=%#v", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPagedMCPCommandConversationMessagesAddErrorsOutputPartial(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
response string
|
||||
want string
|
||||
}{
|
||||
{
|
||||
name: "conversation item not object",
|
||||
response: `{"result":{"conversationMessagesList":["bad"],"hasMore":false,"nextCursor":""}}`,
|
||||
want: "conversation item must be object",
|
||||
},
|
||||
{
|
||||
name: "conversation missing openConversationId",
|
||||
response: `{"result":{"conversationMessagesList":[{"messages":[]}],"hasMore":false,"nextCursor":""}}`,
|
||||
want: "missing openConversationId",
|
||||
},
|
||||
{
|
||||
name: "conversation messages not array",
|
||||
response: `{"result":{"conversationMessagesList":[{"openConversationId":"cid1","messages":"bad"}],"hasMore":false,"nextCursor":""}}`,
|
||||
want: "conversation messages must be array",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
caller := &pagedCommandCaller{steps: []scriptedToolStep{{text: tt.response}}}
|
||||
got, stderr, err := runPagedCommandTest(t, caller, pagedCommandConversationMessagesConfig(nil), "--page-all", "--page-delay", "0")
|
||||
if err == nil || !strings.Contains(err.Error(), tt.want) {
|
||||
t.Fatalf("result=%#v err=%v, want %q", got, err, tt.want)
|
||||
}
|
||||
if !strings.Contains(stderr, "pagination stopped at page 1") {
|
||||
t.Fatalf("stderr=%q", stderr)
|
||||
}
|
||||
paging := got["paging"].(map[string]any)
|
||||
if paging["partial"] != true || paging["itemsFetched"].(float64) != 0 {
|
||||
t.Fatalf("paging=%#v", paging)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestPagedMCPCommandConversationMessagesMaxItemsTruncatesAtConversationBoundary(t *testing.T) {
|
||||
caller := &pagedCommandCaller{steps: []scriptedToolStep{
|
||||
{text: `{"result":{"conversationMessagesList":[{"openConversationId":"cid1","messages":[{"id":"m1"},{"id":"m2"}]},{"openConversationId":"cid2","messages":[{"id":"m3"},{"id":"m4"}]}],"hasMore":true,"nextCursor":"c2"}}`},
|
||||
}}
|
||||
got, _, err := runPagedCommandTest(t, caller, pagedCommandConversationMessagesConfig(nil), "--page-all", "--max-items", "2", "--page-delay", "0")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
conversations := got["result"].(map[string]any)["conversationMessagesList"].([]any)
|
||||
paging := got["paging"].(map[string]any)
|
||||
if len(conversations) != 1 || paging["total"].(float64) != 2 || paging["truncated"] != true {
|
||||
t.Fatalf("result=%#v", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPagedMCPCommandConversationMessagesAcceptsMissingMessages(t *testing.T) {
|
||||
messages, err := conversationMessages(map[string]any{"openConversationId": "cid1"})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(messages) != 0 {
|
||||
t.Fatalf("messages=%#v, want empty", messages)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPagedMCPCommandConversationMessagesRejectsCorruptExistingMessages(t *testing.T) {
|
||||
collection := newPagedCollection(PagedMCPCommandConfig{AggregationMode: PagedAggregationConversationMessages})
|
||||
collection.items = []any{map[string]any{"openConversationId": "cid1", "messages": "bad"}}
|
||||
collection.conversationIndex["cid1"] = 0
|
||||
|
||||
err := collection.Add([]any{map[string]any{"openConversationId": "cid1", "messages": []any{map[string]any{"id": "m2"}}}})
|
||||
if err == nil || !strings.Contains(err.Error(), "conversation messages must be array") {
|
||||
t.Fatalf("err=%v, want corrupt existing messages error", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPagedMCPCommandResponseShapeErrors(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
response string
|
||||
want string
|
||||
}{
|
||||
{
|
||||
name: "items not array",
|
||||
response: `{"result":{"messages":"bad","hasMore":false}}`,
|
||||
want: "result.messages must be array",
|
||||
},
|
||||
{
|
||||
name: "missing hasMore",
|
||||
response: `{"result":{"messages":[]}}`,
|
||||
want: "missing result.hasMore",
|
||||
},
|
||||
{
|
||||
name: "hasMore not bool",
|
||||
response: `{"result":{"messages":[],"hasMore":"yes"}}`,
|
||||
want: "result.hasMore must be boolean",
|
||||
},
|
||||
{
|
||||
name: "missing next cursor",
|
||||
response: `{"result":{"messages":[{"id":"m1"}],"hasMore":true}}`,
|
||||
want: "missing result.nextCursor",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
caller := &pagedCommandCaller{steps: []scriptedToolStep{{text: tt.response}}}
|
||||
got, _, err := runPagedCommandTest(t, caller, pagedCommandMessagesConfig(nil), "--page-all", "--page-delay", "0")
|
||||
if err == nil || !strings.Contains(err.Error(), tt.want) {
|
||||
t.Fatalf("result=%#v err=%v, want %q", got, err, tt.want)
|
||||
}
|
||||
if got != nil {
|
||||
t.Fatalf("result=%#v, want no partial stdout", got)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestPagedMCPCommandPageDelayControlsSleep(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
delay string
|
||||
wantSleeps []time.Duration
|
||||
}{
|
||||
{
|
||||
name: "non zero delay sleeps between pages",
|
||||
delay: "200",
|
||||
wantSleeps: []time.Duration{200 * time.Millisecond},
|
||||
},
|
||||
{
|
||||
name: "zero delay skips sleep",
|
||||
delay: "0",
|
||||
wantSleeps: nil,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
caller := &pagedCommandCaller{steps: []scriptedToolStep{
|
||||
{text: `{"result":{"messages":[{"id":"m1"}],"hasMore":true,"nextCursor":"c2"}}`},
|
||||
{text: `{"result":{"messages":[{"id":"m2"}],"hasMore":false,"nextCursor":""}}`},
|
||||
}}
|
||||
var sleeps []time.Duration
|
||||
got, _, err := runPagedCommandTestWithSleep(t, caller, pagedCommandMessagesConfig(nil), func(d time.Duration) {
|
||||
sleeps = append(sleeps, d)
|
||||
}, "--page-all", "--page-delay", tt.delay)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
items := got["result"].(map[string]any)["messages"].([]any)
|
||||
if len(items) != 2 || len(caller.calls) != 2 {
|
||||
t.Fatalf("items=%#v calls=%#v", items, caller.calls)
|
||||
}
|
||||
if len(sleeps) != len(tt.wantSleeps) {
|
||||
t.Fatalf("sleeps=%v, want %v", sleeps, tt.wantSleeps)
|
||||
}
|
||||
for i := range tt.wantSleeps {
|
||||
if sleeps[i] != tt.wantSleeps[i] {
|
||||
t.Fatalf("sleeps=%v, want %v", sleeps, tt.wantSleeps)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestPagedMCPCommandMaxItemsTruncatesPrecisely(t *testing.T) {
|
||||
caller := &pagedCommandCaller{steps: []scriptedToolStep{
|
||||
{text: `{"result":{"messages":[{"id":"m1"},{"id":"m2"}],"hasMore":true,"nextCursor":"c2"}}`},
|
||||
}}
|
||||
got, _, err := runPagedCommandTest(t, caller, pagedCommandMessagesConfig(nil), "--page-all", "--max-items", "1", "--page-delay", "0")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
items := got["result"].(map[string]any)["messages"].([]any)
|
||||
paging := got["paging"].(map[string]any)
|
||||
if len(items) != 1 || paging["total"].(float64) != 1 || paging["truncated"] != true {
|
||||
t.Fatalf("result = %#v", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPagedMCPCommandMaxItemsStopsWhenPageExactlyReachesLimit(t *testing.T) {
|
||||
caller := &pagedCommandCaller{steps: []scriptedToolStep{
|
||||
{text: `{"result":{"messages":[{"id":"m1"},{"id":"m2"}],"hasMore":true,"nextCursor":"c2"}}`},
|
||||
{err: errors.New("second page should not run")},
|
||||
}}
|
||||
|
||||
got, _, err := runPagedCommandTest(t, caller, pagedCommandMessagesConfig(nil), "--page-all", "--max-items", "2", "--page-delay", "0")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
items := got["result"].(map[string]any)["messages"].([]any)
|
||||
result := got["result"].(map[string]any)
|
||||
paging := got["paging"].(map[string]any)
|
||||
if len(caller.calls) != 1 || len(items) != 2 {
|
||||
t.Fatalf("calls=%#v items=%#v, want one full page", caller.calls, items)
|
||||
}
|
||||
if paging["truncated"] != true || paging["hasMore"] != true || paging["lastCursor"] != "c2" {
|
||||
t.Fatalf("paging=%#v, want safe page-boundary cursor", paging)
|
||||
}
|
||||
if result["hasMore"] != true || result["nextCursor"] != "c2" {
|
||||
t.Fatalf("result=%#v, want safe page-boundary cursor fields", result)
|
||||
}
|
||||
if _, ok := paging["truncatedWithinPage"]; ok {
|
||||
t.Fatalf("paging=%#v, want no within-page truncation marker", paging)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPagedMCPCommandMaxItemsWithinPageKeepsCurrentCursor(t *testing.T) {
|
||||
caller := &pagedCommandCaller{steps: []scriptedToolStep{
|
||||
{text: `{"result":{"messages":[{"id":"m1"},{"id":"m2"}],"hasMore":true,"nextCursor":"c2"}}`},
|
||||
}}
|
||||
|
||||
got, _, err := runPagedCommandTest(t, caller, pagedCommandMessagesConfig(nil), "--page-all", "--max-items", "1", "--page-delay", "0")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
items := got["result"].(map[string]any)["messages"].([]any)
|
||||
result := got["result"].(map[string]any)
|
||||
paging := got["paging"].(map[string]any)
|
||||
if len(items) != 1 || paging["lastCursor"] != "0" {
|
||||
t.Fatalf("result=%#v, want current-page cursor after within-page truncation", got)
|
||||
}
|
||||
if result["hasMore"] != true || result["nextCursor"] != "0" {
|
||||
t.Fatalf("result=%#v, want unreliable current-page cursor fields", result)
|
||||
}
|
||||
if paging["truncatedWithinPage"] != true || paging["resumeCursorReliable"] != false {
|
||||
t.Fatalf("paging=%#v, want unreliable resume marker", paging)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPagedCollectionTruncateReturnsFalseWhenLimitDoesNotTrim(t *testing.T) {
|
||||
collection := newPagedCollection(PagedMCPCommandConfig{})
|
||||
if err := collection.Add([]any{map[string]any{"id": "m1"}}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if collection.Truncate(0) {
|
||||
t.Fatal("Truncate(0) should not trim")
|
||||
}
|
||||
if collection.Truncate(1) {
|
||||
t.Fatal("Truncate(total) should not trim")
|
||||
}
|
||||
if collection.Total() != 1 || len(collection.Values()) != 1 {
|
||||
t.Fatalf("collection=%#v, want unchanged single item", collection.Values())
|
||||
}
|
||||
}
|
||||
|
||||
func TestPagedMCPCommandConversationMessagesMaxItemsWithinPageKeepsCurrentCursor(t *testing.T) {
|
||||
caller := &pagedCommandCaller{steps: []scriptedToolStep{
|
||||
{text: `{"result":{"conversationMessagesList":[{"openConversationId":"cid1","messages":[{"id":"m1"},{"id":"m2"}]}],"hasMore":true,"nextCursor":"c2"}}`},
|
||||
}}
|
||||
|
||||
got, _, err := runPagedCommandTest(t, caller, pagedCommandConversationMessagesConfig(nil), "--page-all", "--max-items", "1", "--page-delay", "0")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
conversations := got["result"].(map[string]any)["conversationMessagesList"].([]any)
|
||||
messages := conversations[0].(map[string]any)["messages"].([]any)
|
||||
paging := got["paging"].(map[string]any)
|
||||
if len(messages) != 1 || paging["lastCursor"] != "0" {
|
||||
t.Fatalf("result=%#v, want truncated conversation with current-page cursor", got)
|
||||
}
|
||||
if paging["truncatedWithinPage"] != true || paging["resumeCursorReliable"] != false {
|
||||
t.Fatalf("paging=%#v, want unreliable resume marker", paging)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPagedMCPCommandPassesCommandContextToCaller(t *testing.T) {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
cancel()
|
||||
caller := &pagedCommandCaller{steps: []scriptedToolStep{
|
||||
{text: `{"result":{"messages":[{"id":"m1"}],"hasMore":false,"nextCursor":""}}`},
|
||||
}}
|
||||
|
||||
out, _, err := executePagedCommandTestWithContext(t, ctx, caller, pagedCommandMessagesConfig(nil), func(time.Duration) {}, &bytes.Buffer{}, "--page-all", "--page-delay", "0")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if strings.TrimSpace(out) == "" || len(caller.calls) != 1 {
|
||||
t.Fatalf("stdout=%q calls=%#v, want one successful call", out, caller.calls)
|
||||
}
|
||||
if caller.calls[0].ctx != ctx || caller.calls[0].ctx.Err() != context.Canceled {
|
||||
t.Fatalf("call ctx=%#v err=%v, want canceled command context", caller.calls[0].ctx, caller.calls[0].ctx.Err())
|
||||
}
|
||||
}
|
||||
|
||||
func TestPagedMCPCommandPageDelayStopsWhenContextCanceled(t *testing.T) {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
cancel()
|
||||
caller := &pagedCommandCaller{steps: []scriptedToolStep{
|
||||
{text: `{"result":{"messages":[{"id":"m1"}],"hasMore":true,"nextCursor":"c2"}}`},
|
||||
{text: `{"result":{"messages":[{"id":"m2"}],"hasMore":false,"nextCursor":""}}`},
|
||||
}}
|
||||
var out bytes.Buffer
|
||||
|
||||
stdout, stderr, err := executePagedCommandTestWithContext(t, ctx, caller, pagedCommandMessagesConfig(nil), nil, &out, "--page-all", "--page-delay", "10")
|
||||
if strings.TrimSpace(stdout) == "" {
|
||||
t.Fatal("stdout is empty, want partial pagination JSON")
|
||||
}
|
||||
if err == nil || !errors.Is(err, context.Canceled) {
|
||||
t.Fatalf("err=%v, want context canceled", err)
|
||||
}
|
||||
if !strings.Contains(stderr, "pagination stopped at page 2") || len(caller.calls) != 1 {
|
||||
t.Fatalf("stderr=%q calls=%#v, want cancellation before second call", stderr, caller.calls)
|
||||
}
|
||||
var got map[string]any
|
||||
if unmarshalErr := json.Unmarshal([]byte(stdout), &got); unmarshalErr != nil {
|
||||
t.Fatalf("stdout JSON = %q, err = %v", stdout, unmarshalErr)
|
||||
}
|
||||
paging := got["paging"].(map[string]any)
|
||||
if paging["partial"] != true || paging["failedPage"].(float64) != 2 || paging["itemsFetched"].(float64) != 1 {
|
||||
t.Fatalf("paging=%#v, want partial cancellation metadata", paging)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPagedMCPCommandPropagatesAggregatedOutputErrors(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
steps []scriptedToolStep
|
||||
args []string
|
||||
wantStderr string
|
||||
}{
|
||||
{
|
||||
name: "normal end",
|
||||
steps: []scriptedToolStep{
|
||||
{text: `{"result":{"messages":[{"id":"m1"}],"hasMore":false,"nextCursor":""}}`},
|
||||
},
|
||||
args: []string{"--page-all", "--page-delay", "0"},
|
||||
},
|
||||
{
|
||||
name: "max items truncation",
|
||||
steps: []scriptedToolStep{
|
||||
{text: `{"result":{"messages":[{"id":"m1"},{"id":"m2"}],"hasMore":true,"nextCursor":"c2"}}`},
|
||||
},
|
||||
args: []string{"--page-all", "--max-items", "1", "--page-delay", "0"},
|
||||
},
|
||||
{
|
||||
name: "page limit truncation",
|
||||
steps: []scriptedToolStep{
|
||||
{text: `{"result":{"messages":[{"id":"m1"}],"hasMore":true,"nextCursor":"c2"}}`},
|
||||
},
|
||||
args: []string{"--page-all", "--page-limit", "1", "--page-delay", "0"},
|
||||
},
|
||||
{
|
||||
name: "partial result after later failure",
|
||||
steps: []scriptedToolStep{
|
||||
{text: `{"result":{"messages":[{"id":"m1"}],"hasMore":true,"nextCursor":"c2"}}`},
|
||||
{err: errors.New("page failed")},
|
||||
},
|
||||
args: []string{"--page-all", "--page-delay", "0"},
|
||||
wantStderr: "pagination stopped at page 2",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
// TC-output-error: aggregate stdout write failures must fail the command.
|
||||
caller := &pagedCommandCaller{steps: tt.steps}
|
||||
_, stderr, err := executePagedCommandTest(t, caller, pagedCommandMessagesConfig(nil), func(time.Duration) {}, failingWriter{}, tt.args...)
|
||||
if err == nil || !strings.Contains(err.Error(), "write failed") {
|
||||
t.Fatalf("err=%v, want propagated write failure", err)
|
||||
}
|
||||
if tt.wantStderr != "" && !strings.Contains(stderr, tt.wantStderr) {
|
||||
t.Fatalf("stderr=%q, want %q", stderr, tt.wantStderr)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestPagedMCPCommandInt64CursorAndItemsPath(t *testing.T) {
|
||||
caller := &pagedCommandCaller{steps: []scriptedToolStep{
|
||||
{text: `{"result":{"items":[{"id":"f1"}],"hasMore":true,"nextCursor":20}}`},
|
||||
{text: `{"result":{"items":[{"id":"f2"}],"hasMore":false,"nextCursor":0}}`},
|
||||
}}
|
||||
cfg := PagedMCPCommandConfig{
|
||||
ServerID: "im",
|
||||
ToolName: "list_message_favorites",
|
||||
ItemPath: "result.items",
|
||||
CursorPath: "result.nextCursor",
|
||||
HasMorePath: "result.hasMore",
|
||||
CursorArg: "cursor",
|
||||
CursorKind: PagedCursorInt64,
|
||||
BuildArgs: func(cmd *cobra.Command) (map[string]any, error) {
|
||||
return map[string]any{"cursor": int64(0), "size": "20"}, nil
|
||||
},
|
||||
Fallback: func(map[string]any) error { return nil },
|
||||
}
|
||||
got, _, err := runPagedCommandTest(t, caller, cfg, "--page-all", "--page-delay", "0")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
items := got["result"].(map[string]any)["items"].([]any)
|
||||
if len(items) != 2 || caller.calls[1].args["cursor"] != int64(20) {
|
||||
t.Fatalf("items=%#v calls=%#v", items, caller.calls)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPagedMCPCommandInt64CursorRejectsNonNumericNextCursor(t *testing.T) {
|
||||
caller := &pagedCommandCaller{steps: []scriptedToolStep{
|
||||
{text: `{"result":{"items":[{"id":"f1"}],"hasMore":true,"nextCursor":"not-a-number"}}`},
|
||||
}}
|
||||
cfg := PagedMCPCommandConfig{
|
||||
ServerID: "im",
|
||||
ToolName: "list_message_favorites",
|
||||
ItemPath: "result.items",
|
||||
CursorPath: "result.nextCursor",
|
||||
HasMorePath: "result.hasMore",
|
||||
CursorArg: "cursor",
|
||||
CursorKind: PagedCursorInt64,
|
||||
BuildArgs: func(cmd *cobra.Command) (map[string]any, error) {
|
||||
return map[string]any{"cursor": int64(0), "size": "20"}, nil
|
||||
},
|
||||
Fallback: func(map[string]any) error { return nil },
|
||||
}
|
||||
got, stderr, err := runPagedCommandTest(t, caller, cfg, "--page-all", "--page-delay", "0")
|
||||
if err == nil || !strings.Contains(err.Error(), "base-10 int64 string") {
|
||||
t.Fatalf("err=%v, want invalid int64 cursor error", err)
|
||||
}
|
||||
if !strings.Contains(stderr, "pagination stopped at page 2") {
|
||||
t.Fatalf("stderr=%q", stderr)
|
||||
}
|
||||
paging := got["paging"].(map[string]any)
|
||||
if paging["partial"] != true || paging["failedCursor"] != "not-a-number" || paging["pagesFetched"].(float64) != 1 {
|
||||
t.Fatalf("paging = %#v", paging)
|
||||
}
|
||||
if len(caller.calls) != 1 {
|
||||
t.Fatalf("calls=%#v, want no second call with cursor 0", caller.calls)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPagedMCPCommandFirstPageFailureReturnsNoPartial(t *testing.T) {
|
||||
caller := &pagedCommandCaller{steps: []scriptedToolStep{{err: errors.New("boom")}}}
|
||||
got, _, err := runPagedCommandTest(t, caller, pagedCommandMessagesConfig(nil), "--page-all")
|
||||
if err == nil || got != nil {
|
||||
t.Fatalf("result=%#v err=%v, want first-page error without stdout", got, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPagedMCPCommandLaterFailureOutputsPartial(t *testing.T) {
|
||||
caller := &pagedCommandCaller{steps: []scriptedToolStep{
|
||||
{text: `{"result":{"messages":[{"id":"m1"}],"hasMore":true,"nextCursor":"c2"}}`},
|
||||
{err: errors.New("page failed")},
|
||||
}}
|
||||
got, stderr, err := runPagedCommandTest(t, caller, pagedCommandMessagesConfig(nil), "--page-all", "--page-delay", "0")
|
||||
if err == nil || !strings.Contains(stderr, "pagination stopped") {
|
||||
t.Fatalf("err=%v stderr=%q", err, stderr)
|
||||
}
|
||||
paging := got["paging"].(map[string]any)
|
||||
if paging["partial"] != true || paging["failedPage"].(float64) != 2 || paging["itemsFetched"].(float64) != 1 {
|
||||
t.Fatalf("paging = %#v", paging)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPagedMCPCommandCursorCycleOutputsPartial(t *testing.T) {
|
||||
caller := &pagedCommandCaller{steps: []scriptedToolStep{
|
||||
{text: `{"result":{"messages":[{"id":"m1"}],"hasMore":true,"nextCursor":"0"}}`},
|
||||
}}
|
||||
got, _, err := runPagedCommandTest(t, caller, pagedCommandMessagesConfig(nil), "--page-all", "--page-delay", "0")
|
||||
if err == nil {
|
||||
t.Fatal("cursor cycle should return error")
|
||||
}
|
||||
paging := got["paging"].(map[string]any)
|
||||
if paging["partial"] != true || paging["pagesFetched"].(float64) != 1 {
|
||||
t.Fatalf("paging = %#v", paging)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPagedMCPCommandSetJSONPathRejectsNonObjectIntermediate(t *testing.T) {
|
||||
root := map[string]any{"result": "not-object"}
|
||||
|
||||
if setJSONPath(root, "result.messages", []any{}) {
|
||||
t.Fatal("setJSONPath should reject a non-object intermediate")
|
||||
}
|
||||
if root["result"] != "not-object" {
|
||||
t.Fatalf("root=%#v, want original intermediate preserved", root)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPagedMCPCommandCursorValueKeyCoversBoundaryKinds(t *testing.T) {
|
||||
if got := cursorValueKey(7, PagedCursorInt64); got != "7" {
|
||||
t.Fatalf("int cursor key=%q, want 7", got)
|
||||
}
|
||||
if got := cursorValueKey(nil, PagedCursorString); got != "" {
|
||||
t.Fatalf("nil string cursor key=%q, want empty", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPagedMCPCommandNormalizeCursorArgCoversBoundaryKinds(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
value any
|
||||
kind PagedCursorKind
|
||||
want any
|
||||
wantErr string
|
||||
}{
|
||||
{
|
||||
name: "nil string cursor",
|
||||
value: nil,
|
||||
kind: PagedCursorString,
|
||||
want: "",
|
||||
},
|
||||
{
|
||||
name: "int64 cursor",
|
||||
value: int64(9),
|
||||
kind: PagedCursorInt64,
|
||||
want: int64(9),
|
||||
},
|
||||
{
|
||||
name: "int cursor",
|
||||
value: 10,
|
||||
kind: PagedCursorInt64,
|
||||
want: int64(10),
|
||||
},
|
||||
{
|
||||
name: "numeric string cursor",
|
||||
value: " 11 ",
|
||||
kind: PagedCursorInt64,
|
||||
want: int64(11),
|
||||
},
|
||||
{
|
||||
name: "fractional float cursor",
|
||||
value: 1.5,
|
||||
kind: PagedCursorInt64,
|
||||
wantErr: "must be an integer",
|
||||
},
|
||||
{
|
||||
name: "unsupported cursor type",
|
||||
value: []string{"bad"},
|
||||
kind: PagedCursorInt64,
|
||||
wantErr: "int64-compatible",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
got, err := normalizeCursorArg(tt.value, tt.kind)
|
||||
if tt.wantErr != "" {
|
||||
if err == nil || !strings.Contains(err.Error(), tt.wantErr) {
|
||||
t.Fatalf("value=%#v err=%v, want %q", tt.value, err, tt.wantErr)
|
||||
}
|
||||
return
|
||||
}
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got != tt.want {
|
||||
t.Fatalf("value=%#v got=%#v, want %#v", tt.value, got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -31,6 +31,17 @@ func executePR868Command(t *testing.T, root *cobra.Command, args ...string) erro
|
||||
}
|
||||
|
||||
func TestCrossPlatformCoverageMinutesNewSurfaces(t *testing.T) {
|
||||
t.Run("permission add requires explicit policy", func(t *testing.T) {
|
||||
caller := &scriptedToolCaller{}
|
||||
installScriptedCaller(t, caller)
|
||||
err := executePR868Command(t, newMinutesCommand(), "permission", "add", "--ids", "task-1", "--member-uids", "user-1")
|
||||
if err == nil || !strings.Contains(err.Error(), "--policy") {
|
||||
t.Fatalf("permission add without --policy error = %v", err)
|
||||
}
|
||||
if caller.calls != 0 {
|
||||
t.Fatalf("permission add called MCP %d times before required policy validation", caller.calls)
|
||||
}
|
||||
})
|
||||
t.Run("hot-word delete dry-run", func(t *testing.T) {
|
||||
installScriptedCaller(t, &scriptedToolCaller{dry: true, format: "json"})
|
||||
if err := executePR868Command(t, newMinutesCommand(), "hot-word", "delete", "--words", "钉钉,OKR"); err != nil {
|
||||
@@ -91,6 +102,21 @@ func TestCrossPlatformCoverageMinutesNewSurfaces(t *testing.T) {
|
||||
t.Fatal("expected missing policy")
|
||||
}
|
||||
})
|
||||
t.Run("permission apply policy flag is int", func(t *testing.T) {
|
||||
// 数值参数声明为 int 类型 flag;必填校验走 cmd.Flags().Changed,
|
||||
// 不能用 validateRequiredFlags(它把 int 零值当成未传)。
|
||||
cmd, _, err := newMinutesCommand().Find([]string{"permission", "apply"})
|
||||
if err != nil {
|
||||
t.Fatalf("find permission apply: %v", err)
|
||||
}
|
||||
flag := cmd.Flags().Lookup("policy")
|
||||
if flag == nil {
|
||||
t.Fatal("flag --policy not found")
|
||||
}
|
||||
if flag.Value.Type() != "int" {
|
||||
t.Fatalf("flag --policy type = %q, want %q", flag.Value.Type(), "int")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("audio-memo list default", func(t *testing.T) {
|
||||
caller := &scriptedToolCaller{steps: []scriptedToolStep{{text: `{"items":[]}`}}}
|
||||
|
||||
@@ -11,6 +11,7 @@ import (
|
||||
"time"
|
||||
"unicode"
|
||||
|
||||
"github.com/DingTalk-Real-AI/dingtalk-workspace-cli/internal/testseam"
|
||||
"github.com/DingTalk-Real-AI/dingtalk-workspace-cli/pkg/edition"
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/pflag"
|
||||
@@ -157,7 +158,15 @@ func TestCrossPlatformCoverageProductCommandExamplesAreExecutableContracts(t *te
|
||||
deps.Out.errW = io.Discard
|
||||
// Product examples execute real RunE paths; whiteboard insert retries must
|
||||
// not burn the suite timeout on real sleep (race CI uses a 12m package cap).
|
||||
// Sheet export uses helperAfter for progressive polling, so keep that clock
|
||||
// deterministic too; context deadlines alone still accumulate heavily under
|
||||
// Windows coverage instrumentation.
|
||||
whiteboardSleep = func(time.Duration) {}
|
||||
testseam.Swap(t, &helperAfter, func(time.Duration) <-chan time.Time {
|
||||
ch := make(chan time.Time, 1)
|
||||
ch <- time.Now()
|
||||
return ch
|
||||
})
|
||||
httpPutFile = func(context.Context, string, map[string]string, string, int64) error { return nil }
|
||||
httpGetFile = func(_ context.Context, _ string, _ map[string]string, destPath string) error {
|
||||
if destPath == "" {
|
||||
|
||||
@@ -15,6 +15,7 @@ package chat
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"math"
|
||||
"path/filepath"
|
||||
@@ -1081,6 +1082,10 @@ func DownloadMessageResources(
|
||||
continue
|
||||
}
|
||||
preferredName := resourceDownloadPreferredName(data)
|
||||
if preferredName == "" {
|
||||
preferredName, _ = resource["name"].(string)
|
||||
preferredName = strings.TrimSpace(preferredName)
|
||||
}
|
||||
filename := resourceDownloadFilename(resourceURL, preferredName)
|
||||
filename = disambiguateResourceDownloadFilename(filename, downloadedNames)
|
||||
output := filepath.Join(outputDir, filename)
|
||||
@@ -1203,9 +1208,10 @@ func uniqueShortcutStrings(values []string) []string {
|
||||
var MessagesQuerySendStatus = shortcut.Shortcut{
|
||||
Service: "chat",
|
||||
Command: "+messages-query-send-status",
|
||||
Aliases: []string{"+messages-send-status"},
|
||||
Product: "im",
|
||||
Description: "查询消息发送状态",
|
||||
Intent: "当你发消息后拿到 openTaskId、想确认这条消息是否发送成功时使用;只读返回发送状态,需传 --open-task-id。",
|
||||
Description: "查询消息投递状态并衔接后续消息操作",
|
||||
Intent: "当你发消息后拿到 openTaskId、想确认投递结果,或后续 edit/recall/read-status 需要取得 openMessageId 和 openConversationId 时使用;openTaskId 不是消息 ID。结果会保留下层响应,并追加版本化 messageRef 与结构化 nextActions。",
|
||||
Risk: shortcut.RiskRead,
|
||||
Safety: contract.SafetySpec{
|
||||
Effect: "read", Risk: "low",
|
||||
@@ -1218,17 +1224,18 @@ var MessagesQuerySendStatus = shortcut.Shortcut{
|
||||
CanonicalPath: "chat.shortcut_messages_query_send_status",
|
||||
CLIPath: "chat +messages-query-send-status",
|
||||
PrimaryCLIPath: "chat +messages-query-send-status",
|
||||
Aliases: []string{"chat +messages-send-status"},
|
||||
},
|
||||
Description: "查询消息发送状态",
|
||||
Description: "查询消息投递状态并衔接后续消息操作",
|
||||
Interface: &contract.InterfaceSpec{
|
||||
Mode: "composite",
|
||||
Availability: "available",
|
||||
Reason: "Reviewed built-in shortcut adapter: the executable CLI owns validation, optional multi-step orchestration, output projection, and confirmation; the complete command contract is not represented by one pinned MCP interface_ref.",
|
||||
},
|
||||
Selection: contract.SelectionSpec{
|
||||
AgentSummary: "查询消息发送状态",
|
||||
UseWhen: []string{"当你发消息后拿到 openTaskId、想确认这条消息是否发送成功时使用;只读返回发送状态,需传 --open-task-id。"},
|
||||
AvoidWhen: []string{"需要该 Shortcut 未公开的底层参数、原始响应或不同执行语义时,改用对应原子命令"},
|
||||
AgentSummary: "查询消息投递状态并衔接后续消息操作",
|
||||
UseWhen: []string{"当你发消息后拿到 openTaskId、想确认投递结果,或后续 edit/recall/read-status 需要取得 openMessageId 和 openConversationId 时使用;openTaskId 不是消息 ID。结果会保留下层响应,并追加版本化 messageRef 与结构化 nextActions。"},
|
||||
AvoidWhen: []string{"没有 openTaskId、已经有消息 ID,或只需查历史消息内容时不要使用"},
|
||||
Examples: []string{"dws chat +messages-query-send-status --open-task-id <openTaskId>"},
|
||||
},
|
||||
},
|
||||
@@ -1237,7 +1244,12 @@ var MessagesQuerySendStatus = shortcut.Shortcut{
|
||||
},
|
||||
Tips: []string{`dws chat +messages-query-send-status --open-task-id <openTaskId>`},
|
||||
Execute: func(rt *shortcut.RuntimeContext) error {
|
||||
return rt.CallMCP("query_message_send_status", map[string]any{"openTaskId": rt.Str("open-task-id")})
|
||||
taskID := rt.Str("open-task-id")
|
||||
data, err := rt.CallMCPData("im", "query_message_send_status", map[string]any{"openTaskId": taskID})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return rt.Output(chatmsg.ProjectMessageSendStatus(data, taskID))
|
||||
},
|
||||
}
|
||||
|
||||
@@ -1541,7 +1553,28 @@ var MessagesSendCard = shortcut.Shortcut{
|
||||
}
|
||||
content := rt.Str("content")
|
||||
if content == "" {
|
||||
return rt.CallMCP("create_and_send_card", params)
|
||||
if rt.DryRun() {
|
||||
return rt.Output(map[string]any{
|
||||
"contractVersion": chatmsg.StreamingCardContractVersion,
|
||||
"dry_run": true,
|
||||
"executed": false,
|
||||
"preview_kind": "plan",
|
||||
"actionCount": 1,
|
||||
"actions": []map[string]any{{
|
||||
"tool": "create_and_send_card",
|
||||
"arguments": params,
|
||||
}},
|
||||
})
|
||||
}
|
||||
created, err := rt.CallMCPWriteData("im", "create_and_send_card", params)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
bizID := findCardBizID(created)
|
||||
if bizID == "" {
|
||||
return cardCreateMissingBizIDError(created)
|
||||
}
|
||||
return rt.Output(chatmsg.ProjectStreamingCardReceipt(created, bizID))
|
||||
}
|
||||
status := rt.Int("flow-status")
|
||||
if rt.DryRun() {
|
||||
@@ -1592,14 +1625,14 @@ var MessagesSendCard = shortcut.Shortcut{
|
||||
if err != nil {
|
||||
return fmt.Errorf("卡片已创建(bizId=%s),但自动更新失败: %w", bizID, err)
|
||||
}
|
||||
return rt.Output(map[string]any{
|
||||
"contractVersion": currentCardWorkflowContract.Version,
|
||||
"ok": true,
|
||||
"bizId": bizID,
|
||||
"flowStatus": status,
|
||||
"created": created,
|
||||
"updated": updated,
|
||||
})
|
||||
if _, err := chatmsg.VerifyStreamingCardUpdate(bizID, updated); err != nil {
|
||||
return fmt.Errorf("卡片已创建(bizId=%s),但自动更新结果不可信: %w", bizID, cardUpdateVerificationError(bizID, err))
|
||||
}
|
||||
payload := chatmsg.ProjectStreamingCardReceipt(created, bizID)
|
||||
payload["bizId"] = bizID
|
||||
payload["flowStatus"] = status
|
||||
payload["updated"] = updated
|
||||
return rt.Output(payload)
|
||||
},
|
||||
}
|
||||
|
||||
@@ -1694,7 +1727,7 @@ var MessagesUpdateCard = shortcut.Shortcut{
|
||||
Selection: contract.SelectionSpec{
|
||||
AgentSummary: "流式更新卡片内容(最后一次 --flow-status 应为 3)",
|
||||
UseWhen: []string{"当你要向已发送的流式文本卡片持续追加/更新内容时使用;会实际更新卡片,需传 send-card 返回的 bizId、新内容及 flowStatus 1-5(最后一次应为 3 表示完成)。当前不支持 Card JSON 组件或 action callback。"},
|
||||
AvoidWhen: []string{"需要该 Shortcut 未公开的底层参数、原始响应或不同执行语义时,改用对应原子命令"},
|
||||
AvoidWhen: []string{"需要底层原始响应、未公开参数,或由调用方自行管理确认与更新节奏时,改用 chat message update-card"},
|
||||
Examples: []string{"dws chat +messages-update-card --biz-id <bizId> --content \"内容\" --flow-status 3"},
|
||||
},
|
||||
},
|
||||
@@ -1708,20 +1741,91 @@ var MessagesUpdateCard = shortcut.Shortcut{
|
||||
},
|
||||
Tips: []string{`dws chat +messages-update-card --biz-id <bizId> --content "内容" --flow-status 3`},
|
||||
Validate: func(rt *shortcut.RuntimeContext) error {
|
||||
if _, err := chatmsg.NormalizeCardBizID(rt.Str("biz-id")); err != nil {
|
||||
return err
|
||||
}
|
||||
if !validCardFlowStatus(rt.Int("flow-status")) {
|
||||
return fmt.Errorf("--flow-status 必须在 1-5 之间")
|
||||
}
|
||||
return nil
|
||||
},
|
||||
Execute: func(rt *shortcut.RuntimeContext) error {
|
||||
return rt.CallMCP("update_streaming_card", map[string]any{
|
||||
"bizId": rt.Str("biz-id"),
|
||||
// Validate has already normalized and rejected empty, placeholder, and
|
||||
// whitespace-containing values before Execute is entered.
|
||||
bizID, _ := chatmsg.NormalizeCardBizID(rt.Str("biz-id"))
|
||||
params := map[string]any{
|
||||
"bizId": bizID,
|
||||
"msgContent": rt.Str("content"),
|
||||
"flowStatus": rt.Int("flow-status"),
|
||||
})
|
||||
}
|
||||
if rt.DryRun() {
|
||||
return rt.Output(map[string]any{
|
||||
"dry_run": true,
|
||||
"executed": false,
|
||||
"verified": false,
|
||||
"action": map[string]any{
|
||||
"product": "im",
|
||||
"tool": "update_streaming_card",
|
||||
"arguments": params,
|
||||
},
|
||||
})
|
||||
}
|
||||
updated, err := rt.CallMCPWriteData("im", "update_streaming_card", params)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
proof, err := chatmsg.VerifyStreamingCardUpdate(bizID, updated)
|
||||
if err != nil {
|
||||
return cardUpdateVerificationError(bizID, err)
|
||||
}
|
||||
return rt.Output(chatmsg.ProjectStreamingCardUpdate(updated, bizID, proof))
|
||||
},
|
||||
}
|
||||
|
||||
func cardCreateMissingBizIDError(created map[string]any) error {
|
||||
return apperrors.NewAPI(
|
||||
"卡片可能已经创建,但服务端未返回后续更新所需的 bizId;CLI 无法确认卡片工作流可继续",
|
||||
apperrors.WithOperation("create_and_send_card"),
|
||||
apperrors.WithServerKey("im"),
|
||||
apperrors.WithOrigin("client_postcondition"),
|
||||
apperrors.WithFailureStage("verify_card_reference"),
|
||||
apperrors.WithExecutionStarted(true),
|
||||
apperrors.WithRetryable(false),
|
||||
apperrors.WithReason("streaming_card_reference_missing"),
|
||||
apperrors.WithHint("不要盲目重试创建;请保留 trace_id 并推动服务端返回 bizId、openMessageId 和 openConversationId"),
|
||||
apperrors.WithDetails(map[string]any{"created": created}),
|
||||
)
|
||||
}
|
||||
|
||||
func cardUpdateVerificationError(bizID string, verifyErr error) error {
|
||||
reason := "streaming_card_update_unverified"
|
||||
message := "服务端未返回卡片实际更新的证据;为避免假成功,CLI 已将本次操作判为失败"
|
||||
hint := "请检查服务端是否返回 updated=true、affectedCount>0 或等价的明确更新结果"
|
||||
switch {
|
||||
case errors.Is(verifyErr, chatmsg.ErrCardUpdateNotApplied):
|
||||
reason = "streaming_card_update_not_applied"
|
||||
message = "服务端明确表示流式卡片没有被更新"
|
||||
hint = "请确认 bizId 来自 send-card、当前账号有权限且卡片仍允许该状态转换"
|
||||
case errors.Is(verifyErr, chatmsg.ErrCardUpdateBizIDDrift):
|
||||
reason = "streaming_card_update_biz_id_mismatch"
|
||||
message = "服务端返回的 bizId 与本次请求不一致;无法确认目标卡片已更新"
|
||||
hint = "请保留 trace_id 并检查 update_streaming_card 的响应映射"
|
||||
}
|
||||
return apperrors.NewAPI(
|
||||
message,
|
||||
apperrors.WithOperation("update_streaming_card"),
|
||||
apperrors.WithServerKey("im"),
|
||||
apperrors.WithOrigin("client_postcondition"),
|
||||
apperrors.WithFailureStage("verify_update_result"),
|
||||
apperrors.WithExecutionStarted(true),
|
||||
apperrors.WithRetryable(false),
|
||||
apperrors.WithReason(reason),
|
||||
apperrors.WithHint(hint),
|
||||
apperrors.WithDetails(map[string]any{"bizId": bizID}),
|
||||
apperrors.WithCause(verifyErr),
|
||||
)
|
||||
}
|
||||
|
||||
// MessagesResourceURL gets a message resource download url (get_resource_download_url, im).
|
||||
var MessagesResourceURL = shortcut.Shortcut{
|
||||
Service: "chat",
|
||||
|
||||
@@ -360,7 +360,9 @@ func TestCrossPlatformCoverageRecallCardAndLedgerBoundaries(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
helpers.InitDeps(&larkAlignmentCaller{})
|
||||
helpers.InitDeps(&larkAlignmentCaller{responses: map[string]string{
|
||||
"im/update_streaming_card": `{"result":{"updated":true}}`,
|
||||
}})
|
||||
root := newPlatformCoverageRoot()
|
||||
root.SetArgs([]string{"chat", "+messages-update-card", "--biz-id", "b", "--content", "x", "--flow-status", "3", "--yes"})
|
||||
if err := root.Execute(); err != nil {
|
||||
|
||||
@@ -20,6 +20,7 @@ import (
|
||||
apperrors "github.com/DingTalk-Real-AI/dingtalk-workspace-cli/internal/errors"
|
||||
"github.com/DingTalk-Real-AI/dingtalk-workspace-cli/internal/helpers"
|
||||
"github.com/DingTalk-Real-AI/dingtalk-workspace-cli/internal/shortcut"
|
||||
"github.com/DingTalk-Real-AI/dingtalk-workspace-cli/internal/shortcut/chatmsg"
|
||||
)
|
||||
|
||||
type chatOutputErrorWriter struct {
|
||||
@@ -124,6 +125,159 @@ func TestCrossPlatformCoverageIMWorkflowContractsPublishRealPositiveAndNegativeB
|
||||
}
|
||||
}
|
||||
|
||||
func TestCrossPlatformCoverageMessagesSendStatusAliasPublishesWorkflowReceipt(t *testing.T) {
|
||||
fake := &larkAlignmentCaller{responses: map[string]string{
|
||||
"im/query_message_send_status": `{"result":{"status":"SUCCESS","openTaskId":"task-1","openMessageId":"msg-1","openConversationId":"cid-1"}}`,
|
||||
}}
|
||||
helpers.InitDeps(fake)
|
||||
root := newPlatformCoverageRoot()
|
||||
var output bytes.Buffer
|
||||
root.SetOut(&output)
|
||||
root.SetArgs([]string{"chat", "+messages-send-status", "--open-task-id", "task-1"})
|
||||
if err := root.Execute(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(fake.calls) != 1 || fake.calls[0].tool != "query_message_send_status" || fake.calls[0].args["openTaskId"] != "task-1" {
|
||||
t.Fatalf("calls = %#v", fake.calls)
|
||||
}
|
||||
var payload map[string]any
|
||||
if err := json.Unmarshal(output.Bytes(), &payload); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if payload["contractVersion"] != chatmsg.MessageSendStatusContractVersion || payload["readyForMessageActions"] != true {
|
||||
t.Fatalf("payload = %#v", payload)
|
||||
}
|
||||
ref, _ := payload["messageRef"].(map[string]any)
|
||||
if ref["openMessageId"] != "msg-1" || ref["openConversationId"] != "cid-1" {
|
||||
t.Fatalf("messageRef = %#v", ref)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCrossPlatformCoverageMessageWorkflowFailureAndPreviewBranches(t *testing.T) {
|
||||
t.Run("send status lower error", func(t *testing.T) {
|
||||
fake := &larkAlignmentCaller{failProductTool: "im/query_message_send_status"}
|
||||
helpers.InitDeps(fake)
|
||||
root := newPlatformCoverageRoot()
|
||||
root.SetArgs([]string{"chat", "+messages-query-send-status", "--open-task-id", "task-1"})
|
||||
if err := root.Execute(); err == nil || !strings.Contains(err.Error(), "fixture lower call failed") {
|
||||
t.Fatalf("error = %v", err)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("create only dry run", func(t *testing.T) {
|
||||
fake := &larkAlignmentCaller{}
|
||||
helpers.InitDeps(fake)
|
||||
root := newPlatformCoverageRoot()
|
||||
var output bytes.Buffer
|
||||
root.SetOut(&output)
|
||||
root.SetArgs([]string{
|
||||
"chat", "+messages-send-card", "--group", "cid", "--dry-run", "--yes",
|
||||
})
|
||||
if err := root.Execute(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(fake.calls) != 0 {
|
||||
t.Fatalf("dry-run made calls: %#v", fake.calls)
|
||||
}
|
||||
var payload map[string]any
|
||||
if err := json.Unmarshal(output.Bytes(), &payload); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if payload["actionCount"] != float64(1) || payload["executed"] != false {
|
||||
t.Fatalf("payload = %#v", payload)
|
||||
}
|
||||
})
|
||||
|
||||
for _, test := range []struct {
|
||||
name string
|
||||
fake *larkAlignmentCaller
|
||||
wantError string
|
||||
}{
|
||||
{
|
||||
name: "create only lower error",
|
||||
fake: &larkAlignmentCaller{failProductTool: "im/create_and_send_card"},
|
||||
wantError: "fixture lower call failed",
|
||||
},
|
||||
{
|
||||
name: "create only missing biz id",
|
||||
fake: &larkAlignmentCaller{responses: map[string]string{
|
||||
"im/create_and_send_card": `{"result":{"created":true}}`,
|
||||
}},
|
||||
wantError: "未返回后续更新所需的 bizId",
|
||||
},
|
||||
} {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
helpers.InitDeps(test.fake)
|
||||
root := newPlatformCoverageRoot()
|
||||
root.SetArgs([]string{"chat", "+messages-send-card", "--group", "cid", "--yes"})
|
||||
if err := root.Execute(); err == nil || !strings.Contains(err.Error(), test.wantError) {
|
||||
t.Fatalf("error = %v, want substring %q", err, test.wantError)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
t.Run("update card lower error", func(t *testing.T) {
|
||||
fake := &larkAlignmentCaller{failProductTool: "im/update_streaming_card"}
|
||||
helpers.InitDeps(fake)
|
||||
root := newPlatformCoverageRoot()
|
||||
root.SetArgs([]string{
|
||||
"chat", "+messages-update-card",
|
||||
"--biz-id", "biz-1", "--content", "完成", "--flow-status", "3", "--yes",
|
||||
})
|
||||
if err := root.Execute(); err == nil || !strings.Contains(err.Error(), "fixture lower call failed") {
|
||||
t.Fatalf("error = %v", err)
|
||||
}
|
||||
})
|
||||
|
||||
createErr := cardCreateMissingBizIDError(map[string]any{"created": true})
|
||||
var typed *apperrors.Error
|
||||
if !errors.As(createErr, &typed) || typed.Reason != "streaming_card_reference_missing" {
|
||||
t.Fatalf("create error = %#v", createErr)
|
||||
}
|
||||
for _, test := range []struct {
|
||||
cause error
|
||||
wantReason string
|
||||
}{
|
||||
{cause: chatmsg.ErrCardUpdateNotApplied, wantReason: "streaming_card_update_not_applied"},
|
||||
{cause: chatmsg.ErrCardUpdateBizIDDrift, wantReason: "streaming_card_update_biz_id_mismatch"},
|
||||
} {
|
||||
typed = nil
|
||||
err := cardUpdateVerificationError("biz-1", test.cause)
|
||||
if !errors.As(err, &typed) || typed.Reason != test.wantReason {
|
||||
t.Errorf("cardUpdateVerificationError(%v) = %#v", test.cause, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestCrossPlatformCoverageMessagesSendPublishesStatusQueryReceipt(t *testing.T) {
|
||||
fake := &larkAlignmentCaller{responses: map[string]string{
|
||||
"chat/send_personal_message": `{"result":{"openTaskId":"task-send-1"}}`,
|
||||
}}
|
||||
helpers.InitDeps(fake)
|
||||
root := newPlatformCoverageRoot()
|
||||
var output bytes.Buffer
|
||||
root.SetOut(&output)
|
||||
root.SetArgs([]string{
|
||||
"chat", "+messages-send", "--as", "user", "--chat-id", "cid-1",
|
||||
"--text", "hello", "--yes",
|
||||
})
|
||||
if err := root.Execute(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var payload map[string]any
|
||||
if err := json.Unmarshal(output.Bytes(), &payload); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
receipt, _ := payload["sendReceipt"].(map[string]any)
|
||||
if receipt["contractVersion"] != chatmsg.MessageSendReceiptContractVersion || receipt["openTaskId"] != "task-send-1" {
|
||||
t.Fatalf("sendReceipt = %#v", receipt)
|
||||
}
|
||||
actions, _ := receipt["nextActions"].([]any)
|
||||
if len(actions) != 1 {
|
||||
t.Fatalf("nextActions = %#v", actions)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCrossPlatformCoverageMessagesSendBotMultiGroupPublishesPerTargetLedger(t *testing.T) {
|
||||
fake := &larkAlignmentCaller{}
|
||||
helpers.InitDeps(fake)
|
||||
@@ -895,6 +1049,14 @@ func TestCrossPlatformCoverageMessagesSendCardDryRunAndFailureBoundaries(t *test
|
||||
},
|
||||
wantError: "biz-preserved",
|
||||
},
|
||||
{
|
||||
name: "unverified update preserves id",
|
||||
fake: &larkAlignmentCaller{responses: map[string]string{
|
||||
"im/create_and_send_card": `{"bizId":"biz-unverified"}`,
|
||||
"im/update_streaming_card": `{"success":true,"errorCode":null}`,
|
||||
}},
|
||||
wantError: "biz-unverified",
|
||||
},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
helpers.InitDeps(tc.fake)
|
||||
@@ -929,6 +1091,120 @@ func TestCrossPlatformCoverageMessagesSendCardDryRunAndFailureBoundaries(t *test
|
||||
}
|
||||
}
|
||||
|
||||
func TestCrossPlatformCoverageMessagesUpdateCardRejectsFalseSuccess(t *testing.T) {
|
||||
t.Run("agent shortcut owns confirmation boundary", func(t *testing.T) {
|
||||
fake := &larkAlignmentCaller{responses: map[string]string{
|
||||
"im/update_streaming_card": `{"result":{"bizId":"biz-confirm","updated":true}}`,
|
||||
}}
|
||||
helpers.InitDeps(fake)
|
||||
root := newPlatformCoverageRoot()
|
||||
root.SetIn(strings.NewReader(""))
|
||||
root.SetArgs([]string{
|
||||
"chat", "+messages-update-card",
|
||||
"--biz-id", "biz-confirm",
|
||||
"--content", "高层更新",
|
||||
"--flow-status", "3",
|
||||
})
|
||||
err := root.Execute()
|
||||
var typed *apperrors.Error
|
||||
if !errors.As(err, &typed) || typed.Reason != "confirmation_required" {
|
||||
t.Fatalf("error = %#v, want confirmation_required", err)
|
||||
}
|
||||
if len(fake.calls) != 0 {
|
||||
t.Fatalf("unconfirmed shortcut reached MCP: %#v", fake.calls)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("generic success is unverified", func(t *testing.T) {
|
||||
fake := &larkAlignmentCaller{responses: map[string]string{
|
||||
"im/update_streaming_card": `{"success":true,"errorCode":null}`,
|
||||
}}
|
||||
helpers.InitDeps(fake)
|
||||
root := newPlatformCoverageRoot()
|
||||
root.SetArgs([]string{
|
||||
"chat", "+messages-update-card",
|
||||
"--biz-id", "中文乱串",
|
||||
"--content", "完成",
|
||||
"--flow-status", "3",
|
||||
"--yes",
|
||||
})
|
||||
err := root.Execute()
|
||||
var typed *apperrors.Error
|
||||
if !errors.As(err, &typed) || typed.Reason != "streaming_card_update_unverified" {
|
||||
t.Fatalf("error = %#v, want streaming_card_update_unverified", err)
|
||||
}
|
||||
if len(fake.calls) != 1 || fake.calls[0].tool != "update_streaming_card" {
|
||||
t.Fatalf("calls = %#v", fake.calls)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("explicit update evidence succeeds", func(t *testing.T) {
|
||||
fake := &larkAlignmentCaller{responses: map[string]string{
|
||||
"im/update_streaming_card": `{"result":{"bizId":"biz-verified","updated":true}}`,
|
||||
}}
|
||||
helpers.InitDeps(fake)
|
||||
root := newPlatformCoverageRoot()
|
||||
root.SetArgs([]string{
|
||||
"chat", "+messages-update-card",
|
||||
"--biz-id", "biz-verified",
|
||||
"--content", "完成",
|
||||
"--flow-status", "3",
|
||||
"--yes",
|
||||
})
|
||||
if err := root.Execute(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("placeholder fails before write", func(t *testing.T) {
|
||||
fake := &larkAlignmentCaller{}
|
||||
helpers.InitDeps(fake)
|
||||
root := newPlatformCoverageRoot()
|
||||
root.SetArgs([]string{
|
||||
"chat", "+messages-update-card",
|
||||
"--biz-id", "<bizId>",
|
||||
"--content", "完成",
|
||||
"--flow-status", "3",
|
||||
"--yes",
|
||||
})
|
||||
if err := root.Execute(); err == nil || !strings.Contains(err.Error(), "占位符") {
|
||||
t.Fatalf("error = %v, want placeholder validation", err)
|
||||
}
|
||||
if len(fake.calls) != 0 {
|
||||
t.Fatalf("invalid placeholder made calls: %#v", fake.calls)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("dry run only publishes plan", func(t *testing.T) {
|
||||
fake := &larkAlignmentCaller{}
|
||||
helpers.InitDeps(fake)
|
||||
root := newPlatformCoverageRoot()
|
||||
var output bytes.Buffer
|
||||
root.SetOut(&output)
|
||||
root.SetArgs([]string{
|
||||
"chat", "+messages-update-card",
|
||||
"--biz-id", "biz-preview",
|
||||
"--content", "完成",
|
||||
"--flow-status", "3",
|
||||
"--dry-run",
|
||||
"--yes",
|
||||
})
|
||||
if err := root.Execute(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(fake.calls) != 0 {
|
||||
t.Fatalf("dry-run made calls: %#v", fake.calls)
|
||||
}
|
||||
var payload map[string]any
|
||||
if err := json.Unmarshal(output.Bytes(), &payload); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if payload["executed"] != false || payload["verified"] != false {
|
||||
t.Fatalf("dry-run payload = %#v", payload)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestCrossPlatformCoverageFindCardBizIDResponseShapes(t *testing.T) {
|
||||
for _, tc := range []struct {
|
||||
value any
|
||||
@@ -1090,6 +1366,9 @@ func TestCrossPlatformCoverageMessageFileResourceDownloadUsesDriveAndPreservesNa
|
||||
fake.calls[0].args["fileId"] != "drive-file" {
|
||||
t.Fatalf("drive call = %#v", fake.calls)
|
||||
}
|
||||
helpers.InitDeps(&larkAlignmentCaller{responses: map[string]string{
|
||||
"drive/download_file": `{"result":{"downloadUrl":"https://download.dingtalk.com/opaque"}}`,
|
||||
}})
|
||||
|
||||
var ledger map[string]any
|
||||
shortcut.Register(shortcut.Shortcut{
|
||||
|
||||
@@ -75,6 +75,8 @@ func (f *larkAlignmentCaller) CallTool(_ context.Context, product, tool string,
|
||||
text = string(payload)
|
||||
case "im/create_group_conversation":
|
||||
text = `{"result":{"cid":"internal-cid","openCid":"open-cid"}}`
|
||||
case "im/create_and_send_card":
|
||||
text = `{"result":{"bizId":"biz-created"}}`
|
||||
case "im/list_messages_by_ids":
|
||||
text = `{"result":[{"openMessageId":"msg","openConversationId":"cid","senderOpenDingTalkId":"D-inferred","content":"{\"mediaId\":\"@image\"}"}]}`
|
||||
case "im/list_conversations_by_category":
|
||||
|
||||
@@ -19,6 +19,7 @@ import (
|
||||
apperrors "github.com/DingTalk-Real-AI/dingtalk-workspace-cli/internal/errors"
|
||||
"github.com/DingTalk-Real-AI/dingtalk-workspace-cli/internal/helpers"
|
||||
"github.com/DingTalk-Real-AI/dingtalk-workspace-cli/internal/shortcut"
|
||||
"github.com/DingTalk-Real-AI/dingtalk-workspace-cli/internal/shortcut/chatmsg"
|
||||
"github.com/DingTalk-Real-AI/dingtalk-workspace-cli/internal/shortcut/targetresolver"
|
||||
)
|
||||
|
||||
@@ -372,12 +373,16 @@ func executeUnifiedMessageWrite(rt *shortcut.RuntimeContext, product, tool strin
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return rt.Output(map[string]any{
|
||||
payload := map[string]any{
|
||||
"ok": true,
|
||||
"identity": messagesSendIdentity(rt),
|
||||
"tool": tool,
|
||||
"result": data,
|
||||
})
|
||||
}
|
||||
if messagesSendIdentity(rt) == "user" && tool == "send_personal_message" {
|
||||
payload["sendReceipt"] = chatmsg.ProjectMessageSendReceipt(data)
|
||||
}
|
||||
return rt.Output(payload)
|
||||
}
|
||||
|
||||
func messagesSendIdentity(rt *shortcut.RuntimeContext) string {
|
||||
@@ -612,7 +617,7 @@ func executeMessagesSendUserFile(
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return rt.Output(map[string]any{
|
||||
payload := map[string]any{
|
||||
"ok": true,
|
||||
"identity": "user",
|
||||
"tool": "send_personal_message",
|
||||
@@ -624,7 +629,9 @@ func executeMessagesSendUserFile(
|
||||
"sizeBytes": meta.FileSize,
|
||||
},
|
||||
"result": data,
|
||||
})
|
||||
}
|
||||
payload["sendReceipt"] = chatmsg.ProjectMessageSendReceipt(data)
|
||||
return rt.Output(payload)
|
||||
}
|
||||
|
||||
func addMessagesSendUserTarget(params map[string]any, group, openID string) {
|
||||
|
||||
@@ -0,0 +1,65 @@
|
||||
// Copyright 2026 Alibaba Group
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
|
||||
package chatmsg
|
||||
|
||||
import "strings"
|
||||
|
||||
// StreamingCardContractVersion identifies the additive card receipt emitted by
|
||||
// high-level card shortcuts.
|
||||
const StreamingCardContractVersion = "im.streaming-card.v1"
|
||||
|
||||
// ProjectStreamingCardReceipt publishes every server-returned identifier in a
|
||||
// single cardRef. referencePairAvailable means this response contained both
|
||||
// the update identifier and the visible message identifiers; it does not claim
|
||||
// that older messages can be resolved without server-side mapping support.
|
||||
func ProjectStreamingCardReceipt(created map[string]any, bizID string) map[string]any {
|
||||
bizID = strings.TrimSpace(bizID)
|
||||
messageID := firstSendStatusString(created, "openMessageId", "messageId", "msgId")
|
||||
conversationID := firstSendStatusString(created, "openConversationId", "conversationId", "openCid")
|
||||
cardRef := map[string]any{}
|
||||
if bizID != "" {
|
||||
cardRef["bizId"] = bizID
|
||||
}
|
||||
if messageID != "" {
|
||||
cardRef["openMessageId"] = messageID
|
||||
}
|
||||
if conversationID != "" {
|
||||
cardRef["openConversationId"] = conversationID
|
||||
}
|
||||
pairAvailable := bizID != "" && messageID != "" && conversationID != ""
|
||||
payload := map[string]any{
|
||||
"contractVersion": StreamingCardContractVersion,
|
||||
"ok": true,
|
||||
"cardRef": cardRef,
|
||||
"referencePairAvailable": pairAvailable,
|
||||
"created": created,
|
||||
"nextActions": []map[string]any{},
|
||||
}
|
||||
if bizID != "" {
|
||||
payload["nextActions"] = []map[string]any{{
|
||||
"cliPath": "chat +messages-update-card",
|
||||
"arguments": map[string]any{
|
||||
"biz-id": bizID,
|
||||
},
|
||||
"requiredArguments": []string{"content", "flow-status"},
|
||||
"ready": false,
|
||||
}}
|
||||
}
|
||||
if !pairAvailable {
|
||||
payload["capabilityGap"] = "服务端尚未同时返回 bizId、openMessageId 和 openConversationId;CLI 只能保留本次响应,不能据此承诺从历史消息反向恢复 bizId"
|
||||
}
|
||||
return payload
|
||||
}
|
||||
|
||||
// ProjectStreamingCardUpdate preserves the lower response while making the
|
||||
// verified target explicit for downstream consumers.
|
||||
func ProjectStreamingCardUpdate(updated map[string]any, bizID, proof string) map[string]any {
|
||||
payload := cloneSendStatusMap(updated)
|
||||
payload["contractVersion"] = StreamingCardContractVersion
|
||||
payload["cardRef"] = map[string]any{"bizId": strings.TrimSpace(bizID)}
|
||||
payload["verified"] = true
|
||||
payload["verificationEvidence"] = proof
|
||||
return payload
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
// Copyright 2026 Alibaba Group
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
|
||||
package chatmsg
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestCrossPlatformCoverageProjectStreamingCardReceipt(t *testing.T) {
|
||||
complete := ProjectStreamingCardReceipt(map[string]any{
|
||||
"result": map[string]any{
|
||||
"bizId": "biz-1",
|
||||
"openMessageId": "msg-1",
|
||||
"openConversationId": "cid-1",
|
||||
},
|
||||
}, "biz-1")
|
||||
if complete["contractVersion"] != StreamingCardContractVersion || complete["referencePairAvailable"] != true {
|
||||
t.Fatalf("complete receipt = %#v", complete)
|
||||
}
|
||||
ref, _ := complete["cardRef"].(map[string]any)
|
||||
if ref["bizId"] != "biz-1" || ref["openMessageId"] != "msg-1" || ref["openConversationId"] != "cid-1" {
|
||||
t.Fatalf("cardRef = %#v", ref)
|
||||
}
|
||||
if _, exists := complete["capabilityGap"]; exists {
|
||||
t.Fatalf("complete receipt has capability gap: %#v", complete)
|
||||
}
|
||||
|
||||
partial := ProjectStreamingCardReceipt(map[string]any{"result": map[string]any{"bizId": "biz-2"}}, "biz-2")
|
||||
if partial["referencePairAvailable"] != false || partial["capabilityGap"] == "" {
|
||||
t.Fatalf("partial receipt = %#v", partial)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCrossPlatformCoverageProjectStreamingCardUpdate(t *testing.T) {
|
||||
payload := ProjectStreamingCardUpdate(map[string]any{"result": map[string]any{"updated": true}}, "biz-1", "updated=true")
|
||||
if payload["contractVersion"] != StreamingCardContractVersion || payload["verified"] != true || payload["verificationEvidence"] != "updated=true" {
|
||||
t.Fatalf("payload = %#v", payload)
|
||||
}
|
||||
if _, exists := payload["result"]; !exists {
|
||||
t.Fatal("lower response was not preserved")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,176 @@
|
||||
// Copyright 2026 Alibaba Group
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package chatmsg
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
"unicode"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrCardUpdateNotApplied = errors.New("streaming card update was not applied")
|
||||
ErrCardUpdateUnverified = errors.New("streaming card update could not be verified")
|
||||
ErrCardUpdateBizIDDrift = errors.New("streaming card update returned a different bizId")
|
||||
)
|
||||
|
||||
// NormalizeCardBizID performs only format-independent checks. bizId is an
|
||||
// opaque server-issued identifier; a stricter character or prefix contract
|
||||
// must not be invented by the CLI without an authoritative API declaration.
|
||||
func NormalizeCardBizID(raw string) (string, error) {
|
||||
bizID := strings.TrimSpace(raw)
|
||||
if bizID == "" {
|
||||
return "", fmt.Errorf("--biz-id 不能为空")
|
||||
}
|
||||
for _, r := range bizID {
|
||||
if unicode.IsControl(r) || unicode.IsSpace(r) {
|
||||
return "", fmt.Errorf("--biz-id 必须是 send-card 返回的单个不透明 ID,不能包含空白或控制字符")
|
||||
}
|
||||
}
|
||||
if isCardBizIDPlaceholder(bizID) {
|
||||
return "", fmt.Errorf("--biz-id 仍是占位符 %q;请传入 send-card 实际返回的 bizId", bizID)
|
||||
}
|
||||
return bizID, nil
|
||||
}
|
||||
|
||||
func isCardBizIDPlaceholder(value string) bool {
|
||||
normalized := strings.ToLower(strings.TrimSpace(value))
|
||||
switch normalized {
|
||||
case "bizid", "biz-id", "your-biz-id", "your_biz_id",
|
||||
"<bizid>", "<biz-id>", "<your-biz-id>",
|
||||
"{bizid}", "{biz-id}", "${bizid}", "${biz-id}":
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
// VerifyStreamingCardUpdate requires affirmative evidence that the requested
|
||||
// write took effect. A transport-level {success:true,errorCode:null} only says
|
||||
// that the RPC returned normally and is deliberately not accepted as proof of
|
||||
// a card update.
|
||||
func VerifyStreamingCardUpdate(requestedBizID string, response map[string]any) (string, error) {
|
||||
requestedBizID = strings.TrimSpace(requestedBizID)
|
||||
observation := cardUpdateObservation{bizIDs: map[string]struct{}{}}
|
||||
observeCardUpdate(response, &observation)
|
||||
|
||||
for responseBizID := range observation.bizIDs {
|
||||
if requestedBizID != "" && responseBizID != requestedBizID {
|
||||
return "", fmt.Errorf("%w: requested %q, response %q", ErrCardUpdateBizIDDrift, requestedBizID, responseBizID)
|
||||
}
|
||||
}
|
||||
if observation.positiveEvidence != "" && observation.negativeEvidence != "" {
|
||||
return "", fmt.Errorf("%w: conflicting evidence %s and %s", ErrCardUpdateUnverified, observation.positiveEvidence, observation.negativeEvidence)
|
||||
}
|
||||
if observation.positiveEvidence != "" {
|
||||
return observation.positiveEvidence, nil
|
||||
}
|
||||
if observation.negativeEvidence != "" {
|
||||
return "", fmt.Errorf("%w: %s", ErrCardUpdateNotApplied, observation.negativeEvidence)
|
||||
}
|
||||
return "", ErrCardUpdateUnverified
|
||||
}
|
||||
|
||||
type cardUpdateObservation struct {
|
||||
bizIDs map[string]struct{}
|
||||
positiveEvidence string
|
||||
negativeEvidence string
|
||||
}
|
||||
|
||||
func observeCardUpdate(value any, observation *cardUpdateObservation) {
|
||||
switch typed := value.(type) {
|
||||
case map[string]any:
|
||||
observeCardUpdateMap(typed, observation)
|
||||
case []any:
|
||||
for _, child := range typed {
|
||||
observeCardUpdate(child, observation)
|
||||
}
|
||||
case bool:
|
||||
if typed {
|
||||
setPositiveCardUpdateEvidence(observation, "result=true")
|
||||
} else {
|
||||
setNegativeCardUpdateEvidence(observation, "result=false")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func observeCardUpdateMap(value map[string]any, observation *cardUpdateObservation) {
|
||||
for _, key := range []string{"bizId", "bizID", "biz_id"} {
|
||||
if candidate, ok := value[key].(string); ok && strings.TrimSpace(candidate) != "" {
|
||||
observation.bizIDs[strings.TrimSpace(candidate)] = struct{}{}
|
||||
}
|
||||
}
|
||||
|
||||
for _, key := range []string{"updated", "applied"} {
|
||||
if applied, ok := value[key].(bool); ok {
|
||||
if applied {
|
||||
setPositiveCardUpdateEvidence(observation, key+"=true")
|
||||
} else {
|
||||
setNegativeCardUpdateEvidence(observation, key+"=false")
|
||||
}
|
||||
}
|
||||
}
|
||||
for _, key := range []string{"affectedCount", "updatedCount", "modifiedCount"} {
|
||||
if count, ok := cardUpdateCount(value[key]); ok {
|
||||
if count > 0 {
|
||||
setPositiveCardUpdateEvidence(observation, fmt.Sprintf("%s=%d", key, count))
|
||||
} else if count == 0 {
|
||||
setNegativeCardUpdateEvidence(observation, fmt.Sprintf("%s=%d", key, count))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Only documented response envelopes are traversed. This prevents an
|
||||
// unrelated extension field containing "updated":true from proving the
|
||||
// business write.
|
||||
for _, key := range []string{"result", "data", "response", "card"} {
|
||||
if child, exists := value[key]; exists {
|
||||
observeCardUpdate(child, observation)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func setPositiveCardUpdateEvidence(observation *cardUpdateObservation, evidence string) {
|
||||
if observation.positiveEvidence == "" {
|
||||
observation.positiveEvidence = evidence
|
||||
}
|
||||
}
|
||||
|
||||
func setNegativeCardUpdateEvidence(observation *cardUpdateObservation, evidence string) {
|
||||
if observation.negativeEvidence == "" {
|
||||
observation.negativeEvidence = evidence
|
||||
}
|
||||
}
|
||||
|
||||
func cardUpdateCount(value any) (int64, bool) {
|
||||
switch typed := value.(type) {
|
||||
case int:
|
||||
return int64(typed), true
|
||||
case int32:
|
||||
return int64(typed), true
|
||||
case int64:
|
||||
return typed, true
|
||||
case float32:
|
||||
return int64(typed), float32(int64(typed)) == typed
|
||||
case float64:
|
||||
return int64(typed), float64(int64(typed)) == typed
|
||||
case json.Number:
|
||||
count, err := typed.Int64()
|
||||
return count, err == nil
|
||||
default:
|
||||
return 0, false
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,107 @@
|
||||
// Copyright 2026 Alibaba Group
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package chatmsg
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestCrossPlatformCoverageNormalizeCardBizID(t *testing.T) {
|
||||
for _, test := range []struct {
|
||||
name string
|
||||
raw string
|
||||
want string
|
||||
wantErr bool
|
||||
}{
|
||||
{name: "opaque id", raw: " card-token-1 ", want: "card-token-1"},
|
||||
{name: "opaque unicode remains server owned", raw: "中文乱串", want: "中文乱串"},
|
||||
{name: "empty", raw: " ", wantErr: true},
|
||||
{name: "placeholder", raw: "<bizId>", wantErr: true},
|
||||
{name: "internal space", raw: "card token", wantErr: true},
|
||||
{name: "control", raw: "card\ntoken", wantErr: true},
|
||||
} {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
got, err := NormalizeCardBizID(test.raw)
|
||||
if test.wantErr {
|
||||
if err == nil {
|
||||
t.Fatalf("NormalizeCardBizID(%q) unexpectedly succeeded", test.raw)
|
||||
}
|
||||
return
|
||||
}
|
||||
if err != nil || got != test.want {
|
||||
t.Fatalf("NormalizeCardBizID(%q) = %q, %v; want %q", test.raw, got, err, test.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestCrossPlatformCoverageVerifyStreamingCardUpdate(t *testing.T) {
|
||||
for _, test := range []struct {
|
||||
name string
|
||||
response map[string]any
|
||||
wantProof string
|
||||
wantErrIs error
|
||||
}{
|
||||
{name: "updated", response: map[string]any{"result": map[string]any{"updated": true}}, wantProof: "updated=true"},
|
||||
{name: "affected", response: map[string]any{"data": map[string]any{"affectedCount": float64(1)}}, wantProof: "affectedCount=1"},
|
||||
{name: "boolean result", response: map[string]any{"result": true}, wantProof: "result=true"},
|
||||
{name: "boolean false result", response: map[string]any{"result": false}, wantErrIs: ErrCardUpdateNotApplied},
|
||||
{name: "matching id", response: map[string]any{"result": map[string]any{"bizId": "biz-1", "applied": true}}, wantProof: "applied=true"},
|
||||
{name: "conflicting evidence", response: map[string]any{"updated": true, "applied": false}, wantErrIs: ErrCardUpdateUnverified},
|
||||
{name: "zero affected", response: map[string]any{"affectedCount": 0}, wantErrIs: ErrCardUpdateNotApplied},
|
||||
{name: "false success has no write proof", response: map[string]any{"success": true, "errorCode": nil}, wantErrIs: ErrCardUpdateUnverified},
|
||||
{name: "explicitly not updated", response: map[string]any{"result": map[string]any{"updated": false}}, wantErrIs: ErrCardUpdateNotApplied},
|
||||
{name: "mismatched id", response: map[string]any{"result": map[string]any{"bizId": "biz-2", "updated": true}}, wantErrIs: ErrCardUpdateBizIDDrift},
|
||||
{name: "unrelated extension ignored", response: map[string]any{"extension": map[string]any{"updated": true}}, wantErrIs: ErrCardUpdateUnverified},
|
||||
} {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
proof, err := VerifyStreamingCardUpdate("biz-1", test.response)
|
||||
if test.wantErrIs != nil {
|
||||
if !errors.Is(err, test.wantErrIs) {
|
||||
t.Fatalf("VerifyStreamingCardUpdate error = %v, want errors.Is(_, %v)", err, test.wantErrIs)
|
||||
}
|
||||
return
|
||||
}
|
||||
if err != nil || proof != test.wantProof {
|
||||
t.Fatalf("VerifyStreamingCardUpdate = %q, %v; want %q", proof, err, test.wantProof)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestCrossPlatformCoverageCardUpdateCountScalarVariants(t *testing.T) {
|
||||
for _, test := range []struct {
|
||||
value any
|
||||
want int64
|
||||
ok bool
|
||||
}{
|
||||
{value: int(1), want: 1, ok: true},
|
||||
{value: int32(2), want: 2, ok: true},
|
||||
{value: int64(3), want: 3, ok: true},
|
||||
{value: float32(4), want: 4, ok: true},
|
||||
{value: float32(4.5), want: 4, ok: false},
|
||||
{value: float64(5), want: 5, ok: true},
|
||||
{value: float64(5.5), want: 5, ok: false},
|
||||
{value: json.Number("6"), want: 6, ok: true},
|
||||
{value: json.Number("6.5"), ok: false},
|
||||
{value: "7", ok: false},
|
||||
} {
|
||||
got, ok := cardUpdateCount(test.value)
|
||||
if got != test.want || ok != test.ok {
|
||||
t.Errorf("cardUpdateCount(%#v) = (%d, %v), want (%d, %v)", test.value, got, ok, test.want, test.ok)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -91,6 +91,7 @@ var messageResultContractV1 = MessageResultContract{
|
||||
"failedCount",
|
||||
"failures",
|
||||
"partial",
|
||||
"scope",
|
||||
"resourceDownloads",
|
||||
},
|
||||
}
|
||||
@@ -283,9 +284,10 @@ func SenderType(m map[string]any) any {
|
||||
// legacy aliases such as time or msgType, but the underlying identity,
|
||||
// context, reaction, quote, forward, and resource semantics come from here.
|
||||
func ProjectMessageV1(m map[string]any, includeReactions bool) map[string]any {
|
||||
ownedResources := Resources(m)
|
||||
row := map[string]any{
|
||||
"sender": Sender(m),
|
||||
"text": Text(m),
|
||||
"text": projectedResourceText(m, ownedResources),
|
||||
"createTime": CreateTime(m),
|
||||
}
|
||||
if value := MessageID(m); value != nil {
|
||||
@@ -357,7 +359,8 @@ func QuotedMessage(m map[string]any) map[string]any {
|
||||
if value := Sender(quoted); value != nil {
|
||||
out["sender"] = value
|
||||
}
|
||||
if value := Text(quoted); value != nil {
|
||||
resources := Resources(quoted)
|
||||
if value := projectedResourceText(quoted, resources); value != nil {
|
||||
out["text"] = value
|
||||
}
|
||||
if value := CreateTime(quoted); value != nil {
|
||||
@@ -366,7 +369,7 @@ func QuotedMessage(m map[string]any) map[string]any {
|
||||
if value := MessageType(quoted); value != nil {
|
||||
out["messageType"] = value
|
||||
}
|
||||
if resources := Resources(quoted); len(resources) > 0 {
|
||||
if len(resources) > 0 {
|
||||
out["resourceRefs"] = resources
|
||||
}
|
||||
return out
|
||||
@@ -400,10 +403,14 @@ func Resources(m map[string]any) []map[string]any {
|
||||
collectResourceIDs(m, "mediaid", mediaIDTextRE, &mediaIDs)
|
||||
mediaIDs = uniqueResourceIDs(mediaIDs)
|
||||
sort.Strings(mediaIDs)
|
||||
mediaNames := make(map[string]resourceNameCandidate)
|
||||
collectResourceNames(m, "mediaid", mediaNames)
|
||||
fileIDs := make([]string, 0)
|
||||
collectResourceIDs(m, "fileid", fileIDTextRE, &fileIDs)
|
||||
fileIDs = uniqueResourceIDs(fileIDs)
|
||||
sort.Strings(fileIDs)
|
||||
fileNames := make(map[string]resourceNameCandidate)
|
||||
collectResourceNames(m, "fileid", fileNames)
|
||||
if len(mediaIDs) == 0 && len(fileIDs) == 0 {
|
||||
return nil
|
||||
}
|
||||
@@ -434,7 +441,7 @@ func Resources(m map[string]any) []map[string]any {
|
||||
} else {
|
||||
missing = append(missing, "open-conversation-id")
|
||||
}
|
||||
out = append(out, map[string]any{
|
||||
resource := map[string]any{
|
||||
"type": "mediaId",
|
||||
"resourceId": id,
|
||||
"download": map[string]any{
|
||||
@@ -443,10 +450,14 @@ func Resources(m map[string]any) []map[string]any {
|
||||
"ready": len(missing) == 0,
|
||||
"missing": missing,
|
||||
},
|
||||
})
|
||||
}
|
||||
if candidate, ok := mediaNames[id]; ok {
|
||||
resource["name"] = candidate.name
|
||||
}
|
||||
out = append(out, resource)
|
||||
}
|
||||
for _, id := range fileIDs {
|
||||
out = append(out, map[string]any{
|
||||
resource := map[string]any{
|
||||
"type": "fileId",
|
||||
"resourceId": id,
|
||||
"download": map[string]any{
|
||||
@@ -458,7 +469,11 @@ func Resources(m map[string]any) []map[string]any {
|
||||
"ready": true,
|
||||
"missing": []string{},
|
||||
},
|
||||
})
|
||||
}
|
||||
if candidate, ok := fileNames[id]; ok {
|
||||
resource["name"] = candidate.name
|
||||
}
|
||||
out = append(out, resource)
|
||||
}
|
||||
return out
|
||||
}
|
||||
@@ -503,6 +518,158 @@ func resourcesDeep(m map[string]any, inheritedConversationID string, depth int)
|
||||
|
||||
var mediaIDTextRE = regexp.MustCompile(`(?i)\bmedia[_-]?id\s*[:=]\s*["']?([^"'\s)\]}>,]+)`)
|
||||
var fileIDTextRE = regexp.MustCompile(`(?i)\bfile[_-]?id\s*[:=]\s*["']?([^"'\s)\]}>,]+)`)
|
||||
var fileNameAndIDTextRE = regexp.MustCompile(`(?i)\[文件\]\s*([^\r\n]*?)\s+file[_-]?id\s*[:=]\s*["']?([^"'\s)\]}>,]+)`)
|
||||
var legacyResourceDownloadHintRE = regexp.MustCompile(`\s*注意:如需下载使用dws\s+(?:chat message download-media|drive download)命令下载\s*`)
|
||||
|
||||
// projectedResourceText removes only the exact, machine-generated download
|
||||
// hint emitted by older IM APIs. The readable resource marker and ID remain in
|
||||
// text, while resourceRefs publishes the current executable download command.
|
||||
// Text without an owned mediaId/fileId is left byte-for-byte unchanged so an
|
||||
// ordinary user sentence mentioning a command can never be rewritten.
|
||||
func projectedResourceText(m map[string]any, resources []map[string]any) any {
|
||||
value := Text(m)
|
||||
text, ok := value.(string)
|
||||
if !ok || len(resources) == 0 ||
|
||||
(!mediaIDTextRE.MatchString(text) && !fileIDTextRE.MatchString(text)) {
|
||||
return value
|
||||
}
|
||||
return strings.TrimSpace(legacyResourceDownloadHintRE.ReplaceAllString(text, ""))
|
||||
}
|
||||
|
||||
type resourceNameCandidate struct {
|
||||
name string
|
||||
priority int
|
||||
}
|
||||
|
||||
const (
|
||||
resourceNamePriorityText = 1
|
||||
resourceNamePriorityStructured = 2
|
||||
)
|
||||
|
||||
// collectResourceNames keeps a resource ID paired with a name only when both
|
||||
// are present in the same structured object or in the legacy, machine-shaped
|
||||
// "[文件] name fileId: id" text. This deliberately does not borrow a generic
|
||||
// message title or sender name: an unknown resource name is safer than a
|
||||
// plausible but incorrect one.
|
||||
func collectResourceNames(value any, targetKey string, out map[string]resourceNameCandidate) {
|
||||
switch typed := value.(type) {
|
||||
case map[string]any:
|
||||
directIDs := directResourceIDs(typed, targetKey)
|
||||
if len(directIDs) == 1 {
|
||||
if name := directResourceName(typed, targetKey); name != "" {
|
||||
recordResourceName(out, directIDs[0], name, resourceNamePriorityStructured)
|
||||
}
|
||||
}
|
||||
keys := make([]string, 0, len(typed))
|
||||
for key := range typed {
|
||||
keys = append(keys, key)
|
||||
}
|
||||
sort.Strings(keys)
|
||||
for _, key := range keys {
|
||||
if isNestedMessageBoundaryKey(normalizeMessageKey(key)) {
|
||||
continue
|
||||
}
|
||||
collectResourceNames(typed[key], targetKey, out)
|
||||
}
|
||||
case []any:
|
||||
for _, child := range typed {
|
||||
collectResourceNames(child, targetKey, out)
|
||||
}
|
||||
case []map[string]any:
|
||||
for _, child := range typed {
|
||||
collectResourceNames(child, targetKey, out)
|
||||
}
|
||||
case string:
|
||||
if targetKey == "fileid" {
|
||||
for _, match := range fileNameAndIDTextRE.FindAllStringSubmatch(typed, -1) {
|
||||
name := strings.TrimSpace(match[1])
|
||||
id := resourceIDScalar(match[2])
|
||||
if name != "" && id != "" {
|
||||
recordResourceName(out, id, name, resourceNamePriorityText)
|
||||
}
|
||||
}
|
||||
}
|
||||
trimmed := strings.TrimSpace(typed)
|
||||
if strings.HasPrefix(trimmed, "{") || strings.HasPrefix(trimmed, "[") {
|
||||
var decoded any
|
||||
if json.Unmarshal([]byte(trimmed), &decoded) == nil {
|
||||
collectResourceNames(decoded, targetKey, out)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func directResourceIDs(value map[string]any, targetKey string) []string {
|
||||
resourceType := normalizeMessageKey(strings.TrimSpace(fmt.Sprint(
|
||||
firstMessageValue(value, "resourceType", "resource_type"))))
|
||||
keys := make([]string, 0, len(value))
|
||||
for key := range value {
|
||||
keys = append(keys, key)
|
||||
}
|
||||
sort.Strings(keys)
|
||||
ids := make([]string, 0, 1)
|
||||
for _, key := range keys {
|
||||
normalizedKey := normalizeMessageKey(key)
|
||||
if normalizedKey != targetKey &&
|
||||
!(normalizedKey == "resourceid" && resourceType == targetKey) {
|
||||
continue
|
||||
}
|
||||
if id := resourceIDScalar(value[key]); id != "" {
|
||||
ids = append(ids, id)
|
||||
}
|
||||
}
|
||||
return uniqueResourceIDs(ids)
|
||||
}
|
||||
|
||||
func directResourceName(value map[string]any, targetKey string) string {
|
||||
for _, wanted := range []string{"filename", "resourcename", "originalfilename"} {
|
||||
if name := directResourceString(value, wanted); name != "" {
|
||||
return name
|
||||
}
|
||||
}
|
||||
// A bare "name" is accepted only inside an explicit resource envelope.
|
||||
// Message rows also commonly contain a sender/group name, which must never
|
||||
// become the attachment filename merely because the row has a resource ID.
|
||||
resourceType := normalizeMessageKey(strings.TrimSpace(fmt.Sprint(
|
||||
firstMessageValue(value, "resourceType", "resource_type"))))
|
||||
if resourceType == targetKey && directResourceString(value, "resourceid") != "" {
|
||||
return directResourceString(value, "name")
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func directResourceString(value map[string]any, wanted string) string {
|
||||
keys := make([]string, 0, len(value))
|
||||
for key := range value {
|
||||
keys = append(keys, key)
|
||||
}
|
||||
sort.Strings(keys)
|
||||
for _, key := range keys {
|
||||
if normalizeMessageKey(key) != wanted {
|
||||
continue
|
||||
}
|
||||
if text, ok := value[key].(string); ok {
|
||||
return strings.TrimSpace(text)
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func recordResourceName(
|
||||
out map[string]resourceNameCandidate,
|
||||
id, name string,
|
||||
priority int,
|
||||
) {
|
||||
id = resourceIDScalar(id)
|
||||
name = strings.TrimSpace(name)
|
||||
if id == "" || name == "" {
|
||||
return
|
||||
}
|
||||
if current, ok := out[id]; ok && current.priority >= priority {
|
||||
return
|
||||
}
|
||||
out[id] = resourceNameCandidate{name: name, priority: priority}
|
||||
}
|
||||
|
||||
func collectResourceIDs(value any, targetKey string, textPattern *regexp.Regexp, out *[]string) {
|
||||
switch typed := value.(type) {
|
||||
|
||||
@@ -540,6 +540,28 @@ func TestCrossPlatformCoverageResourcesBuildsActionableDownloadReferences(t *tes
|
||||
}
|
||||
}
|
||||
|
||||
func TestCrossPlatformCoverageResourcesKeepsNestedResourceNamesWithTheirOwner(t *testing.T) {
|
||||
message := map[string]any{
|
||||
"openMessageId": "parent-message",
|
||||
"openConversationId": "cid-1",
|
||||
"content": `{"fileId":"parent-file","fileName":"parent.pdf"}`,
|
||||
"quotedMessage": map[string]any{
|
||||
"openMessageId": "quoted-message",
|
||||
"content": `{"fileId":"quoted-file","file_name":"quoted.pdf"}`,
|
||||
},
|
||||
}
|
||||
resources := ResourcesDeep(message)
|
||||
if len(resources) != 2 {
|
||||
t.Fatalf("resources = %#v", resources)
|
||||
}
|
||||
if resources[0]["resourceId"] != "parent-file" || resources[0]["name"] != "parent.pdf" {
|
||||
t.Fatalf("parent resource = %#v", resources[0])
|
||||
}
|
||||
if resources[1]["resourceId"] != "quoted-file" || resources[1]["name"] != "quoted.pdf" {
|
||||
t.Fatalf("quoted resource = %#v", resources[1])
|
||||
}
|
||||
}
|
||||
|
||||
func TestCrossPlatformCoverageResourcesReportsMissingDownloadContext(t *testing.T) {
|
||||
resources := Resources(map[string]any{"content": `{"mediaId":"@image-a"}`})
|
||||
if len(resources) != 1 {
|
||||
|
||||
@@ -28,16 +28,18 @@ func TestCrossPlatformCoverageQuotedResourcesAndScalarVariants(t *testing.T) {
|
||||
}
|
||||
resources := Resources(map[string]any{
|
||||
"attachments": []map[string]any{
|
||||
{"resourceType": "mediaId", "resourceId": "@file-a"},
|
||||
{"resourceType": "fileId", "resourceId": "drive-file"},
|
||||
{"resourceType": "mediaId", "resourceId": "@file-a", "name": "photo.png"},
|
||||
{"resourceType": "fileId", "resourceId": "drive-file", "fileName": "canonical-report.txt"},
|
||||
{"mediaId": 42, "fileId": 42},
|
||||
},
|
||||
"content": `[文件] report.txt fileId: drive-file`,
|
||||
})
|
||||
if len(resources) != 2 ||
|
||||
resources[0]["resourceId"] != "@file-a" ||
|
||||
resources[0]["name"] != "photo.png" ||
|
||||
resources[1]["resourceId"] != "drive-file" ||
|
||||
resources[1]["type"] != "fileId" {
|
||||
resources[1]["type"] != "fileId" ||
|
||||
resources[1]["name"] != "canonical-report.txt" {
|
||||
t.Fatalf("resources = %#v", resources)
|
||||
}
|
||||
fileDownload := resources[1]["download"].(map[string]any)
|
||||
@@ -53,6 +55,65 @@ func TestCrossPlatformCoverageQuotedResourcesAndScalarVariants(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestCrossPlatformCoverageResourcesExtractsLegacyFileNameWithoutGuessing(t *testing.T) {
|
||||
resources := Resources(map[string]any{
|
||||
"name": "sender-name-must-not-leak",
|
||||
"openMessageId": "msg-1",
|
||||
"openConversationId": "cid-1",
|
||||
"content": `[文件] 项目最终报告 2026.pdf fileId: drive-file 注意:如需下载使用旧命令`,
|
||||
"unrelatedAttachment": map[string]any{"mediaId": "@image-without-name"},
|
||||
})
|
||||
if len(resources) != 2 {
|
||||
t.Fatalf("resources = %#v", resources)
|
||||
}
|
||||
if resources[0]["resourceId"] != "@image-without-name" {
|
||||
t.Fatalf("first resource = %#v", resources[0])
|
||||
}
|
||||
if _, leaked := resources[0]["name"]; leaked {
|
||||
t.Fatalf("message sender name leaked into media resource: %#v", resources[0])
|
||||
}
|
||||
if resources[1]["resourceId"] != "drive-file" ||
|
||||
resources[1]["name"] != "项目最终报告 2026.pdf" {
|
||||
t.Fatalf("file resource = %#v", resources[1])
|
||||
}
|
||||
}
|
||||
|
||||
func TestCrossPlatformCoverageResourceNameRejectsIncompletePairs(t *testing.T) {
|
||||
names := map[string]resourceNameCandidate{}
|
||||
recordResourceName(names, "", "report.pdf", resourceNamePriorityStructured)
|
||||
recordResourceName(names, "file-1", "", resourceNamePriorityStructured)
|
||||
if len(names) != 0 {
|
||||
t.Fatalf("incomplete resource-name pairs were retained: %#v", names)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCrossPlatformCoverageProjectionRemovesOnlyLegacyResourceDownloadHint(t *testing.T) {
|
||||
legacy := `[文件] 项目最终报告 2026.pdf fileId: drive-file 注意:如需下载使用dws drive download命令下载`
|
||||
row := ProjectMessageV1(map[string]any{"content": legacy}, false)
|
||||
if row["text"] != `[文件] 项目最终报告 2026.pdf fileId: drive-file` {
|
||||
t.Fatalf("projected text = %#v", row["text"])
|
||||
}
|
||||
resources := row["resourceRefs"].([]map[string]any)
|
||||
if len(resources) != 1 ||
|
||||
resources[0]["name"] != "项目最终报告 2026.pdf" ||
|
||||
resources[0]["download"].(map[string]any)["shortcut"] != "+messages-resource-download" {
|
||||
t.Fatalf("projected resources = %#v", resources)
|
||||
}
|
||||
mediaRow := ProjectMessageV1(map[string]any{
|
||||
"openMessageId": "msg-media",
|
||||
"openConversationId": "cid-media",
|
||||
"content": `[图片消息](mediaId=@media) 注意:如需下载使用dws chat message download-media命令下载`,
|
||||
}, false)
|
||||
if mediaRow["text"] != `[图片消息](mediaId=@media)` {
|
||||
t.Fatalf("projected media text = %#v", mediaRow["text"])
|
||||
}
|
||||
|
||||
ordinary := `团队规范:注意:如需下载使用dws drive download命令下载`
|
||||
if got := ProjectMessageV1(map[string]any{"content": ordinary}, false)["text"]; got != ordinary {
|
||||
t.Fatalf("ordinary text was rewritten: got %#v, want %q", got, ordinary)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCrossPlatformCoverageReactionShapeVariants(t *testing.T) {
|
||||
got := Reactions(map[string]any{
|
||||
"reactions": []map[string]any{
|
||||
|
||||
@@ -0,0 +1,192 @@
|
||||
// Copyright 2026 Alibaba Group
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package chatmsg
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// SearchItems locates and flattens the message list returned by the two
|
||||
// DingTalk message-search interfaces. Grouped search responses carry the
|
||||
// conversation identity on the group rather than each message, so the
|
||||
// flattener copies that identity onto every returned message before callers
|
||||
// perform scope checks.
|
||||
func SearchItems(data map[string]any) []map[string]any {
|
||||
if data == nil {
|
||||
return nil
|
||||
}
|
||||
for _, root := range []map[string]any{data, childMap(data, "result")} {
|
||||
if root == nil {
|
||||
continue
|
||||
}
|
||||
if groups, ok := root["conversationMessagesList"].([]any); ok {
|
||||
return flattenSearchGroups(groups)
|
||||
}
|
||||
}
|
||||
keys := []string{"list", "messages", "messageList", "items", "data", "records", "result"}
|
||||
for _, key := range keys {
|
||||
if arr, ok := data[key].([]any); ok {
|
||||
return searchMaps(arr)
|
||||
}
|
||||
if inner, ok := data[key].(map[string]any); ok {
|
||||
for _, innerKey := range []string{"list", "messages", "messageList", "items", "data", "records"} {
|
||||
if arr, ok := inner[innerKey].([]any); ok {
|
||||
return searchMaps(arr)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func childMap(data map[string]any, key string) map[string]any {
|
||||
if value, ok := data[key].(map[string]any); ok {
|
||||
return value
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func flattenSearchGroups(groups []any) []map[string]any {
|
||||
out := make([]map[string]any, 0)
|
||||
for _, rawGroup := range groups {
|
||||
group, ok := rawGroup.(map[string]any)
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
messages, ok := group["messages"].([]any)
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
conversationID := cleanSearchScalar(group["openConversationId"])
|
||||
conversationTitle := cleanSearchScalar(group["title"])
|
||||
singleChat, hasSingleChat := group["singleChat"]
|
||||
for _, rawMessage := range messages {
|
||||
message, ok := rawMessage.(map[string]any)
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
item := make(map[string]any, len(message)+3)
|
||||
for key, value := range message {
|
||||
item[key] = value
|
||||
}
|
||||
if _, exists := item["openConversationId"]; !exists && conversationID != "" {
|
||||
item["openConversationId"] = conversationID
|
||||
}
|
||||
if _, exists := item["conversationTitle"]; !exists && conversationTitle != "" {
|
||||
item["conversationTitle"] = conversationTitle
|
||||
}
|
||||
if _, exists := item["singleChat"]; !exists && hasSingleChat {
|
||||
item["singleChat"] = singleChat
|
||||
}
|
||||
out = append(out, item)
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func searchMaps(items []any) []map[string]any {
|
||||
out := make([]map[string]any, 0, len(items))
|
||||
for _, item := range items {
|
||||
if message, ok := item.(map[string]any); ok {
|
||||
out = append(out, message)
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// FilterConversationScope keeps only messages belonging to the explicitly
|
||||
// requested conversations. A message without a conversation identity is
|
||||
// reported as unverifiable rather than treated as in-scope.
|
||||
func FilterConversationScope(messages []map[string]any, conversationIDs []string) (matched []map[string]any, unverifiableMessageIDs []string) {
|
||||
requested := make(map[string]struct{}, len(conversationIDs))
|
||||
for _, conversationID := range conversationIDs {
|
||||
if value := strings.TrimSpace(conversationID); value != "" {
|
||||
requested[value] = struct{}{}
|
||||
}
|
||||
}
|
||||
matched = make([]map[string]any, 0, len(messages))
|
||||
for _, message := range messages {
|
||||
conversationID := cleanSearchScalar(ConversationID(message))
|
||||
if conversationID == "" {
|
||||
messageID := cleanSearchScalar(MessageID(message))
|
||||
if messageID == "" {
|
||||
messageID = "<unknown>"
|
||||
}
|
||||
unverifiableMessageIDs = append(unverifiableMessageIDs, messageID)
|
||||
continue
|
||||
}
|
||||
if _, ok := requested[conversationID]; ok {
|
||||
matched = append(matched, message)
|
||||
}
|
||||
}
|
||||
return matched, uniqueStrings(unverifiableMessageIDs)
|
||||
}
|
||||
|
||||
// GroupSearchMessages restores the established typed search envelope after a
|
||||
// client-side scoped scan. Group order follows first occurrence in the search
|
||||
// result, and each message retains its original fields.
|
||||
func GroupSearchMessages(messages []map[string]any) []map[string]any {
|
||||
groups := make([]map[string]any, 0)
|
||||
index := make(map[string]int)
|
||||
for _, message := range messages {
|
||||
conversationID := cleanSearchScalar(ConversationID(message))
|
||||
if conversationID == "" {
|
||||
continue
|
||||
}
|
||||
groupIndex, ok := index[conversationID]
|
||||
if !ok {
|
||||
group := map[string]any{
|
||||
"openConversationId": conversationID,
|
||||
"messages": []map[string]any{},
|
||||
}
|
||||
if title := cleanSearchScalar(message["conversationTitle"]); title != "" {
|
||||
group["title"] = title
|
||||
}
|
||||
if singleChat, exists := message["singleChat"]; exists {
|
||||
group["singleChat"] = singleChat
|
||||
}
|
||||
groups = append(groups, group)
|
||||
groupIndex = len(groups) - 1
|
||||
index[conversationID] = groupIndex
|
||||
}
|
||||
groupMessages := groups[groupIndex]["messages"].([]map[string]any)
|
||||
groups[groupIndex]["messages"] = append(groupMessages, message)
|
||||
}
|
||||
return groups
|
||||
}
|
||||
|
||||
func cleanSearchScalar(value any) string {
|
||||
if value == nil {
|
||||
return ""
|
||||
}
|
||||
text := strings.TrimSpace(fmt.Sprint(value))
|
||||
if text == "" || text == "<nil>" || strings.EqualFold(text, "null") {
|
||||
return ""
|
||||
}
|
||||
return text
|
||||
}
|
||||
|
||||
func uniqueStrings(values []string) []string {
|
||||
out := make([]string, 0, len(values))
|
||||
seen := make(map[string]struct{}, len(values))
|
||||
for _, value := range values {
|
||||
if _, ok := seen[value]; ok {
|
||||
continue
|
||||
}
|
||||
seen[value] = struct{}{}
|
||||
out = append(out, value)
|
||||
}
|
||||
return out
|
||||
}
|
||||
@@ -0,0 +1,104 @@
|
||||
// Copyright 2026 Alibaba Group
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package chatmsg
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestSearchItemsCarriesGroupedConversationIdentity(t *testing.T) {
|
||||
data := map[string]any{
|
||||
"result": map[string]any{
|
||||
"conversationMessagesList": []any{
|
||||
map[string]any{
|
||||
"openConversationId": "cid-group",
|
||||
"title": "项目群",
|
||||
"singleChat": false,
|
||||
"messages": []any{
|
||||
map[string]any{"openMessageId": "m1", "content": "hello"},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
items := SearchItems(data)
|
||||
if len(items) != 1 || items[0]["openConversationId"] != "cid-group" ||
|
||||
items[0]["conversationTitle"] != "项目群" || items[0]["singleChat"] != false {
|
||||
t.Fatalf("items = %#v", items)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFilterConversationScopeDropsOtherConversationsAndRejectsMissingIdentity(t *testing.T) {
|
||||
messages := []map[string]any{
|
||||
{"openMessageId": "m1", "openConversationId": "cid-target"},
|
||||
{"openMessageId": "m2", "openConversationId": "cid-other"},
|
||||
{"openMessageId": "m3"},
|
||||
}
|
||||
|
||||
matched, missing := FilterConversationScope(messages, []string{"cid-target"})
|
||||
if len(matched) != 1 || matched[0]["openMessageId"] != "m1" {
|
||||
t.Fatalf("matched = %#v", matched)
|
||||
}
|
||||
if !reflect.DeepEqual(missing, []string{"m3"}) {
|
||||
t.Fatalf("missing = %#v", missing)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGroupSearchMessagesPreservesFirstSeenOrder(t *testing.T) {
|
||||
messages := []map[string]any{
|
||||
{"openMessageId": "m1", "openConversationId": "cid-b", "conversationTitle": "B"},
|
||||
{"openMessageId": "m2", "openConversationId": "cid-a", "conversationTitle": "A"},
|
||||
{"openMessageId": "m3", "openConversationId": "cid-b", "conversationTitle": "B"},
|
||||
}
|
||||
|
||||
groups := GroupSearchMessages(messages)
|
||||
if len(groups) != 2 || groups[0]["openConversationId"] != "cid-b" || groups[1]["openConversationId"] != "cid-a" {
|
||||
t.Fatalf("groups = %#v", groups)
|
||||
}
|
||||
groupMessages, _ := groups[0]["messages"].([]map[string]any)
|
||||
if len(groupMessages) != 2 || groupMessages[1]["openMessageId"] != "m3" {
|
||||
t.Fatalf("group messages = %#v", groupMessages)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCrossPlatformCoverageSearchProjectionEdgeBranches(t *testing.T) {
|
||||
if SearchItems(nil) != nil {
|
||||
t.Fatal("nil search response returned messages")
|
||||
}
|
||||
|
||||
matched, missing := FilterConversationScope(
|
||||
[]map[string]any{{}},
|
||||
[]string{"", "cid-target"},
|
||||
)
|
||||
if len(matched) != 0 || !reflect.DeepEqual(missing, []string{"<unknown>"}) {
|
||||
t.Fatalf("scope result = matched:%#v missing:%#v", matched, missing)
|
||||
}
|
||||
|
||||
groups := GroupSearchMessages([]map[string]any{
|
||||
{"openMessageId": "missing-scope"},
|
||||
{"openMessageId": "m1", "openConversationId": "cid-1", "singleChat": true},
|
||||
})
|
||||
if len(groups) != 1 || groups[0]["singleChat"] != true {
|
||||
t.Fatalf("groups = %#v", groups)
|
||||
}
|
||||
|
||||
if cleanSearchScalar(nil) != "" || cleanSearchScalar(" null ") != "" || cleanSearchScalar(" value ") != "value" {
|
||||
t.Fatal("cleanSearchScalar did not normalize sentinel values")
|
||||
}
|
||||
if got := uniqueStrings([]string{"m1", "m1", "m2"}); !reflect.DeepEqual(got, []string{"m1", "m2"}) {
|
||||
t.Fatalf("uniqueStrings = %#v", got)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,162 @@
|
||||
// Copyright 2026 Alibaba Group
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
|
||||
package chatmsg
|
||||
|
||||
import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
// MessageSendStatusContractVersion identifies the additive workflow fields
|
||||
// projected by the high-level send-status shortcut. The lower response fields
|
||||
// remain at their original locations for compatibility.
|
||||
const MessageSendStatusContractVersion = "im.message-send-status.v1"
|
||||
|
||||
// MessageSendReceiptContractVersion identifies the additive receipt attached
|
||||
// to high-level current-user send results.
|
||||
const MessageSendReceiptContractVersion = "im.message-send-receipt.v1"
|
||||
|
||||
// ProjectMessageSendReceipt connects a send result to its asynchronous status
|
||||
// query without treating openTaskId as a message identifier.
|
||||
func ProjectMessageSendReceipt(raw map[string]any) map[string]any {
|
||||
taskID := firstSendStatusString(raw, "openTaskId", "taskId")
|
||||
messageID := firstSendStatusString(raw, "openMessageId", "messageId", "msgId")
|
||||
conversationID := firstSendStatusString(raw, "openConversationId", "conversationId", "openCid")
|
||||
ready := messageID != "" && conversationID != ""
|
||||
receipt := map[string]any{
|
||||
"contractVersion": MessageSendReceiptContractVersion,
|
||||
"openTaskId": taskID,
|
||||
"readyForMessageActions": ready,
|
||||
"nextActions": []map[string]any{},
|
||||
}
|
||||
if messageID != "" || conversationID != "" {
|
||||
messageRef := map[string]any{}
|
||||
if messageID != "" {
|
||||
messageRef["openMessageId"] = messageID
|
||||
}
|
||||
if conversationID != "" {
|
||||
messageRef["openConversationId"] = conversationID
|
||||
}
|
||||
receipt["messageRef"] = messageRef
|
||||
}
|
||||
switch {
|
||||
case ready:
|
||||
receipt["nextActions"] = sendStatusNextActions(taskID, messageID, conversationID, true)
|
||||
case taskID != "":
|
||||
receipt["nextActions"] = []map[string]any{{
|
||||
"cliPath": "chat +messages-query-send-status",
|
||||
"arguments": map[string]any{
|
||||
"open-task-id": taskID,
|
||||
},
|
||||
"ready": true,
|
||||
"when": "需要确认投递结果或取得真实消息 ID 时",
|
||||
}}
|
||||
default:
|
||||
receipt["capabilityGap"] = "下层发送响应未返回 openTaskId 或完整 messageRef,CLI 无法生成后续状态查询"
|
||||
}
|
||||
return receipt
|
||||
}
|
||||
|
||||
// ProjectMessageSendStatus preserves the lower response and adds a stable
|
||||
// receipt that connects openTaskId to the message identifiers required by
|
||||
// edit, recall, and read-status. It never manufactures a message reference:
|
||||
// downstream actions are marked ready only when both IDs are actually present.
|
||||
func ProjectMessageSendStatus(raw map[string]any, requestedTaskID string) map[string]any {
|
||||
payload := cloneSendStatusMap(raw)
|
||||
taskID := firstSendStatusString(payload, "openTaskId", "taskId")
|
||||
if taskID == "" {
|
||||
taskID = strings.TrimSpace(requestedTaskID)
|
||||
}
|
||||
messageID := firstSendStatusString(payload, "openMessageId", "messageId", "msgId")
|
||||
conversationID := firstSendStatusString(payload, "openConversationId", "conversationId", "openCid")
|
||||
|
||||
payload["contractVersion"] = MessageSendStatusContractVersion
|
||||
payload["openTaskId"] = taskID
|
||||
messageRef := map[string]any{}
|
||||
if messageID != "" {
|
||||
messageRef["openMessageId"] = messageID
|
||||
}
|
||||
if conversationID != "" {
|
||||
messageRef["openConversationId"] = conversationID
|
||||
}
|
||||
if len(messageRef) > 0 {
|
||||
payload["messageRef"] = messageRef
|
||||
}
|
||||
ready := messageID != "" && conversationID != ""
|
||||
payload["readyForMessageActions"] = ready
|
||||
payload["nextActions"] = sendStatusNextActions(taskID, messageID, conversationID, ready)
|
||||
return payload
|
||||
}
|
||||
|
||||
func sendStatusNextActions(taskID, messageID, conversationID string, ready bool) []map[string]any {
|
||||
if !ready {
|
||||
return []map[string]any{{
|
||||
"cliPath": "chat message query-send-status",
|
||||
"arguments": map[string]any{
|
||||
"open-task-id": taskID,
|
||||
},
|
||||
"ready": false,
|
||||
"when": "投递任务尚未返回 openMessageId 和 openConversationId 时稍后重查",
|
||||
}}
|
||||
}
|
||||
messageArgs := map[string]any{
|
||||
"conversation-id": conversationID,
|
||||
"msg-id": messageID,
|
||||
}
|
||||
return []map[string]any{
|
||||
{
|
||||
"cliPath": "chat message recall",
|
||||
"arguments": cloneSendStatusMap(messageArgs),
|
||||
"ready": true,
|
||||
},
|
||||
{
|
||||
"cliPath": "chat message edit",
|
||||
"arguments": map[string]any{
|
||||
"conversation-id": conversationID,
|
||||
"msg-id": messageID,
|
||||
},
|
||||
"requiredArguments": []string{"text 或 content"},
|
||||
"ready": false,
|
||||
},
|
||||
{
|
||||
"cliPath": "chat message read-status",
|
||||
"arguments": map[string]any{
|
||||
"conversation-id": conversationID,
|
||||
"message-id": messageID,
|
||||
},
|
||||
"ready": true,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func firstSendStatusString(value any, keys ...string) string {
|
||||
switch typed := value.(type) {
|
||||
case map[string]any:
|
||||
for _, key := range keys {
|
||||
if candidate, ok := typed[key].(string); ok && strings.TrimSpace(candidate) != "" {
|
||||
return strings.TrimSpace(candidate)
|
||||
}
|
||||
}
|
||||
for _, key := range []string{"result", "data", "response", "content", "message"} {
|
||||
if candidate := firstSendStatusString(typed[key], keys...); candidate != "" {
|
||||
return candidate
|
||||
}
|
||||
}
|
||||
case []any:
|
||||
for _, item := range typed {
|
||||
if candidate := firstSendStatusString(item, keys...); candidate != "" {
|
||||
return candidate
|
||||
}
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func cloneSendStatusMap(source map[string]any) map[string]any {
|
||||
clone := make(map[string]any, len(source)+5)
|
||||
for key, value := range source {
|
||||
clone[key] = value
|
||||
}
|
||||
return clone
|
||||
}
|
||||
@@ -0,0 +1,94 @@
|
||||
// Copyright 2026 Alibaba Group
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
|
||||
package chatmsg
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestCrossPlatformCoverageProjectMessageSendReceiptLinksStatusQuery(t *testing.T) {
|
||||
receipt := ProjectMessageSendReceipt(map[string]any{
|
||||
"result": map[string]any{"openTaskId": "task-1"},
|
||||
})
|
||||
if receipt["contractVersion"] != MessageSendReceiptContractVersion || receipt["openTaskId"] != "task-1" || receipt["readyForMessageActions"] != false {
|
||||
t.Fatalf("receipt = %#v", receipt)
|
||||
}
|
||||
actions, _ := receipt["nextActions"].([]map[string]any)
|
||||
if len(actions) != 1 || actions[0]["cliPath"] != "chat +messages-query-send-status" || actions[0]["ready"] != true {
|
||||
t.Fatalf("nextActions = %#v", actions)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCrossPlatformCoverageProjectMessageSendReceiptReadyWorkflow(t *testing.T) {
|
||||
receipt := ProjectMessageSendReceipt(map[string]any{
|
||||
"openTaskId": "task-ready",
|
||||
"openMessageId": "msg-ready",
|
||||
"openConversationId": "cid-ready",
|
||||
})
|
||||
if receipt["readyForMessageActions"] != true {
|
||||
t.Fatalf("receipt = %#v", receipt)
|
||||
}
|
||||
ref, _ := receipt["messageRef"].(map[string]any)
|
||||
if ref["openMessageId"] != "msg-ready" || ref["openConversationId"] != "cid-ready" {
|
||||
t.Fatalf("messageRef = %#v", ref)
|
||||
}
|
||||
actions, _ := receipt["nextActions"].([]map[string]any)
|
||||
if len(actions) != 3 || actions[0]["ready"] != true {
|
||||
t.Fatalf("nextActions = %#v", actions)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCrossPlatformCoverageProjectMessageSendStatusReadyWorkflow(t *testing.T) {
|
||||
raw := map[string]any{
|
||||
"result": map[string]any{
|
||||
"openTaskId": "task-1",
|
||||
"openMessageId": "msg-1",
|
||||
"openConversationId": "cid-1",
|
||||
"status": "SUCCESS",
|
||||
},
|
||||
}
|
||||
payload := ProjectMessageSendStatus(raw, "ignored")
|
||||
if payload["contractVersion"] != MessageSendStatusContractVersion ||
|
||||
payload["openTaskId"] != "task-1" || payload["readyForMessageActions"] != true {
|
||||
t.Fatalf("payload = %#v", payload)
|
||||
}
|
||||
ref, _ := payload["messageRef"].(map[string]any)
|
||||
if ref["openMessageId"] != "msg-1" || ref["openConversationId"] != "cid-1" {
|
||||
t.Fatalf("messageRef = %#v", ref)
|
||||
}
|
||||
actions, _ := payload["nextActions"].([]map[string]any)
|
||||
if len(actions) != 3 || actions[0]["cliPath"] != "chat message recall" || actions[2]["cliPath"] != "chat message read-status" {
|
||||
t.Fatalf("nextActions = %#v", actions)
|
||||
}
|
||||
if _, ok := payload["result"]; !ok {
|
||||
t.Fatal("raw response field was not preserved")
|
||||
}
|
||||
}
|
||||
|
||||
func TestCrossPlatformCoverageProjectMessageSendStatusPendingDoesNotInventMessageRef(t *testing.T) {
|
||||
payload := ProjectMessageSendStatus(map[string]any{
|
||||
"result": map[string]any{"status": "PENDING"},
|
||||
}, "task-pending")
|
||||
if payload["openTaskId"] != "task-pending" || payload["readyForMessageActions"] != false {
|
||||
t.Fatalf("payload = %#v", payload)
|
||||
}
|
||||
if _, exists := payload["messageRef"]; exists {
|
||||
t.Fatalf("pending payload invented messageRef: %#v", payload)
|
||||
}
|
||||
actions, _ := payload["nextActions"].([]map[string]any)
|
||||
if len(actions) != 1 || actions[0]["ready"] != false {
|
||||
t.Fatalf("nextActions = %#v", actions)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCrossPlatformCoverageFirstSendStatusStringTraversesArrays(t *testing.T) {
|
||||
value := []any{
|
||||
nil,
|
||||
map[string]any{"result": []any{
|
||||
map[string]any{"openTaskId": " task-from-array "},
|
||||
}},
|
||||
}
|
||||
if got := firstSendStatusString(value, "openTaskId"); got != "task-from-array" {
|
||||
t.Fatalf("firstSendStatusString() = %q", got)
|
||||
}
|
||||
}
|
||||
@@ -72,8 +72,9 @@ func (f *platformCoverageCaller) CallTool(_ context.Context, product, tool strin
|
||||
text = f.chatMessagesResult
|
||||
}
|
||||
case "im/search_messages":
|
||||
if f.searchMessagesResult != "" {
|
||||
text = f.searchMessagesResult
|
||||
text = f.searchMessagesResult
|
||||
if text == "" {
|
||||
text = `{"result":{"messages":[],"hasMore":false}}`
|
||||
}
|
||||
}
|
||||
return &edition.ToolResult{Content: []edition.ContentBlock{{Type: "text", Text: text}}}, nil
|
||||
@@ -405,7 +406,8 @@ func TestCrossPlatformCoverageCompatibilityAliases(t *testing.T) {
|
||||
argv: []string{"chat", "+search-msg", "--id", "cid-1", "--keyword", "树莓派", "--no-enrich", "--yes"},
|
||||
wantProduct: "im",
|
||||
wantTool: "search_messages",
|
||||
wantArgs: map[string]any{"openConversationIds": []string{"cid-1"}, "keyword": "树莓派"},
|
||||
wantArgs: map[string]any{"keyword": "树莓派"},
|
||||
wantAbsent: []string{"openConversationIds"},
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
@@ -323,15 +323,18 @@ func TestCrossPlatformCoverageSearchMsgResolvesNaturalChatAndSenderBeforeSearch(
|
||||
if err := root.Execute(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(fake.calls) != 3 {
|
||||
t.Fatalf("calls = %#v, want chat resolve + user resolve + search", fake.calls)
|
||||
if len(fake.calls) != 4 {
|
||||
t.Fatalf("calls = %#v, want chat resolve + user resolve + scope validation + search", fake.calls)
|
||||
}
|
||||
search := fake.calls[2]
|
||||
if preflight := fake.calls[2]; preflight.product != "chat" || preflight.tool != "get_conversation_info" || preflight.args["openConversationId"] != "cid-1" {
|
||||
t.Fatalf("scope preflight = %#v", preflight)
|
||||
}
|
||||
search := fake.calls[3]
|
||||
if search.product != "im" || search.tool != "search_messages" {
|
||||
t.Fatalf("search = %#v", search)
|
||||
}
|
||||
if got, want := search.args["openConversationIds"], []string{"cid-1"}; !reflect.DeepEqual(got, want) {
|
||||
t.Fatalf("openConversationIds = %#v, want %#v", got, want)
|
||||
if _, exists := search.args["openConversationIds"]; exists {
|
||||
t.Fatalf("global fallback unexpectedly forwarded openConversationIds: %#v", search.args)
|
||||
}
|
||||
if got, want := search.args["senderOpenDingTakIds"], []string{"D1"}; !reflect.DeepEqual(got, want) {
|
||||
t.Fatalf("senderOpenDingTakIds = %#v, want %#v", got, want)
|
||||
@@ -392,14 +395,17 @@ func TestCrossPlatformCoverageSearchMsgAcceptsStableIDInChatQuery(t *testing.T)
|
||||
if err := root.Execute(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(fake.calls) != 1 || fake.calls[0].tool != "search_messages" {
|
||||
if len(fake.calls) != 2 || fake.calls[0].tool != "get_conversation_info" || fake.calls[1].tool != "search_messages" {
|
||||
t.Fatalf("calls = %#v", fake.calls)
|
||||
}
|
||||
if got, want := fake.calls[0].args["openConversationIds"], []string{"cid-fixture-chat-0002"}; !reflect.DeepEqual(got, want) {
|
||||
t.Fatalf("openConversationIds = %#v, want %#v", got, want)
|
||||
if fake.calls[0].args["openConversationId"] != "cid-fixture-chat-0002" {
|
||||
t.Fatalf("scope preflight = %#v", fake.calls[0])
|
||||
}
|
||||
if fake.calls[0].args["keyword"] != "评测" {
|
||||
t.Fatalf("keyword = %#v", fake.calls[0].args["keyword"])
|
||||
if _, exists := fake.calls[1].args["openConversationIds"]; exists {
|
||||
t.Fatalf("global fallback unexpectedly forwarded openConversationIds: %#v", fake.calls[1].args)
|
||||
}
|
||||
if fake.calls[1].args["keyword"] != "评测" {
|
||||
t.Fatalf("keyword = %#v", fake.calls[1].args["keyword"])
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -23,6 +23,7 @@ import (
|
||||
|
||||
"github.com/DingTalk-Real-AI/dingtalk-workspace-cli/internal/corecmd/contract"
|
||||
apperrors "github.com/DingTalk-Real-AI/dingtalk-workspace-cli/internal/errors"
|
||||
"github.com/DingTalk-Real-AI/dingtalk-workspace-cli/internal/helpers"
|
||||
"github.com/DingTalk-Real-AI/dingtalk-workspace-cli/internal/shortcut"
|
||||
chatshortcut "github.com/DingTalk-Real-AI/dingtalk-workspace-cli/internal/shortcut/chat"
|
||||
"github.com/DingTalk-Real-AI/dingtalk-workspace-cli/internal/shortcut/chatmsg"
|
||||
@@ -34,15 +35,18 @@ import (
|
||||
// search hits through list_messages_by_ids in chunks of 50. A later-page or
|
||||
// enrichment failure never turns a partial result into a false success: the
|
||||
// output carries an explicit failure ledger and complete=false.
|
||||
const searchMsgIntent = "当你要按关键词、发送者、@对象、消息类型、机器人来源或会话范围组合搜索 IM 消息时使用;可搜索单个、多个或全部会话,会话与发送者过滤使用稳定 ID。默认查询近 7 天,也可指定精确起止时间和输出顺序。" +
|
||||
"显式指定会话时会先验证 CID,再执行有界全局扫描并在本地精确过滤,避免下层忽略非法 CID 或群聊 CID。" +
|
||||
"--page-all 会连续拉取游标页,默认再按消息 ID 分批富化详情;任何续页或富化失败都会保留已取得结果并返回逐项失败 ledger,绝不把截断结果标成完整。" +
|
||||
"--download-resources 使用安全本地路径、默认不覆盖和原子落盘。"
|
||||
|
||||
var SearchMsg = shortcut.Shortcut{
|
||||
Service: "chat",
|
||||
Command: "+search-msg",
|
||||
Product: "im",
|
||||
Description: "按发送者、关键词、@对象、会话或消息类型等条件搜索消息,可限定单个、多个或全部会话",
|
||||
Intent: "当你要按发送者、关键词、@对象、消息类型、机器人来源或会话范围等条件搜索 IM 消息时使用;搜索范围可为单个、多个或全部会话,会话与发送者的公开过滤参数使用稳定 ID。默认查询近 7 天,也可指定精确起止时间及输出顺序。" +
|
||||
"--page-all 会连续拉取游标页,默认再按消息 ID 分批富化详情;任何续页或富化失败都会保留已取得结果并返回逐项失败 ledger,绝不把截断结果标成完整。" +
|
||||
"--download-resources 使用安全本地路径、默认不覆盖和原子落盘。",
|
||||
Risk: shortcut.RiskRead,
|
||||
Description: "按稳定 ID、内容、时间等条件搜索消息,可校验会话范围、全量翻页并批量富化",
|
||||
Intent: searchMsgIntent,
|
||||
Risk: shortcut.RiskRead,
|
||||
Safety: contract.SafetySpec{
|
||||
Effect: "read", Risk: "low",
|
||||
Confirmation: "not_required", Idempotency: "idempotent",
|
||||
@@ -55,15 +59,15 @@ var SearchMsg = shortcut.Shortcut{
|
||||
CLIPath: "chat +search-msg",
|
||||
PrimaryCLIPath: "chat +search-msg",
|
||||
},
|
||||
Description: "按发送者、关键词、@对象、会话或消息类型等条件搜索消息,可限定单个、多个或全部会话",
|
||||
Description: "按稳定 ID、内容、时间等条件搜索消息,可校验会话范围、全量翻页并批量富化",
|
||||
Interface: &contract.InterfaceSpec{
|
||||
Mode: "composite",
|
||||
Availability: "available",
|
||||
Reason: "Reviewed search adapter: it combines filters, cursor pagination, batched mget enrichment, stable projection, completeness accounting, and optional safe resource downloads.",
|
||||
},
|
||||
Selection: contract.SelectionSpec{
|
||||
AgentSummary: "按发送者、关键词、@对象、会话或消息类型等条件搜索消息,可限定单个、多个或全部会话",
|
||||
UseWhen: []string{"当你要按发送者、关键词、@对象、消息类型、机器人来源或会话范围等条件搜索 IM 消息时使用;搜索范围可为单个、多个或全部会话,会话与发送者的公开过滤参数使用稳定 ID。默认查询近 7 天,也可指定精确起止时间及输出顺序。--page-all 会连续拉取游标页,默认再按消息 ID 分批富化详情;任何续页或富化失败都会保留已取得结果并返回逐项失败 ledger,绝不把截断结果标成完整。--download-resources 使用安全本地路径、默认不覆盖和原子落盘。"},
|
||||
AgentSummary: "按稳定 ID、内容、时间等条件搜索消息,可校验会话范围、全量翻页并批量富化",
|
||||
UseWhen: []string{searchMsgIntent},
|
||||
AvoidWhen: []string{"只想查看或导出一个指定会话的消息记录、且没有发送者、关键词、@对象或消息类型等主要筛选条件时使用 +chat-messages;已有精确消息 ID 时使用 +messages-mget"},
|
||||
Examples: []string{
|
||||
"dws chat +search-msg --query \"周报\" --senders <openDingTalkId> --days 3 --page-all",
|
||||
@@ -104,7 +108,7 @@ var SearchMsg = shortcut.Shortcut{
|
||||
{Name: "cursor", Type: shortcut.FlagString, Desc: "分页游标,翻页传上次的 nextCursor", Default: "0"},
|
||||
{Name: "page-token", Type: shortcut.FlagString, Desc: "--cursor 的 lark-cli 对齐别名"},
|
||||
{Name: "page-all", Type: shortcut.FlagBool, Desc: "自动连续拉取所有游标页"},
|
||||
{Name: "page-limit", Type: shortcut.FlagInt, Desc: "--page-all 的最大页数(1-40)", Default: "20"},
|
||||
{Name: "page-limit", Type: shortcut.FlagInt, Desc: "--page-all 或显式会话范围本地扫描的最大页数(1-40)", Default: "20"},
|
||||
{Name: "no-enrich", Type: shortcut.FlagBool, Desc: "不再按消息 ID 批量查询完整详情"},
|
||||
{Name: "no-reactions", Type: shortcut.FlagBool, Desc: "不输出命中消息的 reaction(默认输出)"},
|
||||
}, chatshortcut.MessageResourceDownloadFlags()...),
|
||||
@@ -145,9 +149,21 @@ var SearchMsg = shortcut.Shortcut{
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
requestedConversationIDs, _ := params["openConversationIds"].([]string)
|
||||
scopedSearch := len(requestedConversationIDs) > 0
|
||||
if scopedSearch {
|
||||
if err := validateSearchConversationScope(rt, requestedConversationIDs); err != nil {
|
||||
return err
|
||||
}
|
||||
// The downstream search currently drops invalid CID filters and does
|
||||
// not return group-scoped hits reliably. Scan the same filtered global
|
||||
// stream and apply the already-validated CID set locally instead.
|
||||
delete(params, "openConversationIds")
|
||||
}
|
||||
|
||||
pageLimit := 1
|
||||
if rt.Bool("page-all") {
|
||||
scanAllPages := rt.Bool("page-all") || scopedSearch
|
||||
if scanAllPages {
|
||||
pageLimit = rt.Int("page-limit")
|
||||
}
|
||||
cursor := rt.StrFirst("page-token", "cursor")
|
||||
@@ -176,7 +192,15 @@ var SearchMsg = shortcut.Shortcut{
|
||||
break
|
||||
}
|
||||
pagesFetched++
|
||||
for _, message := range searchMsgItems(data) {
|
||||
pageMessages := searchMsgItems(data)
|
||||
if scopedSearch {
|
||||
var unverifiableMessageIDs []string
|
||||
pageMessages, unverifiableMessageIDs = chatmsg.FilterConversationScope(pageMessages, requestedConversationIDs)
|
||||
if len(unverifiableMessageIDs) > 0 {
|
||||
return searchScopeUnverifiedError(requestedConversationIDs, unverifiableMessageIDs)
|
||||
}
|
||||
}
|
||||
for _, message := range pageMessages {
|
||||
messageID := strings.TrimSpace(fmt.Sprint(searchMsgMessageID(message)))
|
||||
if messageID != "" && messageID != "<nil>" {
|
||||
if seen[messageID] {
|
||||
@@ -204,7 +228,7 @@ var SearchMsg = shortcut.Shortcut{
|
||||
}
|
||||
}
|
||||
hasMore = hasMoreValue
|
||||
if !rt.Bool("page-all") || !hasMore {
|
||||
if !scanAllPages || !hasMore {
|
||||
complete = !hasMore
|
||||
break
|
||||
}
|
||||
@@ -218,7 +242,7 @@ var SearchMsg = shortcut.Shortcut{
|
||||
}
|
||||
cursor = nextCursor
|
||||
}
|
||||
if rt.Bool("page-all") && hasMore && pagesFetched == pageLimit {
|
||||
if scanAllPages && hasMore && pagesFetched == pageLimit {
|
||||
failures = append(failures, map[string]any{
|
||||
"stage": "search-page-limit",
|
||||
"error": fmt.Sprintf("达到 --page-limit=%d,仍有更多结果", pageLimit),
|
||||
@@ -235,6 +259,16 @@ var SearchMsg = shortcut.Shortcut{
|
||||
complete = false
|
||||
}
|
||||
}
|
||||
if scopedSearch {
|
||||
validatedMessages, unverifiableMessageIDs := chatmsg.FilterConversationScope(messages, requestedConversationIDs)
|
||||
if len(unverifiableMessageIDs) > 0 {
|
||||
return searchScopeUnverifiedError(requestedConversationIDs, unverifiableMessageIDs)
|
||||
}
|
||||
if len(validatedMessages) != len(messages) {
|
||||
return searchScopeViolationError(requestedConversationIDs, messages)
|
||||
}
|
||||
messages = validatedMessages
|
||||
}
|
||||
|
||||
order := strings.ToLower(strings.TrimSpace(rt.StrFirst("order", "sort")))
|
||||
if order == "" {
|
||||
@@ -262,6 +296,9 @@ var SearchMsg = shortcut.Shortcut{
|
||||
if len(resolvedFilters.Senders) > 0 {
|
||||
payload["resolvedFilters"] = resolvedFilters
|
||||
}
|
||||
if scopedSearch {
|
||||
payload["scope"] = searchScopePayload(requestedConversationIDs, paginationKnown && !hasMore)
|
||||
}
|
||||
if hasMore && nextCursor != "" && nextCursor != "<nil>" {
|
||||
payload["nextCursor"] = nextCursor
|
||||
}
|
||||
@@ -432,6 +469,63 @@ func uniqueSearchStrings(values []string) []string {
|
||||
return out
|
||||
}
|
||||
|
||||
func validateSearchConversationScope(rt *shortcut.RuntimeContext, conversationIDs []string) error {
|
||||
for _, conversationID := range conversationIDs {
|
||||
_, err := rt.CallMCPData("chat", "get_conversation_info", map[string]any{
|
||||
"openConversationId": conversationID,
|
||||
})
|
||||
if err == nil {
|
||||
continue
|
||||
}
|
||||
return helpers.NormalizeSearchConversationScopeError(conversationID, err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func searchScopeUnverifiedError(conversationIDs, messageIDs []string) error {
|
||||
return apperrors.NewAPI(
|
||||
"搜索结果缺少 conversationId,无法证明会话过滤范围;已停止输出",
|
||||
apperrors.WithReason("search_conversation_scope_unverified"),
|
||||
apperrors.WithDetails(map[string]any{
|
||||
"requestedConversationIds": conversationIDs,
|
||||
"unverifiableMessageIds": messageIDs,
|
||||
}),
|
||||
apperrors.WithRetryable(false),
|
||||
apperrors.WithHint("请保留 trace_id 并检查 IM 搜索服务是否返回 openConversationId"),
|
||||
)
|
||||
}
|
||||
|
||||
func searchScopeViolationError(conversationIDs []string, messages []map[string]any) error {
|
||||
observed := make([]string, 0, len(messages))
|
||||
for _, message := range messages {
|
||||
conversationID := strings.TrimSpace(fmt.Sprint(chatmsg.ConversationID(message)))
|
||||
if conversationID == "" || conversationID == "<nil>" {
|
||||
continue
|
||||
}
|
||||
observed = append(observed, conversationID)
|
||||
}
|
||||
return apperrors.NewAPI(
|
||||
"消息富化结果超出请求的会话范围;已停止输出",
|
||||
apperrors.WithReason("search_conversation_scope_violation"),
|
||||
apperrors.WithDetails(map[string]any{
|
||||
"requestedConversationIds": conversationIDs,
|
||||
"observedConversationIds": uniqueSearchStrings(observed),
|
||||
}),
|
||||
apperrors.WithRetryable(false),
|
||||
)
|
||||
}
|
||||
|
||||
func searchScopePayload(conversationIDs []string, sourceComplete bool) map[string]any {
|
||||
return map[string]any{
|
||||
"requestedConversationIds": append([]string(nil), conversationIDs...),
|
||||
"targetsValidated": true,
|
||||
"filterApplied": true,
|
||||
"filterMode": "client",
|
||||
"resultsWithinScope": true,
|
||||
"sourceComplete": sourceComplete,
|
||||
}
|
||||
}
|
||||
|
||||
func enrichSearchMessages(rt *shortcut.RuntimeContext, messages []map[string]any) ([]map[string]any, int, []map[string]any) {
|
||||
detailsByID := map[string]map[string]any{}
|
||||
failures := make([]map[string]any, 0)
|
||||
@@ -505,31 +599,7 @@ func enrichSearchMessages(rt *shortcut.RuntimeContext, messages []map[string]any
|
||||
// response, probing common container keys at the top level and nested under
|
||||
// "result". Returns nil when no list is found.
|
||||
func searchMsgItems(data map[string]any) []map[string]any {
|
||||
if data == nil {
|
||||
return nil
|
||||
}
|
||||
for _, root := range []map[string]any{data, searchMsgChildMap(data, "result")} {
|
||||
if root == nil {
|
||||
continue
|
||||
}
|
||||
if groups, ok := root["conversationMessagesList"].([]any); ok {
|
||||
return searchMsgFlattenGroups(groups)
|
||||
}
|
||||
}
|
||||
keys := []string{"list", "messages", "messageList", "items", "data", "records", "result"}
|
||||
for _, key := range keys {
|
||||
if arr, ok := data[key].([]any); ok {
|
||||
return searchMsgToMaps(arr)
|
||||
}
|
||||
if inner, ok := data[key].(map[string]any); ok {
|
||||
for _, k2 := range []string{"list", "messages", "messageList", "items", "data", "records"} {
|
||||
if arr, ok := inner[k2].([]any); ok {
|
||||
return searchMsgToMaps(arr)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
return chatmsg.SearchItems(data)
|
||||
}
|
||||
|
||||
func searchMsgChildMap(data map[string]any, key string) map[string]any {
|
||||
@@ -539,56 +609,6 @@ func searchMsgChildMap(data map[string]any, key string) map[string]any {
|
||||
return nil
|
||||
}
|
||||
|
||||
func searchMsgFlattenGroups(groups []any) []map[string]any {
|
||||
out := make([]map[string]any, 0)
|
||||
for _, rawGroup := range groups {
|
||||
group, ok := rawGroup.(map[string]any)
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
messages, ok := group["messages"].([]any)
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
conversationID := strings.TrimSpace(fmt.Sprint(group["openConversationId"]))
|
||||
conversationTitle := strings.TrimSpace(fmt.Sprint(group["title"]))
|
||||
singleChat, hasSingleChat := group["singleChat"]
|
||||
for _, rawMessage := range messages {
|
||||
message, ok := rawMessage.(map[string]any)
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
item := make(map[string]any, len(message)+3)
|
||||
for key, value := range message {
|
||||
item[key] = value
|
||||
}
|
||||
if _, exists := item["openConversationId"]; !exists &&
|
||||
conversationID != "" && conversationID != "<nil>" {
|
||||
item["openConversationId"] = conversationID
|
||||
}
|
||||
if _, exists := item["conversationTitle"]; !exists &&
|
||||
conversationTitle != "" && conversationTitle != "<nil>" {
|
||||
item["conversationTitle"] = conversationTitle
|
||||
}
|
||||
if _, exists := item["singleChat"]; !exists && hasSingleChat {
|
||||
item["singleChat"] = singleChat
|
||||
}
|
||||
out = append(out, item)
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func searchMsgToMaps(arr []any) []map[string]any {
|
||||
out := make([]map[string]any, 0, len(arr))
|
||||
for _, it := range arr {
|
||||
if m, ok := it.(map[string]any); ok {
|
||||
out = append(out, m)
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// searchMsgProject reshapes one matched message into {sender, time, text,
|
||||
// messageId}, running text through the shared chatmsg cleaning (card/auto-reply
|
||||
// JSON → readable, ciphertext → marker) and recursively expanding any forwarded
|
||||
|
||||
@@ -13,6 +13,7 @@ import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
apperrors "github.com/DingTalk-Real-AI/dingtalk-workspace-cli/internal/errors"
|
||||
"github.com/DingTalk-Real-AI/dingtalk-workspace-cli/internal/helpers"
|
||||
"github.com/DingTalk-Real-AI/dingtalk-workspace-cli/pkg/edition"
|
||||
)
|
||||
@@ -23,34 +24,57 @@ type searchMsgExecutionCaller struct {
|
||||
failEnrichment bool
|
||||
omitPagination bool
|
||||
omitMgetItem bool
|
||||
failPreflight bool
|
||||
preflightError error
|
||||
searchResponse string
|
||||
wrongMgetScope bool
|
||||
missingMgetCID bool
|
||||
firstResponse string
|
||||
mgetResponse string
|
||||
}
|
||||
|
||||
func (f *searchMsgExecutionCaller) CallTool(_ context.Context, product, tool string, args map[string]any) (*edition.ToolResult, error) {
|
||||
f.calls = append(f.calls, platformCoverageCall{product: product, tool: tool, args: args})
|
||||
if product == "chat" && tool == "get_conversation_info" {
|
||||
if f.preflightError != nil {
|
||||
return nil, f.preflightError
|
||||
}
|
||||
if f.failPreflight {
|
||||
return nil, errors.New("conversation not found")
|
||||
}
|
||||
return searchMsgToolResult(`{"result":{"openConversationId":"` + args["openConversationId"].(string) + `"}}`), nil
|
||||
}
|
||||
if product != "im" {
|
||||
return nil, errors.New("unexpected product")
|
||||
}
|
||||
switch tool {
|
||||
case "search_messages":
|
||||
if f.searchResponse != "" {
|
||||
return searchMsgToolResult(f.searchResponse), nil
|
||||
}
|
||||
if f.omitPagination {
|
||||
return searchMsgToolResult(`{"result":{"messages":[{"openMessageId":"m1","content":"sparse-1"}]}}`), nil
|
||||
return searchMsgToolResult(`{"result":{"messages":[{"openMessageId":"m1","openConversationId":"cid-1","content":"sparse-1"}]}}`), nil
|
||||
}
|
||||
if args["cursor"] == "c2" {
|
||||
if f.failSecondPage {
|
||||
return nil, errors.New("second page unavailable")
|
||||
}
|
||||
return searchMsgToolResult(`{"result":{"messages":[{"openMessageId":"m2","content":"sparse-2"}],"hasMore":false}}`), nil
|
||||
return searchMsgToolResult(`{"result":{"messages":[{"openMessageId":"m2","openConversationId":"cid-2","content":"sparse-2"}],"hasMore":false}}`), nil
|
||||
}
|
||||
if f.firstResponse != "" {
|
||||
return searchMsgToolResult(f.firstResponse), nil
|
||||
}
|
||||
return searchMsgToolResult(`{"result":{"messages":[{"openMessageId":"m1","content":"sparse-1"}],"hasMore":true,"nextCursor":"c2"}}`), nil
|
||||
return searchMsgToolResult(`{"result":{"messages":[{"openMessageId":"m1","openConversationId":"cid-1","content":"sparse-1"}],"hasMore":true,"nextCursor":"c2"}}`), nil
|
||||
case "list_messages_by_ids":
|
||||
if f.failEnrichment {
|
||||
return nil, errors.New("mget unavailable")
|
||||
}
|
||||
if f.wrongMgetScope {
|
||||
return searchMsgToolResult(`{"result":[{"openMessageId":"m1","openConversationId":"cid-other","content":"detail-1"}]}`), nil
|
||||
}
|
||||
if f.missingMgetCID {
|
||||
return searchMsgToolResult(`{"result":[{"openMessageId":"m1","openConversationId":null,"content":"detail-1"}]}`), nil
|
||||
}
|
||||
if f.omitMgetItem {
|
||||
return searchMsgToolResult(`{"result":[{"openMessageId":"m1","content":"detail-1"}]}`), nil
|
||||
}
|
||||
@@ -78,19 +102,27 @@ func searchMsgToolResult(text string) *edition.ToolResult {
|
||||
|
||||
func executeSearchMsg(t *testing.T, caller *searchMsgExecutionCaller, args ...string) map[string]any {
|
||||
t.Helper()
|
||||
payload, err := executeSearchMsgResult(caller, args...)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return payload
|
||||
}
|
||||
|
||||
func executeSearchMsgResult(caller *searchMsgExecutionCaller, args ...string) (map[string]any, error) {
|
||||
helpers.InitDeps(caller)
|
||||
root := newPlatformCoverageRoot()
|
||||
var output bytes.Buffer
|
||||
root.SetOut(&output)
|
||||
root.SetArgs(append([]string{"chat", "+search-msg", "--yes"}, args...))
|
||||
if err := root.Execute(); err != nil {
|
||||
t.Fatal(err)
|
||||
return nil, err
|
||||
}
|
||||
var payload map[string]any
|
||||
if err := json.Unmarshal(output.Bytes(), &payload); err != nil {
|
||||
t.Fatalf("decode output: %v\n%s", err, output.String())
|
||||
return nil, err
|
||||
}
|
||||
return payload
|
||||
return payload, nil
|
||||
}
|
||||
|
||||
func TestCrossPlatformCoverageSearchMsgPagesAndEnrichesWithAdvancedFilters(t *testing.T) {
|
||||
@@ -112,15 +144,18 @@ func TestCrossPlatformCoverageSearchMsgPagesAndEnrichesWithAdvancedFilters(t *te
|
||||
"--page-limit", "3",
|
||||
)
|
||||
|
||||
if len(caller.calls) != 3 {
|
||||
if len(caller.calls) != 5 {
|
||||
t.Fatalf("calls = %#v", caller.calls)
|
||||
}
|
||||
first := caller.calls[0]
|
||||
if caller.calls[0].tool != "get_conversation_info" || caller.calls[0].args["openConversationId"] != "cid-1" ||
|
||||
caller.calls[1].tool != "get_conversation_info" || caller.calls[1].args["openConversationId"] != "cid-2" {
|
||||
t.Fatalf("scope preflight calls = %#v", caller.calls[:2])
|
||||
}
|
||||
first := caller.calls[2]
|
||||
if first.product != "im" || first.tool != "search_messages" {
|
||||
t.Fatalf("first call = %#v", first)
|
||||
}
|
||||
for key, want := range map[string]any{
|
||||
"openConversationIds": []string{"cid-1", "cid-2"},
|
||||
"senderUserIds": []string{"42"},
|
||||
"senderOpenDingTakIds": []string{"Dsender"},
|
||||
"atUserIds": []string{"43"},
|
||||
@@ -133,10 +168,13 @@ func TestCrossPlatformCoverageSearchMsgPagesAndEnrichesWithAdvancedFilters(t *te
|
||||
t.Errorf("%s = %#v, want %#v", key, first.args[key], want)
|
||||
}
|
||||
}
|
||||
if caller.calls[1].args["cursor"] != "c2" {
|
||||
t.Fatalf("second cursor = %#v", caller.calls[1].args["cursor"])
|
||||
if _, exists := first.args["openConversationIds"]; exists {
|
||||
t.Fatalf("global fallback unexpectedly forwarded openConversationIds: %#v", first.args)
|
||||
}
|
||||
if ids := caller.calls[2].args["openMsgIds"]; !reflect.DeepEqual(ids, []string{"m1", "m2"}) {
|
||||
if caller.calls[3].args["cursor"] != "c2" {
|
||||
t.Fatalf("second cursor = %#v", caller.calls[3].args["cursor"])
|
||||
}
|
||||
if ids := caller.calls[4].args["openMsgIds"]; !reflect.DeepEqual(ids, []string{"m1", "m2"}) {
|
||||
t.Fatalf("mget ids = %#v", ids)
|
||||
}
|
||||
if payload["complete"] != true || payload["count"] != float64(2) ||
|
||||
@@ -149,6 +187,10 @@ func TestCrossPlatformCoverageSearchMsgPagesAndEnrichesWithAdvancedFilters(t *te
|
||||
if firstMessage["text"] != "detail-1" {
|
||||
t.Fatalf("enriched message = %#v", firstMessage)
|
||||
}
|
||||
scope, _ := payload["scope"].(map[string]any)
|
||||
if scope["targetsValidated"] != true || scope["filterMode"] != "client" || scope["resultsWithinScope"] != true {
|
||||
t.Fatalf("scope = %#v", scope)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCrossPlatformCoverageSearchMsgLaterPageFailurePublishesPartialLedger(t *testing.T) {
|
||||
@@ -209,6 +251,175 @@ func TestCrossPlatformCoverageSearchMsgMissingMgetItemPublishesFailureLedger(t *
|
||||
}
|
||||
}
|
||||
|
||||
func TestSearchMsgScopedFallbackFiltersGlobalResults(t *testing.T) {
|
||||
caller := &searchMsgExecutionCaller{searchResponse: `{
|
||||
"result": {
|
||||
"conversationMessagesList": [
|
||||
{"openConversationId":"cid-target","title":"目标群","messages":[{"openMessageId":"m-target","content":"目标"}]},
|
||||
{"openConversationId":"cid-other","title":"其他群","messages":[{"openMessageId":"m-other","content":"越界"}]}
|
||||
],
|
||||
"hasMore": false
|
||||
}
|
||||
}`}
|
||||
payload := executeSearchMsg(t, caller, "--group", "cid-target", "--query", "目标", "--no-enrich")
|
||||
if payload["count"] != float64(1) || payload["complete"] != true {
|
||||
t.Fatalf("payload = %#v", payload)
|
||||
}
|
||||
messages, _ := payload["messages"].([]any)
|
||||
message, _ := messages[0].(map[string]any)
|
||||
if message["conversationId"] != "cid-target" || message["messageId"] != "m-target" {
|
||||
t.Fatalf("messages = %#v", messages)
|
||||
}
|
||||
if len(caller.calls) != 2 || caller.calls[0].tool != "get_conversation_info" || caller.calls[1].tool != "search_messages" {
|
||||
t.Fatalf("calls = %#v", caller.calls)
|
||||
}
|
||||
if _, exists := caller.calls[1].args["openConversationIds"]; exists {
|
||||
t.Fatalf("global fallback unexpectedly forwarded scope: %#v", caller.calls[1].args)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSearchMsgScopedValidEmptyResultIsComplete(t *testing.T) {
|
||||
caller := &searchMsgExecutionCaller{searchResponse: `{"result":{"messages":[],"hasMore":false}}`}
|
||||
payload := executeSearchMsg(t, caller, "--group", "cid-empty", "--query", "不存在", "--no-enrich")
|
||||
if payload["count"] != float64(0) || payload["complete"] != true {
|
||||
t.Fatalf("payload = %#v", payload)
|
||||
}
|
||||
scope, _ := payload["scope"].(map[string]any)
|
||||
if scope["targetsValidated"] != true || scope["sourceComplete"] != true {
|
||||
t.Fatalf("scope = %#v", scope)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSearchMsgScopedEmptyPartialScanCannotClaimComplete(t *testing.T) {
|
||||
caller := &searchMsgExecutionCaller{searchResponse: `{
|
||||
"result": {
|
||||
"conversationMessagesList": [
|
||||
{"openConversationId":"cid-other","messages":[{"openMessageId":"m-other"}]}
|
||||
],
|
||||
"hasMore": true,
|
||||
"nextCursor": "c2"
|
||||
}
|
||||
}`}
|
||||
payload := executeSearchMsg(t, caller,
|
||||
"--group", "cid-target", "--query", "周报", "--no-enrich", "--page-limit", "1")
|
||||
if payload["count"] != float64(0) || payload["complete"] != false || payload["failedCount"] != float64(1) {
|
||||
t.Fatalf("payload = %#v", payload)
|
||||
}
|
||||
scope, _ := payload["scope"].(map[string]any)
|
||||
if scope["sourceComplete"] != false {
|
||||
t.Fatalf("scope = %#v", scope)
|
||||
}
|
||||
failures, _ := payload["failures"].([]any)
|
||||
failure, _ := failures[0].(map[string]any)
|
||||
if failure["stage"] != "search-page-limit" {
|
||||
t.Fatalf("failure = %#v", failure)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSearchMsgInvalidCIDStopsBeforeGlobalSearch(t *testing.T) {
|
||||
caller := &searchMsgExecutionCaller{failPreflight: true}
|
||||
_, err := executeSearchMsgResult(caller, "--group", "cid-invalid", "--query", "周报", "--no-enrich")
|
||||
if err == nil {
|
||||
t.Fatal("invalid CID unexpectedly succeeded")
|
||||
}
|
||||
var typed *apperrors.Error
|
||||
if !errors.As(err, &typed) || typed.Reason != "search_conversation_scope_invalid" {
|
||||
t.Fatalf("error = %#v", err)
|
||||
}
|
||||
if len(caller.calls) != 1 || caller.calls[0].tool != "get_conversation_info" {
|
||||
t.Fatalf("calls = %#v", caller.calls)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCrossPlatformCoverageSearchMsgPreservesAmbiguousPreflightMCPToolErrors(t *testing.T) {
|
||||
for _, test := range []struct {
|
||||
name string
|
||||
want *helpers.CLIError
|
||||
}{
|
||||
{
|
||||
name: "rate limited",
|
||||
want: &helpers.CLIError{
|
||||
Code: helpers.CodeMCPToolError,
|
||||
Message: `{"success":false,"errorCode":"invalidRequest.rateLimited","errorMsg":"slow down"}`,
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "permission denied",
|
||||
want: &helpers.CLIError{
|
||||
Code: helpers.CodeMCPToolError,
|
||||
Message: `{"success":false,"errorCode":"forbidden.noPermission","errorMsg":"permission denied"}`,
|
||||
},
|
||||
},
|
||||
} {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
caller := &searchMsgExecutionCaller{preflightError: test.want}
|
||||
_, err := executeSearchMsgResult(caller,
|
||||
"--group", "cid-target", "--query", "周报", "--no-enrich")
|
||||
if err != test.want {
|
||||
t.Fatalf("error = %#v, want original %#v", err, test.want)
|
||||
}
|
||||
if len(caller.calls) != 1 || caller.calls[0].tool != "get_conversation_info" {
|
||||
t.Fatalf("calls = %#v", caller.calls)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestSearchMsgMissingConversationIdentityFailsClosed(t *testing.T) {
|
||||
caller := &searchMsgExecutionCaller{searchResponse: `{"result":{"messages":[{"openMessageId":"m1","content":"unknown"}],"hasMore":false}}`}
|
||||
_, err := executeSearchMsgResult(caller, "--group", "cid-target", "--query", "周报", "--no-enrich")
|
||||
if err == nil {
|
||||
t.Fatal("unverifiable scoped result unexpectedly succeeded")
|
||||
}
|
||||
var typed *apperrors.Error
|
||||
if !errors.As(err, &typed) || typed.Reason != "search_conversation_scope_unverified" {
|
||||
t.Fatalf("error = %#v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCrossPlatformCoverageSearchMsgScopeFailureBranches(t *testing.T) {
|
||||
want := errors.New("preflight unavailable")
|
||||
caller := &searchMsgExecutionCaller{preflightError: want}
|
||||
_, err := executeSearchMsgResult(caller, "--group", "cid-target", "--query", "周报", "--no-enrich")
|
||||
if !errors.Is(err, want) {
|
||||
t.Fatalf("preflight error = %v, want %v", err, want)
|
||||
}
|
||||
|
||||
caller = &searchMsgExecutionCaller{
|
||||
searchResponse: `{"result":{"messages":[{"openMessageId":"m1","openConversationId":"cid-target","content":"sparse"}],"hasMore":false}}`,
|
||||
missingMgetCID: true,
|
||||
}
|
||||
_, err = executeSearchMsgResult(caller, "--group", "cid-target", "--query", "周报")
|
||||
var typed *apperrors.Error
|
||||
if !errors.As(err, &typed) || typed.Reason != "search_conversation_scope_unverified" {
|
||||
t.Fatalf("enrichment scope error = %#v", err)
|
||||
}
|
||||
|
||||
err = searchScopeViolationError(
|
||||
[]string{"cid-target"},
|
||||
[]map[string]any{{"openMessageId": "m-empty"}, {"openMessageId": "m-other", "openConversationId": "cid-other"}},
|
||||
)
|
||||
typed = nil
|
||||
if !errors.As(err, &typed) || typed.Reason != "search_conversation_scope_violation" {
|
||||
t.Fatalf("scope violation error = %#v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSearchMsgEnrichmentCannotMoveMessageOutsideScope(t *testing.T) {
|
||||
caller := &searchMsgExecutionCaller{
|
||||
searchResponse: `{"result":{"messages":[{"openMessageId":"m1","openConversationId":"cid-target","content":"sparse"}],"hasMore":false}}`,
|
||||
wrongMgetScope: true,
|
||||
}
|
||||
_, err := executeSearchMsgResult(caller, "--group", "cid-target", "--query", "周报")
|
||||
if err == nil {
|
||||
t.Fatal("out-of-scope enrichment unexpectedly succeeded")
|
||||
}
|
||||
var typed *apperrors.Error
|
||||
if !errors.As(err, &typed) || typed.Reason != "search_conversation_scope_violation" {
|
||||
t.Fatalf("error = %#v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCrossPlatformCoverageSearchMsgLarkTimeAliasesAndAscendingOrder(t *testing.T) {
|
||||
caller := &searchMsgExecutionCaller{
|
||||
firstResponse: `{"result":{"messages":[{"openMessageId":"m2","createTime":1782892800000,"content":"later"},{"openMessageId":"m1","createTime":1782806400000,"content":"earlier"}],"hasMore":false}}`,
|
||||
|
||||
@@ -1047,6 +1047,7 @@ func authActions(snapshotPath string) []string {
|
||||
func runtimeActions(snapshotPath string) []string {
|
||||
actions := []string{
|
||||
i18n.T("检查认证、权限和参数后重试原命令"),
|
||||
i18n.T("运行 dws doctor 检查登录态、网络和本地环境;持续失败时保留 Trace ID 和 Server Code"),
|
||||
}
|
||||
_ = snapshotPath
|
||||
return actions
|
||||
@@ -1056,6 +1057,7 @@ func networkActions(snapshotPath string) []string {
|
||||
actions := []string{
|
||||
i18n.T("检查网络、代理和 DNS 配置后重试原命令"),
|
||||
i18n.T("确认 MCP 服务可访问;若持续失败请稍后重试"),
|
||||
i18n.T("运行 dws doctor 获取可共享的环境诊断结果"),
|
||||
}
|
||||
_ = snapshotPath
|
||||
return actions
|
||||
|
||||
@@ -397,13 +397,13 @@ func TestCrossPlatformCoverageHTTPStatusAndDiagnosticsEdges(t *testing.T) {
|
||||
t.Fatalf("HTTP %d returned nil", tc.code)
|
||||
}
|
||||
}
|
||||
if got := networkActions("snap"); len(got) != 2 {
|
||||
if got := networkActions("snap"); len(got) != 3 {
|
||||
t.Fatalf("network actions = %#v", got)
|
||||
}
|
||||
if got := authActions("snap"); len(got) != 2 {
|
||||
t.Fatalf("auth actions = %#v", got)
|
||||
}
|
||||
if got := runtimeActions("snap"); len(got) != 1 {
|
||||
if got := runtimeActions("snap"); len(got) != 2 {
|
||||
t.Fatalf("runtime actions = %#v", got)
|
||||
}
|
||||
if got := actionsForMethod("tools/call", "snap"); len(got) == 0 {
|
||||
|
||||
@@ -42,18 +42,48 @@ func ExtractServerDiagnosticsFromMap(content map[string]any) apperrors.ServerDia
|
||||
return apperrors.ServerDiagnostics{}
|
||||
}
|
||||
diag := apperrors.ServerDiagnostics{
|
||||
TraceID: stringFromMap(content, "trace_id", "traceId"),
|
||||
TraceID: stringFromMapRecursive(content, 0, "trace_id", "traceId", "request_id", "requestId"),
|
||||
ServerErrorCode: serverErrorCodeFromMap(content, 0),
|
||||
TechnicalDetail: stringFromMap(content, "technical_detail"),
|
||||
TechnicalDetail: stringFromMapRecursive(content, 0, "technical_detail", "technicalDetail"),
|
||||
FriendlyHint: stringFromMapRecursive(content, 0, "friendly_hint", "friendlyHint"),
|
||||
ActionURL: stringFromMapRecursive(content, 0, "action_url", "actionUrl"),
|
||||
}
|
||||
if v, ok := content["retryable"].(bool); ok {
|
||||
if v, ok := boolFromMapRecursive(content, 0, "retryable", "server_retryable", "serverRetryable"); ok {
|
||||
diag.ServerRetryable = &v
|
||||
}
|
||||
return diag
|
||||
}
|
||||
|
||||
func boolFromMapRecursive(content map[string]any, depth int, keys ...string) (bool, bool) {
|
||||
if content == nil || depth > 8 {
|
||||
return false, false
|
||||
}
|
||||
for _, key := range keys {
|
||||
if value, ok := content[key].(bool); ok {
|
||||
return value, true
|
||||
}
|
||||
}
|
||||
for _, key := range []string{"content", "result", "data"} {
|
||||
switch child := content[key].(type) {
|
||||
case map[string]any:
|
||||
if value, ok := boolFromMapRecursive(child, depth+1, keys...); ok {
|
||||
return value, true
|
||||
}
|
||||
case []any:
|
||||
for _, item := range child {
|
||||
childMap, ok := item.(map[string]any)
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
if value, ok := boolFromMapRecursive(childMap, depth+1, keys...); ok {
|
||||
return value, true
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return false, false
|
||||
}
|
||||
|
||||
func stringFromMapRecursive(content map[string]any, depth int, keys ...string) string {
|
||||
if content == nil || depth > 8 {
|
||||
return ""
|
||||
|
||||
@@ -110,6 +110,44 @@ func TestExtractServerDiagnosticsFromMapNestedBusinessCode(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestExtractServerDiagnosticsFromMapNestedDiagnosticEnvelope(t *testing.T) {
|
||||
t.Parallel()
|
||||
content := map[string]any{
|
||||
"result": map[string]any{
|
||||
"data": map[string]any{
|
||||
"requestId": "request-nested",
|
||||
"technicalDetail": "nested detail",
|
||||
"serverRetryable": false,
|
||||
},
|
||||
},
|
||||
}
|
||||
diag := ExtractServerDiagnosticsFromMap(content)
|
||||
if diag.TraceID != "request-nested" || diag.TechnicalDetail != "nested detail" ||
|
||||
diag.ServerRetryable == nil || *diag.ServerRetryable {
|
||||
t.Fatalf("nested diagnostics = %#v", diag)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCrossPlatformCoverageBoolDiagnosticsRecursiveEdges(t *testing.T) {
|
||||
t.Parallel()
|
||||
if value, ok := boolFromMapRecursive(nil, 0, "retryable"); ok || value {
|
||||
t.Fatalf("nil recursive bool = (%v, %v)", value, ok)
|
||||
}
|
||||
if value, ok := boolFromMapRecursive(map[string]any{"retryable": true}, 9, "retryable"); ok || value {
|
||||
t.Fatalf("over-depth recursive bool = (%v, %v)", value, ok)
|
||||
}
|
||||
content := map[string]any{
|
||||
"result": []any{
|
||||
"ignore",
|
||||
map[string]any{"data": map[string]any{"serverRetryable": true}},
|
||||
},
|
||||
}
|
||||
value, ok := boolFromMapRecursive(content, 0, "serverRetryable")
|
||||
if !ok || !value {
|
||||
t.Fatalf("nested array recursive bool = (%v, %v)", value, ok)
|
||||
}
|
||||
}
|
||||
|
||||
func TestExtractServerDiagnosticsFromMap_Empty(t *testing.T) {
|
||||
t.Parallel()
|
||||
diag := ExtractServerDiagnosticsFromMap(nil)
|
||||
|
||||
@@ -112,8 +112,10 @@
|
||||
| `workflow get` | 获取单个工作流详情(含 flowSchema) | `--base-id` `--workflow-id` | `--workflow-id` 接受 list 里的 `flowId`(同值) |
|
||||
| `workflow enable` | 启用工作流 | `--base-id` `--workflow-id` | 返回 `{enabled: true}` 是动作确认;要确认真启用看 list 的 `status` |
|
||||
| `workflow disable` | 禁用工作流(高危) | `--base-id` `--workflow-id` `--yes` | 影响业务自动化,建议二次确认;status 变 STOP |
|
||||
| `workflow run` | 立即执行工作流(需确认) | `--base-id` `--workflow-id`;记录触发另需 `--table-id` `--record-ids` | 返回 `executionId`;不确定时先用 history 核对,避免重复执行 |
|
||||
| `workflow history` | 查询工作流执行历史 | `--base-id` `--workflow-id` | 支持 status、Unix 毫秒时间范围和 page/size;`instanceId` 对应 run 的 `executionId` |
|
||||
|
||||
> 当前支持创建、更新、查询和启停;删除、运行历史及手动触发仍未开放。
|
||||
> 当前支持创建、更新、查询、启停、手动执行和执行历史;删除工作流仍未开放。
|
||||
|
||||
### dashboard & chart → 详见 [aitable-dashboard-chart.md](./aitable/aitable-dashboard-chart.md)
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
# workflow — 自动化工作流管理
|
||||
|
||||
创建 / 更新 / 启停 / 查看 / 列出 Base 下的自动化工作流("当 X 时自动 Y" 流程)。
|
||||
适用场景:用户要求创建自动化、修改流程、停掉流程、查询已有流程或恢复运行。
|
||||
创建 / 更新 / 启停 / 手动执行 / 查询执行历史 / 查看 / 列出 Base 下的自动化工作流("当 X 时自动 Y" 流程)。
|
||||
适用场景:用户要求创建自动化、修改流程、停掉或恢复流程、立即执行流程、核对执行结果或查询已有流程。
|
||||
|
||||
## 命令一览
|
||||
|
||||
@@ -14,6 +14,8 @@
|
||||
| `workflow get` | 获取单个工作流详情(含 flowSchema 完整节点定义) |
|
||||
| `workflow enable` | 启用指定工作流(按配置的触发条件自动执行) |
|
||||
| `workflow disable` | 禁用指定工作流(高危,建议 `--yes` 二次确认) |
|
||||
| `workflow run` | 立即执行指定工作流(会产生真实副作用,需确认) |
|
||||
| `workflow history` | 按状态、时间和分页条件查询工作流执行历史 |
|
||||
|
||||
> `workflow edit-example` 无参数;其他子命令的 `--base-id` 必填(可用隐藏别名 `--base`)。
|
||||
|
||||
@@ -104,6 +106,29 @@ dws aitable workflow update \
|
||||
|
||||
update 返回结构与 create 相同,并会对瞬态错误重试。成功时 `data.flowId` 应与传入的工作流 ID 一致;随后用 `workflow get/list` 验证发布结果和运行状态。
|
||||
|
||||
### workflow run — 立即执行工作流
|
||||
|
||||
```bash
|
||||
# 记录类触发器
|
||||
dws aitable workflow run --base-id BASE_ID --workflow-id WORKFLOW_ID \
|
||||
--table-id TABLE_ID --record-ids RECORD_ID_1,RECORD_ID_2
|
||||
|
||||
# 定时触发器不传 table-id / record-ids
|
||||
dws aitable workflow run --base-id BASE_ID --workflow-id WORKFLOW_ID
|
||||
```
|
||||
|
||||
记录类触发器必须同时传 `--table-id` 和 `--record-ids`;记录 ID 限 1–5 个且不可重复。`run` 会启动真实异步执行并产生工作流配置的副作用,必须先取得用户确认。返回的 `executionId` 可与 `workflow history` 项目的 `instanceId` 匹配;网络结果不确定时先查历史,不要直接重复执行。
|
||||
|
||||
### workflow history — 查询执行历史
|
||||
|
||||
```bash
|
||||
dws aitable workflow history --base-id BASE_ID --workflow-id WORKFLOW_ID \
|
||||
--status failed --after-time 1786000000000 --before-time 1787000000000 \
|
||||
--page 0 --size 50
|
||||
```
|
||||
|
||||
`--status` 接受 `success` / `failed` / `running` / `break` / `untrigger`;时间参数是 Unix 毫秒且 after 必须小于 before;`--page` 从 0 开始,`--size` 默认 20、最大 100。返回 `totalCount` 与 `list`,其中 `running` 为非终态。
|
||||
|
||||
### workflow list — 列出工作流
|
||||
|
||||
```bash
|
||||
@@ -211,9 +236,9 @@ dws aitable workflow disable --base-id BASE_ID --workflow-id WORKFLOW_ID --yes -
|
||||
| 列出工作流 | ✅ |
|
||||
| 看工作流详情(含 flowSchema) | ✅ |
|
||||
| 启用/禁用 | ✅ |
|
||||
| 查看运行历史/执行日志 | ✅ `workflow history` |
|
||||
| 手动触发/单次运行 | ✅ `workflow run`(需确认) |
|
||||
| 删除工作流 | ❌ 暂未开放 |
|
||||
| 查看运行历史/执行日志 | ❌ 暂未开放 |
|
||||
| 手动触发/单次运行 | ❌ 暂未开放 |
|
||||
|
||||
## 错误码速查
|
||||
|
||||
@@ -282,4 +307,5 @@ done
|
||||
- create 不自动重试;update 仅对网络、5xx、`retryable:true` 等瞬态错误自动重试。
|
||||
- enable / disable 出参里的 `enabled` / `disabled` 是 **动作确认 flag**,不是当前状态字段。要确认真生效请走 `workflow list` 查 `status`。
|
||||
- `workflow get` 的 `flowSchema` 结构随触发器/动作类型变化,不要假设固定字段。
|
||||
- 删除、运行历史和手动触发当前仍未开放。
|
||||
- `workflow run` 不自动重试;结果不确定时用 `workflow history` 按 executionId / instanceId 核对。
|
||||
- 删除工作流当前仍未开放。
|
||||
|
||||
@@ -836,6 +836,8 @@ Flags:
|
||||
#### 拉取指定时间范围内当前用户的所有会话消息 — 分页拉取当前登录用户在指定时间范围内的所有会话消息
|
||||
|
||||
--start 和 --end 限定时间范围,--limit 指定每页数量,--cursor 传分页游标(首页传 "0",后续从响应中的 nextCursor 获取)。服务端按 cursor 分页返回,hasMore=true 时用返回的 nextCursor 值作为下次 --cursor 继续翻页。若当前账号没有消息搜索权益,CLI 会透传服务端的友好提示与开通入口。
|
||||
|
||||
需要自动遍历全部分页时显式加 `--page-all`。`--page-limit` 控制最多请求页数(默认 50,范围 1-500),`--max-items` 控制最多输出条数(默认 0 不限制,精确截断并输出 `paging.truncated=true`),`--page-delay` 控制页间等待毫秒数(默认 200,0 表示不等待)。只传这些分页控制参数但不传 `--page-all` 时仍保持原单页调用。
|
||||
```
|
||||
Usage:
|
||||
dws chat message list-all [flags]
|
||||
@@ -847,11 +849,16 @@ Flags:
|
||||
--end string 结束时间,格式: yyyy-MM-dd HH:mm:ss (必填)
|
||||
--limit int 每页返回数量(默认 50)
|
||||
--cursor string 分页游标(首页传 "0",后续从响应中的 nextCursor 获取)
|
||||
--page-all 自动按 nextCursor 拉取所有分页
|
||||
--page-limit int 自动翻页最多请求页数(默认 50,范围 1-500)
|
||||
--max-items int 自动翻页最多返回条数(默认 0 表示不限制)
|
||||
--page-delay int 自动翻页每页之间等待毫秒数(默认 200;0 表示不等待)
|
||||
|
||||
注意:
|
||||
- 四个参数每次请求都会传递给服务端,cursor 首页传 "0"
|
||||
- 与 chat message list 的区别:list 拉取指定单个会话(群聊或单聊)的消息,list-all 拉取当前用户所有会话的消息
|
||||
- 翻页:hasMore=true 时,用响应中的 nextCursor 值作为下次 --cursor 参数继续翻页
|
||||
- 自动翻页:`--page-all` 会保留并合并 `result.conversationMessagesList`,同一会话跨页合并 messages,并在顶层输出 `paging` 元数据;shortcut 命令不属于本 typed fallback 小节
|
||||
- 时间格式统一为 yyyy-MM-dd HH:mm:ss
|
||||
- 权限/权益错误不是空结果;应把返回的 friendly_hint 与 action_url 展示给用户,不要继续盲目翻页
|
||||
```
|
||||
@@ -861,6 +868,8 @@ Flags:
|
||||
> 推荐优先使用 `chat message search-advanced --user/--users`(userId)或 `--sender-ids`(openDingTalkId),它还能叠加关键词/群/at 等过滤条件。本命令保留给需要旧 list-by-sender 返回结构的场景。
|
||||
|
||||
搜索特定人发送给我的消息,返回结果包含单聊和群聊标识。--sender-user-id 指定发送者 userId,--sender-open-dingtalk-id 指定发送者 openDingTalkId,二者互斥。分页参数 --limit(默认 50)和 --cursor(默认 "0")始终传递;hasMore=true 时用返回的 nextCursor 作为下次 --cursor 继续翻页。
|
||||
|
||||
自动翻页同样使用 `--page-all` 触发,复用发送者、时间范围和 limit 条件,只替换每页 `cursor`;输出保留并合并 `result.conversationMessagesList`,控制参数为 `--page-limit`、`--max-items`、`--page-delay`。
|
||||
```
|
||||
Usage:
|
||||
dws chat message list-by-sender [flags]
|
||||
@@ -876,6 +885,10 @@ Flags:
|
||||
--end string 结束时间,ISO-8601 格式 (必填)
|
||||
--limit int 每页返回数量(默认 50)
|
||||
--cursor string 分页游标(默认 "0",翻页传 nextCursor)
|
||||
--page-all 自动按 nextCursor 拉取所有分页
|
||||
--page-limit int 自动翻页最多请求页数(默认 50,范围 1-500)
|
||||
--max-items int 自动翻页最多返回条数(默认 0 表示不限制)
|
||||
--page-delay int 自动翻页每页之间等待毫秒数(默认 200;0 表示不等待)
|
||||
|
||||
注意:
|
||||
- --sender-user-id 和 --sender-open-dingtalk-id 二者互斥,必须且只能指定其一:
|
||||
@@ -892,6 +905,8 @@ Flags:
|
||||
> 推荐使用 `chat message search-advanced --at-me`,它还能叠加关键词/群/发送者等过滤条件。本命令适用于仅需拉取 @我 消息的简单场景。
|
||||
|
||||
搜索时间范围内 @我 的消息,可选指定群聊。返回结果包含单聊和群聊标识。分页参数 --limit(默认 50)和 --cursor(默认 "0")始终传递;hasMore=true 时用返回的 nextCursor 作为下次 --cursor 继续翻页。
|
||||
|
||||
自动翻页同样使用 `--page-all` 触发,复用时间范围和 group 过滤条件,只替换每页 `cursor`;输出保留并合并 `result.conversationMessagesList`,控制参数为 `--page-limit`、`--max-items`、`--page-delay`。
|
||||
```
|
||||
Usage:
|
||||
dws chat message list-mentions [flags]
|
||||
@@ -906,6 +921,10 @@ Flags:
|
||||
--end string 结束时间,ISO-8601 格式 (必填)
|
||||
--limit int 每页返回数量(默认 50)
|
||||
--cursor string 分页游标(默认 "0",翻页传 nextCursor)
|
||||
--page-all 自动按 nextCursor 拉取所有分页
|
||||
--page-limit int 自动翻页最多请求页数(默认 50,范围 1-500)
|
||||
--max-items int 自动翻页最多返回条数(默认 0 表示不限制)
|
||||
--page-delay int 自动翻页每页之间等待毫秒数(默认 200;0 表示不等待)
|
||||
|
||||
注意:
|
||||
- --group 可选,不传则查询所有会话中 @我 的消息;传入则只查指定群聊
|
||||
@@ -917,6 +936,8 @@ Flags:
|
||||
#### 拉取特别关注人的消息
|
||||
|
||||
拉取当前用户特别关注人的消息。分页参数 --limit 指定每页数量,--cursor 传分页游标(首次不传或传 0)。返回结果中 hasMore=true 时用 nextCursor 作为下次 --cursor 继续翻页。
|
||||
|
||||
自动翻页使用 `--page-all` 触发,按 int64 cursor 注入下一页游标并聚合 `result.messages`;控制参数为 `--page-limit`、`--max-items`、`--page-delay`。
|
||||
```
|
||||
Usage:
|
||||
dws chat message list-focused [flags]
|
||||
@@ -926,6 +947,10 @@ Example:
|
||||
Flags:
|
||||
--limit int 每页返回数量(默认 50)
|
||||
--cursor int64 分页游标(首次不传或传 0,翻页传 nextCursor)
|
||||
--page-all 自动按 nextCursor 拉取所有分页
|
||||
--page-limit int 自动翻页最多请求页数(默认 50,范围 1-500)
|
||||
--max-items int 自动翻页最多返回条数(默认 0 表示不限制)
|
||||
--page-delay int 自动翻页每页之间等待毫秒数(默认 200;0 表示不等待)
|
||||
|
||||
注意:
|
||||
- 首次调用不传 --cursor 或传 0,后续翻页传 nextCursor
|
||||
@@ -977,6 +1002,8 @@ Flags:
|
||||
> 推荐优先使用 `chat message search-advanced`,它是本命令的严格超集:query 可选(非必填)、支持多个会话(非单个)、还能叠加发送者/at 等维度过滤。
|
||||
|
||||
按关键词搜索消息内容。--query 指定搜索关键词(必填)。可选 --group 限定搜索某个会话,不传则搜索所有会话。时间参数 --start/--end(ISO-8601)限定搜索时间范围。分页参数 --limit(默认 100)和 --cursor(默认 "0")始终传递;hasMore=true 时用返回的 nextCursor 作为下次 --cursor 继续翻页。
|
||||
|
||||
自动翻页同样使用 `--page-all` 触发,复用 query、时间范围和 group 条件,只替换每页 `cursor`;输出保留并合并 `result.conversationMessagesList`,控制参数为 `--page-limit`、`--max-items`、`--page-delay`。
|
||||
```
|
||||
Usage:
|
||||
dws chat message search [flags]
|
||||
@@ -991,6 +1018,10 @@ Flags:
|
||||
--end string 结束时间,ISO-8601 格式 (必填)
|
||||
--limit int 每页返回数量(默认 100)
|
||||
--cursor string 分页游标(默认 "0",翻页传 nextCursor)
|
||||
--page-all 自动按 nextCursor 拉取所有分页
|
||||
--page-limit int 自动翻页最多请求页数(默认 50,范围 1-500)
|
||||
--max-items int 自动翻页最多返回条数(默认 0 表示不限制)
|
||||
--page-delay int 自动翻页每页之间等待毫秒数(默认 200;0 表示不等待)
|
||||
|
||||
注意:
|
||||
- --group 可选,不传则搜索所有会话中的消息;传入则只搜索指定会话
|
||||
@@ -1004,6 +1035,8 @@ Flags:
|
||||
> 推荐:这是消息搜索的首选接口。它可以完全替代 `chat message search`(query 可选 vs 必填,支持多个会话 vs 单个),大部分替代 `chat message list-by-sender`(通过 --user/--users 按 userId 搜索发送者,或通过 --sender-ids 按 openDingTalkId 搜索)和 `chat message list-mentions`(通过 --at-me 搜索@我的消息)。仅在拉取「特别关注人」消息时需要退回 `list-focused`。
|
||||
|
||||
支持按关键词、发送者、@我、@指定人、指定会话、时间范围等多维度搜索消息。发送者 userId 使用 --user/--users;发送者或 @ 人的 openDingTalkId 使用 --sender-ids/--at-ids。所有参数均为可选,至少指定一个搜索条件。
|
||||
|
||||
自动翻页使用 `--page-all` 触发,复用所有高级过滤参数,只替换每页 `cursor`,保留并合并 `result.conversationMessagesList`;控制参数为 `--page-limit`、`--max-items`、`--page-delay`。
|
||||
```
|
||||
Usage:
|
||||
dws chat message search-advanced [flags]
|
||||
@@ -1030,6 +1063,10 @@ Flags:
|
||||
--end string 结束时间,ISO-8601 格式(可选)
|
||||
--cursor string 分页游标(默认 "0")
|
||||
--limit int 每页返回数量(默认 100)
|
||||
--page-all 自动按 nextCursor 拉取所有分页
|
||||
--page-limit int 自动翻页最多请求页数(默认 50,范围 1-500)
|
||||
--max-items int 自动翻页最多返回条数(默认 0 表示不限制)
|
||||
--page-delay int 自动翻页每页之间等待毫秒数(默认 200;0 表示不等待)
|
||||
--conversation-ids 的别名: --groups
|
||||
|
||||
注意:
|
||||
@@ -1423,6 +1460,8 @@ Flags:
|
||||
```
|
||||
|
||||
#### 查询收藏消息 — 分页查询当前用户收藏的消息
|
||||
|
||||
自动翻页使用 `--page-all` 触发,按 int64 cursor 注入下一页游标并聚合 `result.items`(不是 `result.messages`)。`--size` 仍保持 1-30 的 Open 服务范围;`--page-limit` 只控制最多请求页数。
|
||||
```
|
||||
Usage:
|
||||
dws chat message list-favorites [flags]
|
||||
@@ -1433,10 +1472,15 @@ Example:
|
||||
Flags:
|
||||
--cursor int 数字分页游标,默认 0;翻页时传上次返回的 nextCursor
|
||||
--size int 一次拉取的收藏数量,默认 20,范围 1-30
|
||||
--page-all 自动按 nextCursor 拉取所有分页
|
||||
--page-limit int 自动翻页最多请求页数(默认 50,范围 1-500)
|
||||
--max-items int 自动翻页最多返回条数(默认 0 表示不限制)
|
||||
--page-delay int 自动翻页每页之间等待毫秒数(默认 200;0 表示不等待)
|
||||
|
||||
注意:
|
||||
- 首次请求可省略分页参数,CLI 会自动向 Open 服务传入 cursor=0、size="20"
|
||||
- hasMore=true 时,将 nextCursor 作为下一次的 --cursor
|
||||
- 自动翻页输出顶层 `paging` 元数据,达到 `--page-limit` 或 `--max-items` 时 `truncated=true`
|
||||
```
|
||||
|
||||
### bot (机器人管理)
|
||||
|
||||
@@ -57,9 +57,10 @@ dws minutes
|
||||
│ ├── create --file-name "..." --file-size <n> # 创建上传会话,获取上传地址
|
||||
│ ├── complete --session-id <sid> # 确认上传完成
|
||||
│ └── cancel --session-id <sid> # 取消上传会话
|
||||
└── permission <subcommand> # 权限管理(添加/移除听记成员权限)
|
||||
└── permission <subcommand> # 权限管理(添加/移除听记成员权限、为自己申请权限)
|
||||
├── add --ids <uuid1,uuid2> --member-uids <uid1,uid2> --policy <0-4> [--cover] [--sub-resources "..."] # 添加成员权限
|
||||
└── remove --ids <uuid1,uuid2> --member-uids <uid1,uid2> # 移除成员权限
|
||||
├── remove --ids <uuid1,uuid2> --member-uids <uid1,uid2> # 移除成员权限
|
||||
└── apply --id <uuid> --policy <2-4> # 为当前用户申请听记权限
|
||||
```
|
||||
|
||||
**高频错误命令 vs 正确命令对照(真实 badcase 提炼):**
|
||||
@@ -308,7 +309,8 @@ dws minutes
|
||||
│ └── cancel --session-id <sid>
|
||||
├── permission <subcommand>
|
||||
│ ├── add --ids <uuid1,uuid2> --member-uids <uid1,uid2> --policy <0-4> [--cover] [--sub-resources "..."]
|
||||
│ └── remove --ids <uuid1,uuid2> --member-uids <uid1,uid2>
|
||||
│ ├── remove --ids <uuid1,uuid2> --member-uids <uid1,uid2>
|
||||
│ └── apply --id <uuid> --policy <2-4>
|
||||
└── tag <subcommand> # 听记标签/分组管理(标签在听记页面手动创建)
|
||||
├── list # 查询我的听记标签/分组列表(返回 tagId 和标签名称)
|
||||
└── query --tag-id <tagId> [--limit] [--cursor] # 根据标签ID查询该标签下的听记列表
|
||||
@@ -1211,6 +1213,38 @@ Flags:
|
||||
- 如果成员是听记的创建者(所有者),无法通过此命令移除其权限
|
||||
- 建议在移除前先确认成员的当前权限,避免误操作
|
||||
|
||||
### 为当前用户申请听记权限
|
||||
```
|
||||
Usage:
|
||||
dws minutes permission apply [flags]
|
||||
Example:
|
||||
dws minutes permission apply --id <taskUuid> --policy 4
|
||||
dws minutes permission apply --id <taskUuid> --policy 2
|
||||
Flags:
|
||||
--id string 听记 taskUuid (必填)
|
||||
--policy int 权限类型: 2=可编辑, 3=可查看/下载, 4=仅查看 (必填)
|
||||
```
|
||||
|
||||
为当前登录用户申请指定听记的权限,无需传入用户 ID,系统自动识别当前用户身份。
|
||||
适用于用户无权限访问某听记(如打开分享链接提示无权限)时,主动向听记所有者发起权限申请。
|
||||
|
||||
**权限类型说明:**
|
||||
|
||||
| --policy 值 | 含义 | 说明 |
|
||||
|------------|------|------|
|
||||
| 2 | 可编辑 | 可编辑听记的纪要、待办等内容 |
|
||||
| 3 | 可查看/下载 | 可查看和下载听记内容,不可编辑 |
|
||||
| 4 | 仅查看 | 仅可查看听记内容,不可下载 |
|
||||
|
||||
**与 permission add 的区别:**
|
||||
- `permission add` 是听记所有者/管理员**给别人授权**,需传 `--member-uids`
|
||||
- `permission apply` 是当前用户**为自己申请权限**,不需要任何用户 ID,不支持申请管理员/所有者权限(0/1)
|
||||
|
||||
**典型使用场景:**
|
||||
- 打开同事分享的听记链接提示无权限,申请查看权限
|
||||
- 需要编辑某篇只读听记的纪要,申请可编辑权限
|
||||
- 需要下载听记音频但当前仅有查看权限,申请可查看/下载权限
|
||||
|
||||
### 查询我的听记标签/分组列表
|
||||
```
|
||||
Usage:
|
||||
@@ -1595,6 +1629,7 @@ tagId 可通过 `dws minutes tag list` 获取。
|
||||
用户说"把某人加到这个听记中/共享听记给某人/给某人添加听记权限/把听记分享给同事/让某人也能看这个听记" → `permission add`
|
||||
用户说"添加参会者/批量添加参会者/添加成员/批量添加成员/加参会人/加入成员" → `permission add`(**注意:听记模块没有独立的"添加参会者/成员"命令,统一走权限接口**)
|
||||
用户说"移除某人的权限/取消共享/不让某人看这个听记了" → `permission remove`
|
||||
用户说"我没权限看这个听记/帮我申请这个听记的权限/申请查看权限/申请编辑权限" → `permission apply`(**注意:为当前用户自己申请,不是给别人授权**)
|
||||
|
||||
**自然语言示例:**
|
||||
- "把张三加到这个听记中"
|
||||
@@ -1611,6 +1646,9 @@ tagId 可通过 `dws minutes tag list` 获取。
|
||||
- "批量添加成员到这个听记"
|
||||
- "把张三加为参会人"
|
||||
- "移除张三对这个听记的权限"
|
||||
- "我打不开这个听记,帮我申请一下权限"
|
||||
- "帮我申请这个听记的查看权限"
|
||||
- "申请这篇听记的编辑权限"
|
||||
- "取消小王对这个听记的访问"
|
||||
- "不让某人看这个听记了"
|
||||
|
||||
@@ -1618,8 +1656,9 @@ tagId 可通过 `dws minutes tag list` 获取。
|
||||
- 用户提到"加/添加/共享/分享/让...看/让...访问"等增加权限语义 → `permission add`
|
||||
- 用户提到"添加参会者/加参会者/批量添加参会者/添加成员/加成员/批量添加成员/加参会人/添加参会人"等成员语义 → `permission add`(听记没有独立的"添加成员"接口,**统一走权限接口**)
|
||||
- 用户提到"移除/取消/删除/不让...看"等移除权限语义 → `permission remove`
|
||||
- 如果用户未指定权限类型,默认使用 `--policy 4`(可查看/下载/编辑)
|
||||
- 如果用户未提供 member-uids,需要先引导用户提供目标成员的钉钉 UID(可通过 `dws contact user search --query "姓名"` 查询)
|
||||
- 用户提到"我没权限/打不开/帮我申请/申请查看权限/申请编辑权限"等**为自己申请**语义 → `permission apply`(只需 `--id` + `--policy`;**不需要也不接受 `--member-uids`**,当前用户身份由系统自动识别;未说明权限等级时先与用户确认要 2(可编辑)/ 3(可查看下载)/ 4(仅查看))
|
||||
- `permission add` 的 `--policy` 是必填参数,没有默认值。用户未指定权限类型时,先确认要 0(管理员)/ 1(所有者)/ 2(可编辑)/ 3(可查看下载)/ 4(仅查看);即使确认选择 4,命令中仍必须显式传入 `--policy 4`
|
||||
- `permission add` / `permission remove` 需要目标成员的钉钉 UID:如果用户未提供 member-uids,需要先引导用户提供(可通过 `dws contact user search --query "姓名"` 查询);`permission apply` 不适用这一条
|
||||
|
||||
**典型执行链路:**
|
||||
1. 用户说"把张三加到这个听记中" → AI 需获取张三的 UID
|
||||
|
||||
@@ -141,6 +141,8 @@ Flags:
|
||||
| `workflow get` | 获取单个工作流详情(含 flowSchema) | `--base-id` `--workflow-id` | `--workflow-id` 接受 list 里的 `flowId`(同值) |
|
||||
| `workflow enable` | 启用工作流 | `--base-id` `--workflow-id` | 返回 `{enabled: true}` 是动作确认;要确认真启用看 list 的 `status` |
|
||||
| `workflow disable` | 禁用工作流(高危) | `--base-id` `--workflow-id` `--yes` | 影响业务自动化,建议二次确认;status 变 STOP |
|
||||
| `workflow run` | 立即执行工作流(需确认) | `--base-id` `--workflow-id`;记录触发另需 `--table-id` `--record-ids` | 返回 `executionId`;不确定时先用 history 核对,避免重复执行 |
|
||||
| `workflow history` | 查询工作流执行历史 | `--base-id` `--workflow-id` | 支持 status、Unix 毫秒时间范围和 page/size;`instanceId` 对应 run 的 `executionId` |
|
||||
|
||||
> 创建/更新的 `--dsl` 使用钉钉 AI 表格 `workflow-dsl/v1`;完整格式和最小 Demo 见 [aitable-workflow.md](./aitable/aitable-workflow.md)。删除工作流暂未开放。
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
# workflow — 自动化工作流管理
|
||||
|
||||
创建 / 更新 / 启停 / 查看 / 列出 Base 下的自动化工作流("当 X 时自动 Y" 流程)。
|
||||
适用场景:用户要求创建自动化、修改流程、停掉流程、查询已有流程或恢复运行。
|
||||
创建 / 更新 / 启停 / 手动执行 / 查询执行历史 / 查看 / 列出 Base 下的自动化工作流("当 X 时自动 Y" 流程)。
|
||||
适用场景:用户要求创建自动化、修改流程、停掉或恢复流程、立即执行流程、核对执行结果或查询已有流程。
|
||||
|
||||
## 命令一览
|
||||
|
||||
@@ -14,6 +14,8 @@
|
||||
| `workflow get` | 获取单个工作流详情(含 flowSchema 完整节点定义) |
|
||||
| `workflow enable` | 启用指定工作流(按配置的触发条件自动执行) |
|
||||
| `workflow disable` | 禁用指定工作流(高危,建议 `--yes` 二次确认) |
|
||||
| `workflow run` | 立即执行指定工作流(会产生真实副作用,需确认) |
|
||||
| `workflow history` | 按状态、时间和分页条件查询工作流执行历史 |
|
||||
|
||||
> `workflow edit-example` 无参数;其他子命令的 `--base-id` 必填(可用隐藏别名 `--base`)。
|
||||
|
||||
@@ -155,6 +157,46 @@ dws aitable workflow update \
|
||||
|
||||
返回结构与 create 相同,成功时 `flowId` 应为目标工作流。update 使用 AI 表格瞬态错误重试;最终仍必须检查 `data.valid` 和 `issues`,并用 `workflow get/list` 验证发布结果与运行状态。
|
||||
|
||||
### workflow run — 立即执行工作流
|
||||
|
||||
```bash
|
||||
# 记录类触发器
|
||||
dws aitable workflow run --base-id BASE_ID --workflow-id WORKFLOW_ID \
|
||||
--table-id TABLE_ID --record-ids RECORD_ID_1,RECORD_ID_2
|
||||
|
||||
# 定时触发器不传 table-id / record-ids
|
||||
dws aitable workflow run --base-id BASE_ID --workflow-id WORKFLOW_ID
|
||||
```
|
||||
|
||||
| flag | 必填 | 说明 |
|
||||
|------|------|------|
|
||||
| `--base-id` | 是 | 所属 Base ID |
|
||||
| `--workflow-id` | 是 | 目标工作流 ID |
|
||||
| `--table-id` | 条件必填 | 记录类触发器绑定的数据表;必须与触发器配置一致 |
|
||||
| `--record-ids` | 条件必填 | 记录类触发器的记录 ID,1–5 个、逗号分隔且不可重复 |
|
||||
|
||||
`run` 启动真实异步执行,工作流中的发消息、写记录等动作会实际发生;执行前必须取得用户确认。返回项中的 `executionId` 是本次执行标识,可与 `workflow history` 项目的 `instanceId` 匹配。网络结果不确定时不要直接重复执行,先按该标识查询历史。
|
||||
|
||||
### workflow history — 查询执行历史
|
||||
|
||||
```bash
|
||||
dws aitable workflow history --base-id BASE_ID --workflow-id WORKFLOW_ID \
|
||||
--status failed --after-time 1786000000000 --before-time 1787000000000 \
|
||||
--page 0 --size 50
|
||||
```
|
||||
|
||||
| flag | 说明 |
|
||||
|------|------|
|
||||
| `--base-id` | 必填 |
|
||||
| `--workflow-id` | 必填;CLI 会映射为 MCP 的 `flowId` |
|
||||
| `--status` | 可选:`success` / `failed` / `running` / `break` / `untrigger` |
|
||||
| `--after-time` | 可选,Unix 毫秒开始时间 |
|
||||
| `--before-time` | 可选,Unix 毫秒结束时间;与 after-time 同传时必须更大 |
|
||||
| `--page` | 可选,从 0 开始,默认 0 |
|
||||
| `--size` | 可选,默认 20,范围 `[1, 100]` |
|
||||
|
||||
返回 `totalCount` 和 `list`。`running` 是非终态;`success`、`failed`、`break`、`untrigger` 是终态。
|
||||
|
||||
### workflow list — 列出工作流
|
||||
|
||||
```bash
|
||||
@@ -262,9 +304,9 @@ dws aitable workflow disable --base-id BASE_ID --workflow-id WORKFLOW_ID --yes -
|
||||
| 列出工作流 | ✅ |
|
||||
| 看工作流详情(含 flowSchema) | ✅ |
|
||||
| 启用/禁用 | ✅ |
|
||||
| 查看运行历史/执行日志 | ✅ `workflow history` |
|
||||
| 手动触发/单次运行 | ✅ `workflow run`(需确认) |
|
||||
| 删除工作流 | ❌ 暂未开放 |
|
||||
| 查看运行历史/执行日志 | ❌ 暂未开放 |
|
||||
| 手动触发/单次运行 | ❌ 暂未开放 |
|
||||
|
||||
## 错误码速查
|
||||
|
||||
@@ -338,4 +380,5 @@ done
|
||||
- create 不自动重试;update 仅对网络/5xx/`retryable:true` 瞬态错误自动重试。
|
||||
- enable / disable 出参里的 `enabled` / `disabled` 是 **动作确认 flag**,不是当前状态字段。要确认真生效请走 `workflow list` 查 `status`。
|
||||
- `workflow get` 的 `flowSchema` 结构随触发器/动作类型变化,不要假设固定字段。
|
||||
- 删除、运行历史和手动触发当前仍未开放。
|
||||
- `workflow run` 不自动重试;结果不确定时用 `workflow history` 按 executionId / instanceId 核对。
|
||||
- 删除工作流当前仍未开放。
|
||||
|
||||
@@ -148,14 +148,16 @@ dws chat +chat-messages --group "项目群" --page-all --format json \
|
||||
| 命令 | 用途 | 示例与要点 |
|
||||
|------|------|------------|
|
||||
| `message list` | 拉取指定群聊或单聊消息 | `dws chat message list --group <cid> --time "2025-03-01 00:00:00" --direction older`;目标三选一,`--direction newer/older` 优先于旧 `--forward` |
|
||||
| `message list-all` | 时间范围内全部会话消息 | `dws chat message list-all --start <ISO> --end <ISO> --limit 100 --cursor 0`;四个参数每次请求都传,翻页用 `nextCursor` |
|
||||
| `message list-by-sender` | 查指定发送者消息 | `--sender-user-id` 与 `--sender-open-dingtalk-id` 二选一,跨单聊+群聊 |
|
||||
| `message list-mentions` | 查 @ 我的消息 | 可传 `--group` 限定群,不传查全部 |
|
||||
| `message list-focused` | 查特别关注人消息 | 零参数可用,可加 `--limit` / `--cursor` |
|
||||
| `message list-all` | 时间范围内全部会话消息 | `dws chat message list-all --start <ISO> --end <ISO> --limit 100 --cursor 0`;默认一页,完整遍历加 `--page-all`,保留并合并 `result.conversationMessagesList` |
|
||||
| `message list-by-sender` | 查指定发送者消息 | `--sender-user-id` 与 `--sender-open-dingtalk-id` 二选一,跨单聊+群聊;完整遍历加 `--page-all`,同一会话跨页合并 messages |
|
||||
| `message list-mentions` | 查 @ 我的消息 | 可传 `--group` 限定群,不传查全部;完整遍历加 `--page-all`,同一会话跨页合并 messages |
|
||||
| `message list-focused` | 查特别关注人消息 | 零参数可用,可加 `--limit` / `--cursor`;完整遍历加 `--page-all`,cursor 为 int64 |
|
||||
| `message list-unread-conversations` | 未读会话列表 | 可选 `--count` |
|
||||
| `message list-topic-replies` | 拉取话题回复 | `--group <openConversationId> --topic-id <openConvThreadId>` |
|
||||
| `message list-by-ids` | 按消息 ID 批量查询 | `--msg-ids msgId1,msgId2`,最多 50 条 |
|
||||
|
||||
Typed `chat message` 自动翻页只由 `--page-all` 触发;只传 `--page-limit`、`--max-items` 或 `--page-delay` 仍保持默认单页 fallback。`conversationMessagesList` 结构会保留并按 `openConversationId` 合并 messages。分页元数据输出到顶层 `paging`,包含 `truncated`、`hasMore`、`lastCursor`、`pages`、`total`;非第一页失败时会输出 partial 结果和 `failedPage` / `failedCursor` / `pagesFetched` / `itemsFetched`。
|
||||
|
||||
`message list` 注意事项:
|
||||
|
||||
- `--group`、`--user`、`--open-dingtalk-id` 互斥且必须指定一个。
|
||||
@@ -194,6 +196,7 @@ dws chat message search-advanced --query "周报" --start "2026-04-01T00:00:00+0
|
||||
dws chat message search-advanced --user <userId> --start <ISO> --end <ISO>
|
||||
dws chat message search-advanced --at-me --start <ISO> --end <ISO>
|
||||
dws chat message search-advanced --conversation-ids <cid1>,<cid2> --query "合同" --limit 50 --cursor 0
|
||||
dws chat message search-advanced --query "周报" --start <ISO> --end <ISO> --page-all --max-items 200
|
||||
dws chat message search-advanced --message-type file --search-conv-type group_chat --query "附件"
|
||||
dws chat message search-advanced --only-robot-messages --query "通知"
|
||||
```
|
||||
@@ -210,12 +213,14 @@ dws chat message search-advanced --only-robot-messages --query "通知"
|
||||
| `--only-robot-messages` | 只搜索机器人消息 |
|
||||
| `--start` / `--end` | ISO-8601 时间范围 |
|
||||
| `--cursor` / `--limit` | 分页,翻页用 `nextCursor` |
|
||||
| `--page-all` / `--page-limit` / `--max-items` / `--page-delay` | 自动翻页;`--page-all` 是唯一触发开关 |
|
||||
|
||||
仅简单关键词搜索时可用:
|
||||
|
||||
```bash
|
||||
dws chat message search --query "changefree" --start <ISO> --end <ISO> --limit 50 --cursor 0
|
||||
dws chat message search --query "codereview" --group <openConversationId> --start <ISO> --end <ISO>
|
||||
dws chat message search --query "发布计划" --start <ISO> --end <ISO> --page-all --page-delay 0
|
||||
```
|
||||
|
||||
### 消息状态与撤回
|
||||
@@ -307,9 +312,10 @@ Runtime 会把创建响应的 `atTag` 自动放在正文前;不要自行写 ID
|
||||
| `message add-favorite` | 收藏消息 | `--open-message-id` `--open-conversation-id` |
|
||||
| `message remove-favorite` | 取消收藏消息 | `--open-message-id` `--open-conversation-id` |
|
||||
| `+flag-list` | 查询收藏消息列表的默认 Shortcut | 默认一页;要求全部时加 `--page-all`;`--page-size/--size` 范围为 1-30,可用 `--page-token` 或兼容的 `--cursor` 续页,检查 `complete` / `failures` |
|
||||
| `message list-favorites` | 原始单页收藏结果 fallback | 可选 `--cursor` `--size` |
|
||||
| `message list-favorites` | typed 收藏消息列表 | 默认一页;完整遍历加 `--page-all`,数字 cursor,聚合 `result.items`;可选 `--cursor` `--size` |
|
||||
|
||||
`+flag-list` 查询钉钉 message favorite,底层使用数字 cursor;它与消息 Pin、消息 Top 和会话置顶属于不同对象层级。
|
||||
`message list-favorites --page-all` 仍遵守 `--size` 1-30;`--page-limit` 只控制最多请求页数,`--max-items` 可精确截断输出。
|
||||
消息置顶 `set-top-msg` 与会话置顶 `chat set-top` 不同:前者置顶会话内消息,后者置顶整个会话。
|
||||
|
||||
### 表情回应
|
||||
|
||||
@@ -12,7 +12,7 @@
|
||||
<!-- DWS_MESSAGE_RESULT_CONTRACT_START -->
|
||||
- `version`: `im.message-list.v1`
|
||||
- `message_fields`: `messageId`, `conversationId`, `threadId`, `sender`, `senderId`, `senderType`, `messageType`, `text`, `createTime`, `updateTime`, `reactions`, `quotedMessage`, `forwarded`, `resourceRefs`
|
||||
- `envelope_fields`: `contractVersion`, `messages`, `count`, `resolvedFilters`, `queryRange`, `pagesFetched`, `paginationKnown`, `complete`, `hasMore`, `nextPage`, `stopReason`, `truncatedByPageLimit`, `truncatedByResultLimit`, `failedCount`, `failures`, `partial`, `resourceDownloads`
|
||||
- `envelope_fields`: `contractVersion`, `messages`, `count`, `resolvedFilters`, `queryRange`, `pagesFetched`, `paginationKnown`, `complete`, `hasMore`, `nextPage`, `stopReason`, `truncatedByPageLimit`, `truncatedByResultLimit`, `failedCount`, `failures`, `partial`, `scope`, `resourceDownloads`
|
||||
<!-- DWS_MESSAGE_RESULT_CONTRACT_END -->
|
||||
|
||||
当 `complete=false` 时不能称为全量成功。`nextPage` 只能来自真实 lower boundary;
|
||||
|
||||
@@ -66,9 +66,10 @@ dws minutes
|
||||
│ ├── create --file-name "..." --file-size <n> # 创建上传会话,获取上传地址
|
||||
│ ├── complete --session-id <sid> # 确认上传完成
|
||||
│ └── cancel --session-id <sid> # 取消上传会话
|
||||
├── permission <subcommand> # 权限管理(添加/移除听记成员权限)
|
||||
├── permission <subcommand> # 权限管理(添加/移除听记成员权限、为自己申请权限)
|
||||
│ ├── add --ids <uuid1,uuid2> --member-uids <uid1,uid2> --policy <0-4> [--cover] [--sub-resources "..."] # 添加成员权限
|
||||
│ └── remove --ids <uuid1,uuid2> --member-uids <uid1,uid2> # 移除成员权限
|
||||
│ ├── remove --ids <uuid1,uuid2> --member-uids <uid1,uid2> # 移除成员权限
|
||||
│ └── apply --id <uuid> --policy <2-4> # 为当前用户申请听记权限
|
||||
└── tag <subcommand> # 听记标签/分组管理(标签在听记页面手动创建)
|
||||
├── list # 查询我的听记标签/分组列表(返回 tagId 和标签名称)
|
||||
└── query --tag-id <tagId> [--limit] [--cursor] # 根据标签ID查询该标签下的听记列表
|
||||
@@ -417,7 +418,8 @@ dws minutes
|
||||
│ └── cancel --session-id <sid>
|
||||
├── permission <subcommand>
|
||||
│ ├── add --ids <uuid1,uuid2> --member-uids <uid1,uid2> --policy <0-4> [--cover] [--sub-resources "..."]
|
||||
│ └── remove --ids <uuid1,uuid2> --member-uids <uid1,uid2>
|
||||
│ ├── remove --ids <uuid1,uuid2> --member-uids <uid1,uid2>
|
||||
│ └── apply --id <uuid> --policy <2-4>
|
||||
└── tag <subcommand>
|
||||
├── list
|
||||
└── query --tag-id <tagId> [--limit] [--cursor]
|
||||
@@ -1324,6 +1326,38 @@ Flags:
|
||||
- 如果成员是听记的创建者(所有者),无法通过此命令移除其权限
|
||||
- 建议在移除前先确认成员的当前权限,避免误操作
|
||||
|
||||
### 为当前用户申请听记权限
|
||||
```
|
||||
Usage:
|
||||
dws minutes permission apply [flags]
|
||||
Example:
|
||||
dws minutes permission apply --id <taskUuid> --policy 4
|
||||
dws minutes permission apply --id <taskUuid> --policy 2
|
||||
Flags:
|
||||
--id string 听记 taskUuid (必填)
|
||||
--policy int 权限类型: 2=可编辑, 3=可查看/下载, 4=仅查看 (必填)
|
||||
```
|
||||
|
||||
为当前登录用户申请指定听记的权限,无需传入用户 ID,系统自动识别当前用户身份。
|
||||
适用于用户无权限访问某听记(如打开分享链接提示无权限)时,主动向听记所有者发起权限申请。
|
||||
|
||||
**权限类型说明:**
|
||||
|
||||
| --policy 值 | 含义 | 说明 |
|
||||
|------------|------|------|
|
||||
| 2 | 可编辑 | 可编辑听记的纪要、待办等内容 |
|
||||
| 3 | 可查看/下载 | 可查看和下载听记内容,不可编辑 |
|
||||
| 4 | 仅查看 | 仅可查看听记内容,不可下载 |
|
||||
|
||||
**与 permission add 的区别:**
|
||||
- `permission add` 是听记所有者/管理员**给别人授权**,需传 `--member-uids`
|
||||
- `permission apply` 是当前用户**为自己申请权限**,不需要任何用户 ID,不支持申请管理员/所有者权限(0/1)
|
||||
|
||||
**典型使用场景:**
|
||||
- 打开同事分享的听记链接提示无权限,申请查看权限
|
||||
- 需要编辑某篇只读听记的纪要,申请可编辑权限
|
||||
- 需要下载听记音频但当前仅有查看权限,申请可查看/下载权限
|
||||
|
||||
### 查询我的听记标签/分组列表
|
||||
```
|
||||
Usage:
|
||||
@@ -1708,6 +1742,7 @@ tagId 可通过 `dws minutes tag list` 获取。
|
||||
用户说"把某人加到这个听记中/共享听记给某人/给某人添加听记权限/把听记分享给同事/让某人也能看这个听记" → `permission add`
|
||||
用户说"添加参会者/批量添加参会者/添加成员/批量添加成员/加参会人/加入成员" → `permission add`(**注意:听记模块没有独立的"添加参会者/成员"命令,统一走权限接口**)
|
||||
用户说"移除某人的权限/取消共享/不让某人看这个听记了" → `permission remove`
|
||||
用户说"我没权限看这个听记/帮我申请这个听记的权限/申请查看权限/申请编辑权限" → `permission apply`(**注意:为当前用户自己申请,不是给别人授权**)
|
||||
|
||||
**自然语言示例:**
|
||||
- "把张三加到这个听记中"
|
||||
@@ -1724,6 +1759,9 @@ tagId 可通过 `dws minutes tag list` 获取。
|
||||
- "批量添加成员到这个听记"
|
||||
- "把张三加为参会人"
|
||||
- "移除张三对这个听记的权限"
|
||||
- "我打不开这个听记,帮我申请一下权限"
|
||||
- "帮我申请这个听记的查看权限"
|
||||
- "申请这篇听记的编辑权限"
|
||||
- "取消小王对这个听记的访问"
|
||||
- "不让某人看这个听记了"
|
||||
|
||||
@@ -1731,8 +1769,9 @@ tagId 可通过 `dws minutes tag list` 获取。
|
||||
- 用户提到"加/添加/共享/分享/让...看/让...访问"等增加权限语义 → `permission add`
|
||||
- 用户提到"添加参会者/加参会者/批量添加参会者/添加成员/加成员/批量添加成员/加参会人/添加参会人"等成员语义 → `permission add`(听记没有独立的"添加成员"接口,**统一走权限接口**)
|
||||
- 用户提到"移除/取消/删除/不让...看"等移除权限语义 → `permission remove`
|
||||
- 如果用户未指定权限类型,默认使用 `--policy 4`(可查看/下载/编辑)
|
||||
- 如果用户未提供 member-uids,需要先引导用户提供目标成员的钉钉 UID(可通过 `dws aisearch person --keyword "姓名" --dimension name` 查询)
|
||||
- 用户提到"我没权限/打不开/帮我申请/申请查看权限/申请编辑权限"等**为自己申请**语义 → `permission apply`(只需 `--id` + `--policy`;**不需要也不接受 `--member-uids`**,当前用户身份由系统自动识别;未说明权限等级时先与用户确认要 2(可编辑)/ 3(可查看下载)/ 4(仅查看))
|
||||
- `permission add` 的 `--policy` 是必填参数,没有默认值。用户未指定权限类型时,先确认要 0(管理员)/ 1(所有者)/ 2(可编辑)/ 3(可查看下载)/ 4(仅查看);即使确认选择 4,命令中仍必须显式传入 `--policy 4`
|
||||
- `permission add` / `permission remove` 需要目标成员的钉钉 UID:如果用户未提供 member-uids,需要先引导用户提供(可通过 `dws aisearch person --keyword "姓名" --dimension name` 查询);`permission apply` 不适用这一条
|
||||
|
||||
**典型执行链路:**
|
||||
1. 用户说"把张三加到这个听记中" → AI 需获取张三的 UID
|
||||
|
||||
@@ -674,6 +674,7 @@ func TestChangelogPRFastPathWorkflowContract(t *testing.T) {
|
||||
"filename === '.github/actionlint.yaml'",
|
||||
"filename.startsWith('scripts/')",
|
||||
"filename.startsWith('verify/')",
|
||||
"filename.startsWith('internal/helpers/')",
|
||||
"filename === 'test/fixtures/cli-interface-baseline.txt'",
|
||||
"filename.startsWith('internal/interfacesnapshot/')",
|
||||
"filename.startsWith('internal/cobracmd/')",
|
||||
|
||||
@@ -540,6 +540,36 @@ func TestCrossPlatformCoverageDocSkillReferenceLoadingBudget(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestMinutesPermissionAddRequiresExplicitPolicy(t *testing.T) {
|
||||
_, filename, _, ok := runtime.Caller(0)
|
||||
if !ok {
|
||||
t.Fatal("runtime.Caller(0) failed")
|
||||
}
|
||||
root := filepath.Clean(filepath.Join(filepath.Dir(filename), "..", ".."))
|
||||
paths := []string{
|
||||
filepath.Join(root, "skills", "mono", "references", "products", "minutes.md"),
|
||||
filepath.Join(root, "skills", "multi", "dingtalk-minutes", "references", "minutes.md"),
|
||||
}
|
||||
for _, path := range paths {
|
||||
content, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
t.Fatalf("read %s: %v", path, err)
|
||||
}
|
||||
text := string(content)
|
||||
for _, required := range []string{
|
||||
"`permission add` 的 `--policy` 是必填参数,没有默认值",
|
||||
"命令中仍必须显式传入 `--policy 4`",
|
||||
} {
|
||||
if !strings.Contains(text, required) {
|
||||
t.Errorf("%s missing permission add policy contract %q", path, required)
|
||||
}
|
||||
}
|
||||
if strings.Contains(text, "`permission add` 默认使用 `--policy 4`") {
|
||||
t.Errorf("%s still documents a nonexistent permission add policy default", path)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func hasAny(s string, needles []string) bool {
|
||||
for _, needle := range needles {
|
||||
if strings.Contains(s, needle) {
|
||||
|
||||
Reference in New Issue
Block a user