@chainai/core 1.0.0 → 1.0.2
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 +3 -3
- package/dist/client.d.ts +12 -1
- package/dist/client.js +31 -0
- package/dist/index.d.ts +2 -2
- package/dist/index.js +1 -1
- package/dist/services/ic-service.js +15 -1
- package/dist/services/nlp-service.js +18 -1
- package/dist/types/chat.d.ts +32 -0
- package/dist/types/index.d.ts +21 -2
- package/dist/types/index.js +1 -1
- package/package.json +1 -2
package/README.md
CHANGED
|
@@ -1,17 +1,17 @@
|
|
|
1
|
-
# @
|
|
1
|
+
# @chainai/core
|
|
2
2
|
|
|
3
3
|
Core client library for Chain AI - Natural language blockchain assistant powered by the Internet Computer.
|
|
4
4
|
|
|
5
5
|
## Installation
|
|
6
6
|
|
|
7
7
|
```bash
|
|
8
|
-
npm install @
|
|
8
|
+
npm install @chainai/core
|
|
9
9
|
```
|
|
10
10
|
|
|
11
11
|
## Quick Start
|
|
12
12
|
|
|
13
13
|
```typescript
|
|
14
|
-
import { ChainAIClient } from '@
|
|
14
|
+
import { ChainAIClient } from '@chainai/core';
|
|
15
15
|
|
|
16
16
|
// Initialize the client with your API key
|
|
17
17
|
const client = new ChainAIClient({
|
package/dist/client.d.ts
CHANGED
|
@@ -1,23 +1,34 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* ChainAIClient - Main client class for interacting with Chain AI
|
|
3
3
|
*/
|
|
4
|
-
import type { ChainAIConfig, ChatRequest, ChatResponse, StreamCallbacks, ValidationResult, ResponseCustomization } from './types';
|
|
4
|
+
import type { ChainAIConfig, ChatRequest, ChatResponse, StreamCallbacks, ValidationResult, ResponseCustomization, WalletProvider } from './types';
|
|
5
5
|
export declare class ChainAIClient {
|
|
6
6
|
private icService;
|
|
7
7
|
private nlpService;
|
|
8
8
|
private apiKey;
|
|
9
9
|
private config;
|
|
10
|
+
private walletProvider?;
|
|
10
11
|
constructor(config: ChainAIConfig);
|
|
12
|
+
/**
|
|
13
|
+
* Set or update the wallet provider
|
|
14
|
+
*/
|
|
15
|
+
setWalletProvider(provider: WalletProvider): void;
|
|
16
|
+
/**
|
|
17
|
+
* Get current wallet context from the provider
|
|
18
|
+
*/
|
|
19
|
+
private getWalletContext;
|
|
11
20
|
/**
|
|
12
21
|
* Validate the API key
|
|
13
22
|
*/
|
|
14
23
|
validateApiKey(): Promise<ValidationResult>;
|
|
15
24
|
/**
|
|
16
25
|
* Send a chat message and get AI response
|
|
26
|
+
* Automatically includes wallet context if a walletProvider is configured
|
|
17
27
|
*/
|
|
18
28
|
chat(request: Omit<ChatRequest, 'apiKey'>): Promise<ChatResponse>;
|
|
19
29
|
/**
|
|
20
30
|
* Stream chat response with real-time updates
|
|
31
|
+
* Automatically includes wallet context if a walletProvider is configured
|
|
21
32
|
*/
|
|
22
33
|
streamChat(request: Omit<ChatRequest, 'apiKey'>, callbacks: StreamCallbacks): Promise<void>;
|
|
23
34
|
/**
|
package/dist/client.js
CHANGED
|
@@ -10,6 +10,7 @@ class ChainAIClient {
|
|
|
10
10
|
constructor(config) {
|
|
11
11
|
this.config = config;
|
|
12
12
|
this.apiKey = config.apiKey;
|
|
13
|
+
this.walletProvider = config.walletProvider;
|
|
13
14
|
// Initialize IC service
|
|
14
15
|
this.icService = new ic_service_1.ICService({
|
|
15
16
|
host: config.host || 'https://icp0.io',
|
|
@@ -19,6 +20,28 @@ class ChainAIClient {
|
|
|
19
20
|
// Initialize NLP service
|
|
20
21
|
this.nlpService = new nlp_service_1.NLPService(this.icService);
|
|
21
22
|
}
|
|
23
|
+
/**
|
|
24
|
+
* Set or update the wallet provider
|
|
25
|
+
*/
|
|
26
|
+
setWalletProvider(provider) {
|
|
27
|
+
this.walletProvider = provider;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Get current wallet context from the provider
|
|
31
|
+
*/
|
|
32
|
+
async getWalletContext() {
|
|
33
|
+
if (!this.walletProvider) {
|
|
34
|
+
return undefined;
|
|
35
|
+
}
|
|
36
|
+
try {
|
|
37
|
+
const context = await this.walletProvider();
|
|
38
|
+
return context;
|
|
39
|
+
}
|
|
40
|
+
catch (error) {
|
|
41
|
+
console.warn('Failed to get wallet context:', error);
|
|
42
|
+
return undefined;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
22
45
|
/**
|
|
23
46
|
* Validate the API key
|
|
24
47
|
*/
|
|
@@ -27,21 +50,29 @@ class ChainAIClient {
|
|
|
27
50
|
}
|
|
28
51
|
/**
|
|
29
52
|
* Send a chat message and get AI response
|
|
53
|
+
* Automatically includes wallet context if a walletProvider is configured
|
|
30
54
|
*/
|
|
31
55
|
async chat(request) {
|
|
56
|
+
// Get wallet context from provider if available
|
|
57
|
+
const walletContext = request.walletContext || await this.getWalletContext();
|
|
32
58
|
const fullRequest = {
|
|
33
59
|
...request,
|
|
34
60
|
apiKey: this.apiKey,
|
|
61
|
+
walletContext,
|
|
35
62
|
};
|
|
36
63
|
return this.nlpService.chat(fullRequest);
|
|
37
64
|
}
|
|
38
65
|
/**
|
|
39
66
|
* Stream chat response with real-time updates
|
|
67
|
+
* Automatically includes wallet context if a walletProvider is configured
|
|
40
68
|
*/
|
|
41
69
|
async streamChat(request, callbacks) {
|
|
70
|
+
// Get wallet context from provider if available
|
|
71
|
+
const walletContext = request.walletContext || await this.getWalletContext();
|
|
42
72
|
const fullRequest = {
|
|
43
73
|
...request,
|
|
44
74
|
apiKey: this.apiKey,
|
|
75
|
+
walletContext,
|
|
45
76
|
};
|
|
46
77
|
return this.nlpService.streamChat(fullRequest, callbacks);
|
|
47
78
|
}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* @
|
|
2
|
+
* @chainai/core - Core client library for Chain AI
|
|
3
3
|
*
|
|
4
4
|
* Natural language blockchain assistant powered by the Internet Computer
|
|
5
5
|
*/
|
|
6
6
|
export { ChainAIClient } from './client';
|
|
7
7
|
export { ICService } from './services/ic-service';
|
|
8
8
|
export { NLPService } from './services/nlp-service';
|
|
9
|
-
export type { ChainAIConfig, ValidationResult, ResponseCustomization, ResponseStyle, ResponseLength, ActionTemplates, DataFormatOptions, DateFormat, AddressFormat, NumberFormat, ChatRequest, ChatResponse, AIResponse, Message, StreamCallbacks, } from './types';
|
|
9
|
+
export type { ChainAIConfig, ValidationResult, ResponseCustomization, ResponseStyle, ResponseLength, ActionTemplates, DataFormatOptions, DateFormat, AddressFormat, NumberFormat, ChatRequest, ChatResponse, AIResponse, Message, StreamCallbacks, WalletAddress, WalletContext, WalletProvider, } from './types';
|
|
10
10
|
export { DEFAULT_CUSTOMIZATION } from './types';
|
package/dist/index.js
CHANGED
|
@@ -9,6 +9,20 @@ const principal_1 = require("@dfinity/principal");
|
|
|
9
9
|
// IDL factory for NLP Edge canister
|
|
10
10
|
const createNLPEdgeIdl = (idlFactory) => {
|
|
11
11
|
const { IDL } = idlFactory;
|
|
12
|
+
// AIResponse type matches the canister's AIResponse record
|
|
13
|
+
const AIResponseType = IDL.Record({
|
|
14
|
+
'intent': IDL.Text,
|
|
15
|
+
'kind': IDL.Text,
|
|
16
|
+
'response': IDL.Text,
|
|
17
|
+
'toolData': IDL.Text,
|
|
18
|
+
'confidence': IDL.Nat,
|
|
19
|
+
'conversationId': IDL.Text,
|
|
20
|
+
});
|
|
21
|
+
// ChatResponse is Result<AIResponse, Text> = variant { ok: AIResponse; err: Text }
|
|
22
|
+
const ChatResponseType = IDL.Variant({
|
|
23
|
+
'ok': AIResponseType,
|
|
24
|
+
'err': IDL.Text,
|
|
25
|
+
});
|
|
12
26
|
return IDL.Service({
|
|
13
27
|
'chat': IDL.Func([IDL.Record({
|
|
14
28
|
'message': IDL.Text,
|
|
@@ -16,7 +30,7 @@ const createNLPEdgeIdl = (idlFactory) => {
|
|
|
16
30
|
'language': IDL.Opt(IDL.Text),
|
|
17
31
|
'userContext': IDL.Opt(IDL.Text),
|
|
18
32
|
'apiKey': IDL.Opt(IDL.Text),
|
|
19
|
-
})], [
|
|
33
|
+
})], [ChatResponseType], []),
|
|
20
34
|
'health': IDL.Func([], [IDL.Text], ['query']),
|
|
21
35
|
});
|
|
22
36
|
};
|
|
@@ -13,11 +13,28 @@ class NLPService {
|
|
|
13
13
|
*/
|
|
14
14
|
async chat(request) {
|
|
15
15
|
const actor = this.icService.getNLPEdgeActor();
|
|
16
|
+
// Build user context with wallet information
|
|
17
|
+
const userContextData = {};
|
|
18
|
+
// Include legacy userContext if provided
|
|
19
|
+
if (request.userContext) {
|
|
20
|
+
Object.assign(userContextData, request.userContext);
|
|
21
|
+
}
|
|
22
|
+
// Include wallet context (this is the key addition for wallet addresses)
|
|
23
|
+
if (request.walletContext) {
|
|
24
|
+
userContextData.wallets = request.walletContext.wallets;
|
|
25
|
+
if (request.walletContext.activeChain) {
|
|
26
|
+
userContextData.activeChain = request.walletContext.activeChain;
|
|
27
|
+
}
|
|
28
|
+
if (request.walletContext.activeAddress) {
|
|
29
|
+
userContextData.activeAddress = request.walletContext.activeAddress;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
const hasContext = Object.keys(userContextData).length > 0;
|
|
16
33
|
const chatRequest = {
|
|
17
34
|
message: request.text,
|
|
18
35
|
conversationId: request.conversationId || '',
|
|
19
36
|
language: request.language ? [request.language] : [],
|
|
20
|
-
userContext:
|
|
37
|
+
userContext: hasContext ? [JSON.stringify(userContextData)] : [],
|
|
21
38
|
apiKey: request.apiKey ? [request.apiKey] : [],
|
|
22
39
|
};
|
|
23
40
|
try {
|
package/dist/types/chat.d.ts
CHANGED
|
@@ -1,10 +1,42 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Chat request and response types for Chain AI
|
|
3
3
|
*/
|
|
4
|
+
/**
|
|
5
|
+
* Wallet address for a specific chain
|
|
6
|
+
*/
|
|
7
|
+
export interface WalletAddress {
|
|
8
|
+
chain: 'ethereum' | 'bitcoin' | 'solana' | 'icp' | string;
|
|
9
|
+
address: string;
|
|
10
|
+
/** Optional label (e.g., "MetaMask", "Phantom") */
|
|
11
|
+
label?: string;
|
|
12
|
+
/** Whether this is the primary wallet for the chain */
|
|
13
|
+
isPrimary?: boolean;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Wallet context included with each request
|
|
17
|
+
* Contains all connected wallet addresses so the AI can access them
|
|
18
|
+
* for queries like balance, transaction history, etc.
|
|
19
|
+
*/
|
|
20
|
+
export interface WalletContext {
|
|
21
|
+
/** All connected wallet addresses */
|
|
22
|
+
wallets: WalletAddress[];
|
|
23
|
+
/** Currently active/selected chain (optional) */
|
|
24
|
+
activeChain?: string;
|
|
25
|
+
/** Currently active wallet address (optional) */
|
|
26
|
+
activeAddress?: string;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Function type for dynamically fetching wallet addresses
|
|
30
|
+
* Useful when wallet state may change between requests
|
|
31
|
+
*/
|
|
32
|
+
export type WalletProvider = () => WalletContext | Promise<WalletContext>;
|
|
4
33
|
export interface ChatRequest {
|
|
5
34
|
text: string;
|
|
6
35
|
conversationId?: string;
|
|
36
|
+
/** @deprecated Use walletContext instead for wallet data */
|
|
7
37
|
userContext?: any;
|
|
38
|
+
/** Wallet addresses to include with the request */
|
|
39
|
+
walletContext?: WalletContext;
|
|
8
40
|
language?: string;
|
|
9
41
|
apiKey?: string;
|
|
10
42
|
}
|
package/dist/types/index.d.ts
CHANGED
|
@@ -1,14 +1,33 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Main types export for @
|
|
2
|
+
* Main types export for @chainai/core
|
|
3
3
|
*/
|
|
4
4
|
export type { ResponseCustomization, ResponseStyle, ResponseLength, ActionTemplates, DataFormatOptions, DateFormat, AddressFormat, NumberFormat, } from './customization';
|
|
5
5
|
export { DEFAULT_CUSTOMIZATION } from './customization';
|
|
6
|
-
export type { ChatRequest, ChatResponse, AIResponse, Message, StreamCallbacks, } from './chat';
|
|
6
|
+
export type { ChatRequest, ChatResponse, AIResponse, Message, StreamCallbacks, WalletAddress, WalletContext, WalletProvider, } from './chat';
|
|
7
7
|
export interface ChainAIConfig {
|
|
8
8
|
apiKey: string;
|
|
9
9
|
host?: string;
|
|
10
10
|
nlpEdgeCanisterId?: string;
|
|
11
11
|
developerManagerCanisterId?: string;
|
|
12
|
+
/**
|
|
13
|
+
* Wallet provider function that returns connected wallet addresses.
|
|
14
|
+
* Called automatically before each request to include current wallet state.
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* ```ts
|
|
18
|
+
* const config = {
|
|
19
|
+
* apiKey: 'your-api-key',
|
|
20
|
+
* walletProvider: () => ({
|
|
21
|
+
* wallets: [
|
|
22
|
+
* { chain: 'ethereum', address: '0x...', label: 'MetaMask' },
|
|
23
|
+
* { chain: 'solana', address: 'ABC...', label: 'Phantom' },
|
|
24
|
+
* ],
|
|
25
|
+
* activeChain: 'ethereum',
|
|
26
|
+
* }),
|
|
27
|
+
* };
|
|
28
|
+
* ```
|
|
29
|
+
*/
|
|
30
|
+
walletProvider?: import('./chat').WalletProvider;
|
|
12
31
|
}
|
|
13
32
|
export interface ValidationResult {
|
|
14
33
|
ok: boolean;
|
package/dist/types/index.js
CHANGED
package/package.json
CHANGED
|
@@ -1,9 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@chainai/core",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.2",
|
|
4
4
|
"description": "Core client library for Chain AI - Natural language blockchain assistant",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
|
-
"module": "dist/index.esm.js",
|
|
7
6
|
"types": "dist/index.d.ts",
|
|
8
7
|
"files": [
|
|
9
8
|
"dist"
|