@agient/chatbot 1.1.0 → 1.2.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 +14 -38
- package/dist/index.d.ts +96 -118
- package/dist/index.js +1 -10395
- package/package.json +14 -34
package/README.md
CHANGED
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
|
|
7
7
|
<h1>Agient</h1>
|
|
8
8
|
|
|
9
|
-
**
|
|
9
|
+
**Embed an AI chatbot on your website with minimal setup. Register client-side tools to let the AI invoke actions in the user's browser.**
|
|
10
10
|
|
|
11
11
|
</div>
|
|
12
12
|
|
|
@@ -18,27 +18,9 @@
|
|
|
18
18
|
npm i @agient/chatbot
|
|
19
19
|
```
|
|
20
20
|
|
|
21
|
-
**using [yarn](https://yarnpkg.com)**
|
|
22
|
-
|
|
23
|
-
```shell
|
|
24
|
-
yarn add @agient/chatbot
|
|
25
|
-
```
|
|
26
|
-
|
|
27
|
-
**using [pnpm](https://pnpm.io)**
|
|
28
|
-
|
|
29
|
-
```shell
|
|
30
|
-
pnpm add @agient/chatbot
|
|
31
|
-
```
|
|
32
|
-
|
|
33
|
-
**using [bun](https://bun.sh)**
|
|
34
|
-
|
|
35
|
-
```shell
|
|
36
|
-
bun i @agient/chatbot
|
|
37
|
-
```
|
|
38
|
-
|
|
39
21
|
## 🚀 Quick Start
|
|
40
22
|
|
|
41
|
-
|
|
23
|
+
Head to [agient.dev](https://agient.dev) and create your own chatbot project. Once you've created your project, you'll receive an API key that you'll need to initialize the chatbot in your application.
|
|
42
24
|
|
|
43
25
|
```typescript
|
|
44
26
|
import { createAgient } from '@agient/chatbot';
|
|
@@ -46,11 +28,15 @@ import { createAgient } from '@agient/chatbot';
|
|
|
46
28
|
// Initialize the chatbot with your API key
|
|
47
29
|
const agient = createAgient('your-api-key');
|
|
48
30
|
|
|
49
|
-
|
|
50
|
-
|
|
31
|
+
// Called when the AI decides to invoke the tool
|
|
32
|
+
agient.on('getOrderStatus', async ({ orderId }) => {
|
|
33
|
+
const order = await myApi.getOrder(orderId);
|
|
34
|
+
return `Order ${orderId} is ${order.status}`;
|
|
51
35
|
});
|
|
52
36
|
```
|
|
53
37
|
|
|
38
|
+
Tools execute client-side (in the user's browser) and return results to the AI. No server changes needed on your end.
|
|
39
|
+
|
|
54
40
|
## ⚙️ Configuration Options
|
|
55
41
|
|
|
56
42
|
The `createAgient` function accepts the following options:
|
|
@@ -63,22 +49,12 @@ interface AgientOptions {
|
|
|
63
49
|
* @default true
|
|
64
50
|
*/
|
|
65
51
|
autoStart?: boolean;
|
|
52
|
+
/**
|
|
53
|
+
* Connection timeout in milliseconds.
|
|
54
|
+
* @default 10000
|
|
55
|
+
*/
|
|
56
|
+
timeout?: number;
|
|
66
57
|
}
|
|
67
58
|
```
|
|
68
59
|
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
The chatbot supports custom tools that can be implemented to perform specific actions.
|
|
72
|
-
Tools must first be registered inside your chatbot at https://agient.dev. Then you can implement them by their names.
|
|
73
|
-
Here's how to implement a tool:
|
|
74
|
-
|
|
75
|
-
```typescript
|
|
76
|
-
import { createAgient } from '@agient/chatbot';
|
|
77
|
-
|
|
78
|
-
const agient = createAgient('your-api-key');
|
|
79
|
-
|
|
80
|
-
agient.on('incrementCounter', async ({ by }) => {
|
|
81
|
-
const newCount = await incrementCounter(by);
|
|
82
|
-
return `The counter is now ${newCount}`;
|
|
83
|
-
});
|
|
84
|
-
```
|
|
60
|
+
> This package is a thin wrapper — `src/index.ts` re-exports everything from `@agient/widget`. All business logic lives in `libs/widget`.
|
package/dist/index.d.ts
CHANGED
|
@@ -1,108 +1,60 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
*
|
|
7
|
-
* @example
|
|
8
|
-
* ```typescript
|
|
9
|
-
* const agient = createAgient('api-key');
|
|
10
|
-
*
|
|
11
|
-
* // Register a handler for the 'incrementCounter' tool
|
|
12
|
-
* agient.on('incrementCounter', async ({ by }) => {
|
|
13
|
-
* const newCount = await incrementCounter(by);
|
|
14
|
-
* return `The counter is now ${newCount}`;
|
|
15
|
-
* });
|
|
16
|
-
* ```
|
|
17
|
-
*/
|
|
18
|
-
export declare class AgientInstance<T extends ToolsMap = DefaultToolsMap> {
|
|
19
|
-
private readonly apiKey;
|
|
20
|
-
private readonly options;
|
|
21
|
-
/**
|
|
22
|
-
* The status of the chatbot.
|
|
23
|
-
*/
|
|
24
|
-
private status;
|
|
25
|
-
/**
|
|
26
|
-
* The session ID for the chatbot.
|
|
27
|
-
*/
|
|
28
|
-
private _sessionId;
|
|
29
|
-
/**
|
|
30
|
-
* The socket for the chatbot.
|
|
31
|
-
*/
|
|
32
|
-
private _socket;
|
|
33
|
-
/**
|
|
34
|
-
* Whether the chatbot is in playground mode. Used only internally at https://agient.dev
|
|
35
|
-
*/
|
|
36
|
-
private readonly isPlayground;
|
|
37
|
-
/**
|
|
38
|
-
* When the chatbot is active, a provider object is initialized
|
|
39
|
-
*/
|
|
40
|
-
private provider;
|
|
41
|
-
/**
|
|
42
|
-
* Passthrough to AgientElement.onMount
|
|
43
|
-
*/
|
|
44
|
-
private onMount?;
|
|
45
|
-
/**
|
|
46
|
-
* The tools registered by the consumer of the widget.
|
|
47
|
-
*/
|
|
48
|
-
private readonly registeredTools;
|
|
49
|
-
constructor(apiKey: string, options: AgientOptions);
|
|
50
|
-
/**
|
|
51
|
-
* Register a tool handler for a tool defined in the agient app.
|
|
52
|
-
*
|
|
53
|
-
* @param event - The name of the tool to register a handler for.
|
|
54
|
-
* @param fn - The handler for the tool.
|
|
55
|
-
*/
|
|
56
|
-
on<Tool extends ToolKeys<T>>(event: Tool, fn: (args: T[Tool]) => string | Promise<string>): void;
|
|
1
|
+
import { FlexibleSchema } from '@ai-sdk/provider-utils';
|
|
2
|
+
|
|
3
|
+
export declare class AgientInstance {
|
|
4
|
+
private readonly coreReady;
|
|
5
|
+
constructor(apiKey: string, options: CreateAgientOptions);
|
|
57
6
|
/**
|
|
58
|
-
*
|
|
7
|
+
* Starts the chatbot client.
|
|
8
|
+
* @returns A promise that resolves when the chatbot is connected.
|
|
59
9
|
*/
|
|
60
10
|
start(): Promise<void>;
|
|
61
11
|
/**
|
|
62
|
-
*
|
|
12
|
+
* Stops the chatbot client.
|
|
63
13
|
*/
|
|
64
|
-
stop():
|
|
14
|
+
stop(): void;
|
|
65
15
|
/**
|
|
66
|
-
*
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
private get socket();
|
|
70
|
-
private get sessionId();
|
|
71
|
-
/**
|
|
72
|
-
* Create a socket for the chatbot.
|
|
16
|
+
* Registers a client-side tool that the AI agent can invoke during a conversation.
|
|
17
|
+
* The tool becomes available to the agent for the lifetime of the session, until
|
|
18
|
+
* {@link unregisterTool} is called with the returned token.
|
|
73
19
|
*
|
|
74
|
-
* @param
|
|
75
|
-
*
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
*
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
*
|
|
90
|
-
*
|
|
91
|
-
*
|
|
20
|
+
* @param name - A human-readable name for the tool (e.g. `"getWeather"`). Internally
|
|
21
|
+
* suffixed with a UUID to avoid collisions.
|
|
22
|
+
* @param options - Tool configuration including description, input schema, and execute function.
|
|
23
|
+
* @returns An opaque token used to unregister the tool later.
|
|
24
|
+
*
|
|
25
|
+
* @example
|
|
26
|
+
* ```ts
|
|
27
|
+
* import { z } from 'zod';
|
|
28
|
+
*
|
|
29
|
+
* const token = await agient.registerTool('getWeather', {
|
|
30
|
+
* description: 'Get the current weather for a given city.',
|
|
31
|
+
* inputSchema: z.object({
|
|
32
|
+
* city: z.string().describe('The city to look up'),
|
|
33
|
+
* }),
|
|
34
|
+
* execute: async ({ city }) => {
|
|
35
|
+
* const res = await fetch(`/api/weather?city=${city}`);
|
|
36
|
+
* return res.json();
|
|
37
|
+
* },
|
|
38
|
+
* });
|
|
39
|
+
*
|
|
40
|
+
* // Later, when the tool is no longer needed:
|
|
41
|
+
* agient.unregisterTool(token);
|
|
42
|
+
* ```
|
|
92
43
|
*/
|
|
93
|
-
|
|
44
|
+
registerTool<TInput, TOutput>(name: string, options: RegisterToolOptions<TInput, TOutput>): Promise<UnregisterToolToken>;
|
|
94
45
|
/**
|
|
95
|
-
*
|
|
96
|
-
*
|
|
46
|
+
* Unregisters a previously registered tool, removing it from the agent's available tools.
|
|
47
|
+
*
|
|
48
|
+
* @param token - The opaque token returned by {@link registerTool}.
|
|
97
49
|
*/
|
|
98
|
-
|
|
50
|
+
unregisterTool(token: UnregisterToolToken): void;
|
|
99
51
|
}
|
|
100
52
|
|
|
101
53
|
/**
|
|
102
54
|
* Creates a new Agient instance for embedding the chat widget into your application.
|
|
103
55
|
*
|
|
104
56
|
* @param apiKey - Your Agient API key for authentication
|
|
105
|
-
* @param options - Configuration options for the widget instance. See {@link
|
|
57
|
+
* @param options - Configuration options for the widget instance. See {@link CreateAgientOptions} for available options.
|
|
106
58
|
* @returns An {@link AgientInstance} that provides methods to interact with the widget.
|
|
107
59
|
*
|
|
108
60
|
* @example
|
|
@@ -111,42 +63,68 @@ export declare interface AgientOptions {
|
|
|
111
63
|
* autoStart: true
|
|
112
64
|
* });
|
|
113
65
|
*
|
|
114
|
-
* agient.
|
|
115
|
-
*
|
|
116
|
-
*
|
|
66
|
+
* const unregisterToken = await agient.registerTool('getWeather', ({
|
|
67
|
+
* description: 'Get the weather for a given city',
|
|
68
|
+
* inputSchema: z.object({
|
|
69
|
+
* city: z.string(),
|
|
70
|
+
* }),
|
|
71
|
+
* execute: async ({ city }) => `The weather in ${city} is sunny`,
|
|
72
|
+
* metadata: {
|
|
73
|
+
* requiresConfirmation: true,
|
|
74
|
+
* showContextMessage: true,
|
|
75
|
+
* },
|
|
117
76
|
* });
|
|
118
77
|
* ```
|
|
119
78
|
*/
|
|
120
|
-
export declare const createAgient:
|
|
79
|
+
export declare const createAgient: (apiKey: string, options: CreateAgientOptions) => AgientInstance;
|
|
121
80
|
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
81
|
+
export declare interface CreateAgientOptions {
|
|
82
|
+
/**
|
|
83
|
+
* Maximum time to wait for socket operations to complete.
|
|
84
|
+
* @default 10_000
|
|
85
|
+
*/
|
|
86
|
+
timeout?: number;
|
|
87
|
+
/**
|
|
88
|
+
* Whether to automatically start the chatbot client.
|
|
89
|
+
* @default true
|
|
90
|
+
*/
|
|
91
|
+
autoStart?: boolean;
|
|
92
|
+
}
|
|
129
93
|
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
94
|
+
export declare interface RegisterToolOptions<TInput, TOutput> {
|
|
95
|
+
/**
|
|
96
|
+
* The description of the tool.
|
|
97
|
+
*/
|
|
98
|
+
description: string;
|
|
99
|
+
/**
|
|
100
|
+
* The schema for the tool's input.
|
|
101
|
+
*/
|
|
102
|
+
inputSchema?: FlexibleSchema<TInput>;
|
|
103
|
+
/**
|
|
104
|
+
* The function to execute when the tool is called.
|
|
105
|
+
*/
|
|
106
|
+
execute: (input: TInput) => Promise<TOutput> | TOutput;
|
|
107
|
+
/**
|
|
108
|
+
* Additional metadata about the tool.
|
|
109
|
+
*/
|
|
110
|
+
metadata?: {
|
|
111
|
+
/**
|
|
112
|
+
* Whether the tool requires user confirmation before execution.
|
|
113
|
+
* @default false
|
|
114
|
+
*/
|
|
115
|
+
requiresConfirmation?: boolean;
|
|
116
|
+
/**
|
|
117
|
+
* Whether the tool should show a context message to the user while executing.
|
|
118
|
+
* @default false
|
|
119
|
+
*/
|
|
120
|
+
showContextMessage?: boolean;
|
|
121
|
+
};
|
|
122
|
+
}
|
|
137
123
|
|
|
138
|
-
|
|
139
|
-
* Extracts the available tool names from a {@link ToolsMap}.
|
|
140
|
-
* @template Tools - The tools map type to extract keys from
|
|
141
|
-
*/
|
|
142
|
-
export declare type ToolKeys<Tools extends ToolsMap> = keyof Tools & (string | symbol);
|
|
124
|
+
declare const UnregisterToolBrand: unique symbol;
|
|
143
125
|
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
* Each tool is defined as a record of string keys to {@link ToolAgrument} values.
|
|
147
|
-
*/
|
|
148
|
-
export declare type ToolsMap = {
|
|
149
|
-
[key: string]: Record<string, ToolAgrument>;
|
|
126
|
+
export declare type UnregisterToolToken = string & {
|
|
127
|
+
readonly [UnregisterToolBrand]: never;
|
|
150
128
|
};
|
|
151
129
|
|
|
152
130
|
export { }
|