@kadi.build/core 0.0.1-alpha.1 → 0.0.1-alpha.11
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 +361 -230
- package/dist/abilities/AbilityCache.d.ts +242 -0
- package/dist/abilities/AbilityCache.d.ts.map +1 -0
- package/dist/abilities/AbilityCache.js +285 -0
- package/dist/abilities/AbilityCache.js.map +1 -0
- package/dist/abilities/AbilityContext.d.ts +215 -0
- package/dist/abilities/AbilityContext.d.ts.map +1 -0
- package/dist/abilities/AbilityContext.js +36 -0
- package/dist/abilities/AbilityContext.js.map +1 -0
- package/dist/abilities/AbilityLoader.d.ts +203 -0
- package/dist/abilities/AbilityLoader.d.ts.map +1 -0
- package/dist/abilities/AbilityLoader.js +343 -0
- package/dist/abilities/AbilityLoader.js.map +1 -0
- package/dist/abilities/AbilityProxy.d.ts +496 -0
- package/dist/abilities/AbilityProxy.d.ts.map +1 -0
- package/dist/abilities/AbilityProxy.js +551 -0
- package/dist/abilities/AbilityProxy.js.map +1 -0
- package/dist/abilities/AbilityValidator.d.ts +172 -0
- package/dist/abilities/AbilityValidator.d.ts.map +1 -0
- package/dist/abilities/AbilityValidator.js +253 -0
- package/dist/abilities/AbilityValidator.js.map +1 -0
- package/dist/abilities/index.d.ts +26 -0
- package/dist/abilities/index.d.ts.map +1 -0
- package/dist/abilities/index.js +23 -0
- package/dist/abilities/index.js.map +1 -0
- package/dist/abilities/types.d.ts +223 -0
- package/dist/abilities/types.d.ts.map +1 -0
- package/dist/abilities/types.js +10 -0
- package/dist/abilities/types.js.map +1 -0
- package/dist/api/index.d.ts +92 -0
- package/dist/api/index.d.ts.map +1 -0
- package/dist/api/index.js +124 -0
- package/dist/api/index.js.map +1 -0
- package/dist/broker/BrokerConnection.d.ts +253 -0
- package/dist/broker/BrokerConnection.d.ts.map +1 -0
- package/dist/broker/BrokerConnection.js +434 -0
- package/dist/broker/BrokerConnection.js.map +1 -0
- package/dist/broker/BrokerConnectionManager.d.ts +216 -0
- package/dist/broker/BrokerConnectionManager.d.ts.map +1 -0
- package/dist/broker/BrokerConnectionManager.js +305 -0
- package/dist/broker/BrokerConnectionManager.js.map +1 -0
- package/dist/broker/BrokerProtocol.d.ts +280 -0
- package/dist/broker/BrokerProtocol.d.ts.map +1 -0
- package/dist/broker/BrokerProtocol.js +466 -0
- package/dist/broker/BrokerProtocol.js.map +1 -0
- package/dist/broker/index.d.ts +9 -0
- package/dist/broker/index.d.ts.map +1 -0
- package/dist/broker/index.js +9 -0
- package/dist/broker/index.js.map +1 -0
- package/dist/client/KadiClient.d.ts +459 -0
- package/dist/client/KadiClient.d.ts.map +1 -0
- package/dist/client/KadiClient.js +902 -0
- package/dist/client/KadiClient.js.map +1 -0
- package/dist/client/index.d.ts +7 -0
- package/dist/client/index.d.ts.map +1 -0
- package/dist/client/index.js +7 -0
- package/dist/client/index.js.map +1 -0
- package/dist/config/ConfigLoader.d.ts +138 -0
- package/dist/config/ConfigLoader.d.ts.map +1 -0
- package/dist/config/ConfigLoader.js +226 -0
- package/dist/config/ConfigLoader.js.map +1 -0
- package/dist/config/ConfigResolver.d.ts +135 -0
- package/dist/config/ConfigResolver.d.ts.map +1 -0
- package/dist/config/ConfigResolver.js +282 -0
- package/dist/config/ConfigResolver.js.map +1 -0
- package/dist/config/index.d.ts +8 -0
- package/dist/config/index.d.ts.map +1 -0
- package/dist/config/index.js +8 -0
- package/dist/config/index.js.map +1 -0
- package/dist/errors/index.d.ts +9 -0
- package/dist/errors/index.d.ts.map +1 -0
- package/dist/errors/index.js +8 -0
- package/dist/errors/index.js.map +1 -0
- package/dist/events/EventHub.d.ts +172 -0
- package/dist/events/EventHub.d.ts.map +1 -0
- package/dist/events/EventHub.js +333 -0
- package/dist/events/EventHub.js.map +1 -0
- package/dist/events/index.d.ts +7 -0
- package/dist/events/index.d.ts.map +1 -0
- package/dist/events/index.js +7 -0
- package/dist/events/index.js.map +1 -0
- package/dist/index.d.ts +50 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +67 -0
- package/dist/index.js.map +1 -0
- package/dist/messages/index.d.ts +33 -0
- package/dist/messages/index.d.ts.map +1 -0
- package/dist/messages/index.js +33 -0
- package/dist/messages/index.js.map +1 -0
- package/dist/schemas/index.d.ts +22 -0
- package/dist/schemas/index.d.ts.map +1 -0
- package/dist/schemas/index.js +27 -0
- package/dist/schemas/index.js.map +1 -0
- package/dist/schemas/kadi-extensions.d.ts +231 -0
- package/dist/schemas/kadi-extensions.d.ts.map +1 -0
- package/dist/schemas/kadi-extensions.js +14 -0
- package/dist/schemas/kadi-extensions.js.map +1 -0
- package/dist/schemas/mcp/schema.d.ts +1399 -0
- package/dist/schemas/mcp/schema.d.ts.map +1 -0
- package/dist/schemas/mcp/schema.js +53 -0
- package/dist/schemas/mcp/schema.js.map +1 -0
- package/dist/schemas/mcp/version.d.ts +37 -0
- package/dist/schemas/mcp/version.d.ts.map +1 -0
- package/dist/schemas/mcp/version.js +39 -0
- package/dist/schemas/mcp/version.js.map +1 -0
- package/dist/schemas/schema-builders.d.ts +178 -0
- package/dist/schemas/schema-builders.d.ts.map +1 -0
- package/dist/schemas/schema-builders.js +258 -0
- package/dist/schemas/schema-builders.js.map +1 -0
- package/dist/schemas/zod-helpers.d.ts +129 -0
- package/dist/schemas/zod-helpers.d.ts.map +1 -0
- package/dist/schemas/zod-helpers.js +225 -0
- package/dist/schemas/zod-helpers.js.map +1 -0
- package/dist/schemas/zod-to-json-schema.d.ts +159 -0
- package/dist/schemas/zod-to-json-schema.d.ts.map +1 -0
- package/dist/schemas/zod-to-json-schema.js +154 -0
- package/dist/schemas/zod-to-json-schema.js.map +1 -0
- package/dist/tools/ToolRegistry.d.ts +256 -0
- package/dist/tools/ToolRegistry.d.ts.map +1 -0
- package/dist/tools/ToolRegistry.js +340 -0
- package/dist/tools/ToolRegistry.js.map +1 -0
- package/dist/tools/index.d.ts +7 -0
- package/dist/tools/index.d.ts.map +1 -0
- package/dist/tools/index.js +7 -0
- package/dist/tools/index.js.map +1 -0
- package/dist/transports/BrokerTransport.d.ts +151 -0
- package/dist/transports/BrokerTransport.d.ts.map +1 -0
- package/dist/transports/BrokerTransport.js +261 -0
- package/dist/transports/BrokerTransport.js.map +1 -0
- package/dist/transports/NativeTransport.d.ts +178 -0
- package/dist/transports/NativeTransport.d.ts.map +1 -0
- package/dist/transports/NativeTransport.js +397 -0
- package/dist/transports/NativeTransport.js.map +1 -0
- package/dist/transports/StdioTransport.d.ts +250 -0
- package/dist/transports/StdioTransport.d.ts.map +1 -0
- package/dist/transports/StdioTransport.js +487 -0
- package/dist/transports/StdioTransport.js.map +1 -0
- package/dist/transports/index.d.ts +10 -0
- package/dist/transports/index.d.ts.map +1 -0
- package/dist/transports/index.js +9 -0
- package/dist/transports/index.js.map +1 -0
- package/dist/types/broker.d.ts +279 -0
- package/dist/types/broker.d.ts.map +1 -0
- package/dist/types/broker.js +19 -0
- package/dist/types/broker.js.map +1 -0
- package/dist/types/config.d.ts +325 -0
- package/dist/types/config.d.ts.map +1 -0
- package/dist/types/config.js +17 -0
- package/dist/types/config.js.map +1 -0
- package/dist/types/errors.d.ts +178 -0
- package/dist/types/errors.d.ts.map +1 -0
- package/dist/types/errors.js +165 -0
- package/dist/types/errors.js.map +1 -0
- package/dist/types/events.d.ts +210 -0
- package/dist/types/events.d.ts.map +1 -0
- package/dist/types/events.js +8 -0
- package/dist/types/events.js.map +1 -0
- package/dist/types/index.d.ts +34 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/index.js +21 -0
- package/dist/types/index.js.map +1 -0
- package/dist/types/protocol.d.ts +48 -0
- package/dist/types/protocol.d.ts.map +1 -0
- package/dist/types/protocol.js +11 -0
- package/dist/types/protocol.js.map +1 -0
- package/dist/types/tools.d.ts +67 -0
- package/dist/types/tools.d.ts.map +1 -0
- package/dist/types/tools.js +16 -0
- package/dist/types/tools.js.map +1 -0
- package/dist/types/transport.d.ts +250 -0
- package/dist/types/transport.d.ts.map +1 -0
- package/dist/types/transport.js +18 -0
- package/dist/types/transport.js.map +1 -0
- package/dist/types/zod-tools.d.ts +198 -0
- package/dist/types/zod-tools.d.ts.map +1 -0
- package/dist/types/zod-tools.js +14 -0
- package/dist/types/zod-tools.js.map +1 -0
- package/dist/utils/StdioMessageReader.d.ts +122 -0
- package/dist/utils/StdioMessageReader.d.ts.map +1 -0
- package/dist/utils/StdioMessageReader.js +209 -0
- package/dist/utils/StdioMessageReader.js.map +1 -0
- package/dist/utils/StdioMessageWriter.d.ts +104 -0
- package/dist/utils/StdioMessageWriter.d.ts.map +1 -0
- package/dist/utils/StdioMessageWriter.js +162 -0
- package/dist/utils/StdioMessageWriter.js.map +1 -0
- package/dist/validation/SchemaValidator.d.ts +208 -0
- package/dist/validation/SchemaValidator.d.ts.map +1 -0
- package/dist/validation/SchemaValidator.js +411 -0
- package/dist/validation/SchemaValidator.js.map +1 -0
- package/dist/validation/index.d.ts +11 -0
- package/dist/validation/index.d.ts.map +1 -0
- package/dist/validation/index.js +10 -0
- package/dist/validation/index.js.map +1 -0
- package/package.json +70 -5
- package/agent.json +0 -18
- package/broker.js +0 -214
- package/index.js +0 -370
- package/ipc.js +0 -220
- package/ipcInterfaces/pythonAbilityIPC.py +0 -177
|
@@ -0,0 +1,459 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* KADI Client
|
|
3
|
+
*
|
|
4
|
+
* Thin orchestrator that composes specialized modules to provide the
|
|
5
|
+
* complete KADI client functionality.
|
|
6
|
+
*
|
|
7
|
+
* This is dramatically simpler than the old implementation because each
|
|
8
|
+
* concern has been properly separated.
|
|
9
|
+
*
|
|
10
|
+
* @module client/KadiClient
|
|
11
|
+
*/
|
|
12
|
+
import { EventEmitter } from 'events';
|
|
13
|
+
import type { KadiConfig, ResolvedConfig, ToolHandler, KadiTool, EventCallback, UnsubscribeFunction, PublishOptions } from '../types/index.js';
|
|
14
|
+
import { BrokerConnectionManager } from '../broker/BrokerConnectionManager.js';
|
|
15
|
+
import { BrokerProtocol } from '../broker/BrokerProtocol.js';
|
|
16
|
+
import { type LoadedAbility, type LoadOptions, type AgentJson } from '../abilities/index.js';
|
|
17
|
+
import type { IBrokerClient } from '../transports/BrokerTransport.js';
|
|
18
|
+
/**
|
|
19
|
+
* KADI Client
|
|
20
|
+
*
|
|
21
|
+
* Main client class for KADI framework.
|
|
22
|
+
* Orchestrates tool registry, event hub, broker connections, and ability loading.
|
|
23
|
+
*
|
|
24
|
+
* @example
|
|
25
|
+
* ```typescript
|
|
26
|
+
* // Create client
|
|
27
|
+
* const client = new KadiClient({
|
|
28
|
+
* name: 'my-service',
|
|
29
|
+
* brokers: { local: 'ws://localhost:8080' },
|
|
30
|
+
* networks: ['global']
|
|
31
|
+
* });
|
|
32
|
+
*
|
|
33
|
+
* // Register a tool
|
|
34
|
+
* client.registerTool('greet', async ({ name }) => {
|
|
35
|
+
* return { message: `Hello, ${name}!` };
|
|
36
|
+
* });
|
|
37
|
+
*
|
|
38
|
+
* // Connect to brokers
|
|
39
|
+
* await client.connect();
|
|
40
|
+
*
|
|
41
|
+
* // Load an ability (explicit transport)
|
|
42
|
+
* const calc = await client.load('calculator', 'broker');
|
|
43
|
+
* const result = await calc.add({ a: 5, b: 3 });
|
|
44
|
+
*
|
|
45
|
+
* // Subscribe to events
|
|
46
|
+
* client.subscribeToEvent('user.*', (data) => {
|
|
47
|
+
* console.log('User event:', data);
|
|
48
|
+
* });
|
|
49
|
+
*
|
|
50
|
+
* // Publish an event
|
|
51
|
+
* client.publishEvent('user.login', { userId: '123' });
|
|
52
|
+
* ```
|
|
53
|
+
*/
|
|
54
|
+
export declare class KadiClient extends EventEmitter implements IBrokerClient {
|
|
55
|
+
/**
|
|
56
|
+
* Resolved configuration
|
|
57
|
+
*/
|
|
58
|
+
readonly config: ResolvedConfig;
|
|
59
|
+
/**
|
|
60
|
+
* Tool registry
|
|
61
|
+
*/
|
|
62
|
+
private readonly tools;
|
|
63
|
+
/**
|
|
64
|
+
* Event hub
|
|
65
|
+
*/
|
|
66
|
+
private readonly events;
|
|
67
|
+
/**
|
|
68
|
+
* Broker connection manager
|
|
69
|
+
*/
|
|
70
|
+
private readonly brokers;
|
|
71
|
+
/**
|
|
72
|
+
* Broker protocol instances (per broker)
|
|
73
|
+
*/
|
|
74
|
+
private readonly protocols;
|
|
75
|
+
/**
|
|
76
|
+
* Ability loader
|
|
77
|
+
*/
|
|
78
|
+
private readonly abilityLoader;
|
|
79
|
+
/**
|
|
80
|
+
* Whether client is initialized
|
|
81
|
+
*/
|
|
82
|
+
private initialized;
|
|
83
|
+
/**
|
|
84
|
+
* Create a new KADI Client
|
|
85
|
+
*
|
|
86
|
+
* @param config - Client configuration
|
|
87
|
+
*/
|
|
88
|
+
constructor(config: KadiConfig);
|
|
89
|
+
/**
|
|
90
|
+
* Connect to configured brokers
|
|
91
|
+
*
|
|
92
|
+
* @throws {KadiError} If connection fails
|
|
93
|
+
*
|
|
94
|
+
* @example
|
|
95
|
+
* ```typescript
|
|
96
|
+
* await client.connect();
|
|
97
|
+
* console.log('Connected to brokers');
|
|
98
|
+
* ```
|
|
99
|
+
*/
|
|
100
|
+
connect(): Promise<void>;
|
|
101
|
+
/**
|
|
102
|
+
* Connect to all configured brokers
|
|
103
|
+
*/
|
|
104
|
+
private connectToBrokers;
|
|
105
|
+
/**
|
|
106
|
+
* Register capabilities with a broker
|
|
107
|
+
*
|
|
108
|
+
* @param protocol - Broker protocol instance
|
|
109
|
+
*/
|
|
110
|
+
private registerCapabilitiesWithBroker;
|
|
111
|
+
/**
|
|
112
|
+
* Register a tool (MCP-based)
|
|
113
|
+
*
|
|
114
|
+
* @template TInput - Tool input type
|
|
115
|
+
* @template TOutput - Tool output type
|
|
116
|
+
* @param definition - MCP-compliant tool definition
|
|
117
|
+
* @param handler - Tool handler function
|
|
118
|
+
* @returns this for chaining
|
|
119
|
+
*
|
|
120
|
+
* @example
|
|
121
|
+
* ```typescript
|
|
122
|
+
* client.registerTool({
|
|
123
|
+
* name: 'add',
|
|
124
|
+
* description: 'Add two numbers',
|
|
125
|
+
* version: '1.0.0',
|
|
126
|
+
* tags: ['math'],
|
|
127
|
+
* inputSchema: {
|
|
128
|
+
* type: 'object',
|
|
129
|
+
* properties: {
|
|
130
|
+
* a: { type: 'number' },
|
|
131
|
+
* b: { type: 'number' }
|
|
132
|
+
* },
|
|
133
|
+
* required: ['a', 'b']
|
|
134
|
+
* },
|
|
135
|
+
* outputSchema: {
|
|
136
|
+
* type: 'object',
|
|
137
|
+
* properties: {
|
|
138
|
+
* result: { type: 'number' }
|
|
139
|
+
* }
|
|
140
|
+
* }
|
|
141
|
+
* }, async ({ a, b }) => {
|
|
142
|
+
* return { result: a + b };
|
|
143
|
+
* });
|
|
144
|
+
* ```
|
|
145
|
+
*/
|
|
146
|
+
registerTool<TInput = unknown, TOutput = unknown>(definition: KadiTool | (Omit<KadiTool, 'inputSchema' | 'outputSchema'> & {
|
|
147
|
+
input?: any;
|
|
148
|
+
output?: any;
|
|
149
|
+
}), handler: ToolHandler<TInput, TOutput>): this;
|
|
150
|
+
/**
|
|
151
|
+
* Get a registered tool by name
|
|
152
|
+
*
|
|
153
|
+
* Retrieves a tool that has been registered with this client.
|
|
154
|
+
* Useful for inspecting registered tools or accessing their metadata.
|
|
155
|
+
*
|
|
156
|
+
* @param name - Tool name to retrieve
|
|
157
|
+
* @returns The registered tool with its definition and handler, or undefined if not found
|
|
158
|
+
*
|
|
159
|
+
* @example
|
|
160
|
+
* ```typescript
|
|
161
|
+
* const tool = client.getRegisteredTool('add');
|
|
162
|
+
* if (tool) {
|
|
163
|
+
* console.log('Tool:', tool.definition.name);
|
|
164
|
+
* console.log('Description:', tool.definition.description);
|
|
165
|
+
* }
|
|
166
|
+
* ```
|
|
167
|
+
*/
|
|
168
|
+
getRegisteredTool(name: string): import("../schemas/kadi-extensions.js").RegisteredTool<unknown, unknown> | undefined;
|
|
169
|
+
/**
|
|
170
|
+
* Get all registered tools
|
|
171
|
+
*
|
|
172
|
+
* Returns an array of all tools that have been registered with this client.
|
|
173
|
+
* Useful for listing available tools or debugging.
|
|
174
|
+
*
|
|
175
|
+
* @returns Array of all registered tools
|
|
176
|
+
*
|
|
177
|
+
* @example
|
|
178
|
+
* ```typescript
|
|
179
|
+
* const allTools = client.getAllRegisteredTools();
|
|
180
|
+
* console.log('Available tools:', allTools.map(t => t.definition.name));
|
|
181
|
+
* ```
|
|
182
|
+
*/
|
|
183
|
+
getAllRegisteredTools(): import("../schemas/kadi-extensions.js").RegisteredTool<unknown, unknown>[];
|
|
184
|
+
/**
|
|
185
|
+
* Check if a tool is registered
|
|
186
|
+
*
|
|
187
|
+
* @param name - Tool name to check
|
|
188
|
+
* @returns true if the tool is registered, false otherwise
|
|
189
|
+
*
|
|
190
|
+
* @example
|
|
191
|
+
* ```typescript
|
|
192
|
+
* if (client.hasRegisteredTool('add')) {
|
|
193
|
+
* // Tool is available
|
|
194
|
+
* }
|
|
195
|
+
* ```
|
|
196
|
+
*/
|
|
197
|
+
hasRegisteredTool(name: string): boolean;
|
|
198
|
+
/**
|
|
199
|
+
* Load an ability
|
|
200
|
+
*
|
|
201
|
+
* **Explicit Transport, Excellent Developer Experience:**
|
|
202
|
+
* - Explicit transport selection (no magic)
|
|
203
|
+
* - Transport-specific options (only relevant fields shown)
|
|
204
|
+
* - Smart caching (avoids duplicate loads)
|
|
205
|
+
* - Runtime validation (optional)
|
|
206
|
+
* - Helpful errors with suggestions
|
|
207
|
+
*
|
|
208
|
+
* @param name - Ability name
|
|
209
|
+
* @param transport - Transport type ('native', 'stdio', or 'broker')
|
|
210
|
+
* @param options - Load options (transport-specific, optional)
|
|
211
|
+
* @returns Proxied ability with dynamic method access
|
|
212
|
+
*
|
|
213
|
+
* @throws {KadiError} If loading fails
|
|
214
|
+
*
|
|
215
|
+
* @example
|
|
216
|
+
* ```typescript
|
|
217
|
+
* // Simple broker load
|
|
218
|
+
* const calc = await client.load('calculator', 'broker');
|
|
219
|
+
* await calc.add({ a: 5, b: 3 });
|
|
220
|
+
*
|
|
221
|
+
* // With options
|
|
222
|
+
* const remote = await client.load('gpu-service', 'broker', {
|
|
223
|
+
* networks: ['global', 'gpu-cluster']
|
|
224
|
+
* });
|
|
225
|
+
*
|
|
226
|
+
* // With runtime validation
|
|
227
|
+
* const validated = await client.load('calculator', 'broker', {
|
|
228
|
+
* validate: true // Runtime check for expected methods
|
|
229
|
+
* });
|
|
230
|
+
*
|
|
231
|
+
* // Native transport
|
|
232
|
+
* const local = await client.load('math-lib', 'native', {
|
|
233
|
+
* path: './abilities/math'
|
|
234
|
+
* });
|
|
235
|
+
* ```
|
|
236
|
+
*/
|
|
237
|
+
load(name: string, transport: 'native' | 'stdio' | 'broker', options?: LoadOptions): Promise<LoadedAbility>;
|
|
238
|
+
/**
|
|
239
|
+
* Subscribe to events
|
|
240
|
+
*
|
|
241
|
+
* @template T - Event data type
|
|
242
|
+
* @param pattern - Event pattern (supports wildcards)
|
|
243
|
+
* @param callback - Event callback
|
|
244
|
+
* @returns Unsubscribe function
|
|
245
|
+
*
|
|
246
|
+
* @example
|
|
247
|
+
* ```typescript
|
|
248
|
+
* const unsubscribe = client.subscribeToEvent('user.*', (data) => {
|
|
249
|
+
* console.log('User event:', data);
|
|
250
|
+
* });
|
|
251
|
+
*
|
|
252
|
+
* // Later...
|
|
253
|
+
* unsubscribe();
|
|
254
|
+
* ```
|
|
255
|
+
*/
|
|
256
|
+
subscribeToEvent<T = unknown>(pattern: string, callback: EventCallback<T>): UnsubscribeFunction;
|
|
257
|
+
/**
|
|
258
|
+
* Publish an event
|
|
259
|
+
*
|
|
260
|
+
* @template T - Event data type
|
|
261
|
+
* @param eventName - Event name
|
|
262
|
+
* @param data - Event data
|
|
263
|
+
* @param options - Publishing options
|
|
264
|
+
*
|
|
265
|
+
* @example
|
|
266
|
+
* ```typescript
|
|
267
|
+
* client.publishEvent('user.login', {
|
|
268
|
+
* userId: '123',
|
|
269
|
+
* timestamp: Date.now()
|
|
270
|
+
* });
|
|
271
|
+
* ```
|
|
272
|
+
*/
|
|
273
|
+
publishEvent<T = unknown>(eventName: string, data: T, options?: PublishOptions): void;
|
|
274
|
+
/**
|
|
275
|
+
* Disconnect from all brokers and cleanup
|
|
276
|
+
*
|
|
277
|
+
* @example
|
|
278
|
+
* ```typescript
|
|
279
|
+
* await client.disconnect();
|
|
280
|
+
* ```
|
|
281
|
+
*/
|
|
282
|
+
disconnect(): Promise<void>;
|
|
283
|
+
/**
|
|
284
|
+
* Stdio server instance (when running in stdio mode)
|
|
285
|
+
*/
|
|
286
|
+
private stdioServer?;
|
|
287
|
+
/**
|
|
288
|
+
* Start serving this ability in the specified mode
|
|
289
|
+
*
|
|
290
|
+
* **Universal Entry Point**:
|
|
291
|
+
* Starts the appropriate server based on the mode parameter.
|
|
292
|
+
* This enables the same ability code to work across all transports
|
|
293
|
+
*
|
|
294
|
+
* **Modes**:
|
|
295
|
+
* - **native**: No-op (ability already loaded in-process as ES module)
|
|
296
|
+
* - **stdio**: Starts JSON-RPC server over stdin/stdout (for child processes)
|
|
297
|
+
* - **broker**: Connects to WebSocket broker and serves remotely
|
|
298
|
+
*
|
|
299
|
+
* @param mode - Transport mode to serve in ('native' | 'stdio' | 'broker')
|
|
300
|
+
* @returns Promise that resolves when server is ready (never resolves for stdio/broker - they keep running)
|
|
301
|
+
*
|
|
302
|
+
* @example
|
|
303
|
+
* ```typescript
|
|
304
|
+
* // ability.ts
|
|
305
|
+
* const client = new KadiClient({
|
|
306
|
+
* name: 'calculator',
|
|
307
|
+
* version: '1.0.0',
|
|
308
|
+
* brokers: { default: 'ws://localhost:8080' } // For broker mode
|
|
309
|
+
* });
|
|
310
|
+
*
|
|
311
|
+
* client.registerTool({ name: 'add', ... }, async ({ a, b }) => {
|
|
312
|
+
* return { result: a + b };
|
|
313
|
+
* });
|
|
314
|
+
*
|
|
315
|
+
* export default client;
|
|
316
|
+
*
|
|
317
|
+
* // If running standalone, start server
|
|
318
|
+
* if (import.meta.url === `file://${process.argv[1]}`) {
|
|
319
|
+
* await client.serve('stdio'); // or 'broker' for distributed mode
|
|
320
|
+
* }
|
|
321
|
+
* ```
|
|
322
|
+
*/
|
|
323
|
+
serve(mode: 'native' | 'stdio' | 'broker'): Promise<void>;
|
|
324
|
+
/**
|
|
325
|
+
* Serve via stdio (JSON-RPC over stdin/stdout)
|
|
326
|
+
*
|
|
327
|
+
* **Steps**:
|
|
328
|
+
* 1. Redirect console.log to stderr (keeps stdout clean for JSON-RPC)
|
|
329
|
+
* 2. Create Content-Length message reader/writer for stdin/stdout
|
|
330
|
+
* 3. Listen for incoming JSON-RPC requests (invoke, readAgentJson, shutdown)
|
|
331
|
+
* 4. Execute tool handlers for 'invoke' requests
|
|
332
|
+
* 5. Send JSON-RPC responses back via stdout
|
|
333
|
+
* 6. Handle graceful shutdown on SIGTERM/SIGINT
|
|
334
|
+
* 7. Keep process alive until shutdown
|
|
335
|
+
*
|
|
336
|
+
* @returns Promise that never resolves (server runs until killed)
|
|
337
|
+
*/
|
|
338
|
+
private serveStdio;
|
|
339
|
+
/**
|
|
340
|
+
* Serve via broker (WebSocket-based distributed mode)
|
|
341
|
+
*
|
|
342
|
+
* Connects to the broker, registers this ability's tools, and handles
|
|
343
|
+
* incoming tool invocation requests.
|
|
344
|
+
*
|
|
345
|
+
* **Steps**:
|
|
346
|
+
* 1. Get broker URL from constructor config
|
|
347
|
+
* 2. Create and connect to broker via BrokerConnection
|
|
348
|
+
* 3. Perform handshake and authentication
|
|
349
|
+
* 4. Register all tools from the registry
|
|
350
|
+
* 5. Listen for incoming 'kadi.ability.request' messages
|
|
351
|
+
* 6. Execute tool handlers and send responses
|
|
352
|
+
* 7. Handle graceful shutdown on SIGTERM/SIGINT
|
|
353
|
+
* 8. Keep process alive until shutdown
|
|
354
|
+
*
|
|
355
|
+
* @returns Promise that never resolves (server runs until killed)
|
|
356
|
+
* @throws {KadiError} If broker config missing or connection fails
|
|
357
|
+
* @private
|
|
358
|
+
*/
|
|
359
|
+
private serveBroker;
|
|
360
|
+
/**
|
|
361
|
+
* Read agent.json representation
|
|
362
|
+
*
|
|
363
|
+
* Returns the runtime representation of this client's agent.json configuration.
|
|
364
|
+
* This enables KadiClient to function as an ability module that can be loaded
|
|
365
|
+
* natively by other agents.
|
|
366
|
+
*
|
|
367
|
+
* **What this returns:**
|
|
368
|
+
* - Represents what would be in the ability's agent.json file
|
|
369
|
+
* - Runtime `tools` field is mapped from agent.json's `exports` field
|
|
370
|
+
* - Used for discovery, type generation, and validation
|
|
371
|
+
*
|
|
372
|
+
* **Registration Pattern (Option A):**
|
|
373
|
+
* Ability developers write their code once using KadiClient.registerTool(),
|
|
374
|
+
* and it works across all transports (native, stdio, broker).
|
|
375
|
+
*
|
|
376
|
+
* @returns agent.json representation with name, version, and tools
|
|
377
|
+
*
|
|
378
|
+
* @example
|
|
379
|
+
* ```typescript
|
|
380
|
+
* // ability.ts (alongside agent.json file)
|
|
381
|
+
* const client = new KadiClient({ name: 'calculator', version: '1.0.0' });
|
|
382
|
+
* client.registerTool({ name: 'add', ... }, handler);
|
|
383
|
+
* export default client;
|
|
384
|
+
*
|
|
385
|
+
* // When loaded natively:
|
|
386
|
+
* const agentJson = client.readAgentJson();
|
|
387
|
+
* // { name: 'calculator', version: '1.0.0', tools: [...] }
|
|
388
|
+
* // ↑ Represents agent.json's runtime structure
|
|
389
|
+
* ```
|
|
390
|
+
*/
|
|
391
|
+
readAgentJson(): AgentJson;
|
|
392
|
+
/**
|
|
393
|
+
* Invoke a tool by name
|
|
394
|
+
*
|
|
395
|
+
* Enables KadiClient to be used as an ability module. When loaded natively,
|
|
396
|
+
* this method is called by NativeTransport to execute registered tools.
|
|
397
|
+
*
|
|
398
|
+
* **Write Once, Run Anywhere:**
|
|
399
|
+
* The same tool handler works whether the ability is loaded natively,
|
|
400
|
+
* via stdio, or through a broker.
|
|
401
|
+
*
|
|
402
|
+
* @template TInput - Tool input type
|
|
403
|
+
* @template TOutput - Tool output type
|
|
404
|
+
* @param toolName - Name of the tool to invoke
|
|
405
|
+
* @param params - Tool input parameters
|
|
406
|
+
* @returns Promise resolving to tool result
|
|
407
|
+
*
|
|
408
|
+
* @throws {KadiError} If tool not found or invocation fails
|
|
409
|
+
*
|
|
410
|
+
* @example
|
|
411
|
+
* ```typescript
|
|
412
|
+
* // Registered tool:
|
|
413
|
+
* client.registerTool({ name: 'add', ... }, ({ a, b }) => ({ result: a + b }));
|
|
414
|
+
*
|
|
415
|
+
* // Invoke via agent.json protocol (native loading):
|
|
416
|
+
* const result = await client.invoke('add', { a: 5, b: 3 });
|
|
417
|
+
* // { result: 8 }
|
|
418
|
+
* ```
|
|
419
|
+
*/
|
|
420
|
+
invoke<TInput = unknown, TOutput = unknown>(toolName: string, params: TInput): Promise<TOutput>;
|
|
421
|
+
/**
|
|
422
|
+
* Get broker manager (implements IBrokerClient)
|
|
423
|
+
*/
|
|
424
|
+
getBrokerManager(): BrokerConnectionManager;
|
|
425
|
+
/**
|
|
426
|
+
* Get broker protocol instance (implements IBrokerClient)
|
|
427
|
+
*
|
|
428
|
+
* @param brokerName - Optional broker name (uses default if not specified)
|
|
429
|
+
*/
|
|
430
|
+
getBrokerProtocol(brokerName?: string): BrokerProtocol;
|
|
431
|
+
/**
|
|
432
|
+
* Networks (implements IBrokerClient)
|
|
433
|
+
*/
|
|
434
|
+
get networks(): string[];
|
|
435
|
+
/**
|
|
436
|
+
* Create minimal configuration for constructor
|
|
437
|
+
* Full resolution happens in connect()
|
|
438
|
+
*/
|
|
439
|
+
private createMinimalConfig;
|
|
440
|
+
/**
|
|
441
|
+
* Setup broker event forwarding
|
|
442
|
+
*/
|
|
443
|
+
private setupBrokerEventForwarding;
|
|
444
|
+
/**
|
|
445
|
+
* Handle incoming tool invocation requests from broker
|
|
446
|
+
*
|
|
447
|
+
* Flow:
|
|
448
|
+
* 1. Extract id from top-level message (JSON-RPC request id)
|
|
449
|
+
* 2. Look up tool handler by name
|
|
450
|
+
* 3. Execute handler with toolInput
|
|
451
|
+
* 4. Send JSON-RPC response back to broker with matching id
|
|
452
|
+
*
|
|
453
|
+
* @param brokerName - Name of the broker connection to use for response
|
|
454
|
+
* @param message - Full JSON-RPC request message with id, method, and params
|
|
455
|
+
* @private
|
|
456
|
+
*/
|
|
457
|
+
private handleIncomingToolRequest;
|
|
458
|
+
}
|
|
459
|
+
//# sourceMappingURL=KadiClient.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"KadiClient.d.ts","sourceRoot":"","sources":["../../src/client/KadiClient.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAEH,OAAO,EAAE,YAAY,EAAE,MAAM,QAAQ,CAAC;AACtC,OAAO,KAAK,EAEV,UAAU,EACV,cAAc,EACd,WAAW,EACX,QAAQ,EACR,aAAa,EACb,mBAAmB,EACnB,cAAc,EACf,MAAM,mBAAmB,CAAC;AAM3B,OAAO,EAAE,uBAAuB,EAAE,MAAM,sCAAsC,CAAC;AAC/E,OAAO,EAAE,cAAc,EAAoB,MAAM,6BAA6B,CAAC;AAC/E,OAAO,EAAiB,KAAK,aAAa,EAAE,KAAK,WAAW,EAAE,KAAK,SAAS,EAAE,MAAM,uBAAuB,CAAC;AAC5G,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,kCAAkC,CAAC;AAItE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAmCG;AACH,qBAAa,UAAW,SAAQ,YAAa,YAAW,aAAa;IACnE;;OAEG;IACH,SAAgB,MAAM,EAAE,cAAc,CAAC;IAEvC;;OAEG;IACH,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAe;IAErC;;OAEG;IACH,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAW;IAElC;;OAEG;IACH,OAAO,CAAC,QAAQ,CAAC,OAAO,CAA0B;IAElD;;OAEG;IACH,OAAO,CAAC,QAAQ,CAAC,SAAS,CAAqC;IAE/D;;OAEG;IACH,OAAO,CAAC,QAAQ,CAAC,aAAa,CAAgB;IAE9C;;OAEG;IACH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