@agent-native/dispatch 0.2.6 → 0.2.8

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.
@@ -1,5 +1,18 @@
1
- import { getUsageSummary } from "@agent-native/core/usage";
1
+ import {
2
+ getUsageSummary,
3
+ usageBillingForEngine,
4
+ type UsageBillingMode,
5
+ } from "@agent-native/core/usage";
2
6
  import { getDbExec } from "@agent-native/core/db";
7
+ import {
8
+ detectEngineFromEnv,
9
+ detectEngineFromUserSecrets,
10
+ getAgentEngineEntry,
11
+ isAgentEngineSettingConfigured,
12
+ isStoredEngineUsable,
13
+ registerBuiltinEngines,
14
+ } from "@agent-native/core/agent/engine";
15
+ import { getSetting } from "@agent-native/core/settings";
3
16
  import { currentOrgId, currentOwnerEmail } from "./dispatch-store.js";
4
17
  import {
5
18
  listWorkspaceApps,
@@ -8,6 +21,8 @@ import {
8
21
 
9
22
  const DAY_MS = 86_400_000;
10
23
 
24
+ registerBuiltinEngines();
25
+
11
26
  export interface UsageMetricBucket {
12
27
  key: string;
13
28
  label: string;
@@ -70,6 +85,7 @@ export interface RecentUsageMetric {
70
85
  }
71
86
 
72
87
  export interface DispatchUsageMetrics {
88
+ billing: UsageBillingMode;
73
89
  sinceMs: number;
74
90
  sinceDays: number;
75
91
  generatedAt: number;
@@ -159,6 +175,30 @@ function isEnvAdmin(email: string): boolean {
159
175
  ].includes(normalized);
160
176
  }
161
177
 
178
+ async function detectUsageEngineName(): Promise<string | null> {
179
+ try {
180
+ const stored = (await getSetting("agent-engine")) as {
181
+ engine?: string;
182
+ } | null;
183
+ if (isAgentEngineSettingConfigured(stored)) {
184
+ return (stored as { engine: string }).engine;
185
+ }
186
+ if (stored && typeof stored.engine === "string") {
187
+ const entry = getAgentEngineEntry(stored.engine);
188
+ if (entry && isStoredEngineUsable(stored, entry)) {
189
+ return stored.engine;
190
+ }
191
+ }
192
+
193
+ const detectedFromUser = await detectEngineFromUserSecrets();
194
+ if (detectedFromUser) return detectedFromUser.name;
195
+
196
+ return detectEngineFromEnv()?.name ?? null;
197
+ } catch {
198
+ return null;
199
+ }
200
+ }
201
+
162
202
  async function queryRows<T extends Record<string, unknown>>(
163
203
  sql: string,
164
204
  args: unknown[] = [],
@@ -353,6 +393,7 @@ export async function listDispatchUsageMetrics(input: {
353
393
  const { viewerEmail, orgId, role } = await assertCanViewMetrics();
354
394
  const sinceDays = Math.max(1, Math.min(365, input.sinceDays ?? 30));
355
395
  const sinceMs = Date.now() - sinceDays * DAY_MS;
396
+ const billing = usageBillingForEngine(await detectUsageEngineName());
356
397
 
357
398
  // Initializes token_usage on fresh deployments before the read-only
358
399
  // aggregate queries below. The fake owner avoids changing visible data.
@@ -568,6 +609,7 @@ export async function listDispatchUsageMetrics(input: {
568
609
  );
569
610
 
570
611
  return {
612
+ billing,
571
613
  sinceMs,
572
614
  sinceDays,
573
615
  generatedAt: Date.now(),