@tencent-ai/cloud-agent-sdk 0.3.6 → 0.3.7

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.cjs CHANGED
@@ -517,13 +517,6 @@ function truncate(body, maxChars = 500) {
517
517
  }
518
518
  }
519
519
 
520
- //#endregion
521
- //#region src/v1/acp/vendor/sdk.ts
522
- const PROTOCOL_VERSION = 1;
523
- async function loadAcpSdk() {
524
- return import("@agentclientprotocol/sdk");
525
- }
526
-
527
520
  //#endregion
528
521
  //#region src/v1/acp/transport.ts
529
522
  /**
@@ -531,11 +524,11 @@ async function loadAcpSdk() {
531
524
  * 返回的对象实现 Stream 接口(readable + writable),可直接传给 ClientSideConnection。
532
525
  */
533
526
  function createStreamableHttpTransport(options) {
534
- const { endpoint, authToken, headers: customHeaders = {}, reconnect = {}, signal: externalSignal, fetch: customFetch = globalThis.fetch, onConnect, onDisconnect, onError, heartbeatTimeout = 6e4, connectionTimeout = 3e4, postTimeout = 3e4, backpressure = {} } = options;
527
+ const { endpoint, authToken, headers: customHeaders = {}, lastEventId: initialLastEventId, reconnect = {}, signal: externalSignal, fetch: customFetch = globalThis.fetch, onConnect, onDisconnect, onError, heartbeatTimeout = 6e4, connectionTimeout = 3e4, postTimeout = 3e4, backpressure = {} } = options;
535
528
  const { enabled: reconnectEnabled = true, initialDelay = 1e3, maxDelay = 3e4, maxRetries = Infinity, jitter: jitterEnabled = true } = reconnect;
536
529
  const { highWaterMark = 100, lowWaterMark = 50, pauseTimeout = 5e3 } = backpressure;
537
530
  let connectionId;
538
- let lastEventId;
531
+ let lastEventId = initialLastEventId;
539
532
  let reconnectAttempts = 0;
540
533
  let closed = false;
541
534
  let isClosing = false;
@@ -1549,6 +1542,13 @@ var QuestionManager = class {
1549
1542
  }
1550
1543
  };
1551
1544
 
1545
+ //#endregion
1546
+ //#region src/v1/acp/vendor/sdk.ts
1547
+ const PROTOCOL_VERSION = 1;
1548
+ async function loadAcpSdk() {
1549
+ return import("@agentclientprotocol/sdk");
1550
+ }
1551
+
1552
1552
  //#endregion
1553
1553
  //#region src/v1/acp/vendor/client.ts
1554
1554
  /**
@@ -1703,6 +1703,7 @@ var StreamableHttpClient = class {
1703
1703
  endpoint: this.options.endpoint,
1704
1704
  authToken: this.options.authToken,
1705
1705
  headers: this.options.headers,
1706
+ lastEventId: this.options.lastEventId,
1706
1707
  reconnect: this.options.reconnect,
1707
1708
  fetch: this.options.fetch,
1708
1709
  heartbeatTimeout: this.options.heartbeatTimeout,
@@ -2134,6 +2135,7 @@ var AcpClient = class {
2134
2135
  endpoint: acpUrl,
2135
2136
  authToken: token,
2136
2137
  headers: opts?.headers,
2138
+ lastEventId: opts?.lastEventId,
2137
2139
  reconnect: {
2138
2140
  enabled: true,
2139
2141
  initialDelay: opts?.reconnectBackoffMs ?? 1e3,
@@ -2351,6 +2353,7 @@ var AcpClient = class {
2351
2353
  var Session = class {
2352
2354
  /** @internal */
2353
2355
  constructor(id, runtime, restClient) {
2356
+ this._pendingSubscribers = /* @__PURE__ */ new Set();
2354
2357
  this.id = id;
2355
2358
  this.runtimeId = runtime.id;
2356
2359
  this._runtime = runtime;
@@ -2416,6 +2419,7 @@ var Session = class {
2416
2419
  });
2417
2420
  if (opts?.initialize !== false) await client.initialize();
2418
2421
  await client.sessionLoad(this.id);
2422
+ for (const listener of this._pendingSubscribers) client.subscribe(this.id, listener);
2419
2423
  this._acpClient = client;
2420
2424
  }
2421
2425
  /** 断开 ACP 连接。所有订阅器被自动清除。 */
@@ -2496,7 +2500,8 @@ var Session = class {
2496
2500
  * `update` 是上游 11-tag 判别联合,在 switch 分支里类型自动收窄。
2497
2501
  *
2498
2502
  * 订阅独立于 prompt 生命周期——connect 之后注册,直到 unsubscribe 或
2499
- * disconnect 为止。一个 session 支持任意多个订阅者。
2503
+ * disconnect 为止。未 connect 时也可以先注册,后续 connect / reconnect 到
2504
+ * 该 session 后会自动生效。一个 session 支持任意多个订阅者。
2500
2505
  *
2501
2506
  * @returns unsubscribe 函数,调用即退订。
2502
2507
  *
@@ -2517,8 +2522,17 @@ var Session = class {
2517
2522
  * ```
2518
2523
  */
2519
2524
  subscribe(listener) {
2520
- if (!this._acpClient || this._acpClient.state !== "OPEN") throw new ValidationError("Session not connected. Call connect() first.");
2521
- return this._acpClient.subscribe(this.id, listener);
2525
+ this._pendingSubscribers.add(listener);
2526
+ if (this._acpClient && this._acpClient.state === "OPEN") {
2527
+ const unsub = this._acpClient.subscribe(this.id, listener);
2528
+ return () => {
2529
+ unsub();
2530
+ this._pendingSubscribers.delete(listener);
2531
+ };
2532
+ }
2533
+ return () => {
2534
+ this._pendingSubscribers.delete(listener);
2535
+ };
2522
2536
  }
2523
2537
  /**
2524
2538
  * 取消当前运行的 prompt(如果有)。