Module: RailsAiContext::JsonBudget
- Defined in:
- lib/rails_ai_context/json_budget.rb
Overview
Fits a data structure into a character budget and serializes it as JSON that always parses.
A tool response is markdown and can be cut anywhere, so BaseTool#text_response slices the string. Resource payloads are served under an application/json mime type, so the same trick ends the body mid-object and JSON.parse raises. The budget is applied to the DATA instead: whole elements are dropped until what remains fits, and what went missing is reported under a reserved "_truncated" key. Values are otherwise served exact, except an over-long string, which is cut at a marker and counted in the report.
Defined Under Namespace
Classes: Taken
Constant Summary collapse
- TRUNCATION_KEY =
Reserved key carrying the reduction report on an oversized payload, and the marker left inside a string value that had to be cut.
"_truncated"- TRUNCATION_REASON =
"response_size_cap"- STRING_CLIP_MARKER =
"... (truncated)"- TRUNCATION_HINT =
"Whole elements were dropped and an over-long string value may be cut at a " \ "\"#{STRING_CLIP_MARKER}\" marker; nothing else was altered. " \ "Use the rails_get_* tools to page through what is missing."
- MAX_REDUCE_DEPTH =
How deep the reducer descends before it empties a container instead of recursing. Introspection payloads bottom out far above this; the cap is here so a pathological structure cannot drive unbounded recursion.
16- NOTE_RESERVE =
Chars held back from the data budget for the report, how many clipped paths it names, and how many refits are allowed when a report outgrows the reserve. The reserve is capped at a quarter of the budget so a small budget still leaves room for data.
2_048- MAX_CLIP_RECORDS =
12- MAX_FIT_ATTEMPTS =
3- NOTE_ATTACH_OVERHEAD =
Chars it costs to attach the report to a fitted payload, whichever shape with_note ends up using. Slack, not a tight bound.
24- MINIMUM_TRUNCATION_JSON =
Smallest valid JSON that still says the payload was cut. Emitted only when the budget is below the size of a usable report.
%({"#{TRUNCATION_KEY}":true})
Class Method Summary collapse
-
.for_resource(data) ⇒ Object
Resource payloads ride a single JSON-RPC frame with no transport-level size guard, so they get the same char cap tool responses do: a huge schema or routing table must not produce an unbounded payload.
-
.generate(data, max) ⇒ Object
Pretty JSON when the payload fits, a reduced compact document when it does not.
Class Method Details
.for_resource(data) ⇒ Object
Resource payloads ride a single JSON-RPC frame with no transport-level size guard, so they get the same char cap tool responses do: a huge schema or routing table must not produce an unbounded payload.
56 57 58 |
# File 'lib/rails_ai_context/json_budget.rb', line 56 def for_resource(data) generate(data, RailsAiContext.configuration.max_tool_response_chars) end |
.generate(data, max) ⇒ Object
Pretty JSON when the payload fits, a reduced compact document when it does not. Never returns a string that fails to parse.
62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/rails_ai_context/json_budget.rb', line 62 def generate(data, max) content = JSON.pretty_generate(data) return content unless max && content.length > max # Indentation alone can be the whole overage. Dropping it loses no # data, so try that before reducing: the reducer works against a # budget held back for its report and would otherwise clip a payload # that fits, and report a loss that did not happen. compact = JSON.generate(data) return compact if compact.length <= max reduce_to_budget(data, content.length, max) end |