Elffuss BraidHub

kikocis/continue public

b4db4cbf8943 · 3.7 KiBRawPermalink
1import {
2 ContextItem,
3 ContextProviderDescription,
4 ContextProviderExtras,
5 ContextSubmenuItem,
6 LoadSubmenuItemsArgs,
7 FetchFunction,
8} from "../../index.js";
9import { BaseContextProvider } from "../index.js";
10
11//Define helper interfaces for Discord responses
12export interface DiscordChannel {
13 id: string;
14 name?: string;
15 icon?: string;
16 topic?: string;
17 type?: number;
18 guild_id?: string;
19}
20
21interface DiscordMessage {
22 id: string;
23 content: string;
24 author: {
25 id: string;
26 username: string;
27 };
28 timestamp: string;
29}
30
31class DiscordContextProvider extends BaseContextProvider {
32 static description: ContextProviderDescription = {
33 title: "discord",
34 displayTitle: "Discord",
35 description: "Select a channel",
36 type: "submenu",
37 };
38
39 private baseUrl = "https://discord.com/api/v10";
40
41 // Helper function to get the full fetch URL
42 private getUrl(path: string): string {
43 return `${this.baseUrl}${path}`;
44 }
45
46 async fetchMessages(
47 channelId: string,
48 fetch: FetchFunction,
49 ): Promise<Array<DiscordMessage>> {
50 const url = this.getUrl(`/channels/${channelId}/messages`);
51 // Fetch messages from the specified channel using the provided fetch function
52 const response = await fetch(url, {
53 headers: {
54 Authorization: `Bot ${this.options.discordKey}`,
55 "Content-Type": "application/json",
56 },
57 });
58
59 if (!response.ok) {
60 throw new Error(`Failed to fetch messages: ${response.statusText}`);
61 }
62
63 return response.json();
64 }
65
66 async fetchChannels(fetch: FetchFunction): Promise<Array<DiscordChannel>> {
67 const url = this.getUrl(`/guilds/${this.options.guildId}/channels`);
68 const response = await fetch(url, {
69 headers: {
70 Authorization: `Bot ${this.options.discordKey}`,
71 "Content-Type": "application/json",
72 },
73 });
74
75 if (!response.ok) {
76 throw new Error(`Failed to fetch channels: ${response.statusText}`);
77 }
78
79 const channels = await response.json();
80 // Filter channels to only include text channels (type 0)
81 return channels.filter((channel: DiscordChannel) => channel.type === 0);
82 }
83
84 async getContextItems(
85 query: string,
86 extras: ContextProviderExtras,
87 ): Promise<ContextItem[]> {
88 const channels = await this.fetchChannels(extras.fetch);
89
90 let channelId: string;
91 // Find the channel by ID or name in the query string. If not found, use the first channel
92 const selectedChannel = channels.find(
93 (channel) => channel.id === query || channel.name === query,
94 );
95
96 channelId = selectedChannel ? selectedChannel.id : channels[0].id;
97
98 const parts = ["# Discord Channel Messages", `Channel ID: ${channelId}`];
99 const messages = await this.fetchMessages(channelId, extras.fetch);
100
101 if (messages.length > 0) {
102 parts.push("## Messages");
103 // Format each message into a markdown string
104 messages.forEach((message) => {
105 parts.push(
106 `### ${message.author?.username ?? "Unknown User"} on ${
107 message.timestamp
108 }\n\n${message.content}`,
109 );
110 });
111 } else {
112 parts.push("No messages found.");
113 }
114
115 const content = parts.join("\n\n");
116
117 return [
118 {
119 name: `Messages from Channel ${channelId}`,
120 content,
121 description: "Latest messages from the channel",
122 },
123 ];
124 }
125
126 async loadSubmenuItems(
127 args: LoadSubmenuItemsArgs,
128 ): Promise<ContextSubmenuItem[]> {
129 const channels = await this.fetchChannels(args.fetch);
130
131 return channels.map((channel) => ({
132 id: channel.id,
133 title: channel.name || `Channel ${channel.id}`,
134 description: channel.topic ?? "",
135 }));
136 }
137}
138
139export default DiscordContextProvider;