Implementation:0xranx OpenContext AIContext generateReflection
| Knowledge Sources | |
|---|---|
| Domains | AI_Integration, Ideas |
| Last Updated | 2026-10-24 12:00 GMT |
Overview
Concrete tool for generating AI reflections on idea threads with streaming responses, resolving cross-references to documents and other ideas for context.
Description
The generateReflection function is a React context method that generates AI reflections on idea threads. It is a Pattern Doc implementation — it documents how the AI reflection interface works rather than a standalone API.
The reflection process:
- Context Preparation: The `prepareReflectionContext` function builds the message array:
- System prompt (configurable, defaults to a journaling reflection prompt)
- Language instruction (respond in user's preferred language)
- Plain text instruction (no HTML/Markdown formatting)
- User entries from the thread, with resolved cross-references
- Reference Resolution: For each entry, `buildReferenceNotes` extracts `oc://doc/` and `oc://idea/` links, resolves their metadata (document descriptions, idea thread titles), and appends reference notes to the entry text.
- Multimodal Support: Entries with images are formatted as multimodal messages with `image_url` content parts.
- Streaming: The `api.streamAIChat` function sends the messages to the configured AI provider and streams tokens via the `onToken` callback.
The default system prompt instructs the AI to: help the user reflect, not repeat their words, seed new ideas, encourage or debate, and keep responses concise but meaningful.
Usage
- Idea Journaling: Generate AI reflections on idea threads for self-reflection and idea development.
- Multimodal: Supports entries with text and images.
- Cross-referenced: Resolves document and idea references in entries for richer AI context.
Code Reference
Source Location
- Repository: OpenContext
- File: src/ui/src/context/AIContext.jsx
Signature
/**
* Generate AI reflection with streaming
* @param {Entry[]} threadEntries - Thread entries for context
* @param {(token: string) => void} onToken - Streaming token callback
* @returns {Promise<void>}
*/
const generateReflection = useCallback(async (threadEntries, onToken) => {
if (!isAvailable) {
throw new Error('AI not configured');
}
setIsGenerating(true);
const messages = await prepareReflectionContext(threadEntries);
try {
await api.streamAIChat(messages, (token) => {
onToken?.(token);
});
} finally {
setIsGenerating(false);
}
}, [isAvailable, prepareReflectionContext]);
Import
import { useAI } from '../context/AIContext';
// In a React component:
const { generateReflection, isAvailable, isGenerating } = useAI();
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| threadEntries | Entry[] | Yes | Thread entries for AI reflection context |
| onToken | Function | No | Streaming callback: (token: string) => void |
Configuration (from AIContext state)
| Name | Type | Default | Description |
|---|---|---|---|
| config.provider | string | openai | AI provider |
| config.model | string | gpt-4o | Model name |
| config.apiBase | string | https://api.openai.com/v1 | API base URL |
| config.prompt | string | (journaling prompt) | System prompt for reflection |
Outputs
| Name | Type | Description |
|---|---|---|
| (streaming) | void | Tokens are delivered via the onToken callback |
| isGenerating | boolean | Whether generation is in progress |
Usage Examples
Generate Reflection (React)
import { useAI } from '../context/AIContext';
function ReflectionComponent({ thread }) {
const { generateReflection, isGenerating, isAvailable } = useAI();
const [reflection, setReflection] = useState('');
const handleGenerate = async () => {
setReflection('');
await generateReflection(thread.entries, (token) => {
setReflection(prev => prev + token);
});
};
return (
<div>
<button onClick={handleGenerate} disabled={!isAvailable || isGenerating}>
{isGenerating ? 'Generating...' : 'Generate Reflection'}
</button>
<p>{reflection}</p>
</div>
);
}
With Cross-References
// If an entry contains oc://doc/ or oc://idea/ links,
// the reflection automatically resolves them:
// Entry: "Based on [design](oc://doc/abc-123), we should use LanceDB"
// AI receives: "Based on [design](oc://doc/abc-123), we should use LanceDB
// References:
// - Doc: design — System architecture overview · projects/architecture/design.md"