Skip to content

Commit

Permalink
feat(Tools): Refactor the base table plugin (langgenius#9182)
Browse files Browse the repository at this point in the history
Co-authored-by: 黎斌 <libin.23@bytedance.com>
  • Loading branch information
2 people authored and JunXu01 committed Nov 9, 2024
1 parent d0a152f commit 13a6d5a
Show file tree
Hide file tree
Showing 43 changed files with 1,230 additions and 1,282 deletions.
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 api/core/tools/provider/builtin/feishu_base/_assets/icon.svg

This file was deleted.

5 changes: 2 additions & 3 deletions api/core/tools/provider/builtin/feishu_base/feishu_base.py
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)
28 changes: 25 additions & 3 deletions api/core/tools/provider/builtin/feishu_base/feishu_base.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,32 @@ identity:
en_US: Feishu Base
zh_Hans: 飞书多维表格
description:
en_US: Feishu Base
zh_Hans: 飞书多维表格
icon: icon.svg
en_US: |
Feishu base, requires the following permissions: bitable:app.
zh_Hans: |
飞书多维表格,需要开通以下权限: bitable:app。
icon: icon.png
tags:
- social
- productivity
credentials_for_provider:
app_id:
type: text-input
required: true
label:
en_US: APP ID
placeholder:
en_US: Please input your feishu app id
zh_Hans: 请输入你的飞书 app id
help:
en_US: Get your app_id and app_secret from Feishu
zh_Hans: 从飞书获取您的 app_id 和 app_secret
url: https://open.larkoffice.com/app
app_secret:
type: secret-input
required: true
label:
en_US: APP Secret
placeholder:
en_US: Please input your app secret
zh_Hans: 请输入你的飞书 app secret

This file was deleted.

This file was deleted.

21 changes: 21 additions & 0 deletions api/core/tools/provider/builtin/feishu_base/tools/add_records.py
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 api/core/tools/provider/builtin/feishu_base/tools/add_records.yaml
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 api/core/tools/provider/builtin/feishu_base/tools/create_base.py
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)
Loading

0 comments on commit 13a6d5a

Please sign in to comment.