fastpilot 0.1.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.
Files changed (71) hide show
  1. fastpilot-0.1.0/LICENSE +203 -0
  2. fastpilot-0.1.0/PKG-INFO +384 -0
  3. fastpilot-0.1.0/README.md +356 -0
  4. fastpilot-0.1.0/pyproject.toml +140 -0
  5. fastpilot-0.1.0/pyproject.toml.orig +129 -0
  6. fastpilot-0.1.0/src/fastpilot/__init__.py +171 -0
  7. fastpilot-0.1.0/src/fastpilot/contrib/__init__.py +30 -0
  8. fastpilot-0.1.0/src/fastpilot/contrib/echo.py +61 -0
  9. fastpilot-0.1.0/src/fastpilot/contrib/robot.py +168 -0
  10. fastpilot-0.1.0/src/fastpilot/core/__init__.py +7 -0
  11. fastpilot-0.1.0/src/fastpilot/core/children.py +105 -0
  12. fastpilot-0.1.0/src/fastpilot/core/factory.py +34 -0
  13. fastpilot-0.1.0/src/fastpilot/core/handle.py +59 -0
  14. fastpilot-0.1.0/src/fastpilot/core/logical_time.py +142 -0
  15. fastpilot-0.1.0/src/fastpilot/core/queue.py +184 -0
  16. fastpilot-0.1.0/src/fastpilot/core/realtime.py +61 -0
  17. fastpilot-0.1.0/src/fastpilot/core/runtime.py +781 -0
  18. fastpilot-0.1.0/src/fastpilot/core/station.py +141 -0
  19. fastpilot-0.1.0/src/fastpilot/driver.py +175 -0
  20. fastpilot-0.1.0/src/fastpilot/ipython/__init__.py +9 -0
  21. fastpilot-0.1.0/src/fastpilot/ipython/display.py +54 -0
  22. fastpilot-0.1.0/src/fastpilot/observation/__init__.py +38 -0
  23. fastpilot-0.1.0/src/fastpilot/observation/bus.py +185 -0
  24. fastpilot-0.1.0/src/fastpilot/observation/runtime_history.py +98 -0
  25. fastpilot-0.1.0/src/fastpilot/observation/store.py +245 -0
  26. fastpilot-0.1.0/src/fastpilot/observation/updates.py +111 -0
  27. fastpilot-0.1.0/src/fastpilot/protocol/__init__.py +147 -0
  28. fastpilot-0.1.0/src/fastpilot/protocol/catalog.py +465 -0
  29. fastpilot-0.1.0/src/fastpilot/protocol/commands.py +154 -0
  30. fastpilot-0.1.0/src/fastpilot/protocol/contracts.py +69 -0
  31. fastpilot-0.1.0/src/fastpilot/protocol/declarations.py +161 -0
  32. fastpilot-0.1.0/src/fastpilot/protocol/drafts.py +70 -0
  33. fastpilot-0.1.0/src/fastpilot/protocol/effects.py +218 -0
  34. fastpilot-0.1.0/src/fastpilot/protocol/errors.py +13 -0
  35. fastpilot-0.1.0/src/fastpilot/protocol/events.py +117 -0
  36. fastpilot-0.1.0/src/fastpilot/protocol/hosting.py +20 -0
  37. fastpilot-0.1.0/src/fastpilot/protocol/queue.py +77 -0
  38. fastpilot-0.1.0/src/fastpilot/protocol/receipts.py +50 -0
  39. fastpilot-0.1.0/src/fastpilot/protocol/schema.py +144 -0
  40. fastpilot-0.1.0/src/fastpilot/protocol/system_facts.py +228 -0
  41. fastpilot-0.1.0/src/fastpilot/protocol/triggers.py +227 -0
  42. fastpilot-0.1.0/src/fastpilot/protocol/values.py +111 -0
  43. fastpilot-0.1.0/src/fastpilot/remote/__init__.py +7 -0
  44. fastpilot-0.1.0/src/fastpilot/remote/connect.py +86 -0
  45. fastpilot-0.1.0/src/fastpilot/remote/errors.py +21 -0
  46. fastpilot-0.1.0/src/fastpilot/remote/handle.py +47 -0
  47. fastpilot-0.1.0/src/fastpilot/remote/runtime.py +530 -0
  48. fastpilot-0.1.0/src/fastpilot/service/__init__.py +23 -0
  49. fastpilot-0.1.0/src/fastpilot/service/__main__.py +32 -0
  50. fastpilot-0.1.0/src/fastpilot/service/app.py +486 -0
  51. fastpilot-0.1.0/src/fastpilot/service/browser.py +191 -0
  52. fastpilot-0.1.0/src/fastpilot/service/hosting.py +232 -0
  53. fastpilot-0.1.0/src/fastpilot/service/ledger.py +43 -0
  54. fastpilot-0.1.0/src/fastpilot/service/mount.py +53 -0
  55. fastpilot-0.1.0/src/fastpilot/service/streaming.py +53 -0
  56. fastpilot-0.1.0/src/fastpilot/state/__init__.py +32 -0
  57. fastpilot-0.1.0/src/fastpilot/state/chart.py +46 -0
  58. fastpilot-0.1.0/src/fastpilot/state/handlers.py +136 -0
  59. fastpilot-0.1.0/src/fastpilot/state/machine.py +169 -0
  60. fastpilot-0.1.0/src/fastpilot/state/participant.py +227 -0
  61. fastpilot-0.1.0/src/fastpilot/time/__init__.py +27 -0
  62. fastpilot-0.1.0/src/fastpilot/time/calendar.py +48 -0
  63. fastpilot-0.1.0/src/fastpilot/time/clock.py +31 -0
  64. fastpilot-0.1.0/src/fastpilot/time/rules.py +63 -0
  65. fastpilot-0.1.0/src/fastpilot/time/validation.py +25 -0
  66. fastpilot-0.1.0/src/fastpilot/wire/__init__.py +39 -0
  67. fastpilot-0.1.0/src/fastpilot/wire/commands.py +62 -0
  68. fastpilot-0.1.0/src/fastpilot/wire/observations.py +345 -0
  69. fastpilot-0.1.0/src/fastpilot/wire/protocol_values.py +307 -0
  70. fastpilot-0.1.0/src/fastpilot/wire/values.py +102 -0
  71. fastpilot-0.1.0/src/fastpilot/wire/version.py +5 -0
@@ -0,0 +1,203 @@
1
+ Copyright 2024 弥澄亮
2
+
3
+ Apache License
4
+ Version 2.0, January 2004
5
+ http://www.apache.org/licenses/
6
+
7
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
8
+
9
+ 1. Definitions.
10
+
11
+ "License" shall mean the terms and conditions for use, reproduction,
12
+ and distribution as defined by Sections 1 through 9 of this document.
13
+
14
+ "Licensor" shall mean the copyright owner or entity authorized by
15
+ the copyright owner that is granting the License.
16
+
17
+ "Legal Entity" shall mean the union of the acting entity and all
18
+ other entities that control, are controlled by, or are under common
19
+ control with that entity. For the purposes of this definition,
20
+ "control" means (i) the power, direct or indirect, to cause the
21
+ direction or management of such entity, whether by contract or
22
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
23
+ outstanding shares, or (iii) beneficial ownership of such entity.
24
+
25
+ "You" (or "Your") shall mean an individual or Legal Entity
26
+ exercising permissions granted by this License.
27
+
28
+ "Source" form shall mean the preferred form for making modifications,
29
+ including but not limited to software source code, documentation
30
+ source, and configuration files.
31
+
32
+ "Object" form shall mean any form resulting from mechanical
33
+ transformation or translation of a Source form, including but
34
+ not limited to compiled object code, generated documentation,
35
+ and conversions to other media types.
36
+
37
+ "Work" shall mean the work of authorship, whether in Source or
38
+ Object form, made available under the License, as indicated by a
39
+ copyright notice that is included in or attached to the work
40
+ (an example is provided in the Appendix below).
41
+
42
+ "Derivative Works" shall mean any work, whether in Source or Object
43
+ form, that is based on (or derived from) the Work and for which the
44
+ editorial revisions, annotations, elaborations, or other modifications
45
+ represent, as a whole, an original work of authorship. For the purposes
46
+ of this License, Derivative Works shall not include works that remain
47
+ separable from, or merely link (or bind by name) to the interfaces of,
48
+ the Work and Derivative Works thereof.
49
+
50
+ "Contribution" shall mean any work of authorship, including
51
+ the original version of the Work and any modifications or additions
52
+ to that Work or Derivative Works thereof, that is intentionally
53
+ submitted to Licensor for inclusion in the Work by the copyright owner
54
+ or by an individual or Legal Entity authorized to submit on behalf of
55
+ the copyright owner. For the purposes of this definition, "submitted"
56
+ means any form of electronic, verbal, or written communication sent
57
+ to the Licensor or its representatives, including but not limited to
58
+ communication on electronic mailing lists, source code control systems,
59
+ and issue tracking systems that are managed by, or on behalf of, the
60
+ Licensor for the purpose of discussing and improving the Work, but
61
+ excluding communication that is conspicuously marked or otherwise
62
+ designated in writing by the copyright owner as "Not a Contribution."
63
+
64
+ "Contributor" shall mean Licensor and any individual or Legal Entity
65
+ on behalf of whom a Contribution has been received by Licensor and
66
+ subsequently incorporated within the Work.
67
+
68
+ 2. Grant of Copyright License. Subject to the terms and conditions of
69
+ this License, each Contributor hereby grants to You a perpetual,
70
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
71
+ copyright license to reproduce, prepare Derivative Works of,
72
+ publicly display, publicly perform, sublicense, and distribute the
73
+ Work and such Derivative Works in Source or Object form.
74
+
75
+ 3. Grant of Patent License. Subject to the terms and conditions of
76
+ this License, each Contributor hereby grants to You a perpetual,
77
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
78
+ (except as stated in this section) patent license to make, have made,
79
+ use, offer to sell, sell, import, and otherwise transfer the Work,
80
+ where such license applies only to those patent claims licensable
81
+ by such Contributor that are necessarily infringed by their
82
+ Contribution(s) alone or by combination of their Contribution(s)
83
+ with the Work to which such Contribution(s) was submitted. If You
84
+ institute patent litigation against any entity (including a
85
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
86
+ or a Contribution incorporated within the Work constitutes direct
87
+ or contributory patent infringement, then any patent licenses
88
+ granted to You under this License for that Work shall terminate
89
+ as of the date such litigation is filed.
90
+
91
+ 4. Redistribution. You may reproduce and distribute copies of the
92
+ Work or Derivative Works thereof in any medium, with or without
93
+ modifications, and in Source or Object form, provided that You
94
+ meet the following conditions:
95
+
96
+ (a) You must give any other recipients of the Work or
97
+ Derivative Works a copy of this License; and
98
+
99
+ (b) You must cause any modified files to carry prominent notices
100
+ stating that You changed the files; and
101
+
102
+ (c) You must retain, in the Source form of any Derivative Works
103
+ that You distribute, all copyright, patent, trademark, and
104
+ attribution notices from the Source form of the Work,
105
+ excluding those notices that do not pertain to any part of
106
+ the Derivative Works; and
107
+
108
+ (d) If the Work includes a "NOTICE" text file as part of its
109
+ distribution, then any Derivative Works that You distribute must
110
+ include a readable copy of the attribution notices contained
111
+ within such NOTICE file, excluding those notices that do not
112
+ pertain to any part of the Derivative Works, in at least one
113
+ of the following places: within a NOTICE text file distributed
114
+ as part of the Derivative Works; within the Source form or
115
+ documentation, if provided along with the Derivative Works; or,
116
+ within a display generated by the Derivative Works, if and
117
+ wherever such third-party notices normally appear. The contents
118
+ of the NOTICE file are for informational purposes only and
119
+ do not modify the License. You may add Your own attribution
120
+ notices within Derivative Works that You distribute, alongside
121
+ or as an addendum to the NOTICE text from the Work, provided
122
+ that such additional attribution notices cannot be construed
123
+ as modifying the License.
124
+
125
+ You may add Your own copyright statement to Your modifications and
126
+ may provide additional or different license terms and conditions
127
+ for use, reproduction, or distribution of Your modifications, or
128
+ for any such Derivative Works as a whole, provided Your use,
129
+ reproduction, and distribution of the Work otherwise complies with
130
+ the conditions stated in this License.
131
+
132
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
133
+ any Contribution intentionally submitted for inclusion in the Work
134
+ by You to the Licensor shall be under the terms and conditions of
135
+ this License, without any additional terms or conditions.
136
+ Notwithstanding the above, nothing herein shall supersede or modify
137
+ the terms of any separate license agreement you may have executed
138
+ with Licensor regarding such Contributions.
139
+
140
+ 6. Trademarks. This License does not grant permission to use the trade
141
+ names, trademarks, service marks, or product names of the Licensor,
142
+ except as required for reasonable and customary use in describing the
143
+ origin of the Work and reproducing the content of the NOTICE file.
144
+
145
+ 7. Disclaimer of Warranty. Unless required by applicable law or
146
+ agreed to in writing, Licensor provides the Work (and each
147
+ Contributor provides its Contributions) on an "AS IS" BASIS,
148
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
149
+ implied, including, without limitation, any warranties or conditions
150
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
151
+ PARTICULAR PURPOSE. You are solely responsible for determining the
152
+ appropriateness of using or redistributing the Work and assume any
153
+ risks associated with Your exercise of permissions under this License.
154
+
155
+ 8. Limitation of Liability. In no event and under no legal theory,
156
+ whether in tort (including negligence), contract, or otherwise,
157
+ unless required by applicable law (such as deliberate and grossly
158
+ negligent acts) or agreed to in writing, shall any Contributor be
159
+ liable to You for damages, including any direct, indirect, special,
160
+ incidental, or consequential damages of any character arising as a
161
+ result of this License or out of the use or inability to use the
162
+ Work (including but not limited to damages for loss of goodwill,
163
+ work stoppage, computer failure or malfunction, or any and all
164
+ other commercial damages or losses), even if such Contributor
165
+ has been advised of the possibility of such damages.
166
+
167
+ 9. Accepting Warranty or Additional Liability. While redistributing
168
+ the Work or Derivative Works thereof, You may choose to offer,
169
+ and charge a fee for, acceptance of support, warranty, indemnity,
170
+ or other liability obligations and/or rights consistent with this
171
+ License. However, in accepting such obligations, You may act only
172
+ on Your own behalf and on Your sole responsibility, not on behalf
173
+ of any other Contributor, and only if You agree to indemnify,
174
+ defend, and hold each Contributor harmless for any liability
175
+ incurred by, or claims asserted against, such Contributor by reason
176
+ of your accepting any such warranty or additional liability.
177
+
178
+ END OF TERMS AND CONDITIONS
179
+
180
+ APPENDIX: How to apply the Apache License to your work.
181
+
182
+ To apply the Apache License to your work, attach the following
183
+ boilerplate notice, with the fields enclosed by brackets "[]"
184
+ replaced with your own identifying information. (Don't include
185
+ the brackets!) The text should be enclosed in the appropriate
186
+ comment syntax for the file format. We also recommend that a
187
+ file or class name and description of purpose be included on the
188
+ same "printed page" as the copyright notice for easier
189
+ identification within third-party archives.
190
+
191
+ Copyright [yyyy] [name of copyright owner]
192
+
193
+ Licensed under the Apache License, Version 2.0 (the "License");
194
+ you may not use this file except in compliance with the License.
195
+ You may obtain a copy of the License at
196
+
197
+ http://www.apache.org/licenses/LICENSE-2.0
198
+
199
+ Unless required by applicable law or agreed to in writing, software
200
+ distributed under the License is distributed on an "AS IS" BASIS,
201
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
202
+ See the License for the specific language governing permissions and
203
+ limitations under the License.
@@ -0,0 +1,384 @@
1
+ Metadata-Version: 2.4
2
+ Name: fastpilot
3
+ Version: 0.1.0
4
+ Summary: 让 Python 业务对象拥有统一的本地与远程控制、观察形状
5
+ Keywords: runtime,event-driven,ipython,remote-control
6
+ Author: 弥澄亮
7
+ Author-email: 弥澄亮 <t103ooooo@stu.mju.edu.cn>
8
+ License-Expression: Apache-2.0
9
+ License-File: LICENSE
10
+ Classifier: Development Status :: 3 - Alpha
11
+ Classifier: Intended Audience :: Developers
12
+ Classifier: Programming Language :: Python :: 3
13
+ Classifier: Programming Language :: Python :: 3.13
14
+ Classifier: Topic :: Software Development :: Libraries
15
+ Requires-Dist: ipython>=9.0 ; extra == 'ipython'
16
+ Requires-Dist: httpx>=0.28,<1 ; extra == 'remote'
17
+ Requires-Dist: httpx-sse>=0.4.3,<0.5 ; extra == 'remote'
18
+ Requires-Dist: tenacity>=9.1,<10 ; extra == 'remote'
19
+ Requires-Dist: fastapi>=0.141,<1 ; extra == 'service'
20
+ Requires-Dist: uvicorn>=0.52,<1 ; extra == 'service'
21
+ Requires-Dist: pydantic>=2.13,<3 ; extra == 'service'
22
+ Requires-Dist: sse-starlette>=3.4,<4 ; extra == 'service'
23
+ Requires-Python: >=3.13
24
+ Provides-Extra: ipython
25
+ Provides-Extra: remote
26
+ Provides-Extra: service
27
+ Description-Content-Type: text/markdown
28
+
29
+ # FastPilot
30
+
31
+ FastPilot 让一个持续存在的 Python 业务对象,拥有统一的控制、观察与通信方式。
32
+
33
+ 它适合这样的开发过程:你先在 IPython 里写出一个普通业务对象,然后逐步让
34
+ 它能够接收类型化命令、保存需要继续判断的数据、发布领域事实、接受未来动作,
35
+ 并在需要时由浏览器、远程 Python 或自动程序继续推动。
36
+
37
+ 这段过程中,业务保持为你的 Python 代码。FastPilot 提供的是对象周围那条
38
+ 可以继续观察和控制的公共因果链。
39
+
40
+ ## 安装
41
+
42
+ FastPilot 需要 Python 3.13 或更高版本:
43
+
44
+ ```bash
45
+ pip install fastpilot
46
+ ```
47
+
48
+ 核心包不强制安装网络、服务或 IPython 依赖。需要对应能力时,再安装可选依赖:
49
+
50
+ ```bash
51
+ pip install "fastpilot[ipython]"
52
+ pip install "fastpilot[remote]"
53
+ pip install "fastpilot[service]"
54
+ ```
55
+
56
+ ```text
57
+ Command
58
+
59
+ Participant
60
+
61
+ Runtime
62
+
63
+ Fact / Event / Snapshot / CommandReceipt
64
+
65
+ IPython、远程 Python、浏览器、自动程序
66
+ ```
67
+
68
+ ## 先看一种日常写法
69
+
70
+ 假设应用已经有一个普通的目录 SDK。现在我们想把“刷新目录”建模成一项可以
71
+ 观察的业务动作,并保留后续判断需要的计数。
72
+
73
+ 下面的代码可以按 IPython 的 `# %%` cell 逐段执行,也可以保存成普通 Python
74
+ 脚本运行。每个 cell 都继续使用前面已经创建的对象。
75
+
76
+ ### 第一格:声明控制输入和领域事实
77
+
78
+ ```python
79
+ # %%
80
+ from dataclasses import dataclass
81
+ from typing import Literal
82
+
83
+ from fastpilot import (
84
+ Command,
85
+ Fact,
86
+ ParticipantContext,
87
+ command,
88
+ emits,
89
+ fact,
90
+ handles,
91
+ make_runtime,
92
+ )
93
+
94
+
95
+ @command("catalog.refresh")
96
+ @dataclass(frozen=True, slots=True)
97
+ class RefreshCatalog(Command):
98
+ scope: Literal["recent", "all"]
99
+
100
+
101
+ @fact("catalog.refreshed")
102
+ @dataclass(frozen=True, slots=True)
103
+ class CatalogRefreshed(Fact):
104
+ count: int
105
+ scope: str
106
+ ```
107
+
108
+ `RefreshCatalog` 表达“希望目录刷新”的控制输入;`CatalogRefreshed` 表达
109
+ “目录已经完成刷新”的领域事实。它们都是普通的不可变 Python dataclass,字段
110
+ 就是业务需要传递的信息。
111
+
112
+ ### 第二格:把已有业务对象接入 Runtime
113
+
114
+ ```python
115
+ # %%
116
+ class CatalogClient:
117
+ def fetch_catalog(self, scope: str) -> list[str]:
118
+ records = ["book", "pen", "lamp"]
119
+ return records if scope == "all" else records[:2]
120
+
121
+
122
+ class CatalogRefresher:
123
+ initial_data = {"refresh_count": 0}
124
+
125
+ def __init__(self, client: CatalogClient) -> None:
126
+ self.client = client
127
+
128
+ @handles
129
+ @emits(CatalogRefreshed)
130
+ def refresh(
131
+ self,
132
+ context: ParticipantContext,
133
+ command: RefreshCatalog,
134
+ ) -> None:
135
+ records = self.client.fetch_catalog(command.scope)
136
+ refresh_count = context.read_data("refresh_count", 0)
137
+ assert isinstance(refresh_count, int)
138
+
139
+ context.write_data("last_count", len(records))
140
+ context.write_data("refresh_count", refresh_count + 1)
141
+ context.emit(CatalogRefreshed(len(records), command.scope))
142
+
143
+
144
+ catalog = make_runtime(CatalogRefresher(CatalogClient()))
145
+ ```
146
+
147
+ `CatalogRefresher` 是普通 Python 类。它通过构造参数持有已有的
148
+ `CatalogClient`,业务依赖由应用自己组织。
149
+
150
+ `@handles` 把 `RefreshCatalog` 交给 `refresh`;`@emits` 声明这个 handler
151
+ 可以发布 `CatalogRefreshed`。`context` 是这次控制轮次使用的工作材料:
152
+ 成功结束以后,写入的数据和发布的事实一起进入 Runtime 的新边界。
153
+
154
+ ### 第三格:观察并推动一次控制轮次
155
+
156
+ ```python
157
+ # %%
158
+ refreshed = catalog.subscribe(CatalogRefreshed)
159
+
160
+ catalog.start()
161
+ receipt = catalog.dispatch(RefreshCatalog("recent"))
162
+
163
+ print(receipt.outcome)
164
+ print(dict(receipt.snapshot.data))
165
+ print(refreshed.next(timeout=1).fact)
166
+ ```
167
+
168
+ 你会得到类似这样的结果:
169
+
170
+ ```text
171
+ completed
172
+ {'refresh_count': 1, 'last_count': 2}
173
+ CatalogRefreshed(count=2, scope='recent')
174
+ ```
175
+
176
+ 这里有三种互相配合的观察方式:
177
+
178
+ * `receipt` 说明这一轮命令的结果;
179
+ * `receipt.snapshot` 保存这一轮结束时的完整边界;
180
+ * `subscribe()` 让另一个消费者沿自己的游标读取事实。
181
+
182
+ 下一格可以继续使用 `catalog`、`receipt` 和 `refreshed`。这就是 FastPilot
183
+ 最常见的工作感受:先让对象活在工作台里,再根据当前观察决定下一步。
184
+
185
+ ## 这段代码解决了什么问题
186
+
187
+ 普通函数通常适合这样的调用:
188
+
189
+ ```python
190
+ result = refresh_catalog(scope="recent")
191
+ ```
192
+
193
+ 当业务开始需要持续存在时,调用者还会遇到另外一些问题:
194
+
195
+ * 上一次动作留下了什么数据?
196
+ * 这次命令实际产生了哪些事实?
197
+ * 另一个消费者怎样从自己的位置继续观察?
198
+ * 一个动作能不能稍后执行、重排或取消?
199
+ * 浏览器和远程 Python 怎样继续控制同一个对象?
200
+
201
+ FastPilot 把这些信息组织成 Runtime 的控制与观察边界。业务代码决定
202
+ 自己的 Command、Fact 和数据;Runtime 负责把每次控制动作收束为可以继续
203
+ 判断的 `CommandReceipt`、`Event` 和 `Snapshot`。
204
+
205
+ ## 三个需要记住的角色
206
+
207
+ ### Participant:你的业务规则
208
+
209
+ Participant 是普通 Python 对象。它拥有领域依赖和处理 Command 的方法:
210
+
211
+ ```python
212
+ class CatalogRefresher:
213
+ @handles
214
+ @emits(CatalogRefreshed)
215
+ def refresh(self, context: ParticipantContext, command: RefreshCatalog) -> None:
216
+ ...
217
+ ```
218
+
219
+ 你可以从一个只有事件处理的 Participant 开始;当业务确实需要逻辑时间、
220
+ 状态图或未来队列时,再让它声明对应能力。
221
+
222
+ ### Runtime:持续存在的业务世界
223
+
224
+ ```python
225
+ catalog = make_runtime(CatalogRefresher(CatalogClient()))
226
+
227
+ catalog.start()
228
+ catalog.dispatch(RefreshCatalog("recent"))
229
+ catalog.snapshot()
230
+ catalog.history()
231
+ ```
232
+
233
+ Runtime 保存当前生命周期中的领域数据、事实历史、观察游标、队列、转换草稿、
234
+ 可选逻辑时间和子 Runtime。它是同一条控制链的权威拥有者。
235
+
236
+ ### Driver:根据观察选择下一步的人或程序
237
+
238
+ Driver 是一个角色,可以由下面这些对象承担:
239
+
240
+ * 持久 IPython 工作台;
241
+ * 远程 Python 程序;
242
+ * 浏览器控制台;
243
+ * 根据事件自动发送下一条命令的程序。
244
+
245
+ 它们都可以沿着同一个 Runtime 的 `dispatch()`、`snapshot()`、`history()` 和
246
+ `subscribe()` 继续工作。
247
+
248
+ ## 让未来动作先成为一个值
249
+
250
+ Command 可以立即执行,也可以登记为未来意图:
251
+
252
+ ```python
253
+ # %%
254
+ from fastpilot import AtTime
255
+ from fastpilot.contrib import SetEnergy, make_robot
256
+
257
+ robot = make_robot()
258
+ robot.start()
259
+ entry = robot.enqueue(
260
+ SetEnergy(50),
261
+ trigger=AtTime(20),
262
+ )
263
+
264
+ robot.move_entry(entry.id, 0)
265
+ robot.cancel_entry(entry.id, "计划改变")
266
+ ```
267
+
268
+ 队列条目拥有稳定身份,因此未来动作可以被观察、重排、取消,并在历史中留下
269
+ 自己的终态。事件驱动业务可以直接停留在前面的几格代码;需要时间能力的业务
270
+ 再进入 Trigger 和队列。
271
+
272
+ ## 需要状态图时,再加入转换站
273
+
274
+ 状态图用于表达领域允许的状态边:
275
+
276
+ ```python
277
+ # %%
278
+ from fastpilot import StateChart
279
+
280
+ statechart = StateChart.create(
281
+ initial="IDLE",
282
+ transitions={
283
+ "IDLE": {"WALKING"},
284
+ "WALKING": {"RESTING"},
285
+ "RESTING": {"WALKING"},
286
+ },
287
+ )
288
+ ```
289
+
290
+ 这个值可以由 Participant 作为自己的状态图提供:
291
+
292
+ ```python
293
+ class RobotParticipant:
294
+ statechart = statechart
295
+
296
+ # 这里继续放置 Walk、Obstacle 等领域 Command 的 handler。
297
+ ...
298
+ ```
299
+
300
+ 当领域 Command 提出状态变化时,Runtime 可以把下一条状态边停在一个可观察、
301
+ 可编辑的 `TransitionDraft`:
302
+
303
+ ```python
304
+ # %%
305
+ from fastpilot.contrib import Walk, make_robot
306
+
307
+ robot = make_robot()
308
+ robot.dispatch(Walk())
309
+
310
+ assert robot.pending is not None
311
+ robot.pending.context["operator_note"] = "沿北侧通道"
312
+ robot.commit()
313
+ robot.resume()
314
+ ```
315
+
316
+ 这里的日常表达很直接:先看提案,再补充工作材料,提交状态边,最后恢复后续
317
+ 调度。需要这类人工介入的业务,可以在 `examples/03-robot` 中继续阅读。
318
+
319
+ ## 需要网络时,让同一个对象打开入口
320
+
321
+ 本地 Runtime 可以在当前进程中打开 HTTP/SSE 入口:
322
+
323
+ ```python
324
+ # %%
325
+ service = catalog.serve(port=0)
326
+ print(service.url)
327
+ ```
328
+
329
+ 另一个 Python 进程使用同一个协议连接它:
330
+
331
+ ```python
332
+ from fastpilot import connect
333
+
334
+ with connect(service.url) as remote:
335
+ remote.dispatch(RefreshCatalog("all"))
336
+ ```
337
+
338
+ 远程调用发送同一种 `RefreshCatalog` Command,服务端由同一个 Runtime
339
+ 完成解释、记录事件和生成快照。
340
+
341
+ 如果应用本来就有 FastAPI,可以把控制面挂入已有 ASGI 应用:
342
+
343
+ ```python
344
+ from typing import Literal
345
+
346
+ from fastapi import FastAPI
347
+ from pydantic import BaseModel
348
+ from fastpilot.service import mount_runtime, serve_app
349
+
350
+
351
+ class RefreshRequest(BaseModel):
352
+ scope: Literal["recent", "all"]
353
+
354
+
355
+ app = FastAPI()
356
+ mount_runtime(app, catalog, prefix="/_pilot")
357
+
358
+
359
+ @app.post("/catalog/refresh")
360
+ def refresh(request: RefreshRequest) -> dict[str, object]:
361
+ receipt = catalog.dispatch(RefreshCatalog(request.scope))
362
+ return {
363
+ "outcome": receipt.outcome,
364
+ "sequence": receipt.snapshot.sequence,
365
+ }
366
+
367
+
368
+ service = serve_app(app, port=0)
369
+ ```
370
+
371
+ 这段组合里:
372
+
373
+ * `RefreshRequest` 是 HTTP 边界模型;
374
+ * `RefreshCatalog` 是 Runtime 控制意图;
375
+ * `/catalog/refresh` 是应用自己的业务地址;
376
+ * `/_pilot/v1/*` 是同一个 Runtime 的通用控制与观察地址。
377
+
378
+ 普通 FastAPI 路由与 FastPilot 控制面因此可以在一个应用里共同工作。
379
+
380
+ ## 从哪里继续
381
+
382
+ 这份 README 先覆盖安装和最小使用旅程。仓库中的 `examples/`、`docs/`、
383
+ `tests/` 和 `CONTRIBUTING.md` 属于开发维护材料,当前不随 PyPI 发布包提供;
384
+ 它们会在整理成熟后再决定是否公开。