Skip to content
6 changes: 1 addition & 5 deletions nodejs/perplexity/sample-agent/src/agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
MemoryStorage,
TurnContext,
} from "@microsoft/agents-hosting";
import { Activity, ActivityTypes } from "@microsoft/agents-activity";
import { ActivityTypes } from "@microsoft/agents-activity";
import { AgentNotificationActivity } from "@microsoft/agents-a365-notifications";
import { PerplexityAgent } from "./perplexityAgent.js";
import {
Expand Down Expand Up @@ -207,10 +207,6 @@ agentApplication.onActivity(
let count: number = state.conversation.count ?? 0;
state.conversation.count = ++count;

await context.sendActivity(
Activity.fromObject({ type: ActivityTypes.Typing })
);

await perplexityAgent.handleAgentMessageActivity(context, state);
}
);
Expand Down
31 changes: 29 additions & 2 deletions nodejs/perplexity/sample-agent/src/perplexityAgent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,41 @@ export class PerplexityAgent {
return;
}

// Grab streamingResponse if this surface supports it
const streamingResponse = (turnContext as any).streamingResponse;

try {
// Show temporary "I'm working" message with spinner (Playground, and any streaming-enabled client)
if (streamingResponse) {
streamingResponse.queueInformativeUpdate(
"I'm working on your request..."
);
}

const perplexityClient = this.getPerplexityClient();
const response = await perplexityClient.invokeAgentWithScope(userMessage);
await turnContext.sendActivity(response);

if (streamingResponse) {
// Send the final response as a streamed chunk
streamingResponse.queueTextChunk(response);
// Close the stream when done
await streamingResponse.endStream();
} else {
// Fallback for channels that don't support streaming
await turnContext.sendActivity(response);
}
} catch (error) {
console.error("Perplexity query error:", error);
const err = error as any;
await turnContext.sendActivity(`Error: ${err.message || err}`);
const errorMessage = `Error: ${err.message || err}`;

if (streamingResponse) {
// Surface the error through the stream and close it
streamingResponse.queueTextChunk(errorMessage);
await streamingResponse.endStream();
} else {
await turnContext.sendActivity(errorMessage);
}
}
}

Expand Down
Loading