Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Implementation:0xranx OpenContext AIContext generateReflection

From Leeroopedia
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:

  1. 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
  2. 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.
  3. Multimodal Support: Entries with images are formatted as multimodal messages with `image_url` content parts.
  4. 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"

Related Pages

Implements Principle

Requires Environment

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment