@surfishs/dsh-prompt-enhance 0.1.2 → 0.1.3
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/README.en.md +153 -44
- package/README.md +151 -42
- package/docs/images/panel-en-915.png +0 -0
- package/docs/images/panel.png +0 -0
- package/lib/client.js +189 -12
- package/lib/index.js +45 -12
- package/lib/index.js.map +2 -2
- package/package.json +1 -1
- package/src/client/ResultPanel.tsx +58 -1
- package/src/client/enhance-client.ts +38 -0
- package/src/client/locales.ts +21 -10
- package/src/client/settings-page-controls.tsx +1 -1
- package/src/client/styles.ts +106 -0
- package/src/context.ts +87 -7
- package/src/orchestrate.ts +24 -5
- package/src/shared/protocol.ts +20 -0
package/lib/index.js
CHANGED
|
@@ -817,6 +817,12 @@ function toEnhanceError(error) {
|
|
|
817
817
|
import { randomUUID } from "node:crypto";
|
|
818
818
|
|
|
819
819
|
// src/context.ts
|
|
820
|
+
function isInjectedUserContext(message) {
|
|
821
|
+
const source = message.source;
|
|
822
|
+
if (source === null || typeof source !== "object") return false;
|
|
823
|
+
const kind = source.kind;
|
|
824
|
+
return typeof kind === "string" && kind !== "user";
|
|
825
|
+
}
|
|
820
826
|
var TRUNCATION_MARK = "\u2026 (truncated)";
|
|
821
827
|
function cutToCodePoints(text, max) {
|
|
822
828
|
if (max <= 0) return "";
|
|
@@ -843,15 +849,20 @@ function turnTextOf(message) {
|
|
|
843
849
|
}
|
|
844
850
|
return parts.join("\n");
|
|
845
851
|
}
|
|
846
|
-
function
|
|
852
|
+
function selectTurnsWithUsage(messages, options, draft) {
|
|
847
853
|
const maxMessages = Number.isInteger(options.maxMessages) ? Math.max(0, options.maxMessages) : 0;
|
|
848
854
|
const maxChars = Number.isInteger(options.maxChars) ? Math.max(0, options.maxChars) : 0;
|
|
849
|
-
|
|
855
|
+
const empty = () => ({
|
|
856
|
+
turns: [],
|
|
857
|
+
usage: { usedTurns: 0, maxTurns: maxMessages, usedChars: 0, maxChars, droppedTurns: 0, limitedBy: null, cutTurn: false }
|
|
858
|
+
});
|
|
859
|
+
if (maxMessages === 0 || maxChars === 0) return empty();
|
|
850
860
|
const turns = [];
|
|
851
861
|
for (const message of messages) {
|
|
852
862
|
if (message === null || typeof message !== "object") continue;
|
|
853
863
|
const record = message;
|
|
854
864
|
if (record.role !== "user" && record.role !== "assistant") continue;
|
|
865
|
+
if (record.role === "user" && isInjectedUserContext(record)) continue;
|
|
855
866
|
const text = turnTextOf(record);
|
|
856
867
|
if (text === "") continue;
|
|
857
868
|
turns.push({ role: record.role, text });
|
|
@@ -868,23 +879,44 @@ function selectTurns(messages, options, draft) {
|
|
|
868
879
|
}
|
|
869
880
|
}
|
|
870
881
|
}
|
|
882
|
+
const total = turns.length;
|
|
871
883
|
const windowed = turns.slice(-maxMessages);
|
|
884
|
+
const droppedByMessages = total - windowed.length;
|
|
872
885
|
const kept = [];
|
|
873
886
|
let used = 0;
|
|
887
|
+
let limitedBy = null;
|
|
888
|
+
let cutTurn = false;
|
|
874
889
|
for (let index = windowed.length - 1; index >= 0; index--) {
|
|
875
890
|
const turn = windowed[index];
|
|
876
891
|
const length = countText(turn.text);
|
|
877
892
|
if (used + length > maxChars) {
|
|
878
893
|
const room = maxChars - used;
|
|
879
894
|
if (kept.length === 0 && room > TRUNCATION_MARK.length + 40) {
|
|
880
|
-
|
|
895
|
+
const cut = `${cutToCodePoints(turn.text, room - TRUNCATION_MARK.length).trimEnd()}${TRUNCATION_MARK}`;
|
|
896
|
+
kept.unshift({ role: turn.role, text: cut });
|
|
897
|
+
cutTurn = true;
|
|
898
|
+
} else {
|
|
899
|
+
limitedBy = "chars";
|
|
881
900
|
}
|
|
882
901
|
break;
|
|
883
902
|
}
|
|
884
903
|
used += length;
|
|
885
904
|
kept.unshift(turn);
|
|
886
905
|
}
|
|
887
|
-
|
|
906
|
+
if (!cutTurn && limitedBy === null && droppedByMessages > 0) limitedBy = "turns";
|
|
907
|
+
const usedChars = kept.reduce((sum, turn) => sum + countText(turn.text), 0);
|
|
908
|
+
return {
|
|
909
|
+
turns: kept,
|
|
910
|
+
usage: {
|
|
911
|
+
usedTurns: kept.length,
|
|
912
|
+
maxTurns: maxMessages,
|
|
913
|
+
usedChars,
|
|
914
|
+
maxChars,
|
|
915
|
+
droppedTurns: total - kept.length,
|
|
916
|
+
limitedBy,
|
|
917
|
+
cutTurn
|
|
918
|
+
}
|
|
919
|
+
};
|
|
888
920
|
}
|
|
889
921
|
function neutralize(text) {
|
|
890
922
|
return text.replace(/<\/?(conversation_context)>/gi, "<\\/$1>");
|
|
@@ -895,11 +927,11 @@ function formatContextBlock(turns) {
|
|
|
895
927
|
${body}
|
|
896
928
|
</conversation_context>`;
|
|
897
929
|
}
|
|
898
|
-
function
|
|
930
|
+
function buildConversationContextWithUsage(messages, options, draft) {
|
|
899
931
|
if (messages === void 0) return void 0;
|
|
900
|
-
const
|
|
901
|
-
if (turns.length === 0) return void 0;
|
|
902
|
-
return formatContextBlock(turns);
|
|
932
|
+
const selection = selectTurnsWithUsage(messages, options, draft);
|
|
933
|
+
if (selection.turns.length === 0) return void 0;
|
|
934
|
+
return { text: formatContextBlock(selection.turns), usage: selection.usage };
|
|
903
935
|
}
|
|
904
936
|
|
|
905
937
|
// src/reasoning-cache.ts
|
|
@@ -1055,7 +1087,7 @@ function defaultRouteOf(ctx) {
|
|
|
1055
1087
|
const value = serviceEntry(ctx, "settings", "agent-default-model");
|
|
1056
1088
|
return routeOf(value);
|
|
1057
1089
|
}
|
|
1058
|
-
function
|
|
1090
|
+
function conversationContextWithUsage(ctx, sessionId, config, draft) {
|
|
1059
1091
|
if (!config.contextAware) return void 0;
|
|
1060
1092
|
if (sessionId === void 0 || sessionId === "") return void 0;
|
|
1061
1093
|
const session = serviceEntry(ctx, "sessions", sessionId);
|
|
@@ -1068,7 +1100,7 @@ function conversationContextOf(ctx, sessionId, config, draft) {
|
|
|
1068
1100
|
} catch {
|
|
1069
1101
|
return void 0;
|
|
1070
1102
|
}
|
|
1071
|
-
return
|
|
1103
|
+
return buildConversationContextWithUsage(messages, { maxMessages: config.contextMaxMessages, maxChars: config.contextMaxChars }, draft);
|
|
1072
1104
|
}
|
|
1073
1105
|
async function effectiveReasoningEffort(llm, provider, model, configured) {
|
|
1074
1106
|
if (configured === "inherit") return configured;
|
|
@@ -1111,7 +1143,8 @@ async function runEnhance(ctx, config, options) {
|
|
|
1111
1143
|
if (llm === void 0) {
|
|
1112
1144
|
throw new EnhanceFailure({ code: "internal" });
|
|
1113
1145
|
}
|
|
1114
|
-
const
|
|
1146
|
+
const derivedContext = options.context === void 0 ? conversationContextWithUsage(ctx, options.sessionId, config, options.text) : void 0;
|
|
1147
|
+
const context = options.context !== void 0 ? options.context : derivedContext?.text;
|
|
1115
1148
|
const effort = await effectiveReasoningEffort(llm, route.provider, route.model, config.reasoningEffort);
|
|
1116
1149
|
const result = await enhanceText(llm, {
|
|
1117
1150
|
route,
|
|
@@ -1129,7 +1162,7 @@ async function runEnhance(ctx, config, options) {
|
|
|
1129
1162
|
...options.sessionId !== void 0 ? { sessionId: options.sessionId } : {}
|
|
1130
1163
|
});
|
|
1131
1164
|
console.info(`[dsh-prompt-enhance] ${requestId} in=${countText(options.text)} out=${countText(result.text)} ctx=${context === void 0 ? 0 : 1} ${result.elapsedMs}ms ok`);
|
|
1132
|
-
return result;
|
|
1165
|
+
return derivedContext === void 0 ? result : { ...result, context: derivedContext.usage };
|
|
1133
1166
|
} catch (error) {
|
|
1134
1167
|
const wire = toEnhanceError(error);
|
|
1135
1168
|
console.info(`[dsh-prompt-enhance] ${requestId} in=${countText(options.text)} error=${wire.code} ${Date.now() - started}ms`);
|