Skip to content

Commit 4cbcb92

Browse files
committed
rick instructions for bulk insert
1 parent a88c286 commit 4cbcb92

File tree

2 files changed

+93
-1
lines changed

2 files changed

+93
-1
lines changed

flexus_simple_bots/rick/rick_bot.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,14 @@
3636
fi_erp.ERP_TABLE_META_TOOL,
3737
fi_erp.ERP_TABLE_DATA_TOOL,
3838
fi_erp.ERP_TABLE_CRUD_TOOL,
39+
fi_erp.ERP_CSV_IMPORT_TOOL,
3940
fi_mongo_store.MONGO_STORE_TOOL,
4041
fi_crm_automations.CRM_AUTOMATION_TOOL,
4142
]
4243

4344

4445
async def rick_main_loop(fclient: ckit_client.FlexusClient, rcx: ckit_bot_exec.RobotContext) -> None:
45-
dbname = f"{rcx.persona.ws_id}__{rcx.persona.persona_id}"
46+
dbname = f"{rcx.persona.persona_id}_db"
4647
mongo_conn_str = await ckit_mongo.mongo_fetch_creds(fclient, rcx.persona.persona_id)
4748
mongo = AsyncMongoClient(mongo_conn_str)
4849
mydb = mongo[dbname]
@@ -91,6 +92,10 @@ async def toolcall_erp_data(toolcall: ckit_cloudtool.FCloudtoolCall, model_produ
9192
async def toolcall_erp_crud(toolcall: ckit_cloudtool.FCloudtoolCall, model_produced_args: Dict[str, Any]) -> str:
9293
return await erp_integration.handle_erp_crud(toolcall, model_produced_args)
9394

95+
@rcx.on_tool_call(fi_erp.ERP_CSV_IMPORT_TOOL.name)
96+
async def toolcall_erp_csv_import(toolcall: ckit_cloudtool.FCloudtoolCall, model_produced_args: Dict[str, Any]) -> str:
97+
return await erp_integration.handle_csv_import(toolcall, model_produced_args)
98+
9499
@rcx.on_tool_call(fi_mongo_store.MONGO_STORE_TOOL.name)
95100
async def toolcall_mongo_store(toolcall: ckit_cloudtool.FCloudtoolCall, model_produced_args: Dict[str, Any]) -> str:
96101
return await fi_mongo_store.handle_mongo_store(rcx.workdir, mongo_collection, toolcall, model_produced_args)

flexus_simple_bots/rick/rick_prompts.py

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,93 @@
3535
<button type="submit">Submit</button>
3636
</form>
3737
```
38+
39+
## Bulk Importing Records from CSV
40+
41+
When a user wants to import records (e.g., contacts) from a CSV, follow this process:
42+
43+
### Step 1: Get the CSV File
44+
45+
Ask the user to upload their CSV file. They can attach it to the chat, and you will access it via mongo_store.
46+
47+
### Step 2: Analyze CSV and Target Table
48+
49+
1. Read the CSV (headers + sample rows) from Mongo
50+
2. Call erp_table_meta() to retrieve the full schema of the target table (e.g., crm_contact)
51+
3. Identify standard fields and the JSON details field for custom data
52+
53+
### Step 3: Propose Field Mapping
54+
55+
Create an intelligent mapping from CSV → table fields:
56+
57+
1. Match columns by name similarity
58+
2. Propose transformations where needed (e.g., split full name, normalize phone/email, parse dates)
59+
3. Map unmatched CSV columns into the appropriate *_details JSON field
60+
4. Suggest an upsert key for deduplication (e.g., contact_email) if possible
61+
62+
Present the mapping to the user in a clear format:
63+
```
64+
CSV Column → Target Field (Transformation)
65+
-----------------------------------------
66+
Email → contact_email (lowercase, trim)
67+
Full Name → contact_first_name + contact_last_name (split on first space)
68+
Phone → contact_phone (format: remove non-digits)
69+
Company → contact_details.company (custom field)
70+
Source → contact_details.source (custom field)
71+
72+
Upsert key: contact_email (will update existing contacts with same email)
73+
```
74+
75+
### Step 4: Validate and Adjust
76+
77+
Ask the user to confirm or modify, field mappings, transformations, upsert behavior, validation rules
78+
79+
### Step 5: Generate Python Script to Normalize the CSV
80+
Use python_execute() only to transform the uploaded file into a clean CSV whose columns exactly match the ERP table. Read from the Mongo attachment and write a new CSV:
81+
82+
```python
83+
import pandas as pd
84+
85+
SOURCE_FILE = "attachments/solar_root/leads_rows.csv"
86+
TARGET_TABLE = "crm_contact"
87+
OUTPUT_FILE = f"{{TARGET_TABLE}}_import.csv"
88+
89+
df = pd.read_csv(SOURCE_FILE)
90+
records = []
91+
for _, row in df.iterrows():
92+
full_name = str(row.get("Full Name", "")).strip()
93+
parts = full_name.split(" ", 1)
94+
first_name = parts[0] if parts else ""
95+
last_name = parts[1] if len(parts) > 1 else ""
96+
record = {{
97+
"contact_first_name": first_name,
98+
"contact_last_name": last_name,
99+
"contact_email": str(row.get("Email", "")).strip().lower(),
100+
"contact_phone": str(row.get("Phone", "")).strip(),
101+
"contact_details": {{
102+
"company": str(row.get("Company", "")).strip(),
103+
"source": "csv_import"
104+
}}
105+
}}
106+
records.append(record)
107+
108+
normalized = pd.DataFrame(records)
109+
normalized.to_csv(OUTPUT_FILE, index=False)
110+
print(f"Saved {{OUTPUT_FILE}} with {{len(normalized)}} rows")
111+
```
112+
113+
python_execute automatically uploads generated files back to Mongo under their filenames (e.g., `crm_contact_import.csv`), so you can reference them with mongo_store or the new import tool.
114+
115+
### Step 6: Review the Normalized File
116+
1. Use `mongo_store(op="cat", args={{"path": "crm_contact_import.csv"}})` to show the first rows
117+
2. Confirm every column matches the ERP schema (no extras, correct casing) and the upsert key looks good
118+
3. Share stats (row count, notable transforms) with the user
119+
120+
### Step 7: Import with `erp_csv_import`
121+
122+
Use erp_csv_import() to import the cleaned CSV.
123+
124+
After import, offer to create follow-up tasks or automations for the new contacts.
38125
"""
39126

40127
rick_prompt_default = f"""

0 commit comments

Comments
 (0)