@keak/sdk 1.0.9 → 2.0.1

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.
@@ -12,7 +12,7 @@ class AIHelper {
12
12
  content: fileContent,
13
13
  fileName: filePath.split('/').pop(),
14
14
  framework: framework,
15
- task: 'add_keak_provider'
15
+ task: 'add_keak_hook'
16
16
  });
17
17
 
18
18
  const options = {
@@ -111,12 +111,10 @@ class AIHelper {
111
111
 
112
112
  // Fallback AI logic when API is not available
113
113
  class LocalAIHelper {
114
- addKeakProvider(content, isTypeScript = false) {
114
+ addKeakHook(content) {
115
115
  const lines = content.split('\n');
116
116
  const result = [];
117
117
  let importAdded = false;
118
- let inJSXReturn = false;
119
- let jsxDepth = 0;
120
118
  let lastImportLine = -1;
121
119
 
122
120
  // First pass: find last import
@@ -129,76 +127,156 @@ class LocalAIHelper {
129
127
  // Add import after last import
130
128
  for (let i = 0; i < lines.length; i++) {
131
129
  result.push(lines[i]);
132
-
130
+
133
131
  if (i === lastImportLine && !importAdded) {
134
- result.push("import { KeakProvider } from '@keak/sdk';");
132
+ result.push("import { useKeak, KeakToolbar } from '@keak/sdk';");
135
133
  importAdded = true;
136
134
  }
137
135
  }
138
136
 
139
137
  // If no imports found, add at the beginning
140
138
  if (!importAdded) {
141
- result.unshift("import { KeakProvider } from '@keak/sdk';");
139
+ result.unshift("import { useKeak, KeakToolbar } from '@keak/sdk';");
142
140
  }
143
141
 
144
- // Now wrap the appropriate component
142
+ // Now add hook to the appropriate location
145
143
  const finalContent = result.join('\n');
146
-
147
- // Try different wrapping strategies
144
+
145
+ // Try different strategies based on file type
148
146
  if (finalContent.includes('ReactDOM.render')) {
149
147
  return this.wrapReactDOMRender(finalContent);
150
148
  } else if (finalContent.includes('createRoot')) {
151
149
  return this.wrapCreateRoot(finalContent);
152
150
  } else if (finalContent.includes('export default function')) {
153
- return this.wrapDefaultExport(finalContent);
151
+ return this.addHookToDefaultExport(finalContent);
154
152
  } else {
155
- return this.wrapGenericComponent(finalContent);
153
+ return this.addHookToGenericComponent(finalContent);
156
154
  }
157
155
  }
158
156
 
159
157
  wrapReactDOMRender(content) {
160
- return content.replace(
161
- /ReactDOM\.render\s*\(\s*(<[\s\S]*?[\s\S]*?<\/[\s\S]*?)\s*,/,
158
+ // For ReactDOM.render, create a KeakWrapper component with the hook
159
+ const wrapperComponent = `
160
+ // Keak wrapper component with hook
161
+ function KeakWrapper({ children }) {
162
+ useKeak({ debug: true });
163
+ return (
164
+ <>
165
+ {children}
166
+ <KeakToolbar position="bottom-right" />
167
+ </>
168
+ );
169
+ }
170
+ `;
171
+
172
+ // Insert wrapper component before ReactDOM.render
173
+ const renderIndex = content.indexOf('ReactDOM.render');
174
+ if (renderIndex === -1) return content;
175
+
176
+ const beforeRender = content.substring(0, renderIndex);
177
+ const afterRender = content.substring(renderIndex);
178
+
179
+ // Wrap the rendered component
180
+ const wrappedRender = afterRender.replace(
181
+ /ReactDOM\.render\s*\(\s*(<[\s\S]*?>[\s\S]*?<\/[\s\S]*?>)\s*,/,
162
182
  `ReactDOM.render(
163
- <KeakProvider config={{ apiKey: 'demo', debug: true }}$1
164
- </KeakProvider,`
183
+ <KeakWrapper>
184
+ $1
185
+ </KeakWrapper>,`
165
186
  );
187
+
188
+ return beforeRender + wrapperComponent + wrappedRender;
166
189
  }
167
190
 
168
191
  wrapCreateRoot(content) {
169
- return content.replace(
170
- /root\.render\s*\(\s*(<[\s\S]*?[\s\S]*?<\/[\s\S]*?)\s*\)/,
192
+ // For createRoot, create a KeakWrapper component with the hook
193
+ const wrapperComponent = `
194
+ // Keak wrapper component with hook
195
+ function KeakWrapper({ children }) {
196
+ useKeak({ debug: true });
197
+ return (
198
+ <>
199
+ {children}
200
+ <KeakToolbar position="bottom-right" />
201
+ </>
202
+ );
203
+ }
204
+ `;
205
+
206
+ // Insert wrapper component before root.render
207
+ const renderIndex = content.indexOf('root.render');
208
+ if (renderIndex === -1) return content;
209
+
210
+ const beforeRender = content.substring(0, renderIndex);
211
+ const afterRender = content.substring(renderIndex);
212
+
213
+ // Wrap the rendered component
214
+ const wrappedRender = afterRender.replace(
215
+ /root\.render\s*\(\s*(<[\s\S]*?>[\s\S]*?<\/[\s\S]*?>)\s*\)/,
171
216
  `root.render(
172
- <KeakProvider config={{ apiKey: 'demo', debug: true }}$1
173
- </KeakProvider)`
217
+ <KeakWrapper>
218
+ $1
219
+ </KeakWrapper>)`
174
220
  );
221
+
222
+ return beforeRender + wrapperComponent + wrappedRender;
175
223
  }
176
224
 
177
- wrapDefaultExport(content) {
178
- // Find the return statement in the default export function
179
- const functionMatch = content.match(/(export\s+default\s+function\s+\w+\s*\([^)]*\)\s*{[\s\S]*?return\s+)\(?([\s\S]*?)\)?(\s*;?\s*})/);
180
-
225
+ addHookToDefaultExport(content) {
226
+ // For default export components, add the hook directly to the component body
227
+ const functionMatch = content.match(/(export\s+default\s+function\s+\w+\s*\([^)]*\)\s*\{)\s*/);
228
+
181
229
  if (functionMatch) {
182
- const wrapped = `${functionMatch[1]}(
183
- <KeakProvider config={{ apiKey: 'demo', debug: true }}${functionMatch[2]}
184
- </KeakProvider)${functionMatch[3]}`;
185
- return content.replace(functionMatch[0], wrapped);
230
+ // Add hook at the start of the component
231
+ const withHook = content.replace(
232
+ functionMatch[0],
233
+ `${functionMatch[1]}
234
+ useKeak({ debug: true });
235
+ `
236
+ );
237
+
238
+ // Add KeakToolbar before the closing JSX tag
239
+ const returnMatch = withHook.match(/(return\s+\([\s\S]*?)(\s*<\/\w+>\s*\)\s*;?\s*})/);
240
+ if (returnMatch) {
241
+ return withHook.replace(
242
+ returnMatch[0],
243
+ `${returnMatch[1]}
244
+ <KeakToolbar position="bottom-right" />${returnMatch[2]}`
245
+ );
246
+ }
247
+
248
+ return withHook;
186
249
  }
187
-
250
+
188
251
  return content;
189
252
  }
190
253
 
191
- wrapGenericComponent(content) {
192
- // Try to find any return statement with JSX
193
- const returnMatch = content.match(/(return\s+)\(?([\s\S]*?<[\s\S]*?[\s\S]*?<\/[\s\S]*?)\)?(\s*;?)/);
194
-
195
- if (returnMatch) {
196
- const wrapped = `${returnMatch[1]}(
197
- <KeakProvider config={{ apiKey: 'demo', debug: true }}${returnMatch[2]}
198
- </KeakProvider)${returnMatch[3]}`;
199
- return content.replace(returnMatch[0], wrapped);
254
+ addHookToGenericComponent(content) {
255
+ // For generic components, try to add the hook to the component body
256
+ const functionMatch = content.match(/(function\s+\w+\s*\([^)]*\)\s*\{)\s*/);
257
+
258
+ if (functionMatch) {
259
+ // Add hook at the start of the component
260
+ const withHook = content.replace(
261
+ functionMatch[0],
262
+ `${functionMatch[1]}
263
+ useKeak({ debug: true });
264
+ `
265
+ );
266
+
267
+ // Add KeakToolbar before the closing JSX tag
268
+ const returnMatch = withHook.match(/(return\s+\([\s\S]*?)(\s*<\/\w+>\s*\)\s*;?)/);
269
+ if (returnMatch) {
270
+ return withHook.replace(
271
+ returnMatch[0],
272
+ `${returnMatch[1]}
273
+ <KeakToolbar position="bottom-right" />${returnMatch[2]}`
274
+ );
275
+ }
276
+
277
+ return withHook;
200
278
  }
201
-
279
+
202
280
  return content;
203
281
  }
204
282
  }