-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
stackexchange.ts
194 lines (165 loc) Β· 5.07 KB
/
stackexchange.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
import { Tool } from "@langchain/core/tools";
export interface StackExchangeAnswer {
items: StackExchangeItem[];
has_more: boolean;
quota_max: number;
quota_remaining: number;
}
export interface StackExchangeItem {
tags: string[];
question_score: number;
is_accepted: boolean;
has_accepted_answer?: boolean;
answer_count?: number;
is_answered: boolean;
question_id: number;
item_type: string;
score: number;
last_activity_date: number;
creation_date: number;
body: string;
excerpt: string;
title: string;
answer_id?: number;
}
type StackExchangeOptions = Record<string, string | number | boolean>;
export interface StackExchangeAPIParams {
/**
* The maximum number of results to return from the search.
* Limiting to 10 to avoid context overload.
* @default 3
*/
maxResult?: number;
/**
* Which part of StackOverflows items to match against. One of 'all', 'title',
* 'body'.
* @default "all"
*/
queryType?: "all" | "title" | "body";
/**
* Additional params to pass to the StackExchange API
*/
options?: StackExchangeOptions;
/**
* Separator between question,answer pairs.
* @default "\n\n"
*/
resultSeparator?: string;
}
/**
* Class for interacting with the StackExchange API
* It extends the base Tool class to perform retrieval.
*/
export class StackExchangeAPI extends Tool {
name = "stackexchange";
description = "Stack Exchange API Implementation";
private pageSize: number;
private maxResult = 3;
private key: string | null;
private accessToken: string | null;
private site = "stackoverflow";
private version = "2.3";
private baseUrl = "https://api.stackexchange.com";
private queryType = "all";
private options?: StackExchangeOptions = {};
private resultSeparator?: string = "\n\n";
constructor(params: StackExchangeAPIParams = {}) {
const { maxResult, queryType = "all", options, resultSeparator } = params;
super();
this.maxResult = maxResult || this.maxResult;
this.pageSize = 100;
this.baseUrl = `${this.baseUrl}/${this.version}/`;
this.queryType = queryType === "all" ? "q" : queryType;
this.options = options || this.options;
this.resultSeparator = resultSeparator || this.resultSeparator;
}
async _call(query: string): Promise<string> {
const params = {
[this.queryType]: query,
site: this.site,
...this.options,
};
const output = await this._fetch<StackExchangeAnswer>(
"search/excerpts",
params
);
if (output.items.length < 1) {
return `No relevant results found for '${query}' on Stack Overflow.`;
}
const questions = output.items
.filter((item) => item.item_type === "question")
.slice(0, this.maxResult);
const answers = output.items.filter((item) => item.item_type === "answer");
const results: string[] = [];
for (const question of questions) {
let res_text = `Question: ${question.title}\n${question.excerpt}`;
const relevant_answers = answers.filter(
(answer) => answer.question_id === question.question_id
);
const accepted_answers = relevant_answers.filter(
(answer) => answer.is_accepted
);
if (relevant_answers.length > 0) {
const top_answer =
accepted_answers.length > 0
? accepted_answers[0]
: relevant_answers[0];
const { excerpt } = top_answer;
res_text += `\nAnswer: ${excerpt}`;
}
results.push(res_text);
}
return results.join(this.resultSeparator);
}
/**
* Call the StackExchange API
* @param endpoint Name of the endpoint from StackExchange API
* @param params Additional parameters passed to the endpoint
* @param page Number of the page to retrieve
* @param filter Filtering properties
*/
private async _fetch<T>(
endpoint: string,
params: StackExchangeOptions = {},
page = 1,
filter = "default"
): Promise<T> {
try {
if (!endpoint) {
throw new Error("No end point provided.");
}
const queryParams = new URLSearchParams({
pagesize: this.pageSize.toString(),
page: page.toString(),
filter,
...params,
});
if (this.key) {
queryParams.append("key", this.key);
}
if (this.accessToken) {
queryParams.append("access_token", this.accessToken);
}
const queryParamsString = queryParams.toString();
const endpointUrl = `${this.baseUrl}${endpoint}?${queryParamsString}`;
return await this._makeRequest(endpointUrl);
} catch (e) {
throw new Error("Error while calling Stack Exchange API");
}
}
/**
* Fetch the result of a specific endpoint
* @param endpointUrl Endpoint to call
*/
private async _makeRequest<T>(endpointUrl: string): Promise<T> {
try {
const response = await fetch(endpointUrl);
if (response.status !== 200) {
throw new Error(`HTTP Error: ${response.statusText}`);
}
return await response.json();
} catch (e) {
throw new Error(`Error while calling Stack Exchange API: ${endpointUrl}`);
}
}
}