@kadi.build/core 0.0.1-alpha.3 → 0.0.1-alpha.4
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 +571 -594
- package/dist/KadiClient.d.ts +303 -0
- package/dist/KadiClient.d.ts.map +1 -0
- package/dist/KadiClient.js +1162 -0
- package/dist/KadiClient.js.map +1 -0
- package/dist/errors/error-codes.d.ts +215 -0
- package/dist/errors/error-codes.d.ts.map +1 -0
- package/dist/errors/error-codes.js +295 -0
- package/dist/errors/error-codes.js.map +1 -0
- package/dist/index.d.ts +15 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +24 -0
- package/dist/index.js.map +1 -0
- package/dist/loadAbility.d.ts +65 -0
- package/dist/loadAbility.d.ts.map +1 -0
- package/dist/loadAbility.js +335 -0
- package/dist/loadAbility.js.map +1 -0
- package/dist/messages/BrokerMessages.d.ts +84 -0
- package/dist/messages/BrokerMessages.d.ts.map +1 -0
- package/dist/messages/BrokerMessages.js +127 -0
- package/dist/messages/BrokerMessages.js.map +1 -0
- package/dist/messages/MessageBuilder.d.ts +83 -0
- package/dist/messages/MessageBuilder.d.ts.map +1 -0
- package/dist/messages/MessageBuilder.js +144 -0
- package/dist/messages/MessageBuilder.js.map +1 -0
- package/dist/schemas/events.schemas.d.ts +177 -0
- package/dist/schemas/events.schemas.d.ts.map +1 -0
- package/dist/schemas/events.schemas.js +265 -0
- package/dist/schemas/events.schemas.js.map +1 -0
- package/dist/schemas/index.d.ts +3 -0
- package/dist/schemas/index.d.ts.map +1 -0
- package/dist/schemas/index.js +4 -0
- package/dist/schemas/index.js.map +1 -0
- package/dist/schemas/kadi.schemas.d.ts +70 -0
- package/dist/schemas/kadi.schemas.d.ts.map +1 -0
- package/dist/schemas/kadi.schemas.js +120 -0
- package/dist/schemas/kadi.schemas.js.map +1 -0
- package/dist/transports/BrokerTransport.d.ts +96 -0
- package/dist/transports/BrokerTransport.d.ts.map +1 -0
- package/dist/transports/BrokerTransport.js +145 -0
- package/dist/transports/BrokerTransport.js.map +1 -0
- package/dist/transports/NativeTransport.d.ts +92 -0
- package/dist/transports/NativeTransport.d.ts.map +1 -0
- package/dist/transports/NativeTransport.js +221 -0
- package/dist/transports/NativeTransport.js.map +1 -0
- package/dist/transports/StdioTransport.d.ts +112 -0
- package/dist/transports/StdioTransport.d.ts.map +1 -0
- package/dist/transports/StdioTransport.js +440 -0
- package/dist/transports/StdioTransport.js.map +1 -0
- package/dist/transports/Transport.d.ts +93 -0
- package/dist/transports/Transport.d.ts.map +1 -0
- package/dist/transports/Transport.js +13 -0
- package/dist/transports/Transport.js.map +1 -0
- package/dist/types/broker.d.ts +31 -0
- package/dist/types/broker.d.ts.map +1 -0
- package/dist/types/broker.js +6 -0
- package/dist/types/broker.js.map +1 -0
- package/dist/types/core.d.ts +138 -0
- package/dist/types/core.d.ts.map +1 -0
- package/dist/types/core.js +26 -0
- package/dist/types/core.js.map +1 -0
- package/dist/types/events.d.ts +186 -0
- package/dist/types/events.d.ts.map +1 -0
- package/dist/types/events.js +16 -0
- package/dist/types/events.js.map +1 -0
- package/dist/types/index.d.ts +9 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/index.js +13 -0
- package/dist/types/index.js.map +1 -0
- package/dist/types/protocol.d.ts +160 -0
- package/dist/types/protocol.d.ts.map +1 -0
- package/dist/types/protocol.js +5 -0
- package/dist/types/protocol.js.map +1 -0
- package/dist/utils/agentUtils.d.ts +102 -0
- package/dist/utils/agentUtils.d.ts.map +1 -0
- package/dist/utils/agentUtils.js +166 -0
- package/dist/utils/agentUtils.js.map +1 -0
- package/dist/utils/commandUtils.d.ts +45 -0
- package/dist/utils/commandUtils.d.ts.map +1 -0
- package/dist/utils/commandUtils.js +145 -0
- package/dist/utils/commandUtils.js.map +1 -0
- package/dist/utils/configUtils.d.ts +55 -0
- package/dist/utils/configUtils.d.ts.map +1 -0
- package/dist/utils/configUtils.js +100 -0
- package/dist/utils/configUtils.js.map +1 -0
- package/dist/utils/logger.d.ts +59 -0
- package/dist/utils/logger.d.ts.map +1 -0
- package/dist/utils/logger.js +122 -0
- package/dist/utils/logger.js.map +1 -0
- package/dist/utils/pathUtils.d.ts +48 -0
- package/dist/utils/pathUtils.d.ts.map +1 -0
- package/dist/utils/pathUtils.js +128 -0
- package/dist/utils/pathUtils.js.map +1 -0
- package/package.json +56 -5
- package/agent.json +0 -18
- package/examples/example-abilities/echo-js/README.md +0 -131
- package/examples/example-abilities/echo-js/agent.json +0 -63
- package/examples/example-abilities/echo-js/package.json +0 -24
- package/examples/example-abilities/echo-js/service.js +0 -43
- package/examples/example-abilities/hash-go/agent.json +0 -53
- package/examples/example-abilities/hash-go/cmd/hash_ability/main.go +0 -340
- package/examples/example-abilities/hash-go/go.mod +0 -3
- package/examples/example-agent/abilities/echo-js/0.0.1/README.md +0 -131
- package/examples/example-agent/abilities/echo-js/0.0.1/agent.json +0 -63
- package/examples/example-agent/abilities/echo-js/0.0.1/package-lock.json +0 -93
- package/examples/example-agent/abilities/echo-js/0.0.1/package.json +0 -24
- package/examples/example-agent/abilities/echo-js/0.0.1/service.js +0 -41
- package/examples/example-agent/abilities/hash-go/0.0.1/agent.json +0 -53
- package/examples/example-agent/abilities/hash-go/0.0.1/bin/hash_ability +0 -0
- package/examples/example-agent/abilities/hash-go/0.0.1/cmd/hash_ability/main.go +0 -340
- package/examples/example-agent/abilities/hash-go/0.0.1/go.mod +0 -3
- package/examples/example-agent/agent.json +0 -39
- package/examples/example-agent/index.js +0 -102
- package/examples/example-agent/package-lock.json +0 -93
- package/examples/example-agent/package.json +0 -17
- package/src/KadiAbility.js +0 -478
- package/src/index.js +0 -65
- package/src/loadAbility.js +0 -1086
- package/src/servers/BaseRpcServer.js +0 -404
- package/src/servers/BrokerRpcServer.js +0 -776
- package/src/servers/StdioRpcServer.js +0 -360
- package/src/transport/BrokerMessageBuilder.js +0 -377
- package/src/transport/IpcMessageBuilder.js +0 -1229
- package/src/utils/agentUtils.js +0 -137
- package/src/utils/commandUtils.js +0 -64
- package/src/utils/configUtils.js +0 -72
- package/src/utils/logger.js +0 -161
- package/src/utils/pathUtils.js +0 -86
|
@@ -0,0 +1,221 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* NativeTransport - Handles direct in-process ability loading
|
|
3
|
+
*
|
|
4
|
+
* This transport loads abilities directly into the same process,
|
|
5
|
+
* allowing for zero-overhead method invocation. It's used when
|
|
6
|
+
* you want to load local JavaScript/TypeScript modules as abilities.
|
|
7
|
+
*
|
|
8
|
+
* The native protocol is the fastest but requires the ability code
|
|
9
|
+
* to be trusted since it runs in the same process.
|
|
10
|
+
*/
|
|
11
|
+
import path from 'node:path';
|
|
12
|
+
import fs from 'node:fs';
|
|
13
|
+
import { EventEmitter } from 'events';
|
|
14
|
+
import { createComponentLogger } from '../utils/logger.js';
|
|
15
|
+
import { getAbilityJSON } from '../utils/agentUtils.js';
|
|
16
|
+
/**
|
|
17
|
+
* NativeTransport - Loads and executes abilities in the same process
|
|
18
|
+
*
|
|
19
|
+
* This transport provides the fastest execution since there's no
|
|
20
|
+
* serialization or IPC overhead. The ability code runs directly
|
|
21
|
+
* in the Node.js process.
|
|
22
|
+
*/
|
|
23
|
+
export class NativeTransport extends EventEmitter {
|
|
24
|
+
options;
|
|
25
|
+
logger;
|
|
26
|
+
ability;
|
|
27
|
+
methodHandlers = new Map();
|
|
28
|
+
connected = false;
|
|
29
|
+
constructor(options) {
|
|
30
|
+
super();
|
|
31
|
+
this.options = options;
|
|
32
|
+
this.logger = createComponentLogger('NativeTransport');
|
|
33
|
+
this.logger.lifecycle('constructor', `Creating native transport for ${options.abilityName}`);
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Connect by loading the ability module into memory
|
|
37
|
+
*
|
|
38
|
+
* This method dynamically imports the ability's JavaScript/TypeScript
|
|
39
|
+
* module and extracts all available methods.
|
|
40
|
+
*/
|
|
41
|
+
async connect() {
|
|
42
|
+
const { abilityName, abilityVersion, abilityDir, manifest } = this.options;
|
|
43
|
+
this.logger.lifecycle('connect', `Loading ${abilityName} as native module`);
|
|
44
|
+
// Determine entry point for the ability
|
|
45
|
+
const entryFromManifest = manifest?.interfaces?.native?.entry;
|
|
46
|
+
let entryRelPath = entryFromManifest;
|
|
47
|
+
// Fallback to checking agent.json or package.json
|
|
48
|
+
if (!entryRelPath) {
|
|
49
|
+
const moduleJSON = getAbilityJSON(abilityName, abilityVersion || '1.0.0');
|
|
50
|
+
entryRelPath = moduleJSON?.entry || moduleJSON?.main || 'index.js';
|
|
51
|
+
}
|
|
52
|
+
if (!entryRelPath) {
|
|
53
|
+
throw new Error(`Ability '${abilityName}' is missing entry point configuration`);
|
|
54
|
+
}
|
|
55
|
+
const modulePath = path.join(abilityDir, entryRelPath);
|
|
56
|
+
if (!fs.existsSync(modulePath)) {
|
|
57
|
+
throw new Error(`Ability '${abilityName}' entry file not found: ${modulePath}`);
|
|
58
|
+
}
|
|
59
|
+
this.logger.debug('connect', `Loading module from: ${modulePath}`);
|
|
60
|
+
// Set environment variable so the module knows it's being loaded via native protocol
|
|
61
|
+
const originalProtocol = process.env.KADI_PROTOCOL;
|
|
62
|
+
process.env.KADI_PROTOCOL = 'native';
|
|
63
|
+
let rawModule;
|
|
64
|
+
try {
|
|
65
|
+
// Dynamic import of the ability
|
|
66
|
+
const mod = await import(modulePath);
|
|
67
|
+
rawModule = mod.default || mod;
|
|
68
|
+
}
|
|
69
|
+
finally {
|
|
70
|
+
// Restore original environment
|
|
71
|
+
if (originalProtocol !== undefined) {
|
|
72
|
+
process.env.KADI_PROTOCOL = originalProtocol;
|
|
73
|
+
}
|
|
74
|
+
else {
|
|
75
|
+
delete process.env.KADI_PROTOCOL;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
// Extract methods based on the module export format
|
|
79
|
+
await this.extractMethods(rawModule, abilityName);
|
|
80
|
+
this.connected = true;
|
|
81
|
+
this.logger.lifecycle('connect', 'Native ability loaded successfully');
|
|
82
|
+
this.emit('connected');
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Extract methods from the loaded module
|
|
86
|
+
*
|
|
87
|
+
* Handles different export formats:
|
|
88
|
+
* - KadiClient/KadiAbility instances
|
|
89
|
+
* - Plain objects with function properties
|
|
90
|
+
* - Class constructors
|
|
91
|
+
*/
|
|
92
|
+
async extractMethods(rawModule, abilityName) {
|
|
93
|
+
if (typeof rawModule === 'object' && rawModule !== null) {
|
|
94
|
+
// Check if this is a KadiClient or KadiAbility instance
|
|
95
|
+
if (rawModule.constructor?.name === 'KadiClient' ||
|
|
96
|
+
rawModule.constructor?.name === 'KadiAbility' ||
|
|
97
|
+
typeof rawModule.getToolNames === 'function') {
|
|
98
|
+
this.logger.debug('extractMethods', 'Detected KadiClient/KadiAbility instance');
|
|
99
|
+
// Extract methods from KadiClient/KadiAbility
|
|
100
|
+
const methodNames = rawModule.getToolNames();
|
|
101
|
+
for (const methodName of methodNames) {
|
|
102
|
+
const handler = rawModule.getToolHandler(methodName);
|
|
103
|
+
if (handler) {
|
|
104
|
+
this.methodHandlers.set(methodName, handler);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
this.ability = rawModule;
|
|
108
|
+
// Forward events if the ability supports them
|
|
109
|
+
if (typeof rawModule.on === 'function') {
|
|
110
|
+
/**
|
|
111
|
+
* Listen for the internal transport event from the loaded ability.
|
|
112
|
+
* KadiClient instances emit __KADI_INTERNAL_ABILITY_EVENT_TRANSPORT__ when
|
|
113
|
+
* they publish events in native mode. We catch these and forward them
|
|
114
|
+
* to the AbilityProxy as regular 'event' emissions.
|
|
115
|
+
*/
|
|
116
|
+
const ABILITY_EVENT_TRANSPORT = '__KADI_INTERNAL_ABILITY_EVENT_TRANSPORT__';
|
|
117
|
+
this.logger.trace('extractMethods', `Setting up ${ABILITY_EVENT_TRANSPORT} listener on rawModule`);
|
|
118
|
+
rawModule.on(ABILITY_EVENT_TRANSPORT, (eventData) => {
|
|
119
|
+
this.logger.trace('extractMethods', `Received ${ABILITY_EVENT_TRANSPORT} from rawModule: ${JSON.stringify(eventData)}`);
|
|
120
|
+
this.logger.debug('extractMethods', 'Emitting event on transport');
|
|
121
|
+
this.emit('event', eventData);
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
else {
|
|
125
|
+
this.logger.debug('extractMethods', 'rawModule does not support .on() method');
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
else {
|
|
129
|
+
// Plain object with methods as properties
|
|
130
|
+
this.logger.debug('extractMethods', 'Processing plain object with methods');
|
|
131
|
+
for (const [key, value] of Object.entries(rawModule)) {
|
|
132
|
+
if (typeof value === 'function' && !key.startsWith('_')) {
|
|
133
|
+
this.methodHandlers.set(key, value);
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
this.ability = rawModule;
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
else if (typeof rawModule === 'function') {
|
|
140
|
+
// Class constructor - instantiate and extract methods
|
|
141
|
+
this.logger.debug('extractMethods', 'Processing class constructor');
|
|
142
|
+
const instance = new rawModule();
|
|
143
|
+
const prototype = Object.getPrototypeOf(instance);
|
|
144
|
+
const methodNames = Object.getOwnPropertyNames(prototype).filter((name) => name !== 'constructor' &&
|
|
145
|
+
!name.startsWith('_') &&
|
|
146
|
+
typeof prototype[name] === 'function');
|
|
147
|
+
for (const name of methodNames) {
|
|
148
|
+
const method = instance[name];
|
|
149
|
+
if (typeof method === 'function') {
|
|
150
|
+
this.methodHandlers.set(name, method.bind(instance));
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
this.ability = instance;
|
|
154
|
+
}
|
|
155
|
+
else {
|
|
156
|
+
throw new Error(`Unsupported module export type for ability '${abilityName}'`);
|
|
157
|
+
}
|
|
158
|
+
this.logger.info('extractMethods', `Extracted ${this.methodHandlers.size} methods from ${abilityName}`);
|
|
159
|
+
}
|
|
160
|
+
/**
|
|
161
|
+
* Invoke a method on the loaded ability
|
|
162
|
+
*
|
|
163
|
+
* Since this is native, we just call the function directly
|
|
164
|
+
* with no serialization overhead.
|
|
165
|
+
*/
|
|
166
|
+
async invoke(method, params) {
|
|
167
|
+
if (!this.connected) {
|
|
168
|
+
throw new Error('Native ability not loaded');
|
|
169
|
+
}
|
|
170
|
+
this.logger.trace('invoke', `Invoking method: ${method}`);
|
|
171
|
+
const handler = this.methodHandlers.get(method);
|
|
172
|
+
if (!handler) {
|
|
173
|
+
throw new Error(`Method '${method}' not found in native ability`);
|
|
174
|
+
}
|
|
175
|
+
try {
|
|
176
|
+
// Direct function call - no serialization needed
|
|
177
|
+
const result = await handler(params);
|
|
178
|
+
this.logger.trace('invoke', `Method ${method} completed successfully`);
|
|
179
|
+
return result;
|
|
180
|
+
}
|
|
181
|
+
catch (error) {
|
|
182
|
+
this.logger.error('invoke', `Method ${method} failed: ${error}`);
|
|
183
|
+
throw error;
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
/**
|
|
187
|
+
* Get list of available methods
|
|
188
|
+
*/
|
|
189
|
+
getMethods() {
|
|
190
|
+
return Array.from(this.methodHandlers.keys());
|
|
191
|
+
}
|
|
192
|
+
/**
|
|
193
|
+
* Get schema for a specific method (if available)
|
|
194
|
+
*/
|
|
195
|
+
getMethodSchema(method) {
|
|
196
|
+
// If the ability is a KadiClient/KadiAbility, it might have schema info
|
|
197
|
+
if (this.ability && typeof this.ability.getMethodSchema === 'function') {
|
|
198
|
+
return this.ability.getMethodSchema(method);
|
|
199
|
+
}
|
|
200
|
+
return undefined;
|
|
201
|
+
}
|
|
202
|
+
/**
|
|
203
|
+
* Disconnect and cleanup
|
|
204
|
+
*
|
|
205
|
+
* Releases references to the loaded module to allow garbage collection
|
|
206
|
+
*/
|
|
207
|
+
async disconnect() {
|
|
208
|
+
this.logger.lifecycle('disconnect', 'Disconnecting native transport');
|
|
209
|
+
this.ability = undefined;
|
|
210
|
+
this.methodHandlers.clear();
|
|
211
|
+
this.connected = false;
|
|
212
|
+
this.emit('disconnected');
|
|
213
|
+
}
|
|
214
|
+
/**
|
|
215
|
+
* Check if transport is connected
|
|
216
|
+
*/
|
|
217
|
+
isConnected() {
|
|
218
|
+
return this.connected;
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
//# sourceMappingURL=NativeTransport.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"NativeTransport.js","sourceRoot":"","sources":["../../src/transports/NativeTransport.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAEH,OAAO,IAAI,MAAM,WAAW,CAAC;AAC7B,OAAO,EAAE,MAAM,SAAS,CAAC;AACzB,OAAO,EAAE,YAAY,EAAE,MAAM,QAAQ,CAAC;AAEtC,OAAO,EAAE,qBAAqB,EAAmB,MAAM,oBAAoB,CAAC;AAC5E,OAAO,EAAE,cAAc,EAAE,MAAM,wBAAwB,CAAC;AA4BxD;;;;;;GAMG;AACH,MAAM,OAAO,eAAgB,SAAQ,YAAY;IAM3B;IALZ,MAAM,CAAa;IACnB,OAAO,CAAM;IACb,cAAc,GAAG,IAAI,GAAG,EAAyB,CAAC;IAClD,SAAS,GAAG,KAAK,CAAC;IAE1B,YAAoB,OAA+B;QACjD,KAAK,EAAE,CAAC;QADU,YAAO,GAAP,OAAO,CAAwB;QAEjD,IAAI,CAAC,MAAM,GAAG,qBAAqB,CAAC,iBAAiB,CAAC,CAAC;QAEvD,IAAI,CAAC,MAAM,CAAC,SAAS,CACnB,aAAa,EACb,iCAAiC,OAAO,CAAC,WAAW,EAAE,CACvD,CAAC;IACJ,CAAC;IAED;;;;;OAKG;IACH,KAAK,CAAC,OAAO;QACX,MAAM,EAAE,WAAW,EAAE,cAAc,EAAE,UAAU,EAAE,QAAQ,EAAE,GAAG,IAAI,CAAC,OAAO,CAAC;QAE3E,IAAI,CAAC,MAAM,CAAC,SAAS,CAAC,SAAS,EAAE,WAAW,WAAW,mBAAmB,CAAC,CAAC;QAE5E,wCAAwC;QACxC,MAAM,iBAAiB,GAAG,QAAQ,E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
|
|
|
1
|
+
/**
|
|
2
|
+
* StdioTransport - Handles stdio communication for abilities
|
|
3
|
+
*
|
|
4
|
+
* This transport operates in two modes:
|
|
5
|
+
*
|
|
6
|
+
* CLIENT MODE: Spawns an ability as a child process and calls its methods
|
|
7
|
+
* - Parent process sends JSON-RPC requests to child's stdin
|
|
8
|
+
* - Child process sends JSON-RPC responses back via stdout
|
|
9
|
+
*
|
|
10
|
+
* SERVER MODE: Serves an ability's methods via stdio (when running as a child)
|
|
11
|
+
* - Reads JSON-RPC requests from stdin
|
|
12
|
+
* - Writes JSON-RPC responses to stdout
|
|
13
|
+
*
|
|
14
|
+
* Both modes use LSP-style message framing for reliable communication.
|
|
15
|
+
* The transport handles all the complexity of:
|
|
16
|
+
* - Process spawning and lifecycle management (client mode)
|
|
17
|
+
* - Message framing and buffering
|
|
18
|
+
* - Request/response correlation
|
|
19
|
+
* - Timeout handling
|
|
20
|
+
*/
|
|
21
|
+
import { EventEmitter } from 'events';
|
|
22
|
+
import { Transport, TransportOptions } from './Transport.js';
|
|
23
|
+
/**
|
|
24
|
+
* Options specific to StdioTransport
|
|
25
|
+
*/
|
|
26
|
+
interface StdioTransportOptions extends TransportOptions {
|
|
27
|
+
/**
|
|
28
|
+
* Directory where the ability code is located
|
|
29
|
+
*/
|
|
30
|
+
abilityDir: string;
|
|
31
|
+
/**
|
|
32
|
+
* Command to start the ability process
|
|
33
|
+
*/
|
|
34
|
+
startCmd: string;
|
|
35
|
+
/**
|
|
36
|
+
* Optional manifest with configuration
|
|
37
|
+
*/
|
|
38
|
+
manifest?: {
|
|
39
|
+
interface?: {
|
|
40
|
+
timeoutMs?: number;
|
|
41
|
+
};
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* StdioTransport - Manages child process abilities via stdio
|
|
46
|
+
*
|
|
47
|
+
* This transport provides process isolation and language independence.
|
|
48
|
+
* The ability runs in a separate process and can be written in any
|
|
49
|
+
* language that supports JSON-RPC over stdio.
|
|
50
|
+
*/
|
|
51
|
+
export declare class StdioTransport extends EventEmitter implements Transport {
|
|
52
|
+
private options;
|
|
53
|
+
private logger;
|
|
54
|
+
private process?;
|
|
55
|
+
private reader?;
|
|
56
|
+
private writer?;
|
|
57
|
+
private connected;
|
|
58
|
+
private pending;
|
|
59
|
+
private nextId;
|
|
60
|
+
constructor(options: StdioTransportOptions);
|
|
61
|
+
/**
|
|
62
|
+
* Connect by spawning the child process
|
|
63
|
+
*
|
|
64
|
+
* This method spawns the ability as a child process and sets up
|
|
65
|
+
* the stdio communication channels.
|
|
66
|
+
*/
|
|
67
|
+
connect(): Promise<void>;
|
|
68
|
+
/**
|
|
69
|
+
* Handle incoming message from child process
|
|
70
|
+
*/
|
|
71
|
+
private handleMessage;
|
|
72
|
+
/**
|
|
73
|
+
* Invoke a method on the child process ability
|
|
74
|
+
*
|
|
75
|
+
* Sends a JSON-RPC request to the child and waits for the response.
|
|
76
|
+
*/
|
|
77
|
+
invoke(method: string, params: unknown): Promise<unknown>;
|
|
78
|
+
/**
|
|
79
|
+
* Get list of available methods
|
|
80
|
+
*/
|
|
81
|
+
getMethods(): string[];
|
|
82
|
+
/**
|
|
83
|
+
* Disconnect and cleanup
|
|
84
|
+
*
|
|
85
|
+
* Terminates the child process and cleans up resources.
|
|
86
|
+
*/
|
|
87
|
+
disconnect(): Promise<void>;
|
|
88
|
+
/**
|
|
89
|
+
* Cleanup resources
|
|
90
|
+
*/
|
|
91
|
+
private cleanup;
|
|
92
|
+
/**
|
|
93
|
+
* Check if transport is connected
|
|
94
|
+
*/
|
|
95
|
+
isConnected(): boolean;
|
|
96
|
+
/**
|
|
97
|
+
* Publish an event to the parent process (when serving as child)
|
|
98
|
+
*/
|
|
99
|
+
publishEvent(eventName: string, data: unknown): void;
|
|
100
|
+
/**
|
|
101
|
+
* Serve an ability via stdio (server mode)
|
|
102
|
+
*
|
|
103
|
+
* This method is used when the transport needs to serve an ability's methods
|
|
104
|
+
* via stdio (when running as a child process). It reads JSON-RPC requests
|
|
105
|
+
* from stdin and writes responses to stdout.
|
|
106
|
+
*
|
|
107
|
+
* @param ability - The ability (KadiClient) to serve
|
|
108
|
+
*/
|
|
109
|
+
serve(ability: any): Promise<void>;
|
|
110
|
+
}
|
|
111
|
+
export {};
|
|
112
|
+
//# sourceMappingURL=StdioTransport.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"StdioTransport.d.ts","sourceRoot":"","sources":["../../src/transports/StdioTransport.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;GAmBG;AAKH,OAAO,EAAE,YAAY,EAAE,MAAM,QAAQ,CAAC;AAEtC,OAAO,EAAE,SAAS,EAAE,gBAAgB,EAAE,MAAM,gBAAgB,CAAC;AAS7D;;GAEG;AACH,UAAU,qBAAsB,SAAQ,gBAAgB;IACtD;;OAEG;IACH,UAAU,EAAE,MAAM,CAAC;IAEnB;;OAEG;IACH,QAAQ,EAAE,MAAM,CAAC;IAEjB;;OAEG;IACH,QAAQ,CAAC,EAAE;QACT,SAAS,CAAC,EAAE;YACV,SAAS,CAAC,EAAE,MAAM,CAAC;SACpB,CAAC;KACH,CAAC;CACH;AAuKD;;;;;;GAMG;AACH,qBAAa,cAAe,SAAQ,YAAa,YAAW,SAAS;IASvD,OAAO,CAAC,OAAO;IAR3B,OAAO,CAAC,MAAM,CAAa;IAC3B,OAAO,CAAC,OAAO,CAAC,CAAe;IAC/B,OAAO,CAAC,MAAM,CAAC,CAAc;IAC7B,OAAO,CAAC,MAAM,CAAC,CAAc;IAC7B,OAAO,CAAC,SAAS,CAAS;IAC1B,OAAO,CAAC,OAAO,CAA8C;IAC7D,OAAO,CAAC,MAAM,CAAK;gBAEC,OAAO,EAAE,qBAAqB;IAUlD;;;;;OAKG;IACG,OAAO,IAAI,OAAO,CAAC,IAAI,CAAC;IAwE9B;;OAEG;IACH,OAAO,CAAC,aAAa;IAoCrB;;;;OAIG;IACG,MAAM,CAAC,MAAM,EAAE,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