A complete TypeScript web chat showing the ContextDB Cloud memory loop without
putting a project key in browser code. The browser talks only to a small Node
server. The server uses native fetch against https://api.contextdb.ai.
The example demonstrates:
- recalling customer history when a chat session starts;
- preserving structured
userandagentturns; - calling
POST /v1/extract_memorieswithmode: "commit"after each exchange; - calling
POST /v1/recall_for_actionbefore a simulated booking; - asking for confirmation when no action-safe memory is returned;
- calling
POST /v1/confirmonly after an explicit yes, then checking the action gate again; - preserving named non-2xx
FormationResponseterminals such asprovider_errortogether with their HTTP status; - running without credentials in deterministic demo mode.
flowchart LR
B["Browser\nstatic HTML/CSS/JS"] -->|"same-origin /api/* only"| N["Node TypeScript server"]
N -->|"Bearer cdb_…\nnative fetch"| C["ContextDB Cloud\napi.contextdb.ai"]
N -.->|"optional native fetch"| O["OpenAI"]
E[".env / secret store"] -->|"server process only"| N
C --> R["recall → extract/commit\n→ recall_for_action → confirm"]
The ContextDB project key is read from the server environment, kept in a private
field, and used only to construct the server-to-server Authorization header.
Static assets contain no ContextDB URL, key, or authorization logic. A strict
Content Security Policy limits browser connections to the same origin.
Requirements: Node.js 20.10 or newer.
cp .env.example .env
npm install
npm run devOpen http://127.0.0.1:3000, start the default customer session, then:
- Send
Book me Thursday afternoon. - Choose Try booking action.
- Choose Yes, use it when the server asks for confirmation.
Demo mode keeps memory in the Node process and uses a deterministic responder.
It intentionally keeps booking details untrusted until the confirmation route
receives answer: "yes".
Copy the environment template and edit .env:
DEMO_MODE=false
CONTEXTDB_PROJECT_KEY=cdb_your_server_only_project_key
CONTEXTDB_BASE_URL=https://api.contextdb.aiThen run:
npm install
npm run devDo not rename the project key to a browser-exposed variable such as
NEXT_PUBLIC_* or VITE_*. In a real application, derive userId from your
authenticated server session rather than accepting an arbitrary customer ID.
The default responder is deterministic. To use OpenAI for chat wording, set:
OPENAI_API_KEY=your_server_only_openai_key
OPENAI_MODEL=gpt-4o-miniMemory behavior and the verify-before-act gate do not depend on OpenAI. If the optional model call fails, chat falls back to the deterministic response.
POST /api/sessioncalls ContextDBPOST /v1/recall.POST /api/chatstores the exact user turn and generated agent turn in the server session, then sends that pair toPOST /v1/extract_memorieswithmode: "commit".POST /api/actions/bookcallsPOST /v1/recall_for_action.- If the trusted list is empty, the server uses ordinary recall to locate the pending action-relevant memory and asks the browser for an explicit yes.
POST /api/actions/confirmaccepts onlyanswer: "yes", calls ContextDBPOST /v1/confirm, callsrecall_for_actionagain, and simulates the booking only if the confirmed memory now passes the active policy.
extract_memories is different from ordinary API errors. ContextDB can return
a well-formed terminal body with HTTP 502, 503, or 504. The client resolves that
body as a FormationResult and retains both status and httpStatus; it throws
only when the response is not a named formation terminal (for example, a 401
authentication error or malformed response).
POST /api/sessionPOST /api/chatPOST /api/actions/bookPOST /api/actions/confirmGET /api/health
These routes are the only network surface used by public/app.js.
npm test
npm run typecheck
npm run buildThe Vitest suite mocks native fetch for recall, extraction/commit, recall-for-action, confirmation, protocol errors, and named non-2xx formation terminals. It also serves the browser assets with a sentinel project key and asserts that the key never appears in any browser-visible asset or response.
npm run build
npm startdocker build -t contextdb-chat .
docker run --rm -p 3000:3000 --env-file .env contextdb-chatThe container defaults to DEMO_MODE=true; values in --env-file override it.
The booking is deliberately simulated: it returns a generated bookingId but
does not call a real booking provider. Server sessions and pending confirmations
are bounded, in-memory examples with expiry. Replace them with authenticated,
durable application state before deploying multiple server instances.
ContextDB Cloud is currently alpha. Confirmation records an attestation; it does not prove objective truth.
Apache-2.0. See LICENSE.