dsh-thinking-lang 1.0.8 → 1.0.9

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
@@ -8,12 +8,12 @@
8
8
  * section order 1 → the authoritative rule, once per session
9
9
  * pre-step step 1 → a short reminder at the head of the batch, at most once per turn
10
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
11
+ * pre-step step 1 → the closing rule, folded into the skill catalogue or sent
12
+ * as its own message when extra injection is on
13
13
  *
14
14
  * section order 1 → 权威规则,每会话组装一次
15
15
  * pre-step step 1 → 批次开头的短提醒,每轮最多一次(间隔可配置,默认 30 分钟)
16
- * pre-step step 1 → 收尾规则,额外注入开启时折进技能目录
16
+ * pre-step step 1 → 收尾规则,额外注入开启时折进技能目录,或独立发送
17
17
  *
18
18
  * The host context is described by a local structural interface rather than the
19
19
  * `@deepseek-ai/cordis` type, so this package carries no DSH type dependency.
@@ -138,12 +138,12 @@ export const Config = buildSchema();
138
138
 
139
139
  /** The slice of the host settings service this plugin uses. / 本插件用到的宿主设置服务切片。 */
140
140
  interface HostSettings {
141
- describe(): unknown;
142
- update(entryId: string, patch: Record<string, unknown>): unknown;
141
+ describe(options: { redactSecrets: boolean }): unknown;
143
142
  }
144
143
 
145
144
  /** One read of the settings document, reusable until its TTL elapses. / 一次设置文档读取,在缓存有效期内可复用。 */
146
145
  interface SettingsSnapshot {
146
+ readonly service: HostSettings;
147
147
  readonly takenAt: number;
148
148
  readonly sections: Map<string, Record<string, unknown>>;
149
149
  }
@@ -151,25 +151,13 @@ interface SettingsSnapshot {
151
151
  /** Handed back while the settings service is unavailable or unreadable. / 设置服务不可用或读不出时交回的空表。 */
152
152
  const NO_SECTIONS: ReadonlyMap<string, Record<string, unknown>> = new Map();
153
153
 
154
- let snapshot: SettingsSnapshot | undefined;
155
-
156
- /**
157
- * Pull every namespace section out of the settings document.
158
- * 从设置文档里取出各个命名空间的段。
159
- *
160
- * A section is a `{ns, value}` entry whose `value` is a plain object; anything
161
- * else in the listing is not something this plugin can read, so it is skipped
162
- * rather than coerced.
163
- * 一个段就是 `value` 为普通对象的 `{ns, value}` 条目;列表里其它形状不是本插件能读
164
- * 的东西,因此跳过而不做强制转换。
165
- */
166
154
  /**
167
155
  * Call `describe()` without letting a throwing host escape.
168
156
  * 调用 `describe()`,不让抛异常的宿主逃逸出去。
169
157
  */
170
158
  function described(settings: HostSettings): unknown {
171
159
  try {
172
- return settings.describe();
160
+ return settings.describe({ redactSecrets: true });
173
161
  } catch {
174
162
  return undefined;
175
163
  }
@@ -190,42 +178,27 @@ function asSection(value: unknown): Record<string, unknown> | undefined {
190
178
  return value as Record<string, unknown>;
191
179
  }
192
180
 
193
- function collect(settings: HostSettings): Map<string, Record<string, unknown>> {
194
- const found = new Map<string, Record<string, unknown>>();
181
+ function collect(settings: HostSettings): Map<string, Record<string, unknown>> | undefined {
195
182
  const listed = described(settings);
196
- if (!Array.isArray(listed)) return found;
183
+ if (!Array.isArray(listed)) return undefined;
184
+ const found = new Map<string, Record<string, unknown>>();
197
185
  for (const item of listed) {
198
- const { ns, value } = item as { ns?: unknown; value?: unknown };
199
- const section = asSection(value);
200
- if (typeof ns === "string" && section !== undefined) found.set(ns, section);
186
+ const entry = asSection(item);
187
+ const ns = entry?.ns;
188
+ if (ns !== SETTINGS_NAMESPACE && ns !== LOCALE_NAMESPACE) continue;
189
+ const section = asSection(entry?.value);
190
+ if (section !== undefined) found.set(ns, section);
201
191
  }
202
192
  return found;
203
193
  }
204
194
 
205
- /**
206
- * Read the namespace sections, reusing one read for a short while.
207
- * 读取各命名空间的段,并在很短的时间内复用同一次读取。
208
- *
209
- * The cache exists because prompt assembly asks on every model step, while
210
- * `describe()` walks the whole settings document each time.
211
- * 缓存的存在是因为提示词组装每一步都会问,而 `describe()` 每次都要遍历整份设置文档。
212
- */
213
- function sectionsOf(settings: HostSettings | undefined | null): ReadonlyMap<string, Record<string, unknown>> {
214
- if (!settings || typeof settings.describe !== "function") return NO_SECTIONS;
215
- const now = Date.now();
216
- if (snapshot === undefined || now - snapshot.takenAt >= SETTINGS_TTL_MS) {
217
- snapshot = { takenAt: now, sections: collect(settings) };
218
- }
219
- return snapshot.sections;
220
- }
221
-
222
195
  /**
223
196
  * Adapt the settings service to the `get()`-style handle the core reads from.
224
197
  * 把设置服务适配成核心代码所用的 `get()` 风格句柄。
225
198
  */
226
- function settingsReader(settings: HostSettings | undefined | null): Reader {
199
+ function settingsReader(sections: ReadonlyMap<string, Record<string, unknown>>): Reader {
227
200
  return {
228
- get: (ns?: string) => sectionsOf(settings).get(typeof ns === "string" ? ns : SETTINGS_NAMESPACE)
201
+ get: (ns = SETTINGS_NAMESPACE) => sections.get(ns)
229
202
  };
230
203
  }
231
204
 
@@ -309,6 +282,7 @@ interface HostContext {
309
282
  options?: { prepend?: boolean }
310
283
  ): unknown;
311
284
  on(event: "agent/created", listener: (payload: unknown) => unknown): unknown;
285
+ on(event: "settings/document-updated", listener: (namespace: string) => void): unknown;
312
286
  systemPrompt: {
313
287
  section(section: { name: string; order?: number; text: () => string }): unknown;
314
288
  };
@@ -343,23 +317,16 @@ function prefixPrompt(message: Record<string, unknown>, text: string): Record<st
343
317
  }
344
318
 
345
319
  /**
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
- * 模型读作指令的块开头;没有技能目录时退回最后一条注入消息。
320
+ * Attach the rule to this batch's skill catalogue, or append a plugin-owned message last.
321
+ * 把规则加到当前批次的技能目录;没有目录时在批次末尾追加一条本插件来源的消息。
322
+ *
323
+ * The fallback lands after every other injected message, so the closing rule stays
324
+ * the last thing the model reads even when no catalogue was sent.
325
+ * 回退的那条落在全部注入消息之后,因此没有技能目录时,收尾规则仍是模型最后读到的一句。
350
326
  */
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;
327
+ function withClosingReminder(messages: Array<Record<string, unknown>>, text: string): Array<Record<string, unknown>> {
328
+ const index = messages.findIndex((message) => asSection(message.source)?.kind === "skill-catalog");
329
+ if (index < 0) return [...messages, reminderMessage(text)];
363
330
  const next = [...messages];
364
331
  next[index] = prefixPrompt(messages[index], text);
365
332
  return next;
@@ -374,7 +341,24 @@ export function apply(ctx: HostContext): void {
374
341
  const lastReminderAt = new Map<string, number>();
375
342
 
376
343
  ctx.inject(["systemPrompt", "settings"], (promptCtx) => {
377
- const reader = () => settingsReader(promptCtx.get("settings") as HostSettings | undefined);
344
+ // A settings write emits this event before acknowledgement. The TTL also
345
+ // refreshes reads when a host change produced no notification.
346
+ // 设置写入在应答前发出此事件。TTL 也会刷新未收到通知的变动。
347
+ let snapshot: SettingsSnapshot | undefined;
348
+ promptCtx.on("settings/document-updated", (namespace) => {
349
+ if (namespace === SETTINGS_NAMESPACE || namespace === LOCALE_NAMESPACE) snapshot = undefined;
350
+ });
351
+ const reader = (): Reader => {
352
+ const service = promptCtx.get("settings") as HostSettings | undefined;
353
+ if (!service || typeof service.describe !== "function") return settingsReader(NO_SECTIONS);
354
+ const now = Date.now();
355
+ if (snapshot?.service === service && now >= snapshot.takenAt && now - snapshot.takenAt < SETTINGS_TTL_MS) {
356
+ return settingsReader(snapshot.sections);
357
+ }
358
+ const sections = collect(service);
359
+ snapshot = sections === undefined ? undefined : { service, takenAt: now, sections };
360
+ return settingsReader(sections ?? NO_SECTIONS);
361
+ };
378
362
 
379
363
  // The text thunk is re-evaluated per assembly, so the section always
380
364
  // reflects the newest setting without re-registering anything.
@@ -391,7 +375,7 @@ export function apply(ctx: HostContext): void {
391
375
  */
392
376
  const rewrite = async (payload: PreStepPayload, next: () => Promise<PreStepDecision>) => {
393
377
  const decision = await next();
394
- if (decision.kind !== "enter" || payload.signal.aborted || payload.step !== 1) return decision;
378
+ if (decision.kind !== "enter" || payload.signal.aborted || payload.step !== 1 || decision.messages.length === 0) return decision;
395
379
  const settings = currentSettings(reader());
396
380
  const delay = reminderDelayMs(settings.reminderMinutes);
397
381
  if (delay === REMINDER_OFF) return decision;
@@ -405,7 +389,7 @@ export function apply(ctx: HostContext): void {
405
389
  // reminder off skips this whole listener, so both injections stop together.
406
390
  // 收尾规则与提醒共用一个开关:提醒关闭时本段不执行,两处注入一起停。
407
391
  const tail = settings.extraInjection ? tailText(settings.language) : "";
408
- const messages = tail === "" ? decision.messages : prefixInjected(decision.messages, tail);
392
+ const messages = tail === "" ? decision.messages : withClosingReminder(decision.messages, tail);
409
393
  return { ...decision, messages: [reminderMessage(head), ...messages] };
410
394
  };
411
395