Skip to content

Commit 4072a70

Browse files
committed
generate comment to PR / issues
1 parent 5b55784 commit 4072a70

File tree

5 files changed

+117
-20
lines changed

5 files changed

+117
-20
lines changed

packages/plugin-github/src/plugins/addCommentToIssue.ts

+31-5
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,12 @@ import { GitHubService } from "../services/github";
1414
import {
1515
AddCommentToIssueContent,
1616
AddCommentToIssueSchema,
17+
GenerateCommentForASpecificPRSchema,
1718
isAddCommentToIssueContent,
19+
isGenerateCommentForASpecificPRSchema,
1820
} from "../types";
19-
import { addCommentToIssueTemplate } from "../templates";
20-
import { incorporateRepositoryState } from "../utils";
21+
import { addCommentToIssueTemplate, generateCommentForASpecificIssueTemplate } from "../templates";
22+
import { getIssueFromMemories, incorporateRepositoryState } from "../utils";
2123

2224
export const addCommentToIssueAction: Action = {
2325
name: "ADD_COMMENT_TO_ISSUE",
@@ -46,6 +48,7 @@ export const addCommentToIssueAction: Action = {
4648
state = await runtime.updateRecentMessageState(state);
4749
}
4850
const updatedState = await incorporateRepositoryState(state, runtime, message, []);
51+
updatedState.specificIssue = 1; // update to the specific issue number
4952
elizaLogger.info("State:", updatedState);
5053

5154
const context = composeContext({
@@ -67,18 +70,41 @@ export const addCommentToIssueAction: Action = {
6770

6871
const content = details.object as AddCommentToIssueContent;
6972

70-
elizaLogger.info("Adding comment to issue in the repository...");
71-
7273
const githubService = new GitHubService({
7374
owner: content.owner,
7475
repo: content.repo,
7576
auth: runtime.getSetting("GITHUB_API_TOKEN"),
7677
});
78+
const issue = await getIssueFromMemories(runtime, message, content.issue);
79+
updatedState.specificIssue = JSON.stringify(issue.content);
80+
const commentContext = composeContext({
81+
state: updatedState,
82+
template: generateCommentForASpecificIssueTemplate,
83+
});
84+
85+
const commentDetails = await generateObject({
86+
runtime,
87+
context: commentContext,
88+
modelClass: ModelClass.LARGE,
89+
schema: GenerateCommentForASpecificPRSchema,
90+
});
91+
92+
if (!isGenerateCommentForASpecificPRSchema(commentDetails.object)) {
93+
elizaLogger.error("Invalid comment content:", commentDetails.object);
94+
throw new Error("Invalid comment content");
95+
}
96+
97+
const commentBody = commentDetails.object.comment;
98+
99+
elizaLogger.info("Adding comment to issue in the repository...", {
100+
issue,
101+
commentBody,
102+
});
77103

78104
try {
79105
const comment = await githubService.addIssueComment(
80106
content.issue,
81-
content.comment
107+
commentBody
82108
);
83109

84110
elizaLogger.info(

packages/plugin-github/src/plugins/addCommentToPR.ts

+29-3
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,12 @@ import { GitHubService } from "../services/github";
1414
import {
1515
AddCommentToPRContent,
1616
AddCommentToPRSchema,
17+
GenerateCommentForASpecificPRSchema,
1718
isAddCommentToPRContent,
19+
isGenerateCommentForASpecificPRSchema,
1820
} from "../types";
19-
import { addCommentToPRTemplate } from "../templates";
20-
import { incorporateRepositoryState } from "../utils";
21+
import { addCommentToPRTemplate, generateCommentForASpecificPRTemplate } from "../templates";
22+
import { getPullRequestFromMemories, incorporateRepositoryState } from "../utils";
2123

2224
export const addCommentToPRAction: Action = {
2325
name: "ADD_COMMENT_TO_PR",
@@ -68,7 +70,31 @@ export const addCommentToPRAction: Action = {
6870
const content = details.object as AddCommentToPRContent;
6971

7072
elizaLogger.info("Adding comment to pull request in the repository...");
73+
const pullRequest = await getPullRequestFromMemories(runtime, message, content.pullRequest);
74+
updatedState.specificPullRequest = JSON.stringify(pullRequest.content);
75+
const commentContext = composeContext({
76+
state: updatedState,
77+
template: generateCommentForASpecificPRTemplate,
78+
});
79+
80+
const commentDetails = await generateObject({
81+
runtime,
82+
context: commentContext,
83+
modelClass: ModelClass.LARGE,
84+
schema: GenerateCommentForASpecificPRSchema,
85+
});
86+
87+
if (!isGenerateCommentForASpecificPRSchema(commentDetails.object)) {
88+
elizaLogger.error("Invalid comment content:", commentDetails.object);
89+
throw new Error("Invalid comment content");
90+
}
7191

92+
const commentBody = commentDetails.object.comment;
93+
94+
elizaLogger.info("Adding comment to pull request in the repository...", {
95+
pullRequest,
96+
commentBody,
97+
});
7298
const githubService = new GitHubService({
7399
owner: content.owner,
74100
repo: content.repo,
@@ -78,7 +104,7 @@ export const addCommentToPRAction: Action = {
78104
try {
79105
const comment = await githubService.addPRComment(
80106
content.pullRequest,
81-
content.comment
107+
commentBody
82108
);
83109

84110
elizaLogger.info(

packages/plugin-github/src/templates.ts

+27-8
Original file line numberDiff line numberDiff line change
@@ -649,36 +649,55 @@ Here are the recent user messages for context:
649649
`;
650650

651651
export const addCommentToIssueTemplate = `
652-
Extract the details for adding a comment to a GitHub issue and ensure the comment aligns with the character's goals and the user's request:
652+
Extract the details for adding a comment to a specific GitHub issue:
653653
- **owner** (string): The owner of the GitHub repository (e.g., "octocat")
654654
- **repo** (string): The name of the GitHub repository (e.g., "hello-world")
655-
- **issue_number** (number): The number of the issue (e.g., 1)
656-
- **comment** (string): The comment to add (e.g., "Add new documentation")
655+
- **issue_number** (number): The number of the issue to comment on (e.g., 1)
657656
658657
${contextTemplate}
659658
\`\`\`json
660659
{
661660
"owner": "<owner>",
662661
"repo": "<repo>",
663-
"issue_number": "<issue_number>",
662+
"issue_number": "<issue_number>"
663+
}
664+
\`\`\`
665+
`;
666+
667+
export const generateCommentForASpecificIssueTemplate = `
668+
Generate a comment for a specific GitHub issue that aligns with the character's goals and the user's request:
669+
Here is the specific issue to comment on: {{specificIssue}}
670+
${contextTemplate}
671+
\`\`\`json
672+
{
664673
"comment": "<comment>"
665674
}
666675
\`\`\`
667-
e
668676
`;
677+
669678
export const addCommentToPRTemplate = `
670-
Extract the details for adding a comment to a GitHub pull request and ensure the comment aligns with the character's goals and the user's request:
679+
Extract the details for a specific GitHub pull request:
671680
- **owner** (string): The owner of the GitHub repository (e.g., "octocat")
672681
- **repo** (string): The name of the GitHub repository (e.g., "hello-world")
673682
- **pullRequest** (number): The number of the pull request (e.g., 1)
674-
- **comment** (string): The comment to add (e.g., "Add new documentation")
675683
684+
Here is the specific pull request: {{specificPullRequest}}
676685
${contextTemplate}
677686
\`\`\`json
678687
{
679688
"owner": "<owner>",
680689
"repo": "<repo>",
681-
"pullRequest": "<pullRequest>",
690+
"pullRequest": "<pullRequest>"
691+
}
692+
\`\`\`
693+
`;
694+
695+
export const generateCommentForASpecificPRTemplate = `
696+
Generate a comment for a specific GitHub pull request that aligns with the character's goals and the user's request:
697+
Here is the specific pull request to comment on: {{specificPullRequest}}
698+
${contextTemplate}
699+
\`\`\`json
700+
{
682701
"comment": "<comment>"
683702
}
684703
\`\`\`

packages/plugin-github/src/types.ts

+12-4
Original file line numberDiff line numberDiff line change
@@ -183,14 +183,12 @@ export const AddCommentToIssueSchema = z.object({
183183
owner: z.string().min(1, "GitHub owner is required"),
184184
repo: z.string().min(1, "GitHub repo is required"),
185185
issue: z.number().min(1, "Issue number is required"),
186-
comment: z.string().min(1, "Comment is required"),
187186
});
188187

189188
export interface AddCommentToIssueContent {
190189
owner: string;
191190
repo: string;
192191
issue: number;
193-
comment: string;
194192
}
195193

196194
export const isAddCommentToIssueContent = (
@@ -219,14 +217,12 @@ export const AddCommentToPRSchema = z.object({
219217
owner: z.string().min(1, "GitHub owner is required"),
220218
repo: z.string().min(1, "GitHub repo is required"),
221219
pullRequest: z.number().min(1, "Pull request number is required"),
222-
comment: z.string().min(1, "Comment is required"),
223220
});
224221

225222
export interface AddCommentToPRContent {
226223
owner: string;
227224
repo: string;
228225
pullRequest: number;
229-
comment: string;
230226
}
231227

232228
export const isAddCommentToPRContent = (
@@ -238,3 +234,15 @@ export const isAddCommentToPRContent = (
238234
elizaLogger.error("Invalid content: ", object);
239235
return false;
240236
};
237+
238+
export const GenerateCommentForASpecificPRSchema = z.object({
239+
comment: z.string().min(1, "Comment is required"),
240+
});
241+
242+
export interface GenerateCommentForASpecificPRSchema {
243+
comment: string;
244+
}
245+
246+
export const isGenerateCommentForASpecificPRSchema = (object: any): object is GenerateCommentForASpecificPRSchema => {
247+
return GenerateCommentForASpecificPRSchema.safeParse(object).success;
248+
};

packages/plugin-github/src/utils.ts

+18
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,24 @@ export async function getIssuesFromMemories(runtime: IAgentRuntime, owner: strin
265265
return issueMemories;
266266
}
267267

268+
export const getIssueFromMemories = async (runtime: IAgentRuntime, message: Memory, issueNumber: number): Promise<Memory> => {
269+
const roomId = message.roomId;
270+
const memories = await runtime.messageManager.getMemories({
271+
roomId,
272+
});
273+
const issueId = stringToUuid(`${roomId}-${runtime.agentId}-issue-${issueNumber}`);
274+
return memories.find(memory => memory.id === issueId);
275+
}
276+
277+
export const getPullRequestFromMemories = async (runtime: IAgentRuntime, message: Memory, pullRequestNumber: number): Promise<Memory> => {
278+
const roomId = message.roomId;
279+
const memories = await runtime.messageManager.getMemories({
280+
roomId,
281+
});
282+
const prId = stringToUuid(`${roomId}-${runtime.agentId}-pr-${pullRequestNumber}`);
283+
return memories.find(memory => memory.id === prId);
284+
}
285+
268286
export async function incorporateRepositoryState(state: State, runtime: IAgentRuntime, message: Memory, relevantMemories: Memory[]) {
269287
const files = await getFilesFromMemories(runtime, message);
270288
// add additional keys to state

0 commit comments

Comments
 (0)