Never stop coding. Free MIT AI gateway: one endpoint, 375 providers (150+ free), 1,400+ models Kimi, Claude, GPT, Gemini, GLM, DeepSeek, MiniMax. Works with Claude Code, Codex, Cursor, OpenCode, Cline & Copilot. Quota-aware auto-fallback, RTK+Caveman compression saves 15-95% tokens, MCP/A2A, Desktop/PWA. Built by 900+ contributors
TypeScript a2aai-agentsai-gatewayanthropicclaudeclaude-codeclinecodexcopilotcursordeepseekfree-aigeminikimillm-gatewaymcpopenaiopenai-proxyqwentoken-saver
feat(playground): add usePresets/useImprovePrompt hooks
diegosouzapwa180725f5638
diff --git a/src/app/(dashboard)/dashboard/playground/hooks/useImprovePrompt.ts b/src/app/(dashboard)/dashboard/playground/hooks/useImprovePrompt.tsnew file mode 100644index 0000000000..07564d239b--- /dev/null+++ b/src/app/(dashboard)/dashboard/playground/hooks/useImprovePrompt.ts@@ -0,0 +1,69 @@+// src/app/(dashboard)/dashboard/playground/hooks/useImprovePrompt.ts+"use client";++import { useState, useCallback } from "react";+import type { ImprovePromptResult } from "@/lib/playground/promptImprover";++export interface UseImprovePromptState {+ loading: boolean;+ error: string | null;+}++export interface UseImprovePrompt extends UseImprovePromptState {+ improve: (req: {+ system?: string;+ prompt?: string;+ model: string;+ tone?: "concise" | "detailed";+ }) => Promise<ImprovePromptResult | null>;+}++/**+ * Hook for calling /api/playground/improve-prompt.+ *+ * Manages loading / error state; returns the parsed result on success.+ * Warning: each call consumes model quota (D8 — uses the model selected by user).+ */+export function useImprovePrompt(): UseImprovePrompt {+ const [loading, setLoading] = useState(false);+ const [error, setError] = useState<string | null>(null);++ const improve = useCallback(+ async (req: {+ system?: string;+ prompt?: string;+ model: string;+ tone?: "concise" | "detailed";+ }): Promise<ImprovePromptResult | null> => {+ setLoading(true);+ setError(null);+ try {+ const res = await fetch("/api/playground/improve-prompt", {+ method: "POST",+ headers: { "Content-Type": "application/json" },+ body: JSON.stringify(req),+ });+ if (!res.ok) {+ const body = (await res.json().catch(() => ({}))) as {+ error?: { message?: string };+ };+ throw new Error(body.error?.message ?? `HTTP ${res.status}`);+ }+ const result = (await res.json()) as ImprovePromptResult;+ return result;+ } catch (err) {+ setError(err instanceof Error ? err.message : String(err));+ return null;+ } finally {+ setLoading(false);+ }+ },+ [],+ );++ return {+ loading,+ error,+ improve,+ };+}diff --git a/src/app/(dashboard)/dashboard/playground/hooks/usePresets.ts b/src/app/(dashboard)/dashboard/playground/hooks/usePresets.tsnew file mode 100644index 0000000000..b1f57c8ed6--- /dev/null+++ b/src/app/(dashboard)/dashboard/playground/hooks/usePresets.ts@@ -0,0 +1,151 @@+// src/app/(dashboard)/dashboard/playground/hooks/usePresets.ts+"use client";++import { useState, useCallback } from "react";+import type {+ PlaygroundPresetListItem,+ PlaygroundPresetCreateSchema,+ PlaygroundPresetUpdateSchema,+} from "@/shared/schemas/playground";+import type { z } from "zod";++type CreateInput = z.infer<typeof PlaygroundPresetCreateSchema>;+type UpdateInput = z.infer<typeof PlaygroundPresetUpdateSchema>;++export interface UsePresetsState {+ presets: PlaygroundPresetListItem[];+ loading: boolean;+ error: string | null;+}++export interface UsePresets extends UsePresetsState {+ list: () => Promise<void>;+ create: (input: CreateInput) => Promise<PlaygroundPresetListItem | null>;+ update: (id: string, patch: UpdateInput) => Promise<PlaygroundPresetListItem | null>;+ remove: (id: string) => Promise<void>;+}++/**+ * Hook for managing playground presets via REST API.+ * Wraps fetch calls to /api/playground/presets with local state cache.+ */+export function usePresets(): UsePresets {+ const [presets, setPresets] = useState<PlaygroundPresetListItem[]>([]);+ const [loading, setLoading] = useState(false);+ const [error, setError] = useState<string | null>(null);++ const list = useCallback(async (): Promise<void> => {+ setLoading(true);+ setError(null);+ try {+ const res = await fetch("/api/playground/presets");+ if (!res.ok) {+ const body = (await res.json().catch(() => ({}))) as {+ error?: { message?: string };+ };+ throw new Error(body.error?.message ?? `HTTP ${res.status}`);+ }+ const body = (await res.json()) as { presets: PlaygroundPresetListItem[] };+ setPresets(body.presets ?? []);+ } catch (err) {+ setError(err instanceof Error ? err.message : String(err));+ } finally {+ setLoading(false);+ }+ }, []);++ const create = useCallback(+ async (input: CreateInput): Promise<PlaygroundPresetListItem | null> => {+ setLoading(true);+ setError(null);+ try {+ const res = await fetch("/api/playground/presets", {+ method: "POST",+ headers: { "Content-Type": "application/json" },+ body: JSON.stringify(input),+ });+ if (!res.ok) {+ const body = (await res.json().catch(() => ({}))) as {+ error?: { message?: string };+ };+ throw new Error(body.error?.message ?? `HTTP ${res.status}`);+ }+ const created = (await res.json()) as PlaygroundPresetListItem;+ // Refresh the list after mutation.+ await list();+ return created;+ } catch (err) {+ setError(err instanceof Error ? err.message : String(err));+ return null;+ } finally {+ setLoading(false);+ }+ },+ [list],+ );++ const update = useCallback(+ async (id: string, patch: UpdateInput): Promise<PlaygroundPresetListItem | null> => {+ setLoading(true);+ setError(null);+ try {+ const res = await fetch(`/api/playground/presets/${id}`, {+ method: "PUT",+ headers: { "Content-Type": "application/json" },+ body: JSON.stringify(patch),+ });+ if (!res.ok) {+ const body = (await res.json().catch(() => ({}))) as {+ error?: { message?: string };+ };+ throw new Error(body.error?.message ?? `HTTP ${res.status}`);+ }+ const updated = (await res.json()) as PlaygroundPresetListItem;+ // Refresh list after mutation.+ await list();+ return updated;+ } catch (err) {+ setError(err instanceof Error ? err.message : String(err));+ return null;+ } finally {+ setLoading(false);+ }+ },+ [list],+ );++ const remove = useCallback(+ async (id: string): Promise<void> => {+ setLoading(true);+ setError(null);+ try {+ const res = await fetch(`/api/playground/presets/${id}`, {+ method: "DELETE",+ });+ if (!res.ok && res.status !== 204) {+ const body = (await res.json().catch(() => ({}))) as {+ error?: { message?: string };+ };+ throw new Error(body.error?.message ?? `HTTP ${res.status}`);+ }+ // Refresh list after mutation.+ await list();+ } catch (err) {+ setError(err instanceof Error ? err.message : String(err));+ } finally {+ setLoading(false);+ }+ },+ [list],+ );++ return {+ presets,+ loading,+ error,+ list,+ create,+ update,+ remove,+ };+}