Skip to content

Commit

Permalink
feat: code optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
life2015 committed Jan 24, 2025
1 parent bafda53 commit 301df94
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 19 deletions.
6 changes: 3 additions & 3 deletions packages/ai-native/src/browser/ai-core.contribution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,8 @@ export class AINativeBrowserContribution
if (supportsInlineChat) {
this.codeActionSingleHandler.load();
}

this.sumiMCPServerBackendProxy.initBuiltinMCPServer();
});
}

Expand Down Expand Up @@ -435,10 +437,8 @@ export class AINativeBrowserContribution
{ id: 'ai.native.mcp.start', label: 'MCP: Start MCP Server' },
{
execute: async () => {
// this.mcpServerManager.initBuiltinServer();

this.sumiMCPServerBackendProxy.initBuiltinMCPServer();

// TODO 支持第三方 MCP Server
const description: MCPServerDescription = {
name: 'filesystem',
command: 'npx',
Expand Down
16 changes: 9 additions & 7 deletions packages/ai-native/src/browser/components/ChatToolRender.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
import { IChatToolContent, uuid } from "@opensumi/ide-core-common";
import React from "react";
import { CodeEditorWithHighlight } from "./ChatEditor";
import React from 'react';

import { IChatToolContent, uuid } from '@opensumi/ide-core-common';

import { CodeEditorWithHighlight } from './ChatEditor';

export const ChatToolRender = (props: { value: IChatToolContent['content'] }) => {
const { value } = props;
console.log("🚀 ~ ChatToolRender ~ toolCall:", value)
console.log('🚀 ~ ChatToolRender ~ toolCall:', value);

if (!value || !value.function || !value.id) {
return null;
}

return <div>
<span>当前调用的工具: </span>
<span>Using Tool: </span>
<span>{value?.function?.name}</span>
<br />
<span></span>
Expand All @@ -23,5 +25,5 @@ export const ChatToolRender = (props: { value: IChatToolContent['content'] }) =>
relationId={uuid(4)}
/>)
}
</div>
};
</div>;
};
10 changes: 10 additions & 0 deletions packages/ai-native/src/browser/mcp/mcp-server-proxy.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Autowired, Injectable } from '@opensumi/di';
import { ILogger } from '@opensumi/ide-core-browser';

import { ISumiMCPServerBackend, SumiMCPServerProxyServicePath } from '../../common';
import { IMCPServerRegistry, TokenMCPServerRegistry } from '../types';

@Injectable()
Expand All @@ -11,10 +12,15 @@ export class MCPServerProxyService {
@Autowired(ILogger)
private readonly logger: ILogger;

@Autowired(SumiMCPServerProxyServicePath)
private readonly sumiMCPServerProxyService: ISumiMCPServerBackend;

// 调用 OpenSumi 内部注册的 MCP 工具
$callMCPTool(name: string, args: any) {
return this.mcpServerRegistry.callMCPTool(name, args);
}

// 获取 OpenSumi 内部注册的 MCP tools
async $getMCPTools() {
const tools = await this.mcpServerRegistry.getMCPTools().map((tool) =>
// 不要传递 handler
Expand All @@ -29,4 +35,8 @@ export class MCPServerProxyService {

return tools;
}

async getAllMCPTools() {
return this.sumiMCPServerProxyService.getAllMCPTools();
}
}
3 changes: 3 additions & 0 deletions packages/ai-native/src/common/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import { IChatMessage } from '@opensumi/ide-core-common/lib/types/ai-native';
import { DESIGN_MENUBAR_CONTAINER_VIEW_ID } from '@opensumi/ide-design/lib/common/constants';
import { IPosition, ITextModel, InlineCompletionContext } from '@opensumi/ide-monaco/lib/common';

import { MCPTool } from './types';

export const IAINativeService = Symbol('IAINativeService');

/**
Expand Down Expand Up @@ -121,6 +123,7 @@ export const TokenMCPServerProxyService = Symbol('TokenMCPServerProxyService');

export interface ISumiMCPServerBackend {
initBuiltinMCPServer(): void;
getAllMCPTools(): Promise<MCPTool[]>;
}

export const SumiMCPServerProxyServicePath = 'SumiMCPServerProxyServicePath';
Expand Down
14 changes: 7 additions & 7 deletions packages/ai-native/src/common/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ export interface IMCPServerProxyService {
content: { type: string; text: string }[];
isError?: boolean;
}>;
$getMCPTools(): Promise<
{
name: string;
description: string;
inputSchema: any;
}[]
>;
$getMCPTools(): Promise<MCPTool[]>;
}

export interface MCPTool {
name: string;
description: string;
inputSchema: any;
}
10 changes: 8 additions & 2 deletions packages/ai-native/src/node/mcp/sumi-mcp-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ import { CallToolRequestSchema, ListToolsRequestSchema } from '@modelcontextprot
import { Autowired, Injectable } from '@opensumi/di';
import { RPCService } from '@opensumi/ide-connection';

import { ISumiMCPServerBackend } from '../../common';
import { MCPServerManager } from '../../common/mcp-server-manager';
import { IMCPServerProxyService } from '../../common/types';
import { IMCPServerProxyService, MCPTool } from '../../common/types';
import { IMCPServer } from '../mcp-server';
import { MCPServerManagerImpl } from '../mcp-server-manager-impl';

Expand All @@ -19,7 +20,7 @@ import { MCPServerManagerImpl } from '../mcp-server-manager-impl';
// 处理第三方 MCP Server 的注册和调用

@Injectable({ multiple: true })
export class SumiMCPServerBackend extends RPCService<IMCPServerProxyService> {
export class SumiMCPServerBackend extends RPCService<IMCPServerProxyService> implements ISumiMCPServerBackend {

// 这里需要考虑不同的 BrowserTab 的区分问题,目前的 POC 所有的 Tab 都会注册到 tools 中
// 后续需要区分不同的 Tab 对应的实例
Expand Down Expand Up @@ -49,6 +50,11 @@ export class SumiMCPServerBackend extends RPCService<IMCPServerProxyService> {
return this.server;
}

// TODO 这里涉及到 Chat Stream Call 中带上 ClientID,具体方案需要进一步讨论
async getAllMCPTools(): Promise<MCPTool[]> {
return [];
}

initBuiltinMCPServer() {
const builtinMCPServer = new BuiltinMCPServer(this);
this.mcpServerManager.initBuiltinServer(builtinMCPServer);
Expand Down

0 comments on commit 301df94

Please sign in to comment.