continuum-task-server-bundle 1.0.0__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 (90) hide show
  1. continuum_task_server_bundle/__init__.py +1 -0
  2. continuum_task_server_bundle/daytona/__init__.py +4 -0
  3. continuum_task_server_bundle/daytona/common.py +534 -0
  4. continuum_task_server_bundle/daytona/create_sandbox.py +166 -0
  5. continuum_task_server_bundle/daytona/dispatch.py +125 -0
  6. continuum_task_server_bundle/daytona/execute_sandbox.py +243 -0
  7. continuum_task_server_bundle/daytona/filesystem.py +330 -0
  8. continuum_task_server_bundle/daytona/fs_edit.py +127 -0
  9. continuum_task_server_bundle/daytona/git_ops.py +585 -0
  10. continuum_task_server_bundle/daytona/github_app.py +207 -0
  11. continuum_task_server_bundle/daytona/manage_sandbox.py +104 -0
  12. continuum_task_server_bundle/daytona/plugin.py +18 -0
  13. continuum_task_server_bundle/daytona/s3_transfer.py +207 -0
  14. continuum_task_server_bundle/discord/__init__.py +4 -0
  15. continuum_task_server_bundle/discord/attachment_registry_db.py +127 -0
  16. continuum_task_server_bundle/discord/attachment_resolve.py +213 -0
  17. continuum_task_server_bundle/discord/await_cleanup.py +105 -0
  18. continuum_task_server_bundle/discord/discord_ready.py +39 -0
  19. continuum_task_server_bundle/discord/message_await_apply.py +143 -0
  20. continuum_task_server_bundle/discord/message_await_db.py +197 -0
  21. continuum_task_server_bundle/discord/message_await_inbound.py +54 -0
  22. continuum_task_server_bundle/discord/message_await_logic.py +133 -0
  23. continuum_task_server_bundle/discord/message_await_poller.py +122 -0
  24. continuum_task_server_bundle/discord/message_chunking.py +45 -0
  25. continuum_task_server_bundle/discord/methods.py +408 -0
  26. continuum_task_server_bundle/discord/plugin.py +105 -0
  27. continuum_task_server_bundle/discord/reaction_await_apply.py +238 -0
  28. continuum_task_server_bundle/discord/reaction_await_db.py +174 -0
  29. continuum_task_server_bundle/discord/reaction_await_inbound.py +47 -0
  30. continuum_task_server_bundle/discord/reaction_await_logic.py +118 -0
  31. continuum_task_server_bundle/discord/reaction_await_poller.py +131 -0
  32. continuum_task_server_bundle/discord/s3_attachment_config.py +25 -0
  33. continuum_task_server_bundle/discord/s3_message_attachments.py +139 -0
  34. continuum_task_server_bundle/discord/sql/001_message_awaits.sql +112 -0
  35. continuum_task_server_bundle/discord/sql/002_reaction_awaits.sql +61 -0
  36. continuum_task_server_bundle/github/__init__.py +4 -0
  37. continuum_task_server_bundle/github/awaits/__init__.py +1 -0
  38. continuum_task_server_bundle/github/awaits/apply.py +92 -0
  39. continuum_task_server_bundle/github/awaits/cleanup.py +94 -0
  40. continuum_task_server_bundle/github/awaits/db.py +220 -0
  41. continuum_task_server_bundle/github/awaits/evaluators.py +325 -0
  42. continuum_task_server_bundle/github/awaits/fetch.py +192 -0
  43. continuum_task_server_bundle/github/awaits/poller.py +82 -0
  44. continuum_task_server_bundle/github/awaits/register.py +37 -0
  45. continuum_task_server_bundle/github/ci_status.py +70 -0
  46. continuum_task_server_bundle/github/comment_normalize.py +76 -0
  47. continuum_task_server_bundle/github/errors.py +52 -0
  48. continuum_task_server_bundle/github/github_app.py +291 -0
  49. continuum_task_server_bundle/github/github_http.py +377 -0
  50. continuum_task_server_bundle/github/methods/__init__.py +90 -0
  51. continuum_task_server_bundle/github/methods/_common.py +183 -0
  52. continuum_task_server_bundle/github/methods/awaits.py +107 -0
  53. continuum_task_server_bundle/github/methods/ci.py +158 -0
  54. continuum_task_server_bundle/github/methods/comments.py +214 -0
  55. continuum_task_server_bundle/github/methods/credentials.py +59 -0
  56. continuum_task_server_bundle/github/methods/issues.py +131 -0
  57. continuum_task_server_bundle/github/methods/pulls.py +329 -0
  58. continuum_task_server_bundle/github/methods/repos.py +424 -0
  59. continuum_task_server_bundle/github/methods/search.py +109 -0
  60. continuum_task_server_bundle/github/plugin.py +91 -0
  61. continuum_task_server_bundle/github/sql/001_github_awaits.sql +65 -0
  62. continuum_task_server_bundle/github/webhook_server.py +126 -0
  63. continuum_task_server_bundle/jira/__init__.py +4 -0
  64. continuum_task_server_bundle/jira/await_poller.py +236 -0
  65. continuum_task_server_bundle/jira/await_store.py +271 -0
  66. continuum_task_server_bundle/jira/config.py +68 -0
  67. continuum_task_server_bundle/jira/jira_client.py +382 -0
  68. continuum_task_server_bundle/jira/methods.py +246 -0
  69. continuum_task_server_bundle/jira/plugin.py +86 -0
  70. continuum_task_server_bundle/jira/sql/001_jira_issue_comment_waits.sql +19 -0
  71. continuum_task_server_bundle/jira/webhook.py +70 -0
  72. continuum_task_server_bundle/openrouter/__init__.py +4 -0
  73. continuum_task_server_bundle/openrouter/chat_service.py +1037 -0
  74. continuum_task_server_bundle/openrouter/chat_store.py +849 -0
  75. continuum_task_server_bundle/openrouter/environment.py +29 -0
  76. continuum_task_server_bundle/openrouter/handler.py +59 -0
  77. continuum_task_server_bundle/openrouter/image_context.py +207 -0
  78. continuum_task_server_bundle/openrouter/openrouter_client.py +324 -0
  79. continuum_task_server_bundle/openrouter/plugin.py +37 -0
  80. continuum_task_server_bundle/openrouter/prompt_projection.py +417 -0
  81. continuum_task_server_bundle/openrouter/sql/chat.sql +50 -0
  82. continuum_task_server_bundle/openrouter/sql/migrate_prune_then_chunk_v1.sql +11 -0
  83. continuum_task_server_bundle/roblox/__init__.py +4 -0
  84. continuum_task_server_bundle/roblox/open_cloud.py +335 -0
  85. continuum_task_server_bundle/roblox/plugin.py +30 -0
  86. continuum_task_server_bundle/roblox/storage_s3.py +55 -0
  87. continuum_task_server_bundle-1.0.0.dist-info/METADATA +109 -0
  88. continuum_task_server_bundle-1.0.0.dist-info/RECORD +90 -0
  89. continuum_task_server_bundle-1.0.0.dist-info/WHEEL +5 -0
  90. continuum_task_server_bundle-1.0.0.dist-info/top_level.txt +1 -0
@@ -0,0 +1,125 @@
1
+ from daytona import Daytona, DaytonaError
2
+
3
+ from continuum_task_server_bundle.daytona.common import (
4
+ HandlerError,
5
+ error_response,
6
+ map_exception,
7
+ require_object,
8
+ )
9
+ from continuum_task_server_bundle.daytona.create_sandbox import create_sandbox
10
+ from continuum_task_server_bundle.daytona.execute_sandbox import (
11
+ delete_exec_session,
12
+ execute_sandbox,
13
+ get_exec_logs,
14
+ get_exec_status,
15
+ wait_exec,
16
+ )
17
+ from continuum_task_server_bundle.daytona.filesystem import (
18
+ fs_chmod,
19
+ fs_delete,
20
+ fs_info,
21
+ fs_list,
22
+ fs_mkdir,
23
+ fs_move,
24
+ fs_read,
25
+ fs_replace,
26
+ fs_search,
27
+ fs_write,
28
+ )
29
+ from continuum_task_server_bundle.daytona.fs_edit import fs_edit
30
+ from continuum_task_server_bundle.daytona.git_ops import (
31
+ git_add,
32
+ git_authenticate,
33
+ git_branches,
34
+ git_checkout,
35
+ git_clone,
36
+ git_commit,
37
+ git_configure_user,
38
+ git_create_branch,
39
+ git_delete_branch,
40
+ git_get_config,
41
+ git_init,
42
+ git_pull,
43
+ git_push,
44
+ git_remote_add,
45
+ git_remote_get,
46
+ git_remotes,
47
+ git_reset,
48
+ git_restore,
49
+ git_set_config,
50
+ git_status,
51
+ )
52
+ from continuum_task_server_bundle.daytona.manage_sandbox import (
53
+ delete_sandbox,
54
+ get_preview_link,
55
+ get_sandbox_info,
56
+ start_sandbox,
57
+ stop_sandbox,
58
+ )
59
+ from continuum_task_server_bundle.daytona.s3_transfer import s3_to_sandbox, sandbox_to_s3
60
+
61
+ HANDLERS = {
62
+ "create_sandbox": create_sandbox,
63
+ "get_sandbox": get_sandbox_info,
64
+ "start_sandbox": start_sandbox,
65
+ "stop_sandbox": stop_sandbox,
66
+ "delete_sandbox": delete_sandbox,
67
+ "get_preview_link": get_preview_link,
68
+ "exec_sandbox": execute_sandbox,
69
+ "get_exec_status": get_exec_status,
70
+ "wait_exec": wait_exec,
71
+ "get_exec_logs": get_exec_logs,
72
+ "delete_exec_session": delete_exec_session,
73
+ "fs_read": fs_read,
74
+ "fs_write": fs_write,
75
+ "fs_list": fs_list,
76
+ "fs_info": fs_info,
77
+ "fs_mkdir": fs_mkdir,
78
+ "fs_delete": fs_delete,
79
+ "fs_move": fs_move,
80
+ "fs_search": fs_search,
81
+ "fs_replace": fs_replace,
82
+ "fs_edit": fs_edit,
83
+ "fs_chmod": fs_chmod,
84
+ "git_clone": git_clone,
85
+ "git_status": git_status,
86
+ "git_branches": git_branches,
87
+ "git_checkout": git_checkout,
88
+ "git_create_branch": git_create_branch,
89
+ "git_delete_branch": git_delete_branch,
90
+ "git_add": git_add,
91
+ "git_commit": git_commit,
92
+ "git_push": git_push,
93
+ "git_pull": git_pull,
94
+ "git_init": git_init,
95
+ "git_reset": git_reset,
96
+ "git_restore": git_restore,
97
+ "git_remote_add": git_remote_add,
98
+ "git_remotes": git_remotes,
99
+ "git_remote_get": git_remote_get,
100
+ "git_set_config": git_set_config,
101
+ "git_get_config": git_get_config,
102
+ "git_configure_user": git_configure_user,
103
+ "git_authenticate": git_authenticate,
104
+ "s3_to_sandbox": s3_to_sandbox,
105
+ "sandbox_to_s3": sandbox_to_s3,
106
+ }
107
+
108
+
109
+ def dispatch(daytona: Daytona, definition: dict, input_data) -> dict:
110
+ try:
111
+ definition = require_object(definition, "item_definition_json")
112
+ method = definition.get("method")
113
+ if not isinstance(method, str) or not method.strip():
114
+ raise HandlerError("Method is required")
115
+ handler = HANDLERS.get(method.strip())
116
+ if handler is None:
117
+ raise HandlerError(f"Method not found: {method}", "method_not_found")
118
+ parsed_input = require_object(input_data, "input_data_json")
119
+ return handler(daytona, definition, parsed_input)
120
+ except HandlerError as exc:
121
+ return error_response(exc)
122
+ except DaytonaError as exc:
123
+ return error_response(exc)
124
+ except Exception as exc:
125
+ return error_response(map_exception(exc))
@@ -0,0 +1,243 @@
1
+ import shlex
2
+ import time
3
+ import uuid
4
+
5
+ from daytona import (
6
+ Daytona,
7
+ SessionExecuteRequest,
8
+ )
9
+
10
+ from continuum_task_server_bundle.daytona.common import (
11
+ DAYTONA_TIMEOUT_MESSAGE,
12
+ HandlerError,
13
+ call_daytona,
14
+ effective_limit,
15
+ get_running_sandbox,
16
+ get_sandbox,
17
+ optional_bool,
18
+ optional_str,
19
+ request_timeout_for_sandbox,
20
+ require_env_vars,
21
+ require_sandbox_id,
22
+ require_str,
23
+ resolve_cwd,
24
+ resolve_timeout,
25
+ success,
26
+ truncate_text,
27
+ )
28
+
29
+
30
+ def execute_sandbox(daytona: Daytona, definition: dict, input_data: dict) -> dict:
31
+ cmd = require_str(input_data, "cmd")
32
+ env = require_env_vars(input_data, "env")
33
+ cwd = resolve_cwd(input_data)
34
+ background = optional_bool(input_data, "background")
35
+ if background is None:
36
+ background = optional_bool(definition, "background") or False
37
+ sandbox = get_running_sandbox(daytona, input_data, definition)
38
+
39
+ if background:
40
+ return _execute_background(sandbox, definition, input_data, cmd, env, cwd)
41
+ return _execute_foreground(sandbox, definition, input_data, cmd, env, cwd)
42
+
43
+
44
+ def get_exec_status(daytona: Daytona, definition: dict, input_data: dict) -> dict:
45
+ sandbox = get_running_sandbox(daytona, input_data, definition)
46
+ session_id = require_str(input_data, "session_id")
47
+ command_id = require_str(input_data, "command_id")
48
+ timeout = request_timeout_for_sandbox(sandbox)
49
+ command = call_daytona(
50
+ lambda: sandbox.process.get_session_command(
51
+ session_id,
52
+ command_id,
53
+ request_timeout=timeout,
54
+ ),
55
+ retry=True,
56
+ )
57
+ exit_code = getattr(command, "exit_code", None)
58
+ return success(
59
+ {
60
+ "session_id": session_id,
61
+ "command_id": getattr(command, "id", command_id),
62
+ "command": getattr(command, "command", None),
63
+ "exit_code": exit_code,
64
+ "completed": exit_code is not None,
65
+ }
66
+ )
67
+
68
+
69
+
70
+ EXEC_WAIT_POLL_INTERVAL_SECONDS = 5
71
+ MAX_CONSECUTIVE_POLL_FAILURES = 3
72
+ _TRANSIENT_POLL_CODES = {"timeout", "daytona_error"}
73
+
74
+
75
+ def wait_exec(daytona: Daytona, definition: dict, input_data: dict) -> dict:
76
+ """Hold the queue item until a background command has a terminal exit code.
77
+
78
+ This deliberately does not call get_running_sandbox(): restarting a stopped
79
+ sandbox would hide the fact that its background command is no longer live.
80
+ While this handler is active, TaskServer keeps the queue item heartbeated.
81
+ """
82
+ sandbox = get_sandbox(daytona, require_sandbox_id(input_data))
83
+ session_id = require_str(input_data, "session_id")
84
+ command_id = require_str(input_data, "command_id")
85
+ consecutive_failures = 0
86
+
87
+ while True:
88
+ try:
89
+ command = call_daytona(
90
+ lambda: sandbox.process.get_session_command(
91
+ session_id,
92
+ command_id,
93
+ request_timeout=request_timeout_for_sandbox(sandbox),
94
+ ),
95
+ retry=True,
96
+ )
97
+ except HandlerError as exc:
98
+ if exc.code not in _TRANSIENT_POLL_CODES:
99
+ raise
100
+ consecutive_failures += 1
101
+ if consecutive_failures >= MAX_CONSECUTIVE_POLL_FAILURES:
102
+ if exc.code == "timeout" and exc.message == DAYTONA_TIMEOUT_MESSAGE:
103
+ raise
104
+ raise HandlerError(DAYTONA_TIMEOUT_MESSAGE, "timeout") from exc
105
+ time.sleep(EXEC_WAIT_POLL_INTERVAL_SECONDS)
106
+ continue
107
+
108
+ consecutive_failures = 0
109
+ exit_code = getattr(command, "exit_code", None)
110
+ if exit_code is not None:
111
+ return success(
112
+ {
113
+ "sandbox_id": require_sandbox_id(input_data),
114
+ "session_id": session_id,
115
+ "command_id": getattr(command, "id", command_id),
116
+ "command": getattr(command, "command", None),
117
+ "exit_code": exit_code,
118
+ "completed": True,
119
+ }
120
+ )
121
+ time.sleep(EXEC_WAIT_POLL_INTERVAL_SECONDS)
122
+
123
+
124
+ def get_exec_logs(daytona: Daytona, definition: dict, input_data: dict) -> dict:
125
+ sandbox = get_running_sandbox(daytona, input_data, definition)
126
+ session_id = require_str(input_data, "session_id")
127
+ command_id = require_str(input_data, "command_id")
128
+ timeout = request_timeout_for_sandbox(sandbox)
129
+ logs = call_daytona(
130
+ lambda: sandbox.process.get_session_command_logs(
131
+ session_id,
132
+ command_id,
133
+ request_timeout=timeout,
134
+ ),
135
+ retry=True,
136
+ )
137
+ max_bytes = effective_limit(definition, "max_output_bytes")
138
+ max_lines = effective_limit(definition, "max_output_lines")
139
+ stdout = truncate_text(getattr(logs, "stdout", None), max_bytes=max_bytes, max_lines=max_lines)
140
+ stderr = truncate_text(getattr(logs, "stderr", None), max_bytes=max_bytes, max_lines=max_lines)
141
+ output = truncate_text(getattr(logs, "output", None), max_bytes=max_bytes, max_lines=max_lines)
142
+ return success(
143
+ {
144
+ "session_id": session_id,
145
+ "command_id": command_id,
146
+ "stdout": stdout["text"],
147
+ "stderr": stderr["text"],
148
+ "output": output["text"],
149
+ "truncated": stdout["truncated"] or stderr["truncated"] or output["truncated"],
150
+ "truncated_lines": stdout["truncated_lines"]
151
+ or stderr["truncated_lines"]
152
+ or output["truncated_lines"],
153
+ "truncated_bytes": stdout["truncated_bytes"]
154
+ or stderr["truncated_bytes"]
155
+ or output["truncated_bytes"],
156
+ }
157
+ )
158
+
159
+
160
+ def delete_exec_session(daytona: Daytona, definition: dict, input_data: dict) -> dict:
161
+ sandbox = get_running_sandbox(daytona, input_data, definition)
162
+ session_id = require_str(input_data, "session_id")
163
+ timeout = request_timeout_for_sandbox(sandbox)
164
+ call_daytona(
165
+ lambda: sandbox.process.delete_session(session_id, request_timeout=timeout),
166
+ retry=False,
167
+ )
168
+ return success({"session_id": session_id, "deleted": True})
169
+
170
+
171
+ def _execute_foreground(sandbox, definition, input_data, cmd, env, cwd) -> dict:
172
+ timeout = resolve_timeout(
173
+ input_data,
174
+ definition,
175
+ ceiling_key="max_exec_timeout",
176
+ default=effective_limit(definition, "max_exec_timeout"),
177
+ )
178
+ response = call_daytona(
179
+ lambda: sandbox.process.exec(cmd, cwd=cwd, env=env, timeout=timeout),
180
+ retry=False,
181
+ )
182
+ max_bytes = effective_limit(definition, "max_output_bytes")
183
+ max_lines = effective_limit(definition, "max_output_lines")
184
+ truncated = truncate_text(
185
+ getattr(response, "result", None),
186
+ max_bytes=max_bytes,
187
+ max_lines=max_lines,
188
+ )
189
+ payload = {
190
+ "result": truncated["text"],
191
+ "exit_code": getattr(response, "exit_code", None),
192
+ "background": False,
193
+ "truncated": truncated["truncated"],
194
+ "truncated_lines": truncated["truncated_lines"],
195
+ "truncated_bytes": truncated["truncated_bytes"],
196
+ "total_lines": truncated["total_lines"],
197
+ }
198
+ return success(payload)
199
+
200
+
201
+ def _execute_background(sandbox, definition, input_data, cmd, env, cwd) -> dict:
202
+ session_id = optional_str(input_data, "session_id") or uuid.uuid4().hex
203
+ wrapped = _wrap_command(cmd, cwd=cwd, env=env)
204
+ request_timeout = request_timeout_for_sandbox(sandbox)
205
+ try:
206
+ call_daytona(
207
+ lambda: sandbox.process.create_session(session_id, request_timeout=request_timeout),
208
+ retry=False,
209
+ )
210
+ except HandlerError as exc:
211
+ if exc.code != "conflict":
212
+ raise
213
+ response = call_daytona(
214
+ lambda: sandbox.process.execute_session_command(
215
+ session_id,
216
+ SessionExecuteRequest(command=wrapped, run_async=True),
217
+ timeout=request_timeout,
218
+ ),
219
+ retry=False,
220
+ )
221
+ command_id = getattr(response, "cmd_id", None)
222
+ if not command_id:
223
+ raise HandlerError("Daytona did not return a command id", "daytona_error")
224
+ return success(
225
+ {
226
+ "background": True,
227
+ "session_id": session_id,
228
+ "command_id": command_id,
229
+ }
230
+ )
231
+
232
+
233
+ def _wrap_command(cmd: str, *, cwd: str | None, env: dict[str, str] | None) -> str:
234
+ parts: list[str] = []
235
+ if env:
236
+ for key, value in env.items():
237
+ parts.append(f"export {shlex.quote(key)}={shlex.quote(value)}")
238
+ body = cmd
239
+ if cwd:
240
+ body = f"cd {shlex.quote(cwd)} && {cmd}"
241
+ if parts:
242
+ return f"{'; '.join(parts)}; {body}"
243
+ return body
@@ -0,0 +1,330 @@
1
+ import base64
2
+
3
+ from daytona import Daytona
4
+
5
+ from continuum_task_server_bundle.daytona.common import (
6
+ HandlerError,
7
+ call_daytona,
8
+ effective_limit,
9
+ get_running_sandbox,
10
+ optional_bool,
11
+ optional_int,
12
+ optional_str,
13
+ request_timeout_for_sandbox,
14
+ require_path,
15
+ require_str,
16
+ serialize_file_info,
17
+ slice_text_lines,
18
+ success,
19
+ )
20
+
21
+ MAX_MATCH_CONTENT = 200
22
+
23
+
24
+ def fs_read(daytona: Daytona, definition: dict, input_data: dict) -> dict:
25
+ sandbox = get_running_sandbox(daytona, input_data, definition)
26
+ path = require_path(input_data)
27
+ encoding = (optional_str(input_data, "encoding") or "utf-8").lower()
28
+ if encoding not in {"utf-8", "base64"}:
29
+ raise HandlerError("encoding must be utf-8 or base64")
30
+ max_bytes = effective_limit(definition, "max_file_bytes")
31
+ max_lines = effective_limit(definition, "max_file_lines")
32
+ start_line = optional_int(input_data, "start_line", min_value=1) or 1
33
+ line_limit = optional_int(input_data, "line_limit", min_value=1) or max_lines
34
+ line_limit = min(line_limit, max_lines)
35
+ timeout = request_timeout_for_sandbox(sandbox)
36
+ info = call_daytona(
37
+ lambda: sandbox.fs.get_file_info(path, request_timeout=timeout),
38
+ retry=True,
39
+ )
40
+ if getattr(info, "is_dir", False):
41
+ raise HandlerError("path is a directory")
42
+ size = getattr(info, "size", None)
43
+ if isinstance(size, int) and size > max_bytes:
44
+ raise HandlerError(
45
+ f"File is {size} bytes, which exceeds max_file_bytes={max_bytes}",
46
+ "limit_exceeded",
47
+ )
48
+ content = call_daytona(
49
+ lambda: sandbox.fs.download_file(path, timeout),
50
+ retry=True,
51
+ )
52
+ if not isinstance(content, (bytes, bytearray)):
53
+ raise HandlerError("Daytona did not return file bytes", "daytona_error")
54
+ payload = {
55
+ "path": path,
56
+ "encoding": encoding,
57
+ "size": size,
58
+ "name": getattr(info, "name", None),
59
+ }
60
+ if encoding == "base64":
61
+ encoded = bytes(content)
62
+ truncated_bytes = len(encoded) > max_bytes
63
+ if truncated_bytes:
64
+ encoded = encoded[:max_bytes]
65
+ payload.update(
66
+ {
67
+ "content": base64.b64encode(encoded).decode("ascii"),
68
+ "truncated": truncated_bytes,
69
+ "truncated_bytes": truncated_bytes,
70
+ "truncated_lines": False,
71
+ "byte_count": len(encoded),
72
+ }
73
+ )
74
+ return success(payload)
75
+ try:
76
+ text = bytes(content).decode("utf-8")
77
+ except UnicodeDecodeError as exc:
78
+ raise HandlerError(
79
+ "File is not valid UTF-8; retry with encoding=base64",
80
+ "validation_error",
81
+ ) from exc
82
+ sliced = slice_text_lines(
83
+ text,
84
+ start_line=start_line,
85
+ line_limit=line_limit,
86
+ max_bytes=max_bytes,
87
+ )
88
+ payload.update(
89
+ {
90
+ "content": sliced["text"],
91
+ "start_line": sliced["start_line"],
92
+ "returned_lines": sliced["returned_lines"],
93
+ "total_lines": sliced["total_lines"],
94
+ "truncated": sliced["truncated"],
95
+ "truncated_lines": sliced["truncated_lines"],
96
+ "truncated_bytes": sliced["truncated_bytes"],
97
+ "byte_count": sliced["byte_count"],
98
+ }
99
+ )
100
+ return success(payload)
101
+
102
+
103
+ def fs_write(daytona: Daytona, definition: dict, input_data: dict) -> dict:
104
+ sandbox = get_running_sandbox(daytona, input_data, definition)
105
+ path = require_path(input_data)
106
+ if "content" not in input_data or input_data["content"] is None:
107
+ raise HandlerError("content is required")
108
+ content = input_data["content"]
109
+ if not isinstance(content, str):
110
+ raise HandlerError("content must be a string")
111
+ encoding = (optional_str(input_data, "encoding") or "utf-8").lower()
112
+ if encoding not in {"utf-8", "base64"}:
113
+ raise HandlerError("encoding must be utf-8 or base64")
114
+ max_bytes = effective_limit(definition, "max_write_bytes")
115
+ if encoding == "utf-8":
116
+ payload = content.encode("utf-8")
117
+ else:
118
+ try:
119
+ payload = base64.b64decode(content, validate=True)
120
+ except Exception as exc:
121
+ raise HandlerError("content is not valid base64") from exc
122
+ if len(payload) > max_bytes:
123
+ raise HandlerError(
124
+ f"Write payload is {len(payload)} bytes, which exceeds max_write_bytes={max_bytes}",
125
+ "limit_exceeded",
126
+ )
127
+ timeout = request_timeout_for_sandbox(sandbox)
128
+ call_daytona(
129
+ lambda: sandbox.fs.upload_file(payload, path, timeout=timeout),
130
+ retry=False,
131
+ )
132
+ return success(
133
+ {
134
+ "path": path,
135
+ "encoding": encoding,
136
+ "bytes": len(payload),
137
+ }
138
+ )
139
+
140
+
141
+ def fs_list(daytona: Daytona, definition: dict, input_data: dict) -> dict:
142
+ sandbox = get_running_sandbox(daytona, input_data, definition)
143
+ path = require_path(input_data)
144
+ max_depth = effective_limit(definition, "max_list_depth")
145
+ max_entries = effective_limit(definition, "max_list_entries")
146
+ depth = optional_int(input_data, "depth", min_value=1) or 1
147
+ depth = min(depth, max_depth)
148
+ timeout = request_timeout_for_sandbox(sandbox)
149
+ entries = call_daytona(
150
+ lambda: sandbox.fs.list_files(path, depth=depth, request_timeout=timeout),
151
+ retry=True,
152
+ )
153
+ items = [serialize_file_info(entry) for entry in entries or []]
154
+ truncated = len(items) > max_entries
155
+ if truncated:
156
+ items = items[:max_entries]
157
+ return success(
158
+ {
159
+ "path": path,
160
+ "depth": depth,
161
+ "entries": items,
162
+ "count": len(items),
163
+ "truncated": truncated,
164
+ }
165
+ )
166
+
167
+
168
+ def fs_info(daytona: Daytona, definition: dict, input_data: dict) -> dict:
169
+ sandbox = get_running_sandbox(daytona, input_data, definition)
170
+ path = require_path(input_data)
171
+ timeout = request_timeout_for_sandbox(sandbox)
172
+ info = call_daytona(
173
+ lambda: sandbox.fs.get_file_info(path, request_timeout=timeout),
174
+ retry=True,
175
+ )
176
+ return success({"path": path, **serialize_file_info(info)})
177
+
178
+
179
+ def fs_mkdir(daytona: Daytona, definition: dict, input_data: dict) -> dict:
180
+ sandbox = get_running_sandbox(daytona, input_data, definition)
181
+ path = require_path(input_data)
182
+ mode = optional_str(input_data, "mode") or "755"
183
+ _validate_mode(mode)
184
+ timeout = request_timeout_for_sandbox(sandbox)
185
+ call_daytona(
186
+ lambda: sandbox.fs.create_folder(path, mode, request_timeout=timeout),
187
+ retry=False,
188
+ )
189
+ return success({"path": path, "mode": mode})
190
+
191
+
192
+ def fs_delete(daytona: Daytona, definition: dict, input_data: dict) -> dict:
193
+ sandbox = get_running_sandbox(daytona, input_data, definition)
194
+ path = require_path(input_data)
195
+ recursive = optional_bool(input_data, "recursive") or False
196
+ timeout = request_timeout_for_sandbox(sandbox)
197
+ call_daytona(
198
+ lambda: sandbox.fs.delete_file(path, recursive=recursive, request_timeout=timeout),
199
+ retry=False,
200
+ )
201
+ return success({"path": path, "deleted": True, "recursive": recursive})
202
+
203
+
204
+ def fs_move(daytona: Daytona, definition: dict, input_data: dict) -> dict:
205
+ sandbox = get_running_sandbox(daytona, input_data, definition)
206
+ source = require_path(input_data, "source")
207
+ destination = require_path(input_data, "destination")
208
+ timeout = request_timeout_for_sandbox(sandbox)
209
+ call_daytona(
210
+ lambda: sandbox.fs.move_files(source, destination, request_timeout=timeout),
211
+ retry=False,
212
+ )
213
+ return success({"source": source, "destination": destination})
214
+
215
+
216
+ def fs_search(daytona: Daytona, definition: dict, input_data: dict) -> dict:
217
+ sandbox = get_running_sandbox(daytona, input_data, definition)
218
+ path = require_path(input_data)
219
+ pattern = input_data.get("pattern")
220
+ if not isinstance(pattern, str) or not pattern:
221
+ raise HandlerError("pattern must be a non-empty string")
222
+ search_type = (optional_str(input_data, "search_type") or "content").lower()
223
+ if search_type not in {"content", "name"}:
224
+ raise HandlerError("search_type must be content or name")
225
+ max_results = min(optional_int(input_data, "limit", min_value=1) or effective_limit(definition, "max_search_results"), effective_limit(definition, "max_search_results"))
226
+ timeout = request_timeout_for_sandbox(sandbox)
227
+ if search_type == "name":
228
+ result = call_daytona(
229
+ lambda: sandbox.fs.search_files(path, pattern, request_timeout=timeout),
230
+ retry=True,
231
+ )
232
+ files = list(getattr(result, "files", None) or [])
233
+ truncated = len(files) > max_results
234
+ if truncated:
235
+ files = files[:max_results]
236
+ return success(
237
+ {
238
+ "path": path,
239
+ "pattern": pattern,
240
+ "search_type": search_type,
241
+ "files": files,
242
+ "count": len(files),
243
+ "truncated": truncated,
244
+ }
245
+ )
246
+ matches = call_daytona(
247
+ lambda: sandbox.fs.find_files(path, pattern, request_timeout=timeout),
248
+ retry=True,
249
+ )
250
+ matches = matches or []
251
+ truncated = len(matches) > max_results
252
+ items = [_serialize_match(match) for match in matches[:max_results]]
253
+ return success(
254
+ {
255
+ "path": path,
256
+ "pattern": pattern,
257
+ "search_type": search_type,
258
+ "matches": items,
259
+ "count": len(items),
260
+ "truncated": truncated,
261
+ }
262
+ )
263
+
264
+
265
+
266
+ def fs_replace(daytona: Daytona, definition: dict, input_data: dict) -> dict:
267
+ sandbox = get_running_sandbox(daytona, input_data, definition)
268
+ files = input_data.get("files")
269
+ if (not isinstance(files, list) or not files or
270
+ len(files) > effective_limit(definition, "max_list_entries") or
271
+ any(not isinstance(path, str) or not path.strip() for path in files)):
272
+ raise HandlerError("files must be a non-empty list of paths within max_list_entries")
273
+ pattern = input_data.get("pattern")
274
+ new_value = input_data.get("new_value")
275
+ if not isinstance(pattern, str) or not pattern:
276
+ raise HandlerError("pattern must be a non-empty string")
277
+ if not isinstance(new_value, str):
278
+ raise HandlerError("new_value must be a string")
279
+ timeout = request_timeout_for_sandbox(sandbox)
280
+ results = call_daytona(
281
+ lambda: sandbox.fs.replace_in_files(files, pattern, new_value, request_timeout=timeout),
282
+ retry=False,
283
+ )
284
+ return success({"results": [
285
+ {"file": getattr(item, "file", None), "success": getattr(item, "success", None),
286
+ "error": getattr(item, "error", None)} for item in results or []
287
+ ]})
288
+
289
+ def fs_chmod(daytona: Daytona, definition: dict, input_data: dict) -> dict:
290
+ sandbox = get_running_sandbox(daytona, input_data, definition)
291
+ path = require_path(input_data)
292
+ mode = optional_str(input_data, "mode")
293
+ owner = optional_str(input_data, "owner")
294
+ group = optional_str(input_data, "group")
295
+ if mode is None and owner is None and group is None:
296
+ raise HandlerError("mode, owner, or group is required")
297
+ if mode is not None:
298
+ _validate_mode(mode)
299
+ timeout = request_timeout_for_sandbox(sandbox)
300
+ call_daytona(
301
+ lambda: sandbox.fs.set_file_permissions(
302
+ path,
303
+ mode=mode,
304
+ owner=owner,
305
+ group=group,
306
+ request_timeout=timeout,
307
+ ),
308
+ retry=False,
309
+ )
310
+ return success({"path": path, "mode": mode, "owner": owner, "group": group})
311
+
312
+
313
+ def _validate_mode(mode: str) -> None:
314
+ if not mode.isdigit() or len(mode) < 3 or len(mode) > 4:
315
+ raise HandlerError("mode must be an octal permission string such as 644 or 0755")
316
+ if any(ch not in "01234567" for ch in mode):
317
+ raise HandlerError("mode must be an octal permission string such as 644 or 0755")
318
+
319
+
320
+ def _serialize_match(match) -> dict:
321
+ content = getattr(match, "content", "") or ""
322
+ truncated = len(content) > MAX_MATCH_CONTENT
323
+ if truncated:
324
+ content = content[:MAX_MATCH_CONTENT]
325
+ return {
326
+ "file": getattr(match, "file", None),
327
+ "line": getattr(match, "line", None),
328
+ "content": content,
329
+ "truncated": truncated,
330
+ }