@blazingagents/sdk 0.50.0 → 0.52.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +72 -6
- package/dist/chat-relay-wire.d.ts +53 -0
- package/dist/chat-relay-wire.js +60 -0
- package/dist/chat-transport.d.ts +57 -4
- package/dist/chat-transport.js +158 -17
- package/dist/contracts/entities/session-inputs.d.ts +4 -4
- package/dist/contracts/entities/session-inputs.js +1 -1
- package/dist/contracts/entities/session-messages.d.ts +20 -0
- package/dist/contracts/entities/session-messages.js +21 -0
- package/dist/contracts/entities/sessions.d.ts +0 -19
- package/dist/contracts/entities/sessions.js +1 -20
- package/dist/contracts/index.d.ts +3 -2
- package/dist/contracts/index.js +2 -1
- package/dist/errors.js +1 -1
- package/dist/http.d.ts +27 -1
- package/dist/http.js +42 -27
- package/dist/index.d.ts +1 -1
- package/dist/react/chat-state.d.ts +197 -0
- package/dist/react/chat-state.js +425 -0
- package/dist/react/index.d.ts +5 -0
- package/dist/react/index.js +6 -0
- package/dist/react/use-blazing-agents-chat.d.ts +76 -0
- package/dist/react/use-blazing-agents-chat.js +246 -0
- package/dist/relay.d.ts +10 -4
- package/dist/relay.js +100 -54
- package/package.json +22 -1
|
@@ -5,6 +5,7 @@ import { toolExecutionReferenceSchema } from "./agent-approval.js";
|
|
|
5
5
|
import { agentConfigSchema, agentResponseSchema } from "./agents.js";
|
|
6
6
|
import { metadataSchema, userIdSchema } from "./attribution.js";
|
|
7
7
|
import { chatRequestBodySchema } from "./chat.js";
|
|
8
|
+
import { sessionMessageSchema } from "./session-messages.js";
|
|
8
9
|
/**
|
|
9
10
|
* `GET /v1/agents/{agentId}/sessions` list item. `agentId`/`tenantId` never
|
|
10
11
|
* leak to the wire; `userId`/`metadata` come from the first successful Turn.
|
|
@@ -48,26 +49,6 @@ export const latestSessionListItemSchema = sessionListItemSchema
|
|
|
48
49
|
})
|
|
49
50
|
.strip();
|
|
50
51
|
export const latestSessionsListResponseSchema = paginatedResponseSchema(latestSessionListItemSchema);
|
|
51
|
-
/**
|
|
52
|
-
* `GET /v1/agents/{agentId}/sessions/{sessionId}/messages` — transcript as
|
|
53
|
-
* `UIMessage[]` verbatim, paginated backwards (newest page first) with an
|
|
54
|
-
* optional forward `?after={cursor}` for the poll loop.
|
|
55
|
-
*/
|
|
56
|
-
export const sessionMessageSchema = z
|
|
57
|
-
.object({
|
|
58
|
-
id: z.string().min(1),
|
|
59
|
-
role: z.enum(["system", "user", "assistant"]),
|
|
60
|
-
branchable: z.boolean(),
|
|
61
|
-
parts: z
|
|
62
|
-
.array(z
|
|
63
|
-
.object({
|
|
64
|
-
type: z.string().min(1),
|
|
65
|
-
})
|
|
66
|
-
.passthrough())
|
|
67
|
-
.min(1),
|
|
68
|
-
metadata: z.unknown().optional(),
|
|
69
|
-
})
|
|
70
|
-
.passthrough();
|
|
71
52
|
/**
|
|
72
53
|
* The messages endpoint carries two cursors: `nextCursor` for continuing
|
|
73
54
|
* pagination in the requested direction (backward `?cursor=` walks older
|
|
@@ -20,8 +20,9 @@ export { createPromptBodySchema, parsePromptVariables, promptResponseSchema, pro
|
|
|
20
20
|
export { createProviderBodySchema, providerListItemSchema, providerModelsResponseSchema, providerResponseSchema, providersResponseSchema, providerTypeSchema, thinkingLevelsResponseSchema, updateProviderBodySchema, } from "./entities/providers.ts";
|
|
21
21
|
export type { ChatSteerConsumedEvent, SessionActivity, SessionInput, SessionInputResponse, SessionInputState, SessionInputsQuery, SessionInputsResponse, StopSessionBody, StopSessionResponse, SubmitSessionInputBody, } from "./entities/session-inputs.ts";
|
|
22
22
|
export { chatSteerConsumedEventSchema, sessionActivitySchema, sessionInputRequestIdSchema, sessionInputResponseSchema, sessionInputSchema, sessionInputStateSchema, sessionInputsQuerySchema, sessionInputsResponseSchema, stopSessionBodySchema, stopSessionResponseSchema, submitSessionInputBodySchema, } from "./entities/session-inputs.ts";
|
|
23
|
-
export
|
|
24
|
-
export {
|
|
23
|
+
export { type SessionMessage, sessionMessageSchema, } from "./entities/session-messages.ts";
|
|
24
|
+
export type { ContinueToolApprovalsBody, LatestSessionListItem, SessionForkedFrom, SessionListItem, ToolApprovalContinuationState, ToolApprovalDecision, ToolApprovalsResponse, TurnStreamErrorEvent, } from "./entities/sessions.ts";
|
|
25
|
+
export { continueToolApprovalsBodySchema, latestSessionListItemSchema, latestSessionsListResponseSchema, sessionForkedFromSchema, sessionListItemSchema, sessionMessagesResponseSchema, sessionResponseSchema, sessionsListResponseSchema, toolApprovalContinuationSchema, toolApprovalContinuationStateSchema, toolApprovalDecisionSchema, toolApprovalStateSchema, toolApprovalsResponseSchema, turnStreamErrorEventSchema, } from "./entities/sessions.ts";
|
|
25
26
|
export { copySkillBodySchema, createSkillBodySchema, skillArchiveTypeSchema, skillCopyResultsSchema, skillDescriptionSchema, skillDetailSchema, skillFilePathSchema, skillMetadataSchema, skillNameSchema, skillSchema, skillsListResponseSchema, } from "./entities/skills.ts";
|
|
26
27
|
export type { SpendingLimit, SpendingLimitResponse, SpendingLimitStopDetails, UpdateSpendingLimitBody, } from "./entities/spending-limits.ts";
|
|
27
28
|
export { nextSpendingLimitReset, spendingLimitPeriodSchema, spendingLimitResetIntervalSchema, spendingLimitResponseSchema, spendingLimitSchema, spendingLimitStopDetailsSchema, updateSpendingLimitBodySchema, } from "./entities/spending-limits.ts";
|
package/dist/contracts/index.js
CHANGED
|
@@ -14,7 +14,8 @@ export { createMerchantConnectionBodySchema, merchantBindingResponseSchema, merc
|
|
|
14
14
|
export { createPromptBodySchema, parsePromptVariables, promptResponseSchema, promptsListQuerySchema, promptsResponseSchema, promptTemplateSchema, renderPromptTemplate, updatePromptBodySchema, } from "./entities/prompts.js";
|
|
15
15
|
export { createProviderBodySchema, providerListItemSchema, providerModelsResponseSchema, providerResponseSchema, providersResponseSchema, providerTypeSchema, thinkingLevelsResponseSchema, updateProviderBodySchema, } from "./entities/providers.js";
|
|
16
16
|
export { chatSteerConsumedEventSchema, sessionActivitySchema, sessionInputRequestIdSchema, sessionInputResponseSchema, sessionInputSchema, sessionInputStateSchema, sessionInputsQuerySchema, sessionInputsResponseSchema, stopSessionBodySchema, stopSessionResponseSchema, submitSessionInputBodySchema, } from "./entities/session-inputs.js";
|
|
17
|
-
export {
|
|
17
|
+
export { sessionMessageSchema, } from "./entities/session-messages.js";
|
|
18
|
+
export { continueToolApprovalsBodySchema, latestSessionListItemSchema, latestSessionsListResponseSchema, sessionForkedFromSchema, sessionListItemSchema, sessionMessagesResponseSchema, sessionResponseSchema, sessionsListResponseSchema, toolApprovalContinuationSchema, toolApprovalContinuationStateSchema, toolApprovalDecisionSchema, toolApprovalStateSchema, toolApprovalsResponseSchema, turnStreamErrorEventSchema, } from "./entities/sessions.js";
|
|
18
19
|
export { copySkillBodySchema, createSkillBodySchema, skillArchiveTypeSchema, skillCopyResultsSchema, skillDescriptionSchema, skillDetailSchema, skillFilePathSchema, skillMetadataSchema, skillNameSchema, skillSchema, skillsListResponseSchema, } from "./entities/skills.js";
|
|
19
20
|
export { nextSpendingLimitReset, spendingLimitPeriodSchema, spendingLimitResetIntervalSchema, spendingLimitResponseSchema, spendingLimitSchema, spendingLimitStopDetailsSchema, updateSpendingLimitBodySchema, } from "./entities/spending-limits.js";
|
|
20
21
|
export { createTaskBodySchema, createTaskResponseSchema, createTaskRunBodySchema, createTaskRunResponseSchema, taskCronConfigSchema, taskIntervalConfigSchema, taskLatestRunSchema, taskListItemSchema, taskOnceConfigSchema, taskResponseSchema, taskRunMessagesResponseSchema, taskRunResponseSchema, taskRunStatusSchema, taskRunsListResponseSchema, taskScheduleInputSchema, taskScheduleKindSchema, tasksListResponseSchema, updateTaskBodySchema, } from "./entities/tasks.js";
|
package/dist/errors.js
CHANGED
|
@@ -31,7 +31,7 @@ export class BlazingAgentsError extends Error {
|
|
|
31
31
|
* @param options - Configuration for this operation.
|
|
32
32
|
*/
|
|
33
33
|
constructor({ code, details, headers, message, param, requestId, responseBody, responseBodyTruncated, status, }, options) {
|
|
34
|
-
super(
|
|
34
|
+
super(message, options);
|
|
35
35
|
this.name = BLAZING_AGENTS_ERROR_NAME;
|
|
36
36
|
this.code = code;
|
|
37
37
|
this.details = details;
|
package/dist/http.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { BlazingAgentsError } from "./errors.ts";
|
|
2
|
-
import type { HttpConfig, RequestOptions } from "./types.ts";
|
|
2
|
+
import type { BlazingAgentsFetch, BlazingAgentsRequestInit, HttpConfig, RequestOptions } from "./types.ts";
|
|
3
3
|
/**
|
|
4
4
|
* Builds an endpoint path, encoding each interpolated value so it stays inside
|
|
5
5
|
* one path segment. `encodeURIComponent` leaves `.` and `..` unchanged, and
|
|
@@ -31,6 +31,22 @@ export declare function requestJson<T>(config: HttpConfig, path: string, options
|
|
|
31
31
|
* @param options - Configuration for this operation.
|
|
32
32
|
*/
|
|
33
33
|
export declare function requestStream(config: HttpConfig, path: string, options?: RequestOptions): Promise<Response>;
|
|
34
|
+
/**
|
|
35
|
+
* Calls `fetch` and turns its rejection into a `request_aborted` or
|
|
36
|
+
* `network_error` SDK error.
|
|
37
|
+
* @param fetchImpl - The fetch to call.
|
|
38
|
+
* @param url - Request URL.
|
|
39
|
+
* @param init - Request options.
|
|
40
|
+
* @returns The response, whatever its status.
|
|
41
|
+
*/
|
|
42
|
+
export declare function sendRequest(fetchImpl: BlazingAgentsFetch, url: string, init: BlazingAgentsRequestInit): Promise<Response>;
|
|
43
|
+
/**
|
|
44
|
+
* Reads bounded diagnostics and decodes a failed HTTP response.
|
|
45
|
+
* @param response - A response with a non-2xx status.
|
|
46
|
+
* @param signal - Signal that cancels the operation.
|
|
47
|
+
* @returns The decoded error, or `invalid_response` when the body is not an error envelope.
|
|
48
|
+
*/
|
|
49
|
+
export declare function errorFromResponse(response: Response, signal?: AbortSignal): Promise<BlazingAgentsError>;
|
|
34
50
|
/**
|
|
35
51
|
* Checks whether the signal aborted or the cause names an AbortError.
|
|
36
52
|
* @param cause - Original failure to retain.
|
|
@@ -44,3 +60,13 @@ export declare function isRequestAborted(cause: unknown, signal?: AbortSignal):
|
|
|
44
60
|
* @returns A request_aborted error retaining the cause.
|
|
45
61
|
*/
|
|
46
62
|
export declare function requestAbortedError(cause: unknown): BlazingAgentsError;
|
|
63
|
+
/**
|
|
64
|
+
* Builds an invalid_response error with bounded diagnostics and request correlation.
|
|
65
|
+
* @param headers - Response headers.
|
|
66
|
+
* @param status - HTTP response status.
|
|
67
|
+
* @param responseBody - Diagnostic body text.
|
|
68
|
+
* @param responseBodyTruncated - Whether the diagnostic body was cut short.
|
|
69
|
+
* @param cause - The parse or validation failure.
|
|
70
|
+
* @returns The invalid_response error.
|
|
71
|
+
*/
|
|
72
|
+
export declare function invalidResponseError(headers: Headers, status: number, responseBody: string, responseBodyTruncated: boolean, cause?: unknown): BlazingAgentsError;
|
package/dist/http.js
CHANGED
|
@@ -221,47 +221,56 @@ async function rawRequest(config, path, options) {
|
|
|
221
221
|
headers["content-type"] = "application/json";
|
|
222
222
|
body = JSON.stringify(options.json);
|
|
223
223
|
}
|
|
224
|
+
const response = await sendRequest(fetchImpl, url, {
|
|
225
|
+
method,
|
|
226
|
+
headers,
|
|
227
|
+
body,
|
|
228
|
+
...(options.signal ? { signal: options.signal } : {}),
|
|
229
|
+
});
|
|
224
230
|
try {
|
|
225
|
-
|
|
231
|
+
config.onResponse?.({
|
|
226
232
|
method,
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
233
|
+
path,
|
|
234
|
+
status: response.status,
|
|
235
|
+
durationMs: performance.now() - startedAt,
|
|
236
|
+
requestId: response.headers.get("x-request-id") ?? undefined,
|
|
237
|
+
...(clientRequestId === undefined ? {} : { clientRequestId }),
|
|
230
238
|
});
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
239
|
+
}
|
|
240
|
+
catch {
|
|
241
|
+
/** Observation must not affect transport or response ownership. */
|
|
242
|
+
}
|
|
243
|
+
return response;
|
|
244
|
+
}
|
|
245
|
+
/**
|
|
246
|
+
* Calls `fetch` and turns its rejection into a `request_aborted` or
|
|
247
|
+
* `network_error` SDK error.
|
|
248
|
+
* @param fetchImpl - The fetch to call.
|
|
249
|
+
* @param url - Request URL.
|
|
250
|
+
* @param init - Request options.
|
|
251
|
+
* @returns The response, whatever its status.
|
|
252
|
+
*/
|
|
253
|
+
export async function sendRequest(fetchImpl, url, init) {
|
|
254
|
+
try {
|
|
255
|
+
return await fetchImpl(url, init);
|
|
245
256
|
}
|
|
246
257
|
catch (cause) {
|
|
247
|
-
|
|
248
|
-
if (requestAborted) {
|
|
258
|
+
if (isRequestAborted(cause, init.signal ?? undefined)) {
|
|
249
259
|
throw requestAbortedError(cause);
|
|
250
260
|
}
|
|
251
|
-
let message = NETWORK_ERROR_MESSAGE;
|
|
252
|
-
if (cause instanceof Error) {
|
|
253
|
-
message = cause.message;
|
|
254
|
-
}
|
|
255
261
|
throw new BlazingAgentsError({
|
|
256
262
|
code: "network_error",
|
|
257
|
-
message,
|
|
263
|
+
message: cause instanceof Error ? cause.message : NETWORK_ERROR_MESSAGE,
|
|
258
264
|
}, { cause });
|
|
259
265
|
}
|
|
260
266
|
}
|
|
261
267
|
/**
|
|
262
268
|
* Reads bounded diagnostics and decodes a failed HTTP response.
|
|
269
|
+
* @param response - A response with a non-2xx status.
|
|
270
|
+
* @param signal - Signal that cancels the operation.
|
|
271
|
+
* @returns The decoded error, or `invalid_response` when the body is not an error envelope.
|
|
263
272
|
*/
|
|
264
|
-
async function errorFromResponse(response, signal) {
|
|
273
|
+
export async function errorFromResponse(response, signal) {
|
|
265
274
|
const read = await readResponseBody(response, true);
|
|
266
275
|
if (read.readFailed && isRequestAborted(read.cause, signal)) {
|
|
267
276
|
return requestAbortedError(read.cause);
|
|
@@ -480,8 +489,14 @@ function parseErrorEnvelope(bodyText, status, headers) {
|
|
|
480
489
|
}
|
|
481
490
|
/**
|
|
482
491
|
* Builds an invalid_response error with bounded diagnostics and request correlation.
|
|
492
|
+
* @param headers - Response headers.
|
|
493
|
+
* @param status - HTTP response status.
|
|
494
|
+
* @param responseBody - Diagnostic body text.
|
|
495
|
+
* @param responseBodyTruncated - Whether the diagnostic body was cut short.
|
|
496
|
+
* @param cause - The parse or validation failure.
|
|
497
|
+
* @returns The invalid_response error.
|
|
483
498
|
*/
|
|
484
|
-
function invalidResponseError(headers, status, responseBody, responseBodyTruncated, cause) {
|
|
499
|
+
export function invalidResponseError(headers, status, responseBody, responseBodyTruncated, cause) {
|
|
485
500
|
return new BlazingAgentsError({
|
|
486
501
|
code: "invalid_response",
|
|
487
502
|
headers,
|
package/dist/index.d.ts
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
* `UIMessage` is re-exported from `ai`, never redeclared.
|
|
5
5
|
*/
|
|
6
6
|
export type { UIMessage } from "ai";
|
|
7
|
-
export { BlazingAgentsChatTransport, type BlazingAgentsChatTransportOptions, } from "./chat-transport.ts";
|
|
7
|
+
export { BlazingAgentsChatTransport, type BlazingAgentsChatTransportOptions, type SteerOutcome, } from "./chat-transport.ts";
|
|
8
8
|
export { BlazingAgents } from "./client.ts";
|
|
9
9
|
export type { ApprovalDecision, ApprovalPolicy, ToolExecutionReference, ToolReference, } from "./contracts/entities/agent-approval.ts";
|
|
10
10
|
export { AGENT_TOOL_FILE_ACCESS_TAGS, AGENT_TOOL_GROUPS, AGENT_TOOL_TAGS, AGENT_TOOLS, type AgentToolDefinition, type AgentToolFileAccessTag, type AgentToolGroupTag, type AgentToolName, type AgentToolTag, type AgentToolTags, } from "./contracts/entities/agent-tools.ts";
|
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The browser chat's policy as one pure function, `step(state, event)`. The
|
|
3
|
+
* hook owns the only mutable copy of `ChatState`, feeds it events from `Chat`
|
|
4
|
+
* and the transport, and runs the effects it returns. `Chat` owns the rendered
|
|
5
|
+
* transcript and the transport owns the Session ID; this module owns what the
|
|
6
|
+
* server is known to store, the phase, and the user messages not stored yet.
|
|
7
|
+
*/
|
|
8
|
+
import { type UIMessage } from "ai";
|
|
9
|
+
import type { SteerOutcome } from "../chat-transport.ts";
|
|
10
|
+
export type UserMessage<M extends UIMessage> = M & {
|
|
11
|
+
role: "user";
|
|
12
|
+
};
|
|
13
|
+
/** One `send()` call. `key` names its promise and survives the fresh message ID a resend gets. */
|
|
14
|
+
export interface Sent<M extends UIMessage> {
|
|
15
|
+
key: string;
|
|
16
|
+
message: UserMessage<M>;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* A sent user message the chat answers for until it is stored or rolled
|
|
20
|
+
* back. Stages only move forward, except that a message the Turn did not take
|
|
21
|
+
* goes back to `waiting` with a fresh message ID.
|
|
22
|
+
*/
|
|
23
|
+
export type Outgoing<M extends UIMessage> = Sent<M> & ({
|
|
24
|
+
stage: "waiting";
|
|
25
|
+
}
|
|
26
|
+
/** Its Turn ended without `finish`; stored history decides whether it landed. */
|
|
27
|
+
| {
|
|
28
|
+
stage: "unconfirmed";
|
|
29
|
+
} | {
|
|
30
|
+
/**
|
|
31
|
+
* `steering`: the steer request is out. `accepted`: the relay took it,
|
|
32
|
+
* or its answer was lost, and the Turn may still read it. `refused`:
|
|
33
|
+
* this Turn will not take it, so it waits for the Turn to end.
|
|
34
|
+
* `consumed`: the Turn read it; it is stored when the Turn completes.
|
|
35
|
+
*/
|
|
36
|
+
stage: "steering" | "accepted" | "refused" | "consumed";
|
|
37
|
+
requestId: string;
|
|
38
|
+
});
|
|
39
|
+
export type Phase<M extends UIMessage> =
|
|
40
|
+
/**
|
|
41
|
+
* Reading the stored transcript. `load` is what happens next: `running`
|
|
42
|
+
* waits for the answer, `stale` reads again if the answer fails because
|
|
43
|
+
* the credentials changed meanwhile, `failed` reads again on new
|
|
44
|
+
* credentials, a send, or `clearError`.
|
|
45
|
+
*/
|
|
46
|
+
{
|
|
47
|
+
kind: "restoring";
|
|
48
|
+
load: "running" | "stale" | "failed";
|
|
49
|
+
} | {
|
|
50
|
+
kind: "idle";
|
|
51
|
+
}
|
|
52
|
+
/** A chat request is out and the Turn is not live yet, so a steer would be refused. `prompt` is null for a regenerate or an approval continuation. */
|
|
53
|
+
| {
|
|
54
|
+
kind: "starting";
|
|
55
|
+
prompt: Sent<M> | null;
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* The relay answered with a stream: the Turn is live and takes steers.
|
|
59
|
+
* `finished`: the stream carried its `finish` chunk.
|
|
60
|
+
*/
|
|
61
|
+
| {
|
|
62
|
+
kind: "streaming";
|
|
63
|
+
prompt: Sent<M> | null;
|
|
64
|
+
finished: boolean;
|
|
65
|
+
}
|
|
66
|
+
/** Stored history is read again to place read steers, or to learn what a Turn that ended without `finish` stored. */
|
|
67
|
+
| {
|
|
68
|
+
kind: "reconciling";
|
|
69
|
+
};
|
|
70
|
+
export interface ChatState<M extends UIMessage> {
|
|
71
|
+
/** The transcript the server is known to store. The rollback target. */
|
|
72
|
+
completed: M[];
|
|
73
|
+
error: Error | undefined;
|
|
74
|
+
/** Oldest first: every sent message not stored yet, except the running Turn's prompt. */
|
|
75
|
+
outbox: Outgoing<M>[];
|
|
76
|
+
phase: Phase<M>;
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* How `Chat`'s request ended. `closed`: without an error or a Stop; the Turn
|
|
80
|
+
* completed only if its stream carried `finish`. `sessionGone`: the relay
|
|
81
|
+
* answered 404, so the Session is deleted or another End-user's.
|
|
82
|
+
*/
|
|
83
|
+
export type TurnResult<M extends UIMessage> = {
|
|
84
|
+
outcome: "closed";
|
|
85
|
+
messages: M[];
|
|
86
|
+
} | {
|
|
87
|
+
outcome: "stopped";
|
|
88
|
+
} | {
|
|
89
|
+
outcome: "failed";
|
|
90
|
+
error: Error;
|
|
91
|
+
sessionGone: boolean;
|
|
92
|
+
};
|
|
93
|
+
export type ChatEvent<M extends UIMessage> =
|
|
94
|
+
/** Read the stored transcript; the hook sends it once per mount. */
|
|
95
|
+
{
|
|
96
|
+
type: "restore";
|
|
97
|
+
} | {
|
|
98
|
+
type: "credentialsChanged";
|
|
99
|
+
} | {
|
|
100
|
+
type: "historyLoaded";
|
|
101
|
+
messages: M[];
|
|
102
|
+
}
|
|
103
|
+
/** `sessionGone`: the relay answered 404, so the Session is deleted or another End-user's. */
|
|
104
|
+
| {
|
|
105
|
+
type: "historyFailed";
|
|
106
|
+
error: Error;
|
|
107
|
+
sessionGone: boolean;
|
|
108
|
+
} | {
|
|
109
|
+
type: "send";
|
|
110
|
+
sent: Sent<M>;
|
|
111
|
+
} | {
|
|
112
|
+
type: "regenerate";
|
|
113
|
+
}
|
|
114
|
+
/** The user completed an approval round; `Chat` sends the continuation itself. */
|
|
115
|
+
| {
|
|
116
|
+
type: "approvalAnswered";
|
|
117
|
+
} | {
|
|
118
|
+
type: "turnStarted";
|
|
119
|
+
}
|
|
120
|
+
/** The stream carried its `finish` chunk. */
|
|
121
|
+
| {
|
|
122
|
+
type: "turnFinished";
|
|
123
|
+
}
|
|
124
|
+
/** `Chat`'s request ended, however it ended. */
|
|
125
|
+
| {
|
|
126
|
+
type: "turnEnded";
|
|
127
|
+
result: TurnResult<M>;
|
|
128
|
+
} | {
|
|
129
|
+
type: "steerAnswered";
|
|
130
|
+
requestId: string;
|
|
131
|
+
outcome: SteerOutcome;
|
|
132
|
+
}
|
|
133
|
+
/** `answerLost`: the request failed in transit, so the relay may have taken it. */
|
|
134
|
+
| {
|
|
135
|
+
type: "steerFailed";
|
|
136
|
+
requestId: string;
|
|
137
|
+
error: Error;
|
|
138
|
+
answerLost: boolean;
|
|
139
|
+
} | {
|
|
140
|
+
type: "steerConsumed";
|
|
141
|
+
requestId: string;
|
|
142
|
+
} | {
|
|
143
|
+
type: "stop";
|
|
144
|
+
} | {
|
|
145
|
+
type: "newSession";
|
|
146
|
+
} | {
|
|
147
|
+
type: "unmount";
|
|
148
|
+
} | {
|
|
149
|
+
type: "clearError";
|
|
150
|
+
};
|
|
151
|
+
export type ChatEffect<M extends UIMessage> = {
|
|
152
|
+
type: "startTurn";
|
|
153
|
+
message: UserMessage<M>;
|
|
154
|
+
} | {
|
|
155
|
+
type: "regenerate";
|
|
156
|
+
} | {
|
|
157
|
+
type: "submitSteer";
|
|
158
|
+
message: UserMessage<M>;
|
|
159
|
+
requestId: string;
|
|
160
|
+
} | {
|
|
161
|
+
type: "show";
|
|
162
|
+
messages: M[];
|
|
163
|
+
} | {
|
|
164
|
+
type: "loadHistory";
|
|
165
|
+
} | {
|
|
166
|
+
type: "abort";
|
|
167
|
+
}
|
|
168
|
+
/** Forget the stored Session and open a new conversation without one. */
|
|
169
|
+
| {
|
|
170
|
+
type: "forgetSession";
|
|
171
|
+
}
|
|
172
|
+
/** Resolve the `send()` promise named by `key`. */
|
|
173
|
+
| {
|
|
174
|
+
type: "settle";
|
|
175
|
+
key: string;
|
|
176
|
+
landed: boolean;
|
|
177
|
+
};
|
|
178
|
+
export declare const initialChatState: ChatState<never>;
|
|
179
|
+
interface Step<M extends UIMessage> {
|
|
180
|
+
effects: ChatEffect<M>[];
|
|
181
|
+
state: ChatState<M>;
|
|
182
|
+
}
|
|
183
|
+
/**
|
|
184
|
+
* Applies one event, then the `drain` rule. Pure and total: an event that
|
|
185
|
+
* does not apply to the current phase, such as a late steer answer after a
|
|
186
|
+
* rollback, leaves the state unchanged.
|
|
187
|
+
* @param state - Current state.
|
|
188
|
+
* @param event - What happened.
|
|
189
|
+
* @param newId - Mints steer request IDs and the fresh message ID a resend gets.
|
|
190
|
+
* @returns The next state and the effects to run, in order.
|
|
191
|
+
*/
|
|
192
|
+
export declare function step<M extends UIMessage>(state: ChatState<M>, event: ChatEvent<M>, newId: () => string): Step<M>;
|
|
193
|
+
/** True when idle with a stored assistant reply to replace. */
|
|
194
|
+
export declare function canRegenerate<M extends UIMessage>(state: ChatState<M>): boolean;
|
|
195
|
+
/** IDs of the messages sent but not stored yet, the running prompt included. */
|
|
196
|
+
export declare function pendingIds<M extends UIMessage>(state: ChatState<M>): Set<string>;
|
|
197
|
+
export {};
|