@y1x1n/dsh-prompt-optimizer 0.3.16 → 0.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/lib/index.js CHANGED
@@ -7,8 +7,8 @@ var MARKERS = {
7
7
  optimized: "<<<OPTIMIZED>>>",
8
8
  end: "<<<END>>>"
9
9
  };
10
- var ROLE_FULL_ZH = "\u4F60\u662F\u4E00\u4F4D\u8D44\u6DF1\u63D0\u793A\u8BCD\u5DE5\u7A0B\u4E13\u5BB6\u3002\u7528\u6237\u4F1A\u7ED9\u4F60\u4E00\u6BB5\u300C\u63D0\u793A\u8BCD\u8349\u7A3F\u300D,\u4F60\u8981\u5B8C\u6210\u4E24\u4EF6\u4E8B:\u8BCA\u65AD\u5206\u6790 + \u4F18\u5316\u6539\u5199\u3002";
11
- var ROLE_FULL_EN = 'You are a senior prompt engineering expert. Given a "prompt draft" from the user, do two things: diagnose it, then rewrite an optimized version.';
10
+ var ROLE_FULL_ZH = "\u4F60\u662F\u4E00\u4F4D\u8D44\u6DF1\u63D0\u793A\u8BCD\u5DE5\u7A0B\u4E13\u5BB6\u3002\u7528\u6237\u4F1A\u7ED9\u4F60\u4E00\u6BB5\u300C\u63D0\u793A\u8BCD\u8349\u7A3F\u300D,\u4F60\u8981\u8F93\u51FA\u4E24\u6BB5\u5185\u5BB9:\u5148\u8F93\u51FA\u4F18\u5316\u6539\u5199\u7A3F,\u518D\u8F93\u51FA\u8BCA\u65AD\u5206\u6790\u3002";
11
+ var ROLE_FULL_EN = 'You are a senior prompt engineering expert. Given a "prompt draft" from the user, output the optimized rewrite first, then the diagnosis.';
12
12
  var ROLE_FAST_ZH = "\u4F60\u662F\u4E00\u4F4D\u8D44\u6DF1\u63D0\u793A\u8BCD\u5DE5\u7A0B\u4E13\u5BB6\u3002\u7528\u6237\u4F1A\u7ED9\u4F60\u4E00\u6BB5\u300C\u63D0\u793A\u8BCD\u8349\u7A3F\u300D,\u8BF7\u76F4\u63A5\u8F93\u51FA\u4F18\u5316\u540E\u7684\u5B8C\u6574\u63D0\u793A\u8BCD,\u4E0D\u8981\u505A\u4EFB\u4F55\u8BCA\u65AD\u5206\u6790\u3002";
13
13
  var ROLE_FAST_EN = 'You are a senior prompt engineering expert. Given a "prompt draft" from the user, output an optimized version directly \u2014 no diagnosis.';
14
14
  var ANALYSIS_ZH = `# \u5206\u6790\u7EF4\u5EA6
@@ -56,14 +56,14 @@ var RULES_ZH = `# \u4FDD\u771F\u4E0E\u660E\u786E\u5316\u539F\u5219(\u5168\u7A0B\
56
56
  - \u8BED\u4E49\u7B49\u4EF7\u662F\u5E95\u7EBF:\u5DF2\u660E\u786E\u7684\u5BF9\u8C61\u3001\u65B9\u5411\u3001\u6570\u91CF\u3001\u8303\u56F4\u3001\u7981\u6B62\u9879\u3001\u672F\u8BED\u5FC5\u987B\u4E0E\u8349\u7A3F\u4E00\u81F4,\u4E0D\u5F97\u66FF\u6362\u3001\u6269\u5927\u3001\u7F29\u5C0F\u6216\u98A0\u5012
57
57
  - \u6765\u6E90\u53EF\u56DE\u6EAF:\u4EFB\u4F55\u5177\u4F53\u5316\u7684\u5185\u5BB9\u90FD\u5FC5\u987B\u80FD\u4ECE\u8349\u7A3F\u6216\u4E0A\u4E0B\u6587\u5DF2\u786E\u8BA4\u4FE1\u606F\u627E\u5230\u4F9D\u636E;\u63A8\u65AD\u5904\u7528\u300C\u5982\u65E0\u7279\u522B\u8BF4\u660E/\u9ED8\u8BA4\u300D\u63AA\u8F9E\u4FDD\u7559\u9009\u62E9\u6743,\u65E0\u4F9D\u636E\u7684\u5B81\u53EF\u4E0D\u8865
58
58
  - \u4FDD\u771F\u81EA\u68C0:\u8F93\u51FA\u524D\u9010\u8981\u7D20\u6838\u5BF9\u2014\u2014\u8349\u7A3F\u7684\u6BCF\u4E2A\u539F\u5B50\u4FE1\u606F\u90FD\u8981\u5728\u4F18\u5316\u7A3F\u4E2D\u627E\u5230\u5BF9\u5E94,\u627E\u4E0D\u5230\u5373\u4E3A\u8BED\u4E49\u6F02\u79FB,\u5FC5\u987B\u4FEE\u6B63
59
- - \u957F\u5EA6\u7EAA\u5F8B:\u7B80\u5355\u4EFB\u52A1\u7684\u4F18\u5316\u7A3F\u63A7\u5236\u5728 800 \u5B57\u7B26\u4EE5\u5185,\u590D\u6742\u4EFB\u52A1\u53EF\u8D85\u51FA;\u4EFB\u4F55\u60C5\u51B5\u4E0B\u4FDD\u771F\u81EA\u68C0\u4F18\u5148\u4E8E\u957F\u5EA6\u2014\u2014\u8981\u7D20\u7F3A\u5931\u6BD4\u5197\u957F\u66F4\u4E25\u91CD,\u4F46\u4E5F\u4E0D\u5F97\u5197\u4F59
59
+ - \u957F\u5EA6\u7EAA\u5F8B:\u7B80\u5355\u4EFB\u52A1\u7684\u4F18\u5316\u7A3F\u63A7\u5236\u5728 500 \u5B57\u7B26\u4EE5\u5185,\u590D\u6742\u4EFB\u52A1\u53EF\u8D85\u51FA;\u4F18\u5316\u7A3F\u5C31\u662F\u63D0\u793A\u8BCD\u672C\u8EAB\u2014\u2014\u4E0D\u590D\u8FF0\u539F\u7A3F\u3001\u4E0D\u52A0\u591A\u4F59\u7684\u89E3\u91CA\u6216\u524D\u8A00;\u4EFB\u4F55\u60C5\u51B5\u4E0B\u4FDD\u771F\u81EA\u68C0\u4F18\u5148\u4E8E\u957F\u5EA6\u2014\u2014\u8981\u7D20\u7F3A\u5931\u6BD4\u5197\u957F\u66F4\u4E25\u91CD,\u4F46\u4E5F\u4E0D\u5F97\u5197\u4F59
60
60
  - \u8BED\u8A00\u5339\u914D:\u8349\u7A3F\u4EE5\u4E2D\u6587\u4E3A\u4E3B\u4F53\u5219\u8F93\u51FA\u5FC5\u987B\u4E3A\u4E2D\u6587,\u4EE5\u82F1\u6587\u4E3A\u4E3B\u4F53\u5219\u8F93\u51FA\u5FC5\u987B\u4E3A\u82F1\u6587;\u4FDD\u7559\u539F\u6587\u7684\u672F\u8BED\u4E0E\u4E13\u6709\u540D\u8BCD`;
61
61
  var RULES_EN = `# Fidelity & clarification rules (always apply)
62
62
  - Understand first: list what the draft explicitly states (subject, action, constraints, scope, terms, numbers, tone), and separate it from your guesses \u2014 guesses may shape wording only, never enter the output
63
63
  - Semantic equivalence is the floor: what the draft states (subject, direction, quantities, scope, prohibitions, terms) must survive intact \u2014 never replace, widen, narrow, or invert it
64
64
  - Traceability: every concretized detail must trace back to the draft or to information the context has confirmed; mark inferences with "unless otherwise specified / by default" phrasing; when in doubt, leave it out
65
65
  - Fidelity self-check: before output, verify every atomic element of the draft has a counterpart in your rewrite \u2014 a missing one is semantic drift and must be fixed
66
- - Length discipline: keep the rewrite under 800 characters for simple tasks; longer is allowed for complex ones. Fidelity always outranks length \u2014 a missing element is worse than verbosity, but never pad
66
+ - Length discipline: keep the rewrite under 500 characters for simple tasks; longer is allowed for complex ones. The rewrite is the prompt itself \u2014 never restate the draft or add explanations or preamble. Fidelity always outranks length \u2014 a missing element is worse than verbosity, but never pad
67
67
  - Language match: Chinese-dominant draft \u2192 Chinese output; English-dominant \u2192 English output; keep the draft's terms and proper nouns`;
68
68
  var EXAMPLE_TEMPLATE_ZH = `# \u793A\u4F8B(\u4E25\u683C\u6A21\u4EFF\u5176\u300C\u8F93\u5165 \u2192 \u8F93\u51FA\u300D\u7684\u98CE\u683C)
69
69
  \u8F93\u5165:\u5E2E\u6211\u5199\u4E2A\u8BF7\u5047\u6761
@@ -78,18 +78,18 @@ var EXAMPLE_INTENT_EN = `# Example (strictly mirror its "input \u2192 output" st
78
78
  Input: (background: the user runs a 200-member neighborhood parents' swap group; the first chain-post drew only a dozen joins, blamed on weak prizes and a messy format) redesign the chain post for me
79
79
  Output: Redesign the "hidden gems" chain post for my 200-member neighborhood swap group (mostly parents): last round drew only a dozen joins because prizes were unappealing and the format confusing \u2014 provide a simple chain-post template, low-cost but attractive prize ideas, and concrete tactics to lift participation.`;
80
80
  var FORMAT_FULL_ZH = `# \u8F93\u51FA\u683C\u5F0F(\u4E25\u683C\u9075\u5B88,\u6807\u8BB0\u884C\u5355\u72EC\u6210\u884C)
81
- ${MARKERS.analysis}
82
- \u6309\u7EF4\u5EA6\u9010\u6761\u8F93\u51FA\u8BCA\u65AD,\u6BCF\u6761\u4E00\u884C,\u683C\u5F0F:\u300C\u7EF4\u5EA6 | \u95EE\u9898 | \u5EFA\u8BAE\u300D\u3002\u67D0\u7EF4\u5EA6\u65E0\u660E\u663E\u95EE\u9898\u65F6\u5199:\u300C\u7EF4\u5EA6 | \u65E0\u660E\u663E\u95EE\u9898 | -\u300D\u3002
83
81
  ${MARKERS.optimized}
84
82
  \u8F93\u51FA\u4F18\u5316\u540E\u7684\u5B8C\u6574\u63D0\u793A\u8BCD\u5168\u6587
83
+ ${MARKERS.analysis}
84
+ \u6309\u7EF4\u5EA6\u9010\u6761\u8F93\u51FA\u8BCA\u65AD,\u6BCF\u6761\u4E00\u884C,\u683C\u5F0F:\u300C\u7EF4\u5EA6 | \u95EE\u9898 | \u5EFA\u8BAE\u300D\u3002\u67D0\u7EF4\u5EA6\u65E0\u660E\u663E\u95EE\u9898\u65F6\u5199:\u300C\u7EF4\u5EA6 | \u65E0\u660E\u663E\u95EE\u9898 | -\u300D\u3002
85
85
  ${MARKERS.end}
86
86
 
87
87
  \u9664\u4E0A\u8FF0\u6807\u8BB0\u5305\u88F9\u7684\u5185\u5BB9\u5916,\u4E0D\u8981\u8F93\u51FA\u4EFB\u4F55\u5176\u4ED6\u6587\u5B57\u3002`;
88
88
  var FORMAT_FULL_EN = `# Output format (strictly follow; markers on their own lines)
89
- ${MARKERS.analysis}
90
- One finding per line: "dimension | issue | suggestion". If a dimension is fine: "dimension | no issue | -".
91
89
  ${MARKERS.optimized}
92
90
  The full optimized prompt text
91
+ ${MARKERS.analysis}
92
+ One finding per line: "dimension | issue | suggestion". If a dimension is fine: "dimension | no issue | -".
93
93
  ${MARKERS.end}
94
94
 
95
95
  Do not output anything outside these markers.`;
@@ -182,12 +182,26 @@ ${draft}`
182
182
  );
183
183
  return sections.join("\n\n");
184
184
  }
185
+ function isWideTokenChar(code) {
186
+ return code >= 4352 && code <= 4607 || // 谚文字母
187
+ code >= 11904 && code <= 12255 || // CJK 部首补充 / 康熙部首
188
+ code >= 12288 && code <= 12351 || // CJK 符号与标点(全角)
189
+ code >= 12352 && code <= 12543 || // 平假名 / 片假名
190
+ code >= 12592 && code <= 12687 || // 谚文兼容字母
191
+ code >= 13312 && code <= 19903 || // CJK 扩展 A
192
+ code >= 19968 && code <= 40959 || // CJK 统一表意文字
193
+ code >= 43360 && code <= 43391 || // 谚文字母扩展 A
194
+ code >= 44032 && code <= 55215 || // 谚文音节
195
+ code >= 63744 && code <= 64255 || // CJK 兼容表意文字
196
+ code >= 65280 && code <= 65376 || // 全角 ASCII 变体
197
+ code >= 65504 && code <= 65510 || // 全角符号
198
+ code >= 131072 && code <= 262143;
199
+ }
185
200
  function estimateTokens(text) {
186
201
  let cjk = 0;
187
202
  let other = 0;
188
203
  for (const ch of text) {
189
- const code = ch.codePointAt(0) ?? 0;
190
- if (code >= 13312 && code <= 19903 || code >= 19968 && code <= 40959 || code >= 63744 && code <= 64255) {
204
+ if (isWideTokenChar(ch.codePointAt(0) ?? 0)) {
191
205
  cjk += 1;
192
206
  } else {
193
207
  other += 1;
@@ -201,27 +215,110 @@ function findMarker(text, word, fromIndex = 0) {
201
215
  const match = re.exec(text);
202
216
  return match ? { index: match.index, length: match[0].length } : null;
203
217
  }
218
+ function findLastMarker(text, word, beforeIndex = text.length) {
219
+ const re = new RegExp(`<<<\\s*${word}\\s*>>>`, "g");
220
+ let last = null;
221
+ for (const match of text.matchAll(re)) {
222
+ if (match.index >= beforeIndex) break;
223
+ last = { index: match.index, length: match[0].length };
224
+ }
225
+ return last;
226
+ }
227
+ function stripMarkers(text) {
228
+ return text.replace(/<<<\s*(?:ANALYSIS|OPTIMIZED|END)\s*>>>/g, "");
229
+ }
230
+ function findNextMarker(text, fromIndex) {
231
+ const re = /<<<\s*(?:ANALYSIS|OPTIMIZED|END)\s*>>>/g;
232
+ re.lastIndex = fromIndex;
233
+ const match = re.exec(text);
234
+ return match ? { index: match.index, length: match[0].length } : null;
235
+ }
236
+ function segmentAfter(text, marker) {
237
+ const firstEnd = findMarker(text, "END");
238
+ if (firstEnd && marker.index > firstEnd.index) return "";
239
+ const start = marker.index + marker.length;
240
+ const next = findNextMarker(text, start);
241
+ let end = next ? next.index : text.length;
242
+ if (firstEnd && firstEnd.index > start && firstEnd.index < end) end = firstEnd.index;
243
+ return text.slice(start, end).trim();
244
+ }
245
+ var THINK_OPEN = "<think>";
246
+ var THINK_CLOSE = "</think>";
247
+ function stripThinkBlocks(text) {
248
+ let out = "";
249
+ let rest = text;
250
+ for (; ; ) {
251
+ const open = rest.indexOf(THINK_OPEN);
252
+ if (open === -1) return out + rest;
253
+ out += rest.slice(0, open);
254
+ const close = rest.indexOf(THINK_CLOSE, open + THINK_OPEN.length);
255
+ if (close === -1) return out;
256
+ rest = rest.slice(close + THINK_CLOSE.length);
257
+ }
258
+ }
259
+ function createThinkFilter() {
260
+ let buf = "";
261
+ let inThink = false;
262
+ const splitSafeTail = (tagLen) => {
263
+ const lastLt = buf.lastIndexOf("<");
264
+ const keepFrom = lastLt !== -1 && buf.length - lastLt <= tagLen - 1 ? lastLt : buf.length;
265
+ return [buf.slice(0, keepFrom), buf.slice(keepFrom)];
266
+ };
267
+ const push = (chunk) => {
268
+ buf += chunk;
269
+ let out = "";
270
+ for (; ; ) {
271
+ if (inThink) {
272
+ const close = buf.indexOf(THINK_CLOSE);
273
+ if (close === -1) {
274
+ const [emit, keep] = splitSafeTail(THINK_CLOSE.length);
275
+ buf = keep;
276
+ return out;
277
+ }
278
+ buf = buf.slice(close + THINK_CLOSE.length);
279
+ inThink = false;
280
+ continue;
281
+ }
282
+ const open = buf.indexOf(THINK_OPEN);
283
+ if (open === -1) {
284
+ const [emit, keep] = splitSafeTail(THINK_OPEN.length);
285
+ out += emit;
286
+ buf = keep;
287
+ return out;
288
+ }
289
+ out += buf.slice(0, open);
290
+ buf = buf.slice(open + THINK_OPEN.length);
291
+ inThink = true;
292
+ }
293
+ };
294
+ const flush = () => {
295
+ if (inThink) {
296
+ buf = "";
297
+ return "";
298
+ }
299
+ const rest = buf;
300
+ buf = "";
301
+ return rest;
302
+ };
303
+ return { push, flush };
304
+ }
204
305
  function parseOptimizerOutput(raw, mode = "full") {
205
- const text = raw.trim();
206
- const a = findMarker(text, "ANALYSIS");
207
- const o = findMarker(text, "OPTIMIZED");
306
+ const text = stripThinkBlocks(raw).trim();
307
+ const o = findLastMarker(text, "OPTIMIZED");
308
+ const a = findLastMarker(text, "ANALYSIS");
208
309
  const wellFormed = mode === "fast" ? true : Boolean(a && o);
209
310
  if (!a && !o) {
210
- return { analysis: "", optimized: text, wellFormed };
211
- }
212
- let analysis = "";
213
- let optimized = "";
214
- if (a && (!o || a.index < o.index)) {
215
- analysis = text.slice(a.index + a.length, o ? o.index : text.length).trim();
216
- }
217
- if (o) {
218
- const e = findMarker(text, "END", o.index + o.length);
219
- optimized = text.slice(o.index + o.length, e ? e.index : text.length).trim();
311
+ const body = stripMarkers(text).trim();
312
+ return { analysis: "", optimized: body, wellFormed: wellFormed && Boolean(body) };
220
313
  }
314
+ const optimized = o ? segmentAfter(text, o) : "";
315
+ const analysis = a ? segmentAfter(text, a) : "";
221
316
  return {
317
+ // 有标记但段落为空:保持为空(可能只是模型没写内容),绝不回退到含标记的原文。
222
318
  analysis,
223
- optimized: optimized || text,
224
- wellFormed
319
+ optimized,
320
+ // 优化稿为空 = 这次输出不可用,降级展示并让客户端弹格式警示。
321
+ wellFormed: wellFormed && Boolean(optimized)
225
322
  };
226
323
  }
227
324
 
@@ -229,34 +326,53 @@ function parseOptimizerOutput(raw, mode = "full") {
229
326
  var name = "dsh-prompt-optimizer";
230
327
  var inject = ["llm"];
231
328
  var Config = Schema.object({
232
- language: Schema.string().default("zh"),
233
- model: Schema.string(),
234
- fallbackModel: Schema.string(),
235
- maxTokens: Schema.number().min(1024).max(32768).default(8192),
236
- timeoutSeconds: Schema.number().min(10).max(600).default(120),
237
- mode: Schema.string().default("full"),
238
- reasoningEffort: Schema.string().default("lowest"),
239
- temperature: Schema.number().min(0).max(2).default(0.2),
240
- autoMaxTokens: Schema.boolean().default(true),
241
- includeContext: Schema.boolean().default(true)
329
+ language: Schema.string().default("zh").volatile(),
330
+ model: Schema.string().volatile(),
331
+ fallbackModel: Schema.string().volatile(),
332
+ maxTokens: Schema.number().min(1024).max(32768).default(8192).volatile(),
333
+ timeoutSeconds: Schema.number().min(10).max(600).default(120).volatile(),
334
+ mode: Schema.string().default("fast").volatile(),
335
+ reasoningEffort: Schema.string().default("lowest").volatile(),
336
+ temperature: Schema.number().min(0).max(2).default(0.2).volatile(),
337
+ autoMaxTokens: Schema.boolean().default(true).volatile(),
338
+ includeContext: Schema.boolean().default(true).volatile()
242
339
  });
340
+ function unwrapCell(value) {
341
+ return typeof value?.get === "function" ? value.get() : value;
342
+ }
343
+ function readConfig(raw) {
344
+ return {
345
+ language: unwrapCell(raw.language),
346
+ model: unwrapCell(raw.model),
347
+ fallbackModel: unwrapCell(raw.fallbackModel),
348
+ maxTokens: unwrapCell(raw.maxTokens),
349
+ timeoutSeconds: unwrapCell(raw.timeoutSeconds),
350
+ mode: unwrapCell(raw.mode),
351
+ reasoningEffort: unwrapCell(raw.reasoningEffort),
352
+ temperature: unwrapCell(raw.temperature),
353
+ autoMaxTokens: unwrapCell(raw.autoMaxTokens),
354
+ includeContext: unwrapCell(raw.includeContext)
355
+ };
356
+ }
243
357
  var NS = "prompt-optimizer";
244
358
  var ROUTE_PATH = "/dsh-prompt-optimizer/optimize";
245
359
  var TEST_ROUTE_PATH = "/dsh-prompt-optimizer/test-model";
246
360
  var MAX_BODY_BYTES = 256 * 1024;
247
- var warnedConfigValues = /* @__PURE__ */ new Set();
248
- function warnUnknownEnum(value, allowed, label) {
361
+ var MAX_CONCURRENT_OPTIMIZES = 3;
362
+ var SSE_HEARTBEAT_MS = 15e3;
363
+ var ONESHOT_SESSION_ID = "prompt-optimizer-one-shot";
364
+ function warnUnknownEnum(warned, value, allowed, label) {
249
365
  const key = `${label}=${value}`;
250
- if (!allowed.includes(value) && !warnedConfigValues.has(key)) {
251
- warnedConfigValues.add(key);
366
+ if (!allowed.includes(value) && !warned.has(key)) {
367
+ warned.add(key);
252
368
  console.warn(`[${name}] \u914D\u7F6E ${label}="${value}" \u4E0D\u662F\u6709\u6548\u503C(\u652F\u6301:${allowed.join("/")}),\u5DF2\u6309\u9ED8\u8BA4\u5904\u7406`);
253
369
  }
254
370
  }
255
- function warnUnknownEnumConfig(cfg) {
256
- if (cfg.language !== "zh" && cfg.language !== "en") warnUnknownEnum(cfg.language, ["zh", "en"], "language");
257
- if (cfg.mode !== "full" && cfg.mode !== "fast") warnUnknownEnum(cfg.mode, ["full", "fast"], "mode");
371
+ function warnUnknownEnumConfig(warned, cfg) {
372
+ if (cfg.language !== "zh" && cfg.language !== "en") warnUnknownEnum(warned, cfg.language, ["zh", "en"], "language");
373
+ if (cfg.mode !== "full" && cfg.mode !== "fast") warnUnknownEnum(warned, cfg.mode, ["full", "fast"], "mode");
258
374
  if (cfg.reasoningEffort !== "lowest" && cfg.reasoningEffort !== "session") {
259
- warnUnknownEnum(cfg.reasoningEffort, ["lowest", "session"], "reasoningEffort");
375
+ warnUnknownEnum(warned, cfg.reasoningEffort, ["lowest", "session"], "reasoningEffort");
260
376
  }
261
377
  }
262
378
  function asOptionalString(value) {
@@ -275,6 +391,7 @@ function parseContextInput(value) {
275
391
  return turns.length ? capConversationContext(turns) : void 0;
276
392
  }
277
393
  function writeJson(res, status, body) {
394
+ if (res.headersSent || res.writableEnded) return;
278
395
  const payload = JSON.stringify(body);
279
396
  res.writeHead(status, {
280
397
  "content-type": "application/json; charset=utf-8",
@@ -320,7 +437,11 @@ async function readJsonBody(req) {
320
437
  chunks.push(buf);
321
438
  }
322
439
  if (chunks.length === 0) return {};
323
- return JSON.parse(Buffer.concat(chunks).toString("utf-8"));
440
+ const parsed = JSON.parse(Buffer.concat(chunks).toString("utf-8"));
441
+ if (parsed === null || typeof parsed !== "object" || Array.isArray(parsed)) {
442
+ throw new Error("\u8BF7\u6C42\u4F53\u5FC5\u987B\u662F JSON \u5BF9\u8C61");
443
+ }
444
+ return parsed;
324
445
  }
325
446
  async function readBodyOrReply(req, res) {
326
447
  try {
@@ -329,7 +450,7 @@ async function readBodyOrReply(req, res) {
329
450
  if (error instanceof PayloadTooLargeError) {
330
451
  writeJson(res, 413, { ok: false, error: error.message });
331
452
  } else {
332
- writeJson(res, 400, { ok: false, error: "\u8BF7\u6C42\u4F53\u4E0D\u662F\u5408\u6CD5\u7684 JSON" });
453
+ writeJson(res, 400, { ok: false, error: "\u8BF7\u6C42\u4F53\u4E0D\u662F\u5408\u6CD5\u7684 JSON \u5BF9\u8C61" });
333
454
  }
334
455
  return void 0;
335
456
  }
@@ -365,12 +486,30 @@ async function collectText(stream, onDelta) {
365
486
  const text = deltas || [...blocks.entries()].sort(([a], [b]) => a - b).map(([, t]) => t).join("");
366
487
  return { text, truncated };
367
488
  }
489
+ var EFFORT_RANK = [
490
+ { rank: 4, re: /(?:^|[^a-z])(?:x[\s_-]?high|very[\s_-]?high|maximum|max|highest)(?:$|[^a-z])|最高/i },
491
+ { rank: 3, re: /(?:^|[^a-z])high(?:$|[^a-z])|高/i },
492
+ { rank: 2, re: /(?:^|[^a-z])(?:medium|mid(?:dle)?|standard|default)(?:$|[^a-z])|中/i },
493
+ { rank: 1, re: /(?:^|[^a-z])(?:minimal|minimum|lowest|low)(?:$|[^a-z])|最少|最低|低/i },
494
+ { rank: 0, re: /(?:^|[^a-z])(?:none|off|disabled|no[\s_-]?reasoning)(?:$|[^a-z])|^(?:0)$/i }
495
+ ];
496
+ function effortRank(id, name2) {
497
+ for (const { rank, re } of EFFORT_RANK) {
498
+ if (re.test(id) || re.test(name2)) return rank;
499
+ }
500
+ return void 0;
501
+ }
368
502
  async function resolveLowestEffort(llm, provider, model) {
369
503
  try {
370
504
  const efforts = (await llm.resolveModelInfo(provider, model)).reasoning?.efforts;
371
505
  if (!efforts?.length) return void 0;
372
- const keyworded = efforts.find((e) => /none|minimal|low|低/i.test(`${e.id} ${e.name}`));
373
- return String((keyworded ?? efforts[0]).id);
506
+ let best;
507
+ for (const effort of efforts) {
508
+ const rank = effortRank(String(effort.id), String(effort.name ?? ""));
509
+ if (rank === void 0) continue;
510
+ if (!best || rank < best.rank) best = { id: String(effort.id), rank };
511
+ }
512
+ return best?.id;
374
513
  } catch {
375
514
  return void 0;
376
515
  }
@@ -402,6 +541,8 @@ async function resolveRoute(llm, pinnedModel, bodyProvider, bodyModel) {
402
541
  return provider && model ? { provider, model } : void 0;
403
542
  }
404
543
  function apply(ctx, config) {
544
+ const warnedConfigValues = /* @__PURE__ */ new Set();
545
+ let inFlightOptimizes = 0;
405
546
  const effortCache = /* @__PURE__ */ new Map();
406
547
  const resolveLowestEffortCached = async (llm, provider, model) => {
407
548
  const key = `${provider}/${model}`;
@@ -411,32 +552,35 @@ function apply(ctx, config) {
411
552
  if (effort !== void 0) effortCache.set(key, { effort, at: Date.now() });
412
553
  return effort;
413
554
  };
414
- let current = () => config;
555
+ let current = () => readConfig(config);
415
556
  ctx.inject(["settings"], (sctx) => {
416
557
  const settings = sctx.settings;
417
558
  if (!settings) return;
418
- const hooks = {
419
- setSource: (source) => {
420
- current = source;
421
- },
422
- onChange: () => {
423
- }
424
- };
559
+ if (typeof settings.configure === "function") {
560
+ ctx.effect(() => settings.configure({ auto: false }, ctx.fiber));
561
+ }
425
562
  if (typeof settings.installSection === "function") {
426
- settings.installSection(ctx, NS, Config, config, hooks);
563
+ settings.installSection(ctx, NS, Config, config, {
564
+ setSource: (source) => {
565
+ current = () => readConfig(source());
566
+ },
567
+ onChange: () => {
568
+ }
569
+ });
427
570
  return;
428
571
  }
572
+ if (typeof settings.register !== "function") return;
429
573
  const scope = settings.register(NS, Config, { base: config });
430
- hooks.setSource(() => scope.get());
431
- hooks.onChange();
432
- const disposeWatch = scope.watch(() => hooks.onChange());
574
+ current = () => readConfig(scope.get());
575
+ const disposeWatch = scope.watch(() => {
576
+ current = () => readConfig(scope.get());
577
+ });
433
578
  ctx.effect(() => () => {
434
579
  disposeWatch();
435
- hooks.setSource(() => config);
436
- hooks.onChange();
580
+ current = () => readConfig(config);
437
581
  });
438
582
  });
439
- warnUnknownEnumConfig(config);
583
+ warnUnknownEnumConfig(warnedConfigValues, current());
440
584
  let mounted = false;
441
585
  const mount = (server) => {
442
586
  if (!server || mounted) return;
@@ -444,12 +588,13 @@ function apply(ctx, config) {
444
588
  ctx.effect(() => {
445
589
  const disposeOptimize = server.register({ kind: "exact", path: ROUTE_PATH, handler });
446
590
  const disposeTest = server.register({ kind: "exact", path: TEST_ROUTE_PATH, handler: testHandler });
591
+ console.log(`[${name}] loaded, POST ${ROUTE_PATH}`);
447
592
  return () => {
593
+ mounted = false;
448
594
  disposeOptimize();
449
595
  disposeTest();
450
596
  };
451
597
  });
452
- console.log(`[${name}] loaded, POST ${ROUTE_PATH}`);
453
598
  };
454
599
  ctx.inject(["webServer"], (sctx) => mount(sctx.webServer));
455
600
  ctx.inject(["httpServer"], (sctx) => mount(sctx.httpServer));
@@ -467,6 +612,21 @@ function apply(ctx, config) {
467
612
  return;
468
613
  }
469
614
  if (!assertTrustedOrigin(req, res)) return;
615
+ if (inFlightOptimizes >= MAX_CONCURRENT_OPTIMIZES) {
616
+ writeJson(res, 429, {
617
+ ok: false,
618
+ error: `\u540C\u65F6\u8FDB\u884C\u7684\u4F18\u5316\u5DF2\u8FBE\u4E0A\u9650(${MAX_CONCURRENT_OPTIMIZES} \u4E2A),\u8BF7\u7A0D\u540E\u518D\u8BD5\u3002`
619
+ });
620
+ return;
621
+ }
622
+ inFlightOptimizes += 1;
623
+ try {
624
+ await handleOptimize(req, res);
625
+ } finally {
626
+ inFlightOptimizes -= 1;
627
+ }
628
+ }
629
+ async function handleOptimize(req, res) {
470
630
  const abort = new AbortController();
471
631
  res.on("close", () => {
472
632
  if (!res.writableEnded) abort.abort();
@@ -475,19 +635,20 @@ function apply(ctx, config) {
475
635
  const body = await readBodyOrReply(req, res);
476
636
  if (!body) return;
477
637
  const text = asOptionalString(body.text);
638
+ const sessionId = asOptionalString(body.sessionId) ?? ONESHOT_SESSION_ID;
478
639
  if (!text) {
479
640
  writeJson(res, 400, { ok: false, error: "\u63D0\u793A\u8BCD\u5185\u5BB9\u4E3A\u7A7A" });
480
641
  return;
481
642
  }
482
643
  const cfg = current();
483
- warnUnknownEnumConfig(cfg);
644
+ warnUnknownEnumConfig(warnedConfigValues, cfg);
484
645
  const language = cfg.language === "en" ? "en" : "zh";
485
646
  const mode = cfg.mode === "fast" ? "fast" : "full";
486
647
  const route = await resolveRoute(ctx.llm, cfg.model, asOptionalString(body.provider), asOptionalString(body.model));
487
648
  if (!route) {
488
649
  writeJson(res, 409, {
489
650
  ok: false,
490
- error: "\u672A\u627E\u5230\u53EF\u7528\u6A21\u578B:\u8BF7\u5148\u5728 \u8BBE\u7F6E \u2192 \u6A21\u578B \u4E2D\u914D\u7F6E\u63D0\u4F9B\u65B9,\u6216\u5728 \u8BBE\u7F6E \u2192 \u63D2\u4EF6\u914D\u7F6E \u2192 \u63D0\u793A\u8BCD\u4F18\u5316 \u4E2D\u56FA\u5B9A\u4E00\u4E2A\u6A21\u578B"
651
+ error: "\u672A\u627E\u5230\u53EF\u7528\u6A21\u578B:\u8BF7\u5148\u5728 \u8BBE\u7F6E \u2192 \u6A21\u578B \u4E2D\u914D\u7F6E\u63D0\u4F9B\u65B9,\u6216\u5728 \u8BBE\u7F6E\u7684\u63D2\u4EF6\u9875\u300C\u63D0\u793A\u8BCD\u4F18\u5316\u300D \u4E2D\u56FA\u5B9A\u4E00\u4E2A\u6A21\u578B"
491
652
  });
492
653
  return;
493
654
  }
@@ -523,6 +684,9 @@ function apply(ctx, config) {
523
684
 
524
685
  `);
525
686
  };
687
+ const heartbeat = setInterval(() => {
688
+ if (!res.destroyed && !res.writableEnded) res.write(": ping\n\n");
689
+ }, SSE_HEARTBEAT_MS);
526
690
  const timeoutSec = cfg.timeoutSeconds ?? 120;
527
691
  let timedOut = false;
528
692
  const timer = setTimeout(() => {
@@ -545,7 +709,13 @@ function apply(ctx, config) {
545
709
  messages: [message],
546
710
  maxTokens,
547
711
  temperature: cfg.temperature ?? 0.2,
548
- signal: abort.signal
712
+ signal: abort.signal,
713
+ // 会话标注恒存在:客户端会话 id 优先(会话级计量正确归属),
714
+ // 缺席回落合成 id。opencode 等提供方要求会话头(x-opencode-session,
715
+ // 由 pi-ai 适配器按 GenerateOptions.sessionId 注入),缺失会被
716
+ // API 以「缺少请求头」拒绝;其余适配器仅作模型不可见的传输
717
+ // 元数据处理,不外发。
718
+ sessionId
549
719
  };
550
720
  let effort = sessionEffort;
551
721
  if (!preferSessionEffort) {
@@ -554,10 +724,14 @@ function apply(ctx, config) {
554
724
  if (effort) {
555
725
  options.reasoningEffort = effort;
556
726
  }
727
+ const thinkFilter = createThinkFilter();
557
728
  raw = await collectText(ctx.llm.stream(options), (delta) => {
558
729
  sentAny = true;
559
- send({ type: "delta", text: delta });
730
+ const clean = thinkFilter.push(delta);
731
+ if (clean) send({ type: "delta", text: clean });
560
732
  });
733
+ const thinkTail = thinkFilter.flush();
734
+ if (thinkTail) send({ type: "delta", text: thinkTail });
561
735
  lastError = null;
562
736
  break;
563
737
  } catch (error) {
@@ -583,7 +757,7 @@ function apply(ctx, config) {
583
757
  } else if (timedOut) {
584
758
  send({
585
759
  type: "error",
586
- error: `\u4F18\u5316\u8D85\u65F6:${timeoutSec} \u79D2\u5185\u672A\u751F\u6210\u5B8C\u6BD5\u3002\u53EF\u5728 \u8BBE\u7F6E \u2192 \u63D2\u4EF6\u914D\u7F6E \u2192 \u63D0\u793A\u8BCD\u4F18\u5316 \u4E2D\u8C03\u9AD8\u300C\u8D85\u65F6\u65F6\u95F4\u300D\u3002`
760
+ error: `\u4F18\u5316\u8D85\u65F6:${timeoutSec} \u79D2\u5185\u672A\u751F\u6210\u5B8C\u6BD5\u3002\u53EF\u5728 \u8BBE\u7F6E\u7684\u63D2\u4EF6\u9875\u300C\u63D0\u793A\u8BCD\u4F18\u5316\u300D \u4E2D\u8C03\u9AD8\u300C\u8D85\u65F6\u65F6\u95F4\u300D\u3002`
587
761
  });
588
762
  } else if (!abort.signal.aborted && lastError) {
589
763
  const message2 = lastError instanceof Error ? lastError.message : String(lastError);
@@ -592,6 +766,7 @@ function apply(ctx, config) {
592
766
  }
593
767
  } finally {
594
768
  clearTimeout(timer);
769
+ clearInterval(heartbeat);
595
770
  }
596
771
  res.end();
597
772
  } catch (error) {
@@ -599,6 +774,7 @@ function apply(ctx, config) {
599
774
  const message = error instanceof Error ? error.message : String(error);
600
775
  console.error(`[${name}] optimize failed:`, error);
601
776
  writeJson(res, 502, { ok: false, error: message });
777
+ if (!res.writableEnded) res.end();
602
778
  }
603
779
  }
604
780
  async function testHandler(req, res) {
@@ -614,7 +790,7 @@ function apply(ctx, config) {
614
790
  if (!route) {
615
791
  writeJson(res, 409, {
616
792
  ok: false,
617
- error: "\u672A\u627E\u5230\u53EF\u7528\u6A21\u578B:\u8BF7\u5148\u5728 \u8BBE\u7F6E \u2192 \u6A21\u578B \u4E2D\u914D\u7F6E\u63D0\u4F9B\u65B9,\u6216\u5728 \u8BBE\u7F6E \u2192 \u63D2\u4EF6\u914D\u7F6E \u2192 \u63D0\u793A\u8BCD\u4F18\u5316 \u4E2D\u56FA\u5B9A\u4E00\u4E2A\u6A21\u578B"
793
+ error: "\u672A\u627E\u5230\u53EF\u7528\u6A21\u578B:\u8BF7\u5148\u5728 \u8BBE\u7F6E \u2192 \u6A21\u578B \u4E2D\u914D\u7F6E\u63D0\u4F9B\u65B9,\u6216\u5728 \u8BBE\u7F6E\u7684\u63D2\u4EF6\u9875\u300C\u63D0\u793A\u8BCD\u4F18\u5316\u300D \u4E2D\u56FA\u5B9A\u4E00\u4E2A\u6A21\u578B"
618
794
  });
619
795
  return;
620
796
  }
@@ -636,7 +812,17 @@ function apply(ctx, config) {
636
812
  source: { kind: "plugin", plugin: name }
637
813
  };
638
814
  const result = await collectText(
639
- ctx.llm.stream({ provider: route.provider, model: route.model, messages: [message], maxTokens: 32, signal: abort.signal })
815
+ ctx.llm.stream({
816
+ provider: route.provider,
817
+ model: route.model,
818
+ messages: [message],
819
+ maxTokens: 32,
820
+ signal: abort.signal,
821
+ // 探活没有真实会话,标注一个固定合成 id:opencode 等提供方要求
822
+ // 会话头(x-opencode-session,适配器按 sessionId 注入),缺失会被
823
+ // API 以「缺少请求头」拒绝;固定值让探活稳定路由到同一桶。
824
+ sessionId: "prompt-optimizer-connectivity-test"
825
+ })
640
826
  );
641
827
  writeJson(res, 200, {
642
828
  ok: true,