forked from langgenius/dify
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(Tools): Refactor the base table plugin (langgenius#9182)
Co-authored-by: 黎斌 <libin.23@bytedance.com>
- Loading branch information
Showing
43 changed files
with
1,230 additions
and
1,282 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
47 changes: 0 additions & 47 deletions
47
api/core/tools/provider/builtin/feishu_base/_assets/icon.svg
This file was deleted.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,7 @@ | ||
from core.tools.provider.builtin.feishu_base.tools.get_tenant_access_token import GetTenantAccessTokenTool | ||
from core.tools.provider.builtin_tool_provider import BuiltinToolProviderController | ||
from core.tools.utils.feishu_api_utils import auth | ||
|
||
|
||
class FeishuBaseProvider(BuiltinToolProviderController): | ||
def _validate_credentials(self, credentials: dict) -> None: | ||
GetTenantAccessTokenTool() | ||
pass | ||
auth(credentials) |
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
56 changes: 0 additions & 56 deletions
56
api/core/tools/provider/builtin/feishu_base/tools/add_base_record.py
This file was deleted.
Oops, something went wrong.
66 changes: 0 additions & 66 deletions
66
api/core/tools/provider/builtin/feishu_base/tools/add_base_record.yaml
This file was deleted.
Oops, something went wrong.
21 changes: 21 additions & 0 deletions
21
api/core/tools/provider/builtin/feishu_base/tools/add_records.py
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,21 @@ | ||
from typing import Any | ||
|
||
from core.tools.entities.tool_entities import ToolInvokeMessage | ||
from core.tools.tool.builtin_tool import BuiltinTool | ||
from core.tools.utils.feishu_api_utils import FeishuRequest | ||
|
||
|
||
class AddRecordsTool(BuiltinTool): | ||
def _invoke(self, user_id: str, tool_parameters: dict[str, Any]) -> ToolInvokeMessage: | ||
app_id = self.runtime.credentials.get("app_id") | ||
app_secret = self.runtime.credentials.get("app_secret") | ||
client = FeishuRequest(app_id, app_secret) | ||
|
||
app_token = tool_parameters.get("app_token") | ||
table_id = tool_parameters.get("table_id") | ||
table_name = tool_parameters.get("table_name") | ||
records = tool_parameters.get("records") | ||
user_id_type = tool_parameters.get("user_id_type", "open_id") | ||
|
||
res = client.add_records(app_token, table_id, table_name, records, user_id_type) | ||
return self.create_json_message(res) |
91 changes: 91 additions & 0 deletions
91
api/core/tools/provider/builtin/feishu_base/tools/add_records.yaml
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,91 @@ | ||
identity: | ||
name: add_records | ||
author: Doug Lea | ||
label: | ||
en_US: Add Records | ||
zh_Hans: 新增多条记录 | ||
description: | ||
human: | ||
en_US: Add Multiple Records to Multidimensional Table | ||
zh_Hans: 在多维表格数据表中新增多条记录 | ||
llm: A tool for adding multiple records to a multidimensional table. (在多维表格数据表中新增多条记录) | ||
parameters: | ||
- name: app_token | ||
type: string | ||
required: true | ||
label: | ||
en_US: app_token | ||
zh_Hans: app_token | ||
human_description: | ||
en_US: Unique identifier for the multidimensional table, supports inputting document URL. | ||
zh_Hans: 多维表格的唯一标识符,支持输入文档 URL。 | ||
llm_description: 多维表格的唯一标识符,支持输入文档 URL。 | ||
form: llm | ||
|
||
- name: table_id | ||
type: string | ||
required: false | ||
label: | ||
en_US: table_id | ||
zh_Hans: table_id | ||
human_description: | ||
en_US: Unique identifier for the multidimensional table data, either table_id or table_name must be provided, cannot be empty simultaneously. | ||
zh_Hans: 多维表格数据表的唯一标识符,table_id 和 table_name 至少需要提供一个,不能同时为空。 | ||
llm_description: 多维表格数据表的唯一标识符,table_id 和 table_name 至少需要提供一个,不能同时为空。 | ||
form: llm | ||
|
||
- name: table_name | ||
type: string | ||
required: false | ||
label: | ||
en_US: table_name | ||
zh_Hans: table_name | ||
human_description: | ||
en_US: Name of the multidimensional table data, either table_name or table_id must be provided, cannot be empty simultaneously. | ||
zh_Hans: 多维表格数据表的名称,table_name 和 table_id 至少需要提供一个,不能同时为空。 | ||
llm_description: 多维表格数据表的名称,table_name 和 table_id 至少需要提供一个,不能同时为空。 | ||
form: llm | ||
|
||
- name: records | ||
type: string | ||
required: true | ||
label: | ||
en_US: records | ||
zh_Hans: 记录列表 | ||
human_description: | ||
en_US: | | ||
List of records to be added in this request. Example value: [{"multi-line-text":"text content","single_select":"option 1","date":1674206443000}] | ||
For supported field types, refer to the integration guide (https://open.larkoffice.com/document/server-docs/docs/bitable-v1/notification). For data structures of different field types, refer to the data structure overview (https://open.larkoffice.com/document/server-docs/docs/bitable-v1/bitable-structure). | ||
zh_Hans: | | ||
本次请求将要新增的记录列表,示例值:[{"多行文本":"文本内容","单选":"选项 1","日期":1674206443000}]。 | ||
当前接口支持的字段类型请参考接入指南(https://open.larkoffice.com/document/server-docs/docs/bitable-v1/notification),不同类型字段的数据结构请参考数据结构概述(https://open.larkoffice.com/document/server-docs/docs/bitable-v1/bitable-structure)。 | ||
llm_description: | | ||
本次请求将要新增的记录列表,示例值:[{"多行文本":"文本内容","单选":"选项 1","日期":1674206443000}]。 | ||
当前接口支持的字段类型请参考接入指南(https://open.larkoffice.com/document/server-docs/docs/bitable-v1/notification),不同类型字段的数据结构请参考数据结构概述(https://open.larkoffice.com/document/server-docs/docs/bitable-v1/bitable-structure)。 | ||
form: llm | ||
|
||
- name: user_id_type | ||
type: select | ||
required: false | ||
options: | ||
- value: open_id | ||
label: | ||
en_US: open_id | ||
zh_Hans: open_id | ||
- value: union_id | ||
label: | ||
en_US: union_id | ||
zh_Hans: union_id | ||
- value: user_id | ||
label: | ||
en_US: user_id | ||
zh_Hans: user_id | ||
default: "open_id" | ||
label: | ||
en_US: user_id_type | ||
zh_Hans: 用户 ID 类型 | ||
human_description: | ||
en_US: User ID type, optional values are open_id, union_id, user_id, with a default value of open_id. | ||
zh_Hans: 用户 ID 类型,可选值有 open_id、union_id、user_id,默认值为 open_id。 | ||
llm_description: 用户 ID 类型,可选值有 open_id、union_id、user_id,默认值为 open_id。 | ||
form: form |
43 changes: 10 additions & 33 deletions
43
api/core/tools/provider/builtin/feishu_base/tools/create_base.py
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,41 +1,18 @@ | ||
import json | ||
from typing import Any, Union | ||
|
||
import httpx | ||
from typing import Any | ||
|
||
from core.tools.entities.tool_entities import ToolInvokeMessage | ||
from core.tools.tool.builtin_tool import BuiltinTool | ||
from core.tools.utils.feishu_api_utils import FeishuRequest | ||
|
||
|
||
class CreateBaseTool(BuiltinTool): | ||
def _invoke( | ||
self, user_id: str, tool_parameters: dict[str, Any] | ||
) -> Union[ToolInvokeMessage, list[ToolInvokeMessage]]: | ||
url = "https://open.feishu.cn/open-apis/bitable/v1/apps" | ||
|
||
access_token = tool_parameters.get("Authorization", "") | ||
if not access_token: | ||
return self.create_text_message("Invalid parameter access_token") | ||
|
||
name = tool_parameters.get("name", "") | ||
folder_token = tool_parameters.get("folder_token", "") | ||
|
||
headers = { | ||
"Content-Type": "application/json", | ||
"Authorization": f"Bearer {access_token}", | ||
} | ||
def _invoke(self, user_id: str, tool_parameters: dict[str, Any]) -> ToolInvokeMessage: | ||
app_id = self.runtime.credentials.get("app_id") | ||
app_secret = self.runtime.credentials.get("app_secret") | ||
client = FeishuRequest(app_id, app_secret) | ||
|
||
params = {} | ||
payload = {"name": name, "folder_token": folder_token} | ||
name = tool_parameters.get("name") | ||
folder_token = tool_parameters.get("folder_token") | ||
|
||
try: | ||
res = httpx.post(url, headers=headers, params=params, json=payload, timeout=30) | ||
res_json = res.json() | ||
if res.is_success: | ||
return self.create_text_message(text=json.dumps(res_json)) | ||
else: | ||
return self.create_text_message( | ||
f"Failed to create base, status code: {res.status_code}, response: {res.text}" | ||
) | ||
except Exception as e: | ||
return self.create_text_message("Failed to create base. {}".format(e)) | ||
res = client.create_base(name, folder_token) | ||
return self.create_json_message(res) |
Oops, something went wrong.