@microsoft/agents-hosting-express 0.2.14

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 ADDED
@@ -0,0 +1,18 @@
1
+ # microsoft/agents-hosting-express
2
+
3
+ ## Overview
4
+
5
+ Provides integration to host the agent in Express using `startServer`
6
+
7
+ ## Usage
8
+
9
+ ```ts
10
+ import { AgentApplication, TurnState } from '@microsoft/agents-hosting';
11
+ import { startServer } from '@microsoft/agents-hosting-express';
12
+
13
+ const app = new AgentApplication<TurnState>();
14
+ app.message('hello', async (context, state) => {
15
+ await context.sendActivity('Hello, world!');
16
+ });
17
+ startServer(app);
18
+ ```
@@ -0,0 +1 @@
1
+ export * from './startServer';
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./startServer"), exports);
18
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,gDAA6B"}
@@ -0,0 +1,32 @@
1
+ /**
2
+ * Copyright (c) Microsoft Corporation. All rights reserved.
3
+ * Licensed under the MIT License.
4
+ */
5
+ import { AgentApplication, AuthConfiguration, TurnState } from '@microsoft/agents-hosting';
6
+ /**
7
+ * Starts an Express server for handling Agent requests.
8
+ *
9
+ * @param agent - The AgentApplication instance to process incoming activities.
10
+ * @param authConfiguration - Optional custom authentication configuration. If not provided,
11
+ * configuration will be loaded from environment variables.
12
+ * @returns void
13
+ *
14
+ * @remarks
15
+ * This function sets up an Express server with the necessary middleware and routes for handling
16
+ * bot requests. It configures JWT authorization middleware and sets up the message endpoint.
17
+ * The server will listen on the port specified in the environment (or 3978 by default).
18
+ *
19
+ * @example
20
+ * ```typescript
21
+ * import { AgentApplication, TurnState } from '@microsoft/agents-hosting';
22
+ * import { startServer } from '@microsoft/agents-hosting-express';
23
+ *
24
+ * const app = new AgentApplication<TurnState>();
25
+ * app.message('hello', async (context, state) => {
26
+ * await context.sendActivity('Hello, world!');
27
+ * });
28
+ *
29
+ * startServer(app);
30
+ * ```
31
+ */
32
+ export declare const startServer: (agent: AgentApplication<TurnState>, authConfiguration?: AuthConfiguration) => void;
@@ -0,0 +1,86 @@
1
+ "use strict";
2
+ /**
3
+ * Copyright (c) Microsoft Corporation. All rights reserved.
4
+ * Licensed under the MIT License.
5
+ */
6
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
7
+ if (k2 === undefined) k2 = k;
8
+ var desc = Object.getOwnPropertyDescriptor(m, k);
9
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
10
+ desc = { enumerable: true, get: function() { return m[k]; } };
11
+ }
12
+ Object.defineProperty(o, k2, desc);
13
+ }) : (function(o, m, k, k2) {
14
+ if (k2 === undefined) k2 = k;
15
+ o[k2] = m[k];
16
+ }));
17
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
18
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
19
+ }) : function(o, v) {
20
+ o["default"] = v;
21
+ });
22
+ var __importStar = (this && this.__importStar) || (function () {
23
+ var ownKeys = function(o) {
24
+ ownKeys = Object.getOwnPropertyNames || function (o) {
25
+ var ar = [];
26
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
27
+ return ar;
28
+ };
29
+ return ownKeys(o);
30
+ };
31
+ return function (mod) {
32
+ if (mod && mod.__esModule) return mod;
33
+ var result = {};
34
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
35
+ __setModuleDefault(result, mod);
36
+ return result;
37
+ };
38
+ })();
39
+ var __importDefault = (this && this.__importDefault) || function (mod) {
40
+ return (mod && mod.__esModule) ? mod : { "default": mod };
41
+ };
42
+ Object.defineProperty(exports, "__esModule", { value: true });
43
+ exports.startServer = void 0;
44
+ const express_1 = __importDefault(require("express"));
45
+ const agents_hosting_1 = require("@microsoft/agents-hosting");
46
+ /**
47
+ * Starts an Express server for handling Agent requests.
48
+ *
49
+ * @param agent - The AgentApplication instance to process incoming activities.
50
+ * @param authConfiguration - Optional custom authentication configuration. If not provided,
51
+ * configuration will be loaded from environment variables.
52
+ * @returns void
53
+ *
54
+ * @remarks
55
+ * This function sets up an Express server with the necessary middleware and routes for handling
56
+ * bot requests. It configures JWT authorization middleware and sets up the message endpoint.
57
+ * The server will listen on the port specified in the environment (or 3978 by default).
58
+ *
59
+ * @example
60
+ * ```typescript
61
+ * import { AgentApplication, TurnState } from '@microsoft/agents-hosting';
62
+ * import { startServer } from '@microsoft/agents-hosting-express';
63
+ *
64
+ * const app = new AgentApplication<TurnState>();
65
+ * app.message('hello', async (context, state) => {
66
+ * await context.sendActivity('Hello, world!');
67
+ * });
68
+ *
69
+ * startServer(app);
70
+ * ```
71
+ */
72
+ const startServer = (agent, authConfiguration) => {
73
+ const authConfig = authConfiguration !== null && authConfiguration !== void 0 ? authConfiguration : (0, agents_hosting_1.loadAuthConfigFromEnv)();
74
+ const adapter = new agents_hosting_1.CloudAdapter(authConfig);
75
+ const server = (0, express_1.default)();
76
+ server.use(express_1.default.json());
77
+ server.use((0, agents_hosting_1.authorizeJWT)(authConfig));
78
+ server.post('/api/messages', (req, res) => adapter.process(req, res, (context) => agent.run(context)));
79
+ const port = process.env.PORT || 3978;
80
+ server.listen(port, async () => {
81
+ const version = (await Promise.resolve().then(() => __importStar(require('@microsoft/agents-hosting/package.json')))).version;
82
+ console.log(`\nServer listening to port ${port} on sdk ${version} for appId ${authConfig.clientId} debug ${process.env.DEBUG}`);
83
+ }).on('error', console.error);
84
+ };
85
+ exports.startServer = startServer;
86
+ //# sourceMappingURL=startServer.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"startServer.js","sourceRoot":"","sources":["../../src/startServer.ts"],"names":[],"mappings":";AAAA;;;GAGG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAEH,sDAA2C;AAC3C,8DAAsJ;AAEtJ;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACI,MAAM,WAAW,GAAG,CAAC,KAAkC,EAAE,iBAAqC,EAAE,EAAE;IACvG,MAAM,UAAU,GAAsB,iBAAiB,aAAjB,iBAAiB,cAAjB,iBAAiB,GAAI,IAAA,sCAAqB,GAAE,CAAA;IAClF,MAAM,OAAO,GAAG,IAAI,6BAAY,CAAC,UAAU,CAAC,CAAA;IAC5C,MAAM,MAAM,GAAG,IAAA,iBAAO,GAAE,CAAA;IACxB,MAAM,CAAC,GAAG,CAAC,iBAAO,CAAC,IAAI,EAAE,CAAC,CAAA;IAC1B,MAAM,CAAC,GAAG,CAAC,IAAA,6BAAY,EAAC,UAAU,CAAC,CAAC,CAAA;IAEpC,MAAM,CAAC,IAAI,CAAC,eAAe,EAAE,CAAC,GAAY,EAAE,GAAa,EAAE,EAAE,CAC3D,OAAO,CAAC,OAAO,CAAC,GAAG,EAAE,GAAG,EAAE,CAAC,OAAO,EAAE,EAAE,CACpC,KAAK,CAAC,GAAG,CAAC,OAAO,CAAC,CACnB,CACF,CAAA;IAED,MAAM,IAAI,GAAG,OAAO,CAAC,GAAG,CAAC,IAAI,IAAI,IAAI,CAAA;IACrC,MAAM,CAAC,MAAM,CAAC,IAAI,EAAE,KAAK,IAAI,EAAE;QAC7B,MAAM,OAAO,GAAG,CAAC,wDAAa,wCAAwC,GAAC,CAAC,CAAC,OAAO,CAAA;QAChF,OAAO,CAAC,GAAG,CAAC,8BAA8B,IAAI,WAAW,OAAO,cAAc,UAAU,CAAC,QAAQ,UAAU,OAAO,CAAC,GAAG,CAAC,KAAK,EAAE,CAAC,CAAA;IACjI,CAAC,CAAC,CAAC,EAAE,CAAC,OAAO,EAAE,OAAO,CAAC,KAAK,CAAC,CAAA;AAC/B,CAAC,CAAA;AAlBY,QAAA,WAAW,eAkBvB"}
package/package.json ADDED
@@ -0,0 +1,44 @@
1
+ {
2
+ "$schema": "https://json.schemastore.org/package.json",
3
+ "name": "@microsoft/agents-hosting-express",
4
+ "version": "0.2.14",
5
+ "homepage": "https://github.com/microsoft/Agents-for-js",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "git+https://github.com/microsoft/Agents-for-js.git"
9
+ },
10
+ "author": {
11
+ "name": "Microsoft",
12
+ "email": "agentssdk@microsoft.com",
13
+ "url": "https://aka.ms/Agents"
14
+ },
15
+ "description": "Microsoft 365 Agents SDK for JavaScript",
16
+ "keywords": [
17
+ "Agents"
18
+ ],
19
+ "main": "dist/src/index.js",
20
+ "types": "dist/src/index.d.ts",
21
+ "dependencies": {
22
+ "@microsoft/agents-hosting": "0.2.14",
23
+ "express": "^5.1.0"
24
+ },
25
+ "license": "MIT",
26
+ "files": [
27
+ "README.md",
28
+ "dist/src",
29
+ "src"
30
+ ],
31
+ "exports": {
32
+ ".": {
33
+ "import": "./dist/src/index.js",
34
+ "require": "./dist/src/index.js"
35
+ },
36
+ "./package.json": "./package.json"
37
+ },
38
+ "engines": {
39
+ "node": ">=18.0.0"
40
+ },
41
+ "devDependencies": {
42
+ "@types/express": "^5.0.1"
43
+ }
44
+ }
package/src/index.ts ADDED
@@ -0,0 +1 @@
1
+ export * from './startServer'
@@ -0,0 +1,53 @@
1
+ /**
2
+ * Copyright (c) Microsoft Corporation. All rights reserved.
3
+ * Licensed under the MIT License.
4
+ */
5
+
6
+ import express, { Response } from 'express'
7
+ import { AgentApplication, AuthConfiguration, authorizeJWT, CloudAdapter, loadAuthConfigFromEnv, Request, TurnState } from '@microsoft/agents-hosting'
8
+
9
+ /**
10
+ * Starts an Express server for handling Agent requests.
11
+ *
12
+ * @param agent - The AgentApplication instance to process incoming activities.
13
+ * @param authConfiguration - Optional custom authentication configuration. If not provided,
14
+ * configuration will be loaded from environment variables.
15
+ * @returns void
16
+ *
17
+ * @remarks
18
+ * This function sets up an Express server with the necessary middleware and routes for handling
19
+ * bot requests. It configures JWT authorization middleware and sets up the message endpoint.
20
+ * The server will listen on the port specified in the environment (or 3978 by default).
21
+ *
22
+ * @example
23
+ * ```typescript
24
+ * import { AgentApplication, TurnState } from '@microsoft/agents-hosting';
25
+ * import { startServer } from '@microsoft/agents-hosting-express';
26
+ *
27
+ * const app = new AgentApplication<TurnState>();
28
+ * app.message('hello', async (context, state) => {
29
+ * await context.sendActivity('Hello, world!');
30
+ * });
31
+ *
32
+ * startServer(app);
33
+ * ```
34
+ */
35
+ export const startServer = (agent: AgentApplication<TurnState>, authConfiguration?: AuthConfiguration) => {
36
+ const authConfig: AuthConfiguration = authConfiguration ?? loadAuthConfigFromEnv()
37
+ const adapter = new CloudAdapter(authConfig)
38
+ const server = express()
39
+ server.use(express.json())
40
+ server.use(authorizeJWT(authConfig))
41
+
42
+ server.post('/api/messages', (req: Request, res: Response) =>
43
+ adapter.process(req, res, (context) =>
44
+ agent.run(context)
45
+ )
46
+ )
47
+
48
+ const port = process.env.PORT || 3978
49
+ server.listen(port, async () => {
50
+ const version = (await import('@microsoft/agents-hosting/package.json')).version
51
+ console.log(`\nServer listening to port ${port} on sdk ${version} for appId ${authConfig.clientId} debug ${process.env.DEBUG}`)
52
+ }).on('error', console.error)
53
+ }