@patchedcodes/hackathon-widget 0.1.2 → 0.1.4

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.
package/README.md CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  AI-powered frontend editor widget — modify live web interfaces using natural language.
4
4
 
5
- Renders a floating chat button on your page. Users type a natural-language command, the widget captures a screenshot, sends it to your backend, and applies the resulting changes.
5
+ Renders a floating chat button on your page. Users type a natural-language command, the widget captures a screenshot, sends it to your backend, and applies the resulting code changes live.
6
6
 
7
7
  ## Install
8
8
 
@@ -23,11 +23,11 @@ import { init } from "@patchedcodes/hackathon-widget";
23
23
 
24
24
  init({
25
25
  endpoint: "https://your-backend.com/api/command",
26
- projectId: "my-project",
26
+ apiKey: "your-api-key",
27
27
  });
28
28
  ```
29
29
 
30
- That's it. A floating button appears in the bottom-right corner of your page.
30
+ A floating button appears in the bottom-right corner of your page. Click it to open the chat panel, describe a visual change, and the widget handles the rest.
31
31
 
32
32
  ## Configuration
33
33
 
@@ -36,7 +36,7 @@ That's it. A floating button appears in the bottom-right corner of your page.
36
36
  | Option | Type | Default | Description |
37
37
  |---|---|---|---|
38
38
  | `endpoint` | `string` | **(required)** | Backend URL that receives command POST requests. |
39
- | `projectId` | `string` | **(required)** | Project identifier sent with each request so the backend knows which repo to modify. |
39
+ | `apiKey` | `string` | **(required)** | API key sent in the request body to authenticate with the backend. |
40
40
  | `position` | `"bottom-right" \| "bottom-left" \| "top-right" \| "top-left"` | `"bottom-right"` | Position of the floating widget button. |
41
41
  | `theme` | `"light" \| "dark"` | `"light"` | Widget color theme. |
42
42
  | `headers` | `Record<string, string>` | `{}` | Custom headers sent with backend requests (e.g. auth tokens). |
@@ -50,7 +50,7 @@ That's it. A floating button appears in the bottom-right corner of your page.
50
50
  ```ts
51
51
  init({
52
52
  endpoint: "https://your-backend.com/api/command",
53
- projectId: "my-project",
53
+ apiKey: "your-api-key",
54
54
  onDeployReady: (branch, previewUrl) => {
55
55
  console.log(`Deploy ready on branch ${branch}: ${previewUrl}`);
56
56
  },
@@ -69,7 +69,7 @@ init({
69
69
  ```ts
70
70
  const widget = init({
71
71
  endpoint: "https://your-backend.com/api/command",
72
- projectId: "my-project",
72
+ apiKey: "your-api-key",
73
73
  });
74
74
 
75
75
  // Open, close, or toggle the chat panel
@@ -78,14 +78,109 @@ widget.close();
78
78
  widget.toggle();
79
79
  ```
80
80
 
81
+ ## Architecture
82
+
83
+ The widget is a **vanilla TypeScript Web Component** (`<devloyed-widget>`) that renders inside a [Shadow DOM](https://developer.mozilla.org/en-US/docs/Web/API/Web_Components/Using_shadow_DOM). This means:
84
+
85
+ - **Full style encapsulation** — widget styles cannot leak into or be affected by the host page.
86
+ - **Framework-agnostic** — works with React, Vue, Svelte, plain HTML, or any other framework.
87
+ - **Single bundle** — ships as one ES module (`dist/devloyed-widget.js`) with no external runtime dependencies beyond `html2canvas` (bundled).
88
+
89
+ ### How It Works
90
+
91
+ 1. User opens the widget and types a natural-language command (e.g. "Make the header blue").
92
+ 2. The widget captures a screenshot of the page using `html2canvas`.
93
+ 3. A multi-step loader plays with randomized progress labels while the request is in flight.
94
+ 4. The command and API key are sent to your backend via a single `POST` request.
95
+ 5. On response, the loader cascades to completion and the AI's response is shown in the chat.
96
+ 6. If the backend returns a `commitId`, a deploy toast appears with an "Apply" button that reloads the page to pick up the changes.
97
+
98
+ ### Backend Contract
99
+
100
+ The widget sends a `POST` request to your `endpoint` with the following JSON body:
101
+
102
+ ```ts
103
+ interface CommandRequest {
104
+ apiKey: string; // The API key from config
105
+ prompt: string; // The user's natural-language command
106
+ }
107
+ ```
108
+
109
+ Your backend should return:
110
+
111
+ ```ts
112
+ interface CommandResponse {
113
+ commitId: string; // The commit SHA that was pushed
114
+ transcript: string; // AI's explanation of what was changed
115
+ }
116
+ ```
117
+
118
+ ## Routing Utilities
119
+
120
+ The package exports cookie-based routing helpers for reverse-proxy branch switching. These are standalone utilities — not wired into the widget automatically — for consumers who want to implement branch preview routing.
121
+
122
+ ```ts
123
+ import {
124
+ setRoutingCookie,
125
+ getRoutingCookie,
126
+ clearRoutingCookie,
127
+ isOnPreview,
128
+ } from "@patchedcodes/hackathon-widget";
129
+
130
+ // Set cookie to route traffic to a preview branch (reloads by default)
131
+ setRoutingCookie("feature/new-header");
132
+
133
+ // Check current branch
134
+ const branch = getRoutingCookie(); // "feature/new-header" or null
135
+
136
+ // Check if viewing a preview
137
+ if (isOnPreview()) {
138
+ // show "return to production" button
139
+ }
140
+
141
+ // Clear and return to production
142
+ clearRoutingCookie();
143
+ ```
144
+
145
+ Options can be passed to `setRoutingCookie` and `clearRoutingCookie`:
146
+
147
+ | Option | Type | Default | Description |
148
+ |---|---|---|---|
149
+ | `cookieName` | `string` | `"devloyed_branch"` | Cookie name. |
150
+ | `maxAge` | `number` | `3600` | Cookie max-age in seconds. |
151
+ | `path` | `string` | `"/"` | Cookie path. |
152
+ | `reload` | `boolean` | `true` | Whether to reload the page after setting/clearing. |
153
+
154
+ ## Development
155
+
156
+ ```bash
157
+ # Start dev server with mock backend
158
+ npm run dev
159
+
160
+ # Or run them separately:
161
+ npm run dev:mock # Mock backend on port 3001
162
+ npm run dev:vite # Vite dev server
163
+
164
+ # Type-check
165
+ npm run typecheck
166
+
167
+ # Build
168
+ npm run build
169
+ ```
170
+
171
+ The mock server (`dev/mock-server.mjs`) simulates a ~10-second AI processing delay and returns fake commit/branch data for any command.
172
+
81
173
  ## TypeScript
82
174
 
83
175
  Types are shipped with the package — no separate `@types/` install needed. You can import them directly:
84
176
 
85
177
  ```ts
86
178
  import type { WidgetConfig } from "@patchedcodes/hackathon-widget";
179
+ import type { CommandRequest, CommandResponse } from "@patchedcodes/hackathon-widget";
87
180
  ```
88
181
 
182
+ The package also exports `DevloyedWidget` and `TransportClient` classes for advanced usage.
183
+
89
184
  ## License
90
185
 
91
186
  MIT