|
| 1 | +// Copyright (c) 2026 Lark Technologies Pte. Ltd. |
| 2 | +// SPDX-License-Identifier: MIT |
| 3 | + |
| 4 | +// Package note owns the Note domain: querying note detail and the unified |
| 5 | +// transcript by a known note_id. The vc domain locates a |
| 6 | +// note_id from meeting context and delegates note-detail parsing here, so the |
| 7 | +// parsing logic lives in exactly one place. |
| 8 | +package note |
| 9 | + |
| 10 | +import ( |
| 11 | + "context" |
| 12 | + "encoding/json" |
| 13 | + "fmt" |
| 14 | + "net/http" |
| 15 | + "strconv" |
| 16 | + "strings" |
| 17 | + |
| 18 | + "github.com/larksuite/cli/errs" |
| 19 | + "github.com/larksuite/cli/internal/validate" |
| 20 | + "github.com/larksuite/cli/shortcuts/common" |
| 21 | +) |
| 22 | + |
| 23 | +// NoNoteReadPermissionCode is returned when the caller lacks read permission |
| 24 | +// for the requested note. |
| 25 | +const NoNoteReadPermissionCode = 121005 |
| 26 | + |
| 27 | +// artifact_type enum from the note detail API. |
| 28 | +const ( |
| 29 | + artifactTypeMainDoc = 1 // main note document |
| 30 | + artifactTypeVerbatim = 2 // verbatim transcript |
| 31 | +) |
| 32 | + |
| 33 | +// note_display_type enum (i32) from the note detail API. Surfaced to callers as |
| 34 | +// a stable string so Agents route on a name, not a magic number. |
| 35 | +const ( |
| 36 | + displayTypeNormal = 1 |
| 37 | + displayTypeUnified = 2 |
| 38 | +) |
| 39 | + |
| 40 | +// Detail is the parsed note detail shared by `note +detail` and `vc +notes`. |
| 41 | +type Detail struct { |
| 42 | + NoteID string |
| 43 | + CreatorID string |
| 44 | + CreateTime string |
| 45 | + DisplayType string // unknown | normal | unified |
| 46 | + NoteDocToken string |
| 47 | + VerbatimDocToken string |
| 48 | + SharedDocTokens []string |
| 49 | +} |
| 50 | + |
| 51 | +// FetchDetail queries GET /open-apis/vc/v1/notes/{note_id} and parses the note |
| 52 | +// object. API errors are returned as typed errs.* values so callers can enrich |
| 53 | +// user guidance without downgrading the envelope. |
| 54 | +func FetchDetail(_ context.Context, runtime *common.RuntimeContext, noteID string) (*Detail, error) { |
| 55 | + data, err := runtime.DoAPIJSONTyped(http.MethodGet, fmt.Sprintf("/open-apis/vc/v1/notes/%s", validate.EncodePathSegment(noteID)), nil, nil) |
| 56 | + if err != nil { |
| 57 | + return nil, err |
| 58 | + } |
| 59 | + noteObj, _ := data["note"].(map[string]any) |
| 60 | + if noteObj == nil { |
| 61 | + return nil, errs.NewInternalError(errs.SubtypeInvalidResponse, "note detail is empty") |
| 62 | + } |
| 63 | + noteDoc, verbatimDoc := extractArtifactTokens(common.GetSlice(noteObj, "artifacts")) |
| 64 | + return &Detail{ |
| 65 | + NoteID: noteID, |
| 66 | + CreatorID: common.GetString(noteObj, "creator_id"), |
| 67 | + CreateTime: common.FormatTime(noteObj["create_time"]), |
| 68 | + DisplayType: displayTypeString(displayTypeValue(noteObj)), |
| 69 | + NoteDocToken: noteDoc, |
| 70 | + VerbatimDocToken: verbatimDoc, |
| 71 | + SharedDocTokens: extractDocTokens(common.GetSlice(noteObj, "references")), |
| 72 | + }, nil |
| 73 | +} |
| 74 | + |
| 75 | +// ToMap renders the detail as the field map consumed by `vc +notes`, keeping |
| 76 | +// the historical key set (shared_doc_tokens omitted when empty) and adding the |
| 77 | +// note_id / note_display_type fields. |
| 78 | +func (d *Detail) ToMap() map[string]any { |
| 79 | + m := map[string]any{ |
| 80 | + "note_id": d.NoteID, |
| 81 | + "note_display_type": d.DisplayType, |
| 82 | + "creator_id": d.CreatorID, |
| 83 | + "create_time": d.CreateTime, |
| 84 | + "note_doc_token": d.NoteDocToken, |
| 85 | + "verbatim_doc_token": d.VerbatimDocToken, |
| 86 | + } |
| 87 | + if len(d.SharedDocTokens) > 0 { |
| 88 | + m["shared_doc_tokens"] = d.SharedDocTokens |
| 89 | + } |
| 90 | + return m |
| 91 | +} |
| 92 | + |
| 93 | +// displayTypeValue reads the display-type field, tolerating either the |
| 94 | +// documented note_display_type key or a bare display_type fallback. |
| 95 | +func displayTypeValue(note map[string]any) any { |
| 96 | + if v, ok := note["note_display_type"]; ok { |
| 97 | + return v |
| 98 | + } |
| 99 | + return note["display_type"] |
| 100 | +} |
| 101 | + |
| 102 | +func displayTypeString(v any) string { |
| 103 | + switch parseLooseInt(v) { |
| 104 | + case displayTypeNormal: |
| 105 | + return "normal" |
| 106 | + case displayTypeUnified: |
| 107 | + return "unified" |
| 108 | + default: |
| 109 | + return "unknown" |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +// extractArtifactTokens picks main-doc and verbatim-doc tokens from artifacts. |
| 114 | +func extractArtifactTokens(artifacts []any) (noteDoc, verbatimDoc string) { |
| 115 | + for _, a := range artifacts { |
| 116 | + artifact, _ := a.(map[string]any) |
| 117 | + if artifact == nil { |
| 118 | + continue |
| 119 | + } |
| 120 | + docToken, _ := artifact["doc_token"].(string) |
| 121 | + switch parseLooseInt(artifact["artifact_type"]) { |
| 122 | + case artifactTypeMainDoc: |
| 123 | + noteDoc = docToken |
| 124 | + case artifactTypeVerbatim: |
| 125 | + verbatimDoc = docToken |
| 126 | + } |
| 127 | + } |
| 128 | + return |
| 129 | +} |
| 130 | + |
| 131 | +// extractDocTokens collects doc_token values from a list of reference objects. |
| 132 | +func extractDocTokens(refs []any) []string { |
| 133 | + var tokens []string |
| 134 | + for _, s := range refs { |
| 135 | + source, _ := s.(map[string]any) |
| 136 | + if source == nil { |
| 137 | + continue |
| 138 | + } |
| 139 | + if docToken, _ := source["doc_token"].(string); docToken != "" { |
| 140 | + tokens = append(tokens, docToken) |
| 141 | + } |
| 142 | + } |
| 143 | + return tokens |
| 144 | +} |
| 145 | + |
| 146 | +// parseLooseInt extracts an int from the varying JSON number representations |
| 147 | +// DoAPIJSON may yield (json.Number, float64, or int). |
| 148 | +func parseLooseInt(v any) int { |
| 149 | + switch n := v.(type) { |
| 150 | + case json.Number: |
| 151 | + i, _ := n.Int64() |
| 152 | + return int(i) |
| 153 | + case float64: |
| 154 | + // Reject fractional values: truncating 1.9 to 1 would silently coerce |
| 155 | + // a malformed enum into a valid one. |
| 156 | + if n != float64(int64(n)) { |
| 157 | + return 0 |
| 158 | + } |
| 159 | + return int(n) |
| 160 | + case int: |
| 161 | + return n |
| 162 | + default: |
| 163 | + return 0 |
| 164 | + } |
| 165 | +} |
| 166 | + |
| 167 | +// parseLooseCursorID extracts a positive cursor as a string. String cursors are |
| 168 | +// preferred because large JSON numbers lose precision when decoded into any. |
| 169 | +func parseLooseCursorID(v any) (string, bool) { |
| 170 | + switch n := v.(type) { |
| 171 | + case string: |
| 172 | + s := strings.TrimSpace(n) |
| 173 | + if s == "" || s == "0" { |
| 174 | + return "", false |
| 175 | + } |
| 176 | + return s, true |
| 177 | + case json.Number: |
| 178 | + i, err := n.Int64() |
| 179 | + if err != nil || i <= 0 { |
| 180 | + return "", false |
| 181 | + } |
| 182 | + return strconv.FormatInt(i, 10), true |
| 183 | + case float64: |
| 184 | + // encoding/json decodes numbers in map[string]any as float64. Accept |
| 185 | + // only values that can round-trip safely as an integer cursor. |
| 186 | + const maxSafeJSONInteger = 1<<53 - 1 |
| 187 | + if n <= 0 || n != float64(int64(n)) || n > maxSafeJSONInteger { |
| 188 | + return "", false |
| 189 | + } |
| 190 | + return strconv.FormatInt(int64(n), 10), true |
| 191 | + case int64: |
| 192 | + if n <= 0 { |
| 193 | + return "", false |
| 194 | + } |
| 195 | + return strconv.FormatInt(n, 10), true |
| 196 | + case int: |
| 197 | + if n <= 0 { |
| 198 | + return "", false |
| 199 | + } |
| 200 | + return strconv.Itoa(n), true |
| 201 | + default: |
| 202 | + return "", false |
| 203 | + } |
| 204 | +} |
0 commit comments