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

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
@@ -1,4 +1,51 @@
1
1
 
2
+ //#region src/v1/agent.ts
3
+ var Agent = class {
4
+ /** @internal */
5
+ constructor(restClient, info) {
6
+ this.versions = {
7
+ list: async (opts) => {
8
+ const params = {};
9
+ if (opts?.page !== void 0) params.page = String(opts.page);
10
+ if (opts?.pageSize !== void 0) params.pageSize = String(opts.pageSize);
11
+ return this._restClient.get(`/agents/${this.id}/versions`, params, opts);
12
+ },
13
+ get: async (versionId, opts) => this._restClient.get(`/agents/${this.id}/versions/${versionId}`, void 0, opts),
14
+ publish: async (req, opts) => this._restClient.post(`/agents/${this.id}/versions`, req, opts),
15
+ delete: async (versionId, opts) => {
16
+ await this._restClient.delete(`/agents/${this.id}/versions/${versionId}`, opts);
17
+ },
18
+ setCurrent: async (versionId, opts) => {
19
+ await this._restClient.put(`/agents/${this.id}/versions/current`, { versionId }, opts);
20
+ }
21
+ };
22
+ this.draft = {
23
+ save: async (req, opts) => this._restClient.put(`/agents/${this.id}/draft`, req, opts),
24
+ get: async (opts) => this._restClient.get(`/agents/${this.id}/draft`, void 0, opts),
25
+ publish: async (description, opts) => this._restClient.post(`/agents/${this.id}/draft/publish`, { description }, opts)
26
+ };
27
+ this.id = info.id;
28
+ this._info = info;
29
+ this._restClient = restClient;
30
+ }
31
+ get agentInfo() {
32
+ return this._info;
33
+ }
34
+ get agentName() {
35
+ return this._info.agentName;
36
+ }
37
+ get description() {
38
+ return this._info.description;
39
+ }
40
+ get model() {
41
+ return this._info.model;
42
+ }
43
+ get enabled() {
44
+ return this._info.enabled;
45
+ }
46
+ };
47
+
48
+ //#endregion
2
49
  //#region src/v1/internal/log.ts
3
50
  /** 级别数值:越大越详细;方法级别 > 当前级别时,该方法变成 noop。 */
4
51
  const LEVEL_NUMBER = {
@@ -271,6 +318,11 @@ var RestClient = class {
271
318
  const url = this.buildUrl(path);
272
319
  return this.request("POST", url, body, opts);
273
320
  }
321
+ /** PUT 请求。 */
322
+ async put(path, body, opts) {
323
+ const url = this.buildUrl(path);
324
+ return this.request("PUT", url, body, opts);
325
+ }
274
326
  /** PATCH 请求。 */
275
327
  async patch(path, body, opts) {
276
328
  const url = this.buildUrl(path);
@@ -517,13 +569,6 @@ function truncate(body, maxChars = 500) {
517
569
  }
518
570
  }
519
571
 
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
572
  //#endregion
528
573
  //#region src/v1/acp/transport.ts
529
574
  /**
@@ -531,11 +576,11 @@ async function loadAcpSdk() {
531
576
  * 返回的对象实现 Stream 接口(readable + writable),可直接传给 ClientSideConnection。
532
577
  */
533
578
  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;
579
+ 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
580
  const { enabled: reconnectEnabled = true, initialDelay = 1e3, maxDelay = 3e4, maxRetries = Infinity, jitter: jitterEnabled = true } = reconnect;
536
581
  const { highWaterMark = 100, lowWaterMark = 50, pauseTimeout = 5e3 } = backpressure;
537
582
  let connectionId;
538
- let lastEventId;
583
+ let lastEventId = initialLastEventId;
539
584
  let reconnectAttempts = 0;
540
585
  let closed = false;
541
586
  let isClosing = false;
@@ -970,6 +1015,9 @@ const KNOWN_EXTENSIONS = [
970
1015
  //#endregion
971
1016
  //#region src/v1/acp/vendor/constants.ts
972
1017
  /**
1018
+ * Protocol constants for Streamable HTTP ACP Client
1019
+ */
1020
+ /**
973
1021
  * Default timeout for initialize operation
974
1022
  */
975
1023
  const DEFAULT_INITIALIZE_TIMEOUT = 3e4;
@@ -1549,6 +1597,13 @@ var QuestionManager = class {
1549
1597
  }
1550
1598
  };
1551
1599
 
1600
+ //#endregion
1601
+ //#region src/v1/acp/vendor/sdk.ts
1602
+ const PROTOCOL_VERSION = 1;
1603
+ async function loadAcpSdk() {
1604
+ return import("@agentclientprotocol/sdk");
1605
+ }
1606
+
1552
1607
  //#endregion
1553
1608
  //#region src/v1/acp/vendor/client.ts
1554
1609
  /**
@@ -1703,6 +1758,7 @@ var StreamableHttpClient = class {
1703
1758
  endpoint: this.options.endpoint,
1704
1759
  authToken: this.options.authToken,
1705
1760
  headers: this.options.headers,
1761
+ lastEventId: this.options.lastEventId,
1706
1762
  reconnect: this.options.reconnect,
1707
1763
  fetch: this.options.fetch,
1708
1764
  heartbeatTimeout: this.options.heartbeatTimeout,
@@ -1867,7 +1923,7 @@ var StreamableHttpClient = class {
1867
1923
  async setSessionModel(params) {
1868
1924
  this.ensureInitialized("setSessionModel");
1869
1925
  this.options.logger?.debug(`Setting session model: ${params.sessionId} -> ${params.modelId}`);
1870
- return this.connection.unstable_setSessionModel(params);
1926
+ return await this.connection.extMethod("session/set_model", params);
1871
1927
  }
1872
1928
  /**
1873
1929
  * Send a prompt to the agent
@@ -2134,6 +2190,7 @@ var AcpClient = class {
2134
2190
  endpoint: acpUrl,
2135
2191
  authToken: token,
2136
2192
  headers: opts?.headers,
2193
+ lastEventId: opts?.lastEventId,
2137
2194
  reconnect: {
2138
2195
  enabled: true,
2139
2196
  initialDelay: opts?.reconnectBackoffMs ?? 1e3,
@@ -2351,6 +2408,7 @@ var AcpClient = class {
2351
2408
  var Session = class {
2352
2409
  /** @internal */
2353
2410
  constructor(id, runtime, restClient) {
2411
+ this._pendingSubscribers = /* @__PURE__ */ new Set();
2354
2412
  this.id = id;
2355
2413
  this.runtimeId = runtime.id;
2356
2414
  this._runtime = runtime;
@@ -2416,6 +2474,7 @@ var Session = class {
2416
2474
  });
2417
2475
  if (opts?.initialize !== false) await client.initialize();
2418
2476
  await client.sessionLoad(this.id);
2477
+ for (const listener of this._pendingSubscribers) client.subscribe(this.id, listener);
2419
2478
  this._acpClient = client;
2420
2479
  }
2421
2480
  /** 断开 ACP 连接。所有订阅器被自动清除。 */
@@ -2496,7 +2555,8 @@ var Session = class {
2496
2555
  * `update` 是上游 11-tag 判别联合,在 switch 分支里类型自动收窄。
2497
2556
  *
2498
2557
  * 订阅独立于 prompt 生命周期——connect 之后注册,直到 unsubscribe 或
2499
- * disconnect 为止。一个 session 支持任意多个订阅者。
2558
+ * disconnect 为止。未 connect 时也可以先注册,后续 connect / reconnect 到
2559
+ * 该 session 后会自动生效。一个 session 支持任意多个订阅者。
2500
2560
  *
2501
2561
  * @returns unsubscribe 函数,调用即退订。
2502
2562
  *
@@ -2517,8 +2577,17 @@ var Session = class {
2517
2577
  * ```
2518
2578
  */
2519
2579
  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);
2580
+ this._pendingSubscribers.add(listener);
2581
+ if (this._acpClient && this._acpClient.state === "OPEN") {
2582
+ const unsub = this._acpClient.subscribe(this.id, listener);
2583
+ return () => {
2584
+ unsub();
2585
+ this._pendingSubscribers.delete(listener);
2586
+ };
2587
+ }
2588
+ return () => {
2589
+ this._pendingSubscribers.delete(listener);
2590
+ };
2522
2591
  }
2523
2592
  /**
2524
2593
  * 取消当前运行的 prompt(如果有)。
@@ -2582,17 +2651,27 @@ var Runtime = class {
2582
2651
  constructor(restClient, connectionOpts, info) {
2583
2652
  this.sessions = {
2584
2653
  create: async (opts) => {
2585
- const body = {
2586
- sessionId: opts.sessionId,
2587
- sessionName: opts.sessionName,
2588
- agentManifest: opts.agentManifest
2589
- };
2590
- return new Session((await this._restClient.post(`/runtimes/${this.id}/sessions`, body, {
2654
+ const reqOpts = {
2591
2655
  timeoutMs: opts.timeoutMs,
2592
2656
  headers: opts.headers,
2593
2657
  signal: opts.signal,
2594
2658
  requestId: opts.requestId
2595
- })).sessionId, this, this._restClient);
2659
+ };
2660
+ let info;
2661
+ if (opts.agentId) info = await this._restClient.post("/agents/sessions", {
2662
+ agentId: opts.agentId,
2663
+ runtimeId: this.id,
2664
+ sessionName: opts.sessionName
2665
+ }, reqOpts);
2666
+ else {
2667
+ const body = {
2668
+ sessionId: opts.sessionId,
2669
+ sessionName: opts.sessionName,
2670
+ agentManifest: opts.agentManifest
2671
+ };
2672
+ info = await this._restClient.post(`/runtimes/${this.id}/sessions`, body, reqOpts);
2673
+ }
2674
+ return new Session(info.sessionId, this, this._restClient);
2596
2675
  },
2597
2676
  get: async (sessionId, opts) => {
2598
2677
  await this._restClient.get(`/runtimes/${this.id}/sessions/${sessionId}`, void 0, opts);
@@ -2663,6 +2742,26 @@ const CODEBUDDY_API_KEY = "CODEBUDDY_API_KEY";
2663
2742
  var CloudAgentClient = class CloudAgentClient {
2664
2743
  /** 构造(同步,不发网络请求) */
2665
2744
  constructor(opts = {}) {
2745
+ this.agents = {
2746
+ create: async (req, opts) => {
2747
+ const info = await this._restClient.post("/agents", req, opts);
2748
+ return new Agent(this._restClient, info);
2749
+ },
2750
+ get: async (agentId, opts) => {
2751
+ const info = await this._restClient.get(`/agents/${agentId}`, void 0, opts);
2752
+ return new Agent(this._restClient, info);
2753
+ },
2754
+ list: async (opts) => {
2755
+ const params = {};
2756
+ if (opts?.page !== void 0) params.page = String(opts.page);
2757
+ if (opts?.pageSize !== void 0) params.pageSize = String(opts.pageSize);
2758
+ if (opts?.keyword) params.keyword = opts.keyword;
2759
+ return this._restClient.get("/agents", params, opts);
2760
+ },
2761
+ delete: async (agentId, opts) => {
2762
+ await this._restClient.delete(`/agents/${agentId}`, opts);
2763
+ }
2764
+ };
2666
2765
  this.runtimes = {
2667
2766
  create: async (opts) => {
2668
2767
  const agentManifest = withInjectedApiKeySecret(opts.agentManifest, this.opts.apiKey, this.opts);
@@ -2861,6 +2960,7 @@ var ManifestBuilder = class {
2861
2960
 
2862
2961
  //#endregion
2863
2962
  exports.AcpProtocolError = AcpProtocolError;
2963
+ exports.Agent = Agent;
2864
2964
  exports.AuthError = AuthError;
2865
2965
  exports.CloudAgentClient = CloudAgentClient;
2866
2966
  exports.CloudAgentError = CloudAgentError;