Last active
April 15, 2025 16:56
-
-
Save rido-min/944b925a76d9ba5f0340d16e0118af65 to your computer and use it in GitHub Desktop.
miniagent.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import express from 'express' | |
import { | |
ActivityTypes, | |
AgentApplication, | |
MemoryStorage, | |
CloudAdapter, | |
authorizeJWT, | |
loadAuthConfigFromEnv } from '@microsoft/agents-hosting' | |
const app = new AgentApplication({ storage: new MemoryStorage() }) | |
app.message('/reset', async (context, state) => { | |
state.conversation.count = 0 | |
await context.sendActivity('Message count reset.') | |
}) | |
app.activity(ActivityTypes.Message, async (context, state) => { | |
let count = state.conversation.count ?? 0 | |
state.conversation.count = ++count | |
await context.sendActivity(`You have sent ${count} messages.`) | |
}) | |
// from here, boilerplate code to run the agent application in express | |
const authConfig = loadAuthConfigFromEnv() | |
const server = express() | |
server.use(express.json()) | |
server.use(authorizeJWT(authConfig)) | |
const adapter = new CloudAdapter(authConfig) | |
server.post('/api/messages', (req, res) => | |
adapter.process(req, res, (context) => | |
app.run(context))) | |
const port = process.env.PORT || 3978 | |
server.listen(port, () => { | |
console.log(`\nServer listening to port ${port} for appId ${authConfig.clientId} debug ${process.env.DEBUG}`) | |
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"type": "module", | |
"dependencies": { | |
"@microsoft/agents-hosting": "^0.1.49", | |
"express": "^5.1.0" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment