@djangocfg/ext-knowbase 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.
- package/README.md +177 -0
- package/dist/chunk-GNRC54ON.js +4308 -0
- package/dist/hooks.cjs +6003 -0
- package/dist/hooks.d.cts +610 -0
- package/dist/hooks.d.ts +610 -0
- package/dist/hooks.js +1486 -0
- package/dist/index.cjs +4450 -0
- package/dist/index.d.cts +4398 -0
- package/dist/index.d.ts +4398 -0
- package/dist/index.js +1 -0
- package/package.json +81 -0
- package/src/api/generated/ext_knowbase/_utils/fetchers/ext_knowbase__knowbase.ts +2983 -0
- package/src/api/generated/ext_knowbase/_utils/fetchers/index.ts +28 -0
- package/src/api/generated/ext_knowbase/_utils/hooks/ext_knowbase__knowbase.ts +999 -0
- package/src/api/generated/ext_knowbase/_utils/hooks/index.ts +28 -0
- package/src/api/generated/ext_knowbase/_utils/schemas/ArchiveItem.schema.ts +33 -0
- package/src/api/generated/ext_knowbase/_utils/schemas/ArchiveItemChunk.schema.ts +29 -0
- package/src/api/generated/ext_knowbase/_utils/schemas/ArchiveItemChunkDetail.schema.ts +30 -0
- package/src/api/generated/ext_knowbase/_utils/schemas/ArchiveItemChunkRequest.schema.ts +22 -0
- package/src/api/generated/ext_knowbase/_utils/schemas/ArchiveItemDetail.schema.ts +35 -0
- package/src/api/generated/ext_knowbase/_utils/schemas/ArchiveItemRequest.schema.ts +22 -0
- package/src/api/generated/ext_knowbase/_utils/schemas/ArchiveProcessingResult.schema.ts +26 -0
- package/src/api/generated/ext_knowbase/_utils/schemas/ArchiveSearchRequestRequest.schema.ts +25 -0
- package/src/api/generated/ext_knowbase/_utils/schemas/ArchiveSearchResult.schema.ts +24 -0
- package/src/api/generated/ext_knowbase/_utils/schemas/ArchiveStatistics.schema.ts +28 -0
- package/src/api/generated/ext_knowbase/_utils/schemas/ChatHistory.schema.ts +22 -0
- package/src/api/generated/ext_knowbase/_utils/schemas/ChatMessage.schema.ts +27 -0
- package/src/api/generated/ext_knowbase/_utils/schemas/ChatQueryRequest.schema.ts +22 -0
- package/src/api/generated/ext_knowbase/_utils/schemas/ChatResponse.schema.ts +26 -0
- package/src/api/generated/ext_knowbase/_utils/schemas/ChatResponseRequest.schema.ts +26 -0
- package/src/api/generated/ext_knowbase/_utils/schemas/ChatSession.schema.ts +29 -0
- package/src/api/generated/ext_knowbase/_utils/schemas/ChatSessionCreateRequest.schema.ts +22 -0
- package/src/api/generated/ext_knowbase/_utils/schemas/ChatSessionRequest.schema.ts +25 -0
- package/src/api/generated/ext_knowbase/_utils/schemas/ChatSource.schema.ts +21 -0
- package/src/api/generated/ext_knowbase/_utils/schemas/ChatSourceRequest.schema.ts +21 -0
- package/src/api/generated/ext_knowbase/_utils/schemas/ChunkRevectorizationRequestRequest.schema.ts +20 -0
- package/src/api/generated/ext_knowbase/_utils/schemas/Document.schema.ts +32 -0
- package/src/api/generated/ext_knowbase/_utils/schemas/DocumentArchive.schema.ts +44 -0
- package/src/api/generated/ext_knowbase/_utils/schemas/DocumentArchiveDetail.schema.ts +48 -0
- package/src/api/generated/ext_knowbase/_utils/schemas/DocumentArchiveList.schema.ts +35 -0
- package/src/api/generated/ext_knowbase/_utils/schemas/DocumentArchiveRequest.schema.ts +21 -0
- package/src/api/generated/ext_knowbase/_utils/schemas/DocumentCategory.schema.ts +23 -0
- package/src/api/generated/ext_knowbase/_utils/schemas/DocumentCategoryRequest.schema.ts +21 -0
- package/src/api/generated/ext_knowbase/_utils/schemas/DocumentCreateRequest.schema.ts +22 -0
- package/src/api/generated/ext_knowbase/_utils/schemas/DocumentProcessingStatus.schema.ts +23 -0
- package/src/api/generated/ext_knowbase/_utils/schemas/DocumentRequest.schema.ts +22 -0
- package/src/api/generated/ext_knowbase/_utils/schemas/DocumentStats.schema.ts +25 -0
- package/src/api/generated/ext_knowbase/_utils/schemas/PaginatedArchiveItemChunkList.schema.ts +24 -0
- package/src/api/generated/ext_knowbase/_utils/schemas/PaginatedArchiveItemList.schema.ts +24 -0
- package/src/api/generated/ext_knowbase/_utils/schemas/PaginatedArchiveSearchResultList.schema.ts +24 -0
- package/src/api/generated/ext_knowbase/_utils/schemas/PaginatedChatResponseList.schema.ts +24 -0
- package/src/api/generated/ext_knowbase/_utils/schemas/PaginatedChatSessionList.schema.ts +24 -0
- package/src/api/generated/ext_knowbase/_utils/schemas/PaginatedDocumentArchiveListList.schema.ts +24 -0
- package/src/api/generated/ext_knowbase/_utils/schemas/PaginatedDocumentList.schema.ts +24 -0
- package/src/api/generated/ext_knowbase/_utils/schemas/PaginatedPublicCategoryList.schema.ts +24 -0
- package/src/api/generated/ext_knowbase/_utils/schemas/PaginatedPublicDocumentListList.schema.ts +24 -0
- package/src/api/generated/ext_knowbase/_utils/schemas/PatchedArchiveItemChunkRequest.schema.ts +22 -0
- package/src/api/generated/ext_knowbase/_utils/schemas/PatchedArchiveItemRequest.schema.ts +22 -0
- package/src/api/generated/ext_knowbase/_utils/schemas/PatchedChatResponseRequest.schema.ts +26 -0
- package/src/api/generated/ext_knowbase/_utils/schemas/PatchedChatSessionRequest.schema.ts +25 -0
- package/src/api/generated/ext_knowbase/_utils/schemas/PatchedDocumentArchiveRequest.schema.ts +21 -0
- package/src/api/generated/ext_knowbase/_utils/schemas/PatchedDocumentRequest.schema.ts +22 -0
- package/src/api/generated/ext_knowbase/_utils/schemas/PublicCategory.schema.ts +21 -0
- package/src/api/generated/ext_knowbase/_utils/schemas/PublicDocument.schema.ts +25 -0
- package/src/api/generated/ext_knowbase/_utils/schemas/PublicDocumentList.schema.ts +24 -0
- package/src/api/generated/ext_knowbase/_utils/schemas/VectorizationResult.schema.ts +24 -0
- package/src/api/generated/ext_knowbase/_utils/schemas/VectorizationStatistics.schema.ts +26 -0
- package/src/api/generated/ext_knowbase/_utils/schemas/index.ts +70 -0
- package/src/api/generated/ext_knowbase/api-instance.ts +131 -0
- package/src/api/generated/ext_knowbase/client.ts +301 -0
- package/src/api/generated/ext_knowbase/enums.ts +241 -0
- package/src/api/generated/ext_knowbase/errors.ts +116 -0
- package/src/api/generated/ext_knowbase/ext_knowbase__knowbase/client.ts +666 -0
- package/src/api/generated/ext_knowbase/ext_knowbase__knowbase/index.ts +2 -0
- package/src/api/generated/ext_knowbase/ext_knowbase__knowbase/models.ts +1120 -0
- package/src/api/generated/ext_knowbase/http.ts +103 -0
- package/src/api/generated/ext_knowbase/index.ts +273 -0
- package/src/api/generated/ext_knowbase/logger.ts +259 -0
- package/src/api/generated/ext_knowbase/retry.ts +175 -0
- package/src/api/generated/ext_knowbase/schema.json +5865 -0
- package/src/api/generated/ext_knowbase/storage.ts +161 -0
- package/src/api/generated/ext_knowbase/validation-events.ts +133 -0
- package/src/api/index.ts +9 -0
- package/src/components/Chat/ChatUIContext.tsx +110 -0
- package/src/components/Chat/ChatWidget.tsx +476 -0
- package/src/components/Chat/README.md +122 -0
- package/src/components/Chat/components/MessageInput.tsx +124 -0
- package/src/components/Chat/components/MessageList.tsx +169 -0
- package/src/components/Chat/components/SessionList.tsx +192 -0
- package/src/components/Chat/components/index.ts +9 -0
- package/src/components/Chat/hooks/index.ts +6 -0
- package/src/components/Chat/hooks/useInfiniteSessions.ts +81 -0
- package/src/components/Chat/index.tsx +45 -0
- package/src/components/Chat/types.ts +81 -0
- package/src/config.ts +20 -0
- package/src/contexts/knowbase/ChatContext.tsx +173 -0
- package/src/contexts/knowbase/DocumentsContext.tsx +306 -0
- package/src/contexts/knowbase/KnowbaseProvider.tsx +47 -0
- package/src/contexts/knowbase/SessionsContext.tsx +175 -0
- package/src/contexts/knowbase/index.ts +63 -0
- package/src/contexts/knowbase/types.ts +69 -0
- package/src/hooks/index.ts +28 -0
- package/src/index.ts +22 -0
- package/src/utils/logger.ts +10 -0
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Storage adapters for cross-platform token storage.
|
|
3
|
+
*
|
|
4
|
+
* Supports:
|
|
5
|
+
* - LocalStorage (browser)
|
|
6
|
+
* - Cookies (SSR/browser)
|
|
7
|
+
* - Memory (Node.js/Electron/testing)
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import type { APILogger } from './logger';
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Storage adapter interface for cross-platform token storage.
|
|
14
|
+
*/
|
|
15
|
+
export interface StorageAdapter {
|
|
16
|
+
getItem(key: string): string | null;
|
|
17
|
+
setItem(key: string, value: string): void;
|
|
18
|
+
removeItem(key: string): void;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* LocalStorage adapter with safe try-catch for browser environments.
|
|
23
|
+
* Works in modern browsers with localStorage support.
|
|
24
|
+
*
|
|
25
|
+
* Note: This adapter uses window.localStorage and should only be used in browser/client environments.
|
|
26
|
+
* For server-side usage, use MemoryStorageAdapter or CookieStorageAdapter instead.
|
|
27
|
+
*/
|
|
28
|
+
export class LocalStorageAdapter implements StorageAdapter {
|
|
29
|
+
private logger?: APILogger;
|
|
30
|
+
|
|
31
|
+
constructor(logger?: APILogger) {
|
|
32
|
+
this.logger = logger;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
getItem(key: string): string | null {
|
|
36
|
+
try {
|
|
37
|
+
if (typeof window !== 'undefined' && window.localStorage) {
|
|
38
|
+
const value = localStorage.getItem(key);
|
|
39
|
+
this.logger?.debug(`LocalStorage.getItem("${key}"): ${value ? 'found' : 'not found'}`);
|
|
40
|
+
return value;
|
|
41
|
+
}
|
|
42
|
+
this.logger?.warn('LocalStorage not available: window.localStorage is undefined');
|
|
43
|
+
} catch (error) {
|
|
44
|
+
this.logger?.error('LocalStorage.getItem failed:', error);
|
|
45
|
+
}
|
|
46
|
+
return null;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
setItem(key: string, value: string): void {
|
|
50
|
+
try {
|
|
51
|
+
if (typeof window !== 'undefined' && window.localStorage) {
|
|
52
|
+
localStorage.setItem(key, value);
|
|
53
|
+
this.logger?.debug(`LocalStorage.setItem("${key}"): success`);
|
|
54
|
+
} else {
|
|
55
|
+
this.logger?.warn('LocalStorage not available: window.localStorage is undefined');
|
|
56
|
+
}
|
|
57
|
+
} catch (error) {
|
|
58
|
+
this.logger?.error('LocalStorage.setItem failed:', error);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
removeItem(key: string): void {
|
|
63
|
+
try {
|
|
64
|
+
if (typeof window !== 'undefined' && window.localStorage) {
|
|
65
|
+
localStorage.removeItem(key);
|
|
66
|
+
this.logger?.debug(`LocalStorage.removeItem("${key}"): success`);
|
|
67
|
+
} else {
|
|
68
|
+
this.logger?.warn('LocalStorage not available: window.localStorage is undefined');
|
|
69
|
+
}
|
|
70
|
+
} catch (error) {
|
|
71
|
+
this.logger?.error('LocalStorage.removeItem failed:', error);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Cookie-based storage adapter for SSR and browser environments.
|
|
78
|
+
* Useful for Next.js, Nuxt.js, and other SSR frameworks.
|
|
79
|
+
*/
|
|
80
|
+
export class CookieStorageAdapter implements StorageAdapter {
|
|
81
|
+
private logger?: APILogger;
|
|
82
|
+
|
|
83
|
+
constructor(logger?: APILogger) {
|
|
84
|
+
this.logger = logger;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
getItem(key: string): string | null {
|
|
88
|
+
try {
|
|
89
|
+
if (typeof document === 'undefined') {
|
|
90
|
+
this.logger?.warn('Cookies not available: document is undefined (SSR context?)');
|
|
91
|
+
return null;
|
|
92
|
+
}
|
|
93
|
+
const value = `; ${document.cookie}`;
|
|
94
|
+
const parts = value.split(`; ${key}=`);
|
|
95
|
+
if (parts.length === 2) {
|
|
96
|
+
const result = parts.pop()?.split(';').shift() || null;
|
|
97
|
+
this.logger?.debug(`CookieStorage.getItem("${key}"): ${result ? 'found' : 'not found'}`);
|
|
98
|
+
return result;
|
|
99
|
+
}
|
|
100
|
+
this.logger?.debug(`CookieStorage.getItem("${key}"): not found`);
|
|
101
|
+
} catch (error) {
|
|
102
|
+
this.logger?.error('CookieStorage.getItem failed:', error);
|
|
103
|
+
}
|
|
104
|
+
return null;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
setItem(key: string, value: string): void {
|
|
108
|
+
try {
|
|
109
|
+
if (typeof document !== 'undefined') {
|
|
110
|
+
document.cookie = `${key}=${value}; path=/; max-age=31536000`;
|
|
111
|
+
this.logger?.debug(`CookieStorage.setItem("${key}"): success`);
|
|
112
|
+
} else {
|
|
113
|
+
this.logger?.warn('Cookies not available: document is undefined (SSR context?)');
|
|
114
|
+
}
|
|
115
|
+
} catch (error) {
|
|
116
|
+
this.logger?.error('CookieStorage.setItem failed:', error);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
removeItem(key: string): void {
|
|
121
|
+
try {
|
|
122
|
+
if (typeof document !== 'undefined') {
|
|
123
|
+
document.cookie = `${key}=; path=/; expires=Thu, 01 Jan 1970 00:00:00 GMT`;
|
|
124
|
+
this.logger?.debug(`CookieStorage.removeItem("${key}"): success`);
|
|
125
|
+
} else {
|
|
126
|
+
this.logger?.warn('Cookies not available: document is undefined (SSR context?)');
|
|
127
|
+
}
|
|
128
|
+
} catch (error) {
|
|
129
|
+
this.logger?.error('CookieStorage.removeItem failed:', error);
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* In-memory storage adapter for Node.js, Electron, and testing environments.
|
|
136
|
+
* Data is stored in RAM and cleared when process exits.
|
|
137
|
+
*/
|
|
138
|
+
export class MemoryStorageAdapter implements StorageAdapter {
|
|
139
|
+
private storage: Map<string, string> = new Map();
|
|
140
|
+
private logger?: APILogger;
|
|
141
|
+
|
|
142
|
+
constructor(logger?: APILogger) {
|
|
143
|
+
this.logger = logger;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
getItem(key: string): string | null {
|
|
147
|
+
const value = this.storage.get(key) || null;
|
|
148
|
+
this.logger?.debug(`MemoryStorage.getItem("${key}"): ${value ? 'found' : 'not found'}`);
|
|
149
|
+
return value;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
setItem(key: string, value: string): void {
|
|
153
|
+
this.storage.set(key, value);
|
|
154
|
+
this.logger?.debug(`MemoryStorage.setItem("${key}"): success`);
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
removeItem(key: string): void {
|
|
158
|
+
this.storage.delete(key);
|
|
159
|
+
this.logger?.debug(`MemoryStorage.removeItem("${key}"): success`);
|
|
160
|
+
}
|
|
161
|
+
}
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Zod Validation Events - Browser CustomEvent integration
|
|
3
|
+
*
|
|
4
|
+
* Dispatches browser CustomEvents when Zod validation fails, allowing
|
|
5
|
+
* React/frontend apps to listen and handle validation errors globally.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```typescript
|
|
9
|
+
* // In your React app
|
|
10
|
+
* window.addEventListener('zod-validation-error', (event) => {
|
|
11
|
+
* const { operation, path, method, error, response } = event.detail;
|
|
12
|
+
* console.error(`Validation failed for ${method} ${path}`, error);
|
|
13
|
+
* // Show toast notification, log to Sentry, etc.
|
|
14
|
+
* });
|
|
15
|
+
* ```
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
import type { ZodError } from 'zod'
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Validation error event detail
|
|
22
|
+
*/
|
|
23
|
+
export interface ValidationErrorDetail {
|
|
24
|
+
/** Operation/function name that failed validation */
|
|
25
|
+
operation: string
|
|
26
|
+
/** API endpoint path */
|
|
27
|
+
path: string
|
|
28
|
+
/** HTTP method */
|
|
29
|
+
method: string
|
|
30
|
+
/** Zod validation error */
|
|
31
|
+
error: ZodError
|
|
32
|
+
/** Raw response data that failed validation */
|
|
33
|
+
response: any
|
|
34
|
+
/** Timestamp of the error */
|
|
35
|
+
timestamp: Date
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Custom event type for Zod validation errors
|
|
40
|
+
*/
|
|
41
|
+
export type ValidationErrorEvent = CustomEvent<ValidationErrorDetail>
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Dispatch a Zod validation error event.
|
|
45
|
+
*
|
|
46
|
+
* Only dispatches in browser environment (when window is defined).
|
|
47
|
+
* Safe to call in Node.js/SSR - will be a no-op.
|
|
48
|
+
*
|
|
49
|
+
* @param detail - Validation error details
|
|
50
|
+
*/
|
|
51
|
+
export function dispatchValidationError(detail: ValidationErrorDetail): void {
|
|
52
|
+
// Check if running in browser
|
|
53
|
+
if (typeof window === 'undefined') {
|
|
54
|
+
return
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
try {
|
|
58
|
+
const event = new CustomEvent<ValidationErrorDetail>('zod-validation-error', {
|
|
59
|
+
detail,
|
|
60
|
+
bubbles: true,
|
|
61
|
+
cancelable: false,
|
|
62
|
+
})
|
|
63
|
+
|
|
64
|
+
window.dispatchEvent(event)
|
|
65
|
+
} catch (error) {
|
|
66
|
+
// Silently fail - validation event dispatch should never crash the app
|
|
67
|
+
console.warn('Failed to dispatch validation error event:', error)
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Add a global listener for Zod validation errors.
|
|
73
|
+
*
|
|
74
|
+
* @param callback - Function to call when validation error occurs
|
|
75
|
+
* @returns Cleanup function to remove the listener
|
|
76
|
+
*
|
|
77
|
+
* @example
|
|
78
|
+
* ```typescript
|
|
79
|
+
* const cleanup = onValidationError(({ operation, error }) => {
|
|
80
|
+
* toast.error(`Validation failed in ${operation}`);
|
|
81
|
+
* logToSentry(error);
|
|
82
|
+
* });
|
|
83
|
+
*
|
|
84
|
+
* // Later, remove listener
|
|
85
|
+
* cleanup();
|
|
86
|
+
* ```
|
|
87
|
+
*/
|
|
88
|
+
export function onValidationError(
|
|
89
|
+
callback: (detail: ValidationErrorDetail) => void
|
|
90
|
+
): () => void {
|
|
91
|
+
if (typeof window === 'undefined') {
|
|
92
|
+
// Return no-op cleanup function for SSR
|
|
93
|
+
return () => {}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
const handler = (event: Event) => {
|
|
97
|
+
if (event instanceof CustomEvent) {
|
|
98
|
+
callback(event.detail)
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
window.addEventListener('zod-validation-error', handler)
|
|
103
|
+
|
|
104
|
+
// Return cleanup function
|
|
105
|
+
return () => {
|
|
106
|
+
window.removeEventListener('zod-validation-error', handler)
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Format Zod error for logging/display.
|
|
112
|
+
*
|
|
113
|
+
* @param error - Zod validation error
|
|
114
|
+
* @returns Formatted error message
|
|
115
|
+
*/
|
|
116
|
+
export function formatZodError(error: ZodError): string {
|
|
117
|
+
const issues = error.issues.map((issue, index) => {
|
|
118
|
+
const path = issue.path.join('.') || 'root'
|
|
119
|
+
const parts = [`${index + 1}. ${path}: ${issue.message}`]
|
|
120
|
+
|
|
121
|
+
if ('expected' in issue && issue.expected) {
|
|
122
|
+
parts.push(` Expected: ${issue.expected}`)
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
if ('received' in issue && issue.received) {
|
|
126
|
+
parts.push(` Received: ${issue.received}`)
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
return parts.join('\n')
|
|
130
|
+
})
|
|
131
|
+
|
|
132
|
+
return issues.join('\n')
|
|
133
|
+
}
|
package/src/api/index.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Knowbase Extension API
|
|
3
|
+
*
|
|
4
|
+
* Pre-configured API instance with shared authentication
|
|
5
|
+
*/
|
|
6
|
+
import { API } from './generated/ext_knowbase';
|
|
7
|
+
import { createExtensionAPI } from '@djangocfg/ext-base/api';
|
|
8
|
+
|
|
9
|
+
export const apiKnowbase = createExtensionAPI(API);
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Chat UI Context
|
|
3
|
+
* Manages UI state for chat widget (toggle, expand, minimize)
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
'use client';
|
|
7
|
+
|
|
8
|
+
import React, { createContext, useContext, useState, useCallback, type ReactNode } from 'react';
|
|
9
|
+
import type { ChatUIState } from './types';
|
|
10
|
+
|
|
11
|
+
// ─────────────────────────────────────────────────────────────────────────
|
|
12
|
+
// Context Type
|
|
13
|
+
// ─────────────────────────────────────────────────────────────────────────
|
|
14
|
+
|
|
15
|
+
export interface ChatUIContextValue {
|
|
16
|
+
uiState: ChatUIState;
|
|
17
|
+
toggleChat: () => void;
|
|
18
|
+
openChat: () => void;
|
|
19
|
+
closeChat: () => void;
|
|
20
|
+
expandChat: () => void;
|
|
21
|
+
collapseChat: () => void;
|
|
22
|
+
minimizeChat: () => void;
|
|
23
|
+
toggleSources: () => void;
|
|
24
|
+
toggleTimestamps: () => void;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
// ─────────────────────────────────────────────────────────────────────────
|
|
28
|
+
// Context
|
|
29
|
+
// ─────────────────────────────────────────────────────────────────────────
|
|
30
|
+
|
|
31
|
+
const ChatUIContext = createContext<ChatUIContextValue | undefined>(undefined);
|
|
32
|
+
|
|
33
|
+
// ─────────────────────────────────────────────────────────────────────────
|
|
34
|
+
// Provider
|
|
35
|
+
// ─────────────────────────────────────────────────────────────────────────
|
|
36
|
+
|
|
37
|
+
export interface ChatUIProviderProps {
|
|
38
|
+
children: ReactNode;
|
|
39
|
+
initialState?: Partial<ChatUIState>;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export function ChatUIProvider({ children, initialState }: ChatUIProviderProps) {
|
|
43
|
+
const [uiState, setUIState] = useState<ChatUIState>({
|
|
44
|
+
isOpen: false,
|
|
45
|
+
isExpanded: false,
|
|
46
|
+
isMinimized: false,
|
|
47
|
+
showSources: true,
|
|
48
|
+
showTimestamps: false,
|
|
49
|
+
...initialState,
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
const toggleChat = useCallback(() => {
|
|
53
|
+
setUIState((prev) => ({ ...prev, isOpen: !prev.isOpen }));
|
|
54
|
+
}, []);
|
|
55
|
+
|
|
56
|
+
const openChat = useCallback(() => {
|
|
57
|
+
setUIState((prev) => ({ ...prev, isOpen: true }));
|
|
58
|
+
}, []);
|
|
59
|
+
|
|
60
|
+
const closeChat = useCallback(() => {
|
|
61
|
+
setUIState((prev) => ({ ...prev, isOpen: false }));
|
|
62
|
+
}, []);
|
|
63
|
+
|
|
64
|
+
const expandChat = useCallback(() => {
|
|
65
|
+
setUIState((prev) => ({ ...prev, isExpanded: true, isMinimized: false }));
|
|
66
|
+
}, []);
|
|
67
|
+
|
|
68
|
+
const collapseChat = useCallback(() => {
|
|
69
|
+
setUIState((prev) => ({ ...prev, isExpanded: false }));
|
|
70
|
+
}, []);
|
|
71
|
+
|
|
72
|
+
const minimizeChat = useCallback(() => {
|
|
73
|
+
setUIState((prev) => ({ ...prev, isMinimized: true, isExpanded: false }));
|
|
74
|
+
}, []);
|
|
75
|
+
|
|
76
|
+
const toggleSources = useCallback(() => {
|
|
77
|
+
setUIState((prev) => ({ ...prev, showSources: !prev.showSources }));
|
|
78
|
+
}, []);
|
|
79
|
+
|
|
80
|
+
const toggleTimestamps = useCallback(() => {
|
|
81
|
+
setUIState((prev) => ({ ...prev, showTimestamps: !prev.showTimestamps }));
|
|
82
|
+
}, []);
|
|
83
|
+
|
|
84
|
+
const value: ChatUIContextValue = {
|
|
85
|
+
uiState,
|
|
86
|
+
toggleChat,
|
|
87
|
+
openChat,
|
|
88
|
+
closeChat,
|
|
89
|
+
expandChat,
|
|
90
|
+
collapseChat,
|
|
91
|
+
minimizeChat,
|
|
92
|
+
toggleSources,
|
|
93
|
+
toggleTimestamps,
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
return <ChatUIContext.Provider value={value}>{children}</ChatUIContext.Provider>;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
// ─────────────────────────────────────────────────────────────────────────
|
|
100
|
+
// Hook
|
|
101
|
+
// ─────────────────────────────────────────────────────────────────────────
|
|
102
|
+
|
|
103
|
+
export function useChatUI(): ChatUIContextValue {
|
|
104
|
+
const context = useContext(ChatUIContext);
|
|
105
|
+
if (!context) {
|
|
106
|
+
throw new Error('useChatUI must be used within ChatUIProvider');
|
|
107
|
+
}
|
|
108
|
+
return context;
|
|
109
|
+
}
|
|
110
|
+
|