@olane/o-tool 0.6.13 → 0.7.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 +382 -1
- package/dist/src/index.d.ts +2 -5
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +2 -5
- package/dist/src/interfaces/tool.interface.d.ts +2 -2
- package/dist/src/interfaces/tool.interface.d.ts.map +1 -1
- package/dist/src/o-tool.base.d.ts +36 -0
- package/dist/src/o-tool.base.d.ts.map +1 -0
- package/dist/src/o-tool.base.js +207 -0
- package/dist/src/o-tool.d.ts +2 -2
- package/dist/src/o-tool.d.ts.map +1 -1
- package/dist/src/o-tool.js +59 -269
- package/dist/src/router/index.d.ts +3 -0
- package/dist/src/router/index.d.ts.map +1 -0
- package/dist/src/router/index.js +2 -0
- package/dist/src/router/o-request.router.d.ts +15 -0
- package/dist/src/router/o-request.router.d.ts.map +1 -0
- package/dist/src/router/o-request.router.js +10 -0
- package/dist/src/router/o-tool.router.d.ts +6 -0
- package/dist/src/router/o-tool.router.d.ts.map +1 -0
- package/dist/src/router/o-tool.router.js +49 -0
- package/dist/src/router/resolvers/method.resolver.d.ts +8 -0
- package/dist/src/router/resolvers/method.resolver.d.ts.map +1 -0
- package/dist/src/router/resolvers/method.resolver.js +46 -0
- package/dist/src/utils/method.utils.d.ts +7 -0
- package/dist/src/utils/method.utils.d.ts.map +1 -0
- package/dist/src/utils/method.utils.js +26 -0
- package/dist/src/utils/tool.utils.d.ts.map +1 -0
- package/dist/test/method.spec.d.ts +0 -1
- package/dist/test/method.spec.js +29 -28
- package/package.json +4 -4
- package/dist/src/client.tool.d.ts +0 -10
- package/dist/src/client.tool.d.ts.map +0 -1
- package/dist/src/client.tool.js +0 -7
- package/dist/src/nodes/client.node.d.ts +0 -6
- package/dist/src/nodes/client.node.d.ts.map +0 -1
- package/dist/src/nodes/client.node.js +0 -16
- package/dist/src/nodes/index.d.ts +0 -4
- package/dist/src/nodes/index.d.ts.map +0 -1
- package/dist/src/nodes/index.js +0 -3
- package/dist/src/nodes/server.node.d.ts +0 -6
- package/dist/src/nodes/server.node.d.ts.map +0 -1
- package/dist/src/nodes/server.node.js +0 -20
- package/dist/src/nodes/websocket.node.d.ts +0 -6
- package/dist/src/nodes/websocket.node.d.ts.map +0 -1
- package/dist/src/nodes/websocket.node.js +0 -18
- package/dist/src/server.tool.d.ts +0 -10
- package/dist/src/server.tool.d.ts.map +0 -1
- package/dist/src/server.tool.js +0 -7
- package/dist/src/tool.utils.d.ts.map +0 -1
- package/dist/src/virtual.tool.d.ts +0 -10
- package/dist/src/virtual.tool.d.ts.map +0 -1
- package/dist/src/virtual.tool.js +0 -7
- /package/dist/src/{tool.utils.d.ts → utils/tool.utils.d.ts} +0 -0
- /package/dist/src/{tool.utils.js → utils/tool.utils.js} +0 -0
package/dist/src/o-tool.js
CHANGED
|
@@ -1,7 +1,4 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { oProtocolMethods } from '@olane/o-protocol';
|
|
3
|
-
import { ToolUtils } from './tool.utils.js';
|
|
4
|
-
import { v4 as uuidv4 } from 'uuid';
|
|
1
|
+
import { oAddress, oError, oErrorCodes, oRequest, } from '@olane/o-core';
|
|
5
2
|
/**
|
|
6
3
|
* oTool is a mixin that extends the base class and implements the oTool interface
|
|
7
4
|
* @param Base - The base class to extend
|
|
@@ -13,289 +10,82 @@ export function oTool(Base) {
|
|
|
13
10
|
super(...args);
|
|
14
11
|
const config = args[0];
|
|
15
12
|
}
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
if (!this[`_tool_${method}`]) {
|
|
23
|
-
throw new Error(`Tool ${method} is not implemented`);
|
|
24
|
-
}
|
|
25
|
-
return true;
|
|
26
|
-
}
|
|
27
|
-
async handleProtocol(address) {
|
|
28
|
-
this.logger.debug('Handling protocol: ' + address.protocol);
|
|
29
|
-
await this.p2pNode.handle(address.protocol, this.handleStream.bind(this));
|
|
30
|
-
}
|
|
31
|
-
async initialize() {
|
|
32
|
-
await super.initialize();
|
|
33
|
-
await this.handleProtocol(this.address);
|
|
34
|
-
if (this.staticAddress &&
|
|
35
|
-
this.staticAddress?.toString() !== this.address.toString()) {
|
|
36
|
-
await this.handleProtocol(this.staticAddress);
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
async use(address, data) {
|
|
40
|
-
// check if we call ourselves
|
|
41
|
-
if (address.toString() === this.address.toString() ||
|
|
42
|
-
address.toString() === this.staticAddress.toString()) {
|
|
43
|
-
// let's call our own tool
|
|
44
|
-
this.logger.debug('Calling ourselves, skipping...', data);
|
|
45
|
-
const request = new oRequest({
|
|
46
|
-
method: data.method,
|
|
47
|
-
params: {
|
|
48
|
-
_connectionId: 0,
|
|
49
|
-
_requestMethod: data.method,
|
|
50
|
-
...data.params,
|
|
51
|
-
},
|
|
52
|
-
id: 0,
|
|
53
|
-
});
|
|
54
|
-
let success = true;
|
|
55
|
-
const result = await this.execute(request).catch((error) => {
|
|
56
|
-
this.logger.error('Error executing tool: ', error);
|
|
57
|
-
success = false;
|
|
58
|
-
const responseError = error instanceof oToolError
|
|
59
|
-
? error
|
|
60
|
-
: new oToolError(oToolErrorCodes.TOOL_ERROR, error.message);
|
|
61
|
-
return {
|
|
62
|
-
error: responseError.toJSON(),
|
|
63
|
-
};
|
|
64
|
-
});
|
|
65
|
-
if (success) {
|
|
66
|
-
this.successCount++;
|
|
67
|
-
}
|
|
68
|
-
else {
|
|
69
|
-
this.errorCount++;
|
|
70
|
-
}
|
|
71
|
-
return ToolUtils.buildResponse(request, result, result?.error);
|
|
72
|
-
}
|
|
73
|
-
return super.use(address, data);
|
|
74
|
-
}
|
|
75
|
-
async handleStream(streamData) {
|
|
76
|
-
const { stream } = streamData;
|
|
77
|
-
const requestConfig = await CoreUtils.processStream(stream);
|
|
78
|
-
const request = new oRequest(requestConfig);
|
|
79
|
-
let success = true;
|
|
80
|
-
const result = await this.execute(request, stream).catch((error) => {
|
|
81
|
-
this.logger.error('Error executing tool: ', error, typeof error);
|
|
82
|
-
success = false;
|
|
83
|
-
const responseError = error instanceof oToolError
|
|
84
|
-
? error
|
|
85
|
-
: new oToolError(oToolErrorCodes.TOOL_ERROR, error.message);
|
|
86
|
-
return {
|
|
87
|
-
error: responseError.toJSON(),
|
|
88
|
-
};
|
|
89
|
-
});
|
|
90
|
-
if (success) {
|
|
91
|
-
this.successCount++;
|
|
92
|
-
}
|
|
93
|
-
else {
|
|
94
|
-
this.errorCount++;
|
|
95
|
-
}
|
|
96
|
-
// compose the response & add the expected connection + request fields
|
|
97
|
-
const response = ToolUtils.buildResponse(request, result, result?.error);
|
|
98
|
-
// add the request method to the response
|
|
99
|
-
return CoreUtils.sendResponse(response, streamData.stream);
|
|
100
|
-
}
|
|
101
|
-
async execute(req, stream) {
|
|
102
|
-
// const protocolUsageCounter = this.p2pNode.metrics?.registerCounter(
|
|
103
|
-
// 'libp2p_protocol_custom_track',
|
|
104
|
-
// {
|
|
105
|
-
// help: 'Total number of protocol interactions',
|
|
106
|
-
// label: this.address.toString(),
|
|
107
|
-
// },
|
|
108
|
-
// );
|
|
109
|
-
// protocolUsageCounter?.increment();
|
|
110
|
-
let request = req;
|
|
111
|
-
const requestConfig = req.toJSON();
|
|
112
|
-
// validate and run the tool
|
|
113
|
-
this.validateToolCall(request);
|
|
114
|
-
// check if it's a route and we have reached the destination
|
|
115
|
-
if (request.method === oProtocolMethods.ROUTE &&
|
|
116
|
-
request.params.address === this.address.value) {
|
|
117
|
-
const { payload } = request.params;
|
|
118
|
-
request = new oRequest({
|
|
119
|
-
id: requestConfig.id,
|
|
120
|
-
method: payload.method,
|
|
121
|
-
params: {
|
|
122
|
-
_connectionId: requestConfig.params?._connectionId,
|
|
123
|
-
_requestMethod: payload.method,
|
|
124
|
-
...(payload.params || {}), // TODO: is this correct? this line used to be ...payload
|
|
125
|
-
},
|
|
126
|
-
});
|
|
127
|
-
}
|
|
128
|
-
const result = await this.run(request, stream);
|
|
129
|
-
return result;
|
|
13
|
+
async _tool_stop(request) {
|
|
14
|
+
this.logger.debug('Stopping tool: ', request.params);
|
|
15
|
+
this.stop();
|
|
16
|
+
return {
|
|
17
|
+
message: 'Tool stopped',
|
|
18
|
+
};
|
|
130
19
|
}
|
|
131
|
-
async
|
|
132
|
-
|
|
133
|
-
// const processedParams = this.processRunInputs(params);
|
|
134
|
-
const missingParams = this.findMissingParams(request.method, request.params || {});
|
|
135
|
-
if (missingParams.length > 0) {
|
|
136
|
-
this.logger.error('Missing required parameters: ', missingParams, ' with passed params: ', request.params);
|
|
137
|
-
throw new oToolError(oToolErrorCodes.TOOL_MISSING_PARAMETERS, 'Missing required parameters', {
|
|
138
|
-
parameters: missingParams,
|
|
139
|
-
toolAddress: this.address.toString(),
|
|
140
|
-
data: request.params,
|
|
141
|
-
});
|
|
142
|
-
}
|
|
143
|
-
// resolve o:// addresses
|
|
144
|
-
// THIS HAS A RECURSIVE CALL issue
|
|
145
|
-
this.logger.debug('Calling function at address: ', this.address.toString(), 'with request: ', request.method);
|
|
146
|
-
// const isPlaceholder = this.address.toString().includes('placeholder');
|
|
147
|
-
// request = await oRequest.translateToRawRequest(request, this);
|
|
148
|
-
let result = await this.callMyTool(request, stream);
|
|
149
|
-
// result = await oRequest.translateResultForAgent(result, this);
|
|
150
|
-
return result;
|
|
151
|
-
// translate to o:// addresses
|
|
20
|
+
async _tool_handshake(handshake) {
|
|
21
|
+
throw new oError(oErrorCodes.NOT_IMPLEMENTED, 'Handshake not implemented');
|
|
152
22
|
}
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
23
|
+
/**
|
|
24
|
+
* Where all intents go to be resolved.
|
|
25
|
+
* @param request
|
|
26
|
+
* @returns
|
|
27
|
+
*/
|
|
28
|
+
async _tool_intent(request) {
|
|
29
|
+
throw new oError(oErrorCodes.NOT_IMPLEMENTED, 'Intent not implemented');
|
|
160
30
|
}
|
|
161
|
-
async
|
|
162
|
-
const { requestId } = request.params;
|
|
163
|
-
delete this.requests[requestId];
|
|
31
|
+
async _tool_hello_world(request) {
|
|
164
32
|
return {
|
|
165
|
-
message: '
|
|
33
|
+
message: 'Hello, world!',
|
|
166
34
|
};
|
|
167
35
|
}
|
|
168
|
-
async
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
// @ts-ignore
|
|
173
|
-
const result = await this[`_tool_${method}`]({
|
|
174
|
-
...request.toJSON(),
|
|
175
|
-
stream,
|
|
176
|
-
}).catch((error) => {
|
|
177
|
-
delete this.requests[request.id];
|
|
178
|
-
throw error;
|
|
179
|
-
});
|
|
180
|
-
delete this.requests[request.id];
|
|
181
|
-
return result;
|
|
182
|
-
}
|
|
183
|
-
async index() {
|
|
184
|
-
const metadata = await this.whoami();
|
|
185
|
-
if (!metadata.tools.length && !metadata.description) {
|
|
186
|
-
this.logger.warn('No metadata found, skipping...');
|
|
187
|
-
return {
|
|
188
|
-
provider: 'Empty node',
|
|
189
|
-
summary: 'Empty node',
|
|
190
|
-
};
|
|
191
|
-
}
|
|
192
|
-
for (const method in this.methods) {
|
|
193
|
-
const m = this.methods[method];
|
|
194
|
-
await this.use(new oAddress('o://vector-store'), {
|
|
195
|
-
method: 'add_documents',
|
|
196
|
-
params: {
|
|
197
|
-
documents: [
|
|
198
|
-
{
|
|
199
|
-
pageContent: m.description,
|
|
200
|
-
metadata: {
|
|
201
|
-
address: this.address?.toString() + '/' + method,
|
|
202
|
-
id: uuidv4(),
|
|
203
|
-
},
|
|
204
|
-
},
|
|
205
|
-
],
|
|
206
|
-
},
|
|
207
|
-
});
|
|
208
|
-
}
|
|
209
|
-
let summary = metadata.description ? metadata.description : null;
|
|
210
|
-
if (!summary) {
|
|
211
|
-
this.logger.debug('No description found, generating summary...');
|
|
212
|
-
const response = await this.use(new oAddress('o://intelligence'), {
|
|
213
|
-
method: 'prompt',
|
|
214
|
-
params: {
|
|
215
|
-
prompt: `You are a helpful assistant that summarizes what a service does by looking at the service description and the details of the tools that the service contains. \n
|
|
216
|
-
Format the output in JSON using this template:` +
|
|
217
|
-
JSON.stringify({
|
|
218
|
-
summary: 'string',
|
|
219
|
-
}) +
|
|
220
|
-
'Do NOT include any other text other than the JSON response. The following is the JSON input of the service: ' +
|
|
221
|
-
JSON.stringify(metadata),
|
|
222
|
-
},
|
|
223
|
-
});
|
|
224
|
-
const { result } = response;
|
|
225
|
-
const { success } = result;
|
|
226
|
-
if (!success) {
|
|
227
|
-
this.logger.error('Failed to index network: ', result);
|
|
228
|
-
throw new Error('Failed to index network');
|
|
229
|
-
}
|
|
230
|
-
const data = result.data;
|
|
231
|
-
const { message } = data;
|
|
232
|
-
const json = JSON.parse(message);
|
|
233
|
-
summary = json.summary;
|
|
234
|
-
}
|
|
36
|
+
async _tool_index_network(request) {
|
|
37
|
+
this.logger.debug('Indexing network...');
|
|
38
|
+
// collect all the information from the child nodes
|
|
39
|
+
let result = {};
|
|
235
40
|
try {
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
metadata: {
|
|
244
|
-
address: this.address?.toString(),
|
|
245
|
-
id: uuidv4(),
|
|
246
|
-
},
|
|
247
|
-
},
|
|
248
|
-
],
|
|
249
|
-
},
|
|
41
|
+
result = await this.index();
|
|
42
|
+
// index children
|
|
43
|
+
const children = this.hierarchyManager.getChildren();
|
|
44
|
+
for (const child of children) {
|
|
45
|
+
await this.useChild(child, {
|
|
46
|
+
method: 'index_network',
|
|
47
|
+
params: {},
|
|
250
48
|
});
|
|
251
49
|
}
|
|
252
|
-
|
|
253
|
-
summary: summary,
|
|
254
|
-
};
|
|
50
|
+
this.logger.debug('Node + children indexed!');
|
|
255
51
|
}
|
|
256
|
-
catch (
|
|
257
|
-
this.logger.error('
|
|
258
|
-
throw
|
|
52
|
+
catch (error) {
|
|
53
|
+
this.logger.error('Failed to index node:', error);
|
|
54
|
+
throw error;
|
|
259
55
|
}
|
|
56
|
+
return result;
|
|
260
57
|
}
|
|
261
|
-
async
|
|
262
|
-
this.
|
|
263
|
-
|
|
264
|
-
|
|
58
|
+
async _tool_route(request) {
|
|
59
|
+
if (request.params.address === this.address.toString() ||
|
|
60
|
+
request.params.address === this.staticAddress.toString()) {
|
|
61
|
+
this.logger.debug('Route to self, calling tool...');
|
|
62
|
+
const { payload } = request.params;
|
|
63
|
+
return this.callMyTool(new oRequest({
|
|
64
|
+
method: payload.method,
|
|
65
|
+
params: payload.params,
|
|
66
|
+
id: request.id,
|
|
67
|
+
}), request.stream);
|
|
68
|
+
}
|
|
69
|
+
return this.router.route(request, this);
|
|
70
|
+
}
|
|
71
|
+
async _tool_add_child(request) {
|
|
72
|
+
throw new oError(oErrorCodes.NOT_IMPLEMENTED, 'Add child not implemented');
|
|
265
73
|
}
|
|
266
|
-
async
|
|
267
|
-
const
|
|
74
|
+
async _tool_child_register(request) {
|
|
75
|
+
const { address } = request.params;
|
|
76
|
+
const childAddress = new oAddress(address);
|
|
77
|
+
this.hierarchyManager.addChild(childAddress);
|
|
268
78
|
return {
|
|
269
|
-
|
|
270
|
-
tools: this.myTools(),
|
|
271
|
-
description: this.description,
|
|
79
|
+
message: 'Child node registered with parent!',
|
|
272
80
|
};
|
|
273
81
|
}
|
|
274
|
-
|
|
82
|
+
// TODO: implement this
|
|
83
|
+
async _tool_cancel_request(request) {
|
|
84
|
+
const { requestId } = request.params;
|
|
85
|
+
// delete this.requestManager.remove(requestId as string);
|
|
275
86
|
return {
|
|
276
|
-
message: '
|
|
87
|
+
message: 'Request cancelled',
|
|
277
88
|
};
|
|
278
89
|
}
|
|
279
|
-
// ensure that the required parameters are present
|
|
280
|
-
findMissingParams(methodName, params) {
|
|
281
|
-
const method = this.methods[methodName];
|
|
282
|
-
const protectedMethods = Object.values(oProtocolMethods);
|
|
283
|
-
if (protectedMethods.includes(methodName)) {
|
|
284
|
-
return [];
|
|
285
|
-
}
|
|
286
|
-
if (!method) {
|
|
287
|
-
this.logger.warn('No parameter configuration found for method. This is expected for some methods, but may impact AI performance around improvisation.', methodName);
|
|
288
|
-
return [];
|
|
289
|
-
}
|
|
290
|
-
const requiredParams = method.parameters;
|
|
291
|
-
if (!requiredParams || !requiredParams.filter) {
|
|
292
|
-
this.logger.error('[Provider] No handshake parameters found for method: ', methodName);
|
|
293
|
-
return [];
|
|
294
|
-
}
|
|
295
|
-
const missingParams = requiredParams
|
|
296
|
-
.filter((p) => !params[p.name])
|
|
297
|
-
.filter((p) => p.required === undefined || p.required === true);
|
|
298
|
-
return missingParams;
|
|
299
|
-
}
|
|
300
90
|
};
|
|
301
91
|
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/router/index.ts"],"names":[],"mappings":"AAAA,cAAc,oBAAoB,CAAC;AACnC,cAAc,gCAAgC,CAAC"}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { oProtocolMethods, oRouterRequest as oRouterRequestInterface, RequestParams } from '@olane/o-protocol';
|
|
2
|
+
import { oRequest } from '@olane/o-core';
|
|
3
|
+
import { Stream } from '@olane/o-config';
|
|
4
|
+
export declare class oRouterRequest extends oRequest implements oRouterRequestInterface {
|
|
5
|
+
method: oProtocolMethods.ROUTE;
|
|
6
|
+
stream?: Stream | undefined;
|
|
7
|
+
params: RequestParams & {
|
|
8
|
+
address: string;
|
|
9
|
+
payload: {
|
|
10
|
+
[key: string]: unknown;
|
|
11
|
+
};
|
|
12
|
+
};
|
|
13
|
+
constructor(config: oRouterRequestInterface);
|
|
14
|
+
}
|
|
15
|
+
//# sourceMappingURL=o-request.router.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"o-request.router.d.ts","sourceRoot":"","sources":["../../../src/router/o-request.router.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,gBAAgB,EAChB,cAAc,IAAI,uBAAuB,EACzC,aAAa,EACd,MAAM,mBAAmB,CAAC;AAC3B,OAAO,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AACzC,OAAO,EAAE,MAAM,EAAE,MAAM,iBAAiB,CAAC;AAEzC,qBAAa,cACX,SAAQ,QACR,YAAW,uBAAuB;IAElC,MAAM,EAAE,gBAAgB,CAAC,KAAK,CAAC;IAC/B,MAAM,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;IAC5B,MAAM,EAAE,aAAa,GAAG;QACtB,OAAO,EAAE,MAAM,CAAC;QAChB,OAAO,EAAE;YACP,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC;SACxB,CAAC;KACH,CAAC;gBACU,MAAM,EAAE,uBAAuB;CAO5C"}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { oAddress, oCore, oRequest, oRouter, oRouterRequest } from '@olane/o-core';
|
|
2
|
+
export declare abstract class oToolRouter extends oRouter {
|
|
3
|
+
protected abstract forward(address: oAddress, request: oRequest | oRouterRequest, node?: oCore): Promise<any>;
|
|
4
|
+
route(request: oRouterRequest, node: oCore): Promise<any>;
|
|
5
|
+
}
|
|
6
|
+
//# sourceMappingURL=o-tool.router.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"o-tool.router.d.ts","sourceRoot":"","sources":["../../../src/router/o-tool.router.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,QAAQ,EACR,KAAK,EACL,QAAQ,EACR,OAAO,EACP,cAAc,EACf,MAAM,eAAe,CAAC;AAGvB,8BAAsB,WAAY,SAAQ,OAAO;IAC/C,SAAS,CAAC,QAAQ,CAAC,OAAO,CACxB,OAAO,EAAE,QAAQ,EACjB,OAAO,EAAE,QAAQ,GAAG,cAAc,EAClC,IAAI,CAAC,EAAE,KAAK,GACX,OAAO,CAAC,GAAG,CAAC;IAET,KAAK,CAAC,OAAO,EAAE,cAAc,EAAE,IAAI,EAAE,KAAK,GAAG,OAAO,CAAC,GAAG,CAAC;CA+ChE"}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { oAddress, oRouter, oRouterRequest, } from '@olane/o-core';
|
|
2
|
+
import { JSONRPC_VERSION, oProtocolMethods } from '@olane/o-protocol';
|
|
3
|
+
export class oToolRouter extends oRouter {
|
|
4
|
+
async route(request, node) {
|
|
5
|
+
const { payload } = request.params;
|
|
6
|
+
const { address } = request.params;
|
|
7
|
+
const { method } = payload;
|
|
8
|
+
const isHandshake = method === oProtocolMethods.HANDSHAKE;
|
|
9
|
+
const destinationAddress = new oAddress(address);
|
|
10
|
+
// determine the next hop address from the encapsulated address
|
|
11
|
+
const { nextHopAddress, targetAddress, requestOverride } = await this.translate(destinationAddress, node);
|
|
12
|
+
const finalRequest = requestOverride || request;
|
|
13
|
+
const req = new oRouterRequest({
|
|
14
|
+
method: finalRequest.method,
|
|
15
|
+
params: finalRequest.params,
|
|
16
|
+
id: finalRequest.id,
|
|
17
|
+
jsonrpc: JSONRPC_VERSION,
|
|
18
|
+
stream: request.stream,
|
|
19
|
+
});
|
|
20
|
+
if (finalRequest && targetAddress) {
|
|
21
|
+
finalRequest.params.address = targetAddress.toString();
|
|
22
|
+
}
|
|
23
|
+
const params = req.params.payload.params;
|
|
24
|
+
// override the method if it is a handshake
|
|
25
|
+
if (isHandshake) {
|
|
26
|
+
try {
|
|
27
|
+
if (requestOverride) {
|
|
28
|
+
// this is likely a method resolver, so we need to override the method
|
|
29
|
+
// let's specify the method in the request params to optimize on context window
|
|
30
|
+
params.tool = req.params?.payload?.method;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
catch (e) {
|
|
34
|
+
this.logger.error('Error assigning tool to handshake: ', e);
|
|
35
|
+
}
|
|
36
|
+
// update the method to be the handshake
|
|
37
|
+
req.params.payload.method = method;
|
|
38
|
+
}
|
|
39
|
+
else if (requestOverride) {
|
|
40
|
+
// method resolved
|
|
41
|
+
req.params.payload.params = {
|
|
42
|
+
...payload.params, // initial params
|
|
43
|
+
...params, // overloaded params
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
// TODO: send the request to the destination
|
|
47
|
+
return this.forward(nextHopAddress, req, node);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { oAddress, oAddressResolver, oTransport, ResolveRequest, RouteResponse } from '@olane/o-core';
|
|
2
|
+
export declare class oMethodResolver extends oAddressResolver {
|
|
3
|
+
protected readonly address: oAddress;
|
|
4
|
+
constructor(address: oAddress);
|
|
5
|
+
get customTransports(): oTransport[];
|
|
6
|
+
resolve(request: ResolveRequest): Promise<RouteResponse>;
|
|
7
|
+
}
|
|
8
|
+
//# sourceMappingURL=method.resolver.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"method.resolver.d.ts","sourceRoot":"","sources":["../../../../src/router/resolvers/method.resolver.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,QAAQ,EACR,gBAAgB,EAIhB,UAAU,EACV,cAAc,EACd,aAAa,EACd,MAAM,eAAe,CAAC;AAKvB,qBAAa,eAAgB,SAAQ,gBAAgB;IACvC,SAAS,CAAC,QAAQ,CAAC,OAAO,EAAE,QAAQ;gBAAjB,OAAO,EAAE,QAAQ;IAIhD,IAAI,gBAAgB,IAAI,UAAU,EAAE,CAEnC;IAEK,OAAO,CAAC,OAAO,EAAE,cAAc,GAAG,OAAO,CAAC,aAAa,CAAC;CAkC/D"}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { oAddress, oAddressResolver, oCustomTransport, oRouterRequest, } from '@olane/o-core';
|
|
2
|
+
import { JSONRPC_VERSION, oProtocolMethods } from '@olane/o-protocol';
|
|
3
|
+
import { v4 as uuidv4 } from 'uuid';
|
|
4
|
+
export class oMethodResolver extends oAddressResolver {
|
|
5
|
+
constructor(address) {
|
|
6
|
+
super(address);
|
|
7
|
+
this.address = address;
|
|
8
|
+
}
|
|
9
|
+
get customTransports() {
|
|
10
|
+
return [new oCustomTransport('/method')];
|
|
11
|
+
}
|
|
12
|
+
async resolve(request) {
|
|
13
|
+
const { address, node, request: resolveRequest, targetAddress } = request;
|
|
14
|
+
const nextHopAddress = oAddress.next(node?.address, address);
|
|
15
|
+
const method = nextHopAddress.protocol.split('/').pop();
|
|
16
|
+
if (method) {
|
|
17
|
+
const methodName = await node.findMethod(method);
|
|
18
|
+
if (methodName) {
|
|
19
|
+
const req = new oRouterRequest({
|
|
20
|
+
method: oProtocolMethods.ROUTE,
|
|
21
|
+
params: {
|
|
22
|
+
_connectionId: '',
|
|
23
|
+
_requestMethod: oProtocolMethods.ROUTE,
|
|
24
|
+
address: node.address.toString(),
|
|
25
|
+
payload: {
|
|
26
|
+
method: methodName,
|
|
27
|
+
params: {},
|
|
28
|
+
},
|
|
29
|
+
},
|
|
30
|
+
jsonrpc: JSONRPC_VERSION,
|
|
31
|
+
id: uuidv4(),
|
|
32
|
+
});
|
|
33
|
+
return {
|
|
34
|
+
nextHopAddress: node.address,
|
|
35
|
+
targetAddress: node.address,
|
|
36
|
+
requestOverride: req,
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
return {
|
|
41
|
+
nextHopAddress: address,
|
|
42
|
+
targetAddress: targetAddress,
|
|
43
|
+
requestOverride: resolveRequest,
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { oAddress, oParameter } from '@olane/o-core';
|
|
2
|
+
import type { oToolBase } from '../o-tool.base';
|
|
3
|
+
export declare class MethodUtils {
|
|
4
|
+
static getMethod(address: oAddress): string;
|
|
5
|
+
static findMissingParams(tool: oToolBase, methodName: string, params: any): oParameter[];
|
|
6
|
+
}
|
|
7
|
+
//# sourceMappingURL=method.utils.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"method.utils.d.ts","sourceRoot":"","sources":["../../../src/utils/method.utils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,UAAU,EAAE,MAAM,eAAe,CAAC;AACrD,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAGhD,qBAAa,WAAW;IACtB,MAAM,CAAC,SAAS,CAAC,OAAO,EAAE,QAAQ,GAAG,MAAM;IAI3C,MAAM,CAAC,iBAAiB,CACtB,IAAI,EAAE,SAAS,EACf,UAAU,EAAE,MAAM,EAClB,MAAM,EAAE,GAAG,GACV,UAAU,EAAE;CA0BhB"}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { oProtocolMethods } from '@olane/o-protocol';
|
|
2
|
+
export class MethodUtils {
|
|
3
|
+
static getMethod(address) {
|
|
4
|
+
return address.protocol.split('/').pop() || '';
|
|
5
|
+
}
|
|
6
|
+
static findMissingParams(tool, methodName, params) {
|
|
7
|
+
const method = tool.methods[methodName];
|
|
8
|
+
const protectedMethods = Object.values(oProtocolMethods);
|
|
9
|
+
if (protectedMethods.includes(methodName)) {
|
|
10
|
+
return [];
|
|
11
|
+
}
|
|
12
|
+
if (!method) {
|
|
13
|
+
tool.logger.warn('No parameter configuration found for method. This is expected for some methods, but may impact AI performance around improvisation.', methodName);
|
|
14
|
+
return [];
|
|
15
|
+
}
|
|
16
|
+
const requiredParams = method.parameters;
|
|
17
|
+
if (!requiredParams || !requiredParams.filter) {
|
|
18
|
+
tool.logger.error('[Provider] No handshake parameters found for method: ', methodName);
|
|
19
|
+
return [];
|
|
20
|
+
}
|
|
21
|
+
const missingParams = requiredParams
|
|
22
|
+
.filter((p) => !params[p.name])
|
|
23
|
+
.filter((p) => p.required === undefined || p.required === true);
|
|
24
|
+
return missingParams;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tool.utils.d.ts","sourceRoot":"","sources":["../../../src/utils/tool.utils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,eAAe,CAAC;AAC1C,OAAO,EAAE,QAAQ,EAAE,MAAM,mBAAmB,CAAC;AAE7C,qBAAa,SAAS;IACpB,MAAM,CAAC,aAAa,CAAC,OAAO,EAAE,QAAQ,EAAE,MAAM,EAAE,GAAG,EAAE,KAAK,EAAE,GAAG,GAAG,SAAS;CAc5E"}
|
package/dist/test/method.spec.js
CHANGED
|
@@ -1,28 +1,29 @@
|
|
|
1
|
-
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
});
|
|
1
|
+
"use strict";
|
|
2
|
+
// import { oAddress, NodeState, oRequest } from '@olane/o-core';
|
|
3
|
+
// import { oVirtualTool } from '../src/virtual.tool.js';
|
|
4
|
+
// import { expect } from 'chai';
|
|
5
|
+
// describe('o-tool @methods', () => {
|
|
6
|
+
// it('should call the hello_world method', async () => {
|
|
7
|
+
// const node = new oVirtualTool({
|
|
8
|
+
// address: new oAddress('o://test'),
|
|
9
|
+
// leader: null,
|
|
10
|
+
// parent: null,
|
|
11
|
+
// });
|
|
12
|
+
// await node.start();
|
|
13
|
+
// expect(node.state).to.equal(NodeState.RUNNING);
|
|
14
|
+
// // call the tool
|
|
15
|
+
// const req = new oRequest({
|
|
16
|
+
// method: 'hello_world',
|
|
17
|
+
// id: '123',
|
|
18
|
+
// params: {
|
|
19
|
+
// _connectionId: '123',
|
|
20
|
+
// _requestMethod: 'hello_world',
|
|
21
|
+
// },
|
|
22
|
+
// });
|
|
23
|
+
// const data = await node.callMyTool(req);
|
|
24
|
+
// expect(data.message).to.equal('Hello, world!');
|
|
25
|
+
// // stop the node
|
|
26
|
+
// await node.stop();
|
|
27
|
+
// expect(node.state).to.equal(NodeState.STOPPED);
|
|
28
|
+
// });
|
|
29
|
+
// });
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@olane/o-tool",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.7.2",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "dist/src/index.js",
|
|
6
6
|
"types": "dist/src/index.d.ts",
|
|
@@ -55,9 +55,9 @@
|
|
|
55
55
|
"typescript": "^5.8.3"
|
|
56
56
|
},
|
|
57
57
|
"peerDependencies": {
|
|
58
|
-
"@olane/o-config": "^0.
|
|
59
|
-
"@olane/o-core": "^0.
|
|
60
|
-
"@olane/o-protocol": "^0.
|
|
58
|
+
"@olane/o-config": "^0.7.1",
|
|
59
|
+
"@olane/o-core": "^0.7.1",
|
|
60
|
+
"@olane/o-protocol": "^0.7.1"
|
|
61
61
|
},
|
|
62
62
|
"dependencies": {
|
|
63
63
|
"debug": "^4.4.1",
|