@future-explorer/lib 1.0.5 → 1.0.7

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 CHANGED
@@ -33,7 +33,7 @@ const response = await client.getGenericStructuredResponse<PersonInfo>({
33
33
  model: 'grok-2-latest',
34
34
  messages: [
35
35
  { role: 'system', content: 'You are a helpful assistant.' },
36
- { role: 'user', content: 'Extract data from this text...' }
36
+ { role: 'user', content: 'Extract data from this text...' },
37
37
  ],
38
38
  tools: [
39
39
  {
@@ -44,11 +44,11 @@ const response = await client.getGenericStructuredResponse<PersonInfo>({
44
44
  type: 'object',
45
45
  properties: {
46
46
  name: { type: 'string' },
47
- age: { type: 'number' }
47
+ age: { type: 'number' },
48
48
  },
49
- required: ['name', 'age']
50
- }
51
- }
49
+ required: ['name', 'age'],
50
+ },
51
+ },
52
52
  ],
53
53
  });
54
54
 
@@ -65,6 +65,52 @@ if (response) {
65
65
  - `maxTokens` (optional): Default max tokens (default: 4096)
66
66
  - `logger` (optional): Logger instance with `warn` and `error` methods
67
67
 
68
+ ## UnifiedAiClient
69
+
70
+ Multi-provider AI client supporting OpenAI, XAI (Grok), and Google Gemini with Zod schema-based structured outputs.
71
+
72
+ ### Basic Usage
73
+
74
+ ```typescript
75
+ import { UnifiedAiClient, Provider } from '@future-explorer/lib';
76
+ import { z } from 'zod';
77
+
78
+ // Create client with desired provider
79
+ const client = new UnifiedAiClient(Provider.OpenAI);
80
+ // or Provider.XAI, Provider.Gemini
81
+
82
+ // Define a Zod schema for the response
83
+ const SentimentSchema = z.object({
84
+ sentiment: z.enum(['positive', 'negative', 'neutral']),
85
+ confidence: z.number().min(0).max(1),
86
+ summary: z.string(),
87
+ });
88
+
89
+ // Generate structured response
90
+ const result = await client.generateStructuredResponse(
91
+ SentimentSchema,
92
+ 'I absolutely love this product!',
93
+ 'You are a sentiment analysis expert.'
94
+ );
95
+
96
+ console.log(result.sentiment); // 'positive'
97
+ console.log(result.confidence); // 0.95
98
+ console.log(result.summary); // '...'
99
+ ```
100
+
101
+ ### Providers
102
+
103
+ | Provider | Enum Value | Required Environment Variables |
104
+ | ------------- | ----------------- | ---------------------------------------------- |
105
+ | OpenAI | `Provider.OpenAI` | `OPENAI_API_KEY`, `MODEL_OPEN_AI` |
106
+ | XAI (Grok) | `Provider.XAI` | `XAI_API_KEY`, `MODEL_XAI` |
107
+ | Google Gemini | `Provider.Gemini` | `GOOGLE_GENERATIVE_AI_API_KEY`, `MODEL_GEMINI` |
108
+
109
+ ### Methods
110
+
111
+ - `generateStructuredResponse<T>(schema, userPrompt, systemMessage?)`: Generates a structured response matching the Zod schema
112
+ - `getModel()`: Returns the underlying LanguageModel instance
113
+
68
114
  ## Development
69
115
 
70
116
  ### Build
@@ -115,6 +161,16 @@ npm publish
115
161
  ./scripts/publish.sh [patch|minor|major]
116
162
  ```
117
163
 
164
+ ## Changelog
165
+
166
+ ### 1.0.7
167
+
168
+ - Updated peer dependency to zod 4.2.x
169
+
170
+ ### 1.0.6
171
+
172
+ - Added UnifiedAiClient with multi-provider support (OpenAI, XAI, Gemini)
173
+
118
174
  ## License
119
175
 
120
- ISC
176
+ ISC
@@ -0,0 +1,3 @@
1
+ export * from './unified-ai-client';
2
+ export * from './unified-ai-client.model';
3
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../../src/clients/ai/clients/unified-ai-sdk/index.ts"],"names":[],"mappings":"AAAA,cAAc,qBAAqB,CAAC;AACpC,cAAc,2BAA2B,CAAC"}
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./unified-ai-client"), exports);
18
+ __exportStar(require("./unified-ai-client.model"), exports);
@@ -0,0 +1,53 @@
1
+ import { LanguageModel } from 'ai';
2
+ import { ZodSchema } from 'zod';
3
+ import { Provider } from './unified-ai-client.model';
4
+ /**
5
+ * Unified AI Client that supports multiple AI providers (OpenAI, XAI, Gemini)
6
+ * and allows generating structured responses based on Zod schemas.
7
+ */
8
+ export declare class UnifiedAiClient {
9
+ private model;
10
+ constructor(provider: Provider);
11
+ /**
12
+ * Generates a structured response from the AI model based on a Zod schema.
13
+ *
14
+ * @template T - The type inferred from the Zod schema
15
+ *
16
+ * @param schema - Zod schema defining the expected response structure.
17
+ * The AI model will be constrained to return data matching this schema.
18
+ * @param prompt - User input/message to send to the AI model (role: user)
19
+ * @param system - Optional system instructions that define the AI's behavior,
20
+ * persona, and task context (role: system)
21
+ *
22
+ * @returns Promise resolving to the validated object matching the schema type T
23
+ *
24
+ * @throws {Error} If the AI provider fails to generate a valid response
25
+ * @throws {Error} If the response doesn't match the provided schema
26
+ *
27
+ * @example
28
+ * // Define a schema
29
+ * const SentimentSchema = z.object({
30
+ * sentiment: z.enum(['positive', 'negative', 'neutral']),
31
+ * confidence: z.number().min(0).max(1),
32
+ * summary: z.string(),
33
+ * });
34
+ *
35
+ * // Generate structured response
36
+ * const result = await service.generateStructuredResponse(
37
+ * SentimentSchema,
38
+ * 'I absolutely love this product!',
39
+ * 'You are a sentiment analysis expert. Be precise with confidence scores.'
40
+ * );
41
+ *
42
+ * console.log(result.sentiment); // 'positive'
43
+ * console.log(result.confidence); // 0.95
44
+ */
45
+ generateStructuredResponse<T>(schema: ZodSchema<T>, userPrompt: string, systemMessage?: string): Promise<T>;
46
+ /**
47
+ * Retrieves the underlying LanguageModel instance.
48
+ * @returns The LanguageModel instance used by the UnifiedAiClient.
49
+ */
50
+ getModel(): LanguageModel;
51
+ private createModel;
52
+ }
53
+ //# sourceMappingURL=unified-ai-client.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"unified-ai-client.d.ts","sourceRoot":"","sources":["../../../../../src/clients/ai/clients/unified-ai-sdk/unified-ai-client.ts"],"names":[],"mappings":"AAGA,OAAO,EAAkB,aAAa,EAAE,MAAM,IAAI,CAAC;AACnD,OAAO,EAAE,SAAS,EAAE,MAAM,KAAK,CAAC;AAChC,OAAO,EAAE,QAAQ,EAAE,MAAM,2BAA2B,CAAC;AAErD;;;GAGG;AACH,qBAAa,eAAe;IAC1B,OAAO,CAAC,KAAK,CAAgB;gBAEjB,QAAQ,EAAE,QAAQ;IAI9B;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OAiCG;IACG,0BAA0B,CAAC,CAAC,EAChC,MAAM,EAAE,SAAS,CAAC,CAAC,CAAC,EACpB,UAAU,EAAE,MAAM,EAClB,aAAa,CAAC,EAAE,MAAM,GACrB,OAAO,CAAC,CAAC,CAAC;IAWb;;;OAGG;IACH,QAAQ,IAAI,aAAa;IAIzB,OAAO,CAAC,WAAW;CAYpB"}
@@ -0,0 +1,81 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.UnifiedAiClient = void 0;
4
+ const google_1 = require("@ai-sdk/google");
5
+ const openai_1 = require("@ai-sdk/openai");
6
+ const xai_1 = require("@ai-sdk/xai");
7
+ const ai_1 = require("ai");
8
+ const unified_ai_client_model_1 = require("./unified-ai-client.model");
9
+ /**
10
+ * Unified AI Client that supports multiple AI providers (OpenAI, XAI, Gemini)
11
+ * and allows generating structured responses based on Zod schemas.
12
+ */
13
+ class UnifiedAiClient {
14
+ model;
15
+ constructor(provider) {
16
+ this.model = this.createModel(provider);
17
+ }
18
+ /**
19
+ * Generates a structured response from the AI model based on a Zod schema.
20
+ *
21
+ * @template T - The type inferred from the Zod schema
22
+ *
23
+ * @param schema - Zod schema defining the expected response structure.
24
+ * The AI model will be constrained to return data matching this schema.
25
+ * @param prompt - User input/message to send to the AI model (role: user)
26
+ * @param system - Optional system instructions that define the AI's behavior,
27
+ * persona, and task context (role: system)
28
+ *
29
+ * @returns Promise resolving to the validated object matching the schema type T
30
+ *
31
+ * @throws {Error} If the AI provider fails to generate a valid response
32
+ * @throws {Error} If the response doesn't match the provided schema
33
+ *
34
+ * @example
35
+ * // Define a schema
36
+ * const SentimentSchema = z.object({
37
+ * sentiment: z.enum(['positive', 'negative', 'neutral']),
38
+ * confidence: z.number().min(0).max(1),
39
+ * summary: z.string(),
40
+ * });
41
+ *
42
+ * // Generate structured response
43
+ * const result = await service.generateStructuredResponse(
44
+ * SentimentSchema,
45
+ * 'I absolutely love this product!',
46
+ * 'You are a sentiment analysis expert. Be precise with confidence scores.'
47
+ * );
48
+ *
49
+ * console.log(result.sentiment); // 'positive'
50
+ * console.log(result.confidence); // 0.95
51
+ */
52
+ async generateStructuredResponse(schema, userPrompt, systemMessage) {
53
+ const result = await (0, ai_1.generateObject)({
54
+ model: this.model,
55
+ schema: schema, // Option 2: Use jsonSchema instead of Zod directly: schema: jsonSchema<T>(zodToJsonSchema(schema) as any),
56
+ prompt: userPrompt,
57
+ system: systemMessage,
58
+ });
59
+ return result.object;
60
+ }
61
+ /**
62
+ * Retrieves the underlying LanguageModel instance.
63
+ * @returns The LanguageModel instance used by the UnifiedAiClient.
64
+ */
65
+ getModel() {
66
+ return this.model;
67
+ }
68
+ createModel(provider) {
69
+ switch (provider) {
70
+ case unified_ai_client_model_1.Provider.OpenAI:
71
+ return (0, openai_1.openai)(process.env.MODEL_OPEN_AI);
72
+ case unified_ai_client_model_1.Provider.XAI:
73
+ return (0, xai_1.xai)(process.env.MODEL_XAI);
74
+ case unified_ai_client_model_1.Provider.Gemini:
75
+ return (0, google_1.google)(process.env.MODEL_GEMINI);
76
+ default:
77
+ throw new Error(`Unknown provider: ${provider}`);
78
+ }
79
+ }
80
+ }
81
+ exports.UnifiedAiClient = UnifiedAiClient;
@@ -0,0 +1,18 @@
1
+ /**
2
+ * Enum representing supported AI providers.
3
+ */
4
+ export declare enum Provider {
5
+ /**
6
+ * Expects 'OPENAI_API_KEY' and 'MODEL_OPEN_AI' environment variables to be set.
7
+ */
8
+ OpenAI = "openai",
9
+ /**
10
+ * Expects 'XAI_API_KEY' and 'MODEL_XAI' environment variables to be set.
11
+ */
12
+ XAI = "xai",
13
+ /**
14
+ * Expects 'GOOGLE_GENERATIVE_AI_API_KEY' and 'MODEL_GEMINI' environment variables to be set.
15
+ */
16
+ Gemini = "gemini"
17
+ }
18
+ //# sourceMappingURL=unified-ai-client.model.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"unified-ai-client.model.d.ts","sourceRoot":"","sources":["../../../../../src/clients/ai/clients/unified-ai-sdk/unified-ai-client.model.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,oBAAY,QAAQ;IAClB;;OAEG;IACH,MAAM,WAAW;IACjB;;OAEG;IACH,GAAG,QAAQ;IACX;;OAEG;IACH,MAAM,WAAW;CAClB"}
@@ -0,0 +1,21 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Provider = void 0;
4
+ /**
5
+ * Enum representing supported AI providers.
6
+ */
7
+ var Provider;
8
+ (function (Provider) {
9
+ /**
10
+ * Expects 'OPENAI_API_KEY' and 'MODEL_OPEN_AI' environment variables to be set.
11
+ */
12
+ Provider["OpenAI"] = "openai";
13
+ /**
14
+ * Expects 'XAI_API_KEY' and 'MODEL_XAI' environment variables to be set.
15
+ */
16
+ Provider["XAI"] = "xai";
17
+ /**
18
+ * Expects 'GOOGLE_GENERATIVE_AI_API_KEY' and 'MODEL_GEMINI' environment variables to be set.
19
+ */
20
+ Provider["Gemini"] = "gemini";
21
+ })(Provider || (exports.Provider = Provider = {}));
@@ -1,3 +1,4 @@
1
+ export * from './clients/unified-ai-sdk/unified-ai-client';
1
2
  export * from './grok';
2
3
  export * from './shared';
3
4
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/clients/ai/index.ts"],"names":[],"mappings":"AAAA,cAAc,QAAQ,CAAC;AACvB,cAAc,UAAU,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/clients/ai/index.ts"],"names":[],"mappings":"AAAA,cAAc,4CAA4C,CAAC;AAC3D,cAAc,QAAQ,CAAC;AACvB,cAAc,UAAU,CAAC"}
@@ -14,5 +14,6 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
14
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./clients/unified-ai-sdk/unified-ai-client"), exports);
17
18
  __exportStar(require("./grok"), exports);
18
19
  __exportStar(require("./shared"), exports);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@future-explorer/lib",
3
- "version": "1.0.5",
3
+ "version": "1.0.7",
4
4
  "description": "Shared utilities and clients for Future Explorer projects",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -16,10 +16,23 @@
16
16
  },
17
17
  "keywords": [
18
18
  "future-explorer",
19
+ "ai-sdk",
20
+ "openai",
21
+ "gemini",
19
22
  "grok",
20
- "ai-client"
23
+ "xai",
24
+ "unified-ai"
25
+ ],
26
+ "contributors": [
27
+ {
28
+ "name": "Alexander Grigoryan",
29
+ "email": "agrigoryan1982@gmail.com"
30
+ },
31
+ {
32
+ "name": "Renat Gatin",
33
+ "email": "renat.gatin@gmail.com"
34
+ }
21
35
  ],
22
- "author": "",
23
36
  "license": "ISC",
24
37
  "publishConfig": {
25
38
  "access": "public"
@@ -29,17 +42,19 @@
29
42
  "url": "https://github.com/future-explorer/future-explorer.git",
30
43
  "directory": "future-explorer-lib"
31
44
  },
45
+ "dependencies": {
46
+ "@ai-sdk/google": "^2.0.52",
47
+ "@ai-sdk/openai": "^2.0.89",
48
+ "@ai-sdk/xai": "^2.0.31",
49
+ "ai": "^5.0.92"
50
+ },
32
51
  "peerDependencies": {
33
- "@ai-sdk/provider": "^2.0.0",
34
- "@ai-sdk/xai": "^2.0.0"
52
+ "zod": "^4.2.0"
35
53
  },
36
54
  "devDependencies": {
37
- "@ai-sdk/provider": "^2.0.0",
38
- "@ai-sdk/xai": "^2.0.31",
39
55
  "@types/node": "^24.5.2",
40
56
  "@typescript-eslint/eslint-plugin": "^8.0.0",
41
57
  "@typescript-eslint/parser": "^8.0.0",
42
- "ai": "^5.0.92",
43
58
  "eslint": "^9.20.1",
44
59
  "eslint-config-prettier": "^10.0.1",
45
60
  "eslint-plugin-prettier": "^5.2.3",