Skip to content

Commit 3cb36f8

Browse files
整理 pipeline snapshot/预览 dry-run:迁移 Iterable/Iterator 类型导入、移除未使用导入、简化排序逻辑并补齐 EOF 换行
1 parent 46e7c88 commit 3cb36f8

File tree

12 files changed

+596
-173
lines changed

12 files changed

+596
-173
lines changed

astrbot/builtin_stars/astrbot/process_llm_request.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,9 @@ async def _ensure_persona(self, req: ProviderRequest, cfg: dict, umo: str):
4646
default_persona = self.ctx.persona_manager.selected_default_persona_v3
4747
if not default_persona:
4848
try:
49-
default_persona = await self.ctx.persona_manager.get_default_persona_v3(umo)
49+
default_persona = (
50+
await self.ctx.persona_manager.get_default_persona_v3(umo)
51+
)
5052
except Exception:
5153
default_persona = None
5254
if default_persona:

astrbot/core/pipeline/prompt_preview_dry_run.py

Lines changed: 161 additions & 44 deletions
Large diffs are not rendered by default.

astrbot/core/pipeline/snapshot/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22

33
from .builder import build_pipeline_snapshot
44

5-
__all__ = ["build_pipeline_snapshot"]
5+
__all__ = ["build_pipeline_snapshot"]

astrbot/core/pipeline/snapshot/ast_risk_scanner.py

Lines changed: 104 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,10 @@ def _is_persona_base(node: ast.AST) -> bool:
6464

6565
@classmethod
6666
def _is_persona_prompt_attr(cls, node: ast.AST) -> bool:
67-
return isinstance(node, ast.Attribute) and node.attr == "prompt" and cls._is_persona_base(
68-
node.value
67+
return (
68+
isinstance(node, ast.Attribute)
69+
and node.attr == "prompt"
70+
and cls._is_persona_base(node.value)
6971
)
7072

7173
@classmethod
@@ -81,7 +83,9 @@ def _is_persona_prompt_subscript(cls, node: ast.AST) -> bool:
8183
def _confidence_rank(level: str) -> int:
8284
return {"low": 0, "medium": 1, "high": 2}.get(level, -1)
8385

84-
def _record_stop_event(self, *, confidence: str, reason: str, node: ast.AST) -> None:
86+
def _record_stop_event(
87+
self, *, confidence: str, reason: str, node: ast.AST
88+
) -> None:
8589
self.stop_event_sites.append(
8690
{
8791
"confidence": confidence,
@@ -127,8 +131,10 @@ def _record_top_level_args(self, args: ast.arguments) -> None:
127131
lowered = name.lower()
128132
if name == "event" or lowered.endswith("event"):
129133
self.event_vars.add(name)
130-
if name in {"req", "request", "provider_request"} or lowered.endswith("_req") or (
131-
"providerrequest" in lowered
134+
if (
135+
name in {"req", "request", "provider_request"}
136+
or lowered.endswith("_req")
137+
or ("providerrequest" in lowered)
132138
):
133139
self.req_vars.add(name)
134140
if "llm_response" in lowered or "llmresponse" in lowered:
@@ -157,7 +163,9 @@ def _is_chain_attr(self, node: ast.AST) -> bool:
157163
return (
158164
isinstance(node, ast.Attribute)
159165
and node.attr == "chain"
160-
and (self._is_get_result_call(node.value) or self._is_result_ref(node.value))
166+
and (
167+
self._is_get_result_call(node.value) or self._is_result_ref(node.value)
168+
)
161169
)
162170

163171
def _handle_result_var_assign(self, tgt: ast.AST, value: ast.AST) -> None:
@@ -172,7 +180,9 @@ def _is_result_chain_expr(self, node: ast.AST) -> bool:
172180
return (
173181
isinstance(node, ast.Attribute)
174182
and node.attr == "chain"
175-
and (self._is_get_result_call(node.value) or self._is_result_ref(node.value))
183+
and (
184+
self._is_get_result_call(node.value) or self._is_result_ref(node.value)
185+
)
176186
)
177187

178188
def _handle_chain_var_assign(self, tgt: ast.AST, value: ast.AST) -> None:
@@ -183,7 +193,9 @@ def _handle_chain_var_assign(self, tgt: ast.AST, value: ast.AST) -> None:
183193
elif isinstance(value, ast.Name) and value.id in self.chain_vars:
184194
self.chain_vars.add(tgt.id)
185195

186-
def _handle_attr_write(self, target: ast.Attribute, value: ast.AST, node: ast.AST) -> None:
196+
def _handle_attr_write(
197+
self, target: ast.Attribute, value: ast.AST, node: ast.AST
198+
) -> None:
187199
base_name = self._name_id(target.value)
188200

189201
if target.attr in {"prompt", "system_prompt"} and base_name in self.req_vars:
@@ -197,7 +209,11 @@ def _handle_attr_write(self, target: ast.Attribute, value: ast.AST, node: ast.AS
197209
return
198210

199211
if target.attr == "contexts" and base_name in self.req_vars:
200-
op = "clear" if isinstance(value, ast.List) and not getattr(value, "elts", []) else "overwrite"
212+
op = (
213+
"clear"
214+
if isinstance(value, ast.List) and not getattr(value, "elts", [])
215+
else "overwrite"
216+
)
201217
self._add_effect(
202218
target="provider_request.contexts",
203219
op=op,
@@ -208,7 +224,11 @@ def _handle_attr_write(self, target: ast.Attribute, value: ast.AST, node: ast.AS
208224
return
209225

210226
if target.attr == "extra_user_content_parts" and base_name in self.req_vars:
211-
op = "clear" if isinstance(value, ast.List) and not getattr(value, "elts", []) else "overwrite"
227+
op = (
228+
"clear"
229+
if isinstance(value, ast.List) and not getattr(value, "elts", [])
230+
else "overwrite"
231+
)
212232
self._add_effect(
213233
target="provider_request.extra_user_content_parts",
214234
op=op,
@@ -250,12 +270,18 @@ def _handle_attr_write(self, target: ast.Attribute, value: ast.AST, node: ast.AS
250270
}:
251271
eff_target = f"llm_response.{target.attr}"
252272
op = "overwrite"
253-
if target.attr in {"tools_call_name", "tools_call_args"} and isinstance(value, ast.List) and not getattr(
254-
value, "elts", []
273+
if (
274+
target.attr in {"tools_call_name", "tools_call_args"}
275+
and isinstance(value, ast.List)
276+
and not getattr(value, "elts", [])
255277
):
256278
op = "clear"
257-
confidence = "medium" if base_name in self._llm_response_vars_inferred else "high"
258-
evidence_suffix = "inferred" if base_name in self._llm_response_vars_inferred else "param"
279+
confidence = (
280+
"medium" if base_name in self._llm_response_vars_inferred else "high"
281+
)
282+
evidence_suffix = (
283+
"inferred" if base_name in self._llm_response_vars_inferred else "param"
284+
)
259285
self._add_effect(
260286
target=eff_target,
261287
op=op,
@@ -289,8 +315,14 @@ def _handle_attr_write(self, target: ast.Attribute, value: ast.AST, node: ast.AS
289315
)
290316
return
291317

292-
if target.attr == "chain" and (self._is_get_result_call(target.value) or self._is_result_ref(target.value)):
293-
op = "clear" if isinstance(value, ast.List) and not getattr(value, "elts", []) else "overwrite"
318+
if target.attr == "chain" and (
319+
self._is_get_result_call(target.value) or self._is_result_ref(target.value)
320+
):
321+
op = (
322+
"clear"
323+
if isinstance(value, ast.List) and not getattr(value, "elts", [])
324+
else "overwrite"
325+
)
294326
self._add_effect(
295327
target="result.chain",
296328
op=op,
@@ -375,11 +407,19 @@ def visit_Call(self, node: ast.Call) -> Any:
375407

376408
if call_name == "stop_event":
377409
if self._fn_depth >= 2:
378-
self._record_stop_event(confidence="low", reason="nested_callable", node=node)
410+
self._record_stop_event(
411+
confidence="low", reason="nested_callable", node=node
412+
)
379413
elif self._guard_stack:
380-
self._record_stop_event(confidence="medium", reason=f"inside_{self._guard_stack[-1]}", node=node)
414+
self._record_stop_event(
415+
confidence="medium",
416+
reason=f"inside_{self._guard_stack[-1]}",
417+
node=node,
418+
)
381419
else:
382-
self._record_stop_event(confidence="high", reason="unconditional", node=node)
420+
self._record_stop_event(
421+
confidence="high", reason="unconditional", node=node
422+
)
383423
self._add_effect(
384424
target="stop",
385425
op="call",
@@ -394,7 +434,13 @@ def visit_Call(self, node: ast.Call) -> Any:
394434
and isinstance(node.func.value, ast.Name)
395435
and node.func.value.id in self.event_vars
396436
):
397-
self._add_effect(target="send", op="call", confidence="high", evidence="event.send_call", node=node)
437+
self._add_effect(
438+
target="send",
439+
op="call",
440+
confidence="high",
441+
evidence="event.send_call",
442+
node=node,
443+
)
398444

399445
if isinstance(node.func, ast.Attribute):
400446
method = node.func.attr
@@ -420,7 +466,11 @@ def visit_Call(self, node: ast.Call) -> Any:
420466
node=node,
421467
)
422468

423-
if isinstance(recv, ast.Name) and recv.id in self.chain_vars and method in list_ops:
469+
if (
470+
isinstance(recv, ast.Name)
471+
and recv.id in self.chain_vars
472+
and method in list_ops
473+
):
424474
if method in list_ops_clear:
425475
op = "clear"
426476
elif method in list_ops_append:
@@ -435,8 +485,15 @@ def visit_Call(self, node: ast.Call) -> Any:
435485
node=node,
436486
)
437487

438-
if isinstance(recv, ast.Attribute) and isinstance(recv.value, ast.Name) and recv.value.id in self.req_vars:
439-
if recv.attr in {"contexts", "extra_user_content_parts"} and method in list_ops:
488+
if (
489+
isinstance(recv, ast.Attribute)
490+
and isinstance(recv.value, ast.Name)
491+
and recv.value.id in self.req_vars
492+
):
493+
if (
494+
recv.attr in {"contexts", "extra_user_content_parts"}
495+
and method in list_ops
496+
):
440497
if method in list_ops_clear:
441498
op = "clear"
442499
elif method in list_ops_append:
@@ -459,8 +516,15 @@ def visit_Call(self, node: ast.Call) -> Any:
459516
node=node,
460517
)
461518

462-
if isinstance(recv, ast.Attribute) and isinstance(recv.value, ast.Name) and recv.value.id in self.llm_response_vars:
463-
if recv.attr in {"tools_call_name", "tools_call_args"} and method in list_ops:
519+
if (
520+
isinstance(recv, ast.Attribute)
521+
and isinstance(recv.value, ast.Name)
522+
and recv.value.id in self.llm_response_vars
523+
):
524+
if (
525+
recv.attr in {"tools_call_name", "tools_call_args"}
526+
and method in list_ops
527+
):
464528
if method in list_ops_clear:
465529
op = "clear"
466530
elif method in list_ops_append:
@@ -523,7 +587,10 @@ def visit_AugAssign(self, node: ast.AugAssign) -> Any:
523587
)
524588

525589
base_name = self._name_id(node.target.value)
526-
if base_name in self.req_vars and node.target.attr in {"prompt", "system_prompt"}:
590+
if base_name in self.req_vars and node.target.attr in {
591+
"prompt",
592+
"system_prompt",
593+
}:
527594
self._add_effect(
528595
target=f"provider_request.{node.target.attr}",
529596
op="append",
@@ -533,7 +600,8 @@ def visit_AugAssign(self, node: ast.AugAssign) -> Any:
533600
)
534601

535602
if node.target.attr == "chain" and (
536-
self._is_get_result_call(node.target.value) or self._is_result_ref(node.target.value)
603+
self._is_get_result_call(node.target.value)
604+
or self._is_result_ref(node.target.value)
537605
):
538606
self._add_effect(
539607
target="result.chain",
@@ -553,8 +621,14 @@ def visit_AugAssign(self, node: ast.AugAssign) -> Any:
553621
def visit_Yield(self, node: ast.Yield) -> Any:
554622
if isinstance(node.value, ast.Call):
555623
fn = node.value.func
556-
if isinstance(fn, ast.Name) and fn.id in {"MessageEventResult", "CommandResult"}:
624+
if isinstance(fn, ast.Name) and fn.id in {
625+
"MessageEventResult",
626+
"CommandResult",
627+
}:
557628
self.yields_result = True
558-
if isinstance(fn, ast.Attribute) and fn.attr in {"MessageEventResult", "CommandResult"}:
629+
if isinstance(fn, ast.Attribute) and fn.attr in {
630+
"MessageEventResult",
631+
"CommandResult",
632+
}:
559633
self.yields_result = True
560-
self.generic_visit(node)
634+
self.generic_visit(node)

astrbot/core/pipeline/snapshot/ast_scan.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44
import inspect
55
import sys
66
import textwrap
7+
from collections.abc import Iterator
78
from dataclasses import dataclass
8-
from typing import Any, Iterator
9+
from typing import Any
910

1011
from .ast_risk_scanner import _RiskScanner
1112

@@ -144,4 +145,4 @@ def _parse_source_compat(src: str) -> ast.AST:
144145
"_NameCallScanner",
145146
"_format_parse_error",
146147
"_parse_source_compat",
147-
]
148+
]

0 commit comments

Comments
 (0)