@aeolus-fleet/trierarch-plugin 0.32.0 → 0.34.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 (84) hide show
  1. package/dist/adapters/fleet/operator-fleet.d.ts +2 -0
  2. package/dist/adapters/fleet/operator-fleet.js +50 -0
  3. package/dist/adapters/fleet/operator-fleet.js.map +1 -0
  4. package/dist/adapters/fleet/rest-fleet-door.js +2 -1
  5. package/dist/adapters/fleet/rest-fleet-door.js.map +1 -1
  6. package/dist/adapters/prisma/fleet-forgetter.d.ts +1 -1
  7. package/dist/adapters/prisma/fleet-forgetter.js +2 -2
  8. package/dist/adapters/prisma/fleet-forgetter.js.map +1 -1
  9. package/dist/adapters/prisma/generated/browser.d.ts +23 -0
  10. package/dist/adapters/prisma/generated/client.d.ts +23 -0
  11. package/dist/adapters/prisma/generated/commonInputTypes.d.ts +201 -0
  12. package/dist/adapters/prisma/generated/internal/class.d.ts +33 -0
  13. package/dist/adapters/prisma/generated/internal/class.js +4 -4
  14. package/dist/adapters/prisma/generated/internal/class.js.map +1 -1
  15. package/dist/adapters/prisma/generated/internal/prismaNamespace.d.ts +306 -1
  16. package/dist/adapters/prisma/generated/internal/prismaNamespace.js +54 -1
  17. package/dist/adapters/prisma/generated/internal/prismaNamespace.js.map +1 -1
  18. package/dist/adapters/prisma/generated/internal/prismaNamespaceBrowser.d.ts +56 -0
  19. package/dist/adapters/prisma/generated/internal/prismaNamespaceBrowser.js +54 -1
  20. package/dist/adapters/prisma/generated/internal/prismaNamespaceBrowser.js.map +1 -1
  21. package/dist/adapters/prisma/generated/models/SubscriptionReading.d.ts +1113 -0
  22. package/dist/adapters/prisma/generated/models/SubscriptionReading.js +2 -0
  23. package/dist/adapters/prisma/generated/models/SubscriptionReading.js.map +1 -0
  24. package/dist/adapters/prisma/generated/models/UsageDay.d.ts +1554 -0
  25. package/dist/adapters/prisma/generated/models/UsageDay.js +2 -0
  26. package/dist/adapters/prisma/generated/models/UsageDay.js.map +1 -0
  27. package/dist/adapters/prisma/generated/models/UsageHour.d.ts +1445 -0
  28. package/dist/adapters/prisma/generated/models/UsageHour.js +2 -0
  29. package/dist/adapters/prisma/generated/models/UsageHour.js.map +1 -0
  30. package/dist/adapters/prisma/generated/models.d.ts +3 -0
  31. package/dist/adapters/prisma/usage-history.d.ts +3 -0
  32. package/dist/adapters/prisma/usage-history.js +316 -0
  33. package/dist/adapters/prisma/usage-history.js.map +1 -0
  34. package/dist/adapters/trpc/router.d.ts +225 -1
  35. package/dist/adapters/trpc/router.js +185 -1
  36. package/dist/adapters/trpc/router.js.map +1 -1
  37. package/dist/app.js +16 -0
  38. package/dist/app.js.map +1 -1
  39. package/dist/core/connection/ports.d.ts +1 -0
  40. package/dist/core/machines/join-machine.js +2 -4
  41. package/dist/core/machines/join-machine.js.map +1 -1
  42. package/dist/core/machines/new-setup-line.d.ts +32 -0
  43. package/dist/core/machines/new-setup-line.js +45 -0
  44. package/dist/core/machines/new-setup-line.js.map +1 -0
  45. package/dist/core/machines/setup-line.d.ts +14 -0
  46. package/dist/core/machines/setup-line.js +13 -0
  47. package/dist/core/machines/setup-line.js.map +1 -0
  48. package/dist/core/operator/ports.d.ts +27 -2
  49. package/dist/core/shared/errors.d.ts +1 -1
  50. package/dist/core/shared/errors.js.map +1 -1
  51. package/dist/core/usage/ports.d.ts +19 -0
  52. package/dist/core/usage/ports.js +2 -0
  53. package/dist/core/usage/ports.js.map +1 -0
  54. package/dist/core/usage/read-subscription-windows.d.ts +35 -0
  55. package/dist/core/usage/read-subscription-windows.js +32 -0
  56. package/dist/core/usage/read-subscription-windows.js.map +1 -0
  57. package/dist/core/usage/read-token-usage.d.ts +61 -0
  58. package/dist/core/usage/read-token-usage.js +79 -0
  59. package/dist/core/usage/read-token-usage.js.map +1 -0
  60. package/dist/core/usage/read-usage-values.d.ts +36 -0
  61. package/dist/core/usage/read-usage-values.js +31 -0
  62. package/dist/core/usage/read-usage-values.js.map +1 -0
  63. package/dist/core/usage/ship-names.d.ts +19 -0
  64. package/dist/core/usage/ship-names.js +29 -0
  65. package/dist/core/usage/ship-names.js.map +1 -0
  66. package/dist/core/usage/subscription-windows.d.ts +28 -0
  67. package/dist/core/usage/subscription-windows.js +2 -0
  68. package/dist/core/usage/subscription-windows.js.map +1 -0
  69. package/dist/core/usage/token-usage.d.ts +61 -0
  70. package/dist/core/usage/token-usage.js +5 -0
  71. package/dist/core/usage/token-usage.js.map +1 -0
  72. package/dist/core/usage/usage-range.d.ts +12 -0
  73. package/dist/core/usage/usage-range.js +24 -0
  74. package/dist/core/usage/usage-range.js.map +1 -0
  75. package/dist/core/usage/usage-reading.d.ts +53 -0
  76. package/dist/core/usage/usage-reading.js +2 -0
  77. package/dist/core/usage/usage-reading.js.map +1 -0
  78. package/dist/core/usage/usage-values.d.ts +15 -0
  79. package/dist/core/usage/usage-values.js +2 -0
  80. package/dist/core/usage/usage-values.js.map +1 -0
  81. package/package.json +1 -1
  82. package/src/adapters/prisma/migrations/20261028120000_usage_history/migration.sql +57 -0
  83. package/src/adapters/prisma/migrations/20261028150000_usage_cost_per_kind/migration.sql +21 -0
  84. package/src/adapters/prisma/schema.prisma +79 -2
@@ -1,5 +1,5 @@
1
- import type { ConsoleSessionOutput, FleetId, ScopeName } from '@aeolus-fleet/common';
2
- import type { FleetRefusal } from '../connection/ports.js';
1
+ import type { ConsoleSessionOutput, FleetId, ScopeName, ShipId } from '@aeolus-fleet/common';
2
+ import type { FleetRefusal, IssuedPrompt } from '../connection/ports.js';
3
3
  import type { Result } from '../shared/result.js';
4
4
  /**
5
5
  * Outbound port: asks the fleet whether a forwarded console session cookie is
@@ -13,3 +13,28 @@ export interface ConsoleSessions {
13
13
  scopes: readonly ScopeName[];
14
14
  }, FleetRefusal>>;
15
15
  }
16
+ /**
17
+ * The operator's console session as the console's server forwarded it: its
18
+ * cookie and the console's Origin, which the fleet needs on a state-changing
19
+ * call made with the cookie. undefined when the request came with none, which
20
+ * the fleet then refuses.
21
+ */
22
+ export interface OperatorSession {
23
+ cookie: string;
24
+ origin: string | undefined;
25
+ }
26
+ /**
27
+ * Outbound port: the fleet's calls the trierarch plugin makes on behalf of
28
+ * the operator, with the operator's console session, so the operator's own
29
+ * scopes and reach decide (decision 0057), never the plugin's ship's.
30
+ */
31
+ export interface OperatorFleet {
32
+ /** Frees a crewed ship from its session: its crew token and secret stop working (`fleet.release`). */
33
+ release(session: OperatorSession, ship: {
34
+ shipId: ShipId;
35
+ }): Promise<Result<undefined, FleetRefusal>>;
36
+ /** A new starting prompt for a ship awaiting crew; its new secret stops any earlier one working (`fleet.getStartingPrompt`). */
37
+ getStartingPrompt(session: OperatorSession, ship: {
38
+ shipId: ShipId;
39
+ }): Promise<Result<IssuedPrompt, FleetRefusal>>;
40
+ }
@@ -1,6 +1,6 @@
1
1
  import { type Err } from './result.js';
2
2
  /** Every reason the trierarch plugin's core refuses a request. */
3
- export type DomainErrorKind = 'ALREADY_CONNECTED' | 'SECRET_REFUSED' | 'OTHER_FLEET' | 'MISSING_SCOPES' | 'NOT_THE_OPERATOR' | 'REQUEST_ID_USED' | 'NOT_CONNECTED' | 'NAME_TAKEN' | 'NEEDS_SCOPE' | 'FLEET_UNAVAILABLE';
3
+ export type DomainErrorKind = 'ALREADY_CONNECTED' | 'SECRET_REFUSED' | 'OTHER_FLEET' | 'MISSING_SCOPES' | 'NOT_THE_OPERATOR' | 'REQUEST_ID_USED' | 'NOT_CONNECTED' | 'NAME_TAKEN' | 'NEEDS_SCOPE' | 'FLEET_UNAVAILABLE' | 'NOT_A_MACHINE' | 'FLEET_REFUSED' | 'INVALID_RANGE';
4
4
  /** Why the core refused a request. Adapters map the kind to their own error shape; the message is safe to show. */
5
5
  export interface DomainError<K extends DomainErrorKind = DomainErrorKind> {
6
6
  readonly kind: K;
@@ -1 +1 @@
1
- {"version":3,"file":"errors.js","sourceRoot":"","sources":["../../../src/core/shared/errors.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAY,MAAM,aAAa,CAAC;AAqB5C,qDAAqD;AACrD,MAAM,UAAU,MAAM,CAA4B,IAAO,EAAE,OAAe;IACxE,OAAO,GAAG,CAAC,EAAE,IAAI,EAAE,OAAO,EAAE,CAAC,CAAC;AAChC,CAAC"}
1
+ {"version":3,"file":"errors.js","sourceRoot":"","sources":["../../../src/core/shared/errors.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAY,MAAM,aAAa,CAAC;AAwB5C,qDAAqD;AACrD,MAAM,UAAU,MAAM,CAA4B,IAAO,EAAE,OAAe;IACxE,OAAO,GAAG,CAAC,EAAE,IAAI,EAAE,OAAO,EAAE,CAAC,CAAC;AAChC,CAAC"}
@@ -0,0 +1,19 @@
1
+ import type { FleetId } from '@aeolus-fleet/common';
2
+ import type { SubscriptionWindows, WindowsQuery } from './subscription-windows.js';
3
+ import type { TokenUsage, TokenUsageQuery } from './token-usage.js';
4
+ import type { UsageReading } from './usage-reading.js';
5
+ import type { UsageValue, UsageValuesQuery } from './usage-values.js';
6
+ /**
7
+ * Outbound port: each fleet's usage history (0030, the usage design). Recording
8
+ * a reading keeps, per hour row, the newest reading of it, so a reading handed
9
+ * again or arriving late changes nothing; each subscription reading is kept once.
10
+ * Reading token usage adds up, splits, ranks and filters in the store itself,
11
+ * and so does reading a breakdown's values or the subscription windows, a page
12
+ * at a time.
13
+ */
14
+ export interface UsageHistory {
15
+ record(fleetId: FleetId, reading: UsageReading): Promise<void>;
16
+ readTokens(fleetId: FleetId, query: TokenUsageQuery): Promise<TokenUsage>;
17
+ readValues(fleetId: FleetId, query: UsageValuesQuery): Promise<UsageValue[]>;
18
+ readWindows(fleetId: FleetId, query: WindowsQuery): Promise<SubscriptionWindows[]>;
19
+ }
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=ports.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ports.js","sourceRoot":"","sources":["../../../src/core/usage/ports.ts"],"names":[],"mappings":""}
@@ -0,0 +1,35 @@
1
+ import type { FleetId } from '@aeolus-fleet/common';
2
+ import type { ConnectionStore, FleetDoor } from '../connection/ports.js';
3
+ import type { DomainError } from '../shared/errors.js';
4
+ import { type Result } from '../shared/result.js';
5
+ import type { UsageHistory } from './ports.js';
6
+ import type { SubscriptionWindows, WindowsChartKey, WindowsQuery } from './subscription-windows.js';
7
+ /** The machine and harness charts the Usage page shows at a time. */
8
+ export declare const WINDOWS_PAGE = 20;
9
+ export interface WindowsRequest {
10
+ readonly fleetId: FleetId;
11
+ readonly from: Date;
12
+ readonly to: Date;
13
+ readonly filters: WindowsQuery['filters'];
14
+ /** The last chart of the page before; null for the first page. */
15
+ readonly after: WindowsChartKey | null;
16
+ }
17
+ /** A page of charts, each machine named (null for one the fleet does not know), and where the next page starts; null when none is left. */
18
+ export interface WindowsPage {
19
+ readonly charts: readonly (SubscriptionWindows & {
20
+ readonly machineName: string | null;
21
+ })[];
22
+ readonly next: WindowsChartKey | null;
23
+ }
24
+ export type ReadSubscriptionWindows = (request: WindowsRequest) => Promise<Result<WindowsPage, DomainError<'INVALID_RANGE' | 'NOT_CONNECTED' | 'FLEET_UNAVAILABLE'>>>;
25
+ /**
26
+ * Use case: how full each harness's subscription windows were on each machine
27
+ * in a range (the usage design, usage.windows): per machine and harness, 20 a
28
+ * page, each window's readings as the trierarch took them. The range follows
29
+ * the rule of the token usage it is shown under.
30
+ */
31
+ export declare function createReadSubscriptionWindows(deps: {
32
+ usage: UsageHistory;
33
+ door: FleetDoor;
34
+ connections: ConnectionStore;
35
+ }): ReadSubscriptionWindows;
@@ -0,0 +1,32 @@
1
+ import { ok } from '../shared/result.js';
2
+ import { nameOfValue, readShipNames } from './ship-names.js';
3
+ import { usageGrain } from './usage-range.js';
4
+ /** The machine and harness charts the Usage page shows at a time. */
5
+ export const WINDOWS_PAGE = 20;
6
+ /**
7
+ * Use case: how full each harness's subscription windows were on each machine
8
+ * in a range (the usage design, usage.windows): per machine and harness, 20 a
9
+ * page, each window's readings as the trierarch took them. The range follows
10
+ * the rule of the token usage it is shown under.
11
+ */
12
+ export function createReadSubscriptionWindows(deps) {
13
+ return async (request) => {
14
+ const grain = usageGrain(request);
15
+ if (!grain.isOk) {
16
+ return grain;
17
+ }
18
+ // One more than a page tells whether another follows.
19
+ const charts = await deps.usage.readWindows(request.fleetId, { from: request.from, to: request.to, filters: request.filters, after: request.after, limit: WINDOWS_PAGE + 1 });
20
+ const names = await readShipNames(deps, { fleetId: request.fleetId, breakdown: 'machine' });
21
+ if (!names.isOk) {
22
+ return names;
23
+ }
24
+ const page = charts.slice(0, WINDOWS_PAGE);
25
+ const last = page.at(-1);
26
+ return ok({
27
+ charts: page.map((chart) => ({ ...chart, machineName: nameOfValue(chart.machineShipId, names.value) })),
28
+ next: charts.length > WINDOWS_PAGE && last ? { machineShipId: last.machineShipId, harness: last.harness } : null,
29
+ });
30
+ };
31
+ }
32
+ //# sourceMappingURL=read-subscription-windows.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"read-subscription-windows.js","sourceRoot":"","sources":["../../../src/core/usage/read-subscription-windows.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,EAAE,EAAe,MAAM,qBAAqB,CAAC;AAEtD,OAAO,EAAE,WAAW,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAC;AAE7D,OAAO,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC;AAE9C,qEAAqE;AACrE,MAAM,CAAC,MAAM,YAAY,GAAG,EAAE,CAAC;AAmB/B;;;;;GAKG;AACH,MAAM,UAAU,6BAA6B,CAAC,IAA4E;IACxH,OAAO,KAAK,EAAE,OAAO,EAAE,EAAE;QACvB,MAAM,KAAK,GAAG,UAAU,CAAC,OAAO,CAAC,CAAC;QAClC,IAAI,CAAC,KAAK,CAAC,IAAI,EAAE,CAAC;YAChB,OAAO,KAAK,CAAC;QACf,CAAC;QACD,sDAAsD;QACtD,MAAM,MAAM,GAAG,MAAM,IAAI,CAAC,KAAK,CAAC,WAAW,CAAC,OAAO,CAAC,OAAO,EAAE,EAAE,IAAI,EAAE,OAAO,CAAC,IAAI,EAAE,EAAE,EAAE,OAAO,CAAC,EAAE,EAAE,OAAO,EAAE,OAAO,CAAC,OAAO,EAAE,KAAK,EAAE,OAAO,CAAC,KAAK,EAAE,KAAK,EAAE,YAAY,GAAG,CAAC,EAAE,CAAC,CAAC;QAC9K,MAAM,KAAK,GAAG,MAAM,aAAa,CAAC,IAAI,EAAE,EAAE,OAAO,EAAE,OAAO,CAAC,OAAO,EAAE,SAAS,EAAE,SAAS,EAAE,CAAC,CAAC;QAC5F,IAAI,CAAC,KAAK,CAAC,IAAI,EAAE,CAAC;YAChB,OAAO,KAAK,CAAC;QACf,CAAC;QACD,MAAM,IAAI,GAAG,MAAM,CAAC,KAAK,CAAC,CAAC,EAAE,YAAY,CAAC,CAAC;QAC3C,MAAM,IAAI,GAAG,IAAI,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC;QACzB,OAAO,EAAE,CAAC;YACR,MAAM,EAAE,IAAI,CAAC,GAAG,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,CAAC,EAAE,GAAG,KAAK,EAAE,WAAW,EAAE,WAAW,CAAC,KAAK,CAAC,aAAa,EAAE,KAAK,CAAC,KAAK,CAAC,EAAE,CAAC,CAAC;YACvG,IAAI,EAAE,MAAM,CAAC,MAAM,GAAG,YAAY,IAAI,IAAI,CAAC,CAAC,CAAC,EAAE,aAAa,EAAE,IAAI,CAAC,aAAa,EAAE,OAAO,EAAE,IAAI,CAAC,OAAO,EAAE,CAAC,CAAC,CAAC,IAAI;SACjH,CAAC,CAAC;IACL,CAAC,CAAC;AACJ,CAAC"}
@@ -0,0 +1,61 @@
1
+ /**
2
+ * Use case: the fleet's token usage as the console's Usage page reads it (the
3
+ * usage design, "The read the console calls"): hours for a range up to 7 days,
4
+ * UTC days from their rollup past that. The history adds up, ranks and
5
+ * filters; this names each series and splits by token kind. Machines and ships
6
+ * are named from one read of the fleet's ships, retired ones included, and the
7
+ * fleet is read only when the split needs those names.
8
+ */
9
+ import type { FleetId } from '@aeolus-fleet/common';
10
+ import type { ConnectionStore, FleetDoor } from '../connection/ports.js';
11
+ import type { DomainError } from '../shared/errors.js';
12
+ import { type Result } from '../shared/result.js';
13
+ import type { UsageHistory } from './ports.js';
14
+ import type { UsageBreakdown, UsageFilters, UsageGrain, UsageMeasure, UsageSeriesKey } from './token-usage.js';
15
+ import type { TokenCounts } from './usage-reading.js';
16
+ /** The four kinds of tokens, in the order the page shows them. */
17
+ export declare const TOKEN_KINDS: readonly ["input", "output", "cacheRead", "cacheWrite"];
18
+ export type TokenKind = (typeof TOKEN_KINDS)[number];
19
+ export interface TokenUsageRequest {
20
+ readonly fleetId: FleetId;
21
+ readonly from: Date;
22
+ readonly to: Date;
23
+ /** None: one series for all of it. */
24
+ readonly splitBy: UsageBreakdown | 'kind' | null;
25
+ readonly measure: UsageMeasure;
26
+ readonly filters: UsageFilters;
27
+ }
28
+ /** A series' tokens per kind, with its estimated cost (of the priced tokens only) and its tokens not priced, each added up over the kinds. */
29
+ export interface SeriesAmounts {
30
+ readonly tokens: TokenCounts;
31
+ readonly estimatedCostUsd: number;
32
+ readonly unpricedTokens: number;
33
+ }
34
+ export type TokenUsageSeriesKey = UsageSeriesKey | {
35
+ readonly kind: 'tokenKind';
36
+ readonly tokenKind: TokenKind;
37
+ };
38
+ /** Token usage as the console shows it: each series named, with its total, and the range's totals. */
39
+ export interface TokenUsageView {
40
+ readonly grain: UsageGrain;
41
+ /** Largest first, other last. A name is null for other, all, a token kind, a login not reported and a ship the fleet does not know. */
42
+ readonly series: readonly {
43
+ readonly key: TokenUsageSeriesKey;
44
+ readonly name: string | null;
45
+ readonly amounts: SeriesAmounts;
46
+ }[];
47
+ readonly buckets: readonly {
48
+ readonly at: Date;
49
+ readonly amounts: readonly (SeriesAmounts & {
50
+ readonly series: number;
51
+ })[];
52
+ }[];
53
+ readonly totals: SeriesAmounts;
54
+ readonly unpricedModels: readonly string[];
55
+ }
56
+ export type ReadTokenUsage = (request: TokenUsageRequest) => Promise<Result<TokenUsageView, DomainError<'INVALID_RANGE' | 'NOT_CONNECTED' | 'FLEET_UNAVAILABLE'>>>;
57
+ export declare function createReadTokenUsage(deps: {
58
+ usage: UsageHistory;
59
+ door: FleetDoor;
60
+ connections: ConnectionStore;
61
+ }): ReadTokenUsage;
@@ -0,0 +1,79 @@
1
+ import { ok } from '../shared/result.js';
2
+ import { nameOfValue, readShipNames } from './ship-names.js';
3
+ import { usageGrain } from './usage-range.js';
4
+ /** The four kinds of tokens, in the order the page shows them. */
5
+ export const TOKEN_KINDS = ['input', 'output', 'cacheRead', 'cacheWrite'];
6
+ const NO_TOKENS = { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 };
7
+ export function createReadTokenUsage(deps) {
8
+ return async (request) => {
9
+ const grained = usageGrain(request);
10
+ if (!grained.isOk) {
11
+ return grained;
12
+ }
13
+ const grain = grained.value;
14
+ const isByKind = request.splitBy === 'kind';
15
+ const usage = await deps.usage.readTokens(request.fleetId, {
16
+ from: request.from,
17
+ to: request.to,
18
+ grain,
19
+ splitBy: isByKind ? null : request.splitBy,
20
+ measure: request.measure,
21
+ filters: request.filters,
22
+ });
23
+ const names = await readShipNames(deps, { fleetId: request.fleetId, breakdown: request.splitBy });
24
+ if (!names.isOk) {
25
+ return names;
26
+ }
27
+ const split = isByKind ? byTokenKind(usage) : unsplitByKind(usage);
28
+ return ok({
29
+ grain,
30
+ series: split.series.map((key, index) => ({ key, name: nameOf(key, names.value), amounts: addUp(split.buckets.flatMap((bucket) => bucket.amounts.filter((amount) => amount.series === index))) })),
31
+ buckets: split.buckets,
32
+ totals: addUp(usage.buckets.flatMap((bucket) => bucket.amounts.map(addedOverKinds))),
33
+ unpricedModels: usage.unpricedModels,
34
+ });
35
+ };
36
+ }
37
+ /** A series' name: its value's; other, all and a token kind have none. */
38
+ function nameOf(key, shipNames) {
39
+ return key.kind === 'value' ? nameOfValue(key.value, shipNames) : null;
40
+ }
41
+ /** The series as the history answers them, each amount's cost and tokens not priced added up over the kinds. */
42
+ function unsplitByKind(usage) {
43
+ return {
44
+ series: usage.series.map((key) => key),
45
+ buckets: usage.buckets.map((bucket) => ({ at: bucket.at, amounts: bucket.amounts.map((amount) => ({ series: amount.series, ...addedOverKinds(amount) })) })),
46
+ };
47
+ }
48
+ /** The one unsplit series as four, one per token kind, each holding only its kind's tokens, cost and tokens not priced. */
49
+ function byTokenKind(usage) {
50
+ return {
51
+ series: TOKEN_KINDS.map((tokenKind) => ({ kind: 'tokenKind', tokenKind })),
52
+ buckets: usage.buckets.map((bucket) => ({
53
+ at: bucket.at,
54
+ amounts: bucket.amounts.flatMap((amount) => TOKEN_KINDS.map((tokenKind, series) => ({
55
+ series,
56
+ tokens: { ...NO_TOKENS, [tokenKind]: amount.tokens[tokenKind] },
57
+ estimatedCostUsd: amount.estimatedCostUsd[tokenKind],
58
+ unpricedTokens: amount.unpricedTokens[tokenKind],
59
+ }))),
60
+ })),
61
+ };
62
+ }
63
+ function addedOverKinds(amount) {
64
+ const sum = (counts) => TOKEN_KINDS.reduce((total, kind) => total + counts[kind], 0);
65
+ return { tokens: amount.tokens, estimatedCostUsd: sum(amount.estimatedCostUsd), unpricedTokens: sum(amount.unpricedTokens) };
66
+ }
67
+ function addUp(amounts) {
68
+ return amounts.reduce((total, amount) => ({
69
+ tokens: {
70
+ input: total.tokens.input + amount.tokens.input,
71
+ output: total.tokens.output + amount.tokens.output,
72
+ cacheRead: total.tokens.cacheRead + amount.tokens.cacheRead,
73
+ cacheWrite: total.tokens.cacheWrite + amount.tokens.cacheWrite,
74
+ },
75
+ estimatedCostUsd: total.estimatedCostUsd + amount.estimatedCostUsd,
76
+ unpricedTokens: total.unpricedTokens + amount.unpricedTokens,
77
+ }), { tokens: NO_TOKENS, estimatedCostUsd: 0, unpricedTokens: 0 });
78
+ }
79
+ //# sourceMappingURL=read-token-usage.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file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} from '../connection/ports.js';
3
+ import type { DomainError } from '../shared/errors.js';
4
+ import { type Result } from '../shared/result.js';
5
+ import type { UsageHistory } from './ports.js';
6
+ import type { UsageBreakdown } from './token-usage.js';
7
+ import type { UsageValue } from './usage-values.js';
8
+ /** The values a filter menu shows at a time. */
9
+ export declare const USAGE_VALUES_PAGE = 20;
10
+ export interface UsageValuesRequest {
11
+ readonly fleetId: FleetId;
12
+ readonly from: Date;
13
+ readonly to: Date;
14
+ readonly breakdown: UsageBreakdown;
15
+ /** The last value of the page before; null for the first page. */
16
+ readonly after: UsageValue | null;
17
+ }
18
+ /** A page of values, each named as the token usage read names it, and where the next page starts; null when none is left. */
19
+ export interface UsageValuesPage {
20
+ readonly values: readonly (UsageValue & {
21
+ readonly name: string | null;
22
+ })[];
23
+ readonly next: UsageValue | null;
24
+ }
25
+ export type ReadUsageValues = (request: UsageValuesRequest) => Promise<Result<UsageValuesPage, DomainError<'INVALID_RANGE' | 'NOT_CONNECTED' | 'FLEET_UNAVAILABLE'>>>;
26
+ /**
27
+ * Use case: the values of one breakdown in a range, for the Usage page's
28
+ * filter menus (the usage design, usage.values): 20 a page, largest by tokens
29
+ * first, in the grain token usage is read in. Machines and ships are named
30
+ * from one read of the fleet's ships, retired ones included.
31
+ */
32
+ export declare function createReadUsageValues(deps: {
33
+ usage: UsageHistory;
34
+ door: FleetDoor;
35
+ connections: ConnectionStore;
36
+ }): ReadUsageValues;
@@ -0,0 +1,31 @@
1
+ import { ok } from '../shared/result.js';
2
+ import { nameOfValue, readShipNames } from './ship-names.js';
3
+ import { usageGrain } from './usage-range.js';
4
+ /** The values a filter menu shows at a time. */
5
+ export const USAGE_VALUES_PAGE = 20;
6
+ /**
7
+ * Use case: the values of one breakdown in a range, for the Usage page's
8
+ * filter menus (the usage design, usage.values): 20 a page, largest by tokens
9
+ * first, in the grain token usage is read in. Machines and ships are named
10
+ * from one read of the fleet's ships, retired ones included.
11
+ */
12
+ export function createReadUsageValues(deps) {
13
+ return async (request) => {
14
+ const grain = usageGrain(request);
15
+ if (!grain.isOk) {
16
+ return grain;
17
+ }
18
+ // One more than a page tells whether another follows.
19
+ const values = await deps.usage.readValues(request.fleetId, { from: request.from, to: request.to, grain: grain.value, breakdown: request.breakdown, after: request.after, limit: USAGE_VALUES_PAGE + 1 });
20
+ const page = values.slice(0, USAGE_VALUES_PAGE);
21
+ const names = await readShipNames(deps, { fleetId: request.fleetId, breakdown: request.breakdown });
22
+ if (!names.isOk) {
23
+ return names;
24
+ }
25
+ return ok({
26
+ values: page.map((value) => ({ ...value, name: nameOfValue(value.value, names.value) })),
27
+ next: values.length > USAGE_VALUES_PAGE ? (page.at(-1) ?? null) : null,
28
+ });
29
+ };
30
+ }
31
+ //# sourceMappingURL=read-usage-values.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"read-usage-values.js","sourceRoot":"","sources":["../../../src/core/usage/read-usage-values.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,EAAE,EAAe,MAAM,qBAAqB,CAAC;AAGtD,OAAO,EAAE,WAAW,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAC;AAC7D,OAAO,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC;AAG9C,gDAAgD;AAChD,MAAM,CAAC,MAAM,iBAAiB,GAAG,EAAE,CAAC;AAmBpC;;;;;GAKG;AACH,MAAM,UAAU,qBAAqB,CAAC,IAA4E;IAChH,OAAO,KAAK,EAAE,OAAO,EAAE,EAAE;QACvB,MAAM,KAAK,GAAG,UAAU,CAAC,OAAO,CAAC,CAAC;QAClC,IAAI,CAAC,KAAK,CAAC,IAAI,EAAE,CAAC;YAChB,OAAO,KAAK,CAAC;QACf,CAAC;QACD,sDAAsD;QACtD,MAAM,MAAM,GAAG,MAAM,IAAI,CAAC,KAAK,CAAC,UAAU,CAAC,OAAO,CAAC,OAAO,EAAE,EAAE,IAAI,EAAE,OAAO,CAAC,IAAI,EAAE,EAAE,EAAE,OAAO,CAAC,EAAE,EAAE,KAAK,EAAE,KAAK,CAAC,KAAK,EAAE,SAAS,EAAE,OAAO,CAAC,SAAS,EAAE,KAAK,EAAE,OAAO,CAAC,KAAK,EAAE,KAAK,EAAE,iBAAiB,GAAG,CAAC,EAAE,CAAC,CAAC;QAC1M,MAAM,IAAI,GAAG,MAAM,CAAC,KAAK,CAAC,CAAC,EAAE,iBAAiB,CAAC,CAAC;QAChD,MAAM,KAAK,GAAG,MAAM,aAAa,CAAC,IAAI,EAAE,EAAE,OAAO,EAAE,OAAO,CAAC,OAAO,EAAE,SAAS,EAAE,OAAO,CAAC,SAAS,EAAE,CAAC,CAAC;QACpG,IAAI,CAAC,KAAK,CAAC,IAAI,EAAE,CAAC;YAChB,OAAO,KAAK,CAAC;QACf,CAAC;QACD,OAAO,EAAE,CAAC;YACR,MAAM,EAAE,IAAI,CAAC,GAAG,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,CAAC,EAAE,GAAG,KAAK,EAAE,IAAI,EAAE,WAAW,CAAC,KAAK,CAAC,KAAK,EAAE,KAAK,CAAC,KAAK,CAAC,EAAE,CAAC,CAAC;YACxF,IAAI,EAAE,MAAM,CAAC,MAAM,GAAG,iBAAiB,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,IAAI,IAAI,CAAC,CAAC,CAAC,CAAC,IAAI;SACvE,CAAC,CAAC;IACL,CAAC,CAAC;AACJ,CAAC"}
@@ -0,0 +1,19 @@
1
+ import type { FleetId } from '@aeolus-fleet/common';
2
+ import type { ConnectionStore, FleetDoor } from '../connection/ports.js';
3
+ import { type DomainError } from '../shared/errors.js';
4
+ import { type Result } from '../shared/result.js';
5
+ import type { UsageBreakdown } from './token-usage.js';
6
+ /**
7
+ * The names of the fleet's ships by id, from one read of them, retired ones
8
+ * included, so usage of a ship released long ago keeps its name. Read only for
9
+ * a breakdown of machines or ships: other values are their own names.
10
+ */
11
+ export declare function readShipNames(deps: {
12
+ door: FleetDoor;
13
+ connections: ConnectionStore;
14
+ }, read: {
15
+ fleetId: FleetId;
16
+ breakdown: UsageBreakdown | 'kind' | null;
17
+ }): Promise<Result<ReadonlyMap<string, string> | undefined, DomainError<'NOT_CONNECTED' | 'FLEET_UNAVAILABLE'>>>;
18
+ /** A value's name: a machine or ship by its name in the fleet, any other value itself; a login not reported has none. */
19
+ export declare function nameOfValue(value: string | null, shipNames: ReadonlyMap<string, string> | undefined): string | null;
@@ -0,0 +1,29 @@
1
+ import { refuse } from '../shared/errors.js';
2
+ import { ok } from '../shared/result.js';
3
+ /**
4
+ * The names of the fleet's ships by id, from one read of them, retired ones
5
+ * included, so usage of a ship released long ago keeps its name. Read only for
6
+ * a breakdown of machines or ships: other values are their own names.
7
+ */
8
+ export async function readShipNames(deps, read) {
9
+ if (read.breakdown !== 'machine' && read.breakdown !== 'ship') {
10
+ return ok(undefined);
11
+ }
12
+ const crew = await deps.connections.find(read.fleetId);
13
+ if (!crew) {
14
+ return refuse('NOT_CONNECTED', 'The trierarch plugin is not connected to this fleet: connect it in the console');
15
+ }
16
+ const listed = await deps.door.listShips(crew.crewToken);
17
+ if (!listed.isOk) {
18
+ return refuse('FLEET_UNAVAILABLE', `The fleet did not answer its ships: ${listed.error.message}`);
19
+ }
20
+ return ok(new Map(listed.value.map((ship) => [ship.shipId, ship.name])));
21
+ }
22
+ /** A value's name: a machine or ship by its name in the fleet, any other value itself; a login not reported has none. */
23
+ export function nameOfValue(value, shipNames) {
24
+ if (value === null) {
25
+ return null;
26
+ }
27
+ return shipNames ? (shipNames.get(value) ?? null) : value;
28
+ }
29
+ //# sourceMappingURL=ship-names.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ship-names.js","sourceRoot":"","sources":["../../../src/core/usage/ship-names.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,MAAM,EAAoB,MAAM,qBAAqB,CAAC;AAC/D,OAAO,EAAE,EAAE,EAAe,MAAM,qBAAqB,CAAC;AAGtD;;;;GAIG;AACH,MAAM,CAAC,KAAK,UAAU,aAAa,CACjC,IAAuD,EACvD,IAAqE;IAErE,IAAI,IAAI,CAAC,SAAS,KAAK,SAAS,IAAI,IAAI,CAAC,SAAS,KAAK,MAAM,EAAE,CAAC;QAC9D,OAAO,EAAE,CAAC,SAAS,CAAC,CAAC;IACvB,CAAC;IACD,MAAM,IAAI,GAAG,MAAM,IAAI,CAAC,WAAW,CAAC,IAAI,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;IACvD,IAAI,CAAC,IAAI,EAAE,CAAC;QACV,OAAO,MAAM,CAAC,eAAe,EAAE,gFAAgF,CAAC,CAAC;IACnH,CAAC;IACD,MAAM,MAAM,GAAG,MAAM,IAAI,CAAC,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC;IACzD,IAAI,CAAC,MAAM,CAAC,IAAI,EAAE,CAAC;QACjB,OAAO,MAAM,CAAC,mBAAmB,EAAE,uCAAuC,MAAM,CAAC,KAAK,CAAC,OAAO,EAAE,CAAC,CAAC;IACpG,CAAC;IACD,OAAO,EAAE,CAAC,IAAI,GAAG,CAAC,MAAM,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,IAAI,EAAoB,EAAE,CAAC,CAAC,IAAI,CAAC,MAAM,EAAE,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC;AAC7F,CAAC;AAED,yHAAyH;AACzH,MAAM,UAAU,WAAW,CAAC,KAAoB,EAAE,SAAkD;IAClG,IAAI,KAAK,KAAK,IAAI,EAAE,CAAC;QACnB,OAAO,IAAI,CAAC;IACd,CAAC;IACD,OAAO,SAAS,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC,GAAG,CAAC,KAAK,CAAC,IAAI,IAAI,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC;AAC5D,CAAC"}
@@ -0,0 +1,28 @@
1
+ import type { ShipId } from '@aeolus-fleet/common';
2
+ /** One machine and harness: where a page of subscription windows left off. */
3
+ export interface WindowsChartKey {
4
+ readonly machineShipId: ShipId;
5
+ readonly harness: string;
6
+ }
7
+ /** The subscription windows of one harness on one machine in a range: each window's readings, oldest first. */
8
+ export interface SubscriptionWindows extends WindowsChartKey {
9
+ readonly windows: readonly {
10
+ readonly windowMinutes: number;
11
+ readonly readings: readonly {
12
+ readonly readAt: Date;
13
+ readonly usedPercent: number;
14
+ readonly resetsAt: Date;
15
+ }[];
16
+ }[];
17
+ }
18
+ /** One page of machine and harness charts in a range, from inclusive, to exclusive, ordered by machine then harness. */
19
+ export interface WindowsQuery {
20
+ readonly from: Date;
21
+ readonly to: Date;
22
+ readonly filters: {
23
+ readonly machineShipIds?: readonly ShipId[];
24
+ readonly harnesses?: readonly string[];
25
+ };
26
+ readonly after: WindowsChartKey | null;
27
+ readonly limit: number;
28
+ }
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=subscription-windows.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"subscription-windows.js","sourceRoot":"","sources":["../../../src/core/usage/subscription-windows.ts"],"names":[],"mappings":""}
@@ -0,0 +1,61 @@
1
+ import type { ShipId } from '@aeolus-fleet/common';
2
+ import type { TokenCosts, TokenCounts } from './usage-reading.js';
3
+ /** What usage splits by, besides token kind: one of the setup that used the tokens. */
4
+ export declare const USAGE_BREAKDOWNS: readonly ["machine", "harness", "model", "ship", "login"];
5
+ export type UsageBreakdown = (typeof USAGE_BREAKDOWNS)[number];
6
+ /** What the series are ranked and shown by: tokens (all four kinds) or the estimated cost. */
7
+ export declare const USAGE_MEASURES: readonly ["tokens", "cost"];
8
+ export type UsageMeasure = (typeof USAGE_MEASURES)[number];
9
+ /** Hours (from usage_hours) or UTC days (from their rollup). */
10
+ export type UsageGrain = 'hour' | 'day';
11
+ /** The values usage is narrowed to, per breakdown; a breakdown left out is not narrowed. A null login is "not reported". */
12
+ export interface UsageFilters {
13
+ readonly machineShipIds?: readonly ShipId[];
14
+ readonly harnesses?: readonly string[];
15
+ readonly models?: readonly string[];
16
+ readonly shipIds?: readonly ShipId[];
17
+ readonly logins?: readonly (string | null)[];
18
+ }
19
+ /** One read of token usage: a range of whole hours or whole UTC days, from inclusive, to exclusive. */
20
+ export interface TokenUsageQuery {
21
+ readonly from: Date;
22
+ readonly to: Date;
23
+ readonly grain: UsageGrain;
24
+ /** None: one series for all of it. */
25
+ readonly splitBy: UsageBreakdown | null;
26
+ readonly measure: UsageMeasure;
27
+ readonly filters: UsageFilters;
28
+ }
29
+ /** The tokens and estimated cost of one series in one bucket, each kind apart. */
30
+ export interface UsageAmounts {
31
+ readonly tokens: TokenCounts;
32
+ /** Of the priced tokens only. */
33
+ readonly estimatedCostUsd: TokenCosts;
34
+ /** The tokens of models the trierarch does not price. */
35
+ readonly unpricedTokens: TokenCounts;
36
+ }
37
+ /** A series of the read: one value of the breakdown (a null login is "not reported"), the rest as other, or all of it unsplit. */
38
+ export type UsageSeriesKey = {
39
+ readonly kind: 'value';
40
+ readonly value: string | null;
41
+ } | {
42
+ readonly kind: 'other';
43
+ } | {
44
+ readonly kind: 'all';
45
+ };
46
+ /**
47
+ * Token usage as the history answers it: the series by their total in the
48
+ * range, largest first, at most 7 and other for the rest; each bucket that
49
+ * holds usage, oldest first, with the amounts of each series in it by index;
50
+ * and the models in the range the trierarch does not price.
51
+ */
52
+ export interface TokenUsage {
53
+ readonly series: readonly UsageSeriesKey[];
54
+ readonly buckets: readonly {
55
+ readonly at: Date;
56
+ readonly amounts: readonly (UsageAmounts & {
57
+ readonly series: number;
58
+ })[];
59
+ }[];
60
+ readonly unpricedModels: readonly string[];
61
+ }
@@ -0,0 +1,5 @@
1
+ /** What usage splits by, besides token kind: one of the setup that used the tokens. */
2
+ export const USAGE_BREAKDOWNS = ['machine', 'harness', 'model', 'ship', 'login'];
3
+ /** What the series are ranked and shown by: tokens (all four kinds) or the estimated cost. */
4
+ export const USAGE_MEASURES = ['tokens', 'cost'];
5
+ //# sourceMappingURL=token-usage.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"token-usage.js","sourceRoot":"","sources":["../../../src/core/usage/token-usage.ts"],"names":[],"mappings":"AAIA,uFAAuF;AACvF,MAAM,CAAC,MAAM,gBAAgB,GAAG,CAAC,SAAS,EAAE,SAAS,EAAE,OAAO,EAAE,MAAM,EAAE,OAAO,CAAU,CAAC;AAG1F,8FAA8F;AAC9F,MAAM,CAAC,MAAM,cAAc,GAAG,CAAC,QAAQ,EAAE,MAAM,CAAU,CAAC"}
@@ -0,0 +1,12 @@
1
+ import { type DomainError } from '../shared/errors.js';
2
+ import { type Result } from '../shared/result.js';
3
+ import type { UsageGrain } from './token-usage.js';
4
+ /**
5
+ * The grain a range of usage is read in (the usage design, U15): hours up to 7
6
+ * days, UTC days past that. A range starts before it ends, and starts and ends
7
+ * on its grain: a whole hour, or a UTC midnight.
8
+ */
9
+ export declare function usageGrain(range: {
10
+ readonly from: Date;
11
+ readonly to: Date;
12
+ }): Result<UsageGrain, DomainError<'INVALID_RANGE'>>;
@@ -0,0 +1,24 @@
1
+ import { refuse } from '../shared/errors.js';
2
+ import { ok } from '../shared/result.js';
3
+ /** The longest range read in hours; anything longer is read in UTC days. */
4
+ const HOURS_UP_TO_MS = 7 * 24 * 60 * 60 * 1000;
5
+ const HOUR_MS = 60 * 60 * 1000;
6
+ const DAY_MS = 24 * HOUR_MS;
7
+ /**
8
+ * The grain a range of usage is read in (the usage design, U15): hours up to 7
9
+ * days, UTC days past that. A range starts before it ends, and starts and ends
10
+ * on its grain: a whole hour, or a UTC midnight.
11
+ */
12
+ export function usageGrain(range) {
13
+ const length = range.to.getTime() - range.from.getTime();
14
+ if (length <= 0) {
15
+ return refuse('INVALID_RANGE', 'A range ends after it starts');
16
+ }
17
+ const grain = length <= HOURS_UP_TO_MS ? 'hour' : 'day';
18
+ const unit = grain === 'hour' ? HOUR_MS : DAY_MS;
19
+ if (range.from.getTime() % unit !== 0 || range.to.getTime() % unit !== 0) {
20
+ return refuse('INVALID_RANGE', grain === 'hour' ? 'A range of hours starts and ends on a whole hour' : 'A range of days starts and ends at a UTC midnight');
21
+ }
22
+ return ok(grain);
23
+ }
24
+ //# sourceMappingURL=usage-range.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"usage-range.js","sourceRoot":"","sources":["../../../src/core/usage/usage-range.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAoB,MAAM,qBAAqB,CAAC;AAC/D,OAAO,EAAE,EAAE,EAAe,MAAM,qBAAqB,CAAC;AAGtD,4EAA4E;AAC5E,MAAM,cAAc,GAAG,CAAC,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,IAAI,CAAC;AAC/C,MAAM,OAAO,GAAG,EAAE,GAAG,EAAE,GAAG,IAAI,CAAC;AAC/B,MAAM,MAAM,GAAG,EAAE,GAAG,OAAO,CAAC;AAE5B;;;;GAIG;AACH,MAAM,UAAU,UAAU,CAAC,KAAiD;IAC1E,MAAM,MAAM,GAAG,KAAK,CAAC,EAAE,CAAC,OAAO,EAAE,GAAG,KAAK,CAAC,IAAI,CAAC,OAAO,EAAE,CAAC;IACzD,IAAI,MAAM,IAAI,CAAC,EAAE,CAAC;QAChB,OAAO,MAAM,CAAC,eAAe,EAAE,8BAA8B,CAAC,CAAC;IACjE,CAAC;IACD,MAAM,KAAK,GAAe,MAAM,IAAI,cAAc,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,KAAK,CAAC;IACpE,MAAM,IAAI,GAAG,KAAK,KAAK,MAAM,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC;IACjD,IAAI,KAAK,CAAC,IAAI,CAAC,OAAO,EAAE,GAAG,IAAI,KAAK,CAAC,IAAI,KAAK,CAAC,EAAE,CAAC,OAAO,EAAE,GAAG,IAAI,KAAK,CAAC,EAAE,CAAC;QACzE,OAAO,MAAM,CAAC,eAAe,EAAE,KAAK,KAAK,MAAM,CAAC,CAAC,CAAC,kDAAkD,CAAC,CAAC,CAAC,mDAAmD,CAAC,CAAC;IAC9J,CAA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