aboutsummaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
author苏向夜 <fu050409@163.com>2024-03-11 13:06:50 +0800
committer苏向夜 <fu050409@163.com>2024-03-11 13:06:50 +0800
commita60476eb14a5add6e8724993164602f74a7492f1 (patch)
tree6d56bcae3c7623f60d6b076a4abeb4840dd07014
parent93581961abaf7edd2dc89170fb73a97a65857628 (diff)
downloadinfini-a60476eb14a5add6e8724993164602f74a7492f1.tar.gz
infini-a60476eb14a5add6e8724993164602f74a7492f1.zip
feat(router): support variable inject for router
-rw-r--r--src/infini/handler.py10
-rw-r--r--src/infini/interceptor.py8
-rw-r--r--src/infini/router.py35
3 files changed, 27 insertions, 26 deletions
diff --git a/src/infini/handler.py b/src/infini/handler.py
index 3d8cd586..c0e09532 100644
--- a/src/infini/handler.py
+++ b/src/infini/handler.py
@@ -9,15 +9,15 @@ class Handler:
handlers: List[RouterType]
def input(self, input: Input):
- if (queue := self.match(input.get_plain_text())).is_empty():
- yield Output.empty()
- return
injector = Injector()
parameters = {
"input": input,
"plain_text": input.get_plain_text(),
"user_id": input.get_user_id(),
}
+ if (queue := self.match(injector, **parameters)).is_empty():
+ yield Output.empty()
+ return
while not queue.is_empty():
if isinstance(
stream := injector.output(queue.pop(), parameters=parameters), Generator
@@ -32,12 +32,12 @@ class Handler:
return
def match(
- self, text: str
+ self, injector: Injector, **parameters
) -> EventQueue[Callable[..., Union[Output, Generator[Output, Any, None]]]]:
queue = EventQueue()
for handler in self.handlers:
- if handler["router"].match(text):
+ if injector.output(handler["router"].match, parameters=parameters):
queue.push(handler["priority"], handler["handler"])
return queue
diff --git a/src/infini/interceptor.py b/src/infini/interceptor.py
index 87255ae9..c73386ad 100644
--- a/src/infini/interceptor.py
+++ b/src/infini/interceptor.py
@@ -9,13 +9,13 @@ class Interceptor:
interceptors: List[RouterType]
def input(self, input: Input) -> Generator[Union[Output, Input], Any, None]:
- queue = self.match(input.get_plain_text())
injector = Injector()
parameters = {
"input": input,
"plain_text": input.get_plain_text(),
"user_id": input.get_user_id(),
}
+ queue = self.match(injector, **parameters)
while not queue.is_empty():
if isinstance(
stream := injector.output(queue.pop(), parameters=parameters),
@@ -44,7 +44,6 @@ class Interceptor:
self, output: Output, output_text: str
) -> Generator[Union[Output, str], Any, None]:
input = Input(output_text, variables=output.variables)
- queue = self.match(output_text)
injector = Injector()
parameters = {
"input": input,
@@ -52,6 +51,7 @@ class Interceptor:
"plain_text": output_text,
"user_id": input.get_user_id(),
}
+ queue = self.match(injector, **parameters)
while not queue.is_empty():
if isinstance(
stream := injector.output(queue.pop(), parameters=parameters), Generator
@@ -76,14 +76,14 @@ class Interceptor:
yield input.get_plain_text()
def match(
- self, text: str
+ self, injector: Injector, **parameters
) -> EventQueue[
Callable[..., Union[Input, Output, Generator[Union[Input, Output], Any, None]]]
]:
queue = EventQueue()
for interceptor in self.interceptors:
- if interceptor["router"].match(text):
+ if injector.output(interceptor["router"].match, parameters=parameters):
queue.push(interceptor["priority"], interceptor["handler"])
return queue
diff --git a/src/infini/router.py b/src/infini/router.py
index c84e11a4..71da44e2 100644
--- a/src/infini/router.py
+++ b/src/infini/router.py
@@ -1,4 +1,5 @@
from infini.typing import Sequence, Literal
+from infini.input import Input
class Router:
@@ -12,43 +13,43 @@ class Router:
def __eq__(self, __router: "Router") -> bool:
return __router.type == self.type and __router.signs == self.signs
- def match(self, input: str) -> bool:
- return any([input == sign for sign in self.signs])
+ def match(self, plain_text: str) -> bool:
+ text = plain_text.strip()
+ return any([text == sign for sign in self.signs])
class Startswith(Router):
type: Literal["startswith"] = "startswith"
- def match(self, input: str) -> bool:
- input = input.strip()
- return any([input.startswith(sign) for sign in self.signs])
+ def match(self, plain_text: str) -> bool:
+ text = plain_text.strip()
+ return any([text.startswith(sign) for sign in self.signs])
class Contains(Router):
type: Literal["contains"] = "contains"
- def match(self, input: str) -> bool:
- input = input.strip()
- return any([sign in input for sign in self.signs])
+ def match(self, plain_text: str) -> bool:
+ return any([sign in plain_text for sign in self.signs])
class Endswith(Router):
type: Literal["endswith"] = "endswith"
- def match(self, input: str) -> bool:
- input = input.strip()
- return any([input.endswith(sign) for sign in self.signs])
+ def match(self, input: Input) -> bool:
+ text = input.get_plain_text().strip()
+ return any([text.endswith(sign) for sign in self.signs])
class Command(Router):
type: Literal["command"] = "command"
prefix: tuple = (".", "/")
- def match(self, input: str) -> bool:
- input = input.strip()
- if input:
- if input.startswith(self.prefix):
- input = input[1:]
- return any([input.startswith(sign) for sign in self.signs])
+ def match(self, input: Input) -> bool:
+ text = input.get_plain_text().strip()
+ if text:
+ if text.startswith(self.prefix):
+ text = text[1:]
+ return any([text.startswith(sign) for sign in self.signs])
return False