@remit/web-client 0.0.338 → 0.0.339
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.
- package/dist/src/components/settings/classification/ClassificationPanel.js +56 -10
- package/dist/src/components/settings/classification/DailyLimitCard.d.ts +8 -0
- package/dist/src/components/settings/classification/DailyLimitCard.js +39 -0
- package/dist/src/components/settings/classification/Field.d.ts +9 -0
- package/dist/src/components/settings/classification/Field.js +8 -0
- package/dist/src/components/settings/classification/ProviderCard.js +5 -11
- package/dist/src/lib/classification.d.ts +14 -2
- package/dist/src/lib/classification.js +50 -3
- package/package.json +1 -1
|
@@ -1,7 +1,11 @@
|
|
|
1
1
|
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
2
|
+
import { configOperationsGetConfigOptions } from "@remit/api-http-client/@tanstack/react-query.gen.ts";
|
|
3
|
+
import { Badge, Banner, Button, Switch } from "@remit/ui";
|
|
4
|
+
import { useQuery } from "@tanstack/react-query";
|
|
5
|
+
import { useId, useRef, useState } from "react";
|
|
4
6
|
import { useClassificationSettings, useSaveClassification, } from "../../../hooks/useClassificationSettings";
|
|
7
|
+
import { isCapped, resetTime, usageCost } from "../../../lib/classification";
|
|
8
|
+
import { DailyLimitCard } from "./DailyLimitCard";
|
|
5
9
|
import { FailureBanner } from "./FailureBanner";
|
|
6
10
|
import { ProviderCard } from "./ProviderCard";
|
|
7
11
|
const LOAD_FAILED = "Mail classification settings did not load";
|
|
@@ -14,36 +18,78 @@ const switchNote = (settings) => {
|
|
|
14
18
|
}
|
|
15
19
|
return "Provider saved. Built-in rules label your mail until you switch this on.";
|
|
16
20
|
};
|
|
17
|
-
|
|
21
|
+
const namesOf = (accounts) => (accountId) => {
|
|
22
|
+
const account = accounts.find((entry) => entry.accountId === accountId);
|
|
23
|
+
return account?.displayName?.trim() || account?.email || accountId;
|
|
24
|
+
};
|
|
25
|
+
function StatusBadge({ on, capped }) {
|
|
26
|
+
if (!on) {
|
|
27
|
+
return _jsx(Badge, { dot: true, children: "off" });
|
|
28
|
+
}
|
|
29
|
+
if (capped) {
|
|
30
|
+
return (_jsx(Badge, { tone: "warning", dot: true, children: "cap reached" }));
|
|
31
|
+
}
|
|
32
|
+
return (_jsx(Badge, { tone: "positive", dot: true, children: "on" }));
|
|
33
|
+
}
|
|
34
|
+
function UsageRows({ usage: rows, preset, nameOf, }) {
|
|
35
|
+
return (_jsx("dl", { className: "space-y-2", children: rows.map((usage) => (_jsxs("div", { className: "flex flex-wrap items-baseline justify-between gap-x-3", children: [_jsx("dt", { className: "min-w-0 truncate text-sm text-fg", children: nameOf(usage.accountId) }), _jsxs("dd", { className: "text-right text-sm text-fg-muted", children: [_jsxs("span", { className: "text-fg", children: [usage.calls, " / ", usage.dailyLimit, " calls"] }), " ", _jsxs("span", { className: "text-xs", children: [usageCost(usage, preset), isCapped(usage) && (_jsx("span", { className: "text-warning", children: " \u00B7 limit reached" }))] })] })] }, usage.accountId))) }));
|
|
36
|
+
}
|
|
37
|
+
function CapBanner({ first, capped, nameOf, onRaiseLimit, }) {
|
|
38
|
+
const names = new Intl.ListFormat("en", { type: "conjunction" }).format(capped.map((usage) => nameOf(usage.accountId)));
|
|
39
|
+
return (_jsxs(Banner, { tone: "warning", variant: "soft", children: [_jsxs("p", { className: "font-medium", children: [names, " reached the daily limit of ", first.dailyLimit, " calls"] }), _jsxs("p", { className: "mt-0.5 text-fg-muted", children: [capped.length === 1 ? "Its" : "Their", " new mail uses built-in rules until ", resetTime(first.resetsAt), "."] }), _jsx(Button, { variant: "secondary", className: "mt-2 min-h-11", onClick: onRaiseLimit, children: "Raise the limit" })] }));
|
|
40
|
+
}
|
|
41
|
+
function StatusCard({ settings, presets, nameOf, onRaiseLimit, }) {
|
|
18
42
|
const toggle = useSaveClassification();
|
|
19
43
|
const [refusal, setRefusal] = useState(null);
|
|
20
44
|
const ids = useId();
|
|
21
45
|
const configured = settings.state === "Configured";
|
|
22
46
|
const on = configured && settings.enabled;
|
|
47
|
+
const capped = on ? settings.usage.filter(isCapped) : [];
|
|
48
|
+
const [firstCapped] = capped;
|
|
23
49
|
const flip = () => {
|
|
24
50
|
setRefusal(null);
|
|
25
51
|
toggle.mutate({ body: { classificationEnabled: !on } }, { onError: setRefusal });
|
|
26
52
|
};
|
|
27
|
-
return (_jsxs("section", { "aria-labelledby": `${ids}-title`, className: "space-y-3", children: [_jsx("h2", { id: `${ids}-title`, className: "text-xs font-semibold uppercase tracking-wider text-fg-subtle", children: "Status" }), _jsxs("div", { className: "flex min-h-11 items-center gap-3", children: [_jsxs("span", { className: "flex min-w-0 flex-1 flex-col", children: [_jsx("span", { id: `${ids}-label`, className: "text-sm text-fg", children: "Use the model" }), _jsx("span", { id: `${ids}-note`, className: "text-xs text-fg-subtle", children: switchNote(settings) })] }), _jsx(Switch, { checked: on, "aria-labelledby": `${ids}-label`, "aria-describedby": `${ids}-note`, "aria-disabled": !configured || toggle.isPending, "aria-busy": toggle.isPending, onToggle: flip })] }), refusal !== null && (_jsx(FailureBanner, { failure: {
|
|
53
|
+
return (_jsxs("section", { "aria-labelledby": `${ids}-title`, className: "space-y-3", children: [_jsx("h2", { id: `${ids}-title`, className: "text-xs font-semibold uppercase tracking-wider text-fg-subtle", children: "Status" }), _jsxs("div", { className: "flex min-h-11 items-center gap-3", children: [_jsxs("span", { className: "flex min-w-0 flex-1 flex-col", children: [_jsx("span", { id: `${ids}-label`, className: "text-sm text-fg", children: "Use the model" }), _jsx("span", { id: `${ids}-note`, className: "text-xs text-fg-subtle", children: switchNote(settings) })] }), _jsx(StatusBadge, { on: on, capped: firstCapped !== undefined }), _jsx(Switch, { checked: on, "aria-labelledby": `${ids}-label`, "aria-describedby": `${ids}-note`, "aria-disabled": !configured || toggle.isPending, "aria-busy": toggle.isPending, onToggle: flip })] }), firstCapped && (_jsx(CapBanner, { first: firstCapped, capped: capped, nameOf: nameOf, onRaiseLimit: onRaiseLimit })), configured && (_jsx(UsageRows, { usage: settings.usage, preset: presets.find((preset) => preset.presetId === settings.presetId), nameOf: nameOf })), refusal !== null && (_jsx(FailureBanner, { failure: {
|
|
28
54
|
title: on
|
|
29
55
|
? "Classification was not switched off"
|
|
30
56
|
: "Classification was not switched on",
|
|
31
57
|
error: refusal,
|
|
32
58
|
} }))] }));
|
|
33
59
|
}
|
|
60
|
+
function LoadingSkeleton() {
|
|
61
|
+
return (
|
|
62
|
+
// biome-ignore lint/a11y/useAriaPropsSupportedByRole: aria-label on a loading skeleton is what assistive tech has to go on
|
|
63
|
+
_jsx("div", { className: "h-48 animate-pulse rounded-sm border border-line bg-surface", "aria-busy": "true", "aria-label": "Loading mail classification settings" }));
|
|
64
|
+
}
|
|
34
65
|
export function ClassificationPanel() {
|
|
35
66
|
const query = useClassificationSettings();
|
|
67
|
+
const config = useQuery({
|
|
68
|
+
...configOperationsGetConfigOptions(),
|
|
69
|
+
staleTime: Infinity,
|
|
70
|
+
});
|
|
36
71
|
const [announcement, setAnnouncement] = useState("");
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
72
|
+
const limitRef = useRef(null);
|
|
73
|
+
const raiseLimit = () => {
|
|
74
|
+
const input = limitRef.current;
|
|
75
|
+
if (!input)
|
|
76
|
+
return;
|
|
77
|
+
input.focus();
|
|
78
|
+
input.select();
|
|
79
|
+
input.scrollIntoView({ block: "center" });
|
|
80
|
+
};
|
|
81
|
+
if (query.isPending)
|
|
82
|
+
return _jsx(LoadingSkeleton, {});
|
|
42
83
|
if (query.signedOut)
|
|
43
84
|
return null;
|
|
44
85
|
if (query.isError) {
|
|
45
86
|
return (_jsx(FailureBanner, { failure: { title: LOAD_FAILED, error: query.error }, onRetry: () => void query.refetch() }));
|
|
46
87
|
}
|
|
88
|
+
if (config.isPending)
|
|
89
|
+
return _jsx(LoadingSkeleton, {});
|
|
90
|
+
if (config.isError) {
|
|
91
|
+
return (_jsx(FailureBanner, { failure: { title: LOAD_FAILED, error: config.error }, onRetry: () => void config.refetch() }));
|
|
92
|
+
}
|
|
47
93
|
const { classification, classificationPresets } = query.data;
|
|
48
|
-
return (_jsxs("div", { className: "max-w-xl space-y-8", children: [_jsx(StatusCard, { settings: classification }), _jsx(ProviderCard, { presets: classificationPresets, settings: classification, onAnnounce: setAnnouncement }, classification.state), _jsx("p", { "aria-live": "polite", className: "text-sm text-fg-muted", children: announcement })] }));
|
|
94
|
+
return (_jsxs("div", { className: "max-w-xl space-y-8", children: [_jsx(StatusCard, { settings: classification, presets: classificationPresets, nameOf: namesOf(config.data.accounts), onRaiseLimit: raiseLimit }), _jsx(ProviderCard, { presets: classificationPresets, settings: classification, onAnnounce: setAnnouncement }, classification.state), classification.state === "Configured" && (_jsx(DailyLimitCard, { settings: classification, inputRef: limitRef, onAnnounce: setAnnouncement })), _jsx("p", { "aria-live": "polite", className: "text-sm text-fg-muted", children: announcement })] }));
|
|
49
95
|
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { RemitImapClassificationConfigured } from "@remit/api-http-client/types.gen.ts";
|
|
2
|
+
import { type Ref } from "react";
|
|
3
|
+
export interface DailyLimitCardProps {
|
|
4
|
+
settings: RemitImapClassificationConfigured;
|
|
5
|
+
inputRef: Ref<HTMLInputElement>;
|
|
6
|
+
onAnnounce: (text: string) => void;
|
|
7
|
+
}
|
|
8
|
+
export declare function DailyLimitCard({ settings, inputRef, onAnnounce, }: DailyLimitCardProps): import("react").JSX.Element;
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { Button, Input } from "@remit/ui";
|
|
3
|
+
import { useId, useState } from "react";
|
|
4
|
+
import { useSaveClassification } from "../../../hooks/useClassificationSettings";
|
|
5
|
+
import { parseDailyLimit, resetTime } from "../../../lib/classification";
|
|
6
|
+
import { FailureBanner } from "./FailureBanner";
|
|
7
|
+
import { describedBy, Field } from "./Field";
|
|
8
|
+
export function DailyLimitCard({ settings, inputRef, onAnnounce, }) {
|
|
9
|
+
const save = useSaveClassification();
|
|
10
|
+
const [value, setValue] = useState(() => `${settings.dailyLimit}`);
|
|
11
|
+
const [error, setError] = useState(undefined);
|
|
12
|
+
const [failure, setFailure] = useState(null);
|
|
13
|
+
const ids = useId();
|
|
14
|
+
const inputId = `${ids}-limit`;
|
|
15
|
+
const [first] = settings.usage;
|
|
16
|
+
const reset = first ? `Counts reset at ${resetTime(first.resetsAt)}. ` : "";
|
|
17
|
+
const onSave = () => {
|
|
18
|
+
if (save.isPending)
|
|
19
|
+
return;
|
|
20
|
+
const parsed = parseDailyLimit(value);
|
|
21
|
+
if (!parsed.ok) {
|
|
22
|
+
setError(parsed.message);
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
25
|
+
setFailure(null);
|
|
26
|
+
save.mutate({ body: { classificationDailyLimit: parsed.limit } }, {
|
|
27
|
+
onSuccess: () => onAnnounce("Saved"),
|
|
28
|
+
onError: (cause) => setFailure({ title: "The daily limit was not saved", error: cause }),
|
|
29
|
+
});
|
|
30
|
+
};
|
|
31
|
+
return (_jsxs("section", { "aria-labelledby": `${ids}-title`, className: "space-y-4", children: [_jsx("h2", { id: `${ids}-title`, className: "text-xs font-semibold uppercase tracking-wider text-fg-subtle", children: "Daily limit" }), _jsx(Field, { id: inputId, label: "Calls per account per day", hint: `${reset}Past the limit, that account's new mail uses built-in rules.`, error: error, children: _jsx(Input, { id: inputId, ref: inputRef, className: "h-11", inputMode: "numeric", value: value, autoComplete: "off", "aria-invalid": error !== undefined, "aria-describedby": describedBy(inputId, true, error !== undefined), onChange: (event) => {
|
|
32
|
+
setValue(event.target.value);
|
|
33
|
+
setError(undefined);
|
|
34
|
+
onAnnounce("");
|
|
35
|
+
}, onKeyDown: (event) => {
|
|
36
|
+
if (event.key === "Enter")
|
|
37
|
+
onSave();
|
|
38
|
+
} }) }), _jsx(Button, { variant: "primary", className: "min-h-11", "aria-disabled": save.isPending, "aria-busy": save.isPending, onClick: onSave, children: save.isPending ? "Saving…" : "Save limit" }), failure && _jsx(FailureBanner, { failure: failure })] }));
|
|
39
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { ReactNode } from "react";
|
|
2
|
+
export declare function Field({ id, label, hint, error, children, }: {
|
|
3
|
+
id: string;
|
|
4
|
+
label: string;
|
|
5
|
+
hint?: string;
|
|
6
|
+
error?: string;
|
|
7
|
+
children: ReactNode;
|
|
8
|
+
}): import("react").JSX.Element;
|
|
9
|
+
export declare const describedBy: (id: string, hint: boolean, error: boolean) => string | undefined;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { FieldLabel } from "@remit/ui";
|
|
3
|
+
export function Field({ id, label, hint, error, children, }) {
|
|
4
|
+
return (_jsxs("div", { children: [_jsx(FieldLabel, { htmlFor: id, children: label }), children, error && (_jsx("p", { id: `${id}-error`, className: "mt-1 text-xs text-danger", children: error })), hint && (_jsx("p", { id: `${id}-hint`, className: "mt-1 text-xs text-fg-subtle", children: hint }))] }));
|
|
5
|
+
}
|
|
6
|
+
export const describedBy = (id, hint, error) => [error ? `${id}-error` : "", hint ? `${id}-hint` : ""]
|
|
7
|
+
.filter(Boolean)
|
|
8
|
+
.join(" ") || undefined;
|
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
import { jsxs as _jsxs, jsx as _jsx, Fragment as _Fragment } from "react/jsx-runtime";
|
|
2
|
-
import { Banner, Button, ConfirmDialog,
|
|
2
|
+
import { Banner, Button, ConfirmDialog, Input, PasswordInput, Select, } from "@remit/ui";
|
|
3
3
|
import { useEffect, useId, useRef, useState } from "react";
|
|
4
4
|
import { useClearClassification, useSaveClassification, useTestClassification, } from "../../../hooks/useClassificationSettings";
|
|
5
5
|
import { classificationReportHref, draftForPreset, draftFromSettings, groupPresets, hasSavedKey, needsBaseUrl, presetNote, providerSave, switchingFrom, testFailureHeadline, } from "../../../lib/classification";
|
|
6
6
|
import { formatDatePreset } from "../../../lib/format";
|
|
7
7
|
import { ReportLink } from "../ReportLink";
|
|
8
8
|
import { FailureBanner } from "./FailureBanner";
|
|
9
|
+
import { describedBy, Field } from "./Field";
|
|
9
10
|
const FIELD_FIX_LABEL = {
|
|
10
11
|
ApiKey: "Replace key",
|
|
11
12
|
Model: "Fix the model",
|
|
@@ -19,17 +20,11 @@ function TestOutcome({ result, presetLabel, onFix, shown, }) {
|
|
|
19
20
|
const field = result.field;
|
|
20
21
|
return (_jsxs(Banner, { tone: "danger", variant: "soft", children: [_jsx("p", { className: "font-medium", children: headline }), _jsxs("p", { className: "mt-0.5 break-words text-fg-muted", children: [result.fix, " Nothing changed: classification stays as it was."] }), _jsxs("p", { className: "flex flex-wrap items-center gap-x-4", children: [field !== undefined && shown[field] && (_jsx("button", { type: "button", onClick: () => onFix(field), className: "inline-flex min-h-11 items-center font-medium text-accent hover:underline", children: FIELD_FIX_LABEL[field] })), _jsx(ReportLink, { href: classificationReportHref(`Test of ${presetLabel} failed`, `${headline}. ${result.kind}. ${result.fix}`) })] })] }));
|
|
21
22
|
}
|
|
22
|
-
function Field({ id, label, hint, error, children, }) {
|
|
23
|
-
return (_jsxs("div", { children: [_jsx(FieldLabel, { htmlFor: id, children: label }), children, error && (_jsx("p", { id: `${id}-error`, className: "mt-1 text-xs text-danger", children: error })), hint && (_jsx("p", { id: `${id}-hint`, className: "mt-1 text-xs text-fg-subtle", children: hint }))] }));
|
|
24
|
-
}
|
|
25
|
-
const describedBy = (id, hint, error) => [error ? `${id}-error` : "", hint ? `${id}-hint` : ""]
|
|
26
|
-
.filter(Boolean)
|
|
27
|
-
.join(" ") || undefined;
|
|
28
23
|
function PresetForm({ preset, settings, onAnnounce }) {
|
|
29
24
|
const save = useSaveClassification();
|
|
30
25
|
const clear = useClearClassification();
|
|
31
26
|
const test = useTestClassification();
|
|
32
|
-
const [draft, setDraft] = useState(() => draftForPreset(preset
|
|
27
|
+
const [draft, setDraft] = useState(() => draftForPreset(preset, settings));
|
|
33
28
|
const [fieldError, setFieldError] = useState(null);
|
|
34
29
|
const [testResult, setTestResult] = useState(null);
|
|
35
30
|
const [replacing, setReplacing] = useState(false);
|
|
@@ -59,11 +54,10 @@ function PresetForm({ preset, settings, onAnnounce }) {
|
|
|
59
54
|
? settings
|
|
60
55
|
: undefined;
|
|
61
56
|
const showKeyInput = preset.needsKey && (savedKey === undefined || replacing);
|
|
62
|
-
const showBaseUrl = needsBaseUrl(preset);
|
|
63
57
|
const shown = {
|
|
64
58
|
ApiKey: preset.needsKey,
|
|
65
59
|
Model: true,
|
|
66
|
-
BaseUrl:
|
|
60
|
+
BaseUrl: true,
|
|
67
61
|
};
|
|
68
62
|
const errorFor = (field) => fieldError?.field === field ? fieldError.message : undefined;
|
|
69
63
|
const invalid = (field) => errorFor(field) !== undefined ||
|
|
@@ -147,7 +141,7 @@ function PresetForm({ preset, settings, onAnnounce }) {
|
|
|
147
141
|
};
|
|
148
142
|
const enabled = settings.state === "Configured" && settings.enabled;
|
|
149
143
|
const keyError = errorFor("ApiKey");
|
|
150
|
-
return (_jsxs(_Fragment, { children: [_jsx(Field, { id: modelId, label: "Model", hint: `Empty uses ${preset.defaultModel}.`, error: errorFor("Model"), children: _jsx(Input, { id: modelId, ref: modelRef, className: "h-11", value: draft.model, placeholder: preset.defaultModel, autoComplete: "off", spellCheck: false, "aria-invalid": invalid("Model"), "aria-describedby": describedBy(modelId, true, errorFor("Model") !== undefined), onChange: (event) => edit("Model", { model: event.target.value }) }) }),
|
|
144
|
+
return (_jsxs(_Fragment, { children: [_jsx(Field, { id: modelId, label: "Model", hint: `Empty uses ${preset.defaultModel}.`, error: errorFor("Model"), children: _jsx(Input, { id: modelId, ref: modelRef, className: "h-11", value: draft.model, placeholder: preset.defaultModel, autoComplete: "off", spellCheck: false, "aria-invalid": invalid("Model"), "aria-describedby": describedBy(modelId, true, errorFor("Model") !== undefined), onChange: (event) => edit("Model", { model: event.target.value }) }) }), _jsx(Field, { id: baseUrlId, label: "Base URL", hint: "Remit's server calls this URL.", error: errorFor("BaseUrl"), children: _jsx(Input, { id: baseUrlId, ref: baseUrlRef, className: "h-11", type: "url", inputMode: "url", value: draft.baseUrl, placeholder: preset.defaultBaseUrl ?? "http://localhost:11434/v1", autoComplete: "off", spellCheck: false, required: needsBaseUrl(preset), "aria-invalid": invalid("BaseUrl"), "aria-describedby": describedBy(baseUrlId, true, errorFor("BaseUrl") !== undefined), onChange: (event) => edit("BaseUrl", { baseUrl: event.target.value }) }) }), savedKey && !replacing && (_jsxs("div", { children: [_jsx("p", { className: "mb-1 text-xs font-medium text-fg-muted", children: "API key" }), _jsxs("div", { className: "flex flex-wrap items-center gap-x-3 gap-y-2", children: [_jsx("span", { className: invalid("ApiKey") ? "text-sm text-danger" : "text-sm text-fg", children: "set" }), _jsxs("span", { className: "text-sm text-fg-muted", children: ["saved ", formatDatePreset(savedKey.savedAt, "short")] }), _jsx("span", { className: "flex-1" }), _jsx(Button, { ref: replaceRef, variant: "secondary", className: "min-h-11", "aria-invalid": invalid("ApiKey"), onClick: startReplace, children: "Replace" }), _jsx(Button, { variant: "secondary", className: "min-h-11", onClick: () => setConfirmClear(true), children: "Clear" })] })] })), showKeyInput && (_jsx(Field, { id: keyId, label: replacing ? "New API key" : "API key", hint: replacing
|
|
151
145
|
? "The saved key stays in use until you press Save."
|
|
152
146
|
: undefined, error: keyError, children: _jsxs("div", { className: "flex items-center gap-2", children: [_jsx(PasswordInput, { id: keyId, ref: keyRef, className: "h-11 flex-1", value: draft.apiKey, placeholder: replacing ? "Paste the new key" : "Paste the key", autoComplete: "off", spellCheck: false, "aria-invalid": invalid("ApiKey"), "aria-describedby": describedBy(keyId, replacing, keyError !== undefined), onChange: (event) => edit("ApiKey", { apiKey: event.target.value }), onKeyDown: (event) => {
|
|
153
147
|
if (event.key === "Escape" && replacing) {
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { RemitImapClassificationConfigured, RemitImapClassificationField, RemitImapClassificationPreset, RemitImapClassificationPresetGroup, RemitImapClassificationPresetId, RemitImapClassificationSettings, RemitImapClassificationTestFailed, RemitImapUpdateUserSettingsInput } from "@remit/api-http-client/types.gen.ts";
|
|
1
|
+
import type { RemitImapClassificationConfigured, RemitImapClassificationField, RemitImapClassificationPreset, RemitImapClassificationPresetGroup, RemitImapClassificationPresetId, RemitImapClassificationSettings, RemitImapClassificationTestFailed, RemitImapClassificationUsage, RemitImapUpdateUserSettingsInput } from "@remit/api-http-client/types.gen.ts";
|
|
2
2
|
export declare const classificationQueryMeta: {
|
|
3
3
|
softErrorStatuses: number[];
|
|
4
4
|
};
|
|
@@ -14,7 +14,7 @@ export interface ProviderDraft {
|
|
|
14
14
|
apiKey: string;
|
|
15
15
|
}
|
|
16
16
|
export declare const draftFromSettings: (settings: RemitImapClassificationSettings) => ProviderDraft;
|
|
17
|
-
export declare const draftForPreset: (
|
|
17
|
+
export declare const draftForPreset: (preset: RemitImapClassificationPreset, settings: RemitImapClassificationSettings) => ProviderDraft;
|
|
18
18
|
export interface PresetGroup {
|
|
19
19
|
group: RemitImapClassificationPresetGroup;
|
|
20
20
|
label: string;
|
|
@@ -38,3 +38,15 @@ export declare const switchingFrom: (settings: RemitImapClassificationSettings,
|
|
|
38
38
|
export declare const testFailureHeadline: (failure: RemitImapClassificationTestFailed) => string;
|
|
39
39
|
export declare const classificationReportHref: (title: string, detail: string) => string;
|
|
40
40
|
export declare const presetNote: (preset: RemitImapClassificationPreset) => string;
|
|
41
|
+
export declare const LIMIT_INVALID = "Enter a whole number of 1 or more.";
|
|
42
|
+
export type DailyLimitInput = {
|
|
43
|
+
ok: true;
|
|
44
|
+
limit: number;
|
|
45
|
+
} | {
|
|
46
|
+
ok: false;
|
|
47
|
+
message: string;
|
|
48
|
+
};
|
|
49
|
+
export declare const parseDailyLimit: (value: string) => DailyLimitInput;
|
|
50
|
+
export declare const isCapped: (usage: RemitImapClassificationUsage) => boolean;
|
|
51
|
+
export declare const usageCost: (usage: RemitImapClassificationUsage, preset: RemitImapClassificationPreset | undefined) => string;
|
|
52
|
+
export declare const resetTime: (resetsAt: string) => string;
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { buildBugReportContext, buildGitHubIssueUrl } from "./bug-report";
|
|
2
2
|
import { getErrorStatus, ownsServerErrorsMeta, softErrorStatuses, } from "./error-classifier";
|
|
3
|
+
import { formatDate } from "./format";
|
|
3
4
|
export const classificationQueryMeta = softErrorStatuses(403);
|
|
4
5
|
export const classificationActionMeta = {
|
|
5
6
|
...softErrorStatuses(400, 403, 404, 409, 422, 429),
|
|
@@ -14,9 +15,20 @@ export const draftFromSettings = (settings) => settings.state === "Configured"
|
|
|
14
15
|
apiKey: "",
|
|
15
16
|
}
|
|
16
17
|
: { presetId: undefined, model: "", baseUrl: "", apiKey: "" };
|
|
17
|
-
export const draftForPreset = (
|
|
18
|
-
|
|
19
|
-
|
|
18
|
+
export const draftForPreset = (preset, settings) => {
|
|
19
|
+
const draft = settings.state === "Configured" && settings.presetId === preset.presetId
|
|
20
|
+
? draftFromSettings(settings)
|
|
21
|
+
: {
|
|
22
|
+
presetId: preset.presetId,
|
|
23
|
+
model: "",
|
|
24
|
+
baseUrl: "",
|
|
25
|
+
apiKey: "",
|
|
26
|
+
};
|
|
27
|
+
return {
|
|
28
|
+
...draft,
|
|
29
|
+
baseUrl: draft.baseUrl === "" ? (preset.defaultBaseUrl ?? "") : draft.baseUrl,
|
|
30
|
+
};
|
|
31
|
+
};
|
|
20
32
|
const GROUP_LABELS = {
|
|
21
33
|
Hosted: "Hosted",
|
|
22
34
|
Custom: "Your own endpoint",
|
|
@@ -104,3 +116,38 @@ export const presetNote = (preset) => {
|
|
|
104
116
|
}).format(preset.price.perMillionInputTokens);
|
|
105
117
|
return `${key} ${price} per million input tokens.`;
|
|
106
118
|
};
|
|
119
|
+
export const LIMIT_INVALID = "Enter a whole number of 1 or more.";
|
|
120
|
+
const INT32_MAX = 2_147_483_647;
|
|
121
|
+
export const parseDailyLimit = (value) => {
|
|
122
|
+
const trimmed = value.trim();
|
|
123
|
+
if (!/^\d+$/.test(trimmed))
|
|
124
|
+
return { ok: false, message: LIMIT_INVALID };
|
|
125
|
+
const limit = Number(trimmed);
|
|
126
|
+
if (limit < 1 || limit > INT32_MAX) {
|
|
127
|
+
return { ok: false, message: LIMIT_INVALID };
|
|
128
|
+
}
|
|
129
|
+
return { ok: true, limit };
|
|
130
|
+
};
|
|
131
|
+
export const isCapped = (usage) => usage.calls >= usage.dailyLimit;
|
|
132
|
+
export const usageCost = (usage, preset) => {
|
|
133
|
+
const { cost } = usage;
|
|
134
|
+
if (cost.state === "Priced") {
|
|
135
|
+
const format = new Intl.NumberFormat("en", {
|
|
136
|
+
style: "currency",
|
|
137
|
+
currency: cost.currency,
|
|
138
|
+
maximumFractionDigits: 2,
|
|
139
|
+
});
|
|
140
|
+
if (cost.amount > 0 && cost.amount < 0.01) {
|
|
141
|
+
return `under ${format.format(0.01)}`;
|
|
142
|
+
}
|
|
143
|
+
if (cost.amount === 0)
|
|
144
|
+
return format.format(0);
|
|
145
|
+
return `about ${format.format(cost.amount)}`;
|
|
146
|
+
}
|
|
147
|
+
return preset?.group === "Local" ? "no price, self-hosted" : "no price";
|
|
148
|
+
};
|
|
149
|
+
export const resetTime = (resetsAt) => formatDate(resetsAt, {
|
|
150
|
+
hour: "2-digit",
|
|
151
|
+
minute: "2-digit",
|
|
152
|
+
timeZoneName: "shortOffset",
|
|
153
|
+
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@remit/web-client",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.339",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Remit web client, published as composable primitives — the app shell, auth shells, and runtime config. A distributor imports what it composes and bundles it.",
|
|
6
6
|
"exports": {
|