-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclaude-code-tui.ts
More file actions
74 lines (69 loc) · 2.66 KB
/
Copy pathclaude-code-tui.ts
File metadata and controls
74 lines (69 loc) · 2.66 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
62
63
64
65
66
67
68
69
70
71
72
73
74
/**
* Interactive terminal UI (TUI) for Claude Code running in a Coder workspace.
*
* This is examples/claude-code.ts with the AI SDK terminal UI on top: instead of
* a single scripted prompt, you get an interactive chat in your terminal (exit
* with Esc or Ctrl+C).
*
* Prerequisites (same as examples/claude-code.ts):
* - The `coder` CLI on PATH, logged in (`coder login`).
* - 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 (via the adapter's `auth`, or
* present in the workspace environment).
*
* Install the TUI package alongside the harness:
* npm add @ai-sdk/tui
*
* Usage:
* CODER_WORKSPACE=my-dev-ws npx tsx examples/claude-code-tui.ts
*/
import { HarnessAgent, type HarnessAgentSession } from "@ai-sdk/harness/agent";
import { createClaudeCode } from "@ai-sdk/harness-claude-code";
import { type AgentTUIAgent, runAgentTUI } from "@ai-sdk/tui";
import { createCoderWorkspace } from "../src/index.js";
/**
* Adapt a {@link HarnessAgent} — whose `generate`/`stream` need a session — into
* the session-less {@link AgentTUIAgent} the terminal UI drives, by injecting the
* session for the lifetime of the TUI.
*/
function toTUIAgent(agent: HarnessAgent, session: HarnessAgentSession): AgentTUIAgent {
return {
version: "agent-v1",
id: agent.id,
tools: agent.tools,
generate: (request) => agent.generate({ ...request, session }),
stream: (request) => agent.stream({ ...request, session }),
};
}
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({ thinking: { type: "adaptive" } }),
sandbox: createCoderWorkspace({ workspace }),
// To create a fresh workspace from a template instead of wrapping one:
// sandbox: createCoderWorkspace({
// create: { template: 'claude-code-test', useParameterDefaults: true },
// }),
instructions: "You are a careful coding assistant. Prefer small, well-explained changes.",
});
const session = await agent.createSession();
try {
await runAgentTUI({
title: "Claude Code @ Coder",
agent: toTUIAgent(agent, session),
tools: "auto-collapsed",
reasoning: "collapsed",
});
} finally {
await session.destroy();
}
}
main().catch((error) => {
console.error(error);
process.exitCode = 1;
});