Skip to content

Commit eb8ae82

Browse files
committed
fix: Use OCS API to fetch absolute NC URL
Assisted-by: ClaudeCode:claude-opus-4-8 Signed-off-by: Marcel Klehr <mklehr@gmx.net>
1 parent c1e7284 commit eb8ae82

1 file changed

Lines changed: 26 additions & 1 deletion

File tree

ex_app/lib/agent.py

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,30 @@
4444
print(f"Reading file '{key_file_path}'.")
4545
key = file.read()
4646

47+
# The user-facing absolute base URL is stable for the app's lifetime; resolve it once.
48+
_absolute_base_url: str | None = None
49+
50+
51+
async def get_absolute_base_url(nc: AsyncNextcloudApp) -> str:
52+
"""
53+
Return the user-facing absolute base URL of the Nextcloud instance.
54+
55+
``nc.app_cfg.endpoint`` may be an internal address (reverse-proxy / docker host), so
56+
ask app_api for the public URL. The result is cached module-wide to avoid an HTTP
57+
request on every conversation turn; falls back to the endpoint if the lookup fails.
58+
"""
59+
global _absolute_base_url
60+
if _absolute_base_url is None:
61+
try:
62+
_absolute_base_url = (await nc.ocs(
63+
'GET', '/ocs/v2.php/apps/app_api/api/v1/info/nextcloud_url/absolute', json={'url': '/'}
64+
))['absolute_url'].rstrip('/')
65+
except Exception as e:
66+
print(f"Could not resolve absolute Nextcloud URL, falling back to endpoint: {e}")
67+
return nc.app_cfg.endpoint
68+
return _absolute_base_url
69+
70+
4771
def load_conversation_old(conversation_token: str):
4872
"""
4973
Load a checkpointer with the conversation state from the conversation token
@@ -157,7 +181,8 @@ async def call_model(
157181
if tool_enabled("web_fetch"):
158182
system_prompt_text += "Use the web_fetch tool to fetch web content. You can fetch the complete page content of a duckduckgo search result using web_fetch as well.\n"
159183
if tool_enabled("parse_nextcloud_url"):
160-
system_prompt_text += f"This Nextcloud instance is reachable at {nc.app_cfg.endpoint}. URLs starting with this root belong to this Nextcloud instance; use the parse_nextcloud_url tool to turn such a link the user pasted into concrete ids before calling the relevant app tools.\n"
184+
base_url = await get_absolute_base_url(nc)
185+
system_prompt_text += f"This Nextcloud instance is reachable at {base_url}. URLs starting with this root belong to this Nextcloud instance; use the parse_nextcloud_url tool to turn such a link the user pasted into concrete ids before calling the relevant app tools.\n"
161186

162187
if task['input'].get('memories'):
163188
system_prompt_text += "You can remember things from other conversations with the user. If relevant, take into account the following memories:\n\n" + "\n".join(task['input']['memories']) + "\n\n"

0 commit comments

Comments
 (0)