react-observer-agent 0.2.0 → 0.3.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 +131 -27
- package/dist/index.cjs +747 -271
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +167 -15
- package/dist/index.d.ts +167 -15
- package/dist/index.js +753 -271
- package/dist/index.js.map +1 -1
- package/package.json +13 -3
package/dist/index.d.cts
CHANGED
|
@@ -1,16 +1,64 @@
|
|
|
1
1
|
import * as react_jsx_runtime from 'react/jsx-runtime';
|
|
2
2
|
|
|
3
3
|
type JSONSchema = Record<string, unknown>;
|
|
4
|
+
/**
|
|
5
|
+
* Passed to every tool handler as its second argument. Handlers written
|
|
6
|
+
* against older versions take one argument, so read it as `context?.signal`.
|
|
7
|
+
*/
|
|
8
|
+
interface ToolContext {
|
|
9
|
+
/** Aborts when the interaction is cancelled. Forward it to fetch or other long work. */
|
|
10
|
+
signal?: AbortSignal;
|
|
11
|
+
}
|
|
12
|
+
type ToolHandler<TArgs = unknown> = (args: TArgs, context?: ToolContext) => unknown | Promise<unknown>;
|
|
13
|
+
/**
|
|
14
|
+
* A minimal copy of the Standard Schema v1 interface (standardschema.dev).
|
|
15
|
+
* The spec is meant to be copied, which keeps this package dependency free
|
|
16
|
+
* while accepting validators from Zod 3.24+, Valibot 1+ and ArkType 2+.
|
|
17
|
+
*/
|
|
18
|
+
interface StandardSchemaV1<Input = unknown, Output = Input> {
|
|
19
|
+
readonly '~standard': StandardSchemaProps<Input, Output>;
|
|
20
|
+
}
|
|
21
|
+
interface StandardSchemaProps<Input = unknown, Output = Input> {
|
|
22
|
+
readonly version: 1;
|
|
23
|
+
readonly vendor: string;
|
|
24
|
+
readonly validate: (value: unknown) => StandardSchemaResult<Output> | Promise<StandardSchemaResult<Output>>;
|
|
25
|
+
readonly types?: {
|
|
26
|
+
readonly input: Input;
|
|
27
|
+
readonly output: Output;
|
|
28
|
+
} | undefined;
|
|
29
|
+
}
|
|
30
|
+
type StandardSchemaResult<Output> = {
|
|
31
|
+
readonly value: Output;
|
|
32
|
+
readonly issues?: undefined;
|
|
33
|
+
} | {
|
|
34
|
+
readonly issues: ReadonlyArray<StandardSchemaIssue>;
|
|
35
|
+
};
|
|
36
|
+
interface StandardSchemaIssue {
|
|
37
|
+
readonly message: string;
|
|
38
|
+
readonly path?: ReadonlyArray<PropertyKey | {
|
|
39
|
+
readonly key: PropertyKey;
|
|
40
|
+
}> | undefined;
|
|
41
|
+
}
|
|
42
|
+
/** The value a schema produces after validation, which transforms may reshape. */
|
|
43
|
+
type InferSchemaOutput<S extends StandardSchemaV1> = NonNullable<S['~standard']['types']>['output'];
|
|
4
44
|
interface ToolOptions {
|
|
5
45
|
description?: string;
|
|
6
46
|
parameters?: JSONSchema;
|
|
47
|
+
/**
|
|
48
|
+
* Optional Standard Schema validator (Zod 3.24+, Valibot 1+, ArkType 2+).
|
|
49
|
+
* When present, runtime validation runs through it instead of the built-in
|
|
50
|
+
* JSON Schema subset, and the handler receives the validated value.
|
|
51
|
+
* `parameters` still supplies the JSON Schema the model sees.
|
|
52
|
+
*/
|
|
53
|
+
schema?: StandardSchemaV1;
|
|
7
54
|
confirm?: boolean;
|
|
8
55
|
}
|
|
9
56
|
interface ToolDefinition<TArgs = unknown> {
|
|
10
57
|
name: string;
|
|
11
|
-
handler:
|
|
58
|
+
handler: ToolHandler<TArgs>;
|
|
12
59
|
description?: string;
|
|
13
60
|
parameters?: JSONSchema;
|
|
61
|
+
schema?: StandardSchemaV1;
|
|
14
62
|
confirm: boolean;
|
|
15
63
|
}
|
|
16
64
|
/**
|
|
@@ -19,32 +67,65 @@ interface ToolDefinition<TArgs = unknown> {
|
|
|
19
67
|
* handler parameters are contravariant.
|
|
20
68
|
*/
|
|
21
69
|
type AnyToolDefinition = ToolDefinition<any>;
|
|
70
|
+
type ToolCallStatus = 'success' | 'error' | 'denied' | 'confirmed' | 'cancelled';
|
|
22
71
|
interface ToolCallResult {
|
|
23
72
|
toolName: string;
|
|
24
73
|
args: unknown;
|
|
25
74
|
result: unknown;
|
|
26
|
-
status:
|
|
75
|
+
status: ToolCallStatus;
|
|
27
76
|
}
|
|
28
77
|
interface ConversationEntry {
|
|
29
78
|
role: 'user' | 'assistant' | 'tool';
|
|
30
79
|
content: string;
|
|
31
80
|
toolCalls?: ToolCallResult[];
|
|
81
|
+
/** Assistant entries only: the interaction ended with this error, ABORTED included. */
|
|
82
|
+
error?: AgentError;
|
|
32
83
|
timestamp: number;
|
|
33
84
|
}
|
|
85
|
+
/** Observation only. Emitted through `AgentOptions.onEvent` as the loop runs. */
|
|
86
|
+
type AgentEvent = {
|
|
87
|
+
type: 'turn_start';
|
|
88
|
+
turn: number;
|
|
89
|
+
maxTurns: number;
|
|
90
|
+
} | {
|
|
91
|
+
type: 'state_read';
|
|
92
|
+
requested: string[];
|
|
93
|
+
keys: string[];
|
|
94
|
+
} | {
|
|
95
|
+
type: 'tool_start';
|
|
96
|
+
toolName: string;
|
|
97
|
+
args: unknown;
|
|
98
|
+
} | {
|
|
99
|
+
type: 'tool_end';
|
|
100
|
+
toolName: string;
|
|
101
|
+
args: unknown;
|
|
102
|
+
result: unknown;
|
|
103
|
+
status: ToolCallStatus;
|
|
104
|
+
};
|
|
105
|
+
type AgentErrorCode = 'ABORTED' | 'MAX_TURNS' | 'ADAPTER_ERROR' | 'TRUNCATED' | 'REFUSED';
|
|
34
106
|
interface AgentError {
|
|
35
107
|
message: string;
|
|
36
|
-
code?:
|
|
108
|
+
code?: AgentErrorCode;
|
|
109
|
+
/** HTTP status when the failure came from an adapter with one. */
|
|
110
|
+
status?: number;
|
|
37
111
|
cause?: unknown;
|
|
38
112
|
}
|
|
113
|
+
interface TokenUsage {
|
|
114
|
+
/** Total input tokens for the call, cached portion included. */
|
|
115
|
+
promptTokens: number;
|
|
116
|
+
/** Output tokens generated by the model. */
|
|
117
|
+
completionTokens: number;
|
|
118
|
+
/** Part of `promptTokens` served from the cache, when the provider reports it. */
|
|
119
|
+
cacheReadTokens?: number;
|
|
120
|
+
/** Part of `promptTokens` written to the cache, when the provider reports it. */
|
|
121
|
+
cacheWriteTokens?: number;
|
|
122
|
+
}
|
|
39
123
|
interface AgentResponse {
|
|
40
124
|
message: string;
|
|
41
125
|
toolCalls: ToolCallResult[];
|
|
42
126
|
error?: AgentError;
|
|
43
127
|
/** Totalled across every model call in the interaction, when the adapter reports it. */
|
|
44
|
-
usage?:
|
|
45
|
-
promptTokens: number;
|
|
46
|
-
completionTokens: number;
|
|
47
|
-
};
|
|
128
|
+
usage?: TokenUsage;
|
|
48
129
|
}
|
|
49
130
|
interface SendOptions {
|
|
50
131
|
/** Cancels the interaction. The pending response resolves with an ABORTED error. */
|
|
@@ -67,6 +148,10 @@ interface ConversationMessage {
|
|
|
67
148
|
content: string;
|
|
68
149
|
toolCallId?: string;
|
|
69
150
|
toolCalls?: LLMToolCall[];
|
|
151
|
+
/** Tool messages only: the result reports a failure (denied, invalid arguments, handler threw). */
|
|
152
|
+
isError?: boolean;
|
|
153
|
+
/** Opaque, adapter-owned. The loop copies it from ModelResponse and replays it verbatim. */
|
|
154
|
+
providerData?: unknown;
|
|
70
155
|
}
|
|
71
156
|
interface LLMToolDefinition {
|
|
72
157
|
name: string;
|
|
@@ -90,13 +175,14 @@ interface ModelRequest {
|
|
|
90
175
|
/** Adapters should forward this to their transport so requests cancel in flight. */
|
|
91
176
|
signal?: AbortSignal;
|
|
92
177
|
}
|
|
178
|
+
/** Why the model stopped, normalized across providers. */
|
|
179
|
+
type StopReason = 'end' | 'tool_use' | 'max_tokens' | 'refusal' | 'other';
|
|
93
180
|
interface ModelResponse {
|
|
94
181
|
content: string | null;
|
|
95
182
|
toolCalls?: LLMToolCall[];
|
|
96
|
-
usage?:
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
};
|
|
183
|
+
usage?: TokenUsage;
|
|
184
|
+
stopReason?: StopReason;
|
|
185
|
+
providerData?: unknown;
|
|
100
186
|
}
|
|
101
187
|
interface ModelAdapter {
|
|
102
188
|
sendMessage(request: ModelRequest): Promise<ModelResponse>;
|
|
@@ -111,20 +197,25 @@ interface PendingToolCall {
|
|
|
111
197
|
toolName: string;
|
|
112
198
|
args: unknown;
|
|
113
199
|
description?: string;
|
|
200
|
+
/** Aborts when the interaction is cancelled while confirmation is pending. */
|
|
201
|
+
signal?: AbortSignal;
|
|
114
202
|
}
|
|
115
203
|
interface ToolCallEvent {
|
|
116
204
|
toolName: string;
|
|
117
205
|
args: unknown;
|
|
118
206
|
result: unknown;
|
|
119
|
-
status:
|
|
207
|
+
status: ToolCallStatus;
|
|
120
208
|
}
|
|
121
209
|
interface AgentOptions {
|
|
122
210
|
debug?: boolean;
|
|
123
211
|
maxTurns?: number;
|
|
124
212
|
systemPrompt?: string;
|
|
213
|
+
/** Byte ceiling per state key in a snapshot. Unset means no limit. */
|
|
214
|
+
maxStateBytes?: number;
|
|
125
215
|
onError?: (error: AgentError) => void;
|
|
126
216
|
onToolCall?: (call: ToolCallEvent) => void;
|
|
127
217
|
onConfirm?: (call: PendingToolCall) => Promise<boolean>;
|
|
218
|
+
onEvent?: (event: AgentEvent) => void;
|
|
128
219
|
}
|
|
129
220
|
interface AIAgentProviderProps {
|
|
130
221
|
model: ModelAdapter;
|
|
@@ -138,7 +229,8 @@ interface OpenAIAdapterConfig {
|
|
|
138
229
|
apiKey?: string;
|
|
139
230
|
model?: string;
|
|
140
231
|
baseURL?: string;
|
|
141
|
-
|
|
232
|
+
/** Defaults to 0.2. Pass null to omit the field from the request. */
|
|
233
|
+
temperature?: number | null;
|
|
142
234
|
headers?: Record<string, string>;
|
|
143
235
|
}
|
|
144
236
|
interface ClaudeAdapterConfig {
|
|
@@ -147,13 +239,57 @@ interface ClaudeAdapterConfig {
|
|
|
147
239
|
baseURL?: string;
|
|
148
240
|
/** Required by the Anthropic API. Defaults to 16000. */
|
|
149
241
|
maxTokens?: number;
|
|
242
|
+
/** Prompt caching for tools and system prompt. Defaults to true. */
|
|
243
|
+
cache?: boolean;
|
|
150
244
|
headers?: Record<string, string>;
|
|
151
245
|
}
|
|
152
246
|
|
|
153
|
-
|
|
247
|
+
/**
|
|
248
|
+
* With a Standard Schema, the handler's argument type comes from the schema.
|
|
249
|
+
*
|
|
250
|
+
* `schema` is omitted before the intersection rather than narrowed in place.
|
|
251
|
+
* Intersecting leaves `StandardSchemaV1 & S`, and checking a validator against
|
|
252
|
+
* that walks its self-referential methods: a Zod 3 object has `deepPartial()`
|
|
253
|
+
* returning a different object type, which cannot satisfy the intersection, so
|
|
254
|
+
* the call fails to match this overload. Omitting the key first leaves `S`
|
|
255
|
+
* alone and costs nothing, since `S` is already constrained to a validator.
|
|
256
|
+
*/
|
|
257
|
+
declare function registerTool<S extends StandardSchemaV1>(name: string, handler: ToolHandler<InferSchemaOutput<S>>, options: Omit<ToolOptions, 'schema'> & {
|
|
258
|
+
schema: S;
|
|
259
|
+
}): ToolDefinition<InferSchemaOutput<S>>;
|
|
260
|
+
/**
|
|
261
|
+
* Without a schema the argument type is the handler's own. The conditional
|
|
262
|
+
* keeps an inferred schema whose output disagrees with that type from falling
|
|
263
|
+
* through to here instead of failing. `O` defaults to options without a
|
|
264
|
+
* `schema` key because an explicit type argument turns inference off, and with
|
|
265
|
+
* it the conditional: an inline schema next to an explicit type argument would
|
|
266
|
+
* leave two unchecked sources of truth for `TArgs`, so it is an excess
|
|
267
|
+
* property error. Drop the type argument and let the schema supply it. A
|
|
268
|
+
* variable typed as plain `ToolOptions` still passes, with or without an
|
|
269
|
+
* explicit type argument, since its schema output is `unknown`.
|
|
270
|
+
*/
|
|
271
|
+
declare function registerTool<TArgs = unknown, O extends ToolOptions = Omit<ToolOptions, 'schema'>>(name: string, handler: ToolHandler<TArgs>, options?: O & (O extends {
|
|
272
|
+
schema: StandardSchemaV1;
|
|
273
|
+
} ? {
|
|
274
|
+
schema: StandardSchemaV1<unknown, TArgs>;
|
|
275
|
+
} : unknown)): ToolDefinition<TArgs>;
|
|
154
276
|
|
|
155
277
|
declare function validateToolNames(tools: AnyToolDefinition[]): void;
|
|
156
278
|
|
|
279
|
+
type ToolArgsValidation = {
|
|
280
|
+
valid: true;
|
|
281
|
+
value: unknown;
|
|
282
|
+
} | {
|
|
283
|
+
valid: false;
|
|
284
|
+
errors: string[];
|
|
285
|
+
};
|
|
286
|
+
/**
|
|
287
|
+
* Validates tool arguments, preferring a Standard Schema when the tool carries
|
|
288
|
+
* one. The returned `value` is what the handler should receive: a schema may
|
|
289
|
+
* apply defaults or transforms, so it is not always the input.
|
|
290
|
+
*/
|
|
291
|
+
declare function validateToolArgs(tool: AnyToolDefinition, args: unknown): Promise<ToolArgsValidation>;
|
|
292
|
+
|
|
157
293
|
declare function AIAgentProvider({ model, state, tools, permissions, options, children, }: AIAgentProviderProps): react_jsx_runtime.JSX.Element;
|
|
158
294
|
|
|
159
295
|
declare function useAgent(): AgentContext;
|
|
@@ -168,4 +304,20 @@ declare function openAIAdapter(config: OpenAIAdapterConfig): ModelAdapter;
|
|
|
168
304
|
|
|
169
305
|
declare function claudeAdapter(config: ClaudeAdapterConfig): ModelAdapter;
|
|
170
306
|
|
|
171
|
-
|
|
307
|
+
/**
|
|
308
|
+
* Thrown by the built-in adapters. Carries the HTTP status and raw body when
|
|
309
|
+
* the failure came from a response, so callers can tell a 401 from a 429
|
|
310
|
+
* without parsing the message.
|
|
311
|
+
*/
|
|
312
|
+
declare class AdapterError extends Error {
|
|
313
|
+
readonly name: "AdapterError";
|
|
314
|
+
readonly status?: number;
|
|
315
|
+
readonly body?: string;
|
|
316
|
+
constructor(message: string, options?: {
|
|
317
|
+
status?: number;
|
|
318
|
+
body?: string;
|
|
319
|
+
cause?: unknown;
|
|
320
|
+
});
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
export { AIAgentProvider, type AIAgentProviderProps, AdapterError, type AgentContext, type AgentError, type AgentErrorCode, type AgentEvent, type AgentOptions, type AgentResponse, type AnyToolDefinition, type ClaudeAdapterConfig, type ConversationEntry, type ConversationMessage, type InferSchemaOutput, type JSONSchema, type LLMToolCall, type LLMToolDefinition, type ModelAdapter, type ModelRequest, type ModelResponse, type OpenAIAdapterConfig, type PendingToolCall, type PermissionsConfig, type SendOptions, type StandardSchemaIssue, type StandardSchemaProps, type StandardSchemaResult, type StandardSchemaV1, type StateSource, type StopReason, type TokenUsage, type ToolArgsValidation, type ToolCallEvent, type ToolCallResult, type ToolCallStatus, type ToolContext, type ToolDefinition, type ToolHandler, type ToolOptions, claudeAdapter, filterState, filterTools, openAIAdapter, registerTool, useAgent, validateToolArgs, validateToolCall, validateToolNames };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,16 +1,64 @@
|
|
|
1
1
|
import * as react_jsx_runtime from 'react/jsx-runtime';
|
|
2
2
|
|
|
3
3
|
type JSONSchema = Record<string, unknown>;
|
|
4
|
+
/**
|
|
5
|
+
* Passed to every tool handler as its second argument. Handlers written
|
|
6
|
+
* against older versions take one argument, so read it as `context?.signal`.
|
|
7
|
+
*/
|
|
8
|
+
interface ToolContext {
|
|
9
|
+
/** Aborts when the interaction is cancelled. Forward it to fetch or other long work. */
|
|
10
|
+
signal?: AbortSignal;
|
|
11
|
+
}
|
|
12
|
+
type ToolHandler<TArgs = unknown> = (args: TArgs, context?: ToolContext) => unknown | Promise<unknown>;
|
|
13
|
+
/**
|
|
14
|
+
* A minimal copy of the Standard Schema v1 interface (standardschema.dev).
|
|
15
|
+
* The spec is meant to be copied, which keeps this package dependency free
|
|
16
|
+
* while accepting validators from Zod 3.24+, Valibot 1+ and ArkType 2+.
|
|
17
|
+
*/
|
|
18
|
+
interface StandardSchemaV1<Input = unknown, Output = Input> {
|
|
19
|
+
readonly '~standard': StandardSchemaProps<Input, Output>;
|
|
20
|
+
}
|
|
21
|
+
interface StandardSchemaProps<Input = unknown, Output = Input> {
|
|
22
|
+
readonly version: 1;
|
|
23
|
+
readonly vendor: string;
|
|
24
|
+
readonly validate: (value: unknown) => StandardSchemaResult<Output> | Promise<StandardSchemaResult<Output>>;
|
|
25
|
+
readonly types?: {
|
|
26
|
+
readonly input: Input;
|
|
27
|
+
readonly output: Output;
|
|
28
|
+
} | undefined;
|
|
29
|
+
}
|
|
30
|
+
type StandardSchemaResult<Output> = {
|
|
31
|
+
readonly value: Output;
|
|
32
|
+
readonly issues?: undefined;
|
|
33
|
+
} | {
|
|
34
|
+
readonly issues: ReadonlyArray<StandardSchemaIssue>;
|
|
35
|
+
};
|
|
36
|
+
interface StandardSchemaIssue {
|
|
37
|
+
readonly message: string;
|
|
38
|
+
readonly path?: ReadonlyArray<PropertyKey | {
|
|
39
|
+
readonly key: PropertyKey;
|
|
40
|
+
}> | undefined;
|
|
41
|
+
}
|
|
42
|
+
/** The value a schema produces after validation, which transforms may reshape. */
|
|
43
|
+
type InferSchemaOutput<S extends StandardSchemaV1> = NonNullable<S['~standard']['types']>['output'];
|
|
4
44
|
interface ToolOptions {
|
|
5
45
|
description?: string;
|
|
6
46
|
parameters?: JSONSchema;
|
|
47
|
+
/**
|
|
48
|
+
* Optional Standard Schema validator (Zod 3.24+, Valibot 1+, ArkType 2+).
|
|
49
|
+
* When present, runtime validation runs through it instead of the built-in
|
|
50
|
+
* JSON Schema subset, and the handler receives the validated value.
|
|
51
|
+
* `parameters` still supplies the JSON Schema the model sees.
|
|
52
|
+
*/
|
|
53
|
+
schema?: StandardSchemaV1;
|
|
7
54
|
confirm?: boolean;
|
|
8
55
|
}
|
|
9
56
|
interface ToolDefinition<TArgs = unknown> {
|
|
10
57
|
name: string;
|
|
11
|
-
handler:
|
|
58
|
+
handler: ToolHandler<TArgs>;
|
|
12
59
|
description?: string;
|
|
13
60
|
parameters?: JSONSchema;
|
|
61
|
+
schema?: StandardSchemaV1;
|
|
14
62
|
confirm: boolean;
|
|
15
63
|
}
|
|
16
64
|
/**
|
|
@@ -19,32 +67,65 @@ interface ToolDefinition<TArgs = unknown> {
|
|
|
19
67
|
* handler parameters are contravariant.
|
|
20
68
|
*/
|
|
21
69
|
type AnyToolDefinition = ToolDefinition<any>;
|
|
70
|
+
type ToolCallStatus = 'success' | 'error' | 'denied' | 'confirmed' | 'cancelled';
|
|
22
71
|
interface ToolCallResult {
|
|
23
72
|
toolName: string;
|
|
24
73
|
args: unknown;
|
|
25
74
|
result: unknown;
|
|
26
|
-
status:
|
|
75
|
+
status: ToolCallStatus;
|
|
27
76
|
}
|
|
28
77
|
interface ConversationEntry {
|
|
29
78
|
role: 'user' | 'assistant' | 'tool';
|
|
30
79
|
content: string;
|
|
31
80
|
toolCalls?: ToolCallResult[];
|
|
81
|
+
/** Assistant entries only: the interaction ended with this error, ABORTED included. */
|
|
82
|
+
error?: AgentError;
|
|
32
83
|
timestamp: number;
|
|
33
84
|
}
|
|
85
|
+
/** Observation only. Emitted through `AgentOptions.onEvent` as the loop runs. */
|
|
86
|
+
type AgentEvent = {
|
|
87
|
+
type: 'turn_start';
|
|
88
|
+
turn: number;
|
|
89
|
+
maxTurns: number;
|
|
90
|
+
} | {
|
|
91
|
+
type: 'state_read';
|
|
92
|
+
requested: string[];
|
|
93
|
+
keys: string[];
|
|
94
|
+
} | {
|
|
95
|
+
type: 'tool_start';
|
|
96
|
+
toolName: string;
|
|
97
|
+
args: unknown;
|
|
98
|
+
} | {
|
|
99
|
+
type: 'tool_end';
|
|
100
|
+
toolName: string;
|
|
101
|
+
args: unknown;
|
|
102
|
+
result: unknown;
|
|
103
|
+
status: ToolCallStatus;
|
|
104
|
+
};
|
|
105
|
+
type AgentErrorCode = 'ABORTED' | 'MAX_TURNS' | 'ADAPTER_ERROR' | 'TRUNCATED' | 'REFUSED';
|
|
34
106
|
interface AgentError {
|
|
35
107
|
message: string;
|
|
36
|
-
code?:
|
|
108
|
+
code?: AgentErrorCode;
|
|
109
|
+
/** HTTP status when the failure came from an adapter with one. */
|
|
110
|
+
status?: number;
|
|
37
111
|
cause?: unknown;
|
|
38
112
|
}
|
|
113
|
+
interface TokenUsage {
|
|
114
|
+
/** Total input tokens for the call, cached portion included. */
|
|
115
|
+
promptTokens: number;
|
|
116
|
+
/** Output tokens generated by the model. */
|
|
117
|
+
completionTokens: number;
|
|
118
|
+
/** Part of `promptTokens` served from the cache, when the provider reports it. */
|
|
119
|
+
cacheReadTokens?: number;
|
|
120
|
+
/** Part of `promptTokens` written to the cache, when the provider reports it. */
|
|
121
|
+
cacheWriteTokens?: number;
|
|
122
|
+
}
|
|
39
123
|
interface AgentResponse {
|
|
40
124
|
message: string;
|
|
41
125
|
toolCalls: ToolCallResult[];
|
|
42
126
|
error?: AgentError;
|
|
43
127
|
/** Totalled across every model call in the interaction, when the adapter reports it. */
|
|
44
|
-
usage?:
|
|
45
|
-
promptTokens: number;
|
|
46
|
-
completionTokens: number;
|
|
47
|
-
};
|
|
128
|
+
usage?: TokenUsage;
|
|
48
129
|
}
|
|
49
130
|
interface SendOptions {
|
|
50
131
|
/** Cancels the interaction. The pending response resolves with an ABORTED error. */
|
|
@@ -67,6 +148,10 @@ interface ConversationMessage {
|
|
|
67
148
|
content: string;
|
|
68
149
|
toolCallId?: string;
|
|
69
150
|
toolCalls?: LLMToolCall[];
|
|
151
|
+
/** Tool messages only: the result reports a failure (denied, invalid arguments, handler threw). */
|
|
152
|
+
isError?: boolean;
|
|
153
|
+
/** Opaque, adapter-owned. The loop copies it from ModelResponse and replays it verbatim. */
|
|
154
|
+
providerData?: unknown;
|
|
70
155
|
}
|
|
71
156
|
interface LLMToolDefinition {
|
|
72
157
|
name: string;
|
|
@@ -90,13 +175,14 @@ interface ModelRequest {
|
|
|
90
175
|
/** Adapters should forward this to their transport so requests cancel in flight. */
|
|
91
176
|
signal?: AbortSignal;
|
|
92
177
|
}
|
|
178
|
+
/** Why the model stopped, normalized across providers. */
|
|
179
|
+
type StopReason = 'end' | 'tool_use' | 'max_tokens' | 'refusal' | 'other';
|
|
93
180
|
interface ModelResponse {
|
|
94
181
|
content: string | null;
|
|
95
182
|
toolCalls?: LLMToolCall[];
|
|
96
|
-
usage?:
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
};
|
|
183
|
+
usage?: TokenUsage;
|
|
184
|
+
stopReason?: StopReason;
|
|
185
|
+
providerData?: unknown;
|
|
100
186
|
}
|
|
101
187
|
interface ModelAdapter {
|
|
102
188
|
sendMessage(request: ModelRequest): Promise<ModelResponse>;
|
|
@@ -111,20 +197,25 @@ interface PendingToolCall {
|
|
|
111
197
|
toolName: string;
|
|
112
198
|
args: unknown;
|
|
113
199
|
description?: string;
|
|
200
|
+
/** Aborts when the interaction is cancelled while confirmation is pending. */
|
|
201
|
+
signal?: AbortSignal;
|
|
114
202
|
}
|
|
115
203
|
interface ToolCallEvent {
|
|
116
204
|
toolName: string;
|
|
117
205
|
args: unknown;
|
|
118
206
|
result: unknown;
|
|
119
|
-
status:
|
|
207
|
+
status: ToolCallStatus;
|
|
120
208
|
}
|
|
121
209
|
interface AgentOptions {
|
|
122
210
|
debug?: boolean;
|
|
123
211
|
maxTurns?: number;
|
|
124
212
|
systemPrompt?: string;
|
|
213
|
+
/** Byte ceiling per state key in a snapshot. Unset means no limit. */
|
|
214
|
+
maxStateBytes?: number;
|
|
125
215
|
onError?: (error: AgentError) => void;
|
|
126
216
|
onToolCall?: (call: ToolCallEvent) => void;
|
|
127
217
|
onConfirm?: (call: PendingToolCall) => Promise<boolean>;
|
|
218
|
+
onEvent?: (event: AgentEvent) => void;
|
|
128
219
|
}
|
|
129
220
|
interface AIAgentProviderProps {
|
|
130
221
|
model: ModelAdapter;
|
|
@@ -138,7 +229,8 @@ interface OpenAIAdapterConfig {
|
|
|
138
229
|
apiKey?: string;
|
|
139
230
|
model?: string;
|
|
140
231
|
baseURL?: string;
|
|
141
|
-
|
|
232
|
+
/** Defaults to 0.2. Pass null to omit the field from the request. */
|
|
233
|
+
temperature?: number | null;
|
|
142
234
|
headers?: Record<string, string>;
|
|
143
235
|
}
|
|
144
236
|
interface ClaudeAdapterConfig {
|
|
@@ -147,13 +239,57 @@ interface ClaudeAdapterConfig {
|
|
|
147
239
|
baseURL?: string;
|
|
148
240
|
/** Required by the Anthropic API. Defaults to 16000. */
|
|
149
241
|
maxTokens?: number;
|
|
242
|
+
/** Prompt caching for tools and system prompt. Defaults to true. */
|
|
243
|
+
cache?: boolean;
|
|
150
244
|
headers?: Record<string, string>;
|
|
151
245
|
}
|
|
152
246
|
|
|
153
|
-
|
|
247
|
+
/**
|
|
248
|
+
* With a Standard Schema, the handler's argument type comes from the schema.
|
|
249
|
+
*
|
|
250
|
+
* `schema` is omitted before the intersection rather than narrowed in place.
|
|
251
|
+
* Intersecting leaves `StandardSchemaV1 & S`, and checking a validator against
|
|
252
|
+
* that walks its self-referential methods: a Zod 3 object has `deepPartial()`
|
|
253
|
+
* returning a different object type, which cannot satisfy the intersection, so
|
|
254
|
+
* the call fails to match this overload. Omitting the key first leaves `S`
|
|
255
|
+
* alone and costs nothing, since `S` is already constrained to a validator.
|
|
256
|
+
*/
|
|
257
|
+
declare function registerTool<S extends StandardSchemaV1>(name: string, handler: ToolHandler<InferSchemaOutput<S>>, options: Omit<ToolOptions, 'schema'> & {
|
|
258
|
+
schema: S;
|
|
259
|
+
}): ToolDefinition<InferSchemaOutput<S>>;
|
|
260
|
+
/**
|
|
261
|
+
* Without a schema the argument type is the handler's own. The conditional
|
|
262
|
+
* keeps an inferred schema whose output disagrees with that type from falling
|
|
263
|
+
* through to here instead of failing. `O` defaults to options without a
|
|
264
|
+
* `schema` key because an explicit type argument turns inference off, and with
|
|
265
|
+
* it the conditional: an inline schema next to an explicit type argument would
|
|
266
|
+
* leave two unchecked sources of truth for `TArgs`, so it is an excess
|
|
267
|
+
* property error. Drop the type argument and let the schema supply it. A
|
|
268
|
+
* variable typed as plain `ToolOptions` still passes, with or without an
|
|
269
|
+
* explicit type argument, since its schema output is `unknown`.
|
|
270
|
+
*/
|
|
271
|
+
declare function registerTool<TArgs = unknown, O extends ToolOptions = Omit<ToolOptions, 'schema'>>(name: string, handler: ToolHandler<TArgs>, options?: O & (O extends {
|
|
272
|
+
schema: StandardSchemaV1;
|
|
273
|
+
} ? {
|
|
274
|
+
schema: StandardSchemaV1<unknown, TArgs>;
|
|
275
|
+
} : unknown)): ToolDefinition<TArgs>;
|
|
154
276
|
|
|
155
277
|
declare function validateToolNames(tools: AnyToolDefinition[]): void;
|
|
156
278
|
|
|
279
|
+
type ToolArgsValidation = {
|
|
280
|
+
valid: true;
|
|
281
|
+
value: unknown;
|
|
282
|
+
} | {
|
|
283
|
+
valid: false;
|
|
284
|
+
errors: string[];
|
|
285
|
+
};
|
|
286
|
+
/**
|
|
287
|
+
* Validates tool arguments, preferring a Standard Schema when the tool carries
|
|
288
|
+
* one. The returned `value` is what the handler should receive: a schema may
|
|
289
|
+
* apply defaults or transforms, so it is not always the input.
|
|
290
|
+
*/
|
|
291
|
+
declare function validateToolArgs(tool: AnyToolDefinition, args: unknown): Promise<ToolArgsValidation>;
|
|
292
|
+
|
|
157
293
|
declare function AIAgentProvider({ model, state, tools, permissions, options, children, }: AIAgentProviderProps): react_jsx_runtime.JSX.Element;
|
|
158
294
|
|
|
159
295
|
declare function useAgent(): AgentContext;
|
|
@@ -168,4 +304,20 @@ declare function openAIAdapter(config: OpenAIAdapterConfig): ModelAdapter;
|
|
|
168
304
|
|
|
169
305
|
declare function claudeAdapter(config: ClaudeAdapterConfig): ModelAdapter;
|
|
170
306
|
|
|
171
|
-
|
|
307
|
+
/**
|
|
308
|
+
* Thrown by the built-in adapters. Carries the HTTP status and raw body when
|
|
309
|
+
* the failure came from a response, so callers can tell a 401 from a 429
|
|
310
|
+
* without parsing the message.
|
|
311
|
+
*/
|
|
312
|
+
declare class AdapterError extends Error {
|
|
313
|
+
readonly name: "AdapterError";
|
|
314
|
+
readonly status?: number;
|
|
315
|
+
readonly body?: string;
|
|
316
|
+
constructor(message: string, options?: {
|
|
317
|
+
status?: number;
|
|
318
|
+
body?: string;
|
|
319
|
+
cause?: unknown;
|
|
320
|
+
});
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
export { AIAgentProvider, type AIAgentProviderProps, AdapterError, type AgentContext, type AgentError, type AgentErrorCode, type AgentEvent, type AgentOptions, type AgentResponse, type AnyToolDefinition, type ClaudeAdapterConfig, type ConversationEntry, type ConversationMessage, type InferSchemaOutput, type JSONSchema, type LLMToolCall, type LLMToolDefinition, type ModelAdapter, type ModelRequest, type ModelResponse, type OpenAIAdapterConfig, type PendingToolCall, type PermissionsConfig, type SendOptions, type StandardSchemaIssue, type StandardSchemaProps, type StandardSchemaResult, type StandardSchemaV1, type StateSource, type StopReason, type TokenUsage, type ToolArgsValidation, type ToolCallEvent, type ToolCallResult, type ToolCallStatus, type ToolContext, type ToolDefinition, type ToolHandler, type ToolOptions, claudeAdapter, filterState, filterTools, openAIAdapter, registerTool, useAgent, validateToolArgs, validateToolCall, validateToolNames };
|