- 使用 SQLite 保存关联后的请求生命周期和用量记录 - 支持长上下文阶梯价格和可配置的 Fast 计费倍率 - 添加管理接口和可配置列的用量面板 - 保留失败、取消、重试和 compact 请求,便于计费核对
173 lines
4.3 KiB
Go
173 lines
4.3 KiB
Go
package plugin
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/url"
|
|
"time"
|
|
)
|
|
|
|
const (
|
|
ABIVersion uint32 = 1
|
|
SchemaVersion uint32 = 3
|
|
PluginName = "cpa-ext"
|
|
Version = "0.1.0-dev"
|
|
)
|
|
|
|
const (
|
|
MethodPluginRegister = "plugin.register"
|
|
MethodPluginReconfigure = "plugin.reconfigure"
|
|
MethodPluginShutdown = "plugin.shutdown"
|
|
MethodRequestComplete = "request.complete"
|
|
MethodUsageHandle = "usage.handle"
|
|
MethodManagementRegister = "management.register"
|
|
MethodManagementHandle = "management.handle"
|
|
)
|
|
|
|
type Envelope struct {
|
|
OK bool `json:"ok"`
|
|
Result json.RawMessage `json:"result,omitempty"`
|
|
Error *EnvelopeError `json:"error,omitempty"`
|
|
}
|
|
|
|
type EnvelopeError struct {
|
|
Code string `json:"code"`
|
|
Message string `json:"message"`
|
|
Retryable bool `json:"retryable,omitempty"`
|
|
HTTPStatus int `json:"http_status,omitempty"`
|
|
}
|
|
|
|
type LifecycleRequest struct {
|
|
ConfigYAML []byte `json:"config_yaml"`
|
|
SchemaVersion uint32 `json:"schema_version"`
|
|
}
|
|
|
|
type Registration struct {
|
|
SchemaVersion uint32 `json:"schema_version"`
|
|
Metadata Metadata `json:"metadata"`
|
|
Capabilities Capabilities `json:"capabilities"`
|
|
}
|
|
|
|
type Metadata struct {
|
|
Name string
|
|
Version string
|
|
Author string
|
|
GitHubRepository string
|
|
Logo string
|
|
ConfigFields []ConfigField
|
|
}
|
|
|
|
type ConfigField struct {
|
|
Name string
|
|
Type string
|
|
EnumValues []string
|
|
Description string
|
|
}
|
|
|
|
type Capabilities struct {
|
|
RequestLifecyclePlugin bool `json:"request_lifecycle_plugin"`
|
|
UsagePlugin bool `json:"usage_plugin"`
|
|
ManagementAPI bool `json:"management_api"`
|
|
}
|
|
|
|
type RequestCompletionOutcome string
|
|
|
|
const (
|
|
RequestCompletionSucceeded RequestCompletionOutcome = "succeeded"
|
|
RequestCompletionFailed RequestCompletionOutcome = "failed"
|
|
RequestCompletionRejected RequestCompletionOutcome = "rejected"
|
|
RequestCompletionCanceled RequestCompletionOutcome = "canceled"
|
|
)
|
|
|
|
// RequestCompletion 是一次模型执行的终态,Token 由 UsageRecord 单独补充。
|
|
type RequestCompletion struct {
|
|
RequestID string
|
|
TraceID string
|
|
SourceFormat string
|
|
Model string
|
|
RequestedModel string
|
|
Stream bool
|
|
Outcome RequestCompletionOutcome
|
|
StatusCode int
|
|
Error string
|
|
StartedAt time.Time
|
|
CompletedAt time.Time
|
|
Metadata map[string]any
|
|
}
|
|
|
|
type ManagementRegistrationResponse struct {
|
|
Routes []ManagementRoute `json:"routes,omitempty"`
|
|
Resources []ResourceRoute `json:"resources,omitempty"`
|
|
}
|
|
|
|
type ManagementRoute struct {
|
|
Method string `json:"Method"`
|
|
Path string `json:"Path"`
|
|
Description string `json:"Description,omitempty"`
|
|
}
|
|
|
|
type ResourceRoute struct {
|
|
Path string `json:"Path"`
|
|
Menu string `json:"Menu,omitempty"`
|
|
Description string `json:"Description,omitempty"`
|
|
}
|
|
|
|
type ManagementRequest struct {
|
|
Method string `json:"Method"`
|
|
Path string `json:"Path"`
|
|
Headers http.Header `json:"Headers"`
|
|
Query url.Values `json:"Query"`
|
|
Body []byte `json:"Body"`
|
|
}
|
|
|
|
type ManagementResponse struct {
|
|
StatusCode int `json:"StatusCode,omitempty"`
|
|
Headers http.Header `json:"Headers,omitempty"`
|
|
Body []byte `json:"Body,omitempty"`
|
|
}
|
|
|
|
// UsageRecord mirrors CLIProxyAPI sdk/pluginapi. Keep it in sync with the
|
|
// target host because exported field names are part of the JSON wire contract.
|
|
type UsageRecord struct {
|
|
Provider string
|
|
RequestID string
|
|
ExecutionID string
|
|
TraceID string
|
|
ExecutorType string
|
|
Endpoint string
|
|
ClientIP string
|
|
Model string
|
|
Alias string
|
|
APIKey string
|
|
AuthID string
|
|
AuthIndex string
|
|
AuthType string
|
|
Source string
|
|
ReasoningEffort string
|
|
ServiceTier string
|
|
Speed string
|
|
Generate bool
|
|
RequestedAt time.Time
|
|
Latency time.Duration
|
|
TTFT time.Duration
|
|
Failed bool
|
|
Failure UsageFailure
|
|
Detail UsageDetail
|
|
ResponseHeaders http.Header
|
|
}
|
|
|
|
type UsageFailure struct {
|
|
StatusCode int
|
|
Body string
|
|
}
|
|
|
|
type UsageDetail struct {
|
|
InputTokens int64
|
|
OutputTokens int64
|
|
ReasoningTokens int64
|
|
CachedTokens int64
|
|
CacheReadTokens int64
|
|
CacheCreationTokens int64
|
|
TotalTokens int64
|
|
}
|