Skip to content

Commit c4283d0

Browse files
authored
Merge pull request FoundationAgents#345 from shenchucheng/main
Fix 'OSError: [WinError 193] %1 is not a valid Win32 application' on …
2 parents 3ca4d4f + 88baa1c commit c4283d0

File tree

1 file changed

+30
-32
lines changed

1 file changed

+30
-32
lines changed

metagpt/utils/mermaid.py

Lines changed: 30 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,14 @@
66
@File : mermaid.py
77
"""
88
import asyncio
9-
import subprocess
9+
import os
1010
from pathlib import Path
1111

1212
from metagpt.config import CONFIG
1313
from metagpt.const import PROJECT_ROOT
1414
from metagpt.logs import logger
1515
from metagpt.utils.common import check_cmd_exists
16-
import os
17-
import sys
16+
1817

1918
async def mermaid_to_file(mermaid_code, output_file_without_suffix, width=2048, height=2048) -> int:
2019
"""suffix: png/svg/pdf
@@ -28,41 +27,39 @@ async def mermaid_to_file(mermaid_code, output_file_without_suffix, width=2048,
2827
# Write the Mermaid code to a temporary file
2928
dir_name = os.path.dirname(output_file_without_suffix)
3029
if dir_name and not os.path.exists(dir_name):
31-
os.makedirs(dir_name)
30+
os.makedirs(dir_name)
3231
tmp = Path(f"{output_file_without_suffix}.mmd")
3332
tmp.write_text(mermaid_code, encoding="utf-8")
34-
33+
3534
engine = CONFIG.mermaid_engine.lower()
3635
if engine == "nodejs":
37-
if check_cmd_exists("mmdc") != 0:
36+
if check_cmd_exists(CONFIG.mmdc) != 0:
3837
logger.warning("RUN `npm install -g @mermaid-js/mermaid-cli` to install mmdc")
3938
return -1
40-
39+
4140
for suffix in ["pdf", "svg", "png"]:
4241
output_file = f"{output_file_without_suffix}.{suffix}"
4342
# Call the `mmdc` command to convert the Mermaid code to a PNG
4443
logger.info(f"Generating {output_file}..")
4544

4645
if CONFIG.puppeteer_config:
47-
commands =[
48-
CONFIG.mmdc,
49-
"-p",
50-
CONFIG.puppeteer_config,
51-
"-i",
52-
str(tmp),
53-
"-o",
54-
output_file,
55-
"-w",
56-
str(width),
57-
"-H",
58-
str(height),
59-
]
46+
commands = [
47+
CONFIG.mmdc,
48+
"-p",
49+
CONFIG.puppeteer_config,
50+
"-i",
51+
str(tmp),
52+
"-o",
53+
output_file,
54+
"-w",
55+
str(width),
56+
"-H",
57+
str(height),
58+
]
6059
else:
61-
commands =[CONFIG.mmdc, "-i", str(tmp), "-o", output_file, "-w", str(width), "-H", str(height)]
62-
process = await asyncio.create_subprocess_exec(
63-
*commands,
64-
stdout=asyncio.subprocess.PIPE,
65-
stderr=asyncio.subprocess.PIPE
60+
commands = [CONFIG.mmdc, "-i", str(tmp), "-o", output_file, "-w", str(width), "-H", str(height)]
61+
process = await asyncio.create_subprocess_shell(
62+
" ".join(commands), stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.PIPE
6663
)
6764

6865
stdout, stderr = await process.communicate()
@@ -71,15 +68,17 @@ async def mermaid_to_file(mermaid_code, output_file_without_suffix, width=2048,
7168
if stderr:
7269
logger.error(stderr.decode())
7370
else:
74-
75-
if engine =='playwright':
71+
if engine == "playwright":
7672
from metagpt.utils.mmdc_playwright import mermaid_to_file
73+
7774
return await mermaid_to_file(mermaid_code, output_file_without_suffix, width, height)
78-
elif engine =='pyppeteer':
75+
elif engine == "pyppeteer":
7976
from metagpt.utils.mmdc_pyppeteer import mermaid_to_file
77+
8078
return await mermaid_to_file(mermaid_code, output_file_without_suffix, width, height)
81-
elif engine =='ink':
79+
elif engine == "ink":
8280
from metagpt.utils.mmdc_ink import mermaid_to_file
81+
8382
return await mermaid_to_file(mermaid_code, output_file_without_suffix)
8483
else:
8584
logger.warning(f"Unsupported mermaid engine: {engine}")
@@ -137,9 +136,8 @@ class KnowledgeBase {
137136
SE-->>M: return summary"""
138137

139138

140-
141139
if __name__ == "__main__":
142140
loop = asyncio.new_event_loop()
143-
result = loop.run_until_complete(mermaid_to_file(MMC1, PROJECT_ROOT / f"{CONFIG.mermaid_engine}/1"))
144-
result = loop.run_until_complete(mermaid_to_file(MMC2, PROJECT_ROOT / f"{CONFIG.mermaid_engine}/1"))
141+
result = loop.run_until_complete(mermaid_to_file(MMC1, PROJECT_ROOT / f"{CONFIG.mermaid_engine}/1"))
142+
result = loop.run_until_complete(mermaid_to_file(MMC2, PROJECT_ROOT / f"{CONFIG.mermaid_engine}/1"))
145143
loop.close()

0 commit comments

Comments
 (0)