@kaperkunde/tokens 0.0.0-stage → 0.1.0
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/README.md +415 -2
- package/dist/browser-api.d.mts +56 -0
- package/dist/browser-api.mjs +75 -0
- package/dist/errors.d.mts +41 -0
- package/dist/errors.mjs +97 -0
- package/dist/index.d.mts +5 -0
- package/dist/index.mjs +5 -0
- package/dist/react/autorefill.d.mts +21 -0
- package/dist/react/autorefill.mjs +269 -0
- package/dist/react/balance.d.mts +23 -0
- package/dist/react/balance.mjs +116 -0
- package/dist/react/buy.d.mts +27 -0
- package/dist/react/buy.mjs +175 -0
- package/dist/react/history.d.mts +23 -0
- package/dist/react/history.mjs +102 -0
- package/dist/react/hooks.d.mts +52 -0
- package/dist/react/hooks.mjs +150 -0
- package/dist/react/index.d.mts +11 -0
- package/dist/react/index.mjs +10 -0
- package/dist/react/labels.d.mts +114 -0
- package/dist/react/labels.mjs +125 -0
- package/dist/react/own-model.d.mts +31 -0
- package/dist/react/own-model.mjs +358 -0
- package/dist/react/ui.d.mts +61 -0
- package/dist/react/ui.mjs +128 -0
- package/dist/react/usage.d.mts +21 -0
- package/dist/react/usage.mjs +137 -0
- package/dist/server/client.d.mts +76 -0
- package/dist/server/client.mjs +137 -0
- package/dist/server/error-of.d.mts +12 -0
- package/dist/server/error-of.mjs +63 -0
- package/dist/server/gateway.d.mts +62 -0
- package/dist/server/gateway.mjs +110 -0
- package/dist/server/index.d.mts +7 -0
- package/dist/server/index.mjs +7 -0
- package/dist/server/node.d.mts +23 -0
- package/dist/server/node.mjs +81 -0
- package/dist/server/routes.d.mts +56 -0
- package/dist/server/routes.mjs +257 -0
- package/dist/server/webhook.d.mts +47 -0
- package/dist/server/webhook.mjs +99 -0
- package/dist/styles.css +380 -0
- package/dist/types.d.mts +635 -0
- package/dist/types.mjs +142 -0
- package/dist/units.d.mts +37 -0
- package/dist/units.mjs +76 -0
- package/package.json +73 -4
- package/stub/cli.mjs +8 -0
- package/stub/server.d.mts +36 -0
- package/stub/server.mjs +1385 -0
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
// src/react/labels.ts
|
|
2
|
+
/** Every English string the components draw. Override any through `labels` on a component or the provider. */
|
|
3
|
+
const DEFAULT_LABELS = {
|
|
4
|
+
loading: "Loading…",
|
|
5
|
+
retry: "Try again",
|
|
6
|
+
unavailable: "Tokens are unavailable right now. Try again in a moment.",
|
|
7
|
+
somethingWrong: "Something went wrong.",
|
|
8
|
+
stillNeeded: "Still needed: {fields}.",
|
|
9
|
+
balance: "Balance",
|
|
10
|
+
balanceUnknown: "Balance unavailable",
|
|
11
|
+
purchased: "Bought",
|
|
12
|
+
fromGrants: "From drops and grants",
|
|
13
|
+
expiring: "{amount} expire {date}",
|
|
14
|
+
shortfall: "You owe {amount} from your last calls; it comes out of your next purchase.",
|
|
15
|
+
feeNote: "A token buys a little less than a dollar of model use.",
|
|
16
|
+
empty: "Out of tokens",
|
|
17
|
+
buyMore: "Buy tokens",
|
|
18
|
+
buyTitle: "Buy tokens",
|
|
19
|
+
amount: "Amount",
|
|
20
|
+
otherAmount: "Other amount",
|
|
21
|
+
currency: "Currency",
|
|
22
|
+
saveCard: "Save my card for auto-refill",
|
|
23
|
+
buyButton: "Buy {amount}",
|
|
24
|
+
buyButtonBare: "Buy",
|
|
25
|
+
buying: "Opening checkout…",
|
|
26
|
+
buys: "{money} buys {tokens}.",
|
|
27
|
+
tax: {
|
|
28
|
+
inclusive: "Price includes VAT.",
|
|
29
|
+
exclusive: "Before sales tax; tax is added at checkout."
|
|
30
|
+
},
|
|
31
|
+
minimum: "The smallest purchase is {money}.",
|
|
32
|
+
maximum: "The largest purchase is {money}.",
|
|
33
|
+
paymentsUnavailable: "Buying is not available right now.",
|
|
34
|
+
autoRefillTitle: "Auto-refill",
|
|
35
|
+
autoRefillOn: "Refill automatically",
|
|
36
|
+
autoRefillHelp: "When your bought tokens fall below the threshold, your card is charged for the amount below, up to the monthly maximum.",
|
|
37
|
+
threshold: "When bought tokens fall below",
|
|
38
|
+
refillAmount: "Refill amount",
|
|
39
|
+
monthlyMax: "Most per month",
|
|
40
|
+
card: "Card",
|
|
41
|
+
noCard: "No card saved",
|
|
42
|
+
addCard: "Add a card",
|
|
43
|
+
replaceCard: "Replace card",
|
|
44
|
+
removeCard: "Remove card",
|
|
45
|
+
needCardFirst: "Save a card first, then turn auto-refill on.",
|
|
46
|
+
autoRefillFailed: "Your last refill could not be charged, so auto-refill was turned off. Check your card and turn it back on.",
|
|
47
|
+
save: "Save",
|
|
48
|
+
saving: "Saving…",
|
|
49
|
+
saved: "Saved.",
|
|
50
|
+
usageTitle: "Recent usage",
|
|
51
|
+
usageEmpty: "Nothing used yet.",
|
|
52
|
+
cached: "{percent}% cached",
|
|
53
|
+
tokensInOut: "{input} in · {output} out",
|
|
54
|
+
yourOwn: "your own model",
|
|
55
|
+
loadMore: "Show more",
|
|
56
|
+
totals: "{count} calls · {amount} used",
|
|
57
|
+
failedCall: "failed",
|
|
58
|
+
historyTitle: "Purchases and grants",
|
|
59
|
+
historyEmpty: "Nothing yet.",
|
|
60
|
+
purchase: "Purchase",
|
|
61
|
+
refill: "Auto-refill",
|
|
62
|
+
grant: "Grant",
|
|
63
|
+
airdrop: "Drop",
|
|
64
|
+
expiredLot: "Expired",
|
|
65
|
+
adjustment: "Adjustment",
|
|
66
|
+
statusOpen: "waiting for payment",
|
|
67
|
+
statusFailed: "failed",
|
|
68
|
+
statusExpired: "expired",
|
|
69
|
+
ownTitle: "Your own model",
|
|
70
|
+
baseUrl: "Address",
|
|
71
|
+
baseUrlHelp: "An OpenAI-compatible address that does not include /chat/completions.",
|
|
72
|
+
apiKey: "Key",
|
|
73
|
+
apiKeyHelp: "Stored sealed and never shown again.",
|
|
74
|
+
apiKeySaved: "Saved ({hint}). Leave empty to keep it.",
|
|
75
|
+
removeKey: "Remove the saved key",
|
|
76
|
+
models: "Models",
|
|
77
|
+
modelsHelp: "One per line.",
|
|
78
|
+
defaultModel: "Model to use when the one asked for is not listed",
|
|
79
|
+
capabilities: "What it can do",
|
|
80
|
+
limit: "Most tokens a month",
|
|
81
|
+
limitHelp: "Leave empty for no limit.",
|
|
82
|
+
order: "Which first",
|
|
83
|
+
orders: {
|
|
84
|
+
tokens_then_own: "Tokens first, then my own",
|
|
85
|
+
own_then_tokens: "My own first, then tokens",
|
|
86
|
+
tokens_only: "Tokens only",
|
|
87
|
+
own_only: "My own only"
|
|
88
|
+
},
|
|
89
|
+
toolChoice: "When it is given tools",
|
|
90
|
+
toolChoices: {
|
|
91
|
+
auto: "Let the model decide",
|
|
92
|
+
required: "Require it to call one"
|
|
93
|
+
},
|
|
94
|
+
toolChoiceHelp: "Some models only call a tool when told to; choose Require if yours answers in prose instead.",
|
|
95
|
+
orderNeedsOwn: "Save your own model first to use it.",
|
|
96
|
+
test: "Test it",
|
|
97
|
+
testing: "Testing…",
|
|
98
|
+
testOk: "It answered in {ms} ms.",
|
|
99
|
+
testFailed: "It did not answer: {message}",
|
|
100
|
+
remove: "Remove",
|
|
101
|
+
used: "{used} of {limit} used this month.",
|
|
102
|
+
usedNoLimit: "{used} used this month.",
|
|
103
|
+
capability: {
|
|
104
|
+
tools: "Tools",
|
|
105
|
+
streaming: "Streaming",
|
|
106
|
+
vision: "Images",
|
|
107
|
+
json: "JSON",
|
|
108
|
+
voice: "Voice"
|
|
109
|
+
}
|
|
110
|
+
};
|
|
111
|
+
/** Replace `{name}` placeholders. */
|
|
112
|
+
const fill = (template, values) => template.replace(/\{(\w+)\}/g, (_, k) => String(values[k] ?? ""));
|
|
113
|
+
function mergeLabels(...layers) {
|
|
114
|
+
const out = { ...DEFAULT_LABELS };
|
|
115
|
+
for (const layer of layers) for (const [key, value] of Object.entries(layer ?? {})) {
|
|
116
|
+
if (value === void 0) continue;
|
|
117
|
+
const base = out[key];
|
|
118
|
+
out[key] = typeof value === "object" && typeof base === "object" ? {
|
|
119
|
+
...base,
|
|
120
|
+
...value
|
|
121
|
+
} : value;
|
|
122
|
+
}
|
|
123
|
+
return out;
|
|
124
|
+
}
|
|
125
|
+
export { DEFAULT_LABELS, fill, mergeLabels };
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { BackendOrder, OwnEndpoint, OwnEndpointInput, OwnEndpointTest } from "../types.mjs";
|
|
2
|
+
import { LabelOverrides } from "./labels.mjs";
|
|
3
|
+
import { Styling } from "./ui.mjs";
|
|
4
|
+
export type BackendOrderSwitchProps = Styling & {
|
|
5
|
+
order: BackendOrder;
|
|
6
|
+
onChange: (order: BackendOrder) => void | Promise<unknown>;
|
|
7
|
+
/** Whether the user has saved an own endpoint; without one only "tokens only" is accepted. */
|
|
8
|
+
hasOwn: boolean;
|
|
9
|
+
labels?: LabelOverrides;
|
|
10
|
+
};
|
|
11
|
+
/** Which backend comes first: tokens, the user's own model, one only, or both in an order. */
|
|
12
|
+
export declare function BackendOrderSwitch(props: BackendOrderSwitchProps): import("react").JSX.Element;
|
|
13
|
+
export type OwnModelFormProps = Styling & {
|
|
14
|
+
/** The saved endpoint (`null`: none); loaded when `undefined`. */
|
|
15
|
+
endpoint?: OwnEndpoint | null;
|
|
16
|
+
onSave?: (input: OwnEndpointInput) => Promise<OwnEndpoint>;
|
|
17
|
+
onTest?: () => Promise<OwnEndpointTest>;
|
|
18
|
+
onRemove?: () => Promise<unknown>;
|
|
19
|
+
onOrder?: (order: BackendOrder) => Promise<unknown>;
|
|
20
|
+
/** Called with the endpoint after a save. */
|
|
21
|
+
onSaved?: (endpoint: OwnEndpoint) => void;
|
|
22
|
+
labels?: LabelOverrides;
|
|
23
|
+
basePath?: string;
|
|
24
|
+
};
|
|
25
|
+
/**
|
|
26
|
+
* The user's own OpenAI-compatible endpoint: address, key (write-only), models,
|
|
27
|
+
* what it can do, a monthly limit, which backend comes first, and a test.
|
|
28
|
+
*/
|
|
29
|
+
export declare function OwnModelForm(props: OwnModelFormProps): import("react").JSX.Element;
|
|
30
|
+
/** The same form under the contract's name for the endpoint. */
|
|
31
|
+
export declare const OwnEndpointForm: typeof OwnModelForm;
|
|
@@ -0,0 +1,358 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { formatCompact } from "../units.mjs";
|
|
3
|
+
import { refreshTokens, useLabels, useOwnEndpoint, useTokenApi } from "./hooks.mjs";
|
|
4
|
+
import { fill } from "./labels.mjs";
|
|
5
|
+
import { Field, Notice, Panel, Segmented, part, stillNeeded, useAction } from "./ui.mjs";
|
|
6
|
+
import { useEffect, useState } from "react";
|
|
7
|
+
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
|
|
8
|
+
// src/react/own-model.tsx
|
|
9
|
+
const ORDERS = [
|
|
10
|
+
"tokens_then_own",
|
|
11
|
+
"own_then_tokens",
|
|
12
|
+
"tokens_only",
|
|
13
|
+
"own_only"
|
|
14
|
+
];
|
|
15
|
+
const CAPABILITIES = [
|
|
16
|
+
"tools",
|
|
17
|
+
"streaming",
|
|
18
|
+
"vision",
|
|
19
|
+
"json",
|
|
20
|
+
"voice"
|
|
21
|
+
];
|
|
22
|
+
/** Which backend comes first: tokens, the user's own model, one only, or both in an order. */
|
|
23
|
+
function BackendOrderSwitch(props) {
|
|
24
|
+
const labels = useLabels(props.labels);
|
|
25
|
+
const [problem, setProblem] = useState(null);
|
|
26
|
+
return /* @__PURE__ */ jsxs(Fragment, { children: [/* @__PURE__ */ jsx(Segmented, {
|
|
27
|
+
label: labels.order,
|
|
28
|
+
value: props.order,
|
|
29
|
+
options: ORDERS.map((o) => ({
|
|
30
|
+
value: o,
|
|
31
|
+
label: labels.orders[o]
|
|
32
|
+
})),
|
|
33
|
+
onChange: (order) => {
|
|
34
|
+
if (!props.hasOwn && order !== "tokens_only") return setProblem(labels.orderNeedsOwn);
|
|
35
|
+
setProblem(null);
|
|
36
|
+
props.onChange(order);
|
|
37
|
+
},
|
|
38
|
+
styling: props
|
|
39
|
+
}), problem ? /* @__PURE__ */ jsx(Notice, {
|
|
40
|
+
kind: "info",
|
|
41
|
+
styling: props,
|
|
42
|
+
children: problem
|
|
43
|
+
}) : null] });
|
|
44
|
+
}
|
|
45
|
+
const lines = (text) => [...new Set(text.split(/[\n,]/).map((s) => s.trim()).filter(Boolean))];
|
|
46
|
+
/**
|
|
47
|
+
* The user's own OpenAI-compatible endpoint: address, key (write-only), models,
|
|
48
|
+
* what it can do, a monthly limit, which backend comes first, and a test.
|
|
49
|
+
*/
|
|
50
|
+
function OwnModelForm(props) {
|
|
51
|
+
const labels = useLabels(props.labels);
|
|
52
|
+
const api = useTokenApi(props.basePath);
|
|
53
|
+
const styling = props;
|
|
54
|
+
const loaded = useOwnEndpoint({
|
|
55
|
+
basePath: props.basePath,
|
|
56
|
+
skip: props.endpoint !== void 0
|
|
57
|
+
});
|
|
58
|
+
const endpoint = props.endpoint !== void 0 ? props.endpoint : loaded.data;
|
|
59
|
+
const [baseUrl, setBaseUrl] = useState("");
|
|
60
|
+
const [apiKey, setApiKey] = useState("");
|
|
61
|
+
const [removeKey, setRemoveKey] = useState(false);
|
|
62
|
+
const [models, setModels] = useState("");
|
|
63
|
+
const [defaultModel, setDefaultModel] = useState("");
|
|
64
|
+
const [caps, setCaps] = useState(["streaming"]);
|
|
65
|
+
const [limit, setLimit] = useState("");
|
|
66
|
+
const [toolChoice, setToolChoice] = useState("auto");
|
|
67
|
+
const [order, setOrder] = useState("own_then_tokens");
|
|
68
|
+
const [seeded, setSeeded] = useState(void 0);
|
|
69
|
+
const [problem, setProblem] = useState(null);
|
|
70
|
+
const [done, setDone] = useState(false);
|
|
71
|
+
const [test, setTest] = useState(null);
|
|
72
|
+
const save = useAction(labels);
|
|
73
|
+
const testing = useAction(labels);
|
|
74
|
+
const removing = useAction(labels);
|
|
75
|
+
useEffect(() => {
|
|
76
|
+
if (endpoint === void 0 || endpoint === seeded) return;
|
|
77
|
+
setSeeded(endpoint);
|
|
78
|
+
if (endpoint) {
|
|
79
|
+
setBaseUrl(endpoint.baseUrl);
|
|
80
|
+
setModels(endpoint.models.join("\n"));
|
|
81
|
+
setDefaultModel(endpoint.defaultModel);
|
|
82
|
+
setCaps(endpoint.capabilities);
|
|
83
|
+
setToolChoice(endpoint.toolChoice ?? "auto");
|
|
84
|
+
setLimit(endpoint.monthlyTokenLimit === null ? "" : String(endpoint.monthlyTokenLimit));
|
|
85
|
+
setOrder(endpoint.order);
|
|
86
|
+
setTest(endpoint.lastTest);
|
|
87
|
+
}
|
|
88
|
+
}, [endpoint, seeded]);
|
|
89
|
+
async function submit(event) {
|
|
90
|
+
event.preventDefault();
|
|
91
|
+
setDone(false);
|
|
92
|
+
setProblem(null);
|
|
93
|
+
const modelList = lines(models);
|
|
94
|
+
const need = stillNeeded(labels, [{
|
|
95
|
+
label: labels.baseUrl,
|
|
96
|
+
filled: baseUrl.trim() !== ""
|
|
97
|
+
}, {
|
|
98
|
+
label: labels.models,
|
|
99
|
+
filled: modelList.length > 0
|
|
100
|
+
}]);
|
|
101
|
+
if (need) return setProblem(need);
|
|
102
|
+
const input = {
|
|
103
|
+
baseUrl: baseUrl.trim(),
|
|
104
|
+
models: modelList,
|
|
105
|
+
defaultModel: modelList.includes(defaultModel) ? defaultModel : modelList[0],
|
|
106
|
+
capabilities: caps,
|
|
107
|
+
toolChoice,
|
|
108
|
+
monthlyTokenLimit: limit.trim() === "" ? null : Math.max(0, Math.floor(Number(limit))),
|
|
109
|
+
order,
|
|
110
|
+
...removeKey ? { apiKey: null } : apiKey ? { apiKey } : {}
|
|
111
|
+
};
|
|
112
|
+
const saved = await save.run(() => props.onSave ? props.onSave(input) : api.saveOwnEndpoint(input));
|
|
113
|
+
if (saved) {
|
|
114
|
+
setApiKey("");
|
|
115
|
+
setRemoveKey(false);
|
|
116
|
+
setDone(true);
|
|
117
|
+
setSeeded(saved);
|
|
118
|
+
props.onSaved?.(saved);
|
|
119
|
+
loaded.refresh();
|
|
120
|
+
refreshTokens();
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
async function runTest() {
|
|
124
|
+
setTest(null);
|
|
125
|
+
const out = await testing.run(() => props.onTest ? props.onTest() : api.testOwnEndpoint());
|
|
126
|
+
if (out) setTest(out);
|
|
127
|
+
}
|
|
128
|
+
async function remove() {
|
|
129
|
+
if (await removing.run(async () => {
|
|
130
|
+
await (props.onRemove ? props.onRemove() : api.removeOwnEndpoint());
|
|
131
|
+
return true;
|
|
132
|
+
})) {
|
|
133
|
+
setBaseUrl("");
|
|
134
|
+
setModels("");
|
|
135
|
+
setDefaultModel("");
|
|
136
|
+
setLimit("");
|
|
137
|
+
setApiKey("");
|
|
138
|
+
setTest(null);
|
|
139
|
+
setSeeded(null);
|
|
140
|
+
loaded.refresh();
|
|
141
|
+
refreshTokens();
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
async function changeOrder(next) {
|
|
145
|
+
const previous = order;
|
|
146
|
+
setOrder(next);
|
|
147
|
+
try {
|
|
148
|
+
await (props.onOrder ? props.onOrder(next) : api.setBackendOrder(next));
|
|
149
|
+
} catch (error) {
|
|
150
|
+
setOrder(previous);
|
|
151
|
+
setProblem(error instanceof Error ? error.message : labels.somethingWrong);
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
if (endpoint === void 0) return /* @__PURE__ */ jsx(Panel, {
|
|
155
|
+
title: labels.ownTitle,
|
|
156
|
+
styling,
|
|
157
|
+
children: loaded.error ? /* @__PURE__ */ jsx(Notice, {
|
|
158
|
+
styling,
|
|
159
|
+
children: labels.unavailable
|
|
160
|
+
}) : /* @__PURE__ */ jsx("p", {
|
|
161
|
+
className: "tk-muted",
|
|
162
|
+
children: labels.loading
|
|
163
|
+
})
|
|
164
|
+
});
|
|
165
|
+
const modelList = lines(models);
|
|
166
|
+
return /* @__PURE__ */ jsx(Panel, {
|
|
167
|
+
title: labels.ownTitle,
|
|
168
|
+
styling,
|
|
169
|
+
children: /* @__PURE__ */ jsxs("form", {
|
|
170
|
+
className: part(styling, "form"),
|
|
171
|
+
onSubmit: submit,
|
|
172
|
+
noValidate: true,
|
|
173
|
+
children: [
|
|
174
|
+
/* @__PURE__ */ jsx(Field, {
|
|
175
|
+
label: labels.baseUrl,
|
|
176
|
+
hint: labels.baseUrlHelp,
|
|
177
|
+
required: true,
|
|
178
|
+
styling,
|
|
179
|
+
children: (p) => /* @__PURE__ */ jsx("input", {
|
|
180
|
+
...p,
|
|
181
|
+
className: part(styling, "input"),
|
|
182
|
+
type: "url",
|
|
183
|
+
inputMode: "url",
|
|
184
|
+
autoComplete: "off",
|
|
185
|
+
autoCapitalize: "none",
|
|
186
|
+
spellCheck: false,
|
|
187
|
+
placeholder: "https://",
|
|
188
|
+
value: baseUrl,
|
|
189
|
+
onChange: (e) => setBaseUrl(e.target.value)
|
|
190
|
+
})
|
|
191
|
+
}),
|
|
192
|
+
/* @__PURE__ */ jsx(Field, {
|
|
193
|
+
label: labels.apiKey,
|
|
194
|
+
hint: endpoint?.hasKey ? fill(labels.apiKeySaved, { hint: `…${endpoint.keyHint ?? ""}` }) : labels.apiKeyHelp,
|
|
195
|
+
styling,
|
|
196
|
+
children: (p) => /* @__PURE__ */ jsx("input", {
|
|
197
|
+
...p,
|
|
198
|
+
className: part(styling, "input"),
|
|
199
|
+
type: "password",
|
|
200
|
+
autoComplete: "new-password",
|
|
201
|
+
spellCheck: false,
|
|
202
|
+
value: apiKey,
|
|
203
|
+
onChange: (e) => setApiKey(e.target.value)
|
|
204
|
+
})
|
|
205
|
+
}),
|
|
206
|
+
endpoint?.hasKey ? /* @__PURE__ */ jsxs("label", {
|
|
207
|
+
className: part(styling, "check"),
|
|
208
|
+
children: [
|
|
209
|
+
/* @__PURE__ */ jsx("input", {
|
|
210
|
+
type: "checkbox",
|
|
211
|
+
checked: removeKey,
|
|
212
|
+
onChange: (e) => setRemoveKey(e.target.checked)
|
|
213
|
+
}),
|
|
214
|
+
" ",
|
|
215
|
+
labels.removeKey
|
|
216
|
+
]
|
|
217
|
+
}) : null,
|
|
218
|
+
/* @__PURE__ */ jsx(Field, {
|
|
219
|
+
label: labels.models,
|
|
220
|
+
hint: labels.modelsHelp,
|
|
221
|
+
required: true,
|
|
222
|
+
styling,
|
|
223
|
+
children: (p) => /* @__PURE__ */ jsx("textarea", {
|
|
224
|
+
...p,
|
|
225
|
+
className: part(styling, "input", "tk-textarea"),
|
|
226
|
+
rows: 3,
|
|
227
|
+
autoCapitalize: "none",
|
|
228
|
+
spellCheck: false,
|
|
229
|
+
value: models,
|
|
230
|
+
onChange: (e) => setModels(e.target.value)
|
|
231
|
+
})
|
|
232
|
+
}),
|
|
233
|
+
modelList.length > 1 ? /* @__PURE__ */ jsx(Field, {
|
|
234
|
+
label: labels.defaultModel,
|
|
235
|
+
styling,
|
|
236
|
+
children: (p) => /* @__PURE__ */ jsx("select", {
|
|
237
|
+
...p,
|
|
238
|
+
className: part(styling, "input"),
|
|
239
|
+
value: modelList.includes(defaultModel) ? defaultModel : modelList[0],
|
|
240
|
+
onChange: (e) => setDefaultModel(e.target.value),
|
|
241
|
+
children: modelList.map((m) => /* @__PURE__ */ jsx("option", {
|
|
242
|
+
value: m,
|
|
243
|
+
children: m
|
|
244
|
+
}, m))
|
|
245
|
+
})
|
|
246
|
+
}) : null,
|
|
247
|
+
/* @__PURE__ */ jsxs("fieldset", {
|
|
248
|
+
className: part(styling, "fieldset"),
|
|
249
|
+
children: [/* @__PURE__ */ jsx("legend", {
|
|
250
|
+
className: part(styling, "label"),
|
|
251
|
+
children: labels.capabilities
|
|
252
|
+
}), CAPABILITIES.map((c) => /* @__PURE__ */ jsxs("label", {
|
|
253
|
+
className: part(styling, "check"),
|
|
254
|
+
children: [
|
|
255
|
+
/* @__PURE__ */ jsx("input", {
|
|
256
|
+
type: "checkbox",
|
|
257
|
+
checked: caps.includes(c),
|
|
258
|
+
onChange: (e) => setCaps(e.target.checked ? [...caps, c] : caps.filter((x) => x !== c))
|
|
259
|
+
}),
|
|
260
|
+
" ",
|
|
261
|
+
labels.capability[c]
|
|
262
|
+
]
|
|
263
|
+
}, c))]
|
|
264
|
+
}),
|
|
265
|
+
caps.includes("tools") ? /* @__PURE__ */ jsx(Segmented, {
|
|
266
|
+
label: labels.toolChoice,
|
|
267
|
+
value: toolChoice,
|
|
268
|
+
options: ["auto", "required"].map((v) => ({
|
|
269
|
+
value: v,
|
|
270
|
+
label: labels.toolChoices[v]
|
|
271
|
+
})),
|
|
272
|
+
onChange: setToolChoice,
|
|
273
|
+
styling
|
|
274
|
+
}) : null,
|
|
275
|
+
/* @__PURE__ */ jsx(Field, {
|
|
276
|
+
label: labels.limit,
|
|
277
|
+
hint: labels.limitHelp,
|
|
278
|
+
styling,
|
|
279
|
+
children: (p) => /* @__PURE__ */ jsx("input", {
|
|
280
|
+
...p,
|
|
281
|
+
className: part(styling, "input"),
|
|
282
|
+
type: "number",
|
|
283
|
+
inputMode: "numeric",
|
|
284
|
+
min: "0",
|
|
285
|
+
step: "1",
|
|
286
|
+
value: limit,
|
|
287
|
+
onChange: (e) => setLimit(e.target.value)
|
|
288
|
+
})
|
|
289
|
+
}),
|
|
290
|
+
endpoint ? /* @__PURE__ */ jsx("p", {
|
|
291
|
+
className: "tk-muted",
|
|
292
|
+
children: endpoint.monthlyTokenLimit === null ? fill(labels.usedNoLimit, { used: formatCompact(endpoint.usedThisMonth) }) : fill(labels.used, {
|
|
293
|
+
used: formatCompact(endpoint.usedThisMonth),
|
|
294
|
+
limit: formatCompact(endpoint.monthlyTokenLimit)
|
|
295
|
+
})
|
|
296
|
+
}) : null,
|
|
297
|
+
/* @__PURE__ */ jsx(BackendOrderSwitch, {
|
|
298
|
+
order,
|
|
299
|
+
hasOwn: !!endpoint,
|
|
300
|
+
onChange: endpoint ? changeOrder : setOrder,
|
|
301
|
+
classNames: styling.classNames,
|
|
302
|
+
labels: props.labels
|
|
303
|
+
}),
|
|
304
|
+
problem ? /* @__PURE__ */ jsx(Notice, {
|
|
305
|
+
styling,
|
|
306
|
+
children: problem
|
|
307
|
+
}) : null,
|
|
308
|
+
save.error ? /* @__PURE__ */ jsx(Notice, {
|
|
309
|
+
styling,
|
|
310
|
+
children: save.error
|
|
311
|
+
}) : null,
|
|
312
|
+
done ? /* @__PURE__ */ jsx(Notice, {
|
|
313
|
+
kind: "ok",
|
|
314
|
+
styling,
|
|
315
|
+
children: labels.saved
|
|
316
|
+
}) : null,
|
|
317
|
+
/* @__PURE__ */ jsxs("div", {
|
|
318
|
+
className: part(styling, "actions"),
|
|
319
|
+
children: [/* @__PURE__ */ jsx("button", {
|
|
320
|
+
type: "submit",
|
|
321
|
+
className: part(styling, "button", "tk-button--primary"),
|
|
322
|
+
"aria-busy": save.pending,
|
|
323
|
+
disabled: save.pending,
|
|
324
|
+
children: save.pending ? labels.saving : labels.save
|
|
325
|
+
}), endpoint ? /* @__PURE__ */ jsxs(Fragment, { children: [/* @__PURE__ */ jsx("button", {
|
|
326
|
+
type: "button",
|
|
327
|
+
className: part(styling, "button"),
|
|
328
|
+
onClick: runTest,
|
|
329
|
+
"aria-busy": testing.pending,
|
|
330
|
+
children: testing.pending ? labels.testing : labels.test
|
|
331
|
+
}), /* @__PURE__ */ jsx("button", {
|
|
332
|
+
type: "button",
|
|
333
|
+
className: part(styling, "button", "tk-button--danger"),
|
|
334
|
+
onClick: remove,
|
|
335
|
+
"aria-busy": removing.pending,
|
|
336
|
+
children: labels.remove
|
|
337
|
+
})] }) : null]
|
|
338
|
+
}),
|
|
339
|
+
testing.error ? /* @__PURE__ */ jsx(Notice, {
|
|
340
|
+
styling,
|
|
341
|
+
children: testing.error
|
|
342
|
+
}) : null,
|
|
343
|
+
removing.error ? /* @__PURE__ */ jsx(Notice, {
|
|
344
|
+
styling,
|
|
345
|
+
children: removing.error
|
|
346
|
+
}) : null,
|
|
347
|
+
test ? /* @__PURE__ */ jsx(Notice, {
|
|
348
|
+
kind: test.ok ? "ok" : "error",
|
|
349
|
+
styling,
|
|
350
|
+
children: test.ok ? fill(labels.testOk, { ms: test.latencyMs ?? 0 }) : fill(labels.testFailed, { message: test.error?.message ?? labels.somethingWrong })
|
|
351
|
+
}) : null
|
|
352
|
+
]
|
|
353
|
+
})
|
|
354
|
+
});
|
|
355
|
+
}
|
|
356
|
+
/** The same form under the contract's name for the endpoint. */
|
|
357
|
+
const OwnEndpointForm = OwnModelForm;
|
|
358
|
+
export { BackendOrderSwitch, OwnEndpointForm, OwnModelForm };
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { TokenCenterError } from "../errors.mjs";
|
|
2
|
+
import { Labels } from "./labels.mjs";
|
|
3
|
+
import { ReactNode } from "react";
|
|
4
|
+
/** Class names every component accepts: `className` on the root, `classNames` per part. */
|
|
5
|
+
export type Styling = {
|
|
6
|
+
className?: string;
|
|
7
|
+
classNames?: Partial<Record<string, string>>;
|
|
8
|
+
};
|
|
9
|
+
export declare const cx: (...parts: (string | false | null | undefined)[]) => string;
|
|
10
|
+
/** The class for a part: its stable `tk-*` name plus the consumer's. */
|
|
11
|
+
export declare const part: (styling: Styling, name: string, ...extra: (string | false | null | undefined)[]) => string;
|
|
12
|
+
/** Anything thrown, as a sentence a user can read. */
|
|
13
|
+
export declare function messageOf(error: unknown, labels: Labels): string;
|
|
14
|
+
export declare const asTokenError: (error: unknown) => TokenCenterError;
|
|
15
|
+
/** "Still needed: A, B." for the fields not yet filled in, or `null`. */
|
|
16
|
+
export declare function stillNeeded(labels: Labels, fields: {
|
|
17
|
+
label: string;
|
|
18
|
+
filled: boolean;
|
|
19
|
+
}[]): string | null;
|
|
20
|
+
/** One async action's state: pending, the error as words, and a way to clear it. */
|
|
21
|
+
export declare function useAction(labels: Labels): {
|
|
22
|
+
pending: boolean;
|
|
23
|
+
error: string | null;
|
|
24
|
+
setError: import("react").Dispatch<import("react").SetStateAction<string | null>>;
|
|
25
|
+
run: <T>(action: () => Promise<T>) => Promise<T | undefined>;
|
|
26
|
+
};
|
|
27
|
+
export declare function Notice({ kind, children, styling }: {
|
|
28
|
+
kind?: "error" | "ok" | "info";
|
|
29
|
+
children: ReactNode;
|
|
30
|
+
styling: Styling;
|
|
31
|
+
}): import("react").JSX.Element;
|
|
32
|
+
/** A labelled field: the label, the control (given its id), a hint beneath, and "(needed)" when required. */
|
|
33
|
+
export declare function Field({ label, hint, required, children, styling }: {
|
|
34
|
+
label: string;
|
|
35
|
+
hint?: string;
|
|
36
|
+
required?: boolean;
|
|
37
|
+
children: (props: {
|
|
38
|
+
id: string;
|
|
39
|
+
"aria-describedby"?: string;
|
|
40
|
+
}) => ReactNode;
|
|
41
|
+
styling: Styling;
|
|
42
|
+
}): import("react").JSX.Element;
|
|
43
|
+
/** Two to four exclusive choices as one row of real buttons. */
|
|
44
|
+
export declare function Segmented<T extends string | number>({ label, value, options, onChange, styling }: {
|
|
45
|
+
label: string;
|
|
46
|
+
value: T | null;
|
|
47
|
+
options: {
|
|
48
|
+
value: T;
|
|
49
|
+
label: string;
|
|
50
|
+
}[];
|
|
51
|
+
onChange: (value: T) => void;
|
|
52
|
+
styling: Styling;
|
|
53
|
+
}): import("react").JSX.Element;
|
|
54
|
+
export declare function Panel({ title, styling, children, as: Tag }: {
|
|
55
|
+
title?: string;
|
|
56
|
+
styling: Styling;
|
|
57
|
+
children: ReactNode;
|
|
58
|
+
as?: "section" | "div";
|
|
59
|
+
}): import("react").JSX.Element;
|
|
60
|
+
export declare function dateText(iso: string, locale?: string): string;
|
|
61
|
+
export declare function dateTimeText(iso: string, locale?: string): string;
|