Skip to content
Source
Sign inSign up
TanStack/ai
Open History to see the last commit

workspace.ts

151 lines4.7 KB

1import type { SetupInput } from './setup-plan'
2import type { BearerRef, SecretRef, Secrets } from './secrets'
3​
4/**
5 * Workspace definition — the portable description of what the agent sees
6 * inside the sandbox. Each harness adapter PROJECTS this into its own native
7 * format via `projectWorkspace()` (e.g. Claude Code → CLAUDE.md + .claude/skills
8 * + --mcp-config). The definition itself is provider- and harness-agnostic.
9 */
10​
11/** Where the working tree comes from. */
12export type WorkspaceSource =
13 | {
14 type: 'git'
15 url: string
16 ref?: string
17 auth?: { username?: string; token: string }
18 /**
19 * Clone depth. Defaults to `1` (shallow). Pass a number for a specific
20 * depth, or `'full'` to fetch the entire history.
21 */
22 depth?: number | 'full'
23 }
24 | { type: 'local'; path: string }
25 | { type: 'none' }
26​
27/** Clone a git repo into the workspace. `githubRepo` is a convenience wrapper. */
28export function gitSource(input: {
29 url: string
30 ref?: string
31 auth?: { username?: string; token: string }
32 depth?: number | 'full'
33}): WorkspaceSource {
34 return { type: 'git', ...input }
36​
37export function githubRepo(input: {
38 repo: string
39 ref?: string
40 auth?: { username?: string; token: string }
41 depth?: number | 'full'
42}): WorkspaceSource {
43 const url = input.repo.startsWith('http')
44 ? input.repo
45 : `https://github.com/${input.repo}.git`
46 return {
47 type: 'git',
48 url,
49 ref: input.ref,
50 auth: input.auth,
51 depth: input.depth,
52 }
54​
55export function localSource(path: string): WorkspaceSource {
56 return { type: 'local', path }
58​
59/**
60 * An MCP server config where header names/values may be plain strings or
61 * unresolved SecretRef values. Secrets are resolved by each harness projector
62 * at projection time — never at definition time.
63 */
64export type McpConfig = {
65 headers?: Record<string, string | SecretRef | BearerRef>
66 [key: string]: unknown
68​
69/** A unit of agent guidance/config projected into the harness's native format. */
70export type WorkspaceSkill =
71 | { kind: 'file'; path: string; content: string }
72 | { kind: 'agent-skill'; name: string }
73 | { kind: 'mcp'; name: string; config: McpConfig }
74 | {
75 kind: 'git'
76 /** Short `owner/repo` or a full HTTPS URL. */
77 repo: string
78 /** Optional SecretRef for private-repo authentication. */
79 secret?: SecretRef
80 /** Absolute path inside the sandbox to clone into. Defaults to a `.tanstack-skills/<repo>` dir under the workspace root. */
81 into?: string
82 }
83​
84/** Write a file (e.g. CLAUDE.md) into the workspace / harness config. */
85export function fileSkill(input: {
86 path: string
87 content: string
88}): WorkspaceSkill {
89 return { kind: 'file', ...input }
91​
92/** Reference a named agent skill the harness should load. */
93export function agentSkill(name: string): WorkspaceSkill {
94 return { kind: 'agent-skill', name }
96​
97/** Project an MCP server into the harness. Header values may be SecretRefs. */
98export function mcpSkill(name: string, config: McpConfig): WorkspaceSkill {
99 return { kind: 'mcp', name, config }
101​
102/**
103 * Clone a git repository as a workspace skill (e.g. a private skill repo).
104 * The clone is performed during bootstrap; `secret` is resolved from the
105 * workspace `secrets` registry at that time.
106 */
107export function gitSkill(input: {
108 repo: string
109 secret?: SecretRef
110 into?: string
111}): WorkspaceSkill {
112 return { kind: 'git', ...input }
114​
115export type PackageManager = 'npm' | 'pnpm' | 'yarn' | 'bun' | 'auto'
116​
117export interface WorkspaceDefinition {
118 source: WorkspaceSource
119 /** Defaults to `'auto'` — detect from the lockfile after the source lands. */
120 packageManager?: PackageManager
121 /** Commands run once during bootstrap. Accepts a string array (serial) or a builder function for serial/parallel groups. */
122 setup?: SetupInput
123 /** Named commands the agent/user can invoke (e.g. { test: 'pnpm test' }). */
124 scripts?: Record<string, string>
125 /** Guidance/config projected into the harness. */
126 skills?: Array<WorkspaceSkill>
127 /**
128 * Natural-language instructions written to AGENTS.md (and symlinked as
129 * CLAUDE.md, GEMINI.md, etc.) inside the sandbox during bootstrap.
130 */
131 instructions?: string
132 /**
133 * Harness plugin identifiers installed idempotently by each harness
134 * projector (e.g. `['@anthropic/plugin-foo']` for Claude Code).
135 */
136 plugins?: Array<string>
137 /**
138 * Typed secret references. The underlying values are injected into the
139 * sandbox env at create/resume — NEVER written to snapshots, the
140 * SandboxInstanceStore, or the event log.
141 */
142 secrets?: Secrets
143 /** Workspace root inside the sandbox. Defaults to `/workspace`. */
144 root?: string
146​
147export function defineWorkspace(
148 definition: WorkspaceDefinition,
149): WorkspaceDefinition {
150 return definition