|
| 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 | +} |
0 commit comments