@businesskit/agent 1.0.1 → 1.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.claude/commands/crm.md +552 -43
- package/ABOUT-ME.md +96 -0
- package/CLAUDE.md +15 -10
- package/HEARTBEAT.md +81 -0
- package/README.md +101 -157
- package/SOUL.md +117 -0
- package/bin/businesskit.js +17 -11
- package/brand-voice.md +127 -0
- package/ingest.md +110 -0
- package/package.json +1 -1
- package/working-style.md +87 -0
package/.claude/commands/crm.md
CHANGED
|
@@ -1,82 +1,591 @@
|
|
|
1
1
|
# /crm — Agent-First CRM
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
Reads/writes
|
|
3
|
+
Full contact management, deal pipeline, outreach, proposals, invoices, and analytics.
|
|
4
|
+
Reads/writes directly to Turso UserDB. DB triggers keep analytics accurate automatically.
|
|
5
|
+
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
## Architecture
|
|
9
|
+
|
|
10
|
+
```
|
|
11
|
+
You/Agent writes → Turso DB → Triggers fire → crm_analytics always accurate
|
|
12
|
+
→ No app server needed
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
**13 DB triggers** handle all scalar analytics in real-time:
|
|
16
|
+
|
|
17
|
+
- Contact insert/convert/archive → funnel counts
|
|
18
|
+
- Activity insert/approve/reject → agent metrics + approval queue
|
|
19
|
+
- Deal insert/won/lost/update → pipeline + forecast
|
|
20
|
+
|
|
21
|
+
**JSON breakdowns** (7d/30d/12m/lifetime/utm) aggregate on analytics page visit.
|
|
22
|
+
|
|
23
|
+
---
|
|
24
|
+
|
|
25
|
+
## Tables
|
|
26
|
+
|
|
27
|
+
| Table | Purpose |
|
|
28
|
+
|---|---|
|
|
29
|
+
| `crm_contacts` | Leads, prospects, customers + full agent state |
|
|
30
|
+
| `crm_deals` | Pipeline opportunities |
|
|
31
|
+
| `crm_activities` | Append-only timeline (DMs, emails, calls, purchases) |
|
|
32
|
+
| `crm_tasks` | Follow-up to-dos |
|
|
33
|
+
| `crm_notes` | Rich-text notes per contact |
|
|
34
|
+
| `crm_groups` | Named groups (Investors, Clients, VIP etc) |
|
|
35
|
+
| `crm_templates` | Outreach templates with reply rate tracking |
|
|
36
|
+
| `crm_proposals` | Client proposals with e-sign + payment |
|
|
37
|
+
| `crm_invoices` | Invoices with line items |
|
|
38
|
+
| `crm_analytics` | 1 row per profile, always current |
|
|
39
|
+
|
|
40
|
+
---
|
|
5
41
|
|
|
6
42
|
## Session start
|
|
7
|
-
|
|
8
|
-
|
|
43
|
+
|
|
44
|
+
```
|
|
45
|
+
/crm
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
Always run this first:
|
|
49
|
+
|
|
50
|
+
```ts
|
|
51
|
+
// 1. Pipeline summary
|
|
52
|
+
SELECT * FROM crm_analytics LIMIT 1
|
|
53
|
+
// → total_contacts, open_deals, total_pending_approvals, forecast_this_month_cents
|
|
54
|
+
|
|
55
|
+
// 2. Pending approvals (show these first — agent is waiting on you)
|
|
56
|
+
SELECT a.*, c.first_name, c.last_name, c.platform
|
|
57
|
+
FROM crm_activities a
|
|
58
|
+
JOIN crm_contacts c ON c.id = a.contact_id
|
|
59
|
+
WHERE a.approval_status = 'pending_approval'
|
|
60
|
+
ORDER BY a.approval_requested_at DESC
|
|
61
|
+
LIMIT 10
|
|
62
|
+
|
|
63
|
+
// 3. Follow-ups due today
|
|
64
|
+
SELECT t.*, c.first_name, c.last_name
|
|
65
|
+
FROM crm_tasks t
|
|
66
|
+
JOIN crm_contacts c ON c.id = t.contact_id
|
|
67
|
+
WHERE t.status = 'open' AND t.due_at <= strftime('%s','now')
|
|
68
|
+
ORDER BY t.due_at ASC
|
|
69
|
+
LIMIT 10
|
|
70
|
+
|
|
71
|
+
// 4. Hot leads ready for outreach
|
|
72
|
+
SELECT id, first_name, last_name, company, platform, lead_score,
|
|
73
|
+
pain_point, suggested_dm, outreach_status, agent_status
|
|
74
|
+
FROM crm_contacts
|
|
75
|
+
WHERE agent_status = 'outreach_ready'
|
|
76
|
+
AND outreach_status = 'new'
|
|
77
|
+
AND archived = 0
|
|
78
|
+
ORDER BY lead_score DESC
|
|
79
|
+
LIMIT 10
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
---
|
|
9
83
|
|
|
10
84
|
## Contacts
|
|
85
|
+
|
|
86
|
+
### Add a contact (manual)
|
|
87
|
+
|
|
88
|
+
```ts
|
|
89
|
+
INSERT INTO crm_contacts (
|
|
90
|
+
id, profile_id, first_name, last_name, email, company,
|
|
91
|
+
platform, status, source, contact_type
|
|
92
|
+
) VALUES (?, ?, ?, ?, ?, ?, ?, 'lead', 'manual', 'professional')
|
|
93
|
+
// trigger fires → crm_analytics.total_contacts + 1, total_leads + 1
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
### Research a lead (agent)
|
|
97
|
+
|
|
98
|
+
```ts
|
|
99
|
+
// 1. Read existing context before acting
|
|
100
|
+
SELECT * FROM crm_contacts WHERE id = ?
|
|
101
|
+
|
|
102
|
+
// 2. Check agent_paused
|
|
103
|
+
// if agent_paused = 1 AND (agent_paused_until IS NULL OR agent_paused_until > now()) → SKIP
|
|
104
|
+
|
|
105
|
+
// 3. Check agent_instructions for per-contact overrides
|
|
106
|
+
|
|
107
|
+
// 4. Update with research findings
|
|
108
|
+
UPDATE crm_contacts SET
|
|
109
|
+
bio = ?,
|
|
110
|
+
recent_post = ?,
|
|
111
|
+
recent_post_url = ?,
|
|
112
|
+
pain_point = ?,
|
|
113
|
+
audience_size = ?,
|
|
114
|
+
company_size = ?,
|
|
115
|
+
industry = ?,
|
|
116
|
+
lead_score = ?,
|
|
117
|
+
lead_score_reason = ?,
|
|
118
|
+
icp_match = ?, -- 'strong'|'moderate'|'weak'|'unknown'
|
|
119
|
+
urgency = ?,
|
|
120
|
+
buying_intent = ?,
|
|
121
|
+
social_links = ?, -- JSON [{platform, url, username, followers}]
|
|
122
|
+
agent_status = 'enriched',
|
|
123
|
+
agent_context = ?, -- JSON {summary, tone, preferred_channel, do_not_mention[]}
|
|
124
|
+
agent_last_run_at = strftime('%s','now'),
|
|
125
|
+
enriched_at = strftime('%s','now'),
|
|
126
|
+
enrichment_source = 'agent',
|
|
127
|
+
updated_at = strftime('%s','now')
|
|
128
|
+
WHERE id = ?
|
|
11
129
|
```
|
|
12
|
-
"Add a lead: John Smith, LinkedIn @johnsmith, founder at Acme"
|
|
13
|
-
→ crmAgent.createContact({ first_name, last_name, platform, platform_username, company })
|
|
14
130
|
|
|
131
|
+
### List contacts
|
|
132
|
+
|
|
133
|
+
```
|
|
15
134
|
"Show my top leads"
|
|
16
|
-
→
|
|
135
|
+
→ SELECT ... WHERE agent_status = 'enriched' ORDER BY lead_score DESC LIMIT 20
|
|
17
136
|
|
|
18
137
|
"Show leads ready for outreach"
|
|
19
|
-
→
|
|
138
|
+
→ SELECT ... WHERE agent_status = 'outreach_ready' AND outreach_status = 'new'
|
|
20
139
|
|
|
21
140
|
"Show who replied"
|
|
22
|
-
→
|
|
141
|
+
→ SELECT ... WHERE outreach_status = 'replied'
|
|
142
|
+
|
|
143
|
+
"Show prospects in Investors group"
|
|
144
|
+
→ SELECT ... WHERE groups LIKE '%grp_005%'
|
|
145
|
+
|
|
146
|
+
"Show contacts from Twitter"
|
|
147
|
+
→ SELECT ... WHERE platform = 'twitter'
|
|
23
148
|
```
|
|
24
149
|
|
|
25
|
-
|
|
150
|
+
### Score a lead
|
|
151
|
+
|
|
152
|
+
```ts
|
|
153
|
+
UPDATE crm_contacts SET
|
|
154
|
+
lead_score = ?, -- 0-100
|
|
155
|
+
lead_score_reason = ?,
|
|
156
|
+
icp_match = ?,
|
|
157
|
+
urgency = ?,
|
|
158
|
+
buying_intent = ?,
|
|
159
|
+
agent_status = 'outreach_ready',
|
|
160
|
+
updated_at = strftime('%s','now')
|
|
161
|
+
WHERE id = ?
|
|
26
162
|
```
|
|
27
|
-
"Score this lead"
|
|
28
|
-
→ crmAgent.scoreContact(id, score, reason, icpMatch)
|
|
29
163
|
|
|
30
|
-
|
|
31
|
-
|
|
164
|
+
### Update contact status
|
|
165
|
+
|
|
166
|
+
```ts
|
|
167
|
+
UPDATE crm_contacts SET
|
|
168
|
+
status = 'customer', -- trigger fires → total_customers+1, total_leads-1
|
|
169
|
+
updated_at = strftime('%s','now')
|
|
170
|
+
WHERE id = ?
|
|
32
171
|
```
|
|
33
172
|
|
|
173
|
+
---
|
|
174
|
+
|
|
34
175
|
## Outreach
|
|
176
|
+
|
|
177
|
+
### Draft a DM (agent, needs approval)
|
|
178
|
+
|
|
179
|
+
```ts
|
|
180
|
+
// 1. Pull full context first
|
|
181
|
+
SELECT c.*,
|
|
182
|
+
(SELECT body FROM crm_activities WHERE contact_id = c.id ORDER BY occurred_at DESC LIMIT 1) as last_message,
|
|
183
|
+
(SELECT title FROM crm_tasks WHERE contact_id = c.id AND status = 'open' ORDER BY due_at ASC LIMIT 1) as next_task
|
|
184
|
+
FROM crm_contacts c WHERE c.id = ?
|
|
185
|
+
|
|
186
|
+
// 2. Read agent_context + agent_instructions
|
|
187
|
+
// 3. Pick best template
|
|
188
|
+
SELECT * FROM crm_templates
|
|
189
|
+
WHERE type = 'dm'
|
|
190
|
+
AND (platform = ? OR platform IS NULL)
|
|
191
|
+
ORDER BY reply_rate_pct DESC LIMIT 3
|
|
192
|
+
|
|
193
|
+
// 4. Generate DM filling {{first_name}} {{company}} {{pain_point}} variables
|
|
194
|
+
|
|
195
|
+
// 5. Save draft + log activity
|
|
196
|
+
UPDATE crm_contacts SET
|
|
197
|
+
suggested_dm = ?,
|
|
198
|
+
outreach_status = 'dm_drafted',
|
|
199
|
+
updated_at = strftime('%s','now')
|
|
200
|
+
WHERE id = ?
|
|
201
|
+
|
|
202
|
+
INSERT INTO crm_activities (
|
|
203
|
+
id, profile_id, contact_id,
|
|
204
|
+
type, direction, sender,
|
|
205
|
+
body, metadata, searchable_context,
|
|
206
|
+
approval_status, approval_requested_at
|
|
207
|
+
) VALUES (
|
|
208
|
+
?, ?, ?,
|
|
209
|
+
'dm', 'outbound', 'agent',
|
|
210
|
+
?, '{"platform":"twitter"}', ?,
|
|
211
|
+
'pending_approval', strftime('%s','now')
|
|
212
|
+
)
|
|
213
|
+
// trigger fires → total_pending_approvals + 1
|
|
214
|
+
```
|
|
215
|
+
|
|
216
|
+
### Approve a DM (you)
|
|
217
|
+
|
|
218
|
+
```ts
|
|
219
|
+
UPDATE crm_activities SET
|
|
220
|
+
approval_status = 'approved',
|
|
221
|
+
approved_at = strftime('%s','now')
|
|
222
|
+
WHERE id = ?
|
|
223
|
+
// trigger fires → total_pending_approvals - 1, total_approved + 1
|
|
224
|
+
|
|
225
|
+
UPDATE crm_contacts SET
|
|
226
|
+
dm_sent = 1,
|
|
227
|
+
dm_sent_at = strftime('%s','now'),
|
|
228
|
+
outreach_status = 'dm_sent',
|
|
229
|
+
last_contacted_at = strftime('%s','now'),
|
|
230
|
+
last_activity_at = strftime('%s','now'),
|
|
231
|
+
updated_at = strftime('%s','now')
|
|
232
|
+
WHERE id = ?
|
|
233
|
+
```
|
|
234
|
+
|
|
235
|
+
### Reject a DM (you)
|
|
236
|
+
|
|
237
|
+
```ts
|
|
238
|
+
UPDATE crm_activities SET
|
|
239
|
+
approval_status = 'rejected'
|
|
240
|
+
WHERE id = ?
|
|
241
|
+
// trigger fires → total_pending_approvals - 1, total_rejected + 1
|
|
242
|
+
```
|
|
243
|
+
|
|
244
|
+
### Auto-approve mode (contact level)
|
|
245
|
+
|
|
246
|
+
```ts
|
|
247
|
+
// Enable for a contact (agent sends without asking)
|
|
248
|
+
UPDATE crm_contacts SET auto_approve = 1 WHERE id = ?
|
|
249
|
+
|
|
250
|
+
// When auto_approve = 1, use approval_status = 'auto_sent' instead of 'pending_approval'
|
|
251
|
+
// trigger fires → total_agent_actions + 1, but NOT total_pending_approvals
|
|
35
252
|
```
|
|
36
|
-
"Draft a DM for this lead"
|
|
37
|
-
→ crmAgent.draftDM(id, message)
|
|
38
|
-
→ stores in suggested_dm
|
|
39
|
-
→ creates crm_activity with approval_status='pending' (or 'auto_sent' if auto_approve=1)
|
|
40
253
|
|
|
41
|
-
|
|
42
|
-
|
|
254
|
+
### Log a reply (contact replied)
|
|
255
|
+
|
|
256
|
+
```ts
|
|
257
|
+
INSERT INTO crm_activities (
|
|
258
|
+
id, profile_id, contact_id,
|
|
259
|
+
type, direction, sender,
|
|
260
|
+
body, metadata, searchable_context,
|
|
261
|
+
approval_status
|
|
262
|
+
) VALUES (
|
|
263
|
+
?, ?, ?,
|
|
264
|
+
'dm', 'inbound', 'contact',
|
|
265
|
+
?, '{"platform":"twitter"}', ?,
|
|
266
|
+
'auto_sent'
|
|
267
|
+
)
|
|
268
|
+
// trigger fires → total_replies + 1
|
|
269
|
+
|
|
270
|
+
UPDATE crm_contacts SET
|
|
271
|
+
outreach_status = 'replied',
|
|
272
|
+
reply_sentiment = ?, -- 'positive'|'neutral'|'negative'
|
|
273
|
+
last_reply_at = strftime('%s','now'),
|
|
274
|
+
last_reply_channel = 'dm',
|
|
275
|
+
last_activity_at = strftime('%s','now'),
|
|
276
|
+
conversation_thread = json_patch(conversation_thread, ?),
|
|
277
|
+
updated_at = strftime('%s','now')
|
|
278
|
+
WHERE id = ?
|
|
279
|
+
```
|
|
43
280
|
|
|
44
|
-
|
|
45
|
-
→ crmAgent.markDMSent(id)
|
|
281
|
+
### Pause agent for a contact
|
|
46
282
|
|
|
47
|
-
|
|
48
|
-
|
|
283
|
+
```ts
|
|
284
|
+
UPDATE crm_contacts SET
|
|
285
|
+
agent_paused = 1,
|
|
286
|
+
agent_paused_until = strftime('%s','now','+7 days'), -- NULL = forever
|
|
287
|
+
updated_at = strftime('%s','now')
|
|
288
|
+
WHERE id = ?
|
|
49
289
|
```
|
|
50
290
|
|
|
291
|
+
---
|
|
292
|
+
|
|
51
293
|
## Deals
|
|
294
|
+
|
|
295
|
+
### Create a deal
|
|
296
|
+
|
|
297
|
+
```ts
|
|
298
|
+
INSERT INTO crm_deals (id, profile_id, contact_id, title, value_cents, stage, probability)
|
|
299
|
+
VALUES (?, ?, ?, ?, ?, 'proposal', 70)
|
|
300
|
+
// trigger fires → total_deals+1, open_deals+1, total_deal_value_cents + value
|
|
301
|
+
```
|
|
302
|
+
|
|
303
|
+
### Move deal stage
|
|
304
|
+
|
|
305
|
+
```ts
|
|
306
|
+
UPDATE crm_deals SET
|
|
307
|
+
stage = 'won', -- trigger fires → won_deals+1, open_deals-1
|
|
308
|
+
closed_at = strftime('%s','now'),
|
|
309
|
+
updated_at = strftime('%s','now')
|
|
310
|
+
WHERE id = ?
|
|
311
|
+
```
|
|
312
|
+
|
|
313
|
+
### Update deal for forecast
|
|
314
|
+
|
|
315
|
+
```ts
|
|
316
|
+
UPDATE crm_deals SET
|
|
317
|
+
probability = 85, -- trigger fires → forecast recalculated
|
|
318
|
+
expected_close_at = ?,
|
|
319
|
+
value_cents = ?,
|
|
320
|
+
updated_at = strftime('%s','now')
|
|
321
|
+
WHERE id = ?
|
|
52
322
|
```
|
|
53
|
-
"Create a deal: $5,000 consulting project"
|
|
54
|
-
→ crmAgent.createDeal(contactId, { title, value_cents: 500000 })
|
|
55
323
|
|
|
56
|
-
|
|
57
|
-
→ crmAgent.updateDealStage(dealId, 'proposal')
|
|
324
|
+
### Show pipeline
|
|
58
325
|
|
|
59
|
-
|
|
60
|
-
|
|
326
|
+
```ts
|
|
327
|
+
SELECT d.*, c.first_name, c.last_name, c.company, c.avatar_url
|
|
328
|
+
FROM crm_deals d
|
|
329
|
+
JOIN crm_contacts c ON c.id = d.contact_id
|
|
330
|
+
WHERE d.profile_id = ?
|
|
331
|
+
AND d.stage NOT IN ('won','lost')
|
|
332
|
+
ORDER BY d.value_cents DESC
|
|
61
333
|
```
|
|
62
334
|
|
|
335
|
+
---
|
|
336
|
+
|
|
63
337
|
## Tasks
|
|
338
|
+
|
|
339
|
+
### Create a follow-up task
|
|
340
|
+
|
|
341
|
+
```ts
|
|
342
|
+
INSERT INTO crm_tasks (id, profile_id, contact_id, deal_id, title, due_at, priority)
|
|
343
|
+
VALUES (?, ?, ?, ?, ?, strftime('%s','now','+2 days'), 'high')
|
|
64
344
|
```
|
|
65
|
-
"Remind me to follow up with John next Monday"
|
|
66
|
-
→ crmAgent.createTask({ title, contact_id, due_at: unixTimestamp, priority: 'high' })
|
|
67
345
|
|
|
68
|
-
|
|
69
|
-
|
|
346
|
+
### Complete a task
|
|
347
|
+
|
|
348
|
+
```ts
|
|
349
|
+
UPDATE crm_tasks SET
|
|
350
|
+
status = 'done',
|
|
351
|
+
completed_at = strftime('%s','now'),
|
|
352
|
+
updated_at = strftime('%s','now')
|
|
353
|
+
WHERE id = ?
|
|
354
|
+
```
|
|
355
|
+
|
|
356
|
+
### Tasks due today
|
|
357
|
+
|
|
358
|
+
```ts
|
|
359
|
+
SELECT t.*, c.first_name, c.last_name
|
|
360
|
+
FROM crm_tasks t
|
|
361
|
+
JOIN crm_contacts c ON c.id = t.contact_id
|
|
362
|
+
WHERE t.profile_id = ?
|
|
363
|
+
AND t.status = 'open'
|
|
364
|
+
AND t.due_at <= strftime('%s','now')
|
|
365
|
+
ORDER BY t.priority DESC, t.due_at ASC
|
|
366
|
+
```
|
|
367
|
+
|
|
368
|
+
---
|
|
369
|
+
|
|
370
|
+
## Proposals
|
|
371
|
+
|
|
372
|
+
### Create a proposal
|
|
373
|
+
|
|
374
|
+
```ts
|
|
375
|
+
// 1. Get next proposal number
|
|
376
|
+
UPDATE settings SET proposal_sequence = proposal_sequence + 1;
|
|
377
|
+
SELECT proposal_sequence FROM settings LIMIT 1;
|
|
378
|
+
|
|
379
|
+
INSERT INTO crm_proposals (
|
|
380
|
+
id, profile_id, contact_id, deal_id,
|
|
381
|
+
title, body, excerpt, public_slug,
|
|
382
|
+
status, client_name, client_email, client_company,
|
|
383
|
+
payment_required, payment_amount_cents, payment_currency,
|
|
384
|
+
payment_processor, cta_label, cta_url,
|
|
385
|
+
ai_generated, next_reminder_at
|
|
386
|
+
) VALUES (
|
|
387
|
+
?, ?, ?, ?,
|
|
388
|
+
?, ?, ?, ?, -- slug = crypto.randomUUID().slice(0,8)
|
|
389
|
+
'draft', ?, ?, ?,
|
|
390
|
+
1, ?, 'usd',
|
|
391
|
+
'stripe', 'Schedule Kickoff Call', ?,
|
|
392
|
+
1, strftime('%s','now','+3 days')
|
|
393
|
+
)
|
|
394
|
+
```
|
|
395
|
+
|
|
396
|
+
### Send a proposal
|
|
397
|
+
|
|
398
|
+
```ts
|
|
399
|
+
UPDATE crm_proposals SET
|
|
400
|
+
status = 'sent',
|
|
401
|
+
sent_at = strftime('%s','now'),
|
|
402
|
+
payment_url = ?, -- Stripe/Paddle checkout URL
|
|
403
|
+
payment_url_expires_at = strftime('%s','now','+24 hours'),
|
|
404
|
+
updated_at = strftime('%s','now')
|
|
405
|
+
WHERE id = ?
|
|
406
|
+
|
|
407
|
+
// Log activity
|
|
408
|
+
INSERT INTO crm_activities (id, profile_id, contact_id, type, direction, sender, subject, body, metadata, approval_status)
|
|
409
|
+
VALUES (?, ?, ?, 'email', 'outbound', 'agent', 'Proposal sent', ?, '{"proposal_id":"?"}', 'auto_sent')
|
|
410
|
+
```
|
|
411
|
+
|
|
412
|
+
### Show proposals
|
|
413
|
+
|
|
414
|
+
```ts
|
|
415
|
+
"Show all sent proposals"
|
|
416
|
+
→ SELECT p.*, c.first_name, c.last_name FROM crm_proposals p
|
|
417
|
+
JOIN crm_contacts c ON c.id = p.contact_id
|
|
418
|
+
WHERE p.profile_id = ? AND p.status = 'sent'
|
|
419
|
+
ORDER BY p.sent_at DESC
|
|
420
|
+
|
|
421
|
+
"Show proposals awaiting signature"
|
|
422
|
+
→ WHERE p.status IN ('sent','viewed')
|
|
423
|
+
|
|
424
|
+
"Show paid proposals"
|
|
425
|
+
→ WHERE p.status = 'paid'
|
|
426
|
+
```
|
|
427
|
+
|
|
428
|
+
---
|
|
429
|
+
|
|
430
|
+
## Invoices
|
|
431
|
+
|
|
432
|
+
### Create an invoice
|
|
433
|
+
|
|
434
|
+
```ts
|
|
435
|
+
// 1. Get next invoice number
|
|
436
|
+
UPDATE settings SET invoice_sequence = invoice_sequence + 1;
|
|
437
|
+
SELECT invoice_sequence FROM settings LIMIT 1;
|
|
438
|
+
// format as INV-0001 (padded to 4 digits)
|
|
439
|
+
|
|
440
|
+
INSERT INTO crm_invoices (
|
|
441
|
+
id, profile_id, contact_id, deal_id, proposal_id,
|
|
442
|
+
invoice_number, public_slug, status,
|
|
443
|
+
client_name, client_email, client_company,
|
|
444
|
+
line_items, subtotal_cents, total_cents, currency,
|
|
445
|
+
issued_at, due_at, next_reminder_at, memo
|
|
446
|
+
) VALUES (
|
|
447
|
+
?, ?, ?, ?, ?,
|
|
448
|
+
?, ?, 'draft',
|
|
449
|
+
?, ?, ?,
|
|
450
|
+
?, ?, ?, 'usd',
|
|
451
|
+
strftime('%s','now'), strftime('%s','now','+30 days'),
|
|
452
|
+
strftime('%s','now','+3 days'), ?
|
|
453
|
+
)
|
|
454
|
+
```
|
|
455
|
+
|
|
456
|
+
---
|
|
457
|
+
|
|
458
|
+
## Groups
|
|
459
|
+
|
|
460
|
+
### Create a group
|
|
70
461
|
|
|
71
|
-
|
|
72
|
-
|
|
462
|
+
```ts
|
|
463
|
+
INSERT INTO crm_groups (id, profile_id, name, icon, color)
|
|
464
|
+
VALUES (?, ?, 'Investors', '🤑', '#22c55e')
|
|
73
465
|
```
|
|
74
466
|
|
|
75
|
-
|
|
76
|
-
Activities with `approval_status='pending'` need user review before sending.
|
|
77
|
-
Show pending items at session start: `pipelineSummary().pendingApprovals`
|
|
78
|
-
After user confirms → `markDMSent()` or `markEmailSent()`
|
|
467
|
+
### Add contact to group
|
|
79
468
|
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
469
|
+
```ts
|
|
470
|
+
// Read existing groups
|
|
471
|
+
SELECT groups FROM crm_contacts WHERE id = ?
|
|
472
|
+
// Append group ID to JSON array
|
|
473
|
+
UPDATE crm_contacts SET
|
|
474
|
+
groups = json_insert(groups, '$[#]', 'grp_005'),
|
|
475
|
+
updated_at = strftime('%s','now')
|
|
476
|
+
WHERE id = ?
|
|
477
|
+
|
|
478
|
+
// Increment group counter
|
|
479
|
+
UPDATE crm_groups SET contact_count = contact_count + 1 WHERE id = 'grp_005'
|
|
480
|
+
```
|
|
481
|
+
|
|
482
|
+
### List contacts in group
|
|
483
|
+
|
|
484
|
+
```ts
|
|
485
|
+
SELECT * FROM crm_contacts
|
|
486
|
+
WHERE profile_id = ?
|
|
487
|
+
AND groups LIKE '%grp_005%'
|
|
488
|
+
AND archived = 0
|
|
489
|
+
ORDER BY lead_score DESC
|
|
490
|
+
```
|
|
491
|
+
|
|
492
|
+
---
|
|
493
|
+
|
|
494
|
+
## Analytics
|
|
495
|
+
|
|
496
|
+
### Read analytics (instant — always current)
|
|
497
|
+
|
|
498
|
+
```ts
|
|
499
|
+
SELECT * FROM crm_analytics WHERE profile_id = ? LIMIT 1
|
|
500
|
+
```
|
|
501
|
+
|
|
502
|
+
Returns:
|
|
503
|
+
|
|
504
|
+
- `total_contacts`, `total_leads`, `total_customers` — contact funnel
|
|
505
|
+
- `open_deals`, `won_deals`, `total_deal_value_cents` — deal pipeline
|
|
506
|
+
- `forecast_this_month_cents`, `forecast_next_month_cents` — weighted forecast
|
|
507
|
+
- `total_agent_actions`, `total_pending_approvals` — agent metrics
|
|
508
|
+
- `total_replies`, `reply_rate_pct` — outreach performance
|
|
509
|
+
|
|
510
|
+
### Drift recovery (if analytics look wrong)
|
|
511
|
+
|
|
512
|
+
```ts
|
|
513
|
+
// Run CRM_ANALYTICS_RESYNC_SQL from crm.ts
|
|
514
|
+
// Or POST /api/crm/resync
|
|
515
|
+
```
|
|
516
|
+
|
|
517
|
+
---
|
|
518
|
+
|
|
519
|
+
## Context API (Sondex equivalent)
|
|
520
|
+
|
|
521
|
+
Before every agent action on a contact, pull full context in one query:
|
|
522
|
+
|
|
523
|
+
```ts
|
|
524
|
+
const [contact, activities, tasks, deals] = await Promise.all([
|
|
525
|
+
db.execute({ sql: `SELECT * FROM crm_contacts WHERE id = ?`, args: [contactId] }),
|
|
526
|
+
db.execute({
|
|
527
|
+
sql: `SELECT type, sender, direction, body, subject, occurred_at, searchable_context
|
|
528
|
+
FROM crm_activities WHERE contact_id = ?
|
|
529
|
+
ORDER BY occurred_at DESC LIMIT 20`,
|
|
530
|
+
args: [contactId]
|
|
531
|
+
}),
|
|
532
|
+
db.execute({
|
|
533
|
+
sql: `SELECT title, due_at, priority FROM crm_tasks
|
|
534
|
+
WHERE contact_id = ? AND status = 'open'
|
|
535
|
+
ORDER BY due_at ASC LIMIT 5`,
|
|
536
|
+
args: [contactId]
|
|
537
|
+
}),
|
|
538
|
+
db.execute({
|
|
539
|
+
sql: `SELECT title, stage, value_cents, probability FROM crm_deals
|
|
540
|
+
WHERE contact_id = ? AND stage NOT IN ('won','lost')`,
|
|
541
|
+
args: [contactId]
|
|
542
|
+
})
|
|
543
|
+
])
|
|
544
|
+
|
|
545
|
+
// Build context for LLM
|
|
546
|
+
const context = {
|
|
547
|
+
contact: contact.rows[0],
|
|
548
|
+
recent_activities: activities.rows,
|
|
549
|
+
open_tasks: tasks.rows,
|
|
550
|
+
open_deals: deals.rows,
|
|
551
|
+
agent_context: JSON.parse(contact.rows[0].agent_context || '{}'),
|
|
552
|
+
agent_instructions: contact.rows[0].agent_instructions,
|
|
553
|
+
auto_approve: contact.rows[0].auto_approve
|
|
554
|
+
}
|
|
555
|
+
```
|
|
556
|
+
|
|
557
|
+
---
|
|
558
|
+
|
|
559
|
+
## Agent Rules
|
|
560
|
+
|
|
561
|
+
1. **Always check `agent_paused`** before acting on a contact
|
|
562
|
+
2. **Always check `agent_instructions`** for per-contact overrides
|
|
563
|
+
3. **Always read context** before drafting (last 20 activities via `searchable_context`)
|
|
564
|
+
4. **Always set `sender` + `direction`** on activity insert (triggers depend on these)
|
|
565
|
+
5. **Always set `status`** on contact insert (trigger branches on this)
|
|
566
|
+
6. **Always update `last_activity_at`** on contact after activity insert
|
|
567
|
+
7. **Use `pending_approval`** unless `contact.auto_approve = 1`
|
|
568
|
+
8. **Use `auto_sent`** when `contact.auto_approve = 1` or you manually triggered it
|
|
569
|
+
9. **Never edit `crm_activities`** — append only, insert new rows
|
|
570
|
+
10. **Use `searchable_context`** for plain text summaries — avoids JSON parse on every query
|
|
571
|
+
|
|
572
|
+
---
|
|
573
|
+
|
|
574
|
+
## Connecting CRM to other tables
|
|
575
|
+
|
|
576
|
+
```ts
|
|
577
|
+
// Contact → Purchases (by email)
|
|
578
|
+
SELECT * FROM purchases WHERE email = ? ORDER BY created_at DESC
|
|
579
|
+
|
|
580
|
+
// Contact → Subscribers (by email)
|
|
581
|
+
SELECT * FROM subscribers WHERE email = ?
|
|
582
|
+
|
|
583
|
+
// Contact → Form submissions (by email match)
|
|
584
|
+
SELECT * FROM submissions WHERE answers LIKE '%' || ? || '%' LIMIT 10
|
|
585
|
+
|
|
586
|
+
// Contact → Community member (by user_id)
|
|
587
|
+
SELECT * FROM community_members WHERE user_id = ?
|
|
588
|
+
|
|
589
|
+
// All cross-table joins use email or user_id as the bridge
|
|
590
|
+
// user_id is identical in Central DB and UserDB (same ID, generated once)
|
|
591
|
+
```
|