diff --git a/.agent/workflows/plugin-development.md b/.agent/workflows/plugin-development.md index 62344c8..25797b1 100644 --- a/.agent/workflows/plugin-development.md +++ b/.agent/workflows/plugin-development.md @@ -25,6 +25,8 @@ Every plugin **MUST** have bilingual versions for both code and documentation: - **Valves**: Use `pydantic` for configuration. - **Database**: Re-use `open_webui.internal.db` shared connection. - **User Context**: Use `_get_user_context` helper method. +- **Chat Context**: Use `_get_chat_context` helper method for `chat_id` and `message_id`. +- **Debugging**: Use `_emit_debug_log` for frontend console logging (requires `SHOW_DEBUG_LOG` valve). - **Chat API**: For message updates, follow the "OpenWebUI Chat API 更新规范" in `.github/copilot-instructions.md`. - Use Event API for immediate UI updates - Use Chat Persistence API for database storage @@ -86,6 +88,7 @@ Reference: `.github/workflows/release.yml` - Workflow: `.github/workflows/publish_plugin.yml` - Trigger: Release published. - Action: Automatically updates the plugin code and metadata on OpenWebUI.com using `scripts/publish_plugin.py`. + - **Auto-Sync**: If a local plugin has no ID but matches an existing published plugin by **Title**, the script will automatically fetch the ID, update the local file, and proceed with the update. - Requirement: `OPENWEBUI_API_KEY` secret must be set. ### Pull Request Check diff --git a/scripts/openwebui_community_client.py b/scripts/openwebui_community_client.py index 463b00a..110a9cc 100644 --- a/scripts/openwebui_community_client.py +++ b/scripts/openwebui_community_client.py @@ -483,25 +483,44 @@ class OpenWebUICommunityClient: print(f" Uploaded image: {image_url}") media_urls = [image_url] - # 如果没有 post_id,尝试创建新帖子 + # 如果没有 post_id,尝试查找或创建 if not post_id: - if not auto_create: - return False, "No openwebui_id found and auto_create is disabled" + # 1. 尝试通过标题查找已存在的帖子 + print(f" Searching for existing post with title: {title}") + try: + all_posts = self.get_all_posts() + existing_post = next( + (p for p in all_posts if p.get("title") == title), None + ) + except Exception as e: + print(f" Warning: Failed to fetch posts for title check: {e}") + existing_post = None - print(f" Creating new post for: {title}") - new_post_id = self.create_plugin( - title=title, - source_code=content, - readme_content=readme_content or metadata.get("description", ""), - metadata=metadata, - media_urls=media_urls, - ) + if existing_post: + post_id = existing_post.get("id") + print(f" Found existing post: {title} (ID: {post_id})") + self._inject_id_to_file(file_path, post_id) + # post_id 已设置,后续将进入更新流程 - if new_post_id: - # 将新 ID 写回本地文件 - self._inject_id_to_file(file_path, new_post_id) - return True, f"Created new post (ID: {new_post_id})" - return False, "Failed to create new post" + else: + # 2. 如果没找到,且允许自动创建,则创建 + if not auto_create: + return False, "No openwebui_id found and auto_create is disabled" + + print(f" Creating new post for: {title}") + new_post_id = self.create_plugin( + title=title, + source_code=content, + readme_content=readme_content or metadata.get("description", ""), + metadata=metadata, + media_urls=media_urls, + ) + + if new_post_id: + # 将新 ID 写回本地文件 + self._inject_id_to_file(file_path, new_post_id) + return True, f"Created new post (ID: {new_post_id})" + return False, "Failed to create new post" # 获取远程帖子信息(只需获取一次) remote_post = None