dsh-agent-sdk-bridge 1.1.2 → 1.1.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/index.js CHANGED
@@ -4032,6 +4032,16 @@ var CodeBuddySession = class {
4032
4032
  callNonce = randomUUID().slice(0, 8);
4033
4033
  lastUsage;
4034
4034
  resultUsage;
4035
+ /**
4036
+ * Ids of assistant messages whose streaming events this session consumed.
4037
+ *
4038
+ * Kept across turns, not reset with the turn: the echo of a tool round is
4039
+ * delivered on the next stream(), so the previous turn's id is exactly the
4040
+ * one that has to be recognised. Only the last few ids matter, and bounding
4041
+ * the list keeps a long-lived session from growing without limit.
4042
+ */
4043
+ streamedMessageIds = /* @__PURE__ */ new Set();
4044
+ streamedMessageOrder = [];
4035
4045
  output = new TurnOutputAccounting();
4036
4046
  inputChars = 0;
4037
4047
  toolResultTimeoutMs;
@@ -4244,6 +4254,7 @@ var CodeBuddySession = class {
4244
4254
  if (event.type === "message_start") {
4245
4255
  const usage = event.message.usage;
4246
4256
  if (usage !== void 0) this.lastUsage = usage;
4257
+ this.rememberStreamedMessage(event.message.id);
4247
4258
  return;
4248
4259
  }
4249
4260
  if (event.type === "content_block_start") {
@@ -4358,6 +4369,8 @@ var CodeBuddySession = class {
4358
4369
  return;
4359
4370
  }
4360
4371
  if (message.type === "assistant") {
4372
+ const echoOf = message.message.id;
4373
+ if (typeof echoOf === "string" && this.streamedMessageIds.has(echoOf)) return;
4361
4374
  if (this.textBlock === void 0 && this.tools.length === 0) {
4362
4375
  for (const block of message.message.content) if (block.type === "tool_use") {
4363
4376
  this.countOutput(block.name);
@@ -4424,6 +4437,16 @@ var CodeBuddySession = class {
4424
4437
  this.endTurn({ kind: "stop" });
4425
4438
  }
4426
4439
  }
4440
+ /** Record that one assistant message reached the host as streaming events. */
4441
+ rememberStreamedMessage(id) {
4442
+ if (typeof id !== "string" || id.length === 0 || this.streamedMessageIds.has(id)) return;
4443
+ this.streamedMessageIds.add(id);
4444
+ this.streamedMessageOrder.push(id);
4445
+ if (this.streamedMessageOrder.length > 32) {
4446
+ const oldest = this.streamedMessageOrder.shift();
4447
+ if (oldest !== void 0) this.streamedMessageIds.delete(oldest);
4448
+ }
4449
+ }
4427
4450
  resetTurnState() {
4428
4451
  this.blockIndex = 0;
4429
4452
  this.textBlock = void 0;
@@ -209,6 +209,16 @@ export declare class CodeBuddySession {
209
209
  private readonly callNonce;
210
210
  private lastUsage;
211
211
  private resultUsage;
212
+ /**
213
+ * Ids of assistant messages whose streaming events this session consumed.
214
+ *
215
+ * Kept across turns, not reset with the turn: the echo of a tool round is
216
+ * delivered on the next stream(), so the previous turn's id is exactly the
217
+ * one that has to be recognised. Only the last few ids matter, and bounding
218
+ * the list keeps a long-lived session from growing without limit.
219
+ */
220
+ private readonly streamedMessageIds;
221
+ private readonly streamedMessageOrder;
212
222
  private readonly output;
213
223
  private inputChars;
214
224
  private readonly toolResultTimeoutMs;
@@ -263,6 +273,8 @@ export declare class CodeBuddySession {
263
273
  private canUseTool;
264
274
  private consume;
265
275
  private handle;
276
+ /** Record that one assistant message reached the host as streaming events. */
277
+ private rememberStreamedMessage;
266
278
  private resetTurnState;
267
279
  private emit;
268
280
  private countOutput;
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "dsh-agent-sdk-bridge",
3
3
  "displayName": "Agent SDK Bridge",
4
4
  "description": "DeepSeek Harness LLM adapter bridge for Qoder CN, Qoder International, Tencent CodeBuddy and MiniMax Code subscriptions. It reuses each provider's own SDK or official ACP server and CLI login, shows available quota, and exposes per-provider settings pages with independent international and mainland login regions.",
5
- "version": "1.1.2",
5
+ "version": "1.1.3",
6
6
  "type": "module",
7
7
  "license": "MIT",
8
8
  "author": "k1-h2",
package/src/codebuddy.ts CHANGED
@@ -851,6 +851,16 @@ export class CodeBuddySession {
851
851
  private readonly callNonce = randomUUID().slice(0, 8)
852
852
  private lastUsage: TurnUsage | undefined
853
853
  private resultUsage: TurnUsage | undefined
854
+ /**
855
+ * Ids of assistant messages whose streaming events this session consumed.
856
+ *
857
+ * Kept across turns, not reset with the turn: the echo of a tool round is
858
+ * delivered on the next stream(), so the previous turn's id is exactly the
859
+ * one that has to be recognised. Only the last few ids matter, and bounding
860
+ * the list keeps a long-lived session from growing without limit.
861
+ */
862
+ private readonly streamedMessageIds = new Set<string>()
863
+ private readonly streamedMessageOrder: string[] = []
854
864
  private readonly output = new TurnOutputAccounting()
855
865
  private inputChars = 0
856
866
  private readonly toolResultTimeoutMs: number
@@ -1057,6 +1067,12 @@ export class CodeBuddySession {
1057
1067
  if (event.type === 'message_start') {
1058
1068
  const usage = (event.message as { usage?: TurnUsage }).usage
1059
1069
  if (usage !== undefined) this.lastUsage = usage
1070
+ // Remember which message this stream belongs to. A tool round ends the
1071
+ // host turn at `message_stop` (waiting for `result` would deadlock:
1072
+ // the CLI only finishes after the host returns the tool result), so
1073
+ // the CLI's `assistant` echo of this message is still buffered and is
1074
+ // delivered on the NEXT stream() — where it must not be re-emitted.
1075
+ this.rememberStreamedMessage((event.message as { id?: unknown }).id)
1060
1076
  return
1061
1077
  }
1062
1078
  if (event.type === 'content_block_start') {
@@ -1132,6 +1148,12 @@ export class CodeBuddySession {
1132
1148
  return
1133
1149
  }
1134
1150
  if (message.type === 'assistant') {
1151
+ // The CLI sends the assembled message after its streaming events. When
1152
+ // those events already reached the host, this is an echo, not new
1153
+ // content; a replayed echo would print the same reply a second time
1154
+ // (and count its tokens twice).
1155
+ const echoOf = (message.message as { id?: unknown }).id
1156
+ if (typeof echoOf === 'string' && this.streamedMessageIds.has(echoOf)) return
1135
1157
  if (this.textBlock === undefined && this.tools.length === 0) {
1136
1158
  for (const block of message.message.content) {
1137
1159
  if (block.type === 'tool_use') {
@@ -1172,6 +1194,17 @@ export class CodeBuddySession {
1172
1194
  }
1173
1195
  }
1174
1196
 
1197
+ /** Record that one assistant message reached the host as streaming events. */
1198
+ private rememberStreamedMessage(id: unknown): void {
1199
+ if (typeof id !== 'string' || id.length === 0 || this.streamedMessageIds.has(id)) return
1200
+ this.streamedMessageIds.add(id)
1201
+ this.streamedMessageOrder.push(id)
1202
+ if (this.streamedMessageOrder.length > 32) {
1203
+ const oldest = this.streamedMessageOrder.shift()
1204
+ if (oldest !== undefined) this.streamedMessageIds.delete(oldest)
1205
+ }
1206
+ }
1207
+
1175
1208
  private resetTurnState(): void {
1176
1209
  this.blockIndex = 0
1177
1210
  this.textBlock = undefined