UNPKG

functions.do

Version:

A framework and platform for easily creating, managing, evaluating, and iterating on AI functions & object generation

87 lines 2.92 kB
// Helper to generate the API request payload const generateRequest = (functionName, schema, input, config) => { return { functionName, schema, input, config }; }; // Helper to call the functions.do API const callAPI = async (request) => { const url = process.env.FUNCTIONS_API_URL || 'https://functions.do/api/generate'; console.log({ url }); const response = await fetch(url, { method: 'POST', headers: { 'Content-Type': 'application/json', Authorization: `users API-Key ${process.env.FUNCTIONS_DO_API_KEY}`, }, body: JSON.stringify(request) }); if (!response.ok) { console.log(response.status, response.statusText); throw new Error(`API call failed: ${response.statusText}`); } const data = await response.json(); console.log(data); return data; }; // Helper to generate a function from schema and config const createFunction = (name, schema, config) => { return async (input, functionConfig) => { const mergedConfig = { ...config, ...functionConfig }; const request = generateRequest(name, schema, input, mergedConfig); try { const response = await callAPI(request); return response.data ?? response; } catch (error) { console.error('Error calling AI function:', error); throw error; } }; }; // AI factory function for creating strongly-typed functions export const AI = (functions, config) => { const result = {}; // Create the ai instance first so it can be passed to callbacks const aiInstance = new Proxy({}, { get: (target, prop) => { if (typeof prop === 'string' && !prop.startsWith('_')) { return createFunction(prop, {}, {}); } return target[prop]; }, }); for (const [name, value] of Object.entries(functions)) { if (typeof value === 'function') { // Handle function callback result[name] = value; // Immediately invoke the callback if it's a startup function if (name === 'launchStartup') { try { value({ ai: aiInstance, args: {} }); } catch (error) { console.error('Error in launchStartup callback:', error); } } } else { // Handle schema-based function result[name] = createFunction(name, value, config); } } return result; }; // Dynamic ai instance that accepts any function name export const ai = new Proxy({}, { get: (target, prop) => { if (typeof prop === 'string' && !prop.startsWith('_')) { return createFunction(prop, {}, {}); } return target[prop]; }, }); //# sourceMappingURL=index.js.map