NativeLlm

interface NativeLlm(source)

The seam a platform's native (non-Kotlin/Native-importable) LLM API implements and gets injected through, so a Kotlin actual (here, FoundationModelsOnDeviceLlm) reaches it without a cinterop binding. Hoisted from Doori's hand-copied InjectableDocumentAiAnalyzer/InjectableTextGenerator shape (core:ai/feature:agent there) so the pattern exists once in the toolkit instead of being re-copied into every consuming app.

Deliberately NOT OnDeviceLlm itself. Classic ObjC-based Kotlin/Native interop exports a plain Kotlin interface to Swift as an ObjC protocol, and a suspend fun on it as a completion-handler method — so NativeLlm is kept to the simplest shape a Swift class can conform to (Boolean, nullable String, a plain callback for streaming), and FoundationModelsOnDeviceLlm is the small adapter that turns this into the richer typed OnDeviceLlm contract (AiResult, a cancellable Flow) every other backend already exposes. Matches Doori's own split between its Swift-facing TextGenerator and the app-facing LlmGateway a small adapter turns it into.

Inheritors

Functions

Link copied to clipboard
abstract suspend fun generate(prompt: String): String?

Null means "declined" — unavailable, or a runtime failure — never throws. Matches OnDeviceLlm.generate's no-throw contract, and keeps a Swift implementation from having to propagate a native error across the interop boundary.

Link copied to clipboard

Streams prompt token-by-token through callback — real per-token output (Apple's LanguageModelSession.streamResponse), not a single-emission replay of generate. Returns a NativeLlmCancelHandle the Kotlin side calls when its collecting coroutine is cancelled, so Stop/screen-leave actually halts native generation instead of only stopping the Kotlin side from listening — the same contract MediaPipeOnDeviceLlm's Android backend already honors for its own callbackFlow.

Link copied to clipboard
abstract fun isAvailable(): Boolean