TryPatch โ
Production-ready error recovery. When a method fails, @trypatch investigates the error immediately at runtime and returns the value the method should have returned, by running your tools or having the AI build it. Sensitive data stays local. If no correct value can be produced, the method rethrows its original error. Fully customizable behavior.
- ๐ On-the-fly recovery: the AI investigates the failed call and returns its value. How recovery works
- ๐ก๏ธ Schema-checked values: a value the AI builds is validated against your
resultSchema(Zod or JSON Schema). Schemas - ๐ Redaction: secrets are replaced with placeholders before anything leaves your process. Redaction
- ๐ง Tools: investigation tools to look around, result tools to recover (retries, fallbacks, cache reads). Tools, MCP servers
- ๐งพ No silent fallbacks: without a correct value, callers get the original error. How recovery works
- ๐ค Providers: OpenAI, Claude, any OpenAI-compatible endpoint, or Cursor Cloud Agents. Providers
Install โ
npm install @ssandir/trypatch zodRequires Node.js 22.12 or later. Zod schemas work with zod ^3.25.76 or ^4.1.8, imported from zod/v4. Schemas
Quick start โ
import { z } from 'zod/v4'
import { trypatch } from '@ssandir/trypatch'
export const QuoteSchema = z.object({
price: z.number().describe('Price in the smallest currency unit, e.g. cents'),
currency: z.string().describe('ISO 4217 currency code'),
})
export type Quote = z.infer<typeof QuoteSchema>
export class PricingService {
@trypatch({
aiInvestigation: {
resultSchema: QuoteSchema,
investigationProvider: {
provider: 'openai',
apiKey: process.env.OPENAI_API_KEY!,
},
},
})
async getQuote (productId: string): Promise<Quote> {
const response = await fetch(`https://pricing.example.com/products/${productId}/quote`)
if (!response.ok) throw new Error(`Pricing API returned ${response.status}`)
// Throws if the upstream changes its response shape
return QuoteSchema.parse(await response.json())
}
}When getQuote fails, the AI works out the Quote it should have returned. The value is validated against QuoteSchema and returned as if getQuote had succeeded; if there's no correct Quote, getQuote rethrows its original error.
Documentation โ
- How recovery works: outcomes, fallbacks, custom investigations, decorator dialects
- Tools: investigation and result tools, the tool loop, the built-in safe toolbox
- MCP servers
- Redaction
- Cancellation: abort signals and timeouts
- Monitoring: callbacks and logging
- Schemas
- Providers
Every option is documented on its type, so your editor shows it on hover. Guides and the API reference are also on trypatch.dev.