acps-math-visualizer 1.0.0__tar.gz
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.
- acps_math_visualizer-1.0.0/PKG-INFO +39 -0
- acps_math_visualizer-1.0.0/pyproject.toml +79 -0
- acps_math_visualizer-1.0.0/setup.cfg +4 -0
- acps_math_visualizer-1.0.0/src/acps_math_visualizer/__init__.py +5 -0
- acps_math_visualizer-1.0.0/src/acps_math_visualizer/__main__.py +5 -0
- acps_math_visualizer-1.0.0/src/acps_math_visualizer/agents/__init__.py +7 -0
- acps_math_visualizer-1.0.0/src/acps_math_visualizer/agents/base.py +133 -0
- acps_math_visualizer-1.0.0/src/acps_math_visualizer/agents/intuition.py +154 -0
- acps_math_visualizer-1.0.0/src/acps_math_visualizer/agents/plotter.py +162 -0
- acps_math_visualizer-1.0.0/src/acps_math_visualizer/agents/transformation.py +234 -0
- acps_math_visualizer-1.0.0/src/acps_math_visualizer/cli.py +76 -0
- acps_math_visualizer-1.0.0/src/acps_math_visualizer/demo.py +125 -0
- acps_math_visualizer-1.0.0/src/acps_math_visualizer/identity.py +67 -0
- acps_math_visualizer-1.0.0/src/acps_math_visualizer/math_engine.py +333 -0
- acps_math_visualizer-1.0.0/src/acps_math_visualizer/mesh.py +55 -0
- acps_math_visualizer-1.0.0/src/acps_math_visualizer/models.py +214 -0
- acps_math_visualizer-1.0.0/src/acps_math_visualizer/orchestrator.py +300 -0
- acps_math_visualizer-1.0.0/src/acps_math_visualizer/protocol.py +64 -0
- acps_math_visualizer-1.0.0/src/acps_math_visualizer/py.typed +0 -0
- acps_math_visualizer-1.0.0/src/acps_math_visualizer/registry.py +84 -0
- acps_math_visualizer-1.0.0/src/acps_math_visualizer/runtime.py +178 -0
- acps_math_visualizer-1.0.0/src/acps_math_visualizer/visualization.py +160 -0
- acps_math_visualizer-1.0.0/src/acps_math_visualizer.egg-info/PKG-INFO +39 -0
- acps_math_visualizer-1.0.0/src/acps_math_visualizer.egg-info/SOURCES.txt +27 -0
- acps_math_visualizer-1.0.0/src/acps_math_visualizer.egg-info/dependency_links.txt +1 -0
- acps_math_visualizer-1.0.0/src/acps_math_visualizer.egg-info/entry_points.txt +2 -0
- acps_math_visualizer-1.0.0/src/acps_math_visualizer.egg-info/requires.txt +14 -0
- acps_math_visualizer-1.0.0/src/acps_math_visualizer.egg-info/top_level.txt +1 -0
- acps_math_visualizer-1.0.0/tests/test_math_visualizer.py +106 -0
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: acps-math-visualizer
|
|
3
|
+
Version: 1.0.0
|
|
4
|
+
Summary: An ACPs multi-agent studio that turns abstract formulas into inspectable motion for secondary-school functions and university calculus. A Plotter Agent builds curves, data and animations; a Transformation Agent explains amplitude, period and phase; an Intuition Agent supplies memorable analogies. Built on acps-sdk 2.1.0 with ADP discovery, AIP v2 task routing, typed products and auditable traces for teaching, tutoring and self-study.
|
|
5
|
+
Author: ACPs Math Visualizer Engineering
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Project-URL: Homepage, https://pypi.org/project/acps-math-visualizer/
|
|
8
|
+
Keywords: acps,acps-sdk,multi-agent,mathematics,visualization,trigonometry,calculus,education
|
|
9
|
+
Classifier: Development Status :: 5 - Production/Stable
|
|
10
|
+
Classifier: Intended Audience :: Education
|
|
11
|
+
Classifier: Intended Audience :: Science/Research
|
|
12
|
+
Classifier: Operating System :: OS Independent
|
|
13
|
+
Classifier: Programming Language :: Python :: 3
|
|
14
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
19
|
+
Classifier: Topic :: Education
|
|
20
|
+
Classifier: Topic :: Scientific/Engineering :: Mathematics
|
|
21
|
+
Classifier: Topic :: Scientific/Engineering :: Visualization
|
|
22
|
+
Classifier: Typing :: Typed
|
|
23
|
+
Requires-Python: >=3.10
|
|
24
|
+
Description-Content-Type: text/plain
|
|
25
|
+
Requires-Dist: acps-sdk==2.1.0
|
|
26
|
+
Requires-Dist: fastapi>=0.100.0
|
|
27
|
+
Requires-Dist: httpx>=0.25.0
|
|
28
|
+
Requires-Dist: matplotlib>=3.8.0
|
|
29
|
+
Requires-Dist: numpy>=1.26.0
|
|
30
|
+
Requires-Dist: pillow>=10.0.0
|
|
31
|
+
Requires-Dist: pydantic>=2.0.0
|
|
32
|
+
Requires-Dist: uvicorn>=0.27.0
|
|
33
|
+
Provides-Extra: dev
|
|
34
|
+
Requires-Dist: build>=1.0.0; extra == "dev"
|
|
35
|
+
Requires-Dist: pytest>=8.0.0; extra == "dev"
|
|
36
|
+
Requires-Dist: ruff>=0.6.0; extra == "dev"
|
|
37
|
+
Requires-Dist: twine>=5.0.0; extra == "dev"
|
|
38
|
+
|
|
39
|
+
ACPs Math Visualizer coordinates Plotter, Transformation and Intuition agents through acps-sdk 2.1.0 to turn mathematical expressions into plots, transformation animations, data exports and accessible explanations.
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=77.0.0", "wheel"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "acps-math-visualizer"
|
|
7
|
+
version = "1.0.0"
|
|
8
|
+
description = "An ACPs multi-agent studio that turns abstract formulas into inspectable motion for secondary-school functions and university calculus. A Plotter Agent builds curves, data and animations; a Transformation Agent explains amplitude, period and phase; an Intuition Agent supplies memorable analogies. Built on acps-sdk 2.1.0 with ADP discovery, AIP v2 task routing, typed products and auditable traces for teaching, tutoring and self-study."
|
|
9
|
+
readme = {text = "ACPs Math Visualizer coordinates Plotter, Transformation and Intuition agents through acps-sdk 2.1.0 to turn mathematical expressions into plots, transformation animations, data exports and accessible explanations.", content-type = "text/plain"}
|
|
10
|
+
requires-python = ">=3.10"
|
|
11
|
+
license = "MIT"
|
|
12
|
+
authors = [{ name = "ACPs Math Visualizer Engineering" }]
|
|
13
|
+
keywords = [
|
|
14
|
+
"acps",
|
|
15
|
+
"acps-sdk",
|
|
16
|
+
"multi-agent",
|
|
17
|
+
"mathematics",
|
|
18
|
+
"visualization",
|
|
19
|
+
"trigonometry",
|
|
20
|
+
"calculus",
|
|
21
|
+
"education",
|
|
22
|
+
]
|
|
23
|
+
classifiers = [
|
|
24
|
+
"Development Status :: 5 - Production/Stable",
|
|
25
|
+
"Intended Audience :: Education",
|
|
26
|
+
"Intended Audience :: Science/Research",
|
|
27
|
+
"Operating System :: OS Independent",
|
|
28
|
+
"Programming Language :: Python :: 3",
|
|
29
|
+
"Programming Language :: Python :: 3 :: Only",
|
|
30
|
+
"Programming Language :: Python :: 3.10",
|
|
31
|
+
"Programming Language :: Python :: 3.11",
|
|
32
|
+
"Programming Language :: Python :: 3.12",
|
|
33
|
+
"Programming Language :: Python :: 3.13",
|
|
34
|
+
"Topic :: Education",
|
|
35
|
+
"Topic :: Scientific/Engineering :: Mathematics",
|
|
36
|
+
"Topic :: Scientific/Engineering :: Visualization",
|
|
37
|
+
"Typing :: Typed",
|
|
38
|
+
]
|
|
39
|
+
dependencies = [
|
|
40
|
+
"acps-sdk==2.1.0",
|
|
41
|
+
"fastapi>=0.100.0",
|
|
42
|
+
"httpx>=0.25.0",
|
|
43
|
+
"matplotlib>=3.8.0",
|
|
44
|
+
"numpy>=1.26.0",
|
|
45
|
+
"pillow>=10.0.0",
|
|
46
|
+
"pydantic>=2.0.0",
|
|
47
|
+
"uvicorn>=0.27.0",
|
|
48
|
+
]
|
|
49
|
+
|
|
50
|
+
[project.optional-dependencies]
|
|
51
|
+
dev = [
|
|
52
|
+
"build>=1.0.0",
|
|
53
|
+
"pytest>=8.0.0",
|
|
54
|
+
"ruff>=0.6.0",
|
|
55
|
+
"twine>=5.0.0",
|
|
56
|
+
]
|
|
57
|
+
|
|
58
|
+
[project.scripts]
|
|
59
|
+
acps-math-viz = "acps_math_visualizer.cli:main"
|
|
60
|
+
|
|
61
|
+
[project.urls]
|
|
62
|
+
Homepage = "https://pypi.org/project/acps-math-visualizer/"
|
|
63
|
+
|
|
64
|
+
[tool.setuptools.packages.find]
|
|
65
|
+
where = ["src"]
|
|
66
|
+
|
|
67
|
+
[tool.setuptools.package-data]
|
|
68
|
+
acps_math_visualizer = ["py.typed"]
|
|
69
|
+
|
|
70
|
+
[tool.pytest.ini_options]
|
|
71
|
+
testpaths = ["tests"]
|
|
72
|
+
pythonpath = ["src"]
|
|
73
|
+
addopts = "-q"
|
|
74
|
+
|
|
75
|
+
[tool.ruff]
|
|
76
|
+
line-length = 100
|
|
77
|
+
|
|
78
|
+
[tool.ruff.lint]
|
|
79
|
+
select = ["E", "F", "I", "UP", "B"]
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
"""Shared AIP lifecycle and independently advertised agent capability."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from abc import ABC, abstractmethod
|
|
6
|
+
from typing import ClassVar
|
|
7
|
+
|
|
8
|
+
from acps_sdk.acs import AgentCapabilitySpec, AgentSkill
|
|
9
|
+
from acps_sdk.aip import TaskCommand, TaskResult, TaskState
|
|
10
|
+
from acps_sdk.aip.aip_rpc_server import CommandHandlers, DefaultHandlers, TaskManager
|
|
11
|
+
from pydantic import BaseModel
|
|
12
|
+
|
|
13
|
+
from .. import __version__
|
|
14
|
+
from ..identity import build_capability_spec
|
|
15
|
+
from ..models import WireMessage
|
|
16
|
+
from ..protocol import decode_message
|
|
17
|
+
from ..runtime import TERMINAL_STATES, ResultBuilder
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
class AipAgent(ABC):
|
|
21
|
+
"""Base class for one remotely dispatched mathematical partner."""
|
|
22
|
+
|
|
23
|
+
name: ClassVar[str]
|
|
24
|
+
description: ClassVar[str]
|
|
25
|
+
skill_id: ClassVar[str]
|
|
26
|
+
skill_name: ClassVar[str]
|
|
27
|
+
skill_description: ClassVar[str]
|
|
28
|
+
tags: ClassVar[tuple[str, ...]]
|
|
29
|
+
examples: ClassVar[tuple[str, ...]]
|
|
30
|
+
product_name: ClassVar[str]
|
|
31
|
+
|
|
32
|
+
def __init__(self, aic: str) -> None:
|
|
33
|
+
self.aic = aic
|
|
34
|
+
self.endpoint_url: str | None = None
|
|
35
|
+
self._result = ResultBuilder(aic)
|
|
36
|
+
|
|
37
|
+
def attach_endpoint(self, endpoint_url: str) -> None:
|
|
38
|
+
self.endpoint_url = endpoint_url
|
|
39
|
+
|
|
40
|
+
@property
|
|
41
|
+
def capability_spec(self) -> AgentCapabilitySpec:
|
|
42
|
+
if self.endpoint_url is None:
|
|
43
|
+
raise RuntimeError(f"{self.name} has no endpoint")
|
|
44
|
+
skill = AgentSkill(
|
|
45
|
+
id=self.skill_id,
|
|
46
|
+
name=self.skill_name,
|
|
47
|
+
description=self.skill_description,
|
|
48
|
+
version=__version__,
|
|
49
|
+
tags=list(self.tags),
|
|
50
|
+
examples=list(self.examples),
|
|
51
|
+
input_modes=["text/plain", "application/json"],
|
|
52
|
+
output_modes=["application/json"],
|
|
53
|
+
)
|
|
54
|
+
return build_capability_spec(
|
|
55
|
+
aic=self.aic,
|
|
56
|
+
name=self.name,
|
|
57
|
+
description=self.description,
|
|
58
|
+
skill=skill,
|
|
59
|
+
endpoint_url=self.endpoint_url,
|
|
60
|
+
)
|
|
61
|
+
|
|
62
|
+
def build_handlers(self) -> CommandHandlers:
|
|
63
|
+
return CommandHandlers(
|
|
64
|
+
on_start=self._on_start,
|
|
65
|
+
on_continue=self._on_continue,
|
|
66
|
+
on_cancel=self._on_cancel,
|
|
67
|
+
on_complete=self._on_complete,
|
|
68
|
+
)
|
|
69
|
+
|
|
70
|
+
async def _on_start(
|
|
71
|
+
self,
|
|
72
|
+
command: TaskCommand,
|
|
73
|
+
task: TaskResult | None,
|
|
74
|
+
) -> TaskResult:
|
|
75
|
+
if task is not None:
|
|
76
|
+
return task
|
|
77
|
+
created = TaskManager.create_task(command)
|
|
78
|
+
created.senderId = self.aic
|
|
79
|
+
try:
|
|
80
|
+
message = decode_message(command, expected_task_id=created.taskId)
|
|
81
|
+
return self.handle_start(message, created)
|
|
82
|
+
except Exception as exc:
|
|
83
|
+
return self.fail(created, f"{self.name} rejected the start command: {exc}")
|
|
84
|
+
|
|
85
|
+
async def _on_continue(self, command: TaskCommand, task: TaskResult) -> TaskResult:
|
|
86
|
+
if task.status.state in TERMINAL_STATES:
|
|
87
|
+
return task
|
|
88
|
+
if task.status.state is not TaskState.AwaitingInput:
|
|
89
|
+
return self.fail(task, f"{self.name} is not awaiting a follow-up plan")
|
|
90
|
+
try:
|
|
91
|
+
message = decode_message(command, expected_task_id=task.taskId)
|
|
92
|
+
return self.handle_continue(message, task)
|
|
93
|
+
except Exception as exc:
|
|
94
|
+
return self.fail(task, f"{self.name} rejected the continuation: {exc}")
|
|
95
|
+
|
|
96
|
+
async def _on_cancel(self, command: TaskCommand, task: TaskResult) -> TaskResult:
|
|
97
|
+
result = await DefaultHandlers.cancel(command, task)
|
|
98
|
+
result.senderId = self.aic
|
|
99
|
+
return result
|
|
100
|
+
|
|
101
|
+
async def _on_complete(self, command: TaskCommand, task: TaskResult) -> TaskResult:
|
|
102
|
+
result = await DefaultHandlers.complete(command, task)
|
|
103
|
+
result.senderId = self.aic
|
|
104
|
+
return result
|
|
105
|
+
|
|
106
|
+
@abstractmethod
|
|
107
|
+
def handle_start(self, message: WireMessage, task: TaskResult) -> TaskResult:
|
|
108
|
+
"""Handle a validated application start message."""
|
|
109
|
+
|
|
110
|
+
def handle_continue(self, message: WireMessage, task: TaskResult) -> TaskResult:
|
|
111
|
+
return self.fail(task, f"{self.name} does not accept continuation messages")
|
|
112
|
+
|
|
113
|
+
def publish(
|
|
114
|
+
self,
|
|
115
|
+
task: TaskResult,
|
|
116
|
+
product: BaseModel,
|
|
117
|
+
*,
|
|
118
|
+
summary: str,
|
|
119
|
+
state: TaskState,
|
|
120
|
+
) -> TaskResult:
|
|
121
|
+
return self._result.publish(
|
|
122
|
+
task,
|
|
123
|
+
state,
|
|
124
|
+
product.model_dump(mode="json"),
|
|
125
|
+
product_name=self.product_name,
|
|
126
|
+
summary=summary,
|
|
127
|
+
)
|
|
128
|
+
|
|
129
|
+
def fail(self, task: TaskResult, message: str) -> TaskResult:
|
|
130
|
+
return self._result.fail(task, message)
|
|
131
|
+
|
|
132
|
+
|
|
133
|
+
__all__ = ["AipAgent"]
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
"""Intuition Agent: grounded everyday analogies and retrieval practice."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from acps_sdk.aip import TaskResult, TaskState
|
|
6
|
+
|
|
7
|
+
from ..math_engine import format_quantity
|
|
8
|
+
from ..models import (
|
|
9
|
+
IntuitionConcept,
|
|
10
|
+
IntuitionProduct,
|
|
11
|
+
IntuitionRequest,
|
|
12
|
+
MessageKind,
|
|
13
|
+
WireMessage,
|
|
14
|
+
)
|
|
15
|
+
from .base import AipAgent
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
class IntuitionAgent(AipAgent):
|
|
19
|
+
"""Builds explanations from actual upstream mathematical products."""
|
|
20
|
+
|
|
21
|
+
name = "Intuition Agent"
|
|
22
|
+
description = (
|
|
23
|
+
"Turns plotted evidence and transformation parameters into accurate everyday "
|
|
24
|
+
"analogies for functions, limits and derivatives, followed by a self-check."
|
|
25
|
+
)
|
|
26
|
+
skill_id = "mathematical-intuition-coaching"
|
|
27
|
+
skill_name = "Everyday mathematical intuition coaching"
|
|
28
|
+
skill_description = (
|
|
29
|
+
"Explains the geometric story behind a plotted formula with concrete analogies, "
|
|
30
|
+
"while distinguishing a limit's approach from the function's value at a point."
|
|
31
|
+
)
|
|
32
|
+
tags = ("math-intuition", "analogy-coach", "calculus-learning")
|
|
33
|
+
examples = (
|
|
34
|
+
"Explain amplitude and phase using a Ferris wheel.",
|
|
35
|
+
"Explain a limit as approaching a destination without confusing it with arrival.",
|
|
36
|
+
)
|
|
37
|
+
product_name = "mathematical-intuition"
|
|
38
|
+
|
|
39
|
+
def handle_start(self, message: WireMessage, task: TaskResult) -> TaskResult:
|
|
40
|
+
if message.kind is not MessageKind.INTUITION_REQUEST:
|
|
41
|
+
raise ValueError("Intuition Agent requires INTUITION_REQUEST")
|
|
42
|
+
request = IntuitionRequest.model_validate(message.payload)
|
|
43
|
+
upstream_sessions = {
|
|
44
|
+
request.plot.session_id,
|
|
45
|
+
request.transformation.session_id,
|
|
46
|
+
request.animation.session_id,
|
|
47
|
+
}
|
|
48
|
+
if upstream_sessions != {message.session_id}:
|
|
49
|
+
raise ValueError("upstream evidence belongs to a different session")
|
|
50
|
+
concept = request.concept
|
|
51
|
+
if concept is IntuitionConcept.AUTO:
|
|
52
|
+
concept = (
|
|
53
|
+
IntuitionConcept.TRANSFORMATION
|
|
54
|
+
if request.transformation.is_sinusoidal
|
|
55
|
+
else IntuitionConcept.DERIVATIVE
|
|
56
|
+
)
|
|
57
|
+
if concept is IntuitionConcept.LIMIT:
|
|
58
|
+
product = self._limit_product(message)
|
|
59
|
+
elif concept is IntuitionConcept.DERIVATIVE:
|
|
60
|
+
product = self._derivative_product(message, request)
|
|
61
|
+
else:
|
|
62
|
+
product = self._transformation_product(message, request)
|
|
63
|
+
return self.publish(
|
|
64
|
+
task,
|
|
65
|
+
product,
|
|
66
|
+
summary=f"Intuition concept={product.concept}; headline={product.headline}",
|
|
67
|
+
state=TaskState.Completed,
|
|
68
|
+
)
|
|
69
|
+
|
|
70
|
+
def _transformation_product(
|
|
71
|
+
self,
|
|
72
|
+
message: WireMessage,
|
|
73
|
+
request: IntuitionRequest,
|
|
74
|
+
) -> IntuitionProduct:
|
|
75
|
+
transformation = request.transformation
|
|
76
|
+
trig = transformation.trig_function or "sin"
|
|
77
|
+
amplitude = format_quantity(transformation.amplitude or 1.0)
|
|
78
|
+
period = format_quantity(transformation.period or 1.0)
|
|
79
|
+
phase = transformation.phase_shift or 0.0
|
|
80
|
+
direction = "更早" if phase < 0 else "更晚" if phase > 0 else "准时"
|
|
81
|
+
steps = [
|
|
82
|
+
(
|
|
83
|
+
f"振幅像摩天轮半径:半径调到 {amplitude},"
|
|
84
|
+
f"座舱离中心最高、最远的距离也变为 {amplitude}。"
|
|
85
|
+
),
|
|
86
|
+
f"周期像转一圈所需时间:现在每 {period} 个 x 单位重复一次同样的高度变化。",
|
|
87
|
+
(
|
|
88
|
+
f"相位像开场时间:h={format_quantity(phase)} 表示波形上的关键时刻{direction}出现;"
|
|
89
|
+
"地图上看,就是整条曲线向左或向右搬动。"
|
|
90
|
+
),
|
|
91
|
+
]
|
|
92
|
+
if request.animation.animation_path:
|
|
93
|
+
steps.append("动态图像先把‘半径’调大,再调整‘开场时间’,每个参数不再混在一起。")
|
|
94
|
+
return IntuitionProduct(
|
|
95
|
+
session_id=message.session_id,
|
|
96
|
+
task_id=message.task_id,
|
|
97
|
+
agent=self.name,
|
|
98
|
+
concept="function transformation",
|
|
99
|
+
headline="把正弦波想成一座可调的摩天轮",
|
|
100
|
+
analogy=(
|
|
101
|
+
f"基准 y={trig}(x) 像摩天轮座舱随时间变化的高度:A 是半径,"
|
|
102
|
+
"周期是转一圈花多久,相位是这场转动何时开始,D 是中心离地高度。"
|
|
103
|
+
),
|
|
104
|
+
steps=steps,
|
|
105
|
+
checkpoint="若把原式中的 x+π/4 改成 x-π/4,波形应该向哪边移动?",
|
|
106
|
+
answer="应向右移动 π/4。统一改写成 h=-C/B,就不容易被括号里的正负号误导。",
|
|
107
|
+
)
|
|
108
|
+
|
|
109
|
+
def _limit_product(self, message: WireMessage) -> IntuitionProduct:
|
|
110
|
+
return IntuitionProduct(
|
|
111
|
+
session_id=message.session_id,
|
|
112
|
+
task_id=message.task_id,
|
|
113
|
+
agent=self.name,
|
|
114
|
+
concept="limit",
|
|
115
|
+
headline="极限关注‘靠近时去哪里’,不只看‘最后站在哪里’",
|
|
116
|
+
analogy=(
|
|
117
|
+
"把 x→a 想成无人机无限接近一个检查点:极限是在问沿途高度越来越接近哪个值,"
|
|
118
|
+
"就像无限逼近但未必需要抵达的目的地;检查点本身是否允许降落,是函数值 f(a) 的问题。"
|
|
119
|
+
),
|
|
120
|
+
steps=[
|
|
121
|
+
"先分别观察左侧与右侧接近时的函数值。",
|
|
122
|
+
"只有两侧都趋向同一个有限值,双侧极限才存在。",
|
|
123
|
+
"即使该点没有定义,极限仍可能存在;这就是极限能修补‘缺口直觉’的原因。",
|
|
124
|
+
],
|
|
125
|
+
checkpoint="函数在 x=a 处没有定义,是否说明 x→a 时极限一定不存在?",
|
|
126
|
+
answer="不是。极限描述邻域内的逼近趋势,与该点是否有函数值是两个问题。",
|
|
127
|
+
)
|
|
128
|
+
|
|
129
|
+
def _derivative_product(
|
|
130
|
+
self,
|
|
131
|
+
message: WireMessage,
|
|
132
|
+
request: IntuitionRequest,
|
|
133
|
+
) -> IntuitionProduct:
|
|
134
|
+
return IntuitionProduct(
|
|
135
|
+
session_id=message.session_id,
|
|
136
|
+
task_id=message.task_id,
|
|
137
|
+
agent=self.name,
|
|
138
|
+
concept="derivative and curve shape",
|
|
139
|
+
headline="把曲线看成山路,把导数看成此刻的坡度",
|
|
140
|
+
analogy=(
|
|
141
|
+
f"观察 {request.plot.normalized_expression}:函数值告诉你所在位置的高度,"
|
|
142
|
+
"导数则像自行车码表旁的坡度计,告诉你这一刻上坡多陡或下坡多快。"
|
|
143
|
+
),
|
|
144
|
+
steps=[
|
|
145
|
+
"曲线上升处,切线斜率为正;下降处,切线斜率为负。",
|
|
146
|
+
"峰顶或谷底附近,山路可能短暂变平,对应水平切线。",
|
|
147
|
+
"放大很小的一段,平滑曲线会越来越像切线,这就是局部线性化。",
|
|
148
|
+
],
|
|
149
|
+
checkpoint="某点导数为 0,是否一定能断定那里是最大值或最小值?",
|
|
150
|
+
answer="不能。导数为 0 只说明切线水平,还需检查两侧符号或使用更高阶条件。",
|
|
151
|
+
)
|
|
152
|
+
|
|
153
|
+
|
|
154
|
+
__all__ = ["IntuitionAgent"]
|
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
"""Plotter Agent: safe sampling, static plots, data export, and animation."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import uuid
|
|
6
|
+
from dataclasses import dataclass
|
|
7
|
+
from pathlib import Path
|
|
8
|
+
|
|
9
|
+
import numpy as np
|
|
10
|
+
from acps_sdk.aip import TaskResult, TaskState
|
|
11
|
+
|
|
12
|
+
from ..math_engine import SampledFunction, sample_expression
|
|
13
|
+
from ..models import (
|
|
14
|
+
AnimationProduct,
|
|
15
|
+
AnimationRequest,
|
|
16
|
+
FunctionRequest,
|
|
17
|
+
MessageKind,
|
|
18
|
+
PlotPoint,
|
|
19
|
+
PlotProduct,
|
|
20
|
+
WireMessage,
|
|
21
|
+
)
|
|
22
|
+
from ..visualization import render_static_plot, render_transformation_gif, write_points_csv
|
|
23
|
+
from .base import AipAgent
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
@dataclass(frozen=True)
|
|
27
|
+
class PlotMemory:
|
|
28
|
+
"""Task-scoped numerical state required by the animation continuation."""
|
|
29
|
+
|
|
30
|
+
session_id: str
|
|
31
|
+
request: FunctionRequest
|
|
32
|
+
sample: SampledFunction
|
|
33
|
+
output_dir: Path
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
class PlotterAgent(AipAgent):
|
|
37
|
+
"""Owns every visual artifact and accepts a transformation plan as follow-up."""
|
|
38
|
+
|
|
39
|
+
name = "Plotter Agent"
|
|
40
|
+
description = (
|
|
41
|
+
"Safely evaluates a one-variable function, exports numerical samples and a PNG, "
|
|
42
|
+
"then renders an animated GIF from a Transformation Agent keyframe plan."
|
|
43
|
+
)
|
|
44
|
+
skill_id = "function-plot-and-animation"
|
|
45
|
+
skill_name = "Function plotting and transformation animation"
|
|
46
|
+
skill_description = (
|
|
47
|
+
"Produces inspectable plot data, static curves and interpolated mathematical "
|
|
48
|
+
"animations without executing arbitrary Python from user input."
|
|
49
|
+
)
|
|
50
|
+
tags = ("math-plotter", "function-curve", "dynamic-visualization")
|
|
51
|
+
examples = (
|
|
52
|
+
"Plot y=2*sin(x+π/4) from -2π to 2π.",
|
|
53
|
+
"Export sampled points and animate amplitude and phase changes.",
|
|
54
|
+
)
|
|
55
|
+
product_name = "function-plot"
|
|
56
|
+
|
|
57
|
+
def __init__(self, aic: str) -> None:
|
|
58
|
+
super().__init__(aic)
|
|
59
|
+
self._memory: dict[str, PlotMemory] = {}
|
|
60
|
+
|
|
61
|
+
def handle_start(self, message: WireMessage, task: TaskResult) -> TaskResult:
|
|
62
|
+
if message.kind is not MessageKind.PLOT_REQUEST:
|
|
63
|
+
raise ValueError("Plotter Agent requires PLOT_REQUEST")
|
|
64
|
+
request = FunctionRequest.model_validate(message.payload)
|
|
65
|
+
sample = sample_expression(
|
|
66
|
+
request.expression,
|
|
67
|
+
x_min=request.x_min,
|
|
68
|
+
x_max=request.x_max,
|
|
69
|
+
samples=request.samples,
|
|
70
|
+
)
|
|
71
|
+
output_dir = Path(request.output_dir).expanduser().resolve()
|
|
72
|
+
output_dir.mkdir(parents=True, exist_ok=True)
|
|
73
|
+
token = uuid.uuid5(uuid.NAMESPACE_URL, message.session_id).hex[:10]
|
|
74
|
+
image_path = output_dir / f"plot-{token}.png"
|
|
75
|
+
csv_path = output_dir / f"points-{token}.csv"
|
|
76
|
+
render_static_plot(sample, image_path)
|
|
77
|
+
write_points_csv(sample, csv_path)
|
|
78
|
+
self._memory[message.task_id] = PlotMemory(
|
|
79
|
+
session_id=message.session_id,
|
|
80
|
+
request=request,
|
|
81
|
+
sample=sample,
|
|
82
|
+
output_dir=output_dir,
|
|
83
|
+
)
|
|
84
|
+
|
|
85
|
+
finite_indices = np.flatnonzero(np.isfinite(sample.y))
|
|
86
|
+
finite_y = sample.y[finite_indices]
|
|
87
|
+
preview_count = min(9, len(finite_indices))
|
|
88
|
+
preview_positions = np.linspace(0, len(finite_indices) - 1, preview_count, dtype=int)
|
|
89
|
+
preview_indices = finite_indices[preview_positions]
|
|
90
|
+
product = PlotProduct(
|
|
91
|
+
session_id=message.session_id,
|
|
92
|
+
task_id=message.task_id,
|
|
93
|
+
agent=self.name,
|
|
94
|
+
expression=request.expression,
|
|
95
|
+
normalized_expression=sample.normalized_expression,
|
|
96
|
+
x_min=request.x_min,
|
|
97
|
+
x_max=request.x_max,
|
|
98
|
+
sample_count=len(sample.x),
|
|
99
|
+
defined_count=len(finite_indices),
|
|
100
|
+
y_min=float(np.min(finite_y)),
|
|
101
|
+
y_max=float(np.max(finite_y)),
|
|
102
|
+
preview_points=[
|
|
103
|
+
PlotPoint(x=float(sample.x[index]), y=float(sample.y[index]))
|
|
104
|
+
for index in preview_indices
|
|
105
|
+
],
|
|
106
|
+
image_path=str(image_path),
|
|
107
|
+
csv_path=str(csv_path),
|
|
108
|
+
)
|
|
109
|
+
return self.publish(
|
|
110
|
+
task,
|
|
111
|
+
product,
|
|
112
|
+
summary=(
|
|
113
|
+
f"Sampled {product.defined_count}/{product.sample_count} finite points; "
|
|
114
|
+
f"PNG={image_path.name}; CSV={csv_path.name}"
|
|
115
|
+
),
|
|
116
|
+
state=TaskState.AwaitingInput,
|
|
117
|
+
)
|
|
118
|
+
|
|
119
|
+
def handle_continue(self, message: WireMessage, task: TaskResult) -> TaskResult:
|
|
120
|
+
if message.kind is not MessageKind.ANIMATION_REQUEST:
|
|
121
|
+
raise ValueError("Plotter continuation requires ANIMATION_REQUEST")
|
|
122
|
+
memory = self._memory.get(message.task_id)
|
|
123
|
+
if memory is None or memory.session_id != message.session_id:
|
|
124
|
+
raise ValueError("unknown or cross-session Plotter task")
|
|
125
|
+
request = AnimationRequest.model_validate(message.payload)
|
|
126
|
+
animation_path: Path | None = None
|
|
127
|
+
frame_count = 0
|
|
128
|
+
if request.frames:
|
|
129
|
+
if request.trig_function not in {"sin", "cos"}:
|
|
130
|
+
raise ValueError("an animation plan requires a sine or cosine family")
|
|
131
|
+
token = uuid.uuid5(uuid.NAMESPACE_URL, message.session_id).hex[:10]
|
|
132
|
+
animation_path = memory.output_dir / f"transformation-{token}.gif"
|
|
133
|
+
frame_count = render_transformation_gif(
|
|
134
|
+
memory.sample,
|
|
135
|
+
trig_function=request.trig_function,
|
|
136
|
+
frames=request.frames,
|
|
137
|
+
path=animation_path,
|
|
138
|
+
)
|
|
139
|
+
description = (
|
|
140
|
+
f"已按 {len(request.frames)} 个关键帧插值生成 {frame_count} 帧 GIF,"
|
|
141
|
+
"逐步展示参数变化。"
|
|
142
|
+
)
|
|
143
|
+
else:
|
|
144
|
+
description = "该函数没有单正弦/余弦参数变换计划,因此保留静态 PNG 与完整 CSV。"
|
|
145
|
+
product = AnimationProduct(
|
|
146
|
+
session_id=message.session_id,
|
|
147
|
+
task_id=message.task_id,
|
|
148
|
+
agent=self.name,
|
|
149
|
+
animation_path=str(animation_path) if animation_path else None,
|
|
150
|
+
frame_count=frame_count,
|
|
151
|
+
description=description,
|
|
152
|
+
)
|
|
153
|
+
self._memory.pop(message.task_id, None)
|
|
154
|
+
return self.publish(
|
|
155
|
+
task,
|
|
156
|
+
product,
|
|
157
|
+
summary=description,
|
|
158
|
+
state=TaskState.Completed,
|
|
159
|
+
)
|
|
160
|
+
|
|
161
|
+
|
|
162
|
+
__all__ = ["PlotterAgent"]
|