@y1x1n/dsh-prompt-optimizer 0.3.13

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 ADDED
@@ -0,0 +1,648 @@
1
+ // src/index.ts
2
+ import Schema from "@deepseek-ai/schemastery";
3
+ import { installSettingsSection, settingsNamespace } from "@deepseek-ai/dsh-settings";
4
+
5
+ // src/prompt.ts
6
+ var MARKERS = {
7
+ analysis: "<<<ANALYSIS>>>",
8
+ optimized: "<<<OPTIMIZED>>>",
9
+ end: "<<<END>>>"
10
+ };
11
+ 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";
12
+ 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.';
13
+ 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";
14
+ 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.';
15
+ var ANALYSIS_ZH = `# \u5206\u6790\u7EF4\u5EA6
16
+ 1. \u76EE\u6807\u6E05\u6670\u5EA6:\u4EFB\u52A1\u76EE\u6807\u662F\u5426\u660E\u786E\u3001\u53EF\u5224\u65AD\u5B8C\u6210\u4E0E\u5426
17
+ 2. \u4E0A\u4E0B\u6587\u5B8C\u6574\u6027:\u662F\u5426\u7F3A\u5C11\u6A21\u578B\u5B8C\u6210\u4EFB\u52A1\u6240\u5FC5\u9700\u7684\u80CC\u666F\u4FE1\u606F
18
+ 3. \u7EA6\u675F\u4E0E\u8FB9\u754C:\u9650\u5236\u6761\u4EF6\u662F\u5426\u6709\u6B67\u4E49\u3001\u51B2\u7A81\u6216\u7F3A\u5931
19
+ 4. \u7ED3\u6784\u4E0E\u8868\u8FBE:\u7EC4\u7EC7\u662F\u5426\u6E05\u6670,\u6709\u65E0\u5197\u4F59\u3001\u53E3\u8BED\u5316\u6216\u6B67\u4E49\u8868\u8FF0
20
+ 5. \u8F93\u51FA\u89C4\u683C:\u662F\u5426\u6307\u5B9A\u671F\u671B\u7684\u8F93\u51FA\u683C\u5F0F\u3001\u957F\u5EA6\u3001\u98CE\u683C`;
21
+ var ANALYSIS_EN = `# Analysis dimensions
22
+ 1. Goal clarity: is the task explicit and verifiable
23
+ 2. Context completeness: is required background missing
24
+ 3. Constraints & boundaries: ambiguous, conflicting, or missing limits
25
+ 4. Structure & wording: organization, redundancy, ambiguity
26
+ 5. Output spec: expected format, length, style`;
27
+ var STRATEGY_TEMPLATE_ZH = `# \u4F18\u5316\u65B9\u5F0F(\u7ED3\u6784\u6A21\u677F)
28
+ \u5C06\u8349\u7A3F\u6539\u5199\u4E3A\u7ED3\u6784\u5B8C\u6574\u7684\u63D0\u793A\u8BCD,\u8981\u6C42:
29
+ - \u4F7F\u7528\u4E0E\u8349\u7A3F\u76F8\u540C\u7684\u8BED\u8A00
30
+ - \u5FE0\u5B9E\u4FDD\u7559\u539F\u610F,\u4E0D\u81C6\u9020\u7528\u6237\u672A\u8868\u8FBE\u7684\u9700\u6C42
31
+ - \u6309\u7ED3\u6784\u6A21\u677F\u7EC4\u7EC7(\u89D2\u8272 / \u4EFB\u52A1 / \u80CC\u666F / \u7EA6\u675F / \u8F93\u51FA\u683C\u5F0F\u7B49,\u5185\u5BB9\u8F83\u957F\u65F6\u7528 Markdown \u5C0F\u8282)
32
+ - \u7F3A\u5931\u4F46\u5173\u952E\u7684\u4FE1\u606F\u4E0D\u8981\u7F16\u9020,\u5728\u6587\u4E2D\u4EE5 [\u5F85\u8865\u5145:\u2026\u2026] \u6807\u51FA`;
33
+ var STRATEGY_TEMPLATE_EN = `# How to optimize (structured template)
34
+ Rewrite the draft into a well-structured prompt. Requirements:
35
+ - Same language as the draft
36
+ - Faithful to the original intent; never invent requirements
37
+ - Organize with the structure template (role / task / background / constraints / output format; use Markdown sections for longer prompts)
38
+ - Mark missing critical information as [TODO: ...] instead of fabricating it`;
39
+ var STRATEGY_INTENT_ZH = `# \u4F18\u5316\u65B9\u5F0F(\u63D0\u70BC\u76EE\u7684 + \u6DA6\u8272)
40
+ \u7528\u6237\u6D88\u606F\u9644\u5E26 <conversation-context>(\u8BE5\u4F1A\u8BDD\u7684\u8FD1\u671F\u5BF9\u8BDD)\u3002\u5148\u901A\u8BFB\u5B83\u3001\u63D0\u70BC\u7528\u6237\u7684\u771F\u5B9E\u76EE\u7684,\u518D\u56F4\u7ED5\u8BE5\u76EE\u7684\u5BF9\u8349\u7A3F\u505A\u6DA6\u8272:
41
+ - \u4E0D\u8981\u5957\u7528\u6A21\u677F\u7ED3\u6784:\u4FDD\u7559\u8349\u7A3F\u7684\u539F\u59CB\u8868\u8FBE\u6846\u67B6,\u987A\u52BF\u6253\u78E8\u3001\u8865\u5168\u7EC6\u8282
42
+ - \u5145\u5206\u5229\u7528\u4E0A\u4E0B\u6587\u5DF2\u660E\u786E\u7684\u4FE1\u606F(\u5BF9\u8C61\u3001\u7EA6\u675F\u3001\u504F\u597D),\u8BA9\u4F18\u5316\u7A3F\u53EF\u4EE5\u76F4\u63A5\u843D\u5730;\u4E0D\u8981\u91CD\u590D\u8FFD\u95EE\u4E0A\u4E0B\u6587\u5DF2\u7ECF\u7ED9\u51FA\u7684\u5185\u5BB9
43
+ - \u8BC6\u522B\u4E0A\u4E0B\u6587\u4E2D\u5DF2\u88AB\u7528\u6237\u5426\u51B3\u6216\u63A8\u7FFB\u7684\u65B9\u5411(\u7981\u5165\u96C6\u5408),\u4F18\u5316\u7A3F\u4E0D\u5F97\u91CD\u590D\u63D0\u51FA
44
+ - \u8349\u7A3F\u4E0E\u4E0A\u4E0B\u6587\u51B2\u7A81\u65F6,\u4EE5\u8349\u7A3F\u4E3A\u6700\u7EC8\u610F\u56FE,\u53EF\u5728\u4F18\u5316\u7A3F\u4E2D\u8C03\u548C\u8868\u8FF0
45
+ - \u4E0A\u4E0B\u6587\u4EC5\u4F9B\u7406\u89E3\u610F\u56FE:\u4E25\u7981\u56DE\u7B54\u3001\u5EF6\u7EED\u6216\u5F15\u7528\u5176\u4E2D\u7684\u5185\u5BB9;\u4F60\u552F\u4E00\u8981\u4F18\u5316\u7684\u5BF9\u8C61\u662F <prompt-draft> \u91CC\u7684\u8349\u7A3F
46
+ - \u4F7F\u7528\u4E0E\u8349\u7A3F\u76F8\u540C\u7684\u8BED\u8A00;\u8349\u7A3F\u4E0E\u4E0A\u4E0B\u6587\u90FD\u672A\u63D0\u4F9B\u7684\u5173\u952E\u4FE1\u606F\u4EE5 [\u5F85\u8865\u5145:\u2026\u2026] \u6807\u51FA`;
47
+ var STRATEGY_INTENT_EN = `# How to optimize (distill intent, then polish)
48
+ The user message includes <conversation-context> (recent messages of this session). Read it first, distill what the user is really trying to achieve, then polish the draft toward that goal:
49
+ - Do NOT force a template structure: keep the draft's original framing, refine it in place and fill the gaps
50
+ - Exploit what the context already establishes (subject, constraints, preferences) so the optimized prompt is directly actionable; never re-ask what the context already answers
51
+ - Identify directions the user has already rejected or overruled in the context (the do-not-enter set); never propose them again
52
+ - If the draft conflicts with the context, the draft wins as the final intent; you may reconcile the wording in the rewrite
53
+ - The context only explains intent: never answer, continue, or quote it. The only thing you optimize is the draft inside <prompt-draft>
54
+ - Same language as the draft; mark information missing from both draft and context as [TODO: ...]`;
55
+ var RULES_ZH = `# \u4FDD\u771F\u4E0E\u660E\u786E\u5316\u539F\u5219(\u5168\u7A0B\u9075\u5B88)
56
+ - \u7406\u89E3\u4F18\u5148:\u5148\u9010\u6761\u5217\u51FA\u8349\u7A3F\u5DF2\u660E\u786E\u7684\u4FE1\u606F(\u5BF9\u8C61\u3001\u52A8\u4F5C\u3001\u7EA6\u675F\u3001\u8303\u56F4\u3001\u672F\u8BED\u3001\u6570\u5B57\u3001\u8BED\u6C14),\u533A\u5206\u300C\u8349\u7A3F\u660E\u786E\u8868\u8FBE\u7684\u300D\u4E0E\u300C\u4F60\u7684\u63A8\u6D4B\u300D\u2014\u2014\u63A8\u6D4B\u53EA\u53EF\u7528\u4E8E\u63AA\u8F9E,\u7EDD\u4E0D\u5199\u5165\u7ED3\u679C
57
+ - \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
58
+ - \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
59
+ - \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
60
+ - \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
61
+ - \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`;
62
+ var RULES_EN = `# Fidelity & clarification rules (always apply)
63
+ - 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
64
+ - 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
65
+ - 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
66
+ - 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
67
+ - 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
68
+ - Language match: Chinese-dominant draft \u2192 Chinese output; English-dominant \u2192 English output; keep the draft's terms and proper nouns`;
69
+ var EXAMPLE_TEMPLATE_ZH = `# \u793A\u4F8B(\u4E25\u683C\u6A21\u4EFF\u5176\u300C\u8F93\u5165 \u2192 \u8F93\u51FA\u300D\u7684\u98CE\u683C)
70
+ \u8F93\u5165:\u5E2E\u6211\u5199\u4E2A\u8BF7\u5047\u6761
71
+ \u8F93\u51FA:\u8BF7\u5E2E\u6211\u64B0\u5199\u4E00\u5F20\u8BF7\u5047\u6761:\u4E8B\u7531\u4E3A\u611F\u5192\u5C31\u533B,\u8BF7\u5047\u4E24\u5929([\u5F85\u8865\u5145:\u8D77\u6B62\u65E5\u671F]);\u8BED\u6C14\u6B63\u5F0F\u7B80\u6D01,\u5305\u542B\u79F0\u547C\u4E0E\u843D\u6B3E,[\u5F85\u8865\u5145:\u8BF7\u5047\u5BF9\u8C61\u4E0E\u8054\u7CFB\u65B9\u5F0F]\u3002`;
72
+ var EXAMPLE_TEMPLATE_EN = `# Example (strictly mirror its "input \u2192 output" style)
73
+ Input: help me write a leave note
74
+ Output: Write a formal leave request: two days off for a medical visit ([TODO: exact dates]); concise tone, with salutation and sign-off, [TODO: recipient and contact].`;
75
+ var EXAMPLE_INTENT_ZH = `# \u793A\u4F8B(\u4E25\u683C\u6A21\u4EFF\u5176\u300C\u8F93\u5165 \u2192 \u8F93\u51FA\u300D\u7684\u98CE\u683C;\u8F93\u5165\u4E2D\u7684\u80CC\u666F\u6765\u81EA <conversation-context>)
76
+ \u8F93\u5165:(\u80CC\u666F:\u7528\u6237\u5728\u8FD0\u8425 200 \u4EBA\u5C0F\u533A\u5B9D\u5988\u95F2\u7F6E\u7FA4,\u9996\u6B21\u63A5\u9F99\u53EA\u6709\u5341\u51E0\u4EBA\u53C2\u4E0E,\u590D\u76D8\u4E3A\u5956\u54C1\u5DEE\u3001\u683C\u5F0F\u4E71)\u5E2E\u6211\u91CD\u65B0\u8BBE\u8BA1\u4E00\u4E0B\u8FD9\u4E2A\u63A5\u9F99
77
+ \u8F93\u51FA:\u5E2E\u6211\u91CD\u65B0\u8BBE\u8BA1\u5C0F\u533A\u95F2\u7F6E\u7FA4\u7684\u300C\u597D\u7269\u63A8\u8350\u300D\u63A5\u9F99:\u7FA4 200 \u4F59\u4EBA\u3001\u4EE5\u5B9D\u5988\u4E3A\u4E3B,\u4E0A\u6B21\u53C2\u4E0E\u4EC5\u5341\u51E0\u4EBA,\u95EE\u9898\u5728\u4E8E\u5956\u54C1\u5438\u5F15\u529B\u4E0D\u8DB3\u3001\u63A5\u9F99\u683C\u5F0F\u6DF7\u4E71;\u8BF7\u7ED9\u51FA\u683C\u5F0F\u7B80\u660E\u7684\u63A5\u9F99\u6587\u6848\u6A21\u677F\u3001\u4F4E\u6210\u672C\u9AD8\u5438\u5F15\u529B\u7684\u5956\u54C1\u5EFA\u8BAE,\u4EE5\u53CA\u63D0\u5347\u53C2\u4E0E\u5EA6\u7684\u5177\u4F53\u624B\u6BB5\u3002`;
78
+ var EXAMPLE_INTENT_EN = `# Example (strictly mirror its "input \u2192 output" style; the background comes from <conversation-context>)
79
+ 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
80
+ 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.`;
81
+ var FORMAT_FULL_ZH = `# \u8F93\u51FA\u683C\u5F0F(\u4E25\u683C\u9075\u5B88,\u6807\u8BB0\u884C\u5355\u72EC\u6210\u884C)
82
+ ${MARKERS.analysis}
83
+ \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
84
+ ${MARKERS.optimized}
85
+ \u8F93\u51FA\u4F18\u5316\u540E\u7684\u5B8C\u6574\u63D0\u793A\u8BCD\u5168\u6587
86
+ ${MARKERS.end}
87
+
88
+ \u9664\u4E0A\u8FF0\u6807\u8BB0\u5305\u88F9\u7684\u5185\u5BB9\u5916,\u4E0D\u8981\u8F93\u51FA\u4EFB\u4F55\u5176\u4ED6\u6587\u5B57\u3002`;
89
+ var FORMAT_FULL_EN = `# Output format (strictly follow; markers on their own lines)
90
+ ${MARKERS.analysis}
91
+ One finding per line: "dimension | issue | suggestion". If a dimension is fine: "dimension | no issue | -".
92
+ ${MARKERS.optimized}
93
+ The full optimized prompt text
94
+ ${MARKERS.end}
95
+
96
+ Do not output anything outside these markers.`;
97
+ var FORMAT_FAST_ZH = `# \u8F93\u51FA\u683C\u5F0F(\u4E25\u683C\u9075\u5B88,\u6807\u8BB0\u884C\u5355\u72EC\u6210\u884C)
98
+ ${MARKERS.optimized}
99
+ \u8F93\u51FA\u4F18\u5316\u540E\u7684\u5B8C\u6574\u63D0\u793A\u8BCD\u5168\u6587
100
+ ${MARKERS.end}
101
+
102
+ \u9664\u4E0A\u8FF0\u6807\u8BB0\u5305\u88F9\u7684\u5185\u5BB9\u5916,\u4E0D\u8981\u8F93\u51FA\u4EFB\u4F55\u5176\u4ED6\u6587\u5B57\u3002`;
103
+ var FORMAT_FAST_EN = `# Output format (strictly follow; markers on their own lines)
104
+ ${MARKERS.optimized}
105
+ The full optimized prompt text
106
+ ${MARKERS.end}
107
+
108
+ Do not output anything outside these markers.`;
109
+ function buildSystemPrompt(language, mode = "full", strategy = "template") {
110
+ const zh = language !== "en";
111
+ const parts = [zh ? mode === "fast" ? ROLE_FAST_ZH : ROLE_FULL_ZH : mode === "fast" ? ROLE_FAST_EN : ROLE_FULL_EN];
112
+ if (mode !== "fast") parts.push(zh ? ANALYSIS_ZH : ANALYSIS_EN);
113
+ parts.push(zh ? strategy === "intent" ? STRATEGY_INTENT_ZH : STRATEGY_TEMPLATE_ZH : strategy === "intent" ? STRATEGY_INTENT_EN : STRATEGY_TEMPLATE_EN);
114
+ parts.push(zh ? RULES_ZH : RULES_EN);
115
+ parts.push(zh ? strategy === "intent" ? EXAMPLE_INTENT_ZH : EXAMPLE_TEMPLATE_ZH : strategy === "intent" ? EXAMPLE_INTENT_EN : EXAMPLE_TEMPLATE_EN);
116
+ parts.push(mode === "fast" ? zh ? FORMAT_FAST_ZH : FORMAT_FAST_EN : zh ? FORMAT_FULL_ZH : FORMAT_FULL_EN);
117
+ return parts.join("\n\n");
118
+ }
119
+ var CONTEXT_MAX_TURNS = 8;
120
+ var CONTEXT_MAX_CHARS = 1600;
121
+ var CONTEXT_TURN_MAX_CHARS = 600;
122
+ function capConversationContext(turns) {
123
+ const recent = turns.filter((t) => t.text.trim()).slice(-CONTEXT_MAX_TURNS).map((t) => ({
124
+ role: t.role,
125
+ text: t.text.trim().length > CONTEXT_TURN_MAX_CHARS ? `${t.text.trim().slice(0, CONTEXT_TURN_MAX_CHARS)}\u2026` : t.text.trim()
126
+ }));
127
+ let budget = CONTEXT_MAX_CHARS;
128
+ const kept = [];
129
+ for (let i = recent.length - 1; i >= 0; i--) {
130
+ const turn = recent[i];
131
+ if (turn.text.length > budget) {
132
+ if (kept.length === 0) kept.push({ role: turn.role, text: `${turn.text.slice(0, budget)}\u2026` });
133
+ break;
134
+ }
135
+ kept.unshift(turn);
136
+ budget -= turn.text.length;
137
+ }
138
+ return kept;
139
+ }
140
+ function buildUserPayload(draft, language, context, previous) {
141
+ const sections = [];
142
+ if (context?.length) {
143
+ const lines = context.map((t) => `${language === "en" ? t.role === "user" ? "User" : "Assistant" : t.role === "user" ? "\u7528\u6237" : "\u52A9\u624B"}: ${t.text}`);
144
+ sections.push(
145
+ language === "en" ? `Here is the recent conversation of my current session (reference only \u2014 it explains my intent; do NOT answer, continue, or quote it):
146
+
147
+ <conversation-context>
148
+ ${lines.join("\n")}
149
+ </conversation-context>` : `\u4EE5\u4E0B\u662F\u6211\u5F53\u524D\u4F1A\u8BDD\u7684\u8FD1\u671F\u5BF9\u8BDD(\u4EC5\u4F9B\u53C2\u8003,\u7528\u4E8E\u7406\u89E3\u6211\u7684\u610F\u56FE;\u4E0D\u8981\u56DE\u7B54\u3001\u5EF6\u7EED\u6216\u5F15\u7528\u5176\u4E2D\u7684\u5185\u5BB9):
150
+
151
+ <conversation-context>
152
+ ${lines.join("\n")}
153
+ </conversation-context>`
154
+ );
155
+ }
156
+ if (previous?.trim()) {
157
+ sections.push(
158
+ language === "en" ? `Here is the result of my previous optimization round (continuity reference: keep the decisions and phrasing I accepted; adjust only around what my new draft changes \u2014 unless my new draft explicitly overrules them):
159
+
160
+ <previous-optimized>
161
+ ${previous.trim()}
162
+ </previous-optimized>` : `\u4EE5\u4E0B\u662F\u6211\u4E0A\u4E00\u8F6E\u4F18\u5316\u5F97\u5230\u7684\u7ED3\u679C(\u5EF6\u7EED\u53C2\u8003:\u6CBF\u7528\u5176\u4E2D\u6211\u5DF2\u63A5\u53D7\u7684\u51B3\u7B56\u4E0E\u8868\u8FBE,\u53EA\u56F4\u7ED5\u672C\u8F6E\u8349\u7A3F\u7684\u53D8\u5316\u70B9\u8C03\u6574;\u672C\u8F6E\u8349\u7A3F\u660E\u786E\u63A8\u7FFB\u7684\u9664\u5916):
163
+
164
+ <previous-optimized>
165
+ ${previous.trim()}
166
+ </previous-optimized>`
167
+ );
168
+ }
169
+ sections.push(
170
+ language === "en" ? context?.length || previous?.trim() ? `Here is my prompt draft (the only thing you should optimize):
171
+
172
+ <prompt-draft>
173
+ ${draft}
174
+ </prompt-draft>` : `Here is my prompt draft:
175
+
176
+ ${draft}` : context?.length || previous?.trim() ? `\u4EE5\u4E0B\u662F\u6211\u7684\u63D0\u793A\u8BCD\u8349\u7A3F(\u4F60\u552F\u4E00\u9700\u8981\u4F18\u5316\u7684\u5BF9\u8C61):
177
+
178
+ <prompt-draft>
179
+ ${draft}
180
+ </prompt-draft>` : `\u4EE5\u4E0B\u662F\u6211\u7684\u63D0\u793A\u8BCD\u8349\u7A3F:
181
+
182
+ ${draft}`
183
+ );
184
+ return sections.join("\n\n");
185
+ }
186
+ function estimateTokens(text) {
187
+ let cjk = 0;
188
+ let other = 0;
189
+ for (const ch of text) {
190
+ const code = ch.codePointAt(0) ?? 0;
191
+ if (code >= 13312 && code <= 19903 || code >= 19968 && code <= 40959 || code >= 63744 && code <= 64255) {
192
+ cjk += 1;
193
+ } else {
194
+ other += 1;
195
+ }
196
+ }
197
+ return Math.ceil(cjk + other / 4);
198
+ }
199
+ function findMarker(text, word, fromIndex = 0) {
200
+ const re = new RegExp(`<<<\\s*${word}\\s*>>>`, "g");
201
+ re.lastIndex = fromIndex;
202
+ const match = re.exec(text);
203
+ return match ? { index: match.index, length: match[0].length } : null;
204
+ }
205
+ function parseOptimizerOutput(raw, mode = "full") {
206
+ const text = raw.trim();
207
+ const a = findMarker(text, "ANALYSIS");
208
+ const o = findMarker(text, "OPTIMIZED");
209
+ const wellFormed = mode === "fast" ? true : Boolean(a && o);
210
+ if (!a && !o) {
211
+ return { analysis: "", optimized: text, wellFormed };
212
+ }
213
+ let analysis = "";
214
+ let optimized = "";
215
+ if (a && (!o || a.index < o.index)) {
216
+ analysis = text.slice(a.index + a.length, o ? o.index : text.length).trim();
217
+ }
218
+ if (o) {
219
+ const e = findMarker(text, "END", o.index + o.length);
220
+ optimized = text.slice(o.index + o.length, e ? e.index : text.length).trim();
221
+ }
222
+ return {
223
+ analysis,
224
+ optimized: optimized || text,
225
+ wellFormed
226
+ };
227
+ }
228
+
229
+ // src/index.ts
230
+ var name = "dsh-prompt-optimizer";
231
+ var inject = ["llm"];
232
+ var Config = Schema.object({
233
+ language: Schema.string().default("zh"),
234
+ model: Schema.string(),
235
+ fallbackModel: Schema.string(),
236
+ maxTokens: Schema.number().min(1024).max(32768).default(8192),
237
+ timeoutSeconds: Schema.number().min(10).max(600).default(120),
238
+ mode: Schema.string().default("full"),
239
+ reasoningEffort: Schema.string().default("lowest"),
240
+ temperature: Schema.number().min(0).max(2).default(0.2),
241
+ autoMaxTokens: Schema.boolean().default(true),
242
+ includeContext: Schema.boolean().default(true)
243
+ });
244
+ var NS = settingsNamespace("prompt-optimizer");
245
+ var ROUTE_PATH = "/dsh-prompt-optimizer/optimize";
246
+ var TEST_ROUTE_PATH = "/dsh-prompt-optimizer/test-model";
247
+ var MAX_BODY_BYTES = 256 * 1024;
248
+ var warnedConfigValues = /* @__PURE__ */ new Set();
249
+ function warnUnknownEnum(value, allowed, label) {
250
+ const key = `${label}=${value}`;
251
+ if (!allowed.includes(value) && !warnedConfigValues.has(key)) {
252
+ warnedConfigValues.add(key);
253
+ console.warn(`[${name}] \u914D\u7F6E ${label}="${value}" \u4E0D\u662F\u6709\u6548\u503C(\u652F\u6301:${allowed.join("/")}),\u5DF2\u6309\u9ED8\u8BA4\u5904\u7406`);
254
+ }
255
+ }
256
+ function warnUnknownEnumConfig(cfg) {
257
+ if (cfg.language !== "zh" && cfg.language !== "en") warnUnknownEnum(cfg.language, ["zh", "en"], "language");
258
+ if (cfg.mode !== "full" && cfg.mode !== "fast") warnUnknownEnum(cfg.mode, ["full", "fast"], "mode");
259
+ if (cfg.reasoningEffort !== "lowest" && cfg.reasoningEffort !== "session") {
260
+ warnUnknownEnum(cfg.reasoningEffort, ["lowest", "session"], "reasoningEffort");
261
+ }
262
+ }
263
+ function asOptionalString(value) {
264
+ return typeof value === "string" && value.trim() ? value.trim() : void 0;
265
+ }
266
+ function parseContextInput(value) {
267
+ if (!Array.isArray(value)) return void 0;
268
+ const turns = [];
269
+ for (const item of value) {
270
+ if (!item || typeof item !== "object") continue;
271
+ const { role, text } = item;
272
+ if ((role === "user" || role === "assistant") && typeof text === "string" && text.trim()) {
273
+ turns.push({ role, text: text.trim() });
274
+ }
275
+ }
276
+ return turns.length ? capConversationContext(turns) : void 0;
277
+ }
278
+ function writeJson(res, status, body) {
279
+ const payload = JSON.stringify(body);
280
+ res.writeHead(status, {
281
+ "content-type": "application/json; charset=utf-8",
282
+ "content-length": Buffer.byteLength(payload)
283
+ });
284
+ res.end(payload);
285
+ }
286
+ function assertTrustedOrigin(req, res) {
287
+ const origin = req.headers.origin;
288
+ if (!origin) return true;
289
+ const host = req.headers.host;
290
+ let originHost = "";
291
+ try {
292
+ originHost = new URL(origin).host;
293
+ } catch {
294
+ originHost = "";
295
+ }
296
+ if (!host || originHost !== host) {
297
+ writeJson(res, 403, { ok: false, error: "\u5DF2\u62D2\u7EDD\u8DE8\u7AD9\u8BF7\u6C42(Origin \u4E0E Host \u4E0D\u7B26)" });
298
+ return false;
299
+ }
300
+ const hostName = host.replace(/:\d+$/, "");
301
+ const hostPort = /:(\d+)$/.exec(host)?.[1] ?? "80";
302
+ const loopback = hostName === "localhost" || hostName === "127.0.0.1" || hostName === "[::1]" || hostName === "::1";
303
+ if (!loopback) {
304
+ const sock = req.socket;
305
+ if (!(sock?.localAddress && sock?.localPort && hostName === sock.localAddress && hostPort === String(sock.localPort))) {
306
+ writeJson(res, 403, { ok: false, error: "\u8BF7\u6C42\u7684 Host \u4E0E\u672C\u673A\u670D\u52A1\u5730\u5740\u4E0D\u7B26(\u53EF\u80FD\u7684 DNS rebinding),\u5DF2\u62D2\u7EDD" });
307
+ return false;
308
+ }
309
+ }
310
+ return true;
311
+ }
312
+ var PayloadTooLargeError = class extends Error {
313
+ };
314
+ async function readJsonBody(req) {
315
+ const chunks = [];
316
+ let size = 0;
317
+ for await (const chunk of req) {
318
+ const buf = typeof chunk === "string" ? Buffer.from(chunk) : chunk;
319
+ size += buf.length;
320
+ if (size > MAX_BODY_BYTES) throw new PayloadTooLargeError("\u8BF7\u6C42\u4F53\u8D85\u8FC7 256KB \u4E0A\u9650");
321
+ chunks.push(buf);
322
+ }
323
+ if (chunks.length === 0) return {};
324
+ return JSON.parse(Buffer.concat(chunks).toString("utf-8"));
325
+ }
326
+ async function readBodyOrReply(req, res) {
327
+ try {
328
+ return await readJsonBody(req);
329
+ } catch (error) {
330
+ if (error instanceof PayloadTooLargeError) {
331
+ writeJson(res, 413, { ok: false, error: error.message });
332
+ } else {
333
+ writeJson(res, 400, { ok: false, error: "\u8BF7\u6C42\u4F53\u4E0D\u662F\u5408\u6CD5\u7684 JSON" });
334
+ }
335
+ return void 0;
336
+ }
337
+ }
338
+ async function collectText(stream, onDelta) {
339
+ let deltas = "";
340
+ const blocks = /* @__PURE__ */ new Map();
341
+ let truncated = false;
342
+ for await (const chunk of stream) {
343
+ switch (chunk.type) {
344
+ case "text-delta":
345
+ deltas += chunk.text;
346
+ onDelta?.(chunk.text);
347
+ break;
348
+ case "block-end":
349
+ if (chunk.block.type === "text") blocks.set(chunk.index, chunk.block.text);
350
+ break;
351
+ case "finish": {
352
+ const reason = chunk.reason;
353
+ if (reason.kind === "error" || reason.kind === "aborted") {
354
+ throw new Error(`\u6A21\u578B\u8C03\u7528\u5931\u8D25(${reason.failure.code}): ${reason.failure.message}`);
355
+ }
356
+ if (reason.kind === "tool-calls") {
357
+ throw new Error("\u6A21\u578B\u610F\u5916\u8BF7\u6C42\u5DE5\u5177\u8C03\u7528(\u4F18\u5316\u8C03\u7528\u4E0D\u63D0\u4F9B\u5DE5\u5177),\u8BF7\u68C0\u67E5\u6A21\u578B\u8DEF\u7531\u914D\u7F6E");
358
+ }
359
+ if (reason.kind === "max-tokens") truncated = true;
360
+ break;
361
+ }
362
+ default:
363
+ break;
364
+ }
365
+ }
366
+ const text = deltas || [...blocks.entries()].sort(([a], [b]) => a - b).map(([, t]) => t).join("");
367
+ return { text, truncated };
368
+ }
369
+ async function resolveLowestEffort(llm, provider, model) {
370
+ try {
371
+ const efforts = (await llm.resolveModelInfo(provider, model)).reasoning?.efforts;
372
+ if (!efforts?.length) return void 0;
373
+ const keyworded = efforts.find((e) => /none|minimal|low|低/i.test(`${e.id} ${e.name}`));
374
+ return String((keyworded ?? efforts[0]).id);
375
+ } catch {
376
+ return void 0;
377
+ }
378
+ }
379
+ var EFFORT_CACHE_TTL = 10 * 60 * 1e3;
380
+ async function resolveRoute(llm, pinnedModel, bodyProvider, bodyModel) {
381
+ let provider;
382
+ let model;
383
+ const pinned = asOptionalString(pinnedModel);
384
+ if (pinned) {
385
+ const slash = pinned.indexOf("/");
386
+ if (slash > 0 && slash < pinned.length - 1) {
387
+ provider = pinned.slice(0, slash);
388
+ model = pinned.slice(slash + 1);
389
+ }
390
+ }
391
+ provider ??= bodyProvider;
392
+ model ??= bodyModel;
393
+ if (!provider || !model) {
394
+ const first = llm.listProviders()[0];
395
+ if (!provider && first) provider = first.id;
396
+ if (provider && !model) {
397
+ try {
398
+ model = (await llm.listModels(provider))[0]?.id;
399
+ } catch {
400
+ }
401
+ }
402
+ }
403
+ return provider && model ? { provider, model } : void 0;
404
+ }
405
+ function apply(ctx, config) {
406
+ const effortCache = /* @__PURE__ */ new Map();
407
+ const resolveLowestEffortCached = async (llm, provider, model) => {
408
+ const key = `${provider}/${model}`;
409
+ const hit = effortCache.get(key);
410
+ if (hit && Date.now() - hit.at < EFFORT_CACHE_TTL) return hit.effort;
411
+ const effort = await resolveLowestEffort(llm, provider, model);
412
+ if (effort !== void 0) effortCache.set(key, { effort, at: Date.now() });
413
+ return effort;
414
+ };
415
+ let current = () => config;
416
+ installSettingsSection(ctx, NS, Config, config, {
417
+ setSource: (source) => {
418
+ current = source;
419
+ },
420
+ onChange: () => {
421
+ }
422
+ });
423
+ warnUnknownEnumConfig(config);
424
+ let mounted = false;
425
+ const mount = (server) => {
426
+ if (!server || mounted) return;
427
+ mounted = true;
428
+ ctx.effect(() => {
429
+ const disposeOptimize = server.register({ kind: "exact", path: ROUTE_PATH, handler });
430
+ const disposeTest = server.register({ kind: "exact", path: TEST_ROUTE_PATH, handler: testHandler });
431
+ return () => {
432
+ disposeOptimize();
433
+ disposeTest();
434
+ };
435
+ });
436
+ console.log(`[${name}] loaded, POST ${ROUTE_PATH}`);
437
+ };
438
+ ctx.inject(["webServer"], (sctx) => mount(sctx.webServer));
439
+ ctx.inject(["httpServer"], (sctx) => mount(sctx.httpServer));
440
+ ctx.effect(() => {
441
+ const timer = setTimeout(() => {
442
+ if (!mounted) {
443
+ console.error(`[${name}] \u672A\u627E\u5230 webServer/httpServer \u670D\u52A1,\u8DEF\u7531\u672A\u6CE8\u518C\u2014\u2014\u5F53\u524D dsh \u7248\u672C\u53EF\u80FD\u4E0D\u517C\u5BB9`);
444
+ }
445
+ }, 1e4);
446
+ return () => clearTimeout(timer);
447
+ });
448
+ async function handler(req, res) {
449
+ if (req.method !== "POST") {
450
+ writeJson(res, 405, { ok: false, error: "Method Not Allowed" });
451
+ return;
452
+ }
453
+ if (!assertTrustedOrigin(req, res)) return;
454
+ const abort = new AbortController();
455
+ res.on("close", () => {
456
+ if (!res.writableEnded) abort.abort();
457
+ });
458
+ try {
459
+ const body = await readBodyOrReply(req, res);
460
+ if (!body) return;
461
+ const text = asOptionalString(body.text);
462
+ if (!text) {
463
+ writeJson(res, 400, { ok: false, error: "\u63D0\u793A\u8BCD\u5185\u5BB9\u4E3A\u7A7A" });
464
+ return;
465
+ }
466
+ const cfg = current();
467
+ warnUnknownEnumConfig(cfg);
468
+ const language = cfg.language === "en" ? "en" : "zh";
469
+ const mode = cfg.mode === "fast" ? "fast" : "full";
470
+ const route = await resolveRoute(ctx.llm, cfg.model, asOptionalString(body.provider), asOptionalString(body.model));
471
+ if (!route) {
472
+ writeJson(res, 409, {
473
+ ok: false,
474
+ 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"
475
+ });
476
+ return;
477
+ }
478
+ const routes = [route];
479
+ const fallbackRaw = asOptionalString(cfg.fallbackModel);
480
+ if (fallbackRaw) {
481
+ const slash = fallbackRaw.indexOf("/");
482
+ if (slash > 0 && slash < fallbackRaw.length - 1) {
483
+ const fb = { provider: fallbackRaw.slice(0, slash), model: fallbackRaw.slice(slash + 1) };
484
+ if (fb.provider !== route.provider || fb.model !== route.model) routes.push(fb);
485
+ }
486
+ }
487
+ const context = cfg.includeContext === false ? void 0 : parseContextInput(body.context);
488
+ const previousRaw = cfg.includeContext === false ? void 0 : asOptionalString(body.previous);
489
+ const previous = previousRaw ? previousRaw.length > 1500 ? `${previousRaw.slice(0, 1500)}\u2026` : previousRaw : void 0;
490
+ const message = {
491
+ id: `prompt-optimizer-${crypto.randomUUID()}`,
492
+ role: "user",
493
+ content: [{ type: "text", text: buildUserPayload(text, language, context, previous) }],
494
+ source: { kind: "plugin", plugin: name }
495
+ };
496
+ const system = buildSystemPrompt(language, mode, context?.length ? "intent" : "template");
497
+ const maxTokens = cfg.autoMaxTokens === false ? cfg.maxTokens : Math.min(32768, Math.max(cfg.maxTokens, Math.ceil(estimateTokens(text) * (mode === "fast" ? 1.5 : 2))));
498
+ const sessionEffort = asOptionalString(body.reasoningEffort);
499
+ const preferSessionEffort = cfg.reasoningEffort === "session";
500
+ res.writeHead(200, {
501
+ "content-type": "text/event-stream; charset=utf-8",
502
+ "cache-control": "no-cache, no-transform",
503
+ "x-accel-buffering": "no"
504
+ });
505
+ const send = (event) => {
506
+ if (!res.destroyed && !res.writableEnded) res.write(`data: ${JSON.stringify(event)}
507
+
508
+ `);
509
+ };
510
+ const timeoutSec = cfg.timeoutSeconds ?? 120;
511
+ let timedOut = false;
512
+ const timer = setTimeout(() => {
513
+ timedOut = true;
514
+ abort.abort();
515
+ }, timeoutSec * 1e3);
516
+ try {
517
+ let sentAny = false;
518
+ let used = routes[0];
519
+ let raw = null;
520
+ let lastError = null;
521
+ let fallbackReason;
522
+ for (const attempt of routes) {
523
+ used = attempt;
524
+ try {
525
+ const options = {
526
+ provider: attempt.provider,
527
+ model: attempt.model,
528
+ system,
529
+ messages: [message],
530
+ maxTokens,
531
+ temperature: cfg.temperature ?? 0.2,
532
+ signal: abort.signal
533
+ };
534
+ let effort = sessionEffort;
535
+ if (!preferSessionEffort) {
536
+ effort = await resolveLowestEffortCached(ctx.llm, attempt.provider, attempt.model) ?? sessionEffort;
537
+ }
538
+ if (effort) {
539
+ options.reasoningEffort = effort;
540
+ }
541
+ raw = await collectText(ctx.llm.stream(options), (delta) => {
542
+ sentAny = true;
543
+ send({ type: "delta", text: delta });
544
+ });
545
+ lastError = null;
546
+ break;
547
+ } catch (error) {
548
+ lastError = error;
549
+ if (timedOut || abort.signal.aborted || sentAny) break;
550
+ if (attempt !== routes[routes.length - 1]) {
551
+ fallbackReason = error instanceof Error ? error.message : String(error);
552
+ console.error(`[${name}] \u4E3B\u8DEF\u7531 ${attempt.provider}/${attempt.model} \u5931\u8D25,\u5C1D\u8BD5\u56DE\u9000\u8DEF\u7531:`, error);
553
+ }
554
+ }
555
+ }
556
+ if (raw) {
557
+ const parsed = parseOptimizerOutput(raw.text, mode);
558
+ send({
559
+ type: "done",
560
+ ...parsed,
561
+ truncated: raw.truncated,
562
+ provider: used.provider,
563
+ model: used.model,
564
+ fallbackUsed: used !== routes[0],
565
+ ...fallbackReason ? { fallbackReason } : {}
566
+ });
567
+ } else if (timedOut) {
568
+ send({
569
+ type: "error",
570
+ 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`
571
+ });
572
+ } else if (!abort.signal.aborted && lastError) {
573
+ const message2 = lastError instanceof Error ? lastError.message : String(lastError);
574
+ console.error(`[${name}] optimize failed:`, lastError);
575
+ send({ type: "error", error: message2 });
576
+ }
577
+ } finally {
578
+ clearTimeout(timer);
579
+ }
580
+ res.end();
581
+ } catch (error) {
582
+ if (abort.signal.aborted) return;
583
+ const message = error instanceof Error ? error.message : String(error);
584
+ console.error(`[${name}] optimize failed:`, error);
585
+ writeJson(res, 502, { ok: false, error: message });
586
+ }
587
+ }
588
+ async function testHandler(req, res) {
589
+ if (req.method !== "POST") {
590
+ writeJson(res, 405, { ok: false, error: "Method Not Allowed" });
591
+ return;
592
+ }
593
+ if (!assertTrustedOrigin(req, res)) return;
594
+ const body = await readBodyOrReply(req, res);
595
+ if (!body) return;
596
+ const cfg = current();
597
+ const route = await resolveRoute(ctx.llm, cfg.model, asOptionalString(body.provider), asOptionalString(body.model));
598
+ if (!route) {
599
+ writeJson(res, 409, {
600
+ ok: false,
601
+ 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"
602
+ });
603
+ return;
604
+ }
605
+ const abort = new AbortController();
606
+ res.on("close", () => {
607
+ if (!res.writableEnded) abort.abort();
608
+ });
609
+ let timedOut = false;
610
+ const timer = setTimeout(() => {
611
+ timedOut = true;
612
+ abort.abort();
613
+ }, 2e4);
614
+ const started = Date.now();
615
+ try {
616
+ const message = {
617
+ id: `prompt-optimizer-test-${crypto.randomUUID()}`,
618
+ role: "user",
619
+ content: [{ type: "text", text: "ping" }],
620
+ source: { kind: "plugin", plugin: name }
621
+ };
622
+ const result = await collectText(
623
+ ctx.llm.stream({ provider: route.provider, model: route.model, messages: [message], maxTokens: 32, signal: abort.signal })
624
+ );
625
+ writeJson(res, 200, {
626
+ ok: true,
627
+ provider: route.provider,
628
+ model: route.model,
629
+ latencyMs: Date.now() - started,
630
+ reply: result.text.slice(0, 80)
631
+ });
632
+ } catch (error) {
633
+ if (timedOut) {
634
+ writeJson(res, 200, { ok: false, error: "\u8FDE\u63A5\u6D4B\u8BD5\u8D85\u65F6(20s \u65E0\u54CD\u5E94)" });
635
+ } else if (!abort.signal.aborted) {
636
+ writeJson(res, 200, { ok: false, error: error instanceof Error ? error.message : String(error) });
637
+ }
638
+ } finally {
639
+ clearTimeout(timer);
640
+ }
641
+ }
642
+ }
643
+ export {
644
+ Config,
645
+ apply,
646
+ inject,
647
+ name
648
+ };