Skip to content

🀯 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.

♻️ refactor: extract agent-stream into `@lobechat/agent-gateway-client` package (#13866)

Arvin Xudd81642d83fa
diff --git a/apps/cli/package.json b/apps/cli/package.json
index edae30933b9..0892be5e713 100644
--- a/apps/cli/package.json
+++ b/apps/cli/package.json
@@ -28,6 +28,7 @@
"type-check": "tsc --noEmit"
},
"devDependencies": {
+ "@lobechat/agent-gateway-client": "workspace:*",
"@lobechat/device-gateway-client": "workspace:*",
"@lobechat/local-file-shell": "workspace:*",
"@trpc/client": "^11.8.1",
diff --git a/apps/cli/pnpm-workspace.yaml b/apps/cli/pnpm-workspace.yaml
index 8911f42be7a..0ee104d0196 100644
--- a/apps/cli/pnpm-workspace.yaml
+++ b/apps/cli/pnpm-workspace.yaml
@@ -1,4 +1,5 @@
packages:
+ - '../../packages/agent-gateway-client'
- '../../packages/device-gateway-client'
- '../../packages/local-file-shell'
- '../../packages/file-loaders'
diff --git a/apps/cli/src/utils/agentStream.ts b/apps/cli/src/utils/agentStream.ts
index c67a33b21cb..9f83ced9509 100644
--- a/apps/cli/src/utils/agentStream.ts
+++ b/apps/cli/src/utils/agentStream.ts
@@ -1,16 +1,10 @@
+import type { AgentStreamEvent } from '@lobechat/agent-gateway-client';
import pc from 'picocolors';
import urlJoin from 'url-join';
import { log } from './logger';
-export interface AgentStreamEvent {
- data: any;
- id?: string;
- operationId: string;
- stepIndex: number;
- timestamp: number;
- type: string;
-}
+export type { AgentStreamEvent } from '@lobechat/agent-gateway-client';
interface StreamOptions {
json?: boolean;
diff --git a/package.json b/package.json
index 52a1d0a1049..3622ab08d15 100644
--- a/package.json
+++ b/package.json
@@ -200,6 +200,7 @@
"@khmyznikov/pwa-install": "0.3.9",
"@larksuiteoapi/node-sdk": "^1.60.0",
"@lexical/utils": "^0.42.0",
+ "@lobechat/agent-gateway-client": "workspace:*",
"@lobechat/agent-runtime": "workspace:*",
"@lobechat/agent-templates": "workspace:*",
"@lobechat/builtin-agents": "workspace:*",
diff --git a/packages/agent-gateway-client/package.json b/packages/agent-gateway-client/package.json
new file mode 100644
index 00000000000..80ab7634179
--- /dev/null
+++ b/packages/agent-gateway-client/package.json
@@ -0,0 +1,18 @@
+{
+ "name": "@lobechat/agent-gateway-client",
+ "version": "1.0.0",
+ "private": true,
+ "description": "Browser-compatible WebSocket client for the LobeHub Agent Gateway",
+ "exports": {
+ ".": "./src/index.ts"
+ },
+ "main": "./src/index.ts",
+ "types": "./src/index.ts",
+ "scripts": {
+ "test": "bunx vitest run --silent='passed-only'",
+ "test:coverage": "bunx vitest run --coverage"
+ },
+ "devDependencies": {
+ "vitest": "^3.0.0"
+ }
+}
diff --git a/src/libs/agent-stream/client.test.ts b/packages/agent-gateway-client/src/client.test.ts
similarity index 100%
rename from src/libs/agent-stream/client.test.ts
rename to packages/agent-gateway-client/src/client.test.ts
diff --git a/src/libs/agent-stream/client.ts b/packages/agent-gateway-client/src/client.ts
similarity index 100%
rename from src/libs/agent-stream/client.ts
rename to packages/agent-gateway-client/src/client.ts
diff --git a/src/libs/agent-stream/index.ts b/packages/agent-gateway-client/src/index.ts
similarity index 100%
rename from src/libs/agent-stream/index.ts
rename to packages/agent-gateway-client/src/index.ts
diff --git a/src/libs/agent-stream/types.ts b/packages/agent-gateway-client/src/types.ts
similarity index 100%
rename from src/libs/agent-stream/types.ts
rename to packages/agent-gateway-client/src/types.ts
diff --git a/packages/agent-gateway-client/vitest.config.mts b/packages/agent-gateway-client/vitest.config.mts
new file mode 100644
index 00000000000..8c4481a3756
--- /dev/null
+++ b/packages/agent-gateway-client/vitest.config.mts
@@ -0,0 +1,18 @@
+import { defineConfig } from 'vitest/config';
+
+export default defineConfig({
+ test: {
+ coverage: {
+ exclude: [
+ 'coverage/**',
+ 'dist/**',
+ '**/node_modules/**',
+ '**/*.d.ts',
+ '**/*.config.*',
+ '**/index.ts',
+ ],
+ reporter: ['text', 'json', 'lcov', 'text-summary'],
+ },
+ environment: 'happy-dom',
+ },
+});
diff --git a/src/server/modules/AgentRuntime/GatewayStreamNotifier.ts b/src/server/modules/AgentRuntime/GatewayStreamNotifier.ts
index a2130bbf5c7..944122776f8 100644
--- a/src/server/modules/AgentRuntime/GatewayStreamNotifier.ts
+++ b/src/server/modules/AgentRuntime/GatewayStreamNotifier.ts
@@ -1,8 +1,7 @@
+import type { ToolExecuteData } from '@lobechat/agent-gateway-client';
import debug from 'debug';
import urlJoin from 'url-join';
-import type { ToolExecuteData } from '@/libs/agent-stream/types';
-
import {
getDefaultReasonDetail,
type StreamChunkData,
diff --git a/src/server/modules/AgentRuntime/types.ts b/src/server/modules/AgentRuntime/types.ts
index cdfc12b714d..18902e11f72 100644
--- a/src/server/modules/AgentRuntime/types.ts
+++ b/src/server/modules/AgentRuntime/types.ts
@@ -1,7 +1,6 @@
+import type { ToolExecuteData } from '@lobechat/agent-gateway-client';
import { type AgentState } from '@lobechat/agent-runtime';
-import type { ToolExecuteData } from '@/libs/agent-stream/types';
-
import { type AgentOperationMetadata, type StepResult } from './AgentStateManager';
import { type StreamChunkData, type StreamEvent } from './StreamEventManager';
diff --git a/src/store/chat/slices/aiChat/actions/__tests__/clientToolExecution.test.ts b/src/store/chat/slices/aiChat/actions/__tests__/clientToolExecution.test.ts
index b0bf943b233..818537986a2 100644
--- a/src/store/chat/slices/aiChat/actions/__tests__/clientToolExecution.test.ts
+++ b/src/store/chat/slices/aiChat/actions/__tests__/clientToolExecution.test.ts
@@ -1,7 +1,6 @@
+import type { ToolExecuteData } from '@lobechat/agent-gateway-client';
import { beforeEach, describe, expect, it, vi } from 'vitest';
-import type { ToolExecuteData } from '@/libs/agent-stream';
-
import { ClientToolExecutionActionImpl } from '../clientToolExecution';
// ─── Hoisted mocks ───
diff --git a/src/store/chat/slices/aiChat/actions/__tests__/gateway.test.ts b/src/store/chat/slices/aiChat/actions/__tests__/gateway.test.ts
index ef5ec038c24..73ad9e142ba 100644
--- a/src/store/chat/slices/aiChat/actions/__tests__/gateway.test.ts
+++ b/src/store/chat/slices/aiChat/actions/__tests__/gateway.test.ts
@@ -1,6 +1,6 @@
+import type { AgentStreamEvent } from '@lobechat/agent-gateway-client';
import { afterEach, describe, expect, it, vi } from 'vitest';
-import type { AgentStreamEvent } from '@/libs/agent-stream';
import { aiAgentService } from '@/services/aiAgent';
import type { GatewayConnection } from '../gateway';
diff --git a/src/store/chat/slices/aiChat/actions/__tests__/gatewayEventHandler.test.ts b/src/store/chat/slices/aiChat/actions/__tests__/gatewayEventHandler.test.ts
index 9019bd0b0b8..9bc80a1d1fc 100644
--- a/src/store/chat/slices/aiChat/actions/__tests__/gatewayEventHandler.test.ts
+++ b/src/store/chat/slices/aiChat/actions/__tests__/gatewayEventHandler.test.ts
@@ -1,7 +1,6 @@
+import type { AgentStreamEvent } from '@lobechat/agent-gateway-client';
import { describe, expect, it, vi } from 'vitest';
-import type { AgentStreamEvent } from '@/libs/agent-stream';
-
import { createGatewayEventHandler } from '../gatewayEventHandler';
vi.mock('@/services/message', () => ({
diff --git a/src/store/chat/slices/aiChat/actions/clientToolExecution.ts b/src/store/chat/slices/aiChat/actions/clientToolExecution.ts
index ac6c4aaf115..fa9e32338a5 100644
--- a/src/store/chat/slices/aiChat/actions/clientToolExecution.ts
+++ b/src/store/chat/slices/aiChat/actions/clientToolExecution.ts
@@ -1,8 +1,8 @@
+import { type ToolExecuteData, type ToolResultMessage } from '@lobechat/agent-gateway-client';
import { type BuiltinToolContext } from '@lobechat/types';
import debug from 'debug';
import { produce } from 'immer';
-import { type ToolExecuteData, type ToolResultMessage } from '@/libs/agent-stream';
import { mcpService } from '@/services/mcp';
import { type ChatStore } from '@/store/chat/store';
import { hasExecutor, invokeExecutor } from '@/store/tool/slices/builtin/executors';
diff --git a/src/store/chat/slices/aiChat/actions/gateway.ts b/src/store/chat/slices/aiChat/actions/gateway.ts
index 30d442f2560..e45402b45a4 100644
--- a/src/store/chat/slices/aiChat/actions/gateway.ts
+++ b/src/store/chat/slices/aiChat/actions/gateway.ts
@@ -1,12 +1,12 @@
+import {
+ AgentStreamClient,
+ type AgentStreamClientOptions,
+ type AgentStreamEvent,
+ type ConnectionStatus,
+} from '@lobechat/agent-gateway-client';
import type { ConversationContext, ExecAgentResult } from '@lobechat/types';
import { isDesktop } from '@/const/version';
-import type {
- AgentStreamClientOptions,
- AgentStreamEvent,
- ConnectionStatus,
-} from '@/libs/agent-stream';
-import { AgentStreamClient } from '@/libs/agent-stream/client';
import { aiAgentService, type ResumeApprovalParam } from '@/services/aiAgent';
import { messageService } from '@/services/message';
import { topicService } from '@/services/topic';
diff --git a/src/store/chat/slices/aiChat/actions/gatewayEventHandler.ts b/src/store/chat/slices/aiChat/actions/gatewayEventHandler.ts
index 35422f1a78e..9aac728ba6e 100644
--- a/src/store/chat/slices/aiChat/actions/gatewayEventHandler.ts
+++ b/src/store/chat/slices/aiChat/actions/gatewayEventHandler.ts
@@ -1,12 +1,12 @@
-import type { ConversationContext } from '@lobechat/types';
-
import type {
AgentStreamEvent,
StepCompleteData,
StreamChunkData,
StreamStartData,
ToolExecuteData,
-} from '@/libs/agent-stream';
+} from '@lobechat/agent-gateway-client';
+import type { ConversationContext } from '@lobechat/types';
+
import { messageService } from '@/services/message';
import type { ChatStore } from '@/store/chat/store';