MCPcopy Create free account
hub / github.com/TanStack/ai / onFinish

Function onFinish

packages/ai-sandbox/src/middleware.ts:167–200  ·  view source on GitHub ↗
(ctx)

Source from the content-addressed store, hash-verified

165 },
166
167 async onFinish(ctx) {
168 const state = runState.get(ctx)
169 if (!state) return
170 const { handle, ensureCtx } = state
171
172 await state.watcher?.stop()
173
174 const lifecycle = definition.lifecycle
175
176 if (
177 lifecycle?.snapshot === 'after-run' &&
178 handle.capabilities.snapshots &&
179 handle.snapshot
180 ) {
181 const snapshot = await handle.snapshot(`after-run-${ctx.runId}`)
182 const store = ensureCtx.store
183 if (store) {
184 const key = definition.key(ensureCtx)
185 const existing = await store.get(key)
186 if (existing) {
187 await store.upsert({
188 ...existing,
189 latestSnapshotId: snapshot.id,
190 updatedAt: Date.now(),
191 })
192 }
193 }
194 }
195
196 if (lifecycle?.destroyOnComplete) {
197 await definition.destroy(ensureCtx)
198 await definition.hooks?.onDestroy?.()
199 }
200 },
201
202 async onAbort(ctx, _info: AbortInfo) {
203 const state = runState.get(ctx)

Callers

nothing calls this directly

Calls 6

upsertMethod · 0.80
nowMethod · 0.80
getMethod · 0.65
destroyMethod · 0.65
stopMethod · 0.45
snapshotMethod · 0.45

Tested by

no test coverage detected