acp_ruby 0.1.0
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.
- checksums.yaml +7 -0
- data/CHANGELOG.md +19 -0
- data/LICENSE.txt +21 -0
- data/README.md +125 -0
- data/lib/acp_ruby.rb +3 -0
- data/lib/agent_client_protocol/agent/connection.rb +125 -0
- data/lib/agent_client_protocol/agent/router.rb +62 -0
- data/lib/agent_client_protocol/agent.rb +49 -0
- data/lib/agent_client_protocol/client/connection.rb +112 -0
- data/lib/agent_client_protocol/client/router.rb +85 -0
- data/lib/agent_client_protocol/client.rb +53 -0
- data/lib/agent_client_protocol/connection.rb +144 -0
- data/lib/agent_client_protocol/contrib/permission_broker.rb +57 -0
- data/lib/agent_client_protocol/contrib/session_accumulator.rb +160 -0
- data/lib/agent_client_protocol/contrib/tool_call_tracker.rb +115 -0
- data/lib/agent_client_protocol/error.rb +41 -0
- data/lib/agent_client_protocol/helpers.rb +176 -0
- data/lib/agent_client_protocol/meta.rb +30 -0
- data/lib/agent_client_protocol/router.rb +108 -0
- data/lib/agent_client_protocol/schema/base_model.rb +158 -0
- data/lib/agent_client_protocol/schema/generated.rb +508 -0
- data/lib/agent_client_protocol/schema/types.rb +200 -0
- data/lib/agent_client_protocol/stdio.rb +129 -0
- data/lib/agent_client_protocol/transport.rb +70 -0
- data/lib/agent_client_protocol/version.rb +5 -0
- data/lib/agent_client_protocol.rb +54 -0
- data/schema/VERSION +1 -0
- data/schema/meta.json +24 -0
- data/schema/schema.json +3430 -0
- metadata +103 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 1ffa79a20b8968e789ed0de24e3fa89bb528d7518a29e5609bff2569f0d41f5a
|
|
4
|
+
data.tar.gz: 52be0fd47071c8caac178a7340603853c0d7073263f37cd983b0fa823416efc1
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 82f446bb9e4c1338e249b7c0b6a3972d13aeb1395b34792285aa6d7a6f681a825d3e2c741c286b51438df7ea7b2b0149b4a6c85fd27a5ff26adb62b4095c29c6
|
|
7
|
+
data.tar.gz: '0012968ff5ec5aa7878509dacea620de3c1c7f040e8d37398e08f3e9f9fb6777b09cef298372341b089466c9858d677db3182bdf3d0a582490978c7097dd1f4d'
|
data/CHANGELOG.md
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
All notable changes to this project will be documented in this file.
|
|
4
|
+
|
|
5
|
+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
|
|
6
|
+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
7
|
+
|
|
8
|
+
## [Unreleased]
|
|
9
|
+
|
|
10
|
+
### Added
|
|
11
|
+
|
|
12
|
+
- Initial release: full ACP (Agent Client Protocol) SDK for Ruby
|
|
13
|
+
- Code-generated schema types from upstream `schema.json` (v0.10.8)
|
|
14
|
+
- Bidirectional JSON-RPC 2.0 transport over stdio (NDJSON framing)
|
|
15
|
+
- Agent and client connection classes with symmetric calling pattern
|
|
16
|
+
- Process management: `run_agent`, `spawn_agent_process`, `spawn_client_process`
|
|
17
|
+
- Helper functions for building content blocks, tool calls, session updates
|
|
18
|
+
- Contrib helpers: `SessionAccumulator`, `ToolCallTracker`, `PermissionBroker`
|
|
19
|
+
- Examples: echo agent, interactive client, duet (both sides in one process)
|
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 David Copeland
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
|
13
|
+
all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
# AgentClientProtocol
|
|
2
|
+
|
|
3
|
+
[](https://badge.fury.io/rb/acp_ruby)
|
|
4
|
+
|
|
5
|
+
Ruby SDK for the [Agent Client Protocol (ACP)](https://agentclientprotocol.com/) — a JSON-RPC 2.0 based open protocol standardizing communication between code editors and AI coding agents.
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
```sh
|
|
10
|
+
bundle add acp_ruby
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
Or add to your Gemfile:
|
|
14
|
+
|
|
15
|
+
```ruby
|
|
16
|
+
gem "acp_ruby"
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
## Usage
|
|
20
|
+
|
|
21
|
+
### Building an Agent
|
|
22
|
+
|
|
23
|
+
```ruby
|
|
24
|
+
require "agent_client_protocol"
|
|
25
|
+
|
|
26
|
+
class MyAgent
|
|
27
|
+
include AgentClientProtocol::AgentInterface
|
|
28
|
+
include AgentClientProtocol::Helpers
|
|
29
|
+
|
|
30
|
+
def on_connect(conn)
|
|
31
|
+
@conn = conn
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def initialize_agent(protocol_version:, **)
|
|
35
|
+
AgentClientProtocol::Schema::InitializeResponse.new(
|
|
36
|
+
protocol_version: 1,
|
|
37
|
+
agent_info: { name: "my-agent", version: "1.0.0" },
|
|
38
|
+
capabilities: {}
|
|
39
|
+
)
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
def new_session(cwd:, **)
|
|
43
|
+
AgentClientProtocol::Schema::NewSessionResponse.new(session_id: SecureRandom.uuid)
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
def prompt(prompt:, session_id:, **)
|
|
47
|
+
@conn.session_update(
|
|
48
|
+
session_id: session_id,
|
|
49
|
+
update: update_agent_message_text("You said: #{prompt}")
|
|
50
|
+
)
|
|
51
|
+
AgentClientProtocol::Schema::PromptResponse.new(
|
|
52
|
+
stop_reason: "end_turn"
|
|
53
|
+
)
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
AgentClientProtocol.run_agent(MyAgent.new)
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
### Building a Client
|
|
61
|
+
|
|
62
|
+
```ruby
|
|
63
|
+
require "agent_client_protocol"
|
|
64
|
+
|
|
65
|
+
class MyClient
|
|
66
|
+
include AgentClientProtocol::ClientInterface
|
|
67
|
+
|
|
68
|
+
def on_connect(conn)
|
|
69
|
+
@conn = conn
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
def session_update(session_id:, update:, **)
|
|
73
|
+
puts "Update: #{update}"
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
def request_permission(session_id:, tool_call:, options:, **)
|
|
77
|
+
AgentClientProtocol::Schema::RequestPermissionResponse.new(
|
|
78
|
+
allowed: true
|
|
79
|
+
)
|
|
80
|
+
end
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
client = MyClient.new
|
|
84
|
+
AgentClientProtocol.spawn_agent_process(client, "claude-code", "--acp") do |conn, pid|
|
|
85
|
+
resp = conn.initialize_agent(protocol_version: 1, client_info: { name: "my-client", version: "1.0.0" })
|
|
86
|
+
session = conn.new_session(cwd: Dir.pwd)
|
|
87
|
+
result = conn.prompt(session_id: session.session_id, prompt: "Hello!")
|
|
88
|
+
puts "Done: #{result.stop_reason}"
|
|
89
|
+
end
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
### Spawning Both Sides in One Process
|
|
93
|
+
|
|
94
|
+
```ruby
|
|
95
|
+
require "agent_client_protocol"
|
|
96
|
+
|
|
97
|
+
# See examples/duet.rb for a full working example
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
## Error Handling
|
|
101
|
+
|
|
102
|
+
```ruby
|
|
103
|
+
begin
|
|
104
|
+
conn.prompt(session_id: sid, prompt: "hello")
|
|
105
|
+
rescue AgentClientProtocol::RequestError => e
|
|
106
|
+
puts "Error #{e.code}: #{e.message}"
|
|
107
|
+
puts "Data: #{e.data}" if e.data
|
|
108
|
+
end
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
## Development
|
|
112
|
+
|
|
113
|
+
```sh
|
|
114
|
+
bundle install
|
|
115
|
+
bundle exec rake test
|
|
116
|
+
bundle exec ruby script/generate_schema.rb # regenerate schema types
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
## Contributing
|
|
120
|
+
|
|
121
|
+
Bug reports and pull requests are welcome on [GitHub](https://github.com/davidcopeland/agent_client_protocol).
|
|
122
|
+
|
|
123
|
+
## License
|
|
124
|
+
|
|
125
|
+
The gem is available as open source under the terms of the [MIT License](LICENSE.txt).
|
data/lib/acp_ruby.rb
ADDED
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "router"
|
|
4
|
+
|
|
5
|
+
module AgentClientProtocol
|
|
6
|
+
module Agent
|
|
7
|
+
class Connection
|
|
8
|
+
attr_reader :conn
|
|
9
|
+
|
|
10
|
+
def initialize(handler, reader, writer)
|
|
11
|
+
@handler = handler
|
|
12
|
+
router = Agent.build_router(handler)
|
|
13
|
+
@conn = AgentClientProtocol::Connection.new(
|
|
14
|
+
reader: reader,
|
|
15
|
+
writer: writer,
|
|
16
|
+
handler: router.method(:call)
|
|
17
|
+
)
|
|
18
|
+
handler.on_connect(self)
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
# --- Client-calling methods (agent sends to client) ---
|
|
22
|
+
|
|
23
|
+
def session_update(session_id:, update:)
|
|
24
|
+
params = Schema::SessionNotification.new(
|
|
25
|
+
session_id: session_id,
|
|
26
|
+
update: update
|
|
27
|
+
)
|
|
28
|
+
@conn.send_notification("session/update", params.to_h)
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def request_permission(session_id:, tool_call:, options:)
|
|
32
|
+
params = Schema::RequestPermissionRequest.new(
|
|
33
|
+
session_id: session_id,
|
|
34
|
+
tool_call: tool_call.is_a?(Schema::BaseModel) ? tool_call : Schema::ToolCallUpdate.from_hash(tool_call),
|
|
35
|
+
options: options
|
|
36
|
+
)
|
|
37
|
+
result = @conn.send_request("session/request_permission", params.to_h)
|
|
38
|
+
Schema::RequestPermissionResponse.from_hash(result)
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def read_text_file(session_id:, path:, line: nil, limit: nil)
|
|
42
|
+
params = Schema::ReadTextFileRequest.new(
|
|
43
|
+
session_id: session_id,
|
|
44
|
+
path: path,
|
|
45
|
+
line: line,
|
|
46
|
+
limit: limit
|
|
47
|
+
)
|
|
48
|
+
result = @conn.send_request("fs/read_text_file", params.to_h)
|
|
49
|
+
Schema::ReadTextFileResponse.from_hash(result)
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def write_text_file(session_id:, path:, content:)
|
|
53
|
+
params = Schema::WriteTextFileRequest.new(
|
|
54
|
+
session_id: session_id,
|
|
55
|
+
path: path,
|
|
56
|
+
content: content
|
|
57
|
+
)
|
|
58
|
+
result = @conn.send_request("fs/write_text_file", params.to_h)
|
|
59
|
+
Schema::WriteTextFileResponse.from_hash(result)
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
def create_terminal(session_id:, command:, args: nil, cwd: nil, env: nil)
|
|
63
|
+
params = Schema::CreateTerminalRequest.new(
|
|
64
|
+
session_id: session_id,
|
|
65
|
+
command: command,
|
|
66
|
+
args: args,
|
|
67
|
+
cwd: cwd,
|
|
68
|
+
env: env
|
|
69
|
+
)
|
|
70
|
+
result = @conn.send_request("terminal/create", params.to_h)
|
|
71
|
+
Schema::CreateTerminalResponse.from_hash(result)
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
def terminal_output(session_id:, terminal_id:)
|
|
75
|
+
params = Schema::TerminalOutputRequest.new(
|
|
76
|
+
session_id: session_id,
|
|
77
|
+
terminal_id: terminal_id
|
|
78
|
+
)
|
|
79
|
+
result = @conn.send_request("terminal/output", params.to_h)
|
|
80
|
+
Schema::TerminalOutputResponse.from_hash(result)
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
def release_terminal(session_id:, terminal_id:)
|
|
84
|
+
params = Schema::ReleaseTerminalRequest.new(
|
|
85
|
+
session_id: session_id,
|
|
86
|
+
terminal_id: terminal_id
|
|
87
|
+
)
|
|
88
|
+
result = @conn.send_request("terminal/release", params.to_h)
|
|
89
|
+
Schema::ReleaseTerminalResponse.from_hash(result)
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
def wait_for_terminal_exit(session_id:, terminal_id:)
|
|
93
|
+
params = Schema::WaitForTerminalExitRequest.new(
|
|
94
|
+
session_id: session_id,
|
|
95
|
+
terminal_id: terminal_id
|
|
96
|
+
)
|
|
97
|
+
result = @conn.send_request("terminal/wait_for_exit", params.to_h)
|
|
98
|
+
Schema::WaitForTerminalExitResponse.from_hash(result)
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
def kill_terminal(session_id:, terminal_id:)
|
|
102
|
+
params = Schema::KillTerminalCommandRequest.new(
|
|
103
|
+
session_id: session_id,
|
|
104
|
+
terminal_id: terminal_id
|
|
105
|
+
)
|
|
106
|
+
result = @conn.send_request("terminal/kill", params.to_h)
|
|
107
|
+
Schema::KillTerminalCommandResponse.from_hash(result)
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
# --- Lifecycle ---
|
|
111
|
+
|
|
112
|
+
def listen
|
|
113
|
+
@conn.listen
|
|
114
|
+
end
|
|
115
|
+
|
|
116
|
+
def close
|
|
117
|
+
@conn.close
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
def closed?
|
|
121
|
+
@conn.closed?
|
|
122
|
+
end
|
|
123
|
+
end
|
|
124
|
+
end
|
|
125
|
+
end
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module AgentClientProtocol
|
|
4
|
+
module Agent
|
|
5
|
+
S = Schema
|
|
6
|
+
|
|
7
|
+
module_function
|
|
8
|
+
|
|
9
|
+
def build_router(handler)
|
|
10
|
+
router = AgentClientProtocol::Router.new
|
|
11
|
+
|
|
12
|
+
router.on_request("initialize", request_class: S::InitializeRequest) do |params|
|
|
13
|
+
handler.initialize_agent(
|
|
14
|
+
protocol_version: params.protocol_version,
|
|
15
|
+
client_capabilities: params.client_capabilities,
|
|
16
|
+
client_info: params.client_info
|
|
17
|
+
)
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
router.on_request("authenticate", request_class: S::AuthenticateRequest, optional: true) do |params|
|
|
21
|
+
handler.authenticate(method_id: params.method_id)
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
router.on_request("session/new", request_class: S::NewSessionRequest) do |params|
|
|
25
|
+
handler.new_session(cwd: params.cwd, mcp_servers: params.mcp_servers)
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
router.on_request("session/load", request_class: S::LoadSessionRequest, optional: true) do |params|
|
|
29
|
+
handler.load_session(
|
|
30
|
+
cwd: params.cwd,
|
|
31
|
+
session_id: params.session_id,
|
|
32
|
+
mcp_servers: params.mcp_servers
|
|
33
|
+
)
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
router.on_request("session/prompt", request_class: S::PromptRequest) do |params|
|
|
37
|
+
handler.prompt(prompt: params.prompt, session_id: params.session_id)
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
router.on_notification("session/cancel", request_class: S::CancelNotification) do |params|
|
|
41
|
+
handler.cancel(session_id: params.session_id)
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
router.on_request("session/set_mode", request_class: S::SetSessionModeRequest, optional: true) do |params|
|
|
45
|
+
handler.set_session_mode(mode_id: params.mode_id, session_id: params.session_id)
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
router.on_request("session/set_config_option", request_class: S::SetSessionConfigOptionRequest, optional: true) do |params|
|
|
49
|
+
handler.set_config_option(
|
|
50
|
+
config_id: params.config_id,
|
|
51
|
+
session_id: params.session_id,
|
|
52
|
+
value: params.value
|
|
53
|
+
)
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
router.on_ext_method { |method, params| handler.ext_method(method, params) }
|
|
57
|
+
router.on_ext_notification { |method, params| handler.ext_notification(method, params) }
|
|
58
|
+
|
|
59
|
+
router
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
end
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module AgentClientProtocol
|
|
4
|
+
module AgentInterface
|
|
5
|
+
def on_connect(conn)
|
|
6
|
+
raise NotImplementedError, "#{self.class}#on_connect"
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
def initialize_agent(protocol_version:, client_capabilities: nil, client_info: nil, **)
|
|
10
|
+
raise NotImplementedError, "#{self.class}#initialize_agent"
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def new_session(cwd:, mcp_servers: nil, **)
|
|
14
|
+
raise NotImplementedError, "#{self.class}#new_session"
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def prompt(prompt:, session_id:, **)
|
|
18
|
+
raise NotImplementedError, "#{self.class}#prompt"
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def cancel(session_id:, **)
|
|
22
|
+
nil
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def authenticate(method_id:, **)
|
|
26
|
+
raise NotImplementedError, "#{self.class}#authenticate"
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def load_session(cwd:, session_id:, mcp_servers: nil, **)
|
|
30
|
+
nil
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def set_session_mode(mode_id:, session_id:, **)
|
|
34
|
+
nil
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def set_config_option(config_id:, session_id:, value:, **)
|
|
38
|
+
nil
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def ext_method(method, params)
|
|
42
|
+
raise RequestError.method_not_found(method)
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def ext_notification(method, params)
|
|
46
|
+
nil
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
end
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "router"
|
|
4
|
+
|
|
5
|
+
module AgentClientProtocol
|
|
6
|
+
module Client
|
|
7
|
+
class Connection
|
|
8
|
+
attr_reader :conn
|
|
9
|
+
|
|
10
|
+
def initialize(handler, reader, writer)
|
|
11
|
+
@handler = handler
|
|
12
|
+
router = Client.build_router(handler)
|
|
13
|
+
@conn = AgentClientProtocol::Connection.new(
|
|
14
|
+
reader: reader,
|
|
15
|
+
writer: writer,
|
|
16
|
+
handler: router.method(:call)
|
|
17
|
+
)
|
|
18
|
+
handler.on_connect(self)
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
# --- Agent-calling methods (client sends to agent) ---
|
|
22
|
+
|
|
23
|
+
def initialize_agent(protocol_version:, client_capabilities: nil, client_info: nil)
|
|
24
|
+
params = Schema::InitializeRequest.new(
|
|
25
|
+
protocol_version: protocol_version,
|
|
26
|
+
client_capabilities: client_capabilities,
|
|
27
|
+
client_info: client_info
|
|
28
|
+
)
|
|
29
|
+
result = @conn.send_request("initialize", params.to_h)
|
|
30
|
+
Schema::InitializeResponse.from_hash(result)
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def new_session(cwd:, mcp_servers: [])
|
|
34
|
+
params = Schema::NewSessionRequest.new(cwd: cwd, mcp_servers: mcp_servers)
|
|
35
|
+
result = @conn.send_request("session/new", params.to_h)
|
|
36
|
+
Schema::NewSessionResponse.from_hash(result)
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def load_session(cwd:, session_id:, mcp_servers: [])
|
|
40
|
+
params = Schema::LoadSessionRequest.new(
|
|
41
|
+
cwd: cwd,
|
|
42
|
+
session_id: session_id,
|
|
43
|
+
mcp_servers: mcp_servers
|
|
44
|
+
)
|
|
45
|
+
result = @conn.send_request("session/load", params.to_h)
|
|
46
|
+
Schema::LoadSessionResponse.from_hash(result)
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def prompt(session_id:, prompt:)
|
|
50
|
+
prompt_blocks = case prompt
|
|
51
|
+
when String
|
|
52
|
+
[Schema::TextContent.new(text: prompt).to_h]
|
|
53
|
+
when Array
|
|
54
|
+
prompt.map { |b| b.is_a?(Schema::BaseModel) ? b.to_h : b }
|
|
55
|
+
else
|
|
56
|
+
[prompt.is_a?(Schema::BaseModel) ? prompt.to_h : prompt]
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
params = Schema::PromptRequest.new(
|
|
60
|
+
session_id: session_id,
|
|
61
|
+
prompt: prompt_blocks
|
|
62
|
+
)
|
|
63
|
+
result = @conn.send_request("session/prompt", params.to_h)
|
|
64
|
+
Schema::PromptResponse.from_hash(result)
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
def cancel(session_id:)
|
|
68
|
+
params = Schema::CancelNotification.new(session_id: session_id)
|
|
69
|
+
@conn.send_notification("session/cancel", params.to_h)
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
def authenticate(method_id:)
|
|
73
|
+
params = Schema::AuthenticateRequest.new(method_id: method_id)
|
|
74
|
+
result = @conn.send_request("authenticate", params.to_h)
|
|
75
|
+
Schema::AuthenticateResponse.from_hash(result)
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
def set_session_mode(session_id:, mode_id:)
|
|
79
|
+
params = Schema::SetSessionModeRequest.new(
|
|
80
|
+
session_id: session_id,
|
|
81
|
+
mode_id: mode_id
|
|
82
|
+
)
|
|
83
|
+
result = @conn.send_request("session/set_mode", params.to_h)
|
|
84
|
+
Schema::SetSessionModeResponse.from_hash(result)
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
def set_config_option(session_id:, config_id:, value:)
|
|
88
|
+
params = Schema::SetSessionConfigOptionRequest.new(
|
|
89
|
+
session_id: session_id,
|
|
90
|
+
config_id: config_id,
|
|
91
|
+
value: value
|
|
92
|
+
)
|
|
93
|
+
result = @conn.send_request("session/set_config_option", params.to_h)
|
|
94
|
+
Schema::SetSessionConfigOptionResponse.from_hash(result)
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
# --- Lifecycle ---
|
|
98
|
+
|
|
99
|
+
def listen
|
|
100
|
+
@conn.listen
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
def close
|
|
104
|
+
@conn.close
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
def closed?
|
|
108
|
+
@conn.closed?
|
|
109
|
+
end
|
|
110
|
+
end
|
|
111
|
+
end
|
|
112
|
+
end
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module AgentClientProtocol
|
|
4
|
+
module Client
|
|
5
|
+
S = Schema
|
|
6
|
+
|
|
7
|
+
module_function
|
|
8
|
+
|
|
9
|
+
def build_router(handler)
|
|
10
|
+
router = AgentClientProtocol::Router.new
|
|
11
|
+
|
|
12
|
+
router.on_notification("session/update", request_class: S::SessionNotification) do |params|
|
|
13
|
+
handler.session_update(session_id: params.session_id, update: params.update)
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
router.on_request("session/request_permission", request_class: S::RequestPermissionRequest) do |params|
|
|
17
|
+
handler.request_permission(
|
|
18
|
+
session_id: params.session_id,
|
|
19
|
+
tool_call: params.tool_call,
|
|
20
|
+
options: params.options
|
|
21
|
+
)
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
router.on_request("fs/read_text_file", request_class: S::ReadTextFileRequest, optional: true) do |params|
|
|
25
|
+
handler.read_text_file(
|
|
26
|
+
session_id: params.session_id,
|
|
27
|
+
path: params.path,
|
|
28
|
+
line: params.line,
|
|
29
|
+
limit: params.limit
|
|
30
|
+
)
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
router.on_request("fs/write_text_file", request_class: S::WriteTextFileRequest, optional: true) do |params|
|
|
34
|
+
handler.write_text_file(
|
|
35
|
+
session_id: params.session_id,
|
|
36
|
+
path: params.path,
|
|
37
|
+
content: params.content
|
|
38
|
+
)
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
router.on_request("terminal/create", request_class: S::CreateTerminalRequest, optional: true) do |params|
|
|
42
|
+
handler.create_terminal(
|
|
43
|
+
session_id: params.session_id,
|
|
44
|
+
command: params.command,
|
|
45
|
+
args: params.args,
|
|
46
|
+
cwd: params.cwd,
|
|
47
|
+
env: params.env
|
|
48
|
+
)
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
router.on_request("terminal/output", request_class: S::TerminalOutputRequest, optional: true) do |params|
|
|
52
|
+
handler.terminal_output(
|
|
53
|
+
session_id: params.session_id,
|
|
54
|
+
terminal_id: params.terminal_id
|
|
55
|
+
)
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
router.on_request("terminal/release", request_class: S::ReleaseTerminalRequest, optional: true) do |params|
|
|
59
|
+
handler.release_terminal(
|
|
60
|
+
session_id: params.session_id,
|
|
61
|
+
terminal_id: params.terminal_id
|
|
62
|
+
)
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
router.on_request("terminal/wait_for_exit", request_class: S::WaitForTerminalExitRequest, optional: true) do |params|
|
|
66
|
+
handler.wait_for_terminal_exit(
|
|
67
|
+
session_id: params.session_id,
|
|
68
|
+
terminal_id: params.terminal_id
|
|
69
|
+
)
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
router.on_request("terminal/kill", request_class: S::KillTerminalCommandRequest, optional: true) do |params|
|
|
73
|
+
handler.kill_terminal(
|
|
74
|
+
session_id: params.session_id,
|
|
75
|
+
terminal_id: params.terminal_id
|
|
76
|
+
)
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
router.on_ext_method { |method, params| handler.ext_method(method, params) }
|
|
80
|
+
router.on_ext_notification { |method, params| handler.ext_notification(method, params) }
|
|
81
|
+
|
|
82
|
+
router
|
|
83
|
+
end
|
|
84
|
+
end
|
|
85
|
+
end
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module AgentClientProtocol
|
|
4
|
+
module ClientInterface
|
|
5
|
+
def on_connect(conn)
|
|
6
|
+
raise NotImplementedError, "#{self.class}#on_connect"
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
def session_update(session_id:, update:, **)
|
|
10
|
+
nil
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def request_permission(session_id:, tool_call:, options:, **)
|
|
14
|
+
raise NotImplementedError, "#{self.class}#request_permission"
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def read_text_file(session_id:, path:, line: nil, limit: nil, **)
|
|
18
|
+
raise RequestError.method_not_found("fs/read_text_file")
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def write_text_file(session_id:, path:, content:, **)
|
|
22
|
+
raise RequestError.method_not_found("fs/write_text_file")
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def create_terminal(session_id:, command:, args: nil, cwd: nil, env: nil, **)
|
|
26
|
+
raise RequestError.method_not_found("terminal/create")
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def terminal_output(session_id:, terminal_id:, **)
|
|
30
|
+
raise RequestError.method_not_found("terminal/output")
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def release_terminal(session_id:, terminal_id:, **)
|
|
34
|
+
raise RequestError.method_not_found("terminal/release")
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def wait_for_terminal_exit(session_id:, terminal_id:, **)
|
|
38
|
+
raise RequestError.method_not_found("terminal/wait_for_exit")
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def kill_terminal(session_id:, terminal_id:, **)
|
|
42
|
+
raise RequestError.method_not_found("terminal/kill")
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def ext_method(method, params)
|
|
46
|
+
raise RequestError.method_not_found(method)
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def ext_notification(method, params)
|
|
50
|
+
nil
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
end
|