refactor(apple): run tools through the ai sdk instead of natively
Nobody has claimed this yet.
Assessment
- Difficulty
- 5/5
- Estimated time
- Over a week
- Newbie friendliness
- 25/100
- Issue type
- Feature
- Clarity
- Clearly specified
- Activity status
- Active
- Tech stack
- react-native, typescript
- Domain
- backend-api-design, mobile
Research direction
Start with ai-sdk.ts, AppleLLM.mm, and the JSITool and ToolInvoker plumbing to trace the current tool flow. Prototype the iOS 27 path first, then add bun test coverage and run a live check through the native module. Try the example app with stopWhen: stepCountIs(n), approvals, and streaming; completion also requires deciding how to handle built-in tools and verifying the iOS 26 path.
Written by the indexing model from the issue text.
Description
Summary
Today Apple runs JavaScript tools natively, inside one Foundation Models session. Apple could instead stop at the tool call and hand it to the AI SDK, which would run execute. That would make the Apple provider behave like other AI SDK providers: stopWhen, prepareStep, onStepFinish, tool approvals and client-side tools would all work.
This is for later. For now, #265 (toolChoice: 'required' and a named tool through a Dynamic Profile) and #266 (maxToolCalls) improve the native path.
How it works today
- The AI SDK passes the provider tool schemas only, so users register
executea second time withcreateAppleProvider({ availableTools })ormodel.updateTools(...). prepareToolsinai-sdk.tswraps each tool, gives it an id, and stores it inglobalThis.__APPLE_LLM_TOOLS__.- Native builds a
JSIToolper tool. When Apple calls one,JSITool.callgoes through the tool invoker tocallToolWithIdinAppleLLM.mm, which hops to the JS thread, looks the tool up in the global and awaits it. - Tool calls and results come back as
providerExecuted: true. The AI SDK only loops on tool calls it ran itself (clientToolCalls = stepToolCalls.filter((toolCall) => !toolCall.providerExecuted)inai'sgenerateText), so the whole Apple loop is one step:stopWhenandprepareStepnever apply, andonStepFinishfires once.
Proposed design
Each AI SDK step becomes one Apple request that stops at its first round of tool calls.
JS
- Send tools as name, description and schema only. Remove
availableTools,updateTools,prepareToolsand the global registry. doGenerate: return tool calls as plaintool-callparts (noproviderExecuted), withfinishReason: 'tool-calls'.doStream: emit the tool calls and a'tool-calls'finish when native stops.toolChoiceis applied per step by the AI SDK, so'required'maps to Apple's.requiredfor that one request. No Dynamic Profile mode switching is needed, because the request stops at the first call.
Native
-
iOS 27: create the session from a Dynamic Profile with:
- the tools and
.toolCallingMode(...), .transcriptErrorHandlingPolicy(.preserveTranscript),.historyTransformto hide the continuation prompt,.onToolCallthat throws a stop error for JS tools.
On that error, read the calls (with Apple's ids) from the kept transcript.
- the tools and
-
iOS 26: no profiles. The tool's
call(arguments:)records the arguments and throws. Ids are generated by us. -
Let the history end with tool results:
createTranscriptAndPromptcurrently requires the last message to be from the user. Continue with an empty prompt. -
Delete the
ToolInvokerplumbing,JSITool's invoker andcallToolWithId.
What was verified (on-device model, macOS 27, scratch tests)
| Check | Result |
|---|---|
| Stop at the tool calls with parallel calls ("weather and local time in Paris and London") | onToolCall saw all 4 calls, with ids and arguments, before the error. 0 tools ran. preserveTranscript kept one tool-call entry with all 4. |
| The same while streaming | All 4 captured, 0 ran. Stream snapshots don't include the calls, so capture comes from onToolCall or the kept transcript. |
Today's native generateText with AI SDK step-2 messages (user, assistant tool call, tool result) |
Rejected: INVALID_MESSAGE: Last message must be from user role. |
Resume from that history with an empty prompt and historyTransform hiding it |
"The weather in Paris is sunny with a temperature of 22°C.", with no new tool calls. |
required for a single step |
"Tell me a short joke." was forced into one getWeather call, and stopped there. |
| iOS 26-style stop: throw from the tool itself | All 4 parallel calls reached the tool before the error. No call ids. Only run on the macOS 27 runtime. |
Caveats
- Breaking change.
availableToolsandupdateToolsgo away.- Without
stopWhen,generateTextreturns the tool call with no text answer, as with other providers. Today Apple runs the tool and answers in one call.
- Built-in Vision tools (OCR, barcode) must still run on Apple's side.
onToolCallfires for them too, so the stop must only trigger for JS tool names. A round mixing both kinds needs handling. - Each step is a new Apple request that reprocesses the transcript. That may be slower than Apple's internal loop. iOS 27 reports cached input tokens, but reuse across requests hasn't been measured.
- The continuation prompt. Every
respondoverload needs a prompt, so steps after a tool result send an empty one. On iOS 27historyTransformhides it from the model. On iOS 26 it stays in the transcript; it worked in a test without the transform. - iOS 26 differences. There are no tool call ids at stop time, no preserved transcript, and no
requiredmode. - Not yet verified:
- the iOS 26 path on an iOS 26 device;
- the full AI SDK loop through the React Native bridge (needs a prototype and an example-app run);
- mixed rounds of built-in and JS tools.
Suggested next step
Build a prototype on a branch: the iOS 27 path first, with bun test coverage and a live check through the native module. Then run it in the example app with stopWhen: stepCountIs(n), approvals and streaming before deciding. It could ship as part of the iOS 27-only rewrite.
- Dominant language
- TypeScript
- Stars
- 1.4k
- Forks
- 64
- Avg merge
- 11h 8m
- Merged PRs (30d)
- 2
Getting set up
- No Dockerfile or Docker Compose file
- No pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from callstackincubator/ai
-
Difficulty 2/5 1-3 hours Newbie friendliness 62/100
callstackincubator/ai#378 ·
-
feat(android): add ml kit task apis and context cachingPossibly taken A pull request linked to this issue is open or already merged. Openenhancement
Difficulty 5/5 Over a week Newbie friendliness 20/100
callstackincubator/ai#366 · 1 comment ·
-
enhancement
Difficulty 4/5 3-5 days Newbie friendliness 20/100
callstackincubator/ai#365 · 2 comments ·
-
Blocked enhancement
Difficulty 3/5 1-2 days Newbie friendliness 30/100
callstackincubator/ai#364 · 1 comment ·
-
feat(android): add streamed and live transcription with word timingsPossibly taken A pull request linked to this issue is open or already merged. OpenBlocked enhancement
Difficulty 4/5 3-5 days Newbie friendliness 35/100
callstackincubator/ai#363 · 1 comment ·
All issues in callstackincubator/ai
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
Maintainers usually reply within 1 day
-
enhancement good first issue
Difficulty 2/5 1-3 hours Newbie friendliness 85/100
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 80/100
earendil-works/gondolin#222 ·
Maintainers usually reply within 1 day
-
enhancement
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
endora-commerce/endora-commerce#2203 ·
Maintainers usually reply within 1 day
-
enhancement priority:medium ready-for-human
Difficulty 2/5 1-3 hours Newbie friendliness 66/100
wondermoney/wondermoney#1079 ·
Maintainers usually reply within 1 day