Skip to content

Commit 064cb31

Browse files
committed
refactor: use client API for spaces-config endpoint
1 parent 78fb5dd commit 064cb31

4 files changed

Lines changed: 46 additions & 69 deletions

File tree

src/lib/server/api/routes/groups/misc.ts

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { requiresUser } from "$lib/server/auth";
44
import { collections } from "$lib/server/database";
55
import { authCondition } from "$lib/server/auth";
66
import { config } from "$lib/server/config";
7+
import { Client } from "@gradio/client";
78

89
export interface FeatureFlags {
910
searchEnabled: boolean;
@@ -16,6 +17,8 @@ export interface FeatureFlags {
1617
isAdmin: boolean;
1718
}
1819

20+
export type ApiReturnType = Awaited<ReturnType<typeof Client.prototype.view_api>>;
21+
1922
export const misc = new Elysia()
2023
.use(authPlugin)
2124
.get("/public-config", async () => config.getPublicConfig())
@@ -71,7 +74,33 @@ export const misc = new Elysia()
7174
isAdmin: locals.isAdmin,
7275
} satisfies FeatureFlags;
7376
})
74-
.get("/spaces-config", () => {
75-
// todo: get spaces config
76-
return;
77+
.get("/spaces-config", async ({ query }) => {
78+
if (config.COMMUNITY_TOOLS !== "true") {
79+
throw new Error("Community tools are not enabled");
80+
}
81+
82+
const space = query.space;
83+
84+
if (!space) {
85+
throw new Error("Missing space");
86+
}
87+
88+
// Extract namespace from space URL or use as-is if it's already in namespace format
89+
let namespace = null;
90+
if (space.startsWith("https://huggingface.co/spaces/")) {
91+
namespace = space.split("/").slice(-2).join("/");
92+
} else if (space.match(/^[^/]+\/[^/]+$/)) {
93+
namespace = space;
94+
}
95+
96+
if (!namespace) {
97+
throw new Error("Invalid space name. Specify a namespace or a full URL on huggingface.co.");
98+
}
99+
100+
try {
101+
const api = await (await Client.connect(namespace)).view_api();
102+
return api as ApiReturnType;
103+
} catch (e) {
104+
throw new Error("Error fetching space API. Is the name correct?");
105+
}
77106
});

src/lib/utils/getGradioApi.ts

Lines changed: 0 additions & 16 deletions
This file was deleted.

src/routes/api/spaces-config/+server.ts

Lines changed: 0 additions & 45 deletions
This file was deleted.

src/routes/tools/ToolEdit.svelte

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@
88
import { browser } from "$app/environment";
99
import ToolLogo from "$lib/components/ToolLogo.svelte";
1010
import { colors, icons } from "$lib/utils/tools";
11-
import { getGradioApi } from "$lib/utils/getGradioApi";
1211
import { goto } from "$app/navigation";
1312
import { base } from "$app/paths";
1413
import ToolInputComponent from "./ToolInputComponent.svelte";
1514
import { error as errorStore } from "$lib/stores/errors";
1615
1716
import CarbonInformation from "~icons/carbon/information";
1817
import { page } from "$app/state";
18+
import { throwOnError, useAPIClient } from "$lib/APIClient";
1919
2020
interface Props {
2121
tool?: CommunityToolEditable | undefined;
@@ -31,6 +31,9 @@
3131
3232
let APIloading = $state(false);
3333
let formLoading = $state(false);
34+
35+
const client = useAPIClient();
36+
3437
const dispatch = createEventDispatcher<{ close: void }>();
3538
3639
onMount(async () => {
@@ -67,7 +70,13 @@
6770
6871
APIloading = true;
6972
70-
const api = await getGradioApi(editableTool.baseUrl);
73+
const api = await client["spaces-config"]
74+
.get({
75+
query: {
76+
space: editableTool.baseUrl,
77+
},
78+
})
79+
.then(throwOnError);
7180
7281
const newInputs = api.named_endpoints[editableTool.endpoint].parameters.map((param, idx) => {
7382
if (tool?.inputs[idx]?.name === param.parameter_name) {
@@ -319,7 +328,7 @@
319328
{/if}
320329

321330
{#if editableTool.baseUrl}
322-
{#await getGradioApi(spaceUrl)}
331+
{#await client["spaces-config"].get({ query: { space: spaceUrl } }).then(throwOnError)}
323332
<p class="text-sm text-gray-500">Loading...</p>
324333
{:then api}
325334
<div class="flex flex-row flex-wrap gap-4">
@@ -596,8 +605,8 @@
596605
No endpoints found in this space. Try another one.
597606
</p>
598607
{/if}
599-
{:catch error}
600-
<p class="text-sm text-gray-500">{error}</p>
608+
{:catch rejected}
609+
<p class="text-sm text-gray-500">{JSON.parse(rejected.message).value}</p>
601610
{/await}
602611
{/if}
603612
</div>

0 commit comments

Comments
 (0)