@automattic/agenttic-client 0.1.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 +409 -0
- package/dist/MockSalesGraph-ORPXGMCD.js +7 -0
- package/dist/chunk-LAB4XYXR.js +688 -0
- package/dist/chunk-ZDG7Y2LU.js +39 -0
- package/dist/client/index.d.ts +44 -0
- package/dist/client/index.d.ts.map +1 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +2137 -0
- package/dist/message-actions/factories.d.ts.map +1 -0
- package/dist/message-actions/index.d.ts.map +1 -0
- package/dist/message-actions/resolver.d.ts.map +1 -0
- package/dist/message-actions/useMessageActions.d.ts.map +1 -0
- package/dist/mocks/index.js +288 -0
- package/dist/react/agentManager.d.ts.map +1 -0
- package/dist/react/useAgentChat.d.ts.map +1 -0
- package/package.json +74 -0
package/README.md
ADDED
|
@@ -0,0 +1,409 @@
|
|
|
1
|
+
# @automattic/agenttic-client
|
|
2
|
+
|
|
3
|
+
A TypeScript client library for Agent2Agent (A2A) protocol communication with React hooks and component integration. Built for seamless AI agent integration in both browser and Node.js environments.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @automattic/agenttic-client
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Key Features
|
|
12
|
+
|
|
13
|
+
- React hooks for agent communication (`useAgentChat`, `useClientContext`, `useClientTools`)
|
|
14
|
+
- Streaming and non-streaming responses
|
|
15
|
+
- Tool execution system with automatic handling
|
|
16
|
+
- Context injection for each message
|
|
17
|
+
- Conversation persistence and management
|
|
18
|
+
- TypeScript support
|
|
19
|
+
- Message actions and markdown rendering
|
|
20
|
+
|
|
21
|
+
## Quick Start
|
|
22
|
+
|
|
23
|
+
### Basic Chat Integration
|
|
24
|
+
|
|
25
|
+
```typescript
|
|
26
|
+
import { useAgentChat } from '@automattic/agenttic-client';
|
|
27
|
+
|
|
28
|
+
function ChatComponent() {
|
|
29
|
+
const {
|
|
30
|
+
messages,
|
|
31
|
+
isProcessing,
|
|
32
|
+
error,
|
|
33
|
+
onSubmit,
|
|
34
|
+
registerSuggestions,
|
|
35
|
+
registerMarkdownComponents
|
|
36
|
+
} = useAgentChat({
|
|
37
|
+
agentId: 'big-sky',
|
|
38
|
+
sessionId: 'my-session-123',
|
|
39
|
+
authProvider: async () => ({ Authorization: 'Bearer your-token' })
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
return (
|
|
43
|
+
<div>
|
|
44
|
+
{messages.map(msg => (
|
|
45
|
+
<div key={msg.id} className={msg.role}>
|
|
46
|
+
{msg.content.map(content => content.text).join('')}
|
|
47
|
+
</div>
|
|
48
|
+
))}
|
|
49
|
+
<input
|
|
50
|
+
onKeyDown={(e) => e.key === 'Enter' && onSubmit(e.target.value)}
|
|
51
|
+
disabled={isProcessing}
|
|
52
|
+
/>
|
|
53
|
+
{error && <div>Error: {error}</div>}
|
|
54
|
+
</div>
|
|
55
|
+
);
|
|
56
|
+
}
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
### With Tools and Context
|
|
60
|
+
|
|
61
|
+
```typescript
|
|
62
|
+
import { useAgentChat, useClientTools, useClientContext } from '@automattic/agenttic-client';
|
|
63
|
+
|
|
64
|
+
function AdvancedChatComponent() {
|
|
65
|
+
const contextProvider = useClientContext(() => ({
|
|
66
|
+
page: window.location.href,
|
|
67
|
+
timestamp: Date.now(),
|
|
68
|
+
userRole: getCurrentUserRole()
|
|
69
|
+
}));
|
|
70
|
+
|
|
71
|
+
const toolProvider = useClientTools(
|
|
72
|
+
async () => [
|
|
73
|
+
{
|
|
74
|
+
id: 'calculator',
|
|
75
|
+
name: 'Calculator',
|
|
76
|
+
description: 'Perform mathematical calculations',
|
|
77
|
+
input_schema: {
|
|
78
|
+
type: 'object',
|
|
79
|
+
properties: {
|
|
80
|
+
expression: { type: 'string' }
|
|
81
|
+
},
|
|
82
|
+
required: ['expression']
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
],
|
|
86
|
+
async (toolId, args) => {
|
|
87
|
+
if (toolId === 'calculator') {
|
|
88
|
+
return { result: calc(args.expression)};
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
);
|
|
92
|
+
|
|
93
|
+
const chat = useAgentChat({
|
|
94
|
+
agentId: 'big-sky',
|
|
95
|
+
contextProvider,
|
|
96
|
+
toolProvider,
|
|
97
|
+
authProvider: async () => ({ Authorization: 'Bearer token' })
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
return <ChatInterface {...chat} />;
|
|
101
|
+
}
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
## Core APIs
|
|
105
|
+
|
|
106
|
+
### useAgentChat Hook
|
|
107
|
+
|
|
108
|
+
The primary hook for chat functionality, providing everything needed for a complete chat interface.
|
|
109
|
+
|
|
110
|
+
```typescript
|
|
111
|
+
const {
|
|
112
|
+
// Chat state
|
|
113
|
+
messages, // UIMessage[] - formatted for display
|
|
114
|
+
isProcessing, // boolean - request in progress
|
|
115
|
+
error, // string | null - last error
|
|
116
|
+
|
|
117
|
+
// Core methods
|
|
118
|
+
onSubmit, // (message: string) => Promise<void>
|
|
119
|
+
|
|
120
|
+
// Configuration
|
|
121
|
+
registerSuggestions, // (suggestions: Suggestion[]) => void
|
|
122
|
+
registerMarkdownComponents, // (components: MarkdownComponents) => void
|
|
123
|
+
registerMessageActions, // (registration: MessageActionsRegistration) => void
|
|
124
|
+
|
|
125
|
+
// Utilities
|
|
126
|
+
messageRenderer, // React component for markdown rendering
|
|
127
|
+
addMessage, // (message: UIMessage) => void
|
|
128
|
+
} = useAgentChat( config );
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
**Config Options:**
|
|
132
|
+
|
|
133
|
+
- `agentId: string` - Required. Agent identifier
|
|
134
|
+
- `agentUrl?: string` - Agent endpoint URL (defaults to WordPress.com)
|
|
135
|
+
- `sessionId?: string` - Session ID for conversation persistence
|
|
136
|
+
- `contextProvider?: ContextProvider` - Dynamic context injection
|
|
137
|
+
- `toolProvider?: ToolProvider` - Tool execution capabilities
|
|
138
|
+
- `authProvider?: AuthProvider` - Authentication headers
|
|
139
|
+
|
|
140
|
+
### useClientContext Hook
|
|
141
|
+
|
|
142
|
+
Provides dynamic context that refreshes with each message.
|
|
143
|
+
|
|
144
|
+
```typescript
|
|
145
|
+
const contextProvider = useClientContext( () => ( {
|
|
146
|
+
currentPage: {
|
|
147
|
+
url: window.location.href,
|
|
148
|
+
title: document.title,
|
|
149
|
+
selectedText: getSelection(),
|
|
150
|
+
},
|
|
151
|
+
user: {
|
|
152
|
+
role: getUserRole(),
|
|
153
|
+
permissions: getPermissions(),
|
|
154
|
+
},
|
|
155
|
+
timestamp: Date.now(),
|
|
156
|
+
} ) );
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
### useClientTools Hook
|
|
160
|
+
|
|
161
|
+
Enables agents to execute tools in your application.
|
|
162
|
+
|
|
163
|
+
```typescript
|
|
164
|
+
const toolProvider = useClientTools(
|
|
165
|
+
// Define available tools
|
|
166
|
+
async () => [
|
|
167
|
+
{
|
|
168
|
+
id: 'file-reader',
|
|
169
|
+
name: 'File Reader',
|
|
170
|
+
description: 'Read file contents',
|
|
171
|
+
input_schema: {
|
|
172
|
+
type: 'object',
|
|
173
|
+
properties: {
|
|
174
|
+
path: { type: 'string' },
|
|
175
|
+
},
|
|
176
|
+
required: [ 'path' ],
|
|
177
|
+
},
|
|
178
|
+
},
|
|
179
|
+
],
|
|
180
|
+
// Execute tool calls
|
|
181
|
+
async ( toolId, args ) => {
|
|
182
|
+
if ( toolId === 'file-reader' ) {
|
|
183
|
+
return { content: await readFile( args.path ) };
|
|
184
|
+
}
|
|
185
|
+
throw new Error( `Unknown tool: ${ toolId }` );
|
|
186
|
+
}
|
|
187
|
+
);
|
|
188
|
+
```
|
|
189
|
+
|
|
190
|
+
### createClient Function
|
|
191
|
+
|
|
192
|
+
Low-level client for direct A2A communication without React.
|
|
193
|
+
|
|
194
|
+
```typescript
|
|
195
|
+
import { createClient } from '@automattic/agenttic-client';
|
|
196
|
+
|
|
197
|
+
const client = createClient({
|
|
198
|
+
agentId: 'big-sky',
|
|
199
|
+
authProvider: async () => ({ Authorization: 'Bearer token' }),
|
|
200
|
+
toolProvider: {
|
|
201
|
+
getAvailableTools: async () => [...],
|
|
202
|
+
executeTool: async (toolId, args) => ({ result: '...' })
|
|
203
|
+
}
|
|
204
|
+
});
|
|
205
|
+
|
|
206
|
+
// Non-streaming
|
|
207
|
+
const task = await client.sendMessage({
|
|
208
|
+
message: createTextMessage('Hello'),
|
|
209
|
+
sessionId: 'session-123'
|
|
210
|
+
});
|
|
211
|
+
|
|
212
|
+
// Streaming
|
|
213
|
+
for await (const update of client.sendMessageStream({
|
|
214
|
+
message: createTextMessage('Hello'),
|
|
215
|
+
sessionId: 'session-123'
|
|
216
|
+
})) {
|
|
217
|
+
console.log(update.text);
|
|
218
|
+
if (update.final) break;
|
|
219
|
+
}
|
|
220
|
+
```
|
|
221
|
+
|
|
222
|
+
### Agent Manager
|
|
223
|
+
|
|
224
|
+
Functional singleton for managing multiple agent instances.
|
|
225
|
+
|
|
226
|
+
```typescript
|
|
227
|
+
import { getAgentManager } from '@automattic/agenttic-client';
|
|
228
|
+
|
|
229
|
+
const manager = getAgentManager();
|
|
230
|
+
|
|
231
|
+
// Create agent
|
|
232
|
+
await manager.createAgent( 'my-agent', {
|
|
233
|
+
agentId: 'big-sky',
|
|
234
|
+
sessionId: 'session-123',
|
|
235
|
+
contextProvider,
|
|
236
|
+
toolProvider,
|
|
237
|
+
} );
|
|
238
|
+
|
|
239
|
+
// Send messages
|
|
240
|
+
const task = await manager.sendMessage( 'my-agent', 'Hello' );
|
|
241
|
+
|
|
242
|
+
// Streaming
|
|
243
|
+
for await ( const update of manager.sendMessageStream( 'my-agent', 'Hello' ) ) {
|
|
244
|
+
console.log( update );
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
// Manage conversation
|
|
248
|
+
const history = manager.getConversationHistory( 'my-agent' );
|
|
249
|
+
await manager.resetConversation( 'my-agent' );
|
|
250
|
+
```
|
|
251
|
+
|
|
252
|
+
## Additional Features
|
|
253
|
+
|
|
254
|
+
### Message Actions
|
|
255
|
+
|
|
256
|
+
Add interactive buttons to agent messages:
|
|
257
|
+
|
|
258
|
+
```typescript
|
|
259
|
+
const { registerMessageActions, createFeedbackActions } =
|
|
260
|
+
useAgentChat( config );
|
|
261
|
+
|
|
262
|
+
// Built-in feedback actions
|
|
263
|
+
registerMessageActions(
|
|
264
|
+
createFeedbackActions( {
|
|
265
|
+
onFeedback: async ( messageId, feedback ) => {
|
|
266
|
+
console.log( `${ feedback } feedback for ${ messageId }` );
|
|
267
|
+
},
|
|
268
|
+
icons: { up: '👍', down: '👎' },
|
|
269
|
+
} )
|
|
270
|
+
);
|
|
271
|
+
|
|
272
|
+
// Custom actions
|
|
273
|
+
registerMessageActions( {
|
|
274
|
+
id: 'copy-actions',
|
|
275
|
+
actions: [
|
|
276
|
+
{
|
|
277
|
+
id: 'copy',
|
|
278
|
+
label: 'Copy',
|
|
279
|
+
icon: '📋',
|
|
280
|
+
onClick: ( message ) =>
|
|
281
|
+
navigator.clipboard.writeText( message.content[ 0 ].text ),
|
|
282
|
+
},
|
|
283
|
+
],
|
|
284
|
+
} );
|
|
285
|
+
```
|
|
286
|
+
|
|
287
|
+
### Markdown Extensions
|
|
288
|
+
|
|
289
|
+
Extend markdown rendering with custom components:
|
|
290
|
+
|
|
291
|
+
```typescript
|
|
292
|
+
import { BarChart, LineChart } from '@automattic/agenttic-client';
|
|
293
|
+
|
|
294
|
+
const { registerMarkdownComponents, registerMarkdownExtensions } = useAgentChat(config);
|
|
295
|
+
// Register chart components
|
|
296
|
+
registerMarkdownComponents( {
|
|
297
|
+
// Custom heading styles
|
|
298
|
+
h1: ({ children }) => (
|
|
299
|
+
<h1 className="text-2xl font-bold text-brand">{children}</h1>
|
|
300
|
+
),
|
|
301
|
+
// Custom code blocks with syntax highlighting
|
|
302
|
+
code: ({ children, className }) => (
|
|
303
|
+
<SyntaxHighlighter language={className}>
|
|
304
|
+
{children}
|
|
305
|
+
</SyntaxHighlighter>
|
|
306
|
+
),
|
|
307
|
+
// Custom link handling
|
|
308
|
+
a: ({ href, children }) => (
|
|
309
|
+
<a href={href} target="_blank" rel="noopener">
|
|
310
|
+
{children} ↗
|
|
311
|
+
</a>
|
|
312
|
+
),
|
|
313
|
+
blockquote: ( { children, ...props } ) => (
|
|
314
|
+
<blockquote
|
|
315
|
+
{ ...props }
|
|
316
|
+
style={ {
|
|
317
|
+
borderLeft: '4px solid #007cba',
|
|
318
|
+
backgroundColor: '#f0f8ff',
|
|
319
|
+
margin: '16px 0',
|
|
320
|
+
padding: '12px 16px',
|
|
321
|
+
fontStyle: 'italic',
|
|
322
|
+
borderRadius: '0 4px 4px 0',
|
|
323
|
+
} }
|
|
324
|
+
>
|
|
325
|
+
{ children }
|
|
326
|
+
</blockquote>
|
|
327
|
+
),
|
|
328
|
+
});
|
|
329
|
+
|
|
330
|
+
// Register custom extensions
|
|
331
|
+
registerMarkdownExtensions({
|
|
332
|
+
charts: {
|
|
333
|
+
BarChart,
|
|
334
|
+
LineChart
|
|
335
|
+
}
|
|
336
|
+
});
|
|
337
|
+
```
|
|
338
|
+
|
|
339
|
+
### Conversation Suggestions
|
|
340
|
+
|
|
341
|
+
Provide suggested prompts to users:
|
|
342
|
+
|
|
343
|
+
```typescript
|
|
344
|
+
const { registerSuggestions, clearSuggestions } = useAgentChat( config );
|
|
345
|
+
|
|
346
|
+
registerSuggestions( [
|
|
347
|
+
{
|
|
348
|
+
id: '1',
|
|
349
|
+
label: 'Help me write code',
|
|
350
|
+
prompt: 'Can you help me write a function?',
|
|
351
|
+
},
|
|
352
|
+
{
|
|
353
|
+
id: '2',
|
|
354
|
+
label: 'Explain this error',
|
|
355
|
+
prompt: 'What does this error mean?',
|
|
356
|
+
},
|
|
357
|
+
] );
|
|
358
|
+
```
|
|
359
|
+
|
|
360
|
+
## Type Definitions
|
|
361
|
+
|
|
362
|
+
```typescript
|
|
363
|
+
interface UIMessage {
|
|
364
|
+
id: string;
|
|
365
|
+
role: 'user' | 'agent';
|
|
366
|
+
content: Array< {
|
|
367
|
+
type: 'text' | 'image_url' | 'component';
|
|
368
|
+
text?: string;
|
|
369
|
+
image_url?: string;
|
|
370
|
+
component?: React.ComponentType;
|
|
371
|
+
} >;
|
|
372
|
+
timestamp: number;
|
|
373
|
+
actions?: UIMessageAction[];
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
interface Tool {
|
|
377
|
+
id: string;
|
|
378
|
+
name: string;
|
|
379
|
+
description: string;
|
|
380
|
+
input_schema: {
|
|
381
|
+
type: 'object';
|
|
382
|
+
properties: Record< string, any >;
|
|
383
|
+
required?: string[];
|
|
384
|
+
};
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
type AuthProvider = () => Promise< Record< string, string > >;
|
|
388
|
+
type ContextProvider = { getClientContext: () => any };
|
|
389
|
+
type ToolProvider = {
|
|
390
|
+
getAvailableTools: () => Promise< Tool[] >;
|
|
391
|
+
executeTool: ( toolId: string, args: any ) => Promise< any >;
|
|
392
|
+
};
|
|
393
|
+
```
|
|
394
|
+
|
|
395
|
+
## Development
|
|
396
|
+
|
|
397
|
+
```bash
|
|
398
|
+
# Build the package
|
|
399
|
+
pnpm build
|
|
400
|
+
|
|
401
|
+
# Run tests
|
|
402
|
+
pnpm test
|
|
403
|
+
|
|
404
|
+
# Type checking
|
|
405
|
+
pnpm type-check
|
|
406
|
+
|
|
407
|
+
# Lint
|
|
408
|
+
pnpm lint
|
|
409
|
+
```
|