use-vibes 0.2.7 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/README.md +142 -244
  2. package/package.json +40 -49
package/README.md CHANGED
@@ -1,304 +1,202 @@
1
- # useVibes - App Gen Anywhere
1
+ # use-vibes
2
2
 
3
- [![npm version](https://img.shields.io/npm/v/use-vibes.svg)](https://www.npmjs.com/package/use-vibes)
4
- [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
5
-
6
- useVibes is a vanilla browser TypeScript module that transforms any DOM element into an AI-augmented micro-app using an HTML inject-first approach. It leverages the current page's HTML context to create a rich environment for dynamic content generation, powered by the call-ai library.
7
-
8
- ---
9
-
10
- ## Overview
11
-
12
- - **Purpose**: Transform any DOM element into a self-contained, AI-driven micro-app by extracting and utilizing the page's inherent state.
13
- - **Core Features**:
14
- - **Inject-First Approach**: Operates directly on the existing page by capturing document.body.innerHTML, the associated CSS, and a visual snapshot via html2canvas.
15
- - **Context-Aware Transformation**: Uses the page's current state as a germ to generate dynamic content.
16
- - **Promise-Based API**: Returns an app instance once the micro-app is injected.
17
- - **Ongoing Chat Interface**: The app instance provides a chat interface to facilitate live, interactive sessions.
18
-
19
- ---
3
+ A lightweight library that transforms any DOM element into an AI-powered micro-app.
20
4
 
21
5
  ## Installation
22
6
 
23
- ### npm
24
-
25
- ```bash
26
- npm install use-vibes
27
- ```
28
-
29
- ### yarn
30
-
31
- ```bash
32
- yarn add use-vibes
33
- ```
34
-
35
- ### pnpm
36
-
37
7
  ```bash
38
8
  pnpm add use-vibes
39
9
  ```
40
10
 
41
- ## Basic Usage
11
+ ### CSS Loading
42
12
 
43
- ```javascript
44
- import { useVibes } from 'use-vibes';
13
+ The ImgGen component requires CSS styles. You can include them in two ways:
45
14
 
46
- // Set the API key for call-ai (required)
47
- window.CALLAI_API_KEY = 'your-api-key-here';
15
+ #### Option A: Explicit CSS link (recommended for production)
48
16
 
49
- // Get target element (can be a CSS selector string or DOM element)
50
- const target = document.getElementById('my-app-container');
17
+ Add a CSS link tag to your HTML:
51
18
 
52
- // Apply useVibes to the target with a prompt
53
- useVibes(target, {
54
- prompt: 'Create a beautiful hello world message with blue styling'
55
- }).then(app => {
56
- console.log('App created:', app);
57
- }).catch(error => {
58
- console.error('Error creating app:', error);
59
- });
19
+ ```html
20
+ <link rel="stylesheet" href="/node_modules/use-vibes/dist/components/ImgGen.css" />
60
21
  ```
61
22
 
62
- ### Return Value
63
-
64
- The function returns a Promise that resolves to an app instance with the following properties:
65
-
66
- - **container**: The DOM element into which the micro-app has been injected
67
- - **database**: An optional property for state management (may be undefined)
68
-
69
- ## Browser Usage
70
-
71
- ### IIFE Bundle
72
-
73
- If you want to use useVibes directly in the browser without a build step, you can use the IIFE bundle:
23
+ Or for ESM/CDN environments like importmap scenarios:
74
24
 
75
25
  ```html
76
- <script src="https://unpkg.com/use-vibes@latest/lib/use-vibes.iife.js"></script>
77
- <script>
78
- // Set API key
79
- window.CALLAI_API_KEY = 'your-api-key-here';
80
-
81
- // Now the global useVibes function is available
82
- useVibes(document.getElementById('my-element'), {
83
- prompt: 'Create a beautiful hello world message'
84
- });
85
- </script>
26
+ <link rel="stylesheet" href="https://esm.sh/use-vibes@0.4.0/components/ImgGen.css" />
86
27
  ```
87
28
 
88
- ### Bookmarklet
89
-
90
- The useVibes package also includes a bookmarklet that allows you to test useVibes on any website:
91
-
92
- 1. After installing the package, you'll find a `bookmarklet.html` file in the `dist` directory
93
- 2. Open this file in your browser
94
- 3. Drag the bookmarklet link to your bookmarks bar
95
- 4. Navigate to any website, click the bookmarklet, then click on any element you want to enhance
96
- 5. Enter your prompt when prompted
97
-
98
- You'll need to edit the bookmarklet to include your API key. See the HTML file for detailed instructions.
99
-
100
- ### Example
101
-
102
- ```javascript
103
- import { useVibes } from 'useVibes';
104
-
105
- useVibes("#app", { prompt: "create a todo list with emojis" })
106
- .then((app) => {
107
- console.log("Micro-app created successfully!");
29
+ #### Option B: Automatic CSS loading (convenient for prototyping)
108
30
 
109
- // Log the container element
110
- console.log("Injected into:", app.container);
31
+ Import the style-loader early in your application:
111
32
 
112
- // If a database is later configured, it can be accessed via app.database
113
- if (app.database) {
114
- console.log("Database configured as:", app.database);
115
- }
116
-
117
- // Use the chat interface to send a message to the AI
118
- app.chat.sendMessage("Hello, vibe!")
119
- .then(() => console.log("Message sent successfully"))
120
- .catch((err) => console.error("Error sending message:", err));
121
- })
122
- .catch((error) => {
123
- console.error("Error during injection:", error);
124
- });
33
+ ```js
34
+ import 'use-vibes/style-loader'; // Auto-loads CSS when imported
125
35
  ```
126
36
 
127
- ---
128
-
129
- ## Quick Start
130
-
131
- 1. **Include the Module**: Import or bundle useVibes as an ESM module in your project.
132
- 2. **Prepare Your HTML**: Ensure your page includes a target element (e.g., `<div id="app"></div>`) for the micro-app.
133
- 3. **Initialize useVibes**: Call useVibes with your target and configuration. The module will capture the page's state, transform it based on your prompt, and inject the resulting micro-app into your target element.
134
-
135
- ---
136
-
137
- ## Architecture Overview
138
-
139
- - **Purpose**: Build a lightweight, agentic editor that transforms any div into a dynamic micro-app. The module leverages a minimal core—using Fireproof for local persistence and callAi for AI interactions—while preserving the page's inherent structure and style.
140
- - **Architecture**:
141
- - **HTML Injection-First**: The library is injected into a page and operates on the current DOM state.
142
- - **Vanilla Browser Module**: Written in TypeScript and built as an ESM module suitable for distribution via esm.sh/jsr style.
143
-
144
- ---
37
+ This approach is perfect for quick prototypes but for production sites, Option A gives you better control over CSS loading order and timing.
145
38
 
146
- ## Testing
39
+ ## Components
147
40
 
148
- The useVibes library includes a comprehensive testing suite to ensure functionality across different browsers and use cases.
41
+ ### ImgGen
149
42
 
150
- ### Browser Testing
43
+ A React component for generating images with AI:
151
44
 
152
- Browser tests are implemented using Playwright and run on Chrome, Firefox, and WebKit (Safari). The test infrastructure uses a custom build process to create a test bundle with mocked dependencies, allowing tests to run without making actual API calls.
45
+ ```jsx
46
+ import { ImgGen } from 'use-vibes';
153
47
 
154
- ```bash
155
- # Run browser tests
156
- pnpm test:browser
48
+ function MyComponent() {
49
+ return (
50
+ <div>
51
+ <ImgGen prompt="A sunset over mountains" />
52
+ </div>
53
+ );
54
+ }
157
55
  ```
158
56
 
159
- This command will:
160
- 1. Build a test-specific bundle using the `build-browser-test.js` script
161
- 2. Set up browser mocks for all three browser engines
162
- 3. Run the Playwright tests with proper reporting
163
-
164
- ### Unit Testing
57
+ #### Props
165
58
 
166
- ```bash
167
- # Run unit tests
168
- pnpm test:unit
169
- ```
59
+ - `prompt`: Text prompt for image generation (required unless `_id` is provided)
60
+ - `_id`: Document ID to load a specific image instead of generating a new one
61
+ - `options`: Options for image generation (optional)
62
+ - `className`: CSS class name for the image element (optional)
63
+ - `alt`: Alt text for the image (defaults to prompt)
64
+ - `overlay`: Whether to show overlay controls and info button (default: `true`)
65
+ - `database`: Database name or instance to use for storing images (default: `'ImgGen'`)
66
+ - `onLoad`: Callback when image load completes successfully
67
+ - `onError`: Callback when image load fails, receives the error as parameter
68
+ - `onDelete`: Callback when an image is deleted, receives the document ID
69
+ - `onPromptEdit`: Callback when the prompt is edited, receives document ID and new prompt
70
+ - `editedPrompt`: Custom prompt text to use for regeneration (overrides document prompt)
71
+ - `generationId`: Optional ID to trigger regeneration of existing images
72
+ - `classes`: Object containing custom CSS classes for styling component parts (see Styling section)
170
73
 
171
- ### Linting and TypeScript Validation
74
+ #### Features
172
75
 
173
- ```bash
174
- # Check types
175
- pnpm typecheck
76
+ ##### Overlay Controls
176
77
 
177
- # Run linter
178
- pnpm lint
78
+ By default, the ImgGen component shows an info button in the bottom-left corner. Clicking it reveals an overlay with:
179
79
 
180
- # Run linter with auto-fixes
181
- pnpm lint --fix
182
- ```
80
+ - The prompt text (double-clickable to edit)
81
+ - Version navigation buttons (if multiple versions exist)
82
+ - Refresh button to generate a new version
83
+ - Delete button
183
84
 
184
- ## Directory Structure
85
+ Setting `overlay={false}` will hide all these controls, showing only the image.
185
86
 
186
- - **src/**: Contains the source code for the useVibes library.
187
- - **fixtures/**: A collection of HTML challenge files. These fixtures serve as test cases for validating that useVibes can handle a variety of page structures.
188
- - **tests/**: Test suites including browser and unit tests.
189
- - **browser/**: Playwright browser tests with mock implementations.
190
- - **unit/**: Unit tests for core functionality.
191
- - **scripts/**: Build scripts and utilities.
192
- - **docs/**: Documentation directory which includes:
193
- - **llms.txt**: A text file specifying the details and technical context for LLM integrations and other project guidelines.
87
+ ##### Prompt Editing
194
88
 
195
- ---
89
+ Double-click the prompt text in the overlay to edit it. Press Enter to submit changes and regenerate the image with the new prompt.
196
90
 
197
- ## Prerequisites
91
+ ##### Image Regeneration
198
92
 
199
- - Node.js (v14 or higher)
200
- - pnpm package manager
201
- - A modern web browser for testing
93
+ When regenerating images, the component will:
94
+ - Use the edited prompt if one has been provided (via `editedPrompt` prop, `onPromptEdit` callback, or direct editing in the UI)
95
+ - Fall back to the original prompt from the document if no edits were made
96
+ - Preserve versions under the same document ID to maintain history
97
+ - Add the new image as a version to the existing document instead of creating a new one
202
98
 
203
- ---
99
+ This allows for iterative refinement of images while maintaining version history.
204
100
 
205
- ## Getting Started
101
+ #### Styling
206
102
 
207
- 1. **Clone the Repository**:
208
- ```
209
- git clone https://github.com/fireproof-storage/use-vibes.git
210
- cd use-vibes
211
- ```
103
+ The ImgGen component uses CSS custom properties (variables) for styling, making it easy to customize the appearance while maintaining consistency. There are two primary ways to customize styling:
212
104
 
213
- 2. **Install Dependencies**:
214
- ```
215
- pnpm install
216
- ```
105
+ ##### 1. CSS Variables
217
106
 
218
- 3. **Build the Project**:
219
- The project is configured to compile the TypeScript source into an ESM module suitable for browser use.
220
- ```
221
- pnpm run build
222
- ```
107
+ Override the default styles by setting CSS custom properties in your CSS:
223
108
 
224
- ---
109
+ ```css
110
+ /* In your CSS file */
111
+ :root {
112
+ --imggen-text-color: #222;
113
+ --imggen-overlay-bg: rgba(245, 245, 245, 0.85);
114
+ --imggen-accent: #0088ff;
115
+ --imggen-border-radius: 4px;
116
+ }
225
117
 
226
- ## Testing
227
-
228
- ### Fixture Loading Test
229
-
230
- Ensure that HTML fixtures load correctly:
231
- 1. Open a test HTML file from the fixtures/ directory in your browser.
232
- 2. Verify that the page loads as expected without any library interference.
233
-
234
- ### Library Injection Test
235
-
236
- Verify that useVibes loads and executes a basic operation:
237
- 1. Create a simple test page that includes the built useVibes module.
238
- 2. Use the library to inject a "hello world" string into a target element. For example:
239
-
240
- ```html
241
- <!DOCTYPE html>
242
- <html lang="en">
243
- <head>
244
- <meta charset="UTF-8">
245
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
246
- <title>useVibes Test</title>
247
- <!-- Direct ESM import from source during development -->
248
- <script type="module">
249
- // Import directly from source files
250
- import useVibe from './src/index.js';
251
- </script>
252
- </head>
253
- <body>
254
- <div id="vibe-target"></div>
255
- <script type="module">
256
- // Direct import
257
- import useVibe from './src/index.js';
258
-
259
- // Create a new vibe
260
- const vibe = useVibe('HelloWorld');
261
-
262
- // Basic injection test
263
- document.querySelector("#vibe-target").innerHTML = vibe.describe();
264
- </script>
265
- </body>
266
- </html>
118
+ /* Dark theme example */
119
+ .dark-theme {
120
+ --imggen-text-color: #f0f0f0;
121
+ --imggen-overlay-bg: rgba(25, 25, 25, 0.85);
122
+ --imggen-accent: #66b2ff;
123
+ }
267
124
  ```
268
125
 
269
- 3. Open this page in your browser and confirm that "hello world" is injected into the target element.
126
+ ##### 2. Custom Classes
127
+
128
+ For more granular control, provide a `classes` object with custom CSS classes for specific component parts:
129
+
130
+ ```jsx
131
+ <ImgGen
132
+ prompt="A futuristic cityscape"
133
+ classes={{
134
+ root: 'my-custom-container',
135
+ image: 'rounded-xl shadow-lg',
136
+ overlay: 'bg-slate-800/70 text-white',
137
+ progress: 'h-2 bg-green-500',
138
+ button: 'hover:bg-blue-600',
139
+ }}
140
+ />
141
+ ```
270
142
 
271
- ---
143
+ The component uses these classes in addition to the default ones, allowing you to extend or override styles as needed.
144
+
145
+ ##### Available CSS Variables
146
+
147
+ | Variable | Default | Description |
148
+ | ------------------------ | -------------------------- | --------------------------------- |
149
+ | `--imggen-text-color` | `#333` | Main text color |
150
+ | `--imggen-background` | `#333333` | Background color for placeholder |
151
+ | `--imggen-overlay-bg` | `rgba(255, 255, 255, 0.5)` | Overlay panel background |
152
+ | `--imggen-accent` | `#0066cc` | Accent color (progress bar, etc.) |
153
+ | `--imggen-error-text` | `#ff6666` | Error message text color |
154
+ | `--imggen-border-radius` | `8px` | Border radius for containers |
155
+ | `--imggen-button-bg` | `rgba(255, 255, 255, 0.7)` | Button background color |
156
+ | `--imggen-font-size` | `14px` | Default font size |
157
+
158
+ ##### Available Class Slots
159
+
160
+ | Class Property | Description |
161
+ | --------------- | -------------------------------- |
162
+ | `root` | Main container element |
163
+ | `image` | The image element |
164
+ | `container` | Container for image and controls |
165
+ | `overlay` | Overlay panel with controls |
166
+ | `progress` | Progress indicator |
167
+ | `placeholder` | Placeholder shown during loading |
168
+ | `error` | Error message container |
169
+ | `controls` | Control buttons container |
170
+ | `button` | Individual buttons |
171
+ | `prompt` | Prompt text/input container |
172
+ | `deleteOverlay` | Delete confirmation dialog |
173
+
174
+ ## Development
272
175
 
273
- ## Build and Deployment
176
+ ```bash
177
+ # Install dependencies
178
+ pnpm install
274
179
 
275
- - **Buildless ESM Approach**: This project uses a modern buildless ESM approach. TypeScript source files are directly imported during development, and TypeScript compiler is only used for type checking and generating type definitions. This avoids complex bundling processes while leveraging the native ESM support in modern browsers and Node.js environments.
180
+ # Build the library
181
+ pnpm build
276
182
 
277
- ```json
278
- "scripts": {
279
- "build": "tsc",
280
- "typecheck": "tsc --noEmit"
281
- }
282
- ```
183
+ # Run tests
184
+ pnpm test
185
+ ```
283
186
 
284
- - **Deployment**: Direct imports from source files are supported during development. For production, TypeScript is compiled to JavaScript while maintaining ESM imports. The package can be published to npm or JSR for easy consumption via ESM imports.
187
+ ### Testing Notes
285
188
 
286
- ---
189
+ When writing tests for components that use the image generation functionality:
287
190
 
288
- ## Next Steps
191
+ - Use the provided mock implementations for `call-ai` and `use-fireproof`
192
+ - Wrap React state updates in `act()` to prevent test warnings
193
+ - Use `vi.useFakeTimers()` and `vi.advanceTimersByTime()` to control async behavior
194
+ - Be aware that `imageGen` API calls need to be properly mocked
289
195
 
290
- 1. **Vibe Check**: After completing the initial tests, review the results and gather feedback.
291
- 2. **Iterate**: Use feedback to refine functionality and integration.
292
- 3. **Expand**: Continue to develop additional features and tests, integrating further capabilities such as Fireproof, callAi, and more advanced micro-app interactions.
196
+ ### Browser Compatibility
293
197
 
294
- ---
198
+ This library is compatible with all modern browsers that support React 18+ and ES6 features.
295
199
 
296
200
  ## License
297
201
 
298
- This project is open source. See the LICENSE file for details.
299
-
300
- ---
301
-
302
- ## Contact
303
-
304
- For questions, feedback, or contributions, please open an issue on the repository or contact the maintainers.
202
+ MIT
package/package.json CHANGED
@@ -1,19 +1,27 @@
1
1
  {
2
2
  "name": "use-vibes",
3
- "version": "0.2.7",
3
+ "version": "0.4.0",
4
4
  "type": "module",
5
5
  "description": "Transform any DOM element into an AI-powered micro-app",
6
6
  "main": "dist/index.js",
7
- "browser": "dist/index.js",
8
7
  "module": "dist/index.js",
9
8
  "types": "dist/index.d.ts",
10
9
  "exports": {
11
10
  ".": {
11
+ "types": "./dist/index.d.ts",
12
12
  "import": "./dist/index.js",
13
- "require": "./dist/index.js",
14
- "types": "./dist/index.d.ts"
15
- }
13
+ "require": "./dist/index.js"
14
+ },
15
+ "./style-loader": {
16
+ "types": "./dist/style-loader.d.ts",
17
+ "import": "./dist/style-loader.js",
18
+ "require": "./dist/style-loader.js"
19
+ },
20
+ "./components/ImgGen.css": "./dist/components/ImgGen.css"
16
21
  },
22
+ "files": [
23
+ "dist"
24
+ ],
17
25
  "keywords": [
18
26
  "ai",
19
27
  "dom",
@@ -25,61 +33,44 @@
25
33
  ],
26
34
  "author": "",
27
35
  "license": "MIT",
28
- "files": [
29
- "dist"
30
- ],
36
+ "dependencies": {
37
+ "call-ai": "^0.10.1",
38
+ "use-fireproof": "^0.20.4",
39
+ "uuid": "^11.1.0"
40
+ },
41
+ "peerDependencies": {
42
+ "react": "^16.8.0 || ^17.0.0 || ^18.0.0 || ^19.0.0"
43
+ },
31
44
  "devDependencies": {
32
- "@playwright/test": "^1.41.2",
33
- "@rollup/plugin-commonjs": "^28.0.3",
34
- "@rollup/plugin-node-resolve": "^16.0.1",
35
- "@rollup/plugin-typescript": "^12.1.2",
36
45
  "@testing-library/jest-dom": "^6.6.3",
37
46
  "@testing-library/react": "^16.3.0",
38
- "@types/jsdom": "^21.1.7",
39
- "@types/node": "^20.11.19",
40
- "@types/react": "^18.2.0",
41
- "@typescript-eslint/eslint-plugin": "^7.0.1",
42
- "@typescript-eslint/parser": "^7.0.1",
43
- "esbuild": "^0.25.1",
47
+ "@types/react": "^19.1.0",
48
+ "@types/react-dom": "^19.1.2",
49
+ "@typescript-eslint/eslint-plugin": "^8.32.1",
50
+ "@typescript-eslint/parser": "^8.32.1",
51
+ "@vitejs/plugin-react": "^4.4.1",
44
52
  "eslint": "^8.56.0",
45
- "eslint-config-prettier": "^10.1.1",
46
- "eslint-plugin-prettier": "^5.2.5",
47
- "jsdom": "^26.0.0",
48
- "playwright": "^1.41.2",
53
+ "eslint-plugin-import": "^2.31.0",
54
+ "eslint-plugin-react": "^7.37.5",
55
+ "jsdom": "^26.1.0",
49
56
  "prettier": "^3.5.3",
50
- "react": "^19.1.0",
51
- "rollup": "^4.38.0",
52
- "rollup-plugin-terser": "^7.0.2",
53
- "terser": "^5.39.0",
57
+ "react-dom": "^19.1.0",
58
+ "rimraf": "^5.0.5",
54
59
  "typescript": "^5.3.3",
55
- "vite": "^5.1.4",
56
60
  "vitest": "^1.2.2"
57
61
  },
58
- "dependencies": {
59
- "call-ai": "^0.10.0"
60
- },
61
- "peerDependencies": {
62
- "react": "^16.8.0 || ^17.0.0 || ^18.0.0 || ^19.0.0"
63
- },
64
62
  "scripts": {
65
- "build": "tsc",
66
- "build:browser": "node scripts/build-browser.js",
67
- "build:browser:test": "node scripts/build-browser-test.js",
68
- "build:bookmarklet": "node scripts/generate-bookmarklet.js",
69
- "build:all": "npm run build && npm run build:browser && npm run build:bookmarklet",
63
+ "build": "pnpm clean && tsc && pnpm copy-css",
64
+ "copy-css": "mkdir -p dist/components && cp src/components/ImgGen.css dist/components/",
65
+ "build:watch": "tsc --watch",
66
+ "dev:build": "tsc --watch",
67
+ "lint": "eslint src --ext .ts,.tsx",
68
+ "lint:fix": "eslint src --ext .ts,.tsx --fix --rule 'no-unused-vars: 1' --rule 'no-empty: 1' --rule '@typescript-eslint/no-unused-vars: 1'",
69
+ "format": "prettier --write \"./**/*.{js,jsx,ts,tsx,json,md}\"",
70
70
  "test": "vitest run",
71
71
  "test:watch": "vitest",
72
- "test:browser": "npm run build:browser:test && playwright test --reporter=line",
73
- "test:browser:headed": "npm run build:browser:test && playwright test --headed --reporter=line",
74
- "test:browser:debug": "npm run build:browser:test && playwright test --debug --reporter=line",
75
- "lint": "eslint --ext .js,.ts,.tsx src/ tests/ scripts/",
76
- "lint:fix": "eslint --ext .js,.ts,.tsx --fix src/ tests/ scripts/",
77
- "format": "prettier --write 'src/**/*.{js,ts,tsx}' 'tests/**/*.{js,ts,tsx}' 'scripts/**/*.js'",
72
+ "clean": "rimraf dist",
78
73
  "typecheck": "tsc --noEmit",
79
- "validate": "npm run typecheck && npm run test && npm run test:browser && npm run lint",
80
- "fix": "npm run lint:fix && npm run format",
81
- "check": "npm run validate && npm run fix",
82
- "prerelease": "npm run validate",
83
- "serve": "vite fixtures --port 3000"
74
+ "check": "pnpm lint && pnpm format && pnpm typecheck && pnpm test"
84
75
  }
85
76
  }