Skip to content

Commit 71bc602

Browse files
authored
URI handler (#1820)
Simple client to connect to agent-server and execute request, using a URI format.
1 parent 029bf1d commit 71bc602

File tree

7 files changed

+212
-195
lines changed

7 files changed

+212
-195
lines changed

ts/packages/uriHandler/LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) Microsoft Corporation.
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE

ts/packages/uriHandler/README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# URI Handler
2+
3+
## Trademarks
4+
5+
This project may contain trademarks or logos for projects, products, or services. Authorized use of Microsoft
6+
trademarks or logos is subject to and must follow
7+
[Microsoft's Trademark & Brand Guidelines](https://www.microsoft.com/en-us/legal/intellectualproperty/trademarks/usage/general).
8+
Use of Microsoft trademarks or logos in modified versions of this project must not cause confusion or imply Microsoft sponsorship.
9+
Any use of third-party trademarks or logos are subject to those third-party's policies.
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
{
2+
"name": "uri-handler",
3+
"version": "0.0.1",
4+
"private": true,
5+
"description": "TypeAgent URI handler",
6+
"homepage": "https://github.com/microsoft/TypeAgent#readme",
7+
"repository": {
8+
"type": "git",
9+
"url": "https://github.com/microsoft/TypeAgent.git",
10+
"directory": "ts/packages/uriHandler"
11+
},
12+
"license": "MIT",
13+
"author": "Microsoft",
14+
"type": "module",
15+
"main": "",
16+
"bin": {
17+
"agent-uri-handler": "./bin/index.js"
18+
},
19+
"scripts": {
20+
"build": "npm run tsc",
21+
"clean": "rimraf --glob dist *.tsbuildinfo *.done.build.log",
22+
"prettier": "prettier --check . --ignore-path ../../.prettierignore",
23+
"prettier:fix": "prettier --write . --ignore-path ../../.prettierignore",
24+
"start": "node ./dist/index.js",
25+
"tsc": "tsc -b"
26+
},
27+
"dependencies": {
28+
"@typeagent/agent-server-client": "workspace:*",
29+
"agent-dispatcher": "workspace:*"
30+
},
31+
"devDependencies": {
32+
"@types/debug": "^4.1.12",
33+
"@types/jest": "^29.5.7",
34+
"jest": "^29.7.0",
35+
"prettier": "^3.5.3",
36+
"rimraf": "^6.0.1",
37+
"typescript": "~5.4.5"
38+
}
39+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
4+
import { withConsoleClientIO } from "agent-dispatcher/helpers/console";
5+
import { connectDispatcher } from "@typeagent/agent-server-client";
6+
7+
function parseArgs() {
8+
let port: number = 8999;
9+
let uri: string | undefined = undefined;
10+
for (let i = 2; i < process.argv.length; i++) {
11+
const arg = process.argv[i];
12+
if (!arg.startsWith("-")) {
13+
if (uri !== undefined) {
14+
throw new Error("Multiple URIs provided");
15+
}
16+
uri = arg;
17+
continue;
18+
}
19+
20+
if (arg === "--port" || arg === "-p") {
21+
i++;
22+
if (i >= process.argv.length) {
23+
throw new Error("Missing port number after " + arg);
24+
}
25+
port = parseInt(process.argv[i], 10);
26+
if (isNaN(port) || port <= 0 || port >= 65536) {
27+
throw new Error("Invalid port number: " + process.argv[i]);
28+
}
29+
} else {
30+
throw new Error("Unknown argument: " + arg);
31+
}
32+
}
33+
34+
return { uri, port };
35+
}
36+
37+
async function run(): Promise<void> {
38+
const { uri, port } = parseArgs();
39+
if (uri) {
40+
console.log(`Processing URI: ${uri}`);
41+
} else {
42+
throw new Error("No URI provided");
43+
}
44+
45+
const url = new URL(uri);
46+
if (url.protocol !== "type-agent:") {
47+
throw new Error("Invalid URI protocol, must be type-agent://");
48+
}
49+
50+
const request = url.searchParams.get("request");
51+
if (!request) {
52+
throw new Error("No request found in URI");
53+
}
54+
55+
await withConsoleClientIO(async (clientIO) => {
56+
const dispatcher = await connectDispatcher(
57+
clientIO,
58+
`ws://localhost:${port}`,
59+
);
60+
try {
61+
console.log(`Sending request: ${request}`);
62+
await dispatcher.processCommand(request);
63+
} finally {
64+
if (dispatcher) {
65+
await dispatcher.close();
66+
}
67+
}
68+
});
69+
}
70+
71+
try {
72+
await run();
73+
// Some background network (like mongo) might keep the process live, exit explicitly.
74+
process.exit(0);
75+
} catch (e: any) {
76+
console.error("Error:", e.message);
77+
process.exit(1);
78+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"extends": "../../../tsconfig.base.json",
3+
"compilerOptions": {
4+
"composite": true,
5+
"rootDir": ".",
6+
"outDir": "../dist"
7+
},
8+
"include": ["./**/*"],
9+
"ts-node": {
10+
"esm": true
11+
}
12+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"extends": "../../tsconfig.base.json",
3+
"compilerOptions": {
4+
"composite": true,
5+
6+
// These settings need to match src/tsconfig.json and needed here for oclif dev mode to work for ts-node to work
7+
"rootDir": "./src",
8+
"outDir": "./dist"
9+
},
10+
"include": [],
11+
"references": [{ "path": "./src" }],
12+
"ts-node": {
13+
"esm": true
14+
}
15+
}

0 commit comments

Comments
 (0)