open-code-review/cmd/opencodereview
Lei Zhang f58b0f2924
feat: Add OpenAI Responses API support and refactor protocol handling (#363)
* feat(llm): add OpenAI Responses API support

Add `openai-responses` as a third LLM protocol alongside `anthropic` and
`openai-chat-completions`, enabling code review via the OpenAI Responses
API (/v1/responses) for GPT-5.x / o-series models.

Protocol naming refactor (backward-compatible):
- Canonicalize "openai" -> "openai-chat-completions" (alias still accepted)
- Add NormalizeProtocol / ValidateProtocol / IsAnthropicProtocol helpers
- Registry uses canonical constants; resolver normalizes everywhere

New OpenAIResponsesClient (stateless replay, per DESIGN_STATE_CACHE_PHASE.md):
- system messages -> Instructions; tool calls -> function_call items keyed
  by CallID so the agent loop pairs results correctly
- store=false (privacy); prompt_cache_key = sha256(instructions)[:32]
- Phase fields (commentary/final_answer) dropped with TODO for gpt-5.3-codex+

Config plumbing:
- llm.protocol field + OCR_LLM_PROTOCOL env (priority over use_anthropic /
  OCR_USE_ANTHROPIC); TUI exposes all three protocols in Custom & Manual
- anthropic-vertex rejected with friendly "not yet implemented" message

Docs: protocol reference, config examples, env var table, and Responses API
notes (store=false caching caveat, cache key derivation, Phase TODO) updated
across en/zh-CN/ko-KR/ja-JP/ru-RU READMEs.

* refactor(llm): switch PromptCacheKey to precomputed scheme via ChatRequest.CacheKey

Replace per-turn sha256 computation inside buildResponsesParams with a
precomputed cache key that callers compute once per session and pass
through ChatRequest.CacheKey (json:"-"). The key now incorporates the
first user message alongside instructions, so different files under
review land in distinct cache buckets — the previous instructions-only
key was identical across all files.

Changes:
- ChatRequest gains CacheKey string field (json:"-", zero impact on
  Chat Completions / Anthropic clients which never read it)
- New llm.ComputeCacheKey helper: sha256(instructions + "\x00" +
  firstUser)[:32]
- responses_client.go: reads req.CacheKey directly, removes promptCacheKey
  function and first-user-message scanning
- loop.go: RunPerFile computes cacheKey once before the loop, reuses
  every turn
- All 8 remaining call sites (agent, scan, relocation, compression,
  llm_cmd) compute once at request construction
- Update PLAN_RESPONSES_SUPPORT.md and DESIGN_STATE_CACHE_PHASE.md to
  reflect the precomputed scheme
- Update tests: passthrough tests for client, dedicated TestComputeCacheKey

* refactor(llm): use canonical protocol name "openai" and UUID-based session ID for cache key

Two changes to maximize backward compatibility and simplify the design:

1. Protocol naming: revert ProtocolOpenAIChatCompletions value from
   "openai-chat-completions" back to "openai". Old config files with
   protocol: "openai" are now identical to what new configs write —
   zero behavioral difference. The alias direction in NormalizeProtocol
   is reversed: "openai-chat-completions" -> "openai" (for configs
   written during this branch's testing phase only).

2. Cache key: replace content-based sha256 hash (ComputeCacheKey) with a
   random UUID session ID. The agent loop generates one UUID per file in
   RunPerFile and passes it via ChatRequest.SessionID; the Responses
   client uses it as prompt_cache_key. Single-turn call sites no longer
   set a cache key (no multi-turn caching benefit). This removes the
   need to scan messages or compute hashes, and eliminates collision
   risk between files with similar content.

   ChatRequest.CacheKey is renamed to SessionID to reflect its actual
   semantic — a per-session identifier that the Responses client
   repurposes as prompt_cache_key.

Also updates PLAN_RESPONSES_SUPPORT.md, all 5 README translations,
test expectations, and promotes google/uuid to a direct dependency.

* refactor(llm): remove IsAnthropicProtocol helper and anthropic-vertex special case

* refactor(llm): remove openai-chat-completions branch-internal alias

* docs: remove DESIGN_STATE_CACHE_PHASE and PLAN_RESPONSES_SUPPORT design notes

* fix(llm): address code review findings on Responses API support

- provider_cmd: clear stale use_anthropic when switching to openai-responses
- resolver: validate preset protocol with ValidateProtocol for consistency
- responses_client: swap usage mapping to resolveUsage-first (matches OpenAIClient)
- responses_client: map failed/cancelled statuses to 'error' finish reason
- usage_resolver: add Responses API field paths (input_tokens, output_tokens,
  input_tokens_details.cached_tokens)
- add tests for all four fixes

* fix(llm): mirror use_anthropic when setting llm.protocol

- config_cmd: 'ocr config set llm.protocol' now mirrors use_anthropic
  (anthropic -> true, OpenAI family -> false) for backward compat with
  older binaries that predate llm.protocol
- provider_cmd: openai-responses now sets use_anthropic=false instead of
  nil, so older binaries fall back to the OpenAI family rather than
  wrongly defaulting to anthropic
- update tests for both write paths

* fix(llm): mirror protocol when setting llm.use_anthropic

- config_cmd: 'ocr config set llm.use_anthropic' now mirrors protocol
  (true -> anthropic, false -> openai) so the two fields never disagree,
  matching the reverse llm.protocol mirroring added previously
- without this, setting use_anthropic=true while protocol=openai-responses
  left a contradictory config that misled older binaries into using the
  anthropic protocol against an OpenAI endpoint
- extend tests to cover both values and stale-protocol overwrite

* docs(llm): fix NormalizeProtocol comment to match lowercasing behavior

The comment claimed unknown values are 'returned unchanged', but the
default branch lowercases and trims them (corroborated by the
'gRPC -> grpc' test). Update the wording to describe the actual
behavior so callers aren't misled about round-trip fidelity.

* docs(llm): drop OpenAI Responses API implementation notes from READMEs

* fix(llm): address code review findings on Responses API support

- config: preserve openai-responses when setting llm.use_anthropic=false
  (only mirror to openai when protocol is unset or a legacy anthropic/openai)
- config: add Protocol values guidance to unknown-key error message
- protocol: extract normalized local var in NormalizeProtocol
- responses_client: align SDK base URL trimming with NewOpenAIClient
- responses_client: drop unused test-only sdkBaseURL method

* fix(llm): use protocol constants consistently

- providers: edenai now uses ProtocolOpenAIChatCompletions like the rest
  of the registry instead of the "openai" string literal
- provider_cmd: print the normalized protocol variable (what is actually
  saved) instead of the raw TUI value

* fix(llm): drop stream key and surface non-completed status in Responses client

Address two PR review comments on OpenAI Responses API support:

1. extra_body.stream=true was forwarded to Responses.New, making the API
   return SSE while the SDK expects JSON and breaking every call. Skip the
   'stream' key (like OpenAIClient treats it as a non-forwarded key) while
   still forwarding other extra_body entries.

2. The Responses API returns HTTP 200 even for failed/cancelled (terminal)
   and queued/in_progress (background) states, so the SDK reports nil error.
   Surface these as real errors so callers branching on err != nil (ocr llm
   test, review loop) fail instead of treating a dead response as success.

Add table-driven tests covering both fixes.
2026-07-15 12:54:52 +08:00
..
background_file.go fix(background-file) Manage path at git repository root directory Refs:#324 (#327) 2026-07-13 11:27:54 +08:00
background_file_test.go fix(background-file) Manage path at git repository root directory Refs:#324 (#327) 2026-07-13 11:27:54 +08:00
config_cmd.go feat: Add OpenAI Responses API support and refactor protocol handling (#363) 2026-07-15 12:54:52 +08:00
config_cmd_test.go feat: Add OpenAI Responses API support and refactor protocol handling (#363) 2026-07-15 12:54:52 +08:00
config_dispatch_test.go test: improve coverage for cmd/opencodereview package from 42% to 70% 2026-06-27 01:47:48 +08:00
emit_run_result_test.go feat(review): add resumable sessions and session inspection (#306) 2026-07-09 11:43:11 +08:00
flags.go feat: Add OpenAI Responses API support and refactor protocol handling (#363) 2026-07-15 12:54:52 +08:00
flags_test.go feat(review): add resumable sessions and session inspection (#306) 2026-07-09 11:43:11 +08:00
git.go fix(tool): resolve file_read paths against git top-level in monorepos (#309) 2026-07-07 20:06:28 +08:00
git_test.go test: fix golangci-lint errcheck/staticcheck issues in test code (#323) 2026-07-08 22:46:18 +08:00
llm_cmd.go feat(vscode): add full-width BYOK provider config panel (#204) 2026-06-25 10:37:25 +08:00
main.go feat(review): add resumable sessions and session inspection (#306) 2026-07-09 11:43:11 +08:00
output.go feat(review): add resumable sessions and session inspection (#306) 2026-07-09 11:43:11 +08:00
output_helpers_test.go feat(review): add resumable sessions and session inspection (#306) 2026-07-09 11:43:11 +08:00
output_test.go test: fix golangci-lint errcheck/staticcheck issues in test code (#323) 2026-07-08 22:46:18 +08:00
procattr_unix.go feat: add standard MCP tool support (#212) 2026-07-01 19:10:16 +08:00
procattr_windows.go feat: add standard MCP tool support (#212) 2026-07-01 19:10:16 +08:00
provider_cmd.go feat: Add OpenAI Responses API support and refactor protocol handling (#363) 2026-07-15 12:54:52 +08:00
provider_cmd_test.go test: fix golangci-lint errcheck/staticcheck issues in test code (#323) 2026-07-08 22:46:18 +08:00
provider_tui.go feat: Add OpenAI Responses API support and refactor protocol handling (#363) 2026-07-15 12:54:52 +08:00
provider_tui_funcs_test.go fix(tui): persist official-tab models and refine saved secret hint (#260) 2026-07-06 20:38:39 +08:00
provider_tui_test.go feat: Add OpenAI Responses API support and refactor protocol handling (#363) 2026-07-15 12:54:52 +08:00
review_cmd.go feat(telemetry): propagate W3C traceparent from parent process (#352) 2026-07-14 11:28:09 +08:00
review_cmd_test.go fix(cli): reject --to without --from in review flags (#151) 2026-06-16 20:26:22 +08:00
rules_cmd.go feat(rules): add ocr rules check command to preview effective rule for a file path 2026-05-26 22:11:39 +08:00
scan_cmd.go feat(telemetry): propagate W3C traceparent from parent process (#352) 2026-07-14 11:28:09 +08:00
scan_cmd_test.go test: improve coverage for cmd/opencodereview package from 42% to 70% 2026-06-27 01:47:48 +08:00
session_cmd.go feat(review): add resumable sessions and session inspection (#306) 2026-07-09 11:43:11 +08:00
session_cmd_test.go feat(review): add resumable sessions and session inspection (#306) 2026-07-09 11:43:11 +08:00
shared.go feat(review): add resumable sessions and session inspection (#306) 2026-07-09 11:43:11 +08:00
shared_test.go test: fix golangci-lint errcheck/staticcheck issues in test code (#323) 2026-07-08 22:46:18 +08:00
shell_unix.go feat: add standard MCP tool support (#212) 2026-07-01 19:10:16 +08:00
shell_windows.go feat: add standard MCP tool support (#212) 2026-07-01 19:10:16 +08:00
smallfiles_test.go test: improve coverage for cmd/opencodereview package from 42% to 70% 2026-06-27 01:47:48 +08:00
version.go feat(cli): display GitHub repository URL in version and help output 2026-06-02 22:50:36 +08:00
viewer_cmd.go feat: init 2026-05-20 22:03:52 +08:00