🤯 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
⚡️ perf(spa): cut the editor runtime, ajv and database models from the boot path (#19294)
Inneia5e74a9eb42a
diff --git a/apps/cli/src/commands/agent.ts b/apps/cli/src/commands/agent.tsindex f6f698eb7c9..afe5bd85bc1 100644--- a/apps/cli/src/commands/agent.ts+++ b/apps/cli/src/commands/agent.ts@@ -1,6 +1,6 @@import { readFile } from 'node:fs/promises';-import { AgentGraphSchema } from '@lobechat/types';+import { AgentGraphSchema } from '@lobechat/types/agent/graph';import { type Command, InvalidArgumentError } from 'commander';import pc from 'picocolors';diff --git a/apps/server/src/services/aiAgent/helpers/agentFactory.ts b/apps/server/src/services/aiAgent/helpers/agentFactory.tsindex 8c05b20b730..e86b8d2138b 100644--- a/apps/server/src/services/aiAgent/helpers/agentFactory.ts+++ b/apps/server/src/services/aiAgent/helpers/agentFactory.ts@@ -1,7 +1,7 @@import type { Agent, GeneralAgentConfig } from '@lobechat/agent-runtime';import { GeneralChatAgent, GraphAgent } from '@lobechat/agent-runtime';import type { LobeAgentChatConfig, LobeAgentConfig } from '@lobechat/types';-import { AgentGraphSchema } from '@lobechat/types';+import { AgentGraphSchema } from '@lobechat/types/agent/graph';import debug from 'debug';import type { AgentRuntimeServiceOptions } from '@/server/services/agentRuntime/AgentRuntimeService';diff --git a/packages/agent-runtime/src/agents/graph/GraphAgent.ts b/packages/agent-runtime/src/agents/graph/GraphAgent.tsindex 4f5642817ac..8b24f43fed4 100644--- a/packages/agent-runtime/src/agents/graph/GraphAgent.ts+++ b/packages/agent-runtime/src/agents/graph/GraphAgent.ts@@ -1,6 +1,6 @@import { ToolNameResolver } from '@lobechat/context-engine';import type { AgentGraph, AgentGraphEdge, AgentGraphNode } from '@lobechat/types';-import { AGENT_GRAPH_ROOT_NODE_ID } from '@lobechat/types';+import { AGENT_GRAPH_ROOT_NODE_ID } from '@lobechat/types/agent/graph';import { toJsonSafe } from '@lobechat/utils/json';import type { UnknownRecord } from '@lobechat/utils/object';import { isRecord } from '@lobechat/utils/object';diff --git a/packages/agent-runtime/src/agents/graph/__tests__/GraphAgent.test.ts b/packages/agent-runtime/src/agents/graph/__tests__/GraphAgent.test.tsindex 733792311d8..26d3e4cfe6f 100644--- a/packages/agent-runtime/src/agents/graph/__tests__/GraphAgent.test.ts+++ b/packages/agent-runtime/src/agents/graph/__tests__/GraphAgent.test.ts@@ -4,7 +4,7 @@ import { fileURLToPath } from 'node:url';import { ToolNameResolver } from '@lobechat/context-engine';import type { AgentGraph, RuntimeAdditionalContextFragment } from '@lobechat/types';-import { AGENT_GRAPH_ROOT_NODE_ID, AgentGraphSchema } from '@lobechat/types';+import { AGENT_GRAPH_ROOT_NODE_ID, AgentGraphSchema } from '@lobechat/types/agent/graph';import { describe, expect, it } from 'vitest';import type { AgentInstruction, AgentRuntimeContext, AgentState } from '../../../types';diff --git a/packages/context-engine/package.json b/packages/context-engine/package.jsonindex d6f37687f6e..c94f0bfb981 100644--- a/packages/context-engine/package.json+++ b/packages/context-engine/package.json@@ -11,6 +11,10 @@"ai","lobe"],+ "exports": {+ ".": "./src/index.ts",+ "./agent-document": "./src/providers/AgentDocumentInjector/shared.ts"+ },"main": "src/index.ts","scripts": {"test": "vitest",diff --git a/packages/context-engine/src/providers/AgentDocumentInjector/shared.ts b/packages/context-engine/src/providers/AgentDocumentInjector/shared.tsindex bc61f539670..12213a7c886 100644--- a/packages/context-engine/src/providers/AgentDocumentInjector/shared.ts+++ b/packages/context-engine/src/providers/AgentDocumentInjector/shared.ts@@ -3,8 +3,8 @@ import type { AgentDocumentPolicyLoad } from '@lobechat/types';import type {AgentDocumentLoadRule,AgentDocumentLoadRules,-} from '../../../../database/src/models/agentDocuments';-import { matchesLoadRules } from '../../../../database/src/models/agentDocuments';+} from '../../../../database/src/models/agentDocuments/policy/loadPolicy';+import { matchesLoadRules } from '../../../../database/src/models/agentDocuments/policy/loadPolicy';export type { AgentDocumentLoadRule, AgentDocumentLoadRules };export type { AgentDocumentPolicyLoad };diff --git a/packages/locales/src/create.test.ts b/packages/locales/src/create.test.tsindex 20594c6f52b..244a2f94532 100644--- a/packages/locales/src/create.test.ts+++ b/packages/locales/src/create.test.ts@@ -1,7 +1,8 @@import type { InitOptions } from 'i18next';-import { beforeEach, describe, expect, it, vi } from 'vitest';+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';-const { emit, init, on, reloadResources, storeOn, use } = vi.hoisted(() => ({+const { addResourceBundle, emit, init, on, reloadResources, storeOn, use } = vi.hoisted(() => ({+ addResourceBundle: vi.fn(),emit: vi.fn(),init: vi.fn((_options: InitOptions) => Promise.resolve()),on: vi.fn(),@@ -12,6 +13,7 @@ const { emit, init, on, reloadResources, storeOn, use } = vi.hoisted(() => ({vi.mock('i18next', () => {const instance: any = {+ addResourceBundle,emit,init,language: 'zh-CN',@@ -33,11 +35,16 @@ const { createI18nNext } = await import('./create');describe('createI18nNext', () => {beforeEach(() => {+ addResourceBundle.mockClear();init.mockClear();emit.mockClear();reloadResources.mockClear();});+ afterEach(() => {+ vi.unstubAllEnvs();+ });+it('does not bind every consumer to the resource store', () => {createI18nNext('zh-CN').init();@@ -54,6 +61,15 @@ describe('createI18nNext', () => {expect(emit.mock.calls.filter(([event]) => event === 'languageChanged')).toHaveLength(1);});+ it('refreshes consumers once after the dev English overlay landed', async () => {+ vi.stubEnv('NODE_ENV', 'development');++ await createI18nNext('en-US').init();+ await vi.waitFor(() => expect(addResourceBundle).toHaveBeenCalledTimes(4));++ expect(emit.mock.calls.filter(([event]) => event === 'languageChanged')).toHaveLength(1);+ });+it('skips the refresh when the app already runs the default language', async () => {await createI18nNext('en-US').init();await new Promise((r) => setTimeout(r, 0));diff --git a/packages/locales/src/create.ts b/packages/locales/src/create.tsindex c73c3463fba..3a57c317cef 100644--- a/packages/locales/src/create.ts+++ b/packages/locales/src/create.ts@@ -4,16 +4,9 @@ import resourcesToBackend from 'i18next-resources-to-backend';import { initReactI18next } from 'react-i18next';import { isRtlLang } from 'rtl-detect';-import chat from '@/../locales/en-US/chat.json';-import common from '@/../locales/en-US/common.json';-import error from '@/../locales/en-US/error.json';-import home from '@/../locales/en-US/home.json';import { DEFAULT_LANG } from '@/const/locale';import { getDebugConfig } from '@/envs/debug';// Sync load bundled fallback resources without Suspense on first render.-// Use src/locales/default/*.ts as the runtime fallback source, then overlay-// locales/en-US/*.json so dev-preview JSON can still customize English copy-// without dropping newly added default keys.import defaultChat from '@/locales/default/chat';import defaultCommon from '@/locales/default/common';import defaultError from '@/locales/default/error';@@ -23,21 +16,34 @@ import { isOnServerSide } from '@/utils/env';import { unwrapESMModule } from '@/utils/esm/unwrapESMModule';import { loadI18nNamespaceModule } from '@/utils/i18n/loadI18nNamespaceModule';-const mergeNamespace = (- fallbackResources: Record<string, unknown>,- localeResources: Record<string, unknown>,-) => ({- ...fallbackResources,- ...localeResources,-});-const createBundledResources = () => ({- chat: mergeNamespace(defaultChat, chat),- common: mergeNamespace(defaultCommon, common),- error: mergeNamespace(defaultError, error),- home: mergeNamespace(defaultHome, home),+ chat: defaultChat,+ common: defaultCommon,+ error: defaultError,+ home: defaultHome,});+// locales/en-US/*.json mirrors src/default/*.ts, so production ships only the+// source. In dev the JSON is overlaid asynchronously so a preview edit to the+// English copy still shows up without a rebuild.+const overlayDevEnglishCopy = async (instance: typeof i18n) => {+ if (process.env.NODE_ENV !== 'development') return;++ const overlays = {+ chat: import('@/../locales/en-US/chat.json'),+ common: import('@/../locales/en-US/common.json'),+ error: import('@/../locales/en-US/error.json'),+ home: import('@/../locales/en-US/home.json'),+ };++ for (const [ns, loading] of Object.entries(overlays)) {+ instance.addResourceBundle(DEFAULT_LANG, ns, unwrapESMModule(await loading), true, true);+ }+ // Consumers are not bound to the store (see react.bindI18nStore below), so+ // one refresh after every overlay landed is what makes an edit visible.+ instance.emit('languageChanged', instance.language);+};+const defaultResources = createBundledResources();const bundledNamespaces = Object.keys(defaultResources);@@ -116,6 +122,8 @@ export const createI18nNext = (lang?: string) => {showSupportNotice: false,});+ void initPromise.then(() => overlayDevEnglishCopy(instance));+if (initialLang !== DEFAULT_LANG) {initPromise.then(async () => {await instance.reloadResources([initialLang], bundledNamespaces);diff --git a/packages/openapi/src/types/agent.type.ts b/packages/openapi/src/types/agent.type.tsindex 576bfea5f05..18076f88b7e 100644--- a/packages/openapi/src/types/agent.type.ts+++ b/packages/openapi/src/types/agent.type.ts@@ -1,5 +1,5 @@import type { LobeAgentAgencyConfig, LobeAgentChatConfig } from '@lobechat/types';-import { AgentGraphSchema } from '@lobechat/types';+import { AgentGraphSchema } from '@lobechat/types/agent/graph';import { z } from 'zod';import type { PublicAgent, PublicFile, PublicKnowledgeBase } from '../helpers/public-fields';diff --git a/packages/types/package.json b/packages/types/package.jsonindex 3c0b3a0dc61..1bb1ca3d815 100644--- a/packages/types/package.json+++ b/packages/types/package.json@@ -2,7 +2,12 @@"name": "@lobechat/types","version": "1.0.0","private": true,+ "sideEffects": false,"type": "module",+ "exports": {+ ".": "./src/index.ts",+ "./agent/graph": "./src/agent/graph.ts"+ },"main": "./src/index.ts","scripts": {"test": "vitest",diff --git a/packages/types/src/agent/index.ts b/packages/types/src/agent/index.tsindex 927ca3c59f2..b40e1d2c3e2 100644--- a/packages/types/src/agent/index.ts+++ b/packages/types/src/agent/index.ts@@ -4,7 +4,7 @@ export * from './agentIntervention';export * from './chatConfig';export * from './displayName';export * from './document';-export * from './graph';+export type * from './graph';export * from './heteroCliArgs';export * from './heterogeneousAgent';export * from './heterogeneousIntervention';diff --git a/plugins/vite/sharedRendererConfig.ts b/plugins/vite/sharedRendererConfig.tsindex e522d4cf8c6..763f3915b99 100644--- a/plugins/vite/sharedRendererConfig.ts+++ b/plugins/vite/sharedRendererConfig.ts@@ -1,6 +1,6 @@import react from '@vitejs/plugin-react';import { codeInspectorPlugin } from 'code-inspector-plugin';-import type { ModulePreloadOptions } from 'vite';+import type { ModulePreloadOptions, Plugin } from 'vite';import { viteEmotionSpeedy } from './emotionSpeedy';import { lobeIconImports } from './lobeIconImports';@@ -408,6 +408,21 @@ export function sharedRendererPlugins(options: SharedRendererOptions) {viteNodeModuleStub(),vitePlatformResolve(options.platform),+ // GlobalProvider/Editor.tsx reaches @lobehub/editor's provider by file path+ // (the export map has no provider-only entry) so the editor runtime stays+ // off the first screen. In dev that path is served raw while the editors+ // use the prebundled @lobehub/editor/react, which would create a second+ // EditorContext; point the dev import back at the bundle.+ isDev &&+ ({+ enforce: 'pre',+ name: 'lobe-dev-editor-provider',+ resolveId(source, importer) {+ if (!source.includes('@lobehub/editor/es/react/EditorProvider')) return null;+ return this.resolve('@lobehub/editor/react', importer, { skipSelf: true });+ },+ } satisfies Plugin),+isDev && {name: 'lobe-dev-strip-manifest',transformIndexHtml: {diff --git a/src/features/AgentSetting/AgentGraphRuntime/index.tsx b/src/features/AgentSetting/AgentGraphRuntime/index.tsxindex 436e932cbad..d0944bf542a 100644--- a/src/features/AgentSetting/AgentGraphRuntime/index.tsx+++ b/src/features/AgentSetting/AgentGraphRuntime/index.tsx@@ -1,7 +1,7 @@'use client';import type { AgentGraph, LobeAgentChatConfig } from '@lobechat/types';-import { AgentGraphSchema } from '@lobechat/types';+import { AgentGraphSchema } from '@lobechat/types/agent/graph';import { Flexbox, TextArea } from '@lobehub/ui';import { Alert, Button, Switch } from '@lobehub/ui/base-ui';import { createStaticStyles, cssVar } from 'antd-style';diff --git a/src/layout/GlobalProvider/Editor.tsx b/src/layout/GlobalProvider/Editor.tsxindex e5542f0fc65..fdd67355987 100644--- a/src/layout/GlobalProvider/Editor.tsx+++ b/src/layout/GlobalProvider/Editor.tsx@@ -1,10 +1,14 @@'use client';-import { EditorProvider } from '@lobehub/editor/react';import { type PropsWithChildren } from 'react';import { memo, useMemo } from 'react';import { useTranslation } from 'react-i18next';+// @lobehub/editor/react re-exports the whole editor runtime (lexical, yjs,+// fuse) and rolldown does not shake it down to the provider, so reach the+// context module by path until the package exposes a provider-only entry.+import { EditorProvider } from '../../../node_modules/@lobehub/editor/es/react/EditorProvider';+const Editor = memo<PropsWithChildren>(({ children }) => {const {i18n: { language, getResourceBundle },diff --git a/src/utils/agentDocumentContextMapping.ts b/src/utils/agentDocumentContextMapping.tsindex 3e6f7f797a4..93ffcb20caf 100644--- a/src/utils/agentDocumentContextMapping.ts+++ b/src/utils/agentDocumentContextMapping.ts@@ -3,7 +3,7 @@ import {AGENT_DOCUMENT_INJECTION_POSITIONS,type AgentContextDocument,type AgentDocumentInjectionPosition,-} from '@lobechat/context-engine';+} from '@lobechat/context-engine/agent-document';import type { AgentDocumentContextPayload } from '@/database/models/agentDocuments';