fabricatio 0.2.2__cp312-cp312-manylinux_2_34_x86_64.whl → 0.2.3.dev0__cp312-cp312-manylinux_2_34_x86_64.whl

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.
fabricatio/core.py CHANGED
@@ -38,11 +38,11 @@ class Env(BaseModel):
38
38
 
39
39
  @overload
40
40
  def on[**P, R](
41
- self,
42
- event: str | Event,
43
- func: Optional[Callable[P, R]] = None,
44
- /,
45
- ttl: int = -1,
41
+ self,
42
+ event: str | Event,
43
+ func: Optional[Callable[P, R]] = None,
44
+ /,
45
+ ttl: int = -1,
46
46
  ) -> Callable[[Callable[P, R]], Callable[P, R]]:
47
47
  """
48
48
  Registers an event listener with a specific function that listens indefinitely or for a specified number of times.
@@ -58,11 +58,11 @@ class Env(BaseModel):
58
58
  ...
59
59
 
60
60
  def on[**P, R](
61
- self,
62
- event: str | Event,
63
- func: Optional[Callable[P, R]] = None,
64
- /,
65
- ttl=-1,
61
+ self,
62
+ event: str | Event,
63
+ func: Optional[Callable[P, R]] = None,
64
+ /,
65
+ ttl=-1,
66
66
  ) -> Callable[[Callable[P, R]], Callable[P, R]] | Self:
67
67
  """Registers an event listener with a specific function that listens indefinitely or for a specified number of times.
68
68
 
@@ -78,14 +78,13 @@ class Env(BaseModel):
78
78
  event = event.collapse()
79
79
  if func is None:
80
80
  return self._ee.on(event, ttl=ttl)
81
-
82
81
  self._ee.on(event, func, ttl=ttl)
83
82
  return self
84
83
 
85
84
  @overload
86
85
  def once[**P, R](
87
- self,
88
- event: str | Event,
86
+ self,
87
+ event: str | Event,
89
88
  ) -> Callable[[Callable[P, R]], Callable[P, R]]:
90
89
  """
91
90
  Registers an event listener that listens only once.
@@ -100,9 +99,9 @@ class Env(BaseModel):
100
99
 
101
100
  @overload
102
101
  def once[**P, R](
103
- self,
104
- event: str | Event,
105
- func: Callable[[Callable[P, R]], Callable[P, R]],
102
+ self,
103
+ event: str | Event,
104
+ func: Callable[[Callable[P, R]], Callable[P, R]],
106
105
  ) -> Self:
107
106
  """
108
107
  Registers an event listener with a specific function that listens only once.
@@ -117,9 +116,9 @@ class Env(BaseModel):
117
116
  ...
118
117
 
119
118
  def once[**P, R](
120
- self,
121
- event: str | Event,
122
- func: Optional[Callable[P, R]] = None,
119
+ self,
120
+ event: str | Event,
121
+ func: Optional[Callable[P, R]] = None,
123
122
  ) -> Callable[[Callable[P, R]], Callable[P, R]] | Self:
124
123
  """Registers an event listener with a specific function that listens only once.
125
124
 
@@ -163,5 +162,20 @@ class Env(BaseModel):
163
162
  event = event.collapse()
164
163
  return await self._ee.emit_async(event, *args, **kwargs)
165
164
 
165
+ def emit_future[**P](self, event: str | Event, *args: P.args, **kwargs: P.kwargs) -> None:
166
+ """Emits an event to all registered listeners and returns a future object.
167
+
168
+ Args:
169
+ event (str | Event): The event to emit.
170
+ *args: Positional arguments to pass to the listeners.
171
+ **kwargs: Keyword arguments to pass to the listeners.
172
+
173
+ Returns:
174
+ None: The future object.
175
+ """
176
+ if isinstance(event, Event):
177
+ event = event.collapse()
178
+ return self._ee.emit_future(event, *args, **kwargs)
179
+
166
180
 
167
181
  env = Env()
@@ -2,7 +2,7 @@
2
2
 
3
3
  import traceback
4
4
  from abc import abstractmethod
5
- from asyncio import Queue
5
+ from asyncio import Queue, create_task
6
6
  from typing import Any, Dict, Self, Tuple, Type, Union, Unpack
7
7
 
8
8
  from fabricatio.capabilities.rating import GiveRating
@@ -108,7 +108,11 @@ class WorkFlow(WithBriefing, ToolBoxUsage):
108
108
  try:
109
109
  for step in self._instances:
110
110
  logger.debug(f"Executing step: {step.name}")
111
- modified_ctx = await step.act(await self._context.get())
111
+ act_task = create_task(step.act(await self._context.get()))
112
+ if task.is_cancelled():
113
+ act_task.cancel(f"Cancelled by task: {task.name}")
114
+ break
115
+ modified_ctx = await act_task
112
116
  await self._context.put(modified_ctx)
113
117
  current_action = step.name
114
118
  logger.info(f"Finished executing workflow: {self.name}")
fabricatio/models/task.py CHANGED
@@ -60,7 +60,7 @@ class Task[T](WithBriefing, WithJsonExample, WithDependency):
60
60
  dependencies: List[str] = Field(default_factory=list)
61
61
  """A list of file paths, These file are needed to read or write to meet a specific requirement of this task, if it is not directly given out, it SHALL just be a empty list meaning `NOT ASSIGNED`"""
62
62
 
63
- _output: Queue = PrivateAttr(default_factory=lambda: Queue(maxsize=1))
63
+ _output: Queue[T | None] = PrivateAttr(default_factory=Queue)
64
64
  """The output queue of the task."""
65
65
 
66
66
  _status: TaskStatus = PrivateAttr(default=TaskStatus.Pending)
@@ -131,7 +131,7 @@ class Task[T](WithBriefing, WithJsonExample, WithDependency):
131
131
  self.description = description
132
132
  return self
133
133
 
134
- async def get_output(self) -> T:
134
+ async def get_output(self) -> T | None:
135
135
  """Get the output of the task.
136
136
 
137
137
  Returns:
@@ -232,6 +232,7 @@ class Task[T](WithBriefing, WithJsonExample, WithDependency):
232
232
  """
233
233
  logger.info(f"Cancelling task `{self.name}`")
234
234
  self._status = TaskStatus.Cancelled
235
+ await self._output.put(None)
235
236
  await env.emit_async(self.cancelled_label, self)
236
237
  return self
237
238
 
@@ -243,27 +244,38 @@ class Task[T](WithBriefing, WithJsonExample, WithDependency):
243
244
  """
244
245
  logger.info(f"Failing task `{self.name}`")
245
246
  self._status = TaskStatus.Failed
247
+ await self._output.put(None)
246
248
  await env.emit_async(self.failed_label, self)
247
249
  return self
248
250
 
249
- async def publish(self) -> Self:
251
+ def publish(self, new_namespace: Optional[EventLike] = None) -> Self:
250
252
  """Publish the task to the event bus.
251
253
 
254
+ Args:
255
+ new_namespace(EventLike, optional): The new namespace to move the task to.
256
+
252
257
  Returns:
253
- Task: The published instance of the `Task` class
258
+ Task: The published instance of the `Task` class.
254
259
  """
260
+ if new_namespace:
261
+ self.move_to(new_namespace)
255
262
  logger.info(f"Publishing task `{(label := self.pending_label)}`")
256
- await env.emit_async(label, self)
263
+ env.emit_future(label, self)
257
264
  return self
258
265
 
259
- async def delegate(self) -> T:
260
- """Delegate the task to the event bus and wait for the output.
266
+ async def delegate(self, new_namespace: Optional[EventLike] = None) -> T | None:
267
+ """Delegate the task to the event.
268
+
269
+ Args:
270
+ new_namespace(EventLike, optional): The new namespace to move the task to.
261
271
 
262
272
  Returns:
263
- T: The output of the task
273
+ T|None: The output of the task.
264
274
  """
275
+ if new_namespace:
276
+ self.move_to(new_namespace)
265
277
  logger.info(f"Delegating task `{(label := self.pending_label)}`")
266
- await env.emit_async(label, self)
278
+ env.emit_future(label, self)
267
279
  return await self.get_output()
268
280
 
269
281
  @property
@@ -277,3 +289,43 @@ class Task[T](WithBriefing, WithJsonExample, WithDependency):
277
289
  configs.templates.task_briefing_template,
278
290
  self.model_dump(),
279
291
  )
292
+
293
+ def is_running(self) -> bool:
294
+ """Check if the task is running.
295
+
296
+ Returns:
297
+ bool: True if the task is running, False otherwise.
298
+ """
299
+ return self._status == TaskStatus.Running
300
+
301
+ def is_finished(self) -> bool:
302
+ """Check if the task is finished.
303
+
304
+ Returns:
305
+ bool: True if the task is finished, False otherwise.
306
+ """
307
+ return self._status == TaskStatus.Finished
308
+
309
+ def is_failed(self) -> bool:
310
+ """Check if the task is failed.
311
+
312
+ Returns:
313
+ bool: True if the task is failed, False otherwise.
314
+ """
315
+ return self._status == TaskStatus.Failed
316
+
317
+ def is_cancelled(self) -> bool:
318
+ """Check if the task is cancelled.
319
+
320
+ Returns:
321
+ bool: True if the task is cancelled, False otherwise.
322
+ """
323
+ return self._status == TaskStatus.Cancelled
324
+
325
+ def is_pending(self) -> bool:
326
+ """Check if the task is pending.
327
+
328
+ Returns:
329
+ bool: True if the task is pending, False otherwise.
330
+ """
331
+ return self._status == TaskStatus.Pending
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: fabricatio
3
- Version: 0.2.2
3
+ Version: 0.2.3.dev0
4
4
  Classifier: License :: OSI Approved :: MIT License
5
5
  Classifier: Programming Language :: Rust
6
6
  Classifier: Programming Language :: Python :: 3.12
@@ -1,8 +1,8 @@
1
- fabricatio-0.2.2.dist-info/METADATA,sha256=6jYTAggAa5xO9tsHL1XwqJCDKsHq37NcUPjcqxY31YM,11955
2
- fabricatio-0.2.2.dist-info/WHEEL,sha256=RIvmwLDYujv60MYBx2jxyP4vdn1DD7X0kBgz1TQvZuc,108
3
- fabricatio-0.2.2.dist-info/licenses/LICENSE,sha256=yDZaTLnOi03bi3Dk6f5IjhLUc5old2yOsihHWU0z-i0,1067
1
+ fabricatio-0.2.3.dev0.dist-info/METADATA,sha256=SLoJODnhadoGH_p1Bm-lbW2JzuT5j4hzVMRIucpRhhc,11960
2
+ fabricatio-0.2.3.dev0.dist-info/WHEEL,sha256=RIvmwLDYujv60MYBx2jxyP4vdn1DD7X0kBgz1TQvZuc,108
3
+ fabricatio-0.2.3.dev0.dist-info/licenses/LICENSE,sha256=yDZaTLnOi03bi3Dk6f5IjhLUc5old2yOsihHWU0z-i0,1067
4
4
  fabricatio/decorators.py,sha256=cJHsxxbnMhc4SzPl4454CPLuDP3H0qbTrzV_U2rLPrs,6372
5
- fabricatio/core.py,sha256=apwXgI94DCWpGujGlsmXsTZQvJOQMB9llmuUo7ohd-4,5771
5
+ fabricatio/core.py,sha256=MaEKZ6DDmbdScAY-7F1gwGA6fr7ADX6Mz5rNVi2msFA,6277
6
6
  fabricatio/models/generic.py,sha256=4j6DRNkHLNhx8U6aijf0Cz9HSahtEPQBh22i6d_PGTs,4981
7
7
  fabricatio/models/tool.py,sha256=wgNXtobDSWZTVmIrSSaS5oyxhSUYWXhS0S2o9acE2ls,6800
8
8
  fabricatio/models/role.py,sha256=fAYH8EYpt8ad2uKRV9PVjDNqHuGLxY6_s36mXM0S5I0,1782
@@ -10,8 +10,8 @@ fabricatio/models/kwargs_types.py,sha256=S9ABtNLjWp0lknfHRDQ3hnCk3aLRKJuDALvUUBd
10
10
  fabricatio/models/utils.py,sha256=u-eR_v-o0BSFSmCprP3mOyAO-WOyg_P68jjzLjyit_w,2457
11
11
  fabricatio/models/usages.py,sha256=beEX7eHYPol4fB9YVwhxFfgjn9LnDuIEGuAIcHXVQJU,23405
12
12
  fabricatio/models/events.py,sha256=TkMSJYSzRgurbfY2knWIc8gYw7rmaWqB7KeYzy5jeWU,2637
13
- fabricatio/models/task.py,sha256=hLtcjN4CSWIQQ3LIF38VaydE7Dz4aZso--_ma2n8fT4,9626
14
- fabricatio/models/action.py,sha256=T4lcky7JDuKr8qSNpNVbcRkrf5KbewHxhxCxHK11YWs,5581
13
+ fabricatio/models/task.py,sha256=tPeX9i-dPpxQfrkX7xR2ESFlIXUTg2z-TqOVx5_qUCU,11198
14
+ fabricatio/models/action.py,sha256=006Jjfr9p-EBErboAn29yvyolIGi5mbJ_DA5og0Uh64,5780
15
15
  fabricatio/toolboxes/fs.py,sha256=oZjGOmtN8ZbkMXxCMatqvdPavVXyQ87AEDc1lx9jimY,483
16
16
  fabricatio/toolboxes/__init__.py,sha256=iEBsH0aRPLccIe6eyZ6iH5GdK8yYNQTnCwLzw4pWXAA,465
17
17
  fabricatio/toolboxes/arithmetic.py,sha256=sSTPkKI6-mb278DwQKFO9jKyzc9kCx45xNH7V6bGBpE,1307
@@ -31,6 +31,6 @@ fabricatio/parser.py,sha256=Q4laV79Svggl09UKa3lAw6NYPuDz1sP2F-6U4tsSvQw,3429
31
31
  fabricatio/capabilities/rating.py,sha256=KLJlaufG74lHJ5hHI_tjhQgjWjHhO_38Q1ILW2cY9fA,14552
32
32
  fabricatio/capabilities/task.py,sha256=UnkeON3VCADXUBtUmQbMwl9TEwK0yQSED3EglqMMHQ4,5196
33
33
  fabricatio/_rust.pyi,sha256=clhcURuiB9zlFo4m3VyoWQ8Xs4tvg6KNHXpF-ok9h4o,1703
34
- fabricatio/_rust.cpython-312-x86_64-linux-gnu.so,sha256=zuOW9YLJrxii2VOvcedTt_WxWPw6FXWJQIMm8wNB_Yg,1339320
35
- fabricatio-0.2.2.data/scripts/tdown,sha256=P1enq0UfaeGcF3CRC51IYwzkEeUYxWi_C2vsOdAxgVA,4569728
36
- fabricatio-0.2.2.dist-info/RECORD,,
34
+ fabricatio/_rust.cpython-312-x86_64-linux-gnu.so,sha256=RwfP3a0Y0W82yUIHmH-SuNI6ZD_4X1dmu_QoREHvhnw,1336568
35
+ fabricatio-0.2.3.dev0.data/scripts/tdown,sha256=sYeLflc31k_aVFo7_y-QEBe3yfz6uN0PqJ0cLpyMkOU,4569184
36
+ fabricatio-0.2.3.dev0.dist-info/RECORD,,