Skip to content

Commit 15f9cfc

Browse files
committed
fix: dynamically import @aws-sdk/client-polly to prevent missing module failure on Node 20.x CI
1 parent 6c7d420 commit 15f9cfc

1 file changed

Lines changed: 29 additions & 13 deletions

File tree

server/services/voiceReplyFallback.js

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { PollyClient, SynthesizeSpeechCommand } from "@aws-sdk/client-polly";
21
import { buildSolutionRevealText, isExplicitSolutionRequest } from "../../src/core/tutorSolution.js";
32
import { evaluateBuild } from "./buildEvaluator.js";
43
import { evaluateConcept, isTrivialInteraction } from "./conceptEvaluator.js";
@@ -33,19 +32,27 @@ Voice delivery rules:
3332
- Never mention hidden system logic or internal evaluation.`;
3433

3534
let pollyClient = null;
35+
let PollyClientClass = null;
3636

37-
function getPollyClient() {
37+
async function getPollyClient() {
3838
if (!pollyClient) {
39-
pollyClient = new PollyClient({
40-
region: process.env.AWS_REGION || "us-east-1",
41-
credentials: process.env.AWS_ACCESS_KEY_ID && process.env.AWS_SECRET_ACCESS_KEY
42-
? {
43-
accessKeyId: process.env.AWS_ACCESS_KEY_ID,
44-
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
45-
sessionToken: process.env.AWS_SESSION_TOKEN || undefined,
46-
}
47-
: undefined,
48-
});
39+
try {
40+
const pollyModule = await import("@aws-sdk/client-polly");
41+
PollyClientClass = pollyModule.PollyClient;
42+
pollyClient = new PollyClientClass({
43+
region: process.env.AWS_REGION || "us-east-1",
44+
credentials: process.env.AWS_ACCESS_KEY_ID && process.env.AWS_SECRET_ACCESS_KEY
45+
? {
46+
accessKeyId: process.env.AWS_ACCESS_KEY_ID,
47+
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
48+
sessionToken: process.env.AWS_SESSION_TOKEN || undefined,
49+
}
50+
: undefined,
51+
});
52+
} catch (err) {
53+
console.warn("AWS Polly SDK not available:", err?.message || err);
54+
return null;
55+
}
4956
}
5057
return pollyClient;
5158
}
@@ -140,7 +147,16 @@ function voiceMetaShape(meta = null) {
140147

141148
async function synthesizeAssistantAudio({ assistantText = "", voiceId = null }) {
142149
try {
143-
const pollyResponse = await getPollyClient().send(new SynthesizeSpeechCommand({
150+
const client = await getPollyClient();
151+
if (!client) {
152+
return {
153+
audioChunks: [],
154+
sampleRateHertz: POLLY_PCM_SAMPLE_RATE,
155+
audioFallbackUsed: true,
156+
};
157+
}
158+
const { SynthesizeSpeechCommand } = await import("@aws-sdk/client-polly");
159+
const pollyResponse = await client.send(new SynthesizeSpeechCommand({
144160
Engine: "neural",
145161
OutputFormat: "pcm",
146162
SampleRate: String(POLLY_PCM_SAMPLE_RATE),

0 commit comments

Comments
 (0)