@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
@@ -0,0 +1,103 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.recordedUsageSubject = exports.RECORDED_USAGE_MAX_AGE_MS = exports.RECORDED_USAGE_SUBJECTS = exports.RECORDED_USAGE_STREAM = void 0;
13
+ exports.ensureRecordedUsageStream = ensureRecordedUsageStream;
14
+ exports.publishRecordedUsageOnce = publishRecordedUsageOnce;
15
+ exports.runRecordedUsagePublisher = runRecordedUsagePublisher;
16
+ const logger_1 = require("@fonoster/logger");
17
+ const nats_1 = require("nats");
18
+ const logger = (0, logger_1.getLogger)({ service: "apiserver", filePath: __filename });
19
+ const sc = (0, nats_1.StringCodec)();
20
+ /**
21
+ * Stored usage for billing (add-billing-service D2). Not under `usage.>`, which FONOSTER_USAGE
22
+ * owns: these are facts already stored, not usage to store.
23
+ */
24
+ exports.RECORDED_USAGE_STREAM = "FONOSTER_USAGE_RECORDED";
25
+ exports.RECORDED_USAGE_SUBJECTS = "billing.usage.>";
26
+ /** Kept this long while billing is not consuming. */
27
+ exports.RECORDED_USAGE_MAX_AGE_MS = 7 * 24 * 3600000;
28
+ /** The subject an owner's stored usage is published on. */
29
+ const recordedUsageSubject = (ownerRef) => `billing.usage.${ownerRef}`;
30
+ exports.recordedUsageSubject = recordedUsageSubject;
31
+ const toRecorded = (r) => (Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign({ id: r.id, ownerRef: r.owner_ref, workspaceRef: r.workspace_ref }, (r.app_ref ? { appRef: r.app_ref } : {})), (r.call_ref ? { callRef: r.call_ref } : {})), { region: r.region, regionKind: r.region_kind, origin: r.origin, meter: r.meter, quantity: Number(r.quantity) }), (r.provider ? { provider: r.provider } : {})), (r.model ? { model: r.model } : {})), (r.key_source ? { keySource: r.key_source } : {})), { occurredAt: r.occurred_at.toISOString() }));
32
+ const inUse = (e) => { var _a; return /already in use|already exists/i.test((_a = e.message) !== null && _a !== void 0 ? _a : ""); };
33
+ /** Creates the stream billing consumes, if it does not exist. */
34
+ function ensureRecordedUsageStream(nc) {
35
+ return __awaiter(this, void 0, void 0, function* () {
36
+ const jsm = yield nc.jetstreamManager();
37
+ try {
38
+ yield jsm.streams.info(exports.RECORDED_USAGE_STREAM);
39
+ }
40
+ catch (_a) {
41
+ yield jsm.streams
42
+ .add({
43
+ name: exports.RECORDED_USAGE_STREAM,
44
+ subjects: [exports.RECORDED_USAGE_SUBJECTS],
45
+ retention: nats_1.RetentionPolicy.Workqueue,
46
+ storage: nats_1.StorageType.File,
47
+ max_age: (0, nats_1.nanos)(exports.RECORDED_USAGE_MAX_AGE_MS)
48
+ })
49
+ .catch((e) => {
50
+ // Another replica created it at the same moment.
51
+ if (!inUse(e))
52
+ throw e;
53
+ });
54
+ }
55
+ });
56
+ }
57
+ /**
58
+ * Publishes one batch of unpublished usage, oldest first, each with its usage id as the message id
59
+ * (JetStream drops a repeat), then marks the batch published. A crash between the two only
60
+ * publishes the batch again. Returns how many were published.
61
+ */
62
+ function publishRecordedUsageOnce(nc_1, db_1) {
63
+ return __awaiter(this, arguments, void 0, function* (nc, db, batch = 500) {
64
+ const rows = yield db.$queryRawUnsafe(`SELECT id, owner_ref, workspace_ref, app_ref, call_ref, region, region_kind, origin, meter,
65
+ quantity, provider, model, key_source, occurred_at
66
+ FROM usage_events WHERE published_at IS NULL ORDER BY recorded_at, id LIMIT $1`, batch);
67
+ if (!rows.length)
68
+ return 0;
69
+ const js = nc.jetstream();
70
+ for (const row of rows)
71
+ yield js.publish((0, exports.recordedUsageSubject)(row.owner_ref), sc.encode(JSON.stringify(toRecorded(row))), { msgID: row.id });
72
+ yield db.$executeRawUnsafe(`UPDATE usage_events SET published_at = now() WHERE id = ANY($1::varchar[])`, rows.map((r) => r.id));
73
+ return rows.length;
74
+ });
75
+ }
76
+ /**
77
+ * Runs the billing outbox (only when billing is enabled): publishes stored usage until none is
78
+ * left, then waits `intervalMs`. Errors are logged and retried. Returns a stop function.
79
+ */
80
+ function runRecordedUsagePublisher(deps) {
81
+ return __awaiter(this, void 0, void 0, function* () {
82
+ yield ensureRecordedUsageStream(deps.nc);
83
+ let stopped = false;
84
+ let timer;
85
+ const tick = () => __awaiter(this, void 0, void 0, function* () {
86
+ var _a;
87
+ try {
88
+ while (!stopped && (yield publishRecordedUsageOnce(deps.nc, deps.db)))
89
+ ;
90
+ }
91
+ catch (e) {
92
+ logger.warn(`usage not published for billing yet: ${e.message}`);
93
+ }
94
+ if (!stopped)
95
+ timer = setTimeout(tick, (_a = deps.intervalMs) !== null && _a !== void 0 ? _a : 1000);
96
+ });
97
+ void tick();
98
+ return () => {
99
+ stopped = true;
100
+ clearTimeout(timer);
101
+ };
102
+ });
103
+ }
@@ -0,0 +1,29 @@
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 { Usage } from "@fonoster/common";
20
+ import { NatsConnection } from "nats";
21
+ /** The apiserver's bus connection, once connected: usage outside a call is published on it. */
22
+ export declare function attachUsagePublisher(nc: NatsConnection): void;
23
+ /** Publishes usage events on `usage.<workspace>.<meter>`, where the USAGE stream keeps them (D5). */
24
+ export declare function publishUsage(events: Usage.UsageEvent[]): void;
25
+ /**
26
+ * An eval's model usage (origin eval). Evals run in the control plane, so they are attributed to
27
+ * the public default region; each line's id comes from the eval run and request.
28
+ */
29
+ export declare function reportEvalUsage(workspaceRef: string, lines: Usage.UsageLine[], scope: string): void;
@@ -0,0 +1,50 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.attachUsagePublisher = attachUsagePublisher;
4
+ exports.publishUsage = publishUsage;
5
+ exports.reportEvalUsage = reportEvalUsage;
6
+ /**
7
+ * Copyright (C) 2025 by Fonoster Inc (https://fonoster.com)
8
+ * http://github.com/fonoster/fonoster
9
+ *
10
+ * This file is part of Fonoster
11
+ *
12
+ * Licensed under the MIT License (the "License");
13
+ * you may not use this file except in compliance with
14
+ * the License. You may obtain a copy of the License at
15
+ *
16
+ * https://opensource.org/licenses/MIT
17
+ *
18
+ * Unless required by applicable law or agreed to in writing, software
19
+ * distributed under the License is distributed on an "AS IS" BASIS,
20
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21
+ * See the License for the specific language governing permissions and
22
+ * limitations under the License.
23
+ */
24
+ const common_1 = require("@fonoster/common");
25
+ const logger_1 = require("@fonoster/logger");
26
+ const nats_1 = require("nats");
27
+ const runUsageConsumer_1 = require("./runUsageConsumer");
28
+ const logger = (0, logger_1.getLogger)({ service: "apiserver", filePath: __filename });
29
+ const sc = (0, nats_1.StringCodec)();
30
+ let bus;
31
+ /** The apiserver's bus connection, once connected: usage outside a call is published on it. */
32
+ function attachUsagePublisher(nc) {
33
+ bus = nc;
34
+ }
35
+ /** Publishes usage events on `usage.<workspace>.<meter>`, where the USAGE stream keeps them (D5). */
36
+ function publishUsage(events) {
37
+ if (!bus) {
38
+ logger.warn(`${events.length} usage events dropped: no bus connection yet`);
39
+ return;
40
+ }
41
+ events.forEach((e) => bus.publish((0, runUsageConsumer_1.usageSubject)(e.workspaceRef, e.meter), sc.encode(JSON.stringify(e))));
42
+ }
43
+ /**
44
+ * An eval's model usage (origin eval). Evals run in the control plane, so they are attributed to
45
+ * the public default region; each line's id comes from the eval run and request.
46
+ */
47
+ function reportEvalUsage(workspaceRef, lines, scope) {
48
+ const occurredAt = new Date().toISOString();
49
+ publishUsage(lines.map((line) => (Object.assign(Object.assign({}, line), { id: common_1.Usage.usageId(scope, line), workspaceRef, region: "default", regionKind: "public", origin: "eval", occurredAt }))));
50
+ }
@@ -0,0 +1,48 @@
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 { RecordStore } from "@fonoster/callrecord";
20
+ import { UsageDb, UsageDirectory } from "./types";
21
+ /**
22
+ * Recomputes usage_daily for [fromDay, toDay] (UTC, inclusive) from usage_events alone, in one
23
+ * transaction (design D7). Sum meters add up; snapshot meters keep the latest reading of the day.
24
+ */
25
+ export declare function rebuildUsageDaily(db: UsageDb, fromDay: string, toDay: string): Promise<number>;
26
+ type IndexedCall = {
27
+ callRef: string;
28
+ location: string;
29
+ region: string;
30
+ status: string;
31
+ };
32
+ /**
33
+ * Re-reads the sealed records of calls that started in [fromDay, toDay] and stores any usage
34
+ * missing from usage_events (Risks: index messages lost while the apiserver was down). Records
35
+ * of private regions stay in their cell and cannot be read here. Returns how many lines were new.
36
+ */
37
+ export declare function recoverUsageFromRecords(deps: {
38
+ db: UsageDb & {
39
+ callRecord: {
40
+ findMany(args: Record<string, unknown>): Promise<IndexedCall[]>;
41
+ };
42
+ };
43
+ directory: UsageDirectory;
44
+ storeFor: (location: string) => RecordStore | undefined;
45
+ fromDay: string;
46
+ toDay: string;
47
+ }): Promise<number>;
48
+ export {};
@@ -0,0 +1,104 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.rebuildUsageDaily = rebuildUsageDaily;
13
+ exports.recoverUsageFromRecords = recoverUsageFromRecords;
14
+ /**
15
+ * Copyright (C) 2025 by Fonoster Inc (https://fonoster.com)
16
+ * http://github.com/fonoster/fonoster
17
+ *
18
+ * This file is part of Fonoster
19
+ *
20
+ * Licensed under the MIT License (the "License");
21
+ * you may not use this file except in compliance with
22
+ * the License. You may obtain a copy of the License at
23
+ *
24
+ * https://opensource.org/licenses/MIT
25
+ *
26
+ * Unless required by applicable law or agreed to in writing, software
27
+ * distributed under the License is distributed on an "AS IS" BASIS,
28
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
29
+ * See the License for the specific language governing permissions and
30
+ * limitations under the License.
31
+ */
32
+ const callrecord_1 = require("@fonoster/callrecord");
33
+ const common_1 = require("@fonoster/common");
34
+ const logger_1 = require("@fonoster/logger");
35
+ const createIndexRecord_1 = require("../records/createIndexRecord");
36
+ const ingestUsage_1 = require("./ingestUsage");
37
+ const logger = (0, logger_1.getLogger)({ service: "apiserver", filePath: __filename });
38
+ const SNAPSHOT_METERS = common_1.Usage.METER_NAMES.filter((m) => common_1.Usage.METERS[m].aggregation === "snapshot");
39
+ const KEY = `owner_ref, workspace_ref, (occurred_at AT TIME ZONE 'UTC')::date, meter,
40
+ COALESCE(provider, ''), COALESCE(model, ''), COALESCE(key_source, ''), origin, region_kind`;
41
+ /**
42
+ * Recomputes usage_daily for [fromDay, toDay] (UTC, inclusive) from usage_events alone, in one
43
+ * transaction (design D7). Sum meters add up; snapshot meters keep the latest reading of the day.
44
+ */
45
+ function rebuildUsageDaily(db, fromDay, toDay) {
46
+ return __awaiter(this, void 0, void 0, function* () {
47
+ return db.$transaction((tx) => __awaiter(this, void 0, void 0, function* () {
48
+ yield tx.$executeRawUnsafe("DELETE FROM usage_daily WHERE day BETWEEN $1::date AND $2::date", fromDay, toDay);
49
+ const range = `(occurred_at AT TIME ZONE 'UTC')::date BETWEEN $1::date AND $2::date`;
50
+ const sums = yield tx.$executeRawUnsafe(`INSERT INTO usage_daily (owner_ref, workspace_ref, day, meter, provider, model, key_source,
51
+ origin, region_kind, quantity)
52
+ SELECT ${KEY}, SUM(quantity) FROM usage_events
53
+ WHERE ${range} AND NOT (meter = ANY($3::text[]))
54
+ GROUP BY ${KEY}`, fromDay, toDay, SNAPSHOT_METERS);
55
+ const snapshots = yield tx.$executeRawUnsafe(`INSERT INTO usage_daily (owner_ref, workspace_ref, day, meter, provider, model, key_source,
56
+ origin, region_kind, quantity)
57
+ SELECT DISTINCT ON (${KEY}) ${KEY}, quantity FROM usage_events
58
+ WHERE ${range} AND meter = ANY($3::text[])
59
+ ORDER BY ${KEY}, occurred_at DESC, recorded_at DESC`, fromDay, toDay, SNAPSHOT_METERS);
60
+ return sums + snapshots;
61
+ }));
62
+ });
63
+ }
64
+ const vconOf = (signed) => JSON.parse(Buffer.from(signed.payload, "base64url").toString("utf8"));
65
+ /**
66
+ * Re-reads the sealed records of calls that started in [fromDay, toDay] and stores any usage
67
+ * missing from usage_events (Risks: index messages lost while the apiserver was down). Records
68
+ * of private regions stay in their cell and cannot be read here. Returns how many lines were new.
69
+ */
70
+ function recoverUsageFromRecords(deps) {
71
+ return __awaiter(this, void 0, void 0, function* () {
72
+ var _a;
73
+ const calls = yield deps.db.callRecord.findMany({
74
+ where: {
75
+ startedAt: {
76
+ gte: new Date(`${deps.fromDay}T00:00:00.000Z`),
77
+ lt: new Date(Date.parse(`${deps.toDay}T00:00:00.000Z`) + 86400000)
78
+ },
79
+ status: { not: "erased" }
80
+ },
81
+ select: { callRef: true, location: true, region: true, status: true }
82
+ });
83
+ let inserted = 0;
84
+ for (const call of calls) {
85
+ const store = deps.storeFor(call.location);
86
+ if (!store) {
87
+ logger.warn(`no store for ${call.callRef} (${call.location}); skipped`);
88
+ continue;
89
+ }
90
+ const sealed = yield store.getSealed(call.callRef).catch(() => undefined);
91
+ if (!sealed)
92
+ continue;
93
+ const entry = (0, callrecord_1.indexEntry)(call.callRef, sealed, vconOf(sealed.signed), {
94
+ region: call.region,
95
+ location: call.location
96
+ });
97
+ if (!entry.workspaceRef || !((_a = entry.usage) === null || _a === void 0 ? void 0 : _a.length))
98
+ continue;
99
+ const events = (0, createIndexRecord_1.usageEvents)(entry, entry.workspaceRef);
100
+ inserted += yield deps.db.$transaction((tx) => (0, ingestUsage_1.ingestUsageIn)(tx, deps.directory, events));
101
+ }
102
+ return inserted;
103
+ });
104
+ }
@@ -0,0 +1,22 @@
1
+ import { NatsConnection } from "nats";
2
+ import { UsageDb, UsageDirectory } from "./types";
3
+ /** Usage outside a call (evals, storage snapshots) arrives on `usage.<workspace>.<meter>` (D5). */
4
+ export declare const USAGE_SUBJECT = "usage.>";
5
+ export declare const USAGE_STREAM = "FONOSTER_USAGE";
6
+ export declare const USAGE_DURABLE = "apiserver-usage";
7
+ /** Kept this long while nobody can store it (the usage store or the apiserver is down). */
8
+ export declare const USAGE_MAX_AGE_MS: number;
9
+ /**
10
+ * Stores usage events published outside a call. A message is acknowledged once stored; while the
11
+ * database or Identity's directory is unavailable it is retried, never dropped, until it ages out
12
+ * of the stream after seven days. A message that is not usage is logged and acknowledged.
13
+ */
14
+ export declare function runUsageConsumer(deps: {
15
+ nc: NatsConnection;
16
+ db: UsageDb;
17
+ directory: () => UsageDirectory | undefined;
18
+ retryDelayMs?: number;
19
+ ackWaitMs?: number;
20
+ }): Promise<() => Promise<void>>;
21
+ /** The subject a usage event is published on. */
22
+ export declare const usageSubject: (workspaceRef: string, meter: string) => string;
@@ -0,0 +1,78 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.usageSubject = exports.USAGE_MAX_AGE_MS = exports.USAGE_DURABLE = exports.USAGE_STREAM = exports.USAGE_SUBJECT = void 0;
13
+ exports.runUsageConsumer = runUsageConsumer;
14
+ /**
15
+ * Copyright (C) 2025 by Fonoster Inc (https://fonoster.com)
16
+ * http://github.com/fonoster/fonoster
17
+ *
18
+ * This file is part of Fonoster
19
+ *
20
+ * Licensed under the MIT License (the "License");
21
+ * you may not use this file except in compliance with
22
+ * the License. You may obtain a copy of the License at
23
+ *
24
+ * https://opensource.org/licenses/MIT
25
+ *
26
+ * Unless required by applicable law or agreed to in writing, software
27
+ * distributed under the License is distributed on an "AS IS" BASIS,
28
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
29
+ * See the License for the specific language governing permissions and
30
+ * limitations under the License.
31
+ */
32
+ const logger_1 = require("@fonoster/logger");
33
+ const nats_1 = require("nats");
34
+ const durableConsumer_1 = require("../core/durableConsumer");
35
+ const ingestUsage_1 = require("./ingestUsage");
36
+ const logger = (0, logger_1.getLogger)({ service: "apiserver", filePath: __filename });
37
+ const sc = (0, nats_1.StringCodec)();
38
+ /** Usage outside a call (evals, storage snapshots) arrives on `usage.<workspace>.<meter>` (D5). */
39
+ exports.USAGE_SUBJECT = "usage.>";
40
+ exports.USAGE_STREAM = "FONOSTER_USAGE";
41
+ exports.USAGE_DURABLE = "apiserver-usage";
42
+ /** Kept this long while nobody can store it (the usage store or the apiserver is down). */
43
+ exports.USAGE_MAX_AGE_MS = 7 * 24 * 3600000;
44
+ /**
45
+ * Stores usage events published outside a call. A message is acknowledged once stored; while the
46
+ * database or Identity's directory is unavailable it is retried, never dropped, until it ages out
47
+ * of the stream after seven days. A message that is not usage is logged and acknowledged.
48
+ */
49
+ function runUsageConsumer(deps) {
50
+ return __awaiter(this, void 0, void 0, function* () {
51
+ var _a;
52
+ return (0, durableConsumer_1.consumeDurable)(deps.nc, {
53
+ stream: exports.USAGE_STREAM,
54
+ subject: exports.USAGE_SUBJECT,
55
+ durable: exports.USAGE_DURABLE,
56
+ maxAgeMs: exports.USAGE_MAX_AGE_MS,
57
+ maxDeliver: -1,
58
+ retryDelayMs: (_a = deps.retryDelayMs) !== null && _a !== void 0 ? _a : 10000,
59
+ ackWaitMs: deps.ackWaitMs
60
+ }, (data) => __awaiter(this, void 0, void 0, function* () {
61
+ let event;
62
+ try {
63
+ event = JSON.parse(sc.decode(data));
64
+ }
65
+ catch (_a) {
66
+ logger.warn("usage message is not JSON; dropped");
67
+ return;
68
+ }
69
+ const directory = deps.directory();
70
+ if (!directory)
71
+ throw new Error("workspace directory not attached yet");
72
+ yield deps.db.$transaction((tx) => (0, ingestUsage_1.ingestUsageIn)(tx, directory, [event]));
73
+ }), (e) => logger.warn(`usage not stored yet, will retry: ${e.message}`));
74
+ });
75
+ }
76
+ /** The subject a usage event is published on. */
77
+ const usageSubject = (workspaceRef, meter) => `usage.${workspaceRef}.${meter}`;
78
+ exports.usageSubject = usageSubject;
@@ -0,0 +1,14 @@
1
+ import { UsageDb, UsageDirectory } from "./types";
2
+ /**
3
+ * Records `storage.bytes` for one completed day (design D8): the bytes of call records each
4
+ * workspace held at the end of that day, from index metadata only, one line per workspace and
5
+ * region kind. The line's id is derived from the workspace and day, so running it again adds
6
+ * nothing. Returns how many lines were new.
7
+ */
8
+ export declare function snapshotStorage(db: UsageDb, directory: UsageDirectory, day: string): Promise<number>;
9
+ /** Snapshots yesterday (UTC) now and every hour; idempotent, so restarts and replicas are safe. */
10
+ export declare function runStorageSnapshots(deps: {
11
+ db: UsageDb;
12
+ directory: () => UsageDirectory | undefined;
13
+ intervalMs?: number;
14
+ }): () => void;
@@ -0,0 +1,95 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.snapshotStorage = snapshotStorage;
13
+ exports.runStorageSnapshots = runStorageSnapshots;
14
+ /**
15
+ * Copyright (C) 2025 by Fonoster Inc (https://fonoster.com)
16
+ * http://github.com/fonoster/fonoster
17
+ *
18
+ * This file is part of Fonoster
19
+ *
20
+ * Licensed under the MIT License (the "License");
21
+ * you may not use this file except in compliance with
22
+ * the License. You may obtain a copy of the License at
23
+ *
24
+ * https://opensource.org/licenses/MIT
25
+ *
26
+ * Unless required by applicable law or agreed to in writing, software
27
+ * distributed under the License is distributed on an "AS IS" BASIS,
28
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
29
+ * See the License for the specific language governing permissions and
30
+ * limitations under the License.
31
+ */
32
+ const common_1 = require("@fonoster/common");
33
+ const logger_1 = require("@fonoster/logger");
34
+ const types_1 = require("../regions/types");
35
+ const ingestUsage_1 = require("./ingestUsage");
36
+ const logger = (0, logger_1.getLogger)({ service: "apiserver", filePath: __filename });
37
+ const DAY_MS = 86400000;
38
+ const STORED_AT_END_OF_DAY = `
39
+ SELECT access_key_id AS "workspaceRef", region, SUM(size)::bigint AS bytes
40
+ FROM call_records
41
+ WHERE access_key_id IS NOT NULL
42
+ AND started_at < $1
43
+ AND (erased_at IS NULL OR erased_at >= $1)
44
+ GROUP BY access_key_id, region`;
45
+ /**
46
+ * Records `storage.bytes` for one completed day (design D8): the bytes of call records each
47
+ * workspace held at the end of that day, from index metadata only, one line per workspace and
48
+ * region kind. The line's id is derived from the workspace and day, so running it again adds
49
+ * nothing. Returns how many lines were new.
50
+ */
51
+ function snapshotStorage(db, directory, day) {
52
+ return __awaiter(this, void 0, void 0, function* () {
53
+ const end = new Date(Date.parse(`${day}T00:00:00.000Z`) + DAY_MS);
54
+ const rows = yield db.$queryRawUnsafe(STORED_AT_END_OF_DAY, end);
55
+ const totals = new Map();
56
+ rows.forEach((r) => {
57
+ var _a;
58
+ const regionKind = (0, types_1.isPrivateRegionRef)(r.region) ? "private" : "public";
59
+ const key = `${r.workspaceRef}|${regionKind}`;
60
+ const t = (_a = totals.get(key)) !== null && _a !== void 0 ? _a : {
61
+ workspaceRef: r.workspaceRef,
62
+ regionKind,
63
+ region: r.region,
64
+ bytes: 0
65
+ };
66
+ t.bytes += Number(r.bytes);
67
+ totals.set(key, t);
68
+ });
69
+ const line = { meter: "storage.bytes" };
70
+ const events = Array.from(totals.values())
71
+ .filter((t) => t.bytes > 0)
72
+ .map((t) => (Object.assign(Object.assign({}, line), { quantity: t.bytes, id: common_1.Usage.usageId(`${t.workspaceRef}|${day}|${t.regionKind}`, line), workspaceRef: t.workspaceRef, region: t.region, regionKind: t.regionKind, origin: "api", occurredAt: new Date(end.getTime() - 1).toISOString() })));
73
+ return db.$transaction((tx) => (0, ingestUsage_1.ingestUsageIn)(tx, directory, events));
74
+ });
75
+ }
76
+ /** Snapshots yesterday (UTC) now and every hour; idempotent, so restarts and replicas are safe. */
77
+ function runStorageSnapshots(deps) {
78
+ var _a;
79
+ const tick = () => {
80
+ const directory = deps.directory();
81
+ if (!directory)
82
+ return;
83
+ const yesterday = new Date(Date.now() - DAY_MS).toISOString().slice(0, 10);
84
+ snapshotStorage(deps.db, directory, yesterday)
85
+ .then((n) => n &&
86
+ logger.info(`storage usage recorded for ${yesterday} (${n} workspaces)`))
87
+ .catch((e) => logger.warn(`storage usage snapshot failed: ${e.message}`));
88
+ };
89
+ const first = setTimeout(tick, 60000);
90
+ const timer = setInterval(tick, (_a = deps.intervalMs) !== null && _a !== void 0 ? _a : 3600000);
91
+ return () => {
92
+ clearTimeout(first);
93
+ clearInterval(timer);
94
+ };
95
+ }
@@ -0,0 +1,36 @@
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
+ /** Who owns a workspace, from Identity (which has its own database). */
20
+ export type UsageDirectory = {
21
+ workspace(accessKeyId: string): Promise<{
22
+ ownerRef: string;
23
+ } | undefined>;
24
+ /** The access key ids of the workspaces a user owns. */
25
+ ownedBy?(ownerRef: string): Promise<{
26
+ accessKeyId: string;
27
+ }[]>;
28
+ };
29
+ /** The raw-SQL part of a Prisma client or transaction that the usage ledger uses. */
30
+ export type UsageSql = {
31
+ $queryRawUnsafe<T = unknown>(query: string, ...values: unknown[]): Promise<T>;
32
+ $executeRawUnsafe(query: string, ...values: unknown[]): Promise<number>;
33
+ };
34
+ export type UsageDb = UsageSql & {
35
+ $transaction<T>(fn: (tx: UsageSql) => Promise<T>): Promise<T>;
36
+ };
@@ -21,7 +21,7 @@ exports.MULTILINGUAL_MODELS = exports.DeepgramModel = exports.DEEPGRAM_MULTILING
21
21
  */
22
22
  // What is left of the apiserver's speech engines after the rebuild (cleanbreak CB-B4): the
23
23
  // config and credential schemas that application validation still checks. Speech runs in the
24
- // gateway now (add-provider-registry).
24
+ // appbridge now (add-provider-registry).
25
25
  var DeepgramModel;
26
26
  (function (DeepgramModel) {
27
27
  DeepgramModel["NOVA_3"] = "nova-3";
@@ -1,26 +1,49 @@
1
1
  import { Sessions, Webhooks } from "@fonoster/common";
2
- /** `call.ended`'s `endReason` when the appbridge that owned the call died mid-call. */
2
+ /** `call.ended`'s `endReason` when AppBridge that owned the call died mid-call. */
3
3
  export declare const APPBRIDGE_LOST_REASON = "appbridge-lost";
4
4
  /**
5
- * The `call.ended` the control plane sends for a call whose appbridge is gone. Its id depends on
6
- * the call only, so every replica that detects the loss builds the same event and the fan-out
5
+ * The `call.ended` the control plane sends for a call whose AppBridge is gone (the node ended it
6
+ * with cause `app_gone`). Its id depends on the call only, so every replica that detects the loss builds the same event and the fan-out
7
7
  * stores one delivery per endpoint (delivery ids depend on the event id): delivered once.
8
8
  */
9
9
  export declare function lostCallEnded(call: Record<string, unknown>, lastSeenAt: number): Webhooks.WebhookEvent | undefined;
10
+ /** How long after the node's `app_gone` an AppBridge's own end of the call may still show up. */
11
+ export declare const APP_GONE_GRACE_MS = 2000;
10
12
  /**
11
- * Finds calls whose appbridge died mid-call, from the session registry: an appbridge rewrites its
12
- * `gateways.<ref>` entry and each of its `calls.<callRef>` entries every SESSIONS_REFRESH_MS and
13
- * deletes a call's entry when the call ends. A call entry not rewritten for `staleMs` (the lease
14
- * TTL) whose appbridge's entry is not rewritten either is a lost call: `emit` gets its
15
- * `call.ended` (reason `appbridge-lost`) and the call is forgotten. Times are when this replica
16
- * saw each write, so a replica that just started waits a full `staleMs` before judging.
13
+ * How long a call entry is remembered after its last write: an AppBridge rewrites its entries
14
+ * every SESSIONS_REFRESH_MS, and the node reports `app_gone` well within this after its AppBridge
15
+ * stops (stream break at once, keepalive or media timeout within seconds).
17
16
  */
18
- export declare function createLostCallSweep(deps: {
17
+ export declare const LOST_CALL_FORGET_MS: number;
18
+ /**
19
+ * Calls whose AppBridge died mid-call (redesign-roomr 15.4, workspace-webhooks "AppBridge dies
20
+ * mid-call"). AppBridge writes `calls.<callRef>` (with the app and parties once the call reached
21
+ * an app) and deletes it when the call ends, right after it sent its own `call.ended`. When the
22
+ * Roomr node ends a call with cause `app_gone` (its control stream broke, or the app leg went
23
+ * silent), `onAppGone` waits `graceMs` for that delete and then, if the entry was not deleted (it
24
+ * is still there, or it expired with the AppBridge that kept it alive), `emit` gets the call's
25
+ * `call.ended` with `endReason` `appbridge-lost`. Its id depends on the call only, so the event is
26
+ * delivered once per endpoint across replicas; a call AppBridge ended itself gets none.
27
+ *
28
+ * Entries are remembered as this replica saw them on the registry watch (an entry that expires is
29
+ * not reported by the watch, so it stays here until `forgetMs` after its last write).
30
+ */
31
+ export declare function createLostCalls(deps: {
19
32
  emit: (event: Webhooks.WebhookEvent) => unknown;
20
- staleMs?: number;
33
+ graceMs?: number;
34
+ forgetMs?: number;
21
35
  now?: () => number;
22
36
  }): {
23
37
  onEntry: (e: Sessions.SessionEntry) => void;
24
- sweep: () => Webhooks.WebhookEvent<Record<string, unknown>>[];
38
+ /**
39
+ * The node ended `callRef` with cause `app_gone`: after `graceMs` (an AppBridge that ended the
40
+ * call itself deletes its entry by then) the call's `call.ended` (appbridge-lost) is sent.
41
+ * Resolves with the event, or undefined when there is none to send.
42
+ */
43
+ onAppGone(callRef: string): Promise<Webhooks.WebhookEvent | undefined>;
44
+ /** Drops what is remembered past `forgetMs` (also done as entries arrive). */
45
+ forget: () => void;
25
46
  size: () => number;
47
+ stop(): void;
26
48
  };
49
+ export type LostCalls = ReturnType<typeof createLostCalls>;