-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathtools.py
More file actions
1377 lines (1179 loc) · 59.6 KB
/
tools.py
File metadata and controls
1377 lines (1179 loc) · 59.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
Architecture Review Agent - Local Tool Functions
==================================================
Pure-Python helpers for architecture parsing, risk detection, Excalidraw
diagram generation, and component mapping. These are consumed by the
agent via @ai_function wrappers in main.py.
"""
from __future__ import annotations
import asyncio
import json
import logging
import math
import os
import re
from typing import Any
import yaml
from agent_framework import Message
from agent_framework.azure import AzureOpenAIChatClient
logger = logging.getLogger("arch-review")
# ═══════════════════════════════════════════════════════════════════════════
# 1. ARCHITECTURE PARSER
# ═══════════════════════════════════════════════════════════════════════════
_TYPE_KEYWORDS: dict[str, list[str]] = {
"database": ["database", "db", "mysql", "postgres", "mongodb", "redis", "sql", "dynamodb", "cosmos", "firestore"],
"cache": ["cache", "redis", "memcached", "cdn"],
"queue": ["queue", "kafka", "rabbitmq", "sqs", "event hub", "eventhub", "pubsub", "nats"],
"gateway": ["gateway", "api gateway", "load balancer", "lb", "nginx", "envoy", "ingress"],
"frontend": ["frontend", "ui", "spa", "react", "angular", "vue", "web app", "client"],
"storage": ["storage", "s3", "blob", "bucket", "file"],
"external": ["external", "third-party", "3rd party", "stripe", "twilio", "sendgrid"],
"monitoring": ["monitor", "logging", "metrics", "prometheus", "grafana", "datadog", "elastic"],
}
def _infer_type(text: str) -> str:
lower = text.lower()
for comp_type, keywords in _TYPE_KEYWORDS.items():
if any(kw in lower for kw in keywords):
return comp_type
return "service"
def _extract_replicas(text: str) -> int:
# e.g. "3 replica", "5 instances", "2 nodes"
m = re.search(r"(\d+)\s*(?:replica|instance|node|pod)", text, re.IGNORECASE)
if m:
return int(m.group(1))
# Leading count in name, e.g. "3 API servers" or "2 backends"
m2 = re.match(r"^(\d+)\s+\w", text.strip())
if m2:
return int(m2.group(1))
return 1
def _strip_leading_count(text: str) -> str:
"""Remove a leading numeric count from a component name.
``"3 API servers"`` → ``"API servers"`` (replicas extracted separately).
Only strips when the remainder is ≥2 words to avoid stripping names like
``"5th generation service"``.
"""
m = re.match(r"^(\d+)\s+(.+)", text.strip())
if m and len(m.group(2).split()) >= 1:
return m.group(2)
return text
def _sanitize_component_name(raw: str) -> str:
"""Clean component names extracted from free-form text.
Prevents inline explanatory prose from being absorbed into node names,
e.g. ``Redis cache. Auth handled by API`` -> ``Redis cache``.
"""
text = raw.strip().lstrip("-* ").strip().strip('"\'`')
if not text:
return text
# Split trailing prose sentences while preserving names like "Node.js".
text = re.split(r"(?<=[a-z0-9])\.\s+(?=[A-Z])", text, maxsplit=1)[0]
# Remove trailing punctuation noise.
text = re.sub(r"[\s,;:.]+$", "", text)
return re.sub(r"\s+", " ", text).strip()
def _normalize_flat_markdown(content: str) -> str:
"""Rehydrate markdown that was flattened to a single line.
This commonly happens when invoking local agent CLI with
`(Get-Content file) -join " "`.
"""
if "\n" in content:
return content
if "## " not in content and "### " not in content:
return content
text = content
# Headings
text = re.sub(r"\s+(##\s+)", r"\n\1", text)
text = re.sub(r"\s+(###\s+)", r"\n\1", text)
# Bullets (but avoid arrow syntax like '->')
text = re.sub(r"\s+-\s+(?!>)", r"\n- ", text)
return text.strip()
def _truncate_component_label(text: str, max_chars: int = 34) -> str:
"""Keep node labels short enough to fit diagram boxes."""
t = re.sub(r"\s+", " ", text).strip()
return t if len(t) <= max_chars else (t[: max_chars - 3].rstrip() + "...")
def _parse_flattened_yaml_payload(content: str) -> dict[str, Any] | None:
"""Parse YAML-like payloads that were flattened to one line.
Local invocations often use `(Get-Content file) -join " "`, which can
collapse valid YAML into a single line that `yaml.safe_load` cannot parse
reliably (especially when the file starts with comment lines).
"""
if "\n" in content:
return None
lower = content.lower()
if "components:" not in lower:
return None
# Drop any leading prose/comments before the schema starts.
start = lower.find("components:")
if start > 0:
content = content[start:]
known_keys = "name|type|technology|replicas|description|from|to|target|source|protocol|label"
def _extract_field(block: str, key: str) -> str:
pat = rf"\b{key}:\s*(.+?)(?=\s+\b(?:{known_keys})\b:|$)"
m = re.search(pat, block, re.IGNORECASE)
return m.group(1).strip() if m else ""
sections = re.split(r"\bconnections\s*:", content, maxsplit=1, flags=re.IGNORECASE)
comp_text = sections[0]
conn_text = sections[1] if len(sections) > 1 else ""
components: list[dict[str, Any]] = []
connections: list[dict[str, Any]] = []
comp_iter = re.finditer(
r"-\s+name:\s*(.+?)(?=\s+-\s+name:|\s+connections:|$)",
comp_text,
re.IGNORECASE,
)
for m in comp_iter:
block = m.group(1).strip()
name = re.split(r"\s+\b(?:type|technology|replicas|description)\b:\s*", block, maxsplit=1, flags=re.IGNORECASE)[0].strip()
name = _sanitize_component_name(name)
if not name:
continue
cid = re.sub(r"[^a-z0-9]", "_", name.lower()).strip("_")
rtxt = _extract_field(block, "replicas")
components.append({
"id": cid,
"name": name,
"type": (_extract_field(block, "type") or _infer_type(name)).lower(),
"description": _extract_field(block, "description"),
"replicas": int(rtxt) if rtxt.isdigit() else 1,
"technology": _extract_field(block, "technology"),
})
conn_iter = re.finditer(
r"-\s+(?:from|source):\s*(.+?)(?=\s+-\s+(?:from|source):|$)",
conn_text,
re.IGNORECASE,
)
for m in conn_iter:
block = m.group(1).strip()
src = re.split(r"\s+\b(?:to|target|protocol|label|description)\b:\s*", block, maxsplit=1, flags=re.IGNORECASE)[0].strip()
tgt = _extract_field(block, "to") or _extract_field(block, "target")
if not src or not tgt:
continue
source_id = re.sub(r"[^a-z0-9]", "_", _sanitize_component_name(src).lower()).strip("_")
target_id = re.sub(r"[^a-z0-9]", "_", _sanitize_component_name(tgt).lower()).strip("_")
if not source_id or not target_id:
continue
connections.append({
"source": source_id,
"target": target_id,
"label": _extract_field(block, "protocol") or _extract_field(block, "label"),
"type": "sync",
})
if not components and not connections:
return None
return {
"components": components,
"connections": connections,
"metadata": {},
"detected_format": "yaml",
}
_ARCH_YAML_KEYS = frozenset(
"components services nodes connections edges flows links"
" name type technology replicas description from to source target".split()
)
def _detect_format(content: str) -> str:
stripped = content.strip()
# YAML: must start with a document marker OR have a key that looks like a
# known architecture keyword (not just any English sentence ending in ':')
if stripped.startswith("---") or re.search(
r"^(?:" + "|".join(_ARCH_YAML_KEYS) + r")\s*:",
stripped, re.MULTILINE | re.IGNORECASE,
):
try:
data = yaml.safe_load(stripped)
if isinstance(data, dict) and _ARCH_YAML_KEYS & set(data.keys()):
return "yaml"
except yaml.YAMLError:
pass
if re.search(r"^#{1,3}\s+", stripped, re.MULTILINE):
return "markdown"
return "text"
def _parse_yaml(content: str) -> dict[str, Any]:
data = yaml.safe_load(content)
if not isinstance(data, dict):
return {"components": [], "connections": [], "metadata": {}}
components: list[dict] = []
connections: list[dict] = []
for key in ("components", "services", "nodes"):
for item in data.get(key, []) or []:
if isinstance(item, dict):
components.append({
"id": item.get("id") or item.get("name", "").lower().replace(" ", "_"),
"name": item.get("name", item.get("id", "unknown")),
"type": item.get("type", "service"),
"description": item.get("description", ""),
"replicas": item.get("replicas", 1),
"technology": item.get("technology", ""),
})
elif isinstance(item, str):
cid = item.lower().replace(" ", "_").replace("-", "_")
components.append({"id": cid, "name": item, "type": "service",
"description": "", "replicas": 1, "technology": ""})
for key in ("connections", "edges", "flows", "links"):
for item in data.get(key, []) or []:
if isinstance(item, dict):
connections.append({
"source": (item.get("from") or item.get("source", "")).lower().replace(" ", "_"),
"target": (item.get("to") or item.get("target", "")).lower().replace(" ", "_"),
"label": item.get("label") or item.get("protocol", ""),
"type": item.get("type", "sync"),
})
elif isinstance(item, str):
parts = re.split(r"\s*(?:->|→|>>)\s*", item)
if len(parts) == 2:
connections.append({
"source": parts[0].strip().lower().replace(" ", "_"),
"target": parts[1].strip().lower().replace(" ", "_"),
"label": "", "type": "sync",
})
metadata = {k: v for k, v in data.items()
if k not in ("components", "services", "nodes", "connections", "edges", "flows", "links")}
return {"components": components, "connections": connections, "metadata": metadata}
def _parse_markdown(content: str) -> dict[str, Any]:
components: list[dict] = []
connections: list[dict] = []
top_section = ""
current_comp: dict | None = None
for line in content.split("\n"):
# Top-level sections: ## Components, ## Connections
h2 = re.match(r"^##\s+(.+)", line)
if h2:
if current_comp:
components.append(current_comp)
current_comp = None
section_raw = h2.group(1).strip()
inline_tail = ""
if " - " in section_raw:
head, tail = section_raw.split(" - ", 1)
top_section = head.strip().lower()
inline_tail = tail.strip()
else:
top_section = section_raw.lower()
# Flattened markdown can carry connections inline on the section line:
# "## Connections - A -> B (...) - B -> C (...)"
if inline_tail and any(k in top_section for k in ("connection", "flow", "edge", "link")):
for candidate in [c.strip() for c in re.split(r"\s+-\s+", inline_tail) if c.strip()]:
parts = re.split(r"\s*(?:->|→|>>)\s*", candidate)
if len(parts) >= 2:
src = parts[0].strip()
tgt_raw = parts[1].strip()
tgt_m = re.match(r"^(.+?)\s*\(.+\)$", tgt_raw)
tgt = tgt_m.group(1).strip() if tgt_m else tgt_raw
label_m = re.search(r"\((.+?)\)", tgt_raw)
label = label_m.group(1) if label_m else ""
connections.append({
"source": re.sub(r"[^a-z0-9]", "_", src.lower()).strip("_"),
"target": re.sub(r"[^a-z0-9]", "_", tgt.lower()).strip("_"),
"label": label,
"type": "sync",
})
continue
# Component headers: ### Name
h3 = re.match(r"^###\s+(.+)", line)
if h3:
if current_comp:
components.append(current_comp)
raw = h3.group(1).strip()
# Support flattened markdown where metadata is inline, e.g.
# "### Edge Gateway - **Type:** gateway - **Replicas:** 10"
parts = [p.strip() for p in re.split(r"\s+-\s+", raw) if p.strip()]
name = parts[0] if parts else raw
cid = re.sub(r"[^a-z0-9]", "_", name.lower()).strip("_")
current_comp = {"id": cid, "name": name, "type": "service",
"description": "", "replicas": 1, "technology": ""}
# Parse optional inline metadata segments after the name.
for part in parts[1:]:
lower = part.lower()
if lower.startswith("**type:**"):
current_comp["type"] = re.sub(r"^\*\*type:\*\*\s*", "", part, flags=re.IGNORECASE).strip().lower()
elif lower.startswith("**technology:**"):
current_comp["technology"] = re.sub(r"^\*\*technology:\*\*\s*", "", part, flags=re.IGNORECASE).strip()
elif lower.startswith("**replicas:**"):
val = re.sub(r"^\*\*replicas:\*\*\s*", "", part, flags=re.IGNORECASE).strip()
if val.isdigit():
current_comp["replicas"] = int(val)
else:
# Treat any remaining inline segment as description text.
current_comp["description"] = (current_comp["description"] + " " + part).strip()
continue
bullet = re.match(r"^\s*[-*]\s+(.+)", line)
if not bullet:
continue
text = bullet.group(1).strip()
# Inside a component definition - parse metadata bullets
if current_comp and "component" in top_section:
type_m = re.match(r"\*\*Type:\*\*\s*(.+)", text)
tech_m = re.match(r"\*\*Technology:\*\*\s*(.+)", text)
rep_m = re.match(r"\*\*Replicas:\*\*\s*(\d+)", text)
if type_m:
current_comp["type"] = type_m.group(1).strip().lower()
continue
if tech_m:
current_comp["technology"] = tech_m.group(1).strip()
continue
if rep_m:
current_comp["replicas"] = int(rep_m.group(1))
continue
# Remaining bullets are description
current_comp["description"] = text
continue
# Inside connections section
if any(k in top_section for k in ("connection", "flow", "edge", "link")):
parts = re.split(r"\s*(?:->|→|>>)\s*", text)
if len(parts) >= 2:
src = parts[0].strip()
tgt_raw = parts[1].strip()
tgt_m = re.match(r"^(.+?)\s*\(.+\)$", tgt_raw)
tgt = tgt_m.group(1).strip() if tgt_m else tgt_raw
label_m = re.search(r"\((.+?)\)", tgt_raw)
label = label_m.group(1) if label_m else ""
connections.append({
"source": re.sub(r"[^a-z0-9]", "_", src.lower()).strip("_"),
"target": re.sub(r"[^a-z0-9]", "_", tgt.lower()).strip("_"),
"label": label,
"type": "sync",
})
if current_comp:
components.append(current_comp)
return {"components": components, "connections": connections, "metadata": {}}
def _parse_text(content: str) -> dict[str, Any]:
components: list[dict] = []
connections: list[dict] = []
seen: set[str] = set()
for line in content.split("\n"):
line = line.strip()
if not line or line.startswith("#"):
continue
arrow = re.split(r"\s*(?:->|→|>>|=>)\s*", line)
if len(arrow) >= 2:
# Support chained arrows: A -> B -> C creates edges A→B, B→C
parts = [_sanitize_component_name(p) for p in arrow]
for idx in range(len(parts) - 1):
src, tgt = parts[idx], parts[idx + 1]
# Extract replica count from leading number, e.g. "3 API servers"
src_replicas = _extract_replicas(src)
tgt_replicas = _extract_replicas(tgt)
src = _strip_leading_count(src)
tgt = _strip_leading_count(tgt)
sid = re.sub(r"[^a-z0-9]", "_", src.lower()).strip("_")
tid = re.sub(r"[^a-z0-9]", "_", tgt.lower()).strip("_")
if not sid or not tid:
continue
connections.append({"source": sid, "target": tid, "label": "", "type": "sync"})
for cid, cname, creplicas in [(sid, src, src_replicas), (tid, tgt, tgt_replicas)]:
if cid not in seen:
seen.add(cid)
components.append({"id": cid, "name": cname, "type": _infer_type(cname),
"description": "", "replicas": creplicas, "technology": ""})
continue
m = re.match(r"^\s*[-*]\s+(.+)", line)
text = m.group(1).strip() if m else line
name_m = re.match(r"^(.+?)(?:\s*\((.+)\))?$", text)
name = _sanitize_component_name(name_m.group(1).strip() if name_m else text)
desc = name_m.group(2).strip() if name_m and name_m.group(2) else ""
cid = re.sub(r"[^a-z0-9]", "_", name.lower()).strip("_")
if cid and cid not in seen:
seen.add(cid)
components.append({"id": cid, "name": name, "type": _infer_type(name + " " + desc),
"description": desc, "replicas": _extract_replicas(desc), "technology": ""})
return {"components": components, "connections": connections, "metadata": {}}
_PROSE_PREFIX_RE = re.compile(
# Match a conversational phrase ending with ": ".
# Requires at least one interior space (multi-word phrase) to avoid matching
# single-word YAML keys like "name:", "components:", or "connections:".
r"^[A-Za-z][^:\n]*\s[^:\n]{1,60}:\s+",
re.DOTALL,
)
def _strip_leading_prose(content: str) -> str:
"""Remove a short conversational prefix before structured content.
Handles inputs like:
- ``"Review my architecture: Load Balancer -> ..."``
- ``"Analyse this design and highlight risks: ## Components ..."``
Only strips when the prefix is clearly multi-word (not a bare YAML key)
and the remainder looks like structured content (heading, arrow, or YAML key).
"""
m = _PROSE_PREFIX_RE.match(content.strip())
if not m:
return content
remainder = content.strip()[m.end():].lstrip()
# Only strip if remainder begins with something structural
if re.match(r"(?:#{1,3}\s|---\s*\n|[\w][\w\s]*:\s*(?:\S|-)|.+\s*(?:->|→|>>))", remainder):
logger.debug("[PARSER] Stripped leading prose prefix: %r", m.group(0))
return remainder
return content
def parse_architecture(content: str, format_hint: str = "auto") -> dict[str, Any]:
"""Parse architecture description (YAML / Markdown / plaintext) into
``{"components": [...], "connections": [...], "detected_format": str}``.
If the rule-based parser extracts too few results (≤1 component with no
connections) and ``llm_fallback`` isn't explicitly disabled, the original
content is returned with a flag so the caller can decide to invoke the
LLM-based inference.
"""
# Strip a short conversational prefix found in direct agent invocations.
content = _strip_leading_prose(content)
# Special handling for one-line YAML payloads from `Get-Content ... -join " "`.
flat_yaml = _parse_flattened_yaml_payload(content)
if flat_yaml is not None:
result = flat_yaml
else:
content = _normalize_flat_markdown(content)
fmt = format_hint if format_hint != "auto" else _detect_format(content)
logger.debug("[PARSER] Detected format: %s (hint=%s)", fmt, format_hint)
result = {"yaml": _parse_yaml, "markdown": _parse_markdown}.get(fmt, _parse_text)(content)
result["detected_format"] = fmt
# Back-fill components referenced only in connections
known = {c["id"] for c in result["components"]}
for conn in result["connections"]:
for role in ("source", "target"):
if conn[role] not in known:
result["components"].append({
"id": conn[role], "name": conn[role].replace("_", " ").title(),
"type": "service", "description": "(auto-discovered)", "replicas": 1, "technology": "",
})
known.add(conn[role])
# Check if parsing extracted meaningful results
n_comp = len(result["components"])
n_conn = len(result["connections"])
result["parsing_sufficient"] = (n_comp >= 2) or (n_comp >= 1 and n_conn >= 1)
logger.debug("[PARSER] Rule-based result: %d components, %d connections, sufficient=%s",
n_comp, n_conn, result["parsing_sufficient"])
return result
# ═══════════════════════════════════════════════════════════════════════════
# 1b. LLM-BASED ARCHITECTURE INFERENCE
# ═══════════════════════════════════════════════════════════════════════════
_LLM_INFERENCE_PROMPT = """\
You are an expert software architect. Analyse the following content - it may be
a README, design document, code file, deployment manifest, infrastructure config,
or any other text that describes or implies a software system architecture.
**Your task:** extract a structured architecture description in JSON with exactly
this schema:
```json
{
"architecture_name": "<short title for the system>",
"components": [
{
"id": "<snake_case_id>",
"name": "<Display Name>",
"type": "<one of: frontend, gateway, service, database, cache, queue, storage, external, monitoring>",
"description": "<brief purpose>",
"technology": "<tech stack if mentioned, else empty string>",
"replicas": <number, default 1>
}
],
"connections": [
{
"source": "<component id>",
"target": "<component id>",
"label": "<protocol or description>",
"type": "sync"
}
],
"risks": [
{
"component": "<Display Name of affected component>",
"severity": "<critical | high | medium | low>",
"issue": "<one-liner describing the problem>",
"recommendation": "<one-liner actionable fix>"
}
]
}
```
**Rules:**
1. Infer component types from context (names, descriptions, technology).
2. Create connections based on data flow, API calls, event publishing, or any
dependency you can infer - even if not explicitly stated as arrows.
3. If the text only lists services without explicit connections, infer likely
connections based on common architectural patterns.
4. Use snake_case for all IDs. Names should be human-readable.
5. Include ALL components you can identify - even infrastructure (load balancers,
message queues, caches, databases, monitoring).
6. If the input is too vague to extract any architecture, return an empty
components array with a note in architecture_name.
7. Return ONLY valid JSON - no markdown fences, no commentary.
8. For risks: identify SPOFs, missing redundancy, shared-DB anti-patterns,
security gaps, scalability bottlenecks, and missing observability.
Each issue and recommendation MUST be exactly one concise sentence.
"""
async def infer_architecture_llm(content: str) -> dict[str, Any]:
"""Use Azure OpenAI to analyse **any** text and extract an architecture.
Works with READMEs, design docs, code, plain descriptions, infra configs, etc.
Falls back gracefully if Azure OpenAI is not configured.
Uses ``AzureOpenAIChatClient`` from the agent framework which natively
handles Azure endpoints, api-version, and authentication. Reads config
from env vars: ``AZURE_OPENAI_ENDPOINT``, ``AZURE_OPENAI_API_KEY``,
``AZURE_OPENAI_CHAT_DEPLOYMENT_NAME`` / ``MODEL_DEPLOYMENT_NAME``.
Returns the same ``{"components": [...], "connections": [...], ...}``
schema as ``parse_architecture()``.
"""
logger.debug("[LLM] Starting LLM inference (input length: %d chars)", len(content))
endpoint = os.environ.get("AZURE_OPENAI_ENDPOINT", "")
deployment = (
os.environ.get("AZURE_OPENAI_CHAT_DEPLOYMENT_NAME")
or os.environ.get("AZURE_AI_MODEL_DEPLOYMENT_NAME")
or os.environ.get("MODEL_DEPLOYMENT_NAME", "gpt-4.1")
)
if not endpoint:
logger.debug("[LLM] AZURE_OPENAI_ENDPOINT not set - skipping LLM")
return _llm_error("AZURE_OPENAI_ENDPOINT not set - LLM inference unavailable")
logger.debug("[LLM] Using endpoint=%s, deployment=%s", endpoint, deployment)
try:
api_key = os.environ.get("AZURE_OPENAI_API_KEY", "")
if api_key:
logger.debug("[LLM] Using API key authentication")
client = AzureOpenAIChatClient(
endpoint=endpoint,
deployment_name=deployment,
api_key=api_key,
)
else:
logger.debug("[LLM] Using DefaultAzureCredential authentication")
from azure.identity import DefaultAzureCredential
client = AzureOpenAIChatClient(
endpoint=endpoint,
deployment_name=deployment,
credential=DefaultAzureCredential(),
)
response = await client.get_response(
messages=[
Message(role="system", text=_LLM_INFERENCE_PROMPT),
Message(role="user", text=content[:12000]),
],
temperature=0.1,
)
raw = response.text or "{}"
data = json.loads(raw)
logger.debug("[LLM] Response received - %d components, %d connections inferred",
len(data.get("components", [])), len(data.get("connections", [])))
except Exception as exc:
logger.debug("[LLM] Call failed: %s", exc)
return _llm_error(f"LLM call failed: {exc}")
# Normalise into our standard schema
components: list[dict] = []
for c in data.get("components", []):
cid = c.get("id") or re.sub(r"[^a-z0-9]", "_", c.get("name", "unknown").lower()).strip("_")
components.append({
"id": cid,
"name": c.get("name", cid.replace("_", " ").title()),
"type": c.get("type", _infer_type(c.get("name", "") + " " + c.get("technology", ""))),
"description": c.get("description", ""),
"technology": c.get("technology", ""),
"replicas": c.get("replicas", 1),
})
connections: list[dict] = []
known_ids = {c["id"] for c in components}
for conn in data.get("connections", []):
src = conn.get("source", "")
tgt = conn.get("target", "")
if src and tgt:
connections.append({
"source": src,
"target": tgt,
"label": conn.get("label", ""),
"type": conn.get("type", "sync"),
})
# Back-fill components referenced only in connections
for role_id, role_name in [(src, src), (tgt, tgt)]:
if role_id not in known_ids:
components.append({
"id": role_id,
"name": role_id.replace("_", " ").title(),
"type": _infer_type(role_id),
"description": "(LLM-inferred)",
"replicas": 1,
"technology": "",
})
known_ids.add(role_id)
# Normalise LLM-generated risks
llm_risks: list[dict] = []
for r in data.get("risks", []):
sev = r.get("severity", "medium")
if sev not in ("critical", "high", "medium", "low"):
sev = "medium"
llm_risks.append({
"component": r.get("component", "Architecture"),
"severity": sev,
"issue": r.get("issue", ""),
"recommendation": r.get("recommendation", ""),
})
return {
"components": components,
"connections": connections,
"metadata": {"architecture_name": data.get("architecture_name", "")},
"detected_format": "llm-inferred",
"llm_inferred": True,
"llm_risks": llm_risks,
"parsing_sufficient": len(components) >= 1,
}
def _llm_error(msg: str) -> dict[str, Any]:
return {
"components": [], "connections": [], "metadata": {},
"detected_format": "unstructured", "llm_inferred": True,
"parsing_sufficient": False, "error": msg,
}
async def smart_parse(content: str, format_hint: str = "auto") -> dict[str, Any]:
"""Parse architecture with automatic LLM fallback.
1. Try the fast rule-based parser first.
2. If it extracts too few results, invoke the LLM to analyse the content.
3. Return whichever result has more meaningful data.
"""
logger.debug("[SMART_PARSE] Starting - format_hint=%s", format_hint)
result = parse_architecture(content, format_hint)
if result["parsing_sufficient"]:
logger.info("[SMART_PARSE] Using RULE-BASED parser (%s) - %d components, %d connections",
result["detected_format"], len(result["components"]), len(result["connections"]))
return result
# Rule-based parsing was insufficient - try LLM inference
logger.info("[SMART_PARSE] Rule-based insufficient (%d components) - falling back to LLM",
len(result["components"]))
llm_result = await infer_architecture_llm(content)
if llm_result.get("error"):
logger.warning("[SMART_PARSE] LLM fallback failed: %s - using rule-based result",
llm_result["error"])
# LLM unavailable - return whatever we got from rule-based
return result
logger.info("[SMART_PARSE] Using LLM-INFERRED result - %d components, %d connections",
len(llm_result["components"]), len(llm_result["connections"]))
return llm_result
# ═══════════════════════════════════════════════════════════════════════════
# 2. RISK DETECTOR
# ═══════════════════════════════════════════════════════════════════════════
def _detect_spof(comps: list[dict], conns: list[dict]) -> list[dict]:
risks: list[dict] = []
fan_in: dict[str, int] = {}
for c in conns:
fan_in[c["target"]] = fan_in.get(c["target"], 0) + 1
for comp in comps:
replicas = comp.get("replicas", 1)
if replicas <= 1 and fan_in.get(comp["id"], 0) >= 2:
risks.append({"component": comp["name"], "severity": "critical",
"issue": f"Single point of failure - {fan_in[comp['id']]} dependants, 1 replica",
"recommendation": f"Scale {comp['name']} to ≥2 replicas behind a load balancer"})
elif replicas <= 1 and comp.get("type") in ("gateway", "database", "cache", "queue"):
risks.append({"component": comp["name"], "severity": "critical",
"issue": f"Infrastructure '{comp['type']}' has no redundancy",
"recommendation": f"Deploy {comp['name']} in HA configuration (cluster / multi-AZ)"})
return risks
def _detect_scalability(comps: list[dict], conns: list[dict]) -> list[dict]:
fan_in: dict[str, int] = {}
for c in conns:
fan_in[c["target"]] = fan_in.get(c["target"], 0) + 1
return [
{"component": comp["name"], "severity": "medium",
"issue": f"Shared {comp['type']} used by {fan_in[comp['id']]} services - contention risk",
"recommendation": f"Consider per-service {comp['type']} or partitioning"}
for comp in comps
if comp.get("type") in ("cache", "database", "queue") and fan_in.get(comp["id"], 0) >= 3
]
def _detect_security(comps: list[dict], conns: list[dict]) -> list[dict]:
risks: list[dict] = []
has_gw = any(c.get("type") == "gateway" for c in comps)
has_fe = any(c.get("type") == "frontend" for c in comps)
if has_fe and not has_gw:
risks.append({"component": "Architecture", "severity": "high",
"issue": "Frontend talks to backend without an API Gateway",
"recommendation": "Introduce an API Gateway for auth, rate-limiting, and routing"})
db_ids = {c["id"] for c in comps if c.get("type") == "database"}
fe_ids = {c["id"] for c in comps if c.get("type") == "frontend"}
for conn in conns:
if conn["source"] in fe_ids and conn["target"] in db_ids:
risks.append({"component": conn["target"], "severity": "critical",
"issue": "Frontend has direct database access",
"recommendation": "Route DB access through backend services"})
for ext in (c for c in comps if c.get("type") == "external"):
risks.append({"component": ext["name"], "severity": "medium",
"issue": f"External dependency '{ext['name']}' - no circuit-breaker",
"recommendation": f"Add circuit-breaker / retry for calls to {ext['name']}"})
return risks
def _detect_anti_patterns(comps: list[dict], conns: list[dict]) -> list[dict]:
db_ids = {c["id"] for c in comps if c.get("type") == "database"}
writers: dict[str, list[str]] = {}
for conn in conns:
if conn["target"] in db_ids:
writers.setdefault(conn["target"], []).append(conn["source"])
return [
{"component": next((c["name"] for c in comps if c["id"] == db_id), db_id),
"severity": "high",
"issue": f"Shared DB anti-pattern - {len(w)} services write to same database",
"recommendation": "Give each service its own data store"}
for db_id, w in writers.items() if len(w) > 1
]
def analyze_risks(components: list[dict], connections: list[dict]) -> dict[str, Any]:
"""Run all risk detectors. Returns severity-bucketed assessment."""
all_risks: list[dict] = []
all_risks.extend(_detect_spof(components, connections))
all_risks.extend(_detect_scalability(components, connections))
all_risks.extend(_detect_security(components, connections))
all_risks.extend(_detect_anti_patterns(components, connections))
result: dict[str, Any] = {"critical": [], "high": [], "medium": [], "low": []}
for r in all_risks:
result[r.get("severity", "medium")].append(r)
result["summary"] = {
"total": len(all_risks),
"critical": len(result["critical"]), "high": len(result["high"]),
"medium": len(result["medium"]), "low": len(result["low"]),
}
return result
# ═══════════════════════════════════════════════════════════════════════════
# 3. EXCALIDRAW DIAGRAM RENDERER
# ═══════════════════════════════════════════════════════════════════════════
_COLORS: dict[str, dict[str, str]] = {
"frontend": {"bg": "#a5d8ff", "border": "#1971c2"},
"gateway": {"bg": "#d0bfff", "border": "#7048e8"},
"service": {"bg": "#b2f2bb", "border": "#2f9e44"},
"database": {"bg": "#ffec99", "border": "#e67700"},
"cache": {"bg": "#ffd8a8", "border": "#e8590c"},
"queue": {"bg": "#eebefa", "border": "#be4bdb"},
"storage": {"bg": "#d3f9d8", "border": "#37b24d"},
"external": {"bg": "#ffc9c9", "border": "#e03131"},
"monitoring": {"bg": "#dee2e6", "border": "#495057"},
}
_DEFAULT_COL = {"bg": "#e7f5ff", "border": "#1c7ed6"}
_W, _H = 200, 80
def _layout(comps: list[dict]) -> dict[str, tuple[int, int]]:
layer_order = {"frontend": 0, "gateway": 0, "service": 1,
"database": 2, "cache": 2, "queue": 2, "storage": 2,
"external": 2, "monitoring": 3}
layers: dict[int, list[dict]] = {}
for c in comps:
layers.setdefault(layer_order.get(c.get("type", "service"), 1), []).append(c)
positions: dict[str, tuple[int, int]] = {}
x_gap, y_gap = 300, 250
for li in sorted(layers):
items = layers[li]
start_x = -(len(items) * x_gap) // 2 + x_gap // 2
for i, comp in enumerate(items):
positions[comp["id"]] = (start_x + i * x_gap, li * y_gap)
return positions
def _rect(cid: str, name: str, ctype: str, x: int, y: int) -> list[dict]:
col = _COLORS.get(ctype, _DEFAULT_COL)
short_name = _truncate_component_label(name)
return [
{"type": "rectangle", "id": cid, "x": x, "y": y, "width": _W, "height": _H,
"strokeColor": col["border"], "backgroundColor": col["bg"],
"fillStyle": "solid", "roundness": {"type": 3}},
{"type": "text", "id": f"{cid}_lbl", "x": x + 10, "y": y + 12,
"width": _W - 20, "height": 24, "text": short_name, "fontSize": 18,
"textAlign": "center", "strokeColor": "#1e1e1e"},
{"type": "text", "id": f"{cid}_tag", "x": x + 10, "y": y + 44,
"width": _W - 20, "height": 16, "text": f"[{ctype.upper()}]",
"fontSize": 12, "textAlign": "center", "strokeColor": "#868e96"},
]
def _arrow(aid: str, sx: int, sy: int, tx: int, ty: int, label: str = "") -> list[dict]:
if abs(sy - ty) < 50:
x0, y0 = sx + _W, sy + _H // 2
x1, y1 = tx, ty + _H // 2
else:
x0, y0 = sx + _W // 2, sy + _H
x1, y1 = tx + _W // 2, ty
dx, dy = x1 - x0, y1 - y0
elems: list[dict] = [{"type": "arrow", "id": aid, "x": x0, "y": y0,
"width": abs(dx), "height": abs(dy), "strokeColor": "#495057",
"points": [[0, 0], [dx, dy]],
"startArrowhead": None, "endArrowhead": "arrow"}]
if label:
elems.append({"type": "text", "id": f"{aid}_lbl",
"x": x0 + dx // 2 - 40, "y": y0 + dy // 2 - 10,
"width": 80, "height": 16, "text": label,
"fontSize": 12, "textAlign": "center", "strokeColor": "#868e96"})
return elems
def generate_excalidraw_elements(components: list[dict], connections: list[dict]) -> dict[str, Any]:
"""Build Excalidraw elements JSON. Returns ``{"elements_json": str, "element_count": int}``."""
pos = _layout(components)
elems: list[dict] = []
# camera pseudo-element
if pos:
xs = [p[0] for p in pos.values()]
ys = [p[1] for p in pos.values()]
elems.append({"type": "cameraUpdate",
"x": min(xs) - 100, "y": min(ys) - 80,
"width": max(xs) + _W + 100 - (min(xs) - 100),
"height": max(ys) + _H + 80 - (min(ys) - 80)})
for comp in components:
p = pos.get(comp["id"], (0, 0))
elems.extend(_rect(comp["id"], comp["name"], comp.get("type", "service"), p[0], p[1]))
for i, conn in enumerate(connections):
sp, tp = pos.get(conn["source"], (0, 0)), pos.get(conn["target"], (0, 0))
elems.extend(_arrow(f"conn_{i}", sp[0], sp[1], tp[0], tp[1], conn.get("label", "")))
return {"elements_json": json.dumps(elems), "element_count": len(elems)}
# ═══════════════════════════════════════════════════════════════════════════
# 4. COMPONENT MAPPER
# ═══════════════════════════════════════════════════════════════════════════
def build_component_map(components: list[dict], connections: list[dict]) -> dict[str, Any]:
"""Dependency map with fan-in/fan-out metrics."""
outgoing: dict[str, list[str]] = {}
incoming: dict[str, list[str]] = {}
for conn in connections:
outgoing.setdefault(conn["source"], []).append(conn["target"])
incoming.setdefault(conn["target"], []).append(conn["source"])
cmap = [
{"id": c["id"], "name": c["name"], "type": c.get("type", "service"),
"depends_on": outgoing.get(c["id"], []), "depended_by": incoming.get(c["id"], []),
"fan_in": len(incoming.get(c["id"], [])), "fan_out": len(outgoing.get(c["id"], []))}
for c in components
]
return {
"component_map": cmap,
"statistics": {
"total_components": len(components),
"total_connections": len(connections),
"orphan_components": [c["name"] for c in components
if c["id"] not in outgoing and c["id"] not in incoming],
},
}
# ═══════════════════════════════════════════════════════════════════════════
# 5. FILE SAVE HELPER
# ═══════════════════════════════════════════════════════════════════════════
def save_excalidraw_file(elements_json: str, filepath: str = "./output/architecture.excalidraw") -> str:
"""Save Excalidraw elements as a ``.excalidraw`` file for local viewing."""
pseudo = {"cameraUpdate", "delete", "restoreCheckpoint"}
elements = json.loads(elements_json)
real = [e for e in elements if e.get("type") not in pseudo]
data = {"type": "excalidraw", "version": 2, "source": "arch-review",
"elements": real, "appState": {"viewBackgroundColor": "#ffffff"}, "files": {}}
os.makedirs(os.path.dirname(filepath) or ".", exist_ok=True)
with open(filepath, "w", encoding="utf-8") as f:
json.dump(data, f, indent=2)
return os.path.abspath(filepath)
# ═══════════════════════════════════════════════════════════════════════════
# 6. MCP DIAGRAM RENDERER (Excalidraw MCP Server Integration)
# ═══════════════════════════════════════════════════════════════════════════
# ── PNG color helpers (hex → RGB tuple) ──────────────────────────────────
def _hex_to_rgb(h: str) -> tuple[int, int, int]:
h = h.lstrip("#")
return (int(h[0:2], 16), int(h[2:4], 16), int(h[4:6], 16))
# ═══════════════════════════════════════════════════════════════════════════
# 6a. PNG EXPORT (Pillow-based - no external renderers needed)
# ═══════════════════════════════════════════════════════════════════════════
def export_png(
components: list[dict],
connections: list[dict],
filepath: str = "./output/architecture.png",
scale: float = 2.0,
) -> str:
"""Render the architecture diagram as a PNG image using Pillow.
Uses the same layout engine and colour palette as the Excalidraw renderer
so the PNG matches the interactive diagram exactly.
Args:
components: Parsed component list.