Skip to content

Commit 336a69b

Browse files
author
GlissemanTV
committed
change edits to content_edits for more clarity
1 parent 3f85a3a commit 336a69b

File tree

1 file changed

+35
-31
lines changed

1 file changed

+35
-31
lines changed

LLM_Export/docker/mcpo/tools/file_export_mcp.py

Lines changed: 35 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1809,6 +1809,8 @@ def _collect_needs(edit_items): # ADD
18091809
return needs
18101810

18111811

1812+
# ... existing code ...
1813+
18121814
@mcp.tool()
18131815
def edit_document(
18141816
file_id: str,
@@ -1821,7 +1823,7 @@ def edit_document(
18211823
The `edits` parameter must be a dictionary with the following structure:
18221824
{
18231825
"ops": [...], # Operations (structure modifications)
1824-
"edits": [...] # Content modifications
1826+
"conten_edits": [...] # Content modifications
18251827
}
18261828
18271829
## PPTX (PowerPoint) Files
@@ -1831,7 +1833,7 @@ def edit_document(
18311833
- ["insert_before", <slide_id:int>, "nK"] # Insert new slide before specified slide
18321834
- ["delete_slide", <slide_id:int>] # Delete specified slide
18331835
1834-
### Edits (edits):
1836+
### Content edits (conten_edits):
18351837
- ["sid:<slide_id>/shid:<shape_id>", text_or_list] # Edit specific shape by ID
18361838
- ["nK:slot:title", text_or_list] # Set title of new slide nK
18371839
- ["nK:slot:body", text_or_list] # Set body of new slide nK
@@ -1842,7 +1844,7 @@ def edit_document(
18421844
"ops": [
18431845
["insert_after", 256, "n0"]
18441846
],
1845-
"edits": [
1847+
"conten_edits": [
18461848
["sid:256/shid:4", ["Line 1", "Line 2", "Line 3"]],
18471849
["n0:slot:title", "New Slide Title"],
18481850
["n0:slot:body", ["Bullet 1", "Bullet 2"]]
@@ -1859,7 +1861,7 @@ def edit_document(
18591861
- ["insert_before", <para_xml_id:int>, "nK"] # Insert new paragraph before specified paragraph
18601862
- ["delete_paragraph", <para_xml_id:int>] # Delete specified paragraph
18611863
1862-
### Edits (edits):
1864+
### Content edits (conten_edits):
18631865
- ["pid:<para_xml_id>", text_or_list] # Edit paragraph by XML ID (from full_context_document)
18641866
- ["tid:<table_xml_id>/cid:<cell_xml_id>", text] # Edit table cell by XML ID
18651867
- ["nK", text_or_list] # Set content of new paragraph nK
@@ -1875,7 +1877,7 @@ def edit_document(
18751877
"ops": [
18761878
["insert_after", 140234567890123, "n0"]
18771879
],
1878-
"edits": [
1880+
"conten_edits": [
18791881
["pid:140234567890123", "Updated first paragraph"],
18801882
["pid:140234567890456", "Updated third paragraph"],
18811883
["n0", "New paragraph content"]
@@ -1893,7 +1895,7 @@ def edit_document(
18931895
- ["insert_column", "<sheet_name>", <col_idx:int>] # Insert column at position
18941896
- ["delete_column", "<sheet_name>", <col_idx:int>] # Delete column at position
18951897
1896-
### Edits (edits):
1898+
### Content edits (conten_edits):
18971899
- ["sheet:<name>/cell:<ref>", value] # Edit cell by sheet name and reference
18981900
- ["cell:<ref>", value] # Edit cell in active sheet
18991901
- ["<ref>", value]
@@ -1910,7 +1912,7 @@ def edit_document(
19101912
"ops": [
19111913
["insert_row", "Sheet1", 5]
19121914
],
1913-
"edits": [
1915+
"conten_edits": [
19141916
["sheet:Sheet1/cell:A1", "New Header"],
19151917
["sheet:Sheet1/cell:B3", 12345],
19161918
["C5", "Updated value"]
@@ -1929,7 +1931,7 @@ def edit_document(
19291931
2. For DOCX: use "pid:<para_xml_id>" where para_xml_id comes from full_context_document
19301932
3. For XLSX: use "sheet:<name>/cell:<ref>" format (as returned by full_context_document)
19311933
4. For PPTX: use "sid:<slide_id>/shid:<shape_id>" format (as returned by full_context_document)
1932-
5. All formats support ops for structure modifications and edits for content modifications
1934+
5. All formats support ops for structure modifications and conten_edits for content modifications
19331935
"""
19341936
temp_folder = f"/app/temp/{uuid.uuid4()}"
19351937
os.makedirs(temp_folder, exist_ok=True)
@@ -1960,22 +1962,22 @@ def edit_document(
19601962
continue
19611963
para_by_xml_id[para_id_counter] = para
19621964
para_id_counter += 1
1963-
1965+
19641966
for table in doc.tables:
19651967
table_xml_id = id(table._element)
19661968
table_by_xml_id[table_xml_id] = table
19671969
for row in table.rows:
19681970
for cell in row.cells:
19691971
cell_xml_id = id(cell._element)
19701972
cell_by_xml_id[cell_xml_id] = cell
1971-
1973+
19721974
if isinstance(edits, dict):
19731975
ops = edits.get("ops", []) or []
1974-
edit_items = edits.get("edits", []) or []
1976+
edit_items = edits.get("conten_edits", []) or []
19751977
else:
19761978
ops = []
19771979
edit_items = edits
1978-
1980+
19791981
new_refs = {}
19801982

19811983
for op in ops:
@@ -1990,37 +1992,37 @@ def edit_document(
19901992
anchor_para = para_by_xml_id.get(anchor_xml_id)
19911993
if anchor_para:
19921994
para_index = doc.paragraphs.index(anchor_para)
1993-
1995+
19941996
new_para = doc.add_paragraph()
1995-
1997+
19961998
anchor_element = anchor_para._element
19971999
parent = anchor_element.getparent()
19982000
parent.insert(parent.index(anchor_element) + 1, new_para._element)
1999-
2001+
20002002
new_para.style = anchor_para.style
2001-
2003+
20022004
new_xml_id = id(new_para._element)
20032005
new_refs[new_ref] = new_xml_id
20042006
para_by_xml_id[new_xml_id] = new_para
2005-
2007+
20062008
elif kind == "insert_before" and len(op) >= 3:
20072009
anchor_xml_id = int(op[1])
20082010
new_ref = op[2]
20092011

20102012
anchor_para = para_by_xml_id.get(anchor_xml_id)
20112013
if anchor_para:
20122014
new_para = doc.add_paragraph()
2013-
2015+
20142016
anchor_element = anchor_para._element
20152017
parent = anchor_element.getparent()
20162018
parent.insert(parent.index(anchor_element), new_para._element)
2017-
2019+
20182020
new_para.style = anchor_para.style
2019-
2021+
20202022
new_xml_id = id(new_para._element)
20212023
new_refs[new_ref] = new_xml_id
20222024
para_by_xml_id[new_xml_id] = new_para
2023-
2025+
20242026
elif kind == "delete_paragraph" and len(op) >= 2:
20252027
para_xml_id = int(op[1])
20262028
para = para_by_xml_id.get(para_xml_id)
@@ -2032,7 +2034,7 @@ def edit_document(
20322034
for target, new_text in edit_items:
20332035
if not isinstance(target, str):
20342036
continue
2035-
2037+
20362038
t = target.strip()
20372039

20382040
m = re.match(r"^pid:(\d+)$", t, flags=re.I)
@@ -2042,7 +2044,7 @@ def edit_document(
20422044
if para:
20432045
_apply_text_to_paragraph(para, new_text)
20442046
continue
2045-
2047+
20462048
m = re.match(r"^tid:(\d+)/cid:(\d+)$", t, flags=re.I)
20472049
if m:
20482050
table_xml_id = int(m.group(1))
@@ -2052,12 +2054,12 @@ def edit_document(
20522054
for para in cell.paragraphs:
20532055
for _ in range(len(para.runs)):
20542056
para._element.remove(para.runs[0]._element)
2055-
2057+
20562058
if cell.paragraphs:
20572059
first_para = cell.paragraphs[0]
20582060
first_para.add_run(str(new_text))
20592061
continue
2060-
2062+
20612063
m = re.match(r"^n(\d+)$", t, flags=re.I)
20622064
if m:
20632065
new_ref = t
@@ -2067,7 +2069,7 @@ def edit_document(
20672069
if para:
20682070
_apply_text_to_paragraph(para, new_text)
20692071
continue
2070-
2072+
20712073
edited_path = os.path.join(
20722074
temp_folder, f"{os.path.splitext(file_name)[0]}_edited.docx"
20732075
)
@@ -2085,8 +2087,8 @@ def edit_document(
20852087
wb = load_workbook(user_file)
20862088
ws = wb.active
20872089

2088-
edit_items = edits.get("edits", []) if isinstance(edits, dict) and "edits" in edits else edits
2089-
2090+
edit_items = edits.get("conten_edits", []) if isinstance(edits, dict) and "conten_edits" in edits else edits
2091+
20902092
for index, new_text in edit_items:
20912093
try:
20922094
if isinstance(index, str) and re.match(r"^[A-Z]+[0-9]+$", index.strip().upper()):
@@ -2119,7 +2121,7 @@ def edit_document(
21192121
prs = Presentation(user_file)
21202122
if isinstance(edits, dict):
21212123
ops = edits.get("ops", []) or []
2122-
edit_items = edits.get("edits", []) or []
2124+
edit_items = edits.get("conten_edits", []) or []
21232125
else:
21242126
ops = []
21252127
edit_items = edits
@@ -2186,8 +2188,8 @@ def edit_document(
21862188
prs.part.drop_rel(rId)
21872189
del sldIdLst[i]
21882190
order.pop(i)
2189-
slides_by_id.pop(sid, None)
2190-
2191+
slides_by_id.pop(sid, None)
2192+
21912193
for target, new_text in edit_items:
21922194
if not isinstance(target, str):
21932195
continue
@@ -2260,6 +2262,8 @@ def edit_document(
22602262
ensure_ascii=False
22612263
)
22622264

2265+
# ... existing code ...
2266+
22632267
@mcp.tool(
22642268
name="review_document",
22652269
title="Review and comment on various document types",

0 commit comments

Comments
 (0)