-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathutils.ts
89 lines (77 loc) · 2.64 KB
/
utils.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 { format } from 'date-fns'
import { AppConfig } from '@/global'
const FILEPATH_REGEX = /(?:---FILEPATH|Path:|\/\/\s*filepath:)\s*([^\s]+)/g
const COMPARE_CODE_REGEX = /Compare this snippet[^:]*:/g
function parsingByKeys(text: string | undefined, timestamp: string) {
const fallback = `Prompt ${format(new Date(timestamp ?? ''), 'y/MM/dd - hh:mm:ss a')}`
try {
if (!text) return fallback
const filePath = text.match(FILEPATH_REGEX)
const compareCode = text.match(COMPARE_CODE_REGEX)
// there some edge cases in copilot where the prompts are not correctly parsed. In this case is better to show the filepath
if (compareCode || filePath) {
if (filePath)
return `Prompt on file${filePath[0]?.trim().toLocaleLowerCase()}`
if (compareCode)
return `Prompt from snippet ${compareCode[0]?.trim().toLocaleLowerCase()}`
}
return text.trim()
} catch {
return fallback
}
}
export function parsingPromptText(message: string, timestamp: string) {
try {
// checking malformed markdown code blocks
const regex = /^(.*)```[\s\S]*?```(.*)$/s
const match = message.match(regex)
if (match !== null && match !== undefined) {
const beforeMarkdown = match[1]?.trim()
const afterMarkdown = match[2]?.trim()
const title = beforeMarkdown || afterMarkdown
return parsingByKeys(title, timestamp)
}
return parsingByKeys(message, timestamp)
} catch {
return message.trim()
}
}
export function sanitizeQuestionPrompt({
question,
answer,
}: {
question: string
answer: string
}) {
try {
// it shouldn't be possible to receive the prompt answer without a question
if (!answer) {
throw new Error('Missing AI answer')
}
// Check if 'answer' is truthy; if so, try to find and return the text after "Query:"
const index = question.indexOf('Query:')
if (index !== -1) {
// Return the substring starting right after the first occurrence of "Query:"
// Adding the length of "Query:" to the index to start after it
return question.substring(index + 'Query:'.length).trim()
}
return question
} catch (error) {
// Log the error and return the original question as a fallback
console.error('Error processing the question:', error)
return question
}
}
export function getAppConfig(): AppConfig {
const baseApiUrl = window.APP_CONFIG?.DASHBOARD_API_BASE_URL
if (!baseApiUrl || baseApiUrl === '${DASHBOARD_API_BASE_URL}') {
return {
...window.APP_CONFIG,
DASHBOARD_API_BASE_URL: import.meta.env.VITE_BASE_API_URL,
}
}
return {
...window.APP_CONFIG,
DASHBOARD_API_BASE_URL: baseApiUrl,
}
}