feat: enhance context handling by adding code context selection and implementing summary generation (#1091) #release
* feat: add context annotation types and enhance file handling in LLM processing * feat: enhance context handling by adding chatId to annotations and implementing summary generation * removed useless changes * feat: updated token counts to include optimization requests * prompt fix * logging added * useless logs removed
This commit is contained in:
@@ -3,3 +3,36 @@ export const MAX_TOKENS = 8000;
|
||||
|
||||
// limits the number of model responses that can be returned in a single request
|
||||
export const MAX_RESPONSE_SEGMENTS = 2;
|
||||
|
||||
export interface File {
|
||||
type: 'file';
|
||||
content: string;
|
||||
isBinary: boolean;
|
||||
}
|
||||
|
||||
export interface Folder {
|
||||
type: 'folder';
|
||||
}
|
||||
|
||||
type Dirent = File | Folder;
|
||||
|
||||
export type FileMap = Record<string, Dirent | undefined>;
|
||||
|
||||
export const IGNORE_PATTERNS = [
|
||||
'node_modules/**',
|
||||
'.git/**',
|
||||
'dist/**',
|
||||
'build/**',
|
||||
'.next/**',
|
||||
'coverage/**',
|
||||
'.cache/**',
|
||||
'.vscode/**',
|
||||
'.idea/**',
|
||||
'**/*.log',
|
||||
'**/.DS_Store',
|
||||
'**/npm-debug.log*',
|
||||
'**/yarn-debug.log*',
|
||||
'**/yarn-error.log*',
|
||||
'**/*lock.json',
|
||||
'**/*lock.yml',
|
||||
];
|
||||
|
||||
138
app/lib/.server/llm/create-summary.ts
Normal file
138
app/lib/.server/llm/create-summary.ts
Normal file
@@ -0,0 +1,138 @@
|
||||
import { generateText, type CoreTool, type GenerateTextResult, type Message } from 'ai';
|
||||
import type { IProviderSetting } from '~/types/model';
|
||||
import { DEFAULT_MODEL, DEFAULT_PROVIDER, PROVIDER_LIST } from '~/utils/constants';
|
||||
import { extractCurrentContext, extractPropertiesFromMessage, simplifyBoltActions } from './utils';
|
||||
import { createScopedLogger } from '~/utils/logger';
|
||||
import { LLMManager } from '~/lib/modules/llm/manager';
|
||||
|
||||
const logger = createScopedLogger('create-summary');
|
||||
|
||||
export async function createSummary(props: {
|
||||
messages: Message[];
|
||||
env?: Env;
|
||||
apiKeys?: Record<string, string>;
|
||||
providerSettings?: Record<string, IProviderSetting>;
|
||||
promptId?: string;
|
||||
contextOptimization?: boolean;
|
||||
onFinish?: (resp: GenerateTextResult<Record<string, CoreTool<any, any>>, never>) => void;
|
||||
}) {
|
||||
const { messages, env: serverEnv, apiKeys, providerSettings, contextOptimization, onFinish } = props;
|
||||
let currentModel = DEFAULT_MODEL;
|
||||
let currentProvider = DEFAULT_PROVIDER.name;
|
||||
const processedMessages = messages.map((message) => {
|
||||
if (message.role === 'user') {
|
||||
const { model, provider, content } = extractPropertiesFromMessage(message);
|
||||
currentModel = model;
|
||||
currentProvider = provider;
|
||||
|
||||
return { ...message, content };
|
||||
} else if (message.role == 'assistant') {
|
||||
let content = message.content;
|
||||
|
||||
if (contextOptimization) {
|
||||
content = simplifyBoltActions(content);
|
||||
}
|
||||
|
||||
return { ...message, content };
|
||||
}
|
||||
|
||||
return message;
|
||||
});
|
||||
|
||||
const provider = PROVIDER_LIST.find((p) => p.name === currentProvider) || DEFAULT_PROVIDER;
|
||||
const staticModels = LLMManager.getInstance().getStaticModelListFromProvider(provider);
|
||||
let modelDetails = staticModels.find((m) => m.name === currentModel);
|
||||
|
||||
if (!modelDetails) {
|
||||
const modelsList = [
|
||||
...(provider.staticModels || []),
|
||||
...(await LLMManager.getInstance().getModelListFromProvider(provider, {
|
||||
apiKeys,
|
||||
providerSettings,
|
||||
serverEnv: serverEnv as any,
|
||||
})),
|
||||
];
|
||||
|
||||
if (!modelsList.length) {
|
||||
throw new Error(`No models found for provider ${provider.name}`);
|
||||
}
|
||||
|
||||
modelDetails = modelsList.find((m) => m.name === currentModel);
|
||||
|
||||
if (!modelDetails) {
|
||||
// Fallback to first model
|
||||
logger.warn(
|
||||
`MODEL [${currentModel}] not found in provider [${provider.name}]. Falling back to first model. ${modelsList[0].name}`,
|
||||
);
|
||||
modelDetails = modelsList[0];
|
||||
}
|
||||
}
|
||||
|
||||
let slicedMessages = processedMessages;
|
||||
const { summary } = extractCurrentContext(processedMessages);
|
||||
let summaryText: string | undefined = undefined;
|
||||
let chatId: string | undefined = undefined;
|
||||
|
||||
if (summary && summary.type === 'chatSummary') {
|
||||
chatId = summary.chatId;
|
||||
summaryText = `Below is the Chat Summary till now, this is chat summary before the conversation provided by the user
|
||||
you should also use this as historical message while providing the response to the user.
|
||||
${summary.summary}`;
|
||||
|
||||
if (chatId) {
|
||||
let index = 0;
|
||||
|
||||
for (let i = 0; i < processedMessages.length; i++) {
|
||||
if (processedMessages[i].id === chatId) {
|
||||
index = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
slicedMessages = processedMessages.slice(index + 1);
|
||||
}
|
||||
}
|
||||
|
||||
const extractTextContent = (message: Message) =>
|
||||
Array.isArray(message.content)
|
||||
? (message.content.find((item) => item.type === 'text')?.text as string) || ''
|
||||
: message.content;
|
||||
|
||||
// select files from the list of code file from the project that might be useful for the current request from the user
|
||||
const resp = await generateText({
|
||||
system: `
|
||||
You are a software engineer. You are working on a project. tou need to summarize the work till now and provide a summary of the chat till now.
|
||||
|
||||
${summaryText}
|
||||
|
||||
RULES:
|
||||
* Only provide the summary of the chat till now.
|
||||
* Do not provide any new information.
|
||||
`,
|
||||
prompt: `
|
||||
please provide a summary of the chat till now.
|
||||
below is the latest chat:
|
||||
|
||||
---
|
||||
${slicedMessages
|
||||
.map((x) => {
|
||||
return `---\n[${x.role}] ${extractTextContent(x)}\n---`;
|
||||
})
|
||||
.join('\n')}
|
||||
---
|
||||
`,
|
||||
model: provider.getModelInstance({
|
||||
model: currentModel,
|
||||
serverEnv,
|
||||
apiKeys,
|
||||
providerSettings,
|
||||
}),
|
||||
});
|
||||
|
||||
const response = resp.text;
|
||||
|
||||
if (onFinish) {
|
||||
onFinish(resp);
|
||||
}
|
||||
|
||||
return response;
|
||||
}
|
||||
233
app/lib/.server/llm/select-context.ts
Normal file
233
app/lib/.server/llm/select-context.ts
Normal file
@@ -0,0 +1,233 @@
|
||||
import { generateText, type CoreTool, type GenerateTextResult, type Message } from 'ai';
|
||||
import ignore from 'ignore';
|
||||
import type { IProviderSetting } from '~/types/model';
|
||||
import { IGNORE_PATTERNS, type FileMap } from './constants';
|
||||
import { DEFAULT_MODEL, DEFAULT_PROVIDER, PROVIDER_LIST } from '~/utils/constants';
|
||||
import { createFilesContext, extractCurrentContext, extractPropertiesFromMessage, simplifyBoltActions } from './utils';
|
||||
import { createScopedLogger } from '~/utils/logger';
|
||||
import { LLMManager } from '~/lib/modules/llm/manager';
|
||||
|
||||
// Common patterns to ignore, similar to .gitignore
|
||||
|
||||
const ig = ignore().add(IGNORE_PATTERNS);
|
||||
const logger = createScopedLogger('select-context');
|
||||
|
||||
export async function selectContext(props: {
|
||||
messages: Message[];
|
||||
env?: Env;
|
||||
apiKeys?: Record<string, string>;
|
||||
files: FileMap;
|
||||
providerSettings?: Record<string, IProviderSetting>;
|
||||
promptId?: string;
|
||||
contextOptimization?: boolean;
|
||||
summary: string;
|
||||
onFinish?: (resp: GenerateTextResult<Record<string, CoreTool<any, any>>, never>) => void;
|
||||
}) {
|
||||
const { messages, env: serverEnv, apiKeys, files, providerSettings, contextOptimization, summary, onFinish } = props;
|
||||
let currentModel = DEFAULT_MODEL;
|
||||
let currentProvider = DEFAULT_PROVIDER.name;
|
||||
const processedMessages = messages.map((message) => {
|
||||
if (message.role === 'user') {
|
||||
const { model, provider, content } = extractPropertiesFromMessage(message);
|
||||
currentModel = model;
|
||||
currentProvider = provider;
|
||||
|
||||
return { ...message, content };
|
||||
} else if (message.role == 'assistant') {
|
||||
let content = message.content;
|
||||
|
||||
if (contextOptimization) {
|
||||
content = simplifyBoltActions(content);
|
||||
}
|
||||
|
||||
return { ...message, content };
|
||||
}
|
||||
|
||||
return message;
|
||||
});
|
||||
|
||||
const provider = PROVIDER_LIST.find((p) => p.name === currentProvider) || DEFAULT_PROVIDER;
|
||||
const staticModels = LLMManager.getInstance().getStaticModelListFromProvider(provider);
|
||||
let modelDetails = staticModels.find((m) => m.name === currentModel);
|
||||
|
||||
if (!modelDetails) {
|
||||
const modelsList = [
|
||||
...(provider.staticModels || []),
|
||||
...(await LLMManager.getInstance().getModelListFromProvider(provider, {
|
||||
apiKeys,
|
||||
providerSettings,
|
||||
serverEnv: serverEnv as any,
|
||||
})),
|
||||
];
|
||||
|
||||
if (!modelsList.length) {
|
||||
throw new Error(`No models found for provider ${provider.name}`);
|
||||
}
|
||||
|
||||
modelDetails = modelsList.find((m) => m.name === currentModel);
|
||||
|
||||
if (!modelDetails) {
|
||||
// Fallback to first model
|
||||
logger.warn(
|
||||
`MODEL [${currentModel}] not found in provider [${provider.name}]. Falling back to first model. ${modelsList[0].name}`,
|
||||
);
|
||||
modelDetails = modelsList[0];
|
||||
}
|
||||
}
|
||||
|
||||
const { codeContext } = extractCurrentContext(processedMessages);
|
||||
|
||||
let filePaths = getFilePaths(files || {});
|
||||
filePaths = filePaths.filter((x) => {
|
||||
const relPath = x.replace('/home/project/', '');
|
||||
return !ig.ignores(relPath);
|
||||
});
|
||||
|
||||
let context = '';
|
||||
const currrentFiles: string[] = [];
|
||||
const contextFiles: FileMap = {};
|
||||
|
||||
if (codeContext?.type === 'codeContext') {
|
||||
const codeContextFiles: string[] = codeContext.files;
|
||||
Object.keys(files || {}).forEach((path) => {
|
||||
let relativePath = path;
|
||||
|
||||
if (path.startsWith('/home/project/')) {
|
||||
relativePath = path.replace('/home/project/', '');
|
||||
}
|
||||
|
||||
if (codeContextFiles.includes(relativePath)) {
|
||||
contextFiles[relativePath] = files[path];
|
||||
currrentFiles.push(relativePath);
|
||||
}
|
||||
});
|
||||
context = createFilesContext(contextFiles);
|
||||
}
|
||||
|
||||
const summaryText = `Here is the summary of the chat till now: ${summary}`;
|
||||
|
||||
const extractTextContent = (message: Message) =>
|
||||
Array.isArray(message.content)
|
||||
? (message.content.find((item) => item.type === 'text')?.text as string) || ''
|
||||
: message.content;
|
||||
|
||||
const lastUserMessage = processedMessages.filter((x) => x.role == 'user').pop();
|
||||
|
||||
if (!lastUserMessage) {
|
||||
throw new Error('No user message found');
|
||||
}
|
||||
|
||||
// select files from the list of code file from the project that might be useful for the current request from the user
|
||||
const resp = await generateText({
|
||||
system: `
|
||||
You are a software engineer. You are working on a project. You have access to the following files:
|
||||
|
||||
AVAILABLE FILES PATHS
|
||||
---
|
||||
${filePaths.map((path) => `- ${path}`).join('\n')}
|
||||
---
|
||||
|
||||
You have following code loaded in the context buffer that you can refer to:
|
||||
|
||||
CURRENT CONTEXT BUFFER
|
||||
---
|
||||
${context}
|
||||
---
|
||||
|
||||
Now, you are given a task. You need to select the files that are relevant to the task from the list of files above.
|
||||
|
||||
RESPONSE FORMAT:
|
||||
your response shoudl be in following format:
|
||||
---
|
||||
<updateContextBuffer>
|
||||
<includeFile path="path/to/file"/>
|
||||
<excludeFile path="path/to/file"/>
|
||||
</updateContextBuffer>
|
||||
---
|
||||
* Your should start with <updateContextBuffer> and end with </updateContextBuffer>.
|
||||
* You can include multiple <includeFile> and <excludeFile> tags in the response.
|
||||
* You should not include any other text in the response.
|
||||
* You should not include any file that is not in the list of files above.
|
||||
* You should not include any file that is already in the context buffer.
|
||||
* If no changes are needed, you can leave the response empty updateContextBuffer tag.
|
||||
`,
|
||||
prompt: `
|
||||
${summaryText}
|
||||
|
||||
Users Question: ${extractTextContent(lastUserMessage)}
|
||||
|
||||
update the context buffer with the files that are relevant to the task from the list of files above.
|
||||
|
||||
CRITICAL RULES:
|
||||
* Only include relevant files in the context buffer.
|
||||
* context buffer should not include any file that is not in the list of files above.
|
||||
* context buffer is extremlly expensive, so only include files that are absolutely necessary.
|
||||
* If no changes are needed, you can leave the response empty updateContextBuffer tag.
|
||||
* Only 5 files can be placed in the context buffer at a time.
|
||||
* if the buffer is full, you need to exclude files that is not needed and include files that is relevent.
|
||||
|
||||
`,
|
||||
model: provider.getModelInstance({
|
||||
model: currentModel,
|
||||
serverEnv,
|
||||
apiKeys,
|
||||
providerSettings,
|
||||
}),
|
||||
});
|
||||
|
||||
const response = resp.text;
|
||||
const updateContextBuffer = response.match(/<updateContextBuffer>([\s\S]*?)<\/updateContextBuffer>/);
|
||||
|
||||
if (!updateContextBuffer) {
|
||||
throw new Error('Invalid response. Please follow the response format');
|
||||
}
|
||||
|
||||
const includeFiles =
|
||||
updateContextBuffer[1]
|
||||
.match(/<includeFile path="(.*?)"/gm)
|
||||
?.map((x) => x.replace('<includeFile path="', '').replace('"', '')) || [];
|
||||
const excludeFiles =
|
||||
updateContextBuffer[1]
|
||||
.match(/<excludeFile path="(.*?)"/gm)
|
||||
?.map((x) => x.replace('<excludeFile path="', '').replace('"', '')) || [];
|
||||
|
||||
const filteredFiles: FileMap = {};
|
||||
excludeFiles.forEach((path) => {
|
||||
delete contextFiles[path];
|
||||
});
|
||||
includeFiles.forEach((path) => {
|
||||
let fullPath = path;
|
||||
|
||||
if (!path.startsWith('/home/project/')) {
|
||||
fullPath = `/home/project/${path}`;
|
||||
}
|
||||
|
||||
if (!filePaths.includes(fullPath)) {
|
||||
throw new Error(`File ${path} is not in the list of files above.`);
|
||||
}
|
||||
|
||||
if (currrentFiles.includes(path)) {
|
||||
return;
|
||||
}
|
||||
|
||||
filteredFiles[path] = files[fullPath];
|
||||
});
|
||||
|
||||
if (onFinish) {
|
||||
onFinish(resp);
|
||||
}
|
||||
|
||||
return filteredFiles;
|
||||
|
||||
// generateText({
|
||||
}
|
||||
|
||||
export function getFilePaths(files: FileMap) {
|
||||
let filePaths = Object.keys(files);
|
||||
filePaths = filePaths.filter((x) => {
|
||||
const relPath = x.replace('/home/project/', '');
|
||||
return !ig.ignores(relPath);
|
||||
});
|
||||
|
||||
return filePaths;
|
||||
}
|
||||
@@ -1,162 +1,48 @@
|
||||
import { convertToCoreMessages, streamText as _streamText } from 'ai';
|
||||
import { MAX_TOKENS } from './constants';
|
||||
import { convertToCoreMessages, streamText as _streamText, type Message } from 'ai';
|
||||
import { MAX_TOKENS, type FileMap } from './constants';
|
||||
import { getSystemPrompt } from '~/lib/common/prompts/prompts';
|
||||
import {
|
||||
DEFAULT_MODEL,
|
||||
DEFAULT_PROVIDER,
|
||||
MODEL_REGEX,
|
||||
MODIFICATIONS_TAG_NAME,
|
||||
PROVIDER_LIST,
|
||||
PROVIDER_REGEX,
|
||||
WORK_DIR,
|
||||
} from '~/utils/constants';
|
||||
import ignore from 'ignore';
|
||||
import { DEFAULT_MODEL, DEFAULT_PROVIDER, MODIFICATIONS_TAG_NAME, PROVIDER_LIST, WORK_DIR } from '~/utils/constants';
|
||||
import type { IProviderSetting } from '~/types/model';
|
||||
import { PromptLibrary } from '~/lib/common/prompt-library';
|
||||
import { allowedHTMLElements } from '~/utils/markdown';
|
||||
import { LLMManager } from '~/lib/modules/llm/manager';
|
||||
import { createScopedLogger } from '~/utils/logger';
|
||||
|
||||
interface ToolResult<Name extends string, Args, Result> {
|
||||
toolCallId: string;
|
||||
toolName: Name;
|
||||
args: Args;
|
||||
result: Result;
|
||||
}
|
||||
|
||||
interface Message {
|
||||
role: 'user' | 'assistant';
|
||||
content: string;
|
||||
toolInvocations?: ToolResult<string, unknown, unknown>[];
|
||||
model?: string;
|
||||
}
|
||||
import { createFilesContext, extractPropertiesFromMessage, simplifyBoltActions } from './utils';
|
||||
import { getFilePaths } from './select-context';
|
||||
|
||||
export type Messages = Message[];
|
||||
|
||||
export type StreamingOptions = Omit<Parameters<typeof _streamText>[0], 'model'>;
|
||||
|
||||
export interface File {
|
||||
type: 'file';
|
||||
content: string;
|
||||
isBinary: boolean;
|
||||
}
|
||||
|
||||
export interface Folder {
|
||||
type: 'folder';
|
||||
}
|
||||
|
||||
type Dirent = File | Folder;
|
||||
|
||||
export type FileMap = Record<string, Dirent | undefined>;
|
||||
|
||||
export function simplifyBoltActions(input: string): string {
|
||||
// Using regex to match boltAction tags that have type="file"
|
||||
const regex = /(<boltAction[^>]*type="file"[^>]*>)([\s\S]*?)(<\/boltAction>)/g;
|
||||
|
||||
// Replace each matching occurrence
|
||||
return input.replace(regex, (_0, openingTag, _2, closingTag) => {
|
||||
return `${openingTag}\n ...\n ${closingTag}`;
|
||||
});
|
||||
}
|
||||
|
||||
// Common patterns to ignore, similar to .gitignore
|
||||
const IGNORE_PATTERNS = [
|
||||
'node_modules/**',
|
||||
'.git/**',
|
||||
'dist/**',
|
||||
'build/**',
|
||||
'.next/**',
|
||||
'coverage/**',
|
||||
'.cache/**',
|
||||
'.vscode/**',
|
||||
'.idea/**',
|
||||
'**/*.log',
|
||||
'**/.DS_Store',
|
||||
'**/npm-debug.log*',
|
||||
'**/yarn-debug.log*',
|
||||
'**/yarn-error.log*',
|
||||
'**/*lock.json',
|
||||
'**/*lock.yml',
|
||||
];
|
||||
const ig = ignore().add(IGNORE_PATTERNS);
|
||||
|
||||
function createFilesContext(files: FileMap) {
|
||||
let filePaths = Object.keys(files);
|
||||
filePaths = filePaths.filter((x) => {
|
||||
const relPath = x.replace('/home/project/', '');
|
||||
return !ig.ignores(relPath);
|
||||
});
|
||||
|
||||
const fileContexts = filePaths
|
||||
.filter((x) => files[x] && files[x].type == 'file')
|
||||
.map((path) => {
|
||||
const dirent = files[path];
|
||||
|
||||
if (!dirent || dirent.type == 'folder') {
|
||||
return '';
|
||||
}
|
||||
|
||||
return `<file path="${path}">\n${dirent.content}\n</file>`;
|
||||
});
|
||||
|
||||
return `Below are the code files present in the webcontainer:\n <codebase>\n${fileContexts.join('\n\n')}\n</codebase>`;
|
||||
}
|
||||
|
||||
function extractPropertiesFromMessage(message: Message): { model: string; provider: string; content: string } {
|
||||
const textContent = Array.isArray(message.content)
|
||||
? message.content.find((item) => item.type === 'text')?.text || ''
|
||||
: message.content;
|
||||
|
||||
const modelMatch = textContent.match(MODEL_REGEX);
|
||||
const providerMatch = textContent.match(PROVIDER_REGEX);
|
||||
|
||||
/*
|
||||
* Extract model
|
||||
* const modelMatch = message.content.match(MODEL_REGEX);
|
||||
*/
|
||||
const model = modelMatch ? modelMatch[1] : DEFAULT_MODEL;
|
||||
|
||||
/*
|
||||
* Extract provider
|
||||
* const providerMatch = message.content.match(PROVIDER_REGEX);
|
||||
*/
|
||||
const provider = providerMatch ? providerMatch[1] : DEFAULT_PROVIDER.name;
|
||||
|
||||
const cleanedContent = Array.isArray(message.content)
|
||||
? message.content.map((item) => {
|
||||
if (item.type === 'text') {
|
||||
return {
|
||||
type: 'text',
|
||||
text: item.text?.replace(MODEL_REGEX, '').replace(PROVIDER_REGEX, ''),
|
||||
};
|
||||
}
|
||||
|
||||
return item; // Preserve image_url and other types as is
|
||||
})
|
||||
: textContent.replace(MODEL_REGEX, '').replace(PROVIDER_REGEX, '');
|
||||
|
||||
return { model, provider, content: cleanedContent };
|
||||
}
|
||||
|
||||
const logger = createScopedLogger('stream-text');
|
||||
|
||||
export async function streamText(props: {
|
||||
messages: Messages;
|
||||
env: Env;
|
||||
messages: Omit<Message, 'id'>[];
|
||||
env?: Env;
|
||||
options?: StreamingOptions;
|
||||
apiKeys?: Record<string, string>;
|
||||
files?: FileMap;
|
||||
providerSettings?: Record<string, IProviderSetting>;
|
||||
promptId?: string;
|
||||
contextOptimization?: boolean;
|
||||
contextFiles?: FileMap;
|
||||
summary?: string;
|
||||
}) {
|
||||
const { messages, env: serverEnv, options, apiKeys, files, providerSettings, promptId, contextOptimization } = props;
|
||||
|
||||
// console.log({serverEnv});
|
||||
|
||||
const {
|
||||
messages,
|
||||
env: serverEnv,
|
||||
options,
|
||||
apiKeys,
|
||||
files,
|
||||
providerSettings,
|
||||
promptId,
|
||||
contextOptimization,
|
||||
contextFiles,
|
||||
summary,
|
||||
} = props;
|
||||
let currentModel = DEFAULT_MODEL;
|
||||
let currentProvider = DEFAULT_PROVIDER.name;
|
||||
const processedMessages = messages.map((message) => {
|
||||
let processedMessages = messages.map((message) => {
|
||||
if (message.role === 'user') {
|
||||
const { model, provider, content } = extractPropertiesFromMessage(message);
|
||||
currentModel = model;
|
||||
@@ -214,13 +100,44 @@ export async function streamText(props: {
|
||||
modificationTagName: MODIFICATIONS_TAG_NAME,
|
||||
}) ?? getSystemPrompt();
|
||||
|
||||
if (files && contextOptimization) {
|
||||
const codeContext = createFilesContext(files);
|
||||
systemPrompt = `${systemPrompt}\n\n ${codeContext}`;
|
||||
if (files && contextFiles && contextOptimization) {
|
||||
const codeContext = createFilesContext(contextFiles, true);
|
||||
const filePaths = getFilePaths(files);
|
||||
|
||||
systemPrompt = `${systemPrompt}
|
||||
Below are all the files present in the project:
|
||||
---
|
||||
${filePaths.join('\n')}
|
||||
---
|
||||
|
||||
Below is the context loaded into context buffer for you to have knowledge of and might need changes to fullfill current user request.
|
||||
CONTEXT BUFFER:
|
||||
---
|
||||
${codeContext}
|
||||
---
|
||||
`;
|
||||
|
||||
if (summary) {
|
||||
systemPrompt = `${systemPrompt}
|
||||
below is the chat history till now
|
||||
CHAT SUMMARY:
|
||||
---
|
||||
${props.summary}
|
||||
---
|
||||
`;
|
||||
|
||||
const lastMessage = processedMessages.pop();
|
||||
|
||||
if (lastMessage) {
|
||||
processedMessages = [lastMessage];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
logger.info(`Sending llm call to ${provider.name} with model ${modelDetails.name}`);
|
||||
|
||||
// console.log(systemPrompt,processedMessages);
|
||||
|
||||
return await _streamText({
|
||||
model: provider.getModelInstance({
|
||||
model: modelDetails.name,
|
||||
|
||||
128
app/lib/.server/llm/utils.ts
Normal file
128
app/lib/.server/llm/utils.ts
Normal file
@@ -0,0 +1,128 @@
|
||||
import { type Message } from 'ai';
|
||||
import { DEFAULT_MODEL, DEFAULT_PROVIDER, MODEL_REGEX, PROVIDER_REGEX } from '~/utils/constants';
|
||||
import { IGNORE_PATTERNS, type FileMap } from './constants';
|
||||
import ignore from 'ignore';
|
||||
import type { ContextAnnotation } from '~/types/context';
|
||||
|
||||
export function extractPropertiesFromMessage(message: Omit<Message, 'id'>): {
|
||||
model: string;
|
||||
provider: string;
|
||||
content: string;
|
||||
} {
|
||||
const textContent = Array.isArray(message.content)
|
||||
? message.content.find((item) => item.type === 'text')?.text || ''
|
||||
: message.content;
|
||||
|
||||
const modelMatch = textContent.match(MODEL_REGEX);
|
||||
const providerMatch = textContent.match(PROVIDER_REGEX);
|
||||
|
||||
/*
|
||||
* Extract model
|
||||
* const modelMatch = message.content.match(MODEL_REGEX);
|
||||
*/
|
||||
const model = modelMatch ? modelMatch[1] : DEFAULT_MODEL;
|
||||
|
||||
/*
|
||||
* Extract provider
|
||||
* const providerMatch = message.content.match(PROVIDER_REGEX);
|
||||
*/
|
||||
const provider = providerMatch ? providerMatch[1] : DEFAULT_PROVIDER.name;
|
||||
|
||||
const cleanedContent = Array.isArray(message.content)
|
||||
? message.content.map((item) => {
|
||||
if (item.type === 'text') {
|
||||
return {
|
||||
type: 'text',
|
||||
text: item.text?.replace(MODEL_REGEX, '').replace(PROVIDER_REGEX, ''),
|
||||
};
|
||||
}
|
||||
|
||||
return item; // Preserve image_url and other types as is
|
||||
})
|
||||
: textContent.replace(MODEL_REGEX, '').replace(PROVIDER_REGEX, '');
|
||||
|
||||
return { model, provider, content: cleanedContent };
|
||||
}
|
||||
|
||||
export function simplifyBoltActions(input: string): string {
|
||||
// Using regex to match boltAction tags that have type="file"
|
||||
const regex = /(<boltAction[^>]*type="file"[^>]*>)([\s\S]*?)(<\/boltAction>)/g;
|
||||
|
||||
// Replace each matching occurrence
|
||||
return input.replace(regex, (_0, openingTag, _2, closingTag) => {
|
||||
return `${openingTag}\n ...\n ${closingTag}`;
|
||||
});
|
||||
}
|
||||
|
||||
export function createFilesContext(files: FileMap, useRelativePath?: boolean) {
|
||||
const ig = ignore().add(IGNORE_PATTERNS);
|
||||
let filePaths = Object.keys(files);
|
||||
filePaths = filePaths.filter((x) => {
|
||||
const relPath = x.replace('/home/project/', '');
|
||||
return !ig.ignores(relPath);
|
||||
});
|
||||
|
||||
const fileContexts = filePaths
|
||||
.filter((x) => files[x] && files[x].type == 'file')
|
||||
.map((path) => {
|
||||
const dirent = files[path];
|
||||
|
||||
if (!dirent || dirent.type == 'folder') {
|
||||
return '';
|
||||
}
|
||||
|
||||
const codeWithLinesNumbers = dirent.content
|
||||
.split('\n')
|
||||
// .map((v, i) => `${i + 1}|${v}`)
|
||||
.join('\n');
|
||||
|
||||
let filePath = path;
|
||||
|
||||
if (useRelativePath) {
|
||||
filePath = path.replace('/home/project/', '');
|
||||
}
|
||||
|
||||
return `<file path="${filePath}">\n${codeWithLinesNumbers}\n</file>`;
|
||||
});
|
||||
|
||||
return `<codebase>${fileContexts.join('\n\n')}\n\n</codebase>`;
|
||||
}
|
||||
|
||||
export function extractCurrentContext(messages: Message[]) {
|
||||
const lastAssistantMessage = messages.filter((x) => x.role == 'assistant').slice(-1)[0];
|
||||
|
||||
if (!lastAssistantMessage) {
|
||||
return { summary: undefined, codeContext: undefined };
|
||||
}
|
||||
|
||||
let summary: ContextAnnotation | undefined;
|
||||
let codeContext: ContextAnnotation | undefined;
|
||||
|
||||
if (!lastAssistantMessage.annotations?.length) {
|
||||
return { summary: undefined, codeContext: undefined };
|
||||
}
|
||||
|
||||
for (let i = 0; i < lastAssistantMessage.annotations.length; i++) {
|
||||
const annotation = lastAssistantMessage.annotations[i];
|
||||
|
||||
if (!annotation || typeof annotation !== 'object') {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!(annotation as any).type) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const annotationObject = annotation as any;
|
||||
|
||||
if (annotationObject.type === 'codeContext') {
|
||||
codeContext = annotationObject;
|
||||
break;
|
||||
} else if (annotationObject.type === 'chatSummary') {
|
||||
summary = annotationObject;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return { summary, codeContext };
|
||||
}
|
||||
@@ -111,7 +111,7 @@ export abstract class BaseProvider implements ProviderInfo {
|
||||
|
||||
abstract getModelInstance(options: {
|
||||
model: string;
|
||||
serverEnv: Env;
|
||||
serverEnv?: Env;
|
||||
apiKeys?: Record<string, string>;
|
||||
providerSettings?: Record<string, IProviderSetting>;
|
||||
}): LanguageModelV1;
|
||||
|
||||
Reference in New Issue
Block a user