yimo-kit 0.1.0__py3-none-any.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.
- yimo/__init__.py +26 -0
- yimo/kit/__init__.py +98 -0
- yimo/kit/ai/__init__.py +110 -0
- yimo/kit/ai/_internal/__init__.py +27 -0
- yimo/kit/ai/_internal/failover.py +535 -0
- yimo/kit/ai/_internal/pool.py +526 -0
- yimo/kit/ai/_internal/rerank_transport.py +137 -0
- yimo/kit/ai/_internal/retry_after.py +51 -0
- yimo/kit/ai/_internal/sdk_client.py +463 -0
- yimo/kit/ai/_internal/service_base.py +115 -0
- yimo/kit/ai/_internal/types.py +28 -0
- yimo/kit/ai/agent/__init__.py +127 -0
- yimo/kit/ai/agent/approval.py +66 -0
- yimo/kit/ai/agent/errors.py +128 -0
- yimo/kit/ai/agent/loop.py +643 -0
- yimo/kit/ai/agent/mcp.py +278 -0
- yimo/kit/ai/agent/registry.py +203 -0
- yimo/kit/ai/agent/session.py +150 -0
- yimo/kit/ai/agent/skill.py +370 -0
- yimo/kit/ai/agent/types.py +161 -0
- yimo/kit/ai/chat_service.py +724 -0
- yimo/kit/ai/decision/__init__.py +57 -0
- yimo/kit/ai/decision/decima.py +853 -0
- yimo/kit/ai/decision/laya.py +552 -0
- yimo/kit/ai/embedding_service.py +198 -0
- yimo/kit/ai/exceptions.py +109 -0
- yimo/kit/ai/image_service.py +84 -0
- yimo/kit/ai/messages.py +105 -0
- yimo/kit/ai/model_builder.py +121 -0
- yimo/kit/ai/model_client.py +62 -0
- yimo/kit/ai/profiles.py +577 -0
- yimo/kit/ai/rerank_service.py +136 -0
- yimo/kit/ai/runner.py +1127 -0
- yimo/kit/common_kit.py +221 -0
- yimo/kit/config/__init__.py +44 -0
- yimo/kit/config/kit_config.py +210 -0
- yimo/kit/config/log_config.py +171 -0
- yimo/kit/crawl/__init__.py +71 -0
- yimo/kit/crawl/article_io.py +325 -0
- yimo/kit/crawl/article_models.py +172 -0
- yimo/kit/crawl/deepseek_kit.py +673 -0
- yimo/kit/crawl/web_article_kit.py +638 -0
- yimo/kit/crawl/wx_article_kit.py +692 -0
- yimo/kit/db/__init__.py +101 -0
- yimo/kit/db/audit.py +625 -0
- yimo/kit/db/base_db_kit.py +1265 -0
- yimo/kit/db/crud.py +423 -0
- yimo/kit/db/engine.py +235 -0
- yimo/kit/db/exceptions.py +45 -0
- yimo/kit/db/models/__init__.py +33 -0
- yimo/kit/db/models/base.py +56 -0
- yimo/kit/db/mysql_kit.py +348 -0
- yimo/kit/db/pg_kit.py +442 -0
- yimo/kit/db/query.py +338 -0
- yimo/kit/db/session.py +85 -0
- yimo/kit/db/sqlite_kit.py +462 -0
- yimo/kit/db/transaction.py +109 -0
- yimo/kit/docs/__init__.py +34 -0
- yimo/kit/docs/anydoc_kit.py +490 -0
- yimo/kit/docs/docx_kit.py +895 -0
- yimo/kit/docs/md2doc_kit.py +1801 -0
- yimo/kit/docs/mineru_kit.py +1497 -0
- yimo/kit/docs/rc_docparser_kit.py +654 -0
- yimo/kit/file_kit.py +518 -0
- yimo/kit/license_kit.py +533 -0
- yimo/kit/redis_kit.py +392 -0
- yimo_kit-0.1.0.dist-info/METADATA +1673 -0
- yimo_kit-0.1.0.dist-info/RECORD +70 -0
- yimo_kit-0.1.0.dist-info/WHEEL +4 -0
- yimo_kit-0.1.0.dist-info/licenses/LICENSE +201 -0
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Copyright (c) 2021-2026 溢墨修心(Yimo). All Rights Reserved.
|
|
3
|
+
|
|
4
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
you may not use this file except in compliance with the License.
|
|
6
|
+
You may obtain a copy of the License at
|
|
7
|
+
|
|
8
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
|
|
10
|
+
Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
See the License for the specific language governing permissions and
|
|
14
|
+
limitations under the License.
|
|
15
|
+
|
|
16
|
+
Project: yimo
|
|
17
|
+
Author: 溢墨修心(Yimo)
|
|
18
|
+
"""
|
|
19
|
+
|
|
20
|
+
# date: 2026-11-10
|
|
21
|
+
"""yimo.kit.ai.agent - MCP + Skill + 工具循环(PEP 562 延迟导出)。
|
|
22
|
+
|
|
23
|
+
``import yimo.kit.ai.agent`` 本身不加载任何可选依赖;访问 ``Agent`` /
|
|
24
|
+
``McpConnection`` 时才检查 fastmcp 是否安装,缺依赖报错会提示
|
|
25
|
+
``pip install yimo-kit[agent]``。
|
|
26
|
+
"""
|
|
27
|
+
|
|
28
|
+
from importlib import import_module, util
|
|
29
|
+
from typing import Any
|
|
30
|
+
|
|
31
|
+
__all__ = [
|
|
32
|
+
"Agent",
|
|
33
|
+
"AgentConfigError",
|
|
34
|
+
"AgentEmptyReplyError",
|
|
35
|
+
"AgentError",
|
|
36
|
+
"AgentModelError",
|
|
37
|
+
"AgentResult",
|
|
38
|
+
"AgentTimeoutError",
|
|
39
|
+
"AgentTruncatedError",
|
|
40
|
+
"ApprovalDeniedError",
|
|
41
|
+
"ApprovalHook",
|
|
42
|
+
"ApprovalRequest",
|
|
43
|
+
"DefaultApproval",
|
|
44
|
+
"DenyAllApproval",
|
|
45
|
+
"DuplicateSkillError",
|
|
46
|
+
"MaxTurnsExceeded",
|
|
47
|
+
"McpConnection",
|
|
48
|
+
"McpConnectionError",
|
|
49
|
+
"McpServerConfig",
|
|
50
|
+
"SkillError",
|
|
51
|
+
"SkillNotFoundError",
|
|
52
|
+
"SkillPackage",
|
|
53
|
+
"SkillRegistry",
|
|
54
|
+
"SkillScript",
|
|
55
|
+
"SkillValidationError",
|
|
56
|
+
"Session",
|
|
57
|
+
"ToolCallRecord",
|
|
58
|
+
"ToolExecutionError",
|
|
59
|
+
"ToolNotFoundError",
|
|
60
|
+
"ToolRegistry",
|
|
61
|
+
"ToolResult",
|
|
62
|
+
"ToolSpec",
|
|
63
|
+
"load_servers",
|
|
64
|
+
]
|
|
65
|
+
|
|
66
|
+
# 符号 → (模块, 模块内属性名);模块内属性名缺省与符号同名
|
|
67
|
+
_EXPORTS: dict[str, tuple[str, str]] = {
|
|
68
|
+
"Agent": ("yimo.kit.ai.agent.loop", "Agent"),
|
|
69
|
+
"AgentResult": ("yimo.kit.ai.agent.types", "AgentResult"),
|
|
70
|
+
"ToolSpec": ("yimo.kit.ai.agent.types", "ToolSpec"),
|
|
71
|
+
"ToolResult": ("yimo.kit.ai.agent.types", "ToolResult"),
|
|
72
|
+
"ToolCallRecord": ("yimo.kit.ai.agent.types", "ToolCallRecord"),
|
|
73
|
+
"McpServerConfig": ("yimo.kit.ai.agent.types", "McpServerConfig"),
|
|
74
|
+
"load_servers": ("yimo.kit.ai.agent.types", "load_servers"),
|
|
75
|
+
"ToolRegistry": ("yimo.kit.ai.agent.registry", "ToolRegistry"),
|
|
76
|
+
"Session": ("yimo.kit.ai.agent.session", "Session"),
|
|
77
|
+
"ApprovalHook": ("yimo.kit.ai.agent.approval", "ApprovalHook"),
|
|
78
|
+
"ApprovalRequest": ("yimo.kit.ai.agent.approval", "ApprovalRequest"),
|
|
79
|
+
"DefaultApproval": ("yimo.kit.ai.agent.approval", "DefaultApproval"),
|
|
80
|
+
"DenyAllApproval": ("yimo.kit.ai.agent.approval", "DenyAllApproval"),
|
|
81
|
+
"SkillPackage": ("yimo.kit.ai.agent.skill", "SkillPackage"),
|
|
82
|
+
"SkillScript": ("yimo.kit.ai.agent.skill", "SkillScript"),
|
|
83
|
+
"SkillRegistry": ("yimo.kit.ai.agent.skill", "SkillRegistry"),
|
|
84
|
+
"McpConnection": ("yimo.kit.ai.agent.mcp", "McpConnection"),
|
|
85
|
+
"AgentError": ("yimo.kit.ai.agent.errors", "AgentError"),
|
|
86
|
+
"AgentConfigError": ("yimo.kit.ai.agent.errors", "AgentConfigError"),
|
|
87
|
+
"AgentModelError": ("yimo.kit.ai.agent.errors", "AgentModelError"),
|
|
88
|
+
"AgentTimeoutError": ("yimo.kit.ai.agent.errors", "AgentTimeoutError"),
|
|
89
|
+
"AgentEmptyReplyError": ("yimo.kit.ai.agent.errors", "AgentEmptyReplyError"),
|
|
90
|
+
"AgentTruncatedError": ("yimo.kit.ai.agent.errors", "AgentTruncatedError"),
|
|
91
|
+
"MaxTurnsExceeded": ("yimo.kit.ai.agent.errors", "MaxTurnsExceeded"),
|
|
92
|
+
"ApprovalDeniedError": ("yimo.kit.ai.agent.errors", "ApprovalDeniedError"),
|
|
93
|
+
"ToolNotFoundError": ("yimo.kit.ai.agent.errors", "ToolNotFoundError"),
|
|
94
|
+
"ToolExecutionError": ("yimo.kit.ai.agent.errors", "ToolExecutionError"),
|
|
95
|
+
"McpConnectionError": ("yimo.kit.ai.agent.errors", "McpConnectionError"),
|
|
96
|
+
"SkillError": ("yimo.kit.ai.agent.errors", "SkillError"),
|
|
97
|
+
"SkillValidationError": ("yimo.kit.ai.agent.errors", "SkillValidationError"),
|
|
98
|
+
"SkillNotFoundError": ("yimo.kit.ai.agent.errors", "SkillNotFoundError"),
|
|
99
|
+
"DuplicateSkillError": ("yimo.kit.ai.agent.errors", "DuplicateSkillError"),
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
# 需要 fastmcp 的符号:在导入前显式守卫,报错必须指向 [agent]
|
|
103
|
+
_GUARDED = frozenset({"Agent", "McpConnection"})
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
def _require_fastmcp(name: str) -> None:
|
|
107
|
+
if util.find_spec("fastmcp") is not None:
|
|
108
|
+
return
|
|
109
|
+
raise ImportError(
|
|
110
|
+
f"yimo.kit.ai.agent.{name} 需要可选依赖,请安装:pip install yimo-kit[agent]"
|
|
111
|
+
)
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
def __getattr__(name: str) -> Any:
|
|
115
|
+
target = _EXPORTS.get(name)
|
|
116
|
+
if target is None:
|
|
117
|
+
raise AttributeError(f"module 'yimo.kit.ai.agent' has no attribute {name!r}")
|
|
118
|
+
if name in _GUARDED:
|
|
119
|
+
_require_fastmcp(name)
|
|
120
|
+
module, attribute = target
|
|
121
|
+
value = getattr(import_module(module), attribute)
|
|
122
|
+
globals()[name] = value
|
|
123
|
+
return value
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+
def __dir__() -> list[str]:
|
|
127
|
+
return sorted(__all__)
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Copyright (c) 2021-2026 溢墨修心(Yimo). All Rights Reserved.
|
|
3
|
+
|
|
4
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
you may not use this file except in compliance with the License.
|
|
6
|
+
You may obtain a copy of the License at
|
|
7
|
+
|
|
8
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
|
|
10
|
+
Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
See the License for the specific language governing permissions and
|
|
14
|
+
limitations under the License.
|
|
15
|
+
|
|
16
|
+
Project: yimo
|
|
17
|
+
Author: 溢墨修心(Yimo)
|
|
18
|
+
"""
|
|
19
|
+
|
|
20
|
+
# date: 2026-11-10
|
|
21
|
+
"""工具调用审批 Hook。
|
|
22
|
+
|
|
23
|
+
默认策略:MCP 工具放行(用户显式配置 server 才引入,信任边界在配置层);
|
|
24
|
+
skill 脚本拒绝(``subprocess`` 执行任意代码,v1 不开放该工具)。
|
|
25
|
+
"""
|
|
26
|
+
|
|
27
|
+
from dataclasses import dataclass
|
|
28
|
+
from typing import Any, Literal, Protocol
|
|
29
|
+
|
|
30
|
+
__all__ = [
|
|
31
|
+
"ApprovalHook",
|
|
32
|
+
"ApprovalRequest",
|
|
33
|
+
"DefaultApproval",
|
|
34
|
+
"DenyAllApproval",
|
|
35
|
+
]
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
@dataclass(frozen=True)
|
|
39
|
+
class ApprovalRequest:
|
|
40
|
+
"""一次待审批的工具调用。"""
|
|
41
|
+
|
|
42
|
+
kind: Literal["mcp_tool", "skill_script"]
|
|
43
|
+
target: str
|
|
44
|
+
arguments: dict[str, Any]
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
class ApprovalHook(Protocol):
|
|
48
|
+
"""审批 Hook:返回 True 放行,False 拒绝。"""
|
|
49
|
+
|
|
50
|
+
async def __call__(self, request: ApprovalRequest) -> bool:
|
|
51
|
+
"""判断是否放行。"""
|
|
52
|
+
...
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
class DefaultApproval:
|
|
56
|
+
"""默认审批:MCP 工具放行,skill 脚本拒绝。"""
|
|
57
|
+
|
|
58
|
+
async def __call__(self, request: ApprovalRequest) -> bool:
|
|
59
|
+
return request.kind == "mcp_tool"
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
class DenyAllApproval:
|
|
63
|
+
"""全部拒绝(最严格策略)。"""
|
|
64
|
+
|
|
65
|
+
async def __call__(self, request: ApprovalRequest) -> bool:
|
|
66
|
+
return False
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Copyright (c) 2021-2026 溢墨修心(Yimo). All Rights Reserved.
|
|
3
|
+
|
|
4
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
you may not use this file except in compliance with the License.
|
|
6
|
+
You may obtain a copy of the License at
|
|
7
|
+
|
|
8
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
|
|
10
|
+
Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
See the License for the specific language governing permissions and
|
|
14
|
+
limitations under the License.
|
|
15
|
+
|
|
16
|
+
Project: yimo
|
|
17
|
+
Author: 溢墨修心(Yimo)
|
|
18
|
+
"""
|
|
19
|
+
|
|
20
|
+
# date: 2026-11-10
|
|
21
|
+
"""yimo.kit.ai.agent 异常体系(统一继承 AgentError)。"""
|
|
22
|
+
|
|
23
|
+
from typing import Any
|
|
24
|
+
|
|
25
|
+
__all__ = [
|
|
26
|
+
"AgentConfigError",
|
|
27
|
+
"AgentEmptyReplyError",
|
|
28
|
+
"AgentError",
|
|
29
|
+
"AgentModelError",
|
|
30
|
+
"AgentTimeoutError",
|
|
31
|
+
"AgentTruncatedError",
|
|
32
|
+
"ApprovalDeniedError",
|
|
33
|
+
"DuplicateSkillError",
|
|
34
|
+
"MaxTurnsExceeded",
|
|
35
|
+
"McpConnectionError",
|
|
36
|
+
"SkillError",
|
|
37
|
+
"SkillNotFoundError",
|
|
38
|
+
"SkillValidationError",
|
|
39
|
+
"ToolExecutionError",
|
|
40
|
+
"ToolNotFoundError",
|
|
41
|
+
]
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
class AgentError(Exception):
|
|
45
|
+
"""Agent 相关异常基类。"""
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
class AgentConfigError(AgentError):
|
|
49
|
+
"""配置非法(MCP server 配置、Agent 参数等)。"""
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
class AgentModelError(AgentError):
|
|
53
|
+
"""模型调用失败(含锁定档案失败)。"""
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
class AgentTimeoutError(AgentError):
|
|
57
|
+
"""整轮预算耗尽。
|
|
58
|
+
|
|
59
|
+
Attributes:
|
|
60
|
+
turn: 超时发生时所在轮次(从 1 开始)。
|
|
61
|
+
phase: 超时阶段(``"model"`` / ``"tool"``)。
|
|
62
|
+
"""
|
|
63
|
+
|
|
64
|
+
def __init__(self, message: str, *, turn: int, phase: str) -> None:
|
|
65
|
+
super().__init__(message)
|
|
66
|
+
self.turn: int = turn
|
|
67
|
+
self.phase: str = phase
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
class AgentEmptyReplyError(AgentError):
|
|
71
|
+
"""模型既没有返回正文,也没有发起工具调用。"""
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
class AgentTruncatedError(AgentError):
|
|
75
|
+
"""模型输出被长度截断(``finish_reason == "length"``)。"""
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
class MaxTurnsExceeded(AgentError):
|
|
79
|
+
"""工具循环轮次用尽。
|
|
80
|
+
|
|
81
|
+
Attributes:
|
|
82
|
+
max_turns: 允许的最大轮次。
|
|
83
|
+
turns: 实际执行轮次。
|
|
84
|
+
tool_calls: 已完成执行的工具调用记录(可选,便于调用方决定是否续跑)。
|
|
85
|
+
"""
|
|
86
|
+
|
|
87
|
+
def __init__(
|
|
88
|
+
self,
|
|
89
|
+
max_turns: int,
|
|
90
|
+
turns: int | None = None,
|
|
91
|
+
tool_calls: list[Any] | None = None,
|
|
92
|
+
) -> None:
|
|
93
|
+
self.max_turns: int = max_turns
|
|
94
|
+
self.turns: int = turns if turns is not None else max_turns
|
|
95
|
+
self.tool_calls: list[Any] = list(tool_calls or [])
|
|
96
|
+
super().__init__(f"工具循环超过 {max_turns} 轮仍未结束")
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
class ApprovalDeniedError(AgentError):
|
|
100
|
+
"""审批 Hook 拒绝了某次工具调用。"""
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
class ToolNotFoundError(AgentError):
|
|
104
|
+
"""调用了未注册的工具。"""
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
class ToolExecutionError(AgentError):
|
|
108
|
+
"""工具执行失败(handler 抛异常、协议错误等)。"""
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
class McpConnectionError(AgentError):
|
|
112
|
+
"""MCP server 连接或握手失败。"""
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
class SkillError(AgentError):
|
|
116
|
+
"""Skill 相关异常基类。"""
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
class SkillValidationError(SkillError):
|
|
120
|
+
"""SKILL.md 结构或 frontmatter 非法。"""
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
class SkillNotFoundError(SkillError):
|
|
124
|
+
"""技能不存在。"""
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
class DuplicateSkillError(SkillError):
|
|
128
|
+
"""多个根目录存在同名技能。"""
|