Skip to content

7.4 billion tokens per month. 34 free LLM providers. 635 free model endpoints. All behind one /v1 endpoint, plus any custom OpenAI-compatible endpoint. Smart routing, automatic failover, encrypted keys. Personal experimentation only.

TypeScript

fix(keys): parse multiline quoted CSV fields (#1384)

Omansh Bhatnagar09a42828fee9
diff --git a/server/src/__tests__/lib/key-parser.test.ts b/server/src/__tests__/lib/key-parser.test.ts
index 27d1c6b8..0cae80be 100644
--- a/server/src/__tests__/lib/key-parser.test.ts
+++ b/server/src/__tests__/lib/key-parser.test.ts
@@ -188,6 +188,23 @@ describe('key parser', () => {
]);
});
+ it('parses newlines inside quoted CSV fields', () => {
+ const csv = 'platform,key,label,base_url\n"groq","gsk-test","work\nprimary",""\n';
+ expect(parseCsv(csv)).toEqual([
+ { key: 'GROQ_KEY', value: 'gsk-test', platform: 'groq', label: 'work\nprimary' },
+ ]);
+ });
+
+ it('parses multiline and normal CSV records with CRLF line endings', () => {
+ const csv = 'platform,key,label,base_url\r\n' +
+ '"groq","gsk-test","work\r\nprimary",""\r\n' +
+ '"google","AIza-test","Google Key",""\r\n';
+ expect(parseCsv(csv)).toEqual([
+ { key: 'GROQ_KEY', value: 'gsk-test', platform: 'groq', label: 'work\r\nprimary' },
+ { key: 'GOOGLE_KEY', value: 'AIza-test', platform: 'google', label: 'Google Key' },
+ ]);
+ });
+
it('parses CSV format without header', () => {
const csv = 'google,AIza-test,Google Key\n';
expect(parseCsv(csv)).toEqual([
diff --git a/server/src/lib/key-parser.ts b/server/src/lib/key-parser.ts
index 27ec13a9..7ee050a0 100644
--- a/server/src/lib/key-parser.ts
+++ b/server/src/lib/key-parser.ts
@@ -458,22 +458,50 @@ function splitCsvLine(line: string): string[] {
return fields;
}
+/**
+ * Split CSV input into complete records, keeping line breaks inside quoted
+ * fields for splitCsvLine() to parse as part of the field value.
+ */
+function splitCsvRecords(content: string): string[] {
+ const records: string[] = [];
+ let recordStart = 0;
+ let inQuotes = false;
+
+ for (let i = 0; i < content.length; i++) {
+ const ch = content[i];
+ if (ch === '"') {
+ if (inQuotes && content[i + 1] === '"') {
+ i++;
+ } else {
+ inQuotes = !inQuotes;
+ }
+ } else if (!inQuotes && (ch === '\n' || (ch === '\r' && content[i + 1] === '\n'))) {
+ records.push(content.slice(recordStart, i));
+ if (ch === '\r') i++;
+ recordStart = i + 1;
+ }
+ }
+
+ records.push(content.slice(recordStart));
+ return records.filter(record => record.trim());
+}
+
/**
* Parse CSV format: platform,key,label[,base_url] (with optional header row).
* The trailing base_url column is what makes a 'custom' row importable — an
* endpoint is identified by its URL, so a custom key without one is orphaned.
*/
export function parseCsv(content: string): KeyPair[] {
- const lines = content.split('\n').filter(l => l.trim());
- if (lines.length === 0) return [];
+ const records = splitCsvRecords(content);
+ if (records.length === 0) return [];
const result: KeyPair[] = [];
// Skip header row if it looks like a CSV header
- const startIdx = lines[0]!.toLowerCase().startsWith('platform,') ? 1 : 0;
+ const startIdx = records[0]!.toLowerCase().startsWith('platform,') ? 1 : 0;
- for (let i = startIdx; i < lines.length; i++) {
- const fields = splitCsvLine(lines[i]!);
+ for (let i = startIdx; i < records.length; i++) {
+ const fields = splitCsvLine(records[i]!);
const platform = (fields[0] ?? '').trim();
const key = (fields[1] ?? '').trim();
// Undo the export's CSV formula guard (a leading ' before =, +, -, @, tab