(input: SandboxCreateInput)
| 88 | } |
| 89 | |
| 90 | async create(input: SandboxCreateInput): Promise<SandboxHandle> { |
| 91 | // Honor the deterministic id ensure() supplies (see SandboxCreateInput.id). |
| 92 | // The sprite's preview URL is keyed by name, so a random name would strand |
| 93 | // an out-of-band reconnect (a preview iframe) on a different sprite than the |
| 94 | // one the agent edits. Fall back to a random name for direct/advanced use. |
| 95 | const name = |
| 96 | input.id ?? |
| 97 | `${NAME_PREFIX}-${randomUUID().replace(/-/g, '').slice(0, 12)}` |
| 98 | const sprite = await this.client.createSprite(name, { |
| 99 | ...(this.config.waitForCapacity !== undefined |
| 100 | ? { waitForCapacity: this.config.waitForCapacity } |
| 101 | : {}), |
| 102 | ...(input.signal ? { signal: input.signal } : {}), |
| 103 | }) |
| 104 | |
| 105 | if (sprite.urlAuth !== this.urlAuth) { |
| 106 | await this.client.setUrlAuth(sprite.name, this.urlAuth, input.signal) |
| 107 | } |
| 108 | |
| 109 | // Ensure the workspace dir exists before any cwd-bound command runs in it. |
| 110 | // Run from `/` (not the workdir, which does not exist yet) so the exec's own |
| 111 | // cwd resolution does not fail for a non-default `workdir`. |
| 112 | const mkdir = this.client.exec(sprite.name, { |
| 113 | argv: ['mkdir', '-p', this.workdir], |
| 114 | cwd: '/', |
| 115 | ...(input.signal ? { signal: input.signal } : {}), |
| 116 | }) |
| 117 | const code = await mkdir.wait() |
| 118 | if (code !== 0) { |
| 119 | throw new Error( |
| 120 | `Sprites: failed to create workspace directory "${this.workdir}" (exit ${code}).`, |
| 121 | ) |
| 122 | } |
| 123 | |
| 124 | const handle = this.handle(sprite) |
| 125 | if (input.env) await handle.env.set(input.env) |
| 126 | return handle |
| 127 | } |
| 128 | |
| 129 | async resume(input: SandboxResumeInput): Promise<SandboxHandle | null> { |
| 130 | try { |
nothing calls this directly
no test coverage detected