Cartesiandocs

iOS SDK

Headless (core only)

Use CartesianCore to build a completely custom experience.

CartesianCore is Foundation, Combine and async/await, with no UI. Add only the CartesianCore product to your target.

Create a client

import CartesianCore

let client = CartesianClient(
    config: CartesianConfig(vertical: .grocery, baseURL: URL(string: "https://cartesian.yourcompany.com")!),
    sessionStore: KeychainSessionStore()      // the default; or InMemorySessionStore(), or your own SessionStore
)
try await client.signIn(sessionToken: token)
swift

CartesianClient and ChatSession are @MainActor ObservableObjects: every state change happens on the main actor, so you can bind them straight into SwiftUI or subscribe with Combine. Keep one client per backend.

Run a conversation

let chat = client.chat

let cancellable = chat.$state.sink { state in
    guard let last = state.messages.last?.assistant else { return }
    render(last.text, last.products, last.activeStep?.title)
}

try await chat.send("Find whole milk under $5")
await chat.stop()                      // interrupt the running turn
chat.newChat()                         // start over (returns false while a turn runs)
try await chat.open(threadId)          // reopen a past chat
try await chat.retry()                 // resend the last question
try await chat.rate(messageId, .up)
swift

send waits up to 30 seconds for the socket, creates a thread for the first message, and returns once the backend accepts the turn. The answer then streams into state.

State

ChatState
messages[ChatMessage]: .user(UserMessage) or .assistant(AssistantMessage)
isRespondingA turn is running
isLoadingA past thread is loading
errorThe last chat-level error
feedbackRatings by message id

An AssistantMessage has text (Markdown), thinking, steps (AgentStep with title, phase, kind), products, comparisons, cartUpdates, status (.streaming, .done, .stopped, .failed) and error.

chat.suggestions holds up to four follow-up questions, fetched from POST /threads/{id}/suggest after a turn completes.

Events

for await event in chat.events() {
    switch event {
    case let .turnStarted(threadId): ...
    case let .turnFinished(threadId, message): ...
    case let .productsShown(threadId, products): ...
    case let .cartChanged(threadId, update): ...
    case let .error(message): ...
    }
}
swift

chat.eventPublisher is the same stream as a Combine publisher.

Connection and auth

PropertyType
client.authState.restoring, .signedOut or .signedIn(User)
client.connection.idle, .connecting, .open or .closed
client.cart, client.wishlist, client.threadsKept up to date from REST calls and socket frames

At launch with a saved token, call try await client.restore(). It returns .signedOut only when the backend answers 401; on network errors and 5xx it throws and keeps the token, so you can retry. The socket sends a ping after 25 seconds of silence, reconnects with backoff (0.8s growing to 15s) and resumes a running turn when it comes back.

REST

try await client.refreshThreads()
try await client.renameThread(id, name: "Weekly shop")
try await client.deleteThread(id)
try await client.refreshCart()
try await client.addToCart(product, quantity: 2)
try await client.setCartQuantity(productId, quantity: 3)
try await client.removeFromCart(productId)
try await client.clearCart()
try await client.refreshWishlist()
try await client.save(product)
try await client.unsave(productId)
let profile = try await client.profile()
let comparison = try await client.compare([idA, idB])
let text = try await client.readPhotoText(jpegDataURL: dataURL)
await client.signOut()
swift

Errors

Every call throws CartesianError:

CaseWhen
.networkThe backend can't be reached
.http(status:message:)A non-2xx response other than 401
.unauthorized401: the session is cleared
.notSignedInNo token yet
.notConnectedThe socket isn't open
.command(action:message:)The socket rejected a command
.busyA turn is already running

error.message is a sentence you can show to shoppers.