@fonoster/apiserver 0.30.0 → 0.31.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.
Files changed (159) hide show
  1. package/dist/applications/buildService.d.ts +1 -1
  2. package/dist/applications/buildService.js +4 -3
  3. package/dist/applications/createCreateApplication.js +6 -3
  4. package/dist/applications/createCreateTestToken.d.ts +3 -1
  5. package/dist/applications/createCreateTestToken.js +18 -13
  6. package/dist/applications/createGetFnUtil.d.ts +1 -1
  7. package/dist/applications/createUpdateApplication.js +6 -4
  8. package/dist/applications/evalKeys.d.ts +7 -1
  9. package/dist/applications/evalKeys.js +9 -3
  10. package/dist/applications/session/appModel.d.ts +28 -4
  11. package/dist/applications/session/appModel.js +74 -35
  12. package/dist/billing/buildService.d.ts +24 -0
  13. package/dist/billing/buildService.js +116 -0
  14. package/dist/{migration/cli.js → bin/usage.js} +46 -22
  15. package/dist/calls/buildService.d.ts +1 -20
  16. package/dist/calls/buildService.js +50 -10
  17. package/dist/calls/createCall.d.ts +16 -2
  18. package/dist/calls/createCall.js +29 -6
  19. package/dist/calls/createCallPublisher.d.ts +6 -4
  20. package/dist/calls/createCallPublisher.js +6 -5
  21. package/dist/calls/createTrackCall.d.ts +16 -13
  22. package/dist/calls/createTrackCall.js +78 -55
  23. package/dist/calls/getCall.d.ts +3 -3
  24. package/dist/calls/getCall.js +2 -3
  25. package/dist/calls/listCalls.d.ts +4 -4
  26. package/dist/calls/listCalls.js +2 -3
  27. package/dist/calls/types.d.ts +2 -1
  28. package/dist/capabilities/createGetCapabilities.d.ts +3 -0
  29. package/dist/capabilities/createGetCapabilities.js +2 -1
  30. package/dist/config/index.js +1 -3
  31. package/dist/config/schema.d.ts +106 -102
  32. package/dist/config/schema.js +39 -47
  33. package/dist/core/applyCatalog.d.ts +3 -3
  34. package/dist/core/applyCatalog.js +5 -4
  35. package/dist/core/regionHooks.d.ts +1 -0
  36. package/dist/core/runServices.js +25 -4
  37. package/dist/core/services.d.ts +25 -1
  38. package/dist/core/services.js +35 -11
  39. package/dist/core/testTokenConfig.js +2 -2
  40. package/dist/core/upsertRoomrPeer.d.ts +6 -0
  41. package/dist/core/{upsertMediaNodePeer.js → upsertRoomrPeer.js} +9 -9
  42. package/dist/envs.d.ts +30 -16
  43. package/dist/envs.js +49 -31
  44. package/dist/index.js +23 -22
  45. package/dist/outbound/createNodeDirectory.d.ts +35 -13
  46. package/dist/outbound/createNodeDirectory.js +54 -20
  47. package/dist/outbound/index.d.ts +1 -0
  48. package/dist/outbound/index.js +1 -0
  49. package/dist/outbound/placeOutboundCall.d.ts +53 -13
  50. package/dist/outbound/placeOutboundCall.js +135 -88
  51. package/dist/outbound/placementSweep.d.ts +56 -0
  52. package/dist/outbound/placementSweep.js +127 -0
  53. package/dist/outbound/runOutboundCalls.d.ts +18 -14
  54. package/dist/outbound/runOutboundCalls.js +46 -34
  55. package/dist/providers/createProvidersHandlers.d.ts +1 -10
  56. package/dist/providers/createProvidersHandlers.js +20 -25
  57. package/dist/providers/index.d.ts +2 -2
  58. package/dist/providers/index.js +2 -2
  59. package/dist/providers/registryUsage.d.ts +2 -2
  60. package/dist/providers/registryUsage.js +5 -5
  61. package/dist/records/createIndexRecord.d.ts +26 -3
  62. package/dist/records/createIndexRecord.js +37 -10
  63. package/dist/records/createRecordsRouter.js +4 -2
  64. package/dist/records/createSearchRecords.d.ts +4 -3
  65. package/dist/records/createSearchRecords.js +9 -21
  66. package/dist/records/maintenance.d.ts +1 -1
  67. package/dist/records/maintenance.js +1 -1
  68. package/dist/records/runCallRecords.d.ts +2 -0
  69. package/dist/records/runCallRecords.js +1 -1
  70. package/dist/records/types.d.ts +2 -0
  71. package/dist/regions/configSync.d.ts +5 -5
  72. package/dist/regions/configSync.js +9 -9
  73. package/dist/regions/createRegionsRouter.d.ts +6 -6
  74. package/dist/regions/createRegionsRouter.js +5 -5
  75. package/dist/regions/link.js +1 -1
  76. package/dist/regions/runRegions.d.ts +2 -0
  77. package/dist/regions/runRegions.js +4 -0
  78. package/dist/regions/service.js +4 -4
  79. package/dist/regions/sources.d.ts +1 -1
  80. package/dist/regions/sources.js +4 -4
  81. package/dist/regions/status.js +5 -4
  82. package/dist/regions/types.d.ts +2 -2
  83. package/dist/usage/buildService.d.ts +16 -0
  84. package/dist/{calls/influxdb.js → usage/buildService.js} +16 -9
  85. package/dist/{events/createPerCallCache.d.ts → usage/createGetUsage.d.ts} +7 -7
  86. package/dist/usage/createGetUsage.js +118 -0
  87. package/dist/usage/ingestUsage.d.ts +7 -0
  88. package/dist/usage/ingestUsage.js +87 -0
  89. package/dist/usage/publishRecordedUsage.d.ts +52 -0
  90. package/dist/usage/publishRecordedUsage.js +103 -0
  91. package/dist/usage/publishUsage.d.ts +29 -0
  92. package/dist/usage/publishUsage.js +50 -0
  93. package/dist/usage/rebuildUsage.d.ts +48 -0
  94. package/dist/usage/rebuildUsage.js +104 -0
  95. package/dist/usage/runUsageConsumer.d.ts +22 -0
  96. package/dist/usage/runUsageConsumer.js +78 -0
  97. package/dist/usage/snapshotStorage.d.ts +14 -0
  98. package/dist/usage/snapshotStorage.js +95 -0
  99. package/dist/usage/types.d.ts +36 -0
  100. package/dist/voice/stt/types.js +1 -1
  101. package/dist/webhooks/lostCalls.d.ts +35 -12
  102. package/dist/webhooks/lostCalls.js +86 -48
  103. package/dist/webhooks/nodeCallEvents.d.ts +73 -0
  104. package/dist/webhooks/nodeCallEvents.js +177 -0
  105. package/dist/webhooks/runWebhookFanout.d.ts +10 -4
  106. package/dist/webhooks/runWebhookFanout.js +41 -39
  107. package/package.json +17 -19
  108. package/dist/applications/migration/PrismaApplicationStore.d.ts +0 -55
  109. package/dist/applications/migration/PrismaApplicationStore.js +0 -92
  110. package/dist/applications/migration/convertLegacyApplication.d.ts +0 -11
  111. package/dist/applications/migration/convertLegacyApplication.js +0 -112
  112. package/dist/applications/migration/index.d.ts +0 -22
  113. package/dist/applications/migration/index.js +0 -38
  114. package/dist/applications/migration/migrateApplications.d.ts +0 -43
  115. package/dist/applications/migration/migrateApplications.js +0 -87
  116. package/dist/applications/migration/types.d.ts +0 -92
  117. package/dist/applications/migration/types.js +0 -20
  118. package/dist/calls/createFetchCalls.d.ts +0 -22
  119. package/dist/calls/createFetchCalls.js +0 -88
  120. package/dist/calls/createFetchSingleCall.d.ts +0 -22
  121. package/dist/calls/createFetchSingleCall.js +0 -54
  122. package/dist/calls/influxdb.d.ts +0 -2
  123. package/dist/core/upsertMediaNodePeer.d.ts +0 -6
  124. package/dist/events/callRefs.d.ts +0 -29
  125. package/dist/events/callRefs.js +0 -103
  126. package/dist/events/createCdrWriter.d.ts +0 -25
  127. package/dist/events/createCdrWriter.js +0 -95
  128. package/dist/events/createInfluxDbPub.d.ts +0 -10
  129. package/dist/events/createInfluxDbPub.js +0 -46
  130. package/dist/events/createPerCallCache.js +0 -46
  131. package/dist/events/mapCallDirectionToEnum.d.ts +0 -21
  132. package/dist/events/mapCallDirectionToEnum.js +0 -37
  133. package/dist/events/nats.d.ts +0 -44
  134. package/dist/events/nats.js +0 -93
  135. package/dist/events/transformEvent.d.ts +0 -4
  136. package/dist/events/transformEvent.js +0 -87
  137. package/dist/events/types.d.ts +0 -26
  138. package/dist/migration/argv.d.ts +0 -22
  139. package/dist/migration/argv.js +0 -42
  140. package/dist/migration/catalogMapping.d.ts +0 -30
  141. package/dist/migration/catalogMapping.js +0 -53
  142. package/dist/migration/index.d.ts +0 -27
  143. package/dist/migration/index.js +0 -43
  144. package/dist/migration/migrateAssistants.d.ts +0 -8
  145. package/dist/migration/migrateAssistants.js +0 -169
  146. package/dist/migration/migrateCredentials.d.ts +0 -4
  147. package/dist/migration/migrateCredentials.js +0 -107
  148. package/dist/migration/migrateIntegrations.d.ts +0 -32
  149. package/dist/migration/migrateIntegrations.js +0 -136
  150. package/dist/migration/migrateLegacy.d.ts +0 -41
  151. package/dist/migration/migrateLegacy.js +0 -76
  152. package/dist/migration/providerKeys.d.ts +0 -14
  153. package/dist/migration/providerKeys.js +0 -61
  154. package/dist/migration/seedCatalog.d.ts +0 -2
  155. package/dist/migration/seedCatalog.js +0 -160
  156. package/dist/migration/types.d.ts +0 -42
  157. package/dist/migration/types.js +0 -31
  158. /package/dist/{migration/cli.d.ts → bin/usage.d.ts} +0 -0
  159. /package/dist/{events → usage}/types.js +0 -0
@@ -20,16 +20,7 @@ import { GrpcErrorMessage } from "@fonoster/common";
20
20
  export type ProvidersDb = any;
21
21
  /** The Secret name a provider key is stored under. */
22
22
  export declare const providerKeySecretName: (providerId: string) => string;
23
- export type ProvidersOptions = {
24
- /**
25
- * The platform credentials (credentialRef names) the gateways are configured with. When set,
26
- * the catalog lists a provider (and its models and voices) only if its credential is here or
27
- * the workspace stored its own key for it (add-provider-registry 4.1, LO-B12). Unset: no
28
- * credential filtering (development).
29
- */
30
- platformCredentials?: string[];
31
- };
32
- export declare function createProvidersHandlers(db: ProvidersDb, options?: ProvidersOptions): {
23
+ export declare function createProvidersHandlers(db: ProvidersDb): {
33
24
  listCatalog: (call: {
34
25
  request: unknown;
35
26
  }, callback: (error?: GrpcErrorMessage, response?: unknown) => void) => Promise<void>;
@@ -42,7 +42,7 @@ const providerKeySecretName = (providerId) => `provider-key/${providerId}`;
42
42
  exports.providerKeySecretName = providerKeySecretName;
43
43
  const lifecycle = (state) => state.toLowerCase();
44
44
  const visible = { state: { not: "RETIRED" } };
45
- function createProvidersHandlers(db, options = {}) {
45
+ function createProvidersHandlers(db) {
46
46
  const logger = (0, logger_1.getLogger)({ service: "apiserver", filePath: __filename });
47
47
  const workspace = (call) => (0, common_1.getAccessKeyIdFromCall)(call);
48
48
  /** Providers, models and voices that are not retired; other workspaces' voices are hidden. */
@@ -50,7 +50,7 @@ function createProvidersHandlers(db, options = {}) {
50
50
  const accessKeyId = workspace(call);
51
51
  const { kind, type } = call.request;
52
52
  const want = (k) => !kind || kind === k;
53
- const [allProviders, models, voices, keys] = yield Promise.all([
53
+ const [allProviders, models, voices] = yield Promise.all([
54
54
  db.registryProvider.findMany({
55
55
  where: visible,
56
56
  orderBy: { id: "asc" }
@@ -67,22 +67,13 @@ function createProvidersHandlers(db, options = {}) {
67
67
  include: { models: { orderBy: { position: "asc" } } },
68
68
  orderBy: { id: "asc" }
69
69
  })
70
- : [],
71
- options.platformCredentials
72
- ? db.workspaceProviderKey.findMany({
73
- where: { accessKeyId },
74
- select: { providerId: true }
75
- })
76
70
  : []
77
71
  ]);
78
- // Only usable providers (4.1): an adapter in this version and, when the platform's
79
- // credentials are configured, one of them or the workspace's own key.
80
- const own = new Set(keys.map((k) => k.providerId));
72
+ // Every catalog provider with an adapter in this version (4.1). Whether its key works is the
73
+ // operator's config: the catalog is the list of vendors the platform offers.
74
+ const keyless = (p) => !p.credentialRef;
81
75
  const usable = new Set(allProviders
82
- .filter((p) => common_1.Registry.BUILT_IN_ADAPTERS.includes(p.adapter) &&
83
- (!options.platformCredentials ||
84
- options.platformCredentials.includes(p.credentialRef) ||
85
- own.has(p.id)))
76
+ .filter((p) => common_1.Registry.BUILT_IN_ADAPTERS.includes(p.adapter))
86
77
  .map((p) => p.id));
87
78
  const providers = want("provider")
88
79
  ? allProviders.filter((p) => usable.has(p.id))
@@ -91,48 +82,52 @@ function createProvidersHandlers(db, options = {}) {
91
82
  ...providers
92
83
  .filter((p) => !type || p.capabilities.includes(type))
93
84
  .map((p) => {
94
- var _a, _b;
85
+ var _a, _b, _c;
95
86
  return ({
96
87
  id: p.id,
97
88
  kind: "provider",
89
+ label: (_a = p.label) !== null && _a !== void 0 ? _a : "",
98
90
  state: lifecycle(p.state),
99
91
  providerId: p.id,
100
92
  capabilities: p.capabilities,
101
- replacement: (_a = p.replacement) !== null && _a !== void 0 ? _a : "",
102
- retireOn: (_b = p.retireOn) !== null && _b !== void 0 ? _b : ""
93
+ keyless: keyless(p),
94
+ replacement: (_b = p.replacement) !== null && _b !== void 0 ? _b : "",
95
+ retireOn: (_c = p.retireOn) !== null && _c !== void 0 ? _c : ""
103
96
  });
104
97
  }),
105
98
  ...models
106
99
  .filter((m) => usable.has(m.providerId))
107
100
  .map((m) => {
108
- var _a, _b;
101
+ var _a, _b, _c;
109
102
  return ({
110
103
  id: m.id,
111
104
  kind: "model",
105
+ label: (_a = m.label) !== null && _a !== void 0 ? _a : "",
112
106
  state: lifecycle(m.state),
113
107
  providerId: m.providerId,
114
108
  type: m.type,
115
109
  languages: m.languages,
116
110
  features: m.features,
117
- replacement: (_a = m.replacement) !== null && _a !== void 0 ? _a : "",
118
- retireOn: (_b = m.retireOn) !== null && _b !== void 0 ? _b : ""
111
+ replacement: (_b = m.replacement) !== null && _b !== void 0 ? _b : "",
112
+ retireOn: (_c = m.retireOn) !== null && _c !== void 0 ? _c : ""
119
113
  });
120
114
  }),
121
115
  ...voices
122
116
  .filter((v) => usable.has(v.providerId))
123
117
  .map((v) => {
124
- var _a, _b, _c;
118
+ var _a, _b, _c, _d;
125
119
  return ({
126
120
  id: v.id,
127
121
  kind: "voice",
122
+ label: (_a = v.label) !== null && _a !== void 0 ? _a : "",
128
123
  state: lifecycle(v.state),
129
124
  providerId: v.providerId,
130
125
  type: "tts",
131
126
  languages: v.language ? [v.language] : [],
132
127
  models: v.models.map((r) => r.modelId),
133
- gender: (_a = v.gender) !== null && _a !== void 0 ? _a : "",
134
- replacement: (_b = v.replacement) !== null && _b !== void 0 ? _b : "",
135
- retireOn: (_c = v.retireOn) !== null && _c !== void 0 ? _c : ""
128
+ gender: (_b = v.gender) !== null && _b !== void 0 ? _b : "",
129
+ replacement: (_c = v.replacement) !== null && _c !== void 0 ? _c : "",
130
+ retireOn: (_d = v.retireOn) !== null && _d !== void 0 ? _d : ""
136
131
  });
137
132
  })
138
133
  ];
@@ -16,8 +16,8 @@
16
16
  * See the License for the specific language governing permissions and
17
17
  * limitations under the License.
18
18
  */
19
- import { ProvidersDb, ProvidersOptions } from "./createProvidersHandlers";
20
- declare function buildProvidersService(db: ProvidersDb, options?: ProvidersOptions): {
19
+ import { ProvidersDb } from "./createProvidersHandlers";
20
+ declare function buildProvidersService(db: ProvidersDb): {
21
21
  definition: {
22
22
  serviceName: string;
23
23
  pckg: string;
@@ -34,7 +34,7 @@ exports.buildProvidersService = buildProvidersService;
34
34
  * limitations under the License.
35
35
  */
36
36
  const createProvidersHandlers_1 = require("./createProvidersHandlers");
37
- function buildProvidersService(db, options = {}) {
37
+ function buildProvidersService(db) {
38
38
  return {
39
39
  definition: {
40
40
  serviceName: "Providers",
@@ -42,7 +42,7 @@ function buildProvidersService(db, options = {}) {
42
42
  version: "v1beta2",
43
43
  proto: "providers.proto"
44
44
  },
45
- handlers: (0, createProvidersHandlers_1.createProvidersHandlers)(db, options)
45
+ handlers: (0, createProvidersHandlers_1.createProvidersHandlers)(db)
46
46
  };
47
47
  }
48
48
  __exportStar(require("./createProvidersHandlers"), exports);
@@ -15,10 +15,10 @@ export type CatalogUse = {
15
15
  };
16
16
  /** Counts one session's use of an id and, for deprecated or retired ids, updates the notice. */
17
17
  export declare function createRecordCatalogUse(db: ProvidersDb): (use: CatalogUse) => Promise<boolean>;
18
- /** Consumes the gateways' `registry.used` messages (one apiserver replica per message). */
18
+ /** Consumes the AppBridge replicas' `registry.used` messages (one apiserver replica per message). */
19
19
  export declare function subscribeRegistryUsage(nc: NatsConnection, db: ProvidersDb): () => void;
20
20
  export type WarningUse = {
21
- kind: "app" | "assistant";
21
+ kind: "app" | "agent";
22
22
  ref: string;
23
23
  name: string;
24
24
  /** The config field that names the id: voice, ttsModel, sttModel, llm. */
@@ -33,7 +33,7 @@ exports.listWarnings = listWarnings;
33
33
  */
34
34
  const logger_1 = require("@fonoster/logger");
35
35
  const nats_1 = require("nats");
36
- // Runtime catalog use (add-provider-registry, build log LO-B11). Gateways publish one
36
+ // Runtime catalog use (add-provider-registry, build log LO-B11). AppBridges publish one
37
37
  // `registry.used` message per session and catalog id; the control plane counts sessions per
38
38
  // workspace, id and day (for `fonoster-registry usage`) and keeps one runtime notice per workspace
39
39
  // and deprecated or retired id. `listWarnings` turns the catalog's state, the workspace's saved
@@ -108,7 +108,7 @@ function createRecordCatalogUse(db) {
108
108
  return true;
109
109
  });
110
110
  }
111
- /** Consumes the gateways' `registry.used` messages (one apiserver replica per message). */
111
+ /** Consumes the AppBridge replicas' `registry.used` messages (one apiserver replica per message). */
112
112
  function subscribeRegistryUsage(nc, db) {
113
113
  const record = createRecordCatalogUse(db);
114
114
  const sub = nc.subscribe(exports.REGISTRY_USED_SUBJECT, {
@@ -198,13 +198,13 @@ function listWarnings(db_1, accessKeyId_1) {
198
198
  };
199
199
  for (const app of apps) {
200
200
  const config = app.intelligence;
201
- const assistant = config === null || config === void 0 ? void 0 : config.config;
201
+ const agent = config === null || config === void 0 ? void 0 : config.config;
202
202
  const refs = [
203
203
  [app.speechVoice, "app", "voice"],
204
204
  [app.speechTtsModel, "app", "ttsModel"],
205
205
  [app.speechSttModel, "app", "sttModel"],
206
- [(_a = assistant === null || assistant === void 0 ? void 0 : assistant.languageModel) === null || _a === void 0 ? void 0 : _a.model, "assistant", "llm"],
207
- [(_b = assistant === null || assistant === void 0 ? void 0 : assistant.conversationSettings) === null || _b === void 0 ? void 0 : _b.voice, "assistant", "voice"]
206
+ [(_a = agent === null || agent === void 0 ? void 0 : agent.languageModel) === null || _a === void 0 ? void 0 : _a.model, "agent", "llm"],
207
+ [(_b = agent === null || agent === void 0 ? void 0 : agent.conversationSettings) === null || _b === void 0 ? void 0 : _b.voice, "agent", "voice"]
208
208
  ];
209
209
  for (const [id, kind, field] of refs) {
210
210
  if (!id)
@@ -1,19 +1,42 @@
1
1
  import { NatsConnection } from "nats";
2
+ import { UsageDirectory } from "../usage/types";
2
3
  import { RecordIndexEntry, RecordsDb } from "./types";
4
+ /** Where a call's usage lines go: owners come from Identity's workspaces once attached. */
5
+ export type IndexUsageDeps = {
6
+ directory: () => UsageDirectory | undefined;
7
+ };
8
+ /** The usage events of an index entry, attributed from the entry (design D1). */
9
+ export declare function usageEvents(entry: RecordIndexEntry, workspaceRef: string): {
10
+ callRef: string;
11
+ region: string;
12
+ regionKind: string;
13
+ origin: string;
14
+ occurredAt: string;
15
+ appRef?: string;
16
+ id: string;
17
+ workspaceRef: string;
18
+ meter: string;
19
+ quantity: number;
20
+ provider?: string;
21
+ model?: string;
22
+ keySource?: "platform" | "workspace";
23
+ }[];
3
24
  /**
4
25
  * Upserts a record's index entry. Entries arrive at least once and possibly out of order, so a
5
26
  * lower version never overwrites a higher one. An erased record keeps its row, without numbers.
27
+ * The call's usage lines are stored in the same transaction as the row (add-usage-metering D1).
6
28
  */
7
- export declare function createIndexRecord(db: RecordsDb): (entry: RecordIndexEntry) => Promise<boolean>;
8
- /** The work queue that captures the gateways' index entries and its durable consumer (ST-2). */
29
+ export declare function createIndexRecord(db: RecordsDb, usage?: IndexUsageDeps): (entry: RecordIndexEntry) => Promise<boolean>;
30
+ /** The work queue that captures the AppBridge replicas' index entries and its durable consumer (ST-2). */
9
31
  export declare const RECORD_INDEX_STREAM = "FONOSTER_RECORD_INDEX";
10
32
  export declare const RECORD_INDEX_DURABLE = "apiserver-call-records";
11
33
  /**
12
- * Consumes the gateways' index entries (one apiserver replica per entry) through a JetStream
34
+ * Consumes the AppBridge replicas' index entries (one apiserver replica per entry) through a JetStream
13
35
  * durable consumer: an entry is acknowledged once indexed, so one taken by a replica that dies is
14
36
  * indexed by another (indexing is an idempotent upsert). Without JetStream (or the permission),
15
37
  * a core NATS queue group, as before.
16
38
  */
17
39
  export declare function subscribeRecordIndex(nc: NatsConnection, db: RecordsDb, options?: {
18
40
  ackWaitMs?: number;
41
+ usage?: IndexUsageDeps;
19
42
  }): Promise<() => Promise<void> | void>;
@@ -10,6 +10,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
10
10
  };
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
12
  exports.RECORD_INDEX_DURABLE = exports.RECORD_INDEX_STREAM = void 0;
13
+ exports.usageEvents = usageEvents;
13
14
  exports.createIndexRecord = createIndexRecord;
14
15
  exports.subscribeRecordIndex = subscribeRecordIndex;
15
16
  /**
@@ -36,15 +37,27 @@ const logger_1 = require("@fonoster/logger");
36
37
  const nats_1 = require("nats");
37
38
  const durableConsumer_1 = require("../core/durableConsumer");
38
39
  const telemetry_1 = require("../core/telemetry");
40
+ const types_1 = require("../regions/types");
41
+ const ingestUsage_1 = require("../usage/ingestUsage");
39
42
  const logger = (0, logger_1.getLogger)({ service: "apiserver", filePath: __filename });
40
43
  const sc = (0, nats_1.StringCodec)();
44
+ /** The usage events of an index entry, attributed from the entry (design D1). */
45
+ function usageEvents(entry, workspaceRef) {
46
+ var _a;
47
+ const origin = entry.origin === "browser_test" ? "browser_test" : "call";
48
+ return ((_a = entry.usage) !== null && _a !== void 0 ? _a : []).map((line) => {
49
+ var _a;
50
+ return (Object.assign(Object.assign(Object.assign(Object.assign({}, line), { id: common_1.Usage.usageId(entry.callRef, line), workspaceRef }), (entry.appRef ? { appRef: entry.appRef } : {})), { callRef: entry.callRef, region: entry.region, regionKind: (0, types_1.isPrivateRegionRef)(entry.region) ? "private" : "public", origin, occurredAt: new Date((_a = entry.endedAt) !== null && _a !== void 0 ? _a : entry.startedAt).toISOString() }));
51
+ });
52
+ }
41
53
  /**
42
54
  * Upserts a record's index entry. Entries arrive at least once and possibly out of order, so a
43
55
  * lower version never overwrites a higher one. An erased record keeps its row, without numbers.
56
+ * The call's usage lines are stored in the same transaction as the row (add-usage-metering D1).
44
57
  */
45
- function createIndexRecord(db) {
58
+ function createIndexRecord(db, usage) {
46
59
  return (entry) => __awaiter(this, void 0, void 0, function* () {
47
- var _a, _b, _c, _d, _e, _f, _g;
60
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k;
48
61
  const existing = yield db.callRecord.findUnique({
49
62
  where: { callRef: entry.callRef }
50
63
  });
@@ -69,14 +82,15 @@ function createIndexRecord(db) {
69
82
  endReason: erased ? ((_e = existing === null || existing === void 0 ? void 0 : existing.endReason) !== null && _e !== void 0 ? _e : null) : entry.endReason,
70
83
  failureReason: entry.failureReason,
71
84
  sipStatus: entry.sipStatus,
85
+ amdStatus: erased ? null : ((_f = entry.amdStatus) !== null && _f !== void 0 ? _f : null),
72
86
  startedAt: erased && existing ? existing.startedAt : new Date(entry.startedAt),
73
87
  endedAt: erased
74
- ? ((_f = existing === null || existing === void 0 ? void 0 : existing.endedAt) !== null && _f !== void 0 ? _f : null)
88
+ ? ((_g = existing === null || existing === void 0 ? void 0 : existing.endedAt) !== null && _g !== void 0 ? _g : null)
75
89
  : entry.endedAt
76
90
  ? new Date(entry.endedAt)
77
91
  : null,
78
92
  durationSeconds: erased
79
- ? ((_g = existing === null || existing === void 0 ? void 0 : existing.durationSeconds) !== null && _g !== void 0 ? _g : 0)
93
+ ? ((_h = existing === null || existing === void 0 ? void 0 : existing.durationSeconds) !== null && _h !== void 0 ? _h : 0)
80
94
  : entry.durationSeconds,
81
95
  region: entry.region,
82
96
  location: entry.location,
@@ -85,26 +99,39 @@ function createIndexRecord(db) {
85
99
  parts: entry.parts,
86
100
  erasedAt: erased ? new Date() : null
87
101
  };
88
- yield db.callRecord.upsert({
102
+ const upsert = {
89
103
  where: { callRef: entry.callRef },
90
104
  create: Object.assign({ callRef: entry.callRef }, data),
91
105
  update: data
92
- });
106
+ };
107
+ const directory = usage === null || usage === void 0 ? void 0 : usage.directory();
108
+ const workspaceRef = data.accessKeyId;
109
+ if (!erased && directory && workspaceRef && ((_j = entry.usage) === null || _j === void 0 ? void 0 : _j.length)) {
110
+ const events = usageEvents(entry, workspaceRef);
111
+ yield db.$transaction((tx) => __awaiter(this, void 0, void 0, function* () {
112
+ yield tx.callRecord.upsert(upsert);
113
+ yield (0, ingestUsage_1.ingestUsageIn)(tx, directory, events);
114
+ }));
115
+ return true;
116
+ }
117
+ if (!erased && !directory && ((_k = entry.usage) === null || _k === void 0 ? void 0 : _k.length))
118
+ logger.warn(`usage for ${entry.callRef} not stored: no workspace directory yet`);
119
+ yield db.callRecord.upsert(upsert);
93
120
  return true;
94
121
  });
95
122
  }
96
- /** The work queue that captures the gateways' index entries and its durable consumer (ST-2). */
123
+ /** The work queue that captures the AppBridge replicas' index entries and its durable consumer (ST-2). */
97
124
  exports.RECORD_INDEX_STREAM = "FONOSTER_RECORD_INDEX";
98
125
  exports.RECORD_INDEX_DURABLE = "apiserver-call-records";
99
126
  /**
100
- * Consumes the gateways' index entries (one apiserver replica per entry) through a JetStream
127
+ * Consumes the AppBridge replicas' index entries (one apiserver replica per entry) through a JetStream
101
128
  * durable consumer: an entry is acknowledged once indexed, so one taken by a replica that dies is
102
129
  * indexed by another (indexing is an idempotent upsert). Without JetStream (or the permission),
103
130
  * a core NATS queue group, as before.
104
131
  */
105
132
  function subscribeRecordIndex(nc_1, db_1) {
106
133
  return __awaiter(this, arguments, void 0, function* (nc, db, options = {}) {
107
- const index = createIndexRecord(db);
134
+ const index = createIndexRecord(db, options.usage);
108
135
  const handle = (data, headers) => __awaiter(this, void 0, void 0, function* () {
109
136
  var _a;
110
137
  let entry;
@@ -114,7 +141,7 @@ function subscribeRecordIndex(nc_1, db_1) {
114
141
  catch (_b) {
115
142
  return;
116
143
  }
117
- // One span per entry, in the gateway's trace when it sent a traceparent header.
144
+ // One span per entry, in AppBridge's trace when it sent a traceparent header.
118
145
  const telemetry = (0, telemetry_1.apiserverTelemetry)();
119
146
  const span = telemetry.startSpan("apiserver.records.index", { [common_1.CALL_REF_ATTR]: (_a = entry.callRef) !== null && _a !== void 0 ? _a : "" }, (headers === null || headers === void 0 ? void 0 : headers.get(common_1.TRACEPARENT_HEADER)) || undefined);
120
147
  try {
@@ -85,7 +85,7 @@ function createRecordsRouter(deps) {
85
85
  router.use(express_1.default.json());
86
86
  router.use(auth);
87
87
  const view = (row) => {
88
- var _a, _b;
88
+ var _a, _b, _c;
89
89
  return ({
90
90
  callRef: row.callRef,
91
91
  appRef: row.appRef,
@@ -96,8 +96,9 @@ function createRecordsRouter(deps) {
96
96
  endReason: row.endReason,
97
97
  failureReason: row.failureReason,
98
98
  sipStatus: row.sipStatus,
99
+ amdStatus: (_a = row.amdStatus) !== null && _a !== void 0 ? _a : null,
99
100
  startedAt: row.startedAt.toISOString(),
100
- endedAt: (_b = (_a = row.endedAt) === null || _a === void 0 ? void 0 : _a.toISOString()) !== null && _b !== void 0 ? _b : null,
101
+ endedAt: (_c = (_b = row.endedAt) === null || _b === void 0 ? void 0 : _b.toISOString()) !== null && _c !== void 0 ? _c : null,
101
102
  durationSeconds: row.durationSeconds,
102
103
  region: row.region,
103
104
  version: row.version,
@@ -112,6 +113,7 @@ function createRecordsRouter(deps) {
112
113
  before: q.before,
113
114
  number: q.number,
114
115
  appRef: q.appRef,
116
+ direction: q.direction,
115
117
  status: q.status,
116
118
  pageSize: q.pageSize ? Number(q.pageSize) : undefined,
117
119
  pageToken: q.pageToken
@@ -26,6 +26,8 @@ export type RecordSearch = {
26
26
  from?: string;
27
27
  to?: string;
28
28
  appRef?: string;
29
+ /** inbound, outbound or internal. */
30
+ direction?: string;
29
31
  status?: string;
30
32
  pageSize?: number;
31
33
  /** The previous page's last startedAt (ms). */
@@ -33,7 +35,7 @@ export type RecordSearch = {
33
35
  };
34
36
  /**
35
37
  * Searches the index within one workspace (call-record "Fetch and search": time range, number,
36
- * app and status; records from other workspaces are never returned). Newest first.
38
+ * app, direction and status; records from other workspaces are never returned). Newest first.
37
39
  */
38
40
  export declare function createSearchRecords(db: RecordsDb): (accessKeyId: string, search: RecordSearch) => Promise<{
39
41
  items: CallRecordRow[];
@@ -42,8 +44,7 @@ export declare function createSearchRecords(db: RecordsDb): (accessKeyId: string
42
44
  /** A call detail record (the existing Calls API shape) from an index row. */
43
45
  export declare function toCallDetailRecord(row: CallRecordRow): CallDetailRecord;
44
46
  /**
45
- * ListCalls served from the call-record index (task 7.1). It replaces the InfluxDB CDR query
46
- * when `callRecords.listCallsFromIndex` is on; the InfluxDB path stays for now.
47
+ * ListCalls served from the call-record index (task 7.1).
47
48
  */
48
49
  export declare function createFetchCallsFromIndex(db: RecordsDb): (accessKeyId: string, request: ListCallsRequest) => Promise<ListCallsResponse>;
49
50
  /** GetCall from the index. */
@@ -34,7 +34,7 @@ exports.createFetchSingleCallFromIndex = createFetchSingleCallFromIndex;
34
34
  const types_1 = require("@fonoster/types");
35
35
  /**
36
36
  * Searches the index within one workspace (call-record "Fetch and search": time range, number,
37
- * app and status; records from other workspaces are never returned). Newest first.
37
+ * app, direction and status; records from other workspaces are never returned). Newest first.
38
38
  */
39
39
  function createSearchRecords(db) {
40
40
  return (accessKeyId, search) => __awaiter(this, void 0, void 0, function* () {
@@ -48,7 +48,7 @@ function createSearchRecords(db) {
48
48
  if (!startedAt.lt || token < startedAt.lt)
49
49
  startedAt.lt = token;
50
50
  }
51
- const where = Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign({ accessKeyId, erasedAt: null }, (Object.keys(startedAt).length ? { startedAt } : {})), (search.from ? { fromNumber: search.from } : {})), (search.to ? { toNumber: search.to } : {})), (search.appRef ? { appRef: search.appRef } : {})), (search.status ? { status: search.status } : {})), (search.number
51
+ const where = Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign({ accessKeyId, erasedAt: null }, (Object.keys(startedAt).length ? { startedAt } : {})), (search.from ? { fromNumber: search.from } : {})), (search.to ? { toNumber: search.to } : {})), (search.appRef ? { appRef: search.appRef } : {})), (search.direction ? { direction: search.direction } : {})), (search.status ? { status: search.status } : {})), (search.number
52
52
  ? {
53
53
  OR: [{ fromNumber: search.number }, { toNumber: search.number }]
54
54
  }
@@ -75,28 +75,16 @@ const FAILURE_STATUS = {
75
75
  };
76
76
  /** A call detail record (the existing Calls API shape) from an index row. */
77
77
  function toCallDetailRecord(row) {
78
- var _a, _b, _c, _d, _e, _f;
78
+ var _a, _b, _c, _d, _e, _f, _g;
79
79
  const status = row.status === "failed"
80
80
  ? ((_b = FAILURE_STATUS[(_a = row.failureReason) !== null && _a !== void 0 ? _a : ""]) !== null && _b !== void 0 ? _b : types_1.CallStatus.SERVICE_UNAVAILABLE)
81
81
  : types_1.CallStatus.NORMAL_CLEARING;
82
- // Times in epoch milliseconds, as the InfluxDB CDR path returns them.
83
- return {
84
- ref: row.callRef,
85
- accessKeyId: (_c = row.accessKeyId) !== null && _c !== void 0 ? _c : "",
86
- callId: row.callRef,
87
- direction: row.direction === "outbound"
82
+ // The Calls API carries times as epoch seconds (int32 on the wire; the SDK multiplies by 1000).
83
+ return Object.assign({ ref: row.callRef, accessKeyId: (_c = row.accessKeyId) !== null && _c !== void 0 ? _c : "", callId: row.callRef, direction: row.direction === "outbound"
88
84
  ? types_1.CallDirection.TO_PSTN
89
- : types_1.CallDirection.FROM_PSTN,
90
- type: row.direction === "outbound"
85
+ : types_1.CallDirection.FROM_PSTN, type: row.direction === "outbound"
91
86
  ? types_1.CallType.API_ORIGINATED
92
- : types_1.CallType.SIP_ORIGINATED,
93
- status,
94
- startedAt: row.startedAt.getTime(),
95
- endedAt: ((_d = row.endedAt) !== null && _d !== void 0 ? _d : row.startedAt).getTime(),
96
- from: (_e = row.fromNumber) !== null && _e !== void 0 ? _e : "",
97
- to: (_f = row.toNumber) !== null && _f !== void 0 ? _f : "",
98
- duration: row.durationSeconds
99
- };
87
+ : types_1.CallType.SIP_ORIGINATED, status, startedAt: Math.floor(row.startedAt.getTime() / 1000), endedAt: Math.floor(((_d = row.endedAt) !== null && _d !== void 0 ? _d : row.startedAt).getTime() / 1000), from: (_e = row.fromNumber) !== null && _e !== void 0 ? _e : "", to: (_f = row.toNumber) !== null && _f !== void 0 ? _f : "", duration: row.durationSeconds, appRef: (_g = row.appRef) !== null && _g !== void 0 ? _g : "" }, (row.amdStatus ? { amdStatus: row.amdStatus.toUpperCase() } : {}));
100
88
  }
101
89
  const STATUS_FILTER = {
102
90
  [types_1.CallStatus.NORMAL_CLEARING]: { status: "completed" },
@@ -104,8 +92,7 @@ const STATUS_FILTER = {
104
92
  [types_1.CallStatus.NO_ANSWER]: { status: "failed", failureReason: "no_answer" }
105
93
  };
106
94
  /**
107
- * ListCalls served from the call-record index (task 7.1). It replaces the InfluxDB CDR query
108
- * when `callRecords.listCallsFromIndex` is on; the InfluxDB path stays for now.
95
+ * ListCalls served from the call-record index (task 7.1).
109
96
  */
110
97
  function createFetchCallsFromIndex(db) {
111
98
  const search = createSearchRecords(db);
@@ -118,6 +105,7 @@ function createFetchCallsFromIndex(db) {
118
105
  before: request.before,
119
106
  from: request.from,
120
107
  to: request.to,
108
+ appRef: request.appRef,
121
109
  status: status === null || status === void 0 ? void 0 : status.status,
122
110
  pageSize: request.pageSize,
123
111
  pageToken: request.pageToken
@@ -2,7 +2,7 @@ import { NatsConnection } from "nats";
2
2
  import { AuditEntry, MaintenanceClient, RecordsDb } from "./types";
3
3
  /**
4
4
  * Retention and erasure run in the record's region (it holds the files and the signing key);
5
- * the control plane asks over NATS (`callrecord.<region>.maintenance`, served by the gateways).
5
+ * the control plane asks over NATS (`callrecord.<region>.maintenance`, served by the AppBridge replicas).
6
6
  */
7
7
  export declare function natsMaintenanceClient(nc: NatsConnection, timeoutMs?: number): MaintenanceClient;
8
8
  /** Writes audit entries (call-record: what was deleted, when, why and by whom). */
@@ -39,7 +39,7 @@ const logger = (0, logger_1.getLogger)({ service: "apiserver", filePath: __filen
39
39
  const sc = (0, nats_1.StringCodec)();
40
40
  /**
41
41
  * Retention and erasure run in the record's region (it holds the files and the signing key);
42
- * the control plane asks over NATS (`callrecord.<region>.maintenance`, served by the gateways).
42
+ * the control plane asks over NATS (`callrecord.<region>.maintenance`, served by the AppBridge replicas).
43
43
  */
44
44
  function natsMaintenanceClient(nc, timeoutMs = 30000) {
45
45
  return (region, request) => __awaiter(this, void 0, void 0, function* () {
@@ -19,6 +19,7 @@
19
19
  import { ObjectStoreConfig } from "@fonoster/callrecord";
20
20
  import { NatsTarget } from "@fonoster/common";
21
21
  import { Router } from "express";
22
+ import { IndexUsageDeps } from "./createIndexRecord";
22
23
  import { RecordsRouterDeps } from "./createRecordsRouter";
23
24
  import { RecordsDb } from "./types";
24
25
  export type CallRecordsConfig = {
@@ -38,6 +39,7 @@ export declare function runCallRecords(config: CallRecordsConfig, deps: {
38
39
  db: RecordsDb;
39
40
  relay?: RecordsRouterDeps["relay"];
40
41
  onConfigChange?: (accessKeyId: string) => void;
42
+ usage?: IndexUsageDeps;
41
43
  }): Promise<{
42
44
  routers: {
43
45
  path: string;
@@ -27,7 +27,7 @@ function runCallRecords(config, deps) {
27
27
  if (!config.enabled)
28
28
  return { routers: [] };
29
29
  const nc = yield (0, nats_1.connect)(Object.assign(Object.assign({}, (0, common_1.natsConnectOptions)(deps.natsUrl)), { maxReconnectAttempts: -1 }));
30
- yield (0, createIndexRecord_1.subscribeRecordIndex)(nc, deps.db);
30
+ yield (0, createIndexRecord_1.subscribeRecordIndex)(nc, deps.db, { usage: deps.usage });
31
31
  const maintenance = (0, maintenance_1.natsMaintenanceClient)(nc, config.maintenanceTimeoutMs);
32
32
  const retention = setInterval(() => void (0, maintenance_1.runRetention)(deps.db, maintenance)
33
33
  .then((done) => done.length &&
@@ -28,6 +28,8 @@ export type CallRecordRow = {
28
28
  endReason: string | null;
29
29
  failureReason: string | null;
30
30
  sipStatus: number | null;
31
+ /** Who answered, by AMD (human, voicemail, ivr, machine, unknown); null when it didn't run. */
32
+ amdStatus?: string | null;
31
33
  startedAt: Date;
32
34
  endedAt: Date | null;
33
35
  durationSeconds: number;
@@ -4,7 +4,7 @@ import { RegionRow, RegionsDb } from "./types";
4
4
  export type ConfigKinds = {
5
5
  numbers: unknown[];
6
6
  apps: Record<string, unknown>[];
7
- assistants: {
7
+ agents: {
8
8
  appRef: string;
9
9
  productRef?: string;
10
10
  config: unknown;
@@ -23,7 +23,7 @@ export type ConfigKinds = {
23
23
  export type SnapshotSources = {
24
24
  apps(accessKeyId: string): Promise<{
25
25
  apps: Record<string, unknown>[];
26
- assistants: ConfigKinds["assistants"];
26
+ agents: ConfigKinds["agents"];
27
27
  }>;
28
28
  numbers(accessKeyId: string): Promise<unknown[]>;
29
29
  routr(accessKeyId: string): Promise<ConfigKinds["routr"]>;
@@ -32,16 +32,16 @@ export type SnapshotSources = {
32
32
  webhooks(accessKeyId: string): Promise<unknown[]>;
33
33
  recordingPolicy(accessKeyId: string): Promise<Record<string, unknown>>;
34
34
  };
35
- /** The gateway's defaults when an app names no voice or STT model. */
35
+ /** AppBridge's defaults when an app names no voice or STT model. */
36
36
  export declare const DEFAULT_SPEECH: {
37
37
  voice: string;
38
38
  sttModel: string;
39
39
  };
40
40
  /**
41
- * The catalog ids each app and assistant uses, and the secret names (provider ids) they need,
41
+ * The catalog ids each app and agent uses, and the secret names (provider ids) they need,
42
42
  * with who needs them (PR-4: names only, never values).
43
43
  */
44
- export declare function catalogUse(apps: Record<string, unknown>[], assistants: ConfigKinds["assistants"]): {
44
+ export declare function catalogUse(apps: Record<string, unknown>[], agents: ConfigKinds["agents"]): {
45
45
  ids: string[];
46
46
  secrets: {
47
47
  name: string;