@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
package/lib/validate.ts
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
// lib/validate.ts
|
|
2
|
+
// Guard numeric fields before DB writes.
|
|
3
|
+
// DB triggers will reject out-of-bounds — catch early for clearer errors.
|
|
4
|
+
|
|
5
|
+
export function leadScore(n: number): number {
|
|
6
|
+
if (!Number.isFinite(n) || n < 0 || n > 100)
|
|
7
|
+
throw new Error(`lead_score must be 0–100, got ${n}`)
|
|
8
|
+
return Math.round(n)
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export function probability(n: number): number {
|
|
12
|
+
if (!Number.isFinite(n) || n < 0 || n > 100)
|
|
13
|
+
throw new Error(`probability must be 0–100, got ${n}`)
|
|
14
|
+
return Math.round(n)
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export function cents(n: number): number {
|
|
18
|
+
if (!Number.isFinite(n) || n < 0 || !Number.isInteger(n))
|
|
19
|
+
throw new Error(`cents must be non-negative integer, got ${n}`)
|
|
20
|
+
return n
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export function hotScore(n: number): number {
|
|
24
|
+
if (!Number.isFinite(n) || n < 0)
|
|
25
|
+
throw new Error(`hot_score must be >= 0, got ${n}`)
|
|
26
|
+
return n
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export function slug(s: string): string {
|
|
30
|
+
if (!s || s.length > 200)
|
|
31
|
+
throw new Error(`slug must be 1–200 chars, got "${s}"`)
|
|
32
|
+
return s.toLowerCase().replace(/[^a-z0-9-]/g, '-').replace(/-+/g, '-')
|
|
33
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@businesskit/agent",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.4",
|
|
4
4
|
"description": "Autonomous business agent team — CEO, CRM, Social, SEO, Blog Writer and more. Powered by your own Turso database. Works with Claude Code, Claude Cowork, Gemini CLI, and Codex.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"bin": {
|
|
@@ -0,0 +1,244 @@
|
|
|
1
|
+
#!/usr/bin/env tsx
|
|
2
|
+
/**
|
|
3
|
+
* scripts/generate-schema.ts
|
|
4
|
+
*
|
|
5
|
+
* Parses `businesskit-files/*.ts` → emits `lib/schema.ts`.
|
|
6
|
+
*
|
|
7
|
+
* What it does:
|
|
8
|
+
* 1. Finds every `CREATE TABLE IF NOT EXISTS <name> ( ... )` in the schema files
|
|
9
|
+
* 2. Parses columns: name, SQL type, NOT NULL, DEFAULT
|
|
10
|
+
* 3. Detects enum values from the nearest `-- 'a'|'b'|'c'`-style comment
|
|
11
|
+
* on the same line or the next line
|
|
12
|
+
* 4. Emits one `export interface <PascalName>` per table + a `TABLES` const
|
|
13
|
+
*
|
|
14
|
+
* Run:
|
|
15
|
+
* npx tsx scripts/generate-schema.ts
|
|
16
|
+
*
|
|
17
|
+
* The output is intentionally conservative — when in doubt, the column type
|
|
18
|
+
* widens to `string | null`. Never hand-edit lib/schema.ts; regenerate.
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
import { readFileSync, writeFileSync, readdirSync } from 'node:fs'
|
|
22
|
+
import { join, dirname } from 'node:path'
|
|
23
|
+
import { fileURLToPath } from 'node:url'
|
|
24
|
+
|
|
25
|
+
const __dirname = dirname(fileURLToPath(import.meta.url))
|
|
26
|
+
const REPO = join(__dirname, '..')
|
|
27
|
+
const SRC_DIR = join(REPO, 'businesskit-files')
|
|
28
|
+
const OUT = join(REPO, 'lib', 'schema.ts')
|
|
29
|
+
|
|
30
|
+
// ── Types ────────────────────────────────────────────────────────────────────
|
|
31
|
+
|
|
32
|
+
interface Column {
|
|
33
|
+
name: string
|
|
34
|
+
sqlType: string
|
|
35
|
+
notNull: boolean
|
|
36
|
+
hasDefault: boolean
|
|
37
|
+
enumValues: string[] // parsed from -- 'a'|'b'|'c' comments
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
interface Table {
|
|
41
|
+
name: string
|
|
42
|
+
columns: Column[]
|
|
43
|
+
sourceFile: string
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
// ── Parser ───────────────────────────────────────────────────────────────────
|
|
47
|
+
|
|
48
|
+
function extractCreateTables(source: string, sourceFile: string): Table[] {
|
|
49
|
+
const tables: Table[] = []
|
|
50
|
+
// CREATE TABLE IF NOT EXISTS name ( ... ) — match everything up to the closing
|
|
51
|
+
// paren that's followed by `,` or backtick (the template-string end).
|
|
52
|
+
const re = /CREATE TABLE IF NOT EXISTS\s+(\w+)\s*\(([\s\S]*?)\n\s*\)/g
|
|
53
|
+
|
|
54
|
+
let m: RegExpExecArray | null
|
|
55
|
+
while ((m = re.exec(source)) !== null) {
|
|
56
|
+
const name = m[1]
|
|
57
|
+
const body = m[2]
|
|
58
|
+
tables.push({ name, columns: parseColumns(body), sourceFile })
|
|
59
|
+
}
|
|
60
|
+
return tables
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function parseColumns(body: string): Column[] {
|
|
64
|
+
// Split on commas at depth 0 (SQLite col defs don't nest parens much,
|
|
65
|
+
// but constraints like CHECK(...) might; fold them conservatively).
|
|
66
|
+
const lines = splitTopLevelCommas(body)
|
|
67
|
+
const cols: Column[] = []
|
|
68
|
+
let pendingEnum: string[] = []
|
|
69
|
+
|
|
70
|
+
for (let raw of lines) {
|
|
71
|
+
raw = raw.trim()
|
|
72
|
+
if (!raw) continue
|
|
73
|
+
|
|
74
|
+
// Detect comment-only lines — they're enum hints for the PREVIOUS column
|
|
75
|
+
const commentOnly = raw.match(/^--\s*(.+)/)
|
|
76
|
+
if (commentOnly) {
|
|
77
|
+
const hinted = parseEnumComment(commentOnly[1])
|
|
78
|
+
if (hinted.length && cols.length) {
|
|
79
|
+
const last = cols[cols.length - 1]
|
|
80
|
+
if (!last.enumValues.length) last.enumValues = hinted
|
|
81
|
+
}
|
|
82
|
+
continue
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
// Skip table-level constraints (PRIMARY KEY (...), UNIQUE(...), FOREIGN KEY)
|
|
86
|
+
const upper = raw.toUpperCase()
|
|
87
|
+
if (/^(PRIMARY KEY|UNIQUE|FOREIGN KEY|CHECK|CONSTRAINT)\s*[(\s]/.test(upper)) continue
|
|
88
|
+
|
|
89
|
+
const col = parseColumnLine(raw)
|
|
90
|
+
if (!col) continue
|
|
91
|
+
|
|
92
|
+
// Same-line comment → enum values
|
|
93
|
+
if (pendingEnum.length) {
|
|
94
|
+
col.enumValues = pendingEnum
|
|
95
|
+
pendingEnum = []
|
|
96
|
+
}
|
|
97
|
+
const inline = raw.match(/--\s*(.+)$/)
|
|
98
|
+
if (inline) {
|
|
99
|
+
const vals = parseEnumComment(inline[1])
|
|
100
|
+
if (vals.length) col.enumValues = vals
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
cols.push(col)
|
|
104
|
+
}
|
|
105
|
+
return cols
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
function parseColumnLine(line: string): Column | null {
|
|
109
|
+
// strip trailing comma and inline comment for type parsing
|
|
110
|
+
const codeOnly = line.replace(/--.*$/, '').trim().replace(/,$/, '').trim()
|
|
111
|
+
if (!codeOnly) return null
|
|
112
|
+
|
|
113
|
+
// name [type] [constraints]
|
|
114
|
+
const parts = codeOnly.split(/\s+/)
|
|
115
|
+
if (parts.length < 2) return null
|
|
116
|
+
const name = parts[0]
|
|
117
|
+
if (!/^\w+$/.test(name)) return null
|
|
118
|
+
|
|
119
|
+
const rest = parts.slice(1).join(' ')
|
|
120
|
+
const sqlType = (rest.match(/^(TEXT|INTEGER|REAL|BLOB|NUMERIC)/i)?.[1] ?? 'TEXT').toUpperCase()
|
|
121
|
+
const notNull = /\bNOT NULL\b/i.test(rest)
|
|
122
|
+
const hasDefault = /\bDEFAULT\b/i.test(rest)
|
|
123
|
+
|
|
124
|
+
return { name, sqlType, notNull, hasDefault, enumValues: [] }
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
function parseEnumComment(text: string): string[] {
|
|
128
|
+
// Matches 'a'|'b'|'c' (with anything around it)
|
|
129
|
+
const matches = [...text.matchAll(/'([a-z0-9_-]+)'(?:\s*\|\s*'([a-z0-9_-]+)')+/gi)]
|
|
130
|
+
for (const m of matches) {
|
|
131
|
+
const raw = m[0]
|
|
132
|
+
const vals = [...raw.matchAll(/'([a-z0-9_-]+)'/gi)].map(x => x[1])
|
|
133
|
+
if (vals.length >= 2) return vals
|
|
134
|
+
}
|
|
135
|
+
return []
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
function splitTopLevelCommas(body: string): string[] {
|
|
139
|
+
const out: string[] = []
|
|
140
|
+
let buf = ''
|
|
141
|
+
let depth = 0
|
|
142
|
+
let inString = false
|
|
143
|
+
for (const ch of body) {
|
|
144
|
+
if (ch === "'" ) inString = !inString
|
|
145
|
+
if (!inString) {
|
|
146
|
+
if (ch === '(') depth++
|
|
147
|
+
else if (ch === ')') depth--
|
|
148
|
+
else if (ch === ',' && depth === 0) { out.push(buf); buf = ''; continue }
|
|
149
|
+
}
|
|
150
|
+
buf += ch
|
|
151
|
+
}
|
|
152
|
+
if (buf.trim()) out.push(buf)
|
|
153
|
+
return out
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
// ── Emitter ──────────────────────────────────────────────────────────────────
|
|
157
|
+
|
|
158
|
+
function sqlToTs(col: Column): string {
|
|
159
|
+
// Enum literal union takes priority over SQL type
|
|
160
|
+
if (col.enumValues.length) {
|
|
161
|
+
const union = col.enumValues.map(v => `'${v}'`).join(' | ')
|
|
162
|
+
return col.notNull || col.hasDefault ? union : `${union} | null`
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
let base: string
|
|
166
|
+
switch (col.sqlType) {
|
|
167
|
+
case 'INTEGER': base = 'number'; break
|
|
168
|
+
case 'REAL': base = 'number'; break
|
|
169
|
+
case 'TEXT': base = 'string'; break
|
|
170
|
+
case 'BLOB': base = 'Uint8Array'; break
|
|
171
|
+
case 'NUMERIC': base = 'number'; break
|
|
172
|
+
default: base = 'string'
|
|
173
|
+
}
|
|
174
|
+
// NOT NULL or has DEFAULT → reads are always defined
|
|
175
|
+
return col.notNull || col.hasDefault ? base : `${base} | null`
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
function pascalCase(snake: string): string {
|
|
179
|
+
return snake.split('_').map(p => p ? p[0].toUpperCase() + p.slice(1) : '').join('')
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
function emit(tables: Table[]): string {
|
|
183
|
+
const header = `// lib/schema.ts
|
|
184
|
+
// AUTO-GENERATED by scripts/generate-schema.ts — DO NOT EDIT.
|
|
185
|
+
// Source: businesskit-files/*.ts (copies of app-side schema).
|
|
186
|
+
// Regenerate: npm run schema:gen
|
|
187
|
+
//
|
|
188
|
+
// What this gives you:
|
|
189
|
+
// - One \`interface <PascalTable>\` per UserDB table
|
|
190
|
+
// - Enum literal unions for columns with \`-- 'a'|'b'|'c'\` comment hints
|
|
191
|
+
// - \`TABLES\` const for typo-safe table references
|
|
192
|
+
//
|
|
193
|
+
// Rows returned by libSQL's Client are \`Record<string, Value>\` — cast via
|
|
194
|
+
// \`as unknown as <Interface>\` at the call site after SELECT.
|
|
195
|
+
`
|
|
196
|
+
|
|
197
|
+
const groups = new Map<string, Table[]>()
|
|
198
|
+
for (const t of tables) {
|
|
199
|
+
const key = t.sourceFile
|
|
200
|
+
if (!groups.has(key)) groups.set(key, [])
|
|
201
|
+
groups.get(key)!.push(t)
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
const body = [...groups.entries()]
|
|
205
|
+
.map(([file, ts]) => {
|
|
206
|
+
const interfaces = ts.map(t => {
|
|
207
|
+
const fields = t.columns
|
|
208
|
+
.map(c => ` ${c.name}: ${sqlToTs(c)}`)
|
|
209
|
+
.join('\n')
|
|
210
|
+
return `export interface ${pascalCase(t.name)} {\n${fields}\n}`
|
|
211
|
+
}).join('\n\n')
|
|
212
|
+
return `// ── ${file} ${'─'.repeat(Math.max(0, 60 - file.length))}\n\n${interfaces}`
|
|
213
|
+
})
|
|
214
|
+
.join('\n\n')
|
|
215
|
+
|
|
216
|
+
const tableNames = tables.map(t => ` ${t.name}: '${t.name}' as const,`).join('\n')
|
|
217
|
+
const constBlock = `\nexport const TABLES = {\n${tableNames}\n}\n\nexport type TableName = keyof typeof TABLES\n`
|
|
218
|
+
|
|
219
|
+
return `${header}\n${body}\n${constBlock}`
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
// ── Main ─────────────────────────────────────────────────────────────────────
|
|
223
|
+
|
|
224
|
+
function main() {
|
|
225
|
+
const files = readdirSync(SRC_DIR).filter(f => f.endsWith('.ts') && !f.endsWith('.md')).sort()
|
|
226
|
+
if (!files.length) {
|
|
227
|
+
console.error(`No schema files found in ${SRC_DIR}`)
|
|
228
|
+
process.exit(1)
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
const all: Table[] = []
|
|
232
|
+
for (const f of files) {
|
|
233
|
+
const src = readFileSync(join(SRC_DIR, f), 'utf8')
|
|
234
|
+
const tables = extractCreateTables(src, f)
|
|
235
|
+
all.push(...tables)
|
|
236
|
+
console.log(` ${f}: ${tables.length} tables`)
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
const out = emit(all)
|
|
240
|
+
writeFileSync(OUT, out, 'utf8')
|
|
241
|
+
console.log(`\nWrote ${OUT} — ${all.length} tables, ${all.reduce((a, t) => a + t.columns.length, 0)} columns.`)
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
main()
|
package/setup.ts
CHANGED
|
@@ -71,15 +71,29 @@ try {
|
|
|
71
71
|
|
|
72
72
|
// ── Step 3: Profile exists ────────────────────────────────────────────────────
|
|
73
73
|
console.log('\n3. Loading your profile...')
|
|
74
|
+
const PROFILE_ID = env.PROFILE_ID || process.env.PROFILE_ID || ''
|
|
74
75
|
try {
|
|
75
|
-
const { rows } = await db.execute('SELECT id, slug, title FROM profiles
|
|
76
|
+
const { rows } = await db.execute('SELECT id, slug, title FROM profiles')
|
|
76
77
|
if (!rows.length) {
|
|
77
78
|
err('No profile found in your database')
|
|
78
79
|
dim('Complete BusinessKit onboarding at businesskit.io first')
|
|
79
80
|
dim('Then come back and run: npm run setup')
|
|
80
81
|
process.exit(1)
|
|
81
82
|
}
|
|
82
|
-
|
|
83
|
+
if (rows.length > 1 && !PROFILE_ID) {
|
|
84
|
+
err(`Found ${rows.length} profiles — set PROFILE_ID in .env to pick one:`)
|
|
85
|
+
for (const r of rows) dim(` ${r.id} (${r.title} @${r.slug})`)
|
|
86
|
+
process.exit(1)
|
|
87
|
+
}
|
|
88
|
+
const p = PROFILE_ID
|
|
89
|
+
? rows.find(r => r.id === PROFILE_ID)
|
|
90
|
+
: rows[0]
|
|
91
|
+
if (!p) {
|
|
92
|
+
err(`PROFILE_ID=${PROFILE_ID} not found in your database`)
|
|
93
|
+
dim('Available:')
|
|
94
|
+
for (const r of rows) dim(` ${r.id} (${r.title} @${r.slug})`)
|
|
95
|
+
process.exit(1)
|
|
96
|
+
}
|
|
83
97
|
ok(`Profile: ${p.title} (@${p.slug})`)
|
|
84
98
|
dim(`Profile ID: ${p.id}`)
|
|
85
99
|
} catch (e: any) {
|
package/skills/schema/SKILL.md
CHANGED
|
@@ -1,29 +1,37 @@
|
|
|
1
1
|
# Schema — Table Reference
|
|
2
2
|
|
|
3
|
-
## Content
|
|
3
|
+
## Content — ONE unified table
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
All user-facing content (blog, notes, guides, newsletter, compare, alternative, prompt, skills) lives in a single `content` table. The "type" is encoded via `cms_id` → `cms.category_id`:
|
|
6
|
+
|
|
7
|
+
| Kind | Route | cms.category_id |
|
|
6
8
|
|---|---|---|
|
|
7
|
-
|
|
|
8
|
-
|
|
|
9
|
-
|
|
|
10
|
-
|
|
|
11
|
-
|
|
|
12
|
-
|
|
|
13
|
-
|
|
|
14
|
-
|
|
|
9
|
+
| blog | /blog | cat_35 |
|
|
10
|
+
| notes | /notes | cat_37 |
|
|
11
|
+
| guides | /guides | cat_36 |
|
|
12
|
+
| newsletter | /n | cat_20 |
|
|
13
|
+
| compare | /compare | cat_40 |
|
|
14
|
+
| alternative | /alternative | cat_41 |
|
|
15
|
+
| prompt | /prompt | cat_39 |
|
|
16
|
+
| skills | /skills | cat_38 |
|
|
17
|
+
|
|
18
|
+
`content` shared cols: id, cms_id, category_id, profile_id, user_id, slug, title, content, excerpt, published, hidden, collection_id, seo_title (≤60), seo_description (≤160), seo_keywords, word_count, reading_time_mins, internal_links (int), sources JSON, created_at, updated_at.
|
|
19
|
+
|
|
20
|
+
Use `getCmsId(profileId, kind)` from `agents/content/content.ts` to resolve cms_id. Writes are unique per `(profile_id, category_id, slug)`.
|
|
15
21
|
|
|
16
|
-
|
|
22
|
+
## Docs — separate, AUTOINCREMENT ids
|
|
23
|
+
`doc_collections` + `doc_articles` — knowledge base. `id INTEGER PRIMARY KEY AUTOINCREMENT`, so omit id on INSERT.
|
|
17
24
|
|
|
18
25
|
## Products
|
|
19
26
|
type: download|course|meeting|webinar|event|listing|sponsorship|service
|
|
20
27
|
cols: price_cents, sale_price_cents, slug UNIQUE, published, hidden, lessons JSON, calendar_link, webinar_link, file_url
|
|
21
28
|
|
|
22
29
|
## CRM (crm.ts)
|
|
23
|
-
crm_contacts: lead_score 0-100, icp_match (strong|moderate|weak|unknown), outreach_status, agent_status (pending→researching→enriched→outreach_ready→closed), agent_context JSON, auto_approve INT, suggested_dm, email_draft
|
|
24
|
-
crm_activities: type, direction (inbound|outbound), sender (agent|you|contact), body, approval_status (
|
|
25
|
-
crm_deals: contact_id, title, value_cents, stage (new|contacted|proposal|negotiation|won|lost)
|
|
30
|
+
crm_contacts: status (lead|prospect|customer|churned|archived), lead_score 0-100, icp_match (strong|moderate|weak|unknown), outreach_status, agent_status (pending→researching→enriched→outreach_ready→closed), agent_context JSON, auto_approve INT, suggested_dm, email_draft
|
|
31
|
+
crm_activities: type, direction (inbound|outbound), sender (agent|you|contact), body, approval_status (pending_approval|approved|auto_sent|rejected) — APPEND-ONLY, never UPDATE
|
|
32
|
+
crm_deals: contact_id, title, value_cents, stage (new|contacted|proposal|negotiation|won|lost), probability 0-100
|
|
26
33
|
crm_analytics: DB triggers keep this in sync — never update manually
|
|
34
|
+
crm_groups: membership via crm_contact_groups junction — use lib/groups.ts, never LIKE '%grp_x%'
|
|
27
35
|
|
|
28
36
|
## Social (social.ts)
|
|
29
37
|
social_accounts: platform, connection_mode (zernio_byok|zernio_platform|direct|n8n), zernio_account_id
|
|
@@ -36,10 +44,43 @@ memory_log: rolling 20 rows per profile — auto-trimmed
|
|
|
36
44
|
agent_skills: UNIQUE(profile_id,slug) — upsert safe
|
|
37
45
|
|
|
38
46
|
## Analytics — READ ONLY, never write
|
|
39
|
-
|
|
40
|
-
|
|
47
|
+
Maintained by DB triggers (scalar counters) + app-side lazy aggregation (JSON breakdowns). JSON rebuilt on dashboard page visit with `last_aggregated_at` throttle — agents MUST NOT rebuild. If stale, surface a note to the user.
|
|
48
|
+
|
|
49
|
+
- profile_analytics: total_clicks, total_views, bot_views, analytics_7d/30d/12m/lifetime JSON, revenue_* JSON
|
|
50
|
+
- content_analytics (1/row): bot_views, total_views, total_reactions, total_comments, views_7d/30d/12m/lifetime, *_breakdown JSON
|
|
51
|
+
- cms_analytics (1/cms-page): total_posts, total_draft, total_published, total_views, … (maintained by trg_content_insert/update/delete + trg_content_view_insert)
|
|
52
|
+
- doc_analytics (1/doc): views, sad/neutral/happy votes, *_breakdown
|
|
53
|
+
- product_analytics: total_sales, total_revenue_cents, sales_30d JSON
|
|
54
|
+
- crm_analytics (1/profile): scalar counters by 13 triggers; JSON breakdowns aggregated on CRM dashboard visit
|
|
41
55
|
|
|
42
56
|
## ID and timestamp rules
|
|
43
57
|
IDs: ulid() for most | omit id for doc_collections, doc_articles (AUTOINCREMENT)
|
|
44
58
|
INT cols: now() = Math.floor(Date.now()/1000)
|
|
45
59
|
TEXT cols (posts, jobs, forms, docs): iso() = new Date().toISOString().slice(0,19)+'Z'
|
|
60
|
+
|
|
61
|
+
## Status enums — source of truth: lib/enums.ts (never literal)
|
|
62
|
+
|
|
63
|
+
| Column | Values |
|
|
64
|
+
|---|---|
|
|
65
|
+
| crm_contacts.status | lead, prospect, customer, churned, archived |
|
|
66
|
+
| crm_activities.approval_status | pending_approval, approved, rejected, auto_sent |
|
|
67
|
+
| crm_deals.stage | new, contacted, proposal, negotiation, won, lost |
|
|
68
|
+
| social_posts.status | draft, scheduled, queued, publishing, published, failed, cancelled |
|
|
69
|
+
| agent_notes.status | inbox, processing, done, archived |
|
|
70
|
+
| agent_tasks.status | pending, active, running, done, paused, failed, cancelled |
|
|
71
|
+
| agent_kb.entry_type | entity, concept, source, synthesis, index |
|
|
72
|
+
| community_posts.status | published, removed, pending |
|
|
73
|
+
| email_tracking_events.type | delivered, opened, clicked, bounced, complained, failed |
|
|
74
|
+
| affiliate_commissions.status | pending, approved, paid, void |
|
|
75
|
+
|
|
76
|
+
## Numeric bounds — use lib/validate.ts helpers
|
|
77
|
+
lead_score, probability: 0-100 integer
|
|
78
|
+
*_cents: non-negative integer
|
|
79
|
+
hot_score: non-negative
|
|
80
|
+
|
|
81
|
+
## Append-only — never DELETE
|
|
82
|
+
crm_activities (DB blocks DELETE), agent_reports, email_events, clicks_analytics
|
|
83
|
+
crm_activities UPDATEs are allowed only on: approval_status (pending_approval→approved|rejected|auto_sent) and read_at. Never change body/type/direction — insert a correcting row instead.
|
|
84
|
+
|
|
85
|
+
## Soft-delete only — never hard DELETE
|
|
86
|
+
hidden=1 | archived=1 | status='archived'
|