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
12 changes: 8 additions & 4 deletions app/(chat)/api/chat/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,15 @@ import {
import { auth } from '@/app/(auth)/auth';
import { patternProvider } from '@/lib/ai/pattern-provider';
import { deleteChatById, getChatById } from '@/lib/db/queries';
import { generateUUID, getMostRecentUserMessage } from '@/lib/utils';
import {
extractErrorMessageOrDefault,
generateUUID,
getMostRecentUserMessage,
} from '@/lib/utils';

import { getOrCreateConversation } from '../../service';

export const maxDuration = 60;
export const maxDuration = 300;

export async function POST(request: Request) {
const { id, messages }: { id: string; messages: Array<Message> } =
Expand Down Expand Up @@ -70,8 +74,8 @@ export async function POST(request: Request) {
sendReasoning: true,
});
},
onError: () => {
return 'Oops, an error occured!';
onError: (error) => {
return extractErrorMessageOrDefault(error);
},
});
}
Expand Down
11 changes: 10 additions & 1 deletion lib/ai/pattern-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,19 @@ export class PatternModel implements LanguageModelV1 {
* TODO: Re-enable reasoning when backend supports it
* https://github.com/pattern-tech/pattern-app/issues/27
*/
} else if (
event.type === 'completion' ||
event.type === 'heartbeat'
) {
/**
* Ignore heartbeat and completion events. Completion event is
* identified automatically when the stream is ended, and the
* heartbeat event is only for keeping the connection alive
*/
} else {
controller.enqueue({
type: 'error',
error: `Event type is not supported`,
error: `Event type "${(event as unknown as { type: string }).type}" is not supported`,
});
}
});
Expand Down
16 changes: 15 additions & 1 deletion lib/ai/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,21 @@ export interface ToolStartEvent {
tool_input: Record<string, string>;
}

export type PatternStreamingResponseEvent = TokenEvent | ToolStartEvent;
export interface CompletionEvent {
type: 'completion';
data: 'Stream completed';
}

export interface HeartbeatEvent {
type: 'heartbeat';
data: 'still_processing';
}

export type PatternStreamingResponseEvent =
| TokenEvent
| ToolStartEvent
| CompletionEvent
| HeartbeatEvent;

export interface PatternProviderMetadata {
accessToken: string;
Expand Down