neura-sdk
Version:
TypeScript SDK for interacting with the Neura AI API
31 lines (30 loc) • 1.15 kB
JavaScript
import { Fragment as _Fragment, jsx as _jsx } from "react/jsx-runtime";
import { useState, useEffect, useCallback } from 'react';
import { useNeura } from './NeuraProvider';
export const NeuraCompletion = ({ options, onSuccess, onError, children, }) => {
const { sdk } = useNeura();
const [response, setResponse] = useState(null);
const [loading, setLoading] = useState(true);
const [error, setError] = useState(null);
const fetchCompletion = useCallback(async () => {
setLoading(true);
setError(null);
try {
const result = await sdk.createCompletion(options);
setResponse(result);
onSuccess?.(result);
}
catch (err) {
const error = err instanceof Error ? err : new Error(String(err));
setError(error);
onError?.(error);
}
finally {
setLoading(false);
}
}, [sdk, options, onSuccess, onError]);
useEffect(() => {
fetchCompletion();
}, [fetchCompletion]);
return _jsx(_Fragment, { children: children({ response, loading, error, refetch: fetchCompletion }) });
};