createFakeAgentSession
Creates an in-memory AgentSession for tests and stories. There is no
transport: the test publishes snapshots through the returned controls, and
every snapshot is frozen and keeps unchanged messages by reference.
Subscribers are notified synchronously, so React tests wrap control calls in
act.
By default submit records the call, appends a user message, merges any
submitted state into the snapshot's state, and resolves 'success'. Pass
onSubmit to stream a reply or return another outcome.
resume records the call, removes the interrupts it answers and resolves
'success', or 'paused' while others remain. It rejects a response that
names an interrupt the snapshot does not list, or names one twice. A message
or state passed in its options is applied the way submit applies them.
runWithoutMessage, regenerate, retry, checkStatus, onCustomEvent and
clientTools are optional in the contract and absent here by default, so a
view under test offers no control for them. List one in commands, or pass
its handler, to make it present:
runWithoutMessagemerges the state it is given and appends no message.regenerateremoves the named assistant message and everything after it. It rejects during a run and for an id that names no assistant message.retryresolves'aborted'during a run or when the snapshot holds no error, rejects for an error that cannot be retried, and otherwise clears the error and calls again whichever handler last rejected.checkStatuscounts the call and rejects during a run.onCustomEventreceives whatemitCustomEventsends.clientToolsrecords catalogs and results. A result moves its call out ofpendingClientToolsand completes or fails the matching tool call.
import { createFakeAgentSession } from '@threadplane/angular/chat/testing';Declaration · overload 1
export declare function createFakeAgentSession<TTools extends {
[K in keyof TTools]: ToolContract;
} = Record<string, ToolContract>, TState = unknown>(init?: FakeAgentSessionInit<TTools, TState>): FakeAgentSession<TTools, TState>;Type parameters
TTools{ [K in keyof TTools]: ToolContract; }Default:Record<string, ToolContract>TStateDefault:unknown
Parameters
init· optionalFakeAgentSessionInit<TTools, TState>
Returns
FakeAgentSession<TTools, TState>Examples
const fake = createFakeAgentSession({
onSubmit: (input, fake) => {
const reply = fake.stream();
reply.append(`You said: ${input}`);
reply.complete();
},
});
render(<Chat session={fake.session} />);