mslxdff 0.1.38 → 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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/upstream.js +23 -3
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mslxdff",
3
- "version": "0.1.38",
3
+ "version": "0.1.39",
4
4
  "description": "测试项目,请勿使用。",
5
5
  "type": "module",
6
6
  "bin": {
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 headers = {
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
- return { chat, headers };
102
+ // 兼容旧调用:headers 为动态生成,暴露 getter 快照(用于测试/展示)
103
+ const headers = buildHeaders({});
104
+ return { chat, headers, buildHeaders };
85
105
  }
86
106
 
87
107
  function sleep(ms) {