{"record":{"id":"15be7bc37977617f","repo":"larksuite/cli","slug":"draft-has-no-unique-primary-body-part-use-replace","errorCode":null,"errorMessage":"draft has no unique primary body part; use replace_body with body_kind","messagePattern":"draft has no unique primary body part; use replace_body with body_kind","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"shortcuts/mail/draft/patch.go","lineNumber":301,"sourceCode":"// body edits — giving consistent mental model: attachments and signature\n// are draft-level concerns, not body content.\nfunc setBody(snapshot *DraftSnapshot, value string, options PatchOptions) error {\n\tvalue = autoPreserveSystemManagedRegions(snapshot, value)\n\tswitch {\n\tcase snapshot.PrimaryTextPartID != \"\" && snapshot.PrimaryHTMLPartID == \"\":\n\t\treturn replaceBody(snapshot, \"text/plain\", value, options)\n\tcase snapshot.PrimaryTextPartID == \"\" && snapshot.PrimaryHTMLPartID != \"\":\n\t\treturn replaceBody(snapshot, \"text/html\", value, options)\n\tcase snapshot.PrimaryTextPartID != \"\" && snapshot.PrimaryHTMLPartID != \"\":\n\t\tif err := coupledBodySetBodyInputError(snapshot, value); err != nil {\n\t\t\treturn err\n\t\t}\n\t\tif tryApplyCoupledBodySetBody(snapshot, value) {\n\t\t\treturn nil\n\t\t}\n\t\treturn fmt.Errorf(\"draft has both text/plain and text/html body parts, but they are not a supported summary+html pair\")\n\tdefault:\n\t\treturn fmt.Errorf(\"draft has no unique primary body part; use replace_body with body_kind\")\n\t}\n}\n\n// autoPreserveSystemManagedRegions extracts system-managed elements\n// (signature block and large attachment card) from the draft's old HTML\n// body and injects them into value (before any quote block in value, or\n// appended when no quote). Order is [sig][card], matching compose-time\n// layout [user][sig][card][quote].\n//\n// For each element, auto-injection is skipped when value's\n// user-authored region (before any quote block in value) already\n// contains that element — so users who explicitly reconstruct the body\n// with their own signature / card are respected. Elements inside a\n// quote block in value belong to the quoted original message and are\n// ignored for this check.\n//\n// No-op when the draft has no HTML body, or neither element exists in\n// the old body.","sourceCodeStart":283,"sourceCodeEnd":319,"githubUrl":"https://github.com/larksuite/cli/blob/7fd6ef3c07182257ce776cdc5a614e122d5bd4b3/shortcuts/mail/draft/patch.go#L283-L319","documentation":"set_body resolves a unique 'primary' body: text-only drafts edit the text part, HTML-only drafts edit the HTML part, and coupled pairs are handled together. When the draft has neither primary part (both IDs empty), there is no unique body to target, so set_body directs you to replace_body with an explicit body_kind (which can create the part when rewriting is allowed).","triggerScenarios":"A set_body patch op on a snapshot where both PrimaryTextPartID and PrimaryHTMLPartID are empty — e.g. a draft with no body part yet, or a body structure the parser could not map to a primary part.","commonSituations":"Drafts created empty or with only attachments; drafts whose MIME tree the snapshot parser does not recognize as a primary body; operating before any body was ever set.","solutions":["Use replace_body with body_kind set to text/plain or text/html and allow rewrite so the part is created","Verify the draft's MIME structure — set_body expects an existing primary body part","Set the body at draft creation time instead of patching an empty draft"],"exampleFix":"// before\napplyOp(Op{Op: \"set_body\", Value: \"hello\"}) // draft has no primary body part\n// after\napplyOp(Op{Op: \"replace_body\", BodyKind: \"text/html\", Value: \"<p>hello</p>\", RewriteEntireDraft: true})","handlingStrategy":"validation","validationCode":"if snap.PrimaryTextPartID == \"\" && snap.PrimaryHTMLPartID == \"\" {\n    // use replace_body with body_kind and rewrite enabled\n}","typeGuard":null,"tryCatchPattern":"if err := applyOp(op); err != nil && strings.Contains(err.Error(), \"no unique primary body part\") {\n    // retry with Op{Op: \"replace_body\", BodyKind: \"text/html\", RewriteEntireDraft: true}\n}","preventionTips":["Set the body when creating the draft rather than patching an empty one","Check primary part IDs before choosing set_body","Keep rewrite/allow-rewrite enabled when working with sparse drafts"],"tags":["email","draft-patch","mime"],"backgroundTag":"missing-body-part","analyzedSha":"7fd6ef3c07182257ce776cdc5a614e122d5bd4b3","analyzedAt":"2026-09-04T21:17:44.649Z","contentChangedAt":"2026-09-04T21:17:44.649Z","schemaVersion":2},"datasetVersion":"2026-09-12T02:17:10.037Z"}