aboutsummaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
-rw-r--r--pdm.lock16
-rw-r--r--pyproject.toml4
-rw-r--r--src/infini/core.py8
-rw-r--r--src/infini/generator.py4
-rw-r--r--src/infini/handler.py4
-rw-r--r--src/infini/input.py24
-rw-r--r--src/infini/interceptor.py12
-rw-r--r--src/infini/internal.py4
-rw-r--r--src/infini/loader.py29
-rw-r--r--src/infini/register.py28
-rw-r--r--src/infini/typing.py3
11 files changed, 81 insertions, 55 deletions
diff --git a/pdm.lock b/pdm.lock
index f3ca9882..30fc2b9c 100644
--- a/pdm.lock
+++ b/pdm.lock
@@ -127,7 +127,7 @@ files = [
[[package]]
name = "ipdm"
-version = "0.1.2"
+version = "0.1.3"
requires_python = ">=3.10"
summary = "Infini 包管理器"
dependencies = [
@@ -137,8 +137,8 @@ dependencies = [
"typer>=0.9.0",
]
files = [
- {file = "ipdm-0.1.2-py3-none-any.whl", hash = "sha256:7eb5b6f203c8f35deb10ac06c7070c96cb9aaf308eaa420ed8a5576ba14e90cf"},
- {file = "ipdm-0.1.2.tar.gz", hash = "sha256:cba68923b0fad2fbd110d1d6f8617f780a1d00ceaf2381df9fb7f4cfcdfcd261"},
+ {file = "ipdm-0.1.3-py3-none-any.whl", hash = "sha256:5fee9d2c5b52d45c69ddbc38d5bb9a08c595c90eb130aa747892c0b0fb149203"},
+ {file = "ipdm-0.1.3.tar.gz", hash = "sha256:fb62a02a17a068d82598283a12f985f6927445fffed2067066a6589d74a1be13"},
]
[[package]]
@@ -248,20 +248,20 @@ files = [
[[package]]
name = "pytest"
-version = "7.4.4"
-requires_python = ">=3.7"
+version = "8.0.1"
+requires_python = ">=3.8"
summary = "pytest: simple powerful testing with Python"
dependencies = [
"colorama; sys_platform == \"win32\"",
"exceptiongroup>=1.0.0rc8; python_version < \"3.11\"",
"iniconfig",
"packaging",
- "pluggy<2.0,>=0.12",
+ "pluggy<2.0,>=1.3.0",
"tomli>=1.0.0; python_version < \"3.11\"",
]
files = [
- {file = "pytest-7.4.4-py3-none-any.whl", hash = "sha256:b090cdf5ed60bf4c45261be03239c2c1c22df034fbffe691abe93cd80cea01d8"},
- {file = "pytest-7.4.4.tar.gz", hash = "sha256:2cf0005922c6ace4a3e2ec8b4080eb0d9753fdc93107415332f50ce9e7994280"},
+ {file = "pytest-8.0.1-py3-none-any.whl", hash = "sha256:3e4f16fe1c0a9dc9d9389161c127c3edc5d810c38d6793042fb81d9f48a59fca"},
+ {file = "pytest-8.0.1.tar.gz", hash = "sha256:267f6563751877d772019b13aacbe4e860d73fe8f651f28112e9ac37de7513ae"},
]
[[package]]
diff --git a/pyproject.toml b/pyproject.toml
index 408c69d4..3dd8d94f 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -1,6 +1,6 @@
[project]
name = "infini"
-version = "2.0.4"
+version = "2.0.5"
description = "Infini 核心标准文本输入输出模块"
authors = [
{ name = "苏向夜", email = "fu050409@163.com" },
@@ -10,7 +10,7 @@ dependencies = [
"rich>=13.7.0",
"jinja2>=3.1.3",
]
-requires-python = ">=3.10"
+requires-python = ">=3.8"
readme = "README.md"
license = { text = "MIT" }
diff --git a/src/infini/core.py b/src/infini/core.py
index 12a49c03..8fb46752 100644
--- a/src/infini/core.py
+++ b/src/infini/core.py
@@ -3,7 +3,7 @@ from infini.interceptor import Interceptor
from infini.generator import TextGenerator
from infini.handler import Handler
from infini.output import Output
-from infini.typing import Any, Generator
+from infini.typing import Any, Generator, Union
from infini.exceptions import ValueError
@@ -15,7 +15,7 @@ class Core:
def input(
self, input: Input
- ) -> Generator[str | Output, Any, None]: # TODO 支持Workflow
+ ) -> Generator[Union[str, Output], Any, None]: # TODO 支持Workflow
for pre_intercepted_stream in self.pre_intercept(input):
if isinstance(pre_intercepted_stream, Output):
if not isinstance(pre_intercepted_stream, Output):
@@ -51,7 +51,7 @@ class Core:
if handled_stream.block:
return
- def pre_intercept(self, input: Input) -> Generator[Output | Input, Any, None]:
+ def pre_intercept(self, input: Input) -> Generator[Union[Input, Output], Any, None]:
return self.pre_interceptor.input(input)
def handle(self, input: Input) -> Generator[Output, Any, None]:
@@ -62,5 +62,5 @@ class Core:
def generate(self, output: Output) -> str:
return self.generator.output(output)
- def intercept(self, output_text: str) -> Generator[Output | str, Any, None]:
+ def intercept(self, output_text: str) -> Generator[Union[str, Output], Any, None]:
return self.interceptor.output(output_text)
diff --git a/src/infini/generator.py b/src/infini/generator.py
index cc752d70..53ca1a69 100644
--- a/src/infini/generator.py
+++ b/src/infini/generator.py
@@ -1,12 +1,12 @@
from infini.output import Output
-from infini.typing import Dict, Callable
+from infini.typing import Dict, Callable, Union
from infini.exceptions import UnknownEvent
from jinja2 import Template
class TextGenerator: # TODO 兼容多类型事件
events: Dict[str, str]
- global_variables: Dict[str, str | Callable]
+ global_variables: Dict[str, Union[str, Callable]]
def __init__(self) -> None:
self.events = {}
diff --git a/src/infini/handler.py b/src/infini/handler.py
index 79686485..0d8d1fe0 100644
--- a/src/infini/handler.py
+++ b/src/infini/handler.py
@@ -1,6 +1,6 @@
from infini.input import Input
from infini.output import Output
-from infini.typing import List, Any, RouterType, Callable, Generator
+from infini.typing import List, Any, RouterType, Callable, Generator, Union
from infini.queue import EventQueue
@@ -24,7 +24,7 @@ class Handler:
def match(
self, text: str
- ) -> EventQueue[Callable[[Input], Output | Generator[Output, Any, None]]]:
+ ) -> EventQueue[Callable[[Input], Union[Output, Generator[Output, Any, None]]]]:
queue = EventQueue()
for handler in self.handlers:
diff --git a/src/infini/input.py b/src/infini/input.py
index 682e1037..30683a4b 100644
--- a/src/infini/input.py
+++ b/src/infini/input.py
@@ -1,14 +1,21 @@
-from infini.typing import Dict, Any, Generic, T
+from infini.typing import Literal, Optional
+from infini.typing import Dict, Any, Generic, T, Optional
+from infini.output import Output
class Input(Generic[T]):
plain_data: T
variables: Dict[str, Any]
- def __init__(self, plain_data: T, variables: Dict[str, Any] | None = None) -> None:
+ def __init__(
+ self, plain_data: T, variables: Optional[Dict[str, Any]] = None
+ ) -> None:
self.plain_data = plain_data
self.variables = variables or {}
+ def get_user_id(self) -> Optional[str]:
+ return self.variables.get("user_id")
+
def get_session_id(self) -> str:
if session_id := self.variables.get("session_id"):
return session_id
@@ -19,3 +26,16 @@ class Input(Generic[T]):
def get_plain_text(self) -> str:
return str(self.plain_data)
+
+ def output(
+ self,
+ type: Literal["text", "workflow"],
+ name: str,
+ *,
+ status: int = 0,
+ block: bool = False,
+ variables: Dict[str, Any] = {},
+ ):
+ vars = self.variables.copy()
+ vars.update(variables)
+ return Output(type, name, status=status, block=block, variables=vars)
diff --git a/src/infini/interceptor.py b/src/infini/interceptor.py
index c8866ec0..bffd3f8e 100644
--- a/src/infini/interceptor.py
+++ b/src/infini/interceptor.py
@@ -1,13 +1,13 @@
from infini.input import Input
from infini.output import Output
-from infini.typing import List, Any, RouterType, Callable, Generator
+from infini.typing import List, Any, RouterType, Callable, Generator, Union
from infini.queue import EventQueue
class Interceptor:
interceptors: List[RouterType]
- def input(self, input: Input) -> Generator[Output | Input, Any, None]:
+ def input(self, input: Input) -> Generator[Union[Output, Input], Any, None]:
queue = self.match(input.get_plain_text())
while not queue.is_empty():
if isinstance(stream := queue.pop()(input), Generator):
@@ -30,9 +30,7 @@ class Interceptor:
input = stream
yield input
- def output(
- self, output_text: str
- ) -> Generator[Output | str, Any, None]:
+ def output(self, output_text: str) -> Generator[Union[Output, str], Any, None]:
input = Input(output_text)
queue = self.match(input.get_plain_text())
while not queue.is_empty():
@@ -59,7 +57,9 @@ class Interceptor:
def match(
self, text: str
) -> EventQueue[
- Callable[[Input], Input | Output | Generator[Input | Output, Any, None]]
+ Callable[
+ [Input], Union[Input, Output, Generator[Union[Input, Output], Any, None]]
+ ]
]:
queue = EventQueue()
diff --git a/src/infini/internal.py b/src/infini/internal.py
index 5924ad01..e30bda63 100644
--- a/src/infini/internal.py
+++ b/src/infini/internal.py
@@ -1,13 +1,13 @@
from infini.loader import Loader
from infini.register import Register
-from infini.typing import List
+from infini.typing import List, Optional
from pathlib import Path
import sys
import inspect
-def require(name: str, paths: List | None = None) -> Register:
+def require(name: str, paths: Optional[List] = None) -> Register:
caller_frame = inspect.stack()[1][0]
caller_file = caller_frame.f_globals["__file__"]
diff --git a/src/infini/loader.py b/src/infini/loader.py
index 36af2f0a..4dbc392e 100644
--- a/src/infini/loader.py
+++ b/src/infini/loader.py
@@ -4,7 +4,16 @@ from infini.generator import TextGenerator
from infini.handler import Handler
from infini.interceptor import Interceptor
from infini.register import Register
-from infini.typing import List, Dict, Sequence, ModuleType, RouterType, Callable
+from infini.typing import (
+ List,
+ Dict,
+ Sequence,
+ ModuleType,
+ RouterType,
+ Callable,
+ Union,
+ Optional,
+)
from infini.logging import logger
from pathlib import Path
@@ -15,7 +24,7 @@ import importlib.abc
class InfiniMetaFinder(importlib.abc.MetaPathFinder):
- def find_spec(self, fullname: str, path: Sequence[str] | None, target=None):
+ def find_spec(self, fullname: str, path: Optional[Sequence[str]], target=None):
default_entries = [
Path.cwd() / "src",
Path.home() / ".ipm" / "src",
@@ -47,12 +56,14 @@ class InfiniMetaFinder(importlib.abc.MetaPathFinder):
fullname,
filename,
loader=InfiniLoader(str(filename)),
- submodule_search_locations=[
- str(submodule_location)
- for submodule_location in submodule_locations
- ]
- if submodule_locations
- else None,
+ submodule_search_locations=(
+ [
+ str(submodule_location)
+ for submodule_location in submodule_locations
+ ]
+ if submodule_locations
+ else None
+ ),
)
return None
@@ -84,7 +95,7 @@ class Loader:
pre_interceptors: List[RouterType]
handlers: List[RouterType]
events: Dict[str, str]
- global_variables: Dict[str, str | Callable]
+ global_variables: Dict[str, Union[str, Callable]]
interceptors: List[RouterType]
def __init__(self) -> None:
diff --git a/src/infini/register.py b/src/infini/register.py
index 391fcf63..a2addc2b 100644
--- a/src/infini/register.py
+++ b/src/infini/register.py
@@ -1,7 +1,7 @@
from infini.input import Input
from infini.output import Output
from infini.router import Contains, Router
-from infini.typing import List, Dict, Any, Callable, RouterType
+from infini.typing import List, Dict, Any, Callable, RouterType, Optional, Union
from functools import wraps
@@ -9,7 +9,7 @@ class Register:
pre_interceptors: List[RouterType]
handlers: List[RouterType]
events: Dict[str, str]
- global_variables: Dict[str, str | Callable]
+ global_variables: Dict[str, Union[str, Callable]]
interceptors: List[RouterType]
def __init__(self) -> None:
@@ -19,18 +19,16 @@ class Register:
self.global_variables = {}
self.interceptors = []
- def pre_interceptor(self, router: Router | str, priority: int = 0):
+ def pre_interceptor(self, router: Union[Router, str], priority: int = 0):
def decorator(func):
@wraps(func)
- def wrapper(input: Input) -> Input | Output:
+ def wrapper(input: Input) -> Union[Input, Output]:
return func(input)
self.pre_interceptors.append(
{
"priority": priority,
- "router": Contains(router)
- if isinstance(router, str)
- else router,
+ "router": Contains(router) if isinstance(router, str) else router,
"handler": wrapper,
}
)
@@ -38,7 +36,7 @@ class Register:
return decorator
- def handler(self, router: Router | str, priority: int = 0):
+ def handler(self, router: Union[Router, str], priority: int = 0):
def decorator(func):
@wraps(func)
def wrapper(input: Input) -> Output:
@@ -47,9 +45,7 @@ class Register:
self.handlers.append(
{
"priority": priority,
- "router": Contains(router)
- if isinstance(router, str)
- else router,
+ "router": Contains(router) if isinstance(router, str) else router,
"handler": wrapper,
}
)
@@ -63,7 +59,7 @@ class Register:
def regist_variable(self, name: str, data: Any):
self.global_variables[name] = data
- def dynamic_variable(self, name: str | None = None):
+ def dynamic_variable(self, name: Optional[str] = None):
def decorator(func):
@wraps(func)
def wrapper(*args, **kwargs) -> str:
@@ -74,18 +70,16 @@ class Register:
return decorator
- def interceptor(self, router: Router | str, priority: int = 0):
+ def interceptor(self, router: Union[Router, str], priority: int = 0):
def decorator(func):
@wraps(func)
- def wrapper(input: Input) -> Input | Output:
+ def wrapper(input: Input) -> Union[Input, Output]:
return func(input)
self.interceptors.append(
{
"priority": priority,
- "router": Contains(router)
- if isinstance(router, str)
- else router,
+ "router": Contains(router) if isinstance(router, str) else router,
"handler": wrapper,
}
)
diff --git a/src/infini/typing.py b/src/infini/typing.py
index 05eea05e..6bac0099 100644
--- a/src/infini/typing.py
+++ b/src/infini/typing.py
@@ -2,6 +2,7 @@ from typing import (
Dict as Dict,
List as List,
Any as Any,
+ Optional as Optional,
Generic as Generic,
Callable as Callable,
Literal as Literal,
@@ -16,7 +17,7 @@ from types import ModuleType as ModuleType, GeneratorType as GeneratorType
from . import router, input, output
T = TypeVar("T")
-Stream = Union["input.Input", "output.Output"]
+Stream = Union["input.Input[Any]", "output.Output"]
OutputGenerator = Generator["output.Output", Any, None]