File upload processing pipeline with AI-powered image recognition
npm install @techery/asset-visionFile upload processing pipeline with AI-powered image recognition for edge runtimes.
- π Edge Runtime Compatible - Works on Cloudflare Workers, Fastly Compute, and Node.js 20+
- π Plugin Architecture - Extensible storage and queue adapters
- π€ AI Vision Processing - Built-in Gemini Flash integration with structured output
- π Type-Safe Schemas - Zod schemas for AI responses
- β‘ Async Processing - Queue-based background processing
- π― Framework Agnostic - Use with Hono, Express, or any framework
- π Stateless Design - JWT-based slot tracking, no database required
- π¨ Custom Schemas - Define your own AI response structures
``bash`
npm install @techery/asset-vision zod
`typescript
import { AssetVision } from "@techery/asset-vision";
import { foodRecognitionSchema, foodRecognitionPrompt } from "@techery/asset-vision/examples";
const assetVision = new AssetVision({
storage: {
type: "r2",
bucket: env.ASSETS_BUCKET,
publicUrlBase: "https://assets.example.com",
pathPrefix: "uploads/",
},
queue: {
type: "cloudflare-queues",
queue: env.PROCESSING_QUEUE,
},
vision: {
provider: "gemini",
model: "gemini-2.0-flash",
apiKey: env.GOOGLE_API_KEY,
responseSchema: foodRecognitionSchema,
systemPrompt: foodRecognitionPrompt,
},
upload: {
maxFileSizeBytes: 5 1024 1024,
allowedMimeTypes: ["image/jpeg", "image/png", "image/webp"],
},
callbacks: {
onProcessingComplete: async (result) => {
// Save to database, send notification, etc.
console.log("Analysis complete:", result.aiAnalysis);
},
onProcessingError: async (error) => {
console.error("Processing failed:", error);
},
},
}, "https://api.yourapp.com");
`
`typescript
// Backend API endpoint
app.post("/api/upload/slot", async (req, res) => {
const slot = await assetVision.requestSlot({
mimeType: req.body.mimeType,
metadata: { userId: req.user.id },
});
res.json(slot);
// Returns:
// {
// id: "abc123",
// uploadUrl: "https://storage.../signed-url",
// publicUrl: "https://cdn.../file.jpg",
// successUrl: "https://api.../success/jwt-token",
// cancelUrl: "https://api.../cancel/jwt-token",
// expiresAt: "2024-12-19T12:00:00.000Z"
// }
});
`
`typescript
// Frontend: Upload directly to storage
const response = await fetch("/api/upload/slot", {
method: "POST",
body: JSON.stringify({ mimeType: "image/jpeg" }),
});
const slot = await response.json();
// Upload file directly to storage (bypasses backend)
await fetch(slot.uploadUrl, {
method: "PUT",
body: imageFile,
headers: { "Content-Type": "image/jpeg" },
});
// Notify backend that upload succeeded
await fetch(slot.successUrl, { method: "POST" });
`
`typescript
// Cloudflare Worker queue consumer
export default {
async queue(batch, env) {
const assetVision = new AssetVision(config, "https://api.yourapp.com");
for (const message of batch.messages) {
try {
const job = message.body;
await assetVision.processJob(job);
message.ack();
} catch (error) {
console.error("Processing failed:", error);
message.retry();
}
}
},
};
`
``
βββββββββββ βββββββββββ βββββββββββ βββββββββββ βββββββββββ
β Request β ββ> β Upload β ββ> β Success β ββ> β Queue β ββ> β AI β
β Slot β β to β βCallback β β Process β βAnalysis β
β β β Storage β β β β β β β
βββββββββββ βββββββββββ βββββββββββ βββββββββββ βββββββββββ
1. Frontend requests upload slot with signed URL
2. Frontend uploads directly to storage (R2/S3/GCS)
3. Frontend calls success callback URL
4. Backend enqueues processing job
5. Worker processes job with AI vision
6. Results saved and callback triggered
Define your own analysis schema:
`typescript
import { z } from "zod";
const documentSchema = z.object({
documentType: z.enum(["invoice", "receipt", "contract", "id_card"]),
extractedText: z.string(),
confidence: z.number().min(0).max(1),
fields: z.array(z.object({
name: z.string(),
value: z.string(),
confidence: z.number(),
})),
});
const assetVision = new AssetVision({
vision: {
provider: "gemini",
model: "gemini-2.0-flash",
apiKey: env.GOOGLE_API_KEY,
responseSchema: documentSchema,
systemPrompt: "Extract text and fields from this document image.",
},
// ... other config
}, baseUrl);
`
`typescript
import { foodRecognitionSchema, foodRecognitionPrompt } from "@techery/asset-vision/examples";
// Built-in schema for nutritional analysis:
// - Food identification
// - Calorie estimation
// - Macronutrients (protein, carbs, fats)
// - Ingredients/components
// - Barcode detection
// - Image quality assessment
`
Full example in src/examples/cloudflare-worker/index.ts`
- Full Documentation
- API Reference
- Configuration Guide
- Examples
- Stateless: Upload slot state encoded in JWT tokens
- Plugin-based: Easy to extend with new storage/queue providers
- Type-safe: Full TypeScript support with Zod schemas
- Edge-optimized: Works on Cloudflare Workers and similar platforms
- [ ] S3 storage plugin
- [ ] GCS storage plugin
- [ ] BullMQ queue adapter
- [ ] Claude vision processor
- [ ] Retry logic with exponential backoff
- [ ] Multi-image batch processing
- [ ] Streaming AI responses
Contributions welcome! This package is maintained by Techery for internal use but open to community improvements.
Copyright (c) 2024 Techery. All rights reserved.
This software is proprietary and confidential. Unauthorized copying, distribution, or use is strictly prohibited.