@fonoster/apiserver 0.22.14 → 0.24.0

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.
@@ -62,17 +62,7 @@ function createCreateCallSubscriber(config) {
62
62
  extension: common_1.CALL_EXTENSION,
63
63
  endpoint: `PJSIP/${envs_1.ASTERISK_TRUNK}/sip:${to}@${envs_1.ASTERISK_SYSTEM_DOMAIN}`,
64
64
  timeout,
65
- variables: {
66
- "PJSIP_HEADER(add,X-Call-Ref)": ref,
67
- "PJSIP_HEADER(add,X-Dod-Number)": from,
68
- "PJSIP_HEADER(add,X-Access-Key-Id)": accessKeyId,
69
- "PJSIP_HEADER(add,X-Is-Api-Originated-Type)": "true",
70
- CALL_DIRECTION: "peer-to-pstn",
71
- INGRESS_NUMBER: from,
72
- APP_REF: appRef,
73
- METADATA: JSON.stringify(metadata),
74
- CALL_REF: ref
75
- }
65
+ variables: Object.assign({ "PJSIP_HEADER(add,X-Call-Ref)": ref, "PJSIP_HEADER(add,X-Dod-Number)": from, "PJSIP_HEADER(add,X-Access-Key-Id)": accessKeyId, "PJSIP_HEADER(add,X-Is-Api-Originated-Type)": "true", CALL_DIRECTION: "peer-to-pstn", INGRESS_NUMBER: from, APP_REF: appRef, METADATA: JSON.stringify(metadata), CALL_REF: ref }, (envs_1.AMD_ENABLED ? { AMD_ENABLED: "true" } : {}))
76
66
  });
77
67
  });
78
68
  }
package/dist/envs.d.ts CHANGED
@@ -9,6 +9,7 @@ export declare const ASTERISK_SYSTEM_DOMAIN: string;
9
9
  export declare const ASTERISK_TRUNK = "routr";
10
10
  export declare const CALLS_CREATE_SUBJECT = "calls.create";
11
11
  export declare const CALLS_TRACK_CALL_SUBJECT = "calls.track";
12
+ export declare const AMD_ENABLED: boolean;
12
13
  export declare const CLOAK_ENCRYPTION_KEY: string;
13
14
  export declare const DEFAULT_NATS_QUEUE_GROUP = "apiserver";
14
15
  export declare const HTTP_BRIDGE_PORT: number;
package/dist/envs.js CHANGED
@@ -4,8 +4,8 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
4
4
  };
5
5
  var _a;
6
6
  Object.defineProperty(exports, "__esModule", { value: true });
7
- exports.SMTP_AUTH_PASS = exports.ROUTR_DEFAULT_PEER_USERNAME = exports.ROUTR_DEFAULT_PEER_PASSWORD = exports.ROUTR_DEFAULT_PEER_NAME = exports.ROUTR_DEFAULT_PEER_AOR = exports.ROUTR_API_ENDPOINT = exports.OWNER_PASSWORD = exports.OWNER_NAME = exports.OWNER_EMAIL = exports.NATS_URL = exports.INTEGRATIONS_FILE = exports.INFLUXDB_USERNAME = exports.INFLUXDB_URL = exports.INFLUXDB_TOKEN = exports.INFLUXDB_PASSWORD = exports.INFLUXDB_ORG = exports.AUTHZ_SERVICE_METHODS = exports.AUTHZ_SERVICE_PORT = exports.AUTHZ_SERVICE_HOST = exports.AUTHZ_SERVICE_ENABLED = exports.IDENTITY_DATABASE_URL = exports.IDENTITY_WORKSPACE_INVITE_URL = exports.IDENTITY_WORKSPACE_INVITE_EXPIRATION = exports.IDENTITY_WORKSPACE_INVITE_FAIL_URL = exports.IDENTITY_REFRESH_TOKEN_EXPIRES_IN = exports.IDENTITY_PUBLIC_KEY = exports.IDENTITY_PRIVATE_KEY = exports.IDENTITY_OAUTH2_GITHUB_CLIENT_SECRET = exports.IDENTITY_OAUTH2_GITHUB_CLIENT_ID = exports.IDENTITY_OAUTH2_GITHUB_ENABLED = exports.IDENTITY_TWO_FACTOR_AUTHENTICATION_REQUIRED = exports.IDENTITY_CONTACT_VERIFICATION_REQUIRED = exports.IDENTITY_ISSUER = exports.IDENTITY_ID_TOKEN_EXPIRES_IN = exports.IDENTITY_AUDIENCE = exports.IDENTITY_ACCESS_TOKEN_EXPIRES_IN = exports.HTTP_BRIDGE_PORT = exports.DEFAULT_NATS_QUEUE_GROUP = exports.CLOAK_ENCRYPTION_KEY = exports.CALLS_TRACK_CALL_SUBJECT = exports.CALLS_CREATE_SUBJECT = exports.ASTERISK_TRUNK = exports.ASTERISK_SYSTEM_DOMAIN = exports.ASTERISK_ARI_USERNAME = exports.ASTERISK_ARI_SECRET = exports.ASTERISK_ARI_PROXY_URL = exports.APP_URL = exports.APISERVER_SIGNALING_SERVER = exports.APISERVER_HOST = exports.APISERVER_BIND_ADDR = void 0;
8
- exports.TWILIO_PHONE_NUMBER = exports.TWILIO_AUTH_TOKEN = exports.TWILIO_ACCOUNT_SID = exports.TEMPLATES_DIR = exports.SMTP_SENDER = exports.SMTP_SECURE = exports.SMTP_PORT = exports.SMTP_HOST = exports.SMTP_AUTH_USER = void 0;
7
+ exports.ROUTR_DEFAULT_PEER_USERNAME = exports.ROUTR_DEFAULT_PEER_PASSWORD = exports.ROUTR_DEFAULT_PEER_NAME = exports.ROUTR_DEFAULT_PEER_AOR = exports.ROUTR_API_ENDPOINT = exports.OWNER_PASSWORD = exports.OWNER_NAME = exports.OWNER_EMAIL = exports.NATS_URL = exports.INTEGRATIONS_FILE = exports.INFLUXDB_USERNAME = exports.INFLUXDB_URL = exports.INFLUXDB_TOKEN = exports.INFLUXDB_PASSWORD = exports.INFLUXDB_ORG = exports.AUTHZ_SERVICE_METHODS = exports.AUTHZ_SERVICE_PORT = exports.AUTHZ_SERVICE_HOST = exports.AUTHZ_SERVICE_ENABLED = exports.IDENTITY_DATABASE_URL = exports.IDENTITY_WORKSPACE_INVITE_URL = exports.IDENTITY_WORKSPACE_INVITE_EXPIRATION = exports.IDENTITY_WORKSPACE_INVITE_FAIL_URL = exports.IDENTITY_REFRESH_TOKEN_EXPIRES_IN = exports.IDENTITY_PUBLIC_KEY = exports.IDENTITY_PRIVATE_KEY = exports.IDENTITY_OAUTH2_GITHUB_CLIENT_SECRET = exports.IDENTITY_OAUTH2_GITHUB_CLIENT_ID = exports.IDENTITY_OAUTH2_GITHUB_ENABLED = exports.IDENTITY_TWO_FACTOR_AUTHENTICATION_REQUIRED = exports.IDENTITY_CONTACT_VERIFICATION_REQUIRED = exports.IDENTITY_ISSUER = exports.IDENTITY_ID_TOKEN_EXPIRES_IN = exports.IDENTITY_AUDIENCE = exports.IDENTITY_ACCESS_TOKEN_EXPIRES_IN = exports.HTTP_BRIDGE_PORT = exports.DEFAULT_NATS_QUEUE_GROUP = exports.CLOAK_ENCRYPTION_KEY = exports.AMD_ENABLED = exports.CALLS_TRACK_CALL_SUBJECT = exports.CALLS_CREATE_SUBJECT = exports.ASTERISK_TRUNK = exports.ASTERISK_SYSTEM_DOMAIN = exports.ASTERISK_ARI_USERNAME = exports.ASTERISK_ARI_SECRET = exports.ASTERISK_ARI_PROXY_URL = exports.APP_URL = exports.APISERVER_SIGNALING_SERVER = exports.APISERVER_HOST = exports.APISERVER_BIND_ADDR = void 0;
8
+ exports.TWILIO_PHONE_NUMBER = exports.TWILIO_AUTH_TOKEN = exports.TWILIO_ACCOUNT_SID = exports.TEMPLATES_DIR = exports.SMTP_SENDER = exports.SMTP_SECURE = exports.SMTP_PORT = exports.SMTP_HOST = exports.SMTP_AUTH_USER = exports.SMTP_AUTH_PASS = void 0;
9
9
  /**
10
10
  * Copyright (C) 2025 by Fonoster Inc (https://fonoster.com)
11
11
  * http://github.com/fonoster/fonoster
@@ -69,6 +69,14 @@ exports.ASTERISK_SYSTEM_DOMAIN = e.APISERVER_ASTERISK_SYSTEM_DOMAIN || "sip.inva
69
69
  exports.ASTERISK_TRUNK = "routr";
70
70
  exports.CALLS_CREATE_SUBJECT = "calls.create";
71
71
  exports.CALLS_TRACK_CALL_SUBJECT = "calls.track";
72
+ // Answering Machine Detection (AMD) configuration
73
+ //
74
+ // When enabled, outbound calls run Asterisk's AMD() application in the dialplan
75
+ // before entering Stasis, and its verdict is attached to the session request as
76
+ // `amd`. Thresholds live in the media server's amd.conf, not here. Disabled by
77
+ // default: when on, the caller hears silence for up to amd.conf's
78
+ // total_analysis_time before the voice application is dispatched.
79
+ exports.AMD_ENABLED = e.APISERVER_AMD_ENABLED === "true";
72
80
  // Other configurations
73
81
  exports.CLOAK_ENCRYPTION_KEY = e.APISERVER_CLOAK_ENCRYPTION_KEY;
74
82
  exports.DEFAULT_NATS_QUEUE_GROUP = "apiserver";
@@ -0,0 +1,23 @@
1
+ type CachedAmdResult = {
2
+ status: string;
3
+ confidence: number;
4
+ detector: string;
5
+ latencyMs: number;
6
+ cause: string;
7
+ };
8
+ /**
9
+ * Process-local bridge between the voice path and the CDR writer.
10
+ * createCreateVoiceClient stores the AMD verdict here keyed by `callRef` as it
11
+ * reads it off the channel; createInfluxDbPub reads it while building the call's
12
+ * CDR points so the verdict is persisted alongside the rest of the record for
13
+ * later analysis.
14
+ *
15
+ * For API-originated (outbound) calls `callRef` equals the CDR `ref` tag, so the
16
+ * lookup is direct. SIP-originated inbound calls mint independent refs on each
17
+ * side and are not correlated here — AMD only runs on outbound calls.
18
+ */
19
+ declare const amdResultCache: {
20
+ get(key: string): CachedAmdResult;
21
+ set(key: string, value: CachedAmdResult): void;
22
+ };
23
+ export { amdResultCache, CachedAmdResult };
@@ -0,0 +1,35 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.amdResultCache = void 0;
4
+ /**
5
+ * Copyright (C) 2025 by Fonoster Inc (https://fonoster.com)
6
+ * http://github.com/fonoster/fonoster
7
+ *
8
+ * This file is part of Fonoster
9
+ *
10
+ * Licensed under the MIT License (the "License");
11
+ * you may not use this file except in compliance with
12
+ * the License. You may obtain a copy of the License at
13
+ *
14
+ * https://opensource.org/licenses/MIT
15
+ *
16
+ * Unless required by applicable law or agreed to in writing, software
17
+ * distributed under the License is distributed on an "AS IS" BASIS,
18
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19
+ * See the License for the specific language governing permissions and
20
+ * limitations under the License.
21
+ */
22
+ const createPerCallCache_1 = require("./createPerCallCache");
23
+ /**
24
+ * Process-local bridge between the voice path and the CDR writer.
25
+ * createCreateVoiceClient stores the AMD verdict here keyed by `callRef` as it
26
+ * reads it off the channel; createInfluxDbPub reads it while building the call's
27
+ * CDR points so the verdict is persisted alongside the rest of the record for
28
+ * later analysis.
29
+ *
30
+ * For API-originated (outbound) calls `callRef` equals the CDR `ref` tag, so the
31
+ * lookup is direct. SIP-originated inbound calls mint independent refs on each
32
+ * side and are not correlated here — AMD only runs on outbound calls.
33
+ */
34
+ const amdResultCache = (0, createPerCallCache_1.createPerCallCache)(24 * 60 * 60 * 1000);
35
+ exports.amdResultCache = amdResultCache;
@@ -21,6 +21,7 @@ exports.createInfluxDbPub = createInfluxDbPub;
21
21
  */
22
22
  const logger_1 = require("@fonoster/logger");
23
23
  const influxdb_client_1 = require("@influxdata/influxdb-client");
24
+ const amdResultCache_1 = require("./amdResultCache");
24
25
  const createPerCallCache_1 = require("./createPerCallCache");
25
26
  const logger = (0, logger_1.getLogger)({ service: "apiserver", filePath: __filename });
26
27
  function createInfluxDbPub(config) {
@@ -43,6 +44,22 @@ function createInfluxDbPub(config) {
43
44
  if (ref) {
44
45
  point.tag("ref", ref);
45
46
  refByCallId.set(event.tag, ref);
47
+ // Fold in the Answering Machine Detection verdict (if AMD ran for this
48
+ // call), but only on the terminal event — the one carrying the hangup
49
+ // cause as `status`. By then the verdict has long been read off the
50
+ // channel, and writing it once keeps every earlier point (and raw,
51
+ // non-pivoted queries) clean.
52
+ if (event.data.status) {
53
+ const amd = amdResultCache_1.amdResultCache.get(ref);
54
+ if (amd) {
55
+ point
56
+ .stringField("amdStatus", amd.status)
57
+ .floatField("amdConfidence", amd.confidence)
58
+ .stringField("amdDetector", amd.detector)
59
+ .intField("amdLatencyMs", amd.latencyMs)
60
+ .stringField("amdCause", amd.cause);
61
+ }
62
+ }
46
63
  }
47
64
  Object.entries(event.data).forEach(([key, value]) => {
48
65
  if (key === "ref") {
@@ -0,0 +1,38 @@
1
+ /**
2
+ * Copyright (C) 2025 by Fonoster Inc (https://fonoster.com)
3
+ * http://github.com/fonoster/fonoster
4
+ *
5
+ * This file is part of Fonoster
6
+ *
7
+ * Licensed under the MIT License (the "License");
8
+ * you may not use this file except in compliance with
9
+ * the License. You may obtain a copy of the License at
10
+ *
11
+ * https://opensource.org/licenses/MIT
12
+ *
13
+ * Unless required by applicable law or agreed to in writing, software
14
+ * distributed under the License is distributed on an "AS IS" BASIS,
15
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
+ * See the License for the specific language governing permissions and
17
+ * limitations under the License.
18
+ */
19
+ import { Amd } from "@fonoster/common";
20
+ declare const DETECTOR = "asterisk-amd@1";
21
+ /**
22
+ * Maps Asterisk's AMDSTATUS channel variable onto the Amd verdict carried on the
23
+ * session request.
24
+ *
25
+ * Returns undefined when AMD did not run for this call — the detector is off, the
26
+ * call is inbound, or the media server predates the AMD dialplan — which the
27
+ * proto loader (`defaults: false`) surfaces to voice applications as an absent
28
+ * `req.amd` rather than an UNSPECIFIED status.
29
+ *
30
+ * Note that app_amd cannot tell a voicemail greeting or an IVR menu apart from a
31
+ * generic answering machine, so VOICEMAIL and IVR are never returned even though
32
+ * the contract carries them.
33
+ *
34
+ * @param {string} amdStatus - the raw AMDSTATUS channel variable, if set
35
+ * @return {Amd | undefined} the verdict, or undefined if AMD did not run
36
+ */
37
+ declare function mapAsteriskAmd(amdStatus?: string): Amd | undefined;
38
+ export { DETECTOR, mapAsteriskAmd };
@@ -0,0 +1,68 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.DETECTOR = void 0;
4
+ exports.mapAsteriskAmd = mapAsteriskAmd;
5
+ /**
6
+ * Copyright (C) 2025 by Fonoster Inc (https://fonoster.com)
7
+ * http://github.com/fonoster/fonoster
8
+ *
9
+ * This file is part of Fonoster
10
+ *
11
+ * Licensed under the MIT License (the "License");
12
+ * you may not use this file except in compliance with
13
+ * the License. You may obtain a copy of the License at
14
+ *
15
+ * https://opensource.org/licenses/MIT
16
+ *
17
+ * Unless required by applicable law or agreed to in writing, software
18
+ * distributed under the License is distributed on an "AS IS" BASIS,
19
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20
+ * See the License for the specific language governing permissions and
21
+ * limitations under the License.
22
+ */
23
+ const common_1 = require("@fonoster/common");
24
+ // Identifies the detector on the verdict, so a consumer can tell an Asterisk
25
+ // AMD() result apart from a future model-based one. Bump the suffix if the
26
+ // mapping or the thresholds shipped in amd.conf change meaningfully.
27
+ const DETECTOR = "asterisk-amd@1";
28
+ exports.DETECTOR = DETECTOR;
29
+ // Asterisk's app_amd is a deterministic heuristic, not a classifier: it returns
30
+ // a verdict with no probability attached. We report full confidence on a real
31
+ // decision and none on an inconclusive one, so a consumer gating on a threshold
32
+ // (as it would against a model-based detector) keeps working unchanged.
33
+ const statusMap = {
34
+ HUMAN: { status: common_1.AmdStatus.HUMAN, confidence: 1 },
35
+ MACHINE: { status: common_1.AmdStatus.MACHINE, confidence: 1 },
36
+ // The analysis ran out of time without deciding.
37
+ NOTSURE: { status: common_1.AmdStatus.UNKNOWN, confidence: 0 },
38
+ // The far end hung up mid-analysis. Rarely observed here, since the call
39
+ // usually never reaches Stasis, but map it rather than leave it undefined.
40
+ HANGUP: { status: common_1.AmdStatus.UNKNOWN, confidence: 0 }
41
+ };
42
+ /**
43
+ * Maps Asterisk's AMDSTATUS channel variable onto the Amd verdict carried on the
44
+ * session request.
45
+ *
46
+ * Returns undefined when AMD did not run for this call — the detector is off, the
47
+ * call is inbound, or the media server predates the AMD dialplan — which the
48
+ * proto loader (`defaults: false`) surfaces to voice applications as an absent
49
+ * `req.amd` rather than an UNSPECIFIED status.
50
+ *
51
+ * Note that app_amd cannot tell a voicemail greeting or an IVR menu apart from a
52
+ * generic answering machine, so VOICEMAIL and IVR are never returned even though
53
+ * the contract carries them.
54
+ *
55
+ * @param {string} amdStatus - the raw AMDSTATUS channel variable, if set
56
+ * @return {Amd | undefined} the verdict, or undefined if AMD did not run
57
+ */
58
+ function mapAsteriskAmd(amdStatus) {
59
+ var _a;
60
+ const mapped = statusMap[(_a = amdStatus === null || amdStatus === void 0 ? void 0 : amdStatus.trim().toUpperCase()) !== null && _a !== void 0 ? _a : ""];
61
+ if (!mapped) {
62
+ return undefined;
63
+ }
64
+ return Object.assign(Object.assign({}, mapped), { detector: DETECTOR,
65
+ // app_amd reports no elapsed time and the dialplan has no millisecond clock,
66
+ // so this is left unmeasured rather than estimated.
67
+ latencyMs: 0 });
68
+ }
@@ -30,11 +30,14 @@ exports.createCreateVoiceClient = createCreateVoiceClient;
30
30
  */
31
31
  const identity_1 = require("@fonoster/identity");
32
32
  const logger_1 = require("@fonoster/logger");
33
+ const types_1 = require("@fonoster/types");
33
34
  const uuid_1 = require("uuid");
34
35
  const identityConfig_1 = require("../core/identityConfig");
36
+ const amdResultCache_1 = require("../events/amdResultCache");
35
37
  const mapCallDirectionToEnum_1 = require("../events/mapCallDirectionToEnum");
38
+ const mapAsteriskAmd_1 = require("./amd/mapAsteriskAmd");
36
39
  const client_1 = require("./client");
37
- const types_1 = require("./types");
40
+ const types_2 = require("./types");
38
41
  const createGetChannelVarWithoutThrow_1 = require("./utils/createGetChannelVarWithoutThrow");
39
42
  const logger = (0, logger_1.getLogger)({ service: "apiserver", filePath: __filename });
40
43
  const generateCallAccessToken = (0, identity_1.createGenerateCallAccessToken)(identityConfig_1.identityConfig);
@@ -42,24 +45,33 @@ const generateCallAccessToken = (0, identity_1.createGenerateCallAccessToken)(id
42
45
  function createCreateVoiceClient(createContainer) {
43
46
  return function createVoiceClient(params) {
44
47
  return __awaiter(this, void 0, void 0, function* () {
45
- var _a, _b, _c, _d, _e, _f;
48
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j;
46
49
  const { ari, event, channel } = params;
47
50
  const { id: mediaSessionRef, caller } = event.channel;
48
51
  const { name: callerName, number: callerNumber } = caller;
49
52
  const getChannelVar = (0, createGetChannelVarWithoutThrow_1.createGetChannelVarWithoutThrow)(channel);
50
53
  // Variables set by Asterisk's dialplan
51
- const callDirection = (_a = (yield getChannelVar(types_1.ChannelVar.CALL_DIRECTION))) === null || _a === void 0 ? void 0 : _a.value;
52
- const appRef = (_b = (yield getChannelVar(types_1.ChannelVar.APP_REF))) === null || _b === void 0 ? void 0 : _b.value;
53
- const ingressNumber = ((_c = (yield getChannelVar(types_1.ChannelVar.INGRESS_NUMBER))) === null || _c === void 0 ? void 0 : _c.value) || "";
54
+ const callDirection = (_a = (yield getChannelVar(types_2.ChannelVar.CALL_DIRECTION))) === null || _a === void 0 ? void 0 : _a.value;
55
+ const appRef = (_b = (yield getChannelVar(types_2.ChannelVar.APP_REF))) === null || _b === void 0 ? void 0 : _b.value;
56
+ const ingressNumber = ((_c = (yield getChannelVar(types_2.ChannelVar.INGRESS_NUMBER))) === null || _c === void 0 ? void 0 : _c.value) || "";
54
57
  // Try to get callRef from channel variable (set by dialplan from X-Call-Ref header for API-originated calls)
55
58
  // If not found, generate a new UUID (for PSTN-terminated calls)
56
- const callRefFromChannel = (_d = (yield getChannelVar(types_1.ChannelVar.CALL_REF))) === null || _d === void 0 ? void 0 : _d.value;
59
+ const callRefFromChannel = (_d = (yield getChannelVar(types_2.ChannelVar.CALL_REF))) === null || _d === void 0 ? void 0 : _d.value;
57
60
  const callRef = callRefFromChannel || (0, uuid_1.v4)();
58
61
  const { accessKeyId, endpoint, tts, stt } = yield createContainer(appRef);
59
62
  const sessionToken = yield generateCallAccessToken({ accessKeyId, appRef });
60
- const metadataStr = (_f = (_e = (yield getChannelVar(types_1.ChannelVar.METADATA))) === null || _e === void 0 ? void 0 : _e.value) !== null && _f !== void 0 ? _f : "{}";
61
- const config = {
62
- appRef,
63
+ const metadataStr = (_f = (_e = (yield getChannelVar(types_2.ChannelVar.METADATA))) === null || _e === void 0 ? void 0 : _e.value) !== null && _f !== void 0 ? _f : "{}";
64
+ const callDirectionEnum = (0, mapCallDirectionToEnum_1.mapCallDirectionToEnum)(callDirection);
65
+ // Answering Machine Detection. AMD() runs in the dialplan before Stasis, so
66
+ // by the time we get here the verdict is already on the channel — undefined
67
+ // when AMD is off or the media server predates the AMD dialplan. Only
68
+ // outbound calls are ever classified (an inbound channel has not been
69
+ // answered when the dialplan runs), so don't pay the lookup on any other
70
+ // direction: an unset channel variable costs a round-trip that 404s.
71
+ const amd = callDirectionEnum === types_1.CallDirection.TO_PSTN
72
+ ? (0, mapAsteriskAmd_1.mapAsteriskAmd)((_g = (yield getChannelVar(types_2.ChannelVar.AMD_STATUS))) === null || _g === void 0 ? void 0 : _g.value)
73
+ : undefined;
74
+ const config = Object.assign({ appRef,
63
75
  mediaSessionRef,
64
76
  callRef,
65
77
  accessKeyId,
@@ -67,14 +79,17 @@ function createCreateVoiceClient(createContainer) {
67
79
  callerName,
68
80
  callerNumber,
69
81
  ingressNumber,
70
- sessionToken,
71
- callDirection: (0, mapCallDirectionToEnum_1.mapCallDirectionToEnum)(callDirection),
72
- metadata: JSON.parse(metadataStr)
73
- };
82
+ sessionToken, callDirection: callDirectionEnum, metadata: JSON.parse(metadataStr) }, (amd ? { amd } : {}));
83
+ if (amd) {
84
+ // AMDCAUSE says which threshold fired. It is not part of the session
85
+ // contract, so it rides to the CDR on its own for offline tuning.
86
+ amdResultCache_1.amdResultCache.set(callRef, Object.assign(Object.assign({}, amd), { cause: (_j = (_h = (yield getChannelVar(types_2.ChannelVar.AMD_CAUSE))) === null || _h === void 0 ? void 0 : _h.value) !== null && _j !== void 0 ? _j : "" }));
87
+ }
74
88
  logger.verbose("creating voice client with config: ", {
75
89
  appRef,
76
90
  callerNumber,
77
- ingressNumber
91
+ ingressNumber,
92
+ amdStatus: amd === null || amd === void 0 ? void 0 : amd.status
78
93
  });
79
94
  return new client_1.VoiceClientImpl({ ari, config, tts, stt });
80
95
  });
@@ -36,6 +36,8 @@ declare enum ChannelVar {
36
36
  APP_ENDPOINT = "APP_ENDPOINT",
37
37
  METADATA = "METADATA",
38
38
  FROM_EXTERNAL_MEDIA = "FROM_EXTERNAL_MEDIA",
39
- CALL_REF = "CALL_REF"
39
+ CALL_REF = "CALL_REF",
40
+ AMD_STATUS = "AMDSTATUS",
41
+ AMD_CAUSE = "AMDCAUSE"
40
42
  }
41
43
  export { AriEvent, ChannelVar };
@@ -42,4 +42,7 @@ var ChannelVar;
42
42
  ChannelVar["METADATA"] = "METADATA";
43
43
  ChannelVar["FROM_EXTERNAL_MEDIA"] = "FROM_EXTERNAL_MEDIA";
44
44
  ChannelVar["CALL_REF"] = "CALL_REF";
45
+ // Set by app_amd in the dialplan, not by Fonoster; hence the odd names.
46
+ ChannelVar["AMD_STATUS"] = "AMDSTATUS";
47
+ ChannelVar["AMD_CAUSE"] = "AMDCAUSE";
45
48
  })(ChannelVar || (exports.ChannelVar = ChannelVar = {}));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fonoster/apiserver",
3
- "version": "0.22.14",
3
+ "version": "0.24.0",
4
4
  "description": "APIServer for Fonoster",
5
5
  "author": "Pedro Sanders <psanders@fonoster.com>",
6
6
  "homepage": "https://github.com/fonoster/fonoster#readme",
@@ -22,14 +22,14 @@
22
22
  },
23
23
  "dependencies": {
24
24
  "@deepgram/sdk": "^3.5.1",
25
- "@fonoster/authz": "^0.22.7",
26
- "@fonoster/autopilot": "^0.22.14",
27
- "@fonoster/common": "^0.22.3",
28
- "@fonoster/identity": "^0.22.7",
29
- "@fonoster/logger": "^0.22.0",
30
- "@fonoster/sipnet": "^0.22.7",
31
- "@fonoster/streams": "^0.22.13",
32
- "@fonoster/types": "^0.22.7",
25
+ "@fonoster/authz": "^0.24.0",
26
+ "@fonoster/autopilot": "^0.24.0",
27
+ "@fonoster/common": "^0.24.0",
28
+ "@fonoster/identity": "^0.24.0",
29
+ "@fonoster/logger": "^0.24.0",
30
+ "@fonoster/sipnet": "^0.24.0",
31
+ "@fonoster/streams": "^0.24.0",
32
+ "@fonoster/types": "^0.24.0",
33
33
  "@google-cloud/speech": "^6.6.0",
34
34
  "@google-cloud/text-to-speech": "^5.3.0",
35
35
  "@grpc/grpc-js": "~1.10.11",
@@ -76,5 +76,5 @@
76
76
  "@types/uuid": "^10.0.0",
77
77
  "@types/validator": "^13.15.10"
78
78
  },
79
- "gitHead": "25916a2a734e3e04dbc6c9b116df5e78bfc31ddb"
79
+ "gitHead": "36670cb30dbc0604029b2d49a59153c53f69fe1b"
80
80
  }