# Valispace Script for Custom Actions # Purpose: Generate and update titles for specified requirements based on their text # Developed by [Paul Grey] # Date: [Date] #IMPORTANT: Provide your valispace url in the Configuration Section below from typing import Any, Dict from valispace import API # --------------------------------------------- # Configuration Section VALISPACE = { "domain": "https://YOUR_DEPLOYMENT_NAME.valispace.com", # Base URL for the Valispace instance "warn_https": False, # Disable HTTPS warnings if set to False } # --------------------------------------------- def initialize_api(temporary_access_token) -> API: """ Initialize the Valispace API. Returns: API: Initialized Valispace API object. """ return API( url=VALISPACE["domain"], session_token=temporary_access_token, warn_https=VALISPACE.get("warn_https", False) ) def generate_titles(api, objects_ids) -> Dict[int, str]: """ Generate titles for each requirement using a custom prompt. Args: api (API): Initialized Valispace API object. objects_ids (list): List of object IDs for which to generate titles. Returns: Dict[int, str]: Dictionary of object IDs and their generated titles. """ custom_prompt = "Based on the Text provide a short Title for this Requirement in the context of System Engineering. Do not include any numbers or Units in the title. Only return the title and no other text." contenttype = "requirement" field = "text" assistant_response = api.general_prompt( custom_prompt=custom_prompt, model=contenttype, field=field, objects_ids=objects_ids, parallel=True ) if assistant_response.get('errors') == ['']: return {int(obj_id): title for obj_id, title in assistant_response['results'].items()} else: error_message = "Errors encountered in the assistant response: " + ", ".join(assistant_response.get('errors')) raise Exception(error_message) def update_requirement_titles(api, titles_dict) -> None: """ Update the "Title" field of each requirement with the generated title. Args: api (API): Initialized Valispace API object. titles_dict (Dict[int, str]): Dictionary of object IDs and their generated titles. Returns: None """ for obj_id, title in titles_dict.items(): update_response = api.request("PATCH", f"requirements/{obj_id}/", {"title": title}) # Handle the update response here if needed def main(**kwargs) -> Dict[str, Any]: """ Main function to execute the script. Args: **kwargs: Additional arguments passed to the script. Returns: Dict[str, Any]: Result data to be sent back to Valispace. """ api = initialize_api(kwargs['temporary_access_token']) objects_ids = kwargs.get("objects_ids", []) if not objects_ids: return {"error": "No object IDs provided."} generated_titles = generate_titles(api, objects_ids) update_requirement_titles(api, generated_titles) return { "result": "Titles updated successfully for the specified requirements." } # Note: Ensure that the assistant_response['results'] structure matches with the expected format. # The script might need adjustments based on the actual response structure and Valispace API specifics.