Overview
Concrete tool for initializing the OpenContext environment, creating the contexts directory and SQLite database, provided by the native store adapter.
Description
The initEnvironment function initializes the OpenContext knowledge base environment. It derives the contexts root and database paths from environment variables (`OPENCONTEXT_ROOT`, `OPENCONTEXT_CONTEXTS_ROOT`, `OPENCONTEXT_DB_PATH`) or defaults to `~/.opencontext/contexts` and `~/.opencontext/opencontext.db`.
The Rust core (`crates/opencontext-core/src/lib.rs`) creates the directories and initializes the SQLite database with the required schema (folders, docs tables). The function is idempotent — running it multiple times is safe and returns the same paths.
Usage
- Agent Integration Setup: Called at the start of `oc init` to ensure the environment is ready.
- First-time setup: Creates the directory structure and database for a new installation.
- Verification: Returns the paths so subsequent operations know where the knowledge base lives.
Code Reference
Source Location
- Repository: OpenContext
- File (JS adapter): src/core/store-native.js
- File (Rust core): crates/opencontext-core/src/lib.rs
- File (CLI): bin/oc.js
Signature
/**
* Initialize environment and return paths
* @returns {{ contextsRoot: string, dbPath: string }}
*/
function initEnvironment() {
const info = handleResult(native.get().initEnvironment());
return {
contextsRoot: info.contexts_root,
dbPath: info.db_path,
};
}
Import
const { initEnvironment } = require('./core/store-native');
I/O Contract
Inputs
| Name |
Type |
Required |
Description
|
| (none) |
— |
— |
Uses environment variables or defaults
|
Outputs
| Name |
Type |
Description
|
| returns.contextsRoot |
string |
Path to the contexts directory
|
| returns.dbPath |
string |
Path to the SQLite database file
|
Environment Variables
| Variable |
Default |
Description
|
| OPENCONTEXT_ROOT |
~/.opencontext |
Base OpenContext directory
|
| OPENCONTEXT_CONTEXTS_ROOT |
$OPENCONTEXT_ROOT/contexts |
Contexts directory
|
| OPENCONTEXT_DB_PATH |
$OPENCONTEXT_ROOT/opencontext.db |
SQLite database path
|
Usage Examples
Initialize Environment (CLI)
oc init # Calls initEnvironment internally as the first step
Initialize Environment (JavaScript)
const { initEnvironment } = require('./core/store-native');
const { contextsRoot, dbPath } = initEnvironment();
console.log(`Contexts root: ${contextsRoot}`);
console.log(`Database: ${dbPath}`);
With Custom Paths
export OPENCONTEXT_ROOT=/data/opencontext
oc init # Uses /data/opencontext/contexts and /data/opencontext/opencontext.db
Related Pages
Implements Principle
Requires Environment
Page Connections
Double-click a node to navigate. Hold to expand connections.