| 5941 | } |
| 5942 | |
| 5943 | static char *query_graph_json_response_text(const cbm_cypher_result_t *result, int row_offset, |
| 5944 | int row_count, int visible_count, bool exact_total, |
| 5945 | const query_graph_cursor_context_t *cursor_context) { |
| 5946 | bool budget_hit = row_count < visible_count; |
| 5947 | bool materialized_more = row_offset + row_count < result->row_count; |
| 5948 | bool page_limit_hit = !budget_hit && materialized_more; |
| 5949 | bool truncated = budget_hit || materialized_more || result->truncated; |
| 5950 | |
| 5951 | yyjson_mut_doc *doc = yyjson_mut_doc_new(NULL); |
| 5952 | yyjson_mut_val *root = yyjson_mut_obj(doc); |
| 5953 | yyjson_mut_doc_set_root(doc, root); |
| 5954 | yyjson_mut_val *cols = yyjson_mut_arr(doc); |
| 5955 | for (int i = 0; i < result->col_count; i++) { |
| 5956 | yyjson_mut_arr_add_str(doc, cols, result->columns[i]); |
| 5957 | } |
| 5958 | yyjson_mut_obj_add_val(doc, root, "columns", cols); |
| 5959 | yyjson_mut_val *rows = yyjson_mut_arr(doc); |
| 5960 | for (int row_index = 0; row_index < row_count; row_index++) { |
| 5961 | yyjson_mut_val *row = yyjson_mut_arr(doc); |
| 5962 | for (int col = 0; col < result->col_count; col++) { |
| 5963 | yyjson_mut_arr_add_str(doc, row, result->rows[row_offset + row_index][col]); |
| 5964 | } |
| 5965 | yyjson_mut_arr_add_val(rows, row); |
| 5966 | } |
| 5967 | yyjson_mut_obj_add_val(doc, root, "rows", rows); |
| 5968 | yyjson_mut_obj_add_int(doc, root, "returned", row_count); |
| 5969 | yyjson_mut_obj_add_int(doc, root, "total", result->row_count); |
| 5970 | yyjson_mut_obj_add_str(doc, root, "total_relation", exact_total ? "eq" : "gte"); |
| 5971 | if (row_offset > 0) { |
| 5972 | yyjson_mut_obj_add_int(doc, root, "offset", row_offset); |
| 5973 | } |
| 5974 | yyjson_mut_obj_add_bool(doc, root, "has_more", truncated); |
| 5975 | yyjson_mut_obj_add_bool(doc, root, "truncated", truncated); |
| 5976 | const char *reason = query_graph_truncation_reason(result, budget_hit, page_limit_hit); |
| 5977 | if (reason) { |
| 5978 | yyjson_mut_obj_add_str(doc, root, "truncation_reason", reason); |
| 5979 | } |
| 5980 | if (row_count > 0 && materialized_more) { |
| 5981 | char next_cursor[QUERY_GRAPH_CURSOR_TOKEN_CAP]; |
| 5982 | if (query_graph_cursor_encode(cursor_context, row_offset + row_count, next_cursor)) { |
| 5983 | yyjson_mut_obj_add_strcpy(doc, root, "next_cursor", next_cursor); |
| 5984 | } |
| 5985 | yyjson_mut_obj_add_int(doc, root, "next_offset", row_offset + row_count); |
| 5986 | } else if (budget_hit && row_count == 0) { |
| 5987 | yyjson_mut_obj_add_str(doc, root, "hint", |
| 5988 | "First row exceeds output budget; raise max_output_tokens."); |
| 5989 | } |
| 5990 | if (result->warning) { |
| 5991 | yyjson_mut_obj_add_str(doc, root, "warning", result->warning); |
| 5992 | } |
| 5993 | if (result->row_count == 0 && !result->truncated) { |
| 5994 | yyjson_mut_obj_add_str( |
| 5995 | doc, root, "hint", |
| 5996 | "Query returned no results. Use get_graph_schema() to see available labels and " |
| 5997 | "edge types."); |
| 5998 | } |
| 5999 | char *json = yy_doc_to_str(doc); |
| 6000 | yyjson_mut_doc_free(doc); |
no test coverage detected