@girardmedia/bootspring 1.2.0 → 2.0.3
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/README.md +107 -14
- package/bin/bootspring.js +166 -27
- package/cli/agent.js +189 -17
- package/cli/analyze.js +499 -0
- package/cli/audit.js +557 -0
- package/cli/auth.js +495 -38
- package/cli/billing.js +302 -0
- package/cli/build.js +695 -0
- package/cli/business.js +109 -26
- package/cli/checkpoint-utils.js +168 -0
- package/cli/checkpoint.js +639 -0
- package/cli/cloud-sync.js +447 -0
- package/cli/content.js +198 -0
- package/cli/context.js +1 -1
- package/cli/deploy.js +543 -0
- package/cli/fundraise.js +112 -50
- package/cli/github-cmd.js +435 -0
- package/cli/health.js +477 -0
- package/cli/init.js +84 -13
- package/cli/legal.js +107 -95
- package/cli/log.js +2 -2
- package/cli/loop.js +976 -73
- package/cli/manager.js +711 -0
- package/cli/metrics.js +480 -0
- package/cli/monitor.js +812 -0
- package/cli/onboard.js +521 -0
- package/cli/orchestrator.js +12 -24
- package/cli/prd.js +594 -0
- package/cli/preseed-start.js +1483 -0
- package/cli/preseed.js +2302 -0
- package/cli/project.js +436 -0
- package/cli/quality.js +233 -0
- package/cli/security.js +913 -0
- package/cli/seed.js +1441 -5
- package/cli/skill.js +273 -211
- package/cli/suggest.js +989 -0
- package/cli/switch.js +453 -0
- package/cli/visualize.js +527 -0
- package/cli/watch.js +769 -0
- package/cli/workspace.js +607 -0
- package/core/analyze-workflow.js +1134 -0
- package/core/api-client.js +535 -22
- package/core/audit-workflow.js +1350 -0
- package/core/build-orchestrator.js +480 -0
- package/core/build-state.js +577 -0
- package/core/checkpoint-engine.js +408 -0
- package/core/config.js +1109 -26
- package/core/context-loader.js +21 -1
- package/core/deploy-workflow.js +836 -0
- package/core/entitlements.js +93 -22
- package/core/github-sync.js +610 -0
- package/core/index.js +8 -1
- package/core/ingest.js +1111 -0
- package/core/metrics-engine.js +768 -0
- package/core/onboard-workflow.js +1007 -0
- package/core/preseed-workflow.js +934 -0
- package/core/preseed.js +1617 -0
- package/core/project-context.js +325 -0
- package/core/project-state.js +694 -0
- package/core/r2-sync.js +583 -0
- package/core/scaffold.js +525 -7
- package/core/session.js +258 -0
- package/core/task-extractor.js +758 -0
- package/core/telemetry.js +28 -6
- package/core/tier-enforcement.js +737 -0
- package/core/utils.js +38 -14
- package/generators/questionnaire.js +15 -12
- package/generators/sections/ai.js +7 -7
- package/generators/sections/content.js +300 -0
- package/generators/sections/index.js +3 -0
- package/generators/sections/plugins.js +7 -6
- package/generators/templates/build-planning.template.js +596 -0
- package/generators/templates/content.template.js +819 -0
- package/generators/templates/index.js +2 -1
- package/hooks/git-autopilot.js +1250 -0
- package/hooks/index.js +9 -0
- package/intelligence/agent-collab.js +2057 -0
- package/intelligence/auto-suggest.js +634 -0
- package/intelligence/content-gen.js +1589 -0
- package/intelligence/cross-project.js +1647 -0
- package/intelligence/index.js +184 -0
- package/intelligence/learning/insights.json +517 -7
- package/intelligence/learning/pattern-learner.js +1008 -14
- package/intelligence/memory/decision-tracker.js +1431 -31
- package/intelligence/memory/decisions.jsonl +0 -0
- package/intelligence/orchestrator.js +2896 -1
- package/intelligence/prd.js +92 -1
- package/intelligence/recommendation-weights.json +14 -2
- package/intelligence/recommendations.js +463 -9
- package/intelligence/workflow-composer.js +1451 -0
- package/marketplace/index.d.ts +324 -0
- package/marketplace/index.js +1921 -0
- package/mcp/contracts/mcp-contract.v1.json +342 -4
- package/mcp/registry.js +680 -3
- package/mcp/response-formatter.js +23 -0
- package/mcp/tools/assist-tool.js +78 -4
- package/mcp/tools/autopilot-tool.js +408 -0
- package/mcp/tools/content-tool.js +571 -0
- package/mcp/tools/dashboard-tool.js +251 -5
- package/mcp/tools/mvp-tool.js +344 -0
- package/mcp/tools/plugin-tool.js +23 -1
- package/mcp/tools/prd-tool.js +579 -0
- package/mcp/tools/seed-tool.js +447 -0
- package/mcp/tools/skill-tool.js +43 -14
- package/mcp/tools/suggest-tool.js +147 -0
- package/package.json +15 -6
- package/agents/README.md +0 -93
- package/agents/ai-integration-expert/context.md +0 -386
- package/agents/api-expert/context.md +0 -416
- package/agents/architecture-expert/context.md +0 -454
- package/agents/auth-expert/context.md +0 -399
- package/agents/backend-expert/context.md +0 -483
- package/agents/business-strategy-expert/context.md +0 -180
- package/agents/code-review-expert/context.md +0 -365
- package/agents/competitive-analysis-expert/context.md +0 -239
- package/agents/data-modeling-expert/context.md +0 -352
- package/agents/database-expert/context.md +0 -250
- package/agents/devops-expert/context.md +0 -446
- package/agents/email-expert/context.md +0 -379
- package/agents/financial-expert/context.md +0 -213
- package/agents/frontend-expert/context.md +0 -364
- package/agents/fundraising-expert/context.md +0 -257
- package/agents/growth-expert/context.md +0 -249
- package/agents/index.js +0 -140
- package/agents/investor-relations-expert/context.md +0 -266
- package/agents/legal-expert/context.md +0 -284
- package/agents/marketing-expert/context.md +0 -236
- package/agents/monitoring-expert/context.md +0 -362
- package/agents/operations-expert/context.md +0 -279
- package/agents/partnerships-expert/context.md +0 -286
- package/agents/payment-expert/context.md +0 -340
- package/agents/performance-expert/context.md +0 -377
- package/agents/private-equity-expert/context.md +0 -246
- package/agents/railway-expert/context.md +0 -284
- package/agents/research-expert/context.md +0 -245
- package/agents/sales-expert/context.md +0 -241
- package/agents/security-expert/context.md +0 -343
- package/agents/testing-expert/context.md +0 -414
- package/agents/ui-ux-expert/context.md +0 -448
- package/agents/vercel-expert/context.md +0 -426
- package/skills/index.js +0 -787
- package/skills/patterns/README.md +0 -163
- package/skills/patterns/ai/agents.md +0 -281
- package/skills/patterns/ai/claude.md +0 -138
- package/skills/patterns/ai/embeddings.md +0 -150
- package/skills/patterns/ai/rag.md +0 -266
- package/skills/patterns/ai/streaming.md +0 -170
- package/skills/patterns/ai/structured-output.md +0 -162
- package/skills/patterns/ai/tools.md +0 -154
- package/skills/patterns/analytics/tracking.md +0 -220
- package/skills/patterns/api/errors.md +0 -296
- package/skills/patterns/api/graphql.md +0 -440
- package/skills/patterns/api/middleware.md +0 -279
- package/skills/patterns/api/openapi.md +0 -285
- package/skills/patterns/api/rate-limiting.md +0 -231
- package/skills/patterns/api/route-handler.md +0 -217
- package/skills/patterns/api/server-action.md +0 -249
- package/skills/patterns/api/versioning.md +0 -443
- package/skills/patterns/api/webhooks.md +0 -247
- package/skills/patterns/auth/clerk.md +0 -132
- package/skills/patterns/auth/mfa.md +0 -313
- package/skills/patterns/auth/nextauth.md +0 -140
- package/skills/patterns/auth/oauth.md +0 -237
- package/skills/patterns/auth/rbac.md +0 -152
- package/skills/patterns/auth/session-management.md +0 -367
- package/skills/patterns/auth/session.md +0 -120
- package/skills/patterns/database/audit.md +0 -177
- package/skills/patterns/database/migrations.md +0 -177
- package/skills/patterns/database/pagination.md +0 -230
- package/skills/patterns/database/pooling.md +0 -357
- package/skills/patterns/database/prisma.md +0 -180
- package/skills/patterns/database/relations.md +0 -187
- package/skills/patterns/database/seeding.md +0 -246
- package/skills/patterns/database/soft-delete.md +0 -153
- package/skills/patterns/database/transactions.md +0 -162
- package/skills/patterns/deployment/ci-cd.md +0 -231
- package/skills/patterns/deployment/docker.md +0 -188
- package/skills/patterns/deployment/monitoring.md +0 -387
- package/skills/patterns/deployment/vercel.md +0 -160
- package/skills/patterns/email/resend.md +0 -143
- package/skills/patterns/email/templates.md +0 -245
- package/skills/patterns/email/transactional.md +0 -503
- package/skills/patterns/email/verification.md +0 -176
- package/skills/patterns/files/download.md +0 -243
- package/skills/patterns/files/upload.md +0 -239
- package/skills/patterns/i18n/nextintl.md +0 -188
- package/skills/patterns/logging/structured.md +0 -292
- package/skills/patterns/notifications/email-queue.md +0 -248
- package/skills/patterns/notifications/push.md +0 -279
- package/skills/patterns/payments/checkout.md +0 -303
- package/skills/patterns/payments/invoices.md +0 -287
- package/skills/patterns/payments/portal.md +0 -245
- package/skills/patterns/payments/stripe.md +0 -272
- package/skills/patterns/payments/subscriptions.md +0 -300
- package/skills/patterns/payments/usage.md +0 -279
- package/skills/patterns/performance/caching.md +0 -276
- package/skills/patterns/performance/code-splitting.md +0 -233
- package/skills/patterns/performance/edge.md +0 -254
- package/skills/patterns/performance/isr.md +0 -266
- package/skills/patterns/performance/lazy-loading.md +0 -281
- package/skills/patterns/realtime/sse.md +0 -327
- package/skills/patterns/realtime/websockets.md +0 -336
- package/skills/patterns/search/filtering.md +0 -329
- package/skills/patterns/search/fulltext.md +0 -260
- package/skills/patterns/security/audit-logging.md +0 -444
- package/skills/patterns/security/csrf.md +0 -234
- package/skills/patterns/security/headers.md +0 -252
- package/skills/patterns/security/sanitization.md +0 -258
- package/skills/patterns/security/secrets.md +0 -261
- package/skills/patterns/security/validation.md +0 -268
- package/skills/patterns/security/xss.md +0 -229
- package/skills/patterns/seo/metadata.md +0 -252
- package/skills/patterns/state/context.md +0 -349
- package/skills/patterns/state/react-query.md +0 -313
- package/skills/patterns/state/url-state.md +0 -482
- package/skills/patterns/state/zustand.md +0 -262
- package/skills/patterns/testing/api.md +0 -259
- package/skills/patterns/testing/component.md +0 -233
- package/skills/patterns/testing/coverage.md +0 -207
- package/skills/patterns/testing/fixtures.md +0 -225
- package/skills/patterns/testing/integration.md +0 -436
- package/skills/patterns/testing/mocking.md +0 -177
- package/skills/patterns/testing/playwright.md +0 -162
- package/skills/patterns/testing/snapshot.md +0 -175
- package/skills/patterns/testing/vitest.md +0 -307
- package/skills/patterns/ui/accordions.md +0 -395
- package/skills/patterns/ui/cards.md +0 -299
- package/skills/patterns/ui/dropdowns.md +0 -476
- package/skills/patterns/ui/empty-states.md +0 -320
- package/skills/patterns/ui/forms.md +0 -405
- package/skills/patterns/ui/inputs.md +0 -319
- package/skills/patterns/ui/layouts.md +0 -282
- package/skills/patterns/ui/loading.md +0 -291
- package/skills/patterns/ui/modals.md +0 -338
- package/skills/patterns/ui/navigation.md +0 -374
- package/skills/patterns/ui/tables.md +0 -407
- package/skills/patterns/ui/toasts.md +0 -300
- package/skills/patterns/ui/tooltips.md +0 -396
- package/skills/patterns/utils/dates.md +0 -435
- package/skills/patterns/utils/errors.md +0 -451
- package/skills/patterns/utils/formatting.md +0 -345
- package/skills/patterns/utils/validation.md +0 -434
- package/templates/bootspring.config.js +0 -83
- package/templates/business/business-model-canvas.md +0 -246
- package/templates/business/business-plan.md +0 -266
- package/templates/business/competitive-analysis.md +0 -312
- package/templates/fundraising/data-room-checklist.md +0 -300
- package/templates/fundraising/investor-research.md +0 -243
- package/templates/fundraising/pitch-deck-outline.md +0 -253
- package/templates/legal/gdpr-checklist.md +0 -339
- package/templates/legal/privacy-policy.md +0 -285
- package/templates/legal/terms-of-service.md +0 -222
- package/templates/mcp.json +0 -9
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@girardmedia/bootspring",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.3",
|
|
4
4
|
"description": "Development scaffolding with intelligence - AI-powered context, agents, and workflows for any MCP-compatible assistant",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"ai",
|
|
@@ -44,6 +44,10 @@
|
|
|
44
44
|
"./api": {
|
|
45
45
|
"types": "./core/api-client.d.ts",
|
|
46
46
|
"default": "./core/api-client.js"
|
|
47
|
+
},
|
|
48
|
+
"./marketplace": {
|
|
49
|
+
"types": "./marketplace/index.d.ts",
|
|
50
|
+
"default": "./marketplace/index.js"
|
|
47
51
|
}
|
|
48
52
|
},
|
|
49
53
|
"files": [
|
|
@@ -51,15 +55,12 @@
|
|
|
51
55
|
"core/",
|
|
52
56
|
"cli/",
|
|
53
57
|
"mcp/",
|
|
54
|
-
"templates/",
|
|
55
58
|
"intelligence/",
|
|
56
59
|
"quality/",
|
|
57
|
-
"skills/index.js",
|
|
58
|
-
"skills/patterns/",
|
|
59
|
-
"agents/",
|
|
60
60
|
"generators/",
|
|
61
61
|
"hooks/",
|
|
62
|
-
"plugins/"
|
|
62
|
+
"plugins/",
|
|
63
|
+
"marketplace/"
|
|
63
64
|
],
|
|
64
65
|
"scripts": {
|
|
65
66
|
"start": "node bin/bootspring.js",
|
|
@@ -74,6 +75,8 @@
|
|
|
74
75
|
"build:mcp-contract": "node scripts/export-mcp-contract.js",
|
|
75
76
|
"verify:mcp-contract": "node scripts/export-mcp-contract.js --check",
|
|
76
77
|
"verify:package": "node scripts/check-package-boundaries.js",
|
|
78
|
+
"db:sync": "node shared/db/sync.js",
|
|
79
|
+
"db:sync:check": "node shared/db/sync.js --check",
|
|
77
80
|
"prepublishOnly": "npm test && npm run lint --if-present && npm run verify:package && npm run verify:mcp-contract"
|
|
78
81
|
},
|
|
79
82
|
"devDependencies": {
|
|
@@ -90,5 +93,11 @@
|
|
|
90
93
|
},
|
|
91
94
|
"engines": {
|
|
92
95
|
"node": ">=18.0.0"
|
|
96
|
+
},
|
|
97
|
+
"overrides": {
|
|
98
|
+
"table": {
|
|
99
|
+
"ajv": "^8.12.0"
|
|
100
|
+
},
|
|
101
|
+
"minimatch": "^10.2.1"
|
|
93
102
|
}
|
|
94
103
|
}
|
package/agents/README.md
DELETED
|
@@ -1,93 +0,0 @@
|
|
|
1
|
-
# Bootspring Agents
|
|
2
|
-
|
|
3
|
-
Specialized AI agents for different development domains. Each agent provides focused expertise, context, and patterns for its area.
|
|
4
|
-
|
|
5
|
-
## Available Agents
|
|
6
|
-
|
|
7
|
-
| Agent | Domain | Use When |
|
|
8
|
-
|-------|--------|----------|
|
|
9
|
-
| [database-expert](./database-expert/) | Database & ORM | Schema design, queries, migrations |
|
|
10
|
-
| [security-expert](./security-expert/) | Security & Auth | Auth flows, vulnerabilities, OWASP |
|
|
11
|
-
| [frontend-expert](./frontend-expert/) | UI & React | Components, state, styling |
|
|
12
|
-
| [backend-expert](./backend-expert/) | Server Logic | API routes, server actions, business logic |
|
|
13
|
-
| [api-expert](./api-expert/) | API Design | REST, GraphQL, route handlers |
|
|
14
|
-
| [testing-expert](./testing-expert/) | Testing | Unit, integration, E2E tests |
|
|
15
|
-
| [performance-expert](./performance-expert/) | Performance | Optimization, caching, profiling |
|
|
16
|
-
| [devops-expert](./devops-expert/) | DevOps & CI/CD | Deployment, pipelines, infrastructure |
|
|
17
|
-
| [ui-ux-expert](./ui-ux-expert/) | Design & UX | User experience, accessibility, design |
|
|
18
|
-
| [architecture-expert](./architecture-expert/) | Architecture | System design, patterns, scaling |
|
|
19
|
-
| [code-review-expert](./code-review-expert/) | Code Quality | Reviews, best practices, refactoring |
|
|
20
|
-
| [vercel-expert](./vercel-expert/) | Vercel Platform | Deployment, edge, serverless |
|
|
21
|
-
|
|
22
|
-
## Using Agents
|
|
23
|
-
|
|
24
|
-
### Via CLI
|
|
25
|
-
```bash
|
|
26
|
-
# List all agents
|
|
27
|
-
bootspring agent list
|
|
28
|
-
|
|
29
|
-
# Show agent details
|
|
30
|
-
bootspring agent show database-expert
|
|
31
|
-
|
|
32
|
-
# Invoke agent for current context
|
|
33
|
-
bootspring agent invoke security-expert
|
|
34
|
-
```
|
|
35
|
-
|
|
36
|
-
### Via MCP
|
|
37
|
-
When using Claude Code, agents are automatically suggested based on your task context. You can also explicitly request an agent:
|
|
38
|
-
|
|
39
|
-
```
|
|
40
|
-
@bootspring agent database-expert
|
|
41
|
-
```
|
|
42
|
-
|
|
43
|
-
### Agent Selection
|
|
44
|
-
|
|
45
|
-
Bootspring automatically detects which agents are relevant based on keywords in your request:
|
|
46
|
-
|
|
47
|
-
- **database-expert**: database, schema, prisma, drizzle, migration, query, model
|
|
48
|
-
- **security-expert**: auth, login, security, password, jwt, csrf, xss, owasp
|
|
49
|
-
- **frontend-expert**: component, react, ui, tailwind, state, client, hook
|
|
50
|
-
- **backend-expert**: api, server, action, business, logic, service
|
|
51
|
-
- **testing-expert**: test, spec, jest, vitest, playwright, coverage
|
|
52
|
-
- **performance-expert**: performance, optimize, cache, slow, fast, bundle
|
|
53
|
-
- **devops-expert**: deploy, ci, cd, docker, kubernetes, pipeline
|
|
54
|
-
- **architecture-expert**: architecture, design, pattern, scale, structure
|
|
55
|
-
|
|
56
|
-
## Agent Context Files
|
|
57
|
-
|
|
58
|
-
Each agent directory contains:
|
|
59
|
-
|
|
60
|
-
```
|
|
61
|
-
agents/{name}/
|
|
62
|
-
├── context.md # Full agent context and expertise
|
|
63
|
-
├── patterns.md # Common patterns this agent uses
|
|
64
|
-
└── checklist.md # Review checklist for this domain
|
|
65
|
-
```
|
|
66
|
-
|
|
67
|
-
## Creating Custom Agents
|
|
68
|
-
|
|
69
|
-
Add a new directory under `agents/` with at minimum a `context.md` file:
|
|
70
|
-
|
|
71
|
-
```markdown
|
|
72
|
-
# Custom Agent Name
|
|
73
|
-
|
|
74
|
-
## Role
|
|
75
|
-
Brief description of what this agent does.
|
|
76
|
-
|
|
77
|
-
## Expertise
|
|
78
|
-
- Area 1
|
|
79
|
-
- Area 2
|
|
80
|
-
|
|
81
|
-
## Context
|
|
82
|
-
Detailed knowledge and patterns...
|
|
83
|
-
```
|
|
84
|
-
|
|
85
|
-
Then register it in your `bootspring.config.js`:
|
|
86
|
-
|
|
87
|
-
```javascript
|
|
88
|
-
module.exports = {
|
|
89
|
-
agents: {
|
|
90
|
-
custom: ['my-custom-agent']
|
|
91
|
-
}
|
|
92
|
-
};
|
|
93
|
-
```
|
|
@@ -1,386 +0,0 @@
|
|
|
1
|
-
# AI Integration Expert Agent
|
|
2
|
-
|
|
3
|
-
## Role
|
|
4
|
-
Specialized in AI/LLM integration, Anthropic Claude SDK, AI SDK, streaming responses, and building AI-powered features.
|
|
5
|
-
|
|
6
|
-
## Core Expertise
|
|
7
|
-
|
|
8
|
-
### Anthropic Claude Integration
|
|
9
|
-
|
|
10
|
-
```typescript
|
|
11
|
-
// lib/anthropic.ts
|
|
12
|
-
import Anthropic from '@anthropic-ai/sdk';
|
|
13
|
-
|
|
14
|
-
export const anthropic = new Anthropic({
|
|
15
|
-
apiKey: process.env.ANTHROPIC_API_KEY,
|
|
16
|
-
});
|
|
17
|
-
|
|
18
|
-
// Basic message
|
|
19
|
-
export async function chat(prompt: string) {
|
|
20
|
-
const response = await anthropic.messages.create({
|
|
21
|
-
model: 'claude-sonnet-4-20250514',
|
|
22
|
-
max_tokens: 1024,
|
|
23
|
-
messages: [{ role: 'user', content: prompt }],
|
|
24
|
-
});
|
|
25
|
-
|
|
26
|
-
return response.content[0].type === 'text'
|
|
27
|
-
? response.content[0].text
|
|
28
|
-
: null;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
// With system prompt
|
|
32
|
-
export async function chatWithSystem(
|
|
33
|
-
system: string,
|
|
34
|
-
prompt: string
|
|
35
|
-
) {
|
|
36
|
-
const response = await anthropic.messages.create({
|
|
37
|
-
model: 'claude-sonnet-4-20250514',
|
|
38
|
-
max_tokens: 1024,
|
|
39
|
-
system,
|
|
40
|
-
messages: [{ role: 'user', content: prompt }],
|
|
41
|
-
});
|
|
42
|
-
|
|
43
|
-
return response.content[0].type === 'text'
|
|
44
|
-
? response.content[0].text
|
|
45
|
-
: null;
|
|
46
|
-
}
|
|
47
|
-
```
|
|
48
|
-
|
|
49
|
-
### Streaming Responses
|
|
50
|
-
|
|
51
|
-
```typescript
|
|
52
|
-
// app/api/chat/route.ts
|
|
53
|
-
import { anthropic } from '@/lib/anthropic';
|
|
54
|
-
|
|
55
|
-
export async function POST(req: Request) {
|
|
56
|
-
const { messages } = await req.json();
|
|
57
|
-
|
|
58
|
-
const stream = await anthropic.messages.stream({
|
|
59
|
-
model: 'claude-sonnet-4-20250514',
|
|
60
|
-
max_tokens: 1024,
|
|
61
|
-
messages,
|
|
62
|
-
});
|
|
63
|
-
|
|
64
|
-
// Return as streaming response
|
|
65
|
-
return new Response(stream.toReadableStream(), {
|
|
66
|
-
headers: {
|
|
67
|
-
'Content-Type': 'text/event-stream',
|
|
68
|
-
'Cache-Control': 'no-cache',
|
|
69
|
-
Connection: 'keep-alive',
|
|
70
|
-
},
|
|
71
|
-
});
|
|
72
|
-
}
|
|
73
|
-
```
|
|
74
|
-
|
|
75
|
-
```tsx
|
|
76
|
-
// components/chat.tsx
|
|
77
|
-
'use client';
|
|
78
|
-
|
|
79
|
-
import { useState } from 'react';
|
|
80
|
-
|
|
81
|
-
export function Chat() {
|
|
82
|
-
const [messages, setMessages] = useState<Message[]>([]);
|
|
83
|
-
const [input, setInput] = useState('');
|
|
84
|
-
const [isLoading, setIsLoading] = useState(false);
|
|
85
|
-
|
|
86
|
-
async function handleSubmit(e: React.FormEvent) {
|
|
87
|
-
e.preventDefault();
|
|
88
|
-
if (!input.trim()) return;
|
|
89
|
-
|
|
90
|
-
const userMessage = { role: 'user', content: input };
|
|
91
|
-
setMessages((prev) => [...prev, userMessage]);
|
|
92
|
-
setInput('');
|
|
93
|
-
setIsLoading(true);
|
|
94
|
-
|
|
95
|
-
const response = await fetch('/api/chat', {
|
|
96
|
-
method: 'POST',
|
|
97
|
-
body: JSON.stringify({
|
|
98
|
-
messages: [...messages, userMessage],
|
|
99
|
-
}),
|
|
100
|
-
});
|
|
101
|
-
|
|
102
|
-
const reader = response.body?.getReader();
|
|
103
|
-
const decoder = new TextDecoder();
|
|
104
|
-
let assistantMessage = '';
|
|
105
|
-
|
|
106
|
-
while (reader) {
|
|
107
|
-
const { done, value } = await reader.read();
|
|
108
|
-
if (done) break;
|
|
109
|
-
|
|
110
|
-
assistantMessage += decoder.decode(value);
|
|
111
|
-
setMessages((prev) => [
|
|
112
|
-
...prev.slice(0, -1),
|
|
113
|
-
{ role: 'assistant', content: assistantMessage },
|
|
114
|
-
]);
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
setIsLoading(false);
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
return (
|
|
121
|
-
<div className="flex flex-col h-full">
|
|
122
|
-
<div className="flex-1 overflow-y-auto p-4 space-y-4">
|
|
123
|
-
{messages.map((m, i) => (
|
|
124
|
-
<div
|
|
125
|
-
key={i}
|
|
126
|
-
className={`p-4 rounded-lg ${
|
|
127
|
-
m.role === 'user' ? 'bg-blue-100 ml-12' : 'bg-gray-100 mr-12'
|
|
128
|
-
}`}
|
|
129
|
-
>
|
|
130
|
-
{m.content}
|
|
131
|
-
</div>
|
|
132
|
-
))}
|
|
133
|
-
</div>
|
|
134
|
-
<form onSubmit={handleSubmit} className="p-4 border-t">
|
|
135
|
-
<input
|
|
136
|
-
value={input}
|
|
137
|
-
onChange={(e) => setInput(e.target.value)}
|
|
138
|
-
placeholder="Type a message..."
|
|
139
|
-
className="w-full p-2 border rounded"
|
|
140
|
-
disabled={isLoading}
|
|
141
|
-
/>
|
|
142
|
-
</form>
|
|
143
|
-
</div>
|
|
144
|
-
);
|
|
145
|
-
}
|
|
146
|
-
```
|
|
147
|
-
|
|
148
|
-
### Vercel AI SDK
|
|
149
|
-
|
|
150
|
-
```typescript
|
|
151
|
-
// app/api/chat/route.ts (using AI SDK)
|
|
152
|
-
import { anthropic } from '@ai-sdk/anthropic';
|
|
153
|
-
import { streamText } from 'ai';
|
|
154
|
-
|
|
155
|
-
export async function POST(req: Request) {
|
|
156
|
-
const { messages } = await req.json();
|
|
157
|
-
|
|
158
|
-
const result = streamText({
|
|
159
|
-
model: anthropic('claude-sonnet-4-20250514'),
|
|
160
|
-
messages,
|
|
161
|
-
});
|
|
162
|
-
|
|
163
|
-
return result.toDataStreamResponse();
|
|
164
|
-
}
|
|
165
|
-
```
|
|
166
|
-
|
|
167
|
-
```tsx
|
|
168
|
-
// components/chat.tsx (using AI SDK React)
|
|
169
|
-
'use client';
|
|
170
|
-
|
|
171
|
-
import { useChat } from 'ai/react';
|
|
172
|
-
|
|
173
|
-
export function Chat() {
|
|
174
|
-
const { messages, input, handleInputChange, handleSubmit, isLoading } =
|
|
175
|
-
useChat();
|
|
176
|
-
|
|
177
|
-
return (
|
|
178
|
-
<div className="flex flex-col h-full">
|
|
179
|
-
<div className="flex-1 overflow-y-auto p-4 space-y-4">
|
|
180
|
-
{messages.map((m) => (
|
|
181
|
-
<div
|
|
182
|
-
key={m.id}
|
|
183
|
-
className={`p-4 rounded-lg ${
|
|
184
|
-
m.role === 'user' ? 'bg-blue-100 ml-12' : 'bg-gray-100 mr-12'
|
|
185
|
-
}`}
|
|
186
|
-
>
|
|
187
|
-
{m.content}
|
|
188
|
-
</div>
|
|
189
|
-
))}
|
|
190
|
-
</div>
|
|
191
|
-
<form onSubmit={handleSubmit} className="p-4 border-t">
|
|
192
|
-
<input
|
|
193
|
-
value={input}
|
|
194
|
-
onChange={handleInputChange}
|
|
195
|
-
placeholder="Type a message..."
|
|
196
|
-
className="w-full p-2 border rounded"
|
|
197
|
-
disabled={isLoading}
|
|
198
|
-
/>
|
|
199
|
-
</form>
|
|
200
|
-
</div>
|
|
201
|
-
);
|
|
202
|
-
}
|
|
203
|
-
```
|
|
204
|
-
|
|
205
|
-
### Structured Output
|
|
206
|
-
|
|
207
|
-
```typescript
|
|
208
|
-
// lib/ai/structured.ts
|
|
209
|
-
import { anthropic } from '@/lib/anthropic';
|
|
210
|
-
import { z } from 'zod';
|
|
211
|
-
|
|
212
|
-
const ProductSchema = z.object({
|
|
213
|
-
name: z.string(),
|
|
214
|
-
description: z.string(),
|
|
215
|
-
price: z.number(),
|
|
216
|
-
category: z.string(),
|
|
217
|
-
});
|
|
218
|
-
|
|
219
|
-
export async function extractProduct(text: string) {
|
|
220
|
-
const response = await anthropic.messages.create({
|
|
221
|
-
model: 'claude-sonnet-4-20250514',
|
|
222
|
-
max_tokens: 1024,
|
|
223
|
-
messages: [
|
|
224
|
-
{
|
|
225
|
-
role: 'user',
|
|
226
|
-
content: `Extract product information from this text and return as JSON:
|
|
227
|
-
|
|
228
|
-
${text}
|
|
229
|
-
|
|
230
|
-
Return JSON with: name, description, price, category`,
|
|
231
|
-
},
|
|
232
|
-
],
|
|
233
|
-
});
|
|
234
|
-
|
|
235
|
-
const content = response.content[0];
|
|
236
|
-
if (content.type !== 'text') throw new Error('Unexpected response');
|
|
237
|
-
|
|
238
|
-
// Parse JSON from response
|
|
239
|
-
const jsonMatch = content.text.match(/\{[\s\S]*\}/);
|
|
240
|
-
if (!jsonMatch) throw new Error('No JSON found');
|
|
241
|
-
|
|
242
|
-
const parsed = JSON.parse(jsonMatch[0]);
|
|
243
|
-
return ProductSchema.parse(parsed);
|
|
244
|
-
}
|
|
245
|
-
```
|
|
246
|
-
|
|
247
|
-
### Tool Use / Function Calling
|
|
248
|
-
|
|
249
|
-
```typescript
|
|
250
|
-
// lib/ai/tools.ts
|
|
251
|
-
import { anthropic } from '@/lib/anthropic';
|
|
252
|
-
|
|
253
|
-
const tools = [
|
|
254
|
-
{
|
|
255
|
-
name: 'get_weather',
|
|
256
|
-
description: 'Get the current weather for a location',
|
|
257
|
-
input_schema: {
|
|
258
|
-
type: 'object',
|
|
259
|
-
properties: {
|
|
260
|
-
location: {
|
|
261
|
-
type: 'string',
|
|
262
|
-
description: 'City and state, e.g., San Francisco, CA',
|
|
263
|
-
},
|
|
264
|
-
},
|
|
265
|
-
required: ['location'],
|
|
266
|
-
},
|
|
267
|
-
},
|
|
268
|
-
{
|
|
269
|
-
name: 'search_database',
|
|
270
|
-
description: 'Search the product database',
|
|
271
|
-
input_schema: {
|
|
272
|
-
type: 'object',
|
|
273
|
-
properties: {
|
|
274
|
-
query: { type: 'string' },
|
|
275
|
-
limit: { type: 'number', default: 10 },
|
|
276
|
-
},
|
|
277
|
-
required: ['query'],
|
|
278
|
-
},
|
|
279
|
-
},
|
|
280
|
-
];
|
|
281
|
-
|
|
282
|
-
export async function chatWithTools(prompt: string) {
|
|
283
|
-
const response = await anthropic.messages.create({
|
|
284
|
-
model: 'claude-sonnet-4-20250514',
|
|
285
|
-
max_tokens: 1024,
|
|
286
|
-
tools,
|
|
287
|
-
messages: [{ role: 'user', content: prompt }],
|
|
288
|
-
});
|
|
289
|
-
|
|
290
|
-
// Handle tool use
|
|
291
|
-
for (const block of response.content) {
|
|
292
|
-
if (block.type === 'tool_use') {
|
|
293
|
-
const result = await executeTool(block.name, block.input);
|
|
294
|
-
|
|
295
|
-
// Continue conversation with tool result
|
|
296
|
-
return anthropic.messages.create({
|
|
297
|
-
model: 'claude-sonnet-4-20250514',
|
|
298
|
-
max_tokens: 1024,
|
|
299
|
-
messages: [
|
|
300
|
-
{ role: 'user', content: prompt },
|
|
301
|
-
{ role: 'assistant', content: response.content },
|
|
302
|
-
{
|
|
303
|
-
role: 'user',
|
|
304
|
-
content: [
|
|
305
|
-
{
|
|
306
|
-
type: 'tool_result',
|
|
307
|
-
tool_use_id: block.id,
|
|
308
|
-
content: JSON.stringify(result),
|
|
309
|
-
},
|
|
310
|
-
],
|
|
311
|
-
},
|
|
312
|
-
],
|
|
313
|
-
});
|
|
314
|
-
}
|
|
315
|
-
}
|
|
316
|
-
|
|
317
|
-
return response;
|
|
318
|
-
}
|
|
319
|
-
|
|
320
|
-
async function executeTool(name: string, input: any) {
|
|
321
|
-
switch (name) {
|
|
322
|
-
case 'get_weather':
|
|
323
|
-
return { temperature: 72, condition: 'sunny' };
|
|
324
|
-
case 'search_database':
|
|
325
|
-
return await searchProducts(input.query, input.limit);
|
|
326
|
-
default:
|
|
327
|
-
throw new Error(`Unknown tool: ${name}`);
|
|
328
|
-
}
|
|
329
|
-
}
|
|
330
|
-
```
|
|
331
|
-
|
|
332
|
-
### Rate Limiting
|
|
333
|
-
|
|
334
|
-
```typescript
|
|
335
|
-
// lib/ratelimit.ts
|
|
336
|
-
import { Ratelimit } from '@upstash/ratelimit';
|
|
337
|
-
import { Redis } from '@upstash/redis';
|
|
338
|
-
|
|
339
|
-
const redis = new Redis({
|
|
340
|
-
url: process.env.UPSTASH_REDIS_URL!,
|
|
341
|
-
token: process.env.UPSTASH_REDIS_TOKEN!,
|
|
342
|
-
});
|
|
343
|
-
|
|
344
|
-
export const aiRateLimit = new Ratelimit({
|
|
345
|
-
redis,
|
|
346
|
-
limiter: Ratelimit.slidingWindow(10, '1 m'), // 10 requests per minute
|
|
347
|
-
analytics: true,
|
|
348
|
-
});
|
|
349
|
-
|
|
350
|
-
// Usage in API route
|
|
351
|
-
export async function POST(req: Request) {
|
|
352
|
-
const userId = await getUserId(req);
|
|
353
|
-
|
|
354
|
-
const { success, limit, reset, remaining } = await aiRateLimit.limit(userId);
|
|
355
|
-
|
|
356
|
-
if (!success) {
|
|
357
|
-
return Response.json(
|
|
358
|
-
{ error: 'Rate limit exceeded' },
|
|
359
|
-
{
|
|
360
|
-
status: 429,
|
|
361
|
-
headers: {
|
|
362
|
-
'X-RateLimit-Limit': limit.toString(),
|
|
363
|
-
'X-RateLimit-Remaining': remaining.toString(),
|
|
364
|
-
'X-RateLimit-Reset': reset.toString(),
|
|
365
|
-
},
|
|
366
|
-
}
|
|
367
|
-
);
|
|
368
|
-
}
|
|
369
|
-
|
|
370
|
-
// Continue with AI request...
|
|
371
|
-
}
|
|
372
|
-
```
|
|
373
|
-
|
|
374
|
-
## AI Integration Checklist
|
|
375
|
-
|
|
376
|
-
- [ ] API keys configured
|
|
377
|
-
- [ ] Client library set up
|
|
378
|
-
- [ ] Streaming implemented
|
|
379
|
-
- [ ] Error handling in place
|
|
380
|
-
- [ ] Rate limiting configured
|
|
381
|
-
- [ ] Cost tracking enabled
|
|
382
|
-
- [ ] Prompt templates created
|
|
383
|
-
- [ ] Testing with mock responses
|
|
384
|
-
|
|
385
|
-
## Trigger Keywords
|
|
386
|
-
AI, LLM, Claude, Anthropic, OpenAI, GPT, streaming, chat, completion, embedding, RAG, tool use, function calling
|