Skip to content

Commit c748f59

Browse files
authored
feat(ai): add support for submitting and cancelling documents (#1688)
1 parent 3ef60be commit c748f59

File tree

7 files changed

+153
-7
lines changed

7 files changed

+153
-7
lines changed

frontend/src/components/feature/settings/ai/functions/FunctionConstants.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,24 @@ export const FUNCTION_TYPES = [
5353
requires_write_permissions: true,
5454
type: "Bulk Operations"
5555
},
56+
{
57+
value: "Submit Document",
58+
description: "Submit any document in the system.",
59+
requires_write_permissions: true,
60+
type: "Standard"
61+
},
62+
{
63+
value: "Cancel Document",
64+
description: "Cancel any document in the system.",
65+
requires_write_permissions: true,
66+
type: "Standard"
67+
},
68+
{
69+
value: "Get Amended Document",
70+
description: "Get the amended document for a given document. This function is only available for documents that have been cancelled and then amended.",
71+
requires_write_permissions: false,
72+
type: "Standard"
73+
},
5674
{
5775
value: "Custom Function",
5876
description: "Custom function to be used in the system.",

frontend/src/components/feature/settings/ai/functions/FunctionForm.tsx

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ const FunctionForm = ({ isEdit }: { isEdit?: boolean }) => {
2626
<Tabs.Root defaultValue='function_details'>
2727
<Tabs.List>
2828
<Tabs.Trigger value='function_details'><LuSquareFunction {...ICON_PROPS} /> Details</Tabs.Trigger>
29-
<Tabs.Trigger value='variables' disabled={in_list(["Get Document", "Get Multiple Documents", "Delete Document", "Delete Multiple Documents", "Attach File to Document"], type)}><LuVariable {...ICON_PROPS} /> Variables</Tabs.Trigger>
29+
<Tabs.Trigger value='variables' disabled={in_list(["Get Document", "Get Multiple Documents", "Delete Document", "Delete Multiple Documents", "Attach File to Document", "Submit Document", "Cancel Document", "Get Amended Document"], type)}><LuVariable {...ICON_PROPS} /> Variables</Tabs.Trigger>
3030
</Tabs.List>
3131
<Stack pt='4'>
3232
<AINotEnabledCallout />
@@ -278,7 +278,7 @@ const ReferenceDoctypeField = () => {
278278

279279
const type = watch('type')
280280

281-
const DOCUMENT_REF_FUNCTIONS = ["Get Document", "Get Multiple Documents", "Get List", "Create Document", "Create Multiple Documents", "Update Document", "Update Multiple Documents", "Delete Document", "Delete Multiple Documents"]
281+
const DOCUMENT_REF_FUNCTIONS = ["Get Document", "Get Multiple Documents", "Get List", "Create Document", "Create Multiple Documents", "Update Document", "Update Multiple Documents", "Delete Document", "Delete Multiple Documents", "Submit Document", "Cancel Document", "Get Amended Document"]
282282

283283
const onReferenceDoctypeChange = (e: ChangeEvent<HTMLInputElement>) => {
284284

@@ -324,6 +324,19 @@ const ReferenceDoctypeField = () => {
324324
description = `This function deletes multiple ${e.target.value} from the system.`
325325
function_name = `delete_${e.target.value.toLowerCase().replace(/\s/g, '_')}s`
326326
}
327+
if (type === 'Submit Document') {
328+
description = `This function submits a ${e.target.value} in the system.`
329+
function_name = `submit_${e.target.value.toLowerCase().replace(/\s/g, '_')}`
330+
}
331+
if (type === 'Cancel Document') {
332+
description = `This function cancels a ${e.target.value} in the system.`
333+
function_name = `cancel_${e.target.value.toLowerCase().replace(/\s/g, '_')}`
334+
}
335+
336+
if (type === 'Get Amended Document') {
337+
description = `This function gets the amended document for a ${e.target.value} in the system.`
338+
function_name = `get_amended_${e.target.value.toLowerCase().replace(/\s/g, '_')}`
339+
}
327340

328341
if (function_name && !function_name_exists) {
329342
setValue('function_name', function_name)

frontend/src/types/RavenAI/RavenAIFunction.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export interface RavenAIFunction{
1818
/** Function Path : Small Text */
1919
function_path?: string
2020
/** Type : Select */
21-
type: "Get Document" | "Get Multiple Documents" | "Get List" | "Create Document" | "Create Multiple Documents" | "Update Document" | "Update Multiple Documents" | "Delete Document" | "Delete Multiple Documents" | "Custom Function" | "Send Message" | "Attach File to Document" | "Get Report Result"
21+
type: "Get Document" | "Get Multiple Documents" | "Get List" | "Create Document" | "Create Multiple Documents" | "Update Document" | "Update Multiple Documents" | "Delete Document" | "Delete Multiple Documents" | "Submit Document" | "Cancel Document" | "Get Amended Document" | "Custom Function" | "Send Message" | "Attach File to Document" | "Get Report Result"
2222
/** Reference DocType : Link - DocType */
2323
reference_doctype?: string
2424
/** Pass parameters as JSON : Check - If checked, the params will be passed as a JSON object instead of named parameters */

raven/ai/functions.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,54 @@ def delete_documents(doctype: str, document_ids: list):
107107
return {"document_ids": document_ids, "message": "Documents deleted", "doctype": doctype}
108108

109109

110+
def submit_document(doctype: str, document_id: str):
111+
"""
112+
Submit a document in the database
113+
"""
114+
doc = frappe.get_doc(doctype, document_id)
115+
doc.submit()
116+
return {
117+
"document_id": document_id,
118+
"message": f"{doctype} {document_id} submitted",
119+
"doctype": doctype,
120+
}
121+
122+
123+
def cancel_document(doctype: str, document_id: str):
124+
"""
125+
Cancel a document in the database
126+
"""
127+
doc = frappe.get_doc(doctype, document_id)
128+
doc.cancel()
129+
return {
130+
"document_id": document_id,
131+
"message": f"{doctype} {document_id} cancelled",
132+
"doctype": doctype,
133+
}
134+
135+
136+
def get_amended_document_id(doctype: str, document_id: str):
137+
"""
138+
Get the amended document for a given document
139+
"""
140+
amended_doc = frappe.db.exists(doctype, {"amended_from": document_id})
141+
if amended_doc:
142+
return amended_doc
143+
else:
144+
return {"message": f"{doctype} {document_id} is not amended"}
145+
146+
147+
def get_amended_document(doctype: str, document_id: str):
148+
"""
149+
Get the amended document for a given document
150+
"""
151+
amended_doc = frappe.db.exists(doctype, {"amended_from": document_id})
152+
if amended_doc:
153+
return client.get(doctype, name=document_id)
154+
else:
155+
return {"message": f"{doctype} {document_id} is not amended", "doctype": doctype}
156+
157+
110158
def attach_file_to_document(doctype: str, document_id: str, file_path: str):
111159
"""
112160
Attach a file to a document in the database

raven/ai/handler.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,16 @@
88

99
from raven.ai.functions import (
1010
attach_file_to_document,
11+
cancel_document,
1112
create_document,
1213
create_documents,
1314
delete_document,
1415
delete_documents,
16+
get_amended_document,
1517
get_document,
1618
get_documents,
1719
get_list,
20+
submit_document,
1821
update_document,
1922
update_documents,
2023
)
@@ -140,6 +143,20 @@ def handle_requires_action(self, data, run_id):
140143
self.publish_event(f"Fetching multiple {function.reference_doctype}s...")
141144
function_output = get_documents(function.reference_doctype, **args)
142145

146+
if function.type == "Submit Document":
147+
self.publish_event(f"Submitting {function.reference_doctype} {args.get('document_id')}...")
148+
function_output = submit_document(function.reference_doctype, **args)
149+
150+
if function.type == "Cancel Document":
151+
self.publish_event(f"Cancelling {function.reference_doctype} {args.get('document_id')}...")
152+
function_output = cancel_document(function.reference_doctype, **args)
153+
154+
if function.type == "Get Amended Document":
155+
self.publish_event(
156+
f"Fetching amended document for {function.reference_doctype} {args.get('document_id')}..."
157+
)
158+
function_output = get_amended_document(function.reference_doctype, **args)
159+
143160
if function.type == "Delete Document":
144161
self.publish_event(
145162
"Deleting {} {}...".format(function.reference_doctype, args.get("document_id"))

raven/raven_ai/doctype/raven_ai_function/raven_ai_function.json

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
"fieldname": "type",
4949
"fieldtype": "Select",
5050
"label": "Type",
51-
"options": "Get Document\nGet Multiple Documents\nGet List\nCreate Document\nCreate Multiple Documents\nUpdate Document\nUpdate Multiple Documents\nDelete Document\nDelete Multiple Documents\nCustom Function\nSend Message\nAttach File to Document\nGet Report Result",
51+
"options": "Get Document\nGet Multiple Documents\nGet List\nCreate Document\nCreate Multiple Documents\nUpdate Document\nUpdate Multiple Documents\nDelete Document\nDelete Multiple Documents\nSubmit Document\nCancel Document\nGet Amended Document\nCustom Function\nSend Message\nAttach File to Document\nGet Report Result",
5252
"reqd": 1
5353
},
5454
{
@@ -103,9 +103,10 @@
103103
"label": "Strict"
104104
}
105105
],
106+
"grid_page_length": 50,
106107
"index_web_pages_for_search": 1,
107108
"links": [],
108-
"modified": "2024-12-07 23:32:48.633167",
109+
"modified": "2025-05-18 17:42:22.628503",
109110
"modified_by": "Administrator",
110111
"module": "Raven AI",
111112
"name": "Raven AI Function",
@@ -137,8 +138,9 @@
137138
"write": 1
138139
}
139140
],
141+
"row_format": "Dynamic",
140142
"search_fields": "description",
141143
"sort_field": "creation",
142144
"sort_order": "DESC",
143145
"states": []
144-
}
146+
}

raven/raven_ai/doctype/raven_ai_function/raven_ai_function.py

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ class RavenAIFunction(Document):
4141
"Update Multiple Documents",
4242
"Delete Document",
4343
"Delete Multiple Documents",
44+
"Submit Document",
45+
"Cancel Document",
46+
"Get Amended Document",
4447
"Custom Function",
4548
"Send Message",
4649
"Attach File to Document",
@@ -62,7 +65,13 @@ def before_validate(self):
6265
if self.type in WRITE_PERMISSIONS:
6366
self.requires_write_permissions = 1
6467

65-
READ_PERMISSIONS = ["Get Document", "Get Multiple Documents", "Get Report Result", "Get List"]
68+
READ_PERMISSIONS = [
69+
"Get Document",
70+
"Get Multiple Documents",
71+
"Get Report Result",
72+
"Get List",
73+
"Get Amended Document",
74+
]
6675
if self.type in READ_PERMISSIONS:
6776
self.requires_write_permissions = 0
6877

@@ -186,6 +195,42 @@ def prepare_function_params(self):
186195
"required": ["document_ids"],
187196
"additionalProperties": False,
188197
}
198+
elif self.type == "Submit Document":
199+
params = {
200+
"type": "object",
201+
"properties": {
202+
"document_id": {
203+
"type": "string",
204+
"description": f"The ID of the {self.reference_doctype} to submit",
205+
}
206+
},
207+
"required": ["document_id"],
208+
"additionalProperties": False,
209+
}
210+
elif self.type == "Cancel Document":
211+
params = {
212+
"type": "object",
213+
"properties": {
214+
"document_id": {
215+
"type": "string",
216+
"description": f"The ID of the {self.reference_doctype} to cancel",
217+
}
218+
},
219+
"required": ["document_id"],
220+
"additionalProperties": False,
221+
}
222+
elif self.type == "Get Amended Document":
223+
params = {
224+
"type": "object",
225+
"properties": {
226+
"document_id": {
227+
"type": "string",
228+
"description": f"The ID of the {self.reference_doctype} to get the amended document for",
229+
}
230+
},
231+
"required": ["document_id"],
232+
"additionalProperties": False,
233+
}
189234
elif self.type == "Attach File to Document":
190235
params = {
191236
"type": "object",
@@ -360,6 +405,9 @@ def validate(self):
360405
"Update Multiple Documents",
361406
"Delete Document",
362407
"Delete Multiple Documents",
408+
"Submit Document",
409+
"Cancel Document",
410+
"Get Amended Document",
363411
]
364412
if self.type in DOCUMENT_REF_FUNCTIONS:
365413
if not self.reference_doctype:

0 commit comments

Comments
 (0)