@multi-agent-protocol/sdk 0.0.4 → 0.0.5
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/dist/{index-Z76qC_Us.d.cts → index-DMcRbXOS.d.cts} +1226 -1
- package/dist/{index-Z76qC_Us.d.ts → index-DMcRbXOS.d.ts} +1226 -1
- package/dist/index.cjs +1032 -0
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +124 -15
- package/dist/index.d.ts +124 -15
- package/dist/index.js +1020 -1
- package/dist/index.js.map +1 -1
- package/dist/testing.cjs +650 -0
- package/dist/testing.cjs.map +1 -1
- package/dist/testing.d.cts +2 -1
- package/dist/testing.d.ts +2 -1
- package/dist/testing.js +650 -0
- package/dist/testing.js.map +1 -1
- package/package.json +1 -1
package/dist/testing.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { ulid } from 'ulid';
|
|
2
|
+
import { EventEmitter } from 'events';
|
|
2
3
|
|
|
3
4
|
// src/jsonrpc/index.ts
|
|
4
5
|
function isRequest(message) {
|
|
@@ -61,6 +62,7 @@ var EVENT_TYPES = {
|
|
|
61
62
|
PARTICIPANT_DISCONNECTED: "participant_disconnected",
|
|
62
63
|
// Message events
|
|
63
64
|
MESSAGE_SENT: "message_sent",
|
|
65
|
+
MESSAGE_DELIVERED: "message_delivered",
|
|
64
66
|
// Scope events
|
|
65
67
|
SCOPE_CREATED: "scope_created",
|
|
66
68
|
SCOPE_MEMBER_JOINED: "scope_member_joined",
|
|
@@ -1396,6 +1398,11 @@ var TestServer = class {
|
|
|
1396
1398
|
if (participant) {
|
|
1397
1399
|
this.deliverMessage(recipientId, message);
|
|
1398
1400
|
delivered.push(recipientId);
|
|
1401
|
+
this.emitEvent({
|
|
1402
|
+
type: EVENT_TYPES.MESSAGE_DELIVERED,
|
|
1403
|
+
source: senderAgentId ?? senderId,
|
|
1404
|
+
data: { messageId, message, correlationId: params.meta?.correlationId }
|
|
1405
|
+
});
|
|
1399
1406
|
}
|
|
1400
1407
|
}
|
|
1401
1408
|
this.emitEvent({
|
|
@@ -1756,6 +1763,9 @@ var TestServer = class {
|
|
|
1756
1763
|
if (typeof address === "string") {
|
|
1757
1764
|
return [this.#findParticipantForAgent(address) ?? address];
|
|
1758
1765
|
}
|
|
1766
|
+
if ("participant" in address && address.participant) {
|
|
1767
|
+
return this.#participants.has(address.participant) ? [address.participant] : [];
|
|
1768
|
+
}
|
|
1759
1769
|
if ("agent" in address && !("system" in address)) {
|
|
1760
1770
|
const participantId = this.#findParticipantForAgent(address.agent);
|
|
1761
1771
|
return participantId ? [participantId] : [];
|
|
@@ -2552,12 +2562,589 @@ function createSubscription(id, unsubscribe, options, sendAck) {
|
|
|
2552
2562
|
return new Subscription(id, unsubscribe, options, sendAck);
|
|
2553
2563
|
}
|
|
2554
2564
|
|
|
2565
|
+
// src/acp/types.ts
|
|
2566
|
+
var ACPError = class _ACPError extends Error {
|
|
2567
|
+
code;
|
|
2568
|
+
data;
|
|
2569
|
+
constructor(code, message, data) {
|
|
2570
|
+
super(message);
|
|
2571
|
+
this.name = "ACPError";
|
|
2572
|
+
this.code = code;
|
|
2573
|
+
this.data = data;
|
|
2574
|
+
}
|
|
2575
|
+
/**
|
|
2576
|
+
* Create an ACPError from an error response.
|
|
2577
|
+
*/
|
|
2578
|
+
static fromResponse(error) {
|
|
2579
|
+
return new _ACPError(error.code, error.message, error.data);
|
|
2580
|
+
}
|
|
2581
|
+
/**
|
|
2582
|
+
* Convert to JSON-RPC error object.
|
|
2583
|
+
*/
|
|
2584
|
+
toErrorObject() {
|
|
2585
|
+
return {
|
|
2586
|
+
code: this.code,
|
|
2587
|
+
message: this.message,
|
|
2588
|
+
...this.data !== void 0 && { data: this.data }
|
|
2589
|
+
};
|
|
2590
|
+
}
|
|
2591
|
+
};
|
|
2592
|
+
var ACP_METHODS = {
|
|
2593
|
+
// Lifecycle
|
|
2594
|
+
INITIALIZE: "initialize",
|
|
2595
|
+
AUTHENTICATE: "authenticate",
|
|
2596
|
+
// Session management
|
|
2597
|
+
SESSION_NEW: "session/new",
|
|
2598
|
+
SESSION_LOAD: "session/load",
|
|
2599
|
+
SESSION_SET_MODE: "session/set_mode",
|
|
2600
|
+
// Prompt
|
|
2601
|
+
SESSION_PROMPT: "session/prompt",
|
|
2602
|
+
SESSION_CANCEL: "session/cancel",
|
|
2603
|
+
// Notifications
|
|
2604
|
+
SESSION_UPDATE: "session/update",
|
|
2605
|
+
// Agent→Client requests
|
|
2606
|
+
REQUEST_PERMISSION: "request_permission",
|
|
2607
|
+
FS_READ_TEXT_FILE: "fs/read_text_file",
|
|
2608
|
+
FS_WRITE_TEXT_FILE: "fs/write_text_file",
|
|
2609
|
+
TERMINAL_CREATE: "terminal/create",
|
|
2610
|
+
TERMINAL_OUTPUT: "terminal/output",
|
|
2611
|
+
TERMINAL_RELEASE: "terminal/release",
|
|
2612
|
+
TERMINAL_WAIT_FOR_EXIT: "terminal/wait_for_exit",
|
|
2613
|
+
TERMINAL_KILL: "terminal/kill"
|
|
2614
|
+
};
|
|
2615
|
+
function isACPEnvelope(payload) {
|
|
2616
|
+
if (typeof payload !== "object" || payload === null) {
|
|
2617
|
+
return false;
|
|
2618
|
+
}
|
|
2619
|
+
const envelope = payload;
|
|
2620
|
+
if (typeof envelope.acp !== "object" || envelope.acp === null || typeof envelope.acpContext !== "object" || envelope.acpContext === null) {
|
|
2621
|
+
return false;
|
|
2622
|
+
}
|
|
2623
|
+
const acpContext = envelope.acpContext;
|
|
2624
|
+
return typeof acpContext.streamId === "string";
|
|
2625
|
+
}
|
|
2626
|
+
|
|
2627
|
+
// src/acp/stream.ts
|
|
2628
|
+
var ACPStreamConnection = class extends EventEmitter {
|
|
2629
|
+
#mapClient;
|
|
2630
|
+
#options;
|
|
2631
|
+
#streamId;
|
|
2632
|
+
#pendingRequests = /* @__PURE__ */ new Map();
|
|
2633
|
+
#subscription = null;
|
|
2634
|
+
#sessionId = null;
|
|
2635
|
+
#initialized = false;
|
|
2636
|
+
#capabilities = null;
|
|
2637
|
+
#closed = false;
|
|
2638
|
+
#lastEventId = null;
|
|
2639
|
+
#isReconnecting = false;
|
|
2640
|
+
#unsubscribeReconnection = null;
|
|
2641
|
+
/**
|
|
2642
|
+
* Create a new ACP stream connection.
|
|
2643
|
+
*
|
|
2644
|
+
* @param mapClient - The underlying MAP client connection
|
|
2645
|
+
* @param options - Stream configuration options
|
|
2646
|
+
*/
|
|
2647
|
+
constructor(mapClient, options) {
|
|
2648
|
+
super();
|
|
2649
|
+
this.#mapClient = mapClient;
|
|
2650
|
+
this.#options = options;
|
|
2651
|
+
this.#streamId = `acp-${Date.now()}-${Math.random().toString(36).slice(2)}`;
|
|
2652
|
+
this.#unsubscribeReconnection = mapClient.onReconnection((event) => {
|
|
2653
|
+
void this.#handleReconnectionEvent(event);
|
|
2654
|
+
});
|
|
2655
|
+
}
|
|
2656
|
+
// ===========================================================================
|
|
2657
|
+
// Public Properties
|
|
2658
|
+
// ===========================================================================
|
|
2659
|
+
/** Unique identifier for this ACP stream */
|
|
2660
|
+
get streamId() {
|
|
2661
|
+
return this.#streamId;
|
|
2662
|
+
}
|
|
2663
|
+
/** Target agent this stream connects to */
|
|
2664
|
+
get targetAgent() {
|
|
2665
|
+
return this.#options.targetAgent;
|
|
2666
|
+
}
|
|
2667
|
+
/** Current ACP session ID (null until newSession called) */
|
|
2668
|
+
get sessionId() {
|
|
2669
|
+
return this.#sessionId;
|
|
2670
|
+
}
|
|
2671
|
+
/** Whether initialize() has been called */
|
|
2672
|
+
get initialized() {
|
|
2673
|
+
return this.#initialized;
|
|
2674
|
+
}
|
|
2675
|
+
/** Agent capabilities from initialize response */
|
|
2676
|
+
get capabilities() {
|
|
2677
|
+
return this.#capabilities;
|
|
2678
|
+
}
|
|
2679
|
+
/** Whether the stream is closed */
|
|
2680
|
+
get isClosed() {
|
|
2681
|
+
return this.#closed;
|
|
2682
|
+
}
|
|
2683
|
+
/** Last processed event ID (for reconnection support) */
|
|
2684
|
+
get lastEventId() {
|
|
2685
|
+
return this.#lastEventId;
|
|
2686
|
+
}
|
|
2687
|
+
/** Whether the stream is currently reconnecting */
|
|
2688
|
+
get isReconnecting() {
|
|
2689
|
+
return this.#isReconnecting;
|
|
2690
|
+
}
|
|
2691
|
+
// ===========================================================================
|
|
2692
|
+
// Reconnection Handling
|
|
2693
|
+
// ===========================================================================
|
|
2694
|
+
/**
|
|
2695
|
+
* Handle MAP reconnection events.
|
|
2696
|
+
*/
|
|
2697
|
+
async #handleReconnectionEvent(event) {
|
|
2698
|
+
if (this.#closed) return;
|
|
2699
|
+
switch (event.type) {
|
|
2700
|
+
case "disconnected":
|
|
2701
|
+
this.#isReconnecting = true;
|
|
2702
|
+
this.emit("reconnecting");
|
|
2703
|
+
for (const [id, pending] of this.#pendingRequests) {
|
|
2704
|
+
clearTimeout(pending.timeout);
|
|
2705
|
+
pending.reject(new Error("Connection lost during ACP request"));
|
|
2706
|
+
this.#pendingRequests.delete(id);
|
|
2707
|
+
}
|
|
2708
|
+
break;
|
|
2709
|
+
case "reconnected":
|
|
2710
|
+
await this.#handleReconnected();
|
|
2711
|
+
break;
|
|
2712
|
+
case "reconnectFailed":
|
|
2713
|
+
this.#isReconnecting = false;
|
|
2714
|
+
this.emit("error", event.error ?? new Error("MAP reconnection failed"));
|
|
2715
|
+
break;
|
|
2716
|
+
}
|
|
2717
|
+
}
|
|
2718
|
+
/**
|
|
2719
|
+
* Handle successful MAP reconnection.
|
|
2720
|
+
*/
|
|
2721
|
+
async #handleReconnected() {
|
|
2722
|
+
this.#subscription = null;
|
|
2723
|
+
try {
|
|
2724
|
+
await this.#setupSubscription();
|
|
2725
|
+
if (this.#sessionId) {
|
|
2726
|
+
const sessionValid = await this.#verifySessionValid();
|
|
2727
|
+
if (!sessionValid) {
|
|
2728
|
+
const lostSessionId = this.#sessionId;
|
|
2729
|
+
this.#sessionId = null;
|
|
2730
|
+
this.emit("sessionLost", {
|
|
2731
|
+
sessionId: lostSessionId,
|
|
2732
|
+
reason: "Session no longer valid after reconnection"
|
|
2733
|
+
});
|
|
2734
|
+
}
|
|
2735
|
+
}
|
|
2736
|
+
this.#isReconnecting = false;
|
|
2737
|
+
this.emit("reconnected");
|
|
2738
|
+
} catch (error) {
|
|
2739
|
+
this.#isReconnecting = false;
|
|
2740
|
+
this.emit("error", error instanceof Error ? error : new Error(String(error)));
|
|
2741
|
+
}
|
|
2742
|
+
}
|
|
2743
|
+
/**
|
|
2744
|
+
* Verify that the current ACP session is still valid.
|
|
2745
|
+
*
|
|
2746
|
+
* Since ACP doesn't have a dedicated status check method, we attempt
|
|
2747
|
+
* a lightweight operation (cancel with no effect) to verify the session.
|
|
2748
|
+
*/
|
|
2749
|
+
async #verifySessionValid() {
|
|
2750
|
+
if (!this.#sessionId) return false;
|
|
2751
|
+
try {
|
|
2752
|
+
await this.#sendNotification(ACP_METHODS.SESSION_CANCEL, {
|
|
2753
|
+
sessionId: this.#sessionId,
|
|
2754
|
+
reason: "session_verification"
|
|
2755
|
+
});
|
|
2756
|
+
return true;
|
|
2757
|
+
} catch {
|
|
2758
|
+
return false;
|
|
2759
|
+
}
|
|
2760
|
+
}
|
|
2761
|
+
// ===========================================================================
|
|
2762
|
+
// Internal Methods
|
|
2763
|
+
// ===========================================================================
|
|
2764
|
+
/**
|
|
2765
|
+
* Set up the subscription for receiving messages from the target agent.
|
|
2766
|
+
*/
|
|
2767
|
+
async #setupSubscription() {
|
|
2768
|
+
if (this.#subscription) return;
|
|
2769
|
+
this.#subscription = await this.#mapClient.subscribe({
|
|
2770
|
+
fromAgents: [this.#options.targetAgent]
|
|
2771
|
+
});
|
|
2772
|
+
void this.#processEvents();
|
|
2773
|
+
}
|
|
2774
|
+
/**
|
|
2775
|
+
* Process incoming events from the subscription.
|
|
2776
|
+
*/
|
|
2777
|
+
async #processEvents() {
|
|
2778
|
+
if (!this.#subscription) return;
|
|
2779
|
+
try {
|
|
2780
|
+
for await (const event of this.#subscription) {
|
|
2781
|
+
if (this.#closed) break;
|
|
2782
|
+
if (event.id) {
|
|
2783
|
+
this.#lastEventId = event.id;
|
|
2784
|
+
}
|
|
2785
|
+
if (event.type === "message_delivered" && event.data) {
|
|
2786
|
+
const message = event.data.message;
|
|
2787
|
+
if (message?.payload) {
|
|
2788
|
+
await this.#handleIncomingMessage(message);
|
|
2789
|
+
}
|
|
2790
|
+
}
|
|
2791
|
+
}
|
|
2792
|
+
} catch (error) {
|
|
2793
|
+
if (!this.#closed) {
|
|
2794
|
+
this.emit("error", error);
|
|
2795
|
+
}
|
|
2796
|
+
}
|
|
2797
|
+
}
|
|
2798
|
+
/**
|
|
2799
|
+
* Handle an incoming message from the target agent.
|
|
2800
|
+
*/
|
|
2801
|
+
async #handleIncomingMessage(message) {
|
|
2802
|
+
const payload = message.payload;
|
|
2803
|
+
if (!isACPEnvelope(payload)) return;
|
|
2804
|
+
const envelope = payload;
|
|
2805
|
+
const { acp, acpContext } = envelope;
|
|
2806
|
+
if (acpContext.streamId !== this.#streamId) return;
|
|
2807
|
+
if (acp.id !== void 0 && !acp.method) {
|
|
2808
|
+
const requestId = String(acp.id);
|
|
2809
|
+
const pending = this.#pendingRequests.get(requestId);
|
|
2810
|
+
if (pending) {
|
|
2811
|
+
clearTimeout(pending.timeout);
|
|
2812
|
+
this.#pendingRequests.delete(requestId);
|
|
2813
|
+
if (acp.error) {
|
|
2814
|
+
pending.reject(ACPError.fromResponse(acp.error));
|
|
2815
|
+
} else {
|
|
2816
|
+
pending.resolve(acp.result);
|
|
2817
|
+
}
|
|
2818
|
+
}
|
|
2819
|
+
return;
|
|
2820
|
+
}
|
|
2821
|
+
if (acp.method && acp.id === void 0) {
|
|
2822
|
+
await this.#handleNotification(acp.method, acp.params, acpContext);
|
|
2823
|
+
return;
|
|
2824
|
+
}
|
|
2825
|
+
if (acp.method && acp.id !== void 0) {
|
|
2826
|
+
await this.#handleAgentRequest(acp.id, acp.method, acp.params, acpContext, message);
|
|
2827
|
+
}
|
|
2828
|
+
}
|
|
2829
|
+
/**
|
|
2830
|
+
* Handle an ACP notification from the agent.
|
|
2831
|
+
*/
|
|
2832
|
+
async #handleNotification(method, params, _acpContext) {
|
|
2833
|
+
if (method === ACP_METHODS.SESSION_UPDATE) {
|
|
2834
|
+
await this.#options.client.sessionUpdate(params);
|
|
2835
|
+
}
|
|
2836
|
+
}
|
|
2837
|
+
/**
|
|
2838
|
+
* Handle an agent→client request.
|
|
2839
|
+
*/
|
|
2840
|
+
async #handleAgentRequest(requestId, method, params, _ctx, originalMessage) {
|
|
2841
|
+
let result;
|
|
2842
|
+
let error;
|
|
2843
|
+
try {
|
|
2844
|
+
switch (method) {
|
|
2845
|
+
case ACP_METHODS.REQUEST_PERMISSION:
|
|
2846
|
+
result = await this.#options.client.requestPermission(
|
|
2847
|
+
params
|
|
2848
|
+
);
|
|
2849
|
+
break;
|
|
2850
|
+
case ACP_METHODS.FS_READ_TEXT_FILE:
|
|
2851
|
+
if (!this.#options.client.readTextFile) {
|
|
2852
|
+
throw new ACPError(-32601, "Method not supported: fs/read_text_file");
|
|
2853
|
+
}
|
|
2854
|
+
result = await this.#options.client.readTextFile(
|
|
2855
|
+
params
|
|
2856
|
+
);
|
|
2857
|
+
break;
|
|
2858
|
+
case ACP_METHODS.FS_WRITE_TEXT_FILE:
|
|
2859
|
+
if (!this.#options.client.writeTextFile) {
|
|
2860
|
+
throw new ACPError(-32601, "Method not supported: fs/write_text_file");
|
|
2861
|
+
}
|
|
2862
|
+
result = await this.#options.client.writeTextFile(
|
|
2863
|
+
params
|
|
2864
|
+
);
|
|
2865
|
+
break;
|
|
2866
|
+
case ACP_METHODS.TERMINAL_CREATE:
|
|
2867
|
+
if (!this.#options.client.createTerminal) {
|
|
2868
|
+
throw new ACPError(-32601, "Method not supported: terminal/create");
|
|
2869
|
+
}
|
|
2870
|
+
result = await this.#options.client.createTerminal(
|
|
2871
|
+
params
|
|
2872
|
+
);
|
|
2873
|
+
break;
|
|
2874
|
+
case ACP_METHODS.TERMINAL_OUTPUT:
|
|
2875
|
+
if (!this.#options.client.terminalOutput) {
|
|
2876
|
+
throw new ACPError(-32601, "Method not supported: terminal/output");
|
|
2877
|
+
}
|
|
2878
|
+
result = await this.#options.client.terminalOutput(
|
|
2879
|
+
params
|
|
2880
|
+
);
|
|
2881
|
+
break;
|
|
2882
|
+
case ACP_METHODS.TERMINAL_RELEASE:
|
|
2883
|
+
if (!this.#options.client.releaseTerminal) {
|
|
2884
|
+
throw new ACPError(-32601, "Method not supported: terminal/release");
|
|
2885
|
+
}
|
|
2886
|
+
result = await this.#options.client.releaseTerminal(
|
|
2887
|
+
params
|
|
2888
|
+
);
|
|
2889
|
+
break;
|
|
2890
|
+
case ACP_METHODS.TERMINAL_WAIT_FOR_EXIT:
|
|
2891
|
+
if (!this.#options.client.waitForTerminalExit) {
|
|
2892
|
+
throw new ACPError(-32601, "Method not supported: terminal/wait_for_exit");
|
|
2893
|
+
}
|
|
2894
|
+
result = await this.#options.client.waitForTerminalExit(
|
|
2895
|
+
params
|
|
2896
|
+
);
|
|
2897
|
+
break;
|
|
2898
|
+
case ACP_METHODS.TERMINAL_KILL:
|
|
2899
|
+
if (!this.#options.client.killTerminal) {
|
|
2900
|
+
throw new ACPError(-32601, "Method not supported: terminal/kill");
|
|
2901
|
+
}
|
|
2902
|
+
result = await this.#options.client.killTerminal(
|
|
2903
|
+
params
|
|
2904
|
+
);
|
|
2905
|
+
break;
|
|
2906
|
+
default:
|
|
2907
|
+
throw new ACPError(-32601, `Unknown method: ${method}`);
|
|
2908
|
+
}
|
|
2909
|
+
} catch (e) {
|
|
2910
|
+
if (e instanceof ACPError) {
|
|
2911
|
+
error = e;
|
|
2912
|
+
} else {
|
|
2913
|
+
error = new ACPError(-32603, e.message);
|
|
2914
|
+
}
|
|
2915
|
+
}
|
|
2916
|
+
const responseEnvelope = {
|
|
2917
|
+
acp: {
|
|
2918
|
+
jsonrpc: "2.0",
|
|
2919
|
+
id: requestId,
|
|
2920
|
+
...error ? { error: error.toErrorObject() } : { result }
|
|
2921
|
+
},
|
|
2922
|
+
acpContext: {
|
|
2923
|
+
streamId: this.#streamId,
|
|
2924
|
+
sessionId: this.#sessionId,
|
|
2925
|
+
direction: "client-to-agent"
|
|
2926
|
+
}
|
|
2927
|
+
};
|
|
2928
|
+
await this.#mapClient.send(
|
|
2929
|
+
{ agent: this.#options.targetAgent },
|
|
2930
|
+
responseEnvelope,
|
|
2931
|
+
{
|
|
2932
|
+
protocol: "acp",
|
|
2933
|
+
correlationId: originalMessage.id
|
|
2934
|
+
}
|
|
2935
|
+
);
|
|
2936
|
+
}
|
|
2937
|
+
/**
|
|
2938
|
+
* Send an ACP request and wait for response.
|
|
2939
|
+
*/
|
|
2940
|
+
async #sendRequest(method, params) {
|
|
2941
|
+
if (this.#closed) {
|
|
2942
|
+
throw new Error("ACP stream is closed");
|
|
2943
|
+
}
|
|
2944
|
+
await this.#setupSubscription();
|
|
2945
|
+
if (this.#closed) {
|
|
2946
|
+
throw new Error("ACP stream closed");
|
|
2947
|
+
}
|
|
2948
|
+
const correlationId = `${this.#streamId}-${Date.now()}-${Math.random().toString(36).slice(2)}`;
|
|
2949
|
+
const timeout = this.#options.timeout ?? 3e4;
|
|
2950
|
+
const envelope = {
|
|
2951
|
+
acp: {
|
|
2952
|
+
jsonrpc: "2.0",
|
|
2953
|
+
id: correlationId,
|
|
2954
|
+
method,
|
|
2955
|
+
...params !== void 0 && { params }
|
|
2956
|
+
},
|
|
2957
|
+
acpContext: {
|
|
2958
|
+
streamId: this.#streamId,
|
|
2959
|
+
sessionId: this.#sessionId,
|
|
2960
|
+
direction: "client-to-agent"
|
|
2961
|
+
}
|
|
2962
|
+
};
|
|
2963
|
+
const resultPromise = new Promise((resolve, reject) => {
|
|
2964
|
+
const timeoutHandle = setTimeout(() => {
|
|
2965
|
+
this.#pendingRequests.delete(correlationId);
|
|
2966
|
+
reject(new Error(`ACP request timed out after ${timeout}ms: ${method}`));
|
|
2967
|
+
}, timeout);
|
|
2968
|
+
this.#pendingRequests.set(correlationId, {
|
|
2969
|
+
resolve,
|
|
2970
|
+
reject,
|
|
2971
|
+
timeout: timeoutHandle,
|
|
2972
|
+
method
|
|
2973
|
+
});
|
|
2974
|
+
});
|
|
2975
|
+
try {
|
|
2976
|
+
await this.#mapClient.send({ agent: this.#options.targetAgent }, envelope, {
|
|
2977
|
+
protocol: "acp",
|
|
2978
|
+
correlationId
|
|
2979
|
+
});
|
|
2980
|
+
} catch (err) {
|
|
2981
|
+
const pending = this.#pendingRequests.get(correlationId);
|
|
2982
|
+
if (pending) {
|
|
2983
|
+
clearTimeout(pending.timeout);
|
|
2984
|
+
this.#pendingRequests.delete(correlationId);
|
|
2985
|
+
}
|
|
2986
|
+
throw err;
|
|
2987
|
+
}
|
|
2988
|
+
if (this.#closed && !this.#pendingRequests.has(correlationId)) {
|
|
2989
|
+
throw new Error("ACP stream closed");
|
|
2990
|
+
}
|
|
2991
|
+
return resultPromise;
|
|
2992
|
+
}
|
|
2993
|
+
/**
|
|
2994
|
+
* Send an ACP notification (no response expected).
|
|
2995
|
+
*/
|
|
2996
|
+
async #sendNotification(method, params) {
|
|
2997
|
+
if (this.#closed) {
|
|
2998
|
+
throw new Error("ACP stream is closed");
|
|
2999
|
+
}
|
|
3000
|
+
await this.#setupSubscription();
|
|
3001
|
+
const envelope = {
|
|
3002
|
+
acp: {
|
|
3003
|
+
jsonrpc: "2.0",
|
|
3004
|
+
method,
|
|
3005
|
+
...params !== void 0 && { params }
|
|
3006
|
+
},
|
|
3007
|
+
acpContext: {
|
|
3008
|
+
streamId: this.#streamId,
|
|
3009
|
+
sessionId: this.#sessionId,
|
|
3010
|
+
direction: "client-to-agent"
|
|
3011
|
+
}
|
|
3012
|
+
};
|
|
3013
|
+
await this.#mapClient.send({ agent: this.#options.targetAgent }, envelope, {
|
|
3014
|
+
protocol: "acp"
|
|
3015
|
+
});
|
|
3016
|
+
}
|
|
3017
|
+
// ===========================================================================
|
|
3018
|
+
// ACP Lifecycle Methods
|
|
3019
|
+
// ===========================================================================
|
|
3020
|
+
/**
|
|
3021
|
+
* Initialize the ACP connection with the target agent.
|
|
3022
|
+
*/
|
|
3023
|
+
async initialize(params) {
|
|
3024
|
+
if (this.#initialized) {
|
|
3025
|
+
throw new Error("ACP stream already initialized");
|
|
3026
|
+
}
|
|
3027
|
+
const result = await this.#sendRequest(
|
|
3028
|
+
ACP_METHODS.INITIALIZE,
|
|
3029
|
+
params
|
|
3030
|
+
);
|
|
3031
|
+
this.#initialized = true;
|
|
3032
|
+
this.#capabilities = result.agentCapabilities ?? null;
|
|
3033
|
+
return result;
|
|
3034
|
+
}
|
|
3035
|
+
/**
|
|
3036
|
+
* Authenticate with the agent.
|
|
3037
|
+
*/
|
|
3038
|
+
async authenticate(params) {
|
|
3039
|
+
if (!this.#initialized) {
|
|
3040
|
+
throw new Error("Must call initialize() before authenticate()");
|
|
3041
|
+
}
|
|
3042
|
+
return this.#sendRequest(
|
|
3043
|
+
ACP_METHODS.AUTHENTICATE,
|
|
3044
|
+
params
|
|
3045
|
+
);
|
|
3046
|
+
}
|
|
3047
|
+
// ===========================================================================
|
|
3048
|
+
// ACP Session Methods
|
|
3049
|
+
// ===========================================================================
|
|
3050
|
+
/**
|
|
3051
|
+
* Create a new ACP session.
|
|
3052
|
+
*/
|
|
3053
|
+
async newSession(params) {
|
|
3054
|
+
if (!this.#initialized) {
|
|
3055
|
+
throw new Error("Must call initialize() before newSession()");
|
|
3056
|
+
}
|
|
3057
|
+
const result = await this.#sendRequest(
|
|
3058
|
+
ACP_METHODS.SESSION_NEW,
|
|
3059
|
+
params
|
|
3060
|
+
);
|
|
3061
|
+
this.#sessionId = result.sessionId;
|
|
3062
|
+
return result;
|
|
3063
|
+
}
|
|
3064
|
+
/**
|
|
3065
|
+
* Load an existing ACP session.
|
|
3066
|
+
*/
|
|
3067
|
+
async loadSession(params) {
|
|
3068
|
+
if (!this.#initialized) {
|
|
3069
|
+
throw new Error("Must call initialize() before loadSession()");
|
|
3070
|
+
}
|
|
3071
|
+
const result = await this.#sendRequest(
|
|
3072
|
+
ACP_METHODS.SESSION_LOAD,
|
|
3073
|
+
params
|
|
3074
|
+
);
|
|
3075
|
+
this.#sessionId = params.sessionId;
|
|
3076
|
+
return result;
|
|
3077
|
+
}
|
|
3078
|
+
/**
|
|
3079
|
+
* Set the session mode.
|
|
3080
|
+
*/
|
|
3081
|
+
async setSessionMode(params) {
|
|
3082
|
+
return this.#sendRequest(
|
|
3083
|
+
ACP_METHODS.SESSION_SET_MODE,
|
|
3084
|
+
params
|
|
3085
|
+
);
|
|
3086
|
+
}
|
|
3087
|
+
// ===========================================================================
|
|
3088
|
+
// ACP Prompt Methods
|
|
3089
|
+
// ===========================================================================
|
|
3090
|
+
/**
|
|
3091
|
+
* Send a prompt to the agent.
|
|
3092
|
+
* Updates are received via the sessionUpdate handler.
|
|
3093
|
+
*/
|
|
3094
|
+
async prompt(params) {
|
|
3095
|
+
if (!this.#sessionId) {
|
|
3096
|
+
throw new Error("Must call newSession() or loadSession() before prompt()");
|
|
3097
|
+
}
|
|
3098
|
+
return this.#sendRequest(
|
|
3099
|
+
ACP_METHODS.SESSION_PROMPT,
|
|
3100
|
+
params
|
|
3101
|
+
);
|
|
3102
|
+
}
|
|
3103
|
+
/**
|
|
3104
|
+
* Cancel ongoing operations for the current session.
|
|
3105
|
+
*/
|
|
3106
|
+
async cancel(params) {
|
|
3107
|
+
if (!this.#sessionId) {
|
|
3108
|
+
throw new Error("No active session to cancel");
|
|
3109
|
+
}
|
|
3110
|
+
await this.#sendNotification(ACP_METHODS.SESSION_CANCEL, {
|
|
3111
|
+
sessionId: this.#sessionId,
|
|
3112
|
+
...params
|
|
3113
|
+
});
|
|
3114
|
+
}
|
|
3115
|
+
// ===========================================================================
|
|
3116
|
+
// Lifecycle
|
|
3117
|
+
// ===========================================================================
|
|
3118
|
+
/**
|
|
3119
|
+
* Close this ACP stream and clean up resources.
|
|
3120
|
+
*/
|
|
3121
|
+
async close() {
|
|
3122
|
+
if (this.#closed) return;
|
|
3123
|
+
this.#closed = true;
|
|
3124
|
+
if (this.#unsubscribeReconnection) {
|
|
3125
|
+
this.#unsubscribeReconnection();
|
|
3126
|
+
this.#unsubscribeReconnection = null;
|
|
3127
|
+
}
|
|
3128
|
+
for (const [id, pending] of this.#pendingRequests) {
|
|
3129
|
+
clearTimeout(pending.timeout);
|
|
3130
|
+
pending.reject(new Error("ACP stream closed"));
|
|
3131
|
+
this.#pendingRequests.delete(id);
|
|
3132
|
+
}
|
|
3133
|
+
if (this.#subscription) {
|
|
3134
|
+
await this.#subscription.unsubscribe();
|
|
3135
|
+
this.#subscription = null;
|
|
3136
|
+
}
|
|
3137
|
+
this.emit("close");
|
|
3138
|
+
}
|
|
3139
|
+
};
|
|
3140
|
+
|
|
2555
3141
|
// src/connection/client.ts
|
|
2556
3142
|
var ClientConnection = class _ClientConnection {
|
|
2557
3143
|
#connection;
|
|
2558
3144
|
#subscriptions = /* @__PURE__ */ new Map();
|
|
2559
3145
|
#subscriptionStates = /* @__PURE__ */ new Map();
|
|
2560
3146
|
#reconnectionHandlers = /* @__PURE__ */ new Set();
|
|
3147
|
+
#acpStreams = /* @__PURE__ */ new Map();
|
|
2561
3148
|
#options;
|
|
2562
3149
|
#sessionId = null;
|
|
2563
3150
|
#serverCapabilities = null;
|
|
@@ -2668,6 +3255,10 @@ var ClientConnection = class _ClientConnection {
|
|
|
2668
3255
|
);
|
|
2669
3256
|
resumeToken = result.resumeToken;
|
|
2670
3257
|
} finally {
|
|
3258
|
+
for (const stream of this.#acpStreams.values()) {
|
|
3259
|
+
await stream.close();
|
|
3260
|
+
}
|
|
3261
|
+
this.#acpStreams.clear();
|
|
2671
3262
|
for (const subscription of this.#subscriptions.values()) {
|
|
2672
3263
|
subscription._close();
|
|
2673
3264
|
}
|
|
@@ -2849,6 +3440,65 @@ var ClientConnection = class _ClientConnection {
|
|
|
2849
3440
|
}
|
|
2850
3441
|
}
|
|
2851
3442
|
// ===========================================================================
|
|
3443
|
+
// ACP Streams
|
|
3444
|
+
// ===========================================================================
|
|
3445
|
+
/**
|
|
3446
|
+
* Create a virtual ACP stream connection to an agent.
|
|
3447
|
+
*
|
|
3448
|
+
* This allows clients to interact with ACP-compatible agents using the
|
|
3449
|
+
* familiar ACP interface while routing all messages through MAP.
|
|
3450
|
+
*
|
|
3451
|
+
* @param options - Stream configuration options
|
|
3452
|
+
* @returns ACPStreamConnection instance ready for initialize()
|
|
3453
|
+
*
|
|
3454
|
+
* @example
|
|
3455
|
+
* ```typescript
|
|
3456
|
+
* const acp = client.createACPStream({
|
|
3457
|
+
* targetAgent: 'coding-agent-1',
|
|
3458
|
+
* client: {
|
|
3459
|
+
* requestPermission: async (req) => ({
|
|
3460
|
+
* outcome: { outcome: 'selected', optionId: 'allow' }
|
|
3461
|
+
* }),
|
|
3462
|
+
* sessionUpdate: async (update) => {
|
|
3463
|
+
* console.log('Agent update:', update);
|
|
3464
|
+
* }
|
|
3465
|
+
* }
|
|
3466
|
+
* });
|
|
3467
|
+
*
|
|
3468
|
+
* await acp.initialize({
|
|
3469
|
+
* protocolVersion: 20241007,
|
|
3470
|
+
* clientInfo: { name: 'IDE', version: '1.0' }
|
|
3471
|
+
* });
|
|
3472
|
+
* const { sessionId } = await acp.newSession({ cwd: '/project', mcpServers: [] });
|
|
3473
|
+
* const result = await acp.prompt({
|
|
3474
|
+
* sessionId,
|
|
3475
|
+
* prompt: [{ type: 'text', text: 'Hello' }]
|
|
3476
|
+
* });
|
|
3477
|
+
*
|
|
3478
|
+
* await acp.close();
|
|
3479
|
+
* ```
|
|
3480
|
+
*/
|
|
3481
|
+
createACPStream(options) {
|
|
3482
|
+
const stream = new ACPStreamConnection(this, options);
|
|
3483
|
+
this.#acpStreams.set(stream.streamId, stream);
|
|
3484
|
+
stream.on("close", () => {
|
|
3485
|
+
this.#acpStreams.delete(stream.streamId);
|
|
3486
|
+
});
|
|
3487
|
+
return stream;
|
|
3488
|
+
}
|
|
3489
|
+
/**
|
|
3490
|
+
* Get an active ACP stream by ID.
|
|
3491
|
+
*/
|
|
3492
|
+
getACPStream(streamId) {
|
|
3493
|
+
return this.#acpStreams.get(streamId);
|
|
3494
|
+
}
|
|
3495
|
+
/**
|
|
3496
|
+
* Get all active ACP streams.
|
|
3497
|
+
*/
|
|
3498
|
+
get acpStreams() {
|
|
3499
|
+
return this.#acpStreams;
|
|
3500
|
+
}
|
|
3501
|
+
// ===========================================================================
|
|
2852
3502
|
// Subscriptions
|
|
2853
3503
|
// ===========================================================================
|
|
2854
3504
|
/**
|