| 5192 | } |
| 5193 | |
| 5194 | static char *sg_render_payload(bool json_format, const cbm_search_output_t *out, int offset, |
| 5195 | const char *const *fields, int requested_fields, cbm_store_t *store, |
| 5196 | const char *relationship, bool connected_requested, bool detail_ids, |
| 5197 | const sg_semantic_page_t *semantic, bool semantic_only, |
| 5198 | const char *diagnostic_hint, const sg_render_plan_t *plan, |
| 5199 | size_t max_output_bytes) { |
| 5200 | sg_render_plan_t meta_plan = *plan; |
| 5201 | int page_total = semantic_only ? semantic->total : out->total; |
| 5202 | int page_offset = semantic_only ? semantic->offset : offset; |
| 5203 | if (semantic_only) { |
| 5204 | meta_plan.returned = plan->semantic_returned; |
| 5205 | } |
| 5206 | bool continuation_supported = |
| 5207 | !semantic_only || !sg_semantic_engine_saturated(semantic, plan->semantic_returned); |
| 5208 | |
| 5209 | if (!json_format) { |
| 5210 | cbm_sb_t sb; |
| 5211 | cbm_sb_init(&sb); |
| 5212 | if (!semantic_only) { |
| 5213 | if (detail_ids) { |
| 5214 | emit_search_results_ids_tree(&sb, out, plan->returned); |
| 5215 | } else { |
| 5216 | emit_search_results_tree(&sb, out, fields, plan->fields_returned, store, |
| 5217 | relationship, plan->connected_returned, plan->returned); |
| 5218 | } |
| 5219 | } |
| 5220 | if (semantic->present) { |
| 5221 | sg_emit_semantic_paging_tree(&sb, semantic, plan->semantic_returned); |
| 5222 | emit_semantic_results_toon(&sb, semantic->results, plan->semantic_returned); |
| 5223 | } |
| 5224 | if (plan->core_hint_returned && diagnostic_hint) { |
| 5225 | cbm_tree_scalar_str(&sb, "hint", diagnostic_hint); |
| 5226 | } |
| 5227 | sg_emit_paging_tree(&sb, page_total, page_offset, &meta_plan, requested_fields, |
| 5228 | connected_requested, max_output_bytes, continuation_supported, |
| 5229 | semantic_only && semantic->limit == 0); |
| 5230 | return cbm_sb_finish(&sb); |
| 5231 | } |
| 5232 | |
| 5233 | yyjson_mut_doc *doc = yyjson_mut_doc_new(NULL); |
| 5234 | yyjson_mut_val *root = yyjson_mut_obj(doc); |
| 5235 | yyjson_mut_doc_set_root(doc, root); |
| 5236 | if (!semantic_only) { |
| 5237 | if (detail_ids) { |
| 5238 | emit_search_results_ids_json(doc, root, out, plan->returned); |
| 5239 | } else { |
| 5240 | emit_search_results_tree_json(doc, root, out, fields, plan->fields_returned, store, |
| 5241 | relationship, plan->connected_returned, plan->returned); |
| 5242 | } |
| 5243 | } else { |
| 5244 | /* The structured shape stays stable for semantic-only calls: an empty |
| 5245 | * groups array rather than an absent structural section. */ |
| 5246 | yyjson_mut_obj_add_val(doc, root, "groups", yyjson_mut_arr(doc)); |
| 5247 | } |
| 5248 | if (semantic->present) { |
| 5249 | sg_emit_semantic_paging_json(doc, root, semantic, plan->semantic_returned); |
| 5250 | emit_semantic_results(doc, root, semantic->results, plan->semantic_returned); |
| 5251 | } |
no test coverage detected