@better-agent/client 0.1.0-canary.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 +3 -0
- package/dist/controller-CJ79_cSR.d.mts +657 -0
- package/dist/controller-CJ79_cSR.d.mts.map +1 -0
- package/dist/controller-Cf_JhTdJ.mjs +2123 -0
- package/dist/controller-Cf_JhTdJ.mjs.map +1 -0
- package/dist/index.d.mts +266 -0
- package/dist/index.d.mts.map +1 -0
- package/dist/index.mjs +643 -0
- package/dist/index.mjs.map +1 -0
- package/dist/preact/index.d.mts +96 -0
- package/dist/preact/index.d.mts.map +1 -0
- package/dist/preact/index.mjs +134 -0
- package/dist/preact/index.mjs.map +1 -0
- package/dist/react/index.d.mts +96 -0
- package/dist/react/index.d.mts.map +1 -0
- package/dist/react/index.mjs +123 -0
- package/dist/react/index.mjs.map +1 -0
- package/dist/solid/index.d.mts +99 -0
- package/dist/solid/index.d.mts.map +1 -0
- package/dist/solid/index.mjs +130 -0
- package/dist/solid/index.mjs.map +1 -0
- package/dist/svelte/index.d.mts +83 -0
- package/dist/svelte/index.d.mts.map +1 -0
- package/dist/svelte/index.mjs +107 -0
- package/dist/svelte/index.mjs.map +1 -0
- package/dist/utils-CiHUj_BW.mjs +34 -0
- package/dist/utils-CiHUj_BW.mjs.map +1 -0
- package/dist/vue/index.d.mts +95 -0
- package/dist/vue/index.d.mts.map +1 -0
- package/dist/vue/index.mjs +155 -0
- package/dist/vue/index.mjs.map +1 -0
- package/package.json +117 -0
package/README.md
ADDED
|
@@ -0,0 +1,657 @@
|
|
|
1
|
+
import { Event } from "@better-agent/core/events";
|
|
2
|
+
import { BetterAgentApp, ConversationReplayOptions, InferSchemaInput, RunAdvancedOptions, RunResult, SubmitToolApprovalParams, SubmitToolResultParams, ToolTarget } from "@better-agent/core";
|
|
3
|
+
import { AudioContentBase, Capabilities, ConversationItem, EmbeddingContentBase, FileContentBase, GenerativeModel, GenerativeModelInput, GenerativeModelInputItem, GenerativeModelResponse, ImageContentBase, InputEnabled, ModalitiesParam, Modality, ModalityOptionsFor, ModelOptions, OutputSchemaForCaps, TextContentBase, VideoContentBase } from "@better-agent/core/providers";
|
|
4
|
+
|
|
5
|
+
//#region src/types/client-type-helpers.d.ts
|
|
6
|
+
type InferPortableOrSchemaInput<TValue> = TValue extends {
|
|
7
|
+
"~standard"?: {
|
|
8
|
+
types?: {
|
|
9
|
+
input?: unknown;
|
|
10
|
+
};
|
|
11
|
+
};
|
|
12
|
+
} ? InferSchemaInput<TValue> : TValue extends {
|
|
13
|
+
type: unknown;
|
|
14
|
+
} ? InferSchemaInput<TValue> : TValue;
|
|
15
|
+
type InferPortableOrSchemaOutput<TValue> = TValue extends {
|
|
16
|
+
schema: infer TSchema;
|
|
17
|
+
} ? InferPortableOrSchemaInput<NonNullable<TSchema>> : TValue;
|
|
18
|
+
/** Extracts `config.agents` from the typed app config. */
|
|
19
|
+
type AgentsFromApp<TApp> = TApp extends {
|
|
20
|
+
__appTypes?: {
|
|
21
|
+
agents: infer TAgents;
|
|
22
|
+
};
|
|
23
|
+
} ? TAgents extends readonly {
|
|
24
|
+
name: string;
|
|
25
|
+
}[] ? TAgents : never : TApp extends {
|
|
26
|
+
config: {
|
|
27
|
+
agents: infer TAgents;
|
|
28
|
+
};
|
|
29
|
+
} ? TAgents extends readonly {
|
|
30
|
+
name: string;
|
|
31
|
+
}[] ? TAgents : never : TApp extends BetterAgentApp<infer TAgents, infer _TPlugins> ? TAgents extends readonly {
|
|
32
|
+
name: string;
|
|
33
|
+
}[] ? TAgents : never : never;
|
|
34
|
+
/**
|
|
35
|
+
* Agent names available from the typed server app.
|
|
36
|
+
*
|
|
37
|
+
* Falls back to `string` for untyped clients.
|
|
38
|
+
*/
|
|
39
|
+
type AgentNameFromApp<TApp> = [AgentsFromApp<TApp>] extends [never] ? string : AgentsFromApp<TApp>[number]["name"];
|
|
40
|
+
type AgentsRawFromApp<TApp> = TApp extends {
|
|
41
|
+
__appTypes?: {
|
|
42
|
+
agents: infer TAgents;
|
|
43
|
+
};
|
|
44
|
+
} ? TAgents extends readonly unknown[] ? TAgents : never : TApp extends {
|
|
45
|
+
config: {
|
|
46
|
+
agents: infer TAgents;
|
|
47
|
+
};
|
|
48
|
+
} ? TAgents extends readonly unknown[] ? TAgents : never : TApp extends BetterAgentApp<infer TAgents, infer _TPlugins> ? TAgents extends readonly unknown[] ? TAgents : never : never;
|
|
49
|
+
type AppToolsArrayFromApp<TApp> = TApp extends {
|
|
50
|
+
__appTypes?: {
|
|
51
|
+
tools: infer TTools extends readonly unknown[];
|
|
52
|
+
};
|
|
53
|
+
} ? TTools : TApp extends {
|
|
54
|
+
config: {
|
|
55
|
+
tools?: infer TTools extends readonly unknown[];
|
|
56
|
+
};
|
|
57
|
+
} ? TTools : never;
|
|
58
|
+
type SlimAgentForClient<TAgent> = TAgent extends {
|
|
59
|
+
name: infer TName extends string;
|
|
60
|
+
} ? {
|
|
61
|
+
name: TName;
|
|
62
|
+
} & (TAgent extends {
|
|
63
|
+
model: infer TModel;
|
|
64
|
+
} ? {
|
|
65
|
+
model: TModel;
|
|
66
|
+
} : Record<never, never>) & {} & (TAgent extends {
|
|
67
|
+
contextSchema: infer TContextSchema;
|
|
68
|
+
} ? {
|
|
69
|
+
contextSchema: TContextSchema;
|
|
70
|
+
} : Record<never, never>) & (TAgent extends {
|
|
71
|
+
outputSchema: infer TOutputSchema;
|
|
72
|
+
} ? {
|
|
73
|
+
outputSchema: TOutputSchema;
|
|
74
|
+
} : Record<never, never>) & (TAgent extends {
|
|
75
|
+
execution?: infer TExecution;
|
|
76
|
+
} ? {
|
|
77
|
+
execution: TExecution;
|
|
78
|
+
} : Record<never, never>) & (TAgent extends {
|
|
79
|
+
tools?: infer TTools;
|
|
80
|
+
} ? {
|
|
81
|
+
tools: TTools;
|
|
82
|
+
} : Record<never, never>) : never;
|
|
83
|
+
type SlimAgentsForClient<TAgents extends readonly unknown[]> = { readonly [TIndex in keyof TAgents]: SlimAgentForClient<TAgents[TIndex]> };
|
|
84
|
+
/** Normalized app shape used for client-side type inference. */
|
|
85
|
+
type NormalizeClientApp<TApp> = [AgentsRawFromApp<TApp>] extends [never] ? unknown : {
|
|
86
|
+
config: {
|
|
87
|
+
agents: SlimAgentsForClient<AgentsRawFromApp<TApp>>;
|
|
88
|
+
tools?: AppToolsArrayFromApp<TApp>;
|
|
89
|
+
};
|
|
90
|
+
};
|
|
91
|
+
type AppLevelToolsFromApp<TApp> = TApp extends {
|
|
92
|
+
__appTypes?: {
|
|
93
|
+
tools: infer TTools extends readonly unknown[];
|
|
94
|
+
};
|
|
95
|
+
} ? TTools[number] : TApp extends {
|
|
96
|
+
config: {
|
|
97
|
+
tools?: infer TTools extends readonly unknown[];
|
|
98
|
+
};
|
|
99
|
+
} ? TTools[number] : never;
|
|
100
|
+
type AgentStaticToolsFromAgent<TAgent> = TAgent extends {
|
|
101
|
+
tools?: infer TTools;
|
|
102
|
+
} ? TTools extends readonly unknown[] ? TTools[number] : never : never;
|
|
103
|
+
type AgentStaticToolsFromApp<TApp> = [AgentsFromApp<TApp>] extends [never] ? never : AgentStaticToolsFromAgent<AgentsRawFromApp<TApp>[number]>;
|
|
104
|
+
type ClientToolFromApp<TApp> = Extract<AppLevelToolsFromApp<TApp> | AgentStaticToolsFromApp<TApp>, {
|
|
105
|
+
kind: "client";
|
|
106
|
+
name: string;
|
|
107
|
+
schema: unknown;
|
|
108
|
+
}>;
|
|
109
|
+
/** Keeps only named client tools with literal names. */
|
|
110
|
+
type LiteralClientToolFromApp<TApp> = ClientToolFromApp<TApp> extends infer TTool ? TTool extends {
|
|
111
|
+
kind: "client";
|
|
112
|
+
name: string;
|
|
113
|
+
schema: unknown;
|
|
114
|
+
} ? string extends TTool["name"] ? never : TTool : never : never;
|
|
115
|
+
type KnownToolNameFromApp<TApp> = LiteralClientToolFromApp<TApp>["name"];
|
|
116
|
+
type ToolNameFromApp<TApp> = [KnownToolNameFromApp<TApp>] extends [never] ? string : KnownToolNameFromApp<TApp>;
|
|
117
|
+
type ToolByName<TApp, TToolName extends ToolNameFromApp<TApp>> = Extract<LiteralClientToolFromApp<TApp>, {
|
|
118
|
+
name: TToolName;
|
|
119
|
+
}>;
|
|
120
|
+
/** Input type for a specific client tool. */
|
|
121
|
+
type ToolInputFromApp<TApp, TToolName extends ToolNameFromApp<TApp>> = [ToolByName<TApp, TToolName>] extends [never] ? unknown : ToolByName<TApp, TToolName>["schema"] extends infer TSchema ? TSchema extends object ? InferPortableOrSchemaInput<TSchema> : unknown : unknown;
|
|
122
|
+
/** Client tool names available from the typed server app. */
|
|
123
|
+
type ToolNameForApp<TApp> = ToolNameFromApp<TApp>;
|
|
124
|
+
type UntypedRunInput = {
|
|
125
|
+
input: unknown;
|
|
126
|
+
context?: unknown;
|
|
127
|
+
output?: unknown;
|
|
128
|
+
modalities?: readonly Modality[];
|
|
129
|
+
modelOptions?: Record<string, unknown>;
|
|
130
|
+
maxSteps?: number;
|
|
131
|
+
conversationId?: string;
|
|
132
|
+
[key: string]: unknown;
|
|
133
|
+
};
|
|
134
|
+
type AgentByNameFromApp<TApp, TAgentName extends AgentNameFromApp<TApp>> = Extract<AgentsFromApp<TApp>[number], {
|
|
135
|
+
name: TAgentName;
|
|
136
|
+
}>;
|
|
137
|
+
type AgentFromApp<TApp, TAgentName extends AgentNameFromApp<TApp>> = AgentByNameFromApp<TApp, TAgentName>;
|
|
138
|
+
type AgentModelFromApp<TApp, TAgentName extends AgentNameFromApp<TApp>> = AgentFromApp<TApp, TAgentName> extends {
|
|
139
|
+
model: infer TModel;
|
|
140
|
+
} ? TModel : never;
|
|
141
|
+
type AgentOutputSchemaFromApp<TApp, TAgentName extends AgentNameFromApp<TApp>> = AgentFromApp<TApp, TAgentName> extends {
|
|
142
|
+
outputSchema: infer TOutputSchema;
|
|
143
|
+
} ? TOutputSchema : never;
|
|
144
|
+
type AgentCapsFromApp<TApp, TAgentName extends AgentNameFromApp<TApp>> = AgentModelFromApp<TApp, TAgentName> extends {
|
|
145
|
+
caps: infer TCaps extends Capabilities;
|
|
146
|
+
} ? TCaps : Capabilities;
|
|
147
|
+
type AgentModelOptionsFromApp<TApp, TAgentName extends AgentNameFromApp<TApp>> = AgentModelFromApp<TApp, TAgentName> extends GenerativeModel<infer TOptions, infer _TProviderId, infer _TModelId, infer _TCaps> ? ModelOptions<TOptions> : AgentModelFromApp<TApp, TAgentName> extends {
|
|
148
|
+
options: infer TOptions extends Record<string, unknown>;
|
|
149
|
+
} ? ModelOptions<TOptions> : Record<never, never>;
|
|
150
|
+
type AgentOutputOverrideForApp<TApp, TAgentName extends AgentNameFromApp<TApp>> = OutputSchemaForCaps<AgentCapsFromApp<TApp, TAgentName>>;
|
|
151
|
+
type TypedModalitiesForAgent<TApp, TAgentName extends AgentNameFromApp<TApp>> = ModalitiesParam<AgentCapsFromApp<TApp, TAgentName>>;
|
|
152
|
+
type ModalitiesForAgent<TApp, TAgentName extends AgentNameFromApp<TApp>> = [AgentsFromApp<TApp>] extends [never] ? readonly Modality[] : TypedModalitiesForAgent<TApp, TAgentName>;
|
|
153
|
+
type TextInputShorthandForAgent<TApp, TAgentName extends AgentNameFromApp<TApp>> = [AgentsFromApp<TApp>] extends [never] ? string : InputEnabled<AgentCapsFromApp<TApp, TAgentName>, "text"> extends true ? string : never;
|
|
154
|
+
type StructuredOutputForAgent<TApp, TAgentName extends AgentNameFromApp<TApp>, TOutput> = [AgentsFromApp<TApp>] extends [never] ? unknown : [TOutput] extends [undefined] ? InferPortableOrSchemaOutput<AgentOutputSchemaFromApp<TApp, TAgentName>> : InferPortableOrSchemaOutput<TOutput>;
|
|
155
|
+
type DefaultStructuredOutputForAgent<TApp, TAgentName extends AgentNameFromApp<TApp>> = StructuredOutputForAgent<TApp, TAgentName, undefined>;
|
|
156
|
+
type StructuredRunResult<TStructured> = Omit<RunResult<TStructured>, "structured"> & {
|
|
157
|
+
structured: TStructured;
|
|
158
|
+
};
|
|
159
|
+
type UnstructuredRunResult = Omit<RunResult<never>, "structured">;
|
|
160
|
+
type TypedContextInput<TApp, TAgentName extends AgentNameFromApp<TApp>> = [AgentsFromApp<TApp>] extends [never] ? {
|
|
161
|
+
context?: unknown;
|
|
162
|
+
} : AgentByNameFromApp<TApp, TAgentName> extends {
|
|
163
|
+
contextSchema: infer TContextSchema;
|
|
164
|
+
} ? {
|
|
165
|
+
context: InferPortableOrSchemaInput<TContextSchema>;
|
|
166
|
+
} : {
|
|
167
|
+
context?: never;
|
|
168
|
+
};
|
|
169
|
+
type TypedRunnerInputForAgent<TApp, TAgentName extends AgentNameFromApp<TApp>, TModalities extends TypedModalitiesForAgent<TApp, TAgentName> | undefined = undefined> = [AgentsFromApp<TApp>] extends [never] ? UntypedRunInput : {
|
|
170
|
+
input: GenerativeModelInput<AgentCapsFromApp<TApp, TAgentName>>;
|
|
171
|
+
modalities?: TModalities;
|
|
172
|
+
modelOptions?: AgentModelOptionsFromApp<TApp, TAgentName> & ModalityOptionsFor<AgentCapsFromApp<TApp, TAgentName>, TModalities> & Record<string, unknown>;
|
|
173
|
+
conversationId?: string;
|
|
174
|
+
conversationReplay?: ConversationReplayOptions;
|
|
175
|
+
replaceHistory?: boolean;
|
|
176
|
+
maxSteps?: number;
|
|
177
|
+
advanced?: RunAdvancedOptions;
|
|
178
|
+
} & TypedContextInput<TApp, TAgentName>;
|
|
179
|
+
/**
|
|
180
|
+
* Typed run input for one agent.
|
|
181
|
+
*
|
|
182
|
+
* This excludes `onClientToolCall`, client-side tool handling is configured through stream request options.
|
|
183
|
+
*/
|
|
184
|
+
type RunInputForAgent<TApp = unknown, TAgentName extends AgentNameFromApp<TApp> = AgentNameFromApp<TApp>, TModalities = undefined> = [AgentsFromApp<TApp>] extends [never] ? UntypedRunInput : TypedRunnerInputForAgent<TApp, TAgentName, Extract<TModalities, TypedModalitiesForAgent<TApp, TAgentName> | undefined>>;
|
|
185
|
+
type RunResultForAgent<TApp, TAgentName extends AgentNameFromApp<TApp>, TOutput> = [AgentsFromApp<TApp>] extends [never] ? RunResult<unknown> : [StructuredOutputForAgent<TApp, TAgentName, Extract<TOutput, AgentOutputOverrideForApp<TApp, TAgentName> | undefined>>] extends [never] ? UnstructuredRunResult : StructuredRunResult<StructuredOutputForAgent<TApp, TAgentName, Extract<TOutput, AgentOutputOverrideForApp<TApp, TAgentName> | undefined>>>;
|
|
186
|
+
type AgentRunInput<TApp, TAgentName extends AgentNameFromApp<TApp>> = RunInputForAgent<TApp, TAgentName>;
|
|
187
|
+
type AgentContext<TApp, TAgentName extends AgentNameFromApp<TApp>> = AgentByNameFromApp<TApp, TAgentName> extends {
|
|
188
|
+
contextSchema: infer TContextSchema;
|
|
189
|
+
} ? InferPortableOrSchemaInput<TContextSchema> : [AgentsFromApp<TApp>] extends [never] ? unknown : never;
|
|
190
|
+
type AgentContextInputField<TApp, TAgentName extends AgentNameFromApp<TApp>> = [AgentsFromApp<TApp>] extends [never] ? {
|
|
191
|
+
context?: unknown;
|
|
192
|
+
} : AgentByNameFromApp<TApp, TAgentName> extends {
|
|
193
|
+
contextSchema: infer TContextSchema;
|
|
194
|
+
} ? {
|
|
195
|
+
context: InferPortableOrSchemaInput<TContextSchema>;
|
|
196
|
+
} : {
|
|
197
|
+
context?: AgentContext<TApp, TAgentName>;
|
|
198
|
+
};
|
|
199
|
+
//#endregion
|
|
200
|
+
//#region src/types/client.d.ts
|
|
201
|
+
/** Configuration for `createClient`. */
|
|
202
|
+
interface ClientConfig<TApp = unknown> {
|
|
203
|
+
/** Better Agent API base URL. */
|
|
204
|
+
baseURL: string;
|
|
205
|
+
/** Better Agent auth secret, when the server requires bearer auth. */
|
|
206
|
+
secret?: string;
|
|
207
|
+
/** Default request headers. */
|
|
208
|
+
headers?: Headers | Record<string, string>;
|
|
209
|
+
/** Custom fetch implementation. */
|
|
210
|
+
fetch?: typeof fetch;
|
|
211
|
+
/** Default client tool handlers. */
|
|
212
|
+
toolHandlers?: ToolHandlers<TApp>;
|
|
213
|
+
/** Advanced options. */
|
|
214
|
+
advanced?: {
|
|
215
|
+
/** Rewrites outgoing requests. */prepareRequest?: (context: PrepareRequestContext) => PrepareRequestResult | undefined | Promise<PrepareRequestResult | undefined>; /** Max retries for tool results and approvals. */
|
|
216
|
+
toolSubmissionMaxAttempts?: number; /** Base retry delay for tool results and approvals. */
|
|
217
|
+
toolSubmissionRetryDelayMs?: number;
|
|
218
|
+
};
|
|
219
|
+
}
|
|
220
|
+
/** Standard HTTP request methods. */
|
|
221
|
+
type HttpMethod = "GET" | "HEAD" | "POST" | "PUT" | "DELETE" | "CONNECT" | "OPTIONS" | "TRACE" | "PATCH";
|
|
222
|
+
/** Context passed to `prepareRequest`. */
|
|
223
|
+
type PrepareRequestContext = {
|
|
224
|
+
/** Request operation. */operation: "run" | "stream" | "resume-stream" | "resume-conversation" | "load-conversation" | "abort-run" | "tool-result" | "tool-approval"; /** Request URL before overrides. */
|
|
225
|
+
url: string; /** Request method. */
|
|
226
|
+
method: HttpMethod; /** Request headers before overrides. */
|
|
227
|
+
headers: Headers | Record<string, string>; /** Request body, when present. */
|
|
228
|
+
body?: BodyInit | null;
|
|
229
|
+
};
|
|
230
|
+
/** Overrides returned by `prepareRequest`. */
|
|
231
|
+
type PrepareRequestResult = {
|
|
232
|
+
/** URL override. */url?: string; /** Method override. */
|
|
233
|
+
method?: HttpMethod; /** Headers override. */
|
|
234
|
+
headers?: Headers | Record<string, string>; /** Body override. */
|
|
235
|
+
body?: BodyInit | null;
|
|
236
|
+
};
|
|
237
|
+
/** Per-request transport options. */
|
|
238
|
+
interface RequestOptions<_TApp = unknown> {
|
|
239
|
+
/** Request-scoped headers. */
|
|
240
|
+
headers?: Headers | Record<string, string>;
|
|
241
|
+
/** Abort signal. */
|
|
242
|
+
signal?: AbortSignal | null;
|
|
243
|
+
/** Called after the HTTP response is received. */
|
|
244
|
+
onResponse?: (response: Response) => void;
|
|
245
|
+
}
|
|
246
|
+
/** Options for streaming requests. */
|
|
247
|
+
interface StreamRequestOptions<TApp = unknown> extends RequestOptions<TApp> {
|
|
248
|
+
/** Request-scoped function tool handler. */
|
|
249
|
+
onToolCall?: OnToolCall<TApp>;
|
|
250
|
+
/** Request-scoped map tool handlers. */
|
|
251
|
+
toolHandlers?: ToolHandlers<TApp>;
|
|
252
|
+
}
|
|
253
|
+
/** Request payload for `submitToolResult`. */
|
|
254
|
+
type SubmitToolResultRequest<TApp = unknown> = {
|
|
255
|
+
agent: AgentNameFromApp<TApp>;
|
|
256
|
+
} & SubmitToolResultParams;
|
|
257
|
+
/** Request payload for `submitToolApproval`. */
|
|
258
|
+
type SubmitToolApprovalRequest<TApp = unknown> = {
|
|
259
|
+
agent: AgentNameFromApp<TApp>;
|
|
260
|
+
} & SubmitToolApprovalParams;
|
|
261
|
+
/** Request payload for `abortRun`. */
|
|
262
|
+
type AbortRunRequest<TApp = unknown> = {
|
|
263
|
+
/** Agent name. */agent: AgentNameFromApp<TApp>; /** Run id. */
|
|
264
|
+
runId: string;
|
|
265
|
+
};
|
|
266
|
+
interface BetterAgentClient<TApp = unknown> {
|
|
267
|
+
/**
|
|
268
|
+
* Runs an agent and returns the final response.
|
|
269
|
+
*
|
|
270
|
+
* Use `stream()` when the agent may use client tools or approvals.
|
|
271
|
+
*/
|
|
272
|
+
run<TAgentName extends AgentNameFromApp<TApp>, const TModalities extends ModalitiesForAgent<TApp, TAgentName> | undefined = undefined, TOutput = undefined>(agent: TAgentName, input: RunInputForAgent<TApp, TAgentName, TModalities> & {
|
|
273
|
+
output?: TOutput;
|
|
274
|
+
}, options?: RequestOptions<TApp>): Promise<RunResultForAgent<TApp, TAgentName, Extract<TOutput, unknown>>>;
|
|
275
|
+
/** Runs an agent and streams events. */
|
|
276
|
+
stream<TAgentName extends AgentNameFromApp<TApp>, const TModalities extends ModalitiesForAgent<TApp, TAgentName> | undefined = undefined>(agent: TAgentName, input: RunInputForAgent<TApp, TAgentName, TModalities>, options?: StreamRequestOptions<TApp>): AsyncIterable<ClientEvent>;
|
|
277
|
+
/** Resumes one stream. */
|
|
278
|
+
resumeStream<TAgentName extends AgentNameFromApp<TApp>>(agent: TAgentName, input: ReplayInput, options?: RequestOptions<TApp>): AsyncIterable<ClientEvent>;
|
|
279
|
+
/** Resumes the active stream for a conversation. */
|
|
280
|
+
resumeConversation<TAgentName extends AgentNameFromApp<TApp>>(agent: TAgentName, input: {
|
|
281
|
+
conversationId: string;
|
|
282
|
+
afterSeq?: number;
|
|
283
|
+
}, options?: RequestOptions<TApp>): AsyncIterable<ClientEvent>;
|
|
284
|
+
/**
|
|
285
|
+
* Loads persisted conversation items.
|
|
286
|
+
*
|
|
287
|
+
* Returns `null` when no stored history exists.
|
|
288
|
+
*/
|
|
289
|
+
loadConversation?(agent: AgentNameFromApp<TApp>, conversationId: string, options?: RequestOptions<TApp>): Promise<{
|
|
290
|
+
items: ConversationItem[];
|
|
291
|
+
} | null>;
|
|
292
|
+
/** Aborts an active run. */
|
|
293
|
+
abortRun(req: AbortRunRequest<TApp>, options?: RequestOptions<TApp>): Promise<void>;
|
|
294
|
+
/** Submits a client tool result. */
|
|
295
|
+
submitToolResult(req: SubmitToolResultRequest<TApp>): Promise<void>;
|
|
296
|
+
/** Submits a tool approval decision. */
|
|
297
|
+
submitToolApproval(req: SubmitToolApprovalRequest<TApp>): Promise<void>;
|
|
298
|
+
}
|
|
299
|
+
/** Context passed to client tool handlers. */
|
|
300
|
+
type ToolCallContext<TApp = unknown> = {
|
|
301
|
+
/** Agent name. */agent: AgentNameFromApp<TApp>; /** Run id. */
|
|
302
|
+
runId: string; /** Tool-call id. */
|
|
303
|
+
toolCallId: string; /** Aborts when the run ends or disconnects. */
|
|
304
|
+
signal?: AbortSignal;
|
|
305
|
+
};
|
|
306
|
+
/**
|
|
307
|
+
* Map of tool handlers keyed by tool name.
|
|
308
|
+
*/
|
|
309
|
+
type ToolHandlers<TApp = unknown> = Partial<{ [TToolName in ToolNameForApp<TApp>]: (input: ToolInputFromApp<TApp, TToolName>, context: ToolCallContext<TApp>) => unknown | Promise<unknown> }>;
|
|
310
|
+
/**
|
|
311
|
+
* Tool-call payload for function handlers.
|
|
312
|
+
*/
|
|
313
|
+
type ToolCallRequest<TApp = unknown> = {
|
|
314
|
+
/** Tool name. */toolName: ToolNameForApp<TApp>; /** Tool input. */
|
|
315
|
+
input: unknown; /** Tool call context. */
|
|
316
|
+
context: ToolCallContext<TApp>;
|
|
317
|
+
};
|
|
318
|
+
/** Function form for handling client tool calls. */
|
|
319
|
+
type OnToolCall<TApp = unknown> = (params: ToolCallRequest<TApp>) => unknown | Promise<unknown>;
|
|
320
|
+
/** Input for replay and resume APIs. */
|
|
321
|
+
type ReplayInput = {
|
|
322
|
+
streamId: string;
|
|
323
|
+
/**
|
|
324
|
+
* Cursor to continue after.
|
|
325
|
+
*
|
|
326
|
+
* Use `-1` to resume from the start.
|
|
327
|
+
*/
|
|
328
|
+
afterSeq?: number;
|
|
329
|
+
};
|
|
330
|
+
/**
|
|
331
|
+
* Events emitted by client streams.
|
|
332
|
+
*
|
|
333
|
+
* `seq`, `streamId`, and `runId` are added by the client transport when needed.
|
|
334
|
+
*/
|
|
335
|
+
type ClientEvent = Event & {
|
|
336
|
+
seq?: number;
|
|
337
|
+
streamId?: string;
|
|
338
|
+
runId?: string;
|
|
339
|
+
};
|
|
340
|
+
/** Helper type for exposing the inferred client and agent-name union together. */
|
|
341
|
+
type InferClient<TApp> = {
|
|
342
|
+
agentNames: AgentNameFromApp<TApp>;
|
|
343
|
+
client: BetterAgentClient<TApp>;
|
|
344
|
+
};
|
|
345
|
+
//#endregion
|
|
346
|
+
//#region src/core/error.d.ts
|
|
347
|
+
/** One frame from a serialized Better Agent error trace. */
|
|
348
|
+
interface AgentClientErrorTraceFrame {
|
|
349
|
+
/** Trace step name. */
|
|
350
|
+
at: string;
|
|
351
|
+
/** Optional trace metadata. */
|
|
352
|
+
data?: Record<string, unknown>;
|
|
353
|
+
}
|
|
354
|
+
/** Client-normalized Better Agent error shape. */
|
|
355
|
+
interface AgentClientError extends Error {
|
|
356
|
+
/** Stable error code. */
|
|
357
|
+
code?: string;
|
|
358
|
+
/** HTTP status when available. */
|
|
359
|
+
status?: number;
|
|
360
|
+
/** Whether the request may succeed on retry. */
|
|
361
|
+
retryable?: boolean;
|
|
362
|
+
/** Short human title. */
|
|
363
|
+
title?: string;
|
|
364
|
+
/** Detailed error message. */
|
|
365
|
+
detail?: string;
|
|
366
|
+
/** Validation or structured issues. */
|
|
367
|
+
issues?: unknown[];
|
|
368
|
+
/** Server trace id. */
|
|
369
|
+
traceId?: string;
|
|
370
|
+
/** Extra error context. */
|
|
371
|
+
context?: Record<string, unknown>;
|
|
372
|
+
/** Structured trace frames. */
|
|
373
|
+
trace?: AgentClientErrorTraceFrame[];
|
|
374
|
+
/** Original thrown value. */
|
|
375
|
+
raw?: unknown;
|
|
376
|
+
}
|
|
377
|
+
/** Normalizes unknown failures into `AgentClientError`. */
|
|
378
|
+
declare const toAgentClientError: (error: unknown, fallbackMessage?: string) => AgentClientError;
|
|
379
|
+
/**
|
|
380
|
+
* Resolves a user-facing message from an event error payload.
|
|
381
|
+
*/
|
|
382
|
+
declare const getEventErrorMessage: (error: unknown) => string;
|
|
383
|
+
//#endregion
|
|
384
|
+
//#region src/types/ui.d.ts
|
|
385
|
+
interface TextPart extends TextContentBase {
|
|
386
|
+
/** Present when the part is complete. */
|
|
387
|
+
state?: "complete";
|
|
388
|
+
}
|
|
389
|
+
interface AudioPart extends AudioContentBase {
|
|
390
|
+
/** Present when the part is complete. */
|
|
391
|
+
state?: "complete";
|
|
392
|
+
}
|
|
393
|
+
interface ImagePart extends ImageContentBase {
|
|
394
|
+
/** Present when the part is complete. */
|
|
395
|
+
state?: "complete";
|
|
396
|
+
}
|
|
397
|
+
interface FilePart extends FileContentBase {
|
|
398
|
+
/** Present when the part is complete. */
|
|
399
|
+
state?: "complete";
|
|
400
|
+
}
|
|
401
|
+
interface VideoPart extends VideoContentBase {
|
|
402
|
+
/** Present when the part is complete. */
|
|
403
|
+
state?: "complete";
|
|
404
|
+
}
|
|
405
|
+
interface EmbeddingPart extends EmbeddingContentBase {
|
|
406
|
+
/** Present when the part is complete. */
|
|
407
|
+
state?: "complete";
|
|
408
|
+
}
|
|
409
|
+
interface TranscriptPart {
|
|
410
|
+
/** Part type. */
|
|
411
|
+
type: "transcript";
|
|
412
|
+
/** Transcript text. */
|
|
413
|
+
text: string;
|
|
414
|
+
/** Transcript segments. */
|
|
415
|
+
segments?: Array<{
|
|
416
|
+
/** Segment id. */id: string; /** Segment start time in seconds. */
|
|
417
|
+
start: number; /** Segment end time in seconds. */
|
|
418
|
+
end: number; /** Segment text. */
|
|
419
|
+
text: string; /** Speaker label. */
|
|
420
|
+
speaker?: string;
|
|
421
|
+
}>;
|
|
422
|
+
/** Provider metadata. */
|
|
423
|
+
providerMetadata?: Record<string, unknown>;
|
|
424
|
+
/** Present when the part is complete. */
|
|
425
|
+
state?: "complete";
|
|
426
|
+
}
|
|
427
|
+
interface ReasoningPart {
|
|
428
|
+
/** Part type. */
|
|
429
|
+
type: "reasoning";
|
|
430
|
+
/** Reasoning text. */
|
|
431
|
+
text: string;
|
|
432
|
+
/** Reasoning visibility. */
|
|
433
|
+
visibility: "summary" | "full";
|
|
434
|
+
/** Provider name. */
|
|
435
|
+
provider?: string;
|
|
436
|
+
/** Provider metadata. */
|
|
437
|
+
providerMetadata?: Record<string, unknown>;
|
|
438
|
+
/** Present when the part is complete. */
|
|
439
|
+
state?: "complete";
|
|
440
|
+
}
|
|
441
|
+
interface ToolCallPart {
|
|
442
|
+
/** Part type. */
|
|
443
|
+
type: "tool-call";
|
|
444
|
+
/** Tool-call id. */
|
|
445
|
+
callId: string;
|
|
446
|
+
/** Tool name. */
|
|
447
|
+
name?: string;
|
|
448
|
+
/** Tool arguments as JSON. */
|
|
449
|
+
args?: string;
|
|
450
|
+
/** Tool location. */
|
|
451
|
+
toolTarget?: ToolTarget;
|
|
452
|
+
/** Approval details. */
|
|
453
|
+
approval?: {
|
|
454
|
+
/** Approval input. */input?: unknown; /** Approval metadata. */
|
|
455
|
+
meta?: Record<string, unknown>; /** Approval note. */
|
|
456
|
+
note?: string; /** Actor id. */
|
|
457
|
+
actorId?: string;
|
|
458
|
+
};
|
|
459
|
+
/** Tool-call status. */
|
|
460
|
+
status: "pending" | "success" | "error";
|
|
461
|
+
/** Tool-call lifecycle state. */
|
|
462
|
+
state?: "awaiting-input" | "input-streaming" | "input-complete" | "approval-requested" | "approval-approved" | "approval-denied" | "approval-expired" | "completed";
|
|
463
|
+
}
|
|
464
|
+
interface ToolResultPart {
|
|
465
|
+
/** Part type. */
|
|
466
|
+
type: "tool-result";
|
|
467
|
+
/** Tool-call id. */
|
|
468
|
+
callId: string;
|
|
469
|
+
/** Tool result payload. */
|
|
470
|
+
result?: unknown;
|
|
471
|
+
/** Tool-result status. */
|
|
472
|
+
status: "pending" | "success" | "error";
|
|
473
|
+
}
|
|
474
|
+
/** One part inside a `UIMessage`. */
|
|
475
|
+
type UIMessagePart = TextPart | AudioPart | ImagePart | FilePart | VideoPart | EmbeddingPart | TranscriptPart | ReasoningPart | ToolCallPart | ToolResultPart;
|
|
476
|
+
interface UIMessage {
|
|
477
|
+
/**
|
|
478
|
+
* Local message id.
|
|
479
|
+
*
|
|
480
|
+
* `setMessages` may omit this field; Better Agent will generate one.
|
|
481
|
+
*/
|
|
482
|
+
localId: string;
|
|
483
|
+
/** Server message id. */
|
|
484
|
+
id?: string;
|
|
485
|
+
/** Message role. */
|
|
486
|
+
role: "system" | "assistant" | "user" | (string & {});
|
|
487
|
+
/** Message parts. */
|
|
488
|
+
parts: UIMessagePart[];
|
|
489
|
+
/** Local delivery state. */
|
|
490
|
+
status?: "pending" | "sent" | "failed";
|
|
491
|
+
/** Local error message. */
|
|
492
|
+
error?: string;
|
|
493
|
+
}
|
|
494
|
+
/** Pending approval extracted from the current message state. */
|
|
495
|
+
interface PendingToolApproval {
|
|
496
|
+
/** Tool-call id. */
|
|
497
|
+
toolCallId: string;
|
|
498
|
+
/** Tool name. */
|
|
499
|
+
toolName?: string;
|
|
500
|
+
/** Tool arguments as JSON. */
|
|
501
|
+
args?: string;
|
|
502
|
+
/** Tool location. */
|
|
503
|
+
toolTarget?: ToolTarget;
|
|
504
|
+
/** Approval input. */
|
|
505
|
+
input?: unknown;
|
|
506
|
+
/** Approval metadata. */
|
|
507
|
+
meta?: Record<string, unknown>;
|
|
508
|
+
/** Approval note. */
|
|
509
|
+
note?: string;
|
|
510
|
+
/** Actor id. */
|
|
511
|
+
actorId?: string;
|
|
512
|
+
}
|
|
513
|
+
//#endregion
|
|
514
|
+
//#region src/types/controller.d.ts
|
|
515
|
+
/** Input for a `UIMessage` before `localId` is assigned. */
|
|
516
|
+
type UIMessageInput = Omit<UIMessage, "localId"> & {
|
|
517
|
+
localId?: string;
|
|
518
|
+
};
|
|
519
|
+
/** Next messages or a function that returns them. */
|
|
520
|
+
type SetMessagesInput = UIMessageInput[] | ((messages: UIMessage[]) => UIMessageInput[]);
|
|
521
|
+
/** Input for `sendMessage`. */
|
|
522
|
+
type SubmitInput<TApp = unknown, TAgentName extends AgentNameFromApp<TApp> = AgentNameFromApp<TApp>, TModalities extends ModalitiesForAgent<TApp, TAgentName> | undefined = undefined> = Omit<RunInputForAgent<TApp, TAgentName, TModalities>, "conversationId" | "sendClientHistory">;
|
|
523
|
+
/** Public `sendMessage` input. */
|
|
524
|
+
type SendMessageInputForAgent<TApp = unknown, TAgentName extends AgentNameFromApp<TApp> = AgentNameFromApp<TApp>, TModalities extends ModalitiesForAgent<TApp, TAgentName> | undefined = undefined> = TextInputShorthandForAgent<TApp, TAgentName> | SubmitInput<TApp, TAgentName, TModalities>;
|
|
525
|
+
/** Custom model-history preparation for `sendClientHistory`. */
|
|
526
|
+
type PrepareMessages = (params: {
|
|
527
|
+
messages: UIMessage[];
|
|
528
|
+
input: unknown;
|
|
529
|
+
}) => GenerativeModelInputItem[];
|
|
530
|
+
/** Internal controller run input shape. */
|
|
531
|
+
type ControllerRunInput = Record<string, unknown> & {
|
|
532
|
+
input: unknown;
|
|
533
|
+
context?: unknown | undefined;
|
|
534
|
+
modalities?: readonly string[] | undefined;
|
|
535
|
+
modelOptions?: Record<string, unknown> | undefined;
|
|
536
|
+
};
|
|
537
|
+
/** Immutable controller snapshot. */
|
|
538
|
+
interface AgentChatSnapshot {
|
|
539
|
+
/** Stable local chat id. */
|
|
540
|
+
id: string;
|
|
541
|
+
/** Conversation id, when provided. */
|
|
542
|
+
conversationId: string | undefined;
|
|
543
|
+
/** Conversation messages. */
|
|
544
|
+
messages: UIMessage[];
|
|
545
|
+
/** Current request status. */
|
|
546
|
+
status: AgentStatus;
|
|
547
|
+
/** Latest client error. */
|
|
548
|
+
error: AgentClientError | undefined;
|
|
549
|
+
/** Latest stream id. */
|
|
550
|
+
streamId: string | undefined;
|
|
551
|
+
/** Latest run id. */
|
|
552
|
+
runId: string | undefined;
|
|
553
|
+
/** True while a request is active. */
|
|
554
|
+
isLoading: boolean;
|
|
555
|
+
/** True while streaming events are being consumed. */
|
|
556
|
+
isStreaming: boolean;
|
|
557
|
+
/** Pending tool approvals. */
|
|
558
|
+
pendingToolApprovals: PendingToolApproval[];
|
|
559
|
+
}
|
|
560
|
+
/** Chat request status. */
|
|
561
|
+
type AgentStatus = "ready" | "hydrating" | "submitted" | "streaming" | "error";
|
|
562
|
+
/** Startup resume behavior. */
|
|
563
|
+
type ResumeOption = boolean | {
|
|
564
|
+
afterSeq?: number;
|
|
565
|
+
streamId?: string;
|
|
566
|
+
};
|
|
567
|
+
/** Options for `AgentChatController`. */
|
|
568
|
+
type AgentChatControllerOptions<TApp = unknown, TAgentName extends AgentNameFromApp<TApp> = AgentNameFromApp<TApp>> = {
|
|
569
|
+
/** Stable local chat id. */id?: string; /** Agent name. */
|
|
570
|
+
agent: TAgentName; /** Conversation id. */
|
|
571
|
+
conversationId?: string; /** Default provider model options. */
|
|
572
|
+
modelOptions?: RunInputForAgent<TApp, TAgentName> extends {
|
|
573
|
+
modelOptions?: infer TModelOptions;
|
|
574
|
+
} ? TModelOptions : Record<string, unknown>; /** Delivery strategy. */
|
|
575
|
+
delivery?: "auto" | "stream" | "final"; /** Initial UI messages. */
|
|
576
|
+
initialMessages?: UIMessageInput[]; /** Resumes a stream or conversation on init. */
|
|
577
|
+
resume?: ResumeOption; /** Sends full client-side history with each request. */
|
|
578
|
+
sendClientHistory?: boolean; /** Fully overrides client history serialization when replay is enabled. */
|
|
579
|
+
prepareMessages?: PrepareMessages;
|
|
580
|
+
/**
|
|
581
|
+
* Loads conversation history from the server on init when `conversationId` is set.
|
|
582
|
+
*
|
|
583
|
+
* Server history replaces `initialMessages` when found.
|
|
584
|
+
*/
|
|
585
|
+
hydrateFromServer?: boolean; /** Generates local message ids. */
|
|
586
|
+
generateMessageId?: (message?: Partial<UIMessageInput>) => string; /** Controls optimistic user-message insertion. */
|
|
587
|
+
optimisticUserMessage?: boolean | {
|
|
588
|
+
enabled?: boolean;
|
|
589
|
+
onError?: "fail" | "remove";
|
|
590
|
+
}; /** Per-run function tool handler. */
|
|
591
|
+
onToolCall?: OnToolCall<TApp>; /** Per-run map-based tool handlers. */
|
|
592
|
+
toolHandlers?: ToolHandlers<TApp>; /** Called when a run ends. */
|
|
593
|
+
onFinish?: (opts: OnFinishParams<TApp, TAgentName>) => void; /** Called when an optimistic user message fails. */
|
|
594
|
+
onOptimisticUserMessageError?: (params: {
|
|
595
|
+
message: UIMessage;
|
|
596
|
+
error: Error;
|
|
597
|
+
}) => void; /** Called for `DATA_PART` events. */
|
|
598
|
+
onData?: (part: {
|
|
599
|
+
id?: string;
|
|
600
|
+
data: unknown;
|
|
601
|
+
}) => void; /** Called after the HTTP response is received. */
|
|
602
|
+
onResponse?: (response: Response) => void; /** Called when the run fails. */
|
|
603
|
+
onError?: (error: Error) => void; /** Called for every streamed event. */
|
|
604
|
+
onEvent?: (event: ClientEvent) => void; /** Called when streaming disconnects. */
|
|
605
|
+
onDisconnect?: (info: {
|
|
606
|
+
error: Error;
|
|
607
|
+
runId?: string;
|
|
608
|
+
streamId?: string;
|
|
609
|
+
}) => void;
|
|
610
|
+
} & AgentContextInputField<TApp, TAgentName>;
|
|
611
|
+
type OnFinishStructuredField<TApp, TAgentName extends AgentNameFromApp<TApp>> = [DefaultStructuredOutputForAgent<TApp, TAgentName>] extends [never] ? object : {
|
|
612
|
+
/** Final parsed structured output, when this agent has a output schema. */structured: DefaultStructuredOutputForAgent<TApp, TAgentName>;
|
|
613
|
+
};
|
|
614
|
+
/** `onFinish` callback parameters. */
|
|
615
|
+
type OnFinishParams<TApp = unknown, TAgentName extends AgentNameFromApp<TApp> = AgentNameFromApp<TApp>> = {
|
|
616
|
+
/** Final local messages. */messages: UIMessage[]; /** Run id, when available. */
|
|
617
|
+
runId?: string; /** Stream id, when available. */
|
|
618
|
+
streamId?: string; /** Conversation id, when configured. */
|
|
619
|
+
conversationId?: string; /** Final model response, when available. */
|
|
620
|
+
response?: GenerativeModelResponse; /** Finish reason, when available. */
|
|
621
|
+
finishReason?: GenerativeModelResponse["finishReason"]; /** Usage, when available. */
|
|
622
|
+
usage?: GenerativeModelResponse["usage"]; /** True when the run was aborted. */
|
|
623
|
+
isAbort: boolean;
|
|
624
|
+
} & OnFinishStructuredField<TApp, TAgentName>;
|
|
625
|
+
/** Result from `sendMessage`. */
|
|
626
|
+
interface SendResult {
|
|
627
|
+
/** Run id, when available. */
|
|
628
|
+
runId?: string;
|
|
629
|
+
/** Stream id, when available. */
|
|
630
|
+
streamId?: string;
|
|
631
|
+
}
|
|
632
|
+
/** Result from `retryMessage`. */
|
|
633
|
+
interface RetryResult {
|
|
634
|
+
/** Stream id, when available. */
|
|
635
|
+
streamId?: string;
|
|
636
|
+
}
|
|
637
|
+
/** Parameters for `approveToolCall`. */
|
|
638
|
+
interface ApproveToolCallParams {
|
|
639
|
+
/** Tool-call id. */
|
|
640
|
+
toolCallId: string;
|
|
641
|
+
/** Run id override. Defaults to the latest run id. */
|
|
642
|
+
runId?: string;
|
|
643
|
+
/** Approval decision. */
|
|
644
|
+
decision: "approved" | "denied";
|
|
645
|
+
/** Approval note. */
|
|
646
|
+
note?: string;
|
|
647
|
+
/** Actor id. */
|
|
648
|
+
actorId?: string;
|
|
649
|
+
}
|
|
650
|
+
/** Options for `sendMessage`. */
|
|
651
|
+
interface SendMessageOptions {
|
|
652
|
+
/** Abort signal. */
|
|
653
|
+
signal?: AbortSignal;
|
|
654
|
+
}
|
|
655
|
+
//#endregion
|
|
656
|
+
export { getEventErrorMessage as A, SubmitToolApprovalRequest as B, ToolCallPart as C, UIMessagePart as D, UIMessage as E, InferClient as F, AgentContext as G, ToolCallContext as H, OnToolCall as I, AgentsFromApp as J, AgentNameFromApp as K, ReplayInput as L, BetterAgentClient as M, ClientConfig as N, VideoPart as O, ClientEvent as P, TextInputShorthandForAgent as Q, RequestOptions as R, TextPart as S, TranscriptPart as T, ToolCallRequest as U, SubmitToolResultRequest as V, ToolHandlers as W, NormalizeClientApp as X, ModalitiesForAgent as Y, RunInputForAgent as Z, EmbeddingPart as _, ControllerRunInput as a, PendingToolApproval as b, ResumeOption as c, SendMessageOptions as d, SendResult as f, AudioPart as g, UIMessageInput as h, ApproveToolCallParams as i, toAgentClientError as j, AgentClientError as k, RetryResult as l, SubmitInput as m, AgentChatSnapshot as n, OnFinishParams as o, SetMessagesInput as p, AgentRunInput as q, AgentStatus as r, PrepareMessages as s, AgentChatControllerOptions as t, SendMessageInputForAgent as u, FilePart as v, ToolResultPart as w, ReasoningPart as x, ImagePart as y, StreamRequestOptions as z };
|
|
657
|
+
//# sourceMappingURL=controller-CJ79_cSR.d.mts.map
|