|
44 | 44 | print(f"Reading file '{key_file_path}'.") |
45 | 45 | key = file.read() |
46 | 46 |
|
| 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 | + |
47 | 71 | def load_conversation_old(conversation_token: str): |
48 | 72 | """ |
49 | 73 | Load a checkpointer with the conversation state from the conversation token |
@@ -157,7 +181,8 @@ async def call_model( |
157 | 181 | if tool_enabled("web_fetch"): |
158 | 182 | 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" |
159 | 183 | 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" |
161 | 186 |
|
162 | 187 | if task['input'].get('memories'): |
163 | 188 | 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