react-mcp-chatbot 0.0.6 → 1.0.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/CHANGELOG.md +31 -0
- package/LICENSE +21 -0
- package/README.md +199 -147
- package/dist/index.cjs +16175 -0
- package/dist/index.d.cts +275 -0
- package/dist/index.d.ts +250 -33
- package/dist/index.js +833 -551
- package/package.json +34 -14
- package/dist/send-2CSO66UQ.svg +0 -3
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,275 @@
|
|
|
1
|
+
import * as react from 'react';
|
|
2
|
+
|
|
3
|
+
type ToolParameterType = "string" | "number" | "integer" | "boolean" | "object" | "array";
|
|
4
|
+
type MCPToolParameter = {
|
|
5
|
+
type: ToolParameterType;
|
|
6
|
+
description?: string;
|
|
7
|
+
/** Parameters are required unless `optional: true`. */
|
|
8
|
+
optional?: boolean;
|
|
9
|
+
enum?: (string | number)[];
|
|
10
|
+
/** For `array` parameters: JSON-schema of the items. */
|
|
11
|
+
items?: Record<string, unknown>;
|
|
12
|
+
/** For `object` parameters: JSON-schema properties. */
|
|
13
|
+
properties?: Record<string, unknown>;
|
|
14
|
+
};
|
|
15
|
+
type MCPTool<TArgs = any> = {
|
|
16
|
+
name: string;
|
|
17
|
+
description: string;
|
|
18
|
+
parameters?: Record<string, MCPToolParameter>;
|
|
19
|
+
call: (args: TArgs) => Promise<any> | any;
|
|
20
|
+
};
|
|
21
|
+
/** JSON-schema flavoured description of a tool, as sent to providers. */
|
|
22
|
+
type ToolSpec = {
|
|
23
|
+
name: string;
|
|
24
|
+
description: string;
|
|
25
|
+
parameters: {
|
|
26
|
+
type: "object";
|
|
27
|
+
properties: Record<string, Record<string, unknown>>;
|
|
28
|
+
required: string[];
|
|
29
|
+
};
|
|
30
|
+
};
|
|
31
|
+
type HttpMethod = "GET" | "POST" | "PUT" | "PATCH" | "DELETE";
|
|
32
|
+
type ApiParameter = MCPToolParameter & {
|
|
33
|
+
/**
|
|
34
|
+
* Where the value goes in the HTTP request.
|
|
35
|
+
* Default: `path` if `{name}` appears in the path, otherwise `query` for
|
|
36
|
+
* GET/DELETE and `body` for POST/PUT/PATCH.
|
|
37
|
+
*/
|
|
38
|
+
in?: "path" | "query" | "body" | "header";
|
|
39
|
+
};
|
|
40
|
+
type ApiEndpoint = {
|
|
41
|
+
/** Tool name shown to the model, e.g. `get_order`. */
|
|
42
|
+
name: string;
|
|
43
|
+
/** What the endpoint does. This is what the model reads – be specific. */
|
|
44
|
+
description: string;
|
|
45
|
+
method?: HttpMethod;
|
|
46
|
+
/** Path relative to `baseURL`, may contain `{placeholders}`. */
|
|
47
|
+
path: string;
|
|
48
|
+
parameters?: Record<string, ApiParameter>;
|
|
49
|
+
/** Extra headers for this endpoint only. */
|
|
50
|
+
headers?: Record<string, string>;
|
|
51
|
+
};
|
|
52
|
+
type HeadersInput = Record<string, string> | (() => Record<string, string> | Promise<Record<string, string>>);
|
|
53
|
+
type ApiConfig = {
|
|
54
|
+
/** e.g. `https://api.myapp.com` or `/api` */
|
|
55
|
+
baseURL: string;
|
|
56
|
+
/** Static headers or a function (use it for auth tokens that can change). */
|
|
57
|
+
headers?: HeadersInput;
|
|
58
|
+
/** Pass `"include"` to send cookies to your backend. */
|
|
59
|
+
credentials?: RequestCredentials;
|
|
60
|
+
/** Hand-described endpoints. */
|
|
61
|
+
endpoints?: ApiEndpoint[];
|
|
62
|
+
/**
|
|
63
|
+
* An OpenAPI 3 / Swagger 2 document (object) or a URL to one.
|
|
64
|
+
* Every operation becomes a tool automatically.
|
|
65
|
+
*/
|
|
66
|
+
openapi?: string | Record<string, any>;
|
|
67
|
+
/** With `openapi`: only expose these operations (by tool name). */
|
|
68
|
+
include?: string[];
|
|
69
|
+
/** With `openapi`: hide these operations (by tool name). */
|
|
70
|
+
exclude?: string[];
|
|
71
|
+
/** Max characters of an API response given back to the model. Default 8000. */
|
|
72
|
+
maxResponseChars?: number;
|
|
73
|
+
};
|
|
74
|
+
type ToolCall = {
|
|
75
|
+
id: string;
|
|
76
|
+
name: string;
|
|
77
|
+
args: Record<string, any>;
|
|
78
|
+
/** Provider-specific payload that must be echoed back (e.g. Gemini thought signatures). */
|
|
79
|
+
raw?: unknown;
|
|
80
|
+
};
|
|
81
|
+
type ChatMessage = {
|
|
82
|
+
id: string;
|
|
83
|
+
role: "user";
|
|
84
|
+
content: string;
|
|
85
|
+
} | {
|
|
86
|
+
id: string;
|
|
87
|
+
role: "assistant";
|
|
88
|
+
content: string;
|
|
89
|
+
toolCalls?: ToolCall[];
|
|
90
|
+
/** UI-only error bubble. Never sent to the model. */
|
|
91
|
+
error?: boolean;
|
|
92
|
+
} | {
|
|
93
|
+
id: string;
|
|
94
|
+
role: "tool";
|
|
95
|
+
toolCallId: string;
|
|
96
|
+
name: string;
|
|
97
|
+
content: string;
|
|
98
|
+
};
|
|
99
|
+
type ProviderRequest = {
|
|
100
|
+
system: string;
|
|
101
|
+
messages: ChatMessage[];
|
|
102
|
+
tools: ToolSpec[];
|
|
103
|
+
temperature?: number;
|
|
104
|
+
signal?: AbortSignal;
|
|
105
|
+
};
|
|
106
|
+
type ProviderResponse = {
|
|
107
|
+
content: string;
|
|
108
|
+
toolCalls: ToolCall[];
|
|
109
|
+
};
|
|
110
|
+
interface Provider {
|
|
111
|
+
complete(req: ProviderRequest): Promise<ProviderResponse>;
|
|
112
|
+
}
|
|
113
|
+
type ProviderName = "openai" | "anthropic" | "gemini" | "deepseek" | "groq" | "ollama" | "openrouter" | "mistral" | "xai" | "together" | "openai-compatible";
|
|
114
|
+
type ProviderConfig = {
|
|
115
|
+
provider: ProviderName;
|
|
116
|
+
model: string;
|
|
117
|
+
/** Required for hosted providers. Note: it is visible to your users – see README. */
|
|
118
|
+
apiKey?: string;
|
|
119
|
+
/**
|
|
120
|
+
* Override the API base URL. Point it at your own proxy to keep the key off
|
|
121
|
+
* the client. Required for `openai-compatible`; defaults to
|
|
122
|
+
* `http://localhost:11434` for `ollama`.
|
|
123
|
+
*/
|
|
124
|
+
providerURL?: string;
|
|
125
|
+
temperature?: number;
|
|
126
|
+
/** Extra headers sent to the provider. */
|
|
127
|
+
headers?: Record<string, string>;
|
|
128
|
+
};
|
|
129
|
+
declare const DEFAULT_SYSTEM_PROMPT = "You are a helpful assistant embedded in a web application.\n\n- Use the available tools to look up real data or perform actions whenever the user's request needs them. Never invent data that a tool could provide.\n- If a tool fails, read the error, fix the arguments and retry when it makes sense; otherwise explain the problem briefly.\n- Ask a short clarifying question when a required value is missing.\n- Reply in clean Markdown (lists, tables, **bold** for key values). Do not mention tool names or HTTP details unless asked.";
|
|
130
|
+
type ChatConfig = ProviderConfig & {
|
|
131
|
+
/** Instructions that shape the assistant. Replaces the default prompt. */
|
|
132
|
+
systemPrompt?: string;
|
|
133
|
+
/** Custom JS tools. */
|
|
134
|
+
tools?: MCPTool[];
|
|
135
|
+
/** Describe your backend and the endpoints become tools automatically. */
|
|
136
|
+
api?: ApiConfig;
|
|
137
|
+
/** Save history to localStorage. `true` uses the default key; a string sets the key. Default false. */
|
|
138
|
+
persist?: boolean | string;
|
|
139
|
+
/** Messages sent to the model per request (older ones stay visible but are dropped). Default 40. */
|
|
140
|
+
maxHistoryMessages?: number;
|
|
141
|
+
/** Max tool-calling round trips per user message. Default 8. */
|
|
142
|
+
maxToolRounds?: number;
|
|
143
|
+
/** Max characters of a tool result given to the model. Default 8000. */
|
|
144
|
+
maxToolResultChars?: number;
|
|
145
|
+
/**
|
|
146
|
+
* Called before every tool runs. Return `false` to block it – handy for
|
|
147
|
+
* asking the user to confirm POST/PUT/DELETE calls.
|
|
148
|
+
*/
|
|
149
|
+
beforeToolCall?: (call: {
|
|
150
|
+
name: string;
|
|
151
|
+
args: Record<string, any>;
|
|
152
|
+
/** HTTP method for tools generated from `api`, otherwise undefined. */
|
|
153
|
+
method?: HttpMethod;
|
|
154
|
+
}) => boolean | Promise<boolean>;
|
|
155
|
+
onToolCall?: (call: {
|
|
156
|
+
name: string;
|
|
157
|
+
args: Record<string, any>;
|
|
158
|
+
result: unknown;
|
|
159
|
+
error?: string;
|
|
160
|
+
}) => void;
|
|
161
|
+
onError?: (error: Error) => void;
|
|
162
|
+
};
|
|
163
|
+
type ChatState = {
|
|
164
|
+
messages: ChatMessage[];
|
|
165
|
+
loading: boolean;
|
|
166
|
+
/** Human-readable progress, e.g. "Calling get_order…". */
|
|
167
|
+
status: string | null;
|
|
168
|
+
};
|
|
169
|
+
|
|
170
|
+
type BaseUIProps = {
|
|
171
|
+
title?: string;
|
|
172
|
+
align?: "left" | "right";
|
|
173
|
+
primaryColor?: string;
|
|
174
|
+
width?: number;
|
|
175
|
+
height?: number;
|
|
176
|
+
theme?: "light" | "dark";
|
|
177
|
+
buttonLabel?: string;
|
|
178
|
+
placeholder?: string;
|
|
179
|
+
borderRadius?: number;
|
|
180
|
+
/** Show the trash button in the header. Default true. */
|
|
181
|
+
showClearButton?: boolean;
|
|
182
|
+
/** Start with the chat window open. Default false. */
|
|
183
|
+
defaultOpen?: boolean;
|
|
184
|
+
/** Clickable starter prompts shown in the empty chat. */
|
|
185
|
+
suggestions?: string[];
|
|
186
|
+
};
|
|
187
|
+
type MCPAgentProps = ChatConfig & BaseUIProps;
|
|
188
|
+
/** Imperative handle available through `ref` on `<MCPAgent />`. */
|
|
189
|
+
type MCPAgentHandle = {
|
|
190
|
+
clear: () => void;
|
|
191
|
+
send: (text: string) => Promise<void>;
|
|
192
|
+
stop: () => void;
|
|
193
|
+
};
|
|
194
|
+
|
|
195
|
+
declare const MCPAgent: react.ForwardRefExoticComponent<ProviderConfig & {
|
|
196
|
+
systemPrompt?: string;
|
|
197
|
+
tools?: MCPTool[];
|
|
198
|
+
api?: ApiConfig;
|
|
199
|
+
persist?: boolean | string;
|
|
200
|
+
maxHistoryMessages?: number;
|
|
201
|
+
maxToolRounds?: number;
|
|
202
|
+
maxToolResultChars?: number;
|
|
203
|
+
beforeToolCall?: (call: {
|
|
204
|
+
name: string;
|
|
205
|
+
args: Record<string, any>;
|
|
206
|
+
method?: HttpMethod;
|
|
207
|
+
}) => boolean | Promise<boolean>;
|
|
208
|
+
onToolCall?: (call: {
|
|
209
|
+
name: string;
|
|
210
|
+
args: Record<string, any>;
|
|
211
|
+
result: unknown;
|
|
212
|
+
error?: string;
|
|
213
|
+
}) => void;
|
|
214
|
+
onError?: (error: Error) => void;
|
|
215
|
+
} & BaseUIProps & react.RefAttributes<MCPAgentHandle>>;
|
|
216
|
+
|
|
217
|
+
/**
|
|
218
|
+
* Framework-agnostic chat core: history, system prompt, tool-calling loop.
|
|
219
|
+
* Everything runs in the browser.
|
|
220
|
+
*/
|
|
221
|
+
declare class ChatEngine {
|
|
222
|
+
private cfg;
|
|
223
|
+
private provider;
|
|
224
|
+
private providerSig;
|
|
225
|
+
private state;
|
|
226
|
+
private listeners;
|
|
227
|
+
private abortCtl;
|
|
228
|
+
private apiTools;
|
|
229
|
+
private apiSig;
|
|
230
|
+
constructor(config: ChatConfig);
|
|
231
|
+
/** Update config without losing history. Safe to call on every render. */
|
|
232
|
+
configure(config: ChatConfig): void;
|
|
233
|
+
private sigOf;
|
|
234
|
+
private makeProvider;
|
|
235
|
+
getState: () => ChatState;
|
|
236
|
+
subscribe: (fn: () => void) => () => void;
|
|
237
|
+
private set;
|
|
238
|
+
private emit;
|
|
239
|
+
private storageKey;
|
|
240
|
+
private load;
|
|
241
|
+
private save;
|
|
242
|
+
/** Wipe the conversation (and the saved copy). Also cancels a request in flight. */
|
|
243
|
+
clear(): void;
|
|
244
|
+
abort(): void;
|
|
245
|
+
send(text: string): Promise<void>;
|
|
246
|
+
private lastIndexOf;
|
|
247
|
+
private getTools;
|
|
248
|
+
/** Messages sent to the model: no UI-only errors, windowed to maxHistoryMessages at a turn boundary. */
|
|
249
|
+
private requestMessages;
|
|
250
|
+
private run;
|
|
251
|
+
private execute;
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
/**
|
|
255
|
+
* Headless hook – build your own UI on top of the same engine.
|
|
256
|
+
*
|
|
257
|
+
* const { messages, send, clear, loading } = useChat({ provider, model, apiKey, api });
|
|
258
|
+
*/
|
|
259
|
+
declare function useChat(config: ChatConfig): {
|
|
260
|
+
send: (text: string) => Promise<void>;
|
|
261
|
+
clear: () => void;
|
|
262
|
+
stop: () => void;
|
|
263
|
+
engine: ChatEngine;
|
|
264
|
+
messages: ChatMessage[];
|
|
265
|
+
loading: boolean;
|
|
266
|
+
status: string | null;
|
|
267
|
+
};
|
|
268
|
+
|
|
269
|
+
declare function endpointsFromOpenAPI(rawDoc: any): ApiEndpoint[];
|
|
270
|
+
|
|
271
|
+
/** Default base URLs. Override with `providerURL`. */
|
|
272
|
+
declare const PROVIDER_URLS: Record<Exclude<ProviderName, "openai-compatible" | "ollama">, string>;
|
|
273
|
+
declare function createProvider(cfg: ProviderConfig): Provider;
|
|
274
|
+
|
|
275
|
+
export { type ApiConfig, type ApiEndpoint, type ApiParameter, type ChatConfig, ChatEngine, type ChatMessage, type ChatState, DEFAULT_SYSTEM_PROMPT, MCPAgent, type MCPAgentHandle, type MCPAgentProps, type MCPTool, type MCPToolParameter, PROVIDER_URLS, type Provider, type ProviderConfig, type ProviderName, type ToolCall, createProvider, endpointsFromOpenAPI, useChat };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,16 +1,172 @@
|
|
|
1
|
-
import * as
|
|
1
|
+
import * as react from 'react';
|
|
2
2
|
|
|
3
|
+
type ToolParameterType = "string" | "number" | "integer" | "boolean" | "object" | "array";
|
|
3
4
|
type MCPToolParameter = {
|
|
4
|
-
type:
|
|
5
|
+
type: ToolParameterType;
|
|
5
6
|
description?: string;
|
|
7
|
+
/** Parameters are required unless `optional: true`. */
|
|
6
8
|
optional?: boolean;
|
|
9
|
+
enum?: (string | number)[];
|
|
10
|
+
/** For `array` parameters: JSON-schema of the items. */
|
|
11
|
+
items?: Record<string, unknown>;
|
|
12
|
+
/** For `object` parameters: JSON-schema properties. */
|
|
13
|
+
properties?: Record<string, unknown>;
|
|
7
14
|
};
|
|
8
15
|
type MCPTool<TArgs = any> = {
|
|
9
16
|
name: string;
|
|
10
17
|
description: string;
|
|
11
18
|
parameters?: Record<string, MCPToolParameter>;
|
|
12
|
-
call: (args: TArgs) => Promise<any
|
|
19
|
+
call: (args: TArgs) => Promise<any> | any;
|
|
13
20
|
};
|
|
21
|
+
/** JSON-schema flavoured description of a tool, as sent to providers. */
|
|
22
|
+
type ToolSpec = {
|
|
23
|
+
name: string;
|
|
24
|
+
description: string;
|
|
25
|
+
parameters: {
|
|
26
|
+
type: "object";
|
|
27
|
+
properties: Record<string, Record<string, unknown>>;
|
|
28
|
+
required: string[];
|
|
29
|
+
};
|
|
30
|
+
};
|
|
31
|
+
type HttpMethod = "GET" | "POST" | "PUT" | "PATCH" | "DELETE";
|
|
32
|
+
type ApiParameter = MCPToolParameter & {
|
|
33
|
+
/**
|
|
34
|
+
* Where the value goes in the HTTP request.
|
|
35
|
+
* Default: `path` if `{name}` appears in the path, otherwise `query` for
|
|
36
|
+
* GET/DELETE and `body` for POST/PUT/PATCH.
|
|
37
|
+
*/
|
|
38
|
+
in?: "path" | "query" | "body" | "header";
|
|
39
|
+
};
|
|
40
|
+
type ApiEndpoint = {
|
|
41
|
+
/** Tool name shown to the model, e.g. `get_order`. */
|
|
42
|
+
name: string;
|
|
43
|
+
/** What the endpoint does. This is what the model reads – be specific. */
|
|
44
|
+
description: string;
|
|
45
|
+
method?: HttpMethod;
|
|
46
|
+
/** Path relative to `baseURL`, may contain `{placeholders}`. */
|
|
47
|
+
path: string;
|
|
48
|
+
parameters?: Record<string, ApiParameter>;
|
|
49
|
+
/** Extra headers for this endpoint only. */
|
|
50
|
+
headers?: Record<string, string>;
|
|
51
|
+
};
|
|
52
|
+
type HeadersInput = Record<string, string> | (() => Record<string, string> | Promise<Record<string, string>>);
|
|
53
|
+
type ApiConfig = {
|
|
54
|
+
/** e.g. `https://api.myapp.com` or `/api` */
|
|
55
|
+
baseURL: string;
|
|
56
|
+
/** Static headers or a function (use it for auth tokens that can change). */
|
|
57
|
+
headers?: HeadersInput;
|
|
58
|
+
/** Pass `"include"` to send cookies to your backend. */
|
|
59
|
+
credentials?: RequestCredentials;
|
|
60
|
+
/** Hand-described endpoints. */
|
|
61
|
+
endpoints?: ApiEndpoint[];
|
|
62
|
+
/**
|
|
63
|
+
* An OpenAPI 3 / Swagger 2 document (object) or a URL to one.
|
|
64
|
+
* Every operation becomes a tool automatically.
|
|
65
|
+
*/
|
|
66
|
+
openapi?: string | Record<string, any>;
|
|
67
|
+
/** With `openapi`: only expose these operations (by tool name). */
|
|
68
|
+
include?: string[];
|
|
69
|
+
/** With `openapi`: hide these operations (by tool name). */
|
|
70
|
+
exclude?: string[];
|
|
71
|
+
/** Max characters of an API response given back to the model. Default 8000. */
|
|
72
|
+
maxResponseChars?: number;
|
|
73
|
+
};
|
|
74
|
+
type ToolCall = {
|
|
75
|
+
id: string;
|
|
76
|
+
name: string;
|
|
77
|
+
args: Record<string, any>;
|
|
78
|
+
/** Provider-specific payload that must be echoed back (e.g. Gemini thought signatures). */
|
|
79
|
+
raw?: unknown;
|
|
80
|
+
};
|
|
81
|
+
type ChatMessage = {
|
|
82
|
+
id: string;
|
|
83
|
+
role: "user";
|
|
84
|
+
content: string;
|
|
85
|
+
} | {
|
|
86
|
+
id: string;
|
|
87
|
+
role: "assistant";
|
|
88
|
+
content: string;
|
|
89
|
+
toolCalls?: ToolCall[];
|
|
90
|
+
/** UI-only error bubble. Never sent to the model. */
|
|
91
|
+
error?: boolean;
|
|
92
|
+
} | {
|
|
93
|
+
id: string;
|
|
94
|
+
role: "tool";
|
|
95
|
+
toolCallId: string;
|
|
96
|
+
name: string;
|
|
97
|
+
content: string;
|
|
98
|
+
};
|
|
99
|
+
type ProviderRequest = {
|
|
100
|
+
system: string;
|
|
101
|
+
messages: ChatMessage[];
|
|
102
|
+
tools: ToolSpec[];
|
|
103
|
+
temperature?: number;
|
|
104
|
+
signal?: AbortSignal;
|
|
105
|
+
};
|
|
106
|
+
type ProviderResponse = {
|
|
107
|
+
content: string;
|
|
108
|
+
toolCalls: ToolCall[];
|
|
109
|
+
};
|
|
110
|
+
interface Provider {
|
|
111
|
+
complete(req: ProviderRequest): Promise<ProviderResponse>;
|
|
112
|
+
}
|
|
113
|
+
type ProviderName = "openai" | "anthropic" | "gemini" | "deepseek" | "groq" | "ollama" | "openrouter" | "mistral" | "xai" | "together" | "openai-compatible";
|
|
114
|
+
type ProviderConfig = {
|
|
115
|
+
provider: ProviderName;
|
|
116
|
+
model: string;
|
|
117
|
+
/** Required for hosted providers. Note: it is visible to your users – see README. */
|
|
118
|
+
apiKey?: string;
|
|
119
|
+
/**
|
|
120
|
+
* Override the API base URL. Point it at your own proxy to keep the key off
|
|
121
|
+
* the client. Required for `openai-compatible`; defaults to
|
|
122
|
+
* `http://localhost:11434` for `ollama`.
|
|
123
|
+
*/
|
|
124
|
+
providerURL?: string;
|
|
125
|
+
temperature?: number;
|
|
126
|
+
/** Extra headers sent to the provider. */
|
|
127
|
+
headers?: Record<string, string>;
|
|
128
|
+
};
|
|
129
|
+
declare const DEFAULT_SYSTEM_PROMPT = "You are a helpful assistant embedded in a web application.\n\n- Use the available tools to look up real data or perform actions whenever the user's request needs them. Never invent data that a tool could provide.\n- If a tool fails, read the error, fix the arguments and retry when it makes sense; otherwise explain the problem briefly.\n- Ask a short clarifying question when a required value is missing.\n- Reply in clean Markdown (lists, tables, **bold** for key values). Do not mention tool names or HTTP details unless asked.";
|
|
130
|
+
type ChatConfig = ProviderConfig & {
|
|
131
|
+
/** Instructions that shape the assistant. Replaces the default prompt. */
|
|
132
|
+
systemPrompt?: string;
|
|
133
|
+
/** Custom JS tools. */
|
|
134
|
+
tools?: MCPTool[];
|
|
135
|
+
/** Describe your backend and the endpoints become tools automatically. */
|
|
136
|
+
api?: ApiConfig;
|
|
137
|
+
/** Save history to localStorage. `true` uses the default key; a string sets the key. Default false. */
|
|
138
|
+
persist?: boolean | string;
|
|
139
|
+
/** Messages sent to the model per request (older ones stay visible but are dropped). Default 40. */
|
|
140
|
+
maxHistoryMessages?: number;
|
|
141
|
+
/** Max tool-calling round trips per user message. Default 8. */
|
|
142
|
+
maxToolRounds?: number;
|
|
143
|
+
/** Max characters of a tool result given to the model. Default 8000. */
|
|
144
|
+
maxToolResultChars?: number;
|
|
145
|
+
/**
|
|
146
|
+
* Called before every tool runs. Return `false` to block it – handy for
|
|
147
|
+
* asking the user to confirm POST/PUT/DELETE calls.
|
|
148
|
+
*/
|
|
149
|
+
beforeToolCall?: (call: {
|
|
150
|
+
name: string;
|
|
151
|
+
args: Record<string, any>;
|
|
152
|
+
/** HTTP method for tools generated from `api`, otherwise undefined. */
|
|
153
|
+
method?: HttpMethod;
|
|
154
|
+
}) => boolean | Promise<boolean>;
|
|
155
|
+
onToolCall?: (call: {
|
|
156
|
+
name: string;
|
|
157
|
+
args: Record<string, any>;
|
|
158
|
+
result: unknown;
|
|
159
|
+
error?: string;
|
|
160
|
+
}) => void;
|
|
161
|
+
onError?: (error: Error) => void;
|
|
162
|
+
};
|
|
163
|
+
type ChatState = {
|
|
164
|
+
messages: ChatMessage[];
|
|
165
|
+
loading: boolean;
|
|
166
|
+
/** Human-readable progress, e.g. "Calling get_order…". */
|
|
167
|
+
status: string | null;
|
|
168
|
+
};
|
|
169
|
+
|
|
14
170
|
type BaseUIProps = {
|
|
15
171
|
title?: string;
|
|
16
172
|
align?: "left" | "right";
|
|
@@ -21,38 +177,99 @@ type BaseUIProps = {
|
|
|
21
177
|
buttonLabel?: string;
|
|
22
178
|
placeholder?: string;
|
|
23
179
|
borderRadius?: number;
|
|
180
|
+
/** Show the trash button in the header. Default true. */
|
|
181
|
+
showClearButton?: boolean;
|
|
182
|
+
/** Start with the chat window open. Default false. */
|
|
183
|
+
defaultOpen?: boolean;
|
|
184
|
+
/** Clickable starter prompts shown in the empty chat. */
|
|
185
|
+
suggestions?: string[];
|
|
24
186
|
};
|
|
25
|
-
type
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
providerURL: string;
|
|
32
|
-
apiKey?: never;
|
|
187
|
+
type MCPAgentProps = ChatConfig & BaseUIProps;
|
|
188
|
+
/** Imperative handle available through `ref` on `<MCPAgent />`. */
|
|
189
|
+
type MCPAgentHandle = {
|
|
190
|
+
clear: () => void;
|
|
191
|
+
send: (text: string) => Promise<void>;
|
|
192
|
+
stop: () => void;
|
|
33
193
|
};
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
194
|
+
|
|
195
|
+
declare const MCPAgent: react.ForwardRefExoticComponent<ProviderConfig & {
|
|
196
|
+
systemPrompt?: string;
|
|
197
|
+
tools?: MCPTool[];
|
|
198
|
+
api?: ApiConfig;
|
|
199
|
+
persist?: boolean | string;
|
|
200
|
+
maxHistoryMessages?: number;
|
|
201
|
+
maxToolRounds?: number;
|
|
202
|
+
maxToolResultChars?: number;
|
|
203
|
+
beforeToolCall?: (call: {
|
|
204
|
+
name: string;
|
|
205
|
+
args: Record<string, any>;
|
|
206
|
+
method?: HttpMethod;
|
|
207
|
+
}) => boolean | Promise<boolean>;
|
|
208
|
+
onToolCall?: (call: {
|
|
209
|
+
name: string;
|
|
210
|
+
args: Record<string, any>;
|
|
211
|
+
result: unknown;
|
|
212
|
+
error?: string;
|
|
213
|
+
}) => void;
|
|
214
|
+
onError?: (error: Error) => void;
|
|
215
|
+
} & BaseUIProps & react.RefAttributes<MCPAgentHandle>>;
|
|
216
|
+
|
|
217
|
+
/**
|
|
218
|
+
* Framework-agnostic chat core: history, system prompt, tool-calling loop.
|
|
219
|
+
* Everything runs in the browser.
|
|
220
|
+
*/
|
|
221
|
+
declare class ChatEngine {
|
|
222
|
+
private cfg;
|
|
223
|
+
private provider;
|
|
224
|
+
private providerSig;
|
|
225
|
+
private state;
|
|
226
|
+
private listeners;
|
|
227
|
+
private abortCtl;
|
|
228
|
+
private apiTools;
|
|
229
|
+
private apiSig;
|
|
230
|
+
constructor(config: ChatConfig);
|
|
231
|
+
/** Update config without losing history. Safe to call on every render. */
|
|
232
|
+
configure(config: ChatConfig): void;
|
|
233
|
+
private sigOf;
|
|
234
|
+
private makeProvider;
|
|
235
|
+
getState: () => ChatState;
|
|
236
|
+
subscribe: (fn: () => void) => () => void;
|
|
237
|
+
private set;
|
|
238
|
+
private emit;
|
|
239
|
+
private storageKey;
|
|
240
|
+
private load;
|
|
241
|
+
private save;
|
|
242
|
+
/** Wipe the conversation (and the saved copy). Also cancels a request in flight. */
|
|
243
|
+
clear(): void;
|
|
244
|
+
abort(): void;
|
|
245
|
+
send(text: string): Promise<void>;
|
|
246
|
+
private lastIndexOf;
|
|
247
|
+
private getTools;
|
|
248
|
+
/** Messages sent to the model: no UI-only errors, windowed to maxHistoryMessages at a turn boundary. */
|
|
249
|
+
private requestMessages;
|
|
250
|
+
private run;
|
|
251
|
+
private execute;
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
/**
|
|
255
|
+
* Headless hook – build your own UI on top of the same engine.
|
|
256
|
+
*
|
|
257
|
+
* const { messages, send, clear, loading } = useChat({ provider, model, apiKey, api });
|
|
258
|
+
*/
|
|
259
|
+
declare function useChat(config: ChatConfig): {
|
|
260
|
+
send: (text: string) => Promise<void>;
|
|
261
|
+
clear: () => void;
|
|
262
|
+
stop: () => void;
|
|
263
|
+
engine: ChatEngine;
|
|
264
|
+
messages: ChatMessage[];
|
|
265
|
+
loading: boolean;
|
|
266
|
+
status: string | null;
|
|
53
267
|
};
|
|
54
|
-
type MCPAgentProps = OllamaAgentProps | GroqAgentProps | OpenaiAgentProps | GeminiAgentProps | DeepSeekAgentProps;
|
|
55
268
|
|
|
56
|
-
declare function
|
|
269
|
+
declare function endpointsFromOpenAPI(rawDoc: any): ApiEndpoint[];
|
|
270
|
+
|
|
271
|
+
/** Default base URLs. Override with `providerURL`. */
|
|
272
|
+
declare const PROVIDER_URLS: Record<Exclude<ProviderName, "openai-compatible" | "ollama">, string>;
|
|
273
|
+
declare function createProvider(cfg: ProviderConfig): Provider;
|
|
57
274
|
|
|
58
|
-
export { MCPAgent, type MCPAgentProps, type MCPTool };
|
|
275
|
+
export { type ApiConfig, type ApiEndpoint, type ApiParameter, type ChatConfig, ChatEngine, type ChatMessage, type ChatState, DEFAULT_SYSTEM_PROMPT, MCPAgent, type MCPAgentHandle, type MCPAgentProps, type MCPTool, type MCPToolParameter, PROVIDER_URLS, type Provider, type ProviderConfig, type ProviderName, type ToolCall, createProvider, endpointsFromOpenAPI, useChat };
|