@docyrus/ui-pro-ai-assistant 0.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.
- package/dist/index.js +26161 -0
- package/dist/index.js.map +1 -0
- package/package.json +155 -0
- package/src/components/assistant-animations.tsx +29 -0
- package/src/components/assistant-dialogs.tsx +235 -0
- package/src/components/code-view.tsx +278 -0
- package/src/components/create-agent-task.tsx +104 -0
- package/src/components/create-new-work-version.tsx +30 -0
- package/src/components/extract-web.tsx +160 -0
- package/src/components/forward-to-agent.tsx +90 -0
- package/src/components/generate-chart.tsx +101 -0
- package/src/components/generative-action-button.tsx +122 -0
- package/src/components/generative-tool.tsx +685 -0
- package/src/components/generative-ui-object.tsx +210 -0
- package/src/components/input-area.tsx +1209 -0
- package/src/components/json-schema-layout.tsx +326 -0
- package/src/components/list-item-card.tsx +92 -0
- package/src/components/mermaid-diagram.tsx +192 -0
- package/src/components/preview-image.tsx +47 -0
- package/src/components/request-approval.tsx +48 -0
- package/src/components/request-user-input.tsx +270 -0
- package/src/components/search-web.tsx +319 -0
- package/src/components/sheet-command.tsx +88 -0
- package/src/components/shell-canvas.tsx +122 -0
- package/src/components/show-advanced-data-table.tsx +352 -0
- package/src/components/show-generated-content-options.tsx +93 -0
- package/src/components/show-people-cards.tsx +180 -0
- package/src/components/subagent-tool.tsx +180 -0
- package/src/components/text-editor-tool.tsx +328 -0
- package/src/components/work-canvas.tsx +88 -0
- package/src/components/work-card.tsx +42 -0
- package/src/declarations.d.ts +1 -0
- package/src/docy-assistant.tsx +1962 -0
- package/src/hooks/index.ts +7 -0
- package/src/hooks/use-assistant-api.ts +507 -0
- package/src/hooks/use-deployment-data.ts +162 -0
- package/src/hooks/use-project-state.ts +347 -0
- package/src/hooks/use-session-state.ts +207 -0
- package/src/hooks/use-speech-recognition.ts +137 -0
- package/src/hooks/use-ui-state.ts +185 -0
- package/src/hooks/use-works-state.ts +146 -0
- package/src/i18n/context.tsx +253 -0
- package/src/i18n/index.ts +19 -0
- package/src/i18n/locales/de.json +198 -0
- package/src/i18n/locales/el.json +198 -0
- package/src/i18n/locales/en.json +226 -0
- package/src/i18n/locales/es.json +198 -0
- package/src/i18n/locales/fr.json +198 -0
- package/src/i18n/locales/it.json +198 -0
- package/src/i18n/locales/pt.json +198 -0
- package/src/i18n/locales/sl.json +198 -0
- package/src/i18n/locales/tr.json +211 -0
- package/src/i18n/types.ts +23 -0
- package/src/i18n/use-translation.ts +17 -0
- package/src/index.ts +18 -0
- package/src/internal/plate-editor/editor/auth-context.ts +11 -0
- package/src/internal/plate-editor/editor/editor-base-kit.tsx +39 -0
- package/src/internal/plate-editor/editor/editor-kit.tsx +89 -0
- package/src/internal/plate-editor/editor/plate-editor.tsx +75 -0
- package/src/internal/plate-editor/editor/plate-types.ts +126 -0
- package/src/internal/plate-editor/editor/plugins/ai-kit.tsx +172 -0
- package/src/internal/plate-editor/editor/plugins/autoformat-kit.tsx +211 -0
- package/src/internal/plate-editor/editor/plugins/basic-blocks-base-kit.tsx +26 -0
- package/src/internal/plate-editor/editor/plugins/basic-blocks-kit.tsx +51 -0
- package/src/internal/plate-editor/editor/plugins/basic-marks-base-kit.tsx +24 -0
- package/src/internal/plate-editor/editor/plugins/basic-marks-kit.tsx +38 -0
- package/src/internal/plate-editor/editor/plugins/basic-nodes-kit.tsx +6 -0
- package/src/internal/plate-editor/editor/plugins/block-menu-kit.tsx +14 -0
- package/src/internal/plate-editor/editor/plugins/block-placeholder-kit.tsx +17 -0
- package/src/internal/plate-editor/editor/plugins/block-selection-kit.tsx +31 -0
- package/src/internal/plate-editor/editor/plugins/callout-base-kit.tsx +5 -0
- package/src/internal/plate-editor/editor/plugins/callout-kit.tsx +7 -0
- package/src/internal/plate-editor/editor/plugins/code-block-base-kit.tsx +23 -0
- package/src/internal/plate-editor/editor/plugins/code-block-kit.tsx +26 -0
- package/src/internal/plate-editor/editor/plugins/column-base-kit.tsx +8 -0
- package/src/internal/plate-editor/editor/plugins/column-kit.tsx +7 -0
- package/src/internal/plate-editor/editor/plugins/comment-base-kit.tsx +5 -0
- package/src/internal/plate-editor/editor/plugins/comment-kit.tsx +174 -0
- package/src/internal/plate-editor/editor/plugins/copilot-kit.tsx +68 -0
- package/src/internal/plate-editor/editor/plugins/cursor-overlay-kit.tsx +13 -0
- package/src/internal/plate-editor/editor/plugins/date-base-kit.tsx +5 -0
- package/src/internal/plate-editor/editor/plugins/date-kit.tsx +7 -0
- package/src/internal/plate-editor/editor/plugins/discussion-kit.tsx +36 -0
- package/src/internal/plate-editor/editor/plugins/dnd-kit.tsx +27 -0
- package/src/internal/plate-editor/editor/plugins/docx-export-kit.tsx +43 -0
- package/src/internal/plate-editor/editor/plugins/docx-kit.tsx +6 -0
- package/src/internal/plate-editor/editor/plugins/emoji-kit.tsx +15 -0
- package/src/internal/plate-editor/editor/plugins/exit-break-kit.tsx +12 -0
- package/src/internal/plate-editor/editor/plugins/floating-toolbar-kit.tsx +19 -0
- package/src/internal/plate-editor/editor/plugins/font-base-kit.tsx +36 -0
- package/src/internal/plate-editor/editor/plugins/font-kit.tsx +47 -0
- package/src/internal/plate-editor/editor/plugins/indent-base-kit.tsx +19 -0
- package/src/internal/plate-editor/editor/plugins/indent-kit.tsx +22 -0
- package/src/internal/plate-editor/editor/plugins/link-base-kit.tsx +5 -0
- package/src/internal/plate-editor/editor/plugins/link-kit.tsx +35 -0
- package/src/internal/plate-editor/editor/plugins/list-base-kit.tsx +24 -0
- package/src/internal/plate-editor/editor/plugins/list-kit.tsx +27 -0
- package/src/internal/plate-editor/editor/plugins/markdown-kit.tsx +18 -0
- package/src/internal/plate-editor/editor/plugins/math-base-kit.tsx +8 -0
- package/src/internal/plate-editor/editor/plugins/math-kit.tsx +10 -0
- package/src/internal/plate-editor/editor/plugins/media-base-kit.tsx +37 -0
- package/src/internal/plate-editor/editor/plugins/media-kit.tsx +53 -0
- package/src/internal/plate-editor/editor/plugins/mention-base-kit.tsx +5 -0
- package/src/internal/plate-editor/editor/plugins/mention-kit.tsx +36 -0
- package/src/internal/plate-editor/editor/plugins/slash-kit.tsx +17 -0
- package/src/internal/plate-editor/editor/plugins/suggestion-base-kit.tsx +5 -0
- package/src/internal/plate-editor/editor/plugins/suggestion-kit.tsx +95 -0
- package/src/internal/plate-editor/editor/plugins/table-base-kit.tsx +20 -0
- package/src/internal/plate-editor/editor/plugins/table-kit.tsx +22 -0
- package/src/internal/plate-editor/editor/plugins/toc-base-kit.tsx +5 -0
- package/src/internal/plate-editor/editor/plugins/toc-kit.tsx +14 -0
- package/src/internal/plate-editor/editor/plugins/toggle-base-kit.tsx +5 -0
- package/src/internal/plate-editor/editor/plugins/toggle-kit.tsx +9 -0
- package/src/internal/plate-editor/editor/transforms.ts +165 -0
- package/src/internal/plate-editor/editor/use-chat.ts +152 -0
- package/src/internal/plate-editor/hooks/index.ts +3 -0
- package/src/internal/plate-editor/hooks/use-copy-to-clipboard.ts +31 -0
- package/src/internal/plate-editor/hooks/use-is-touch-device.ts +26 -0
- package/src/internal/plate-editor/hooks/use-lock-scroll.ts +21 -0
- package/src/internal/plate-editor/hooks/use-media-query.ts +44 -0
- package/src/internal/plate-editor/hooks/use-mounted.ts +18 -0
- package/src/internal/plate-editor/hooks/use-on-click-outside.ts +114 -0
- package/src/internal/plate-editor/hooks/use-upload-file.ts +81 -0
- package/src/internal/plate-editor/i18n/context.tsx +58 -0
- package/src/internal/plate-editor/i18n/index.ts +3 -0
- package/src/internal/plate-editor/i18n/locales/de.json +57 -0
- package/src/internal/plate-editor/i18n/locales/el.json +57 -0
- package/src/internal/plate-editor/i18n/locales/en.json +57 -0
- package/src/internal/plate-editor/i18n/locales/es.json +57 -0
- package/src/internal/plate-editor/i18n/locales/fr.json +57 -0
- package/src/internal/plate-editor/i18n/locales/it.json +57 -0
- package/src/internal/plate-editor/i18n/locales/pt.json +57 -0
- package/src/internal/plate-editor/i18n/locales/sl.json +57 -0
- package/src/internal/plate-editor/i18n/locales/tr.json +57 -0
- package/src/internal/plate-editor/i18n/types.ts +59 -0
- package/src/internal/plate-editor/i18n/use-translation.ts +22 -0
- package/src/internal/plate-editor/index.ts +39 -0
- package/src/internal/plate-editor/lib/ai-output-converter.ts +153 -0
- package/src/internal/plate-editor/lib/download-file.ts +17 -0
- package/src/internal/plate-editor/plate-ui/ai-chat-editor.tsx +26 -0
- package/src/internal/plate-editor/plate-ui/ai-menu.tsx +828 -0
- package/src/internal/plate-editor/plate-ui/ai-node.tsx +41 -0
- package/src/internal/plate-editor/plate-ui/ai-toolbar-button.tsx +27 -0
- package/src/internal/plate-editor/plate-ui/alert-dialog.tsx +147 -0
- package/src/internal/plate-editor/plate-ui/align-toolbar-button.tsx +90 -0
- package/src/internal/plate-editor/plate-ui/avatar.tsx +3 -0
- package/src/internal/plate-editor/plate-ui/block-context-menu.tsx +106 -0
- package/src/internal/plate-editor/plate-ui/block-discussion.tsx +364 -0
- package/src/internal/plate-editor/plate-ui/block-draggable.tsx +556 -0
- package/src/internal/plate-editor/plate-ui/block-list-static.tsx +78 -0
- package/src/internal/plate-editor/plate-ui/block-list.tsx +85 -0
- package/src/internal/plate-editor/plate-ui/block-menu.tsx +557 -0
- package/src/internal/plate-editor/plate-ui/block-selection.tsx +47 -0
- package/src/internal/plate-editor/plate-ui/block-suggestion.tsx +469 -0
- package/src/internal/plate-editor/plate-ui/blockquote-node-static.tsx +10 -0
- package/src/internal/plate-editor/plate-ui/blockquote-node.tsx +11 -0
- package/src/internal/plate-editor/plate-ui/button.tsx +190 -0
- package/src/internal/plate-editor/plate-ui/calendar.tsx +3 -0
- package/src/internal/plate-editor/plate-ui/callout-node-static.tsx +76 -0
- package/src/internal/plate-editor/plate-ui/callout-node.tsx +54 -0
- package/src/internal/plate-editor/plate-ui/caption.tsx +48 -0
- package/src/internal/plate-editor/plate-ui/checkbox.tsx +3 -0
- package/src/internal/plate-editor/plate-ui/code-block-node-static.tsx +172 -0
- package/src/internal/plate-editor/plate-ui/code-block-node.tsx +228 -0
- package/src/internal/plate-editor/plate-ui/code-node-static.tsx +11 -0
- package/src/internal/plate-editor/plate-ui/code-node.tsx +12 -0
- package/src/internal/plate-editor/plate-ui/column-node-static.tsx +65 -0
- package/src/internal/plate-editor/plate-ui/column-node.tsx +24 -0
- package/src/internal/plate-editor/plate-ui/command.tsx +204 -0
- package/src/internal/plate-editor/plate-ui/comment-node-static.tsx +12 -0
- package/src/internal/plate-editor/plate-ui/comment-node.tsx +45 -0
- package/src/internal/plate-editor/plate-ui/comment-toolbar-button.tsx +24 -0
- package/src/internal/plate-editor/plate-ui/comment.tsx +619 -0
- package/src/internal/plate-editor/plate-ui/cursor-overlay.tsx +85 -0
- package/src/internal/plate-editor/plate-ui/date-node-static.tsx +43 -0
- package/src/internal/plate-editor/plate-ui/date-node.tsx +56 -0
- package/src/internal/plate-editor/plate-ui/dialog.tsx +426 -0
- package/src/internal/plate-editor/plate-ui/dropdown-menu.tsx +266 -0
- package/src/internal/plate-editor/plate-ui/editor-static.tsx +40 -0
- package/src/internal/plate-editor/plate-ui/editor.tsx +148 -0
- package/src/internal/plate-editor/plate-ui/emoji-node.tsx +48 -0
- package/src/internal/plate-editor/plate-ui/emoji-toolbar-button.tsx +626 -0
- package/src/internal/plate-editor/plate-ui/equation-node-static.tsx +155 -0
- package/src/internal/plate-editor/plate-ui/equation-node.tsx +226 -0
- package/src/internal/plate-editor/plate-ui/equation-toolbar-button.tsx +26 -0
- package/src/internal/plate-editor/plate-ui/export-toolbar-button.tsx +208 -0
- package/src/internal/plate-editor/plate-ui/fixed-toolbar-buttons.tsx +157 -0
- package/src/internal/plate-editor/plate-ui/fixed-toolbar.tsx +27 -0
- package/src/internal/plate-editor/plate-ui/floating-discussion.tsx +1129 -0
- package/src/internal/plate-editor/plate-ui/floating-toolbar-buttons.tsx +129 -0
- package/src/internal/plate-editor/plate-ui/floating-toolbar.tsx +99 -0
- package/src/internal/plate-editor/plate-ui/font-color-toolbar-button.tsx +211 -0
- package/src/internal/plate-editor/plate-ui/font-size-toolbar-button.tsx +154 -0
- package/src/internal/plate-editor/plate-ui/ghost-text.tsx +20 -0
- package/src/internal/plate-editor/plate-ui/heading-node-static.tsx +52 -0
- package/src/internal/plate-editor/plate-ui/heading-node.tsx +56 -0
- package/src/internal/plate-editor/plate-ui/highlight-node-static.tsx +9 -0
- package/src/internal/plate-editor/plate-ui/highlight-node.tsx +11 -0
- package/src/internal/plate-editor/plate-ui/history-toolbar-button.tsx +52 -0
- package/src/internal/plate-editor/plate-ui/hover-card.tsx +7 -0
- package/src/internal/plate-editor/plate-ui/hr-node-static.tsx +18 -0
- package/src/internal/plate-editor/plate-ui/hr-node.tsx +28 -0
- package/src/internal/plate-editor/plate-ui/import-toolbar-button.tsx +124 -0
- package/src/internal/plate-editor/plate-ui/indent-toolbar-button.tsx +34 -0
- package/src/internal/plate-editor/plate-ui/inline-combobox.tsx +409 -0
- package/src/internal/plate-editor/plate-ui/input.tsx +39 -0
- package/src/internal/plate-editor/plate-ui/insert-toolbar-button.tsx +260 -0
- package/src/internal/plate-editor/plate-ui/label.tsx +1 -0
- package/src/internal/plate-editor/plate-ui/line-height-toolbar-button.tsx +71 -0
- package/src/internal/plate-editor/plate-ui/link-node-static.tsx +15 -0
- package/src/internal/plate-editor/plate-ui/link-node.tsx +33 -0
- package/src/internal/plate-editor/plate-ui/link-toolbar-button.tsx +30 -0
- package/src/internal/plate-editor/plate-ui/link-toolbar.tsx +149 -0
- package/src/internal/plate-editor/plate-ui/list-toolbar-button.tsx +179 -0
- package/src/internal/plate-editor/plate-ui/mark-toolbar-button.tsx +36 -0
- package/src/internal/plate-editor/plate-ui/media-audio-node-static.tsx +21 -0
- package/src/internal/plate-editor/plate-ui/media-audio-node.tsx +32 -0
- package/src/internal/plate-editor/plate-ui/media-embed-node.tsx +103 -0
- package/src/internal/plate-editor/plate-ui/media-file-node-static.tsx +30 -0
- package/src/internal/plate-editor/plate-ui/media-file-node.tsx +52 -0
- package/src/internal/plate-editor/plate-ui/media-image-node-static.tsx +37 -0
- package/src/internal/plate-editor/plate-ui/media-image-node.tsx +183 -0
- package/src/internal/plate-editor/plate-ui/media-placeholder-node.tsx +441 -0
- package/src/internal/plate-editor/plate-ui/media-preview-dialog.tsx +127 -0
- package/src/internal/plate-editor/plate-ui/media-toolbar-button.tsx +229 -0
- package/src/internal/plate-editor/plate-ui/media-toolbar.tsx +216 -0
- package/src/internal/plate-editor/plate-ui/media-upload-toast.tsx +73 -0
- package/src/internal/plate-editor/plate-ui/media-video-node-static.tsx +35 -0
- package/src/internal/plate-editor/plate-ui/media-video-node.tsx +119 -0
- package/src/internal/plate-editor/plate-ui/mention-node-static.tsx +46 -0
- package/src/internal/plate-editor/plate-ui/mention-node.tsx +79 -0
- package/src/internal/plate-editor/plate-ui/menu.tsx +532 -0
- package/src/internal/plate-editor/plate-ui/mode-toolbar-button.tsx +126 -0
- package/src/internal/plate-editor/plate-ui/more-toolbar-button.tsx +34 -0
- package/src/internal/plate-editor/plate-ui/paragraph-node-static.tsx +15 -0
- package/src/internal/plate-editor/plate-ui/paragraph-node.tsx +16 -0
- package/src/internal/plate-editor/plate-ui/popover.tsx +77 -0
- package/src/internal/plate-editor/plate-ui/progress.tsx +1 -0
- package/src/internal/plate-editor/plate-ui/remote-cursor-overlay.tsx +81 -0
- package/src/internal/plate-editor/plate-ui/resize-handle.tsx +88 -0
- package/src/internal/plate-editor/plate-ui/separator.tsx +43 -0
- package/src/internal/plate-editor/plate-ui/slash-node.tsx +435 -0
- package/src/internal/plate-editor/plate-ui/spinner.tsx +1 -0
- package/src/internal/plate-editor/plate-ui/suggestion-node-static.tsx +35 -0
- package/src/internal/plate-editor/plate-ui/suggestion-node.tsx +168 -0
- package/src/internal/plate-editor/plate-ui/suggestion-toolbar-button.tsx +24 -0
- package/src/internal/plate-editor/plate-ui/table-node-static.tsx +85 -0
- package/src/internal/plate-editor/plate-ui/table-node.tsx +285 -0
- package/src/internal/plate-editor/plate-ui/table-toolbar-button.tsx +254 -0
- package/src/internal/plate-editor/plate-ui/tabs.tsx +3 -0
- package/src/internal/plate-editor/plate-ui/textarea.tsx +58 -0
- package/src/internal/plate-editor/plate-ui/toc-node-static.tsx +142 -0
- package/src/internal/plate-editor/plate-ui/toc-node.tsx +57 -0
- package/src/internal/plate-editor/plate-ui/toc-sidebar.tsx +50 -0
- package/src/internal/plate-editor/plate-ui/toggle-node-static.tsx +18 -0
- package/src/internal/plate-editor/plate-ui/toggle-node.tsx +33 -0
- package/src/internal/plate-editor/plate-ui/toggle-toolbar-button.tsx +26 -0
- package/src/internal/plate-editor/plate-ui/toggle.tsx +3 -0
- package/src/internal/plate-editor/plate-ui/toolbar.tsx +380 -0
- package/src/internal/plate-editor/plate-ui/tooltip.tsx +149 -0
- package/src/internal/plate-editor/plate-ui/turn-into-toolbar-button.tsx +177 -0
- package/src/internal/plate-editor/types/index.ts +22 -0
- package/src/internal/plate-editor/vite.ts +284 -0
- package/src/internal/sheets/components/univer-sheets.tsx +1104 -0
- package/src/internal/sheets/i18n/context.tsx +183 -0
- package/src/internal/sheets/i18n/index.ts +19 -0
- package/src/internal/sheets/i18n/locales/de.json +21 -0
- package/src/internal/sheets/i18n/locales/el.json +21 -0
- package/src/internal/sheets/i18n/locales/en.json +21 -0
- package/src/internal/sheets/i18n/locales/es.json +21 -0
- package/src/internal/sheets/i18n/locales/fr.json +21 -0
- package/src/internal/sheets/i18n/locales/it.json +21 -0
- package/src/internal/sheets/i18n/locales/pt.json +21 -0
- package/src/internal/sheets/i18n/locales/sl.json +21 -0
- package/src/internal/sheets/i18n/locales/tr.json +21 -0
- package/src/internal/sheets/i18n/types.ts +23 -0
- package/src/internal/sheets/i18n/use-translation.ts +17 -0
- package/src/internal/sheets/index.ts +14 -0
- package/src/internal/sheets/types/css.d.ts +11 -0
- package/src/internal/sheets/types/index.ts +260 -0
- package/src/internal/sheets/xlsx.ts +1169 -0
- package/src/lib/api-client.ts +77 -0
- package/src/lib/assistant-api-actions.ts +549 -0
- package/src/lib/assistant-config.tsx +71 -0
- package/src/lib/class-utils.ts +40 -0
- package/src/lib/index.ts +7 -0
- package/src/lib/message-utils.ts +131 -0
- package/src/tools/tools-schema.json +512 -0
- package/src/types/index.ts +235 -0
- package/src/views/assistant-view.tsx +1137 -0
- package/src/views/canvas-app.tsx +839 -0
- package/src/views/canvas-code.tsx +93 -0
- package/src/views/canvas-deep-research.tsx +44 -0
- package/src/views/canvas-image.tsx +25 -0
- package/src/views/canvas-record-view.tsx +285 -0
- package/src/views/canvas-spreadsheet.tsx +125 -0
- package/src/views/canvas-text.tsx +52 -0
- package/src/views/canvas.tsx +274 -0
- package/src/views/chat-panel.tsx +149 -0
- package/src/views/index.ts +20 -0
- package/src/views/memories-panel.tsx +365 -0
- package/src/views/message-list.tsx +370 -0
- package/src/views/project-detail.tsx +257 -0
- package/src/views/projects-panel.tsx +131 -0
- package/src/views/sessions-list.tsx +98 -0
- package/src/views/sidebar-content.tsx +256 -0
- package/src/views/work-detail.tsx +98 -0
- package/src/vite.ts +284 -0
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
|
|
3
|
+
import { useMemo } from 'react';
|
|
4
|
+
|
|
5
|
+
import { AsyncTokenManager, RestApiClient } from '@docyrus/api-client';
|
|
6
|
+
|
|
7
|
+
import { useAssistantConfig } from './assistant-config';
|
|
8
|
+
|
|
9
|
+
// Response type that matches the API format
|
|
10
|
+
export interface ApiResponse {
|
|
11
|
+
success: boolean;
|
|
12
|
+
data: unknown;
|
|
13
|
+
error?: string;
|
|
14
|
+
error_description?: string;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Hook to create an API client for the assistant
|
|
19
|
+
* Uses @docyrus/api-client with AsyncTokenManager
|
|
20
|
+
*/
|
|
21
|
+
export function useApiClient() {
|
|
22
|
+
const config = useAssistantConfig();
|
|
23
|
+
|
|
24
|
+
return useMemo(() => {
|
|
25
|
+
// Create async token manager that uses config.getAuthToken
|
|
26
|
+
const tokenManager = new AsyncTokenManager(
|
|
27
|
+
async () => {
|
|
28
|
+
const token = await config.getAuthToken();
|
|
29
|
+
|
|
30
|
+
return token || null;
|
|
31
|
+
},
|
|
32
|
+
async () => {
|
|
33
|
+
// No-op: Token is managed externally
|
|
34
|
+
},
|
|
35
|
+
async () => {
|
|
36
|
+
// No-op: Token is managed externally
|
|
37
|
+
}
|
|
38
|
+
);
|
|
39
|
+
|
|
40
|
+
// Create RestApiClient instance
|
|
41
|
+
const client = new RestApiClient({
|
|
42
|
+
baseURL: config.apiBaseUrl,
|
|
43
|
+
tokenManager
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
// Wrapper to catch errors and return consistent ApiResponse format
|
|
47
|
+
async function wrapRequest(request: () => Promise<unknown>): Promise<ApiResponse> {
|
|
48
|
+
try {
|
|
49
|
+
const data = await request();
|
|
50
|
+
|
|
51
|
+
// If the API returns { success, data, error }, pass it through
|
|
52
|
+
if (data && typeof data === 'object' && 'success' in data) {
|
|
53
|
+
return data as ApiResponse;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// Otherwise wrap in success response
|
|
57
|
+
return { success: true, data };
|
|
58
|
+
} catch (error: unknown) {
|
|
59
|
+
const errorMessage = error instanceof Error ? error.message : 'Unknown error';
|
|
60
|
+
|
|
61
|
+
return {
|
|
62
|
+
success: false,
|
|
63
|
+
data: undefined,
|
|
64
|
+
error: errorMessage
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
return {
|
|
70
|
+
get: <T = unknown>(endpoint: string, params?: Record<string, unknown>) => wrapRequest(() => client.get<T>(endpoint, params as Record<string, string | number | boolean | null | undefined>)),
|
|
71
|
+
post: (endpoint: string, body?: unknown) => wrapRequest(() => client.post(endpoint, body)),
|
|
72
|
+
put: (endpoint: string, body?: unknown) => wrapRequest(() => client.put(endpoint, body)),
|
|
73
|
+
patch: (endpoint: string, body?: unknown) => wrapRequest(() => client.patch(endpoint, body)),
|
|
74
|
+
delete: (endpoint: string) => wrapRequest(() => client.delete(endpoint))
|
|
75
|
+
};
|
|
76
|
+
}, [config]);
|
|
77
|
+
}
|
|
@@ -0,0 +1,549 @@
|
|
|
1
|
+
|
|
2
|
+
import { toast } from 'sonner';
|
|
3
|
+
|
|
4
|
+
import { type ProjectState } from '../hooks/use-project-state';
|
|
5
|
+
import { type SessionState } from '../hooks/use-session-state';
|
|
6
|
+
import {
|
|
7
|
+
type ApiResponse, type AssistantSession, type Project, type Work
|
|
8
|
+
} from '../types';
|
|
9
|
+
|
|
10
|
+
import { processMessagesData } from './message-utils';
|
|
11
|
+
|
|
12
|
+
type ApiClient = {
|
|
13
|
+
get: (url: string, params?: any) => Promise<any>;
|
|
14
|
+
post: (url: string, data?: any) => Promise<any>;
|
|
15
|
+
patch: (url: string, data?: any) => Promise<any>;
|
|
16
|
+
delete: (url: string) => Promise<any>;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
type TranslationFn = (key: string) => string;
|
|
20
|
+
|
|
21
|
+
// Fetch threads for an agent
|
|
22
|
+
export async function fetchAgentThreads(
|
|
23
|
+
apiClient: ApiClient,
|
|
24
|
+
tenantAiAgentId: string,
|
|
25
|
+
onSuccess: (threads: AssistantSession[]) => void,
|
|
26
|
+
userId?: string | null,
|
|
27
|
+
deploymentId?: string | null
|
|
28
|
+
) {
|
|
29
|
+
try {
|
|
30
|
+
const apiParams: any = {
|
|
31
|
+
columns: 'id,subject,created_on,last_modified_on,body_text,tenant_ai_agent_id,created_by',
|
|
32
|
+
orderBy: JSON.stringify({ field: 'created_on', direction: 'desc' })
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
const agentRule = deploymentId ? { field: 'tenant_ai_agent_deployment_id', operator: '=', value: deploymentId } : { field: 'tenant_ai_agent_id', operator: '=', value: tenantAiAgentId };
|
|
36
|
+
|
|
37
|
+
const rules: any[] = [agentRule, { field: 'archived', operator: '=', value: 'false' }];
|
|
38
|
+
|
|
39
|
+
if (userId) {
|
|
40
|
+
rules.push({
|
|
41
|
+
combinator: 'or',
|
|
42
|
+
rules: [{ field: 'record_owner', operator: '=', value: userId }, { field: 'record_owner', operator: 'shared_to_me' }]
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
apiParams.filters = JSON.stringify({ combinator: 'and', rules });
|
|
47
|
+
|
|
48
|
+
const response = (await apiClient.get('/apps/base/data-sources/thread/items', apiParams)) as ApiResponse<any[]>;
|
|
49
|
+
|
|
50
|
+
if (response.success && response.data) {
|
|
51
|
+
const items = Array.isArray(response.data) ? response.data : (response.data as any).items || [];
|
|
52
|
+
const threads: AssistantSession[] = items.map((item: any) => ({
|
|
53
|
+
id: item.id,
|
|
54
|
+
title: item.subject,
|
|
55
|
+
messages: [],
|
|
56
|
+
createdAt: new Date(item.created_on),
|
|
57
|
+
updatedAt: new Date(item.last_modified_on || item.created_on)
|
|
58
|
+
}));
|
|
59
|
+
|
|
60
|
+
onSuccess(threads);
|
|
61
|
+
} else {
|
|
62
|
+
console.error('Failed to fetch threads:', response.error || (response as any).message);
|
|
63
|
+
}
|
|
64
|
+
} catch (error) {
|
|
65
|
+
console.error('Failed to fetch threads:', error);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
// Fetch threads for a specific project
|
|
70
|
+
export async function fetchProjectThreads(
|
|
71
|
+
apiClient: ApiClient,
|
|
72
|
+
projectId: string,
|
|
73
|
+
onSuccess: (projectId: string, threads: AssistantSession[]) => void
|
|
74
|
+
) {
|
|
75
|
+
try {
|
|
76
|
+
const apiParams: any = {
|
|
77
|
+
columns: 'id,subject,created_on,last_modified_on,body_text,tenant_ai_agent_id,tenant_ai_project_id ',
|
|
78
|
+
orderBy: JSON.stringify({ field: 'created_on', direction: 'desc' })
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
apiParams.filters = JSON.stringify({
|
|
82
|
+
rules: [{ field: 'tenant_ai_project_id ', operator: '=', value: projectId }]
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
const response = (await apiClient.get('/apps/base/data-sources/thread/items', apiParams)) as ApiResponse<any[]>;
|
|
86
|
+
|
|
87
|
+
if (response.success && response.data) {
|
|
88
|
+
const items = Array.isArray(response.data) ? response.data : (response.data as any).items || [];
|
|
89
|
+
const threads: AssistantSession[] = items.map((item: any) => ({
|
|
90
|
+
id: item.id,
|
|
91
|
+
title: item.subject,
|
|
92
|
+
messages: [],
|
|
93
|
+
createdAt: new Date(item.created_on),
|
|
94
|
+
updatedAt: new Date(item.last_modified_on)
|
|
95
|
+
}));
|
|
96
|
+
|
|
97
|
+
onSuccess(projectId, threads);
|
|
98
|
+
} else {
|
|
99
|
+
console.error('Failed to fetch project threads:', response.error || (response as any).message);
|
|
100
|
+
}
|
|
101
|
+
} catch (error) {
|
|
102
|
+
console.error('Failed to fetch project threads:', error);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
// Fetch works for a specific project
|
|
107
|
+
export async function fetchProjectWorks(
|
|
108
|
+
apiClient: ApiClient,
|
|
109
|
+
projectId: string,
|
|
110
|
+
onSuccess: (projectId: string, works: Work[]) => void,
|
|
111
|
+
tenantAiAgentId?: string,
|
|
112
|
+
userId?: string | null
|
|
113
|
+
) {
|
|
114
|
+
try {
|
|
115
|
+
const response = (await apiClient.get('/ai/works')) as ApiResponse<any[]>;
|
|
116
|
+
|
|
117
|
+
if (response.success && response.data) {
|
|
118
|
+
const allWorks = Array.isArray(response.data) ? response.data : (response.data as any).items || [];
|
|
119
|
+
const projectWorks = allWorks.filter((work: any) => work.tenant_ai_project_id === projectId
|
|
120
|
+
&& (!tenantAiAgentId || work.tenant_ai_agent_id === tenantAiAgentId)
|
|
121
|
+
&& (!userId || work.created_by === userId || (Array.isArray(work.shared_to) && work.shared_to.includes(userId))));
|
|
122
|
+
|
|
123
|
+
const formattedWorks: Work[] = projectWorks.map((item: Work) => ({
|
|
124
|
+
id: item.id,
|
|
125
|
+
title: item.title,
|
|
126
|
+
base_thread_id: item.base_thread_id,
|
|
127
|
+
content_json: item.content_json,
|
|
128
|
+
content_text: item.content_text,
|
|
129
|
+
description: item.description,
|
|
130
|
+
tenant_ai_agent_deployment_id: item.tenant_ai_agent_deployment_id,
|
|
131
|
+
tenant_ai_agent_id: item.tenant_ai_agent_id,
|
|
132
|
+
tenant_data_source_id: item.tenant_data_source_id,
|
|
133
|
+
tenant_ai_project_id: item.tenant_ai_project_id,
|
|
134
|
+
type: item.type,
|
|
135
|
+
created_by: item.created_by,
|
|
136
|
+
created_on: item.created_on,
|
|
137
|
+
last_modified_on: item.last_modified_on,
|
|
138
|
+
image_url: item.image_url || undefined
|
|
139
|
+
}));
|
|
140
|
+
|
|
141
|
+
onSuccess(projectId, formattedWorks);
|
|
142
|
+
} else {
|
|
143
|
+
console.error('Failed to fetch project works:', response.error || (response as any).message);
|
|
144
|
+
}
|
|
145
|
+
} catch (error) {
|
|
146
|
+
console.error('Failed to fetch project works:', error);
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
// Fetch all projects
|
|
151
|
+
export async function fetchProjects(
|
|
152
|
+
apiClient: ApiClient,
|
|
153
|
+
userId: string | null,
|
|
154
|
+
onSuccess: (projects: Project[]) => void,
|
|
155
|
+
onProjectLoaded: (project: Project) => void,
|
|
156
|
+
onComplete: () => void,
|
|
157
|
+
tenantAiAgentId?: string
|
|
158
|
+
) {
|
|
159
|
+
try {
|
|
160
|
+
const response = (await apiClient.get('/ai/projects')) as ApiResponse<any[]>;
|
|
161
|
+
|
|
162
|
+
if (response.success && response.data) {
|
|
163
|
+
const responseItems = Array.isArray(response.data) ? response.data : (response.data as any).items || [];
|
|
164
|
+
const allProjects: Project[] = responseItems.map((item: any) => ({
|
|
165
|
+
id: item.id,
|
|
166
|
+
tenant_ai_agent_id: item.tenant_ai_agent_id,
|
|
167
|
+
tenant_ai_agent_deployment_id: item.tenant_ai_agent_deployment_id,
|
|
168
|
+
name: item.name,
|
|
169
|
+
description: item.description,
|
|
170
|
+
created_by: item.created_by,
|
|
171
|
+
shared_to: item.shared_to
|
|
172
|
+
}));
|
|
173
|
+
|
|
174
|
+
const filteredProjects = allProjects.filter((project) => {
|
|
175
|
+
const matchesAgent = !tenantAiAgentId || project.tenant_ai_agent_id === tenantAiAgentId;
|
|
176
|
+
const matchesUser = !userId || project.created_by === userId || (project.shared_to && project.shared_to.includes(userId));
|
|
177
|
+
|
|
178
|
+
return matchesAgent && matchesUser;
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
onSuccess(filteredProjects);
|
|
182
|
+
filteredProjects.forEach(onProjectLoaded);
|
|
183
|
+
} else {
|
|
184
|
+
console.error('Failed to fetch projects:', response.error || (response as any).message);
|
|
185
|
+
}
|
|
186
|
+
} catch (error) {
|
|
187
|
+
console.error('Failed to fetch projects:', error);
|
|
188
|
+
} finally {
|
|
189
|
+
onComplete();
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
// Fetch all works
|
|
194
|
+
export async function fetchWorks(
|
|
195
|
+
apiClient: ApiClient,
|
|
196
|
+
onSuccess: (works: Work[]) => void,
|
|
197
|
+
tenantAiAgentId?: string,
|
|
198
|
+
userId?: string | null
|
|
199
|
+
) {
|
|
200
|
+
try {
|
|
201
|
+
const response = (await apiClient.get('/ai/works')) as ApiResponse<any[]>;
|
|
202
|
+
|
|
203
|
+
if (response.success && response.data) {
|
|
204
|
+
const allItems = Array.isArray(response.data) ? response.data : (response.data as any).items || [];
|
|
205
|
+
const worksItems = allItems.filter((item: any) => {
|
|
206
|
+
const matchesAgent = !tenantAiAgentId || item.tenant_ai_agent_id === tenantAiAgentId;
|
|
207
|
+
const matchesUser = !userId || item.created_by === userId || (Array.isArray(item.shared_to) && item.shared_to.includes(userId));
|
|
208
|
+
|
|
209
|
+
return matchesAgent && matchesUser;
|
|
210
|
+
});
|
|
211
|
+
const formattedWorks: Work[] = worksItems.map((item: any) => ({
|
|
212
|
+
id: item.id,
|
|
213
|
+
title: item.title,
|
|
214
|
+
base_thread_id: item.base_thread_id,
|
|
215
|
+
content_json: item.content_json,
|
|
216
|
+
content_text: item.content_text,
|
|
217
|
+
description: item.description,
|
|
218
|
+
tenant_ai_agent_deployment_id: item.tenant_ai_agent_deployment_id,
|
|
219
|
+
tenant_ai_agent_id: item.tenant_ai_agent_id,
|
|
220
|
+
tenant_data_source_id: item.tenant_data_source_id,
|
|
221
|
+
type: item.type,
|
|
222
|
+
created_by: item.created_by,
|
|
223
|
+
created_on: item.created_on,
|
|
224
|
+
last_modified_on: item.last_modified_on,
|
|
225
|
+
image_url: item.image_url || undefined
|
|
226
|
+
}));
|
|
227
|
+
|
|
228
|
+
onSuccess(formattedWorks);
|
|
229
|
+
} else {
|
|
230
|
+
console.error('Failed to fetch works:', response.error || (response as any).message);
|
|
231
|
+
}
|
|
232
|
+
} catch (error) {
|
|
233
|
+
console.error('Failed to fetch works:', error);
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
// Load messages for a thread
|
|
238
|
+
export async function loadThreadMessages(
|
|
239
|
+
apiClient: ApiClient,
|
|
240
|
+
threadId: string,
|
|
241
|
+
setMessages: (messages: any[]) => void
|
|
242
|
+
) {
|
|
243
|
+
try {
|
|
244
|
+
const response = (await apiClient.get('/apps/base/data-sources/message/items', {
|
|
245
|
+
columns: 'body_json,created_on,record_owner,role',
|
|
246
|
+
filters: JSON.stringify({ rules: [{ field: 'thread', operator: '=', value: threadId }] }),
|
|
247
|
+
sort: JSON.stringify([{ field: 'created_on', direction: 'asc' }])
|
|
248
|
+
})) as ApiResponse<any[]>;
|
|
249
|
+
|
|
250
|
+
if (response.success && response.data) {
|
|
251
|
+
const formattedMessages = processMessagesData(response.data);
|
|
252
|
+
|
|
253
|
+
setMessages(formattedMessages as any);
|
|
254
|
+
} else {
|
|
255
|
+
console.info('No messages found for thread');
|
|
256
|
+
setMessages([]);
|
|
257
|
+
}
|
|
258
|
+
} catch (error) {
|
|
259
|
+
console.error('Error loading thread messages:', error);
|
|
260
|
+
setMessages([]);
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
// Update project
|
|
265
|
+
export async function updateProject(
|
|
266
|
+
apiClient: ApiClient,
|
|
267
|
+
projectState: ProjectState,
|
|
268
|
+
projectActions: {
|
|
269
|
+
setSaving: (saving: boolean) => void;
|
|
270
|
+
stopEditing: () => void;
|
|
271
|
+
},
|
|
272
|
+
refreshProjects: () => void,
|
|
273
|
+
t: TranslationFn
|
|
274
|
+
) {
|
|
275
|
+
if (!projectState.name.trim() || !projectState.selectedProject) return;
|
|
276
|
+
|
|
277
|
+
projectActions.setSaving(true);
|
|
278
|
+
try {
|
|
279
|
+
const response: ApiResponse = await apiClient.patch(`/ai/projects/${projectState.selectedProject.id}`, {
|
|
280
|
+
name: projectState.name.trim(),
|
|
281
|
+
description: projectState.description.trim()
|
|
282
|
+
});
|
|
283
|
+
|
|
284
|
+
if (response.success) {
|
|
285
|
+
projectActions.stopEditing();
|
|
286
|
+
refreshProjects();
|
|
287
|
+
toast.success(t('toast.project_updated_success'));
|
|
288
|
+
} else {
|
|
289
|
+
throw new Error('Failed to update project');
|
|
290
|
+
}
|
|
291
|
+
} catch (error) {
|
|
292
|
+
console.error('Error updating project:', error);
|
|
293
|
+
toast.error(t('toast.failed_update_project'));
|
|
294
|
+
} finally {
|
|
295
|
+
projectActions.setSaving(false);
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
// Delete project
|
|
300
|
+
export async function deleteProject(
|
|
301
|
+
apiClient: ApiClient,
|
|
302
|
+
projectState: ProjectState,
|
|
303
|
+
projectActions: {
|
|
304
|
+
setDeleteDialogOpen: (open: boolean) => void;
|
|
305
|
+
setDeleting: (deleting: boolean) => void;
|
|
306
|
+
},
|
|
307
|
+
refreshProjects: () => void,
|
|
308
|
+
t: TranslationFn
|
|
309
|
+
) {
|
|
310
|
+
if (!projectState.selectedProject) return;
|
|
311
|
+
|
|
312
|
+
projectActions.setDeleting(true);
|
|
313
|
+
try {
|
|
314
|
+
const response: ApiResponse = await apiClient.delete(`/ai/projects/${projectState.selectedProject.id}`);
|
|
315
|
+
|
|
316
|
+
if (response.success) {
|
|
317
|
+
projectActions.setDeleteDialogOpen(false);
|
|
318
|
+
projectActions.setDeleting(false);
|
|
319
|
+
refreshProjects();
|
|
320
|
+
toast.success(t('toast.project_deleted_success'));
|
|
321
|
+
} else {
|
|
322
|
+
toast.error(t('toast.failed_delete_project'));
|
|
323
|
+
throw new Error('Failed to delete project');
|
|
324
|
+
}
|
|
325
|
+
} catch (error) {
|
|
326
|
+
console.error('Error deleting project:', error);
|
|
327
|
+
toast.error(t('toast.failed_delete_project'));
|
|
328
|
+
} finally {
|
|
329
|
+
projectActions.setDeleting(false);
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
// Update session
|
|
334
|
+
export async function updateSession(
|
|
335
|
+
apiClient: ApiClient,
|
|
336
|
+
sessionState: SessionState,
|
|
337
|
+
sessionActions: {
|
|
338
|
+
setSaving: (saving: boolean) => void;
|
|
339
|
+
stopEditing: () => void;
|
|
340
|
+
},
|
|
341
|
+
refreshThreads: () => void,
|
|
342
|
+
refreshProjectThreads: (projectId: string) => void,
|
|
343
|
+
projectContextId: string | null,
|
|
344
|
+
selectedProjectId: string | null,
|
|
345
|
+
t: TranslationFn
|
|
346
|
+
) {
|
|
347
|
+
if (!sessionState.name.trim() || !sessionState.selectedSession) return;
|
|
348
|
+
|
|
349
|
+
sessionActions.setSaving(true);
|
|
350
|
+
try {
|
|
351
|
+
const response = (await apiClient.patch(
|
|
352
|
+
`/apps/base/data-sources/thread/items/${sessionState.selectedSession.id}`,
|
|
353
|
+
{ subject: sessionState.name.trim() }
|
|
354
|
+
)) as ApiResponse<any>;
|
|
355
|
+
|
|
356
|
+
if (response.success) {
|
|
357
|
+
sessionActions.stopEditing();
|
|
358
|
+
refreshThreads();
|
|
359
|
+
if (projectContextId) refreshProjectThreads(projectContextId);
|
|
360
|
+
if (selectedProjectId) refreshProjectThreads(selectedProjectId);
|
|
361
|
+
toast.success(t('toast.session_renamed_success'));
|
|
362
|
+
} else {
|
|
363
|
+
toast.error(t('toast.failed_rename_session'));
|
|
364
|
+
}
|
|
365
|
+
} catch (error) {
|
|
366
|
+
console.error('Error updating session:', error);
|
|
367
|
+
toast.error(t('toast.failed_rename_session'));
|
|
368
|
+
} finally {
|
|
369
|
+
sessionActions.setSaving(false);
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
// Delete session
|
|
374
|
+
export async function deleteSession(
|
|
375
|
+
apiClient: ApiClient,
|
|
376
|
+
sessionState: SessionState,
|
|
377
|
+
sessionActions: {
|
|
378
|
+
setDeleteDialogOpen: (open: boolean) => void;
|
|
379
|
+
setDeleting: (deleting: boolean) => void;
|
|
380
|
+
},
|
|
381
|
+
refreshThreads: () => void,
|
|
382
|
+
refreshProjectThreads: (projectId: string) => void,
|
|
383
|
+
projectContextId: string | null,
|
|
384
|
+
selectedProjectId: string | null,
|
|
385
|
+
t: TranslationFn
|
|
386
|
+
) {
|
|
387
|
+
if (!sessionState.selectedSession) return;
|
|
388
|
+
|
|
389
|
+
sessionActions.setDeleting(true);
|
|
390
|
+
try {
|
|
391
|
+
const response = (await apiClient.delete(
|
|
392
|
+
`/apps/base/data-sources/thread/items/${sessionState.selectedSession.id}`
|
|
393
|
+
)) as ApiResponse<any>;
|
|
394
|
+
|
|
395
|
+
if (response.success) {
|
|
396
|
+
sessionActions.setDeleteDialogOpen(false);
|
|
397
|
+
refreshThreads();
|
|
398
|
+
if (projectContextId) refreshProjectThreads(projectContextId);
|
|
399
|
+
if (selectedProjectId) refreshProjectThreads(selectedProjectId);
|
|
400
|
+
toast.success(t('toast.session_deleted_success'));
|
|
401
|
+
} else {
|
|
402
|
+
toast.error(t('toast.failed_delete_session'));
|
|
403
|
+
}
|
|
404
|
+
} catch (error) {
|
|
405
|
+
console.error('Error deleting session:', error);
|
|
406
|
+
toast.error(t('toast.failed_delete_session'));
|
|
407
|
+
} finally {
|
|
408
|
+
sessionActions.setDeleting(false);
|
|
409
|
+
}
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
// Update project instructions
|
|
413
|
+
export async function updateProjectInstructions(
|
|
414
|
+
apiClient: ApiClient,
|
|
415
|
+
projectState: ProjectState,
|
|
416
|
+
projectActions: {
|
|
417
|
+
setSavingInstructions: (saving: boolean) => void;
|
|
418
|
+
stopEditingInstructions: () => void;
|
|
419
|
+
updateProject: (project: Project) => void;
|
|
420
|
+
},
|
|
421
|
+
refreshProjects: () => void,
|
|
422
|
+
t: TranslationFn
|
|
423
|
+
) {
|
|
424
|
+
if (!projectState.selectedProject) return;
|
|
425
|
+
|
|
426
|
+
projectActions.setSavingInstructions(true);
|
|
427
|
+
try {
|
|
428
|
+
const response = (await apiClient.patch(`/ai/projects/${projectState.selectedProject.id}`, {
|
|
429
|
+
instructions: projectState.instructions.trim() || null
|
|
430
|
+
})) as ApiResponse<any>;
|
|
431
|
+
|
|
432
|
+
if (response.success) {
|
|
433
|
+
projectActions.stopEditingInstructions();
|
|
434
|
+
projectActions.updateProject({
|
|
435
|
+
...projectState.selectedProject,
|
|
436
|
+
instructions: projectState.instructions.trim() || undefined
|
|
437
|
+
});
|
|
438
|
+
refreshProjects();
|
|
439
|
+
toast.success(t('toast.instructions_updated_success'));
|
|
440
|
+
} else {
|
|
441
|
+
toast.error(t('toast.failed_update_instructions'));
|
|
442
|
+
}
|
|
443
|
+
} catch (error) {
|
|
444
|
+
console.error('Error updating instructions:', error);
|
|
445
|
+
toast.error(t('toast.failed_update_instructions'));
|
|
446
|
+
} finally {
|
|
447
|
+
projectActions.setSavingInstructions(false);
|
|
448
|
+
}
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
// Create new thread
|
|
452
|
+
export async function createThread(
|
|
453
|
+
apiClient: ApiClient,
|
|
454
|
+
data: {
|
|
455
|
+
subject: string;
|
|
456
|
+
body_text: string;
|
|
457
|
+
sender_name: string;
|
|
458
|
+
deploymentId?: string;
|
|
459
|
+
tenantAiAgentId: string;
|
|
460
|
+
projectId?: string | null;
|
|
461
|
+
}
|
|
462
|
+
): Promise<{
|
|
463
|
+
id: string; subject: string; created_on: string; last_modified_on: string;
|
|
464
|
+
} | null> {
|
|
465
|
+
try {
|
|
466
|
+
const response = (await apiClient.post('/apps/base/data-sources/thread/items', {
|
|
467
|
+
subject: data.subject,
|
|
468
|
+
body_text: data.body_text,
|
|
469
|
+
sender_name: data.sender_name,
|
|
470
|
+
thread_type: 'assistant',
|
|
471
|
+
tenant_ai_agent_deployment_id: data.deploymentId ? String(data.deploymentId) : null,
|
|
472
|
+
tenant_ai_agent_id: String(data.tenantAiAgentId),
|
|
473
|
+
tenant_ai_project_id: data.projectId || null
|
|
474
|
+
})) as ApiResponse<any>;
|
|
475
|
+
|
|
476
|
+
if (response && response.success && response.data) {
|
|
477
|
+
return response.data;
|
|
478
|
+
} else {
|
|
479
|
+
console.error('Failed to create thread:', response?.error || 'Unknown error');
|
|
480
|
+
|
|
481
|
+
return null;
|
|
482
|
+
}
|
|
483
|
+
} catch (error) {
|
|
484
|
+
console.error('Failed to create thread:', error);
|
|
485
|
+
|
|
486
|
+
return null;
|
|
487
|
+
}
|
|
488
|
+
}
|
|
489
|
+
|
|
490
|
+
// Upload file to thread via REST API
|
|
491
|
+
export async function uploadThreadFile(
|
|
492
|
+
apiClient: ApiClient,
|
|
493
|
+
threadId: string,
|
|
494
|
+
file: File
|
|
495
|
+
): Promise<string | null> {
|
|
496
|
+
try {
|
|
497
|
+
const formData = new FormData();
|
|
498
|
+
|
|
499
|
+
formData.append('file', file);
|
|
500
|
+
|
|
501
|
+
const response = (await apiClient.post(
|
|
502
|
+
`/apps/base/data-sources/thread/items/${threadId}/files/upload`,
|
|
503
|
+
formData
|
|
504
|
+
)) as ApiResponse<any>;
|
|
505
|
+
|
|
506
|
+
if (response.success && response.data) {
|
|
507
|
+
// Return the storage path (file_name field)
|
|
508
|
+
const fileData = Array.isArray(response.data) ? response.data[0] : response.data;
|
|
509
|
+
|
|
510
|
+
return fileData?.file_name || fileData?.path || null;
|
|
511
|
+
}
|
|
512
|
+
|
|
513
|
+
console.error('Failed to upload file:', response.error);
|
|
514
|
+
|
|
515
|
+
return null;
|
|
516
|
+
} catch (error) {
|
|
517
|
+
console.error('Failed to upload file:', error);
|
|
518
|
+
|
|
519
|
+
return null;
|
|
520
|
+
}
|
|
521
|
+
}
|
|
522
|
+
|
|
523
|
+
// Create new project
|
|
524
|
+
export async function createProject(
|
|
525
|
+
apiClient: ApiClient,
|
|
526
|
+
data: {
|
|
527
|
+
name: string;
|
|
528
|
+
description?: string;
|
|
529
|
+
tenantAiAgentId: string;
|
|
530
|
+
},
|
|
531
|
+
onSuccess: (project: any) => void,
|
|
532
|
+
onComplete: () => void
|
|
533
|
+
) {
|
|
534
|
+
try {
|
|
535
|
+
const response: any = await apiClient.post('/ai/projects', {
|
|
536
|
+
name: data.name,
|
|
537
|
+
tenant_ai_agent_id: data.tenantAiAgentId,
|
|
538
|
+
description: data.description || undefined
|
|
539
|
+
});
|
|
540
|
+
|
|
541
|
+
if (response.success && response.data) {
|
|
542
|
+
onSuccess(response.data);
|
|
543
|
+
}
|
|
544
|
+
} catch (error) {
|
|
545
|
+
console.error('Failed to create project:', error);
|
|
546
|
+
} finally {
|
|
547
|
+
onComplete();
|
|
548
|
+
}
|
|
549
|
+
}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
createContext, type ReactNode, use, useMemo
|
|
5
|
+
} from 'react';
|
|
6
|
+
|
|
7
|
+
import { Toaster } from 'sonner';
|
|
8
|
+
|
|
9
|
+
import { AssistantI18nProvider } from '../i18n';
|
|
10
|
+
|
|
11
|
+
// User interface for assistant
|
|
12
|
+
export interface AssistantUser {
|
|
13
|
+
id: string;
|
|
14
|
+
email?: string;
|
|
15
|
+
firstname?: string;
|
|
16
|
+
lastname?: string;
|
|
17
|
+
photo?: string;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export interface DataSourceField {
|
|
21
|
+
id: string;
|
|
22
|
+
name: string;
|
|
23
|
+
slug: string;
|
|
24
|
+
type: string;
|
|
25
|
+
read_only?: boolean;
|
|
26
|
+
sort_order?: number;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export interface DataSourceSchema {
|
|
30
|
+
id: string;
|
|
31
|
+
name: string;
|
|
32
|
+
slug: string;
|
|
33
|
+
app: { id: string; name: string; slug: string };
|
|
34
|
+
fields: DataSourceField[];
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export interface AssistantConfig {
|
|
38
|
+
apiBaseUrl: string;
|
|
39
|
+
getAuthToken: () => Promise<string>;
|
|
40
|
+
user?: AssistantUser | null;
|
|
41
|
+
getDataSourceSchema?: (id: string) => Promise<DataSourceSchema | null>;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
const AssistantConfigContext = createContext<AssistantConfig | null>(null);
|
|
45
|
+
|
|
46
|
+
export interface AssistantProviderProps {
|
|
47
|
+
config: AssistantConfig;
|
|
48
|
+
children: ReactNode;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export function AssistantProvider({ config, children }: AssistantProviderProps) {
|
|
52
|
+
// Stabilize context value so consumers only re-render when config actually changes
|
|
53
|
+
const value = useMemo(() => config, [config]);
|
|
54
|
+
|
|
55
|
+
return (
|
|
56
|
+
<AssistantConfigContext value={value}>
|
|
57
|
+
<AssistantI18nProvider>{children}</AssistantI18nProvider>
|
|
58
|
+
<Toaster />
|
|
59
|
+
</AssistantConfigContext>
|
|
60
|
+
);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export function useAssistantConfig(): AssistantConfig {
|
|
64
|
+
const config = use(AssistantConfigContext);
|
|
65
|
+
|
|
66
|
+
if (!config) {
|
|
67
|
+
throw new Error('useAssistantConfig must be used within an AssistantProvider');
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
return config;
|
|
71
|
+
}
|