Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 74 additions & 15 deletions apps/snow-leopard/app/api/chat/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,14 @@ import {
} from '@/lib/utils';
import { generateTitleFromUserMessage } from '@/app/api/chat/actions/chat';
import { updateDocument } from '@/lib/ai/tools/update-document';
import { createDocument } from '@/lib/ai/tools/create-document';
import { isProductionEnvironment } from '@/lib/constants';
import { NextResponse } from 'next/server';
import { myProvider } from '@/lib/ai/providers';
import { auth } from "@/lib/auth";
import { headers } from 'next/headers';
import { ArtifactKind } from '@/components/artifact';
import type { Document } from '@snow-leopard/db';

export const maxDuration = 60;

Expand Down Expand Up @@ -261,11 +264,23 @@ export async function POST(request: Request) {
});

let validatedActiveDocumentId: string | undefined = undefined;
let activeDocumentKind: ArtifactKind | undefined = undefined;

if (activeDocumentId) {
const uuidRegex = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
if (uuidRegex.test(activeDocumentId)) {
validatedActiveDocumentId = activeDocumentId;
console.log(`[Chat API] Validated active document ID for tools: ${validatedActiveDocumentId}`);
try {
const activeDoc = await getDocumentById({ id: activeDocumentId });
if (activeDoc) {
validatedActiveDocumentId = activeDocumentId;
activeDocumentKind = activeDoc.kind as ArtifactKind;
console.log(`[Chat API] Validated active document ID: ${validatedActiveDocumentId}, Kind: ${activeDocumentKind}`);
} else {
console.warn(`[Chat API] Active document ${activeDocumentId} not found in DB.`);
}
} catch (error) {
console.error(`[Chat API] Error fetching active document ${activeDocumentId} for kind:`, error);
}
} else {
console.warn(`[Chat API] Invalid active document ID format, not passing to tools: ${activeDocumentId}`);
}
Expand All @@ -274,26 +289,70 @@ export async function POST(request: Request) {
console.log(`[Chat API POST] Calling streamText with model: ${selectedChatModel}`);

return createDataStreamResponse({
execute: (dataStream) => {
execute: async (dataStream) => {
// --- Build tools dynamically (Re-refined) ---
const availableTools: any = {};
const activeToolsList: string[] = [];
const MIN_CONTENT_LENGTH_FOR_UPDATE = 5; // Threshold

// Only add tools if there IS an active document context
if (validatedActiveDocumentId) {
let activeDoc: Document | null = null;
let currentKind: ArtifactKind | undefined = undefined;

// Fetch the active document within execute
try {
activeDoc = await getDocumentById({ id: validatedActiveDocumentId });
if (activeDoc) {
currentKind = activeDoc.kind as ArtifactKind;
} else {
console.warn(`[Chat API Execute] Active document ${validatedActiveDocumentId} not found when checking for tool selection.`);
}
} catch (error) {
console.error('[Chat API Execute] Error fetching active document for tool selection:', error);
}

// Now, decide which tool to offer based on content
if (activeDoc && currentKind) { // Ensure we found the doc and its kind
const hasContent = activeDoc.content && activeDoc.content.length >= MIN_CONTENT_LENGTH_FOR_UPDATE;

if (hasContent) {
// Document has content -> Offer ONLY updateDocument
availableTools.updateDocument = updateDocument({
session: toolSession,
documentId: validatedActiveDocumentId,
});
activeToolsList.push('updateDocument');
console.log('[Chat API] Active document has content. Offering ONLY updateDocument tool.');
} else {
// Document is empty/minimal -> Offer ONLY createDocument
availableTools.createDocument = createDocument({
session: toolSession,
dataStream,
// No documentId/Kind needed for the tool itself anymore
});
activeToolsList.push('createDocument');
console.log('[Chat API] Active document is empty/minimal. Offering ONLY createDocument tool.');
}
} else {
console.log(`[Chat API] Could not find active document details (${validatedActiveDocumentId}) or kind, no document tools added.`);
}

} else {
console.log('[Chat API] No active document ID, no document tools added.');
}
// --- End Build tools ---

const result = streamText({
model: myProvider.languageModel(selectedChatModel),
system: enhancedSystemPrompt,
messages,
maxSteps: 5,
experimental_activeTools: validatedActiveDocumentId
? ['updateDocument']
: [],
experimental_transform: smoothStream({ chunking: 'word' }),
experimental_activeTools: activeToolsList,
experimental_transform: smoothStream({ chunking: 'line' }),
experimental_generateMessageId: generateUUID,
tools: {
updateDocument: updateDocument({
session: toolSession,
documentId: validatedActiveDocumentId
}),
},
tools: availableTools,
onFinish: async ({ response, reasoning }) => {
console.log('[Chat API POST onFinish] Full response object:', JSON.stringify(response, null, 2));

if (userId) {
try {
const sanitizedResponseMessages = sanitizeResponseMessages({
Expand Down
8 changes: 4 additions & 4 deletions apps/snow-leopard/artifacts/text/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import { updateDocumentPrompt } from '@/lib/ai/prompts';
export const textDocumentHandler = createDocumentHandler<'text'>({
kind: 'text',
onCreateDocument: async ({ title, dataStream }) => {
let draftContent = '';
// No need to track draft content - we're just streaming to the client
// Editor's auto-save will handle saving later

const { fullStream } = streamText({
model: myProvider.languageModel('artifact-model'),
Expand All @@ -22,16 +23,15 @@ export const textDocumentHandler = createDocumentHandler<'text'>({
if (type === 'text-delta') {
const { textDelta } = delta;

draftContent += textDelta;

// Stream the content - no need to accumulate it
dataStream.writeData({
type: 'text-delta',
content: textDelta,
});
}
}

return draftContent;
// No return value needed
},
onUpdateDocument: async ({ document, description, dataStream }) => {
let draftContent = '';
Expand Down
2 changes: 1 addition & 1 deletion apps/snow-leopard/components/chat/chat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export function Chat({
const [confirmedMentions, setConfirmedMentions] = useState<MentionedDocument[]>([]);

useEffect(() => {
const hasDocumentContext = documentId !== 'init' && documentContent;
const hasDocumentContext = documentId !== 'init';
setDocumentContextActive(Boolean(hasDocumentContext));

if (hasDocumentContext) {
Expand Down
47 changes: 19 additions & 28 deletions apps/snow-leopard/components/data-stream-handler.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ export type DataStreamDelta = {
| 'clear'
| 'finish'
| 'kind'
| 'artifactUpdate';
| 'artifactUpdate'
| 'force-save';
content: string;
};

Expand Down Expand Up @@ -57,34 +58,29 @@ export function DataStreamHandler({ id }: { id: string }) {

switch (delta.type) {
case 'id':
console.log(`[DataStreamHandler] Received ID delta: ${delta.content}. Updating artifact state.`);
return {
...draftArtifact,
documentId: delta.content as string,
status: 'streaming',
documentId: delta.content,
};

case 'title':
return {
...draftArtifact,
title: delta.content as string,
status: 'streaming',
};

case 'kind':
return {
...draftArtifact,
kind: delta.content as ArtifactKind,
status: 'streaming',
};
case 'force-save':
if (draftArtifact.documentId && draftArtifact.documentId !== 'init') {
if (typeof window !== 'undefined') {
window.dispatchEvent(new CustomEvent('editor:force-save-document', {
detail: { documentId: draftArtifact.documentId }
}));
}
}
return draftArtifact;

case 'clear':
return {
...draftArtifact,
content: '',
status: 'streaming',
};

case 'finish':
if (draftArtifact.status === 'streaming' && draftArtifact.documentId !== 'init') {
console.log(`[DataStreamHandler] Dispatching creation-stream-finished for ${draftArtifact.documentId}`);
window.dispatchEvent(new CustomEvent('editor:creation-stream-finished', {
detail: { documentId: draftArtifact.documentId }
}));
}
return {
...draftArtifact,
status: 'idle',
Expand All @@ -95,11 +91,9 @@ export function DataStreamHandler({ id }: { id: string }) {
const updateData = JSON.parse(delta.content as string);
console.log('[DataStreamHandler] Received artifactUpdate, dispatching to editor:', updateData);

// Verify running client-side before dispatch
if (typeof window !== 'undefined') {
console.log('[DataStreamHandler] Window context confirmed. Attempting to dispatch editor:stream-data...');
try {
// Dispatch the data directly for the editor to consume
window.dispatchEvent(new CustomEvent('editor:stream-data', {
detail: { type: 'artifactUpdate', content: delta.content }
}));
Expand All @@ -113,12 +107,9 @@ export function DataStreamHandler({ id }: { id: string }) {
} catch (error) {
console.error('[DataStreamHandler] Error parsing artifactUpdate content:', error);
}
// No state change needed here, just dispatching the event
return draftArtifact;

default:
// Handle text-delta, code-delta etc. via artifactDefinition.onStreamPart
// If no handler handles it, just return current state
return draftArtifact;
}
});
Expand Down
Loading