@prur/dsh-chat-service 0.1.17 → 0.3.0

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/events.d.ts CHANGED
@@ -11,6 +11,15 @@
11
11
  import type { Context } from '@deepseek-ai/cordis';
12
12
  /** The three chat events a client may receive on the downlink. */
13
13
  export type ChatDownlinkEvent = 'chat/message' | 'chat/chunk' | 'chat/status';
14
+ /** Cross-plugin cordis event emitted when a chat turn completes (B4 E2E push hook). */
15
+ export interface ChatTurnEndEvent {
16
+ conversationId: string;
17
+ }
18
+ declare module '@deepseek-ai/cordis' {
19
+ interface Events {
20
+ 'chat/turn-end'(evt: ChatTurnEndEvent): void;
21
+ }
22
+ }
14
23
  /**
15
24
  * Downlink frame: the full `server-request` envelope the DSH downlink
16
25
  * transport uses (exact same shape as `/api/events.host` remote-event
package/lib/events.js CHANGED
@@ -15,6 +15,8 @@ import WebSocket, { WebSocketServer } from 'ws';
15
15
  export const CHAT_EVENTS_PATH = '/chat/events';
16
16
  /** Heartbeat interval; keeps proxies from dropping an idle downlink. */
17
17
  const HEARTBEAT_INTERVAL_MS = 15_000;
18
+ /** /chat/events 下行背压水位:客户端 ws `bufferedAmount` 超过该值视为慢性慢消费者,断开让其重连 + 历史基线回填自愈。 */
19
+ const CHAT_DOWNLINK_BACKPRESSURE_BYTES = 16 * 1024 * 1024;
18
20
  /**
19
21
  * In-process event bus. The service publishes; the gateway subscribes and
20
22
  * broadcasts to every connected client. Synchronous, listener failures are
@@ -82,8 +84,16 @@ export class ChatEventsGateway {
82
84
  broadcast(frame) {
83
85
  const payload = JSON.stringify(frame);
84
86
  for (const client of this.clients) {
85
- if (client.readyState === WebSocket.OPEN)
86
- client.send(payload);
87
+ if (client.readyState !== WebSocket.OPEN)
88
+ continue;
89
+ // 慢消费者背压:ws send 缓冲区(bufferedAmount)无界增长会令宿主进程内存滞留到 OOM
90
+ // (手机经中继弱网消费慢)。超过水位即断开该客户端——其重连后经 chat/history 尾页基线
91
+ // + chat/status 快照回填自愈,不因积压把宿主进程拖垮。
92
+ if (client.bufferedAmount > CHAT_DOWNLINK_BACKPRESSURE_BYTES) {
93
+ client.terminate();
94
+ continue;
95
+ }
96
+ client.send(payload);
87
97
  }
88
98
  }
89
99
  handleUpgrade(req, socket, head) {
package/lib/index.d.ts CHANGED
@@ -54,6 +54,8 @@ export declare class ChatService extends TypertRemoteService {
54
54
  private readonly runners;
55
55
  private readonly bus;
56
56
  private readonly gateway;
57
+ /** Cordis context for emitting cross-plugin events (e.g. chat turn completion for push). */
58
+ private _ctx;
57
59
  constructor(ctx: Context, config: ChatServiceConfig);
58
60
  /** Re-run queued sends and settle half-finished conversations on startup. */
59
61
  private recover;
package/lib/index.js CHANGED
@@ -140,8 +140,11 @@ let ChatService = (() => {
140
140
  runners = new Map();
141
141
  bus = new ChatEventBus();
142
142
  gateway;
143
+ /** Cordis context for emitting cross-plugin events (e.g. chat turn completion for push). */
144
+ _ctx;
143
145
  constructor(ctx, config) {
144
146
  super(ctx, 'chat');
147
+ this._ctx = ctx;
145
148
  this.gateway = new ChatEventsGateway(ctx, config.trustedHosts, config.apiToken);
146
149
  this.bus.subscribe(frame => this.gateway.broadcast(frame));
147
150
  // Recover pending queues after a host restart: a half-finished turn can
@@ -564,6 +567,12 @@ let ChatService = (() => {
564
567
  }
565
568
  emitStatus(conversationId, status) {
566
569
  this.bus.publish('chat/status', [conversationId, status]);
570
+ // B4 E2E: on a real turn completion (running→false, not user-aborted), broadcast a
571
+ // cross-plugin cordis event so the push-bridge can emit a wake-up push carrying the
572
+ // conversationId (client routes to the Chat surface).
573
+ if (status.running === false && status.reason !== 'aborted') {
574
+ this._ctx.emit('chat/turn-end', { conversationId });
575
+ }
567
576
  }
568
577
  };
569
578
  })();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@prur/dsh-chat-service",
3
- "version": "0.1.17",
3
+ "version": "0.3.0",
4
4
  "description": "Typert Remote namespace exposing a plain streaming model chat (no agent loop) to DSH Mobile clients",
5
5
  "repository": {
6
6
  "type": "git",
@@ -35,7 +35,7 @@
35
35
  "@deepseek-ai/schemastery": "^3.18.1",
36
36
  "ws": "^8.21.0",
37
37
  "zod": "^4.4.3",
38
- "@prur/dsh-client-connection-mobile": "^0.1.17"
38
+ "@prur/dsh-client-connection-mobile": "^0.3.0"
39
39
  },
40
40
  "peerDependencies": {
41
41
  "@deepseek-ai/cordis": "^4.0.1",