feat: add React UI and AI page sharing tools
Move HTTP page rendering to the React app, fix multi-tool-call message handling, add API docs lookup, web page lookup, and Markdown preview page sharing.
This commit is contained in:
+1
-1
@@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "ias-ai"
|
||||
version = "0.1.2"
|
||||
version = "0.1.5"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
|
||||
+149
-46
@@ -1,13 +1,14 @@
|
||||
use crate::{
|
||||
tools::api_docs::query_api_docs,
|
||||
tools::date_time::get_system_date_time,
|
||||
tools::memory::{delete_memory, query_memory, remember_memory, update_memory},
|
||||
tools::web::create_web_page,
|
||||
tools::web::{create_markdown_preview, create_web_page, query_web_pages},
|
||||
workflow::core::{call_flow, load_tools_define},
|
||||
};
|
||||
|
||||
use ias_common::{
|
||||
model::ai::{ChatCompletionFunction, ChatCompletionToolCall},
|
||||
queue::{ChannelMessage, QueueMessage},
|
||||
queue::{ChannelMessage, QueueMessage, ToolMessage},
|
||||
};
|
||||
use serde_json::Value;
|
||||
|
||||
@@ -22,51 +23,47 @@ pub async fn call_tools(
|
||||
return Ok(None);
|
||||
}
|
||||
|
||||
if tools.len() > 1 {
|
||||
eprintln!("模型一次返回了{}个工具调用,当前仅处理第一个", tools.len());
|
||||
let mut results = Vec::with_capacity(tools.len());
|
||||
for tool in tools {
|
||||
let (tool_name, tool_result) = match tool.function.as_ref() {
|
||||
Some(function) => {
|
||||
println!(
|
||||
"开始调用工具: {}, arguments={}",
|
||||
function.name, function.arguments
|
||||
);
|
||||
(
|
||||
function.name.clone(),
|
||||
execute_tool(&channel, &tool_path, function).await,
|
||||
)
|
||||
}
|
||||
None => {
|
||||
eprintln!("工具调用缺少 function 字段: id={}", tool.id);
|
||||
(
|
||||
"unknown".to_string(),
|
||||
"工具调用失败: tool call 缺少 function 字段".to_string(),
|
||||
)
|
||||
}
|
||||
};
|
||||
|
||||
let tool_result = if tool_result.trim().is_empty() {
|
||||
format!("工具调用失败: {tool_name} 返回空结果")
|
||||
} else {
|
||||
tool_result
|
||||
};
|
||||
println!(
|
||||
"工具调用完成: {}, result_len={}",
|
||||
tool_name,
|
||||
tool_result.len()
|
||||
);
|
||||
|
||||
results.push(ToolMessage {
|
||||
tool_call_id: tool.id,
|
||||
name: tool_name,
|
||||
result: tool_result,
|
||||
});
|
||||
}
|
||||
|
||||
let Some(tool) = tools.first() else {
|
||||
return Ok(None);
|
||||
};
|
||||
|
||||
let (tool_name, tool_result) = match tool.function.as_ref() {
|
||||
Some(function) => {
|
||||
println!(
|
||||
"开始调用工具: {}, arguments={}",
|
||||
function.name, function.arguments
|
||||
);
|
||||
(
|
||||
function.name.clone(),
|
||||
execute_tool(&channel, &tool_path, function).await,
|
||||
)
|
||||
}
|
||||
None => {
|
||||
eprintln!("工具调用缺少 function 字段: id={}", tool.id);
|
||||
(
|
||||
"unknown".to_string(),
|
||||
"工具调用失败: tool call 缺少 function 字段".to_string(),
|
||||
)
|
||||
}
|
||||
};
|
||||
|
||||
let tool_result = if tool_result.trim().is_empty() {
|
||||
format!("工具调用失败: {tool_name} 返回空结果")
|
||||
} else {
|
||||
tool_result
|
||||
};
|
||||
println!(
|
||||
"工具调用完成: {}, result_len={}",
|
||||
tool_name,
|
||||
tool_result.len()
|
||||
);
|
||||
|
||||
Ok(Some(QueueMessage::Tool(
|
||||
channel,
|
||||
tool.id.clone(),
|
||||
tool_name,
|
||||
tool_result,
|
||||
)))
|
||||
Ok(Some(QueueMessage::Tools(channel, results)))
|
||||
}
|
||||
|
||||
async fn execute_tool(
|
||||
@@ -83,7 +80,10 @@ async fn execute_tool(
|
||||
Ok(value) => value,
|
||||
Err(error) => format!("工具调用失败: 加载工具定义失败: {error}"),
|
||||
},
|
||||
"query_api_docs" => query_api_docs(&function.arguments).await,
|
||||
"create_web_page" => create_web_page(&function.arguments).await,
|
||||
"create_markdown_preview" => create_markdown_preview(&function.arguments).await,
|
||||
"query_web_pages" => query_web_pages(&function.arguments).await,
|
||||
"remember_memory" => remember_memory(channel, &function.arguments).await,
|
||||
"query_memory" => query_memory(channel, &function.arguments).await,
|
||||
"update_memory" => update_memory(channel, &function.arguments).await,
|
||||
@@ -137,7 +137,7 @@ pub fn get_tool() -> Vec<serde_json::Value> {
|
||||
"type": "function",
|
||||
"function": {
|
||||
"name": "create_web_page",
|
||||
"description": "创建一个可通过链接访问的动态内容页面,用于把较长或结构复杂的内容交给用户查看。邮件提醒请使用 page_type=mail,工具结果会返回 /mail/{slug} 链接。",
|
||||
"description": "创建一个可通过链接访问的动态内容页面,用于把较长或结构复杂的内容交给用户查看。Markdown 长文本优先使用 create_markdown_preview;邮件提醒请使用 page_type=mail,工具结果会返回 /mail/{slug} 链接。",
|
||||
"parameters": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
@@ -174,6 +174,109 @@ pub fn get_tool() -> Vec<serde_json::Value> {
|
||||
}
|
||||
}
|
||||
}),
|
||||
serde_json::json!({
|
||||
"type": "function",
|
||||
"function": {
|
||||
"name": "create_markdown_preview",
|
||||
"description": "创建 Markdown 预览页面,用于把长篇 Markdown、报告、代码说明、列表、表格或结构化答复做成可分享链接,避免 IM/平台消息长度限制。工具结果会返回 /md/{slug} 链接,最终回复用户时应发送该链接和简短摘要。",
|
||||
"parameters": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"title": {
|
||||
"type": "string",
|
||||
"description": "Markdown 预览页标题。"
|
||||
},
|
||||
"summary": {
|
||||
"type": "string",
|
||||
"description": "简短摘要,会显示在页面标题下方,也可用于最终回复用户。"
|
||||
},
|
||||
"content": {
|
||||
"type": "string",
|
||||
"description": "Markdown 正文,支持标题、列表、表格、引用、链接、代码块等 GFM 语法。"
|
||||
},
|
||||
"source_label": {
|
||||
"type": "string",
|
||||
"description": "内容来源,例如 AI 回复、报告名称或服务名。"
|
||||
},
|
||||
"metadata": {
|
||||
"type": "object",
|
||||
"description": "可选附加结构化信息;系统会自动补充 content_format=markdown。"
|
||||
},
|
||||
"text": {
|
||||
"type": "string",
|
||||
"description": "可选。调用工具前发送给用户的提醒消息;为空则不发送。"
|
||||
}
|
||||
},
|
||||
"required": ["title", "content"]
|
||||
}
|
||||
}
|
||||
}),
|
||||
serde_json::json!({
|
||||
"type": "function",
|
||||
"function": {
|
||||
"name": "query_web_pages",
|
||||
"description": "查询已创建的动态 web 页面,并返回可直接发给用户的页面链接。用户询问之前生成的页面、邮件预览页面、页面链接或需要查找可发送页面时使用。",
|
||||
"parameters": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"page_type": {
|
||||
"type": "string",
|
||||
"description": "可选页面类型,例如 page、mail、markdown;为空时查询所有类型。"
|
||||
},
|
||||
"query": {
|
||||
"type": "string",
|
||||
"description": "可选关键词,会匹配标题、摘要、正文、来源和 metadata。"
|
||||
},
|
||||
"page": {
|
||||
"type": "integer",
|
||||
"description": "可选页码,默认 1。"
|
||||
},
|
||||
"per_page": {
|
||||
"type": "integer",
|
||||
"description": "可选每页数量,默认 10,最大 20。"
|
||||
},
|
||||
"text": {
|
||||
"type": "string",
|
||||
"description": "可选。调用工具前发送给用户的提醒消息;为空则不发送。"
|
||||
}
|
||||
},
|
||||
"required": []
|
||||
}
|
||||
}
|
||||
}),
|
||||
serde_json::json!({
|
||||
"type": "function",
|
||||
"function": {
|
||||
"name": "query_api_docs",
|
||||
"description": "查询 iAs HTTP API 文档。用户询问接口路径、参数、请求体、响应格式、页面数据 API、邮件 API、长期记忆 API 或控制台 API 时使用。返回结果可直接用于中文回复用户。",
|
||||
"parameters": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"query": {
|
||||
"type": "string",
|
||||
"description": "可选关键词,会匹配接口 id、分组、路径、摘要、说明、参数、响应和备注。"
|
||||
},
|
||||
"group": {
|
||||
"type": "string",
|
||||
"description": "可选分组,例如 api-docs、system、updates、web、mail、console、context。"
|
||||
},
|
||||
"endpoint_id": {
|
||||
"type": "string",
|
||||
"description": "可选。查询单个接口详情,例如 web.pages.list、web.pages.create、mail.messages.get、context.memories.search。传入后会忽略 query/group。"
|
||||
},
|
||||
"limit": {
|
||||
"type": "integer",
|
||||
"description": "可选。列表模式返回数量,默认 50,最大 100。"
|
||||
},
|
||||
"text": {
|
||||
"type": "string",
|
||||
"description": "可选。调用工具前发送给用户的提醒消息;为空则不发送。"
|
||||
}
|
||||
},
|
||||
"required": []
|
||||
}
|
||||
}
|
||||
}),
|
||||
serde_json::json!({
|
||||
"type": "function",
|
||||
"function": {
|
||||
|
||||
@@ -0,0 +1,206 @@
|
||||
use anyhow::{Context, anyhow};
|
||||
use serde::Deserialize;
|
||||
use serde_json::Value;
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
struct QueryApiDocsArgs {
|
||||
query: Option<String>,
|
||||
group: Option<String>,
|
||||
endpoint_id: Option<String>,
|
||||
limit: Option<usize>,
|
||||
#[allow(dead_code)]
|
||||
text: Option<String>,
|
||||
}
|
||||
|
||||
pub async fn query_api_docs(arguments: &str) -> String {
|
||||
match query_api_docs_inner(arguments).await {
|
||||
Ok(result) => result,
|
||||
Err(error) => format!("工具调用失败: 查询 API 文档失败: {error}"),
|
||||
}
|
||||
}
|
||||
|
||||
async fn query_api_docs_inner(arguments: &str) -> anyhow::Result<String> {
|
||||
let args: QueryApiDocsArgs =
|
||||
serde_json::from_str(arguments).context("解析 query_api_docs 参数失败")?;
|
||||
|
||||
if let Some(endpoint_id) = normalized_text(args.endpoint_id) {
|
||||
let endpoint_id = validate_endpoint_id(&endpoint_id)?;
|
||||
let url = reqwest::Url::parse(&format!(
|
||||
"{}/v1/api-docs/{}",
|
||||
api_docs_base_url(),
|
||||
endpoint_id
|
||||
))
|
||||
.context("构造 API 文档详情地址失败")?;
|
||||
let value = request_json(url).await?;
|
||||
return format_doc_detail(&value);
|
||||
}
|
||||
|
||||
let mut url = reqwest::Url::parse(&format!("{}/v1/api-docs", api_docs_base_url()))
|
||||
.context("构造 API 文档查询地址失败")?;
|
||||
{
|
||||
let mut query_pairs = url.query_pairs_mut();
|
||||
if let Some(query) = normalized_text(args.query) {
|
||||
query_pairs.append_pair("q", &query);
|
||||
}
|
||||
if let Some(group) = normalized_text(args.group) {
|
||||
query_pairs.append_pair("group", &group);
|
||||
}
|
||||
if let Some(limit) = args.limit {
|
||||
query_pairs.append_pair("limit", &limit.to_string());
|
||||
}
|
||||
}
|
||||
|
||||
let value = request_json(url).await?;
|
||||
format_doc_list(&value)
|
||||
}
|
||||
|
||||
async fn request_json(url: reqwest::Url) -> anyhow::Result<Value> {
|
||||
let client = reqwest::Client::new();
|
||||
let response = client
|
||||
.get(url.clone())
|
||||
.send()
|
||||
.await
|
||||
.with_context(|| format!("请求 API 文档接口失败: {url}"))?;
|
||||
let status = response.status();
|
||||
let body = response.text().await.unwrap_or_default();
|
||||
|
||||
if !status.is_success() {
|
||||
return Err(anyhow!("接口返回失败: status={status}, body={body}"));
|
||||
}
|
||||
|
||||
serde_json::from_str(&body).context("解析 API 文档接口响应失败")
|
||||
}
|
||||
|
||||
fn format_doc_list(value: &Value) -> anyhow::Result<String> {
|
||||
let docs = value
|
||||
.get("docs")
|
||||
.and_then(Value::as_array)
|
||||
.ok_or_else(|| anyhow!("接口响应缺少 docs 字段"))?;
|
||||
if docs.is_empty() {
|
||||
return Ok("没有查到匹配的 API 文档。".to_string());
|
||||
}
|
||||
|
||||
let total = value
|
||||
.get("total")
|
||||
.and_then(Value::as_u64)
|
||||
.unwrap_or(docs.len() as u64);
|
||||
let lines = docs
|
||||
.iter()
|
||||
.filter_map(format_doc_brief)
|
||||
.collect::<Vec<_>>()
|
||||
.join("\n");
|
||||
|
||||
Ok(format!(
|
||||
"API 文档查询结果:共 {total} 条,返回 {} 条。\n{}\n需要完整请求体、响应示例或备注时,可用 endpoint_id 查询单个接口。",
|
||||
docs.len(),
|
||||
lines
|
||||
))
|
||||
}
|
||||
|
||||
fn format_doc_detail(value: &Value) -> anyhow::Result<String> {
|
||||
let doc = value
|
||||
.get("doc")
|
||||
.ok_or_else(|| anyhow!("接口响应缺少 doc 字段"))?;
|
||||
let id = required_field(doc, "id")?;
|
||||
let method = required_field(doc, "method")?;
|
||||
let path = required_field(doc, "path")?;
|
||||
let group = required_field(doc, "group")?;
|
||||
let summary = required_field(doc, "summary")?;
|
||||
let description = required_field(doc, "description")?;
|
||||
let auth = required_field(doc, "auth")?;
|
||||
let response = required_field(doc, "response")?;
|
||||
|
||||
let mut sections = vec![
|
||||
format!("API 文档:{id}"),
|
||||
format!("{method} {path}"),
|
||||
format!("分组:{group}"),
|
||||
format!("摘要:{summary}"),
|
||||
format!("说明:{description}"),
|
||||
format!("鉴权:{auth}"),
|
||||
];
|
||||
|
||||
append_list_section(&mut sections, "Path 参数", doc.get("path_params"));
|
||||
append_list_section(&mut sections, "Query 参数", doc.get("query_params"));
|
||||
|
||||
if let Some(body) = doc.get("body").and_then(Value::as_str) {
|
||||
if !body.trim().is_empty() {
|
||||
sections.push(format!("请求体:{body}"));
|
||||
}
|
||||
}
|
||||
|
||||
sections.push(format!("响应示例:{response}"));
|
||||
append_list_section(&mut sections, "备注", doc.get("notes"));
|
||||
|
||||
Ok(sections.join("\n"))
|
||||
}
|
||||
|
||||
fn format_doc_brief(doc: &Value) -> Option<String> {
|
||||
let id = doc.get("id")?.as_str()?;
|
||||
let method = doc.get("method")?.as_str()?;
|
||||
let path = doc.get("path")?.as_str()?;
|
||||
let summary = doc.get("summary")?.as_str()?;
|
||||
let auth = doc
|
||||
.get("auth")
|
||||
.and_then(Value::as_str)
|
||||
.filter(|value| !value.trim().is_empty())
|
||||
.unwrap_or("-");
|
||||
Some(format!(
|
||||
"- [{id}] {method} {path}\n 摘要:{summary}\n 鉴权:{auth}"
|
||||
))
|
||||
}
|
||||
|
||||
fn append_list_section(sections: &mut Vec<String>, title: &str, value: Option<&Value>) {
|
||||
let items = value
|
||||
.and_then(Value::as_array)
|
||||
.map(|values| {
|
||||
values
|
||||
.iter()
|
||||
.filter_map(Value::as_str)
|
||||
.map(str::trim)
|
||||
.filter(|value| !value.is_empty())
|
||||
.map(|value| format!("- {value}"))
|
||||
.collect::<Vec<_>>()
|
||||
})
|
||||
.unwrap_or_default();
|
||||
if !items.is_empty() {
|
||||
sections.push(format!("{title}:\n{}", items.join("\n")));
|
||||
}
|
||||
}
|
||||
|
||||
fn required_field<'a>(doc: &'a Value, name: &str) -> anyhow::Result<&'a str> {
|
||||
doc.get(name)
|
||||
.and_then(Value::as_str)
|
||||
.map(str::trim)
|
||||
.filter(|value| !value.is_empty())
|
||||
.ok_or_else(|| anyhow!("接口文档缺少 {name} 字段"))
|
||||
}
|
||||
|
||||
fn normalized_text(value: Option<String>) -> Option<String> {
|
||||
value
|
||||
.map(|value| value.trim().to_string())
|
||||
.filter(|value| !value.is_empty())
|
||||
}
|
||||
|
||||
fn validate_endpoint_id(value: &str) -> anyhow::Result<String> {
|
||||
if value
|
||||
.bytes()
|
||||
.all(|byte| byte.is_ascii_alphanumeric() || byte == b'.' || byte == b'-' || byte == b'_')
|
||||
{
|
||||
Ok(value.to_string())
|
||||
} else {
|
||||
Err(anyhow!(
|
||||
"endpoint_id 只能包含字母、数字、点、下划线或连字符"
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
fn api_docs_base_url() -> String {
|
||||
std::env::var("IA_API_DOCS_BASE_URL")
|
||||
.or_else(|_| std::env::var("IA_WEB_BASE_URL"))
|
||||
.or_else(|_| std::env::var("WEB_BASE_URL"))
|
||||
.or_else(|_| std::env::var("HOST").map(|host| format!("http://{host}")))
|
||||
.unwrap_or_else(|_| "http://127.0.0.1:9003".to_string())
|
||||
.trim()
|
||||
.trim_end_matches('/')
|
||||
.replace("0.0.0.0", "127.0.0.1")
|
||||
}
|
||||
@@ -1,3 +1,4 @@
|
||||
pub mod api_docs;
|
||||
pub mod date_time;
|
||||
pub mod memory;
|
||||
pub mod web;
|
||||
|
||||
+193
-2
@@ -14,6 +14,17 @@ struct CreateWebPageArgs {
|
||||
text: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
struct CreateMarkdownPreviewArgs {
|
||||
title: String,
|
||||
summary: Option<String>,
|
||||
content: String,
|
||||
source_label: Option<String>,
|
||||
metadata: Option<Value>,
|
||||
#[allow(dead_code)]
|
||||
text: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
struct CreateWebPageResponse {
|
||||
success: bool,
|
||||
@@ -24,6 +35,35 @@ struct CreateWebPageResponse {
|
||||
url: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
struct QueryWebPagesArgs {
|
||||
page_type: Option<String>,
|
||||
query: Option<String>,
|
||||
page: Option<i64>,
|
||||
per_page: Option<i64>,
|
||||
#[allow(dead_code)]
|
||||
text: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
struct QueryWebPagesResponse {
|
||||
success: bool,
|
||||
page: i64,
|
||||
per_page: i64,
|
||||
total: i64,
|
||||
pages: Vec<WebPageItem>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
struct WebPageItem {
|
||||
slug: String,
|
||||
page_type: String,
|
||||
title: String,
|
||||
summary: Option<String>,
|
||||
source_label: Option<String>,
|
||||
created_at: Option<String>,
|
||||
}
|
||||
|
||||
pub async fn create_web_page(arguments: &str) -> String {
|
||||
match create_web_page_inner(arguments).await {
|
||||
Ok(result) => result,
|
||||
@@ -31,6 +71,20 @@ pub async fn create_web_page(arguments: &str) -> String {
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn create_markdown_preview(arguments: &str) -> String {
|
||||
match create_markdown_preview_inner(arguments).await {
|
||||
Ok(result) => result,
|
||||
Err(error) => format!("工具调用失败: 创建 Markdown 预览页面失败: {error}"),
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn query_web_pages(arguments: &str) -> String {
|
||||
match query_web_pages_inner(arguments).await {
|
||||
Ok(result) => result,
|
||||
Err(error) => format!("工具调用失败: 查询 web 页面失败: {error}"),
|
||||
}
|
||||
}
|
||||
|
||||
async fn create_web_page_inner(arguments: &str) -> anyhow::Result<String> {
|
||||
let args: CreateWebPageArgs =
|
||||
serde_json::from_str(arguments).context("解析 create_web_page 参数失败")?;
|
||||
@@ -41,14 +95,40 @@ async fn create_web_page_inner(arguments: &str) -> anyhow::Result<String> {
|
||||
return Err(anyhow!("content 不能为空"));
|
||||
}
|
||||
|
||||
let payload = json!({
|
||||
create_page_request(json!({
|
||||
"page_type": args.page_type,
|
||||
"title": args.title,
|
||||
"summary": args.summary,
|
||||
"content": args.content,
|
||||
"source_label": args.source_label,
|
||||
"metadata": args.metadata,
|
||||
});
|
||||
}))
|
||||
.await
|
||||
}
|
||||
|
||||
async fn create_markdown_preview_inner(arguments: &str) -> anyhow::Result<String> {
|
||||
let args: CreateMarkdownPreviewArgs =
|
||||
serde_json::from_str(arguments).context("解析 create_markdown_preview 参数失败")?;
|
||||
if args.title.trim().is_empty() {
|
||||
return Err(anyhow!("title 不能为空"));
|
||||
}
|
||||
if args.content.trim().is_empty() {
|
||||
return Err(anyhow!("content 不能为空"));
|
||||
}
|
||||
|
||||
let metadata = markdown_metadata(args.metadata);
|
||||
create_page_request(json!({
|
||||
"page_type": "markdown",
|
||||
"title": args.title,
|
||||
"summary": args.summary,
|
||||
"content": args.content,
|
||||
"source_label": args.source_label,
|
||||
"metadata": metadata,
|
||||
}))
|
||||
.await
|
||||
}
|
||||
|
||||
async fn create_page_request(payload: Value) -> anyhow::Result<String> {
|
||||
let base_url = web_base_url();
|
||||
let endpoint = format!("{base_url}/v1/web/pages");
|
||||
let client = reqwest::Client::new();
|
||||
@@ -84,6 +164,117 @@ async fn create_web_page_inner(arguments: &str) -> anyhow::Result<String> {
|
||||
))
|
||||
}
|
||||
|
||||
fn markdown_metadata(metadata: Option<Value>) -> Value {
|
||||
let mut value = metadata.unwrap_or_else(|| json!({}));
|
||||
let Value::Object(map) = &mut value else {
|
||||
return json!({
|
||||
"content_format": "markdown",
|
||||
"preview_type": "markdown",
|
||||
});
|
||||
};
|
||||
|
||||
map.insert("content_format".to_string(), json!("markdown"));
|
||||
map.insert("preview_type".to_string(), json!("markdown"));
|
||||
value
|
||||
}
|
||||
|
||||
async fn query_web_pages_inner(arguments: &str) -> anyhow::Result<String> {
|
||||
let args: QueryWebPagesArgs =
|
||||
serde_json::from_str(arguments).context("解析 query_web_pages 参数失败")?;
|
||||
let page = args.page.unwrap_or(1).max(1);
|
||||
let per_page = args.per_page.unwrap_or(10).clamp(1, 20);
|
||||
|
||||
let mut url = reqwest::Url::parse(&format!("{}/v1/web/pages", web_base_url()))
|
||||
.context("构造 web 页面查询地址失败")?;
|
||||
{
|
||||
let mut query_pairs = url.query_pairs_mut();
|
||||
query_pairs.append_pair("page", &page.to_string());
|
||||
query_pairs.append_pair("per_page", &per_page.to_string());
|
||||
if let Some(page_type) = optional_text(args.page_type) {
|
||||
query_pairs.append_pair("page_type", &page_type);
|
||||
}
|
||||
if let Some(query) = optional_text(args.query) {
|
||||
query_pairs.append_pair("q", &query);
|
||||
}
|
||||
}
|
||||
|
||||
let client = reqwest::Client::new();
|
||||
let response = client
|
||||
.get(url.clone())
|
||||
.send()
|
||||
.await
|
||||
.with_context(|| format!("请求 web 页面列表接口失败: {url}"))?;
|
||||
let status = response.status();
|
||||
let body = response.text().await.unwrap_or_default();
|
||||
if !status.is_success() {
|
||||
return Err(anyhow!("接口返回失败: status={status}, body={body}"));
|
||||
}
|
||||
|
||||
let result: QueryWebPagesResponse =
|
||||
serde_json::from_str(&body).context("解析 web 页面列表接口响应失败")?;
|
||||
if !result.success {
|
||||
return Err(anyhow!("接口未成功查询页面: {body}"));
|
||||
}
|
||||
if result.pages.is_empty() {
|
||||
return Ok("没有查到匹配的 web 页面。".to_string());
|
||||
}
|
||||
|
||||
let pages = result
|
||||
.pages
|
||||
.iter()
|
||||
.map(format_web_page_item)
|
||||
.collect::<Vec<_>>()
|
||||
.join("\n");
|
||||
|
||||
Ok(format!(
|
||||
"查到 {} 个匹配页面,本次返回 {} 个(第 {} 页,每页 {} 个):\n{}",
|
||||
result.total,
|
||||
result.pages.len(),
|
||||
result.page,
|
||||
result.per_page,
|
||||
pages
|
||||
))
|
||||
}
|
||||
|
||||
fn format_web_page_item(page: &WebPageItem) -> String {
|
||||
let url = web_page_url(page);
|
||||
let summary = page
|
||||
.summary
|
||||
.as_deref()
|
||||
.map(str::trim)
|
||||
.filter(|value| !value.is_empty())
|
||||
.unwrap_or("-");
|
||||
let source = page
|
||||
.source_label
|
||||
.as_deref()
|
||||
.map(str::trim)
|
||||
.filter(|value| !value.is_empty())
|
||||
.unwrap_or("-");
|
||||
let created_at = page.created_at.as_deref().unwrap_or("-");
|
||||
|
||||
format!(
|
||||
"- {}\n 链接:{}\n 类型:{} 来源:{} 创建时间:{}\n 摘要:{}",
|
||||
page.title, url, page.page_type, source, created_at, summary
|
||||
)
|
||||
}
|
||||
|
||||
fn web_page_url(page: &WebPageItem) -> String {
|
||||
let path = if page.page_type == "mail" {
|
||||
format!("/mail/{}", page.slug)
|
||||
} else if page.page_type == "markdown" || page.page_type == "md" {
|
||||
format!("/md/{}", page.slug)
|
||||
} else {
|
||||
format!("/web/{}/{}", page.page_type, page.slug)
|
||||
};
|
||||
format!("{}{}", web_base_url(), path)
|
||||
}
|
||||
|
||||
fn optional_text(value: Option<String>) -> Option<String> {
|
||||
value
|
||||
.map(|value| value.trim().to_string())
|
||||
.filter(|value| !value.is_empty())
|
||||
}
|
||||
|
||||
fn web_base_url() -> String {
|
||||
std::env::var("IA_WEB_BASE_URL")
|
||||
.or_else(|_| std::env::var("WEB_BASE_URL"))
|
||||
|
||||
Reference in New Issue
Block a user