responses_client_tools.go 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883
  1. package apicompat
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "fmt"
  6. "strings"
  7. )
  8. // ResponsesClientToolMapping records the reversible lowering applied before a
  9. // native Responses request is sent to an upstream that only understands
  10. // function tools.
  11. type ResponsesClientToolMapping struct {
  12. CustomTools map[string]bool
  13. ToolSearch bool
  14. NamespaceTools map[string]ResponsesNamespaceName
  15. }
  16. // AdaptResponsesClientTools lowers Codex client-only tools in req to
  17. // ordinary function tools. It mutates req and returns the mapping required to
  18. // restore the upstream response.
  19. func AdaptResponsesClientTools(req map[string]any) (ResponsesClientToolMapping, bool, error) {
  20. if req == nil {
  21. return ResponsesClientToolMapping{}, false, nil
  22. }
  23. tools, ok := req["tools"].([]any)
  24. if !ok || len(tools) == 0 {
  25. return ResponsesClientToolMapping{}, false, nil
  26. }
  27. discovered, err := promoteResponsesToolSearchDiscoveries(req)
  28. if err != nil {
  29. return ResponsesClientToolMapping{}, false, err
  30. }
  31. if discovered {
  32. tools, _ = req["tools"].([]any)
  33. }
  34. adapter := ResponsesClientToolMapping{CustomTools: make(map[string]bool)}
  35. functionNames := make(map[string]bool)
  36. customNames := make(map[string]bool)
  37. for _, raw := range tools {
  38. tool, ok := raw.(map[string]any)
  39. if !ok {
  40. continue
  41. }
  42. name := strings.TrimSpace(stringValue(tool["name"]))
  43. switch strings.TrimSpace(stringValue(tool["type"])) {
  44. case "function":
  45. if name != "" {
  46. functionNames[name] = true
  47. }
  48. case "custom":
  49. if name != "" {
  50. customNames[name] = true
  51. }
  52. case "tool_search":
  53. adapter.ToolSearch = true
  54. }
  55. }
  56. for name := range customNames {
  57. if functionNames[name] {
  58. return ResponsesClientToolMapping{}, false, fmt.Errorf("custom tool %q conflicts with a function tool of the same name; this upstream cannot disambiguate them, rename one of the tools", name)
  59. }
  60. }
  61. if adapter.ToolSearch && (functionNames[toolSearchProxyName] || customNames[toolSearchProxyName]) {
  62. return ResponsesClientToolMapping{}, false, fmt.Errorf("built-in tool_search conflicts with a declared tool named %q; this upstream cannot disambiguate them, rename the tool", toolSearchProxyName)
  63. }
  64. // Namespace flattening also rewrites namespace-qualified history and choice.
  65. names, flattened, err := FlattenResponsesNamespaces(req)
  66. if err != nil {
  67. return ResponsesClientToolMapping{}, false, err
  68. }
  69. adapter.NamespaceTools = names
  70. if adapter.ToolSearch {
  71. if _, exists := names[toolSearchProxyName]; exists {
  72. return ResponsesClientToolMapping{}, false, fmt.Errorf("built-in tool_search conflicts with namespace tool flattened as %q; this upstream cannot disambiguate them, rename the tool", toolSearchProxyName)
  73. }
  74. }
  75. tools, _ = req["tools"].([]any)
  76. lowered := make([]any, 0, len(tools))
  77. changed := discovered || flattened
  78. seenSearch := false
  79. for _, raw := range tools {
  80. tool, ok := raw.(map[string]any)
  81. if !ok {
  82. lowered = append(lowered, raw)
  83. continue
  84. }
  85. typ := strings.TrimSpace(stringValue(tool["type"]))
  86. name := strings.TrimSpace(stringValue(tool["name"]))
  87. switch typ {
  88. case "custom":
  89. if name == "" {
  90. lowered = append(lowered, raw)
  91. continue
  92. }
  93. copy := copyClientTool(tool)
  94. copy["type"] = "function"
  95. copy["parameters"] = json.RawMessage(customToolInputSchema)
  96. delete(copy, "format")
  97. adapter.CustomTools[name] = true
  98. lowered = append(lowered, copy)
  99. changed = true
  100. case "tool_search":
  101. if seenSearch {
  102. changed = true
  103. continue
  104. }
  105. seenSearch = true
  106. lowered = append(lowered, map[string]any{
  107. "type": "function", "name": toolSearchProxyName,
  108. "description": "Search and load Codex tools, plugins, connectors, and MCP namespaces for the current task.",
  109. "parameters": json.RawMessage(toolSearchProxySchema),
  110. })
  111. changed = true
  112. default:
  113. lowered = append(lowered, raw)
  114. }
  115. }
  116. if stripResponsesDeferredToolFlags(lowered) {
  117. changed = true
  118. }
  119. if changed {
  120. req["tools"] = lowered
  121. }
  122. historyChanged, err := rewriteClientToolHistory(req["input"], &adapter)
  123. if err != nil {
  124. return ResponsesClientToolMapping{}, false, err
  125. }
  126. if historyChanged {
  127. changed = true
  128. }
  129. if rewriteClientToolChoice(req, &adapter) {
  130. changed = true
  131. }
  132. if len(adapter.CustomTools) == 0 {
  133. adapter.CustomTools = nil
  134. }
  135. if len(adapter.NamespaceTools) == 0 {
  136. adapter.NamespaceTools = nil
  137. }
  138. return adapter, changed, nil
  139. }
  140. // stripResponsesDeferredToolFlags removes defer_loading only when the final
  141. // declaration list no longer contains the built-in tool_search it requires.
  142. func stripResponsesDeferredToolFlags(tools []any) bool {
  143. if hasResponsesToolSearchDeclaration(tools) {
  144. return false
  145. }
  146. changed := false
  147. for _, raw := range tools {
  148. tool, ok := raw.(map[string]any)
  149. if !ok {
  150. continue
  151. }
  152. if _, exists := tool["defer_loading"]; exists {
  153. delete(tool, "defer_loading")
  154. changed = true
  155. }
  156. }
  157. return changed
  158. }
  159. // AdaptResponsesClientToolsWithInheritedMapping lowers client-tool history on
  160. // a follow-up request that omits the session-level tools declaration. An
  161. // explicitly present tools field, including an empty or malformed value,
  162. // always replaces the inherited mapping and is handled by the ordinary
  163. // declaration-driven adapter.
  164. func AdaptResponsesClientToolsWithInheritedMapping(
  165. req map[string]any,
  166. inherited ResponsesClientToolMapping,
  167. inheritedLoweredTools ...[]any,
  168. ) (ResponsesClientToolMapping, bool, error) {
  169. if req == nil {
  170. return ResponsesClientToolMapping{}, false, nil
  171. }
  172. if _, toolsPresent := req["tools"]; toolsPresent {
  173. return AdaptResponsesClientTools(req)
  174. }
  175. if len(inherited.CustomTools) == 0 && !inherited.ToolSearch && len(inherited.NamespaceTools) == 0 {
  176. return ResponsesClientToolMapping{}, false, nil
  177. }
  178. if len(inheritedLoweredTools) > 0 && len(inheritedLoweredTools[0]) > 0 {
  179. req["tools"] = restoreInheritedResponsesClientToolDeclarations(inheritedLoweredTools[0], inherited)
  180. return AdaptResponsesClientTools(req)
  181. }
  182. changed, err := rewriteClientToolHistory(req["input"], &inherited)
  183. if err != nil {
  184. return ResponsesClientToolMapping{}, false, err
  185. }
  186. if len(inherited.NamespaceTools) > 0 {
  187. before := changed
  188. rewriteNamespaceQualifiedCalls(req["input"], inherited.NamespaceTools)
  189. // Namespace rewriting does not currently report whether it changed a
  190. // value. A retained namespace mapping is only used for follow-up
  191. // history, so conservatively rebuild the request when input exists.
  192. if _, inputPresent := req["input"]; inputPresent && !before {
  193. changed = true
  194. }
  195. }
  196. if rewriteClientToolChoice(req, &inherited) {
  197. changed = true
  198. }
  199. return inherited, changed, nil
  200. }
  201. func copyClientTool(tool map[string]any) map[string]any {
  202. copy := make(map[string]any, len(tool))
  203. for key, value := range tool {
  204. copy[key] = value
  205. }
  206. return copy
  207. }
  208. func rewriteClientToolHistory(value any, adapter *ResponsesClientToolMapping) (bool, error) {
  209. changed := false
  210. var visit func(any) error
  211. visit = func(value any) error {
  212. switch typed := value.(type) {
  213. case []any:
  214. for _, item := range typed {
  215. if err := visit(item); err != nil {
  216. return err
  217. }
  218. }
  219. case map[string]any:
  220. typ := strings.TrimSpace(stringValue(typed["type"]))
  221. switch typ {
  222. case "custom_tool_call":
  223. if adapter.CustomTools[strings.TrimSpace(stringValue(typed["name"]))] {
  224. typed["type"] = "function_call"
  225. typed["arguments"] = customToolCallArguments(stringValue(typed["input"]))
  226. delete(typed, "input")
  227. normalizeLoweredFunctionItemID(typed)
  228. changed = true
  229. }
  230. case "custom_tool_call_output":
  231. typed["type"] = "function_call_output"
  232. normalizeLoweredFunctionItemID(typed)
  233. normalizeClientToolOutput(typed)
  234. changed = true
  235. case "tool_search_call":
  236. if adapter.ToolSearch {
  237. typed["type"] = "function_call"
  238. typed["name"] = toolSearchProxyName
  239. typed["arguments"] = rawObjectString(typed["arguments"])
  240. delete(typed, "execution")
  241. normalizeLoweredFunctionItemID(typed)
  242. changed = true
  243. }
  244. case "tool_search_output":
  245. if adapter.ToolSearch {
  246. callID := strings.TrimSpace(stringValue(typed["call_id"]))
  247. if callID == "" {
  248. return fmt.Errorf("tool_search_output requires a non-empty string call_id before it can be lowered to function_call_output")
  249. }
  250. typed["type"] = "function_call_output"
  251. normalizeLoweredFunctionItemID(typed)
  252. if err := normalizeToolSearchOutput(typed); err != nil {
  253. return err
  254. }
  255. changed = true
  256. }
  257. }
  258. for _, child := range typed {
  259. if err := visit(child); err != nil {
  260. return err
  261. }
  262. }
  263. }
  264. return nil
  265. }
  266. if err := visit(value); err != nil {
  267. return false, err
  268. }
  269. return changed, nil
  270. }
  271. // normalizeLoweredFunctionItemID reconciles Codex client-only item IDs such as
  272. // ctc_*, ctco_*, tsc_*, and tso_* after their item type is lowered to the
  273. // function protocol, which validates these IDs with the fc prefix. A ctc_/tsc_
  274. // call ID maps back to the fc_ ID it was raised from; the remaining IDs have no
  275. // function-protocol counterpart and are dropped. call_id, which is preserved
  276. // separately, stays the tool call/output pairing key either way.
  277. func normalizeLoweredFunctionItemID(item map[string]any) {
  278. id := strings.TrimSpace(stringValue(item["id"]))
  279. if id == "" || strings.HasPrefix(id, "fc") {
  280. return
  281. }
  282. // A ctc_/tsc_ call ID on a lowered call item is the one we minted from the
  283. // upstream's own fc_ ID when the item was raised, so map it back instead of
  284. // dropping it. Anything else has no known function-protocol counterpart.
  285. if recovered := retypedResponsesToolCallItemID(id, "function_call"); recovered != id {
  286. item["id"] = recovered
  287. return
  288. }
  289. delete(item, "id")
  290. }
  291. // responsesToolCallItemIDPrefixes lists the Responses item ID prefixes that are
  292. // tied to a specific tool-call item type.
  293. var responsesToolCallItemIDPrefixes = []string{"fc_", "ctc_", "tsc_"}
  294. // responsesToolCallItemIDPrefix reports the ID prefix the Responses API
  295. // validates for itemType, or "" when the type constrains no prefix.
  296. func responsesToolCallItemIDPrefix(itemType string) string {
  297. switch itemType {
  298. case "custom_tool_call":
  299. return "ctc_"
  300. case "tool_search_call":
  301. return "tsc_"
  302. case "function_call":
  303. return "fc_"
  304. default:
  305. return ""
  306. }
  307. }
  308. // retypedResponsesToolCallItemID re-prefixes an upstream item ID so it agrees
  309. // with the item type we raise it back to. A function-only upstream answers a
  310. // lowered custom tool with an fc_ item ID; emitting that ID on the restored
  311. // custom_tool_call poisons the client's history, because a later replay of the
  312. // same item to an upstream that validates IDs fails with
  313. // "Invalid 'input[N].id' ... Expected an ID that begins with 'ctc'".
  314. // The suffix is preserved so the ID stays stable and unique per upstream item.
  315. // IDs that carry no known tool-call prefix are left alone rather than guessed at.
  316. func retypedResponsesToolCallItemID(id, itemType string) string {
  317. want := responsesToolCallItemIDPrefix(itemType)
  318. if want == "" || id == "" || strings.HasPrefix(id, want) {
  319. return id
  320. }
  321. for _, known := range responsesToolCallItemIDPrefixes {
  322. if known != want && strings.HasPrefix(id, known) {
  323. return want + strings.TrimPrefix(id, known)
  324. }
  325. }
  326. return id
  327. }
  328. // retypeResponsesToolCallItemID applies retypedResponsesToolCallItemID to a
  329. // decoded item map.
  330. func retypeResponsesToolCallItemID(item map[string]any, itemType string) {
  331. id := strings.TrimSpace(stringValue(item["id"]))
  332. if retyped := retypedResponsesToolCallItemID(id, itemType); retyped != id {
  333. item["id"] = retyped
  334. }
  335. }
  336. func normalizeClientToolOutput(item map[string]any) {
  337. output, exists := item["output"]
  338. if !exists {
  339. return
  340. }
  341. if _, ok := output.(string); ok {
  342. return
  343. }
  344. if isResponsesToolOutputContent(output) {
  345. return
  346. }
  347. if output == nil {
  348. item["output"] = ""
  349. return
  350. }
  351. encoded, err := json.Marshal(output)
  352. if err != nil {
  353. item["output"] = ""
  354. return
  355. }
  356. item["output"] = string(encoded)
  357. }
  358. func isResponsesToolOutputContent(output any) bool {
  359. parts, ok := output.([]any)
  360. if !ok || len(parts) == 0 {
  361. return false
  362. }
  363. for _, part := range parts {
  364. typed, ok := part.(map[string]any)
  365. if !ok {
  366. return false
  367. }
  368. switch stringValue(typed["type"]) {
  369. case "input_text", "input_image", "input_file":
  370. default:
  371. return false
  372. }
  373. }
  374. return true
  375. }
  376. // normalizeToolSearchOutput converts both tool_search output wire shapes into
  377. // the string output required by function_call_output. Older clients send an
  378. // output field directly; newer Codex clients return discovered definitions in
  379. // a top-level tools field. Codex treats that field's value as the tool output,
  380. // so serialize the value directly rather than wrapping it in another object.
  381. func normalizeToolSearchOutput(item map[string]any) error {
  382. if output, hasOutput := item["output"]; hasOutput {
  383. switch typed := output.(type) {
  384. case string:
  385. item["output"] = typed
  386. case nil:
  387. item["output"] = ""
  388. default:
  389. encoded, err := json.Marshal(typed)
  390. if err != nil {
  391. return fmt.Errorf("tool_search_output output cannot be encoded as function_call_output output: %w", err)
  392. }
  393. item["output"] = string(encoded)
  394. }
  395. dropToolSearchOutputPrivateFields(item)
  396. return nil
  397. }
  398. tools, hasTools := item["tools"]
  399. if !hasTools {
  400. return fmt.Errorf("tool_search_output requires output or tools before it can be lowered to function_call_output")
  401. }
  402. encoded, err := json.Marshal(tools)
  403. if err != nil {
  404. return fmt.Errorf("tool_search_output tools cannot be encoded as function_call_output output: %w", err)
  405. }
  406. item["output"] = string(encoded)
  407. dropToolSearchOutputPrivateFields(item)
  408. return nil
  409. }
  410. func dropToolSearchOutputPrivateFields(item map[string]any) {
  411. delete(item, "tools")
  412. delete(item, "status")
  413. delete(item, "execution")
  414. }
  415. func rewriteClientToolChoice(req map[string]any, adapter *ResponsesClientToolMapping) bool {
  416. choice, ok := req["tool_choice"].(map[string]any)
  417. if !ok {
  418. return false
  419. }
  420. typ := strings.TrimSpace(stringValue(choice["type"]))
  421. name := strings.TrimSpace(stringValue(choice["name"]))
  422. if typ == "custom" && adapter.CustomTools[name] {
  423. choice["type"] = "function"
  424. return true
  425. }
  426. if typ == "tool_search" && adapter.ToolSearch {
  427. req["tool_choice"] = map[string]any{"type": "function", "name": toolSearchProxyName}
  428. return true
  429. }
  430. return false
  431. }
  432. func customToolCallArguments(input string) string {
  433. encoded, _ := json.Marshal(map[string]string{"input": input})
  434. return string(encoded)
  435. }
  436. func rawObjectString(value any) string {
  437. if text, ok := value.(string); ok {
  438. return text
  439. }
  440. encoded, err := json.Marshal(value)
  441. if err != nil {
  442. return "{}"
  443. }
  444. return string(encoded)
  445. }
  446. // RestoreResponsesClientToolPayload restores client tool calls in a non-stream
  447. // native Responses JSON payload.
  448. func RestoreResponsesClientToolPayload(payload []byte, mapping ResponsesClientToolMapping) ([]byte, bool, error) {
  449. if len(payload) == 0 {
  450. return payload, false, nil
  451. }
  452. var value any
  453. if err := json.Unmarshal(payload, &value); err != nil {
  454. return payload, false, err
  455. }
  456. changed := restoreClientToolValue(value, &mapping)
  457. if !changed {
  458. if len(mapping.NamespaceTools) == 0 {
  459. return payload, false, nil
  460. }
  461. return RestoreResponsesNamespaceCalls(payload, mapping.NamespaceTools)
  462. }
  463. var rebuilt bytes.Buffer
  464. encoder := json.NewEncoder(&rebuilt)
  465. encoder.SetEscapeHTML(false)
  466. if err := encoder.Encode(value); err != nil {
  467. return payload, false, err
  468. }
  469. rebuiltPayload := bytes.TrimSuffix(rebuilt.Bytes(), []byte("\n"))
  470. if len(mapping.NamespaceTools) == 0 {
  471. return rebuiltPayload, true, nil
  472. }
  473. restored, _, err := RestoreResponsesNamespaceCalls(rebuiltPayload, mapping.NamespaceTools)
  474. if err != nil {
  475. return payload, false, err
  476. }
  477. return restored, true, nil
  478. }
  479. func restoreClientToolValue(value any, adapter *ResponsesClientToolMapping) bool {
  480. changed := false
  481. switch typed := value.(type) {
  482. case []any:
  483. for _, item := range typed {
  484. changed = restoreClientToolValue(item, adapter) || changed
  485. }
  486. case map[string]any:
  487. if strings.TrimSpace(stringValue(typed["type"])) == "function_call" {
  488. name := strings.TrimSpace(stringValue(typed["name"]))
  489. if adapter.CustomTools[name] {
  490. typed["type"] = "custom_tool_call"
  491. retypeResponsesToolCallItemID(typed, "custom_tool_call")
  492. typed["input"] = extractCustomToolCallInput(rawObjectString(typed["arguments"]))
  493. delete(typed, "arguments")
  494. delete(typed, "namespace")
  495. changed = true
  496. } else if adapter.ToolSearch && name == toolSearchProxyName {
  497. typed["type"] = "tool_search_call"
  498. retypeResponsesToolCallItemID(typed, "tool_search_call")
  499. typed["execution"] = "client"
  500. typed["arguments"] = json.RawMessage(toolSearchCallArgumentsJSON(rawObjectString(typed["arguments"])))
  501. delete(typed, "name")
  502. delete(typed, "namespace")
  503. changed = true
  504. }
  505. }
  506. for _, child := range typed {
  507. changed = restoreClientToolValue(child, adapter) || changed
  508. }
  509. }
  510. return changed
  511. }
  512. // ResponsesClientToolStreamRestorer restores client tool stream lifecycles.
  513. // It is intentionally stateful because custom tools need their function
  514. // arguments buffered until the upstream signals the call is complete.
  515. type ResponsesClientToolStreamRestorer struct {
  516. adapter ResponsesClientToolMapping
  517. nextSeq int
  518. seenSeq bool
  519. calls map[string]*responsesClientToolStreamCall
  520. byOutput map[int]*responsesClientToolStreamCall
  521. }
  522. type responsesClientToolStreamCall struct {
  523. kind string
  524. name string
  525. // callID and itemID stay as the upstream sent them so later upstream
  526. // events keep matching this call; clientItemID is what we emit.
  527. callID string
  528. itemID string
  529. clientItemID string
  530. outputIdx int
  531. arguments strings.Builder
  532. }
  533. func NewResponsesClientToolStreamRestorer(mapping ResponsesClientToolMapping) *ResponsesClientToolStreamRestorer {
  534. return &ResponsesClientToolStreamRestorer{adapter: mapping, calls: make(map[string]*responsesClientToolStreamCall), byOutput: make(map[int]*responsesClientToolStreamCall)}
  535. }
  536. // Restore transforms one upstream SSE event into zero or more client events.
  537. // Returned sequence numbers are continuous even when function argument events
  538. // are suppressed or a custom completion expands into two events.
  539. func (r *ResponsesClientToolStreamRestorer) Restore(event ResponsesStreamEvent) []ResponsesStreamEvent {
  540. if r == nil {
  541. return []ResponsesStreamEvent{event}
  542. }
  543. if !r.seenSeq {
  544. r.nextSeq = event.SequenceNumber
  545. r.seenSeq = true
  546. }
  547. var out []ResponsesStreamEvent
  548. emit := func(event ResponsesStreamEvent) {
  549. event.SequenceNumber = r.nextSeq
  550. r.nextSeq++
  551. out = append(out, event)
  552. }
  553. switch event.Type {
  554. case "response.output_item.added":
  555. if call := r.recordItem(event); call != nil {
  556. if call.kind == "custom" {
  557. event.Item.Type = "custom_tool_call"
  558. event.Item.Input = ""
  559. event.Item.Arguments = ""
  560. event.Item.Namespace = ""
  561. } else {
  562. event.Item.Type = "tool_search_call"
  563. event.Item.Name = ""
  564. event.Item.Arguments = "{}"
  565. event.Item.Namespace = ""
  566. }
  567. if call.clientItemID != "" {
  568. event.Item.ID = call.clientItemID
  569. }
  570. }
  571. emit(r.restoreNamespaceEvent(event))
  572. case "response.function_call_arguments.delta":
  573. if call := r.callFor(event); call != nil {
  574. _, _ = call.arguments.WriteString(event.Delta)
  575. return nil
  576. }
  577. emit(r.restoreNamespaceEvent(event))
  578. case "response.function_call_arguments.done":
  579. if call := r.callFor(event); call != nil {
  580. if event.Arguments != "" {
  581. call.arguments.Reset()
  582. _, _ = call.arguments.WriteString(event.Arguments)
  583. }
  584. if call.kind == "custom" {
  585. input := extractCustomToolCallInput(call.arguments.String())
  586. if input != "" {
  587. emit(ResponsesStreamEvent{Type: "response.custom_tool_call_input.delta", OutputIndex: call.outputIdx, ItemID: call.clientItemID, Delta: input})
  588. }
  589. emit(ResponsesStreamEvent{Type: "response.custom_tool_call_input.done", OutputIndex: call.outputIdx, ItemID: call.clientItemID, CallID: call.callID, Name: call.name, Input: input})
  590. }
  591. return out
  592. }
  593. emit(r.restoreNamespaceEvent(event))
  594. case "response.output_item.done":
  595. if call := r.recordItem(event); call != nil {
  596. if call.kind == "custom" {
  597. event.Item.Type = "custom_tool_call"
  598. event.Item.Input = extractCustomToolCallInput(call.arguments.String())
  599. event.Item.Arguments = ""
  600. event.Item.Namespace = ""
  601. } else {
  602. event.Item.Type = "tool_search_call"
  603. event.Item.Name = ""
  604. event.Item.Arguments = call.arguments.String()
  605. if strings.TrimSpace(event.Item.Arguments) == "" {
  606. event.Item.Arguments = "{}"
  607. }
  608. event.Item.Namespace = ""
  609. }
  610. if call.clientItemID != "" {
  611. event.Item.ID = call.clientItemID
  612. }
  613. delete(r.calls, call.itemID)
  614. delete(r.calls, call.callID)
  615. delete(r.byOutput, call.outputIdx)
  616. }
  617. emit(r.restoreNamespaceEvent(event))
  618. default:
  619. // response.completed carries the non-stream representation.
  620. if event.Response != nil {
  621. restoreResponsesOutputClientTools(event.Response.Output, &r.adapter)
  622. }
  623. emit(r.restoreNamespaceEvent(event))
  624. }
  625. return out
  626. }
  627. // RestoreEvent restores one Responses SSE JSON data payload. Custom tool
  628. // completions can expand to multiple payloads and proxy argument deltas can be
  629. // intentionally dropped, hence the slice return value.
  630. func (r *ResponsesClientToolStreamRestorer) RestoreEvent(payload []byte) ([][]byte, bool, error) {
  631. if len(payload) == 0 {
  632. return nil, false, nil
  633. }
  634. var wire struct {
  635. Type string `json:"type"`
  636. Sequence int `json:"sequence_number"`
  637. }
  638. if err := json.Unmarshal(payload, &wire); err != nil {
  639. return nil, false, err
  640. }
  641. if isResponsesClientToolTerminalEvent(wire.Type) {
  642. restored, changed, err := RestoreResponsesClientToolPayload(payload, r.adapter)
  643. if err != nil {
  644. return nil, false, err
  645. }
  646. return r.resequenceRaw(restored, wire.Sequence, changed)
  647. }
  648. if !clientToolLifecycleEvent(wire.Type) {
  649. return r.resequenceRaw(payload, wire.Sequence, false)
  650. }
  651. if !r.clientToolEventPayload(payload) {
  652. return r.resequenceRaw(payload, wire.Sequence, false)
  653. }
  654. var event ResponsesStreamEvent
  655. if err := json.Unmarshal(payload, &event); err != nil {
  656. return nil, false, err
  657. }
  658. events := r.Restore(event)
  659. if len(events) == 1 {
  660. unchanged, err := json.Marshal(events[0])
  661. if err == nil && bytes.Equal(bytes.TrimSpace(unchanged), bytes.TrimSpace(payload)) {
  662. return [][]byte{payload}, false, nil
  663. }
  664. }
  665. result := make([][]byte, 0, len(events))
  666. for _, restored := range events {
  667. encoded, err := json.Marshal(restored)
  668. if err != nil {
  669. return nil, false, err
  670. }
  671. result = append(result, encoded)
  672. }
  673. return result, true, nil
  674. }
  675. func isResponsesClientToolTerminalEvent(typ string) bool {
  676. switch strings.TrimSpace(typ) {
  677. case "response.completed", "response.done", "response.incomplete", "response.failed", "response.cancelled", "response.canceled":
  678. return true
  679. default:
  680. return false
  681. }
  682. }
  683. func (r *ResponsesClientToolStreamRestorer) clientToolEventPayload(payload []byte) bool {
  684. var raw struct {
  685. ItemID string `json:"item_id"`
  686. CallID string `json:"call_id"`
  687. Name string `json:"name"`
  688. OutputIndex int `json:"output_index"`
  689. Item *struct {
  690. Type string `json:"type"`
  691. ID string `json:"id"`
  692. CallID string `json:"call_id"`
  693. Name string `json:"name"`
  694. } `json:"item"`
  695. }
  696. if err := json.Unmarshal(payload, &raw); err != nil {
  697. return false
  698. }
  699. if raw.Item != nil {
  700. if raw.Item.Type != "function_call" {
  701. return false
  702. }
  703. _, namespaceTool := r.adapter.NamespaceTools[raw.Item.Name]
  704. return r.adapter.CustomTools[raw.Item.Name] || (r.adapter.ToolSearch && raw.Item.Name == toolSearchProxyName) || namespaceTool || r.calls[raw.Item.ID] != nil || r.calls[raw.Item.CallID] != nil
  705. }
  706. if _, namespaceTool := r.adapter.NamespaceTools[raw.Name]; namespaceTool {
  707. return true
  708. }
  709. if r.calls[raw.ItemID] != nil || r.calls[raw.CallID] != nil || r.byOutput[raw.OutputIndex] != nil {
  710. return true
  711. }
  712. return false
  713. }
  714. func clientToolLifecycleEvent(typ string) bool {
  715. switch typ {
  716. case "response.output_item.added", "response.output_item.done", "response.function_call_arguments.delta", "response.function_call_arguments.done":
  717. return true
  718. default:
  719. return false
  720. }
  721. }
  722. // resequenceRaw deliberately keeps opaque upstream event fields untouched.
  723. func (r *ResponsesClientToolStreamRestorer) resequenceRaw(payload []byte, sequence int, changed bool) ([][]byte, bool, error) {
  724. if !r.seenSeq {
  725. r.nextSeq, r.seenSeq = sequence, true
  726. }
  727. if r.nextSeq == sequence && !changed {
  728. r.nextSeq++
  729. return [][]byte{payload}, false, nil
  730. }
  731. var raw map[string]any
  732. if err := json.Unmarshal(payload, &raw); err != nil {
  733. return nil, false, err
  734. }
  735. raw["sequence_number"] = r.nextSeq
  736. r.nextSeq++
  737. encoded, err := json.Marshal(raw)
  738. if err != nil {
  739. return nil, false, err
  740. }
  741. return [][]byte{encoded}, true, nil
  742. }
  743. // responsesClientToolItemType maps a restorer call kind to the item type the
  744. // client sees.
  745. func responsesClientToolItemType(kind string) string {
  746. if kind == "custom" {
  747. return "custom_tool_call"
  748. }
  749. return "tool_search_call"
  750. }
  751. func (r *ResponsesClientToolStreamRestorer) recordItem(event ResponsesStreamEvent) *responsesClientToolStreamCall {
  752. if event.Item == nil || event.Item.Type != "function_call" {
  753. return nil
  754. }
  755. name := event.Item.Name
  756. kind := ""
  757. if r.adapter.CustomTools[name] {
  758. kind = "custom"
  759. } else if r.adapter.ToolSearch && name == toolSearchProxyName {
  760. kind = "tool_search"
  761. }
  762. if kind == "" {
  763. return nil
  764. }
  765. key := event.Item.ID
  766. if key == "" {
  767. key = event.Item.CallID
  768. }
  769. call := r.calls[key]
  770. if call == nil {
  771. call = &responsesClientToolStreamCall{
  772. kind: kind,
  773. name: name,
  774. callID: event.Item.CallID,
  775. itemID: event.Item.ID,
  776. clientItemID: retypedResponsesToolCallItemID(event.Item.ID, responsesClientToolItemType(kind)),
  777. outputIdx: event.OutputIndex,
  778. }
  779. r.calls[key] = call
  780. if call.callID != "" {
  781. r.calls[call.callID] = call
  782. }
  783. r.byOutput[call.outputIdx] = call
  784. }
  785. if event.Item.Arguments != "" {
  786. call.arguments.Reset()
  787. _, _ = call.arguments.WriteString(event.Item.Arguments)
  788. }
  789. return call
  790. }
  791. func (r *ResponsesClientToolStreamRestorer) callFor(event ResponsesStreamEvent) *responsesClientToolStreamCall {
  792. if call := r.calls[event.ItemID]; call != nil {
  793. return call
  794. }
  795. if call := r.byOutput[event.OutputIndex]; call != nil {
  796. return call
  797. }
  798. for _, call := range r.calls {
  799. if (event.CallID != "" && call.callID == event.CallID) || (event.ItemID == "" && event.Name != "" && call.name == event.Name) {
  800. return call
  801. }
  802. }
  803. return nil
  804. }
  805. func (r *ResponsesClientToolStreamRestorer) restoreNamespaceEvent(event ResponsesStreamEvent) ResponsesStreamEvent {
  806. if len(r.adapter.NamespaceTools) == 0 {
  807. return event
  808. }
  809. if event.Item != nil && event.Item.Type == "function_call" {
  810. if name, ok := r.adapter.NamespaceTools[event.Item.Name]; ok {
  811. event.Item.Name, event.Item.Namespace = name.Name, name.Namespace
  812. }
  813. }
  814. if event.Type == "response.function_call_arguments.delta" || event.Type == "response.function_call_arguments.done" {
  815. if name, ok := r.adapter.NamespaceTools[event.Name]; ok {
  816. event.Name = name.Name
  817. }
  818. }
  819. return event
  820. }
  821. func restoreResponsesOutputClientTools(outputs []ResponsesOutput, adapter *ResponsesClientToolMapping) {
  822. for index := range outputs {
  823. output := &outputs[index]
  824. if output.Type != "function_call" {
  825. continue
  826. }
  827. if adapter.CustomTools[output.Name] {
  828. output.Type = "custom_tool_call"
  829. output.ID = retypedResponsesToolCallItemID(output.ID, output.Type)
  830. output.Input = extractCustomToolCallInput(output.Arguments)
  831. output.Arguments = ""
  832. output.Namespace = ""
  833. } else if adapter.ToolSearch && output.Name == toolSearchProxyName {
  834. output.Type = "tool_search_call"
  835. output.ID = retypedResponsesToolCallItemID(output.ID, output.Type)
  836. output.Name = ""
  837. output.Namespace = ""
  838. }
  839. if name, ok := adapter.NamespaceTools[output.Name]; ok && output.Type == "function_call" {
  840. output.Name, output.Namespace = name.Name, name.Namespace
  841. }
  842. }
  843. }