Open History to see the last commit
1import { toolDefinition } from '@tanstack/ai'
2import { SandboxSnapshotError } from './snapshots'
3import type { SandboxSnapshots } from './snapshot-operations'
4
5export interface CreateSnapshotToolsOptions {
6 threadId: string
7 runId: string
8 createThreadId: () => string
9 tenant?: { userId?: string; orgId?: string }
10 onForked?: (input: {
11 destinationThreadId: string
12 checkpointId: string
13 }) => void | Promise<void>
14}
15
16function field(value: unknown, key: string): unknown {
17 if (value === null || typeof value !== 'object') return undefined
18 return Reflect.get(value, key)
19}
20
21function requiredString(value: unknown, key: string): string {
22 const candidate = field(value, key)
23 if (typeof candidate !== 'string' || candidate.length === 0) {
24 throw new SandboxSnapshotError(
25 'SANDBOX_SNAPSHOT_INVALID_TOOL_INPUT',
26 `Snapshot tool requires a non-empty ${key}`,
27 )
28 }
29 return candidate
30}
31
32function optionalString(value: unknown, key: string): string | undefined {
33 const candidate = field(value, key)
34 if (candidate === undefined) return undefined
35 if (typeof candidate !== 'string' || candidate.length === 0) {
36 throw new SandboxSnapshotError(
37 'SANDBOX_SNAPSHOT_INVALID_TOOL_INPUT',
38 `Snapshot tool ${key} must be a non-empty string when provided`,
39 )
40 }
41 return candidate
42}
43
44function requireIdentifier(value: string, label: string): string {
45 if (value.length === 0) {
46 throw new SandboxSnapshotError(
47 'SANDBOX_SNAPSHOT_INVALID_TOOL_INPUT',
48 `${label} must be a non-empty string`,
49 )
50 }
51 return value
52}
53
54export function createSnapshotTools(
55 snapshots: SandboxSnapshots,
56 options: CreateSnapshotToolsOptions,
57) {
58 const threadId = requireIdentifier(options.threadId, 'threadId')
59 const runId = requireIdentifier(options.runId, 'runId')
60 const createThreadId = options.createThreadId
61 const tenant = options.tenant
62 const onForked = options.onForked
63 if (typeof createThreadId !== 'function') {
64 throw new SandboxSnapshotError(
65 'SANDBOX_SNAPSHOT_INVALID_TOOL_INPUT',
66 'createSnapshotTools requires createThreadId',
67 )
68 }
69
70 const save = toolDefinition({
71 name: 'save_sandbox_snapshot',
72 description:
73 'Save a named checkpoint of the current live sandbox for this thread. Do not pass a thread id.',
74 inputSchema: {
75 type: 'object',
76 properties: {
77 label: {
78 type: 'string',
79 description: 'A short name for this version, such as release-1.',
80 },
81 },
82 required: ['label'],
83 additionalProperties: false,
84 },
85 outputSchema: {
86 type: 'object',
87 properties: {
88 checkpointId: { type: 'string' },
89 label: { type: 'string' },
90 threadId: { type: 'string' },
91 },
92 required: ['checkpointId', 'label', 'threadId'],
93 additionalProperties: false,
94 },
95 }).server(async (input) => {
96 const label = requiredString(input, 'label')
97 const checkpoint = await snapshots.save({
98 threadId,
99 runId,
100 label,
101 ...(tenant === undefined ? {} : { tenant }),
102 })
103 return {
104 checkpointId: checkpoint.id,
105 label: checkpoint.label ?? label,
106 threadId,
107 }
108 })
109
110 const fork = toolDefinition({
111 name: 'fork_sandbox_snapshot',
112 description:
113 'Copy one checkpoint from this thread into a new empty thread. Omit checkpointId to copy the latest checkpoint. Do not pass thread ids.',
114 inputSchema: {
115 type: 'object',
116 properties: {
117 checkpointId: {
118 type: 'string',
119 description:
120 'The checkpoint to copy. When omitted, the latest checkpoint is copied.',
121 },
122 },
123 additionalProperties: false,
124 },
125 outputSchema: {
126 type: 'object',
127 properties: {
128 checkpointId: { type: 'string' },
129 destinationThreadId: { type: 'string' },
130 },
131 required: ['checkpointId', 'destinationThreadId'],
132 additionalProperties: false,
133 },
134 }).server(async (input) => {
135 const suppliedCheckpointId = optionalString(input, 'checkpointId')
136 const checkpointId =
137 suppliedCheckpointId ?? (await snapshots.checkpoints.getHead(threadId))
138 if (checkpointId === null) {
139 throw new SandboxSnapshotError(
140 'SANDBOX_SNAPSHOT_MISSING_CHECKPOINT',
141 'This thread has no checkpoint to fork',
142 )
143 }
144 const destinationThreadId = requireIdentifier(
145 createThreadId(),
146 'destinationThreadId',
147 )
148 const checkpoint = await snapshots.fork({
149 threadId,
150 checkpointId,
151 destinationThreadId,
152 })
153 if (onForked !== undefined) {
154 await onForked({
155 destinationThreadId,
156 checkpointId: checkpoint.id,
157 })
158 }
159 return {
160 checkpointId: checkpoint.id,
161 destinationThreadId,
162 }
163 })
164
165 const readArtifact = toolDefinition({
166 name: 'read_sandbox_snapshot_artifact',
167 description:
168 'Read metadata for one artifact on a checkpoint in this thread. Do not pass a thread id.',
169 inputSchema: {
170 type: 'object',
171 properties: {
172 checkpointId: { type: 'string' },
173 artifactId: { type: 'string' },
174 },
175 required: ['checkpointId', 'artifactId'],
176 additionalProperties: false,
177 },
178 outputSchema: {
179 type: 'object',
180 properties: {
181 artifactId: { type: 'string' },
182 name: { type: 'string' },
183 mimeType: { type: 'string' },
184 size: { type: 'number' },
185 createdAt: { type: 'number' },
186 },
187 required: ['artifactId', 'name', 'mimeType', 'size', 'createdAt'],
188 additionalProperties: false,
189 },
190 }).server(async (input) => {
191 const checkpointId = requiredString(input, 'checkpointId')
192 const artifactId = requiredString(input, 'artifactId')
193 const resolved = await snapshots.readArtifact({
194 threadId,
195 checkpointId,
196 artifactId,
197 })
198 return {
199 artifactId: resolved.artifact.artifactId,
200 name: resolved.artifact.name,
201 mimeType: resolved.artifact.mimeType,
202 size: resolved.artifact.size,
203 createdAt: resolved.artifact.createdAt,
204 }
205 })
206
207 return [save, fork, readArtifact] as const
208}