Skip to content

Commit 3a4ad9a

Browse files
committed
api spec generation PoC
1 parent 7f49252 commit 3a4ad9a

File tree

3 files changed

+40
-0
lines changed

3 files changed

+40
-0
lines changed

main.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,7 @@ def start_comfyui(asyncio_loop=None):
314314
setup_database()
315315

316316
prompt_server.add_routes()
317+
prompt_server.serve_api_spec()
317318
hijack_progress(prompt_server)
318319

319320
threading.Thread(target=prompt_worker, daemon=True, args=(prompt_server.prompt_queue, prompt_server,)).start()

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ tqdm
2020
psutil
2121
alembic
2222
SQLAlchemy
23+
aiohttp-apispec<2.3.0
2324

2425
#non essential dependencies:
2526
kornia>=0.7.1

server.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,13 @@
2020

2121
import aiohttp
2222
from aiohttp import web
23+
from aiohttp_apispec import (
24+
docs,
25+
request_schema,
26+
response_schema,
27+
setup_aiohttp_apispec,
28+
)
29+
from marshmallow import Schema, fields
2330
import logging
2431

2532
import mimetypes
@@ -38,6 +45,15 @@
3845
from api_server.routes.internal.internal_routes import InternalRoutes
3946
from protocol import BinaryEventTypes
4047

48+
def inline_schema(name, **fields_map):
49+
""" Utility function to create an inline schema for aiohttp_apispec """
50+
return type(name+"Schema", (Schema,), fields_map)()
51+
52+
53+
def inline_schema_list(name, **fields_map):
54+
""" Utility function to create an inline schema for aiohttp_apispec where the top-level is a list """
55+
return type(name+"Schema", (Schema,), fields_map)(many=True)
56+
4157
async def send_socket_catch_exception(function, message):
4258
try:
4359
await function(message)
@@ -258,6 +274,16 @@ async def get_root(request):
258274
return response
259275

260276
@routes.get("/embeddings")
277+
@docs(
278+
tags=["Core"],
279+
summary="(UI) Get embeddings",
280+
description="Returns a list of the files located in the embeddings/ directory that can be used as arguments for embedding nodes. The file extension is omitted.",
281+
)
282+
@request_schema(inline_schema("EmbeddingRequestTestParams",
283+
my_test_param1=fields.Int(required=True, description="Test description of a parameter"),
284+
my_test_param2=fields.Boolean(description="description for my_test_param2")
285+
))
286+
@response_schema(inline_schema_list("EmbeddingNames"))
261287
def get_embeddings(request):
262288
embeddings = folder_paths.get_filename_list("embeddings")
263289
return web.json_response(list(map(lambda a: os.path.splitext(a)[0], embeddings)))
@@ -791,6 +817,18 @@ def add_routes(self):
791817
web.static('/', self.web_root),
792818
])
793819

820+
def serve_api_spec(self):
821+
"""
822+
Serve the OpenAPI specification for the API. Must be called after routes are added.
823+
"""
824+
setup_aiohttp_apispec(
825+
app=self.app,
826+
title="ComfyUI API Documentation",
827+
version="v1",
828+
url="/api/docs/swagger.json",
829+
swagger_path="/api/docs",
830+
)
831+
794832
def get_queue_info(self):
795833
prompt_info = {}
796834
exec_info = {}

0 commit comments

Comments
 (0)