|
| 1 | +const { OpenAI } = require("openai"); |
| 2 | +const { QueryAntVDocumentTool, ExtractAntVTopicTool }= require('@antv/mcp-server-antv/build/tools'); |
| 3 | + |
| 4 | + |
| 5 | +/** |
| 6 | + * @param {Object} param |
| 7 | + * @param {import('@actions/github').GitHub} param.github |
| 8 | + * @param {import('@actions/core')} param.core |
| 9 | + * @param {Object} param.context GitHub Action context |
| 10 | + */ |
| 11 | +module.exports = async ({ github, core, context, issue }) => { |
| 12 | + try { |
| 13 | + core.info('开始处理 issue...'); |
| 14 | + const library = `${context.repo.repo}` |
| 15 | + if (!issue) { |
| 16 | + core.setFailed('找不到 issue 信息'); |
| 17 | + return; |
| 18 | + } |
| 19 | + |
| 20 | + const issueNumber = issue.number; |
| 21 | + const issueTitle = issue.title; |
| 22 | + |
| 23 | + core.info(`处理 issue #${issueNumber}: ${issueTitle}`); |
| 24 | + |
| 25 | + const combinedQuery = prepareAIPrompt(context, issue); |
| 26 | + |
| 27 | + |
| 28 | + const topicExtractionResult = await ExtractAntVTopicTool.run({ query: combinedQuery }); |
| 29 | + |
| 30 | + const aiResponse = await getAIResponse(core, topicExtractionResult.content[0].text); |
| 31 | + const jsonMatch = aiResponse.match(/```json\s*(\{[\s\S]*?\})\s*```/); |
| 32 | + const processedTopicContent = JSON.parse(jsonMatch[1]); |
| 33 | + |
| 34 | + const queryDocumentParams = { |
| 35 | + library, |
| 36 | + query: combinedQuery, |
| 37 | + topic: processedTopicContent.topic, |
| 38 | + intent: processedTopicContent.intent, |
| 39 | + tokens: 5000, |
| 40 | + ...(processedTopicContent.subTasks && { subTasks: processedTopicContent.subTasks }), |
| 41 | + }; |
| 42 | + |
| 43 | + const documentationResult = await QueryAntVDocumentTool.run(queryDocumentParams); |
| 44 | + |
| 45 | + const response = await getAIResponse(core, documentationResult.content[0].text); |
| 46 | + |
| 47 | + await github.rest.issues.createComment({ |
| 48 | + issue_number: issue.number, |
| 49 | + owner: context.repo.owner, |
| 50 | + repo: context.repo.repo, |
| 51 | + body: `@${issue.user.login} 您好!以下是关于您问题的自动回复:\n\n${response}\n\n---\n*此回复由 AI 助手自动生成。如有任何问题,我们的团队会尽快跟进。*` |
| 52 | + }); |
| 53 | + |
| 54 | + core.info('Issue 处理完成'); |
| 55 | + |
| 56 | + } catch (error) { |
| 57 | + core.setFailed(`处理 issue 失败: ${error.message}`); |
| 58 | + core.error(error.stack); |
| 59 | + } |
| 60 | +}; |
| 61 | + |
| 62 | +function prepareAIPrompt(context, issue) { |
| 63 | + return ` |
| 64 | + 你是 ${context.repo.repo} 项目的智能助手。这是一个处理 GitHub issue 的自动回复系统。 |
| 65 | + 请分析以下 issue 并提供专业、有帮助的回复。 |
| 66 | +
|
| 67 | + ## 当前 Issue |
| 68 | + - 标题: ${issue.title} |
| 69 | + - 内容: ${issue.body} |
| 70 | +
|
| 71 | + 请提供完整、有帮助的回复,但不要过于冗长。回复应该条理清晰,使用适当的 Markdown 格式。 |
| 72 | +`; |
| 73 | +} |
| 74 | + |
| 75 | +/** |
| 76 | + * 调用 GitHub AI API 获取回复 |
| 77 | + */ |
| 78 | +async function getAIResponse(core, userQuestion) { |
| 79 | + try { |
| 80 | + core.info('正在调用 GitHub AI API...'); |
| 81 | + |
| 82 | + const token = process.env.GH_TOKEN; |
| 83 | + |
| 84 | + if (!token) { |
| 85 | + throw new Error('未找到 GH_TOKEN 环境变量'); |
| 86 | + } |
| 87 | + |
| 88 | + const endpoint = "https://models.github.ai/inference"; |
| 89 | + const model = "openai/gpt-4.1"; |
| 90 | + |
| 91 | + const client = new OpenAI({ |
| 92 | + baseURL: endpoint, |
| 93 | + apiKey: token |
| 94 | + }); |
| 95 | + |
| 96 | + const response = await client.chat.completions.create({ |
| 97 | + messages: [ |
| 98 | + { role: "user", content: userQuestion } |
| 99 | + ], |
| 100 | + temperature: 0.7, |
| 101 | + top_p: 1.0, |
| 102 | + model: model |
| 103 | + }); |
| 104 | + |
| 105 | + core.info('成功获取 AI 响应'); |
| 106 | + return response.choices[0].message.content; |
| 107 | + |
| 108 | + } catch (error) { |
| 109 | + core.warning(`调用 GitHub AI API 失败: ${error.message}`); |
| 110 | + // 默认回复 |
| 111 | + return ` |
| 112 | + 感谢您提交这个 issue! |
| 113 | +
|
| 114 | + 我们的团队会尽快查看您的问题。为了帮助我们更快地解决,请确保您提供了: |
| 115 | +
|
| 116 | + - 问题的详细描述 |
| 117 | + - 复现步骤 (如果是 bug) |
| 118 | + - 预期行为和实际行为 |
| 119 | + - 使用的版本信息 |
| 120 | +
|
| 121 | + 谢谢您的理解与支持! |
| 122 | +`; |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | + |
0 commit comments