162 lines
4.8 KiB
JavaScript
162 lines
4.8 KiB
JavaScript
export const codeWriteTool = {
|
||
name: "code_write",
|
||
description: "根据需求描述生成代码编写指引。返回结构化的需求分析、技术方案、代码骨架和最佳实践建议。",
|
||
inputSchema: {
|
||
type: "object",
|
||
properties: {
|
||
requirement: {
|
||
type: "string",
|
||
description: "功能需求描述",
|
||
},
|
||
language: {
|
||
type: "string",
|
||
description: "编程语言(如 typescript, python, java 等)",
|
||
},
|
||
framework: {
|
||
type: "string",
|
||
description: "框架(可选,如 react, express, fastapi 等)",
|
||
},
|
||
context: {
|
||
type: "string",
|
||
description: "额外上下文(可选,如现有代码片段、接口定义等)",
|
||
},
|
||
},
|
||
required: ["requirement", "language"],
|
||
},
|
||
};
|
||
const languageBestPractices = {
|
||
typescript: [
|
||
"使用严格类型,避免 any",
|
||
"用 interface/type 定义数据结构",
|
||
"async/await 处理异步",
|
||
"使用 const 优先,必要时 let",
|
||
"错误处理使用 try-catch + 自定义 Error 类",
|
||
],
|
||
javascript: [
|
||
"使用 const/let,禁用 var",
|
||
"使用 JSDoc 注释函数签名",
|
||
"async/await 处理异步",
|
||
"使用解构赋值简化代码",
|
||
"模块化:每个文件单一职责",
|
||
],
|
||
python: [
|
||
"遵循 PEP 8 风格规范",
|
||
"使用 type hints 标注类型",
|
||
"使用 dataclass/Pydantic 定义数据模型",
|
||
"用 with 语句管理资源",
|
||
"异常处理:捕获具体异常而非 Exception",
|
||
],
|
||
java: [
|
||
"遵循 Java 命名约定",
|
||
"使用 Optional 避免 NullPointerException",
|
||
"用 Stream API 处理集合",
|
||
"接口优先于实现",
|
||
"使用 Lombok 减少样板代码",
|
||
],
|
||
};
|
||
const frameworkTemplates = {
|
||
react: `// React 组件骨架
|
||
import React, { useState, useEffect } from 'react';
|
||
|
||
interface Props {
|
||
// TODO: 定义 props
|
||
}
|
||
|
||
export function ComponentName({ }: Props) {
|
||
const [state, setState] = useState();
|
||
|
||
useEffect(() => {
|
||
// TODO: 副作用逻辑
|
||
}, []);
|
||
|
||
return (
|
||
<div>
|
||
{/* TODO: JSX 结构 */}
|
||
</div>
|
||
);
|
||
}`,
|
||
express: `// Express 路由骨架
|
||
import express from 'express';
|
||
const router = express.Router();
|
||
|
||
router.get('/', async (req, res) => {
|
||
try {
|
||
// TODO: 业务逻辑
|
||
res.json({ success: true, data: {} });
|
||
} catch (error) {
|
||
res.status(500).json({ success: false, message: error.message });
|
||
}
|
||
});
|
||
|
||
export default router;`,
|
||
fastapi: `# FastAPI 路由骨架
|
||
from fastapi import APIRouter, HTTPException
|
||
from pydantic import BaseModel
|
||
|
||
router = APIRouter()
|
||
|
||
class RequestModel(BaseModel):
|
||
# TODO: 定义请求模型
|
||
pass
|
||
|
||
class ResponseModel(BaseModel):
|
||
success: bool
|
||
data: dict = {}
|
||
|
||
@router.get("/", response_model=ResponseModel)
|
||
async def handler():
|
||
try:
|
||
# TODO: 业务逻辑
|
||
return ResponseModel(success=True, data={})
|
||
except Exception as e:
|
||
raise HTTPException(status_code=500, detail=str(e))`,
|
||
vue: `<!-- Vue 组件骨架 -->
|
||
<template>
|
||
<div>
|
||
<!-- TODO: 模板结构 -->
|
||
</div>
|
||
</template>
|
||
|
||
<script setup lang="ts">
|
||
import { ref, onMounted } from 'vue';
|
||
|
||
// TODO: 定义响应式状态
|
||
const state = ref();
|
||
|
||
onMounted(() => {
|
||
// TODO: 初始化逻辑
|
||
});
|
||
</script>`,
|
||
};
|
||
export async function executeCodeWrite(args) {
|
||
const { requirement, language, framework, context } = args;
|
||
const practices = languageBestPractices[language.toLowerCase()] || [
|
||
"遵循语言社区标准",
|
||
"添加类型标注",
|
||
"做好错误处理",
|
||
"保持函数单一职责",
|
||
];
|
||
const output = [
|
||
`# 代码编写指引`,
|
||
``,
|
||
`## 需求`,
|
||
requirement,
|
||
``,
|
||
`## 技术规格`,
|
||
`- **语言**: ${language}`,
|
||
];
|
||
if (framework) {
|
||
output.push(`- **框架**: ${framework}`);
|
||
}
|
||
output.push(``, `## ${language} 最佳实践`, ...practices.map((p) => `- ${p}`), ``);
|
||
if (framework && frameworkTemplates[framework.toLowerCase()]) {
|
||
output.push(`## 代码骨架模板`, "```" + language, frameworkTemplates[framework.toLowerCase()], "```", ``);
|
||
}
|
||
output.push(`## 编写要求`, `1. 代码必须完整可运行,包含所有必要的 import`, `2. 遵循 ${language} 最佳实践和命名规范`, `3. 关键逻辑添加简洁注释`, `4. 包含必要的错误处理和边界检查`, `5. 函数保持单一职责,不超过 50 行`, ``);
|
||
if (context) {
|
||
output.push(`## 参考上下文`, "```", context, "```", ``);
|
||
}
|
||
output.push(`## 请根据以上指引生成完整代码`);
|
||
return output.join("\n");
|
||
}
|
||
//# sourceMappingURL=codeWrite.js.map
|