| Server IP : 121.121.20.254 / Your IP : 216.73.217.64 Web Server : Microsoft-IIS/10.0 System : Windows NT WEB-SERVER 10.0 build 20348 (Windows Server 2022) AMD64 User : IUSR ( 0) PHP Version : 8.3.28 Disable Function : NONE MySQL : ON | cURL : ON | WGET : OFF | Perl : OFF | Python : OFF | Sudo : OFF | Pkexec : OFF Directory : C:/inetpub/wwwroot/AIWEBSTATION/wp-content/plugins/code-snippets-pro/js/hooks/ |
Upload File : |
import { useMemo } from 'react'
import { REST_CLOUD_BASE } from '../utils/restAPI'
import { useRestAPI } from './useRestAPI'
import type { Snippet, SnippetType } from '../types/Snippet'
export type ExplainSnippetFields = keyof Pick<Snippet, 'code' | 'desc' | 'tags'>
export interface GeneratedSnippet {
name?: string
code?: string
desc?: string
}
export interface ExplainedSnippet {
name?: string
lines?: Record<number, string>[]
desc?: string
tags?: string[]
}
interface ApiResponse<T> {
success: boolean
message: T
}
export interface GenerativeAPI {
generateSnippet: (prompt: string, type: SnippetType) => Promise<GeneratedSnippet>
explainSnippet: (code: string, field: ExplainSnippetFields) => Promise<ExplainedSnippet>
}
export const useGenerativeAPI = (): GenerativeAPI => {
const { api: { post } } = useRestAPI()
return useMemo((): GenerativeAPI => ({
generateSnippet: (prompt, type) =>
post<ApiResponse<GeneratedSnippet>>(`${REST_CLOUD_BASE}/ai/prompt`, { prompt, type })
.then(response => response.message),
explainSnippet: (code, field) =>
post<ApiResponse<ExplainedSnippet>>(`${REST_CLOUD_BASE}/ai/explain`, { code, field })
.then(response => response.message)
}), [post])
}