Skip to content

Commit 0af23df

Browse files
committed
feat(fetch): implement resolveAppUrl function for dynamic URL resolution
1 parent 938b4f3 commit 0af23df

1 file changed

Lines changed: 30 additions & 16 deletions

File tree

public/index.md

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1895,6 +1895,20 @@
18951895
applyAttachmentAvailability(false);
18961896

18971897
/* ========== UTILITY FUNCTIONS ========== */
1898+
function resolveAppUrl(path) {
1899+
const cleanPath = String(path || '').replace(/^\/+/, '');
1900+
1901+
if (typeof document !== 'undefined' && document.baseURI) {
1902+
return new URL(cleanPath, document.baseURI).toString();
1903+
}
1904+
1905+
if (typeof window !== 'undefined' && window.location) {
1906+
return new URL(cleanPath, window.location.href).toString();
1907+
}
1908+
1909+
return cleanPath;
1910+
}
1911+
18981912
function isAtBottom() {
18991913
if (!resultsDiv) return true;
19001914
const threshold = 50; // Allow margin for rounding errors
@@ -2238,7 +2252,7 @@
22382252
}
22392253

22402254
try {
2241-
const response = await fetch('/result', {
2255+
const response = await fetch(resolveAppUrl('result'), {
22422256
method: 'POST',
22432257
headers: { 'Content-Type': 'application/json; charset=utf-8' },
22442258
body: JSON.stringify({ uuid })
@@ -2457,7 +2471,7 @@
24572471
throw new Error('No conversation available to export.');
24582472
}
24592473

2460-
const response = await fetch('/md2html', {
2474+
const response = await fetch(resolveAppUrl('md2html'), {
24612475
method: 'POST',
24622476
headers: { 'Content-Type': 'application/json; charset=utf-8' },
24632477
body: JSON.stringify({ markdown: markdownText })
@@ -2597,7 +2611,7 @@
25972611
resetPlanPanel();
25982612
try { hljs.highlightAll(); } catch (e) { /* ignore */ }
25992613

2600-
const response = await fetch('/prompt', {
2614+
const response = await fetch(resolveAppUrl('prompt'), {
26012615
method: 'POST',
26022616
headers: { 'Content-Type': 'application/json; charset=utf-8' },
26032617
body: JSON.stringify({ prompt: promptForRetry, uuid: retryUuid })
@@ -4304,7 +4318,7 @@
43044318
let shouldEnableMath = false;
43054319

43064320
try {
4307-
const response = await fetch('/info', {
4321+
const response = await fetch(resolveAppUrl('info'), {
43084322
headers: { 'Accept': 'application/json' }
43094323
});
43104324

@@ -4379,7 +4393,7 @@
43794393
.filter(uuid => typeof uuid === 'string' && uuid.length > 0);
43804394

43814395
await Promise.allSettled(uuidsToClear.map(uuid => {
4382-
return fetch('/clear', {
4396+
return fetch(resolveAppUrl('clear'), {
43834397
method: 'POST',
43844398
headers: { 'Content-Type': 'application/json; charset=utf-8' },
43854399
body: JSON.stringify({ uuid, force: true })
@@ -4421,7 +4435,7 @@
44214435

44224436
if (entry && entry.uuid) {
44234437
try {
4424-
await fetch('/clear', {
4438+
await fetch(resolveAppUrl('clear'), {
44254439
method: 'POST',
44264440
headers: { 'Content-Type': 'application/json; charset=utf-8' },
44274441
body: JSON.stringify({ uuid: entry.uuid, force: true })
@@ -4516,7 +4530,7 @@
45164530
}
45174531

45184532
try {
4519-
const response = await fetch('/load-history', {
4533+
const response = await fetch(resolveAppUrl('load-history'), {
45204534
method: 'POST',
45214535
headers: { 'Content-Type': 'application/json; charset=utf-8' },
45224536
body: JSON.stringify(payload)
@@ -4582,7 +4596,7 @@
45824596
}
45834597

45844598
try {
4585-
const response = await fetch('/result', {
4599+
const response = await fetch(resolveAppUrl('result'), {
45864600
method: 'POST',
45874601
headers: { 'Content-Type': 'application/json; charset=utf-8' },
45884602
body: JSON.stringify({ uuid })
@@ -4932,7 +4946,7 @@
49324946
stopStream();
49334947

49344948
if (currentSessionUuid) {
4935-
fetch('/result', {
4949+
fetch(resolveAppUrl('result'), {
49364950
method: 'POST',
49374951
headers: { 'Content-Type': 'application/json; charset=utf-8' },
49384952
body: JSON.stringify({ uuid: currentSessionUuid, request: 'stop' })
@@ -4984,7 +4998,7 @@
49844998
}
49854999

49865000
function collectBrowserContext() {
4987-
const colorScheme = (typeof window !== 'undefined' && window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches)
5001+
const colorScheme = (document.body.classList.contains('markdown-body-dark') || _isD === true || (typeof __isDark !== 'undefined' && __isDark))
49885002
? 'dark'
49895003
: 'light';
49905004
const promptStyles = (typeof window !== 'undefined' && promptInput) ? window.getComputedStyle(promptInput) : null;
@@ -5044,7 +5058,7 @@
50445058
lastRawContent += userPromptDiv;
50455059
await renderRawContent(lastRawContent);
50465060

5047-
const response = await fetch('/prompt', {
5061+
const response = await fetch(resolveAppUrl('prompt'), {
50485062
method: 'POST',
50495063
headers: { 'Content-Type': 'application/json; charset=utf-8' },
50505064
body: JSON.stringify({ prompt: finalPrompt, uuid: currentSessionUuid, browserContext })
@@ -5067,7 +5081,7 @@
50675081

50685082
async function pollOnce() {
50695083
try {
5070-
const response = await fetch('/result', {
5084+
const response = await fetch(resolveAppUrl('result'), {
50715085
method: 'POST',
50725086
headers: { 'Content-Type': 'application/json; charset=utf-8' },
50735087
body: JSON.stringify({ uuid: currentSessionUuid })
@@ -5209,7 +5223,7 @@
52095223
streamActive = true;
52105224
streamBuffer = '';
52115225

5212-
const streamUrl = '/stream?uuid=' + encodeURIComponent(uuid);
5226+
const streamUrl = resolveAppUrl('stream') + '?uuid=' + encodeURIComponent(uuid);
52135227
streamSource = new EventSource(streamUrl);
52145228
streamSource.addEventListener('stream', (event) => {
52155229
if (!event || !event.data) return;
@@ -5264,7 +5278,7 @@
52645278
async function sendPing(uuid) {
52655279
if (!uuid) return;
52665280
try {
5267-
await fetch('/ping', {
5281+
await fetch(resolveAppUrl('ping'), {
52685282
method: 'POST',
52695283
headers: { 'Content-Type': 'application/json; charset=utf-8' },
52705284
body: JSON.stringify({ uuid })
@@ -5284,7 +5298,7 @@
52845298
}
52855299

52865300
if (uuidToClear) {
5287-
fetch('/clear', {
5301+
fetch(resolveAppUrl('clear'), {
52885302
method: 'POST',
52895303
headers: { 'Content-Type': 'application/json; charset=utf-8' },
52905304
body: JSON.stringify({ uuid: uuidToClear })
@@ -5567,4 +5581,4 @@
55675581
bindThemeListener(window.matchMedia('(prefers-color-scheme: dark)'));
55685582
bindThemeListener(window.matchMedia('(prefers-color-scheme: light)'));
55695583
</script>
5570-
<script src="/js/mdtablesort.js"></script>
5584+
<script src="js/mdtablesort.js"></script>

0 commit comments

Comments
 (0)