Skip to content

Compaction support#62

Open
aubrypaul wants to merge 6 commits intomainfrom
compaction-support
Open

Compaction support#62
aubrypaul wants to merge 6 commits intomainfrom
compaction-support

Conversation

@aubrypaul
Copy link
Contributor

@aubrypaul aubrypaul commented Mar 11, 2026

The changes add server-side context compaction functionality to the Chat class. Two new public methods (enableCompaction() and setCompactionThreshold()) allow configuration of this feature. The implementation introduces state variables to track settings and modifies both OpenAI and Gemini payload builders to conditionally include context management configurations when enabled.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Mar 11, 2026

📝 Walkthrough

Summary by CodeRabbit

  • Documentation

    • README updated with examples, usage notes, and API reference for server-side context compaction, including default behavior and example calls.
  • New Features

    • Added server-side context compaction for OpenAI and Gemini integrations.
    • New public controls to enable compaction and set the compaction threshold, with documented defaults and minimum validation.

Walkthrough

Adds server-side context compaction to GenAIApp.Chat: new state fields (compaction_enabled, compaction_threshold), two public methods (enableCompaction, setCompactionThreshold) with validation, propagation into OpenAI/Gemini payloads, and inclusion in Chat serialization and README docs.

Changes

Cohort / File(s) Summary
Documentation
README.md
Added "Enable OpenAI server-side compaction" subsection, examples for enableCompaction(true) and setCompactionThreshold(...), and notes about default/minimum thresholds.
Chat implementation & serialization
src/code.gs
Added compaction_enabled (default false) and compaction_threshold (default 10000) to GenAIApp.Chat; new public methods enableCompaction(enabled) and setCompactionThreshold(threshold) with validation (threshold must be finite and >= 1000); _toJson() includes compaction fields.
Payload builders
src/code.gs (… _buildOpenAIPayload(), _buildGeminiPayload())
When compaction is enabled, OpenAI and Gemini payload builders include context_management: { type: "compaction", compact_threshold: <value> } in outgoing request payloads.

Sequence Diagram(s)

sequenceDiagram
    participant Client as Client
    participant Chat as GenAIApp.Chat
    participant Serializer as Chat._toJson
    participant API as OpenAI/Gemini API

    Client->>Chat: enableCompaction(true)\nsetCompactionThreshold(10000)
    Chat-->>Chat: set compaction_enabled\nset compaction_threshold
    Client->>Chat: prepare/send message
    Chat->>Serializer: _toJson() (includes compaction fields)
    Chat->>API: _buildOpenAIPayload() / _buildGeminiPayload()\n(adds context_management when enabled)
    API-->>Chat: response
    Chat-->>Client: deliver response
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title 'Compaction support' directly and clearly summarizes the main change: adding server-side context compaction functionality to the Chat class.
Description check ✅ Passed The description is directly related to the changeset, detailing the new public methods, state variables, and payload builder modifications that constitute the server-side context compaction feature.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch compaction-support
  • 🛠️ JSDoc Checks: Commit on current branch
  • 🛠️ JSDoc Checks: Create PR
📝 Coding Plan
  • Generate coding plan for human review comments

Comment @coderabbitai help to get the list of available commands and usage tips.

Tip

CodeRabbit can suggest fixes for GitHub Check annotations.

Configure the reviews.tools.github-checks setting to adjust the time to wait for GitHub Checks to complete.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 3

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@README.md`:
- Line 162: The README states the default compaction threshold is 200000 but the
code initializes compaction_threshold to 10000; pick one and make them
consistent: either update the README text to state the actual default
(compaction_threshold = 10000) or change the initialization of the
compaction_threshold variable in src/code.gs to 200000 so it matches the README;
ensure you update the single source of truth (variable compaction_threshold and
any comments or docs referencing its default) so both docs and code reflect the
same value.
- Around line 166-167: Add a blank line after the preceding fenced code block so
the heading "### Give a web page as a knowledge base (optional)" is separated by
an empty line (fixing markdownlint MD031); update the README.md by inserting a
single empty line between the closing ``` code fence and that heading.

In `@src/code.gs`:
- Around line 346-349: The setCompactionThreshold method currently assigns any
value to compaction_threshold; add input validation in setCompactionThreshold to
ensure the provided threshold is a number and >= 1000 (the API-required
minimum). If the value is invalid (non-numeric, NaN, less than 1000), throw or
return an error (e.g., throw new Error or return this with no-op) with a clear
message and do not assign compaction_threshold; otherwise assign and return
this. Update the function named setCompactionThreshold and the
compaction_threshold assignment accordingly so callers cannot set invalid
thresholds.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Pro

Run ID: d0851e1f-1aff-4234-8c3b-86707fbb78ba

📥 Commits

Reviewing files that changed from the base of the PR and between 2577175 and 6ee4e3f.

📒 Files selected for processing (2)
  • README.md
  • src/code.gs

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

♻️ Duplicate comments (1)
README.md (1)

462-463: ⚠️ Potential issue | 🟡 Minor

Fix the documented default threshold in the Chat reference.

This section still says 200000, but the implementation default is 10000, and the usage example above already documents 10000. The mismatch will mislead consumers of the public API.

📝 Proposed fix
- `setCompactionThreshold(threshold)`: Set the compaction threshold (`200000` by default).
+ `setCompactionThreshold(threshold)`: Set the compaction threshold (`10000` by default).
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@README.md` around lines 462 - 463, Update the README Chat reference to match
the implementation default: change the documented default for
setCompactionThreshold from `200000` to `10000` so it aligns with the actual
behavior; specifically edit the lines mentioning `enableCompaction(enabled)` and
`setCompactionThreshold(threshold)` to reflect `10000` as the default for
setCompactionThreshold and ensure the usage example and descriptive text are
consistent with the function names `enableCompaction` and
`setCompactionThreshold`.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@src/code.gs`:
- Around line 337-339: The enableCompaction function currently coerces inputs
via Boolean(enabled) which treats strings like "false" as true; change it to
require a real boolean by checking typeof enabled === "boolean" and assign
compaction_enabled = enabled directly (do not coerce). If the check fails, throw
a TypeError (or explicitly return/error) with a clear message so callers using
string-based config don't accidentally enable compaction; update the function
named enableCompaction and the variable compaction_enabled accordingly.

---

Duplicate comments:
In `@README.md`:
- Around line 462-463: Update the README Chat reference to match the
implementation default: change the documented default for setCompactionThreshold
from `200000` to `10000` so it aligns with the actual behavior; specifically
edit the lines mentioning `enableCompaction(enabled)` and
`setCompactionThreshold(threshold)` to reflect `10000` as the default for
setCompactionThreshold and ensure the usage example and descriptive text are
consistent with the function names `enableCompaction` and
`setCompactionThreshold`.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Pro

Run ID: 77caba5d-5b3c-4a0d-8950-548349f429a2

📥 Commits

Reviewing files that changed from the base of the PR and between 6ee4e3f and e3f1365.

📒 Files selected for processing (2)
  • README.md
  • src/code.gs

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@README.md`:
- Around line 152-155: The review points out that compaction_enabled and
compaction_threshold are being passed into _buildGeminiPayload() via
advancedParametersObject but are not used there; either remove these keys from
the Gemini path or implement support for them in _buildGeminiPayload() so Gemini
receives/handles compaction_enabled and compaction_threshold consistently with
_buildOpenAIPayload(); also update documentation to state that
setCompactionThreshold() enforces a minimum threshold of 1000 tokens (>= 1000)
so callers know the validation rule and avoid runtime errors.
- Around line 157-159: Update the README example and API reference to document
setCompactionThreshold's validation: in the example where
GenAIApp.newChat().setCompactionThreshold(120000) is shown, add an inline
comment noting the method enforces a minimum value of 1000 and rejects
non-finite numbers; in the API reference entry for setCompactionThreshold,
explicitly state the parameter must be a finite number with a minimum allowed
value of 1000 (matching the runtime guards in setCompactionThreshold).
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Pro

Run ID: f8629070-37b6-4463-ace5-952bf8ebf09f

📥 Commits

Reviewing files that changed from the base of the PR and between e3f1365 and 3e856a2.

📒 Files selected for processing (1)
  • README.md

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@README.md`:
- Around line 462-463: Update the README entry for setCompactionThreshold to
match the action-phrase style used elsewhere: change the line for
setCompactionThreshold(threshold) to start with "Set" (e.g.,
"setCompactionThreshold(threshold): Set the compaction threshold (`10000` by
default, minimum `1000`; finite numbers only).") so it is consistent with
enableCompaction(enabled) and other method descriptions; locate the existing
string for setCompactionThreshold in README.md and replace its wording
accordingly.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Pro

Run ID: 0f344807-2467-4366-ac51-65cae781d12e

📥 Commits

Reviewing files that changed from the base of the PR and between 3e856a2 and 25d6b08.

📒 Files selected for processing (1)
  • README.md

Comment on lines +462 to +463
- `enableCompaction(enabled)`: Enable/disable OpenAI Responses API server-side compaction (`false` by default).
- `setCompactionThreshold(threshold)`: (`10000` by default, minimum `1000`; finite numbers only).
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick | 🔵 Trivial

Make setCompactionThreshold entry consistent with the reference style.
Line 463 currently omits the action phrase used by other methods. Consider: “setCompactionThreshold(threshold): Set the compaction threshold (10000 by default, minimum 1000; finite numbers only).”

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@README.md` around lines 462 - 463, Update the README entry for
setCompactionThreshold to match the action-phrase style used elsewhere: change
the line for setCompactionThreshold(threshold) to start with "Set" (e.g.,
"setCompactionThreshold(threshold): Set the compaction threshold (`10000` by
default, minimum `1000`; finite numbers only).") so it is consistent with
enableCompaction(enabled) and other method descriptions; locate the existing
string for setCompactionThreshold in README.md and replace its wording
accordingly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant