mirror of
https://github.com/farion1231/cc-switch.git
synced 2026-07-28 00:35:32 +08:00
0bb3b7515a
* feat(usage): support importing model pricing from models.dev Add an "Import from models.dev" button to the Add Pricing panel that fetches https://models.dev/api.json, lists priced models sorted by release date (newest 50 by default, full-text search across ~4800), and bulk-imports the selected entries through the same update_model_pricing command as manual entry. - Normalize imported model IDs to match the backend's clean_model_id_for_pricing rules (strip vendor prefix, lowercase, truncate ':' suffix, map '@' to '-', drop the [1m] marker) so the stored rows actually match cost-attribution lookups - Dedupe selections that collapse to the same model_id and report skipped duplicates in the success toast - Invalidate usage queries on settled (not just success) so partial import failures still refresh the pricing list - Keep ESC inside the picker's search input from closing the dialog and discarding the selection - Add i18n keys for zh/en/zh-TW/ja and unit tests for the normalization, price formatting and flattening logic Fixes #4017 Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * fix(usage): match scoped cost backfill against raw model aliases The scoped backfill selected zero-cost rows via exact SQL string match, but log columns store raw model strings (route prefixes, :free variants, date suffixes), so alias rows were skipped until the next full backfill on startup. Filter rows in Rust with the same model_pricing_candidates normalization used by the pricing lookup; pricing decision logic is untouched. Pre-existing gap from schema v11, surfaced by bulk import. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * refactor(usage): restrict models.dev pricing import to a single model Each update_model_pricing call triggers a backfill pass that loads every zero-cost usage row before filtering by model, so bulk-importing N entries scaled as selectedModels x allZeroCostLogs full scans. Re-importing pricing is rare, so drop the batch path instead of optimizing it: the picker is now single-select, one import runs exactly one update_model_pricing call and one backfill pass. This also removes the normalized-ID dedup logic and the useImportModelPricing hook in favor of the existing useUpdateModelPricing. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> --------- Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
260 lines
7.5 KiB
TypeScript
260 lines
7.5 KiB
TypeScript
import { useState } from "react";
|
|
import { useTranslation } from "react-i18next";
|
|
import { toast } from "sonner";
|
|
import { Save, Plus, Globe } from "lucide-react";
|
|
import { FullScreenPanel } from "@/components/common/FullScreenPanel";
|
|
import { Button } from "@/components/ui/button";
|
|
import { Input } from "@/components/ui/input";
|
|
import { Label } from "@/components/ui/label";
|
|
import { useUpdateModelPricing } from "@/lib/query/usage";
|
|
import { isNonNegativeDecimalString, type ModelPricing } from "@/types/usage";
|
|
import { ModelsDevPickerDialog } from "./ModelsDevPickerDialog";
|
|
|
|
interface PricingEditModalProps {
|
|
open: boolean;
|
|
model: ModelPricing;
|
|
isNew?: boolean;
|
|
onClose: () => void;
|
|
}
|
|
|
|
const PRICE_INPUT_STEP = "0.0001";
|
|
|
|
export function PricingEditModal({
|
|
open,
|
|
model,
|
|
isNew = false,
|
|
onClose,
|
|
}: PricingEditModalProps) {
|
|
const { t } = useTranslation();
|
|
const updatePricing = useUpdateModelPricing();
|
|
const [isPickerOpen, setIsPickerOpen] = useState(false);
|
|
|
|
const [formData, setFormData] = useState({
|
|
modelId: model.modelId,
|
|
displayName: model.displayName,
|
|
inputCost: model.inputCostPerMillion,
|
|
outputCost: model.outputCostPerMillion,
|
|
cacheReadCost: model.cacheReadCostPerMillion,
|
|
cacheCreationCost: model.cacheCreationCostPerMillion,
|
|
});
|
|
|
|
const handleSubmit = async (e: React.FormEvent) => {
|
|
e.preventDefault();
|
|
|
|
// 验证模型 ID
|
|
if (isNew && !formData.modelId.trim()) {
|
|
toast.error(t("usage.modelIdRequired", "模型 ID 不能为空"));
|
|
return;
|
|
}
|
|
|
|
// 验证非负数
|
|
const values = [
|
|
formData.inputCost,
|
|
formData.outputCost,
|
|
formData.cacheReadCost,
|
|
formData.cacheCreationCost,
|
|
];
|
|
|
|
for (const value of values) {
|
|
if (!isNonNegativeDecimalString(value)) {
|
|
toast.error(t("usage.invalidPrice", "价格必须为非负数"));
|
|
return;
|
|
}
|
|
}
|
|
|
|
try {
|
|
await updatePricing.mutateAsync({
|
|
modelId: isNew ? formData.modelId : model.modelId,
|
|
displayName: formData.displayName,
|
|
inputCost: formData.inputCost,
|
|
outputCost: formData.outputCost,
|
|
cacheReadCost: formData.cacheReadCost,
|
|
cacheCreationCost: formData.cacheCreationCost,
|
|
});
|
|
|
|
toast.success(
|
|
isNew
|
|
? t("usage.pricingAdded", "定价已添加")
|
|
: t("usage.pricingUpdated", "定价已更新"),
|
|
{ closeButton: true },
|
|
);
|
|
|
|
onClose();
|
|
} catch (error) {
|
|
toast.error(String(error));
|
|
}
|
|
};
|
|
|
|
return (
|
|
<FullScreenPanel
|
|
isOpen={open}
|
|
title={
|
|
isNew
|
|
? t("usage.addPricing", "新增定价")
|
|
: `${t("usage.editPricing", "编辑定价")} - ${model.modelId}`
|
|
}
|
|
onClose={onClose}
|
|
footer={
|
|
<Button
|
|
type="submit"
|
|
form="pricing-form"
|
|
disabled={updatePricing.isPending}
|
|
>
|
|
{isNew ? (
|
|
<Plus className="h-4 w-4 mr-2" />
|
|
) : (
|
|
<Save className="h-4 w-4 mr-2" />
|
|
)}
|
|
{updatePricing.isPending
|
|
? t("common.saving", "保存中...")
|
|
: isNew
|
|
? t("common.add", "新增")
|
|
: t("common.save", "保存")}
|
|
</Button>
|
|
}
|
|
>
|
|
{isNew && (
|
|
<div className="mb-6 flex items-center justify-between gap-3 rounded-md border border-border/50 bg-muted/20 px-3 py-2.5">
|
|
<p className="text-xs text-muted-foreground">
|
|
{t(
|
|
"usage.modelsDevHint",
|
|
"无需手动填写,可从 models.dev 选择模型定价",
|
|
)}
|
|
</p>
|
|
<Button
|
|
type="button"
|
|
variant="outline"
|
|
size="sm"
|
|
onClick={() => setIsPickerOpen(true)}
|
|
className="shrink-0"
|
|
>
|
|
<Globe className="mr-1.5 h-4 w-4" />
|
|
{t("usage.importFromModelsDev", "从 models.dev 导入")}
|
|
</Button>
|
|
</div>
|
|
)}
|
|
|
|
<form id="pricing-form" onSubmit={handleSubmit} className="space-y-6">
|
|
{isNew && (
|
|
<div className="space-y-2">
|
|
<Label htmlFor="modelId">{t("usage.modelId", "模型 ID")}</Label>
|
|
<Input
|
|
id="modelId"
|
|
value={formData.modelId}
|
|
onChange={(e) =>
|
|
setFormData({ ...formData, modelId: e.target.value })
|
|
}
|
|
placeholder={t("usage.modelIdPlaceholder", {
|
|
defaultValue: "例如: claude-3-5-sonnet-20241022",
|
|
})}
|
|
required
|
|
/>
|
|
</div>
|
|
)}
|
|
|
|
<div className="space-y-2">
|
|
<Label htmlFor="displayName">
|
|
{t("usage.displayName", "显示名称")}
|
|
</Label>
|
|
<Input
|
|
id="displayName"
|
|
value={formData.displayName}
|
|
onChange={(e) =>
|
|
setFormData({ ...formData, displayName: e.target.value })
|
|
}
|
|
placeholder={t("usage.displayNamePlaceholder", {
|
|
defaultValue: "例如: Claude 3.5 Sonnet",
|
|
})}
|
|
required
|
|
/>
|
|
</div>
|
|
|
|
<div className="space-y-2">
|
|
<Label htmlFor="inputCost">
|
|
{t("usage.inputCostPerMillion", "输入成本 (每百万 tokens, USD)")}
|
|
</Label>
|
|
<Input
|
|
id="inputCost"
|
|
type="number"
|
|
step={PRICE_INPUT_STEP}
|
|
min="0"
|
|
value={formData.inputCost}
|
|
onChange={(e) =>
|
|
setFormData({ ...formData, inputCost: e.target.value })
|
|
}
|
|
required
|
|
/>
|
|
</div>
|
|
|
|
<div className="space-y-2">
|
|
<Label htmlFor="outputCost">
|
|
{t("usage.outputCostPerMillion", "输出成本 (每百万 tokens, USD)")}
|
|
</Label>
|
|
<Input
|
|
id="outputCost"
|
|
type="number"
|
|
step={PRICE_INPUT_STEP}
|
|
min="0"
|
|
value={formData.outputCost}
|
|
onChange={(e) =>
|
|
setFormData({ ...formData, outputCost: e.target.value })
|
|
}
|
|
required
|
|
/>
|
|
</div>
|
|
|
|
<div className="space-y-2">
|
|
<Label htmlFor="cacheReadCost">
|
|
{t(
|
|
"usage.cacheReadCostPerMillion",
|
|
"缓存读取成本 (每百万 tokens, USD)",
|
|
)}
|
|
</Label>
|
|
<Input
|
|
id="cacheReadCost"
|
|
type="number"
|
|
step={PRICE_INPUT_STEP}
|
|
min="0"
|
|
value={formData.cacheReadCost}
|
|
onChange={(e) =>
|
|
setFormData({ ...formData, cacheReadCost: e.target.value })
|
|
}
|
|
required
|
|
/>
|
|
</div>
|
|
|
|
<div className="space-y-2">
|
|
<Label htmlFor="cacheCreationCost">
|
|
{t(
|
|
"usage.cacheCreationCostPerMillion",
|
|
"缓存写入成本 (每百万 tokens, USD)",
|
|
)}
|
|
</Label>
|
|
<Input
|
|
id="cacheCreationCost"
|
|
type="number"
|
|
step={PRICE_INPUT_STEP}
|
|
min="0"
|
|
value={formData.cacheCreationCost}
|
|
onChange={(e) =>
|
|
setFormData({ ...formData, cacheCreationCost: e.target.value })
|
|
}
|
|
required
|
|
/>
|
|
</div>
|
|
</form>
|
|
|
|
{isNew && isPickerOpen && (
|
|
<ModelsDevPickerDialog
|
|
open={isPickerOpen}
|
|
onClose={() => setIsPickerOpen(false)}
|
|
onImported={() => {
|
|
setIsPickerOpen(false);
|
|
onClose();
|
|
}}
|
|
/>
|
|
)}
|
|
</FullScreenPanel>
|
|
);
|
|
}
|