capacitor-apple-llm
v0.1.0
Published
Capacitor 7 插件:iOS 调用 Apple Foundation Models(原生与前端交互)
Downloads
14
Maintainers
Readme
capacitor-apple-LLM
Apple LLM (iOS Only)
Capacitor 7 plugin for iOS to call Apple Foundation Models (requires iOS 26+).
Install
In your app project:
npm i file:./MobileApp/capacitorplugins/capacitor-apple-LLM
npx cap sync iosUsage
import { AppleLLM } from 'capacitor-apple-llm';
const supported = await AppleLLM.isSupported(); // iOS 26+ returns supported=true
if (!supported.supported) {
console.warn(supported.reason);
}
const result = await AppleLLM.generateText({
instructions: 'You are a helpful assistant. Answer concisely.',
prompt: 'Introduce San Francisco in one sentence',
});
console.log(result.text);API
isSupported(): Promise<{ supported: boolean; reason?: string }>generateText(options: { instructions: string; prompt: string; }): Promise<{ text: string }>cancel(): Promise<{ cancelled: boolean }>checkSpeechPermissions(): Promise<{ speech: PermissionState; microphone: PermissionState }>requestSpeechPermissions(): Promise<{ speech: PermissionState; microphone: PermissionState }>transcribeAudio(options: { audioPath: string; locale?: string }): Promise<{ text: string; isFinal: boolean; confidence?: number }>
Speech-to-Text
Real-time Microphone Recognition
import { AppleLLM } from 'capacitor-apple-llm';
// 1) Permissions
const perms1 = await AppleLLM.checkSpeechPermissions();
if (perms1.speech !== 'granted' || perms1.microphone !== 'granted') {
const perms2 = await AppleLLM.requestSpeechPermissions();
if (perms2.speech !== 'granted' || perms2.microphone !== 'granted') {
throw new Error('Speech or Microphone permission denied');
}
}
// 2) Start real-time listening
const listener = await AppleLLM.addListener('speechResult', ({ text, isFinal, confidence }) => {
console.log('Speech:', text, isFinal ? '(final)' : '(partial)');
if (isFinal) {
// Process final result
console.log('Final text:', text);
}
});
await AppleLLM.startListening({
locale: 'en-US',
partialResults: true
});
// 3) Stop when done
await AppleLLM.stopListening();
listener.remove();File Transcription
// Transcribe a local audio file
const result = await AppleLLM.transcribeAudio({
audioPath: 'file:///path/to/audio.m4a',
locale: 'en-US',
});
console.log(result.text);Note: Implementation follows Apple's API shape. If Apple exposes additional options in future SDKs, this plugin can be extended accordingly.
