@surfishs/dsh-prompt-enhance 0.1.1 → 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/lib/index.js CHANGED
@@ -79,7 +79,11 @@ var DEFAULT_VALUES = {
79
79
  streaming: true,
80
80
  contextAware: true,
81
81
  contextMaxMessages: 8,
82
- contextMaxChars: 4e3
82
+ contextMaxChars: 4e3,
83
+ /** Play the card's expand/collapse transition. */
84
+ foldAnimation: true,
85
+ /** Fold the card after Apply, and re-open it after Undo. */
86
+ autoFold: true
83
87
  };
84
88
 
85
89
  // src/shared/limits.ts
@@ -112,6 +116,8 @@ var Config = z.object({
112
116
  maxConcurrent: z.number().step(1).min(FIELD_LIMITS.maxConcurrent.min).max(FIELD_LIMITS.maxConcurrent.max).default(DEFAULT_CONFIG.maxConcurrent).description("\u5BBF\u4E3B\u4FA7\u5E76\u53D1\u4E0A\u9650\uFF1A\u540C\u65F6\u8FDB\u884C\u7684\u589E\u5F3A\u8C03\u7528\u6570\uFF0C\u8D85\u51FA\u7684\u8BF7\u6C42\u8FD4\u56DE 429").volatile(),
113
117
  rateLimitPerMinute: z.number().step(1).min(FIELD_LIMITS.rateLimitPerMinute.min).max(FIELD_LIMITS.rateLimitPerMinute.max).default(DEFAULT_CONFIG.rateLimitPerMinute).description("\u6BCF\u5206\u949F\u589E\u5F3A\u6B21\u6570\u4E0A\u9650\uFF08\u6ED1\u52A8\u7A97\u53E3\uFF09\uFF0C\u8D85\u51FA\u8FD4\u56DE 429").volatile(),
114
118
  streaming: z.boolean().default(DEFAULT_CONFIG.streaming).description("\u589E\u91CF\u5C55\u793A\uFF1A\u6A21\u578B\u8FB9\u5199\u8FB9\u5728\u9762\u677F\u663E\u793A\uFF08\u4EC5\u5F71\u54CD\u663E\u793A\u8282\u594F\uFF0C\u6700\u7EC8\u7ED3\u679C\u4E0D\u53D8\uFF1B\u5BBF\u4E3B\u6216\u7F51\u7EDC\u4E0D\u652F\u6301\u65F6\u81EA\u52A8\u56DE\u9000\u4E3A\u4E00\u6B21\u6027\u8FD4\u56DE\uFF09").volatile(),
119
+ foldAnimation: z.boolean().default(DEFAULT_CONFIG.foldAnimation).description("\u5361\u7247\u6298\u53E0\u52A8\u753B\uFF1A\u5C55\u5F00/\u6536\u8D77\u65F6\u64AD\u653E\u8FC7\u6E21\uFF1B\u5173\u95ED\u540E\u7ACB\u5373\u5207\u6362\uFF08\u7CFB\u7EDF\u300C\u51CF\u5C11\u52A8\u6001\u6548\u679C\u300D\u504F\u597D\u59CB\u7EC8\u4F18\u5148\uFF09").volatile(),
120
+ autoFold: z.boolean().default(DEFAULT_CONFIG.autoFold).description("\u81EA\u52A8\u6536\u7EB3\u5361\u7247\uFF1A\u56DE\u586B\u5230\u8F93\u5165\u6846\u540E\u81EA\u52A8\u6536\u8D77\u5361\u7247\uFF0C\u64A4\u9500\u540E\u81EA\u52A8\u5C55\u5F00\uFF1B\u5173\u95ED\u540E\u5361\u7247\u4FDD\u6301\u5F53\u524D\u5C55\u5F00\u72B6\u6001").volatile(),
115
121
  contextAware: z.boolean().default(DEFAULT_CONFIG.contextAware).description("\u4E0A\u4E0B\u6587\u611F\u77E5\uFF1A\u8BFB\u53D6\u5F53\u524D\u4F1A\u8BDD\u8FD1\u671F\u5BF9\u8BDD\uFF0C\u7528\u4E8E\u6D88\u89E3\u6307\u4EE3\u3001\u8865\u5168\u7701\u7565\u4E0E\u672F\u8BED\u7EA6\u675F\uFF1B\u65E0\u5386\u53F2\u6216\u5386\u53F2\u4E0D\u8DB3\u65F6\u81EA\u52A8\u56DE\u9000\u4E3A\u5355\u6761\u589E\u5F3A\uFF0C\u4E0D\u81C6\u9020\u4FE1\u606F").volatile(),
116
122
  contextMaxMessages: z.number().step(1).min(FIELD_LIMITS.contextMaxMessages.min).max(FIELD_LIMITS.contextMaxMessages.max).default(DEFAULT_CONFIG.contextMaxMessages).description("\u4E0A\u4E0B\u6587\u7A97\u53E3\uFF08\u6761\u6570\uFF09\uFF1A\u6700\u591A\u53C2\u8003\u6700\u8FD1\u591A\u5C11\u6761 user/assistant \u8F6E\u6B21\uFF1B0 \u8868\u793A\u4E0D\u53C2\u8003").volatile(),
117
123
  contextMaxChars: z.number().step(1).min(FIELD_LIMITS.contextMaxChars.min).max(FIELD_LIMITS.contextMaxChars.max).default(DEFAULT_CONFIG.contextMaxChars).description("\u4E0A\u4E0B\u6587\u7A97\u53E3\uFF08\u5B57\u6570\uFF09\uFF1A\u5386\u53F2\u7247\u6BB5\u7684\u5B57\u7B26\u9884\u7B97\uFF0C\u4ECE\u6700\u65B0\u4E00\u6761\u5411\u524D\u88C5\u914D\uFF1B0 \u8868\u793A\u4E0D\u53C2\u8003").volatile()
@@ -166,6 +172,8 @@ function resolveConfig(config) {
166
172
  maxConcurrent: intInRange(config.maxConcurrent, "maxConcurrent", FIELD_LIMITS.maxConcurrent),
167
173
  rateLimitPerMinute: intInRange(config.rateLimitPerMinute, "rateLimitPerMinute", FIELD_LIMITS.rateLimitPerMinute),
168
174
  streaming: typeof config.streaming === "boolean" ? config.streaming : DEFAULT_CONFIG.streaming,
175
+ foldAnimation: typeof config.foldAnimation === "boolean" ? config.foldAnimation : DEFAULT_CONFIG.foldAnimation,
176
+ autoFold: typeof config.autoFold === "boolean" ? config.autoFold : DEFAULT_CONFIG.autoFold,
169
177
  contextAware: typeof config.contextAware === "boolean" ? config.contextAware : DEFAULT_CONFIG.contextAware,
170
178
  contextMaxMessages: intInRange(config.contextMaxMessages, "contextMaxMessages", FIELD_LIMITS.contextMaxMessages),
171
179
  contextMaxChars: intInRange(config.contextMaxChars, "contextMaxChars", FIELD_LIMITS.contextMaxChars)
@@ -680,6 +688,9 @@ async function enhanceText(llm, options) {
680
688
  provider: options.route.provider,
681
689
  model: options.route.model,
682
690
  elapsedMs: Date.now() - started,
691
+ // What was actually sent: a rejected tier is retried without the field, so
692
+ // the retry path reports inherit rather than the tier that never ran.
693
+ reasoningEffort: retriedWithoutEffort ? "inherit" : requestedEffort,
683
694
  ...reportedUsage === void 0 ? {} : { usage: reportedUsage },
684
695
  ...outcome.truncated ? { truncated: true } : {}
685
696
  };
@@ -806,6 +817,12 @@ function toEnhanceError(error) {
806
817
  import { randomUUID } from "node:crypto";
807
818
 
808
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
+ }
809
826
  var TRUNCATION_MARK = "\u2026 (truncated)";
810
827
  function cutToCodePoints(text, max) {
811
828
  if (max <= 0) return "";
@@ -832,15 +849,20 @@ function turnTextOf(message) {
832
849
  }
833
850
  return parts.join("\n");
834
851
  }
835
- function selectTurns(messages, options, draft) {
852
+ function selectTurnsWithUsage(messages, options, draft) {
836
853
  const maxMessages = Number.isInteger(options.maxMessages) ? Math.max(0, options.maxMessages) : 0;
837
854
  const maxChars = Number.isInteger(options.maxChars) ? Math.max(0, options.maxChars) : 0;
838
- if (maxMessages === 0 || maxChars === 0) return [];
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();
839
860
  const turns = [];
840
861
  for (const message of messages) {
841
862
  if (message === null || typeof message !== "object") continue;
842
863
  const record = message;
843
864
  if (record.role !== "user" && record.role !== "assistant") continue;
865
+ if (record.role === "user" && isInjectedUserContext(record)) continue;
844
866
  const text = turnTextOf(record);
845
867
  if (text === "") continue;
846
868
  turns.push({ role: record.role, text });
@@ -857,23 +879,44 @@ function selectTurns(messages, options, draft) {
857
879
  }
858
880
  }
859
881
  }
882
+ const total = turns.length;
860
883
  const windowed = turns.slice(-maxMessages);
884
+ const droppedByMessages = total - windowed.length;
861
885
  const kept = [];
862
886
  let used = 0;
887
+ let limitedBy = null;
888
+ let cutTurn = false;
863
889
  for (let index = windowed.length - 1; index >= 0; index--) {
864
890
  const turn = windowed[index];
865
891
  const length = countText(turn.text);
866
892
  if (used + length > maxChars) {
867
893
  const room = maxChars - used;
868
894
  if (kept.length === 0 && room > TRUNCATION_MARK.length + 40) {
869
- kept.unshift({ role: turn.role, text: `${cutToCodePoints(turn.text, room - TRUNCATION_MARK.length).trimEnd()}${TRUNCATION_MARK}` });
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";
870
900
  }
871
901
  break;
872
902
  }
873
903
  used += length;
874
904
  kept.unshift(turn);
875
905
  }
876
- return kept;
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
+ };
877
920
  }
878
921
  function neutralize(text) {
879
922
  return text.replace(/<\/?(conversation_context)>/gi, "<\\/$1>");
@@ -884,11 +927,11 @@ function formatContextBlock(turns) {
884
927
  ${body}
885
928
  </conversation_context>`;
886
929
  }
887
- function buildConversationContext(messages, options, draft) {
930
+ function buildConversationContextWithUsage(messages, options, draft) {
888
931
  if (messages === void 0) return void 0;
889
- const turns = selectTurns(messages, options, draft);
890
- if (turns.length === 0) return void 0;
891
- 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 };
892
935
  }
893
936
 
894
937
  // src/reasoning-cache.ts
@@ -1044,7 +1087,7 @@ function defaultRouteOf(ctx) {
1044
1087
  const value = serviceEntry(ctx, "settings", "agent-default-model");
1045
1088
  return routeOf(value);
1046
1089
  }
1047
- function conversationContextOf(ctx, sessionId, config, draft) {
1090
+ function conversationContextWithUsage(ctx, sessionId, config, draft) {
1048
1091
  if (!config.contextAware) return void 0;
1049
1092
  if (sessionId === void 0 || sessionId === "") return void 0;
1050
1093
  const session = serviceEntry(ctx, "sessions", sessionId);
@@ -1057,7 +1100,7 @@ function conversationContextOf(ctx, sessionId, config, draft) {
1057
1100
  } catch {
1058
1101
  return void 0;
1059
1102
  }
1060
- return buildConversationContext(messages, { maxMessages: config.contextMaxMessages, maxChars: config.contextMaxChars }, draft);
1103
+ return buildConversationContextWithUsage(messages, { maxMessages: config.contextMaxMessages, maxChars: config.contextMaxChars }, draft);
1061
1104
  }
1062
1105
  async function effectiveReasoningEffort(llm, provider, model, configured) {
1063
1106
  if (configured === "inherit") return configured;
@@ -1100,7 +1143,8 @@ async function runEnhance(ctx, config, options) {
1100
1143
  if (llm === void 0) {
1101
1144
  throw new EnhanceFailure({ code: "internal" });
1102
1145
  }
1103
- const context = options.context !== void 0 ? options.context : conversationContextOf(ctx, options.sessionId, config, options.text);
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;
1104
1148
  const effort = await effectiveReasoningEffort(llm, route.provider, route.model, config.reasoningEffort);
1105
1149
  const result = await enhanceText(llm, {
1106
1150
  route,
@@ -1118,7 +1162,7 @@ async function runEnhance(ctx, config, options) {
1118
1162
  ...options.sessionId !== void 0 ? { sessionId: options.sessionId } : {}
1119
1163
  });
1120
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`);
1121
- return result;
1165
+ return derivedContext === void 0 ? result : { ...result, context: derivedContext.usage };
1122
1166
  } catch (error) {
1123
1167
  const wire = toEnhanceError(error);
1124
1168
  console.info(`[dsh-prompt-enhance] ${requestId} in=${countText(options.text)} error=${wire.code} ${Date.now() - started}ms`);