ak-gemini 2.1.1 → 2.1.3
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/chat.js +29 -0
- package/index.cjs +24 -0
- package/package.json +1 -1
- package/types.d.ts +8 -0
package/chat.js
CHANGED
|
@@ -9,6 +9,7 @@ import log from './logger.js';
|
|
|
9
9
|
/**
|
|
10
10
|
* @typedef {import('./types').ChatOptions} ChatOptions
|
|
11
11
|
* @typedef {import('./types').ChatResponse} ChatResponse
|
|
12
|
+
* @typedef {import('./types').ChatStreamEvent} ChatStreamEvent
|
|
12
13
|
*/
|
|
13
14
|
|
|
14
15
|
/**
|
|
@@ -82,6 +83,34 @@ class Chat extends BaseGemini {
|
|
|
82
83
|
usage: this.getLastUsage()
|
|
83
84
|
};
|
|
84
85
|
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Send a message and stream the response as events.
|
|
89
|
+
*
|
|
90
|
+
* @param {string} message - The user's message
|
|
91
|
+
* @param {Object} [opts={}] - Per-message options
|
|
92
|
+
* @yields {ChatStreamEvent}
|
|
93
|
+
*/
|
|
94
|
+
async *stream(message, opts = {}) {
|
|
95
|
+
if (!this.chatSession) await this.init();
|
|
96
|
+
|
|
97
|
+
let fullText = '';
|
|
98
|
+
const streamResponse = await this._withRetry(() => this.chatSession.sendMessageStream({ message }));
|
|
99
|
+
|
|
100
|
+
for await (const chunk of streamResponse) {
|
|
101
|
+
if (chunk.candidates?.[0]?.content?.parts?.[0]?.text) {
|
|
102
|
+
const text = chunk.candidates[0].content.parts[0].text;
|
|
103
|
+
fullText += text;
|
|
104
|
+
yield { type: 'text', text };
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
yield {
|
|
109
|
+
type: 'done',
|
|
110
|
+
fullText,
|
|
111
|
+
usage: this.getLastUsage()
|
|
112
|
+
};
|
|
113
|
+
}
|
|
85
114
|
}
|
|
86
115
|
|
|
87
116
|
export default Chat;
|
package/index.cjs
CHANGED
|
@@ -1216,6 +1216,30 @@ var Chat = class extends base_default {
|
|
|
1216
1216
|
usage: this.getLastUsage()
|
|
1217
1217
|
};
|
|
1218
1218
|
}
|
|
1219
|
+
/**
|
|
1220
|
+
* Send a message and stream the response as events.
|
|
1221
|
+
*
|
|
1222
|
+
* @param {string} message - The user's message
|
|
1223
|
+
* @param {Object} [opts={}] - Per-message options
|
|
1224
|
+
* @yields {ChatStreamEvent}
|
|
1225
|
+
*/
|
|
1226
|
+
async *stream(message, opts = {}) {
|
|
1227
|
+
if (!this.chatSession) await this.init();
|
|
1228
|
+
let fullText = "";
|
|
1229
|
+
const streamResponse = await this._withRetry(() => this.chatSession.sendMessageStream({ message }));
|
|
1230
|
+
for await (const chunk of streamResponse) {
|
|
1231
|
+
if (chunk.candidates?.[0]?.content?.parts?.[0]?.text) {
|
|
1232
|
+
const text = chunk.candidates[0].content.parts[0].text;
|
|
1233
|
+
fullText += text;
|
|
1234
|
+
yield { type: "text", text };
|
|
1235
|
+
}
|
|
1236
|
+
}
|
|
1237
|
+
yield {
|
|
1238
|
+
type: "done",
|
|
1239
|
+
fullText,
|
|
1240
|
+
usage: this.getLastUsage()
|
|
1241
|
+
};
|
|
1242
|
+
}
|
|
1219
1243
|
};
|
|
1220
1244
|
var chat_default = Chat;
|
|
1221
1245
|
|
package/package.json
CHANGED
package/types.d.ts
CHANGED
|
@@ -421,6 +421,13 @@ export interface ChatResponse {
|
|
|
421
421
|
usage: UsageData | null;
|
|
422
422
|
}
|
|
423
423
|
|
|
424
|
+
export interface ChatStreamEvent {
|
|
425
|
+
type: 'text' | 'done';
|
|
426
|
+
text?: string;
|
|
427
|
+
fullText?: string;
|
|
428
|
+
usage?: UsageData | null;
|
|
429
|
+
}
|
|
430
|
+
|
|
424
431
|
export interface MessageResponse {
|
|
425
432
|
/** The model's text response */
|
|
426
433
|
text: string;
|
|
@@ -548,6 +555,7 @@ export declare class Chat extends BaseGemini {
|
|
|
548
555
|
constructor(options?: ChatOptions);
|
|
549
556
|
|
|
550
557
|
send(message: string, opts?: { labels?: Record<string, string> }): Promise<ChatResponse>;
|
|
558
|
+
stream(message: string, opts?: { labels?: Record<string, string> }): AsyncGenerator<ChatStreamEvent, void, unknown>;
|
|
551
559
|
}
|
|
552
560
|
|
|
553
561
|
export declare class Message extends BaseGemini {
|