agora-agents 2.12.0 → 3.0.0-alpha.2

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.
Files changed (96) hide show
  1. package/README.md +38 -3
  2. package/dist/cjs/AgoraPoolClient.d.ts +15 -27
  3. package/dist/cjs/AgoraPoolClient.js +75 -54
  4. package/dist/cjs/BaseClient.js +3 -2
  5. package/dist/cjs/agentkit/Agent.d.ts +14 -1
  6. package/dist/cjs/agentkit/Agent.js +126 -9
  7. package/dist/cjs/agentkit/AgentSession.d.ts +11 -2
  8. package/dist/cjs/agentkit/AgentSession.js +134 -22
  9. package/dist/cjs/agentkit/index.d.ts +4 -1
  10. package/dist/cjs/agentkit/index.js +12 -2
  11. package/dist/cjs/agentkit/session-backend.d.ts +25 -0
  12. package/dist/cjs/agentkit/session-backend.js +197 -0
  13. package/dist/cjs/agentkit/types.d.ts +27 -4
  14. package/dist/cjs/agentkit/v3.d.ts +70 -0
  15. package/dist/cjs/agentkit/v3.js +700 -0
  16. package/dist/cjs/agentkit/vendors/avatar.d.ts +24 -12
  17. package/dist/cjs/agentkit/vendors/avatar.js +48 -3
  18. package/dist/cjs/agentkit/vendors/base.d.ts +8 -0
  19. package/dist/cjs/agentkit/vendors/base.js +33 -7
  20. package/dist/cjs/agentkit/vendors/llm.d.ts +7 -7
  21. package/dist/cjs/agentkit/vendors/llm.js +26 -22
  22. package/dist/cjs/agentkit/vendors/tts.d.ts +20 -0
  23. package/dist/cjs/agentkit/vendors/tts.js +5 -2
  24. package/dist/cjs/core/publicHeaders.d.ts +2 -0
  25. package/dist/cjs/core/publicHeaders.js +8 -0
  26. package/dist/cjs/index.d.ts +5 -0
  27. package/dist/cjs/index.js +14 -1
  28. package/dist/cjs/v3/campaign-schema.d.ts +1 -0
  29. package/dist/cjs/v3/campaign-schema.js +18 -0
  30. package/dist/cjs/v3/campaigns.d.ts +50 -0
  31. package/dist/cjs/v3/campaigns.js +81 -0
  32. package/dist/cjs/v3/contract.d.ts +1 -0
  33. package/dist/cjs/v3/contract.js +9115 -0
  34. package/dist/cjs/v3/index.d.ts +15 -0
  35. package/dist/cjs/v3/index.js +56 -0
  36. package/dist/cjs/v3/resources.d.ts +121 -0
  37. package/dist/cjs/v3/resources.js +186 -0
  38. package/dist/cjs/v3/session-lifecycle.d.ts +51 -0
  39. package/dist/cjs/v3/session-lifecycle.js +127 -0
  40. package/dist/cjs/v3/session-schema.d.ts +1 -0
  41. package/dist/cjs/v3/session-schema.js +18 -0
  42. package/dist/cjs/v3/transport.d.ts +36 -0
  43. package/dist/cjs/v3/transport.js +161 -0
  44. package/dist/cjs/v3/types.d.ts +1004 -0
  45. package/dist/cjs/v3/types.js +2 -0
  46. package/dist/cjs/version.d.ts +1 -1
  47. package/dist/cjs/version.js +1 -1
  48. package/dist/esm/AgoraPoolClient.d.mts +15 -27
  49. package/dist/esm/AgoraPoolClient.mjs +69 -48
  50. package/dist/esm/BaseClient.mjs +3 -2
  51. package/dist/esm/agentkit/Agent.d.mts +14 -1
  52. package/dist/esm/agentkit/Agent.mjs +126 -9
  53. package/dist/esm/agentkit/AgentSession.d.mts +11 -2
  54. package/dist/esm/agentkit/AgentSession.mjs +134 -22
  55. package/dist/esm/agentkit/index.d.mts +4 -1
  56. package/dist/esm/agentkit/index.mjs +2 -0
  57. package/dist/esm/agentkit/session-backend.d.mts +25 -0
  58. package/dist/esm/agentkit/session-backend.mjs +191 -0
  59. package/dist/esm/agentkit/types.d.mts +27 -4
  60. package/dist/esm/agentkit/v3.d.mts +70 -0
  61. package/dist/esm/agentkit/v3.mjs +687 -0
  62. package/dist/esm/agentkit/vendors/avatar.d.mts +24 -12
  63. package/dist/esm/agentkit/vendors/avatar.mjs +45 -6
  64. package/dist/esm/agentkit/vendors/base.d.mts +8 -0
  65. package/dist/esm/agentkit/vendors/base.mjs +33 -7
  66. package/dist/esm/agentkit/vendors/llm.d.mts +7 -7
  67. package/dist/esm/agentkit/vendors/llm.mjs +26 -22
  68. package/dist/esm/agentkit/vendors/tts.d.mts +20 -0
  69. package/dist/esm/agentkit/vendors/tts.mjs +5 -2
  70. package/dist/esm/core/publicHeaders.d.mts +2 -0
  71. package/dist/esm/core/publicHeaders.mjs +5 -0
  72. package/dist/esm/index.d.mts +5 -0
  73. package/dist/esm/index.mjs +3 -0
  74. package/dist/esm/v3/campaign-schema.d.mts +1 -0
  75. package/dist/esm/v3/campaign-schema.mjs +2 -0
  76. package/dist/esm/v3/campaigns.d.mts +50 -0
  77. package/dist/esm/v3/campaigns.mjs +77 -0
  78. package/dist/esm/v3/contract.d.mts +1 -0
  79. package/dist/esm/v3/contract.mjs +9112 -0
  80. package/dist/esm/v3/index.d.mts +15 -0
  81. package/dist/esm/v3/index.mjs +15 -0
  82. package/dist/esm/v3/resources.d.mts +121 -0
  83. package/dist/esm/v3/resources.mjs +179 -0
  84. package/dist/esm/v3/session-lifecycle.d.mts +51 -0
  85. package/dist/esm/v3/session-lifecycle.mjs +121 -0
  86. package/dist/esm/v3/session-schema.d.mts +1 -0
  87. package/dist/esm/v3/session-schema.mjs +2 -0
  88. package/dist/esm/v3/transport.d.mts +36 -0
  89. package/dist/esm/v3/transport.mjs +151 -0
  90. package/dist/esm/v3/types.d.mts +1004 -0
  91. package/dist/esm/v3/types.mjs +1 -0
  92. package/dist/esm/version.d.mts +1 -1
  93. package/dist/esm/version.mjs +1 -1
  94. package/openapi.yaml +8917 -0
  95. package/package.json +7 -4
  96. package/reference.md +3 -0
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -1 +1 @@
1
- export declare const SDK_VERSION = "v2.12.0";
1
+ export declare const SDK_VERSION = "v3.0.0-alpha.2";
@@ -1,4 +1,4 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.SDK_VERSION = void 0;
4
- exports.SDK_VERSION = "v2.12.0";
4
+ exports.SDK_VERSION = "v3.0.0-alpha.2";
@@ -1,47 +1,39 @@
1
+ import { V3Client, type V3Options } from "./v3/index.mjs";
1
2
  import type { AgoraArea } from "./agentkit/area.mjs";
2
3
  import type { BaseClientOptions, BaseRequestOptions } from "./BaseClient.mjs";
3
4
  import { AgoraClient as BaseAgoraClient } from "./Client.mjs";
4
5
  import { Pool } from "./core/domain/index.mjs";
5
6
  /**
6
7
  * Auth mode for the client:
7
- * - `basic`: Customer ID + Secret for REST API (HTTP Basic Auth)
8
8
  * - `token`: Pre-built `agora token=<value>` auth header
9
9
  * - `app-credentials`: appId + appCertificate only; AgentSession generates
10
10
  * a fresh ConvoAI token per API call
11
11
  */
12
- export type AgoraAuthMode = "basic" | "token" | "app-credentials";
12
+ export type AgoraAuthMode = "token" | "app-credentials";
13
13
  export declare namespace AgoraClient {
14
14
  type Options<TArea extends AgoraArea = AgoraArea> = BaseOptions<TArea> & AuthOptions;
15
15
  interface BaseOptions<TArea extends AgoraArea = AgoraArea> extends Omit<BaseClientOptions, "baseUrl" | "environment" | "username" | "password" | "authorization"> {
16
16
  /** The area to use for regional URL selection */
17
17
  area: TArea;
18
+ /** Optional public origin override; baseUrl is a host, without the API prefix. */
19
+ v3?: V3Options;
18
20
  /** Agora App ID — used as the `appid` path parameter and for RTC token generation */
19
21
  appId: string;
20
- /** Agora App Certificate — used to sign RTC tokens. Keep this secret. */
21
- appCertificate: string;
22
- }
23
- /** Use Customer ID + Secret for REST API authentication (HTTP Basic Auth) */
24
- interface BasicAuthOptions {
25
- customerId: string;
26
- customerSecret: string;
27
- authToken?: never;
28
22
  }
29
23
  /** Use a pre-built Agora token for REST API authentication. Pass the raw token; the SDK sets `Authorization: agora token=<authToken>` automatically. */
30
24
  interface TokenAuthOptions {
25
+ appCertificate?: string;
31
26
  authToken: string;
32
- customerId?: never;
33
- customerSecret?: never;
34
27
  }
35
28
  /**
36
29
  * Use only appId + appCertificate. AgentSession generates a fresh
37
30
  * ConvoAI token per API call — no Customer Secret required.
38
31
  */
39
32
  interface AppCredentialsOptions {
40
- customerId?: never;
41
- customerSecret?: never;
33
+ appCertificate: string;
42
34
  authToken?: never;
43
35
  }
44
- type AuthOptions = BasicAuthOptions | TokenAuthOptions | AppCredentialsOptions;
36
+ type AuthOptions = TokenAuthOptions | AppCredentialsOptions;
45
37
  interface RequestOptions extends BaseRequestOptions {
46
38
  }
47
39
  }
@@ -49,7 +41,7 @@ export declare namespace AgoraClient {
49
41
  * AgoraClient extends the base client with domain pool support for
50
42
  * regional URL cycling and automatic domain selection.
51
43
  *
52
- * Three authentication modes are supported:
44
+ * Two authentication modes are supported:
53
45
  *
54
46
  * **App credentials (recommended)** — no Customer Secret needed; AgentSession
55
47
  * generates a short-lived ConvoAI token per API call:
@@ -71,23 +63,19 @@ export declare namespace AgoraClient {
71
63
  * });
72
64
  * ```
73
65
  *
74
- * **Basic auth** — Customer ID + Secret from Agora Console:
75
- * ```typescript
76
- * const client = new AgoraClient({
77
- * area: Area.US,
78
- * appId: "your-app-id",
79
- * appCertificate: "your-app-certificate",
80
- * customerId: "your-customer-id",
81
- * customerSecret: "your-customer-secret",
82
- * });
83
- * ```
84
66
  */
85
67
  export declare class AgoraClient<TArea extends AgoraArea = AgoraArea> extends BaseAgoraClient {
86
68
  protected readonly _pool: Pool;
69
+ readonly v3: V3Client;
70
+ get campaigns(): V3Client["campaigns"];
71
+ /** Project secrets shared with the advanced resource client. */
72
+ get secrets(): V3Client["secrets"];
73
+ /** Registered project numbers shared with the advanced resource client. */
74
+ get numbers(): V3Client["numbers"];
87
75
  /** Agora App ID */
88
76
  readonly appId: string;
89
77
  /** Agora App Certificate (used for RTC token signing) */
90
- readonly appCertificate: string;
78
+ readonly appCertificate: string | undefined;
91
79
  /** The active authentication mode */
92
80
  readonly authMode: AgoraAuthMode;
93
81
  /** The configured routing area */
@@ -1,4 +1,3 @@
1
- // This file was auto-generated by Fern from our API Definition.
2
1
  var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
2
  function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
3
  return new (P || (P = Promise))(function (resolve, reject) {
@@ -19,9 +18,11 @@ var __rest = (this && this.__rest) || function (s, e) {
19
18
  }
20
19
  return t;
21
20
  };
21
+ import { V3Client, V3Transport } from "./v3/index.mjs";
22
22
  import { generateConvoAIToken } from "./agentkit/token.mjs";
23
23
  import { AgoraClient as BaseAgoraClient } from "./Client.mjs";
24
24
  import { Area, CNAPIPath, GlobalAPIPath, Pool } from "./core/domain/index.mjs";
25
+ import { isInternalHeader } from "./core/publicHeaders.mjs";
25
26
  import { AgoraError } from "./errors/index.mjs";
26
27
  const API_BASE_URL_ENV = "AGORA_AGENTS_API_BASE_URL";
27
28
  function getConfiguredApiBaseUrl() {
@@ -65,7 +66,7 @@ function createClientPool(area) {
65
66
  * AgoraClient extends the base client with domain pool support for
66
67
  * regional URL cycling and automatic domain selection.
67
68
  *
68
- * Three authentication modes are supported:
69
+ * Two authentication modes are supported:
69
70
  *
70
71
  * **App credentials (recommended)** — no Customer Secret needed; AgentSession
71
72
  * generates a short-lived ConvoAI token per API call:
@@ -87,60 +88,80 @@ function createClientPool(area) {
87
88
  * });
88
89
  * ```
89
90
  *
90
- * **Basic auth** — Customer ID + Secret from Agora Console:
91
- * ```typescript
92
- * const client = new AgoraClient({
93
- * area: Area.US,
94
- * appId: "your-app-id",
95
- * appCertificate: "your-app-certificate",
96
- * customerId: "your-customer-id",
97
- * customerSecret: "your-customer-secret",
98
- * });
99
- * ```
100
91
  */
101
92
  export class AgoraClient extends BaseAgoraClient {
93
+ get campaigns() {
94
+ return this.v3.campaigns;
95
+ }
96
+ /** Project secrets shared with the advanced resource client. */
97
+ get secrets() {
98
+ return this.v3.secrets;
99
+ }
100
+ /** Registered project numbers shared with the advanced resource client. */
101
+ get numbers() {
102
+ return this.v3.numbers;
103
+ }
102
104
  constructor(options) {
105
+ var _a, _b, _c, _d;
103
106
  const pool = createClientPool(options.area);
104
- let authMode;
105
- let username;
106
- let password;
107
- let authToken = "";
108
- const opts = options;
109
- if (opts.customerId != null && opts.customerSecret != null) {
110
- authMode = "basic";
111
- username = opts.customerId;
112
- password = opts.customerSecret;
113
- }
114
- else if (opts.customerId != null) {
115
- throw new Error("customerSecret is required when customerId is provided");
116
- }
117
- else if (opts.authToken != null) {
118
- authMode = "token";
119
- authToken = opts.authToken;
120
- username = "";
121
- password = "";
122
- }
123
- else {
124
- authMode = "app-credentials";
125
- username = "";
126
- password = "";
107
+ for (const key of Object.keys(options)) {
108
+ if ([
109
+ "customerid",
110
+ "customersecret",
111
+ "clientid",
112
+ "clientsecret",
113
+ "username",
114
+ "password",
115
+ "authorization",
116
+ ].includes(key.replace(/_/g, "").toLowerCase())) {
117
+ throw new Error(`${key} is not supported; use appId/appCertificate or authToken`);
118
+ }
127
119
  }
128
- const { customerId: _cid, customerSecret: _cs, authToken: _at } = opts, rest = __rest(opts, ["customerId", "customerSecret", "authToken"]);
129
- // _getCustomAuthorizationHeaders() in the generated client spreads { Authorization }
130
- // after _getAuthorizationHeader(), so it overrides the Basic header on every request.
131
- const authorization = authMode === "basic"
132
- ? `Basic ${Buffer.from(`${username}:${password}`).toString("base64")}`
133
- : authMode === "token"
134
- ? `agora token=${authToken}`
135
- : "";
136
- super(Object.assign(Object.assign({}, rest), { username,
137
- password,
138
- authorization, baseUrl: () => pool.getCurrentURL() }));
120
+ const authMode = options.authToken !== undefined ? "token" : "app-credentials";
121
+ const { authToken, appId, appCertificate } = options, rest = __rest(options, ["authToken", "appId", "appCertificate"]);
122
+ if (authMode === "app-credentials" && !appCertificate)
123
+ throw new Error("appCertificate or authToken is required");
124
+ const authorization = () => authMode === "token"
125
+ ? `agora token=${authToken}`
126
+ : `agora token=${generateConvoAIToken({ appId, appCertificate: appCertificate, channelName: "", uid: 0 })}`;
127
+ const originalFetch = (_a = options.fetch) !== null && _a !== void 0 ? _a : globalThis.fetch;
128
+ const tokenOnlyFetch = (input, init) => __awaiter(this, void 0, void 0, function* () {
129
+ const finalHeaders = new Headers(input instanceof Request ? input.headers : undefined);
130
+ new Headers(init === null || init === void 0 ? void 0 : init.headers).forEach((value, key) => finalHeaders.set(key, value));
131
+ const auth = finalHeaders.get("authorization");
132
+ if (auth !== null && !/^agora token=\S+$/i.test(auth))
133
+ throw new Error("Only Agora token authorization is supported");
134
+ for (const name of finalHeaders.keys())
135
+ if (isInternalHeader(name))
136
+ throw new Error(`${name} is an internal header and is not supported`);
137
+ return originalFetch(input, init);
138
+ });
139
+ super(Object.assign(Object.assign({}, rest), { fetch: tokenOnlyFetch, username: "", password: "", authorization, baseUrl: () => pool.getCurrentURL() }));
139
140
  this._pool = pool;
140
- this.appId = options.appId;
141
- this.appCertificate = options.appCertificate;
141
+ this.appId = appId;
142
+ this.appCertificate = appCertificate;
142
143
  this.authMode = authMode;
143
144
  this.area = options.area;
145
+ const v3Base = (_c = (_b = options.v3) === null || _b === void 0 ? void 0 : _b.baseUrl) !== null && _c !== void 0 ? _c : process.env.AGORA_AGENTS_V3_BASE_URL;
146
+ if (v3Base !== undefined) {
147
+ let origin;
148
+ try {
149
+ origin = new URL(v3Base);
150
+ }
151
+ catch (_e) {
152
+ throw new Error("v3.baseUrl must be an HTTP(S) origin");
153
+ }
154
+ if (!["http:", "https:"].includes(origin.protocol) ||
155
+ origin.username ||
156
+ origin.password ||
157
+ origin.pathname !== "/" ||
158
+ origin.search ||
159
+ origin.hash ||
160
+ v3Base.includes("?") ||
161
+ v3Base.includes("#"))
162
+ throw new Error("v3.baseUrl must be an HTTP(S) origin without credentials, path, query or fragment");
163
+ }
164
+ this.v3 = new V3Client(new V3Transport(this._options, this.appId, () => v3Base !== null && v3Base !== void 0 ? v3Base : "https://preview.ai.agora.io", authorization, (_d = options.v3) === null || _d === void 0 ? void 0 : _d.headers));
144
165
  }
145
166
  /**
146
167
  * Get the underlying domain pool for advanced usage.
@@ -1,12 +1,13 @@
1
1
  // This file was auto-generated by Fern from our API Definition.
2
2
  import { mergeHeaders } from "./core/headers.mjs";
3
3
  import * as core from "./core/index.mjs";
4
+ import { SDK_VERSION } from "./version.mjs";
4
5
  export function normalizeClientOptions(options) {
5
6
  const headers = mergeHeaders({
6
7
  "X-Fern-Language": "JavaScript",
7
8
  "X-Fern-SDK-Name": "agora-agents",
8
- "X-Fern-SDK-Version": "v2.12.0",
9
- "User-Agent": "agora-agents/v2.12.0",
9
+ "X-Fern-SDK-Version": SDK_VERSION,
10
+ "User-Agent": `agora-agents/${SDK_VERSION}`,
10
11
  "X-Fern-Runtime": core.RUNTIME.type,
11
12
  "X-Fern-Runtime-Version": core.RUNTIME.version,
12
13
  }, options === null || options === void 0 ? void 0 : options.headers);
@@ -1,3 +1,4 @@
1
+ import { type DeployedAgent, type DeployOptions, type SavedAgent, type SaveOptions, type VariableDefinition } from "./v3.mjs";
1
2
  /**
2
3
  * Agent class - A reusable agent definition.
3
4
  *
@@ -10,7 +11,7 @@ import type * as Agora from "../api/index.mjs";
10
11
  import { AgentSession } from "./AgentSession.mjs";
11
12
  import type { AgoraArea } from "./area.mjs";
12
13
  import type { AvatarVendor, CNMllmVendor, GlobalMllmVendor, LlmVendor, SttVendor, TtsVendor } from "./region-vendors.mjs";
13
- import type { AdvancedFeatures, AvatarConfig, FillerWordsConfig, GeofenceConfig, InterruptionConfig, Labels, LlmConfig, LlmGreetingConfigs, MllmConfig, ParametersAudioScenario, RtcConfig, SalConfig, SessionOptions, SessionParamsInput, SttConfig, TtsConfig, TurnDetectionConfig } from "./types.mjs";
14
+ import type { AdvancedFeatures, AvatarConfig, CallOptions, FillerWordsConfig, GeofenceConfig, InterruptionConfig, Labels, LlmConfig, LlmGreetingConfigs, MllmConfig, ParametersAudioScenario, RtcConfig, SalConfig, SessionOptions, SessionParamsInput, SttConfig, TtsConfig, TurnDetectionConfig } from "./types.mjs";
14
15
  /**
15
16
  * Configuration options for creating an Agent.
16
17
  *
@@ -97,6 +98,7 @@ export interface AgentOptions<TArea extends AgoraArea = AgoraArea> {
97
98
  * ```
98
99
  */
99
100
  export declare class Agent<TTSSampleRate extends number = number, TArea extends AgoraArea = AgoraArea> {
101
+ private _variables;
100
102
  private _client;
101
103
  private _pipelineId?;
102
104
  private _llm?;
@@ -412,7 +414,17 @@ export declare class Agent<TTSSampleRate extends number = number, TArea extends
412
414
  * const agentId = await session.start();
413
415
  * ```
414
416
  */
417
+ withVariable(name: string, definition: VariableDefinition): Agent<TTSSampleRate, TArea>;
418
+ /** Persist this configuration and return a stored-agent handle. Does not start a session. */
419
+ save(options?: SaveOptions): Promise<SavedAgent>;
420
+ /** Compatibility alias for save(). */
421
+ deploy(options?: DeployOptions): Promise<DeployedAgent>;
415
422
  createSession(options: SessionOptions): AgentSession;
423
+ /** Start an unsaved configuration explicitly through the Platform API. */
424
+ createEphemeralSession(options: SessionOptions): AgentSession;
425
+ /** Create an outbound phone-call session from this unsaved configuration. */
426
+ call(options: CallOptions): AgentSession;
427
+ private _createPlatformSession;
416
428
  /**
417
429
  * Converts the Agent configuration to the Fern request properties format.
418
430
  *
@@ -456,6 +468,7 @@ export declare class Agent<TTSSampleRate extends number = number, TArea extends
456
468
  * If a new private field is added to Agent, it MUST also be added here.
457
469
  */
458
470
  private _clone;
471
+ private _resolvePlatformAsrConfig;
459
472
  private _resolveAsrConfig;
460
473
  private _resolveTurnDetectionConfig;
461
474
  }
@@ -1,10 +1,16 @@
1
- /**
2
- * Agent class - A reusable agent definition.
3
- *
4
- * This class represents an agent configuration that can be used to create
5
- * multiple sessions. Set the unique agent instance `name` in
6
- * {@link Agent.createSession}, not on the Agent itself.
7
- */
1
+ var __rest = (this && this.__rest) || function (s, e) {
2
+ var t = {};
3
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
4
+ t[p] = s[p];
5
+ if (s != null && typeof Object.getOwnPropertySymbols === "function")
6
+ for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
7
+ if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
8
+ t[p[i]] = s[p[i]];
9
+ }
10
+ return t;
11
+ };
12
+ import { callSessionOptions, createStoredSession } from "./session-backend.mjs";
13
+ import { assertNoSecretRefs, buildV3Agent, deployAgent, SecretRefNotSupportedError, validateVariable, } from "./v3.mjs";
8
14
  import { Area } from "../core/domain/index.mjs";
9
15
  import { AgentSession } from "./AgentSession.mjs";
10
16
  import { AudioScenario } from "./constants.mjs";
@@ -83,6 +89,7 @@ function assertTurnDetectionLanguage(value) {
83
89
  */
84
90
  export class Agent {
85
91
  constructor(options) {
92
+ this._variables = {};
86
93
  this._client = options.client;
87
94
  this._pipelineId = options.pipelineId;
88
95
  this._instructions = options.instructions;
@@ -570,10 +577,110 @@ export class Agent {
570
577
  * const agentId = await session.start();
571
578
  * ```
572
579
  */
580
+ withVariable(name, definition) {
581
+ validateVariable(name, definition);
582
+ const next = this._clone();
583
+ next._variables = Object.assign(Object.assign({}, this._variables), { [name]: Object.assign(Object.assign({}, definition), (definition.options ? { options: [...definition.options] } : {})) });
584
+ return next;
585
+ }
586
+ /** Persist this configuration and return a stored-agent handle. Does not start a session. */
587
+ save(options = {}) {
588
+ var _a, _b;
589
+ const config = this.config;
590
+ if (config.mllm === undefined && (config.stt !== undefined || ((_a = config.turnDetection) === null || _a === void 0 ? void 0 : _a.language) !== undefined)) {
591
+ // Keep Platform language precedence separate from legacy interaction defaults.
592
+ config.stt = this._resolvePlatformAsrConfig();
593
+ }
594
+ return deployAgent(this._client, buildV3Agent(config, this._variables, (_b = options.name) !== null && _b !== void 0 ? _b : `agent-${Date.now()}`), options);
595
+ }
596
+ /** Compatibility alias for save(). */
597
+ deploy(options = {}) {
598
+ return this.save(options);
599
+ }
573
600
  createSession(options) {
574
- var _a;
601
+ var _a, _b;
602
+ if ("clientReference" in options || "client_reference" in options)
603
+ throw new Error("client_reference is being removed; use the returned session ID for correlation");
604
+ const _c = this.config, { client: _client } = _c, config = __rest(_c, ["client"]);
605
+ let requiresStoredSession = Object.keys(this._variables).length > 0 ||
606
+ options.values !== undefined ||
607
+ options.idempotencyKey !== undefined ||
608
+ options.encryption !== undefined ||
609
+ options.audioScenario !== undefined ||
610
+ options.geofence !== undefined ||
611
+ options.gracefulStop !== undefined ||
612
+ options.dataPolicy !== undefined ||
613
+ options.labels !== undefined ||
614
+ options.maxDuration !== undefined;
615
+ try {
616
+ assertNoSecretRefs(config);
617
+ }
618
+ catch (error) {
619
+ if (error instanceof SecretRefNotSupportedError)
620
+ requiresStoredSession = true;
621
+ else
622
+ throw error;
623
+ }
624
+ if (requiresStoredSession)
625
+ return this.createEphemeralSession(options);
575
626
  const name = (_a = options.name) !== null && _a !== void 0 ? _a : `agent-${Date.now()}`;
576
- return new AgentSession(Object.assign(Object.assign({ client: this._client, agent: this, appId: this._client.appId, appCertificate: this._client.appCertificate }, options), { name }));
627
+ return new AgentSession(Object.assign(Object.assign({ client: this._client, agent: this, appId: this._client.appId, appCertificate: this._client.appCertificate }, options), { agentUid: (_b = options.agentUid) !== null && _b !== void 0 ? _b : "0", name }));
628
+ }
629
+ /** Start an unsaved configuration explicitly through the Platform API. */
630
+ createEphemeralSession(options) {
631
+ return this._createPlatformSession(options);
632
+ }
633
+ /** Create an outbound phone-call session from this unsaved configuration. */
634
+ call(options) {
635
+ return this._createPlatformSession(callSessionOptions(options), {
636
+ type: "telephony",
637
+ from: options.from,
638
+ to: options.to,
639
+ call_policy: options.callPolicy,
640
+ });
641
+ }
642
+ _createPlatformSession(options, transport) {
643
+ var _a, _b, _c, _d, _e, _f, _g;
644
+ const normalized = this.config;
645
+ const sessionOptions = Object.assign({}, options);
646
+ if (normalized.geofence !== undefined) {
647
+ (_a = sessionOptions.geofence) !== null && _a !== void 0 ? _a : (sessionOptions.geofence = Object.assign({ area: normalized.geofence.area.toLowerCase() }, (normalized.geofence.exclude_area === undefined
648
+ ? {}
649
+ : {
650
+ exclude_area: normalized.geofence.exclude_area.toLowerCase(),
651
+ })));
652
+ normalized.geofence = undefined;
653
+ }
654
+ const params = Object.assign({}, normalized.parameters);
655
+ if (params.opt_out !== undefined) {
656
+ (_b = sessionOptions.dataPolicy) !== null && _b !== void 0 ? _b : (sessionOptions.dataPolicy = { retention: params.opt_out ? "none" : "30_days" });
657
+ delete params.opt_out;
658
+ }
659
+ if (params.farewell_config !== undefined) {
660
+ const farewell = params.farewell_config;
661
+ (_c = sessionOptions.gracefulStop) !== null && _c !== void 0 ? _c : (sessionOptions.gracefulStop = {
662
+ enabled: farewell.graceful_enabled,
663
+ timeout_ms: farewell.graceful_timeout_seconds === undefined
664
+ ? undefined
665
+ : farewell.graceful_timeout_seconds * 1000,
666
+ });
667
+ delete params.farewell_config;
668
+ }
669
+ if (params.audio_scenario !== undefined) {
670
+ (_d = sessionOptions.audioScenario) !== null && _d !== void 0 ? _d : (sessionOptions.audioScenario = params.audio_scenario === "default" ? "aiserver" : params.audio_scenario);
671
+ delete params.audio_scenario;
672
+ }
673
+ normalized.parameters = Object.keys(params).length ? params : undefined;
674
+ if (normalized.labels !== undefined) {
675
+ (_e = sessionOptions.labels) !== null && _e !== void 0 ? _e : (sessionOptions.labels = normalized.labels);
676
+ normalized.labels = undefined;
677
+ }
678
+ if (normalized.mllm === undefined &&
679
+ (normalized.stt !== undefined || ((_f = normalized.turnDetection) === null || _f === void 0 ? void 0 : _f.language) !== undefined)) {
680
+ normalized.stt = this._resolvePlatformAsrConfig();
681
+ }
682
+ const _h = buildV3Agent(normalized, this._variables, (_g = options.name) !== null && _g !== void 0 ? _g : "ephemeral"), { agent_name: _name } = _h, agent = __rest(_h, ["agent_name"]);
683
+ return createStoredSession(this._client, sessionOptions, { agent }, this, transport);
577
684
  }
578
685
  /**
579
686
  * Converts the Agent configuration to the Fern request properties format.
@@ -584,6 +691,8 @@ export class Agent {
584
691
  */
585
692
  toProperties(opts) {
586
693
  var _a, _b, _c, _d, _e, _f, _g, _h;
694
+ const _j = this.config, { client: _client } = _j, config = __rest(_j, ["client"]);
695
+ assertNoSecretRefs(config);
587
696
  // In MLLM mode the backend handles audio end-to-end; LLM, TTS, and ASR
588
697
  // are not required.
589
698
  const isMllmMode = ((_a = this._mllm) === null || _a === void 0 ? void 0 : _a.enable) === true || this._mllm !== undefined;
@@ -708,6 +817,7 @@ export class Agent {
708
817
  */
709
818
  _clone() {
710
819
  const newAgent = new Agent({ client: this._client });
820
+ newAgent._variables = Object.assign({}, this._variables);
711
821
  newAgent._pipelineId = this._pipelineId;
712
822
  newAgent._llm = this._llm;
713
823
  newAgent._tts = this._tts;
@@ -730,6 +840,13 @@ export class Agent {
730
840
  newAgent._greetingConfigs = this._greetingConfigs;
731
841
  return newAgent;
732
842
  }
843
+ _resolvePlatformAsrConfig() {
844
+ var _a, _b, _c, _d;
845
+ const config = this._stt ? Object.assign({}, this._stt) : { vendor: this._client.area === Area.CN ? "fengming" : "ares" };
846
+ const params = config.params;
847
+ const language = (_d = (_c = (_b = (_a = this._turnDetection) === null || _a === void 0 ? void 0 : _a.language) !== null && _b !== void 0 ? _b : config.language) !== null && _c !== void 0 ? _c : params === null || params === void 0 ? void 0 : params.language) !== null && _d !== void 0 ? _d : (["ares", "fengming"].includes(config.vendor) ? DEFAULT_TURN_DETECTION_LANGUAGE : undefined);
848
+ return Object.assign(Object.assign({}, config), (typeof language === "string" ? { language } : {}));
849
+ }
733
850
  _resolveAsrConfig(turnDetectionConfig) {
734
851
  const asrConfig = this._stt !== undefined
735
852
  ? Object.assign({}, this._stt)
@@ -10,6 +10,7 @@ import type { AgentsClient } from "../api/resources/agents/client/Client.mjs";
10
10
  import type { Agent } from "./Agent.mjs";
11
11
  import type { AgoraArea } from "./area.mjs";
12
12
  import { type PresetInput } from "./presets.mjs";
13
+ import { type SessionBackend } from "./session-backend.mjs";
13
14
  import type { AgentConfigUpdate, ConversationHistory, ConversationTurns, GetTurnsOptions, SayOptions, SessionInfo, ThinkOptions, ThinkResponse } from "./types.mjs";
14
15
  /**
15
16
  * Event types that can be emitted by AgentSession.
@@ -26,7 +27,9 @@ export interface AgentSessionOptions {
26
27
  /** The Agora client instance */
27
28
  client: AgoraClient<AgoraArea>;
28
29
  /** The agent configuration */
29
- agent: Agent<number, AgoraArea>;
30
+ agent?: Agent<number, AgoraArea>;
31
+ /** @internal Transport adapter for stored/ephemeral session identities. */
32
+ backend?: SessionBackend;
30
33
  /** The App ID */
31
34
  appId: string;
32
35
  /** The App Certificate — enables automatic RTC token generation when starting sessions */
@@ -98,7 +101,8 @@ export interface AgentSessionOptions {
98
101
  */
99
102
  export declare class AgentSession {
100
103
  private readonly _client;
101
- private readonly _agent;
104
+ private readonly _agent?;
105
+ private readonly _backend?;
102
106
  private readonly _appId;
103
107
  private readonly _appCertificate?;
104
108
  private readonly _name;
@@ -119,6 +123,11 @@ export declare class AgentSession {
119
123
  private _sessionBaseUrl?;
120
124
  private _agentId;
121
125
  private _status;
126
+ private _lifecycleRevision;
127
+ private _infoReadRevision;
128
+ private _pendingLifecycle;
129
+ private _beginLifecycle;
130
+ private _finishLifecycle;
122
131
  private _eventHandlers;
123
132
  private readonly _warn;
124
133
  constructor(options: AgentSessionOptions);