-
Notifications
You must be signed in to change notification settings - Fork 667
feat(lingo): add Feishu dictionary shortcuts and skill #855
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
emac
wants to merge
3
commits into
larksuite:main
Choose a base branch
from
emac:feat/lingo
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| // Copyright (c) 2026 Lark Technologies Pte. Ltd. | ||
| // SPDX-License-Identifier: MIT | ||
|
|
||
| package lingo | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "encoding/json" | ||
| "strings" | ||
| "testing" | ||
|
|
||
| "github.com/spf13/cobra" | ||
|
|
||
| "github.com/larksuite/cli/internal/cmdutil" | ||
| "github.com/larksuite/cli/internal/core" | ||
| "github.com/larksuite/cli/shortcuts/common" | ||
| ) | ||
|
|
||
| // lingoTestConfig builds an isolated CliConfig per test so cached state | ||
| // (tokens, profile) cannot leak across parallel tests. | ||
| func lingoTestConfig(t *testing.T) *core.CliConfig { | ||
| t.Helper() | ||
| suffix := strings.NewReplacer("/", "-", " ", "-").Replace(strings.ToLower(t.Name())) | ||
| return &core.CliConfig{ | ||
| AppID: "test-lingo-" + suffix, | ||
| AppSecret: "secret-lingo-" + suffix, | ||
| Brand: core.BrandFeishu, | ||
| } | ||
| } | ||
|
|
||
| // runLingoShortcut mounts the given shortcut under a fresh "lingo" parent | ||
| // and executes it with the given args. | ||
| func runLingoShortcut(t *testing.T, s common.Shortcut, f *cmdutil.Factory, stdout *bytes.Buffer, args []string) error { | ||
| t.Helper() | ||
| parent := &cobra.Command{Use: "lingo"} | ||
| s.Mount(parent, f) | ||
| parent.SetArgs(args) | ||
| parent.SilenceErrors = true | ||
| parent.SilenceUsage = true | ||
| if stdout != nil { | ||
| stdout.Reset() | ||
| } | ||
| return parent.Execute() | ||
| } | ||
|
|
||
| // decodeEnvelope parses the success envelope and returns the data field. | ||
| func decodeEnvelope(t *testing.T, stdout *bytes.Buffer) map[string]interface{} { | ||
| t.Helper() | ||
| var envelope map[string]interface{} | ||
| if err := json.Unmarshal(stdout.Bytes(), &envelope); err != nil { | ||
| t.Fatalf("failed to decode output: %v\nraw=%s", err, stdout.String()) | ||
| } | ||
| data, _ := envelope["data"].(map[string]interface{}) | ||
| if data == nil { | ||
| t.Fatalf("missing data in output envelope: %#v", envelope) | ||
| } | ||
| return data | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,185 @@ | ||
| // Copyright (c) 2026 Lark Technologies Pte. Ltd. | ||
| // SPDX-License-Identifier: MIT | ||
|
|
||
| package lingo | ||
|
|
||
| import ( | ||
| "context" | ||
| "encoding/json" | ||
| "fmt" | ||
| "io" | ||
| "strings" | ||
|
|
||
| "github.com/larksuite/cli/internal/validate" | ||
| "github.com/larksuite/cli/shortcuts/common" | ||
| larkcore "github.com/larksuite/oapi-sdk-go/v3/core" | ||
| ) | ||
|
|
||
| // LingoEntityCreate creates a new dictionary entry. | ||
| // Entries created via this API default to the review queue unless the | ||
| // app has been granted baike:entity:exempt_review. | ||
| // | ||
| // Accepts either a plain-text description (--description) or an HTML | ||
| // rich-text body (--rich-text); the two flags are mutually exclusive. | ||
| // Optional structured metadata (classifications, related users/chats/docs/ | ||
| // links/images/oncalls, abbreviation cross-links) can be attached via | ||
| // --related-meta as a JSON object. | ||
| var LingoEntityCreate = common.Shortcut{ | ||
| Service: "lingo", | ||
| Command: "+create", | ||
| Description: "Create a dictionary entry (enters review queue by default; supports --description/--rich-text and --related-meta)", | ||
| Risk: "write", | ||
| Scopes: []string{"baike:entity"}, | ||
| AuthTypes: []string{"user", "bot"}, | ||
| HasFormat: true, | ||
| Flags: []common.Flag{ | ||
| {Name: "main-key", Desc: "main key (required, e.g. \"飞书\")", Required: true}, | ||
| {Name: "aliases", Desc: "comma-separated alias list (optional)"}, | ||
| {Name: "description", Desc: "plain-text description (mutually exclusive with --rich-text)", Input: []string{common.File, common.Stdin}}, | ||
| {Name: "rich-text", Desc: "HTML rich-text description, e.g. <p><b>主词</b><span>释义</span></p> (mutually exclusive with --description)", Input: []string{common.File, common.Stdin}}, | ||
| {Name: "related-meta", Desc: "related metadata as JSON object: classifications/abbreviations/users/chats/docs/links/oncalls/images (supports @file or stdin)", Input: []string{common.File, common.Stdin}}, | ||
| {Name: "repo-id", Desc: "dictionary repo ID; empty = shared company dictionary"}, | ||
| {Name: "allow-highlight", Type: "bool", Default: "true", Desc: "whether the entry is highlighted in documents"}, | ||
| {Name: "allow-search", Type: "bool", Default: "true", Desc: "whether the entry participates in search"}, | ||
| }, | ||
| Validate: func(ctx context.Context, runtime *common.RuntimeContext) error { | ||
| mainKey := strings.TrimSpace(runtime.Str("main-key")) | ||
| if mainKey == "" { | ||
| return common.FlagErrorf("--main-key cannot be empty") | ||
| } | ||
| if err := validate.RejectControlChars(mainKey, "main-key"); err != nil { | ||
| return err | ||
| } | ||
| if v := runtime.Str("aliases"); v != "" { | ||
| if err := validate.RejectControlChars(v, "aliases"); err != nil { | ||
| return err | ||
| } | ||
| } | ||
| desc := runtime.Str("description") | ||
| rich := runtime.Str("rich-text") | ||
| if desc != "" && rich != "" { | ||
| return common.FlagErrorf("--description and --rich-text are mutually exclusive") | ||
| } | ||
| if desc != "" { | ||
| if err := validate.RejectControlChars(desc, "description"); err != nil { | ||
| return err | ||
| } | ||
| } | ||
| if rich != "" { | ||
| if err := validate.RejectControlChars(rich, "rich-text"); err != nil { | ||
| return err | ||
| } | ||
| } | ||
| if v := runtime.Str("related-meta"); v != "" { | ||
| if _, err := parseRelatedMeta(v); err != nil { | ||
| return err | ||
| } | ||
| } | ||
| if v := runtime.Str("repo-id"); v != "" { | ||
| if err := validate.RejectControlChars(v, "repo-id"); err != nil { | ||
| return err | ||
| } | ||
| } | ||
| return nil | ||
| }, | ||
| DryRun: func(ctx context.Context, runtime *common.RuntimeContext) *common.DryRunAPI { | ||
| body := buildCreateBody(runtime) | ||
| return common.NewDryRunAPI(). | ||
| POST("/open-apis/lingo/v1/entities"). | ||
| Body(body). | ||
| Desc("Create dictionary entry") | ||
| }, | ||
| Execute: func(ctx context.Context, runtime *common.RuntimeContext) error { | ||
| body := buildCreateBody(runtime) | ||
| data, err := runtime.DoAPIJSON("POST", "/open-apis/lingo/v1/entities", larkcore.QueryParams{}, body) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| runtime.OutFormat(data, nil, func(w io.Writer) { | ||
| entity, _ := data["entity"].(map[string]interface{}) | ||
| if entity == nil { | ||
| fmt.Fprintln(w, "Created (no entity echoed)") | ||
| return | ||
| } | ||
| id, _ := entity["id"].(string) | ||
| fmt.Fprintf(w, "Created entity [%s] %s\n", id, mainKeyText(entity)) | ||
| fmt.Fprintln(w, " (entries enter review queue unless app has baike:entity:exempt_review)") | ||
| }) | ||
| return nil | ||
| }, | ||
| } | ||
|
|
||
| // buildCreateBody assembles the create request body from flags. | ||
| func buildCreateBody(runtime *common.RuntimeContext) map[string]interface{} { | ||
| display := map[string]interface{}{ | ||
| "allow_highlight": runtime.Bool("allow-highlight"), | ||
| "allow_search": runtime.Bool("allow-search"), | ||
| } | ||
|
|
||
| mainKey := map[string]interface{}{ | ||
| "key": runtime.Str("main-key"), | ||
| "display_status": display, | ||
| } | ||
|
|
||
| body := map[string]interface{}{ | ||
| "main_keys": []map[string]interface{}{mainKey}, | ||
| } | ||
|
|
||
| if aliasStr := runtime.Str("aliases"); aliasStr != "" { | ||
| aliases := splitAliases(aliasStr, display) | ||
| if len(aliases) > 0 { | ||
| body["aliases"] = aliases | ||
| } | ||
| } | ||
|
|
||
| if desc := runtime.Str("description"); desc != "" { | ||
| body["description"] = desc | ||
| } else if rich := runtime.Str("rich-text"); rich != "" { | ||
| body["rich_text"] = rich | ||
| } | ||
|
|
||
| if rm := runtime.Str("related-meta"); rm != "" { | ||
| // Validate already ran; ignore error here. | ||
| if parsed, err := parseRelatedMeta(rm); err == nil { | ||
| body["related_meta"] = parsed | ||
| } | ||
| } | ||
|
|
||
| if repo := runtime.Str("repo-id"); repo != "" { | ||
| body["repo_id"] = repo | ||
| } | ||
|
|
||
| return body | ||
| } | ||
|
|
||
| // parseRelatedMeta unmarshals the --related-meta flag value into a JSON object. | ||
| // Returns an actionable error if the input is not a valid JSON object. | ||
| func parseRelatedMeta(raw string) (map[string]interface{}, error) { | ||
| var out map[string]interface{} | ||
| if err := json.Unmarshal([]byte(raw), &out); err != nil { | ||
| return nil, common.FlagErrorf("--related-meta must be a JSON object: %s", err) | ||
| } | ||
| if out == nil { | ||
| return nil, common.FlagErrorf("--related-meta must be a JSON object, got null") | ||
| } | ||
| return out, nil | ||
| } | ||
|
|
||
| // splitAliases splits a comma-separated alias list and pairs each with the display_status block. | ||
| // Empty values (after trim) are skipped. | ||
| func splitAliases(s string, display map[string]interface{}) []map[string]interface{} { | ||
| parts := strings.Split(s, ",") | ||
| out := make([]map[string]interface{}, 0, len(parts)) | ||
| for _, p := range parts { | ||
| k := strings.TrimSpace(p) | ||
| if k == "" { | ||
| continue | ||
| } | ||
| out = append(out, map[string]interface{}{ | ||
| "key": k, | ||
| "display_status": display, | ||
| }) | ||
| } | ||
| return out | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Restrict
+createto bot auth only.Line 33 currently allows
user, but this write endpoint is bot-only in the behavior documented for this feature and should be blocked at CLI preflight.Suggested fix
📝 Committable suggestion
🤖 Prompt for AI Agents