dsh-thinking-lang 1.0.6 → 1.0.8

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/src/index.ts CHANGED
@@ -6,10 +6,14 @@
6
6
  * 注入形态(设计理由见 README):
7
7
  *
8
8
  * section order 1 → the authoritative rule, once per session
9
- * pre-step step 1 → a short reminder, at most once per turn (10 min apart)
9
+ * pre-step step 1 → a short reminder at the head of the batch, at most once per turn
10
+ * (interval configurable, default 30 min)
11
+ * pre-step step 1 → the closing rule, folded into the skill catalogue when the
12
+ * extra-injection setting is on
10
13
  *
11
14
  * section order 1 → 权威规则,每会话组装一次
12
- * pre-step step 1 → 短提醒,每轮最多一次(间隔 10 分钟)
15
+ * pre-step step 1 → 批次开头的短提醒,每轮最多一次(间隔可配置,默认 30 分钟)
16
+ * pre-step step 1 → 收尾规则,额外注入开启时折进技能目录
13
17
  *
14
18
  * The host context is described by a local structural interface rather than the
15
19
  * `@deepseek-ai/cordis` type, so this package carries no DSH type dependency.
@@ -20,7 +24,12 @@ import { randomUUID } from "node:crypto";
20
24
  import z from "@deepseek-ai/schemastery";
21
25
  import {
22
26
  AUTO,
27
+ DEFAULT_REMINDER_MINUTES,
28
+ DEFAULTS,
29
+ FIELD_EXTRA,
23
30
  FIELD_LANGUAGE,
31
+ FIELD_REMINDER,
32
+ REMINDER_OFF,
24
33
  SELECTABLE_TAGS,
25
34
  SETTINGS_NAMESPACE,
26
35
  type Reader,
@@ -28,7 +37,9 @@ import {
28
37
  effectiveCode,
29
38
  nativeNameFor,
30
39
  normalizeSettings,
40
+ reminderDelayMs,
31
41
  reminderText,
42
+ tailText,
32
43
  sectionText
33
44
  } from "./core.js";
34
45
 
@@ -47,9 +58,6 @@ export const inject: string[] = [];
47
58
  */
48
59
  const SECTION_ORDER = 1;
49
60
 
50
- /** Minimum milliseconds between two per-turn reminders in one session. / 同一会话中两次每轮提醒之间的最小毫秒数。 */
51
- const REMINDER_INTERVAL_MS = 600_000;
52
-
53
61
  /** Settings namespace holding the desktop UI locale. / 保存桌面 UI 语言的设置命名空间。 */
54
62
  const LOCALE_NAMESPACE = "locale";
55
63
 
@@ -118,7 +126,11 @@ function labelled<T>(node: z<T>, label: string | undefined): z<T> {
118
126
  */
119
127
  function buildSchema(): z<ThinkingSettings> {
120
128
  const choices = [AUTO, ...SELECTABLE_TAGS].map((value) => labelled(z.const(value), nativeNameFor(value)));
121
- return z.object({ [FIELD_LANGUAGE]: live(z.union(choices).default(AUTO)) });
129
+ return z.object({
130
+ [FIELD_LANGUAGE]: live(z.union(choices).default(AUTO)),
131
+ [FIELD_REMINDER]: live(z.number().default(DEFAULT_REMINDER_MINUTES)),
132
+ [FIELD_EXTRA]: live(z.boolean().default(DEFAULTS.extraInjection))
133
+ });
122
134
  }
123
135
 
124
136
  /** Durable settings schema; the field defaults to {@link AUTO}. / 持久化设置 schema;字段默认为 {@link AUTO}。 */
@@ -261,13 +273,16 @@ function osLocale(): string | undefined {
261
273
  }
262
274
 
263
275
  /**
264
- * Resolve the effective language for one prompt build.
265
- * 为一次提示词构建解析出生效语言。
276
+ * Resolve the configuration in effect for one prompt build.
277
+ * 为一次提示词构建解析出当前生效的配置。
266
278
  */
267
- function currentLanguage(reader: Reader): string {
279
+ function currentSettings(reader: Reader): ThinkingSettings {
268
280
  const settings = normalizeSettings(sectionOf(reader, SETTINGS_NAMESPACE));
269
281
  const desktop = sectionOf(reader, LOCALE_NAMESPACE)?.[LOCALE_FIELD];
270
- return effectiveCode(settings, osLocale(), typeof desktop === "string" ? desktop : undefined);
282
+ return {
283
+ ...settings,
284
+ language: effectiveCode(settings, osLocale(), typeof desktop === "string" ? desktop : undefined)
285
+ };
271
286
  }
272
287
 
273
288
  /** One pre-step payload slice this entry reads. / 本入口读取的 pre-step 载荷切片。 */
@@ -293,6 +308,7 @@ interface HostContext {
293
308
  listener: (payload: PreStepPayload, next: () => Promise<PreStepDecision>) => Promise<PreStepDecision>,
294
309
  options?: { prepend?: boolean }
295
310
  ): unknown;
311
+ on(event: "agent/created", listener: (payload: unknown) => unknown): unknown;
296
312
  systemPrompt: {
297
313
  section(section: { name: string; order?: number; text: () => string }): unknown;
298
314
  };
@@ -315,6 +331,40 @@ function reminderMessage(text: string): Record<string, unknown> {
315
331
  };
316
332
  }
317
333
 
334
+ /**
335
+ * Put the rule in front of the text a message already carries.
336
+ * 把规则放到消息已有文本的前面。
337
+ */
338
+ function prefixPrompt(message: Record<string, unknown>, text: string): Record<string, unknown> {
339
+ const content = Array.isArray(message.content) ? (message.content as Array<Record<string, unknown>>) : [];
340
+ const head = content[0];
341
+ if (head && head.type === "text" && typeof head.text === "string" && head.text.startsWith(text)) return message;
342
+ return { ...message, content: [{ type: "text", text: `${text}\n` }, ...content] };
343
+ }
344
+
345
+ /**
346
+ * Rewrite the skill-catalog injection so the rule opens a block the model reads as
347
+ * instruction, not as the text it has to answer. Falls back to the last injected
348
+ * message when no skill catalogue is present. / 改写 skill-catalog 注入,让规则出现在
349
+ * 模型读作指令的块开头;没有技能目录时退回最后一条注入消息。
350
+ */
351
+ function prefixInjected(messages: Array<Record<string, unknown>>, text: string): Array<Record<string, unknown>> {
352
+ let index = messages.findIndex((message) => (message.source as { kind?: unknown } | undefined)?.kind === "skill-catalog");
353
+ if (index < 0) {
354
+ for (let cursor = messages.length - 1; cursor >= 0; cursor -= 1) {
355
+ const source = messages[cursor].source as { kind?: unknown } | undefined;
356
+ if (messages[cursor].role === "user" && source?.kind !== name) {
357
+ index = cursor;
358
+ break;
359
+ }
360
+ }
361
+ }
362
+ if (index < 0) return messages;
363
+ const next = [...messages];
364
+ next[index] = prefixPrompt(messages[index], text);
365
+ return next;
366
+ }
367
+
318
368
  /**
319
369
  * Register the settings consumer, the prompt section, and the per-turn reminder.
320
370
  * 注册设置消费方、提示词段,以及每轮一次的提醒。
@@ -332,22 +382,48 @@ export function apply(ctx: HostContext): void {
332
382
  promptCtx.systemPrompt.section({
333
383
  name: "app:tk-lang",
334
384
  order: SECTION_ORDER,
335
- text: () => sectionText(currentLanguage(reader()))
385
+ text: () => sectionText(currentSettings(reader()).language)
336
386
  });
337
387
 
338
- // The reminder runs before the other pre-step listeners, so its message is
339
- // appended after theirs and stays closest to the end of the request.
340
- // 该提醒先于其它 pre-step 监听器执行,因此它的消息追加在它们之后,离请求末尾最近。
341
- promptCtx.on("agent/pre-step", async (payload, next) => {
388
+ /**
389
+ * Wrap the batch in two reminders: one before the prompt, one after every injection.
390
+ * 用两条提醒夹住批次:一条在提问之前,一条在全部注入之后。
391
+ */
392
+ const rewrite = async (payload: PreStepPayload, next: () => Promise<PreStepDecision>) => {
342
393
  const decision = await next();
343
394
  if (decision.kind !== "enter" || payload.signal.aborted || payload.step !== 1) return decision;
395
+ const settings = currentSettings(reader());
396
+ const delay = reminderDelayMs(settings.reminderMinutes);
397
+ if (delay === REMINDER_OFF) return decision;
344
398
  const now = Date.now();
345
399
  const previous = lastReminderAt.get(payload.agent.session.id);
346
- if (previous !== undefined && now - previous < REMINDER_INTERVAL_MS) return decision;
347
- const text = reminderText(currentLanguage(reader()));
348
- if (text === "") return decision;
400
+ if (delay > 0 && previous !== undefined && now - previous < delay) return decision;
401
+ const head = reminderText(settings.language);
402
+ if (head === "") return decision;
349
403
  lastReminderAt.set(payload.agent.session.id, now);
350
- return { ...decision, messages: [...decision.messages, reminderMessage(text)] };
351
- }, { prepend: true });
404
+ // The closing rule rides on the same switch as the reminder: turning the
405
+ // reminder off skips this whole listener, so both injections stop together.
406
+ // 收尾规则与提醒共用一个开关:提醒关闭时本段不执行,两处注入一起停。
407
+ const tail = settings.extraInjection ? tailText(settings.language) : "";
408
+ const messages = tail === "" ? decision.messages : prefixInjected(decision.messages, tail);
409
+ return { ...decision, messages: [reminderMessage(head), ...messages] };
410
+ };
411
+
412
+ /**
413
+ * `prepend` unshifts, so the listener prepended last runs first and returns last:
414
+ * only it sees every message the other pre-step listeners appended, including the
415
+ * trailing `time-context` batch.
416
+ * `prepend` 是 unshift:最后 prepend 的监听器最先执行、最后返回,只有它能看见其它
417
+ * pre-step 监听器追加的全部消息,包括压在最后的 `time-context`。
418
+ */
419
+ let disposeStep: (() => void) | undefined;
420
+ const bindOutermost = (): void => {
421
+ disposeStep?.();
422
+ disposeStep = promptCtx.on("agent/pre-step", rewrite, { prepend: true }) as () => void;
423
+ };
424
+ bindOutermost();
425
+ promptCtx.on("agent/created", () => {
426
+ bindOutermost();
427
+ });
352
428
  });
353
429
  }