Expose adaptive context APIs for Swift LLM#512
Conversation
|
Important Review skippedDraft detected. Please check the settings in the CodeRabbit UI or the ⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: You can disable this status message by setting the Use the checkbox below for a quick retry:
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 1 potential issue.
Bugbot Autofix is ON, but it could not run because the branch was deleted or merged before autofix could start.
Comment @cursor review or bugbot run to trigger another review on this PR
Reviewed by Cursor Bugbot for commit 369d02c. Configure here.
| return RAC_ERROR_COMPONENT_NOT_READY; | ||
| } | ||
|
|
||
| return rac_llm_generate_from_context(service, query, options, out_result); |
There was a problem hiding this comment.
Null options skip component defaults
Medium Severity
rac_llm_component_generate_from_context forwards a null options pointer straight to the service, even though its header documents null for defaults. rac_llm_component_generate and streaming generation on the same component substitute component->default_options first, so callers using the new adaptive-context entry point get different (often unsafe) behavior than other generation APIs.
Reviewed by Cursor Bugbot for commit 369d02c. Configure here.
|
Could a maintainer please approve the GitHub Actions run for this fork PR when you get a chance? I’m contributing from a Windows environment, so I can’t run the macOS/iOS Swift checks locally. I’ve included the local static checks I was able to run in the PR description. |


Summary
Fixes #500.
This PR exposes the adaptive-context LLM APIs through the component layer and adds lifecycle-owned Swift accessors so Swift callers can use the model loaded through the existing
RunAnywhere.loadModelflow.Changes included:
rac_llm_component_inject_system_prompt,rac_llm_component_append_context,rac_llm_component_generate_from_context, andrac_llm_component_clear_context.rac_llm_inject_system_prompt_lifecyclerac_llm_append_context_lifecyclerac_llm_generate_from_context_protorac_llm_clear_context_lifecycleRunAnywhere.injectSystemPrompt(_:)RunAnywhere.appendContext(_:)RunAnywhere.generateFromContext(query:options:)RunAnywhere.clearContext()RACommons.exportsso dynamic Apple builds export the new symbols.Why
The service layer already exposes adaptive-context operations, but Swift's
CppBridge.LLMoperates through the component/lifecycle surface. Without wrappers, Swift consumers cannot use prefix-caching/adaptive-context workflows without bypassing the normal model lifecycle.During local review, the public Swift path was kept lifecycle-owned rather than component-handle-owned so these APIs use the same model loaded by
RunAnywhere.loadModeland do not require opening a parallel LLM handle.Local verification
Verified locally on Windows:
git diff --checkRACommons.exportsgenerate_from_contextpaths to confirm they continue from existing context/cache rather than clearing itNot run locally because this contributor environment does not have macOS/iOS tooling:
swift buildbashmaps to WSL here, but WSL2 is not enabled)CI/macOS validation is needed before this is ready for merge.
Note
Medium Risk
Changes affect on-device LLM KV-cache behavior and add a new public inference path; risk is moderated by thin wrappers over existing backend ops and lifecycle locking, but incorrect context sequencing could still yield wrong generations.
Overview
Exposes adaptive-context LLM operations (prefix/KV caching for RAG-style flows) through the component layer and a lifecycle-owned C ABI, then surfaces them on Swift as
RunAnywhere.injectSystemPrompt,appendContext,generateFromContext, andclearContext.Commons: New
rac_llm_component_*entry points delegate to the existing service vtable (inject_system_prompt,append_context,generate_from_context,clear_context). New lifecycle helpers (rac_llm_inject_system_prompt_lifecycle,rac_llm_append_context_lifecycle,rac_llm_generate_from_context_proto,rac_llm_clear_context_lifecycle) target the model fromrac_model_lifecycle_load_proto;generate_from_context_protomirrors normal proto generate (events, thinking split, structured output) but callsgenerate_from_contextso the KV cache is not cleared first.Swift:
CppBridge.LLMloads those symbols viaNativeProtoABI(lifecycle path, not a separate component handle). Public text-generation extensions gate on SDK init and forward to the bridge. Vendored CRACommons headers andRACommons.exportslist the new symbols for dynamic Apple builds.Reviewed by Cursor Bugbot for commit 369d02c. Configure here.