@ratley/react-native-apple-foundation-models
Version:
Access Apple’s on-device Foundation Models (text + image AI)
94 lines • 3.2 kB
TypeScript
/**
* Parameters for the `useLLMSession` React hook.
*
* - `instructions`: Optional system prompt to steer generations.
* - `initialId`: Provide to resume an existing session by id.
* - `autoCreate`: Lazily create a session on mount if supported. Defaults to `true`.
*/
export type UseLLMSessionParams = {
instructions?: string;
initialId?: string;
autoCreate?: boolean;
};
/**
* Return shape for the `useLLMSession` React hook.
*/
export type UseLLMSessionReturn = {
/**
* Current session identifier, if available.
*/
sessionId?: string;
/**
* Whether the platform supports local text generation.
*/
isAvailable: boolean;
/**
* Lifecycle status of the hook: `idle`, `running`, `error`, or `unsupported`.
*/
status: "idle" | "running" | "error" | "unsupported";
/**
* Normalized error, when present.
*/
error?: {
code: string;
message: string;
};
/**
* In-memory chat history for the current session.
*/
history: Array<{
role: "user" | "assistant";
content: string;
}>;
/**
* Send a prompt to the model and append the response to `history`.
*
* @param prompt User message to generate from.
* @param options Optional generation controls.
* @returns The generated text.
* @throws Normalized error if unsupported or native call fails.
* @see https://developer.apple.com/documentation/foundationmodels/languagemodelsession
*/
ask: (prompt: string, options?: {
temperature?: number;
maxOutputTokens?: number;
}) => Promise<string>;
/**
* Re-run the last prompt, if any, and append the new response.
*/
regenerate: () => Promise<string>;
/**
* Update the system instructions for subsequent generations.
*/
setInstructions: (value?: string) => void;
/**
* Reset session state and optionally apply new instructions.
*/
reset: (value?: {
instructions?: string;
}) => void;
/**
* Forget the current session and clear history. A new session will be
* created lazily on the next call to `ask()` if supported.
*/
destroy: () => void;
};
/**
* React hook that manages a local language model session, providing
* availability detection, lifecycle state, in-memory history, and convenient
* helpers for generation. Conceptually aligned with Apple's
* `LanguageModelSession`, but adapted for React usage and this module's
* surface area.
*
* The hook performs a support check on mount via `isTextModelAvailable()`. If
* supported and `autoCreate` is true, a session is created lazily so callers
* can immediately call `ask()`.
*
* @param params Optional configuration for initial instructions, an initial
* session id, and automatic creation behavior.
* @returns `UseLLMSessionReturn` with helpers and state.
* @see https://developer.apple.com/documentation/foundationmodels/languagemodelsession
*/
export declare function useLLMSession({ instructions, initialId, autoCreate, }?: UseLLMSessionParams): UseLLMSessionReturn;
export default useLLMSession;
//# sourceMappingURL=useLLMSession.d.ts.map