| 1 | using System.Text.Json; |
| 2 | |
| 3 | namespace AgentClientProtocol; |
| 4 | |
| 5 | public sealed class AgentSideConnection : IDisposable |
| 6 | { |
| 7 | readonly CancellationTokenSource cts = new(); |
| 8 | readonly JsonRpcEndpoint endpoint; |
| 9 | |
| 10 | public AgentSideConnection(IAcpAgent agent, TextReader reader, TextWriter writer) |
| 11 | { |
| 12 | endpoint = new( |
| 13 | _ => new(reader.ReadLine()), |
| 14 | (s, _) => |
| 15 | { |
| 16 | writer.WriteLine(s); |
| 17 | return default; |
| 18 | }, |
| 19 | (s, _) => default |
| 20 | ); |
| 21 | |
| 22 | endpoint.SetRequestHandler(AgentMethods.Initialize, async (request, ct) => |
| 23 | { |
| 24 | AcpException.ThrowIfParamIsNull(request.Params); |
| 25 | |
| 26 | var response = await agent.InitializeAsync(JsonSerializer.Deserialize( |
| 27 | request.Params!.Value, |
| 28 | AcpJsonSerializerContext.Default.Options.GetTypeInfo<InitializeRequest>())!, ct); |
| 29 | |
| 30 | return new JsonRpcResponse |
| 31 | { |
| 32 | Id = request.Id, |
| 33 | Result = JsonSerializer.SerializeToElement(response, AcpJsonSerializerContext.Default.Options.GetTypeInfo<InitializeResponse>()) |
| 34 | }; |
| 35 | }); |
| 36 | |
| 37 | endpoint.SetRequestHandler(AgentMethods.Authenticate, async (request, ct) => |
| 38 | { |
| 39 | AcpException.ThrowIfParamIsNull(request.Params); |
| 40 | |
| 41 | var response = await agent.AuthenticateAsync(JsonSerializer.Deserialize( |
| 42 | request.Params!.Value, |
| 43 | AcpJsonSerializerContext.Default.Options.GetTypeInfo<AuthenticateRequest>())!, ct); |
| 44 | |
| 45 | return new JsonRpcResponse |
| 46 | { |
| 47 | Id = request.Id, |
| 48 | Result = JsonSerializer.SerializeToElement(response, AcpJsonSerializerContext.Default.Options.GetTypeInfo<AuthenticateResponse>()) |
| 49 | }; |
| 50 | }); |
| 51 | |
| 52 | endpoint.SetRequestHandler(AgentMethods.SessionNew, async (request, ct) => |
| 53 | { |
| 54 | AcpException.ThrowIfParamIsNull(request.Params); |
| 55 | |
| 56 | var response = await agent.NewSessionAsync(JsonSerializer.Deserialize( |
| 57 | request.Params!.Value, |
| 58 | AcpJsonSerializerContext.Default.Options.GetTypeInfo<NewSessionRequest>())!, ct); |
| 59 | |
| 60 | return new JsonRpcResponse |
| 61 | { |
| 62 | Id = request.Id, |
| 63 | Result = JsonSerializer.SerializeToElement(response, AcpJsonSerializerContext.Default.Options.GetTypeInfo<NewSessionResponse>()) |
| 64 | }; |
| 65 | }); |
| 66 | |
| 67 | endpoint.SetRequestHandler(AgentMethods.SessionPrompt, async (request, ct) => |
| 68 | { |
| 69 | AcpException.ThrowIfParamIsNull(request.Params); |
| 70 | |
| 71 | var response = await agent.PromptAsync(JsonSerializer.Deserialize( |
| 72 | request.Params!.Value, |
| 73 | AcpJsonSerializerContext.Default.Options.GetTypeInfo<PromptRequest>())!, ct); |
| 74 | |
| 75 | return new JsonRpcResponse |
| 76 | { |
| 77 | Id = request.Id, |
| 78 | Result = JsonSerializer.SerializeToElement(response, AcpJsonSerializerContext.Default.Options.GetTypeInfo<PromptResponse>()) |
| 79 | }; |
| 80 | }); |
| 81 | |
| 82 | endpoint.SetRequestHandler(AgentMethods.SessionLoad, async (request, ct) => |
| 83 | { |
| 84 | AcpException.ThrowIfParamIsNull(request.Params); |
| 85 | |
| 86 | var response = await agent.LoadSessionAsync(JsonSerializer.Deserialize( |
| 87 | request.Params!.Value, |
| 88 | AcpJsonSerializerContext.Default.Options.GetTypeInfo<LoadSessionRequest>())!, ct); |
| 89 | |
| 90 | return new JsonRpcResponse |
| 91 | { |
| 92 | Id = request.Id, |
| 93 | Result = JsonSerializer.SerializeToElement(response, AcpJsonSerializerContext.Default.Options.GetTypeInfo<LoadSessionResponse>()) |
| 94 | }; |
| 95 | }); |
| 96 | |
| 97 | endpoint.SetRequestHandler(AgentMethods.SessionSetMode, async (request, ct) => |
| 98 | { |
| 99 | AcpException.ThrowIfParamIsNull(request.Params); |
| 100 | |
| 101 | var response = await agent.SetSessionModeAsync(JsonSerializer.Deserialize( |
| 102 | request.Params!.Value, |
| 103 | AcpJsonSerializerContext.Default.Options.GetTypeInfo<SetSessionModeRequest>())!, ct); |
| 104 | |
| 105 | return new JsonRpcResponse |
| 106 | { |
| 107 | Id = request.Id, |
| 108 | Result = JsonSerializer.SerializeToElement(response, AcpJsonSerializerContext.Default.Options.GetTypeInfo<SetSessionModeResponse>()) |
| 109 | }; |
| 110 | }); |
| 111 | |
| 112 | endpoint.SetRequestHandler(AgentMethods.SessionSetModel, async (request, ct) => |
| 113 | { |
| 114 | AcpException.ThrowIfParamIsNull(request.Params); |
| 115 | |
| 116 | var response = await agent.SetSessionModelAsync(JsonSerializer.Deserialize( |
| 117 | request.Params!.Value, |
| 118 | AcpJsonSerializerContext.Default.Options.GetTypeInfo<SetSessionModelRequest>())!, ct); |
| 119 | |
| 120 | return new JsonRpcResponse |
| 121 | { |
| 122 | Id = request.Id, |
| 123 | Result = JsonSerializer.SerializeToElement(response, AcpJsonSerializerContext.Default.Options.GetTypeInfo<SetSessionModelResponse>()) |
| 124 | }; |
| 125 | }); |
| 126 | |
| 127 | endpoint.SetNotificationHandler(AgentMethods.SessionCancel, async (notification, ct) => |
| 128 | { |
| 129 | AcpException.ThrowIfParamIsNull(notification.Params); |
| 130 | |
| 131 | var cancelNotification = JsonSerializer.Deserialize( |
| 132 | notification.Params!.Value, |
| 133 | AcpJsonSerializerContext.Default.Options.GetTypeInfo<CancelNotification>())!; |
| 134 | |
| 135 | await agent.CancelAsync(cancelNotification, ct); |
| 136 | }); |
| 137 | |
| 138 | endpoint.SetDefaultRequestHandler(async (request, ct) => |
| 139 | { |
| 140 | var response = await agent.ExtMethodAsync(request.Method, request.Params ?? default, ct); |
| 141 | return new JsonRpcResponse |
| 142 | { |
| 143 | Id = request.Id, |
| 144 | Result = response |
| 145 | }; |
| 146 | }); |
| 147 | |
| 148 | endpoint.SetDefaultNotificationHandler(async (notification, ct) => |
| 149 | { |
| 150 | await agent.ExtNotificationAsync(notification.Method, notification.Params ?? default, ct); |
| 151 | }); |
| 152 | } |
| 153 | |
| 154 | public void Dispose() |
| 155 | { |
| 156 | cts.Cancel(); |
| 157 | cts.Dispose(); |
| 158 | } |
| 159 | |
| 160 | public void Open() |
| 161 | { |
| 162 | Task.Run(async () => await endpoint.ReadMessagesAsync(cts.Token)); |
| 163 | } |
| 164 | } |