Skip to content

Studio: Add opt-in tool execution for external providers - #7805

Closed
Etherll wants to merge 49 commits into
unslothai:mainfrom
Etherll:codex/issue-7761-ollama-tools
Closed

Etherll wants to merge 49 commits into
unslothai:mainfrom
Etherll:codex/issue-7761-ollama-tools

Conversation

@Etherll

@Etherll Etherll commented Aug 4, 2026

Copy link
Copy Markdown
Collaborator

Summary

Adds a per-connection studio_tool_execution option for external providers. It is disabled by default and lets compatible models use Studio web search, Python, terminal, and MCP tools through the existing permission and sandbox controls.

Supports OpenAI-compatible endpoints, including Ollama, plus native OpenAI, Anthropic, and Gemini provider paths when the selected model supports function calling.

Motivation

External connections could use some provider-hosted tools, but they could not run Studio-managed tools. Issue #7761 specifically requests web search, MCP, and code tools for models connected through Ollama.

Changes

  • Adds studio_tool_execution to provider create, update, and response models in studio/backend/models/providers.py:58.
  • Persists the opt-in in SQLite and defaults existing connections to disabled in studio/backend/storage/providers_db.py:79.
  • Adds a disabled-by-default "Studio tools" checkbox to the connection editor in studio/frontend/src/features/chat/chat-providers-dialog.tsx:1124.
  • Adds a multi-round external tool loop in studio/backend/core/inference/external_tool_loop.py:188.
    • Executes Studio tools through the existing sandbox and permission system.
    • Supports confirmation prompts, cancellation, timeouts, duplicate-call protection, and tool-call limits.
    • Streams tool_start, progress, and tool_end events to the existing chat UI.
    • Sends tool results back to the external model for its final response.
  • Makes the saved provider record authoritative for the provider type, endpoint, and tool-execution opt-in in studio/backend/routes/inference.py:8984.
  • Preserves unrelated provider-hosted tools while Studio-managed tools are active in studio/frontend/src/features/chat/api/external-tool-payload.ts:13.
  • Translates Studio function declarations and streamed tool_use blocks for Anthropic in studio/backend/core/inference/external_provider.py:2050.
  • Uses the existing normalized function-call paths for OpenAI Responses, OpenAI-compatible APIs, Ollama, and Gemini.
  • Adds backend and frontend coverage for persistence, routing authority, cancellation, tool failures, hosted-tool coexistence, Anthropic translation, Gemini translation, and legacy defaults.

Migration notes

The provider database is migrated automatically when Studio opens it:

ALTER TABLE llm_providers
ADD COLUMN studio_tool_execution INTEGER NOT NULL DEFAULT 0;

Existing connections remain disabled. No manual migration or downtime is required. Downgrading leaves the additive column in place, but older versions do not use it.

Screenshots

127 0 0 1_8888_chat (2)

How to test

Run the focused backend tests:

python -m pytest -q studio/backend/tests/test_external_ollama_tool_loop.py studio/backend/tests/test_external_provider_routing.py studio/backend/tests/test_gemini_provider.py studio/backend/tests/test_providers_db_models.py

Run the frontend test and typecheck:

cd studio/frontend
node --experimental-strip-types --test tests/external-studio-tools.test.ts
npm run typecheck

Manual verification:

  1. Install and launch Studio with unsloth studio -p 8888.
  2. Add an external provider and leave "Studio tools" disabled.
  3. Confirm the connection cannot activate Studio-managed local or MCP tools.
  4. Edit the connection, enable "Studio tools", and select a model that supports function calling.
  5. Enable Search, Code, or MCP and request a task that requires the selected tool.
  6. Confirm Studio displays the tool call, applies the configured permission mode, executes it, and returns the result to the model.
  7. Disable the option again and confirm later requests use only supported provider-hosted capabilities.

Related

Closes #7761

@Etherll
Etherll requested a review from danielhanchen as a code owner August 4, 2026 02:10
chatgpt-codex-connector[bot]

This comment was marked as resolved.

chatgpt-codex-connector[bot]

This comment was marked as resolved.

@Etherll

Etherll commented Aug 7, 2026

Copy link
Copy Markdown
Collaborator Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

if not sent_done:
if not stream_failed:
api_monitor.finish(monitor_id)

P2 Badge Mark event-driven cancellation before completing the monitor

When /api/inference/cancel sets this managed loop's cancel_event, the loop returns normally rather than raising CancelledError, so control reaches this completion branch with stream_failed=false. The API monitor is consequently finalized as a successful request and a normal terminal frame is appended, unlike the other streaming paths that inspect their cancellation event and record cancelled; this corrupts cancellation and completion metrics for externally managed tool runs. Check cancel_event.is_set() before calling the successful finish path.

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

choice = raw_choice
delta = choice.get("delta")
if isinstance(delta, dict):
delta.pop("tool_calls", None)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Preserve provider metadata beyond the current request

Fresh evidence beyond the fixed immediate continuation is that this still removes the only browser-bound copy of each tool call's extra_content, while the synthesized tool_start contains only normalized name, arguments, ID, and provenance. After a Gemini, Anthropic, or OpenAI reasoning tool round completes, the frontend therefore stores the call without its thought signature, signed thinking blocks, or reasoning items; the next user message replays that historical function call without the provider-required metadata and can be rejected even though the in-process continuation succeeded. Persist the opaque continuation metadata with the stored tool call, or retain equivalent server-side history.

Useful? React with 👍 / 👎.

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.

Rejected: the provider contracts scope strict continuation metadata to the active tool-use turn, which the managed loop already preserves in process. Google explicitly validates only the current turn after the latest ordinary user message: https://ai.google.dev/gemini-api/docs/generate-content/thought-signatures

@Etherll

Etherll commented Aug 7, 2026

Copy link
Copy Markdown
Collaborator Author

Fixed the cancellation-status issue from the latest review in bc4b66f0a: event-driven managed external cancellations now finish the API monitor as cancelled and do not emit a normal terminal frame. The same commit also fixes an order-dependent loss of provider-hosted tool events found during verification.

chatgpt-codex-connector[bot]

This comment was marked as resolved.

chatgpt-codex-connector[bot]

This comment was marked as resolved.

@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Aug 7, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Aug 7, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Aug 7, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Aug 7, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Aug 7, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Aug 7, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Aug 7, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Aug 7, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Aug 7, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Aug 7, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Aug 7, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Aug 7, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Aug 8, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Aug 8, 2026
@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Hooray!

Reviewed commit: 3c56effe41

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

chatgpt-codex-connector[bot]

This comment was marked as resolved.

@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Aug 8, 2026
@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Keep it up!

Reviewed commit: 4071a0492f

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

chatgpt-codex-connector[bot]

This comment was marked as resolved.

@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Aug 8, 2026
@Etherll

Etherll commented Aug 8, 2026

Copy link
Copy Markdown
Collaborator Author

@codex review

1 similar comment
@Etherll

Etherll commented Aug 8, 2026

Copy link
Copy Markdown
Collaborator Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Chef's kiss.

Reviewed commit: 413f42336a

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@abc-nix

abc-nix commented Aug 11, 2026

Copy link
Copy Markdown

Sorry for being noise here. I am interested in using unsloth desktop to connect to my llama-swap instance, so tool calls for this external provider would be very welcomed.

Etherll and others added 2 commits August 12, 2026 08:32
Resolve Studio-managed provider tools alongside credential persistence and upstream provider safeguards.

Co-Authored-By: ForgeCode <[email protected]>
danielhanchen added a commit that referenced this pull request Aug 14, 2026
The squash merge of #8665 kept only two trailers, so the four PRs that work was
built out of are not attributed anywhere in main's history. Recording them here.

#8626 (@khalidejaz) contributed the registry capability and the hidden-entry
exposure, down to the provider_runs_local_tools() name.

#8630 (@mahiatlinux) contributed the loop internals: the user-turn append, the
tool-stream advance and drain, usage merging, the approval flush delay, the
budget-exhausted nudge and the streamed tool-name fix, plus a number of its test
cases. They also built the merged branch and drove it in a browser against a
control worktree, which found several real defects before release, including a
truncated turn discarding a healed call together with the text describing it.

#7805 (@Etherll) contributed the hosted-provider reach.

#7330 (@Souravrajvi0) is the original OpenAI-compatible framing the work started
from.

Two things were deliberately not taken, and the difference is worth recording:
the per-connection framing from #8630 and the studio_tool_execution column from
#7805. #8665 shipped a static disclosure instead while widening the capability
from four self-hosted types to thirteen.

Co-authored-by: Malik Khalid Ejaz <[email protected]>
Co-authored-by: Maheswar Kumar <[email protected]>
Co-authored-by: Etherl <[email protected]>
Co-authored-by: Souravrajvi0 <[email protected]>
danielhanchen added a commit that referenced this pull request Aug 14, 2026
)

The squash merge of #8665 kept only two trailers, so the four PRs that work was
built out of are not attributed anywhere in main's history. Recording them here.

#8626 (@khalidejaz) contributed the registry capability and the hidden-entry
exposure, down to the provider_runs_local_tools() name.

#8630 (@mahiatlinux) contributed the loop internals: the user-turn append, the
tool-stream advance and drain, usage merging, the approval flush delay, the
budget-exhausted nudge and the streamed tool-name fix, plus a number of its test
cases. They also built the merged branch and drove it in a browser against a
control worktree, which found several real defects before release, including a
truncated turn discarding a healed call together with the text describing it.

#7805 (@Etherll) contributed the hosted-provider reach.

#7330 (@Souravrajvi0) is the original OpenAI-compatible framing the work started
from.

Two things were deliberately not taken, and the difference is worth recording:
the per-connection framing from #8630 and the studio_tool_execution column from
#7805. #8665 shipped a static disclosure instead while widening the capability
from four self-hosted types to thirteen.

Co-authored-by: Malik Khalid Ejaz <[email protected]>
Co-authored-by: Maheswar Kumar <[email protected]>
Co-authored-by: Etherl <[email protected]>
Co-authored-by: Souravrajvi0 <[email protected]>
@Etherll Etherll closed this Aug 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature] Add toolcalling etc for external API calls

3 participants