@openai/agents-extensions 0.8.4 → 0.9.0

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 (218) hide show
  1. package/dist/ai-sdk-ui/uiMessageStream.d.ts +5 -0
  2. package/dist/ai-sdk-ui/uiMessageStream.js +10 -3
  3. package/dist/ai-sdk-ui/uiMessageStream.js.map +1 -1
  4. package/dist/ai-sdk-ui/uiMessageStream.mjs +9 -3
  5. package/dist/ai-sdk-ui/uiMessageStream.mjs.map +1 -1
  6. package/dist/metadata.js +2 -2
  7. package/dist/metadata.mjs +2 -2
  8. package/dist/sandbox/blaxel/index.d.ts +2 -0
  9. package/dist/sandbox/blaxel/index.js +19 -0
  10. package/dist/sandbox/blaxel/index.js.map +1 -0
  11. package/dist/sandbox/blaxel/index.mjs +3 -0
  12. package/dist/sandbox/blaxel/index.mjs.map +1 -0
  13. package/dist/sandbox/blaxel/mounts.d.ts +101 -0
  14. package/dist/sandbox/blaxel/mounts.js +542 -0
  15. package/dist/sandbox/blaxel/mounts.js.map +1 -0
  16. package/dist/sandbox/blaxel/mounts.mjs +529 -0
  17. package/dist/sandbox/blaxel/mounts.mjs.map +1 -0
  18. package/dist/sandbox/blaxel/sandbox.d.ts +155 -0
  19. package/dist/sandbox/blaxel/sandbox.js +1110 -0
  20. package/dist/sandbox/blaxel/sandbox.js.map +1 -0
  21. package/dist/sandbox/blaxel/sandbox.mjs +1072 -0
  22. package/dist/sandbox/blaxel/sandbox.mjs.map +1 -0
  23. package/dist/sandbox/cloudflare/index.d.ts +2 -0
  24. package/dist/sandbox/cloudflare/index.js +19 -0
  25. package/dist/sandbox/cloudflare/index.js.map +1 -0
  26. package/dist/sandbox/cloudflare/index.mjs +3 -0
  27. package/dist/sandbox/cloudflare/index.mjs.map +1 -0
  28. package/dist/sandbox/cloudflare/mounts.d.ts +28 -0
  29. package/dist/sandbox/cloudflare/mounts.js +150 -0
  30. package/dist/sandbox/cloudflare/mounts.js.map +1 -0
  31. package/dist/sandbox/cloudflare/mounts.mjs +143 -0
  32. package/dist/sandbox/cloudflare/mounts.mjs.map +1 -0
  33. package/dist/sandbox/cloudflare/sandbox.d.ts +91 -0
  34. package/dist/sandbox/cloudflare/sandbox.js +1163 -0
  35. package/dist/sandbox/cloudflare/sandbox.js.map +1 -0
  36. package/dist/sandbox/cloudflare/sandbox.mjs +1125 -0
  37. package/dist/sandbox/cloudflare/sandbox.mjs.map +1 -0
  38. package/dist/sandbox/daytona/index.d.ts +2 -0
  39. package/dist/sandbox/daytona/index.js +19 -0
  40. package/dist/sandbox/daytona/index.js.map +1 -0
  41. package/dist/sandbox/daytona/index.mjs +3 -0
  42. package/dist/sandbox/daytona/index.mjs.map +1 -0
  43. package/dist/sandbox/daytona/mounts.d.ts +11 -0
  44. package/dist/sandbox/daytona/mounts.js +17 -0
  45. package/dist/sandbox/daytona/mounts.js.map +1 -0
  46. package/dist/sandbox/daytona/mounts.mjs +12 -0
  47. package/dist/sandbox/daytona/mounts.mjs.map +1 -0
  48. package/dist/sandbox/daytona/sandbox.d.ts +160 -0
  49. package/dist/sandbox/daytona/sandbox.js +938 -0
  50. package/dist/sandbox/daytona/sandbox.js.map +1 -0
  51. package/dist/sandbox/daytona/sandbox.mjs +900 -0
  52. package/dist/sandbox/daytona/sandbox.mjs.map +1 -0
  53. package/dist/sandbox/e2b/index.d.ts +2 -0
  54. package/dist/sandbox/e2b/index.js +19 -0
  55. package/dist/sandbox/e2b/index.js.map +1 -0
  56. package/dist/sandbox/e2b/index.mjs +3 -0
  57. package/dist/sandbox/e2b/index.mjs.map +1 -0
  58. package/dist/sandbox/e2b/mounts.d.ts +11 -0
  59. package/dist/sandbox/e2b/mounts.js +17 -0
  60. package/dist/sandbox/e2b/mounts.js.map +1 -0
  61. package/dist/sandbox/e2b/mounts.mjs +12 -0
  62. package/dist/sandbox/e2b/mounts.mjs.map +1 -0
  63. package/dist/sandbox/e2b/sandbox.d.ts +169 -0
  64. package/dist/sandbox/e2b/sandbox.js +829 -0
  65. package/dist/sandbox/e2b/sandbox.js.map +1 -0
  66. package/dist/sandbox/e2b/sandbox.mjs +791 -0
  67. package/dist/sandbox/e2b/sandbox.mjs.map +1 -0
  68. package/dist/sandbox/modal/index.d.ts +2 -0
  69. package/dist/sandbox/modal/index.js +19 -0
  70. package/dist/sandbox/modal/index.js.map +1 -0
  71. package/dist/sandbox/modal/index.mjs +3 -0
  72. package/dist/sandbox/modal/index.mjs.map +1 -0
  73. package/dist/sandbox/modal/mounts.d.ts +24 -0
  74. package/dist/sandbox/modal/mounts.js +194 -0
  75. package/dist/sandbox/modal/mounts.js.map +1 -0
  76. package/dist/sandbox/modal/mounts.mjs +188 -0
  77. package/dist/sandbox/modal/mounts.mjs.map +1 -0
  78. package/dist/sandbox/modal/sandbox.d.ts +220 -0
  79. package/dist/sandbox/modal/sandbox.js +1621 -0
  80. package/dist/sandbox/modal/sandbox.js.map +1 -0
  81. package/dist/sandbox/modal/sandbox.mjs +1581 -0
  82. package/dist/sandbox/modal/sandbox.mjs.map +1 -0
  83. package/dist/sandbox/runloop/index.d.ts +2 -0
  84. package/dist/sandbox/runloop/index.js +19 -0
  85. package/dist/sandbox/runloop/index.js.map +1 -0
  86. package/dist/sandbox/runloop/index.mjs +3 -0
  87. package/dist/sandbox/runloop/index.mjs.map +1 -0
  88. package/dist/sandbox/runloop/mounts.d.ts +11 -0
  89. package/dist/sandbox/runloop/mounts.js +17 -0
  90. package/dist/sandbox/runloop/mounts.js.map +1 -0
  91. package/dist/sandbox/runloop/mounts.mjs +12 -0
  92. package/dist/sandbox/runloop/mounts.mjs.map +1 -0
  93. package/dist/sandbox/runloop/sandbox.d.ts +266 -0
  94. package/dist/sandbox/runloop/sandbox.js +1390 -0
  95. package/dist/sandbox/runloop/sandbox.js.map +1 -0
  96. package/dist/sandbox/runloop/sandbox.mjs +1346 -0
  97. package/dist/sandbox/runloop/sandbox.mjs.map +1 -0
  98. package/dist/sandbox/shared/archive.d.ts +34 -0
  99. package/dist/sandbox/shared/archive.js +397 -0
  100. package/dist/sandbox/shared/archive.js.map +1 -0
  101. package/dist/sandbox/shared/archive.mjs +389 -0
  102. package/dist/sandbox/shared/archive.mjs.map +1 -0
  103. package/dist/sandbox/shared/editor.d.ts +17 -0
  104. package/dist/sandbox/shared/editor.js +65 -0
  105. package/dist/sandbox/shared/editor.js.map +1 -0
  106. package/dist/sandbox/shared/editor.mjs +61 -0
  107. package/dist/sandbox/shared/editor.mjs.map +1 -0
  108. package/dist/sandbox/shared/environment.d.ts +1 -0
  109. package/dist/sandbox/shared/environment.js +13 -0
  110. package/dist/sandbox/shared/environment.js.map +1 -0
  111. package/dist/sandbox/shared/environment.mjs +2 -0
  112. package/dist/sandbox/shared/environment.mjs.map +1 -0
  113. package/dist/sandbox/shared/git.d.ts +4 -0
  114. package/dist/sandbox/shared/git.js +20 -0
  115. package/dist/sandbox/shared/git.js.map +1 -0
  116. package/dist/sandbox/shared/git.mjs +17 -0
  117. package/dist/sandbox/shared/git.mjs.map +1 -0
  118. package/dist/sandbox/shared/inContainerMounts.d.ts +40 -0
  119. package/dist/sandbox/shared/inContainerMounts.js +670 -0
  120. package/dist/sandbox/shared/inContainerMounts.js.map +1 -0
  121. package/dist/sandbox/shared/inContainerMounts.mjs +662 -0
  122. package/dist/sandbox/shared/inContainerMounts.mjs.map +1 -0
  123. package/dist/sandbox/shared/index.d.ts +23 -0
  124. package/dist/sandbox/shared/index.js +112 -0
  125. package/dist/sandbox/shared/index.js.map +1 -0
  126. package/dist/sandbox/shared/index.mjs +18 -0
  127. package/dist/sandbox/shared/index.mjs.map +1 -0
  128. package/dist/sandbox/shared/localSources.d.ts +8 -0
  129. package/dist/sandbox/shared/localSources.js +378 -0
  130. package/dist/sandbox/shared/localSources.js.map +1 -0
  131. package/dist/sandbox/shared/localSources.mjs +372 -0
  132. package/dist/sandbox/shared/localSources.mjs.map +1 -0
  133. package/dist/sandbox/shared/manifest.d.ts +38 -0
  134. package/dist/sandbox/shared/manifest.js +254 -0
  135. package/dist/sandbox/shared/manifest.js.map +1 -0
  136. package/dist/sandbox/shared/manifest.mjs +233 -0
  137. package/dist/sandbox/shared/manifest.mjs.map +1 -0
  138. package/dist/sandbox/shared/media.d.ts +1 -0
  139. package/dist/sandbox/shared/media.js +10 -0
  140. package/dist/sandbox/shared/media.js.map +1 -0
  141. package/dist/sandbox/shared/media.mjs +2 -0
  142. package/dist/sandbox/shared/media.mjs.map +1 -0
  143. package/dist/sandbox/shared/metadata.d.ts +7 -0
  144. package/dist/sandbox/shared/metadata.js +87 -0
  145. package/dist/sandbox/shared/metadata.js.map +1 -0
  146. package/dist/sandbox/shared/metadata.mjs +81 -0
  147. package/dist/sandbox/shared/metadata.mjs.map +1 -0
  148. package/dist/sandbox/shared/output.d.ts +1 -0
  149. package/dist/sandbox/shared/output.js +8 -0
  150. package/dist/sandbox/shared/output.js.map +1 -0
  151. package/dist/sandbox/shared/output.mjs +2 -0
  152. package/dist/sandbox/shared/output.mjs.map +1 -0
  153. package/dist/sandbox/shared/paths.d.ts +27 -0
  154. package/dist/sandbox/shared/paths.js +245 -0
  155. package/dist/sandbox/shared/paths.js.map +1 -0
  156. package/dist/sandbox/shared/paths.mjs +236 -0
  157. package/dist/sandbox/shared/paths.mjs.map +1 -0
  158. package/dist/sandbox/shared/ports.d.ts +14 -0
  159. package/dist/sandbox/shared/ports.js +75 -0
  160. package/dist/sandbox/shared/ports.js.map +1 -0
  161. package/dist/sandbox/shared/ports.mjs +69 -0
  162. package/dist/sandbox/shared/ports.mjs.map +1 -0
  163. package/dist/sandbox/shared/process.d.ts +16 -0
  164. package/dist/sandbox/shared/process.js +90 -0
  165. package/dist/sandbox/shared/process.js.map +1 -0
  166. package/dist/sandbox/shared/process.mjs +86 -0
  167. package/dist/sandbox/shared/process.mjs.map +1 -0
  168. package/dist/sandbox/shared/pty.d.ts +81 -0
  169. package/dist/sandbox/shared/pty.js +382 -0
  170. package/dist/sandbox/shared/pty.js.map +1 -0
  171. package/dist/sandbox/shared/pty.mjs +335 -0
  172. package/dist/sandbox/shared/pty.mjs.map +1 -0
  173. package/dist/sandbox/shared/runAs.d.ts +46 -0
  174. package/dist/sandbox/shared/runAs.js +149 -0
  175. package/dist/sandbox/shared/runAs.js.map +1 -0
  176. package/dist/sandbox/shared/runAs.mjs +141 -0
  177. package/dist/sandbox/shared/runAs.mjs.map +1 -0
  178. package/dist/sandbox/shared/session.d.ts +16 -0
  179. package/dist/sandbox/shared/session.js +138 -0
  180. package/dist/sandbox/shared/session.js.map +1 -0
  181. package/dist/sandbox/shared/session.mjs +128 -0
  182. package/dist/sandbox/shared/session.mjs.map +1 -0
  183. package/dist/sandbox/shared/sessionBase.d.ts +90 -0
  184. package/dist/sandbox/shared/sessionBase.js +446 -0
  185. package/dist/sandbox/shared/sessionBase.js.map +1 -0
  186. package/dist/sandbox/shared/sessionBase.mjs +442 -0
  187. package/dist/sandbox/shared/sessionBase.mjs.map +1 -0
  188. package/dist/sandbox/shared/sessionState.d.ts +7 -0
  189. package/dist/sandbox/shared/sessionState.js +35 -0
  190. package/dist/sandbox/shared/sessionState.js.map +1 -0
  191. package/dist/sandbox/shared/sessionState.mjs +31 -0
  192. package/dist/sandbox/shared/sessionState.mjs.map +1 -0
  193. package/dist/sandbox/shared/snapshots.d.ts +9 -0
  194. package/dist/sandbox/shared/snapshots.js +62 -0
  195. package/dist/sandbox/shared/snapshots.js.map +1 -0
  196. package/dist/sandbox/shared/snapshots.mjs +57 -0
  197. package/dist/sandbox/shared/snapshots.mjs.map +1 -0
  198. package/dist/sandbox/shared/typeGuards.d.ts +10 -0
  199. package/dist/sandbox/shared/typeGuards.js +50 -0
  200. package/dist/sandbox/shared/typeGuards.js.map +1 -0
  201. package/dist/sandbox/shared/typeGuards.mjs +38 -0
  202. package/dist/sandbox/shared/typeGuards.mjs.map +1 -0
  203. package/dist/sandbox/shared/types.d.ts +24 -0
  204. package/dist/sandbox/shared/types.js +3 -0
  205. package/dist/sandbox/shared/types.js.map +1 -0
  206. package/dist/sandbox/shared/types.mjs +2 -0
  207. package/dist/sandbox/shared/types.mjs.map +1 -0
  208. package/dist/sandbox/vercel/index.d.ts +1 -0
  209. package/dist/sandbox/vercel/index.js +18 -0
  210. package/dist/sandbox/vercel/index.js.map +1 -0
  211. package/dist/sandbox/vercel/index.mjs +2 -0
  212. package/dist/sandbox/vercel/index.mjs.map +1 -0
  213. package/dist/sandbox/vercel/sandbox.d.ts +135 -0
  214. package/dist/sandbox/vercel/sandbox.js +1001 -0
  215. package/dist/sandbox/vercel/sandbox.js.map +1 -0
  216. package/dist/sandbox/vercel/sandbox.mjs +963 -0
  217. package/dist/sandbox/vercel/sandbox.mjs.map +1 -0
  218. package/package.json +108 -4
@@ -0,0 +1,938 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
35
+ Object.defineProperty(exports, "__esModule", { value: true });
36
+ exports.DaytonaSandboxClient = exports.DaytonaSandboxSession = void 0;
37
+ const agents_core_1 = require("@openai/agents-core");
38
+ const sandbox_1 = require("@openai/agents-core/sandbox");
39
+ const shared_1 = require("../shared/index.js");
40
+ const inContainerMounts_1 = require("../shared/inContainerMounts.js");
41
+ const mounts_1 = require("./mounts.js");
42
+ const localSources_1 = require("../shared/localSources.js");
43
+ const DEFAULT_WORKSPACE_ROOT = '/home/daytona/workspace';
44
+ const DEFAULT_EXPOSED_PORT_URL_TTL_S = 60;
45
+ const DAYTONA_DELETE_RETRY_DELAYS_MS = [1_000, 2_000, 4_000];
46
+ class DaytonaSandboxSession {
47
+ state;
48
+ sandbox;
49
+ ptyProcesses = new shared_1.PtyProcessRegistry();
50
+ remotePathResolver = async (path, options) => await this.resolveRemotePath(path, options);
51
+ activeMountPaths = new Set();
52
+ concurrencyLimits;
53
+ stopPromise;
54
+ deletePromise;
55
+ constructor(args) {
56
+ this.state = args.state;
57
+ this.sandbox = args.sandbox;
58
+ this.concurrencyLimits = args.concurrencyLimits;
59
+ }
60
+ createEditor(runAs) {
61
+ if (runAs) {
62
+ return (0, shared_1.createRunAsRemoteEditor)({
63
+ providerName: 'DaytonaSandboxClient',
64
+ providerId: 'daytona',
65
+ runAs,
66
+ resolvePath: this.remotePathResolver,
67
+ runCommand: this.runAsCommandRunner.bind(this),
68
+ writer: this.writer(),
69
+ });
70
+ }
71
+ return new shared_1.RemoteSandboxEditor({
72
+ resolvePath: this.remotePathResolver,
73
+ pathExists: async (path) => await this.pathExists(path),
74
+ mkdir: async (path) => {
75
+ await this.mkdirEditorPath(path);
76
+ },
77
+ readText: async (path) => {
78
+ return await this.readEditorText(path);
79
+ },
80
+ writeText: async (path, content) => {
81
+ await this.writeEditorText(path, content);
82
+ },
83
+ deletePath: async (path) => {
84
+ await this.deleteEditorPath(path);
85
+ },
86
+ });
87
+ }
88
+ supportsPty() {
89
+ return typeof this.sandbox.process.createPty === 'function';
90
+ }
91
+ async execCommand(args) {
92
+ if (args.tty) {
93
+ return await this.execPtyCommand(args);
94
+ }
95
+ const start = Date.now();
96
+ const result = await this.sandbox.process.executeCommand(commandForDaytonaUser(args.cmd, args.runAs), (0, shared_1.resolveSandboxWorkdir)(this.state.manifest.root, args.workdir), this.state.environment);
97
+ const combinedOutput = result.artifacts?.stdout ?? result.result ?? '';
98
+ const output = (0, shared_1.truncateOutput)(combinedOutput, args.maxOutputTokens);
99
+ return (0, shared_1.formatExecResponse)({
100
+ output: output.text,
101
+ wallTimeSeconds: (0, shared_1.elapsedSeconds)(start),
102
+ exitCode: result.exitCode,
103
+ originalTokenCount: output.originalTokenCount,
104
+ });
105
+ }
106
+ async writeStdin(args) {
107
+ return await (0, shared_1.writePtyStdin)({
108
+ providerName: 'DaytonaSandboxClient',
109
+ registry: this.ptyProcesses,
110
+ sessionId: args.sessionId,
111
+ chars: args.chars,
112
+ yieldTimeMs: args.yieldTimeMs,
113
+ maxOutputTokens: args.maxOutputTokens,
114
+ });
115
+ }
116
+ async execPtyCommand(args) {
117
+ if (!this.sandbox.process.createPty) {
118
+ throw new sandbox_1.SandboxUnsupportedFeatureError('DaytonaSandboxClient tty=true requires Daytona SDK PTY support.', {
119
+ provider: 'daytona',
120
+ feature: 'tty',
121
+ });
122
+ }
123
+ const start = Date.now();
124
+ const command = (0, shared_1.shellCommandForPty)(args);
125
+ const entry = (0, shared_1.createPtyProcessEntry)({ tty: true });
126
+ const providerSessionId = `sandbox-${Math.random().toString(16).slice(2, 14)}`;
127
+ const handle = await this.sandbox.process.createPty({
128
+ id: providerSessionId,
129
+ cwd: (0, shared_1.resolveSandboxWorkdir)(this.state.manifest.root, args.workdir),
130
+ envs: this.state.environment,
131
+ cols: 80,
132
+ rows: 24,
133
+ onData: (data) => (0, shared_1.appendPtyOutput)(entry, data),
134
+ });
135
+ if (handle.waitForConnection) {
136
+ await handle.waitForConnection();
137
+ }
138
+ if (!handle.sendInput) {
139
+ await this.terminatePtyHandle(handle, providerSessionId);
140
+ throw new sandbox_1.SandboxUnsupportedFeatureError('DaytonaSandboxClient tty=true requires Daytona SDK PTY stdin support.', {
141
+ provider: 'daytona',
142
+ feature: 'tty.stdin',
143
+ });
144
+ }
145
+ if (!handle.wait) {
146
+ await this.terminatePtyHandle(handle, providerSessionId);
147
+ throw new sandbox_1.SandboxUnsupportedFeatureError('DaytonaSandboxClient tty=true requires Daytona SDK PTY wait support.', {
148
+ provider: 'daytona',
149
+ feature: 'tty.wait',
150
+ });
151
+ }
152
+ const waitForExit = handle.wait.bind(handle);
153
+ entry.sendInput = async (chars) => {
154
+ await handle.sendInput(chars);
155
+ };
156
+ entry.terminate = async () => {
157
+ if (handle.kill) {
158
+ await handle.kill();
159
+ }
160
+ else {
161
+ await this.sandbox.process.killPtySession?.(handle.sessionId ?? providerSessionId);
162
+ }
163
+ await handle.disconnect?.();
164
+ };
165
+ const { sessionId, pruned } = this.ptyProcesses.register(entry);
166
+ if (pruned) {
167
+ await pruned.terminate?.().catch(() => { });
168
+ }
169
+ (0, shared_1.watchPtyProcess)(entry, async () => await waitForExit(), (result, error) => exitCodeFromDaytonaResult(result) ??
170
+ exitCodeFromDaytonaResult(error) ??
171
+ exitCodeFromDaytonaResult(handle));
172
+ await entry.sendInput(`${commandForDaytonaUser(command, args.runAs)}\n`);
173
+ return await (0, shared_1.formatPtyExecUpdate)({
174
+ registry: this.ptyProcesses,
175
+ sessionId,
176
+ entry,
177
+ startTime: start,
178
+ yieldTimeMs: args.yieldTimeMs,
179
+ maxOutputTokens: args.maxOutputTokens,
180
+ });
181
+ }
182
+ async terminatePtyHandle(handle, providerSessionId) {
183
+ if (handle.kill) {
184
+ try {
185
+ await handle.kill();
186
+ }
187
+ catch {
188
+ // Ignore best-effort PTY cleanup failures.
189
+ }
190
+ }
191
+ else {
192
+ try {
193
+ await this.sandbox.process.killPtySession?.(handle.sessionId ?? providerSessionId);
194
+ }
195
+ catch {
196
+ // Ignore best-effort PTY cleanup failures.
197
+ }
198
+ }
199
+ try {
200
+ await handle.disconnect?.();
201
+ }
202
+ catch {
203
+ // Ignore best-effort PTY cleanup failures.
204
+ }
205
+ }
206
+ async viewImage(args) {
207
+ const bytes = args.runAs
208
+ ? await (0, shared_1.readRunAsRemoteFile)({
209
+ providerName: 'DaytonaSandboxClient',
210
+ providerId: 'daytona',
211
+ path: await this.resolveRemotePath(args.path),
212
+ runAs: args.runAs,
213
+ runCommand: this.runAsCommandRunner.bind(this),
214
+ })
215
+ : await this.readFileBytes(args.path);
216
+ return (0, shared_1.imageOutputFromBytes)(args.path, bytes);
217
+ }
218
+ async readFile(args) {
219
+ const bytes = args.runAs
220
+ ? await (0, shared_1.readRunAsRemoteFile)({
221
+ providerName: 'DaytonaSandboxClient',
222
+ providerId: 'daytona',
223
+ path: await this.resolveRemotePath(args.path),
224
+ runAs: args.runAs,
225
+ runCommand: this.runAsCommandRunner.bind(this),
226
+ })
227
+ : await this.readFileBytes(args.path);
228
+ if (typeof args.maxBytes === 'number' && bytes.byteLength > args.maxBytes) {
229
+ return bytes.subarray(0, args.maxBytes);
230
+ }
231
+ return bytes;
232
+ }
233
+ async pathExists(path, runAs) {
234
+ const absolutePath = await this.resolveRemotePath(path);
235
+ if (!runAs) {
236
+ const result = await this.sandbox.process.executeCommand(`test -e ${(0, shared_1.shellQuote)(absolutePath)}`, this.state.manifest.root, this.state.environment, 5);
237
+ return result.exitCode === 0;
238
+ }
239
+ return await (0, shared_1.runAsRemotePathExists)(absolutePath, runAs, this.runAsCommandRunner.bind(this));
240
+ }
241
+ async running() {
242
+ try {
243
+ const result = await this.sandbox.process.executeCommand('true', this.state.manifest.root, this.state.environment, 5);
244
+ return result.exitCode === 0;
245
+ }
246
+ catch {
247
+ return false;
248
+ }
249
+ }
250
+ async resolveExposedPort(port) {
251
+ const requestedPort = (0, shared_1.assertConfiguredExposedPort)({
252
+ providerName: 'DaytonaSandboxClient',
253
+ port,
254
+ configuredPorts: this.state.configuredExposedPorts,
255
+ });
256
+ const cached = getUsableDaytonaCachedExposedPortEndpoint(this.state, requestedPort);
257
+ if (cached) {
258
+ return cached;
259
+ }
260
+ const preview = await this.createSignedPreviewUrl(requestedPort);
261
+ const url = typeof preview === 'string' ? preview : preview.url;
262
+ if (typeof url !== 'string') {
263
+ throw new sandbox_1.SandboxProviderError('DaytonaSandboxClient exposed port preview did not include a URL.', {
264
+ provider: 'daytona',
265
+ port: requestedPort,
266
+ });
267
+ }
268
+ return (0, shared_1.recordResolvedExposedPortEndpoint)(this.state, requestedPort, withDaytonaPreviewExpiration((0, shared_1.parseExposedPortEndpoint)(url, {
269
+ providerName: 'DaytonaSandboxClient',
270
+ source: 'preview URL',
271
+ }), this.state.exposedPortUrlTtlS));
272
+ }
273
+ async materializeEntry(args) {
274
+ (0, shared_1.assertSandboxEntryMetadataSupported)('DaytonaSandboxClient', args.path, args.entry, shared_1.MOUNT_MANIFEST_METADATA_SUPPORT);
275
+ await (0, localSources_1.applyLocalSourceManifestEntryToState)(this.state, args.path, args.entry, 'daytona', this.writer(), this.remotePathResolver, this.manifestMaterializationOptions(args.runAs));
276
+ }
277
+ async applyManifest(manifest, runAs) {
278
+ const resolvedManifest = resolveManifestRoot(manifest);
279
+ if (resolvedManifest.root !== this.state.manifest.root) {
280
+ throw new agents_core_1.UserError('DaytonaSandboxClient cannot apply a manifest with a different root than the active session. Create or resume a session with the desired root instead.');
281
+ }
282
+ (0, shared_1.assertSandboxManifestMetadataSupported)('DaytonaSandboxClient', resolvedManifest, shared_1.MOUNT_MANIFEST_METADATA_SUPPORT);
283
+ await (0, localSources_1.applyLocalSourceManifestToState)(this.state, resolvedManifest, 'daytona', this.writer(), this.remotePathResolver, this.manifestMaterializationOptions(runAs));
284
+ }
285
+ async materializeInitialManifest(manifest) {
286
+ await (0, localSources_1.materializeLocalSourceManifest)(this.writer(), manifest, 'daytona', this.remotePathResolver, this.manifestMaterializationOptions());
287
+ }
288
+ async rematerializeMountEntries() {
289
+ const targets = [];
290
+ for (const target of this.state.manifest.mountTargetsForMaterialization()) {
291
+ targets.push({
292
+ ...target,
293
+ resolvedMountPath: await this.resolveMountEntryPath(target.mountPath, target.entry),
294
+ });
295
+ }
296
+ for (const { resolvedMountPath } of [...targets].reverse()) {
297
+ await this.unmountMountPath(resolvedMountPath);
298
+ }
299
+ this.activeMountPaths.clear();
300
+ for (const { mountPath, entry } of targets) {
301
+ await this.materializeMountEntry(mountPath, entry);
302
+ }
303
+ }
304
+ async prepareWorkspaceRoot() {
305
+ const root = this.state.manifest.root;
306
+ const result = await this.sandbox.process.executeCommand(`mkdir -p -- ${(0, shared_1.shellQuote)(root)}`, '/', this.state.environment);
307
+ if (result.exitCode !== 0) {
308
+ throw new sandbox_1.SandboxProviderError('DaytonaSandboxClient failed to prepare the workspace root.', {
309
+ provider: 'daytona',
310
+ operation: 'prepare workspace root',
311
+ sandboxId: this.state.sandboxId,
312
+ root,
313
+ stdout: result.artifacts?.stdout ?? result.result ?? '',
314
+ });
315
+ }
316
+ }
317
+ async persistWorkspace() {
318
+ (0, shared_1.assertTarWorkspacePersistence)('DaytonaSandboxClient', 'tar');
319
+ return await (0, shared_1.persistRemoteWorkspaceTar)({
320
+ providerName: 'DaytonaSandboxClient',
321
+ manifest: this.state.manifest,
322
+ io: this.archiveIo(),
323
+ });
324
+ }
325
+ async hydrateWorkspace(data) {
326
+ (0, shared_1.assertTarWorkspacePersistence)('DaytonaSandboxClient', 'tar');
327
+ await (0, shared_1.hydrateRemoteWorkspaceTar)({
328
+ providerName: 'DaytonaSandboxClient',
329
+ manifest: this.state.manifest,
330
+ io: this.archiveIo(),
331
+ data,
332
+ });
333
+ }
334
+ async close() {
335
+ if (this.state.pauseOnExit) {
336
+ await this.stopOnce();
337
+ return;
338
+ }
339
+ await this.deleteOnce();
340
+ }
341
+ async shutdown() {
342
+ await this.close();
343
+ }
344
+ async delete() {
345
+ await this.deleteOnce();
346
+ }
347
+ async stopOnce() {
348
+ this.stopPromise ??= (async () => {
349
+ await this.ptyProcesses.terminateAll();
350
+ await (0, shared_1.withSandboxSpan)('sandbox.stop', {
351
+ backend_id: 'daytona',
352
+ sandbox_id: this.state.sandboxId,
353
+ }, async () => {
354
+ await this.sandbox.stop();
355
+ });
356
+ })();
357
+ await this.stopPromise;
358
+ }
359
+ async deleteOnce() {
360
+ this.deletePromise ??= (async () => {
361
+ await this.ptyProcesses.terminateAll();
362
+ await (0, shared_1.withSandboxSpan)('sandbox.shutdown', {
363
+ backend_id: 'daytona',
364
+ sandbox_id: this.state.sandboxId,
365
+ }, async () => {
366
+ await this.unmountActiveMounts();
367
+ await deleteDaytonaSandboxWithRetry(async () => {
368
+ await this.sandbox.delete();
369
+ });
370
+ });
371
+ })();
372
+ await this.deletePromise;
373
+ }
374
+ writer() {
375
+ return {
376
+ mkdir: async (path) => {
377
+ await this.sandbox.fs.createFolder(path, '755');
378
+ },
379
+ writeFile: async (path, content) => {
380
+ await this.ensureParentDir(path);
381
+ await this.sandbox.fs.uploadFile(Buffer.from(content), path);
382
+ },
383
+ };
384
+ }
385
+ manifestMaterializationOptions(runAs) {
386
+ return (0, shared_1.manifestMaterializationOptionsWithRunAs)({
387
+ providerName: 'DaytonaSandboxClient',
388
+ providerId: 'daytona',
389
+ runAs,
390
+ runCommand: this.runAsCommandRunner.bind(this),
391
+ options: {
392
+ materializeMount: this.materializeMountEntry.bind(this),
393
+ concurrencyLimits: this.concurrencyLimits,
394
+ },
395
+ support: shared_1.MOUNT_MANIFEST_METADATA_SUPPORT,
396
+ });
397
+ }
398
+ async materializeMountEntry(absolutePath, entry) {
399
+ if (!(0, mounts_1.isDaytonaCloudBucketMountEntry)(entry)) {
400
+ throw new sandbox_1.SandboxUnsupportedFeatureError('DaytonaSandboxClient only supports DaytonaCloudBucketMountStrategy mount entries.', {
401
+ provider: 'daytona',
402
+ feature: 'entry.mountStrategy',
403
+ path: absolutePath,
404
+ mountType: entry.type,
405
+ strategyType: entry.mountStrategy?.type,
406
+ });
407
+ }
408
+ const mountPath = await this.resolveMountEntryPath(absolutePath, entry);
409
+ await (0, inContainerMounts_1.mountRcloneCloudBucket)({
410
+ providerName: 'DaytonaSandboxClient',
411
+ providerId: 'daytona',
412
+ strategyType: 'daytona_cloud_bucket',
413
+ entry,
414
+ mountPath,
415
+ pattern: (0, inContainerMounts_1.rclonePatternFromMountStrategy)(entry.mountStrategy),
416
+ runCommand: this.mountCommandRunner(),
417
+ writeFile: async (path, content) => {
418
+ await this.ensureParentDir(path);
419
+ await this.sandbox.fs.uploadFile(Buffer.from(content), path);
420
+ },
421
+ packageManagers: ['apt', 'apk'],
422
+ });
423
+ this.activeMountPaths.add(mountPath);
424
+ }
425
+ async unmountActiveMounts() {
426
+ for (const mountPath of [...this.activeMountPaths].reverse()) {
427
+ await this.unmountMountPath(mountPath);
428
+ }
429
+ }
430
+ async unmountMountPath(mountPath) {
431
+ await (0, inContainerMounts_1.unmountRcloneMount)({
432
+ providerName: 'DaytonaSandboxClient',
433
+ providerId: 'daytona',
434
+ mountPath,
435
+ runCommand: this.mountCommandRunner(),
436
+ }).catch(() => { });
437
+ this.activeMountPaths.delete(mountPath);
438
+ }
439
+ async resolveMountEntryPath(absolutePath, entry) {
440
+ return await this.resolveRemotePath(entry.mountPath ?? absolutePath, {
441
+ forWrite: true,
442
+ });
443
+ }
444
+ mountCommandRunner() {
445
+ return async (command, options = {}) => {
446
+ const commandToRun = commandForDaytonaUser(command, options.user);
447
+ const result = await this.sandbox.process.executeCommand(commandToRun, this.state.manifest.root, this.state.environment, options.timeoutMs ? Math.ceil(options.timeoutMs / 1000) : undefined);
448
+ return {
449
+ status: result.exitCode,
450
+ stdout: result.artifacts?.stdout ?? result.result ?? '',
451
+ stderr: '',
452
+ };
453
+ };
454
+ }
455
+ async runAsCommandRunner(command, options = {}) {
456
+ const result = await this.sandbox.process.executeCommand(commandForDaytonaUser(command, options.runAs), this.state.manifest.root, this.state.environment, this.state.timeoutSec);
457
+ return {
458
+ status: result.exitCode,
459
+ stdout: result.artifacts?.stdout ?? result.result ?? '',
460
+ stderr: '',
461
+ };
462
+ }
463
+ archiveIo() {
464
+ return {
465
+ runCommand: async (command) => {
466
+ const result = await this.sandbox.process.executeCommand(command, this.state.manifest.root, this.state.environment, this.state.timeoutSec);
467
+ return {
468
+ status: result.exitCode,
469
+ stdout: result.artifacts?.stdout ?? result.result ?? '',
470
+ stderr: '',
471
+ };
472
+ },
473
+ readFile: async (path) => Uint8Array.from(await this.sandbox.fs.downloadFile(path)),
474
+ writeFile: async (path, content) => {
475
+ await this.ensureParentDir(path);
476
+ await this.sandbox.fs.uploadFile(Buffer.from(content), path);
477
+ },
478
+ mkdir: async (path) => {
479
+ await this.sandbox.fs.createFolder(path, '755');
480
+ },
481
+ };
482
+ }
483
+ async readFileBytes(path) {
484
+ const absolutePath = this.resolveEditorPath(path);
485
+ const output = await this.runEditorFileCommand([
486
+ 'set -eu',
487
+ `root=${(0, shared_1.shellQuote)(this.state.manifest.root)}`,
488
+ `path=${(0, shared_1.shellQuote)(absolutePath)}`,
489
+ 'resolved_root=$(realpath -m -- "$root")',
490
+ 'resolved=$(readlink -f -- "$path")',
491
+ 'case "$resolved" in "$resolved_root"|"$resolved_root"/*) ;; *) printf "workspace escape: %s\\n" "$resolved" >&2; exit 111 ;; esac',
492
+ '[ -f "$resolved" ] || { printf "not a regular file: %s\\n" "$resolved" >&2; exit 112; }',
493
+ 'exec 3< "$resolved"',
494
+ 'resolved=$(readlink -f "/proc/$$/fd/3")',
495
+ 'case "$resolved" in "$resolved_root"|"$resolved_root"/*) ;; *) printf "workspace escape: %s\\n" "$resolved" >&2; exit 111 ;; esac',
496
+ 'base64 <&3',
497
+ ].join('\n'));
498
+ return Buffer.from(output.replace(/\s+/gu, ''), 'base64');
499
+ }
500
+ async readEditorText(path) {
501
+ return new TextDecoder().decode(await this.readFileBytes(path));
502
+ }
503
+ async mkdirEditorPath(path) {
504
+ const absolutePath = this.resolveEditorPath(path, { forWrite: true });
505
+ await this.runEditorFileCommand([
506
+ 'set -eu',
507
+ `root=${(0, shared_1.shellQuote)(this.state.manifest.root)}`,
508
+ `path=${(0, shared_1.shellQuote)(absolutePath)}`,
509
+ 'resolved_root=$(realpath -m -- "$root")',
510
+ 'resolved_path=$(realpath -m -- "$path")',
511
+ 'case "$resolved_path" in "$resolved_root"|"$resolved_root"/*) ;; *) printf "workspace escape: %s\\n" "$resolved_path" >&2; exit 111 ;; esac',
512
+ 'mkdir -p -- "$resolved_path"',
513
+ 'resolved_created=$(realpath -m -- "$resolved_path")',
514
+ 'case "$resolved_created" in "$resolved_root"|"$resolved_root"/*) ;; *) printf "workspace escape: %s\\n" "$resolved_created" >&2; exit 111 ;; esac',
515
+ '[ -d "$resolved_created" ] || { printf "not a directory: %s\\n" "$resolved_created" >&2; exit 112; }',
516
+ ].join('\n'));
517
+ }
518
+ async writeEditorText(path, content) {
519
+ const absolutePath = this.resolveEditorPath(path, { forWrite: true });
520
+ const encoded = Buffer.from(content, 'utf8').toString('base64');
521
+ await this.runEditorFileCommand([
522
+ 'set -eu',
523
+ `root=${(0, shared_1.shellQuote)(this.state.manifest.root)}`,
524
+ `path=${(0, shared_1.shellQuote)(absolutePath)}`,
525
+ 'resolved_root=$(realpath -m -- "$root")',
526
+ 'parent=$(dirname -- "$path")',
527
+ 'base=$(basename -- "$path")',
528
+ 'resolved_parent=$(realpath -m -- "$parent")',
529
+ 'case "$resolved_parent" in "$resolved_root"|"$resolved_root"/*) ;; *) printf "workspace escape: %s\\n" "$resolved_parent" >&2; exit 111 ;; esac',
530
+ 'target="$resolved_parent/$base"',
531
+ 'if [ -d "$target" ]; then printf "directory target: %s\\n" "$target" >&2; exit 112; fi',
532
+ 'tmp=$(mktemp "$resolved_parent/.openai-agents-write.XXXXXX")',
533
+ 'cleanup() { rm -f -- "$tmp"; }',
534
+ 'trap cleanup EXIT HUP INT TERM',
535
+ 'base64 -d > "$tmp" <<\'OPENAI_AGENTS_CONTENT\'',
536
+ encoded,
537
+ 'OPENAI_AGENTS_CONTENT',
538
+ 'chmod 644 "$tmp"',
539
+ 'mv -f -- "$tmp" "$target"',
540
+ 'trap - EXIT',
541
+ ].join('\n'));
542
+ }
543
+ async deleteEditorPath(path) {
544
+ const absolutePath = this.resolveEditorPath(path, { forWrite: true });
545
+ await this.runEditorFileCommand([
546
+ 'set -eu',
547
+ `root=${(0, shared_1.shellQuote)(this.state.manifest.root)}`,
548
+ `path=${(0, shared_1.shellQuote)(absolutePath)}`,
549
+ 'resolved_root=$(realpath -m -- "$root")',
550
+ 'parent=$(dirname -- "$path")',
551
+ 'base=$(basename -- "$path")',
552
+ 'resolved_parent=$(realpath -m -- "$parent")',
553
+ 'case "$resolved_parent" in "$resolved_root"|"$resolved_root"/*) ;; *) printf "workspace escape: %s\\n" "$resolved_parent" >&2; exit 111 ;; esac',
554
+ 'rm -f -- "$resolved_parent/$base"',
555
+ ].join('\n'));
556
+ }
557
+ async runEditorFileCommand(command) {
558
+ const result = await this.sandbox.process.executeCommand(command, this.state.manifest.root, this.state.environment, this.state.timeoutSec);
559
+ if (result.exitCode !== 0) {
560
+ throw new agents_core_1.UserError((result.artifacts?.stdout ||
561
+ result.result ||
562
+ 'remote editor operation failed')
563
+ .trim()
564
+ .split(/\r?\n/u)
565
+ .join('; '));
566
+ }
567
+ return result.artifacts?.stdout ?? result.result ?? '';
568
+ }
569
+ resolveEditorPath(path, options = {}) {
570
+ return (0, shared_1.resolveSandboxAbsolutePath)(this.state.manifest.root, path, options);
571
+ }
572
+ async ensureParentDir(path) {
573
+ const parent = (0, shared_1.posixDirname)(path);
574
+ if (parent !== '.' && parent !== '/') {
575
+ await this.sandbox.fs.createFolder(parent, '755');
576
+ }
577
+ }
578
+ async resolveRemotePath(path, options = {}) {
579
+ return await (0, shared_1.validateRemoteSandboxPathForManifest)({
580
+ manifest: this.state.manifest,
581
+ path,
582
+ options,
583
+ runCommand: async (command) => {
584
+ const result = await this.sandbox.process.executeCommand(command, this.state.manifest.root, this.state.environment);
585
+ return {
586
+ status: result.exitCode,
587
+ stdout: result.artifacts?.stdout ?? result.result ?? '',
588
+ stderr: '',
589
+ };
590
+ },
591
+ });
592
+ }
593
+ async createSignedPreviewUrl(port) {
594
+ try {
595
+ if (this.sandbox.getSignedPreviewUrl) {
596
+ return await this.sandbox.getSignedPreviewUrl(port, this.state.exposedPortUrlTtlS);
597
+ }
598
+ }
599
+ catch (error) {
600
+ throw new sandbox_1.SandboxProviderError(`DaytonaSandboxClient failed to resolve exposed port ${port}.`, {
601
+ provider: 'daytona',
602
+ port,
603
+ cause: error instanceof Error ? error.message : String(error),
604
+ });
605
+ }
606
+ throw new sandbox_1.SandboxProviderError('DaytonaSandboxClient exposed port resolution requires a Daytona signed preview URL API.', {
607
+ provider: 'daytona',
608
+ port,
609
+ });
610
+ }
611
+ }
612
+ exports.DaytonaSandboxSession = DaytonaSandboxSession;
613
+ /**
614
+ * @see {@link https://www.daytona.io/docs/ | Daytona docs}.
615
+ * @see {@link https://www.daytona.io/docs/en/getting-started/ | Getting started}.
616
+ * @see {@link https://www.daytona.io/docs/en/typescript-sdk/sandbox/ | TypeScript SDK Sandbox reference}.
617
+ */
618
+ class DaytonaSandboxClient {
619
+ backendId = 'daytona';
620
+ options;
621
+ constructor(options = {}) {
622
+ this.options = options;
623
+ }
624
+ async create(args, manifestOptions) {
625
+ const createArgs = (0, sandbox_1.normalizeSandboxClientCreateArgs)(args, manifestOptions);
626
+ (0, shared_1.assertCoreSnapshotUnsupported)('DaytonaSandboxClient', createArgs.snapshot);
627
+ const manifest = createArgs.manifest;
628
+ const resolvedOptions = {
629
+ ...this.options,
630
+ ...createArgs.options,
631
+ };
632
+ const resolvedManifest = resolveManifestRoot(manifest);
633
+ (0, shared_1.assertSandboxManifestMetadataSupported)('DaytonaSandboxClient', resolvedManifest, shared_1.MOUNT_MANIFEST_METADATA_SUPPORT);
634
+ const client = await createDaytonaClient(resolvedOptions);
635
+ return await (0, shared_1.withSandboxSpan)('sandbox.start', {
636
+ backend_id: this.backendId,
637
+ }, async () => {
638
+ const environment = await (0, shared_1.materializeEnvironment)(resolvedManifest, resolvedOptions.env);
639
+ const sandbox = await (0, shared_1.withProviderError)('DaytonaSandboxClient', 'daytona', 'create sandbox', async () => await client.create({
640
+ ...(resolvedOptions.sandboxSnapshotName
641
+ ? { snapshot: resolvedOptions.sandboxSnapshotName }
642
+ : { image: resolvedOptions.image ?? 'debian:12.9' }),
643
+ ...(!resolvedOptions.sandboxSnapshotName &&
644
+ resolvedOptions.resources
645
+ ? { resources: resolvedOptions.resources }
646
+ : {}),
647
+ ...(resolvedOptions.name ? { name: resolvedOptions.name } : {}),
648
+ ...(typeof resolvedOptions.autoStopInterval === 'number'
649
+ ? { autoStopInterval: resolvedOptions.autoStopInterval }
650
+ : {}),
651
+ ...(typeof resolvedOptions.startTimeoutSec === 'number'
652
+ ? { startTimeoutSec: resolvedOptions.startTimeoutSec }
653
+ : {}),
654
+ ...(typeof resolvedOptions.timeoutSec === 'number'
655
+ ? { timeoutSec: resolvedOptions.timeoutSec }
656
+ : {}),
657
+ ...(resolvedOptions.exposedPorts
658
+ ? { exposedPorts: resolvedOptions.exposedPorts }
659
+ : {}),
660
+ ...(typeof resolvedOptions.exposedPortUrlTtlS === 'number'
661
+ ? { exposedPortUrlTtlS: resolvedOptions.exposedPortUrlTtlS }
662
+ : {}),
663
+ envVars: environment,
664
+ }, ...(typeof resolvedOptions.createTimeoutSec === 'number'
665
+ ? [{ timeout: resolvedOptions.createTimeoutSec }]
666
+ : [])), resolvedOptions.sandboxSnapshotName
667
+ ? { snapshot: resolvedOptions.sandboxSnapshotName }
668
+ : { image: resolvedOptions.image ?? 'debian:12.9' });
669
+ const session = new DaytonaSandboxSession({
670
+ sandbox,
671
+ concurrencyLimits: createArgs.concurrencyLimits,
672
+ state: {
673
+ manifest: resolvedManifest,
674
+ sandboxId: sandbox.id,
675
+ image: resolvedOptions.image,
676
+ resources: resolvedOptions.resources,
677
+ pauseOnExit: resolvedOptions.pauseOnExit ?? false,
678
+ createTimeoutSec: resolvedOptions.createTimeoutSec,
679
+ startTimeoutSec: resolvedOptions.startTimeoutSec,
680
+ timeoutSec: resolvedOptions.timeoutSec,
681
+ sandboxSnapshotName: resolvedOptions.sandboxSnapshotName,
682
+ configuredExposedPorts: resolvedOptions.exposedPorts,
683
+ exposedPortUrlTtlS: resolvedOptions.exposedPortUrlTtlS,
684
+ name: resolvedOptions.name,
685
+ autoStopInterval: resolvedOptions.autoStopInterval,
686
+ environment,
687
+ apiKey: resolvedOptions.apiKey,
688
+ apiUrl: resolvedOptions.apiUrl,
689
+ target: resolvedOptions.target,
690
+ },
691
+ });
692
+ try {
693
+ await session.prepareWorkspaceRoot();
694
+ await session.materializeInitialManifest(resolvedManifest);
695
+ }
696
+ catch (error) {
697
+ session.state.pauseOnExit = false;
698
+ await (0, shared_1.closeRemoteSessionOnManifestError)('Daytona', session, error);
699
+ }
700
+ return session;
701
+ });
702
+ }
703
+ async serializeSessionState(state) {
704
+ return (0, shared_1.serializeRemoteSandboxSessionState)(state);
705
+ }
706
+ canPersistOwnedSessionState(state) {
707
+ return state.pauseOnExit;
708
+ }
709
+ async deserializeSessionState(state) {
710
+ const baseState = (0, shared_1.deserializeRemoteSandboxSessionStateValues)(state, this.options.env);
711
+ return {
712
+ ...state,
713
+ ...baseState,
714
+ sandboxId: (0, shared_1.readString)(state, 'sandboxId'),
715
+ pauseOnExit: Boolean(state.pauseOnExit),
716
+ image: (0, shared_1.readOptionalString)(state, 'image'),
717
+ resources: (0, shared_1.readOptionalRecord)(state.resources),
718
+ createTimeoutSec: (0, shared_1.readOptionalNumber)(state, 'createTimeoutSec'),
719
+ startTimeoutSec: (0, shared_1.readOptionalNumber)(state, 'startTimeoutSec'),
720
+ timeoutSec: (0, shared_1.readOptionalNumber)(state, 'timeoutSec'),
721
+ sandboxSnapshotName: (0, shared_1.readOptionalString)(state, 'sandboxSnapshotName'),
722
+ configuredExposedPorts: (0, shared_1.readOptionalNumberArray)(state.configuredExposedPorts),
723
+ exposedPortUrlTtlS: (0, shared_1.readOptionalNumber)(state, 'exposedPortUrlTtlS'),
724
+ name: (0, shared_1.readOptionalString)(state, 'name'),
725
+ autoStopInterval: (0, shared_1.readOptionalNumber)(state, 'autoStopInterval'),
726
+ apiKey: (0, shared_1.readOptionalString)(state, 'apiKey'),
727
+ apiUrl: (0, shared_1.readOptionalString)(state, 'apiUrl'),
728
+ target: (0, shared_1.readOptionalString)(state, 'target'),
729
+ };
730
+ }
731
+ async resume(state) {
732
+ const client = await createDaytonaClient({
733
+ ...this.options,
734
+ apiKey: state.apiKey ?? this.options.apiKey,
735
+ apiUrl: state.apiUrl ?? this.options.apiUrl,
736
+ target: state.target ?? this.options.target,
737
+ });
738
+ let sandbox;
739
+ try {
740
+ sandbox = await client.get(state.sandboxId);
741
+ await sandbox.start(state.startTimeoutSec);
742
+ }
743
+ catch (error) {
744
+ (0, shared_1.assertResumeRecreateAllowed)(error, {
745
+ providerName: 'DaytonaSandboxClient',
746
+ provider: 'daytona',
747
+ details: { sandboxId: state.sandboxId },
748
+ });
749
+ return await this.recreateFromPersistedState(client, state);
750
+ }
751
+ const session = new DaytonaSandboxSession({ state, sandbox });
752
+ await session.prepareWorkspaceRoot();
753
+ await session.rematerializeMountEntries();
754
+ return session;
755
+ }
756
+ async recreateFromPersistedState(client, state) {
757
+ const sandbox = await (0, shared_1.withProviderError)('DaytonaSandboxClient', 'daytona', 'create sandbox', async () => await client.create({
758
+ ...(state.sandboxSnapshotName
759
+ ? { snapshot: state.sandboxSnapshotName }
760
+ : { image: state.image ?? 'debian:12.9' }),
761
+ ...(!state.sandboxSnapshotName && state.resources
762
+ ? { resources: state.resources }
763
+ : {}),
764
+ ...(state.name ? { name: state.name } : {}),
765
+ ...(typeof state.autoStopInterval === 'number'
766
+ ? { autoStopInterval: state.autoStopInterval }
767
+ : {}),
768
+ ...(typeof state.startTimeoutSec === 'number'
769
+ ? { startTimeoutSec: state.startTimeoutSec }
770
+ : {}),
771
+ ...(typeof state.timeoutSec === 'number'
772
+ ? { timeoutSec: state.timeoutSec }
773
+ : {}),
774
+ ...(state.configuredExposedPorts
775
+ ? { exposedPorts: state.configuredExposedPorts }
776
+ : {}),
777
+ ...(typeof state.exposedPortUrlTtlS === 'number'
778
+ ? { exposedPortUrlTtlS: state.exposedPortUrlTtlS }
779
+ : {}),
780
+ envVars: state.environment,
781
+ }, ...(typeof state.createTimeoutSec === 'number'
782
+ ? [{ timeout: state.createTimeoutSec }]
783
+ : [])), state.sandboxSnapshotName
784
+ ? { snapshot: state.sandboxSnapshotName }
785
+ : { image: state.image ?? 'debian:12.9' });
786
+ const nextState = {
787
+ ...state,
788
+ sandboxId: sandbox.id,
789
+ environment: { ...state.environment },
790
+ };
791
+ delete nextState.exposedPorts;
792
+ const session = new DaytonaSandboxSession({
793
+ sandbox,
794
+ state: nextState,
795
+ });
796
+ try {
797
+ await session.prepareWorkspaceRoot();
798
+ await session.materializeInitialManifest(state.manifest);
799
+ }
800
+ catch (error) {
801
+ session.state.pauseOnExit = false;
802
+ await (0, shared_1.closeRemoteSessionOnManifestError)('Daytona', session, error);
803
+ }
804
+ return session;
805
+ }
806
+ }
807
+ exports.DaytonaSandboxClient = DaytonaSandboxClient;
808
+ function getUsableDaytonaCachedExposedPortEndpoint(state, requestedPort) {
809
+ const cached = (0, shared_1.getCachedExposedPortEndpoint)(state, requestedPort);
810
+ if (!cached) {
811
+ return undefined;
812
+ }
813
+ const expiresAtMs = cached.daytonaExpiresAtMs;
814
+ return typeof expiresAtMs === 'number' &&
815
+ Number.isFinite(expiresAtMs) &&
816
+ expiresAtMs > Date.now()
817
+ ? cached
818
+ : undefined;
819
+ }
820
+ function withDaytonaPreviewExpiration(endpoint, ttlS) {
821
+ const resolvedTtlS = typeof ttlS === 'number' ? ttlS : DEFAULT_EXPOSED_PORT_URL_TTL_S;
822
+ if (!Number.isFinite(resolvedTtlS) || resolvedTtlS <= 0) {
823
+ return endpoint;
824
+ }
825
+ return {
826
+ ...endpoint,
827
+ daytonaExpiresAtMs: Date.now() + resolvedTtlS * 1000,
828
+ };
829
+ }
830
+ async function createDaytonaClient(options) {
831
+ try {
832
+ const { Daytona } = await Promise.resolve().then(() => __importStar(require('@daytonaio/sdk')));
833
+ return adaptDaytonaClient(new Daytona({
834
+ ...(options.apiKey ? { apiKey: options.apiKey } : {}),
835
+ ...(options.apiUrl ? { apiUrl: options.apiUrl } : {}),
836
+ ...(options.target ? { target: options.target } : {}),
837
+ }));
838
+ }
839
+ catch (error) {
840
+ throw new agents_core_1.UserError(`Daytona sandbox support requires the optional \`@daytonaio/sdk\` package. Install it before using Daytona-backed sandbox examples. ${error.message}`);
841
+ }
842
+ }
843
+ function adaptDaytonaClient(client) {
844
+ return {
845
+ create: async (params, options) => adaptDaytonaSandbox(await client.create(params, options)),
846
+ get: async (idOrName) => adaptDaytonaSandbox(await client.get(idOrName)),
847
+ };
848
+ }
849
+ function adaptDaytonaSandbox(sandbox) {
850
+ return {
851
+ id: sandbox.id,
852
+ start: async (timeout) => await sandbox.start(timeout),
853
+ stop: async (timeout, force) => await sandbox.stop(timeout, force),
854
+ delete: async (timeout) => await sandbox.delete(timeout),
855
+ fs: {
856
+ createFolder: async (path, mode) => await sandbox.fs.createFolder(path, mode),
857
+ uploadFile: async (source, remotePath, timeout) => {
858
+ if (typeof source === 'string') {
859
+ if (typeof timeout === 'number') {
860
+ await sandbox.fs.uploadFile(source, remotePath, timeout);
861
+ }
862
+ else {
863
+ await sandbox.fs.uploadFile(source, remotePath);
864
+ }
865
+ return;
866
+ }
867
+ if (typeof timeout === 'number') {
868
+ await sandbox.fs.uploadFile(source, remotePath, timeout);
869
+ }
870
+ else {
871
+ await sandbox.fs.uploadFile(source, remotePath);
872
+ }
873
+ },
874
+ downloadFile: async (remotePath, timeout) => await sandbox.fs.downloadFile(remotePath, timeout),
875
+ deleteFile: async (path, recursive) => await sandbox.fs.deleteFile(path, recursive),
876
+ },
877
+ process: {
878
+ executeCommand: async (command, cwd, env, timeout) => typeof timeout === 'number'
879
+ ? await sandbox.process.executeCommand(command, cwd, env, timeout)
880
+ : await sandbox.process.executeCommand(command, cwd, env),
881
+ ...(typeof sandbox.process.createPty === 'function'
882
+ ? {
883
+ createPty: async (options) => await sandbox.process.createPty(options),
884
+ }
885
+ : {}),
886
+ ...(typeof sandbox.process.killPtySession === 'function'
887
+ ? {
888
+ killPtySession: async (id) => await sandbox.process.killPtySession(id),
889
+ }
890
+ : {}),
891
+ },
892
+ getSignedPreviewUrl: async (port, expiresInSeconds) => await sandbox.getSignedPreviewUrl(port, expiresInSeconds),
893
+ };
894
+ }
895
+ function resolveManifestRoot(manifest) {
896
+ if (manifest.root === '/workspace') {
897
+ return (0, shared_1.cloneManifestWithRoot)(manifest, DEFAULT_WORKSPACE_ROOT);
898
+ }
899
+ return manifest;
900
+ }
901
+ function commandForDaytonaUser(command, user) {
902
+ return (0, shared_1.sandboxUserShellCommand)(command, user);
903
+ }
904
+ async function deleteDaytonaSandboxWithRetry(deleteSandbox) {
905
+ let attempt = 0;
906
+ while (true) {
907
+ try {
908
+ await deleteSandbox();
909
+ return;
910
+ }
911
+ catch (error) {
912
+ const delayMs = DAYTONA_DELETE_RETRY_DELAYS_MS[attempt];
913
+ if (!isDaytonaStateChangeInProgressError(error) ||
914
+ delayMs === undefined) {
915
+ throw error;
916
+ }
917
+ attempt += 1;
918
+ await new Promise((resolve) => setTimeout(resolve, delayMs));
919
+ }
920
+ }
921
+ }
922
+ function isDaytonaStateChangeInProgressError(error) {
923
+ if (typeof error !== 'object' || error === null) {
924
+ return false;
925
+ }
926
+ const record = error;
927
+ return (record.statusCode === 409 &&
928
+ error instanceof Error &&
929
+ /state change in progress/i.test(error.message));
930
+ }
931
+ function exitCodeFromDaytonaResult(value) {
932
+ if (typeof value !== 'object' || value === null || Array.isArray(value)) {
933
+ return undefined;
934
+ }
935
+ const record = value;
936
+ return typeof record.exitCode === 'number' ? record.exitCode : undefined;
937
+ }
938
+ //# sourceMappingURL=sandbox.js.map