@tencent-ai/cloud-agent-sdk 0.3.3 → 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,
@@ -2244,6 +2246,18 @@ var AcpClient = class {
2244
2246
  await this._client.cancel(sessionId);
2245
2247
  }
2246
2248
  /**
2249
+ * 设置 session 使用的模型。
2250
+ *
2251
+ * @experimental 对应 ACP 协议的 `unstable_setSessionModel`,API 尚未稳定。
2252
+ */
2253
+ async setSessionModel(sessionId, modelId) {
2254
+ if (!this._client) throw new AcpProtocolError("Not connected");
2255
+ return this._client.setSessionModel({
2256
+ sessionId,
2257
+ modelId
2258
+ });
2259
+ }
2260
+ /**
2247
2261
  * 订阅指定 sessionId 的上游 notifications。
2248
2262
  *
2249
2263
  * Listener 收到的是上游 `SessionNotification` 原样(`{ sessionId, update, _meta? }`)。
@@ -2339,6 +2353,7 @@ var AcpClient = class {
2339
2353
  var Session = class {
2340
2354
  /** @internal */
2341
2355
  constructor(id, runtime, restClient) {
2356
+ this._pendingSubscribers = /* @__PURE__ */ new Set();
2342
2357
  this.id = id;
2343
2358
  this.runtimeId = runtime.id;
2344
2359
  this._runtime = runtime;
@@ -2404,6 +2419,7 @@ var Session = class {
2404
2419
  });
2405
2420
  if (opts?.initialize !== false) await client.initialize();
2406
2421
  await client.sessionLoad(this.id);
2422
+ for (const listener of this._pendingSubscribers) client.subscribe(this.id, listener);
2407
2423
  this._acpClient = client;
2408
2424
  }
2409
2425
  /** 断开 ACP 连接。所有订阅器被自动清除。 */
@@ -2484,7 +2500,8 @@ var Session = class {
2484
2500
  * `update` 是上游 11-tag 判别联合,在 switch 分支里类型自动收窄。
2485
2501
  *
2486
2502
  * 订阅独立于 prompt 生命周期——connect 之后注册,直到 unsubscribe 或
2487
- * disconnect 为止。一个 session 支持任意多个订阅者。
2503
+ * disconnect 为止。未 connect 时也可以先注册,后续 connect / reconnect 到
2504
+ * 该 session 后会自动生效。一个 session 支持任意多个订阅者。
2488
2505
  *
2489
2506
  * @returns unsubscribe 函数,调用即退订。
2490
2507
  *
@@ -2505,8 +2522,17 @@ var Session = class {
2505
2522
  * ```
2506
2523
  */
2507
2524
  subscribe(listener) {
2508
- if (!this._acpClient || this._acpClient.state !== "OPEN") throw new ValidationError("Session not connected. Call connect() first.");
2509
- 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
+ };
2510
2536
  }
2511
2537
  /**
2512
2538
  * 取消当前运行的 prompt(如果有)。
@@ -2525,6 +2551,26 @@ var Session = class {
2525
2551
  loggerFor(this._runtime._connectionOpts).warn(`[session ${this.id}] cancel failed: ${err.message}`);
2526
2552
  }
2527
2553
  }
2554
+ /**
2555
+ * 设置 session 使用的模型。
2556
+ *
2557
+ * 必须在 `connect()` 之后调用。
2558
+ *
2559
+ * @param modelId 目标模型 ID,例如 `"gpt-4o"` / `"claude-3-7-sonnet"`
2560
+ *
2561
+ * @experimental 对应 ACP 协议的 `unstable_setSessionModel`,API 尚未稳定。
2562
+ *
2563
+ * @example
2564
+ * ```ts
2565
+ * await session.connect();
2566
+ * await session.setModel('claude-3-7-sonnet');
2567
+ * const r = await session.prompt('hello');
2568
+ * ```
2569
+ */
2570
+ async setModel(modelId) {
2571
+ if (!this._acpClient || this._acpClient.state !== "OPEN") throw new ValidationError("Session not connected. Call connect() first.");
2572
+ await this._acpClient.setSessionModel(this.id, modelId);
2573
+ }
2528
2574
  };
2529
2575
  /**
2530
2576
  * 合并两个 AbortSignal:任一 abort 时返回的 signal 也 abort。