2026-04-04 11:00:55 +08:00
|
|
|
/**
|
|
|
|
|
* Admin Channels API endpoints
|
|
|
|
|
* Handles channel management for administrators
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
import { apiClient } from '../client'
|
2026-04-21 01:05:14 +08:00
|
|
|
import type { BillingMode, ChannelStatus, BillingModelSource } from '@/constants/channel'
|
2026-04-04 11:00:55 +08:00
|
|
|
|
2026-04-21 01:05:14 +08:00
|
|
|
export type { BillingMode } from '@/constants/channel'
|
2026-04-04 11:00:55 +08:00
|
|
|
|
|
|
|
|
export interface PricingInterval {
|
|
|
|
|
id?: number
|
|
|
|
|
min_tokens: number
|
|
|
|
|
max_tokens: number | null
|
|
|
|
|
tier_label: string
|
|
|
|
|
input_price: number | null
|
|
|
|
|
output_price: number | null
|
|
|
|
|
cache_write_price: number | null
|
|
|
|
|
cache_read_price: number | null
|
|
|
|
|
per_request_price: number | null
|
|
|
|
|
sort_order: number
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export interface ChannelModelPricing {
|
|
|
|
|
id?: number
|
2026-03-30 15:04:30 +08:00
|
|
|
platform: string
|
2026-04-04 11:00:55 +08:00
|
|
|
models: string[]
|
|
|
|
|
billing_mode: BillingMode
|
|
|
|
|
input_price: number | null
|
|
|
|
|
output_price: number | null
|
|
|
|
|
cache_write_price: number | null
|
|
|
|
|
cache_read_price: number | null
|
|
|
|
|
image_output_price: number | null
|
2026-03-30 13:26:05 +08:00
|
|
|
per_request_price: number | null
|
2026-04-04 11:00:55 +08:00
|
|
|
intervals: PricingInterval[]
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-11 23:39:49 +08:00
|
|
|
export interface AccountStatsPricingRule {
|
|
|
|
|
id?: number
|
|
|
|
|
name: string
|
|
|
|
|
group_ids: number[]
|
|
|
|
|
account_ids: number[]
|
|
|
|
|
pricing: ChannelModelPricing[]
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-04 11:00:55 +08:00
|
|
|
export interface Channel {
|
|
|
|
|
id: number
|
|
|
|
|
name: string
|
|
|
|
|
description: string
|
2026-04-21 01:05:14 +08:00
|
|
|
status: ChannelStatus
|
|
|
|
|
billing_model_source: BillingModelSource
|
2026-03-30 13:26:05 +08:00
|
|
|
restrict_models: boolean
|
2026-04-14 17:35:27 +08:00
|
|
|
features_config?: Record<string, unknown>
|
2026-04-04 11:00:55 +08:00
|
|
|
group_ids: number[]
|
|
|
|
|
model_pricing: ChannelModelPricing[]
|
2026-03-30 15:04:30 +08:00
|
|
|
model_mapping: Record<string, Record<string, string>> // platform → {src→dst}
|
2026-04-11 23:39:49 +08:00
|
|
|
apply_pricing_to_account_stats: boolean
|
|
|
|
|
account_stats_pricing_rules: AccountStatsPricingRule[]
|
2026-04-04 11:00:55 +08:00
|
|
|
created_at: string
|
|
|
|
|
updated_at: string
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export interface CreateChannelRequest {
|
|
|
|
|
name: string
|
|
|
|
|
description?: string
|
|
|
|
|
group_ids?: number[]
|
|
|
|
|
model_pricing?: ChannelModelPricing[]
|
2026-03-30 15:04:30 +08:00
|
|
|
model_mapping?: Record<string, Record<string, string>>
|
2026-03-30 13:26:05 +08:00
|
|
|
billing_model_source?: string
|
|
|
|
|
restrict_models?: boolean
|
2026-04-14 17:35:27 +08:00
|
|
|
features_config?: Record<string, unknown>
|
2026-04-11 23:39:49 +08:00
|
|
|
apply_pricing_to_account_stats?: boolean
|
|
|
|
|
account_stats_pricing_rules?: AccountStatsPricingRule[]
|
2026-04-04 11:00:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export interface UpdateChannelRequest {
|
|
|
|
|
name?: string
|
|
|
|
|
description?: string
|
|
|
|
|
status?: string
|
|
|
|
|
group_ids?: number[]
|
|
|
|
|
model_pricing?: ChannelModelPricing[]
|
2026-03-30 15:04:30 +08:00
|
|
|
model_mapping?: Record<string, Record<string, string>>
|
2026-03-30 13:26:05 +08:00
|
|
|
billing_model_source?: string
|
|
|
|
|
restrict_models?: boolean
|
2026-04-14 17:35:27 +08:00
|
|
|
features_config?: Record<string, unknown>
|
2026-04-11 23:39:49 +08:00
|
|
|
apply_pricing_to_account_stats?: boolean
|
|
|
|
|
account_stats_pricing_rules?: AccountStatsPricingRule[]
|
2026-04-04 11:00:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
interface PaginatedResponse<T> {
|
|
|
|
|
items: T[]
|
|
|
|
|
total: number
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* List channels with pagination
|
|
|
|
|
*/
|
|
|
|
|
export async function list(
|
|
|
|
|
page: number = 1,
|
|
|
|
|
pageSize: number = 20,
|
|
|
|
|
filters?: {
|
|
|
|
|
status?: string
|
|
|
|
|
search?: string
|
2026-04-09 18:14:28 +08:00
|
|
|
sort_by?: string
|
|
|
|
|
sort_order?: 'asc' | 'desc'
|
2026-04-04 11:00:55 +08:00
|
|
|
},
|
|
|
|
|
options?: { signal?: AbortSignal }
|
|
|
|
|
): Promise<PaginatedResponse<Channel>> {
|
|
|
|
|
const { data } = await apiClient.get<PaginatedResponse<Channel>>('/admin/channels', {
|
|
|
|
|
params: {
|
|
|
|
|
page,
|
|
|
|
|
page_size: pageSize,
|
|
|
|
|
...filters
|
|
|
|
|
},
|
|
|
|
|
signal: options?.signal
|
|
|
|
|
})
|
|
|
|
|
return data
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get channel by ID
|
|
|
|
|
*/
|
|
|
|
|
export async function getById(id: number): Promise<Channel> {
|
|
|
|
|
const { data } = await apiClient.get<Channel>(`/admin/channels/${id}`)
|
|
|
|
|
return data
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Create a new channel
|
|
|
|
|
*/
|
|
|
|
|
export async function create(req: CreateChannelRequest): Promise<Channel> {
|
|
|
|
|
const { data } = await apiClient.post<Channel>('/admin/channels', req)
|
|
|
|
|
return data
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Update a channel
|
|
|
|
|
*/
|
|
|
|
|
export async function update(id: number, req: UpdateChannelRequest): Promise<Channel> {
|
|
|
|
|
const { data } = await apiClient.put<Channel>(`/admin/channels/${id}`, req)
|
|
|
|
|
return data
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Delete a channel
|
|
|
|
|
*/
|
|
|
|
|
export async function remove(id: number): Promise<void> {
|
|
|
|
|
await apiClient.delete(`/admin/channels/${id}`)
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-30 16:11:49 +08:00
|
|
|
export interface ModelDefaultPricing {
|
|
|
|
|
found: boolean
|
|
|
|
|
input_price?: number // per-token price
|
|
|
|
|
output_price?: number
|
|
|
|
|
cache_write_price?: number
|
|
|
|
|
cache_read_price?: number
|
2026-04-01 15:08:57 +08:00
|
|
|
image_output_price?: number
|
2026-03-30 16:11:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function getModelDefaultPricing(model: string): Promise<ModelDefaultPricing> {
|
|
|
|
|
const { data } = await apiClient.get<ModelDefaultPricing>('/admin/channels/model-pricing', {
|
|
|
|
|
params: { model }
|
|
|
|
|
})
|
|
|
|
|
return data
|
|
|
|
|
}
|
|
|
|
|
|
feat(channels): add "Available Channels" aggregate view
Add a read-only aggregate view per channel: its linked groups and a
deterministic wildcard-free supported-model list with pricing details.
Backend
- service.Channel.SupportedModels(): combine ModelMapping keys with
same-platform ModelPricing.Models; trailing "*" keys expand via
pricing prefix match; platforms without a mapping produce no
entries (intentional "no mapping = not shown" rule).
- Extract splitWildcardSuffix() shared with toModelEntry.
- Build a per-call pricing lookup map (platform+lowerName -> *pricing)
to avoid O(N*M) scans in SupportedModels.
- ChannelService.ListAvailable() aggregates channels + active groups;
filters out group IDs no longer active.
- Admin route GET /api/v1/admin/channels/available returns the full
DTO (id, status, billing_model_source, restrict_models, groups,
supported_models).
- User route GET /api/v1/channels/available applies three filters:
Status==active, visible-group intersection, and platform filter
on supported_models (prevents cross-platform leak when a channel
links to both a user-accessible group and an inaccessible one on
another platform). Response is a plain array (matches the
/groups/available sibling shape). Field whitelist omits
billing_model_source, restrict_models, ids, status, sort_order.
Frontend
- New /admin/available-channels and /available-channels views backed
by a shared AvailableChannelsTable component (admin adds status +
billing-source columns via slots).
- PricingRow extracted to its own SFC; SupportedModelChip references
shared billing-mode constants in constants/channel.ts.
- Sidebar: new entry above "渠道管理" for admin; matching entry in
user nav.
- i18n: zh + en coverage for both namespaces.
Tests
- SupportedModels: wildcard-only pricing skipped, prefix-matches-
nothing, cross-platform bleed, case-insensitive dedup, empty
platform mapping.
- ListAvailable: nil groupRepo, inactive-group-ID dropped, stable
case-insensitive name sort.
- User handler: 401 on unauthenticated, visible-group intersection,
platform filter on supported_models, JSON whitelist.
- Admin handler: full DTO including default BillingModelSource
fallback.
Refs: issue #1729
2026-04-21 00:27:10 +08:00
|
|
|
// --- Available channels (聚合视图:渠道 + 分组 + 支持模型) ---
|
|
|
|
|
|
|
|
|
|
export interface AvailableGroupRef {
|
|
|
|
|
id: number
|
|
|
|
|
name: string
|
|
|
|
|
platform: string
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export interface SupportedModel {
|
|
|
|
|
name: string
|
|
|
|
|
platform: string
|
|
|
|
|
pricing: ChannelModelPricing | null
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export interface AvailableChannel {
|
|
|
|
|
id: number
|
|
|
|
|
name: string
|
|
|
|
|
description: string
|
2026-04-21 01:05:14 +08:00
|
|
|
status: ChannelStatus
|
|
|
|
|
billing_model_source: BillingModelSource
|
feat(channels): add "Available Channels" aggregate view
Add a read-only aggregate view per channel: its linked groups and a
deterministic wildcard-free supported-model list with pricing details.
Backend
- service.Channel.SupportedModels(): combine ModelMapping keys with
same-platform ModelPricing.Models; trailing "*" keys expand via
pricing prefix match; platforms without a mapping produce no
entries (intentional "no mapping = not shown" rule).
- Extract splitWildcardSuffix() shared with toModelEntry.
- Build a per-call pricing lookup map (platform+lowerName -> *pricing)
to avoid O(N*M) scans in SupportedModels.
- ChannelService.ListAvailable() aggregates channels + active groups;
filters out group IDs no longer active.
- Admin route GET /api/v1/admin/channels/available returns the full
DTO (id, status, billing_model_source, restrict_models, groups,
supported_models).
- User route GET /api/v1/channels/available applies three filters:
Status==active, visible-group intersection, and platform filter
on supported_models (prevents cross-platform leak when a channel
links to both a user-accessible group and an inaccessible one on
another platform). Response is a plain array (matches the
/groups/available sibling shape). Field whitelist omits
billing_model_source, restrict_models, ids, status, sort_order.
Frontend
- New /admin/available-channels and /available-channels views backed
by a shared AvailableChannelsTable component (admin adds status +
billing-source columns via slots).
- PricingRow extracted to its own SFC; SupportedModelChip references
shared billing-mode constants in constants/channel.ts.
- Sidebar: new entry above "渠道管理" for admin; matching entry in
user nav.
- i18n: zh + en coverage for both namespaces.
Tests
- SupportedModels: wildcard-only pricing skipped, prefix-matches-
nothing, cross-platform bleed, case-insensitive dedup, empty
platform mapping.
- ListAvailable: nil groupRepo, inactive-group-ID dropped, stable
case-insensitive name sort.
- User handler: 401 on unauthenticated, visible-group intersection,
platform filter on supported_models, JSON whitelist.
- Admin handler: full DTO including default BillingModelSource
fallback.
Refs: issue #1729
2026-04-21 00:27:10 +08:00
|
|
|
restrict_models: boolean
|
|
|
|
|
groups: AvailableGroupRef[]
|
|
|
|
|
supported_models: SupportedModel[]
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
interface AvailableChannelsResponse {
|
|
|
|
|
items: AvailableChannel[]
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** 列出所有可用渠道(含关联分组与支持模型) */
|
|
|
|
|
export async function listAvailable(options?: { signal?: AbortSignal }): Promise<AvailableChannel[]> {
|
|
|
|
|
const { data } = await apiClient.get<AvailableChannelsResponse>('/admin/channels/available', {
|
|
|
|
|
signal: options?.signal
|
|
|
|
|
})
|
|
|
|
|
return data.items
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const channelsAPI = { list, getById, create, update, remove, getModelDefaultPricing, listAvailable }
|
2026-04-04 11:00:55 +08:00
|
|
|
export default channelsAPI
|