Skip to content

Commit 7cdf295

Browse files
Merge pull request #2 from Olemi-llm-apprentice/feature/cline-integration
Feature/cline integration
2 parents 2b1eddb + 0eb86e4 commit 7cdf295

File tree

6 files changed

+308
-1
lines changed

6 files changed

+308
-1
lines changed

provider/excel-plugin.yaml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@ identity:
1212
icon: icon.svg
1313
tools:
1414
- tools/markdown-to-excel.yaml
15+
- tools/excel-cell-editor.yaml
16+
- tools/excel-cell-writer.yaml
1517
- tools/excel-to-xml.yaml
1618
- tools/xml-to-excel.yaml
1719
extra:
1820
python:
19-
source: provider/excel-plugin.py
21+
source: provider/excel-plugin.py

test_data/test.xlsx

Whitespace-only changes.

tools/excel-cell-editor.py

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
from typing import Any, Generator
2+
import openpyxl
3+
import requests
4+
from io import BytesIO
5+
from urllib.parse import urlparse
6+
7+
from dify_plugin import Tool
8+
from dify_plugin.entities.tool import ToolInvokeMessage
9+
10+
def get_url_from_file_data(file_data: Any) -> str:
11+
"""DifyのファイルデータからURLを抽出する"""
12+
if hasattr(file_data, 'url'):
13+
return file_data.url
14+
elif isinstance(file_data, dict) and 'url' in file_data:
15+
return file_data['url']
16+
return ''
17+
18+
class ExcelCellEditorTool(Tool):
19+
def _invoke(self, tool_parameters: dict[str, Any]) -> Generator[ToolInvokeMessage, None, None]:
20+
try:
21+
# Excelファイルの取得
22+
excel_file = tool_parameters.get("excel_file")
23+
if not excel_file:
24+
yield ToolInvokeMessage(
25+
type="text",
26+
message={"text": "Excelファイルが提供されていません。"}
27+
)
28+
return
29+
30+
# 操作モードの取得
31+
# operation = tool_parameters.get("operation", "read")
32+
33+
# ファイルのURLを取得
34+
file_url = get_url_from_file_data(excel_file)
35+
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:
50+
yield ToolInvokeMessage(
51+
type="text",
52+
message={"text": f"ファイルのダウンロードに失敗しました: {str(e)}"}
53+
)
54+
return
55+
56+
# Excelファイルの読み込み
57+
try:
58+
wb = openpyxl.load_workbook(BytesIO(file_bytes))
59+
ws = wb.active
60+
except Exception as e:
61+
yield ToolInvokeMessage(
62+
type="text",
63+
message={"text": f"Excelファイル読み込みエラー: {str(e)}"}
64+
)
65+
return
66+
67+
# if operation == "read":
68+
# # セル内容の読み取り
69+
cell_data = {}
70+
for row in ws.iter_rows():
71+
for cell in row:
72+
if cell.value:
73+
cell_data[cell.coordinate] = cell.value
74+
75+
yield ToolInvokeMessage(
76+
type="text",
77+
message={"text": str(cell_data)}
78+
)
79+
80+
except Exception as e:
81+
yield ToolInvokeMessage(
82+
type="text",
83+
message={"text": f"エラーが発生しました: {str(e)}"}
84+
)

tools/excel-cell-editor.yaml

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
identity:
2+
name: excel-cell-editor
3+
author: Dify
4+
label:
5+
en_US: Excel Cell Editor
6+
ja_JP: Excelセルエディタ
7+
zh_Hans: Excel单元格编辑器
8+
9+
description:
10+
human:
11+
en_US: Read and edit Excel cell contents
12+
ja_JP: Excelのセル内容を読み取り・編集します
13+
zh_Hans: 读取和编辑Excel单元格内容
14+
llm: A tool for reading and editing Excel cell contents
15+
16+
parameters:
17+
- name: excel_file
18+
type: file
19+
required: true
20+
label:
21+
en_US: Excel File
22+
ja_JP: Excelファイル
23+
zh_Hans: Excel文件
24+
human_description:
25+
en_US: The Excel file to read/edit
26+
ja_JP: 読み取り/編集するExcelファイル
27+
zh_Hans: 要读取/编辑的Excel文件
28+
llm_description: The Excel file that needs to be read/edited
29+
form: llm
30+
31+
extra:
32+
python:
33+
source: tools/excel-cell-editor.py

tools/excel-cell-writer.py

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
from typing import Any, Generator
2+
import openpyxl
3+
import requests
4+
from io import BytesIO
5+
from urllib.parse import urlparse
6+
7+
from dify_plugin import Tool
8+
from dify_plugin.entities.tool import ToolInvokeMessage
9+
10+
def get_url_from_file_data(file_data: Any) -> str:
11+
"""DifyのファイルデータからURLを抽出する"""
12+
if hasattr(file_data, 'url'):
13+
return file_data.url
14+
elif isinstance(file_data, dict) and 'url' in file_data:
15+
return file_data['url']
16+
return ''
17+
18+
class ExcelCellWriterTool(Tool):
19+
def _invoke(self, tool_parameters: dict[str, Any]) -> Generator[ToolInvokeMessage, None, None]:
20+
try:
21+
# Excelファイルの取得
22+
excel_file = tool_parameters.get("excel_file")
23+
if not excel_file:
24+
yield ToolInvokeMessage(
25+
type="text",
26+
message={"text": "Excelファイルが提供されていません。"}
27+
)
28+
return
29+
30+
# 編集データの取得
31+
updates = tool_parameters.get("updates", {})
32+
if not updates:
33+
yield ToolInvokeMessage(
34+
type="text",
35+
message={"text": "更新データが提供されていません。"}
36+
)
37+
return
38+
39+
# ファイルのURLを取得
40+
file_url = get_url_from_file_data(excel_file)
41+
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:
56+
yield ToolInvokeMessage(
57+
type="text",
58+
message={"text": f"ファイルのダウンロードに失敗しました: {str(e)}"}
59+
)
60+
return
61+
62+
# Excelファイルの読み込み
63+
try:
64+
wb = openpyxl.load_workbook(BytesIO(file_bytes))
65+
ws = wb.active
66+
except Exception as e:
67+
yield ToolInvokeMessage(
68+
type="text",
69+
message={"text": f"Excelファイル読み込みエラー: {str(e)}"}
70+
)
71+
return
72+
73+
# セル内容の更新
74+
try:
75+
# updatesが文字列の場合、JSONとしてパース
76+
if isinstance(updates, str):
77+
import json
78+
try:
79+
# シングルクォートをダブルクォートに置換
80+
updates = updates.replace("'", '"')
81+
# datetimeオブジェクトを文字列に変換
82+
updates = updates.replace(
83+
"datetime.datetime(", '"datetime('
84+
).replace(")", ')"')
85+
updates = json.loads(updates)
86+
except json.JSONDecodeError as e:
87+
yield ToolInvokeMessage(
88+
type="text",
89+
message={"text": f"JSONパースエラー: {str(e)}\n入力データ: {updates}"}
90+
)
91+
return
92+
93+
# updatesが辞書型か確認
94+
if not isinstance(updates, dict):
95+
yield ToolInvokeMessage(
96+
type="text",
97+
message={"text": f"無効なupdates形式です。辞書型またはJSON文字列を指定してください\n入力データ: {updates}"}
98+
)
99+
return
100+
101+
# セル更新処理
102+
for cell_ref, new_value in updates.items():
103+
ws[cell_ref] = new_value
104+
105+
except json.JSONDecodeError:
106+
yield ToolInvokeMessage(
107+
type="text",
108+
message={"text": "JSONパースエラー: updatesが有効なJSON形式ではありません"}
109+
)
110+
return
111+
except Exception as e:
112+
yield ToolInvokeMessage(
113+
type="text",
114+
message={"text": f"セル更新エラー: {str(e)}"}
115+
)
116+
return
117+
118+
# 編集結果を保存
119+
try:
120+
output = BytesIO()
121+
wb.save(output)
122+
output.seek(0)
123+
124+
yield self.create_blob_message(
125+
blob=output.getvalue(),
126+
meta={
127+
"mime_type": "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
128+
"filename": "edited_file.xlsx"
129+
}
130+
)
131+
except Exception as e:
132+
yield ToolInvokeMessage(
133+
type="text",
134+
message={"text": f"ファイル保存エラー: {str(e)}"}
135+
)
136+
return
137+
138+
except Exception as e:
139+
yield ToolInvokeMessage(
140+
type="text",
141+
message={"text": f"エラーが発生しました: {str(e)}"}
142+
)

tools/excel-cell-writer.yaml

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
identity:
2+
name: excel-cell-writer
3+
author: Dify
4+
label:
5+
en_US: Excel Cell Writer
6+
ja_JP: Excelセルライター
7+
zh_Hans: Excel单元格写入器
8+
9+
description:
10+
human:
11+
en_US: Write data to specific cells in Excel files
12+
ja_JP: Excelファイルの指定されたセルにデータを書き込みます
13+
zh_Hans: 将数据写入Excel文件的指定单元格
14+
llm: A tool for writing data to specific cells in Excel files
15+
16+
parameters:
17+
- name: excel_file
18+
type: file
19+
required: true
20+
label:
21+
en_US: Excel File
22+
ja_JP: Excelファイル
23+
zh_Hans: Excel文件
24+
human_description:
25+
en_US: The Excel file to write data to
26+
ja_JP: データを書き込むExcelファイル
27+
zh_Hans: 要写入数据的Excel文件
28+
llm_description: The Excel file that needs to be written to
29+
form: llm
30+
- name: updates
31+
type: string
32+
required: true
33+
label:
34+
en_US: Cell Updates
35+
ja_JP: セル更新
36+
zh_Hans: 单元格更新
37+
human_description:
38+
en_US: Specify the cell references and their new values
39+
ja_JP: セルの参照と新しい値を指定
40+
zh_Hans: 指定单元格引用及其新值
41+
llm_description: A mapping of cell references to new values
42+
form: llm
43+
44+
extra:
45+
python:
46+
source: tools/excel-cell-writer.py

0 commit comments

Comments
 (0)