Skip to content

Commit 48026a0

Browse files
committed
Simplify tool schema TypeScript loading
1 parent af56726 commit 48026a0

2 files changed

Lines changed: 51 additions & 64 deletions

File tree

packages/react/src/api/atoms.tsx

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,6 @@ export const sourceToolsAtom = (sourceId: string, scopeId: ScopeId) =>
4040
});
4141

4242
export const toolSchemaAtom = (scopeId: ScopeId, toolId: ToolId) =>
43-
ExecutorApiClient.query("tools", "schema", {
44-
params: { scopeId, toolId },
45-
query: {},
46-
timeToLive: "1 minute",
47-
reactivityKeys: [ReactivityKey.tools],
48-
});
49-
50-
export const toolTypeScriptAtom = (scopeId: ScopeId, toolId: ToolId) =>
5143
ExecutorApiClient.query("tools", "schema", {
5244
params: { scopeId, toolId },
5345
query: { includeTypeScript: "true" },

packages/react/src/components/tool-detail.tsx

Lines changed: 51 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { useMemo, useState } from "react";
22
import { useAtomValue } from "@effect/atom-react";
33
import * as AsyncResult from "effect/unstable/reactivity/AsyncResult";
4-
import { toolSchemaAtom, toolTypeScriptAtom } from "../api/atoms";
4+
import { toolSchemaAtom } from "../api/atoms";
55
import {
66
ScopeId,
77
ToolId,
@@ -109,11 +109,19 @@ export function ToolDetail(props: {
109109
const data = useMemo(() => {
110110
if (!AsyncResult.isSuccess(toolContract)) return null;
111111
const v = toolContract.value;
112+
const definitions = Object.entries(v.typeScriptDefinitions ?? {}).map(([name, body]) => ({
113+
name,
114+
code: String(body),
115+
}));
116+
112117
return {
113118
description: v.description,
114119
inputSchema: v.inputSchema,
115120
outputSchema: v.outputSchema,
116121
schemaDefinitions: v.schemaDefinitions,
122+
inputTypeScript: v.inputTypeScript ? `type Input = ${v.inputTypeScript}` : null,
123+
outputTypeScript: v.outputTypeScript ? `type Output = ${v.outputTypeScript}` : null,
124+
definitions,
117125
};
118126
}, [toolContract]);
119127

@@ -213,68 +221,55 @@ export function ToolDetail(props: {
213221
)}
214222
</div>
215223
) : (
216-
<ToolTypeScriptPanel scopeId={props.scopeId} toolId={props.toolId as ToolId} />
224+
<ToolTypeScriptPanel
225+
inputTypeScript={data?.inputTypeScript ?? null}
226+
outputTypeScript={data?.outputTypeScript ?? null}
227+
definitions={data?.definitions ?? []}
228+
/>
217229
),
218230
})}
219231
</div>
220232
</div>
221233
);
222234
}
223235

224-
function ToolTypeScriptPanel(props: { scopeId: ScopeId; toolId: ToolId }) {
225-
const toolContract = useAtomValue(toolTypeScriptAtom(props.scopeId, props.toolId));
226-
227-
const data = useMemo(() => {
228-
if (!AsyncResult.isSuccess(toolContract)) return null;
229-
const v = toolContract.value;
230-
const definitions = Object.entries(v.typeScriptDefinitions ?? {}).map(([name, body]) => ({
231-
name,
232-
code: String(body),
233-
}));
234-
235-
return {
236-
inputTypeScript: v.inputTypeScript ? `type Input = ${v.inputTypeScript}` : null,
237-
outputTypeScript: v.outputTypeScript ? `type Output = ${v.outputTypeScript}` : null,
238-
definitions,
239-
};
240-
}, [toolContract]);
241-
242-
return AsyncResult.match(toolContract, {
243-
onInitial: () => <div className="p-5 text-sm text-muted-foreground">Loading…</div>,
244-
onFailure: () => <div className="p-5 text-sm text-destructive">Something went wrong</div>,
245-
onSuccess: () => (
246-
<div className="px-5 py-5 space-y-5">
247-
{data?.inputTypeScript ? (
248-
<CardStack>
249-
<CardStackHeader>Input</CardStackHeader>
250-
<CardStackContent>
251-
<ExpandableCodeBlock
252-
code={data.inputTypeScript}
253-
definitions={data.definitions}
254-
className="rounded-none border-0"
255-
/>
256-
</CardStackContent>
257-
</CardStack>
258-
) : (
259-
<EmptySection title="Input" message="void" />
260-
)}
261-
{data?.outputTypeScript ? (
262-
<CardStack>
263-
<CardStackHeader>Output</CardStackHeader>
264-
<CardStackContent>
265-
<ExpandableCodeBlock
266-
code={data.outputTypeScript}
267-
definitions={data.definitions}
268-
className="rounded-none border-0"
269-
/>
270-
</CardStackContent>
271-
</CardStack>
272-
) : (
273-
<EmptySection title="Output" message="void" />
274-
)}
275-
</div>
276-
),
277-
});
236+
function ToolTypeScriptPanel(props: {
237+
inputTypeScript: string | null;
238+
outputTypeScript: string | null;
239+
definitions: ReadonlyArray<{ name: string; code: string }>;
240+
}) {
241+
return (
242+
<div className="px-5 py-5 space-y-5">
243+
{props.inputTypeScript ? (
244+
<CardStack>
245+
<CardStackHeader>Input</CardStackHeader>
246+
<CardStackContent>
247+
<ExpandableCodeBlock
248+
code={props.inputTypeScript}
249+
definitions={props.definitions}
250+
className="rounded-none border-0"
251+
/>
252+
</CardStackContent>
253+
</CardStack>
254+
) : (
255+
<EmptySection title="Input" message="void" />
256+
)}
257+
{props.outputTypeScript ? (
258+
<CardStack>
259+
<CardStackHeader>Output</CardStackHeader>
260+
<CardStackContent>
261+
<ExpandableCodeBlock
262+
code={props.outputTypeScript}
263+
definitions={props.definitions}
264+
className="rounded-none border-0"
265+
/>
266+
</CardStackContent>
267+
</CardStack>
268+
) : (
269+
<EmptySection title="Output" message="void" />
270+
)}
271+
</div>
272+
);
278273
}
279274

280275
// ---------------------------------------------------------------------------

0 commit comments

Comments
 (0)