|
| 1 | +# Artinet SDK |
| 2 | + |
| 3 | +A TypeScript client library for the [Agent2Agent (A2A) Protocol](https://github.com/google/A2A) - an open protocol for communication between AI agents. |
| 4 | + |
| 5 | +## Installation |
| 6 | + |
| 7 | +```bash |
| 8 | +npm install @artinet/sdk |
| 9 | +``` |
| 10 | + |
| 11 | +## Features |
| 12 | + |
| 13 | +- Full implementation of the A2A Protocol using the official Google schema |
| 14 | +- TypeScript types for all protocol structures |
| 15 | +- Streaming support for real-time updates |
| 16 | +- Push notification capabilities |
| 17 | +- Built-in error handling |
| 18 | +- Flexible authentication mechanisms |
| 19 | + |
| 20 | +## Usage |
| 21 | + |
| 22 | +### Basic Usage |
| 23 | + |
| 24 | +```typescript |
| 25 | +import { A2AClient, Message, Part, TaskSendParams } from "@artinet/sdk"; |
| 26 | + |
| 27 | +// Create a new client instance |
| 28 | +const client = new A2AClient("https://your-a2a-server.com"); |
| 29 | + |
| 30 | +// Get the agent card to discover capabilities |
| 31 | +const agentCard = await client.agentCard(); |
| 32 | +console.log(`Connected to agent: ${agentCard.name}`); |
| 33 | + |
| 34 | +// Check if the server supports streaming |
| 35 | +const supportsStreaming = await client.supports("streaming"); |
| 36 | + |
| 37 | +// Create a message to send |
| 38 | +const message: Message = { |
| 39 | + role: "user", |
| 40 | + parts: [ |
| 41 | + { |
| 42 | + type: "text", |
| 43 | + text: "Hello! Can you help me with a question?", |
| 44 | + }, |
| 45 | + ], |
| 46 | +}; |
| 47 | + |
| 48 | +// Send a task |
| 49 | +const task = await client.sendTask({ |
| 50 | + id: "task-123", |
| 51 | + message, |
| 52 | +}); |
| 53 | + |
| 54 | +console.log(`Task status: ${task?.status.state}`); |
| 55 | +``` |
| 56 | + |
| 57 | +### Streaming Updates |
| 58 | + |
| 59 | +```typescript |
| 60 | +import { A2AClient, Message } from "@artinet/sdk"; |
| 61 | + |
| 62 | +const client = new A2AClient("https://your-a2a-server.com"); |
| 63 | + |
| 64 | +// Check if the server supports streaming |
| 65 | +if (await client.supports("streaming")) { |
| 66 | + const message = { |
| 67 | + role: "user", |
| 68 | + parts: [{ type: "text", text: "Tell me a story about space exploration" }], |
| 69 | + }; |
| 70 | + |
| 71 | + // Send a task and subscribe to updates |
| 72 | + const stream = client.sendTaskSubscribe({ |
| 73 | + id: "streaming-task-123", |
| 74 | + message, |
| 75 | + }); |
| 76 | + |
| 77 | + // Process the updates as they arrive |
| 78 | + for await (const update of stream) { |
| 79 | + if ("status" in update) { |
| 80 | + console.log(`Task status: ${update.status.state}`); |
| 81 | + |
| 82 | + // Display agent's response when available |
| 83 | + if (update.status.message) { |
| 84 | + const textParts = update.status.message.parts |
| 85 | + .filter((part) => part.type === "text") |
| 86 | + .map((part) => (part as any).text); |
| 87 | + |
| 88 | + console.log("Agent response:", textParts.join("\n")); |
| 89 | + } |
| 90 | + } else if ("artifact" in update) { |
| 91 | + console.log("Received artifact:", update.artifact.name); |
| 92 | + } |
| 93 | + } |
| 94 | +} |
| 95 | +``` |
| 96 | + |
| 97 | +### Authentication |
| 98 | + |
| 99 | +```typescript |
| 100 | +import { A2AClient } from "@artinet/sdk"; |
| 101 | + |
| 102 | +// Add authentication headers |
| 103 | +const client = new A2AClient("https://your-a2a-server.com"); |
| 104 | +client.addHeader("Authorization", "Bearer your-token-here"); |
| 105 | + |
| 106 | +// Or set multiple headers at once |
| 107 | +client.setHeaders({ |
| 108 | + Authorization: "Bearer your-token-here", |
| 109 | + "X-Custom-Header": "custom-value", |
| 110 | +}); |
| 111 | +``` |
| 112 | + |
| 113 | +## Logging |
| 114 | + |
| 115 | +The SDK uses [pino](https://github.com/pinojs/pino), a fast and low overhead logging library: |
| 116 | + |
| 117 | +```typescript |
| 118 | +import { logger, configureLogger, LogLevel } from "@artinet/sdk"; |
| 119 | + |
| 120 | +// Configure the logger |
| 121 | +configureLogger({ |
| 122 | + level: "debug", // Options: 'silent', 'error', 'warn', 'info', 'debug', 'trace' |
| 123 | + name: "MyApplication" // Optional custom logger name |
| 124 | +}); |
| 125 | + |
| 126 | +// Use the logger directly |
| 127 | +logger.info("Connection established"); |
| 128 | +logger.error({ err: new Error("Failed to connect") }, "Connection error"); |
| 129 | +logger.debug({ data: { id: 123, status: "active" } }, "Task details"); |
| 130 | + |
| 131 | +// In production, set a more restrictive level |
| 132 | +configureLogger({ level: "error" }); |
| 133 | +// Or disable completely |
| 134 | +configureLogger({ level: "silent" }); |
| 135 | +``` |
| 136 | + |
| 137 | +The default log level is 'error', which means only error messages and above are shown. |
| 138 | + |
| 139 | +For more advanced configurations, you can access the pino logger instance directly: |
| 140 | + |
| 141 | +```typescript |
| 142 | +import { logger } from "@artinet/sdk"; |
| 143 | +import { pino } from "pino"; |
| 144 | + |
| 145 | +// Create a custom child logger |
| 146 | +const customLogger = logger.child({ module: "TaskProcessor" }); |
| 147 | +customLogger.info("Processing started"); |
| 148 | +``` |
| 149 | + |
| 150 | +## API Reference |
| 151 | + |
| 152 | +### Classes |
| 153 | + |
| 154 | +- `A2AClient` - Main client for interacting with A2A servers |
| 155 | +- `RpcError` - Error class for handling A2A protocol errors |
| 156 | + |
| 157 | +### Utilities |
| 158 | + |
| 159 | +- `logger` - Pino logger instance for SDK operations |
| 160 | +- `configureLogger()` - Utility function to configure the logger |
| 161 | + |
| 162 | +## Contributing |
| 163 | + |
| 164 | +Contributions are welcome! Please feel free to submit a Pull Request. |
| 165 | + |
| 166 | +## License |
| 167 | + |
| 168 | +This project is licensed under the MIT License - see the LICENSE file for details. |
| 169 | + |
| 170 | +## Acknowledgements |
| 171 | + |
| 172 | +This SDK directly incorporates the [official A2A Protocol schema](https://github.com/google/A2A/blob/main/samples/js/src/schema.ts) created by Google, ensuring complete compatibility with the specification. |
0 commit comments