|
|
@@ -1,3 +1,5 @@
|
|
|
+import { useCallback } from 'react';
|
|
|
+
|
|
|
import {
|
|
|
SseMessageSchema,
|
|
|
SseDetectedDiffSchema,
|
|
|
@@ -8,33 +10,46 @@ import {
|
|
|
} from '~/features/openai/interfaces/editor-assistant/sse-schemas';
|
|
|
import { handleIfSuccessfullyParsed } from '~/features/openai/utils/handle-if-successfully-parsed';
|
|
|
|
|
|
-export const postMessage = async(threadId: string, userMessage: string, markdown: string, aiAssistantId?: string): Promise<Response> => {
|
|
|
- const response = await fetch('/_api/v3/openai/edit', {
|
|
|
- method: 'POST',
|
|
|
- headers: { 'Content-Type': 'application/json' },
|
|
|
- body: JSON.stringify({
|
|
|
- aiAssistantId,
|
|
|
- threadId,
|
|
|
- userMessage,
|
|
|
- markdown,
|
|
|
- }),
|
|
|
- });
|
|
|
+interface PostMessage {
|
|
|
+ (threadId: string, userMessage: string, markdown: string, aiAssistantId?: string): Promise<Response>;
|
|
|
+}
|
|
|
+interface ProcessMessage {
|
|
|
+ (data: unknown, handler: {
|
|
|
+ onMessage: (data: SseMessage) => void;
|
|
|
+ onDetectedDiff: (data: SseDetectedDiff) => void;
|
|
|
+ onFinalized: (data: SseFinalized) => void;
|
|
|
+ }): void;
|
|
|
+}
|
|
|
|
|
|
- return response;
|
|
|
-};
|
|
|
+export const useEditorAssistant = (): { postMessage: PostMessage, processMessage: ProcessMessage } => {
|
|
|
+ const postMessage: PostMessage = useCallback(async(threadId, userMessage, markdown, aiAssistantId) => {
|
|
|
+ const response = await fetch('/_api/v3/openai/edit', {
|
|
|
+ method: 'POST',
|
|
|
+ headers: { 'Content-Type': 'application/json' },
|
|
|
+ body: JSON.stringify({
|
|
|
+ aiAssistantId,
|
|
|
+ threadId,
|
|
|
+ userMessage,
|
|
|
+ markdown,
|
|
|
+ }),
|
|
|
+ });
|
|
|
+ return response;
|
|
|
+ }, []);
|
|
|
+
|
|
|
+ const processMessage: ProcessMessage = useCallback((data, handler) => {
|
|
|
+ handleIfSuccessfullyParsed(data, SseMessageSchema, (data: SseMessage) => {
|
|
|
+ handler.onMessage(data);
|
|
|
+ });
|
|
|
+ handleIfSuccessfullyParsed(data, SseDetectedDiffSchema, (data: SseDetectedDiff) => {
|
|
|
+ handler.onDetectedDiff(data);
|
|
|
+ });
|
|
|
+ handleIfSuccessfullyParsed(data, SseFinalizedSchema, (data: SseFinalized) => {
|
|
|
+ handler.onFinalized(data);
|
|
|
+ });
|
|
|
+ }, []);
|
|
|
|
|
|
-export const processMessage = (data: unknown, handler: {
|
|
|
- onMessage: (data: SseMessage) => void,
|
|
|
- onDetectedDiff: (data: SseDetectedDiff) => void,
|
|
|
- onFinalized: (data: SseFinalized) => void,
|
|
|
-}): void => {
|
|
|
- handleIfSuccessfullyParsed(data, SseMessageSchema, (data: SseMessage) => {
|
|
|
- handler.onMessage(data);
|
|
|
- });
|
|
|
- handleIfSuccessfullyParsed(data, SseDetectedDiffSchema, (data: SseDetectedDiff) => {
|
|
|
- handler.onDetectedDiff(data);
|
|
|
- });
|
|
|
- handleIfSuccessfullyParsed(data, SseFinalizedSchema, (data: SseFinalized) => {
|
|
|
- handler.onFinalized(data);
|
|
|
- });
|
|
|
+ return {
|
|
|
+ postMessage,
|
|
|
+ processMessage,
|
|
|
+ };
|
|
|
};
|