@future-explorer/lib 1.0.4
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 +120 -0
- package/dist/GrokAiClient.d.ts +19 -0
- package/dist/GrokAiClient.d.ts.map +1 -0
- package/dist/GrokAiClient.js +52 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +5 -0
- package/dist/types.d.ts +17 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +2 -0
- package/package.json +49 -0
package/README.md
ADDED
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
# Future Explorer Lib
|
|
2
|
+
|
|
3
|
+
Shared utilities and clients for Future Explorer projects.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @future-explorer/lib
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## GrokAiClient
|
|
12
|
+
|
|
13
|
+
AI client for interacting with Grok (xAI) models with structured output support.
|
|
14
|
+
|
|
15
|
+
### Basic Usage
|
|
16
|
+
|
|
17
|
+
```typescript
|
|
18
|
+
import { GrokAiClient } from '@future-explorer/lib';
|
|
19
|
+
|
|
20
|
+
const client = new GrokAiClient({
|
|
21
|
+
apiKey: 'your-api-key', // or set XAI_API_KEY env var
|
|
22
|
+
temperature: 0.1,
|
|
23
|
+
maxTokens: 4096,
|
|
24
|
+
logger: console, // optional
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
interface PersonInfo {
|
|
28
|
+
name: string;
|
|
29
|
+
age: number;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const response = await client.getGenericStructuredResponse<PersonInfo>({
|
|
33
|
+
model: 'grok-2-latest',
|
|
34
|
+
messages: [
|
|
35
|
+
{ role: 'system', content: 'You are a helpful assistant.' },
|
|
36
|
+
{ role: 'user', content: 'Extract data from this text...' }
|
|
37
|
+
],
|
|
38
|
+
tools: [
|
|
39
|
+
{
|
|
40
|
+
type: 'function',
|
|
41
|
+
name: 'extract_info',
|
|
42
|
+
description: 'Extract structured information',
|
|
43
|
+
parameters: {
|
|
44
|
+
type: 'object',
|
|
45
|
+
properties: {
|
|
46
|
+
name: { type: 'string' },
|
|
47
|
+
age: { type: 'number' }
|
|
48
|
+
},
|
|
49
|
+
required: ['name', 'age']
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
],
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
if (response) {
|
|
56
|
+
console.log(response.args); // { name: '...', age: ... }
|
|
57
|
+
console.log(response.functionName); // 'extract_info'
|
|
58
|
+
}
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
### Constructor Options
|
|
62
|
+
|
|
63
|
+
- `apiKey` (optional): xAI API key. Falls back to `XAI_API_KEY` env var
|
|
64
|
+
- `temperature` (optional): Default temperature for requests (default: 0.1)
|
|
65
|
+
- `maxTokens` (optional): Default max tokens (default: 4096)
|
|
66
|
+
- `logger` (optional): Logger instance with `warn` and `error` methods
|
|
67
|
+
|
|
68
|
+
## Development
|
|
69
|
+
|
|
70
|
+
### Build
|
|
71
|
+
|
|
72
|
+
```bash
|
|
73
|
+
npm run build
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
### Watch Mode
|
|
77
|
+
|
|
78
|
+
```bash
|
|
79
|
+
npm run watch
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
### Lint
|
|
83
|
+
|
|
84
|
+
```bash
|
|
85
|
+
npm run lint
|
|
86
|
+
npm run lint:fix
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
### Local Development
|
|
90
|
+
|
|
91
|
+
Link the package locally for testing in other projects:
|
|
92
|
+
|
|
93
|
+
```bash
|
|
94
|
+
./scripts/link-local.sh
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
Then in your project:
|
|
98
|
+
|
|
99
|
+
```bash
|
|
100
|
+
npm link @future-explorer/lib
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
## Publishing
|
|
104
|
+
|
|
105
|
+
### Manual Publish
|
|
106
|
+
|
|
107
|
+
```bash
|
|
108
|
+
npm run build
|
|
109
|
+
npm publish
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
### Using Script
|
|
113
|
+
|
|
114
|
+
```bash
|
|
115
|
+
./scripts/publish.sh [patch|minor|major]
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
## License
|
|
119
|
+
|
|
120
|
+
ISC
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { GetGenericStructuredResponse, GetGenericStructuredResponseArgs } from './types';
|
|
2
|
+
export interface GrokAiClientOptions {
|
|
3
|
+
apiKey?: string;
|
|
4
|
+
temperature?: number;
|
|
5
|
+
maxTokens?: number;
|
|
6
|
+
logger?: {
|
|
7
|
+
warn: (message: string, ...args: any[]) => void;
|
|
8
|
+
error: (message: string, ...args: any[]) => void;
|
|
9
|
+
};
|
|
10
|
+
}
|
|
11
|
+
export declare class GrokAiClient {
|
|
12
|
+
private readonly xaiClient;
|
|
13
|
+
private temperature;
|
|
14
|
+
private maxTokens;
|
|
15
|
+
private logger?;
|
|
16
|
+
constructor(options?: GrokAiClientOptions);
|
|
17
|
+
getGenericStructuredResponse<T>(payload: GetGenericStructuredResponseArgs): Promise<GetGenericStructuredResponse<T> | null>;
|
|
18
|
+
}
|
|
19
|
+
//# sourceMappingURL=GrokAiClient.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"GrokAiClient.d.ts","sourceRoot":"","sources":["../src/GrokAiClient.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,4BAA4B,EAAE,gCAAgC,EAAE,MAAM,SAAS,CAAC;AAEzF,MAAM,WAAW,mBAAmB;IAClC,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,MAAM,CAAC,EAAE;QACP,IAAI,EAAE,CAAC,OAAO,EAAE,MAAM,EAAE,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,IAAI,CAAC;QAChD,KAAK,EAAE,CAAC,OAAO,EAAE,MAAM,EAAE,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,IAAI,CAAC;KAClD,CAAC;CACH;AAED,qBAAa,YAAY;IACvB,OAAO,CAAC,QAAQ,CAAC,SAAS,CAA+B;IACzD,OAAO,CAAC,WAAW,CAAS;IAC5B,OAAO,CAAC,SAAS,CAAS;IAC1B,OAAO,CAAC,MAAM,CAAC,CAAgC;gBAEnC,OAAO,GAAE,mBAAwB;IAahC,4BAA4B,CAAC,CAAC,EACzC,OAAO,EAAE,gCAAgC,GACxC,OAAO,CAAC,4BAA4B,CAAC,CAAC,CAAC,GAAG,IAAI,CAAC;CA2CnD"}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.GrokAiClient = void 0;
|
|
4
|
+
const xai_1 = require("@ai-sdk/xai");
|
|
5
|
+
class GrokAiClient {
|
|
6
|
+
xaiClient;
|
|
7
|
+
temperature;
|
|
8
|
+
maxTokens;
|
|
9
|
+
logger;
|
|
10
|
+
constructor(options = {}) {
|
|
11
|
+
const key = options.apiKey || process.env.XAI_API_KEY;
|
|
12
|
+
if (!key) {
|
|
13
|
+
throw new Error('XAI_API_KEY is required (env var or constructor arg)');
|
|
14
|
+
}
|
|
15
|
+
this.xaiClient = (0, xai_1.createXai)({ apiKey: key });
|
|
16
|
+
this.temperature = options.temperature ?? 0.1;
|
|
17
|
+
this.maxTokens = options.maxTokens ?? 4096;
|
|
18
|
+
this.logger = options.logger;
|
|
19
|
+
}
|
|
20
|
+
async getGenericStructuredResponse(payload) {
|
|
21
|
+
const { model, messages, tools, toolChoice = { type: 'tool', toolName: 'auto' }, temperature = this.temperature, maxOutputTokens = this.maxTokens, } = payload;
|
|
22
|
+
try {
|
|
23
|
+
const result = await this.xaiClient.chat(model).doGenerate({
|
|
24
|
+
prompt: messages,
|
|
25
|
+
tools,
|
|
26
|
+
toolChoice,
|
|
27
|
+
temperature,
|
|
28
|
+
maxOutputTokens,
|
|
29
|
+
});
|
|
30
|
+
const toolsCallResult = result.content.find((item) => item.type === 'tool-call');
|
|
31
|
+
if (!toolsCallResult?.input) {
|
|
32
|
+
this.logger?.warn('The model did not call the function.');
|
|
33
|
+
return null;
|
|
34
|
+
}
|
|
35
|
+
const { input, toolName } = toolsCallResult;
|
|
36
|
+
try {
|
|
37
|
+
return {
|
|
38
|
+
args: JSON.parse(input),
|
|
39
|
+
functionName: toolName,
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
catch (e) {
|
|
43
|
+
throw new Error(`Failed to parse response as JSON: ${e}`);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
catch (err) {
|
|
47
|
+
this.logger?.error('GrokAiClient error:', err);
|
|
48
|
+
throw err;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
exports.GrokAiClient = GrokAiClient;
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,mBAAmB,EAAE,MAAM,gBAAgB,CAAC;AACnE,OAAO,EAAE,4BAA4B,EAAE,gCAAgC,EAAE,MAAM,SAAS,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.GrokAiClient = void 0;
|
|
4
|
+
var GrokAiClient_1 = require("./GrokAiClient");
|
|
5
|
+
Object.defineProperty(exports, "GrokAiClient", { enumerable: true, get: function () { return GrokAiClient_1.GrokAiClient; } });
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { LanguageModelV2Message } from '@ai-sdk/provider';
|
|
2
|
+
export interface GetGenericStructuredResponseArgs {
|
|
3
|
+
model?: string;
|
|
4
|
+
messages: LanguageModelV2Message[];
|
|
5
|
+
tools?: any[];
|
|
6
|
+
toolChoice?: {
|
|
7
|
+
type: 'tool';
|
|
8
|
+
toolName: 'auto' | 'required' | string;
|
|
9
|
+
};
|
|
10
|
+
temperature?: number;
|
|
11
|
+
maxOutputTokens?: number;
|
|
12
|
+
}
|
|
13
|
+
export interface GetGenericStructuredResponse<T> {
|
|
14
|
+
args: T | null;
|
|
15
|
+
functionName: string;
|
|
16
|
+
}
|
|
17
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,sBAAsB,EAAE,MAAM,kBAAkB,CAAC;AAE1D,MAAM,WAAW,gCAAgC;IAC/C,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,QAAQ,EAAE,sBAAsB,EAAE,CAAC;IACnC,KAAK,CAAC,EAAE,GAAG,EAAE,CAAC;IACd,UAAU,CAAC,EAAE;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,QAAQ,EAAE,MAAM,GAAG,UAAU,GAAG,MAAM,CAAA;KAAE,CAAC;IACtE,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,eAAe,CAAC,EAAE,MAAM,CAAC;CAC1B;AAED,MAAM,WAAW,4BAA4B,CAAC,CAAC;IAC7C,IAAI,EAAE,CAAC,GAAG,IAAI,CAAC;IACf,YAAY,EAAE,MAAM,CAAC;CACtB"}
|
package/dist/types.js
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@future-explorer/lib",
|
|
3
|
+
"version": "1.0.4",
|
|
4
|
+
"description": "Shared utilities and clients for Future Explorer projects",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"types": "dist/index.d.ts",
|
|
7
|
+
"files": [
|
|
8
|
+
"dist"
|
|
9
|
+
],
|
|
10
|
+
"scripts": {
|
|
11
|
+
"build": "rimraf ./dist && tsc",
|
|
12
|
+
"prepublishOnly": "npm run build",
|
|
13
|
+
"watch": "tsc --watch",
|
|
14
|
+
"lint": "eslint src --ext .ts",
|
|
15
|
+
"lint:fix": "eslint src --ext .ts --fix"
|
|
16
|
+
},
|
|
17
|
+
"keywords": [
|
|
18
|
+
"future-explorer",
|
|
19
|
+
"grok",
|
|
20
|
+
"ai-client"
|
|
21
|
+
],
|
|
22
|
+
"author": "",
|
|
23
|
+
"license": "ISC",
|
|
24
|
+
"publishConfig": {
|
|
25
|
+
"access": "public"
|
|
26
|
+
},
|
|
27
|
+
"repository": {
|
|
28
|
+
"type": "git",
|
|
29
|
+
"url": "https://github.com/future-explorer/future-explorer.git",
|
|
30
|
+
"directory": "future-explorer-lib"
|
|
31
|
+
},
|
|
32
|
+
"peerDependencies": {
|
|
33
|
+
"@ai-sdk/provider": "^2.0.0",
|
|
34
|
+
"@ai-sdk/xai": "^2.0.0"
|
|
35
|
+
},
|
|
36
|
+
"devDependencies": {
|
|
37
|
+
"@ai-sdk/provider": "^2.0.0",
|
|
38
|
+
"@ai-sdk/xai": "^2.0.31",
|
|
39
|
+
"@types/node": "^24.5.2",
|
|
40
|
+
"@typescript-eslint/eslint-plugin": "^8.0.0",
|
|
41
|
+
"@typescript-eslint/parser": "^8.0.0",
|
|
42
|
+
"eslint": "^9.20.1",
|
|
43
|
+
"eslint-config-prettier": "^10.0.1",
|
|
44
|
+
"eslint-plugin-prettier": "^5.2.3",
|
|
45
|
+
"prettier": "^3.5.1",
|
|
46
|
+
"rimraf": "^6.0.1",
|
|
47
|
+
"typescript": "^5.6.3"
|
|
48
|
+
}
|
|
49
|
+
}
|