Skip to content

Commit a5bba72

Browse files
committed
feat: Add get_or_create_user tool
1 parent 73fedb1 commit a5bba72

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed

tools/user/get_or_create_user.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
from collections.abc import Generator
2+
from typing import Any
3+
import json
4+
5+
from dify_plugin import Tool
6+
from dify_plugin.entities.tool import ToolInvokeMessage
7+
from memobase import MemoBaseClient
8+
9+
class GetOrCreateUserTool(Tool):
10+
def _invoke(self, tool_parameters: dict[str, Any]) -> Generator[ToolInvokeMessage]:
11+
user_id = tool_parameters.get("user_id", "")
12+
13+
try:
14+
credentials = self.runtime.credentials
15+
memobase_url = credentials.get("memobase_url", "")
16+
memobase_api_key = credentials.get("memobase_api_key", "")
17+
18+
client = MemoBaseClient(
19+
project_url=memobase_url,
20+
api_key=memobase_api_key,
21+
)
22+
23+
user_id = client.get_or_create_user(user_id)
24+
25+
yield self.create_json_message({"status": "success", "user_id": user_id})
26+
27+
except Exception as e:
28+
yield self.create_json_message({
29+
"status": "error",
30+
"message": str(e)
31+
})

tools/user/get_or_create_user.yaml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
identity:
2+
name: get_or_create_user
3+
author: acane0320
4+
label:
5+
en_US: Get or Create a User
6+
zh_Hans: 获取或创建用户
7+
pt_BR: Obter ou Criar Usuário
8+
description:
9+
human:
10+
en_US: Get or create a user in Memobase
11+
zh_Hans: 在 Memobase 中获取或创建用户
12+
pt_BR: Obter ou Criar Usuário no Memobase
13+
llm: Use this tool to get or create a user in Memobase. You need to provide the user ID.
14+
parameters:
15+
- name: user_id
16+
type: string
17+
required: true
18+
label:
19+
en_US: User ID
20+
zh_Hans: 用户 ID
21+
pt_BR: ID do Usuário
22+
human_description:
23+
en_US: Unique identifier for the user
24+
zh_Hans: 用户的唯一标识符
25+
pt_BR: Identificador exclusivo para o usuário
26+
llm_description: Unique identifier for the user.
27+
form: llm
28+
extra:
29+
python:
30+
source: tools/user/get_or_create_user.py

0 commit comments

Comments
 (0)