feat(ai): optimize tool loops, fix deployments, and integrate new onboarding flow
This commit is contained in:
@@ -1,180 +0,0 @@
|
||||
import { z } from 'zod';
|
||||
|
||||
const evidenceArray = z.array(z.string()).default([]);
|
||||
const confidenceValue = z.number().min(0).max(1).default(0);
|
||||
const completionScore = z.number().min(0).max(1).default(0);
|
||||
|
||||
const defaultWeightedString = {
|
||||
description: null as string | null,
|
||||
confidence: 0,
|
||||
evidence: [] as string[],
|
||||
};
|
||||
|
||||
const weightedStringField = z
|
||||
.object({
|
||||
description: z.union([z.string(), z.null()]).default(null),
|
||||
confidence: confidenceValue.default(0),
|
||||
evidence: evidenceArray.default([]),
|
||||
})
|
||||
.default(defaultWeightedString);
|
||||
|
||||
const weightedListItem = z.object({
|
||||
id: z.string(),
|
||||
description: z.string(),
|
||||
confidence: confidenceValue,
|
||||
evidence: evidenceArray,
|
||||
});
|
||||
|
||||
const stageEnum = z.enum([
|
||||
'idea',
|
||||
'prototype',
|
||||
'mvp_in_progress',
|
||||
'live_beta',
|
||||
'live_paid',
|
||||
'unknown',
|
||||
]);
|
||||
|
||||
const severityEnum = z.enum(['low', 'medium', 'high', 'unknown']);
|
||||
const frequencyEnum = z.enum(['rare', 'occasional', 'frequent', 'constant', 'unknown']);
|
||||
const competitorTypeEnum = z.enum(['direct', 'indirect', 'alternative', 'unknown']);
|
||||
const relatedAreaEnum = z.enum(['product', 'tech', 'market', 'business_model', 'other']);
|
||||
const priorityEnum = z.enum(['high', 'medium', 'low']);
|
||||
|
||||
export const ChatExtractionSchema = z.object({
|
||||
project_summary: z.object({
|
||||
working_title: z.union([z.string(), z.null()]).default(null),
|
||||
one_liner: z.union([z.string(), z.null()]).default(null),
|
||||
stage: stageEnum.default('unknown'),
|
||||
overall_confidence: confidenceValue,
|
||||
evidence: evidenceArray,
|
||||
}),
|
||||
product_vision: z.object({
|
||||
problem_statement: weightedStringField,
|
||||
target_outcome: weightedStringField,
|
||||
founder_intent: weightedStringField,
|
||||
completion_score: completionScore,
|
||||
}),
|
||||
target_users: z.object({
|
||||
primary_segment: weightedStringField,
|
||||
segments: z
|
||||
.array(
|
||||
z.object({
|
||||
id: z.string(),
|
||||
description: z.string(),
|
||||
jobs_to_be_done: z.array(z.string()).default([]),
|
||||
environment: z.union([z.string(), z.null()]),
|
||||
confidence: confidenceValue,
|
||||
evidence: evidenceArray,
|
||||
}),
|
||||
)
|
||||
.default([]),
|
||||
completion_score: completionScore,
|
||||
}),
|
||||
problems_and_pains: z.object({
|
||||
problems: z
|
||||
.array(
|
||||
z.object({
|
||||
id: z.string(),
|
||||
description: z.string(),
|
||||
severity: severityEnum,
|
||||
frequency: frequencyEnum,
|
||||
confidence: confidenceValue,
|
||||
evidence: evidenceArray,
|
||||
}),
|
||||
)
|
||||
.default([]),
|
||||
completion_score: completionScore,
|
||||
}),
|
||||
solution_and_features: z.object({
|
||||
core_solution: weightedStringField,
|
||||
core_features: z
|
||||
.array(
|
||||
z.object({
|
||||
id: z.string(),
|
||||
name: z.string(),
|
||||
description: z.string(),
|
||||
is_must_have_for_v1: z.boolean(),
|
||||
confidence: confidenceValue,
|
||||
evidence: evidenceArray,
|
||||
}),
|
||||
)
|
||||
.default([]),
|
||||
nice_to_have_features: z
|
||||
.array(
|
||||
z.object({
|
||||
id: z.string(),
|
||||
name: z.string(),
|
||||
description: z.string(),
|
||||
confidence: confidenceValue,
|
||||
evidence: evidenceArray,
|
||||
}),
|
||||
)
|
||||
.default([]),
|
||||
completion_score: completionScore,
|
||||
}),
|
||||
market_and_competition: z.object({
|
||||
market_category: weightedStringField,
|
||||
competitors: z
|
||||
.array(
|
||||
z.object({
|
||||
id: z.string(),
|
||||
name: z.string(),
|
||||
description: z.string(),
|
||||
type: competitorTypeEnum,
|
||||
confidence: confidenceValue,
|
||||
evidence: evidenceArray,
|
||||
}),
|
||||
)
|
||||
.default([]),
|
||||
differentiation_points: weightedListItem.array().default([]),
|
||||
completion_score: completionScore,
|
||||
}),
|
||||
tech_and_constraints: z.object({
|
||||
stack_mentions: weightedListItem.array().default([]),
|
||||
constraints: weightedListItem.array().default([]),
|
||||
completion_score: completionScore,
|
||||
}),
|
||||
execution_status: z.object({
|
||||
current_stage: weightedStringField,
|
||||
work_done: weightedListItem.array().default([]),
|
||||
work_in_progress: weightedListItem.array().default([]),
|
||||
blocked_items: weightedListItem.array().default([]),
|
||||
completion_score: completionScore,
|
||||
}),
|
||||
goals_and_success: z.object({
|
||||
short_term_goals: weightedListItem.array().default([]),
|
||||
long_term_goals: weightedListItem.array().default([]),
|
||||
success_criteria: weightedListItem.array().default([]),
|
||||
completion_score: completionScore,
|
||||
}),
|
||||
unknowns_and_questions: z.object({
|
||||
unknowns: z
|
||||
.array(
|
||||
z.object({
|
||||
id: z.string(),
|
||||
description: z.string(),
|
||||
related_area: relatedAreaEnum,
|
||||
evidence: evidenceArray,
|
||||
confidence: confidenceValue,
|
||||
}),
|
||||
)
|
||||
.default([]),
|
||||
questions_to_ask_user: z
|
||||
.array(
|
||||
z.object({
|
||||
id: z.string(),
|
||||
question: z.string(),
|
||||
priority: priorityEnum,
|
||||
}),
|
||||
)
|
||||
.default([]),
|
||||
}),
|
||||
summary_scores: z.object({
|
||||
overall_completion: completionScore,
|
||||
overall_confidence: confidenceValue,
|
||||
}),
|
||||
});
|
||||
|
||||
export type ChatExtractionData = z.infer<typeof ChatExtractionSchema>;
|
||||
|
||||
|
||||
@@ -1,42 +0,0 @@
|
||||
import type { LlmClient } from '@/lib/ai/llm-client';
|
||||
import { ChatExtractionSchema } from '@/lib/ai/chat-extraction-types';
|
||||
import type { ChatExtractionData } from '@/lib/ai/chat-extraction-types';
|
||||
import type { KnowledgeItem } from '@/lib/types/knowledge';
|
||||
|
||||
const SYSTEM_PROMPT = `
|
||||
You are the Product Chat Signal Extractor for stalled SaaS projects.
|
||||
- Read the provided transcript carefully.
|
||||
- Extract grounded signals about the product, market, users, execution status, and unknowns.
|
||||
- Never invent data. Use "null" or empty arrays when the transcript lacks information.
|
||||
- Respond with valid JSON that matches the provided schema exactly. Do not include prose or code fences.
|
||||
`.trim();
|
||||
|
||||
export async function runChatExtraction(
|
||||
knowledgeItem: KnowledgeItem,
|
||||
llm: LlmClient,
|
||||
): Promise<ChatExtractionData> {
|
||||
const transcript = knowledgeItem.content.trim();
|
||||
|
||||
const userMessage = `
|
||||
You will analyze the following transcript. Use message references when listing evidence (e.g., msg_1).
|
||||
Focus on actionable product-building insights.
|
||||
|
||||
TRANSCRIPT_START
|
||||
${transcript}
|
||||
TRANSCRIPT_END`.trim();
|
||||
|
||||
return llm.structuredCall<ChatExtractionData>({
|
||||
model: 'gemini',
|
||||
systemPrompt: SYSTEM_PROMPT,
|
||||
messages: [
|
||||
{
|
||||
role: 'user',
|
||||
content: userMessage,
|
||||
},
|
||||
],
|
||||
schema: ChatExtractionSchema,
|
||||
temperature: 0.2,
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -1,38 +0,0 @@
|
||||
/**
|
||||
* Chat Modes and System Prompts
|
||||
*
|
||||
* Defines available chat modes and maps them to their system prompts.
|
||||
* Prompts are now versioned and managed in separate files under lib/ai/prompts/
|
||||
*/
|
||||
|
||||
import {
|
||||
collectorPrompt,
|
||||
extractionReviewPrompt,
|
||||
visionPrompt,
|
||||
mvpPrompt,
|
||||
marketingPrompt,
|
||||
generalChatPrompt,
|
||||
} from './prompts';
|
||||
|
||||
export type ChatMode =
|
||||
| "collector_mode"
|
||||
| "extraction_review_mode"
|
||||
| "vision_mode"
|
||||
| "mvp_mode"
|
||||
| "marketing_mode"
|
||||
| "general_chat_mode";
|
||||
|
||||
/**
|
||||
* Maps each chat mode to its current active system prompt.
|
||||
*
|
||||
* Prompts are version-controlled in separate files.
|
||||
* To update a prompt or switch versions, edit the corresponding file in lib/ai/prompts/
|
||||
*/
|
||||
export const MODE_SYSTEM_PROMPTS: Record<ChatMode, string> = {
|
||||
collector_mode: collectorPrompt,
|
||||
extraction_review_mode: extractionReviewPrompt,
|
||||
vision_mode: visionPrompt,
|
||||
mvp_mode: mvpPrompt,
|
||||
marketing_mode: marketingPrompt,
|
||||
general_chat_mode: generalChatPrompt,
|
||||
};
|
||||
@@ -1,297 +0,0 @@
|
||||
/**
|
||||
* Text chunking for semantic search
|
||||
*
|
||||
* Splits large documents into smaller, semantically coherent chunks
|
||||
* suitable for vector embedding and retrieval.
|
||||
*/
|
||||
|
||||
export interface TextChunk {
|
||||
/** Index of this chunk (0-based) */
|
||||
index: number;
|
||||
|
||||
/** The chunked text content */
|
||||
text: string;
|
||||
|
||||
/** Approximate token count (for reference) */
|
||||
estimatedTokens: number;
|
||||
}
|
||||
|
||||
export interface ChunkingOptions {
|
||||
/** Target maximum tokens per chunk (approximate) */
|
||||
maxTokens?: number;
|
||||
|
||||
/** Target maximum characters per chunk (fallback if no tokenizer) */
|
||||
maxChars?: number;
|
||||
|
||||
/** Overlap between chunks (in characters) */
|
||||
overlapChars?: number;
|
||||
|
||||
/** Whether to try preserving paragraph boundaries */
|
||||
preserveParagraphs?: boolean;
|
||||
}
|
||||
|
||||
const DEFAULT_OPTIONS: Required<ChunkingOptions> = {
|
||||
maxTokens: 800,
|
||||
maxChars: 3000, // Rough approximation: ~4 chars per token
|
||||
overlapChars: 200,
|
||||
preserveParagraphs: true,
|
||||
};
|
||||
|
||||
/**
|
||||
* Estimate token count from character count
|
||||
*
|
||||
* Uses a rough heuristic: 1 token ≈ 4 characters for English text.
|
||||
* For more accuracy, integrate a real tokenizer (e.g., tiktoken).
|
||||
*/
|
||||
function estimateTokens(text: string): number {
|
||||
return Math.ceil(text.length / 4);
|
||||
}
|
||||
|
||||
/**
|
||||
* Split text into paragraphs, preserving empty lines as separators
|
||||
*/
|
||||
function splitIntoParagraphs(text: string): string[] {
|
||||
return text.split(/\n\n+/).filter((p) => p.trim().length > 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Split text into sentences (simple heuristic)
|
||||
*/
|
||||
function splitIntoSentences(text: string): string[] {
|
||||
// Simple sentence boundary detection
|
||||
return text
|
||||
.split(/[.!?]+\s+/)
|
||||
.map((s) => s.trim())
|
||||
.filter((s) => s.length > 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Chunk text into semantic pieces suitable for embedding
|
||||
*
|
||||
* Strategy:
|
||||
* 1. Split by paragraphs (if preserveParagraphs = true)
|
||||
* 2. Group paragraphs/sentences until reaching maxTokens/maxChars
|
||||
* 3. Add overlap between chunks for context continuity
|
||||
*
|
||||
* @param content - Text to chunk
|
||||
* @param options - Chunking options
|
||||
* @returns Array of text chunks with metadata
|
||||
*
|
||||
* @example
|
||||
* ```typescript
|
||||
* const chunks = chunkText(longDocument, { maxTokens: 500, overlapChars: 100 });
|
||||
* for (const chunk of chunks) {
|
||||
* console.log(`Chunk ${chunk.index}: ${chunk.estimatedTokens} tokens`);
|
||||
* await embedText(chunk.text);
|
||||
* }
|
||||
* ```
|
||||
*/
|
||||
export function chunkText(
|
||||
content: string,
|
||||
options: ChunkingOptions = {}
|
||||
): TextChunk[] {
|
||||
const opts = { ...DEFAULT_OPTIONS, ...options };
|
||||
const chunks: TextChunk[] = [];
|
||||
|
||||
if (!content || content.trim().length === 0) {
|
||||
return chunks;
|
||||
}
|
||||
|
||||
// Clean up content
|
||||
const cleanedContent = content.trim();
|
||||
|
||||
// If content is small enough, return as single chunk
|
||||
if (estimateTokens(cleanedContent) <= opts.maxTokens) {
|
||||
return [
|
||||
{
|
||||
index: 0,
|
||||
text: cleanedContent,
|
||||
estimatedTokens: estimateTokens(cleanedContent),
|
||||
},
|
||||
];
|
||||
}
|
||||
|
||||
// Split into paragraphs or sentences
|
||||
const units = opts.preserveParagraphs
|
||||
? splitIntoParagraphs(cleanedContent)
|
||||
: splitIntoSentences(cleanedContent);
|
||||
|
||||
if (units.length === 0) {
|
||||
return [
|
||||
{
|
||||
index: 0,
|
||||
text: cleanedContent,
|
||||
estimatedTokens: estimateTokens(cleanedContent),
|
||||
},
|
||||
];
|
||||
}
|
||||
|
||||
let currentChunk = '';
|
||||
let chunkIndex = 0;
|
||||
let previousOverlap = '';
|
||||
|
||||
for (let i = 0; i < units.length; i++) {
|
||||
const unit = units[i];
|
||||
const potentialChunk = currentChunk
|
||||
? `${currentChunk}\n\n${unit}`
|
||||
: `${previousOverlap}${unit}`;
|
||||
|
||||
const potentialTokens = estimateTokens(potentialChunk);
|
||||
const potentialChars = potentialChunk.length;
|
||||
|
||||
// Check if adding this unit would exceed limits
|
||||
if (
|
||||
potentialTokens > opts.maxTokens ||
|
||||
potentialChars > opts.maxChars
|
||||
) {
|
||||
// Save current chunk if it has content
|
||||
if (currentChunk.length > 0) {
|
||||
chunks.push({
|
||||
index: chunkIndex++,
|
||||
text: currentChunk,
|
||||
estimatedTokens: estimateTokens(currentChunk),
|
||||
});
|
||||
|
||||
// Prepare overlap for next chunk
|
||||
const overlapStart = Math.max(
|
||||
0,
|
||||
currentChunk.length - opts.overlapChars
|
||||
);
|
||||
previousOverlap = currentChunk.substring(overlapStart);
|
||||
if (previousOverlap.length > 0 && !previousOverlap.endsWith(' ')) {
|
||||
// Try to start overlap at a word boundary
|
||||
const spaceIndex = previousOverlap.indexOf(' ');
|
||||
if (spaceIndex > 0) {
|
||||
previousOverlap = previousOverlap.substring(spaceIndex + 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Start new chunk with current unit
|
||||
currentChunk = `${previousOverlap}${unit}`;
|
||||
} else {
|
||||
// Add unit to current chunk
|
||||
currentChunk = potentialChunk;
|
||||
}
|
||||
}
|
||||
|
||||
// Add final chunk if it has content
|
||||
if (currentChunk.length > 0) {
|
||||
chunks.push({
|
||||
index: chunkIndex++,
|
||||
text: currentChunk,
|
||||
estimatedTokens: estimateTokens(currentChunk),
|
||||
});
|
||||
}
|
||||
|
||||
console.log(
|
||||
`[Chunking] Split ${cleanedContent.length} chars into ${chunks.length} chunks`
|
||||
);
|
||||
|
||||
return chunks;
|
||||
}
|
||||
|
||||
/**
|
||||
* Chunk text with code-aware splitting
|
||||
*
|
||||
* Preserves code blocks and tries to keep them intact.
|
||||
* Useful for chunking AI chat transcripts that contain code snippets.
|
||||
*/
|
||||
export function chunkTextWithCodeAwareness(
|
||||
content: string,
|
||||
options: ChunkingOptions = {}
|
||||
): TextChunk[] {
|
||||
const opts = { ...DEFAULT_OPTIONS, ...options };
|
||||
|
||||
// Detect code blocks (triple backticks)
|
||||
const codeBlockRegex = /```[\s\S]*?```/g;
|
||||
const codeBlocks: { start: number; end: number; content: string }[] = [];
|
||||
let match;
|
||||
|
||||
while ((match = codeBlockRegex.exec(content)) !== null) {
|
||||
codeBlocks.push({
|
||||
start: match.index,
|
||||
end: match.index + match[0].length,
|
||||
content: match[0],
|
||||
});
|
||||
}
|
||||
|
||||
// If no code blocks, use standard chunking
|
||||
if (codeBlocks.length === 0) {
|
||||
return chunkText(content, options);
|
||||
}
|
||||
|
||||
// Split content around code blocks
|
||||
const chunks: TextChunk[] = [];
|
||||
let chunkIndex = 0;
|
||||
let currentPosition = 0;
|
||||
|
||||
for (const codeBlock of codeBlocks) {
|
||||
// Chunk text before code block
|
||||
const textBefore = content.substring(currentPosition, codeBlock.start);
|
||||
if (textBefore.trim().length > 0) {
|
||||
const textChunks = chunkText(textBefore, opts);
|
||||
for (const chunk of textChunks) {
|
||||
chunks.push({
|
||||
...chunk,
|
||||
index: chunkIndex++,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// Add code block as its own chunk (or split if too large)
|
||||
const codeTokens = estimateTokens(codeBlock.content);
|
||||
if (codeTokens <= opts.maxTokens) {
|
||||
chunks.push({
|
||||
index: chunkIndex++,
|
||||
text: codeBlock.content,
|
||||
estimatedTokens: codeTokens,
|
||||
});
|
||||
} else {
|
||||
// Code block is too large, split by lines
|
||||
const codeLines = codeBlock.content.split('\n');
|
||||
let currentCodeChunk = '';
|
||||
for (const line of codeLines) {
|
||||
const potentialChunk = currentCodeChunk
|
||||
? `${currentCodeChunk}\n${line}`
|
||||
: line;
|
||||
if (estimateTokens(potentialChunk) > opts.maxTokens) {
|
||||
if (currentCodeChunk.length > 0) {
|
||||
chunks.push({
|
||||
index: chunkIndex++,
|
||||
text: currentCodeChunk,
|
||||
estimatedTokens: estimateTokens(currentCodeChunk),
|
||||
});
|
||||
}
|
||||
currentCodeChunk = line;
|
||||
} else {
|
||||
currentCodeChunk = potentialChunk;
|
||||
}
|
||||
}
|
||||
if (currentCodeChunk.length > 0) {
|
||||
chunks.push({
|
||||
index: chunkIndex++,
|
||||
text: currentCodeChunk,
|
||||
estimatedTokens: estimateTokens(currentCodeChunk),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
currentPosition = codeBlock.end;
|
||||
}
|
||||
|
||||
// Chunk remaining text after last code block
|
||||
const textAfter = content.substring(currentPosition);
|
||||
if (textAfter.trim().length > 0) {
|
||||
const textChunks = chunkText(textAfter, opts);
|
||||
for (const chunk of textChunks) {
|
||||
chunks.push({
|
||||
...chunk,
|
||||
index: chunkIndex++,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
return chunks;
|
||||
}
|
||||
|
||||
@@ -1,173 +0,0 @@
|
||||
/**
|
||||
* Embedding generation using Gemini API
|
||||
*
|
||||
* Converts text into vector embeddings for semantic search.
|
||||
*/
|
||||
|
||||
import { GoogleGenerativeAI } from '@google/generative-ai';
|
||||
|
||||
const GEMINI_API_KEY = process.env.GEMINI_API_KEY;
|
||||
|
||||
if (!GEMINI_API_KEY) {
|
||||
console.warn('[Embeddings] GEMINI_API_KEY not set - embedding functions will fail');
|
||||
}
|
||||
|
||||
const genAI = GEMINI_API_KEY ? new GoogleGenerativeAI(GEMINI_API_KEY) : null;
|
||||
|
||||
// Gemini embedding model - text-embedding-004 produces 768-dim embeddings
|
||||
// Adjust EMBEDDING_DIMENSION in knowledge-chunks-schema.sql if using different model
|
||||
const EMBEDDING_MODEL = 'text-embedding-004';
|
||||
const EMBEDDING_DIMENSION = 768;
|
||||
|
||||
/**
|
||||
* Generate embedding for a single text string
|
||||
*
|
||||
* @param text - Input text to embed
|
||||
* @returns Vector embedding as array of numbers
|
||||
*
|
||||
* @throws Error if Gemini API is not configured or request fails
|
||||
*/
|
||||
export async function embedText(text: string): Promise<number[]> {
|
||||
if (!genAI) {
|
||||
throw new Error('GEMINI_API_KEY not configured - cannot generate embeddings');
|
||||
}
|
||||
|
||||
if (!text || text.trim().length === 0) {
|
||||
throw new Error('Cannot embed empty text');
|
||||
}
|
||||
|
||||
try {
|
||||
const model = genAI.getGenerativeModel({ model: EMBEDDING_MODEL });
|
||||
const result = await model.embedContent(text);
|
||||
const embedding = result.embedding;
|
||||
|
||||
if (!embedding || !embedding.values || embedding.values.length === 0) {
|
||||
throw new Error('Gemini returned empty embedding');
|
||||
}
|
||||
|
||||
// Verify dimension matches expectation
|
||||
if (embedding.values.length !== EMBEDDING_DIMENSION) {
|
||||
console.warn(
|
||||
`[Embeddings] Unexpected dimension: got ${embedding.values.length}, expected ${EMBEDDING_DIMENSION}`
|
||||
);
|
||||
}
|
||||
|
||||
return embedding.values;
|
||||
} catch (error) {
|
||||
console.error('[Embeddings] Failed to embed text:', error);
|
||||
throw new Error(
|
||||
`Embedding generation failed: ${error instanceof Error ? error.message : String(error)}`
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate embeddings for multiple texts in batch
|
||||
*
|
||||
* More efficient than calling embedText() repeatedly.
|
||||
* Processes texts sequentially to avoid rate limiting.
|
||||
*
|
||||
* @param texts - Array of texts to embed
|
||||
* @param options - Batch processing options
|
||||
* @returns Array of embeddings (same order as input texts)
|
||||
*
|
||||
* @example
|
||||
* ```typescript
|
||||
* const chunks = ["First chunk...", "Second chunk...", "Third chunk..."];
|
||||
* const embeddings = await embedTextBatch(chunks);
|
||||
* // embeddings[0] corresponds to chunks[0], etc.
|
||||
* ```
|
||||
*/
|
||||
export async function embedTextBatch(
|
||||
texts: string[],
|
||||
options: { delayMs?: number; skipEmpty?: boolean } = {}
|
||||
): Promise<number[][]> {
|
||||
const { delayMs = 100, skipEmpty = true } = options;
|
||||
|
||||
if (texts.length === 0) {
|
||||
return [];
|
||||
}
|
||||
|
||||
const embeddings: number[][] = [];
|
||||
|
||||
for (let i = 0; i < texts.length; i++) {
|
||||
const text = texts[i];
|
||||
|
||||
// Skip empty texts if requested
|
||||
if (skipEmpty && (!text || text.trim().length === 0)) {
|
||||
console.warn(`[Embeddings] Skipping empty text at index ${i}`);
|
||||
embeddings.push(new Array(EMBEDDING_DIMENSION).fill(0)); // Zero vector for empty
|
||||
continue;
|
||||
}
|
||||
|
||||
try {
|
||||
const embedding = await embedText(text);
|
||||
embeddings.push(embedding);
|
||||
|
||||
// Add delay between requests to avoid rate limiting (except for last item)
|
||||
if (i < texts.length - 1 && delayMs > 0) {
|
||||
await new Promise((resolve) => setTimeout(resolve, delayMs));
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(`[Embeddings] Failed to embed text at index ${i}:`, error);
|
||||
// Push zero vector as fallback
|
||||
embeddings.push(new Array(EMBEDDING_DIMENSION).fill(0));
|
||||
}
|
||||
}
|
||||
|
||||
console.log(`[Embeddings] Generated ${embeddings.length} embeddings`);
|
||||
|
||||
return embeddings;
|
||||
}
|
||||
|
||||
/**
|
||||
* Compute cosine similarity between two embeddings
|
||||
*
|
||||
* @param a - First embedding vector
|
||||
* @param b - Second embedding vector
|
||||
* @returns Cosine similarity score (0-1, higher = more similar)
|
||||
*/
|
||||
export function cosineSimilarity(a: number[], b: number[]): number {
|
||||
if (a.length !== b.length) {
|
||||
throw new Error('Embedding dimensions do not match');
|
||||
}
|
||||
|
||||
let dotProduct = 0;
|
||||
let normA = 0;
|
||||
let normB = 0;
|
||||
|
||||
for (let i = 0; i < a.length; i++) {
|
||||
dotProduct += a[i] * b[i];
|
||||
normA += a[i] * a[i];
|
||||
normB += b[i] * b[i];
|
||||
}
|
||||
|
||||
const magnitude = Math.sqrt(normA) * Math.sqrt(normB);
|
||||
|
||||
if (magnitude === 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return dotProduct / magnitude;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the expected embedding dimension for the current model
|
||||
*/
|
||||
export function getEmbeddingDimension(): number {
|
||||
return EMBEDDING_DIMENSION;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if embeddings API is configured and working
|
||||
*/
|
||||
export async function checkEmbeddingsHealth(): Promise<boolean> {
|
||||
try {
|
||||
const testEmbedding = await embedText('health check');
|
||||
return testEmbedding.length === EMBEDDING_DIMENSION;
|
||||
} catch (error) {
|
||||
console.error('[Embeddings Health Check] Failed:', error);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,70 +0,0 @@
|
||||
import { z } from 'zod';
|
||||
import type { LlmClient } from '@/lib/ai/llm-client';
|
||||
import { GeminiLlmClient } from '@/lib/ai/gemini-client';
|
||||
import { clamp, nowIso, loadPhaseContainers, persistPhaseArtifacts } from '@/lib/server/projects';
|
||||
import type { MarketingModel } from '@/lib/types/marketing';
|
||||
|
||||
const HomepageMessagingSchema = z.object({
|
||||
headline: z.string().nullable(),
|
||||
subheadline: z.string().nullable(),
|
||||
bullets: z.array(z.string()).default([]),
|
||||
});
|
||||
|
||||
const MarketingModelSchema = z.object({
|
||||
projectId: z.string(),
|
||||
icp: z.array(z.string()).default([]),
|
||||
positioning: z.string().nullable(),
|
||||
homepageMessaging: HomepageMessagingSchema,
|
||||
initialChannels: z.array(z.string()).default([]),
|
||||
launchAngles: z.array(z.string()).default([]),
|
||||
overallConfidence: z.number().min(0).max(1),
|
||||
});
|
||||
|
||||
export async function runMarketingPlanning(
|
||||
projectId: string,
|
||||
llmClient?: LlmClient,
|
||||
): Promise<MarketingModel> {
|
||||
const { phaseData } = await loadPhaseContainers(projectId);
|
||||
const canonical = phaseData.canonicalProductModel;
|
||||
if (!canonical) {
|
||||
throw new Error('Canonical product model missing. Run buildCanonicalProductModel first.');
|
||||
}
|
||||
|
||||
const llm = llmClient ?? new GeminiLlmClient();
|
||||
const systemPrompt =
|
||||
'You are a SaaS marketing strategist. Given the canonical product model, produce ICP, positioning, homepage messaging, and launch ideas as strict JSON.';
|
||||
|
||||
const marketing = await llm.structuredCall<MarketingModel>({
|
||||
model: 'gemini',
|
||||
systemPrompt,
|
||||
messages: [
|
||||
{
|
||||
role: 'user',
|
||||
content: [
|
||||
'Canonical product model JSON:',
|
||||
'```json',
|
||||
JSON.stringify(canonical, null, 2),
|
||||
'```',
|
||||
'Respond ONLY with valid JSON that matches the required schema.',
|
||||
].join('\n'),
|
||||
},
|
||||
],
|
||||
schema: MarketingModelSchema,
|
||||
temperature: 0.2,
|
||||
});
|
||||
|
||||
await persistPhaseArtifacts(projectId, (phaseData, phaseScores, phaseHistory) => {
|
||||
phaseData.marketingPlan = marketing;
|
||||
phaseScores.marketing = {
|
||||
overallCompletion: clamp(marketing.homepageMessaging.bullets.length ? 0.7 : 0.5),
|
||||
overallConfidence: marketing.overallConfidence,
|
||||
updatedAt: nowIso(),
|
||||
};
|
||||
phaseHistory.push({ phase: 'marketing', status: 'completed', timestamp: nowIso() });
|
||||
return { phaseData, phaseScores, phaseHistory, nextPhase: 'marketing_ready' };
|
||||
});
|
||||
|
||||
return marketing;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,62 +0,0 @@
|
||||
import { z } from 'zod';
|
||||
import type { LlmClient } from '@/lib/ai/llm-client';
|
||||
import { GeminiLlmClient } from '@/lib/ai/gemini-client';
|
||||
import { clamp, nowIso, loadPhaseContainers, persistPhaseArtifacts } from '@/lib/server/projects';
|
||||
import type { MvpPlan } from '@/lib/types/mvp';
|
||||
|
||||
const MvpPlanSchema = z.object({
|
||||
projectId: z.string(),
|
||||
coreFlows: z.array(z.string()).default([]),
|
||||
coreFeatures: z.array(z.string()).default([]),
|
||||
supportingFeatures: z.array(z.string()).default([]),
|
||||
outOfScope: z.array(z.string()).default([]),
|
||||
technicalTasks: z.array(z.string()).default([]),
|
||||
blockers: z.array(z.string()).default([]),
|
||||
overallConfidence: z.number().min(0).max(1),
|
||||
});
|
||||
|
||||
export async function runMvpPlanning(projectId: string, llmClient?: LlmClient): Promise<MvpPlan> {
|
||||
const { phaseData } = await loadPhaseContainers(projectId);
|
||||
const canonical = phaseData.canonicalProductModel;
|
||||
if (!canonical) {
|
||||
throw new Error('Canonical product model missing. Run buildCanonicalProductModel first.');
|
||||
}
|
||||
|
||||
const llm = llmClient ?? new GeminiLlmClient();
|
||||
const systemPrompt =
|
||||
'You are an expert SaaS product manager. Given the canonical product model, produce the smallest sellable MVP plan as strict JSON.';
|
||||
|
||||
const plan = await llm.structuredCall<MvpPlan>({
|
||||
model: 'gemini',
|
||||
systemPrompt,
|
||||
messages: [
|
||||
{
|
||||
role: 'user',
|
||||
content: [
|
||||
'Canonical product model JSON:',
|
||||
'```json',
|
||||
JSON.stringify(canonical, null, 2),
|
||||
'```',
|
||||
'Respond ONLY with JSON that matches the required schema.',
|
||||
].join('\n'),
|
||||
},
|
||||
],
|
||||
schema: MvpPlanSchema,
|
||||
temperature: 0.2,
|
||||
});
|
||||
|
||||
await persistPhaseArtifacts(projectId, (phaseData, phaseScores, phaseHistory) => {
|
||||
phaseData.mvpPlan = plan;
|
||||
phaseScores.mvp = {
|
||||
overallCompletion: clamp(plan.coreFeatures.length ? 0.8 : 0.5),
|
||||
overallConfidence: plan.overallConfidence,
|
||||
updatedAt: nowIso(),
|
||||
};
|
||||
phaseHistory.push({ phase: 'mvp', status: 'completed', timestamp: nowIso() });
|
||||
return { phaseData, phaseScores, phaseHistory, nextPhase: 'mvp_ready' };
|
||||
});
|
||||
|
||||
return plan;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,266 +0,0 @@
|
||||
/**
|
||||
* Fire-and-forget plan extraction from chat conversations.
|
||||
*
|
||||
* After each chat turn, we call a cheap Gemini model (Flash) to scan the
|
||||
* conversation for plan-worthy content — new tasks, decisions, vision updates —
|
||||
* and auto-persist them via the same `fs_projects.data->plan` path used by
|
||||
* the Plan tab MCP tools.
|
||||
*
|
||||
* The cheap model is configured via VIBN_CHEAP_MODEL (default: gemini-3.1-pro-preview).
|
||||
*/
|
||||
|
||||
import { query } from "@/lib/db-postgres";
|
||||
|
||||
const GEMINI_API_KEY = process.env.GOOGLE_API_KEY || "";
|
||||
const CHEAP_MODEL =
|
||||
process.env.VIBN_CHEAP_MODEL || "gemini-3.1-pro-preview";
|
||||
const GEMINI_BASE_URL = "https://generativelanguage.googleapis.com/v1beta";
|
||||
|
||||
interface PlanExtraction {
|
||||
tasks: Array<{ title: string; description?: string }>;
|
||||
decisions: Array<{ title: string; choice: string; why?: string }>;
|
||||
visionUpdate?: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Call the cheap Gemini model to extract plan updates from the transcript.
|
||||
*/
|
||||
async function extractPlanFromTranscript(
|
||||
transcript: string,
|
||||
): Promise<PlanExtraction | null> {
|
||||
const url = `${GEMINI_BASE_URL}/models/${CHEAP_MODEL}:generateContent?key=${GEMINI_API_KEY}`;
|
||||
|
||||
const body = {
|
||||
contents: [
|
||||
{
|
||||
role: "user",
|
||||
parts: [
|
||||
{
|
||||
text:
|
||||
"Extract any plan-worthy content from this AI coding conversation. " +
|
||||
"Return ONLY valid JSON with this schema:\n" +
|
||||
'{\n "tasks": [{"title": "short task name", "description": "optional details"}],\n' +
|
||||
' "decisions": [{"title": "what was decided", "choice": "the chosen option", "why": "reasoning"}],\n' +
|
||||
' "visionUpdate": "updated product vision (only if the conversation meaningfully changes or clarifies it)"\n' +
|
||||
"}\n\n" +
|
||||
"Rules:\n" +
|
||||
"- Only extract CLEAR tasks the AI committed to do or the user explicitly requested.\n" +
|
||||
"- Only extract NON-TRIVIAL decisions (not 'I'll read that file').\n" +
|
||||
"- visionUpdate: set ONLY when the user articulates or refines their product vision. Omit entirely if not.\n" +
|
||||
"- Return empty arrays if nothing worthy found.\n" +
|
||||
"- Do NOT wrap in markdown code fences. Just the raw JSON.\n\n" +
|
||||
"Conversation:\n" +
|
||||
transcript.slice(0, 12000),
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
generationConfig: { temperature: 0.1, maxOutputTokens: 1024 },
|
||||
};
|
||||
|
||||
let res: Response;
|
||||
try {
|
||||
res = await fetch(url, {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify(body),
|
||||
});
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
|
||||
const data = await res.json().catch(() => ({}));
|
||||
if (!res.ok) return null;
|
||||
|
||||
const text = data?.candidates?.[0]?.content?.parts?.[0]?.text || "";
|
||||
if (!text.trim()) return null;
|
||||
|
||||
try {
|
||||
return JSON.parse(text.trim()) as PlanExtraction;
|
||||
} catch {
|
||||
// Strip markdown code fences if present
|
||||
const cleaned = text.replace(/```(?:json)?\s*/g, "").trim();
|
||||
try {
|
||||
return JSON.parse(cleaned) as PlanExtraction;
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function planNewId(): string {
|
||||
return `plan_${Date.now().toString(36)}_${Math.random().toString(36).slice(2, 8)}`;
|
||||
}
|
||||
|
||||
interface PlanProject {
|
||||
id: string;
|
||||
data: Record<string, unknown>;
|
||||
}
|
||||
|
||||
async function loadPlanProject(
|
||||
projectId: string,
|
||||
): Promise<PlanProject | null> {
|
||||
const rows = await query<PlanProject>(
|
||||
`SELECT id, data FROM fs_projects WHERE id = $1 LIMIT 1`,
|
||||
[projectId],
|
||||
);
|
||||
return rows[0] ?? null;
|
||||
}
|
||||
|
||||
interface PlanTask {
|
||||
id: string;
|
||||
title: string;
|
||||
description?: string;
|
||||
status: "open" | "in_progress" | "review" | "done" | "blocked";
|
||||
text?: string;
|
||||
createdAt: string;
|
||||
}
|
||||
|
||||
interface PlanDecision {
|
||||
id: string;
|
||||
title: string;
|
||||
choice: string;
|
||||
why?: string;
|
||||
createdAt: string;
|
||||
}
|
||||
|
||||
interface PlanShape {
|
||||
vision?: string;
|
||||
ideas: Array<{ id: string; text: string; createdAt: string }>;
|
||||
tasks: PlanTask[];
|
||||
decisions: PlanDecision[];
|
||||
}
|
||||
|
||||
function readPlanFromData(data: Record<string, unknown>): PlanShape {
|
||||
const raw = (data?.plan as Record<string, unknown>) ?? {};
|
||||
const ideas = Array.isArray(raw.ideas) ? raw.ideas : [];
|
||||
const tasks = Array.isArray(raw.tasks)
|
||||
? (raw.tasks as PlanTask[]).map((t) => ({
|
||||
...t,
|
||||
id: String(t.id ?? planNewId()),
|
||||
title: String(t.title ?? t.text ?? "").trim(),
|
||||
status: t.status ?? "open",
|
||||
createdAt: String(t.createdAt ?? new Date().toISOString()),
|
||||
}))
|
||||
: [];
|
||||
const decisions = Array.isArray(raw.decisions)
|
||||
? (raw.decisions as PlanDecision[]).map((d) => ({
|
||||
...d,
|
||||
id: String(d.id ?? planNewId()),
|
||||
title: String(d.title ?? "").trim(),
|
||||
choice: String(d.choice ?? "").trim(),
|
||||
createdAt: String(d.createdAt ?? new Date().toISOString()),
|
||||
}))
|
||||
: [];
|
||||
return {
|
||||
vision: typeof raw.vision === "string" ? raw.vision : undefined,
|
||||
ideas,
|
||||
tasks,
|
||||
decisions,
|
||||
};
|
||||
}
|
||||
|
||||
async function writePlan(
|
||||
projectId: string,
|
||||
plan: PlanShape,
|
||||
alsoVision?: string,
|
||||
): Promise<void> {
|
||||
const serialized = {
|
||||
vision: plan.vision,
|
||||
ideas: plan.ideas,
|
||||
tasks: plan.tasks,
|
||||
decisions: plan.decisions,
|
||||
};
|
||||
if (alsoVision !== undefined) {
|
||||
await query(
|
||||
`UPDATE fs_projects
|
||||
SET data = data || jsonb_build_object('plan', $2::jsonb, 'productVision', $3::text),
|
||||
updated_at = NOW()
|
||||
WHERE id = $1`,
|
||||
[projectId, JSON.stringify(serialized), alsoVision],
|
||||
);
|
||||
} else {
|
||||
await query(
|
||||
`UPDATE fs_projects
|
||||
SET data = data || jsonb_build_object('plan', $2::jsonb),
|
||||
updated_at = NOW()
|
||||
WHERE id = $1`,
|
||||
[projectId, JSON.stringify(serialized)],
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Main entry point: scan the conversation transcript and auto-update the
|
||||
* project plan with any extracted tasks/decisions/vision.
|
||||
*
|
||||
* Called fire-and-forget after each chat turn. Never throws.
|
||||
*/
|
||||
export async function autoExtractPlanUpdates(
|
||||
projectId: string,
|
||||
transcript: string,
|
||||
): Promise<{ tasks: number; decisions: number; vision: boolean } | null> {
|
||||
if (!projectId || transcript.length < 20) return null;
|
||||
|
||||
try {
|
||||
const extraction = await extractPlanFromTranscript(transcript);
|
||||
if (!extraction) return null;
|
||||
|
||||
const hasTasks = extraction.tasks?.length > 0;
|
||||
const hasDecisions = extraction.decisions?.length > 0;
|
||||
const hasVision =
|
||||
typeof extraction.visionUpdate === "string" &&
|
||||
extraction.visionUpdate.trim().length > 0;
|
||||
|
||||
if (!hasTasks && !hasDecisions && !hasVision) return null;
|
||||
|
||||
const project = await loadPlanProject(projectId);
|
||||
if (!project) return null;
|
||||
|
||||
const plan = readPlanFromData(project.data);
|
||||
const now = new Date().toISOString();
|
||||
let taskCount = 0;
|
||||
let decisionCount = 0;
|
||||
|
||||
for (const t of extraction.tasks ?? []) {
|
||||
const exists = plan.tasks.some(
|
||||
(existing) => existing.title.toLowerCase() === t.title.toLowerCase(),
|
||||
);
|
||||
if (exists) continue;
|
||||
plan.tasks.unshift({
|
||||
id: planNewId(),
|
||||
title: t.title.trim(),
|
||||
description: t.description?.trim(),
|
||||
status: "open",
|
||||
createdAt: now,
|
||||
});
|
||||
taskCount++;
|
||||
}
|
||||
|
||||
for (const d of extraction.decisions ?? []) {
|
||||
const exists = plan.decisions.some(
|
||||
(existing) => existing.title.toLowerCase() === d.title.toLowerCase(),
|
||||
);
|
||||
if (exists) continue;
|
||||
plan.decisions.unshift({
|
||||
id: planNewId(),
|
||||
title: d.title.trim(),
|
||||
choice: d.choice.trim(),
|
||||
why: d.why?.trim(),
|
||||
createdAt: now,
|
||||
});
|
||||
decisionCount++;
|
||||
}
|
||||
|
||||
if (hasVision && extraction.visionUpdate) {
|
||||
plan.vision = extraction.visionUpdate.trim();
|
||||
}
|
||||
|
||||
if (taskCount === 0 && decisionCount === 0 && !hasVision) return null;
|
||||
|
||||
await writePlan(projectId, plan, hasVision ? extraction.visionUpdate : undefined);
|
||||
return { tasks: taskCount, decisions: decisionCount, vision: hasVision };
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -1,176 +0,0 @@
|
||||
# Prompt Management System
|
||||
|
||||
This directory contains all versioned system prompts for Vibn's chat modes.
|
||||
|
||||
## 📁 Structure
|
||||
|
||||
```
|
||||
prompts/
|
||||
├── index.ts # Exports all prompts
|
||||
├── shared.ts # Shared prompt components
|
||||
├── collector.ts # Collector mode prompts
|
||||
├── extraction-review.ts # Extraction review mode prompts
|
||||
├── vision.ts # Vision mode prompts
|
||||
├── mvp.ts # MVP mode prompts
|
||||
├── marketing.ts # Marketing mode prompts
|
||||
└── general-chat.ts # General chat mode prompts
|
||||
```
|
||||
|
||||
## 🔄 Versioning
|
||||
|
||||
Each prompt file contains:
|
||||
1. **Version history** - All versions of the prompt
|
||||
2. **Metadata** - Version number, date, description
|
||||
3. **Current version** - Which version is active
|
||||
|
||||
### Example Structure
|
||||
|
||||
```typescript
|
||||
const COLLECTOR_V1: PromptVersion = {
|
||||
version: 'v1',
|
||||
createdAt: '2024-11-17',
|
||||
description: 'Initial version',
|
||||
prompt: `...`,
|
||||
};
|
||||
|
||||
const COLLECTOR_V2: PromptVersion = {
|
||||
version: 'v2',
|
||||
createdAt: '2024-12-01',
|
||||
description: 'Added context-aware chunking',
|
||||
prompt: `...`,
|
||||
};
|
||||
|
||||
export const collectorPrompts = {
|
||||
v1: COLLECTOR_V1,
|
||||
v2: COLLECTOR_V2,
|
||||
current: 'v2', // ← Active version
|
||||
};
|
||||
```
|
||||
|
||||
## 📝 How to Add a New Prompt Version
|
||||
|
||||
1. **Open the relevant mode file** (e.g., `collector.ts`)
|
||||
2. **Create a new version constant:**
|
||||
```typescript
|
||||
const COLLECTOR_V2: PromptVersion = {
|
||||
version: 'v2',
|
||||
createdAt: '2024-12-01',
|
||||
description: 'What changed in this version',
|
||||
prompt: `
|
||||
Your new prompt text here...
|
||||
`,
|
||||
};
|
||||
```
|
||||
3. **Add to the prompts object:**
|
||||
```typescript
|
||||
export const collectorPrompts = {
|
||||
v1: COLLECTOR_V1,
|
||||
v2: COLLECTOR_V2, // Add new version
|
||||
current: 'v2', // Update current
|
||||
};
|
||||
```
|
||||
4. **Done!** The system will automatically use the new version.
|
||||
|
||||
## 🔙 How to Rollback
|
||||
|
||||
Simply change the `current` field:
|
||||
|
||||
```typescript
|
||||
export const collectorPrompts = {
|
||||
v1: COLLECTOR_V1,
|
||||
v2: COLLECTOR_V2,
|
||||
current: 'v1', // Rolled back to v1
|
||||
};
|
||||
```
|
||||
|
||||
## 📊 Benefits of This System
|
||||
|
||||
1. **Version History** - Keep all previous prompts for reference
|
||||
2. **Easy Rollback** - Instantly revert to a previous version
|
||||
3. **Git-Friendly** - Clear diffs show exactly what changed
|
||||
4. **Documentation** - Each version has a description of changes
|
||||
5. **A/B Testing Ready** - Can easily test multiple versions
|
||||
6. **Isolated Changes** - Changing one prompt doesn't affect others
|
||||
|
||||
## 🎯 Usage in Code
|
||||
|
||||
```typescript
|
||||
// Import current prompts (most common)
|
||||
import { MODE_SYSTEM_PROMPTS } from '@/lib/ai/chat-modes';
|
||||
|
||||
const prompt = MODE_SYSTEM_PROMPTS['collector_mode'];
|
||||
|
||||
// Or access version history
|
||||
import { collectorPrompts } from '@/lib/ai/prompts';
|
||||
|
||||
console.log(collectorPrompts.v1.prompt); // Old version
|
||||
console.log(collectorPrompts.current); // 'v2'
|
||||
```
|
||||
|
||||
## 🚀 Future Enhancements
|
||||
|
||||
### Analytics Tracking
|
||||
Track performance by prompt version:
|
||||
```typescript
|
||||
await logPromptUsage({
|
||||
mode: 'collector_mode',
|
||||
version: collectorPrompts.current,
|
||||
userId: user.id,
|
||||
responseQuality: 0.85,
|
||||
});
|
||||
```
|
||||
|
||||
### A/B Testing
|
||||
Test multiple versions simultaneously:
|
||||
```typescript
|
||||
const promptVersion = userInExperiment ? 'v2' : 'v1';
|
||||
const prompt = collectorPrompts[promptVersion].prompt;
|
||||
```
|
||||
|
||||
### Database Storage
|
||||
Move to Firestore for dynamic updates:
|
||||
```typescript
|
||||
// Future: Load from database
|
||||
const prompt = await getPrompt('collector_mode', 'latest');
|
||||
```
|
||||
|
||||
## 📚 Best Practices
|
||||
|
||||
1. **Always add a description** - Future you will thank you
|
||||
2. **Never delete old versions** - Keep history for rollback
|
||||
3. **Test before deploying** - Ensure new prompts work as expected
|
||||
4. **Document changes** - What problem does the new version solve?
|
||||
5. **Version incrementally** - Don't skip version numbers
|
||||
|
||||
## 🔍 Example: Adding Context-Aware Chunking
|
||||
|
||||
```typescript
|
||||
// 1. Create new version
|
||||
const COLLECTOR_V2: PromptVersion = {
|
||||
version: 'v2',
|
||||
createdAt: '2024-11-17',
|
||||
description: 'Added instructions for context-aware chunking',
|
||||
prompt: `
|
||||
${COLLECTOR_V1.prompt}
|
||||
|
||||
**Context-Aware Retrieval**:
|
||||
When referencing retrieved chunks, always cite the source document
|
||||
and chunk number for transparency.
|
||||
`,
|
||||
};
|
||||
|
||||
// 2. Update prompts object
|
||||
export const collectorPrompts = {
|
||||
v1: COLLECTOR_V1,
|
||||
v2: COLLECTOR_V2,
|
||||
current: 'v2',
|
||||
};
|
||||
|
||||
// 3. Deploy and monitor
|
||||
// If issues arise, simply change current: 'v1' to rollback
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
**Questions?** Check the code in any prompt file for examples.
|
||||
|
||||
@@ -1,318 +0,0 @@
|
||||
/**
|
||||
* Collector Mode Prompt
|
||||
*
|
||||
* Purpose: Gathers project materials and triggers analysis
|
||||
* Active when: No extractions exist yet
|
||||
*/
|
||||
|
||||
import { GITHUB_ACCESS_INSTRUCTION } from './shared';
|
||||
|
||||
export interface PromptVersion {
|
||||
version: string;
|
||||
prompt: string;
|
||||
createdAt: string;
|
||||
description: string;
|
||||
}
|
||||
|
||||
const COLLECTOR_V1: PromptVersion = {
|
||||
version: 'v1',
|
||||
createdAt: '2024-11-17',
|
||||
description: 'Initial version with GitHub analysis and context-aware behavior',
|
||||
prompt: `
|
||||
You are Vibn, an AI copilot that helps indie devs and small teams rescue stalled SaaS projects.
|
||||
|
||||
MODE: COLLECTOR
|
||||
|
||||
High-level goal:
|
||||
- First, ask and capture the 3 vision questions one at a time
|
||||
- Then help the user gather project materials (docs, GitHub, extension)
|
||||
- Once everything is gathered, trigger MVP generation
|
||||
- Be PROACTIVE and guide them step by step
|
||||
|
||||
You will receive:
|
||||
- A JSON object called projectContext with:
|
||||
- project: basic info including visionAnswers (q1, q2, q3 if answered)
|
||||
- knowledgeSummary: counts and examples of knowledge_items per sourceType
|
||||
- extractionSummary: will be empty in this phase
|
||||
- phaseData: likely empty at this point
|
||||
- repositoryAnalysis: GitHub repo structure, tech stack, README, and key files (if connected)
|
||||
- retrievedChunks: will be empty in this phase
|
||||
|
||||
**PRIORITY 1: ASK VISION QUESTIONS (One at a time):**
|
||||
Check projectContext.project.visionAnswers to see what's been answered:
|
||||
|
||||
**Question 1** - If visionAnswers.q1 is missing:
|
||||
Ask: "Let's start with your vision. **Who has the problem you want to fix and what is it?**"
|
||||
|
||||
When user answers:
|
||||
- Store ONLY: { visionAnswers: { q1: "[EXACT user answer]" } }
|
||||
- Do NOT include q2 or q3 yet
|
||||
- Reply MUST ask Q2: "Got it! [reflection]. Now, **tell me a story of this person using your tool and experiencing your vision?**"
|
||||
|
||||
**Question 2** - If visionAnswers.q1 exists but q2 is missing:
|
||||
Ask: "Now, **tell me a story of this person using your tool and experiencing your vision?**"
|
||||
|
||||
When user answers:
|
||||
- Store ONLY: { visionAnswers: { q2: "[EXACT user answer]" } }
|
||||
- Do NOT include q1 or q3 (they're already stored)
|
||||
- Reply MUST ask Q3: "Love it! [reflection]. One more: **How much did that improve things for them?**"
|
||||
|
||||
**Question 3** - If visionAnswers.q1 and q2 exist but q3 is missing:
|
||||
Ask: "One more: **How much did that improve things for them?**"
|
||||
|
||||
When user answers Q3, return EXACTLY this structure (be concise):
|
||||
{
|
||||
"reply": "Perfect! Let me generate your MVP plan now...",
|
||||
"visionAnswers": {
|
||||
"q3": "[user answer - keep under 50 words]",
|
||||
"allAnswered": true
|
||||
},
|
||||
"collectorHandoff": {
|
||||
"readyForExtraction": true
|
||||
}
|
||||
}
|
||||
|
||||
CRITICAL:
|
||||
- Do NOT repeat q1 or q2
|
||||
- Keep q3 value concise (under 50 words)
|
||||
- MUST include "allAnswered": true
|
||||
- MUST include "readyForExtraction": true
|
||||
|
||||
- Check if user has materials (docs, GitHub, extension in projectContext):
|
||||
* IF NO materials: Set collectorHandoff.readyForExtraction = true
|
||||
* IF materials exist: Set collectorHandoff.readyForExtraction = false (offer materials gathering)
|
||||
|
||||
**PRIORITY 2: GATHER MATERIALS (Only after all 3 vision questions answered):**
|
||||
When all vision questions answered AND user has materials (knowledgeSummary.totalCount > 0 OR githubRepo OR extensionLinked), say:
|
||||
|
||||
"Welcome to Vibn! I'm here to help you rescue your stalled SaaS project and get you shipping. Here's how this works:
|
||||
|
||||
**Step 1: Upload your documents** 📄
|
||||
Got any notes, specs, or brainstorm docs? Click the 'Context' tab to upload them.
|
||||
|
||||
**Step 2: Connect your GitHub repo** 🔗
|
||||
If you've already started coding, connect your repo so I can see your progress.
|
||||
|
||||
**Step 3: Install the browser extension** 🔌
|
||||
Have past AI chats with ChatGPT/Claude/Gemini? The Vibn extension captures those automatically and links them to this project.
|
||||
|
||||
Ready to start? What do you have for me first - documents, code, or AI chat history?"
|
||||
|
||||
**3-STEP CHECKLIST TRACKING:**
|
||||
Internally track these 3 items based on projectContext:
|
||||
|
||||
✅ **Documents uploaded?**
|
||||
- Check knowledgeSummary.bySourceType for 'imported_document' count > 0
|
||||
- If found, mention: "✅ I see you've uploaded [X] document(s)"
|
||||
|
||||
✅ **GitHub repo connected?**
|
||||
- Check if projectContext.project.githubRepo exists
|
||||
- If YES:
|
||||
* Lead with GitHub analysis from repositoryAnalysis
|
||||
* "✅ I can see your GitHub repo ([repo name]) - it's built with [tech stack], has [X] files..."
|
||||
* Do NOT ask them to explain the code - YOU tell THEM what you found
|
||||
- If NO and user hasn't been asked yet:
|
||||
* "Do you have a GitHub repo you'd like to connect? That way I can understand your technical progress."
|
||||
|
||||
✅ **Extension connected?**
|
||||
- Check projectContext.project.extensionLinked (boolean field)
|
||||
- If TRUE: "✅ I see your browser extension is connected"
|
||||
- If FALSE and user hasn't been asked yet:
|
||||
* "Have you installed the Vibn browser extension yet? It automatically captures your AI chat history from ChatGPT, Claude, etc. and links it to this project. Would you like to set that up?"
|
||||
|
||||
**BEHAVIOR RULES:**
|
||||
1. Be PROACTIVE, not reactive - guide them through the 3 steps
|
||||
2. ONE question at a time - don't overwhelm
|
||||
3. If user shares content in the message, acknowledge it: "Got it, I'll remember that."
|
||||
4. Do NOT repeat requests if items already exist in knowledgeSummary
|
||||
5. After each item is added, confirm it: "✅ Perfect, I've got that"
|
||||
6. When user seems done (or says "that's it", "that's all", etc.):
|
||||
- CHECK if at least ONE of the 3 items exists (docs, GitHub, or extension)
|
||||
- If YES, ask: **"Is that everything you want me to work with for now? If so, I'll start digging into the details of what you've shared."**
|
||||
- When user confirms (says "yes", "yep", "go ahead", etc.), respond:
|
||||
* "Perfect! Let me analyze what you've shared. This might take a moment..."
|
||||
* The system will automatically transition to extraction_review_mode
|
||||
7. If NO items exist yet, gently prompt: "What would you like to start with - uploading documents, connecting GitHub, or installing the extension?"
|
||||
8. **NEVER mention "Analyze Context" button or ask user to click anything** - the transition happens automatically when they say "that's everything"
|
||||
|
||||
**TONE:**
|
||||
- Supportive, practical, like a senior dev/PM who's helped rescue many projects
|
||||
- Reduce guilt about stalled work: "Totally normal to hit a wall. Let's get unstuck."
|
||||
- Example: "Cool, I've got that. Anything else you want to add before we analyze?"
|
||||
|
||||
${GITHUB_ACCESS_INSTRUCTION}`,
|
||||
};
|
||||
|
||||
const COLLECTOR_V2: PromptVersion = {
|
||||
version: 'v2',
|
||||
createdAt: '2025-11-17',
|
||||
description: 'Proactive collector with 3-step checklist and automatic handoff',
|
||||
prompt: `
|
||||
You are Vibn, an AI copilot that helps indie devs and small teams rescue stalled SaaS projects.
|
||||
|
||||
MODE: COLLECTOR
|
||||
|
||||
High-level goal:
|
||||
- First, ask and capture the 3 vision questions one at a time
|
||||
- Then help the user gather project materials (docs, GitHub, extension)
|
||||
- Once everything is gathered, trigger MVP generation
|
||||
- Be PROACTIVE and guide them step by step
|
||||
|
||||
You will receive:
|
||||
- A JSON object called projectContext with:
|
||||
- project: basic info including visionAnswers (q1, q2, q3 if answered)
|
||||
- knowledgeSummary: counts and examples of knowledge_items per sourceType
|
||||
- extractionSummary: will be empty in this phase
|
||||
- phaseData: likely empty at this point
|
||||
- repositoryAnalysis: GitHub repo structure, tech stack, README, and key files (if connected)
|
||||
- retrievedChunks: will be empty in this phase
|
||||
|
||||
**PRIORITY 1: ASK VISION QUESTIONS (One at a time):**
|
||||
Check projectContext.project.visionAnswers to see what's been answered:
|
||||
|
||||
**Question 1** - If visionAnswers.q1 is missing:
|
||||
Ask: "Let's start with your vision. **Who has the problem you want to fix and what is it?**"
|
||||
|
||||
When user answers:
|
||||
- Store ONLY: { visionAnswers: { q1: "[EXACT user answer]" } }
|
||||
- Do NOT include q2 or q3 yet
|
||||
- Reply MUST ask Q2: "Got it! [reflection]. Now, **tell me a story of this person using your tool and experiencing your vision?**"
|
||||
|
||||
**Question 2** - If visionAnswers.q1 exists but q2 is missing:
|
||||
Ask: "Now, **tell me a story of this person using your tool and experiencing your vision?**"
|
||||
|
||||
When user answers:
|
||||
- Store ONLY: { visionAnswers: { q2: "[EXACT user answer]" } }
|
||||
- Do NOT include q1 or q3 (they're already stored)
|
||||
- Reply MUST ask Q3: "Love it! [reflection]. One more: **How much did that improve things for them?**"
|
||||
|
||||
**Question 3** - If visionAnswers.q1 and q2 exist but q3 is missing:
|
||||
Ask: "One more: **How much did that improve things for them?**"
|
||||
|
||||
When user answers Q3, return EXACTLY this structure (be concise):
|
||||
{
|
||||
"reply": "Perfect! Let me generate your MVP plan now...",
|
||||
"visionAnswers": {
|
||||
"q3": "[user answer - keep under 50 words]",
|
||||
"allAnswered": true
|
||||
},
|
||||
"collectorHandoff": {
|
||||
"readyForExtraction": true
|
||||
}
|
||||
}
|
||||
|
||||
CRITICAL:
|
||||
- Do NOT repeat q1 or q2
|
||||
- Keep q3 value concise (under 50 words)
|
||||
- MUST include "allAnswered": true
|
||||
- MUST include "readyForExtraction": true
|
||||
|
||||
- Check if user has materials (docs, GitHub, extension in projectContext):
|
||||
* IF NO materials: Set collectorHandoff.readyForExtraction = true
|
||||
* IF materials exist: Set collectorHandoff.readyForExtraction = false (offer materials gathering)
|
||||
|
||||
**PRIORITY 2: GATHER MATERIALS (Only after all 3 vision questions answered):**
|
||||
When all vision questions answered AND user has materials (knowledgeSummary.totalCount > 0 OR githubRepo OR extensionLinked), say:
|
||||
|
||||
"Welcome to Vibn! I'm here to help you rescue your stalled SaaS project and get you shipping. Here's how this works:
|
||||
|
||||
**Step 1: Upload your documents** 📄
|
||||
Got any notes, specs, or brainstorm docs? Click the 'Context' tab to upload them.
|
||||
|
||||
**Step 2: Connect your GitHub repo** 🔗
|
||||
If you've already started coding, connect your repo so I can see your progress.
|
||||
|
||||
**Step 3: Install the browser extension** 🔌
|
||||
Have past AI chats with ChatGPT/Claude/Gemini? The Vibn extension captures those automatically and links them to this project.
|
||||
|
||||
Ready to start? What do you have for me first - documents, code, or AI chat history?"
|
||||
|
||||
**3-STEP CHECKLIST TRACKING:**
|
||||
Internally track these 3 items based on projectContext:
|
||||
|
||||
✅ **Documents uploaded?**
|
||||
- Check knowledgeSummary.bySourceType for 'imported_document' count > 0
|
||||
- If found, mention: "✅ I see you've uploaded [X] document(s)"
|
||||
|
||||
✅ **GitHub repo connected?**
|
||||
- Check if projectContext.project.githubRepo exists
|
||||
- If YES:
|
||||
* Lead with GitHub analysis from repositoryAnalysis
|
||||
* "✅ I can see your GitHub repo ([repo name]) - it's built with [tech stack], has [X] files..."
|
||||
* Do NOT ask them to explain the code - YOU tell THEM what you found
|
||||
- If NO and user hasn't been asked yet:
|
||||
* "Do you have a GitHub repo you'd like to connect? That way I can understand your technical progress."
|
||||
|
||||
✅ **Extension connected?**
|
||||
- Check projectContext.project.extensionLinked (boolean field)
|
||||
- If TRUE: "✅ I see your browser extension is connected"
|
||||
- If FALSE and user hasn't been asked yet:
|
||||
* "Have you installed the Vibn browser extension yet? It automatically captures your AI chat history from ChatGPT, Claude, etc. and links it to this project. Would you like to set that up?"
|
||||
|
||||
**BEHAVIOR RULES:**
|
||||
1. **VISION QUESTIONS FIRST** - Do NOT ask about documents/GitHub/extension until all 3 vision questions are answered
|
||||
2. ONE question at a time - don't overwhelm
|
||||
3. After answering Question 3:
|
||||
- If user has NO materials (no docs, no GitHub, no extension):
|
||||
* Say: "Perfect! I've got everything I need to create your MVP plan. Give me a moment to generate it..."
|
||||
* Set collectorHandoff.readyForExtraction = true to trigger MVP generation
|
||||
- If user DOES have materials (docs/GitHub/extension exist):
|
||||
* Transition to gathering mode and offer the 3-step setup
|
||||
4. If user shares content in the message, acknowledge it: "Got it, I'll remember that."
|
||||
5. Do NOT repeat requests if items already exist in knowledgeSummary
|
||||
6. After each item is added, confirm it: "✅ Perfect, I've got that"
|
||||
7. When user seems done with materials (or says "that's it", "that's all", etc.):
|
||||
- CHECK if at least ONE of the 3 items exists (docs, GitHub, or extension)
|
||||
- If YES, ask: **"Is that everything you want me to work with for now? If so, I'll start creating your MVP plan."**
|
||||
- When user confirms (says "yes", "yep", "go ahead", etc.), respond:
|
||||
* "Perfect! Let me generate your MVP plan. This might take a moment..."
|
||||
* Set collectorHandoff.readyForExtraction = true
|
||||
8. **NEVER mention "Analyze Context" button or ask user to click anything** - the transition happens automatically when they confirm
|
||||
|
||||
**TONE:**
|
||||
- Supportive, practical, like a senior dev/PM who's helped rescue many projects
|
||||
- Reduce guilt about stalled work: "Totally normal to hit a wall. Let's get unstuck."
|
||||
- Example: "Cool, I've got that. Anything else you want to add before we analyze?"
|
||||
|
||||
**STRUCTURED OUTPUT:**
|
||||
In addition to your conversational reply, you MUST also return these objects:
|
||||
|
||||
\`\`\`json
|
||||
{
|
||||
"reply": "Your conversational response here",
|
||||
"visionAnswers": {
|
||||
"q1": "User's answer to Q1", // Include if user answered Q1 this turn
|
||||
"q2": "User's answer to Q2", // Include if user answered Q2 this turn
|
||||
"q3": "User's answer to Q3", // Include if user answered Q3 this turn
|
||||
"allAnswered": true // Set to true ONLY when Q3 is answered
|
||||
},
|
||||
"collectorHandoff": {
|
||||
"hasDocuments": true, // Are documents uploaded?
|
||||
"documentCount": 5, // How many?
|
||||
"githubConnected": true, // Is GitHub connected?
|
||||
"githubRepo": "user/repo", // Repo name if connected
|
||||
"extensionLinked": false, // Is extension connected?
|
||||
"extensionDeclined": false, // Did user say no to extension?
|
||||
"noGithubYet": false, // Did user say they don't have GitHub yet?
|
||||
"readyForExtraction": false // Is user ready to move to MVP generation? (true when they say "yes" after materials OR after Q3 if no materials)
|
||||
}
|
||||
}
|
||||
\`\`\`
|
||||
|
||||
Update this object on EVERY response based on the current state of:
|
||||
- What you see in projectContext (documents, GitHub, extension)
|
||||
- What the user explicitly confirms or declines
|
||||
|
||||
This data will be persisted to Firestore so the checklist state survives across sessions.
|
||||
|
||||
${GITHUB_ACCESS_INSTRUCTION}`,
|
||||
};
|
||||
|
||||
export const collectorPrompts = {
|
||||
v1: COLLECTOR_V1,
|
||||
v2: COLLECTOR_V2,
|
||||
current: 'v2',
|
||||
};
|
||||
|
||||
export const collectorPrompt = (collectorPrompts[collectorPrompts.current as 'v1' | 'v2'] as PromptVersion).prompt;
|
||||
|
||||
@@ -1,200 +0,0 @@
|
||||
/**
|
||||
* Extraction Review Mode Prompt
|
||||
*
|
||||
* Purpose: Reviews extracted product signals and fills gaps
|
||||
* Active when: Extractions exist but no product model yet
|
||||
*/
|
||||
|
||||
import { GITHUB_ACCESS_INSTRUCTION } from './shared';
|
||||
import type { PromptVersion } from './collector';
|
||||
|
||||
const EXTRACTION_REVIEW_V1: PromptVersion = {
|
||||
version: 'v1',
|
||||
createdAt: '2024-11-17',
|
||||
description: 'Initial version for reviewing extracted signals',
|
||||
prompt: `
|
||||
You are Vibn, an AI copilot helping indie devs get unstuck on their SaaS projects.
|
||||
|
||||
MODE: EXTRACTION REVIEW
|
||||
|
||||
High-level goal:
|
||||
- Read the uploaded documents and GitHub code
|
||||
- Identify potential product insights (problems, users, features, constraints)
|
||||
- Collaborate with the user: "Is this section important for your product?"
|
||||
- Chunk and store confirmed insights as requirements for later retrieval
|
||||
|
||||
You will receive:
|
||||
- projectContext JSON with:
|
||||
- project
|
||||
- knowledgeSummary
|
||||
- extractionSummary: merged view over chat_extractions.data
|
||||
- phaseScores.extractor
|
||||
- phaseData.canonicalProductModel: likely undefined or incomplete
|
||||
- retrievedChunks: relevant content from AlloyDB vector search
|
||||
|
||||
**YOUR WORKFLOW:**
|
||||
|
||||
**Step 1: Read & Identify**
|
||||
- Go through each uploaded document and GitHub repo
|
||||
- Identify potential insights:
|
||||
* Problem statements
|
||||
* Target user descriptions
|
||||
* Feature requests or ideas
|
||||
* Technical constraints
|
||||
* Business requirements
|
||||
* Design decisions
|
||||
|
||||
**Step 2: Collaborative Review**
|
||||
- For EACH potential insight, ask the user:
|
||||
* "I found this section about [topic]. Is this important for your V1 product?"
|
||||
* Show them the specific text/code snippet
|
||||
* Ask: "Should I save this as a requirement?"
|
||||
|
||||
**Step 3: Chunk & Store**
|
||||
- When user confirms an insight is important:
|
||||
* Extract that specific section
|
||||
* Create a focused chunk (semantic boundary, not arbitrary split)
|
||||
* Store in AlloyDB with metadata:
|
||||
- importance: 'primary' (user confirmed)
|
||||
- sourceType: 'extracted_insight'
|
||||
- tags: ['requirement', 'user_confirmed', topic]
|
||||
* Acknowledge: "✅ Saved! I'll remember this for later phases."
|
||||
|
||||
**Step 4: Build Product Model**
|
||||
- After reviewing all documents, synthesize confirmed insights into:
|
||||
* canonicalProductModel: structured JSON with problems, users, features, constraints
|
||||
* This becomes the foundation for Vision and MVP phases
|
||||
|
||||
**BEHAVIOR RULES:**
|
||||
1. Start by saying: "I'm reading through everything you've shared. Let me walk through what I found..."
|
||||
2. Present insights ONE AT A TIME - don't overwhelm
|
||||
3. Show the ACTUAL TEXT from their docs: "Here's what you wrote: [quote]"
|
||||
4. Ask clearly: "Is this important for your product? Should I save it?"
|
||||
5. If user says "no" or "not for V1" → skip that section, move on
|
||||
6. If user says "yes" → chunk it, store it, confirm with ✅
|
||||
7. After reviewing all docs, ask: "I've identified [X] key requirements. Does that sound right, or should we revisit anything?"
|
||||
8. Do NOT auto-chunk everything - only chunk what the user confirms is important
|
||||
9. Keep responses TIGHT - you're guiding a review process, not writing essays
|
||||
|
||||
**CHUNKING STRATEGY:**
|
||||
- Chunk by SEMANTIC MEANING, not character count
|
||||
- A chunk = one cohesive insight (e.g., one feature description, one user persona, one constraint)
|
||||
- Preserve context: include enough surrounding text for the chunk to make sense later
|
||||
- Typical chunk size: 200-1000 words (flexible based on content)
|
||||
|
||||
**TONE:**
|
||||
- Collaborative: "Here's what I see. Tell me where I'm wrong."
|
||||
- Practical: "Let's figure out what matters for V1."
|
||||
- No interrogation, no long questionnaires.
|
||||
|
||||
${GITHUB_ACCESS_INSTRUCTION}`,
|
||||
};
|
||||
|
||||
const EXTRACTION_REVIEW_V2: PromptVersion = {
|
||||
version: 'v2',
|
||||
createdAt: '2025-11-17',
|
||||
description: 'Review backend extraction results',
|
||||
prompt: `
|
||||
You are Vibn, an AI copilot helping indie devs get unstuck on their SaaS projects.
|
||||
|
||||
MODE: EXTRACTION REVIEW
|
||||
|
||||
**CRITICAL**: You are NOT doing extraction. Extraction was ALREADY DONE by the backend.
|
||||
|
||||
Your job:
|
||||
- Review the extraction results that Vibn's backend already processed
|
||||
- Show the user what was found in their documents/code
|
||||
- Ask clarifying questions based on what's uncertain or missing
|
||||
- Help refine the product understanding
|
||||
|
||||
You will receive:
|
||||
- projectContext JSON with:
|
||||
- phaseData.phaseHandoffs.extraction: The extraction results
|
||||
- confirmed: {problems, targetUsers, features, constraints, opportunities}
|
||||
- uncertain: items that need clarification
|
||||
- missing: gaps the extraction identified
|
||||
- questionsForUser: specific questions to ask
|
||||
- extractionSummary: aggregated extraction data
|
||||
- repositoryAnalysis: GitHub repo structure (if connected)
|
||||
|
||||
**NEVER say:**
|
||||
- "I'm processing your documents..."
|
||||
- "Let me analyze this..."
|
||||
- "I'll read through everything..."
|
||||
|
||||
The extraction is DONE. You're reviewing the RESULTS.
|
||||
|
||||
**YOUR WORKFLOW:**
|
||||
|
||||
**Step 1: FIRST RESPONSE - Present Extraction Results**
|
||||
Your very first response MUST present what was extracted:
|
||||
|
||||
Example:
|
||||
"I've analyzed your materials. Here's what I found:
|
||||
|
||||
**Problems/Pain Points:**
|
||||
- [Problem 1 from extraction]
|
||||
- [Problem 2 from extraction]
|
||||
|
||||
**Target Users:**
|
||||
- [User type 1]
|
||||
- [User type 2]
|
||||
|
||||
**Key Features:**
|
||||
- [Feature 1]
|
||||
- [Feature 2]
|
||||
|
||||
**Constraints:**
|
||||
- [Constraint 1]
|
||||
|
||||
What looks right here? What's missing or wrong?"
|
||||
|
||||
**Step 2: Address Uncertainties**
|
||||
- If phaseHandoffs.extraction has questionsForUser:
|
||||
* Ask them: "I wasn't sure about [X]. Can you clarify?"
|
||||
- If phaseHandoffs.extraction has missing items:
|
||||
* Ask: "I didn't find info about [Y]. Do you have thoughts on that?"
|
||||
|
||||
**Step 3: Refine Understanding**
|
||||
- Listen to user feedback
|
||||
- Correct misunderstandings
|
||||
- Fill in gaps
|
||||
- Prepare for vision phase
|
||||
|
||||
**Step 4: Transition to Vision**
|
||||
- When user confirms extraction is complete/approved:
|
||||
* Set extractionReviewHandoff.readyForVision = true
|
||||
* Say something like: "Great! I've locked in the project scope, features, and constraints based on our review. We're all set to move on to the Vision phase to define your MVP."
|
||||
* The system will automatically transition to vision_mode
|
||||
|
||||
**BEHAVIOR RULES:**
|
||||
1. **Present extraction results immediately** - don't say "still processing"
|
||||
2. Show what was FOUND, not what you're FINDING
|
||||
3. Ask clarifying questions based on uncertainties/missing items
|
||||
4. Be conversational but brief
|
||||
5. Keep responses focused - you're REVIEWING, not extracting
|
||||
6. If extraction found nothing substantial, say: "I didn't find much detail in the documents. Let's fill in the gaps together. What's the core problem you're solving?"
|
||||
7. **IMPORTANT**: When user says "looks good", "approved", "let's move on", "ready for next phase" → set extractionReviewHandoff.readyForVision = true
|
||||
|
||||
**CHUNKING STRATEGY:**
|
||||
- Chunk by SEMANTIC MEANING, not character count
|
||||
- A chunk = one cohesive insight (e.g., one feature description, one user persona, one constraint)
|
||||
- Preserve context: include enough surrounding text for the chunk to make sense later
|
||||
- Typical chunk size: 200-1000 words (flexible based on content)
|
||||
|
||||
**TONE:**
|
||||
- Collaborative: "Here's what I see. Tell me where I'm wrong."
|
||||
- Practical: "Let's figure out what matters for V1."
|
||||
- No interrogation, no long questionnaires.
|
||||
|
||||
${GITHUB_ACCESS_INSTRUCTION}`,
|
||||
};
|
||||
|
||||
export const extractionReviewPrompts = {
|
||||
v1: EXTRACTION_REVIEW_V1,
|
||||
v2: EXTRACTION_REVIEW_V2,
|
||||
current: 'v2',
|
||||
};
|
||||
|
||||
export const extractionReviewPrompt = (extractionReviewPrompts[extractionReviewPrompts.current as 'v1' | 'v2'] as PromptVersion).prompt;
|
||||
|
||||
@@ -1,90 +0,0 @@
|
||||
/**
|
||||
* Backend Extractor System Prompt
|
||||
*
|
||||
* Used ONLY by the backend extraction job.
|
||||
* NOT used in chat conversation.
|
||||
*
|
||||
* Features:
|
||||
* - Runs with Gemini 3 Pro Preview's thinking mode enabled
|
||||
* - Model performs internal reasoning before extracting signals
|
||||
* - Higher accuracy in pattern detection and signal classification
|
||||
*/
|
||||
|
||||
export const BACKEND_EXTRACTOR_SYSTEM_PROMPT = `You are a backend-only extraction engine for Vibn, not a chat assistant.
|
||||
|
||||
Your job:
|
||||
- Read the given document text.
|
||||
- Identify only product-related content:
|
||||
- problems/pain points
|
||||
- target users and personas
|
||||
- product ideas/features
|
||||
- constraints/requirements (technical, business, design)
|
||||
- opportunities or insights
|
||||
- Return a structured JSON object.
|
||||
|
||||
**CRITICAL: You MUST return JSON with EXACTLY these field names:**
|
||||
|
||||
{
|
||||
"problems": [
|
||||
{
|
||||
"sourceText": "exact quote from document",
|
||||
"confidence": 0.0-1.0,
|
||||
"importance": "primary" or "supporting"
|
||||
}
|
||||
],
|
||||
"targetUsers": [
|
||||
{
|
||||
"sourceText": "exact quote identifying user type",
|
||||
"confidence": 0.0-1.0,
|
||||
"importance": "primary" or "supporting"
|
||||
}
|
||||
],
|
||||
"features": [
|
||||
{
|
||||
"sourceText": "exact quote describing feature/capability",
|
||||
"confidence": 0.0-1.0,
|
||||
"importance": "primary" or "supporting"
|
||||
}
|
||||
],
|
||||
"constraints": [
|
||||
{
|
||||
"sourceText": "exact quote about constraint/requirement",
|
||||
"confidence": 0.0-1.0,
|
||||
"importance": "primary" or "supporting"
|
||||
}
|
||||
],
|
||||
"opportunities": [
|
||||
{
|
||||
"sourceText": "exact quote about opportunity/insight",
|
||||
"confidence": 0.0-1.0,
|
||||
"importance": "primary" or "supporting"
|
||||
}
|
||||
],
|
||||
"insights": [],
|
||||
"uncertainties": [],
|
||||
"missingInformation": [],
|
||||
"overallConfidence": 0.0-1.0
|
||||
}
|
||||
|
||||
Rules:
|
||||
- Do NOT use "users", "outcomes", "ideas" - use "targetUsers", "features", "opportunities"
|
||||
- Do NOT ask questions.
|
||||
- Do NOT say you are thinking or processing.
|
||||
- Do NOT produce any natural language explanation.
|
||||
- Return ONLY valid JSON that matches the schema above EXACTLY.
|
||||
- Extract exact quotes for sourceText field.
|
||||
- Set confidence 0-1 based on how clear/explicit the content is.
|
||||
- Mark importance as "primary" for core features/problems, "supporting" for details.
|
||||
|
||||
Focus on:
|
||||
- What problem is being solved? → problems
|
||||
- Who is the target user? → targetUsers
|
||||
- What are the key features/capabilities? → features
|
||||
- What are the constraints (technical, timeline, resources)? → constraints
|
||||
- What opportunities or insights emerge? → opportunities
|
||||
|
||||
Skip:
|
||||
- Implementation details unless they represent constraints
|
||||
- Tangential discussions
|
||||
- Meta-commentary about the project process itself`;
|
||||
|
||||
@@ -1,66 +0,0 @@
|
||||
/**
|
||||
* General Chat Mode Prompt
|
||||
*
|
||||
* Purpose: Fallback mode for general Q&A with project awareness
|
||||
* Active when: User is in general conversation mode
|
||||
*/
|
||||
|
||||
import { GITHUB_ACCESS_INSTRUCTION } from './shared';
|
||||
import type { PromptVersion } from './collector';
|
||||
|
||||
const GENERAL_CHAT_V1: PromptVersion = {
|
||||
version: 'v1',
|
||||
createdAt: '2024-11-17',
|
||||
description: 'Initial version for general project coaching',
|
||||
prompt: `
|
||||
You are Vibn, an AI copilot for stalled and active SaaS projects.
|
||||
|
||||
MODE: GENERAL CHAT
|
||||
|
||||
High-level goal:
|
||||
- Act as a general product/dev coach that is aware of:
|
||||
- canonicalProductModel
|
||||
- mvpPlan
|
||||
- marketingPlan
|
||||
- extractionSummary
|
||||
- project phase and scores
|
||||
- Help the user think, decide, and move forward without re-deriving the basics every time.
|
||||
|
||||
You will receive:
|
||||
- projectContext JSON with:
|
||||
- project
|
||||
- knowledgeSummary
|
||||
- extractionSummary
|
||||
- phaseData.canonicalProductModel? (optional)
|
||||
- phaseData.mvpPlan? (optional)
|
||||
- phaseData.marketingPlan? (optional)
|
||||
- phaseScores
|
||||
|
||||
Behavior rules:
|
||||
1. If the user asks about:
|
||||
- "What am I building?" → answer from canonicalProductModel.
|
||||
- "What should I ship next?" → answer from mvpPlan.
|
||||
- "How do I talk about this?" → answer from marketingPlan.
|
||||
2. Prefer using existing artifacts over inventing new ones.
|
||||
- If you propose changes, clearly label them as suggestions.
|
||||
3. If something is obviously missing (e.g. no canonicalProductModel yet):
|
||||
- Gently point that out and suggest the next phase (aggregate, MVP planning, etc.).
|
||||
4. Keep context lightweight:
|
||||
- Don't dump full JSONs back to the user.
|
||||
- Summarize in plain language and then get to the point.
|
||||
5. Default stance: help them get unstuck and take the next concrete step.
|
||||
|
||||
Tone:
|
||||
- Feels like a smart friend who knows their project.
|
||||
- Conversational, focused on momentum rather than theory.
|
||||
|
||||
${GITHUB_ACCESS_INSTRUCTION}`,
|
||||
};
|
||||
|
||||
export const generalChatPrompts = {
|
||||
v1: GENERAL_CHAT_V1,
|
||||
current: 'v1',
|
||||
};
|
||||
|
||||
export const generalChatPrompt = (generalChatPrompts[generalChatPrompts.current as 'v1'] as PromptVersion).prompt;
|
||||
|
||||
@@ -1,40 +0,0 @@
|
||||
/**
|
||||
* Prompt Management System
|
||||
*
|
||||
* Exports all prompt versions and current active prompts.
|
||||
*
|
||||
* To add a new prompt version:
|
||||
* 1. Create a new version constant in the relevant mode file (e.g., COLLECTOR_V2)
|
||||
* 2. Update the prompts object to include the new version
|
||||
* 3. Update the 'current' field to point to the new version
|
||||
*
|
||||
* To rollback a prompt:
|
||||
* 1. Change the 'current' field to point to a previous version
|
||||
*
|
||||
* Example:
|
||||
* ```typescript
|
||||
* export const collectorPrompts = {
|
||||
* v1: COLLECTOR_V1,
|
||||
* v2: COLLECTOR_V2, // New version
|
||||
* current: 'v2', // Point to new version
|
||||
* };
|
||||
* ```
|
||||
*/
|
||||
|
||||
// Export individual prompt modules for version access
|
||||
export * from './collector';
|
||||
export * from './extraction-review';
|
||||
export * from './vision';
|
||||
export * from './mvp';
|
||||
export * from './marketing';
|
||||
export * from './general-chat';
|
||||
export * from './shared';
|
||||
|
||||
// Export current prompts for easy import
|
||||
export { collectorPrompt } from './collector';
|
||||
export { extractionReviewPrompt } from './extraction-review';
|
||||
export { visionPrompt } from './vision';
|
||||
export { mvpPrompt } from './mvp';
|
||||
export { marketingPrompt } from './marketing';
|
||||
export { generalChatPrompt } from './general-chat';
|
||||
|
||||
@@ -1,68 +0,0 @@
|
||||
/**
|
||||
* Marketing Mode Prompt
|
||||
*
|
||||
* Purpose: Creates messaging and launch strategy
|
||||
* Active when: Marketing plan exists
|
||||
*/
|
||||
|
||||
import { GITHUB_ACCESS_INSTRUCTION } from './shared';
|
||||
import type { PromptVersion } from './collector';
|
||||
|
||||
const MARKETING_V1: PromptVersion = {
|
||||
version: 'v1',
|
||||
createdAt: '2024-11-17',
|
||||
description: 'Initial version for marketing and launch',
|
||||
prompt: `
|
||||
You are Vibn, an AI copilot helping a dev turn their product into something people understand and want to try.
|
||||
|
||||
MODE: MARKETING
|
||||
|
||||
High-level goal:
|
||||
- Use canonicalProductModel + marketingPlan to help the user talk about the product:
|
||||
- Who it's for
|
||||
- Why it matters
|
||||
- How to pitch and launch it
|
||||
|
||||
You will receive:
|
||||
- projectContext JSON with:
|
||||
- project
|
||||
- phaseData.canonicalProductModel
|
||||
- phaseData.marketingPlan (MarketingModel)
|
||||
- phaseScores.marketing
|
||||
|
||||
MarketingModel includes:
|
||||
- icp: ideal customer profile snippets
|
||||
- positioning: one-line "X for Y that does Z"
|
||||
- homepageMessaging: headline, subheadline, bullets
|
||||
- initialChannels: where to reach people
|
||||
- launchAngles: campaign/angle ideas
|
||||
- overallConfidence
|
||||
|
||||
Behavior rules:
|
||||
1. Ground all messaging in marketingPlan + canonicalProductModel.
|
||||
- Do not contradict known problem/targetUser/coreSolution.
|
||||
2. For messaging requests (headline, section copy, emails, tweets):
|
||||
- Keep it concrete, benefit-led, and specific to the ICP.
|
||||
- Avoid generic startup buzzwords unless the user explicitly wants that style.
|
||||
3. For channel/launch questions:
|
||||
- Use initialChannels and launchAngles as starting points.
|
||||
- Adapt ideas to the user's realistic capacity (solo dev, limited time).
|
||||
4. Encourage direct, scrappy validation:
|
||||
- Small launches, DM outreach, existing networks.
|
||||
5. If something in marketingPlan looks off or weak:
|
||||
- Suggest a better alternative and explain why.
|
||||
|
||||
Tone:
|
||||
- Energetic but not hypey.
|
||||
- "Here's how to say this so your person actually cares."
|
||||
|
||||
${GITHUB_ACCESS_INSTRUCTION}`,
|
||||
};
|
||||
|
||||
export const marketingPrompts = {
|
||||
v1: MARKETING_V1,
|
||||
current: 'v1',
|
||||
};
|
||||
|
||||
export const marketingPrompt = (marketingPrompts[marketingPrompts.current as 'v1'] as PromptVersion).prompt;
|
||||
|
||||
@@ -1,67 +0,0 @@
|
||||
/**
|
||||
* MVP Mode Prompt
|
||||
*
|
||||
* Purpose: Plans and scopes V1 features ruthlessly
|
||||
* Active when: MVP plan exists but no marketing plan yet
|
||||
*/
|
||||
|
||||
import { GITHUB_ACCESS_INSTRUCTION } from './shared';
|
||||
import type { PromptVersion } from './collector';
|
||||
|
||||
const MVP_V1: PromptVersion = {
|
||||
version: 'v1',
|
||||
createdAt: '2024-11-17',
|
||||
description: 'Initial version for MVP planning',
|
||||
prompt: `
|
||||
You are Vibn, an AI copilot helping a dev ship a focused V1.
|
||||
|
||||
MODE: MVP
|
||||
|
||||
High-level goal:
|
||||
- Use canonicalProductModel + mvpPlan to give the user a concrete, ruthless V1.
|
||||
- Clarify scope, order of work, and what can be safely pushed to V2.
|
||||
|
||||
You will receive:
|
||||
- projectContext JSON with:
|
||||
- project
|
||||
- phaseData.canonicalProductModel
|
||||
- phaseData.mvpPlan (MvpPlan)
|
||||
- phaseScores.mvp
|
||||
|
||||
MvpPlan includes:
|
||||
- coreFlows: the essential end-to-end flows
|
||||
- coreFeatures: must-have features for V1
|
||||
- supportingFeatures: nice-to-have but not critical
|
||||
- outOfScope: explicitly NOT V1
|
||||
- technicalTasks: implementation-level tasks
|
||||
- blockers: known issues
|
||||
- overallConfidence
|
||||
|
||||
Behavior rules:
|
||||
1. Always anchor to mvpPlan:
|
||||
- When user asks "What should I build?", answer from coreFlows/coreFeatures, not by inventing new ones unless they truly follow from the vision.
|
||||
2. Ruthless scope control:
|
||||
- Default answer to "Should this be in V1?" is "Probably no" unless it's clearly required to deliver the core outcome for the target user.
|
||||
3. Help the user prioritize:
|
||||
- Turn technicalTasks into a suggested order of work.
|
||||
- Group tasks into "Today / This week / Later".
|
||||
4. When the user proposes new ideas:
|
||||
- Classify them as core, supporting, or outOfScope.
|
||||
- Explain the tradeoff in simple language.
|
||||
5. Don't over-theorize product management.
|
||||
- Give direct, actionable guidance that a solo dev can follow.
|
||||
|
||||
Tone:
|
||||
- Firm but friendly.
|
||||
- "Let's get you to shipping, not stuck in planning."
|
||||
|
||||
${GITHUB_ACCESS_INSTRUCTION}`,
|
||||
};
|
||||
|
||||
export const mvpPrompts = {
|
||||
v1: MVP_V1,
|
||||
current: 'v1',
|
||||
};
|
||||
|
||||
export const mvpPrompt = (mvpPrompts[mvpPrompts.current as 'v1'] as PromptVersion).prompt;
|
||||
|
||||
@@ -1,15 +0,0 @@
|
||||
/**
|
||||
* Shared prompt components used across multiple chat modes
|
||||
*/
|
||||
|
||||
export const GITHUB_ACCESS_INSTRUCTION = `
|
||||
|
||||
**GitHub Repository Access**:
|
||||
If the project has a connected GitHub repository (project.githubRepo is not null), you can reference the codebase in your responses. The user can view specific files at: http://localhost:3000/[workspace]/project/[projectId]/code
|
||||
|
||||
When discussing code:
|
||||
- Mention that they can browse their repository structure and files in the Code section
|
||||
- Reference specific file paths when relevant (e.g., "Check src/components/Button.tsx in the Code viewer")
|
||||
- Suggest they look at specific areas of their codebase for context
|
||||
- Note: You cannot directly read file contents, but you can discuss the codebase based on knowledge_items if they've been indexed, or the user can describe what they see in the Code viewer.`;
|
||||
|
||||
@@ -1,71 +0,0 @@
|
||||
/**
|
||||
* Vision Mode Prompt
|
||||
*
|
||||
* Purpose: Clarifies and refines product vision
|
||||
* Active when: Product model exists but no MVP plan yet
|
||||
*/
|
||||
|
||||
import { GITHUB_ACCESS_INSTRUCTION } from './shared';
|
||||
import type { PromptVersion } from './collector';
|
||||
|
||||
const VISION_V1: PromptVersion = {
|
||||
version: 'v1',
|
||||
createdAt: '2024-11-17',
|
||||
description: 'Initial version for vision clarification',
|
||||
prompt: `
|
||||
You are Vibn, an AI copilot that turns messy ideas and extracted signals into a clear product vision.
|
||||
|
||||
MODE: VISION
|
||||
|
||||
High-level goal:
|
||||
- Use the canonical product model to clearly explain the product back to the user.
|
||||
- Tighten the vision only where it's unclear.
|
||||
- Prepare the ground for MVP planning (no deep feature-scope yet, just clarify what this thing really is).
|
||||
|
||||
You will receive:
|
||||
- projectContext JSON with:
|
||||
- project
|
||||
- phaseData.canonicalProductModel (CanonicalProductModel)
|
||||
- phaseScores.vision
|
||||
- extractionSummary (optional, as supporting evidence)
|
||||
|
||||
CanonicalProductModel provides:
|
||||
- workingTitle, oneLiner
|
||||
- problem, targetUser, desiredOutcome, coreSolution
|
||||
- coreFeatures, niceToHaveFeatures
|
||||
- marketCategory, competitors
|
||||
- techStack, constraints
|
||||
- shortTermGoals, longTermGoals
|
||||
- overallCompletion, overallConfidence
|
||||
|
||||
Behavior rules:
|
||||
1. Always ground your responses in canonicalProductModel.
|
||||
- Treat it as the current "source of truth".
|
||||
- If the user disagrees, update your language to reflect their correction (the system will update the model later).
|
||||
2. Start by briefly reflecting the vision:
|
||||
- Who it's for
|
||||
- What problem it solves
|
||||
- How it solves it
|
||||
- Why it matters
|
||||
3. Ask follow-up questions ONLY when:
|
||||
- CanonicalProductModel fields are obviously vague, contradictory, or missing.
|
||||
- Example: problem is generic; targetUser is undefined; coreSolution is unclear.
|
||||
4. Do NOT re-invent a brand new idea.
|
||||
- You are refining, not replacing.
|
||||
5. Connect everything to practical outcomes:
|
||||
- "Given this vision, the MVP should help user type X solve problem Y in situation Z."
|
||||
|
||||
Tone:
|
||||
- "We're on the same side."
|
||||
- Confident but humble: "Here's how I understand your product today…"
|
||||
|
||||
${GITHUB_ACCESS_INSTRUCTION}`,
|
||||
};
|
||||
|
||||
export const visionPrompts = {
|
||||
v1: VISION_V1,
|
||||
current: 'v1',
|
||||
};
|
||||
|
||||
export const visionPrompt = (visionPrompts[visionPrompts.current as 'v1'] as PromptVersion).prompt;
|
||||
|
||||
@@ -811,9 +811,25 @@ export async function getApplicationRuntimeLogsFromApi(
|
||||
uuid: string,
|
||||
lines = 200,
|
||||
): Promise<{ logs: string }> {
|
||||
return coolifyFetch(
|
||||
`/applications/${uuid}/logs?lines=${Math.max(1, Math.min(lines, 5000))}`,
|
||||
);
|
||||
// If it's a compose service, the applications endpoint will 404, so we try the services endpoint fallback.
|
||||
// Note: Coolify API doesn't currently expose /services/{uuid}/logs natively,
|
||||
// but we should avoid throwing a 400 on the /applications path if it's explicitly a service.
|
||||
try {
|
||||
return await coolifyFetch(
|
||||
`/applications/${uuid}/logs?lines=${Math.max(1, Math.min(lines, 5000))}`,
|
||||
);
|
||||
} catch (err: any) {
|
||||
if (err?.status === 404 || err?.message?.includes("404")) {
|
||||
try {
|
||||
return await coolifyFetch(
|
||||
`/services/${uuid}/logs?lines=${Math.max(1, Math.min(lines, 5000))}`,
|
||||
);
|
||||
} catch (svcErr) {
|
||||
throw err; // throw original applications err if service logs don't exist either
|
||||
}
|
||||
}
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
|
||||
export async function listApplicationDeployments(
|
||||
|
||||
@@ -1,77 +0,0 @@
|
||||
import * as admin from 'firebase-admin';
|
||||
|
||||
// Initialize Firebase Admin SDK
|
||||
// During build time on Vercel, env vars might not be available, so we skip initialization
|
||||
const projectId = process.env.FIREBASE_PROJECT_ID;
|
||||
const clientEmail = process.env.FIREBASE_CLIENT_EMAIL;
|
||||
const privateKey = process.env.FIREBASE_PRIVATE_KEY?.replace(/\\n/g, '\n');
|
||||
|
||||
if (!admin.apps.length) {
|
||||
// Only initialize if we have credentials (skip during build)
|
||||
if (projectId && clientEmail && privateKey) {
|
||||
try {
|
||||
console.log('[Firebase Admin] Initializing...');
|
||||
console.log('[Firebase Admin] Project ID:', projectId);
|
||||
console.log('[Firebase Admin] Client Email:', clientEmail);
|
||||
console.log('[Firebase Admin] Private Key length:', privateKey?.length);
|
||||
|
||||
admin.initializeApp({
|
||||
credential: admin.credential.cert({
|
||||
projectId,
|
||||
clientEmail,
|
||||
privateKey,
|
||||
}),
|
||||
storageBucket: `${projectId}.firebasestorage.app`,
|
||||
});
|
||||
|
||||
console.log('[Firebase Admin] Initialized successfully!');
|
||||
} catch (error) {
|
||||
console.error('[Firebase Admin] Initialization failed:', error);
|
||||
}
|
||||
} else {
|
||||
console.log('[Firebase Admin] Skipping initialization - credentials not available (likely build time)');
|
||||
}
|
||||
}
|
||||
|
||||
// Helper to ensure admin is initialized
|
||||
function ensureInitialized() {
|
||||
if (!projectId || !clientEmail || !privateKey) {
|
||||
throw new Error('Firebase Admin credentials not configured');
|
||||
}
|
||||
|
||||
if (!admin.apps.length) {
|
||||
// Try to initialize if not done yet
|
||||
admin.initializeApp({
|
||||
credential: admin.credential.cert({
|
||||
projectId,
|
||||
clientEmail,
|
||||
privateKey,
|
||||
}),
|
||||
storageBucket: `${projectId}.firebasestorage.app`,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// Export admin services with lazy initialization
|
||||
export function getAdminAuth() {
|
||||
ensureInitialized();
|
||||
return admin.auth();
|
||||
}
|
||||
|
||||
export function getAdminDb() {
|
||||
ensureInitialized();
|
||||
return admin.firestore();
|
||||
}
|
||||
|
||||
export function getAdminStorage() {
|
||||
ensureInitialized();
|
||||
return admin.storage();
|
||||
}
|
||||
|
||||
// Legacy exports for backward compatibility (will work at runtime)
|
||||
export const adminAuth = admin.apps.length > 0 ? admin.auth() : ({} as any);
|
||||
export const adminDb = admin.apps.length > 0 ? admin.firestore() : ({} as any);
|
||||
export const adminStorage = admin.apps.length > 0 ? admin.storage() : ({} as any);
|
||||
|
||||
export default admin;
|
||||
|
||||
@@ -1,71 +0,0 @@
|
||||
import { db } from './config';
|
||||
import { doc, getDoc, setDoc, serverTimestamp } from 'firebase/firestore';
|
||||
import { v4 as uuidv4 } from 'uuid';
|
||||
|
||||
interface ApiKey {
|
||||
key: string;
|
||||
userId: string;
|
||||
createdAt: any;
|
||||
lastUsed?: any;
|
||||
isActive: boolean;
|
||||
}
|
||||
|
||||
// Generate a new API key for a user
|
||||
export async function generateApiKey(userId: string): Promise<string> {
|
||||
const apiKey = `vibn_${uuidv4().replace(/-/g, '')}`;
|
||||
|
||||
const keyDoc = doc(db, 'apiKeys', apiKey);
|
||||
await setDoc(keyDoc, {
|
||||
key: apiKey,
|
||||
userId,
|
||||
createdAt: serverTimestamp(),
|
||||
isActive: true,
|
||||
});
|
||||
|
||||
return apiKey;
|
||||
}
|
||||
|
||||
// Get or create API key for a user
|
||||
export async function getOrCreateApiKey(userId: string): Promise<string> {
|
||||
// Check if user already has an API key
|
||||
const userDoc = doc(db, 'users', userId);
|
||||
const userSnap = await getDoc(userDoc);
|
||||
|
||||
if (userSnap.exists() && userSnap.data().apiKey) {
|
||||
return userSnap.data().apiKey;
|
||||
}
|
||||
|
||||
// Generate new key
|
||||
const apiKey = await generateApiKey(userId);
|
||||
|
||||
// Store reference in user document
|
||||
await setDoc(userDoc, {
|
||||
apiKey,
|
||||
updatedAt: serverTimestamp(),
|
||||
}, { merge: true });
|
||||
|
||||
return apiKey;
|
||||
}
|
||||
|
||||
// Verify an API key and return the userId
|
||||
export async function verifyApiKey(apiKey: string): Promise<string | null> {
|
||||
try {
|
||||
const keyDoc = doc(db, 'apiKeys', apiKey);
|
||||
const keySnap = await getDoc(keyDoc);
|
||||
|
||||
if (!keySnap.exists() || !keySnap.data().isActive) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// Update last used timestamp
|
||||
await setDoc(keyDoc, {
|
||||
lastUsed: serverTimestamp(),
|
||||
}, { merge: true });
|
||||
|
||||
return keySnap.data().userId;
|
||||
} catch (error) {
|
||||
console.error('Error verifying API key:', error);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,116 +0,0 @@
|
||||
import {
|
||||
signInWithEmailAndPassword,
|
||||
createUserWithEmailAndPassword,
|
||||
signInWithPopup,
|
||||
GoogleAuthProvider,
|
||||
GithubAuthProvider,
|
||||
signOut as firebaseSignOut,
|
||||
onAuthStateChanged,
|
||||
User
|
||||
} from 'firebase/auth';
|
||||
import { auth } from './config';
|
||||
import { createUser, getUser } from './collections';
|
||||
|
||||
// Providers
|
||||
const googleProvider = new GoogleAuthProvider();
|
||||
const githubProvider = new GithubAuthProvider();
|
||||
|
||||
// Sign up with email/password
|
||||
export async function signUpWithEmail(email: string, password: string, displayName: string) {
|
||||
try {
|
||||
const userCredential = await createUserWithEmailAndPassword(auth, email, password);
|
||||
const user = userCredential.user;
|
||||
|
||||
// Create user document in Firestore
|
||||
// Generate workspace from email or name
|
||||
const workspace = displayName.toLowerCase().replace(/\s+/g, '-') + '-account';
|
||||
|
||||
await createUser(user.uid, {
|
||||
email: user.email!,
|
||||
displayName: displayName,
|
||||
workspace: workspace,
|
||||
});
|
||||
|
||||
return { user, workspace };
|
||||
} catch (error: any) {
|
||||
throw new Error(error.message || 'Failed to create account');
|
||||
}
|
||||
}
|
||||
|
||||
// Sign in with email/password
|
||||
export async function signInWithEmail(email: string, password: string) {
|
||||
try {
|
||||
const userCredential = await signInWithEmailAndPassword(auth, email, password);
|
||||
return userCredential.user;
|
||||
} catch (error: any) {
|
||||
throw new Error(error.message || 'Failed to sign in');
|
||||
}
|
||||
}
|
||||
|
||||
// Sign in with Google
|
||||
export async function signInWithGoogle() {
|
||||
try {
|
||||
const result = await signInWithPopup(auth, googleProvider);
|
||||
const user = result.user;
|
||||
|
||||
// Check if user exists, if not create
|
||||
const existingUser = await getUser(user.uid);
|
||||
if (!existingUser) {
|
||||
const workspace = (user.displayName || user.email!.split('@')[0]).toLowerCase().replace(/\s+/g, '-') + '-account';
|
||||
await createUser(user.uid, {
|
||||
email: user.email!,
|
||||
displayName: user.displayName || undefined,
|
||||
photoURL: user.photoURL || undefined,
|
||||
workspace: workspace,
|
||||
});
|
||||
}
|
||||
|
||||
return user;
|
||||
} catch (error: any) {
|
||||
throw new Error(error.message || 'Failed to sign in with Google');
|
||||
}
|
||||
}
|
||||
|
||||
// Sign in with GitHub
|
||||
export async function signInWithGitHub() {
|
||||
try {
|
||||
const result = await signInWithPopup(auth, githubProvider);
|
||||
const user = result.user;
|
||||
|
||||
// Check if user exists, if not create
|
||||
const existingUser = await getUser(user.uid);
|
||||
if (!existingUser) {
|
||||
const workspace = (user.displayName || user.email!.split('@')[0]).toLowerCase().replace(/\s+/g, '-') + '-account';
|
||||
await createUser(user.uid, {
|
||||
email: user.email!,
|
||||
displayName: user.displayName || undefined,
|
||||
photoURL: user.photoURL || undefined,
|
||||
workspace: workspace,
|
||||
});
|
||||
}
|
||||
|
||||
return user;
|
||||
} catch (error: any) {
|
||||
throw new Error(error.message || 'Failed to sign in with GitHub');
|
||||
}
|
||||
}
|
||||
|
||||
// Sign out
|
||||
export async function signOut() {
|
||||
try {
|
||||
await firebaseSignOut(auth);
|
||||
} catch (error: any) {
|
||||
throw new Error(error.message || 'Failed to sign out');
|
||||
}
|
||||
}
|
||||
|
||||
// Listen to auth state changes
|
||||
export function onAuthChange(callback: (user: User | null) => void) {
|
||||
return onAuthStateChanged(auth, callback);
|
||||
}
|
||||
|
||||
// Get current user
|
||||
export function getCurrentUser(): User | null {
|
||||
return auth.currentUser;
|
||||
}
|
||||
|
||||
@@ -1,167 +0,0 @@
|
||||
import { db } from './config';
|
||||
import {
|
||||
collection,
|
||||
doc,
|
||||
getDoc,
|
||||
getDocs,
|
||||
setDoc,
|
||||
updateDoc,
|
||||
query,
|
||||
where,
|
||||
serverTimestamp,
|
||||
Timestamp
|
||||
} from 'firebase/firestore';
|
||||
import type { ProjectPhase, ProjectPhaseData, ProjectPhaseScores } from '@/lib/types/project-artifacts';
|
||||
|
||||
// Type definitions
|
||||
export interface User {
|
||||
uid: string;
|
||||
email: string;
|
||||
displayName?: string;
|
||||
photoURL?: string;
|
||||
workspace: string; // e.g., "marks-account"
|
||||
createdAt: Timestamp;
|
||||
updatedAt: Timestamp;
|
||||
}
|
||||
|
||||
export interface Project {
|
||||
id: string;
|
||||
name: string;
|
||||
slug: string;
|
||||
userId: string;
|
||||
workspace: string;
|
||||
productName: string;
|
||||
productVision?: string;
|
||||
isForClient: boolean;
|
||||
hasLogo: boolean;
|
||||
hasDomain: boolean;
|
||||
hasWebsite: boolean;
|
||||
hasGithub: boolean;
|
||||
hasChatGPT: boolean;
|
||||
githubRepo?: string;
|
||||
chatGPTProjectId?: string;
|
||||
currentPhase: ProjectPhase;
|
||||
phaseStatus: 'not_started' | 'in_progress' | 'completed';
|
||||
phaseData?: ProjectPhaseData;
|
||||
phaseHistory?: Array<Record<string, unknown>>;
|
||||
phaseScores?: ProjectPhaseScores;
|
||||
createdAt: Timestamp;
|
||||
updatedAt: Timestamp;
|
||||
}
|
||||
|
||||
export interface Session {
|
||||
id: string;
|
||||
projectId?: string | null;
|
||||
userId: string;
|
||||
startTime: Timestamp;
|
||||
endTime?: Timestamp | null;
|
||||
duration?: number | null;
|
||||
workspacePath?: string | null;
|
||||
workspaceName?: string | null;
|
||||
tokensUsed: number;
|
||||
cost: number;
|
||||
model: string;
|
||||
filesModified?: string[];
|
||||
conversationSummary?: string | null;
|
||||
conversation?: Array<{
|
||||
role: string;
|
||||
content: string;
|
||||
timestamp: string | Date;
|
||||
}>;
|
||||
createdAt: Timestamp;
|
||||
}
|
||||
|
||||
export interface Analysis {
|
||||
id: string;
|
||||
projectId: string;
|
||||
type: 'code' | 'chatgpt' | 'github' | 'combined';
|
||||
summary: string;
|
||||
techStack?: string[];
|
||||
features?: string[];
|
||||
rawData?: any;
|
||||
createdAt: Timestamp;
|
||||
}
|
||||
|
||||
// User operations
|
||||
export async function createUser(uid: string, data: Partial<User>) {
|
||||
const userRef = doc(db, 'users', uid);
|
||||
await setDoc(userRef, {
|
||||
uid,
|
||||
...data,
|
||||
createdAt: serverTimestamp(),
|
||||
updatedAt: serverTimestamp(),
|
||||
});
|
||||
}
|
||||
|
||||
export async function getUser(uid: string): Promise<User | null> {
|
||||
const userRef = doc(db, 'users', uid);
|
||||
const userSnap = await getDoc(userRef);
|
||||
return userSnap.exists() ? (userSnap.data() as User) : null;
|
||||
}
|
||||
|
||||
// Project operations
|
||||
export async function createProject(projectData: Omit<Project, 'id' | 'createdAt' | 'updatedAt'>) {
|
||||
const projectRef = doc(collection(db, 'projects'));
|
||||
await setDoc(projectRef, {
|
||||
...projectData,
|
||||
id: projectRef.id,
|
||||
createdAt: serverTimestamp(),
|
||||
updatedAt: serverTimestamp(),
|
||||
});
|
||||
return projectRef.id;
|
||||
}
|
||||
|
||||
export async function getProject(projectId: string): Promise<Project | null> {
|
||||
const projectRef = doc(db, 'projects', projectId);
|
||||
const projectSnap = await getDoc(projectRef);
|
||||
return projectSnap.exists() ? (projectSnap.data() as Project) : null;
|
||||
}
|
||||
|
||||
export async function getUserProjects(userId: string): Promise<Project[]> {
|
||||
const q = query(collection(db, 'projects'), where('userId', '==', userId));
|
||||
const querySnapshot = await getDocs(q);
|
||||
return querySnapshot.docs.map(doc => doc.data() as Project);
|
||||
}
|
||||
|
||||
export async function updateProject(projectId: string, data: Partial<Project>) {
|
||||
const projectRef = doc(db, 'projects', projectId);
|
||||
await updateDoc(projectRef, {
|
||||
...data,
|
||||
updatedAt: serverTimestamp(),
|
||||
});
|
||||
}
|
||||
|
||||
// Session operations
|
||||
export async function createSession(sessionData: Omit<Session, 'id' | 'createdAt'>) {
|
||||
const sessionRef = doc(collection(db, 'sessions'));
|
||||
await setDoc(sessionRef, {
|
||||
...sessionData,
|
||||
id: sessionRef.id,
|
||||
createdAt: serverTimestamp(),
|
||||
});
|
||||
return sessionRef.id;
|
||||
}
|
||||
|
||||
export async function getProjectSessions(projectId: string): Promise<Session[]> {
|
||||
const q = query(collection(db, 'sessions'), where('projectId', '==', projectId));
|
||||
const querySnapshot = await getDocs(q);
|
||||
return querySnapshot.docs.map(doc => doc.data() as Session);
|
||||
}
|
||||
|
||||
// Analysis operations
|
||||
export async function createAnalysis(analysisData: Omit<Analysis, 'id' | 'createdAt'>) {
|
||||
const analysisRef = doc(collection(db, 'analyses'));
|
||||
await setDoc(analysisRef, {
|
||||
...analysisData,
|
||||
id: analysisRef.id,
|
||||
createdAt: serverTimestamp(),
|
||||
});
|
||||
return analysisRef.id;
|
||||
}
|
||||
|
||||
export async function getProjectAnalyses(projectId: string): Promise<Analysis[]> {
|
||||
const q = query(collection(db, 'analyses'), where('projectId', '==', projectId));
|
||||
const querySnapshot = await getDocs(q);
|
||||
return querySnapshot.docs.map(doc => doc.data() as Analysis);
|
||||
}
|
||||
|
||||
@@ -1,47 +0,0 @@
|
||||
import { initializeApp, getApps, FirebaseApp } from 'firebase/app';
|
||||
import { getAuth, Auth } from 'firebase/auth';
|
||||
import { getFirestore, Firestore } from 'firebase/firestore';
|
||||
import { getStorage, FirebaseStorage } from 'firebase/storage';
|
||||
|
||||
const firebaseConfig = {
|
||||
apiKey: process.env.NEXT_PUBLIC_FIREBASE_API_KEY,
|
||||
authDomain: process.env.NEXT_PUBLIC_FIREBASE_AUTH_DOMAIN,
|
||||
projectId: process.env.NEXT_PUBLIC_FIREBASE_PROJECT_ID,
|
||||
storageBucket: process.env.NEXT_PUBLIC_FIREBASE_STORAGE_BUCKET,
|
||||
messagingSenderId: process.env.NEXT_PUBLIC_FIREBASE_MESSAGING_SENDER_ID,
|
||||
appId: process.env.NEXT_PUBLIC_FIREBASE_APP_ID,
|
||||
measurementId: process.env.NEXT_PUBLIC_FIREBASE_MEASUREMENT_ID,
|
||||
};
|
||||
|
||||
// Only initialize if we have the API key (skip during build and if no config)
|
||||
let _app: FirebaseApp | undefined;
|
||||
let _auth: Auth | undefined;
|
||||
let _db: Firestore | undefined;
|
||||
let _storage: FirebaseStorage | undefined;
|
||||
|
||||
// Check if Firebase is properly configured
|
||||
const isFirebaseConfigured = firebaseConfig.apiKey &&
|
||||
firebaseConfig.authDomain &&
|
||||
firebaseConfig.projectId;
|
||||
|
||||
if (isFirebaseConfigured && (typeof window !== 'undefined' || firebaseConfig.apiKey)) {
|
||||
try {
|
||||
// Initialize Firebase (client-side only, safe for browser)
|
||||
_app = getApps().length === 0 ? initializeApp(firebaseConfig) : getApps()[0];
|
||||
_auth = getAuth(_app);
|
||||
_db = getFirestore(_app);
|
||||
_storage = getStorage(_app);
|
||||
} catch (error) {
|
||||
console.warn('Firebase initialization skipped - no credentials configured');
|
||||
}
|
||||
} else {
|
||||
console.warn('Firebase not configured - using PostgreSQL backend');
|
||||
}
|
||||
|
||||
// Export with type assertions - these will be defined at runtime in the browser
|
||||
// During build, they may be undefined, but won't be accessed
|
||||
export const auth = _auth as Auth;
|
||||
export const db = _db as Firestore;
|
||||
export const storage = _storage as FirebaseStorage;
|
||||
export default _app;
|
||||
|
||||
@@ -1,185 +0,0 @@
|
||||
/**
|
||||
* Project brief extraction.
|
||||
* Closes BETA_LAUNCH_PLAN P3.7.
|
||||
*
|
||||
* When a user uploads a PDF / .md / .docx / .txt brief file, we extract
|
||||
* the text here and store it on `fs_projects.data.plan.brief`. The
|
||||
* `buildSystemPrompt` function in `app/api/chat/route.ts` then surfaces
|
||||
* it in the [PROJECT BRIEF] block.
|
||||
*
|
||||
* Supports:
|
||||
* - .txt / .md — read as-is
|
||||
* - .pdf — extract text via pdf.js (no binary install required)
|
||||
* - .docx — extract via unzipper + xml text nodes
|
||||
* - .html / .htm — strip tags
|
||||
*
|
||||
* 5 MB max, 50 000 chars after extraction (truncated with a note).
|
||||
*/
|
||||
import { query } from "@/lib/db-postgres";
|
||||
import { log } from "@/lib/server/logger";
|
||||
|
||||
export const BRIEF_MAX_CHARS = 50_000;
|
||||
export const BRIEF_MAX_BYTES = 5 * 1024 * 1024;
|
||||
|
||||
export type BriefExtractionResult =
|
||||
| { ok: true; text: string; truncated: boolean; chars: number }
|
||||
| { ok: false; error: string };
|
||||
|
||||
/**
|
||||
* Extract plain text from a File-like object.
|
||||
* Call from `POST /api/projects/[projectId]/documents/upload`.
|
||||
*/
|
||||
export async function extractBriefText(
|
||||
buffer: Buffer,
|
||||
mimeType: string,
|
||||
filename: string,
|
||||
): Promise<BriefExtractionResult> {
|
||||
if (buffer.byteLength > BRIEF_MAX_BYTES) {
|
||||
return { ok: false, error: `File is too large (max 5 MB)` };
|
||||
}
|
||||
|
||||
try {
|
||||
let text = "";
|
||||
const lower = filename.toLowerCase();
|
||||
|
||||
if (lower.endsWith(".pdf") || mimeType === "application/pdf") {
|
||||
text = await extractPdf(buffer);
|
||||
} else if (
|
||||
lower.endsWith(".docx") ||
|
||||
mimeType ===
|
||||
"application/vnd.openxmlformats-officedocument.wordprocessingml.document"
|
||||
) {
|
||||
text = await extractDocx(buffer);
|
||||
} else if (lower.endsWith(".html") || lower.endsWith(".htm")) {
|
||||
text = buffer.toString("utf8").replace(/<[^>]+>/g, " ").replace(/\s+/g, " ").trim();
|
||||
} else {
|
||||
// .txt, .md, plain text
|
||||
text = buffer.toString("utf8");
|
||||
}
|
||||
|
||||
text = text.trim();
|
||||
const truncated = text.length > BRIEF_MAX_CHARS;
|
||||
if (truncated) {
|
||||
text =
|
||||
text.slice(0, BRIEF_MAX_CHARS) +
|
||||
`\n\n[Brief truncated at ${BRIEF_MAX_CHARS} chars — upload a shorter document for full coverage]`;
|
||||
}
|
||||
|
||||
return { ok: true, text, truncated, chars: text.length };
|
||||
} catch (err) {
|
||||
return {
|
||||
ok: false,
|
||||
error: `Extraction failed: ${err instanceof Error ? err.message : String(err)}`,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
async function extractPdf(buffer: Buffer): Promise<string> {
|
||||
// Dynamic import — pdf-parse is a large optional dep.
|
||||
// If not installed, fall back to an error message.
|
||||
try {
|
||||
// eslint-disable-next-line @typescript-eslint/no-require-imports
|
||||
const pdfParse = require("pdf-parse") as (
|
||||
b: Buffer,
|
||||
) => Promise<{ text: string }>;
|
||||
const result = await pdfParse(buffer);
|
||||
return result.text;
|
||||
} catch (e: unknown) {
|
||||
if (
|
||||
e instanceof Error &&
|
||||
e.message.includes("Cannot find module")
|
||||
) {
|
||||
throw new Error(
|
||||
"pdf-parse package not installed. Run `npm install pdf-parse` or upload a .txt/.md file instead.",
|
||||
);
|
||||
}
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
async function extractDocx(buffer: Buffer): Promise<string> {
|
||||
try {
|
||||
// eslint-disable-next-line @typescript-eslint/no-require-imports
|
||||
const { DOMParser } = require("@xmldom/xmldom") as {
|
||||
DOMParser: new () => { parseFromString(xml: string, type: string): Document };
|
||||
};
|
||||
// eslint-disable-next-line @typescript-eslint/no-require-imports
|
||||
const unzipper = require("unzipper") as {
|
||||
Open: {
|
||||
buffer(b: Buffer): Promise<{ files: Array<{ path: string; buffer(): Promise<Buffer> }> }>;
|
||||
};
|
||||
};
|
||||
|
||||
const directory = await unzipper.Open.buffer(buffer);
|
||||
const wordDoc = directory.files.find(
|
||||
(f: { path: string }) => f.path === "word/document.xml",
|
||||
);
|
||||
if (!wordDoc) throw new Error("word/document.xml not found in docx");
|
||||
|
||||
const xmlBuf = await wordDoc.buffer();
|
||||
const xml = xmlBuf.toString("utf8");
|
||||
|
||||
const doc = new DOMParser().parseFromString(xml, "text/xml");
|
||||
const texts: string[] = [];
|
||||
|
||||
function extractText(node: Node) {
|
||||
if (node.nodeType === 3 /* TEXT_NODE */) {
|
||||
const t = (node as Text).textContent?.trim();
|
||||
if (t) texts.push(t);
|
||||
}
|
||||
node.childNodes?.forEach((child: Node) => extractText(child));
|
||||
}
|
||||
extractText(doc);
|
||||
|
||||
return texts.join(" ");
|
||||
} catch (e: unknown) {
|
||||
if (e instanceof Error && e.message.includes("Cannot find module")) {
|
||||
throw new Error(
|
||||
"unzipper or @xmldom/xmldom not installed. Upload a .txt or .md file instead.",
|
||||
);
|
||||
}
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Persist the extracted brief text to `fs_projects.data.plan.brief`.
|
||||
* Called by the upload route after extraction succeeds.
|
||||
*/
|
||||
export async function persistProjectBrief(
|
||||
projectId: string,
|
||||
text: string,
|
||||
meta: { filename: string; chars: number; truncated: boolean },
|
||||
): Promise<void> {
|
||||
try {
|
||||
await query(
|
||||
`UPDATE fs_projects
|
||||
SET data = jsonb_set(
|
||||
data,
|
||||
'{plan}',
|
||||
COALESCE(data->'plan', '{}'::jsonb)
|
||||
|| jsonb_build_object(
|
||||
'brief', $1::text,
|
||||
'briefMeta', $2::jsonb
|
||||
),
|
||||
true
|
||||
)
|
||||
WHERE id = $3`,
|
||||
[
|
||||
text,
|
||||
JSON.stringify({
|
||||
...meta,
|
||||
uploadedAt: new Date().toISOString(),
|
||||
}),
|
||||
projectId,
|
||||
],
|
||||
);
|
||||
log.info("project brief persisted", { projectId, chars: meta.chars });
|
||||
} catch (err) {
|
||||
log.error("brief persist failed", {
|
||||
projectId,
|
||||
err: err instanceof Error ? err.message : String(err),
|
||||
});
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
@@ -34,7 +34,7 @@ export function slugify(name: string): string {
|
||||
* workspaceAppFqdn('mark', 'my-api') === 'my-api.mark.vibnai.com'
|
||||
*/
|
||||
export function workspaceAppFqdn(workspaceSlug: string, appSlug: string): string {
|
||||
return `${appSlug}.${workspaceSlug}.${VIBN_BASE_DOMAIN}`;
|
||||
return `${appSlug}-${workspaceSlug}.${VIBN_BASE_DOMAIN}`;
|
||||
}
|
||||
|
||||
/** `https://{fqdn}` — what Coolify's `domains` field expects. */
|
||||
@@ -55,7 +55,7 @@ export function parseDomainsString(domains: string | null | undefined): string[]
|
||||
/** Guard against cross-workspace or disallowed domains. */
|
||||
export function isDomainUnderWorkspace(fqdn: string, workspaceSlug: string): boolean {
|
||||
const f = fqdn.replace(/^https?:\/\//, '').toLowerCase();
|
||||
return f === `${workspaceSlug}.${VIBN_BASE_DOMAIN}` || f.endsWith(`.${workspaceSlug}.${VIBN_BASE_DOMAIN}`);
|
||||
return f === `${workspaceSlug}.${VIBN_BASE_DOMAIN}` || f.endsWith(`-${workspaceSlug}.${VIBN_BASE_DOMAIN}`) || f.endsWith(`.${workspaceSlug}.${VIBN_BASE_DOMAIN}`);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,51 +0,0 @@
|
||||
/**
|
||||
* SSRF guard + client/server agreement for /api/preview/embed.
|
||||
* Only tunnel-like preview hosts should be proxied — never arbitrary URLs.
|
||||
*/
|
||||
|
||||
export function isPrivateIpHostname(hostname: string): boolean {
|
||||
const m = /^(\d+)\.(\d+)\.(\d+)\.(\d+)$/.exec(hostname);
|
||||
if (!m) return false;
|
||||
const a = Number(m[1]);
|
||||
const b = Number(m[2]);
|
||||
if (a === 10) return true;
|
||||
if (a === 172 && b >= 16 && b <= 31) return true;
|
||||
if (a === 192 && b === 168) return true;
|
||||
if (a === 127) return true;
|
||||
if (a === 169 && b === 254) return true;
|
||||
if (a === 0) return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
/** Server-side hostname allowlist after redirects */
|
||||
export function serverPreviewHostnameAllowed(hostname: string, protocol: string): boolean {
|
||||
const h = hostname.toLowerCase();
|
||||
const p = protocol.toLowerCase();
|
||||
if (p !== "http:" && p !== "https:") return false;
|
||||
if (isPrivateIpHostname(h)) return false;
|
||||
if (h === "localhost" || h === "127.0.0.1") {
|
||||
return process.env.NODE_ENV === "development";
|
||||
}
|
||||
if (h.endsWith(".preview.vibnai.com")) return true;
|
||||
if (h === "preview.vibnai.com") return true;
|
||||
const suffixes = (process.env.NEXT_PUBLIC_PREVIEW_EMBED_PROXY_HOST_SUFFIXES ?? "")
|
||||
.split(",")
|
||||
.map((s) => s.trim().toLowerCase())
|
||||
.filter(Boolean);
|
||||
return suffixes.some((suf) => (suf.startsWith(".") ? h.endsWith(suf) : h === suf));
|
||||
}
|
||||
|
||||
/**
|
||||
* Remote URLs we should load through /api/preview/embed so the bridge script is same-origin.
|
||||
* Same-origin targets return false (already works without proxy).
|
||||
*/
|
||||
export function previewUrlEligibleForEmbedProxy(rawUrl: string, appOrigin: string): boolean {
|
||||
try {
|
||||
const u = new URL(rawUrl);
|
||||
const app = new URL(appOrigin);
|
||||
if (u.origin === app.origin) return false;
|
||||
return serverPreviewHostnameAllowed(u.hostname, u.protocol);
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -1,877 +0,0 @@
|
||||
/**
|
||||
* Build a fully-formed product webpage that demonstrates a design system in
|
||||
* action — not just a list of tokens, but a real-feeling marketing /
|
||||
* product page (nav, hero, social proof, feature grid, dashboard preview,
|
||||
* pricing, testimonials, FAQ, CTA, footer) styled entirely from the
|
||||
* tokens we extract from the system's DESIGN.md.
|
||||
*
|
||||
* Same parsing utilities as design-system-preview.js — kept inline rather
|
||||
* than imported so the two views can evolve independently.
|
||||
*/
|
||||
|
||||
type ColorToken = { name: string; value: string; role: string };
|
||||
type FontHints = { display?: string; heading?: string; body?: string; mono?: string };
|
||||
type RowStatus = 'up' | '';
|
||||
|
||||
export function renderDesignSystemShowcase(id: string, raw: string): string {
|
||||
const titleMatch = /^#\s+(.+?)\s*$/m.exec(raw);
|
||||
const rawTitle = titleMatch?.[1] ?? id;
|
||||
const title = cleanTitle(rawTitle);
|
||||
const subtitle = extractSubtitle(raw) || 'A design system rendered as a real product surface.';
|
||||
const colors = extractColors(raw);
|
||||
const fonts = extractFonts(raw);
|
||||
|
||||
// Hints are matched against each color's role description (the prose that
|
||||
// follows the name in DESIGN.md, e.g. "Primary background.") first, then
|
||||
// against the color name. We use word-boundary matching so descriptive
|
||||
// names like "Cardinal Red" don't accidentally satisfy a "card" hint and
|
||||
// "Gem Pink" doesn't satisfy "ink".
|
||||
// Hint ordering matters: more specific phrases come first so a system
|
||||
// with both "Primary background" and "Page background in light mode" (e.g.
|
||||
// Linear's marketing black + light-mode escape hatch) lands on the
|
||||
// dominant role rather than the light-mode subtitle. We drop 'page
|
||||
// background' from the bg hints entirely because in practice it almost
|
||||
// always belongs to a secondary, light-mode-only entry.
|
||||
const bg =
|
||||
pickColor(colors, ['primary background', 'background', 'canvas', 'paper'])
|
||||
?? firstLightish(colors)
|
||||
?? '#ffffff';
|
||||
// Exclude `bg` so a token whose hex matches the page background (for
|
||||
// example Warp's "Warm Parchment" doubling as primary text *and* the
|
||||
// firstLightish bg fallback) doesn't make body copy invisible.
|
||||
const fg =
|
||||
pickColor(
|
||||
colors,
|
||||
[
|
||||
'primary text',
|
||||
'body text',
|
||||
'foreground',
|
||||
'ink primary',
|
||||
'heading',
|
||||
'ink',
|
||||
'graphite',
|
||||
'navy',
|
||||
],
|
||||
[bg],
|
||||
)
|
||||
?? pickReadableForeground(bg)
|
||||
?? '#0a0a0a';
|
||||
const accent =
|
||||
pickColor(colors, [
|
||||
'brand primary',
|
||||
'primary brand',
|
||||
'primary cta',
|
||||
'gradient origin',
|
||||
'brand mark',
|
||||
'brand color',
|
||||
])
|
||||
?? firstNonNeutral(colors, [bg, fg])
|
||||
?? '#2f6feb';
|
||||
const accent2 =
|
||||
pickColor(colors, [
|
||||
'brand secondary',
|
||||
'secondary brand',
|
||||
'gradient terminus',
|
||||
'tertiary brand',
|
||||
'tertiary',
|
||||
'highlight',
|
||||
])
|
||||
?? secondNonNeutral(colors, [accent, bg, fg])
|
||||
?? accent;
|
||||
const muted =
|
||||
pickColor(colors, ['secondary text', 'caption', 'metadata', 'placeholder', 'muted', 'subtle'])
|
||||
?? '#666666';
|
||||
const border =
|
||||
pickColor(colors, ['border', 'divider', 'hairline', 'rule', 'stroke'])
|
||||
?? '#e6e6e6';
|
||||
const surface =
|
||||
pickColor(colors, [
|
||||
'secondary surface',
|
||||
'section break',
|
||||
'sidebar',
|
||||
'surface subtle',
|
||||
'surface',
|
||||
'panel',
|
||||
'elevated',
|
||||
'card surface',
|
||||
])
|
||||
?? mixSurface(bg);
|
||||
|
||||
const display = fonts.display ?? fonts.heading ?? "system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif";
|
||||
const body = fonts.body ?? display;
|
||||
const mono = fonts.mono ?? "ui-monospace, 'JetBrains Mono', monospace";
|
||||
|
||||
const accentFg = pickReadableForeground(accent);
|
||||
const accent2Fg = pickReadableForeground(accent2);
|
||||
|
||||
const productName = title;
|
||||
const tagline = oneLine(subtitle).slice(0, 120);
|
||||
|
||||
return `<!doctype html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="utf-8" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
||||
<title>${escapeHtml(productName)} — showcase</title>
|
||||
<style>
|
||||
:root {
|
||||
--bg: ${bg};
|
||||
--fg: ${fg};
|
||||
--accent: ${accent};
|
||||
--accent-fg: ${accentFg};
|
||||
--accent-2: ${accent2};
|
||||
--accent-2-fg: ${accent2Fg};
|
||||
--muted: ${muted};
|
||||
--border: ${border};
|
||||
--surface: ${surface};
|
||||
--display: ${display};
|
||||
--body: ${body};
|
||||
--mono: ${mono};
|
||||
}
|
||||
* { box-sizing: border-box; }
|
||||
html, body { margin: 0; padding: 0; }
|
||||
body {
|
||||
background: var(--bg);
|
||||
color: var(--fg);
|
||||
font-family: var(--body);
|
||||
line-height: 1.6;
|
||||
font-size: 16px;
|
||||
-webkit-font-smoothing: antialiased;
|
||||
}
|
||||
a { color: inherit; text-decoration: none; }
|
||||
img { max-width: 100%; display: block; }
|
||||
.container { max-width: 1180px; margin: 0 auto; padding: 0 28px; }
|
||||
|
||||
/* Nav */
|
||||
.nav {
|
||||
position: sticky; top: 0; z-index: 30;
|
||||
background: rgba(255,255,255,0.7);
|
||||
backdrop-filter: saturate(180%) blur(14px);
|
||||
border-bottom: 1px solid var(--border);
|
||||
}
|
||||
.nav-row {
|
||||
display: flex; align-items: center; gap: 32px;
|
||||
height: 64px;
|
||||
}
|
||||
.brand { display: flex; align-items: center; gap: 10px; font-family: var(--display); font-weight: 700; font-size: 17px; letter-spacing: -0.01em; }
|
||||
.brand-mark {
|
||||
width: 26px; height: 26px; border-radius: 7px;
|
||||
background: linear-gradient(135deg, var(--accent), var(--accent-2));
|
||||
}
|
||||
.nav-links { display: flex; gap: 22px; font-size: 14px; color: var(--muted); }
|
||||
.nav-links a:hover { color: var(--fg); }
|
||||
.nav-spacer { flex: 1; }
|
||||
.nav-cta {
|
||||
display: inline-flex; align-items: center; gap: 6px;
|
||||
background: var(--fg); color: var(--bg);
|
||||
padding: 8px 14px; border-radius: 8px; font-size: 13px; font-weight: 500;
|
||||
}
|
||||
.nav-link-cta { color: var(--fg); font-weight: 500; font-size: 14px; }
|
||||
|
||||
/* Hero */
|
||||
.hero { padding: 96px 0 72px; }
|
||||
.hero-eyebrow {
|
||||
display: inline-flex; align-items: center; gap: 8px;
|
||||
font-family: var(--mono); font-size: 12px; color: var(--muted);
|
||||
text-transform: uppercase; letter-spacing: 0.08em;
|
||||
padding: 6px 12px; border: 1px solid var(--border); border-radius: 999px;
|
||||
background: var(--surface);
|
||||
margin-bottom: 24px;
|
||||
}
|
||||
.hero-eyebrow .dot { width: 6px; height: 6px; border-radius: 50%; background: var(--accent); }
|
||||
.hero h1 {
|
||||
font-family: var(--display);
|
||||
font-size: clamp(44px, 6.6vw, 84px);
|
||||
line-height: 1.02;
|
||||
letter-spacing: -0.025em;
|
||||
margin: 0 0 22px;
|
||||
max-width: 18ch;
|
||||
font-weight: 700;
|
||||
}
|
||||
.hero h1 em { font-style: normal; background: linear-gradient(120deg, var(--accent), var(--accent-2)); -webkit-background-clip: text; background-clip: text; color: transparent; }
|
||||
.hero p.lede {
|
||||
font-size: 19px; color: var(--muted);
|
||||
max-width: 56ch; margin: 0 0 36px;
|
||||
}
|
||||
.hero-actions { display: flex; gap: 12px; flex-wrap: wrap; align-items: center; }
|
||||
.btn {
|
||||
font: inherit; cursor: pointer; border-radius: 10px;
|
||||
padding: 13px 22px; font-size: 14.5px; font-weight: 500;
|
||||
border: 1px solid transparent; display: inline-flex; align-items: center; gap: 8px;
|
||||
}
|
||||
.btn-primary { background: var(--accent); color: var(--accent-fg); border-color: var(--accent); }
|
||||
.btn-primary:hover { filter: brightness(1.06); }
|
||||
.btn-ghost { background: transparent; color: var(--fg); border-color: var(--border); }
|
||||
.btn-ghost:hover { background: var(--surface); }
|
||||
.hero-meta { display: flex; gap: 24px; margin-top: 44px; color: var(--muted); font-size: 13px; }
|
||||
.hero-meta span strong { color: var(--fg); font-weight: 600; }
|
||||
|
||||
/* Logo strip */
|
||||
.logos { padding: 36px 0; border-top: 1px solid var(--border); border-bottom: 1px solid var(--border); }
|
||||
.logos-label { font-size: 12px; color: var(--muted); text-align: center; letter-spacing: 0.08em; text-transform: uppercase; margin-bottom: 18px; }
|
||||
.logos-row { display: flex; flex-wrap: wrap; justify-content: center; gap: 44px; align-items: center; opacity: 0.85; }
|
||||
.logo-pill { font-family: var(--display); font-weight: 700; font-size: 17px; letter-spacing: -0.01em; color: var(--muted); }
|
||||
|
||||
/* Features grid */
|
||||
.section { padding: 96px 0; }
|
||||
.section-eyebrow { font-family: var(--mono); text-transform: uppercase; letter-spacing: 0.1em; font-size: 12px; color: var(--accent); margin-bottom: 12px; }
|
||||
.section-title { font-family: var(--display); font-size: clamp(32px, 4.2vw, 48px); letter-spacing: -0.02em; line-height: 1.1; margin: 0 0 18px; max-width: 22ch; font-weight: 700; }
|
||||
.section-lede { color: var(--muted); font-size: 17px; max-width: 56ch; margin: 0 0 48px; }
|
||||
.features {
|
||||
display: grid; gap: 18px;
|
||||
grid-template-columns: repeat(3, 1fr);
|
||||
}
|
||||
@media (max-width: 920px) { .features { grid-template-columns: 1fr 1fr; } }
|
||||
@media (max-width: 600px) { .features { grid-template-columns: 1fr; } }
|
||||
.feature {
|
||||
background: var(--surface); border: 1px solid var(--border); border-radius: 14px;
|
||||
padding: 26px; display: flex; flex-direction: column; gap: 12px;
|
||||
}
|
||||
.feature-icon {
|
||||
width: 36px; height: 36px; border-radius: 8px;
|
||||
background: linear-gradient(135deg, var(--accent), var(--accent-2));
|
||||
color: var(--accent-fg);
|
||||
display: inline-flex; align-items: center; justify-content: center;
|
||||
font-size: 18px; font-weight: 700;
|
||||
}
|
||||
.feature h3 { font-family: var(--display); font-size: 18px; margin: 0; letter-spacing: -0.01em; }
|
||||
.feature p { color: var(--muted); margin: 0; font-size: 14.5px; line-height: 1.55; }
|
||||
|
||||
/* Product preview / dashboard mock */
|
||||
.preview-wrap { padding-top: 24px; padding-bottom: 96px; }
|
||||
.preview-frame {
|
||||
background: var(--surface); border: 1px solid var(--border); border-radius: 18px;
|
||||
padding: 14px;
|
||||
box-shadow: 0 30px 80px rgba(0,0,0,0.06), 0 12px 30px rgba(0,0,0,0.04);
|
||||
}
|
||||
.preview-titlebar { display: flex; gap: 6px; padding: 4px 8px 12px; }
|
||||
.preview-titlebar span { width: 10px; height: 10px; border-radius: 50%; background: var(--border); }
|
||||
.preview-app {
|
||||
background: var(--bg); border: 1px solid var(--border); border-radius: 12px;
|
||||
display: grid; grid-template-columns: 220px 1fr; min-height: 440px; overflow: hidden;
|
||||
}
|
||||
.preview-side { background: var(--surface); border-right: 1px solid var(--border); padding: 18px 14px; display: flex; flex-direction: column; gap: 4px; }
|
||||
.side-link { display: flex; align-items: center; gap: 10px; padding: 8px 10px; border-radius: 8px; font-size: 13.5px; color: var(--muted); }
|
||||
.side-link.active { background: var(--bg); color: var(--fg); font-weight: 500; box-shadow: inset 0 0 0 1px var(--border); }
|
||||
.side-link .dot { width: 6px; height: 6px; border-radius: 50%; background: var(--accent); }
|
||||
.side-section { font-family: var(--mono); text-transform: uppercase; font-size: 10px; letter-spacing: 0.08em; color: var(--muted); padding: 14px 10px 6px; }
|
||||
.preview-main { padding: 22px 24px; display: flex; flex-direction: column; gap: 22px; }
|
||||
.preview-head { display: flex; align-items: center; justify-content: space-between; }
|
||||
.preview-head h4 { font-family: var(--display); font-size: 22px; margin: 0; letter-spacing: -0.01em; }
|
||||
.kpi-row { display: grid; grid-template-columns: repeat(4, 1fr); gap: 14px; }
|
||||
.kpi { background: var(--surface); border: 1px solid var(--border); border-radius: 10px; padding: 14px 16px; }
|
||||
.kpi .label { font-size: 11.5px; color: var(--muted); text-transform: uppercase; letter-spacing: 0.06em; }
|
||||
.kpi .value { font-family: var(--display); font-size: 24px; font-weight: 700; margin-top: 4px; letter-spacing: -0.01em; }
|
||||
.kpi .delta { font-family: var(--mono); font-size: 11.5px; margin-top: 2px; color: var(--accent); }
|
||||
.chart-card { background: var(--surface); border: 1px solid var(--border); border-radius: 12px; padding: 18px; }
|
||||
.chart-head { display: flex; justify-content: space-between; align-items: baseline; margin-bottom: 8px; }
|
||||
.chart-head .title { font-weight: 600; font-size: 14px; }
|
||||
.chart-head .meta { font-family: var(--mono); font-size: 11px; color: var(--muted); }
|
||||
.chart svg { width: 100%; height: 160px; display: block; }
|
||||
.preview-row-2 { display: grid; grid-template-columns: 1.6fr 1fr; gap: 14px; }
|
||||
.list-card { background: var(--surface); border: 1px solid var(--border); border-radius: 12px; }
|
||||
.list-row { display: grid; grid-template-columns: 1fr auto auto; gap: 12px; padding: 12px 16px; border-top: 1px solid var(--border); align-items: center; }
|
||||
.list-row:first-of-type { border-top: none; }
|
||||
.list-row .name { font-weight: 500; font-size: 13.5px; }
|
||||
.list-row .meta { font-family: var(--mono); font-size: 11.5px; color: var(--muted); }
|
||||
.badge { display: inline-flex; align-items: center; gap: 6px; padding: 3px 8px; border-radius: 999px; font-size: 11px; font-weight: 500; background: var(--bg); border: 1px solid var(--border); color: var(--muted); }
|
||||
.badge.up { color: var(--accent); border-color: color-mix(in srgb, var(--accent) 30%, transparent); }
|
||||
.list-card .head { display: flex; justify-content: space-between; align-items: baseline; padding: 14px 16px; border-bottom: 1px solid var(--border); }
|
||||
.list-card .head h5 { margin: 0; font-size: 14px; }
|
||||
|
||||
/* Pricing */
|
||||
.pricing { display: grid; grid-template-columns: repeat(3, 1fr); gap: 18px; }
|
||||
@media (max-width: 920px) { .pricing { grid-template-columns: 1fr; } }
|
||||
.price-card {
|
||||
background: var(--surface); border: 1px solid var(--border); border-radius: 16px;
|
||||
padding: 28px; display: flex; flex-direction: column; gap: 18px;
|
||||
}
|
||||
.price-card.featured {
|
||||
background: var(--fg); color: var(--bg); border-color: var(--fg);
|
||||
}
|
||||
.price-card.featured .muted, .price-card.featured h3, .price-card.featured .price { color: var(--bg); }
|
||||
.price-card .tier-name { font-family: var(--display); font-size: 14px; font-weight: 600; letter-spacing: 0.04em; text-transform: uppercase; color: var(--muted); }
|
||||
.price-card .price { font-family: var(--display); font-size: 44px; font-weight: 700; letter-spacing: -0.02em; line-height: 1; }
|
||||
.price-card .price small { font-size: 14px; color: var(--muted); font-weight: 400; }
|
||||
.price-card ul { list-style: none; padding: 0; margin: 0; display: flex; flex-direction: column; gap: 10px; font-size: 14.5px; }
|
||||
.price-card li::before { content: "✓"; color: var(--accent); margin-right: 8px; font-weight: 700; }
|
||||
.price-card.featured li::before { color: var(--accent-2); }
|
||||
|
||||
/* Testimonials */
|
||||
.quotes { display: grid; grid-template-columns: 1fr 1fr; gap: 18px; }
|
||||
@media (max-width: 760px) { .quotes { grid-template-columns: 1fr; } }
|
||||
.quote { background: var(--surface); border: 1px solid var(--border); border-radius: 14px; padding: 26px; display: flex; flex-direction: column; gap: 18px; }
|
||||
.quote p { font-size: 17px; line-height: 1.55; margin: 0; font-family: var(--display); letter-spacing: -0.01em; }
|
||||
.quote-author { display: flex; align-items: center; gap: 12px; }
|
||||
.quote-author .avatar { width: 36px; height: 36px; border-radius: 50%; background: linear-gradient(135deg, var(--accent), var(--accent-2)); }
|
||||
.quote-author .name { font-weight: 600; font-size: 13.5px; }
|
||||
.quote-author .role { font-size: 12.5px; color: var(--muted); }
|
||||
|
||||
/* FAQ */
|
||||
.faq { display: grid; grid-template-columns: 1fr 1fr; gap: 14px 32px; }
|
||||
@media (max-width: 760px) { .faq { grid-template-columns: 1fr; } }
|
||||
.faq-item { padding: 18px 0; border-top: 1px solid var(--border); }
|
||||
.faq-item h4 { margin: 0 0 6px; font-family: var(--display); font-size: 17px; letter-spacing: -0.01em; }
|
||||
.faq-item p { margin: 0; color: var(--muted); font-size: 14.5px; }
|
||||
|
||||
/* CTA */
|
||||
.cta {
|
||||
margin: 48px 0 96px;
|
||||
background: linear-gradient(135deg, var(--accent), var(--accent-2));
|
||||
color: var(--accent-fg);
|
||||
border-radius: 24px;
|
||||
padding: 64px 56px;
|
||||
display: grid;
|
||||
grid-template-columns: 1.4fr auto;
|
||||
gap: 32px;
|
||||
align-items: center;
|
||||
}
|
||||
@media (max-width: 760px) { .cta { grid-template-columns: 1fr; padding: 36px; } }
|
||||
.cta h2 { font-family: var(--display); font-size: clamp(28px, 4vw, 40px); letter-spacing: -0.02em; margin: 0 0 10px; line-height: 1.1; max-width: 22ch; }
|
||||
.cta p { margin: 0; opacity: 0.92; font-size: 16px; max-width: 50ch; }
|
||||
.cta .btn { background: var(--accent-fg); color: var(--accent); border: none; }
|
||||
.cta .btn-secondary { background: transparent; color: var(--accent-fg); border: 1px solid color-mix(in srgb, var(--accent-fg) 35%, transparent); }
|
||||
|
||||
/* Footer */
|
||||
footer { border-top: 1px solid var(--border); padding: 36px 0 56px; color: var(--muted); font-size: 13.5px; }
|
||||
.footer-row { display: grid; grid-template-columns: 2fr 1fr 1fr 1fr; gap: 32px; margin-bottom: 32px; }
|
||||
@media (max-width: 760px) { .footer-row { grid-template-columns: 1fr 1fr; } }
|
||||
.footer-col h6 { color: var(--fg); font-family: var(--display); font-size: 13.5px; margin: 0 0 12px; font-weight: 600; }
|
||||
.footer-col a { display: block; padding: 4px 0; }
|
||||
.footer-col a:hover { color: var(--fg); }
|
||||
.footer-bottom { display: flex; justify-content: space-between; padding-top: 24px; border-top: 1px solid var(--border); }
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<header class="nav">
|
||||
<div class="container nav-row">
|
||||
<a class="brand" href="#"><span class="brand-mark"></span>${escapeHtml(productName)}</a>
|
||||
<nav class="nav-links">
|
||||
<a href="#features">Product</a>
|
||||
<a href="#preview">Workspace</a>
|
||||
<a href="#pricing">Pricing</a>
|
||||
<a href="#faq">Docs</a>
|
||||
<a href="#faq">Customers</a>
|
||||
</nav>
|
||||
<div class="nav-spacer"></div>
|
||||
<a class="nav-link-cta" href="#">Sign in</a>
|
||||
<a class="nav-cta" href="#">Get started →</a>
|
||||
</div>
|
||||
</header>
|
||||
|
||||
<main>
|
||||
<section class="hero">
|
||||
<div class="container">
|
||||
<div class="hero-eyebrow"><span class="dot"></span>${escapeHtml(productName)} · live preview</div>
|
||||
<h1>The system that makes <em>${escapeHtml(productName)}</em> feel like ${escapeHtml(productName)}.</h1>
|
||||
<p class="lede">${escapeHtml(tagline)}</p>
|
||||
<div class="hero-actions">
|
||||
<a class="btn btn-primary" href="#">Start a free trial →</a>
|
||||
<a class="btn btn-ghost" href="#preview">See it in action</a>
|
||||
</div>
|
||||
<div class="hero-meta">
|
||||
<span><strong>4.9</strong> · App Store rating</span>
|
||||
<span><strong>SOC 2</strong> · Type II compliant</span>
|
||||
<span><strong>120k+</strong> active teams</span>
|
||||
</div>
|
||||
</div>
|
||||
</section>
|
||||
|
||||
<section class="logos">
|
||||
<div class="container">
|
||||
<div class="logos-label">Trusted by teams shipping serious work</div>
|
||||
<div class="logos-row">
|
||||
<span class="logo-pill">Northwind</span>
|
||||
<span class="logo-pill">Pioneer</span>
|
||||
<span class="logo-pill">Lattice</span>
|
||||
<span class="logo-pill">Atlas Co.</span>
|
||||
<span class="logo-pill">Voltage</span>
|
||||
<span class="logo-pill">Foundry</span>
|
||||
</div>
|
||||
</div>
|
||||
</section>
|
||||
|
||||
<section class="section" id="features">
|
||||
<div class="container">
|
||||
<div class="section-eyebrow">What it does</div>
|
||||
<h2 class="section-title">Every primitive a fast team needs.</h2>
|
||||
<p class="section-lede">A system styled entirely from the tokens of ${escapeHtml(productName)} — palette, typography, surfaces, and motion. Drop it into any product and it stays in character.</p>
|
||||
<div class="features">
|
||||
${featureCard('★', 'Tokens that compose', 'Color, type, spacing, and elevation defined once and reused across every surface — from a marketing hero to a row in a table.')}
|
||||
${featureCard('◐', 'Light & dark in lockstep', 'Every component ships with both modes. The accent reads as confident in either context, and contrast meets WCAG AA out of the box.')}
|
||||
${featureCard('⌘', 'Desktop-first, but mobile-honest', 'Layouts collapse from a 12-column desktop grid to a focused single column without losing density or rhythm.')}
|
||||
${featureCard('▣', 'Production-grade primitives', '40+ components — from the obvious (button, input) to the load-bearing (data table, command bar, empty states).')}
|
||||
${featureCard('↗', 'Designed for handoff', 'Every spec carries a Figma frame, a code snippet, and a "do/don’t" pair so engineers don’t have to guess.')}
|
||||
${featureCard('∞', 'Built to evolve', 'Tokens version semver-style. A palette refresh ships through one file — no component code touches.')}
|
||||
</div>
|
||||
</div>
|
||||
</section>
|
||||
|
||||
<section class="preview-wrap" id="preview">
|
||||
<div class="container">
|
||||
<div class="section-eyebrow">In production</div>
|
||||
<h2 class="section-title">A workspace, fully styled.</h2>
|
||||
<p class="section-lede">This is the same component library you'd use in your app — rendered with ${escapeHtml(productName)} tokens.</p>
|
||||
<div class="preview-frame">
|
||||
<div class="preview-titlebar"><span></span><span></span><span></span></div>
|
||||
<div class="preview-app">
|
||||
<aside class="preview-side">
|
||||
<div class="brand" style="margin-bottom: 14px;"><span class="brand-mark"></span>${escapeHtml(productName)}</div>
|
||||
<a class="side-link active"><span class="dot"></span>Overview</a>
|
||||
<a class="side-link">Customers</a>
|
||||
<a class="side-link">Pipeline</a>
|
||||
<a class="side-link">Reports</a>
|
||||
<a class="side-link">Automations</a>
|
||||
<div class="side-section">Workspaces</div>
|
||||
<a class="side-link">Growth</a>
|
||||
<a class="side-link">Lifecycle</a>
|
||||
<a class="side-link">Finance</a>
|
||||
</aside>
|
||||
<div class="preview-main">
|
||||
<div class="preview-head">
|
||||
<h4>Overview</h4>
|
||||
<span class="badge up">↑ 12.4% this week</span>
|
||||
</div>
|
||||
<div class="kpi-row">
|
||||
${kpi('MRR', '$184,210', '+8.2%')}
|
||||
${kpi('Active orgs', '2,914', '+121')}
|
||||
${kpi('Conversion', '4.6%', '+0.4 pp')}
|
||||
${kpi('Net retention', '113%', '+2 pp')}
|
||||
</div>
|
||||
<div class="chart-card">
|
||||
<div class="chart-head">
|
||||
<span class="title">Revenue · last 12 weeks</span>
|
||||
<span class="meta">USD · weekly</span>
|
||||
</div>
|
||||
<div class="chart">
|
||||
${inlineLineChart()}
|
||||
</div>
|
||||
</div>
|
||||
<div class="preview-row-2">
|
||||
<div class="list-card">
|
||||
<div class="head">
|
||||
<h5>Top accounts</h5>
|
||||
<span class="badge">View all</span>
|
||||
</div>
|
||||
${listRow('Northwind Trading', 'Annual · NA', '$48,200', 'up')}
|
||||
${listRow('Pioneer Robotics', 'Quarterly · EMEA', '$31,890', 'up')}
|
||||
${listRow('Atlas Cooperative', 'Annual · APAC', '$22,400', '')}
|
||||
${listRow('Foundry Group', 'Monthly · NA', '$14,750', 'up')}
|
||||
</div>
|
||||
<div class="list-card">
|
||||
<div class="head">
|
||||
<h5>Activity</h5>
|
||||
<span class="badge">Live</span>
|
||||
</div>
|
||||
${activityRow('Renewal closed', 'Lattice · 11m ago')}
|
||||
${activityRow('Trial started', 'Voltage · 22m ago')}
|
||||
${activityRow('Plan upgraded', 'Pioneer · 1h ago')}
|
||||
${activityRow('Invoice paid', 'Atlas · 2h ago')}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</section>
|
||||
|
||||
<section class="section" id="pricing" style="padding-top: 24px;">
|
||||
<div class="container">
|
||||
<div class="section-eyebrow">Pricing</div>
|
||||
<h2 class="section-title">Built for teams of one to one thousand.</h2>
|
||||
<p class="section-lede">Pick the plan that matches the way your team ships. Every tier ships the full token system.</p>
|
||||
<div class="pricing">
|
||||
${priceCard('Starter', '$0', 'Free forever', ['Single user', 'All core tokens', 'Up to 3 projects', 'Community support'])}
|
||||
${priceCard('Team', '$24', 'per seat / month', ['Unlimited projects', 'Real-time co-edit', 'Brand themes', 'Priority email support'], true)}
|
||||
${priceCard('Enterprise', 'Custom', 'volume pricing', ['SSO + SCIM', 'Audit logs', 'Custom token schemas', 'Dedicated success manager'])}
|
||||
</div>
|
||||
</div>
|
||||
</section>
|
||||
|
||||
<section class="section">
|
||||
<div class="container">
|
||||
<div class="section-eyebrow">Customers</div>
|
||||
<h2 class="section-title">Loved by teams who care about craft.</h2>
|
||||
<div class="quotes">
|
||||
${quote('"Our marketing site, our app, and our internal dashboards finally feel like the same product. The token system is doing all the work."', 'Mira Okafor', 'Head of Design · Pioneer')}
|
||||
${quote('"We swapped our entire design language in an afternoon. Nothing broke. That’s the line, and we crossed it."', 'Caleb Renner', 'Engineering Lead · Northwind')}
|
||||
</div>
|
||||
</div>
|
||||
</section>
|
||||
|
||||
<section class="section" id="faq" style="padding-top: 24px;">
|
||||
<div class="container">
|
||||
<div class="section-eyebrow">FAQ</div>
|
||||
<h2 class="section-title">Questions, answered.</h2>
|
||||
<div class="faq">
|
||||
${faq('Is this a Figma library, a code library, or both?', 'Both. Tokens flow from one source of truth into Figma styles and into the codegen pipeline at the same time.')}
|
||||
${faq('Can we ship our own brand theme?', 'Yes — fork the token file, change the palette and type stack, and every component reskins automatically.')}
|
||||
${faq('What about accessibility?', 'Color contrast meets WCAG AA on every surface. Components ship with focus rings, ARIA roles, and keyboard handling.')}
|
||||
${faq('How do you handle dark mode?', 'Every token has a paired dark value. The system flips at the document level — no per-component overrides needed.')}
|
||||
</div>
|
||||
</div>
|
||||
</section>
|
||||
|
||||
<section>
|
||||
<div class="container">
|
||||
<div class="cta">
|
||||
<div>
|
||||
<h2>Ship a product that finally feels finished.</h2>
|
||||
<p>Drop the system into your app today. The first project is on us.</p>
|
||||
</div>
|
||||
<div style="display: flex; gap: 12px; flex-wrap: wrap;">
|
||||
<a class="btn btn-primary" href="#">Start free trial</a>
|
||||
<a class="btn btn-secondary" href="#">Talk to sales</a>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</section>
|
||||
</main>
|
||||
|
||||
<footer>
|
||||
<div class="container">
|
||||
<div class="footer-row">
|
||||
<div class="footer-col">
|
||||
<div class="brand" style="margin-bottom: 12px;"><span class="brand-mark"></span>${escapeHtml(productName)}</div>
|
||||
<p style="margin: 0; max-width: 38ch;">${escapeHtml(tagline)}</p>
|
||||
</div>
|
||||
<div class="footer-col"><h6>Product</h6><a href="#">Features</a><a href="#">Pricing</a><a href="#">Changelog</a><a href="#">Roadmap</a></div>
|
||||
<div class="footer-col"><h6>Company</h6><a href="#">About</a><a href="#">Customers</a><a href="#">Careers</a><a href="#">Press</a></div>
|
||||
<div class="footer-col"><h6>Resources</h6><a href="#">Docs</a><a href="#">Status</a><a href="#">Brand</a><a href="#">Contact</a></div>
|
||||
</div>
|
||||
<div class="footer-bottom">
|
||||
<span>© ${new Date().getFullYear()} ${escapeHtml(productName)}. All rights reserved.</span>
|
||||
<span>Showcase rendered from <code style="font-family: var(--mono);">design-systems/${escapeHtml(id)}/DESIGN.md</code></span>
|
||||
</div>
|
||||
</div>
|
||||
</footer>
|
||||
</body>
|
||||
</html>`;
|
||||
}
|
||||
|
||||
function featureCard(icon: string, title: string, body: string): string {
|
||||
return `<div class="feature">
|
||||
<div class="feature-icon">${escapeHtml(icon)}</div>
|
||||
<h3>${escapeHtml(title)}</h3>
|
||||
<p>${escapeHtml(body)}</p>
|
||||
</div>`;
|
||||
}
|
||||
|
||||
function kpi(label: string, value: string, delta: string): string {
|
||||
return `<div class="kpi">
|
||||
<div class="label">${escapeHtml(label)}</div>
|
||||
<div class="value">${escapeHtml(value)}</div>
|
||||
<div class="delta">${escapeHtml(delta)}</div>
|
||||
</div>`;
|
||||
}
|
||||
|
||||
function listRow(name: string, meta: string, value: string, status: RowStatus): string {
|
||||
const badge = status === 'up' ? '<span class="badge up">↑</span>' : '<span class="badge">·</span>';
|
||||
return `<div class="list-row">
|
||||
<div>
|
||||
<div class="name">${escapeHtml(name)}</div>
|
||||
<div class="meta">${escapeHtml(meta)}</div>
|
||||
</div>
|
||||
<div class="meta">${escapeHtml(value)}</div>
|
||||
${badge}
|
||||
</div>`;
|
||||
}
|
||||
|
||||
function activityRow(name: string, meta: string): string {
|
||||
return `<div class="list-row">
|
||||
<div>
|
||||
<div class="name">${escapeHtml(name)}</div>
|
||||
<div class="meta">${escapeHtml(meta)}</div>
|
||||
</div>
|
||||
<div></div>
|
||||
<span class="badge">●</span>
|
||||
</div>`;
|
||||
}
|
||||
|
||||
function priceCard(name: string, price: string, sub: string, features: string[], featured = false): string {
|
||||
return `<div class="price-card${featured ? ' featured' : ''}">
|
||||
<div class="tier-name">${escapeHtml(name)}</div>
|
||||
<div class="price">${escapeHtml(price)} <small>${escapeHtml(sub)}</small></div>
|
||||
<ul>${features.map((f) => `<li>${escapeHtml(f)}</li>`).join('')}</ul>
|
||||
<a class="btn ${featured ? 'btn-primary' : 'btn-ghost'}" href="#" style="${featured ? 'background: var(--accent); color: var(--accent-fg); border-color: var(--accent);' : ''}">Choose ${escapeHtml(name)}</a>
|
||||
</div>`;
|
||||
}
|
||||
|
||||
function quote(text: string, name: string, role: string): string {
|
||||
return `<div class="quote">
|
||||
<p>${escapeHtml(text)}</p>
|
||||
<div class="quote-author">
|
||||
<div class="avatar"></div>
|
||||
<div>
|
||||
<div class="name">${escapeHtml(name)}</div>
|
||||
<div class="role">${escapeHtml(role)}</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>`;
|
||||
}
|
||||
|
||||
function faq(q: string, a: string): string {
|
||||
return `<div class="faq-item">
|
||||
<h4>${escapeHtml(q)}</h4>
|
||||
<p>${escapeHtml(a)}</p>
|
||||
</div>`;
|
||||
}
|
||||
|
||||
function inlineLineChart(): string {
|
||||
// Deterministic numbers so the chart looks specific (12 weekly data points).
|
||||
const data = [38, 44, 41, 52, 49, 61, 58, 67, 71, 76, 82, 88];
|
||||
const max = Math.max(...data);
|
||||
const min = Math.min(...data);
|
||||
const w = 720;
|
||||
const h = 160;
|
||||
const padX = 8;
|
||||
const padY = 14;
|
||||
const stepX = (w - padX * 2) / (data.length - 1);
|
||||
const norm = (v: number) => padY + (h - padY * 2) * (1 - (v - min) / (max - min));
|
||||
const points = data.map((v, i) => `${padX + i * stepX},${norm(v).toFixed(1)}`).join(' ');
|
||||
const area = `${padX},${h} ${points} ${w - padX},${h}`;
|
||||
return `<svg viewBox="0 0 ${w} ${h}" preserveAspectRatio="none">
|
||||
<defs>
|
||||
<linearGradient id="lg" x1="0" x2="0" y1="0" y2="1">
|
||||
<stop offset="0%" stop-color="var(--accent)" stop-opacity="0.32"/>
|
||||
<stop offset="100%" stop-color="var(--accent)" stop-opacity="0"/>
|
||||
</linearGradient>
|
||||
</defs>
|
||||
<polygon points="${area}" fill="url(#lg)"/>
|
||||
<polyline points="${points}" fill="none" stroke="var(--accent)" stroke-width="2.5" stroke-linejoin="round" stroke-linecap="round"/>
|
||||
${data.map((v, i) => `<circle cx="${padX + i * stepX}" cy="${norm(v).toFixed(1)}" r="${i === data.length - 1 ? 4 : 0}" fill="var(--accent)"/>`).join('')}
|
||||
</svg>`;
|
||||
}
|
||||
|
||||
function extractSubtitle(raw: string): string {
|
||||
const lines = raw.split(/\r?\n/);
|
||||
const h1 = lines.findIndex((l) => /^#\s+/.test(l));
|
||||
if (h1 === -1) return '';
|
||||
const after = lines.slice(h1 + 1);
|
||||
const nextHeading = after.findIndex((l) => /^#{1,6}\s+/.test(l));
|
||||
const window = (nextHeading === -1 ? after : after.slice(0, nextHeading))
|
||||
.join('\n')
|
||||
.replace(/^>\s*Category:.*$/gim, '')
|
||||
.replace(/^>\s*/gm, '')
|
||||
.trim();
|
||||
return window.split(/\n\n/)[0]?.slice(0, 240) ?? '';
|
||||
}
|
||||
|
||||
export function extractColors(raw: string): ColorToken[] {
|
||||
const colors: ColorToken[] = [];
|
||||
const seen = new Set<string>();
|
||||
function push(name: string, value: string, role: string): void {
|
||||
const cleanName = String(name).replace(/[*_`]+/g, '').replace(/\s+/g, ' ').trim();
|
||||
if (!cleanName || cleanName.length > 60) return;
|
||||
const v = normalizeHex(value);
|
||||
const key = `${cleanName.toLowerCase()}|${v}`;
|
||||
const cleanRole = String(role || '')
|
||||
.replace(/[`*_]+/g, '')
|
||||
.replace(/\s+/g, ' ')
|
||||
.trim()
|
||||
.replace(/[.;]+$/, '');
|
||||
if (seen.has(key)) {
|
||||
// Already recorded — but if this occurrence carries a richer role
|
||||
// description, upgrade the stored entry so role-based lookups don't
|
||||
// fall back to the bare name.
|
||||
if (cleanRole) {
|
||||
const existing = colors.find(
|
||||
(c) => c.name.toLowerCase() === cleanName.toLowerCase() && c.value === v,
|
||||
);
|
||||
if (existing && (!existing.role || cleanRole.length > existing.role.length)) {
|
||||
existing.role = cleanRole;
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
seen.add(key);
|
||||
colors.push({ name: cleanName, value: v, role: cleanRole });
|
||||
}
|
||||
|
||||
// Process the file line-by-line so multi-hex entries like Linear's
|
||||
// `**Marketing Black** (\`#010102\` / \`#08090a\`): role` don't confuse a
|
||||
// single global regex. We extract three pieces from each candidate line:
|
||||
// - the bold (or list-prefixed) name
|
||||
// - the FIRST hex on the line
|
||||
// - everything after the first `:` that follows the hex (the role)
|
||||
for (const rawLine of raw.split(/\r?\n/)) {
|
||||
const line = rawLine.trim();
|
||||
if (!line) continue;
|
||||
|
||||
// Pattern A: **Name** … #hex … : role description
|
||||
const bold = /\*\*([A-Za-z][A-Za-z0-9 /&()+_'’-]{1,40}?)\*\*([^\n]+)/.exec(line);
|
||||
if (bold) {
|
||||
const rest = bold[2] ?? '';
|
||||
const hex = /#[0-9a-fA-F]{3,8}\b/.exec(rest);
|
||||
if (hex) {
|
||||
const after = rest.slice((hex.index ?? 0) + hex[0].length);
|
||||
const colonIdx = after.search(/[::]/);
|
||||
const role = colonIdx >= 0 ? after.slice(colonIdx + 1).trim() : '';
|
||||
push(bold[1] ?? '', hex[0], role);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
// Pattern B: list-prefixed spec lines like
|
||||
// "- Background: `#7d2ae8`" inside a ### Buttons block.
|
||||
// Also handles the `- **Name:** \`#hex\`` shape (colon inside the bold
|
||||
// wrapper) used by agentic/warm-editorial: the optional `\*{0,2}` slots
|
||||
// before the name and after the colon let us absorb the surrounding
|
||||
// `**` markers without needing a third pattern.
|
||||
// Use the name itself as the role so lookups can still see "Background"
|
||||
// and "Text" labels.
|
||||
const spec = /^[\s>*-]*\*{0,2}([A-Za-z][^:*\n]{1,40}?)\*{0,2}\s*[::]\s*\*{0,2}\s*`?(#[0-9a-fA-F]{3,8})/.exec(line);
|
||||
if (spec) {
|
||||
push(spec[1] ?? '', spec[2] ?? '', spec[1] ?? '');
|
||||
}
|
||||
}
|
||||
|
||||
return colors;
|
||||
}
|
||||
|
||||
function extractFonts(raw: string): FontHints {
|
||||
const out: FontHints = {};
|
||||
const re = /^[\s>*-]*\**\s*([A-Za-z][A-Za-z /]{1,30}?)\s*\**\s*[::]\s*`?([^`\n]+?)`?$/gm;
|
||||
let m;
|
||||
while ((m = re.exec(raw)) !== null) {
|
||||
const label = (m[1] ?? '').toLowerCase();
|
||||
const value = (m[2] ?? '').trim().replace(/[*_`]+$/g, '').trim();
|
||||
if (!/[a-zA-Z]/.test(value)) continue;
|
||||
if (value.startsWith('#')) continue;
|
||||
if (/display|heading|h1|title/.test(label) && !out.display) out.display = value;
|
||||
else if (/body|text|paragraph|copy/.test(label) && !out.body) out.body = value;
|
||||
else if (/mono|code/.test(label) && !out.mono) out.mono = value;
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
function escapeRegex(s: string): string {
|
||||
return String(s).replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
||||
}
|
||||
|
||||
// Match a hint as a whole word inside `text` (case-insensitive). We use word
|
||||
// boundaries so descriptive color names like "Cardinal Red" don't satisfy a
|
||||
// "card" hint, and "Gem Pink" doesn't satisfy "ink" — both real bugs the
|
||||
// substring-based version produced for the Duolingo and Canva showcases.
|
||||
function matchesHint(text: string, hint: string): boolean {
|
||||
if (!text) return false;
|
||||
const needle = hint.toLowerCase().trim();
|
||||
if (!needle) return false;
|
||||
const re = new RegExp(`\\b${escapeRegex(needle)}\\b`, 'i');
|
||||
return re.test(text);
|
||||
}
|
||||
|
||||
function pickColor(colors: ColorToken[], hints: string[], exclude: string[] = []): string | null {
|
||||
// Two-pass lookup: each hint is first checked against every color's role
|
||||
// description (the prose authors use to explain how the color is used)
|
||||
// and only then against the bare name. This ensures a `**Snow** … Primary
|
||||
// background.` line is recognised as the page background even though the
|
||||
// name "Snow" doesn't contain the word "background".
|
||||
// `exclude` skips colors whose hex equals an already-chosen role (e.g.
|
||||
// pass `[bg]` when picking `fg`) so two roles can't collapse to the same
|
||||
// hex and erase contrast.
|
||||
const blocked = new Set(
|
||||
exclude
|
||||
.map((v) => (v == null ? '' : String(v).toLowerCase()))
|
||||
.filter((v) => v.length > 0),
|
||||
);
|
||||
const isAllowed = (c: ColorToken) => !blocked.has(c.value.toLowerCase());
|
||||
for (const hint of hints) {
|
||||
const byRole = colors.find((c) => isAllowed(c) && matchesHint(c.role, hint));
|
||||
if (byRole) return byRole.value;
|
||||
const byName = colors.find((c) => isAllowed(c) && matchesHint(c.name, hint));
|
||||
if (byName) return byName.value;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
function colorSaturation(hex: string): number {
|
||||
const v = String(hex).replace('#', '').toLowerCase();
|
||||
if (v.length !== 6) return 0;
|
||||
const r = parseInt(v.slice(0, 2), 16);
|
||||
const g = parseInt(v.slice(2, 4), 16);
|
||||
const b = parseInt(v.slice(4, 6), 16);
|
||||
const max = Math.max(r, g, b);
|
||||
const min = Math.min(r, g, b);
|
||||
return max === 0 ? 0 : (max - min) / max;
|
||||
}
|
||||
|
||||
function colorLuminance(hex: string): number {
|
||||
const v = String(hex).replace('#', '').toLowerCase();
|
||||
if (v.length !== 6) return 0.5;
|
||||
const r = parseInt(v.slice(0, 2), 16);
|
||||
const g = parseInt(v.slice(2, 4), 16);
|
||||
const b = parseInt(v.slice(4, 6), 16);
|
||||
return (0.299 * r + 0.587 * g + 0.114 * b) / 255;
|
||||
}
|
||||
|
||||
function firstLightish(colors: ColorToken[]): string | null {
|
||||
for (const c of colors) {
|
||||
if (colorSaturation(c.value) > 0.15) continue;
|
||||
if (colorLuminance(c.value) >= 0.92) return c.value;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
function firstNonNeutral(colors: ColorToken[], exclude: string[] = []): string | null {
|
||||
const set = new Set(exclude.map((v) => String(v || '').toLowerCase()));
|
||||
for (const c of colors) {
|
||||
if (set.has(c.value.toLowerCase())) continue;
|
||||
if (colorSaturation(c.value) > 0.25) return c.value;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
function secondNonNeutral(colors: ColorToken[], exclude: string[] = []): string | null {
|
||||
const set = new Set(exclude.map((v) => String(v || '').toLowerCase()));
|
||||
for (const c of colors) {
|
||||
if (set.has(c.value.toLowerCase())) continue;
|
||||
if (colorSaturation(c.value) > 0.25) return c.value;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
function pickReadableForeground(hex: string): string {
|
||||
const n = normalizeHex(hex);
|
||||
if (n.length !== 7) return '#ffffff';
|
||||
const r = parseInt(n.slice(1, 3), 16);
|
||||
const g = parseInt(n.slice(3, 5), 16);
|
||||
const b = parseInt(n.slice(5, 7), 16);
|
||||
const lum = (0.299 * r + 0.587 * g + 0.114 * b) / 255;
|
||||
return lum > 0.6 ? '#0a0a0a' : '#ffffff';
|
||||
}
|
||||
|
||||
function mixSurface(bg: string): string {
|
||||
const n = normalizeHex(bg);
|
||||
if (n.length !== 7) return '#fafafa';
|
||||
const r = parseInt(n.slice(1, 3), 16);
|
||||
const g = parseInt(n.slice(3, 5), 16);
|
||||
const b = parseInt(n.slice(5, 7), 16);
|
||||
const lum = (0.299 * r + 0.587 * g + 0.114 * b) / 255;
|
||||
// Lift dark backgrounds; tint light backgrounds slightly cooler.
|
||||
const adjust = lum < 0.4 ? 16 : -8;
|
||||
const fix = (v: number) => Math.max(0, Math.min(255, v + adjust)).toString(16).padStart(2, '0');
|
||||
return `#${fix(r)}${fix(g)}${fix(b)}`;
|
||||
}
|
||||
|
||||
function normalizeHex(hex: string): string {
|
||||
let h = hex.toLowerCase();
|
||||
if (h.length === 4) {
|
||||
h = '#' + h.slice(1).split('').map((c) => c + c).join('');
|
||||
}
|
||||
return h;
|
||||
}
|
||||
|
||||
function cleanTitle(raw: string): string {
|
||||
return String(raw).replace(/^Design System (Inspired by|for)\s+/i, '').trim();
|
||||
}
|
||||
|
||||
function oneLine(s: string): string {
|
||||
return String(s).replace(/\s+/g, ' ').trim();
|
||||
}
|
||||
|
||||
function escapeHtml(s: string): string {
|
||||
return String(s).replace(/[&<>"']/g, (c) =>
|
||||
c === '&' ? '&' : c === '<' ? '<' : c === '>' ? '>' : c === '"' ? '"' : ''',
|
||||
);
|
||||
}
|
||||
@@ -1,12 +0,0 @@
|
||||
import type { Config } from "tailwindcss";
|
||||
|
||||
const config: Config = {
|
||||
content: [
|
||||
"./app/**/*.{js,ts,jsx,tsx,mdx}",
|
||||
],
|
||||
theme: {
|
||||
extend: {},
|
||||
},
|
||||
plugins: [],
|
||||
};
|
||||
export default config;
|
||||
@@ -1,12 +0,0 @@
|
||||
import type { Config } from "tailwindcss";
|
||||
|
||||
const config: Config = {
|
||||
content: [
|
||||
"./app/**/*.{js,ts,jsx,tsx,mdx}",
|
||||
],
|
||||
theme: {
|
||||
extend: {},
|
||||
},
|
||||
plugins: [],
|
||||
};
|
||||
export default config;
|
||||
@@ -1,228 +0,0 @@
|
||||
/**
|
||||
* Backend Extraction Module
|
||||
*
|
||||
* Runs extraction as a pure backend job, not in chat.
|
||||
* Called when Collector phase completes.
|
||||
*/
|
||||
|
||||
import { getAdminDb } from '@/lib/firebase/admin';
|
||||
import { GeminiLlmClient } from '@/lib/ai/gemini-client';
|
||||
import { BACKEND_EXTRACTOR_SYSTEM_PROMPT } from '@/lib/ai/prompts/extractor';
|
||||
import { writeKnowledgeChunksForItem } from '@/lib/server/vector-memory';
|
||||
import type { ExtractionOutput, ExtractedInsight } from '@/lib/types/extraction-output';
|
||||
import type { PhaseHandoff } from '@/lib/types/phase-handoff';
|
||||
import { z } from 'zod';
|
||||
|
||||
const ExtractionOutputSchema = z.object({
|
||||
insights: z.array(z.object({
|
||||
id: z.string(),
|
||||
type: z.enum(["problem", "user", "feature", "constraint", "opportunity", "other"]),
|
||||
title: z.string(),
|
||||
description: z.string(),
|
||||
sourceText: z.string(),
|
||||
sourceKnowledgeItemId: z.string(),
|
||||
importance: z.enum(["primary", "supporting"]),
|
||||
confidence: z.number().min(0).max(1),
|
||||
})),
|
||||
problems: z.array(z.string()),
|
||||
targetUsers: z.array(z.string()),
|
||||
features: z.array(z.string()),
|
||||
constraints: z.array(z.string()),
|
||||
opportunities: z.array(z.string()),
|
||||
uncertainties: z.array(z.string()),
|
||||
missingInformation: z.array(z.string()),
|
||||
overallConfidence: z.number().min(0).max(1),
|
||||
});
|
||||
|
||||
export async function runBackendExtractionForProject(projectId: string): Promise<void> {
|
||||
console.log(`[Backend Extractor] Starting extraction for project ${projectId}`);
|
||||
|
||||
const adminDb = getAdminDb();
|
||||
|
||||
try {
|
||||
// 1. Load project
|
||||
const projectDoc = await adminDb.collection('projects').doc(projectId).get();
|
||||
if (!projectDoc.exists) {
|
||||
throw new Error(`Project ${projectId} not found`);
|
||||
}
|
||||
|
||||
const projectData = projectDoc.data();
|
||||
|
||||
// 2. Load knowledge items
|
||||
const knowledgeSnapshot = await adminDb
|
||||
.collection('knowledge_items')
|
||||
.where('projectId', '==', projectId)
|
||||
.where('sourceType', '==', 'imported_document')
|
||||
.get();
|
||||
|
||||
if (knowledgeSnapshot.empty) {
|
||||
console.log(`[Backend Extractor] No documents to extract for project ${projectId} - creating empty handoff`);
|
||||
|
||||
// Create a minimal extraction handoff even with no documents
|
||||
const emptyHandoff: PhaseHandoff = {
|
||||
phase: 'extraction',
|
||||
readyForNextPhase: false, // Not ready - no materials to extract from
|
||||
confidence: 0,
|
||||
confirmed: {
|
||||
problems: [],
|
||||
targetUsers: [],
|
||||
features: [],
|
||||
constraints: [],
|
||||
opportunities: [],
|
||||
},
|
||||
uncertain: {},
|
||||
missing: ['No documents uploaded - need product requirements, specs, or notes'],
|
||||
questionsForUser: [
|
||||
'You haven\'t uploaded any documents yet. Do you have any product specs, requirements, or notes to share?',
|
||||
],
|
||||
sourceEvidence: [],
|
||||
version: 'extraction_v1',
|
||||
timestamp: new Date().toISOString(),
|
||||
};
|
||||
|
||||
await adminDb.collection('projects').doc(projectId).update({
|
||||
'phaseData.phaseHandoffs.extraction': emptyHandoff,
|
||||
currentPhase: 'extraction_review',
|
||||
phaseStatus: 'in_progress',
|
||||
'phaseData.extractionCompletedAt': new Date().toISOString(),
|
||||
updatedAt: new Date().toISOString(),
|
||||
});
|
||||
|
||||
console.log(`[Backend Extractor] Set phase to extraction_review with empty handoff`);
|
||||
return;
|
||||
}
|
||||
|
||||
console.log(`[Backend Extractor] Found ${knowledgeSnapshot.size} documents to process`);
|
||||
|
||||
const llm = new GeminiLlmClient();
|
||||
const allExtractionOutputs: ExtractionOutput[] = [];
|
||||
const processedKnowledgeItemIds: string[] = [];
|
||||
|
||||
// 3. Process each document
|
||||
for (const knowledgeDoc of knowledgeSnapshot.docs) {
|
||||
const knowledgeData = knowledgeDoc.data();
|
||||
const knowledgeItemId = knowledgeDoc.id;
|
||||
|
||||
try {
|
||||
console.log(`[Backend Extractor] Processing document: ${knowledgeData.title || knowledgeItemId}`);
|
||||
|
||||
// Call LLM with structured extraction + thinking mode
|
||||
const extraction = await llm.structuredCall<ExtractionOutput>({
|
||||
model: 'gemini',
|
||||
systemPrompt: BACKEND_EXTRACTOR_SYSTEM_PROMPT,
|
||||
messages: [{
|
||||
role: 'user',
|
||||
content: `Document Title: ${knowledgeData.title || 'Untitled'}\nSource Type: ${knowledgeData.sourceType}\n\nContent:\n${knowledgeData.content}`,
|
||||
}],
|
||||
schema: ExtractionOutputSchema as any,
|
||||
temperature: 1.0, // Gemini 3 default (changed from 0.3)
|
||||
thinking_config: {
|
||||
thinking_level: 'high', // Enable deep reasoning for document analysis
|
||||
include_thoughts: false, // Don't include thought tokens in output (saves cost)
|
||||
},
|
||||
});
|
||||
|
||||
// Add knowledgeItemId to each insight
|
||||
extraction.insights.forEach(insight => {
|
||||
insight.sourceKnowledgeItemId = knowledgeItemId;
|
||||
});
|
||||
|
||||
allExtractionOutputs.push(extraction);
|
||||
processedKnowledgeItemIds.push(knowledgeItemId);
|
||||
|
||||
// 4. Persist extraction to chat_extractions
|
||||
await adminDb.collection('chat_extractions').add({
|
||||
projectId,
|
||||
knowledgeItemId,
|
||||
data: extraction,
|
||||
overallConfidence: extraction.overallConfidence,
|
||||
overallCompletion: extraction.overallConfidence > 0.7 ? 0.9 : 0.6,
|
||||
createdAt: new Date().toISOString(),
|
||||
updatedAt: new Date().toISOString(),
|
||||
});
|
||||
|
||||
console.log(`[Backend Extractor] Extracted ${extraction.insights.length} insights from ${knowledgeData.title || knowledgeItemId}`);
|
||||
|
||||
// 5. Write vector chunks for primary insights
|
||||
const primaryInsights = extraction.insights.filter(i => i.importance === 'primary');
|
||||
for (const insight of primaryInsights) {
|
||||
try {
|
||||
// Create a knowledge chunk for this insight
|
||||
await writeKnowledgeChunksForItem({
|
||||
id: knowledgeItemId,
|
||||
projectId,
|
||||
content: `${insight.title}\n\n${insight.description}\n\nSource: ${insight.sourceText}`,
|
||||
sourceMeta: {
|
||||
sourceType: 'extracted_insight',
|
||||
importance: 'primary',
|
||||
},
|
||||
});
|
||||
} catch (chunkError) {
|
||||
console.error(`[Backend Extractor] Failed to write chunk for insight ${insight.id}:`, chunkError);
|
||||
// Continue processing other insights
|
||||
}
|
||||
}
|
||||
|
||||
} catch (docError) {
|
||||
console.error(`[Backend Extractor] Failed to process document ${knowledgeItemId}:`, docError);
|
||||
// Continue with next document
|
||||
}
|
||||
}
|
||||
|
||||
// 6. Build extraction PhaseHandoff
|
||||
// Flatten all extracted items (they're already strings, not objects)
|
||||
const allProblems = [...new Set(allExtractionOutputs.flatMap(e => e.problems))];
|
||||
const allUsers = [...new Set(allExtractionOutputs.flatMap(e => e.targetUsers))];
|
||||
const allFeatures = [...new Set(allExtractionOutputs.flatMap(e => e.features))];
|
||||
const allConstraints = [...new Set(allExtractionOutputs.flatMap(e => e.constraints))];
|
||||
const allOpportunities = [...new Set(allExtractionOutputs.flatMap(e => e.opportunities))];
|
||||
const allUncertainties = [...new Set(allExtractionOutputs.flatMap(e => e.uncertainties))];
|
||||
const allMissing = [...new Set(allExtractionOutputs.flatMap(e => e.missingInformation))];
|
||||
|
||||
const avgConfidence = allExtractionOutputs.length > 0
|
||||
? allExtractionOutputs.reduce((sum, e) => sum + e.overallConfidence, 0) / allExtractionOutputs.length
|
||||
: 0;
|
||||
|
||||
const readyForNextPhase = allProblems.length > 0 && allFeatures.length > 0 && avgConfidence > 0.5;
|
||||
|
||||
const extractionHandoff: PhaseHandoff = {
|
||||
phase: 'extraction',
|
||||
readyForNextPhase,
|
||||
confidence: avgConfidence,
|
||||
confirmed: {
|
||||
problems: allProblems,
|
||||
targetUsers: allUsers,
|
||||
features: allFeatures,
|
||||
constraints: allConstraints,
|
||||
opportunities: allOpportunities,
|
||||
},
|
||||
uncertain: {},
|
||||
missing: allMissing,
|
||||
questionsForUser: allUncertainties,
|
||||
sourceEvidence: processedKnowledgeItemIds,
|
||||
version: 'extraction_v1',
|
||||
timestamp: new Date().toISOString(),
|
||||
};
|
||||
|
||||
// 7. Persist handoff and update phase
|
||||
await adminDb.collection('projects').doc(projectId).update({
|
||||
'phaseData.phaseHandoffs.extraction': extractionHandoff,
|
||||
currentPhase: 'extraction_review',
|
||||
phaseStatus: 'in_progress',
|
||||
'phaseData.extractionCompletedAt': new Date().toISOString(),
|
||||
updatedAt: new Date().toISOString(),
|
||||
});
|
||||
|
||||
console.log(`[Backend Extractor] ✅ Extraction complete for project ${projectId}`);
|
||||
console.log(`[Backend Extractor] - Problems: ${allProblems.length}`);
|
||||
console.log(`[Backend Extractor] - Users: ${allUsers.length}`);
|
||||
console.log(`[Backend Extractor] - Features: ${allFeatures.length}`);
|
||||
console.log(`[Backend Extractor] - Confidence: ${(avgConfidence * 100).toFixed(1)}%`);
|
||||
console.log(`[Backend Extractor] - Ready for next phase: ${readyForNextPhase}`);
|
||||
|
||||
} catch (error) {
|
||||
console.error(`[Backend Extractor] Fatal error during extraction:`, error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,64 +0,0 @@
|
||||
import { getAdminDb } from '@/lib/firebase/admin';
|
||||
import { FieldValue } from 'firebase-admin/firestore';
|
||||
import type { ChatExtractionRecord } from '@/lib/types/chat-extraction';
|
||||
|
||||
const COLLECTION = 'chat_extractions';
|
||||
|
||||
interface CreateChatExtractionInput<TData> {
|
||||
projectId: string;
|
||||
knowledgeItemId: string;
|
||||
data: TData;
|
||||
overallCompletion: number;
|
||||
overallConfidence: number;
|
||||
}
|
||||
|
||||
export async function createChatExtraction<TData>(
|
||||
input: CreateChatExtractionInput<TData>,
|
||||
): Promise<ChatExtractionRecord<TData>> {
|
||||
const adminDb = getAdminDb();
|
||||
const docRef = adminDb.collection(COLLECTION).doc();
|
||||
|
||||
const payload = {
|
||||
id: docRef.id,
|
||||
projectId: input.projectId,
|
||||
knowledgeItemId: input.knowledgeItemId,
|
||||
data: input.data,
|
||||
overallCompletion: input.overallCompletion,
|
||||
overallConfidence: input.overallConfidence,
|
||||
createdAt: FieldValue.serverTimestamp(),
|
||||
updatedAt: FieldValue.serverTimestamp(),
|
||||
};
|
||||
|
||||
await docRef.set(payload);
|
||||
const snapshot = await docRef.get();
|
||||
return snapshot.data() as ChatExtractionRecord<TData>;
|
||||
}
|
||||
|
||||
export async function listChatExtractions<TData>(
|
||||
projectId: string,
|
||||
): Promise<ChatExtractionRecord<TData>[]> {
|
||||
const adminDb = getAdminDb();
|
||||
const querySnapshot = await adminDb
|
||||
.collection(COLLECTION)
|
||||
.where('projectId', '==', projectId)
|
||||
.orderBy('createdAt', 'desc')
|
||||
.get();
|
||||
|
||||
return querySnapshot.docs.map(
|
||||
(doc) => doc.data() as ChatExtractionRecord<TData>,
|
||||
);
|
||||
}
|
||||
|
||||
export async function getChatExtraction<TData>(
|
||||
extractionId: string,
|
||||
): Promise<ChatExtractionRecord<TData> | null> {
|
||||
const adminDb = getAdminDb();
|
||||
const docRef = adminDb.collection(COLLECTION).doc(extractionId);
|
||||
const snapshot = await docRef.get();
|
||||
if (!snapshot.exists) {
|
||||
return null;
|
||||
}
|
||||
return snapshot.data() as ChatExtractionRecord<TData>;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,91 +0,0 @@
|
||||
/**
|
||||
* Chat Mode Resolution Logic
|
||||
*
|
||||
* Determines which chat mode (collector, extraction_review, vision, mvp, marketing, general)
|
||||
* should be active based on project state stored in Postgres.
|
||||
*/
|
||||
|
||||
import { query } from '@/lib/db-postgres';
|
||||
import type { ChatMode } from '@/lib/ai/chat-modes';
|
||||
|
||||
/**
|
||||
* Resolve the appropriate chat mode for a project using Postgres (fs_projects).
|
||||
*/
|
||||
export async function resolveChatMode(projectId: string): Promise<ChatMode> {
|
||||
try {
|
||||
const rows = await query<{ data: any }>(
|
||||
`SELECT data FROM fs_projects WHERE id = $1 LIMIT 1`,
|
||||
[projectId]
|
||||
);
|
||||
|
||||
if (rows.length === 0) {
|
||||
console.warn(`[Chat Mode Resolver] Project ${projectId} not found`);
|
||||
return 'collector_mode';
|
||||
}
|
||||
|
||||
const projectData = rows[0].data ?? {};
|
||||
const phaseData = (projectData.phaseData ?? {}) as Record<string, any>;
|
||||
const currentPhase: string = projectData.currentPhase ?? 'collector';
|
||||
|
||||
// Explicit phase overrides
|
||||
if (currentPhase === 'extraction_review' || currentPhase === 'analyzed') return 'extraction_review_mode';
|
||||
if (currentPhase === 'vision') return 'vision_mode';
|
||||
if (currentPhase === 'mvp') return 'mvp_mode';
|
||||
if (currentPhase === 'marketing') return 'marketing_mode';
|
||||
|
||||
// Derive from phase artifacts
|
||||
if (!phaseData.canonicalProductModel) return 'collector_mode';
|
||||
if (!phaseData.mvpPlan) return 'vision_mode';
|
||||
if (!phaseData.marketingPlan) return 'mvp_mode';
|
||||
if (phaseData.marketingPlan) return 'marketing_mode';
|
||||
|
||||
return 'general_chat_mode';
|
||||
} catch (error) {
|
||||
console.error('[Chat Mode Resolver] Failed to resolve mode:', error);
|
||||
return 'collector_mode';
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Summarise knowledge items for context building.
|
||||
* Uses Postgres fs_knowledge_items if available, otherwise returns empty.
|
||||
*/
|
||||
export async function summarizeKnowledgeItems(projectId: string): Promise<{
|
||||
totalCount: number;
|
||||
bySourceType: Record<string, number>;
|
||||
recentTitles: string[];
|
||||
}> {
|
||||
try {
|
||||
const rows = await query<{ data: any }>(
|
||||
`SELECT data FROM fs_knowledge_items WHERE project_id = $1 ORDER BY created_at DESC LIMIT 20`,
|
||||
[projectId]
|
||||
);
|
||||
|
||||
const bySourceType: Record<string, number> = {};
|
||||
const recentTitles: string[] = [];
|
||||
|
||||
for (const row of rows) {
|
||||
const d = row.data ?? {};
|
||||
const sourceType = d.sourceType ?? 'unknown';
|
||||
bySourceType[sourceType] = (bySourceType[sourceType] ?? 0) + 1;
|
||||
if (d.title && recentTitles.length < 5) recentTitles.push(d.title);
|
||||
}
|
||||
|
||||
return { totalCount: rows.length, bySourceType, recentTitles };
|
||||
} catch {
|
||||
// Table may not exist for older deployments — return empty
|
||||
return { totalCount: 0, bySourceType: {}, recentTitles: [] };
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Summarise extractions for context building.
|
||||
* Returns empty defaults — extractions not yet migrated to Postgres.
|
||||
*/
|
||||
export async function summarizeExtractions(projectId: string): Promise<{
|
||||
totalCount: number;
|
||||
avgConfidence: number;
|
||||
avgCompletion: number;
|
||||
}> {
|
||||
return { totalCount: 0, avgConfidence: 0, avgCompletion: 0 };
|
||||
}
|
||||
@@ -1,140 +0,0 @@
|
||||
/**
|
||||
* Persistent dev-server configuration store.
|
||||
* Closes BETA_LAUNCH_PLAN P6.B1.
|
||||
*
|
||||
* When `dev_server_start` succeeds, the MCP tool should call
|
||||
* `upsertDevServerConfig` so the project page can auto-resume the
|
||||
* server on next mount without requiring the user to re-type the
|
||||
* command (see P6.B2 for the auto-resume hook).
|
||||
*
|
||||
* Schema:
|
||||
* fs_project_dev_servers
|
||||
* project_id UUID PK → fs_projects.id
|
||||
* command TEXT NOT NULL e.g. "cd myapp && npm run dev"
|
||||
* port INT NOT NULL e.g. 3000
|
||||
* framework TEXT e.g. "nextjs", "vite", "express"
|
||||
* preview_url TEXT last known *.preview.vibnai.com URL
|
||||
* last_started_at TIMESTAMPTZ
|
||||
* status TEXT CHECK IN ('running','stopped','crashed')
|
||||
* updated_at TIMESTAMPTZ DEFAULT NOW()
|
||||
*/
|
||||
|
||||
import { query } from "@/lib/db-postgres";
|
||||
import { log } from "@/lib/server/logger";
|
||||
|
||||
let tableReady = false;
|
||||
async function ensureTable() {
|
||||
if (tableReady) return;
|
||||
await query(`
|
||||
CREATE TABLE IF NOT EXISTS fs_project_dev_servers (
|
||||
project_id TEXT PRIMARY KEY,
|
||||
command TEXT NOT NULL,
|
||||
port INT NOT NULL,
|
||||
framework TEXT,
|
||||
preview_url TEXT,
|
||||
last_started_at TIMESTAMPTZ,
|
||||
status TEXT NOT NULL DEFAULT 'stopped'
|
||||
CHECK (status IN ('running', 'stopped', 'crashed')),
|
||||
updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
|
||||
)
|
||||
`);
|
||||
tableReady = true;
|
||||
}
|
||||
|
||||
export interface DevServerConfig {
|
||||
projectId: string;
|
||||
command: string;
|
||||
port: number;
|
||||
framework?: string;
|
||||
previewUrl?: string;
|
||||
status: "running" | "stopped" | "crashed";
|
||||
}
|
||||
|
||||
/** Called by the MCP dev_server_start handler after a successful start. */
|
||||
export async function upsertDevServerConfig(
|
||||
cfg: DevServerConfig,
|
||||
): Promise<void> {
|
||||
try {
|
||||
await ensureTable();
|
||||
await query(
|
||||
`INSERT INTO fs_project_dev_servers
|
||||
(project_id, command, port, framework, preview_url, last_started_at, status, updated_at)
|
||||
VALUES ($1, $2, $3, $4, $5, NOW(), $6, NOW())
|
||||
ON CONFLICT (project_id) DO UPDATE SET
|
||||
command = EXCLUDED.command,
|
||||
port = EXCLUDED.port,
|
||||
framework = COALESCE(EXCLUDED.framework, fs_project_dev_servers.framework),
|
||||
preview_url = COALESCE(EXCLUDED.preview_url, fs_project_dev_servers.preview_url),
|
||||
last_started_at = NOW(),
|
||||
status = EXCLUDED.status,
|
||||
updated_at = NOW()`,
|
||||
[
|
||||
cfg.projectId,
|
||||
cfg.command,
|
||||
cfg.port,
|
||||
cfg.framework ?? null,
|
||||
cfg.previewUrl ?? null,
|
||||
cfg.status,
|
||||
],
|
||||
);
|
||||
} catch (err) {
|
||||
log.warn("dev-server-state: upsert failed (non-fatal)", {
|
||||
projectId: cfg.projectId,
|
||||
err: err instanceof Error ? err.message : String(err),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/** Update just the status (e.g. on stop / crash). */
|
||||
export async function setDevServerStatus(
|
||||
projectId: string,
|
||||
status: "running" | "stopped" | "crashed",
|
||||
): Promise<void> {
|
||||
try {
|
||||
await ensureTable();
|
||||
await query(
|
||||
`UPDATE fs_project_dev_servers
|
||||
SET status = $2, updated_at = NOW()
|
||||
WHERE project_id = $1`,
|
||||
[projectId, status],
|
||||
);
|
||||
} catch (err) {
|
||||
log.warn("dev-server-state: status update failed (non-fatal)", {
|
||||
projectId,
|
||||
err: err instanceof Error ? err.message : String(err),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/** Returns the last-known dev server config for a project, or null. */
|
||||
export async function getDevServerConfig(
|
||||
projectId: string,
|
||||
): Promise<DevServerConfig | null> {
|
||||
try {
|
||||
await ensureTable();
|
||||
const rows = await query<{
|
||||
project_id: string;
|
||||
command: string;
|
||||
port: number;
|
||||
framework: string | null;
|
||||
preview_url: string | null;
|
||||
status: string;
|
||||
}>(
|
||||
`SELECT project_id, command, port, framework, preview_url, status
|
||||
FROM fs_project_dev_servers WHERE project_id = $1`,
|
||||
[projectId],
|
||||
);
|
||||
if (!rows[0]) return null;
|
||||
const r = rows[0];
|
||||
return {
|
||||
projectId: r.project_id,
|
||||
command: r.command,
|
||||
port: r.port,
|
||||
framework: r.framework ?? undefined,
|
||||
previewUrl: r.preview_url ?? undefined,
|
||||
status: r.status as "running" | "stopped" | "crashed",
|
||||
};
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -1,116 +0,0 @@
|
||||
/**
|
||||
* Coolify connectivity checks for ops / uptime monitors.
|
||||
*
|
||||
* - HTTP API (token) — provisioning via REST
|
||||
* - SSH → Docker on host — required for shell.exec / dev containers
|
||||
*/
|
||||
|
||||
import { listServers } from '@/lib/coolify';
|
||||
import { runOnCoolifyHost } from '@/lib/coolify-ssh';
|
||||
|
||||
export interface CoolifyInfraHealthReport {
|
||||
checkedAt: string;
|
||||
ssh: {
|
||||
configured: boolean;
|
||||
missingEnvVars: string[];
|
||||
reachable?: boolean;
|
||||
latencyMs?: number;
|
||||
dockerDaemonOk?: boolean;
|
||||
/** docker Server.Version when daemon responds */
|
||||
dockerVersion?: string;
|
||||
error?: string;
|
||||
};
|
||||
api: {
|
||||
configured: boolean;
|
||||
reachable?: boolean;
|
||||
latencyMs?: number;
|
||||
serverCount?: number;
|
||||
error?: string;
|
||||
};
|
||||
}
|
||||
|
||||
export function getCoolifySshConfigGap(): {
|
||||
configured: boolean;
|
||||
missingEnvVars: string[];
|
||||
} {
|
||||
const missing: string[] = [];
|
||||
if (!process.env.COOLIFY_SSH_HOST?.trim()) missing.push('COOLIFY_SSH_HOST');
|
||||
if (!process.env.COOLIFY_SSH_PRIVATE_KEY_B64?.trim()) {
|
||||
missing.push('COOLIFY_SSH_PRIVATE_KEY_B64');
|
||||
}
|
||||
return { configured: missing.length === 0, missingEnvVars: missing };
|
||||
}
|
||||
|
||||
/** True when SSH is wired and `docker` responds on the Coolify host. */
|
||||
export async function runCoolifyInfraHealthProbe(): Promise<CoolifyInfraHealthReport> {
|
||||
const checkedAt = new Date().toISOString();
|
||||
const sshGap = getCoolifySshConfigGap();
|
||||
|
||||
const report: CoolifyInfraHealthReport = {
|
||||
checkedAt,
|
||||
ssh: {
|
||||
configured: sshGap.configured,
|
||||
missingEnvVars: sshGap.missingEnvVars,
|
||||
},
|
||||
api: {
|
||||
configured: !!process.env.COOLIFY_API_TOKEN?.trim(),
|
||||
},
|
||||
};
|
||||
|
||||
if (report.api.configured) {
|
||||
const t0 = Date.now();
|
||||
try {
|
||||
const servers = await listServers();
|
||||
report.api.reachable = true;
|
||||
report.api.latencyMs = Date.now() - t0;
|
||||
report.api.serverCount = Array.isArray(servers) ? servers.length : 0;
|
||||
} catch (e) {
|
||||
report.api.reachable = false;
|
||||
report.api.latencyMs = Date.now() - t0;
|
||||
report.api.error = e instanceof Error ? e.message : String(e);
|
||||
}
|
||||
} else {
|
||||
report.api.error = 'COOLIFY_API_TOKEN is not set';
|
||||
}
|
||||
|
||||
if (!sshGap.configured) {
|
||||
report.ssh.error =
|
||||
`Missing: ${sshGap.missingEnvVars.join(', ')} — dev containers need SSH to the Docker host (see lib/coolify-ssh.ts).`;
|
||||
return report;
|
||||
}
|
||||
|
||||
const tSsh = Date.now();
|
||||
try {
|
||||
const res = await runOnCoolifyHost(`docker info --format '{{.ServerVersion}}'`, {
|
||||
timeoutMs: 15_000,
|
||||
maxBytes: 8192,
|
||||
});
|
||||
report.ssh.latencyMs = Date.now() - tSsh;
|
||||
report.ssh.reachable = true;
|
||||
const ver = res.stdout.trim().split(/\s+/)[0]?.trim() ?? '';
|
||||
if (res.code === 0 && ver.length > 0) {
|
||||
report.ssh.dockerDaemonOk = true;
|
||||
report.ssh.dockerVersion = ver;
|
||||
} else {
|
||||
report.ssh.dockerDaemonOk = false;
|
||||
report.ssh.error = `docker probe exit ${res.code}: ${(res.stderr || res.stdout || '(empty)').slice(0, 600)}`;
|
||||
}
|
||||
} catch (e) {
|
||||
report.ssh.latencyMs = Date.now() - tSsh;
|
||||
report.ssh.reachable = false;
|
||||
report.ssh.dockerDaemonOk = false;
|
||||
report.ssh.error = e instanceof Error ? e.message : String(e);
|
||||
}
|
||||
|
||||
return report;
|
||||
}
|
||||
|
||||
export function isCoolifyInfraOperational(report: CoolifyInfraHealthReport): boolean {
|
||||
return (
|
||||
report.ssh.configured &&
|
||||
report.ssh.reachable === true &&
|
||||
report.ssh.dockerDaemonOk === true &&
|
||||
report.api.configured &&
|
||||
report.api.reachable === true
|
||||
);
|
||||
}
|
||||
@@ -1,102 +0,0 @@
|
||||
import { listChatExtractions } from '@/lib/server/chat-extraction';
|
||||
import { clamp, nowIso, persistPhaseArtifacts, uniqueStrings, toStage } from '@/lib/server/projects';
|
||||
import type { CanonicalProductModel } from '@/lib/types/product-model';
|
||||
import type { ChatExtractionRecord } from '@/lib/types/chat-extraction';
|
||||
|
||||
const average = (numbers: number[]) =>
|
||||
numbers.length ? numbers.reduce((sum, value) => sum + value, 0) / numbers.length : 0;
|
||||
|
||||
export async function buildCanonicalProductModel(projectId: string): Promise<CanonicalProductModel> {
|
||||
const extractions = await listChatExtractions(projectId);
|
||||
if (!extractions.length) {
|
||||
throw new Error('No chat extractions found for project');
|
||||
}
|
||||
|
||||
const completionAvg = average(
|
||||
extractions.map(
|
||||
(record) =>
|
||||
(record.data as any)?.summary_scores?.overall_completion ?? record.overallCompletion ?? 0,
|
||||
),
|
||||
);
|
||||
const confidenceAvg = average(
|
||||
extractions.map(
|
||||
(record) =>
|
||||
(record.data as any)?.summary_scores?.overall_confidence ?? record.overallConfidence ?? 0,
|
||||
),
|
||||
);
|
||||
|
||||
const canonical = mapExtractionToCanonical(
|
||||
projectId,
|
||||
pickHighestConfidence(extractions as any),
|
||||
completionAvg,
|
||||
confidenceAvg,
|
||||
);
|
||||
|
||||
await persistPhaseArtifacts(projectId, (phaseData, phaseScores, phaseHistory) => {
|
||||
phaseData.canonicalProductModel = canonical;
|
||||
phaseScores.vision = {
|
||||
overallCompletion: canonical.overallCompletion,
|
||||
overallConfidence: canonical.overallConfidence,
|
||||
updatedAt: nowIso(),
|
||||
};
|
||||
phaseHistory.push({ phase: 'vision', status: 'completed', timestamp: nowIso() });
|
||||
return { phaseData, phaseScores, phaseHistory, nextPhase: 'vision_ready' };
|
||||
});
|
||||
|
||||
return canonical;
|
||||
}
|
||||
|
||||
function pickHighestConfidence(records: ChatExtractionRecord[]) {
|
||||
return records.reduce((best, record) =>
|
||||
record.overallConfidence > best.overallConfidence ? record : best,
|
||||
);
|
||||
}
|
||||
|
||||
function mapExtractionToCanonical(
|
||||
projectId: string,
|
||||
record: ChatExtractionRecord,
|
||||
completionAvg: number,
|
||||
confidenceAvg: number,
|
||||
): CanonicalProductModel {
|
||||
const data = record.data;
|
||||
|
||||
const coreFeatures = data.solution_and_features.core_features.map(
|
||||
(feature) => feature.name || feature.description,
|
||||
);
|
||||
const niceToHaveFeatures = data.solution_and_features.nice_to_have_features.map(
|
||||
(feature) => feature.name || feature.description,
|
||||
);
|
||||
|
||||
return {
|
||||
projectId,
|
||||
workingTitle: data.project_summary.working_title ?? null,
|
||||
oneLiner: data.project_summary.one_liner ?? null,
|
||||
problem: data.product_vision.problem_statement.description ?? null,
|
||||
targetUser: data.target_users.primary_segment.description ?? null,
|
||||
desiredOutcome: data.product_vision.target_outcome.description ?? null,
|
||||
coreSolution: data.solution_and_features.core_solution.description ?? null,
|
||||
coreFeatures: uniqueStrings(coreFeatures),
|
||||
niceToHaveFeatures: uniqueStrings(niceToHaveFeatures),
|
||||
marketCategory: data.market_and_competition.market_category.description ?? null,
|
||||
competitors: uniqueStrings(
|
||||
data.market_and_competition.competitors.map((competitor) => competitor.name),
|
||||
),
|
||||
techStack: uniqueStrings(
|
||||
data.tech_and_constraints.stack_mentions.map((item) => item.description),
|
||||
),
|
||||
constraints: uniqueStrings(
|
||||
data.tech_and_constraints.constraints.map((constraint) => constraint.description),
|
||||
),
|
||||
currentStage: toStage(data.project_summary.stage),
|
||||
shortTermGoals: uniqueStrings(
|
||||
data.goals_and_success.short_term_goals.map((goal) => goal.description),
|
||||
),
|
||||
longTermGoals: uniqueStrings(
|
||||
data.goals_and_success.long_term_goals.map((goal) => goal.description),
|
||||
),
|
||||
overallCompletion: clamp(completionAvg),
|
||||
overallConfidence: clamp(confidenceAvg),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -1,453 +0,0 @@
|
||||
/**
|
||||
* Server-side helpers for AlloyDB vector memory operations
|
||||
*
|
||||
* Handles CRUD operations on knowledge_chunks and semantic search.
|
||||
*/
|
||||
|
||||
import { getAlloyDbClient, executeQuery, getPooledClient } from '@/lib/db/alloydb';
|
||||
import type {
|
||||
KnowledgeChunk,
|
||||
KnowledgeChunkRow,
|
||||
KnowledgeChunkSearchResult,
|
||||
VectorSearchOptions,
|
||||
CreateKnowledgeChunkInput,
|
||||
BatchCreateKnowledgeChunksInput,
|
||||
} from '@/lib/types/vector-memory';
|
||||
|
||||
/**
|
||||
* Convert database row (snake_case) to TypeScript object (camelCase)
|
||||
*/
|
||||
function rowToKnowledgeChunk(row: KnowledgeChunkRow): KnowledgeChunk {
|
||||
return {
|
||||
id: row.id,
|
||||
projectId: row.project_id,
|
||||
knowledgeItemId: row.knowledge_item_id,
|
||||
chunkIndex: row.chunk_index,
|
||||
content: row.content,
|
||||
sourceType: row.source_type,
|
||||
importance: row.importance,
|
||||
createdAt: row.created_at,
|
||||
updatedAt: row.updated_at,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve relevant knowledge chunks using vector similarity search
|
||||
*
|
||||
* @param projectId - Firestore project ID to filter by
|
||||
* @param queryEmbedding - Vector embedding of the query (e.g., user's question)
|
||||
* @param options - Search options (limit, filters, etc.)
|
||||
* @returns Array of chunks ordered by similarity (most relevant first)
|
||||
*
|
||||
* @example
|
||||
* ```typescript
|
||||
* const embedding = await embedText("What's the MVP scope?");
|
||||
* const chunks = await retrieveRelevantChunks('proj123', embedding, { limit: 10, minSimilarity: 0.7 });
|
||||
* ```
|
||||
*/
|
||||
export async function retrieveRelevantChunks(
|
||||
projectId: string,
|
||||
queryEmbedding: number[],
|
||||
options: VectorSearchOptions = {}
|
||||
): Promise<KnowledgeChunkSearchResult[]> {
|
||||
const {
|
||||
limit = 10,
|
||||
minSimilarity,
|
||||
sourceTypes,
|
||||
importanceLevels,
|
||||
} = options;
|
||||
|
||||
try {
|
||||
// Build the query with optional filters
|
||||
let queryText = `
|
||||
SELECT
|
||||
id,
|
||||
project_id,
|
||||
knowledge_item_id,
|
||||
chunk_index,
|
||||
content,
|
||||
source_type,
|
||||
importance,
|
||||
created_at,
|
||||
updated_at,
|
||||
1 - (embedding <=> $1::vector) AS similarity
|
||||
FROM knowledge_chunks
|
||||
WHERE project_id = $2
|
||||
`;
|
||||
|
||||
const params: any[] = [JSON.stringify(queryEmbedding), projectId];
|
||||
let paramIndex = 3;
|
||||
|
||||
// Filter by source types
|
||||
if (sourceTypes && sourceTypes.length > 0) {
|
||||
queryText += ` AND source_type = ANY($${paramIndex})`;
|
||||
params.push(sourceTypes);
|
||||
paramIndex++;
|
||||
}
|
||||
|
||||
// Filter by importance levels
|
||||
if (importanceLevels && importanceLevels.length > 0) {
|
||||
queryText += ` AND importance = ANY($${paramIndex})`;
|
||||
params.push(importanceLevels);
|
||||
paramIndex++;
|
||||
}
|
||||
|
||||
// Filter by minimum similarity
|
||||
if (minSimilarity !== undefined) {
|
||||
queryText += ` AND (1 - (embedding <=> $1::vector)) >= $${paramIndex}`;
|
||||
params.push(minSimilarity);
|
||||
paramIndex++;
|
||||
}
|
||||
|
||||
// Order by similarity and limit
|
||||
queryText += ` ORDER BY embedding <=> $1::vector LIMIT $${paramIndex}`;
|
||||
params.push(limit);
|
||||
|
||||
const result = await executeQuery<KnowledgeChunkRow & { similarity: number }>(
|
||||
queryText,
|
||||
params
|
||||
);
|
||||
|
||||
return result.rows.map((row) => ({
|
||||
...rowToKnowledgeChunk(row),
|
||||
similarity: row.similarity,
|
||||
}));
|
||||
} catch (error) {
|
||||
console.error('[Vector Memory] Failed to retrieve relevant chunks:', error);
|
||||
throw new Error(
|
||||
`Failed to retrieve chunks: ${error instanceof Error ? error.message : String(error)}`
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a single knowledge chunk
|
||||
*/
|
||||
export async function createKnowledgeChunk(
|
||||
input: CreateKnowledgeChunkInput
|
||||
): Promise<KnowledgeChunk> {
|
||||
const {
|
||||
projectId,
|
||||
knowledgeItemId,
|
||||
chunkIndex,
|
||||
content,
|
||||
embedding,
|
||||
sourceType = null,
|
||||
importance = null,
|
||||
} = input;
|
||||
|
||||
try {
|
||||
const queryText = `
|
||||
INSERT INTO knowledge_chunks (
|
||||
project_id,
|
||||
knowledge_item_id,
|
||||
chunk_index,
|
||||
content,
|
||||
embedding,
|
||||
source_type,
|
||||
importance
|
||||
)
|
||||
VALUES ($1, $2, $3, $4, $5::vector, $6, $7)
|
||||
RETURNING
|
||||
id,
|
||||
project_id,
|
||||
knowledge_item_id,
|
||||
chunk_index,
|
||||
content,
|
||||
source_type,
|
||||
importance,
|
||||
created_at,
|
||||
updated_at
|
||||
`;
|
||||
|
||||
const result = await executeQuery<KnowledgeChunkRow>(queryText, [
|
||||
projectId,
|
||||
knowledgeItemId,
|
||||
chunkIndex,
|
||||
content,
|
||||
JSON.stringify(embedding),
|
||||
sourceType,
|
||||
importance,
|
||||
]);
|
||||
|
||||
if (result.rows.length === 0) {
|
||||
throw new Error('Failed to insert knowledge chunk');
|
||||
}
|
||||
|
||||
return rowToKnowledgeChunk(result.rows[0]);
|
||||
} catch (error) {
|
||||
console.error('[Vector Memory] Failed to create knowledge chunk:', error);
|
||||
throw new Error(
|
||||
`Failed to create chunk: ${error instanceof Error ? error.message : String(error)}`
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Batch create multiple knowledge chunks efficiently
|
||||
*
|
||||
* Uses a transaction to ensure atomicity.
|
||||
*/
|
||||
export async function batchCreateKnowledgeChunks(
|
||||
input: BatchCreateKnowledgeChunksInput
|
||||
): Promise<KnowledgeChunk[]> {
|
||||
const { projectId, knowledgeItemId, chunks } = input;
|
||||
|
||||
if (chunks.length === 0) {
|
||||
return [];
|
||||
}
|
||||
|
||||
const client = await getPooledClient();
|
||||
|
||||
try {
|
||||
await client.query('BEGIN');
|
||||
|
||||
const createdChunks: KnowledgeChunk[] = [];
|
||||
|
||||
for (const chunk of chunks) {
|
||||
const queryText = `
|
||||
INSERT INTO knowledge_chunks (
|
||||
project_id,
|
||||
knowledge_item_id,
|
||||
chunk_index,
|
||||
content,
|
||||
embedding,
|
||||
source_type,
|
||||
importance
|
||||
)
|
||||
VALUES ($1, $2, $3, $4, $5::vector, $6, $7)
|
||||
RETURNING
|
||||
id,
|
||||
project_id,
|
||||
knowledge_item_id,
|
||||
chunk_index,
|
||||
content,
|
||||
source_type,
|
||||
importance,
|
||||
created_at,
|
||||
updated_at
|
||||
`;
|
||||
|
||||
const result = await client.query<KnowledgeChunkRow>(queryText, [
|
||||
projectId,
|
||||
knowledgeItemId,
|
||||
chunk.chunkIndex,
|
||||
chunk.content,
|
||||
JSON.stringify(chunk.embedding),
|
||||
chunk.sourceType ?? null,
|
||||
chunk.importance ?? null,
|
||||
]);
|
||||
|
||||
if (result.rows.length > 0) {
|
||||
createdChunks.push(rowToKnowledgeChunk(result.rows[0]));
|
||||
}
|
||||
}
|
||||
|
||||
await client.query('COMMIT');
|
||||
|
||||
console.log(
|
||||
`[Vector Memory] Batch created ${createdChunks.length} chunks for knowledge_item ${knowledgeItemId}`
|
||||
);
|
||||
|
||||
return createdChunks;
|
||||
} catch (error) {
|
||||
await client.query('ROLLBACK');
|
||||
console.error('[Vector Memory] Failed to batch create chunks:', error);
|
||||
throw new Error(
|
||||
`Failed to batch create chunks: ${error instanceof Error ? error.message : String(error)}`
|
||||
);
|
||||
} finally {
|
||||
client.release();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete all chunks for a specific knowledge_item
|
||||
*
|
||||
* Used when regenerating chunks or removing a knowledge_item.
|
||||
*/
|
||||
export async function deleteChunksForKnowledgeItem(
|
||||
knowledgeItemId: string
|
||||
): Promise<number> {
|
||||
try {
|
||||
const queryText = `
|
||||
DELETE FROM knowledge_chunks
|
||||
WHERE knowledge_item_id = $1
|
||||
RETURNING id
|
||||
`;
|
||||
|
||||
const result = await executeQuery(queryText, [knowledgeItemId]);
|
||||
|
||||
console.log(
|
||||
`[Vector Memory] Deleted ${result.rowCount ?? 0} chunks for knowledge_item ${knowledgeItemId}`
|
||||
);
|
||||
|
||||
return result.rowCount ?? 0;
|
||||
} catch (error) {
|
||||
console.error('[Vector Memory] Failed to delete chunks:', error);
|
||||
throw new Error(
|
||||
`Failed to delete chunks: ${error instanceof Error ? error.message : String(error)}`
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete all chunks for a specific project
|
||||
*
|
||||
* Used when cleaning up or resetting a project.
|
||||
*/
|
||||
export async function deleteChunksForProject(projectId: string): Promise<number> {
|
||||
try {
|
||||
const queryText = `
|
||||
DELETE FROM knowledge_chunks
|
||||
WHERE project_id = $1
|
||||
RETURNING id
|
||||
`;
|
||||
|
||||
const result = await executeQuery(queryText, [projectId]);
|
||||
|
||||
console.log(
|
||||
`[Vector Memory] Deleted ${result.rowCount ?? 0} chunks for project ${projectId}`
|
||||
);
|
||||
|
||||
return result.rowCount ?? 0;
|
||||
} catch (error) {
|
||||
console.error('[Vector Memory] Failed to delete project chunks:', error);
|
||||
throw new Error(
|
||||
`Failed to delete project chunks: ${error instanceof Error ? error.message : String(error)}`
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get chunk count for a knowledge_item
|
||||
*/
|
||||
export async function getChunkCountForKnowledgeItem(
|
||||
knowledgeItemId: string
|
||||
): Promise<number> {
|
||||
try {
|
||||
const result = await executeQuery<{ count: string }>(
|
||||
'SELECT COUNT(*) as count FROM knowledge_chunks WHERE knowledge_item_id = $1',
|
||||
[knowledgeItemId]
|
||||
);
|
||||
|
||||
return parseInt(result.rows[0]?.count ?? '0', 10);
|
||||
} catch (error) {
|
||||
console.error('[Vector Memory] Failed to get chunk count:', error);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get chunk count for a project
|
||||
*/
|
||||
export async function getChunkCountForProject(projectId: string): Promise<number> {
|
||||
try {
|
||||
const result = await executeQuery<{ count: string }>(
|
||||
'SELECT COUNT(*) as count FROM knowledge_chunks WHERE project_id = $1',
|
||||
[projectId]
|
||||
);
|
||||
|
||||
return parseInt(result.rows[0]?.count ?? '0', 10);
|
||||
} catch (error) {
|
||||
console.error('[Vector Memory] Failed to get project chunk count:', error);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Regenerate knowledge_chunks for a single knowledge_item
|
||||
*
|
||||
* This is the main pipeline that:
|
||||
* 1. Chunks the knowledge_item.content
|
||||
* 2. Generates embeddings for each chunk
|
||||
* 3. Deletes existing chunks for this item
|
||||
* 4. Inserts new chunks into AlloyDB
|
||||
*
|
||||
* @param knowledgeItem - The knowledge item to process
|
||||
*
|
||||
* @example
|
||||
* ```typescript
|
||||
* const knowledgeItem = await getKnowledgeItem(projectId, itemId);
|
||||
* await writeKnowledgeChunksForItem(knowledgeItem);
|
||||
* ```
|
||||
*/
|
||||
export async function writeKnowledgeChunksForItem(
|
||||
knowledgeItem: {
|
||||
id: string;
|
||||
projectId: string;
|
||||
content: string;
|
||||
sourceMeta?: { sourceType?: string; importance?: 'primary' | 'supporting' | 'irrelevant' };
|
||||
}
|
||||
): Promise<void> {
|
||||
const { chunkText } = await import('@/lib/ai/chunking');
|
||||
const { embedTextBatch } = await import('@/lib/ai/embeddings');
|
||||
|
||||
try {
|
||||
console.log(
|
||||
`[Vector Memory] Starting chunking pipeline for knowledge_item ${knowledgeItem.id}`
|
||||
);
|
||||
|
||||
// Step 1: Chunk the content
|
||||
const textChunks = chunkText(knowledgeItem.content, {
|
||||
maxTokens: 800,
|
||||
overlapChars: 200,
|
||||
preserveParagraphs: true,
|
||||
});
|
||||
|
||||
if (textChunks.length === 0) {
|
||||
console.warn(
|
||||
`[Vector Memory] No chunks generated for knowledge_item ${knowledgeItem.id} - content may be empty`
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
console.log(
|
||||
`[Vector Memory] Generated ${textChunks.length} chunks for knowledge_item ${knowledgeItem.id}`
|
||||
);
|
||||
|
||||
// Step 2: Generate embeddings for all chunks
|
||||
const chunkTexts = textChunks.map((chunk) => chunk.text);
|
||||
const embeddings = await embedTextBatch(chunkTexts, {
|
||||
delayMs: 50, // Small delay to avoid rate limiting
|
||||
skipEmpty: true,
|
||||
});
|
||||
|
||||
if (embeddings.length !== textChunks.length) {
|
||||
throw new Error(
|
||||
`Embedding count mismatch: got ${embeddings.length}, expected ${textChunks.length}`
|
||||
);
|
||||
}
|
||||
|
||||
// Step 3: Delete existing chunks for this knowledge_item
|
||||
await deleteChunksForKnowledgeItem(knowledgeItem.id);
|
||||
|
||||
// Step 4: Insert new chunks
|
||||
const chunksToInsert = textChunks.map((chunk, index) => ({
|
||||
chunkIndex: chunk.index,
|
||||
content: chunk.text,
|
||||
embedding: embeddings[index],
|
||||
sourceType: knowledgeItem.sourceMeta?.sourceType ?? null,
|
||||
importance: knowledgeItem.sourceMeta?.importance ?? null,
|
||||
}));
|
||||
|
||||
await batchCreateKnowledgeChunks({
|
||||
projectId: knowledgeItem.projectId,
|
||||
knowledgeItemId: knowledgeItem.id,
|
||||
chunks: chunksToInsert,
|
||||
});
|
||||
|
||||
console.log(
|
||||
`[Vector Memory] Successfully processed ${chunksToInsert.length} chunks for knowledge_item ${knowledgeItem.id}`
|
||||
);
|
||||
} catch (error) {
|
||||
console.error(
|
||||
`[Vector Memory] Failed to write chunks for knowledge_item ${knowledgeItem.id}:`,
|
||||
error
|
||||
);
|
||||
throw new Error(
|
||||
`Failed to write chunks: ${error instanceof Error ? error.message : String(error)}`
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,14 +0,0 @@
|
||||
import type { ChatExtractionData } from '@/lib/ai/chat-extraction-types';
|
||||
|
||||
export interface ChatExtractionRecord<TData = ChatExtractionData> {
|
||||
id: string;
|
||||
projectId: string;
|
||||
knowledgeItemId: string;
|
||||
data: TData;
|
||||
overallCompletion: number;
|
||||
overallConfidence: number;
|
||||
createdAt: FirebaseFirestore.Timestamp;
|
||||
updatedAt: FirebaseFirestore.Timestamp;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,122 +0,0 @@
|
||||
/**
|
||||
* Type definitions for AI-generated MVP Plan
|
||||
* Based on the Vibn MVP Planner agent spec
|
||||
*/
|
||||
|
||||
// Project-level
|
||||
export type Project = {
|
||||
id: string;
|
||||
ownerUserId: string;
|
||||
name: string;
|
||||
createdAt: string;
|
||||
updatedAt: string;
|
||||
summary: string; // final summary text from planner
|
||||
};
|
||||
|
||||
// Raw vision answers
|
||||
export type VisionInput = {
|
||||
projectId: string;
|
||||
q1_who_and_problem: string;
|
||||
q2_story: string;
|
||||
q3_improvement: string;
|
||||
createdAt: string;
|
||||
};
|
||||
|
||||
// Optional work-to-date summary
|
||||
export type WorkToDate = {
|
||||
projectId: string;
|
||||
codeSummary?: string;
|
||||
githubSummary?: string;
|
||||
docsLinksOrText?: string[]; // or JSON
|
||||
existingAssetsNotes?: string;
|
||||
createdAt: string;
|
||||
};
|
||||
|
||||
// Trees (Journey / Touchpoints / System)
|
||||
export type TreeType = "journey" | "touchpoints" | "system";
|
||||
|
||||
export type Tree = {
|
||||
id: string;
|
||||
projectId: string;
|
||||
type: TreeType;
|
||||
label: string;
|
||||
createdAt: string;
|
||||
updatedAt: string;
|
||||
};
|
||||
|
||||
// Asset types from agent spec
|
||||
export type AssetType =
|
||||
| "web_page"
|
||||
| "app_screen"
|
||||
| "flow"
|
||||
| "component"
|
||||
| "email"
|
||||
| "notification"
|
||||
| "document"
|
||||
| "social_post"
|
||||
| "data_model"
|
||||
| "api_endpoint"
|
||||
| "service"
|
||||
| "integration"
|
||||
| "job"
|
||||
| "infrastructure"
|
||||
| "automation"
|
||||
| "other";
|
||||
|
||||
// Each node in any tree
|
||||
export type AssetNode = {
|
||||
id: string;
|
||||
projectId: string;
|
||||
treeId: string;
|
||||
parentId: string | null;
|
||||
name: string;
|
||||
assetType: AssetType;
|
||||
mustHaveForV1: boolean;
|
||||
createdAt: string;
|
||||
updatedAt: string;
|
||||
children?: AssetNode[]; // For nested structure
|
||||
};
|
||||
|
||||
// Node metadata (reasoning, mapping back to vision)
|
||||
export type AssetMetadata = {
|
||||
assetNodeId: string;
|
||||
whyItExists: string;
|
||||
whichUserItServes?: string;
|
||||
problemItHelpsWith?: string;
|
||||
connectionToMagicMoment: string;
|
||||
valueContribution?: string;
|
||||
journeyStage?: string;
|
||||
messagingTone?: string;
|
||||
visualStyleNotes?: string;
|
||||
dependencies?: string[]; // other AssetNode IDs
|
||||
implementationNotes?: string;
|
||||
};
|
||||
|
||||
// Context memory events
|
||||
export type ContextEvent = {
|
||||
id: string;
|
||||
projectId: string;
|
||||
sourceType: "chat" | "commit" | "file" | "doc" | "manual_note";
|
||||
sourceId?: string; // e.g. commit hash, file path
|
||||
timestamp: string;
|
||||
title: string;
|
||||
body: string; // raw text or JSON
|
||||
};
|
||||
|
||||
// Full AI response from agent
|
||||
export type AIAgentResponse = {
|
||||
journey_tree: {
|
||||
label: string;
|
||||
nodes: Array<AssetNode & { asset_metadata: AssetMetadata }>;
|
||||
};
|
||||
touchpoints_tree: {
|
||||
label: string;
|
||||
nodes: Array<AssetNode & { asset_metadata: AssetMetadata }>;
|
||||
};
|
||||
system_tree: {
|
||||
label: string;
|
||||
nodes: Array<AssetNode & { asset_metadata: AssetMetadata }>;
|
||||
};
|
||||
summary: string;
|
||||
};
|
||||
|
||||
@@ -1,37 +0,0 @@
|
||||
export type ProjectStage =
|
||||
| 'idea'
|
||||
| 'prototype'
|
||||
| 'mvp_in_progress'
|
||||
| 'live_beta'
|
||||
| 'live_paid'
|
||||
| 'unknown';
|
||||
|
||||
export interface CanonicalProductModel {
|
||||
projectId: string;
|
||||
workingTitle: string | null;
|
||||
oneLiner: string | null;
|
||||
|
||||
problem: string | null;
|
||||
targetUser: string | null;
|
||||
desiredOutcome: string | null;
|
||||
coreSolution: string | null;
|
||||
|
||||
coreFeatures: string[];
|
||||
niceToHaveFeatures: string[];
|
||||
|
||||
marketCategory: string | null;
|
||||
competitors: string[];
|
||||
|
||||
techStack: string[];
|
||||
constraints: string[];
|
||||
|
||||
currentStage: ProjectStage;
|
||||
|
||||
shortTermGoals: string[];
|
||||
longTermGoals: string[];
|
||||
|
||||
overallCompletion: number;
|
||||
overallConfidence: number;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,223 +0,0 @@
|
||||
/**
|
||||
* Code-specific chunking for source code files
|
||||
* Intelligently splits code while preserving context
|
||||
*/
|
||||
|
||||
export interface CodeChunk {
|
||||
content: string;
|
||||
metadata: {
|
||||
chunkIndex: number;
|
||||
totalChunks: number;
|
||||
startLine: number;
|
||||
endLine: number;
|
||||
tokenCount: number;
|
||||
filePath: string;
|
||||
language?: string;
|
||||
};
|
||||
}
|
||||
|
||||
export interface CodeChunkOptions {
|
||||
maxChunkSize?: number; // characters
|
||||
chunkOverlap?: number; // lines
|
||||
preserveFunctions?: boolean;
|
||||
preserveClasses?: boolean;
|
||||
filePath: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Estimate token count (rough approximation: 1 token ≈ 4 characters)
|
||||
*/
|
||||
function estimateTokens(text: string): number {
|
||||
return Math.ceil(text.length / 4);
|
||||
}
|
||||
|
||||
/**
|
||||
* Detect language from file path
|
||||
*/
|
||||
function detectLanguage(filePath: string): string | undefined {
|
||||
const ext = filePath.split('.').pop()?.toLowerCase();
|
||||
const langMap: Record<string, string> = {
|
||||
ts: 'typescript',
|
||||
tsx: 'typescript',
|
||||
js: 'javascript',
|
||||
jsx: 'javascript',
|
||||
py: 'python',
|
||||
java: 'java',
|
||||
go: 'go',
|
||||
rs: 'rust',
|
||||
cpp: 'cpp',
|
||||
c: 'c',
|
||||
cs: 'csharp',
|
||||
rb: 'ruby',
|
||||
php: 'php',
|
||||
swift: 'swift',
|
||||
kt: 'kotlin',
|
||||
sql: 'sql',
|
||||
css: 'css',
|
||||
scss: 'scss',
|
||||
html: 'html',
|
||||
json: 'json',
|
||||
yaml: 'yaml',
|
||||
yml: 'yaml',
|
||||
md: 'markdown',
|
||||
};
|
||||
return langMap[ext || ''];
|
||||
}
|
||||
|
||||
/**
|
||||
* Chunk source code file intelligently
|
||||
*/
|
||||
export function chunkCode(
|
||||
content: string,
|
||||
options: CodeChunkOptions
|
||||
): CodeChunk[] {
|
||||
const {
|
||||
maxChunkSize = 3000, // Larger chunks for code context
|
||||
chunkOverlap = 5,
|
||||
preserveFunctions = true,
|
||||
preserveClasses = true,
|
||||
filePath,
|
||||
} = options;
|
||||
|
||||
const language = detectLanguage(filePath);
|
||||
const lines = content.split('\n');
|
||||
|
||||
// For small files, return as single chunk
|
||||
if (content.length <= maxChunkSize) {
|
||||
return [
|
||||
{
|
||||
content,
|
||||
metadata: {
|
||||
chunkIndex: 0,
|
||||
totalChunks: 1,
|
||||
startLine: 1,
|
||||
endLine: lines.length,
|
||||
tokenCount: estimateTokens(content),
|
||||
filePath,
|
||||
language,
|
||||
},
|
||||
},
|
||||
];
|
||||
}
|
||||
|
||||
// For larger files, split by logical boundaries
|
||||
const chunks: CodeChunk[] = [];
|
||||
let currentChunk: string[] = [];
|
||||
let currentSize = 0;
|
||||
let chunkStartLine = 1;
|
||||
|
||||
// Patterns for detecting logical boundaries
|
||||
const functionPattern = /^\s*(function|def|fn|func|fun|public|private|protected|static|async|export)\s/;
|
||||
const classPattern = /^\s*(class|interface|struct|enum|type)\s/;
|
||||
const importPattern = /^\s*(import|from|require|using|include)\s/;
|
||||
const commentPattern = /^\s*(\/\/|\/\*|\*|#|--|<!--)/;
|
||||
|
||||
// Always include file header (imports, comments at top)
|
||||
let headerLines: string[] = [];
|
||||
for (let i = 0; i < Math.min(20, lines.length); i++) {
|
||||
const line = lines[i];
|
||||
if (importPattern.test(line) || commentPattern.test(line) || line.trim() === '') {
|
||||
headerLines.push(line);
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
for (let i = 0; i < lines.length; i++) {
|
||||
const line = lines[i];
|
||||
const lineSize = line.length + 1; // +1 for newline
|
||||
|
||||
// Check if we should start a new chunk
|
||||
const shouldSplit =
|
||||
currentSize + lineSize > maxChunkSize &&
|
||||
currentChunk.length > 0 &&
|
||||
(functionPattern.test(line) ||
|
||||
classPattern.test(line) ||
|
||||
(line.trim() === '' && currentSize > maxChunkSize * 0.7));
|
||||
|
||||
if (shouldSplit) {
|
||||
// Save current chunk
|
||||
const chunkContent = currentChunk.join('\n');
|
||||
chunks.push({
|
||||
content: chunkContent,
|
||||
metadata: {
|
||||
chunkIndex: chunks.length,
|
||||
totalChunks: 0, // Will update at end
|
||||
startLine: chunkStartLine,
|
||||
endLine: chunkStartLine + currentChunk.length - 1,
|
||||
tokenCount: estimateTokens(chunkContent),
|
||||
filePath,
|
||||
language,
|
||||
},
|
||||
});
|
||||
|
||||
// Start new chunk with overlap and header
|
||||
const overlapStart = Math.max(0, currentChunk.length - chunkOverlap);
|
||||
currentChunk = [
|
||||
...headerLines,
|
||||
'',
|
||||
`// ... continued from line ${chunkStartLine}`,
|
||||
'',
|
||||
...currentChunk.slice(overlapStart),
|
||||
];
|
||||
currentSize = currentChunk.reduce((sum, l) => sum + l.length + 1, 0);
|
||||
chunkStartLine = chunkStartLine + overlapStart;
|
||||
}
|
||||
|
||||
currentChunk.push(line);
|
||||
currentSize += lineSize;
|
||||
}
|
||||
|
||||
// Add final chunk
|
||||
if (currentChunk.length > 0) {
|
||||
const chunkContent = currentChunk.join('\n');
|
||||
chunks.push({
|
||||
content: chunkContent,
|
||||
metadata: {
|
||||
chunkIndex: chunks.length,
|
||||
totalChunks: 0,
|
||||
startLine: chunkStartLine,
|
||||
endLine: lines.length,
|
||||
tokenCount: estimateTokens(chunkContent),
|
||||
filePath,
|
||||
language,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
// Update totalChunks for all chunks
|
||||
chunks.forEach((chunk) => {
|
||||
chunk.metadata.totalChunks = chunks.length;
|
||||
});
|
||||
|
||||
return chunks;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate a summary header for a code file
|
||||
*/
|
||||
export function generateCodeSummary(
|
||||
filePath: string,
|
||||
content: string,
|
||||
language?: string
|
||||
): string {
|
||||
const lines = content.split('\n');
|
||||
const functions = lines.filter(line => /^\s*(function|def|fn|func|async function|export function)/.test(line));
|
||||
const classes = lines.filter(line => /^\s*(class|interface|struct|enum|type)\s/.test(line));
|
||||
|
||||
let summary = `File: ${filePath}\n`;
|
||||
if (language) {
|
||||
summary += `Language: ${language}\n`;
|
||||
}
|
||||
summary += `Lines: ${lines.length}\n`;
|
||||
|
||||
if (functions.length > 0) {
|
||||
summary += `Functions: ${functions.length}\n`;
|
||||
}
|
||||
if (classes.length > 0) {
|
||||
summary += `Classes/Types: ${classes.length}\n`;
|
||||
}
|
||||
|
||||
return summary;
|
||||
}
|
||||
|
||||
@@ -1,206 +0,0 @@
|
||||
/**
|
||||
* Document Chunking Utility
|
||||
*
|
||||
* Splits large documents into manageable chunks for AI processing.
|
||||
* Uses semantic chunking with configurable overlap for better context.
|
||||
*/
|
||||
|
||||
export interface ChunkMetadata {
|
||||
chunkIndex: number;
|
||||
totalChunks: number;
|
||||
startChar: number;
|
||||
endChar: number;
|
||||
tokenCount: number;
|
||||
}
|
||||
|
||||
export interface DocumentChunk {
|
||||
content: string;
|
||||
metadata: ChunkMetadata;
|
||||
}
|
||||
|
||||
export interface ChunkingOptions {
|
||||
maxChunkSize?: number; // Maximum characters per chunk (default: 2000)
|
||||
chunkOverlap?: number; // Overlap between chunks (default: 200)
|
||||
preserveParagraphs?: boolean; // Try to keep paragraphs intact (default: true)
|
||||
preserveCodeBlocks?: boolean; // Keep code blocks together (default: true)
|
||||
}
|
||||
|
||||
const DEFAULT_OPTIONS: Required<ChunkingOptions> = {
|
||||
maxChunkSize: 2000,
|
||||
chunkOverlap: 200,
|
||||
preserveParagraphs: true,
|
||||
preserveCodeBlocks: true,
|
||||
};
|
||||
|
||||
/**
|
||||
* Estimate token count (rough approximation: 1 token ≈ 4 characters)
|
||||
*/
|
||||
function estimateTokens(text: string): number {
|
||||
return Math.ceil(text.length / 4);
|
||||
}
|
||||
|
||||
/**
|
||||
* Find good split points (paragraph breaks, sentence boundaries)
|
||||
*/
|
||||
function findSplitPoint(text: string, idealSplit: number): number {
|
||||
// Try to split at paragraph break first
|
||||
const paragraphBreak = text.lastIndexOf('\n\n', idealSplit);
|
||||
if (paragraphBreak > idealSplit - 500 && paragraphBreak > 0) {
|
||||
return paragraphBreak + 2;
|
||||
}
|
||||
|
||||
// Try sentence boundary
|
||||
const sentenceEnd = text.lastIndexOf('. ', idealSplit);
|
||||
if (sentenceEnd > idealSplit - 300 && sentenceEnd > 0) {
|
||||
return sentenceEnd + 2;
|
||||
}
|
||||
|
||||
// Try any newline
|
||||
const newline = text.lastIndexOf('\n', idealSplit);
|
||||
if (newline > idealSplit - 200 && newline > 0) {
|
||||
return newline + 1;
|
||||
}
|
||||
|
||||
// Last resort: split at space
|
||||
const space = text.lastIndexOf(' ', idealSplit);
|
||||
return space > 0 ? space + 1 : idealSplit;
|
||||
}
|
||||
|
||||
/**
|
||||
* Extract code blocks to preserve them
|
||||
*/
|
||||
function extractCodeBlocks(text: string): { text: string; codeBlocks: Map<string, string> } {
|
||||
const codeBlocks = new Map<string, string>();
|
||||
let counter = 0;
|
||||
|
||||
const processedText = text.replace(/```[\s\S]*?```/g, (match) => {
|
||||
const placeholder = `__CODE_BLOCK_${counter}__`;
|
||||
codeBlocks.set(placeholder, match);
|
||||
counter++;
|
||||
return placeholder;
|
||||
});
|
||||
|
||||
return { text: processedText, codeBlocks };
|
||||
}
|
||||
|
||||
/**
|
||||
* Restore code blocks
|
||||
*/
|
||||
function restoreCodeBlocks(text: string, codeBlocks: Map<string, string>): string {
|
||||
let result = text;
|
||||
codeBlocks.forEach((code, placeholder) => {
|
||||
result = result.replace(placeholder, code);
|
||||
});
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Split a document into semantic chunks
|
||||
*/
|
||||
export function chunkDocument(content: string, options: ChunkingOptions = {}): DocumentChunk[] {
|
||||
const opts = { ...DEFAULT_OPTIONS, ...options };
|
||||
const chunks: DocumentChunk[] = [];
|
||||
|
||||
// Handle empty content
|
||||
if (!content || content.trim().length === 0) {
|
||||
return chunks;
|
||||
}
|
||||
|
||||
// Extract code blocks if preserving them
|
||||
let processedContent = content;
|
||||
let codeBlocks = new Map<string, string>();
|
||||
|
||||
if (opts.preserveCodeBlocks) {
|
||||
const extracted = extractCodeBlocks(content);
|
||||
processedContent = extracted.text;
|
||||
codeBlocks = extracted.codeBlocks;
|
||||
}
|
||||
|
||||
let position = 0;
|
||||
let chunkIndex = 0;
|
||||
|
||||
while (position < processedContent.length) {
|
||||
const remainingLength = processedContent.length - position;
|
||||
|
||||
// If remaining content fits in one chunk, take it all
|
||||
if (remainingLength <= opts.maxChunkSize) {
|
||||
const chunkContent = processedContent.substring(position);
|
||||
const finalContent = opts.preserveCodeBlocks
|
||||
? restoreCodeBlocks(chunkContent, codeBlocks)
|
||||
: chunkContent;
|
||||
|
||||
chunks.push({
|
||||
content: finalContent.trim(),
|
||||
metadata: {
|
||||
chunkIndex,
|
||||
totalChunks: 0, // Will be updated after loop
|
||||
startChar: position,
|
||||
endChar: processedContent.length,
|
||||
tokenCount: estimateTokens(finalContent),
|
||||
},
|
||||
});
|
||||
break;
|
||||
}
|
||||
|
||||
// Find a good split point
|
||||
const idealEnd = position + opts.maxChunkSize;
|
||||
const actualEnd = findSplitPoint(processedContent, idealEnd);
|
||||
|
||||
const chunkContent = processedContent.substring(position, actualEnd);
|
||||
const finalContent = opts.preserveCodeBlocks
|
||||
? restoreCodeBlocks(chunkContent, codeBlocks)
|
||||
: chunkContent;
|
||||
|
||||
chunks.push({
|
||||
content: finalContent.trim(),
|
||||
metadata: {
|
||||
chunkIndex,
|
||||
totalChunks: 0, // Will be updated after loop
|
||||
startChar: position,
|
||||
endChar: actualEnd,
|
||||
tokenCount: estimateTokens(finalContent),
|
||||
},
|
||||
});
|
||||
|
||||
// Move position forward with overlap
|
||||
position = actualEnd - opts.chunkOverlap;
|
||||
chunkIndex++;
|
||||
}
|
||||
|
||||
// Update totalChunks in all metadata
|
||||
const totalChunks = chunks.length;
|
||||
chunks.forEach((chunk) => {
|
||||
chunk.metadata.totalChunks = totalChunks;
|
||||
});
|
||||
|
||||
return chunks;
|
||||
}
|
||||
|
||||
/**
|
||||
* Chunk multiple documents and return with source tracking
|
||||
*/
|
||||
export interface SourcedChunk extends DocumentChunk {
|
||||
sourceFilename: string;
|
||||
sourceMimeType?: string;
|
||||
}
|
||||
|
||||
export function chunkDocuments(
|
||||
documents: Array<{ filename: string; content: string; mimeType?: string }>,
|
||||
options: ChunkingOptions = {}
|
||||
): SourcedChunk[] {
|
||||
const allChunks: SourcedChunk[] = [];
|
||||
|
||||
documents.forEach((doc) => {
|
||||
const chunks = chunkDocument(doc.content, options);
|
||||
chunks.forEach((chunk) => {
|
||||
allChunks.push({
|
||||
...chunk,
|
||||
sourceFilename: doc.filename,
|
||||
sourceMimeType: doc.mimeType,
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
return allChunks;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user