Implementation:0xranx OpenContext IOS AI Service
| Knowledge Sources | |
|---|---|
| Domains | AI, Reflection, iOS |
| Last Updated | 2026-10-24 00:00 GMT |
Overview
AI configuration and reflection service for the iOS app, managing AI settings via AsyncStorage and generating reflective responses via OpenAI-compatible chat completions API.
Description
The ai.js module in src-ios/services/ provides four functions: loadAIConfig (reads AI config from AsyncStorage with defaults), saveAIConfig (merges updates into stored config), isAIAvailable (checks if API key is set), and generateReflection (sends idea entries to an OpenAI-compatible chat completions endpoint and returns the AI's reflection text). The buildMessages helper constructs a system prompt with language constraints, and resolveLanguageLabel maps locale codes to human-readable language names for the prompt.
Usage
Import these functions in iOS settings and ideas screens to manage AI configuration and generate AI reflections on journal entries.
Code Reference
Source Location
- Repository: 0xranx_OpenContext
- File: src-ios/services/ai.js
- Lines: 1-95
Signature
async function loadAIConfig() // → { apiBase, model, apiKey, prompt }
async function saveAIConfig(updates) // → merged config
function isAIAvailable(config) // → boolean
async function generateReflection(entries, options = {}) // → string
function buildMessages(entries, config, lang) // → Array<{role, content}>
function resolveLanguageLabel(lang) // → string
Import
const { loadAIConfig, saveAIConfig, isAIAvailable, generateReflection } = require('./services/ai');
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| entries | Array | Yes | Idea entries with content/text |
| config | Object | No | AI config (loaded if not provided) |
| language | string | No | Locale code (e.g. "en", "zh") |
Outputs
| Name | Type | Description |
|---|---|---|
| generateReflection | string | AI reflection text |
| loadAIConfig | Object | { apiBase, model, apiKey, prompt } |
Usage Examples
Generate AI Reflection
const { loadAIConfig, isAIAvailable, generateReflection } = require('./services/ai');
const config = await loadAIConfig();
if (!isAIAvailable(config)) {
console.log('AI not configured');
return;
}
const entries = [
{ content: 'I had a productive day working on my project.' },
{ content: 'I feel good about the progress.' }
];
const reflection = await generateReflection(entries, { config, language: 'en' });
console.log(reflection);