🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.
TypeScript agentagent-collaborationagent-harnessloop-engineeringmcppersonal-assistantproactive-agentskillssuper-agenttask-management
♻️ refactor: improve codebase (#9353)
Arvin Xu7dc000e10ca6
diff --git a/packages/utils/src/fetch/fetchEventSource/index.ts b/packages/utils/src/client/fetchEventSource/index.tssimilarity index 100%rename from packages/utils/src/fetch/fetchEventSource/index.tsrename to packages/utils/src/client/fetchEventSource/index.tsdiff --git a/packages/utils/src/fetch/fetchEventSource/parse.ts b/packages/utils/src/client/fetchEventSource/parse.tssimilarity index 100%rename from packages/utils/src/fetch/fetchEventSource/parse.tsrename to packages/utils/src/client/fetchEventSource/parse.tsdiff --git a/packages/utils/src/client/index.ts b/packages/utils/src/client/index.tsindex c504effac16..866082f7c96 100644--- a/packages/utils/src/client/index.ts+++ b/packages/utils/src/client/index.ts@@ -1,4 +1,5 @@export * from './clipboard';export * from './downloadFile';export * from './exportFile';+export * from './fetchEventSource';export * from './sanitize';diff --git a/packages/utils/src/fetch/__tests__/fetchSSE.test.ts b/packages/utils/src/fetch/__tests__/fetchSSE.test.tsindex 9f6dfc69503..52b1bba1d6d 100644--- a/packages/utils/src/fetch/__tests__/fetchSSE.test.ts+++ b/packages/utils/src/fetch/__tests__/fetchSSE.test.ts@@ -2,9 +2,9 @@ import { MESSAGE_CANCEL_FLAT } from '@lobechat/const';import { ChatMessageError } from '@lobechat/types';import { afterEach, describe, expect, it, vi } from 'vitest';+import { FetchEventSourceInit } from '../../client/fetchEventSource';+import { fetchEventSource } from '../../client/fetchEventSource';import { sleep } from '../../sleep';-import { FetchEventSourceInit } from '../fetchEventSource';-import { fetchEventSource } from '../fetchEventSource';import { fetchSSE } from '../fetchSSE';// 模拟 i18next@@ -12,7 +12,7 @@ vi.mock('i18next', () => ({t: vi.fn((key) => `translated_${key}`),}));-vi.mock('../fetchEventSource', () => ({+vi.mock('../../client/fetchEventSource', () => ({fetchEventSource: vi.fn(),}));diff --git a/packages/utils/src/fetch/fetchSSE.ts b/packages/utils/src/fetch/fetchSSE.tsindex 61d3ad84eea..bdcc748d127 100644--- a/packages/utils/src/fetch/fetchSSE.ts+++ b/packages/utils/src/fetch/fetchSSE.ts@@ -15,8 +15,8 @@ import {ResponseAnimationStyle,} from '@lobechat/types';+import { fetchEventSource } from '../client/fetchEventSource';import { nanoid } from '../uuid';-import { fetchEventSource } from './fetchEventSource';import { getMessageError } from './parseError';type SSEFinishType = 'done' | 'error' | 'abort';diff --git a/src/services/chat/contextEngineering.test.ts b/src/services/chat/contextEngineering.test.tsindex 2ac47bbd917..63caba21444 100644--- a/src/services/chat/contextEngineering.test.ts+++ b/src/services/chat/contextEngineering.test.ts@@ -260,17 +260,13 @@ describe('contextEngineering', () => {},];- const result = await contextEngineering(- {- messages,- model: 'gpt-4',- provider: 'openai',- },- {- isWelcomeQuestion: true,- trace: { sessionId: 'inbox' },- },- );+ const result = await contextEngineering({+ messages,+ model: 'gpt-4',+ provider: 'openai',+ isWelcomeQuestion: true,+ sessionId: 'inbox',+ });// Should have system message with inbox guide contentconst systemMessage = result.find((msg) => msg.role === 'system');@@ -295,16 +291,12 @@ describe('contextEngineering', () => {},];- const result = await contextEngineering(- {- messages,- model: 'gpt-4',- provider: 'openai',- },- {- historySummary,- },- );+ const result = await contextEngineering({+ messages,+ model: 'gpt-4',+ historySummary,+ provider: 'openai',+ });// Should have system message with history summaryconst systemMessage = result.find((msg) => msg.role === 'system');diff --git a/src/services/chat/contextEngineering.ts b/src/services/chat/contextEngineering.tsindex eec7aab2486..6a9d4c5f539 100644--- a/src/services/chat/contextEngineering.ts+++ b/src/services/chat/contextEngineering.ts@@ -24,30 +24,34 @@ import { VARIABLE_GENERATORS } from '@/utils/client/parserPlaceholder';import { genToolCallingName } from '@/utils/toolCall';import { isCanUseFC, isCanUseVision } from './helper';-import { FetchOptions } from './types';-export const contextEngineering = async (- {- messages = [],- tools,- model,- provider,- systemRole,- inputTemplate,- enableHistoryCount,- historyCount,- }: {- enableHistoryCount?: boolean;- historyCount?: number;- inputTemplate?: string;- messages: ChatMessage[];- model: string;- provider: string;- systemRole?: string;- tools?: string[];- },- options?: FetchOptions,-): Promise<OpenAIChatMessage[]> => {+interface ContextEngineeringContext {+ enableHistoryCount?: boolean;+ historyCount?: number;+ historySummary?: string;+ inputTemplate?: string;+ isWelcomeQuestion?: boolean;+ messages: ChatMessage[];+ model: string;+ provider: string;+ sessionId?: string;+ systemRole?: string;+ tools?: string[];+}++export const contextEngineering = async ({+ messages = [],+ tools,+ model,+ provider,+ systemRole,+ inputTemplate,+ enableHistoryCount,+ historyCount,+ historySummary,+ sessionId,+ isWelcomeQuestion,+}: ContextEngineeringContext): Promise<OpenAIChatMessage[]> => {const pipeline = new ContextEngine({pipeline: [// 1. History truncation (MUST be first, before any message injection)@@ -62,8 +66,8 @@ export const contextEngineering = async (new InboxGuideProvider({inboxGuideSystemRole: INBOX_GUIDE_SYSTEMROLE,inboxSessionId: INBOX_SESSION_ID,- isWelcomeQuestion: options?.isWelcomeQuestion,- sessionId: options?.trace?.sessionId,+ isWelcomeQuestion: isWelcomeQuestion,+ sessionId: sessionId,}),// 4. Tool system role injection@@ -78,7 +82,7 @@ export const contextEngineering = async (// 5. History summary injectionnew HistorySummaryProvider({formatHistorySummary: historySummaryPrompt,- historySummary: options?.historySummary,+ historySummary: historySummary,}),// Create message processing processorsdiff --git a/src/services/chat/index.ts b/src/services/chat/index.tsindex 1c4c1bbd785..893aaccaead 100644--- a/src/services/chat/index.ts+++ b/src/services/chat/index.ts@@ -114,20 +114,20 @@ class ChatService {const agentConfig = agentSelectors.currentAgentConfig(agentStoreState);// Apply context engineering with preprocessing configuration- const oaiMessages = await contextEngineering(- {- enableHistoryCount: agentChatConfigSelectors.enableHistoryCount(agentStoreState),- // include user messages- historyCount: agentChatConfigSelectors.historyCount(agentStoreState) + 2,- inputTemplate: chatConfig.inputTemplate,- messages,- model: payload.model,- provider: payload.provider!,- systemRole: agentConfig.systemRole,- tools: pluginIds,- },- options,- );+ const oaiMessages = await contextEngineering({+ enableHistoryCount: agentChatConfigSelectors.enableHistoryCount(agentStoreState),+ // include user messages+ historyCount: agentChatConfigSelectors.historyCount(agentStoreState) + 2,+ historySummary: options?.historySummary,+ inputTemplate: chatConfig.inputTemplate,+ isWelcomeQuestion: options?.isWelcomeQuestion,+ messages,+ model: payload.model,+ provider: payload.provider!,+ sessionId: options?.trace?.sessionId,+ systemRole: agentConfig.systemRole,+ tools: pluginIds,+ });// ============ 2. preprocess tools ============ //