@allternit/gizzi-code 1.0.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 (64) hide show
  1. package/WORKSPACE_INTEGRATION.md +290 -0
  2. package/chat-sessions/sessions-index.json +98 -0
  3. package/package.json +43 -0
  4. package/package.json.original +31 -0
  5. package/package.json.publish +47 -0
  6. package/src/bootstrap/state.d.ts +169 -0
  7. package/src/bootstrap/state.js +261 -0
  8. package/src/bootstrap/state.ts +371 -0
  9. package/src/context/pack-builder.d.ts +24 -0
  10. package/src/context/pack-builder.js +281 -0
  11. package/src/context/pack-builder.ts +334 -0
  12. package/src/hooks/useExitOnCtrlCD.d.ts +86 -0
  13. package/src/hooks/useExitOnCtrlCD.js +130 -0
  14. package/src/hooks/useExitOnCtrlCD.ts +202 -0
  15. package/src/hooks/useKeybinding.d.ts +89 -0
  16. package/src/hooks/useKeybinding.js +163 -0
  17. package/src/hooks/useKeybinding.ts +226 -0
  18. package/src/hooks/useTerminalSize.d.ts +41 -0
  19. package/src/hooks/useTerminalSize.js +90 -0
  20. package/src/hooks/useTerminalSize.ts +117 -0
  21. package/src/index.d.ts +23 -0
  22. package/src/index.js +24 -0
  23. package/src/index.ts +132 -0
  24. package/src/runtime/brand/brand.d.ts +488 -0
  25. package/src/runtime/brand/brand.js +464 -0
  26. package/src/runtime/brand/brand.ts +667 -0
  27. package/src/runtime/services/tokenEstimation.d.ts +187 -0
  28. package/src/runtime/services/tokenEstimation.js +423 -0
  29. package/src/runtime/services/tokenEstimation.ts +547 -0
  30. package/src/runtime/tools/Tool.d.ts +239 -0
  31. package/src/runtime/tools/Tool.js +285 -0
  32. package/src/runtime/tools/Tool.ts +524 -0
  33. package/src/scheduler/TaskScheduler.d.ts +98 -0
  34. package/src/scheduler/TaskScheduler.js +201 -0
  35. package/src/scheduler/TaskScheduler.ts +259 -0
  36. package/src/state/AppStateStore.d.ts +347 -0
  37. package/src/state/AppStateStore.js +439 -0
  38. package/src/state/AppStateStore.ts +658 -0
  39. package/src/state/WorkspaceStateStore.d.ts +43 -0
  40. package/src/state/WorkspaceStateStore.js +224 -0
  41. package/src/state/WorkspaceStateStore.ts +297 -0
  42. package/src/types/message.d.ts +190 -0
  43. package/src/types/message.js +77 -0
  44. package/src/types/message.ts +277 -0
  45. package/src/utils/format.d.ts +126 -0
  46. package/src/utils/format.js +230 -0
  47. package/src/utils/format.ts +336 -0
  48. package/src/utils/fullscreen.d.ts +161 -0
  49. package/src/utils/fullscreen.js +277 -0
  50. package/src/utils/fullscreen.ts +382 -0
  51. package/src/utils/sessionStorage.d.ts +210 -0
  52. package/src/utils/sessionStorage.js +435 -0
  53. package/src/utils/sessionStorage.ts +559 -0
  54. package/src/watcher/WorkspaceWatcher.d.ts +70 -0
  55. package/src/watcher/WorkspaceWatcher.js +191 -0
  56. package/src/watcher/WorkspaceWatcher.ts +251 -0
  57. package/src/workspace/loader.d.ts +30 -0
  58. package/src/workspace/loader.js +117 -0
  59. package/src/workspace/loader.ts +171 -0
  60. package/src/workspace/types.d.ts +91 -0
  61. package/src/workspace/types.js +7 -0
  62. package/src/workspace/types.ts +122 -0
  63. package/threads/thread_1775361175022.json +9 -0
  64. package/tsconfig.json +14 -0
@@ -0,0 +1,435 @@
1
+ /**
2
+ * @fileoverview Session storage utilities for gizzi-code integration
3
+ * @module utils/sessionStorage
4
+ *
5
+ * Provides session persistence, storage management, and user type detection
6
+ * with safe fallbacks for environments without storage access.
7
+ */
8
+ /**
9
+ * Storage keys used throughout the application
10
+ * @constant {Record<string, string>}
11
+ */
12
+ export const STORAGE_KEYS = {
13
+ SESSION: 'gizzi_session',
14
+ USER_TYPE: 'gizzi_user_type',
15
+ MESSAGES: 'gizzi_messages',
16
+ PREFERENCES: 'gizzi_preferences',
17
+ LAST_ACTIVITY: 'gizzi_last_activity',
18
+ SESSION_METADATA: 'gizzi_session_metadata',
19
+ };
20
+ /**
21
+ * Valid user types for runtime validation
22
+ * @constant {readonly UserType[]}
23
+ */
24
+ export const VALID_USER_TYPES = [
25
+ 'anonymous',
26
+ 'free',
27
+ 'pro',
28
+ 'enterprise',
29
+ 'admin',
30
+ ];
31
+ /**
32
+ * Checks if localStorage/sessionStorage is available
33
+ * @returns {boolean} True if storage APIs are available
34
+ */
35
+ function isStorageAvailable() {
36
+ try {
37
+ if (typeof window === 'undefined') {
38
+ return false;
39
+ }
40
+ const testKey = '__gizzi_storage_test__';
41
+ window.sessionStorage.setItem(testKey, 'test');
42
+ window.sessionStorage.removeItem(testKey);
43
+ return true;
44
+ }
45
+ catch {
46
+ return false;
47
+ }
48
+ }
49
+ /**
50
+ * Safely parses JSON with error handling
51
+ *
52
+ * @template T
53
+ * @param {string} json - JSON string to parse
54
+ * @param {T} [defaultValue] - Default value if parsing fails
55
+ * @returns {T | null} Parsed value or default/null on error
56
+ */
57
+ function safeJsonParse(json, defaultValue) {
58
+ try {
59
+ return JSON.parse(json);
60
+ }
61
+ catch {
62
+ return defaultValue ?? null;
63
+ }
64
+ }
65
+ /**
66
+ * Safely stringifies JSON with error handling
67
+ *
68
+ * @param {unknown} value - Value to stringify
69
+ * @returns {string | null} JSON string or null on error
70
+ */
71
+ function safeJsonStringify(value) {
72
+ try {
73
+ return JSON.stringify(value);
74
+ }
75
+ catch {
76
+ return null;
77
+ }
78
+ }
79
+ /**
80
+ * In-memory fallback storage when sessionStorage is unavailable
81
+ * @constant {Map<string, string>}
82
+ */
83
+ const memoryStorage = new Map();
84
+ /**
85
+ * Gets an item from storage (sessionStorage or memory fallback)
86
+ *
87
+ * @param {string} key - Storage key
88
+ * @returns {string | null} The stored value or null
89
+ */
90
+ function getStorageItem(key) {
91
+ if (isStorageAvailable()) {
92
+ return window.sessionStorage.getItem(key);
93
+ }
94
+ return memoryStorage.get(key) ?? null;
95
+ }
96
+ /**
97
+ * Sets an item in storage (sessionStorage or memory fallback)
98
+ *
99
+ * @param {string} key - Storage key
100
+ * @param {string} value - Value to store
101
+ * @returns {boolean} True if successful
102
+ */
103
+ function setStorageItem(key, value) {
104
+ try {
105
+ if (isStorageAvailable()) {
106
+ window.sessionStorage.setItem(key, value);
107
+ }
108
+ else {
109
+ memoryStorage.set(key, value);
110
+ }
111
+ return true;
112
+ }
113
+ catch (error) {
114
+ console.error(`Failed to set storage item '${key}':`, error);
115
+ return false;
116
+ }
117
+ }
118
+ /**
119
+ * Removes an item from storage
120
+ *
121
+ * @param {string} key - Storage key to remove
122
+ * @returns {boolean} True if successful
123
+ */
124
+ function removeStorageItem(key) {
125
+ try {
126
+ if (isStorageAvailable()) {
127
+ window.sessionStorage.removeItem(key);
128
+ }
129
+ else {
130
+ memoryStorage.delete(key);
131
+ }
132
+ return true;
133
+ }
134
+ catch (error) {
135
+ console.error(`Failed to remove storage item '${key}':`, error);
136
+ return false;
137
+ }
138
+ }
139
+ /**
140
+ * Saves the current session to storage
141
+ *
142
+ * @param {UserSession} session - The session to save
143
+ * @param {Object} [options] - Save options
144
+ * @param {TypedMessage[]} [options.messages] - Messages to persist
145
+ * @param {Record<string, unknown>} [options.preferences] - Preferences to persist
146
+ * @returns {StorageResult<void>} Result of the save operation
147
+ * @example
148
+ * ```typescript
149
+ * const result = saveSession(session, { messages, preferences });
150
+ * if (!result.success) {
151
+ * console.error('Failed to save session:', result.error);
152
+ * }
153
+ * ```
154
+ */
155
+ export function saveSession(session, options = {}) {
156
+ if (!session) {
157
+ return { success: false, data: null, error: 'Session is required' };
158
+ }
159
+ const existingMetadata = loadSessionMetadata();
160
+ const metadata = {
161
+ createdAt: existingMetadata?.createdAt ?? new Date().toISOString(),
162
+ lastActivity: new Date().toISOString(),
163
+ messageCount: options.messages?.length ?? 0,
164
+ version: 1,
165
+ deviceInfo: isStorageAvailable()
166
+ ? {
167
+ userAgent: navigator.userAgent,
168
+ platform: navigator.platform,
169
+ language: navigator.language,
170
+ }
171
+ : undefined,
172
+ };
173
+ const sessionData = {
174
+ session,
175
+ metadata,
176
+ messages: options.messages ?? [],
177
+ preferences: options.preferences ?? {},
178
+ };
179
+ const serialized = safeJsonStringify(sessionData);
180
+ if (!serialized) {
181
+ return { success: false, data: null, error: 'Failed to serialize session data' };
182
+ }
183
+ if (!setStorageItem(STORAGE_KEYS.SESSION, serialized)) {
184
+ return { success: false, data: null, error: 'Failed to write to storage' };
185
+ }
186
+ // Also save user type separately for quick access
187
+ setStorageItem(STORAGE_KEYS.USER_TYPE, session.userType);
188
+ setStorageItem(STORAGE_KEYS.LAST_ACTIVITY, metadata.lastActivity);
189
+ return { success: true, data: undefined, error: null };
190
+ }
191
+ /**
192
+ * Loads the session from storage
193
+ *
194
+ * @returns {StorageResult<SessionData>} The loaded session data or error
195
+ * @example
196
+ * ```typescript
197
+ * const result = loadSession();
198
+ * if (result.success && result.data) {
199
+ * const { session, messages } = result.data;
200
+ * // Restore session...
201
+ * }
202
+ * ```
203
+ */
204
+ export function loadSession() {
205
+ const serialized = getStorageItem(STORAGE_KEYS.SESSION);
206
+ if (!serialized) {
207
+ return { success: false, data: null, error: 'No session found in storage' };
208
+ }
209
+ const data = safeJsonParse(serialized);
210
+ if (!data) {
211
+ return { success: false, data: null, error: 'Failed to parse session data' };
212
+ }
213
+ // Validate session has not expired
214
+ if (data.session.expiresAt) {
215
+ const expiresAt = new Date(data.session.expiresAt);
216
+ if (expiresAt < new Date()) {
217
+ clearSession();
218
+ return { success: false, data: null, error: 'Session has expired' };
219
+ }
220
+ }
221
+ // Update last activity
222
+ const updatedMetadata = {
223
+ ...data.metadata,
224
+ lastActivity: new Date().toISOString(),
225
+ };
226
+ // Silently update last activity in storage
227
+ const updatedData = { ...data, metadata: updatedMetadata };
228
+ const serializedUpdated = safeJsonStringify(updatedData);
229
+ if (serializedUpdated) {
230
+ setStorageItem(STORAGE_KEYS.SESSION, serializedUpdated);
231
+ }
232
+ return { success: true, data: updatedData, error: null };
233
+ }
234
+ /**
235
+ * Clears all session data from storage
236
+ *
237
+ * @returns {boolean} True if successful
238
+ * @example
239
+ * ```typescript
240
+ * clearSession(); // Logs out the user
241
+ * ```
242
+ */
243
+ export function clearSession() {
244
+ const keysToRemove = [
245
+ STORAGE_KEYS.SESSION,
246
+ STORAGE_KEYS.USER_TYPE,
247
+ STORAGE_KEYS.MESSAGES,
248
+ STORAGE_KEYS.PREFERENCES,
249
+ STORAGE_KEYS.LAST_ACTIVITY,
250
+ STORAGE_KEYS.SESSION_METADATA,
251
+ ];
252
+ let allSuccess = true;
253
+ for (const key of keysToRemove) {
254
+ if (!removeStorageItem(key)) {
255
+ allSuccess = false;
256
+ }
257
+ }
258
+ // Also clear memory storage
259
+ memoryStorage.clear();
260
+ return allSuccess;
261
+ }
262
+ /**
263
+ * Gets the user type from storage
264
+ * Returns 'anonymous' if no user type is stored or on error
265
+ *
266
+ * @returns {UserType} The stored user type or 'anonymous'
267
+ * @example
268
+ * ```typescript
269
+ * const userType = getUserType();
270
+ * if (userType === 'pro') {
271
+ * // Show pro features
272
+ * }
273
+ * ```
274
+ */
275
+ export function getUserType() {
276
+ const stored = getStorageItem(STORAGE_KEYS.USER_TYPE);
277
+ if (stored && VALID_USER_TYPES.includes(stored)) {
278
+ return stored;
279
+ }
280
+ // Try to infer from session
281
+ const sessionResult = loadSession();
282
+ if (sessionResult.success && sessionResult.data) {
283
+ const sessionType = sessionResult.data.session.userType;
284
+ if (VALID_USER_TYPES.includes(sessionType)) {
285
+ return sessionType;
286
+ }
287
+ }
288
+ return 'anonymous';
289
+ }
290
+ /**
291
+ * Loads session metadata without loading the full session
292
+ *
293
+ * @returns {SessionMetadata | null} The metadata or null if not found
294
+ */
295
+ function loadSessionMetadata() {
296
+ const sessionResult = loadSession();
297
+ if (sessionResult.success && sessionResult.data) {
298
+ return sessionResult.data.metadata;
299
+ }
300
+ return null;
301
+ }
302
+ /**
303
+ * Saves messages to session storage
304
+ *
305
+ * @param {TypedMessage[]} messages - Messages to save
306
+ * @returns {StorageResult<void>} Result of the operation
307
+ */
308
+ export function saveMessages(messages) {
309
+ const serialized = safeJsonStringify(messages);
310
+ if (!serialized) {
311
+ return { success: false, data: null, error: 'Failed to serialize messages' };
312
+ }
313
+ if (!setStorageItem(STORAGE_KEYS.MESSAGES, serialized)) {
314
+ return { success: false, data: null, error: 'Failed to write messages to storage' };
315
+ }
316
+ // Update message count in session metadata
317
+ const sessionResult = loadSession();
318
+ if (sessionResult.success && sessionResult.data) {
319
+ const { session, metadata, preferences } = sessionResult.data;
320
+ saveSession(session, {
321
+ messages,
322
+ preferences,
323
+ });
324
+ }
325
+ return { success: true, data: undefined, error: null };
326
+ }
327
+ /**
328
+ * Loads messages from session storage
329
+ *
330
+ * @returns {StorageResult<TypedMessage[]>} The loaded messages or error
331
+ */
332
+ export function loadMessages() {
333
+ const serialized = getStorageItem(STORAGE_KEYS.MESSAGES);
334
+ if (!serialized) {
335
+ return { success: false, data: null, error: 'No messages found in storage' };
336
+ }
337
+ const messages = safeJsonParse(serialized, []);
338
+ if (!messages) {
339
+ return { success: false, data: null, error: 'Failed to parse messages' };
340
+ }
341
+ return { success: true, data: messages, error: null };
342
+ }
343
+ /**
344
+ * Saves user preferences to storage
345
+ *
346
+ * @param {Record<string, unknown>} preferences - Preferences to save
347
+ * @returns {StorageResult<void>} Result of the operation
348
+ */
349
+ export function savePreferences(preferences) {
350
+ const serialized = safeJsonStringify(preferences);
351
+ if (!serialized) {
352
+ return { success: false, data: null, error: 'Failed to serialize preferences' };
353
+ }
354
+ if (!setStorageItem(STORAGE_KEYS.PREFERENCES, serialized)) {
355
+ return { success: false, data: null, error: 'Failed to write preferences to storage' };
356
+ }
357
+ return { success: true, data: undefined, error: null };
358
+ }
359
+ /**
360
+ * Loads user preferences from storage
361
+ *
362
+ * @returns {StorageResult<Record<string, unknown>>} The loaded preferences or error
363
+ */
364
+ export function loadPreferences() {
365
+ const serialized = getStorageItem(STORAGE_KEYS.PREFERENCES);
366
+ if (!serialized) {
367
+ return { success: false, data: null, error: 'No preferences found in storage' };
368
+ }
369
+ const preferences = safeJsonParse(serialized, {});
370
+ if (!preferences) {
371
+ return { success: false, data: null, error: 'Failed to parse preferences' };
372
+ }
373
+ return { success: true, data: preferences, error: null };
374
+ }
375
+ /**
376
+ * Gets the timestamp of the last user activity
377
+ *
378
+ * @returns {Date | null} The last activity date or null if not found
379
+ */
380
+ export function getLastActivity() {
381
+ const stored = getStorageItem(STORAGE_KEYS.LAST_ACTIVITY);
382
+ if (!stored) {
383
+ return null;
384
+ }
385
+ try {
386
+ const date = new Date(stored);
387
+ return isNaN(date.getTime()) ? null : date;
388
+ }
389
+ catch {
390
+ return null;
391
+ }
392
+ }
393
+ /**
394
+ * Updates the last activity timestamp to now
395
+ *
396
+ * @returns {boolean} True if successful
397
+ */
398
+ export function updateLastActivity() {
399
+ return setStorageItem(STORAGE_KEYS.LAST_ACTIVITY, new Date().toISOString());
400
+ }
401
+ /**
402
+ * Checks if there is an active session in storage
403
+ *
404
+ * @returns {boolean} True if a valid session exists
405
+ */
406
+ export function hasActiveSession() {
407
+ const result = loadSession();
408
+ return result.success;
409
+ }
410
+ /**
411
+ * Type guard to check if a value is a valid UserType
412
+ *
413
+ * @param {unknown} value - The value to check
414
+ * @returns {boolean} True if the value is a valid UserType
415
+ */
416
+ export function isUserType(value) {
417
+ return typeof value === 'string' && VALID_USER_TYPES.includes(value);
418
+ }
419
+ // Default export
420
+ export default {
421
+ STORAGE_KEYS,
422
+ VALID_USER_TYPES,
423
+ saveSession,
424
+ loadSession,
425
+ clearSession,
426
+ getUserType,
427
+ saveMessages,
428
+ loadMessages,
429
+ savePreferences,
430
+ loadPreferences,
431
+ getLastActivity,
432
+ updateLastActivity,
433
+ hasActiveSession,
434
+ isUserType,
435
+ };