Skip to main content

Chat controller

Drive one conversation​

ChatController holds the transcript, requirements, and the current candidate page for one chat. SourcingChat is a view over it.

import { ChatController, SourcingClient } from '@search-engine-sdk/core';

const client = new SourcingClient({ baseUrl: '/api/v2' });
const { id } = await client.chats().create({ title: 'Go engineers in Austin' });
const controller = new ChatController(client.chat(id), {
historyPageSize: 15,
candidatePageSize: 10,
});
const unsubscribe = controller.subscribe(() => render(controller.snapshot));

await controller.load();
if (controller.snapshot.status === 'ready') {
await controller.sendText('Go engineers in Austin');
}

unsubscribe();
controller.dispose();

historyPageSize defaults to 15 and windows messages already loaded with the workspace. candidatePageSize defaults to 10 and is capped at 200. render is the host's snapshot handler.

MethodBehavior
load()Reads the workspace. A missing workspace becomes status: 'ready' with an empty transcript. An active saved turn is followed. Otherwise the first candidate page is loaded.
loadOlder()Reveals the previous window of messages already in memory.
sendText(text)Submits one turn. The client picks the endpoint.
answerQuestion(questionId, option)Sends the option as an answer to that question.
updateFilters(version, edits)Posts the tree edits, saves the workspace, and reloads page 1. No-ops while a turn is running or when edits is empty.
loadCandidates(page)Loads that page. The default page is 1.
abort()Cancels the turn when snapshot.canCancel is true, then follows the saved cancellation.
release()Stops local requests. An accepted turn keeps running on the server.
dispose()release(), then drops listeners. Later calls no-op.

sendText resolves after the turn settles. Failures land on snapshot.error. Read that field, or onError in React. An HTTP failure uses the status text below. A failed turn uses the stream message. The raw response body stays off both.

The first message on an empty plan uses POST /plan/outline/stream?engine=requirements when features() reports outline and the requirements engine. A plan that already has filters or criteria, and any answer to a question, uses POST /agent/stream. Pass the message only.

The closing outline event is result. The closing agent events are done and new_search. Each of those is applied, then the stream stops. new_search sets snapshot.newSearch to the suggested text. After requirements change, the controller prepares a filter set and loads candidates. When that search is for a filter set already stored on the workspace and the server returns 409, the controller prepares a new filter set once and loads the page again.

snapshot.candidates is the hydrated page (data, count). Rank fields from the search stay on each candidate.rank. Sort order is the server's score order. A host that sorts columns calls candidates.search itself.