@yuva-devlab/ai-client 0.2.0 → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,171 @@
1
+ # 🧠 @yuva-devlab/ai-client
2
+
3
+ [![npm version](https://img.shields.io/npm/v/@yuva-devlab/ai-client.svg)](https://www.npmjs.com/package/@yuva-devlab/ai-client)
4
+ [![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](LICENSE)
5
+ [![TypeScript 6](https://img.shields.io/badge/TypeScript-6.x-3178c6.svg)](https://www.typescriptlang.org)
6
+
7
+ > Unified multi-provider AI client, streaming adapters (Ollama, Groq, Google Gemini, OpenAI, Anthropic), multi-tier fallback cascade, and self-healing structured JSON extraction for the DevLab platform.
8
+
9
+ ---
10
+
11
+ ## Features
12
+
13
+ - 🌐 **Unified Multi-Provider Adapters**: One common interface across Ollama, Groq, Google (Gemini), OpenAI, and Anthropic.
14
+ - 🌊 **First-Class Streaming Support**: Type-safe async generator streams for token-by-token rendering and tool call deltas.
15
+ - 🪜 **Multi-Tier Fallback Cascade**: Automatically route requests across cheaper or local models first, falling back to frontier cloud providers on failure or rate limits.
16
+ - 🩹 **Self-Healing Structured Outputs**: Parse and repair malformed JSON responses using Zod schemas and automatic reflection passes.
17
+ - 🔑 **Centralized Credential Resolution**: Automatic resolution from environment variables or custom secret providers.
18
+ - 📦 **Dual ESM & CJS Build**: Complete support across Node.js services and edge functions.
19
+
20
+ ---
21
+
22
+ ## Table of Contents
23
+
24
+ 1. [Installation](#installation)
25
+ 2. [Quick Start](#quick-start)
26
+ 3. [Provider Adapters](#provider-adapters)
27
+ 4. [Streaming Text & Tool Calls](#streaming-text--tool-calls)
28
+ 5. [Fallback Cascade Engine](#fallback-cascade-engine)
29
+ 6. [Self-Healing Structured JSON](#self-healing-structured-json)
30
+ 7. [Author & Attribution](#author--attribution)
31
+
32
+ ---
33
+
34
+ ## Installation
35
+
36
+ ```bash
37
+ # pnpm (recommended)
38
+ pnpm add @yuva-devlab/ai-client zod
39
+
40
+ # npm
41
+ npm install @yuva-devlab/ai-client zod
42
+ ```
43
+
44
+ ---
45
+
46
+ ## Quick Start
47
+
48
+ Create an AI client for any supported provider:
49
+
50
+ ```typescript
51
+ import { createAiClient } from "@yuva-devlab/ai-client";
52
+
53
+ const client = createAiClient({
54
+ provider: "google",
55
+ apiKey: process.env.GEMINI_API_KEY,
56
+ model: "gemini-2.5-pro",
57
+ });
58
+
59
+ const response = await client.generateText({
60
+ prompt: "Explain how zero-knowledge rollups scale decentralized finance.",
61
+ temperature: 0.2,
62
+ });
63
+
64
+ console.log(response.text);
65
+ ```
66
+
67
+ ---
68
+
69
+ ## Provider Adapters
70
+
71
+ Switch between providers with zero application logic changes:
72
+
73
+ ```typescript
74
+ // 1. Ollama (Local LLM)
75
+ const localClient = createAiClient({
76
+ provider: "ollama",
77
+ baseUrl: "http://localhost:11434",
78
+ model: "llama3.3:70b",
79
+ });
80
+
81
+ // 2. Groq (Ultra-low latency)
82
+ const groqClient = createAiClient({
83
+ provider: "groq",
84
+ apiKey: process.env.GROQ_API_KEY,
85
+ model: "llama-3.3-70b-versatile",
86
+ });
87
+
88
+ // 3. Anthropic
89
+ const claudeClient = createAiClient({
90
+ provider: "anthropic",
91
+ apiKey: process.env.ANTHROPIC_API_KEY,
92
+ model: "claude-3-7-sonnet-20250219",
93
+ });
94
+ ```
95
+
96
+ ---
97
+
98
+ ## Streaming Text & Tool Calls
99
+
100
+ Consume real-time token streams seamlessly:
101
+
102
+ ```typescript
103
+ const stream = await client.streamText({
104
+ prompt: "Generate an executive summary of quarterly earnings.",
105
+ });
106
+
107
+ for await (const chunk of stream) {
108
+ process.stdout.write(chunk.delta);
109
+ }
110
+ ```
111
+
112
+ ---
113
+
114
+ ## Fallback Cascade Engine
115
+
116
+ Define multi-tier reliability strategies to avoid downtime and rate-limiting outages:
117
+
118
+ ```typescript
119
+ import { createCascadeClient } from "@yuva-devlab/ai-client";
120
+
121
+ const cascade = createCascadeClient({
122
+ tiers: [
123
+ // Tier 1: Fast local inference
124
+ { provider: "ollama", model: "llama3.3:70b" },
125
+ // Tier 2: Low-latency cloud inference
126
+ { provider: "groq", model: "llama-3.3-70b-versatile" },
127
+ // Tier 3: Frontier cloud fallback
128
+ { provider: "google", model: "gemini-2.5-flash" },
129
+ ],
130
+ retryCountPerTier: 2,
131
+ backoffMs: 500,
132
+ });
133
+
134
+ const result = await cascade.generateText({
135
+ prompt: "Summarize this critical transaction.",
136
+ });
137
+
138
+ console.log(`Fulfilled by tier provider: ${result.meta.resolvedProvider}`);
139
+ ```
140
+
141
+ ---
142
+
143
+ ## Self-Healing Structured JSON
144
+
145
+ Guarantee valid JSON output validated against a Zod schema:
146
+
147
+ ```typescript
148
+ import { z } from "zod";
149
+
150
+ const RiskAssessmentSchema = z.object({
151
+ score: z.number().min(0).max(100),
152
+ riskLevel: z.enum(["LOW", "MEDIUM", "HIGH", "CRITICAL"]),
153
+ factors: z.array(z.string()),
154
+ });
155
+
156
+ const assessment = await client.generateStructured({
157
+ prompt:
158
+ "Evaluate the volatility risk for high-frequency algorithmic trade #8892.",
159
+ schema: RiskAssessmentSchema,
160
+ schemaName: "RiskAssessment",
161
+ });
162
+
163
+ console.log(assessment.data.riskLevel);
164
+ ```
165
+
166
+ ---
167
+
168
+ ## Author & Attribution
169
+
170
+ - **Author**: Yuva DevLab (`yuva.devlab@gmail.com`)
171
+ - **License**: MIT