-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclaude-code.ts
More file actions
61 lines (57 loc) · 2.37 KB
/
Copy pathclaude-code.ts
File metadata and controls
61 lines (57 loc) · 2.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/**
* Run Claude Code inside a Coder workspace via the AI SDK HarnessAgent.
*
* Prerequisites:
* - The `coder` CLI on PATH, logged in (`coder login`) — or pass `url`/`token`
* to `new CoderCliTransport({ url, token })`.
* - A running workspace whose image has Node.js and pnpm (`corepack enable`),
* since the bridge installs the Claude Code CLI + its SDK via pnpm on first
* use, plus outbound access to the npm registry and api.anthropic.com.
* - `ANTHROPIC_API_KEY` available to the bridge (configure via the adapter's
* `auth`, or ensure it is present in the workspace environment).
*
* Usage:
* CODER_WORKSPACE=my-dev-ws npx tsx examples/claude-code.ts "Summarize this repo"
*/
import { HarnessAgent } from "@ai-sdk/harness/agent";
import { createClaudeCode } from "@ai-sdk/harness-claude-code";
import { createCoderWorkspace } from "../src/index.js";
async function main(): Promise<void> {
const workspace = process.env.CODER_WORKSPACE;
if (!workspace) {
throw new Error("Set CODER_WORKSPACE to the workspace to use, e.g. CODER_WORKSPACE=my-dev-ws");
}
const agent = new HarnessAgent({
harness: createClaudeCode({
// model: 'claude-opus-4-8',
thinking: { type: "adaptive" },
// `port` defaults to the first port the sandbox exposes (4000 below).
}),
sandbox: createCoderWorkspace({
workspace,
// ports: [4000], // the bridge binds ports[0]; getPortUrl forwards it
// ownsLifecycle: false, // wrap an existing workspace (default); stop/destroy are no-ops
// ensureStarted: true, // run `coder start` first if it may be stopped
// url/token are not options here — they live on the transport. Import
// CoderCliTransport from '@coder/ai-sdk-sandbox' and pass:
// transport: new CoderCliTransport({ url: process.env.CODER_URL, token: process.env.CODER_SESSION_TOKEN }),
}),
instructions: "You are a careful coding assistant. Prefer small, well-explained changes.",
});
const session = await agent.createSession();
try {
const result = await agent.generate({
session,
prompt:
process.argv[2] ??
"Create a short TODO.md in the repository root with three improvement ideas.",
});
console.log(result.text);
} finally {
await session.destroy();
}
}
main().catch((error) => {
console.error(error);
process.exitCode = 1;
});