MCPcopy Create free account
hub / github.com/OpenRaiser/PaperFlow / ensure_role_profiles

Function ensure_role_profiles

scripts/runtime_bootstrap.py:102–157  ·  view source on GitHub ↗
(root_dir: Path = PROJECT_ROOT)

Source from the content-addressed store, hash-verified

100
101
102def ensure_role_profiles(root_dir: Path = PROJECT_ROOT) -> Dict[str, Any]:
103 roles_path = root_dir / "data" / "roles.json"
104 if not roles_path.exists():
105 return {"created": [], "updated": []}
106
107 try:
108 roles_meta = json.loads(roles_path.read_text(encoding="utf-8"))
109 except json.JSONDecodeError:
110 return {"created": [], "updated": []}
111
112 db_ops = importlib.import_module("skills.storage-helper.scripts.db_ops")
113 coldstart_agent = importlib.import_module("agents.coldstart-agent.main")
114 db_path = root_dir / "data" / "paperflow.db"
115 original_db_path = getattr(db_ops, "DB_PATH", None)
116
117 created: list[str] = []
118 updated: list[str] = []
119 try:
120 db_ops.DB_PATH = db_path
121 for role_name, role_data in (roles_meta.get("roles") or {}).items():
122 if not isinstance(role_data, dict):
123 continue
124
125 user_id = str(role_data.get("user_id") or f"user_{role_name}").strip()
126 if not user_id:
127 continue
128
129 profile = db_ops.get_profile(user_id)
130 is_new_profile = profile is None
131 if profile is None:
132 profile = coldstart_agent.build_empty_profile(user_id)
133 else:
134 profile = coldstart_agent.ensure_profile_shape(profile, user_id)
135
136 feishu_chat_id = str(role_data.get("feishu_chat_id") or "").strip()
137 if feishu_chat_id:
138 profile["feishu_chat_id"] = feishu_chat_id
139
140 bootstrap_text = str(
141 role_data.get("natural_language") or role_data.get("description") or ""
142 ).strip()
143 if bootstrap_text and not profile.get("core_directions") and not profile.get("topic_weights"):
144 parsed = coldstart_agent.parse_natural_language(bootstrap_text, use_llm=False)
145 coldstart_agent.merge_parsed_profile_into_profile(profile, parsed)
146
147 if is_new_profile:
148 db_ops.create_profile(user_id, profile)
149 created.append(user_id)
150 else:
151 db_ops.update_profile(user_id, profile)
152 updated.append(user_id)
153 finally:
154 if original_db_path is not None:
155 db_ops.DB_PATH = original_db_path
156
157 return {"created": created, "updated": updated}
158
159

Callers 1

bootstrap_runtimeFunction · 0.85

Calls 1

getMethod · 0.80

Tested by

no test coverage detected