@remit/web-client 0.0.341 → 0.0.342
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 +17 -6
- package/dist/src/components/settings/classification/DailyLimitCard.js +2 -2
- package/dist/src/components/settings/classification/ProviderCard.js +53 -32
- package/dist/src/lib/classification.d.ts +13 -2
- package/dist/src/lib/classification.js +59 -30
- package/package.json +1 -1
|
@@ -1,23 +1,34 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { jsxs as _jsxs, jsx as _jsx } from "react/jsx-runtime";
|
|
2
2
|
import { configOperationsGetConfigOptions } from "@remit/api-http-client/@tanstack/react-query.gen.ts";
|
|
3
|
+
import { ClassificationUnrunnableReason } from "@remit/domain-enums";
|
|
3
4
|
import { Badge, Banner, Button, Switch } from "@remit/ui";
|
|
4
5
|
import { useQuery } from "@tanstack/react-query";
|
|
5
6
|
import { useId, useRef, useState } from "react";
|
|
6
7
|
import { useClassificationSettings, useSaveClassification, } from "../../../hooks/useClassificationSettings";
|
|
7
|
-
import { isCapped, resetTime, usageCost } from "../../../lib/classification";
|
|
8
|
+
import { isCapped, resetTime, usageCost, withSourceNote, } from "../../../lib/classification";
|
|
8
9
|
import { DailyLimitCard } from "./DailyLimitCard";
|
|
9
10
|
import { FailureBanner } from "./FailureBanner";
|
|
10
11
|
import { ProviderCard } from "./ProviderCard";
|
|
11
12
|
const LOAD_FAILED = "Mail classification settings did not load";
|
|
13
|
+
const UNRUNNABLE_PIECE = {
|
|
14
|
+
[ClassificationUnrunnableReason.NoBaseUrl]: "a base URL",
|
|
15
|
+
[ClassificationUnrunnableReason.NoApiKey]: "an API key",
|
|
16
|
+
};
|
|
12
17
|
const switchNote = (settings) => {
|
|
13
18
|
if (settings.state === "NotConfigured") {
|
|
14
19
|
return "Save a provider below first. Until then, built-in rules label your mail.";
|
|
15
20
|
}
|
|
16
|
-
if (settings.
|
|
17
|
-
return "
|
|
21
|
+
if (settings.state === "Unrunnable") {
|
|
22
|
+
return "The provider cannot run yet. Built-in rules label your mail.";
|
|
18
23
|
}
|
|
19
|
-
|
|
24
|
+
const base = settings.enabled
|
|
25
|
+
? "New mail goes to the model. A failed call falls back to built-in rules."
|
|
26
|
+
: "Provider saved. Built-in rules label your mail until you switch this on.";
|
|
27
|
+
return withSourceNote(base, settings.sources.enabled);
|
|
20
28
|
};
|
|
29
|
+
function UnrunnableNotice({ settings, }) {
|
|
30
|
+
return (_jsxs(Banner, { tone: "warning", variant: "soft", children: [_jsxs("p", { className: "font-medium", children: ["Classification cannot run without ", UNRUNNABLE_PIECE[settings.reason]] }), _jsxs("p", { className: "mt-0.5 text-fg-muted", children: ["Add ", UNRUNNABLE_PIECE[settings.reason], " below and save. Until then, built-in rules label your mail."] })] }));
|
|
31
|
+
}
|
|
21
32
|
const namesOf = (accounts) => (accountId) => {
|
|
22
33
|
const account = accounts.find((entry) => entry.accountId === accountId);
|
|
23
34
|
return account?.displayName?.trim() || account?.email || accountId;
|
|
@@ -50,7 +61,7 @@ function StatusCard({ settings, presets, nameOf, onRaiseLimit, }) {
|
|
|
50
61
|
setRefusal(null);
|
|
51
62
|
toggle.mutate({ body: { classificationEnabled: !on } }, { onError: setRefusal });
|
|
52
63
|
};
|
|
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: {
|
|
64
|
+
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 })] }), settings.state === "Unrunnable" && (_jsx(UnrunnableNotice, { settings: settings })), 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: {
|
|
54
65
|
title: on
|
|
55
66
|
? "Classification was not switched off"
|
|
56
67
|
: "Classification was not switched on",
|
|
@@ -2,7 +2,7 @@ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
|
2
2
|
import { Button, Input } from "@remit/ui";
|
|
3
3
|
import { useId, useState } from "react";
|
|
4
4
|
import { useSaveClassification } from "../../../hooks/useClassificationSettings";
|
|
5
|
-
import { parseDailyLimit, resetTime } from "../../../lib/classification";
|
|
5
|
+
import { parseDailyLimit, resetTime, withSourceNote, } from "../../../lib/classification";
|
|
6
6
|
import { FailureBanner } from "./FailureBanner";
|
|
7
7
|
import { describedBy, Field } from "./Field";
|
|
8
8
|
export function DailyLimitCard({ settings, inputRef, onAnnounce, }) {
|
|
@@ -28,7 +28,7 @@ export function DailyLimitCard({ settings, inputRef, onAnnounce, }) {
|
|
|
28
28
|
onError: (cause) => setFailure({ title: "The daily limit was not saved", error: cause }),
|
|
29
29
|
});
|
|
30
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) => {
|
|
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: withSourceNote(`${reset}Past the limit, that account's new mail uses built-in rules.`, settings.sources.dailyLimit), 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
32
|
setValue(event.target.value);
|
|
33
33
|
setError(undefined);
|
|
34
34
|
onAnnounce("");
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { jsxs as _jsxs, jsx as _jsx, Fragment as _Fragment } from "react/jsx-runtime";
|
|
2
|
+
import { ClassificationValueSource } from "@remit/domain-enums";
|
|
2
3
|
import { Banner, Button, ConfirmDialog, Input, PasswordInput, Select, } from "@remit/ui";
|
|
3
4
|
import { useEffect, useId, useRef, useState } from "react";
|
|
4
5
|
import { useClearClassification, useSaveClassification, useTestClassification, } from "../../../hooks/useClassificationSettings";
|
|
5
|
-
import { classificationReportHref, draftForPreset, draftFromSettings, groupPresets,
|
|
6
|
-
import { formatDatePreset } from "../../../lib/format";
|
|
6
|
+
import { classificationReportHref, draftForPreset, draftFromSettings, effectiveValue, groupPresets, hasSavedProvider, isKeyRequiredRefusal, needsBaseUrl, PASTE_KEY, presetNote, providerSave, settingsSources, sourcesFor, switchingFrom, testFailureHeadline, withSourceNote, } from "../../../lib/classification";
|
|
7
7
|
import { ReportLink } from "../ReportLink";
|
|
8
8
|
import { FailureBanner } from "./FailureBanner";
|
|
9
9
|
import { describedBy, Field } from "./Field";
|
|
@@ -22,13 +22,11 @@ function TestOutcome({ result, presetLabel, onFix, shown, }) {
|
|
|
22
22
|
}
|
|
23
23
|
function PresetForm({ preset, settings, onAnnounce }) {
|
|
24
24
|
const save = useSaveClassification();
|
|
25
|
-
const clear = useClearClassification();
|
|
26
25
|
const test = useTestClassification();
|
|
27
26
|
const [draft, setDraft] = useState(() => draftForPreset(preset, settings));
|
|
28
27
|
const [fieldError, setFieldError] = useState(null);
|
|
29
28
|
const [testResult, setTestResult] = useState(null);
|
|
30
29
|
const [replacing, setReplacing] = useState(false);
|
|
31
|
-
const [confirmClear, setConfirmClear] = useState(false);
|
|
32
30
|
const [failure, setFailure] = useState(null);
|
|
33
31
|
const [focusTarget, setFocusTarget] = useState(null);
|
|
34
32
|
const modelRef = useRef(null);
|
|
@@ -50,10 +48,11 @@ function PresetForm({ preset, settings, onAnnounce }) {
|
|
|
50
48
|
};
|
|
51
49
|
refs[focusTarget.target].current?.focus();
|
|
52
50
|
}, [focusTarget]);
|
|
53
|
-
const
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
const
|
|
51
|
+
const sources = sourcesFor(settings, preset.presetId);
|
|
52
|
+
const keySource = sources?.apiKey ?? ClassificationValueSource.Default;
|
|
53
|
+
const keyInUse = preset.needsKey && keySource !== ClassificationValueSource.Default;
|
|
54
|
+
const keyInherited = keySource === ClassificationValueSource.Deployment;
|
|
55
|
+
const showKeyInput = preset.needsKey && (!keyInUse || replacing);
|
|
57
56
|
const shown = {
|
|
58
57
|
ApiKey: preset.needsKey,
|
|
59
58
|
Model: true,
|
|
@@ -80,7 +79,7 @@ function PresetForm({ preset, settings, onAnnounce }) {
|
|
|
80
79
|
focus("Replace");
|
|
81
80
|
};
|
|
82
81
|
const fixField = (field) => {
|
|
83
|
-
if (field === "ApiKey" &&
|
|
82
|
+
if (field === "ApiKey" && keyInUse) {
|
|
84
83
|
startReplace();
|
|
85
84
|
return;
|
|
86
85
|
}
|
|
@@ -90,7 +89,7 @@ function PresetForm({ preset, settings, onAnnounce }) {
|
|
|
90
89
|
const result = providerSave(draft, preset, settings);
|
|
91
90
|
if (!result.ok) {
|
|
92
91
|
setFieldError({ field: result.field, message: result.message });
|
|
93
|
-
|
|
92
|
+
fixField(result.field);
|
|
94
93
|
return;
|
|
95
94
|
}
|
|
96
95
|
setFailure(null);
|
|
@@ -104,7 +103,14 @@ function PresetForm({ preset, settings, onAnnounce }) {
|
|
|
104
103
|
if (wasReplacing)
|
|
105
104
|
focus("Replace");
|
|
106
105
|
},
|
|
107
|
-
onError: (error) =>
|
|
106
|
+
onError: (error) => {
|
|
107
|
+
if (isKeyRequiredRefusal(error)) {
|
|
108
|
+
setFieldError({ field: "ApiKey", message: PASTE_KEY });
|
|
109
|
+
fixField("ApiKey");
|
|
110
|
+
return;
|
|
111
|
+
}
|
|
112
|
+
setFailure({ title: `${preset.label} was not saved`, error });
|
|
113
|
+
},
|
|
108
114
|
});
|
|
109
115
|
};
|
|
110
116
|
const onTest = () => {
|
|
@@ -128,22 +134,14 @@ function PresetForm({ preset, settings, onAnnounce }) {
|
|
|
128
134
|
}),
|
|
129
135
|
});
|
|
130
136
|
};
|
|
131
|
-
const
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
setFailure({
|
|
136
|
-
title: `The ${preset.label} key was not cleared`,
|
|
137
|
-
error,
|
|
138
|
-
});
|
|
139
|
-
},
|
|
140
|
-
});
|
|
141
|
-
};
|
|
142
|
-
const enabled = settings.state === "Configured" && settings.enabled;
|
|
137
|
+
const modelPlaceholder = effectiveValue(settings, preset.presetId, "model") ?? preset.defaultModel;
|
|
138
|
+
const baseUrlPlaceholder = effectiveValue(settings, preset.presetId, "baseUrl") ??
|
|
139
|
+
preset.defaultBaseUrl ??
|
|
140
|
+
"http://localhost:11434/v1";
|
|
143
141
|
const keyError = errorFor("ApiKey");
|
|
144
|
-
return (_jsxs(_Fragment, { children: [_jsx(Field, { id: modelId, label: "Model", hint: `Empty uses ${
|
|
145
|
-
|
|
146
|
-
|
|
142
|
+
return (_jsxs(_Fragment, { children: [_jsx(Field, { id: modelId, label: "Model", hint: withSourceNote(`Empty uses ${modelPlaceholder}.`, sources?.model), error: errorFor("Model"), children: _jsx(Input, { id: modelId, ref: modelRef, className: "h-11", value: draft.model, placeholder: modelPlaceholder, 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: withSourceNote("Remit's server calls this URL.", sources?.baseUrl), error: errorFor("BaseUrl"), children: _jsx(Input, { id: baseUrlId, ref: baseUrlRef, className: "h-11", type: "url", inputMode: "url", value: draft.baseUrl, placeholder: baseUrlPlaceholder, 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 }) }) }), keyInUse && !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: [keyInherited ? (_jsx("span", { className: invalid("ApiKey")
|
|
143
|
+
? "text-sm text-danger"
|
|
144
|
+
: "text-sm text-fg-muted", children: "provided by the deployment" })) : (_jsx("span", { className: invalid("ApiKey") ? "text-sm text-danger" : "text-sm text-fg", children: "set" })), _jsx("span", { className: "flex-1" }), _jsx(Button, { ref: replaceRef, variant: "secondary", className: "min-h-11", "aria-invalid": invalid("ApiKey"), onClick: startReplace, children: keyInherited ? "Use your own key" : "Replace" })] })] })), showKeyInput && (_jsx(Field, { id: keyId, label: replacing ? "New API key" : "API key", hint: replacing ? "The key in use stays until you press Save." : 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) => {
|
|
147
145
|
if (event.key === "Escape" && replacing) {
|
|
148
146
|
event.preventDefault();
|
|
149
147
|
event.stopPropagation();
|
|
@@ -152,17 +150,40 @@ function PresetForm({ preset, settings, onAnnounce }) {
|
|
|
152
150
|
} }), replacing && (_jsx(Button, { variant: "ghost", className: "min-h-11", onClick: cancelReplace, children: "Cancel" }))] }) })), _jsxs("div", { className: "flex flex-wrap gap-2", children: [_jsx(Button, { variant: "primary", className: "min-h-11", "aria-disabled": save.isPending, onClick: () => {
|
|
153
151
|
if (!save.isPending)
|
|
154
152
|
onSave();
|
|
155
|
-
}, children: save.isPending ? "Saving…" : "Save" }), _jsx(Button, { variant: "secondary", className: "min-h-11", "aria-disabled": test.isPending, "aria-busy": test.isPending, onClick: onTest, children: test.isPending ? "Testing…" : "Test connection" })] }), _jsx("p", { "aria-live": "polite", className: "text-sm text-fg-muted empty:hidden", children: test.isPending ? `Sending one sample message to ${preset.label}` : "" }), testResult && (_jsx(TestOutcome, { result: testResult, presetLabel: preset.label, onFix: fixField, shown: shown })), failure && _jsx(FailureBanner, { failure: failure })
|
|
156
|
-
? `Classification is on. Clearing the key switches it off, and new mail goes back to built-in rules. To use ${preset.label} again you paste the key again.`
|
|
157
|
-
: `To use ${preset.label} again you paste the key again.`, confirmLabel: "Clear key", destructive: true, isBusy: clear.isPending, onConfirm: onClear, onCancel: () => setConfirmClear(false) })] }));
|
|
153
|
+
}, children: save.isPending ? "Saving…" : "Save" }), _jsx(Button, { variant: "secondary", className: "min-h-11", "aria-disabled": test.isPending, "aria-busy": test.isPending, onClick: onTest, children: test.isPending ? "Testing…" : "Test connection" })] }), _jsx("p", { "aria-live": "polite", className: "text-sm text-fg-muted empty:hidden", children: test.isPending ? `Sending one sample message to ${preset.label}` : "" }), testResult && (_jsx(TestOutcome, { result: testResult, presetLabel: preset.label, onFix: fixField, shown: shown })), failure && _jsx(FailureBanner, { failure: failure })] }));
|
|
158
154
|
}
|
|
159
155
|
export function ProviderCard({ presets, settings, onAnnounce, }) {
|
|
160
156
|
const [presetId, setPresetId] = useState(() => draftFromSettings(settings).presetId);
|
|
157
|
+
const clear = useClearClassification();
|
|
158
|
+
const [confirmClear, setConfirmClear] = useState(false);
|
|
159
|
+
const [cleared, setCleared] = useState(0);
|
|
160
|
+
const [clearFailure, setClearFailure] = useState(null);
|
|
161
161
|
const presetFieldId = useId();
|
|
162
162
|
const preset = presets.find((entry) => entry.presetId === presetId);
|
|
163
163
|
const from = switchingFrom(settings, presetId, presets);
|
|
164
|
-
|
|
165
|
-
|
|
164
|
+
const enabled = settings.state === "Configured" && settings.enabled;
|
|
165
|
+
const onClear = () => {
|
|
166
|
+
setClearFailure(null);
|
|
167
|
+
clear.mutate({}, {
|
|
168
|
+
onSuccess: (response) => {
|
|
169
|
+
setConfirmClear(false);
|
|
170
|
+
setPresetId(draftFromSettings(response.classification).presetId);
|
|
171
|
+
setCleared((count) => count + 1);
|
|
172
|
+
},
|
|
173
|
+
onError: (error) => {
|
|
174
|
+
setConfirmClear(false);
|
|
175
|
+
setClearFailure({
|
|
176
|
+
title: "The provider settings were not cleared",
|
|
177
|
+
error,
|
|
178
|
+
});
|
|
179
|
+
},
|
|
180
|
+
});
|
|
181
|
+
};
|
|
182
|
+
return (_jsxs("section", { className: "space-y-4", children: [_jsxs("div", { className: "flex min-h-11 items-center gap-3", children: [_jsx("h2", { className: "flex-1 text-xs font-semibold uppercase tracking-wider text-fg-subtle", children: "Provider" }), hasSavedProvider(settings) && (_jsx(Button, { variant: "secondary", className: "min-h-11", onClick: () => setConfirmClear(true), children: "Clear" }))] }), clearFailure && _jsx(FailureBanner, { failure: clearFailure }), _jsx(Field, { id: presetFieldId, label: "Provider", hint: preset ? presetNote(preset) : undefined, children: _jsxs(Select, { id: presetFieldId, className: "h-11", value: presetId ?? "", onChange: (event) => setPresetId(presets.find((entry) => entry.presetId === event.target.value)
|
|
183
|
+
?.presetId), "aria-describedby": describedBy(presetFieldId, preset !== undefined, false), children: [_jsx("option", { value: "", disabled: true, children: "Choose a provider" }), groupPresets(presets).map((group) => (_jsx("optgroup", { label: group.label, children: group.presets.map((entry) => (_jsx("option", { value: entry.presetId, children: entry.label }, entry.presetId))) }, group.group)))] }) }), from && (_jsxs("p", { className: "text-sm text-warning", children: ["Saving switches from ", from.label, settingsSources(settings).sources?.apiKey ===
|
|
184
|
+
ClassificationValueSource.Saved
|
|
166
185
|
? " and deletes its saved key."
|
|
167
|
-
: "."] })), preset && (_jsx(PresetForm, { preset: preset, settings: settings, onAnnounce: onAnnounce }, preset.presetId))
|
|
186
|
+
: "."] })), preset && (_jsx(PresetForm, { preset: preset, settings: settings, onAnnounce: onAnnounce }, `${preset.presetId}-${cleared}`)), _jsx(ConfirmDialog, { isOpen: confirmClear, title: "Clear the provider settings?", description: enabled
|
|
187
|
+
? "Classification is on. Clearing the provider switches it off, and new mail goes back to built-in rules. Every field returns to the deployment default."
|
|
188
|
+
: "Clearing the provider returns every field to the deployment default.", confirmLabel: "Clear", destructive: true, isBusy: clear.isPending, onConfirm: onClear, onCancel: () => setConfirmClear(false) })] }));
|
|
168
189
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { RemitImapClassificationField, RemitImapClassificationPreset, RemitImapClassificationPresetGroup, RemitImapClassificationPresetId, RemitImapClassificationSettings, RemitImapClassificationSources, RemitImapClassificationTestFailed, RemitImapClassificationUsage, RemitImapClassificationValueSource, RemitImapUpdateUserSettingsInput } from "@remit/api-http-client/types.gen.ts";
|
|
2
2
|
export declare const classificationQueryMeta: {
|
|
3
3
|
softErrorStatuses: number[];
|
|
4
4
|
};
|
|
@@ -13,8 +13,17 @@ export interface ProviderDraft {
|
|
|
13
13
|
baseUrl: string;
|
|
14
14
|
apiKey: string;
|
|
15
15
|
}
|
|
16
|
+
export interface SettingsSources {
|
|
17
|
+
presetId: RemitImapClassificationPresetId | undefined;
|
|
18
|
+
sources: RemitImapClassificationSources | undefined;
|
|
19
|
+
}
|
|
20
|
+
export declare const settingsSources: (settings: RemitImapClassificationSettings) => SettingsSources;
|
|
21
|
+
export declare const sourcesFor: (settings: RemitImapClassificationSettings, presetId: RemitImapClassificationPresetId) => RemitImapClassificationSources | undefined;
|
|
22
|
+
type ProviderValueField = "model" | "baseUrl";
|
|
23
|
+
export declare const effectiveValue: (settings: RemitImapClassificationSettings, presetId: RemitImapClassificationPresetId, field: ProviderValueField) => string | undefined;
|
|
16
24
|
export declare const draftFromSettings: (settings: RemitImapClassificationSettings) => ProviderDraft;
|
|
17
25
|
export declare const draftForPreset: (preset: RemitImapClassificationPreset, settings: RemitImapClassificationSettings) => ProviderDraft;
|
|
26
|
+
export declare const hasSavedProvider: (settings: RemitImapClassificationSettings) => boolean;
|
|
18
27
|
export interface PresetGroup {
|
|
19
28
|
group: RemitImapClassificationPresetGroup;
|
|
20
29
|
label: string;
|
|
@@ -22,7 +31,7 @@ export interface PresetGroup {
|
|
|
22
31
|
}
|
|
23
32
|
export declare const groupPresets: (presets: readonly RemitImapClassificationPreset[]) => PresetGroup[];
|
|
24
33
|
export declare const needsBaseUrl: (preset: RemitImapClassificationPreset) => boolean;
|
|
25
|
-
export declare const
|
|
34
|
+
export declare const withSourceNote: (text: string, source: RemitImapClassificationValueSource | undefined) => string;
|
|
26
35
|
export type ProviderSave = {
|
|
27
36
|
ok: true;
|
|
28
37
|
body: RemitImapUpdateUserSettingsInput;
|
|
@@ -32,6 +41,7 @@ export type ProviderSave = {
|
|
|
32
41
|
message: string;
|
|
33
42
|
};
|
|
34
43
|
export declare const PASTE_KEY = "Paste an API key";
|
|
44
|
+
export declare const isKeyRequiredRefusal: (error: unknown) => boolean;
|
|
35
45
|
export declare const BAD_URL = "Enter a URL that starts with http:// or https://";
|
|
36
46
|
export declare const providerSave: (draft: ProviderDraft, preset: RemitImapClassificationPreset, settings: RemitImapClassificationSettings) => ProviderSave;
|
|
37
47
|
export declare const switchingFrom: (settings: RemitImapClassificationSettings, presetId: RemitImapClassificationPresetId | undefined, presets: readonly RemitImapClassificationPreset[]) => RemitImapClassificationPreset | undefined;
|
|
@@ -50,3 +60,4 @@ export declare const parseDailyLimit: (value: string) => DailyLimitInput;
|
|
|
50
60
|
export declare const isCapped: (usage: RemitImapClassificationUsage) => boolean;
|
|
51
61
|
export declare const usageCost: (usage: RemitImapClassificationUsage, preset: RemitImapClassificationPreset | undefined) => string;
|
|
52
62
|
export declare const resetTime: (resetsAt: string) => string;
|
|
63
|
+
export {};
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { ClassificationValueSource } from "@remit/domain-enums";
|
|
2
|
+
import { codedApiErrorBody } from "./api";
|
|
1
3
|
import { buildBugReportContext, buildGitHubIssueUrl } from "./bug-report";
|
|
2
4
|
import { getErrorStatus, ownsServerErrorsMeta, softErrorStatuses, } from "./error-classifier";
|
|
3
5
|
import { formatDate } from "./format";
|
|
@@ -7,27 +9,43 @@ export const classificationActionMeta = {
|
|
|
7
9
|
...ownsServerErrorsMeta,
|
|
8
10
|
};
|
|
9
11
|
export const isClassificationSignedOut = (error) => getErrorStatus(error) === 403;
|
|
10
|
-
export const
|
|
11
|
-
|
|
12
|
-
presetId:
|
|
13
|
-
model: settings.model,
|
|
14
|
-
baseUrl: settings.baseUrl,
|
|
15
|
-
apiKey: "",
|
|
12
|
+
export const settingsSources = (settings) => {
|
|
13
|
+
if (settings.state === "NotConfigured") {
|
|
14
|
+
return { presetId: undefined, sources: undefined };
|
|
16
15
|
}
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
16
|
+
if (settings.state === "Unrunnable") {
|
|
17
|
+
return { presetId: settings.presetId, sources: undefined };
|
|
18
|
+
}
|
|
19
|
+
return { presetId: settings.presetId, sources: settings.sources };
|
|
20
|
+
};
|
|
21
|
+
export const sourcesFor = (settings, presetId) => {
|
|
22
|
+
const current = settingsSources(settings);
|
|
23
|
+
return current.presetId === presetId ? current.sources : undefined;
|
|
24
|
+
};
|
|
25
|
+
const savedValue = (settings, field) => settings.state === "Configured" &&
|
|
26
|
+
settings.sources[field] === ClassificationValueSource.Saved
|
|
27
|
+
? settings[field]
|
|
28
|
+
: "";
|
|
29
|
+
export const effectiveValue = (settings, presetId, field) => settings.state === "Configured" &&
|
|
30
|
+
settings.presetId === presetId &&
|
|
31
|
+
settings[field] !== ""
|
|
32
|
+
? settings[field]
|
|
33
|
+
: undefined;
|
|
34
|
+
export const draftFromSettings = (settings) => ({
|
|
35
|
+
presetId: settingsSources(settings).presetId,
|
|
36
|
+
model: savedValue(settings, "model"),
|
|
37
|
+
baseUrl: savedValue(settings, "baseUrl"),
|
|
38
|
+
apiKey: "",
|
|
39
|
+
});
|
|
40
|
+
export const draftForPreset = (preset, settings) => settingsSources(settings).presetId === preset.presetId
|
|
41
|
+
? draftFromSettings(settings)
|
|
42
|
+
: { presetId: preset.presetId, model: "", baseUrl: "", apiKey: "" };
|
|
43
|
+
export const hasSavedProvider = (settings) => {
|
|
44
|
+
if (settings.state === "Unrunnable")
|
|
45
|
+
return true;
|
|
46
|
+
if (settings.state === "NotConfigured")
|
|
47
|
+
return false;
|
|
48
|
+
return Object.values(settings.sources).includes(ClassificationValueSource.Saved);
|
|
31
49
|
};
|
|
32
50
|
const GROUP_LABELS = {
|
|
33
51
|
Hosted: "Hosted",
|
|
@@ -51,10 +69,17 @@ export const groupPresets = (presets) => {
|
|
|
51
69
|
return groups;
|
|
52
70
|
};
|
|
53
71
|
export const needsBaseUrl = (preset) => preset.defaultBaseUrl === undefined;
|
|
54
|
-
export const
|
|
55
|
-
|
|
56
|
-
|
|
72
|
+
export const withSourceNote = (text, source) => {
|
|
73
|
+
if (source === ClassificationValueSource.Saved) {
|
|
74
|
+
return `${text} Saved by you.`;
|
|
75
|
+
}
|
|
76
|
+
if (source === ClassificationValueSource.Deployment) {
|
|
77
|
+
return `${text} Provided by the deployment.`;
|
|
78
|
+
}
|
|
79
|
+
return text;
|
|
80
|
+
};
|
|
57
81
|
export const PASTE_KEY = "Paste an API key";
|
|
82
|
+
export const isKeyRequiredRefusal = (error) => codedApiErrorBody(error)?.code === "ClassificationApiKeyRequired";
|
|
58
83
|
export const BAD_URL = "Enter a URL that starts with http:// or https://";
|
|
59
84
|
const isHttpUrl = (value) => {
|
|
60
85
|
if (!URL.canParse(value))
|
|
@@ -65,18 +90,22 @@ const isHttpUrl = (value) => {
|
|
|
65
90
|
export const providerSave = (draft, preset, settings) => {
|
|
66
91
|
const baseUrl = draft.baseUrl.trim();
|
|
67
92
|
const apiKey = draft.apiKey.trim();
|
|
93
|
+
const sources = sourcesFor(settings, preset.presetId);
|
|
68
94
|
if (baseUrl !== "" && !isHttpUrl(baseUrl)) {
|
|
69
95
|
return { ok: false, field: "BaseUrl", message: BAD_URL };
|
|
70
96
|
}
|
|
71
|
-
if (baseUrl === "" &&
|
|
97
|
+
if (baseUrl === "" &&
|
|
98
|
+
needsBaseUrl(preset) &&
|
|
99
|
+
effectiveValue(settings, preset.presetId, "baseUrl") === undefined) {
|
|
72
100
|
return { ok: false, field: "BaseUrl", message: BAD_URL };
|
|
73
101
|
}
|
|
102
|
+
const keySource = sources?.apiKey ?? ClassificationValueSource.Default;
|
|
74
103
|
if (preset.needsKey &&
|
|
75
104
|
apiKey === "" &&
|
|
76
|
-
|
|
105
|
+
keySource === ClassificationValueSource.Default) {
|
|
77
106
|
return { ok: false, field: "ApiKey", message: PASTE_KEY };
|
|
78
107
|
}
|
|
79
|
-
const switching = settings.
|
|
108
|
+
const switching = settingsSources(settings).presetId !== preset.presetId;
|
|
80
109
|
return {
|
|
81
110
|
ok: true,
|
|
82
111
|
body: {
|
|
@@ -90,12 +119,12 @@ export const providerSave = (draft, preset, settings) => {
|
|
|
90
119
|
};
|
|
91
120
|
};
|
|
92
121
|
export const switchingFrom = (settings, presetId, presets) => {
|
|
93
|
-
|
|
122
|
+
const current = settingsSources(settings).presetId;
|
|
123
|
+
if (current === undefined || presetId === undefined)
|
|
94
124
|
return undefined;
|
|
95
|
-
|
|
96
|
-
if (settings.presetId === presetId)
|
|
125
|
+
if (current === presetId)
|
|
97
126
|
return undefined;
|
|
98
|
-
return presets.find((preset) => preset.presetId ===
|
|
127
|
+
return presets.find((preset) => preset.presetId === current);
|
|
99
128
|
};
|
|
100
129
|
export const testFailureHeadline = (failure) => {
|
|
101
130
|
const answer = failure.status === undefined ? failure.kind : `${failure.status}`;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@remit/web-client",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.342",
|
|
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": {
|