ltcai 11.2.0 → 11.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (247) hide show
  1. package/README.md +46 -53
  2. package/docs/CHANGELOG.md +61 -0
  3. package/docs/COMMUNITY_AND_PLUGINS.md +1 -1
  4. package/docs/DEVELOPMENT.md +1 -1
  5. package/docs/MULTI_AGENT_RUNTIME.md +1 -1
  6. package/docs/ONBOARDING.md +1 -1
  7. package/docs/OPERATIONS.md +6 -2
  8. package/docs/PERMISSION_MODE.md +1 -1
  9. package/docs/TRUST_MODEL.md +1 -1
  10. package/docs/WHY_LATTICE.md +1 -1
  11. package/docs/kg-schema.md +2 -2
  12. package/docs/v11.3.0_PLAN.md +202 -0
  13. package/docs/v11.4.0_RUST_FOUNDATION_PLAN.md +176 -0
  14. package/lattice_brain/__init__.py +1 -1
  15. package/lattice_brain/graph/_kg_common/__init__.py +287 -0
  16. package/lattice_brain/graph/_kg_common/extraction.py +516 -0
  17. package/lattice_brain/graph/_kg_common/relations.py +161 -0
  18. package/lattice_brain/graph/_kg_common/text.py +479 -0
  19. package/lattice_brain/graph/discovery_index/__init__.py +35 -0
  20. package/lattice_brain/graph/discovery_index/cleanup.py +182 -0
  21. package/lattice_brain/graph/discovery_index/extract.py +137 -0
  22. package/lattice_brain/graph/discovery_index/scan.py +411 -0
  23. package/lattice_brain/graph/discovery_index/upsert.py +495 -0
  24. package/lattice_brain/graph/projection/__init__.py +42 -0
  25. package/lattice_brain/graph/projection/curation.py +500 -0
  26. package/lattice_brain/graph/{projection.py → projection/v2_schema.py} +15 -477
  27. package/lattice_brain/graph/retrieval/__init__.py +54 -0
  28. package/lattice_brain/graph/retrieval/context.py +197 -0
  29. package/lattice_brain/graph/retrieval/graph_view.py +319 -0
  30. package/lattice_brain/graph/retrieval/hybrid.py +488 -0
  31. package/lattice_brain/graph/retrieval/maintenance.py +121 -0
  32. package/lattice_brain/graph/retrieval/signals.py +95 -0
  33. package/lattice_brain/graph/retrieval_vector/__init__.py +42 -0
  34. package/lattice_brain/graph/retrieval_vector/fingerprint.py +97 -0
  35. package/lattice_brain/graph/retrieval_vector/indexing.py +347 -0
  36. package/lattice_brain/graph/retrieval_vector/search.py +560 -0
  37. package/lattice_brain/graph/retrieval_vector/status.py +374 -0
  38. package/lattice_brain/ingestion/__init__.py +130 -0
  39. package/lattice_brain/ingestion/_contract.py +90 -0
  40. package/lattice_brain/ingestion/constants.py +127 -0
  41. package/lattice_brain/ingestion/folder_scan.py +57 -0
  42. package/lattice_brain/ingestion/folders.py +258 -0
  43. package/lattice_brain/ingestion/hashing.py +26 -0
  44. package/lattice_brain/ingestion/jobs_api.py +107 -0
  45. package/lattice_brain/ingestion/models.py +80 -0
  46. package/lattice_brain/ingestion/pipeline.py +486 -0
  47. package/lattice_brain/ingestion/quality.py +209 -0
  48. package/lattice_brain/ingestion/routing.py +295 -0
  49. package/lattice_brain/multimodal/__init__.py +164 -0
  50. package/lattice_brain/multimodal/audio.py +77 -0
  51. package/lattice_brain/multimodal/common.py +118 -0
  52. package/lattice_brain/multimodal/images.py +498 -0
  53. package/lattice_brain/multimodal/ports.py +169 -0
  54. package/lattice_brain/multimodal/video.py +410 -0
  55. package/lattice_brain/portability/__init__.py +90 -0
  56. package/lattice_brain/portability/_contract.py +42 -0
  57. package/lattice_brain/portability/backups.py +338 -0
  58. package/lattice_brain/portability/bundles.py +136 -0
  59. package/lattice_brain/portability/constants.py +93 -0
  60. package/lattice_brain/portability/fsops.py +138 -0
  61. package/lattice_brain/portability/service.py +41 -0
  62. package/lattice_brain/{portability.py → portability/sharing.py} +44 -677
  63. package/lattice_brain/runtime/__init__.py +1 -1
  64. package/lattice_brain/runtime/multi_agent.py +1 -1
  65. package/latticeai/__init__.py +1 -1
  66. package/latticeai/api/chronicle.py +63 -0
  67. package/latticeai/core/agent/__init__.py +93 -0
  68. package/latticeai/core/agent/_contract.py +79 -0
  69. package/latticeai/core/agent/context.py +57 -0
  70. package/latticeai/core/agent/deps.py +125 -0
  71. package/latticeai/core/agent/execution.py +622 -0
  72. package/latticeai/core/agent/planning.py +145 -0
  73. package/latticeai/core/agent/recovery.py +157 -0
  74. package/latticeai/core/agent/runtime.py +210 -0
  75. package/latticeai/core/agent/verification.py +231 -0
  76. package/latticeai/core/embedding_providers/__init__.py +151 -0
  77. package/latticeai/core/embedding_providers/base.py +199 -0
  78. package/latticeai/core/embedding_providers/captions.py +162 -0
  79. package/latticeai/core/embedding_providers/profiles.py +126 -0
  80. package/latticeai/core/embedding_providers/text.py +350 -0
  81. package/latticeai/core/embedding_providers/vision.py +352 -0
  82. package/latticeai/core/file_generation/__init__.py +115 -0
  83. package/latticeai/core/file_generation/bundles.py +76 -0
  84. package/latticeai/core/file_generation/extraction.py +154 -0
  85. package/latticeai/core/file_generation/inference.py +235 -0
  86. package/latticeai/core/file_generation/orchestration.py +152 -0
  87. package/latticeai/core/file_generation/prompting.py +117 -0
  88. package/latticeai/core/file_generation/repair.py +114 -0
  89. package/latticeai/core/file_generation/sanitize.py +61 -0
  90. package/latticeai/core/file_generation/validation.py +201 -0
  91. package/latticeai/core/legacy_compatibility.py +1 -1
  92. package/latticeai/core/marketplace.py +1 -1
  93. package/latticeai/core/messages.py +9 -0
  94. package/latticeai/core/workspace_os_constants.py +1 -1
  95. package/latticeai/integrations/telegram_bot/__init__.py +123 -0
  96. package/latticeai/integrations/telegram_bot/__main__.py +17 -0
  97. package/latticeai/integrations/telegram_bot/config.py +86 -0
  98. package/latticeai/integrations/telegram_bot/dispatch.py +311 -0
  99. package/latticeai/integrations/telegram_bot/flows.py +478 -0
  100. package/latticeai/integrations/telegram_bot/helpers.py +322 -0
  101. package/latticeai/integrations/telegram_bot/screens.py +394 -0
  102. package/latticeai/models/router/__init__.py +88 -0
  103. package/latticeai/models/router/_contract.py +66 -0
  104. package/latticeai/models/router/branding.py +56 -0
  105. package/latticeai/models/router/catalog.py +69 -0
  106. package/latticeai/models/router/documents.py +199 -0
  107. package/latticeai/models/router/errors.py +37 -0
  108. package/latticeai/models/router/generation.py +258 -0
  109. package/latticeai/models/router/loading.py +291 -0
  110. package/latticeai/models/router/local_models.py +85 -0
  111. package/latticeai/models/router/registry.py +147 -0
  112. package/latticeai/runtime/build_phases/__init__.py +82 -0
  113. package/latticeai/runtime/build_phases/features.py +407 -0
  114. package/latticeai/runtime/build_phases/foundation.py +555 -0
  115. package/latticeai/runtime/build_phases/web.py +492 -0
  116. package/latticeai/runtime/runtime_context.py +1 -0
  117. package/latticeai/services/architecture_readiness.py +48 -19
  118. package/latticeai/services/brain_intelligence/__init__.py +58 -0
  119. package/latticeai/services/brain_intelligence/_contract.py +71 -0
  120. package/latticeai/services/brain_intelligence/consistency.py +193 -0
  121. package/latticeai/services/brain_intelligence/constants.py +47 -0
  122. package/latticeai/services/brain_intelligence/digest.py +258 -0
  123. package/latticeai/services/brain_intelligence/health.py +331 -0
  124. package/latticeai/services/brain_intelligence/proposals.py +264 -0
  125. package/latticeai/services/brain_intelligence/sampling.py +84 -0
  126. package/latticeai/services/brain_intelligence/service.py +48 -0
  127. package/latticeai/services/chronicle.py +557 -0
  128. package/latticeai/services/memory_service/__init__.py +52 -0
  129. package/latticeai/services/memory_service/_contract.py +100 -0
  130. package/latticeai/services/memory_service/brief.py +431 -0
  131. package/latticeai/services/memory_service/constants.py +57 -0
  132. package/latticeai/services/memory_service/maintenance.py +138 -0
  133. package/latticeai/services/memory_service/manager.py +186 -0
  134. package/latticeai/services/memory_service/proof.py +136 -0
  135. package/latticeai/services/memory_service/recall.py +225 -0
  136. package/latticeai/services/memory_service/service.py +48 -0
  137. package/latticeai/services/memory_service/stores.py +110 -0
  138. package/latticeai/services/model_runtime/__init__.py +322 -0
  139. package/latticeai/services/model_runtime/cloud.py +87 -0
  140. package/latticeai/services/model_runtime/download.py +282 -0
  141. package/latticeai/services/model_runtime/engines.py +341 -0
  142. package/latticeai/services/model_runtime/loading.py +178 -0
  143. package/latticeai/services/model_runtime/service.py +129 -0
  144. package/latticeai/services/model_runtime/state.py +131 -0
  145. package/latticeai/services/model_runtime/status.py +255 -0
  146. package/latticeai/services/product_readiness.py +15 -7
  147. package/latticeai/setup/wizard/__init__.py +126 -0
  148. package/latticeai/setup/wizard/catalog.py +172 -0
  149. package/latticeai/setup/wizard/detect.py +323 -0
  150. package/latticeai/setup/wizard/install.py +348 -0
  151. package/latticeai/setup/wizard/paths.py +168 -0
  152. package/latticeai/setup/wizard/plans.py +74 -0
  153. package/latticeai/setup/wizard/recommend.py +320 -0
  154. package/package.json +6 -2
  155. package/scripts/bump_version.py +14 -0
  156. package/scripts/capture_release_evidence.mjs +33 -21
  157. package/scripts/check_current_release_docs.mjs +1 -1
  158. package/scripts/check_i18n_namespace_coverage.mjs +41 -4
  159. package/scripts/check_max_file_lines.mjs +102 -0
  160. package/scripts/check_release_evidence_bound.mjs +30 -15
  161. package/scripts/check_screenshot_pixel_delta.py +34 -4
  162. package/scripts/check_server_i18n.mjs +1 -0
  163. package/scripts/generate_rust_parity_fixtures.py +562 -0
  164. package/scripts/lib/mock_server_fingerprint.mjs +94 -0
  165. package/scripts/release_screen_claims.json +31 -2
  166. package/src-tauri/Cargo.lock +361 -3
  167. package/src-tauri/Cargo.toml +6 -1
  168. package/src-tauri/src/backend.rs +349 -0
  169. package/src-tauri/src/folder.rs +33 -0
  170. package/src-tauri/src/main.rs +97 -399
  171. package/src-tauri/tauri.conf.json +1 -1
  172. package/static/app/asset-manifest.json +41 -37
  173. package/static/app/assets/Act-yYpYnn0v.js +1 -0
  174. package/static/app/assets/AdminConsole-DL3Cr5pL.js +1 -0
  175. package/static/app/assets/{Brain-tuhI4sOC.js → Brain-C1HBN0Wf.js} +2 -2
  176. package/static/app/assets/BrainHome-DoXRhUUC.js +2 -0
  177. package/static/app/assets/BrainSignals-6yR6ir5t.js +1 -0
  178. package/static/app/assets/Capture-CFIRsFNE.js +1 -0
  179. package/static/app/assets/Chronicle-BZbEgiwN.js +1 -0
  180. package/static/app/assets/CommandPalette-D2pMxC2I.js +1 -0
  181. package/static/app/assets/Library-DwO3yZST.js +1 -0
  182. package/static/app/assets/{LivingBrain-DBwhto14.js → LivingBrain-Jn1GK0-S.js} +1 -1
  183. package/static/app/assets/ProductFlow-B-w1R4Oo.js +1 -0
  184. package/static/app/assets/ReviewCard-6B27X8Vg.js +3 -0
  185. package/static/app/assets/System-DW8F-2xL.js +1 -0
  186. package/static/app/assets/arrow-left-DXvKg9U6.js +1 -0
  187. package/static/app/assets/{bot-Cia42c2h.js → bot-IM_E_Y12.js} +1 -1
  188. package/static/app/assets/brain-Ci1CkWjM.js +1 -0
  189. package/static/app/assets/{button-2j2Ijzgq.js → button-COwyqfHM.js} +1 -1
  190. package/static/app/assets/circle-check-DfInj-qD.js +1 -0
  191. package/static/app/assets/{circle-pause-BEFeWpVW.js → circle-pause-DEM4A1Y5.js} +1 -1
  192. package/static/app/assets/{circle-play-ujXMcHxl.js → circle-play-C9djDuLd.js} +1 -1
  193. package/static/app/assets/{cpu-k4awryFq.js → cpu-DFdo1gw-.js} +1 -1
  194. package/static/app/assets/{download-DFbLJ_ig.js → download-SnJL6oqk.js} +1 -1
  195. package/static/app/assets/{folder-open-7y_b6xkM.js → folder-open-CqZeDkjE.js} +1 -1
  196. package/static/app/assets/{hard-drive-Bidh02Kr.js → hard-drive-j1jJXYYf.js} +1 -1
  197. package/static/app/assets/{index-DwDl9-8Y.css → index-BLPb5lmE.css} +1 -1
  198. package/static/app/assets/index-_u5iUHDr.js +10 -0
  199. package/static/app/assets/input-B0lPdRQZ.js +1 -0
  200. package/static/app/assets/link-2-CoFbooHS.js +1 -0
  201. package/static/app/assets/{permissionCopy-Bpb83Hx9.js → permissionCopy-BsyLxtao.js} +1 -1
  202. package/static/app/assets/primitives-DEbN-d6p.js +1 -0
  203. package/static/app/assets/search-BybIWPNd.js +1 -0
  204. package/static/app/assets/{share-2-BH1M-WNi.js → share-2-CVtZ_ewX.js} +1 -1
  205. package/static/app/assets/{shield-alert-BlKdBXcG.js → shield-alert-CBi2GNWM.js} +1 -1
  206. package/static/app/assets/{textarea-CCWbUfFB.js → textarea-DNMpB5ih.js} +1 -1
  207. package/static/app/assets/{useFocusTrap-YdHQ7pJ1.js → useFocusTrap-C83t3GXF.js} +1 -1
  208. package/static/app/assets/useMutation-DtbJDoyz.js +1 -0
  209. package/static/app/assets/{useQuery-CXQiwbVT.js → useQuery-Dcp1OChy.js} +1 -1
  210. package/static/app/assets/utils-BlZr7Pd4.js +4 -0
  211. package/static/app/assets/workspace-jJY4RuAV.js +1 -0
  212. package/static/app/index.html +4 -4
  213. package/static/sw.js +1 -1
  214. package/lattice_brain/graph/_kg_common.py +0 -1331
  215. package/lattice_brain/graph/discovery_index.py +0 -1141
  216. package/lattice_brain/graph/retrieval.py +0 -1120
  217. package/lattice_brain/graph/retrieval_vector.py +0 -1293
  218. package/lattice_brain/ingestion.py +0 -1525
  219. package/lattice_brain/multimodal.py +0 -1258
  220. package/latticeai/core/agent.py +0 -1465
  221. package/latticeai/core/embedding_providers.py +0 -1196
  222. package/latticeai/core/file_generation.py +0 -1047
  223. package/latticeai/integrations/telegram_bot.py +0 -1390
  224. package/latticeai/models/router.py +0 -1007
  225. package/latticeai/runtime/build_phases.py +0 -1450
  226. package/latticeai/services/brain_intelligence.py +0 -1083
  227. package/latticeai/services/memory_service.py +0 -1177
  228. package/latticeai/services/model_runtime.py +0 -1281
  229. package/latticeai/setup/wizard.py +0 -1310
  230. package/static/app/assets/Act-AWf0SAKp.js +0 -1
  231. package/static/app/assets/AdminConsole-D0u8Tiyj.js +0 -1
  232. package/static/app/assets/BrainHome-Ts7G_Ila.js +0 -2
  233. package/static/app/assets/BrainSignals-jMYgQ2Ar.js +0 -1
  234. package/static/app/assets/Capture-CqOSzyPr.js +0 -1
  235. package/static/app/assets/CommandPalette-DC0Bzh-I.js +0 -1
  236. package/static/app/assets/Library-CX-bbhmK.js +0 -1
  237. package/static/app/assets/ProductFlow-BHA2cfKI.js +0 -1
  238. package/static/app/assets/ReviewCard-BUhCKRNM.js +0 -3
  239. package/static/app/assets/System-Bu2t5hn1.js +0 -1
  240. package/static/app/assets/arrow-left-Dzwa5zRb.js +0 -1
  241. package/static/app/assets/brain-DJMoqrwx.js +0 -1
  242. package/static/app/assets/index-BpYkzcVm.js +0 -10
  243. package/static/app/assets/input-DSlJJxRs.js +0 -1
  244. package/static/app/assets/primitives-BCx6TvfG.js +0 -1
  245. package/static/app/assets/search-Cgy8cCFJ.js +0 -1
  246. package/static/app/assets/utils-zqPZJxdx.js +0 -4
  247. package/static/app/assets/workspace-DXTihhfU.js +0 -1
@@ -0,0 +1,154 @@
1
+ """Recover the intended file payload from an arbitrary model reply.
2
+
3
+ Step 2 of the pipeline described in the package docstring: strip reasoning
4
+ blocks and conversational framing, pick the best fenced block, and slice known
5
+ document boundaries. Pure string work — nothing here decides whether the result
6
+ is *good*, only what the model most plausibly meant to hand over.
7
+ """
8
+
9
+ from __future__ import annotations
10
+
11
+ import json
12
+ import re
13
+ from typing import Dict, List, Optional, Tuple
14
+
15
+ from latticeai.core.quiet import quiet
16
+
17
+ _THINK_BLOCK_RE = re.compile(
18
+ r"<(think|thinking|reasoning|reflection)>.*?</\1>",
19
+ re.DOTALL | re.IGNORECASE,
20
+ )
21
+ # Unclosed think block (model hit the token limit mid-reasoning).
22
+ _THINK_OPEN_RE = re.compile(r"<(think|thinking|reasoning)>.*\Z", re.DOTALL | re.IGNORECASE)
23
+
24
+ _FENCE_RE = re.compile(r"```([\w.+-]*)[ \t]*\n(.*?)```", re.DOTALL)
25
+
26
+ # Conversational lines that small models prepend/append around the payload.
27
+ _CHAT_LINE_RE = re.compile(
28
+ r"^\s*("
29
+ r"(sure|of course|certainly|okay|ok|alright|great|absolutely)\b[^\n]*"
30
+ r"|here('s| is| are)\b[^\n]*"
31
+ r"|i('ve| have) (created|written|generated|made)\b[^\n]*"
32
+ r"|(below|following) is\b[^\n]*"
33
+ r"|let me know\b[^\n]*"
34
+ r"|hope (this|that) helps[^\n]*"
35
+ r"|feel free\b[^\n]*"
36
+ r"|물론(입니다|이죠|이에요)?[!., ]*[^\n]*"
37
+ r"|네[,!. ][^\n]*"
38
+ r"|알겠습니다[^\n]*"
39
+ r"|다음은[^\n]*(입니다|합니다)[:.]?[^\n]*"
40
+ r"|아래는?[^\n]*(입니다|내용)[^\n]*"
41
+ r"|(요청하신|원하시는)[^\n]*(입니다|만들었습니다|작성했습니다)[^\n]*"
42
+ r"|(파일|내용|코드)[을를]?\s*(생성|작성|만들)[^\n]*"
43
+ r"|도움이 (필요하|되)[^\n]*"
44
+ r"|추가로[^\n]*(말씀|요청)[^\n]*"
45
+ r")\s*$",
46
+ re.IGNORECASE,
47
+ )
48
+
49
+
50
+ # Language tags that identify a fenced block as the payload for an extension.
51
+ _EXT_FENCE_LANGS: Dict[str, Tuple[str, ...]] = {
52
+ ".html": ("html", "htm", "xhtml"),
53
+ ".htm": ("html", "htm", "xhtml"),
54
+ ".css": ("css",),
55
+ ".js": ("js", "javascript"),
56
+ ".jsx": ("jsx", "javascript"),
57
+ ".ts": ("ts", "typescript"),
58
+ ".tsx": ("tsx", "typescript"),
59
+ ".py": ("py", "python"),
60
+ ".json": ("json",),
61
+ ".yaml": ("yaml", "yml"),
62
+ ".yml": ("yaml", "yml"),
63
+ ".toml": ("toml",),
64
+ ".md": ("md", "markdown"),
65
+ ".markdown": ("md", "markdown"),
66
+ ".sql": ("sql",),
67
+ ".sh": ("sh", "bash", "shell", "zsh"),
68
+ ".xml": ("xml", "svg"),
69
+ ".csv": ("csv",),
70
+ ".txt": ("txt", "text", "plaintext"),
71
+ ".vue": ("vue", "html"),
72
+ ".svelte": ("svelte", "html"),
73
+ }
74
+
75
+
76
+ def _ext(path: str) -> str:
77
+ dot = path.rfind(".")
78
+ return path[dot:].lower() if dot >= 0 else ""
79
+
80
+
81
+ def _strip_chat_lines(text: str) -> str:
82
+ """Drop leading/trailing conversational lines around the payload."""
83
+ lines = text.split("\n")
84
+ start, end = 0, len(lines)
85
+ while start < end and (not lines[start].strip() or _CHAT_LINE_RE.match(lines[start])):
86
+ start += 1
87
+ while end > start and (not lines[end - 1].strip() or _CHAT_LINE_RE.match(lines[end - 1])):
88
+ end -= 1
89
+ stripped = "\n".join(lines[start:end]).strip()
90
+ return stripped if stripped else text.strip()
91
+
92
+
93
+ def extract_file_content(raw: str, target_path: str) -> str:
94
+ """Recover the intended file payload from an arbitrary model reply."""
95
+ text = (raw or "").strip()
96
+ if not text:
97
+ return ""
98
+ text = _THINK_BLOCK_RE.sub("", text)
99
+ text = _THINK_OPEN_RE.sub("", text).strip()
100
+
101
+ ext = _ext(target_path)
102
+ fences = _FENCE_RE.findall(text + ("\n```" if text.count("```") % 2 else ""))
103
+ if fences:
104
+ wanted = _EXT_FENCE_LANGS.get(ext, ())
105
+ matching = [body for lang, body in fences if lang.lower() in wanted]
106
+ candidates = matching if matching else [body for _, body in fences]
107
+ # The payload is the largest block; short blocks are usually usage
108
+ # snippets ("run it with: python app.py").
109
+ content = max(candidates, key=len).strip()
110
+ else:
111
+ content = _strip_chat_lines(text)
112
+
113
+ if ext in (".html", ".htm"):
114
+ content = _slice_html_document(content)
115
+ elif ext == ".json":
116
+ sliced = _slice_json_document(content)
117
+ if sliced is not None:
118
+ content = sliced
119
+ return content.strip()
120
+
121
+
122
+ def _slice_html_document(content: str) -> str:
123
+ """Cut a complete HTML document out of surrounding prose when present."""
124
+ lower = content.lower()
125
+ start = lower.find("<!doctype")
126
+ if start < 0:
127
+ start = lower.find("<html")
128
+ if start > 0:
129
+ content = content[start:]
130
+ lower = lower[start:]
131
+ end = lower.rfind("</html>")
132
+ if end >= 0:
133
+ content = content[: end + len("</html>")]
134
+ return content
135
+
136
+
137
+ def _slice_json_document(content: str) -> Optional[str]:
138
+ """Return the largest parseable JSON value inside ``content``, if any."""
139
+ candidates: List[str] = [content]
140
+ for opener, closer in (("{", "}"), ("[", "]")):
141
+ start = content.find(opener)
142
+ end = content.rfind(closer)
143
+ if start >= 0 and end > start:
144
+ candidates.append(content[start : end + 1])
145
+ best: Optional[str] = None
146
+ for candidate in candidates:
147
+ try:
148
+ json.loads(candidate)
149
+ except (ValueError, TypeError):
150
+ quiet()
151
+ continue
152
+ if best is None or len(candidate) > len(best):
153
+ best = candidate
154
+ return best
@@ -0,0 +1,235 @@
1
+ """What the user asked for, when they never said a filename.
2
+
3
+ Two deliberately narrow, fully deterministic inferences (weak local models
4
+ never see either decision): a single filename for "html 파일 만들어줘", and a
5
+ multi-file project manifest for "todo 앱 html+css+js로 만들어줘". Both require
6
+ a creation verb and an explicit type keyword, so anything less specific keeps
7
+ flowing to the paths that handled it before.
8
+ """
9
+
10
+ from __future__ import annotations
11
+
12
+ import re
13
+ from typing import Any, Dict, List, Optional, Tuple
14
+
15
+ _CREATE_VERB_RE = re.compile(
16
+ r"(만들|생성|작성|써\s*줘|저장|create|make|write|generate|build|save)",
17
+ re.IGNORECASE,
18
+ )
19
+
20
+
21
+ # Explicit type keyword → default filename. Ordered: first match wins.
22
+ _TYPE_KEYWORDS: Tuple[Tuple[str, str], ...] = (
23
+ (r"\bhtml\b|웹\s*페이지|웹페이지|홈페이지|landing\s*page|web\s*page", "generated_page.html"),
24
+ (r"\bcss\b|스타일\s*시트", "styles.css"),
25
+ (r"\bjavascript\b|\bjs\b\s*(파일|file)|자바스크립트", "script.js"),
26
+ (r"\bpython\b|파이썬", "script.py"),
27
+ (r"\bjson\b", "data.json"),
28
+ (r"\bcsv\b", "data.csv"),
29
+ (r"\byaml\b|\byml\b", "config.yaml"),
30
+ (r"\bxml\b", "data.xml"),
31
+ (r"\bsql\b", "query.sql"),
32
+ (r"마크다운|\bmarkdown\b|\bmd\b\s*(파일|file)", "notes.md"),
33
+ (r"텍스트\s*파일|\btext\s*file\b|\btxt\b", "notes.txt"),
34
+ )
35
+
36
+
37
+ def infer_file_target(message: str) -> Optional[str]:
38
+ """Infer a filename for creation requests that name a type but no path.
39
+
40
+ "html 파일 만들어줘" previously fell through to the agent JSON loop, which
41
+ small models fail at. Inference keeps such requests on the deterministic
42
+ direct-write path. Deliberately narrow: requires a creation verb and an
43
+ explicit file-type keyword — report/document prose requests keep flowing
44
+ to the document generator.
45
+ """
46
+ text = (message or "").strip()
47
+ if not text or not _CREATE_VERB_RE.search(text):
48
+ return None
49
+ lower = text.lower()
50
+ for pattern, filename in _TYPE_KEYWORDS:
51
+ if re.search(pattern, lower):
52
+ return filename
53
+ return None
54
+
55
+
56
+ # ``\b`` fails against Korean particles ("js로") because Hangul is ``\w`` —
57
+ # use ASCII lookarounds so type keywords match with or without a particle.
58
+ _HTML_HINT_RE = re.compile(
59
+ r"(?<![a-z0-9])html(?![a-z0-9])"
60
+ r"|웹\s*페이지|웹페이지|홈페이지|웹\s*사이트|웹사이트|website|web\s*page|landing\s*page",
61
+ )
62
+ _CSS_HINT_RE = re.compile(r"(?<![a-z0-9])css(?![a-z0-9])|스타일\s*시트|stylesheet")
63
+ _JS_HINT_RE = re.compile(
64
+ r"(?<![a-z0-9])js(?![a-z0-9])|javascript|자바스크립트|자바\s*스크립트"
65
+ )
66
+ # An explicit filename means the user is managing paths — keep the
67
+ # deterministic single-file flow untouched.
68
+ _EXPLICIT_FILENAME_RE = re.compile(
69
+ r"[\w-]+\.(?:html?|css|js|jsx|ts|tsx|py|json|md|txt|csv|vue|svelte)\b",
70
+ re.IGNORECASE,
71
+ )
72
+ _PROJECT_NAME_RE = re.compile(r"([A-Za-z][A-Za-z0-9_-]{1,30})\s*(?:앱|app\b)", re.IGNORECASE)
73
+ # React/Vite intent: the react keyword is specific enough on its own.
74
+ _REACT_HINT_RE = re.compile(r"(?<![a-z0-9])react(?![a-z0-9])|리액트")
75
+ _VITE_HINT_RE = re.compile(r"(?<![a-z0-9])vite(?![a-z0-9])")
76
+ # Python package intent: language + package word, both required.
77
+ _PYTHON_HINT_RE = re.compile(r"(?<![a-z0-9])python(?![a-z0-9])|파이썬")
78
+ _PACKAGE_HINT_RE = re.compile(r"패키지|(?<![a-z0-9])package(?![a-z0-9])")
79
+ _PKG_NAME_RE = re.compile(
80
+ r"([A-Za-z][A-Za-z0-9_-]{1,30})\s*(?:패키지|package\b)", re.IGNORECASE
81
+ )
82
+
83
+
84
+ def _react_manifest(text: str) -> Dict[str, Any]:
85
+ """Vite + React starter manifest (review Wave 4: manifest 확장)."""
86
+ name_match = _PROJECT_NAME_RE.search(text)
87
+ name = f"{name_match.group(1).lower()}-app" if name_match else "react-app"
88
+ return {
89
+ "name": name,
90
+ "kind": "react",
91
+ "files": [
92
+ {
93
+ "path": "package.json",
94
+ "brief": (
95
+ f'Vite React app manifest: strictly valid JSON with "name": "{name}", '
96
+ '"private": true, "type": "module", "scripts" {"dev": "vite", '
97
+ '"build": "vite build", "preview": "vite preview"}, "dependencies" '
98
+ 'with react and react-dom (^18), and "devDependencies" with vite '
99
+ "and @vitejs/plugin-react."
100
+ ),
101
+ },
102
+ {
103
+ "path": "index.html",
104
+ "brief": (
105
+ "The Vite entry HTML: <div id=\"root\"></div> in <body> and "
106
+ "<script type=\"module\" src=\"/src/main.jsx\"></script> just "
107
+ "before </body>. No inline styles or scripts."
108
+ ),
109
+ },
110
+ {
111
+ "path": "src/main.jsx",
112
+ "brief": (
113
+ "React entry: createRoot from react-dom/client rendering <App /> "
114
+ "into #root; imports ./App.jsx and ./App.css."
115
+ ),
116
+ },
117
+ {
118
+ "path": "src/App.jsx",
119
+ "brief": (
120
+ "The main App component implementing the user's request as one "
121
+ "self-contained React component (hooks allowed, no extra deps)."
122
+ ),
123
+ },
124
+ {
125
+ "path": "src/App.css",
126
+ "brief": "All visual styles for the App component.",
127
+ },
128
+ ],
129
+ }
130
+
131
+
132
+ def _python_package_manifest(text: str) -> Dict[str, Any]:
133
+ """Multi-file Python package manifest (review Wave 4: manifest 확장)."""
134
+ name_match = _PKG_NAME_RE.search(text)
135
+ raw_name = name_match.group(1).lower() if name_match else "my_package"
136
+ module = re.sub(r"[^a-z0-9_]", "_", raw_name)
137
+ if not re.match(r"[a-z_]", module):
138
+ module = f"pkg_{module}"
139
+ return {
140
+ "name": module,
141
+ "kind": "python",
142
+ "files": [
143
+ {
144
+ "path": f"{module}/__init__.py",
145
+ "brief": (
146
+ f"Package init for {module}: import and re-export the public "
147
+ "API from .core with an explicit __all__."
148
+ ),
149
+ },
150
+ {
151
+ "path": f"{module}/core.py",
152
+ "brief": (
153
+ "Implement the user's request as clean, documented functions/"
154
+ "classes with type hints. Standard library only."
155
+ ),
156
+ },
157
+ {
158
+ "path": f"{module}/cli.py",
159
+ "brief": (
160
+ "argparse CLI wrapping the core API: a main() function and an "
161
+ 'if __name__ == "__main__": main() guard.'
162
+ ),
163
+ },
164
+ {
165
+ "path": "README.md",
166
+ "brief": (
167
+ f"Usage documentation for the {module} package: install, import "
168
+ "example, and CLI example."
169
+ ),
170
+ },
171
+ ],
172
+ }
173
+
174
+
175
+ def infer_project_manifest(message: str) -> Optional[Dict[str, Any]]:
176
+ """Infer a multi-file project manifest from a creation request.
177
+
178
+ "todo 앱 html+css+js로 만들어줘" should yield real linked files, not one
179
+ inlined page. Deliberately narrow and deterministic (weak local models
180
+ never see this decision): requires a creation verb, a recognized project
181
+ intent (web page + css/js, React/Vite app, or Python package), and no
182
+ explicit filename. Single-type requests return ``None`` so the existing
183
+ single-file flow is completely unchanged.
184
+ """
185
+ text = (message or "").strip()
186
+ if not text or not _CREATE_VERB_RE.search(text):
187
+ return None
188
+ if _EXPLICIT_FILENAME_RE.search(text):
189
+ return None
190
+ lower = text.lower()
191
+
192
+ # Most-specific first: React (its own structure), then Python package,
193
+ # then the classic html+css/js web bundle.
194
+ if _REACT_HINT_RE.search(lower) or _VITE_HINT_RE.search(lower):
195
+ return _react_manifest(text)
196
+ if _PYTHON_HINT_RE.search(lower) and _PACKAGE_HINT_RE.search(lower):
197
+ return _python_package_manifest(text)
198
+
199
+ wants_html = bool(_HTML_HINT_RE.search(lower))
200
+ wants_css = bool(_CSS_HINT_RE.search(lower))
201
+ wants_js = bool(_JS_HINT_RE.search(lower))
202
+ if not wants_html or not (wants_css or wants_js):
203
+ return None
204
+
205
+ name_match = _PROJECT_NAME_RE.search(text)
206
+ name = f"{name_match.group(1).lower()}-app" if name_match else "web-project"
207
+
208
+ files: List[Dict[str, str]] = []
209
+ html_refs: List[str] = []
210
+ if wants_css:
211
+ html_refs.append('<link rel="stylesheet" href="style.css"> in <head>')
212
+ if wants_js:
213
+ html_refs.append('<script src="app.js"></script> just before </body>')
214
+ files.append({
215
+ "path": "index.html",
216
+ "brief": (
217
+ "The main HTML page of the project. Reference the sibling files: "
218
+ + " and ".join(html_refs)
219
+ + ". Do not inline styles or behavior scripts."
220
+ ),
221
+ })
222
+ if wants_css:
223
+ files.append({
224
+ "path": "style.css",
225
+ "brief": "All visual styles for index.html (layout, colors, typography).",
226
+ })
227
+ if wants_js:
228
+ files.append({
229
+ "path": "app.js",
230
+ "brief": (
231
+ "All page behavior for index.html as plain browser JavaScript "
232
+ "(no build step, no imports of missing files)."
233
+ ),
234
+ })
235
+ return {"name": name, "kind": "web", "files": files}
@@ -0,0 +1,152 @@
1
+ """Prompt → extract → validate → retry → repair, in that order.
2
+
3
+ The pipeline's driver. ``generate_file_content`` is the only place the model
4
+ is actually called; every other module here is pure. The salvage score decides
5
+ which rejected candidate repair gets to work from — closest to a file, not
6
+ longest.
7
+ """
8
+
9
+ from __future__ import annotations
10
+
11
+ import ast
12
+ from typing import Any, Awaitable, Callable, Dict, List, Optional, Tuple
13
+
14
+ from .extraction import _ext, _slice_json_document, extract_file_content
15
+ from .prompting import build_file_generation_context
16
+ from .repair import repair_file_content
17
+ from .validation import (
18
+ _BRACED_CODE_EXTENSIONS,
19
+ _COMPONENT_EXTENSIONS,
20
+ _check_balanced_delimiters,
21
+ _check_component_blocks,
22
+ looks_like_refusal,
23
+ validate_file_content,
24
+ )
25
+
26
+
27
+ async def generate_file_content(
28
+ generate: Callable[[str], Awaitable[Any]],
29
+ *,
30
+ target_path: str,
31
+ user_request: str,
32
+ max_attempts: int = 2,
33
+ bundle_files: Optional[List[str]] = None,
34
+ ) -> Tuple[str, Dict[str, Any]]:
35
+ """Generate validated file content with any LLM.
36
+
37
+ ``generate`` is an async callable ``context -> raw model text``. Runs up
38
+ to ``max_attempts`` model calls (each retry carrying corrective feedback),
39
+ then falls back to deterministic repair, so the returned content is
40
+ always non-empty and structurally valid for the target type.
41
+
42
+ One extra call beyond ``max_attempts`` is spent — at most once per
43
+ request — when the model has returned a byte-identical rejected reply.
44
+ That is the one case where the ordinary retry is known to be dead on
45
+ arrival: the corrective feedback did not change the reply, so the budget
46
+ is better spent on a prompt that names the repetition than on a third
47
+ identical round trip. Small local models hit this constantly; large ones
48
+ never do, so the extra call is not charged to models that do not need it.
49
+ """
50
+ attempts: List[Dict[str, Any]] = []
51
+ feedback: Optional[str] = None
52
+ best_candidate = ""
53
+ best_score = (-1, -1)
54
+ seen: set[str] = set()
55
+ escalations_left = 1
56
+ attempt = 0
57
+ budget = max_attempts
58
+ while attempt < budget:
59
+ attempt += 1
60
+ context = build_file_generation_context(
61
+ target_path, user_request, feedback=feedback, bundle_files=bundle_files,
62
+ )
63
+ try:
64
+ raw = await generate(context)
65
+ except Exception as exc: # model backend hiccup — repair still delivers
66
+ attempts.append({"attempt": attempt, "valid": False, "reason": f"generation error: {exc}"})
67
+ feedback = "the model call failed"
68
+ continue
69
+ candidate = extract_file_content(str(raw or ""), target_path)
70
+ ok, reason = validate_file_content(candidate, target_path)
71
+ record: Dict[str, Any] = {"attempt": attempt, "valid": ok, "reason": reason}
72
+ if ok:
73
+ attempts.append(record)
74
+ return candidate, {"attempts": attempts, "repaired": False}
75
+
76
+ # A small model handed the same corrective feedback often replays the
77
+ # same reply verbatim. Saying "you sent this before" is the only signal
78
+ # left that has any chance of moving it, and it makes the wasted retry
79
+ # visible in the trace instead of looking like two genuine tries.
80
+ fingerprint = candidate.strip()
81
+ repeated = fingerprint in seen and bool(fingerprint)
82
+ record["repeated"] = repeated
83
+ seen.add(fingerprint)
84
+ if repeated and escalations_left and attempt >= budget:
85
+ # The retry budget is exhausted and the last thing it bought was a
86
+ # duplicate. Buy one more, but only with a prompt that says so.
87
+ escalations_left -= 1
88
+ budget += 1
89
+ record["escalated"] = True
90
+ attempts.append(record)
91
+
92
+ # Keep the candidate that is *closest to a file*, not the longest one.
93
+ # Longest-wins handed repair a 900-character apology in preference to a
94
+ # 300-character HTML document that only needed its </html> closing —
95
+ # and repair can finish the document but can only bury the apology.
96
+ score = _salvage_score(candidate, target_path)
97
+ if score > best_score:
98
+ best_score, best_candidate = score, candidate
99
+
100
+ feedback = (
101
+ f"{reason}. You already sent exactly this reply and it was rejected "
102
+ "for the same reason — do not repeat it. Output the file itself, "
103
+ "starting at its first character."
104
+ if repeated else reason
105
+ )
106
+ repaired = repair_file_content(best_candidate, target_path, user_request)
107
+ return repaired, {"attempts": attempts, "repaired": True}
108
+
109
+
110
+ def _salvage_score(candidate: str, target_path: str) -> Tuple[int, int]:
111
+ """How useful an invalid candidate is as raw material for repair.
112
+
113
+ ``(tier, length)`` — tier first, so a short real document always beats a
114
+ long non-document; length breaks ties within a tier.
115
+
116
+ Tier 2 something of the right shape that repair can finish (an HTML
117
+ document missing its close tag, parseable-ish JSON, Python that
118
+ at least tokenises).
119
+ Tier 1 ordinary text: no structure, but the words may be the content.
120
+ Tier 0 a refusal — repair should prefer literally anything else, because
121
+ an apology written into the file is worse than an empty stub.
122
+ """
123
+ text = candidate.strip()
124
+ if not text:
125
+ return (0, 0)
126
+ if looks_like_refusal(text):
127
+ return (0, len(text))
128
+
129
+ ext = _ext(target_path)
130
+ lower = text.lower()
131
+ if ext in (".html", ".htm"):
132
+ if lower.startswith("<!doctype") or lower.startswith("<html"):
133
+ return (2, len(text))
134
+ elif ext == ".json":
135
+ if _slice_json_document(text) is not None:
136
+ return (2, len(text))
137
+ elif ext == ".py":
138
+ try:
139
+ ast.parse(text)
140
+ except SyntaxError:
141
+ pass
142
+ else:
143
+ return (2, len(text))
144
+ elif ext in _BRACED_CODE_EXTENSIONS:
145
+ if _check_balanced_delimiters(text)[0]:
146
+ return (2, len(text))
147
+ elif ext in _COMPONENT_EXTENSIONS:
148
+ if _check_component_blocks(text)[0]:
149
+ return (2, len(text))
150
+ elif ext == ".css" and "{" in text and "}" in text:
151
+ return (2, len(text))
152
+ return (1, len(text))
@@ -0,0 +1,117 @@
1
+ """Extension-aware generation instructions — step 1 of the pipeline.
2
+
3
+ Small models ignore abstract rules but reliably imitate concrete anchors, so
4
+ the prompt pins the exact first line of the expected output and the structural
5
+ rule for the target type. Multi-file bundles swap in the rule that links
6
+ sibling files instead of inlining everything.
7
+ """
8
+
9
+ from __future__ import annotations
10
+
11
+ from typing import Dict, List, Optional
12
+
13
+ from .extraction import _ext
14
+
15
+ _FIRST_LINE_HINTS: Dict[str, str] = {
16
+ ".html": "<!DOCTYPE html>",
17
+ ".htm": "<!DOCTYPE html>",
18
+ ".py": "# (python code — imports or code on the first line)",
19
+ ".sh": "#!/bin/sh",
20
+ ".json": "{",
21
+ ".xml": "<?xml version=\"1.0\" encoding=\"UTF-8\"?>",
22
+ }
23
+
24
+
25
+ _TYPE_RULES: Dict[str, str] = {
26
+ ".html": (
27
+ "Produce ONE complete standalone HTML5 document: <!DOCTYPE html>, <html>, "
28
+ "<head> with <meta charset=\"utf-8\"> and a <title>, inline <style> for CSS, "
29
+ "and a closed </html> tag. Do not reference external files."
30
+ ),
31
+ ".htm": (
32
+ "Produce ONE complete standalone HTML5 document ending with </html>."
33
+ ),
34
+ ".json": "Produce strictly valid JSON (double quotes, no comments, no trailing commas).",
35
+ ".css": "Produce valid CSS rules only.",
36
+ ".md": "Produce well-structured Markdown with headings.",
37
+ ".markdown": "Produce well-structured Markdown with headings.",
38
+ ".csv": "Produce CSV with a header row; comma-separated, one record per line.",
39
+ ".py": "Produce complete runnable Python source code.",
40
+ ".js": "Produce complete valid JavaScript source code.",
41
+ ".jsx": "Produce one complete React component file in JSX.",
42
+ ".ts": "Produce complete valid TypeScript source code.",
43
+ ".tsx": "Produce one complete React component file in TSX (TypeScript).",
44
+ ".vue": "Produce ONE complete Vue single-file component with closed <template>/<script>/<style> blocks.",
45
+ ".svelte": "Produce ONE complete Svelte component; every <script>/<style> block must be closed.",
46
+ }
47
+
48
+
49
+ # Multi-file bundles override the standalone-HTML rule: the page must link
50
+ # its sibling files instead of inlining everything.
51
+ _BUNDLE_HTML_RULE = (
52
+ "Produce ONE complete HTML5 document: <!DOCTYPE html>, <html>, <head> with "
53
+ "<meta charset=\"utf-8\"> and a <title>, and a closed </html> tag. "
54
+ "This page is part of a multi-file project: link the project stylesheet(s) "
55
+ "with <link rel=\"stylesheet\" href=\"...\"> and load the project script(s) "
56
+ "with <script src=\"...\"></script> just before </body>. Reference ONLY the "
57
+ "project files listed below — no other external files, no inline <style> "
58
+ "blocks, no inline behavior scripts."
59
+ )
60
+
61
+
62
+ # Vite/React bundles need a module entry point, not classic script tags.
63
+ _BUNDLE_HTML_MODULE_RULE = (
64
+ "Produce ONE complete HTML5 document: <!DOCTYPE html>, <html>, <head> with "
65
+ "<meta charset=\"utf-8\"> and a <title>, and a closed </html> tag. "
66
+ "This page is the Vite entry of a React project: the <body> must contain "
67
+ "<div id=\"root\"></div> and load the app with "
68
+ "<script type=\"module\" src=\"/src/main.jsx\"></script> just before "
69
+ "</body>. No inline <style> blocks, no other scripts, no external files."
70
+ )
71
+
72
+
73
+ def _bundle_html_rule(bundle_files: List[str]) -> str:
74
+ """Pick the HTML bundle rule that matches the bundle's technology."""
75
+ if any(str(path).lower().endswith((".jsx", ".tsx")) for path in bundle_files):
76
+ return _BUNDLE_HTML_MODULE_RULE
77
+ return _BUNDLE_HTML_RULE
78
+
79
+
80
+ def build_file_generation_context(
81
+ target_path: str,
82
+ user_request: str,
83
+ feedback: Optional[str] = None,
84
+ bundle_files: Optional[List[str]] = None,
85
+ ) -> str:
86
+ """Strict, extension-aware generation instructions.
87
+
88
+ Small models ignore abstract rules but reliably imitate concrete anchors,
89
+ so the prompt pins the exact first line of the expected output.
90
+ """
91
+ ext = _ext(target_path)
92
+ parts = [
93
+ "You are a file content generator. Your entire reply is saved verbatim "
94
+ f"as the file `{target_path}` — it is NOT shown in a chat.",
95
+ "Rules:",
96
+ "- Output ONLY the raw file content.",
97
+ "- No Markdown code fences (```), no explanations, no greetings, "
98
+ "no text before or after the content.",
99
+ ]
100
+ type_rule = _TYPE_RULES.get(ext)
101
+ if bundle_files and ext in (".html", ".htm"):
102
+ type_rule = _bundle_html_rule(bundle_files)
103
+ if type_rule:
104
+ parts.append(f"- {type_rule}")
105
+ if bundle_files:
106
+ listed = ", ".join(bundle_files)
107
+ parts.append(f"- Project files in this bundle: {listed}")
108
+ first_line = _FIRST_LINE_HINTS.get(ext)
109
+ if first_line:
110
+ parts.append(f"- The very first line of your reply must be: {first_line}")
111
+ if feedback:
112
+ parts.append(
113
+ "Your previous attempt was rejected: "
114
+ f"{feedback}. Fix that and output only the corrected file content."
115
+ )
116
+ parts.append(f"\nUser request: {user_request}")
117
+ return "\n".join(parts)