Skip to content

Commit 205e226

Browse files
Merge pull request #5 from Olemi-llm-apprentice/feature/file-data-blob
refactor: update Excel cell editor and writer tools to use blob data …
2 parents a72c5be + 3bea1e6 commit 205e226

File tree

3 files changed

+30
-41
lines changed

3 files changed

+30
-41
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,3 +166,6 @@ cython_debug/
166166
# and can be added to the global gitignore or merged into this file. For a more nuclear
167167
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
168168
#.idea/
169+
170+
samples/
171+

tools/excel-cell-editor.py

Lines changed: 14 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,14 @@ def get_url_from_file_data(file_data: Any) -> str:
1515
return file_data['url']
1616
return ''
1717

18+
def get_blob_from_file_data(file_data: Any) -> bytes:
19+
"""Difyのファイルデータからblobを抽出する"""
20+
if hasattr(file_data, 'blob'):
21+
return file_data.blob
22+
elif isinstance(file_data, dict) and 'blob' in file_data:
23+
return file_data['blob']
24+
return None
25+
1826
class ExcelCellEditorTool(Tool):
1927
def _invoke(self, tool_parameters: dict[str, Any]) -> Generator[ToolInvokeMessage, None, None]:
2028
try:
@@ -27,35 +35,19 @@ def _invoke(self, tool_parameters: dict[str, Any]) -> Generator[ToolInvokeMessag
2735
)
2836
return
2937

30-
# 操作モードの取得
31-
# operation = tool_parameters.get("operation", "read")
32-
33-
# ファイルのURLを取得
34-
file_url = get_url_from_file_data(excel_file)
38+
# ファイルのblobデータを取得
39+
file_blob = get_blob_from_file_data(excel_file)
3540

36-
if not file_url:
37-
yield ToolInvokeMessage(
38-
type="text",
39-
message={"text": "ファイルのURLが見つかりません。"}
40-
)
41-
return
42-
43-
# ファイルをダウンロード
44-
try:
45-
response = requests.get(file_url)
46-
response.raise_for_status()
47-
file_bytes = response.content
48-
49-
except Exception as e:
41+
if not file_blob:
5042
yield ToolInvokeMessage(
5143
type="text",
52-
message={"text": f"ファイルのダウンロードに失敗しました: {str(e)}"}
44+
message={"text": "ファイルのblobデータが見つかりません。"}
5345
)
5446
return
5547

5648
# Excelファイルの読み込み
5749
try:
58-
wb = openpyxl.load_workbook(BytesIO(file_bytes))
50+
wb = openpyxl.load_workbook(BytesIO(file_blob))
5951
ws = wb.active
6052
except Exception as e:
6153
yield ToolInvokeMessage(
@@ -64,8 +56,7 @@ def _invoke(self, tool_parameters: dict[str, Any]) -> Generator[ToolInvokeMessag
6456
)
6557
return
6658

67-
# if operation == "read":
68-
# # セル内容の読み取り
59+
# セル内容の読み取り
6960
cell_data = {}
7061
for row in ws.iter_rows():
7162
for cell in row:

tools/excel-cell-writer.py

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,14 @@ def get_url_from_file_data(file_data: Any) -> str:
1515
return file_data['url']
1616
return ''
1717

18+
def get_blob_from_file_data(file_data: Any) -> bytes:
19+
"""Difyのファイルデータからblobを抽出する"""
20+
if hasattr(file_data, 'blob'):
21+
return file_data.blob
22+
elif isinstance(file_data, dict) and 'blob' in file_data:
23+
return file_data['blob']
24+
return None
25+
1826
class ExcelCellWriterTool(Tool):
1927
def _invoke(self, tool_parameters: dict[str, Any]) -> Generator[ToolInvokeMessage, None, None]:
2028
try:
@@ -36,32 +44,19 @@ def _invoke(self, tool_parameters: dict[str, Any]) -> Generator[ToolInvokeMessag
3644
)
3745
return
3846

39-
# ファイルのURLを取得
40-
file_url = get_url_from_file_data(excel_file)
47+
# ファイルのblobデータを取得
48+
file_blob = get_blob_from_file_data(excel_file)
4149

42-
if not file_url:
43-
yield ToolInvokeMessage(
44-
type="text",
45-
message={"text": "ファイルのURLが見つかりません。"}
46-
)
47-
return
48-
49-
# ファイルをダウンロード
50-
try:
51-
response = requests.get(file_url)
52-
response.raise_for_status()
53-
file_bytes = response.content
54-
55-
except Exception as e:
50+
if not file_blob:
5651
yield ToolInvokeMessage(
5752
type="text",
58-
message={"text": f"ファイルのダウンロードに失敗しました: {str(e)}"}
53+
message={"text": "ファイルのblobデータが見つかりません。"}
5954
)
6055
return
6156

6257
# Excelファイルの読み込み
6358
try:
64-
wb = openpyxl.load_workbook(BytesIO(file_bytes))
59+
wb = openpyxl.load_workbook(BytesIO(file_blob))
6560
ws = wb.active
6661
except Exception as e:
6762
yield ToolInvokeMessage(

0 commit comments

Comments
 (0)