@y1x1n/dsh-prompt-optimizer 0.3.15 → 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/LICENSE +21 -21
- package/README.en.md +245 -212
- package/README.md +63 -28
- package/README.simple.md +136 -136
- package/cordis.patch.yml +5 -5
- package/lib/client.js +732 -519
- package/lib/controller.js +109 -35
- package/lib/index.js +262 -63
- package/lib/prompt.js +147 -43
- package/package.json +71 -74
package/lib/controller.js
CHANGED
|
@@ -1,3 +1,8 @@
|
|
|
1
|
+
// src/client/host-faces.ts
|
|
2
|
+
function legacyQueryFace(connection) {
|
|
3
|
+
return connection?.api;
|
|
4
|
+
}
|
|
5
|
+
|
|
1
6
|
// src/prompt.ts
|
|
2
7
|
var MARKERS = {
|
|
3
8
|
analysis: "<<<ANALYSIS>>>",
|
|
@@ -5,18 +10,18 @@ var MARKERS = {
|
|
|
5
10
|
end: "<<<END>>>"
|
|
6
11
|
};
|
|
7
12
|
var FORMAT_FULL_ZH = `# \u8F93\u51FA\u683C\u5F0F(\u4E25\u683C\u9075\u5B88,\u6807\u8BB0\u884C\u5355\u72EC\u6210\u884C)
|
|
8
|
-
${MARKERS.analysis}
|
|
9
|
-
\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
|
|
10
13
|
${MARKERS.optimized}
|
|
11
14
|
\u8F93\u51FA\u4F18\u5316\u540E\u7684\u5B8C\u6574\u63D0\u793A\u8BCD\u5168\u6587
|
|
15
|
+
${MARKERS.analysis}
|
|
16
|
+
\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
|
|
12
17
|
${MARKERS.end}
|
|
13
18
|
|
|
14
19
|
\u9664\u4E0A\u8FF0\u6807\u8BB0\u5305\u88F9\u7684\u5185\u5BB9\u5916,\u4E0D\u8981\u8F93\u51FA\u4EFB\u4F55\u5176\u4ED6\u6587\u5B57\u3002`;
|
|
15
20
|
var FORMAT_FULL_EN = `# Output format (strictly follow; markers on their own lines)
|
|
16
|
-
${MARKERS.analysis}
|
|
17
|
-
One finding per line: "dimension | issue | suggestion". If a dimension is fine: "dimension | no issue | -".
|
|
18
21
|
${MARKERS.optimized}
|
|
19
22
|
The full optimized prompt text
|
|
23
|
+
${MARKERS.analysis}
|
|
24
|
+
One finding per line: "dimension | issue | suggestion". If a dimension is fine: "dimension | no issue | -".
|
|
20
25
|
${MARKERS.end}
|
|
21
26
|
|
|
22
27
|
Do not output anything outside these markers.`;
|
|
@@ -59,27 +64,70 @@ function findMarker(text, word, fromIndex = 0) {
|
|
|
59
64
|
const match = re.exec(text);
|
|
60
65
|
return match ? { index: match.index, length: match[0].length } : null;
|
|
61
66
|
}
|
|
62
|
-
function
|
|
63
|
-
const
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
analysis = raw.slice(a.index + a.length, o ? o.index : raw.length).trim();
|
|
67
|
+
function findLastMarker(text, word, beforeIndex = text.length) {
|
|
68
|
+
const re = new RegExp(`<<<\\s*${word}\\s*>>>`, "g");
|
|
69
|
+
let last = null;
|
|
70
|
+
for (const match of text.matchAll(re)) {
|
|
71
|
+
if (match.index >= beforeIndex) break;
|
|
72
|
+
last = { index: match.index, length: match[0].length };
|
|
69
73
|
}
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
74
|
+
return last;
|
|
75
|
+
}
|
|
76
|
+
function findNextMarker(text, fromIndex) {
|
|
77
|
+
const re = /<<<\s*(?:ANALYSIS|OPTIMIZED|END)\s*>>>/g;
|
|
78
|
+
re.lastIndex = fromIndex;
|
|
79
|
+
const match = re.exec(text);
|
|
80
|
+
return match ? { index: match.index, length: match[0].length } : null;
|
|
81
|
+
}
|
|
82
|
+
function segmentAfter(text, marker) {
|
|
83
|
+
const firstEnd = findMarker(text, "END");
|
|
84
|
+
if (firstEnd && marker.index > firstEnd.index) return "";
|
|
85
|
+
const start = marker.index + marker.length;
|
|
86
|
+
const next = findNextMarker(text, start);
|
|
87
|
+
let end = next ? next.index : text.length;
|
|
88
|
+
if (firstEnd && firstEnd.index > start && firstEnd.index < end) end = firstEnd.index;
|
|
89
|
+
return text.slice(start, end).trim();
|
|
90
|
+
}
|
|
91
|
+
var THINK_OPEN = "<think>";
|
|
92
|
+
var THINK_CLOSE = "</think>";
|
|
93
|
+
function stripThinkBlocks(text) {
|
|
94
|
+
let out = "";
|
|
95
|
+
let rest = text;
|
|
96
|
+
for (; ; ) {
|
|
97
|
+
const open = rest.indexOf(THINK_OPEN);
|
|
98
|
+
if (open === -1) return out + rest;
|
|
99
|
+
out += rest.slice(0, open);
|
|
100
|
+
const close = rest.indexOf(THINK_CLOSE, open + THINK_OPEN.length);
|
|
101
|
+
if (close === -1) return out;
|
|
102
|
+
rest = rest.slice(close + THINK_CLOSE.length);
|
|
73
103
|
}
|
|
74
|
-
|
|
104
|
+
}
|
|
105
|
+
function parsePartialOptimizerOutput(raw) {
|
|
106
|
+
const text = stripThinkBlocks(raw);
|
|
107
|
+
const o = findLastMarker(text, "OPTIMIZED");
|
|
108
|
+
const a = findLastMarker(text, "ANALYSIS");
|
|
109
|
+
return {
|
|
110
|
+
analysis: a ? segmentAfter(text, a) : "",
|
|
111
|
+
optimized: o ? segmentAfter(text, o) : ""
|
|
112
|
+
};
|
|
75
113
|
}
|
|
76
114
|
function compactPartialBuffer(raw) {
|
|
77
|
-
const o =
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
${raw.slice(
|
|
115
|
+
const o = findLastMarker(raw, "OPTIMIZED");
|
|
116
|
+
const a = findLastMarker(raw, "ANALYSIS");
|
|
117
|
+
if (o && a && o.index < a.index) {
|
|
118
|
+
return {
|
|
119
|
+
compacted: `${MARKERS.analysis}
|
|
120
|
+
${raw.slice(a.index + a.length)}`,
|
|
121
|
+
analysis: "",
|
|
122
|
+
optimized: raw.slice(o.index + o.length, a.index).trim()
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
if (o) {
|
|
126
|
+
const analysis = a && a.index < o.index ? raw.slice(a.index + a.length, o.index).trim() : "";
|
|
127
|
+
return { compacted: `${MARKERS.optimized}
|
|
128
|
+
${raw.slice(o.index + o.length)}`, analysis, optimized: "" };
|
|
129
|
+
}
|
|
130
|
+
return { compacted: null, analysis: "", optimized: "" };
|
|
83
131
|
}
|
|
84
132
|
|
|
85
133
|
// src/client/controller.ts
|
|
@@ -92,6 +140,9 @@ function splitCommandPrefix(text) {
|
|
|
92
140
|
if (!match[2] || !match[2].trim()) return { body: trimmed, commandOnly: true };
|
|
93
141
|
return { prefix: match[1], body: match[2].trim() };
|
|
94
142
|
}
|
|
143
|
+
function joinLast(last) {
|
|
144
|
+
return last.prefix ? `${last.prefix} ${last.text}` : last.text;
|
|
145
|
+
}
|
|
95
146
|
function shouldAutoClose(input) {
|
|
96
147
|
if (!input.open || input.status === "loading") return false;
|
|
97
148
|
return !input.draft.trim() || !input.prevRunning && input.running || input.queued > input.prevQueued;
|
|
@@ -132,6 +183,7 @@ function createOptimizerController(ctx, opts = {}) {
|
|
|
132
183
|
let abort = null;
|
|
133
184
|
let liveRaw = "";
|
|
134
185
|
let frozenAnalysis = "";
|
|
186
|
+
let frozenOptimized = "";
|
|
135
187
|
let liveTimer = null;
|
|
136
188
|
const clearLiveTimer = () => {
|
|
137
189
|
if (liveTimer) {
|
|
@@ -162,6 +214,7 @@ function createOptimizerController(ctx, opts = {}) {
|
|
|
162
214
|
clearLiveTimer();
|
|
163
215
|
liveRaw = "";
|
|
164
216
|
frozenAnalysis = "";
|
|
217
|
+
frozenOptimized = "";
|
|
165
218
|
const controller = new AbortController();
|
|
166
219
|
abort = controller;
|
|
167
220
|
const watchdogMs = Math.max(1e3, opts.getWatchdogMs?.() ?? 125e3);
|
|
@@ -176,7 +229,7 @@ function createOptimizerController(ctx, opts = {}) {
|
|
|
176
229
|
set({
|
|
177
230
|
status: "error",
|
|
178
231
|
live: null,
|
|
179
|
-
error: `\u8BF7\u6C42\u8D85\u65F6(\u5BA2\u6237\u7AEF ${seconds} \u79D2\u65E0\u54CD\u5E94,Host \u53EF\u80FD\u672A\u8FD0\u884C)\u3002\u53EF\u5728 \u8BBE\u7F6E
|
|
232
|
+
error: `\u8BF7\u6C42\u8D85\u65F6(\u5BA2\u6237\u7AEF ${seconds} \u79D2\u65E0\u54CD\u5E94,Host \u53EF\u80FD\u672A\u8FD0\u884C)\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`
|
|
180
233
|
});
|
|
181
234
|
};
|
|
182
235
|
const startedAt = Date.now();
|
|
@@ -186,12 +239,17 @@ function createOptimizerController(ctx, opts = {}) {
|
|
|
186
239
|
(async () => {
|
|
187
240
|
if (opts.isModelPinned?.()) return void 0;
|
|
188
241
|
try {
|
|
189
|
-
const
|
|
190
|
-
if (
|
|
191
|
-
|
|
242
|
+
const directories = opts.getSessionModelDirectories?.();
|
|
243
|
+
if (!directories) {
|
|
244
|
+
console.warn("[dsh-prompt-optimizer] \u4F1A\u8BDD\u6A21\u578B\u76EE\u5F55\u670D\u52A1\u5728\u5F53\u524D dsh \u7248\u672C\u7F3A\u5E2D,\u6539\u7528 Host \u56DE\u9000");
|
|
245
|
+
return void 0;
|
|
246
|
+
}
|
|
247
|
+
const state2 = await directories.directoryFor(sessionId).load();
|
|
248
|
+
const current = state2.current;
|
|
249
|
+
if (current?.provider && current.model) {
|
|
192
250
|
return { provider: current.provider, model: current.model, reasoningEffort: current.reasoningEffort };
|
|
193
251
|
}
|
|
194
|
-
console.warn("[dsh-prompt-optimizer] \u4F1A\u8BDD\
|
|
252
|
+
console.warn("[dsh-prompt-optimizer] \u4F1A\u8BDD\u6CA1\u6709\u53EF\u7528\u7684\u6A21\u578B\u9009\u62E9,\u6539\u7528 Host \u56DE\u9000:", state2.error ?? current);
|
|
195
253
|
} catch (cause) {
|
|
196
254
|
console.warn("[dsh-prompt-optimizer] \u4F1A\u8BDD\u6A21\u578B\u67E5\u8BE2\u5931\u8D25,\u6539\u7528 Host \u56DE\u9000:", cause);
|
|
197
255
|
}
|
|
@@ -200,12 +258,17 @@ function createOptimizerController(ctx, opts = {}) {
|
|
|
200
258
|
(async () => {
|
|
201
259
|
if (!(opts.isContextEnabled?.() ?? true)) return void 0;
|
|
202
260
|
try {
|
|
203
|
-
const
|
|
204
|
-
if (
|
|
205
|
-
|
|
261
|
+
const history = legacyQueryFace(ctx.connection)?.sessions?.history;
|
|
262
|
+
if (!history) {
|
|
263
|
+
console.warn("[dsh-prompt-optimizer] \u4F1A\u8BDD\u5386\u53F2\u67E5\u8BE2\u9762\u5728\u5F53\u524D dsh \u7248\u672C\u7F3A\u5E2D,\u6309\u65E0\u4E0A\u4E0B\u6587\u4F18\u5316");
|
|
264
|
+
return void 0;
|
|
265
|
+
}
|
|
266
|
+
const result = await history({ sessionId, maxMessages: 24 }, controller.signal);
|
|
267
|
+
if (result.ok) {
|
|
268
|
+
const turns = extractContextTurns(result.value.events ?? []);
|
|
206
269
|
return turns.length ? turns : void 0;
|
|
207
270
|
}
|
|
208
|
-
console.warn("[dsh-prompt-optimizer] \u4F1A\u8BDD\u5386\u53F2\u67E5\u8BE2\u88AB\u62D2\u7EDD,\u6309\u65E0\u4E0A\u4E0B\u6587\u4F18\u5316:",
|
|
271
|
+
console.warn("[dsh-prompt-optimizer] \u4F1A\u8BDD\u5386\u53F2\u67E5\u8BE2\u88AB\u62D2\u7EDD,\u6309\u65E0\u4E0A\u4E0B\u6587\u4F18\u5316:", result.error);
|
|
209
272
|
} catch (cause) {
|
|
210
273
|
console.warn("[dsh-prompt-optimizer] \u4F1A\u8BDD\u5386\u53F2\u67E5\u8BE2\u5931\u8D25,\u6309\u65E0\u4E0A\u4E0B\u6587\u4F18\u5316:", cause);
|
|
211
274
|
}
|
|
@@ -215,7 +278,10 @@ function createOptimizerController(ctx, opts = {}) {
|
|
|
215
278
|
const resp = await fetch(ROUTE, {
|
|
216
279
|
method: "POST",
|
|
217
280
|
headers: { "content-type": "application/json" },
|
|
218
|
-
|
|
281
|
+
// sessionId 随请求带给 Host:Host 把它标注到 llm 调用上,要求会话头
|
|
282
|
+
// 的提供方(如 opencode 的 x-opencode-session)才能路由成功,同时
|
|
283
|
+
// 会话级计量会把这次调用归属到当前会话。
|
|
284
|
+
body: JSON.stringify({ text: draft, sessionId, provider: selection?.provider, model: selection?.model, reasoningEffort: selection?.reasoningEffort, context, previous }),
|
|
219
285
|
signal: controller.signal
|
|
220
286
|
});
|
|
221
287
|
if (!resp.ok || !(resp.headers.get("content-type") ?? "").includes("text/event-stream")) {
|
|
@@ -240,7 +306,13 @@ function createOptimizerController(ctx, opts = {}) {
|
|
|
240
306
|
buffer = buffer.slice(sep + 2);
|
|
241
307
|
const line = frame.split("\n").find((l) => l.startsWith("data:"));
|
|
242
308
|
if (!line) continue;
|
|
243
|
-
|
|
309
|
+
let event;
|
|
310
|
+
try {
|
|
311
|
+
event = JSON.parse(line.slice(5).trim());
|
|
312
|
+
} catch {
|
|
313
|
+
console.warn("[dsh-prompt-optimizer] \u8DF3\u8FC7\u65E0\u6CD5\u89E3\u6790\u7684 SSE \u5E27:", line.slice(0, 120));
|
|
314
|
+
continue;
|
|
315
|
+
}
|
|
244
316
|
if (event.type === "delta") {
|
|
245
317
|
liveRaw += String(event.text ?? "");
|
|
246
318
|
if (!liveTimer) {
|
|
@@ -249,11 +321,12 @@ function createOptimizerController(ctx, opts = {}) {
|
|
|
249
321
|
if (state.status === "loading" && abort === controller) {
|
|
250
322
|
const comp = compactPartialBuffer(liveRaw);
|
|
251
323
|
if (comp.compacted !== null) {
|
|
252
|
-
frozenAnalysis = comp.analysis;
|
|
324
|
+
if (comp.analysis) frozenAnalysis = comp.analysis;
|
|
325
|
+
if (comp.optimized) frozenOptimized = comp.optimized;
|
|
253
326
|
liveRaw = comp.compacted;
|
|
254
327
|
}
|
|
255
328
|
const partial = parsePartialOptimizerOutput(liveRaw);
|
|
256
|
-
const live = { analysis: partial.analysis || frozenAnalysis, optimized: partial.optimized };
|
|
329
|
+
const live = { analysis: partial.analysis || frozenAnalysis, optimized: partial.optimized || frozenOptimized };
|
|
257
330
|
if (opts.isFastMode?.() && !live.analysis && !live.optimized && liveRaw.trim() && !liveRaw.includes("<<<")) {
|
|
258
331
|
live.optimized = liveRaw.trim();
|
|
259
332
|
}
|
|
@@ -312,7 +385,7 @@ function createOptimizerController(ctx, opts = {}) {
|
|
|
312
385
|
getSnapshot: () => state,
|
|
313
386
|
optimize,
|
|
314
387
|
retry() {
|
|
315
|
-
if (state.last) void optimize(state.last
|
|
388
|
+
if (state.last) void optimize(joinLast(state.last), state.last.sessionId);
|
|
316
389
|
},
|
|
317
390
|
markApplied(applied) {
|
|
318
391
|
set({ applied });
|
|
@@ -343,6 +416,7 @@ function createOptimizerController(ctx, opts = {}) {
|
|
|
343
416
|
export {
|
|
344
417
|
createOptimizerController,
|
|
345
418
|
extractContextTurns,
|
|
419
|
+
joinLast,
|
|
346
420
|
shouldAutoClose,
|
|
347
421
|
splitCommandPrefix
|
|
348
422
|
};
|