eval-protocol 0.0.3__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (130) hide show
  1. development/__init__.py +1 -0
  2. development/normalize_sandbox_fusion.py +628 -0
  3. development/utils/__init__.py +1 -0
  4. development/utils/generate_api_key.py +31 -0
  5. development/utils/subprocess_manager.py +481 -0
  6. eval_protocol/__init__.py +86 -0
  7. eval_protocol/__main__.py +10 -0
  8. eval_protocol/_version.py +21 -0
  9. eval_protocol/adapters/__init__.py +1 -0
  10. eval_protocol/adapters/braintrust.py +8 -0
  11. eval_protocol/adapters/trl.py +8 -0
  12. eval_protocol/agent/__init__.py +29 -0
  13. eval_protocol/agent/models.py +69 -0
  14. eval_protocol/agent/orchestrator.py +893 -0
  15. eval_protocol/agent/resource_abc.py +89 -0
  16. eval_protocol/agent/resource_pool.py +184 -0
  17. eval_protocol/agent/resources/__init__.py +44 -0
  18. eval_protocol/agent/resources/bfcl_envs/__init__.py +1 -0
  19. eval_protocol/agent/resources/bfcl_envs/gorilla_file_system.py +342 -0
  20. eval_protocol/agent/resources/bfcl_envs/math_api.py +40 -0
  21. eval_protocol/agent/resources/bfcl_envs/posting_api.py +157 -0
  22. eval_protocol/agent/resources/bfcl_sim_api_resource.py +314 -0
  23. eval_protocol/agent/resources/docker_resource.py +479 -0
  24. eval_protocol/agent/resources/filesystem_resource.py +371 -0
  25. eval_protocol/agent/resources/http_rollout_protocol.py +85 -0
  26. eval_protocol/agent/resources/http_rollout_resource.py +325 -0
  27. eval_protocol/agent/resources/python_state_resource.py +170 -0
  28. eval_protocol/agent/resources/sql_resource.py +271 -0
  29. eval_protocol/agent/task_manager.py +1064 -0
  30. eval_protocol/agent/tool_registry.py +111 -0
  31. eval_protocol/auth.py +156 -0
  32. eval_protocol/cli.py +425 -0
  33. eval_protocol/cli_commands/__init__.py +1 -0
  34. eval_protocol/cli_commands/agent_eval_cmd.py +264 -0
  35. eval_protocol/cli_commands/common.py +242 -0
  36. eval_protocol/cli_commands/deploy.py +486 -0
  37. eval_protocol/cli_commands/deploy_mcp.py +287 -0
  38. eval_protocol/cli_commands/preview.py +186 -0
  39. eval_protocol/cli_commands/run_eval_cmd.py +202 -0
  40. eval_protocol/common_utils.py +36 -0
  41. eval_protocol/config.py +180 -0
  42. eval_protocol/datasets/__init__.py +1 -0
  43. eval_protocol/datasets/loader.py +521 -0
  44. eval_protocol/evaluation.py +1045 -0
  45. eval_protocol/execution/__init__.py +1 -0
  46. eval_protocol/execution/pipeline.py +920 -0
  47. eval_protocol/gcp_tools.py +484 -0
  48. eval_protocol/generation/cache.py +141 -0
  49. eval_protocol/generation/clients/base.py +67 -0
  50. eval_protocol/generation/clients.py +248 -0
  51. eval_protocol/generic_server.py +165 -0
  52. eval_protocol/integrations/__init__.py +12 -0
  53. eval_protocol/integrations/braintrust.py +51 -0
  54. eval_protocol/integrations/deepeval.py +106 -0
  55. eval_protocol/integrations/openeval.py +40 -0
  56. eval_protocol/integrations/trl.py +187 -0
  57. eval_protocol/mcp/__init__.py +48 -0
  58. eval_protocol/mcp/adapter.py +131 -0
  59. eval_protocol/mcp/client/__init__.py +12 -0
  60. eval_protocol/mcp/client/connection.py +499 -0
  61. eval_protocol/mcp/clients.py +195 -0
  62. eval_protocol/mcp/execution/__init__.py +23 -0
  63. eval_protocol/mcp/execution/base_policy.py +227 -0
  64. eval_protocol/mcp/execution/fireworks_policy.py +209 -0
  65. eval_protocol/mcp/execution/manager.py +506 -0
  66. eval_protocol/mcp/execution/policy.py +421 -0
  67. eval_protocol/mcp/grid_renderer.py +54 -0
  68. eval_protocol/mcp/mcpgym.py +637 -0
  69. eval_protocol/mcp/process_manager.py +177 -0
  70. eval_protocol/mcp/session/__init__.py +11 -0
  71. eval_protocol/mcp/session/manager.py +228 -0
  72. eval_protocol/mcp/simple_process_manager.py +291 -0
  73. eval_protocol/mcp/simulation_server.py +458 -0
  74. eval_protocol/mcp/types.py +80 -0
  75. eval_protocol/mcp_agent/__init__.py +1 -0
  76. eval_protocol/mcp_agent/config.py +147 -0
  77. eval_protocol/mcp_agent/intermediary_server.py +542 -0
  78. eval_protocol/mcp_agent/main.py +210 -0
  79. eval_protocol/mcp_agent/orchestration/__init__.py +1 -0
  80. eval_protocol/mcp_agent/orchestration/base_client.py +132 -0
  81. eval_protocol/mcp_agent/orchestration/local_docker_client.py +702 -0
  82. eval_protocol/mcp_agent/orchestration/remote_http_client.py +304 -0
  83. eval_protocol/mcp_agent/orchestration/stdio_mcp_client_helper.py +3 -0
  84. eval_protocol/mcp_agent/session.py +79 -0
  85. eval_protocol/mcp_env.py +304 -0
  86. eval_protocol/models.py +366 -0
  87. eval_protocol/packaging.py +219 -0
  88. eval_protocol/platform_api.py +360 -0
  89. eval_protocol/playback_policy.py +396 -0
  90. eval_protocol/resources.py +128 -0
  91. eval_protocol/reward_function.py +410 -0
  92. eval_protocol/rewards/__init__.py +94 -0
  93. eval_protocol/rewards/accuracy.py +454 -0
  94. eval_protocol/rewards/accuracy_length.py +173 -0
  95. eval_protocol/rewards/apps_coding_reward.py +331 -0
  96. eval_protocol/rewards/apps_execution_utils.py +149 -0
  97. eval_protocol/rewards/apps_testing_util.py +559 -0
  98. eval_protocol/rewards/bfcl_reward.py +313 -0
  99. eval_protocol/rewards/code_execution.py +1620 -0
  100. eval_protocol/rewards/code_execution_utils.py +72 -0
  101. eval_protocol/rewards/cpp_code.py +861 -0
  102. eval_protocol/rewards/deepcoder_reward.py +161 -0
  103. eval_protocol/rewards/format.py +129 -0
  104. eval_protocol/rewards/function_calling.py +541 -0
  105. eval_protocol/rewards/json_schema.py +422 -0
  106. eval_protocol/rewards/language_consistency.py +700 -0
  107. eval_protocol/rewards/lean_prover.py +479 -0
  108. eval_protocol/rewards/length.py +375 -0
  109. eval_protocol/rewards/list_comparison_math_reward.py +221 -0
  110. eval_protocol/rewards/math.py +762 -0
  111. eval_protocol/rewards/multiple_choice_math_reward.py +232 -0
  112. eval_protocol/rewards/reasoning_steps.py +249 -0
  113. eval_protocol/rewards/repetition.py +342 -0
  114. eval_protocol/rewards/tag_count.py +162 -0
  115. eval_protocol/rl_processing.py +82 -0
  116. eval_protocol/server.py +271 -0
  117. eval_protocol/typed_interface.py +260 -0
  118. eval_protocol/utils/__init__.py +8 -0
  119. eval_protocol/utils/batch_evaluation.py +217 -0
  120. eval_protocol/utils/batch_transformation.py +205 -0
  121. eval_protocol/utils/dataset_helpers.py +112 -0
  122. eval_protocol/utils/module_loader.py +56 -0
  123. eval_protocol/utils/packaging_utils.py +108 -0
  124. eval_protocol/utils/static_policy.py +305 -0
  125. eval_protocol-0.0.3.dist-info/METADATA +635 -0
  126. eval_protocol-0.0.3.dist-info/RECORD +130 -0
  127. eval_protocol-0.0.3.dist-info/WHEEL +5 -0
  128. eval_protocol-0.0.3.dist-info/entry_points.txt +4 -0
  129. eval_protocol-0.0.3.dist-info/licenses/LICENSE +201 -0
  130. eval_protocol-0.0.3.dist-info/top_level.txt +2 -0
@@ -0,0 +1,219 @@
1
+ import os
2
+ import sys
3
+ from pathlib import Path
4
+ from typing import Optional, Tuple
5
+
6
+ DEFAULT_PYTHON_VERSION = "3.10"
7
+
8
+
9
+ def _resolve_module_path_and_name(function_ref: str) -> Optional[Tuple[Path, str, str]]:
10
+ """
11
+ Resolves the file system path for the top-level module/package of a function reference
12
+ and the module path to be copied.
13
+
14
+ Args:
15
+ function_ref: e.g., "my_package.my_module.my_function" or "my_module.my_function"
16
+
17
+ Returns:
18
+ A tuple (path_to_copy, top_level_module_name, relative_path_to_copy_as) if resolvable, else None.
19
+ - path_to_copy: Absolute path to the directory or file to be copied.
20
+ - top_level_module_name: The name of the top-level module/package (e.g., "my_package" or "my_module").
21
+ This will be the destination name in the Docker image.
22
+ - relative_path_to_copy_as: The path to use as the destination in the COPY instruction (e.g. "my_package" or "my_module.py")
23
+ """
24
+ parts = function_ref.split(".")
25
+ if not parts:
26
+ return None
27
+
28
+ # Try to find the module/package in sys.path, prioritizing CWD
29
+ # This is a simplified approach. A more robust one might involve inspecting __file__
30
+ # of an imported module, but that requires the module to be importable in the current env.
31
+
32
+ # Check CWD first
33
+ potential_path_str = parts[0] # e.g. "my_package" or "my_module"
34
+
35
+ # Path to check in CWD
36
+ path_in_cwd_dir = Path(os.getcwd()) / potential_path_str
37
+ path_in_cwd_file = Path(os.getcwd()) / f"{potential_path_str}.py"
38
+
39
+ if path_in_cwd_dir.is_dir() and (path_in_cwd_dir / "__init__.py").exists(): # It's a package
40
+ return path_in_cwd_dir, potential_path_str, potential_path_str
41
+ elif path_in_cwd_file.is_file(): # It's a module .py file
42
+ return path_in_cwd_file, potential_path_str, f"{potential_path_str}.py"
43
+
44
+ # Fallback: could search sys.path but that gets complicated for Docker context.
45
+ # For now, assume the module/package is in CWD or a sub-path accessible from CWD.
46
+ # If function_ref is like "subdir.module.func", we assume "subdir" is in CWD.
47
+
48
+ # If parts[0] is not directly in CWD, it might be a deeper structure.
49
+ # This simplistic resolver assumes the first part of function_ref is the item to copy from CWD.
50
+ # e.g. if function_ref is "app.services.rewards.my_func" and "app" is a dir in CWD.
51
+
52
+ # A more robust solution for finding the "root" of the user's code might be needed,
53
+ # or clear instructions on project structure.
54
+ # For now, this handles simple cases: module.py in CWD or package/ in CWD.
55
+
56
+ print(
57
+ f"Warning: Could not reliably resolve path for '{function_ref}'. "
58
+ f"Attempting to use '{parts[0]}' as the top-level module/package name from CWD."
59
+ )
60
+
61
+ # If we couldn't find it as a direct file or package, we can't be sure.
62
+ # This part needs to be more robust or have clearer assumptions.
63
+ # For now, let's assume if it's not found as above, it's an error for Dockerfile generation.
64
+ return None
65
+
66
+
67
+ def generate_dockerfile_content(
68
+ function_ref: str,
69
+ python_version: str = DEFAULT_PYTHON_VERSION,
70
+ eval_protocol_install_source: str = "eval-protocol", # e.g., "eval-protocol", "eval-protocol[dev]", or path to local wheel/sdist
71
+ user_requirements_path: Optional[str] = None, # Path relative to CWD or absolute
72
+ inline_requirements_content: Optional[str] = None, # Direct content for requirements.txt
73
+ service_port: int = 8080,
74
+ ) -> Optional[str]:
75
+ """
76
+ Generates the content for a Dockerfile to serve a given reward function.
77
+
78
+ Args:
79
+ function_ref: Python import string for the reward function (e.g., 'my_module.my_reward_func').
80
+ python_version: The Python version for the base image (e.g., "3.10").
81
+ eval_protocol_install_source: Pip install string for eval-protocol.
82
+ user_requirements_path: Optional path to a requirements.txt for user dependencies.
83
+ inline_requirements_content: Optional string containing the content of requirements.txt.
84
+ service_port: Port the service inside the container will listen on.
85
+
86
+ Returns:
87
+ The Dockerfile content as a string, or None if user code path cannot be resolved.
88
+ """
89
+
90
+ resolved_code = _resolve_module_path_and_name(function_ref)
91
+ if not resolved_code:
92
+ print(
93
+ f"Error: Could not resolve path for function reference '{function_ref}'. "
94
+ "Ensure the first part of the reference (e.g., 'my_module' or 'my_package') "
95
+ "is a Python module (.py file) or package (directory with __init__.py) "
96
+ "in the current working directory."
97
+ )
98
+ return None
99
+
100
+ path_to_copy, top_module_name, copy_dest_name = resolved_code
101
+
102
+ # Determine the source path for COPY relative to the Docker build context (assumed to be CWD)
103
+ # If path_to_copy is absolute, we need its name relative to CWD for the COPY instruction.
104
+ # For simplicity, _resolve_module_path_and_name returns paths that are effectively relative to CWD for copying.
105
+ # So, copy_source_path will be top_module_name (for a package) or f"{top_module_name}.py" (for a module file).
106
+ copy_source_path = copy_dest_name # This is what we determined to copy (e.g. "my_package" or "my_module.py")
107
+
108
+ dockerfile_lines = [
109
+ f"FROM python:{python_version}-slim",
110
+ "",
111
+ "WORKDIR /app",
112
+ "",
113
+ "# Copy the entire application source (build context)",
114
+ "COPY . .", # Copies setup.py, eval_protocol package, user's function module, etc.
115
+ "",
116
+ "# Install reward-kit from local source and its dependencies",
117
+ # This assumes setup.py is configured to install eval_protocol and its deps.
118
+ # Add [dev] if extra dev dependencies are needed by generic_server itself, though unlikely.
119
+ "RUN pip install --no-cache-dir .",
120
+ "",
121
+ ]
122
+
123
+ # The user's reward function module (e.g., dummy_rewards.py) is now copied by "COPY . ."
124
+ # So, the specific COPY for resolved_code is no longer needed here.
125
+ # The function_ref in CMD will be resolved from /app.
126
+
127
+ # Handle user-specific requirements.txt, if provided
128
+ # This should be relative to the build context root (copied by "COPY . .")
129
+ if user_requirements_path:
130
+ # The user_requirements_path is relative to the build context root.
131
+ # "COPY . ." will have copied this file into the /app directory.
132
+ # The RUN command below will install these dependencies if the file exists.
133
+ dockerfile_lines.extend(
134
+ [
135
+ f"# Copy and install user-specific dependencies (if {user_requirements_path} exists in context)",
136
+ # The file is already copied by "COPY . .". We just need to run pip install.
137
+ # The path inside the container will be user_requirements_path relative to /app
138
+ f'RUN if [ -f {user_requirements_path} ]; then pip install --no-cache-dir -r {user_requirements_path}; else echo "User requirements file {user_requirements_path} not found in /app, skipping."; fi',
139
+ "",
140
+ ]
141
+ )
142
+
143
+ # Handle inline requirements content, if provided
144
+ if inline_requirements_content and inline_requirements_content.strip():
145
+ # Escape backslashes and quotes for the echo command
146
+ escaped_requirements = inline_requirements_content.replace("\\", "\\\\").replace("'", "'\\''")
147
+ dockerfile_lines.extend(
148
+ [
149
+ "# Create and install dependencies from inline requirements content",
150
+ f"RUN echo '{escaped_requirements}' > /app/generated_requirements.txt",
151
+ "RUN pip install --no-cache-dir -r /app/generated_requirements.txt",
152
+ "",
153
+ ]
154
+ )
155
+
156
+ dockerfile_lines.extend(
157
+ [
158
+ f"ENV PORT {service_port}",
159
+ f"EXPOSE {service_port}",
160
+ "",
161
+ "# Run the generic server, pointing to the user's function",
162
+ # Using shell form for CMD to allow $PORT expansion
163
+ f"CMD python -m eval_protocol.generic_server {function_ref} --host 0.0.0.0 --port $PORT",
164
+ ]
165
+ )
166
+
167
+ return "\n".join(dockerfile_lines)
168
+
169
+
170
+ if __name__ == "__main__":
171
+ # Example Usage
172
+ print("--- Example 1: Module in CWD ---")
173
+ # Create dummy my_test_reward_module.py
174
+ with open("my_test_reward_module.py", "w") as f:
175
+ f.write("def my_reward_func(): pass\n")
176
+
177
+ dockerfile_content_module = generate_dockerfile_content(
178
+ function_ref="my_test_reward_module.my_reward_func",
179
+ user_requirements_path="dummy_requirements.txt", # Test non-existent req file
180
+ )
181
+ if dockerfile_content_module:
182
+ print("\nGenerated Dockerfile (module):")
183
+ print(dockerfile_content_module)
184
+ os.remove("my_test_reward_module.py")
185
+
186
+ print("\n--- Example 2: Package in CWD ---")
187
+ # Create dummy package my_test_reward_pkg/
188
+ pkg_name = "my_test_reward_pkg"
189
+ Path(pkg_name).mkdir(exist_ok=True)
190
+ with open(Path(pkg_name) / "__init__.py", "w") as f:
191
+ f.write("# Package init\n")
192
+ with open(Path(pkg_name) / "rewards.py", "w") as f:
193
+ f.write("def complex_reward_func(): pass\n")
194
+
195
+ # Create dummy requirements.txt for this package example
196
+ user_reqs_name = "pkg_requirements.txt"
197
+ with open(user_reqs_name, "w") as f:
198
+ f.write("numpy==1.23.0\n")
199
+
200
+ dockerfile_content_pkg = generate_dockerfile_content(
201
+ function_ref="my_test_reward_pkg.rewards.complex_reward_func",
202
+ user_requirements_path=user_reqs_name,
203
+ )
204
+ if dockerfile_content_pkg:
205
+ print("\nGenerated Dockerfile (package):")
206
+ print(dockerfile_content_pkg)
207
+
208
+ # Cleanup
209
+ os.remove(Path(pkg_name) / "rewards.py")
210
+ os.remove(Path(pkg_name) / "__init__.py")
211
+ Path(pkg_name).rmdir()
212
+ os.remove(user_reqs_name)
213
+
214
+ print("\n--- Example 3: Unresolvable function ref ---")
215
+ dockerfile_content_bad = generate_dockerfile_content(function_ref="non_existent_module.some_func")
216
+ if dockerfile_content_bad:
217
+ print(dockerfile_content_bad)
218
+ else:
219
+ print("Dockerfile generation failed as expected for non_existent_module.")
@@ -0,0 +1,360 @@
1
+ # eval_protocol/platform_api.py
2
+ import logging
3
+ import sys
4
+ from typing import Any, Dict, Optional
5
+
6
+ import requests
7
+ from dotenv import find_dotenv, load_dotenv
8
+
9
+ from eval_protocol.auth import (
10
+ get_fireworks_account_id,
11
+ get_fireworks_api_base,
12
+ get_fireworks_api_key,
13
+ )
14
+
15
+ logger = logging.getLogger(__name__)
16
+
17
+ # --- Load .env files ---
18
+ # Attempt to load .env.dev first, then .env as a fallback.
19
+ # This happens when the module is imported.
20
+ # We use override=False (default) so that existing environment variables
21
+ # (e.g., set in the shell) are NOT overridden by .env files.
22
+ ENV_DEV_PATH = find_dotenv(filename=".env.dev", raise_error_if_not_found=False, usecwd=True)
23
+ if ENV_DEV_PATH:
24
+ load_dotenv(dotenv_path=ENV_DEV_PATH, override=False)
25
+ logger.info(f"eval_protocol.platform_api: Loaded environment variables from: {ENV_DEV_PATH}")
26
+ else:
27
+ ENV_PATH = find_dotenv(filename=".env", raise_error_if_not_found=False, usecwd=True)
28
+ if ENV_PATH:
29
+ load_dotenv(dotenv_path=ENV_PATH, override=False)
30
+ logger.info(f"eval_protocol.platform_api: Loaded environment variables from: {ENV_PATH}")
31
+ else:
32
+ logger.info(
33
+ "eval_protocol.platform_api: No .env.dev or .env file found. "
34
+ "Relying on shell/existing environment variables."
35
+ )
36
+ # --- End .env loading ---
37
+
38
+
39
+ class PlatformAPIError(Exception):
40
+ """Custom exception for platform API errors."""
41
+
42
+ def __init__(
43
+ self,
44
+ message: str,
45
+ status_code: Optional[int] = None,
46
+ response_text: Optional[str] = None,
47
+ ):
48
+ super().__init__(message)
49
+ self.status_code = status_code
50
+ self.response_text = response_text
51
+
52
+ def __str__(self) -> str:
53
+ return f"{super().__str__()} (Status: {self.status_code}, Response: {self.response_text or 'N/A'})"
54
+
55
+
56
+ def create_or_update_fireworks_secret(
57
+ account_id: str,
58
+ key_name: str, # This is the identifier for the secret, e.g., "my-eval-api-key"
59
+ secret_value: str,
60
+ api_key: Optional[str] = None,
61
+ api_base: Optional[str] = None,
62
+ ) -> bool:
63
+ """
64
+ Creates a new secret on the Fireworks AI platform or updates it if it already exists.
65
+ The 'name' of the secret in Fireworks API terms is the resource path, while 'keyName' is the identifier.
66
+
67
+ Args:
68
+ account_id: Fireworks Account ID.
69
+ key_name: The identifier for the secret (e.g., "WOLFRAM_ALPHA_API_KEY", "my_eval_shim_key").
70
+ secret_value: The actual secret string.
71
+ api_key: Fireworks API key for authenticating this request. Resolves from env/config if None.
72
+ api_base: Fireworks API base URL. Resolves from env/config if None.
73
+
74
+ Returns:
75
+ True if successful, False otherwise.
76
+ """
77
+ resolved_api_key = api_key or get_fireworks_api_key()
78
+ resolved_api_base = api_base or get_fireworks_api_base()
79
+ resolved_account_id = account_id # Must be provided
80
+
81
+ if not all([resolved_api_key, resolved_api_base, resolved_account_id]):
82
+ logger.error("Missing Fireworks API key, base URL, or account ID for creating/updating secret.")
83
+ return False
84
+
85
+ headers = {
86
+ "Authorization": f"Bearer {resolved_api_key}",
87
+ "Content-Type": "application/json",
88
+ }
89
+
90
+ # The secret_id for GET/PATCH/DELETE operations is the key_name.
91
+ # The 'name' field in the gatewaySecret model for POST/PATCH is a bit ambiguous.
92
+ # For POST (create), the body is gatewaySecret, which has 'name', 'keyName', 'value'.
93
+ # 'name' in POST body is likely just the 'keyName' or 'secret_id' for creation context,
94
+ # as the full resource name 'accounts/.../secrets/...' is server-generated.
95
+ # Let's assume for POST, we send 'keyName' and 'value'.
96
+ # For PATCH, the path contains {secret_id} which is the key_name. The body is also gatewaySecret.
97
+
98
+ # Check if secret exists using GET (path uses secret_id which is our key_name)
99
+ get_url = f"{resolved_api_base.rstrip('/')}/v1/accounts/{resolved_account_id}/secrets/{key_name}"
100
+ secret_exists = False
101
+ try:
102
+ response = requests.get(get_url, headers=headers, timeout=10)
103
+ if response.status_code == 200:
104
+ secret_exists = True
105
+ logger.info(f"Secret '{key_name}' already exists. Will attempt to update.")
106
+ elif response.status_code == 404:
107
+ logger.info(f"Secret '{key_name}' does not exist. Will attempt to create.")
108
+ secret_exists = False
109
+ elif response.status_code == 500: # As per user feedback, 500 on GET might mean not found
110
+ logger.warning(
111
+ f"Received 500 error when checking for secret '{key_name}'. Assuming it does not exist and will attempt to create. Response: {response.text}"
112
+ )
113
+ secret_exists = False
114
+ else:
115
+ logger.error(f"Error checking for secret '{key_name}': {response.status_code} - {response.text}")
116
+ return False
117
+ except requests.exceptions.RequestException as e:
118
+ logger.error(f"Request exception while checking for secret '{key_name}': {e}")
119
+ return False
120
+
121
+ if secret_exists:
122
+ # Update existing secret (PATCH)
123
+ patch_url = f"{resolved_api_base.rstrip('/')}/v1/accounts/{resolved_account_id}/secrets/{key_name}"
124
+ # Body for PATCH requires 'keyName' and 'value'.
125
+ # Transform key_name for payload: uppercase and underscores
126
+ payload_key_name = key_name.upper().replace("-", "_")
127
+ # Ensure it starts with an uppercase letter (though .upper() should handle it)
128
+ if not payload_key_name or not payload_key_name[0].isupper():
129
+ # This case should be rare if key_name is not empty and contains letters
130
+ logger.warning(
131
+ f"Could not transform key_name '{key_name}' to valid starting uppercase for payload. Using default 'EP_SECRET.'"
132
+ )
133
+ payload_key_name = "EP_SECRET" # Fallback, though unlikely needed with .upper()
134
+
135
+ payload = {"keyName": payload_key_name, "value": secret_value}
136
+ try:
137
+ logger.debug(f"PATCH payload for '{key_name}': {payload}")
138
+ response = requests.patch(patch_url, headers=headers, json=payload, timeout=30)
139
+ response.raise_for_status()
140
+ logger.info(f"Successfully updated secret '{key_name}' on Fireworks platform.")
141
+ return True
142
+ except requests.exceptions.HTTPError as e:
143
+ logger.error(f"HTTP error updating secret '{key_name}': {e.response.status_code} - {e.response.text}")
144
+ return False
145
+ except requests.exceptions.RequestException as e:
146
+ logger.error(f"Request exception updating secret '{key_name}': {e}")
147
+ return False
148
+ else:
149
+ # Create new secret (POST)
150
+ post_url = f"{resolved_api_base.rstrip('/')}/v1/accounts/{resolved_account_id}/secrets"
151
+ # Body for POST is gatewaySecret. 'name' field in payload is tricky.
152
+ # Let's assume for POST, the 'name' in payload can be omitted or is the key_name.
153
+ # The API should ideally use 'keyName' from URL or a specific 'secretId' in payload for creation if 'name' is server-assigned.
154
+ # Given the Swagger, 'name' is required in gatewaySecret.
155
+ # Let's try with 'name' being the 'key_name' for the payload, as the full path is not known yet.
156
+ # This might need adjustment based on actual API behavior.
157
+ # Construct the full 'name' path for the POST payload as per Swagger's title for 'name'
158
+ full_resource_name_for_payload = (
159
+ f"accounts/{resolved_account_id}/secrets/{key_name}" # Path uses lowercase-hyphenated key_name
160
+ )
161
+
162
+ # Transform key_name for payload "keyName" field: uppercase and underscores
163
+ payload_key_name = key_name.upper().replace("-", "_")
164
+ if not payload_key_name or not payload_key_name[0].isupper():
165
+ logger.warning(
166
+ f"Could not transform key_name '{key_name}' to valid starting uppercase for payload. Using default 'EP_SECRET.'"
167
+ )
168
+ payload_key_name = "EP_SECRET"
169
+
170
+ payload = {
171
+ "name": full_resource_name_for_payload, # This 'name' is the resource path
172
+ "keyName": payload_key_name, # This 'keyName' is the specific field with new rules
173
+ "value": secret_value,
174
+ }
175
+ try:
176
+ logger.debug(f"POST payload for '{key_name}': {payload}")
177
+ response = requests.post(post_url, headers=headers, json=payload, timeout=30)
178
+ response.raise_for_status()
179
+ logger.info(
180
+ f"Successfully created secret '{key_name}' on Fireworks platform. Full name: {response.json().get('name')}"
181
+ )
182
+ return True
183
+ except requests.exceptions.HTTPError as e:
184
+ logger.error(f"HTTP error creating secret '{key_name}': {e.response.status_code} - {e.response.text}")
185
+ # If error is due to 'name' field, this log will show it.
186
+ return False
187
+ except requests.exceptions.RequestException as e:
188
+ logger.error(f"Request exception creating secret '{key_name}': {e}")
189
+ return False
190
+
191
+
192
+ def get_fireworks_secret(
193
+ account_id: str,
194
+ key_name: str, # This is the identifier for the secret
195
+ api_key: Optional[str] = None,
196
+ api_base: Optional[str] = None,
197
+ ) -> Optional[Dict[str, Any]]:
198
+ """
199
+ Retrieves a secret from the Fireworks AI platform by its keyName.
200
+ Note: This typically does not return the secret's actual value for security reasons,
201
+ but rather its metadata.
202
+ """
203
+ resolved_api_key = api_key or get_fireworks_api_key()
204
+ resolved_api_base = api_base or get_fireworks_api_base()
205
+ resolved_account_id = account_id
206
+
207
+ if not all([resolved_api_key, resolved_api_base, resolved_account_id]):
208
+ logger.error("Missing Fireworks API key, base URL, or account ID for getting secret.")
209
+ return None
210
+
211
+ headers = {"Authorization": f"Bearer {resolved_api_key}"}
212
+ url = f"{resolved_api_base.rstrip('/')}/v1/accounts/{resolved_account_id}/secrets/{key_name}"
213
+
214
+ try:
215
+ response = requests.get(url, headers=headers, timeout=10)
216
+ if response.status_code == 200:
217
+ logger.info(f"Successfully retrieved secret '{key_name}'.")
218
+ return response.json()
219
+ elif response.status_code == 404:
220
+ logger.info(f"Secret '{key_name}' not found.")
221
+ return None
222
+ else:
223
+ logger.error(f"Error getting secret '{key_name}': {response.status_code} - {response.text}")
224
+ return None
225
+ except requests.exceptions.RequestException as e:
226
+ logger.error(f"Request exception while getting secret '{key_name}': {e}")
227
+ return None
228
+
229
+
230
+ def delete_fireworks_secret(
231
+ account_id: str,
232
+ key_name: str, # This is the identifier for the secret
233
+ api_key: Optional[str] = None,
234
+ api_base: Optional[str] = None,
235
+ ) -> bool:
236
+ """
237
+ Deletes a secret from the Fireworks AI platform by its keyName.
238
+ """
239
+ resolved_api_key = api_key or get_fireworks_api_key()
240
+ resolved_api_base = api_base or get_fireworks_api_base()
241
+ resolved_account_id = account_id
242
+
243
+ if not all([resolved_api_key, resolved_api_base, resolved_account_id]):
244
+ logger.error("Missing Fireworks API key, base URL, or account ID for deleting secret.")
245
+ return False
246
+
247
+ headers = {"Authorization": f"Bearer {resolved_api_key}"}
248
+ url = f"{resolved_api_base.rstrip('/')}/v1/accounts/{resolved_account_id}/secrets/{key_name}"
249
+
250
+ try:
251
+ response = requests.delete(url, headers=headers, timeout=30)
252
+ if response.status_code == 200 or response.status_code == 204: # 204 No Content is also success for DELETE
253
+ logger.info(f"Successfully deleted secret '{key_name}'.")
254
+ return True
255
+ elif response.status_code == 404:
256
+ logger.info(f"Secret '{key_name}' not found, nothing to delete.")
257
+ return True
258
+ elif (
259
+ response.status_code == 500
260
+ ): # As per user feedback, 500 on GET might mean not found, apply same logic for DELETE
261
+ logger.warning(
262
+ f"Received 500 error when deleting secret '{key_name}'. Assuming it might not have existed. Response: {response.text}"
263
+ )
264
+ return True # Consider deletion successful if it results in non-existence
265
+ else:
266
+ logger.error(f"Error deleting secret '{key_name}': {response.status_code} - {response.text}")
267
+ return False
268
+ except requests.exceptions.RequestException as e:
269
+ logger.error(f"Request exception while deleting secret '{key_name}': {e}")
270
+ return False
271
+
272
+
273
+ if __name__ == "__main__":
274
+ # Example usage for manual testing of secret management
275
+ logging.basicConfig(
276
+ level=logging.INFO,
277
+ format="%(asctime)s - %(levelname)s - %(name)s - %(message)s",
278
+ )
279
+
280
+ # Note: .env file loading is now handled at the module level when platform_api.py is imported.
281
+ # The section that was here for loading .env files specifically for __main__ has been removed
282
+ # to rely on the module-level loading.
283
+
284
+ # These should be set in your .env.dev, .env file (or shell environment) for this test to run
285
+ # FIREWORKS_API_KEY="your_fireworks_api_key"
286
+ # FIREWORKS_ACCOUNT_ID="your_fireworks_account_id"
287
+ # FIREWORKS_API_BASE="https://api.fireworks.ai" # or your dev/staging endpoint
288
+
289
+ test_account_id = get_fireworks_account_id()
290
+ test_api_key = get_fireworks_api_key() # Not passed directly, functions will resolve
291
+ test_api_base = get_fireworks_api_base()
292
+
293
+ logger.info(f"Attempting to use the following configuration for testing Fireworks secrets API:")
294
+ logger.info(f" Resolved FIREWORKS_ACCOUNT_ID: {test_account_id}")
295
+ logger.info(f" Resolved FIREWORKS_API_BASE: {test_api_base}")
296
+ logger.info(
297
+ f" Resolved FIREWORKS_API_KEY: {'********' + test_api_key[-4:] if test_api_key and len(test_api_key) > 4 else 'Not set or too short'}"
298
+ )
299
+
300
+ if not test_account_id or not test_api_key or not test_api_base:
301
+ logger.error(
302
+ "CRITICAL: FIREWORKS_ACCOUNT_ID, FIREWORKS_API_KEY, and FIREWORKS_API_BASE must be correctly set in environment or .env file to run this test."
303
+ )
304
+ import sys # Make sure sys is imported if using sys.exit
305
+
306
+ sys.exit(1)
307
+
308
+ test_secret_key_name = "rewardkit-test-secret-delete-me" # Changed to be valid
309
+ test_secret_value = "test_secret_value_12345"
310
+ updated_secret_value = "updated_secret_value_67890"
311
+
312
+ logger.info(f"--- Testing Fireworks Secret Management for account: {test_account_id} ---")
313
+
314
+ # 1. Ensure it doesn't exist initially (or delete if it does from a previous failed run)
315
+ logger.info(f"\n[Test Step 0] Attempting to delete '{test_secret_key_name}' if it exists (cleanup)...")
316
+ delete_fireworks_secret(test_account_id, test_secret_key_name)
317
+ retrieved = get_fireworks_secret(test_account_id, test_secret_key_name)
318
+ if retrieved is None:
319
+ logger.info(f"Confirmed secret '{test_secret_key_name}' does not exist before creation test.")
320
+ else:
321
+ logger.error(f"Secret '{test_secret_key_name}' still exists after cleanup attempt. Manual check needed.")
322
+ # sys.exit(1) # Optional: make it fatal
323
+
324
+ # 2. Create secret
325
+ logger.info(f"\n[Test Step 1] Creating secret '{test_secret_key_name}' with value '{test_secret_value}'...")
326
+ success_create = create_or_update_fireworks_secret(test_account_id, test_secret_key_name, test_secret_value)
327
+ logger.info(f"Create operation success: {success_create}")
328
+
329
+ # 3. Get secret (to verify creation, though value won't be returned)
330
+ logger.info(f"\n[Test Step 2] Getting secret '{test_secret_key_name}'...")
331
+ retrieved_after_create = get_fireworks_secret(test_account_id, test_secret_key_name)
332
+ if retrieved_after_create:
333
+ logger.info(f"Retrieved secret metadata: {retrieved_after_create}")
334
+ # Assert against the transformed keyName that's expected in the payload/response body
335
+ expected_payload_key_name = test_secret_key_name.upper().replace("-", "_")
336
+ assert retrieved_after_create.get("keyName") == expected_payload_key_name
337
+ assert retrieved_after_create.get("value") == test_secret_value # Also check value if returned
338
+ else:
339
+ logger.error(f"Failed to retrieve secret '{test_secret_key_name}' after creation.")
340
+
341
+ # 4. Update secret
342
+ logger.info(f"\n[Test Step 3] Updating secret '{test_secret_key_name}' with value '{updated_secret_value}'...")
343
+ success_update = create_or_update_fireworks_secret(test_account_id, test_secret_key_name, updated_secret_value)
344
+ logger.info(f"Update operation success: {success_update}")
345
+ # (Getting again won't show the value, so we assume PATCH worked if it returned True)
346
+
347
+ # 5. Delete secret
348
+ logger.info(f"\n[Test Step 4] Deleting secret '{test_secret_key_name}'...")
349
+ success_delete = delete_fireworks_secret(test_account_id, test_secret_key_name)
350
+ logger.info(f"Delete operation success: {success_delete}")
351
+
352
+ # 6. Get secret (to verify deletion)
353
+ logger.info(f"\n[Test Step 5] Getting secret '{test_secret_key_name}' again to confirm deletion...")
354
+ retrieved_after_delete = get_fireworks_secret(test_account_id, test_secret_key_name)
355
+ if retrieved_after_delete is None:
356
+ logger.info(f"Secret '{test_secret_key_name}' successfully confirmed as deleted.")
357
+ else:
358
+ logger.error(f"Secret '{test_secret_key_name}' still exists after delete operation: {retrieved_after_delete}")
359
+
360
+ logger.info("\n--- Test script finished ---")