#!/usr/bin/env python3 # -*- coding: utf-8 -*- """专题十五 · 步骤 2:查 app_id / agent_id / snapshot_id / draft_id。""" from __future__ import annotations import sys import _bootstrap # noqa: F401 import _config as C from dify_api import Dify def main() -> int: d = Dify(C.DIFY_EMAIL, C.DIFY_PASSWORD) try: d.login() sql = """ SELECT ap.id AS app_id, ap.mode, ap.name, a.id AS agent_id, a.active_config_snapshot_id AS snapshot_id, d.id AS draft_id FROM apps ap LEFT JOIN agents a ON a.app_id = ap.id LEFT JOIN agent_config_drafts d ON d.agent_id = a.id WHERE ap.name ILIKE '%%%s%%' ORDER BY ap.created_at DESC LIMIT 5; """ % C.NEW_APP_NAME.replace("'", "''") out, err = d.sh( 'docker exec %s psql -U postgres -d dify -c "%s"' % (C.PG_CONTAINER, sql.replace("\n", " ")) ) print(out or err) print("\n=== API 列表(最近 10 个应用)===") r = d.get("/apps?page=1&limit=10") for item in (r.get("json") or {}).get("data") or []: print(item.get("id"), item.get("mode"), item.get("name")) return 0 finally: d.close() if __name__ == "__main__": sys.exit(main())