@businesskit/agent 1.0.4 → 1.0.7
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-plugin/plugin.json +1 -1
- package/.cursor-plugin/plugin.json +25 -0
- package/.cursorrules +10 -0
- package/.superconsole-plugin/plugin.json +62 -0
- package/AGENTS.md +105 -63
- package/CLAUDE.md +11 -84
- package/GEMINI.md +11 -34
- package/README.md +95 -154
- package/assets/logo.svg +3 -0
- package/bin/businesskit.js +45 -18
- package/commands/add-contact.md +47 -0
- package/commands/add-inventory-from-invoice.md +80 -0
- package/commands/adjust-stock.md +46 -0
- package/commands/brief.md +12 -0
- package/commands/capabilities.md +33 -0
- package/commands/chase.md +12 -0
- package/commands/check-stock.md +43 -0
- package/commands/create-component.md +109 -0
- package/commands/create-invoice.md +69 -0
- package/commands/create-new-bill.md +65 -0
- package/commands/create-page.md +124 -0
- package/commands/crm.md +13 -0
- package/commands/followups.md +13 -0
- package/commands/list-components.md +41 -0
- package/commands/list-content.md +46 -0
- package/commands/list-pages.md +45 -0
- package/commands/pipeline.md +13 -0
- package/commands/publish-page.md +64 -0
- package/commands/send-invoice.md +42 -0
- package/commands/seo-audit.md +36 -0
- package/commands/update-agent.md +33 -0
- package/commands/update-contact.md +46 -0
- package/commands/update-content.md +48 -0
- package/commands/update-page.md +56 -0
- package/commands/update-price.md +47 -0
- package/commands/write-blog.md +56 -0
- package/commands/write-docs.md +90 -0
- package/commands/write-guide.md +51 -0
- package/commands/write-newsletter.md +50 -0
- package/dist/server.js +4099 -0
- package/mcp_config.json +12 -0
- package/package.json +54 -25
- package/plugin.json +32 -0
- package/scripts/setup.sh +169 -0
- package/scripts/setup.ts +129 -0
- package/scripts/update.sh +56 -0
- package/server.json +21 -0
- package/skills/README.md +20 -27
- package/skills/billing/SKILL.md +64 -0
- package/skills/brand/SKILL.md +30 -86
- package/skills/cms/SKILL.md +111 -0
- package/skills/cms/references/cms-hubs.md +84 -0
- package/skills/cms/references/html-blocks.md +177 -0
- package/skills/copywriting/SKILL.md +1 -1
- package/skills/crm/SKILL.md +24 -0
- package/skills/docs/SKILL.md +65 -0
- package/skills/docs/references/how-to.md +155 -0
- package/skills/inbox/SKILL.md +49 -0
- package/skills/inventory/SKILL.md +69 -0
- package/skills/pages/SKILL.md +214 -0
- package/skills/pages/references/sections-catalog.json +1183 -0
- package/skills/social/SKILL.md +84 -211
- package/skills/social/evals/evals.json +66 -0
- package/skills/social/evals/rubric.md +26 -0
- package/skills/social/examples/repurposing-blog-to-social.md +93 -0
- package/skills/social/examples/weekly-content-calendar.md +30 -0
- package/skills/social/linkedin/SKILL.md +107 -0
- package/skills/social/linkedin/evals/evals.json +106 -0
- package/skills/social/linkedin/evals/rubric.md +28 -0
- package/skills/social/linkedin/examples/gasp-weekly-sprint.md +24 -0
- package/skills/social/linkedin/examples/growth-posts-by-format.md +140 -0
- package/skills/social/linkedin/references/gasp-funnel-and-calibration.md +90 -0
- package/skills/social/linkedin/references/six-growth-formats.md +114 -0
- package/skills/social/references/platforms.md +76 -0
- package/skills/social/references/post-templates.md +105 -0
- package/skills/social/references/reverse-engineering.md +62 -0
- package/src/data/sections-catalog.json +950 -0
- package/src/db.ts +170 -0
- package/src/server.ts +75 -0
- package/src/tools/content.ts +326 -0
- package/src/tools/crm.ts +1416 -0
- package/src/tools/domains.ts +89 -0
- package/src/tools/inbox.ts +270 -0
- package/src/tools/index.ts +116 -0
- package/src/tools/pages.ts +1217 -0
- package/src/tools/shop.ts +771 -0
- package/src/tools/system.ts +94 -0
- package/src/tools/types.ts +17 -0
- package/templates/antigravity.json +8 -0
- package/templates/claude-code.mcp.json +8 -0
- package/templates/cursor.mcp.json +8 -0
- package/templates/vscode.mcp.json +9 -0
- package/.claude/commands/analytics.md +0 -7
- package/.claude/commands/blog-writer.md +0 -9
- package/.claude/commands/cbo.md +0 -7
- package/.claude/commands/ceo.md +0 -10
- package/.claude/commands/cmo.md +0 -7
- package/.claude/commands/compress.md +0 -73
- package/.claude/commands/coo.md +0 -11
- package/.claude/commands/copywriter.md +0 -15
- package/.claude/commands/course-creator.md +0 -9
- package/.claude/commands/crm.md +0 -591
- package/.claude/commands/deep.md +0 -50
- package/.claude/commands/docs-writer.md +0 -8
- package/.claude/commands/forms-builder.md +0 -10
- package/.claude/commands/ingest.md +0 -110
- package/.claude/commands/jobs-manager.md +0 -8
- package/.claude/commands/kb.md +0 -107
- package/.claude/commands/newsletter-writer.md +0 -9
- package/.claude/commands/preserve.md +0 -64
- package/.claude/commands/resume.md +0 -67
- package/.claude/commands/scheduler.md +0 -8
- package/.claude/commands/seo.md +0 -60
- package/.claude/commands/social.md +0 -51
- package/.claude/commands/store-manager.md +0 -11
- package/.claude/settings.json +0 -16
- package/.claude/settings.local.json +0 -11
- package/.env.example +0 -12
- package/.mcp.json +0 -3
- package/HEARTBEAT.md +0 -105
- package/PLAN.md +0 -345
- package/PRD-Agent-Composition.md +0 -149
- package/PRD-Agent-Harness.md +0 -316
- package/REFERENCE.md +0 -160
- package/SOUL.md +0 -148
- package/SYSTEM.md +0 -71
- package/agents/_base.ts +0 -371
- package/agents/analytics/SOUL.md +0 -11
- package/agents/analytics/analytics.ts +0 -405
- package/agents/business/SOUL.md +0 -11
- package/agents/business/business.ts +0 -109
- package/agents/ceo/SOUL.md +0 -15
- package/agents/ceo/ceo.ts +0 -146
- package/agents/content/SOUL.md +0 -11
- package/agents/content/content.ts +0 -379
- package/agents/copywriting/SOUL.md +0 -11
- package/agents/copywriting/copywriting.ts +0 -89
- package/agents/courses/SOUL.md +0 -11
- package/agents/courses/courses.ts +0 -451
- package/agents/docs/SOUL.md +0 -11
- package/agents/docs/docs.ts +0 -77
- package/agents/forms/SOUL.md +0 -11
- package/agents/forms/forms.ts +0 -364
- package/agents/hiring/SOUL.md +0 -11
- package/agents/hiring/hiring.ts +0 -314
- package/agents/marketing/SOUL.md +0 -15
- package/agents/marketing/marketing.ts +0 -109
- package/agents/newsletter/SOUL.md +0 -11
- package/agents/newsletter/newsletter.ts +0 -91
- package/agents/operations/SOUL.md +0 -11
- package/agents/operations/operations.ts +0 -157
- package/agents/sales/SOUL.md +0 -44
- package/agents/sales/sales.ts +0 -883
- package/agents/scheduler/SOUL.md +0 -11
- package/agents/scheduler/scheduler.ts +0 -68
- package/agents/seo/SOUL.md +0 -11
- package/agents/seo/seo.ts +0 -288
- package/agents/social/SOUL.md +0 -11
- package/agents/social/social.ts +0 -744
- package/agents/store/SOUL.md +0 -11
- package/agents/store/store.ts +0 -514
- package/cli.ts +0 -144
- package/context/about-me.md +0 -96
- package/context/brand-voice.md +0 -127
- package/context/brand.md +0 -80
- package/context/business.md +0 -66
- package/context/working-style.md +0 -87
- package/lib/analytics.ts +0 -81
- package/lib/cms.ts +0 -88
- package/lib/db.adapter.ts +0 -30
- package/lib/db.ts +0 -63
- package/lib/enums.ts +0 -25
- package/lib/groups.ts +0 -122
- package/lib/id.ts +0 -43
- package/lib/kb.ts +0 -337
- package/lib/memory.ts +0 -219
- package/lib/pattern-detector.ts +0 -169
- package/lib/profile.ts +0 -155
- package/lib/schema.ts +0 -2015
- package/lib/slug.ts +0 -30
- package/lib/validate.ts +0 -33
- package/setup.ts +0 -141
- package/skills/agents/SKILL.md +0 -56
- package/skills/analytics/SKILL.md +0 -52
- package/skills/blog-writer/SKILL.md +0 -452
- package/skills/ceo/SKILL.md +0 -25
- package/skills/cmo/SKILL.md +0 -8
- package/skills/schema/SKILL.md +0 -86
- package/skills/store/SKILL.md +0 -62
- package/tsconfig.json +0 -14
package/lib/db.ts
DELETED
|
@@ -1,63 +0,0 @@
|
|
|
1
|
-
// lib/db.ts
|
|
2
|
-
// Turso DB client — retry on SQLITE_BUSY, optional read replica.
|
|
3
|
-
// Phase 1: .env credentials. Phase 2: swap via db.adapter.ts (one line).
|
|
4
|
-
|
|
5
|
-
import { createClient, type Client } from '@libsql/client/web'
|
|
6
|
-
|
|
7
|
-
// Permissive statement type — the libSQL `InStatement` is strict about arg
|
|
8
|
-
// element types (`InValue`), but the agent layer uses `unknown[]` everywhere
|
|
9
|
-
// since we bind ids, ISO strings, integers, JSON blobs, and nulls freely.
|
|
10
|
-
// The runtime client accepts all of these; we forward them as `any`.
|
|
11
|
-
export type Stmt = { sql: string; args: unknown[] } | string
|
|
12
|
-
|
|
13
|
-
let _writer: Client | null = null
|
|
14
|
-
let _reader: Client | null = null
|
|
15
|
-
|
|
16
|
-
function writer(): Client {
|
|
17
|
-
if (!_writer) {
|
|
18
|
-
const url = process.env.TURSO_URL || ''
|
|
19
|
-
const token = process.env.TURSO_TOKEN || ''
|
|
20
|
-
if (!url || !token) throw new Error('TURSO_URL and TURSO_TOKEN required. Run: cp .env.example .env')
|
|
21
|
-
_writer = createClient({ url, authToken: token })
|
|
22
|
-
}
|
|
23
|
-
return _writer
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
function reader(): Client {
|
|
27
|
-
if (!_reader) {
|
|
28
|
-
const url = process.env.TURSO_REPLICA_URL || process.env.TURSO_URL || ''
|
|
29
|
-
const token = process.env.TURSO_TOKEN || ''
|
|
30
|
-
_reader = createClient({ url, authToken: token })
|
|
31
|
-
}
|
|
32
|
-
return _reader
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
// P0-3: SQLITE_BUSY retry — exponential backoff + jitter
|
|
36
|
-
export async function withRetry<T>(op: () => Promise<T>, maxTries = 5): Promise<T> {
|
|
37
|
-
for (let i = 0; i < maxTries; i++) {
|
|
38
|
-
try { return await op() }
|
|
39
|
-
catch (e: any) {
|
|
40
|
-
const retryable = /SQLITE_BUSY|database is locked|SQLITE_LOCKED/i.test(e?.message ?? '')
|
|
41
|
-
if (!retryable || i === maxTries - 1) throw e
|
|
42
|
-
await new Promise(r => setTimeout(r, 50 * 2 ** i + Math.random() * 50))
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
throw new Error('unreachable')
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
export const db = {
|
|
49
|
-
// Reads — replica if TURSO_REPLICA_URL set, otherwise primary
|
|
50
|
-
execute: (q: Stmt) =>
|
|
51
|
-
withRetry(() => reader().execute(q as any)),
|
|
52
|
-
|
|
53
|
-
// Writes — always primary
|
|
54
|
-
write: (q: Stmt) =>
|
|
55
|
-
withRetry(() => writer().execute(q as any)),
|
|
56
|
-
|
|
57
|
-
// P0-2: Atomic multi-statement batch — back-compat alias `batch` too
|
|
58
|
-
txn: (stmts: Stmt[]) =>
|
|
59
|
-
withRetry(() => writer().batch(stmts as any, 'write')),
|
|
60
|
-
|
|
61
|
-
batch: (stmts: Stmt[]) =>
|
|
62
|
-
withRetry(() => writer().batch(stmts as any, 'write')),
|
|
63
|
-
}
|
package/lib/enums.ts
DELETED
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
// lib/enums.ts
|
|
2
|
-
// Single source of truth for all status strings used in DB writes.
|
|
3
|
-
// Never use raw string literals for statuses in agent files.
|
|
4
|
-
// Keep in sync with app repo's src/lib/enums.ts
|
|
5
|
-
|
|
6
|
-
export const CRM_CONTACT_STATUS = ['lead', 'prospect', 'customer', 'churned', 'archived'] as const
|
|
7
|
-
export const CRM_ACTIVITY_APPROVAL = ['pending_approval', 'approved', 'rejected', 'auto_sent'] as const
|
|
8
|
-
export const CRM_DEAL_STAGE = ['new', 'contacted', 'proposal', 'negotiation', 'won', 'lost'] as const
|
|
9
|
-
export const SOCIAL_POST_STATUS = ['draft', 'scheduled', 'queued', 'publishing', 'published', 'failed', 'cancelled'] as const
|
|
10
|
-
export const AGENT_NOTE_STATUS = ['inbox', 'processing', 'done', 'archived'] as const
|
|
11
|
-
export const AGENT_TASK_STATUS = ['pending', 'active', 'running', 'done', 'paused', 'failed', 'cancelled'] as const
|
|
12
|
-
export const AGENT_KB_ENTRY_TYPE = ['entity', 'concept', 'source', 'synthesis', 'index'] as const
|
|
13
|
-
export const APPROVAL_STATUS = ['pending_approval', 'approved', 'rejected', 'auto_sent'] as const
|
|
14
|
-
export const COMMUNITY_POST_STATUS = ['published', 'removed', 'pending'] as const
|
|
15
|
-
export const EMAIL_EVENT_TYPE = ['delivered', 'opened', 'clicked', 'bounced', 'complained', 'failed'] as const
|
|
16
|
-
export const AFFILIATE_COMMISSION = ['pending', 'approved', 'paid', 'void'] as const
|
|
17
|
-
|
|
18
|
-
export type CRMContactStatus = typeof CRM_CONTACT_STATUS[number]
|
|
19
|
-
export type CRMActivityApproval = typeof CRM_ACTIVITY_APPROVAL[number]
|
|
20
|
-
export type CRMDealStage = typeof CRM_DEAL_STAGE[number]
|
|
21
|
-
export type SocialPostStatus = typeof SOCIAL_POST_STATUS[number]
|
|
22
|
-
export type AgentNoteStatus = typeof AGENT_NOTE_STATUS[number]
|
|
23
|
-
export type AgentTaskStatus = typeof AGENT_TASK_STATUS[number]
|
|
24
|
-
export type AgentKBEntryType = typeof AGENT_KB_ENTRY_TYPE[number]
|
|
25
|
-
export type ApprovalStatus = typeof APPROVAL_STATUS[number]
|
package/lib/groups.ts
DELETED
|
@@ -1,122 +0,0 @@
|
|
|
1
|
-
// lib/groups.ts — P0-4: CRM groups via junction table
|
|
2
|
-
// Replaces LIKE '%grp_x%' which is O(N), un-indexable, and has
|
|
3
|
-
// substring collision bug (grp_0055 matches grp_005).
|
|
4
|
-
//
|
|
5
|
-
// Requires: crm_contact_groups junction table (app-side migration).
|
|
6
|
-
// Gate on schema version before switching — see P1-1 in task spec.
|
|
7
|
-
//
|
|
8
|
-
// Old (delete everywhere):
|
|
9
|
-
// WHERE groups LIKE '%grp_005%'
|
|
10
|
-
// UPDATE crm_contacts SET groups = json_insert(groups, '$[#]', 'grp_005') WHERE id = ?
|
|
11
|
-
//
|
|
12
|
-
// New (use these helpers):
|
|
13
|
-
|
|
14
|
-
import { db } from './db.ts'
|
|
15
|
-
import { iso } from './id.ts'
|
|
16
|
-
|
|
17
|
-
/**
|
|
18
|
-
* Get contacts in a group — indexed, no substring collision.
|
|
19
|
-
*/
|
|
20
|
-
export async function getContactsByGroup(
|
|
21
|
-
profileId: string,
|
|
22
|
-
groupId: string,
|
|
23
|
-
limit = 50,
|
|
24
|
-
offset = 0,
|
|
25
|
-
): Promise<unknown[]> {
|
|
26
|
-
const { rows } = await db.execute({
|
|
27
|
-
sql: `SELECT c.*
|
|
28
|
-
FROM crm_contacts c
|
|
29
|
-
JOIN crm_contact_groups ccg ON ccg.contact_id = c.id
|
|
30
|
-
WHERE ccg.group_id = ?
|
|
31
|
-
AND c.profile_id = ?
|
|
32
|
-
AND c.archived = 0
|
|
33
|
-
ORDER BY c.last_activity_at DESC
|
|
34
|
-
LIMIT ? OFFSET ?`,
|
|
35
|
-
args: [groupId, profileId, limit, offset],
|
|
36
|
-
})
|
|
37
|
-
return rows
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
/**
|
|
41
|
-
* Add contact to group — idempotent (INSERT OR IGNORE).
|
|
42
|
-
* Also increments contact_count on crm_groups.
|
|
43
|
-
*/
|
|
44
|
-
export async function addContactToGroup(
|
|
45
|
-
profileId: string,
|
|
46
|
-
contactId: string,
|
|
47
|
-
groupId: string,
|
|
48
|
-
): Promise<void> {
|
|
49
|
-
await db.txn([
|
|
50
|
-
{
|
|
51
|
-
sql: `INSERT OR IGNORE INTO crm_contact_groups
|
|
52
|
-
(contact_id, group_id, profile_id, created_at)
|
|
53
|
-
VALUES (?, ?, ?, ?)`,
|
|
54
|
-
args: [contactId, groupId, profileId, iso()],
|
|
55
|
-
},
|
|
56
|
-
{
|
|
57
|
-
// Only increment if the row was actually new
|
|
58
|
-
sql: `UPDATE crm_groups
|
|
59
|
-
SET contact_count = (
|
|
60
|
-
SELECT COUNT(*) FROM crm_contact_groups WHERE group_id = ?
|
|
61
|
-
)
|
|
62
|
-
WHERE id = ? AND profile_id = ?`,
|
|
63
|
-
args: [groupId, groupId, profileId],
|
|
64
|
-
},
|
|
65
|
-
])
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
/**
|
|
69
|
-
* Remove contact from group.
|
|
70
|
-
*/
|
|
71
|
-
export async function removeContactFromGroup(
|
|
72
|
-
profileId: string,
|
|
73
|
-
contactId: string,
|
|
74
|
-
groupId: string,
|
|
75
|
-
): Promise<void> {
|
|
76
|
-
await db.txn([
|
|
77
|
-
{
|
|
78
|
-
sql: `DELETE FROM crm_contact_groups
|
|
79
|
-
WHERE contact_id = ? AND group_id = ?`,
|
|
80
|
-
args: [contactId, groupId],
|
|
81
|
-
},
|
|
82
|
-
{
|
|
83
|
-
sql: `UPDATE crm_groups
|
|
84
|
-
SET contact_count = MAX(0, (
|
|
85
|
-
SELECT COUNT(*) FROM crm_contact_groups WHERE group_id = ?
|
|
86
|
-
))
|
|
87
|
-
WHERE id = ? AND profile_id = ?`,
|
|
88
|
-
args: [groupId, groupId, profileId],
|
|
89
|
-
},
|
|
90
|
-
])
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
/**
|
|
94
|
-
* Get all groups for a contact.
|
|
95
|
-
*/
|
|
96
|
-
export async function getGroupsForContact(
|
|
97
|
-
profileId: string,
|
|
98
|
-
contactId: string,
|
|
99
|
-
): Promise<unknown[]> {
|
|
100
|
-
const { rows } = await db.execute({
|
|
101
|
-
sql: `SELECT g.*
|
|
102
|
-
FROM crm_groups g
|
|
103
|
-
JOIN crm_contact_groups ccg ON ccg.group_id = g.id
|
|
104
|
-
WHERE ccg.contact_id = ?
|
|
105
|
-
AND g.profile_id = ?`,
|
|
106
|
-
args: [contactId, profileId],
|
|
107
|
-
})
|
|
108
|
-
return rows
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
/**
|
|
112
|
-
* Schema-version check — gate P0-4 on this.
|
|
113
|
-
* Returns true when crm_contact_groups table exists in UserDB.
|
|
114
|
-
*/
|
|
115
|
-
export async function hasGroupsJunction(): Promise<boolean> {
|
|
116
|
-
try {
|
|
117
|
-
await db.execute(`SELECT 1 FROM crm_contact_groups LIMIT 0`)
|
|
118
|
-
return true
|
|
119
|
-
} catch {
|
|
120
|
-
return false
|
|
121
|
-
}
|
|
122
|
-
}
|
package/lib/id.ts
DELETED
|
@@ -1,43 +0,0 @@
|
|
|
1
|
-
// lib/id.ts — ID generation and idempotency helpers
|
|
2
|
-
|
|
3
|
-
/** UUID v4 for row IDs */
|
|
4
|
-
export function ulid(): string {
|
|
5
|
-
return crypto.randomUUID()
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
/** ISO timestamp for TEXT columns */
|
|
9
|
-
export function iso(): string {
|
|
10
|
-
return new Date().toISOString().replace(/\.\d{3}Z$/, 'Z')
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
/** Unix epoch for INTEGER columns */
|
|
14
|
-
export function now(): number {
|
|
15
|
-
return Math.floor(Date.now() / 1000)
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
/**
|
|
19
|
-
* P0-1: Idempotency key — UUID v7 style, timestamp-prefixed.
|
|
20
|
-
* One key per action per command invocation.
|
|
21
|
-
* Reuse the SAME key on retry within the same invocation.
|
|
22
|
-
*
|
|
23
|
-
* Usage:
|
|
24
|
-
* const key = generateIdempotencyKey()
|
|
25
|
-
* await db.txn([
|
|
26
|
-
* { sql: 'INSERT OR IGNORE INTO crm_activities (..., idempotency_key) VALUES (..., ?)', args: [..., key] },
|
|
27
|
-
* { sql: 'INSERT INTO agent_audit (..., idempotency_key) VALUES (..., ?)', args: [..., key] },
|
|
28
|
-
* ])
|
|
29
|
-
*
|
|
30
|
-
* Tables that gain idempotency_key column (coordinate with app team as columns land):
|
|
31
|
-
* crm_activities, crm_tasks, crm_notes, crm_proposals, crm_invoices
|
|
32
|
-
* social_posts, social_queue
|
|
33
|
-
* agent_notes, agent_reports, agent_tasks, agent_memory
|
|
34
|
-
*
|
|
35
|
-
* Until columns land: store key in metadata JSON blob where available.
|
|
36
|
-
*/
|
|
37
|
-
export function generateIdempotencyKey(): string {
|
|
38
|
-
const ts = Date.now()
|
|
39
|
-
const rand = crypto.getRandomValues(new Uint8Array(10))
|
|
40
|
-
const tsHex = ts.toString(16).padStart(12, '0')
|
|
41
|
-
const randHex = Array.from(rand).map(b => b.toString(16).padStart(2, '0')).join('')
|
|
42
|
-
return `${tsHex.slice(0, 8)}-${tsHex.slice(8, 12)}-7${randHex.slice(0, 3)}-${randHex.slice(3, 7)}-${randHex.slice(7, 19)}`
|
|
43
|
-
}
|
package/lib/kb.ts
DELETED
|
@@ -1,337 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* kb.ts — Karpathy LLM Wiki for BusinessKit agents
|
|
3
|
-
*
|
|
4
|
-
* The agent_kb table is a persistent, compounding knowledge base.
|
|
5
|
-
* Agents maintain it. Users only read it.
|
|
6
|
-
*
|
|
7
|
-
* Three-layer architecture (Karpathy):
|
|
8
|
-
* agent_notes → raw sources (user drops anything here)
|
|
9
|
-
* agent_kb → compiled wiki (agent integrates, cross-references, updates)
|
|
10
|
-
* agent_skills → schema (tells agent how the KB is structured)
|
|
11
|
-
*
|
|
12
|
-
* How it works:
|
|
13
|
-
* 1. User drops notes into agent_notes (links, ideas, articles, screenshots)
|
|
14
|
-
* 2. Agent runs processInboxNotes() periodically
|
|
15
|
-
* 3. Agent reads each note, decides: new KB entry OR update existing one
|
|
16
|
-
* 4. If new source contradicts existing entry → marks stale=1
|
|
17
|
-
* 5. One new source can update 10-15 existing KB pages (Karpathy's observation)
|
|
18
|
-
* 6. Answers to user questions get filed back as KB entries (compounding)
|
|
19
|
-
*
|
|
20
|
-
* Index pattern (token-efficient):
|
|
21
|
-
* Agent reads slug+summary only (getIndex) → cheap scan
|
|
22
|
-
* Drills into content only for relevant entries (getEntry)
|
|
23
|
-
* Never loads all content at once
|
|
24
|
-
*/
|
|
25
|
-
|
|
26
|
-
import { db } from './db.ts'
|
|
27
|
-
import { getProfile } from './profile.ts'
|
|
28
|
-
import { ulid, iso } from './id.ts'
|
|
29
|
-
|
|
30
|
-
export type KBEntryType = 'entity' | 'concept' | 'source' | 'synthesis' | 'index'
|
|
31
|
-
|
|
32
|
-
export interface KBEntry {
|
|
33
|
-
id: string
|
|
34
|
-
profile_id: string
|
|
35
|
-
entry_type: KBEntryType
|
|
36
|
-
title: string
|
|
37
|
-
slug: string
|
|
38
|
-
content: string
|
|
39
|
-
summary: string | null
|
|
40
|
-
related_ids: string[]
|
|
41
|
-
source_note_ids: string[]
|
|
42
|
-
tags: string[]
|
|
43
|
-
stale: number
|
|
44
|
-
version: number
|
|
45
|
-
updated_at: string
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
export interface KBIndexEntry {
|
|
49
|
-
id: string
|
|
50
|
-
slug: string
|
|
51
|
-
title: string
|
|
52
|
-
summary: string | null
|
|
53
|
-
entry_type: KBEntryType
|
|
54
|
-
stale: number
|
|
55
|
-
updated_at: string
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
// ─────────────────────────────────────────────────────────────────────────────
|
|
59
|
-
// Read
|
|
60
|
-
// ─────────────────────────────────────────────────────────────────────────────
|
|
61
|
-
|
|
62
|
-
/**
|
|
63
|
-
* Load the KB index — slug + summary only.
|
|
64
|
-
* Token-efficient: agent uses this to find relevant entries without loading content.
|
|
65
|
-
* Karpathy: "agent reads the index first, finds relevant pages, drills in."
|
|
66
|
-
*/
|
|
67
|
-
export async function getIndex(opts: {
|
|
68
|
-
entry_type?: KBEntryType
|
|
69
|
-
stale_only?: boolean
|
|
70
|
-
limit?: number
|
|
71
|
-
} = {}): Promise<KBIndexEntry[]> {
|
|
72
|
-
const profile = await getProfile()
|
|
73
|
-
const args: unknown[] = [profile.id]
|
|
74
|
-
let where = 'WHERE profile_id=? AND hidden=0'
|
|
75
|
-
|
|
76
|
-
if (opts.entry_type) { where += ' AND entry_type=?'; args.push(opts.entry_type) }
|
|
77
|
-
if (opts.stale_only) { where += ' AND stale=1' }
|
|
78
|
-
|
|
79
|
-
args.push(opts.limit ?? 200)
|
|
80
|
-
const { rows } = await db.execute({
|
|
81
|
-
sql: `SELECT id,slug,title,summary,entry_type,stale,updated_at
|
|
82
|
-
FROM agent_kb ${where}
|
|
83
|
-
ORDER BY updated_at DESC LIMIT ?`,
|
|
84
|
-
args,
|
|
85
|
-
})
|
|
86
|
-
return rows.map(parse_index) as KBIndexEntry[]
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
/**
|
|
90
|
-
* Get a full KB entry by slug. Loads content.
|
|
91
|
-
* Only call after finding the slug in getIndex().
|
|
92
|
-
*/
|
|
93
|
-
export async function getEntry(slug: string): Promise<KBEntry | null> {
|
|
94
|
-
const profile = await getProfile()
|
|
95
|
-
const { rows: [r] } = await db.execute({
|
|
96
|
-
sql: `SELECT * FROM agent_kb WHERE profile_id=? AND slug=? AND hidden=0 LIMIT 1`,
|
|
97
|
-
args: [profile.id, slug],
|
|
98
|
-
})
|
|
99
|
-
return r ? parse_entry(r) : null
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
/**
|
|
103
|
-
* Get multiple entries by their IDs (for loading related_ids).
|
|
104
|
-
* Returns slug+summary only — caller drills into content if needed.
|
|
105
|
-
*/
|
|
106
|
-
export async function getRelated(ids: string[]): Promise<KBIndexEntry[]> {
|
|
107
|
-
if (!ids.length) return []
|
|
108
|
-
const profile = await getProfile()
|
|
109
|
-
const placeholders = ids.map(() => '?').join(',')
|
|
110
|
-
const { rows } = await db.execute({
|
|
111
|
-
sql: `SELECT id,slug,title,summary,entry_type,stale,updated_at
|
|
112
|
-
FROM agent_kb WHERE profile_id=? AND id IN (${placeholders}) AND hidden=0`,
|
|
113
|
-
args: [profile.id, ...ids],
|
|
114
|
-
})
|
|
115
|
-
return rows.map(parse_index) as KBIndexEntry[]
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
/**
|
|
119
|
-
* Search KB by title/content match.
|
|
120
|
-
* Simple full-text via LIKE — sufficient at KB scale (<500 entries).
|
|
121
|
-
*/
|
|
122
|
-
export async function searchKB(query: string, limit = 20): Promise<KBIndexEntry[]> {
|
|
123
|
-
const profile = await getProfile()
|
|
124
|
-
const term = `%${query}%`
|
|
125
|
-
const { rows } = await db.execute({
|
|
126
|
-
sql: `SELECT id,slug,title,summary,entry_type,stale,updated_at
|
|
127
|
-
FROM agent_kb
|
|
128
|
-
WHERE profile_id=? AND hidden=0
|
|
129
|
-
AND (title LIKE ? OR summary LIKE ? OR content LIKE ?)
|
|
130
|
-
ORDER BY updated_at DESC LIMIT ?`,
|
|
131
|
-
args: [profile.id, term, term, term, limit],
|
|
132
|
-
})
|
|
133
|
-
return rows.map(parse_index) as KBIndexEntry[]
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
// ─────────────────────────────────────────────────────────────────────────────
|
|
137
|
-
// Write (agent-only — users never write directly)
|
|
138
|
-
// ─────────────────────────────────────────────────────────────────────────────
|
|
139
|
-
|
|
140
|
-
/**
|
|
141
|
-
* Create or update a KB entry by slug (upsert).
|
|
142
|
-
* Agent calls this when integrating a new source or updating an existing page.
|
|
143
|
-
* Karpathy: "one new source can ripple through 10-15 existing pages."
|
|
144
|
-
*/
|
|
145
|
-
export async function upsertEntry(input: {
|
|
146
|
-
entry_type: KBEntryType
|
|
147
|
-
title: string
|
|
148
|
-
slug: string
|
|
149
|
-
content: string
|
|
150
|
-
summary?: string
|
|
151
|
-
related_ids?: string[]
|
|
152
|
-
source_note_ids?: string[]
|
|
153
|
-
tags?: string[]
|
|
154
|
-
}): Promise<string> {
|
|
155
|
-
const profile = await getProfile()
|
|
156
|
-
const existing = await getEntry(input.slug)
|
|
157
|
-
const now = iso()
|
|
158
|
-
|
|
159
|
-
if (existing) {
|
|
160
|
-
// Update — increment version, clear stale flag
|
|
161
|
-
await db.execute({
|
|
162
|
-
sql: `UPDATE agent_kb SET
|
|
163
|
-
title=?,content=?,summary=?,related_ids=?,
|
|
164
|
-
source_note_ids=?,tags=?,stale=0,
|
|
165
|
-
version=version+1,updated_at=?
|
|
166
|
-
WHERE id=?`,
|
|
167
|
-
args: [
|
|
168
|
-
input.title, input.content,
|
|
169
|
-
input.summary ?? null,
|
|
170
|
-
JSON.stringify(input.related_ids ?? []),
|
|
171
|
-
JSON.stringify(input.source_note_ids ?? []),
|
|
172
|
-
JSON.stringify(input.tags ?? []),
|
|
173
|
-
now, existing.id,
|
|
174
|
-
],
|
|
175
|
-
})
|
|
176
|
-
return existing.id
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
// Create new entry
|
|
180
|
-
const id = ulid()
|
|
181
|
-
await db.execute({
|
|
182
|
-
sql: `INSERT INTO agent_kb
|
|
183
|
-
(id,profile_id,entry_type,title,slug,content,summary,
|
|
184
|
-
related_ids,source_note_ids,tags,created_at,updated_at)
|
|
185
|
-
VALUES (?,?,?,?,?,?,?,?,?,?,?,?)`,
|
|
186
|
-
args: [
|
|
187
|
-
id, profile.id, input.entry_type,
|
|
188
|
-
input.title, input.slug, input.content,
|
|
189
|
-
input.summary ?? null,
|
|
190
|
-
JSON.stringify(input.related_ids ?? []),
|
|
191
|
-
JSON.stringify(input.source_note_ids ?? []),
|
|
192
|
-
JSON.stringify(input.tags ?? []),
|
|
193
|
-
now, now,
|
|
194
|
-
],
|
|
195
|
-
})
|
|
196
|
-
return id
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
/**
|
|
200
|
-
* Mark an entry as stale when new info contradicts it.
|
|
201
|
-
* Agent reviews stale entries and updates them.
|
|
202
|
-
*/
|
|
203
|
-
export async function markStale(slug: string): Promise<void> {
|
|
204
|
-
const profile = await getProfile()
|
|
205
|
-
await db.execute({
|
|
206
|
-
sql: `UPDATE agent_kb SET stale=1,updated_at=? WHERE profile_id=? AND slug=?`,
|
|
207
|
-
args: [iso(), profile.id, slug],
|
|
208
|
-
})
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
/**
|
|
212
|
-
* File back an agent answer as a KB synthesis entry.
|
|
213
|
-
* Karpathy: "your questions and the agents' answers get filed back as new wiki pages."
|
|
214
|
-
* Call this whenever the agent produces a useful analysis or answer.
|
|
215
|
-
*/
|
|
216
|
-
export async function fileAnswer(
|
|
217
|
-
question: string,
|
|
218
|
-
answer: string,
|
|
219
|
-
related_slugs: string[] = []
|
|
220
|
-
): Promise<string> {
|
|
221
|
-
const slug = 'qa-' + question.toLowerCase().replace(/[^a-z0-9]+/g, '-').slice(0, 50)
|
|
222
|
-
const summary = answer.slice(0, 200).replace(/\n/g, ' ') + '…'
|
|
223
|
-
|
|
224
|
-
const related_ids: string[] = []
|
|
225
|
-
for (const s of related_slugs) {
|
|
226
|
-
const e = await getEntry(s)
|
|
227
|
-
if (e) related_ids.push(e.id)
|
|
228
|
-
}
|
|
229
|
-
|
|
230
|
-
return upsertEntry({
|
|
231
|
-
entry_type: 'synthesis',
|
|
232
|
-
title: question,
|
|
233
|
-
slug,
|
|
234
|
-
content: `# Q: ${question}\n\n${answer}`,
|
|
235
|
-
summary,
|
|
236
|
-
related_ids,
|
|
237
|
-
tags: ['qa', 'synthesis'],
|
|
238
|
-
})
|
|
239
|
-
}
|
|
240
|
-
|
|
241
|
-
// ─────────────────────────────────────────────────────────────────────────────
|
|
242
|
-
// Process inbox notes → KB (the core Karpathy integration loop)
|
|
243
|
-
// ─────────────────────────────────────────────────────────────────────────────
|
|
244
|
-
|
|
245
|
-
/**
|
|
246
|
-
* Get unprocessed notes from agent_notes (status='inbox').
|
|
247
|
-
* Agent reads these, integrates them into KB, marks them 'done'.
|
|
248
|
-
*/
|
|
249
|
-
export async function getInboxNotes(limit = 10): Promise<Array<{
|
|
250
|
-
id: string; content: string; tags: string[]
|
|
251
|
-
}>> {
|
|
252
|
-
const profile = await getProfile()
|
|
253
|
-
const { rows } = await db.execute({
|
|
254
|
-
sql: `SELECT id,content,tags FROM agent_notes
|
|
255
|
-
WHERE profile_id=? AND status='inbox' AND hidden=0
|
|
256
|
-
ORDER BY created_at ASC LIMIT ?`,
|
|
257
|
-
args: [profile.id, limit],
|
|
258
|
-
})
|
|
259
|
-
return rows.map(r => ({
|
|
260
|
-
id: r.id as string,
|
|
261
|
-
content: r.content as string,
|
|
262
|
-
tags: safeJSON(r.tags, []) as string[],
|
|
263
|
-
}))
|
|
264
|
-
}
|
|
265
|
-
|
|
266
|
-
/**
|
|
267
|
-
* Mark a note as processed after integrating it into KB.
|
|
268
|
-
*/
|
|
269
|
-
export async function markNoteProcessed(
|
|
270
|
-
noteId: string,
|
|
271
|
-
agentResponse: string
|
|
272
|
-
): Promise<void> {
|
|
273
|
-
const profile = await getProfile()
|
|
274
|
-
await db.execute({
|
|
275
|
-
sql: `UPDATE agent_notes SET status='done',agent_response=?,updated_at=?
|
|
276
|
-
WHERE id=? AND profile_id=?`,
|
|
277
|
-
args: [agentResponse, iso(), noteId, profile.id],
|
|
278
|
-
})
|
|
279
|
-
}
|
|
280
|
-
|
|
281
|
-
/**
|
|
282
|
-
* Get all stale entries for agent review.
|
|
283
|
-
* Agent should update these when it has new info.
|
|
284
|
-
*/
|
|
285
|
-
export async function getStaleEntries(): Promise<KBIndexEntry[]> {
|
|
286
|
-
return getIndex({ stale_only: true })
|
|
287
|
-
}
|
|
288
|
-
|
|
289
|
-
/**
|
|
290
|
-
* Format the KB index as a compact context string for agents.
|
|
291
|
-
* Token-efficient — only slug, type, and summary.
|
|
292
|
-
*/
|
|
293
|
-
export async function kbContext(limit = 50): Promise<string> {
|
|
294
|
-
const index = await getIndex({ limit })
|
|
295
|
-
if (!index.length) return '(Knowledge base is empty — add notes via agent_notes inbox)'
|
|
296
|
-
|
|
297
|
-
const lines = index.map(e =>
|
|
298
|
-
`[${e.entry_type}] ${e.slug}: ${e.summary ?? e.title}${e.stale ? ' ⚠️ stale' : ''}`
|
|
299
|
-
)
|
|
300
|
-
return `## Knowledge Base Index (${index.length} entries)\n${lines.join('\n')}`
|
|
301
|
-
}
|
|
302
|
-
|
|
303
|
-
// ─── helpers ─────────────────────────────────────────────────────────────────
|
|
304
|
-
|
|
305
|
-
function safeJSON(v: unknown, fb: unknown) {
|
|
306
|
-
try { return JSON.parse(v as string) } catch { return fb }
|
|
307
|
-
}
|
|
308
|
-
|
|
309
|
-
function parse_index(r: Record<string, unknown>): KBIndexEntry {
|
|
310
|
-
return {
|
|
311
|
-
id: r.id as string,
|
|
312
|
-
slug: r.slug as string,
|
|
313
|
-
title: r.title as string,
|
|
314
|
-
summary: r.summary as string | null,
|
|
315
|
-
entry_type: r.entry_type as KBEntryType,
|
|
316
|
-
stale: r.stale as number,
|
|
317
|
-
updated_at: r.updated_at as string,
|
|
318
|
-
}
|
|
319
|
-
}
|
|
320
|
-
|
|
321
|
-
function parse_entry(r: Record<string, unknown>): KBEntry {
|
|
322
|
-
return {
|
|
323
|
-
id: r.id as string,
|
|
324
|
-
profile_id: r.profile_id as string,
|
|
325
|
-
entry_type: r.entry_type as KBEntryType,
|
|
326
|
-
title: r.title as string,
|
|
327
|
-
slug: r.slug as string,
|
|
328
|
-
content: r.content as string,
|
|
329
|
-
summary: r.summary as string | null,
|
|
330
|
-
related_ids: safeJSON(r.related_ids, []) as string[],
|
|
331
|
-
source_note_ids: safeJSON(r.source_note_ids, []) as string[],
|
|
332
|
-
tags: safeJSON(r.tags, []) as string[],
|
|
333
|
-
stale: r.stale as number,
|
|
334
|
-
version: r.version as number,
|
|
335
|
-
updated_at: r.updated_at as string,
|
|
336
|
-
}
|
|
337
|
-
}
|