useputer
Version:
A **Zustand store wrapper** for [Puter.js](https://puter.com) that provides **state management and hooks** for authentication, file system operations, AI tools, and key-value storage — all inside your React applications.
138 lines (135 loc) • 4.43 kB
TypeScript
import * as zustand from 'zustand';
interface FSItem {
id: string;
uid: string;
name: string;
path: string;
is_dir: boolean;
parent_id: string;
parent_uid: string;
created: number;
modified: number;
accessed: number;
size: number | null;
writable: boolean;
}
interface PuterUser {
uuid: string;
username: string;
}
interface KVItem {
key: string;
value: string;
}
interface ChatMessageContent {
type: "file" | "text";
puter_path?: string;
text?: string;
}
interface ChatMessage {
role: "user" | "assistant" | "system";
content: string | ChatMessageContent[];
}
interface PuterChatOptions {
model?: string;
stream?: boolean;
max_tokens?: number;
temperature?: number;
tools?: {
type: "function";
function: {
name: string;
description: string;
parameters: {
type: string;
properties: any;
};
}[];
};
}
interface AIResponse {
index: number;
message: {
role: string;
content: string | any[];
refusal: null | string;
annotations: any[];
};
logprobs: null | any;
finish_reason: string;
usage: {
type: string;
model: string;
amount: number;
cost: number;
}[];
via_ai_chat_service: boolean;
}
declare global {
interface Window {
puter: {
auth: {
getUser: () => Promise<PuterUser>;
isSignedIn: () => Promise<boolean>;
signIn: () => Promise<void>;
signOut: () => Promise<void>;
};
fs: {
write: (path: string, data: string | File | Blob) => Promise<File | undefined>;
read: (path: string) => Promise<Blob>;
upload: (file: File[] | Blob[]) => Promise<FSItem>;
delete: (path: string) => Promise<void>;
readdir: (path: string) => Promise<FSItem[] | undefined>;
};
ai: {
chat: (prompt: string | ChatMessage[], imageURL?: string | PuterChatOptions, testMode?: boolean, options?: PuterChatOptions) => Promise<object>;
img2txt: (image: string | File | Blob, testMode?: boolean) => Promise<string>;
};
kv: {
get: (key: string) => Promise<string | null>;
set: (key: string, value: string) => Promise<boolean>;
delete: (key: string) => Promise<boolean>;
list: (pattern: string, returnValues?: boolean) => Promise<string[]>;
flush: () => Promise<boolean>;
};
};
}
}
interface PuterStore {
isLoading: boolean;
error: string | null;
puterReady: boolean;
auth: {
user: PuterUser | null;
isAuthenticated: boolean;
signIn: () => Promise<void>;
signOut: () => Promise<void>;
refreshUser: () => Promise<void>;
checkAuthStatus: () => Promise<boolean>;
getUser: () => PuterUser | null;
};
fs: {
write: (path: string, data: string | File | Blob) => Promise<File | undefined>;
read: (path: string) => Promise<Blob | undefined>;
upload: (file: File[] | Blob[]) => Promise<FSItem | undefined>;
delete: (path: string) => Promise<void>;
readDir: (path: string) => Promise<FSItem[] | undefined>;
};
ai: {
chat: (prompt: string | ChatMessage[], imageURL?: string | PuterChatOptions, testMode?: boolean, options?: PuterChatOptions) => Promise<AIResponse | undefined>;
feedback: (path: string, message: string) => Promise<AIResponse | undefined>;
img2txt: (image: string | File | Blob, testMode?: boolean) => Promise<string | undefined>;
};
kv: {
get: (key: string) => Promise<string | null | undefined>;
set: (key: string, value: string) => Promise<boolean | undefined>;
delete: (key: string) => Promise<boolean | undefined>;
list: (pattern: string, returnValues?: boolean) => Promise<string[] | KVItem[] | undefined>;
flush: () => Promise<boolean | undefined>;
};
init: () => void;
clearError: () => void;
}
declare const usePuterStore: zustand.UseBoundStore<zustand.StoreApi<PuterStore>>;
declare function ClientInit(): null;
export { ClientInit as default, usePuterStore };