@forklaunch/core 3.3.0 → 3.5.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/bin/gateway-mock/routes/payments.mjs +47 -0
- package/lib/agent/agent.d.ts +26 -0
- package/lib/agent/agent.d.ts.map +1 -0
- package/lib/agent/continuation.d.ts +29 -0
- package/lib/agent/continuation.d.ts.map +1 -0
- package/lib/agent/index.d.ts +8 -0
- package/lib/agent/index.d.ts.map +1 -0
- package/lib/agent/index.js +1049 -0
- package/lib/agent/index.js.map +1 -0
- package/lib/agent/index.mjs +1005 -0
- package/lib/agent/index.mjs.map +1 -0
- package/lib/agent/sources.d.ts +27 -0
- package/lib/agent/sources.d.ts.map +1 -0
- package/lib/agent/tools.d.ts +39 -0
- package/lib/agent/tools.d.ts.map +1 -0
- package/lib/agent/types.d.ts +130 -0
- package/lib/agent/types.d.ts.map +1 -0
- package/lib/http/index.d.ts +2 -0
- package/lib/http/index.d.ts.map +1 -1
- package/lib/http/index.js +430 -51
- package/lib/http/index.js.map +1 -1
- package/lib/http/index.mjs +425 -51
- package/lib/http/index.mjs.map +1 -1
- package/lib/http/localModelGatewayClient.d.ts +27 -0
- package/lib/http/localModelGatewayClient.d.ts.map +1 -0
- package/lib/http/mcpGenerator/mcpGenerator.d.ts.map +1 -1
- package/lib/http/mcpGenerator/mcpHttpHandler.d.ts +57 -0
- package/lib/http/mcpGenerator/mcpHttpHandler.d.ts.map +1 -0
- package/lib/http/modelGatewayClient.d.ts.map +1 -1
- package/lib/http/modelStream.d.ts +4 -0
- package/lib/http/modelStream.d.ts.map +1 -0
- package/lib/http/paymentsClient.d.ts +55 -1
- package/lib/http/paymentsClient.d.ts.map +1 -1
- package/lib/http/previewModelGatewayClient.d.ts +5 -1
- package/lib/http/previewModelGatewayClient.d.ts.map +1 -1
- package/lib/http/telemetry/openTelemetryCollector.d.ts.map +1 -1
- package/lib/http/types/expressLikeOptions.d.ts +12 -0
- package/lib/http/types/expressLikeOptions.d.ts.map +1 -1
- package/lib/persistence/compliantField.d.ts.map +1 -1
- package/lib/persistence/index.js +6 -1
- package/lib/persistence/index.js.map +1 -1
- package/lib/persistence/index.mjs +6 -1
- package/lib/persistence/index.mjs.map +1 -1
- package/package.json +33 -26
package/lib/http/index.mjs
CHANGED
|
@@ -446,13 +446,15 @@ new NodeSDK({
|
|
|
446
446
|
url: `${getEnvVar("OTEL_EXPORTER_OTLP_ENDPOINT") ?? "http://localhost:4318"}/v1/traces`,
|
|
447
447
|
headers: otelHeaders
|
|
448
448
|
}),
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
449
|
+
metricReaders: [
|
|
450
|
+
new PeriodicExportingMetricReader({
|
|
451
|
+
exporter: new OTLPMetricExporter({
|
|
452
|
+
url: `${getEnvVar("OTEL_EXPORTER_OTLP_ENDPOINT") ?? "http://localhost:4318"}/v1/metrics`,
|
|
453
|
+
headers: otelHeaders
|
|
454
|
+
}),
|
|
455
|
+
exportIntervalMillis: 5e3
|
|
456
|
+
})
|
|
457
|
+
],
|
|
456
458
|
logRecordProcessors: [
|
|
457
459
|
new BatchLogRecordProcessor({
|
|
458
460
|
exporter: new OTLPLogExporter({
|
|
@@ -2861,12 +2863,55 @@ function signPlatformEvent(params) {
|
|
|
2861
2863
|
});
|
|
2862
2864
|
}
|
|
2863
2865
|
|
|
2866
|
+
// src/http/modelStream.ts
|
|
2867
|
+
async function* readModelStream(response) {
|
|
2868
|
+
const reader = response.body?.getReader();
|
|
2869
|
+
if (!reader)
|
|
2870
|
+
throw new ModelGatewayRequestError(502, "The model stream is empty.");
|
|
2871
|
+
const decoder = new TextDecoder();
|
|
2872
|
+
let buffer = "", bytes = 0;
|
|
2873
|
+
try {
|
|
2874
|
+
for (; ; ) {
|
|
2875
|
+
const part = await reader.read();
|
|
2876
|
+
if (part.done) break;
|
|
2877
|
+
bytes += part.value.byteLength;
|
|
2878
|
+
if (bytes > 512e3) throw new Error("oversized");
|
|
2879
|
+
buffer += decoder.decode(part.value, { stream: true });
|
|
2880
|
+
let match;
|
|
2881
|
+
while (match = buffer.match(/\r?\n\r?\n/)) {
|
|
2882
|
+
const event = buffer.slice(0, match.index);
|
|
2883
|
+
buffer = buffer.slice(match.index + match[0].length);
|
|
2884
|
+
const data = event.split(/\r?\n/).filter((line) => line.startsWith("data:")).map((line) => line.slice(5).trimStart()).join("\n");
|
|
2885
|
+
if (!data) continue;
|
|
2886
|
+
if (data === "[DONE]") return;
|
|
2887
|
+
const value = JSON.parse(data);
|
|
2888
|
+
if (value.error || !Array.isArray(value.choices))
|
|
2889
|
+
throw new Error("invalid");
|
|
2890
|
+
yield value;
|
|
2891
|
+
}
|
|
2892
|
+
}
|
|
2893
|
+
throw new Error("interrupted");
|
|
2894
|
+
} catch {
|
|
2895
|
+
throw new ModelGatewayRequestError(
|
|
2896
|
+
502,
|
|
2897
|
+
"The model stream was interrupted. No automatic retry was sent."
|
|
2898
|
+
);
|
|
2899
|
+
} finally {
|
|
2900
|
+
await reader.cancel().catch(() => {
|
|
2901
|
+
});
|
|
2902
|
+
reader.releaseLock();
|
|
2903
|
+
}
|
|
2904
|
+
}
|
|
2905
|
+
|
|
2864
2906
|
// src/http/modelGatewayClient.ts
|
|
2865
2907
|
var ModelGatewayRequestError = class extends InstanceGatewayRequestError {
|
|
2866
2908
|
name = "ModelGatewayRequestError";
|
|
2867
2909
|
};
|
|
2868
2910
|
function createModelGatewayClient(options2 = {}) {
|
|
2869
|
-
const transport = createInstanceGatewayTransport(
|
|
2911
|
+
const transport = createInstanceGatewayTransport(
|
|
2912
|
+
options2,
|
|
2913
|
+
"The model gateway"
|
|
2914
|
+
);
|
|
2870
2915
|
async function send(method, route, body, signal) {
|
|
2871
2916
|
try {
|
|
2872
2917
|
return await transport.request(method, route, body, { signal });
|
|
@@ -2902,42 +2947,12 @@ function createModelGatewayClient(options2 = {}) {
|
|
|
2902
2947
|
request,
|
|
2903
2948
|
streamOptions.signal
|
|
2904
2949
|
);
|
|
2905
|
-
|
|
2906
|
-
for await (const data of sseData(response.body)) {
|
|
2907
|
-
if (data === "[DONE]") return;
|
|
2908
|
-
try {
|
|
2909
|
-
yield JSON.parse(data);
|
|
2910
|
-
} catch {
|
|
2911
|
-
}
|
|
2912
|
-
}
|
|
2950
|
+
yield* readModelStream(response);
|
|
2913
2951
|
}
|
|
2914
2952
|
}
|
|
2915
2953
|
}
|
|
2916
2954
|
};
|
|
2917
2955
|
}
|
|
2918
|
-
async function* sseData(stream) {
|
|
2919
|
-
const decoder = new TextDecoder();
|
|
2920
|
-
let buffer = "";
|
|
2921
|
-
const reader = stream.getReader();
|
|
2922
|
-
try {
|
|
2923
|
-
for (; ; ) {
|
|
2924
|
-
const { value, done } = await reader.read();
|
|
2925
|
-
if (done) break;
|
|
2926
|
-
buffer += decoder.decode(value, { stream: true });
|
|
2927
|
-
let boundary;
|
|
2928
|
-
while ((boundary = buffer.search(/\r?\n\r?\n/)) !== -1) {
|
|
2929
|
-
const event = buffer.slice(0, boundary);
|
|
2930
|
-
buffer = buffer.slice(boundary).replace(/^\r?\n\r?\n/, "");
|
|
2931
|
-
const data = event.split(/\r?\n/).filter((line) => line.startsWith("data:")).map((line) => line.slice(5).trimStart()).join("\n");
|
|
2932
|
-
if (data) yield data;
|
|
2933
|
-
}
|
|
2934
|
-
}
|
|
2935
|
-
const tail = buffer.trim();
|
|
2936
|
-
if (tail.startsWith("data:")) yield tail.slice(5).trimStart();
|
|
2937
|
-
} finally {
|
|
2938
|
-
reader.releaseLock();
|
|
2939
|
-
}
|
|
2940
|
-
}
|
|
2941
2956
|
|
|
2942
2957
|
// src/http/previewModelGatewayClient.ts
|
|
2943
2958
|
function record(value) {
|
|
@@ -2993,9 +3008,9 @@ function createPreviewModelGatewayClient(options2 = {}) {
|
|
|
2993
3008
|
options2,
|
|
2994
3009
|
"The local preview model gateway"
|
|
2995
3010
|
);
|
|
2996
|
-
async function send(method, route, body) {
|
|
3011
|
+
async function send(method, route, body, signal) {
|
|
2997
3012
|
try {
|
|
2998
|
-
return await transport.request(method, route, body);
|
|
3013
|
+
return await transport.request(method, route, body, { signal });
|
|
2999
3014
|
} catch (error) {
|
|
3000
3015
|
if (error instanceof InstanceGatewayRequestError)
|
|
3001
3016
|
throw new ModelGatewayRequestError(
|
|
@@ -3013,6 +3028,15 @@ function createPreviewModelGatewayClient(options2 = {}) {
|
|
|
3013
3028
|
},
|
|
3014
3029
|
chat: {
|
|
3015
3030
|
completions: {
|
|
3031
|
+
async *stream(request, options3 = {}) {
|
|
3032
|
+
const response = await send(
|
|
3033
|
+
"POST",
|
|
3034
|
+
"/models/chat/completions",
|
|
3035
|
+
{ ...request, stream: true },
|
|
3036
|
+
options3.signal
|
|
3037
|
+
);
|
|
3038
|
+
yield* readModelStream(response);
|
|
3039
|
+
},
|
|
3016
3040
|
async create(request) {
|
|
3017
3041
|
return await (await send(
|
|
3018
3042
|
"POST",
|
|
@@ -3546,6 +3570,19 @@ function createPaymentsClient(options2 = {}) {
|
|
|
3546
3570
|
},
|
|
3547
3571
|
status() {
|
|
3548
3572
|
return call("GET", "/payments/status");
|
|
3573
|
+
},
|
|
3574
|
+
payouts(params = {}) {
|
|
3575
|
+
const { limit } = params;
|
|
3576
|
+
if (limit !== void 0 && (!Number.isInteger(limit) || limit < 1 || limit > 50)) {
|
|
3577
|
+
throw new TypeError("limit must be a whole number from 1 to 50");
|
|
3578
|
+
}
|
|
3579
|
+
return call(
|
|
3580
|
+
"GET",
|
|
3581
|
+
limit === void 0 ? "/payments/payouts" : `/payments/payouts?limit=${limit}`
|
|
3582
|
+
);
|
|
3583
|
+
},
|
|
3584
|
+
dashboardLink() {
|
|
3585
|
+
return call("POST", "/payments/dashboard-link", {});
|
|
3549
3586
|
}
|
|
3550
3587
|
};
|
|
3551
3588
|
}
|
|
@@ -3630,8 +3667,8 @@ function createStripeClient(options2 = {}) {
|
|
|
3630
3667
|
return new StripeClass(options2.apiKey, options2.config);
|
|
3631
3668
|
}
|
|
3632
3669
|
const settings = instanceGatewaySettings(options2, "Payments");
|
|
3633
|
-
const
|
|
3634
|
-
const protocol =
|
|
3670
|
+
const gateway2 = new URL(settings.gatewayUrl);
|
|
3671
|
+
const protocol = gateway2.protocol === "http:" ? "http" : "https";
|
|
3635
3672
|
return new StripeClass(
|
|
3636
3673
|
// No key: the authenticator below replaces it, and the gateway holds the
|
|
3637
3674
|
// real one. (stripe-node accepts an authenticator in place of a key.)
|
|
@@ -3639,8 +3676,8 @@ function createStripeClient(options2 = {}) {
|
|
|
3639
3676
|
{
|
|
3640
3677
|
maxNetworkRetries: 2,
|
|
3641
3678
|
...options2.config,
|
|
3642
|
-
host:
|
|
3643
|
-
port:
|
|
3679
|
+
host: gateway2.hostname,
|
|
3680
|
+
port: gateway2.port || (protocol === "http" ? 80 : 443),
|
|
3644
3681
|
protocol,
|
|
3645
3682
|
telemetry: false,
|
|
3646
3683
|
// The signed fetch sets `authorization`; nothing else may.
|
|
@@ -4745,6 +4782,182 @@ function unpackRouters(routers, recursiveBasePath = []) {
|
|
|
4745
4782
|
}, []);
|
|
4746
4783
|
}
|
|
4747
4784
|
|
|
4785
|
+
// src/http/mcpGenerator/mcpHttpHandler.ts
|
|
4786
|
+
import { WebStandardStreamableHTTPServerTransport } from "@modelcontextprotocol/sdk/server/webStandardStreamableHttp.js";
|
|
4787
|
+
import { AsyncLocalStorage as AsyncLocalStorage2 } from "async_hooks";
|
|
4788
|
+
import { randomUUID as randomUUID2 } from "crypto";
|
|
4789
|
+
var DEFAULT_MCP_SESSION_IDLE_TIMEOUT_MS = 15 * 60 * 1e3;
|
|
4790
|
+
var mcpTransportHeaders = new AsyncLocalStorage2();
|
|
4791
|
+
function getMcpTransportHeaders() {
|
|
4792
|
+
return mcpTransportHeaders.getStore();
|
|
4793
|
+
}
|
|
4794
|
+
function createMcpHttpHandler(mcpServer, options2) {
|
|
4795
|
+
const mode = options2?.sessions ?? "stateless";
|
|
4796
|
+
if (mode === "stateful" && (options2?.workerCount ?? 1) > 1) {
|
|
4797
|
+
throw new Error(
|
|
4798
|
+
"Stateful MCP sessions live in one process's memory and cannot be shared across workers. Use mcp.sessions: 'stateless' or a single worker."
|
|
4799
|
+
);
|
|
4800
|
+
}
|
|
4801
|
+
const idleTimeoutMs = options2?.sessionIdleTimeoutMs ?? DEFAULT_MCP_SESSION_IDLE_TIMEOUT_MS;
|
|
4802
|
+
const sessions = /* @__PURE__ */ new Map();
|
|
4803
|
+
const jsonRpcError = (status, code, message, headers) => new Response(
|
|
4804
|
+
JSON.stringify({ jsonrpc: "2.0", error: { code, message }, id: null }),
|
|
4805
|
+
{
|
|
4806
|
+
status,
|
|
4807
|
+
headers: { "Content-Type": "application/json", ...headers }
|
|
4808
|
+
}
|
|
4809
|
+
);
|
|
4810
|
+
let sweepTimer;
|
|
4811
|
+
if (mode === "stateful") {
|
|
4812
|
+
sweepTimer = setInterval(
|
|
4813
|
+
() => {
|
|
4814
|
+
const cutoff = Date.now() - idleTimeoutMs;
|
|
4815
|
+
for (const [id, entry] of sessions) {
|
|
4816
|
+
if (entry.lastSeen < cutoff) {
|
|
4817
|
+
sessions.delete(id);
|
|
4818
|
+
entry.session.close().catch(() => void 0);
|
|
4819
|
+
}
|
|
4820
|
+
}
|
|
4821
|
+
},
|
|
4822
|
+
Math.min(idleTimeoutMs, 6e4)
|
|
4823
|
+
);
|
|
4824
|
+
sweepTimer.unref();
|
|
4825
|
+
}
|
|
4826
|
+
async function authenticateRequest(authRequest) {
|
|
4827
|
+
if (!options2?.authenticate) {
|
|
4828
|
+
return { ok: true, auth: void 0 };
|
|
4829
|
+
}
|
|
4830
|
+
try {
|
|
4831
|
+
const auth = await options2.authenticate(authRequest);
|
|
4832
|
+
return auth ? { ok: true, auth } : { ok: false };
|
|
4833
|
+
} catch {
|
|
4834
|
+
return { ok: false };
|
|
4835
|
+
}
|
|
4836
|
+
}
|
|
4837
|
+
async function handleStateless(request, auth) {
|
|
4838
|
+
if (request.method !== "POST") {
|
|
4839
|
+
return jsonRpcError(405, -32e3, "Method not allowed", {
|
|
4840
|
+
Allow: "POST"
|
|
4841
|
+
});
|
|
4842
|
+
}
|
|
4843
|
+
const transport = new WebStandardStreamableHTTPServerTransport({
|
|
4844
|
+
sessionIdGenerator: void 0
|
|
4845
|
+
});
|
|
4846
|
+
const session = await mcpServer.connect(transport, auth);
|
|
4847
|
+
let closed = false;
|
|
4848
|
+
const closeSession = () => {
|
|
4849
|
+
if (!closed) {
|
|
4850
|
+
closed = true;
|
|
4851
|
+
session.close().catch(() => void 0);
|
|
4852
|
+
}
|
|
4853
|
+
};
|
|
4854
|
+
try {
|
|
4855
|
+
const response = await mcpTransportHeaders.run(
|
|
4856
|
+
request.headers,
|
|
4857
|
+
() => transport.handleRequest(request)
|
|
4858
|
+
);
|
|
4859
|
+
return closeWhenBodyEnds(response, closeSession);
|
|
4860
|
+
} catch (error) {
|
|
4861
|
+
closeSession();
|
|
4862
|
+
throw error;
|
|
4863
|
+
}
|
|
4864
|
+
}
|
|
4865
|
+
async function handleStateful(request, auth) {
|
|
4866
|
+
const sessionId = request.headers.get("mcp-session-id");
|
|
4867
|
+
if (sessionId) {
|
|
4868
|
+
const entry = sessions.get(sessionId);
|
|
4869
|
+
if (!entry) {
|
|
4870
|
+
return jsonRpcError(404, -32001, "Session not found");
|
|
4871
|
+
}
|
|
4872
|
+
entry.lastSeen = Date.now();
|
|
4873
|
+
if (auth) {
|
|
4874
|
+
entry.session.updateAuth(auth);
|
|
4875
|
+
}
|
|
4876
|
+
return mcpTransportHeaders.run(
|
|
4877
|
+
request.headers,
|
|
4878
|
+
() => entry.transport.handleRequest(request)
|
|
4879
|
+
);
|
|
4880
|
+
}
|
|
4881
|
+
const transport = new WebStandardStreamableHTTPServerTransport({
|
|
4882
|
+
sessionIdGenerator: () => randomUUID2(),
|
|
4883
|
+
onsessioninitialized: (id) => {
|
|
4884
|
+
sessions.set(id, { transport, session, lastSeen: Date.now() });
|
|
4885
|
+
},
|
|
4886
|
+
onsessionclosed: (id) => {
|
|
4887
|
+
sessions.delete(id);
|
|
4888
|
+
}
|
|
4889
|
+
});
|
|
4890
|
+
const session = await mcpServer.connect(transport, auth);
|
|
4891
|
+
transport.onclose = /* @__PURE__ */ ((onclose) => () => {
|
|
4892
|
+
if (transport.sessionId) {
|
|
4893
|
+
sessions.delete(transport.sessionId);
|
|
4894
|
+
}
|
|
4895
|
+
onclose?.();
|
|
4896
|
+
})(transport.onclose);
|
|
4897
|
+
const response = await mcpTransportHeaders.run(
|
|
4898
|
+
request.headers,
|
|
4899
|
+
() => transport.handleRequest(request)
|
|
4900
|
+
);
|
|
4901
|
+
if (!transport.sessionId) {
|
|
4902
|
+
await session.close();
|
|
4903
|
+
}
|
|
4904
|
+
return response;
|
|
4905
|
+
}
|
|
4906
|
+
async function handle(request, authRequest) {
|
|
4907
|
+
const authResult = await authenticateRequest(authRequest);
|
|
4908
|
+
if (!authResult.ok) {
|
|
4909
|
+
return jsonRpcError(401, -32001, "Unauthorized", {
|
|
4910
|
+
"WWW-Authenticate": "Bearer"
|
|
4911
|
+
});
|
|
4912
|
+
}
|
|
4913
|
+
return mode === "stateless" ? handleStateless(request, authResult.auth) : handleStateful(request, authResult.auth);
|
|
4914
|
+
}
|
|
4915
|
+
async function close() {
|
|
4916
|
+
if (sweepTimer) {
|
|
4917
|
+
clearInterval(sweepTimer);
|
|
4918
|
+
}
|
|
4919
|
+
const entries = [...sessions.values()];
|
|
4920
|
+
sessions.clear();
|
|
4921
|
+
await Promise.all(entries.map(({ session }) => session.close()));
|
|
4922
|
+
}
|
|
4923
|
+
return { handle, close, sessions };
|
|
4924
|
+
}
|
|
4925
|
+
function closeWhenBodyEnds(response, done) {
|
|
4926
|
+
if (!response.body) {
|
|
4927
|
+
done();
|
|
4928
|
+
return response;
|
|
4929
|
+
}
|
|
4930
|
+
const reader = response.body.getReader();
|
|
4931
|
+
const body = new ReadableStream({
|
|
4932
|
+
async pull(controller) {
|
|
4933
|
+
try {
|
|
4934
|
+
const { done: finished, value } = await reader.read();
|
|
4935
|
+
if (finished) {
|
|
4936
|
+
controller.close();
|
|
4937
|
+
done();
|
|
4938
|
+
} else {
|
|
4939
|
+
controller.enqueue(value);
|
|
4940
|
+
}
|
|
4941
|
+
} catch (error) {
|
|
4942
|
+
controller.error(error);
|
|
4943
|
+
done();
|
|
4944
|
+
}
|
|
4945
|
+
},
|
|
4946
|
+
cancel(reason) {
|
|
4947
|
+
done();
|
|
4948
|
+
return reader.cancel(reason);
|
|
4949
|
+
}
|
|
4950
|
+
});
|
|
4951
|
+
return new Response(body, {
|
|
4952
|
+
status: response.status,
|
|
4953
|
+
statusText: response.statusText,
|
|
4954
|
+
headers: response.headers
|
|
4955
|
+
});
|
|
4956
|
+
}
|
|
4957
|
+
function isSeparateMcpListener(configuredPort) {
|
|
4958
|
+
return process.env.MCP_LISTENER === "separate" || configuredPort != null || Boolean(Number(process.env.MCP_PORT));
|
|
4959
|
+
}
|
|
4960
|
+
|
|
4748
4961
|
// src/http/mcpGenerator/mcpGenerator.ts
|
|
4749
4962
|
function generateInputSchema(schemaValidator, body, params, query, requestHeaders, auth) {
|
|
4750
4963
|
let discriminatedBody;
|
|
@@ -4774,6 +4987,36 @@ function generateInputSchema(schemaValidator, body, params, query, requestHeader
|
|
|
4774
4987
|
} : {}
|
|
4775
4988
|
});
|
|
4776
4989
|
}
|
|
4990
|
+
function buildRouteHeaders(toolHeaders, contentType) {
|
|
4991
|
+
const transportHeaders = getMcpTransportHeaders();
|
|
4992
|
+
const routeHeaders = new Headers();
|
|
4993
|
+
const authorization = transportHeaders?.get("authorization");
|
|
4994
|
+
if (authorization) {
|
|
4995
|
+
routeHeaders.set("authorization", authorization);
|
|
4996
|
+
}
|
|
4997
|
+
for (const [name, value] of Object.entries(toolHeaders ?? {})) {
|
|
4998
|
+
if (value != null) {
|
|
4999
|
+
routeHeaders.set(name, String(value));
|
|
5000
|
+
}
|
|
5001
|
+
}
|
|
5002
|
+
const correlationId = transportHeaders?.get("x-correlation-id");
|
|
5003
|
+
if (correlationId) {
|
|
5004
|
+
routeHeaders.set("x-correlation-id", correlationId);
|
|
5005
|
+
}
|
|
5006
|
+
const traceparent = transportHeaders?.get("traceparent");
|
|
5007
|
+
if (traceparent) {
|
|
5008
|
+
routeHeaders.set("traceparent", traceparent);
|
|
5009
|
+
routeHeaders.delete("tracestate");
|
|
5010
|
+
const tracestate = transportHeaders?.get("tracestate");
|
|
5011
|
+
if (tracestate) {
|
|
5012
|
+
routeHeaders.set("tracestate", tracestate);
|
|
5013
|
+
}
|
|
5014
|
+
}
|
|
5015
|
+
if (contentType !== void 0) {
|
|
5016
|
+
routeHeaders.set("content-type", contentType);
|
|
5017
|
+
}
|
|
5018
|
+
return routeHeaders;
|
|
5019
|
+
}
|
|
4777
5020
|
function generateMcpServer(schemaValidator, protocol, host, port, version, application, appOptions, options2, contentTypeMap, authenticate) {
|
|
4778
5021
|
if (!(schemaValidator instanceof ZodSchemaValidator)) {
|
|
4779
5022
|
throw new Error(
|
|
@@ -4926,12 +5169,10 @@ function generateMcpServer(schemaValidator, protocol, host, port, version, appli
|
|
|
4926
5169
|
}
|
|
4927
5170
|
const response = await fetch(url, {
|
|
4928
5171
|
method: route.method.toUpperCase(),
|
|
4929
|
-
headers:
|
|
4930
|
-
|
|
4931
|
-
|
|
4932
|
-
|
|
4933
|
-
} : {}
|
|
4934
|
-
},
|
|
5172
|
+
headers: buildRouteHeaders(
|
|
5173
|
+
headers,
|
|
5174
|
+
discriminatedBody?.contentType != "multipart/form-data" ? contentType ?? discriminatedBody?.contentType : void 0
|
|
5175
|
+
),
|
|
4935
5176
|
body: parsedBody
|
|
4936
5177
|
});
|
|
4937
5178
|
if (response.status >= 300) {
|
|
@@ -5767,6 +6008,134 @@ function registerDelegatedSurface(surface, mount, register) {
|
|
|
5767
6008
|
}
|
|
5768
6009
|
return register(mount);
|
|
5769
6010
|
}
|
|
6011
|
+
|
|
6012
|
+
// src/http/localModelGatewayClient.ts
|
|
6013
|
+
import { execFile } from "child_process";
|
|
6014
|
+
import { promisify } from "util";
|
|
6015
|
+
var execute = promisify(execFile);
|
|
6016
|
+
var gateway = "https://studio-orchestrator.forklaunch.com/studio-orchestrator/desktop-runtime-model-gateway";
|
|
6017
|
+
function validate(value) {
|
|
6018
|
+
if (!value || !/^[a-f0-9]{64}$/.test(value.grantId) || !/^[a-f0-9]{64}$/.test(value.capability) || value.gatewayBaseUrl !== `${gateway}/${value.grantId}/v1` || value.scope !== "local-runtime" || !Number.isSafeInteger(value.expiresAt) || value.expiresAt <= Date.now() || value.expiresAt > Date.now() + 9e5 || !/^[A-Za-z0-9._-]{1,128}$/.test(value.model) || !Number.isInteger(value.maxCalls) || value.maxCalls < 1 || value.maxCalls > 20 || !Number.isInteger(value.maxUnits) || value.maxUnits < 1 || value.maxUnits > 1e5) {
|
|
6019
|
+
throw new Error(
|
|
6020
|
+
"Invalid local model connection. No model request was sent."
|
|
6021
|
+
);
|
|
6022
|
+
}
|
|
6023
|
+
return value;
|
|
6024
|
+
}
|
|
6025
|
+
function createLocalModelGatewayClient(options2 = {}) {
|
|
6026
|
+
if (process.env.FORKLAUNCH_API_KEY === void 0 && !options2.credentialProvider && process.env.FORKLAUNCH_LOCAL_MODEL_RELAY === "1") {
|
|
6027
|
+
return createPreviewModelGatewayClient({ fetch: options2.fetch });
|
|
6028
|
+
}
|
|
6029
|
+
const applicationId = options2.applicationId ?? process.env.FORKLAUNCH_APPLICATION_ID;
|
|
6030
|
+
const model = options2.model ?? process.env.AGENT_MODEL;
|
|
6031
|
+
if (applicationId && !/^[a-f0-9-]{36}$/i.test(applicationId)) {
|
|
6032
|
+
throw new Error(
|
|
6033
|
+
"FORKLAUNCH_APPLICATION_ID must be a saved application UUID."
|
|
6034
|
+
);
|
|
6035
|
+
}
|
|
6036
|
+
const provider = options2.credentialProvider ?? (async () => {
|
|
6037
|
+
const args = ["model", "credential", "--json"];
|
|
6038
|
+
if (applicationId) args.push("--application-id", applicationId);
|
|
6039
|
+
if (model) args.push("--model", model);
|
|
6040
|
+
try {
|
|
6041
|
+
const { stdout } = await execute("forklaunch", args, {
|
|
6042
|
+
timeout: 35e3,
|
|
6043
|
+
maxBuffer: 65536,
|
|
6044
|
+
windowsHide: true
|
|
6045
|
+
});
|
|
6046
|
+
return JSON.parse(stdout);
|
|
6047
|
+
} catch {
|
|
6048
|
+
throw new Error(
|
|
6049
|
+
"Could not connect the local agent. Run forklaunch login, check app access, and use a CLI that supports model credential."
|
|
6050
|
+
);
|
|
6051
|
+
}
|
|
6052
|
+
});
|
|
6053
|
+
const optionsFetch = options2.fetch ?? fetch;
|
|
6054
|
+
let pending;
|
|
6055
|
+
let credential;
|
|
6056
|
+
async function connection() {
|
|
6057
|
+
if (credential && credential.expiresAt > Date.now()) return credential;
|
|
6058
|
+
pending ??= provider().then(validate).then((value) => credential = value).finally(() => {
|
|
6059
|
+
pending = void 0;
|
|
6060
|
+
});
|
|
6061
|
+
return pending;
|
|
6062
|
+
}
|
|
6063
|
+
return {
|
|
6064
|
+
async models() {
|
|
6065
|
+
const c = await connection();
|
|
6066
|
+
return {
|
|
6067
|
+
models: [{ id: c.model }],
|
|
6068
|
+
preview: {
|
|
6069
|
+
expiresAt: c.expiresAt,
|
|
6070
|
+
maxCalls: c.maxCalls,
|
|
6071
|
+
maxUnits: c.maxUnits
|
|
6072
|
+
}
|
|
6073
|
+
};
|
|
6074
|
+
},
|
|
6075
|
+
chat: {
|
|
6076
|
+
completions: {
|
|
6077
|
+
async *stream(request, options3 = {}) {
|
|
6078
|
+
const c = await connection();
|
|
6079
|
+
if (request.model !== c.model)
|
|
6080
|
+
throw new ModelGatewayRequestError(
|
|
6081
|
+
400,
|
|
6082
|
+
"Choose the model authorized for this local session."
|
|
6083
|
+
);
|
|
6084
|
+
const response = await optionsFetch(
|
|
6085
|
+
`${c.gatewayBaseUrl}/chat/completions`,
|
|
6086
|
+
{
|
|
6087
|
+
method: "POST",
|
|
6088
|
+
redirect: "error",
|
|
6089
|
+
signal: options3.signal ? AbortSignal.any([options3.signal, AbortSignal.timeout(6e4)]) : AbortSignal.timeout(6e4),
|
|
6090
|
+
headers: {
|
|
6091
|
+
authorization: `Bearer ${c.capability}`,
|
|
6092
|
+
"content-type": "application/json"
|
|
6093
|
+
},
|
|
6094
|
+
body: JSON.stringify({ ...request, stream: true })
|
|
6095
|
+
}
|
|
6096
|
+
);
|
|
6097
|
+
if (!response.ok) {
|
|
6098
|
+
await response.body?.cancel();
|
|
6099
|
+
throw new ModelGatewayRequestError(
|
|
6100
|
+
response.status,
|
|
6101
|
+
"The local model stream was refused. No automatic retry was sent."
|
|
6102
|
+
);
|
|
6103
|
+
}
|
|
6104
|
+
yield* readModelStream(response);
|
|
6105
|
+
},
|
|
6106
|
+
async create(request) {
|
|
6107
|
+
const c = await connection();
|
|
6108
|
+
if (request.model !== c.model)
|
|
6109
|
+
throw new ModelGatewayRequestError(
|
|
6110
|
+
400,
|
|
6111
|
+
"Choose the model authorized for this local session."
|
|
6112
|
+
);
|
|
6113
|
+
const response = await (options2.fetch ?? fetch)(
|
|
6114
|
+
`${c.gatewayBaseUrl}/chat/completions`,
|
|
6115
|
+
{
|
|
6116
|
+
method: "POST",
|
|
6117
|
+
redirect: "error",
|
|
6118
|
+
signal: AbortSignal.timeout(6e4),
|
|
6119
|
+
headers: {
|
|
6120
|
+
authorization: `Bearer ${c.capability}`,
|
|
6121
|
+
"content-type": "application/json"
|
|
6122
|
+
},
|
|
6123
|
+
body: JSON.stringify({ ...request, stream: false })
|
|
6124
|
+
}
|
|
6125
|
+
);
|
|
6126
|
+
if (!response.ok) {
|
|
6127
|
+
await response.body?.cancel();
|
|
6128
|
+
throw new ModelGatewayRequestError(
|
|
6129
|
+
response.status,
|
|
6130
|
+
"The local model request was refused or failed. No automatic retry was sent."
|
|
6131
|
+
);
|
|
6132
|
+
}
|
|
6133
|
+
return response.json();
|
|
6134
|
+
}
|
|
6135
|
+
}
|
|
6136
|
+
}
|
|
6137
|
+
};
|
|
6138
|
+
}
|
|
5770
6139
|
export {
|
|
5771
6140
|
ATTR_API_NAME,
|
|
5772
6141
|
ATTR_APPLICATION_ID,
|
|
@@ -5776,6 +6145,7 @@ export {
|
|
|
5776
6145
|
ATTR_HTTP_ROUTE,
|
|
5777
6146
|
ATTR_SERVICE_NAME,
|
|
5778
6147
|
AuditLogger,
|
|
6148
|
+
DEFAULT_MCP_SESSION_IDLE_TIMEOUT_MS,
|
|
5779
6149
|
EMAIL_LIMITS,
|
|
5780
6150
|
EmailRequestError,
|
|
5781
6151
|
ForklaunchExpressLikeApplication,
|
|
@@ -5809,6 +6179,8 @@ export {
|
|
|
5809
6179
|
createEmailClient,
|
|
5810
6180
|
createHmacToken,
|
|
5811
6181
|
createInstanceGatewayTransport,
|
|
6182
|
+
createLocalModelGatewayClient,
|
|
6183
|
+
createMcpHttpHandler,
|
|
5812
6184
|
createModelGatewayClient,
|
|
5813
6185
|
createPaymentsClient,
|
|
5814
6186
|
createPreviewModelGatewayClient,
|
|
@@ -5829,6 +6201,7 @@ export {
|
|
|
5829
6201
|
get,
|
|
5830
6202
|
getCachedJwks,
|
|
5831
6203
|
getCodeForStatus,
|
|
6204
|
+
getMcpTransportHeaders,
|
|
5832
6205
|
head,
|
|
5833
6206
|
httpErrorsTotalCounter,
|
|
5834
6207
|
httpRequestDurationMsHistogram,
|
|
@@ -5845,6 +6218,7 @@ export {
|
|
|
5845
6218
|
isManagedInstance,
|
|
5846
6219
|
isPortBound,
|
|
5847
6220
|
isRedirection,
|
|
6221
|
+
isSeparateMcpListener,
|
|
5848
6222
|
isServerError,
|
|
5849
6223
|
isSuccessful,
|
|
5850
6224
|
isValidStatusCode,
|