-
Notifications
You must be signed in to change notification settings - Fork 1.4k
feat(im): add opt-in normalized message list JSON #2586
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
xiaoxiangyu-123
wants to merge
4
commits into
main
Choose a base branch
from
codex/im-compact-json-output
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 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
8c28d49
feat(im)!: normalize message list JSON context
xiaoxiangyu-123 6006361
fix(im): make normalized message JSON opt-in
xiaoxiangyu-123 69690e4
fix(im): guard normalized context projection
xiaoxiangyu-123 8253922
test(im): remove unused lint waiver
xiaoxiangyu-123 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
Some comments aren't visible on the classic Files Changed page.
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
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,196 @@ | ||
| // Copyright (c) 2026 Lark Technologies Pte. Ltd. | ||
| // SPDX-License-Identifier: MIT | ||
|
|
||
| package im | ||
|
|
||
| import ( | ||
| "reflect" | ||
|
|
||
| "github.com/larksuite/cli/internal/output" | ||
| ) | ||
|
|
||
| func messageListOutputData( | ||
| runtimeFormat string, | ||
| jqExpr string, | ||
| messages []map[string]interface{}, | ||
| chatID string, | ||
| threadID string, | ||
| hasMore bool, | ||
| pageToken string, | ||
| ) map[string]interface{} { | ||
| legacy := map[string]interface{}{ | ||
| "messages": messages, | ||
| "total": len(messages), | ||
| "has_more": hasMore, | ||
| "page_token": pageToken, | ||
| } | ||
| if threadID != "" { | ||
| legacy["thread_id"] = threadID | ||
| } | ||
|
|
||
| // JQ always filters the JSON envelope. Unknown formats also fall back to | ||
| // JSON in Emitter.Success, so both paths must see the normalized shape. | ||
| if jqExpr != "" { | ||
| return compactMessageListData(messages, chatID, threadID, hasMore, pageToken) | ||
| } | ||
| if runtimeFormat == "pretty" { | ||
| return legacy | ||
| } | ||
| format, known := output.ParseFormat(runtimeFormat) | ||
| if !known || format == output.FormatJSON { | ||
| return compactMessageListData(messages, chatID, threadID, hasMore, pageToken) | ||
| } | ||
| return legacy | ||
| } | ||
|
|
||
| // compactMessageListData normalizes repeated conversation metadata for JSON | ||
| // output. It never mutates messages: the enriched message tree remains the | ||
| // source for human and record-oriented renderers. | ||
| func compactMessageListData( | ||
| messages []map[string]interface{}, | ||
| chatID string, | ||
| threadID string, | ||
| hasMore bool, | ||
| pageToken string, | ||
| ) map[string]interface{} { | ||
| if chatID == "" { | ||
| chatID = commonMessageString(messages, "chat_id") | ||
| } | ||
|
|
||
| participants, reusableSenders := compactParticipants(messages) | ||
| projected := make([]map[string]interface{}, 0, len(messages)) | ||
| for _, message := range messages { | ||
| projected = append(projected, compactMessage(message, chatID, threadID, reusableSenders)) | ||
| } | ||
|
|
||
| out := map[string]interface{}{ | ||
| "messages": projected, | ||
| "total": len(projected), | ||
| "has_more": hasMore, | ||
| "page_token": pageToken, | ||
| } | ||
| if chatID != "" { | ||
| out["chat_id"] = chatID | ||
| } | ||
| if threadID != "" { | ||
| out["thread_id"] = threadID | ||
| } | ||
| if len(participants) > 0 { | ||
| out["participants"] = participants | ||
| } | ||
| return out | ||
| } | ||
|
|
||
| // compactParticipants returns sender records that can be referenced without | ||
| // losing information. A sender id is reusable only when every occurrence has | ||
| // identical metadata; conflicting occurrences stay inline in compactMessage. | ||
| func compactParticipants(messages []map[string]interface{}) (map[string]map[string]interface{}, map[string]bool) { | ||
| originalByID := make(map[string]map[string]interface{}) | ||
| participants := make(map[string]map[string]interface{}) | ||
| reusable := make(map[string]bool) | ||
|
|
||
| walkMessageTree(messages, func(message map[string]interface{}) { | ||
| sender, ok := message["sender"].(map[string]interface{}) | ||
| if !ok { | ||
| return | ||
| } | ||
| id, _ := sender["id"].(string) | ||
| if id == "" { | ||
| return | ||
| } | ||
| if existing, seen := originalByID[id]; seen { | ||
| if !reflect.DeepEqual(existing, sender) { | ||
| reusable[id] = false | ||
| } | ||
| return | ||
| } | ||
| originalByID[id] = cloneStringMap(sender) | ||
| participant := cloneStringMap(sender) | ||
| delete(participant, "id") | ||
| participants[id] = participant | ||
| reusable[id] = true | ||
| }) | ||
|
|
||
| for id := range participants { | ||
| if !reusable[id] { | ||
| delete(participants, id) | ||
| } | ||
| } | ||
| return participants, reusable | ||
| } | ||
|
|
||
| func compactMessage(message map[string]interface{}, chatID string, threadID string, reusableSenders map[string]bool) map[string]interface{} { | ||
| out := cloneStringMap(message) | ||
| if messageChatID, _ := out["chat_id"].(string); chatID != "" && messageChatID == chatID { | ||
| delete(out, "chat_id") | ||
| } | ||
| if messageThreadID, _ := out["thread_id"].(string); threadID != "" && messageThreadID == threadID { | ||
| delete(out, "thread_id") | ||
| } | ||
| if sender, ok := out["sender"].(map[string]interface{}); ok { | ||
| if id, _ := sender["id"].(string); id != "" && reusableSenders[id] { | ||
| delete(out, "sender") | ||
| out["sender_id"] = id | ||
| } | ||
| } | ||
| if replies := messageSlice(out["thread_replies"]); replies != nil { | ||
| projected := make([]map[string]interface{}, 0, len(replies)) | ||
| for _, reply := range replies { | ||
| projected = append(projected, compactMessage(reply, chatID, threadID, reusableSenders)) | ||
| } | ||
| out["thread_replies"] = projected | ||
| } | ||
| return out | ||
| } | ||
|
|
||
| func commonMessageString(messages []map[string]interface{}, key string) string { | ||
| common := "" | ||
| conflict := false | ||
| walkMessageTree(messages, func(message map[string]interface{}) { | ||
| value, _ := message[key].(string) | ||
| if value == "" || conflict { | ||
| return | ||
|
coderabbitai[bot] marked this conversation as resolved.
Outdated
|
||
| } | ||
| if common == "" { | ||
| common = value | ||
| return | ||
| } | ||
| if common != value { | ||
| common = "" | ||
| conflict = true | ||
| } | ||
| }) | ||
| return common | ||
| } | ||
|
|
||
| func walkMessageTree(messages []map[string]interface{}, visit func(map[string]interface{})) { | ||
| for _, message := range messages { | ||
| visit(message) | ||
| walkMessageTree(messageSlice(message["thread_replies"]), visit) | ||
| } | ||
| } | ||
|
|
||
| func messageSlice(value interface{}) []map[string]interface{} { | ||
| switch items := value.(type) { | ||
| case []map[string]interface{}: | ||
| return items | ||
| case []interface{}: | ||
| out := make([]map[string]interface{}, 0, len(items)) | ||
| for _, item := range items { | ||
| if message, ok := item.(map[string]interface{}); ok { | ||
| out = append(out, message) | ||
| } | ||
| } | ||
| return out | ||
| default: | ||
| return nil | ||
| } | ||
| } | ||
|
|
||
| func cloneStringMap(source map[string]interface{}) map[string]interface{} { | ||
| out := make(map[string]interface{}, len(source)) | ||
| for key, value := range source { | ||
| out[key] = value | ||
| } | ||
| 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.
Uh oh!
There was an error while loading. Please reload this page.