femo-plugin 1.0.1 → 1.0.2
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.
- package/FEMO_logo.svg +55 -0
- package/FEMO_logo_slogan.svg +29 -0
- package/_tmp/fallback/driver2.py +176 -0
- package/_tmp/fallback/events.jsonl +108 -0
- package/_tmp/fallback/gateway_ui.py +325 -0
- package/_tmp/fallback/outbox/j1553_human_[/347/234/213/347/211/214]_1.done +1 -0
- package/_tmp/fallback/outbox/r1.done +1 -0
- package/_tmp/fallback/outbox/r2.done +1 -0
- package/_tmp/fallback/outbox/r3.done +1 -0
- package/_tmp/fallback/outbox/r4.done +1 -0
- package/_tmp/fallback/outbox/r5.done +1 -0
- package/femoExamples//350/260/201/346/230/257/345/215/247/345/272/225//350/260/201/346/230/257/345/215/247/345/272/225.femo +3 -2
- package/hostAdapter/dshAdapter/client/client-ui/composer.tsx +112 -9
- package/hostAdapter/dshAdapter/client/client-ui/styles.ts +17 -0
- package/hostAdapter/dshAdapter/client/fa-icons.tsx +3 -0
- package/hostAdapter/dshAdapter/host/engine-events.ts +4 -1
- package/hostAdapter/dshAdapter/host/index.ts +5 -0
- package/hostAdapter/dshAdapter/host/preset-install.ts +59 -0
- package/hostAdapter/dshAdapter/host/projection-input.ts +14 -6
- package/hostAdapter/dshAdapter/lib/client.js +124 -5
- package/hostAdapter/dshAdapter/lib/client.js.map +3 -3
- package/hostAdapter/dshAdapter/lib/index.js +58 -18
- package/hostAdapter/dshAdapter/lib/index.js.map +4 -4
- package/hostAdapter/dshAdapter/package.json +1 -5
- package/hostAdapter/dshAdapter/preset/agent.cordis.yml +220 -0
- package/hostAdapter/dshAdapter/preset/preset.yml +3 -0
- package/hostAdapter/zcodeAdapter/.tmp/femo/host-history/gateway.port +1 -0
- package/hostAdapter/zcodeAdapter/.tmp/femo/host-history/proj-mirror/femo-main-a-_40_52a9_624b.jsonl +3 -0
- package/hostAdapter/zcodeAdapter/.tmp/femo/host-history/proj-mirror/femo-main-a-_40_6211.jsonl +10 -0
- package/hostAdapter/zcodeAdapter/.tmp/femo/host-history/proj-mirror/femo-main-god.jsonl +11 -0
- package/hostAdapter/zcodeAdapter/.tmp/femo/host-history/proj-mirror/femo-main-stage.jsonl +11 -0
- package/hostAdapter/zcodeAdapter/.zcode-plugin/plugin.json +23 -0
- package/hostAdapter/zcodeAdapter/README.md +61 -0
- package/hostAdapter/zcodeAdapter/commands/femo.md +9 -0
- package/hostAdapter/zcodeAdapter/femo-zcode-dist/.zcode-plugin/plugin.json +23 -0
- package/hostAdapter/zcodeAdapter/femo-zcode-dist/LICENSE +201 -0
- package/hostAdapter/zcodeAdapter/femo-zcode-dist/README.md +635 -0
- package/hostAdapter/zcodeAdapter/femo-zcode-dist/commands/femo.md +9 -0
- package/hostAdapter/zcodeAdapter/femo-zcode-dist/femo2host/__init__.py +4 -0
- package/hostAdapter/zcodeAdapter/femo-zcode-dist/femo2host/femoToolcall/chronica.py +156 -0
- package/hostAdapter/zcodeAdapter/femo-zcode-dist/femo2host/femoToolcall/femo-chat.mjs +69 -0
- package/hostAdapter/zcodeAdapter/femo-zcode-dist/femo2host/femoToolcall/femo_debugger.py +1515 -0
- package/hostAdapter/zcodeAdapter/femo-zcode-dist/femo2host/femo_api.py +437 -0
- package/hostAdapter/zcodeAdapter/femo-zcode-dist/femo2host/femo_gen_api.jsx +77 -0
- package/hostAdapter/zcodeAdapter/femo-zcode-dist/femoBridges/ContextExample.py +555 -0
- package/hostAdapter/zcodeAdapter/femo-zcode-dist/femoBridges/MemoryExample.py +163 -0
- package/hostAdapter/zcodeAdapter/femo-zcode-dist/femoBridges/__init__.py +1 -0
- package/hostAdapter/zcodeAdapter/femo-zcode-dist/femoBridges/getDir/get_dir.py +56 -0
- package/hostAdapter/zcodeAdapter/femo-zcode-dist/femoBridges/getDir//344/275/240/345/217/257/344/273/245/350/256/276/345/256/232/344/273/273/346/204/217/346/226/207/344/273/266/345/244/271/344/270/272/347/224/250/346/210/267/346/225/260/346/215/256/346/226/207/344/273/266/345/244/271.txt +10 -0
- package/hostAdapter/zcodeAdapter/femo-zcode-dist/femoBridges/llmBridge.py +247 -0
- package/hostAdapter/zcodeAdapter/femo-zcode-dist/femoBridges/llmProviders.py +308 -0
- package/hostAdapter/zcodeAdapter/femo-zcode-dist/femoCompiler/FEMO_CLIrenderer.py +198 -0
- package/hostAdapter/zcodeAdapter/femo-zcode-dist/femoCompiler/FEMO_config.py +35 -0
- package/hostAdapter/zcodeAdapter/femo-zcode-dist/femoCompiler/FEMO_errors.py +278 -0
- package/hostAdapter/zcodeAdapter/femo-zcode-dist/femoCompiler/FEMO_interpreter.py +163 -0
- package/hostAdapter/zcodeAdapter/femo-zcode-dist/femoCompiler/FEMO_normalizer.py +396 -0
- package/hostAdapter/zcodeAdapter/femo-zcode-dist/femoCompiler/FEMO_parser.py +2468 -0
- package/hostAdapter/zcodeAdapter/femo-zcode-dist/femoCompiler/FEMO_runtime.py +3630 -0
- package/hostAdapter/zcodeAdapter/femo-zcode-dist/femoCompiler/FEMO_scope_resolver.py +209 -0
- package/hostAdapter/zcodeAdapter/femo-zcode-dist/femoCompiler/__init__.py +1 -0
- package/hostAdapter/zcodeAdapter/femo-zcode-dist/femoCompiler/actor_resolver.py +86 -0
- package/hostAdapter/zcodeAdapter/femo-zcode-dist/femoCompiler/block_collector.py +441 -0
- package/hostAdapter/zcodeAdapter/femo-zcode-dist/femoCompiler/db_utils.py +485 -0
- package/hostAdapter/zcodeAdapter/femo-zcode-dist/femoCompiler/femoAsync.py +401 -0
- package/hostAdapter/zcodeAdapter/femo-zcode-dist/femoCompiler/host_manifest.py +68 -0
- package/hostAdapter/zcodeAdapter/femo-zcode-dist/femoCompiler/job_manager.py +767 -0
- package/hostAdapter/zcodeAdapter/femo-zcode-dist/femoCompiler/protocol.py +126 -0
- package/hostAdapter/zcodeAdapter/femo-zcode-dist/femoCompiler/save_dialog.py +402 -0
- package/hostAdapter/zcodeAdapter/femo-zcode-dist/femoCompiler/task_world.py +544 -0
- package/hostAdapter/zcodeAdapter/femo-zcode-dist/femoCompiler/vars/__init__.py +19 -0
- package/hostAdapter/zcodeAdapter/femo-zcode-dist/femoCompiler/vars/checkpoint.py +113 -0
- package/hostAdapter/zcodeAdapter/femo-zcode-dist/femoCompiler/vars/env.py +686 -0
- package/hostAdapter/zcodeAdapter/femo-zcode-dist/femoCompiler/vars/evaluator.py +567 -0
- package/hostAdapter/zcodeAdapter/femo-zcode-dist/femoCompiler/vars/join.py +303 -0
- package/hostAdapter/zcodeAdapter/femo-zcode-dist/femoCompiler/vars/merge.py +157 -0
- package/hostAdapter/zcodeAdapter/femo-zcode-dist/femoCompiler/vars/model.py +245 -0
- package/hostAdapter/zcodeAdapter/femo-zcode-dist/femoExamples/AI/345/211/252/350/247/206/351/242/221/AI/345/211/252/350/247/206/351/242/221.femo +192 -0
- package/hostAdapter/zcodeAdapter/femo-zcode-dist/femoExamples/AI/350/201/212/345/244/251/345/256/244/group-chat.femo +42 -0
- package/hostAdapter/zcodeAdapter/femo-zcode-dist/femoExamples/AI/350/201/212/345/244/251/345/256/244/random_wait_time.py +10 -0
- package/hostAdapter/zcodeAdapter/femo-zcode-dist/femoExamples/LuxFiat/luxfiat.femo +38 -0
- package/hostAdapter/zcodeAdapter/femo-zcode-dist/femoExamples/LuxFiat/wait.py +29 -0
- package/hostAdapter/zcodeAdapter/femo-zcode-dist/femoExamples/debug/347/245/236/345/231/250/debug/347/245/236/345/231/250.femo +222 -0
- package/hostAdapter/zcodeAdapter/femo-zcode-dist/femoExamples/discussion.femo +70 -0
- package/hostAdapter/zcodeAdapter/femo-zcode-dist/femoExamples/goal-mode.femo +23 -0
- package/hostAdapter/zcodeAdapter/femo-zcode-dist/femoExamples//345/270/270/347/224/250/345/267/245/345/205/267python/wait.py +29 -0
- package/hostAdapter/zcodeAdapter/femo-zcode-dist/femoExamples//347/213/274/344/272/272/346/235/200/werewolf_utils.py +205 -0
- package/hostAdapter/zcodeAdapter/femo-zcode-dist/femoExamples//347/213/274/344/272/272/346/235/200/werewolf_utils2.py +324 -0
- package/hostAdapter/zcodeAdapter/femo-zcode-dist/femoExamples//347/213/274/344/272/272/346/235/200//347/213/274/344/272/272/346/235/200-/350/255/246/351/225/277/347/211/210.femo +544 -0
- package/hostAdapter/zcodeAdapter/femo-zcode-dist/femoExamples//347/213/274/344/272/272/346/235/200//347/213/274/344/272/272/346/235/200.femo +331 -0
- package/hostAdapter/zcodeAdapter/femo-zcode-dist/femoExamples//350/260/201/346/230/257/345/215/247/345/272/225/undercover_utils.py +683 -0
- package/hostAdapter/zcodeAdapter/femo-zcode-dist/femoExamples//350/260/201/346/230/257/345/215/247/345/272/225//350/260/201/346/230/257/345/215/247/345/272/225-/350/217/234/345/270/202/345/234/272/345/261/200.femo +348 -0
- package/hostAdapter/zcodeAdapter/femo-zcode-dist/femoExamples//350/260/201/346/230/257/345/215/247/345/272/225//350/260/201/346/230/257/345/215/247/345/272/225.femo +393 -0
- package/hostAdapter/zcodeAdapter/femo-zcode-dist/hooks/after-tool.mjs +3 -0
- package/hostAdapter/zcodeAdapter/femo-zcode-dist/hooks/hooks.json +16 -0
- package/hostAdapter/zcodeAdapter/femo-zcode-dist/hooks/lib.mjs +59 -0
- package/hostAdapter/zcodeAdapter/femo-zcode-dist/hooks/prompt-state.mjs +3 -0
- package/hostAdapter/zcodeAdapter/femo-zcode-dist/hooks/session-start.mjs +3 -0
- package/hostAdapter/zcodeAdapter/femo-zcode-dist/hooks/stop-intent.mjs +3 -0
- package/hostAdapter/zcodeAdapter/femo-zcode-dist/hostAdapter/dshAdapter/python/dev-bridge-test.py +43 -0
- package/hostAdapter/zcodeAdapter/femo-zcode-dist/hostAdapter/dshAdapter/python/femo_bridge.py +533 -0
- package/hostAdapter/zcodeAdapter/femo-zcode-dist/mcp/bridge-manager.mjs +192 -0
- package/hostAdapter/zcodeAdapter/femo-zcode-dist/mcp/event-router.mjs +258 -0
- package/hostAdapter/zcodeAdapter/femo-zcode-dist/mcp/femo-server.mjs +429 -0
- package/hostAdapter/zcodeAdapter/femo-zcode-dist/mcp/gateway.mjs +163 -0
- package/hostAdapter/zcodeAdapter/femo-zcode-dist/mcp/projection-writer.mjs +294 -0
- package/hostAdapter/zcodeAdapter/femo-zcode-dist/skills/femo/SKILL.md +93 -0
- package/hostAdapter/zcodeAdapter/femo-zcode-dist/tests/bridge-smoke.mjs +26 -0
- package/hostAdapter/zcodeAdapter/femo-zcode-dist/tests/event-router.test.mjs +157 -0
- package/hostAdapter/zcodeAdapter/femo-zcode-dist/tests/gateway-smoke.mjs +107 -0
- package/hostAdapter/zcodeAdapter/femo-zcode-dist/tests/integration-human.mjs +111 -0
- package/hostAdapter/zcodeAdapter/femo-zcode-dist/tests/main-actor.mjs +107 -0
- package/hostAdapter/zcodeAdapter/femo-zcode-dist/tests/mcp-smoke.mjs +130 -0
- package/hostAdapter/zcodeAdapter/femo-zcode-dist/tests/projection-writer.test.mjs +144 -0
- package/hostAdapter/zcodeAdapter/femo-zcode-dist/zcode.host.manifest.json +5 -0
- package/hostAdapter/zcodeAdapter/femo-zcode-dist//346/212/225/345/275/261/347/252/227/351/207/215/350/256/276/350/256/241-/350/256/250/350/256/272/347/250/277.md +113 -0
- package/hostAdapter/zcodeAdapter/femo-zcode-dist//350/257/255/346/263/225/346/226/207/346/241/243.md +1136 -0
- package/hostAdapter/zcodeAdapter/hooks/after-tool.mjs +3 -0
- package/hostAdapter/zcodeAdapter/hooks/hooks.json +16 -0
- package/hostAdapter/zcodeAdapter/hooks/lib.mjs +59 -0
- package/hostAdapter/zcodeAdapter/hooks/prompt-state.mjs +3 -0
- package/hostAdapter/zcodeAdapter/hooks/session-start.mjs +3 -0
- package/hostAdapter/zcodeAdapter/hooks/stop-intent.mjs +3 -0
- package/hostAdapter/zcodeAdapter/mcp/bridge-manager.mjs +192 -0
- package/hostAdapter/zcodeAdapter/mcp/event-router.mjs +258 -0
- package/hostAdapter/zcodeAdapter/mcp/femo-server.mjs +429 -0
- package/hostAdapter/zcodeAdapter/mcp/gateway.mjs +163 -0
- package/hostAdapter/zcodeAdapter/mcp/projection-writer.mjs +294 -0
- package/hostAdapter/zcodeAdapter/skills/femo/SKILL.md +93 -0
- package/hostAdapter/zcodeAdapter/tests/bridge-smoke.mjs +26 -0
- package/hostAdapter/zcodeAdapter/tests/event-router.test.mjs +157 -0
- package/hostAdapter/zcodeAdapter/tests/gateway-smoke.mjs +107 -0
- package/hostAdapter/zcodeAdapter/tests/integration-human.mjs +111 -0
- package/hostAdapter/zcodeAdapter/tests/main-actor.mjs +107 -0
- package/hostAdapter/zcodeAdapter/tests/mcp-smoke.mjs +130 -0
- package/hostAdapter/zcodeAdapter/tests/projection-writer.test.mjs +144 -0
- package/hostAdapter/zcodeAdapter/zcode.host.manifest.json +5 -0
- package/hostAdapter/zcodeAdapter//346/212/225/345/275/261/347/252/227/351/207/215/350/256/276/350/256/241-/350/256/250/350/256/272/347/250/277.md +113 -0
- package/package.json +1 -1
- package/scripts/diag-find-redblue.mjs +38 -0
- package/scripts/diag-god-log.mjs +31 -0
- package/scripts/diag-god-log2.mjs +39 -0
- package/scripts/diag-god-log3.mjs +35 -0
- package/scripts/diag-god-log4.mjs +59 -0
- package/scripts/diag-god-log4b.mjs +39 -0
- package/scripts/diag-redblue-god.mjs +39 -0
- package/scripts/diag-redblue-log.mjs +37 -0
- package/scripts/diag-redblue-span.mjs +41 -0
- package/scripts/diag-stage-1017.mjs +63 -0
- package/scripts/dump_session.py +36 -0
- package/scripts/link-workspace.ps1 +51 -0
- package/scripts/list_souls.py +18 -0
- package/scripts/probe-composer-refresh.mjs +197 -0
- package/scripts/seed_story_souls.py +86 -0
- package/scripts/sync-zcode-dist.ps1 +55 -0
- package/scripts/theme-tokenize/color-extract.txt +288 -0
- package/scripts/theme-tokenize/round1-quoted.ps1 +149 -0
- package/scripts/theme-tokenize/round2-bare.ps1 +138 -0
- package/scripts/theme-tokenize/round3-shape.ps1 +110 -0
- package/tests/_diag_r4_join.femo +32 -0
- package/tests/conftest.py +95 -0
- package/tests/diag-subagent-dir-3081.mjs +101 -0
- package/tests/femoGenerator.test.mjs +3909 -0
- package/tests/femoParser.test.mjs +4813 -0
- package/tests/femogen_notice.test.mjs +109 -0
- package/tests/femoparser_validation.test.mjs +84 -0
- package/tests/main-delivery-queue.test.mjs +329 -0
- package/tests/pre-step-gate.test.mjs +282 -0
- package/tests/read-subagent-model.mjs +24 -0
- package/tests/run-script-tests.py +97 -0
- package/tests/safe-steer.test.mjs +59 -0
- package/tests/strip_comments.test.mjs +164 -0
- package/tests/test_actor_failed.py +92 -0
- package/tests/test_compile_warnings.py +99 -0
- package/tests/test_debug_log_bus.py +108 -0
- package/tests/test_debugger_module_mode.py +141 -0
- package/tests/test_error_buckets.py +117 -0
- package/tests/test_error_dispatcher.py +261 -0
- package/tests/test_exec_ai_retry.py +155 -0
- package/tests/test_exec_human_retry.py +159 -0
- package/tests/test_file_resolve.py +104 -0
- package/tests/test_flow_events.py +311 -0
- package/tests/test_flow_ref_validation.py +92 -0
- package/tests/test_func_dict_fallback.py +39 -0
- package/tests/test_job_lifecycle.py +236 -0
- package/tests/test_job_manager.py +424 -0
- package/tests/test_mind.py +99 -0
- package/tests/test_module_call_args.py +258 -0
- package/tests/test_module_visibility.py +238 -0
- package/tests/test_notice.py +143 -0
- package/tests/test_out_whitelist.py +83 -0
- package/tests/test_par_if_dispatch.py +64 -0
- package/tests/test_par_nested_for.py +78 -0
- package/tests/test_parser.py +494 -0
- package/tests/test_pause.py +102 -0
- package/tests/test_pause_contract.py +135 -0
- package/tests/test_protocol_transcript.py +158 -0
- package/tests/test_scope_self.py +202 -0
- package/tests/test_source_validate.py +113 -0
- package/tests/test_task_world.py +447 -0
- package/tests/test_town_structure.py +70 -0
- package/tests/test_v2_checkpoint.py +115 -0
- package/tests/test_v2_env.py +383 -0
- package/tests/test_v2_evaluator.py +227 -0
- package/tests/test_v2_join.py +315 -0
- package/tests/test_v2_join_wiring.py +429 -0
- package/tests/test_v2_merge.py +167 -0
- package/tests/test_v2_model.py +143 -0
- package/tests/test_v2_resume_e2e.py +472 -0
- package/tests/test_var_decls.py +271 -0
- package/tests/verify-model-functions.mjs +90 -0
- package/tests/verify-subagent-model-3081.mjs +153 -0
- package//346/226/275/345/267/245/346/270/205/345/215/225/node-notice-/346/226/275/345/267/245/346/270/205/345/215/225.md +395 -0
- package//346/226/275/345/267/245/346/270/205/345/215/225//344/270/244/344/270/252/345/276/205/350/247/243/345/206/263/347/232/204/351/227/256/351/242/230.MD +66 -0
- package//346/226/275/345/267/245/346/270/205/345/215/225//345/201/234/346/255/242/351/223/276/344/277/256/345/244/215-/346/226/275/345/267/245/346/270/205/345/215/225.md +475 -0
- package//346/226/275/345/267/245/346/270/205/345/215/225//345/217/230/351/207/217/344/270/216task/347/263/273/347/273/237/351/207/215/346/236/204-/346/226/275/345/267/245/346/270/205/345/215/225-v2.md +847 -0
- package//346/226/275/345/267/245/346/270/205/345/215/225//350/277/220/350/241/214/347/212/266/346/200/201/351/223/276/350/267/257/351/207/215/346/236/204-/346/226/275/345/267/245/346/270/205/345/215/225v4.md +654 -0
- package//346/226/275/345/267/245/346/270/205/345/215/225//351/224/231/350/257/257/346/263/250/345/205/245/347/273/255/350/201/212-/346/226/275/345/267/245/346/270/205/345/215/225.md +471 -0
|
@@ -0,0 +1,396 @@
|
|
|
1
|
+
"""
|
|
2
|
+
femoCompiler/FEMO_normalizer.py — 脚本标准化器
|
|
3
|
+
将随意编写的 .femo 脚本转换为结构清晰、无语法糖的标准化文本,
|
|
4
|
+
供后续编译器(FEMO_parser)直接解析,无需再处理裸动作、续行、控制流内嵌等复杂情况。
|
|
5
|
+
|
|
6
|
+
处理步骤:
|
|
7
|
+
0. 去注释、空行,连接续行
|
|
8
|
+
1. 识别 mainflow: 之后的流程区域
|
|
9
|
+
2. 在流程区域内:
|
|
10
|
+
a. 提取所有节点定义 [Name]: action/module,并移出流程区域
|
|
11
|
+
b. 将裸动作、&module 替换为自动生成的节点引用并注册
|
|
12
|
+
c. 分离链内嵌控制关键字(fork/for/par/join),插入临时网关节点
|
|
13
|
+
d. 将 to 行与 join 同级化,拆分 to [X] -> [Y]
|
|
14
|
+
3. 补充缺失的空节点定义(凡引用过的 [Node] 若未定义,则自动创建空定义)
|
|
15
|
+
4. 收集所有节点定义,统一输出到 mainflow: 之前
|
|
16
|
+
5. 重新输出整洁的流程区域
|
|
17
|
+
"""
|
|
18
|
+
|
|
19
|
+
import re
|
|
20
|
+
from typing import List, Dict
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
class FEMONormalizer:
|
|
24
|
+
"""FEMO 脚本标准化器"""
|
|
25
|
+
|
|
26
|
+
def __init__(self):
|
|
27
|
+
self._gateway_counter = 0
|
|
28
|
+
# 节点定义存储:key 为不带括号的节点名(普通动作名如 EveMove 或 &module)
|
|
29
|
+
self._definitions: Dict[str, str] = {}
|
|
30
|
+
|
|
31
|
+
def _next_gateway_id(self, prefix: str = "gw") -> str:
|
|
32
|
+
"""生成唯一的网关 ID,形如 __fork_1__, __join_2__"""
|
|
33
|
+
self._gateway_counter += 1
|
|
34
|
+
return f"__{prefix}_{self._gateway_counter}__"
|
|
35
|
+
|
|
36
|
+
def _process_flow_block(self, inner_lines: List[str], base_indent: int) -> List[str]:
|
|
37
|
+
"""处理一个 flow 块的内部内容,返回标准化后的行列表(保持原缩进),
|
|
38
|
+
并将本块产生的定义行输出在流程区之前。
|
|
39
|
+
"""
|
|
40
|
+
if not inner_lines:
|
|
41
|
+
return []
|
|
42
|
+
|
|
43
|
+
# 记录已有定义,以便检测本块新增的定义
|
|
44
|
+
old_def_keys = set(self._definitions.keys())
|
|
45
|
+
|
|
46
|
+
# 1. 计算内部行的最小缩进(从内容行实际缩进取最小值;
|
|
47
|
+
# 初始 None 防止 base_indent+2 假设被 4 空格缩进剧本打破)
|
|
48
|
+
min_indent = None
|
|
49
|
+
for line in inner_lines:
|
|
50
|
+
if line.strip():
|
|
51
|
+
indent = len(line) - len(line.lstrip())
|
|
52
|
+
if min_indent is None or indent < min_indent:
|
|
53
|
+
min_indent = indent
|
|
54
|
+
if min_indent is None:
|
|
55
|
+
min_indent = base_indent + 2
|
|
56
|
+
|
|
57
|
+
# 2. 去除最小缩进
|
|
58
|
+
dedented_lines = []
|
|
59
|
+
for line in inner_lines:
|
|
60
|
+
if line.strip():
|
|
61
|
+
dedented_lines.append(line[min_indent:])
|
|
62
|
+
else:
|
|
63
|
+
dedented_lines.append('')
|
|
64
|
+
|
|
65
|
+
# 3. 按 [START] 或 [IN] 分割声明区和流程区
|
|
66
|
+
decl_lines = []
|
|
67
|
+
flow_part = []
|
|
68
|
+
flow_started = False
|
|
69
|
+
for dline in dedented_lines:
|
|
70
|
+
stripped = dline.strip()
|
|
71
|
+
if not flow_started and re.search(r'\[(START|IN)\]', stripped):
|
|
72
|
+
flow_started = True
|
|
73
|
+
if not flow_started:
|
|
74
|
+
decl_lines.append(dline)
|
|
75
|
+
else:
|
|
76
|
+
flow_part.append(dline)
|
|
77
|
+
|
|
78
|
+
# ★ 兜底(2026-08-24):没有 [START]/[IN] 标记、且声明区里连一条节点
|
|
79
|
+
# 定义([X]: 绑定)都没有时,整块内容其实是流程——原逻辑会把它们全部
|
|
80
|
+
# 当声明区静默吞掉(实测 notify-theater 裸名 mainflow 被吃成空图,引
|
|
81
|
+
# 擎空流程秒跑完还报 ✅)。
|
|
82
|
+
# 只兜「全部行都是纯裸名 action/&module 引用」这一种可证明形态:按顺
|
|
83
|
+
# 序合成规范单链 [START] -> a -> b -> [END](与最小模板同形)。混入控
|
|
84
|
+
# 制块/箭头等复杂形态时维持原状,由 parse_script 的空流程图保险丝响
|
|
85
|
+
# 亮报错,提示作者补 [START]——宁可报错不可静默。
|
|
86
|
+
if not flow_started and decl_lines:
|
|
87
|
+
has_def = any(re.search(r'\[\w+\]\s*:', ln) for ln in decl_lines)
|
|
88
|
+
if not has_def:
|
|
89
|
+
steps = [ln.strip() for ln in decl_lines if ln.strip()]
|
|
90
|
+
simple = bool(steps) and all(
|
|
91
|
+
re.fullmatch(r'[\w\u4e00-\u9fff&]+', s) is not None
|
|
92
|
+
and re.fullmatch(r'(?:for|par|fork|join|to)', s) is None
|
|
93
|
+
for s in steps
|
|
94
|
+
)
|
|
95
|
+
if simple:
|
|
96
|
+
flow_part = ['[START] -> ' + ' -> '.join(steps) + ' -> [END]']
|
|
97
|
+
decl_lines = []
|
|
98
|
+
|
|
99
|
+
# 4. 从声明区提取定义
|
|
100
|
+
local_defs = {}
|
|
101
|
+
for line in decl_lines:
|
|
102
|
+
modified = line
|
|
103
|
+
while True:
|
|
104
|
+
m = re.search(r'\[(\w+)\]\s*:\s*(\S.*?)(?=\s*->|\s*$)', modified)
|
|
105
|
+
if not m:
|
|
106
|
+
break
|
|
107
|
+
node_name = m.group(1)
|
|
108
|
+
binding = m.group(2).strip()
|
|
109
|
+
self._definitions[node_name] = binding
|
|
110
|
+
local_defs[node_name] = binding
|
|
111
|
+
start, end = m.span()
|
|
112
|
+
modified = modified[:start] + f'[{node_name}]' + modified[end:]
|
|
113
|
+
|
|
114
|
+
# 5. 流程区替换裸动作(同时可能产生内联定义)
|
|
115
|
+
expanded_flow = []
|
|
116
|
+
for line in flow_part:
|
|
117
|
+
expanded_flow.append(self._replace_bare_actions(line))
|
|
118
|
+
|
|
119
|
+
# 6. 补充缺失的节点定义
|
|
120
|
+
self._fill_missing_node_definitions(expanded_flow)
|
|
121
|
+
|
|
122
|
+
# 7. 收集本块新增的所有定义(包括内联产生的)
|
|
123
|
+
new_def_keys = set(self._definitions.keys()) - old_def_keys
|
|
124
|
+
for key in new_def_keys:
|
|
125
|
+
if key not in local_defs:
|
|
126
|
+
local_defs[key] = self._definitions[key]
|
|
127
|
+
|
|
128
|
+
# 8. 构建输出行(带缩进)
|
|
129
|
+
result = []
|
|
130
|
+
for node, binding in local_defs.items():
|
|
131
|
+
def_line = f"[{node}]: {binding}" if binding else f"[{node}]:"
|
|
132
|
+
result.append(' ' * min_indent + def_line)
|
|
133
|
+
for line in expanded_flow:
|
|
134
|
+
result.append(' ' * min_indent + line)
|
|
135
|
+
|
|
136
|
+
return result
|
|
137
|
+
|
|
138
|
+
|
|
139
|
+
def normalize(self, text: str) -> str:
|
|
140
|
+
lines = text.splitlines()
|
|
141
|
+
# 找出多行文本块区间(prompt: | / showprompt: | / key = |),块内注释豁免
|
|
142
|
+
prompt_ranges = self._find_multiline_ranges(lines)
|
|
143
|
+
# 去注释、空行
|
|
144
|
+
clean_lines = []
|
|
145
|
+
for idx, raw_line in enumerate(lines):
|
|
146
|
+
if any(s <= idx < e for s, e in prompt_ranges):
|
|
147
|
+
clean_lines.append(raw_line) # prompt 块内原样保留(# // 是内容)
|
|
148
|
+
continue
|
|
149
|
+
line = self._remove_comment(raw_line)
|
|
150
|
+
if line.strip() == '':
|
|
151
|
+
continue
|
|
152
|
+
clean_lines.append(line)
|
|
153
|
+
|
|
154
|
+
# ★ 移除所有 sketch: 块(无论在顶层还是模块内)
|
|
155
|
+
clean_lines = self._remove_sketch_blocks(clean_lines)
|
|
156
|
+
|
|
157
|
+
# 收集所有 flow/mainflow 块的起止索引
|
|
158
|
+
blocks = []
|
|
159
|
+
i = 0
|
|
160
|
+
while i < len(clean_lines):
|
|
161
|
+
line = clean_lines[i]
|
|
162
|
+
stripped = line.lstrip()
|
|
163
|
+
if re.match(r'^(mainflow|flow)\s*:', stripped):
|
|
164
|
+
indent = len(line) - len(line.lstrip())
|
|
165
|
+
j = i + 1
|
|
166
|
+
while j < len(clean_lines):
|
|
167
|
+
next_line = clean_lines[j]
|
|
168
|
+
if next_line.strip() == '':
|
|
169
|
+
j += 1
|
|
170
|
+
continue
|
|
171
|
+
next_indent = len(next_line) - len(next_line.lstrip())
|
|
172
|
+
if next_indent <= indent:
|
|
173
|
+
break
|
|
174
|
+
j += 1
|
|
175
|
+
blocks.append((i, j))
|
|
176
|
+
i = j
|
|
177
|
+
else:
|
|
178
|
+
i += 1
|
|
179
|
+
|
|
180
|
+
if not blocks:
|
|
181
|
+
return '\n'.join(clean_lines)
|
|
182
|
+
|
|
183
|
+
result = list(clean_lines)
|
|
184
|
+
for start_idx, end_idx in reversed(blocks):
|
|
185
|
+
header_line = result[start_idx]
|
|
186
|
+
inner_lines = result[start_idx+1:end_idx]
|
|
187
|
+
indent = len(header_line) - len(header_line.lstrip())
|
|
188
|
+
processed_inner = self._process_flow_block(inner_lines, indent)
|
|
189
|
+
result[start_idx+1:end_idx] = processed_inner
|
|
190
|
+
|
|
191
|
+
return '\n'.join(result)
|
|
192
|
+
|
|
193
|
+
|
|
194
|
+
def _remove_sketch_blocks(self, lines: List[str]) -> List[str]:
|
|
195
|
+
"""删除所有 sketch: 及其缩进块内容"""
|
|
196
|
+
result = []
|
|
197
|
+
i = 0
|
|
198
|
+
n = len(lines)
|
|
199
|
+
while i < n:
|
|
200
|
+
line = lines[i]
|
|
201
|
+
m = re.match(r'(\s*)sketch\s*:', line)
|
|
202
|
+
if m:
|
|
203
|
+
base_indent = len(m.group(1))
|
|
204
|
+
i += 1 # 跳过 sketch: 行本身
|
|
205
|
+
# 跳过所有缩进严格大于 base_indent 的行(块内容)
|
|
206
|
+
while i < n and (len(lines[i]) - len(lines[i].lstrip())) > base_indent:
|
|
207
|
+
i += 1
|
|
208
|
+
continue # 不添加到结果中
|
|
209
|
+
else:
|
|
210
|
+
result.append(line)
|
|
211
|
+
i += 1
|
|
212
|
+
return result
|
|
213
|
+
|
|
214
|
+
|
|
215
|
+
def _find_multiline_ranges(self, lines: List[str]) -> List[tuple]:
|
|
216
|
+
"""找出多行文本块的行区间 [start, end):prompt: | / showprompt: | / key = |。
|
|
217
|
+
块内注释豁免(# 和 // 是文本内容)。"""
|
|
218
|
+
ranges = []
|
|
219
|
+
i = 0
|
|
220
|
+
n = len(lines)
|
|
221
|
+
while i < n:
|
|
222
|
+
m = re.match(
|
|
223
|
+
r'^\s*(?:(?:prompt|showprompt):\s*[||]|(?:@|\$@|\$)?[\w\u4e00-\u9fff]+\s*=\s*[||])\s*(?:#.*)?$',
|
|
224
|
+
lines[i],
|
|
225
|
+
)
|
|
226
|
+
if m:
|
|
227
|
+
base = len(lines[i]) - len(lines[i].lstrip())
|
|
228
|
+
j = i + 1
|
|
229
|
+
while j < n:
|
|
230
|
+
if not lines[j].strip():
|
|
231
|
+
j += 1
|
|
232
|
+
continue # 空行算块内容
|
|
233
|
+
if len(lines[j]) - len(lines[j].lstrip()) <= base:
|
|
234
|
+
break # 缩进回到字段层 → 块结束
|
|
235
|
+
j += 1
|
|
236
|
+
ranges.append((i + 1, j))
|
|
237
|
+
i = j
|
|
238
|
+
continue
|
|
239
|
+
i += 1
|
|
240
|
+
return ranges
|
|
241
|
+
|
|
242
|
+
def _remove_comment(self, line: str) -> str:
|
|
243
|
+
in_str = False
|
|
244
|
+
quote_char = None
|
|
245
|
+
for i, ch in enumerate(line):
|
|
246
|
+
if in_str:
|
|
247
|
+
if ch == '\\' and i + 1 < len(line):
|
|
248
|
+
pass
|
|
249
|
+
elif ch == quote_char:
|
|
250
|
+
in_str = False
|
|
251
|
+
else:
|
|
252
|
+
if ch in ('"', "'"):
|
|
253
|
+
in_str = True
|
|
254
|
+
quote_char = ch
|
|
255
|
+
elif ch == '#' or (ch == '/' and i + 1 < len(line) and line[i + 1] == '/'):
|
|
256
|
+
return line[:i].rstrip()
|
|
257
|
+
return line
|
|
258
|
+
|
|
259
|
+
def _replace_bare_actions(self, line: str) -> str:
|
|
260
|
+
"""将不在 [] 中的独立动作名或 &module 替换为 [自动节点],但保护控制关键字片段"""
|
|
261
|
+
# 按 '->' 分割,保留分隔符
|
|
262
|
+
parts = re.split(r'(\s*->\s*)', line)
|
|
263
|
+
new_parts = []
|
|
264
|
+
for part in parts:
|
|
265
|
+
if re.match(r'\s*->\s*', part):
|
|
266
|
+
# 箭头本身,直接保留
|
|
267
|
+
new_parts.append(part)
|
|
268
|
+
continue
|
|
269
|
+
stripped = part.strip()
|
|
270
|
+
# 1) 先检查是否为内联节点定义:[node]: binding
|
|
271
|
+
def_match = re.match(r'^\[(\w+)\]:\s*(.*)$', stripped)
|
|
272
|
+
if def_match:
|
|
273
|
+
node_name = def_match.group(1)
|
|
274
|
+
binding = def_match.group(2).strip()
|
|
275
|
+
# 记录定义(若同名已存在则覆盖,通常不会)
|
|
276
|
+
self._definitions[node_name] = binding
|
|
277
|
+
# 替换为仅 [node],保留原有缩进空白
|
|
278
|
+
leading = part[:len(part) - len(part.lstrip())]
|
|
279
|
+
new_parts.append(leading + f'[{node_name}]')
|
|
280
|
+
continue
|
|
281
|
+
|
|
282
|
+
# 1.5) to 行:提取 to [node]: ref 绑定(to [DONE]:finish -> [END])
|
|
283
|
+
if stripped.startswith('to '):
|
|
284
|
+
tm = re.match(r'^to\s+\[(\w+)\]\s*:\s*(\S+)', stripped)
|
|
285
|
+
if tm:
|
|
286
|
+
self._definitions[tm.group(1)] = tm.group(2)
|
|
287
|
+
leading = part[:len(part) - len(part.lstrip())]
|
|
288
|
+
new_parts.append(leading + f'to [{tm.group(1)}]')
|
|
289
|
+
continue
|
|
290
|
+
new_parts.append(part)
|
|
291
|
+
continue
|
|
292
|
+
|
|
293
|
+
# 2) 检查是否为控制关键字片段(if, for, par, fork, join, to)
|
|
294
|
+
if re.match(r'\b(if|for|par|fork|join|to)\b', stripped):
|
|
295
|
+
new_parts.append(part)
|
|
296
|
+
continue
|
|
297
|
+
|
|
298
|
+
# 3) 普通片段:应用裸动作替换
|
|
299
|
+
leading = part[:len(part) - len(part.lstrip())]
|
|
300
|
+
content = part.lstrip()
|
|
301
|
+
new_content = self._replace_bare_in_fragment(content)
|
|
302
|
+
new_parts.append(leading + new_content)
|
|
303
|
+
return ''.join(new_parts)
|
|
304
|
+
|
|
305
|
+
def _replace_bare_in_fragment(self, fragment: str) -> str:
|
|
306
|
+
"""对不包含控制关键字的片段进行裸动作替换"""
|
|
307
|
+
def replacer(m):
|
|
308
|
+
token = m.group(1)
|
|
309
|
+
# 关键字保护(作为兜底)
|
|
310
|
+
keywords = {'fork', 'for', 'par', 'join', 'to', 'if', 'in', 'all', 'any', 'n'}
|
|
311
|
+
if token.lower() in keywords:
|
|
312
|
+
return token
|
|
313
|
+
# 生成节点内部键:&module 的节点名不带 &(绑定保留 & 前缀)
|
|
314
|
+
base = token.lstrip('&')
|
|
315
|
+
node_key = base
|
|
316
|
+
original_key = base
|
|
317
|
+
counter = 1
|
|
318
|
+
while node_key in self._definitions:
|
|
319
|
+
node_key = f'{original_key}_{counter}'
|
|
320
|
+
counter += 1
|
|
321
|
+
self._definitions[node_key] = token
|
|
322
|
+
return f'[{node_key}]'
|
|
323
|
+
|
|
324
|
+
# 第一步·模块引用预处理(点路径 &Outer.Inner、带参 &Name(args),
|
|
325
|
+
# 二者可组合),必须先于裸词替换且作用于整段:裸词正则不识别 '.' 和
|
|
326
|
+
# '('——点路径会被拆成 &Outer + Inner 两个独立 token(语义损坏成两个
|
|
327
|
+
# 节点),参数表达式会丢参并被捏造成幽灵 action 节点(实锤
|
|
328
|
+
# 2026-09-12:&Inner2(visits) 编译报"未声明的动作 'visits'")。无参
|
|
329
|
+
# 形态一并由此处理,与带参同一事实源。
|
|
330
|
+
fragment = re.sub(
|
|
331
|
+
r'(?<!\[)&([\w\u4e00-\u9fff]+(?:\.[\w\u4e00-\u9fff]+)*)(?:\s*\(([^)]*)\))?',
|
|
332
|
+
lambda m: self._handle_module_ref(m.group(1), m.group(2)),
|
|
333
|
+
fragment
|
|
334
|
+
)
|
|
335
|
+
|
|
336
|
+
# 第二步·裸词替换:先切出节点标签段 [label] 整段保护(label 可含
|
|
337
|
+
# 点路径,如预处理刚生成的 [Outer.Inner2])——裸词替换不得进入标签
|
|
338
|
+
# 内部,否则其中间的点段(Inner2)会被捏造成幽灵节点。只对标签外
|
|
339
|
+
# 的部分做替换;注意 & 是非单词字符,\b 在 & 前不成立,必须用
|
|
340
|
+
# (&?\b\w+) 才能把 & 一并捕获。
|
|
341
|
+
def _bare_pass(seg: str) -> str:
|
|
342
|
+
return re.sub(r'(&?\b\w+)\b(?!\])', replacer, seg)
|
|
343
|
+
|
|
344
|
+
parts = re.split(r'(\[[^\]]*\])', fragment)
|
|
345
|
+
return ''.join(
|
|
346
|
+
part if idx % 2 == 1 else _bare_pass(part)
|
|
347
|
+
for idx, part in enumerate(parts)
|
|
348
|
+
)
|
|
349
|
+
|
|
350
|
+
def _handle_module_ref(self, mod_name: str, args) -> str:
|
|
351
|
+
# args=None = 无参调用(token 不带括号);带参时参数原文随绑定交给
|
|
352
|
+
# 运行时求值入帧。mod_name 允许点路径('Outer.Inner',嵌套模块完整
|
|
353
|
+
# 路径),节点键同步用它,保证不同路径的同名模块节点不互相吞并。
|
|
354
|
+
token = f'&{mod_name}({args})' if args is not None else f'&{mod_name}'
|
|
355
|
+
node_key = mod_name # 节点名不带 &
|
|
356
|
+
counter = 1
|
|
357
|
+
while node_key in self._definitions:
|
|
358
|
+
node_key = f'{mod_name}_{counter}'
|
|
359
|
+
counter += 1
|
|
360
|
+
self._definitions[node_key] = token
|
|
361
|
+
return f'[{node_key}]'
|
|
362
|
+
|
|
363
|
+
def _fill_missing_node_definitions(self, flow_lines: List[str]):
|
|
364
|
+
"""扫描所有 [...] 节点引用,若未定义则添加空定义(跳过保留字和内部网关)"""
|
|
365
|
+
RESERVED = {'START', 'END', 'BREAK', 'IN', 'OUT'}
|
|
366
|
+
for line in flow_lines:
|
|
367
|
+
refs = re.findall(r'\[(\w+)\]', line)
|
|
368
|
+
for name in refs:
|
|
369
|
+
if name in RESERVED:
|
|
370
|
+
continue
|
|
371
|
+
if name.startswith('__') and name.endswith('__'):
|
|
372
|
+
continue # 内部网关节点不声明
|
|
373
|
+
if name not in self._definitions:
|
|
374
|
+
self._definitions[name] = '' # 空定义
|
|
375
|
+
|
|
376
|
+
# 测试入口
|
|
377
|
+
if __name__ == '__main__':
|
|
378
|
+
print("请输入 FEMO 流程文本(多行),输入完成后在新行输入 'END' 并回车,或按 Ctrl+D (Unix) / Ctrl+Z (Windows) 结束。")
|
|
379
|
+
lines = []
|
|
380
|
+
try:
|
|
381
|
+
while True:
|
|
382
|
+
line = input()
|
|
383
|
+
if line.strip() == 'END':
|
|
384
|
+
break
|
|
385
|
+
lines.append(line)
|
|
386
|
+
except EOFError:
|
|
387
|
+
pass
|
|
388
|
+
|
|
389
|
+
user_text = '\n'.join(lines)
|
|
390
|
+
if 'mainflow:' not in user_text:
|
|
391
|
+
user_text = 'mainflow:\n' + user_text
|
|
392
|
+
|
|
393
|
+
norm = FEMONormalizer()
|
|
394
|
+
result = norm.normalize(user_text)
|
|
395
|
+
print("\n--- 标准化结果 ---")
|
|
396
|
+
print(result)
|