React SDK
Headless
Use the Cartesian client without the bundled UI, in React or in plain TypeScript.
@cogitx/cartesian-react/core has the client, the chat session and the models, and no React. Use it to build your own interface, in another framework, or in Node.
import { CartesianClient } from "@cogitx/cartesian-react/core";
const client = new CartesianClient({ vertical: "apparel", baseUrl: "https://assistant.example.com" });
await client.signIn(token);
await client.chat.send("Running shoes for a half marathon under $150");tsObservable state
Each piece of state is a small store with getSnapshot() and subscribe(listener). Snapshots are immutable and only change when the state changes, so they work with React's useSyncExternalStore:
const chat = useSyncExternalStore(client.chat.state.subscribe, client.chat.state.getSnapshot);tsx| Store | Value |
|---|---|
client.auth | { status: "restoring" }, { status: "signedOut" } or { status: "signedIn", user } |
client.connection | "idle", "connecting", "open" or "closed" |
client.cart | Cart |
client.wishlist | Wishlist |
client.threads | ThreadSummary[] |
client.license | CartesianLicense |
client.chat.state | ChatState: threadId, messages, isResponding, isLoading, error, feedback, isEmpty |
client.chat.suggestions | Follow-up questions after the latest answer (up to 4) |
waitFor(store, predicate, timeoutMs?) resolves when a store matches.
Chat
await client.chat.send(text); // throws CartesianError "busy" while an answer is running
await client.chat.stop();
client.chat.newChat();
await client.chat.open(threadId); // load an earlier conversation and resume a running answer
await client.chat.retry(); // resend the last question after a failure
await client.chat.rate(messageId, "up");
client.chat.dismissError();
const off = client.chat.onEvent((e) => {
if (e.type === "turnFinished") console.log(e.message.text);
});tsEvents: turnStarted, turnFinished, productsShown, cartChanged and error.
An assistant message has text, thinking, steps (the trace), products, comparisons, cartUpdates, status (streaming, done, stopped, failed) and error.
After each answer the session saves the transcript with PUT /api/threads/{id}/transcript and asks POST /api/threads/{id}/suggest for follow-ups. On the grocery vertical it loads products from GET /api/threads/{id}/evidence; on apparel it asks POST /api/threads/{id}/present when the answer has no products.
Account and catalog
await client.refreshCart();
await client.addToCart(product, 2);
await client.setCartQuantity(productId, 3);
await client.removeFromCart(productId);
await client.clearCart();
await client.refreshWishlist();
await client.save(product);
await client.unsave(productId);
await client.refreshThreads();
await client.renameThread(id, "Weekly shop");
await client.deleteThread(id);
await client.profile(); // ShopperProfile
await client.compare([idA, idB]); // Comparison, needs an open chat
await client.readPhotoText(jpegDataUrl);tsConnection
The client opens the WebSocket when the shopper signs in. It sends a ping after 25 s without traffic, reconnects with backoff (0.8 s growing to 15 s) and resumes the open conversation after a reconnect. client.connect(), client.disconnect() and client.reconnectNow() control it by hand, and client.close() stops it for good.
If the socket keeps failing before the backend says it is ready, or closes with code 4401 or 4403, the client checks the session with GET /api/auth/session. Only a 401 from that check signs the shopper out.
Errors
Calls reject with CartesianError. Its kind is one of network, http (with status), unauthorized, notSignedIn, notConnected, command (with action) or busy.
The controller in React
Inside CartesianProvider, useCartesian() returns the same controller the bundled UI uses: all of the state above plus draft, drawer, suggestions, canSend, cartCount, savedCount, isDeviceOnline and actions such as send, addToCart, toggleSaved, openCart, checkout and retryConnection. It respects your feature modes and listener. See the Reference.
Testing without a browser
In Node, pass fetch and webSocket so the grocery cookie can be sent:
import WebSocket from "ws";
const client = new CartesianClient({
vertical: "grocery",
baseUrl,
webSocket: ({ url, token }) => new WebSocket(url, { headers: { cookie: `grocery_session=${token}` } }),
});ts