11 * Creates a tool for making HTTP requests to vals in a Val Town project
12 */
13export const makeFetchTool = (
14 { bearerToken, project, branch_id }: { bearerToken?: string; project?: any; branch_id?: string } = {},
15) =>
16 tool({
17 name: "fetch",
18 description: "Make an HTTP request to a Val Town val and return the response. Useful for testing HTTP vals.",
19 parameters: z.object({
68 return {
69 type: "error",
70 message: `Error fetching val at path '${valPath}': ${error.message}`,
71 };
72 }
83 return {
84 type: "error",
85 message: `The val at path '${valPath}' is not an HTTP val. Only HTTP vals can be called with fetch.`,
86 };
87 }
111 let response;
112 try {
113 response = await fetch(valEndpoint + urlPath, options);
114 } catch (error: any) {
115 // Return error information
239
240 // Inject data to avoid extra round-trips
241 const initialData = await fetchInitialData();
242 const dataScript = `<script>
243 window.__INITIAL_DATA__ = ${JSON.stringify(initialData)};
286
2875. **API Design:**
288 - `fetch` handler is the entry point for HTTP vals
289 - Run the Hono app with `export default app.fetch // This is the entry point for HTTP vals`
290
291
49 files={project.data?.files}
50 branchId={branchId}
51 refetch={project.refetch}
52 />
53 </ProjectContext>
59 files,
60 branchId,
61 refetch,
62}: {
63 project: any;
64 files: any[];
65 branchId: string;
66 refetch: () => void;
67}) {
68 const [images, setImages] = useState<(string|null)[]>([]);
93 if (!messages?.length) return;
94 let last = messages.at(-1);
95 if (shouldRefetch(last)) {
96 refetch();
97 }
98 }, [messages]);
168}
169
170function shouldRefetch (message) {
171 for (let i = 0; i < message?.parts?.length; i++) {
172 let part = message.parts[i];
32 return;
33 }
34 branches.refetch();
35 if (res?.branch?.id) {
36 navigate(`/chat/${projectId}/branch/${res.branch.id}`);
19 try {
20 const endpoint = isRegistering ? '/api/auth/register' : '/api/auth/login';
21 const response = await fetch(endpoint, {
22 method: 'POST',
23 headers: {
23 const loadMessages = async () => {
24 try {
25 const response = await fetch('/api/messages');
26 const data = await response.json();
27 if (data.success) {
47 setLoading(true);
48 try {
49 const response = await fetch('/api/messages', {
50 method: 'POST',
51 headers: {
34});
35
36export default app.fetch;
16 };
17
18 const fetchMessages = async () => {
19 try {
20 const response = await fetch('/api/chat/messages');
21 if (response.ok) {
22 const messagesData = await response.json();
25 }
26 } catch (error) {
27 console.error('Error fetching messages:', error);
28 } finally {
29 setLoading(false);
32
33 useEffect(() => {
34 fetchMessages();
35 // Poll for new messages every 3 seconds
36 const interval = setInterval(fetchMessages, 3000);
37 return () => clearInterval(interval);
38 }, []);
55 };
56
57 const response = await fetch('/api/chat/messages', {
58 method: 'POST',
59 headers: {
27
28 try {
29 const response = await fetch('/api/jobs', {
30 method: 'POST',
31 headers: {
9 const [showForm, setShowForm] = useState(false);
10
11 const fetchJobs = async () => {
12 try {
13 const response = await fetch('/api/jobs');
14 if (response.ok) {
15 const jobsData = await response.json();
17 }
18 } catch (error) {
19 console.error('Error fetching jobs:', error);
20 } finally {
21 setLoading(false);
24
25 useEffect(() => {
26 fetchJobs();
27 }, []);
28