@nxcode/sdk 1.0.0 → 1.0.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 +535 -0
- package/dist/nxcode.esm.js +323 -2
- package/dist/nxcode.esm.js.map +1 -1
- package/dist/nxcode.js +323 -2
- package/dist/nxcode.js.map +1 -1
- package/dist/nxcode.min.js +1 -1
- package/dist/types/ai.d.ts +124 -0
- package/dist/types/auth.test.d.ts +6 -0
- package/dist/types/index.d.ts +52 -3
- package/package.json +1 -1
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Nxcode SDK - AI Module
|
|
3
|
+
*
|
|
4
|
+
* Provides access to AI capabilities through the Nxcode AI Gateway.
|
|
5
|
+
* All AI calls require user authentication and are billed to the user's balance.
|
|
6
|
+
*/
|
|
7
|
+
/**
|
|
8
|
+
* Content part for multimodal messages
|
|
9
|
+
*/
|
|
10
|
+
export interface TextPart {
|
|
11
|
+
type: 'text';
|
|
12
|
+
text: string;
|
|
13
|
+
}
|
|
14
|
+
export interface ImagePart {
|
|
15
|
+
type: 'image';
|
|
16
|
+
/** Base64 encoded image data */
|
|
17
|
+
data: string;
|
|
18
|
+
/** MIME type (e.g., 'image/png', 'image/jpeg') */
|
|
19
|
+
mimeType: string;
|
|
20
|
+
}
|
|
21
|
+
export interface ImageUrlPart {
|
|
22
|
+
type: 'image_url';
|
|
23
|
+
/** URL of the image */
|
|
24
|
+
url: string;
|
|
25
|
+
}
|
|
26
|
+
export type ContentPart = TextPart | ImagePart | ImageUrlPart;
|
|
27
|
+
export interface ChatMessage {
|
|
28
|
+
role: 'user' | 'assistant' | 'system';
|
|
29
|
+
/** Text content or array of content parts for multimodal */
|
|
30
|
+
content: string | ContentPart[];
|
|
31
|
+
}
|
|
32
|
+
export interface ChatOptions {
|
|
33
|
+
messages: ChatMessage[];
|
|
34
|
+
model?: string;
|
|
35
|
+
}
|
|
36
|
+
export interface ChatResponse {
|
|
37
|
+
content: string;
|
|
38
|
+
usage?: {
|
|
39
|
+
inputTokens: number;
|
|
40
|
+
outputTokens: number;
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
export interface GenerateOptions {
|
|
44
|
+
prompt: string;
|
|
45
|
+
model?: string;
|
|
46
|
+
}
|
|
47
|
+
export interface GenerateResponse {
|
|
48
|
+
text: string;
|
|
49
|
+
usage?: {
|
|
50
|
+
inputTokens: number;
|
|
51
|
+
outputTokens: number;
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
export interface StreamChunk {
|
|
55
|
+
content: string;
|
|
56
|
+
done: boolean;
|
|
57
|
+
usage?: {
|
|
58
|
+
inputTokens: number;
|
|
59
|
+
outputTokens: number;
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
export type StreamCallback = (chunk: StreamChunk) => void;
|
|
63
|
+
export declare class NxcodeAI {
|
|
64
|
+
private apiEndpoint;
|
|
65
|
+
private appId;
|
|
66
|
+
private getToken;
|
|
67
|
+
constructor(apiEndpoint: string, appId: string, getToken: () => string | null);
|
|
68
|
+
/**
|
|
69
|
+
* Send a chat message and get a response.
|
|
70
|
+
*
|
|
71
|
+
* @example
|
|
72
|
+
* const response = await Nxcode.ai.chat({
|
|
73
|
+
* messages: [
|
|
74
|
+
* { role: 'user', content: 'Hello!' }
|
|
75
|
+
* ]
|
|
76
|
+
* });
|
|
77
|
+
* console.log(response.content);
|
|
78
|
+
*/
|
|
79
|
+
chat(options: ChatOptions): Promise<ChatResponse>;
|
|
80
|
+
/**
|
|
81
|
+
* Generate text from a prompt.
|
|
82
|
+
*
|
|
83
|
+
* @example
|
|
84
|
+
* const response = await Nxcode.ai.generate({
|
|
85
|
+
* prompt: 'Write a haiku about coding'
|
|
86
|
+
* });
|
|
87
|
+
* console.log(response.text);
|
|
88
|
+
*/
|
|
89
|
+
generate(options: GenerateOptions): Promise<GenerateResponse>;
|
|
90
|
+
/**
|
|
91
|
+
* Stream a chat response in real-time.
|
|
92
|
+
*
|
|
93
|
+
* @example
|
|
94
|
+
* await Nxcode.ai.chatStream({
|
|
95
|
+
* messages: [{ role: 'user', content: 'Tell me a story' }],
|
|
96
|
+
* onChunk: (chunk) => {
|
|
97
|
+
* process.stdout.write(chunk.content);
|
|
98
|
+
* if (chunk.done) console.log('\n--- Done ---');
|
|
99
|
+
* }
|
|
100
|
+
* });
|
|
101
|
+
*/
|
|
102
|
+
chatStream(options: ChatOptions & {
|
|
103
|
+
onChunk: StreamCallback;
|
|
104
|
+
}): Promise<void>;
|
|
105
|
+
/**
|
|
106
|
+
* Stream text generation from a prompt.
|
|
107
|
+
*
|
|
108
|
+
* @example
|
|
109
|
+
* await Nxcode.ai.generateStream({
|
|
110
|
+
* prompt: 'Write a poem',
|
|
111
|
+
* onChunk: (chunk) => console.log(chunk.content)
|
|
112
|
+
* });
|
|
113
|
+
*/
|
|
114
|
+
generateStream(options: GenerateOptions & {
|
|
115
|
+
onChunk: StreamCallback;
|
|
116
|
+
}): Promise<void>;
|
|
117
|
+
private getHeaders;
|
|
118
|
+
private getAnonymousHeaders;
|
|
119
|
+
private convertMessagesToGemini;
|
|
120
|
+
private convertContentToParts;
|
|
121
|
+
private parseGeminiResponse;
|
|
122
|
+
private parseStreamChunk;
|
|
123
|
+
private handleError;
|
|
124
|
+
}
|
package/dist/types/index.d.ts
CHANGED
|
@@ -11,12 +11,14 @@
|
|
|
11
11
|
* console.log('Logged in:', user.email);
|
|
12
12
|
* </script>
|
|
13
13
|
*/
|
|
14
|
+
import { type ChatOptions, type ChatResponse, type GenerateOptions, type GenerateResponse, type StreamChunk, type StreamCallback, type ChatMessage, type ContentPart, type TextPart, type ImagePart, type ImageUrlPart } from './ai';
|
|
14
15
|
import type { NxcodeUser, NxcodeConfig, ChargeOptions, ChargeResult, Transaction, AuthStateCallback } from './types';
|
|
15
16
|
declare class NxcodeSDK {
|
|
16
17
|
private config;
|
|
17
18
|
private _auth;
|
|
18
19
|
private _billing;
|
|
19
20
|
private _payment;
|
|
21
|
+
private _ai;
|
|
20
22
|
private initPromise;
|
|
21
23
|
private apiEndpoint;
|
|
22
24
|
constructor();
|
|
@@ -29,9 +31,11 @@ declare class NxcodeSDK {
|
|
|
29
31
|
*/
|
|
30
32
|
private init;
|
|
31
33
|
/**
|
|
32
|
-
* Manually configure SDK with app ID
|
|
34
|
+
* Manually configure SDK with app ID and optional endpoint
|
|
33
35
|
*/
|
|
34
|
-
configure(appId: string
|
|
36
|
+
configure(appId: string, options?: {
|
|
37
|
+
apiEndpoint?: string;
|
|
38
|
+
}): void;
|
|
35
39
|
/**
|
|
36
40
|
* Set up SDK modules after configuration
|
|
37
41
|
*/
|
|
@@ -90,6 +94,51 @@ declare class NxcodeSDK {
|
|
|
90
94
|
*/
|
|
91
95
|
getTransactions(limit?: number, offset?: number): Promise<Transaction[]>;
|
|
92
96
|
};
|
|
97
|
+
get ai(): {
|
|
98
|
+
/**
|
|
99
|
+
* Send a chat message and get a response
|
|
100
|
+
*
|
|
101
|
+
* @example
|
|
102
|
+
* const response = await Nxcode.ai.chat({
|
|
103
|
+
* messages: [{ role: 'user', content: 'Hello!' }]
|
|
104
|
+
* });
|
|
105
|
+
*/
|
|
106
|
+
chat(options: ChatOptions): Promise<ChatResponse>;
|
|
107
|
+
/**
|
|
108
|
+
* Generate text from a prompt
|
|
109
|
+
*
|
|
110
|
+
* @example
|
|
111
|
+
* const response = await Nxcode.ai.generate({ prompt: 'Write a haiku' });
|
|
112
|
+
*/
|
|
113
|
+
generate(options: GenerateOptions): Promise<GenerateResponse>;
|
|
114
|
+
/**
|
|
115
|
+
* Stream a chat response in real-time
|
|
116
|
+
*
|
|
117
|
+
* @example
|
|
118
|
+
* await Nxcode.ai.chatStream({
|
|
119
|
+
* messages: [{ role: 'user', content: 'Tell me a story' }],
|
|
120
|
+
* onChunk: (chunk) => {
|
|
121
|
+
* document.body.innerText += chunk.content;
|
|
122
|
+
* if (chunk.done) console.log('Done!');
|
|
123
|
+
* }
|
|
124
|
+
* });
|
|
125
|
+
*/
|
|
126
|
+
chatStream(options: ChatOptions & {
|
|
127
|
+
onChunk: StreamCallback;
|
|
128
|
+
}): Promise<void>;
|
|
129
|
+
/**
|
|
130
|
+
* Stream text generation from a prompt
|
|
131
|
+
*
|
|
132
|
+
* @example
|
|
133
|
+
* await Nxcode.ai.generateStream({
|
|
134
|
+
* prompt: 'Write a poem',
|
|
135
|
+
* onChunk: (chunk) => console.log(chunk.content)
|
|
136
|
+
* });
|
|
137
|
+
*/
|
|
138
|
+
generateStream(options: GenerateOptions & {
|
|
139
|
+
onChunk: StreamCallback;
|
|
140
|
+
}): Promise<void>;
|
|
141
|
+
};
|
|
93
142
|
/**
|
|
94
143
|
* Get current configuration
|
|
95
144
|
*/
|
|
@@ -106,4 +155,4 @@ declare class NxcodeSDK {
|
|
|
106
155
|
declare const Nxcode: NxcodeSDK;
|
|
107
156
|
export default Nxcode;
|
|
108
157
|
export { Nxcode };
|
|
109
|
-
export type { NxcodeUser, NxcodeConfig, ChargeOptions, ChargeResult, Transaction, AuthStateCallback, };
|
|
158
|
+
export type { NxcodeUser, NxcodeConfig, ChargeOptions, ChargeResult, Transaction, AuthStateCallback, ChatOptions, ChatResponse, ChatMessage, ContentPart, TextPart, ImagePart, ImageUrlPart, GenerateOptions, GenerateResponse, StreamChunk, StreamCallback, };
|