Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions fixtures/react-parity/ag-ui-candidate/installed-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,12 @@ export function checkCandidate(signal: AbortSignal) {
void agent.submit({ message: 'Hello' });
// @ts-expect-error Native resume stays on the native session.
void agent.resume;
const regenerated: Promise<CompleteOutcome> | undefined =
session.regenerate?.('assistant-id', { signal });
void regenerated;
void agent.regenerate?.('assistant-id', { signal });
// @ts-expect-error Regenerate takes a message id, not an index.
void session.regenerate?.(1);
// @ts-expect-error The view wraps a session, not its options.
createAgentSession(options);
return { session, snapshot, rows, stable, messages, toolCalls, agent, core };
Expand Down
25 changes: 23 additions & 2 deletions libs/ag-ui/src/runtime/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,9 @@ const session = createAgentSession(native);
changes. Subscribers hear about those changes only, so a native publication
that touches only state, child evidence or a decision is silent here.
- `submit` takes text and goes straight to the native session, as do `stop` and
`dispose`. Disposing the view disposes the session. `retry` goes to the native
session too, when it has one (see "Retry" below).
`dispose`. Disposing the view disposes the session. `retry` and `regenerate`
go to the native session too, when it has them (see "Retry" and
"Regenerate" below).
- A failed run becomes `snapshot.error`. A backend `RUN_ERROR` is a `server`
error carrying the backend's message. A stream that ended without a terminal
is `interrupted`. A response that was not OK carries its HTTP status: 401 and
Expand Down Expand Up @@ -195,6 +196,26 @@ otherwise resolves with the native re-run's outcome.
- A `RUN_ERROR` settles that the run failed, not that it did nothing. Work the
backend did before failing can be repeated.

### Regenerate

The plain native owner has `regenerate(messageId, options?)`, with the same
semantics as the Angular adapter's `regenerate`. It takes the `id` of a root
assistant record, or of the reasoning record a reasoning-only answer is shown
under, which is the `id` the core view gives that message. It keeps the history
up to and including the user message before that record, drops everything after
it, and sends a run with that history and the current state. The prompt is not
added again. Regenerating an earlier answer drops every later turn.

- It rejects without sending after disposal, while a run is active, while a
decision is waiting, for an id that is not an assistant record, and when no
user message precedes it.
- A regenerate that fails with a backend `RUN_ERROR` can be sent again with
`retry`, from the same trimmed history.
- The core view forwards it as the core session's `regenerate`, so React `Chat`
offers Regenerate on assistant messages.
- The client-tool owner has no `regenerate`: its ledger owns the history of the
calls it ran, and trimming that history is not supported.

### Approvals

`decisionState(snapshot)` reads the native snapshot's pending decision and how
Expand Down
25 changes: 20 additions & 5 deletions libs/ag-ui/src/runtime/agent-session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type {
AgentSession,
AgentSessionCustomEvent,
AgentSnapshot,
AgentSubmitOptions,
AgentUsage,
AgentUsageEntry,
} from '@threadplane/core';
Expand All @@ -16,7 +17,13 @@ import {
/** The native owner, with or without a client-tool catalog. */
export type NativeSession = Pick<
Session,
'subscribe' | 'submit' | 'stop' | 'dispose' | 'retry' | 'onCustomEvent'
| 'subscribe'
| 'submit'
| 'stop'
| 'dispose'
| 'retry'
| 'regenerate'
| 'onCustomEvent'
> & { getSnapshot(): SessionSnapshot };

/**
Expand Down Expand Up @@ -165,17 +172,19 @@ function project(
* It is a view, not a second owner. The snapshot is derived from the native
* snapshot on read and keeps its reference until something a core observer can
* see changes; subscribers are told about those changes only. `submit`, `stop`,
* `dispose` and, when the native session has it, `retry` go straight to the
* native session, so disposing the view disposes the session. `retry` rejects
* unless the error is `recovery: 'retry'`. Custom event listeners pass straight
* through to the native session too, when it has them. Application state,
* `dispose` and, when the native session has them, `retry` and `regenerate`
* go straight to the native session, so disposing the view disposes the
* session. `retry` rejects unless the error is `recovery: 'retry'`. Custom
* event listeners pass straight through to the native session too, when it
* has them. Application state,
* native resume and client-tool evidence have no core equivalent: keep the
* native session for those.
*/
export function createAgentSession(session: NativeSession): AgentSession {
let seen: SessionSnapshot | undefined;
let current: AgentSnapshot | undefined;
const retry = session.retry;
const regenerate = session.regenerate;
const listen = session.onCustomEvent;
const getSnapshot = (): AgentSnapshot => {
const native = session.getSnapshot();
Expand Down Expand Up @@ -204,6 +213,12 @@ export function createAgentSession(session: NativeSession): AgentSession {
submit: (input, options) => session.submit(input, options),
stop: () => session.stop(),
dispose: () => session.dispose(),
// The native owner rejects what it cannot regenerate, as the core
// contract asks, so the command passes straight through.
...(regenerate && {
regenerate: (messageId: string, options?: AgentSubmitOptions) =>
regenerate.call(session, messageId, options),
}),
...(retry && {
async retry() {
const snapshot = getSnapshot();
Expand Down
70 changes: 70 additions & 0 deletions libs/ag-ui/src/runtime/create-session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,21 @@ export interface Session {
* owner, which blocks every dispatched failure as uncertain.
*/
retry?(): Promise<CompleteOutcome>;
/**
* Answers the prompt behind one assistant message again, as the Angular
* adapter's `regenerate` does. Takes the `id` of a root assistant record (or
* of the reasoning record a reasoning-only answer is shown under), drops
* every record after the user message that precedes it, and sends a run
* with that history and the current state. The prompt is not added again.
* Rejects without sending while a run is active, while a decision is
* waiting, after disposal, for an id that is not an assistant record, and
* when no user message precedes it. `retry` can send a failed regenerate
* again. Absent on the client-tool owner, whose ledger owns its history.
*/
regenerate?(
messageId: string,
options?: { readonly signal?: AbortSignal }
): Promise<CompleteOutcome>;
/**
* Listens for `CUSTOM` events other than `on_interrupt` while runs stream.
* The factory always supplies it; it is optional so hand-written sessions
Expand Down Expand Up @@ -619,6 +634,61 @@ export function createSession(
);
});
}),
regenerate: (messageId, options) =>
new Promise<CompleteOutcome>((resolve, reject) => {
const signal = options?.signal;
publication.command(() => {
const previous = publication.getSnapshot();
let prompt = -1;
try {
if (disposed) throw new Error('The session is disposed.');
if (active)
throw new Error('Cannot regenerate while a response is running.');
if (previous.decision)
throw new Error('Cannot regenerate while a decision is waiting.');
const { transcript } = previous;
const root = (index: number) =>
typeof transcript[index].subagentRunId !== 'string';
const at = transcript.findIndex(
(record, index) =>
record.id === messageId &&
(record.role === 'assistant' || record.role === 'reasoning') &&
root(index)
);
if (at < 0)
throw new Error('Regenerate needs the id of an assistant message.');
for (let index = at - 1; index >= 0 && prompt < 0; index--)
if (transcript[index].role === 'user' && root(index))
prompt = index;
if (prompt < 0) throw new Error('No prompt precedes that message.');
} catch (error) {
reject(error);
return;
}
if (signal?.aborted) {
resolve('aborted');
return;
}
const id = crypto.randomUUID();
// Keep the prompt: it ends the history and is the next run's input.
const transcript = Object.freeze(
previous.transcript.slice(0, prompt + 1)
);
saved = { runId: id, transcript, state: previous.state };
admit(
id,
Object.freeze({
...previous,
status: 'running',
transcript,
...nextInvocationSubagents(previous),
run: Object.freeze({ id }),
}),
signal,
resolve
);
});
}),
stop: () => close(false),
dispose: () => close(true),
};
Expand Down
Loading
Loading