diff --git a/frontend/src/hooks/api/actions/useAIApi/useAIApi.ts b/frontend/src/hooks/api/actions/useAIApi/useAIApi.ts new file mode 100644 index 000000000000..0d0d5fc3f198 --- /dev/null +++ b/frontend/src/hooks/api/actions/useAIApi/useAIApi.ts @@ -0,0 +1,36 @@ +import useAPI from '../useApi/useApi'; + +const ENDPOINT = 'api/admin/ai'; + +export type ChatMessage = { + role: 'system' | 'user' | 'assistant'; + content: string; +}; + +export const useAIApi = () => { + const { makeRequest, createRequest, errors, loading } = useAPI({ + propagateErrors: true, + }); + + const chat = async (messages: ChatMessage[]): Promise => { + const requestId = 'chat'; + + const req = createRequest(`${ENDPOINT}/chat`, { + method: 'POST', + body: JSON.stringify({ + messages, + }), + requestId, + }); + + const response = await makeRequest(req.caller, req.id); + const { messages: newMessages } = await response.json(); + return newMessages; + }; + + return { + chat, + errors, + loading, + }; +}; diff --git a/package.json b/package.json index eae82aa62d1d..c86836658ee8 100644 --- a/package.json +++ b/package.json @@ -41,6 +41,7 @@ "build": "yarn run clean && concurrently \"yarn:copy-templates\" \"yarn:build:frontend\" \"yarn:build:backend\"", "dev:backend": "TZ=UTC NODE_ENV=development tsc-watch --strictNullChecks false --onSuccess \"node dist/server-dev.js\"", "dev:frontend": "wait-on tcp:4242 && yarn --cwd ./frontend run dev", + "dev:frontend:cloud": "UNLEASH_BASE_PATH=/demo/ yarn run dev:frontend", "dev": "concurrently \"yarn:dev:backend\" \"yarn:dev:frontend\"", "prepare:backend": "concurrently \"yarn:copy-templates\" \"yarn:build:backend\"", "start:dev": "yarn run clean && TZ=UTC NODE_ENV=development tsc-watch --strictNullChecks false --onSuccess \"node dist/server-dev.js\"",