mslxdff 0.1.37 → 0.1.39
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/mslxdff.js +3 -1
- package/package.json +1 -1
- package/src/routes.js +2 -1
- package/src/upstream.js +23 -3
package/bin/mslxdff.js
CHANGED
|
@@ -1199,8 +1199,10 @@ function fmtEvent(e) {
|
|
|
1199
1199
|
return `${head} peer race 开始并发给组员 model=${m(e.model)} peers=${e.peers}`;
|
|
1200
1200
|
case "peer-health":
|
|
1201
1201
|
return `${head} peer check ${e.peer} -> ${e.count ? e.healthy.join(", ") : "no healthy models"}${e.strict ? " (strict)" : ""}`;
|
|
1202
|
+
case "peer-request":
|
|
1203
|
+
return `${head} peer req 并发发出 peer=${e.peer} model=${m(e.model)} hops=${e.hops}`;
|
|
1202
1204
|
case "peer-forward":
|
|
1203
|
-
return `${head} peer
|
|
1205
|
+
return `${head} peer resp 收到响应 peer=${e.peer} model=${m(e.model)} ${e.ok ? "ok" : "fail"} ${e.latencyMs ? `${e.latencyMs}ms` : ""} hops=${e.hops}${e.retry ? " (retry)" : ""}`;
|
|
1204
1206
|
case "peer-error":
|
|
1205
1207
|
return `${head} peer err ${e.peer} ${e.status ? `HTTP ${e.status}` : "network"}: ${m(e.message)} model=${m(e.model)}`;
|
|
1206
1208
|
case "peer-race-win":
|
package/package.json
CHANGED
package/src/routes.js
CHANGED
|
@@ -643,11 +643,12 @@ async function racePeerCandidates(candidates, ctx) {
|
|
|
643
643
|
const order = [];
|
|
644
644
|
const total = prepared.length;
|
|
645
645
|
for (const { peer, target } of prepared) {
|
|
646
|
+
ctx.evt("peer-request", { peer: peer.url, model: target, hops: ctx.hops + 1 });
|
|
646
647
|
const t0 = performance.now();
|
|
647
648
|
forwardToPeer(peer, ctx.body, target, ctx.hops).then((res) => {
|
|
648
649
|
const latencyMs = Math.round(performance.now() - t0);
|
|
649
650
|
const failed = res instanceof Error || res.status >= 400;
|
|
650
|
-
ctx.evt("peer-forward", { peer: peer.url, model: target, hops: ctx.hops + 1 });
|
|
651
|
+
ctx.evt("peer-forward", { peer: peer.url, model: target, hops: ctx.hops + 1, latencyMs, ok: !failed });
|
|
651
652
|
if (failed) {
|
|
652
653
|
const status = res instanceof Error ? 502 : res.status;
|
|
653
654
|
ctx.logError(ctx.model,
|
package/src/upstream.js
CHANGED
|
@@ -1,3 +1,9 @@
|
|
|
1
|
+
import crypto from "node:crypto";
|
|
2
|
+
|
|
3
|
+
function genId(prefix) {
|
|
4
|
+
return `${prefix}${crypto.randomUUID().replace(/-/g, "")}`;
|
|
5
|
+
}
|
|
6
|
+
|
|
1
7
|
export function createUpstreamClient({
|
|
2
8
|
baseUrl = process.env.UPSTREAM_BASE_URL || "https://opencode.ai",
|
|
3
9
|
authToken = process.env.UPSTREAM_AUTH_TOKEN || "public",
|
|
@@ -11,13 +17,24 @@ export function createUpstreamClient({
|
|
|
11
17
|
},
|
|
12
18
|
fetchImpl = fetch,
|
|
13
19
|
} = {}) {
|
|
14
|
-
const
|
|
20
|
+
const baseHeaders = {
|
|
15
21
|
"Content-Type": "application/json",
|
|
16
22
|
"Authorization": `Bearer ${authToken}`,
|
|
17
23
|
"x-opencode-client": "desktop",
|
|
18
|
-
"Accept": "text/event-stream",
|
|
19
24
|
};
|
|
20
25
|
|
|
26
|
+
function buildHeaders(body) {
|
|
27
|
+
const isStream = body?.stream !== false;
|
|
28
|
+
return {
|
|
29
|
+
...baseHeaders,
|
|
30
|
+
"Accept": isStream ? "text/event-stream" : "*/*",
|
|
31
|
+
"User-Agent": "opencode",
|
|
32
|
+
"x-opencode-session": genId("ses_"),
|
|
33
|
+
"x-opencode-request": genId("msg_"),
|
|
34
|
+
"x-opencode-project": "global",
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
|
|
21
38
|
async function chat(body) {
|
|
22
39
|
const url = `${baseUrl}/zen/v1/chat/completions`;
|
|
23
40
|
const t0 = performance.now();
|
|
@@ -67,6 +84,7 @@ export function createUpstreamClient({
|
|
|
67
84
|
connectTimeoutMs
|
|
68
85
|
);
|
|
69
86
|
try {
|
|
87
|
+
const headers = buildHeaders(body);
|
|
70
88
|
const res = await fetchImpl(url, {
|
|
71
89
|
method: "POST",
|
|
72
90
|
headers,
|
|
@@ -81,7 +99,9 @@ export function createUpstreamClient({
|
|
|
81
99
|
}
|
|
82
100
|
}
|
|
83
101
|
|
|
84
|
-
|
|
102
|
+
// 兼容旧调用:headers 为动态生成,暴露 getter 快照(用于测试/展示)
|
|
103
|
+
const headers = buildHeaders({});
|
|
104
|
+
return { chat, headers, buildHeaders };
|
|
85
105
|
}
|
|
86
106
|
|
|
87
107
|
function sleep(ms) {
|