UNPKG

puterjs

Version:

A wrapper around puterjs

68 lines 2.62 kB
/** * Comprehensive TypeScript type definitions for Puter.js * Based on Puter.js v2 API documentation * * Note: This package requires DOM types. Either: * 1. Add "DOM" to your tsconfig.json lib array: "lib": ["ES2020", "DOM", "DOM.Iterable"] * 2. Or install @types/node for server-side usage with appropriate DOM polyfills */ // UTILITY FUNCTIONS /** * Checks if the Puter.js script is loaded in the current document * @returns true if the script is found, false otherwise */ export function isPuterScriptLoaded() { if (typeof document === "undefined") { console.warn("isPuterScriptLoaded() can only be used in browser environments"); return false; } const scripts = document.querySelectorAll("script[src]"); return Array.from(scripts).some((script) => { const src = script.getAttribute("src"); if (!src) return false; // Check if src starts with "https://js.puter.com/v" followed by any version and optional "index.js" const regex = /^https:\/\/js\.puter\.com\/v\d+\/?(index\.js)?$/; return regex.test(src); }); } /** * Gets the puter object from the global scope with proper typing * @returns The puter object if available, null otherwise */ export function getPuter() { if (typeof window === "undefined") { throw new Error("getPuter() can only be used in browser environments"); } if (!isPuterScriptLoaded()) { throw new Error('Puter.js script not found. Make sure to include: <script src="https://js.puter.com/v{latest_version}/"></script>'); } if ("puter" in window) { return window.puter; } throw new Error("No puter object in window, puterjs can only be called in client side"); } /** * Utility function to safely access puter with error handling * @param callback Function to execute with the puter object * @returns Promise that resolves with the callback result or rejects if puter is not available */ export async function withPuter(callback) { const puter = getPuter(); if (!puter) { throw new Error("Puter.js is not available. Make sure the script is loaded."); } return callback(puter); } /** * Type guard to check if a chat response contains tool calls * @param response The chat response to check * @returns true if response has tool calls */ export function hasToolCalls(response) { return (typeof response !== "string" && response.message?.tool_calls !== undefined && Array.isArray(response.message.tool_calls) && response.message.tool_calls.length > 0); } //# sourceMappingURL=index.js.map