dsh-message-gateway 0.1.25 → 0.1.27

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
@@ -1896,36 +1896,43 @@ var BridgeManager = class {
1896
1896
  }
1897
1897
  /** 轮询注入会话的事件流:chunk → 流式推送;assistant/message → 定稿 + HTTP。 */
1898
1898
  pollPending(key, p, session) {
1899
- const events = session.events;
1900
- for (let i = p.cursor; i < events.length; i += 1) {
1901
- p.cursor = i + 1;
1902
- const event = events[i];
1903
- if (event.type === "assistant/chunk") {
1904
- const chunk = event.data.chunk;
1905
- if (chunk.type === "text-delta") {
1906
- p.buffer += chunk.text;
1907
- this.scheduleStream(p);
1899
+ if (!session) return;
1900
+ const events = typeof session.snapshotEvents === "function" ? session.snapshotEvents() : Array.isArray(session.events) ? session.events : [];
1901
+ if (!Array.isArray(events)) return;
1902
+ try {
1903
+ for (let i = p.cursor; i < events.length; i += 1) {
1904
+ p.cursor = i + 1;
1905
+ const event = events[i];
1906
+ if (!event) continue;
1907
+ if (event.type === "assistant/chunk") {
1908
+ const chunk = event.data?.chunk;
1909
+ if (chunk && chunk.type === "text-delta" && typeof chunk.text === "string") {
1910
+ p.buffer += chunk.text;
1911
+ this.scheduleStream(p);
1912
+ }
1913
+ continue;
1908
1914
  }
1909
- continue;
1910
- }
1911
- if (event.type === "assistant/message") {
1912
- const text = extractText(event.data.message);
1913
- if (text === "" || isToolCallOnly(text)) continue;
1914
- p.buffer = text;
1915
- const streamed = p.pushed;
1916
- console.log("[dsh-message-gateway] assistant done", { key, seq: event.seq, len: p.buffer.length });
1917
- void this.pushStream(p, true);
1918
- void this.deliverHttp(p, streamed);
1919
- this.finishPending(key);
1920
- return;
1921
- }
1922
- if (event.type === "turn/end") {
1923
- if (p.buffer !== "") {
1915
+ if (event.type === "assistant/message") {
1916
+ const text = extractText(event.data?.message);
1917
+ if (text === "" || isToolCallOnly(text)) continue;
1918
+ p.buffer = text;
1919
+ const streamed = p.pushed;
1920
+ console.log("[dsh-message-gateway] assistant done", { key, seq: event.seq, len: p.buffer.length });
1924
1921
  void this.pushStream(p, true);
1922
+ void this.deliverHttp(p, streamed);
1925
1923
  this.finishPending(key);
1926
1924
  return;
1927
1925
  }
1926
+ if (event.type === "turn/end") {
1927
+ if (p.buffer !== "") {
1928
+ void this.pushStream(p, true);
1929
+ this.finishPending(key);
1930
+ return;
1931
+ }
1932
+ }
1928
1933
  }
1934
+ } catch (err) {
1935
+ console.error("[dsh-message-gateway] pollPending error", err);
1929
1936
  }
1930
1937
  }
1931
1938
  /** 流式推送:随轮询节奏(每 400ms 一次)全量更新,避免高频发送。 */
@@ -2161,8 +2168,12 @@ var BridgeManager = class {
2161
2168
  agent.send(message, "next-turn", true);
2162
2169
  console.log("[dsh-message-gateway] sent to agent", { key: id.key, text: sanitizeSecrets(text.slice(0, 60)), baseSeq: p.cursor });
2163
2170
  p.timer = setInterval(() => {
2164
- if (this.pendingMap.get(id.key) !== p) return;
2165
- this.pollPending(id.key, p, session);
2171
+ try {
2172
+ if (this.pendingMap.get(id.key) !== p) return;
2173
+ this.pollPending(id.key, p, session);
2174
+ } catch (pollErr) {
2175
+ console.error("[dsh-message-gateway] timer pollPending caught error", pollErr);
2176
+ }
2166
2177
  }, 400);
2167
2178
  p.fallback = setTimeout(() => {
2168
2179
  if (this.pendingMap.get(id.key) === p) this.finishPending(id.key);
@@ -2206,7 +2217,7 @@ var BridgeManager = class {
2206
2217
  clearInterval(poll);
2207
2218
  return;
2208
2219
  }
2209
- const events = session.events;
2220
+ const events = typeof session.snapshotEvents === "function" ? session.snapshotEvents() : Array.isArray(session.events) ? session.events : [];
2210
2221
  for (let i = cursor; i < events.length; i += 1) {
2211
2222
  const event = events[i];
2212
2223
  if (event.type === "assistant/message") {