@businesskit/agent 1.0.3 → 1.0.4
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/settings.json +16 -0
- package/.claude/settings.local.json +11 -0
- package/.env.example +10 -0
- package/CLAUDE.md +32 -13
- package/PLAN.md +95 -134
- package/REFERENCE.md +60 -24
- package/agents/_base.ts +352 -44
- package/agents/analytics/SOUL.md +11 -0
- package/agents/analytics/analytics.ts +405 -0
- package/agents/business/SOUL.md +11 -0
- package/agents/{csuite/cbo.ts → business/business.ts} +2 -2
- package/agents/ceo/SOUL.md +15 -0
- package/agents/{csuite → ceo}/ceo.ts +33 -8
- package/agents/content/SOUL.md +11 -0
- package/agents/content/content.ts +379 -0
- package/agents/copywriting/SOUL.md +11 -0
- package/agents/courses/SOUL.md +11 -0
- package/agents/courses/courses.ts +451 -0
- package/agents/docs/SOUL.md +11 -0
- package/agents/forms/SOUL.md +11 -0
- package/agents/forms/forms.ts +364 -0
- package/agents/hiring/SOUL.md +11 -0
- package/agents/hiring/hiring.ts +314 -0
- package/agents/marketing/SOUL.md +15 -0
- package/agents/{csuite/cmo.ts → marketing/marketing.ts} +3 -3
- package/agents/newsletter/SOUL.md +11 -0
- package/agents/operations/SOUL.md +11 -0
- package/agents/operations/operations.ts +157 -0
- package/agents/sales/SOUL.md +44 -0
- package/agents/sales/sales.ts +883 -0
- package/agents/scheduler/SOUL.md +11 -0
- package/agents/{growth → scheduler}/scheduler.ts +2 -2
- package/agents/seo/SOUL.md +11 -0
- package/agents/{growth/seo-agent.ts → seo/seo.ts} +60 -54
- package/agents/social/SOUL.md +11 -0
- package/agents/social/social.ts +744 -0
- package/agents/store/SOUL.md +11 -0
- package/agents/store/store.ts +514 -0
- package/cli.ts +44 -25
- package/lib/cms.ts +88 -0
- package/lib/db.ts +58 -11
- package/lib/enums.ts +25 -0
- package/lib/groups.ts +122 -0
- package/lib/id.ts +38 -10
- package/lib/profile.ts +44 -8
- package/lib/schema.ts +2015 -0
- package/lib/validate.ts +33 -0
- package/package.json +1 -1
- package/scripts/generate-schema.ts +244 -0
- package/setup.ts +16 -2
- package/skills/schema/SKILL.md +57 -16
- package/tsconfig.json +2 -1
- package/agents/creators/blog-writer.ts +0 -347
- package/agents/creators/course-creator.ts +0 -106
- package/agents/creators/crm-agent.ts +0 -479
- package/agents/creators/forms-builder.ts +0 -90
- package/agents/creators/jobs-manager.ts +0 -82
- package/agents/creators/store-manager.ts +0 -138
- package/agents/csuite/coo.ts +0 -115
- package/agents/growth/analytics-agent.ts +0 -52
- package/agents/growth/social-agent.ts +0 -346
- /package/{ingest.md → .claude/commands/ingest.md} +0 -0
- /package/agents/{creators/copywriter.ts → copywriting/copywriting.ts} +0 -0
- /package/agents/{creators/docs-writer.ts → docs/docs.ts} +0 -0
- /package/agents/{creators/newsletter-writer.ts → newsletter/newsletter.ts} +0 -0
- /package/{ABOUT-ME.md → context/about-me.md} +0 -0
- /package/{brand-voice.md → context/brand-voice.md} +0 -0
- /package/{working-style.md → context/working-style.md} +0 -0
|
@@ -1,479 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* CRM Agent — Agent-First CRM
|
|
3
|
-
*
|
|
4
|
-
* Reads crm_contacts.agent_context blob for per-contact memory.
|
|
5
|
-
* Drives the full outreach pipeline: research → enrich → DM draft → send → follow-up.
|
|
6
|
-
* Writes crm_activities as append-only interaction log.
|
|
7
|
-
* Updates crm_analytics via DB triggers (defined in crm.ts).
|
|
8
|
-
*
|
|
9
|
-
* agent_status lifecycle:
|
|
10
|
-
* pending → researching → enriched → outreach_ready → closed
|
|
11
|
-
*
|
|
12
|
-
* approval_status on activities:
|
|
13
|
-
* pending → agent drafts, waits for user
|
|
14
|
-
* approved → user confirmed, agent sends
|
|
15
|
-
* auto_sent → auto_approve=1 on contact, sent without waiting
|
|
16
|
-
*/
|
|
17
|
-
|
|
18
|
-
import { BaseAgent, db, ulid, now } from '../_base.ts'
|
|
19
|
-
|
|
20
|
-
// ── Types ─────────────────────────────────────────────────────────────────────
|
|
21
|
-
|
|
22
|
-
export interface Contact {
|
|
23
|
-
id: string
|
|
24
|
-
profile_id: string
|
|
25
|
-
first_name: string
|
|
26
|
-
last_name: string | null
|
|
27
|
-
email: string | null
|
|
28
|
-
platform: string | null
|
|
29
|
-
platform_username: string | null
|
|
30
|
-
platform_url: string | null
|
|
31
|
-
status: 'lead' | 'prospect' | 'customer' | 'churned' | 'archived'
|
|
32
|
-
outreach_status: string
|
|
33
|
-
lead_score: number
|
|
34
|
-
icp_match: string
|
|
35
|
-
agent_status: string
|
|
36
|
-
agent_context: Record<string, unknown>
|
|
37
|
-
auto_approve: number
|
|
38
|
-
suggested_dm: string | null
|
|
39
|
-
email_draft: string | null
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
export interface CreateContactInput {
|
|
43
|
-
first_name: string
|
|
44
|
-
last_name?: string
|
|
45
|
-
email?: string
|
|
46
|
-
phone?: string
|
|
47
|
-
company?: string
|
|
48
|
-
job_title?: string
|
|
49
|
-
platform?: string
|
|
50
|
-
platform_username?: string
|
|
51
|
-
platform_url?: string
|
|
52
|
-
bio?: string
|
|
53
|
-
audience_size?: number
|
|
54
|
-
source?: 'manual' | 'form' | 'import' | 'subscriber' | 'purchase' | 'agent_research' | 'n8n_webhook'
|
|
55
|
-
source_id?: string
|
|
56
|
-
tags?: string[]
|
|
57
|
-
notes?: string
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
export class CRMAgent extends BaseAgent {
|
|
61
|
-
readonly name = 'CRM Agent'
|
|
62
|
-
readonly title = 'CRM Agent'
|
|
63
|
-
|
|
64
|
-
// ── Contacts ─────────────────────────────────────────────────────────────────
|
|
65
|
-
|
|
66
|
-
async createContact(input: CreateContactInput): Promise<string> {
|
|
67
|
-
await this.init()
|
|
68
|
-
const id = ulid()
|
|
69
|
-
const ts = now()
|
|
70
|
-
|
|
71
|
-
await db.execute({
|
|
72
|
-
sql: `INSERT INTO crm_contacts
|
|
73
|
-
(id,profile_id,user_id,first_name,last_name,email,phone,company,
|
|
74
|
-
job_title,platform,platform_username,platform_url,bio,audience_size,
|
|
75
|
-
source,source_id,tags,notes,agent_status,agent_context,
|
|
76
|
-
created_at,updated_at)
|
|
77
|
-
VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,'pending','{}',?,?)`,
|
|
78
|
-
args: [
|
|
79
|
-
id, this.profileId, this.userId,
|
|
80
|
-
input.first_name, input.last_name ?? null,
|
|
81
|
-
input.email ?? null, input.phone ?? null,
|
|
82
|
-
input.company ?? null, input.job_title ?? null,
|
|
83
|
-
input.platform ?? null, input.platform_username ?? null,
|
|
84
|
-
input.platform_url ?? null, input.bio ?? null,
|
|
85
|
-
input.audience_size ?? null,
|
|
86
|
-
input.source ?? 'manual', input.source_id ?? null,
|
|
87
|
-
input.tags ? JSON.stringify(input.tags) : '[]',
|
|
88
|
-
input.notes ?? null, ts, ts,
|
|
89
|
-
],
|
|
90
|
-
})
|
|
91
|
-
return id
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
async getContact(id: string): Promise<Contact> {
|
|
95
|
-
const { rows: [r] } = await db.execute({
|
|
96
|
-
sql: 'SELECT * FROM crm_contacts WHERE id=? AND profile_id=? LIMIT 1',
|
|
97
|
-
args: [id, this.profileId],
|
|
98
|
-
})
|
|
99
|
-
if (!r) throw new Error(`Contact not found: ${id}`)
|
|
100
|
-
return this.parseContact(r)
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
async listContacts(opts: {
|
|
104
|
-
status?: string
|
|
105
|
-
outreach_status?: string
|
|
106
|
-
agent_status?: string
|
|
107
|
-
limit?: number
|
|
108
|
-
order_by?: 'lead_score' | 'created_at' | 'next_follow_up_at'
|
|
109
|
-
} = {}) {
|
|
110
|
-
await this.init()
|
|
111
|
-
const args: unknown[] = [this.profileId]
|
|
112
|
-
let where = 'WHERE profile_id=? AND archived=0'
|
|
113
|
-
|
|
114
|
-
if (opts.status) { where += ' AND status=?'; args.push(opts.status) }
|
|
115
|
-
if (opts.outreach_status) { where += ' AND outreach_status=?'; args.push(opts.outreach_status) }
|
|
116
|
-
if (opts.agent_status) { where += ' AND agent_status=?'; args.push(opts.agent_status) }
|
|
117
|
-
|
|
118
|
-
const orderMap = {
|
|
119
|
-
lead_score: 'lead_score DESC',
|
|
120
|
-
created_at: 'created_at DESC',
|
|
121
|
-
next_follow_up_at: 'next_follow_up_at ASC',
|
|
122
|
-
}
|
|
123
|
-
const order = orderMap[opts.order_by ?? 'created_at']
|
|
124
|
-
|
|
125
|
-
const { rows } = await db.execute({
|
|
126
|
-
sql: `SELECT id,first_name,last_name,email,platform,platform_username,
|
|
127
|
-
status,outreach_status,agent_status,lead_score,icp_match,
|
|
128
|
-
suggested_dm,next_follow_up_at,total_spent_cents,total_purchases
|
|
129
|
-
FROM crm_contacts ${where} ORDER BY ${order} LIMIT ?`,
|
|
130
|
-
args: [...args, opts.limit ?? 50],
|
|
131
|
-
})
|
|
132
|
-
return rows
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
// ── Lead scoring + enrichment ─────────────────────────────────────────────
|
|
136
|
-
|
|
137
|
-
async scoreContact(id: string, score: number, reason: string, icpMatch: 'strong' | 'moderate' | 'weak') {
|
|
138
|
-
const ts = now()
|
|
139
|
-
await db.execute({
|
|
140
|
-
sql: `UPDATE crm_contacts SET
|
|
141
|
-
lead_score=?,lead_score_reason=?,icp_match=?,
|
|
142
|
-
agent_status='enriched',updated_at=? WHERE id=?`,
|
|
143
|
-
args: [score, reason, icpMatch, ts, id],
|
|
144
|
-
})
|
|
145
|
-
await this.logActivity(id, 'agent_action', 'outbound', 'agent',
|
|
146
|
-
`Scored: ${score}/100 (${icpMatch} ICP) — ${reason}`)
|
|
147
|
-
return this.getContact(id)
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
async enrichContact(id: string, enrichment: {
|
|
151
|
-
pain_point?: string
|
|
152
|
-
company_size?: string
|
|
153
|
-
industry?: string
|
|
154
|
-
business_model?: string
|
|
155
|
-
estimated_revenue?: string
|
|
156
|
-
recent_post?: string
|
|
157
|
-
recent_post_url?: string
|
|
158
|
-
enrichment_source?: string
|
|
159
|
-
}) {
|
|
160
|
-
const ts = now()
|
|
161
|
-
const sets: string[] = ['enriched_at=?', 'updated_at=?', "agent_status='enriched'"]
|
|
162
|
-
const args: unknown[] = [ts, ts]
|
|
163
|
-
|
|
164
|
-
const fieldMap: Record<string, string> = {
|
|
165
|
-
pain_point: 'pain_point', company_size: 'company_size',
|
|
166
|
-
industry: 'industry', business_model: 'business_model',
|
|
167
|
-
estimated_revenue: 'estimated_revenue',
|
|
168
|
-
recent_post: 'recent_post', recent_post_url: 'recent_post_url',
|
|
169
|
-
enrichment_source: 'enrichment_source',
|
|
170
|
-
}
|
|
171
|
-
for (const [k, col] of Object.entries(fieldMap)) {
|
|
172
|
-
if ((enrichment as any)[k] !== undefined) {
|
|
173
|
-
sets.push(`${col}=?`)
|
|
174
|
-
args.push((enrichment as any)[k])
|
|
175
|
-
}
|
|
176
|
-
}
|
|
177
|
-
args.push(id)
|
|
178
|
-
|
|
179
|
-
await db.execute({ sql: `UPDATE crm_contacts SET ${sets.join(',')} WHERE id=?`, args })
|
|
180
|
-
return this.getContact(id)
|
|
181
|
-
}
|
|
182
|
-
|
|
183
|
-
// ── Outreach ──────────────────────────────────────────────────────────────
|
|
184
|
-
|
|
185
|
-
/** Draft a DM — stores in suggested_dm, creates pending activity */
|
|
186
|
-
async draftDM(id: string, message: string): Promise<void> {
|
|
187
|
-
const ts = now()
|
|
188
|
-
const contact = await this.getContact(id)
|
|
189
|
-
|
|
190
|
-
await db.execute({
|
|
191
|
-
sql: `UPDATE crm_contacts SET
|
|
192
|
-
suggested_dm=?,outreach_status='dm_drafted',
|
|
193
|
-
agent_status='outreach_ready',updated_at=? WHERE id=?`,
|
|
194
|
-
args: [message, ts, id],
|
|
195
|
-
})
|
|
196
|
-
|
|
197
|
-
const approvalStatus = contact.auto_approve ? 'auto_sent' : 'pending'
|
|
198
|
-
await this.logActivity(id, 'dm', 'outbound', 'agent', message, {
|
|
199
|
-
approval_status: approvalStatus,
|
|
200
|
-
channel: contact.platform ?? 'dm',
|
|
201
|
-
})
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
/** Draft a cold email */
|
|
205
|
-
async draftEmail(id: string, subject: string, body: string): Promise<void> {
|
|
206
|
-
const ts = now()
|
|
207
|
-
const contact = await this.getContact(id)
|
|
208
|
-
|
|
209
|
-
await db.execute({
|
|
210
|
-
sql: `UPDATE crm_contacts SET
|
|
211
|
-
email_subject=?,email_draft=?,outreach_status='email_drafted',
|
|
212
|
-
agent_status='outreach_ready',updated_at=? WHERE id=?`,
|
|
213
|
-
args: [subject, body, ts, id],
|
|
214
|
-
})
|
|
215
|
-
|
|
216
|
-
const approvalStatus = contact.auto_approve ? 'auto_sent' : 'pending'
|
|
217
|
-
await this.logActivity(id, 'email', 'outbound', 'agent', body, {
|
|
218
|
-
subject,
|
|
219
|
-
approval_status: approvalStatus,
|
|
220
|
-
})
|
|
221
|
-
}
|
|
222
|
-
|
|
223
|
-
/** Mark DM as sent (after user approves + actually sends) */
|
|
224
|
-
async markDMSent(id: string): Promise<void> {
|
|
225
|
-
const ts = now()
|
|
226
|
-
await db.execute({
|
|
227
|
-
sql: `UPDATE crm_contacts SET
|
|
228
|
-
dm_sent=1,dm_sent_at=?,outreach_status='dm_sent',
|
|
229
|
-
outreach_attempts=outreach_attempts+1,last_contacted_at=?,updated_at=? WHERE id=?`,
|
|
230
|
-
args: [ts, ts, ts, id],
|
|
231
|
-
})
|
|
232
|
-
}
|
|
233
|
-
|
|
234
|
-
/** Mark email as sent */
|
|
235
|
-
async markEmailSent(id: string): Promise<void> {
|
|
236
|
-
const ts = now()
|
|
237
|
-
await db.execute({
|
|
238
|
-
sql: `UPDATE crm_contacts SET
|
|
239
|
-
email_sent=1,email_sent_at=?,outreach_status='email_sent',
|
|
240
|
-
outreach_attempts=outreach_attempts+1,last_contacted_at=?,updated_at=? WHERE id=?`,
|
|
241
|
-
args: [ts, ts, ts, id],
|
|
242
|
-
})
|
|
243
|
-
}
|
|
244
|
-
|
|
245
|
-
/** Log an inbound reply */
|
|
246
|
-
async logReply(id: string, channel: 'dm' | 'email', content: string, sentiment: 'positive' | 'neutral' | 'negative') {
|
|
247
|
-
const ts = now()
|
|
248
|
-
await db.execute({
|
|
249
|
-
sql: `UPDATE crm_contacts SET
|
|
250
|
-
outreach_status='replied',last_reply_at=?,last_reply_channel=?,
|
|
251
|
-
reply_sentiment=?,last_activity_at=?,updated_at=? WHERE id=?`,
|
|
252
|
-
args: [ts, channel, sentiment, ts, ts, id],
|
|
253
|
-
})
|
|
254
|
-
await this.logActivity(id, channel === 'dm' ? 'dm' : 'email', 'inbound', 'contact', content)
|
|
255
|
-
}
|
|
256
|
-
|
|
257
|
-
// ── Deals ──────────────────────────────────────────────────────────────────
|
|
258
|
-
|
|
259
|
-
async createDeal(contactId: string, input: {
|
|
260
|
-
title: string
|
|
261
|
-
value_cents: number
|
|
262
|
-
stage?: string
|
|
263
|
-
probability?: number
|
|
264
|
-
product_id?: string
|
|
265
|
-
expected_close_at?: number
|
|
266
|
-
}): Promise<string> {
|
|
267
|
-
await this.init()
|
|
268
|
-
const id = ulid()
|
|
269
|
-
const ts = now()
|
|
270
|
-
|
|
271
|
-
await db.execute({
|
|
272
|
-
sql: `INSERT INTO crm_deals
|
|
273
|
-
(id,profile_id,contact_id,title,value_cents,stage,probability,product_id,
|
|
274
|
-
expected_close_at,created_at,updated_at)
|
|
275
|
-
VALUES (?,?,?,?,?,?,?,?,?,?,?)`,
|
|
276
|
-
args: [
|
|
277
|
-
id, this.profileId, contactId,
|
|
278
|
-
input.title, input.value_cents,
|
|
279
|
-
input.stage ?? 'new', input.probability ?? 10,
|
|
280
|
-
input.product_id ?? null, input.expected_close_at ?? null,
|
|
281
|
-
ts, ts,
|
|
282
|
-
],
|
|
283
|
-
})
|
|
284
|
-
return id
|
|
285
|
-
}
|
|
286
|
-
|
|
287
|
-
async updateDealStage(dealId: string, stage: string, lostReason?: string) {
|
|
288
|
-
const ts = now()
|
|
289
|
-
const closedAt = ['won', 'lost'].includes(stage) ? ts : null
|
|
290
|
-
await db.execute({
|
|
291
|
-
sql: `UPDATE crm_deals SET stage=?,lost_reason=?,closed_at=?,updated_at=? WHERE id=?`,
|
|
292
|
-
args: [stage, lostReason ?? null, closedAt, ts, dealId],
|
|
293
|
-
})
|
|
294
|
-
}
|
|
295
|
-
|
|
296
|
-
async listDeals(opts: { stage?: string; contact_id?: string } = {}) {
|
|
297
|
-
await this.init()
|
|
298
|
-
let where = 'WHERE profile_id=?'
|
|
299
|
-
const args: unknown[] = [this.profileId]
|
|
300
|
-
if (opts.stage) { where += ' AND stage=?'; args.push(opts.stage) }
|
|
301
|
-
if (opts.contact_id) { where += ' AND contact_id=?'; args.push(opts.contact_id) }
|
|
302
|
-
|
|
303
|
-
const { rows } = await db.execute({
|
|
304
|
-
sql: `SELECT d.*,c.first_name,c.last_name,c.company
|
|
305
|
-
FROM crm_deals d
|
|
306
|
-
JOIN crm_contacts c ON c.id=d.contact_id
|
|
307
|
-
${where} ORDER BY d.created_at DESC`,
|
|
308
|
-
args,
|
|
309
|
-
})
|
|
310
|
-
return rows
|
|
311
|
-
}
|
|
312
|
-
|
|
313
|
-
// ── Tasks ─────────────────────────────────────────────────────────────────
|
|
314
|
-
|
|
315
|
-
async createTask(input: {
|
|
316
|
-
title: string
|
|
317
|
-
contact_id?: string
|
|
318
|
-
deal_id?: string
|
|
319
|
-
due_at?: number
|
|
320
|
-
priority?: 'low' | 'medium' | 'high'
|
|
321
|
-
description?: string
|
|
322
|
-
}): Promise<string> {
|
|
323
|
-
await this.init()
|
|
324
|
-
const id = ulid()
|
|
325
|
-
const ts = now()
|
|
326
|
-
|
|
327
|
-
await db.execute({
|
|
328
|
-
sql: `INSERT INTO crm_tasks
|
|
329
|
-
(id,profile_id,contact_id,deal_id,title,description,due_at,priority,status,created_at,updated_at)
|
|
330
|
-
VALUES (?,?,?,?,?,?,?,?, 'open',?,?)`,
|
|
331
|
-
args: [
|
|
332
|
-
id, this.profileId,
|
|
333
|
-
input.contact_id ?? null, input.deal_id ?? null,
|
|
334
|
-
input.title, input.description ?? null,
|
|
335
|
-
input.due_at ?? null, input.priority ?? 'medium',
|
|
336
|
-
ts, ts,
|
|
337
|
-
],
|
|
338
|
-
})
|
|
339
|
-
return id
|
|
340
|
-
}
|
|
341
|
-
|
|
342
|
-
async completeTask(id: string) {
|
|
343
|
-
const ts = now()
|
|
344
|
-
await db.execute({
|
|
345
|
-
sql: `UPDATE crm_tasks SET status='done',completed_at=?,updated_at=? WHERE id=?`,
|
|
346
|
-
args: [ts, ts, id],
|
|
347
|
-
})
|
|
348
|
-
}
|
|
349
|
-
|
|
350
|
-
async listTasks(opts: { status?: string; due_today?: boolean } = {}) {
|
|
351
|
-
await this.init()
|
|
352
|
-
let where = 'WHERE profile_id=?'
|
|
353
|
-
const args: unknown[] = [this.profileId]
|
|
354
|
-
if (opts.status) { where += ' AND status=?'; args.push(opts.status) }
|
|
355
|
-
if (opts.due_today) {
|
|
356
|
-
const endOfDay = Math.floor(new Date().setHours(23, 59, 59, 999) / 1000)
|
|
357
|
-
where += ' AND due_at<=? AND status=\'open\''
|
|
358
|
-
args.push(endOfDay)
|
|
359
|
-
}
|
|
360
|
-
const { rows } = await db.execute({
|
|
361
|
-
sql: `SELECT t.*,c.first_name,c.last_name FROM crm_tasks t
|
|
362
|
-
LEFT JOIN crm_contacts c ON c.id=t.contact_id
|
|
363
|
-
${where} ORDER BY due_at ASC NULLS LAST`,
|
|
364
|
-
args,
|
|
365
|
-
})
|
|
366
|
-
return rows
|
|
367
|
-
}
|
|
368
|
-
|
|
369
|
-
// ── Pipeline overview for CXO ─────────────────────────────────────────────
|
|
370
|
-
|
|
371
|
-
async pipelineSummary() {
|
|
372
|
-
await this.init()
|
|
373
|
-
const { rows: [stats] } = await db.execute({
|
|
374
|
-
sql: `SELECT * FROM crm_analytics WHERE profile_id=? LIMIT 1`,
|
|
375
|
-
args: [this.profileId],
|
|
376
|
-
})
|
|
377
|
-
|
|
378
|
-
const { rows: pendingApprovals } = await db.execute({
|
|
379
|
-
sql: `SELECT a.id,a.type,a.body,c.first_name,c.last_name,c.platform
|
|
380
|
-
FROM crm_activities a
|
|
381
|
-
JOIN crm_contacts c ON c.id=a.contact_id
|
|
382
|
-
WHERE a.profile_id=? AND a.approval_status='pending'
|
|
383
|
-
ORDER BY a.occurred_at ASC LIMIT 10`,
|
|
384
|
-
args: [this.profileId],
|
|
385
|
-
})
|
|
386
|
-
|
|
387
|
-
const { rows: followUps } = await db.execute({
|
|
388
|
-
sql: `SELECT id,first_name,last_name,platform_username,outreach_status,next_follow_up_at
|
|
389
|
-
FROM crm_contacts
|
|
390
|
-
WHERE profile_id=? AND next_follow_up_at<=? AND archived=0
|
|
391
|
-
ORDER BY next_follow_up_at ASC LIMIT 10`,
|
|
392
|
-
args: [this.profileId, now()],
|
|
393
|
-
})
|
|
394
|
-
|
|
395
|
-
return { stats, pendingApprovals, followUps }
|
|
396
|
-
}
|
|
397
|
-
|
|
398
|
-
/** Top leads by score, ready for outreach */
|
|
399
|
-
async hotLeads(limit = 10) {
|
|
400
|
-
await this.init()
|
|
401
|
-
const { rows } = await db.execute({
|
|
402
|
-
sql: `SELECT id,first_name,last_name,platform,platform_username,
|
|
403
|
-
lead_score,icp_match,pain_point,audience_size,
|
|
404
|
-
outreach_status,agent_status
|
|
405
|
-
FROM crm_contacts
|
|
406
|
-
WHERE profile_id=? AND archived=0 AND agent_status IN ('enriched','outreach_ready')
|
|
407
|
-
ORDER BY lead_score DESC LIMIT ?`,
|
|
408
|
-
args: [this.profileId, limit],
|
|
409
|
-
})
|
|
410
|
-
return rows
|
|
411
|
-
}
|
|
412
|
-
|
|
413
|
-
// ── Agent context (scratchpad per contact) ───────────────────────────────
|
|
414
|
-
|
|
415
|
-
async updateAgentContext(id: string, updates: Record<string, unknown>) {
|
|
416
|
-
const contact = await this.getContact(id)
|
|
417
|
-
const merged = { ...contact.agent_context, ...updates, updated_at: iso() }
|
|
418
|
-
const ts = now()
|
|
419
|
-
await db.execute({
|
|
420
|
-
sql: `UPDATE crm_contacts SET agent_context=?,updated_at=? WHERE id=?`,
|
|
421
|
-
args: [JSON.stringify(merged), ts, id],
|
|
422
|
-
})
|
|
423
|
-
}
|
|
424
|
-
|
|
425
|
-
async setNextFollowUp(id: string, atUnix: number, note?: string) {
|
|
426
|
-
const ts = now()
|
|
427
|
-
await db.execute({
|
|
428
|
-
sql: `UPDATE crm_contacts SET next_follow_up_at=?,follow_up_count=follow_up_count+1,updated_at=? WHERE id=?`,
|
|
429
|
-
args: [atUnix, ts, id],
|
|
430
|
-
})
|
|
431
|
-
if (note) await this.logActivity(id, 'note', 'outbound', 'agent', note)
|
|
432
|
-
}
|
|
433
|
-
|
|
434
|
-
// ── Private helpers ───────────────────────────────────────────────────────
|
|
435
|
-
|
|
436
|
-
private async logActivity(
|
|
437
|
-
contactId: string,
|
|
438
|
-
type: string,
|
|
439
|
-
direction: 'inbound' | 'outbound',
|
|
440
|
-
sender: 'agent' | 'you' | 'contact',
|
|
441
|
-
body: string,
|
|
442
|
-
extra: Record<string, unknown> = {}
|
|
443
|
-
) {
|
|
444
|
-
await this.init()
|
|
445
|
-
const id = ulid()
|
|
446
|
-
const ts = now()
|
|
447
|
-
const { approval_status = 'auto_sent', subject, channel } = extra
|
|
448
|
-
|
|
449
|
-
await db.execute({
|
|
450
|
-
sql: `INSERT INTO crm_activities
|
|
451
|
-
(id,profile_id,contact_id,type,direction,sender,body,
|
|
452
|
-
approval_status,searchable_context,occurred_at,created_at)
|
|
453
|
-
VALUES (?,?,?,?,?,?,?,?,?,?,?)`,
|
|
454
|
-
args: [
|
|
455
|
-
id, this.profileId, contactId, type, direction, sender, body,
|
|
456
|
-
approval_status,
|
|
457
|
-
`${type} ${direction} ${body.slice(0, 200)}`,
|
|
458
|
-
ts, ts,
|
|
459
|
-
],
|
|
460
|
-
})
|
|
461
|
-
}
|
|
462
|
-
|
|
463
|
-
private parseContact(r: Record<string, unknown>): Contact {
|
|
464
|
-
return {
|
|
465
|
-
...r,
|
|
466
|
-
agent_context: safeJSON(r.agent_context, {}),
|
|
467
|
-
} as unknown as Contact
|
|
468
|
-
}
|
|
469
|
-
}
|
|
470
|
-
|
|
471
|
-
const safeJSON = (v: unknown, fb: unknown) => {
|
|
472
|
-
try { return JSON.parse(v as string) } catch { return fb }
|
|
473
|
-
}
|
|
474
|
-
|
|
475
|
-
function iso() {
|
|
476
|
-
return new Date().toISOString().slice(0, 19) + 'Z'
|
|
477
|
-
}
|
|
478
|
-
|
|
479
|
-
export const crmAgent = new CRMAgent()
|
|
@@ -1,90 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* FELIX — Forms Builder
|
|
3
|
-
* "I turn questions into insights."
|
|
4
|
-
* Writes to: forms, questions. Reads: submissions, form_analytics
|
|
5
|
-
*/
|
|
6
|
-
|
|
7
|
-
import { BaseAgent, db, ulid, iso } from '../_base.ts'
|
|
8
|
-
import { uniqueSlug } from '../../lib/slug.ts'
|
|
9
|
-
|
|
10
|
-
export type QuestionType = 'text'|'email'|'select'|'multiselect'|'rating'|'date'|'file'|'url'|'embed'
|
|
11
|
-
|
|
12
|
-
export interface Question {
|
|
13
|
-
type: QuestionType; title: string; description?: string
|
|
14
|
-
required?: boolean; options?: string[]; embed_url?: string
|
|
15
|
-
settings?: Record<string, unknown>
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
export interface FormInput {
|
|
19
|
-
title: string; questions: Question[]
|
|
20
|
-
thank_you?: { title?: string; message?: string; redirect_url?: string }
|
|
21
|
-
publish?: boolean
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
export class FormsBuilder extends BaseAgent {
|
|
25
|
-
readonly name = 'FELIX'
|
|
26
|
-
readonly title = 'Forms Builder'
|
|
27
|
-
|
|
28
|
-
async create(input: FormInput) {
|
|
29
|
-
await this.init()
|
|
30
|
-
const id = ulid()
|
|
31
|
-
const slug = await uniqueSlug('forms', input.title, this.profileId)
|
|
32
|
-
const ts = iso()
|
|
33
|
-
|
|
34
|
-
await db.execute({
|
|
35
|
-
sql: 'INSERT INTO forms (id,profile_id,title,slug,published,hidden,thank_you_settings,created_at) VALUES (?,?,?,?,?,0,?,?)',
|
|
36
|
-
args: [id, this.profileId, input.title, slug, input.publish ? 1 : 0,
|
|
37
|
-
input.thank_you ? JSON.stringify(input.thank_you) : null, ts],
|
|
38
|
-
})
|
|
39
|
-
|
|
40
|
-
if (input.questions.length) {
|
|
41
|
-
await db.batch(input.questions.map((q, i) => ({
|
|
42
|
-
sql: `INSERT INTO questions (id,form_id,type,title,description,position,options,embed_url,required,settings)
|
|
43
|
-
VALUES (?,?,?,?,?,?,?,?,?,?)`,
|
|
44
|
-
args: [
|
|
45
|
-
ulid(), id, q.type, q.title, q.description ?? null, i,
|
|
46
|
-
q.options ? JSON.stringify(q.options) : null,
|
|
47
|
-
q.embed_url ?? null, q.required ? 1 : 0,
|
|
48
|
-
q.settings ? JSON.stringify(q.settings) : null,
|
|
49
|
-
],
|
|
50
|
-
})))
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
// Seed analytics
|
|
54
|
-
await db.execute({ sql: 'INSERT OR IGNORE INTO form_analytics (form_id,updated_at) VALUES (?,?)', args: [id, ts] })
|
|
55
|
-
return this.get(id)
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
async get(id: string) {
|
|
59
|
-
const [{ rows: [form] }, { rows: questions }] = await Promise.all([
|
|
60
|
-
db.execute({ sql: 'SELECT * FROM forms WHERE id=?', args: [id] }),
|
|
61
|
-
db.execute({ sql: 'SELECT * FROM questions WHERE form_id=? ORDER BY position', args: [id] }),
|
|
62
|
-
])
|
|
63
|
-
if (!form) throw new Error(`Form not found: ${id}`)
|
|
64
|
-
return { ...form, questions }
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
async list() {
|
|
68
|
-
await this.init()
|
|
69
|
-
const { rows } = await db.execute({
|
|
70
|
-
sql: `SELECT f.id,f.title,f.slug,f.published,fa.views,fa.submissions
|
|
71
|
-
FROM forms f LEFT JOIN form_analytics fa ON fa.form_id=f.id
|
|
72
|
-
WHERE f.profile_id=? AND f.hidden=0 ORDER BY f.created_at DESC`,
|
|
73
|
-
args: [this.profileId],
|
|
74
|
-
})
|
|
75
|
-
return rows
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
async submissions(formId: string, limit = 50) {
|
|
79
|
-
const { rows } = await db.execute({
|
|
80
|
-
sql: 'SELECT * FROM submissions WHERE form_id=? ORDER BY created_at DESC LIMIT ?',
|
|
81
|
-
args: [formId, limit],
|
|
82
|
-
})
|
|
83
|
-
return rows
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
async publish(id: string) { await super.publish('forms', id); return this.get(id) }
|
|
87
|
-
async archive(id: string) { await super.archive('forms', id) }
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
export const formsBuilder = new FormsBuilder()
|
|
@@ -1,82 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* JOB — Jobs Manager
|
|
3
|
-
* "Every great team starts with a great listing."
|
|
4
|
-
* Writes to: job_listings, reads job_applications
|
|
5
|
-
*/
|
|
6
|
-
|
|
7
|
-
import { BaseAgent, db, ulid, iso } from '../_base.ts'
|
|
8
|
-
import { uniqueSlug } from '../../lib/slug.ts'
|
|
9
|
-
|
|
10
|
-
export interface JobInput {
|
|
11
|
-
title: string; company: string; location: string
|
|
12
|
-
location_type: 'remote' | 'onsite' | 'hybrid'
|
|
13
|
-
employment_type: 'full-time' | 'part-time' | 'contract' | 'freelance'
|
|
14
|
-
description: string; excerpt?: string; requirements?: string
|
|
15
|
-
salary_min?: number; salary_max?: number; salary_currency?: string
|
|
16
|
-
image_url?: string; expires_at?: string; publish?: boolean
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
export class JobsManager extends BaseAgent {
|
|
20
|
-
readonly name = 'JOB'
|
|
21
|
-
readonly title = 'Jobs Manager'
|
|
22
|
-
|
|
23
|
-
async create(input: JobInput) {
|
|
24
|
-
await this.init()
|
|
25
|
-
const id = ulid()
|
|
26
|
-
const slug = await uniqueSlug('job_listings', input.title, this.profileId)
|
|
27
|
-
const ts = iso()
|
|
28
|
-
|
|
29
|
-
await db.execute({
|
|
30
|
-
sql: `INSERT INTO job_listings
|
|
31
|
-
(id,profile_id,user_id,title,company,location,location_type,
|
|
32
|
-
employment_type,description,excerpt,requirements,
|
|
33
|
-
salary_min,salary_max,salary_currency,slug,image_url,
|
|
34
|
-
expires_at,published,hidden,created_at,updated_at)
|
|
35
|
-
VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,0,?,?)`,
|
|
36
|
-
args: [
|
|
37
|
-
id, this.profileId, this.userId,
|
|
38
|
-
input.title, input.company, input.location, input.location_type,
|
|
39
|
-
input.employment_type, input.description,
|
|
40
|
-
input.excerpt ?? null, input.requirements ?? null,
|
|
41
|
-
input.salary_min ?? null, input.salary_max ?? null,
|
|
42
|
-
input.salary_currency ?? 'USD', slug,
|
|
43
|
-
input.image_url ?? null, input.expires_at ?? null,
|
|
44
|
-
input.publish ? 1 : 0, ts, ts,
|
|
45
|
-
],
|
|
46
|
-
})
|
|
47
|
-
return this.get(id)
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
async get(id: string) {
|
|
51
|
-
const { rows: [r] } = await db.execute({ sql: 'SELECT * FROM job_listings WHERE id=?', args: [id] })
|
|
52
|
-
if (!r) throw new Error(`Job not found: ${id}`)
|
|
53
|
-
return r
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
async list(opts: { published?: boolean } = {}) {
|
|
57
|
-
await this.init()
|
|
58
|
-
const args: unknown[] = [this.profileId]
|
|
59
|
-
let where = 'WHERE profile_id=? AND hidden=0'
|
|
60
|
-
if (opts.published !== undefined) { where += ' AND published=?'; args.push(opts.published ? 1 : 0) }
|
|
61
|
-
const { rows } = await db.execute({
|
|
62
|
-
sql: `SELECT id,title,company,location_type,employment_type,published,total_applicants
|
|
63
|
-
FROM job_listings ${where} ORDER BY created_at DESC`,
|
|
64
|
-
args,
|
|
65
|
-
})
|
|
66
|
-
return rows
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
async applications(jobId: string) {
|
|
70
|
-
const { rows } = await db.execute({
|
|
71
|
-
sql: 'SELECT id,full_name,email,city,country,created_at FROM job_applications WHERE job_id=? ORDER BY created_at DESC',
|
|
72
|
-
args: [jobId],
|
|
73
|
-
})
|
|
74
|
-
return rows
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
async publish(id: string) { await super.publish('job_listings', id); return this.get(id) }
|
|
78
|
-
async archive(id: string) { await super.archive('job_listings', id) }
|
|
79
|
-
async summary() { await this.init(); return this.count('job_listings') }
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
export const jobsManager = new JobsManager()
|