@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
@@ -34,7 +34,7 @@ exports.createConfigSync = createConfigSync;
34
34
  const logger_1 = require("@fonoster/logger");
35
35
  const types_1 = require("./types");
36
36
  const logger = (0, logger_1.getLogger)({ service: "apiserver", filePath: __filename });
37
- /** The gateway's defaults when an app names no voice or STT model. */
37
+ /** AppBridge's defaults when an app names no voice or STT model. */
38
38
  exports.DEFAULT_SPEECH = {
39
39
  voice: "deepgram/aura-2-thalia-en",
40
40
  sttModel: "deepgram/nova-3"
@@ -43,10 +43,10 @@ const providerOf = (catalogId) => typeof catalogId === "string" && catalogId.inc
43
43
  ? catalogId.split("/")[0]
44
44
  : undefined;
45
45
  /**
46
- * The catalog ids each app and assistant uses, and the secret names (provider ids) they need,
46
+ * The catalog ids each app and agent uses, and the secret names (provider ids) they need,
47
47
  * with who needs them (PR-4: names only, never values).
48
48
  */
49
- function catalogUse(apps, assistants) {
49
+ function catalogUse(apps, agents) {
50
50
  var _a, _b, _c, _d;
51
51
  const ids = new Set();
52
52
  const usedBy = new Map();
@@ -68,7 +68,7 @@ function catalogUse(apps, assistants) {
68
68
  if (app.speechTtsModel)
69
69
  use(app.speechTtsModel, name);
70
70
  }
71
- for (const a of assistants) {
71
+ for (const a of agents) {
72
72
  const app = apps.find((x) => x.ref === a.appRef);
73
73
  const name = String((_b = app === null || app === void 0 ? void 0 : app.name) !== null && _b !== void 0 ? _b : a.appRef);
74
74
  const config = ((_c = a.config) !== null && _c !== void 0 ? _c : {});
@@ -111,14 +111,14 @@ function createConfigSync(deps) {
111
111
  const staleMs = Math.max(2000, debounceMs * 4);
112
112
  const rootDomain = () => { var _a, _b; return (_b = (_a = deps.rootDomain) === null || _a === void 0 ? void 0 : _a.call(deps)) !== null && _b !== void 0 ? _b : ""; };
113
113
  const workspaceKinds = (accessKeyId) => __awaiter(this, void 0, void 0, function* () {
114
- const [{ apps, assistants }, numbers, routr, webhooks, recordingPolicy] = yield Promise.all([
114
+ const [{ apps, agents }, numbers, routr, webhooks, recordingPolicy] = yield Promise.all([
115
115
  sources.apps(accessKeyId),
116
116
  sources.numbers(accessKeyId),
117
117
  sources.routr(accessKeyId),
118
118
  sources.webhooks(accessKeyId),
119
119
  sources.recordingPolicy(accessKeyId)
120
120
  ]);
121
- return { numbers, apps, assistants, routr, webhooks, recordingPolicy };
121
+ return { numbers, apps, agents, routr, webhooks, recordingPolicy };
122
122
  });
123
123
  const snapshot = (row) => __awaiter(this, void 0, void 0, function* () {
124
124
  var _a, _b;
@@ -128,7 +128,7 @@ function createConfigSync(deps) {
128
128
  const providers = new Set();
129
129
  for (const ws of bound) {
130
130
  const kinds = yield workspaceKinds(ws.accessKeyId);
131
- const use = catalogUse(kinds.apps, kinds.assistants);
131
+ const use = catalogUse(kinds.apps, kinds.agents);
132
132
  use.ids.forEach((i) => ids.add(i));
133
133
  use.secrets.forEach((s) => providers.add(s.name));
134
134
  const first = (_a = kinds.routr.domains[0]) === null || _a === void 0 ? void 0 : _a.domainUri;
@@ -160,8 +160,8 @@ function createConfigSync(deps) {
160
160
  const perWorkspace = {};
161
161
  const usedBy = new Map();
162
162
  for (const ws of bound) {
163
- const { apps, assistants } = yield sources.apps(ws.accessKeyId);
164
- const secrets = catalogUse(apps, assistants).secrets;
163
+ const { apps, agents } = yield sources.apps(ws.accessKeyId);
164
+ const secrets = catalogUse(apps, agents).secrets;
165
165
  perWorkspace[ws.accessKeyId] = secrets.map((s) => s.name);
166
166
  for (const s of secrets) {
167
167
  if (!usedBy.has(s.name))
@@ -49,12 +49,12 @@ export declare function regionView(row: RegionRow, service: RegionService, optio
49
49
  lastSeenAt: string;
50
50
  enrolledAt: string;
51
51
  components: {
52
- mediaNodes: number;
53
- gateways: number;
52
+ roomrNodes: number;
53
+ appbridges: number;
54
54
  autopilot: boolean;
55
55
  };
56
- mediaNodes: number;
57
- gateways: number;
56
+ roomrNodes: number;
57
+ appbridges: number;
58
58
  activeCalls: number;
59
59
  configVersion: number;
60
60
  reportedConfigVersion: number;
@@ -102,8 +102,8 @@ export declare function configuredView(r: PublicRegion): {
102
102
  status: "connected";
103
103
  state: "connected";
104
104
  components: {
105
- mediaNodes: number;
106
- gateways: number;
105
+ roomrNodes: number;
106
+ appbridges: number;
107
107
  autopilot: boolean;
108
108
  };
109
109
  secretsNeeded: string[];
@@ -105,12 +105,12 @@ function regionView(row_1, service_1) {
105
105
  lastSeenAt,
106
106
  enrolledAt: (_k = row.enrolledAt) === null || _k === void 0 ? void 0 : _k.toISOString(),
107
107
  components: {
108
- mediaNodes: (_l = c.mediaNodes) !== null && _l !== void 0 ? _l : 0,
109
- gateways: (_m = c.gateways) !== null && _m !== void 0 ? _m : 0,
108
+ roomrNodes: (_l = c.roomrNodes) !== null && _l !== void 0 ? _l : 0,
109
+ appbridges: (_m = c.appbridges) !== null && _m !== void 0 ? _m : 0,
110
110
  autopilot: (_o = c.autopilot) !== null && _o !== void 0 ? _o : false
111
111
  },
112
- mediaNodes: (_p = c.mediaNodes) !== null && _p !== void 0 ? _p : 0,
113
- gateways: (_q = c.gateways) !== null && _q !== void 0 ? _q : 0,
112
+ roomrNodes: (_p = c.roomrNodes) !== null && _p !== void 0 ? _p : 0,
113
+ appbridges: (_q = c.appbridges) !== null && _q !== void 0 ? _q : 0,
114
114
  activeCalls: row.activeCalls,
115
115
  configVersion: row.configVersion,
116
116
  reportedConfigVersion: row.reportedConfig,
@@ -153,7 +153,7 @@ function configuredView(r) {
153
153
  default: !!r.default,
154
154
  status: "connected",
155
155
  state: "connected",
156
- components: { mediaNodes: 0, gateways: 0, autopilot: false },
156
+ components: { roomrNodes: 0, appbridges: 0, autopilot: false },
157
157
  secretsNeeded: [],
158
158
  secretsSet: [],
159
159
  secrets: [],
@@ -57,7 +57,7 @@ function workspaceKey(db, accessKeyId, providerId) {
57
57
  */
58
58
  function startRegionLink(deps) {
59
59
  const { nc, db, service, configSync } = deps;
60
- const index = (0, createIndexRecord_1.createIndexRecord)(db);
60
+ const index = (0, createIndexRecord_1.createIndexRecord)(db, { directory: () => service.directory });
61
61
  const enrolled = (msg) => __awaiter(this, void 0, void 0, function* () {
62
62
  const ref = msg.subject.split(".")[1];
63
63
  if (!types_1.REGION_REF.test(ref))
@@ -93,6 +93,8 @@ export declare function createWorkspaceRegionHooks(publicRegions: PublicRegion[]
93
93
  defaultRegion: string;
94
94
  attach(r: RegionsRuntime): void;
95
95
  attachDirectory(d: WorkspaceDirectory): void;
96
+ /** Identity's workspaces once attached (usage ingest resolves owners with it). */
97
+ directory(): WorkspaceDirectory | undefined;
96
98
  /**
97
99
  * The `Workspace.region` value for what the caller picked: an offered public region id, or
98
100
  * one of the caller's private regions (id or ref). Throws RegionError 400 otherwise.
@@ -198,6 +198,10 @@ function createWorkspaceRegionHooks(publicRegions) {
198
198
  directory = d;
199
199
  connect();
200
200
  },
201
+ /** Identity's workspaces once attached (usage ingest resolves owners with it). */
202
+ directory() {
203
+ return directory;
204
+ },
201
205
  /**
202
206
  * The `Workspace.region` value for what the caller picked: an offered public region id, or
203
207
  * one of the caller's private regions (id or ref). Throws RegionError 400 otherwise.
@@ -386,8 +386,8 @@ function createRegionService(deps) {
386
386
  }
387
387
  });
388
388
  const components = {
389
- mediaNodes: Number((_c = (_b = body.components) === null || _b === void 0 ? void 0 : _b.mediaNodes) !== null && _c !== void 0 ? _c : 0),
390
- gateways: Number((_e = (_d = body.components) === null || _d === void 0 ? void 0 : _d.gateways) !== null && _e !== void 0 ? _e : 0),
389
+ roomrNodes: Number((_c = (_b = body.components) === null || _b === void 0 ? void 0 : _b.roomrNodes) !== null && _c !== void 0 ? _c : 0),
390
+ appbridges: Number((_e = (_d = body.components) === null || _d === void 0 ? void 0 : _d.appbridges) !== null && _e !== void 0 ? _e : 0),
391
391
  autopilot: Boolean((_f = body.components) === null || _f === void 0 ? void 0 : _f.autopilot)
392
392
  };
393
393
  const updated = yield db.region.update({
@@ -431,8 +431,8 @@ function createRegionService(deps) {
431
431
  data: {
432
432
  version: hb.version ? String(hb.version) : row.version,
433
433
  components: {
434
- mediaNodes: Number((_b = c.mediaNodes) !== null && _b !== void 0 ? _b : 0),
435
- gateways: Number((_c = c.gateways) !== null && _c !== void 0 ? _c : 0),
434
+ roomrNodes: Number((_b = c.roomrNodes) !== null && _b !== void 0 ? _b : 0),
435
+ appbridges: Number((_c = c.appbridges) !== null && _c !== void 0 ? _c : 0),
436
436
  autopilot: Boolean(c.autopilot)
437
437
  },
438
438
  activeCalls: Number((_d = hb.activeCalls) !== null && _d !== void 0 ? _d : 0),
@@ -7,7 +7,7 @@ type Handler = (call: unknown, callback: (err: unknown, res?: {
7
7
  /** Every item of a workspace from a gRPC list handler (the sipnet services filter by workspace). */
8
8
  export declare function listAll(handler: Handler | undefined, accessKeyId: string): Promise<unknown[]>;
9
9
  /**
10
- * The production snapshot sources: Prisma for apps, assistants, webhooks, the catalog and the
10
+ * The production snapshot sources: Prisma for apps, agents, webhooks, the catalog and the
11
11
  * recording policy; the Routr-backed gRPC handlers for numbers and Routr resources.
12
12
  */
13
13
  export declare function createSnapshotSources(deps: {
@@ -84,7 +84,7 @@ function listAll(handler, accessKeyId) {
84
84
  });
85
85
  }
86
86
  const iso = (d) => (d instanceof Date ? d.toISOString() : d);
87
- /** An application row as the cell's gateway reads it (the gateway's `appFromRow` columns). */
87
+ /** An application row as the cell's AppBridge reads it (AppBridge's `appFromRow` columns). */
88
88
  function appRow(row) {
89
89
  const out = {};
90
90
  for (const [k, v] of Object.entries(row)) {
@@ -95,7 +95,7 @@ function appRow(row) {
95
95
  return out;
96
96
  }
97
97
  /**
98
- * The production snapshot sources: Prisma for apps, assistants, webhooks, the catalog and the
98
+ * The production snapshot sources: Prisma for apps, agents, webhooks, the catalog and the
99
99
  * recording policy; the Routr-backed gRPC handlers for numbers and Routr resources.
100
100
  */
101
101
  function createSnapshotSources(deps) {
@@ -108,7 +108,7 @@ function createSnapshotSources(deps) {
108
108
  include: { intelligence: true },
109
109
  orderBy: { createdAt: "asc" }
110
110
  });
111
- const assistants = rows
111
+ const agents = rows
112
112
  .filter((r) => r.intelligence)
113
113
  .map((r) => {
114
114
  const i = r.intelligence;
@@ -118,7 +118,7 @@ function createSnapshotSources(deps) {
118
118
  config: i.config
119
119
  };
120
120
  });
121
- return { apps: rows.map(appRow), assistants };
121
+ return { apps: rows.map(appRow), agents };
122
122
  });
123
123
  },
124
124
  numbers: (accessKeyId) => listAll(handlers.listNumbers, accessKeyId),
@@ -50,10 +50,11 @@ function regionStatus(row, options) {
50
50
  }
51
51
  const c = ((_e = row.components) !== null && _e !== void 0 ? _e : {});
52
52
  const down = [];
53
- if (!c.mediaNodes)
54
- down.push("media nodes");
55
- if (!c.gateways)
56
- down.push("gateways");
53
+ if (!c.roomrNodes)
54
+ down.push("Roomr nodes");
55
+ // `appbridges` is 1 when AppBridge passes its health check through its Service, 0 when not.
56
+ if (!c.appbridges)
57
+ down.push("AppBridge");
57
58
  if (c.autopilot === false)
58
59
  down.push("autopilot");
59
60
  if (down.length)
@@ -42,8 +42,8 @@ export declare const subjects: (ref: string) => {
42
42
  inbox: string;
43
43
  };
44
44
  export type Components = {
45
- mediaNodes: number;
46
- gateways: number;
45
+ roomrNodes: number;
46
+ appbridges: number;
47
47
  autopilot: boolean;
48
48
  };
49
49
  export type Heartbeat = {
@@ -0,0 +1,16 @@
1
+ import { UsageSql } from "./types";
2
+ /** The Usage gRPC service (usage.proto): what a workspace consumed, never money. */
3
+ declare function buildService(db: UsageSql): {
4
+ definition: {
5
+ serviceName: string;
6
+ pckg: string;
7
+ version: string;
8
+ proto: string;
9
+ };
10
+ handlers: {
11
+ getUsage: (call: {
12
+ request: unknown;
13
+ }, callback: (error?: import("@fonoster/common").GrpcErrorMessage, response?: unknown) => void) => Promise<void>;
14
+ };
15
+ };
16
+ export { buildService };
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.influxdb = void 0;
3
+ exports.buildService = buildService;
4
4
  /**
5
5
  * Copyright (C) 2025 by Fonoster Inc (https://fonoster.com)
6
6
  * http://github.com/fonoster/fonoster
@@ -19,11 +19,18 @@ exports.influxdb = void 0;
19
19
  * See the License for the specific language governing permissions and
20
20
  * limitations under the License.
21
21
  */
22
- const influxdb_client_1 = require("@influxdata/influxdb-client");
23
- const envs_1 = require("../envs");
24
- const client = new influxdb_client_1.InfluxDB({
25
- url: envs_1.INFLUXDB_URL,
26
- token: envs_1.INFLUXDB_TOKEN
27
- });
28
- const influxdb = client.getQueryApi(envs_1.INFLUXDB_ORG);
29
- exports.influxdb = influxdb;
22
+ const createGetUsage_1 = require("./createGetUsage");
23
+ /** The Usage gRPC service (usage.proto): what a workspace consumed, never money. */
24
+ function buildService(db) {
25
+ return {
26
+ definition: {
27
+ serviceName: "Usage",
28
+ pckg: "usage",
29
+ version: "v1beta2",
30
+ proto: "usage.proto"
31
+ },
32
+ handlers: {
33
+ getUsage: (0, createGetUsage_1.createGetUsage)(db)
34
+ }
35
+ };
36
+ }
@@ -16,12 +16,12 @@
16
16
  * See the License for the specific language governing permissions and
17
17
  * limitations under the License.
18
18
  */
19
+ import { GrpcErrorMessage } from "@fonoster/common";
20
+ import { UsageSql } from "./types";
19
21
  /**
20
- * Keeps a value stable across a call's lifetime (keyed by callId), pruned by
21
- * age rather than by guessing which event is "final" from its fields.
22
+ * Usage totals (usage-metering "Usage API"): quantities from usage_daily, never money. Each day is
23
+ * totalled first; across days counted meters add up and snapshot meters (storage) take the peak.
22
24
  */
23
- declare function createPerCallCache<T>(ttlMs: number): {
24
- get(key: string): T | undefined;
25
- set(key: string, value: T): void;
26
- };
27
- export { createPerCallCache };
25
+ export declare function createGetUsage(db: UsageSql): (call: {
26
+ request: unknown;
27
+ }, callback: (error?: import("@fonoster/common").GrpcErrorMessage, response?: unknown) => void) => Promise<void>;
@@ -0,0 +1,118 @@
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.createGetUsage = createGetUsage;
13
+ /**
14
+ * Copyright (C) 2025 by Fonoster Inc (https://fonoster.com)
15
+ * http://github.com/fonoster/fonoster
16
+ *
17
+ * This file is part of Fonoster
18
+ *
19
+ * Licensed under the MIT License (the "License");
20
+ * you may not use this file except in compliance with
21
+ * the License. You may obtain a copy of the License at
22
+ *
23
+ * https://opensource.org/licenses/MIT
24
+ *
25
+ * Unless required by applicable law or agreed to in writing, software
26
+ * distributed under the License is distributed on an "AS IS" BASIS,
27
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
28
+ * See the License for the specific language governing permissions and
29
+ * limitations under the License.
30
+ */
31
+ const common_1 = require("@fonoster/common");
32
+ const logger_1 = require("@fonoster/logger");
33
+ const types_1 = require("@fonoster/types");
34
+ const grpc_js_1 = require("@grpc/grpc-js");
35
+ const zod_1 = require("zod");
36
+ const logger = (0, logger_1.getLogger)({ service: "apiserver", filePath: __filename });
37
+ const DAY = /^\d{4}-\d{2}-\d{2}$/;
38
+ const MAX_DAYS = 400;
39
+ /** Dimension → its usage_daily column. Totals are always split by meter. */
40
+ const COLUMNS = {
41
+ day: "day",
42
+ workspace: "workspace_ref",
43
+ meter: "meter",
44
+ provider: "provider",
45
+ model: "model",
46
+ key_source: "key_source",
47
+ origin: "origin"
48
+ };
49
+ const schema = zod_1.z
50
+ .object({
51
+ fromDay: zod_1.z.string().regex(DAY, "fromDay must be YYYY-MM-DD"),
52
+ toDay: zod_1.z.string().regex(DAY, "toDay must be YYYY-MM-DD"),
53
+ groupBy: zod_1.z
54
+ .array(zod_1.z.enum(Object.keys(COLUMNS)))
55
+ .optional(),
56
+ allOwned: zod_1.z.boolean().optional()
57
+ })
58
+ .refine((r) => r.fromDay <= r.toDay, "fromDay must not be after toDay")
59
+ .refine((r) => (Date.parse(r.toDay) - Date.parse(r.fromDay)) / 86400000 < MAX_DAYS, `the range must be shorter than ${MAX_DAYS} days`);
60
+ const SNAPSHOT_METERS = common_1.Usage.METER_NAMES.filter((m) => common_1.Usage.METERS[m].aggregation === "snapshot");
61
+ /** The workspaces the caller may read: theirs, or (all_owned) every one their token says they own. */
62
+ function workspacesOf(call, allOwned) {
63
+ var _a;
64
+ const grpcCall = call;
65
+ if (!allOwned)
66
+ return [(0, common_1.getAccessKeyIdFromCall)(grpcCall)];
67
+ const token = (0, common_1.decodeToken)((0, common_1.getTokenFromCall)(grpcCall));
68
+ const owned = ((_a = token.access) !== null && _a !== void 0 ? _a : [])
69
+ .filter((a) => a.role === types_1.Role.WORKSPACE_OWNER)
70
+ .map((a) => a.accessKeyId);
71
+ if (!owned.length)
72
+ throw new common_1.GrpcError(grpc_js_1.status.PERMISSION_DENIED, "all_owned needs a workspace owner");
73
+ return owned;
74
+ }
75
+ /**
76
+ * Usage totals (usage-metering "Usage API"): quantities from usage_daily, never money. Each day is
77
+ * totalled first; across days counted meters add up and snapshot meters (storage) take the peak.
78
+ */
79
+ function createGetUsage(db) {
80
+ const fn = (call, callback) => __awaiter(this, void 0, void 0, function* () {
81
+ const { fromDay, toDay, groupBy = [], allOwned = false } = call.request;
82
+ const workspaces = workspacesOf(call, allOwned);
83
+ const dims = Array.from(new Set([...groupBy, "meter"]));
84
+ const others = dims.filter((d) => d !== "day").map((d) => COLUMNS[d]);
85
+ const perDay = ["day", ...others].join(", ");
86
+ const outer = dims.map((d) => COLUMNS[d]).join(", ");
87
+ logger.verbose("call to getUsage", { workspaces, fromDay, toDay, dims });
88
+ const rows = yield db.$queryRawUnsafe(`WITH per_day AS (
89
+ SELECT ${perDay}, SUM(quantity) AS q FROM usage_daily
90
+ WHERE workspace_ref = ANY($1::text[]) AND day BETWEEN $2::date AND $3::date
91
+ GROUP BY ${perDay}
92
+ )
93
+ SELECT ${outer},
94
+ CASE WHEN meter = ANY($4::text[]) THEN MAX(q) ELSE SUM(q) END AS quantity
95
+ FROM per_day GROUP BY ${outer} ORDER BY ${outer}`, workspaces, fromDay, toDay, SNAPSHOT_METERS);
96
+ const text = (v) => v instanceof Date
97
+ ? v.toISOString().slice(0, 10)
98
+ : v === undefined || v === null
99
+ ? ""
100
+ : String(v);
101
+ callback(null, {
102
+ rows: rows.map((r) => ({
103
+ day: text(r.day),
104
+ workspaceRef: text(r.workspace_ref),
105
+ meter: String(r.meter),
106
+ unit: common_1.Usage.isMeter(String(r.meter))
107
+ ? common_1.Usage.METERS[r.meter].unit
108
+ : "",
109
+ provider: text(r.provider),
110
+ model: text(r.model),
111
+ keySource: text(r.key_source),
112
+ origin: text(r.origin),
113
+ quantity: Number(r.quantity)
114
+ }))
115
+ });
116
+ });
117
+ return (0, common_1.withErrorHandlingAndValidation)(fn, schema);
118
+ }
@@ -0,0 +1,7 @@
1
+ import { UsageDirectory, UsageSql } from "./types";
2
+ /**
3
+ * Stores usage events in a transaction the caller owns (design D7): each event once, by its id,
4
+ * and the day's total grows only when the event was new, so a replay changes nothing. Events that
5
+ * fail validation are logged and skipped. Returns how many events were new.
6
+ */
7
+ export declare function ingestUsageIn(tx: UsageSql, directory: UsageDirectory, events: unknown[]): Promise<number>;
@@ -0,0 +1,87 @@
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.ingestUsageIn = ingestUsageIn;
13
+ /**
14
+ * Copyright (C) 2025 by Fonoster Inc (https://fonoster.com)
15
+ * http://github.com/fonoster/fonoster
16
+ *
17
+ * This file is part of Fonoster
18
+ *
19
+ * Licensed under the MIT License (the "License");
20
+ * you may not use this file except in compliance with
21
+ * the License. You may obtain a copy of the License at
22
+ *
23
+ * https://opensource.org/licenses/MIT
24
+ *
25
+ * Unless required by applicable law or agreed to in writing, software
26
+ * distributed under the License is distributed on an "AS IS" BASIS,
27
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
28
+ * See the License for the specific language governing permissions and
29
+ * limitations under the License.
30
+ */
31
+ const common_1 = require("@fonoster/common");
32
+ const logger_1 = require("@fonoster/logger");
33
+ const logger = (0, logger_1.getLogger)({ service: "apiserver", filePath: __filename });
34
+ const INSERT_EVENT = `
35
+ INSERT INTO usage_events (id, owner_ref, workspace_ref, app_ref, call_ref, region, region_kind,
36
+ origin, meter, unit, quantity, provider, model, key_source, occurred_at)
37
+ VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15)
38
+ ON CONFLICT (id) DO NOTHING
39
+ RETURNING id`;
40
+ const upsertDaily = (aggregation) => `
41
+ INSERT INTO usage_daily (owner_ref, workspace_ref, day, meter, provider, model, key_source,
42
+ origin, region_kind, quantity, updated_at)
43
+ VALUES ($1, $2, $3::date, $4, $5, $6, $7, $8, $9, $10, now())
44
+ ON CONFLICT (owner_ref, workspace_ref, day, meter, provider, model, key_source, origin, region_kind)
45
+ DO UPDATE SET quantity = ${aggregation === "sum"
46
+ ? "usage_daily.quantity + EXCLUDED.quantity"
47
+ : "EXCLUDED.quantity"}, updated_at = now()`;
48
+ /**
49
+ * Stores usage events in a transaction the caller owns (design D7): each event once, by its id,
50
+ * and the day's total grows only when the event was new, so a replay changes nothing. Events that
51
+ * fail validation are logged and skipped. Returns how many events were new.
52
+ */
53
+ function ingestUsageIn(tx, directory, events) {
54
+ return __awaiter(this, void 0, void 0, function* () {
55
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j;
56
+ const owners = new Map();
57
+ const ownerOf = (workspaceRef) => __awaiter(this, void 0, void 0, function* () {
58
+ var _a;
59
+ if (!owners.has(workspaceRef)) {
60
+ const ws = yield directory.workspace(workspaceRef).catch(() => undefined);
61
+ if (!ws)
62
+ logger.warn(`usage for unknown workspace ${workspaceRef}; owner left empty`);
63
+ owners.set(workspaceRef, (_a = ws === null || ws === void 0 ? void 0 : ws.ownerRef) !== null && _a !== void 0 ? _a : "");
64
+ }
65
+ return owners.get(workspaceRef);
66
+ });
67
+ let inserted = 0;
68
+ for (const input of events) {
69
+ let event;
70
+ try {
71
+ event = common_1.Usage.parseUsageEvent(input);
72
+ }
73
+ catch (e) {
74
+ logger.warn(`usage line rejected: ${e.message}`);
75
+ continue;
76
+ }
77
+ const meter = common_1.Usage.meterOf(event.meter);
78
+ const ownerRef = (_a = event.ownerRef) !== null && _a !== void 0 ? _a : (yield ownerOf(event.workspaceRef));
79
+ const rows = yield tx.$queryRawUnsafe(INSERT_EVENT, event.id, ownerRef, event.workspaceRef, (_b = event.appRef) !== null && _b !== void 0 ? _b : null, (_c = event.callRef) !== null && _c !== void 0 ? _c : null, event.region, event.regionKind, event.origin, event.meter, meter.unit, BigInt(Math.round(event.quantity)), (_d = event.provider) !== null && _d !== void 0 ? _d : null, (_e = event.model) !== null && _e !== void 0 ? _e : null, (_f = event.keySource) !== null && _f !== void 0 ? _f : null, new Date(event.occurredAt));
80
+ if (rows.length === 0)
81
+ continue;
82
+ inserted++;
83
+ yield tx.$executeRawUnsafe(upsertDaily(meter.aggregation), ownerRef, event.workspaceRef, event.occurredAt.slice(0, 10), event.meter, (_g = event.provider) !== null && _g !== void 0 ? _g : "", (_h = event.model) !== null && _h !== void 0 ? _h : "", (_j = event.keySource) !== null && _j !== void 0 ? _j : "", event.origin, event.regionKind, BigInt(Math.round(event.quantity)));
84
+ }
85
+ return inserted;
86
+ });
87
+ }
@@ -0,0 +1,52 @@
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
+ import { UsageSql } from "./types";
22
+ /**
23
+ * Stored usage for billing (add-billing-service D2). Not under `usage.>`, which FONOSTER_USAGE
24
+ * owns: these are facts already stored, not usage to store.
25
+ */
26
+ export declare const RECORDED_USAGE_STREAM = "FONOSTER_USAGE_RECORDED";
27
+ export declare const RECORDED_USAGE_SUBJECTS = "billing.usage.>";
28
+ /** Kept this long while billing is not consuming. */
29
+ export declare const RECORDED_USAGE_MAX_AGE_MS: number;
30
+ /** The subject an owner's stored usage is published on. */
31
+ export declare const recordedUsageSubject: (ownerRef: string) => string;
32
+ /** A stored usage event as billing receives it: the usage.v1 event plus its owner. */
33
+ export type RecordedUsage = Usage.UsageEvent & {
34
+ ownerRef: string;
35
+ };
36
+ /** Creates the stream billing consumes, if it does not exist. */
37
+ export declare function ensureRecordedUsageStream(nc: NatsConnection): Promise<void>;
38
+ /**
39
+ * Publishes one batch of unpublished usage, oldest first, each with its usage id as the message id
40
+ * (JetStream drops a repeat), then marks the batch published. A crash between the two only
41
+ * publishes the batch again. Returns how many were published.
42
+ */
43
+ export declare function publishRecordedUsageOnce(nc: NatsConnection, db: UsageSql, batch?: number): Promise<number>;
44
+ /**
45
+ * Runs the billing outbox (only when billing is enabled): publishes stored usage until none is
46
+ * left, then waits `intervalMs`. Errors are logged and retried. Returns a stop function.
47
+ */
48
+ export declare function runRecordedUsagePublisher(deps: {
49
+ nc: NatsConnection;
50
+ db: UsageSql;
51
+ intervalMs?: number;
52
+ }): Promise<() => void>;