Skip to content
Open
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
143 changes: 143 additions & 0 deletions docs/research/crm-guide-notes.md

Large diffs are not rendered by default.

179 changes: 179 additions & 0 deletions docs/specs/agent-native-crm.md

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions products/crm/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
__pycache__/
*.sqlite
*.sqlite3
*.sqlite-wal
*.sqlite-shm
48 changes: 48 additions & 0 deletions products/crm/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# Local CRM

A small CRM an agent can run on one machine. It stores people, companies, deals, activities and custom objects in a SQLite file. The command line and the MCP server speak JSON. Nothing in this folder calls Salesforce, HubSpot or any other network service.

It is separate from the Rust workspace. The workspace only approves `serde`, `serde_json` and `rustix`, and a new crate needs approval. This slice uses the Python standard library.

The database can hold notes about real people. Keep the file outside the git checkout.

## Run

```sh
python3 -I products/crm/test_crm.py
python3 -I products/crm/crm.py --db /tmp/openwiz-crm.sqlite init
python3 -I products/crm/crm.py --db /tmp/openwiz-crm.sqlite record-create \
--object person --set name=Ada --set email=ada@example.com
python3 -I products/crm/crm.py --db /tmp/openwiz-crm.sqlite context --id rec_...
OPENWIZ_CRM_DB=/tmp/openwiz-crm.sqlite python3 -I products/crm/mcp.py
```

`--db` or `OPENWIZ_CRM_DB` chooses the file. `--actor` is stored on each change and defaults to `agent`. Every command prints one JSON object. `ok` is true and `result` holds the payload, or `ok` is false and `error.code` is one of `validation`, `unknown_object`, `unknown_field`, `not_found`, `conflict`, `object_mismatch` or `internal`.

## Commands

| Command | What it does |
| --- | --- |
| `init`, `schema` | Create the file and list objects and fields |
| `object-add`, `field-add` | Add a custom object or field |
| `record-create`, `record-get`, `record-update`, `record-delete`, `record-search` | Records. Search skips deleted rows. Get still returns them |
| `associate` | A labeled link from one record to another |
| `activity-log` | An activity linked to one or more records with the label `about` |
| `context` | The record, one hop of linked records, and recent changes |
| `ref-bind`, `ref-get` | Bind or find `provider` + object type + external id |

`record-update` and `record-delete` take `--if-revision`. Pass the `revision` from the record you read. A newer write is refused with `conflict`. Omit it only when a lost update is acceptable.

`record-search --query` is a case-insensitive match on text and select fields. `--where name=value` is exact, after the field type is applied. `--set` and `--where` are repeated `name=value` pairs.

Field types are `text`, `number`, `boolean`, `date` and `select`. A select field needs `--option` values that are themselves api names. Dates are `YYYY-MM-DD` or `YYYY-MM-DDTHH:MM:SSZ`.

## MCP

`mcp.py` reads newline-delimited JSON-RPC on stdin and writes it on stdout. It accepts protocol versions `2024-11-05`, `2025-03-26` and `2025-06-18`. The tools are `crm_schema`, `crm_object_add`, `crm_field_add`, `crm_record_create`, `crm_record_update`, `crm_record_search`, `crm_record_context`, `crm_associate`, `crm_activity_log` and `crm_ref_bind`. A tool error is a result with `isError` true and the same JSON shape as the command line.

## Standard objects

`person` (name, email, role), `company` (name, domain), `deal` (name, stage, amount) and `activity` (kind, summary, body, happened_at, source). Stage is `new`, `qualifying`, `proposal`, `won` or `lost`. Activity kind is `note`, `email`, `meeting`, `call`, `slack`, `github`, `x` or `other`. Custom objects use the same record API.

External ids include the provider object type. HubSpot record ids are unique inside one object, so contact `1001` and company `1001` are different bindings.
10 changes: 10 additions & 0 deletions products/crm/crm.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/usr/bin/env python3
import sys
from pathlib import Path

sys.path.insert(0, str(Path(__file__).resolve().parent))

from openwiz_crm.cli import main

if __name__ == "__main__":
raise SystemExit(main())
10 changes: 10 additions & 0 deletions products/crm/mcp.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/usr/bin/env python3
import sys
from pathlib import Path

sys.path.insert(0, str(Path(__file__).resolve().parent))

from openwiz_crm.mcp import main

if __name__ == "__main__":
raise SystemExit(main())
1 change: 1 addition & 0 deletions products/crm/openwiz_crm/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
__version__ = "0.1.0"
168 changes: 168 additions & 0 deletions products/crm/openwiz_crm/cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
import argparse
import json
import os
import sqlite3
import sys

from openwiz_crm.errors import CrmError
from openwiz_crm.store import Store


def emit(payload):
sys.stdout.write(json.dumps(payload, sort_keys=True) + "\n")


def fail(error):
emit(error.payload())
return 1


def pairs(values):
fields = {}
for item in values or []:
if "=" not in item:
raise CrmError("validation", "expected name=value")
name, raw = item.split("=", 1)
if not name or name in fields:
raise CrmError("validation", "each field may be set once")
fields[name] = raw
return fields


def build_parser():
parser = argparse.ArgumentParser(
prog="crm.py",
description="Local CRM for agents. Prints one JSON object to stdout.",
)
parser.add_argument("--db", default=os.environ.get("OPENWIZ_CRM_DB"))
parser.add_argument("--actor", default="agent")
commands = parser.add_subparsers(dest="command", required=True)

commands.add_parser("init", help="Create the database and standard objects")
commands.add_parser("schema", help="List objects and fields")

object_add = commands.add_parser("object-add", help="Add a custom object")
object_add.add_argument("--name", required=True)
object_add.add_argument("--label", required=True)

field_add = commands.add_parser("field-add", help="Add a custom field")
field_add.add_argument("--object", required=True)
field_add.add_argument("--name", required=True)
field_add.add_argument("--label")
field_add.add_argument("--type", required=True, dest="field_type")
field_add.add_argument("--required", action="store_true")
field_add.add_argument("--option", action="append", dest="options")

create = commands.add_parser("record-create", help="Create a record")
create.add_argument("--object", required=True)
create.add_argument("--set", action="append", dest="sets")

get = commands.add_parser("record-get", help="Get one record, including a deleted one")
get.add_argument("--id", required=True)

update = commands.add_parser("record-update", help="Change fields on a record")
update.add_argument("--object", required=True)
update.add_argument("--id", required=True)
update.add_argument("--set", action="append", dest="sets")
update.add_argument("--clear", action="append", dest="clear")
update.add_argument("--if-revision", type=int)

delete = commands.add_parser("record-delete", help="Soft-delete a record")
delete.add_argument("--object", required=True)
delete.add_argument("--id", required=True)
delete.add_argument("--if-revision", type=int)

search = commands.add_parser("record-search", help="Search live records")
search.add_argument("--object", required=True)
search.add_argument("--query")
search.add_argument("--where", action="append", dest="wheres")
search.add_argument("--limit", type=int, default=50)

associate = commands.add_parser("associate", help="Link two records")
associate.add_argument("--from", dest="from_id", required=True)
associate.add_argument("--to", required=True)
associate.add_argument("--label", required=True)

activity = commands.add_parser("activity-log", help="Record an activity and link it")
activity.add_argument("--kind", required=True)
activity.add_argument("--summary", required=True)
activity.add_argument("--body")
activity.add_argument("--happened-at")
activity.add_argument("--source", default="cli")
activity.add_argument("--link", action="append", dest="links", required=True)

context = commands.add_parser("context", help="Record, one hop of links, and recent changes")
context.add_argument("--id", required=True)

bind = commands.add_parser("ref-bind", help="Bind an external id to a record")
bind.add_argument("--provider", required=True)
bind.add_argument("--type", required=True)
bind.add_argument("--external-id", required=True)
bind.add_argument("--id", required=True)

find = commands.add_parser("ref-get", help="Find a record by external id")
find.add_argument("--provider", required=True)
find.add_argument("--type", required=True)
find.add_argument("--external-id", required=True)
return parser


def run(args):
if not args.db:
raise CrmError("validation", "pass --db or set OPENWIZ_CRM_DB")
store = Store.open(args.db)
try:
if args.command == "init":
result = store.schema()
elif args.command == "schema":
result = store.schema()
elif args.command == "object-add":
result = store.add_object(args.name, args.label)
elif args.command == "field-add":
result = store.add_field(
args.object, args.name, args.label, args.field_type,
args.required, args.options,
)
elif args.command == "record-create":
result = store.create_record(args.object, pairs(args.sets), args.actor)
elif args.command == "record-get":
result = store.get_record(args.id)
elif args.command == "record-update":
result = store.update_record(
args.object, args.id, pairs(args.sets), args.clear or [], args.actor,
args.if_revision,
)
elif args.command == "record-delete":
result = store.delete_record(args.object, args.id, args.actor, args.if_revision)
elif args.command == "record-search":
result = store.search(args.object, args.query, pairs(args.wheres), args.limit)
elif args.command == "associate":
result = store.associate(args.from_id, args.to, args.label, args.actor)
elif args.command == "activity-log":
result = store.log_activity(
args.kind, args.summary, args.links, args.actor,
args.body, args.happened_at, args.source,
)
elif args.command == "context":
result = store.context(args.id)
elif args.command == "ref-bind":
result = store.bind_ref(args.provider, args.type, args.external_id, args.id)
elif args.command == "ref-get":
result = store.find_ref(args.provider, args.type, args.external_id)
else:
raise CrmError("validation", "unknown command")
finally:
store.close()
return result


def main(argv=None):
parser = build_parser()
try:
args = parser.parse_args(argv)
emit({"ok": True, "result": run(args)})
return 0
except CrmError as error:
return fail(error)
except sqlite3.Error:
return fail(CrmError("internal", "database error"))
8 changes: 8 additions & 0 deletions products/crm/openwiz_crm/errors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class CrmError(Exception):
def __init__(self, code, message):
super().__init__(message)
self.code = code
self.message = message

def payload(self):
return {"ok": False, "error": {"code": self.code, "message": self.message}}
Loading
Loading