|
| 1 | +const { OpenAI } = require("openai"); |
| 2 | +const { QueryAntVDocumentTool, ExtractAntVTopicTool } = require('@antv/mcp-server-antv/build/tools'); |
| 3 | + |
| 4 | +/** |
| 5 | + * @param {Object} param |
| 6 | + * @param {import('@actions/github').GitHub} param.github |
| 7 | + * @param {import('@actions/core')} param.core |
| 8 | + * @param {Object} param.context GitHub Action context |
| 9 | + * @param {Object} param.discussion The discussion object from the payload |
| 10 | + */ |
| 11 | +module.exports = async ({ github, core, context, discussion }) => { |
| 12 | + try { |
| 13 | + core.info('Starting to process discussion...', context.repo.repo); |
| 14 | + const library = `g`; |
| 15 | + if (!discussion || !discussion.node_id) { // Check if node_id exists |
| 16 | + core.setFailed('Could not find discussion information or node_id'); |
| 17 | + return; |
| 18 | + } |
| 19 | + |
| 20 | + // 1. Get information from discussion object (unchanged) |
| 21 | + const discussionNumber = discussion.number; |
| 22 | + const discussionTitle = discussion.title; |
| 23 | + core.info(`Processing discussion #${discussionNumber}: ${discussionTitle}`); |
| 24 | + |
| 25 | + // 2. AI processing logic (completely unchanged) |
| 26 | + const combinedQuery = prepareAIPrompt(context, discussion); |
| 27 | + const topicExtractionResult = await ExtractAntVTopicTool.run({ query: combinedQuery }); |
| 28 | + const aiResponse = await getAIResponse(core, topicExtractionResult.content[0].text); |
| 29 | + const jsonMatch = aiResponse.match(/```json\s*(\{[\s\S]*?\})\s*```/); |
| 30 | + const processedTopicContent = JSON.parse(jsonMatch[1]); |
| 31 | + const queryDocumentParams = { |
| 32 | + library, |
| 33 | + query: combinedQuery, |
| 34 | + topic: processedTopicContent.topic, |
| 35 | + intent: processedTopicContent.intent, |
| 36 | + tokens: 5000, |
| 37 | + ...(processedTopicContent.subTasks && { subTasks: processedTopicContent.subTasks }), |
| 38 | + }; |
| 39 | + const documentationResult = await QueryAntVDocumentTool.run(queryDocumentParams); |
| 40 | + const response = await getAIResponse(core, documentationResult.content[0].text); |
| 41 | + |
| 42 | + // 3. Key modification: Use GraphQL Mutation to create a comment |
| 43 | + core.info(`Preparing to post a reply to Discussion #${discussion.number}...`); |
| 44 | + const mutation = ` |
| 45 | + mutation AddDiscussionComment($discussionId: ID!, $body: String!) { |
| 46 | + addDiscussionComment(input: {discussionId: $discussionId, body: $body}) { |
| 47 | + comment { |
| 48 | + id |
| 49 | + url |
| 50 | + } |
| 51 | + } |
| 52 | + }`; |
| 53 | + |
| 54 | + const variables = { |
| 55 | + // Key: GraphQL needs the global node_id, not the number |
| 56 | + discussionId: discussion.node_id, |
| 57 | + body: `@${discussion.user.login} Hello! Here is an automated response to your question:\n\n${response}\n\n---\n*This is an automated response generated by AI. If you have any questions, our team will follow up as soon as possible.*` |
| 58 | + }; |
| 59 | + |
| 60 | + await github.graphql(mutation, variables); |
| 61 | + |
| 62 | + core.info('Successfully posted the reply!'); |
| 63 | + |
| 64 | + } catch (error) { |
| 65 | + core.setFailed(`Failed to process discussion: ${error.message}`); |
| 66 | + core.error(error.stack); |
| 67 | + } |
| 68 | +}; |
| 69 | + |
| 70 | +// prepareAIPrompt 函数保持不变 |
| 71 | +function prepareAIPrompt(context, post) { |
| 72 | + return ` |
| 73 | + You are an intelligent assistant for the ${context.repo.repo} project. This is an automated response system for GitHub discussions. |
| 74 | + Please analyze the following discussion and provide a professional, helpful response. |
| 75 | +
|
| 76 | + ## Current Discussion |
| 77 | + - Title: ${post.title} |
| 78 | + - Content: ${post.body} |
| 79 | +
|
| 80 | + Please provide a complete and helpful response, but avoid being too verbose. The response should be well-structured and use appropriate Markdown formatting. |
| 81 | +`; |
| 82 | +} |
| 83 | + |
| 84 | +// getAIResponse 函数保持不变 |
| 85 | +async function getAIResponse(core, userQuestion) { |
| 86 | + try { |
| 87 | + core.info('Calling GitHub AI API...'); |
| 88 | + const token = process.env.GH_TOKEN; |
| 89 | + if (!token) { throw new Error('GH_TOKEN environment variable not found'); } |
| 90 | + const endpoint = "https://models.github.ai/inference"; |
| 91 | + const client = new OpenAI({ baseURL: endpoint, apiKey: token }); |
| 92 | + const response = await client.chat.completions.create({ |
| 93 | + messages: [{ role: "user", content: userQuestion }], |
| 94 | + temperature: 0.7, |
| 95 | + top_p: 1.0, |
| 96 | + model: "openai/gpt-4.1" |
| 97 | + }); |
| 98 | + core.info('Successfully received AI response'); |
| 99 | + return response.choices[0].message.content; |
| 100 | + } catch (error) { |
| 101 | + core.warning(`Failed to call GitHub AI API: ${error.message}`); |
| 102 | + return `Thank you for starting this discussion!\n\nOur team will review your question as soon as possible. Thank you for your understanding and support!`; |
| 103 | + } |
| 104 | +} |
0 commit comments