@stdiobus/workers-registry 1.3.14 → 1.3.16
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 +16 -16
- package/index.js +3 -3
- package/launch/index.js +4 -0
- package/launch/index.js.map +7 -0
- package/out/dist/workers/acp-registry/registry-launcher-client.js +49 -9
- package/out/dist/workers/acp-registry/{registry-launcher-client.cjs.map → registry-launcher-client.js.map} +2 -2
- package/out/dist/workers/acp-registry/registry-launcher-config.json +4 -5
- package/out/dist/workers/acp-worker/index.js +6 -11
- package/out/dist/workers/acp-worker/index.js.map +7 -0
- package/out/dist/workers/echo-worker/echo-worker.js +2 -11
- package/out/dist/workers/echo-worker/{echo-worker.cjs.map → echo-worker.js.map} +2 -2
- package/out/dist/workers/index.d.ts +1 -1
- package/out/dist/workers/index.js +3 -3
- package/out/dist/workers/mcp-echo-server/index.js +2 -11
- package/out/dist/workers/mcp-echo-server/index.js.map +7 -0
- package/out/dist/workers/mcp-to-acp-proxy/proxy.js +2 -11
- package/out/dist/workers/mcp-to-acp-proxy/{proxy.cjs.map → proxy.js.map} +2 -2
- package/out/tsc/workers-registry/acp-worker/src/index.d.ts +14 -0
- package/out/tsc/workers-registry/index.d.ts +1 -1
- package/out/tsc/workers-registry/launch/index.d.ts +30 -0
- package/package.json +25 -5
- package/launcher/index.cjs +0 -4
- package/launcher/index.cjs.map +0 -7
- package/launcher/index.js +0 -12
- package/launcher/index.js.map +0 -7
- package/out/dist/workers/acp-registry/registry-launcher-client.cjs +0 -52
- package/out/dist/workers/acp-worker/index.cjs +0 -46
- package/out/dist/workers/acp-worker/index.cjs.map +0 -7
- package/out/dist/workers/echo-worker/echo-worker.cjs +0 -3
- package/out/dist/workers/mcp-echo-server/index.cjs +0 -45
- package/out/dist/workers/mcp-echo-server/index.cjs.map +0 -7
- package/out/dist/workers/mcp-to-acp-proxy/proxy.cjs +0 -3
- package/out/tsc/workers-registry/launcher/index.d.ts +0 -2
package/README.md
CHANGED
|
@@ -143,7 +143,7 @@ See [stdio Bus kernel repository](https://github.com/stdiobus/stdiobus) for buil
|
|
|
143
143
|
"id": "acp-worker",
|
|
144
144
|
"command": "node",
|
|
145
145
|
"args": [
|
|
146
|
-
"./workers/
|
|
146
|
+
"./stdiobus/node_modules/@stdiobus/workers-registry/launch",
|
|
147
147
|
"acp-worker"
|
|
148
148
|
],
|
|
149
149
|
"instances": 1
|
|
@@ -204,7 +204,7 @@ node ./node_modules/@stdiobus/workers-registry/out/dist/index.js echo-worker
|
|
|
204
204
|
"id": "acp-worker",
|
|
205
205
|
"command": "node",
|
|
206
206
|
"args": [
|
|
207
|
-
"./workers/
|
|
207
|
+
"./stdiobus/node_modules/@stdiobus/workers-registry/launch",
|
|
208
208
|
"acp-worker"
|
|
209
209
|
],
|
|
210
210
|
"instances": 1
|
|
@@ -220,7 +220,7 @@ node ./node_modules/@stdiobus/workers-registry/out/dist/index.js echo-worker
|
|
|
220
220
|
"id": "registry-launcher",
|
|
221
221
|
"command": "node",
|
|
222
222
|
"args": [
|
|
223
|
-
"./workers/
|
|
223
|
+
"./stdiobus/node_modules/@stdiobus/workers-registry/launch",
|
|
224
224
|
"registry-launcher",
|
|
225
225
|
"./api-keys.json"
|
|
226
226
|
],
|
|
@@ -238,7 +238,7 @@ node ./node_modules/@stdiobus/workers-registry/out/dist/index.js echo-worker
|
|
|
238
238
|
"id": "acp-worker",
|
|
239
239
|
"command": "node",
|
|
240
240
|
"args": [
|
|
241
|
-
"./workers/
|
|
241
|
+
"./stdiobus/node_modules/@stdiobus/workers-registry/launch",
|
|
242
242
|
"acp-worker"
|
|
243
243
|
],
|
|
244
244
|
"instances": 2
|
|
@@ -247,7 +247,7 @@ node ./node_modules/@stdiobus/workers-registry/out/dist/index.js echo-worker
|
|
|
247
247
|
"id": "echo-worker",
|
|
248
248
|
"command": "node",
|
|
249
249
|
"args": [
|
|
250
|
-
"./workers/
|
|
250
|
+
"./stdiobus/node_modules/@stdiobus/workers-registry/launch",
|
|
251
251
|
"echo-worker"
|
|
252
252
|
],
|
|
253
253
|
"instances": 1
|
|
@@ -305,7 +305,7 @@ Using Docker:
|
|
|
305
305
|
docker run \
|
|
306
306
|
--name stdiobus-acp \
|
|
307
307
|
-p 9000:9000 \
|
|
308
|
-
-v $(pwd)
|
|
308
|
+
-v $(pwd):/stdiobus:ro \
|
|
309
309
|
-v $(pwd)/workers-registry/acp-worker/acp-worker-config.json:/config.json:ro \
|
|
310
310
|
stdiobus/stdiobus:latest \
|
|
311
311
|
--config /config.json --tcp 0.0.0.0:9000
|
|
@@ -347,7 +347,7 @@ Routes messages to any agent in the [ACP Registry](https://cdn.agentclientprotoc
|
|
|
347
347
|
"id": "registry-launcher",
|
|
348
348
|
"command": "node",
|
|
349
349
|
"args": [
|
|
350
|
-
"./workers/
|
|
350
|
+
"./stdiobus/node_modules/@stdiobus/workers-registry/launch",
|
|
351
351
|
"registry-launcher",
|
|
352
352
|
"./api-keys.json"
|
|
353
353
|
],
|
|
@@ -364,7 +364,7 @@ Using Docker:
|
|
|
364
364
|
docker run \
|
|
365
365
|
--name stdiobus-registry \
|
|
366
366
|
-p 9000:9000 \
|
|
367
|
-
-v $(pwd)
|
|
367
|
+
-v $(pwd):/stdiobus:ro \
|
|
368
368
|
-v $(pwd)/workers-registry/acp-registry/registry-launcher-config.json:/config.json:ro \
|
|
369
369
|
-v $(pwd)/api-keys.json:/api-keys.json:ro \
|
|
370
370
|
stdiobus/stdiobus:latest \
|
|
@@ -396,7 +396,7 @@ IDE (MCP Client) → MCP-to-ACP Proxy → stdio Bus → Registry Launcher → AC
|
|
|
396
396
|
"stdio-bus-acp": {
|
|
397
397
|
"command": "node",
|
|
398
398
|
"args": [
|
|
399
|
-
"./workers/
|
|
399
|
+
"./stdiobus/node_modules/@stdiobus/workers-registry/launch",
|
|
400
400
|
"mcp-to-acp-proxy"
|
|
401
401
|
],
|
|
402
402
|
"env": {
|
|
@@ -436,7 +436,7 @@ Using Docker:
|
|
|
436
436
|
docker run \
|
|
437
437
|
--name stdiobus-echo \
|
|
438
438
|
-p 9000:9000 \
|
|
439
|
-
-v $(pwd)
|
|
439
|
+
-v $(pwd):/stdiobus:ro \
|
|
440
440
|
-v $(pwd)/workers-registry/echo-worker/echo-worker-config.json:/config.json:ro \
|
|
441
441
|
stdiobus/stdiobus:latest \
|
|
442
442
|
--config /config.json --tcp 0.0.0.0:9000
|
|
@@ -545,7 +545,7 @@ stdio Bus kernel is configured via JSON files. This repository includes example
|
|
|
545
545
|
"id": "acp-worker",
|
|
546
546
|
"command": "node",
|
|
547
547
|
"args": [
|
|
548
|
-
"./workers/
|
|
548
|
+
"./stdiobus/node_modules/@stdiobus/workers-registry/launch",
|
|
549
549
|
"acp-worker"
|
|
550
550
|
],
|
|
551
551
|
"instances": 4
|
|
@@ -567,7 +567,7 @@ stdio Bus kernel is configured via JSON files. This repository includes example
|
|
|
567
567
|
"id": "acp-worker",
|
|
568
568
|
"command": "node",
|
|
569
569
|
"args": [
|
|
570
|
-
"./workers/
|
|
570
|
+
"./stdiobus/node_modules/@stdiobus/workers-registry/launch",
|
|
571
571
|
"acp-worker"
|
|
572
572
|
],
|
|
573
573
|
"instances": 2
|
|
@@ -576,7 +576,7 @@ stdio Bus kernel is configured via JSON files. This repository includes example
|
|
|
576
576
|
"id": "echo-worker",
|
|
577
577
|
"command": "node",
|
|
578
578
|
"args": [
|
|
579
|
-
"./workers/
|
|
579
|
+
"./stdiobus/node_modules/@stdiobus/workers-registry/launch",
|
|
580
580
|
"echo-worker"
|
|
581
581
|
],
|
|
582
582
|
"instances": 1
|
|
@@ -658,7 +658,7 @@ npm run test:property
|
|
|
658
658
|
docker run \
|
|
659
659
|
--name stdiobus-test \
|
|
660
660
|
-p 9000:9000 \
|
|
661
|
-
-v $(pwd)
|
|
661
|
+
-v $(pwd):/stdiobus:ro \
|
|
662
662
|
-v $(pwd)/workers-registry/echo-worker/echo-worker-config.json:/config.json:ro \
|
|
663
663
|
stdiobus/stdiobus:latest \
|
|
664
664
|
--config /config.json --tcp 0.0.0.0:9000
|
|
@@ -676,7 +676,7 @@ docker stop stdiobus-test && docker rm stdiobus-test
|
|
|
676
676
|
docker run \
|
|
677
677
|
--name stdiobus-acp-test \
|
|
678
678
|
-p 9000:9000 \
|
|
679
|
-
-v $(pwd)
|
|
679
|
+
-v $(pwd):/stdiobus:ro \
|
|
680
680
|
-v $(pwd)/workers-registry/acp-worker/acp-worker-config.json:/config.json:ro \
|
|
681
681
|
stdiobus/stdiobus:latest \
|
|
682
682
|
--config /config.json --tcp 0.0.0.0:9000
|
|
@@ -694,7 +694,7 @@ docker stop stdiobus-acp-test && docker rm stdiobus-acp-test
|
|
|
694
694
|
docker run \
|
|
695
695
|
--name stdiobus-registry-test \
|
|
696
696
|
-p 9000:9000 \
|
|
697
|
-
-v $(pwd)
|
|
697
|
+
-v $(pwd):/stdiobus:ro \
|
|
698
698
|
-v $(pwd)/workers-registry/acp-registry/registry-launcher-config.json:/config.json:ro \
|
|
699
699
|
-v $(pwd)/api-keys.json:/api-keys.json:ro \
|
|
700
700
|
stdiobus/stdiobus:latest \
|
package/index.js
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
/*
|
|
3
3
|
* Universal Worker Launcher
|
|
4
|
-
*
|
|
4
|
+
*
|
|
5
5
|
* Usage:
|
|
6
6
|
* stdiobus-worker <worker-name>
|
|
7
7
|
* node index.js <worker-name>
|
|
8
|
-
*
|
|
8
|
+
*
|
|
9
9
|
* Examples:
|
|
10
10
|
* stdiobus-worker launcher
|
|
11
11
|
* stdiobus-worker acp-worker
|
|
@@ -28,7 +28,7 @@ if (!workerName) {
|
|
|
28
28
|
|
|
29
29
|
// Map worker names to their entry points
|
|
30
30
|
const workers = {
|
|
31
|
-
'
|
|
31
|
+
'launch': './out/dist/workers/launch/index.js',
|
|
32
32
|
'acp-worker': './out/dist/workers/acp-worker/index.js',
|
|
33
33
|
'echo-worker': './out/dist/workers/echo-worker/echo-worker.js',
|
|
34
34
|
'mcp-echo-server': './out/dist/workers/mcp-echo-server/index.js',
|
package/launch/index.js
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import{fileURLToPath}from"url";import{dirname,join}from"path";import{readFile}from"fs/promises";var __filename=fileURLToPath(import.meta.url);var __dirname=dirname(__filename);var STDIO_BUS_WORKERS={"acp-worker":{path:"../out/dist/workers/acp-worker/index.js",description:"Full ACP protocol implementation with MCP integration"},"acp-registry":{path:"../out/dist/workers/acp-registry/registry-launcher-client.js",description:"Registry Launcher for ACP Registry agents"},"echo-worker":{path:"../out/dist/workers/echo-worker/echo-worker.js",description:"Simple echo worker for testing NDJSON protocol"},"mcp-echo-server":{path:"../out/dist/workers/mcp-echo-server/index.js",description:"MCP server example for testing"},"mcp-to-acp-proxy":{path:"../out/dist/workers/mcp-to-acp-proxy/proxy.js",description:"MCP-to-ACP protocol bridge"}};function showUsage(){console.error("Usage: node index.js <worker-name>");console.error("");console.error("Available workers:");for(const[name,config]of Object.entries(STDIO_BUS_WORKERS)){console.error(` - ${name.padEnd(20)} ${config.description}`)}console.error("");console.error("Examples:");console.error(" node index.js acp-worker");console.error(" node index.js echo-worker");console.error(" node index.js mcp-echo-server")}function isValidWorkerName(name){return name in STDIO_BUS_WORKERS}async function main(){const workerName=process.argv[2];if(!workerName){console.error("Error: Worker name is required\n");showUsage();process.exit(1)}if(!isValidWorkerName(workerName)){console.error(`Error: Unknown worker "${workerName}"
|
|
3
|
+
`);showUsage();process.exit(1)}const workerConfig=STDIO_BUS_WORKERS[workerName];const workerPath=workerConfig.path;const absolutePath=join(__dirname,workerPath);try{await readFile(absolutePath);console.error(`[launcher] Starting worker: ${workerName}`);console.error(`[launcher] Description: ${workerConfig.description}`);console.error(`[launcher] Path: ${absolutePath}`);await import(absolutePath)}catch(error){if(error&&typeof error==="object"&&"code"in error&&error.code==="ENOENT"){console.error(`Error: Worker file not found: ${absolutePath}`);console.error("");console.error('Please run "npm run build" first to compile the workers.');process.exit(1)}const errorMessage=error instanceof Error?error.message:String(error);const errorStack=error instanceof Error?error.stack:void 0;console.error(`Error loading worker "${workerName}":`,errorMessage);if(errorStack){console.error(errorStack)}process.exit(1)}}main().catch(error=>{console.error("Fatal error:",error);process.exit(1)});export{STDIO_BUS_WORKERS,isValidWorkerName,main as launch,showUsage};
|
|
4
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../workers-registry/launch/index.ts"],
|
|
4
|
+
"sourcesContent": ["#!/usr/bin/env node\n\n/*\n * Apache License 2.0\n * Copyright (c) 2025\u2013present Raman Marozau, Target Insight Function.\n * Contact: raman@worktif.com\n *\n * This file is part of the stdio bus protocol reference implementation:\n * stdio_bus_kernel_workers (target: <target_stdio_bus_kernel_workers>).\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n *\n * SPDX-License-Identifier: Apache-2.0\n */\n\n/**\n * Universal worker launcher for stdio Bus Workers Registry\n *\n * Usage:\n * node index.js <worker-name>\n * node index.js acp-worker\n * node index.js echo-worker\n * node index.js mcp-echo-server\n *\n * This script dynamically imports and runs the specified worker from the compiled output.\n */\n\nimport { fileURLToPath } from 'url';\nimport { dirname, join } from 'path';\nimport { readFile } from 'fs/promises';\n\nconst __filename = fileURLToPath(import.meta.url);\nconst __dirname = dirname(__filename);\n\n/**\n * Worker configuration mapping worker names to their entry points\n */\ninterface WorkerConfig {\n readonly path: string;\n readonly description: string;\n}\n\n/**\n * Available workers mapping\n */\nconst STDIO_BUS_WORKERS: Readonly<Record<string, WorkerConfig>> = {\n 'acp-worker': {\n path: '../out/dist/workers/acp-worker/index.js',\n description: 'Full ACP protocol implementation with MCP integration'\n },\n 'acp-registry': {\n path: '../out/dist/workers/acp-registry/registry-launcher-client.js',\n description: 'Registry Launcher for ACP Registry agents'\n },\n 'echo-worker': {\n path: '../out/dist/workers/echo-worker/echo-worker.js',\n description: 'Simple echo worker for testing NDJSON protocol'\n },\n 'mcp-echo-server': {\n path: '../out/dist/workers/mcp-echo-server/index.js',\n description: 'MCP server example for testing'\n },\n 'mcp-to-acp-proxy': {\n path: '../out/dist/workers/mcp-to-acp-proxy/proxy.js',\n description: 'MCP-to-ACP protocol bridge'\n }\n} as const;\n\n/**\n * Worker name type\n */\ntype WorkerName = keyof typeof STDIO_BUS_WORKERS;\n\n/**\n * Display usage information\n */\nfunction showUsage(): void {\n console.error('Usage: node index.js <worker-name>');\n console.error('');\n console.error('Available workers:');\n\n for (const [name, config] of Object.entries(STDIO_BUS_WORKERS)) {\n console.error(` - ${name.padEnd(20)} ${config.description}`);\n }\n\n console.error('');\n console.error('Examples:');\n console.error(' node index.js acp-worker');\n console.error(' node index.js echo-worker');\n console.error(' node index.js mcp-echo-server');\n}\n\n/**\n * Validate worker name\n */\nfunction isValidWorkerName(name: string): name is WorkerName {\n return name in STDIO_BUS_WORKERS;\n}\n\n/**\n * Main entry point\n */\nasync function main(): Promise<void> {\n const workerName = process.argv[2];\n\n // Check if worker name is provided\n if (!workerName) {\n console.error('Error: Worker name is required\\n');\n showUsage();\n process.exit(1);\n }\n\n // Check if worker exists\n if (!isValidWorkerName(workerName)) {\n console.error(`Error: Unknown worker \"${workerName}\"\\n`);\n showUsage();\n process.exit(1);\n }\n\n const workerConfig = STDIO_BUS_WORKERS[workerName];\n const workerPath = workerConfig.path;\n\n // Resolve absolute path\n const absolutePath = join(__dirname, workerPath);\n\n try {\n // Verify the worker file exists\n await readFile(absolutePath);\n\n // Import and run the worker\n console.error(`[launcher] Starting worker: ${workerName}`);\n console.error(`[launcher] Description: ${workerConfig.description}`);\n console.error(`[launcher] Path: ${absolutePath}`);\n\n await import(absolutePath);\n } catch (error) {\n if (error && typeof error === 'object' && 'code' in error && error.code === 'ENOENT') {\n console.error(`Error: Worker file not found: ${absolutePath}`);\n console.error('');\n console.error('Please run \"npm run build\" first to compile the workers.');\n process.exit(1);\n }\n\n const errorMessage = error instanceof Error ? error.message : String(error);\n const errorStack = error instanceof Error ? error.stack : undefined;\n\n console.error(`Error loading worker \"${workerName}\":`, errorMessage);\n if (errorStack) {\n console.error(errorStack);\n }\n process.exit(1);\n }\n}\n\n// Run main function\nmain().catch((error) => {\n console.error('Fatal error:', error);\n process.exit(1);\n});\n\n// Export for programmatic usage\nexport { STDIO_BUS_WORKERS, showUsage, isValidWorkerName, main as launch };\nexport type { WorkerConfig, WorkerName };\n"],
|
|
5
|
+
"mappings": ";AAqCA,OAAS,kBAAqB,MAC9B,OAAS,QAAS,SAAY,OAC9B,OAAS,aAAgB,cAEzB,IAAM,WAAa,cAAc,YAAY,GAAG,EAChD,IAAM,UAAY,QAAQ,UAAU,EAapC,IAAM,kBAA4D,CAChE,aAAc,CACZ,KAAM,0CACN,YAAa,uDACf,EACA,eAAgB,CACd,KAAM,+DACN,YAAa,2CACf,EACA,cAAe,CACb,KAAM,iDACN,YAAa,gDACf,EACA,kBAAmB,CACjB,KAAM,+CACN,YAAa,gCACf,EACA,mBAAoB,CAClB,KAAM,gDACN,YAAa,4BACf,CACF,EAUA,SAAS,WAAkB,CACzB,QAAQ,MAAM,oCAAoC,EAClD,QAAQ,MAAM,EAAE,EAChB,QAAQ,MAAM,oBAAoB,EAElC,SAAW,CAAC,KAAM,MAAM,IAAK,OAAO,QAAQ,iBAAiB,EAAG,CAC9D,QAAQ,MAAM,OAAO,KAAK,OAAO,EAAE,CAAC,IAAI,OAAO,WAAW,EAAE,CAC9D,CAEA,QAAQ,MAAM,EAAE,EAChB,QAAQ,MAAM,WAAW,EACzB,Q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AAC",
|
|
6
|
+
"names": []
|
|
7
|
+
}
|
|
@@ -1,12 +1,52 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
import { fileURLToPath } from 'url';
|
|
5
|
-
import { dirname, join } from 'path';
|
|
2
|
+
import net from"net";import readline from"readline";import crypto from"crypto";function parseArgs(){const args=process.argv.slice(2);const options={tcp:null,unix:null,agentId:null,flow:null,sessionId:null,prompt:"Hello, agent!",interactive:false,timeout:3e4,help:false};for(let i=0;i<args.length;i++){const arg=args[i];switch(arg){case"--tcp":options.tcp=args[++i];break;case"--unix":options.unix=args[++i];break;case"--agent":options.agentId=args[++i];break;case"--flow":options.flow=args[++i];break;case"--session":options.sessionId=args[++i];break;case"--prompt":options.prompt=args[++i];break;case"--interactive":options.interactive=true;break;case"--timeout":options.timeout=parseInt(args[++i],10);break;case"--help":case"-h":options.help=true;break;default:console.error(`Unknown option: ${arg}`);process.exit(1)}}return options}function showHelp(){console.log(`
|
|
3
|
+
ACP Registry Transit Test Client
|
|
6
4
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
const require = createRequire(import.meta.url);
|
|
5
|
+
This client demonstrates sending ACP messages through the Registry Launcher
|
|
6
|
+
transit chain: Client \u2192 stdio Bus \u2192 Registry Launcher \u2192 ACP Agent \u2192 back
|
|
10
7
|
|
|
11
|
-
|
|
12
|
-
|
|
8
|
+
Usage:
|
|
9
|
+
node registry-launcher-client.js --tcp <host:port> --agent <id> [options]
|
|
10
|
+
node registry-launcher-client.js --unix <path> --agent <id> [options]
|
|
11
|
+
|
|
12
|
+
Connection (one required):
|
|
13
|
+
--tcp <host:port> Connect via TCP (e.g., localhost:9000)
|
|
14
|
+
--unix <path> Connect via Unix socket (e.g., /tmp/stdio_bus.sock)
|
|
15
|
+
|
|
16
|
+
Required:
|
|
17
|
+
--agent <id> Agent ID from ACP Registry to route messages to
|
|
18
|
+
|
|
19
|
+
ACP Flow Options:
|
|
20
|
+
--flow <type> ACP flow to execute:
|
|
21
|
+
initialize - Send initialize request
|
|
22
|
+
session-new - Create new session
|
|
23
|
+
session-prompt - Send prompt to session
|
|
24
|
+
full - Run full flow (init \u2192 new \u2192 prompt)
|
|
25
|
+
--session <id> Session ID for session/prompt (auto-generated if not set)
|
|
26
|
+
--prompt <text> Prompt text for session/prompt (default: "Hello, agent!")
|
|
27
|
+
|
|
28
|
+
Modes:
|
|
29
|
+
--interactive Read JSON from stdin, auto-add agentId to messages
|
|
30
|
+
--timeout <ms> Response timeout in ms (default: 30000)
|
|
31
|
+
|
|
32
|
+
Other:
|
|
33
|
+
--help, -h Show this help message
|
|
34
|
+
|
|
35
|
+
Examples:
|
|
36
|
+
# Run full ACP flow with an agent
|
|
37
|
+
node registry-launcher-client.js --tcp localhost:9000 --agent my-agent --flow full
|
|
38
|
+
|
|
39
|
+
# Send just an initialize request
|
|
40
|
+
node registry-launcher-client.js --tcp localhost:9000 --agent my-agent --flow initialize
|
|
41
|
+
|
|
42
|
+
# Create a new session
|
|
43
|
+
node registry-launcher-client.js --tcp localhost:9000 --agent my-agent --flow session-new
|
|
44
|
+
|
|
45
|
+
# Send a prompt to an existing session
|
|
46
|
+
node registry-launcher-client.js --tcp localhost:9000 --agent my-agent --flow session-prompt --session sess-123
|
|
47
|
+
|
|
48
|
+
# Interactive mode - type JSON messages, agentId added automatically
|
|
49
|
+
node registry-launcher-client.js --tcp localhost:9000 --agent my-agent --interactive
|
|
50
|
+
`)}function generateId(prefix="req"){return`${prefix}-${crypto.randomUUID().slice(0,8)}`}function generateSessionId(){return`sess-${crypto.randomUUID().slice(0,8)}`}function buildInitializeRequest(agentId){return{jsonrpc:"2.0",id:generateId("init"),method:"initialize",agentId,params:{protocolVersion:1,clientCapabilities:{},clientInfo:{name:"registry-launcher-test-client",version:"1.0.0"}}}}function buildAuthenticateRequest(agentId,methodId){return{jsonrpc:"2.0",id:generateId("auth"),method:"authenticate",agentId,params:{methodId}}}function buildSessionNewRequest(agentId){return{jsonrpc:"2.0",id:generateId("session-new"),method:"session/new",agentId,params:{cwd:process.cwd(),mcpServers:[]}}}function buildSessionPromptRequest(agentId,sessionId,promptText){return{jsonrpc:"2.0",id:generateId("prompt"),method:"session/prompt",agentId,params:{sessionId,prompt:[{type:"text",text:promptText}]}}}function createConnection(options){if(options.tcp){const[host,portStr]=options.tcp.split(":");const port=parseInt(portStr,10);if(!host||isNaN(port)){console.error("Invalid TCP address. Use format: host:port");process.exit(1)}console.error(`Connecting to TCP ${host}:${port}...`);return net.createConnection({host,port})}else if(options.unix){console.error(`Connecting to Unix socket ${options.unix}...`);return net.createConnection({path:options.unix})}else{console.error("Error: Must specify --tcp or --unix");process.exit(1)}}function sendRequest(socket,request,timeout){return new Promise((resolve,reject)=>{let buffer="";let timeoutId;const cleanup=()=>{clearTimeout(timeoutId);socket.removeListener("data",onData);socket.removeListener("error",onError);socket.removeListener("close",onClose)};const onData=data=>{buffer+=data.toString();let newlineIndex;while((newlineIndex=buffer.indexOf("\n"))!==-1){const line=buffer.slice(0,newlineIndex);buffer=buffer.slice(newlineIndex+1);if(line.trim()){try{const response=JSON.parse(line);if(response.id===request.id){cleanup();resolve(response);return}else{console.error(`\u2190 Received (other): ${JSON.stringify(response)}`)}}catch(err){console.error(`Error parsing response: ${err.message}`)}}}};const onError=err=>{cleanup();reject(new Error(`Connection error: ${err.message}`))};const onClose=()=>{cleanup();reject(new Error("Connection closed before response received"))};timeoutId=setTimeout(()=>{cleanup();reject(new Error(`Timeout: No response within ${timeout}ms`))},timeout);socket.on("data",onData);socket.on("error",onError);socket.on("close",onClose);console.error(`\u2192 Sending: ${JSON.stringify(request)}`);socket.write(JSON.stringify(request)+"\n")})}async function runInitializeFlow(socket,options){console.error("\n=== Initialize Flow ===");const request=buildInitializeRequest(options.agentId);const response=await sendRequest(socket,request,options.timeout);console.log("\nInitialize Response:");console.log(JSON.stringify(response,null,2));if(response.error){console.error(`\u2717 Initialize failed: ${response.error.message}`)}else{console.error("\u2713 Initialize successful");if(response.result?.protocolVersion){console.error(` Protocol version: ${response.result.protocolVersion}`)}if(response.result?.serverInfo){console.error(` Server: ${response.result.serverInfo.name} v${response.result.serverInfo.version}`)}}return response}async function runAuthenticateFlow(socket,options,methodId){console.error("\n=== Authenticate Flow ===");console.error(` Method: ${methodId}`);const request=buildAuthenticateRequest(options.agentId,methodId);const response=await sendRequest(socket,request,options.timeout);console.log("\nAuthenticate Response:");console.log(JSON.stringify(response,null,2));if(response.error){console.error(`\u2717 Authentication failed: ${response.error.message}`)}else{console.error("\u2713 Authentication successful")}return response}async function runSessionNewFlow(socket,options){console.error("\n=== Session/New Flow ===");const request=buildSessionNewRequest(options.agentId);const response=await sendRequest(socket,request,options.timeout);console.log("\nSession/New Response:");console.log(JSON.stringify(response,null,2));if(response.error){console.error(`\u2717 Session creation failed: ${response.error.message}`)}else{console.error("\u2713 Session created successfully");if(response.result?.sessionId){console.error(` Session ID: ${response.result.sessionId}`)}}return response}async function runSessionPromptFlow(socket,options,sessionId){console.error("\n=== Session/Prompt Flow ===");const request=buildSessionPromptRequest(options.agentId,sessionId,options.prompt);const response=await sendRequest(socket,request,options.timeout);console.log("\nSession/Prompt Response:");console.log(JSON.stringify(response,null,2));if(response.error){console.error(`\u2717 Prompt failed: ${response.error.message}`)}else{console.error("\u2713 Prompt successful");if(response.result?.messages){console.error(` Response messages: ${response.result.messages.length}`)}}return response}async function runFullFlow(socket,options){console.error("\n========================================");console.error("Running Full ACP Flow");console.error(`Agent: ${options.agentId}`);console.error("========================================");const initResponse=await runInitializeFlow(socket,options);if(initResponse.error){console.error("\nFull flow aborted due to initialize failure.");return}const authMethods=initResponse.result?.authMethods||[];if(authMethods.length>0){const openaiMethod=authMethods.find(m=>m.id==="openai-api-key");const apiKeyMethod=authMethods.find(m=>m.id.includes("api-key")||m.id.includes("apikey"));const methodId=openaiMethod?.id||apiKeyMethod?.id||authMethods[0].id;const authResponse=await runAuthenticateFlow(socket,options,methodId);if(authResponse.error){console.error("\nFull flow aborted due to authentication failure.");return}}const sessionResponse=await runSessionNewFlow(socket,options);if(sessionResponse.error){console.error("\nFull flow aborted due to session creation failure.");return}const sessionId=sessionResponse.result?.sessionId||options.sessionId||generateSessionId();await runSessionPromptFlow(socket,options,sessionId);console.error("\n========================================");console.error("Full ACP Flow Complete");console.error("========================================")}async function runSingleFlow(options){const socket=createConnection(options);socket.on("connect",async()=>{console.error("Connected.");try{switch(options.flow){case"initialize":await runInitializeFlow(socket,options);break;case"session-new":await runSessionNewFlow(socket,options);break;case"session-prompt":const sessionId=options.sessionId||generateSessionId();if(!options.sessionId){console.error(`Note: Using generated session ID: ${sessionId}`)}await runSessionPromptFlow(socket,options,sessionId);break;case"full":await runFullFlow(socket,options);break;default:console.error(`Unknown flow: ${options.flow}`);console.error("Valid flows: initialize, session-new, session-prompt, full")}}catch(err){console.error(`
|
|
51
|
+
Error: ${err.message}`)}finally{socket.end()}});socket.on("error",err=>{console.error(`Connection error: ${err.message}`);process.exit(1)});socket.on("close",()=>{console.error("\nConnection closed.");process.exit(0)})}function runInteractive(options){const socket=createConnection(options);let buffer="";const rl=readline.createInterface({input:process.stdin,output:process.stderr,terminal:false});socket.on("connect",()=>{console.error("Connected in interactive mode.");console.error(`Agent ID: ${options.agentId} (will be added to all messages)`);console.error("\nEnter JSON-RPC messages (one per line). agentId will be added automatically.");console.error('Example: {"jsonrpc":"2.0","id":"1","method":"initialize","params":{}}');console.error("Press Ctrl+D to exit.\n")});socket.on("data",data=>{buffer+=data.toString();let newlineIndex;while((newlineIndex=buffer.indexOf("\n"))!==-1){const line=buffer.slice(0,newlineIndex);buffer=buffer.slice(newlineIndex+1);if(line.trim()){try{const response=JSON.parse(line);console.log("\n\u2190 Response:");console.log(JSON.stringify(response,null,2));console.error("")}catch(err){console.error(`Error parsing response: ${err.message}`)}}}});rl.on("line",line=>{if(!line.trim())return;try{const msg=JSON.parse(line);msg.agentId=options.agentId;console.error(`\u2192 Sending (with agentId=${options.agentId}): ${JSON.stringify(msg)}`);socket.write(JSON.stringify(msg)+"\n")}catch(err){console.error(`Invalid JSON: ${err.message}`);console.error("Please enter a valid JSON object.")}});rl.on("close",()=>{console.error("\nClosing connection...");socket.end()});socket.on("error",err=>{console.error(`Connection error: ${err.message}`);rl.close();process.exit(1)});socket.on("close",()=>{console.error("Connection closed.");process.exit(0)})}function main(){const options=parseArgs();if(options.help){showHelp();process.exit(0)}if(!options.tcp&&!options.unix){console.error("Error: Must specify --tcp or --unix connection");console.error("Use --help for usage information.");process.exit(1)}if(!options.agentId){console.error("Error: Must specify --agent <id> for routing");console.error("Use --help for usage information.");process.exit(1)}if(options.interactive){runInteractive(options)}else if(options.flow){runSingleFlow(options)}else{options.flow="full";runSingleFlow(options)}}process.on("uncaughtException",err=>{console.error(`Uncaught exception: ${err.message}`);process.exit(1)});process.on("unhandledRejection",reason=>{console.error(`Unhandled rejection: ${reason}`);process.exit(1)});main();
|
|
52
|
+
//# sourceMappingURL=registry-launcher-client.js.map
|
|
@@ -2,6 +2,6 @@
|
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../../../workers-registry/acp-registry/registry-launcher-client.js"],
|
|
4
4
|
"sourcesContent": ["#!/usr/bin/env node\n\n/*\n * Apache License 2.0\n * Copyright (c) 2025\u2013present Raman Marozau, Target Insight Function.\n * Contact: raman@worktif.com\n *\n * This file is part of the stdio bus protocol reference implementation:\n * stdio_bus_kernel_workers (target: <target_stdio_bus_kernel_workers>).\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n *\n * SPDX-License-Identifier: Apache-2.0\n */\n\n/**\n * @file registry-launcher-client.js\n * @brief Test client for ACP Registry Transit via stdio Bus\n *\n * This test client demonstrates sending ACP messages through the Registry Launcher\n * transit chain. It shows how to use the agentId field for routing messages to\n * specific agents registered in the ACP Registry.\n *\n * ## Transit Chain\n *\n * ```\n * This Client \u2192 stdio Bus \u2192 Registry Launcher \u2192 ACP Agent \u2192 back\n * ```\n *\n * ## Connection Modes\n *\n * The client supports TCP and Unix socket connections to stdio Bus:\n *\n * - **TCP**: Connect to `--tcp <host:port>` mode\n * ```bash\n * node workers-registry/acp-registry/registry-launcher-client.js --tcp localhost:9000 --agent my-agent\n * ```\n *\n * - **Unix Socket**: Connect to `--unix <path>` mode\n * ```bash\n * node workers-registry/acp-registry/registry-launcher-client.js --unix /tmp/stdio_bus.sock --agent my-agent\n * ```\n *\n * ## Usage\n *\n * ```bash\n * # Start stdio Bus with Registry Launcher configuration. stdio Bus kernel repo: https://github.com/stdiobus/stdiobus\n * ./stdio_bus --config workers-registry/acp-registry/registry-launcher-config.json --tcp localhost:9000\n *\n * # In another terminal, run the test client\n * node workers-registry/acp-registry/registry-launcher-client.js --tcp localhost:9000 --agent my-agent\n *\n * # Run specific ACP flow\n * node workers-registry/acp-registry/registry-launcher-client.js --tcp localhost:9000 --agent my-agent --flow initialize\n * node workers-registry/acp-registry/registry-launcher-client.js --tcp localhost:9000 --agent my-agent --flow session-new\n * node workers-registry/acp-registry/registry-launcher-client.js --tcp localhost:9000 --agent my-agent --flow session-prompt\n *\n * # Run full ACP flow (initialize \u2192 session/new \u2192 session/prompt)\n * node workers-registry/acp-registry/registry-launcher-client.js --tcp localhost:9000 --agent my-agent --flow full\n *\n * # Interactive mode - send custom messages with agentId\n * node workers-registry/acp-registry/registry-launcher-client.js --tcp localhost:9000 --agent my-agent --interactive\n * ```\n *\n * ## Command Line Options\n *\n * | Option | Description |\n * |--------|-------------|\n * | `--tcp <host:port>` | Connect via TCP to specified host and port |\n * | `--unix <path>` | Connect via Unix domain socket |\n * | `--agent <id>` | Agent ID from ACP Registry (required) |\n * | `--flow <type>` | ACP flow to execute: initialize, session-new, session-prompt, full |\n * | `--session <id>` | Session ID for session-based requests (auto-generated if not provided) |\n * | `--prompt <text>` | Prompt text for session/prompt request (default: \"Hello, agent!\") |\n * | `--interactive` | Interactive mode: read JSON from stdin, auto-add agentId |\n * | `--timeout <ms>` | Response timeout in milliseconds (default: 30000) |\n * | `--help` | Show usage information |\n *\n * ## ACP Message Flows\n *\n * ### Initialize Flow\n *\n * Sends an `initialize` request to establish protocol version and capabilities:\n *\n * ```json\n * {\n * \"jsonrpc\": \"2.0\",\n * \"id\": \"init-1\",\n * \"method\": \"initialize\",\n * \"agentId\": \"my-agent\",\n * \"params\": {\n * \"protocolVersion\": 1,\n * \"capabilities\": {},\n * \"clientInfo\": {\n * \"name\": \"registry-launcher-test-client\",\n * \"version\": \"1.0.0\"\n * }\n * }\n * }\n * ```\n *\n * ### Session/New Flow\n *\n * Creates a new session with the agent:\n *\n * ```json\n * {\n * \"jsonrpc\": \"2.0\",\n * \"id\": \"session-new-1\",\n * \"method\": \"session/new\",\n * \"agentId\": \"my-agent\",\n * \"params\": {}\n * }\n * ```\n *\n * ### Session/Prompt Flow\n *\n * Sends a prompt to an existing session:\n *\n * ```json\n * {\n * \"jsonrpc\": \"2.0\",\n * \"id\": \"prompt-1\",\n * \"method\": \"session/prompt\",\n * \"agentId\": \"my-agent\",\n * \"params\": {\n * \"sessionId\": \"sess-123\",\n * \"prompt\": {\n * \"messages\": [\n * {\n * \"role\": \"user\",\n * \"content\": { \"type\": \"text\", \"text\": \"Hello, agent!\" }\n * }\n * ]\n * }\n * }\n * }\n * ```\n *\n * ## Important Notes\n *\n * - The `agentId` field is required for all messages and is used by the Registry\n * Launcher to route messages to the correct agent process\n * - The Registry Launcher removes the `agentId` field before forwarding to the agent\n * - Responses from agents are forwarded unchanged (no agentId added)\n * - Session IDs returned by session/new should be used in subsequent session/prompt calls\n */\n\nimport net from 'net';\nimport readline from 'readline';\nimport crypto from 'crypto';\n\n/**\n * Parse command line arguments.\n * @returns {Object} Parsed options\n */\nfunction parseArgs() {\n const args = process.argv.slice(2);\n const options = {\n tcp: null,\n unix: null,\n agentId: null,\n flow: null,\n sessionId: null,\n prompt: 'Hello, agent!',\n interactive: false,\n timeout: 30000,\n help: false\n };\n\n for (let i = 0; i < args.length; i++) {\n const arg = args[i];\n switch (arg) {\n case '--tcp':\n options.tcp = args[++i];\n break;\n case '--unix':\n options.unix = args[++i];\n break;\n case '--agent':\n options.agentId = args[++i];\n break;\n case '--flow':\n options.flow = args[++i];\n break;\n case '--session':\n options.sessionId = args[++i];\n break;\n case '--prompt':\n options.prompt = args[++i];\n break;\n case '--interactive':\n options.interactive = true;\n break;\n case '--timeout':\n options.timeout = parseInt(args[++i], 10);\n break;\n case '--help':\n case '-h':\n options.help = true;\n break;\n default:\n console.error(`Unknown option: ${arg}`);\n process.exit(1);\n }\n }\n\n return options;\n}\n\n/**\n * Display usage information.\n */\nfunction showHelp() {\n console.log(`\nACP Registry Transit Test Client\n\nThis client demonstrates sending ACP messages through the Registry Launcher\ntransit chain: Client \u2192 stdio Bus \u2192 Registry Launcher \u2192 ACP Agent \u2192 back\n\nUsage:\n node registry-launcher-client.js --tcp <host:port> --agent <id> [options]\n node registry-launcher-client.js --unix <path> --agent <id> [options]\n\nConnection (one required):\n --tcp <host:port> Connect via TCP (e.g., localhost:9000)\n --unix <path> Connect via Unix socket (e.g., /tmp/stdio_bus.sock)\n\nRequired:\n --agent <id> Agent ID from ACP Registry to route messages to\n\nACP Flow Options:\n --flow <type> ACP flow to execute:\n initialize - Send initialize request\n session-new - Create new session\n session-prompt - Send prompt to session\n full - Run full flow (init \u2192 new \u2192 prompt)\n --session <id> Session ID for session/prompt (auto-generated if not set)\n --prompt <text> Prompt text for session/prompt (default: \"Hello, agent!\")\n\nModes:\n --interactive Read JSON from stdin, auto-add agentId to messages\n --timeout <ms> Response timeout in ms (default: 30000)\n\nOther:\n --help, -h Show this help message\n\nExamples:\n # Run full ACP flow with an agent\n node registry-launcher-client.js --tcp localhost:9000 --agent my-agent --flow full\n\n # Send just an initialize request\n node registry-launcher-client.js --tcp localhost:9000 --agent my-agent --flow initialize\n\n # Create a new session\n node registry-launcher-client.js --tcp localhost:9000 --agent my-agent --flow session-new\n\n # Send a prompt to an existing session\n node registry-launcher-client.js --tcp localhost:9000 --agent my-agent --flow session-prompt --session sess-123\n\n # Interactive mode - type JSON messages, agentId added automatically\n node registry-launcher-client.js --tcp localhost:9000 --agent my-agent --interactive\n`);\n}\n\n/**\n * Generate a unique request ID.\n * @param {string} prefix - Prefix for the ID\n * @returns {string} Unique ID\n */\nfunction generateId(prefix = 'req') {\n return `${prefix}-${crypto.randomUUID().slice(0, 8)}`;\n}\n\n/**\n * Generate a unique session ID.\n * @returns {string} Session ID\n */\nfunction generateSessionId() {\n return `sess-${crypto.randomUUID().slice(0, 8)}`;\n}\n\n/**\n * Build an ACP initialize request.\n * @param {string} agentId - Agent ID for routing\n * @returns {Object} JSON-RPC initialize request\n */\nfunction buildInitializeRequest(agentId) {\n return {\n jsonrpc: '2.0',\n id: generateId('init'),\n method: 'initialize',\n agentId,\n params: {\n protocolVersion: 1,\n clientCapabilities: {},\n clientInfo: {\n name: 'registry-launcher-test-client',\n version: '1.0.0'\n }\n }\n };\n}\n\n/**\n * Build an ACP authenticate request.\n * @param {string} agentId - Agent ID for routing\n * @param {string} methodId - Authentication method ID\n * @returns {Object} JSON-RPC authenticate request\n */\nfunction buildAuthenticateRequest(agentId, methodId) {\n return {\n jsonrpc: '2.0',\n id: generateId('auth'),\n method: 'authenticate',\n agentId,\n params: {\n methodId\n }\n };\n}\n\n/**\n * Build an ACP session/new request.\n * @param {string} agentId - Agent ID for routing\n * @returns {Object} JSON-RPC session/new request\n */\nfunction buildSessionNewRequest(agentId) {\n return {\n jsonrpc: '2.0',\n id: generateId('session-new'),\n method: 'session/new',\n agentId,\n params: {\n cwd: process.cwd(),\n mcpServers: []\n }\n };\n}\n\n/**\n * Build an ACP session/prompt request.\n * @param {string} agentId - Agent ID for routing\n * @param {string} sessionId - Session ID\n * @param {string} promptText - Prompt text\n * @returns {Object} JSON-RPC session/prompt request\n */\nfunction buildSessionPromptRequest(agentId, sessionId, promptText) {\n return {\n jsonrpc: '2.0',\n id: generateId('prompt'),\n method: 'session/prompt',\n agentId,\n params: {\n sessionId,\n prompt: [\n {\n type: 'text',\n text: promptText\n }\n ]\n }\n };\n}\n\n/**\n * Create a socket connection to stdio Bus.\n * @param {Object} options - Connection options\n * @returns {net.Socket} Connected socket\n */\nfunction createConnection(options) {\n if (options.tcp) {\n const [host, portStr] = options.tcp.split(':');\n const port = parseInt(portStr, 10);\n if (!host || isNaN(port)) {\n console.error('Invalid TCP address. Use format: host:port');\n process.exit(1);\n }\n console.error(`Connecting to TCP ${host}:${port}...`);\n return net.createConnection({ host, port });\n } else if (options.unix) {\n console.error(`Connecting to Unix socket ${options.unix}...`);\n return net.createConnection({ path: options.unix });\n } else {\n console.error('Error: Must specify --tcp or --unix');\n process.exit(1);\n }\n}\n\n/**\n * Send a request and wait for response.\n * @param {net.Socket} socket - Connected socket\n * @param {Object} request - Request to send\n * @param {number} timeout - Timeout in ms\n * @returns {Promise<Object>} Response object\n */\nfunction sendRequest(socket, request, timeout) {\n return new Promise((resolve, reject) => {\n let buffer = '';\n let timeoutId;\n\n const cleanup = () => {\n clearTimeout(timeoutId);\n socket.removeListener('data', onData);\n socket.removeListener('error', onError);\n socket.removeListener('close', onClose);\n };\n\n const onData = (data) => {\n buffer += data.toString();\n\n let newlineIndex;\n while ((newlineIndex = buffer.indexOf('\\n')) !== -1) {\n const line = buffer.slice(0, newlineIndex);\n buffer = buffer.slice(newlineIndex + 1);\n\n if (line.trim()) {\n try {\n const response = JSON.parse(line);\n // Check if this response matches our request ID\n if (response.id === request.id) {\n cleanup();\n resolve(response);\n return;\n } else {\n // Log other responses (notifications, etc.)\n console.error(`\u2190 Received (other): ${JSON.stringify(response)}`);\n }\n } catch (err) {\n console.error(`Error parsing response: ${err.message}`);\n }\n }\n }\n };\n\n const onError = (err) => {\n cleanup();\n reject(new Error(`Connection error: ${err.message}`));\n };\n\n const onClose = () => {\n cleanup();\n reject(new Error('Connection closed before response received'));\n };\n\n timeoutId = setTimeout(() => {\n cleanup();\n reject(new Error(`Timeout: No response within ${timeout}ms`));\n }, timeout);\n\n socket.on('data', onData);\n socket.on('error', onError);\n socket.on('close', onClose);\n\n // Send the request\n console.error(`\u2192 Sending: ${JSON.stringify(request)}`);\n socket.write(JSON.stringify(request) + '\\n');\n });\n}\n\n/**\n * Run the initialize flow.\n * @param {net.Socket} socket - Connected socket\n * @param {Object} options - Options\n * @returns {Promise<Object>} Initialize response\n */\nasync function runInitializeFlow(socket, options) {\n console.error('\\n=== Initialize Flow ===');\n const request = buildInitializeRequest(options.agentId);\n const response = await sendRequest(socket, request, options.timeout);\n\n console.log('\\nInitialize Response:');\n console.log(JSON.stringify(response, null, 2));\n\n if (response.error) {\n console.error(`\u2717 Initialize failed: ${response.error.message}`);\n } else {\n console.error('\u2713 Initialize successful');\n if (response.result?.protocolVersion) {\n console.error(` Protocol version: ${response.result.protocolVersion}`);\n }\n if (response.result?.serverInfo) {\n console.error(` Server: ${response.result.serverInfo.name} v${response.result.serverInfo.version}`);\n }\n }\n\n return response;\n}\n\n/**\n * Run the authenticate flow.\n * @param {net.Socket} socket - Connected socket\n * @param {Object} options - Options\n * @param {string} methodId - Authentication method ID\n * @returns {Promise<Object>} Authenticate response\n */\nasync function runAuthenticateFlow(socket, options, methodId) {\n console.error('\\n=== Authenticate Flow ===');\n console.error(` Method: ${methodId}`);\n const request = buildAuthenticateRequest(options.agentId, methodId);\n const response = await sendRequest(socket, request, options.timeout);\n\n console.log('\\nAuthenticate Response:');\n console.log(JSON.stringify(response, null, 2));\n\n if (response.error) {\n console.error(`\u2717 Authentication failed: ${response.error.message}`);\n } else {\n console.error('\u2713 Authentication successful');\n }\n\n return response;\n}\n\n/**\n * Run the session/new flow.\n * @param {net.Socket} socket - Connected socket\n * @param {Object} options - Options\n * @returns {Promise<Object>} Session/new response\n */\nasync function runSessionNewFlow(socket, options) {\n console.error('\\n=== Session/New Flow ===');\n const request = buildSessionNewRequest(options.agentId);\n const response = await sendRequest(socket, request, options.timeout);\n\n console.log('\\nSession/New Response:');\n console.log(JSON.stringify(response, null, 2));\n\n if (response.error) {\n console.error(`\u2717 Session creation failed: ${response.error.message}`);\n } else {\n console.error('\u2713 Session created successfully');\n if (response.result?.sessionId) {\n console.error(` Session ID: ${response.result.sessionId}`);\n }\n }\n\n return response;\n}\n\n/**\n * Run the session/prompt flow.\n * @param {net.Socket} socket - Connected socket\n * @param {Object} options - Options\n * @param {string} sessionId - Session ID to use\n * @returns {Promise<Object>} Session/prompt response\n */\nasync function runSessionPromptFlow(socket, options, sessionId) {\n console.error('\\n=== Session/Prompt Flow ===');\n const request = buildSessionPromptRequest(options.agentId, sessionId, options.prompt);\n const response = await sendRequest(socket, request, options.timeout);\n\n console.log('\\nSession/Prompt Response:');\n console.log(JSON.stringify(response, null, 2));\n\n if (response.error) {\n console.error(`\u2717 Prompt failed: ${response.error.message}`);\n } else {\n console.error('\u2713 Prompt successful');\n if (response.result?.messages) {\n console.error(` Response messages: ${response.result.messages.length}`);\n }\n }\n\n return response;\n}\n\n/**\n * Run the full ACP flow (initialize \u2192 session/new \u2192 session/prompt).\n * @param {net.Socket} socket - Connected socket\n * @param {Object} options - Options\n */\nasync function runFullFlow(socket, options) {\n console.error('\\n========================================');\n console.error('Running Full ACP Flow');\n console.error(`Agent: ${options.agentId}`);\n console.error('========================================');\n\n // Step 1: Initialize\n const initResponse = await runInitializeFlow(socket, options);\n if (initResponse.error) {\n console.error('\\nFull flow aborted due to initialize failure.');\n return;\n }\n\n // Step 2: Authenticate if required\n const authMethods = initResponse.result?.authMethods || [];\n if (authMethods.length > 0) {\n // Prefer openai-api-key, then any api-key method, then first available\n const openaiMethod = authMethods.find(m => m.id === 'openai-api-key');\n const apiKeyMethod = authMethods.find(m =>\n m.id.includes('api-key') || m.id.includes('apikey')\n );\n const methodId = openaiMethod?.id || apiKeyMethod?.id || authMethods[0].id;\n\n const authResponse = await runAuthenticateFlow(socket, options, methodId);\n if (authResponse.error) {\n console.error('\\nFull flow aborted due to authentication failure.');\n return;\n }\n }\n\n // Step 3: Create session\n const sessionResponse = await runSessionNewFlow(socket, options);\n if (sessionResponse.error) {\n console.error('\\nFull flow aborted due to session creation failure.');\n return;\n }\n\n // Extract session ID from response\n const sessionId = sessionResponse.result?.sessionId || options.sessionId || generateSessionId();\n\n // Step 4: Send prompt\n await runSessionPromptFlow(socket, options, sessionId);\n\n console.error('\\n========================================');\n console.error('Full ACP Flow Complete');\n console.error('========================================');\n}\n\n/**\n * Run a single flow based on options.\n * @param {Object} options - Parsed options\n */\nasync function runSingleFlow(options) {\n const socket = createConnection(options);\n\n socket.on('connect', async () => {\n console.error('Connected.');\n\n try {\n switch (options.flow) {\n case 'initialize':\n await runInitializeFlow(socket, options);\n break;\n case 'session-new':\n await runSessionNewFlow(socket, options);\n break;\n case 'session-prompt':\n const sessionId = options.sessionId || generateSessionId();\n if (!options.sessionId) {\n console.error(`Note: Using generated session ID: ${sessionId}`);\n }\n await runSessionPromptFlow(socket, options, sessionId);\n break;\n case 'full':\n await runFullFlow(socket, options);\n break;\n default:\n console.error(`Unknown flow: ${options.flow}`);\n console.error('Valid flows: initialize, session-new, session-prompt, full');\n }\n } catch (err) {\n console.error(`\\nError: ${err.message}`);\n } finally {\n socket.end();\n }\n });\n\n socket.on('error', (err) => {\n console.error(`Connection error: ${err.message}`);\n process.exit(1);\n });\n\n socket.on('close', () => {\n console.error('\\nConnection closed.');\n process.exit(0);\n });\n}\n\n/**\n * Run in interactive mode.\n * Reads JSON messages from stdin and adds agentId before sending.\n * @param {Object} options - Parsed options\n */\nfunction runInteractive(options) {\n const socket = createConnection(options);\n let buffer = '';\n\n const rl = readline.createInterface({\n input: process.stdin,\n output: process.stderr,\n terminal: false\n });\n\n socket.on('connect', () => {\n console.error('Connected in interactive mode.');\n console.error(`Agent ID: ${options.agentId} (will be added to all messages)`);\n console.error('\\nEnter JSON-RPC messages (one per line). agentId will be added automatically.');\n console.error('Example: {\"jsonrpc\":\"2.0\",\"id\":\"1\",\"method\":\"initialize\",\"params\":{}}');\n console.error('Press Ctrl+D to exit.\\n');\n });\n\n // Handle incoming responses\n socket.on('data', (data) => {\n buffer += data.toString();\n\n let newlineIndex;\n while ((newlineIndex = buffer.indexOf('\\n')) !== -1) {\n const line = buffer.slice(0, newlineIndex);\n buffer = buffer.slice(newlineIndex + 1);\n\n if (line.trim()) {\n try {\n const response = JSON.parse(line);\n console.log('\\n\u2190 Response:');\n console.log(JSON.stringify(response, null, 2));\n console.error('');\n } catch (err) {\n console.error(`Error parsing response: ${err.message}`);\n }\n }\n }\n });\n\n // Handle user input\n rl.on('line', (line) => {\n if (!line.trim()) return;\n\n try {\n // Parse the user's JSON\n const msg = JSON.parse(line);\n\n // Add agentId for routing\n msg.agentId = options.agentId;\n\n console.error(`\u2192 Sending (with agentId=${options.agentId}): ${JSON.stringify(msg)}`);\n socket.write(JSON.stringify(msg) + '\\n');\n } catch (err) {\n console.error(`Invalid JSON: ${err.message}`);\n console.error('Please enter a valid JSON object.');\n }\n });\n\n rl.on('close', () => {\n console.error('\\nClosing connection...');\n socket.end();\n });\n\n socket.on('error', (err) => {\n console.error(`Connection error: ${err.message}`);\n rl.close();\n process.exit(1);\n });\n\n socket.on('close', () => {\n console.error('Connection closed.');\n process.exit(0);\n });\n}\n\n/**\n * Main entry point.\n */\nfunction main() {\n const options = parseArgs();\n\n if (options.help) {\n showHelp();\n process.exit(0);\n }\n\n // Validate required options\n if (!options.tcp && !options.unix) {\n console.error('Error: Must specify --tcp or --unix connection');\n console.error('Use --help for usage information.');\n process.exit(1);\n }\n\n if (!options.agentId) {\n console.error('Error: Must specify --agent <id> for routing');\n console.error('Use --help for usage information.');\n process.exit(1);\n }\n\n // Determine mode\n if (options.interactive) {\n runInteractive(options);\n } else if (options.flow) {\n runSingleFlow(options);\n } else {\n // Default to full flow if no specific flow or interactive mode\n options.flow = 'full';\n runSingleFlow(options);\n }\n}\n\n// Handle uncaught exceptions\nprocess.on('uncaughtException', (err) => {\n console.error(`Uncaught exception: ${err.message}`);\n process.exit(1);\n});\n\n// Handle unhandled promise rejections\nprocess.on('unhandledRejection', (reason) => {\n console.error(`Unhandled rejection: ${reason}`);\n process.exit(1);\n});\n\nmain();\n"],
|
|
5
|
-
"mappings": ";
|
|
6
|
-
"names": [
|
|
5
|
+
"mappings": ";AA8JA,OAAO,QAAS,MAChB,OAAO,aAAc,WACrB,OAAO,WAAY,SAMnB,SAAS,WAAY,CACnB,MAAM,KAAO,QAAQ,KAAK,MAAM,CAAC,EACjC,MAAM,QAAU,CACd,IAAK,KACL,KAAM,KACN,QAAS,KACT,KAAM,KACN,UAAW,KACX,OAAQ,gBACR,YAAa,MACb,QAAS,IACT,KAAM,KACR,EAEA,QAAS,EAAI,EAAG,EAAI,KAAK,OAAQ,IAAK,CACpC,MAAM,IAAM,KAAK,CAAC,EAClB,OAAQ,IAAK,CACX,IAAK,QACH,QAAQ,IAAM,KAAK,EAAE,CAAC,EACtB,MACF,IAAK,SACH,QAAQ,KAAO,KAAK,EAAE,CAAC,EACvB,MACF,IAAK,UACH,QAAQ,QAAU,KAAK,EAAE,CAAC,EAC1B,MACF,IAAK,SACH,QAAQ,KAAO,KAAK,EAAE,CAAC,EACvB,MACF,IAAK,YACH,QAAQ,UAAY,KAAK,EAAE,CAAC,EAC5B,MACF,IAAK,WACH,QAAQ,OAAS,KAAK,EAAE,CAAC,EACzB,MACF,IAAK,gBACH,QAAQ,YAAc,KACtB,MACF,IAAK,YACH,QAAQ,QAAU,SAAS,KAAK,EAAE,CAAC,EAAG,EAAE,EACxC,MACF,IAAK,SACL,IAAK,KACH,QAAQ,KAAO,KACf,MACF,QACE,QAAQ,MAAM,mBAAmB,GAAG,EAAE,EACtC,QAAQ,KAAK,CAAC,CAClB,CACF,CAEA,OAAO,OACT,CAKA,SAAS,UAAW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,EACjD,QAAQ,KAAK,CAAC,CAChB,CAEA,GAAI,CAAC,QAAQ,QAAS,CACpB,QAAQ,MAAM,8CAA8C,EAC5D,QAAQ,MAAM,mCAAmC,EACjD,QAAQ,KAAK,CAAC,CAChB,CAGA,GAAI,QAAQ,YAAa,CACvB,eAAe,OAAO,CACxB,SAAW,QAAQ,KAAM,CACvB,cAAc,OAAO,CACvB,KAAO,CAEL,QAAQ,KAAO,OACf,cAAc,OAAO,CACvB,CACF,CAGA,QAAQ,GAAG,oBAAsB,KAAQ,CACvC,QAAQ,MAAM,uBAAuB,IAAI,OAAO,EAAE,EAClD,QAAQ,KAAK,CAAC,CAChB,CAAC,EAGD,QAAQ,GAAG,qBAAuB,QAAW,CAC3C,QAAQ,MAAM,wBAAwB,MAAM,EAAE,EAC9C,QAAQ,KAAK,CAAC,CAChB,CAAC,EAED,KAAK",
|
|
6
|
+
"names": []
|
|
7
7
|
}
|
|
@@ -5,11 +5,10 @@
|
|
|
5
5
|
{
|
|
6
6
|
"_comment": "Registry Launcher worker pool - routes messages to ACP Registry agents",
|
|
7
7
|
"id": "registry-launcher",
|
|
8
|
-
"command": "
|
|
8
|
+
"command": "node",
|
|
9
9
|
"args": [
|
|
10
|
-
"
|
|
11
|
-
"
|
|
12
|
-
"./workers-registry/acp-registry/registry-launcher-worker-config.json"
|
|
10
|
+
"/workers-registry/launch/index.js",
|
|
11
|
+
"acp-registry"
|
|
13
12
|
],
|
|
14
13
|
"instances": 1
|
|
15
14
|
}
|
|
@@ -28,4 +27,4 @@
|
|
|
28
27
|
"_comment_backpressure_timeout_sec": "Timeout for backpressure before dropping messages",
|
|
29
28
|
"backpressure_timeout_sec": 60
|
|
30
29
|
}
|
|
31
|
-
}
|
|
30
|
+
}
|
|
@@ -1,12 +1,7 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
// ESM wrapper for CommonJS bundle
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
const __dirname = dirname(__filename);
|
|
9
|
-
const require = createRequire(import.meta.url);
|
|
10
|
-
|
|
11
|
-
// Import the CommonJS bundle
|
|
12
|
-
require('./index.cjs');
|
|
2
|
+
import{Readable,Writable}from"node:stream";import{AgentSideConnection,ndJsonStream}from"@agentclientprotocol/sdk";import{PROTOCOL_VERSION}from"@agentclientprotocol/sdk";import{Client}from"@modelcontextprotocol/sdk/client/index.js";import{StdioClientTransport}from"@modelcontextprotocol/sdk/client/stdio.js";var defaultFactories={createClient:options=>new Client(options),createTransport:options=>new StdioClientTransport(options)};var MCPManager=class{connections=new Map;toolToServer=new Map;onServerCrash;factories;constructor(factories){this.factories={...defaultFactories,...factories}}setOnServerCrash(callback){this.onServerCrash=callback}async connect(servers){for(const serverConfig of servers){try{const transport=this.factories.createTransport({command:serverConfig.command,args:serverConfig.args,env:serverConfig.env});const client=this.factories.createClient({name:"stdio-bus-worker",version:"1.0.0"});await client.connect(transport);const capabilities=client.getServerCapabilities();const connection2={client,transport,config:serverConfig,connected:true,capabilities};this.connections.set(serverConfig.id,connection2);this.setupCrashDetection(serverConfig.id,client);console.error(`[MCP] Connected to server: ${serverConfig.id}`)}catch(error){console.error(`[MCP] Failed to connect to server ${serverConfig.id}:`,error)}}}setupCrashDetection(serverId,client){client.onclose=()=>{const connection2=this.connections.get(serverId);if(connection2&&connection2.connected){connection2.connected=false;connection2.crashError="Server process exited unexpectedly";for(const[toolName,toolServerId]of this.toolToServer.entries()){if(toolServerId===serverId){this.toolToServer.delete(toolName)}}console.error(`[MCP] Server ${serverId} crashed: ${connection2.crashError}`);if(this.onServerCrash){this.onServerCrash(serverId,connection2.crashError)}}}}async listTools(){const allTools=[];this.toolToServer.clear();for(const[serverId,connection2]of this.connections){if(!connection2.connected){continue}try{let cursor;do{const result=await connection2.client.listTools(cursor?{cursor}:void 0);for(const tool of result.tools){allTools.push({name:tool.name,description:tool.description,inputSchema:tool.inputSchema,serverId});this.toolToServer.set(tool.name,serverId)}cursor=result.nextCursor}while(cursor)}catch(error){console.error(`[MCP] Failed to list tools from server ${serverId}:`,error)}}return allTools}async callTool(name,args,serverId){const targetServerId=serverId??this.toolToServer.get(name);if(!targetServerId){throw new Error(`Tool "${name}" not found. Call listTools() first to discover available tools.`)}const connection2=this.connections.get(targetServerId);if(!connection2){throw new Error(`Server "${targetServerId}" not found.`)}if(!connection2.connected){const crashMessage=connection2.crashError||"Server is not connected";throw new Error(`Server "${targetServerId}" is unavailable: ${crashMessage}`)}try{const result=await connection2.client.callTool({name,arguments:args});const content=result.content.map(item=>{if(item.type==="text"){return{type:"text",text:item.text}}else if(item.type==="image"){return{type:"image",data:item.data,mimeType:item.mimeType}}else if(item.type==="resource"){const resource=item.resource;return{type:"resource",resource:{uri:resource.uri,mimeType:resource.mimeType,text:resource.text,blob:resource.blob}}}return{type:"text",text:JSON.stringify(item)}});return{content,isError:result.isError===true}}catch(error){console.error(`[MCP] Failed to call tool "${name}" on server ${targetServerId}:`,error);throw error}}async listResources(){const allResources=[];for(const[serverId,connection2]of this.connections){if(!connection2.connected){continue}try{let cursor;do{const result=await connection2.client.listResources(cursor?{cursor}:void 0);for(const resource of result.resources){allResources.push({uri:resource.uri,name:resource.name,description:resource.description,mimeType:resource.mimeType,serverId})}cursor=result.nextCursor}while(cursor)}catch(error){console.error(`[MCP] Failed to list resources from server ${serverId}:`,error)}}return allResources}async readResource(uri,serverId){let targetServerId=serverId;if(!targetServerId){for(const[,connection3]of this.connections){if(!connection3.connected){continue}try{const result=await connection3.client.readResource({uri});const contents=result.contents.map(item=>{const resourceItem=item;if("text"in resourceItem&&resourceItem.text!==void 0){return{uri:resourceItem.uri,mimeType:resourceItem.mimeType,text:resourceItem.text}}else if("blob"in resourceItem&&resourceItem.blob!==void 0){return{uri:resourceItem.uri,mimeType:resourceItem.mimeType,blob:resourceItem.blob}}return{uri:resourceItem.uri,mimeType:resourceItem.mimeType,text:""}});return{contents}}catch{continue}}throw new Error(`Resource "${uri}" not found on any connected server.`)}const connection2=this.connections.get(targetServerId);if(!connection2){throw new Error(`Server "${targetServerId}" not found.`)}if(!connection2.connected){throw new Error(`Server "${targetServerId}" is not connected.`)}try{const result=await connection2.client.readResource({uri});const contents=result.contents.map(item=>{const resourceItem=item;if("text"in resourceItem&&resourceItem.text!==void 0){return{uri:resourceItem.uri,mimeType:resourceItem.mimeType,text:resourceItem.text}}else if("blob"in resourceItem&&resourceItem.blob!==void 0){return{uri:resourceItem.uri,mimeType:resourceItem.mimeType,blob:resourceItem.blob}}return{uri:resourceItem.uri,mimeType:resourceItem.mimeType,text:""}});return{contents}}catch(error){console.error(`[MCP] Failed to read resource "${uri}" from server ${targetServerId}:`,error);throw error}}getConnection(serverId){return this.connections.get(serverId)}getAllConnections(){return Array.from(this.connections.values()).filter(conn=>conn.connected)}getServerCapabilities(serverId){const connection2=this.connections.get(serverId);return connection2?.connected?connection2.capabilities:void 0}async close(){for(const connection2 of this.connections.values()){try{await connection2.client.close();connection2.connected=false}catch(error){console.error(`[MCP] Error closing connection ${connection2.config.id}:`,error)}}this.connections.clear();this.toolToServer.clear()}abortPendingOperations(){for(const connection2 of this.connections.values()){connection2.connected=false}}isServerCrashed(serverId){const connection2=this.connections.get(serverId);return connection2!==void 0&&!connection2.connected&&connection2.crashError!==void 0}getServerCrashError(serverId){const connection2=this.connections.get(serverId);return connection2?.crashError}getCrashedServers(){const crashed=[];for(const[serverId,connection2]of this.connections){if(!connection2.connected&&connection2.crashError){crashed.push({serverId,error:connection2.crashError})}}return crashed}};var defaultMcpManagerFactory=()=>new MCPManager;var Session=class{id;cwd;mcpManager;cancelled=false;createdAt;history=[];constructor(id,cwd,mcpManagerFactory){this.id=id;this.cwd=cwd;this.mcpManager=(mcpManagerFactory??defaultMcpManagerFactory)();this.createdAt=new Date}isCancelled(){return this.cancelled}cancel(){this.cancelled=true;this.mcpManager.abortPendingOperations()}addHistoryEntry(role,content){this.history.push({role,content,timestamp:new Date})}getHistory(){return[...this.history]}clearHistory(){this.history=[]}getState(){return{id:this.id,cwd:this.cwd,cancelled:this.cancelled,createdAt:this.createdAt,history:[...this.history]}}async close(){await this.mcpManager.close()}};var SessionManager=class{sessions=new Map;mcpManagerFactory;constructor(mcpManagerFactory){this.mcpManagerFactory=mcpManagerFactory}async createSession(cwd,mcpServers){const id=this.generateSessionId();const session=new Session(id,cwd,this.mcpManagerFactory);if(mcpServers&&mcpServers.length>0){await session.mcpManager.connect(mcpServers)}this.sessions.set(id,session);return session}getSession(id){return this.sessions.get(id)}cancelSession(id){const session=this.sessions.get(id);if(session){session.cancel();return true}return false}async closeSession(id){const session=this.sessions.get(id);if(session){await session.close();this.sessions.delete(id);return true}return false}async closeAll(){for(const session of this.sessions.values()){await session.close()}this.sessions.clear()}getAllSessions(){return Array.from(this.sessions.values())}removeSession(id){return this.sessions.delete(id)}generateSessionId(){return crypto.randomUUID()}};var ACPAgent=class{_connection;_sessionManager;_clientCapabilities=null;constructor(connection2){this._connection=connection2;this._sessionManager=new SessionManager}get connection(){return this._connection}get sessionManager(){return this._sessionManager}get clientCapabilities(){return this._clientCapabilities}async initialize(params){this._clientCapabilities=params.clientCapabilities??null;return{protocolVersion:PROTOCOL_VERSION,agentInfo:{name:"stdio-bus-worker",version:"1.0.0"},agentCapabilities:{promptCapabilities:{embeddedContext:true}},authMethods:[]}}async newSession(params){const mcpServers=params.mcpServers?.map(server=>{if("command"in server){return{id:server.name,command:server.command,args:server.args,env:server.env?.reduce((acc,envVar)=>{acc[envVar.name]=envVar.value;return acc},{})}}return null}).filter(s=>s!==null);const session=await this._sessionManager.createSession(params.cwd,mcpServers);return{sessionId:session.id}}async loadSession(_params){return{}}async authenticate(_params){}async prompt(params){const session=this._sessionManager.getSession(params.sessionId);if(!session){throw new Error(`Session not found: ${params.sessionId}`)}if(session.isCancelled()){return{stopReason:"cancelled"}}for(const block of params.prompt){if(session.isCancelled()){return{stopReason:"cancelled"}}if(block.type==="text"){await this._connection.sessionUpdate({sessionId:params.sessionId,update:{sessionUpdate:"agent_message_chunk",content:{type:"text",text:block.text}}})}else if(block.type==="resource_link"){const resourceLink=block;try{const result=await session.mcpManager.readResource(resourceLink.uri);if(result.contents.length>0){const content=result.contents[0];if("text"in content){await this._connection.sessionUpdate({sessionId:params.sessionId,update:{sessionUpdate:"agent_message_chunk",content:{type:"text",text:`[Resource: ${resourceLink.name}]
|
|
3
|
+
${content.text}`}}})}else if("blob"in content){await this._connection.sessionUpdate({sessionId:params.sessionId,update:{sessionUpdate:"agent_message_chunk",content:{type:"text",text:`[Resource: ${resourceLink.name}] (binary data, ${content.blob.length} bytes)`}}})}}}catch{await this._connection.sessionUpdate({sessionId:params.sessionId,update:{sessionUpdate:"agent_message_chunk",content:{type:"text",text:`[Resource link: ${resourceLink.name} (${resourceLink.uri})]`}}})}}else if(block.type==="resource"){const resource=block;if(resource.resource.text!==void 0){await this._connection.sessionUpdate({sessionId:params.sessionId,update:{sessionUpdate:"agent_message_chunk",content:{type:"text",text:`[Embedded resource: ${resource.resource.uri}]
|
|
4
|
+
${resource.resource.text}`}}})}else if(resource.resource.blob!==void 0){await this._connection.sessionUpdate({sessionId:params.sessionId,update:{sessionUpdate:"agent_message_chunk",content:{type:"text",text:`[Embedded resource: ${resource.resource.uri}] (binary data)`}}})}}else if(block.type==="image"){const image=block;await this._connection.sessionUpdate({sessionId:params.sessionId,update:{sessionUpdate:"agent_message_chunk",content:{type:"text",text:`[Image: ${image.mimeType}]`}}})}}if(session.isCancelled()){return{stopReason:"cancelled"}}return{stopReason:"end_turn"}}async cancel(params){this._sessionManager.cancelSession(params.sessionId)}};var PlatformNotSupportedError=class extends Error{constructor(agentId,platform){super(`Platform not supported: ${platform} for agent ${agentId}`);this.agentId=agentId;this.platform=platform;this.name="PlatformNotSupportedError"}};var NoDistributionError=class extends Error{constructor(agentId){super(`No supported distribution type for agent ${agentId}`);this.agentId=agentId;this.name="NoDistributionError"}};function getCurrentPlatform(){const platform=process.platform;const arch=process.arch;if(platform==="darwin"&&arch==="arm64")return"darwin-aarch64";if(platform==="darwin"&&arch==="x64")return"darwin-x86_64";if(platform==="linux"&&arch==="arm64")return"linux-aarch64";if(platform==="linux"&&arch==="x64")return"linux-x86_64";if(platform==="win32"&&arch==="arm64")return"windows-aarch64";if(platform==="win32"&&arch==="x64")return"windows-x86_64";return"linux-x86_64"}function resolveBinary(distribution,agentId){const currentPlatform=getCurrentPlatform();const target=distribution[currentPlatform];if(!target){throw new PlatformNotSupportedError(agentId,currentPlatform)}return{command:target.cmd,args:target.args??[],env:target.env}}function resolveNpx(distribution){return{command:"npx",args:[distribution.package,...distribution.args??[]],env:distribution.env}}function resolveUvx(distribution){return{command:"uvx",args:[distribution.package,...distribution.args??[]],env:distribution.env}}function resolve(distribution,agentId){if(distribution.npx){return resolveNpx(distribution.npx)}if(distribution.uvx){return resolveUvx(distribution.uvx)}if(distribution.binary){return resolveBinary(distribution.binary,agentId)}throw new NoDistributionError(agentId)}import{spawn}from"child_process";var DEFAULT_TERMINATE_TIMEOUT_MS=5e3;var AgentRuntimeImpl=class _AgentRuntimeImpl{agentId;state;process;onExitCallback;constructor(agentId,process2,onExit){this.agentId=agentId;this.process=process2;this.state="starting";this.onExitCallback=onExit;this.setupProcessHandlers()}static spawn(agentId,spawnCommand,onExit){const childProcess=spawn(spawnCommand.command,spawnCommand.args,{stdio:["pipe","pipe","pipe"],env:{...process.env,...spawnCommand.env},detached:false});if(childProcess.stdout){childProcess.stdout.setEncoding("utf8")}if(childProcess.stderr){childProcess.stderr.setEncoding("utf8")}return new _AgentRuntimeImpl(agentId,childProcess,onExit)}setupProcessHandlers(){this.process.on("spawn",()=>{if(this.state==="starting"){this.state="running"}});this.process.on("error",error=>{this.state="stopped";process.stderr.write(`[${new Date().toISOString()}] ERROR: Agent ${this.agentId} process error: ${error.message}
|
|
5
|
+
`)});this.process.on("exit",(code,signal)=>{this.state="stopped";if(this.onExitCallback){this.onExitCallback(code,signal)}});if(this.process.stdin){this.process.stdin.on("error",error=>{process.stderr.write(`[${new Date().toISOString()}] WARN: Agent ${this.agentId} stdin error: ${error.message}
|
|
6
|
+
`)})}}write(message){if(this.state!=="running"&&this.state!=="starting"){return false}if(!this.process.stdin||this.process.stdin.destroyed){return false}try{const ndjsonLine=JSON.stringify(message)+"\n";return this.process.stdin.write(ndjsonLine)}catch{return false}}async terminate(timeout=DEFAULT_TERMINATE_TIMEOUT_MS){if(this.state==="stopped"){return}if(this.state==="stopping"){return this.waitForExit()}this.state="stopping";if(this.process.stdin&&!this.process.stdin.destroyed){this.process.stdin.end()}this.process.kill("SIGTERM");const exitPromise=this.waitForExit();const timeoutPromise=new Promise(resolve2=>{setTimeout(()=>resolve2("timeout"),timeout)});const result=await Promise.race([exitPromise,timeoutPromise]);if(result==="timeout"&&!this.process.killed&&this.process.exitCode===null){this.process.kill("SIGKILL");await this.waitForExit()}}waitForExit(){if(this.state==="stopped"){return Promise.resolve()}return new Promise(resolve2=>{this.process.once("exit",()=>{resolve2()})})}};var DEFAULT_SHUTDOWN_TIMEOUT_MS=5e3;var AgentRuntimeManager=class{runtimes=new Map;exitCallbacks=[];async getOrSpawn(agentId,spawnCommand){const existing=this.runtimes.get(agentId);if(existing&&existing.state!=="stopped"){return existing}const runtime=AgentRuntimeImpl.spawn(agentId,spawnCommand,(code,_signal)=>{this.handleAgentExit(agentId,code)});this.runtimes.set(agentId,runtime);return runtime}get(agentId){return this.runtimes.get(agentId)}async terminate(agentId,timeout=DEFAULT_SHUTDOWN_TIMEOUT_MS){const runtime=this.runtimes.get(agentId);if(!runtime){return}await runtime.terminate(timeout);this.runtimes.delete(agentId)}async terminateAll(timeout=DEFAULT_SHUTDOWN_TIMEOUT_MS){const terminatePromises=[];for(const[agentId,runtime]of this.runtimes){if(runtime.state!=="stopped"){terminatePromises.push(runtime.terminate(timeout).then(()=>{this.runtimes.delete(agentId)}))}}await Promise.all(terminatePromises)}onAgentExit(callback){this.exitCallbacks.push(callback)}handleAgentExit(agentId,code){this.runtimes.delete(agentId);for(const callback of this.exitCallbacks){try{callback(agentId,code)}catch{}}}get size(){return this.runtimes.size}has(agentId){return this.runtimes.has(agentId)}};function logError(message){const timestamp=new Date().toISOString();console.error(`[${timestamp}] [ERROR] [ndjson] ${message}`)}var NDJSONHandler=class{buffer="";output;messageCallback=null;errorCallback=null;constructor(output){this.output=output}onMessage(callback){this.messageCallback=callback}onError(callback){this.errorCallback=callback}write(message){if(!this.output.writable){return false}try{const json=JSON.stringify(message);this.output.write(json+"\n");return true}catch{return false}}processChunk(chunk){this.buffer+=chunk.toString("utf-8");this.processBuffer()}processBuffer(){let newlineIndex;while((newlineIndex=this.buffer.indexOf("\n"))!==-1){const line=this.buffer.slice(0,newlineIndex);this.buffer=this.buffer.slice(newlineIndex+1);if(line.trim().length===0){continue}this.parseLine(line)}}parseLine(line){try{const message=JSON.parse(line);if(message===null||typeof message!=="object"){const error=new Error("Parsed JSON is not an object");logError(`Malformed NDJSON line (not an object): ${this.truncateLine(line)}`);this.errorCallback?.(error,line);return}this.messageCallback?.(message)}catch(error){logError(`Failed to parse NDJSON line: ${this.truncateLine(line)}`);this.errorCallback?.(error,line)}}truncateLine(line,maxLength=100){if(line.length<=maxLength){return line}return line.slice(0,maxLength)+"..."}};var AgentNotFoundError=class extends Error{constructor(agentId){super(`Agent not found: ${agentId}`);this.agentId=agentId;this.name="AgentNotFoundError"}};function getAgentApiKey(apiKeys,agentId){const keys=apiKeys[agentId];if(!keys||!keys.apiKey||keys.apiKey.length===0){return void 0}return keys.apiKey}var RoutingErrorCodes={MISSING_AGENT_ID:-32600,AGENT_NOT_FOUND:-32001,PLATFORM_NOT_SUPPORTED:-32002,SPAWN_FAILED:-32003};function logError2(message){const timestamp=new Date().toISOString();console.error(`[${timestamp}] [ERROR] [router] ${message}`)}function logInfo(message){const timestamp=new Date().toISOString();console.error(`[${timestamp}] [INFO] [router] ${message}`)}function createErrorResponse(id,code,message,data){const response={jsonrpc:"2.0",id,error:{code,message}};if(data!==void 0){response.error.data=data}return response}function extractAgentId(message){const msg=message;const agentId=msg.agentId;if(typeof agentId==="string"&&agentId.length>0){return agentId}return void 0}function extractId(message){const msg=message;const id=msg.id;if(typeof id==="string"||typeof id==="number"){return id}return null}function transformMessage(message){const{agentId:_,...rest}=message;return rest}var MessageRouter=class{registry;runtimeManager;writeCallback;apiKeys;pendingRequests=new Map;authState=new Map;sessionIdMap=new Map;constructor(registry,runtimeManager,writeCallback,apiKeys={}){this.registry=registry;this.runtimeManager=runtimeManager;this.writeCallback=writeCallback;this.apiKeys=apiKeys}async route(message){const id=extractId(message);const agentId=extractAgentId(message);if(agentId===void 0){logError2("Missing agentId in request");return createErrorResponse(id,RoutingErrorCodes.MISSING_AGENT_ID,"Missing agentId")}let spawnCommand;try{spawnCommand=this.registry.resolve(agentId)}catch(error){if(error instanceof AgentNotFoundError){logError2(`Agent not found: ${agentId}`);return createErrorResponse(id,RoutingErrorCodes.AGENT_NOT_FOUND,"Agent not found",{agentId})}if(error instanceof PlatformNotSupportedError){logError2(`Platform not supported for agent: ${agentId}`);return createErrorResponse(id,RoutingErrorCodes.PLATFORM_NOT_SUPPORTED,"Platform not supported",{agentId,platform:error.platform})}throw error}let runtime;try{runtime=await this.runtimeManager.getOrSpawn(agentId,spawnCommand)}catch(error){logError2(`Failed to spawn agent ${agentId}: ${error.message}`);return createErrorResponse(id,RoutingErrorCodes.SPAWN_FAILED,"Agent spawn failed",{agentId,error:error.message})}if(id!==null){const msg=message;const clientSessionId=typeof msg.sessionId==="string"?msg.sessionId:void 0;this.pendingRequests.set(id,{id,agentId,timestamp:Date.now(),clientSessionId})}const transformedMessage=transformMessage(message);const success=runtime.write(transformedMessage);if(!success){logError2(`Failed to write to agent ${agentId}`);if(id!==null){this.pendingRequests.delete(id)}}else{logInfo(`Routed message to agent ${agentId}`)}return void 0}handleAgentResponse(agentId,response){const id=extractId(response);const msg=response;if(id!==null){const pending=this.pendingRequests.get(id);if(pending&&pending.agentId===agentId){const result=msg.result;if(result&&Array.isArray(result.authMethods)&&result.authMethods.length>0){logInfo(`Agent ${agentId} requires authentication, attempting auto-auth`);this.authState.set(agentId,"pending");void this.attemptAuthentication(agentId,result.authMethods)}if(result&&typeof result.sessionId==="string"){const agentSessionId=result.sessionId;const clientSessionId=pending.clientSessionId;if(clientSessionId){this.sessionIdMap.set(agentSessionId,clientSessionId);logInfo(`Mapped agent sessionId ${agentSessionId} to client sessionId ${clientSessionId}`)}}this.pendingRequests.delete(id)}}if(id===null&&msg.method){const params=msg.params;if(params&&typeof params.sessionId==="string"){const agentSessionId=params.sessionId;const clientSessionId=this.sessionIdMap.get(agentSessionId);if(clientSessionId){const enriched={...msg,sessionId:clientSessionId,params:{...params,sessionId:agentSessionId}};this.writeCallback(enriched);return}else{logError2(`No sessionId mapping found for agent sessionId: ${agentSessionId}`)}}}this.writeCallback(response)}async attemptAuthentication(agentId,authMethods){const apiKey=getAgentApiKey(this.apiKeys,agentId);if(!apiKey){logError2(`No API key found for agent ${agentId}, authentication will fail`);this.authState.set(agentId,"none");return}let selectedMethod=authMethods.find(m=>m.id==="openai-api-key");if(!selectedMethod){selectedMethod=authMethods.find(m=>m.id.includes("api-key")||m.id.includes("apikey"))}if(!selectedMethod){selectedMethod=authMethods[0]}logInfo(`Authenticating agent ${agentId} with method: ${selectedMethod.id}`);let runtime;try{const spawnCommand=this.registry.resolve(agentId);runtime=await this.runtimeManager.getOrSpawn(agentId,spawnCommand)}catch(error){logError2(`Failed to get runtime for authentication: ${error.message}`);this.authState.set(agentId,"none");return}const authRequest={jsonrpc:"2.0",id:`auth-${agentId}-${Date.now()}`,method:"authenticate",params:{methodId:selectedMethod.id,credentials:{apiKey}}};const transformed=transformMessage(authRequest);const serialized=JSON.stringify(transformed)+"\n";if(runtime.process.stdin){runtime.process.stdin.write(serialized,error=>{if(error){logError2(`Failed to send authenticate request to ${agentId}: ${error.message}`);this.authState.set(agentId,"none")}else{logInfo(`Sent authenticate request to agent ${agentId}`);this.authState.set(agentId,"authenticated")}})}}get pendingCount(){return this.pendingRequests.size}isPending(id){return this.pendingRequests.has(id)}clearPending(){this.pendingRequests.clear()}};import{readFileSync}from"node:fs";var DEFAULT_CONFIG={registryUrl:"https://cdn.agentclientprotocol.com/registry/v1/latest/registry.json",apiKeysPath:"./api-keys.json",shutdownTimeoutSec:5};var ENV_REGISTRY_URL="ACP_REGISTRY_URL";var ENV_API_KEYS_PATH="ACP_API_KEYS_PATH";function logWarning(message){const timestamp=new Date().toISOString();console.error(`[${timestamp}] [WARN] [config] ${message}`)}function isNonEmptyString(value){return typeof value==="string"&&value.length>0}function isPositiveNumber(value){return typeof value==="number"&&value>0&&Number.isFinite(value)}function parseConfigObject(obj){const config={...DEFAULT_CONFIG};if(obj===null||typeof obj!=="object"){logWarning("Config file does not contain a valid object, using defaults");return config}const rawConfig=obj;if("registryUrl"in rawConfig){if(isNonEmptyString(rawConfig.registryUrl)){config.registryUrl=rawConfig.registryUrl}else{logWarning('Config field "registryUrl" is not a valid string, using default')}}if("apiKeysPath"in rawConfig){if(isNonEmptyString(rawConfig.apiKeysPath)){config.apiKeysPath=rawConfig.apiKeysPath}else{logWarning('Config field "apiKeysPath" is not a valid string, using default')}}if("shutdownTimeoutSec"in rawConfig){if(isPositiveNumber(rawConfig.shutdownTimeoutSec)){config.shutdownTimeoutSec=rawConfig.shutdownTimeoutSec}else{logWarning('Config field "shutdownTimeoutSec" is not a valid positive number, using default')}}return config}function applyEnvironmentOverrides(config){const envRegistryUrl=process.env[ENV_REGISTRY_URL];const envApiKeysPath=process.env[ENV_API_KEYS_PATH];const overrides={};if(isNonEmptyString(envRegistryUrl)){overrides.registryUrl=envRegistryUrl}if(isNonEmptyString(envApiKeysPath)){overrides.apiKeysPath=envApiKeysPath}return{...config,...overrides}}function loadConfig(configPath){let config={...DEFAULT_CONFIG};if(configPath){try{const fileContent=readFileSync(configPath,"utf-8");const parsed=JSON.parse(fileContent);config=parseConfigObject(parsed)}catch(error){if(error instanceof SyntaxError){logWarning(`Config file "${configPath}" contains malformed JSON, using defaults`)}else if(error.code==="ENOENT"){logWarning(`Config file "${configPath}" not found, using defaults`)}else if(error.code==="EACCES"){logWarning(`Config file "${configPath}" is not readable, using defaults`)}else{logWarning(`Failed to read config file "${configPath}": ${error.message}, using defaults`)}}}config=applyEnvironmentOverrides(config);return config}function canReadFile(capabilities){return capabilities?.fs?.readTextFile===true}function canWriteFile(capabilities){return capabilities?.fs?.writeTextFile===true}function canUseTerminal(capabilities){return capabilities?.terminal===true}async function readFile(connection2,sessionId,path,options){try{const response=await connection2.readTextFile({sessionId,path,line:options?.line,limit:options?.limit});return{content:response.content,success:true}}catch(error){const errorMessage=error instanceof Error?error.message:String(error);console.error(`[ACP] Failed to read file "${path}":`,error);return{content:"",success:false,error:errorMessage}}}async function writeFile(connection2,sessionId,path,content){try{await connection2.writeTextFile({sessionId,path,content});return{success:true}}catch(error){const errorMessage=error instanceof Error?error.message:String(error);console.error(`[ACP] Failed to write file "${path}":`,error);return{success:false,error:errorMessage}}}async function executeCommand(connection2,sessionId,command,options){let terminal=null;try{terminal=await connection2.createTerminal({sessionId,command,args:options?.args,cwd:options?.cwd,env:options?.env,outputByteLimit:options?.outputByteLimit});let timeoutId=null;let timedOut=false;if(options?.timeout&&options.timeout>0){timeoutId=setTimeout(async()=>{timedOut=true;if(terminal){try{await terminal.kill()}catch{}}},options.timeout)}try{const exitResult=await terminal.waitForExit();if(timeoutId){clearTimeout(timeoutId)}const outputResult=await terminal.currentOutput();return{output:outputResult.output,exitCode:timedOut?null:exitResult.exitCode??null,signal:timedOut?"SIGTERM":exitResult.signal??null,truncated:outputResult.truncated,success:!timedOut&&exitResult.exitCode===0,error:timedOut?"Command timed out":void 0}}finally{if(timeoutId){clearTimeout(timeoutId)}}}catch(error){const errorMessage=error instanceof Error?error.message:String(error);console.error(`[ACP] Failed to execute command "${command}":`,error);return{output:"",exitCode:null,signal:null,truncated:false,success:false,error:errorMessage}}finally{if(terminal){try{await terminal.release()}catch{}}}}async function startCommand(connection2,sessionId,command,options){return connection2.createTerminal({sessionId,command,args:options?.args,cwd:options?.cwd,env:options?.env,outputByteLimit:options?.outputByteLimit})}function mapMCPContentToACPContentBlock(mcpContent){switch(mcpContent.type){case"text":return mapTextContent(mcpContent);case"image":return mapImageContent(mcpContent);case"resource":return mapEmbeddedResource(mcpContent);default:return{type:"text",text:JSON.stringify(mcpContent)}}}function mapTextContent(mcpText){const result={type:"text",text:mcpText.text};return result}function mapImageContent(mcpImage){const result={type:"image",data:mcpImage.data,mimeType:mcpImage.mimeType};return result}function mapEmbeddedResource(mcpResource){const{resource}=mcpResource;if(resource.text!==void 0){const result={type:"resource",resource:{uri:resource.uri,mimeType:resource.mimeType,text:resource.text}};return result}else if(resource.blob!==void 0){const result={type:"resource",resource:{uri:resource.uri,mimeType:resource.mimeType,blob:resource.blob}};return result}else{const result={type:"resource",resource:{uri:resource.uri,mimeType:resource.mimeType,text:""}};return result}}function mapMCPResultToACPToolCallContent(mcpContents){return mcpContents.map(mcpContent=>{const contentBlock=mapMCPContentToACPContentBlock(mcpContent);return{type:"content",content:contentBlock}})}function createErrorToolCallContent(errorMessage){return[{type:"content",content:{type:"text",text:`Error: ${errorMessage}`}}]}function mapToolResultToACPContent(content,isError=false){if(isError&&content.length>0){const errorText=content.filter(c=>c.type==="text").map(c=>c.text).join("\n");if(errorText){return createErrorToolCallContent(errorText)}}return mapMCPResultToACPToolCallContent(content)}function isResourceLink(block){return block.type==="resource_link"&&"uri"in block}function mapMCPResourceContentsToACPContentBlock(contents){if("text"in contents){const result2={type:"resource",resource:{uri:contents.uri,mimeType:contents.mimeType,text:contents.text}};return result2}const result={type:"resource",resource:{uri:contents.uri,mimeType:contents.mimeType,blob:contents.blob}};return result}function extractResourceLinkUri(block){if(isResourceLink(block)){return block.uri}return null}var toolCallCounter=0;function generateToolCallId(){toolCallCounter++;return`tool-${Date.now()}-${toolCallCounter}`}function determineToolKind(toolName,description){const name=toolName.toLowerCase();const desc=(description||"").toLowerCase();if(desc.includes("external")||desc.includes("api")||desc.includes("http")){return"fetch"}if(name.includes("read")||name.includes("get")||name.includes("list")||name.includes("fetch")){return"read"}if(name.includes("write")||name.includes("edit")||name.includes("update")||name.includes("modify")){return"edit"}if(name.includes("delete")||name.includes("remove")){return"delete"}if(name.includes("move")||name.includes("rename")){return"move"}if(name.includes("search")||name.includes("find")||name.includes("query")){return"search"}if(name.includes("exec")||name.includes("run")||name.includes("shell")||name.includes("command")){return"execute"}if(name.includes("http")||name.includes("api")||name.includes("request")){return"fetch"}return"other"}async function sendToolCallInitiation(connection2,sessionId,toolCallId,title,kind="other",status="pending"){await connection2.sessionUpdate({sessionId,update:{sessionUpdate:"tool_call",toolCallId,title,kind,status}})}async function sendToolCallUpdate(connection2,sessionId,toolCallId,status,content,title){await connection2.sessionUpdate({sessionId,update:{sessionUpdate:"tool_call_update",toolCallId,status,content,title}})}async function requestToolPermission(connection2,sessionId,toolCallId,title,kind="other",options){const permissionOptions=options??[{optionId:"allow_once",name:"Allow once",kind:"allow_once"},{optionId:"allow_always",name:"Allow always",kind:"allow_always"},{optionId:"reject_once",name:"Reject",kind:"reject_once"}];const toolCall={toolCallId,title,kind,status:"pending"};try{const response=await connection2.requestPermission({sessionId,toolCall,options:permissionOptions});if(response.outcome.outcome==="cancelled"){return{granted:false,cancelled:true}}if(response.outcome.outcome==="selected"){const selectedOption=response.outcome.optionId;const isAllowed=selectedOption.startsWith("allow");return{granted:isAllowed,optionId:selectedOption,cancelled:false}}return{granted:false,cancelled:false}}catch(error){console.error("[ACP] Permission request failed:",error);return{granted:false,cancelled:true}}}async function executeToolCall(connection2,sessionId,mcpManager,toolName,args,description){const toolCallId=generateToolCallId();const kind=determineToolKind(toolName,description);const title=`Executing: ${toolName}`;try{await sendToolCallInitiation(connection2,sessionId,toolCallId,title,kind,"pending");await sendToolCallUpdate(connection2,sessionId,toolCallId,"in_progress");const result=await mcpManager.callTool(toolName,args);const content=mapToolResultToACPContent(result.content,result.isError);const finalStatus=result.isError?"failed":"completed";await sendToolCallUpdate(connection2,sessionId,toolCallId,finalStatus,content);return content}catch(error){const errorMessage=error instanceof Error?error.message:String(error);const errorContent=[{type:"content",content:{type:"text",text:`Error: ${errorMessage}`}}];await sendToolCallUpdate(connection2,sessionId,toolCallId,"failed",errorContent);return errorContent}}async function executeToolCallWithPermission(connection2,sessionId,mcpManager,toolName,args,description,requirePermission=true){const toolCallId=generateToolCallId();const kind=determineToolKind(toolName,description);const title=`Executing: ${toolName}`;try{await sendToolCallInitiation(connection2,sessionId,toolCallId,title,kind,"pending");if(requirePermission){const permissionResult=await requestToolPermission(connection2,sessionId,toolCallId,title,kind);if(!permissionResult.granted){const status=permissionResult.cancelled?"failed":"failed";const message=permissionResult.cancelled?"Permission request cancelled":"Permission denied";const errorContent=[{type:"content",content:{type:"text",text:message}}];await sendToolCallUpdate(connection2,sessionId,toolCallId,status,errorContent);return{content:errorContent,permissionResult}}}await sendToolCallUpdate(connection2,sessionId,toolCallId,"in_progress");const result=await mcpManager.callTool(toolName,args);const content=mapToolResultToACPContent(result.content,result.isError);const finalStatus=result.isError?"failed":"completed";await sendToolCallUpdate(connection2,sessionId,toolCallId,finalStatus,content);return{content}}catch(error){const errorMessage=error instanceof Error?error.message:String(error);const errorContent=[{type:"content",content:{type:"text",text:`Error: ${errorMessage}`}}];await sendToolCallUpdate(connection2,sessionId,toolCallId,"failed",errorContent);return{content:errorContent}}}console.error("[worker] Starting ACP/MCP Protocol Worker...");var inputStream=Readable.toWeb(process.stdin);var outputStream=Writable.toWeb(process.stdout);var stream=ndJsonStream(outputStream,inputStream);var connection=new AgentSideConnection(conn=>new ACPAgent(conn),stream);console.error("[worker] AgentSideConnection established, ready for messages");process.on("SIGTERM",async()=>{console.error("[worker] Received SIGTERM, shutting down...");await connection.closed;process.exit(0)});process.on("SIGINT",async()=>{console.error("[worker] Received SIGINT, shutting down...");await connection.closed;process.exit(0)});process.on("uncaughtException",error=>{console.error("[worker] Uncaught exception:",error);process.exit(1)});process.on("unhandledRejection",(reason,promise)=>{console.error("[worker] Unhandled rejection at:",promise,"reason:",reason)});connection.closed.then(()=>{console.error("[worker] Connection closed");process.exit(0)}).catch(error=>{console.error("[worker] Connection error:",error);process.exit(1)});export{ACPAgent,AgentRuntimeImpl,AgentRuntimeManager,MCPManager,MessageRouter,NDJSONHandler,NoDistributionError,PlatformNotSupportedError,RoutingErrorCodes,canReadFile,canUseTerminal,canWriteFile,createErrorResponse,createErrorToolCallContent,determineToolKind,executeCommand,executeToolCall,executeToolCallWithPermission,extractAgentId,extractId,extractResourceLinkUri,generateToolCallId,getCurrentPlatform,isResourceLink,loadConfig,mapMCPContentToACPContentBlock,mapMCPResourceContentsToACPContentBlock,mapMCPResultToACPToolCallContent,mapToolResultToACPContent,readFile,requestToolPermission,resolve,resolveBinary,resolveNpx,resolveUvx,sendToolCallInitiation,sendToolCallUpdate,startCommand,transformMessage,writeFile};
|
|
7
|
+
//# sourceMappingURL=index.js.map
|