-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement hybrid message processing with Rust and Python integr…
…ation
- Loading branch information
1 parent
17f1f0b
commit 2710e4b
Showing
16 changed files
with
313 additions
and
111 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
{ | ||
"version": "0.2.0", | ||
"configurations": [ | ||
|
||
{ | ||
"name": "Python: Current File", | ||
"type": "python", | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
rule_file: "rules.yaml" | ||
platforms: | ||
- type: WeChat | ||
name: "企业微信" | ||
config: | ||
api_key: "your_key" | ||
|
||
- type: Slack | ||
name: "Slack生产环境" | ||
config: | ||
token: "xoxb-123" | ||
|
||
# rules.yaml | ||
rules: | ||
- name: 技术支持请求 | ||
when: | ||
contains: "帮助" | ||
or: | ||
contains: "怎么操作" | ||
then: | ||
- type: reply | ||
content: "请访问帮助中心:help.example.com" | ||
- type: forward | ||
platform: "Slack" | ||
channel: "support-channel" | ||
|
||
- name: 紧急情况处理 | ||
when: | ||
contains: "紧急" | ||
platform: ["wechat", "slack"] | ||
then: | ||
- type: webhook | ||
url: "https://alert.example.com/notify" | ||
- type: reply | ||
content: "已收到您的紧急请求,正在处理中..." |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
from iamai._core import RuleEngine, process_message, PyMessage | ||
from typing import List, Dict | ||
import json | ||
|
||
|
||
class HybridMessage(PyMessage): | ||
"""Python-Rust共享消息结构""" | ||
|
||
def to_json(self) -> str: | ||
return json.dumps( | ||
{ | ||
"content": self.content, | ||
"platform": self.platform, | ||
"user_id": self.user_id, | ||
} | ||
) | ||
|
||
|
||
class HybridRuleEngine: | ||
def __init__(self): | ||
self.rust_engine = RuleEngine() | ||
self.py_plugins: List[object] = [] | ||
|
||
def add_regex_rule(self, pattern: str): | ||
"""添加高性能正则规则到Rust引擎""" | ||
self.rust_engine.add_regex_rule(pattern) | ||
|
||
def add_py_plugin(self, plugin: object): | ||
"""添加Python插件""" | ||
self.py_plugins.append(plugin) | ||
|
||
def process(self, msg: HybridMessage) -> List[str]: | ||
"""混合处理流程""" | ||
responses = [] | ||
|
||
# Rust侧处理 | ||
rust_resp = process_message(msg) | ||
responses.append(rust_resp) | ||
|
||
# Rust正则匹配 | ||
matched_rules = self.rust_engine.match_message(msg) | ||
for rule in matched_rules: | ||
responses.append(f"[Rust规则匹配] {rule}") | ||
|
||
# Python插件处理 | ||
for plugin in self.py_plugins: | ||
if hasattr(plugin, "match_rule") and plugin.match_rule(msg): | ||
responses.append(plugin.execute_action(msg)) | ||
|
||
return responses | ||
|
||
|
||
# math_plugin.py | ||
class MathPlugin: | ||
def match_rule(self, msg: HybridMessage) -> bool: | ||
return msg.content.startswith("计算") | ||
|
||
def execute_action(self, msg: HybridMessage) -> str: | ||
try: | ||
expr = msg.content[2:].strip() | ||
return f"计算结果: {expr} = {eval(expr)}" | ||
except: | ||
return "计算失败" | ||
|
||
|
||
import time | ||
from iamai._core import HybridMessage | ||
|
||
# 创建测试消息 | ||
msg = HybridMessage(content="计算2+2", platform="test", user_id="perf") | ||
|
||
# 纯Python版本 | ||
start = time.time() | ||
for _ in range(10_000): | ||
MathPlugin().execute_action(msg) | ||
py_time = time.time() - start | ||
|
||
# Rust混合版本 | ||
from iamai._core import process_message | ||
|
||
start = time.time() | ||
for _ in range(10_000): | ||
process_message(msg) | ||
rs_time = time.time() - start | ||
|
||
print(f"Python耗时: {py_time:.4f}s") | ||
print(f"Rust耗时: {rs_time:.4f}s") | ||
print(f"性能提升: {(py_time/rs_time):.1f}x") |
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pub mod rule; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
use pyo3::prelude::*; | ||
|
||
#[pyclass] | ||
struct Rule { | ||
condition: PyObject, | ||
action: PyObject, | ||
priority: i32, | ||
} |
Oops, something went wrong.