@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,397 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Native Transport
|
|
3
|
+
*
|
|
4
|
+
* Loads abilities in-process for zero-overhead communication.
|
|
5
|
+
* Best for abilities written in JavaScript/TypeScript that can run in the same process.
|
|
6
|
+
*
|
|
7
|
+
* @module transports/NativeTransport
|
|
8
|
+
*/
|
|
9
|
+
import { EventEmitter } from 'events';
|
|
10
|
+
import { resolve } from 'path';
|
|
11
|
+
import { KadiError, ErrorCode } from '../types/index.js';
|
|
12
|
+
/**
|
|
13
|
+
* Type guard to check if loaded module is a KadiClient instance
|
|
14
|
+
*
|
|
15
|
+
* Detection strategy:
|
|
16
|
+
* - Has readAgentJson() method (returns agent.json representation with tools)
|
|
17
|
+
* - Has invoke() method (executes tools by name)
|
|
18
|
+
*
|
|
19
|
+
* This identifies abilities written using the Registration Pattern (Option A),
|
|
20
|
+
* where the ability has an agent.json file and uses KadiClient.registerTool().
|
|
21
|
+
*
|
|
22
|
+
* @param obj - Object to check
|
|
23
|
+
* @returns True if object is a KadiClient instance
|
|
24
|
+
*
|
|
25
|
+
* @example
|
|
26
|
+
* ```typescript
|
|
27
|
+
* const module = await import('./ability.js');
|
|
28
|
+
* if (isKadiClient(module.default)) {
|
|
29
|
+
* const agentJson = module.default.readAgentJson();
|
|
30
|
+
* // { name: 'calculator', tools: [...] }
|
|
31
|
+
* }
|
|
32
|
+
* ```
|
|
33
|
+
*/
|
|
34
|
+
function isKadiClient(obj) {
|
|
35
|
+
return (obj !== null &&
|
|
36
|
+
typeof obj === 'object' &&
|
|
37
|
+
'readAgentJson' in obj &&
|
|
38
|
+
typeof obj.readAgentJson === 'function' &&
|
|
39
|
+
'invoke' in obj &&
|
|
40
|
+
typeof obj.invoke === 'function');
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Native Transport
|
|
44
|
+
*
|
|
45
|
+
* Loads Node.js modules directly into the current process.
|
|
46
|
+
* Provides zero-overhead method invocation since everything runs in-process.
|
|
47
|
+
*
|
|
48
|
+
* @example
|
|
49
|
+
* ```typescript
|
|
50
|
+
* const transport = new NativeTransport({
|
|
51
|
+
* abilityName: 'calculator',
|
|
52
|
+
* abilityPath: '/path/to/calculator',
|
|
53
|
+
* entryPoint: 'index.js'
|
|
54
|
+
* });
|
|
55
|
+
*
|
|
56
|
+
* await transport.connect();
|
|
57
|
+
* const result = await transport.invoke('add', { a: 5, b: 3 });
|
|
58
|
+
* console.log(result); // { result: 8 }
|
|
59
|
+
* ```
|
|
60
|
+
*/
|
|
61
|
+
export class NativeTransport extends EventEmitter {
|
|
62
|
+
/**
|
|
63
|
+
* Transport configuration
|
|
64
|
+
*/
|
|
65
|
+
options;
|
|
66
|
+
/**
|
|
67
|
+
* Loaded ability module
|
|
68
|
+
*/
|
|
69
|
+
ability = null;
|
|
70
|
+
/**
|
|
71
|
+
* Whether loaded module is a KadiClient instance
|
|
72
|
+
*/
|
|
73
|
+
isKadiClientInstance = false;
|
|
74
|
+
/**
|
|
75
|
+
* Discovered methods
|
|
76
|
+
*/
|
|
77
|
+
methods = [];
|
|
78
|
+
/**
|
|
79
|
+
* Method schemas
|
|
80
|
+
*/
|
|
81
|
+
schemas = new Map();
|
|
82
|
+
/**
|
|
83
|
+
* Whether transport is connected
|
|
84
|
+
*/
|
|
85
|
+
connected = false;
|
|
86
|
+
/**
|
|
87
|
+
* Create a new NativeTransport
|
|
88
|
+
*
|
|
89
|
+
* @param options - Native transport options
|
|
90
|
+
*/
|
|
91
|
+
constructor(options) {
|
|
92
|
+
super();
|
|
93
|
+
this.options = {
|
|
94
|
+
entryPoint: options.entryPoint ?? 'index.js',
|
|
95
|
+
timeout: options.timeout ?? 30000,
|
|
96
|
+
abilityPath: options.abilityPath,
|
|
97
|
+
abilityName: options.abilityName,
|
|
98
|
+
abilityVersion: options.abilityVersion ?? '1.0.0'
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* Connect to the ability
|
|
103
|
+
*
|
|
104
|
+
* Loads the module from disk and performs discovery.
|
|
105
|
+
*
|
|
106
|
+
* @throws {KadiError} If module cannot be loaded
|
|
107
|
+
*
|
|
108
|
+
* @example
|
|
109
|
+
* ```typescript
|
|
110
|
+
* await transport.connect();
|
|
111
|
+
* ```
|
|
112
|
+
*/
|
|
113
|
+
async connect() {
|
|
114
|
+
if (this.connected) {
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
117
|
+
try {
|
|
118
|
+
// Resolve module path
|
|
119
|
+
const modulePath = resolve(this.options.abilityPath, this.options.entryPoint);
|
|
120
|
+
// Load the module
|
|
121
|
+
const module = await import(modulePath);
|
|
122
|
+
// Get the ability (default export or named export)
|
|
123
|
+
this.ability = module.default ?? module;
|
|
124
|
+
if (!this.ability || typeof this.ability !== 'object') {
|
|
125
|
+
throw new KadiError(`Ability module must export an object or class instance`, ErrorCode.ABILITY_LOAD_FAILED, 500, { abilityName: this.options.abilityName, modulePath });
|
|
126
|
+
}
|
|
127
|
+
// Check if it's a KadiClient instance (Registration Pattern - Option A)
|
|
128
|
+
this.isKadiClientInstance = isKadiClient(this.ability);
|
|
129
|
+
if (this.isKadiClientInstance) {
|
|
130
|
+
// KadiClient instance - read agent.json representation
|
|
131
|
+
const agentJson = this.ability.readAgentJson();
|
|
132
|
+
this.methods = agentJson.tools.map(tool => tool.name);
|
|
133
|
+
// Store schemas from tool definitions (agent.json's 'exports' → runtime 'tools')
|
|
134
|
+
for (const tool of agentJson.tools) {
|
|
135
|
+
if (tool.inputSchema || tool.outputSchema) {
|
|
136
|
+
this.schemas.set(tool.name, {
|
|
137
|
+
inputSchema: tool.inputSchema,
|
|
138
|
+
outputSchema: tool.outputSchema,
|
|
139
|
+
description: tool.description
|
|
140
|
+
});
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
else {
|
|
145
|
+
// Legacy module - use introspection
|
|
146
|
+
await this.discoverMethods();
|
|
147
|
+
}
|
|
148
|
+
this.connected = true;
|
|
149
|
+
this.emit('connected');
|
|
150
|
+
}
|
|
151
|
+
catch (error) {
|
|
152
|
+
const kadiError = error instanceof KadiError
|
|
153
|
+
? error
|
|
154
|
+
: new KadiError(`Failed to load ability: ${error instanceof Error ? error.message : String(error)}`, ErrorCode.ABILITY_LOAD_FAILED, 500, {
|
|
155
|
+
abilityName: this.options.abilityName,
|
|
156
|
+
abilityPath: this.options.abilityPath,
|
|
157
|
+
originalError: error instanceof Error ? error.message : String(error)
|
|
158
|
+
});
|
|
159
|
+
this.emit('error', kadiError);
|
|
160
|
+
throw kadiError;
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
/**
|
|
164
|
+
* Invoke a method on the ability
|
|
165
|
+
*
|
|
166
|
+
* @template TInput - Input parameter type
|
|
167
|
+
* @template TOutput - Return type
|
|
168
|
+
*
|
|
169
|
+
* @param method - Method name
|
|
170
|
+
* @param params - Method parameters
|
|
171
|
+
* @returns Promise resolving to method result
|
|
172
|
+
*
|
|
173
|
+
* @throws {KadiError} If method doesn't exist or invocation fails
|
|
174
|
+
*
|
|
175
|
+
* @example
|
|
176
|
+
* ```typescript
|
|
177
|
+
* const result = await transport.invoke<{ a: number; b: number }, { result: number }>(
|
|
178
|
+
* 'add',
|
|
179
|
+
* { a: 5, b: 3 }
|
|
180
|
+
* );
|
|
181
|
+
* ```
|
|
182
|
+
*/
|
|
183
|
+
async invoke(method, params) {
|
|
184
|
+
if (!this.connected) {
|
|
185
|
+
throw new KadiError('Transport not connected', ErrorCode.TRANSPORT_NOT_CONNECTED, 503, { abilityName: this.options.abilityName });
|
|
186
|
+
}
|
|
187
|
+
// Check if method exists
|
|
188
|
+
if (!this.methods.includes(method)) {
|
|
189
|
+
throw new KadiError(`Method '${method}' not found on ability '${this.options.abilityName}'`, ErrorCode.ABILITY_METHOD_NOT_FOUND, 404, { abilityName: this.options.abilityName, method, availableMethods: this.methods });
|
|
190
|
+
}
|
|
191
|
+
try {
|
|
192
|
+
// KadiClient instance - use agent.json protocol (invoke by name)
|
|
193
|
+
if (this.isKadiClientInstance) {
|
|
194
|
+
const timeoutPromise = new Promise((_, reject) => {
|
|
195
|
+
setTimeout(() => {
|
|
196
|
+
reject(new KadiError(`Method invocation timeout after ${this.options.timeout}ms`, ErrorCode.TOOL_TIMEOUT, 408, { abilityName: this.options.abilityName, method, timeout: this.options.timeout }));
|
|
197
|
+
}, this.options.timeout);
|
|
198
|
+
});
|
|
199
|
+
const result = await Promise.race([
|
|
200
|
+
this.ability.invoke(method, params),
|
|
201
|
+
timeoutPromise
|
|
202
|
+
]);
|
|
203
|
+
return result;
|
|
204
|
+
}
|
|
205
|
+
// Legacy module - use direct method access
|
|
206
|
+
const handler = this.ability[method];
|
|
207
|
+
if (typeof handler !== 'function') {
|
|
208
|
+
throw new KadiError(`Method '${method}' is not a function`, ErrorCode.ABILITY_METHOD_NOT_FOUND, 500, { abilityName: this.options.abilityName, method, type: typeof handler });
|
|
209
|
+
}
|
|
210
|
+
// Invoke with timeout
|
|
211
|
+
const timeoutPromise = new Promise((_, reject) => {
|
|
212
|
+
setTimeout(() => {
|
|
213
|
+
reject(new KadiError(`Method invocation timeout after ${this.options.timeout}ms`, ErrorCode.TOOL_TIMEOUT, 408, { abilityName: this.options.abilityName, method, timeout: this.options.timeout }));
|
|
214
|
+
}, this.options.timeout);
|
|
215
|
+
});
|
|
216
|
+
const result = await Promise.race([
|
|
217
|
+
handler.call(this.ability, params),
|
|
218
|
+
timeoutPromise
|
|
219
|
+
]);
|
|
220
|
+
return result;
|
|
221
|
+
}
|
|
222
|
+
catch (error) {
|
|
223
|
+
// Don't wrap KadiError
|
|
224
|
+
if (error instanceof KadiError) {
|
|
225
|
+
throw error;
|
|
226
|
+
}
|
|
227
|
+
// Enhance the original error with context
|
|
228
|
+
if (error instanceof Error) {
|
|
229
|
+
const enhancedError = error;
|
|
230
|
+
enhancedError.message = `${error.message} (in ${this.options.abilityName}.${method})`;
|
|
231
|
+
throw enhancedError;
|
|
232
|
+
}
|
|
233
|
+
throw new KadiError(`Method invocation failed: ${String(error)}`, ErrorCode.TOOL_INVOCATION_FAILED, 500, { abilityName: this.options.abilityName, method });
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
/**
|
|
237
|
+
* Get list of available methods
|
|
238
|
+
*
|
|
239
|
+
* @returns Array of method names
|
|
240
|
+
*
|
|
241
|
+
* @example
|
|
242
|
+
* ```typescript
|
|
243
|
+
* const methods = transport.getMethods();
|
|
244
|
+
* console.log('Available methods:', methods);
|
|
245
|
+
* ```
|
|
246
|
+
*/
|
|
247
|
+
getMethods() {
|
|
248
|
+
return [...this.methods];
|
|
249
|
+
}
|
|
250
|
+
/**
|
|
251
|
+
* Get schema for a specific method
|
|
252
|
+
*
|
|
253
|
+
* @param method - Method name
|
|
254
|
+
* @returns Method schema or undefined
|
|
255
|
+
*
|
|
256
|
+
* @example
|
|
257
|
+
* ```typescript
|
|
258
|
+
* const schema = transport.getMethodSchema('add');
|
|
259
|
+
* if (schema) {
|
|
260
|
+
* console.log('Input schema:', schema.inputSchema);
|
|
261
|
+
* }
|
|
262
|
+
* ```
|
|
263
|
+
*/
|
|
264
|
+
getMethodSchema(method) {
|
|
265
|
+
return this.schemas.get(method);
|
|
266
|
+
}
|
|
267
|
+
/**
|
|
268
|
+
* Read agent.json representation (only for KadiClient instances)
|
|
269
|
+
*
|
|
270
|
+
* Returns the agent.json representation from KadiClient.readAgentJson() if
|
|
271
|
+
* the loaded module is a KadiClient instance. Returns undefined for legacy modules.
|
|
272
|
+
*
|
|
273
|
+
* **What this returns:**
|
|
274
|
+
* - Runtime representation of the ability's agent.json file
|
|
275
|
+
* - Contains name, version, description, and tools (mapped from agent.json's 'exports')
|
|
276
|
+
* - Used for type generation during `kadi install`
|
|
277
|
+
*
|
|
278
|
+
* @returns agent.json representation or undefined
|
|
279
|
+
*
|
|
280
|
+
* @example
|
|
281
|
+
* ```typescript
|
|
282
|
+
* const agentJson = transport.readAgentJson();
|
|
283
|
+
* if (agentJson) {
|
|
284
|
+
* console.log('Ability:', agentJson.name, agentJson.version);
|
|
285
|
+
* console.log('Tools:', agentJson.tools.map(t => t.name));
|
|
286
|
+
* // This represents what's in the ability's agent.json file
|
|
287
|
+
* }
|
|
288
|
+
* ```
|
|
289
|
+
*/
|
|
290
|
+
readAgentJson() {
|
|
291
|
+
if (!this.isKadiClientInstance || !this.ability) {
|
|
292
|
+
return undefined;
|
|
293
|
+
}
|
|
294
|
+
return this.ability.readAgentJson();
|
|
295
|
+
}
|
|
296
|
+
/**
|
|
297
|
+
* Disconnect from the ability
|
|
298
|
+
*
|
|
299
|
+
* Cleans up resources and removes references.
|
|
300
|
+
*
|
|
301
|
+
* @example
|
|
302
|
+
* ```typescript
|
|
303
|
+
* await transport.disconnect();
|
|
304
|
+
* ```
|
|
305
|
+
*/
|
|
306
|
+
async disconnect() {
|
|
307
|
+
if (!this.connected) {
|
|
308
|
+
return;
|
|
309
|
+
}
|
|
310
|
+
// Clean up
|
|
311
|
+
this.ability = null;
|
|
312
|
+
this.methods = [];
|
|
313
|
+
this.schemas.clear();
|
|
314
|
+
this.connected = false;
|
|
315
|
+
this.emit('disconnected');
|
|
316
|
+
}
|
|
317
|
+
/**
|
|
318
|
+
* Publish an event from the ability
|
|
319
|
+
*
|
|
320
|
+
* @param eventName - Event name
|
|
321
|
+
* @param data - Event data
|
|
322
|
+
*
|
|
323
|
+
* @example
|
|
324
|
+
* ```typescript
|
|
325
|
+
* transport.publishEvent('calculation.completed', { result: 42 });
|
|
326
|
+
* ```
|
|
327
|
+
*/
|
|
328
|
+
publishEvent(eventName, data) {
|
|
329
|
+
this.emit('event', {
|
|
330
|
+
eventName,
|
|
331
|
+
data,
|
|
332
|
+
timestamp: Date.now(),
|
|
333
|
+
source: this.options.abilityName
|
|
334
|
+
});
|
|
335
|
+
}
|
|
336
|
+
/**
|
|
337
|
+
* Discover methods on the loaded ability
|
|
338
|
+
*
|
|
339
|
+
* Introspects the ability object to find callable methods.
|
|
340
|
+
* Excludes internal methods (starting with _) and special methods.
|
|
341
|
+
*/
|
|
342
|
+
async discoverMethods() {
|
|
343
|
+
if (!this.ability || typeof this.ability !== 'object') {
|
|
344
|
+
return;
|
|
345
|
+
}
|
|
346
|
+
const excludedMethods = new Set([
|
|
347
|
+
'constructor',
|
|
348
|
+
'toString',
|
|
349
|
+
'toJSON',
|
|
350
|
+
'valueOf',
|
|
351
|
+
'hasOwnProperty',
|
|
352
|
+
'isPrototypeOf',
|
|
353
|
+
'propertyIsEnumerable'
|
|
354
|
+
]);
|
|
355
|
+
// Get all property names (own and inherited)
|
|
356
|
+
const allProps = new Set();
|
|
357
|
+
// Own properties
|
|
358
|
+
for (const prop of Object.getOwnPropertyNames(this.ability)) {
|
|
359
|
+
allProps.add(prop);
|
|
360
|
+
}
|
|
361
|
+
// Prototype properties
|
|
362
|
+
let proto = Object.getPrototypeOf(this.ability);
|
|
363
|
+
while (proto && proto !== Object.prototype) {
|
|
364
|
+
for (const prop of Object.getOwnPropertyNames(proto)) {
|
|
365
|
+
allProps.add(prop);
|
|
366
|
+
}
|
|
367
|
+
proto = Object.getPrototypeOf(proto);
|
|
368
|
+
}
|
|
369
|
+
// Filter to functions, exclude internal methods
|
|
370
|
+
for (const prop of allProps) {
|
|
371
|
+
if (excludedMethods.has(prop)) {
|
|
372
|
+
continue;
|
|
373
|
+
}
|
|
374
|
+
if (prop.startsWith('_')) {
|
|
375
|
+
continue; // Internal method
|
|
376
|
+
}
|
|
377
|
+
try {
|
|
378
|
+
const value = this.ability[prop];
|
|
379
|
+
if (typeof value === 'function') {
|
|
380
|
+
this.methods.push(prop);
|
|
381
|
+
// Try to get schema if available
|
|
382
|
+
const schemaGetter = this.ability[`${prop}Schema`];
|
|
383
|
+
if (typeof schemaGetter === 'function') {
|
|
384
|
+
const schema = schemaGetter.call(this.ability);
|
|
385
|
+
if (schema) {
|
|
386
|
+
this.schemas.set(prop, schema);
|
|
387
|
+
}
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
catch {
|
|
392
|
+
// Skip properties that throw on access
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
}
|
|
397
|
+
//# sourceMappingURL=NativeTransport.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"NativeTransport.js","sourceRoot":"","sources":["../../src/transports/NativeTransport.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,EAAE,YAAY,EAAE,MAAM,QAAQ,CAAC;AACtC,OAAO,EAAE,OAAO,EAAE,MAAM,MAAM,CAAC;AAM/B,OAAO,EAAE,SAAS,EAAE,SAAS,EAAE,MAAM,mBAAmB,CAAC;AAiBzD;;;;;;;;;;;;;;;;;;;;;GAqBG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,EAAE,CAAC;wBACvC,MAAM,MAAM,GAAG,YAAY,CAAC,IAAI,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;wBAC/C,IAAI,MAAM,EAAE,CAAC;4BACX,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,IAAI,EAAE,MAAsB,CAAC,CAAC;wBACjD,CAAC;oBACH,CAAC;gBACH,CAAC;YACH,CAAC;YAAC,MAAM,CAAC;gBACP,uCAAuC;YACzC,CAAC;QACH,CAAC;IACH,CAAC;CACF"}
|
|
@@ -0,0 +1,250 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Stdio Transport
|
|
3
|
+
*
|
|
4
|
+
* Spawns abilities as child processes and communicates via stdin/stdout
|
|
5
|
+
* using Content-Length framed JSON-RPC messages (LSP-style).
|
|
6
|
+
*
|
|
7
|
+
* @module transports/StdioTransport
|
|
8
|
+
*/
|
|
9
|
+
import { EventEmitter } from 'events';
|
|
10
|
+
import type { AbilityTransport, MethodSchema } from '../types/index.js';
|
|
11
|
+
import type { AgentJson } from '../abilities/types.js';
|
|
12
|
+
/**
|
|
13
|
+
* Stdio Transport Options
|
|
14
|
+
*/
|
|
15
|
+
export interface StdioTransportOptions {
|
|
16
|
+
/** Ability name (for error messages) */
|
|
17
|
+
abilityName: string;
|
|
18
|
+
/** Command to execute (e.g., 'node', 'python') */
|
|
19
|
+
command: string;
|
|
20
|
+
/** Command arguments (e.g., ['./ability.js']) */
|
|
21
|
+
args?: string[];
|
|
22
|
+
/** Working directory for child process */
|
|
23
|
+
cwd?: string;
|
|
24
|
+
/** Environment variables */
|
|
25
|
+
env?: Record<string, string>;
|
|
26
|
+
/** Request timeout in milliseconds */
|
|
27
|
+
timeout?: number;
|
|
28
|
+
/** Ability version */
|
|
29
|
+
abilityVersion?: string;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Stdio Transport
|
|
33
|
+
*
|
|
34
|
+
* Spawns an ability as a child process and communicates via JSON-RPC
|
|
35
|
+
* over stdin/stdout using Content-Length framing (LSP-style).
|
|
36
|
+
*
|
|
37
|
+
* **Features**:
|
|
38
|
+
* - Spawns child process with configurable command/args
|
|
39
|
+
* - Content-Length framed messages (reliable even with logs)
|
|
40
|
+
* - Captures stderr separately (ability logs)
|
|
41
|
+
* - Request/response matching via JSON-RPC ids
|
|
42
|
+
* - Timeout handling per request
|
|
43
|
+
* - Graceful shutdown with SIGTERM
|
|
44
|
+
*
|
|
45
|
+
* @example
|
|
46
|
+
* ```typescript
|
|
47
|
+
* const transport = new StdioTransport({
|
|
48
|
+
* abilityName: 'calculator',
|
|
49
|
+
* command: 'node',
|
|
50
|
+
* args: ['./abilities/calculator/index.js']
|
|
51
|
+
* });
|
|
52
|
+
*
|
|
53
|
+
* await transport.connect();
|
|
54
|
+
* const result = await transport.invoke('add', { a: 5, b: 3 });
|
|
55
|
+
* console.log(result); // { result: 8 }
|
|
56
|
+
* await transport.disconnect();
|
|
57
|
+
* ```
|
|
58
|
+
*/
|
|
59
|
+
export declare class StdioTransport extends EventEmitter implements AbilityTransport {
|
|
60
|
+
/**
|
|
61
|
+
* Transport configuration
|
|
62
|
+
*/
|
|
63
|
+
private readonly options;
|
|
64
|
+
/**
|
|
65
|
+
* Child process instance
|
|
66
|
+
*/
|
|
67
|
+
private process?;
|
|
68
|
+
/**
|
|
69
|
+
* Message reader for stdout
|
|
70
|
+
*/
|
|
71
|
+
private reader?;
|
|
72
|
+
/**
|
|
73
|
+
* Message writer for stdin
|
|
74
|
+
*/
|
|
75
|
+
private writer?;
|
|
76
|
+
/**
|
|
77
|
+
* Stderr buffer
|
|
78
|
+
*/
|
|
79
|
+
private stderrBuffer;
|
|
80
|
+
/**
|
|
81
|
+
* Whether transport is connected
|
|
82
|
+
*/
|
|
83
|
+
private connected;
|
|
84
|
+
/**
|
|
85
|
+
* Whether we're intentionally disconnecting
|
|
86
|
+
*/
|
|
87
|
+
private disconnecting;
|
|
88
|
+
/**
|
|
89
|
+
* Discovered methods
|
|
90
|
+
*/
|
|
91
|
+
private methods;
|
|
92
|
+
/**
|
|
93
|
+
* Method schemas
|
|
94
|
+
*/
|
|
95
|
+
private schemas;
|
|
96
|
+
/**
|
|
97
|
+
* Agent.json representation (from readAgentJson)
|
|
98
|
+
*/
|
|
99
|
+
private agentJson?;
|
|
100
|
+
/**
|
|
101
|
+
* Request ID counter
|
|
102
|
+
*/
|
|
103
|
+
private requestIdCounter;
|
|
104
|
+
/**
|
|
105
|
+
* Pending requests (waiting for responses)
|
|
106
|
+
*/
|
|
107
|
+
private pendingRequests;
|
|
108
|
+
/**
|
|
109
|
+
* Create a new StdioTransport
|
|
110
|
+
*
|
|
111
|
+
* @param options - Transport options
|
|
112
|
+
*/
|
|
113
|
+
constructor(options: StdioTransportOptions);
|
|
114
|
+
/**
|
|
115
|
+
* Connect to the ability
|
|
116
|
+
*
|
|
117
|
+
* Spawns the child process, sets up stdio communication,
|
|
118
|
+
* and performs discovery via readAgentJson.
|
|
119
|
+
*
|
|
120
|
+
* @throws {KadiError} If spawn fails or discovery fails
|
|
121
|
+
*
|
|
122
|
+
* @example
|
|
123
|
+
* ```typescript
|
|
124
|
+
* await transport.connect();
|
|
125
|
+
* ```
|
|
126
|
+
*/
|
|
127
|
+
connect(): Promise<void>;
|
|
128
|
+
/**
|
|
129
|
+
* Invoke a method on the ability
|
|
130
|
+
*
|
|
131
|
+
* @template TInput - Input parameter type
|
|
132
|
+
* @template TOutput - Return type
|
|
133
|
+
*
|
|
134
|
+
* @param method - Method name
|
|
135
|
+
* @param params - Method parameters
|
|
136
|
+
* @returns Promise resolving to method result
|
|
137
|
+
*
|
|
138
|
+
* @throws {KadiError} If method doesn't exist or invocation fails
|
|
139
|
+
*
|
|
140
|
+
* @example
|
|
141
|
+
* ```typescript
|
|
142
|
+
* const result = await transport.invoke<{ a: number; b: number }, { result: number }>(
|
|
143
|
+
* 'add',
|
|
144
|
+
* { a: 5, b: 3 }
|
|
145
|
+
* );
|
|
146
|
+
* ```
|
|
147
|
+
*/
|
|
148
|
+
invoke<TInput = unknown, TOutput = unknown>(method: string, params: TInput): Promise<TOutput>;
|
|
149
|
+
/**
|
|
150
|
+
* Send a JSON-RPC request and wait for response
|
|
151
|
+
*
|
|
152
|
+
* @param method - JSON-RPC method name
|
|
153
|
+
* @param params - Request parameters
|
|
154
|
+
* @returns Promise resolving to response result
|
|
155
|
+
*
|
|
156
|
+
* @throws {KadiError} If request fails or times out
|
|
157
|
+
*/
|
|
158
|
+
private sendRequest;
|
|
159
|
+
/**
|
|
160
|
+
* Handle incoming JSON-RPC message
|
|
161
|
+
*
|
|
162
|
+
* Matches responses to pending requests via id.
|
|
163
|
+
*
|
|
164
|
+
* @param message - Incoming message
|
|
165
|
+
*/
|
|
166
|
+
private handleMessage;
|
|
167
|
+
/**
|
|
168
|
+
* Get list of available methods
|
|
169
|
+
*
|
|
170
|
+
* @returns Array of method names
|
|
171
|
+
*
|
|
172
|
+
* @example
|
|
173
|
+
* ```typescript
|
|
174
|
+
* const methods = transport.getMethods();
|
|
175
|
+
* console.log('Available methods:', methods);
|
|
176
|
+
* ```
|
|
177
|
+
*/
|
|
178
|
+
getMethods(): string[];
|
|
179
|
+
/**
|
|
180
|
+
* Get schema for a specific method
|
|
181
|
+
*
|
|
182
|
+
* @param method - Method name
|
|
183
|
+
* @returns Method schema or undefined
|
|
184
|
+
*
|
|
185
|
+
* @example
|
|
186
|
+
* ```typescript
|
|
187
|
+
* const schema = transport.getMethodSchema('add');
|
|
188
|
+
* if (schema) {
|
|
189
|
+
* console.log('Input schema:', schema.inputSchema);
|
|
190
|
+
* }
|
|
191
|
+
* ```
|
|
192
|
+
*/
|
|
193
|
+
getMethodSchema(method: string): MethodSchema | undefined;
|
|
194
|
+
/**
|
|
195
|
+
* Read agent.json representation
|
|
196
|
+
*
|
|
197
|
+
* Returns the agent.json representation retrieved during connection.
|
|
198
|
+
*
|
|
199
|
+
* @returns agent.json representation or undefined
|
|
200
|
+
*
|
|
201
|
+
* @example
|
|
202
|
+
* ```typescript
|
|
203
|
+
* const agentJson = transport.readAgentJson();
|
|
204
|
+
* if (agentJson) {
|
|
205
|
+
* console.log('Ability:', agentJson.name, agentJson.version);
|
|
206
|
+
* console.log('Tools:', agentJson.tools.map(t => t.name));
|
|
207
|
+
* }
|
|
208
|
+
* ```
|
|
209
|
+
*/
|
|
210
|
+
readAgentJson(): AgentJson | undefined;
|
|
211
|
+
/**
|
|
212
|
+
* Get captured stderr output
|
|
213
|
+
*
|
|
214
|
+
* Returns all stderr output captured from the child process.
|
|
215
|
+
* Useful for debugging or viewing ability logs.
|
|
216
|
+
*
|
|
217
|
+
* @returns Stderr content as string
|
|
218
|
+
*
|
|
219
|
+
* @example
|
|
220
|
+
* ```typescript
|
|
221
|
+
* const stderr = transport.getStderr();
|
|
222
|
+
* console.log('Ability logs:', stderr);
|
|
223
|
+
* ```
|
|
224
|
+
*/
|
|
225
|
+
getStderr(): string;
|
|
226
|
+
/**
|
|
227
|
+
* Disconnect from the ability
|
|
228
|
+
*
|
|
229
|
+
* Kills the child process with SIGTERM and cleans up resources.
|
|
230
|
+
*
|
|
231
|
+
* @example
|
|
232
|
+
* ```typescript
|
|
233
|
+
* await transport.disconnect();
|
|
234
|
+
* ```
|
|
235
|
+
*/
|
|
236
|
+
disconnect(): Promise<void>;
|
|
237
|
+
/**
|
|
238
|
+
* Publish an event from the ability
|
|
239
|
+
*
|
|
240
|
+
* @param eventName - Event name
|
|
241
|
+
* @param data - Event data
|
|
242
|
+
*
|
|
243
|
+
* @example
|
|
244
|
+
* ```typescript
|
|
245
|
+
* transport.publishEvent('calculation.completed', { result: 42 });
|
|
246
|
+
* ```
|
|
247
|
+
*/
|
|
248
|
+
publishEvent(eventName: string, data: unknown): void;
|
|
249
|
+
}
|
|
250
|
+
//# sourceMappingURL=StdioTransport.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"StdioTransport.d.ts","sourceRoot":"","sources":["../../src/transports/StdioTransport.ts"],"names":[],"mappings":"A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