feat(sdk): let a client send context with a user message - #36
Merged
Conversation
`user-chat.sendMessage` takes an optional `context`: what the client knew when the user sent the message (the page on screen, a selected element). The LLM gets it in a `<user_context>` block after the message. The chat history keeps it in a separate field, so a UI that renders `content` does not show it. The hard token limit counts content and context together. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012fX9ohNay3XuPYkKGdGZ3i
`setMessageContext` stored the context, but `sendMessage` never read it, so the agent did not receive it. Send it as the new `context` field. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012fX9ohNay3XuPYkKGdGZ3i
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
A client often knows something the user does not type: the page shown in a preview, a selected element, the editor state. The agent needs this to understand messages like "make this heading bigger".
@roj-ai/client-reactalready has a place for it.useSessionStorehasmessageContextandsetMessageContext. ButsendMessagenever reads the value, anduser-chat.sendMessagehas no field that could carry it. The context is computed and then dropped. A production consumer shipped a "select an element and ask the agent" feature on top of this store, and the agent never received the selection.Change
user-chat.sendMessagetakes an optionalcontext: string.user_chat_message_receivedevent andUserChatMessageget an optionalcontext. The field is omitted when the client sends none, so events and state for plain messages do not change.contentstays what the user typed. A UI that renderscontentdoes not show the context.MESSAGE_MAX_TOKENS) counts content and context together. Soft truncation still applies to content only.client-react:sendMessagesendsmessageContextascontext.Compatibility
contextkey (z.object), so a new client works with an old server. The agent then does not see the context, as before.Tests
user-chat.integration.test.ts:<user_context>block. The chat history keeps the context apart fromcontent.contextkey.bun run test: 1606 pass, 77 skip, 0 fail.bun run ts:build,bun run lintandtsc -p packages/sdk/tsconfig.test.jsonare clean.🤖 Generated with Claude Code
https://claude.ai/code/session_012fX9ohNay3XuPYkKGdGZ3i