Nothing anywhere recorded what a model call cost: the harness used a provider's usage numbers only to decide whether a turn had finished, then dropped them. So a reply could not say what it took to make, one model could not be compared with another on price, and there was no way to ask what a day, a week or a month of the crew's work came to. - `token_usages` records one row per call: user, bot, workspace, session, job and message, the harness, the provider and model that actually answered, the prompt and completion counts, and the cost when the provider reports one. - `harness-usage-scope.ts` carries those ids in an AsyncLocalStorage scope that `run-bot-job.ts` enters once, so a run's turns, its session summary, its transcriptions, images and embeddings are all attributed without threading ids through every harness function. Nested runs merge rather than replace, and a null genuinely clears a field (a bot's own chat has no workspace). - Work with no run behind it is charged to its owner instead: `run-with-fallback-scope.ts` reaches the workspace's creator else the bot's, and deliberately does not override a real run, so an ingest a bot asked for is not misattributed to the bot's owner. - All six harnesses record through `withCandidates`'s `on_success`. Recording never throws: a model that answered must not lose its answer to a failed insert. - A job's calls land before its message exists, so `complete-bot-job.ts` back-fills `message_id` once the message is inserted. - The message's actions row shows what it cost, beside the timestamp and the play button, with the breakdown in its title. - `/admin/usage` totals the install's spending for 7, 30 or 90 days: the total against the previous period, a day-by-day chart, a split per kind of call, and a table per model with the price per thousand tokens worked out from cost and tokens, which is what makes two models comparable. Crew managers only, since it is the whole install's work rather than one user's conversation. It sits with Routines and Activity in the user popover rather than under Settings, because it is a record of what happened, not something to configure. Days are grouped in local time (`date(created_at / 1000, 'unixepoch', 'localtime')`), matching the local midnights the window builds and the `YYYY-MM-DD` buckets it looks up. Grouping by the bare UTC day put a late evening's calls under a name no bucket carried, so they vanished and the chart's total silently disagreed with the totals card; a test pins it. `canSeeRailItem` now decides who sees a popover or rail entry. The two each filtered with their own inline rule and only one understood `can_view`, so a gated page could be offered in the popover to everyone.
73 lines
2.9 KiB
TypeScript
73 lines
2.9 KiB
TypeScript
"use client";
|
|
|
|
import { useContext, useState } from "react";
|
|
import Paper from "@moduletrace/twui/elements/Paper";
|
|
import Divider from "@moduletrace/twui/layout/Divider";
|
|
import { Download } from "lucide-react";
|
|
import { RailUserMenuItems } from "../(dict)/rail-user-menu-dict";
|
|
import RailUserMenuEntry from "./rail-user-menu-entry";
|
|
import RailUserMenuAction from "./rail-user-menu-action";
|
|
import InstallAppModal from "./install-app-modal";
|
|
import { AppContext } from "@/src/pages/__root";
|
|
import canSeeRailItem from "@/src/utils/rbac/can-see-rail-item";
|
|
import isPassThroughUser from "@/src/utils/rbac/is-pass-through-user";
|
|
import useInstallPrompt from "@/src/hooks/use-install-prompt";
|
|
|
|
/**
|
|
* 220px popover: the only element in the shell with elevation. Items
|
|
* follow the same rule as the rail's lists: super admin ones show to
|
|
* super admins only, RBAC-gated ones by their own rule. Install app shows
|
|
* while the browser can install it; Safari gets the Share menu steps
|
|
* instead.
|
|
*/
|
|
export default function RailUserMenu() {
|
|
const { pageProps } = useContext(AppContext);
|
|
const is_super_admin = isPassThroughUser({ user_types: pageProps?.user_types });
|
|
const shown = RailUserMenuItems.filter((item) =>
|
|
canSeeRailItem({ item, is_super_admin, user_types: pageProps?.user_types }),
|
|
);
|
|
const items = shown.filter((item) => !item.destructive);
|
|
const destructive = shown.filter((item) => item.destructive);
|
|
|
|
const { can_install, needs_ios_steps, installing, install } =
|
|
useInstallPrompt();
|
|
const [ios_modal_open, setIosModalOpen] = useState(false);
|
|
|
|
const show_install = can_install || needs_ios_steps;
|
|
|
|
return (
|
|
<Paper
|
|
role="menu"
|
|
className="w-[220px] mb-1 p-0! py-1 gap-0! items-stretch rounded-default bg-surface dark:bg-surface border-hairline dark:border-hairline shadow-popover! dark:shadow-popover!"
|
|
>
|
|
{items.map((item) => (
|
|
<RailUserMenuEntry key={item.title} {...item} />
|
|
))}
|
|
{show_install ? (
|
|
<RailUserMenuAction
|
|
title={needs_ios_steps ? "How to install" : "Install app"}
|
|
Icon={Download}
|
|
disabled={installing}
|
|
onClick={() => {
|
|
if (needs_ios_steps) {
|
|
setIosModalOpen(true);
|
|
return;
|
|
}
|
|
install();
|
|
}}
|
|
/>
|
|
) : null}
|
|
<Divider className="my-1 mx-3 w-auto border-hairline dark:border-hairline" />
|
|
{destructive.map((item) => (
|
|
<RailUserMenuEntry key={item.title} {...item} />
|
|
))}
|
|
{needs_ios_steps ? (
|
|
<InstallAppModal
|
|
open={ios_modal_open}
|
|
setOpen={setIosModalOpen}
|
|
/>
|
|
) : null}
|
|
</Paper>
|
|
);
|
|
}
|