claude.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600
  1. package dto
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "strings"
  6. "github.com/QuantumNous/new-api/common"
  7. "github.com/QuantumNous/new-api/types"
  8. "github.com/gin-gonic/gin"
  9. )
  10. type ClaudeMetadata struct {
  11. UserId string `json:"user_id"`
  12. }
  13. type ClaudeMediaMessage struct {
  14. Type string `json:"type,omitempty"`
  15. Text *string `json:"text,omitempty"`
  16. Model string `json:"model,omitempty"`
  17. Source *ClaudeMessageSource `json:"source,omitempty"`
  18. Usage *ClaudeUsage `json:"usage,omitempty"`
  19. StopReason *string `json:"stop_reason,omitempty"`
  20. PartialJson *string `json:"partial_json,omitempty"`
  21. Role string `json:"role,omitempty"`
  22. Thinking *string `json:"thinking,omitempty"`
  23. Signature string `json:"signature,omitempty"`
  24. Delta string `json:"delta,omitempty"`
  25. CacheControl json.RawMessage `json:"cache_control,omitempty"`
  26. // tool_calls
  27. Id string `json:"id,omitempty"`
  28. Name string `json:"name,omitempty"`
  29. Input any `json:"input,omitempty"`
  30. Content any `json:"content,omitempty"`
  31. ToolUseId string `json:"tool_use_id,omitempty"`
  32. }
  33. func (c *ClaudeMediaMessage) SetText(s string) {
  34. c.Text = &s
  35. }
  36. func (c *ClaudeMediaMessage) GetText() string {
  37. if c.Text == nil {
  38. return ""
  39. }
  40. return *c.Text
  41. }
  42. func (c *ClaudeMediaMessage) IsStringContent() bool {
  43. if c.Content == nil {
  44. return false
  45. }
  46. _, ok := c.Content.(string)
  47. if ok {
  48. return true
  49. }
  50. return false
  51. }
  52. func (c *ClaudeMediaMessage) GetStringContent() string {
  53. if c.Content == nil {
  54. return ""
  55. }
  56. switch c.Content.(type) {
  57. case string:
  58. return c.Content.(string)
  59. case []any:
  60. var contentStr string
  61. for _, contentItem := range c.Content.([]any) {
  62. contentMap, ok := contentItem.(map[string]any)
  63. if !ok {
  64. continue
  65. }
  66. if contentMap["type"] == ContentTypeText {
  67. if subStr, ok := contentMap["text"].(string); ok {
  68. contentStr += subStr
  69. }
  70. }
  71. }
  72. return contentStr
  73. }
  74. return ""
  75. }
  76. func (c *ClaudeMediaMessage) GetJsonRowString() string {
  77. jsonContent, _ := common.Marshal(c)
  78. return string(jsonContent)
  79. }
  80. func (c *ClaudeMediaMessage) SetContent(content any) {
  81. c.Content = content
  82. }
  83. func (c *ClaudeMediaMessage) ParseMediaContent() []ClaudeMediaMessage {
  84. mediaContent, _ := common.Any2Type[[]ClaudeMediaMessage](c.Content)
  85. return mediaContent
  86. }
  87. func (m *ClaudeMediaMessage) ToFileSource() types.FileSource {
  88. if m.Source == nil {
  89. return nil
  90. }
  91. data := m.Source.Url
  92. if data == "" {
  93. data = common.Interface2String(m.Source.Data)
  94. }
  95. if data == "" {
  96. return nil
  97. }
  98. return types.NewFileSourceFromData(data, m.Source.MediaType)
  99. }
  100. type ClaudeMessageSource struct {
  101. Type string `json:"type"`
  102. MediaType string `json:"media_type,omitempty"`
  103. Data any `json:"data,omitempty"`
  104. Url string `json:"url,omitempty"`
  105. }
  106. type ClaudeMessage struct {
  107. Role string `json:"role"`
  108. Content any `json:"content"`
  109. }
  110. func (c *ClaudeMessage) IsStringContent() bool {
  111. if c.Content == nil {
  112. return false
  113. }
  114. _, ok := c.Content.(string)
  115. return ok
  116. }
  117. func (c *ClaudeMessage) GetStringContent() string {
  118. if c.Content == nil {
  119. return ""
  120. }
  121. switch c.Content.(type) {
  122. case string:
  123. return c.Content.(string)
  124. case []any:
  125. var contentStr string
  126. for _, contentItem := range c.Content.([]any) {
  127. contentMap, ok := contentItem.(map[string]any)
  128. if !ok {
  129. continue
  130. }
  131. if contentMap["type"] == ContentTypeText {
  132. if subStr, ok := contentMap["text"].(string); ok {
  133. contentStr += subStr
  134. }
  135. }
  136. }
  137. return contentStr
  138. }
  139. return ""
  140. }
  141. func (c *ClaudeMessage) SetStringContent(content string) {
  142. c.Content = content
  143. }
  144. func (c *ClaudeMessage) SetContent(content any) {
  145. c.Content = content
  146. }
  147. func (c *ClaudeMessage) ParseContent() ([]ClaudeMediaMessage, error) {
  148. return common.Any2Type[[]ClaudeMediaMessage](c.Content)
  149. }
  150. type Tool struct {
  151. Name string `json:"name"`
  152. Description string `json:"description,omitempty"`
  153. InputSchema map[string]interface{} `json:"input_schema"`
  154. }
  155. type InputSchema struct {
  156. Type string `json:"type"`
  157. Properties any `json:"properties,omitempty"`
  158. Required any `json:"required,omitempty"`
  159. }
  160. type ClaudeWebSearchTool struct {
  161. Type string `json:"type"`
  162. Name string `json:"name"`
  163. MaxUses int `json:"max_uses,omitempty"`
  164. UserLocation *ClaudeWebSearchUserLocation `json:"user_location,omitempty"`
  165. }
  166. type ClaudeWebSearchUserLocation struct {
  167. Type string `json:"type"`
  168. Timezone string `json:"timezone,omitempty"`
  169. Country string `json:"country,omitempty"`
  170. Region string `json:"region,omitempty"`
  171. City string `json:"city,omitempty"`
  172. }
  173. type ClaudeToolChoice struct {
  174. Type string `json:"type"`
  175. Name string `json:"name,omitempty"`
  176. DisableParallelToolUse bool `json:"disable_parallel_tool_use,omitempty"`
  177. }
  178. type ClaudeRequest struct {
  179. Model string `json:"model"`
  180. Prompt string `json:"prompt,omitempty"`
  181. System any `json:"system,omitempty"`
  182. Messages []ClaudeMessage `json:"messages,omitempty"`
  183. CacheControl json.RawMessage `json:"cache_control,omitempty"`
  184. // InferenceGeo controls Claude data residency region.
  185. // This field is filtered by default and can be enabled via channel setting allow_inference_geo.
  186. InferenceGeo string `json:"inference_geo,omitempty"`
  187. MaxTokens *uint `json:"max_tokens,omitempty"`
  188. MaxTokensToSample *uint `json:"max_tokens_to_sample,omitempty"`
  189. StopSequences []string `json:"stop_sequences,omitempty"`
  190. Temperature *float64 `json:"temperature,omitempty"`
  191. TopP *float64 `json:"top_p,omitempty"`
  192. TopK *int `json:"top_k,omitempty"`
  193. Stream *bool `json:"stream,omitempty"`
  194. Tools any `json:"tools,omitempty"`
  195. ContextManagement json.RawMessage `json:"context_management,omitempty"`
  196. OutputConfig json.RawMessage `json:"output_config,omitempty"`
  197. OutputFormat json.RawMessage `json:"output_format,omitempty"`
  198. Container json.RawMessage `json:"container,omitempty"`
  199. ToolChoice any `json:"tool_choice,omitempty"`
  200. Thinking *Thinking `json:"thinking,omitempty"`
  201. McpServers json.RawMessage `json:"mcp_servers,omitempty"`
  202. Metadata json.RawMessage `json:"metadata,omitempty"`
  203. // Speed specifies the Claude inference speed mode.
  204. // This field is filtered by default and can be enabled via channel setting allow_speed.
  205. Speed json.RawMessage `json:"speed,omitempty"`
  206. // ServiceTier specifies upstream service level and may affect billing.
  207. // This field is filtered by default and can be enabled via channel setting allow_service_tier.
  208. ServiceTier string `json:"service_tier,omitempty"`
  209. }
  210. // OutputConfigForEffort just for extract effort
  211. type OutputConfigForEffort struct {
  212. Effort string `json:"effort,omitempty"`
  213. }
  214. func (c *ClaudeRequest) GetTokenCountMeta() *types.TokenCountMeta {
  215. maxTokens := 0
  216. if c.MaxTokens != nil {
  217. maxTokens = int(*c.MaxTokens)
  218. }
  219. var tokenCountMeta = types.TokenCountMeta{
  220. TokenType: types.TokenTypeTokenizer,
  221. MaxTokens: maxTokens,
  222. }
  223. var texts = make([]string, 0)
  224. var fileMeta = make([]*types.FileMeta, 0)
  225. // system
  226. if c.System != nil {
  227. if c.IsStringSystem() {
  228. sys := c.GetStringSystem()
  229. if sys != "" {
  230. texts = append(texts, sys)
  231. }
  232. } else {
  233. systemMedia := c.ParseSystem()
  234. for _, media := range systemMedia {
  235. switch media.Type {
  236. case "text":
  237. texts = append(texts, media.GetText())
  238. case "image":
  239. if source := media.ToFileSource(); source != nil {
  240. fileMeta = append(fileMeta, &types.FileMeta{
  241. FileType: types.FileTypeImage,
  242. Source: source,
  243. })
  244. }
  245. }
  246. }
  247. }
  248. }
  249. // messages
  250. for _, message := range c.Messages {
  251. tokenCountMeta.MessagesCount++
  252. texts = append(texts, message.Role)
  253. if message.IsStringContent() {
  254. content := message.GetStringContent()
  255. if content != "" {
  256. texts = append(texts, content)
  257. }
  258. continue
  259. }
  260. content, _ := message.ParseContent()
  261. for _, media := range content {
  262. switch media.Type {
  263. case "text":
  264. texts = append(texts, media.GetText())
  265. case "image":
  266. if source := media.ToFileSource(); source != nil {
  267. fileMeta = append(fileMeta, &types.FileMeta{
  268. FileType: types.FileTypeImage,
  269. Source: source,
  270. })
  271. }
  272. case "tool_use":
  273. if media.Name != "" {
  274. texts = append(texts, media.Name)
  275. }
  276. if media.Input != nil {
  277. b, _ := common.Marshal(media.Input)
  278. texts = append(texts, string(b))
  279. }
  280. case "tool_result":
  281. if media.Content != nil {
  282. b, _ := common.Marshal(media.Content)
  283. texts = append(texts, string(b))
  284. }
  285. }
  286. }
  287. }
  288. // tools
  289. if c.Tools != nil {
  290. tools := c.GetTools()
  291. normalTools, webSearchTools := ProcessTools(tools)
  292. if normalTools != nil {
  293. for _, t := range normalTools {
  294. tokenCountMeta.ToolsCount++
  295. if t.Name != "" {
  296. texts = append(texts, t.Name)
  297. }
  298. if t.Description != "" {
  299. texts = append(texts, t.Description)
  300. }
  301. if t.InputSchema != nil {
  302. b, _ := common.Marshal(t.InputSchema)
  303. texts = append(texts, string(b))
  304. }
  305. }
  306. }
  307. if webSearchTools != nil {
  308. for _, t := range webSearchTools {
  309. tokenCountMeta.ToolsCount++
  310. if t.Name != "" {
  311. texts = append(texts, t.Name)
  312. }
  313. if t.UserLocation != nil {
  314. b, _ := common.Marshal(t.UserLocation)
  315. texts = append(texts, string(b))
  316. }
  317. }
  318. }
  319. }
  320. tokenCountMeta.CombineText = strings.Join(texts, "\n")
  321. tokenCountMeta.Files = fileMeta
  322. return &tokenCountMeta
  323. }
  324. func (c *ClaudeRequest) IsStream(ctx *gin.Context) bool {
  325. if c.Stream == nil {
  326. return false
  327. }
  328. return *c.Stream
  329. }
  330. func (c *ClaudeRequest) SetModelName(modelName string) {
  331. if modelName != "" {
  332. c.Model = modelName
  333. }
  334. }
  335. func (c *ClaudeRequest) SearchToolNameByToolCallId(toolCallId string) string {
  336. for _, message := range c.Messages {
  337. content, _ := message.ParseContent()
  338. for _, mediaMessage := range content {
  339. if mediaMessage.Id == toolCallId {
  340. return mediaMessage.Name
  341. }
  342. }
  343. }
  344. return ""
  345. }
  346. // AddTool 添加工具到请求中
  347. func (c *ClaudeRequest) AddTool(tool any) {
  348. if c.Tools == nil {
  349. c.Tools = make([]any, 0)
  350. }
  351. switch tools := c.Tools.(type) {
  352. case []any:
  353. c.Tools = append(tools, tool)
  354. default:
  355. // 如果Tools不是[]any类型,重新初始化为[]any
  356. c.Tools = []any{tool}
  357. }
  358. }
  359. // GetTools 获取工具列表
  360. func (c *ClaudeRequest) GetTools() []any {
  361. if c.Tools == nil {
  362. return nil
  363. }
  364. switch tools := c.Tools.(type) {
  365. case []any:
  366. return tools
  367. default:
  368. return nil
  369. }
  370. }
  371. func (c *ClaudeRequest) GetEfforts() string {
  372. var OutputConfig OutputConfigForEffort
  373. if err := json.Unmarshal(c.OutputConfig, &OutputConfig); err == nil {
  374. effort := OutputConfig.Effort
  375. return effort
  376. }
  377. return ""
  378. }
  379. // ProcessTools 处理工具列表,支持类型断言
  380. func ProcessTools(tools []any) ([]*Tool, []*ClaudeWebSearchTool) {
  381. var normalTools []*Tool
  382. var webSearchTools []*ClaudeWebSearchTool
  383. for _, tool := range tools {
  384. switch t := tool.(type) {
  385. case *Tool:
  386. normalTools = append(normalTools, t)
  387. case *ClaudeWebSearchTool:
  388. webSearchTools = append(webSearchTools, t)
  389. case Tool:
  390. normalTools = append(normalTools, &t)
  391. case ClaudeWebSearchTool:
  392. webSearchTools = append(webSearchTools, &t)
  393. default:
  394. // 未知类型,跳过
  395. continue
  396. }
  397. }
  398. return normalTools, webSearchTools
  399. }
  400. type Thinking struct {
  401. Type string `json:"type,omitempty"`
  402. BudgetTokens *int `json:"budget_tokens,omitempty"`
  403. // Display controls whether thinking content is returned in the response.
  404. // Used with adaptive thinking on Claude Opus 4.7+: "summarized" restores
  405. // the visible summary that was default on Opus 4.6; "omitted" (default on
  406. // 4.7) suppresses it. Pass-through field from upstream Anthropic API.
  407. Display string `json:"display,omitempty"`
  408. }
  409. func (c *Thinking) GetBudgetTokens() int {
  410. if c.BudgetTokens == nil {
  411. return 0
  412. }
  413. return *c.BudgetTokens
  414. }
  415. func (c *ClaudeRequest) IsStringSystem() bool {
  416. _, ok := c.System.(string)
  417. return ok
  418. }
  419. func (c *ClaudeRequest) GetStringSystem() string {
  420. if c.IsStringSystem() {
  421. return c.System.(string)
  422. }
  423. return ""
  424. }
  425. func (c *ClaudeRequest) SetStringSystem(system string) {
  426. c.System = system
  427. }
  428. func (c *ClaudeRequest) ParseSystem() []ClaudeMediaMessage {
  429. mediaContent, _ := common.Any2Type[[]ClaudeMediaMessage](c.System)
  430. return mediaContent
  431. }
  432. type ClaudeErrorWithStatusCode struct {
  433. Error types.ClaudeError `json:"error"`
  434. StatusCode int `json:"status_code"`
  435. LocalError bool
  436. }
  437. type ClaudeResponse struct {
  438. Id string `json:"id,omitempty"`
  439. Type string `json:"type"`
  440. Role string `json:"role,omitempty"`
  441. Content []ClaudeMediaMessage `json:"content,omitempty"`
  442. Completion string `json:"completion,omitempty"`
  443. StopReason string `json:"stop_reason,omitempty"`
  444. Model string `json:"model,omitempty"`
  445. Error any `json:"error,omitempty"`
  446. Usage *ClaudeUsage `json:"usage,omitempty"`
  447. Index *int `json:"index,omitempty"`
  448. ContentBlock *ClaudeMediaMessage `json:"content_block,omitempty"`
  449. Delta *ClaudeMediaMessage `json:"delta,omitempty"`
  450. Message *ClaudeMediaMessage `json:"message,omitempty"`
  451. }
  452. // set index
  453. func (c *ClaudeResponse) SetIndex(i int) {
  454. c.Index = &i
  455. }
  456. // get index
  457. func (c *ClaudeResponse) GetIndex() int {
  458. if c.Index == nil {
  459. return 0
  460. }
  461. return *c.Index
  462. }
  463. // GetClaudeError 从动态错误类型中提取ClaudeError结构
  464. func (c *ClaudeResponse) GetClaudeError() *types.ClaudeError {
  465. if c.Error == nil {
  466. return nil
  467. }
  468. switch err := c.Error.(type) {
  469. case types.ClaudeError:
  470. return &err
  471. case *types.ClaudeError:
  472. return err
  473. case map[string]interface{}:
  474. // 处理从JSON解析来的map结构
  475. claudeErr := &types.ClaudeError{}
  476. if errType, ok := err["type"].(string); ok {
  477. claudeErr.Type = errType
  478. }
  479. if errMsg, ok := err["message"].(string); ok {
  480. claudeErr.Message = errMsg
  481. }
  482. return claudeErr
  483. case string:
  484. // 处理简单字符串错误
  485. return &types.ClaudeError{
  486. Type: "upstream_error",
  487. Message: err,
  488. }
  489. default:
  490. // 未知类型,尝试转换为字符串
  491. return &types.ClaudeError{
  492. Type: "unknown_upstream_error",
  493. Message: fmt.Sprintf("unknown_error: %v", err),
  494. }
  495. }
  496. }
  497. type ClaudeUsage struct {
  498. InputTokens int `json:"input_tokens"`
  499. CacheCreationInputTokens int `json:"cache_creation_input_tokens"`
  500. CacheReadInputTokens int `json:"cache_read_input_tokens"`
  501. OutputTokens int `json:"output_tokens"`
  502. CacheCreation *ClaudeCacheCreationUsage `json:"cache_creation,omitempty"`
  503. // claude cache 1h
  504. ClaudeCacheCreation5mTokens int `json:"claude_cache_creation_5_m_tokens"`
  505. ClaudeCacheCreation1hTokens int `json:"claude_cache_creation_1_h_tokens"`
  506. ServerToolUse *ClaudeServerToolUse `json:"server_tool_use,omitempty"`
  507. }
  508. type ClaudeCacheCreationUsage struct {
  509. Ephemeral5mInputTokens int `json:"ephemeral_5m_input_tokens,omitempty"`
  510. Ephemeral1hInputTokens int `json:"ephemeral_1h_input_tokens,omitempty"`
  511. }
  512. func (u *ClaudeUsage) GetCacheCreation5mTokens() int {
  513. if u == nil || u.CacheCreation == nil {
  514. return 0
  515. }
  516. return u.CacheCreation.Ephemeral5mInputTokens
  517. }
  518. func (u *ClaudeUsage) GetCacheCreation1hTokens() int {
  519. if u == nil || u.CacheCreation == nil {
  520. return 0
  521. }
  522. return u.CacheCreation.Ephemeral1hInputTokens
  523. }
  524. func (u *ClaudeUsage) GetCacheCreationTotalTokens() int {
  525. if u == nil {
  526. return 0
  527. }
  528. if u.CacheCreationInputTokens > 0 {
  529. return u.CacheCreationInputTokens
  530. }
  531. return u.GetCacheCreation5mTokens() + u.GetCacheCreation1hTokens()
  532. }
  533. type ClaudeServerToolUse struct {
  534. WebSearchRequests int `json:"web_search_requests"`
  535. }