Skip to content

Add Kotlin snippet for the CallbackContext memory-write helpers - #2121

Merged
happyhuman merged 4 commits into
mainfrom
docs-kotlin-callback-memory-writes
Aug 18, 2026
Merged

Add Kotlin snippet for the CallbackContext memory-write helpers#2121
happyhuman merged 4 commits into
mainfrom
docs-kotlin-callback-memory-writes

Conversation

@happyhuman

Copy link
Copy Markdown
Collaborator

Summary

adk-kotlin 0.7.0 added addEventsToMemory and addMemory to CallbackContext,
alongside the addSessionToMemory that docs/sessions/memory.md already shows
in Kotlin. This adds a subsection covering the two cases the existing snippet
can't: a chosen subset of events, and facts you construct yourself.

Compiling changed the snippet's shape

The obvious version read the current turn's events off the context:

val thisTurn = context.invocationContext.session.events.filter { ... }   //

That doesn't compile — CallbackContext.invocationContext is internal, and
the class exposes no other route to the session. So addEventsToMemory takes
events the caller already holds, and the snippet's KDoc says so plainly rather
than leaving a reader to discover it. Worth knowing when reviewing: the ergonomic
gap is real, not a documentation choice.

Notes for reviewers

  • All three helpers throw IllegalStateException when the runner has no
    memory service — a runtime failure with nothing at compile time to warn you, so
    the page states it. Verified both messages by running them:
    Cannot add events to memory: memory service is not available. and
    Cannot add memory: memory service is not available.
  • Badged Kotlin v0.7.0, verified not assumed — neither helper exists on
    CallbackContext at v0.6.0 (0 of 2 present).
  • Kotlin-only subsection. The service-level equivalents are covered for Python
    under Extend memory capabilities; these are the callback-side convenience
    wrappers, which only Kotlin has.
  • Appended to the existing MemoryExample.kt, already registered, so CI
    compiles and lints it.

Verification

OK addEventsToMemory succeeded with a memory service
OK addMemory succeeded with a memory service
without a memory service -> IllegalStateException: Cannot add memory: ...
addEventsToMemory w/o service -> IllegalStateException: Cannot add events to memory: ...

verify_snippets.py passes all six levels.

adk-kotlin 0.7.0 added addEventsToMemory and addMemory to CallbackContext,
alongside the addSessionToMemory the page already shows in Kotlin. They cover
the cases addSessionToMemory cannot: a chosen subset of events, and facts you
construct yourself rather than letting the service derive them.

Compiling changed the shape of this snippet. The obvious version read the
current turn's events off context.invocationContext, but that property is
internal -- CallbackContext exposes no way to reach the session. So
addEventsToMemory takes events the caller already holds, and the snippet says
so rather than quietly implying otherwise.

Both helpers, and addSessionToMemory, throw IllegalStateException when the
runner has no memory service. That is a runtime failure with nothing at compile
time to warn you, so the page states it. Verified by running both paths:
"Cannot add events to memory: memory service is not available." and
"Cannot add memory: memory service is not available."

Badged Kotlin v0.7.0, verified rather than assumed: neither helper exists on
CallbackContext at v0.6.0.

Appended to the existing MemoryExample.kt, already registered, so CI compiles
and lints it.
@netlify

netlify Bot commented Aug 11, 2026

Copy link
Copy Markdown

Deploy Preview for adk-docs-preview ready!

Name Link
🔨 Latest commit 8e272a5
🔍 Latest deploy log https://app.netlify.com/projects/adk-docs-preview/deploys/6a849fad21c754000805fe44
😎 Deploy Preview https://deploy-preview-2121--adk-docs-preview.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
🤖 Make changes Run an agent on this branch

To edit notification comments on pull requests, go to your Netlify project configuration.

@happyhuman happyhuman self-assigned this Aug 11, 2026
Comment thread docs/sessions/memory.md Outdated
<span class="lst-supported">Supported in ADK</span><span class="lst-kotlin">Kotlin v0.7.0</span>
</div>

`addSessionToMemory` saves the whole session. When you want finer control,

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

don't start sentences with code syntax, and provide more context when you lead into a topic:

The CallbackContext.addSessionToMemory method is the default behavior for memory and saves the whole session of your agent.

@joefernandez joefernandez left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Approved with comment

@happyhuman
happyhuman merged commit 9bca69d into main Aug 18, 2026
12 checks passed
@happyhuman
happyhuman deleted the docs-kotlin-callback-memory-writes branch August 18, 2026 18:13
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.

3 participants