|
| 1 | +import { Octokit } from "@octokit/rest"; |
| 2 | +import { glob } from "glob"; |
| 3 | +import simpleGit, { SimpleGit } from "simple-git"; |
| 4 | +import path from "path"; |
| 5 | +import fs from "fs/promises"; |
| 6 | +import { existsSync } from "fs"; |
| 7 | +import { createHash } from "crypto"; |
| 8 | +import { |
| 9 | + elizaLogger, |
| 10 | + AgentRuntime, |
| 11 | + Client, |
| 12 | + IAgentRuntime, |
| 13 | + Content, |
| 14 | + Memory, |
| 15 | + stringToUuid, |
| 16 | + embeddingZeroVector, |
| 17 | + splitChunks, |
| 18 | + embed, |
| 19 | +} from "@ai16z/eliza"; |
| 20 | + |
| 21 | +export interface GitHubConfig { |
| 22 | + owner: string; |
| 23 | + repo: string; |
| 24 | + branch?: string; |
| 25 | + path?: string; |
| 26 | + token: string; |
| 27 | +} |
| 28 | + |
| 29 | +export class GitHubClient { |
| 30 | + private octokit: Octokit; |
| 31 | + private git: SimpleGit; |
| 32 | + private config: GitHubConfig; |
| 33 | + private runtime: AgentRuntime; |
| 34 | + private repoPath: string; |
| 35 | + |
| 36 | + constructor(runtime: AgentRuntime) { |
| 37 | + this.runtime = runtime; |
| 38 | + this.config = { |
| 39 | + owner: runtime.getSetting("GITHUB_OWNER") as string, |
| 40 | + repo: runtime.getSetting("GITHUB_REPO") as string, |
| 41 | + branch: runtime.getSetting("GITHUB_BRANCH") as string, |
| 42 | + path: runtime.getSetting("GITHUB_PATH") as string, |
| 43 | + token: runtime.getSetting("GITHUB_API_TOKEN") as string, |
| 44 | + }; |
| 45 | + this.octokit = new Octokit({ auth: this.config.token }); |
| 46 | + this.git = simpleGit(); |
| 47 | + this.repoPath = path.join( |
| 48 | + process.cwd(), |
| 49 | + ".repos", |
| 50 | + this.config.owner, |
| 51 | + this.config.repo |
| 52 | + ); |
| 53 | + } |
| 54 | + |
| 55 | + async initialize() { |
| 56 | + // Create repos directory if it doesn't exist |
| 57 | + await fs.mkdir(path.join(process.cwd(), ".repos", this.config.owner), { |
| 58 | + recursive: true, |
| 59 | + }); |
| 60 | + |
| 61 | + // Clone or pull repository |
| 62 | + if (!existsSync(this.repoPath)) { |
| 63 | + await this.git.clone( |
| 64 | + `https://github.com/${this.config.owner}/${this.config.repo}.git`, |
| 65 | + this.repoPath |
| 66 | + ); |
| 67 | + } else { |
| 68 | + const git = simpleGit(this.repoPath); |
| 69 | + await git.pull(); |
| 70 | + } |
| 71 | + |
| 72 | + // Checkout specified branch if provided |
| 73 | + if (this.config.branch) { |
| 74 | + const git = simpleGit(this.repoPath); |
| 75 | + await git.checkout(this.config.branch); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + async createMemoriesFromFiles() { |
| 80 | + console.log("Create memories"); |
| 81 | + const searchPath = this.config.path |
| 82 | + ? path.join(this.repoPath, this.config.path, "**/*") |
| 83 | + : path.join(this.repoPath, "**/*"); |
| 84 | + |
| 85 | + const files = await glob(searchPath, { nodir: true }); |
| 86 | + |
| 87 | + for (const file of files) { |
| 88 | + const relativePath = path.relative(this.repoPath, file); |
| 89 | + const content = await fs.readFile(file, "utf-8"); |
| 90 | + const contentHash = createHash("sha256") |
| 91 | + .update(content) |
| 92 | + .digest("hex"); |
| 93 | + const knowledgeId = stringToUuid( |
| 94 | + `github-${this.config.owner}-${this.config.repo}-${relativePath}` |
| 95 | + ); |
| 96 | + |
| 97 | + const existingDocument = |
| 98 | + await this.runtime.documentsManager.getMemoryById(knowledgeId); |
| 99 | + |
| 100 | + if ( |
| 101 | + existingDocument && |
| 102 | + existingDocument.content["hash"] == contentHash |
| 103 | + ) { |
| 104 | + continue; |
| 105 | + } |
| 106 | + |
| 107 | + console.log( |
| 108 | + "Processing knowledge for ", |
| 109 | + this.runtime.character.name, |
| 110 | + " - ", |
| 111 | + relativePath |
| 112 | + ); |
| 113 | + |
| 114 | + const memory: Memory = { |
| 115 | + id: knowledgeId, |
| 116 | + agentId: this.runtime.agentId, |
| 117 | + userId: this.runtime.agentId, |
| 118 | + roomId: this.runtime.agentId, |
| 119 | + content: { |
| 120 | + text: content, |
| 121 | + hash: contentHash, |
| 122 | + source: "github", |
| 123 | + attachments: [], |
| 124 | + metadata: { |
| 125 | + path: relativePath, |
| 126 | + repo: this.config.repo, |
| 127 | + owner: this.config.owner, |
| 128 | + }, |
| 129 | + }, |
| 130 | + embedding: embeddingZeroVector, |
| 131 | + }; |
| 132 | + |
| 133 | + await this.runtime.documentsManager.createMemory(memory); |
| 134 | + |
| 135 | + // Only split if content exceeds 4000 characters |
| 136 | + const fragments = |
| 137 | + content.length > 4000 |
| 138 | + ? await splitChunks(content, 2000, 200) |
| 139 | + : [content]; |
| 140 | + |
| 141 | + for (const fragment of fragments) { |
| 142 | + // Skip empty fragments |
| 143 | + if (!fragment.trim()) continue; |
| 144 | + |
| 145 | + // Add file path context to the fragment before embedding |
| 146 | + const fragmentWithPath = `File: ${relativePath}\n\n${fragment}`; |
| 147 | + const embedding = await embed(this.runtime, fragmentWithPath); |
| 148 | + |
| 149 | + await this.runtime.knowledgeManager.createMemory({ |
| 150 | + // We namespace the knowledge base uuid to avoid id |
| 151 | + // collision with the document above. |
| 152 | + id: stringToUuid(knowledgeId + fragment), |
| 153 | + roomId: this.runtime.agentId, |
| 154 | + agentId: this.runtime.agentId, |
| 155 | + userId: this.runtime.agentId, |
| 156 | + content: { |
| 157 | + source: knowledgeId, |
| 158 | + text: fragment, |
| 159 | + }, |
| 160 | + embedding, |
| 161 | + }); |
| 162 | + } |
| 163 | + } |
| 164 | + } |
| 165 | + |
| 166 | + async createPullRequest( |
| 167 | + title: string, |
| 168 | + branch: string, |
| 169 | + files: Array<{ path: string; content: string }>, |
| 170 | + description?: string |
| 171 | + ) { |
| 172 | + // Create new branch |
| 173 | + const git = simpleGit(this.repoPath); |
| 174 | + await git.checkout(["-b", branch]); |
| 175 | + |
| 176 | + // Write files |
| 177 | + for (const file of files) { |
| 178 | + const filePath = path.join(this.repoPath, file.path); |
| 179 | + await fs.mkdir(path.dirname(filePath), { recursive: true }); |
| 180 | + await fs.writeFile(filePath, file.content); |
| 181 | + } |
| 182 | + |
| 183 | + // Commit and push changes |
| 184 | + await git.add("."); |
| 185 | + await git.commit(title); |
| 186 | + await git.push("origin", branch); |
| 187 | + |
| 188 | + // Create PR |
| 189 | + const pr = await this.octokit.pulls.create({ |
| 190 | + owner: this.config.owner, |
| 191 | + repo: this.config.repo, |
| 192 | + title, |
| 193 | + body: description || title, |
| 194 | + head: branch, |
| 195 | + base: this.config.branch || "main", |
| 196 | + }); |
| 197 | + |
| 198 | + return pr.data; |
| 199 | + } |
| 200 | + |
| 201 | + async createCommit( |
| 202 | + message: string, |
| 203 | + files: Array<{ path: string; content: string }> |
| 204 | + ) { |
| 205 | + const git = simpleGit(this.repoPath); |
| 206 | + |
| 207 | + // Write files |
| 208 | + for (const file of files) { |
| 209 | + const filePath = path.join(this.repoPath, file.path); |
| 210 | + await fs.mkdir(path.dirname(filePath), { recursive: true }); |
| 211 | + await fs.writeFile(filePath, file.content); |
| 212 | + } |
| 213 | + |
| 214 | + // Commit and push changes |
| 215 | + await git.add("."); |
| 216 | + await git.commit(message); |
| 217 | + await git.push(); |
| 218 | + } |
| 219 | +} |
| 220 | + |
| 221 | +export const GitHubClientInterface: Client = { |
| 222 | + start: async (runtime: IAgentRuntime) => { |
| 223 | + elizaLogger.log("GitHubClientInterface start"); |
| 224 | + |
| 225 | + const client = new GitHubClient(runtime as AgentRuntime); |
| 226 | + await client.initialize(); |
| 227 | + await client.createMemoriesFromFiles(); |
| 228 | + |
| 229 | + return client; |
| 230 | + }, |
| 231 | + stop: async (runtime: IAgentRuntime) => { |
| 232 | + elizaLogger.log("GitHubClientInterface stop"); |
| 233 | + }, |
| 234 | +}; |
| 235 | + |
| 236 | +export default GitHubClientInterface; |
0 commit comments