2026-04-04 11:00:55 +08:00
|
|
|
/**
|
|
|
|
|
* Admin Channels API endpoints
|
|
|
|
|
* Handles channel management for administrators
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
import { apiClient } from '../client'
|
|
|
|
|
|
|
|
|
|
export type BillingMode = 'token' | 'per_request' | 'image'
|
|
|
|
|
|
|
|
|
|
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[]
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export interface Channel {
|
|
|
|
|
id: number
|
|
|
|
|
name: string
|
|
|
|
|
description: string
|
|
|
|
|
status: string
|
2026-03-30 13:26:05 +08:00
|
|
|
billing_model_source: string // "requested" | "upstream"
|
|
|
|
|
restrict_models: boolean
|
feat(gateway): add web search emulation for Anthropic API Key accounts
Inject web search capability for Claude Console (API Key) accounts that
don't natively support Anthropic's web_search tool. When a pure
web_search request is detected, the gateway calls Brave Search or Tavily
API directly and constructs an Anthropic-protocol-compliant SSE/JSON
response without forwarding to upstream.
Backend:
- New `pkg/websearch/` SDK: Brave and Tavily provider implementations
with io.LimitReader, proxy support, and Redis-based quota tracking
(Lua atomic INCR + TTL, DECR rollback on failure)
- Global config via `settings.web_search_emulation_config` (JSON) with
in-process cache + singleflight, input validation, API key merge on
save, and sanitized API responses
- Channel-level toggle via `channels.features_config` JSONB column
(DB migration 101)
- Account-level toggle via `accounts.extra.web_search_emulation`
- Request interception in `Forward()` with SSE streaming response
construction using json.Marshal (no manual string concatenation)
- Manager hot-reload: `RebuildWebSearchManager()` called on config save
and startup via `SetWebSearchRedisClient()`
- 70 unit tests covering providers, manager, config validation,
sanitization, tool detection, query extraction, and response building
Frontend:
- Settings → Gateway tab: Web Search Emulation config card with global
toggle, provider list (add/remove, API key, priority, quota, proxy)
- Channels → Anthropic tab: web search emulation toggle with global
state linkage (disabled when global off)
- Account Create/Edit modals: web search emulation toggle for API Key
type with Toggle component
- Full i18n coverage (zh + en)
2026-04-12 00:02:26 +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-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
|
feat(gateway): add web search emulation for Anthropic API Key accounts
Inject web search capability for Claude Console (API Key) accounts that
don't natively support Anthropic's web_search tool. When a pure
web_search request is detected, the gateway calls Brave Search or Tavily
API directly and constructs an Anthropic-protocol-compliant SSE/JSON
response without forwarding to upstream.
Backend:
- New `pkg/websearch/` SDK: Brave and Tavily provider implementations
with io.LimitReader, proxy support, and Redis-based quota tracking
(Lua atomic INCR + TTL, DECR rollback on failure)
- Global config via `settings.web_search_emulation_config` (JSON) with
in-process cache + singleflight, input validation, API key merge on
save, and sanitized API responses
- Channel-level toggle via `channels.features_config` JSONB column
(DB migration 101)
- Account-level toggle via `accounts.extra.web_search_emulation`
- Request interception in `Forward()` with SSE streaming response
construction using json.Marshal (no manual string concatenation)
- Manager hot-reload: `RebuildWebSearchManager()` called on config save
and startup via `SetWebSearchRedisClient()`
- 70 unit tests covering providers, manager, config validation,
sanitization, tool detection, query extraction, and response building
Frontend:
- Settings → Gateway tab: Web Search Emulation config card with global
toggle, provider list (add/remove, API key, priority, quota, proxy)
- Channels → Anthropic tab: web search emulation toggle with global
state linkage (disabled when global off)
- Account Create/Edit modals: web search emulation toggle for API Key
type with Toggle component
- Full i18n coverage (zh + en)
2026-04-12 00:02:26 +08:00
|
|
|
features_config?: Record<string, unknown>
|
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
|
feat(gateway): add web search emulation for Anthropic API Key accounts
Inject web search capability for Claude Console (API Key) accounts that
don't natively support Anthropic's web_search tool. When a pure
web_search request is detected, the gateway calls Brave Search or Tavily
API directly and constructs an Anthropic-protocol-compliant SSE/JSON
response without forwarding to upstream.
Backend:
- New `pkg/websearch/` SDK: Brave and Tavily provider implementations
with io.LimitReader, proxy support, and Redis-based quota tracking
(Lua atomic INCR + TTL, DECR rollback on failure)
- Global config via `settings.web_search_emulation_config` (JSON) with
in-process cache + singleflight, input validation, API key merge on
save, and sanitized API responses
- Channel-level toggle via `channels.features_config` JSONB column
(DB migration 101)
- Account-level toggle via `accounts.extra.web_search_emulation`
- Request interception in `Forward()` with SSE streaming response
construction using json.Marshal (no manual string concatenation)
- Manager hot-reload: `RebuildWebSearchManager()` called on config save
and startup via `SetWebSearchRedisClient()`
- 70 unit tests covering providers, manager, config validation,
sanitization, tool detection, query extraction, and response building
Frontend:
- Settings → Gateway tab: Web Search Emulation config card with global
toggle, provider list (add/remove, API key, priority, quota, proxy)
- Channels → Anthropic tab: web search emulation toggle with global
state linkage (disabled when global off)
- Account Create/Edit modals: web search emulation toggle for API Key
type with Toggle component
- Full i18n coverage (zh + en)
2026-04-12 00:02:26 +08:00
|
|
|
features_config?: Record<string, unknown>
|
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
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const channelsAPI = { list, getById, create, update, remove, getModelDefaultPricing }
|
2026-04-04 11:00:55 +08:00
|
|
|
export default channelsAPI
|