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