import { OpenAI } from "https://esm.town/v/std/openai";
const OPENAI_API_KEY = "your_openai_api_key";
export default async function main(req: Request): Promise<Response> {
const url = new URL(req.url);
const user1 = url.searchParams.get('user1') || 'ejfox';
const user2 = url.searchParams.get('user2') || 'octocat';
async function fetchUserActivity(username: string) {
const oneYearAgo = new Date(Date.now() - 365 * 24 * 60 * 60 * 1000).toISOString();
const response = await fetch(`https://api.github.com/users/${username}/events?per_page=100&since=${oneYearAgo}`);
const data = await response.json();
if (!Array.isArray(data)) {
throw new Error(`Unexpected GitHub API response for user ${username}`);
}
return data;
}
try {
const [user1Data, user2Data] = await Promise.all([
fetchUserActivity(user1),
fetchUserActivity(user2)
]);
function summarizeActivity(data: any[]) {
const repoSet = new Set();
const languageSet = new Set();
data.forEach((event: any) => {
repoSet.add(event.repo.name);
if (event.payload.commits) {
event.payload.commits.forEach((commit: any) => {
if (commit.url) {
languageSet.add("Unknown");
}
});
}
});
return {
repos: Array.from(repoSet),
languages: Array.from(languageSet)
};
}
const user1Summary = summarizeActivity(user1Data);
const user2Summary = summarizeActivity(user2Data);
const openai = new OpenAI(OPENAI_API_KEY);
const completion = await openai.chat.completions.create({
model: "gpt-3.5-turbo",
messages: [
{ role: "system", content: "You are a helpful assistant that suggests collaboration opportunities based on GitHub activity." },
{ role: "user", content: `Analyze the GitHub activity of two users and suggest collaboration opportunities:
User 1 (${user1}):
Repositories: ${user1Summary.repos.join(", ")}
Languages: ${user1Summary.languages.join(", ")}
User 2 (${user2}):
Repositories: ${user2Summary.repos.join(", ")}
Languages: ${user2Summary.languages.join(", ")}
Based on this information, suggest:
1. Potential collaborative projects they could work on together
2. Technologies they could explore or learn from each other
3. Ways they could complement each other's skills
Provide a detailed analysis and specific suggestions.` }
],
max_tokens: 500
});
const suggestions = completion.choices[0].message.content;
return new Response(suggestions, {
headers: { "Content-Type": "text/plain" }
});
} catch (error) {
return new Response(`Error: ${error.message}`, { status: 500 });
}
}