Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

usageAccess.ts

90 lines3.2 KB

1import {
2 AuthDiagnosticsReadScope,
3 sessionGrantsScope,
4 type ServerConfig,
5 type SessionGrantInput,
6 type UsageAccessRequest,
7 type UsageSummary,
8} from "@t3tools/contracts";
9​
10import type { EnvironmentConnectionPhase } from "../connection/presentation.ts";
11import { mFallback, msg } from "gt-i18n";
12import type { MFunctionType } from "gt-i18n/types";
13​
14export function resolveUsageAccess(
15 input: {
16 readonly connectionPhase: EnvironmentConnectionPhase;
17 readonly session: SessionGrantInput | null;
18 readonly hasSessionError: boolean;
19 },
20 resolve: MFunctionType = mFallback,
21) {
22 if (input.hasSessionError) {
23 return {
24 canReadDiagnostics: false,
25 isPending: false,
26 error: resolve(msg("Could not check this connection's access to diagnostics and usage.")),
27 };
28 }
29 if (input.session === null) {
30 // An unprepared offline connection cannot finish its session check. Keep
31 // it out of pending totals until the connection starts another attempt.
32 const isPending =
33 input.connectionPhase === "connected" ||
34 input.connectionPhase === "connecting" ||
35 input.connectionPhase === "reconnecting";
36 return {
37 canReadDiagnostics: false,
38 isPending,
39 error: isPending ? null : resolve(msg("This environment is not connected.")),
40 };
41 }
42 const canReadDiagnostics = sessionGrantsScope(input.session, AuthDiagnosticsReadScope);
43 return {
44 canReadDiagnostics,
45 isPending: false,
46 error: canReadDiagnostics
47 ? null
48 : resolve(msg("This connection does not have access to diagnostics and usage.")),
49 };
51​
52/**
53 * The agents a session can still answer for. A summary lists who was unanswered when its scan
54 * began, and a first scan can run long, so it is checked against the live settings.
55 */
56export function unansweredUsageAccessRequests(input: {
57 readonly canAnswer: boolean;
58 readonly summary: Pick<UsageSummary, "accessRequests"> | null;
59 /** Null until the environment's settings and provider list have loaded. */
60 readonly config: Pick<ServerConfig, "settings" | "providers"> | null;
61}): readonly UsageAccessRequest[] {
62 const { config } = input;
63 // An answer is saved by rewriting the instance from live settings, so nothing is asked before they load.
64 if (!input.canAnswer || config === null) return [];
65 return (input.summary?.accessRequests ?? []).filter(
66 (request) =>
67 config.settings.providerInstances[request.instanceId]?.usageAccess === undefined &&
68 // An agent removed or turned off since the scan must not be saved back into settings.
69 config.providers.some(
70 (provider) => provider.instanceId === request.instanceId && provider.enabled,
71 ),
72 );
74​
75/** What usage access lets T3 Code read for an agent, as the short note beside its switch. */
76export function usageAccessReadsLabel(driver: string, resolve: MFunctionType = mFallback): string {
77 switch (driver) {
78 case "claudeAgent":
79 case "grok":
80 return resolve(msg("Session history and sign-in"));
81 case "opencode":
82 return resolve(msg("Usage database and Go key"));
83 case "antigravity":
84 return resolve(msg("Conversation history"));
85 case "cursor":
86 return resolve(msg("Account usage from Cursor"));
87 default:
88 return resolve(msg("Session history"));
89 }