mirror of
https://github.com/microsoft/agent-framework.git
synced 2026-06-16 21:04:09 +08:00
94c5d59984
* feat: ModelClient and content types * refactor: Pythonify ChatResponseFormat and ChatRole * feat: Add guardrail interfaces * refactor: Remove CancellationToken * feat: Solidify the Usage APIs * Adds well-known keys for additional_counts, and guidance for how to avoid collisions between providers * Implement sum-aggregation for usage * refactor: Move AITool out of model_client * refactor: Copy editing * fix: CI checks (pyupgrade, ruff, etc.) * ci: Fix pre-commit to use pyright in uv venv The existing pyright precommit hook inside of python-pyright is no longer being maintained by the owner (see https://github.com/RobertCraigie/pyright-python/issues/265) The fix is to define the hook ourselves, relying on `uv run` to drive it. In order for that to work right we need to use the "system" language to break out of the sandbox. * fix: Pyright error fixes * docs: Update models and types design docs * Python: Refinement of content types and model client (#112) * refinement of structure and buildup with ports from semantigen * refined the data and uri contents * refined chat response and updates * moved things and added tests * moved out of src folder * fixed imports and tests * small tweaks * missing build system * upgrade * add mypy * fixed typing for types * fix tests * fixed tool * disable json checks on vscode * remove print --------- Co-authored-by: Eduard van Valkenburg <eavanvalkenburg@users.noreply.github.com> Co-authored-by: eavanvalkenburg <github@vanvalkenburg.eu>
51 lines
1.6 KiB
Python
51 lines
1.6 KiB
Python
# Copyright (c) Microsoft. All rights reserved.
|
|
|
|
import threading
|
|
from asyncio import Future
|
|
from collections.abc import Callable
|
|
from typing import Any
|
|
|
|
|
|
# from https://github.com/microsoft/autogen/blob/main/python/packages/autogen-core/src/autogen_core/_cancellation_token.py
|
|
class CancellationToken:
|
|
"""A token used to cancel pending async calls."""
|
|
|
|
def __init__(self) -> None:
|
|
self._cancelled: bool = False
|
|
self._lock: threading.Lock = threading.Lock()
|
|
self._callbacks: list[Callable[[], None]] = []
|
|
|
|
def cancel(self) -> None:
|
|
"""Cancel pending async calls linked to this cancellation token."""
|
|
with self._lock:
|
|
if not self._cancelled:
|
|
self._cancelled = True
|
|
for callback in self._callbacks:
|
|
callback()
|
|
|
|
def is_cancelled(self) -> bool:
|
|
"""Check if the CancellationToken has been used."""
|
|
with self._lock:
|
|
return self._cancelled
|
|
|
|
def add_callback(self, callback: Callable[[], None]) -> None:
|
|
"""Attach a callback that will be called when cancel is invoked."""
|
|
with self._lock:
|
|
if self._cancelled:
|
|
callback()
|
|
else:
|
|
self._callbacks.append(callback)
|
|
|
|
def link_future(self, future: Future[Any]) -> Future[Any]:
|
|
"""Link a pending async call to a token to allow its cancellation."""
|
|
with self._lock:
|
|
if self._cancelled:
|
|
future.cancel()
|
|
else:
|
|
|
|
def _cancel() -> None:
|
|
future.cancel()
|
|
|
|
self._callbacks.append(_cancel)
|
|
return future
|