forked from AIObjectives/talk-to-the-city-reports
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpage_v0.ts
89 lines (80 loc) · 2.29 KB
/
webpage_v0.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
import nodes from '$lib/node_register';
import categories from '$lib/node_categories';
import type { DGNodeInterface, BaseData } from '$lib/node_data_types';
export default class WebpageNode {
id: string;
data: WebpageData;
position: { x: number; y: number };
type: string;
constructor(node_data: WebpageNodeInterface) {
const { id, data, position, type } = node_data;
this.id = id;
this.data = data;
this.position = position;
this.type = type;
}
async compute(
inputData: { text?: string },
context: string,
info: (arg: string) => void,
error: (arg: string) => void,
success: (arg: string) => void,
slug: string,
Cookies: any
) {
const url = this.data.text;
this.data.dirty = false;
if (!url) {
error('URL not provided.');
return;
}
try {
// Todo: relative URLs don't work server-side
// consider using ${import.meta.env.VITE_APP_BASE_URL}
const response = await fetch(`/api/readability?url=${encodeURIComponent(url)}`);
if (response.ok) {
const text = await response.text();
const content = text;
if (content) {
this.data.output = content;
return content;
} else {
console.error('Content not found in the response.');
error('Content not found in the response.');
}
} else {
console.error(`API call failed with status: ${response.status}`);
error(`API call failed with status: ${response.status}`);
}
} catch (e) {
console.error(`An error occurred during API call: ${(e as Error).message}`);
error(`An error occurred during API call: ${(e as Error).message}`);
}
}
}
interface WebpageData extends BaseData {
output: string;
text: string;
}
type WebpageNodeInterface = DGNodeInterface & {
data: WebpageData;
};
export const webpage_node_data: WebpageNodeInterface = {
id: 'webpage',
data: {
label: 'webpage',
dirty: false,
output: '',
text: '',
compute_type: 'webpage_v0',
input_ids: {},
category: categories.input.id,
icon: 'webpage_v0',
show_in_ui: true,
message: ''
},
position: { x: 0, y: 0 },
type: 'text_input_v0'
};
export const webpage_node = new WebpageNode(webpage_node_data);
nodes.register(WebpageNode, webpage_node_data);