|
| 1 | +--- |
| 2 | +sidebar_position: 3 |
| 3 | +--- |
| 4 | + |
| 5 | +# API Reference |
| 6 | + |
| 7 | +## LanguageModelSession |
| 8 | + |
| 9 | +### Constructor |
| 10 | + |
| 11 | +Creates a new LanguageModelSession instance. Throws an error if Apple Intelligence is not available. |
| 12 | + |
| 13 | +```typescript |
| 14 | +constructor(config?: LanguageModelSessionConfig) |
| 15 | +``` |
| 16 | + |
| 17 | +**Parameters**: |
| 18 | +- `config.instructions?: string` - System instructions defining AI behavior |
| 19 | +- `config.tools?: ToolDefinition[]` - Array of tools the AI can invoke |
| 20 | + |
| 21 | +### Instance Methods |
| 22 | + |
| 23 | +#### `streamResponse(prompt, callback)` |
| 24 | + |
| 25 | +Initiates a streaming response from the language model. |
| 26 | + |
| 27 | +```typescript |
| 28 | +streamResponse(prompt: string, callback: (fullResponse: string) => void): void |
| 29 | +``` |
| 30 | + |
| 31 | +**Parameters**: |
| 32 | +- `prompt: string` - The user's message |
| 33 | +- `callback: (fullResponse: string) => void` - Called with the complete response so far |
| 34 | + |
| 35 | +## Functions |
| 36 | + |
| 37 | +### `checkFoundationModelsAvailability()` |
| 38 | + |
| 39 | +Check if Apple Intelligence is available on the device. |
| 40 | + |
| 41 | +```typescript |
| 42 | +function checkFoundationModelsAvailability(): FoundationModelsAvailability |
| 43 | +``` |
| 44 | + |
| 45 | +**Returns**: Availability status object with `isAvailable`, `status`, and `message`. |
| 46 | + |
| 47 | +## Hooks |
| 48 | + |
| 49 | +### `useLanguageModel(config?)` |
| 50 | + |
| 51 | +React hook for managing language model sessions with automatic lifecycle management. |
| 52 | + |
| 53 | +```typescript |
| 54 | +function useLanguageModel(config?: UseLanguageModelConfig): UseLanguageModelReturn |
| 55 | +``` |
| 56 | + |
| 57 | +**Parameters**: |
| 58 | +- `config.instructions?: string` - System instructions for the AI |
| 59 | +- `config.tools?: ToolDefinition[]` - Tools the AI can use |
| 60 | +- `config.onResponse?: (response: string) => void` - Callback for responses |
| 61 | +- `config.onError?: (error: AppleAIError) => void` - Callback for errors |
| 62 | + |
| 63 | +**Returns**: |
| 64 | +- `session: LanguageModelSession | null` - Current session instance |
| 65 | +- `response: string` - Latest response from the AI |
| 66 | +- `loading: boolean` - Whether a request is in progress |
| 67 | +- `error: AppleAIError | null` - Last error that occurred |
| 68 | +- `send: (prompt: string) => Promise<string>` - Function to send messages |
| 69 | +- `reset: () => void` - Reset response and error state |
| 70 | +- `isSessionReady: boolean` - Whether session is initialized and ready |
| 71 | + |
| 72 | +### `useStreamingResponse(session)` |
| 73 | + |
| 74 | +Lower-level hook for streaming AI responses with more control. |
| 75 | + |
| 76 | +```typescript |
| 77 | +function useStreamingResponse(session: LanguageModelSession): UseStreamingResponseReturn |
| 78 | +``` |
| 79 | + |
| 80 | +**Parameters**: |
| 81 | +- `session: LanguageModelSession` - The session to use for streaming |
| 82 | + |
| 83 | +**Returns**: |
| 84 | +- `response: string` - Current response text |
| 85 | +- `isStreaming: boolean` - Whether currently streaming |
| 86 | +- `isComplete: boolean` - Whether streaming is complete |
| 87 | +- `error: AppleAIError | null` - Last error that occurred |
| 88 | +- `streamResponse: (prompt: string, options?: StreamingOptions) => Promise<string>` - Start streaming |
| 89 | +- `cancel: () => void` - Cancel current stream |
| 90 | +- `reset: () => void` - Reset state |
| 91 | + |
| 92 | +## Types |
| 93 | + |
| 94 | +### `FoundationModelsAvailability` |
| 95 | + |
| 96 | +```typescript |
| 97 | +interface FoundationModelsAvailability { |
| 98 | + isAvailable: boolean; |
| 99 | + status: AvailabilityStatus; |
| 100 | + message: string; |
| 101 | +} |
| 102 | +``` |
| 103 | + |
| 104 | +### `AvailabilityStatus` |
| 105 | + |
| 106 | +```typescript |
| 107 | +type AvailabilityStatus = |
| 108 | + | 'available' |
| 109 | + | 'unavailable.platformNotSupported' |
| 110 | + | 'unavailable.deviceNotEligible' |
| 111 | + | 'unavailable.appleIntelligenceNotEnabled' |
| 112 | + | 'unavailable.modelNotReady' |
| 113 | + | 'unavailable.unknown' |
| 114 | +``` |
| 115 | + |
| 116 | +### `LanguageModelSessionConfig` |
| 117 | + |
| 118 | +```typescript |
| 119 | +interface LanguageModelSessionConfig { |
| 120 | + instructions?: string; |
| 121 | + tools?: ToolDefinition[]; |
| 122 | +} |
| 123 | +``` |
| 124 | + |
| 125 | +### `ToolDefinition` |
| 126 | + |
| 127 | +```typescript |
| 128 | +interface ToolDefinition { |
| 129 | + name: string; |
| 130 | + description: string; |
| 131 | + arguments: AnyMap; |
| 132 | + handler: (args: AnyMap) => Promise<AnyMap>; |
| 133 | +} |
| 134 | +``` |
| 135 | + |
| 136 | +### `AppleAIError` |
| 137 | + |
| 138 | +```typescript |
| 139 | +class AppleAIError extends Error { |
| 140 | + readonly code: string; |
| 141 | + readonly details?: Record<string, any>; |
| 142 | +
|
| 143 | + constructor(code: string, message: string, details?: Record<string, any>); |
| 144 | + static fromErrorInfo(errorInfo: AppleAIErrorInfo): AppleAIError; |
| 145 | + toErrorInfo(): AppleAIErrorInfo; |
| 146 | +} |
| 147 | +``` |
| 148 | + |
| 149 | +#### Error Codes |
| 150 | + |
| 151 | +- `SESSION_NOT_INITIALIZED` - Session is not ready |
| 152 | +- `TOOL_CALL_ERROR` - Tool call failed |
| 153 | +- `TOOL_EXECUTION_ERROR` - Tool execution failed |
| 154 | +- `SCHEMA_CREATION_ERROR` - Failed to create tool schema |
| 155 | +- `ARGUMENT_PARSING_ERROR` - Failed to parse tool arguments |
| 156 | +- `RESPONSE_PARSING_ERROR` - Failed to parse tool response |
| 157 | +- `UNKNOWN_TOOL_ERROR` - Unknown tool referenced |
| 158 | +- `SESSION_STREAMING_ERROR` - Streaming failed |
| 159 | +- `UNSUPPORTED_PLATFORM` - Platform not supported |
| 160 | + |
| 161 | +### `StreamingOptions` |
| 162 | + |
| 163 | +```typescript |
| 164 | +interface StreamingOptions { |
| 165 | + onToken?: (token: string) => void; |
| 166 | + onComplete?: (fullResponse: string) => void; |
| 167 | + onError?: (error: AppleAIError) => void; |
| 168 | +} |
| 169 | +``` |
| 170 | + |
| 171 | +## Utilities |
| 172 | + |
| 173 | +### `createTool(definition)` |
| 174 | + |
| 175 | +Helper function to create type-safe tools with Zod schema validation. |
| 176 | + |
| 177 | +```typescript |
| 178 | +function createTool<T extends ZodObjectSchema>(definition: { |
| 179 | + name: string; |
| 180 | + description: string; |
| 181 | + arguments: T; |
| 182 | + handler: (params: z.infer<T>) => Promise<AnyMap>; |
| 183 | +}): ToolDefinition |
| 184 | +``` |
| 185 | + |
| 186 | +### `isAppleAIError(error)` |
| 187 | + |
| 188 | +Type guard to check if an error is an AppleAIError. |
| 189 | + |
| 190 | +```typescript |
| 191 | +function isAppleAIError(error: any): error is AppleAIError |
| 192 | +``` |
| 193 | + |
| 194 | +### `parseNativeError(error)` |
| 195 | + |
| 196 | +Parse native errors into AppleAIError instances. |
| 197 | + |
| 198 | +```typescript |
| 199 | +function parseNativeError(error: any): AppleAIError |
| 200 | +``` |
0 commit comments