account_test_service.go 118 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305
  1. package service
  2. import (
  3. "bufio"
  4. "bytes"
  5. "context"
  6. "crypto/rand"
  7. "encoding/base64"
  8. "encoding/binary"
  9. "encoding/hex"
  10. "encoding/json"
  11. "errors"
  12. "fmt"
  13. "image"
  14. _ "image/gif"
  15. _ "image/jpeg"
  16. _ "image/png"
  17. "io"
  18. "log"
  19. "mime/multipart"
  20. "net/http"
  21. "net/http/httptest"
  22. "net/url"
  23. "regexp"
  24. "strings"
  25. "sync"
  26. "time"
  27. "github.com/Wei-Shaw/sub2api/internal/config"
  28. "github.com/Wei-Shaw/sub2api/internal/pkg/claude"
  29. "github.com/Wei-Shaw/sub2api/internal/pkg/geminicli"
  30. "github.com/Wei-Shaw/sub2api/internal/pkg/openai"
  31. "github.com/Wei-Shaw/sub2api/internal/pkg/openai_compat"
  32. "github.com/Wei-Shaw/sub2api/internal/pkg/xai"
  33. "github.com/Wei-Shaw/sub2api/internal/util/urlvalidator"
  34. "github.com/gin-gonic/gin"
  35. "github.com/google/uuid"
  36. "github.com/tidwall/gjson"
  37. )
  38. // sseDataPrefix matches SSE data lines with optional whitespace after colon.
  39. // Some upstream APIs return non-standard "data:" without space (should be "data: ").
  40. var sseDataPrefix = regexp.MustCompile(`^data:\s*`)
  41. const (
  42. testClaudeAPIURL = "https://api.anthropic.com/v1/messages?beta=true"
  43. chatgptCodexAPIURL = "https://chatgpt.com/backend-api/codex/responses"
  44. defaultAntigravityTestModel = "claude-sonnet-4-6"
  45. )
  46. // TestEvent represents a SSE event for account testing
  47. type TestEvent struct {
  48. Type string `json:"type"`
  49. Text string `json:"text,omitempty"`
  50. Model string `json:"model,omitempty"`
  51. Status string `json:"status,omitempty"`
  52. Code string `json:"code,omitempty"`
  53. ImageURL string `json:"image_url,omitempty"`
  54. // AudioURL / VideoURL are data: or https URLs for in-browser media players.
  55. AudioURL string `json:"audio_url,omitempty"`
  56. VideoURL string `json:"video_url,omitempty"`
  57. MimeType string `json:"mime_type,omitempty"`
  58. Data any `json:"data,omitempty"`
  59. Success bool `json:"success,omitempty"`
  60. Error string `json:"error,omitempty"`
  61. }
  62. // AccountTestOptions carries optional media for admin connectivity tests.
  63. // ImageDataURL / AudioDataURL are full data URLs (data:<mime>;base64,...).
  64. type AccountTestOptions struct {
  65. ImageDataURL string
  66. AudioDataURL string
  67. }
  68. func firstAccountTestOptions(opts []AccountTestOptions) AccountTestOptions {
  69. if len(opts) == 0 {
  70. return AccountTestOptions{}
  71. }
  72. return opts[0]
  73. }
  74. // maxAccountTestMediaBytes caps inbound data-URL payloads for admin tests (~8 MiB).
  75. const maxAccountTestMediaBytes = 8 << 20
  76. const (
  77. defaultGeminiTextTestPrompt = "hi"
  78. defaultGeminiImageTestPrompt = "Generate a cute orange cat astronaut sticker on a clean pastel background."
  79. defaultOpenAIImageTestPrompt = "Generate a cute orange cat astronaut sticker on a clean pastel background."
  80. defaultGrokImageTestPrompt = "Generate a cute orange cat astronaut sticker on a clean pastel background."
  81. defaultGrokVideoTestPrompt = "A red ball bouncing once on a white floor, short simple motion."
  82. defaultGrokSearchTestQuery = "xAI Grok"
  83. defaultGrokTTSTestText = "Hello from Sub2API account connectivity test."
  84. // Grok account-test modes (admin UI). Empty / default / text = Responses probe.
  85. // image/video may also be inferred from model_id when mode is default.
  86. AccountTestModeGrokText = "text"
  87. AccountTestModeGrokImage = "image"
  88. AccountTestModeGrokVideo = "video"
  89. AccountTestModeGrokSearch = "search"
  90. AccountTestModeGrokTTS = "tts"
  91. AccountTestModeGrokSTT = "stt"
  92. AccountTestModeGrokRealtime = "realtime"
  93. defaultGrokRealtimeTestModel = "grok-voice-latest"
  94. grokRealtimeProbeTimeout = DefaultGrokRealtimeDialTimeout
  95. )
  96. // isOpenAIImageModel checks if the model is an OpenAI image generation model (e.g. gpt-image-2).
  97. func isOpenAIImageModel(model string) bool {
  98. return strings.HasPrefix(strings.ToLower(model), "gpt-image-")
  99. }
  100. func isGrokVideoGenerationModel(model string) bool {
  101. return isGrokVideoBillingModel(model) ||
  102. strings.HasPrefix(strings.ToLower(strings.TrimSpace(model)), "grok-video")
  103. }
  104. func normalizeGrokAccountTestMode(mode string) string {
  105. switch strings.ToLower(strings.TrimSpace(mode)) {
  106. case AccountTestModeGrokText:
  107. return AccountTestModeGrokText
  108. case AccountTestModeGrokImage:
  109. return AccountTestModeGrokImage
  110. case AccountTestModeGrokVideo:
  111. return AccountTestModeGrokVideo
  112. case AccountTestModeGrokSearch:
  113. return AccountTestModeGrokSearch
  114. case AccountTestModeGrokTTS:
  115. return AccountTestModeGrokTTS
  116. case AccountTestModeGrokSTT:
  117. return AccountTestModeGrokSTT
  118. case AccountTestModeGrokRealtime:
  119. return AccountTestModeGrokRealtime
  120. default:
  121. return AccountTestModeDefault
  122. }
  123. }
  124. // AccountTestService handles account testing operations
  125. type AccountTestService struct {
  126. concurrencyService *ConcurrencyService
  127. accountRepo AccountRepository
  128. geminiTokenProvider *GeminiTokenProvider
  129. claudeTokenProvider *ClaudeTokenProvider
  130. grokTokenProvider *GrokTokenProvider
  131. antigravityGatewayService *AntigravityGatewayService
  132. httpUpstream HTTPUpstream
  133. cfg *config.Config
  134. settingService *SettingService
  135. tlsFPProfileService *TLSFingerprintProfileService
  136. modelMetadataRegistryMu sync.Mutex
  137. modelMetadataRegistry map[string]modelsDevProvider
  138. modelMetadataRegistryAt time.Time
  139. pluginManager *PluginManager
  140. openaiGatewayService *OpenAIGatewayService
  141. agentIdentityTaskMu sync.Mutex
  142. agentIdentityWS agentIdentityWSConnectionInvalidator
  143. // grokWSDialer is optional; realtime account tests use the default OpenAI-style
  144. // WS dialer when nil (supports proxy + coder/websocket handshake).
  145. grokWSDialer openAIWSClientDialer
  146. }
  147. func (s *AccountTestService) SetSettingService(settingService *SettingService) {
  148. if s != nil {
  149. s.settingService = settingService
  150. }
  151. }
  152. func (s *AccountTestService) SetPluginManager(pluginManager *PluginManager) {
  153. if s != nil {
  154. s.pluginManager = pluginManager
  155. }
  156. }
  157. func (s *AccountTestService) SetOpenAIGatewayService(gateway *OpenAIGatewayService) {
  158. if s != nil {
  159. s.openaiGatewayService = gateway
  160. }
  161. }
  162. // FetchOpenAIAccountModels uses the shared cached discovery path for the test picker.
  163. func (s *AccountTestService) FetchOpenAIAccountModels(ctx context.Context, account *Account) ([]openai.Model, error) {
  164. if s == nil || s.openaiGatewayService == nil {
  165. return nil, errors.New("OpenAI model discovery service is unavailable")
  166. }
  167. response, err := s.openaiGatewayService.FetchOpenAIModelsList(ctx, account)
  168. if err != nil {
  169. return nil, err
  170. }
  171. var payload struct {
  172. Data []openai.Model `json:"data"`
  173. }
  174. if err := json.Unmarshal(response.Body, &payload); err != nil {
  175. return nil, fmt.Errorf("decode OpenAI account models: %w", err)
  176. }
  177. // Standard model catalogs do not require the fields used by the admin picker.
  178. // Populate them here without changing the shared discovery response or cache.
  179. for i := range payload.Data {
  180. model := &payload.Data[i]
  181. if strings.TrimSpace(model.DisplayName) == "" {
  182. model.DisplayName = model.ID
  183. }
  184. if strings.TrimSpace(model.Type) == "" {
  185. model.Type = "model"
  186. }
  187. }
  188. // Codex discovery lists Responses drivers, not image_generation tool models.
  189. // Add locally supported image choices only to the OAuth test picker; keep the
  190. // shared upstream catalog and API-key discovery authoritative.
  191. if account != nil && account.IsOpenAIOAuthLike() {
  192. seen := make(map[string]bool, len(payload.Data))
  193. for _, model := range payload.Data {
  194. seen[model.ID] = true
  195. }
  196. for _, model := range openai.DefaultModels {
  197. if IsGPTImageGenerationModel(model.ID) && account.IsModelSupported(model.ID) && !seen[model.ID] {
  198. payload.Data = append(payload.Data, model)
  199. seen[model.ID] = true
  200. }
  201. }
  202. for model := range account.GetModelMapping() {
  203. if IsGPTImageGenerationModel(model) && !strings.Contains(model, "*") && !seen[model] {
  204. payload.Data = append(payload.Data, openai.Model{ID: model, Object: "model", Type: "model", OwnedBy: "openai", DisplayName: model})
  205. }
  206. }
  207. }
  208. return payload.Data, nil
  209. }
  210. // NewAccountTestService creates a new AccountTestService
  211. func NewAccountTestService(
  212. accountRepo AccountRepository,
  213. geminiTokenProvider *GeminiTokenProvider,
  214. claudeTokenProvider *ClaudeTokenProvider,
  215. grokTokenProvider *GrokTokenProvider,
  216. antigravityGatewayService *AntigravityGatewayService,
  217. httpUpstream HTTPUpstream,
  218. cfg *config.Config,
  219. tlsFPProfileService *TLSFingerprintProfileService,
  220. ) *AccountTestService {
  221. return &AccountTestService{
  222. accountRepo: accountRepo,
  223. geminiTokenProvider: geminiTokenProvider,
  224. claudeTokenProvider: claudeTokenProvider,
  225. grokTokenProvider: grokTokenProvider,
  226. antigravityGatewayService: antigravityGatewayService,
  227. httpUpstream: httpUpstream,
  228. cfg: cfg,
  229. tlsFPProfileService: tlsFPProfileService,
  230. }
  231. }
  232. func (s *AccountTestService) validateUpstreamBaseURL(raw string) (string, error) {
  233. if s.cfg == nil {
  234. return "", errors.New("config is not available")
  235. }
  236. if !s.cfg.Security.URLAllowlist.Enabled {
  237. return urlvalidator.ValidateURLFormat(raw, s.cfg.Security.URLAllowlist.AllowInsecureHTTP)
  238. }
  239. normalized, err := urlvalidator.ValidateHTTPSURL(raw, urlvalidator.ValidationOptions{
  240. AllowedHosts: s.cfg.Security.URLAllowlist.UpstreamHosts,
  241. RequireAllowlist: true,
  242. AllowPrivate: s.cfg.Security.URLAllowlist.AllowPrivateHosts,
  243. })
  244. if err != nil {
  245. return "", err
  246. }
  247. return normalized, nil
  248. }
  249. // generateSessionString generates a Claude Code style session string.
  250. // The output format is determined by the UA version in claude.DefaultHeaders,
  251. // ensuring consistency between the user_id format and the UA sent to upstream.
  252. func generateSessionString() (string, error) {
  253. b := make([]byte, 32)
  254. if _, err := rand.Read(b); err != nil {
  255. return "", err
  256. }
  257. hex64 := hex.EncodeToString(b)
  258. sessionUUID := uuid.New().String()
  259. uaVersion := ExtractCLIVersion(claude.DefaultHeaders["User-Agent"])
  260. return FormatMetadataUserID(hex64, "", sessionUUID, uaVersion), nil
  261. }
  262. // createTestPayload creates a Claude Code style test request payload
  263. func createTestPayload(modelID string) (map[string]any, error) {
  264. sessionID, err := generateSessionString()
  265. if err != nil {
  266. return nil, err
  267. }
  268. return map[string]any{
  269. "model": modelID,
  270. "messages": []map[string]any{
  271. {
  272. "role": "user",
  273. "content": []map[string]any{
  274. {
  275. "type": "text",
  276. "text": "hi",
  277. "cache_control": map[string]string{
  278. "type": "ephemeral",
  279. },
  280. },
  281. },
  282. },
  283. },
  284. "system": []map[string]any{
  285. {
  286. "type": "text",
  287. "text": claudeCodeSystemPrompt,
  288. "cache_control": map[string]string{
  289. "type": "ephemeral",
  290. },
  291. },
  292. },
  293. "metadata": map[string]string{
  294. "user_id": sessionID,
  295. },
  296. "max_tokens": 1024,
  297. "temperature": 1,
  298. "stream": true,
  299. }, nil
  300. }
  301. // TestAccountConnection tests an account's connection by sending a test request
  302. // All account types use full Claude Code client characteristics, only auth header differs
  303. // modelID is optional - if empty, defaults to claude.DefaultTestModel
  304. // mode is optional - "compact" routes OpenAI accounts to the /responses/compact probe path
  305. // opts is optional media (image/audio data URLs for real generation / STT).
  306. func (s *AccountTestService) TestAccountConnection(c *gin.Context, accountID int64, modelID string, prompt string, mode string, opts ...AccountTestOptions) error {
  307. ctx := c.Request.Context()
  308. testOpts := firstAccountTestOptions(opts)
  309. // Get account
  310. account, err := s.accountRepo.GetByID(ctx, accountID)
  311. if err != nil {
  312. return s.sendErrorAndEnd(c, "Account not found")
  313. }
  314. if err := accountTestCooldown(ctx, account, resolveIntelligentTestModel(account, modelID), time.Now()); err != nil {
  315. var wait *TestAdmissionWaitError
  316. if errors.As(err, &wait) {
  317. return s.sendErrorAndEnd(c, fmt.Sprintf("%s;请在 %s 后重新测试", wait.Reason, wait.Until.Local().Format("2006-01-02 15:04:05")))
  318. }
  319. return s.sendErrorAndEnd(c, err.Error())
  320. }
  321. release, err := s.acquireTestAccountSlot(ctx, account)
  322. if err != nil {
  323. return s.sendErrorAndEnd(c, err.Error())
  324. }
  325. defer release()
  326. // Synthetic UI load-test accounts exercise the real SSE parsing and modal
  327. // interactions, but intentionally do not send their placeholder credentials
  328. // to an upstream provider.
  329. if account.IsSyntheticUITest() {
  330. testModelID := modelID
  331. if testModelID == "" {
  332. testModelID = claude.DefaultTestModel
  333. }
  334. s.sendEvent(c, TestEvent{Type: "test_start", Model: testModelID})
  335. s.sendEvent(c, TestEvent{Type: "content", Text: "Synthetic Anthropic OAuth account is healthy and interactive."})
  336. s.sendEvent(c, TestEvent{Type: "test_complete", Success: true})
  337. return nil
  338. }
  339. return s.testAccountConnectionForAccount(c, account, modelID, prompt, mode, testOpts)
  340. }
  341. // testAccountConnectionForAccount always invokes the real provider adapter.
  342. // Capability jobs use this path after authorization and persistence.
  343. func (s *AccountTestService) testAccountConnectionForAccount(c *gin.Context, account *Account, modelID, prompt, mode string, testOpts AccountTestOptions) error {
  344. // Route to platform-specific test method
  345. if account.IsCNProvider() {
  346. switch account.GetAPIProtocol() {
  347. case APIProtocolAdaptive:
  348. return s.testCNProviderAdaptiveConnection(c, account, modelID, prompt)
  349. case APIProtocolResponses:
  350. return s.testOpenAIAccountConnection(c, account, modelID, prompt, normalizeAccountTestMode(mode))
  351. case APIProtocolChatCompletions:
  352. return s.testCNProviderChatCompletionsConnection(c, account, modelID, prompt)
  353. case APIProtocolAnthropic:
  354. return s.testCNProviderAnthropicConnection(c, account, modelID)
  355. }
  356. }
  357. if account.IsOpenAI() {
  358. return s.testOpenAIAccountConnection(c, account, modelID, prompt, normalizeAccountTestMode(mode))
  359. }
  360. if account.IsGemini() {
  361. return s.testGeminiAccountConnection(c, account, modelID, prompt)
  362. }
  363. if account.Platform == PlatformGrok {
  364. return s.testGrokAccountConnection(c, account, modelID, prompt, mode, testOpts)
  365. }
  366. if account.Platform == PlatformAntigravity {
  367. return s.routeAntigravityTest(c, account, modelID, prompt)
  368. }
  369. return s.testClaudeAccountConnection(c, account, modelID)
  370. }
  371. func (s *AccountTestService) testCNProviderChatCompletionsConnection(c *gin.Context, account *Account, modelID string, prompt string) error {
  372. testModelID := strings.TrimSpace(modelID)
  373. if testModelID == "" {
  374. testModelID = openai.DefaultTestModel
  375. }
  376. testModelID = account.GetMappedModel(testModelID)
  377. authToken := strings.TrimSpace(account.GetOpenAIProtocolAPIKey())
  378. if authToken == "" {
  379. return s.sendErrorAndEnd(c, "No API key available")
  380. }
  381. baseURL := account.GetOpenAIBaseURL()
  382. normalizedBaseURL, err := s.validateUpstreamBaseURL(baseURL)
  383. if err != nil {
  384. return s.sendErrorAndEnd(c, fmt.Sprintf("Invalid base URL: %s", err.Error()))
  385. }
  386. return s.testOpenAIChatCompletionsConnection(c, account, testModelID, prompt, normalizedBaseURL, authToken)
  387. }
  388. // testClaudeAccountConnection tests an Anthropic Claude account's connection
  389. func (s *AccountTestService) testClaudeAccountConnection(c *gin.Context, account *Account, modelID string) error {
  390. ctx := c.Request.Context()
  391. // Determine the model to use
  392. testModelID := modelID
  393. if testModelID == "" {
  394. testModelID = claude.DefaultTestModel
  395. }
  396. // API Key 账号测试连接时也需要应用通配符模型映射。
  397. if account.Type == "apikey" {
  398. testModelID = account.GetMappedModel(testModelID)
  399. }
  400. // Bedrock accounts use a separate test path
  401. if account.IsBedrock() {
  402. return s.testBedrockAccountConnection(c, ctx, account, testModelID)
  403. }
  404. if account.Type == AccountTypeServiceAccount {
  405. return s.testClaudeVertexServiceAccountConnection(c, ctx, account, testModelID)
  406. }
  407. // Determine authentication method and API URL
  408. var authToken string
  409. var apiURL string
  410. if account.IsOAuth() {
  411. apiURL = testClaudeAPIURL
  412. authToken = account.GetCredential("access_token")
  413. if authToken == "" {
  414. return s.sendErrorAndEnd(c, "No access token available")
  415. }
  416. } else if account.Type == "apikey" {
  417. authToken = account.GetCredential("api_key")
  418. if authToken == "" {
  419. return s.sendErrorAndEnd(c, "No API key available")
  420. }
  421. baseURL := account.GetBaseURL()
  422. if baseURL == "" {
  423. baseURL = "https://api.anthropic.com"
  424. }
  425. normalizedBaseURL, err := s.validateUpstreamBaseURL(baseURL)
  426. if err != nil {
  427. return s.sendErrorAndEnd(c, fmt.Sprintf("Invalid base URL: %s", err.Error()))
  428. }
  429. apiURL = strings.TrimSuffix(normalizedBaseURL, "/") + "/v1/messages?beta=true"
  430. } else {
  431. return s.sendErrorAndEnd(c, fmt.Sprintf("Unsupported account type: %s", account.Type))
  432. }
  433. // Set SSE headers
  434. c.Writer.Header().Set("Content-Type", "text/event-stream")
  435. c.Writer.Header().Set("Cache-Control", "no-cache")
  436. c.Writer.Header().Set("Connection", "keep-alive")
  437. c.Writer.Header().Set("X-Accel-Buffering", "no")
  438. c.Writer.Flush()
  439. // Create Claude Code style payload (same for all account types)
  440. payload, err := createTestPayload(testModelID)
  441. if err != nil {
  442. return s.sendErrorAndEnd(c, "Failed to create test payload")
  443. }
  444. applyIntelligentPayloadPrompt(ctx, payload)
  445. payloadBytes, _ := json.Marshal(payload)
  446. // Send test_start event
  447. s.sendEvent(c, TestEvent{Type: "test_start", Model: testModelID})
  448. req, err := http.NewRequestWithContext(ctx, "POST", apiURL, bytes.NewReader(payloadBytes))
  449. if err != nil {
  450. return s.sendErrorAndEnd(c, "Failed to create request")
  451. }
  452. // Set common headers
  453. req.Header.Set("Content-Type", "application/json")
  454. req.Header.Set("anthropic-version", "2023-06-01")
  455. // Apply Claude Code client headers
  456. for key, value := range claude.DefaultHeaders {
  457. req.Header.Set(key, value)
  458. }
  459. // Set authentication header
  460. if account.IsOAuth() {
  461. req.Header.Set("anthropic-beta", claude.DefaultBetaHeader)
  462. req.Header.Set("Authorization", "Bearer "+authToken)
  463. } else {
  464. req.Header.Set("anthropic-beta", claude.APIKeyBetaHeader)
  465. // Ollama Cloud Anthropic 兼容端点按实际 base_url 强制 Bearer,
  466. // 其余保持 extra/default 行为。
  467. setAnthropicAPIKeyAuthHeader(req.Header, account, authToken, account.GetBaseURL())
  468. }
  469. // 账号级请求头覆写:测试请求与真实转发保持一致的最终头
  470. account.ApplyHeaderOverrides(req.Header)
  471. // Get proxy URL
  472. proxyURL := ""
  473. if account.ProxyID != nil && account.Proxy != nil {
  474. proxyURL = account.Proxy.URL()
  475. }
  476. resp, err := s.httpUpstream.DoWithTLS(WithAccountTrafficRequest(req, account), proxyURL, account.ID, account.Concurrency, s.tlsFPProfileService.ResolveTLSProfile(account))
  477. if err != nil {
  478. return s.sendErrorAndEnd(c, fmt.Sprintf("Request failed: %s", err.Error()))
  479. }
  480. defer func() { _ = resp.Body.Close() }()
  481. if resp.StatusCode != http.StatusOK {
  482. body, _ := io.ReadAll(resp.Body)
  483. errMsg := fmt.Sprintf("API returned %d: %s", resp.StatusCode, string(body))
  484. // 403 表示账号被上游封禁,标记为 error 状态
  485. if resp.StatusCode == http.StatusForbidden {
  486. _ = s.accountRepo.SetError(ctx, account.ID, errMsg)
  487. }
  488. return s.sendErrorAndEnd(c, errMsg)
  489. }
  490. // Process SSE stream
  491. return s.processClaudeStream(c, resp.Body)
  492. }
  493. func (s *AccountTestService) testClaudeVertexServiceAccountConnection(c *gin.Context, ctx context.Context, account *Account, testModelID string) error {
  494. if mappedModel, matched := account.ResolveMappedModel(testModelID); matched {
  495. testModelID = mappedModel
  496. } else {
  497. testModelID = normalizeVertexAnthropicModelID(claude.NormalizeModelID(testModelID))
  498. }
  499. c.Writer.Header().Set("Content-Type", "text/event-stream")
  500. c.Writer.Header().Set("Cache-Control", "no-cache")
  501. c.Writer.Header().Set("Connection", "keep-alive")
  502. c.Writer.Header().Set("X-Accel-Buffering", "no")
  503. c.Writer.Flush()
  504. payload, err := createTestPayload(testModelID)
  505. if err != nil {
  506. return s.sendErrorAndEnd(c, "Failed to create test payload")
  507. }
  508. applyIntelligentPayloadPrompt(ctx, payload)
  509. payloadBytes, _ := json.Marshal(payload)
  510. vertexBody, err := buildVertexAnthropicRequestBody(payloadBytes)
  511. if err != nil {
  512. return s.sendErrorAndEnd(c, fmt.Sprintf("Failed to create Vertex request body: %s", err.Error()))
  513. }
  514. if s.claudeTokenProvider == nil {
  515. return s.sendErrorAndEnd(c, "Claude token provider not configured")
  516. }
  517. accessToken, err := s.claudeTokenProvider.GetAccessToken(ctx, account)
  518. if err != nil {
  519. return s.sendErrorAndEnd(c, fmt.Sprintf("Failed to get service account access token: %s", err.Error()))
  520. }
  521. fullURL, err := buildVertexAnthropicURL(account.VertexProjectID(), account.VertexLocation(testModelID), testModelID, true)
  522. if err != nil {
  523. return s.sendErrorAndEnd(c, fmt.Sprintf("Failed to build Vertex URL: %s", err.Error()))
  524. }
  525. s.sendEvent(c, TestEvent{Type: "test_start", Model: testModelID})
  526. req, err := http.NewRequestWithContext(ctx, http.MethodPost, fullURL, bytes.NewReader(vertexBody))
  527. if err != nil {
  528. return s.sendErrorAndEnd(c, "Failed to create request")
  529. }
  530. req.Header.Set("Content-Type", "application/json")
  531. req.Header.Set("Authorization", "Bearer "+accessToken)
  532. proxyURL := ""
  533. if account.ProxyID != nil && account.Proxy != nil {
  534. proxyURL = account.Proxy.URL()
  535. }
  536. resp, err := s.httpUpstream.DoWithTLS(WithAccountTrafficRequest(req, account), proxyURL, account.ID, account.Concurrency, s.tlsFPProfileService.ResolveTLSProfile(account))
  537. if err != nil {
  538. return s.sendErrorAndEnd(c, fmt.Sprintf("Request failed: %s", err.Error()))
  539. }
  540. defer func() { _ = resp.Body.Close() }()
  541. if resp.StatusCode != http.StatusOK {
  542. body, _ := io.ReadAll(resp.Body)
  543. errMsg := fmt.Sprintf("API returned %d: %s", resp.StatusCode, string(body))
  544. if resp.StatusCode == http.StatusForbidden {
  545. _ = s.accountRepo.SetError(ctx, account.ID, errMsg)
  546. }
  547. return s.sendErrorAndEnd(c, errMsg)
  548. }
  549. return s.processClaudeStream(c, resp.Body)
  550. }
  551. // testBedrockAccountConnection tests a Bedrock (SigV4 or API Key) account using non-streaming invoke
  552. func (s *AccountTestService) testBedrockAccountConnection(c *gin.Context, ctx context.Context, account *Account, testModelID string) error {
  553. region := bedrockRuntimeRegion(account)
  554. resolvedModelID, ok := ResolveBedrockModelID(account, testModelID)
  555. if !ok {
  556. return s.sendErrorAndEnd(c, fmt.Sprintf("Unsupported Bedrock model: %s", testModelID))
  557. }
  558. testModelID = resolvedModelID
  559. // Set SSE headers (test UI expects SSE)
  560. c.Writer.Header().Set("Content-Type", "text/event-stream")
  561. c.Writer.Header().Set("Cache-Control", "no-cache")
  562. c.Writer.Header().Set("Connection", "keep-alive")
  563. c.Writer.Header().Set("X-Accel-Buffering", "no")
  564. c.Writer.Flush()
  565. // Create a minimal Bedrock-compatible payload (no stream, no cache_control)
  566. bedrockPayload := map[string]any{
  567. "anthropic_version": "bedrock-2023-05-31",
  568. "messages": []map[string]any{
  569. {
  570. "role": "user",
  571. "content": []map[string]any{
  572. {
  573. "type": "text",
  574. "text": "hi",
  575. },
  576. },
  577. },
  578. },
  579. "max_tokens": 256,
  580. "temperature": 1,
  581. }
  582. applyIntelligentPayloadPrompt(ctx, bedrockPayload)
  583. bedrockBody, _ := json.Marshal(bedrockPayload)
  584. // Use non-streaming endpoint (response is standard Claude JSON)
  585. apiURL := BuildBedrockURL(region, testModelID, false)
  586. s.sendEvent(c, TestEvent{Type: "test_start", Model: testModelID})
  587. req, err := http.NewRequestWithContext(ctx, "POST", apiURL, bytes.NewReader(bedrockBody))
  588. if err != nil {
  589. return s.sendErrorAndEnd(c, "Failed to create request")
  590. }
  591. req.Header.Set("Content-Type", "application/json")
  592. // Sign or set auth based on account type
  593. if account.IsBedrockAPIKey() {
  594. apiKey := account.GetCredential("api_key")
  595. if apiKey == "" {
  596. return s.sendErrorAndEnd(c, "No API key available")
  597. }
  598. req.Header.Set("Authorization", "Bearer "+apiKey)
  599. } else {
  600. signer, err := NewBedrockSignerFromAccount(account)
  601. if err != nil {
  602. return s.sendErrorAndEnd(c, fmt.Sprintf("Failed to create Bedrock signer: %s", err.Error()))
  603. }
  604. if err := signer.SignRequest(ctx, req, bedrockBody); err != nil {
  605. return s.sendErrorAndEnd(c, fmt.Sprintf("Failed to sign request: %s", err.Error()))
  606. }
  607. }
  608. proxyURL := ""
  609. if account.ProxyID != nil && account.Proxy != nil {
  610. proxyURL = account.Proxy.URL()
  611. }
  612. resp, err := s.httpUpstream.DoWithTLS(WithAccountTrafficRequest(req, account), proxyURL, account.ID, account.Concurrency, nil)
  613. if err != nil {
  614. return s.sendErrorAndEnd(c, fmt.Sprintf("Request failed: %s", err.Error()))
  615. }
  616. defer func() { _ = resp.Body.Close() }()
  617. body, _ := io.ReadAll(resp.Body)
  618. if resp.StatusCode != http.StatusOK {
  619. return s.sendErrorAndEnd(c, fmt.Sprintf("API returned %d: %s", resp.StatusCode, string(body)))
  620. }
  621. // Bedrock non-streaming response is standard Claude JSON, extract the text
  622. var result struct {
  623. Content []struct {
  624. Text string `json:"text"`
  625. } `json:"content"`
  626. }
  627. if err := json.Unmarshal(body, &result); err != nil {
  628. return s.sendErrorAndEnd(c, fmt.Sprintf("Failed to parse response: %s", err.Error()))
  629. }
  630. text := ""
  631. if len(result.Content) > 0 {
  632. text = result.Content[0].Text
  633. }
  634. if text == "" {
  635. text = "(empty response)"
  636. }
  637. s.sendEvent(c, TestEvent{Type: "content", Text: text})
  638. s.sendEvent(c, TestEvent{Type: "test_complete", Success: true})
  639. return nil
  640. }
  641. // testOpenAIAccountConnection tests an OpenAI account's connection
  642. func (s *AccountTestService) testOpenAIAccountConnection(c *gin.Context, account *Account, modelID string, prompt string, mode string) error {
  643. ctx := c.Request.Context()
  644. mode = normalizeAccountTestMode(mode)
  645. // Default to openai.DefaultTestModel for OpenAI testing
  646. testModelID := modelID
  647. if testModelID == "" {
  648. testModelID = openai.DefaultTestModel
  649. }
  650. // Align test routing with gateway behavior: OpenAI accounts apply normal
  651. // account model mapping. Native remote compaction v2 rides the ordinary
  652. // /responses wire and does NOT apply the legacy compact-only mapping
  653. // (post-#5641 semantics: compact_model_mapping is /responses/compact-only).
  654. testModelID = account.GetMappedModel(testModelID)
  655. if mode == AccountTestModeCompact {
  656. return s.testOpenAICompactConnection(c, account, testModelID)
  657. }
  658. // Route to image generation test if an image model is selected
  659. if isOpenAIImageModel(testModelID) {
  660. imagePrompt := strings.TrimSpace(prompt)
  661. if imagePrompt == "" {
  662. imagePrompt = defaultOpenAIImageTestPrompt
  663. }
  664. if account.Type == "apikey" {
  665. return s.testOpenAIImageAPIKey(c, ctx, account, testModelID, imagePrompt)
  666. }
  667. return s.testOpenAIImageOAuth(c, ctx, account, testModelID, imagePrompt)
  668. }
  669. credentialAccount := account
  670. if account.IsCredentialShadow() {
  671. resolved, err := resolveCredentialAccount(ctx, s.accountRepo, account)
  672. if err != nil {
  673. return s.sendErrorAndEnd(c, err.Error())
  674. }
  675. credentialAccount = resolved
  676. }
  677. // Determine authentication method and API URL
  678. var authToken string
  679. var apiURL string
  680. var isOAuth bool
  681. if credentialAccount.IsOAuth() {
  682. isOAuth = true
  683. // Agent Identity signs each request and does not retain the OAuth token.
  684. if !credentialAccount.IsOpenAIAgentIdentity() {
  685. authToken = credentialAccount.GetOpenAIAccessToken()
  686. }
  687. if authToken == "" && !credentialAccount.IsOpenAIAgentIdentity() {
  688. return s.sendErrorAndEnd(c, "No access token available")
  689. }
  690. // OAuth uses ChatGPT internal API
  691. apiURL = chatgptCodexAPIURL
  692. } else if credentialAccount.Type == "apikey" {
  693. // API Key - use Platform API
  694. authToken = credentialAccount.GetOpenAIProtocolAPIKey()
  695. if authToken == "" {
  696. return s.sendErrorAndEnd(c, "No API key available")
  697. }
  698. baseURL := credentialAccount.GetOpenAIBaseURL()
  699. if baseURL == "" {
  700. baseURL = "https://api.openai.com"
  701. }
  702. normalizedBaseURL, err := s.validateUpstreamBaseURL(baseURL)
  703. if err != nil {
  704. return s.sendErrorAndEnd(c, fmt.Sprintf("Invalid base URL: %s", err.Error()))
  705. }
  706. if !openai_compat.ShouldUseResponsesAPI(account.Extra) {
  707. return s.testOpenAIChatCompletionsConnection(c, account, testModelID, prompt, normalizedBaseURL, authToken)
  708. }
  709. apiURL = buildOpenAIResponsesURLForPlatform(credentialAccount.Platform, normalizedBaseURL)
  710. } else {
  711. return s.sendErrorAndEnd(c, fmt.Sprintf("Unsupported account type: %s", account.Type))
  712. }
  713. // Set SSE headers
  714. c.Writer.Header().Set("Content-Type", "text/event-stream")
  715. c.Writer.Header().Set("Cache-Control", "no-cache")
  716. c.Writer.Header().Set("Connection", "keep-alive")
  717. c.Writer.Header().Set("X-Accel-Buffering", "no")
  718. c.Writer.Flush()
  719. // Create OpenAI Responses API payload. OAuth accounts use ChatGPT Codex
  720. // upstream and must apply the same model normalization as real forwarding.
  721. upstreamTestModelID := testModelID
  722. if isOAuth {
  723. upstreamTestModelID = normalizeOpenAIModelForUpstream(credentialAccount, resolveIntelligentTestModel(credentialAccount, testModelID))
  724. }
  725. payload := createOpenAITestPayload(upstreamTestModelID, isOAuth)
  726. applyIntelligentPayloadPrompt(ctx, payload)
  727. if err := prepareIntelligentTestProtection(c, account, payload); err != nil {
  728. return s.sendErrorAndEnd(c, err.Error())
  729. }
  730. payloadBytes, _ := json.Marshal(payload)
  731. // Send test_start event once. A task-invalid Agent Identity response may
  732. // restart this probe after registering a replacement task.
  733. if !agentIdentityTaskRecoveryWasTried(ctx) {
  734. // Report the model sent to the upstream, not a display alias that the
  735. // ChatGPT/Codex endpoint cannot accept.
  736. s.sendEvent(c, TestEvent{Type: "test_start", Model: upstreamTestModelID})
  737. }
  738. req, err := http.NewRequestWithContext(ctx, "POST", apiURL, bytes.NewReader(payloadBytes))
  739. if err != nil {
  740. return s.sendErrorAndEnd(c, "Failed to create request")
  741. }
  742. req = req.WithContext(WithHTTPUpstreamProfile(req.Context(), HTTPUpstreamProfileOpenAI))
  743. // Set common headers
  744. req.Header.Set("Content-Type", "application/json")
  745. if !isOAuth {
  746. applyOpenAICodexProbeHeaders(req.Header)
  747. }
  748. if credentialAccount.IsOpenAIAgentIdentity() {
  749. authHeaders, authErr := buildAgentIdentityAuthenticationHeaders(ctx, s.accountRepo, s.agentIdentityWS, &s.agentIdentityTaskMu, credentialAccount)
  750. if authErr != nil {
  751. return s.sendErrorAndEnd(c, "Failed to build Agent Identity authentication")
  752. }
  753. for key, values := range authHeaders {
  754. for _, value := range values {
  755. req.Header.Add(key, value)
  756. }
  757. }
  758. } else {
  759. req.Header.Set("Authorization", "Bearer "+authToken)
  760. }
  761. // Set OAuth-specific headers for ChatGPT internal API
  762. if isOAuth {
  763. req.Host = "chatgpt.com"
  764. req.Header.Set("accept", "text/event-stream")
  765. req.Header.Set("OpenAI-Beta", "responses=experimental")
  766. canonical := resolveCodexOutboundIdentity("")
  767. req.Header.Set("Originator", canonical.originator)
  768. if customUA := strings.TrimSpace(credentialAccount.GetOpenAIUserAgent()); customUA != "" {
  769. req.Header.Set("User-Agent", customUA)
  770. } else {
  771. req.Header.Set("User-Agent", canonical.userAgent)
  772. }
  773. setOpenAIChatGPTAccountHeaders(req.Header, credentialAccount)
  774. // 与真实转发一致:账号级自定义 UA 同样作为管理员显式配置传入,否则测试用的身份
  775. // 与该账号真实出站的身份不是同一个(issue #3901 的配对不变式由收口保证)。
  776. enforceCodexIdentityHeadersWithUA(req.Header, credentialAccount.GetOpenAIUserAgent())
  777. }
  778. // 账号级请求头覆写:测试请求与真实转发保持一致的最终头
  779. credentialAccount.ApplyHeaderOverrides(req.Header)
  780. if err := applyIntelligentTestProtection(c, account, req.Header, payloadBytes); err != nil {
  781. return s.sendErrorAndEnd(c, err.Error())
  782. }
  783. // Get proxy URL
  784. proxyURL := ""
  785. if account.ProxyID != nil && account.Proxy != nil {
  786. proxyURL = account.Proxy.URL()
  787. }
  788. resp, err := s.doOpenAIAccountTestUpstream(req, proxyURL, account, true)
  789. if err != nil {
  790. return s.sendErrorAndEnd(c, fmt.Sprintf("Request failed: %s", err.Error()))
  791. }
  792. defer func() { _ = resp.Body.Close() }()
  793. if isOAuth && s.accountRepo != nil {
  794. if updates, err := extractOpenAICodexProbeUpdates(resp); err == nil && len(updates) > 0 {
  795. _ = s.accountRepo.UpdateExtra(ctx, account.ID, updates)
  796. mergeAccountExtra(account, updates)
  797. }
  798. }
  799. if resp.StatusCode != http.StatusOK {
  800. body, _ := io.ReadAll(resp.Body)
  801. body = redactAgentIdentitySensitiveBodyForAccount(ctx, s.accountRepo, credentialAccount, body)
  802. if !agentIdentityTaskRecoveryWasTried(ctx) && credentialAccount.IsOpenAIAgentIdentity() && isAgentIdentityTaskInvalidHTTPResponse(resp.StatusCode, body) {
  803. expectedTaskID := credentialAccount.GetCredential("task_id")
  804. if err := ensureAgentIdentityTaskForAccount(ctx, s.accountRepo, s.agentIdentityWS, &s.agentIdentityTaskMu, credentialAccount, expectedTaskID); err != nil {
  805. return s.sendErrorAndEnd(c, fmt.Sprintf("Agent Identity task recovery failed: %s", err.Error()))
  806. }
  807. c.Request = c.Request.WithContext(markAgentIdentityTaskRecoveryTried(ctx))
  808. return s.testOpenAIAccountConnection(c, account, modelID, prompt, mode)
  809. }
  810. if resp.StatusCode == http.StatusTooManyRequests {
  811. s.reconcileOpenAI429State(ctx, account, resp.Header, body)
  812. }
  813. // 401 Unauthorized: 标记账号为永久错误
  814. if resp.StatusCode == http.StatusUnauthorized && s.accountRepo != nil {
  815. errMsg := fmt.Sprintf("Authentication failed (401): %s", string(body))
  816. _ = s.accountRepo.SetError(ctx, account.ID, errMsg)
  817. }
  818. return s.sendErrorAndEnd(c, fmt.Sprintf("API returned %d: %s", resp.StatusCode, string(body)))
  819. }
  820. // Process SSE stream
  821. return s.processOpenAIStream(c, resp.Body)
  822. }
  823. // testGrokAccountConnection routes Grok admin connectivity tests by explicit mode first,
  824. // then by selected model family for media. Standalone modes (search/tts/stt) never share
  825. // the text Responses path; image/video never hit Responses either.
  826. //
  827. // Modes:
  828. // - default/text → Responses (optional model)
  829. // - image → /v1/images/generations (model optional; defaults to grok-imagine-image)
  830. // - video → /v1/videos/generations (model optional; defaults to grok-imagine-video)
  831. // - search → standalone web-search probe (gateway /v1/web_search semantics)
  832. // - tts → HTTP /v1/tts
  833. // - stt → HTTP /v1/stt (synthetic tiny wav probe)
  834. // - realtime → WS /v1/realtime dial + optional first server event
  835. //
  836. // When mode is default, image/video can still be inferred from model_id for backward compat.
  837. func (s *AccountTestService) testGrokAccountConnection(c *gin.Context, account *Account, modelID, prompt, mode string, opts AccountTestOptions) error {
  838. ctx := c.Request.Context()
  839. // Realtime is WebSocket-only and does not need HTTP upstream.
  840. mode = normalizeGrokAccountTestMode(mode)
  841. if mode != AccountTestModeGrokRealtime && s.httpUpstream == nil {
  842. return s.sendErrorAndEnd(c, "HTTP upstream not configured")
  843. }
  844. authToken, err := s.grokTestAccessToken(ctx, account)
  845. if err != nil {
  846. return s.sendErrorAndEnd(c, err.Error())
  847. }
  848. // Explicit standalone / media modes always win over model id.
  849. switch mode {
  850. case AccountTestModeGrokSearch:
  851. return s.testGrokWebSearch(c, ctx, account, authToken, prompt)
  852. case AccountTestModeGrokTTS:
  853. return s.testGrokTTS(c, ctx, account, authToken, prompt)
  854. case AccountTestModeGrokSTT:
  855. return s.testGrokSTT(c, ctx, account, authToken, opts.AudioDataURL)
  856. case AccountTestModeGrokRealtime:
  857. return s.testGrokRealtime(c, ctx, account, authToken, modelID)
  858. case AccountTestModeGrokImage:
  859. return s.testGrokImageGeneration(c, ctx, account, authToken, resolveGrokImageTestModel(account, modelID), resolveGrokImagePrompt(prompt), opts.ImageDataURL)
  860. case AccountTestModeGrokVideo:
  861. return s.testGrokVideoGeneration(c, ctx, account, authToken, resolveGrokVideoTestModel(account, modelID), resolveGrokVideoPrompt(prompt), opts)
  862. case AccountTestModeGrokText:
  863. // Force text Responses even if model_id looks like media.
  864. testModelID := strings.TrimSpace(modelID)
  865. if testModelID == "" {
  866. testModelID = grokDefaultResponsesModel
  867. }
  868. if mapped := strings.TrimSpace(account.GetMappedModel(testModelID)); mapped != "" {
  869. testModelID = mapped
  870. }
  871. return s.testGrokResponsesConnection(c, ctx, account, authToken, testModelID)
  872. }
  873. // mode == default: infer from model family (legacy UI / API clients).
  874. testModelID := strings.TrimSpace(modelID)
  875. if testModelID == "" {
  876. testModelID = grokDefaultResponsesModel
  877. }
  878. if mapped := strings.TrimSpace(account.GetMappedModel(testModelID)); mapped != "" {
  879. testModelID = mapped
  880. }
  881. switch {
  882. case isGrokImageGenerationModel(testModelID):
  883. return s.testGrokImageGeneration(c, ctx, account, authToken, testModelID, resolveGrokImagePrompt(prompt), opts.ImageDataURL)
  884. case isGrokVideoGenerationModel(testModelID):
  885. return s.testGrokVideoGeneration(c, ctx, account, authToken, testModelID, resolveGrokVideoPrompt(prompt), opts)
  886. default:
  887. return s.testGrokResponsesConnection(c, ctx, account, authToken, testModelID)
  888. }
  889. }
  890. func resolveGrokImagePrompt(prompt string) string {
  891. if strings.TrimSpace(prompt) == "" {
  892. return defaultGrokImageTestPrompt
  893. }
  894. return strings.TrimSpace(prompt)
  895. }
  896. func resolveGrokVideoPrompt(prompt string) string {
  897. if strings.TrimSpace(prompt) == "" {
  898. return defaultGrokVideoTestPrompt
  899. }
  900. return strings.TrimSpace(prompt)
  901. }
  902. func resolveGrokImageTestModel(account *Account, modelID string) string {
  903. testModelID := strings.TrimSpace(modelID)
  904. if testModelID == "" {
  905. testModelID = "grok-imagine-image"
  906. }
  907. if mapped := strings.TrimSpace(account.GetMappedModel(testModelID)); mapped != "" {
  908. return mapped
  909. }
  910. return testModelID
  911. }
  912. func resolveGrokVideoTestModel(account *Account, modelID string) string {
  913. testModelID := strings.TrimSpace(modelID)
  914. if testModelID == "" {
  915. testModelID = "grok-imagine-video"
  916. }
  917. if mapped := strings.TrimSpace(account.GetMappedModel(testModelID)); mapped != "" {
  918. return mapped
  919. }
  920. return testModelID
  921. }
  922. func (s *AccountTestService) grokTestAccessToken(ctx context.Context, account *Account) (string, error) {
  923. switch account.Type {
  924. case AccountTypeOAuth:
  925. if s.grokTokenProvider == nil {
  926. return "", fmt.Errorf("grok token provider not configured")
  927. }
  928. // Manual tests skip production scheduling eligibility so paused/rate-limited
  929. // accounts can still be probed by admins (same as Codex/OpenAI tests).
  930. token, err := s.grokTokenProvider.GetAccessTokenForManualTest(ctx, account)
  931. if err != nil {
  932. return "", fmt.Errorf("failed to get grok access token: %s", err.Error())
  933. }
  934. return token, nil
  935. case AccountTypeAPIKey:
  936. authToken := strings.TrimSpace(account.GetCredential("api_key"))
  937. if authToken == "" {
  938. return "", fmt.Errorf("grok api key is missing")
  939. }
  940. return authToken, nil
  941. default:
  942. return "", fmt.Errorf("unsupported grok account type: %s", account.Type)
  943. }
  944. }
  945. func (s *AccountTestService) grokTestProxyURL(account *Account) string {
  946. if account.ProxyID != nil && account.Proxy != nil {
  947. return account.Proxy.URL()
  948. }
  949. return ""
  950. }
  951. func (s *AccountTestService) prepareGrokTestSSE(c *gin.Context) {
  952. c.Writer.Header().Set("Content-Type", "text/event-stream")
  953. c.Writer.Header().Set("Cache-Control", "no-cache")
  954. c.Writer.Header().Set("Connection", "keep-alive")
  955. c.Writer.Header().Set("X-Accel-Buffering", "no")
  956. c.Writer.Flush()
  957. }
  958. func (s *AccountTestService) applyGrokTestRequestHeaders(req *http.Request, account *Account, authToken string, accept string) {
  959. req.Header.Set("Content-Type", "application/json")
  960. if accept != "" {
  961. req.Header.Set("Accept", accept)
  962. }
  963. req.Header.Set("Authorization", "Bearer "+authToken)
  964. // Match gateway media/voice: CLI identity headers only on the CLI chat proxy.
  965. // api.x.ai media (images/videos) rejects or mistreats OAuth when CLI headers
  966. // are stamped on the official API host (e.g. ZDR upload_url false positives).
  967. if account.IsGrokOAuth() && req.URL != nil && isGrokCLIProxyTarget(req.URL.String()) {
  968. applyGrokCLIHeaders(req.Header)
  969. }
  970. account.ApplyHeaderOverrides(req.Header)
  971. }
  972. func (s *AccountTestService) observeGrokTestResponse(ctx context.Context, account *Account, resp *http.Response) {
  973. if resp == nil {
  974. return
  975. }
  976. now := time.Now()
  977. // Error bodies carry Grok's free-usage, billing, and content-policy
  978. // classifications when quota headers are absent. Read only non-success
  979. // responses here, then restore the body because the caller still needs it
  980. // for the user-facing test result.
  981. var responseBody []byte
  982. if resp.StatusCode >= http.StatusBadRequest && resp.Body != nil {
  983. responseBody, _ = io.ReadAll(resp.Body)
  984. _ = resp.Body.Close()
  985. resp.Body = io.NopCloser(bytes.NewReader(responseBody))
  986. }
  987. snapshot := parseGrokQuotaSnapshot(resp.Header, resp.StatusCode, now)
  988. stampGrokQuotaSnapshotForPlan(account, snapshot, grokRequestedModelFromCtx(ctx))
  989. if snapshot != nil && s.accountRepo != nil {
  990. resetAt, limited := grokRateLimitResetAtForAccount(account, snapshot, now)
  991. if limited {
  992. normalizeGrokExhaustedWindowResets(snapshot, resetAt, now)
  993. }
  994. _ = s.accountRepo.UpdateExtra(ctx, account.ID, map[string]any{
  995. grokQuotaSnapshotExtraKey: snapshot,
  996. })
  997. if limited {
  998. persistGrokRateLimit(ctx, s.accountRepo, account, resetAt)
  999. } else if isSuccessfulGrokRateLimitRecovery(account, snapshot) {
  1000. clearGrokRateLimitAfterRecovery(ctx, s.accountRepo, account)
  1001. }
  1002. } else if s.accountRepo != nil && isSuccessfulGrokRateLimitRecovery(account, &xai.QuotaSnapshot{StatusCode: resp.StatusCode}) {
  1003. clearGrokRateLimitAfterRecovery(ctx, s.accountRepo, account)
  1004. }
  1005. if s.accountRepo == nil || len(responseBody) == 0 {
  1006. if resp.StatusCode == http.StatusPaymentRequired && s.accountRepo != nil {
  1007. stateCtx, cancel := openAIAccountStateContext(ctx)
  1008. defer cancel()
  1009. _ = s.accountRepo.SetTempUnschedulable(stateCtx, account.ID, now.Add(30*time.Minute), "grok payment required")
  1010. }
  1011. return
  1012. }
  1013. if isGrokContentPolicyRejection(resp.StatusCode, responseBody) {
  1014. return
  1015. }
  1016. decision := classifyGrokUpstreamFailure(resp.StatusCode, responseBody, "")
  1017. if decision.Class == GrokFailureFreeUsage {
  1018. if resetAt, limited := grokRateLimitResetAtForAccount(account, snapshot, now); limited && resetAt.After(now) {
  1019. persistGrokRateLimit(ctx, s.accountRepo, account, resetAt)
  1020. } else {
  1021. stateCtx, cancel := openAIAccountStateContext(ctx)
  1022. _ = s.accountRepo.SetTempUnschedulable(stateCtx, account.ID, now.Add(grokFreeUsageProbeCooldown), "grok free usage exhausted")
  1023. cancel()
  1024. }
  1025. return
  1026. }
  1027. if decision.Class == GrokFailureBilling && (isGrokSpendingLimitError(responseBody) || strings.Contains(strings.ToLower(decision.Reason), "credit")) {
  1028. persistGrokRateLimit(ctx, s.accountRepo, account, grokSpendingLimitResetAt(account, now))
  1029. return
  1030. }
  1031. cooldown := time.Duration(0)
  1032. reason := ""
  1033. switch resp.StatusCode {
  1034. case http.StatusUnauthorized:
  1035. cooldown, reason = 10*time.Minute, "grok oauth token unauthorized"
  1036. case http.StatusPaymentRequired:
  1037. cooldown, reason = 30*time.Minute, "grok payment required"
  1038. case http.StatusForbidden:
  1039. cooldown, reason = 30*time.Minute, "grok entitlement or subscription tier denied"
  1040. default:
  1041. if resp.StatusCode >= 500 {
  1042. cooldown, reason = 2*time.Minute, "grok upstream temporary error"
  1043. }
  1044. }
  1045. if decision.Class == GrokFailureBilling && cooldown == 0 {
  1046. cooldown, reason = 30*time.Minute, "grok payment required"
  1047. }
  1048. if cooldown > 0 {
  1049. stateCtx, cancel := openAIAccountStateContext(ctx)
  1050. defer cancel()
  1051. until := now.Add(cooldown)
  1052. if account.TempUnschedulableUntil != nil && account.TempUnschedulableUntil.After(until) {
  1053. until = *account.TempUnschedulableUntil
  1054. }
  1055. _ = s.accountRepo.SetTempUnschedulable(
  1056. stateCtx,
  1057. account.ID,
  1058. until,
  1059. reason,
  1060. )
  1061. }
  1062. }
  1063. func (s *AccountTestService) testGrokResponsesConnection(c *gin.Context, ctx context.Context, account *Account, authToken, testModelID string) error {
  1064. apiURL, err := buildGrokResponsesURL(account, s.cfg, s.settingService)
  1065. if err != nil {
  1066. return s.sendErrorAndEnd(c, fmt.Sprintf("Invalid Grok base URL: %s", err.Error()))
  1067. }
  1068. s.prepareGrokTestSSE(c)
  1069. payloadBytes, err := buildGrokQuotaProbeBody(testModelID)
  1070. if err != nil {
  1071. return s.sendErrorAndEnd(c, "Failed to create Grok test payload")
  1072. }
  1073. if intelligentPrompt(ctx) != "" {
  1074. payload := createOpenAITestPayload(testModelID, false)
  1075. applyIntelligentPayloadPrompt(ctx, payload)
  1076. payloadBytes, _ = json.Marshal(payload)
  1077. }
  1078. if !agentIdentityTaskRecoveryWasTried(ctx) {
  1079. s.sendEvent(c, TestEvent{Type: "test_start", Model: testModelID})
  1080. }
  1081. req, err := http.NewRequestWithContext(ctx, http.MethodPost, apiURL, bytes.NewReader(payloadBytes))
  1082. if err != nil {
  1083. return s.sendErrorAndEnd(c, "Failed to create Grok request")
  1084. }
  1085. s.applyGrokTestRequestHeaders(req, account, authToken, "application/json, text/event-stream")
  1086. resp, err := s.httpUpstream.Do(WithAccountTrafficRequest(req, account), s.grokTestProxyURL(account), account.ID, account.Concurrency)
  1087. if err != nil {
  1088. return s.sendErrorAndEnd(c, fmt.Sprintf("Grok Responses API request failed: %s", err.Error()))
  1089. }
  1090. defer func() { _ = resp.Body.Close() }()
  1091. s.observeGrokTestResponse(withGrokTeamRateLimitModel(ctx, testModelID), account, resp)
  1092. if resp.StatusCode != http.StatusOK {
  1093. body, _ := io.ReadAll(resp.Body)
  1094. return s.sendErrorAndEnd(c, fmt.Sprintf("Grok Responses API returned %d: %s", resp.StatusCode, string(body)))
  1095. }
  1096. return s.processOpenAIStream(c, resp.Body)
  1097. }
  1098. func (s *AccountTestService) testGrokImageGeneration(c *gin.Context, ctx context.Context, account *Account, authToken, modelID, prompt, imageDataURL string) error {
  1099. // With a source image, prefer /images/edits; otherwise /images/generations.
  1100. endpoint := GrokMediaEndpointImagesGenerations
  1101. imageDataURL = strings.TrimSpace(imageDataURL)
  1102. hasSourceImage := imageDataURL != ""
  1103. if hasSourceImage {
  1104. endpoint = GrokMediaEndpointImagesEdits
  1105. }
  1106. // Align model aliases with gateway (e.g. grok-imagine → grok-imagine-image-quality).
  1107. modelID = NormalizeGrokMediaModelForEndpoint(endpoint, modelID, hasSourceImage)
  1108. if modelID == "" {
  1109. modelID = "grok-imagine-image-quality"
  1110. }
  1111. apiURL, err := buildGrokMediaURL(account, s.cfg, endpoint, "")
  1112. if err != nil {
  1113. return s.sendErrorAndEnd(c, fmt.Sprintf("Invalid Grok media base URL: %s", err.Error()))
  1114. }
  1115. s.prepareGrokTestSSE(c)
  1116. s.sendEvent(c, TestEvent{Type: "test_start", Model: modelID})
  1117. if endpoint == GrokMediaEndpointImagesEdits {
  1118. s.sendEvent(c, TestEvent{Type: "status", Text: "Calling Grok /v1/images/edits with uploaded source image..."})
  1119. } else {
  1120. s.sendEvent(c, TestEvent{Type: "status", Text: "Calling Grok /v1/images/generations..."})
  1121. }
  1122. // Zero-data-retention teams reject URL format; always request base64 for admin tests.
  1123. payload := map[string]any{
  1124. "model": modelID,
  1125. "prompt": prompt,
  1126. "n": 1,
  1127. "response_format": "b64_json",
  1128. }
  1129. if hasSourceImage {
  1130. normalized, err := normalizeAccountTestImageDataURL(imageDataURL)
  1131. if err != nil {
  1132. return s.sendErrorAndEnd(c, err.Error())
  1133. }
  1134. // Match gateway prepareGrokMediaForwardBody shape: {url, type:image_url}.
  1135. payload["image"] = grokMediaImageObject(normalized)
  1136. s.sendEvent(c, TestEvent{Type: "content", Text: fmt.Sprintf("source image ready (%d chars data URL)\n", len(normalized))})
  1137. }
  1138. payloadBytes, err := json.Marshal(payload)
  1139. if err != nil {
  1140. return s.sendErrorAndEnd(c, "Failed to marshal Grok image request")
  1141. }
  1142. req, err := http.NewRequestWithContext(ctx, http.MethodPost, apiURL, bytes.NewReader(payloadBytes))
  1143. if err != nil {
  1144. return s.sendErrorAndEnd(c, "Failed to create Grok image request")
  1145. }
  1146. s.applyGrokTestRequestHeaders(req, account, authToken, "application/json")
  1147. req.ContentLength = int64(len(payloadBytes))
  1148. req.GetBody = func() (io.ReadCloser, error) {
  1149. return io.NopCloser(bytes.NewReader(payloadBytes)), nil
  1150. }
  1151. // One retry on transport EOF (proxies occasionally drop large edit payloads).
  1152. var resp *http.Response
  1153. var doErr error
  1154. for attempt := 0; attempt < 2; attempt++ {
  1155. if attempt > 0 {
  1156. s.sendEvent(c, TestEvent{Type: "status", Text: "Retrying Grok image request after transport error..."})
  1157. req, err = http.NewRequestWithContext(ctx, http.MethodPost, apiURL, bytes.NewReader(payloadBytes))
  1158. if err != nil {
  1159. return s.sendErrorAndEnd(c, "Failed to create Grok image retry request")
  1160. }
  1161. s.applyGrokTestRequestHeaders(req, account, authToken, "application/json")
  1162. req.ContentLength = int64(len(payloadBytes))
  1163. }
  1164. resp, doErr = s.httpUpstream.Do(WithAccountTrafficRequest(req, account), s.grokTestProxyURL(account), account.ID, account.Concurrency)
  1165. if doErr == nil {
  1166. break
  1167. }
  1168. if !isTransientGrokTransportError(doErr) || attempt == 1 {
  1169. return s.sendErrorAndEnd(c, formatGrokImageTransportError(doErr, hasSourceImage, len(payloadBytes)))
  1170. }
  1171. }
  1172. defer func() { _ = resp.Body.Close() }()
  1173. s.observeGrokTestResponse(ctx, account, resp)
  1174. body, err := io.ReadAll(resp.Body)
  1175. if err != nil {
  1176. return s.sendErrorAndEnd(c, fmt.Sprintf("Failed to read Grok image response: %s", err.Error()))
  1177. }
  1178. if resp.StatusCode != http.StatusOK {
  1179. return s.sendErrorAndEnd(c, formatGrokImagesAPIError(resp.StatusCode, body, hasSourceImage))
  1180. }
  1181. var result struct {
  1182. Data []struct {
  1183. URL string `json:"url"`
  1184. B64JSON string `json:"b64_json"`
  1185. RevisedPrompt string `json:"revised_prompt"`
  1186. MimeType string `json:"mime_type"`
  1187. } `json:"data"`
  1188. }
  1189. if err := json.Unmarshal(body, &result); err != nil {
  1190. return s.sendErrorAndEnd(c, fmt.Sprintf("Failed to parse Grok image response: %s", err.Error()))
  1191. }
  1192. if len(result.Data) == 0 {
  1193. return s.sendErrorAndEnd(c, "No images returned from Grok API")
  1194. }
  1195. for _, item := range result.Data {
  1196. if item.RevisedPrompt != "" {
  1197. s.sendEvent(c, TestEvent{Type: "content", Text: item.RevisedPrompt})
  1198. }
  1199. mimeType := strings.TrimSpace(item.MimeType)
  1200. if mimeType == "" {
  1201. mimeType = "image/jpeg"
  1202. }
  1203. switch {
  1204. case strings.TrimSpace(item.B64JSON) != "":
  1205. s.sendEvent(c, TestEvent{
  1206. Type: "image",
  1207. ImageURL: "data:" + mimeType + ";base64," + item.B64JSON,
  1208. MimeType: mimeType,
  1209. })
  1210. case strings.TrimSpace(item.URL) != "":
  1211. s.sendEvent(c, TestEvent{Type: "image", ImageURL: item.URL, MimeType: mimeType})
  1212. }
  1213. }
  1214. s.sendEvent(c, TestEvent{Type: "test_complete", Success: true})
  1215. return nil
  1216. }
  1217. func (s *AccountTestService) testGrokVideoGeneration(c *gin.Context, ctx context.Context, account *Account, authToken, modelID, prompt string, opts AccountTestOptions) error {
  1218. apiURL, err := buildGrokMediaURL(account, s.cfg, GrokMediaEndpointVideosGenerations, "")
  1219. if err != nil {
  1220. return s.sendErrorAndEnd(c, fmt.Sprintf("Invalid Grok media base URL: %s", err.Error()))
  1221. }
  1222. s.prepareGrokTestSSE(c)
  1223. s.sendEvent(c, TestEvent{Type: "test_start", Model: modelID})
  1224. s.sendEvent(c, TestEvent{Type: "status", Text: "Calling Grok /v1/videos/generations..."})
  1225. payload := map[string]any{
  1226. "model": modelID,
  1227. "prompt": prompt,
  1228. "duration": 6,
  1229. "aspect_ratio": "16:9",
  1230. "resolution": "480p",
  1231. }
  1232. if img := strings.TrimSpace(opts.ImageDataURL); img != "" {
  1233. normalized, err := normalizeAccountTestImageDataURL(img)
  1234. if err != nil {
  1235. return s.sendErrorAndEnd(c, err.Error())
  1236. }
  1237. // First-frame / image-to-video input (xAI image field).
  1238. payload["image"] = grokMediaImageObject(normalized)
  1239. s.sendEvent(c, TestEvent{Type: "content", Text: "using uploaded first-frame / reference image\n"})
  1240. }
  1241. applyIntelligentPayloadPrompt(ctx, payload)
  1242. payloadBytes, _ := json.Marshal(payload)
  1243. req, err := http.NewRequestWithContext(ctx, http.MethodPost, apiURL, bytes.NewReader(payloadBytes))
  1244. if err != nil {
  1245. return s.sendErrorAndEnd(c, "Failed to create Grok video request")
  1246. }
  1247. s.applyGrokTestRequestHeaders(req, account, authToken, "application/json")
  1248. resp, err := s.httpUpstream.Do(WithAccountTrafficRequest(req, account), s.grokTestProxyURL(account), account.ID, account.Concurrency)
  1249. if err != nil {
  1250. return s.sendErrorAndEnd(c, fmt.Sprintf("Grok video request failed: %s", err.Error()))
  1251. }
  1252. defer func() { _ = resp.Body.Close() }()
  1253. s.observeGrokTestResponse(ctx, account, resp)
  1254. body, err := io.ReadAll(resp.Body)
  1255. if err != nil {
  1256. return s.sendErrorAndEnd(c, fmt.Sprintf("Failed to read Grok video response: %s", err.Error()))
  1257. }
  1258. if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusAccepted && resp.StatusCode != http.StatusCreated {
  1259. return s.sendErrorAndEnd(c, fmt.Sprintf("Grok videos API returned %d: %s", resp.StatusCode, string(body)))
  1260. }
  1261. requestID := strings.TrimSpace(gjson.GetBytes(body, "request_id").String())
  1262. if requestID == "" {
  1263. requestID = strings.TrimSpace(gjson.GetBytes(body, "id").String())
  1264. }
  1265. if requestID == "" {
  1266. return s.sendErrorAndEnd(c, fmt.Sprintf("Grok video create response missing request_id: %s", string(body)))
  1267. }
  1268. s.sendEvent(c, TestEvent{Type: "content", Text: fmt.Sprintf("video request accepted: %s\n", requestID)})
  1269. s.sendEvent(c, TestEvent{Type: "status", Text: "Polling video status until done (max ~60s)..."})
  1270. statusURL, err := buildGrokMediaURL(account, s.cfg, GrokMediaEndpointVideoStatus, requestID)
  1271. if err != nil {
  1272. return s.sendErrorAndEnd(c, fmt.Sprintf("Invalid Grok video status URL: %s", err.Error()))
  1273. }
  1274. deadline := time.Now().Add(60 * time.Second)
  1275. for time.Now().Before(deadline) {
  1276. if ctx.Err() != nil {
  1277. return s.sendErrorAndEnd(c, "Grok video poll canceled")
  1278. }
  1279. statusReq, err := http.NewRequestWithContext(ctx, http.MethodGet, statusURL, nil)
  1280. if err != nil {
  1281. return s.sendErrorAndEnd(c, "Failed to create Grok video status request")
  1282. }
  1283. s.applyGrokTestRequestHeaders(statusReq, account, authToken, "application/json")
  1284. statusResp, err := s.httpUpstream.Do(WithAccountTrafficRequest(statusReq, account), s.grokTestProxyURL(account), account.ID, account.Concurrency)
  1285. if err != nil {
  1286. return s.sendErrorAndEnd(c, fmt.Sprintf("Grok video status failed: %s", err.Error()))
  1287. }
  1288. statusBody, _ := io.ReadAll(statusResp.Body)
  1289. _ = statusResp.Body.Close()
  1290. if statusResp.StatusCode != http.StatusOK && statusResp.StatusCode != http.StatusAccepted {
  1291. return s.sendErrorAndEnd(c, fmt.Sprintf("Grok video status returned %d: %s", statusResp.StatusCode, string(statusBody)))
  1292. }
  1293. st := strings.ToLower(strings.TrimSpace(gjson.GetBytes(statusBody, "status").String()))
  1294. progress := gjson.GetBytes(statusBody, "progress")
  1295. if progress.Exists() {
  1296. s.sendEvent(c, TestEvent{Type: "status", Text: fmt.Sprintf("status=%s progress=%v", st, progress.Value())})
  1297. } else {
  1298. s.sendEvent(c, TestEvent{Type: "status", Text: "status=" + st})
  1299. }
  1300. switch st {
  1301. case "done", "completed", "succeeded", "success":
  1302. return s.emitGrokVideoResult(c, ctx, account, authToken, requestID, statusBody)
  1303. case "failed", "error", "canceled", "cancelled":
  1304. return s.sendErrorAndEnd(c, fmt.Sprintf("Grok video failed: %s", string(statusBody)))
  1305. }
  1306. select {
  1307. case <-ctx.Done():
  1308. return s.sendErrorAndEnd(c, "Grok video poll canceled")
  1309. case <-time.After(3 * time.Second):
  1310. }
  1311. }
  1312. return s.sendErrorAndEnd(c, "Grok video still processing after 60s (request_id="+requestID+")")
  1313. }
  1314. // emitGrokVideoResult surfaces a playable video URL or downloads /content as data URL.
  1315. func (s *AccountTestService) emitGrokVideoResult(c *gin.Context, ctx context.Context, account *Account, authToken, requestID string, statusBody []byte) error {
  1316. videoURL := firstNonEmpty(
  1317. strings.TrimSpace(gjson.GetBytes(statusBody, "video.url").String()),
  1318. strings.TrimSpace(gjson.GetBytes(statusBody, "url").String()),
  1319. strings.TrimSpace(gjson.GetBytes(statusBody, "video_url").String()),
  1320. strings.TrimSpace(gjson.GetBytes(statusBody, "download_url").String()),
  1321. )
  1322. if videoURL != "" && (strings.HasPrefix(videoURL, "http://") || strings.HasPrefix(videoURL, "https://") || strings.HasPrefix(videoURL, "data:")) {
  1323. s.sendEvent(c, TestEvent{Type: "content", Text: "video ready: " + videoURL + "\n"})
  1324. s.sendEvent(c, TestEvent{Type: "video", VideoURL: videoURL, MimeType: "video/mp4"})
  1325. s.sendEvent(c, TestEvent{Type: "test_complete", Success: true})
  1326. return nil
  1327. }
  1328. // Fetch binary content via official /videos/{id}/content (Bearer-authenticated).
  1329. contentURL, err := buildGrokMediaURL(account, s.cfg, GrokMediaEndpointVideoContent, requestID)
  1330. if err != nil {
  1331. return s.sendErrorAndEnd(c, fmt.Sprintf("Invalid Grok video content URL: %s", err.Error()))
  1332. }
  1333. s.sendEvent(c, TestEvent{Type: "status", Text: "Downloading video content for preview..."})
  1334. req, err := http.NewRequestWithContext(ctx, http.MethodGet, contentURL, nil)
  1335. if err != nil {
  1336. return s.sendErrorAndEnd(c, "Failed to create Grok video content request")
  1337. }
  1338. s.applyGrokTestRequestHeaders(req, account, authToken, "video/*, application/octet-stream, */*")
  1339. resp, err := s.httpUpstream.Do(WithAccountTrafficRequest(req, account), s.grokTestProxyURL(account), account.ID, account.Concurrency)
  1340. if err != nil {
  1341. return s.sendErrorAndEnd(c, fmt.Sprintf("Grok video content download failed: %s", err.Error()))
  1342. }
  1343. defer func() { _ = resp.Body.Close() }()
  1344. body, _ := io.ReadAll(io.LimitReader(resp.Body, 64<<20)) // 64 MiB cap for admin preview
  1345. if resp.StatusCode != http.StatusOK {
  1346. // Fall back to status URL when binary content is unavailable.
  1347. if videoURL != "" {
  1348. s.sendEvent(c, TestEvent{Type: "content", Text: "video completed; content download unavailable, reported url=" + videoURL + "\n"})
  1349. s.sendEvent(c, TestEvent{Type: "video", VideoURL: videoURL, MimeType: "video/mp4"})
  1350. s.sendEvent(c, TestEvent{Type: "test_complete", Success: true})
  1351. return nil
  1352. }
  1353. return s.sendErrorAndEnd(c, fmt.Sprintf("Grok video content returned %d: %s", resp.StatusCode, truncateString(string(body), 300)))
  1354. }
  1355. ct := resp.Header.Get("Content-Type")
  1356. if ct == "" || strings.HasPrefix(ct, "application/octet-stream") {
  1357. ct = "video/mp4"
  1358. }
  1359. // Keep only type/subtype for data URL.
  1360. if i := strings.Index(ct, ";"); i >= 0 {
  1361. ct = strings.TrimSpace(ct[:i])
  1362. }
  1363. dataURL := "data:" + ct + ";base64," + base64.StdEncoding.EncodeToString(body)
  1364. s.sendEvent(c, TestEvent{Type: "content", Text: fmt.Sprintf("video content downloaded: content-type=%s bytes=%d\n", ct, len(body))})
  1365. s.sendEvent(c, TestEvent{Type: "video", VideoURL: dataURL, MimeType: ct})
  1366. s.sendEvent(c, TestEvent{Type: "test_complete", Success: true})
  1367. return nil
  1368. }
  1369. func (s *AccountTestService) testGrokWebSearch(c *gin.Context, ctx context.Context, account *Account, authToken, query string) error {
  1370. query = strings.TrimSpace(query)
  1371. if query == "" {
  1372. query = defaultGrokSearchTestQuery
  1373. }
  1374. // Account-test "web_search" mode mirrors the standalone gateway endpoint
  1375. // POST /v1/web_search (not a free-form chat with tools). Implementation still
  1376. // uses the same DoGrokNativeResponsesJSON helper as the gateway handler so
  1377. // results match production search.
  1378. s.prepareGrokTestSSE(c)
  1379. s.sendEvent(c, TestEvent{Type: "test_start", Model: "grok-web-search"})
  1380. s.sendEvent(c, TestEvent{Type: "status", Text: "Calling standalone web_search probe (same as gateway /v1/web_search)..."})
  1381. // Keep parity with handler.buildGrokWebSearchPrompt / include sources.
  1382. const maxResults = 5
  1383. prompt := fmt.Sprintf(
  1384. `Search the web for the user query below. Return ONLY valid JSON with this exact shape: {"results":[{"url":"https://...","title":"page title","snippet":"concise factual summary"}]}. Return at most %d unique results. Every URL must be an actual web_search source. Populate a non-empty title and snippet for every result. Do not wrap the JSON in markdown.
  1385. User query:
  1386. %s`, maxResults, query)
  1387. payload := map[string]any{
  1388. "model": grokDefaultResponsesModel,
  1389. "input": prompt,
  1390. "tools": []map[string]any{{"type": "web_search"}},
  1391. "include": []string{"web_search_call.action.sources"},
  1392. "store": false,
  1393. "stream": false,
  1394. }
  1395. applyIntelligentPayloadPrompt(ctx, payload)
  1396. payloadBytes, _ := json.Marshal(payload)
  1397. apiURL, err := buildGrokResponsesURL(account, s.cfg, s.settingService)
  1398. if err != nil {
  1399. return s.sendErrorAndEnd(c, fmt.Sprintf("Invalid Grok base URL: %s", err.Error()))
  1400. }
  1401. req, err := http.NewRequestWithContext(ctx, http.MethodPost, apiURL, bytes.NewReader(payloadBytes))
  1402. if err != nil {
  1403. return s.sendErrorAndEnd(c, "Failed to create standalone web_search probe request")
  1404. }
  1405. s.applyGrokTestRequestHeaders(req, account, authToken, "application/json")
  1406. resp, err := s.httpUpstream.Do(WithAccountTrafficRequest(req, account), s.grokTestProxyURL(account), account.ID, account.Concurrency)
  1407. if err != nil {
  1408. return s.sendErrorAndEnd(c, fmt.Sprintf("standalone web_search probe failed: %s", err.Error()))
  1409. }
  1410. defer func() { _ = resp.Body.Close() }()
  1411. s.observeGrokTestResponse(withGrokTeamRateLimitModel(ctx, grokDefaultResponsesModel), account, resp)
  1412. body, _ := io.ReadAll(resp.Body)
  1413. if resp.StatusCode != http.StatusOK {
  1414. return s.sendErrorAndEnd(c, fmt.Sprintf("standalone web_search probe returned %d: %s", resp.StatusCode, string(body)))
  1415. }
  1416. // Normalize like gateway extractGrokWebSearchSources (URL-only sources are enough for connectivity).
  1417. sourceCount := 0
  1418. gjson.GetBytes(body, "output").ForEach(func(_, item gjson.Result) bool {
  1419. if item.Get("type").String() != "web_search_call" {
  1420. return true
  1421. }
  1422. sources := item.Get("action.sources")
  1423. if sources.IsArray() {
  1424. sourceCount += len(sources.Array())
  1425. }
  1426. return true
  1427. })
  1428. searchCount := countGrokNativeSearchCallsFromJSONBytes(body)
  1429. s.sendEvent(c, TestEvent{Type: "content", Text: fmt.Sprintf("web_search ok: query=%q tool_calls=%d sources=%d\n", query, searchCount, sourceCount)})
  1430. // Optional: first structured result title if model returned JSON text.
  1431. gjson.GetBytes(body, "output").ForEach(func(_, item gjson.Result) bool {
  1432. if item.Get("type").String() != "message" {
  1433. return true
  1434. }
  1435. for _, part := range item.Get("content").Array() {
  1436. text := strings.TrimSpace(part.Get("text").String())
  1437. if text == "" {
  1438. continue
  1439. }
  1440. if len(text) > 300 {
  1441. text = text[:300] + "..."
  1442. }
  1443. s.sendEvent(c, TestEvent{Type: "content", Text: text + "\n"})
  1444. return false
  1445. }
  1446. return true
  1447. })
  1448. if searchCount == 0 && sourceCount == 0 {
  1449. return s.sendErrorAndEnd(c, "standalone web_search probe completed but no search sources/tool calls were observed")
  1450. }
  1451. s.sendEvent(c, TestEvent{Type: "test_complete", Success: true})
  1452. return nil
  1453. }
  1454. func (s *AccountTestService) testGrokTTS(c *gin.Context, ctx context.Context, account *Account, authToken, text string) error {
  1455. text = strings.TrimSpace(text)
  1456. if text == "" {
  1457. text = defaultGrokTTSTestText
  1458. }
  1459. apiURL, err := buildGrokVoiceURL(account, s.cfg, "tts")
  1460. if err != nil {
  1461. return s.sendErrorAndEnd(c, fmt.Sprintf("Invalid Grok TTS URL: %s", err.Error()))
  1462. }
  1463. s.prepareGrokTestSSE(c)
  1464. s.sendEvent(c, TestEvent{Type: "test_start", Model: "grok-voice-tts"})
  1465. s.sendEvent(c, TestEvent{Type: "status", Text: "Calling standalone /v1/tts..."})
  1466. // xAI requires `language`; optional voice_id. Prefer the shape that matches
  1467. // live gateway probes (text + language [+ voice_id]).
  1468. payloads := []map[string]any{
  1469. {"text": text, "language": "en", "voice_id": "Ara"},
  1470. {"text": text, "language": "en"},
  1471. {"text": text, "language": "English", "voice_id": "Ara"},
  1472. }
  1473. var lastBody string
  1474. var lastCode int
  1475. for _, payload := range payloads {
  1476. applyIntelligentPayloadPrompt(ctx, payload)
  1477. payloadBytes, _ := json.Marshal(payload)
  1478. req, err := http.NewRequestWithContext(ctx, http.MethodPost, apiURL, bytes.NewReader(payloadBytes))
  1479. if err != nil {
  1480. return s.sendErrorAndEnd(c, "Failed to create Grok TTS request")
  1481. }
  1482. s.applyGrokTestRequestHeaders(req, account, authToken, "audio/*, application/json, */*")
  1483. resp, err := s.httpUpstream.Do(WithAccountTrafficRequest(req, account), s.grokTestProxyURL(account), account.ID, account.Concurrency)
  1484. if err != nil {
  1485. return s.sendErrorAndEnd(c, fmt.Sprintf("Grok TTS failed: %s", err.Error()))
  1486. }
  1487. body, _ := io.ReadAll(resp.Body)
  1488. _ = resp.Body.Close()
  1489. s.observeGrokTestResponse(ctx, account, resp)
  1490. lastCode = resp.StatusCode
  1491. lastBody = string(body)
  1492. if resp.StatusCode == http.StatusOK {
  1493. ct := resp.Header.Get("Content-Type")
  1494. if ct == "" {
  1495. ct = "audio/mpeg"
  1496. }
  1497. if i := strings.Index(ct, ";"); i >= 0 {
  1498. ct = strings.TrimSpace(ct[:i])
  1499. }
  1500. // Cap preview size so SSE stays manageable (~4 MiB audio).
  1501. if len(body) > 4<<20 {
  1502. body = body[:4<<20]
  1503. }
  1504. audioURL := "data:" + ct + ";base64," + base64.StdEncoding.EncodeToString(body)
  1505. s.sendEvent(c, TestEvent{Type: "content", Text: fmt.Sprintf("tts ok: content-type=%s bytes=%d\n", ct, len(body))})
  1506. s.sendEvent(c, TestEvent{Type: "audio", AudioURL: audioURL, MimeType: ct})
  1507. s.sendEvent(c, TestEvent{Type: "test_complete", Success: true})
  1508. return nil
  1509. }
  1510. if resp.StatusCode < 400 || resp.StatusCode >= 500 {
  1511. break
  1512. }
  1513. }
  1514. return s.sendErrorAndEnd(c, fmt.Sprintf("Grok TTS returned %d: %s", lastCode, lastBody))
  1515. }
  1516. // testGrokSTT posts audio to /v1/stt. When audioDataURL is set, uses the
  1517. // uploaded file; otherwise a tiny synthetic silent WAV for connectivity only.
  1518. func (s *AccountTestService) testGrokSTT(c *gin.Context, ctx context.Context, account *Account, authToken, audioDataURL string) error {
  1519. apiURL, err := buildGrokVoiceURL(account, s.cfg, "stt")
  1520. if err != nil {
  1521. return s.sendErrorAndEnd(c, fmt.Sprintf("Invalid Grok STT URL: %s", err.Error()))
  1522. }
  1523. s.prepareGrokTestSSE(c)
  1524. s.sendEvent(c, TestEvent{Type: "test_start", Model: "grok-voice-stt"})
  1525. var audioBytes []byte
  1526. filename := "probe.wav"
  1527. if audioDataURL = strings.TrimSpace(audioDataURL); audioDataURL != "" {
  1528. if err := validateAccountTestDataURL(audioDataURL, "audio/"); err != nil {
  1529. return s.sendErrorAndEnd(c, err.Error())
  1530. }
  1531. raw, mime, err := decodeAccountTestDataURL(audioDataURL)
  1532. if err != nil {
  1533. return s.sendErrorAndEnd(c, "Invalid audio data URL: "+err.Error())
  1534. }
  1535. audioBytes = raw
  1536. filename = sttFilenameForMIME(mime)
  1537. s.sendEvent(c, TestEvent{Type: "status", Text: "Calling standalone /v1/stt with uploaded audio..."})
  1538. s.sendEvent(c, TestEvent{Type: "content", Text: fmt.Sprintf("uploaded audio: mime=%s bytes=%d\n", mime, len(audioBytes))})
  1539. } else {
  1540. audioBytes = minimalSilentWAV()
  1541. s.sendEvent(c, TestEvent{Type: "status", Text: "Calling standalone /v1/stt with a synthetic silent WAV..."})
  1542. }
  1543. var bodyBuf bytes.Buffer
  1544. w := multipart.NewWriter(&bodyBuf)
  1545. part, err := w.CreateFormFile("file", filename)
  1546. if err != nil {
  1547. return s.sendErrorAndEnd(c, "Failed to build STT multipart body")
  1548. }
  1549. if _, err := part.Write(audioBytes); err != nil {
  1550. return s.sendErrorAndEnd(c, "Failed to write STT audio part")
  1551. }
  1552. _ = w.WriteField("model", "grok-stt")
  1553. _ = w.WriteField("language", "en")
  1554. if err := w.Close(); err != nil {
  1555. return s.sendErrorAndEnd(c, "Failed to finalize STT multipart body")
  1556. }
  1557. req, err := http.NewRequestWithContext(ctx, http.MethodPost, apiURL, &bodyBuf)
  1558. if err != nil {
  1559. return s.sendErrorAndEnd(c, "Failed to create Grok STT request")
  1560. }
  1561. req.Header.Set("Content-Type", w.FormDataContentType())
  1562. req.Header.Set("Accept", "application/json")
  1563. req.Header.Set("Authorization", "Bearer "+authToken)
  1564. if account.IsGrokOAuth() {
  1565. applyGrokCLIHeaders(req.Header)
  1566. }
  1567. account.ApplyHeaderOverrides(req.Header)
  1568. resp, err := s.httpUpstream.Do(WithAccountTrafficRequest(req, account), s.grokTestProxyURL(account), account.ID, account.Concurrency)
  1569. if err != nil {
  1570. return s.sendErrorAndEnd(c, fmt.Sprintf("Grok STT failed: %s", err.Error()))
  1571. }
  1572. defer func() { _ = resp.Body.Close() }()
  1573. s.observeGrokTestResponse(ctx, account, resp)
  1574. respBody, _ := io.ReadAll(resp.Body)
  1575. if resp.StatusCode != http.StatusOK {
  1576. // 4xx on synthetic audio still proves the STT endpoint is wired; report clearly.
  1577. return s.sendErrorAndEnd(c, fmt.Sprintf("Grok STT returned %d: %s", resp.StatusCode, string(respBody)))
  1578. }
  1579. text := strings.TrimSpace(gjson.GetBytes(respBody, "text").String())
  1580. if text == "" {
  1581. text = strings.TrimSpace(string(respBody))
  1582. if len(text) > 200 {
  1583. text = text[:200] + "..."
  1584. }
  1585. }
  1586. s.sendEvent(c, TestEvent{Type: "content", Text: fmt.Sprintf("stt ok: %s\n", text)})
  1587. s.sendEvent(c, TestEvent{Type: "test_complete", Success: true})
  1588. return nil
  1589. }
  1590. // testGrokRealtime dials the standalone xAI Voice Realtime WebSocket
  1591. // (wss://api.x.ai/v1/realtime?model=...) to verify auth + endpoint reachability.
  1592. // It does not run a full audio session — success is WS handshake, optionally
  1593. // enriched with the first server event type when one arrives quickly.
  1594. func (s *AccountTestService) testGrokRealtime(c *gin.Context, ctx context.Context, account *Account, authToken, modelID string) (trafficErr error) {
  1595. ctx, permit, admissionErr := beginAccountTrafficTurn(ctx, s.httpUpstream, account)
  1596. if admissionErr != nil {
  1597. return s.sendErrorAndEnd(c, admissionErr.Error())
  1598. }
  1599. defer func() { finishAccountTrafficTurn(permit, trafficErr) }()
  1600. model := strings.TrimSpace(modelID)
  1601. if model == "" {
  1602. model = defaultGrokRealtimeTestModel
  1603. }
  1604. if mapped := strings.TrimSpace(account.GetMappedModel(model)); mapped != "" {
  1605. model = mapped
  1606. }
  1607. base, err := buildGrokVoiceURL(account, s.cfg, "realtime")
  1608. if err != nil {
  1609. return s.sendErrorAndEnd(c, fmt.Sprintf("Invalid Grok Realtime URL: %s", err.Error()))
  1610. }
  1611. u, err := url.Parse(base)
  1612. if err != nil {
  1613. return s.sendErrorAndEnd(c, fmt.Sprintf("Invalid Grok Realtime URL: %s", err.Error()))
  1614. }
  1615. switch strings.ToLower(u.Scheme) {
  1616. case "https":
  1617. u.Scheme = "wss"
  1618. case "http":
  1619. u.Scheme = "ws"
  1620. case "wss", "ws":
  1621. // already websocket
  1622. default:
  1623. return s.sendErrorAndEnd(c, "Invalid Grok Realtime URL scheme")
  1624. }
  1625. q := u.Query()
  1626. if q.Get("model") == "" {
  1627. q.Set("model", model)
  1628. }
  1629. u.RawQuery = q.Encode()
  1630. wsURL := u.String()
  1631. s.prepareGrokTestSSE(c)
  1632. s.sendEvent(c, TestEvent{Type: "test_start", Model: model})
  1633. s.sendEvent(c, TestEvent{Type: "status", Text: "Dialing standalone wss /v1/realtime (connectivity probe)..."})
  1634. s.sendEvent(c, TestEvent{Type: "content", Text: fmt.Sprintf("realtime target: %s\n", redactGrokRealtimeURLForLog(wsURL))})
  1635. headers := http.Header{}
  1636. headers.Set("Authorization", "Bearer "+authToken)
  1637. if account.IsGrokOAuth() {
  1638. applyGrokCLIHeaders(headers)
  1639. }
  1640. account.ApplyHeaderOverrides(headers)
  1641. dialer := s.grokWSDialer
  1642. if dialer == nil {
  1643. dialer = newDefaultOpenAIWSClientDialer()
  1644. }
  1645. dialCtx, cancel := context.WithTimeout(ctx, grokRealtimeProbeTimeout)
  1646. defer cancel()
  1647. conn, status, _, dialErr := dialer.Dial(dialCtx, wsURL, headers, s.grokTestProxyURL(account))
  1648. if dialErr != nil {
  1649. detail := dialErr.Error()
  1650. var hs *openAIWSHandshakeError
  1651. if errors.As(dialErr, &hs) && len(hs.Body) > 0 {
  1652. body := strings.TrimSpace(string(hs.Body))
  1653. if len(body) > 300 {
  1654. body = body[:300] + "..."
  1655. }
  1656. detail = fmt.Sprintf("%s body=%s", detail, body)
  1657. }
  1658. if status > 0 {
  1659. return s.sendErrorAndEnd(c, fmt.Sprintf("Grok Realtime WS handshake failed (HTTP %d): %s", status, detail))
  1660. }
  1661. return s.sendErrorAndEnd(c, fmt.Sprintf("Grok Realtime WS dial failed: %s", detail))
  1662. }
  1663. defer func() { _ = conn.Close() }()
  1664. s.sendEvent(c, TestEvent{Type: "content", Text: "realtime ws handshake ok\n"})
  1665. // Best-effort: read one server event if it arrives quickly (session.created etc.).
  1666. // Handshake alone is enough for connectivity; missing first event is not a failure.
  1667. readCtx, readCancel := context.WithTimeout(ctx, 3*time.Second)
  1668. defer readCancel()
  1669. if msg, readErr := conn.ReadMessage(readCtx); readErr == nil && len(msg) > 0 {
  1670. eventType := strings.TrimSpace(gjson.GetBytes(msg, "type").String())
  1671. if eventType == "" {
  1672. eventType = "unknown"
  1673. }
  1674. preview := strings.TrimSpace(string(msg))
  1675. if len(preview) > 240 {
  1676. preview = preview[:240] + "..."
  1677. }
  1678. s.sendEvent(c, TestEvent{
  1679. Type: "content",
  1680. Text: fmt.Sprintf("realtime first event: type=%s payload=%s\n", eventType, preview),
  1681. })
  1682. } else {
  1683. s.sendEvent(c, TestEvent{
  1684. Type: "content",
  1685. Text: "realtime handshake succeeded (no server event within 3s; still connectivity OK)\n",
  1686. })
  1687. }
  1688. s.sendEvent(c, TestEvent{Type: "test_complete", Success: true})
  1689. return nil
  1690. }
  1691. // validateAccountTestDataURL ensures data URLs are well-formed and size-bounded.
  1692. func validateAccountTestDataURL(raw, requiredPrefix string) error {
  1693. raw = strings.TrimSpace(raw)
  1694. if raw == "" {
  1695. return fmt.Errorf("media data URL is empty")
  1696. }
  1697. if !strings.HasPrefix(raw, "data:") {
  1698. return fmt.Errorf("media must be a data: URL (data:<mime>;base64,...)")
  1699. }
  1700. // Rough size check before decode (base64 expands ~4/3).
  1701. if len(raw) > maxAccountTestMediaBytes*2 {
  1702. return fmt.Errorf("media data URL exceeds size limit")
  1703. }
  1704. _, mime, err := decodeAccountTestDataURL(raw)
  1705. if err != nil {
  1706. return err
  1707. }
  1708. if requiredPrefix != "" && !strings.HasPrefix(strings.ToLower(mime), strings.ToLower(requiredPrefix)) {
  1709. return fmt.Errorf("expected media type prefix %q, got %q", requiredPrefix, mime)
  1710. }
  1711. return nil
  1712. }
  1713. // normalizeAccountTestImageDataURL validates an image data URL, enforces xAI
  1714. // minimum dimensions (8x8), and rewrites to a clean data:image/<type>;base64,... form.
  1715. func normalizeAccountTestImageDataURL(raw string) (string, error) {
  1716. if err := validateAccountTestDataURL(raw, "image/"); err != nil {
  1717. return "", err
  1718. }
  1719. data, mime, err := decodeAccountTestDataURL(raw)
  1720. if err != nil {
  1721. return "", err
  1722. }
  1723. // Soft cap decoded bytes (~4 MiB) for edit payloads to avoid upstream/proxy EOF.
  1724. const maxDecodedImage = 4 << 20
  1725. if len(data) > maxDecodedImage {
  1726. return "", fmt.Errorf(
  1727. "source image is too large (%d bytes decoded). Please use a smaller image (under ~4 MB) for admin edit tests",
  1728. len(data),
  1729. )
  1730. }
  1731. cfg, _, err := image.DecodeConfig(bytes.NewReader(data))
  1732. if err != nil {
  1733. // Keep raw data URL if decoder does not understand the codec (e.g. webp
  1734. // without golang.org/x/image/webp); still send upstream and let xAI validate.
  1735. return "data:" + mime + ";base64," + base64.StdEncoding.EncodeToString(data), nil
  1736. }
  1737. if cfg.Width < 8 || cfg.Height < 8 {
  1738. return "", fmt.Errorf(
  1739. "source image is too small (%dx%d). xAI requires both width and height to be at least 8 pixels",
  1740. cfg.Width, cfg.Height,
  1741. )
  1742. }
  1743. // Prefer a stable mime from config when known.
  1744. if mime == "" || mime == "application/octet-stream" {
  1745. mime = "image/png"
  1746. }
  1747. return "data:" + mime + ";base64," + base64.StdEncoding.EncodeToString(data), nil
  1748. }
  1749. func isTransientGrokTransportError(err error) bool {
  1750. if err == nil {
  1751. return false
  1752. }
  1753. msg := strings.ToLower(err.Error())
  1754. return strings.Contains(msg, "unexpected eof") ||
  1755. strings.Contains(msg, "connection reset") ||
  1756. strings.Contains(msg, "broken pipe") ||
  1757. strings.Contains(msg, "i/o timeout") ||
  1758. strings.Contains(msg, "timeout awaiting response")
  1759. }
  1760. func formatGrokImageTransportError(err error, hasSourceImage bool, payloadBytes int) string {
  1761. base := fmt.Sprintf("Grok image request failed: %s", err.Error())
  1762. if !hasSourceImage {
  1763. return base
  1764. }
  1765. return base + fmt.Sprintf(
  1766. " (edit payload ~%d bytes). Tips: use a smaller source image (<4 MB / lower resolution), ensure the account proxy is stable, and retry. xAI /images/edits expects image as {\"url\":\"data:image/...;base64,...\",\"type\":\"image_url\"}.",
  1767. payloadBytes,
  1768. )
  1769. }
  1770. func formatGrokImagesAPIError(status int, body []byte, hasSourceImage bool) string {
  1771. msg := strings.TrimSpace(string(body))
  1772. if len(msg) > 800 {
  1773. msg = msg[:800] + "..."
  1774. }
  1775. prefix := fmt.Sprintf("Grok images API returned %d: %s", status, msg)
  1776. lower := strings.ToLower(msg)
  1777. if hasSourceImage && (strings.Contains(lower, "too small") || strings.Contains(lower, "at least 8")) {
  1778. return prefix + " — upload a source image with both width and height ≥ 8 px."
  1779. }
  1780. return prefix
  1781. }
  1782. func decodeAccountTestDataURL(raw string) (data []byte, mime string, err error) {
  1783. raw = strings.TrimSpace(raw)
  1784. if !strings.HasPrefix(raw, "data:") {
  1785. return nil, "", fmt.Errorf("not a data URL")
  1786. }
  1787. rest := strings.TrimPrefix(raw, "data:")
  1788. comma := strings.Index(rest, ",")
  1789. if comma < 0 {
  1790. return nil, "", fmt.Errorf("invalid data URL (missing comma)")
  1791. }
  1792. meta := rest[:comma]
  1793. payload := rest[comma+1:]
  1794. mime = "application/octet-stream"
  1795. if semi := strings.Index(meta, ";"); semi >= 0 {
  1796. if t := strings.TrimSpace(meta[:semi]); t != "" {
  1797. mime = t
  1798. }
  1799. } else if t := strings.TrimSpace(meta); t != "" {
  1800. mime = t
  1801. }
  1802. if !strings.Contains(strings.ToLower(meta), ";base64") {
  1803. return nil, "", fmt.Errorf("only base64 data URLs are supported")
  1804. }
  1805. decoded, err := base64.StdEncoding.DecodeString(payload)
  1806. if err != nil {
  1807. // Some browsers emit URL-safe base64 without padding.
  1808. decoded, err = base64.RawStdEncoding.DecodeString(strings.TrimRight(payload, "="))
  1809. if err != nil {
  1810. return nil, "", fmt.Errorf("base64 decode failed: %w", err)
  1811. }
  1812. }
  1813. if len(decoded) == 0 {
  1814. return nil, "", fmt.Errorf("decoded media is empty")
  1815. }
  1816. if len(decoded) > maxAccountTestMediaBytes {
  1817. return nil, "", fmt.Errorf("media exceeds %d byte limit", maxAccountTestMediaBytes)
  1818. }
  1819. return decoded, mime, nil
  1820. }
  1821. func sttFilenameForMIME(mime string) string {
  1822. switch strings.ToLower(strings.TrimSpace(mime)) {
  1823. case "audio/mpeg", "audio/mp3":
  1824. return "upload.mp3"
  1825. case "audio/wav", "audio/x-wav", "audio/wave":
  1826. return "upload.wav"
  1827. case "audio/webm":
  1828. return "upload.webm"
  1829. case "audio/ogg", "audio/opus":
  1830. return "upload.ogg"
  1831. case "audio/mp4", "audio/m4a", "audio/x-m4a":
  1832. return "upload.m4a"
  1833. default:
  1834. return "upload.bin"
  1835. }
  1836. }
  1837. // redactGrokRealtimeURLForLog strips query secrets while keeping model for diagnostics.
  1838. func redactGrokRealtimeURLForLog(raw string) string {
  1839. u, err := url.Parse(strings.TrimSpace(raw))
  1840. if err != nil || u == nil {
  1841. return raw
  1842. }
  1843. // Keep model query only.
  1844. model := u.Query().Get("model")
  1845. u.RawQuery = ""
  1846. if model != "" {
  1847. u.RawQuery = "model=" + url.QueryEscape(model)
  1848. }
  1849. // Never log bearer in fragment/userinfo.
  1850. u.User = nil
  1851. u.Fragment = ""
  1852. return u.String()
  1853. }
  1854. // minimalSilentWAV returns a valid tiny mono 8kHz 16-bit PCM WAV (~0.05s silence).
  1855. func minimalSilentWAV() []byte {
  1856. // 400 samples * 2 bytes = 800 data bytes
  1857. const sampleRate = 8000
  1858. const numSamples = 400
  1859. dataSize := numSamples * 2
  1860. buf := make([]byte, 44+dataSize)
  1861. copy(buf[0:], []byte("RIFF"))
  1862. binary.LittleEndian.PutUint32(buf[4:], uint32(36+dataSize))
  1863. copy(buf[8:], []byte("WAVE"))
  1864. copy(buf[12:], []byte("fmt "))
  1865. binary.LittleEndian.PutUint32(buf[16:], 16) // PCM chunk size
  1866. binary.LittleEndian.PutUint16(buf[20:], 1) // PCM
  1867. binary.LittleEndian.PutUint16(buf[22:], 1) // mono
  1868. binary.LittleEndian.PutUint32(buf[24:], sampleRate)
  1869. binary.LittleEndian.PutUint32(buf[28:], sampleRate*2) // byte rate
  1870. binary.LittleEndian.PutUint16(buf[32:], 2) // block align
  1871. binary.LittleEndian.PutUint16(buf[34:], 16) // bits
  1872. copy(buf[36:], []byte("data"))
  1873. binary.LittleEndian.PutUint32(buf[40:], uint32(dataSize))
  1874. // samples already zero (silence)
  1875. return buf
  1876. }
  1877. // testOpenAIChatCompletionsConnection tests an OpenAI-compatible APIKey account
  1878. // through the raw /v1/chat/completions endpoint.
  1879. func (s *AccountTestService) testOpenAIChatCompletionsConnection(
  1880. c *gin.Context,
  1881. account *Account,
  1882. testModelID string,
  1883. prompt string,
  1884. normalizedBaseURL string,
  1885. authToken string,
  1886. ) error {
  1887. ctx := c.Request.Context()
  1888. apiURL := buildOpenAIChatCompletionsURL(normalizedBaseURL)
  1889. c.Writer.Header().Set("Content-Type", "text/event-stream")
  1890. c.Writer.Header().Set("Cache-Control", "no-cache")
  1891. c.Writer.Header().Set("Connection", "keep-alive")
  1892. c.Writer.Header().Set("X-Accel-Buffering", "no")
  1893. c.Writer.Flush()
  1894. payload := createOpenAIChatCompletionsTestPayload(testModelID, prompt)
  1895. applyIntelligentPayloadPrompt(ctx, payload)
  1896. payloadBytes, _ := json.Marshal(payload)
  1897. s.sendEvent(c, TestEvent{Type: "test_start", Model: testModelID})
  1898. s.sendEvent(c, TestEvent{Type: "status", Text: "正在通过 /v1/chat/completions 测试连接"})
  1899. req, err := http.NewRequestWithContext(ctx, http.MethodPost, apiURL, bytes.NewReader(payloadBytes))
  1900. if err != nil {
  1901. return s.sendErrorAndEnd(c, "Failed to create Chat Completions request")
  1902. }
  1903. req = req.WithContext(WithHTTPUpstreamProfile(req.Context(), HTTPUpstreamProfileOpenAI))
  1904. req.Header.Set("Content-Type", "application/json")
  1905. req.Header.Set("Accept", "text/event-stream")
  1906. req.Header.Set("Authorization", "Bearer "+authToken)
  1907. // 账号级请求头覆写:测试请求与真实转发保持一致的最终头
  1908. account.ApplyHeaderOverrides(req.Header)
  1909. proxyURL := ""
  1910. if account.ProxyID != nil && account.Proxy != nil {
  1911. proxyURL = account.Proxy.URL()
  1912. }
  1913. resp, err := s.httpUpstream.DoWithTLS(WithAccountTrafficRequest(req, account), proxyURL, account.ID, account.Concurrency, s.tlsFPProfileService.ResolveTLSProfile(account))
  1914. if err != nil {
  1915. return s.sendErrorAndEnd(c, fmt.Sprintf("Chat Completions API (/v1/chat/completions) request failed: %s", err.Error()))
  1916. }
  1917. defer func() { _ = resp.Body.Close() }()
  1918. if resp.StatusCode != http.StatusOK {
  1919. body, _ := io.ReadAll(resp.Body)
  1920. if resp.StatusCode == http.StatusTooManyRequests {
  1921. s.reconcileOpenAI429State(ctx, account, resp.Header, body)
  1922. }
  1923. if resp.StatusCode == http.StatusUnauthorized && s.accountRepo != nil {
  1924. errMsg := fmt.Sprintf("Chat Completions authentication failed (401): %s", string(body))
  1925. _ = s.accountRepo.SetError(ctx, account.ID, errMsg)
  1926. }
  1927. return s.sendErrorAndEnd(c, fmt.Sprintf("Chat Completions API (/v1/chat/completions) returned %d: %s", resp.StatusCode, string(body)))
  1928. }
  1929. return s.processOpenAIChatCompletionsStream(c, resp.Body)
  1930. }
  1931. // testOpenAICompactConnection probes native remote compaction v2 (streaming
  1932. // /responses with a compaction_trigger input item) and persists the resulting
  1933. // capability state on the account. The legacy unary /responses/compact
  1934. // endpoint has been sunset upstream (404, #5598/#5624) and is no longer probed.
  1935. func (s *AccountTestService) testOpenAICompactConnection(c *gin.Context, account *Account, testModelID string) error {
  1936. ctx := c.Request.Context()
  1937. credentialAccount := account
  1938. if account.IsShadow() {
  1939. resolved, err := resolveCredentialAccount(ctx, s.accountRepo, account)
  1940. if err != nil {
  1941. return s.sendErrorAndEnd(c, "Failed to resolve account credentials")
  1942. }
  1943. credentialAccount = resolved
  1944. }
  1945. authToken := ""
  1946. apiURL := ""
  1947. isOAuth := false
  1948. switch {
  1949. case credentialAccount.IsOAuth():
  1950. isOAuth = true
  1951. if !credentialAccount.IsOpenAIAgentIdentity() {
  1952. authToken = credentialAccount.GetOpenAIAccessToken()
  1953. }
  1954. if authToken == "" && !credentialAccount.IsOpenAIAgentIdentity() {
  1955. return s.sendErrorAndEnd(c, "No access token available")
  1956. }
  1957. apiURL = chatgptCodexAPIURL
  1958. case account.Type == AccountTypeAPIKey:
  1959. authToken = account.GetOpenAIProtocolAPIKey()
  1960. if authToken == "" {
  1961. return s.sendErrorAndEnd(c, "No API key available")
  1962. }
  1963. baseURL := account.GetOpenAIBaseURL()
  1964. if baseURL == "" {
  1965. baseURL = "https://api.openai.com"
  1966. }
  1967. normalizedBaseURL, err := s.validateUpstreamBaseURL(baseURL)
  1968. if err != nil {
  1969. return s.sendErrorAndEnd(c, fmt.Sprintf("Invalid base URL: %s", err.Error()))
  1970. }
  1971. apiURL = buildOpenAIResponsesURLForPlatform(account.Platform, normalizedBaseURL)
  1972. default:
  1973. return s.sendErrorAndEnd(c, fmt.Sprintf("Unsupported account type: %s", account.Type))
  1974. }
  1975. c.Writer.Header().Set("Content-Type", "text/event-stream")
  1976. c.Writer.Header().Set("Cache-Control", "no-cache")
  1977. c.Writer.Header().Set("Connection", "keep-alive")
  1978. c.Writer.Header().Set("X-Accel-Buffering", "no")
  1979. c.Writer.Flush()
  1980. // 原生 v2 走普通 /responses 线:OAuth 与真实转发一致做上游模型归一化。
  1981. if isOAuth {
  1982. testModelID = normalizeOpenAIModelForUpstream(credentialAccount, testModelID)
  1983. }
  1984. payloadBytes, _ := json.Marshal(createOpenAICompactProbePayload(testModelID, isOAuth))
  1985. if !agentIdentityTaskRecoveryWasTried(ctx) {
  1986. s.sendEvent(c, TestEvent{Type: "test_start", Model: testModelID})
  1987. }
  1988. req, err := http.NewRequestWithContext(ctx, "POST", apiURL, bytes.NewReader(payloadBytes))
  1989. if err != nil {
  1990. return s.sendErrorAndEnd(c, "Failed to create request")
  1991. }
  1992. req = req.WithContext(WithHTTPUpstreamProfile(req.Context(), HTTPUpstreamProfileOpenAI))
  1993. req.Header.Set("Content-Type", "application/json")
  1994. // v2 探测是流式请求;同时补注协商头,与真实 codex 出站线型一致。
  1995. req.Header.Set("Accept", "text/event-stream")
  1996. ensureOpenAIRemoteCompactionV2BetaFeature(req.Header)
  1997. if credentialAccount.IsOpenAIAgentIdentity() {
  1998. authHeaders, authErr := buildAgentIdentityAuthenticationHeaders(ctx, s.accountRepo, s.agentIdentityWS, &s.agentIdentityTaskMu, credentialAccount)
  1999. if authErr != nil {
  2000. return s.sendErrorAndEnd(c, "Failed to build Agent Identity authentication")
  2001. }
  2002. for key, values := range authHeaders {
  2003. for _, value := range values {
  2004. req.Header.Add(key, value)
  2005. }
  2006. }
  2007. } else {
  2008. req.Header.Set("Authorization", "Bearer "+authToken)
  2009. }
  2010. applyOpenAICodexProbeHeaders(req.Header)
  2011. if isOAuth {
  2012. enforceCodexIdentityHeadersWithUA(req.Header, credentialAccount.GetOpenAIUserAgent())
  2013. }
  2014. probeSessionID := compactProbeSessionID(account.ID)
  2015. req.Header.Set("Session_ID", probeSessionID)
  2016. req.Header.Set("Conversation_ID", probeSessionID)
  2017. if isOAuth {
  2018. req.Host = "chatgpt.com"
  2019. setOpenAIChatGPTAccountHeaders(req.Header, credentialAccount)
  2020. // 指纹收敛:探测与真实转发走同一个 /responses 端点,身份也必须同构,
  2021. // 否则探测流量会以「缺 x-codex-installation-id + 非收敛 session」的
  2022. // 形态暴露在上游眼里。账号关闭收敛(off)时返回 nil,探测保持原样。
  2023. if fpIDs := resolveCodexFingerprintIDsFromRequest(account, req.Header); fpIDs != nil {
  2024. applyCodexFingerprintHeaders(req.Header, fpIDs)
  2025. }
  2026. }
  2027. // 账号级请求头覆写:测试请求与真实转发保持一致的最终头
  2028. account.ApplyHeaderOverrides(req.Header)
  2029. proxyURL := ""
  2030. if account.ProxyID != nil && account.Proxy != nil {
  2031. proxyURL = account.Proxy.URL()
  2032. }
  2033. resp, err := s.doOpenAIAccountTestUpstream(req, proxyURL, account, true)
  2034. if err != nil {
  2035. if s.accountRepo != nil {
  2036. updates := buildOpenAICompactProbeExtraUpdates(nil, nil, err, false, time.Now())
  2037. _ = s.accountRepo.UpdateExtra(ctx, account.ID, updates)
  2038. mergeAccountExtra(account, updates)
  2039. }
  2040. return s.sendErrorAndEnd(c, fmt.Sprintf("Request failed: %s", err.Error()))
  2041. }
  2042. defer func() { _ = resp.Body.Close() }()
  2043. body, _ := io.ReadAll(io.LimitReader(resp.Body, 2<<20))
  2044. body = redactAgentIdentitySensitiveBodyForAccount(ctx, s.accountRepo, credentialAccount, body)
  2045. if !agentIdentityTaskRecoveryWasTried(ctx) && credentialAccount.IsOpenAIAgentIdentity() && isAgentIdentityTaskInvalidHTTPResponse(resp.StatusCode, body) {
  2046. expectedTaskID := credentialAccount.GetCredential("task_id")
  2047. if err := ensureAgentIdentityTaskForAccount(ctx, s.accountRepo, s.agentIdentityWS, &s.agentIdentityTaskMu, credentialAccount, expectedTaskID); err != nil {
  2048. return s.sendErrorAndEnd(c, fmt.Sprintf("Agent Identity task recovery failed: %s", err.Error()))
  2049. }
  2050. c.Request = c.Request.WithContext(markAgentIdentityTaskRecoveryTried(ctx))
  2051. return s.testOpenAICompactConnection(c, account, testModelID)
  2052. }
  2053. compactionFound := openAICompactProbeFoundCompactionItem(body)
  2054. if s.accountRepo != nil {
  2055. updates := buildOpenAICompactProbeExtraUpdates(resp, body, nil, compactionFound, time.Now())
  2056. if codexUpdates, err := extractOpenAICodexProbeUpdates(resp); err == nil && len(codexUpdates) > 0 {
  2057. updates = mergeExtraUpdates(updates, codexUpdates)
  2058. }
  2059. if len(updates) > 0 {
  2060. _ = s.accountRepo.UpdateExtra(ctx, account.ID, updates)
  2061. mergeAccountExtra(account, updates)
  2062. }
  2063. // 探测如返回 429,主动同步限流状态,避免后续短时间内继续选中。
  2064. if resp.StatusCode == http.StatusTooManyRequests {
  2065. s.reconcileOpenAI429State(ctx, account, resp.Header, body)
  2066. }
  2067. }
  2068. if resp.StatusCode != http.StatusOK {
  2069. if resp.StatusCode == http.StatusUnauthorized && s.accountRepo != nil {
  2070. errMsg := fmt.Sprintf("Authentication failed (401): %s", string(body))
  2071. _ = s.accountRepo.SetError(ctx, account.ID, errMsg)
  2072. }
  2073. return s.sendErrorAndEnd(c, fmt.Sprintf("API returned %d: %s", resp.StatusCode, string(body)))
  2074. }
  2075. if !compactionFound {
  2076. return s.sendErrorAndEnd(c, "Upstream returned 2xx without a compaction output item (native remote compaction v2 unsupported on this chain)")
  2077. }
  2078. s.sendEvent(c, TestEvent{Type: "content", Text: "Compact probe succeeded (native remote compaction v2)"})
  2079. s.sendEvent(c, TestEvent{Type: "test_complete", Success: true})
  2080. return nil
  2081. }
  2082. func (s *AccountTestService) reconcileOpenAI429State(ctx context.Context, account *Account, headers http.Header, body []byte) {
  2083. if s == nil || s.accountRepo == nil || account == nil {
  2084. return
  2085. }
  2086. persistOpenAI429PlanType(ctx, s.accountRepo, account, body)
  2087. var resetAt *time.Time
  2088. if calculated := calculateOpenAI429ResetTime(headers); calculated != nil {
  2089. resetAt = calculated
  2090. } else if unixTs := parseOpenAIRateLimitResetTime(body); unixTs != nil {
  2091. t := time.Unix(*unixTs, 0)
  2092. resetAt = &t
  2093. }
  2094. if resetAt == nil {
  2095. return
  2096. }
  2097. if err := s.accountRepo.SetRateLimited(ctx, account.ID, *resetAt); err != nil {
  2098. return
  2099. }
  2100. now := time.Now()
  2101. account.RateLimitedAt = &now
  2102. account.RateLimitResetAt = resetAt
  2103. if account.Status == StatusError {
  2104. if err := s.accountRepo.ClearError(ctx, account.ID); err != nil {
  2105. return
  2106. }
  2107. account.Status = StatusActive
  2108. account.ErrorMessage = ""
  2109. }
  2110. }
  2111. // testGeminiAccountConnection tests a Gemini account's connection
  2112. func (s *AccountTestService) testGeminiAccountConnection(c *gin.Context, account *Account, modelID string, prompt string) error {
  2113. ctx := c.Request.Context()
  2114. // Determine the model to use
  2115. testModelID := modelID
  2116. if testModelID == "" {
  2117. testModelID = geminicli.DefaultTestModel
  2118. }
  2119. // For static upstream credentials with model mapping, map the model
  2120. if account.Type == AccountTypeAPIKey || account.Type == AccountTypeServiceAccount {
  2121. mapping := account.GetModelMapping()
  2122. if len(mapping) > 0 {
  2123. if mappedModel, exists := mapping[testModelID]; exists {
  2124. testModelID = mappedModel
  2125. }
  2126. }
  2127. }
  2128. // Set SSE headers
  2129. c.Writer.Header().Set("Content-Type", "text/event-stream")
  2130. c.Writer.Header().Set("Cache-Control", "no-cache")
  2131. c.Writer.Header().Set("Connection", "keep-alive")
  2132. c.Writer.Header().Set("X-Accel-Buffering", "no")
  2133. c.Writer.Flush()
  2134. // Create test payload (Gemini format)
  2135. payload := createGeminiTestPayload(testModelID, prompt)
  2136. // Build request based on account type
  2137. var req *http.Request
  2138. var err error
  2139. switch account.Type {
  2140. case AccountTypeAPIKey:
  2141. req, err = s.buildGeminiAPIKeyRequest(ctx, account, testModelID, payload)
  2142. case AccountTypeOAuth:
  2143. req, err = s.buildGeminiOAuthRequest(ctx, account, testModelID, payload)
  2144. case AccountTypeServiceAccount:
  2145. req, err = s.buildGeminiServiceAccountRequest(ctx, account, testModelID, payload)
  2146. default:
  2147. return s.sendErrorAndEnd(c, fmt.Sprintf("Unsupported account type: %s", account.Type))
  2148. }
  2149. if err != nil {
  2150. return s.sendErrorAndEnd(c, fmt.Sprintf("Failed to build request: %s", err.Error()))
  2151. }
  2152. // Send test_start event
  2153. s.sendEvent(c, TestEvent{Type: "test_start", Model: testModelID})
  2154. // Get proxy and execute request
  2155. proxyURL := ""
  2156. if account.ProxyID != nil && account.Proxy != nil {
  2157. proxyURL = account.Proxy.URL()
  2158. }
  2159. resp, err := s.httpUpstream.DoWithTLS(WithAccountTrafficRequest(req, account), proxyURL, account.ID, account.Concurrency, s.tlsFPProfileService.ResolveTLSProfile(account))
  2160. if err != nil {
  2161. return s.sendErrorAndEnd(c, fmt.Sprintf("Request failed: %s", err.Error()))
  2162. }
  2163. defer func() { _ = resp.Body.Close() }()
  2164. if resp.StatusCode != http.StatusOK {
  2165. body, _ := io.ReadAll(resp.Body)
  2166. return s.sendErrorAndEnd(c, fmt.Sprintf("API returned %d: %s", resp.StatusCode, string(body)))
  2167. }
  2168. // Process SSE stream
  2169. return s.processGeminiStream(c, resp.Body)
  2170. }
  2171. // routeAntigravityTest 路由 Antigravity 账号的测试请求。
  2172. // APIKey 类型走原生协议(与 gateway_handler 路由一致),OAuth/Upstream 走 CRS 中转。
  2173. func (s *AccountTestService) routeAntigravityTest(c *gin.Context, account *Account, modelID string, prompt string) error {
  2174. if account.Type == AccountTypeAPIKey {
  2175. if strings.HasPrefix(modelID, "gemini-") {
  2176. return s.testGeminiAccountConnection(c, account, modelID, prompt)
  2177. }
  2178. return s.testClaudeAccountConnection(c, account, modelID)
  2179. }
  2180. return s.testAntigravityAccountConnection(c, account, modelID)
  2181. }
  2182. // testAntigravityAccountConnection tests an Antigravity account's connection
  2183. // 支持 Claude 和 Gemini 两种协议,使用非流式请求
  2184. func (s *AccountTestService) testAntigravityAccountConnection(c *gin.Context, account *Account, modelID string) error {
  2185. ctx := c.Request.Context()
  2186. testModelID := antigravityConnectionTestModel(modelID)
  2187. if s.antigravityGatewayService == nil {
  2188. return s.sendErrorAndEnd(c, "Antigravity gateway service not configured")
  2189. }
  2190. // Set SSE headers
  2191. c.Writer.Header().Set("Content-Type", "text/event-stream")
  2192. c.Writer.Header().Set("Cache-Control", "no-cache")
  2193. c.Writer.Header().Set("Connection", "keep-alive")
  2194. c.Writer.Header().Set("X-Accel-Buffering", "no")
  2195. c.Writer.Flush()
  2196. // Send test_start event
  2197. s.sendEvent(c, TestEvent{Type: "test_start", Model: testModelID})
  2198. // 调用 AntigravityGatewayService.TestConnection(复用协议转换逻辑)
  2199. result, err := s.antigravityGatewayService.TestConnection(ctx, account, testModelID)
  2200. if err != nil {
  2201. return s.sendErrorAndEnd(c, err.Error())
  2202. }
  2203. // 发送响应内容
  2204. if result.Text != "" {
  2205. s.sendEvent(c, TestEvent{Type: "content", Text: result.Text})
  2206. }
  2207. s.sendEvent(c, TestEvent{Type: "test_complete", Success: true})
  2208. return nil
  2209. }
  2210. func antigravityConnectionTestModel(modelID string) string {
  2211. if modelID == "" {
  2212. return defaultAntigravityTestModel
  2213. }
  2214. return modelID
  2215. }
  2216. // buildGeminiAPIKeyRequest builds request for Gemini API Key accounts
  2217. func (s *AccountTestService) buildGeminiAPIKeyRequest(ctx context.Context, account *Account, modelID string, payload []byte) (*http.Request, error) {
  2218. apiKey := account.GetCredential("api_key")
  2219. if strings.TrimSpace(apiKey) == "" {
  2220. return nil, fmt.Errorf("no API key available")
  2221. }
  2222. baseURL := account.GetCredential("base_url")
  2223. if baseURL == "" {
  2224. baseURL = geminicli.AIStudioBaseURL
  2225. }
  2226. normalizedBaseURL, err := s.validateUpstreamBaseURL(baseURL)
  2227. if err != nil {
  2228. return nil, err
  2229. }
  2230. // Use streamGenerateContent for real-time feedback
  2231. fullURL, err := buildGeminiAIStudioModelActionURL(normalizedBaseURL, modelID, "streamGenerateContent", true)
  2232. if err != nil {
  2233. return nil, err
  2234. }
  2235. req, err := http.NewRequestWithContext(ctx, "POST", fullURL, bytes.NewReader(payload))
  2236. if err != nil {
  2237. return nil, err
  2238. }
  2239. req.Header.Set("Content-Type", "application/json")
  2240. req.Header.Set("x-goog-api-key", apiKey)
  2241. return req, nil
  2242. }
  2243. // buildGeminiOAuthRequest builds request for Gemini OAuth accounts
  2244. func (s *AccountTestService) buildGeminiOAuthRequest(ctx context.Context, account *Account, modelID string, payload []byte) (*http.Request, error) {
  2245. if s.geminiTokenProvider == nil {
  2246. return nil, fmt.Errorf("gemini token provider not configured")
  2247. }
  2248. // Get access token (auto-refreshes if needed)
  2249. accessToken, err := s.geminiTokenProvider.GetAccessToken(ctx, account)
  2250. if err != nil {
  2251. return nil, fmt.Errorf("failed to get access token: %w", err)
  2252. }
  2253. projectID := strings.TrimSpace(account.GetCredential("project_id"))
  2254. if projectID == "" {
  2255. // AI Studio OAuth mode (no project_id): call generativelanguage API directly with Bearer token.
  2256. baseURL := account.GetCredential("base_url")
  2257. if strings.TrimSpace(baseURL) == "" {
  2258. baseURL = geminicli.AIStudioBaseURL
  2259. }
  2260. normalizedBaseURL, err := s.validateUpstreamBaseURL(baseURL)
  2261. if err != nil {
  2262. return nil, err
  2263. }
  2264. fullURL, err := buildGeminiAIStudioModelActionURL(normalizedBaseURL, modelID, "streamGenerateContent", true)
  2265. if err != nil {
  2266. return nil, err
  2267. }
  2268. req, err := http.NewRequestWithContext(ctx, http.MethodPost, fullURL, bytes.NewReader(payload))
  2269. if err != nil {
  2270. return nil, err
  2271. }
  2272. req.Header.Set("Content-Type", "application/json")
  2273. req.Header.Set("Authorization", "Bearer "+accessToken)
  2274. return req, nil
  2275. }
  2276. // Code Assist mode (with project_id)
  2277. return s.buildCodeAssistRequest(ctx, accessToken, projectID, modelID, payload)
  2278. }
  2279. func (s *AccountTestService) buildGeminiServiceAccountRequest(ctx context.Context, account *Account, modelID string, payload []byte) (*http.Request, error) {
  2280. if s.geminiTokenProvider == nil {
  2281. return nil, fmt.Errorf("gemini token provider not configured")
  2282. }
  2283. accessToken, err := s.geminiTokenProvider.GetAccessToken(ctx, account)
  2284. if err != nil {
  2285. return nil, fmt.Errorf("failed to get service account access token: %w", err)
  2286. }
  2287. fullURL, err := buildVertexGeminiURL(account.VertexProjectID(), account.VertexLocation(modelID), modelID, "streamGenerateContent", true)
  2288. if err != nil {
  2289. return nil, err
  2290. }
  2291. req, err := http.NewRequestWithContext(ctx, http.MethodPost, fullURL, bytes.NewReader(payload))
  2292. if err != nil {
  2293. return nil, err
  2294. }
  2295. req.Header.Set("Content-Type", "application/json")
  2296. req.Header.Set("Authorization", "Bearer "+accessToken)
  2297. return req, nil
  2298. }
  2299. // buildCodeAssistRequest builds request for Google Code Assist API (used by Gemini CLI and Antigravity)
  2300. func (s *AccountTestService) buildCodeAssistRequest(ctx context.Context, accessToken, projectID, modelID string, payload []byte) (*http.Request, error) {
  2301. var inner map[string]any
  2302. if err := json.Unmarshal(payload, &inner); err != nil {
  2303. return nil, err
  2304. }
  2305. wrapped := map[string]any{
  2306. "model": modelID,
  2307. "project": projectID,
  2308. "request": inner,
  2309. }
  2310. wrappedBytes, _ := json.Marshal(wrapped)
  2311. normalizedBaseURL, err := s.validateUpstreamBaseURL(geminicli.GeminiCliBaseURL)
  2312. if err != nil {
  2313. return nil, err
  2314. }
  2315. fullURL := fmt.Sprintf("%s/v1internal:streamGenerateContent?alt=sse", normalizedBaseURL)
  2316. req, err := http.NewRequestWithContext(ctx, "POST", fullURL, bytes.NewReader(wrappedBytes))
  2317. if err != nil {
  2318. return nil, err
  2319. }
  2320. req.Header.Set("Content-Type", "application/json")
  2321. req.Header.Set("Authorization", "Bearer "+accessToken)
  2322. req.Header.Set("User-Agent", geminicli.GeminiCLIUserAgent)
  2323. return req, nil
  2324. }
  2325. // createGeminiTestPayload creates a minimal test payload for Gemini API.
  2326. // Image models use the image-generation path so the frontend can preview the returned image.
  2327. func createGeminiTestPayload(modelID string, prompt string) []byte {
  2328. if isImageGenerationModel(modelID) {
  2329. imagePrompt := strings.TrimSpace(prompt)
  2330. if imagePrompt == "" {
  2331. imagePrompt = defaultGeminiImageTestPrompt
  2332. }
  2333. payload := map[string]any{
  2334. "contents": []map[string]any{
  2335. {
  2336. "role": "user",
  2337. "parts": []map[string]any{
  2338. {"text": imagePrompt},
  2339. },
  2340. },
  2341. },
  2342. "generationConfig": map[string]any{
  2343. "responseModalities": []string{"TEXT", "IMAGE"},
  2344. "imageConfig": map[string]any{
  2345. "aspectRatio": "1:1",
  2346. },
  2347. },
  2348. }
  2349. bytes, _ := json.Marshal(payload)
  2350. return bytes
  2351. }
  2352. textPrompt := strings.TrimSpace(prompt)
  2353. if textPrompt == "" {
  2354. textPrompt = defaultGeminiTextTestPrompt
  2355. }
  2356. payload := map[string]any{
  2357. "contents": []map[string]any{
  2358. {
  2359. "role": "user",
  2360. "parts": []map[string]any{
  2361. {"text": textPrompt},
  2362. },
  2363. },
  2364. },
  2365. "systemInstruction": map[string]any{
  2366. "parts": []map[string]any{
  2367. {"text": "You are a helpful AI assistant."},
  2368. },
  2369. },
  2370. }
  2371. bytes, _ := json.Marshal(payload)
  2372. return bytes
  2373. }
  2374. // processGeminiStream processes SSE stream from Gemini API
  2375. func (s *AccountTestService) processGeminiStream(c *gin.Context, body io.Reader) error {
  2376. reader := bufio.NewReader(body)
  2377. for {
  2378. line, err := reader.ReadString('\n')
  2379. if err != nil {
  2380. if err == io.EOF {
  2381. s.sendEvent(c, TestEvent{Type: "test_complete", Success: true})
  2382. return nil
  2383. }
  2384. return s.sendErrorAndEnd(c, fmt.Sprintf("Stream read error: %s", err.Error()))
  2385. }
  2386. line = strings.TrimSpace(line)
  2387. if line == "" || !strings.HasPrefix(line, "data: ") {
  2388. continue
  2389. }
  2390. jsonStr := strings.TrimPrefix(line, "data: ")
  2391. if jsonStr == "[DONE]" {
  2392. s.sendEvent(c, TestEvent{Type: "test_complete", Success: true})
  2393. return nil
  2394. }
  2395. var data map[string]any
  2396. if err := json.Unmarshal([]byte(jsonStr), &data); err != nil {
  2397. continue
  2398. }
  2399. // Support two Gemini response formats:
  2400. // - AI Studio: {"candidates": [...]}
  2401. // - Gemini CLI: {"response": {"candidates": [...]}}
  2402. if resp, ok := data["response"].(map[string]any); ok && resp != nil {
  2403. data = resp
  2404. }
  2405. if candidates, ok := data["candidates"].([]any); ok && len(candidates) > 0 {
  2406. if candidate, ok := candidates[0].(map[string]any); ok {
  2407. // Extract content first (before checking completion)
  2408. if content, ok := candidate["content"].(map[string]any); ok {
  2409. if parts, ok := content["parts"].([]any); ok {
  2410. for _, part := range parts {
  2411. if partMap, ok := part.(map[string]any); ok {
  2412. if text, ok := partMap["text"].(string); ok && text != "" {
  2413. s.sendEvent(c, TestEvent{Type: "content", Text: text})
  2414. }
  2415. if inlineData, ok := partMap["inlineData"].(map[string]any); ok {
  2416. mimeType, _ := inlineData["mimeType"].(string)
  2417. data, _ := inlineData["data"].(string)
  2418. if strings.HasPrefix(strings.ToLower(mimeType), "image/") && data != "" {
  2419. s.sendEvent(c, TestEvent{
  2420. Type: "image",
  2421. ImageURL: fmt.Sprintf("data:%s;base64,%s", mimeType, data),
  2422. MimeType: mimeType,
  2423. })
  2424. }
  2425. }
  2426. }
  2427. }
  2428. }
  2429. }
  2430. // Check for completion after extracting content
  2431. if finishReason, ok := candidate["finishReason"].(string); ok && finishReason != "" {
  2432. s.sendEvent(c, TestEvent{Type: "test_complete", Success: true})
  2433. return nil
  2434. }
  2435. }
  2436. }
  2437. // Handle errors
  2438. if errData, ok := data["error"].(map[string]any); ok {
  2439. errorMsg := "Unknown error"
  2440. if msg, ok := errData["message"].(string); ok {
  2441. errorMsg = msg
  2442. }
  2443. return s.sendErrorAndEnd(c, errorMsg)
  2444. }
  2445. }
  2446. }
  2447. // createOpenAITestPayload creates a test payload for OpenAI Responses API
  2448. func createOpenAITestPayload(modelID string, isOAuth bool) map[string]any {
  2449. payload := map[string]any{
  2450. "model": modelID,
  2451. "input": []map[string]any{
  2452. {
  2453. "role": "user",
  2454. "content": []map[string]any{
  2455. {
  2456. "type": "input_text",
  2457. "text": "hi",
  2458. },
  2459. },
  2460. },
  2461. },
  2462. "stream": true,
  2463. }
  2464. // OAuth accounts using ChatGPT internal API require store: false
  2465. if isOAuth {
  2466. payload["store"] = false
  2467. }
  2468. // All accounts require instructions for Responses API
  2469. payload["instructions"] = openai.DefaultInstructions
  2470. return payload
  2471. }
  2472. func createOpenAIChatCompletionsTestPayload(modelID string, prompt string) map[string]any {
  2473. testPrompt := strings.TrimSpace(prompt)
  2474. if testPrompt == "" {
  2475. testPrompt = "hi"
  2476. }
  2477. return map[string]any{
  2478. "model": modelID,
  2479. "messages": []map[string]any{
  2480. {
  2481. "role": "user",
  2482. "content": testPrompt,
  2483. },
  2484. },
  2485. "stream": true,
  2486. }
  2487. }
  2488. // processClaudeStream processes the SSE stream from Claude API
  2489. func (s *AccountTestService) processClaudeStream(c *gin.Context, body io.Reader) error {
  2490. reader := bufio.NewReader(body)
  2491. for {
  2492. line, err := reader.ReadString('\n')
  2493. if err != nil {
  2494. if err == io.EOF {
  2495. s.sendEvent(c, TestEvent{Type: "test_complete", Success: true})
  2496. return nil
  2497. }
  2498. return s.sendErrorAndEnd(c, fmt.Sprintf("Stream read error: %s", err.Error()))
  2499. }
  2500. line = strings.TrimSpace(line)
  2501. if line == "" || !sseDataPrefix.MatchString(line) {
  2502. continue
  2503. }
  2504. jsonStr := sseDataPrefix.ReplaceAllString(line, "")
  2505. if jsonStr == "[DONE]" {
  2506. s.sendEvent(c, TestEvent{Type: "test_complete", Success: true})
  2507. return nil
  2508. }
  2509. var data map[string]any
  2510. if err := json.Unmarshal([]byte(jsonStr), &data); err != nil {
  2511. continue
  2512. }
  2513. eventType, _ := data["type"].(string)
  2514. switch eventType {
  2515. case "content_block_delta":
  2516. if delta, ok := data["delta"].(map[string]any); ok {
  2517. if text, ok := delta["text"].(string); ok {
  2518. s.sendEvent(c, TestEvent{Type: "content", Text: text})
  2519. }
  2520. }
  2521. case "message_stop":
  2522. s.sendEvent(c, TestEvent{Type: "test_complete", Success: true})
  2523. return nil
  2524. case "error":
  2525. errorMsg := "Unknown error"
  2526. if errData, ok := data["error"].(map[string]any); ok {
  2527. if msg, ok := errData["message"].(string); ok {
  2528. errorMsg = msg
  2529. }
  2530. }
  2531. return s.sendErrorAndEnd(c, errorMsg)
  2532. }
  2533. }
  2534. }
  2535. // processOpenAIChatCompletionsStream processes SSE chunks from the
  2536. // OpenAI-compatible Chat Completions API.
  2537. func (s *AccountTestService) processOpenAIChatCompletionsStream(c *gin.Context, body io.Reader) error {
  2538. reader := bufio.NewReader(body)
  2539. seenJSON := false
  2540. seenFinish := false
  2541. for {
  2542. line, err := reader.ReadString('\n')
  2543. if err != nil {
  2544. if err == io.EOF {
  2545. if seenFinish {
  2546. s.sendEvent(c, TestEvent{Type: "status", Text: "已通过 /v1/chat/completions 验证"})
  2547. s.sendEvent(c, TestEvent{Type: "test_complete", Success: true})
  2548. return nil
  2549. }
  2550. if seenJSON {
  2551. return s.sendErrorAndEnd(c, "Chat Completions stream from /v1/chat/completions ended before [DONE]")
  2552. }
  2553. return s.sendErrorAndEnd(c, "Invalid Chat Completions response from /v1/chat/completions: expected SSE JSON data")
  2554. }
  2555. return s.sendErrorAndEnd(c, fmt.Sprintf("Chat Completions stream read error from /v1/chat/completions: %s", err.Error()))
  2556. }
  2557. line = strings.TrimSpace(line)
  2558. if line == "" || !sseDataPrefix.MatchString(line) {
  2559. continue
  2560. }
  2561. jsonStr := sseDataPrefix.ReplaceAllString(line, "")
  2562. if jsonStr == "[DONE]" {
  2563. s.sendEvent(c, TestEvent{Type: "status", Text: "已通过 /v1/chat/completions 验证"})
  2564. s.sendEvent(c, TestEvent{Type: "test_complete", Success: true})
  2565. return nil
  2566. }
  2567. var data map[string]any
  2568. if err := json.Unmarshal([]byte(jsonStr), &data); err != nil {
  2569. return s.sendErrorAndEnd(c, "Invalid Chat Completions response from /v1/chat/completions: expected JSON data")
  2570. }
  2571. seenJSON = true
  2572. if errData, ok := data["error"].(map[string]any); ok {
  2573. errorMsg := "Chat Completions API (/v1/chat/completions) returned an error"
  2574. if msg, ok := errData["message"].(string); ok && msg != "" {
  2575. errorMsg = msg
  2576. }
  2577. return s.sendErrorAndEnd(c, fmt.Sprintf("Chat Completions API (/v1/chat/completions) error: %s", errorMsg))
  2578. }
  2579. choices, ok := data["choices"].([]any)
  2580. if !ok {
  2581. continue
  2582. }
  2583. for _, choiceValue := range choices {
  2584. choice, ok := choiceValue.(map[string]any)
  2585. if !ok {
  2586. continue
  2587. }
  2588. if delta, ok := choice["delta"].(map[string]any); ok {
  2589. if text, ok := delta["content"].(string); ok && text != "" {
  2590. s.sendEvent(c, TestEvent{Type: "content", Text: text})
  2591. }
  2592. }
  2593. if message, ok := choice["message"].(map[string]any); ok {
  2594. if text, ok := message["content"].(string); ok && text != "" {
  2595. s.sendEvent(c, TestEvent{Type: "content", Text: text})
  2596. }
  2597. }
  2598. if finishReason, ok := choice["finish_reason"].(string); ok && finishReason != "" {
  2599. seenFinish = true
  2600. }
  2601. }
  2602. }
  2603. }
  2604. // processOpenAIStream processes the SSE stream from OpenAI Responses API
  2605. func (s *AccountTestService) processOpenAIStream(c *gin.Context, body io.Reader) error {
  2606. reader := bufio.NewReader(body)
  2607. seenCompleted := false
  2608. seenText := false
  2609. for {
  2610. line, err := reader.ReadString('\n')
  2611. if err != nil {
  2612. if err == io.EOF {
  2613. if seenCompleted {
  2614. s.sendEvent(c, TestEvent{Type: "test_complete", Success: true})
  2615. return nil
  2616. }
  2617. return s.sendErrorAndEnd(c, "Stream ended before response.completed")
  2618. }
  2619. return s.sendErrorAndEnd(c, fmt.Sprintf("Stream read error: %s", err.Error()))
  2620. }
  2621. line = strings.TrimSpace(line)
  2622. if line == "" || !sseDataPrefix.MatchString(line) {
  2623. continue
  2624. }
  2625. jsonStr := sseDataPrefix.ReplaceAllString(line, "")
  2626. if jsonStr == "[DONE]" {
  2627. if seenCompleted {
  2628. s.sendEvent(c, TestEvent{Type: "test_complete", Success: true})
  2629. return nil
  2630. }
  2631. return s.sendErrorAndEnd(c, "Stream ended before response.completed")
  2632. }
  2633. var data map[string]any
  2634. if err := json.Unmarshal([]byte(jsonStr), &data); err != nil {
  2635. continue
  2636. }
  2637. eventType, _ := data["type"].(string)
  2638. switch eventType {
  2639. case "response.output_text.delta":
  2640. // OpenAI Responses API uses "delta" field for text content
  2641. if delta, ok := data["delta"].(string); ok && delta != "" {
  2642. seenText = true
  2643. s.sendEvent(c, TestEvent{Type: "content", Text: delta})
  2644. }
  2645. case "response.completed", "response.done":
  2646. if !seenText {
  2647. response, _ := data["response"].(map[string]any)
  2648. if output, ok := response["output"].([]any); ok {
  2649. for _, raw := range output {
  2650. if item, ok := raw.(map[string]any); ok {
  2651. if content, ok := item["content"].([]any); ok {
  2652. for _, part := range content {
  2653. if text, ok := part.(map[string]any); ok && text["type"] == "output_text" {
  2654. if value, ok := text["text"].(string); ok && value != "" {
  2655. s.sendEvent(c, TestEvent{Type: "content", Text: value})
  2656. }
  2657. }
  2658. }
  2659. }
  2660. }
  2661. }
  2662. }
  2663. }
  2664. s.sendEvent(c, TestEvent{Type: "test_complete", Success: true})
  2665. return nil
  2666. case "response.failed":
  2667. errorMsg := "OpenAI response failed"
  2668. if responseData, ok := data["response"].(map[string]any); ok {
  2669. if errData, ok := responseData["error"].(map[string]any); ok {
  2670. if msg, ok := errData["message"].(string); ok && msg != "" {
  2671. errorMsg = msg
  2672. }
  2673. }
  2674. }
  2675. return s.sendErrorAndEnd(c, errorMsg)
  2676. case "error":
  2677. errorMsg := "Unknown error"
  2678. if errData, ok := data["error"].(map[string]any); ok {
  2679. if msg, ok := errData["message"].(string); ok {
  2680. errorMsg = msg
  2681. }
  2682. }
  2683. return s.sendErrorAndEnd(c, errorMsg)
  2684. }
  2685. }
  2686. }
  2687. // testOpenAIImageAPIKey tests OpenAI image generation using an API Key account.
  2688. func (s *AccountTestService) testOpenAIImageAPIKey(c *gin.Context, ctx context.Context, account *Account, modelID, prompt string) error {
  2689. authToken := account.GetOpenAIApiKey()
  2690. if authToken == "" {
  2691. return s.sendErrorAndEnd(c, "No API key available")
  2692. }
  2693. baseURL := account.GetOpenAIBaseURL()
  2694. if baseURL == "" {
  2695. baseURL = "https://api.openai.com"
  2696. }
  2697. normalizedBaseURL, err := s.validateUpstreamBaseURL(baseURL)
  2698. if err != nil {
  2699. return s.sendErrorAndEnd(c, fmt.Sprintf("Invalid base URL: %s", err.Error()))
  2700. }
  2701. apiURL := buildOpenAIImagesURL(normalizedBaseURL, openAIImagesGenerationsEndpoint)
  2702. // Set SSE headers
  2703. c.Writer.Header().Set("Content-Type", "text/event-stream")
  2704. c.Writer.Header().Set("Cache-Control", "no-cache")
  2705. c.Writer.Header().Set("Connection", "keep-alive")
  2706. c.Writer.Header().Set("X-Accel-Buffering", "no")
  2707. c.Writer.Flush()
  2708. s.sendEvent(c, TestEvent{Type: "test_start", Model: modelID})
  2709. payload := map[string]any{
  2710. "model": modelID,
  2711. "prompt": prompt,
  2712. "n": 1,
  2713. "response_format": "b64_json",
  2714. }
  2715. applyIntelligentPayloadPrompt(ctx, payload)
  2716. payloadBytes, _ := json.Marshal(payload)
  2717. req, err := http.NewRequestWithContext(ctx, "POST", apiURL, bytes.NewReader(payloadBytes))
  2718. if err != nil {
  2719. return s.sendErrorAndEnd(c, "Failed to create request")
  2720. }
  2721. req = req.WithContext(WithHTTPUpstreamProfile(req.Context(), HTTPUpstreamProfileOpenAI))
  2722. req.Header.Set("Content-Type", "application/json")
  2723. req.Header.Set("Authorization", "Bearer "+authToken)
  2724. // 账号级请求头覆写:测试请求与真实转发保持一致的最终头
  2725. account.ApplyHeaderOverrides(req.Header)
  2726. proxyURL := ""
  2727. if account.ProxyID != nil && account.Proxy != nil {
  2728. proxyURL = account.Proxy.URL()
  2729. }
  2730. resp, err := s.httpUpstream.DoWithTLS(WithAccountTrafficRequest(req, account), proxyURL, account.ID, account.Concurrency, s.tlsFPProfileService.ResolveTLSProfile(account))
  2731. if err != nil {
  2732. return s.sendErrorAndEnd(c, fmt.Sprintf("Request failed: %s", err.Error()))
  2733. }
  2734. defer func() { _ = resp.Body.Close() }()
  2735. body, err := io.ReadAll(resp.Body)
  2736. if err != nil {
  2737. return s.sendErrorAndEnd(c, fmt.Sprintf("Failed to read response: %s", err.Error()))
  2738. }
  2739. if resp.StatusCode != http.StatusOK {
  2740. return s.sendErrorAndEnd(c, fmt.Sprintf("API returned %d: %s", resp.StatusCode, string(body)))
  2741. }
  2742. // Parse {"data": [{"b64_json": "...", "revised_prompt": "..."}]}
  2743. var result struct {
  2744. Data []struct {
  2745. B64JSON string `json:"b64_json"`
  2746. RevisedPrompt string `json:"revised_prompt"`
  2747. } `json:"data"`
  2748. }
  2749. if err := json.Unmarshal(body, &result); err != nil {
  2750. return s.sendErrorAndEnd(c, fmt.Sprintf("Failed to parse response: %s", err.Error()))
  2751. }
  2752. if len(result.Data) == 0 {
  2753. return s.sendErrorAndEnd(c, "No images returned from API")
  2754. }
  2755. for _, item := range result.Data {
  2756. if item.RevisedPrompt != "" {
  2757. s.sendEvent(c, TestEvent{Type: "content", Text: item.RevisedPrompt})
  2758. }
  2759. if item.B64JSON != "" {
  2760. s.sendEvent(c, TestEvent{
  2761. Type: "image",
  2762. ImageURL: "data:image/png;base64," + item.B64JSON,
  2763. MimeType: "image/png",
  2764. })
  2765. }
  2766. }
  2767. s.sendEvent(c, TestEvent{Type: "test_complete", Success: true})
  2768. return nil
  2769. }
  2770. // testOpenAIImageOAuth tests OpenAI image generation using an OAuth account via Codex /responses API.
  2771. func (s *AccountTestService) testOpenAIImageOAuth(c *gin.Context, ctx context.Context, account *Account, modelID, prompt string) error {
  2772. credentialAccount := account
  2773. if account.IsShadow() {
  2774. resolved, err := resolveCredentialAccount(ctx, s.accountRepo, account)
  2775. if err != nil {
  2776. return s.sendErrorAndEnd(c, "Failed to resolve account credentials")
  2777. }
  2778. credentialAccount = resolved
  2779. }
  2780. authToken := ""
  2781. if !credentialAccount.IsOpenAIAgentIdentity() {
  2782. authToken = credentialAccount.GetOpenAIAccessToken()
  2783. }
  2784. if authToken == "" && !credentialAccount.IsOpenAIAgentIdentity() {
  2785. return s.sendErrorAndEnd(c, "No access token available")
  2786. }
  2787. // Set SSE headers
  2788. c.Writer.Header().Set("Content-Type", "text/event-stream")
  2789. c.Writer.Header().Set("Cache-Control", "no-cache")
  2790. c.Writer.Header().Set("Connection", "keep-alive")
  2791. c.Writer.Header().Set("X-Accel-Buffering", "no")
  2792. c.Writer.Flush()
  2793. s.sendEvent(c, TestEvent{Type: "test_start", Model: modelID})
  2794. s.sendEvent(c, TestEvent{Type: "content", Text: "Calling Codex /responses image tool...\n"})
  2795. parsed := &OpenAIImagesRequest{
  2796. Endpoint: openAIImagesGenerationsEndpoint,
  2797. Model: strings.TrimSpace(modelID),
  2798. Prompt: prompt,
  2799. }
  2800. applyOpenAIImagesDefaults(parsed)
  2801. s.sendEvent(c, TestEvent{Type: "content", Text: fmt.Sprintf("Responses driver: %s; image model: %s\n", openAIImagesResponsesMainModelValue(), parsed.Model)})
  2802. responsesBody, err := buildOpenAIImagesResponsesRequest(parsed, parsed.Model)
  2803. if err != nil {
  2804. return s.sendErrorAndEnd(c, fmt.Sprintf("Failed to build image request: %s", err.Error()))
  2805. }
  2806. req, err := http.NewRequestWithContext(ctx, http.MethodPost, chatgptCodexAPIURL, bytes.NewReader(responsesBody))
  2807. if err != nil {
  2808. return s.sendErrorAndEnd(c, "Failed to create request")
  2809. }
  2810. req = req.WithContext(WithHTTPUpstreamProfile(req.Context(), HTTPUpstreamProfileOpenAI))
  2811. req.Host = "chatgpt.com"
  2812. if credentialAccount.IsOpenAIAgentIdentity() {
  2813. authHeaders, authErr := buildAgentIdentityAuthenticationHeaders(ctx, s.accountRepo, s.agentIdentityWS, &s.agentIdentityTaskMu, credentialAccount)
  2814. if authErr != nil {
  2815. return s.sendErrorAndEnd(c, "Failed to build Agent Identity authentication")
  2816. }
  2817. for key, values := range authHeaders {
  2818. for _, value := range values {
  2819. req.Header.Add(key, value)
  2820. }
  2821. }
  2822. } else {
  2823. req.Header.Set("Authorization", "Bearer "+authToken)
  2824. }
  2825. req.Header.Set("Content-Type", "application/json")
  2826. req.Header.Set("Accept", "text/event-stream")
  2827. req.Header.Set("OpenAI-Beta", "responses=experimental")
  2828. canonical := resolveCodexOutboundIdentity("")
  2829. req.Header.Set("originator", canonical.originator)
  2830. if customUA := strings.TrimSpace(credentialAccount.GetOpenAIUserAgent()); customUA != "" {
  2831. req.Header.Set("User-Agent", customUA)
  2832. } else {
  2833. req.Header.Set("User-Agent", canonical.userAgent)
  2834. }
  2835. setOpenAIChatGPTAccountHeaders(req.Header, credentialAccount)
  2836. // 与真实转发一致:账号级自定义 UA 同样作为管理员显式配置传入,否则测试用的身份
  2837. // 与该账号真实出站的身份不是同一个(issue #3901 的配对不变式由收口保证)。
  2838. enforceCodexIdentityHeadersWithUA(req.Header, credentialAccount.GetOpenAIUserAgent())
  2839. proxyURL := ""
  2840. if account.ProxyID != nil && account.Proxy != nil {
  2841. proxyURL = account.Proxy.URL()
  2842. }
  2843. resp, err := s.doOpenAIAccountTestUpstream(req, proxyURL, account, false)
  2844. if err != nil {
  2845. return s.sendErrorAndEnd(c, fmt.Sprintf("Responses API request failed: %s", err.Error()))
  2846. }
  2847. defer func() {
  2848. if resp != nil && resp.Body != nil {
  2849. _ = resp.Body.Close()
  2850. }
  2851. }()
  2852. if resp.StatusCode >= 400 {
  2853. body, _ := io.ReadAll(io.LimitReader(resp.Body, 2<<20))
  2854. body = redactAgentIdentitySensitiveBodyForAccount(ctx, s.accountRepo, credentialAccount, body)
  2855. message := strings.TrimSpace(extractUpstreamErrorMessage(body))
  2856. if message == "" {
  2857. message = fmt.Sprintf("Responses API returned %d", resp.StatusCode)
  2858. }
  2859. return s.sendErrorAndEnd(c, message)
  2860. }
  2861. body, err := io.ReadAll(resp.Body)
  2862. if err != nil {
  2863. return s.sendErrorAndEnd(c, fmt.Sprintf("Failed to read image response: %s", err.Error()))
  2864. }
  2865. body = redactAgentIdentitySensitiveBodyForAccount(ctx, s.accountRepo, credentialAccount, body)
  2866. results, _, _, _, _, err := collectOpenAIImagesFromResponsesBody(body)
  2867. if err != nil {
  2868. return s.sendErrorAndEnd(c, fmt.Sprintf("Failed to parse image response: %s", err.Error()))
  2869. }
  2870. if len(results) == 0 {
  2871. if upstreamErr := extractOpenAIImagesUpstreamError(body); upstreamErr != nil {
  2872. return s.sendErrorAndEnd(c, upstreamErr.clientMessage())
  2873. }
  2874. if textErr := openAIImagesTextFallbackError(body); textErr != nil {
  2875. return s.sendErrorAndEnd(c, textErr.clientMessage())
  2876. }
  2877. return s.sendErrorAndEnd(c, "No images returned from responses API")
  2878. }
  2879. for _, item := range results {
  2880. if item.RevisedPrompt != "" {
  2881. s.sendEvent(c, TestEvent{Type: "content", Text: item.RevisedPrompt})
  2882. }
  2883. mimeType := openAIImageOutputMIMEType(item.OutputFormat)
  2884. s.sendEvent(c, TestEvent{
  2885. Type: "image",
  2886. ImageURL: "data:" + mimeType + ";base64," + item.Result,
  2887. MimeType: mimeType,
  2888. })
  2889. }
  2890. s.sendEvent(c, TestEvent{Type: "test_complete", Success: true})
  2891. return nil
  2892. }
  2893. func (s *AccountTestService) sendEvent(c *gin.Context, event TestEvent) {
  2894. if event.Type == "test_complete" {
  2895. if suppress, ok := c.Get(accountTestSuppressCompletionContextKey); ok {
  2896. if suppressCompletion, _ := suppress.(bool); suppressCompletion {
  2897. return
  2898. }
  2899. }
  2900. }
  2901. eventJSON, _ := json.Marshal(event)
  2902. if _, err := fmt.Fprintf(c.Writer, "data: %s\n\n", eventJSON); err != nil {
  2903. log.Printf("failed to write SSE event: %v", err)
  2904. return
  2905. }
  2906. c.Writer.Flush()
  2907. }
  2908. // sendErrorAndEnd sends an error event and ends the stream
  2909. func (s *AccountTestService) sendErrorAndEnd(c *gin.Context, errorMsg string) error {
  2910. if c.Request != nil {
  2911. if run := intelligentContext(c.Request.Context()); run != nil {
  2912. errorMsg = run.capture.redact(errorMsg)
  2913. }
  2914. }
  2915. log.Printf("Account test error: %s", errorMsg)
  2916. s.sendEvent(c, TestEvent{Type: "error", Error: errorMsg})
  2917. return fmt.Errorf("%s", errorMsg)
  2918. }
  2919. // RunTestBackground executes an account test in-memory (no real HTTP client),
  2920. // capturing SSE output via httptest.NewRecorder, then parses the result.
  2921. func (s *AccountTestService) RunTestBackground(ctx context.Context, accountID int64, modelID string) (*ScheduledTestResult, error) {
  2922. startedAt := time.Now()
  2923. w := httptest.NewRecorder()
  2924. ginCtx, _ := gin.CreateTestContext(w)
  2925. ginCtx.Request = (&http.Request{}).WithContext(ctx)
  2926. testErr := s.TestAccountConnection(ginCtx, accountID, modelID, "", AccountTestModeDefault)
  2927. finishedAt := time.Now()
  2928. body := w.Body.String()
  2929. responseText, errMsg := parseTestSSEOutput(body)
  2930. status := "success"
  2931. if testErr != nil || errMsg != "" {
  2932. status = "failed"
  2933. if errMsg == "" && testErr != nil {
  2934. errMsg = testErr.Error()
  2935. }
  2936. }
  2937. return &ScheduledTestResult{
  2938. Status: status,
  2939. ResponseText: responseText,
  2940. ErrorMessage: errMsg,
  2941. LatencyMs: finishedAt.Sub(startedAt).Milliseconds(),
  2942. StartedAt: startedAt,
  2943. FinishedAt: finishedAt,
  2944. }, nil
  2945. }
  2946. // parseTestSSEOutput extracts response text and error message from captured SSE output.
  2947. func parseTestSSEOutput(body string) (responseText, errMsg string) {
  2948. var texts []string
  2949. for _, line := range strings.Split(body, "\n") {
  2950. line = strings.TrimSpace(line)
  2951. if !strings.HasPrefix(line, "data: ") {
  2952. continue
  2953. }
  2954. jsonStr := strings.TrimPrefix(line, "data: ")
  2955. var event TestEvent
  2956. if err := json.Unmarshal([]byte(jsonStr), &event); err != nil {
  2957. continue
  2958. }
  2959. switch event.Type {
  2960. case "content":
  2961. if event.Text != "" {
  2962. texts = append(texts, event.Text)
  2963. }
  2964. case "error":
  2965. errMsg = event.Error
  2966. }
  2967. }
  2968. responseText = strings.Join(texts, "")
  2969. return
  2970. }