refactor(dbgpts): Refactor dbgpts for 0.7.0 (#2397)

This commit is contained in:
Fangyin Cheng
2025-03-05 13:33:03 +08:00
committed by GitHub
parent 243e98123d
commit d5a2a0bf3b
15 changed files with 504 additions and 69 deletions

View File

@@ -20,80 +20,82 @@ def _generate_dbgpts_zip(package_name: str, flow: ServeRequest) -> io.BytesIO:
flow_label = flow.label
flow_description = flow.description
dag_json = json.dumps(flow.flow_data.dict(), indent=4, ensure_ascii=False)
with zipfile.ZipFile(zip_buffer, "a", zipfile.ZIP_DEFLATED, False) as zip_file:
# Create MANIFEST.in
manifest = f"include dbgpts.toml\ninclude {flow_name}/definition/*.json"
readme = f"# {flow_label}\n\n{flow_description}"
zip_file.writestr(f"{package_name}/MANIFEST.in", manifest)
# Create README.md
readme = f"# {flow_label}\n\n{flow_description}"
zip_file.writestr(f"{package_name}/README.md", readme)
# Create module __init__.py
zip_file.writestr(
f"{package_name}/{flow_name}/__init__.py",
"",
)
# Create flow definition json
zip_file.writestr(
f"{package_name}/{flow_name}/definition/flow_definition.json",
dag_json,
)
# Create dbgpts.toml
dbgpts_toml = tomlkit.document()
# Add flow information
dbgpts_flow_toml = tomlkit.document()
dbgpts_flow_toml.add("label", "Simple Streaming Chat")
name_with_comment = tomlkit.string("awel_flow_simple_streaming_chat")
dbgpts_flow_toml.add("label", flow_label)
name_with_comment = tomlkit.string(flow_name)
name_with_comment.comment("A unique name for all dbgpts")
dbgpts_flow_toml.add("name", name_with_comment)
dbgpts_flow_toml.add("version", "0.1.0")
dbgpts_flow_toml.add(
"description",
flow_description,
)
dbgpts_flow_toml.add("authors", [])
definition_type_with_comment = tomlkit.string("json")
definition_type_with_comment.comment("How to define the flow, python or json")
dbgpts_flow_toml.add("definition_type", definition_type_with_comment)
dbgpts_toml.add("flow", dbgpts_flow_toml)
# Add python and json config
python_config = tomlkit.table()
dbgpts_toml.add("python_config", python_config)
json_config = tomlkit.table()
json_config.add("file_path", "definition/flow_definition.json")
json_config.comment("Json config")
dbgpts_toml.add("json_config", json_config)
# Transform to string
# Transform dbgpts.toml to string
toml_string = tomlkit.dumps(dbgpts_toml)
zip_file.writestr(f"{package_name}/dbgpts.toml", toml_string)
# Create pyproject.toml (uv style)
pyproject_toml = tomlkit.document()
# Add [tool.poetry] section
tool_poetry_toml = tomlkit.table()
tool_poetry_toml.add("name", package_name)
tool_poetry_toml.add("version", "0.1.0")
tool_poetry_toml.add("description", "A dbgpts package")
tool_poetry_toml.add("authors", [])
tool_poetry_toml.add("readme", "README.md")
pyproject_toml["tool"] = tomlkit.table()
pyproject_toml["tool"]["poetry"] = tool_poetry_toml
# Add [tool.poetry.dependencies] section
dependencies = tomlkit.table()
dependencies.add("python", "^3.10")
pyproject_toml["tool"]["poetry"]["dependencies"] = dependencies
# Add [project] section (modern PEP 621 format used by uv)
project_section = tomlkit.table()
project_section.add("name", package_name)
project_section.add("version", "0.1.0")
project_section.add("description", flow_description or "A dbgpts package")
project_section.add("readme", "README.md")
project_section.add("requires-python", ">=3.8")
project_section.add("dependencies", [])
pyproject_toml["project"] = project_section
# Add [build-system] section
build_system = tomlkit.table()
build_system.add("requires", ["poetry-core"])
build_system.add("build-backend", "poetry.core.masonry.api")
build_system.add("requires", ["setuptools>=61.0"])
build_system.add("build-backend", "setuptools.build_meta")
pyproject_toml["build-system"] = build_system
# Transform to string
pyproject_toml_string = tomlkit.dumps(pyproject_toml)
zip_file.writestr(f"{package_name}/pyproject.toml", pyproject_toml_string)
zip_buffer.seek(0)
return zip_buffer