forked from AIObjectives/talk-to-the-city-reports
-
Notifications
You must be signed in to change notification settings - Fork 0
/
open_ai_key_v0.ts
85 lines (76 loc) · 2.11 KB
/
open_ai_key_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
import nodes from '$lib/node_register';
import categories from '$lib/node_categories';
import type { DGNodeInterface, BaseData } from '$lib/node_data_types';
export default class OpenAIKeyNode {
id: string;
data: OpenAIKeyData;
position: { x: number; y: number };
type: string;
constructor(node_data) {
const { id, data, position, type } = node_data;
this.id = id;
this.data = data;
this.position = position;
this.type = type;
}
async compute(
inputData: object,
context: string,
info: (arg: string) => void,
error: (arg: string) => void,
success: (arg: string) => void,
slug: string,
Cookies: any
) {
const ui_key = this.data.text;
const ui_key_is_valid = this.keyIsValid(ui_key);
this.data.dirty = false;
if (ui_key && ui_key_is_valid) {
Cookies.set('open_ai_key', ui_key);
this.data.text = 'sk-...(key hidden)';
this.data.output = ui_key;
return ui_key;
}
if (ui_key && !ui_key_is_valid) {
this.data.text = 'Invalid key';
}
const local_key = Cookies.get('open_ai_key');
const local_key_is_valid = this.keyIsValid(local_key);
if (local_key && local_key_is_valid) {
this.data.text = 'sk-...(key hidden)';
this.data.output = local_key;
return local_key;
}
this.data.text = 'Invalid key';
return '';
}
keyIsValid(key) {
return key && key.length == 51 && key.slice(0, 3) == 'sk-';
}
}
interface OpenAIKeyData extends BaseData {
text: string;
prevent_dirty: boolean;
}
type OpenAIKeyNodeInterface = DGNodeInterface & {
data: OpenAIKeyData;
};
export const open_ai_key_node_data: OpenAIKeyNodeInterface = {
id: 'open_ai_key',
data: {
label: 'open_ai_key',
text: 'sk-...',
dirty: false,
compute_type: 'open_ai_key_v0',
input_ids: {},
category: categories.input.id,
icon: 'open_ai_key_v0',
show_in_ui: true,
message: '',
prevent_dirty: true
},
position: { x: 0, y: 0 },
type: 'text_input_v0'
};
export const open_ai_key_node = new OpenAIKeyNode(open_ai_key_node_data);
nodes.register(OpenAIKeyNode, open_ai_key_node_data);