@metorial/sdk 1.2.0 → 2.0.0
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/dist/index.cjs +174 -487
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +147 -43
- package/dist/index.d.ts +147 -43
- package/dist/index.js +176 -489
- package/dist/index.js.map +1 -1
- package/package.json +12 -12
package/dist/index.js
CHANGED
|
@@ -1,439 +1,167 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { MetorialMcpSession } from '@metorial/mcp-session';
|
|
3
|
-
import { metorialOpenAI } from '@metorial/openai';
|
|
4
|
-
import { metorialAnthropic } from '@metorial/anthropic';
|
|
5
|
-
import { metorialDeepseek } from '@metorial/deepseek';
|
|
6
|
-
import { metorialGoogle } from '@metorial/google';
|
|
7
|
-
import { metorialMistral } from '@metorial/mistral';
|
|
8
|
-
import { metorialXai } from '@metorial/xai';
|
|
9
|
-
import { metorialTogetherAi } from '@metorial/togetherai';
|
|
1
|
+
import { createMetorialPulsarCoreSDK, createMetorialMagnetarCoreSDK } from '@metorial/core';
|
|
2
|
+
import { MetorialMcpSession, MetorialMagnetarMcpSession } from '@metorial/mcp-session';
|
|
10
3
|
|
|
11
4
|
// src/metorial.ts
|
|
12
|
-
var
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
maxSteps = 25,
|
|
19
|
-
tools: requestedTools,
|
|
20
|
-
withProviderSession,
|
|
21
|
-
...openaiOptions
|
|
22
|
-
} = config;
|
|
23
|
-
return withProviderSession(
|
|
24
|
-
metorialOpenAI.chatCompletions,
|
|
25
|
-
{
|
|
26
|
-
serverDeployments: Array.isArray(serverDeployments) ? serverDeployments : [serverDeployments]
|
|
27
|
-
},
|
|
28
|
-
async (session) => {
|
|
29
|
-
let tools = requestedTools ? session.tools.filter((t) => requestedTools.includes(t.name)) : session.tools;
|
|
30
|
-
let messages = [
|
|
31
|
-
{ role: "user", content: message }
|
|
32
|
-
];
|
|
33
|
-
for (let step = 0; step < maxSteps; step++) {
|
|
34
|
-
let response = await client.chat.completions.create({
|
|
35
|
-
model,
|
|
36
|
-
messages,
|
|
37
|
-
tools,
|
|
38
|
-
...openaiOptions
|
|
39
|
-
});
|
|
40
|
-
let choice = response.choices[0];
|
|
41
|
-
if (!choice.message.tool_calls) {
|
|
42
|
-
return {
|
|
43
|
-
text: choice.message.content || "",
|
|
44
|
-
steps: step + 1
|
|
45
|
-
};
|
|
46
|
-
}
|
|
47
|
-
let toolResponses = await session.callTools(choice.message.tool_calls);
|
|
48
|
-
messages.push(
|
|
49
|
-
{ role: "assistant", tool_calls: choice.message.tool_calls },
|
|
50
|
-
...toolResponses
|
|
51
|
-
);
|
|
52
|
-
}
|
|
53
|
-
throw new Error(`Max steps (${maxSteps}) reached without final response`);
|
|
54
|
-
}
|
|
55
|
-
);
|
|
56
|
-
};
|
|
57
|
-
var runWithAnthropic = async (config) => {
|
|
58
|
-
let {
|
|
59
|
-
message,
|
|
60
|
-
serverDeployments,
|
|
61
|
-
client,
|
|
62
|
-
model,
|
|
63
|
-
maxSteps = 25,
|
|
64
|
-
tools: requestedTools,
|
|
65
|
-
withProviderSession,
|
|
66
|
-
max_tokens = 4096,
|
|
67
|
-
...anthropicOptions
|
|
68
|
-
} = config;
|
|
69
|
-
return withProviderSession(
|
|
70
|
-
metorialAnthropic,
|
|
71
|
-
{
|
|
72
|
-
serverDeployments: Array.isArray(serverDeployments) ? serverDeployments : [serverDeployments]
|
|
73
|
-
},
|
|
74
|
-
async (session) => {
|
|
75
|
-
let tools = requestedTools ? session.tools.filter((t) => requestedTools.includes(t.name)) : session.tools;
|
|
76
|
-
let messages = [
|
|
77
|
-
{ role: "user", content: message }
|
|
78
|
-
];
|
|
79
|
-
let uniqueTools = Array.from(new Map(tools.map((t) => [t.name, t])).values());
|
|
80
|
-
for (let step = 0; step < maxSteps; step++) {
|
|
81
|
-
let response = await client.messages.create({
|
|
82
|
-
model,
|
|
83
|
-
max_tokens,
|
|
84
|
-
messages,
|
|
85
|
-
tools: uniqueTools,
|
|
86
|
-
...anthropicOptions
|
|
87
|
-
});
|
|
88
|
-
if (response.stop_reason !== "tool_use") {
|
|
89
|
-
let textContent = response.content.find(
|
|
90
|
-
(block) => block.type === "text"
|
|
91
|
-
);
|
|
92
|
-
return {
|
|
93
|
-
text: (textContent == null ? void 0 : textContent.text) || "",
|
|
94
|
-
steps: step + 1
|
|
95
|
-
};
|
|
96
|
-
}
|
|
97
|
-
let toolUseBlocks = response.content.filter(
|
|
98
|
-
(block) => block.type === "tool_use"
|
|
99
|
-
);
|
|
100
|
-
let toolResponse = await session.callTools(toolUseBlocks);
|
|
101
|
-
let toolResponseMessage = {
|
|
102
|
-
role: "user",
|
|
103
|
-
content: toolResponse.content.map((block) => ({
|
|
104
|
-
type: "tool_result",
|
|
105
|
-
tool_use_id: block.tool_use_id,
|
|
106
|
-
content: block.content
|
|
107
|
-
}))
|
|
108
|
-
};
|
|
109
|
-
messages.push(
|
|
110
|
-
{ role: "assistant", content: response.content },
|
|
111
|
-
toolResponseMessage
|
|
112
|
-
);
|
|
113
|
-
}
|
|
114
|
-
throw new Error(`Max steps (${maxSteps}) reached without final response`);
|
|
115
|
-
}
|
|
116
|
-
);
|
|
117
|
-
};
|
|
118
|
-
var runWithDeepSeek = async (config) => {
|
|
119
|
-
let {
|
|
120
|
-
message,
|
|
121
|
-
serverDeployments,
|
|
122
|
-
client,
|
|
123
|
-
model,
|
|
124
|
-
maxSteps = 25,
|
|
125
|
-
tools: requestedTools,
|
|
126
|
-
withProviderSession,
|
|
127
|
-
...deepseekOptions
|
|
128
|
-
} = config;
|
|
129
|
-
return withProviderSession(
|
|
130
|
-
metorialDeepseek,
|
|
131
|
-
{
|
|
132
|
-
serverDeployments: Array.isArray(serverDeployments) ? serverDeployments : [serverDeployments]
|
|
133
|
-
},
|
|
134
|
-
async (session) => {
|
|
135
|
-
let tools = requestedTools ? session.tools.filter((t) => {
|
|
136
|
-
var _a;
|
|
137
|
-
return requestedTools.includes(((_a = t.function) == null ? void 0 : _a.name) || t.name);
|
|
138
|
-
}) : session.tools;
|
|
139
|
-
let uniqueTools = Array.from(
|
|
140
|
-
new Map(tools.map((t) => {
|
|
141
|
-
var _a;
|
|
142
|
-
return [((_a = t.function) == null ? void 0 : _a.name) || t.name, t];
|
|
143
|
-
})).values()
|
|
144
|
-
);
|
|
145
|
-
let messages = [
|
|
146
|
-
{ role: "user", content: message }
|
|
147
|
-
];
|
|
148
|
-
for (let step = 0; step < maxSteps; step++) {
|
|
149
|
-
let response = await client.chat.completions.create({
|
|
150
|
-
model,
|
|
151
|
-
messages,
|
|
152
|
-
tools: uniqueTools,
|
|
153
|
-
...deepseekOptions
|
|
154
|
-
});
|
|
155
|
-
let choice = response.choices[0];
|
|
156
|
-
if (!choice.message.tool_calls || choice.message.tool_calls.length === 0) {
|
|
157
|
-
return {
|
|
158
|
-
text: choice.message.content || "",
|
|
159
|
-
steps: step + 1
|
|
160
|
-
};
|
|
161
|
-
}
|
|
162
|
-
let toolResponses = await session.callTools(choice.message.tool_calls);
|
|
163
|
-
messages.push(
|
|
164
|
-
{ role: "assistant", tool_calls: choice.message.tool_calls },
|
|
165
|
-
...toolResponses
|
|
166
|
-
);
|
|
167
|
-
}
|
|
168
|
-
throw new Error(`Max steps (${maxSteps}) reached without final response`);
|
|
169
|
-
}
|
|
170
|
-
);
|
|
171
|
-
};
|
|
172
|
-
var runWithGoogle = async (config) => {
|
|
173
|
-
let {
|
|
174
|
-
message,
|
|
175
|
-
serverDeployments,
|
|
176
|
-
client,
|
|
177
|
-
model,
|
|
178
|
-
maxSteps = 25,
|
|
179
|
-
tools: requestedTools,
|
|
180
|
-
withProviderSession,
|
|
181
|
-
...googleOptions
|
|
182
|
-
} = config;
|
|
183
|
-
return withProviderSession(
|
|
184
|
-
metorialGoogle,
|
|
185
|
-
{
|
|
186
|
-
serverDeployments: Array.isArray(serverDeployments) ? serverDeployments : [serverDeployments]
|
|
187
|
-
},
|
|
188
|
-
async (session) => {
|
|
189
|
-
var _a, _b, _c, _d, _e, _f, _g, _h, _i;
|
|
190
|
-
let tools = requestedTools ? session.tools.filter((t) => requestedTools.includes(t.name)) : session.tools;
|
|
191
|
-
let response = await client.models.generateContent({
|
|
192
|
-
model,
|
|
193
|
-
contents: [
|
|
194
|
-
{
|
|
195
|
-
role: "user",
|
|
196
|
-
parts: [{ text: message }]
|
|
197
|
-
}
|
|
198
|
-
],
|
|
199
|
-
config: {
|
|
200
|
-
tools,
|
|
201
|
-
...googleOptions
|
|
202
|
-
}
|
|
203
|
-
});
|
|
204
|
-
let text = (_e = (_d = (_c = (_b = (_a = response.candidates) == null ? void 0 : _a[0]) == null ? void 0 : _b.content) == null ? void 0 : _c.parts) == null ? void 0 : _d[0]) == null ? void 0 : _e.text;
|
|
205
|
-
let functionCalls = (_i = (_h = (_g = (_f = response.candidates) == null ? void 0 : _f[0]) == null ? void 0 : _g.content) == null ? void 0 : _h.parts) == null ? void 0 : _i.filter((part) => part.functionCall).map((part) => part.functionCall);
|
|
206
|
-
if (functionCalls && functionCalls.length > 0) {
|
|
207
|
-
let toolResponses = await session.callTools(functionCalls);
|
|
208
|
-
return {
|
|
209
|
-
text: text || "",
|
|
210
|
-
toolResponses,
|
|
211
|
-
steps: 1
|
|
212
|
-
};
|
|
213
|
-
}
|
|
214
|
-
return {
|
|
215
|
-
text: text || "",
|
|
216
|
-
steps: 1
|
|
217
|
-
};
|
|
5
|
+
var Metorial = class {
|
|
6
|
+
constructor(init) {
|
|
7
|
+
let apiHost = init.apiHost;
|
|
8
|
+
let mcpHost = init.mcpHost;
|
|
9
|
+
if (mcpHost && !apiHost) {
|
|
10
|
+
apiHost = mcpHost.replace("://connect.", "://api.");
|
|
218
11
|
}
|
|
219
|
-
|
|
220
|
-
};
|
|
221
|
-
var extractTextFromContent = (content) => {
|
|
222
|
-
if (typeof content === "string") {
|
|
223
|
-
return content;
|
|
12
|
+
this.pulsarSdk = createMetorialPulsarCoreSDK({ ...init, apiHost });
|
|
13
|
+
this.magnetarSdk = createMetorialMagnetarCoreSDK({ ...init, apiHost, mcpHost });
|
|
224
14
|
}
|
|
225
|
-
|
|
226
|
-
|
|
15
|
+
// ── Magnetar (default) ───────────────────────────────────────────
|
|
16
|
+
get providers() {
|
|
17
|
+
return this.magnetarSdk.providers;
|
|
227
18
|
}
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
maxSteps = 25,
|
|
237
|
-
tools: requestedTools,
|
|
238
|
-
withProviderSession,
|
|
239
|
-
...mistralOptions
|
|
240
|
-
} = config;
|
|
241
|
-
return withProviderSession(
|
|
242
|
-
metorialMistral,
|
|
243
|
-
{
|
|
244
|
-
serverDeployments: Array.isArray(serverDeployments) ? serverDeployments : [serverDeployments]
|
|
245
|
-
},
|
|
246
|
-
async (session) => {
|
|
247
|
-
let tools = requestedTools ? session.tools.filter((t) => {
|
|
248
|
-
var _a;
|
|
249
|
-
return requestedTools.includes(((_a = t.function) == null ? void 0 : _a.name) || t.name);
|
|
250
|
-
}) : session.tools;
|
|
251
|
-
let fixedTools = tools.map((tool) => {
|
|
252
|
-
var _a;
|
|
253
|
-
if ((_a = tool.function) == null ? void 0 : _a.parameters) {
|
|
254
|
-
let fixedParams = { ...tool.function.parameters };
|
|
255
|
-
fixedParams.additionalProperties = false;
|
|
256
|
-
if (fixedParams.properties) {
|
|
257
|
-
Object.values(fixedParams.properties).forEach((prop) => {
|
|
258
|
-
if (prop && typeof prop === "object" && prop.type === "object") {
|
|
259
|
-
prop.additionalProperties = false;
|
|
260
|
-
}
|
|
261
|
-
});
|
|
262
|
-
}
|
|
263
|
-
return {
|
|
264
|
-
...tool,
|
|
265
|
-
function: {
|
|
266
|
-
...tool.function,
|
|
267
|
-
parameters: fixedParams
|
|
268
|
-
}
|
|
269
|
-
};
|
|
270
|
-
}
|
|
271
|
-
return tool;
|
|
272
|
-
});
|
|
273
|
-
let messages = [
|
|
274
|
-
{ role: "user", content: message }
|
|
275
|
-
];
|
|
276
|
-
for (let step = 0; step < maxSteps; step++) {
|
|
277
|
-
let response = await client.chat.complete({
|
|
278
|
-
model,
|
|
279
|
-
messages,
|
|
280
|
-
tools: fixedTools,
|
|
281
|
-
...mistralOptions
|
|
282
|
-
});
|
|
283
|
-
let choice = response.choices[0];
|
|
284
|
-
let toolCalls = choice.message.toolCalls;
|
|
285
|
-
if (!toolCalls || toolCalls.length === 0) {
|
|
286
|
-
return {
|
|
287
|
-
text: extractTextFromContent(choice.message.content),
|
|
288
|
-
steps: step + 1
|
|
289
|
-
};
|
|
290
|
-
}
|
|
291
|
-
let toolResponses = await session.callTools(toolCalls);
|
|
292
|
-
messages.push(
|
|
293
|
-
{ role: "assistant", toolCalls },
|
|
294
|
-
...toolResponses
|
|
295
|
-
);
|
|
296
|
-
}
|
|
297
|
-
throw new Error(`Max steps (${maxSteps}) reached without final response`);
|
|
298
|
-
}
|
|
299
|
-
);
|
|
300
|
-
};
|
|
301
|
-
var runWithXAI = async (config) => {
|
|
302
|
-
let {
|
|
303
|
-
message,
|
|
304
|
-
serverDeployments,
|
|
305
|
-
client,
|
|
306
|
-
model,
|
|
307
|
-
maxSteps = 25,
|
|
308
|
-
tools: requestedTools,
|
|
309
|
-
withProviderSession,
|
|
310
|
-
...xaiOptions
|
|
311
|
-
} = config;
|
|
312
|
-
return withProviderSession(
|
|
313
|
-
metorialXai,
|
|
314
|
-
{
|
|
315
|
-
serverDeployments: Array.isArray(serverDeployments) ? serverDeployments : [serverDeployments]
|
|
316
|
-
},
|
|
317
|
-
async (session) => {
|
|
318
|
-
let tools = requestedTools ? session.tools.filter((t) => {
|
|
319
|
-
var _a;
|
|
320
|
-
return requestedTools.includes(((_a = t.function) == null ? void 0 : _a.name) || t.name);
|
|
321
|
-
}) : session.tools;
|
|
322
|
-
let uniqueTools = Array.from(
|
|
323
|
-
new Map(tools.map((t) => [t.function.name, t])).values()
|
|
324
|
-
);
|
|
325
|
-
let messages = [
|
|
326
|
-
{ role: "user", content: message }
|
|
327
|
-
];
|
|
328
|
-
for (let step = 0; step < maxSteps; step++) {
|
|
329
|
-
let response = await client.chat.completions.create({
|
|
330
|
-
model,
|
|
331
|
-
messages,
|
|
332
|
-
tools: uniqueTools,
|
|
333
|
-
...xaiOptions
|
|
334
|
-
});
|
|
335
|
-
let choice = response.choices[0];
|
|
336
|
-
if (!choice.message.tool_calls || choice.message.tool_calls.length === 0) {
|
|
337
|
-
return {
|
|
338
|
-
text: choice.message.content || "",
|
|
339
|
-
steps: step + 1
|
|
340
|
-
};
|
|
341
|
-
}
|
|
342
|
-
let toolResponses = await session.callTools(choice.message.tool_calls);
|
|
343
|
-
messages.push(
|
|
344
|
-
{ role: "assistant", tool_calls: choice.message.tool_calls },
|
|
345
|
-
...toolResponses
|
|
346
|
-
);
|
|
347
|
-
}
|
|
348
|
-
throw new Error(`Max steps (${maxSteps}) reached without final response`);
|
|
349
|
-
}
|
|
350
|
-
);
|
|
351
|
-
};
|
|
352
|
-
var runWithTogetherAI = async (config) => {
|
|
353
|
-
let {
|
|
354
|
-
message,
|
|
355
|
-
serverDeployments,
|
|
356
|
-
client,
|
|
357
|
-
model,
|
|
358
|
-
maxSteps = 25,
|
|
359
|
-
tools: requestedTools,
|
|
360
|
-
withProviderSession,
|
|
361
|
-
...togetheraiOptions
|
|
362
|
-
} = config;
|
|
363
|
-
return withProviderSession(
|
|
364
|
-
metorialTogetherAi,
|
|
365
|
-
{
|
|
366
|
-
serverDeployments: Array.isArray(serverDeployments) ? serverDeployments : [serverDeployments]
|
|
367
|
-
},
|
|
368
|
-
async (session) => {
|
|
369
|
-
let tools = requestedTools ? session.tools.filter((t) => {
|
|
370
|
-
var _a;
|
|
371
|
-
return requestedTools.includes(((_a = t.function) == null ? void 0 : _a.name) || t.name);
|
|
372
|
-
}) : session.tools;
|
|
373
|
-
let messages = [
|
|
374
|
-
{ role: "user", content: message }
|
|
375
|
-
];
|
|
376
|
-
for (let step = 0; step < maxSteps; step++) {
|
|
377
|
-
let response = await client.chat.completions.create({
|
|
378
|
-
model,
|
|
379
|
-
messages,
|
|
380
|
-
tools,
|
|
381
|
-
...togetheraiOptions
|
|
382
|
-
});
|
|
383
|
-
let choice = response.choices[0];
|
|
384
|
-
if (!choice.message.tool_calls || choice.message.tool_calls.length === 0) {
|
|
385
|
-
return {
|
|
386
|
-
text: choice.message.content || "",
|
|
387
|
-
steps: step + 1
|
|
388
|
-
};
|
|
389
|
-
}
|
|
390
|
-
let toolResponses = await session.callTools(choice.message.tool_calls);
|
|
391
|
-
messages.push(
|
|
392
|
-
{ role: "assistant", tool_calls: choice.message.tool_calls },
|
|
393
|
-
...toolResponses
|
|
394
|
-
);
|
|
395
|
-
}
|
|
396
|
-
throw new Error(`Max steps (${maxSteps}) reached without final response`);
|
|
397
|
-
}
|
|
398
|
-
);
|
|
399
|
-
};
|
|
400
|
-
|
|
401
|
-
// src/metorial.ts
|
|
402
|
-
var Metorial = class {
|
|
403
|
-
constructor(init) {
|
|
404
|
-
this.sdk = createMetorialCoreSDK(init);
|
|
19
|
+
get providerDeployments() {
|
|
20
|
+
let deployments = this.magnetarSdk.providerDeployments;
|
|
21
|
+
return {
|
|
22
|
+
...deployments,
|
|
23
|
+
setupSessions: Object.assign(deployments.setupSessions, {
|
|
24
|
+
waitForCompletion: this.waitForSetupSession.bind(this)
|
|
25
|
+
})
|
|
26
|
+
};
|
|
405
27
|
}
|
|
406
|
-
get
|
|
407
|
-
return this.
|
|
28
|
+
get sessions() {
|
|
29
|
+
return this.magnetarSdk.sessions;
|
|
408
30
|
}
|
|
409
|
-
get
|
|
410
|
-
return this.
|
|
31
|
+
get sessionTemplates() {
|
|
32
|
+
return this.magnetarSdk.sessionTemplates;
|
|
33
|
+
}
|
|
34
|
+
get providerRuns() {
|
|
35
|
+
return this.magnetarSdk.providerRuns;
|
|
36
|
+
}
|
|
37
|
+
get instance() {
|
|
38
|
+
return this.magnetarSdk.instance;
|
|
411
39
|
}
|
|
40
|
+
// ── Backward compat (Pulsar endpoints at top level) ──────────────
|
|
412
41
|
get servers() {
|
|
413
|
-
return this.
|
|
42
|
+
return this.pulsarSdk.servers;
|
|
414
43
|
}
|
|
415
|
-
get
|
|
416
|
-
return this.
|
|
44
|
+
get secrets() {
|
|
45
|
+
return this.pulsarSdk.secrets;
|
|
417
46
|
}
|
|
418
47
|
get oauth() {
|
|
419
48
|
return {
|
|
420
|
-
...this.
|
|
49
|
+
...this.pulsarSdk.oauth,
|
|
421
50
|
waitForCompletion: this.waitForOAuthCompletion.bind(this)
|
|
422
51
|
};
|
|
423
52
|
}
|
|
424
53
|
get _config() {
|
|
425
|
-
return this.
|
|
54
|
+
return this.pulsarSdk._config;
|
|
426
55
|
}
|
|
56
|
+
// ── v1 namespace (legacy Pulsar) ─────────────────────────────────
|
|
57
|
+
get v1() {
|
|
58
|
+
let pulsarSdk = this.pulsarSdk;
|
|
59
|
+
let self = this;
|
|
60
|
+
return {
|
|
61
|
+
get instance() {
|
|
62
|
+
return pulsarSdk.instance;
|
|
63
|
+
},
|
|
64
|
+
get secrets() {
|
|
65
|
+
return pulsarSdk.secrets;
|
|
66
|
+
},
|
|
67
|
+
get servers() {
|
|
68
|
+
return pulsarSdk.servers;
|
|
69
|
+
},
|
|
70
|
+
get sessions() {
|
|
71
|
+
return pulsarSdk.sessions;
|
|
72
|
+
},
|
|
73
|
+
get oauth() {
|
|
74
|
+
return self.oauth;
|
|
75
|
+
},
|
|
76
|
+
get _config() {
|
|
77
|
+
return pulsarSdk._config;
|
|
78
|
+
},
|
|
79
|
+
mcp: {
|
|
80
|
+
createSession: (init) => new MetorialMcpSession(pulsarSdk, init),
|
|
81
|
+
withSession: self._pulsarWithSession.bind(self),
|
|
82
|
+
withProviderSession: self._pulsarWithProviderSession.bind(self),
|
|
83
|
+
createConnection: self._pulsarCreateMcpConnection.bind(self)
|
|
84
|
+
},
|
|
85
|
+
withSession: self._pulsarWithSession.bind(self),
|
|
86
|
+
withProviderSession: self._pulsarWithProviderSession.bind(self),
|
|
87
|
+
createMcpConnection: self._pulsarCreateMcpConnection.bind(self)
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
// ── MCP (Magnetar default) ───────────────────────────────────────
|
|
427
91
|
get mcp() {
|
|
428
92
|
return {
|
|
429
|
-
createSession: (init) => new
|
|
93
|
+
createSession: (init) => new MetorialMagnetarMcpSession(this.magnetarSdk, init),
|
|
430
94
|
withSession: this.withSession.bind(this),
|
|
431
95
|
withProviderSession: this.withProviderSession.bind(this),
|
|
432
96
|
createConnection: this.createMcpConnection.bind(this)
|
|
433
97
|
};
|
|
434
98
|
}
|
|
99
|
+
session(init) {
|
|
100
|
+
return new MetorialMagnetarMcpSession(this.magnetarSdk, init);
|
|
101
|
+
}
|
|
435
102
|
async createMcpConnection(init) {
|
|
436
|
-
let session = new
|
|
103
|
+
let session = new MetorialMagnetarMcpSession(this.magnetarSdk, init);
|
|
104
|
+
return await session.getClient();
|
|
105
|
+
}
|
|
106
|
+
async withSession(init, action) {
|
|
107
|
+
let session = new MetorialMagnetarMcpSession(this.magnetarSdk, init);
|
|
108
|
+
try {
|
|
109
|
+
return await action(session);
|
|
110
|
+
} finally {
|
|
111
|
+
await session.close();
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
async withProviderSession(provider, init, action) {
|
|
115
|
+
if (init.streaming) {
|
|
116
|
+
return this.withMagnetarStreamingSession(provider, init, action);
|
|
117
|
+
}
|
|
118
|
+
return this.withSession(init, async (session) => {
|
|
119
|
+
let providerData = await provider(session);
|
|
120
|
+
return action({
|
|
121
|
+
...providerData,
|
|
122
|
+
session,
|
|
123
|
+
getSession: session.getSession.bind(session),
|
|
124
|
+
getCapabilities: session.getCapabilities.bind(session),
|
|
125
|
+
getToolManager: session.getToolManager.bind(session),
|
|
126
|
+
closeSession: async () => {
|
|
127
|
+
}
|
|
128
|
+
});
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
async withMagnetarStreamingSession(provider, init, action) {
|
|
132
|
+
let session = new MetorialMagnetarMcpSession(this.magnetarSdk, init);
|
|
133
|
+
let sessionClosed = false;
|
|
134
|
+
const closeSession = async () => {
|
|
135
|
+
if (!sessionClosed) {
|
|
136
|
+
sessionClosed = true;
|
|
137
|
+
await session.close();
|
|
138
|
+
}
|
|
139
|
+
};
|
|
140
|
+
try {
|
|
141
|
+
let providerData = await provider(session);
|
|
142
|
+
let result = await action({
|
|
143
|
+
...providerData,
|
|
144
|
+
session,
|
|
145
|
+
getSession: session.getSession.bind(session),
|
|
146
|
+
getCapabilities: session.getCapabilities.bind(session),
|
|
147
|
+
getToolManager: session.getToolManager.bind(session),
|
|
148
|
+
closeSession
|
|
149
|
+
});
|
|
150
|
+
let timeout = setTimeout(async () => {
|
|
151
|
+
if (!sessionClosed) {
|
|
152
|
+
await closeSession();
|
|
153
|
+
}
|
|
154
|
+
}, 6e4);
|
|
155
|
+
timeout.unref();
|
|
156
|
+
return result;
|
|
157
|
+
} catch (error) {
|
|
158
|
+
await closeSession();
|
|
159
|
+
throw error;
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
// ── Pulsar MCP helpers (used by v1) ──────────────────────────────
|
|
163
|
+
async _pulsarCreateMcpConnection(init) {
|
|
164
|
+
let session = new MetorialMcpSession(this.pulsarSdk, {
|
|
437
165
|
serverDeployments: [init]
|
|
438
166
|
});
|
|
439
167
|
let deployments = await session.getServerDeployments();
|
|
@@ -441,19 +169,19 @@ var Metorial = class {
|
|
|
441
169
|
deploymentId: deployments[0].id
|
|
442
170
|
});
|
|
443
171
|
}
|
|
444
|
-
async
|
|
445
|
-
let session = new MetorialMcpSession(this.
|
|
172
|
+
async _pulsarWithSession(init, action) {
|
|
173
|
+
let session = new MetorialMcpSession(this.pulsarSdk, init);
|
|
446
174
|
try {
|
|
447
175
|
return await action(session);
|
|
448
176
|
} finally {
|
|
449
177
|
await session.close();
|
|
450
178
|
}
|
|
451
179
|
}
|
|
452
|
-
async
|
|
180
|
+
async _pulsarWithProviderSession(provider, init, action) {
|
|
453
181
|
if (init.streaming) {
|
|
454
182
|
return this.withStreamingSession(provider, init, action);
|
|
455
183
|
}
|
|
456
|
-
return this.
|
|
184
|
+
return this._pulsarWithSession(init, async (session) => {
|
|
457
185
|
let providerData = await provider(session);
|
|
458
186
|
return action({
|
|
459
187
|
...providerData,
|
|
@@ -467,12 +195,11 @@ var Metorial = class {
|
|
|
467
195
|
});
|
|
468
196
|
}
|
|
469
197
|
async withStreamingSession(provider, init, action) {
|
|
470
|
-
let session = new MetorialMcpSession(this.
|
|
198
|
+
let session = new MetorialMcpSession(this.pulsarSdk, init);
|
|
471
199
|
let sessionClosed = false;
|
|
472
200
|
const closeSession = async () => {
|
|
473
201
|
if (!sessionClosed) {
|
|
474
202
|
sessionClosed = true;
|
|
475
|
-
console.log("[Metorial] Closing streaming session");
|
|
476
203
|
await session.close();
|
|
477
204
|
}
|
|
478
205
|
};
|
|
@@ -488,42 +215,51 @@ var Metorial = class {
|
|
|
488
215
|
getToolManager: session.getToolManager.bind(session),
|
|
489
216
|
closeSession
|
|
490
217
|
});
|
|
491
|
-
setTimeout(async () => {
|
|
218
|
+
let timeout = setTimeout(async () => {
|
|
492
219
|
if (!sessionClosed) {
|
|
493
|
-
console.log("[Metorial] Streaming timeout reached - auto-closing session");
|
|
494
220
|
await closeSession();
|
|
495
221
|
}
|
|
496
222
|
}, 6e4);
|
|
223
|
+
timeout.unref();
|
|
497
224
|
return result;
|
|
498
225
|
} catch (error) {
|
|
499
226
|
await closeSession();
|
|
500
227
|
throw error;
|
|
501
228
|
}
|
|
502
229
|
}
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
if (modelLower.includes("deepseek")) {
|
|
512
|
-
return "deepseek";
|
|
513
|
-
}
|
|
514
|
-
if (modelLower.startsWith("gemini-") || modelLower.includes("google")) {
|
|
515
|
-
return "google";
|
|
516
|
-
}
|
|
517
|
-
if (modelLower.startsWith("mistral-") || modelLower.includes("mistral")) {
|
|
518
|
-
return "mistral";
|
|
519
|
-
}
|
|
520
|
-
if (modelLower.startsWith("x-") || modelLower === "grok-beta") {
|
|
521
|
-
return "xai";
|
|
230
|
+
async waitForSetupSession(sessions, options) {
|
|
231
|
+
var _a, _b;
|
|
232
|
+
let sessionList = Array.isArray(sessions) ? sessions : [sessions];
|
|
233
|
+
let pollInterval = Math.max((_a = options == null ? void 0 : options.pollInterval) != null ? _a : 5e3, 2e3);
|
|
234
|
+
let timeout = (_b = options == null ? void 0 : options.timeout) != null ? _b : 6e5;
|
|
235
|
+
let startTime = Date.now();
|
|
236
|
+
if (sessionList.length === 0) {
|
|
237
|
+
return [];
|
|
522
238
|
}
|
|
523
|
-
|
|
524
|
-
|
|
239
|
+
while (true) {
|
|
240
|
+
if (Date.now() - startTime > timeout) {
|
|
241
|
+
throw new Error(`Setup session timed out after ${timeout / 1e3} seconds`);
|
|
242
|
+
}
|
|
243
|
+
try {
|
|
244
|
+
let statuses = await Promise.all(
|
|
245
|
+
sessionList.map((s) => this.magnetarSdk.providerDeployments.setupSessions.get(s.id))
|
|
246
|
+
);
|
|
247
|
+
let failed = statuses.filter((s) => s.status === "failed");
|
|
248
|
+
if (failed.length > 0) {
|
|
249
|
+
throw new Error(`${failed.length} setup session(s) failed`);
|
|
250
|
+
}
|
|
251
|
+
let allCompleted = statuses.every((s) => s.status === "completed");
|
|
252
|
+
if (allCompleted) {
|
|
253
|
+
return statuses;
|
|
254
|
+
}
|
|
255
|
+
await new Promise((resolve) => setTimeout(resolve, pollInterval));
|
|
256
|
+
} catch (error) {
|
|
257
|
+
if (error instanceof Error && (error.message.includes("setup session") && (error.message.includes("failed") || error.message.includes("timed out")))) {
|
|
258
|
+
throw error;
|
|
259
|
+
}
|
|
260
|
+
await new Promise((resolve) => setTimeout(resolve, pollInterval));
|
|
261
|
+
}
|
|
525
262
|
}
|
|
526
|
-
throw new Error(`Unable to infer provider from model "${model}".`);
|
|
527
263
|
}
|
|
528
264
|
async waitForOAuthCompletion(sessions, options) {
|
|
529
265
|
var _a, _b;
|
|
@@ -560,55 +296,6 @@ var Metorial = class {
|
|
|
560
296
|
}
|
|
561
297
|
}
|
|
562
298
|
}
|
|
563
|
-
async run(config) {
|
|
564
|
-
let provider = this.inferProvider(config.model);
|
|
565
|
-
switch (provider) {
|
|
566
|
-
case "openai":
|
|
567
|
-
return runWithOpenAI({
|
|
568
|
-
...config,
|
|
569
|
-
client: config.client,
|
|
570
|
-
withProviderSession: this.withProviderSession.bind(this)
|
|
571
|
-
});
|
|
572
|
-
case "anthropic":
|
|
573
|
-
return runWithAnthropic({
|
|
574
|
-
...config,
|
|
575
|
-
client: config.client,
|
|
576
|
-
withProviderSession: this.withProviderSession.bind(this)
|
|
577
|
-
});
|
|
578
|
-
case "deepseek":
|
|
579
|
-
return runWithDeepSeek({
|
|
580
|
-
...config,
|
|
581
|
-
client: config.client,
|
|
582
|
-
withProviderSession: this.withProviderSession.bind(this)
|
|
583
|
-
});
|
|
584
|
-
case "google":
|
|
585
|
-
return runWithGoogle({
|
|
586
|
-
...config,
|
|
587
|
-
client: config.client,
|
|
588
|
-
withProviderSession: this.withProviderSession.bind(this)
|
|
589
|
-
});
|
|
590
|
-
case "mistral":
|
|
591
|
-
return runWithMistral({
|
|
592
|
-
...config,
|
|
593
|
-
client: config.client,
|
|
594
|
-
withProviderSession: this.withProviderSession.bind(this)
|
|
595
|
-
});
|
|
596
|
-
case "xai":
|
|
597
|
-
return runWithXAI({
|
|
598
|
-
...config,
|
|
599
|
-
client: config.client,
|
|
600
|
-
withProviderSession: this.withProviderSession.bind(this)
|
|
601
|
-
});
|
|
602
|
-
case "togetherai":
|
|
603
|
-
return runWithTogetherAI({
|
|
604
|
-
...config,
|
|
605
|
-
client: config.client,
|
|
606
|
-
withProviderSession: this.withProviderSession.bind(this)
|
|
607
|
-
});
|
|
608
|
-
default:
|
|
609
|
-
throw new Error(`Unsupported provider: ${provider}`);
|
|
610
|
-
}
|
|
611
|
-
}
|
|
612
299
|
};
|
|
613
300
|
|
|
614
301
|
export { Metorial };
|