Skip to content

Commit 5872f33

Browse files
committed
remove unncessary env validation; DRY Stuff
1 parent d34e748 commit 5872f33

File tree

3 files changed

+79
-93
lines changed

3 files changed

+79
-93
lines changed

packages/core/src/helper.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { RecursiveCharacterTextSplitter } from "langchain/text_splitter";
88
export function logFunctionCall(functionName: string, runtime?: IAgentRuntime) {
99
elizaLogger.info(`Function call: ${functionName}`, {
1010
functionName,
11-
runtime: JSON.stringify(runtime?.getModelProvider())
11+
// runtime: JSON.stringify(runtime?.getModelProvider())
1212
});
1313
}
1414

packages/plugin-node/src/environment.ts

+8-4
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ import type { IAgentRuntime } from "@elizaos/core";
22
import { z } from "zod";
33

44
export const nodeEnvSchema = z.object({
5-
OPENAI_API_KEY: z.string().min(1, "OpenAI API key is required"),
5+
6+
// TODO: check if single interface can work for these providers
7+
OPENAI_API_KEY: z.string().optional(),
8+
DEEPGRAM_API_KEY: z.string().optional(),
69

710
// Core settings
811
ELEVENLABS_XI_API_KEY: z.string().optional(),
@@ -31,9 +34,10 @@ export async function validateNodeConfig(
3134

3235
// Only include what's absolutely required
3336
const config = {
34-
OPENAI_API_KEY:
35-
runtime.getSetting("OPENAI_API_KEY") ||
36-
process.env.OPENAI_API_KEY,
37+
// Only include OpenAI key if it exists
38+
...(runtime.getSetting("OPENAI_API_KEY") || process.env.OPENAI_API_KEY ? {
39+
OPENAI_API_KEY: runtime.getSetting("OPENAI_API_KEY") || process.env.OPENAI_API_KEY,
40+
} : {}),
3741
ELEVENLABS_XI_API_KEY:
3842
runtime.getSetting("ELEVENLABS_XI_API_KEY") ||
3943
process.env.ELEVENLABS_XI_API_KEY,

packages/plugin-node/src/services/transcription.ts

+70-88
Original file line numberDiff line numberDiff line change
@@ -49,92 +49,6 @@ export class TranscriptionService
4949
private queue: { audioBuffer: ArrayBuffer; resolve: Function }[] = [];
5050
private processing = false;
5151

52-
/**
53-
* CHANGED: initialize() now checks:
54-
* 1) character.settings.transcription (if available and keys exist),
55-
* 2) then the .env TRANSCRIPTION_PROVIDER,
56-
* 3) then old fallback logic (Deepgram -> OpenAI -> local).
57-
*/
58-
async initialize(_runtime: IAgentRuntime): Promise<void> {
59-
this.runtime = _runtime;
60-
61-
// 1) Check character settings
62-
let chosenProvider: TranscriptionProvider | null = null;
63-
const charSetting = this.runtime.character?.settings?.transcription;
64-
65-
if (charSetting === TranscriptionProvider.Deepgram) {
66-
const deepgramKey = this.runtime.getSetting("DEEPGRAM_API_KEY");
67-
if (deepgramKey) {
68-
this.deepgram = createClient(deepgramKey);
69-
chosenProvider = TranscriptionProvider.Deepgram;
70-
}
71-
} else if (charSetting === TranscriptionProvider.OpenAI) {
72-
const openaiKey = this.runtime.getSetting("OPENAI_API_KEY");
73-
if (openaiKey) {
74-
this.openai = new OpenAI({ apiKey: openaiKey });
75-
chosenProvider = TranscriptionProvider.OpenAI;
76-
}
77-
} else if (charSetting === TranscriptionProvider.Local) {
78-
chosenProvider = TranscriptionProvider.Local;
79-
}
80-
81-
// 2) If not chosen from character, check .env
82-
if (!chosenProvider) {
83-
const envProvider = this.runtime.getSetting(
84-
"TRANSCRIPTION_PROVIDER"
85-
);
86-
if (envProvider) {
87-
switch (envProvider.toLowerCase()) {
88-
case "deepgram":
89-
{
90-
const dgKey =
91-
this.runtime.getSetting("DEEPGRAM_API_KEY");
92-
if (dgKey) {
93-
this.deepgram = createClient(dgKey);
94-
chosenProvider = TranscriptionProvider.Deepgram;
95-
}
96-
}
97-
break;
98-
case "openai":
99-
{
100-
const openaiKey =
101-
this.runtime.getSetting("OPENAI_API_KEY");
102-
if (openaiKey) {
103-
this.openai = new OpenAI({ apiKey: openaiKey });
104-
chosenProvider = TranscriptionProvider.OpenAI;
105-
}
106-
}
107-
break;
108-
case "local":
109-
chosenProvider = TranscriptionProvider.Local;
110-
break;
111-
}
112-
}
113-
}
114-
115-
// 3) If still none, fallback to old logic: Deepgram -> OpenAI -> local
116-
if (!chosenProvider) {
117-
const deepgramKey = this.runtime.getSetting("DEEPGRAM_API_KEY");
118-
if (deepgramKey) {
119-
this.deepgram = createClient(deepgramKey);
120-
chosenProvider = TranscriptionProvider.Deepgram;
121-
} else {
122-
const openaiKey = this.runtime.getSetting("OPENAI_API_KEY");
123-
if (openaiKey) {
124-
this.openai = new OpenAI({ apiKey: openaiKey });
125-
chosenProvider = TranscriptionProvider.OpenAI;
126-
} else {
127-
chosenProvider = TranscriptionProvider.Local;
128-
}
129-
}
130-
}
131-
132-
this.transcriptionProvider = chosenProvider;
133-
134-
// Leave detectCuda as is.
135-
this.detectCuda();
136-
}
137-
13852
constructor() {
13953
super();
14054
const rootDir = path.resolve(__dirname, "../../");
@@ -317,14 +231,14 @@ export class TranscriptionService
317231
/**
318232
* Original logic from main is now handled by the final fallback in initialize().
319233
* We'll keep transcribeUsingDefaultLogic() if needed by other code references,
320-
* but its no longer invoked in the new flow.
234+
* but it's no longer invoked in the new flow.
321235
*/
322236
private async transcribeUsingDefaultLogic(
323237
audioBuffer: ArrayBuffer
324238
): Promise<string | null> {
325239
if (this.deepgram) {
326240
return await this.transcribeWithDeepgram(audioBuffer);
327-
} else if (this.openai) {
241+
}if (this.openai) {
328242
return await this.transcribeWithOpenAI(audioBuffer);
329243
}
330244
return await this.transcribeLocally(audioBuffer);
@@ -468,4 +382,72 @@ export class TranscriptionService
468382
return null;
469383
}
470384
}
385+
386+
private async setupProvider(providerType: TranscriptionProvider): Promise<TranscriptionProvider | null> {
387+
switch (providerType) {
388+
case TranscriptionProvider.Deepgram: {
389+
const deepgramKey = this.runtime.getSetting("DEEPGRAM_API_KEY");
390+
if (deepgramKey) {
391+
this.deepgram = createClient(deepgramKey);
392+
return TranscriptionProvider.Deepgram;
393+
}
394+
break;
395+
}
396+
case TranscriptionProvider.OpenAI: {
397+
const openaiKey = this.runtime.getSetting("OPENAI_API_KEY");
398+
if (openaiKey) {
399+
this.openai = new OpenAI({ apiKey: openaiKey });
400+
return TranscriptionProvider.OpenAI;
401+
}
402+
break;
403+
}
404+
case TranscriptionProvider.Local: {
405+
return TranscriptionProvider.Local;
406+
}
407+
}
408+
return null;
409+
}
410+
411+
async initialize(_runtime: IAgentRuntime): Promise<void> {
412+
this.runtime = _runtime;
413+
let chosenProvider: TranscriptionProvider | null = null;
414+
415+
// 1) Check character settings
416+
const charSetting = this.runtime.character?.settings?.transcription;
417+
if (charSetting) {
418+
chosenProvider = await this.setupProvider(charSetting);
419+
}
420+
421+
// 2) If not chosen from character, check .env
422+
if (!chosenProvider) {
423+
const envProvider = this.runtime.getSetting("TRANSCRIPTION_PROVIDER");
424+
if (envProvider) {
425+
switch (envProvider.toLowerCase()) {
426+
case "deepgram":
427+
chosenProvider = await this.setupProvider(TranscriptionProvider.Deepgram);
428+
break;
429+
case "openai":
430+
chosenProvider = await this.setupProvider(TranscriptionProvider.OpenAI);
431+
break;
432+
case "local":
433+
chosenProvider = TranscriptionProvider.Local;
434+
break;
435+
}
436+
}
437+
}
438+
439+
// 3) If still none, try Deepgram first and only check OpenAI if Deepgram fails
440+
if (!chosenProvider) {
441+
chosenProvider = await this.setupProvider(TranscriptionProvider.Deepgram);
442+
if (!chosenProvider) {
443+
chosenProvider = await this.setupProvider(TranscriptionProvider.OpenAI);
444+
}
445+
if (!chosenProvider) {
446+
chosenProvider = TranscriptionProvider.Local;
447+
}
448+
}
449+
450+
this.transcriptionProvider = chosenProvider;
451+
this.detectCuda();
452+
}
471453
}

0 commit comments

Comments
 (0)