@lixiangzhong/dsh-suggestion 0.2.2 → 0.2.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/route.js CHANGED
@@ -153,7 +153,7 @@ async function handleSuggest(ctx, service, request, response) {
153
153
  if (response.writableEnded !== true) controller.abort(new Error('client closed the suggestion request'));
154
154
  });
155
155
 
156
- const text = await service.suggest({ ...query, signal: controller.signal });
156
+ const text = await service.suggest({ ...query, signal: controller.signal, source: 'request' });
157
157
  if (response?.writableEnded === true) return;
158
158
  sendJson(response, HTTP_OK, { text: typeof text === 'string' ? text : null });
159
159
  } catch {
package/lib/service.js CHANGED
@@ -121,6 +121,21 @@ export function createSuggestionService(deps) {
121
121
  const backgroundTimers = new Map();
122
122
  /** 后台兜底不知道界面语言,沿用最近一次客户端请求报上来的语言。 */
123
123
  let lastLocale = '';
124
+ /**
125
+ * 本服务实例身份:用来区分「同一轮为什么又生成了一次」是缓存丢了、
126
+ * 还是请求落到了另一个仍然存活的旧实例上。
127
+ */
128
+ const instance = Math.random().toString(36).slice(2, 8);
129
+
130
+ /**
131
+ * 写一条 Host 侧留痕;自动带上服务实例 id,便于区分「缓存丢了」与「请求落到旧实例」。
132
+ * @param {string} event - 事件名。
133
+ * @param {object} extra - 附带字段。
134
+ * @returns {void}
135
+ */
136
+ function hostTrace(event, extra) {
137
+ traceEvent({ scope: 'host', event, instance, ...extra });
138
+ }
124
139
  const backgroundDelayMs = Number.isFinite(deps.backgroundDelayMs)
125
140
  ? Math.max(0, Number(deps.backgroundDelayMs))
126
141
  : BACKGROUND_DELAY_MS;
@@ -251,9 +266,8 @@ export function createSuggestionService(deps) {
251
266
  * @returns {void}
252
267
  */
253
268
  const trace = (outcome, extra = {}) => {
254
- traceEvent({
255
- scope: 'host',
256
- event: 'suggest',
269
+ hostTrace('suggest', {
270
+ source: request.source ?? 'request',
257
271
  sessionId: request.sessionId,
258
272
  locale: request.locale,
259
273
  outcome,
@@ -298,15 +312,16 @@ export function createSuggestionService(deps) {
298
312
  const cacheKey = `${request.sessionId}#${turnSeq}#${boundary.reasonKind}`;
299
313
  const cached = cache.get(cacheKey);
300
314
  if (cached !== undefined && Date.now() - cached.at <= CACHE_TTL_MS) {
301
- trace('cached', { text: cached.text });
315
+ trace('cached', { cacheKey, text: cached.text });
302
316
  return cached.text;
303
317
  }
304
318
 
305
319
  const running = inflight.get(cacheKey);
306
320
  if (running !== undefined) {
307
- trace('inflight');
321
+ trace('inflight', { cacheKey });
308
322
  return running;
309
323
  }
324
+ trace('cache-miss', { cacheKey, cacheSize: cache.size });
310
325
 
311
326
  // 先查缓存再看新旧:已经生成过的轮次,切回多久都能直接给。
312
327
  const endedAt = events[boundary.endIndex]?.time;
@@ -396,8 +411,8 @@ export function createSuggestionService(deps) {
396
411
  const timer = setTimeout(() => {
397
412
  backgroundTimers.delete(sessionId);
398
413
  const locale = lastLocale !== '' ? lastLocale : 'en';
399
- traceEvent({ scope: 'host', event: 'background.attempt', sessionId, locale });
400
- void suggest({ sessionId, locale }).catch(() => null);
414
+ hostTrace('background.attempt', { sessionId, locale });
415
+ void suggest({ sessionId, locale, source: 'background' }).catch(() => null);
401
416
  }, backgroundDelayMs);
402
417
  // 宿主关闭时不要因为一个待触发的兜底让进程多挂 2 秒。
403
418
  timer.unref?.();
@@ -429,6 +444,8 @@ export function createSuggestionService(deps) {
429
444
  if (typeof locale === 'string' && locale !== '') lastLocale = locale;
430
445
  }
431
446
 
447
+ hostTrace('service.created', { cacheSize: 0 });
448
+
432
449
  return {
433
450
  suggest,
434
451
  scheduleBackground,
@@ -436,6 +453,7 @@ export function createSuggestionService(deps) {
436
453
  noteLocale,
437
454
  trace: traceEvent,
438
455
  dispose() {
456
+ hostTrace('dispose', { cacheSize: cache.size });
439
457
  for (const timer of backgroundTimers.values()) clearTimeout(timer);
440
458
  backgroundTimers.clear();
441
459
  cache.clear();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lixiangzhong/dsh-suggestion",
3
- "version": "0.2.2",
3
+ "version": "0.2.3",
4
4
  "description": "Writes a model-generated next-step suggestion into the DSH composer placeholder after each turn; Tab fills it into the draft.",
5
5
  "author": "lixiangzhong",
6
6
  "repository": {