import{i as e}from"./chunk-B3pr0VN8.js";import{n as t,t as n}from"./jsx-runtime-wTfS3W87.js";import{r}from"./authSession-CrP9lMPq.js";var i=e(t(),1),a=n(),o=async()=>{let e=(await r())?.token||``;return{"Content-Type":`application/json`,Authorization:`Bearer ${e}`,"x-auth-token":e}},s=e=>new Date(e).toLocaleString(`zh-CN`,{hour12:!1}),c=e=>e.replace(`/api/open/v1/`,``),l=()=>{let[e,t]=(0,i.useState)([]),[n,r]=(0,i.useState)(!0),[l,u]=(0,i.useState)(!1),[d,f]=(0,i.useState)(null),[p,m]=(0,i.useState)(!1),h=(0,i.useCallback)(async(e=!1)=>{e?(r(!0),f(null)):u(!0);try{let n=new URL(`/api/me/open-api/usage`,window.location.origin);n.searchParams.set(`limit`,`50`),!e&&d&&n.searchParams.set(`cursor`,d);let r=await(await fetch(n.toString(),{headers:await o()})).json();r?.ok&&(t(t=>e?r.items:[...t,...r.items]),f(r.nextCursor),m(!!r.nextCursor))}catch{}finally{r(!1),u(!1)}},[d]);(0,i.useEffect)(()=>{h(!0)},[]);let g=e.length,_=e.reduce((e,t)=>e+t.totalTokens,0),v=e.reduce((e,t)=>e+t.billedPointCents,0),y=e.filter(e=>e.status===`succeeded`).length;return(0,a.jsxs)(a.Fragment,{children:[(0,a.jsxs)(`div`,{className:`me-page-head`,children:[(0,a.jsx)(`p`,{className:`me-page-eyebrow`,children:`API USAGE`}),(0,a.jsx)(`h1`,{className:`me-page-title`,children:`API 用量`}),(0,a.jsx)(`p`,{className:`me-page-desc`,children:`Open API 调用流量与 token 消耗记录。每次调用的输入/输出 token、扣费与状态均在此可查。`})]}),(0,a.jsxs)(`div`,{style:{display:`grid`,gridTemplateColumns:`repeat(auto-fit, minmax(160px, 1fr))`,gap:12,marginBottom:16},children:[(0,a.jsxs)(`div`,{className:`me-card`,style:{marginBottom:0},children:[(0,a.jsx)(`div`,{style:{fontSize:12,color:`var(--ds-text-muted)`},children:`本页请求`}),(0,a.jsx)(`div`,{style:{fontSize:22,fontWeight:600,color:`var(--ds-text-primary)`,marginTop:4},children:g})]}),(0,a.jsxs)(`div`,{className:`me-card`,style:{marginBottom:0},children:[(0,a.jsx)(`div`,{style:{fontSize:12,color:`var(--ds-text-muted)`},children:`Token 消耗`}),(0,a.jsx)(`div`,{style:{fontSize:22,fontWeight:600,color:`var(--ds-text-primary)`,marginTop:4},children:_.toLocaleString()})]}),(0,a.jsxs)(`div`,{className:`me-card`,style:{marginBottom:0},children:[(0,a.jsx)(`div`,{style:{fontSize:12,color:`var(--ds-text-muted)`},children:`已扣积分`}),(0,a.jsx)(`div`,{style:{fontSize:22,fontWeight:600,color:`var(--ds-brand)`,marginTop:4},children:(v/100).toFixed(2)})]}),(0,a.jsxs)(`div`,{className:`me-card`,style:{marginBottom:0},children:[(0,a.jsx)(`div`,{style:{fontSize:12,color:`var(--ds-text-muted)`},children:`成功率`}),(0,a.jsxs)(`div`,{style:{fontSize:22,fontWeight:600,color:`var(--ds-text-primary)`,marginTop:4},children:[g?Math.round(y/g*100):0,`%`]})]})]}),(0,a.jsxs)(`div`,{className:`me-card`,children:[(0,a.jsx)(`h2`,{className:`me-card-title`,children:`调用流水`}),n?(0,a.jsx)(`p`,{style:{color:`var(--ds-text-secondary)`,fontSize:14},children:`加载中…`}):e.length===0?(0,a.jsx)(`p`,{style:{color:`var(--ds-text-secondary)`,fontSize:14,margin:0},children:`暂无 API 调用记录。前往「请求管理」创建 API Key 后即可产生调用流水。`}):(0,a.jsxs)(a.Fragment,{children:[(0,a.jsx)(`div`,{className:`me-table-wrap`,children:(0,a.jsxs)(`table`,{className:`me-table`,children:[(0,a.jsx)(`thead`,{children:(0,a.jsxs)(`tr`,{children:[(0,a.jsx)(`th`,{children:`时间`}),(0,a.jsx)(`th`,{children:`接口`}),(0,a.jsx)(`th`,{children:`模型`}),(0,a.jsx)(`th`,{children:`输入 token`}),(0,a.jsx)(`th`,{children:`输出 token`}),(0,a.jsx)(`th`,{children:`总 token`}),(0,a.jsx)(`th`,{children:`扣积分`}),(0,a.jsx)(`th`,{children:`耗时`}),(0,a.jsx)(`th`,{children:`状态`})]})}),(0,a.jsx)(`tbody`,{children:e.map(e=>(0,a.jsxs)(`tr`,{children:[(0,a.jsx)(`td`,{className:`me-table-time`,children:s(e.createdAt)}),(0,a.jsx)(`td`,{children:(0,a.jsxs)(`code`,{className:`me-table-mono`,children:[e.method,` `,c(e.endpoint)]})}),(0,a.jsx)(`td`,{className:`me-table-muted`,children:e.model||e.provider||`—`}),(0,a.jsx)(`td`,{className:`me-table-num`,children:e.promptTokens.toLocaleString()}),(0,a.jsx)(`td`,{className:`me-table-num`,children:e.completionTokens.toLocaleString()}),(0,a.jsx)(`td`,{className:`me-table-num`,style:{fontWeight:600},children:e.totalTokens.toLocaleString()}),(0,a.jsx)(`td`,{style:{color:`var(--ds-brand)`,fontWeight:600},children:e.pages==null?(e.billedPointCents/100).toFixed(2):`${e.pages} 页`}),(0,a.jsx)(`td`,{className:`me-table-muted`,children:e.latencyMs==null?`—`:`${(e.latencyMs/1e3).toFixed(1)}s`}),(0,a.jsx)(`td`,{children:e.status===`succeeded`?(0,a.jsx)(`span`,{style:{color:`var(--ds-brand)`},children:`成功`}):(0,a.jsx)(`span`,{style:{color:`var(--ds-status-danger, #b91c1c)`},children:`失败`})})]},e.requestId))})]})}),p?(0,a.jsx)(`div`,{style:{marginTop:14,textAlign:`center`},children:(0,a.jsx)(`button`,{type:`button`,onClick:()=>void h(!1),disabled:l,className:`me-btn-ghost`,children:l?`加载中…`:`加载更多`})}):null]})]})]})};export{l as MeApiUsagePage,l as default};