Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 68 additions & 0 deletions flexus_client_kit/integrations/fi_memory.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
from flexus_client_kit import ckit_cloudtool


MEMORY_PROMPT = """
# Daily Memory Collection

You run once per day to extract reusable Q&A knowledge from recent customer conversations.

## Process

1. Determine `since_ts`:
- Call flexus_policy_document(op="list", args={"p": "/memory/"}) and find the newest `/memory/YYYYMMDD-daily` document.
- If found, read it with flexus_policy_document(op="cat") and use `faq.meta.processed_until_ts` as `since_ts`.
- If no previous doc or no `processed_until_ts`, fall back to last 24 hours.

2. Query recent inbound CRM activities:
erp_table_data(table_name="crm_activity", options={"filters": {"AND": ["activity_direction:=:INBOUND", "activity_occurred_ts:>:SINCE_TS"]}, "sort_by": ["activity_occurred_ts:ASC"], "limit": 100})
These are real customer conversations linked to threads.

3. Extract Q&A pairs from `activity_summary` fields — the summary already contains what the customer asked and how it was resolved.
- Skip greetings, test pings, chitchat, bot-only interactions
- Only store Q&A where the answer provides procedural steps, specific policies, limits, or non-obvious information. Skip promotions, discounts, stock availability, or other things that change too fast to memorize.
- Group by topic (billing, product, onboarding, technical, shipping, etc.)
- Anonymize: replace names, emails, phone numbers, platform IDs with placeholders
- Merge similar questions from different conversations into one

4. Set `processed_until_ts` to the maximum `activity_occurred_ts` you processed. If nothing processed, use current time.

5. Save results:
- If Q&A pairs extracted → write /memory/YYYYMMDD-daily with flexus_policy_document(op="create" or "overwrite").
- If no Q&A but previous doc exists → just update its timestamp:
flexus_policy_document(op="update_at_location", args={"p": "/memory/PREV-daily", "updates": [["faq.meta.processed_until_ts", NEW_TS]]})
- If no Q&A and no previous doc → create one with just meta to persist the timestamp.

## QA Document Format

```json
{
"faq": {
"meta": {
"author": "memory-expert",
"created": "YYYYMMDD",
"processed_from_ts": 0,
"processed_until_ts": 0
},
"section01-TOPIC": {
"title": "Topic Name",
"question01-SLUG": {"q": "Customer question in generic form", "a": "Correct answer based on actual resolution"}
}
}
}
```

Use kebab-case slugs. Number sections and questions sequentially.

## After Writing

Resolve with a brief summary: how many activities reviewed, how many Q&A pairs created.
"""

SCHED_MEMORY_DAILY = {
"sched_type": "SCHED_ANY",
"sched_when": "WEEKDAYS:MO:TU:WE:TH:FR:SA:SU/23:00",
"sched_first_question": "Run daily memory collection.",
"sched_fexp_name": "memory",
}

TOOLS_MEMORY = {"flexus_policy_document", "erp_table_meta", "erp_table_data"} | ckit_cloudtool.KANBAN_SAFE
11 changes: 11 additions & 0 deletions flexus_simple_bots/karen/karen_install.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from flexus_simple_bots import prompts_common
from flexus_simple_bots.karen import karen_bot
from flexus_simple_bots.karen import karen_prompts
from flexus_client_kit.integrations import fi_memory


TOOLS_DEFAULT = {
Expand Down Expand Up @@ -162,6 +163,15 @@
"log-crm-activity",
]),
)),
("memory", ckit_bot_install.FMarketplaceExpertInput(
fexp_system_prompt=fi_memory.MEMORY_PROMPT,
fexp_python_kernel="",
fexp_allow_tools=",".join(fi_memory.TOOLS_MEMORY),
fexp_nature="NATURE_AUTONOMOUS",
fexp_inactivity_timeout=300,
fexp_model_class="cheap",
fexp_description="Daily extraction of reusable Q&A knowledge from completed customer conversations into /memory/ policy documents.",
)),
("explore", ckit_bot_install.FMarketplaceExpertInput(
fexp_system_prompt=karen_prompts.EXPLORE_PROMPT,
fexp_python_kernel=KAREN_EXPLORE_KERNEL,
Expand Down Expand Up @@ -209,6 +219,7 @@ async def install(client: ckit_client.FlexusClient):
marketable_schedule=[
prompts_common.SCHED_TASK_SORT_10M | {"sched_when": "EVERY:1m", "sched_fexp_name": "messages_triage"},
prompts_common.SCHED_TODO_5M | {"sched_when": "EVERY:1m"},
fi_memory.SCHED_MEMORY_DAILY,
],
marketable_forms={},
marketable_auth_supported=["slack", "telegram", "discord_manual", "shopify", "resend"],
Expand Down
Loading