diff --git a/.github/scripts/account-ops-team-screenshots.mjs b/.github/scripts/account-ops-team-screenshots.mjs new file mode 100644 index 000000000000..bf155578be05 --- /dev/null +++ b/.github/scripts/account-ops-team-screenshots.mjs @@ -0,0 +1,85 @@ +import { execFileSync } from 'node:child_process' +import { mkdirSync, writeFileSync, rmSync } from 'node:fs' +import { resolve, join } from 'node:path' +import { pathToFileURL } from 'node:url' +const root = resolve('frontend'), out = process.env.EVIDENCE_DIR +const { createServer } = await import(pathToFileURL(join(root, 'node_modules/vite/dist/node/index.js'))) +const { default: vue } = await import(pathToFileURL(join(root, 'node_modules/@vitejs/plugin-vue/dist/index.mjs'))) +const { chromium } = await import(pathToFileURL(process.env.SUB2API_PLAYWRIGHT_MODULE)) +const baseline = 'ce45c5a85f2a68544994ac146f3f809f31308e1a' +const head = execFileSync('git', ['rev-parse', 'HEAD'], { encoding: 'utf8' }).trim() +const origin = 'http://127.0.0.1:4309', entry = join(root, '.account-ops-team-preview.js') +const original = new Map(['AccountOpsEmailDialog.vue', 'AccountOpsRuleList.vue'].map(name => [join(root, 'src/components/admin/operations', name), execFileSync('git', ['show', `${baseline}:frontend/src/components/admin/operations/${name}`], { encoding: 'utf8' })])) +async function settleDialog(page) { + await page.waitForFunction(() => { + const panel=document.querySelector('.modal-content'), overlay=panel?.parentElement + if(!panel||!overlay||overlay.className.includes('modal-enter'))return false + const bounds=panel.getBoundingClientRect() + return Number(getComputedStyle(overlay).opacity)===1 && Number(getComputedStyle(panel).opacity)===1 && bounds.width>0 && bounds.height>0 && bounds.top>=0 && bounds.bottom<=innerHeight + }) +} +let before = true, server, browser +mkdirSync(out, { recursive: true }) +try { + writeFileSync(entry, ` +import {createApp,h,ref} from 'vue';import {createPinia} from 'pinia'; +import {i18n,loadLocaleMessages} from '/src/i18n/index.ts';import {apiClient} from '/src/api/client.ts'; +import Rules from '/src/components/admin/operations/AccountOpsRuleList.vue'; +import Email from '/src/components/admin/operations/AccountOpsEmailDialog.vue'; +import Groups from '/src/components/admin/operations/AccountOpsGroupDialog.vue'; +import '/src/style.css'; +const language=new URLSearchParams(location.search).get('lang')||'zh';await loadLocaleMessages(language);i18n.global.locale.value=language; +if(location.search.includes('dark'))document.documentElement.classList.add('dark'); +const before=location.search.includes('before'); +const accounts=[1,2].map(id=>({account_id:id,account_name:'示例供应商 Key '+id,platform:'openai',type:'apikey',provider_id:before?undefined:7,balance:35,unit:'USD',balance_status:'ok',probe_enabled:true,received_at:'2026-01-01T08:00:00Z',usage_windows:[]})); +accounts.push({account_id:3,account_name:'示例临时 OAuth',platform:'openai',type:'oauth',oauth_lifecycle:'temporary',balance:null,unit:'%',balance_status:'unknown',received_at:null,usage_windows:[]}); +accounts.push({account_id:4,account_name:'示例长期 OAuth',platform:'openai',type:'oauth',oauth_lifecycle:'long_term',balance:null,unit:'%',balance_status:'unknown',received_at:null,usage_windows:[]}); +const groups=[{id:'manual-provider-7',name:'示例供应商',provider_id:before?undefined:7,provider:'provider',site:'https://supplier.example.test',mode:'manual',account_ids:[1,2]},...accounts.slice(2).map(a=>({id:'manual-oauth-'+a.account_id,name:a.account_name,provider:'manual',site:'',mode:'manual',account_ids:[a.account_id]}))]; +const config={enabled:true,recipient:before?'ops@example.test':'ops@example.test, oncall@example.test',email_name:'值班团队',balance_low:true,weekly_quota:true,cooldown_minutes:60,groups,balance_thresholds:[1,2].map(account_id=>({account_id,enabled:true,threshold:50,unit:'USD',notify_alert:true,notify_recovery:true}))}; +apiClient.defaults.adapter=async c=>({config:c,status:200,statusText:'OK',headers:{},data:{code:0,data:c.url.includes('/providers')?{items:[{id:7,name:'示例供应商'}],total:1}:config}}); +const email=ref(false),group=ref(false); +createApp({setup(){return()=>h('main',{class:'p-6 space-y-4'},[ + h('p',{class:'text-sm text-gray-500'},'虚构数据 · 实际账号运维组件'), + h('button',{'data-testid':'open-email',class:'btn btn-primary',onClick:()=>email.value=true},'配置收件邮箱'), + h('button',{'data-testid':'open-groups',class:'btn btn-secondary',onClick:()=>group.value=true},'关联供应商'), + h(Rules,{accounts,groups,config,userId:42,loading:false,ready:true,error:''}), + h(Email,{show:email.value,config,smtpConfigured:true,userId:42,onClose:()=>email.value=false}), + h(Groups,{show:group.value,groups,accounts,onClose:()=>group.value=false}) +])}}).use(createPinia()).use(i18n).mount('#app'); + `) + process.chdir(root) // Tailwind resolves the project theme relative to cwd. + server = await createServer({ root, configFile: false, server: { host: '127.0.0.1', port: 4309, strictPort: true }, resolve: { alias: { '@': join(root, 'src') } }, plugins: [{ name: 'baseline-components', enforce: 'pre', load(id) { if (before && original.has(id)) return original.get(id) }, configureServer(vite) { + vite.middlewares.use((req, res, next) => { + if (req.url?.split('?')[0] !== '/preview') return next() + res.setHeader('Content-Type', 'text/html'); res.end('
') + }) + } }, vue()] }) + + await server.listen(); browser = await chromium.launch({ headless: true }) + const captures = [] + for (const language of ['zh', 'en']) for (const phase of ['before', 'after']) { + before = phase === 'before'; server.moduleGraph.invalidateAll() + const context = await browser.newContext({ viewport: { width: 1440, height: 1000 } }) + await context.route('**/*', route => route.request().url().startsWith(origin) ? route.continue() : route.abort()) + const page = await context.newPage(), errors = [] + page.on('pageerror', e => { errors.push(e.message); console.error('preview page error:', e.message) }) + await page.goto(`${origin}/preview?${phase}&lang=${language}`, { waitUntil: 'networkidle' }) + await page.getByTestId('account-ops-rules').waitFor() + await page.screenshot({ path: join(out, `${phase}-${language}-rules.png`), fullPage: true, animations: 'disabled' }) + await page.getByTestId('open-email').click(); await page.locator('#account-ops-email').waitFor(); await settleDialog(page) + await page.screenshot({ path: join(out, `${phase}-${language}-email.png`), fullPage: true, animations: 'disabled' }) + if (phase === 'after') { + if (await page.locator('textarea#account-ops-email').count() !== 1) throw new Error('multiple-email editor missing') + await page.reload({ waitUntil: 'networkidle' }) + await page.getByTestId('open-groups').click() + await page.getByTestId('ops-provider-select').first().evaluate(el => { el.closest('details').open = true }) + await page.getByTestId('ops-provider-select').first().focus() + await page.getByTestId('ops-provider-select').first().locator('option').filter({ hasText: '示例供应商' }).waitFor({ state: 'attached' }) + await settleDialog(page) + await page.screenshot({ path: join(out, `after-${language}-provider-link.png`), fullPage: true, animations: 'disabled' }) + } + if (errors.length) throw new Error(errors.join('\n')) + captures.push({ phase, language, source: before ? baseline : head }); await context.close() + } + writeFileSync(join(out, 'source.json'), JSON.stringify({ head, baseline, captures, scope: 'Actual Vue components with synthetic data; all external requests blocked' }, null, 2)) +} finally { await browser?.close(); await server?.close(); rmSync(entry, { force: true }) } diff --git a/.github/scripts/bps-search-preview.mjs b/.github/scripts/bps-search-preview.mjs new file mode 100644 index 000000000000..38412e704273 --- /dev/null +++ b/.github/scripts/bps-search-preview.mjs @@ -0,0 +1,73 @@ +// Render the actual Vue component in CI with a fictional administrator. +import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs' +import { execFileSync } from 'node:child_process' +import { createRequire } from 'node:module' +import { tmpdir } from 'node:os' +import { dirname, join, resolve } from 'node:path' +import { pathToFileURL } from 'node:url' +const [repoArg, outputArg, baseline = ''] = process.argv.slice(2) +const repo = resolve(repoArg), output = resolve(outputArg), root = join(repo, 'frontend') +const require = createRequire(join(root, 'package.json')) +const { createServer } = await import(pathToFileURL(join(dirname(require.resolve('vite/package.json')), 'dist/node/index.js')).href) +const vue = (await import(pathToFileURL(require.resolve('@vitejs/plugin-vue')).href)).default +const { chromium } = await import(pathToFileURL(process.env.SUB2API_PLAYWRIGHT_MODULE).href) +const entry = '.bps-search-preview.js', cacheDir = mkdtempSync(join(tmpdir(), 'bps-search-preview-')) +let server, browser +mkdirSync(output, { recursive: true }) +try { + writeFileSync(join(root, entry), ` +import {mount} from '@vue/test-utils'; +import {createPinia,setActivePinia} from 'pinia'; +import {i18n,loadLocaleMessages} from '/src/i18n/index.ts'; +import {useAuthStore} from '/src/stores/auth.ts'; +import View from '/src/components/admin/operations/BPSDefaultsCard.vue'; +import {defaultExcelBPSDefaults} from '/src/utils/excelBPSDefaults.ts'; +import '/src/style.css'; +await loadLocaleMessages('zh');i18n.global.locale.value='zh'; +const pinia=createPinia();setActivePinia(pinia);useAuthStore().user={id:42,role:'admin',username:'demo'}; +document.querySelector('#app').innerHTML='

OAuth 自动配置 → BPS 默认模板 · 实际组件 / 虚构管理员

'; +window.preview=mount(View,{attachTo:'#preview',props:{groups:[],modelValue:{...defaultExcelBPSDefaults(),all_models:true},'onUpdate:modelValue':value=>window.preview.setProps({modelValue:value})},global:{plugins:[pinia,i18n]}}); +`) + process.chdir(root) + server = await createServer({ root, configFile: false, cacheDir, + resolve: { alias: { '@': join(root, 'src') } }, + plugins: [{ name: 'bps-search-fixture', enforce: 'pre', load(id) { + if (baseline && id === join(root,'src/components/admin/operations/BPSDefaultsCard.vue')) return execFileSync('git',['show',baseline+':frontend/src/components/admin/operations/BPSDefaultsCard.vue'],{cwd:repo,encoding:'utf8'}) + }, configureServer(vite) { vite.middlewares.use((request, response, next) => { + if (request.url === '/') { response.setHeader('Content-Type','text/html'); response.end('
') } + else if (request.url === '/favicon.ico') { response.statusCode = 204; response.end() } + else next() + }) } }, vue()], server: { host: '127.0.0.1', port: 4299, strictPort: true }, + optimizeDeps: { entries: [], include: ['vue','pinia','vue-i18n','axios','@vue/test-utils'] } }) + await server.listen(); browser = await chromium.launch({ headless: true }) + const page = await browser.newPage({ viewport: { width: 1100, height: 1000 } }), errors = [] + page.on('pageerror', error => errors.push(error.message)) + await page.route('**/*', route => new URL(route.request().url()).origin === 'http://127.0.0.1:4299' ? route.continue() : route.abort()) + await page.goto('http://127.0.0.1:4299', { waitUntil: 'networkidle' }) + await page.getByTestId('bps-all-models').waitFor() + if (baseline) { + if (await page.getByTestId('bps-search-options').count()) throw Error('Baseline unexpectedly includes search options') + await page.screenshot({path:join(output,'before.png'),fullPage:true}) + } else { + await page.getByTestId('bps-search-options').waitFor() + await page.screenshot({ path: join(output,'default-off.png'), fullPage: true }) + await page.getByTestId('excel-bps-web-search').check() + await page.getByTestId('excel-bps-web-search-auto').check() + await page.waitForFunction(() => window.preview.props('modelValue').web_search && window.preview.props('modelValue').web_search_auto) + await page.screenshot({ path: join(output,'search-and-auto.png'), fullPage: true }) + await page.evaluate(() => document.documentElement.classList.add('dark')) + await page.screenshot({ path: join(output,'dark.png'), fullPage: true }) + await page.setViewportSize({ width: 390, height: 700 }) + await page.screenshot({ path: join(output,'mobile.png'), fullPage: true }) + await page.getByTestId('excel-bps-web-search').uncheck() + await page.waitForFunction(() => !window.preview.props('modelValue').web_search && !window.preview.props('modelValue').web_search_auto) + if (await page.getByTestId('excel-bps-web-search-auto').isChecked()) throw Error('Automatic search remained enabled') + const saved = await page.evaluate(() => window.preview.props('modelValue')) + if (saved.web_search || saved.web_search_auto) throw Error('Template model retained disabled search options') + } + if (errors.length) throw Error(JSON.stringify(errors)) + writeFileSync(join(output,'evidence.json'),JSON.stringify({ source:execFileSync('git',['rev-parse','HEAD'],{cwd:repo,encoding:'utf8'}).trim(),baseline:baseline||null,fixture:true,errors,states:baseline?['before']:['default-off','search-and-auto','dark','mobile'],production:false },null,2)) +} finally { + await browser?.close(); await server?.close() + rmSync(join(root,entry),{force:true}); rmSync(cacheDir,{recursive:true,force:true}) +} diff --git a/.github/workflows/account-ops-team.yml b/.github/workflows/account-ops-team.yml new file mode 100644 index 000000000000..234541e0a080 --- /dev/null +++ b/.github/workflows/account-ops-team.yml @@ -0,0 +1,66 @@ +name: Account operations team and provider balances +on: + pull_request: + paths: + - 'backend/internal/service/account_ops*' + - 'backend/internal/repository/account_ops*' + - 'backend/internal/handler/admin/account_ops*' + - 'frontend/src/components/admin/operations/AccountOps*' + - 'frontend/src/components/admin/operations/__tests__/AccountOps*' + - 'frontend/src/api/admin/accountOps.ts' + - 'frontend/src/views/admin/AccountOpsView.vue' + - 'frontend/src/components/admin/operations/MatureProvidersPanel.vue' + - '.github/workflows/account-ops-team.yml' + - '.github/scripts/account-ops-team-screenshots.mjs' + workflow_dispatch: +permissions: + contents: read +jobs: + backend: + runs-on: ubuntu-latest + timeout-minutes: 30 + steps: + - uses: actions/checkout@v6 + with: + ref: ${{ github.event.pull_request.head.sha || github.sha }} + - uses: actions/setup-go@v6 + with: + go-version-file: backend/go.mod + cache-dependency-path: backend/go.sum + - run: go test -race -tags=unit ./internal/service ./internal/handler/admin -run '^TestAccountOps' -count=1 -timeout=10m + working-directory: backend + - run: go test -tags=integration ./internal/repository -run '^TestAccountOpsProviderSnapshot' -count=1 -v -timeout=10m + working-directory: backend + frontend: + runs-on: ubuntu-latest + timeout-minutes: 25 + steps: + - uses: actions/checkout@v6 + with: + ref: ${{ github.event.pull_request.head.sha || github.sha }} + fetch-depth: 0 + - uses: pnpm/action-setup@v6 + with: + version: 9 + - uses: actions/setup-node@v6 + with: + node-version: '22' + cache: pnpm + cache-dependency-path: frontend/pnpm-lock.yaml + - run: pnpm --dir frontend install --frozen-lockfile + - run: pnpm --dir frontend exec vitest run src/components/admin/operations/__tests__/AccountOps src/components/common/__tests__/NewFeatureBadge.spec.ts src/views/admin/__tests__/AccountOpsView.spec.ts src/views/admin/__tests__/MatureProviders.spec.ts + - run: pnpm --dir frontend run build + - name: Install screenshot browser + run: | + npm install --prefix "$RUNNER_TEMP/ops-team-browser" --no-save --ignore-scripts playwright@1.56.1 + node "$RUNNER_TEMP/ops-team-browser/node_modules/playwright/cli.js" install --with-deps chromium + - name: Render actual components with synthetic provider and account data + env: + EVIDENCE_DIR: ${{ runner.temp }}/account-ops-team-ui + SUB2API_PLAYWRIGHT_MODULE: ${{ runner.temp }}/ops-team-browser/node_modules/playwright/index.mjs + run: node .github/scripts/account-ops-team-screenshots.mjs + - uses: actions/upload-artifact@v7 + if: always() + with: + name: account-ops-team-ui-${{ github.event.pull_request.head.sha || github.sha }} + path: ${{ runner.temp }}/account-ops-team-ui diff --git a/.github/workflows/bps-search.yml b/.github/workflows/bps-search.yml new file mode 100644 index 000000000000..c12447915b43 --- /dev/null +++ b/.github/workflows/bps-search.yml @@ -0,0 +1,68 @@ +name: BPS search protocol and settings +on: + pull_request: + paths: + - backend/internal/service/basispoints/web_search* + - backend/internal/service/openai_excel_bps_search* + - backend/internal/service/bps_search_settings_test.go + - frontend/src/components/account/ExcelBPSSearchOptions.vue + - frontend/src/components/account/__tests__/ExcelBPSSearchOptions.spec.ts + - frontend/src/utils/excelBPSDefaults.ts + - .github/scripts/bps-search-preview.mjs + - .github/workflows/bps-search.yml +permissions: + contents: read +jobs: + search-protocol: + runs-on: ubuntu-latest + timeout-minutes: 25 + steps: + - uses: actions/checkout@v6 + with: + ref: ${{ github.event.pull_request.head.sha }} + - uses: actions/setup-go@v6 + with: + go-version-file: backend/go.mod + cache-dependency-path: backend/go.sum + - working-directory: backend + run: go test -race ./internal/service/basispoints -run 'Test(WebSearch|AlphaSearch|ToolChoice|CurrentEncrypted|CompactionPolicy)' -count=1 -v + - working-directory: backend + run: go test -race -tags=unit ./internal/service -run 'Test(ExcelBPSAccountSearch|ExcelBPSSearch|BPSSearchDefaults)' -count=1 -v -timeout=10m + - working-directory: backend + run: go test -tags=integration ./internal/repository -run '^TestQuality(BPSSnapshotEqualDefaultsAndManualChanges|EnableBPSRestoreAfterAccountEditorSave|BPSSearchRestorePreservesManualChanges)$' -count=1 -v -timeout=10m + search-ui: + runs-on: ubuntu-latest + timeout-minutes: 20 + steps: + - uses: actions/checkout@v6 + with: + ref: ${{ github.event.pull_request.head.sha }} + - uses: pnpm/action-setup@v6 + with: + version: 9 + - uses: actions/setup-node@v6 + with: + node-version: '22' + cache: pnpm + cache-dependency-path: frontend/pnpm-lock.yaml + - run: pnpm --dir frontend install --frozen-lockfile + - run: pnpm --dir frontend exec vitest run src/components/account/__tests__/ExcelBPSSearchOptions.spec.ts src/components/common/__tests__/NewFeatureBadge.spec.ts src/components/account/__tests__/EditAccountModal.spec.ts src/components/account/__tests__/BulkEditAccountModal.spec.ts src/views/admin/__tests__/AutoConfigView.spec.ts src/composables/__tests__/useExcelBPSDefaults.spec.ts src/utils/__tests__/qualityRulePatch.spec.ts src/i18n/__tests__/localeKeyCompleteness.spec.ts + - run: pnpm --dir frontend typecheck + - name: Install isolated screenshot browser + run: | + npm install --prefix "$RUNNER_TEMP/bps-search-browser" --no-save --ignore-scripts playwright@1.56.1 + node "$RUNNER_TEMP/bps-search-browser/node_modules/playwright/cli.js" install --with-deps chromium + - name: Fetch immutable before UI + run: git fetch --no-tags --depth=1 origin 713cb0f3efe3f838d159cd57998128c2cdaa84b1 + - name: Render actual BPS search settings + env: + SUB2API_PLAYWRIGHT_MODULE: ${{ runner.temp }}/bps-search-browser/node_modules/playwright/index.mjs + run: | + node .github/scripts/bps-search-preview.mjs "$GITHUB_WORKSPACE" "$RUNNER_TEMP/bps-search-evidence/before" 713cb0f3efe3f838d159cd57998128c2cdaa84b1 + node .github/scripts/bps-search-preview.mjs "$GITHUB_WORKSPACE" "$RUNNER_TEMP/bps-search-evidence/after" + - uses: actions/upload-artifact@v7 + if: always() + with: + name: bps-search-evidence-${{ github.event.pull_request.head.sha }} + path: ${{ runner.temp }}/bps-search-evidence + retention-days: 30 diff --git a/backend/internal/handler/admin/account_ops_handler.go b/backend/internal/handler/admin/account_ops_handler.go index 01bace22b0d2..646f23af2c84 100644 --- a/backend/internal/handler/admin/account_ops_handler.go +++ b/backend/internal/handler/admin/account_ops_handler.go @@ -18,6 +18,26 @@ type AccountOpsHandler struct { func NewAccountOpsHandler(svc *service.AccountOpsService, email *service.EmailService) *AccountOpsHandler { return &AccountOpsHandler{svc: svc, email: email} } +func (h *AccountOpsHandler) SetOAuthLifecycle(c *gin.Context) { + id, err := strconv.ParseInt(c.Param("id"), 10, 64) + var v struct { + Lifecycle string `json:"lifecycle"` + } + if err != nil || id <= 0 || c.ShouldBindJSON(&v) != nil { + response.BadRequest(c, "Invalid OAuth lifecycle request") + return + } + if err = h.svc.SetOAuthLifecycle(c.Request.Context(), id, v.Lifecycle); err != nil { + if errors.Is(err, service.ErrAccountOpsConfigValidation) { + response.BadRequest(c, err.Error()) + } else { + response.ErrorFrom(c, err) + } + return + } + response.Success(c, gin.H{"account_id": id, "lifecycle": v.Lifecycle}) +} + func (h *AccountOpsHandler) GetConfig(c *gin.Context) { cfg, err := h.svc.GetConfig(c.Request.Context()) if err != nil { diff --git a/backend/internal/handler/admin/account_ops_handler_test.go b/backend/internal/handler/admin/account_ops_handler_test.go index b6201b629d0e..7b6eba320695 100644 --- a/backend/internal/handler/admin/account_ops_handler_test.go +++ b/backend/internal/handler/admin/account_ops_handler_test.go @@ -74,7 +74,7 @@ func TestAccountOpsInvalidConfigurationReturnsSafe400WithoutWrites(t *testing.T) }{ {"amount rule on OAuth", `,"balance_thresholds":[{"account_id":41,"enabled":true,"threshold":5,"unit":"USD"}]`, service.AccountTypeOAuth}, {"quota rule on API key", `,"quota_thresholds":[{"account_id":41,"enabled":true,"threshold_percent":80,"window":"any"}]`, service.AccountTypeAPIKey}, - {"invalid recipient", `,"recipient":"canary-secret@example.test,another@example.test"`, service.AccountTypeAPIKey}, + {"invalid recipient", `,"recipient":"canary-secret@example.test,not-an-email"`, service.AccountTypeAPIKey}, {"invalid official URL", `,"webhooks":[{"id":"robot","provider":"wecom","url":"https://bad.example/canary-secret?key=private-canary"}]`, service.AccountTypeAPIKey}, {"negative threshold", `,"balance_thresholds":[{"account_id":41,"enabled":true,"threshold":-1,"unit":"USD"}]`, service.AccountTypeAPIKey}, } { diff --git a/backend/internal/handler/channel_monitor_v3_handler_test.go b/backend/internal/handler/channel_monitor_v3_handler_test.go index e63adb3cec56..64736e4c85e2 100644 --- a/backend/internal/handler/channel_monitor_v3_handler_test.go +++ b/backend/internal/handler/channel_monitor_v3_handler_test.go @@ -133,7 +133,33 @@ func TestChannelMonitorV3StatusScopesOrdinaryUsersServerSide(t *testing.T) { require.NotNil(t, last) require.Equal(t, service.ChannelMonitorV3StatusDown, last.Status) require.Zero(t, last.Requests) - require.NotContains(t, recorder.Body.String(), "4321", "request volume never reaches users") + // Inspect field names, not decimal substrings that can occur in timestamps + // or public success rates. Check the wire JSON so even zero-valued private + // fields cannot silently become part of the ordinary-user response. + var wire map[string]any + require.NoError(t, json.Unmarshal(recorder.Body.Bytes(), &wire)) + var checkPrivateFields func(any) + checkPrivateFields = func(value any) { + switch value := value.(type) { + case map[string]any: + for _, field := range []string{"requests", "errors", "ignored_errors", "probes", "successes"} { + require.NotContains(t, value, field, "request volume never reaches users") + } + for _, child := range value { + checkPrivateFields(child) + } + case []any: + for _, child := range value { + checkPrivateFields(child) + } + } + } + checkPrivateFields(wire) + // The exact timestamp that caused the CI failure is legitimate public data. + data, ok := wire["data"].(map[string]any) + require.True(t, ok, "status envelope must contain an object") + data["generated_at"] = "2026-11-14T19:24:11.151432152Z" + checkPrivateFields(wire) } func TestChannelMonitorV3StatusShowsAdminsEverything(t *testing.T) { diff --git a/backend/internal/repository/account_ops_providers.go b/backend/internal/repository/account_ops_providers.go new file mode 100644 index 000000000000..2963f3827ae4 --- /dev/null +++ b/backend/internal/repository/account_ops_providers.go @@ -0,0 +1,34 @@ +package repository + +import ( + "context" + "encoding/json" + "github.com/Wei-Shaw/sub2api/internal/service" +) + +// Reuse the provider catalog's metadata. Credentials, login sessions and +// upstream HTTP requests remain exclusively owned by the provider module. +func (r *accountRepository) ReadAccountOpsProviders(ctx context.Context, ids []int64) ([]service.MatureProvider, error) { + raw, err := json.Marshal(ids) + if err != nil { + return nil, err + } + rows, err := r.sql.QueryContext(ctx, `SELECT id,name,status,metrics,observed_at,refresh_interval_seconds FROM mature_providers WHERE id IN (SELECT jsonb_array_elements_text($1::jsonb)::bigint)`, string(raw)) + if err != nil { + return nil, err + } + defer func() { _ = rows.Close() }() + providers := []service.MatureProvider{} + for rows.Next() { + var p service.MatureProvider + var data []byte + if err = rows.Scan(&p.ID, &p.Name, &p.Status, &data, &p.ObservedAt, &p.RefreshIntervalSeconds); err != nil { + return nil, err + } + if err = json.Unmarshal(data, &p.Metrics); err != nil { + return nil, err + } + providers = append(providers, p) + } + return providers, rows.Err() +} diff --git a/backend/internal/repository/account_ops_providers_integration_test.go b/backend/internal/repository/account_ops_providers_integration_test.go new file mode 100644 index 000000000000..8d60883072ca --- /dev/null +++ b/backend/internal/repository/account_ops_providers_integration_test.go @@ -0,0 +1,37 @@ +//go:build integration + +package repository_test + +import ( + "context" + "encoding/json" + "github.com/Wei-Shaw/sub2api/internal/repository" + "github.com/Wei-Shaw/sub2api/internal/service" + "github.com/stretchr/testify/require" + "testing" +) + +func TestAccountOpsProviderSnapshotReadsExistingCatalogWithoutCredentials(t *testing.T) { + db, client := repository.UpstreamChannelsTestDatabase(t) + var id int64 + require.NoError(t, db.QueryRow(`INSERT INTO mature_providers(name,site_type,base_url,status,metrics,observed_at,credential_cipher,refresh_interval_seconds) VALUES('Snapshot fixture','sub2api','https://fixture.invalid','connected','{"balance":-2.5,"currency":"USD"}',NOW(),'synthetic-cipher',300) RETURNING id`).Scan(&id)) + accounts := repository.NewAdminAccountRepository(client, db, nil) + reader, ok := accounts.(interface { + ReadAccountOpsProviders(context.Context, []int64) ([]service.MatureProvider, error) + }) + require.True(t, ok) + rows, err := reader.ReadAccountOpsProviders(context.Background(), []int64{id}) + require.NoError(t, err) + require.Len(t, rows, 1) + require.Equal(t, -2.5, *rows[0].Metrics.Balance) + require.Equal(t, "USD", rows[0].Metrics.Currency) + require.NotNil(t, rows[0].ObservedAt) + encoded, err := json.Marshal(rows) + require.NoError(t, err) + require.NotContains(t, string(encoded), "synthetic-cipher") + require.Empty(t, rows[0].Account) + require.False(t, rows[0].HasCredential) + rows, err = reader.ReadAccountOpsProviders(context.Background(), []int64{}) + require.NoError(t, err) + require.Empty(t, rows) +} diff --git a/backend/internal/repository/account_quality_bps.go b/backend/internal/repository/account_quality_bps.go index 2c59165ed67a..a8f7b1c543df 100644 --- a/backend/internal/repository/account_quality_bps.go +++ b/backend/internal/repository/account_quality_bps.go @@ -264,7 +264,7 @@ func qualityBPSDefaultJSON(key string) json.RawMessage { case service.ExcelBPS403RecoveryIntervalMinutesKey: return json.RawMessage(`60`) case "openai_excel_bps", service.ExcelBPSOmitUnsupportedToolsKey, - service.ExcelBPSIgnoreEncryptedContentKey, + service.ExcelBPSIgnoreEncryptedContentKey, service.ExcelBPSWebSearchKey, service.ExcelBPSWebSearchAutoKey, "openai_excel_bps_auto_disable_on_403", service.ExcelBPSAutoRecoverOn403Key, service.ExcelBPSAutoMoveOn403Key, "openai_excel_bps_mihomo", "openai_excel_bps_cache_creation_as_input": return json.RawMessage(`false`) diff --git a/backend/internal/repository/account_quality_bps_integration_test.go b/backend/internal/repository/account_quality_bps_integration_test.go index 95c6d5250109..97ccaa8c6299 100644 --- a/backend/internal/repository/account_quality_bps_integration_test.go +++ b/backend/internal/repository/account_quality_bps_integration_test.go @@ -316,3 +316,19 @@ func TestQualityEnableBPSRequiresExcelAuthorization(t *testing.T) { f.exec(`INSERT INTO openai_excel_oauth_credentials(account_id,credentials_ciphertext) VALUES($1,'test-encrypted-grant')`) // The worker callback, tested separately, clears preparation automatically. } + +func TestQualityBPSSearchRestorePreservesManualChanges(t *testing.T) { + for _, key := range []string{service.ExcelBPSWebSearchKey, service.ExcelBPSWebSearchAutoKey} { + t.Run(key, func(t *testing.T) { + f := newQualityBPSFixture(t, `{}`, &service.QualityPolicy{Action: service.QualityActionEnableBPS, AutoRestore: true, BPS: &service.QualityBPSPolicy{FailureThreshold: 1, PassThreshold: 1, AllModels: true, WebSearch: true, WebSearchAuto: true}}) + require.Equal(t, "bps_enabled", f.apply("failed")) + require.Equal(t, true, f.extra()[key]) + repo := newAccountRepositoryWithSQL(testEntClient(t), integrationDB, nil) + account, err := repo.GetByID(context.Background(), f.account) + require.NoError(t, err) + delete(account.Extra, key) + require.NoError(t, repo.Update(context.Background(), account)) + require.Equal(t, "restore_conflict", f.apply("passed"), "manually disabling an enabled search option must retain the ownership conflict") + }) + } +} diff --git a/backend/internal/repository/account_quality_bps_test.go b/backend/internal/repository/account_quality_bps_test.go index beceaf7f62d4..b3afef2835d5 100644 --- a/backend/internal/repository/account_quality_bps_test.go +++ b/backend/internal/repository/account_quality_bps_test.go @@ -14,6 +14,9 @@ func TestQualityBPSSnapshotEqualDefaultsAndManualChanges(t *testing.T) { equal bool }{ {"omitted false switches", map[string]any{"openai_excel_bps": true, "openai_excel_bps_cache_creation_as_input": false, "openai_excel_bps_mihomo": false}, map[string]any{"openai_excel_bps": true}, true}, + {"omitted search switches", map[string]any{"openai_excel_bps_web_search": false, "openai_excel_bps_web_search_auto": false}, nil, true}, + {"manual search disable", map[string]any{"openai_excel_bps_web_search": true}, nil, false}, + {"manual automatic search disable", map[string]any{"openai_excel_bps_web_search_auto": true}, nil, false}, {"default recovery interval", map[string]any{"openai_excel_bps_403_recovery_interval_minutes": 60}, nil, true}, {"custom recovery interval", map[string]any{"openai_excel_bps_403_recovery_interval_minutes": 30}, nil, false}, {"default proxy", map[string]any{"openai_excel_bps_proxy_source": "mihomo"}, nil, true}, diff --git a/backend/internal/server/routes/admin.go b/backend/internal/server/routes/admin.go index 78ecf2984816..b4f73aa266ac 100644 --- a/backend/internal/server/routes/admin.go +++ b/backend/internal/server/routes/admin.go @@ -947,6 +947,7 @@ func registerScheduledTestRoutes(admin *gin.RouterGroup, h *handler.Handlers) { admin.GET("/account-ops/auto-config", h.Admin.AccountOps.GetAutoConfig) admin.PUT("/account-ops/auto-config", h.Admin.AccountOps.SaveAutoConfig) admin.GET("/account-ops/auto-config/events", h.Admin.AccountOps.ListAutoConfigEvents) + admin.PUT("/account-ops/accounts/:id/lifecycle", h.Admin.AccountOps.SetOAuthLifecycle) admin.GET("/account-ops/config", h.Admin.AccountOps.GetConfig) admin.PUT("/account-ops/config", h.Admin.AccountOps.SaveConfig) admin.PUT("/account-ops/notification-settings", h.Admin.AccountOps.SaveNotificationSettings) diff --git a/backend/internal/service/account_ops.go b/backend/internal/service/account_ops.go index 4e03aba93307..3619fc9f4f57 100644 --- a/backend/internal/service/account_ops.go +++ b/backend/internal/service/account_ops.go @@ -4,8 +4,6 @@ import ( "context" "errors" "net/http" - "net/mail" - "strings" "sync" "sync/atomic" "time" @@ -40,12 +38,10 @@ func ValidateAccountOpsConfig(c AccountOpsConfig) (err error) { if c.CooldownMinutes < 5 || c.CooldownMinutes > 1440 { return errors.New("cooldown_minutes must be between 5 and 1440") } - if c.Recipient != "" { - address, err := mail.ParseAddress(c.Recipient) - if err != nil || address.Address != c.Recipient || strings.ContainsAny(c.Recipient, "\r\n") || len(c.Recipient) > 254 { - return errors.New("recipient must be one email address") - } + if _, err := parseAccountOpsRecipients(c.Recipient); err != nil { + return err } + if err := validateAccountOpsExtras(c); err != nil { return err } @@ -186,6 +182,15 @@ func (s *AccountOpsService) Observe(account *Account, status int, headers http.H name = name[:120] } group := accountOpsGroupAssignment(account, c) + // Linked wallets use one authoritative balance-threshold episode. Request + // failures from individual keys must not send a second low-balance alert. + if kind == "balance_low" && accountOpsProviderID(c, account.ID) > 0 { + for _, rule := range c.BalanceThresholds { + if rule.Enabled && thresholdAccountGroup(nil, rule.AccountID, c).ID == group.ID { + return + } + } + } event := AccountOpsEvent{AccountID: account.ID, AccountName: string(name), GroupID: group.ID, GroupName: group.Name, Kind: kind, Signal: signal, HTTPStatus: status} // The hot request path never waits for SMTP or the database. Raw errors and credentials do not enter the queue. select { diff --git a/backend/internal/service/account_ops_balance.go b/backend/internal/service/account_ops_balance.go index 466b6398df2c..6511a53ed7de 100644 --- a/backend/internal/service/account_ops_balance.go +++ b/backend/internal/service/account_ops_balance.go @@ -22,6 +22,8 @@ type AccountOpsUsageWindow struct { ResetsAt *time.Time `json:"resets_at,omitempty"` } type AccountOpsBalanceAccount struct { + ProviderID int64 `json:"provider_id,omitempty"` + OAuthLifecycle string `json:"oauth_lifecycle,omitempty"` AccountID int64 `json:"account_id"` AccountName string `json:"account_name"` GroupID string `json:"group_id"` @@ -254,7 +256,7 @@ func (s *AccountOpsService) ThresholdGroups(ctx context.Context) ([]AccountOpsGr // Keep the collection JSON-shaped even for an empty group. A nil slice // becomes `null`, while the frontend treats account_ids as an array. accountIDs := append([]int64{}, group.AccountIDs...) - out = append(out, AccountOpsGroupView{ID: group.ID, Name: name, DefaultName: name, Provider: provider, Site: site, Mode: "manual", AccountIDs: accountIDs}) + out = append(out, AccountOpsGroupView{ProviderID: group.ProviderID, ID: group.ID, Name: name, DefaultName: name, Provider: provider, Site: site, Mode: "manual", AccountIDs: accountIDs}) } return out, nil } @@ -271,13 +273,17 @@ func (s *AccountOpsService) thresholdAccounts(ctx context.Context, apiOnly bool) if err != nil { return nil, err } + ctx = s.withAccountOpsProviderSnapshots(ctx, c) now := time.Now() for i := range accounts { a := &accounts[i] if a.Type != AccountTypeAPIKey && (apiOnly || a.Type != AccountTypeOAuth) { continue } - item := opsBalanceAccount(a, now) + item := s.opsBalanceAccount(ctx, a, c, now) + if a.Type == AccountTypeOAuth { + item.OAuthLifecycle = accountOpsOAuthLifecycle(a) + } group := accountOpsGroupAssignment(a, c) item.GroupID, item.GroupName, item.GroupMode = group.ID, group.Name, group.Mode if a.Type == AccountTypeOAuth { @@ -358,7 +364,7 @@ func (s *AccountOpsService) assessThreshold(ctx context.Context, a *Account, kin if kind == "balance_threshold" && a.Type == AccountTypeAPIKey { for _, r := range c.BalanceThresholds { if r.AccountID == a.ID && r.Enabled { - item := opsBalanceAccount(a, now) + item := s.opsBalanceAccount(ctx, a, c, now) if item.Balance == nil || item.Unit != r.Unit { return nil, "suppressed" } @@ -431,8 +437,9 @@ func thresholdRuleIDs(c AccountOpsConfig, kind string) []int64 { func accountOpsGroupCriteria(c AccountOpsConfig, kind string, members []accountOpsThresholdMember) (string, bool, bool, bool) { type part struct { - ID int64 `json:"id"` - Criteria string `json:"criteria"` + ProviderID int64 `json:"provider_id,omitempty"` + ID int64 `json:"id"` + Criteria string `json:"criteria"` } parts := make([]part, 0, len(members)) alert, recovery := false, true @@ -441,15 +448,39 @@ func accountOpsGroupCriteria(c AccountOpsConfig, kind string, members []accountO if !enabled || criteria == "" { continue } - parts = append(parts, part{ID: member.id, Criteria: criteria}) + parts = append(parts, part{ProviderID: accountOpsProviderID(c, member.id), ID: member.id, Criteria: criteria}) alert = alert || notifyAlert recovery = recovery && notifyRecovery } if len(parts) == 0 { return "", false, alert, recovery } + // A provider wallet keeps its episode when keys are added or reordered. + // Only a different provider or a changed set of thresholds invalidates it. + if kind == "balance_threshold" && parts[0].ProviderID > 0 { + providerID := parts[0].ProviderID + criteria := []string{} + seen := map[string]bool{} + shared := true + for _, p := range parts { + if p.ProviderID != providerID { + shared = false + break + } + if !seen[p.Criteria] { + criteria = append(criteria, p.Criteria) + seen[p.Criteria] = true + } + } + if shared { + sort.Strings(criteria) + raw, _ := json.Marshal([]any{providerID, criteria}) + sum := sha256.Sum256(raw) + return hex.EncodeToString(sum[:]), true, alert, recovery + } + } sort.Slice(parts, func(i, j int) bool { return parts[i].ID < parts[j].ID }) - if len(parts) == 1 { + if len(parts) == 1 && parts[0].ProviderID == 0 { return parts[0].Criteria, true, alert, recovery } raw, _ := json.Marshal(parts) @@ -543,6 +574,7 @@ func (s *AccountOpsService) scanBalances(ctx context.Context) { if !c.Enabled || s.accounts == nil { return } + ctx = s.withAccountOpsProviderSnapshots(ctx, c) ids := append(thresholdRuleIDs(c, "balance_threshold"), thresholdRuleIDs(c, "quota_threshold")...) accounts := s.loadThresholdAccounts(ctx, ids) @@ -574,6 +606,7 @@ func (s *AccountOpsService) scanBalances(ctx context.Context) { } } func (s *AccountOpsService) recheckThreshold(ctx context.Context, e *AccountOpsEvent, c AccountOpsConfig) (string, error) { + ctx = s.withAccountOpsProviderSnapshots(ctx, c) if s.accounts == nil { return "failed", errors.New("account repository unavailable") } @@ -622,6 +655,13 @@ func (s *AccountOpsService) recheckThreshold(ctx context.Context, e *AccountOpsE } } if e.Phase == "recovery" { + if accountOpsProviderID(c, e.AccountID) > 0 { + fresh, state := s.assessThresholdGroup(query, group, members, e.Kind, c, time.Now()) + if state != "resolved" || fresh == nil { + return "deferred", nil + } + e.Details = fresh.Details + } return "active", nil } fresh, state := s.assessThresholdGroup(query, group, members, e.Kind, c, time.Now()) diff --git a/backend/internal/service/account_ops_config.go b/backend/internal/service/account_ops_config.go index 2efeead19f10..d398959317bc 100644 --- a/backend/internal/service/account_ops_config.go +++ b/backend/internal/service/account_ops_config.go @@ -227,12 +227,19 @@ func validateAccountOpsExtras(c AccountOpsConfig) error { func validateAccountOpsGroups(groups []AccountOpsGroup) error { seenGroups := map[string]bool{} + seenProviders := map[int64]bool{} seenAccounts := map[int64]bool{} for _, group := range groups { if !opsGroupID.MatchString(group.ID) || seenGroups[group.ID] { return errors.New("invalid or duplicate account group ID") } seenGroups[group.ID] = true + if group.ProviderID < 0 || (group.ProviderID > 0 && seenProviders[group.ProviderID]) { + return errors.New("each provider must have a single balance group") + } + if group.ProviderID > 0 { + seenProviders[group.ProviderID] = true + } name := strings.TrimSpace(group.Name) if name != "" { if err := validateAccountOpsChannelName(name); err != nil { @@ -324,7 +331,11 @@ func (s *AccountOpsService) saveConfigWithRuleScope(ctx context.Context, c Accou } c.BalanceThresholds = append([]AccountOpsBalanceRule(nil), c.BalanceThresholds...) c.QuotaThresholds = append([]AccountOpsQuotaRule(nil), c.QuotaThresholds...) - c.Recipient = strings.TrimSpace(c.Recipient) + recipients, err := parseAccountOpsRecipients(c.Recipient) + if err != nil { + return accountOpsConfigValidation(err.Error()) + } + c.Recipient = strings.Join(recipients, ", ") if len(c.Webhooks) > 5 || len(c.BalanceThresholds)+len(c.QuotaThresholds) > 1000 { return accountOpsConfigValidation("too many robot destinations or threshold rules") } diff --git a/backend/internal/service/account_ops_group.go b/backend/internal/service/account_ops_group.go index 6f979d57cb24..cb8c44e57213 100644 --- a/backend/internal/service/account_ops_group.go +++ b/backend/internal/service/account_ops_group.go @@ -17,6 +17,7 @@ const ( // AccountOpsGroup is a persisted, manually managed account group. type AccountOpsGroup struct { + ProviderID int64 `json:"provider_id,omitempty"` ID string `json:"id"` Name string `json:"name"` Provider string `json:"provider,omitempty"` @@ -34,6 +35,7 @@ type AccountOpsGroupAssignment struct { } type AccountOpsGroupView struct { + ProviderID int64 `json:"provider_id,omitempty"` ID string `json:"id"` Name string `json:"name"` DefaultName string `json:"default_name"` diff --git a/backend/internal/service/account_ops_group_manual.go b/backend/internal/service/account_ops_group_manual.go index 4d5b4c46cc82..cbf746f5334f 100644 --- a/backend/internal/service/account_ops_group_manual.go +++ b/backend/internal/service/account_ops_group_manual.go @@ -183,6 +183,7 @@ func (s *AccountOpsService) reconcileAccountOpsGroups(ctx context.Context, c *Ac for i := range accounts { byID[accounts[i].ID] = &accounts[i] } + providerChanged := reconcileAccountOpsProviderGroups(c, accounts) original := cloneAccountOpsGroups(c.Groups) wasInitialized := c.GroupsInitialized bootstrap := !wasInitialized @@ -222,7 +223,7 @@ func (s *AccountOpsService) reconcileAccountOpsGroups(ctx context.Context, c *Ac for _, rule := range c.QuotaThresholds { knownAccounts[rule.AccountID] = true } - changed := !reflect.DeepEqual(original, groups) + changed := providerChanged || !reflect.DeepEqual(original, groups) for i := range groups { provider, site := accountOpsGroupSite(groups[i], byID) @@ -314,7 +315,7 @@ func (s *AccountOpsService) reconcileAccountOpsGroups(ctx context.Context, c *Ac site = assignment.Site } for groupIndex := range groups { - if canonicalAccountOpsSite(groups[groupIndex].Site) == canonicalAccountOpsSite(site) && site != "" { + if groups[groupIndex].ProviderID == 0 && canonicalAccountOpsSite(groups[groupIndex].Site) == canonicalAccountOpsSite(site) && site != "" { if appendUniqueAccountOpsGroupMember(&groups[groupIndex], account.ID) { changed = true } diff --git a/backend/internal/service/account_ops_lifecycle.go b/backend/internal/service/account_ops_lifecycle.go new file mode 100644 index 000000000000..f7fc517f580d --- /dev/null +++ b/backend/internal/service/account_ops_lifecycle.go @@ -0,0 +1,33 @@ +package service + +import ( + "context" + "errors" +) + +const accountOpsOAuthLifecycleKey = "oauth_lifecycle" + +func accountOpsOAuthLifecycle(a *Account) string { + if value, _ := a.Extra[accountOpsOAuthLifecycleKey].(string); value == "long_term" { + return value + } + // Existing OAuth inventory is disposable. This label never deletes accounts + // or treats an expiring, refreshable access token as account expiry. + return "temporary" +} +func (s *AccountOpsService) SetOAuthLifecycle(ctx context.Context, id int64, value string) error { + if value != "temporary" && value != "long_term" { + return accountOpsConfigValidation("invalid OAuth lifecycle") + } + if s.accounts == nil { + return errors.New("account repository unavailable") + } + account, err := s.accounts.GetByID(ctx, id) + if err != nil { + return err + } + if account == nil || account.Type != AccountTypeOAuth { + return accountOpsConfigValidation("lifecycle applies only to OAuth accounts") + } + return s.accounts.UpdateExtra(ctx, id, map[string]any{accountOpsOAuthLifecycleKey: value}) +} diff --git a/backend/internal/service/account_ops_notification.go b/backend/internal/service/account_ops_notification.go index 4be4a02e284f..4fc019677ac4 100644 --- a/backend/internal/service/account_ops_notification.go +++ b/backend/internal/service/account_ops_notification.go @@ -43,7 +43,11 @@ func (s *AccountOpsService) notificationMessage(e *AccountOpsEvent) accountOpsMe if e.GroupName != "" && !strings.HasPrefix(e.GroupID, "account:") { accountLabel = e.GroupName } - fields := [][2]string{{"账号", fmt.Sprintf("%s(#%d)", accountLabel, e.AccountID)}, {"提醒原因", reason}} + accountValue := fmt.Sprintf("%s(#%d)", accountLabel, e.AccountID) + if e.GroupName != "" && !strings.HasPrefix(e.GroupID, "account:") { + accountValue = accountLabel + } + fields := [][2]string{{"账号", accountValue}, {"提醒原因", reason}} if e.Details != nil { d := e.Details if d.UsedPercent != nil { @@ -112,7 +116,7 @@ func (s *AccountOpsService) compactRobotMessage(e *AccountOpsEvent, title string if account == "" { account = "未命名账号" } - if e.AccountID > 0 { + if e.AccountID > 0 && (e.GroupName == "" || strings.HasPrefix(e.GroupID, "account:")) { account = fmt.Sprintf("%s (#%d)", account, e.AccountID) } balance, threshold := "未获取", "未获取" @@ -207,18 +211,28 @@ func (s *AccountOpsService) deliverNotification(ctx context.Context, e *AccountO e.Deliveries = map[string]AccountOpsDelivery{} } destinations := []AccountOpsWebhook{} - if c.Recipient != "" { - destinations = append(destinations, AccountOpsWebhook{ID: opsEmailIdentity(c.Recipient), Provider: "email"}) + for _, recipient := range c.emailRecipients() { + destinations = append(destinations, AccountOpsWebhook{ID: opsEmailIdentity(recipient), Provider: "email"}) } for _, w := range c.Webhooks { if w.Enabled { destinations = append(destinations, w) } } - // Six channels at twelve seconds each, including rate waits, remain below the two minute lease. + // Share a bounded send budget across recipients and robots within the + // two-minute delivery lease. Persist each recipient receipt independently. + sendBudget := 15 * time.Second + if len(destinations) > 6 { + sendBudget = 90 * time.Second / time.Duration(len(destinations)) + } deliveryCtx, stop := context.WithTimeout(ctx, 100*time.Second) defer stop() for _, w := range destinations { + if deliveryCtx.Err() != nil { + state = "failed" + break + } + recipient := "" query, cancel := context.WithTimeout(deliveryCtx, 5*time.Second) s.settingsMu.Lock() freshConfig, configErr := s.loadConfig(query) @@ -235,10 +249,10 @@ func (s *AccountOpsService) deliverNotification(ctx context.Context, e *AccountO return } if w.Provider == "email" { - if c.Recipient == "" { + recipient = c.emailRecipient(w.ID) + if recipient == "" { continue } - w.ID = opsEmailIdentity(c.Recipient) } else { found := false for _, current := range c.Webhooks { @@ -282,7 +296,7 @@ func (s *AccountOpsService) deliverNotification(ctx context.Context, e *AccountO } message = s.notificationMessage(e) } - send, cancel := context.WithTimeout(deliveryCtx, 15*time.Second) + send, cancel := context.WithTimeout(deliveryCtx, sendBudget) if w.Provider != "email" { if reserve, ok := s.repo.(accountOpsRobotReservationRepository); ok { identity, identityErr := s.robotIdentity(w) @@ -316,7 +330,8 @@ func (s *AccountOpsService) deliverNotification(ctx context.Context, e *AccountO } unchanged := false if w.Provider == "email" { - unchanged = opsEmailIdentity(latest.Recipient) == key && latest.Recipient != "" + recipient = latest.emailRecipient(key) + unchanged = recipient != "" } else { for _, current := range latest.Webhooks { if current.ID == w.ID && current.Provider == w.Provider && current.revision == w.revision && current.Enabled { @@ -351,7 +366,7 @@ func (s *AccountOpsService) deliverNotification(ctx context.Context, e *AccountO if s.email == nil { err = errors.New("email unavailable") } else { - err = s.email.SendEmail(send, c.Recipient, message.title, message.html) + err = s.email.SendEmail(send, recipient, message.title, message.html) } } else { err = s.sendRobot(send, w, message) @@ -361,6 +376,12 @@ func (s *AccountOpsService) deliverNotification(ctx context.Context, e *AccountO out.Name = "" if w.Provider == "email" { out.Name = c.EmailName + if len(c.emailRecipients()) > 1 { + out.Name = recipient + if c.EmailName != "" { + out.Name = c.EmailName + " · " + recipient + } + } } else if w.Name != nil { out.Name = *w.Name } diff --git a/backend/internal/service/account_ops_providers.go b/backend/internal/service/account_ops_providers.go new file mode 100644 index 000000000000..5a7e93929fb6 --- /dev/null +++ b/backend/internal/service/account_ops_providers.go @@ -0,0 +1,182 @@ +package service + +import ( + "context" + "math" + "sort" + "strconv" + "time" +) + +type accountOpsProviderReader interface { + ReadAccountOpsProviders(context.Context, []int64) ([]MatureProvider, error) +} +type accountOpsProviderContextKey struct{} +type accountOpsProviderSnapshots map[int64]MatureProvider + +func accountOpsProviderID(c AccountOpsConfig, accountID int64) int64 { + for _, g := range c.Groups { + for _, id := range g.AccountIDs { + if id == accountID { + return g.ProviderID + } + } + } + return 0 +} +func (s *AccountOpsService) withAccountOpsProviderSnapshots(ctx context.Context, c AccountOpsConfig) context.Context { + ids := []int64{} + seen := map[int64]bool{} + for _, g := range c.Groups { + if g.ProviderID > 0 && !seen[g.ProviderID] { + ids = append(ids, g.ProviderID) + seen[g.ProviderID] = true + } + } + snapshots := accountOpsProviderSnapshots{} + if len(ids) > 0 { + if reader, ok := s.accounts.(accountOpsProviderReader); ok { + // A failed read is unknown. Never substitute a member key's quota for a + // missing provider wallet or manufacture a recovered/zero balance. + if providers, err := reader.ReadAccountOpsProviders(ctx, ids); err == nil { + for _, p := range providers { + snapshots[p.ID] = p + } + } + } + } + return context.WithValue(ctx, accountOpsProviderContextKey{}, snapshots) +} +func (s *AccountOpsService) opsBalanceAccount(ctx context.Context, a *Account, c AccountOpsConfig, now time.Time) AccountOpsBalanceAccount { + id := accountOpsProviderID(c, a.ID) + if id == 0 || a.Type != AccountTypeAPIKey { + return opsBalanceAccount(a, now) + } + item := AccountOpsBalanceAccount{AccountID: a.ID, AccountName: a.Name, Platform: a.Platform, Type: a.Type, ProviderID: id, Unit: "USD", BalanceStatus: "unknown", UsageWindows: []AccountOpsUsageWindow{}} + snapshots, ok := ctx.Value(accountOpsProviderContextKey{}).(accountOpsProviderSnapshots) + if !ok { + ctx = s.withAccountOpsProviderSnapshots(ctx, c) + snapshots, _ = ctx.Value(accountOpsProviderContextKey{}).(accountOpsProviderSnapshots) + } + p, exists := snapshots[id] + if !exists { + return item + } + item.Unit = p.Metrics.Currency + item.ReceivedAt = p.ObservedAt + item.ProbeEnabled = p.RefreshIntervalSeconds > 0 + if p.Status != "connected" { + item.BalanceStatus = "failed" + return item + } + if p.Metrics.Balance == nil || math.IsNaN(*p.Metrics.Balance) || math.IsInf(*p.Metrics.Balance, 0) || p.ObservedAt == nil || p.Metrics.Currency == "" { + return item + } + ttl := time.Hour + if p.RefreshIntervalSeconds > 0 { + ttl = 2 * time.Duration(p.RefreshIntervalSeconds) * time.Second + } + if p.ObservedAt.After(now) { + return item + } + if now.Sub(*p.ObservedAt) > ttl { + item.BalanceStatus = "stale" + item.LastKnownBalance = p.Metrics.Balance + return item + } + item.BalanceStatus = "ok" + item.Balance = p.Metrics.Balance + return item +} +func (s *AccountOpsService) validateAccountOpsProviderGroups(ctx context.Context, c AccountOpsConfig, accounts []Account) error { + byID := map[int64]Account{} + for _, a := range accounts { + byID[a.ID] = a + } + for _, g := range c.Groups { + if g.ProviderID == 0 { + continue + } + for _, id := range g.AccountIDs { + if a, ok := byID[id]; ok && a.Type != AccountTypeAPIKey { + return accountOpsConfigValidation("provider balances apply only to API key accounts") + } + } + } + return nil +} +func importedAccountOpsProvider(a Account) (int64, string) { + if a.Type != AccountTypeAPIKey { + return 0, "" + } + binding, _ := a.Extra["provider_binding"].(map[string]any) + id, ok := opsNumber(binding["provider_id"]) + if !ok || id <= 0 || id > 9007199254740991 || math.Trunc(id) != id { + return 0, "" + } + name, _ := binding["provider_name"].(string) + return int64(id), name +} + +// Imported keys already carry #850's provider ID. Materialize one stable +// monitor group per provider, without guessing shared wallets from hostnames. +func reconcileAccountOpsProviderGroups(c *AccountOpsConfig, accounts []Account) bool { + linked := map[int64]int{} + known := map[int64]bool{} + for _, id := range c.UngroupedAccountIDs { + known[id] = true + } + for i, g := range c.Groups { + for _, id := range g.AccountIDs { + known[id] = true + } + if g.ProviderID > 0 { + linked[g.ProviderID] = i + } + } + ordered := append([]Account(nil), accounts...) + sort.Slice(ordered, func(i, j int) bool { return ordered[i].ID < ordered[j].ID }) + changed := false + for _, a := range ordered { + // Existing manual membership (including an explicit unlink) wins. + if known[a.ID] { + continue + } + providerID, name := importedAccountOpsProvider(a) + if providerID == 0 { + continue + } + index, exists := linked[providerID] + if !exists { + if name == "" { + name = "Provider #" + strconv.FormatInt(providerID, 10) + } + runes := []rune(name) + if len(runes) > 80 { + name = string(runes[:80]) + } + c.Groups = append(c.Groups, AccountOpsGroup{ID: "manual-provider-" + strconv.FormatInt(providerID, 10), Name: name, Provider: "provider", ProviderID: providerID, AccountIDs: []int64{}}) + index = len(c.Groups) - 1 + linked[providerID] = index + changed = true + } + if appendUniqueAccountOpsGroupMember(&c.Groups[index], a.ID) { + changed = true + } + for i := range c.Groups { + if i == index { + continue + } + next := []int64{} + for _, id := range c.Groups[i].AccountIDs { + if id == a.ID { + changed = true + } else { + next = append(next, id) + } + } + c.Groups[i].AccountIDs = next + } + } + return changed +} diff --git a/backend/internal/service/account_ops_providers_test.go b/backend/internal/service/account_ops_providers_test.go new file mode 100644 index 000000000000..7aaa8032151d --- /dev/null +++ b/backend/internal/service/account_ops_providers_test.go @@ -0,0 +1,127 @@ +package service + +import ( + "context" + "errors" + "github.com/stretchr/testify/require" + "testing" + "time" +) + +type opsProviderAccounts struct { + accountOpsGroupAccounts + providers []MatureProvider + providerErr error + reads int +} + +func (r *opsProviderAccounts) ReadAccountOpsProviders(context.Context, []int64) ([]MatureProvider, error) { + r.reads++ + return r.providers, r.providerErr +} +func TestAccountOpsProviderImportedKeysShareOneGroup(t *testing.T) { + makeKey := func(id int64, provider float64, kind string) Account { + return Account{ID: id, Type: kind, Extra: map[string]any{"provider_binding": map[string]any{"provider_id": provider, "provider_name": "Fixture provider"}}} + } + accounts := []Account{makeKey(1, 7, AccountTypeAPIKey), makeKey(2, 7, AccountTypeAPIKey), makeKey(3, 8, AccountTypeAPIKey), makeKey(4, 7, AccountTypeOAuth)} + cfg := defaultAccountOpsConfig() + require.True(t, reconcileAccountOpsProviderGroups(&cfg, accounts)) + require.Len(t, cfg.Groups, 2) + require.Equal(t, []int64{1, 2}, cfg.Groups[0].AccountIDs) + require.Equal(t, int64(7), cfg.Groups[0].ProviderID) + require.Equal(t, []int64{3}, cfg.Groups[1].AccountIDs) + require.False(t, reconcileAccountOpsProviderGroups(&cfg, accounts)) + cfg.Groups = append(cfg.Groups, AccountOpsGroup{ID: "manual-duplicate", ProviderID: 7}) + require.Error(t, validateAccountOpsGroups(cfg.Groups)) +} +func TestAccountOpsProviderBalanceIsSharedAndDoesNotFallBackToKeyQuota(t *testing.T) { + now := time.Now() + fresh := now.Add(time.Hour) + balance := 4.0 + makeKey := func(id int64) *Account { + return &Account{ID: id, Name: "Fixture", Type: AccountTypeAPIKey, Platform: PlatformOpenAI, Extra: map[string]any{UpstreamBillingProbeExtraKey: &UpstreamBillingProbeSnapshot{Balance: &UpstreamBalanceSnapshot{Status: "ok", ReceivedAt: &now, FreshUntil: &fresh, Data: map[string]any{"remaining": 999.0, "unit": "USD"}}}}} + } + accounts := &opsProviderAccounts{accountOpsGroupAccounts: accountOpsGroupAccounts{items: map[int64]*Account{1: makeKey(1), 2: makeKey(2)}}, providers: []MatureProvider{{ID: 7, Status: "connected", Metrics: ProviderMetrics{Balance: &balance, Currency: "USD"}, ObservedAt: &now, RefreshIntervalSeconds: 300}}} + repo := &opsOnceRepoStub{} + svc := NewAccountOpsService(&accountOpsSettingsStub{}, repo, nil) + svc.SetNotificationDependencies(accounts, nil, false, "UTC") + cfg := defaultAccountOpsConfig() + cfg.Enabled = true + cfg.Recipient = "one@example.test,two@example.test" + cfg.Groups = []AccountOpsGroup{{ID: "manual-provider-7", ProviderID: 7, Name: "Shared", AccountIDs: []int64{1, 2}}} + cfg.BalanceThresholds = []AccountOpsBalanceRule{{AccountID: 1, Enabled: true, Threshold: 50, Unit: "USD"}, {AccountID: 2, Enabled: true, Threshold: 50, Unit: "USD"}} + svc.config.Store(cfg) + svc.scanBalances(context.Background()) + require.Len(t, repo.events, 1) + require.Equal(t, "active", repo.observations[0]) + require.Equal(t, 4.0, *repo.events[0].Details.Balance) + require.Equal(t, 1, accounts.reads) + svc.Observe(accounts.items[1], 402, nil, []byte(`{"error":{"code":"insufficient_balance"}}`)) + require.Empty(t, svc.queue, "member failures must not generate a duplicate wallet notification") + for _, state := range []string{"stale", "failed", "missing", "storage_error"} { + t.Run(state, func(t *testing.T) { + saved := append([]MatureProvider(nil), accounts.providers...) + defer func() { accounts.providers = saved; accounts.providerErr = nil }() + switch state { + case "stale": + old := now.Add(-time.Hour) + accounts.providers[0].ObservedAt = &old + case "failed": + accounts.providers[0].Status = "disconnected" + case "missing": + accounts.providers = nil + case "storage_error": + accounts.providerErr = errors.New("offline") + } + item := svc.opsBalanceAccount(context.Background(), accounts.items[1], cfg, now) + require.Nil(t, item.Balance) + require.NotEqual(t, "ok", item.BalanceStatus) + }) + } + old, _, _, _ := cfg.ThresholdGroupCriteria(cfg.Groups[0].ID, "balance_threshold") + cfg.Groups[0].AccountIDs = append(cfg.Groups[0].AccountIDs, 3) + cfg.BalanceThresholds = append(cfg.BalanceThresholds, AccountOpsBalanceRule{AccountID: 3, Enabled: true, Threshold: 50, Unit: "USD"}) + withExtraKey, _, _, _ := cfg.ThresholdGroupCriteria(cfg.Groups[0].ID, "balance_threshold") + require.Equal(t, old, withExtraKey, "adding a Key must not reopen the same provider wallet episode") + + cfg.Groups[0].ProviderID = 8 + updated, _, _, _ := cfg.ThresholdGroupCriteria(cfg.Groups[0].ID, "balance_threshold") + require.NotEqual(t, old, updated, "relinking must invalidate queued alerts and recoveries") +} +func TestAccountOpsProviderKeepsNativeCurrency(t *testing.T) { + now := time.Now() + amount := -2.0 + a := &Account{ID: 1, Type: AccountTypeAPIKey} + repo := &opsProviderAccounts{accountOpsGroupAccounts: accountOpsGroupAccounts{items: map[int64]*Account{1: a}}, providers: []MatureProvider{{ID: 7, Status: "connected", ObservedAt: &now, Metrics: ProviderMetrics{Balance: &amount, Currency: "CNY"}}}} + svc := NewAccountOpsService(&accountOpsSettingsStub{}, &accountOpsRepoStub{}, nil) + svc.SetNotificationDependencies(repo, nil, false, "UTC") + cfg := defaultAccountOpsConfig() + cfg.Groups = []AccountOpsGroup{{ID: "manual-provider-7", ProviderID: 7, AccountIDs: []int64{1}}} + cfg.BalanceThresholds = []AccountOpsBalanceRule{{AccountID: 1, Enabled: true, Threshold: 50, Unit: "USD"}} + item := svc.opsBalanceAccount(context.Background(), a, cfg, now) + require.Equal(t, "CNY", item.Unit) + require.Equal(t, -2.0, *item.Balance) + event, state := svc.assessThreshold(context.Background(), a, "balance_threshold", cfg, now) + require.Nil(t, event) + require.Equal(t, "suppressed", state, "do not compare a CNY wallet with a USD threshold") +} + +func TestAccountOpsProviderRecoveryRechecksCurrentWallet(t *testing.T) { + now := time.Now() + balance := 100.0 + a := &Account{ID: 1, Type: AccountTypeAPIKey} + accounts := &opsProviderAccounts{accountOpsGroupAccounts: accountOpsGroupAccounts{items: map[int64]*Account{1: a}}, providers: []MatureProvider{{ID: 7, Status: "connected", ObservedAt: &now, Metrics: ProviderMetrics{Balance: &balance, Currency: "USD"}}}} + svc := NewAccountOpsService(&accountOpsSettingsStub{}, &opsOnceRepoStub{eligible: true}, nil) + svc.SetNotificationDependencies(accounts, nil, false, "UTC") + cfg := defaultAccountOpsConfig() + cfg.Groups = []AccountOpsGroup{{ID: "manual-provider-7", ProviderID: 7, Name: "Shared", AccountIDs: []int64{1}}} + cfg.BalanceThresholds = []AccountOpsBalanceRule{{AccountID: 1, Enabled: true, Threshold: 50, Unit: "USD"}} + event := &AccountOpsEvent{AccountID: 1, GroupID: "manual-provider-7", Kind: "balance_threshold", Phase: "recovery"} + state, err := svc.recheckThreshold(context.Background(), event, cfg) + require.NoError(t, err) + require.Equal(t, "active", state) + accounts.providers = nil + state, err = svc.recheckThreshold(context.Background(), event, cfg) + require.NoError(t, err) + require.Equal(t, "deferred", state, "removed provider must not announce recovery using an old snapshot") +} diff --git a/backend/internal/service/account_ops_recipients.go b/backend/internal/service/account_ops_recipients.go new file mode 100644 index 000000000000..afa56a3d3450 --- /dev/null +++ b/backend/internal/service/account_ops_recipients.go @@ -0,0 +1,55 @@ +package service + +import ( + "errors" + "net/mail" + "strings" +) + +const maxAccountOpsRecipients = 10 + +// Keep the existing string field and single-address delivery identities. Lists +// are normalized on save; no recipient address is added to event identity keys. +func parseAccountOpsRecipients(raw string) ([]string, error) { + if len(raw) > 5120 || strings.ContainsRune(raw, '\r') { + return nil, errors.New("invalid recipient email list") + } + recipients := []string{} + seen := map[string]bool{} + for _, part := range strings.FieldsFunc(raw, func(r rune) bool { return r == ',' || r == ';' || r == '\n' }) { + value := strings.TrimSpace(part) + if value == "" { + continue + } + address, err := mail.ParseAddress(value) + if err != nil || address.Address != value || len(value) > 254 { + return nil, errors.New("recipients must contain plain email addresses") + } + identity := strings.ToLower(value) + if !seen[identity] { + recipients = append(recipients, value) + seen[identity] = true + } + } + if len(recipients) > maxAccountOpsRecipients { + return nil, errors.New("at most 10 recipient email addresses are allowed") + } + if strings.TrimSpace(raw) != "" && len(recipients) == 0 { + return nil, errors.New("recipient email list is empty") + } + return recipients, nil +} + +func (c AccountOpsConfig) emailRecipients() []string { + recipients, _ := parseAccountOpsRecipients(c.Recipient) + return recipients +} + +func (c AccountOpsConfig) emailRecipient(identity string) string { + for _, recipient := range c.emailRecipients() { + if opsEmailIdentity(recipient) == identity { + return recipient + } + } + return "" +} diff --git a/backend/internal/service/account_ops_scoped.go b/backend/internal/service/account_ops_scoped.go index 191c8cc295c8..4eca619cc55b 100644 --- a/backend/internal/service/account_ops_scoped.go +++ b/backend/internal/service/account_ops_scoped.go @@ -54,6 +54,10 @@ func (s *AccountOpsService) SaveGroups(ctx context.Context, groups []AccountOpsG if err != nil { return err } + reconcileAccountOpsProviderGroups(c, accounts) + if err := s.validateAccountOpsProviderGroups(ctx, *c, accounts); err != nil { + return err + } byID := make(map[int64]*Account, len(accounts)) for i := range accounts { byID[accounts[i].ID] = &accounts[i] @@ -244,6 +248,7 @@ func (s *AccountOpsService) SaveRuleGroupsBatch(ctx context.Context, groups []Ac } } return s.updateConfig(ctx, func(c *AccountOpsConfig) error { + ctx = s.withAccountOpsProviderSnapshots(ctx, *c) for _, group := range groups { for _, id := range group.AccountIDs { if err := s.applyRule(ctx, c, id, group.Rule, true); err != nil { @@ -315,7 +320,7 @@ func (s *AccountOpsService) applyRule(ctx context.Context, c *AccountOpsConfig, if err != nil { return err } - if requireCurrentUnit && opsBalanceAccount(account, time.Now()).Unit != r.Unit { + if requireCurrentUnit && s.opsBalanceAccount(ctx, account, *c, time.Now()).Unit != r.Unit { return accountOpsConfigValidation("batch balance rules require the same current account unit") } if index < 0 { diff --git a/backend/internal/service/account_ops_team_test.go b/backend/internal/service/account_ops_team_test.go new file mode 100644 index 000000000000..8db12832d9ab --- /dev/null +++ b/backend/internal/service/account_ops_team_test.go @@ -0,0 +1,106 @@ +package service + +import ( + "context" + "errors" + "github.com/stretchr/testify/require" + "strings" + "testing" +) + +func TestAccountOpsRecipientListValidationAndNormalization(t *testing.T) { + recipients, err := parseAccountOpsRecipients(" one@example.test; two@example.test\nONE@example.test ") + require.NoError(t, err) + require.Equal(t, []string{"one@example.test", "two@example.test"}, recipients) + for _, bad := range []string{"Name ", "one@example.test\r\nBcc: x@example.test", "one@example.test\nBcc: x@example.test", ";", strings.Repeat("a", 5121)} { + _, err = parseAccountOpsRecipients(bad) + require.Error(t, err) + } + cfg := defaultAccountOpsConfig() + cfg.Recipient = " one@example.test;two@example.test\nONE@example.test " + svc := NewAccountOpsService(&accountOpsSettingsStub{}, &accountOpsRepoStub{}, nil) + require.NoError(t, svc.SaveConfig(context.Background(), cfg)) + got, err := svc.GetConfig(context.Background()) + require.NoError(t, err) + require.Equal(t, "one@example.test, two@example.test", got.Recipient) + list := []string{} + for _, name := range []string{"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k"} { + list = append(list, name+"@example.test") + } + _, err = parseAccountOpsRecipients(strings.Join(list, ",")) + require.Error(t, err) +} + +type opsTeamSender struct { + calls map[string]int + failed string + after func(string) +} + +func (s *opsTeamSender) SendEmail(_ context.Context, to, _, _ string) error { + s.calls[to]++ + if s.after != nil { + s.after(to) + } + if to == s.failed { + return errors.New("synthetic delivery failure") + } + return nil +} +func TestAccountOpsMultipleRecipientsRetryOnlyFailedRecipient(t *testing.T) { + cfg := defaultAccountOpsConfig() + cfg.Enabled = true + cfg.Recipient = "one@example.test,two@example.test,ONE@example.test" + repo := &accountOpsRepoStub{} + sender := &opsTeamSender{calls: map[string]int{}, failed: "two@example.test"} + svc := NewAccountOpsService(&accountOpsSettingsStub{}, repo, sender) + require.NoError(t, svc.SaveConfig(context.Background(), cfg)) + event := &AccountOpsEvent{AccountID: 1, GroupID: "manual-shared123", GroupName: "Shared supplier", Kind: "balance_low", Attempts: 1} + svc.deliverNotification(context.Background(), event) + require.Equal(t, "failed", repo.state) + require.Len(t, event.Deliveries, 2) + sender.failed = "" + event.Attempts = 2 + svc.deliverNotification(context.Background(), event) + require.Equal(t, "sent", repo.state) + require.Equal(t, map[string]int{"one@example.test": 1, "two@example.test": 2}, sender.calls) + require.Equal(t, "sent", event.Deliveries[opsEmailIdentity("one@example.test")].Status) +} +func TestAccountOpsRemovedEmailDoesNotReceivePendingNotification(t *testing.T) { + cfg := defaultAccountOpsConfig() + cfg.Enabled = true + cfg.Recipient = "one@example.test,two@example.test" + sender := &opsTeamSender{calls: map[string]int{}} + svc := NewAccountOpsService(&accountOpsSettingsStub{}, &accountOpsRepoStub{}, sender) + require.NoError(t, svc.SaveConfig(context.Background(), cfg)) + sender.after = func(to string) { + cfg.Recipient = "one@example.test" + require.NoError(t, svc.SaveConfig(context.Background(), cfg)) + } + svc.deliverNotification(context.Background(), &AccountOpsEvent{AccountID: 1, Kind: "balance_low"}) + require.Equal(t, map[string]int{"one@example.test": 1}, sender.calls) +} + +type opsLifecycleRepo struct { + accountOpsGroupAccounts + updated map[string]any +} + +func (r *opsLifecycleRepo) UpdateExtra(_ context.Context, _ int64, extra map[string]any) error { + r.updated = extra + return nil +} +func TestAccountOpsOAuthLifecycleDoesNotChangeSchedulingOrCredentials(t *testing.T) { + a := &Account{ID: 1, Type: AccountTypeOAuth, Status: "active", Schedulable: true, Extra: map[string]any{"import_source": "codex_session"}} + require.Equal(t, "temporary", accountOpsOAuthLifecycle(a)) + repo := &opsLifecycleRepo{accountOpsGroupAccounts: accountOpsGroupAccounts{items: map[int64]*Account{1: a}}} + svc := NewAccountOpsService(&accountOpsSettingsStub{}, &accountOpsRepoStub{}, nil) + svc.SetNotificationDependencies(repo, nil, false, "UTC") + require.NoError(t, svc.SetOAuthLifecycle(context.Background(), 1, "long_term")) + require.Equal(t, map[string]any{"oauth_lifecycle": "long_term"}, repo.updated) + require.True(t, a.Schedulable) + require.Equal(t, "active", a.Status) + require.Error(t, svc.SetOAuthLifecycle(context.Background(), 1, "unknown")) + a.Type = AccountTypeAPIKey + require.Error(t, svc.SetOAuthLifecycle(context.Background(), 1, "temporary")) +} diff --git a/backend/internal/service/account_ops_test.go b/backend/internal/service/account_ops_test.go index 97517bf81b03..631f20ec2826 100644 --- a/backend/internal/service/account_ops_test.go +++ b/backend/internal/service/account_ops_test.go @@ -127,10 +127,10 @@ func TestAccountOpsMailFailureDoesNotBecomeSent(t *testing.T) { svc.deliverNotification(context.Background(), e) require.Equal(t, time.Hour, repo.delay) } -func TestAccountOpsRejectsMultipleRecipients(t *testing.T) { +func TestAccountOpsRejectsInvalidRecipients(t *testing.T) { c := defaultAccountOpsConfig() c.Enabled = true - for _, recipient := range []string{"", "one@example.com,two@example.com", "Name ", "one@example.com\r\nBcc: other@example.com"} { + for _, recipient := range []string{"", "Name ", "one@example.com\r\nBcc: other@example.com"} { c.Recipient = recipient require.Error(t, ValidateAccountOpsConfig(c)) } diff --git a/backend/internal/service/account_quality_bps.go b/backend/internal/service/account_quality_bps.go index 746236509b8a..2736a49ea1d0 100644 --- a/backend/internal/service/account_quality_bps.go +++ b/backend/internal/service/account_quality_bps.go @@ -26,6 +26,8 @@ type QualityBPSPolicy struct { AllModels bool `json:"all_models"` Models []string `json:"models"` OmitUnsupportedTools bool `json:"omit_unsupported_tools"` + WebSearch bool `json:"web_search"` + WebSearchAuto bool `json:"web_search_auto"` IgnoreEncryptedContent bool `json:"ignore_encrypted_content"` AutoDisableOn403 bool `json:"auto_disable_on_403"` AutoRecoverOn403 bool `json:"auto_recover_on_403"` @@ -44,6 +46,8 @@ var QualityBPSManagedKeys = []string{ "openai_excel_bps_models", ExcelBPSOmitUnsupportedToolsKey, ExcelBPSIgnoreEncryptedContentKey, + ExcelBPSWebSearchKey, + ExcelBPSWebSearchAutoKey, "openai_excel_bps_auto_disable_on_403", ExcelBPSAutoRecoverOn403Key, ExcelBPS403RecoveryIntervalMinutesKey, @@ -127,6 +131,8 @@ func QualityBPSExtra(b *QualityBPSPolicy) map[string]any { interval = *b.RecoveryIntervalMinutes } extra := map[string]any{ + ExcelBPSWebSearchKey: b.WebSearch, + ExcelBPSWebSearchAutoKey: b.WebSearch && b.WebSearchAuto, ExcelBPS403RecoveryIntervalMinutesKey: interval, "openai_excel_bps": true, "openai_excel_bps_models": nil, diff --git a/backend/internal/service/basispoints/WEB_SEARCH.md b/backend/internal/service/basispoints/WEB_SEARCH.md index ab114cdeebed..a29f5f9ef85b 100644 --- a/backend/internal/service/basispoints/WEB_SEARCH.md +++ b/backend/internal/service/basispoints/WEB_SEARCH.md @@ -54,3 +54,11 @@ provide an established billable token contract here; no synthetic token charge is invented for it. Any provider-side search cost/entitlement needs separate operational verification before enabling accounts. No real paid call is part of CI or local validation. + +## Management entry points + +The account editor, bulk editor, OAuth BPS defaults card and quality-rule BPS +settings share the same search controls. Automatic search is disabled and cleared +when its parent search switch is off. Quality-rule snapshots manage both fields. +The notice ID `bps-account-search-initial-announcement` is shared across these +entry points and dismissed per administrator/browser using existing localStorage. diff --git a/backend/internal/service/bps_search_settings_test.go b/backend/internal/service/bps_search_settings_test.go new file mode 100644 index 000000000000..de07b56d2cd2 --- /dev/null +++ b/backend/internal/service/bps_search_settings_test.go @@ -0,0 +1,32 @@ +package service + +import ( + "encoding/json" + "testing" + + "github.com/stretchr/testify/require" +) + +func TestBPSSearchDefaultsAndQualityPolicyRoundTrip(t *testing.T) { + defaults := DefaultExcelBPSDefaults() + require.False(t, defaults.WebSearch) + require.False(t, defaults.WebSearchAuto) + for _, enabled := range []bool{false, true} { + defaults.WebSearch, defaults.WebSearchAuto = enabled, true + raw, err := json.Marshal(defaults) + require.NoError(t, err) + var restored ExcelBPSDefaults + require.NoError(t, json.Unmarshal(raw, &restored)) + require.Equal(t, enabled, restored.extra()[ExcelBPSWebSearchKey]) + require.Equal(t, enabled, restored.extra()[ExcelBPSWebSearchAutoKey]) + extra := QualityBPSExtra(&QualityBPSPolicy{WebSearch: enabled, WebSearchAuto: true}) + require.Equal(t, enabled, extra[ExcelBPSWebSearchKey]) + require.Equal(t, enabled, extra[ExcelBPSWebSearchAutoKey]) + } + require.Contains(t, QualityBPSManagedKeys, ExcelBPSWebSearchKey) + require.Contains(t, QualityBPSManagedKeys, ExcelBPSWebSearchAutoKey) + var legacy ExcelBPSDefaults + require.NoError(t, json.Unmarshal([]byte(`{"models":["gpt-6-astra"]}`), &legacy)) + require.False(t, legacy.WebSearch) + require.False(t, legacy.WebSearchAuto) +} diff --git a/backend/internal/service/oauth_auto_config_bps.go b/backend/internal/service/oauth_auto_config_bps.go index a1d6485062b5..b2fa8e5551ea 100644 --- a/backend/internal/service/oauth_auto_config_bps.go +++ b/backend/internal/service/oauth_auto_config_bps.go @@ -13,6 +13,8 @@ type ExcelBPSDefaults struct { AllModels bool `json:"all_models"` Models []string `json:"models"` OmitUnsupportedTools bool `json:"omit_unsupported_tools"` + WebSearch bool `json:"web_search"` + WebSearchAuto bool `json:"web_search_auto"` IgnoreEncryptedContent bool `json:"ignore_encrypted_content"` AutoDisableOn403 bool `json:"auto_disable_on_403"` AutoRecoverOn403 bool `json:"auto_recover_on_403"` @@ -63,6 +65,8 @@ func validateExcelBPSDefaults(b ExcelBPSDefaults) error { func (b ExcelBPSDefaults) extra() map[string]any { extra := map[string]any{ + ExcelBPSWebSearchKey: b.WebSearch, + ExcelBPSWebSearchAutoKey: b.WebSearch && b.WebSearchAuto, "openai_excel_bps": true, "openai_excel_bps_omit_unsupported_tools": b.OmitUnsupportedTools, "openai_excel_bps_ignore_encrypted_content": b.IgnoreEncryptedContent, diff --git a/docs/account-operations-team.md b/docs/account-operations-team.md new file mode 100644 index 000000000000..15e62a286b5a --- /dev/null +++ b/docs/account-operations-team.md @@ -0,0 +1,14 @@ +# 账号运维:供应商余额与多人通知 + +在 `/admin/account-ops` 配置账号运营分组、提醒阈值与收件人。 + +- API Key:同一供应商使用一份平台余额。新导入且尚未分组的 Key 按保存的供应商 ID 自动归组;已有手动分组和明确的未分组状态保持不变;已有账号可在分组编辑中选择供应商。没有关联供应商的组继续使用首个账号的余额。 +- 供应商余额、币种和更新时间来自渠道运维现有目录,不重复登录上游,也不把不同 Key 的额度相加。缺失、失败或过期的数据不会退回 Key 额度,也不算零余额或恢复。手动刷新样本一小时内有效;开启定时刷新时使用两倍刷新间隔判断过期。 +- 供应商的余额采集及刷新在 `/admin/channel-operations` 管理;外发通知阈值、告警/恢复通知开关在账号运维管理。阈值单位须与供应商原始币种一致,不隐式换算人民币或 USD。渠道目录原有低余额标记保留,外发通知复用账号运维的事件与投递队列。 +- 每组每次告警/恢复产生一个事件。每个收件邮箱收到一份;地址去重,成功的邮箱不会因其他邮箱失败而重发。关联供应商且已设置余额阈值时,不再因组内 Key 的余额不足响应额外产生重复告警。 +- 最多 10 个收件邮箱,可用换行、逗号或分号分隔。邮件分别发送,投递结果分别保存;Webhook 沿用现有配置。 +- OAuth 分为“速抛/临时号”和“长期号”。旧账号默认临时号,管理员可逐个修改。分类只修改 `extra.oauth_lifecycle`,不改变凭据、调度或清理策略,不因 access token 到期自动删除账号。 + +这是基于 #850 的后续功能,不创建另一份供应商目录、刷新任务或数据库迁移。供应商删除或余额查询失败后,关联组显示未知数据;必须重新关联有效供应商或切回账号采集,不能继续用旧平台余额发送新告警。 + +旧单邮箱配置直接兼容。新版本将多个地址规范化到现有 `recipient` 字符串字段;回滚到只支持单邮箱的旧版本前,应先改为单邮箱并解除供应商余额关联。OAuth 用途字段是附加元数据,旧版本会忽略它。 diff --git a/frontend/src/api/admin/accountOps.ts b/frontend/src/api/admin/accountOps.ts index e9350bbea5c0..be62ba65cdac 100644 --- a/frontend/src/api/admin/accountOps.ts +++ b/frontend/src/api/admin/accountOps.ts @@ -36,7 +36,13 @@ export interface AccountOpsQuotaThreshold { notify_recovery?: boolean } export type AccountOpsObservationStatus = 'ok' | 'stale' | 'failed' | 'unsupported' | 'unknown' +export type AccountOpsOAuthLifecycle = 'temporary' | 'long_term' +export async function setAccountOpsOAuthLifecycle(id: number, lifecycle: AccountOpsOAuthLifecycle): Promise { + await apiClient.put(`/admin/account-ops/accounts/${id}/lifecycle`, { lifecycle }) +} export interface AccountOpsThresholdAccount { + provider_id?: number + oauth_lifecycle?: AccountOpsOAuthLifecycle account_id: number account_name: string group_id?: string @@ -60,6 +66,7 @@ export interface AccountOpsThresholdAccount { }> } export interface AccountOpsGroup { + provider_id?: number id: string name: string default_name: string @@ -76,7 +83,7 @@ export interface AccountOpsConfig { weekly_quota: boolean cooldown_minutes: number webhooks?: AccountOpsWebhook[] - groups?: Array<{ id: string; name: string; provider?: string; site?: string; account_ids?: number[] }> + groups?: Array<{ id: string; name: string; provider_id?: number; provider?: string; site?: string; account_ids?: number[] }> balance_thresholds?: AccountOpsBalanceThreshold[] quota_thresholds?: AccountOpsQuotaThreshold[] } @@ -164,7 +171,7 @@ export async function getAccountOpsThresholdGroups(): Promise account_ids: Array.isArray(group.account_ids) ? [...group.account_ids] : [], })) } -export async function saveAccountOpsGroups(groups: Array<{ id: string; name: string; provider?: string; site?: string; account_ids: number[] }>): Promise { +export async function saveAccountOpsGroups(groups: Array<{ id: string; name: string; provider_id?: number; provider?: string; site?: string; account_ids: number[] }>): Promise { return (await apiClient.put('/admin/account-ops/groups', { groups })).data } export async function testAccountOpsWebhook(id: string): Promise<{ ok: boolean }> { diff --git a/frontend/src/components/account/BulkEditAccountModal.vue b/frontend/src/components/account/BulkEditAccountModal.vue index 9a11746ebd7a..12e9996ffcf7 100644 --- a/frontend/src/components/account/BulkEditAccountModal.vue +++ b/frontend/src/components/account/BulkEditAccountModal.vue @@ -66,6 +66,7 @@

{{ t('admin.accounts.openai.excelBPSModelsHint') }}

{{ t('admin.accounts.openai.excelBPSNotice') }}

+

{{ t('admin.accounts.openai.excelBPSNotice') }}

+
@@ -32,7 +34,7 @@ const busy = ref(null), feedback = ref('') let alive = true onBeforeUnmount(() => { alive = false }) async function refresh(account: AccountOpsThresholdAccount) { - if (props.disabled || busy.value !== null) return + if (props.disabled || busy.value !== null || account.provider_id) return busy.value = account.account_id feedback.value = '' try { diff --git a/frontend/src/components/admin/operations/AccountOpsEmailDialog.vue b/frontend/src/components/admin/operations/AccountOpsEmailDialog.vue index 1c5172a30c9d..96670d6c9f09 100644 --- a/frontend/src/components/admin/operations/AccountOpsEmailDialog.vue +++ b/frontend/src/components/admin/operations/AccountOpsEmailDialog.vue @@ -8,8 +8,8 @@ {{ t('accountOps.channelNameHint') }}