Skip to content

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

Open
happyhuman wants to merge 2 commits into
mainfrom
docs-kotlin-callback-memory-writes
Open

Add Kotlin snippet for the CallbackContext memory-write helpers#2121
happyhuman wants to merge 2 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 ea6be87
🔍 Latest deploy log https://app.netlify.com/projects/adk-docs-preview/deploys/6a7f2d50940dd900085c49fa
😎 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
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.

2 participants