@coder/ai-sdk-sandbox 0.1.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.
- package/LICENSE +202 -0
- package/README.md +328 -0
- package/dist/index.d.ts +447 -0
- package/dist/index.js +982 -0
- package/package.json +75 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,328 @@
|
|
|
1
|
+
# @coder/ai-sdk-sandbox
|
|
2
|
+
|
|
3
|
+
A [Coder](https://coder.com) workspace sandbox provider for the Vercel AI SDK v7
|
|
4
|
+
**HarnessAgent**. It lets you run CLI coding agents inside a real Coder workspace
|
|
5
|
+
instead of an ephemeral cloud sandbox. Claude Code is verified end-to-end here;
|
|
6
|
+
Codex is expected to work via the same bridge mechanism but is not yet verified
|
|
7
|
+
in this repo.
|
|
8
|
+
|
|
9
|
+
It implements the `HarnessV1SandboxProvider` contract from `@ai-sdk/harness`, so
|
|
10
|
+
you pass it as the `sandbox` to a `HarnessAgent` exactly like
|
|
11
|
+
`@ai-sdk/sandbox-vercel`.
|
|
12
|
+
|
|
13
|
+
> **Status:** experimental. The AI SDK harness packages are published under the
|
|
14
|
+
> `@canary` tag and their APIs can change between releases. This provider tracks
|
|
15
|
+
> `@ai-sdk/harness@1.0.0-canary.11`.
|
|
16
|
+
|
|
17
|
+
## Install
|
|
18
|
+
|
|
19
|
+
```bash
|
|
20
|
+
npm add @coder/ai-sdk-sandbox @ai-sdk/harness@canary @ai-sdk/harness-claude-code@canary @ai-sdk/provider-utils@canary
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
On the host you also need:
|
|
24
|
+
|
|
25
|
+
- the [`coder` CLI](https://coder.com/docs/install) on PATH and authenticated
|
|
26
|
+
(`coder login`); for non-ambient auth, configure the transport explicitly with
|
|
27
|
+
`new CoderCliTransport({ url, token })` (see [Settings](#settings));
|
|
28
|
+
- an **OpenSSH client** (`ssh`) on PATH — exec runs through it (see "How it works").
|
|
29
|
+
|
|
30
|
+
## Quick start
|
|
31
|
+
|
|
32
|
+
Wrap an existing, running workspace and run Claude Code in it:
|
|
33
|
+
|
|
34
|
+
```ts
|
|
35
|
+
import { HarnessAgent } from "@ai-sdk/harness/agent";
|
|
36
|
+
import { createClaudeCode } from "@ai-sdk/harness-claude-code";
|
|
37
|
+
import { createCoderWorkspace } from "@coder/ai-sdk-sandbox";
|
|
38
|
+
|
|
39
|
+
const agent = new HarnessAgent({
|
|
40
|
+
harness: createClaudeCode({ thinking: "adaptive" }),
|
|
41
|
+
sandbox: createCoderWorkspace({ workspace: "my-dev-workspace" }),
|
|
42
|
+
instructions: "You are a careful coding assistant.",
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
const session = await agent.createSession();
|
|
46
|
+
try {
|
|
47
|
+
const result = await agent.generate({
|
|
48
|
+
session,
|
|
49
|
+
prompt: "Create a short TODO.md in the repo root.",
|
|
50
|
+
});
|
|
51
|
+
console.log(result.text);
|
|
52
|
+
} finally {
|
|
53
|
+
await session.destroy();
|
|
54
|
+
}
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
See [`examples/claude-code.ts`](./examples/claude-code.ts) for a runnable version.
|
|
58
|
+
|
|
59
|
+
## Creating workspaces on demand
|
|
60
|
+
|
|
61
|
+
Instead of pointing at an existing workspace, you can have the provider **create
|
|
62
|
+
one from a template** — with parameters and/or a preset — and tear it down when
|
|
63
|
+
the session ends. Add a `create` block:
|
|
64
|
+
|
|
65
|
+
```ts
|
|
66
|
+
const agent = new HarnessAgent({
|
|
67
|
+
harness: createClaudeCode({ thinking: "adaptive" }),
|
|
68
|
+
sandbox: createCoderWorkspace({
|
|
69
|
+
create: {
|
|
70
|
+
template: "docker", // required: the template to create from
|
|
71
|
+
preset: "Large", // optional: a template version preset
|
|
72
|
+
parameters: { cpus: 8, region: "us-west-2" },
|
|
73
|
+
useParameterDefaults: true, // accept template defaults for the rest
|
|
74
|
+
stopAfter: "8h", // auto-stop TTL
|
|
75
|
+
},
|
|
76
|
+
}),
|
|
77
|
+
});
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
By default this is **fresh-per-session**: the workspace name is derived from the
|
|
81
|
+
harness `sessionId` (e.g. `agent-1a2b3c4d5e6f`), so each session gets its own
|
|
82
|
+
workspace, and `session.destroy()` deletes it. `resumeSession` re-derives the
|
|
83
|
+
same name and reattaches. The provider waits for the workspace agent to finish
|
|
84
|
+
connecting and running its startup script (`lifecycle_state: ready`) before the
|
|
85
|
+
harness runs — a successful _build_ is not enough on its own.
|
|
86
|
+
|
|
87
|
+
You can also **get-or-create a named workspace** by combining `workspace` with
|
|
88
|
+
`create`: if it exists the provider attaches to it (and never deletes it); if it
|
|
89
|
+
doesn't, the provider creates it (and, by default, owns it).
|
|
90
|
+
|
|
91
|
+
```ts
|
|
92
|
+
createCoderWorkspace({
|
|
93
|
+
workspace: "my-agent-ws",
|
|
94
|
+
create: { template: "docker", ifExists: "attach" }, // 'attach' (default) | 'error'
|
|
95
|
+
});
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
**Parameters vs. presets.** A preset's parameter values take precedence over an
|
|
99
|
+
overlapping `parameters` entry of the same name (this is Coder's behavior), so
|
|
100
|
+
set a given value via the preset _or_ `parameters`, not both. Required
|
|
101
|
+
parameters (those without a template default) must be supplied via `parameters`,
|
|
102
|
+
`parameterFile`, a `preset`, or `useParameterDefaults` — otherwise creation
|
|
103
|
+
fails (it can't prompt non-interactively). If you set a `preset`, the provider
|
|
104
|
+
preflight-validates the name against the template's presets and fails fast with
|
|
105
|
+
the available names (set `validate: false` to skip).
|
|
106
|
+
|
|
107
|
+
### Create settings
|
|
108
|
+
|
|
109
|
+
```ts
|
|
110
|
+
createCoderWorkspace({
|
|
111
|
+
create: {
|
|
112
|
+
template: "docker", // required
|
|
113
|
+
templateVersion: undefined, // default: the template's active version
|
|
114
|
+
preset: undefined, // 'none' forces no preset
|
|
115
|
+
parameters: {}, // { name: value }; numbers/bools stringified
|
|
116
|
+
parameterFile: undefined, // path to a YAML rich-parameter file
|
|
117
|
+
useParameterDefaults: false, // accept template defaults where unset
|
|
118
|
+
ephemeralParameters: {}, // one-time build parameters
|
|
119
|
+
stopAfter: undefined, // e.g. '8h' (auto-stop TTL)
|
|
120
|
+
automaticUpdates: undefined, // 'always' | 'never'
|
|
121
|
+
org: undefined, // --org, for ambiguous template names
|
|
122
|
+
owner: undefined, // owner for a derived name (owner/name)
|
|
123
|
+
ifExists: "attach", // 'attach' | 'error'
|
|
124
|
+
namePrefix: "agent", // prefix for the derived per-session name
|
|
125
|
+
validate: true, // preflight-check the preset name
|
|
126
|
+
},
|
|
127
|
+
readyTimeoutMs: 300_000, // wait budget for the agent to become ready
|
|
128
|
+
});
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
## Terminal UI
|
|
132
|
+
|
|
133
|
+
For an interactive chat in your terminal instead of one-shot `generate()` calls,
|
|
134
|
+
wrap the same agent with the AI SDK terminal UI ([`@ai-sdk/tui`](https://ai-sdk.dev/v7/docs/ai-sdk-harnesses/terminal-ui)):
|
|
135
|
+
|
|
136
|
+
```bash
|
|
137
|
+
npm add @ai-sdk/tui@canary
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
The TUI drives a session-less agent, so adapt the `HarnessAgent` (whose
|
|
141
|
+
`generate`/`stream` take a session) by injecting the session for the TUI's
|
|
142
|
+
lifetime:
|
|
143
|
+
|
|
144
|
+
```ts
|
|
145
|
+
import { HarnessAgent, type HarnessAgentSession } from "@ai-sdk/harness/agent";
|
|
146
|
+
import { createClaudeCode } from "@ai-sdk/harness-claude-code";
|
|
147
|
+
import { runAgentTUI, type AgentTUIAgent } from "@ai-sdk/tui";
|
|
148
|
+
import { createCoderWorkspace } from "@coder/ai-sdk-sandbox";
|
|
149
|
+
|
|
150
|
+
const agent = new HarnessAgent({
|
|
151
|
+
harness: createClaudeCode({ thinking: "adaptive" }),
|
|
152
|
+
sandbox: createCoderWorkspace({ workspace: "my-dev-ws" }),
|
|
153
|
+
// or, to create a fresh workspace per session from a template:
|
|
154
|
+
// sandbox: createCoderWorkspace({ create: { template: 'claude-code-test' } }),
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
const toTUIAgent = (agent: HarnessAgent, session: HarnessAgentSession): AgentTUIAgent => ({
|
|
158
|
+
version: "agent-v1",
|
|
159
|
+
id: agent.id,
|
|
160
|
+
tools: agent.tools,
|
|
161
|
+
generate: (request) => agent.generate({ ...request, session }),
|
|
162
|
+
stream: (request) => agent.stream({ ...request, session }),
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
const session = await agent.createSession();
|
|
166
|
+
try {
|
|
167
|
+
await runAgentTUI({ title: "Claude Code @ Coder", agent: toTUIAgent(agent, session) });
|
|
168
|
+
} finally {
|
|
169
|
+
await session.destroy();
|
|
170
|
+
}
|
|
171
|
+
```
|
|
172
|
+
|
|
173
|
+
See [`examples/claude-code-tui.ts`](./examples/claude-code-tui.ts) for a runnable
|
|
174
|
+
version: `CODER_WORKSPACE=my-dev-ws npx tsx examples/claude-code-tui.ts` (exit with
|
|
175
|
+
Esc or Ctrl+C).
|
|
176
|
+
|
|
177
|
+
## Workspace requirements
|
|
178
|
+
|
|
179
|
+
Because the bridge runs inside the workspace, the workspace image must have:
|
|
180
|
+
|
|
181
|
+
- **Node.js** (the docs use `node24`). The bridge is `node bridge.mjs`.
|
|
182
|
+
- **pnpm** available on PATH (e.g. via `corepack enable`) — the adapter uses it to
|
|
183
|
+
bootstrap the bridge.
|
|
184
|
+
- **Outbound network access** to the npm registry (the adapter `pnpm install`s
|
|
185
|
+
the bridge's dependencies + the Claude Code CLI on first use) and to the model
|
|
186
|
+
API (`api.anthropic.com` for Claude Code, `api.openai.com` for Codex). Bake the
|
|
187
|
+
dependencies into the image to avoid per-session installs.
|
|
188
|
+
- **The model API key** available to the bridge — `ANTHROPIC_API_KEY` /
|
|
189
|
+
`OPENAI_API_KEY`. Configure it through the adapter's `auth` option or ensure it
|
|
190
|
+
is present in the workspace environment.
|
|
191
|
+
- `bash` and `base64` (standard on any Linux dev image).
|
|
192
|
+
|
|
193
|
+
## Settings
|
|
194
|
+
|
|
195
|
+
**At least one of `workspace` or `create` is required** (you may set both — see
|
|
196
|
+
[Creating workspaces on demand](#creating-workspaces-on-demand)).
|
|
197
|
+
|
|
198
|
+
```ts
|
|
199
|
+
import { createCoderWorkspace, CoderCliTransport } from "@coder/ai-sdk-sandbox";
|
|
200
|
+
|
|
201
|
+
createCoderWorkspace({
|
|
202
|
+
// One of these is required (TypeScript enforces it):
|
|
203
|
+
workspace: "my-ws", // fixed name, or (sessionId) => `agent-${sessionId}`
|
|
204
|
+
create: undefined, // create from a template; see "Creating workspaces"
|
|
205
|
+
|
|
206
|
+
readyTimeoutMs: 300_000, // wait budget for the agent to become ready
|
|
207
|
+
ports: [4000], // exposed ports; ports[0] is the bridge port
|
|
208
|
+
defaultWorkingDirectory: "/home/coder", // default: resolved from $HOME, else /home/coder
|
|
209
|
+
ownsLifecycle: false, // see "Lifecycle modes" below
|
|
210
|
+
ensureStarted: false, // run `coder start` before attaching
|
|
211
|
+
|
|
212
|
+
// Transport. Defaults to an ambient-login CoderCliTransport. Configure the CLI
|
|
213
|
+
// transport (binary paths, url/token, env, login shell, wait mode) — or supply
|
|
214
|
+
// a non-CLI/test transport — by constructing one explicitly:
|
|
215
|
+
transport: new CoderCliTransport({
|
|
216
|
+
// coderBinary: 'coder', sshBinary: 'ssh',
|
|
217
|
+
// url: process.env.CODER_URL, token: process.env.CODER_SESSION_TOKEN,
|
|
218
|
+
// env: {}, loginShell: true, waitMode: 'no',
|
|
219
|
+
}),
|
|
220
|
+
});
|
|
221
|
+
```
|
|
222
|
+
|
|
223
|
+
### Lifecycle modes
|
|
224
|
+
|
|
225
|
+
- **Wrap an existing workspace (default when there's no `create`,
|
|
226
|
+
`ownsLifecycle: false`).** `stop()` and `destroy()` only release host-side
|
|
227
|
+
resources (port-forwards); the workspace keeps running. The natural fit for
|
|
228
|
+
long-lived dev workspaces.
|
|
229
|
+
- **Own the workspace (`ownsLifecycle: true`).** `stop()` runs `coder stop` and
|
|
230
|
+
`destroy()` runs `coder delete`.
|
|
231
|
+
- **Create mode (`create` set).** `ownsLifecycle` defaults to `true`, so a
|
|
232
|
+
workspace the provider creates is deleted on `destroy()` and `onFirstCreate`
|
|
233
|
+
runs as its bootstrap hook. As a safety measure, a workspace the provider only
|
|
234
|
+
_attached_ to (an explicitly-named, pre-existing one) is **never** deleted —
|
|
235
|
+
only ones it actually created. A per-session derived name is always treated as
|
|
236
|
+
owned. Set `ownsLifecycle: false` for "create-if-missing but never delete".
|
|
237
|
+
|
|
238
|
+
### Ports
|
|
239
|
+
|
|
240
|
+
The adapter binds its bridge to a port and resolves it from
|
|
241
|
+
`createClaudeCode({ port })` or, by default, `sandbox.ports[0]`. Expose that port
|
|
242
|
+
via `ports` (default `[4000]`); `getPortUrl` opens an OpenSSH `-L` local forward
|
|
243
|
+
(over the same `coder ssh --stdio` ProxyCommand) to it on demand and returns a
|
|
244
|
+
loopback `ws://` URL. The forward is plaintext on loopback, so `https`/`wss`
|
|
245
|
+
requests resolve to their `http`/`ws` loopback equivalent.
|
|
246
|
+
|
|
247
|
+
## How it works
|
|
248
|
+
|
|
249
|
+
A `HarnessAgent` doesn't run the agent CLI directly. For bridge-backed adapters
|
|
250
|
+
(Claude Code, Codex) it installs a small Node "bridge" program **inside the
|
|
251
|
+
sandbox**, spawns it, and talks to it over an **authenticated WebSocket**. The
|
|
252
|
+
bridge runs the vendor SDK in-workspace and streams events back to the host.
|
|
253
|
+
|
|
254
|
+
This provider maps that contract onto Coder primitives:
|
|
255
|
+
|
|
256
|
+
| Harness contract | Coder implementation |
|
|
257
|
+
| ------------------------------------------- | ------------------------------------------------------------------------------------------- |
|
|
258
|
+
| `run` / `spawn` | OpenSSH `bash -lc '…'` over a `coder ssh --stdio` ProxyCommand |
|
|
259
|
+
| `readFile` / `writeFile` / `read*`/`write*` | base64 piped over the SSH connection (binary-safe) |
|
|
260
|
+
| `getPortUrl({ port, protocol })` | OpenSSH `-L <local>:127.0.0.1:<port>` over the same ProxyCommand → `ws://127.0.0.1:<local>` |
|
|
261
|
+
| `ports` / `setPorts` | the workspace's exposed port set |
|
|
262
|
+
| `createSession` / `resumeSession` / `id` | attach to a workspace by name |
|
|
263
|
+
| `stop` / `destroy` | `coder stop` / `coder delete` (only when it owns the lifecycle) |
|
|
264
|
+
|
|
265
|
+
**Why OpenSSH and not `coder ssh <ws> -- cmd`?** `coder ssh` allocates a PTY for
|
|
266
|
+
the command, which rewrites newlines to CRLF, merges stdout and stderr onto one
|
|
267
|
+
stream, and does not reliably propagate exit codes — all fatal for programmatic
|
|
268
|
+
use and for the bridge's stdout parsing. `coder ssh`'s own help recommends
|
|
269
|
+
`coder config-ssh` "for users who need the full functionality of SSH"; this
|
|
270
|
+
provider does the programmatic equivalent, running real OpenSSH over a
|
|
271
|
+
`coder ssh --stdio` ProxyCommand. That yields clean, separated streams and
|
|
272
|
+
correct exit codes (verified against a live workspace).
|
|
273
|
+
|
|
274
|
+
The WebSocket the harness opens against `getPortUrl(...)` is the critical path,
|
|
275
|
+
and it needs no wildcard access URLs — the host running `HarnessAgent` is already
|
|
276
|
+
a Coder client. We forward via OpenSSH `-L` rather than `coder port-forward`:
|
|
277
|
+
the bridge sends an _unprompted_ `bridge-hello` frame immediately after the WS
|
|
278
|
+
upgrade, and in testing a freshly-created `coder port-forward` tunnel did not
|
|
279
|
+
reliably deliver that first server-initiated frame to the first WS client,
|
|
280
|
+
whereas SSH local forwarding does. This path is verified end-to-end against a
|
|
281
|
+
real workspace — both a synthetic WebSocket round-trip (`scripts/verify-real.ts`)
|
|
282
|
+
and a full Claude Code turn with tool use (`scripts/e2e-claude.ts`).
|
|
283
|
+
|
|
284
|
+
## Limitations & notes
|
|
285
|
+
|
|
286
|
+
- `setNetworkPolicy` is not implemented (omitted) — egress is governed by your
|
|
287
|
+
Coder template/deployment, not this provider.
|
|
288
|
+
- `bridgePorts` is intentionally left undefined: this provider binds one
|
|
289
|
+
workspace per session rather than leasing ports from a shared sandbox.
|
|
290
|
+
- File reads buffer the whole file (binary content moves as base64). Fine for
|
|
291
|
+
bootstrap-sized files; not intended for streaming very large files.
|
|
292
|
+
- `@ai-sdk/sandbox-just-bash` cannot expose ports and is rejected by bridge-backed
|
|
293
|
+
adapters — this provider exists precisely to provide that port.
|
|
294
|
+
- To run Claude Code / Codex, the **workspace** image needs Node.js (the adapter
|
|
295
|
+
installs the bridge + CLI on first use) and egress to the npm registry and the
|
|
296
|
+
model API. Provide the API key via the adapter's `auth` option.
|
|
297
|
+
|
|
298
|
+
## Development
|
|
299
|
+
|
|
300
|
+
```bash
|
|
301
|
+
npm install
|
|
302
|
+
npm run typecheck # tsc against the real canary harness types
|
|
303
|
+
npm test # vitest: unit + local integration (fake `coder` + `ssh`)
|
|
304
|
+
npm run build # tsup → dist/ (ESM + d.ts)
|
|
305
|
+
|
|
306
|
+
# Formatting & linting (Biome):
|
|
307
|
+
npm run format # biome format --write . (apply formatting)
|
|
308
|
+
npm run lint # biome lint . (report lint issues)
|
|
309
|
+
npm run check # biome check . (format + lint, read-only; for CI)
|
|
310
|
+
|
|
311
|
+
# End-to-end against a real workspace (needs the coder CLI + a running workspace):
|
|
312
|
+
npm run verify:real -- my-ws
|
|
313
|
+
|
|
314
|
+
# End-to-end of create mode (creates a throwaway workspace, then deletes it):
|
|
315
|
+
npm run verify:create -- docker
|
|
316
|
+
```
|
|
317
|
+
|
|
318
|
+
The local integration tests exercise the real transport (argument building,
|
|
319
|
+
stdin, base64 file round-trips, streaming, the port-forward lifecycle, and the
|
|
320
|
+
create/status/presets JSON paths) against fake `coder`/`ssh` executables that run
|
|
321
|
+
commands locally — no Coder deployment needed. `scripts/verify-real.ts` runs the
|
|
322
|
+
same surface — plus a real WebSocket-over-SSH round-trip — against an actual
|
|
323
|
+
workspace; `scripts/verify-create.ts` creates a throwaway workspace from a
|
|
324
|
+
template, waits for readiness, runs a command in it, and deletes it.
|
|
325
|
+
|
|
326
|
+
## License
|
|
327
|
+
|
|
328
|
+
Apache-2.0
|