@distri/core 0.1.8 → 0.2.2

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 ADDED
@@ -0,0 +1,140 @@
1
+ # @distri/core
2
+
3
+ Core API client for Distri Framework with support for DistriMessage and DistriPart types.
4
+
5
+ ## Features
6
+
7
+ - **DistriMessage**: Enhanced message structure with typed parts
8
+ - **DistriPart**: Typed message parts supporting text, code observations, tool calls, and more
9
+ - **A2A Protocol Integration**: Seamless conversion between A2A and Distri message formats
10
+ - **Enhanced Message Rendering**: Rich display of different message part types
11
+
12
+ ## DistriMessage Structure
13
+
14
+ ```typescript
15
+ interface DistriMessage {
16
+ id: string;
17
+ role: 'user' | 'assistant';
18
+ parts: DistriPart[];
19
+ created_at?: string;
20
+ metadata?: any;
21
+ }
22
+ ```
23
+
24
+ ## DistriPart Types
25
+
26
+ ```typescript
27
+ type DistriPart =
28
+ | { type: 'text'; text: string }
29
+ | { type: 'code_observation'; code_observation: CodeObservation }
30
+ | { type: 'image'; image: FileType }
31
+ | { type: 'data'; data: any }
32
+ | { type: 'tool_call'; tool_call: ToolCall }
33
+ | { type: 'tool_result'; tool_result: ToolResponse }
34
+ | { type: 'plan'; plan: string };
35
+ ```
36
+
37
+ ## Usage
38
+
39
+ ### Creating DistriMessages
40
+
41
+ ```typescript
42
+ import { DistriClient, DistriMessage } from '@distri/core';
43
+
44
+ // Create a simple text message
45
+ const textMessage = DistriClient.initDistriMessage('Hello, world!', 'user');
46
+
47
+ // Create a message with multiple parts
48
+ const complexMessage = DistriClient.initDistriMessage([
49
+ { type: 'text', text: 'Here is my analysis:' },
50
+ {
51
+ type: 'code_observation',
52
+ code_observation: {
53
+ thought: 'I need to analyze this data',
54
+ code: 'const result = data.map(x => x * 2);'
55
+ }
56
+ },
57
+ {
58
+ type: 'tool_call',
59
+ tool_call: {
60
+ tool_call_id: 'call_123',
61
+ tool_name: 'calculate',
62
+ input: { numbers: [1, 2, 3] }
63
+ }
64
+ }
65
+ ], 'assistant');
66
+ ```
67
+
68
+ ### Converting Between Formats
69
+
70
+ ```typescript
71
+ import {
72
+ convertA2AMessageToDistri,
73
+ convertDistriMessageToA2A
74
+ } from '@distri/core';
75
+
76
+ // Convert A2A Message to DistriMessage
77
+ const a2aMessage = /* A2A message from protocol */;
78
+ const distriMessage = convertA2AMessageToDistri(a2aMessage);
79
+
80
+ // Convert DistriMessage to A2A Message
81
+ const a2aMessage = convertDistriMessageToA2A(distriMessage);
82
+ ```
83
+
84
+ ### Extracting Content
85
+
86
+ ```typescript
87
+ import {
88
+ extractTextFromDistriMessage,
89
+ extractToolCallsFromDistriMessage,
90
+ extractToolResultsFromDistriMessage
91
+ } from '@distri/core';
92
+
93
+ // Extract text content
94
+ const text = extractTextFromDistriMessage(message);
95
+
96
+ // Extract tool calls
97
+ const toolCalls = extractToolCallsFromDistriMessage(message);
98
+
99
+ // Extract tool results
100
+ const toolResults = extractToolResultsFromDistriMessage(message);
101
+ ```
102
+
103
+ ## Message Rendering
104
+
105
+ The React package includes enhanced message rendering components that can display all DistriPart types:
106
+
107
+ - **Text**: Standard text rendering with markdown support
108
+ - **Code Observations**: Thought process and code with syntax highlighting
109
+ - **Tool Calls**: Tool execution details with input/output display
110
+ - **Tool Results**: Success/failure status with result data
111
+ - **Plans**: Structured plan display
112
+ - **Images**: Base64 image rendering
113
+ - **Data**: JSON data display
114
+
115
+ ## Migration from A2A Messages
116
+
117
+ The framework provides backward compatibility while encouraging the use of DistriMessage:
118
+
119
+ ```typescript
120
+ // Old way (still supported)
121
+ const oldMessage = DistriClient.initMessage('Hello', 'user');
122
+
123
+ // New way (recommended)
124
+ const newMessage = DistriClient.initDistriMessage('Hello', 'user');
125
+ ```
126
+
127
+ ## Type Safety
128
+
129
+ All DistriPart types are fully typed, providing compile-time safety and better IDE support:
130
+
131
+ ```typescript
132
+ // TypeScript will ensure correct structure
133
+ const part: DistriPart = {
134
+ type: 'code_observation',
135
+ code_observation: {
136
+ thought: 'My analysis',
137
+ code: 'console.log("Hello");'
138
+ }
139
+ };
140
+ ```
package/dist/index.d.mts CHANGED
@@ -1,7 +1,191 @@
1
- import { AgentSkill, Message, TaskStatusUpdateEvent, TaskArtifactUpdateEvent, Task, MessageSendParams } from '@a2a-js/sdk/client';
2
- export * from '@a2a-js/sdk/client';
3
- export { AgentCard, Message, MessageSendParams, Task, TaskArtifactUpdateEvent, TaskStatus, TaskStatusUpdateEvent } from '@a2a-js/sdk/client';
1
+ import { AgentSkill, Message, TaskStatusUpdateEvent, TaskArtifactUpdateEvent, Task, MessageSendParams, Part } from '@a2a-js/sdk/client';
4
2
 
3
+ type Role = 'user' | 'system' | 'assistant';
4
+ interface RunStartedEvent {
5
+ type: 'run_started';
6
+ data: {};
7
+ }
8
+ interface RunFinishedEvent {
9
+ type: 'run_finished';
10
+ data: {};
11
+ }
12
+ interface RunErrorEvent {
13
+ type: 'run_error';
14
+ data: {
15
+ message: string;
16
+ code?: string;
17
+ };
18
+ }
19
+ interface TextMessageStartEvent {
20
+ type: 'text_message_start';
21
+ data: {
22
+ message_id: string;
23
+ role: Role;
24
+ };
25
+ }
26
+ interface TextMessageContentEvent {
27
+ type: 'text_message_content';
28
+ data: {
29
+ message_id: string;
30
+ delta: string;
31
+ };
32
+ }
33
+ interface TextMessageEndEvent {
34
+ type: 'text_message_end';
35
+ data: {
36
+ message_id: string;
37
+ };
38
+ }
39
+ interface ToolCallStartEvent {
40
+ type: 'tool_call_start';
41
+ data: {
42
+ tool_call_id: string;
43
+ tool_call_name: string;
44
+ parent_message_id?: string;
45
+ is_external?: boolean;
46
+ };
47
+ }
48
+ interface ToolCallArgsEvent {
49
+ type: 'tool_call_args';
50
+ data: {
51
+ tool_call_id: string;
52
+ delta: string;
53
+ };
54
+ }
55
+ interface ToolCallEndEvent {
56
+ type: 'tool_call_end';
57
+ data: {
58
+ tool_call_id: string;
59
+ };
60
+ }
61
+ interface ToolCallResultEvent {
62
+ type: 'tool_call_result';
63
+ data: {
64
+ tool_call_id: string;
65
+ result: string;
66
+ };
67
+ }
68
+ interface AgentHandoverEvent {
69
+ type: 'agent_handover';
70
+ data: {
71
+ from_agent: string;
72
+ to_agent: string;
73
+ reason?: string;
74
+ };
75
+ }
76
+ type DistriEvent = RunStartedEvent | RunFinishedEvent | RunErrorEvent | TextMessageStartEvent | TextMessageContentEvent | TextMessageEndEvent | ToolCallStartEvent | ToolCallArgsEvent | ToolCallEndEvent | ToolCallResultEvent | AgentHandoverEvent;
77
+
78
+ /**
79
+ * Message roles supported by Distri
80
+ */
81
+ type MessageRole = 'system' | 'assistant' | 'user' | 'tool';
82
+ /**
83
+ * Distri-specific message structure with parts
84
+ */
85
+ interface DistriMessage {
86
+ id: string;
87
+ role: MessageRole;
88
+ parts: DistriPart[];
89
+ created_at?: string;
90
+ }
91
+ type DistriStreamEvent = DistriMessage | DistriEvent;
92
+ /**
93
+ * Context required for constructing A2A messages from DistriMessage
94
+ */
95
+ interface InvokeContext {
96
+ thread_id: string;
97
+ run_id?: string;
98
+ metadata?: any;
99
+ }
100
+ /**
101
+ * Distri message parts - equivalent to Rust enum Part
102
+ */
103
+ type TextPart = {
104
+ type: 'text';
105
+ text: string;
106
+ };
107
+ type CodeObservationPart = {
108
+ type: 'code_observation';
109
+ thought: string;
110
+ code: string;
111
+ };
112
+ type ImageUrlPart = {
113
+ type: 'image_url';
114
+ image: FileUrl;
115
+ };
116
+ type ImageBytesPart = {
117
+ type: 'image_bytes';
118
+ image: FileBytes;
119
+ };
120
+ type ImagePart = ImageUrlPart | ImageBytesPart;
121
+ type DataPart = {
122
+ type: 'data';
123
+ data: any;
124
+ };
125
+ type ToolCallPart = {
126
+ type: 'tool_call';
127
+ tool_call: ToolCall;
128
+ };
129
+ type ToolResultPart = {
130
+ type: 'tool_result';
131
+ tool_result: ToolResult;
132
+ };
133
+ type PlanPart = {
134
+ type: 'plan';
135
+ plan: string;
136
+ };
137
+ type DistriPart = TextPart | CodeObservationPart | ImagePart | DataPart | ToolCallPart | ToolResultPart | PlanPart;
138
+ /**
139
+ * File type for images
140
+ */
141
+ interface FileBytes {
142
+ mime_type: string;
143
+ data: string;
144
+ name?: string;
145
+ }
146
+ interface FileUrl {
147
+ mime_type: string;
148
+ url: string;
149
+ name?: string;
150
+ }
151
+ type FileType = FileBytes | FileUrl;
152
+ /**
153
+ * Tool definition interface following AG-UI pattern
154
+ */
155
+ interface DistriBaseTool {
156
+ name: string;
157
+ type: 'function' | 'ui';
158
+ description: string;
159
+ input_schema: object;
160
+ }
161
+ interface DistriFnTool extends DistriBaseTool {
162
+ type: 'function';
163
+ handler: ToolHandler;
164
+ onToolComplete?: (toolCallId: string, toolResult: ToolResult) => void;
165
+ }
166
+ /**
167
+ * Tool handler function
168
+ */
169
+ interface ToolHandler {
170
+ (input: any): Promise<string | number | boolean | null | object>;
171
+ }
172
+ /**
173
+ * Tool call from agent
174
+ */
175
+ interface ToolCall {
176
+ tool_call_id: string;
177
+ tool_name: string;
178
+ input: any;
179
+ }
180
+ /**
181
+ * Tool result for responding to tool calls
182
+ */
183
+ interface ToolResult {
184
+ tool_call_id: string;
185
+ result: string | number | boolean | null;
186
+ success: boolean;
187
+ error?: string;
188
+ }
5
189
  /**
6
190
  * Distri-specific Agent type that wraps A2A AgentCard
7
191
  */
@@ -66,12 +250,6 @@ interface DistriThread {
66
250
  message_count: number;
67
251
  last_message?: string;
68
252
  }
69
- interface Agent {
70
- id: string;
71
- name: string;
72
- description: string;
73
- status: 'online' | 'offline';
74
- }
75
253
  interface Thread {
76
254
  id: string;
77
255
  title: string;
@@ -83,7 +261,7 @@ interface Thread {
83
261
  }
84
262
  interface ChatProps {
85
263
  thread: Thread;
86
- agent: Agent;
264
+ agent: DistriAgent;
87
265
  onThreadUpdate?: () => void;
88
266
  }
89
267
  /**
@@ -103,26 +281,9 @@ interface DistriClientConfig {
103
281
  headers?: Record<string, string>;
104
282
  interceptor?: (init?: RequestInit) => Promise<RequestInit | undefined>;
105
283
  }
106
- /**
107
- * Error Types
108
- */
109
- declare class DistriError extends Error {
110
- code: string;
111
- details?: any | undefined;
112
- constructor(message: string, code: string, details?: any | undefined);
113
- }
114
- declare class A2AProtocolError extends DistriError {
115
- constructor(message: string, details?: any);
116
- }
117
- declare class ApiError extends DistriError {
118
- statusCode: number;
119
- constructor(message: string, statusCode: number, details?: any);
120
- }
121
- declare class ConnectionError extends DistriError {
122
- constructor(message: string, details?: any);
123
- }
124
-
125
284
  type A2AStreamEventData = Message | TaskStatusUpdateEvent | TaskArtifactUpdateEvent | Task;
285
+ declare function isDistriMessage(event: DistriStreamEvent): event is DistriMessage;
286
+ declare function isDistriEvent(event: DistriStreamEvent): event is DistriEvent;
126
287
 
127
288
  /**
128
289
  * Enhanced Distri Client that wraps A2AClient and adds Distri-specific features
@@ -168,6 +329,14 @@ declare class DistriClient {
168
329
  * Get thread messages
169
330
  */
170
331
  getThreadMessages(threadId: string): Promise<Message[]>;
332
+ /**
333
+ * Get messages from a thread as DistriMessage format
334
+ */
335
+ getThreadMessagesAsDistri(threadId: string): Promise<DistriMessage[]>;
336
+ /**
337
+ * Send a DistriMessage to a thread
338
+ */
339
+ sendDistriMessage(threadId: string, message: DistriMessage, context: InvokeContext): Promise<void>;
171
340
  /**
172
341
  * Get the base URL for making direct requests
173
342
  */
@@ -191,84 +360,132 @@ declare class DistriClient {
191
360
  /**
192
361
  * Helper method to create A2A messages
193
362
  */
194
- static initMessage(input: string, role?: 'agent' | 'user', contextId?: string, messageId?: string, taskId?: string): Message;
363
+ static initMessage(parts: Part[] | string, role: "agent" | "user" | undefined, message: Omit<Partial<Message>, 'parts' | 'role' | 'kind'>): Message;
364
+ /**
365
+ * Create a DistriMessage instance
366
+ */
367
+ static initDistriMessage(role: DistriMessage['role'], parts: DistriPart[], id?: string, created_at?: string): DistriMessage;
195
368
  /**
196
369
  * Helper method to create message send parameters
197
370
  */
198
- static initMessageParams(message: Message, configuration?: MessageSendParams['configuration']): MessageSendParams;
371
+ static initMessageParams(message: Message, configuration?: MessageSendParams['configuration'], metadata?: any): MessageSendParams;
372
+ /**
373
+ * Create MessageSendParams from a DistriMessage using InvokeContext
374
+ */
375
+ static initDistriMessageParams(message: DistriMessage, context: InvokeContext): MessageSendParams;
199
376
  }
200
377
  declare function uuidv4(): string;
201
378
 
202
- type Role = 'user' | 'system' | 'assistant';
203
- interface RunStartedEvent {
204
- type: 'run_started';
205
- }
206
- interface RunFinishedEvent {
207
- type: 'run_finished';
208
- }
209
- interface RunErrorEvent {
210
- type: 'run_error';
211
- data: {
212
- message: string;
213
- code?: string;
214
- };
215
- }
216
- interface TextMessageStartEvent {
217
- type: 'text_message_start';
218
- data: {
219
- message_id: string;
220
- role: Role;
221
- };
222
- }
223
- interface TextMessageContentEvent {
224
- type: 'text_message_content';
225
- data: {
226
- message_id: string;
227
- delta: string;
228
- };
229
- }
230
- interface TextMessageEndEvent {
231
- type: 'text_message_end';
232
- data: {
233
- message_id: string;
234
- };
235
- }
236
- interface ToolCallStartEvent {
237
- type: 'tool_call_start';
238
- data: {
239
- tool_call_id: string;
240
- tool_call_name: string;
241
- parent_message_id?: string;
242
- };
243
- }
244
- interface ToolCallArgsEvent {
245
- type: 'tool_call_args';
246
- data: {
247
- tool_call_id: string;
248
- delta: string;
249
- };
250
- }
251
- interface ToolCallEndEvent {
252
- type: 'tool_call_end';
253
- data: {
254
- tool_call_id: string;
255
- };
379
+ /**
380
+ * Configuration for Agent invoke method
381
+ */
382
+ interface InvokeConfig {
383
+ /** Configuration for the message */
384
+ configuration?: MessageSendParams['configuration'];
385
+ /** Context/thread ID */
386
+ contextId?: string;
387
+ /** Metadata for the requests */
388
+ metadata?: any;
256
389
  }
257
- interface ToolCallResultEvent {
258
- type: 'tool_call_result';
259
- data: {
260
- tool_call_id: string;
261
- result: string;
262
- };
390
+ /**
391
+ * Result from agent invoke
392
+ */
393
+ interface InvokeResult {
394
+ /** Final response message */
395
+ message?: Message;
396
+ /** Task if created */
397
+ task?: any;
398
+ /** Whether the response was streamed */
399
+ streamed: boolean;
263
400
  }
264
- interface AgentHandoverEvent {
265
- type: 'agent_handover';
266
- data: {
267
- from_agent: string;
268
- to_agent: string;
269
- reason?: string;
270
- };
401
+ /**
402
+ * Enhanced Agent class with simple tool system following AG-UI pattern
403
+ */
404
+ declare class Agent {
405
+ private client;
406
+ private agentDefinition;
407
+ private tools;
408
+ constructor(agentDefinition: DistriAgent, client: DistriClient);
409
+ /**
410
+ * Add a tool to the agent (AG-UI style)
411
+ */
412
+ registerTool(tool: DistriBaseTool): void;
413
+ /**
414
+ * Add multiple tools at once
415
+ */
416
+ registerTools(tools: DistriBaseTool[]): void;
417
+ /**
418
+ * Remove a tool
419
+ */
420
+ unregisterTool(toolName: string): void;
421
+ /**
422
+ * Get all registered tools
423
+ */
424
+ getTools(): DistriBaseTool[];
425
+ /**
426
+ * Check if a tool is registered
427
+ */
428
+ hasTool(toolName: string): boolean;
429
+ /**
430
+ * Get agent information
431
+ */
432
+ get id(): string;
433
+ get name(): string;
434
+ get description(): string | undefined;
435
+ get iconUrl(): string | undefined;
436
+ /**
437
+ * Fetch messages for a thread (public method for useChat)
438
+ */
439
+ getThreadMessages(threadId: string): Promise<Message[]>;
440
+ /**
441
+ * Direct (non-streaming) invoke
442
+ */
443
+ invoke(params: MessageSendParams): Promise<Message>;
444
+ /**
445
+ * Streaming invoke
446
+ */
447
+ invokeStream(params: MessageSendParams): Promise<AsyncGenerator<DistriEvent | DistriMessage>>;
448
+ /**
449
+ * Enhance message params with tool definitions
450
+ */
451
+ private enhanceParamsWithTools;
452
+ /**
453
+ * Create an agent instance from an agent ID
454
+ */
455
+ static create(agentId: string, client: DistriClient): Promise<Agent>;
456
+ /**
457
+ * List all available agents
458
+ */
459
+ static list(client: DistriClient): Promise<Agent[]>;
271
460
  }
272
- type DistriEvent = RunStartedEvent | RunFinishedEvent | RunErrorEvent | TextMessageStartEvent | TextMessageContentEvent | TextMessageEndEvent | ToolCallStartEvent | ToolCallArgsEvent | ToolCallEndEvent | ToolCallResultEvent | AgentHandoverEvent;
273
461
 
274
- export { A2AProtocolError, type A2AStreamEventData, type Agent, type AgentHandoverEvent, ApiError, type ChatProps, ConnectionError, type ConnectionStatus, type DistriAgent, DistriClient, type DistriClientConfig, DistriError, type DistriEvent, type DistriThread, type McpDefinition, type McpServerType, type ModelProvider, type ModelSettings, type Role, type RunErrorEvent, type RunFinishedEvent, type RunStartedEvent, type TextMessageContentEvent, type TextMessageEndEvent, type TextMessageStartEvent, type Thread, type ToolCallArgsEvent, type ToolCallEndEvent, type ToolCallResultEvent, type ToolCallStartEvent, uuidv4 };
462
+ /**
463
+ * Converts an A2A Message to a DistriMessage
464
+ */
465
+ declare function convertA2AMessageToDistri(a2aMessage: Message): DistriMessage;
466
+ /**
467
+ * Converts an A2A Part to a DistriPart
468
+ */
469
+ declare function convertA2APartToDistri(a2aPart: Part): DistriPart;
470
+ /**
471
+ * Converts a DistriMessage to an A2A Message using the provided context
472
+ */
473
+ declare function convertDistriMessageToA2A(distriMessage: DistriMessage, context: InvokeContext): Message;
474
+ /**
475
+ * Converts a DistriPart to an A2A Part
476
+ */
477
+ declare function convertDistriPartToA2A(distriPart: DistriPart): Part;
478
+ /**
479
+ * Extract text content from DistriMessage
480
+ */
481
+ declare function extractTextFromDistriMessage(message: DistriMessage): string;
482
+ /**
483
+ * Extract tool calls from DistriMessage
484
+ */
485
+ declare function extractToolCallsFromDistriMessage(message: DistriMessage): any[];
486
+ /**
487
+ * Extract tool results from DistriMessage
488
+ */
489
+ declare function extractToolResultsFromDistriMessage(message: DistriMessage): any[];
490
+
491
+ export { Agent, type AgentHandoverEvent, type ChatProps, type CodeObservationPart, type ConnectionStatus, type DataPart, type DistriAgent, type DistriBaseTool, DistriClient, type DistriClientConfig, type DistriEvent, type DistriFnTool, type DistriMessage, type DistriPart, type DistriStreamEvent, type DistriThread, type FileType, type ImagePart, type InvokeConfig, type InvokeContext, type InvokeResult, type McpDefinition, type McpServerType, type MessageRole, type ModelProvider, type ModelSettings, type PlanPart, type RunErrorEvent, type RunFinishedEvent, type RunStartedEvent, type TextMessageContentEvent, type TextMessageEndEvent, type TextMessageStartEvent, type TextPart, type Thread, type ToolCall, type ToolCallArgsEvent, type ToolCallEndEvent, type ToolCallPart, type ToolCallResultEvent, type ToolCallStartEvent, type ToolResult, type ToolResultPart, convertA2AMessageToDistri, convertA2APartToDistri, convertDistriMessageToA2A, convertDistriPartToA2A, extractTextFromDistriMessage, extractToolCallsFromDistriMessage, extractToolResultsFromDistriMessage, isDistriEvent, isDistriMessage, uuidv4 };