Skip to content

Commit 4de1859

Browse files
committed
fix docs fetch and update ergonomics
1 parent c0730b4 commit 4de1859

6 files changed

Lines changed: 223 additions & 9 deletions

File tree

shortcuts/doc/docs_fetch_v2.go

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,6 @@ func validateFetchV2(_ context.Context, runtime *common.RuntimeContext) error {
3838
return err
3939
}
4040
if _, err := parseDocumentRef(runtime.Str("doc")); err != nil {
41-
return errs.NewValidationError(errs.SubtypeInvalidArgument, "invalid --doc: %v", err).WithParam("--doc")
42-
}
43-
if err := validateFetchDetail(runtime); err != nil {
4441
return err
4542
}
4643
if err := validateReadModeFlags(runtime); err != nil {
@@ -71,6 +68,9 @@ func executeFetchV2(_ context.Context, runtime *common.RuntimeContext) error {
7168
if err != nil {
7269
return err
7370
}
71+
if warning := addFetchDetailDowngradeWarning(runtime, data); warning != "" && runtime.Format == "pretty" {
72+
fmt.Fprintf(runtime.IO().ErrOut, "warning: %s\n", warning)
73+
}
7474

7575
runtime.OutFormatRaw(data, nil, func(w io.Writer) {
7676
if doc, ok := data["document"].(map[string]interface{}); ok {
@@ -90,7 +90,7 @@ func buildFetchBody(runtime *common.RuntimeContext) map[string]interface{} {
9090
body["revision_id"] = v
9191
}
9292

93-
detail := runtime.Str("detail")
93+
detail := effectiveFetchDetail(runtime)
9494
switch detail {
9595
case "", "simple":
9696
body["export_option"] = map[string]interface{}{
@@ -146,17 +146,33 @@ func buildReadOption(runtime *common.RuntimeContext) map[string]interface{} {
146146
return ro
147147
}
148148

149-
// validateFetchDetail 非 xml 格式(markdown)不承载 block_id 与样式属性,拒绝 with-ids/full。
150-
func validateFetchDetail(runtime *common.RuntimeContext) error {
149+
// effectiveFetchDetail degrades detail options that cannot be represented by
150+
// non-XML exports. The original flag value is left intact so callers can still
151+
// surface an explicit warning in execute output.
152+
func effectiveFetchDetail(runtime *common.RuntimeContext) string {
151153
format := strings.TrimSpace(runtime.Str("doc-format"))
152154
detail := strings.TrimSpace(runtime.Str("detail"))
153155
if format == "" || format == "xml" {
154-
return nil
156+
return detail
155157
}
156158
if detail == "with-ids" || detail == "full" {
157-
return errs.NewValidationError(errs.SubtypeInvalidArgument, "--detail %s is only supported with --doc-format xml; %s output has no block ids, use --detail simple or switch to --doc-format xml", detail, format).WithParam("--detail")
159+
return "simple"
158160
}
159-
return nil
161+
return detail
162+
}
163+
164+
func addFetchDetailDowngradeWarning(runtime *common.RuntimeContext, data map[string]interface{}) string {
165+
format := strings.TrimSpace(runtime.Str("doc-format"))
166+
detail := strings.TrimSpace(runtime.Str("detail"))
167+
if format == "" || format == "xml" {
168+
return ""
169+
}
170+
if detail != "with-ids" && detail != "full" {
171+
return ""
172+
}
173+
warning := fmt.Sprintf("--detail %s is only supported with --doc-format xml; returning %s output and ignoring the unsupported detail option", detail, format)
174+
appendDocWarning(data, warning)
175+
return warning
160176
}
161177

162178
// validateReadModeFlags 客户端前置校验,服务端也会再校验一次。

shortcuts/doc/docs_fetch_v2_test.go

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,12 @@ package doc
55

66
import (
77
"context"
8+
"encoding/json"
89
"strings"
910
"testing"
1011

12+
"github.com/larksuite/cli/internal/cmdutil"
13+
"github.com/larksuite/cli/internal/httpmock"
1114
"github.com/larksuite/cli/shortcuts/common"
1215
"github.com/spf13/cobra"
1316
)
@@ -96,6 +99,126 @@ func TestDocsFetchAPIVersionV1StillUsesV2Endpoint(t *testing.T) {
9699
}
97100
}
98101

102+
func TestDocsFetchMarkdownDetailDowngradesToSimple(t *testing.T) {
103+
t.Parallel()
104+
105+
for _, detail := range []string{"with-ids", "full"} {
106+
t.Run(detail, func(t *testing.T) {
107+
t.Parallel()
108+
109+
runtime := newFetchShortcutTestRuntime(t, "", map[string]string{
110+
"doc-format": "markdown",
111+
"detail": detail,
112+
})
113+
if err := validateFetchV2(context.Background(), runtime); err != nil {
114+
t.Fatalf("validateFetchV2() error = %v", err)
115+
}
116+
117+
dry := decodeDocDryRun(t, DocsFetch.DryRun(context.Background(), runtime))
118+
exportOption, _ := dry.API[0].Body["export_option"].(map[string]interface{})
119+
if exportOption == nil {
120+
t.Fatalf("missing export_option: %#v", dry.API[0].Body)
121+
}
122+
if got := exportOption["export_block_id"]; got != false {
123+
t.Fatalf("export_block_id = %#v, want false after markdown detail downgrade", got)
124+
}
125+
if got := exportOption["export_style_attrs"]; got != false {
126+
t.Fatalf("export_style_attrs = %#v, want false after markdown detail downgrade", got)
127+
}
128+
if got := exportOption["export_cite_extra_data"]; got != false {
129+
t.Fatalf("export_cite_extra_data = %#v, want false after markdown detail downgrade", got)
130+
}
131+
})
132+
}
133+
}
134+
135+
func TestDocsFetchMarkdownDetailDowngradeWarnsInOutput(t *testing.T) {
136+
t.Setenv("LARKSUITE_CLI_CONFIG_DIR", t.TempDir())
137+
138+
f, stdout, _, reg := cmdutil.TestFactory(t, docsTestConfigWithAppID("docs-fetch-detail-warning"))
139+
reg.Register(&httpmock.Stub{
140+
Method: "POST",
141+
URL: "/open-apis/docs_ai/v1/documents/doxcnFetchWarning/fetch",
142+
Body: map[string]interface{}{
143+
"code": 0,
144+
"msg": "ok",
145+
"data": map[string]interface{}{
146+
"document": map[string]interface{}{
147+
"document_id": "doxcnFetchWarning",
148+
"revision_id": float64(1),
149+
"content": "# hello",
150+
},
151+
},
152+
},
153+
})
154+
155+
err := mountAndRunDocs(t, DocsFetch, []string{
156+
"+fetch",
157+
"--doc", "doxcnFetchWarning",
158+
"--doc-format", "markdown",
159+
"--detail", "with-ids",
160+
"--as", "bot",
161+
}, f, stdout)
162+
if err != nil {
163+
t.Fatalf("unexpected error: %v", err)
164+
}
165+
166+
var envelope map[string]interface{}
167+
if err := json.Unmarshal(stdout.Bytes(), &envelope); err != nil {
168+
t.Fatalf("decode output: %v\nraw=%s", err, stdout.String())
169+
}
170+
data, _ := envelope["data"].(map[string]interface{})
171+
warnings, _ := data["warnings"].([]interface{})
172+
if len(warnings) != 1 {
173+
t.Fatalf("warnings = %#v, want one downgrade warning", data["warnings"])
174+
}
175+
if got, _ := warnings[0].(string); !strings.Contains(got, "returning markdown output") || !strings.Contains(got, "ignoring the unsupported detail option") {
176+
t.Fatalf("unexpected warning: %q", got)
177+
}
178+
}
179+
180+
func TestDocsFetchMarkdownDetailDowngradeWarnsInPrettyOutput(t *testing.T) {
181+
t.Setenv("LARKSUITE_CLI_CONFIG_DIR", t.TempDir())
182+
183+
f, stdout, stderr, reg := cmdutil.TestFactory(t, docsTestConfigWithAppID("docs-fetch-detail-pretty-warning"))
184+
reg.Register(&httpmock.Stub{
185+
Method: "POST",
186+
URL: "/open-apis/docs_ai/v1/documents/doxcnFetchPrettyWarning/fetch",
187+
Body: map[string]interface{}{
188+
"code": 0,
189+
"msg": "ok",
190+
"data": map[string]interface{}{
191+
"document": map[string]interface{}{
192+
"document_id": "doxcnFetchPrettyWarning",
193+
"revision_id": float64(1),
194+
"content": "# hello",
195+
},
196+
},
197+
},
198+
})
199+
200+
err := mountAndRunDocs(t, DocsFetch, []string{
201+
"+fetch",
202+
"--doc", "doxcnFetchPrettyWarning",
203+
"--doc-format", "markdown",
204+
"--detail", "full",
205+
"--format", "pretty",
206+
"--as", "bot",
207+
}, f, stdout)
208+
if err != nil {
209+
t.Fatalf("unexpected error: %v", err)
210+
}
211+
212+
if got := stdout.String(); got != "# hello\n" {
213+
t.Fatalf("stdout = %q, want markdown content only", got)
214+
}
215+
if got := stderr.String(); !strings.Contains(got, "warning: --detail full is only supported with --doc-format xml") ||
216+
!strings.Contains(got, "returning markdown output") ||
217+
!strings.Contains(got, "ignoring the unsupported detail option") {
218+
t.Fatalf("stderr missing downgrade warning: %q", got)
219+
}
220+
}
221+
99222
func TestDocsFetchRejectsLegacyFlags(t *testing.T) {
100223
tests := []struct {
101224
name string

shortcuts/doc/helpers.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,3 +91,22 @@ func buildDriveRouteExtra(docID string) (string, error) {
9191
}
9292
return string(extra), nil
9393
}
94+
95+
func appendDocWarning(data map[string]interface{}, warning string) {
96+
if data == nil {
97+
return
98+
}
99+
if strings.TrimSpace(warning) == "" {
100+
return
101+
}
102+
switch existing := data["warnings"].(type) {
103+
case []interface{}:
104+
data["warnings"] = append(existing, warning)
105+
case []string:
106+
data["warnings"] = append(existing, warning)
107+
case nil:
108+
data["warnings"] = []string{warning}
109+
default:
110+
data["warnings"] = []interface{}{existing, warning}
111+
}
112+
}

shortcuts/doc/helpers_test.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
package doc
55

66
import (
7+
"reflect"
78
"strings"
89
"testing"
910
)
@@ -88,3 +89,51 @@ func TestBuildDriveRouteExtraEscapesJSON(t *testing.T) {
8889
t.Fatalf("buildDriveRouteExtra() = %q, want %q", got, want)
8990
}
9091
}
92+
93+
func TestAppendDocWarning(t *testing.T) {
94+
t.Parallel()
95+
96+
appendDocWarning(nil, "ignored")
97+
98+
empty := map[string]interface{}{}
99+
appendDocWarning(empty, " ")
100+
if _, ok := empty["warnings"]; ok {
101+
t.Fatalf("blank warning should be ignored: %#v", empty)
102+
}
103+
104+
tests := []struct {
105+
name string
106+
data map[string]interface{}
107+
want interface{}
108+
}{
109+
{
110+
name: "missing warnings",
111+
data: map[string]interface{}{},
112+
want: []string{"new warning"},
113+
},
114+
{
115+
name: "string slice warnings",
116+
data: map[string]interface{}{"warnings": []string{"old warning"}},
117+
want: []string{"old warning", "new warning"},
118+
},
119+
{
120+
name: "interface slice warnings",
121+
data: map[string]interface{}{"warnings": []interface{}{"old warning"}},
122+
want: []interface{}{"old warning", "new warning"},
123+
},
124+
{
125+
name: "scalar warning",
126+
data: map[string]interface{}{"warnings": "old warning"},
127+
want: []interface{}{"old warning", "new warning"},
128+
},
129+
}
130+
131+
for _, tt := range tests {
132+
t.Run(tt.name, func(t *testing.T) {
133+
appendDocWarning(tt.data, "new warning")
134+
if got := tt.data["warnings"]; !reflect.DeepEqual(got, tt.want) {
135+
t.Fatalf("warnings = %#v, want %#v", got, tt.want)
136+
}
137+
})
138+
}
139+
}

skills/lark-doc/references/lark-doc-update.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,8 @@ lark-cli docs +update --api-version v2 --doc "<doc_id>" --command block_replace
113113
--content '<p>替换后的段落内容</p>'
114114
```
115115

116+
> `block_replace` 由服务端执行整块替换,目标 block 的 ID 不保证在替换后继续可用。后续如果还要在替换后的块附近继续 `block_insert_after``range` 或其他 block 级操作,先重新 `docs +fetch --detail with-ids` 获取最新 block ID,不要复用旧 ID。
117+
116118
### block_delete — 删除指定 block
117119

118120
```bash
@@ -234,6 +236,7 @@ lark-cli docs +update --api-version v2 --doc "<doc_id>" --command str_replace \
234236
- **保护不可重建的内容**:图片、画板、电子表格等以 token 形式存储,替换时避开这些 block
235237
- **str_replace 的 replacement 支持富文本**:可以用行内标签 `<b>``<a>``<cite>``<latex>` 等替换普通文本为富文本
236238
- **同一 block 只能被 replace 一次**:多次修改同一 block 请合并为一次 block_replace
239+
- **block_replace 后重新获取 ID**`block_replace` 成功后旧 block ID 不保证继续可用;继续做相邻块操作前,重新 `docs +fetch --detail with-ids`
237240
- **block_delete 支持批量**:用逗号分隔多个 block_id 一次删除
238241
- **复杂结构重组**:将多个段落转换为 grid / table 等复杂布局时,分步操作比 overwrite 更安全:
239242
1.`block_insert_after` 在目标位置插入新的富文本结构

skills/lark-doc/references/lark-doc-xml.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,10 @@ p, h1-h9, ul, ol, li, table, thead, tbody, tr, th, td, blockquote, pre, code, hr
7777
</ul>
7878
```
7979

80+
## 代码块
81+
- 代码块必须写成 `<pre lang="xxx" caption="可选说明"><code>代码内容</code></pre>`
82+
- 不要将代码文本直接放在 `<pre>` 下;应放在内层 `<code>` 中。
83+
8084

8185
## 用户名写入规则
8286

0 commit comments

Comments
 (0)