-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.ts
145 lines (119 loc) · 3.95 KB
/
index.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
import Riza from '@riza-io/api';
const riza = new Riza({});
const API_KEY_MAP = {
"elevenlabs":{name: "ELEVENLABS_API_KEY", value: process.env.ELEVENLABS_API_KEY},
"cohere":{name: "COHERE_API_KEY", value: process.env.COHERE_API_KEY},
}
const EXTENSION_MAP = {
"python": "python",
"typescript": "typescript",
}
const RUNTIME_REVISION_ID_MAP = {
"typescript": process.env.RIZA_RUNTIME_REVISION_ID_TYPESCRIPT,
"python": process.env.RIZA_RUNTIME_REVISION_ID_PYTHON,
}
export const parseContent = (content: string, language: 'typescript' | 'python'): string[] => {
const lines = content.split('\n');
const codeBlocks: string[] = [];
let inCodeBlock = false;
let currentBlock: string[] = [];
lines.forEach((line, index) => {
const trimmedLine = line.trim();
if (trimmedLine === `\`\`\`${EXTENSION_MAP[language]}`) {
inCodeBlock = true;
return;
}
if (trimmedLine === "```" && inCodeBlock) {
inCodeBlock = false;
codeBlocks.push(currentBlock.join('\n'));
currentBlock = [];
return;
}
if (inCodeBlock) {
currentBlock.push(line);
}
});
if (inCodeBlock) {
console.warn(`Warning: Found unclosed ${language} code block`);
}
return codeBlocks;
};
export const parseFile = async (filePath: string, language: 'typescript' | 'python'): Promise<string[]> => {
try {
const file = Bun.file(filePath);
const content = await file.text();
return parseContent(content, language);
} catch (error) {
console.error(`Error reading file ${filePath}:`, error);
return [];
}
};
const provider = process.argv[2] as keyof typeof API_KEY_MAP;
if (!provider) {
console.error("Please provide a provider name as the first argument");
process.exit(1);
}
const language = (process.argv[3] || 'typescript') as 'typescript' | 'python';
if (!language) {
console.error("Please provide a language as the second argument");
process.exit(1);
}
const runtimeRevisionId = RUNTIME_REVISION_ID_MAP[language];
if (!runtimeRevisionId) {
console.error(`Runtime revision ID not found for ${language}`);
process.exit(1);
}
const apiKey = API_KEY_MAP[provider].value;
if (!apiKey) {
console.error(`API key not found for ${provider}`);
process.exit(1);
}
const snippets = await parseFile(`${provider}.txt`, language);
let snippetsSkipped = 0
let successFullSnippets = 0
let failedSnippets = 0
for (let snippet of snippets) {
if (snippet.includes("YOUR_API_KEY")) {
snippet = snippet.replace("YOUR_API_KEY", apiKey);
} else if (snippet.includes("<<apiKey>>")) {
snippet = snippet.replace("<<apiKey>>", apiKey);
} else if (snippet.includes("YOUR_TOKEN")) {
snippet = snippet.replace("YOUR_TOKEN", apiKey);
} else if (snippet.includes("<YOUR API KEY>")) {
snippet = snippet.replace("<YOUR API KEY>", apiKey);
} else if (snippet.includes("<apiKey>")) {
snippet = snippet.replace("<apiKey>", apiKey);
}
console.log(snippet);
let resp;
try {
resp = await riza.command.exec({
language,
code: snippet,
runtime_revision_id: runtimeRevisionId,
env: {
[API_KEY_MAP[provider].name]: apiKey,
},
http: {
allow: [{
host: "*",
}]
},
});
if (resp.stderr?.length === 0) {
successFullSnippets += 1;
} else {
failedSnippets += 1;
}
console.log(resp.stdout);
console.error(resp.stderr);
} catch (error) {
console.error("Error executing snippet");
console.error(error);
failedSnippets += 1;
}
console.log(`${snippetsSkipped} snippets skipped`);
console.log(`${successFullSnippets} snippets succeeded`);
console.log(`${failedSnippets} snippets failed`);
await Bun.sleep(1000);
}