Skip to content

refactor(openai-shim): extract Ollama adapter - #2004

Merged
kevincodex1 merged 1 commit into
Twigpine:mainfrom
jatmn:de-mono1-ollama-adapter
Jul 22, 2026
Merged

kevincodex1 merged 1 commit into
Twigpine:mainfrom
jatmn:de-mono1-ollama-adapter

Conversation

@jatmn

@jatmn jatmn commented Jul 18, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Extracts native Ollama request/response adaptation from src/services/api/openaiShim.ts into src/services/api/openaiShim/ollamaAdapter.ts.

The module owns native URL selection, context sizing, multipart/tool-message normalization, and native streaming/non-streaming conversion. The façade keeps route selection and delegates the adapter work.

All branches share stable source and test extraction seams so accumulated merges remain conflict-free in arbitrary order.

Independent merge

  • Based on upstream main at 0effa0f42b6dbc6a4800e19f4b2d8d588269906b.
  • Fork branch: jatmn/openclaude:de-mono1-ollama-adapter.
  • Exact head: b2013885285c2ed208ae4cf22bada64612a2124b.
  • All 45 pairwise combinations are clean; full ten-branch merges pass forward and reverse.
  • Submitted as a draft to upstream.

Source-file budget

openaiShim.ts: +45 / -408 = 453 changed lines, below the 1,500-line cap. Tests are excluded.

Test migration

  • Monolithic test: +699 / -75; the two owned Ollama test bodies are removed.
  • ollamaAdapter.test.ts: 321 lines / 7 focused tests.
  • Focused adapter, retained façade, and affected bugfix suites: 272 passed / 782 assertions.
  • The all-PR result leaves only three façade-contract tests in the monolithic test.

Validation

  • git diff --check, bun run typecheck, bun run typecheck:type-tests, and bun run build: passed.
  • Exact-head host bun run check: 7,190 passed / 2 skipped; only the same 10 pre-existing PowerShell governance failures remain on Linux.

Prepared according to CONTRIBUTING.md and AGENTS.md.

Summary by CodeRabbit

  • Improvements
    • Improved Ollama compatibility for chat requests and responses.
    • Added support for native Ollama streaming, tool calls, image content, and usage reporting.
    • Automatically translates OpenAI-style settings such as token limits and context size for Ollama.
    • Improved handling of Ollama completion statuses and streamed responses.

@coderabbitai

coderabbitai Bot commented Jul 18, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

The Ollama OpenAI-compatibility implementation moves into a dedicated adapter, with request normalization, response conversion, streaming support, shim integration, and focused regression tests.

Changes

Ollama adapter extraction

Layer / File(s) Summary
Request contracts and normalization
src/services/api/openaiShim/ollamaAdapter.ts, src/services/api/openaiShim/ollamaAdapter.test.ts
Adds Ollama request types, URL/context helpers, image extraction, message normalization, and tool-call conversion with coverage for these behaviors.
Response conversion
src/services/api/openaiShim/ollamaAdapter.ts, src/services/api/openaiShim/ollamaAdapter.test.ts
Converts non-streaming Ollama JSON and streaming NDJSON into OpenAI completion and SSE formats, including finish reasons, tool calls, and usage.
Shim integration and regression coverage
src/services/api/openaiShim.ts, src/services/api/openaiShim/ollamaAdapter.test.ts, src/__tests__/bugfixes.test.ts
Wires adapter helpers into the shim, removes the duplicated implementation, adds façade tests, and removes the former source-inspection test.

Estimated code review effort: 4 (Complex) | ~45 minutes

Possibly related PRs

  • Gitlawb/openclaude#1977: Modifies related OpenAI shim tool-call handling for self-hosted and OpenAI-compatible backends.

Suggested reviewers: kevincodex1, chioarub

🚥 Pre-merge checks | ✅ 6 | ❌ 1

❌ Failed checks (1 inconclusive)

Check name Status Explanation Resolution
Risk Surface Disclosed ❓ Inconclusive PR touches provider routing/outbound HTTP (Ollama URL rewriting/stream conversion), but no actual review text is provided to verify risk/blocker disclosure. Please provide the review note/approval text that explicitly states the Ollama network/routing risk and whether it is blocking.
✅ Passed checks (6 passed)
Check name Status Explanation
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
No Hidden Policy Change ✅ Passed Pure Ollama refactor: same URL rewrite, ctx sizing, and response conversion; no telemetry, trust, routing-default, or permission-policy behavior changed.
Title check ✅ Passed The title is concise, scoped, and accurately summarizes the Ollama adapter extraction refactor.
Description check ✅ Passed The description covers the summary and validation well, but it does not follow the template's Impact and Notes sections.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Comment @coderabbitai help to get the list of available commands.

@jatmn jatmn self-assigned this Jul 18, 2026
@jatmn jatmn added the enhancement New feature or request label Jul 18, 2026

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 3

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@src/__tests__/bugfixes.test.ts`:
- Around line 94-106: Replace the source-text checks in the test “Ollama adapter
uses native chat with request-level num_ctx” with behavioral coverage of the
Ollama provider/model path: verify native /api/chat request normalization and
both streaming and non-streaming response conversion. Reuse existing
endpoint-test fixtures or helpers where available; if that path is already
covered, remove this redundant test.

In `@src/services/api/openaiShim/ollamaAdapter.ts`:
- Around line 100-131: Update normalizeOllamaNativeMessages to track assistant
tool-call IDs and their function names while iterating messages, then translate
role "tool" messages from tool_call_id to the corresponding native tool_name and
omit tool_call_id. Preserve existing message normalization for other roles and
add a regression test covering an assistant tool call followed by its tool
result.
- Around line 173-176: Update the non-streaming response construction in the
Ollama adapter so finish_reason is set to "tool_calls" whenever toolCalls is
non-empty, otherwise preserve mapOllamaDoneReason(data.done_reason). Add or
update coverage for a native non-streaming tool response to verify this
behavior.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro

Run ID: 9c140ef4-d459-4a2c-acd5-6b146d865c52

📥 Commits

Reviewing files that changed from the base of the PR and between 0effa0f and 5ea4c5f.

📒 Files selected for processing (5)
  • src/__tests__/bugfixes.test.ts
  • src/services/api/openaiShim.test.ts
  • src/services/api/openaiShim.ts
  • src/services/api/openaiShim/ollamaAdapter.test.ts
  • src/services/api/openaiShim/ollamaAdapter.ts
💤 Files with no reviewable changes (1)
  • src/services/api/openaiShim.test.ts
📜 Review details
⏰ Context from checks skipped due to timeout. (3)
  • GitHub Check: smoke-and-tests (24.11.x)
  • GitHub Check: smoke-and-tests (22)
  • GitHub Check: typecheck
🧰 Additional context used
📓 Path-based instructions (5)
**/*.{ts,tsx}

📄 CodeRabbit inference engine (AGENTS.md)

TypeScript code in this repository must use strict mode and ESM imports.

**/*.{ts,tsx}: When changing provider behavior, avoid breaking third-party providers and test the exact provider/model path changed when possible.
Run bun run typecheck and bun run typecheck:type-tests for TypeScript changes.
Run provider tests and provider recommendation tests when changing provider behavior: bun run test:provider and bun run test:provider-recommendation.

Files:

  • src/__tests__/bugfixes.test.ts
  • src/services/api/openaiShim/ollamaAdapter.test.ts
  • src/services/api/openaiShim/ollamaAdapter.ts
  • src/services/api/openaiShim.ts
**/*

📄 CodeRabbit inference engine (CONTRIBUTING.md)

**/*: Keep changes focused on one problem or feature and avoid mixing unrelated cleanup into the same change.
Preserve existing repository patterns unless intentionally refactoring them.
Update documentation when setup, commands, or user-facing behavior changes.
Review AI-generated changes for correctness, style consistency, unnecessary changes, and adherence to project architecture before submitting them.
Follow the existing code style in touched files.
Prefer small, readable changes over broad rewrites.
Do not reformat unrelated files.
Keep comments useful and concise.
Provider changes must explicitly identify affected providers, limitations, and follow-up work in the pull request description.
Do not assign or use provider tags; provider tags are controlled by maintainers.
Run the relevant validation checks locally before submitting changes; pull requests must pass CI checks.
Run bun run security:pr-scan before submitting a pull request.
Dependency changes require a concrete project benefit, such as fixing a bug, addressing a security issue, or supporting an approved feature.
Do not change the project's language, core runtime, or dependency stack without prior maintainer agreement.

Files:

  • src/__tests__/bugfixes.test.ts
  • src/services/api/openaiShim/ollamaAdapter.test.ts
  • src/services/api/openaiShim/ollamaAdapter.ts
  • src/services/api/openaiShim.ts

⚙️ CodeRabbit configuration file

**/*: Apply the OpenClaude maintainer review rubric from AGENTS.md. Review the current diff, not stale discussion context. Separate real blockers from suggestions. Do not request changes for vague style churn. Treat approval as merge-ready from CodeRabbit's side, pending required human review and GitHub Checks. If checks are failing or unavailable, say so clearly instead of implying the PR is fully ready.

Files:

  • src/__tests__/bugfixes.test.ts
  • src/services/api/openaiShim/ollamaAdapter.test.ts
  • src/services/api/openaiShim/ollamaAdapter.ts
  • src/services/api/openaiShim.ts
**/*.{ts,tsx,js,jsx}

📄 CodeRabbit inference engine (CONTRIBUTING.md)

Add or update tests when a code change affects behavior.

Files:

  • src/__tests__/bugfixes.test.ts
  • src/services/api/openaiShim/ollamaAdapter.test.ts
  • src/services/api/openaiShim/ollamaAdapter.ts
  • src/services/api/openaiShim.ts
{src/**/*.test.ts,src/**/*.test.tsx,tests/**,scripts/**/*.test.ts,vscode-extension/**/*.test.js}

⚙️ CodeRabbit configuration file

{src/**/*.test.ts,src/**/*.test.tsx,tests/**,scripts/**/*.test.ts,vscode-extension/**/*.test.js}: Review tests for meaningful coverage of the changed behavior, isolation of global/env/config state, async cleanup, fake timers, provider profile leaks, and Windows-compatible assumptions. Block when risky runtime changes lack focused regression coverage or tests assert implementation details while missing the user-visible behavior.

Files:

  • src/__tests__/bugfixes.test.ts
  • src/services/api/openaiShim/ollamaAdapter.test.ts
{src/services/api/**,src/integrations/**,src/utils/model/**,src/utils/provider*.ts,src/commands/provider/**}

⚙️ CodeRabbit configuration file

{src/services/api/**,src/integrations/**,src/utils/model/**,src/utils/provider*.ts,src/commands/provider/**}: Review provider routing, model selection, env precedence, auth/token handling, OpenAI-compatible shims, retries, proxy behavior, and outbound HTTP behavior with high scrutiny. Block on silent default changes, hidden fallback expansion, credential reuse mistakes, hardcoded provider assumptions, or new network reach that is not intentional and documented.

Files:

  • src/services/api/openaiShim/ollamaAdapter.test.ts
  • src/services/api/openaiShim/ollamaAdapter.ts
  • src/services/api/openaiShim.ts
🔇 Additional comments (2)
src/services/api/openaiShim.ts (1)

122-128: LGTM!

Also applies to: 478-478, 4356-4365

src/services/api/openaiShim/ollamaAdapter.test.ts (1)

1-8: 📐 Maintainability & Code Quality

Report the required provider validation results.

The supplied context reports shim tests and typecheck, but not bun run typecheck:type-tests, bun run test:provider, bun run test:provider-recommendation, or bun run security:pr-scan. Please run and report them before merge.

As per coding guidelines, “Run provider tests and provider recommendation tests when changing provider behavior” and “Run bun run security:pr-scan before submitting a pull request”; as per path instructions, run the listed focused adapter and shim tests.

Sources: Coding guidelines, Path instructions

Comment thread src/__tests__/bugfixes.test.ts Outdated
Comment thread src/services/api/openaiShim/ollamaAdapter.ts
Comment thread src/services/api/openaiShim/ollamaAdapter.ts Outdated
@jatmn
jatmn force-pushed the de-mono1-ollama-adapter branch from 907c0cf to 890ab7d Compare July 18, 2026 16:38
@jatmn
jatmn force-pushed the de-mono1-ollama-adapter branch 2 times, most recently from 78b7cd0 to b201388 Compare July 18, 2026 20:42
coderabbitai[bot]
coderabbitai Bot previously approved these changes Jul 18, 2026
@jatmn
jatmn force-pushed the de-mono1-ollama-adapter branch from 50ed940 to 89cafc7 Compare July 22, 2026 14:52
@jatmn
jatmn marked this pull request as ready for review July 22, 2026 15:34

@jatmn jatmn left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kevincodex1 LGTM, please merge

@kevincodex1 kevincodex1 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@kevincodex1
kevincodex1 merged commit 6bef0e1 into Twigpine:main Jul 22, 2026
5 checks passed
@jatmn
jatmn deleted the de-mono1-ollama-adapter branch July 22, 2026 23:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants