@cogitator-ai/channels 0.2.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 +349 -0
- package/dist/assistant-config.d.ts +92 -0
- package/dist/assistant-config.d.ts.map +1 -0
- package/dist/assistant-config.js +89 -0
- package/dist/assistant-config.js.map +1 -0
- package/dist/capabilities.d.ts +14 -0
- package/dist/capabilities.d.ts.map +1 -0
- package/dist/capabilities.js +28 -0
- package/dist/capabilities.js.map +1 -0
- package/dist/channels/discord.d.ts +26 -0
- package/dist/channels/discord.d.ts.map +1 -0
- package/dist/channels/discord.js +153 -0
- package/dist/channels/discord.js.map +1 -0
- package/dist/channels/slack.d.ts +23 -0
- package/dist/channels/slack.d.ts.map +1 -0
- package/dist/channels/slack.js +89 -0
- package/dist/channels/slack.js.map +1 -0
- package/dist/channels/telegram.d.ts +30 -0
- package/dist/channels/telegram.d.ts.map +1 -0
- package/dist/channels/telegram.js +236 -0
- package/dist/channels/telegram.js.map +1 -0
- package/dist/channels/terminal.d.ts +30 -0
- package/dist/channels/terminal.d.ts.map +1 -0
- package/dist/channels/terminal.js +110 -0
- package/dist/channels/terminal.js.map +1 -0
- package/dist/channels/webchat.d.ts +24 -0
- package/dist/channels/webchat.d.ts.map +1 -0
- package/dist/channels/webchat.js +106 -0
- package/dist/channels/webchat.js.map +1 -0
- package/dist/channels/whatsapp.d.ts +28 -0
- package/dist/channels/whatsapp.d.ts.map +1 -0
- package/dist/channels/whatsapp.js +117 -0
- package/dist/channels/whatsapp.js.map +1 -0
- package/dist/envelope.d.ts +4 -0
- package/dist/envelope.d.ts.map +1 -0
- package/dist/envelope.js +64 -0
- package/dist/envelope.js.map +1 -0
- package/dist/formatters/discord-chunker.d.ts +6 -0
- package/dist/formatters/discord-chunker.d.ts.map +1 -0
- package/dist/formatters/discord-chunker.js +169 -0
- package/dist/formatters/discord-chunker.js.map +1 -0
- package/dist/formatters/markdown.d.ts +5 -0
- package/dist/formatters/markdown.d.ts.map +1 -0
- package/dist/formatters/markdown.js +59 -0
- package/dist/formatters/markdown.js.map +1 -0
- package/dist/formatters/whatsapp-markdown.d.ts +2 -0
- package/dist/formatters/whatsapp-markdown.d.ts.map +1 -0
- package/dist/formatters/whatsapp-markdown.js +26 -0
- package/dist/formatters/whatsapp-markdown.js.map +1 -0
- package/dist/gateway.d.ts +38 -0
- package/dist/gateway.d.ts.map +1 -0
- package/dist/gateway.js +347 -0
- package/dist/gateway.js.map +1 -0
- package/dist/heartbeat.d.ts +28 -0
- package/dist/heartbeat.d.ts.map +1 -0
- package/dist/heartbeat.js +143 -0
- package/dist/heartbeat.js.map +1 -0
- package/dist/hooks.d.ts +4 -0
- package/dist/hooks.d.ts.map +1 -0
- package/dist/hooks.js +30 -0
- package/dist/hooks.js.map +1 -0
- package/dist/inbound-debounce.d.ts +13 -0
- package/dist/inbound-debounce.d.ts.map +1 -0
- package/dist/inbound-debounce.js +61 -0
- package/dist/inbound-debounce.js.map +1 -0
- package/dist/index.d.ts +54 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +33 -0
- package/dist/index.js.map +1 -0
- package/dist/media/deepgram-stt.d.ts +14 -0
- package/dist/media/deepgram-stt.d.ts.map +1 -0
- package/dist/media/deepgram-stt.js +30 -0
- package/dist/media/deepgram-stt.js.map +1 -0
- package/dist/media/index.d.ts +7 -0
- package/dist/media/index.d.ts.map +1 -0
- package/dist/media/index.js +5 -0
- package/dist/media/index.js.map +1 -0
- package/dist/media/media-processor.d.ts +20 -0
- package/dist/media/media-processor.d.ts.map +1 -0
- package/dist/media/media-processor.js +80 -0
- package/dist/media/media-processor.js.map +1 -0
- package/dist/media/whisper-api.d.ts +22 -0
- package/dist/media/whisper-api.d.ts.map +1 -0
- package/dist/media/whisper-api.js +53 -0
- package/dist/media/whisper-api.js.map +1 -0
- package/dist/media/whisper-local.d.ts +16 -0
- package/dist/media/whisper-local.d.ts.map +1 -0
- package/dist/media/whisper-local.js +155 -0
- package/dist/media/whisper-local.js.map +1 -0
- package/dist/media/whisper-tool.d.ts +4 -0
- package/dist/media/whisper-tool.d.ts.map +1 -0
- package/dist/media/whisper-tool.js +19 -0
- package/dist/media/whisper-tool.js.map +1 -0
- package/dist/message-queue.d.ts +17 -0
- package/dist/message-queue.d.ts.map +1 -0
- package/dist/message-queue.js +115 -0
- package/dist/message-queue.js.map +1 -0
- package/dist/middleware/auto-extract.d.ts +36 -0
- package/dist/middleware/auto-extract.d.ts.map +1 -0
- package/dist/middleware/auto-extract.js +73 -0
- package/dist/middleware/auto-extract.js.map +1 -0
- package/dist/middleware/dm-policy.d.ts +39 -0
- package/dist/middleware/dm-policy.d.ts.map +1 -0
- package/dist/middleware/dm-policy.js +177 -0
- package/dist/middleware/dm-policy.js.map +1 -0
- package/dist/middleware/owner-commands.d.ts +25 -0
- package/dist/middleware/owner-commands.d.ts.map +1 -0
- package/dist/middleware/owner-commands.js +131 -0
- package/dist/middleware/owner-commands.js.map +1 -0
- package/dist/middleware/pairing.d.ts +20 -0
- package/dist/middleware/pairing.d.ts.map +1 -0
- package/dist/middleware/pairing.js +70 -0
- package/dist/middleware/pairing.js.map +1 -0
- package/dist/middleware/rate-limit.d.ts +14 -0
- package/dist/middleware/rate-limit.d.ts.map +1 -0
- package/dist/middleware/rate-limit.js +31 -0
- package/dist/middleware/rate-limit.js.map +1 -0
- package/dist/runtime-builder.d.ts +42 -0
- package/dist/runtime-builder.d.ts.map +1 -0
- package/dist/runtime-builder.js +607 -0
- package/dist/runtime-builder.js.map +1 -0
- package/dist/simple-timer-store.d.ts +28 -0
- package/dist/simple-timer-store.d.ts.map +1 -0
- package/dist/simple-timer-store.js +129 -0
- package/dist/simple-timer-store.js.map +1 -0
- package/dist/status-reactions.d.ts +22 -0
- package/dist/status-reactions.d.ts.map +1 -0
- package/dist/status-reactions.js +86 -0
- package/dist/status-reactions.js.map +1 -0
- package/dist/stream-buffer.d.ts +31 -0
- package/dist/stream-buffer.d.ts.map +1 -0
- package/dist/stream-buffer.js +170 -0
- package/dist/stream-buffer.js.map +1 -0
- package/dist/tools/self-config.d.ts +9 -0
- package/dist/tools/self-config.d.ts.map +1 -0
- package/dist/tools/self-config.js +122 -0
- package/dist/tools/self-config.js.map +1 -0
- package/package.json +102 -0
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
export class InboundDebouncer {
|
|
2
|
+
config;
|
|
3
|
+
onFlush;
|
|
4
|
+
buffers = new Map();
|
|
5
|
+
defaultDelay;
|
|
6
|
+
constructor(config, onFlush) {
|
|
7
|
+
this.config = config;
|
|
8
|
+
this.onFlush = onFlush;
|
|
9
|
+
this.defaultDelay = config.delayMs ?? 1500;
|
|
10
|
+
}
|
|
11
|
+
enqueue(msg) {
|
|
12
|
+
const key = `${msg.channelType}:${msg.channelId}:${msg.userId}`;
|
|
13
|
+
const delay = this.config.byChannel?.[msg.channelType] ?? this.defaultDelay;
|
|
14
|
+
const existing = this.buffers.get(key);
|
|
15
|
+
if (existing) {
|
|
16
|
+
clearTimeout(existing.timer);
|
|
17
|
+
existing.messages.push(msg);
|
|
18
|
+
existing.timer = setTimeout(() => void this.flush(key), delay);
|
|
19
|
+
}
|
|
20
|
+
else {
|
|
21
|
+
const timer = setTimeout(() => void this.flush(key), delay);
|
|
22
|
+
this.buffers.set(key, { messages: [msg], timer });
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
async flushAll() {
|
|
26
|
+
const keys = [...this.buffers.keys()];
|
|
27
|
+
await Promise.all(keys.map((key) => this.flush(key)));
|
|
28
|
+
}
|
|
29
|
+
dispose() {
|
|
30
|
+
for (const entry of this.buffers.values()) {
|
|
31
|
+
clearTimeout(entry.timer);
|
|
32
|
+
}
|
|
33
|
+
this.buffers.clear();
|
|
34
|
+
}
|
|
35
|
+
async flush(key) {
|
|
36
|
+
const entry = this.buffers.get(key);
|
|
37
|
+
if (!entry)
|
|
38
|
+
return;
|
|
39
|
+
this.buffers.delete(key);
|
|
40
|
+
const messages = entry.messages;
|
|
41
|
+
if (messages.length === 0)
|
|
42
|
+
return;
|
|
43
|
+
const first = messages[0];
|
|
44
|
+
const last = messages[messages.length - 1];
|
|
45
|
+
const merged = {
|
|
46
|
+
id: first.id,
|
|
47
|
+
channelType: first.channelType,
|
|
48
|
+
channelId: first.channelId,
|
|
49
|
+
userId: first.userId,
|
|
50
|
+
userName: first.userName,
|
|
51
|
+
groupId: first.groupId,
|
|
52
|
+
text: messages.map((m) => m.text).join('\n'),
|
|
53
|
+
raw: last.raw,
|
|
54
|
+
};
|
|
55
|
+
const allAttachments = messages.flatMap((m) => m.attachments ?? []);
|
|
56
|
+
if (allAttachments.length > 0)
|
|
57
|
+
merged.attachments = allAttachments;
|
|
58
|
+
await this.onFlush(merged);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
//# sourceMappingURL=inbound-debounce.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"inbound-debounce.js","sourceRoot":"","sources":["../src/inbound-debounce.ts"],"names":[],"mappings":"AAOA,MAAM,OAAO,gBAAgB;IAKR;IACA;IALX,OAAO,GAAG,IAAI,GAAG,EAAuB,CAAC;IAChC,YAAY,CAAS;IAEtC,YACmB,MAAsB,EACtB,OAAkD;QADlD,WAAM,GAAN,MAAM,CAAgB;QACtB,YAAO,GAAP,OAAO,CAA2C;QAEnE,IAAI,CAAC,YAAY,GAAG,MAAM,CAAC,OAAO,IAAI,IAAI,CAAC;IAC7C,CAAC;IAED,OAAO,CAAC,GAAmB;QACzB,MAAM,GAAG,GAAG,GAAG,GAAG,CAAC,WAAW,IAAI,GAAG,CAAC,SAAS,IAAI,GAAG,CAAC,MAAM,EAAE,CAAC;QAChE,MAAM,KAAK,GAAG,IAAI,CAAC,MAAM,CAAC,SAAS,EAAE,CAAC,GAAG,CAAC,WAAW,CAAC,IAAI,IAAI,CAAC,YAAY,CAAC;QAE5E,MAAM,QAAQ,GAAG,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,GAAG,CAAC,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C,GAAG,CAAC,CAAC;QAEzB,MAAM,QAAQ,GAAG,KAAK,CAAC,QAAQ,CAAC;QAChC,IAAI,QAAQ,CAAC,MAAM,KAAK,CAAC;YAAE,OAAO;QAElC,MAAM,KAAK,GAAG,QAAQ,CAAC,CAAC,CAAC,CAAC;QAC1B,MAAM,IAAI,GAAG,QAAQ,CAAC,QAAQ,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC;QAE3C,MAAM,MAAM,GAAmB;YAC7B,EAAE,EAAE,KAAK,CAAC,EAAE;YACZ,WAAW,EAAE,KAAK,CAAC,WAAW;YAC9B,SAAS,EAAE,KAAK,CAAC,SAAS;YAC1B,MAAM,EAAE,KAAK,CAAC,MAAM;YACpB,QAAQ,EAAE,KAAK,CAAC,QAAQ;YACxB,OAAO,EAAE,KAAK,CAAC,OAAO;YACtB,IAAI,EAAE,QAAQ,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC;YAC5C,GAAG,EAAE,IAAI,CAAC,GAAG;SACd,CAAC;QAEF,MAAM,cAAc,GAAG,QAAQ,CAAC,OAAO,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,WAAW,IAAI,EAAE,CAAC,CAAC;QACpE,IAAI,cAAc,CAAC,MAAM,GAAG,CAAC;YAAE,MAAM,CAAC,WAAW,GAAG,cAAc,CAAC;QAEnE,MAAM,IAAI,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC;IAC7B,CAAC;CACF"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
export { Gateway } from './gateway';
|
|
2
|
+
export type { GatewayFullConfig } from './gateway';
|
|
3
|
+
export { StreamBuffer } from './stream-buffer';
|
|
4
|
+
export { HeartbeatScheduler } from './heartbeat';
|
|
5
|
+
export type { HeartbeatConfig } from './heartbeat';
|
|
6
|
+
export { StatusReactionTracker } from './status-reactions';
|
|
7
|
+
export { InboundDebouncer } from './inbound-debounce';
|
|
8
|
+
export { MessageQueue } from './message-queue';
|
|
9
|
+
export { formatEnvelope, formatElapsed } from './envelope';
|
|
10
|
+
export { TerminalChannel, terminalChannel } from './channels/terminal';
|
|
11
|
+
export type { TerminalConfig } from './channels/terminal';
|
|
12
|
+
export { WebChatChannel, webchatChannel } from './channels/webchat';
|
|
13
|
+
export type { WebChatConfig } from './channels/webchat';
|
|
14
|
+
export { TelegramChannel, telegramChannel } from './channels/telegram';
|
|
15
|
+
export type { TelegramConfig } from './channels/telegram';
|
|
16
|
+
export { DiscordChannel, discordChannel } from './channels/discord';
|
|
17
|
+
export type { DiscordConfig } from './channels/discord';
|
|
18
|
+
export { SlackChannel, slackChannel } from './channels/slack';
|
|
19
|
+
export type { SlackConfig } from './channels/slack';
|
|
20
|
+
export { WhatsAppChannel, whatsappChannel } from './channels/whatsapp';
|
|
21
|
+
export type { WhatsAppChannelConfig } from './channels/whatsapp';
|
|
22
|
+
export { PairingMiddleware, pairing } from './middleware/pairing';
|
|
23
|
+
export type { PairingConfig } from './middleware/pairing';
|
|
24
|
+
export { DmPolicyMiddleware, dmPolicy } from './middleware/dm-policy';
|
|
25
|
+
export type { DmPolicyConfig, DmPolicyMode } from './middleware/dm-policy';
|
|
26
|
+
export { RateLimitMiddleware, rateLimit } from './middleware/rate-limit';
|
|
27
|
+
export type { RateLimitConfig } from './middleware/rate-limit';
|
|
28
|
+
export { OwnerCommandsMiddleware, ownerCommands } from './middleware/owner-commands';
|
|
29
|
+
export type { OwnerCommandsConfig, CommandLevel } from './middleware/owner-commands';
|
|
30
|
+
export { createHookRegistry } from './hooks';
|
|
31
|
+
export type { HookName, HookHandler, HookRegistry } from './hooks';
|
|
32
|
+
export { AutoExtractMiddleware, autoExtract } from './middleware/auto-extract';
|
|
33
|
+
export type { AutoExtractConfig, EntityExtractor, ExtractedEntities, } from './middleware/auto-extract';
|
|
34
|
+
export { generateCapabilitiesDoc } from './capabilities';
|
|
35
|
+
export type { CapabilitiesInput } from './capabilities';
|
|
36
|
+
export { RuntimeBuilder } from './runtime-builder';
|
|
37
|
+
export type { AssistantConfig, BuiltRuntime, RuntimeBuilderOpts } from './runtime-builder';
|
|
38
|
+
export { SimpleTimerStore } from './simple-timer-store';
|
|
39
|
+
export { MediaProcessor } from './media/media-processor';
|
|
40
|
+
export type { MediaProcessResult, SttProvider } from './media/media-processor';
|
|
41
|
+
export { LocalWhisper } from './media/whisper-local';
|
|
42
|
+
export { createWhisperDownloadTool } from './media/whisper-tool';
|
|
43
|
+
export { GroqSttProvider, OpenAISttProvider } from './media/whisper-api';
|
|
44
|
+
export type { GroqSttConfig, OpenAISttConfig } from './media/whisper-api';
|
|
45
|
+
export { DeepgramSttProvider } from './media/deepgram-stt';
|
|
46
|
+
export type { DeepgramSttConfig } from './media/deepgram-stt';
|
|
47
|
+
export { createSelfConfigTools } from './tools/self-config';
|
|
48
|
+
export { AssistantConfigSchema, type AssistantConfigInput, type AssistantConfigOutput, } from './assistant-config';
|
|
49
|
+
export { adaptMarkdown, chunkMessage, getPlatformLimit } from './formatters/markdown';
|
|
50
|
+
export { chunkDiscordText } from './formatters/discord-chunker';
|
|
51
|
+
export type { ChunkDiscordTextOpts } from './formatters/discord-chunker';
|
|
52
|
+
export { markdownToWhatsApp } from './formatters/whatsapp-markdown';
|
|
53
|
+
export type { Channel, ChannelMessage, ChannelUser, ChannelType, Attachment, SendOptions, StreamConfig, GatewayConfig, GatewayStats, GatewayMiddleware, MiddlewareContext, StatusPhase, StatusReactionConfig, DebounceConfig, EnvelopeConfig, QueueMode, } from '@cogitator-ai/types';
|
|
54
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AACpC,YAAY,EAAE,iBAAiB,EAAE,MAAM,WA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AAC;AAC1E,OAAO,EAAE,mBAAmB,EAAE,MAAM,sBAAsB,CAAC;AAC3D,YAAY,EAAE,iBAAiB,EAAE,MAAM,sBAAsB,CAAC;AAE9D,OAAO,EAAE,qBAAqB,EAAE,MAAM,qBAAqB,CAAC;AAE5D,OAAO,EACL,qBAAqB,EACrB,KAAK,oBAAoB,EACzB,KAAK,qBAAqB,GAC3B,MAAM,oBAAoB,CAAC;AAE5B,OAAO,EAAE,aAAa,EAAE,YAAY,EAAE,gBAAgB,EAAE,MAAM,uBAAuB,CAAC;AACtF,OAAO,EAAE,gBAAgB,EAAE,MAAM,8BAA8B,CAAC;AAChE,YAAY,EAAE,oBAAoB,EAAE,MAAM,8BAA8B,CAAC;AACzE,OAAO,EAAE,kBAAkB,EAAE,MAAM,gCAAgC,CAAC;AAEpE,YAAY,EACV,OAAO,EACP,cAAc,EACd,WAAW,EACX,WAAW,EACX,UAAU,EACV,WAAW,EACX,YAAY,EACZ,aAAa,EACb,YAAY,EACZ,iBAAiB,EACjB,iBAAiB,EACjB,WAAW,EACX,oBAAoB,EACpB,cAAc,EACd,cAAc,EACd,SAAS,GACV,MAAM,qBAAqB,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
export { Gateway } from './gateway';
|
|
2
|
+
export { StreamBuffer } from './stream-buffer';
|
|
3
|
+
export { HeartbeatScheduler } from './heartbeat';
|
|
4
|
+
export { StatusReactionTracker } from './status-reactions';
|
|
5
|
+
export { InboundDebouncer } from './inbound-debounce';
|
|
6
|
+
export { MessageQueue } from './message-queue';
|
|
7
|
+
export { formatEnvelope, formatElapsed } from './envelope';
|
|
8
|
+
export { TerminalChannel, terminalChannel } from './channels/terminal';
|
|
9
|
+
export { WebChatChannel, webchatChannel } from './channels/webchat';
|
|
10
|
+
export { TelegramChannel, telegramChannel } from './channels/telegram';
|
|
11
|
+
export { DiscordChannel, discordChannel } from './channels/discord';
|
|
12
|
+
export { SlackChannel, slackChannel } from './channels/slack';
|
|
13
|
+
export { WhatsAppChannel, whatsappChannel } from './channels/whatsapp';
|
|
14
|
+
export { PairingMiddleware, pairing } from './middleware/pairing';
|
|
15
|
+
export { DmPolicyMiddleware, dmPolicy } from './middleware/dm-policy';
|
|
16
|
+
export { RateLimitMiddleware, rateLimit } from './middleware/rate-limit';
|
|
17
|
+
export { OwnerCommandsMiddleware, ownerCommands } from './middleware/owner-commands';
|
|
18
|
+
export { createHookRegistry } from './hooks';
|
|
19
|
+
export { AutoExtractMiddleware, autoExtract } from './middleware/auto-extract';
|
|
20
|
+
export { generateCapabilitiesDoc } from './capabilities';
|
|
21
|
+
export { RuntimeBuilder } from './runtime-builder';
|
|
22
|
+
export { SimpleTimerStore } from './simple-timer-store';
|
|
23
|
+
export { MediaProcessor } from './media/media-processor';
|
|
24
|
+
export { LocalWhisper } from './media/whisper-local';
|
|
25
|
+
export { createWhisperDownloadTool } from './media/whisper-tool';
|
|
26
|
+
export { GroqSttProvider, OpenAISttProvider } from './media/whisper-api';
|
|
27
|
+
export { DeepgramSttProvider } from './media/deepgram-stt';
|
|
28
|
+
export { createSelfConfigTools } from './tools/self-config';
|
|
29
|
+
export { AssistantConfigSchema, } from './assistant-config';
|
|
30
|
+
export { adaptMarkdown, chunkMessage, getPlatformLimit } from './formatters/markdown';
|
|
31
|
+
export { chunkDiscordText } from './formatters/discord-chunker';
|
|
32
|
+
export { markdownToWhatsApp } from './formatters/whatsapp-markdown';
|
|
33
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AAGpC,OAAO,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAC;AAE/C,OAAO,EAAE,kBAAkB,EAAE,MAAM,aAAa,CAAC;AAGjD,OAAO,EAAE,qBAAqB,EAAE,MAAM,oBAAoB,CAAC;AA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BAAsB,CAAC;AACjE,OAAO,EAAE,eAAe,EAAE,iBAAiB,EAAE,MAAM,qBAAqB,CAAC;AAEzE,OAAO,EAAE,mBAAmB,EAAE,MAAM,sBAAsB,CAAC;AAG3D,OAAO,EAAE,qBAAqB,EAAE,MAAM,qBAAqB,CAAC;AAE5D,OAAO,EACL,qBAAqB,GAGtB,MAAM,oBAAoB,CAAC;AAE5B,OAAO,EAAE,aAAa,EAAE,YAAY,EAAE,gBAAgB,EAAE,MAAM,uBAAuB,CAAC;AACtF,OAAO,EAAE,gBAAgB,EAAE,MAAM,8BAA8B,CAAC;AAEhE,OAAO,EAAE,kBAAkB,EAAE,MAAM,gCAAgC,CAAC"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { SttProvider } from './media-processor';
|
|
2
|
+
export interface DeepgramSttConfig {
|
|
3
|
+
apiKey: string;
|
|
4
|
+
model?: string;
|
|
5
|
+
language?: string;
|
|
6
|
+
}
|
|
7
|
+
export declare class DeepgramSttProvider implements SttProvider {
|
|
8
|
+
private readonly apiKey;
|
|
9
|
+
private readonly model;
|
|
10
|
+
private readonly language;
|
|
11
|
+
constructor(config: DeepgramSttConfig);
|
|
12
|
+
transcribe(buffer: Buffer, _mimeType: string): Promise<string>;
|
|
13
|
+
}
|
|
14
|
+
//# sourceMappingURL=deepgram-stt.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"deepgram-stt.d.ts","sourceRoot":"","sources":["../../src/media/deepgram-stt.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAErD,MAAM,WAAW,iBAAiB;IAChC,MAAM,EAAE,MAAM,CAAC;IACf,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,QAAQ,CAAC,EAAE,MAAM,CAAC;CACnB;AAED,qBAAa,mBAAoB,YAAW,WAAW;IACrD,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAS;IAChC,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAS;IAC/B,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAqB;gBAElC,MAAM,EAAE,iBAAiB;IAM/B,UAAU,CAAC,MAAM,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;CAwBrE"}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
export class DeepgramSttProvider {
|
|
2
|
+
apiKey;
|
|
3
|
+
model;
|
|
4
|
+
language;
|
|
5
|
+
constructor(config) {
|
|
6
|
+
this.apiKey = config.apiKey;
|
|
7
|
+
this.model = config.model ?? 'nova-3';
|
|
8
|
+
this.language = config.language;
|
|
9
|
+
}
|
|
10
|
+
async transcribe(buffer, _mimeType) {
|
|
11
|
+
const params = new URLSearchParams({ model: this.model });
|
|
12
|
+
if (this.language)
|
|
13
|
+
params.set('language', this.language);
|
|
14
|
+
const res = await fetch(`https://api.deepgram.com/v1/listen?${params}`, {
|
|
15
|
+
method: 'POST',
|
|
16
|
+
headers: {
|
|
17
|
+
Authorization: `Token ${this.apiKey}`,
|
|
18
|
+
'Content-Type': 'audio/ogg',
|
|
19
|
+
},
|
|
20
|
+
body: new Uint8Array(buffer),
|
|
21
|
+
});
|
|
22
|
+
if (!res.ok) {
|
|
23
|
+
const body = await res.text();
|
|
24
|
+
throw new Error(`Deepgram STT failed (${res.status}): ${body}`);
|
|
25
|
+
}
|
|
26
|
+
const data = (await res.json());
|
|
27
|
+
return data.results.channels[0]?.alternatives[0]?.transcript?.trim() ?? '';
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
//# sourceMappingURL=deepgram-stt.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"deepgram-stt.js","sourceRoot":"","sources":["../../src/media/deepgram-stt.ts"],"names":[],"mappings":"AAQA,MAAM,OAAO,mBAAmB;IACb,MAAM,CAAS;IACf,KAAK,CAAS;IACd,QAAQ,CAAqB;IAE9C,YAAY,MAAy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vider } from './media-processor';
|
|
3
|
+
export { LocalWhisper } from './whisper-local';
|
|
4
|
+
export { createWhisperDownloadTool } from './whisper-tool';
|
|
5
|
+
export { GroqSttProvider, OpenAISttProvider } from './whisper-api';
|
|
6
|
+
export type { GroqSttConfig, OpenAISttConfig } from './whisper-api';
|
|
7
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/media/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,mBAAmB,CAAC;AACnD,YAAY,EAAE,kBAAkB,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AACzE,OAAO,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAC;AAC/C,OAAO,EAAE,yBAAyB,EAAE,MAAM,gBAAgB,CAAC;AAC3D,OAAO,EAAE,eAAe,EAAE,iBAAiB,EAAE,MAAM,eAAe,CAAC;AACnE,YAAY,EAAE,aAAa,EAAE,eAAe,EAAE,MAAM,eAAe,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/media/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,mBAAmB,CAAC;AAEnD,OAAO,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAC;AAC/C,OAAO,EAAE,yBAAyB,EAAE,MAAM,gBAAgB,CAAC;AAC3D,OAAO,EAAE,eAAe,EAAE,iBAAiB,EAAE,MAAM,eAAe,CAAC"}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { Attachment, ImageInput } from '@cogitator-ai/types';
|
|
2
|
+
import type { LocalWhisper } from './whisper-local';
|
|
3
|
+
export interface SttProvider {
|
|
4
|
+
transcribe(buffer: Buffer, mimeType: string): Promise<string>;
|
|
5
|
+
}
|
|
6
|
+
export interface MediaProcessResult {
|
|
7
|
+
images: ImageInput[];
|
|
8
|
+
transcribedText: string | null;
|
|
9
|
+
systemNotes: string[];
|
|
10
|
+
}
|
|
11
|
+
export declare class MediaProcessor {
|
|
12
|
+
private whisper;
|
|
13
|
+
private checkVision;
|
|
14
|
+
private sttProvider?;
|
|
15
|
+
constructor(whisper: LocalWhisper | null, checkVision: (modelId: string) => boolean, sttProvider?: (SttProvider | null) | undefined);
|
|
16
|
+
process(attachments: Attachment[], modelId: string): Promise<MediaProcessResult>;
|
|
17
|
+
private transcribeAudio;
|
|
18
|
+
private normalizeImageMime;
|
|
19
|
+
}
|
|
20
|
+
//# sourceMappingURL=media-processor.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"media-processor.d.ts","sourceRoot":"","sources":["../../src/media/media-processor.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,UAAU,EAAE,MAAM,qBAAqB,CAAC;AAClE,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAC;AAEpD,MAAM,WAAW,WAAW;IAC1B,UAAU,CAAC,MAAM,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC;CAC/D;AAED,MAAM,WAAW,kBAAkB;IACjC,MAAM,EAAE,UAAU,EAAE,CAAC;IACrB,eAAe,EAAE,MAAM,GAAG,IAAI,CAAC;IAC/B,WAAW,EAAE,MAAM,EAAE,CAAC;CACvB;AAED,qBAAa,cAAc;IAEvB,OAAO,CAAC,OAAO;IACf,OAAO,CAAC,WAAW;IACnB,OAAO,CAAC,WAAW,CAAC;gBAFZ,OAAO,EAAE,YAAY,GAAG,IAAI,EAC5B,WAAW,EAAE,CAAC,OAAO,EAAE,MAAM,KAAK,OAAO,EACzC,WAAW,CAAC,GAAE,WAAW,GAAG,IAAI,aAAA;IAGpC,OAAO,CAAC,WAAW,EAAE,UAAU,EAAE,EAAE,OAAO,EAAE,MAAM,GAAG,OAAO,CAAC,kBAAkB,CAAC;YA2CxE,eAAe;IAqC7B,OAAO,CAAC,kBAAkB;CAQ3B"}
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
export class MediaProcessor {
|
|
2
|
+
whisper;
|
|
3
|
+
checkVision;
|
|
4
|
+
sttProvider;
|
|
5
|
+
constructor(whisper, checkVision, sttProvider) {
|
|
6
|
+
this.whisper = whisper;
|
|
7
|
+
this.checkVision = checkVision;
|
|
8
|
+
this.sttProvider = sttProvider;
|
|
9
|
+
}
|
|
10
|
+
async process(attachments, modelId) {
|
|
11
|
+
const images = [];
|
|
12
|
+
let transcribedText = null;
|
|
13
|
+
const systemNotes = [];
|
|
14
|
+
const imageAttachments = attachments.filter((a) => a.type === 'image');
|
|
15
|
+
const audioAttachments = attachments.filter((a) => a.type === 'audio');
|
|
16
|
+
if (imageAttachments.length > 0) {
|
|
17
|
+
const hasVision = this.checkVision(modelId);
|
|
18
|
+
if (hasVision) {
|
|
19
|
+
for (const att of imageAttachments) {
|
|
20
|
+
if (att.buffer) {
|
|
21
|
+
const mimeType = this.normalizeImageMime(att.mimeType);
|
|
22
|
+
images.push({
|
|
23
|
+
data: att.buffer.toString('base64'),
|
|
24
|
+
mimeType,
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
else if (att.url) {
|
|
28
|
+
images.push(att.url);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
else {
|
|
33
|
+
systemNotes.push('[System: the user sent an image but your model does not support image recognition. Let them know politely.]');
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
if (audioAttachments.length > 0) {
|
|
37
|
+
const firstAudio = audioAttachments[0];
|
|
38
|
+
if (firstAudio.buffer) {
|
|
39
|
+
transcribedText = await this.transcribeAudio(firstAudio.buffer, firstAudio.mimeType, systemNotes);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
return { images, transcribedText, systemNotes };
|
|
43
|
+
}
|
|
44
|
+
async transcribeAudio(buffer, mimeType, systemNotes) {
|
|
45
|
+
if (this.sttProvider) {
|
|
46
|
+
try {
|
|
47
|
+
return await this.sttProvider.transcribe(buffer, mimeType);
|
|
48
|
+
}
|
|
49
|
+
catch (err) {
|
|
50
|
+
console.error('[media] STT provider failed:', err.message);
|
|
51
|
+
systemNotes.push('[System: the user sent a voice message but transcription failed due to a technical error.]');
|
|
52
|
+
return null;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
if (this.whisper?.isModelDownloaded()) {
|
|
56
|
+
try {
|
|
57
|
+
return await this.whisper.transcribe(buffer, mimeType);
|
|
58
|
+
}
|
|
59
|
+
catch (err) {
|
|
60
|
+
console.error('[media] Whisper transcription failed:', err.message);
|
|
61
|
+
systemNotes.push('[System: the user sent a voice message but transcription failed due to a technical error.]');
|
|
62
|
+
return null;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
systemNotes.push('[System: the user sent a voice message but speech recognition is not set up yet. ' +
|
|
66
|
+
'Suggest downloading it using the download_stt_model tool (~75MB, works offline). ' +
|
|
67
|
+
'Ask the user for confirmation first.]');
|
|
68
|
+
return null;
|
|
69
|
+
}
|
|
70
|
+
normalizeImageMime(mime) {
|
|
71
|
+
if (mime === 'image/png')
|
|
72
|
+
return 'image/png';
|
|
73
|
+
if (mime === 'image/gif')
|
|
74
|
+
return 'image/gif';
|
|
75
|
+
if (mime === 'image/webp')
|
|
76
|
+
return 'image/webp';
|
|
77
|
+
return 'image/jpeg';
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
//# sourceMappingURL=media-processor.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"media-processor.js","sourceRoot":"","sources":["../../src/media/media-processor.ts"],"names":[],"mappings":"AAaA,MAAM,OAAO,cAAc;IAEf;IACA;IACA;IAHV,YACU,OAA4B,EAC5B,WAAyC,EACzC,WAAgC;QAFhC,YAAO,GAAP,OAAO,CAAqB;QAC5B,gBAAW,GAAX,WAAW,CAA8B;QACzC,gB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IAAI,IAAI,KAAK,WAAW;YAAE,OAAO,WAAW,CAAC;QAC7C,IAAI,IAAI,KAAK,WAAW;YAAE,OAAO,WAAW,CAAC;QAC7C,IAAI,IAAI,KAAK,YAAY;YAAE,OAAO,YAAY,CAAC;QAC/C,OAAO,YAAY,CAAC;IACtB,CAAC;CACF"}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { SttProvider } from './media-processor';
|
|
2
|
+
export interface GroqSttConfig {
|
|
3
|
+
apiKey: string;
|
|
4
|
+
model?: string;
|
|
5
|
+
}
|
|
6
|
+
export declare class GroqSttProvider implements SttProvider {
|
|
7
|
+
private readonly apiKey;
|
|
8
|
+
private readonly model;
|
|
9
|
+
constructor(config: GroqSttConfig);
|
|
10
|
+
transcribe(buffer: Buffer, mimeType: string): Promise<string>;
|
|
11
|
+
}
|
|
12
|
+
export interface OpenAISttConfig {
|
|
13
|
+
apiKey: string;
|
|
14
|
+
model?: string;
|
|
15
|
+
}
|
|
16
|
+
export declare class OpenAISttProvider implements SttProvider {
|
|
17
|
+
private readonly apiKey;
|
|
18
|
+
private readonly model;
|
|
19
|
+
constructor(config: OpenAISttConfig);
|
|
20
|
+
transcribe(buffer: Buffer, mimeType: string): Promise<string>;
|
|
21
|
+
}
|
|
22
|
+
//# sourceMappingURL=whisper-api.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"whisper-api.d.ts","sourceRoot":"","sources":["../../src/media/whisper-api.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAErD,MAAM,WAAW,aAAa;IAC5B,MAAM,EAAE,MAAM,CAAC;IACf,KAAK,CAAC,EAAE,MAAM,CAAC;CAChB;AAED,qBAAa,eAAgB,YAAW,WAAW;IACjD,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAS;IAChC,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAS;gBAEnB,MAAM,EAAE,aAAa;IAK3B,UAAU,CAAC,MAAM,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;CAsBpE;AAED,MAAM,WAAW,eAAe;IAC9B,MAAM,EAAE,MAAM,CAAC;IACf,KAAK,CAAC,EAAE,MAAM,CAAC;CAChB;AAED,qBAAa,iBAAkB,YAAW,WAAW;IACnD,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAS;IAChC,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAS;gBAEnB,MAAM,EAAE,eAAe;IAK7B,UAAU,CAAC,MAAM,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;CAsBpE"}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
export class GroqSttProvider {
|
|
2
|
+
apiKey;
|
|
3
|
+
model;
|
|
4
|
+
constructor(config) {
|
|
5
|
+
this.apiKey = config.apiKey;
|
|
6
|
+
this.model = config.model ?? 'whisper-large-v3';
|
|
7
|
+
}
|
|
8
|
+
async transcribe(buffer, mimeType) {
|
|
9
|
+
const ext = mimeType.includes('ogg') ? 'ogg' : mimeType.includes('wav') ? 'wav' : 'ogg';
|
|
10
|
+
const blob = new Blob([new Uint8Array(buffer)], { type: mimeType });
|
|
11
|
+
const form = new FormData();
|
|
12
|
+
form.append('file', blob, `audio.${ext}`);
|
|
13
|
+
form.append('model', this.model);
|
|
14
|
+
const res = await fetch('https://api.groq.com/openai/v1/audio/transcriptions', {
|
|
15
|
+
method: 'POST',
|
|
16
|
+
headers: { Authorization: `Bearer ${this.apiKey}` },
|
|
17
|
+
body: form,
|
|
18
|
+
});
|
|
19
|
+
if (!res.ok) {
|
|
20
|
+
const body = await res.text();
|
|
21
|
+
throw new Error(`Groq STT failed (${res.status}): ${body}`);
|
|
22
|
+
}
|
|
23
|
+
const data = (await res.json());
|
|
24
|
+
return data.text.trim();
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
export class OpenAISttProvider {
|
|
28
|
+
apiKey;
|
|
29
|
+
model;
|
|
30
|
+
constructor(config) {
|
|
31
|
+
this.apiKey = config.apiKey;
|
|
32
|
+
this.model = config.model ?? 'whisper-1';
|
|
33
|
+
}
|
|
34
|
+
async transcribe(buffer, mimeType) {
|
|
35
|
+
const ext = mimeType.includes('ogg') ? 'ogg' : mimeType.includes('wav') ? 'wav' : 'ogg';
|
|
36
|
+
const blob = new Blob([new Uint8Array(buffer)], { type: mimeType });
|
|
37
|
+
const form = new FormData();
|
|
38
|
+
form.append('file', blob, `audio.${ext}`);
|
|
39
|
+
form.append('model', this.model);
|
|
40
|
+
const res = await fetch('https://api.openai.com/v1/audio/transcriptions', {
|
|
41
|
+
method: 'POST',
|
|
42
|
+
headers: { Authorization: `Bearer ${this.apiKey}` },
|
|
43
|
+
body: form,
|
|
44
|
+
});
|
|
45
|
+
if (!res.ok) {
|
|
46
|
+
const body = await res.text();
|
|
47
|
+
throw new Error(`OpenAI STT failed (${res.status}): ${body}`);
|
|
48
|
+
}
|
|
49
|
+
const data = (await res.json());
|
|
50
|
+
return data.text.trim();
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
//# sourceMappingURL=whisper-api.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"whisper-api.js","sourceRoot":"","sources":["../../src/media/whisper-api.ts"],"names":[],"mappings":"AAOA,MAAM,OAAO,eAAe;IACT,MAAM,CAAS;IACf,KAAK,CAAS;IAE/B,YAAY,MAAqB;QAC/B,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC,MAAM,CAAC;QAC5B,IAAI,CAAC,KAAK,GAAG,MAAM,CAAC,KAAK,IAAI,kBAAkB,CAAC;IAClD,CAAC;IAED,KAAK,CAAC,UAAU,CAAC,MAAc,EAAE,QAAgB;QAC/C,MAAM,GAAG,GAAG,QAAQ,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,QAAQ,CA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elDir?: string);
|
|
6
|
+
isModelDownloaded(): boolean;
|
|
7
|
+
download(): Promise<void>;
|
|
8
|
+
transcribe(audioBuffer: Buffer, mimeType: string): Promise<string>;
|
|
9
|
+
private ensureLoaded;
|
|
10
|
+
private ensureDeps;
|
|
11
|
+
private loadTransformers;
|
|
12
|
+
private decodeOgg;
|
|
13
|
+
private decodeWav;
|
|
14
|
+
private resample;
|
|
15
|
+
}
|
|
16
|
+
//# sourceMappingURL=whisper-local.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"whisper-local.d.ts","sourceRoot":"","sources":["../../src/media/whisper-local.ts"],"names":[],"mappings":"AAQA,qBAAa,YAAY;IACvB,OAAO,CAAC,QAAQ,CAAiB;IACjC,OAAO,CAAC,OAAO,CAA8B;IAC7C,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;gBAEd,QAAQ,CAAC,EAAE,MAAM;IAI7B,iBAAiB,IAAI,OAAO;IAgBtB,QAAQ,IAAI,OAAO,CAAC,IAAI,CAAC;IAkBzB,UAAU,CAAC,WAAW,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;YAgC1D,YAAY;YA0BZ,UAAU;YAoBV,gBAAgB;YAIhB,SAAS;IAoBvB,OAAO,CAAC,SAAS;IAyBjB,OAAO,CAAC,QAAQ;CAajB"}
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
import { existsSync, readdirSync } from 'node:fs';
|
|
2
|
+
import { execSync } from 'node:child_process';
|
|
3
|
+
import { join } from 'node:path';
|
|
4
|
+
import { homedir } from 'node:os';
|
|
5
|
+
const WHISPER_MODEL = 'Xenova/whisper-tiny';
|
|
6
|
+
const SAMPLE_RATE = 16000;
|
|
7
|
+
export class LocalWhisper {
|
|
8
|
+
pipeline = null;
|
|
9
|
+
loading = null;
|
|
10
|
+
modelDir;
|
|
11
|
+
constructor(modelDir) {
|
|
12
|
+
this.modelDir = modelDir ?? join(homedir(), '.cogitator', 'models');
|
|
13
|
+
}
|
|
14
|
+
isModelDownloaded() {
|
|
15
|
+
const jsCache = join(this.modelDir, 'Xenova', 'whisper-tiny', 'onnx');
|
|
16
|
+
if (existsSync(jsCache))
|
|
17
|
+
return true;
|
|
18
|
+
const pyCache = join(this.modelDir, 'models--Xenova--whisper-tiny', 'snapshots');
|
|
19
|
+
if (existsSync(pyCache)) {
|
|
20
|
+
try {
|
|
21
|
+
return readdirSync(pyCache).length > 0;
|
|
22
|
+
}
|
|
23
|
+
catch {
|
|
24
|
+
return false;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
return false;
|
|
28
|
+
}
|
|
29
|
+
async download() {
|
|
30
|
+
await this.ensureDeps();
|
|
31
|
+
const transformers = await this.loadTransformers();
|
|
32
|
+
const { env, pipeline } = transformers;
|
|
33
|
+
env.cacheDir = this.modelDir;
|
|
34
|
+
env.allowLocalModels = true;
|
|
35
|
+
console.log(`[whisper] Downloading ${WHISPER_MODEL} to ${this.modelDir}...`);
|
|
36
|
+
this.pipeline = await pipeline('automatic-speech-recognition', WHISPER_MODEL, {
|
|
37
|
+
dtype: 'q8',
|
|
38
|
+
});
|
|
39
|
+
console.log('[whisper] Model downloaded and ready');
|
|
40
|
+
}
|
|
41
|
+
async transcribe(audioBuffer, mimeType) {
|
|
42
|
+
if (!this.pipeline) {
|
|
43
|
+
await this.ensureLoaded();
|
|
44
|
+
}
|
|
45
|
+
let pcmFloat32;
|
|
46
|
+
if (mimeType === 'audio/ogg' || mimeType === 'audio/opus') {
|
|
47
|
+
pcmFloat32 = await this.decodeOgg(audioBuffer);
|
|
48
|
+
}
|
|
49
|
+
else if (mimeType === 'audio/wav' || mimeType === 'audio/wave') {
|
|
50
|
+
pcmFloat32 = this.decodeWav(audioBuffer);
|
|
51
|
+
}
|
|
52
|
+
else {
|
|
53
|
+
pcmFloat32 = await this.decodeOgg(audioBuffer);
|
|
54
|
+
}
|
|
55
|
+
const pipe = this.pipeline;
|
|
56
|
+
const startToken = pipe.model.config.decoder_start_token_id;
|
|
57
|
+
const noTsToken = pipe.model.generation_config.no_timestamps_token_id;
|
|
58
|
+
const result = await pipe(pcmFloat32, {
|
|
59
|
+
decoder_input_ids: [startToken, noTsToken],
|
|
60
|
+
});
|
|
61
|
+
return result.text.trim();
|
|
62
|
+
}
|
|
63
|
+
async ensureLoaded() {
|
|
64
|
+
if (this.pipeline)
|
|
65
|
+
return;
|
|
66
|
+
if (this.loading) {
|
|
67
|
+
await this.loading;
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
this.loading = (async () => {
|
|
71
|
+
const transformers = await this.loadTransformers();
|
|
72
|
+
const { env, pipeline } = transformers;
|
|
73
|
+
env.cacheDir = this.modelDir;
|
|
74
|
+
env.allowLocalModels = true;
|
|
75
|
+
this.pipeline = await pipeline('automatic-speech-recognition', WHISPER_MODEL, {
|
|
76
|
+
dtype: 'q8',
|
|
77
|
+
});
|
|
78
|
+
})();
|
|
79
|
+
await this.loading;
|
|
80
|
+
this.loading = null;
|
|
81
|
+
}
|
|
82
|
+
async ensureDeps() {
|
|
83
|
+
const missing = [];
|
|
84
|
+
try {
|
|
85
|
+
await import('@huggingface/transformers');
|
|
86
|
+
}
|
|
87
|
+
catch {
|
|
88
|
+
missing.push('@huggingface/transformers');
|
|
89
|
+
}
|
|
90
|
+
try {
|
|
91
|
+
await import('ogg-opus-decoder');
|
|
92
|
+
}
|
|
93
|
+
catch {
|
|
94
|
+
missing.push('ogg-opus-decoder');
|
|
95
|
+
}
|
|
96
|
+
if (missing.length === 0)
|
|
97
|
+
return;
|
|
98
|
+
console.log(`[whisper] Installing dependencies: ${missing.join(', ')}...`);
|
|
99
|
+
const cmd = `npm install --no-save ${missing.join(' ')}`;
|
|
100
|
+
execSync(cmd, { stdio: 'pipe', timeout: 120_000 });
|
|
101
|
+
console.log('[whisper] Dependencies installed');
|
|
102
|
+
}
|
|
103
|
+
async loadTransformers() {
|
|
104
|
+
return import('@huggingface/transformers');
|
|
105
|
+
}
|
|
106
|
+
async decodeOgg(buffer) {
|
|
107
|
+
const mod = (await import('ogg-opus-decoder'));
|
|
108
|
+
const OggOpusDecoder = mod.OggOpusDecoder;
|
|
109
|
+
const decoder = new OggOpusDecoder();
|
|
110
|
+
await decoder.ready;
|
|
111
|
+
const result = await decoder.decode(new Uint8Array(buffer));
|
|
112
|
+
decoder.free();
|
|
113
|
+
const samples = result.channelData[0];
|
|
114
|
+
if (result.sampleRate === SAMPLE_RATE)
|
|
115
|
+
return samples;
|
|
116
|
+
return this.resample(samples, result.sampleRate, SAMPLE_RATE);
|
|
117
|
+
}
|
|
118
|
+
decodeWav(buffer) {
|
|
119
|
+
const dataOffset = buffer.indexOf('data') + 8;
|
|
120
|
+
const bitsPerSample = buffer.readUInt16LE(34);
|
|
121
|
+
const sampleRate = buffer.readUInt32LE(24);
|
|
122
|
+
const numChannels = buffer.readUInt16LE(22);
|
|
123
|
+
const dataSize = buffer.readUInt32LE(dataOffset - 4);
|
|
124
|
+
const numSamples = dataSize / (bitsPerSample / 8) / numChannels;
|
|
125
|
+
const samples = new Float32Array(numSamples);
|
|
126
|
+
let offset = dataOffset;
|
|
127
|
+
for (let i = 0; i < numSamples; i++) {
|
|
128
|
+
if (bitsPerSample === 16) {
|
|
129
|
+
samples[i] = buffer.readInt16LE(offset) / 32768;
|
|
130
|
+
offset += 2 * numChannels;
|
|
131
|
+
}
|
|
132
|
+
else if (bitsPerSample === 32) {
|
|
133
|
+
samples[i] = buffer.readFloatLE(offset);
|
|
134
|
+
offset += 4 * numChannels;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
if (sampleRate === SAMPLE_RATE)
|
|
138
|
+
return samples;
|
|
139
|
+
return this.resample(samples, sampleRate, SAMPLE_RATE);
|
|
140
|
+
}
|
|
141
|
+
resample(samples, fromRate, toRate) {
|
|
142
|
+
const ratio = fromRate / toRate;
|
|
143
|
+
const newLength = Math.round(samples.length / ratio);
|
|
144
|
+
const result = new Float32Array(newLength);
|
|
145
|
+
for (let i = 0; i < newLength; i++) {
|
|
146
|
+
const srcIdx = i * ratio;
|
|
147
|
+
const lo = Math.floor(srcIdx);
|
|
148
|
+
const hi = Math.min(lo + 1, samples.length - 1);
|
|
149
|
+
const frac = srcIdx - lo;
|
|
150
|
+
result[i] = samples[lo] * (1 - frac) + samples[hi] * frac;
|
|
151
|
+
}
|
|
152
|
+
return result;
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
//# sourceMappingURL=whisper-local.js.map
|