Skip to content
Toggle navigation
Projects
Groups
Snippets
Help
ai
/
ai-workflow
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit 14b54102
authored
Jul 09, 2026
by
sunqi02
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
feat: 添加手机号参数到课程推荐工具,优化生成的链接
1 parent
ab76ac94
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
105 additions
and
10 deletions
workspace/agent-admin/backend/app/agent_core/agent_hub.py
workspace/agent-admin/backend/config/models.json
workspace/agent-admin/backend/skills/course-recommend/SKILL.md
workspace/agent-admin/backend/skills/course-recommend/scripts/recommend.py
workspace/agent-admin/backend/app/agent_core/agent_hub.py
View file @
14b5410
...
...
@@ -5,6 +5,7 @@ from contextvars import ContextVar
from
datetime
import
datetime
,
timezone
from
functools
import
partial
from
pathlib
import
Path
import
sys
from
typing
import
Dict
,
List
,
Optional
,
Any
from
app.config.loader
import
ConfigLoader
from
app.config.settings
import
AgentConfig
...
...
@@ -307,6 +308,7 @@ class AgentHub:
self
.
session_service
=
session_service
self
.
im_service
=
im_service
self
.
_db_factory
=
db_factory
self
.
_phone_cache
:
Dict
[
str
,
str
]
=
{}
if
preloaded_agents
is
None
:
raise
AgentHubError
(
"preloaded_agents is required — load from DB via ConfigLoader.load_agents_from_db()"
)
...
...
@@ -447,6 +449,10 @@ class AgentHub:
self
.
agent_toolkits
:
Dict
[
str
,
Toolkit
]
=
{}
self
.
_build_all_agent_toolkits
()
# 注册 course_recommend wrapper 工具(自动注入 phone)
recommend_tool
=
self
.
_make_course_recommend_tool
()
self
.
register_skill_tool
(
"course-recommend"
,
recommend_tool
,
"course_recommend"
)
self
.
_agents_loaded_at
=
datetime
.
now
(
timezone
.
utc
)
.
replace
(
tzinfo
=
None
)
def
_register_im_hooks
(
...
...
@@ -580,6 +586,9 @@ class AgentHub:
self
.
agents
=
new_agents
self
.
_inject_mouth_agent
()
self
.
_build_all_agent_toolkits
()
# 热加载后重新注册 course_recommend wrapper 工具
recommend_tool
=
self
.
_make_course_recommend_tool
()
self
.
register_skill_tool
(
"course-recommend"
,
recommend_tool
,
"course_recommend"
)
self
.
_agents_loaded_at
=
datetime
.
now
(
timezone
.
utc
)
.
replace
(
tzinfo
=
None
)
logger
.
info
(
"Agent 配置已热加载:
%
d 个"
,
len
(
self
.
agents
))
...
...
@@ -668,9 +677,26 @@ class AgentHub:
# 暂存路由上下文(per-asyncio-Task,并发安全),
# 供 _call_sub_agent 创建子 Agent 时注册 hook 和发 IM
# 查询当前群绑定的手机号,写入缓存 + route context 供 course_recommend 工具使用
phone
=
self
.
_phone_cache
.
get
(
session_id
)
if
phone
is
None
and
self
.
_db_factory
:
try
:
with
self
.
_db_factory
()
as
db
:
from
app.customer_info.model
import
CustomerGroupPhone
record
=
db
.
query
(
CustomerGroupPhone
)
.
filter
(
CustomerGroupPhone
.
group_id
==
session_id
,
CustomerGroupPhone
.
phone
.
isnot
(
None
),
)
.
first
()
if
record
and
record
.
phone
:
phone
=
record
.
phone
self
.
_phone_cache
[
session_id
]
=
phone
except
Exception
:
logger
.
warning
(
"查询手机号失败: session_id=
%
s"
,
session_id
,
exc_info
=
True
)
_route_context_var
.
set
(
dict
(
room_id
=
room_id
,
user_id
=
user_id
,
bot_id_mapping
=
bot_id_mapping
or
{},
session_id
=
session_id
,
phone
=
phone
or
""
,
call_count
=
0
,
called_agents
=
set
(),
))
...
...
@@ -1135,6 +1161,71 @@ class AgentHub:
return
read_pending_messages
def
_make_course_recommend_tool
(
self
):
"""创建 course_recommend wrapper 工具,自动从缓存注入 phone。"""
skills_base
=
Path
(
__file__
)
.
resolve
()
.
parent
.
parent
.
parent
/
"skills"
script_path
=
skills_base
/
"course-recommend"
/
"scripts"
/
"recommend.py"
backend_dir
=
skills_base
.
parent
async
def
course_recommend
(
query
:
str
=
""
)
->
ToolResponse
:
"""根据用户的学习需求推荐相关课程。
当用户想找课程学习某个主题或提升某方面能力时使用。
内部自动根据当前群的绑定手机号生成含 phone 的完整链接,
无需用户提供手机号。
Args:
query: 用户的学习需求描述,例如"想学Python"、"提升管理能力"
Returns:
JSON 字符串,包含推荐课程列表
"""
if
not
query
.
strip
():
return
ToolResponse
(
content
=
[
TextBlock
(
type
=
"text"
,
text
=
"错误:query 不能为空"
)],
)
# 从 route context 取 session_id,sub-agent 时提取 :agent: 前的内容
ctx
=
_route_context_var
.
get
(
None
)
session_id
=
ctx
.
get
(
"session_id"
,
""
)
if
ctx
else
""
group_id
=
session_id
.
split
(
":agent:"
)[
0
]
if
session_id
else
""
phone
=
self
.
_phone_cache
.
get
(
group_id
,
""
)
cmd
=
[
sys
.
executable
,
str
(
script_path
),
query
]
if
phone
:
cmd
.
append
(
phone
)
try
:
proc
=
await
asyncio
.
create_subprocess_exec
(
*
cmd
,
cwd
=
str
(
backend_dir
),
stdout
=
asyncio
.
subprocess
.
PIPE
,
stderr
=
asyncio
.
subprocess
.
PIPE
,
)
stdout
,
stderr
=
await
proc
.
communicate
()
if
proc
.
returncode
!=
0
:
return
ToolResponse
(
content
=
[
TextBlock
(
type
=
"text"
,
text
=
f
"查询失败:{stderr.decode().strip()}"
,
)],
)
return
ToolResponse
(
content
=
[
TextBlock
(
type
=
"text"
,
text
=
stdout
.
decode
()
.
strip
(),
)],
)
except
Exception
as
e
:
return
ToolResponse
(
content
=
[
TextBlock
(
type
=
"text"
,
text
=
f
"执行出错:{str(e)}"
,
)],
)
return
course_recommend
async
def
_summarize_thinking
(
self
,
thinking_text
:
str
)
->
str
:
"""将思考过程总结为简短的状态更新,不暴露内部推理细节。"""
try
:
...
...
workspace/agent-admin/backend/config/models.json
View file @
14b5410
...
...
@@ -4,19 +4,19 @@
"type"
:
"openai_compatible"
,
"api_base"
:
"https://dashscope.aliyuncs.com/compatible-mode/v1"
,
"model_name"
:
"qwen3.7-plus"
,
"api_key"
:
"sk-
321cbda8e3b24103928c984802f47301
"
"api_key"
:
"sk-
ws-H.EMRRHMX.S4k3.MEUCIQDnkdAOI5V_gOXO0s4h0uj66P9Yp5DFxghgCCoLB0cxkAIgWbLaokBGxsQjlN90pivlsjwVBIRNIfCDVuyCW__6ldU
"
},
"message_gate"
:
{
"type"
:
"openai_compatible"
,
"api_base"
:
"https://dashscope.aliyuncs.com/compatible-mode/v1"
,
"model_name"
:
"deepseek-v4-flash"
,
"api_key"
:
"sk-
321cbda8e3b24103928c984802f47301
"
"api_key"
:
"sk-
ws-H.EMRRHMX.S4k3.MEUCIQDnkdAOI5V_gOXO0s4h0uj66P9Yp5DFxghgCCoLB0cxkAIgWbLaokBGxsQjlN90pivlsjwVBIRNIfCDVuyCW__6ldU
"
},
"thinking_summary"
:
{
"type"
:
"openai_compatible"
,
"api_base"
:
"https://dashscope.aliyuncs.com/compatible-mode/v1"
,
"model_name"
:
"deepseek-v4-flash"
,
"api_key"
:
"sk-
321cbda8e3b24103928c984802f47301
"
"api_key"
:
"sk-
ws-H.EMRRHMX.S4k3.MEUCIQDnkdAOI5V_gOXO0s4h0uj66P9Yp5DFxghgCCoLB0cxkAIgWbLaokBGxsQjlN90pivlsjwVBIRNIfCDVuyCW__6ldU
"
}
},
"meeting_minutes"
:
{
...
...
@@ -24,13 +24,13 @@
"type"
:
"dashscope_asr"
,
"api_base"
:
"https://dashscope.aliyuncs.com/api/v1"
,
"model_name"
:
"fun-asr"
,
"api_key"
:
"sk-
321cbda8e3b24103928c984802f47301
"
"api_key"
:
"sk-
ws-H.EMRRHMX.S4k3.MEUCIQDnkdAOI5V_gOXO0s4h0uj66P9Yp5DFxghgCCoLB0cxkAIgWbLaokBGxsQjlN90pivlsjwVBIRNIfCDVuyCW__6ldU
"
},
"qwen3.6-plus"
:
{
"type"
:
"openai_compatible"
,
"api_base"
:
"https://dashscope.aliyuncs.com/compatible-mode/v1"
,
"model_name"
:
"qwen3.6-plus"
,
"api_key"
:
"sk-
321cbda8e3b24103928c984802f47301
"
"api_key"
:
"sk-
ws-H.EMRRHMX.S4k3.MEUCIQDnkdAOI5V_gOXO0s4h0uj66P9Yp5DFxghgCCoLB0cxkAIgWbLaokBGxsQjlN90pivlsjwVBIRNIfCDVuyCW__6ldU
"
},
"oss"
:
{
"access_key_id"
:
"LTAI5tB2Q3EJFmaosMYN3uPc"
,
...
...
workspace/agent-admin/backend/skills/course-recommend/SKILL.md
View file @
14b5410
...
...
@@ -17,7 +17,8 @@ skill 启动时优先从 `backend/.env` 加载,找不到对应变量时再从
## Workflow
1.
用户说"想学 X / 不擅长 X / 上一条回答相关的课" → 调用本 skill
2.
跑:
`python scripts/recommend.py "<user_query>"`
2.
跑:
`python scripts/recommend.py "<user_query>" ["<phone>"]`
-
phone 为可选参数,当已知用户手机号时传入,生成的跳转链接会附带
`&phone={phone}`
3.
读 stdout(单行 JSON),提取课程名称和 URL,以纯文本形式呈现给用户
4.
不要二次加工 url、不要解释匹配算法、不要猜测 courseId
...
...
@@ -40,7 +41,7 @@ skill 启动时优先从 `backend/.env` 加载,找不到对应变量时再从
"count"
:
2
,
"matches"
:
[
{
"courseId"
:
"abc123"
,
"url"
:
"http://localhost:5173/?courseId=abc123"
,
"title"
:
"课程A"
},
{
"courseId"
:
"def456"
,
"url"
:
"http://localhost:5173/?courseId=def456"
,
"title"
:
"课程B"
}
{
"courseId"
:
"def456"
,
"url"
:
"http://localhost:5173/?courseId=def456
&phone=13800138000
"
,
"title"
:
"课程B"
}
]
}
```
...
...
workspace/agent-admin/backend/skills/course-recommend/scripts/recommend.py
View file @
14b5410
"""Course recommend: query MySQL, keyword-match, emit SSO relay URL on stdout.
Usage:
python recommend.py "<user_query>"
python recommend.py "<user_query>"
["<phone>"]
Reads env (auto-loads .env.skill from skill root):
COURSE_DATABASE_URL mysql://user:pass@host:3306/db
...
...
@@ -10,6 +10,7 @@ Reads env (auto-loads .env.skill from skill root):
Output: single JSON line on stdout
{"courseId":"...","url":".../?courseId=...","title":"..."}
When phone is provided, the url includes &phone={phone}.
Errors: stderr + exit 1
"""
import
json
...
...
@@ -100,10 +101,11 @@ def fetch_courses(db_url: str) -> list[tuple[str, str, str]]:
def
main
():
load_env
()
if
len
(
sys
.
argv
)
<
2
or
not
sys
.
argv
[
1
]
.
strip
():
print
(
"usage: recommend.py <query>"
,
file
=
sys
.
stderr
)
print
(
"usage: recommend.py <query>
[phone]
"
,
file
=
sys
.
stderr
)
sys
.
exit
(
1
)
query
=
sys
.
argv
[
1
]
phone
=
sys
.
argv
[
2
]
if
len
(
sys
.
argv
)
>
2
and
sys
.
argv
[
2
]
.
strip
()
else
None
db_url
=
require_env
(
"COURSE_DATABASE_URL"
)
base_url
=
require_env
(
"COURSE_BASE_URL"
)
.
rstrip
(
"/"
)
...
...
@@ -119,10 +121,11 @@ def main():
print
(
f
"ERROR: no course matched query: {query}"
,
file
=
sys
.
stderr
)
sys
.
exit
(
1
)
phone_suffix
=
f
"&phone={phone}"
if
phone
else
""
matches
=
[
{
"courseId"
:
cid
,
"url"
:
f
"{base_url}/?courseId={cid}"
,
"url"
:
f
"{base_url}/?courseId={cid}
{phone_suffix}
"
,
"title"
:
title
,
}
for
s
,
cid
,
title
in
scored
...
...
Write
Preview
Markdown
is supported
Attach a file
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to post a comment