@aituber-onair/core 0.1.0
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 +723 -0
- package/dist/constants/api.d.ts +4 -0
- package/dist/constants/api.js +13 -0
- package/dist/constants/api.js.map +1 -0
- package/dist/constants/index.d.ts +23 -0
- package/dist/constants/index.js +25 -0
- package/dist/constants/index.js.map +1 -0
- package/dist/constants/openaiApi.d.ts +15 -0
- package/dist/constants/openaiApi.js +15 -0
- package/dist/constants/openaiApi.js.map +1 -0
- package/dist/constants/prompts.d.ts +2 -0
- package/dist/constants/prompts.js +13 -0
- package/dist/constants/prompts.js.map +1 -0
- package/dist/core/AITuberOnAirCore.d.ts +142 -0
- package/dist/core/AITuberOnAirCore.js +316 -0
- package/dist/core/AITuberOnAirCore.js.map +1 -0
- package/dist/core/ChatProcessor.d.ts +86 -0
- package/dist/core/ChatProcessor.js +246 -0
- package/dist/core/ChatProcessor.js.map +1 -0
- package/dist/core/EventEmitter.d.ts +35 -0
- package/dist/core/EventEmitter.js +72 -0
- package/dist/core/EventEmitter.js.map +1 -0
- package/dist/core/MemoryManager.d.ts +98 -0
- package/dist/core/MemoryManager.js +208 -0
- package/dist/core/MemoryManager.js.map +1 -0
- package/dist/index.d.ts +24 -0
- package/dist/index.js +22 -0
- package/dist/index.js.map +1 -0
- package/dist/services/chat/ChatService.d.ts +21 -0
- package/dist/services/chat/ChatService.js +2 -0
- package/dist/services/chat/ChatService.js.map +1 -0
- package/dist/services/chat/ChatServiceFactory.d.ts +38 -0
- package/dist/services/chat/ChatServiceFactory.js +55 -0
- package/dist/services/chat/ChatServiceFactory.js.map +1 -0
- package/dist/services/chat/OpenAIChatService.d.ts +38 -0
- package/dist/services/chat/OpenAIChatService.js +166 -0
- package/dist/services/chat/OpenAIChatService.js.map +1 -0
- package/dist/services/chat/OpenAISummarizer.d.ts +25 -0
- package/dist/services/chat/OpenAISummarizer.js +70 -0
- package/dist/services/chat/OpenAISummarizer.js.map +1 -0
- package/dist/services/chat/providers/ChatServiceProvider.d.ts +44 -0
- package/dist/services/chat/providers/ChatServiceProvider.js +2 -0
- package/dist/services/chat/providers/ChatServiceProvider.js.map +1 -0
- package/dist/services/chat/providers/OpenAIChatServiceProvider.d.ts +33 -0
- package/dist/services/chat/providers/OpenAIChatServiceProvider.js +44 -0
- package/dist/services/chat/providers/OpenAIChatServiceProvider.js.map +1 -0
- package/dist/services/voice/VoiceEngineAdapter.d.ts +46 -0
- package/dist/services/voice/VoiceEngineAdapter.js +173 -0
- package/dist/services/voice/VoiceEngineAdapter.js.map +1 -0
- package/dist/services/voice/VoiceService.d.ts +55 -0
- package/dist/services/voice/VoiceService.js +2 -0
- package/dist/services/voice/VoiceService.js.map +1 -0
- package/dist/services/voice/engines/AivisSpeechEngine.d.ts +10 -0
- package/dist/services/voice/engines/AivisSpeechEngine.js +70 -0
- package/dist/services/voice/engines/AivisSpeechEngine.js.map +1 -0
- package/dist/services/voice/engines/NijiVoiceEngine.d.ts +12 -0
- package/dist/services/voice/engines/NijiVoiceEngine.js +105 -0
- package/dist/services/voice/engines/NijiVoiceEngine.js.map +1 -0
- package/dist/services/voice/engines/OpenAiEngine.d.ts +9 -0
- package/dist/services/voice/engines/OpenAiEngine.js +34 -0
- package/dist/services/voice/engines/OpenAiEngine.js.map +1 -0
- package/dist/services/voice/engines/VoiceEngine.d.ts +21 -0
- package/dist/services/voice/engines/VoiceEngine.js +2 -0
- package/dist/services/voice/engines/VoiceEngine.js.map +1 -0
- package/dist/services/voice/engines/VoiceEngineFactory.d.ts +14 -0
- package/dist/services/voice/engines/VoiceEngineFactory.js +34 -0
- package/dist/services/voice/engines/VoiceEngineFactory.js.map +1 -0
- package/dist/services/voice/engines/VoicePeakEngine.d.ts +13 -0
- package/dist/services/voice/engines/VoicePeakEngine.js +46 -0
- package/dist/services/voice/engines/VoicePeakEngine.js.map +1 -0
- package/dist/services/voice/engines/VoiceVoxEngine.d.ts +13 -0
- package/dist/services/voice/engines/VoiceVoxEngine.js +67 -0
- package/dist/services/voice/engines/VoiceVoxEngine.js.map +1 -0
- package/dist/services/voice/engines/index.d.ts +7 -0
- package/dist/services/voice/engines/index.js +7 -0
- package/dist/services/voice/engines/index.js.map +1 -0
- package/dist/services/voice/messages.d.ts +38 -0
- package/dist/services/voice/messages.js +49 -0
- package/dist/services/voice/messages.js.map +1 -0
- package/dist/services/youtube/YouTubeDataApiService.d.ts +69 -0
- package/dist/services/youtube/YouTubeDataApiService.js +255 -0
- package/dist/services/youtube/YouTubeDataApiService.js.map +1 -0
- package/dist/services/youtube/YouTubeService.d.ts +63 -0
- package/dist/services/youtube/YouTubeService.js +2 -0
- package/dist/services/youtube/YouTubeService.js.map +1 -0
- package/dist/types/index.d.ts +82 -0
- package/dist/types/index.js +5 -0
- package/dist/types/index.js.map +1 -0
- package/dist/types/nijiVoice.d.ts +27 -0
- package/dist/types/nijiVoice.js +2 -0
- package/dist/types/nijiVoice.js.map +1 -0
- package/dist/utils/index.d.ts +5 -0
- package/dist/utils/index.js +6 -0
- package/dist/utils/index.js.map +1 -0
- package/dist/utils/screenplay.d.ts +19 -0
- package/dist/utils/screenplay.js +42 -0
- package/dist/utils/screenplay.js.map +1 -0
- package/dist/utils/screenshot.d.ts +19 -0
- package/dist/utils/screenshot.js +44 -0
- package/dist/utils/screenshot.js.map +1 -0
- package/dist/utils/storage.d.ts +44 -0
- package/dist/utils/storage.js +103 -0
- package/dist/utils/storage.js.map +1 -0
- package/package.json +33 -0
|
@@ -0,0 +1,208 @@
|
|
|
1
|
+
import { EventEmitter } from './EventEmitter';
|
|
2
|
+
/**
|
|
3
|
+
* Manager for managing chat history and summarization
|
|
4
|
+
*/
|
|
5
|
+
export class MemoryManager extends EventEmitter {
|
|
6
|
+
/**
|
|
7
|
+
* Constructor
|
|
8
|
+
* @param options Settings options
|
|
9
|
+
* @param summarizer Summarizer implementation (if omitted, summarization is disabled)
|
|
10
|
+
* @param storage Storage implementation for persistence (optional)
|
|
11
|
+
*/
|
|
12
|
+
constructor(options, summarizer, storage) {
|
|
13
|
+
super();
|
|
14
|
+
this.memories = [];
|
|
15
|
+
this.options = {
|
|
16
|
+
enableSummarization: options.enableSummarization,
|
|
17
|
+
shortTermDuration: options.shortTermDuration || 60 * 1000, // Default 1 minute
|
|
18
|
+
midTermDuration: options.midTermDuration || 4 * 60 * 1000, // Default 4 minutes
|
|
19
|
+
longTermDuration: options.longTermDuration || 9 * 60 * 1000, // Default 9 minutes
|
|
20
|
+
maxMessagesBeforeSummarization: options.maxMessagesBeforeSummarization || 20,
|
|
21
|
+
maxSummaryLength: options.maxSummaryLength || 256,
|
|
22
|
+
memoryRetentionPeriod: options.memoryRetentionPeriod || 60 * 60 * 1000, // Default 1 hour
|
|
23
|
+
summaryPromptTemplate: options.summaryPromptTemplate,
|
|
24
|
+
};
|
|
25
|
+
this.summarizer = summarizer;
|
|
26
|
+
this.storage = storage;
|
|
27
|
+
// Load memories from storage if available
|
|
28
|
+
this.loadFromStorage();
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Load memories from storage
|
|
32
|
+
*/
|
|
33
|
+
async loadFromStorage() {
|
|
34
|
+
if (!this.storage)
|
|
35
|
+
return;
|
|
36
|
+
try {
|
|
37
|
+
const loadedMemories = await this.storage.load();
|
|
38
|
+
if (loadedMemories && loadedMemories.length > 0) {
|
|
39
|
+
this.memories = loadedMemories;
|
|
40
|
+
this.emit('memoriesLoaded', loadedMemories);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
catch (error) {
|
|
44
|
+
console.error('Error loading memories from storage:', error);
|
|
45
|
+
this.emit('error', error);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Save memories to storage
|
|
50
|
+
*/
|
|
51
|
+
async saveToStorage() {
|
|
52
|
+
if (!this.storage)
|
|
53
|
+
return;
|
|
54
|
+
try {
|
|
55
|
+
await this.storage.save(this.memories);
|
|
56
|
+
this.emit('memoriesSaved', this.memories);
|
|
57
|
+
}
|
|
58
|
+
catch (error) {
|
|
59
|
+
console.error('Error saving memories to storage:', error);
|
|
60
|
+
this.emit('error', error);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Import memory records (used for restoring from external storage)
|
|
65
|
+
* @param records Memory records to import
|
|
66
|
+
*/
|
|
67
|
+
importMemoryRecords(records) {
|
|
68
|
+
if (!records || records.length === 0)
|
|
69
|
+
return;
|
|
70
|
+
// Filter out any records with invalid types
|
|
71
|
+
const validRecords = records.filter((record) => ['short', 'mid', 'long'].includes(record.type));
|
|
72
|
+
// Replace existing memories of same types
|
|
73
|
+
const existingTypes = new Set(validRecords.map((r) => r.type));
|
|
74
|
+
this.memories = [
|
|
75
|
+
...this.memories.filter((m) => !existingTypes.has(m.type)),
|
|
76
|
+
...validRecords,
|
|
77
|
+
];
|
|
78
|
+
this.emit('memoriesImported', validRecords);
|
|
79
|
+
// Save to storage if available
|
|
80
|
+
this.saveToStorage();
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Summarize chat log and save as memory
|
|
84
|
+
* @param chatLog Chat log
|
|
85
|
+
* @param chatStartTime Chat start time
|
|
86
|
+
*/
|
|
87
|
+
async createMemoryIfNeeded(chatLog, chatStartTime) {
|
|
88
|
+
if (!this.options.enableSummarization || !this.summarizer) {
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
91
|
+
const now = Date.now();
|
|
92
|
+
const elapsedTime = now - chatStartTime;
|
|
93
|
+
try {
|
|
94
|
+
// Create short-term memory (1 minute passed)
|
|
95
|
+
if (elapsedTime >= this.options.shortTermDuration) {
|
|
96
|
+
await this.createMemory('short', chatLog, now);
|
|
97
|
+
}
|
|
98
|
+
// Create mid-term memory (4 minutes passed)
|
|
99
|
+
if (elapsedTime >= this.options.midTermDuration) {
|
|
100
|
+
await this.createMemory('mid', chatLog, now);
|
|
101
|
+
}
|
|
102
|
+
// Create long-term memory (9 minutes passed)
|
|
103
|
+
if (elapsedTime >= this.options.longTermDuration) {
|
|
104
|
+
await this.createMemory('long', chatLog, now);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
catch (error) {
|
|
108
|
+
console.error('Error creating memory:', error);
|
|
109
|
+
this.emit('error', error);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* Create memory of specified type
|
|
114
|
+
* @param type Memory type
|
|
115
|
+
* @param chatLog Chat log
|
|
116
|
+
* @param timestamp Timestamp
|
|
117
|
+
*/
|
|
118
|
+
async createMemory(type, chatLog, timestamp) {
|
|
119
|
+
if (!this.summarizer)
|
|
120
|
+
return;
|
|
121
|
+
try {
|
|
122
|
+
// Delete old memories of the same type
|
|
123
|
+
this.memories = this.memories.filter((mem) => mem.type !== type);
|
|
124
|
+
// Summarize chat log
|
|
125
|
+
const summary = await this.summarizer.summarize(chatLog, this.options.maxSummaryLength);
|
|
126
|
+
// Add new memory
|
|
127
|
+
const memoryRecord = {
|
|
128
|
+
type,
|
|
129
|
+
summary,
|
|
130
|
+
timestamp,
|
|
131
|
+
};
|
|
132
|
+
this.memories.push(memoryRecord);
|
|
133
|
+
this.emit('memoryCreated', memoryRecord);
|
|
134
|
+
// Save to storage if available
|
|
135
|
+
await this.saveToStorage();
|
|
136
|
+
}
|
|
137
|
+
catch (error) {
|
|
138
|
+
console.error(`Error creating ${type} memory:`, error);
|
|
139
|
+
this.emit('error', error);
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
/**
|
|
143
|
+
* Get all memories
|
|
144
|
+
*/
|
|
145
|
+
getAllMemories() {
|
|
146
|
+
return [...this.memories];
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* Get memory of specified type
|
|
150
|
+
* @param type Memory type
|
|
151
|
+
*/
|
|
152
|
+
getMemoryByType(type) {
|
|
153
|
+
return this.memories.find((mem) => mem.type === type);
|
|
154
|
+
}
|
|
155
|
+
/**
|
|
156
|
+
* Generate memory text for AI prompt
|
|
157
|
+
*/
|
|
158
|
+
getMemoryForPrompt() {
|
|
159
|
+
const memoryParts = [];
|
|
160
|
+
const shortMemory = this.getMemoryByType('short');
|
|
161
|
+
if (shortMemory) {
|
|
162
|
+
memoryParts.push(`[Short-term memory: 1min]\n${shortMemory.summary}`);
|
|
163
|
+
}
|
|
164
|
+
const midMemory = this.getMemoryByType('mid');
|
|
165
|
+
if (midMemory) {
|
|
166
|
+
memoryParts.push(`[Mid-term memory: 4min]\n${midMemory.summary}`);
|
|
167
|
+
}
|
|
168
|
+
const longMemory = this.getMemoryByType('long');
|
|
169
|
+
if (longMemory) {
|
|
170
|
+
memoryParts.push(`[Long-term memory: 9min]\n${longMemory.summary}`);
|
|
171
|
+
}
|
|
172
|
+
return memoryParts.join('\n\n');
|
|
173
|
+
}
|
|
174
|
+
/**
|
|
175
|
+
* Clean up old memories
|
|
176
|
+
*/
|
|
177
|
+
async cleanupOldMemories() {
|
|
178
|
+
const now = Date.now();
|
|
179
|
+
const retentionPeriod = this.options.memoryRetentionPeriod || 60 * 60 * 1000; // Default 1 hour
|
|
180
|
+
const oldMemories = this.memories.filter((mem) => now - mem.timestamp > retentionPeriod);
|
|
181
|
+
if (oldMemories.length > 0) {
|
|
182
|
+
this.memories = this.memories.filter((mem) => now - mem.timestamp <= retentionPeriod);
|
|
183
|
+
this.emit('memoriesRemoved', oldMemories);
|
|
184
|
+
// Save to storage if available
|
|
185
|
+
await this.saveToStorage();
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
/**
|
|
189
|
+
* Clear all memories
|
|
190
|
+
*/
|
|
191
|
+
async clearAllMemories() {
|
|
192
|
+
const oldMemories = [...this.memories];
|
|
193
|
+
this.memories = [];
|
|
194
|
+
this.emit('memoriesRemoved', oldMemories);
|
|
195
|
+
// Clear storage if available
|
|
196
|
+
if (this.storage) {
|
|
197
|
+
try {
|
|
198
|
+
await this.storage.clear();
|
|
199
|
+
this.emit('storageCleared');
|
|
200
|
+
}
|
|
201
|
+
catch (error) {
|
|
202
|
+
console.error('Error clearing storage:', error);
|
|
203
|
+
this.emit('error', error);
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
//# sourceMappingURL=MemoryManager.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"MemoryManager.js","sourceRoot":"","sources":["../../src/core/MemoryManager.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;AAyC9C;;GAEG;AACH,MAAM,OAAO,aAAc,SAAQ,YAAY;IAM7C;;;;;OAKG;IACH,YACE,OAAsB,EACtB,UAAuB,EACvB,OAAuB;QAEvB,KAAK,EAAE,CAAC;QAdF,aAAQ,GAAmB,EAAE,CAAC;QAepC,IAAI,CAAC,OAAO,GAAG;YACb,mBAAmB,EAAE,OAAO,CAAC,mBAAmB;YAChD,iBAAiB,EAAE,OAAO,CAAC,iBAAiB,IAAI,EAAE,GAAG,IAAI,EAAE,mBAAmB;YAC9E,eAAe,EAAE,OAAO,CAAC,eAAe,IAAI,CAAC,GAAG,EAAE,GAAG,IAAI,EAAE,oBAAoB;YAC/E,gBAAgB,EAAE,OAAO,CAAC,gBAAgB,IAAI,CAAC,GAAG,EAAE,GAAG,IAAI,EAAE,oBAAoB;YACjF,8BAA8B,EAC5B,OAAO,CAAC,8BAA8B,IAAI,EAAE;YAC9C,gBAAgB,EAAE,OAAO,CAAC,gBAAgB,IAAI,GAAG;YACjD,qBAAqB,EAAE,OAAO,CAAC,qBAAqB,IAAI,EAAE,GAAG,EAAE,GAAG,IAAI,EAAE,iBAAiB;YACzF,qBAAqB,EAAE,OAAO,CAAC,qBAAqB;SACrD,CAAC;QACF,IAAI,CAAC,UAAU,GAAG,UAAU,CAAC;QAC7B,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC;QAEvB,0CAA0C;QAC1C,IAAI,CAAC,eAAe,EAAE,CAAC;IACzB,CAAC;IAED;;OAEG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G,GAAG,GAAG,CAAC,SAAS,GAAG,eAAe,CAC/C,CAAC;QAEF,IAAI,WAAW,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;YAC3B,IAAI,CAAC,QAAQ,GAAG,IAAI,CAAC,QAAQ,CAAC,MAAM,CAClC,CAAC,GAAG,EAAE,EAAE,CAAC,GAAG,GAAG,GAAG,CAAC,SAAS,IAAI,eAAe,CAChD,CAAC;YAEF,IAAI,CAAC,IAAI,CAAC,iBAAiB,EAAE,WAAW,CAAC,CAAC;YAE1C,+BAA+B;YAC/B,MAAM,IAAI,CAAC,aAAa,EAAE,CAAC;QAC7B,CAAC;IACH,CAAC;IAED;;OAEG;IACH,KAAK,CAAC,gBAAgB;QACpB,MAAM,WAAW,GAAG,CAAC,GAAG,IAAI,CAAC,QAAQ,CAAC,CAAC;QACvC,IAAI,CAAC,QAAQ,GAAG,EAAE,CAAC;QACnB,IAAI,CAAC,IAAI,CAAC,iBAAiB,EAAE,WAAW,CAAC,CAAC;QAE1C,6BAA6B;QAC7B,IAAI,IAAI,CAAC,OAAO,EAAE,CAAC;YACjB,IAAI,CAAC;gBACH,MAAM,IAAI,CAAC,OAAO,CAAC,KAAK,EAAE,CAAC;gBAC3B,IAAI,CAAC,IAAI,CAAC,gBAAgB,CAAC,CAAC;YAC9B,CAAC;YAAC,OAAO,KAAK,EAAE,CAAC;gBACf,OAAO,CAAC,KAAK,CAAC,yBAAyB,EAAE,KAAK,CAAC,CAAC;gBAChD,IAAI,CAAC,IAAI,CAAC,OAAO,EAAE,KAAK,CAAC,CAAC;YAC5B,CAAC;QACH,CAAC;IACH,CAAC;CACF"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* AITuber Core
|
|
3
|
+
* Modularized library of AITuber OnAir core features
|
|
4
|
+
*/
|
|
5
|
+
export * from './types';
|
|
6
|
+
export type { ChatService } from './services/chat/ChatService';
|
|
7
|
+
export { OpenAIChatService } from './services/chat/OpenAIChatService';
|
|
8
|
+
export { OpenAISummarizer } from './services/chat/OpenAISummarizer';
|
|
9
|
+
export type { ChatServiceOptions, ChatServiceProvider, } from './services/chat/providers/ChatServiceProvider';
|
|
10
|
+
export { ChatServiceFactory } from './services/chat/ChatServiceFactory';
|
|
11
|
+
export { OpenAIChatServiceProvider } from './services/chat/providers/OpenAIChatServiceProvider';
|
|
12
|
+
export type { VoiceService, VoiceServiceOptions, AudioPlayOptions, } from './services/voice/VoiceService';
|
|
13
|
+
export { VoiceEngineAdapter } from './services/voice/VoiceEngineAdapter';
|
|
14
|
+
export { EventEmitter } from './core/EventEmitter';
|
|
15
|
+
export type { MemoryOptions, Summarizer } from './core/MemoryManager';
|
|
16
|
+
export { MemoryManager } from './core/MemoryManager';
|
|
17
|
+
export type { ChatProcessorOptions } from './core/ChatProcessor';
|
|
18
|
+
export { ChatProcessor } from './core/ChatProcessor';
|
|
19
|
+
export type { AITuberOnAirCoreOptions } from './core/AITuberOnAirCore';
|
|
20
|
+
export { AITuberOnAirCore, AITuberOnAirCoreEvent, } from './core/AITuberOnAirCore';
|
|
21
|
+
export * from './utils';
|
|
22
|
+
export { createMemoryStorage } from './utils/storage';
|
|
23
|
+
export * from './constants';
|
|
24
|
+
export type { Screenplay } from './types';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* AITuber Core
|
|
3
|
+
* Modularized library of AITuber OnAir core features
|
|
4
|
+
*/
|
|
5
|
+
// Type definitions exports
|
|
6
|
+
export * from './types';
|
|
7
|
+
export { OpenAIChatService } from './services/chat/OpenAIChatService';
|
|
8
|
+
export { OpenAISummarizer } from './services/chat/OpenAISummarizer';
|
|
9
|
+
export { ChatServiceFactory } from './services/chat/ChatServiceFactory';
|
|
10
|
+
export { OpenAIChatServiceProvider } from './services/chat/providers/OpenAIChatServiceProvider';
|
|
11
|
+
export { VoiceEngineAdapter } from './services/voice/VoiceEngineAdapter';
|
|
12
|
+
// Core module exports
|
|
13
|
+
export { EventEmitter } from './core/EventEmitter';
|
|
14
|
+
export { MemoryManager } from './core/MemoryManager';
|
|
15
|
+
export { ChatProcessor } from './core/ChatProcessor';
|
|
16
|
+
export { AITuberOnAirCore, AITuberOnAirCoreEvent, } from './core/AITuberOnAirCore';
|
|
17
|
+
// Utility exports
|
|
18
|
+
export * from './utils';
|
|
19
|
+
export { createMemoryStorage } from './utils/storage';
|
|
20
|
+
// Constants exports
|
|
21
|
+
export * from './constants';
|
|
22
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,2BAA2B;AAC3B,cAAc,SAAS,CAAC;AAIxB,OAAO,EAAE,iBAAiB,EAAE,MAAM,mCAAmC,CAAC;AACtE,OAAO,EAAE,gBAAgB,EAAE,MAAM,kCAAkC,CAAC;AAOpE,OAAO,EAAE,kBAAkB,EAAE,MAAM,oCAAoC,CAAC;AACxE,OAAO,EAAE,yBAAyB,EAAE,MAAM,qDAAqD,CAAC;AAQhG,OAAO,EAAE,kBAAkB,EAAE,MAAM,qCAAqC,CAAC;AAEzE,sBAAsB;AACtB,OAAO,EAAE,YAAY,EAAE,MAAM,qBAAqB,CAAC;AAEnD,OAAO,EAAE,aAAa,EAAE,MAAM,sBAAsB,CAAC;AAErD,OAAO,EAAE,aAAa,EAAE,MAAM,sBAAsB,CAAC;AAErD,OAAO,EACL,gBAAgB,EAChB,qBAAqB,GACtB,MAAM,yBAAyB,CAAC;AAEjC,kBAAkB;AAClB,cAAc,SAAS,CAAC;AACxB,OAAO,EAAE,mBAAmB,EAAE,MAAM,iBAAiB,CAAC;AAEtD,oBAAoB;AACpB,cAAc,aAAa,CAAC"}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { Message, MessageWithVision } from '../../types';
|
|
2
|
+
/**
|
|
3
|
+
* Chat service interface
|
|
4
|
+
* Abstracts interaction with AI models
|
|
5
|
+
*/
|
|
6
|
+
export interface ChatService {
|
|
7
|
+
/**
|
|
8
|
+
* Process chat messages
|
|
9
|
+
* @param messages Array of messages to send
|
|
10
|
+
* @param onPartialResponse Callback to receive each part of streaming response
|
|
11
|
+
* @param onCompleteResponse Callback to execute when response is complete
|
|
12
|
+
*/
|
|
13
|
+
processChat(messages: Message[], onPartialResponse: (text: string) => void, onCompleteResponse: (text: string) => Promise<void>): Promise<void>;
|
|
14
|
+
/**
|
|
15
|
+
* Process chat messages with images
|
|
16
|
+
* @param messages Array of messages to send (including images)
|
|
17
|
+
* @param onPartialResponse Callback to receive each part of streaming response
|
|
18
|
+
* @param onCompleteResponse Callback to execute when response is complete
|
|
19
|
+
*/
|
|
20
|
+
processVisionChat(messages: MessageWithVision[], onPartialResponse: (text: string) => void, onCompleteResponse: (text: string) => Promise<void>): Promise<void>;
|
|
21
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ChatService.js","sourceRoot":"","sources":["../../../src/services/chat/ChatService.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { ChatService } from './ChatService';
|
|
2
|
+
import { ChatServiceOptions, ChatServiceProvider } from './providers/ChatServiceProvider';
|
|
3
|
+
/**
|
|
4
|
+
* Chat service factory
|
|
5
|
+
* Manages and creates various AI providers
|
|
6
|
+
*/
|
|
7
|
+
export declare class ChatServiceFactory {
|
|
8
|
+
/** Map of registered providers */
|
|
9
|
+
private static providers;
|
|
10
|
+
/**
|
|
11
|
+
* Register a new provider
|
|
12
|
+
* @param provider Provider instance
|
|
13
|
+
*/
|
|
14
|
+
static registerProvider(provider: ChatServiceProvider): void;
|
|
15
|
+
/**
|
|
16
|
+
* Create a chat service with the specified provider name and options
|
|
17
|
+
* @param providerName Provider name
|
|
18
|
+
* @param options Service options
|
|
19
|
+
* @returns Created ChatService instance
|
|
20
|
+
*/
|
|
21
|
+
static createChatService(providerName: string, options: ChatServiceOptions): ChatService;
|
|
22
|
+
/**
|
|
23
|
+
* Get registered providers
|
|
24
|
+
* @returns Provider map
|
|
25
|
+
*/
|
|
26
|
+
static getProviders(): Map<string, ChatServiceProvider>;
|
|
27
|
+
/**
|
|
28
|
+
* Get array of available provider names
|
|
29
|
+
* @returns Array of provider names
|
|
30
|
+
*/
|
|
31
|
+
static getAvailableProviders(): string[];
|
|
32
|
+
/**
|
|
33
|
+
* Get models supported by the specified provider
|
|
34
|
+
* @param providerName Provider name
|
|
35
|
+
* @returns Array of supported models, empty array if provider doesn't exist
|
|
36
|
+
*/
|
|
37
|
+
static getSupportedModels(providerName: string): string[];
|
|
38
|
+
}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { OpenAIChatServiceProvider } from './providers/OpenAIChatServiceProvider';
|
|
2
|
+
/**
|
|
3
|
+
* Chat service factory
|
|
4
|
+
* Manages and creates various AI providers
|
|
5
|
+
*/
|
|
6
|
+
export class ChatServiceFactory {
|
|
7
|
+
/**
|
|
8
|
+
* Register a new provider
|
|
9
|
+
* @param provider Provider instance
|
|
10
|
+
*/
|
|
11
|
+
static registerProvider(provider) {
|
|
12
|
+
this.providers.set(provider.getProviderName(), provider);
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Create a chat service with the specified provider name and options
|
|
16
|
+
* @param providerName Provider name
|
|
17
|
+
* @param options Service options
|
|
18
|
+
* @returns Created ChatService instance
|
|
19
|
+
*/
|
|
20
|
+
static createChatService(providerName, options) {
|
|
21
|
+
const provider = this.providers.get(providerName);
|
|
22
|
+
if (!provider) {
|
|
23
|
+
throw new Error(`Unknown chat provider: ${providerName}`);
|
|
24
|
+
}
|
|
25
|
+
return provider.createChatService(options);
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Get registered providers
|
|
29
|
+
* @returns Provider map
|
|
30
|
+
*/
|
|
31
|
+
static getProviders() {
|
|
32
|
+
return this.providers;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Get array of available provider names
|
|
36
|
+
* @returns Array of provider names
|
|
37
|
+
*/
|
|
38
|
+
static getAvailableProviders() {
|
|
39
|
+
return Array.from(this.providers.keys());
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Get models supported by the specified provider
|
|
43
|
+
* @param providerName Provider name
|
|
44
|
+
* @returns Array of supported models, empty array if provider doesn't exist
|
|
45
|
+
*/
|
|
46
|
+
static getSupportedModels(providerName) {
|
|
47
|
+
const provider = this.providers.get(providerName);
|
|
48
|
+
return provider ? provider.getSupportedModels() : [];
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
/** Map of registered providers */
|
|
52
|
+
ChatServiceFactory.providers = new Map();
|
|
53
|
+
// Register OpenAI as the default provider
|
|
54
|
+
ChatServiceFactory.registerProvider(new OpenAIChatServiceProvider());
|
|
55
|
+
//# sourceMappingURL=ChatServiceFactory.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ChatServiceFactory.js","sourceRoot":"","sources":["../../../src/services/chat/ChatServiceFactory.ts"],"names":[],"mappings":"AAKA,OAAO,EAAE,yBAAyB,EAAE,MAAM,uCAAuC,CAAC;AAElF;;;GAGG;AACH,MAAM,OAAO,kBAAkB;IAI7B;;;OAGG;IACH,MAAM,CAAC,gBAAgB,CAAC,QAA6B;QACnD,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,QAAQ,CAAC,eAAe,EAAE,EAAE,QAAQ,CAAC,CAAC;IAC3D,CAAC;IAED;;;;;OAKG;IACH,MAAM,CAAC,iBAAiB,CACtB,YAAoB,EACpB,OAA2B;QAE3B,MAAM,QAAQ,GAAG,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,YAAY,CAAC,CAAC;QAClD,IAAI,CAAC,QAAQ,EAAE,CAAC;YACd,MAAM,IAAI,KAAK,CAAC,0BAA0B,YAAY,EAAE,CAAC,CAAC;QAC5D,CAAC;QACD,OAAO,QAAQ,CAAC,iBAAiB,CAAC,OAAO,CAAC,CAAC;IAC7C,CAAC;IAED;;;OAGG;IACH,MAAM,CAAC,YAAY;QACjB,OAAO,IAAI,CAAC,SAAS,CAAC;IACxB,CAAC;IAED;;;OAGG;IACH,MAAM,CAAC,qBAAqB;QAC1B,OAAO,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,SAAS,CAAC,IAAI,EAAE,CAAC,CAAC;IAC3C,CAAC;IAED;;;;OAIG;IACH,MAAM,CAAC,kBAAkB,CAAC,YAAoB;QAC5C,MAAM,QAAQ,GAAG,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,YAAY,CAAC,CAAC;QAClD,OAAO,QAAQ,CAAC,CAAC,CAAC,QAAQ,CAAC,kBAAkB,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;IACvD,CAAC;;AApDD,kCAAkC;AACnB,4BAAS,GAAqC,IAAI,GAAG,EAAE,CAAC;AAsDzE,0CAA0C;AAC1C,kBAAkB,CAAC,gBAAgB,CAAC,IAAI,yBAAyB,EAAE,CAAC,CAAC"}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { ChatService } from './ChatService';
|
|
2
|
+
import { Message, MessageWithVision } from '../../types';
|
|
3
|
+
/**
|
|
4
|
+
* OpenAI implementation of ChatService
|
|
5
|
+
*/
|
|
6
|
+
export declare class OpenAIChatService implements ChatService {
|
|
7
|
+
private apiKey;
|
|
8
|
+
private model;
|
|
9
|
+
private visionModel;
|
|
10
|
+
/** Provider name */
|
|
11
|
+
readonly provider: string;
|
|
12
|
+
/**
|
|
13
|
+
* Constructor
|
|
14
|
+
* @param apiKey OpenAI API key
|
|
15
|
+
* @param model Name of the model to use
|
|
16
|
+
* @param visionModel Name of the vision model
|
|
17
|
+
*/
|
|
18
|
+
constructor(apiKey: string, model?: string, visionModel?: string);
|
|
19
|
+
/**
|
|
20
|
+
* Get the current model name
|
|
21
|
+
* @returns Model name
|
|
22
|
+
*/
|
|
23
|
+
getModel(): string;
|
|
24
|
+
/**
|
|
25
|
+
* Process chat messages
|
|
26
|
+
* @param messages Array of messages to send
|
|
27
|
+
* @param onPartialResponse Callback to receive each part of streaming response
|
|
28
|
+
* @param onCompleteResponse Callback to execute when response is complete
|
|
29
|
+
*/
|
|
30
|
+
processChat(messages: Message[], onPartialResponse: (text: string) => void, onCompleteResponse: (text: string) => Promise<void>): Promise<void>;
|
|
31
|
+
/**
|
|
32
|
+
* Process chat messages with images
|
|
33
|
+
* @param messages Array of messages to send (including images)
|
|
34
|
+
* @param onPartialResponse Callback to receive each part of streaming response
|
|
35
|
+
* @param onCompleteResponse Callback to execute when response is complete
|
|
36
|
+
*/
|
|
37
|
+
processVisionChat(messages: MessageWithVision[], onPartialResponse: (text: string) => void, onCompleteResponse: (text: string) => Promise<void>): Promise<void>;
|
|
38
|
+
}
|
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
import { ENDPOINT_OPENAI_CHAT_COMPLETIONS_API, MODEL_GPT_4O_MINI } from '../../constants';
|
|
2
|
+
/**
|
|
3
|
+
* OpenAI implementation of ChatService
|
|
4
|
+
*/
|
|
5
|
+
export class OpenAIChatService {
|
|
6
|
+
/**
|
|
7
|
+
* Constructor
|
|
8
|
+
* @param apiKey OpenAI API key
|
|
9
|
+
* @param model Name of the model to use
|
|
10
|
+
* @param visionModel Name of the vision model
|
|
11
|
+
*/
|
|
12
|
+
constructor(apiKey, model = MODEL_GPT_4O_MINI, visionModel = MODEL_GPT_4O_MINI) {
|
|
13
|
+
/** Provider name */
|
|
14
|
+
this.provider = 'openai';
|
|
15
|
+
this.apiKey = apiKey;
|
|
16
|
+
this.model = model;
|
|
17
|
+
this.visionModel = visionModel;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Get the current model name
|
|
21
|
+
* @returns Model name
|
|
22
|
+
*/
|
|
23
|
+
getModel() {
|
|
24
|
+
return this.model;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Process chat messages
|
|
28
|
+
* @param messages Array of messages to send
|
|
29
|
+
* @param onPartialResponse Callback to receive each part of streaming response
|
|
30
|
+
* @param onCompleteResponse Callback to execute when response is complete
|
|
31
|
+
*/
|
|
32
|
+
async processChat(messages, onPartialResponse, onCompleteResponse) {
|
|
33
|
+
try {
|
|
34
|
+
// Convert messages to API request format
|
|
35
|
+
const apiMessages = messages.map((msg) => ({
|
|
36
|
+
role: msg.role,
|
|
37
|
+
content: msg.content,
|
|
38
|
+
}));
|
|
39
|
+
// Request to OpenAI API
|
|
40
|
+
const response = await fetch(ENDPOINT_OPENAI_CHAT_COMPLETIONS_API, {
|
|
41
|
+
method: 'POST',
|
|
42
|
+
headers: {
|
|
43
|
+
'Content-Type': 'application/json',
|
|
44
|
+
Authorization: `Bearer ${this.apiKey}`,
|
|
45
|
+
},
|
|
46
|
+
body: JSON.stringify({
|
|
47
|
+
model: this.model,
|
|
48
|
+
messages: apiMessages,
|
|
49
|
+
stream: true,
|
|
50
|
+
}),
|
|
51
|
+
});
|
|
52
|
+
if (!response.ok) {
|
|
53
|
+
const errorData = await response.json();
|
|
54
|
+
throw new Error(`OpenAI API error: ${errorData.error?.message || response.statusText}`);
|
|
55
|
+
}
|
|
56
|
+
// Process streaming response
|
|
57
|
+
const reader = response.body?.getReader();
|
|
58
|
+
const decoder = new TextDecoder('utf-8');
|
|
59
|
+
let fullText = '';
|
|
60
|
+
if (!reader) {
|
|
61
|
+
throw new Error('Failed to get response reader');
|
|
62
|
+
}
|
|
63
|
+
while (true) {
|
|
64
|
+
const { done, value } = await reader.read();
|
|
65
|
+
if (done)
|
|
66
|
+
break;
|
|
67
|
+
const chunk = decoder.decode(value);
|
|
68
|
+
const lines = chunk
|
|
69
|
+
.split('\n')
|
|
70
|
+
.filter((line) => line.trim() !== '' && line.trim() !== 'data: [DONE]');
|
|
71
|
+
for (const line of lines) {
|
|
72
|
+
try {
|
|
73
|
+
if (!line.startsWith('data: '))
|
|
74
|
+
continue;
|
|
75
|
+
const jsonStr = line.slice(6); // Remove 'data: ' prefix
|
|
76
|
+
if (jsonStr === '[DONE]')
|
|
77
|
+
continue;
|
|
78
|
+
const json = JSON.parse(jsonStr);
|
|
79
|
+
const content = json.choices[0]?.delta?.content || '';
|
|
80
|
+
if (content) {
|
|
81
|
+
fullText += content;
|
|
82
|
+
onPartialResponse(content);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
catch (e) {
|
|
86
|
+
console.error('Error parsing stream:', e);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
// Complete response callback
|
|
91
|
+
await onCompleteResponse(fullText);
|
|
92
|
+
}
|
|
93
|
+
catch (error) {
|
|
94
|
+
console.error('Error in processChat:', error);
|
|
95
|
+
throw error;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* Process chat messages with images
|
|
100
|
+
* @param messages Array of messages to send (including images)
|
|
101
|
+
* @param onPartialResponse Callback to receive each part of streaming response
|
|
102
|
+
* @param onCompleteResponse Callback to execute when response is complete
|
|
103
|
+
*/
|
|
104
|
+
async processVisionChat(messages, onPartialResponse, onCompleteResponse) {
|
|
105
|
+
try {
|
|
106
|
+
const response = await fetch(ENDPOINT_OPENAI_CHAT_COMPLETIONS_API, {
|
|
107
|
+
method: 'POST',
|
|
108
|
+
headers: {
|
|
109
|
+
'Content-Type': 'application/json',
|
|
110
|
+
Authorization: `Bearer ${this.apiKey}`,
|
|
111
|
+
},
|
|
112
|
+
body: JSON.stringify({
|
|
113
|
+
model: this.visionModel,
|
|
114
|
+
messages: messages,
|
|
115
|
+
max_tokens: 1000,
|
|
116
|
+
stream: true,
|
|
117
|
+
}),
|
|
118
|
+
});
|
|
119
|
+
if (!response.ok) {
|
|
120
|
+
const errorData = await response.json();
|
|
121
|
+
throw new Error(`OpenAI API error: ${errorData.error?.message || response.statusText}`);
|
|
122
|
+
}
|
|
123
|
+
// Process streaming response
|
|
124
|
+
const reader = response.body?.getReader();
|
|
125
|
+
const decoder = new TextDecoder('utf-8');
|
|
126
|
+
let fullText = '';
|
|
127
|
+
if (!reader) {
|
|
128
|
+
throw new Error('Failed to get response reader');
|
|
129
|
+
}
|
|
130
|
+
while (true) {
|
|
131
|
+
const { done, value } = await reader.read();
|
|
132
|
+
if (done)
|
|
133
|
+
break;
|
|
134
|
+
const chunk = decoder.decode(value);
|
|
135
|
+
const lines = chunk
|
|
136
|
+
.split('\n')
|
|
137
|
+
.filter((line) => line.trim() !== '' && line.trim() !== 'data: [DONE]');
|
|
138
|
+
for (const line of lines) {
|
|
139
|
+
try {
|
|
140
|
+
if (!line.startsWith('data: '))
|
|
141
|
+
continue;
|
|
142
|
+
const jsonStr = line.slice(6); // Remove 'data: ' prefix
|
|
143
|
+
if (jsonStr === '[DONE]')
|
|
144
|
+
continue;
|
|
145
|
+
const json = JSON.parse(jsonStr);
|
|
146
|
+
const content = json.choices[0]?.delta?.content || '';
|
|
147
|
+
if (content) {
|
|
148
|
+
fullText += content;
|
|
149
|
+
onPartialResponse(content);
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
catch (e) {
|
|
153
|
+
console.error('Error parsing stream:', e);
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
// Complete response callback
|
|
158
|
+
await onCompleteResponse(fullText);
|
|
159
|
+
}
|
|
160
|
+
catch (error) {
|
|
161
|
+
console.error('Error in processVisionChat:', error);
|
|
162
|
+
throw error;
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
//# sourceMappingURL=OpenAIChatService.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"OpenAIChatService.js","sourceRoot":"","sources":["../../../src/services/chat/OpenAIChatService.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,oCAAoC,EAAE,iBAAiB,EAAE,MAAM,iBAAiB,CAAC;AAE1F;;GAEG;AACH,MAAM,OAAO,iBAAiB;IAO5B;;;;;OAKG;IACH,YACE,MAAc,E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AAM,kBAAkB,CAAC,QAAQ,CAAC,CAAC;QACrC,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,OAAO,CAAC,KAAK,CAAC,6BAA6B,EAAE,KAAK,CAAC,CAAC;YACpD,MAAM,KAAK,CAAC;QACd,CAAC;IACH,CAAC;CACF"}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { Message } from '../../types';
|
|
2
|
+
import { Summarizer } from '../../core/MemoryManager';
|
|
3
|
+
/**
|
|
4
|
+
* Implementation of summarization functionality using OpenAI
|
|
5
|
+
*/
|
|
6
|
+
export declare class OpenAISummarizer implements Summarizer {
|
|
7
|
+
private apiKey;
|
|
8
|
+
private model;
|
|
9
|
+
private defaultPromptTemplate;
|
|
10
|
+
/**
|
|
11
|
+
* Constructor
|
|
12
|
+
* @param apiKey OpenAI API key
|
|
13
|
+
* @param model Name of the model to use
|
|
14
|
+
* @param defaultPromptTemplate Default prompt template for summarization
|
|
15
|
+
*/
|
|
16
|
+
constructor(apiKey: string, model?: string, defaultPromptTemplate?: string);
|
|
17
|
+
/**
|
|
18
|
+
* Summarize chat messages
|
|
19
|
+
* @param messages Array of messages to summarize
|
|
20
|
+
* @param maxLength Maximum number of characters (default 256)
|
|
21
|
+
* @param customPrompt Custom prompt template for summarization (optional)
|
|
22
|
+
* @returns Summarized text
|
|
23
|
+
*/
|
|
24
|
+
summarize(messages: Message[], maxLength?: number, customPrompt?: string): Promise<string>;
|
|
25
|
+
}
|