Skip to content

feat(widget,server): setUser and setContext - #432

Merged
anurag629 merged 1 commit into
devfrom
feat/widget-user-context
Oct 28, 2026
Merged

anurag629 merged 1 commit into
devfrom
feat/widget-user-context

Conversation

@anurag629

Copy link
Copy Markdown
Member

W6 in the P2 plan (F-05 task 26), second part: ChatCops.setUser() and ChatCops.setContext(). update() comes next.

setUser({ name?, email?, token? } | null)

  • token sets the client's identity token for signed-in visitors (F-04) and drops the saved session token, so the next message gets a session for the new identity. The server already refuses to carry a session over to another user (security/sessions.ts:130), and starts a new conversation then.
  • Switching users, or signing out with null, calls clear(), so one person's conversation never shows for the next.
    • Signing in from anonymous keeps the chat.
    • Calling it again with the same token does nothing.
    • Only a real change of identity touches storage.
  • name and email become visitor details, sent with a conversation's first message even without a pre-chat form, and the name greets the visitor.

The widget used to send visitor details only until it knew the server spoke v1. With newConversation() and clear() from #429, a new conversation on a v1 server would never get them. Now they go with the first message of each conversation, tracked by conversation id.

setContext(data | null)

  • What it sends: details for the next messages, such as { plan: 'Team' }. They go with each message as the new pageContext.data, and null removes them.
  • Types: PageContext.data is in core's types and the widget's request type.
  • Server limits: at most 20 fields, keys up to 40 characters, values up to 500 (MAX_PAGE_DATA_*).
  • What the model sees: the details go in the visitor context block after the page lines, under "Details the page added (not verified):". They get the same cleaning as the other visitor values, so a value can't close the block or add lines.

Script tag loader

Both calls are kept until the chat loads, then applied right after it mounts. So a signed-in page can call setUser() on load without loading the chat early or touching storage. A test spies on Storage.prototype to check that.

Tests

  • Widget (16 in api.test.ts, 5 of them new here):
    • setContext in the request body and removed with null
    • setUser details sent with the first message, and the greeting
    • anonymous to signed in keeps the chat, the same token does nothing, another user and null clear it
    • the details sent again after newConversation() on a v1 server
    • the loader keeping both until mount, without storage access
  • Types: the two methods match across builds, and setContext takes only strings.
  • Server: page details are rendered after the page lines and marked not verified, they're left out when empty and can't break the block, and the schema limits work.

Checks

Local:

  • pnpm -r build
  • pnpm test:coverage: 1757 passed
  • pnpm lint: 39 warnings and 6 infos, same as dev
  • pnpm docs:check
  • pnpm size: the loader is 4.38 of 5 kB (+50 B) and the panel 16.45 of 17 kB (+240 B)

Docs: both methods on the API page, and pageContext.data in the request schema on the server setup and protocol pages.

@anurag629
anurag629 merged commit 86085c0 into dev Oct 28, 2026
5 checks passed
@anurag629
anurag629 deleted the feat/widget-user-context branch October 28, 2026 19:37
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