|
| 1 | +--- |
| 2 | +sync external: ../jsbrains/smart-environment/README.md |
| 3 | +--- |
| 4 | +# Smart Environment |
| 5 | + |
| 6 | +Welcome to Smart Environment, the efficient, productive, and user-aligned AI-powered ecosystem for personal and professional use. Smart Environment empowers individuals by providing a suite of AI tools in a private, secure, and open-source software package. |
| 7 | + |
| 8 | +- [Intro to Smart Environments video](https://youtu.be/0obRntW8Cto) |
| 9 | + |
| 10 | +## Key Features |
| 11 | + |
| 12 | +- **Local-first**: Your data stays on your device under your control |
| 13 | +- **AI Oversight**: Review changes made by AI to maintain alignment |
| 14 | +- **Privacy**: Limit data-sharing to only what's necessary |
| 15 | +- **Context Profiles**: Easily switch between home, work, and project configurations |
| 16 | +- **Modular Architecture**: Seamlessly integrate various AI-powered Smart Modules |
| 17 | +- **Complements Foundation Models**: Enhances capabilities of large language models |
| 18 | + |
| 19 | +## Overview |
| 20 | + |
| 21 | +Smart Environment is designed to be your hub for enabling personal alignment with AI. It provides an efficient, productive environment with AI-powered tools that put the user first. |
| 22 | + |
| 23 | +Key principles: |
| 24 | +- Maintain an open, interdependent architecture |
| 25 | +- Orchestrate modular Smart Modules |
| 26 | +- Complement and enhance foundation models |
| 27 | +- Prioritize user agency, privacy and oversight |
| 28 | +- Loads collections (wake-up) |
| 29 | + |
| 30 | +## Architecture |
| 31 | + |
| 32 | +Smart Environment enables individuals and teams to leverage the power of AI while maintaining control over their data and workflows. Designed for extensibility, it provides a framework for integrating AI capabilities into your existing tools and processes. |
| 33 | + |
| 34 | +The SmartEnv class uses a singleton pattern to ensure that only one instance of Smart Environment is created. This instance is accessible through a global reference, which is either `window` or `global`, depending on the environment. |
| 35 | + |
| 36 | +## Interface |
| 37 | + |
| 38 | +### `static async create(main, main_env_opts={})` |
| 39 | + |
| 40 | +Creates or updates a SmartEnv instance. |
| 41 | + |
| 42 | +- `main`: The main object to be added to the SmartEnv instance. |
| 43 | +- `main_env_opts`: Options for configuring the SmartEnv instance. |
| 44 | + |
| 45 | +Returns: The SmartEnv instance. |
| 46 | + |
| 47 | +### `constructor(opts={})` |
| 48 | + |
| 49 | +Initializes a new SmartEnv instance. |
| 50 | + |
| 51 | +- `opts`: Configuration options for the SmartEnv instance. |
| 52 | + |
| 53 | +### `async init(main, main_env_opts = {})` |
| 54 | + |
| 55 | +Initializes the SmartEnv instance with the provided main object and options. |
| 56 | + |
| 57 | +### `init_main(main, main_env_opts = {})` |
| 58 | + |
| 59 | +Adds a new main object to the SmartEnv instance. |
| 60 | + |
| 61 | +### `async load_main(main_key)` |
| 62 | + |
| 63 | +Loads the main object and its associated collections. |
| 64 | + |
| 65 | +### `async init_collections(config=this.opts)` |
| 66 | + |
| 67 | +Initializes collections based on the provided configuration. |
| 68 | + |
| 69 | +### `async load_collections(collections=this.collections)` |
| 70 | + |
| 71 | +Loads the specified collections. |
| 72 | + |
| 73 | +### `merge_env_config(opts)` |
| 74 | + |
| 75 | +Merges provided options into the SmartEnv instance, performing a deep merge for objects. |
| 76 | + |
| 77 | +### `unload_main(main_key)` |
| 78 | + |
| 79 | +Unloads a main object and its associated collections and options. |
| 80 | + |
| 81 | +### `save()` |
| 82 | + |
| 83 | +Saves all collections in the SmartEnv instance. |
| 84 | + |
| 85 | +### `init_module(module_key, opts={})` |
| 86 | + |
| 87 | +Initializes a module with the given key and options. |
| 88 | + |
| 89 | +### `async render_settings(container=this.settings_container)` |
| 90 | + |
| 91 | +Renders the settings UI for the SmartEnv instance. |
| 92 | + |
| 93 | +### `async save_settings(settings)` |
| 94 | + |
| 95 | +Saves the current settings to the file system. |
| 96 | + |
| 97 | +### `async load_settings()` |
| 98 | + |
| 99 | +Loads the settings from the file system. |
| 100 | + |
| 101 | +## Rendering Settings |
| 102 | + |
| 103 | +The Smart Environment provides a flexible and extensible way to render settings for all collections and Smart Modules. This is achieved through the `render_settings` method. |
| 104 | + |
| 105 | +### `render_settings(opts={})` |
| 106 | + |
| 107 | +This method is responsible for rendering the settings UI for each collection and Smart Module. It utilizes a template-based approach for maximum flexibility. |
| 108 | + |
| 109 | +#### Process Overview: |
| 110 | + |
| 111 | +1. **Template Creation**: The method starts by creating a document fragment using the `settings_template` from `./components/settings.js`. |
| 112 | + |
| 113 | +2. **Collection Settings**: It iterates through all initialized collections and calls their individual `render_settings` methods. |
| 114 | + |
| 115 | +3. **Smart Module Settings**: If any Smart Modules have settings, they are also rendered. |
| 116 | + |
| 117 | +4. **Container Handling**: If a container is provided in the options, the rendered settings are appended to it. |
| 118 | + |
| 119 | +### Settings Template |
| 120 | + |
| 121 | +The settings template (`./components/settings.js`) is responsible for structuring the overall settings UI: |
| 122 | + |
| 123 | +This template: |
| 124 | +1. Creates a document fragment |
| 125 | +2. Renders general setting components |
| 126 | +3. Iterates through collections, creating containers for each if not already present |
| 127 | + - looks for containers based on id `{collection_key}_settings` |
| 128 | + - if container has `data-settings-keys` attribute, it will only render the settings keys specified |
| 129 | +4. Calls each collection's `render_settings` method |
| 130 | +5. Renders additional setting components if needed |
| 131 | + |
| 132 | +### Collection-Specific Settings |
| 133 | + |
| 134 | +Each collection is expected to implement its own `render_settings` method. This method should populate the provided container with the collection's specific settings UI. |
| 135 | + |
| 136 | +### Customization |
| 137 | + |
| 138 | +The `render_settings` method and its associated template can be easily customized to fit specific UI requirements or to add additional functionality. This could include: |
| 139 | + |
| 140 | +- Adding global settings sections |
| 141 | +- Implementing tabbed interfaces for different setting categories |
| 142 | +- Incorporating real-time setting updates |
| 143 | +- Adding validation and error handling for setting inputs |
| 144 | + |
| 145 | +By leveraging this flexible architecture, Smart Environment ensures that settings for all components can be easily managed and displayed in a cohesive, user-friendly interface. |
| 146 | + |
| 147 | +## Configuration: `smart_env.config.js` and `main_env_opts` |
| 148 | + |
| 149 | +Smart Environment uses a configuration system based on two key concepts: |
| 150 | + |
| 151 | +1. **smart_env.config.js** - A static configuration file that defines the default environment setup |
| 152 | +2. **main_env_opts** - Runtime options passed during initialization |
| 153 | + |
| 154 | +The Smart Environment relies on a configuration object, `main_env_opts`, during initialization to set up the environment according to your specific needs. This configuration defines collections, modules, settings, and other options that the Smart Environment uses to orchestrate its components. |
| 155 | + |
| 156 | +The `smart_env.config.js` file defines the core configuration for your Smart Environment instance. This file should export a configuration object that defines: |
| 157 | + |
| 158 | +### Understanding `smart_env.config.js` |
| 159 | + |
| 160 | +The `smart_env.config.js` file is the primary place where you define your `main_env_opts`. This file exports a configuration object that specifies how the Smart Environment should be set up. By organizing your configuration in this file, you maintain a clear separation between your application's logic and its configuration, making it easier to manage and scale. |
| 161 | + |
| 162 | +- **`env_path`**: Specifies the base path for your environment. This could be the root directory of your application or any other path where your environment's data will reside. |
| 163 | +- **`collections`**: Defines the collections that the Smart Environment will manage. Each collection should specify its class and any necessary adapters or item types. |
| 164 | +- **`modules`**: Specifies the modules (also known as Smart Modules) to be included in the environment. Each module should define its class and any necessary adapters. |
| 165 | +- **`default_settings`**: Provides default settings for the environment, which can be overridden by user settings. |
| 166 | +- **`components`**: Defines UI components for settings, connections, or other interactive elements. |
| 167 | + |
| 168 | +### Using `main_env_opts` in Initialization |
| 169 | + |
| 170 | +When initializing the Smart Environment, you need to provide `main_env_opts` to the `SmartEnv.create` method. This configuration object should be imported from your `smart_env.config.js` file. |
| 171 | + |
| 172 | + |
| 173 | +### Importance of `main_env_opts` |
| 174 | + |
| 175 | +The `main_env_opts` object is essential for initializing the Smart Environment. It provides all the necessary configurations, including paths, collections, modules, settings, and components. By defining `main_env_opts` in the `smart_env.config.js` file, you achieve: |
| 176 | + |
| 177 | +- **Modularity**: Keep configuration separate from application logic. |
| 178 | +- **Reusability**: Easily share and reuse configurations across different projects or environments. |
| 179 | +- **Maintainability**: Simplify updates and changes to the configuration without modifying the core application code. |
| 180 | + |
| 181 | +### Multiple Mains and Configurations |
| 182 | + |
| 183 | +If your application uses multiple mains (primary objects) or needs to manage different configurations, you can define separate `smart_env.config.js` files for each main. The Smart Environment will merge these configurations appropriately. |
| 184 | + |
| 185 | +### Normalizing Options |
| 186 | + |
| 187 | +The Smart Environment internally normalizes the options provided in `main_env_opts` to ensure consistency. This includes: |
| 188 | + |
| 189 | +- **Key Formatting**: Converts camelCase keys to snake_case to maintain a consistent naming convention. |
| 190 | +- **Class Definitions**: Ensures that collections and modules are properly defined with a `class` property. |
| 191 | +- **Deep Merging**: Merges nested objects without overwriting existing properties unless specified. |
| 192 | + |
| 193 | +### Key Components of `main_env_opts` |
| 194 | + |
| 195 | +- **`env_path`**: The base path for the environment, important for file system operations. |
| 196 | +- **`collections`**: Defines the data collections managed by the environment. |
| 197 | + - Each collection should have: |
| 198 | + - **`class`**: The constructor function or class for the collection. |
| 199 | + - **`data_adapter`**: Adapter for handling data persistence. |
| 200 | + - **`item_types`**: Definitions of item classes within the collection. |
| 201 | +- **`modules`**: Specifies additional functionalities or services. |
| 202 | + - Each module should have: |
| 203 | + - **`class`**: The constructor function or class for the module. |
| 204 | + - **`adapter`**: Adapter specific to the module's operation. |
| 205 | +- **`default_settings`**: Default configuration settings that can be overridden by user preferences. |
| 206 | +- **`components`**: UI components for rendering settings, views, and other interactive elements. |
| 207 | + |
| 208 | +```js |
| 209 | +export const smart_env_config = { |
| 210 | + |
| 211 | + // Base path for the environment |
| 212 | + env_path: '', |
| 213 | + |
| 214 | + // Collections to initialize |
| 215 | + collections: { |
| 216 | + smart_sources: { |
| 217 | + class: SmartSources, |
| 218 | + data_adapter: SmartCollectionMultiFileDataAdapter, |
| 219 | + // Collection-specific options... |
| 220 | + }, |
| 221 | + // Other collections... |
| 222 | + }, |
| 223 | + |
| 224 | + // Available item types |
| 225 | + item_types: { |
| 226 | + SmartSource, |
| 227 | + SmartBlock, |
| 228 | + }, |
| 229 | + |
| 230 | + // Module configurations |
| 231 | + modules: { |
| 232 | + smart_fs: { |
| 233 | + class: SmartFs, |
| 234 | + adapter: SmartFsAdapter, |
| 235 | + }, |
| 236 | + smart_view: { |
| 237 | + class: SmartView, |
| 238 | + adapter: ViewAdapter, |
| 239 | + }, |
| 240 | + // Other modules... |
| 241 | + }, |
| 242 | + |
| 243 | + // Default settings |
| 244 | + default_settings: { |
| 245 | + file_exclusions: 'Untitled', |
| 246 | + folder_exclusions: 'smart-chats', |
| 247 | + // Other default settings... |
| 248 | + } |
| 249 | +}; |
| 250 | +``` |
| 251 | + |
| 252 | +### Runtime Options (main_env_opts) |
| 253 | + |
| 254 | +When initializing Smart Environment, you must provide runtime options through `main_env_opts`. These options typically import and extend the base configuration: |
| 255 | + |
| 256 | +```js |
| 257 | +import { smart_env_config } from './smart_env.config.js'; |
| 258 | + |
| 259 | +const main_env_opts = { |
| 260 | + ...smart_env_config, |
| 261 | + // Override or add runtime-specific options |
| 262 | + env_path: '/custom/path', |
| 263 | + collections: { |
| 264 | + ...smart_env_config.collections, |
| 265 | + // Add or modify collections |
| 266 | + } |
| 267 | +}; |
| 268 | + |
| 269 | +// Initialize Smart Environment |
| 270 | +const env = await SmartEnv.create(main, main_env_opts); |
| 271 | +``` |
| 272 | + |
| 273 | +### Configuration Merging |
| 274 | + |
| 275 | +Smart Environment merges configurations in this order: |
| 276 | + |
| 277 | +1. Default internal options |
| 278 | +2. Base configuration from smart_env.config.js |
| 279 | +3. Runtime options from main_env_opts |
| 280 | +4. Settings loaded from the environment's data directory |
| 281 | + |
| 282 | +This allows for flexible configuration while maintaining sensible defaults. |
| 283 | + |
| 284 | +### Required Configuration |
| 285 | + |
| 286 | +At minimum, your configuration should specify: |
| 287 | + |
| 288 | +- **collections**: The collection classes to initialize |
| 289 | +- **modules**: Core modules like smart_fs, smart_view |
| 290 | +- **item_types**: Available item types for collections |
| 291 | +- **env_path**: Base path for the environment (if not using default) |
| 292 | + |
| 293 | +Example of minimal configuration: |
| 294 | + |
| 295 | +```js |
| 296 | +const minimal_config = { |
| 297 | + collections: { |
| 298 | + smart_sources: { |
| 299 | + class: SmartSources, |
| 300 | + data_adapter: DataAdapter |
| 301 | + } |
| 302 | + }, |
| 303 | + modules: { |
| 304 | + smart_fs: { |
| 305 | + class: SmartFs, |
| 306 | + adapter: FsAdapter |
| 307 | + } |
| 308 | + }, |
| 309 | + item_types: { |
| 310 | + SmartSource |
| 311 | + } |
| 312 | +}; |
| 313 | +``` |
| 314 | + |
| 315 | +## Usage |
| 316 | + |
| 317 | +```js |
| 318 | +import { SmartEnv } from 'smart-environment'; |
| 319 | + |
| 320 | +class MyApp { |
| 321 | + constructor() { |
| 322 | + this.init(); |
| 323 | + } |
| 324 | + |
| 325 | + async init() { |
| 326 | + this.env = await SmartEnv.create(this, { |
| 327 | + env_path: '/path/to/my/app', |
| 328 | + collections: { |
| 329 | + // Define your collections here |
| 330 | + }, |
| 331 | + modules: { |
| 332 | + // Define your modules here |
| 333 | + } |
| 334 | + }); |
| 335 | + |
| 336 | + // Your app initialization code here |
| 337 | + } |
| 338 | +} |
| 339 | + |
| 340 | +const app = new MyApp(); |
| 341 | +``` |
| 342 | + |
0 commit comments