@nirvana-labs/nirvana-mcp 1.26.1 → 1.26.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/code-tool.d.mts +44 -2
- package/code-tool.d.mts.map +1 -1
- package/code-tool.d.ts +44 -2
- package/code-tool.d.ts.map +1 -1
- package/code-tool.js +22 -153
- package/code-tool.js.map +1 -1
- package/code-tool.mjs +22 -117
- package/code-tool.mjs.map +1 -1
- package/docs-search-tool.d.mts.map +1 -1
- package/docs-search-tool.d.ts.map +1 -1
- package/docs-search-tool.js +3 -0
- package/docs-search-tool.js.map +1 -1
- package/docs-search-tool.mjs +3 -0
- package/docs-search-tool.mjs.map +1 -1
- package/package.json +2 -18
- package/server.js +1 -1
- package/server.mjs +1 -1
- package/src/code-tool.ts +32 -148
- package/src/docs-search-tool.ts +7 -0
- package/src/server.ts +1 -1
- package/code-tool-paths.cjs +0 -6
- package/code-tool-paths.cjs.map +0 -1
- package/code-tool-paths.d.cts +0 -2
- package/code-tool-paths.d.cts.map +0 -1
- package/code-tool-worker.d.mts +0 -5
- package/code-tool-worker.d.mts.map +0 -1
- package/code-tool-worker.d.ts +0 -5
- package/code-tool-worker.d.ts.map +0 -1
- package/code-tool-worker.js +0 -329
- package/code-tool-worker.js.map +0 -1
- package/code-tool-worker.mjs +0 -324
- package/code-tool-worker.mjs.map +0 -1
- package/src/code-tool-paths.cts +0 -3
- package/src/code-tool-worker.ts +0 -377
package/src/code-tool-worker.ts
DELETED
|
@@ -1,377 +0,0 @@
|
|
|
1
|
-
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
|
|
2
|
-
|
|
3
|
-
import path from 'node:path';
|
|
4
|
-
import util from 'node:util';
|
|
5
|
-
|
|
6
|
-
import Fuse from 'fuse.js';
|
|
7
|
-
import ts from 'typescript';
|
|
8
|
-
|
|
9
|
-
import { WorkerInput, WorkerSuccess, WorkerError } from './code-tool-types';
|
|
10
|
-
import { NirvanaLabs } from '@nirvana-labs/nirvana';
|
|
11
|
-
|
|
12
|
-
function getRunFunctionSource(code: string): {
|
|
13
|
-
type: 'declaration' | 'expression';
|
|
14
|
-
client: string | undefined;
|
|
15
|
-
code: string;
|
|
16
|
-
} | null {
|
|
17
|
-
const sourceFile = ts.createSourceFile('code.ts', code, ts.ScriptTarget.Latest, true);
|
|
18
|
-
const printer = ts.createPrinter();
|
|
19
|
-
|
|
20
|
-
for (const statement of sourceFile.statements) {
|
|
21
|
-
// Check for top-level function declarations
|
|
22
|
-
if (ts.isFunctionDeclaration(statement)) {
|
|
23
|
-
if (statement.name?.text === 'run') {
|
|
24
|
-
return {
|
|
25
|
-
type: 'declaration',
|
|
26
|
-
client: statement.parameters[0]?.name.getText(),
|
|
27
|
-
code: printer.printNode(ts.EmitHint.Unspecified, statement.body!, sourceFile),
|
|
28
|
-
};
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
// Check for variable declarations: const run = () => {} or const run = function() {}
|
|
33
|
-
if (ts.isVariableStatement(statement)) {
|
|
34
|
-
for (const declaration of statement.declarationList.declarations) {
|
|
35
|
-
if (
|
|
36
|
-
ts.isIdentifier(declaration.name) &&
|
|
37
|
-
declaration.name.text === 'run' &&
|
|
38
|
-
// Check if it's initialized with a function
|
|
39
|
-
declaration.initializer &&
|
|
40
|
-
(ts.isFunctionExpression(declaration.initializer) || ts.isArrowFunction(declaration.initializer))
|
|
41
|
-
) {
|
|
42
|
-
return {
|
|
43
|
-
type: 'expression',
|
|
44
|
-
client: declaration.initializer.parameters[0]?.name.getText(),
|
|
45
|
-
code: printer.printNode(ts.EmitHint.Unspecified, declaration.initializer, sourceFile),
|
|
46
|
-
};
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
return null;
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
function getTSDiagnostics(code: string): string[] {
|
|
56
|
-
const functionSource = getRunFunctionSource(code)!;
|
|
57
|
-
const codeWithImport = [
|
|
58
|
-
'import { NirvanaLabs } from "@nirvana-labs/nirvana";',
|
|
59
|
-
functionSource.type === 'declaration' ?
|
|
60
|
-
`async function run(${functionSource.client}: NirvanaLabs)`
|
|
61
|
-
: `const run: (${functionSource.client}: NirvanaLabs) => Promise<unknown> =`,
|
|
62
|
-
functionSource.code,
|
|
63
|
-
].join('\n');
|
|
64
|
-
const sourcePath = path.resolve('code.ts');
|
|
65
|
-
const ast = ts.createSourceFile(sourcePath, codeWithImport, ts.ScriptTarget.Latest, true);
|
|
66
|
-
const options = ts.getDefaultCompilerOptions();
|
|
67
|
-
options.target = ts.ScriptTarget.Latest;
|
|
68
|
-
options.module = ts.ModuleKind.NodeNext;
|
|
69
|
-
options.moduleResolution = ts.ModuleResolutionKind.NodeNext;
|
|
70
|
-
const host = ts.createCompilerHost(options, true);
|
|
71
|
-
const newHost: typeof host = {
|
|
72
|
-
...host,
|
|
73
|
-
getSourceFile: (...args) => {
|
|
74
|
-
if (path.resolve(args[0]) === sourcePath) {
|
|
75
|
-
return ast;
|
|
76
|
-
}
|
|
77
|
-
return host.getSourceFile(...args);
|
|
78
|
-
},
|
|
79
|
-
readFile: (...args) => {
|
|
80
|
-
if (path.resolve(args[0]) === sourcePath) {
|
|
81
|
-
return codeWithImport;
|
|
82
|
-
}
|
|
83
|
-
return host.readFile(...args);
|
|
84
|
-
},
|
|
85
|
-
fileExists: (...args) => {
|
|
86
|
-
if (path.resolve(args[0]) === sourcePath) {
|
|
87
|
-
return true;
|
|
88
|
-
}
|
|
89
|
-
return host.fileExists(...args);
|
|
90
|
-
},
|
|
91
|
-
};
|
|
92
|
-
const program = ts.createProgram({
|
|
93
|
-
options,
|
|
94
|
-
rootNames: [sourcePath],
|
|
95
|
-
host: newHost,
|
|
96
|
-
});
|
|
97
|
-
const diagnostics = ts.getPreEmitDiagnostics(program, ast);
|
|
98
|
-
return diagnostics.map((d) => {
|
|
99
|
-
const message = ts.flattenDiagnosticMessageText(d.messageText, '\n');
|
|
100
|
-
if (!d.file || !d.start) return `- ${message}`;
|
|
101
|
-
const { line: tsLine } = ts.getLineAndCharacterOfPosition(d.file, d.start);
|
|
102
|
-
// We add two lines in the beginning, for the client import and the function declaration.
|
|
103
|
-
// So the actual (zero-based) line number is tsLine - 2.
|
|
104
|
-
const lineNumber = tsLine - 2;
|
|
105
|
-
const line = code.split('\n').at(lineNumber)?.trim();
|
|
106
|
-
return line ? `- ${message}\n at line ${lineNumber + 1}\n ${line}` : `- ${message}`;
|
|
107
|
-
});
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
const fuse = new Fuse(
|
|
111
|
-
[
|
|
112
|
-
'client.user.get',
|
|
113
|
-
'client.apiKeys.create',
|
|
114
|
-
'client.apiKeys.delete',
|
|
115
|
-
'client.apiKeys.get',
|
|
116
|
-
'client.apiKeys.list',
|
|
117
|
-
'client.apiKeys.update',
|
|
118
|
-
'client.operations.get',
|
|
119
|
-
'client.operations.list',
|
|
120
|
-
'client.projects.create',
|
|
121
|
-
'client.projects.delete',
|
|
122
|
-
'client.projects.get',
|
|
123
|
-
'client.projects.list',
|
|
124
|
-
'client.projects.update',
|
|
125
|
-
'client.compute.vms.create',
|
|
126
|
-
'client.compute.vms.delete',
|
|
127
|
-
'client.compute.vms.get',
|
|
128
|
-
'client.compute.vms.list',
|
|
129
|
-
'client.compute.vms.restart',
|
|
130
|
-
'client.compute.vms.update',
|
|
131
|
-
'client.compute.vms.availability.create',
|
|
132
|
-
'client.compute.vms.availability.update',
|
|
133
|
-
'client.compute.vms.volumes.list',
|
|
134
|
-
'client.compute.vms.osImages.list',
|
|
135
|
-
'client.compute.volumes.create',
|
|
136
|
-
'client.compute.volumes.delete',
|
|
137
|
-
'client.compute.volumes.get',
|
|
138
|
-
'client.compute.volumes.list',
|
|
139
|
-
'client.compute.volumes.update',
|
|
140
|
-
'client.compute.volumes.availability.create',
|
|
141
|
-
'client.compute.volumes.availability.update',
|
|
142
|
-
'client.networking.vpcs.create',
|
|
143
|
-
'client.networking.vpcs.delete',
|
|
144
|
-
'client.networking.vpcs.get',
|
|
145
|
-
'client.networking.vpcs.list',
|
|
146
|
-
'client.networking.vpcs.update',
|
|
147
|
-
'client.networking.vpcs.availability.create',
|
|
148
|
-
'client.networking.vpcs.availability.update',
|
|
149
|
-
'client.networking.firewallRules.create',
|
|
150
|
-
'client.networking.firewallRules.delete',
|
|
151
|
-
'client.networking.firewallRules.get',
|
|
152
|
-
'client.networking.firewallRules.list',
|
|
153
|
-
'client.networking.firewallRules.update',
|
|
154
|
-
'client.networking.connect.connections.create',
|
|
155
|
-
'client.networking.connect.connections.delete',
|
|
156
|
-
'client.networking.connect.connections.get',
|
|
157
|
-
'client.networking.connect.connections.list',
|
|
158
|
-
'client.networking.connect.connections.update',
|
|
159
|
-
'client.networking.connect.routes.list',
|
|
160
|
-
'client.rpcNodes.flex.create',
|
|
161
|
-
'client.rpcNodes.flex.delete',
|
|
162
|
-
'client.rpcNodes.flex.get',
|
|
163
|
-
'client.rpcNodes.flex.list',
|
|
164
|
-
'client.rpcNodes.flex.update',
|
|
165
|
-
'client.rpcNodes.flex.blockchains.list',
|
|
166
|
-
'client.rpcNodes.dedicated.get',
|
|
167
|
-
'client.rpcNodes.dedicated.list',
|
|
168
|
-
'client.rpcNodes.dedicated.blockchains.list',
|
|
169
|
-
'client.vektor.registry.assets.list',
|
|
170
|
-
'client.vektor.registry.blockchains.list',
|
|
171
|
-
'client.vektor.registry.venues.list',
|
|
172
|
-
'client.vektor.registry.errors.list',
|
|
173
|
-
'client.vektor.registry.lendMarkets.list',
|
|
174
|
-
'client.vektor.registry.borrowMarkets.list',
|
|
175
|
-
'client.vektor.registry.lpPools.list',
|
|
176
|
-
'client.vektor.balances.list',
|
|
177
|
-
'client.vektor.balances.listHistorical',
|
|
178
|
-
'client.vektor.prices.list',
|
|
179
|
-
'client.vektor.prices.listHistorical',
|
|
180
|
-
'client.vektor.lend.markets.list',
|
|
181
|
-
'client.vektor.lend.markets.listHistorical',
|
|
182
|
-
'client.vektor.lend.positions.list',
|
|
183
|
-
'client.vektor.lend.positions.listHistorical',
|
|
184
|
-
'client.vektor.lend.lend.create',
|
|
185
|
-
'client.vektor.lend.withdraw.create',
|
|
186
|
-
'client.vektor.lend.setCollateral.create',
|
|
187
|
-
'client.vektor.borrow.markets.list',
|
|
188
|
-
'client.vektor.borrow.markets.listHistorical',
|
|
189
|
-
'client.vektor.borrow.positions.list',
|
|
190
|
-
'client.vektor.borrow.positions.listHistorical',
|
|
191
|
-
'client.vektor.borrow.accounts.list',
|
|
192
|
-
'client.vektor.borrow.accounts.listHistorical',
|
|
193
|
-
'client.vektor.borrow.borrow.create',
|
|
194
|
-
'client.vektor.borrow.repay.create',
|
|
195
|
-
'client.vektor.lp.pools.list',
|
|
196
|
-
'client.vektor.lp.pools.listHistorical',
|
|
197
|
-
'client.vektor.lp.positions.list',
|
|
198
|
-
'client.vektor.lp.positions.listHistorical',
|
|
199
|
-
'client.vektor.lp.depositQuote.create',
|
|
200
|
-
'client.vektor.lp.withdrawQuote.create',
|
|
201
|
-
'client.vektor.buy.quotes.list',
|
|
202
|
-
'client.vektor.buy.buy.create',
|
|
203
|
-
'client.vektor.sell.quotes.list',
|
|
204
|
-
'client.vektor.sell.sell.create',
|
|
205
|
-
'client.vektor.move.create',
|
|
206
|
-
'client.vektor.wrap.wrap.create',
|
|
207
|
-
'client.vektor.wrap.unwrap.create',
|
|
208
|
-
'client.vektor.bridge.quotes.list',
|
|
209
|
-
'client.vektor.lock.markets.list',
|
|
210
|
-
'client.vektor.lock.positions.list',
|
|
211
|
-
'client.vektor.vote.markets.list',
|
|
212
|
-
'client.vektor.vote.rewards.list',
|
|
213
|
-
'client.vektor.incentivize.list',
|
|
214
|
-
'client.vektor.executions.get',
|
|
215
|
-
'client.vektor.executions.list',
|
|
216
|
-
'client.vektor.executions.steps.get',
|
|
217
|
-
'client.vektor.executions.steps.sign',
|
|
218
|
-
],
|
|
219
|
-
{ threshold: 1, shouldSort: true },
|
|
220
|
-
);
|
|
221
|
-
|
|
222
|
-
function getMethodSuggestions(fullyQualifiedMethodName: string): string[] {
|
|
223
|
-
return fuse
|
|
224
|
-
.search(fullyQualifiedMethodName)
|
|
225
|
-
.map(({ item }) => item)
|
|
226
|
-
.slice(0, 5);
|
|
227
|
-
}
|
|
228
|
-
|
|
229
|
-
const proxyToObj = new WeakMap<any, any>();
|
|
230
|
-
const objToProxy = new WeakMap<any, any>();
|
|
231
|
-
|
|
232
|
-
type ClientProxyConfig = {
|
|
233
|
-
path: string[];
|
|
234
|
-
isBelievedBad?: boolean;
|
|
235
|
-
};
|
|
236
|
-
|
|
237
|
-
function makeSdkProxy<T extends object>(obj: T, { path, isBelievedBad = false }: ClientProxyConfig): T {
|
|
238
|
-
let proxy: T = objToProxy.get(obj);
|
|
239
|
-
|
|
240
|
-
if (!proxy) {
|
|
241
|
-
proxy = new Proxy(obj, {
|
|
242
|
-
get(target, prop, receiver) {
|
|
243
|
-
const propPath = [...path, String(prop)];
|
|
244
|
-
const value = Reflect.get(target, prop, receiver);
|
|
245
|
-
|
|
246
|
-
if (isBelievedBad || (!(prop in target) && value === undefined)) {
|
|
247
|
-
// If we're accessing a path that doesn't exist, it will probably eventually error.
|
|
248
|
-
// Let's proxy it and mark it bad so that we can control the error message.
|
|
249
|
-
// We proxy an empty class so that an invocation or construction attempt is possible.
|
|
250
|
-
return makeSdkProxy(class {}, { path: propPath, isBelievedBad: true });
|
|
251
|
-
}
|
|
252
|
-
|
|
253
|
-
if (value !== null && (typeof value === 'object' || typeof value === 'function')) {
|
|
254
|
-
return makeSdkProxy(value, { path: propPath, isBelievedBad });
|
|
255
|
-
}
|
|
256
|
-
|
|
257
|
-
return value;
|
|
258
|
-
},
|
|
259
|
-
|
|
260
|
-
apply(target, thisArg, args) {
|
|
261
|
-
if (isBelievedBad || typeof target !== 'function') {
|
|
262
|
-
const fullyQualifiedMethodName = path.join('.');
|
|
263
|
-
const suggestions = getMethodSuggestions(fullyQualifiedMethodName);
|
|
264
|
-
throw new Error(
|
|
265
|
-
`${fullyQualifiedMethodName} is not a function. Did you mean: ${suggestions.join(', ')}`,
|
|
266
|
-
);
|
|
267
|
-
}
|
|
268
|
-
|
|
269
|
-
return Reflect.apply(target, proxyToObj.get(thisArg) ?? thisArg, args);
|
|
270
|
-
},
|
|
271
|
-
|
|
272
|
-
construct(target, args, newTarget) {
|
|
273
|
-
if (isBelievedBad || typeof target !== 'function') {
|
|
274
|
-
const fullyQualifiedMethodName = path.join('.');
|
|
275
|
-
const suggestions = getMethodSuggestions(fullyQualifiedMethodName);
|
|
276
|
-
throw new Error(
|
|
277
|
-
`${fullyQualifiedMethodName} is not a constructor. Did you mean: ${suggestions.join(', ')}`,
|
|
278
|
-
);
|
|
279
|
-
}
|
|
280
|
-
|
|
281
|
-
return Reflect.construct(target, args, newTarget);
|
|
282
|
-
},
|
|
283
|
-
});
|
|
284
|
-
|
|
285
|
-
objToProxy.set(obj, proxy);
|
|
286
|
-
proxyToObj.set(proxy, obj);
|
|
287
|
-
}
|
|
288
|
-
|
|
289
|
-
return proxy;
|
|
290
|
-
}
|
|
291
|
-
|
|
292
|
-
function parseError(code: string, error: unknown): string | undefined {
|
|
293
|
-
if (!(error instanceof Error)) return;
|
|
294
|
-
const message = error.name ? `${error.name}: ${error.message}` : error.message;
|
|
295
|
-
try {
|
|
296
|
-
// Deno uses V8; the first "<anonymous>:LINE:COLUMN" is the top of stack.
|
|
297
|
-
const lineNumber = error.stack?.match(/<anonymous>:([0-9]+):[0-9]+/)?.[1];
|
|
298
|
-
// -1 for the zero-based indexing
|
|
299
|
-
const line =
|
|
300
|
-
lineNumber &&
|
|
301
|
-
code
|
|
302
|
-
.split('\n')
|
|
303
|
-
.at(parseInt(lineNumber, 10) - 1)
|
|
304
|
-
?.trim();
|
|
305
|
-
return line ? `${message}\n at line ${lineNumber}\n ${line}` : message;
|
|
306
|
-
} catch {
|
|
307
|
-
return message;
|
|
308
|
-
}
|
|
309
|
-
}
|
|
310
|
-
|
|
311
|
-
const fetch = async (req: Request): Promise<Response> => {
|
|
312
|
-
const { opts, code } = (await req.json()) as WorkerInput;
|
|
313
|
-
|
|
314
|
-
const runFunctionSource = code ? getRunFunctionSource(code) : null;
|
|
315
|
-
if (!runFunctionSource) {
|
|
316
|
-
const message =
|
|
317
|
-
code ?
|
|
318
|
-
'The code is missing a top-level `run` function.'
|
|
319
|
-
: 'The code argument is missing. Provide one containing a top-level `run` function.';
|
|
320
|
-
return Response.json(
|
|
321
|
-
{
|
|
322
|
-
message: `${message} Write code within this template:\n\n\`\`\`\nasync function run(client) {\n // Fill this out\n}\n\`\`\``,
|
|
323
|
-
logLines: [],
|
|
324
|
-
errLines: [],
|
|
325
|
-
} satisfies WorkerError,
|
|
326
|
-
{ status: 400, statusText: 'Code execution error' },
|
|
327
|
-
);
|
|
328
|
-
}
|
|
329
|
-
|
|
330
|
-
const diagnostics = getTSDiagnostics(code);
|
|
331
|
-
if (diagnostics.length > 0) {
|
|
332
|
-
return Response.json(
|
|
333
|
-
{
|
|
334
|
-
message: `The code contains TypeScript diagnostics:\n${diagnostics.join('\n')}`,
|
|
335
|
-
logLines: [],
|
|
336
|
-
errLines: [],
|
|
337
|
-
} satisfies WorkerError,
|
|
338
|
-
{ status: 400, statusText: 'Code execution error' },
|
|
339
|
-
);
|
|
340
|
-
}
|
|
341
|
-
|
|
342
|
-
const client = new NirvanaLabs({
|
|
343
|
-
...opts,
|
|
344
|
-
});
|
|
345
|
-
|
|
346
|
-
const logLines: string[] = [];
|
|
347
|
-
const errLines: string[] = [];
|
|
348
|
-
const console = {
|
|
349
|
-
log: (...args: unknown[]) => {
|
|
350
|
-
logLines.push(util.format(...args));
|
|
351
|
-
},
|
|
352
|
-
error: (...args: unknown[]) => {
|
|
353
|
-
errLines.push(util.format(...args));
|
|
354
|
-
},
|
|
355
|
-
};
|
|
356
|
-
try {
|
|
357
|
-
let run_ = async (client: any) => {};
|
|
358
|
-
eval(`${code}\nrun_ = run;`);
|
|
359
|
-
const result = await run_(makeSdkProxy(client, { path: ['client'] }));
|
|
360
|
-
return Response.json({
|
|
361
|
-
result,
|
|
362
|
-
logLines,
|
|
363
|
-
errLines,
|
|
364
|
-
} satisfies WorkerSuccess);
|
|
365
|
-
} catch (e) {
|
|
366
|
-
return Response.json(
|
|
367
|
-
{
|
|
368
|
-
message: parseError(code, e),
|
|
369
|
-
logLines,
|
|
370
|
-
errLines,
|
|
371
|
-
} satisfies WorkerError,
|
|
372
|
-
{ status: 400, statusText: 'Code execution error' },
|
|
373
|
-
);
|
|
374
|
-
}
|
|
375
|
-
};
|
|
376
|
-
|
|
377
|
-
export default { fetch };
|