feat: client component for run workflow and get outputs
This commit is contained in:
parent
e88230997b
commit
770acfcedf
@ -23,44 +23,74 @@ import Link from "next/link";
|
|||||||
const curlTemplate = `
|
const curlTemplate = `
|
||||||
curl --request POST \
|
curl --request POST \
|
||||||
--url <URL> \
|
--url <URL> \
|
||||||
--header 'Content-Type: application/json' \
|
--header "Content-Type: application/json" \
|
||||||
--data '{
|
--data "{
|
||||||
"deployment_id": "<ID>"
|
"deployment_id": "<ID>"
|
||||||
}'
|
}"
|
||||||
`;
|
`;
|
||||||
|
|
||||||
const curlTemplate_checkStatus = `
|
const curlTemplate_checkStatus = `
|
||||||
curl --request GET \
|
curl --request GET \
|
||||||
--url 'http://localhost:3000/api/run?run_id=xxx' \
|
--url "<URL>/api/run?run_id=xxx" \
|
||||||
--header 'Content-Type: application/json'
|
--header "Content-Type: application/json"
|
||||||
`;
|
`;
|
||||||
|
|
||||||
const jsTemplate = `
|
const jsTemplate = `
|
||||||
const { run_id } = await fetch('<URL>', {
|
const { run_id } = await fetch("<URL>", {
|
||||||
method: 'POST',
|
method: "POST",
|
||||||
headers: {
|
headers: {
|
||||||
'Content-Type': 'application/json',
|
"Content-Type": "application/json",
|
||||||
'Authorization': 'Bearer ' + process.env.COMFY_DEPLOY_API_KEY,
|
"Authorization": "Bearer " + process.env.COMFY_DEPLOY_API_KEY,
|
||||||
},
|
},
|
||||||
body: JSON.stringify({
|
body: JSON.stringify({
|
||||||
deployment_id: '<ID>',
|
deployment_id: "<ID>",
|
||||||
inputs: {}
|
inputs: {}
|
||||||
}),
|
}),
|
||||||
}).then(response => response.json())
|
}).then(response => response.json())
|
||||||
`;
|
`;
|
||||||
|
|
||||||
const jsTemplate_checkStatus = `
|
const jsTemplate_checkStatus = `
|
||||||
const run_id = '<RUN_ID>';
|
const run_id = "<RUN_ID>";
|
||||||
|
|
||||||
const output = fetch('<URL>?run_id=' + run_id, {
|
const output = fetch("<URL>?run_id=" + run_id, {
|
||||||
method: 'GET',
|
method: "GET",
|
||||||
headers: {
|
headers: {
|
||||||
'Content-Type': 'application/json',
|
"Content-Type": "application/json",
|
||||||
'Authorization': 'Bearer ' + process.env.COMFY_DEPLOY_API_KEY,
|
"Authorization": "Bearer " + process.env.COMFY_DEPLOY_API_KEY,
|
||||||
},
|
},
|
||||||
}).then(response => response.json())
|
}).then(response => response.json())
|
||||||
`;
|
`;
|
||||||
|
|
||||||
|
const clientTemplate = `
|
||||||
|
const client = new ComfyDeployClient({
|
||||||
|
apiBase: "<URLONLY>",
|
||||||
|
apiToken: process.env.COMFY_DEPLOY_API_KEY!,
|
||||||
|
});
|
||||||
|
|
||||||
|
export async function generateTextures(scrImageId: string) {
|
||||||
|
const result = await client.run("<ID>", {
|
||||||
|
input_image: "",
|
||||||
|
});
|
||||||
|
if (!result || !result.run_id) return { error: "run id not found" };
|
||||||
|
return { id: result.run_id };
|
||||||
|
}
|
||||||
|
`;
|
||||||
|
|
||||||
|
const clientTemplate_checkStatus = `
|
||||||
|
const run_id = "<RUN_ID>";
|
||||||
|
|
||||||
|
while (true) {
|
||||||
|
const run = await client.getRun(run_id);
|
||||||
|
await new Promise((resolve) => setTimeout(resolve, 3000));
|
||||||
|
|
||||||
|
if (!run) continue;
|
||||||
|
run.outputs.map((val) => {
|
||||||
|
if (!val.data.image) return;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
`;
|
||||||
|
|
||||||
export function DeploymentDisplay({
|
export function DeploymentDisplay({
|
||||||
deployment,
|
deployment,
|
||||||
}: {
|
}: {
|
||||||
@ -91,21 +121,55 @@ export function DeploymentDisplay({
|
|||||||
</TableCell>
|
</TableCell>
|
||||||
</TableRow>
|
</TableRow>
|
||||||
</DialogTrigger>
|
</DialogTrigger>
|
||||||
<DialogContent className="max-w-2xl">
|
<DialogContent className="max-w-3xl">
|
||||||
<DialogHeader>
|
<DialogHeader>
|
||||||
<DialogTitle className="capitalize">
|
<DialogTitle className="capitalize">
|
||||||
{deployment.environment} Deployment
|
{deployment.environment} Deployment
|
||||||
</DialogTitle>
|
</DialogTitle>
|
||||||
<DialogDescription>Code for your deployment client</DialogDescription>
|
<DialogDescription>Code for your deployment client</DialogDescription>
|
||||||
</DialogHeader>
|
</DialogHeader>
|
||||||
<ScrollArea className="max-h-[600px]">
|
<ScrollArea className="max-h-[600px] pr-4">
|
||||||
{deployment.environment !== "public-share" ? (
|
{deployment.environment !== "public-share" ? (
|
||||||
<Tabs defaultValue="js" className="w-full">
|
<Tabs defaultValue="client" className="w-full gap-2">
|
||||||
<TabsList className="grid w-fit grid-cols-2">
|
<TabsList className="grid w-fit grid-cols-3">
|
||||||
|
<TabsTrigger value="client">client</TabsTrigger>
|
||||||
<TabsTrigger value="js">js</TabsTrigger>
|
<TabsTrigger value="js">js</TabsTrigger>
|
||||||
<TabsTrigger value="curl">curl</TabsTrigger>
|
<TabsTrigger value="curl">curl</TabsTrigger>
|
||||||
</TabsList>
|
</TabsList>
|
||||||
<TabsContent className="flex flex-col gap-2" value="js">
|
<TabsContent className="flex flex-col gap-2 !mt-0" value="client">
|
||||||
|
<div>
|
||||||
|
Trigger the workflow with
|
||||||
|
<a
|
||||||
|
href="https://github.com/BennyKok/comfyui-deploy-next-example/blob/main/src/lib/comfy-deploy.ts"
|
||||||
|
className="text-blue-500 hover:underline"
|
||||||
|
target="_blank"
|
||||||
|
>
|
||||||
|
comfy deploy wrapper
|
||||||
|
</a>
|
||||||
|
</div>
|
||||||
|
<div>
|
||||||
|
Copy the wrapper to your project, and import the function
|
||||||
|
</div>
|
||||||
|
<CodeBlock
|
||||||
|
lang="js"
|
||||||
|
code={formatCode(
|
||||||
|
clientTemplate,
|
||||||
|
deployment,
|
||||||
|
domain,
|
||||||
|
workflowInput
|
||||||
|
)}
|
||||||
|
/>
|
||||||
|
Check the status of the run, and retrieve the outputs
|
||||||
|
<CodeBlock
|
||||||
|
lang="js"
|
||||||
|
code={formatCode(
|
||||||
|
clientTemplate_checkStatus,
|
||||||
|
deployment,
|
||||||
|
domain
|
||||||
|
)}
|
||||||
|
/>
|
||||||
|
</TabsContent>
|
||||||
|
<TabsContent className="flex flex-col gap-2 !mt-0" value="js">
|
||||||
Trigger the workflow
|
Trigger the workflow
|
||||||
<CodeBlock
|
<CodeBlock
|
||||||
lang="js"
|
lang="js"
|
||||||
@ -122,7 +186,7 @@ export function DeploymentDisplay({
|
|||||||
code={formatCode(jsTemplate_checkStatus, deployment, domain)}
|
code={formatCode(jsTemplate_checkStatus, deployment, domain)}
|
||||||
/>
|
/>
|
||||||
</TabsContent>
|
</TabsContent>
|
||||||
<TabsContent className="flex flex-col gap-2" value="curl">
|
<TabsContent className="flex flex-col gap-2 !mt-2" value="curl">
|
||||||
<CodeBlock
|
<CodeBlock
|
||||||
lang="bash"
|
lang="bash"
|
||||||
code={formatCode(curlTemplate, deployment, domain)}
|
code={formatCode(curlTemplate, deployment, domain)}
|
||||||
@ -190,5 +254,6 @@ function formatCode(
|
|||||||
}
|
}
|
||||||
return codeTemplate
|
return codeTemplate
|
||||||
.replace("<URL>", `${domain ?? "http://localhost:3000"}/api/run`)
|
.replace("<URL>", `${domain ?? "http://localhost:3000"}/api/run`)
|
||||||
.replace("<ID>", deployment.id);
|
.replace("<ID>", deployment.id)
|
||||||
|
.replace("<URLONLY>", domain ?? "http://localhost:3000");
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user