diff --git a/frontend/src/components/feature/settings/ai/functions/FunctionConstants.ts b/frontend/src/components/feature/settings/ai/functions/FunctionConstants.ts index 2e32737cf..c58afcc1b 100644 --- a/frontend/src/components/feature/settings/ai/functions/FunctionConstants.ts +++ b/frontend/src/components/feature/settings/ai/functions/FunctionConstants.ts @@ -11,12 +11,12 @@ export const FUNCTION_TYPES = [ requires_write_permissions: false, type: "Bulk Operations" }, - // { - // value: "Get List", - // description: "Fetch a list of documents from the system (using filters).", - // requires_write_permissions: false, - // type: "Standard" - // }, + { + value: "Get List", + description: "Fetch a list of documents from the system (using filters).", + requires_write_permissions: false, + type: "Standard" + }, { value: "Create Document", description: "Create any document in the system.", @@ -109,4 +109,4 @@ export interface ArrayVariableType extends BaseVariableType { type: 'array' items: StringVariableType | NumberVariableType, minItems?: number -} \ No newline at end of file +} diff --git a/raven/ai/functions.py b/raven/ai/functions.py index 01c1ed947..dae3f0251 100644 --- a/raven/ai/functions.py +++ b/raven/ai/functions.py @@ -137,3 +137,16 @@ def attach_file_to_document(doctype: str, document_id: str, file_path: str): newFile.insert() return {"document_id": document_id, "message": "File attached", "file_id": newFile.name} + +def get_list(doctype: str, filters: dict = None, fields: list = None, limit: int = 20): + """ + Get a list of documents from the database + """ + if filters is None: + filters = {} + + if fields is None: + fields = ["*"] + + # Use the frappe.get_list method to get the list of documents + return frappe.get_list(doctype, filters=filters, fields=fields, limit=limit) diff --git a/raven/ai/handler.py b/raven/ai/handler.py index 3dbb93c3c..64b4db8b3 100644 --- a/raven/ai/handler.py +++ b/raven/ai/handler.py @@ -16,6 +16,7 @@ get_documents, update_document, update_documents, + get_list ) from raven.ai.openai_client import get_open_ai_client @@ -195,6 +196,15 @@ def handle_requires_action(self, data, run_id): self.publish_event(f"Attaching file to {doctype} {document_id}...") function_output = attach_file_to_document(doctype, document_id, file_path) + if function.type == "Get List": + self.publish_event(f"Fetching list of {function.reference_doctype}...") + function_output = get_list( + function.reference_doctype, + filters=args.get("filters"), + fields=args.get("fields"), + limit=args.get("limit", 20) + ) + tool_outputs.append( {"tool_call_id": tool.id, "output": json.dumps(function_output, default=str)} ) diff --git a/raven/raven_ai/doctype/raven_ai_function/raven_ai_function.py b/raven/raven_ai/doctype/raven_ai_function/raven_ai_function.py index 53223f7da..d9340f12f 100644 --- a/raven/raven_ai/doctype/raven_ai_function/raven_ai_function.py +++ b/raven/raven_ai/doctype/raven_ai_function/raven_ai_function.py @@ -208,6 +208,28 @@ def prepare_function_params(self): } elif self.type == "Custom Function": params = json.loads(self.params) + elif self.type == "Get List": + params = { + "type": "object", + "properties": { + "filters": { + "type": "object", + "description": "Filters to apply when retrieving the list", + }, + "fields": { + "type": "array", + "items": {"type": "string"}, + "description": "Fields to retrieve for each document", + }, + "limit": { + "type": "integer", + "description": "Maximum number of documents to retrieve", + "default": 20, + }, + }, + "required": ["filters", "fields"], + "additionalProperties": False, + } else: params = self.build_params_json_from_table()