@questionbase/deskfree 0.3.0-alpha.29 → 0.3.0-alpha.30

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/dist/index.js CHANGED
@@ -4117,6 +4117,7 @@ var CLOSE_MAX_RETRIES = 3;
4117
4117
  var DeskFreeStreamingSession = class {
4118
4118
  client;
4119
4119
  log;
4120
+ sendViaWs;
4120
4121
  messageId = null;
4121
4122
  currentText = "";
4122
4123
  closed = false;
@@ -4126,9 +4127,10 @@ var DeskFreeStreamingSession = class {
4126
4127
  queue = Promise.resolve();
4127
4128
  /** Full accumulated text (currentText + pendingDelta) */
4128
4129
  fullText = "";
4129
- constructor(client, log) {
4130
+ constructor(client, log, sendViaWs) {
4130
4131
  this.client = client;
4131
4132
  this.log = log;
4133
+ this.sendViaWs = sendViaWs;
4132
4134
  }
4133
4135
  /**
4134
4136
  * Send the first chunk as a new message.
@@ -4228,13 +4230,13 @@ var DeskFreeStreamingSession = class {
4228
4230
  if (!delta || !this.messageId || this.closed) return;
4229
4231
  this.pendingDelta = "";
4230
4232
  const fullContent = this.fullText;
4233
+ const messageId = this.messageId;
4231
4234
  this.queue = this.queue.then(async () => {
4232
4235
  try {
4233
- await this.client.streamChunk({
4234
- messageId: this.messageId,
4235
- delta,
4236
- fullContent
4237
- });
4236
+ const sentViaWs = this.sendViaWs?.(messageId, delta, fullContent);
4237
+ if (!sentViaWs) {
4238
+ await this.client.streamChunk({ messageId, delta, fullContent });
4239
+ }
4238
4240
  this.currentText = fullContent;
4239
4241
  this.lastUpdateTime = Date.now();
4240
4242
  } catch (err) {
@@ -4619,7 +4621,11 @@ async function deliverMessageToAgent(ctx, message, client) {
4619
4621
  if (kind === "block") {
4620
4622
  accumulatedText += text;
4621
4623
  if (!streamingSession) {
4622
- streamingSession = new DeskFreeStreamingSession(client, log);
4624
+ streamingSession = new DeskFreeStreamingSession(
4625
+ client,
4626
+ log,
4627
+ sendWsStreamChunk
4628
+ );
4623
4629
  log.info(
4624
4630
  `Streaming reply to DeskFree (task: ${taskId ?? "none"}): "${accumulatedText.slice(0, 80)}..."`
4625
4631
  );
@@ -4743,6 +4749,17 @@ var BACKOFF_FACTOR = 1.8;
4743
4749
  var HEALTH_LOG_INTERVAL_MS = 30 * 60 * 1e3;
4744
4750
  var MAX_CONSECUTIVE_POLL_FAILURES = 5;
4745
4751
  var deliveredMessageIds = /* @__PURE__ */ new Set();
4752
+ function sendWsStreamChunk(messageId, delta, fullContent) {
4753
+ if (!activeWs || activeWs.readyState !== wrapper_default.OPEN) return false;
4754
+ try {
4755
+ activeWs.send(
4756
+ JSON.stringify({ action: "streamChunk", messageId, delta, fullContent })
4757
+ );
4758
+ return true;
4759
+ } catch {
4760
+ return false;
4761
+ }
4762
+ }
4746
4763
  var healthState = /* @__PURE__ */ new Map();
4747
4764
  function initializeHealth(accountId) {
4748
4765
  if (!healthState.has(accountId)) {
@@ -5100,7 +5117,7 @@ async function runWebSocketConnection(opts) {
5100
5117
  ws.on("close", (code, reason) => {
5101
5118
  cleanup();
5102
5119
  isConnected = false;
5103
- activeWs = null;
5120
+ if (activeWs === ws) activeWs = null;
5104
5121
  ctx.setStatus({ running: false, lastStopAt: Date.now() });
5105
5122
  if (code === 1e3) {
5106
5123
  log.info(`WebSocket closed normally: ${code} ${reason.toString()}`);
@@ -5150,6 +5167,7 @@ async function runWebSocketConnection(opts) {
5150
5167
  ws.on("error", (err) => {
5151
5168
  cleanup();
5152
5169
  isConnected = false;
5170
+ if (activeWs === ws) activeWs = null;
5153
5171
  const errorMessage = err instanceof Error ? err.message : String(err);
5154
5172
  log.error(`WebSocket error: ${errorMessage}`);
5155
5173
  reportError("error", `WebSocket error: ${errorMessage}`, {