@redaksjon/protokoll 0.0.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.nvmrc +2 -0
- package/LICENSE +190 -0
- package/README.md +88 -0
- package/dist/arguments.js +158 -0
- package/dist/arguments.js.map +1 -0
- package/dist/constants.js +82 -0
- package/dist/constants.js.map +1 -0
- package/dist/logging.js +46 -0
- package/dist/logging.js.map +1 -0
- package/dist/main.js +5 -0
- package/dist/main.js.map +1 -0
- package/dist/phases/locate.js +55 -0
- package/dist/phases/locate.js.map +1 -0
- package/dist/phases/transcribe.js +149 -0
- package/dist/phases/transcribe.js.map +1 -0
- package/dist/processor.js +35 -0
- package/dist/processor.js.map +1 -0
- package/dist/prompt/instructions/transcribe.md +46 -0
- package/dist/prompt/personas/transcriber.md +37 -0
- package/dist/prompt/transcribe.js +41 -0
- package/dist/prompt/transcribe.js.map +1 -0
- package/dist/protokoll.js +81 -0
- package/dist/protokoll.js.map +1 -0
- package/dist/util/dates.js +96 -0
- package/dist/util/dates.js.map +1 -0
- package/dist/util/general.js +39 -0
- package/dist/util/general.js.map +1 -0
- package/dist/util/media.js +103 -0
- package/dist/util/media.js.map +1 -0
- package/dist/util/openai.js +92 -0
- package/dist/util/openai.js.map +1 -0
- package/dist/util/storage.js +135 -0
- package/dist/util/storage.js.map +1 -0
- package/docs/index.html +16 -0
- package/docs/package-lock.json +1521 -0
- package/docs/package.json +21 -0
- package/docs/vite.config.js +10 -0
- package/eslint.config.mjs +82 -0
- package/nodemon.json +14 -0
- package/output/kodrdriv/250702-1905-commit-message.md +1 -0
- package/output/kodrdriv/250702-1905-commit.request.json +14 -0
- package/output/kodrdriv/250702-1905-commit.response.json +36 -0
- package/output/kodrdriv/250702-1906-commit-message.md +1 -0
- package/output/kodrdriv/250702-1907-commit-message.md +1 -0
- package/output/kodrdriv/250702-1907-commit.request.json +14 -0
- package/output/kodrdriv/250702-1907-commit.response.json +36 -0
- package/output/kodrdriv/250716-1517-review-analysis.md +39 -0
- package/output/kodrdriv/250716-1517-review-notes.md +69 -0
- package/output/kodrdriv/250716-1518-review-analysis.md +15 -0
- package/output/kodrdriv/250716-1518-review-notes.md +67 -0
- package/output/kodrdriv/250716-1523-review-analysis.md +36 -0
- package/output/kodrdriv/250716-1523-review-notes.md +87 -0
- package/output/kodrdriv/250722-1135-commit-message.md +1 -0
- package/output/kodrdriv/250722-1331-commit-message.md +1 -0
- package/output/kodrdriv/250722-1335-commit-message.md +1 -0
- package/output/kodrdriv/250722-1337-commit-message.md +1 -0
- package/output/kodrdriv/250722-1342-release-notes.md +26 -0
- package/output/kodrdriv/250722-1416-commit-message.md +3 -0
- package/output/kodrdriv/250722-1420-commit-message.md +1 -0
- package/output/kodrdriv/250722-1422-commit-message.md +1 -0
- package/output/kodrdriv/250722-1423-commit-message.md +1 -0
- package/output/kodrdriv/250722-1425-release-notes.md +41 -0
- package/output/kodrdriv/250722-1527-commit-message.md +13 -0
- package/output/kodrdriv/250722-1532-commit-message.md +1 -0
- package/output/kodrdriv/250722-1532-release-notes.md +32 -0
- package/output/kodrdriv/250722-2314-review-analysis.md +28 -0
- package/output/kodrdriv/250722-2314-review-notes.md +464 -0
- package/output/kodrdriv/250722-2315-review-analysis.md +28 -0
- package/output/kodrdriv/250722-2315-review-notes.md +477 -0
- package/output/kodrdriv/250804-1623-review-analysis.md +38 -0
- package/output/kodrdriv/250804-1623-review-notes.md +479 -0
- package/output/kodrdriv/250804-1638-review-analysis.md +56 -0
- package/output/kodrdriv/250804-1638-review-notes.md +502 -0
- package/output/kodrdriv/250812-2021-review-analysis.md +27 -0
- package/output/kodrdriv/250812-2021-review-notes.md +571 -0
- package/output/kodrdriv/250826-0700-commit-message.md +12 -0
- package/output/kodrdriv/RELEASE_NOTES.md +30 -0
- package/output/kodrdriv/RELEASE_TITLE.md +1 -0
- package/package.json +78 -0
- package/tsconfig.tsbuildinfo +1 -0
- package/vite.config.ts +124 -0
- package/vitest.config.ts +30 -0
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import dayjs from 'dayjs';
|
|
2
|
+
import timezone from 'dayjs/plugin/timezone.js';
|
|
3
|
+
import utc from 'dayjs/plugin/utc.js';
|
|
4
|
+
import 'moment-timezone';
|
|
5
|
+
|
|
6
|
+
// eslint-disable-next-line no-restricted-imports
|
|
7
|
+
dayjs.extend(utc);
|
|
8
|
+
dayjs.extend(timezone);
|
|
9
|
+
const create = (parameters)=>{
|
|
10
|
+
const { timezone } = parameters;
|
|
11
|
+
const now = ()=>{
|
|
12
|
+
return date(undefined);
|
|
13
|
+
};
|
|
14
|
+
const date = (date)=>{
|
|
15
|
+
let value;
|
|
16
|
+
try {
|
|
17
|
+
if (date) {
|
|
18
|
+
value = dayjs.tz(date, timezone);
|
|
19
|
+
} else {
|
|
20
|
+
value = dayjs().tz(timezone);
|
|
21
|
+
}
|
|
22
|
+
} catch (error) {
|
|
23
|
+
throw new Error(`Invalid date: ${date}, error: ${error.message}`);
|
|
24
|
+
}
|
|
25
|
+
return value.toDate();
|
|
26
|
+
};
|
|
27
|
+
const parse = (date, format)=>{
|
|
28
|
+
let value;
|
|
29
|
+
try {
|
|
30
|
+
value = dayjs.tz(date, format, timezone);
|
|
31
|
+
} catch (error) {
|
|
32
|
+
throw new Error(`Invalid date: ${date}, expected format: ${format}, error: ${error.message}`);
|
|
33
|
+
}
|
|
34
|
+
return value.toDate();
|
|
35
|
+
};
|
|
36
|
+
const addDays = (date, days)=>{
|
|
37
|
+
return dayjs.tz(date, timezone).add(days, 'day').toDate();
|
|
38
|
+
};
|
|
39
|
+
const addMonths = (date, months)=>{
|
|
40
|
+
return dayjs.tz(date, timezone).add(months, 'month').toDate();
|
|
41
|
+
};
|
|
42
|
+
const addYears = (date, years)=>{
|
|
43
|
+
return dayjs.tz(date, timezone).add(years, 'year').toDate();
|
|
44
|
+
};
|
|
45
|
+
const format = (date, format)=>{
|
|
46
|
+
return dayjs.tz(date, timezone).format(format);
|
|
47
|
+
};
|
|
48
|
+
const subDays = (date, days)=>{
|
|
49
|
+
return dayjs.tz(date, timezone).subtract(days, 'day').toDate();
|
|
50
|
+
};
|
|
51
|
+
const subMonths = (date, months)=>{
|
|
52
|
+
return dayjs.tz(date, timezone).subtract(months, 'month').toDate();
|
|
53
|
+
};
|
|
54
|
+
const subYears = (date, years)=>{
|
|
55
|
+
return dayjs.tz(date, timezone).subtract(years, 'year').toDate();
|
|
56
|
+
};
|
|
57
|
+
const startOfMonth = (date)=>{
|
|
58
|
+
return dayjs.tz(date, timezone).startOf('month').toDate();
|
|
59
|
+
};
|
|
60
|
+
const endOfMonth = (date)=>{
|
|
61
|
+
return dayjs.tz(date, timezone).endOf('month').toDate();
|
|
62
|
+
};
|
|
63
|
+
const startOfYear = (date)=>{
|
|
64
|
+
return dayjs.tz(date, timezone).startOf('year').toDate();
|
|
65
|
+
};
|
|
66
|
+
const endOfYear = (date)=>{
|
|
67
|
+
return dayjs.tz(date, timezone).endOf('year').toDate();
|
|
68
|
+
};
|
|
69
|
+
const isBefore = (date, other)=>{
|
|
70
|
+
return dayjs.tz(date, timezone).isBefore(dayjs.tz(other, timezone));
|
|
71
|
+
};
|
|
72
|
+
const isAfter = (date, other)=>{
|
|
73
|
+
return dayjs.tz(date, timezone).isAfter(dayjs.tz(other, timezone));
|
|
74
|
+
};
|
|
75
|
+
return {
|
|
76
|
+
now,
|
|
77
|
+
date,
|
|
78
|
+
parse,
|
|
79
|
+
addDays,
|
|
80
|
+
addMonths,
|
|
81
|
+
addYears,
|
|
82
|
+
format,
|
|
83
|
+
subDays,
|
|
84
|
+
subMonths,
|
|
85
|
+
subYears,
|
|
86
|
+
startOfMonth,
|
|
87
|
+
endOfMonth,
|
|
88
|
+
startOfYear,
|
|
89
|
+
endOfYear,
|
|
90
|
+
isBefore,
|
|
91
|
+
isAfter
|
|
92
|
+
};
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
export { create };
|
|
96
|
+
//# sourceMappingURL=dates.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"dates.js","sources":["../../src/util/dates.ts"],"sourcesContent":["// eslint-disable-next-line no-restricted-imports\nimport dayjs from 'dayjs';\nimport timezone from 'dayjs/plugin/timezone';\nimport utc from 'dayjs/plugin/utc';\n// eslint-disable-next-line no-restricted-imports\nimport moment from 'moment-timezone';\n\ndayjs.extend(utc);\ndayjs.extend(timezone);\n\n/**\n * Yes, wrapping dayjs is a bit annoying and might seem overly paranoid. However, I feel strongly\n * about not letting Dayjs instances leak into the rest of the codebase. Having Dayjs objects\n * floating around the application leads to inconsistent timezone handling, makes testing more\n * difficult, and creates subtle bugs that are hard to track down.\n * \n * By wrapping dayjs completely and only exposing plain JavaScript Date objects, we get several\n * key benefits:\n * 1. Consistent timezone handling through a single configuration point\n * 2. Simpler testing since we only need to mock this one library\n * 3. Type safety - the rest of the codebase only deals with standard Date objects\n * 4. No risk of dayjs method chains creating unexpected timezone shifts\n * \n * The Library interface gives us full control over all date operations while keeping the messy\n * details of timezone manipulation contained in one place. Yes it's more code, but the peace of\n * mind is worth it.\n */\nexport interface Utility {\n now: () => Date;\n date: (date: string | number | Date | null | undefined) => Date;\n parse: (date: string | number | Date | null | undefined, format: string) => Date;\n addDays: (date: Date, days: number) => Date;\n addMonths: (date: Date, months: number) => Date;\n addYears: (date: Date, years: number) => Date;\n format: (date: Date, format: string) => string;\n subDays: (date: Date, days: number) => Date;\n subMonths: (date: Date, months: number) => Date;\n subYears: (date: Date, years: number) => Date;\n startOfMonth: (date: Date) => Date;\n endOfMonth: (date: Date) => Date;\n startOfYear: (date: Date) => Date;\n endOfYear: (date: Date) => Date;\n isBefore: (date: Date, other: Date) => boolean;\n isAfter: (date: Date, other: Date) => boolean;\n}\n\nexport const create = (parameters: { timezone: string }) => {\n const { timezone } = parameters;\n const now = () => {\n return date(undefined);\n }\n\n const date = (date: string | number | Date | null | undefined) => {\n let value: dayjs.Dayjs;\n try {\n if (date) {\n value = dayjs.tz(date, timezone);\n } else {\n value = dayjs().tz(timezone);\n }\n } catch (error: any) {\n throw new Error(`Invalid date: ${date}, error: ${error.message}`);\n }\n\n return value.toDate();\n }\n\n const parse = (date: string | number | Date | null | undefined, format: string) => {\n let value: dayjs.Dayjs;\n try {\n value = dayjs.tz(date, format, timezone);\n } catch (error: any) {\n throw new Error(`Invalid date: ${date}, expected format: ${format}, error: ${error.message}`);\n }\n\n return value.toDate();\n }\n\n const addDays = (date: Date, days: number) => {\n return dayjs.tz(date, timezone).add(days, 'day').toDate();\n }\n\n const addMonths = (date: Date, months: number) => {\n return dayjs.tz(date, timezone).add(months, 'month').toDate();\n }\n\n const addYears = (date: Date, years: number) => {\n return dayjs.tz(date, timezone).add(years, 'year').toDate();\n }\n\n const format = (date: Date, format: string) => {\n return dayjs.tz(date, timezone).format(format);\n }\n\n const subDays = (date: Date, days: number) => {\n return dayjs.tz(date, timezone).subtract(days, 'day').toDate();\n }\n\n const subMonths = (date: Date, months: number) => {\n return dayjs.tz(date, timezone).subtract(months, 'month').toDate();\n }\n\n const subYears = (date: Date, years: number) => {\n return dayjs.tz(date, timezone).subtract(years, 'year').toDate();\n }\n\n const startOfMonth = (date: Date) => {\n return dayjs.tz(date, timezone).startOf('month').toDate();\n }\n\n const endOfMonth = (date: Date) => {\n return dayjs.tz(date, timezone).endOf('month').toDate();\n }\n\n const startOfYear = (date: Date) => {\n return dayjs.tz(date, timezone).startOf('year').toDate();\n }\n\n const endOfYear = (date: Date) => {\n return dayjs.tz(date, timezone).endOf('year').toDate();\n }\n\n const isBefore = (date: Date, other: Date) => {\n return dayjs.tz(date, timezone).isBefore(dayjs.tz(other, timezone));\n }\n\n const isAfter = (date: Date, other: Date) => {\n return dayjs.tz(date, timezone).isAfter(dayjs.tz(other, timezone));\n }\n\n return { now, date, parse, addDays, addMonths, addYears, format, subDays, subMonths, subYears, startOfMonth, endOfMonth, startOfYear, endOfYear, isBefore, isAfter };\n}\n\nexport const validTimezones = () => {\n return moment.tz.names();\n}\n"],"names":["dayjs","extend","utc","timezone","create","parameters","now","date","undefined","value","tz","error","Error","message","toDate","parse","format","addDays","days","add","addMonths","months","addYears","years","subDays","subtract","subMonths","subYears","startOfMonth","startOf","endOfMonth","endOf","startOfYear","endOfYear","isBefore","other","isAfter"],"mappings":";;;;;AAAA;AAOAA,KAAAA,CAAMC,MAAM,CAACC,GAAAA,CAAAA;AACbF,KAAAA,CAAMC,MAAM,CAACE,QAAAA,CAAAA;AAsCN,MAAMC,SAAS,CAACC,UAAAA,GAAAA;IACnB,MAAM,EAAEF,QAAQ,EAAE,GAAGE,UAAAA;AACrB,IAAA,MAAMC,GAAAA,GAAM,IAAA;AACR,QAAA,OAAOC,IAAAA,CAAKC,SAAAA,CAAAA;AAChB,IAAA,CAAA;AAEA,IAAA,MAAMD,OAAO,CAACA,IAAAA,GAAAA;QACV,IAAIE,KAAAA;QACJ,IAAI;AACA,YAAA,IAAIF,IAAAA,EAAM;gBACNE,KAAAA,GAAQT,KAAAA,CAAMU,EAAE,CAACH,IAAAA,EAAMJ,QAAAA,CAAAA;YAC3B,CAAA,MAAO;gBACHM,KAAAA,GAAQT,KAAAA,EAAAA,CAAQU,EAAE,CAACP,QAAAA,CAAAA;AACvB,YAAA;AACJ,QAAA,CAAA,CAAE,OAAOQ,KAAAA,EAAY;YACjB,MAAM,IAAIC,KAAAA,CAAM,CAAC,cAAc,EAAEL,KAAK,SAAS,EAAEI,KAAAA,CAAME,OAAO,CAAA,CAAE,CAAA;AACpE,QAAA;AAEA,QAAA,OAAOJ,MAAMK,MAAM,EAAA;AACvB,IAAA,CAAA;IAEA,MAAMC,KAAAA,GAAQ,CAACR,IAAAA,EAAiDS,MAAAA,GAAAA;QAC5D,IAAIP,KAAAA;QACJ,IAAI;AACAA,YAAAA,KAAAA,GAAQT,KAAAA,CAAMU,EAAE,CAACH,IAAAA,EAAMS,MAAAA,EAAQb,QAAAA,CAAAA;AACnC,QAAA,CAAA,CAAE,OAAOQ,KAAAA,EAAY;AACjB,YAAA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AAOhC,QAAAA,CAAAA,CAAAA;AAC7D,IAAA,CAAA;IAEA,MAAMiC,OAAAA,GAAU,CAAC7B,IAAAA,EAAY4B,KAAAA,GAAAA;QACzB,OAAOnC,KAAAA,CAAMU,EAAE,CAACH,IAAAA,EAAMJ,QAAAA,CAAAA,CAAUiC,OAAO,CAACpC,KAAAA,CAAMU,EAAE,CAACyB,KAAAA,EAAOhC,QAAAA,CAAAA,CAAAA;AAC5D,IAAA,CAAA;IAEA,OAAO;AAAEG,QAAAA,GAAAA;AAAKC,QAAAA,IAAAA;AAAMQ,QAAAA,KAAAA;AAAOE,QAAAA,OAAAA;AAASG,QAAAA,SAAAA;AAAWE,QAAAA,QAAAA;AAAUN,QAAAA,MAAAA;AAAQQ,QAAAA,OAAAA;AAASE,QAAAA,SAAAA;AAAWC,QAAAA,QAAAA;AAAUC,QAAAA,YAAAA;AAAcE,QAAAA,UAAAA;AAAYE,QAAAA,WAAAA;AAAaC,QAAAA,SAAAA;AAAWC,QAAAA,QAAAA;AAAUE,QAAAA;AAAQ,KAAA;AACvK;;;;"}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
// Utility function for deep merging two objects.
|
|
2
|
+
//Recursive implementation of jSON.stringify;
|
|
3
|
+
const stringifyJSON = function(obj) {
|
|
4
|
+
const arrOfKeyVals = [];
|
|
5
|
+
const arrVals = [];
|
|
6
|
+
let objKeys = [];
|
|
7
|
+
/*********CHECK FOR PRIMITIVE TYPES**********/ if (typeof obj === 'number' || typeof obj === 'boolean' || obj === null) return '' + obj;
|
|
8
|
+
else if (typeof obj === 'string') return '"' + obj + '"';
|
|
9
|
+
else if (Array.isArray(obj)) {
|
|
10
|
+
//check for empty array
|
|
11
|
+
if (obj[0] === undefined) return '[]';
|
|
12
|
+
else {
|
|
13
|
+
obj.forEach(function(el) {
|
|
14
|
+
arrVals.push(stringifyJSON(el));
|
|
15
|
+
});
|
|
16
|
+
return '[' + arrVals + ']';
|
|
17
|
+
}
|
|
18
|
+
} else if (obj instanceof Object) {
|
|
19
|
+
//get object keys
|
|
20
|
+
objKeys = Object.keys(obj);
|
|
21
|
+
//set key output;
|
|
22
|
+
objKeys.forEach(function(key) {
|
|
23
|
+
const keyOut = '"' + key + '":';
|
|
24
|
+
const keyValOut = obj[key];
|
|
25
|
+
//skip functions and undefined properties
|
|
26
|
+
if (keyValOut instanceof Function || keyValOut === undefined) arrOfKeyVals.push('');
|
|
27
|
+
else if (typeof keyValOut === 'string') arrOfKeyVals.push(keyOut + '"' + keyValOut + '"');
|
|
28
|
+
else if (typeof keyValOut === 'boolean' || typeof keyValOut === 'number' || keyValOut === null) arrOfKeyVals.push(keyOut + keyValOut);
|
|
29
|
+
else if (keyValOut instanceof Object) {
|
|
30
|
+
arrOfKeyVals.push(keyOut + stringifyJSON(keyValOut));
|
|
31
|
+
}
|
|
32
|
+
});
|
|
33
|
+
return '{' + arrOfKeyVals + '}';
|
|
34
|
+
}
|
|
35
|
+
return '';
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
export { stringifyJSON };
|
|
39
|
+
//# sourceMappingURL=general.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"general.js","sources":["../../src/util/general.ts"],"sourcesContent":["// Utility function for deep merging two objects.\nexport function deepMerge(target: any, source: any): any {\n for (const key in source) {\n if (Object.prototype.hasOwnProperty.call(source, key)) {\n // Block prototype-polluting keys\n if (key === '__proto__' || key === 'constructor') {\n continue;\n }\n if (source[key] && typeof source[key] === 'object' && !Array.isArray(source[key])) {\n if (!target[key]) {\n target[key] = {};\n }\n deepMerge(target[key], source[key]);\n } else {\n target[key] = source[key];\n }\n }\n }\n return target;\n}\n\n//Recursive implementation of jSON.stringify;\nexport const stringifyJSON = function (obj: any): string {\n\n const arrOfKeyVals: string[] = [];\n const arrVals: string[] = [];\n let objKeys: string[] = [];\n\n /*********CHECK FOR PRIMITIVE TYPES**********/\n if (typeof obj === 'number' || typeof obj === 'boolean' || obj === null)\n return '' + obj;\n else if (typeof obj === 'string')\n return '\"' + obj + '\"';\n\n /*********CHECK FOR ARRAY**********/\n else if (Array.isArray(obj)) {\n //check for empty array\n if (obj[0] === undefined)\n return '[]';\n else {\n obj.forEach(function (el) {\n arrVals.push(stringifyJSON(el));\n });\n return '[' + arrVals + ']';\n }\n }\n /*********CHECK FOR OBJECT**********/\n else if (obj instanceof Object) {\n //get object keys\n objKeys = Object.keys(obj);\n //set key output;\n objKeys.forEach(function (key) {\n const keyOut = '\"' + key + '\":';\n const keyValOut = obj[key];\n //skip functions and undefined properties\n if (keyValOut instanceof Function || keyValOut === undefined)\n arrOfKeyVals.push('');\n else if (typeof keyValOut === 'string')\n arrOfKeyVals.push(keyOut + '\"' + keyValOut + '\"');\n else if (typeof keyValOut === 'boolean' || typeof keyValOut === 'number' || keyValOut === null)\n arrOfKeyVals.push(keyOut + keyValOut);\n //check for nested objects, call recursively until no more objects\n else if (keyValOut instanceof Object) {\n arrOfKeyVals.push(keyOut + stringifyJSON(keyValOut));\n }\n });\n return '{' + arrOfKeyVals + '}';\n }\n return '';\n};"],"names":["stringifyJSON","obj","arrOfKeyVals","arrVals","objKeys","Array","isArray","undefined","forEach","el","push","Object","keys","key","keyOut","keyValOut","Function"],"mappings":"AAAA;AAqBA;AACO,MAAMA,aAAAA,GAAgB,SAAUC,GAAQ,EAAA;AAE3C,IAAA,MAAMC,eAAyB,EAAE;AACjC,IAAA,MAAMC,UAAoB,EAAE;AAC5B,IAAA,IAAIC,UAAoB,EAAE;mDAG1B,IAAI,OAAOH,GAAAA,KAAQ,QAAA,IAAY,OAAOA,GAAAA,KAAQ,SAAA,IAAaA,GAAAA,KAAQ,IAAA,EAC/D,OAAO,EAAA,GAAKA,GAAAA;AACX,SAAA,IAAI,OAAOA,GAAAA,KAAQ,QAAA,EACpB,OAAO,MAAMA,GAAAA,GAAM,GAAA;SAGlB,IAAII,KAAAA,CAAMC,OAAO,CAACL,GAAAA,CAAAA,EAAM;;AAEzB,QAAA,IAAIA,GAA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ent-ffmpeg';
|
|
2
|
+
import path from 'path';
|
|
3
|
+
import { create as create$1 } from './storage.js';
|
|
4
|
+
|
|
5
|
+
const ffprobeAsync = (filePath)=>{
|
|
6
|
+
return new Promise((resolve, reject)=>{
|
|
7
|
+
ffmpeg.ffprobe(filePath, (err, metadata)=>{
|
|
8
|
+
if (err) return reject(err);
|
|
9
|
+
resolve(metadata);
|
|
10
|
+
});
|
|
11
|
+
});
|
|
12
|
+
};
|
|
13
|
+
const create = (logger)=>{
|
|
14
|
+
const storage = create$1({
|
|
15
|
+
log: logger.debug
|
|
16
|
+
});
|
|
17
|
+
// Extract creation time from audio file using ffmpeg
|
|
18
|
+
const getAudioCreationTime = async (filePath)=>{
|
|
19
|
+
try {
|
|
20
|
+
var _metadata_format, _metadata_streams;
|
|
21
|
+
const metadata = await ffprobeAsync(filePath);
|
|
22
|
+
// Look for creation_time in format tags
|
|
23
|
+
const formatTags = metadata === null || metadata === void 0 ? void 0 : (_metadata_format = metadata.format) === null || _metadata_format === void 0 ? void 0 : _metadata_format.tags;
|
|
24
|
+
if (formatTags === null || formatTags === void 0 ? void 0 : formatTags.creation_time) {
|
|
25
|
+
logger.debug('Found creation_time in format tags: %s', formatTags.creation_time);
|
|
26
|
+
return new Date(formatTags.creation_time);
|
|
27
|
+
}
|
|
28
|
+
// Check for creation_time in stream tags as fallback
|
|
29
|
+
if ((metadata === null || metadata === void 0 ? void 0 : (_metadata_streams = metadata.streams) === null || _metadata_streams === void 0 ? void 0 : _metadata_streams.length) > 0) {
|
|
30
|
+
for (const stream of metadata.streams){
|
|
31
|
+
var _stream_tags;
|
|
32
|
+
if ((_stream_tags = stream.tags) === null || _stream_tags === void 0 ? void 0 : _stream_tags.creation_time) {
|
|
33
|
+
logger.debug('Found creation_time in stream tags: %s', stream.tags.creation_time);
|
|
34
|
+
return new Date(stream.tags.creation_time);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
logger.debug('No creation_time found in audio file metadata');
|
|
39
|
+
return null;
|
|
40
|
+
} catch (error) {
|
|
41
|
+
logger.error('Error extracting creation time from audio file: %s', error);
|
|
42
|
+
return null;
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
// Get file size in bytes
|
|
46
|
+
const getFileSize = async (filePath)=>{
|
|
47
|
+
try {
|
|
48
|
+
return await storage.getFileSize(filePath);
|
|
49
|
+
} catch (error) {
|
|
50
|
+
logger.error('Error getting file size: %s', error);
|
|
51
|
+
throw new Error(`Failed to get file size for ${filePath}: ${error}`);
|
|
52
|
+
}
|
|
53
|
+
};
|
|
54
|
+
// Split large audio file into smaller chunks
|
|
55
|
+
const splitAudioFile = async (filePath, outputDir, maxSizeBytes)=>{
|
|
56
|
+
try {
|
|
57
|
+
const metadata = await ffprobeAsync(filePath);
|
|
58
|
+
const duration = parseFloat(metadata.format.duration);
|
|
59
|
+
// Calculate how many segments we need based on file size and max size
|
|
60
|
+
const fileSize = await getFileSize(filePath);
|
|
61
|
+
const segmentCount = Math.ceil(fileSize / maxSizeBytes);
|
|
62
|
+
// Calculate segment duration
|
|
63
|
+
const segmentDuration = duration / segmentCount;
|
|
64
|
+
logger.debug(`Splitting ${filePath} (${fileSize} bytes) into ${segmentCount} segments of ~${segmentDuration} seconds each`);
|
|
65
|
+
// Create output directory if it doesn't exist
|
|
66
|
+
await storage.createDirectory(outputDir);
|
|
67
|
+
const outputFiles = [];
|
|
68
|
+
const fileExt = path.extname(filePath);
|
|
69
|
+
const fileName = path.basename(filePath, fileExt);
|
|
70
|
+
// Create a promise for each segment
|
|
71
|
+
const promises = [];
|
|
72
|
+
for(let i = 0; i < segmentCount; i++){
|
|
73
|
+
const startTime = i * segmentDuration;
|
|
74
|
+
const outputPath = path.join(outputDir, `${fileName}_part${i + 1}${fileExt}`);
|
|
75
|
+
outputFiles.push(outputPath);
|
|
76
|
+
const promise = new Promise((resolve, reject)=>{
|
|
77
|
+
ffmpeg(filePath).setStartTime(startTime).setDuration(segmentDuration).output(outputPath).on('end', ()=>{
|
|
78
|
+
logger.debug(`Created segment ${i + 1}/${segmentCount}: ${outputPath}`);
|
|
79
|
+
resolve();
|
|
80
|
+
}).on('error', (err)=>{
|
|
81
|
+
logger.error(`Error creating segment ${i + 1}/${segmentCount}: ${err}`);
|
|
82
|
+
reject(err);
|
|
83
|
+
}).run();
|
|
84
|
+
});
|
|
85
|
+
promises.push(promise);
|
|
86
|
+
}
|
|
87
|
+
// Wait for all segments to be created
|
|
88
|
+
await Promise.all(promises);
|
|
89
|
+
return outputFiles;
|
|
90
|
+
} catch (error) {
|
|
91
|
+
logger.error('Error splitting audio file: %s', error);
|
|
92
|
+
throw new Error(`Failed to split audio file ${filePath}: ${error}`);
|
|
93
|
+
}
|
|
94
|
+
};
|
|
95
|
+
return {
|
|
96
|
+
getAudioCreationTime,
|
|
97
|
+
getFileSize,
|
|
98
|
+
splitAudioFile
|
|
99
|
+
};
|
|
100
|
+
};
|
|
101
|
+
|
|
102
|
+
export { create };
|
|
103
|
+
//# sourceMappingURL=media.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"media.js","sources":["../../src/util/media.ts"],"sourcesContent":["import ffmpeg from 'fluent-ffmpeg';\nimport { Logger } from 'winston';\nimport path from 'path';\nimport * as Storage from '@/util/storage';\n\nexport interface Media {\n getAudioCreationTime: (filePath: string) => Promise<Date | null>;\n getFileSize: (filePath: string) => Promise<number>;\n splitAudioFile: (filePath: string, outputDir: string, maxSizeBytes: number) => Promise<string[]>;\n}\n\nconst ffprobeAsync = (filePath: string): Promise<any> => {\n return new Promise((resolve, reject) => {\n ffmpeg.ffprobe(filePath, (err, metadata) => {\n if (err) return reject(err);\n resolve(metadata);\n });\n });\n};\n\n\nexport const create = (logger: Logger): Media => {\n const storage = Storage.create({ log: logger.debug });\n\n // Extract creation time from audio file using ffmpeg\n const getAudioCreationTime = async (filePath: string): Promise<Date | null> => {\n try {\n const metadata = await ffprobeAsync(filePath);\n\n // Look for creation_time in format tags\n const formatTags = metadata?.format?.tags;\n if (formatTags?.creation_time) {\n logger.debug('Found creation_time in format tags: %s', formatTags.creation_time);\n return new Date(formatTags.creation_time);\n }\n\n // Check for creation_time in stream tags as fallback\n if (metadata?.streams?.length > 0) {\n for (const stream of metadata.streams) {\n if (stream.tags?.creation_time) {\n logger.debug('Found creation_time in stream tags: %s', stream.tags.creation_time);\n return new Date(stream.tags.creation_time);\n }\n }\n }\n\n logger.debug('No creation_time found in audio file metadata');\n return null;\n } catch (error) {\n logger.error('Error extracting creation time from audio file: %s', error);\n return null;\n }\n };\n\n // Get file size in bytes\n const getFileSize = async (filePath: string): Promise<number> => {\n try {\n return await storage.getFileSize(filePath);\n } catch (error) {\n logger.error('Error getting file size: %s', error);\n throw new Error(`Failed to get file size for ${filePath}: ${error}`);\n }\n };\n\n // Split large audio file into smaller chunks\n const splitAudioFile = async (filePath: string, outputDir: string, maxSizeBytes: number): Promise<string[]> => {\n try {\n const metadata = await ffprobeAsync(filePath);\n const duration = parseFloat(metadata.format.duration);\n\n // Calculate how many segments we need based on file size and max size\n const fileSize = await getFileSize(filePath);\n const segmentCount = Math.ceil(fileSize / maxSizeBytes);\n\n // Calculate segment duration\n const segmentDuration = duration / segmentCount;\n logger.debug(`Splitting ${filePath} (${fileSize} bytes) into ${segmentCount} segments of ~${segmentDuration} seconds each`);\n\n // Create output directory if it doesn't exist\n await storage.createDirectory(outputDir);\n\n const outputFiles: string[] = [];\n const fileExt = path.extname(filePath);\n const fileName = path.basename(filePath, fileExt);\n\n // Create a promise for each segment\n const promises = [];\n\n for (let i = 0; i < segmentCount; i++) {\n const startTime = i * segmentDuration;\n const outputPath = path.join(outputDir, `${fileName}_part${i + 1}${fileExt}`);\n outputFiles.push(outputPath);\n\n const promise = new Promise<void>((resolve, reject) => {\n ffmpeg(filePath)\n .setStartTime(startTime)\n .setDuration(segmentDuration)\n .output(outputPath)\n .on('end', () => {\n logger.debug(`Created segment ${i + 1}/${segmentCount}: ${outputPath}`);\n resolve();\n })\n .on('error', (err) => {\n logger.error(`Error creating segment ${i + 1}/${segmentCount}: ${err}`);\n reject(err);\n })\n .run();\n });\n\n promises.push(promise);\n }\n\n // Wait for all segments to be created\n await Promise.all(promises);\n return outputFiles;\n } catch (error) {\n logger.error('Error splitting audio file: %s', error);\n throw new Error(`Failed to split audio file ${filePath}: ${error}`);\n }\n };\n\n return {\n getAudioCreationTime,\n getFileSize,\n splitAudioFile,\n }\n}\n"],"names":["ffprobeAsync","filePath","Promise","resolve","reject","ffmpeg","ffprobe","err","metadata","create","logger","storage","Storage","log","debug","getAudioCreationTime","formatTags","format","tags","creation_time","Date","streams","length","stream","error","getFileSize","Error","splitAudioFile","outputDir","maxSizeBytes","duration","parseFloat","fileSize","segmentCount","Math","ceil","segmentDuration","createDirectory","outputFiles","fileExt","path","extname","fileName","basename","promises","i","startTime","outputPath","join","push","promise","setStartTime","setDuration","output","on","run","all"],"mappings":";;;;AAWA,MAAMA,eAAe,CAACC,QAAAA,GAAAA;IAClB,OAAO,IAAIC,OAAAA,CAAQ,CAACC,OAAAA,EAASC,MAAAA,GAAAA;AACzBC,QAAAA,MAAAA,CAAOC,OAAO,CAACL,QAAAA,EAAU,CAACM,GAAAA,EAAKC,QAAAA,GAAAA;YAC3B,IAAID,GAAAA,EAAK,OAAOH,MAAAA,CAAOG,GAAAA,CAAAA;YACvBJ,OAAAA,CAAQK,QAAAA,CAAAA;AACZ,QAAA,CAAA,CAAA;AACJ,IAAA,CAAA,CAAA;AACJ,CAAA;AAGO,MAAMC,SAAS,CAACC,MAAAA,GAAAA;IACnB,MAAMC,OAAAA,GAAUC,QAAc,CAAC;AAAEC,QAAAA,GAAAA,EAAKH,OAAOI;AAAM,KAAA,CAAA;;AAGnD,IAAA,MAAMC,uBAAuB,OAAOd,QAAAA,GAAAA;QAChC,IAAI;gBAImBO,gBAAAA,EAOfA,iBAAAA;YAVJ,MAAMA,QAAAA,GAAW,MAAMR,YAAAA,CAAaC,QAAAA,CAAAA;;YAGpC,M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,KAAK,CAAC,CAAC,uBAAuB,EAAEqB,CAAAA,GAAI,CAAA,CAAE,CAAC,EAAEZ,YAAAA,CAAa,EAAE,EAAE1B,GAAAA,CAAAA,CAAK,CAAA;wBACtEH,MAAAA,CAAOG,GAAAA,CAAAA;AACX,oBAAA,CAAA,CAAA,CACCgD,GAAG,EAAA;AACZ,gBAAA,CAAA,CAAA;AAEAX,gBAAAA,QAAAA,CAASK,IAAI,CAACC,OAAAA,CAAAA;AAClB,YAAA;;YAGA,MAAMhD,OAAAA,CAAQsD,GAAG,CAACZ,QAAAA,CAAAA;YAClB,OAAON,WAAAA;AACX,QAAA,CAAA,CAAE,OAAOd,KAAAA,EAAO;YACZd,MAAAA,CAAOc,KAAK,CAAC,gCAAA,EAAkCA,KAAAA,CAAAA;YAC/C,MAAM,IAAIE,MAAM,CAAC,2BAA2B,EAAEzB,QAAAA,CAAS,EAAE,EAAEuB,KAAAA,CAAAA,CAAO,CAAA;AACtE,QAAA;AACJ,IAAA,CAAA;IAEA,OAAO;AACHT,QAAAA,oBAAAA;AACAU,QAAAA,WAAAA;AACAE,QAAAA;AACJ,KAAA;AACJ;;;;"}
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import { OpenAI } from 'openai';
|
|
2
|
+
import { create } from './storage.js';
|
|
3
|
+
import { getLogger } from '../logging.js';
|
|
4
|
+
|
|
5
|
+
class OpenAIError extends Error {
|
|
6
|
+
constructor(message){
|
|
7
|
+
super(message);
|
|
8
|
+
this.name = 'OpenAIError';
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
async function createCompletion(messages, options = {
|
|
12
|
+
model: "gpt-4o-mini"
|
|
13
|
+
}) {
|
|
14
|
+
const logger = getLogger();
|
|
15
|
+
const storage = create({
|
|
16
|
+
log: logger.debug
|
|
17
|
+
});
|
|
18
|
+
try {
|
|
19
|
+
var _completion_choices__message_content, _completion_choices__message, _completion_choices_;
|
|
20
|
+
const apiKey = process.env.OPENAI_API_KEY;
|
|
21
|
+
if (!apiKey) {
|
|
22
|
+
throw new OpenAIError('OPENAI_API_KEY environment variable is not set');
|
|
23
|
+
}
|
|
24
|
+
const openai = new OpenAI({
|
|
25
|
+
apiKey: apiKey
|
|
26
|
+
});
|
|
27
|
+
logger.debug('Sending prompt to OpenAI: %j', messages);
|
|
28
|
+
const completion = await openai.chat.completions.create({
|
|
29
|
+
model: options.model || "gpt-4o-mini",
|
|
30
|
+
messages,
|
|
31
|
+
max_completion_tokens: 10000,
|
|
32
|
+
response_format: options.responseFormat
|
|
33
|
+
});
|
|
34
|
+
if (options.debug && options.debugFile) {
|
|
35
|
+
await storage.writeFile(options.debugFile, JSON.stringify(completion, null, 2), 'utf8');
|
|
36
|
+
logger.debug('Wrote debug file to %s', options.debugFile);
|
|
37
|
+
}
|
|
38
|
+
const response = (_completion_choices_ = completion.choices[0]) === null || _completion_choices_ === void 0 ? void 0 : (_completion_choices__message = _completion_choices_.message) === null || _completion_choices__message === void 0 ? void 0 : (_completion_choices__message_content = _completion_choices__message.content) === null || _completion_choices__message_content === void 0 ? void 0 : _completion_choices__message_content.trim();
|
|
39
|
+
if (!response) {
|
|
40
|
+
throw new OpenAIError('No response received from OpenAI');
|
|
41
|
+
}
|
|
42
|
+
logger.debug('Received response from OpenAI: %s', response);
|
|
43
|
+
if (options.responseFormat) {
|
|
44
|
+
return JSON.parse(response);
|
|
45
|
+
} else {
|
|
46
|
+
return response;
|
|
47
|
+
}
|
|
48
|
+
} catch (error) {
|
|
49
|
+
logger.error('Error calling OpenAI API: %s %s', error.message, error.stack);
|
|
50
|
+
throw new OpenAIError(`Failed to create completion: ${error.message}`);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
async function transcribeAudio(filePath, options = {
|
|
54
|
+
model: "whisper-1"
|
|
55
|
+
}) {
|
|
56
|
+
const logger = getLogger();
|
|
57
|
+
const storage = create({
|
|
58
|
+
log: logger.debug
|
|
59
|
+
});
|
|
60
|
+
try {
|
|
61
|
+
const apiKey = process.env.OPENAI_API_KEY;
|
|
62
|
+
if (!apiKey) {
|
|
63
|
+
throw new OpenAIError('OPENAI_API_KEY environment variable is not set');
|
|
64
|
+
}
|
|
65
|
+
const openai = new OpenAI({
|
|
66
|
+
apiKey: apiKey
|
|
67
|
+
});
|
|
68
|
+
logger.debug('Transcribing audio file: %s', filePath);
|
|
69
|
+
const audioStream = await storage.readStream(filePath);
|
|
70
|
+
const transcription = await openai.audio.transcriptions.create({
|
|
71
|
+
model: options.model || "whisper-1",
|
|
72
|
+
file: audioStream,
|
|
73
|
+
response_format: "json"
|
|
74
|
+
});
|
|
75
|
+
if (options.debug && options.debugFile) {
|
|
76
|
+
await storage.writeFile(options.debugFile, JSON.stringify(transcription, null, 2), 'utf8');
|
|
77
|
+
logger.debug('Wrote debug file to %s', options.debugFile);
|
|
78
|
+
}
|
|
79
|
+
const response = transcription;
|
|
80
|
+
if (!response) {
|
|
81
|
+
throw new OpenAIError('No transcription received from OpenAI');
|
|
82
|
+
}
|
|
83
|
+
logger.debug('Received transcription from OpenAI: %s', response);
|
|
84
|
+
return response;
|
|
85
|
+
} catch (error) {
|
|
86
|
+
logger.error('Error transcribing audio file: %s %s', error.message, error.stack);
|
|
87
|
+
throw new OpenAIError(`Failed to transcribe audio: ${error.message}`);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
export { OpenAIError, createCompletion, transcribeAudio };
|
|
92
|
+
//# sourceMappingURL=openai.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"openai.js","sources":["../../src/util/openai.ts"],"sourcesContent":["import { OpenAI } from 'openai';\nimport { ChatCompletionMessageParam } from 'openai/resources';\nimport * as Storage from '@/util/storage';\nimport { getLogger } from '@/logging';\nexport interface Transcription {\n text: string;\n}\n\nexport class OpenAIError extends Error {\n constructor(message: string) {\n super(message);\n this.name = 'OpenAIError';\n }\n}\n\n\nexport async function createCompletion(messages: ChatCompletionMessageParam[], options: { responseFormat?: any, model?: string, debug?: boolean, debugFile?: string } = { model: \"gpt-4o-mini\" }): Promise<string | any> {\n const logger = getLogger();\n const storage = Storage.create({ log: logger.debug });\n try {\n const apiKey = process.env.OPENAI_API_KEY;\n if (!apiKey) {\n throw new OpenAIError('OPENAI_API_KEY environment variable is not set');\n }\n\n const openai = new OpenAI({\n apiKey: apiKey,\n });\n\n logger.debug('Sending prompt to OpenAI: %j', messages);\n\n const completion = await openai.chat.completions.create({\n model: options.model || \"gpt-4o-mini\",\n messages,\n max_completion_tokens: 10000,\n response_format: options.responseFormat,\n });\n\n if (options.debug && options.debugFile) {\n await storage.writeFile(options.debugFile, JSON.stringify(completion, null, 2), 'utf8');\n logger.debug('Wrote debug file to %s', options.debugFile);\n }\n\n const response = completion.choices[0]?.message?.content?.trim();\n if (!response) {\n throw new OpenAIError('No response received from OpenAI');\n }\n\n logger.debug('Received response from OpenAI: %s', response);\n if (options.responseFormat) {\n return JSON.parse(response);\n } else {\n return response;\n }\n\n } catch (error: any) {\n logger.error('Error calling OpenAI API: %s %s', error.message, error.stack);\n throw new OpenAIError(`Failed to create completion: ${error.message}`);\n }\n}\n\nexport async function transcribeAudio(filePath: string, options: { model?: string, debug?: boolean, debugFile?: string } = { model: \"whisper-1\" }): Promise<Transcription> {\n const logger = getLogger();\n const storage = Storage.create({ log: logger.debug });\n try {\n const apiKey = process.env.OPENAI_API_KEY;\n if (!apiKey) {\n throw new OpenAIError('OPENAI_API_KEY environment variable is not set');\n }\n\n const openai = new OpenAI({\n apiKey: apiKey,\n });\n\n logger.debug('Transcribing audio file: %s', filePath);\n\n const audioStream = await storage.readStream(filePath);\n const transcription = await openai.audio.transcriptions.create({\n model: options.model || \"whisper-1\",\n file: audioStream,\n response_format: \"json\",\n });\n\n if (options.debug && options.debugFile) {\n await storage.writeFile(options.debugFile, JSON.stringify(transcription, null, 2), 'utf8');\n logger.debug('Wrote debug file to %s', options.debugFile);\n }\n\n const response = transcription;\n if (!response) {\n throw new OpenAIError('No transcription received from OpenAI');\n }\n\n logger.debug('Received transcription from OpenAI: %s', response);\n return response;\n\n } catch (error: any) {\n logger.error('Error transcribing audio file: %s %s', error.message, error.stack);\n throw new OpenAIError(`Failed to transcribe audio: ${error.message}`);\n }\n}\n"],"names":["OpenAIError","Error","message","name","createCompletion","messages","options","model","logger","getLogger","storage","Storage","log","debug","completion","apiKey","process","env","OPENAI_API_KEY","openai","OpenAI","chat","completions","create","max_completion_tokens","response_format","responseFormat","debugFile","writeFile","JSON","stringify","response","choices","content","trim","parse","error","stack","transcribeAudio","filePath","audioStream","readStream","transcription","audio","transcriptions","file"],"mappings":";;;;AAQO,MAAMA,WAAAA,SAAoBC,KAAAA,CAAAA;AAC7B,IAAA,WAAA,CAAYC,OAAe,CAAE;AACzB,QAAA,KAAK,CAACA,OAAAA,CAAAA;QACN,IAAI,CAACC,IAAI,GAAG,aAAA;AAChB,IAAA;AACJ;AAGO,eAAeC,gBAAAA,CAAiBC,QAAsC,EAAEC,OAAAA,GAAyF;IAAEC,KAAAA,EAAO;AAAc,CAAC,EAAA;AAC5L,IAAA,MAAMC,MAAAA,GAASC,SAAAA,EAAAA;IACf,MAAMC,OAAAA,GAAUC,MAAc,CAAC;AAAEC,QAAAA,GAAAA,EAAKJ,OAAOK;AAAM,KAAA,CAAA;IACnD,IAAI;AAwBiBC,QAAAA,IAAAA,oCAAAA,EAAAA,4BAAAA,EAAAA,oBAAAA;AAvBjB,QAAA,MAAMC,MAAAA,GAASC,OAAAA,CAAQC,GAAG,CAACC,cAAc;AACzC,QAAA,IAAI,CAACH,MAAAA,EAAQ;AACT,YAAA,MAAM,IAAIf,WAAAA,CAAY,gDAAA,CAAA;AAC1B,QAAA;QAEA,MAAMmB,MAAAA,GAAS,IAAIC,MAAAA,CAAO;YACtBL,MAAAA,EAAQA;AACZ,SAAA,CAAA;QAEAP,MAAAA,CAAOK,KAAK,CAAC,8BAAA,EAAgCR,QAAAA,CAAAA;QAE7C,MAAMS,UAAAA,GAAa,MAAMK,MAAAA,CAAO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,OAAAA,CAAQqB,SAAS,EAAEE,IAAAA,CAAKC,SAAS,CAACY,aAAAA,EAAe,IAAA,EAAM,CAAA,CAAA,EAAI,MAAA,CAAA;AACnFlC,YAAAA,MAAAA,CAAOK,KAAK,CAAC,wBAAA,EAA0BP,OAAAA,CAAQqB,SAAS,CAAA;AAC5D,QAAA;AAEA,QAAA,MAAMI,QAAAA,GAAWW,aAAAA;AACjB,QAAA,IAAI,CAACX,QAAAA,EAAU;AACX,YAAA,MAAM,IAAI/B,WAAAA,CAAY,uCAAA,CAAA;AAC1B,QAAA;QAEAQ,MAAAA,CAAOK,KAAK,CAAC,wCAAA,EAA0CkB,QAAAA,CAAAA;QACvD,OAAOA,QAAAA;AAEX,IAAA,CAAA,CAAE,OAAOK,KAAAA,EAAY;AACjB5B,QAAAA,MAAAA,CAAO4B,KAAK,CAAC,sCAAA,EAAwCA,MAAMlC,OAAO,EAAEkC,MAAMC,KAAK,CAAA;AAC/E,QAAA,MAAM,IAAIrC,WAAAA,CAAY,CAAC,4BAA4B,EAAEoC,KAAAA,CAAMlC,OAAO,CAAA,CAAE,CAAA;AACxE,IAAA;AACJ;;;;"}
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
import * as fs from 'fs';
|
|
2
|
+
import { glob } from 'glob';
|
|
3
|
+
import path from 'path';
|
|
4
|
+
import crypto from 'crypto';
|
|
5
|
+
|
|
6
|
+
// eslint-disable-next-line no-restricted-imports
|
|
7
|
+
const create = (params)=>{
|
|
8
|
+
// eslint-disable-next-line no-console
|
|
9
|
+
const log = params.log || console.log;
|
|
10
|
+
const exists = async (path)=>{
|
|
11
|
+
try {
|
|
12
|
+
await fs.promises.stat(path);
|
|
13
|
+
return true;
|
|
14
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
15
|
+
} catch (error) {
|
|
16
|
+
return false;
|
|
17
|
+
}
|
|
18
|
+
};
|
|
19
|
+
const isDirectory = async (path)=>{
|
|
20
|
+
const stats = await fs.promises.stat(path);
|
|
21
|
+
if (!stats.isDirectory()) {
|
|
22
|
+
log(`${path} is not a directory`);
|
|
23
|
+
return false;
|
|
24
|
+
}
|
|
25
|
+
return true;
|
|
26
|
+
};
|
|
27
|
+
const isFile = async (path)=>{
|
|
28
|
+
const stats = await fs.promises.stat(path);
|
|
29
|
+
if (!stats.isFile()) {
|
|
30
|
+
log(`${path} is not a file`);
|
|
31
|
+
return false;
|
|
32
|
+
}
|
|
33
|
+
return true;
|
|
34
|
+
};
|
|
35
|
+
const isReadable = async (path)=>{
|
|
36
|
+
try {
|
|
37
|
+
await fs.promises.access(path, fs.constants.R_OK);
|
|
38
|
+
} catch (error) {
|
|
39
|
+
log(`${path} is not readable: %s %s`, error.message, error.stack);
|
|
40
|
+
return false;
|
|
41
|
+
}
|
|
42
|
+
return true;
|
|
43
|
+
};
|
|
44
|
+
const isWritable = async (path)=>{
|
|
45
|
+
try {
|
|
46
|
+
await fs.promises.access(path, fs.constants.W_OK);
|
|
47
|
+
} catch (error) {
|
|
48
|
+
log(`${path} is not writable: %s %s`, error.message, error.stack);
|
|
49
|
+
return false;
|
|
50
|
+
}
|
|
51
|
+
return true;
|
|
52
|
+
};
|
|
53
|
+
const isFileReadable = async (path)=>{
|
|
54
|
+
return await exists(path) && await isFile(path) && await isReadable(path);
|
|
55
|
+
};
|
|
56
|
+
const isDirectoryWritable = async (path)=>{
|
|
57
|
+
return await exists(path) && await isDirectory(path) && await isWritable(path);
|
|
58
|
+
};
|
|
59
|
+
const isDirectoryReadable = async (path)=>{
|
|
60
|
+
return await exists(path) && await isDirectory(path) && await isReadable(path);
|
|
61
|
+
};
|
|
62
|
+
const createDirectory = async (path)=>{
|
|
63
|
+
try {
|
|
64
|
+
await fs.promises.mkdir(path, {
|
|
65
|
+
recursive: true
|
|
66
|
+
});
|
|
67
|
+
} catch (mkdirError) {
|
|
68
|
+
throw new Error(`Failed to create output directory ${path}: ${mkdirError.message} ${mkdirError.stack}`);
|
|
69
|
+
}
|
|
70
|
+
};
|
|
71
|
+
const readFile = async (path, encoding)=>{
|
|
72
|
+
return await fs.promises.readFile(path, {
|
|
73
|
+
encoding: encoding
|
|
74
|
+
});
|
|
75
|
+
};
|
|
76
|
+
const writeFile = async (path, data, encoding)=>{
|
|
77
|
+
await fs.promises.writeFile(path, data, {
|
|
78
|
+
encoding: encoding
|
|
79
|
+
});
|
|
80
|
+
};
|
|
81
|
+
const forEachFileIn = async (directory, callback, options = {
|
|
82
|
+
pattern: '*.*'
|
|
83
|
+
})=>{
|
|
84
|
+
try {
|
|
85
|
+
const files = await glob(options.pattern, {
|
|
86
|
+
cwd: directory,
|
|
87
|
+
nodir: true
|
|
88
|
+
});
|
|
89
|
+
for (const file of files){
|
|
90
|
+
await callback(path.join(directory, file));
|
|
91
|
+
}
|
|
92
|
+
} catch (err) {
|
|
93
|
+
throw new Error(`Failed to glob pattern ${options.pattern} in ${directory}: ${err.message}`);
|
|
94
|
+
}
|
|
95
|
+
};
|
|
96
|
+
const readStream = async (path)=>{
|
|
97
|
+
return fs.createReadStream(path);
|
|
98
|
+
};
|
|
99
|
+
const hashFile = async (path, length)=>{
|
|
100
|
+
const file = await readFile(path, 'utf8');
|
|
101
|
+
return crypto.createHash('sha256').update(file).digest('hex').slice(0, length);
|
|
102
|
+
};
|
|
103
|
+
const listFiles = async (directory)=>{
|
|
104
|
+
return await fs.promises.readdir(directory);
|
|
105
|
+
};
|
|
106
|
+
const deleteFile = async (path)=>{
|
|
107
|
+
await fs.promises.unlink(path);
|
|
108
|
+
};
|
|
109
|
+
const getFileSize = async (path)=>{
|
|
110
|
+
const stats = await fs.promises.stat(path);
|
|
111
|
+
return stats.size;
|
|
112
|
+
};
|
|
113
|
+
return {
|
|
114
|
+
exists,
|
|
115
|
+
isDirectory,
|
|
116
|
+
isFile,
|
|
117
|
+
isReadable,
|
|
118
|
+
isWritable,
|
|
119
|
+
isFileReadable,
|
|
120
|
+
isDirectoryWritable,
|
|
121
|
+
isDirectoryReadable,
|
|
122
|
+
createDirectory,
|
|
123
|
+
readFile,
|
|
124
|
+
readStream,
|
|
125
|
+
writeFile,
|
|
126
|
+
forEachFileIn,
|
|
127
|
+
hashFile,
|
|
128
|
+
listFiles,
|
|
129
|
+
deleteFile,
|
|
130
|
+
getFileSize
|
|
131
|
+
};
|
|
132
|
+
};
|
|
133
|
+
|
|
134
|
+
export { create };
|
|
135
|
+
//# sourceMappingURL=storage.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"storage.js","sources":["../../src/util/storage.ts"],"sourcesContent":["// eslint-disable-next-line no-restricted-imports\nimport * as fs from 'fs';\nimport { glob } from 'glob';\nimport path from 'path';\nimport crypto from 'crypto';\n/**\n * This module exists to isolate filesystem operations from the rest of the codebase.\n * This makes testing easier by avoiding direct fs mocking in jest configuration.\n * \n * Additionally, abstracting storage operations allows for future flexibility - \n * this export utility may need to work with storage systems other than the local filesystem\n * (e.g. S3, Google Cloud Storage, etc).\n */\n\nexport interface Utility {\n exists: (path: string) => Promise<boolean>;\n isDirectory: (path: string) => Promise<boolean>;\n isFile: (path: string) => Promise<boolean>;\n isReadable: (path: string) => Promise<boolean>;\n isWritable: (path: string) => Promise<boolean>;\n isFileReadable: (path: string) => Promise<boolean>;\n isDirectoryWritable: (path: string) => Promise<boolean>;\n isDirectoryReadable: (path: string) => Promise<boolean>;\n createDirectory: (path: string) => Promise<void>;\n readFile: (path: string, encoding: string) => Promise<string>;\n readStream: (path: string) => Promise<fs.ReadStream>;\n writeFile: (path: string, data: string | Buffer, encoding: string) => Promise<void>;\n forEachFileIn: (directory: string, callback: (path: string) => Promise<void>, options?: { pattern: string }) => Promise<void>;\n hashFile: (path: string, length: number) => Promise<string>;\n listFiles: (directory: string) => Promise<string[]>;\n deleteFile: (path: string) => Promise<void>;\n getFileSize: (path: string) => Promise<number>;\n}\n\nexport const create = (params: { log?: (message: string, ...args: any[]) => void }): Utility => {\n\n // eslint-disable-next-line no-console\n const log = params.log || console.log;\n\n const exists = async (path: string): Promise<boolean> => {\n try {\n await fs.promises.stat(path);\n return true;\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\n } catch (error: any) {\n return false;\n }\n }\n\n const isDirectory = async (path: string): Promise<boolean> => {\n const stats = await fs.promises.stat(path);\n if (!stats.isDirectory()) {\n log(`${path} is not a directory`);\n return false;\n }\n return true;\n }\n\n const isFile = async (path: string): Promise<boolean> => {\n const stats = await fs.promises.stat(path);\n if (!stats.isFile()) {\n log(`${path} is not a file`);\n return false;\n }\n return true;\n }\n\n const isReadable = async (path: string): Promise<boolean> => {\n try {\n await fs.promises.access(path, fs.constants.R_OK);\n } catch (error: any) {\n log(`${path} is not readable: %s %s`, error.message, error.stack);\n return false;\n }\n return true;\n }\n\n const isWritable = async (path: string): Promise<boolean> => {\n try {\n await fs.promises.access(path, fs.constants.W_OK);\n } catch (error: any) {\n log(`${path} is not writable: %s %s`, error.message, error.stack);\n return false;\n }\n return true;\n }\n\n const isFileReadable = async (path: string): Promise<boolean> => {\n return await exists(path) && await isFile(path) && await isReadable(path);\n }\n\n const isDirectoryWritable = async (path: string): Promise<boolean> => {\n return await exists(path) && await isDirectory(path) && await isWritable(path);\n }\n\n const isDirectoryReadable = async (path: string): Promise<boolean> => {\n return await exists(path) && await isDirectory(path) && await isReadable(path);\n }\n\n const createDirectory = async (path: string): Promise<void> => {\n try {\n await fs.promises.mkdir(path, { recursive: true });\n } catch (mkdirError: any) {\n throw new Error(`Failed to create output directory ${path}: ${mkdirError.message} ${mkdirError.stack}`);\n }\n }\n\n const readFile = async (path: string, encoding: string): Promise<string> => {\n return await fs.promises.readFile(path, { encoding: encoding as BufferEncoding });\n }\n\n const writeFile = async (path: string, data: string | Buffer, encoding: string): Promise<void> => {\n await fs.promises.writeFile(path, data, { encoding: encoding as BufferEncoding });\n }\n\n const forEachFileIn = async (directory: string, callback: (file: string) => Promise<void>, options: { pattern: string | string[] } = { pattern: '*.*' }): Promise<void> => {\n try {\n const files = await glob(options.pattern, { cwd: directory, nodir: true });\n for (const file of files) {\n await callback(path.join(directory, file));\n }\n } catch (err: any) {\n throw new Error(`Failed to glob pattern ${options.pattern} in ${directory}: ${err.message}`);\n }\n }\n\n const readStream = async (path: string): Promise<fs.ReadStream> => {\n return fs.createReadStream(path);\n }\n\n const hashFile = async (path: string, length: number): Promise<string> => {\n const file = await readFile(path, 'utf8');\n return crypto.createHash('sha256').update(file).digest('hex').slice(0, length);\n }\n\n const listFiles = async (directory: string): Promise<string[]> => {\n return await fs.promises.readdir(directory);\n }\n\n const deleteFile = async (path: string): Promise<void> => {\n await fs.promises.unlink(path);\n }\n\n const getFileSize = async (path: string): Promise<number> => {\n const stats = await fs.promises.stat(path);\n return stats.size;\n }\n\n return {\n exists,\n isDirectory,\n isFile,\n isReadable,\n isWritable,\n isFileReadable,\n isDirectoryWritable,\n isDirectoryReadable,\n createDirectory,\n readFile,\n readStream,\n writeFile,\n forEachFileIn,\n hashFile,\n listFiles,\n deleteFile,\n getFileSize,\n };\n}"],"names":["create","params","log","console","exists","path","fs","promises","stat","error","isDirectory","stats","isFile","isReadable","access","constants","R_OK","message","stack","isWritable","W_OK","isFileReadable","isDirectoryWritable","isDirectoryReadable","createDirectory","mkdir","recursive","mkdirError","Error","readFile","encoding","writeFile","data","forEachFileIn","directory","callback","options","pattern","files","glob","cwd","nodir","file","join","err","readStream","createReadStream","hashFile","length","crypto","createHash","update","digest","slice","listFiles","readdir","deleteFile","unlink","getFileSize","size"],"mappings":";;;;;AAAA;AAkCO,MAAMA,SAAS,CAACC,MAAAA,GAAAA;;AAGnB,IAAA,MAAMC,GAAAA,GAAMD,MAAAA,CAAOC,GAAG,IAAIC,QAAQD,GAAG;AAErC,IAAA,MAAME,SAAS,OAAOC,IAAAA,GAAAA;QAClB,IAAI;AACA,YAAA,MAAMC,EAAAA,CAAGC,QAAQ,CAACC,IAAI,CAACH,IAAAA,CAAAA;YACvB,OAAO,IAAA;;AAEX,QAAA,CAAA,CAAE,OAAOI,KAAAA,EAAY;YACjB,OA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s/index.html
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
<!doctype html>
|
|
2
|
+
<html lang="en">
|
|
3
|
+
|
|
4
|
+
<head>
|
|
5
|
+
<meta charset="UTF-8" />
|
|
6
|
+
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
|
|
7
|
+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
|
8
|
+
<title>Matnava - Focused Audio Transcription Tool</title>
|
|
9
|
+
</head>
|
|
10
|
+
|
|
11
|
+
<body>
|
|
12
|
+
<div id="root"></div>
|
|
13
|
+
<script type="module" src="/src/main.jsx"></script>
|
|
14
|
+
</body>
|
|
15
|
+
|
|
16
|
+
</html>
|