mirror of
https://github.com/stackblitz/bolt.new
synced 2025-03-12 06:51:11 +00:00
Added sanitization for user messages.
Use regex defined in constants.ts instead of redefining.
This commit is contained in:
parent
074e2f3016
commit
2a362b9e0b
@ -1,7 +1,7 @@
|
|||||||
// @ts-nocheck
|
// @ts-nocheck
|
||||||
// Preventing TS checks with files presented in the video for a better presentation.
|
// Preventing TS checks with files presented in the video for a better presentation.
|
||||||
import { modificationsRegex } from '~/utils/diff';
|
import { modificationsRegex } from '~/utils/diff';
|
||||||
import { MODEL_REGEX } from '~/utils/constants';
|
import { MODEL_REGEX, PROVIDER_REGEX } from '~/utils/constants';
|
||||||
import { Markdown } from './Markdown';
|
import { Markdown } from './Markdown';
|
||||||
|
|
||||||
interface UserMessageProps {
|
interface UserMessageProps {
|
||||||
@ -17,5 +17,5 @@ export function UserMessage({ content }: UserMessageProps) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function sanitizeUserMessage(content: string) {
|
function sanitizeUserMessage(content: string) {
|
||||||
return content.replace(modificationsRegex, '').replace(MODEL_REGEX, '').trim();
|
return content.replace(modificationsRegex, '').replace(MODEL_REGEX, '').replace(PROVIDER_REGEX, '').trim();
|
||||||
}
|
}
|
||||||
|
@ -4,7 +4,7 @@ import { streamText as _streamText, convertToCoreMessages } from 'ai';
|
|||||||
import { getModel } from '~/lib/.server/llm/model';
|
import { getModel } from '~/lib/.server/llm/model';
|
||||||
import { MAX_TOKENS } from './constants';
|
import { MAX_TOKENS } from './constants';
|
||||||
import { getSystemPrompt } from './prompts';
|
import { getSystemPrompt } from './prompts';
|
||||||
import { MODEL_LIST, DEFAULT_MODEL, DEFAULT_PROVIDER } from '~/utils/constants';
|
import { MODEL_LIST, DEFAULT_MODEL, DEFAULT_PROVIDER, MODEL_REGEX, PROVIDER_REGEX } from '~/utils/constants';
|
||||||
|
|
||||||
interface ToolResult<Name extends string, Args, Result> {
|
interface ToolResult<Name extends string, Args, Result> {
|
||||||
toolCallId: string;
|
toolCallId: string;
|
||||||
@ -25,21 +25,18 @@ export type Messages = Message[];
|
|||||||
export type StreamingOptions = Omit<Parameters<typeof _streamText>[0], 'model'>;
|
export type StreamingOptions = Omit<Parameters<typeof _streamText>[0], 'model'>;
|
||||||
|
|
||||||
function extractPropertiesFromMessage(message: Message): { model: string; provider: string; content: string } {
|
function extractPropertiesFromMessage(message: Message): { model: string; provider: string; content: string } {
|
||||||
const modelRegex = /^\[Model: (.*?)\]\n\n/;
|
|
||||||
const providerRegex = /\[Provider: (.*?)\]\n\n/;
|
|
||||||
|
|
||||||
// Extract model
|
// Extract model
|
||||||
const modelMatch = message.content.match(modelRegex);
|
const modelMatch = message.content.match(MODEL_REGEX);
|
||||||
const model = modelMatch ? modelMatch[1] : DEFAULT_MODEL;
|
const model = modelMatch ? modelMatch[1] : DEFAULT_MODEL;
|
||||||
|
|
||||||
// Extract provider
|
// Extract provider
|
||||||
const providerMatch = message.content.match(providerRegex);
|
const providerMatch = message.content.match(PROVIDER_REGEX);
|
||||||
const provider = providerMatch ? providerMatch[1] : DEFAULT_PROVIDER;
|
const provider = providerMatch ? providerMatch[1] : DEFAULT_PROVIDER;
|
||||||
|
|
||||||
// Remove model and provider lines from content
|
// Remove model and provider lines from content
|
||||||
const cleanedContent = message.content
|
const cleanedContent = message.content
|
||||||
.replace(modelRegex, '')
|
.replace(MODEL_REGEX, '')
|
||||||
.replace(providerRegex, '')
|
.replace(PROVIDER_REGEX, '')
|
||||||
.trim();
|
.trim();
|
||||||
|
|
||||||
return { model, provider, content: cleanedContent };
|
return { model, provider, content: cleanedContent };
|
||||||
|
@ -4,6 +4,7 @@ export const WORK_DIR_NAME = 'project';
|
|||||||
export const WORK_DIR = `/home/${WORK_DIR_NAME}`;
|
export const WORK_DIR = `/home/${WORK_DIR_NAME}`;
|
||||||
export const MODIFICATIONS_TAG_NAME = 'bolt_file_modifications';
|
export const MODIFICATIONS_TAG_NAME = 'bolt_file_modifications';
|
||||||
export const MODEL_REGEX = /^\[Model: (.*?)\]\n\n/;
|
export const MODEL_REGEX = /^\[Model: (.*?)\]\n\n/;
|
||||||
|
export const PROVIDER_REGEX = /\[Provider: (.*?)\]\n\n/;
|
||||||
export const DEFAULT_MODEL = 'claude-3-5-sonnet-20240620';
|
export const DEFAULT_MODEL = 'claude-3-5-sonnet-20240620';
|
||||||
export const DEFAULT_PROVIDER = 'Anthropic';
|
export const DEFAULT_PROVIDER = 'Anthropic';
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user