copilot-sdk-supercharged 2.5.2 → 3.0.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 +4 -4
- data/README.md +217 -2
- data/lib/copilot/client.rb +400 -9
- data/lib/copilot/define_tool.rb +5 -2
- data/lib/copilot/json_rpc_client.rb +39 -2
- data/lib/copilot/sdk_protocol_version.rb +1 -1
- data/lib/copilot/session.rb +213 -5
- data/lib/copilot/session_params.rb +261 -0
- data/lib/copilot/types.rb +430 -6
- data/lib/copilot/version.rb +1 -1
- data/lib/copilot.rb +1 -0
- metadata +3 -2
data/lib/copilot/client.rb
CHANGED
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
# Copyright (c) Microsoft Corporation. All rights reserved.
|
|
4
4
|
|
|
5
5
|
require "open3"
|
|
6
|
+
require "securerandom"
|
|
6
7
|
require "socket"
|
|
7
8
|
require "thread"
|
|
8
9
|
require "fileutils"
|
|
@@ -43,6 +44,14 @@ module Copilot
|
|
|
43
44
|
# @param env [Hash, nil] environment variables for the CLI process
|
|
44
45
|
# @param github_token [String, nil] GitHub token for authentication
|
|
45
46
|
# @param use_logged_in_user [Boolean, nil] use logged-in user auth (default: true unless github_token)
|
|
47
|
+
# @param client_info [ClientInfo, Hash, nil] identity of the integrating application
|
|
48
|
+
# (application_name, application_version, integration_name, integration_version),
|
|
49
|
+
# declared on the +connect+ handshake when any field is set
|
|
50
|
+
# @param mode [String] client mode: "coding-agent" (default) or "empty"
|
|
51
|
+
# (see {ClientMode}); "copilot-cli" is accepted as an alias of "coding-agent"
|
|
52
|
+
# @param network [NetworkConfig, Hash, nil] host network callbacks
|
|
53
|
+
# (scope, resolve_proxy, authenticate_negotiate, authenticate_basic), registered
|
|
54
|
+
# with +network.setProvider+ after connecting
|
|
46
55
|
#
|
|
47
56
|
# @raise [ArgumentError] if mutually exclusive options are provided
|
|
48
57
|
def initialize(
|
|
@@ -58,7 +67,10 @@ module Copilot
|
|
|
58
67
|
env: nil,
|
|
59
68
|
github_token: nil,
|
|
60
69
|
use_logged_in_user: nil,
|
|
61
|
-
on_get_trace_context: nil
|
|
70
|
+
on_get_trace_context: nil,
|
|
71
|
+
client_info: nil,
|
|
72
|
+
mode: ClientMode::CODING_AGENT,
|
|
73
|
+
network: nil
|
|
62
74
|
)
|
|
63
75
|
# Validate mutually exclusive options
|
|
64
76
|
if cli_url && (use_stdio == false || cli_path)
|
|
@@ -85,6 +97,19 @@ module Copilot
|
|
|
85
97
|
# Default use_logged_in_user based on github_token
|
|
86
98
|
use_logged_in_user = github_token ? false : true if use_logged_in_user.nil?
|
|
87
99
|
|
|
100
|
+
client_info = ClientInfo.new(**client_info.transform_keys(&:to_sym)) if client_info.is_a?(Hash)
|
|
101
|
+
|
|
102
|
+
mode = (mode || ClientMode::CODING_AGENT).to_s
|
|
103
|
+
mode = ClientMode::CODING_AGENT if mode == ClientMode::COPILOT_CLI
|
|
104
|
+
unless [ClientMode::CODING_AGENT, ClientMode::EMPTY].include?(mode)
|
|
105
|
+
raise ArgumentError, "mode must be \"#{ClientMode::CODING_AGENT}\" or \"#{ClientMode::EMPTY}\""
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
network = NetworkConfig.new(**network.transform_keys(&:to_sym)) if network.is_a?(Hash)
|
|
109
|
+
if network && !network.scope.nil? && ![NetworkScope::CONNECTION, NetworkScope::RUNTIME].include?(network.scope.to_s)
|
|
110
|
+
raise ArgumentError, "network scope must be \"#{NetworkScope::CONNECTION}\" or \"#{NetworkScope::RUNTIME}\""
|
|
111
|
+
end
|
|
112
|
+
|
|
88
113
|
@options = ClientOptions.new(
|
|
89
114
|
cli_path: cli_path || "copilot",
|
|
90
115
|
cli_args: cli_args,
|
|
@@ -99,6 +124,9 @@ module Copilot
|
|
|
99
124
|
github_token: github_token,
|
|
100
125
|
use_logged_in_user: use_logged_in_user,
|
|
101
126
|
on_get_trace_context: on_get_trace_context,
|
|
127
|
+
client_info: client_info,
|
|
128
|
+
mode: mode,
|
|
129
|
+
network: network,
|
|
102
130
|
)
|
|
103
131
|
|
|
104
132
|
@process = nil
|
|
@@ -119,6 +147,10 @@ module Copilot
|
|
|
119
147
|
@typed_lifecycle_handlers = {} # type => [handler]
|
|
120
148
|
@lifecycle_handlers_lock = Mutex.new
|
|
121
149
|
|
|
150
|
+
# registration id => { provider:, session_id: } for session github_token_provider callbacks
|
|
151
|
+
@github_token_providers = {}
|
|
152
|
+
@github_token_providers_lock = Mutex.new
|
|
153
|
+
|
|
122
154
|
@stderr_thread = nil
|
|
123
155
|
end
|
|
124
156
|
|
|
@@ -136,7 +168,9 @@ module Copilot
|
|
|
136
168
|
begin
|
|
137
169
|
start_cli_server unless @is_external_server
|
|
138
170
|
connect_to_server
|
|
171
|
+
send_client_info
|
|
139
172
|
verify_protocol_version
|
|
173
|
+
register_network_provider
|
|
140
174
|
@state = ConnectionState::CONNECTED
|
|
141
175
|
rescue StandardError
|
|
142
176
|
@state = ConnectionState::ERROR
|
|
@@ -174,6 +208,9 @@ module Copilot
|
|
|
174
208
|
# Clear models cache
|
|
175
209
|
@models_cache_lock.synchronize { @models_cache = nil }
|
|
176
210
|
|
|
211
|
+
# Drop all GitHub token provider registrations
|
|
212
|
+
@github_token_providers_lock.synchronize { @github_token_providers.clear }
|
|
213
|
+
|
|
177
214
|
# Close socket if TCP
|
|
178
215
|
if @socket
|
|
179
216
|
begin
|
|
@@ -232,6 +269,7 @@ module Copilot
|
|
|
232
269
|
end
|
|
233
270
|
|
|
234
271
|
@models_cache_lock.synchronize { @models_cache = nil }
|
|
272
|
+
@github_token_providers_lock.synchronize { @github_token_providers.clear }
|
|
235
273
|
|
|
236
274
|
if @socket
|
|
237
275
|
begin
|
|
@@ -273,9 +311,20 @@ module Copilot
|
|
|
273
311
|
# @raise [RuntimeError] if not connected and auto_start is disabled
|
|
274
312
|
def create_session(**config)
|
|
275
313
|
ensure_connected!
|
|
314
|
+
validate_session_config_for_mode!(config)
|
|
276
315
|
|
|
316
|
+
registration_id = register_github_token_provider(config, config[:session_id])
|
|
277
317
|
payload = build_create_session_payload(config)
|
|
278
|
-
|
|
318
|
+
payload[:gitHubTokenProviderRegistrationId] = registration_id if registration_id
|
|
319
|
+
payload = Copilot.group_session_params(payload, resume: false)
|
|
320
|
+
Copilot.apply_empty_mode_session_defaults(payload) if @options.mode == ClientMode::EMPTY
|
|
321
|
+
|
|
322
|
+
begin
|
|
323
|
+
response = @rpc_client.request("session.create", payload)
|
|
324
|
+
rescue StandardError
|
|
325
|
+
unregister_github_token_provider(registration_id)
|
|
326
|
+
raise
|
|
327
|
+
end
|
|
279
328
|
|
|
280
329
|
session_id = response["sessionId"]
|
|
281
330
|
workspace_path = response["workspacePath"]
|
|
@@ -286,9 +335,11 @@ module Copilot
|
|
|
286
335
|
session._register_user_input_handler(config[:on_user_input_request]) if config[:on_user_input_request]
|
|
287
336
|
session._register_hooks(config[:hooks]) if config[:hooks]
|
|
288
337
|
session._register_exit_plan_mode_handler(config[:on_exit_plan_mode]) if config[:on_exit_plan_mode]
|
|
338
|
+
register_session_v3_handlers(session, config)
|
|
289
339
|
if @options.on_get_trace_context
|
|
290
340
|
session._register_trace_context_provider(@options.on_get_trace_context)
|
|
291
341
|
end
|
|
342
|
+
attach_github_token_provider(session, registration_id)
|
|
292
343
|
|
|
293
344
|
@sessions_lock.synchronize { @sessions[session_id] = session }
|
|
294
345
|
session
|
|
@@ -301,9 +352,20 @@ module Copilot
|
|
|
301
352
|
# @return [CopilotSession]
|
|
302
353
|
def resume_session(session_id, **config)
|
|
303
354
|
ensure_connected!
|
|
355
|
+
validate_session_config_for_mode!(config)
|
|
304
356
|
|
|
357
|
+
registration_id = register_github_token_provider(config, session_id)
|
|
305
358
|
payload = build_resume_session_payload(session_id, config)
|
|
306
|
-
|
|
359
|
+
payload[:gitHubTokenProviderRegistrationId] = registration_id if registration_id
|
|
360
|
+
payload = Copilot.group_session_params(payload, resume: true)
|
|
361
|
+
Copilot.apply_empty_mode_session_defaults(payload) if @options.mode == ClientMode::EMPTY
|
|
362
|
+
|
|
363
|
+
begin
|
|
364
|
+
response = @rpc_client.request("session.resume", payload)
|
|
365
|
+
rescue StandardError
|
|
366
|
+
unregister_github_token_provider(registration_id)
|
|
367
|
+
raise
|
|
368
|
+
end
|
|
307
369
|
|
|
308
370
|
resumed_id = response["sessionId"]
|
|
309
371
|
workspace_path = response["workspacePath"]
|
|
@@ -314,9 +376,11 @@ module Copilot
|
|
|
314
376
|
session._register_user_input_handler(config[:on_user_input_request]) if config[:on_user_input_request]
|
|
315
377
|
session._register_hooks(config[:hooks]) if config[:hooks]
|
|
316
378
|
session._register_exit_plan_mode_handler(config[:on_exit_plan_mode]) if config[:on_exit_plan_mode]
|
|
379
|
+
register_session_v3_handlers(session, config)
|
|
317
380
|
if @options.on_get_trace_context
|
|
318
381
|
session._register_trace_context_provider(@options.on_get_trace_context)
|
|
319
382
|
end
|
|
383
|
+
attach_github_token_provider(session, registration_id)
|
|
320
384
|
|
|
321
385
|
@sessions_lock.synchronize { @sessions[resumed_id] = session }
|
|
322
386
|
session
|
|
@@ -460,6 +524,16 @@ module Copilot
|
|
|
460
524
|
@rpc_client.request("sessionFs.setProvider", params)
|
|
461
525
|
end
|
|
462
526
|
|
|
527
|
+
# Clear the runtime's cached managed (enterprise policy) settings so they are
|
|
528
|
+
# re-read on next use.
|
|
529
|
+
#
|
|
530
|
+
# @return [Hash] the raw result
|
|
531
|
+
def clear_managed_settings_cache
|
|
532
|
+
raise_not_connected! unless @rpc_client
|
|
533
|
+
|
|
534
|
+
@rpc_client.request("managedSettings.clearCache", {})
|
|
535
|
+
end
|
|
536
|
+
|
|
463
537
|
# Subscribe to session lifecycle events.
|
|
464
538
|
#
|
|
465
539
|
# @overload on(&handler)
|
|
@@ -562,7 +636,7 @@ module Copilot
|
|
|
562
636
|
spawn_env = @options.env ? @options.env.dup : ENV.to_h
|
|
563
637
|
spawn_env["COPILOT_SDK_AUTH_TOKEN"] = @options.github_token if @options.github_token
|
|
564
638
|
|
|
565
|
-
cmd = [cli_path] + args
|
|
639
|
+
cmd = cli_path.end_with?(".js") ? ["node", cli_path] + args : [cli_path] + args
|
|
566
640
|
|
|
567
641
|
if @options.use_stdio
|
|
568
642
|
@stdin, @stdout, @stderr, wait_thr = Open3.popen3(spawn_env, *cmd, chdir: @options.cwd)
|
|
@@ -653,6 +727,25 @@ module Copilot
|
|
|
653
727
|
handle_exit_plan_mode_request(params)
|
|
654
728
|
end
|
|
655
729
|
|
|
730
|
+
@rpc_client.on_request("gitHubToken.getToken") do |params|
|
|
731
|
+
handle_github_token_request(params)
|
|
732
|
+
end
|
|
733
|
+
|
|
734
|
+
# Shell authentication prompts run off the reader thread so that a
|
|
735
|
+
# shellAuthentication.cancel can be processed while the handler prompts.
|
|
736
|
+
@rpc_client.on_request("shellAuthentication.request", async: true) do |params|
|
|
737
|
+
handle_shell_authentication_request(params)
|
|
738
|
+
end
|
|
739
|
+
|
|
740
|
+
@rpc_client.on_request("shellAuthentication.cancel") do |params|
|
|
741
|
+
handle_shell_authentication_cancel(params)
|
|
742
|
+
end
|
|
743
|
+
|
|
744
|
+
@rpc_client.on_request("memoryStore.store", async: true) do |params|
|
|
745
|
+
handle_memory_store_request(params)
|
|
746
|
+
end
|
|
747
|
+
|
|
748
|
+
register_network_handlers
|
|
656
749
|
register_session_fs_handlers
|
|
657
750
|
end
|
|
658
751
|
|
|
@@ -677,24 +770,73 @@ module Copilot
|
|
|
677
770
|
@rpc_client.on_request("sessionFs.rename") { |params| handle_session_fs_rename(params) }
|
|
678
771
|
end
|
|
679
772
|
|
|
773
|
+
# Declare the integrating application's identity (+client_info+) on the
|
|
774
|
+
# +connect+ handshake, before the protocol-version ping. Skipped when no field
|
|
775
|
+
# is set; older CLIs that do not implement +connect+ are tolerated.
|
|
776
|
+
def send_client_info
|
|
777
|
+
wire = @options.client_info&.to_wire
|
|
778
|
+
return unless wire
|
|
779
|
+
|
|
780
|
+
@rpc_client.request("connect", { clientInfo: wire })
|
|
781
|
+
rescue JsonRpcError => e
|
|
782
|
+
raise unless e.code == -32601 || e.message.include?("Unhandled method connect")
|
|
783
|
+
end
|
|
784
|
+
|
|
680
785
|
def verify_protocol_version
|
|
681
786
|
expected = Copilot.sdk_protocol_version
|
|
682
787
|
result = ping
|
|
683
788
|
server_version = result.protocol_version
|
|
684
789
|
|
|
685
790
|
if server_version.nil?
|
|
686
|
-
raise "SDK protocol version mismatch: SDK
|
|
791
|
+
raise "SDK protocol version mismatch: SDK supports version #{expected}, " \
|
|
687
792
|
"but server does not report a protocol version. " \
|
|
688
793
|
"Please update your server to ensure compatibility."
|
|
689
794
|
end
|
|
690
795
|
|
|
691
796
|
if server_version != expected
|
|
692
|
-
raise "SDK protocol version mismatch: SDK
|
|
797
|
+
raise "SDK protocol version mismatch: SDK supports version #{expected}, " \
|
|
693
798
|
"but server reports version #{server_version}. " \
|
|
694
799
|
"Please update your SDK or server to ensure compatibility."
|
|
695
800
|
end
|
|
696
801
|
end
|
|
697
802
|
|
|
803
|
+
# Registers the host network callbacks with +network.setProvider+. An empty object
|
|
804
|
+
# marks each callback the client answers; the scope is sent only when set.
|
|
805
|
+
def register_network_provider
|
|
806
|
+
network = @options.network
|
|
807
|
+
return unless network&.any_callback?
|
|
808
|
+
|
|
809
|
+
params = {}
|
|
810
|
+
params[:scope] = network.scope.to_s if network.scope
|
|
811
|
+
params[:resolveProxy] = {} if network.resolve_proxy
|
|
812
|
+
params[:authenticateNegotiate] = {} if network.authenticate_negotiate
|
|
813
|
+
params[:authenticateBasic] = {} if network.authenticate_basic
|
|
814
|
+
@rpc_client.request("network.setProvider", params)
|
|
815
|
+
end
|
|
816
|
+
|
|
817
|
+
# Registers the server -> client network callback handlers for the configured
|
|
818
|
+
# callbacks. They run off the reader thread so slow lookups never stall it.
|
|
819
|
+
def register_network_handlers
|
|
820
|
+
network = @options.network
|
|
821
|
+
return unless network
|
|
822
|
+
|
|
823
|
+
if network.resolve_proxy
|
|
824
|
+
@rpc_client.on_request("network.resolveProxy", async: true) do |params|
|
|
825
|
+
handle_network_resolve_proxy(params)
|
|
826
|
+
end
|
|
827
|
+
end
|
|
828
|
+
if network.authenticate_negotiate
|
|
829
|
+
@rpc_client.on_request("network.authenticateNegotiate", async: true) do |params|
|
|
830
|
+
handle_network_authenticate_negotiate(params)
|
|
831
|
+
end
|
|
832
|
+
end
|
|
833
|
+
if network.authenticate_basic
|
|
834
|
+
@rpc_client.on_request("network.authenticateBasic", async: true) do |params|
|
|
835
|
+
handle_network_authenticate_basic(params)
|
|
836
|
+
end
|
|
837
|
+
end
|
|
838
|
+
end
|
|
839
|
+
|
|
698
840
|
# ---- Notification handlers ----
|
|
699
841
|
|
|
700
842
|
def handle_session_event_notification(params)
|
|
@@ -826,6 +968,189 @@ module Copilot
|
|
|
826
968
|
end
|
|
827
969
|
end
|
|
828
970
|
|
|
971
|
+
# ---- GitHub token provider (gitHubToken.getToken) ----
|
|
972
|
+
|
|
973
|
+
# Validates a session's +github_token_provider+ and stores it in the client-level
|
|
974
|
+
# registry. Returns the opaque registration id sent on the wire, or nil.
|
|
975
|
+
def register_github_token_provider(config, session_id = nil)
|
|
976
|
+
provider = config[:github_token_provider]
|
|
977
|
+
return nil unless provider
|
|
978
|
+
|
|
979
|
+
if config[:github_token] || config[:git_hub_token] || config[:auth_token]
|
|
980
|
+
raise ArgumentError, "github_token and github_token_provider are mutually exclusive"
|
|
981
|
+
end
|
|
982
|
+
raise ArgumentError, "github_token_provider must respond to #call" unless provider.respond_to?(:call)
|
|
983
|
+
|
|
984
|
+
registration_id = SecureRandom.uuid
|
|
985
|
+
@github_token_providers_lock.synchronize do
|
|
986
|
+
@github_token_providers[registration_id] = { provider: provider, session_id: session_id }
|
|
987
|
+
end
|
|
988
|
+
registration_id
|
|
989
|
+
end
|
|
990
|
+
|
|
991
|
+
def unregister_github_token_provider(registration_id)
|
|
992
|
+
return unless registration_id
|
|
993
|
+
|
|
994
|
+
@github_token_providers_lock.synchronize { @github_token_providers.delete(registration_id) }
|
|
995
|
+
end
|
|
996
|
+
|
|
997
|
+
# Binds a registration to its session and releases it when the session is destroyed.
|
|
998
|
+
def attach_github_token_provider(session, registration_id)
|
|
999
|
+
return unless registration_id
|
|
1000
|
+
|
|
1001
|
+
@github_token_providers_lock.synchronize do
|
|
1002
|
+
entry = @github_token_providers[registration_id]
|
|
1003
|
+
entry[:session_id] = session.session_id if entry
|
|
1004
|
+
end
|
|
1005
|
+
session._on_destroy { unregister_github_token_provider(registration_id) }
|
|
1006
|
+
end
|
|
1007
|
+
|
|
1008
|
+
def handle_github_token_request(params)
|
|
1009
|
+
registration_id = params["registrationId"]
|
|
1010
|
+
registration = @github_token_providers_lock.synchronize { @github_token_providers[registration_id] }
|
|
1011
|
+
unless registration
|
|
1012
|
+
raise "No GitHub token provider registered for registration ID \"#{registration_id}\""
|
|
1013
|
+
end
|
|
1014
|
+
|
|
1015
|
+
args = GitHubTokenProviderArgs.new(
|
|
1016
|
+
host: params["host"],
|
|
1017
|
+
session_id: params["sessionId"] || registration[:session_id],
|
|
1018
|
+
reason: params["reason"]
|
|
1019
|
+
)
|
|
1020
|
+
normalize_github_token_result(registration[:provider].call(args))
|
|
1021
|
+
end
|
|
1022
|
+
|
|
1023
|
+
# Accepts a GitHubTokenResult or a Hash with camelCase or snake_case keys.
|
|
1024
|
+
def normalize_github_token_result(result)
|
|
1025
|
+
return result.to_wire if result.respond_to?(:to_wire)
|
|
1026
|
+
raise "GitHub token provider must return a Hash or GitHubTokenResult" unless result.is_a?(Hash)
|
|
1027
|
+
|
|
1028
|
+
h = result.transform_keys(&:to_s)
|
|
1029
|
+
return { kind: "cancelled" } if h["kind"].to_s == "cancelled"
|
|
1030
|
+
|
|
1031
|
+
GitHubTokenResult.new(
|
|
1032
|
+
kind: "token",
|
|
1033
|
+
access_token: h["accessToken"] || h["access_token"],
|
|
1034
|
+
token_type: h["tokenType"] || h["token_type"],
|
|
1035
|
+
expires_in: h["expiresIn"] || h["expires_in"]
|
|
1036
|
+
).to_wire
|
|
1037
|
+
end
|
|
1038
|
+
|
|
1039
|
+
# ---- v3.0.0 session callbacks and client mode ----
|
|
1040
|
+
|
|
1041
|
+
# Registers the session's shell authentication and memory store callbacks.
|
|
1042
|
+
def register_session_v3_handlers(session, config)
|
|
1043
|
+
if config[:on_shell_authentication_request]
|
|
1044
|
+
session._register_shell_authentication_handler(config[:on_shell_authentication_request])
|
|
1045
|
+
end
|
|
1046
|
+
session._register_memory_store(config[:store_memory]) if config[:store_memory]
|
|
1047
|
+
end
|
|
1048
|
+
|
|
1049
|
+
# In "empty" mode every session must explicitly opt into its tools.
|
|
1050
|
+
def validate_session_config_for_mode!(config)
|
|
1051
|
+
if config[:on_shell_authentication_request] && !config[:on_shell_authentication_request].respond_to?(:call)
|
|
1052
|
+
raise ArgumentError, "on_shell_authentication_request must respond to #call"
|
|
1053
|
+
end
|
|
1054
|
+
if config[:store_memory] && !config[:store_memory].respond_to?(:call)
|
|
1055
|
+
raise ArgumentError, "store_memory must respond to #call"
|
|
1056
|
+
end
|
|
1057
|
+
return unless @options.mode == ClientMode::EMPTY && config[:available_tools].nil?
|
|
1058
|
+
|
|
1059
|
+
raise ArgumentError,
|
|
1060
|
+
"CopilotClient is in mode \"empty\" but the session config did not specify " \
|
|
1061
|
+
"available_tools. Empty mode requires every session to explicitly opt into " \
|
|
1062
|
+
"the tools it wants."
|
|
1063
|
+
end
|
|
1064
|
+
|
|
1065
|
+
SHELL_AUTHENTICATION_INPUT_KINDS = [
|
|
1066
|
+
ShellAuthenticationInputKind::SECRET,
|
|
1067
|
+
ShellAuthenticationInputKind::TEXT,
|
|
1068
|
+
ShellAuthenticationInputKind::CONFIRM,
|
|
1069
|
+
].freeze
|
|
1070
|
+
private_constant :SHELL_AUTHENTICATION_INPUT_KINDS
|
|
1071
|
+
|
|
1072
|
+
# Answers +shellAuthentication.request+ with +{ action: "accept", value: }+ or
|
|
1073
|
+
# +{ action: "cancel" }+. Malformed requests and unknown sessions cancel.
|
|
1074
|
+
def handle_shell_authentication_request(params)
|
|
1075
|
+
unless params.is_a?(Hash) && params["sessionId"].is_a?(String) && params["requestId"].is_a?(String) &&
|
|
1076
|
+
params["program"].is_a?(String) && params["prompt"].is_a?(String) &&
|
|
1077
|
+
SHELL_AUTHENTICATION_INPUT_KINDS.include?(params["inputKind"])
|
|
1078
|
+
return { action: "cancel" }
|
|
1079
|
+
end
|
|
1080
|
+
|
|
1081
|
+
session = @sessions_lock.synchronize { @sessions[params["sessionId"]] }
|
|
1082
|
+
return { action: "cancel" } unless session
|
|
1083
|
+
|
|
1084
|
+
session._handle_shell_authentication_request(ShellAuthenticationRequest.from_hash(params))
|
|
1085
|
+
end
|
|
1086
|
+
|
|
1087
|
+
# Handles +shellAuthentication.cancel+ by cancelling the pending prompt, if any.
|
|
1088
|
+
def handle_shell_authentication_cancel(params)
|
|
1089
|
+
if params.is_a?(Hash) && params["sessionId"].is_a?(String) && params["requestId"].is_a?(String)
|
|
1090
|
+
session = @sessions_lock.synchronize { @sessions[params["sessionId"]] }
|
|
1091
|
+
session&._cancel_shell_authentication_request(params["requestId"])
|
|
1092
|
+
end
|
|
1093
|
+
{}
|
|
1094
|
+
end
|
|
1095
|
+
|
|
1096
|
+
# Handles +memoryStore.store+ by invoking the session's +store_memory+ callback.
|
|
1097
|
+
# Answers JSON null on success.
|
|
1098
|
+
def handle_memory_store_request(params)
|
|
1099
|
+
session_id = params.is_a?(Hash) ? params["sessionId"] : nil
|
|
1100
|
+
session = @sessions_lock.synchronize { @sessions[session_id] }
|
|
1101
|
+
raise "Memory store owner is unavailable" unless session
|
|
1102
|
+
|
|
1103
|
+
session._handle_memory_store(StoredMemory.from_hash(params))
|
|
1104
|
+
nil
|
|
1105
|
+
end
|
|
1106
|
+
|
|
1107
|
+
# Answers +network.resolveProxy+ with +{ proxy: }+, or +{}+ to keep the runtime's
|
|
1108
|
+
# own proxy resolution.
|
|
1109
|
+
def handle_network_resolve_proxy(params)
|
|
1110
|
+
request = NetworkResolveProxyRequest.new(url: params["url"])
|
|
1111
|
+
proxy = invoke_network_callback("Proxy resolution") { @options.network.resolve_proxy.call(request) }
|
|
1112
|
+
proxy.nil? || proxy.to_s.empty? ? {} : { proxy: proxy.to_s }
|
|
1113
|
+
end
|
|
1114
|
+
|
|
1115
|
+
# Answers +network.authenticateNegotiate+ with +{ token: }+ or +{}+. The token is never logged.
|
|
1116
|
+
def handle_network_authenticate_negotiate(params)
|
|
1117
|
+
request = NetworkAuthenticateNegotiateRequest.new(spn: params["spn"])
|
|
1118
|
+
token = invoke_network_callback("Negotiate authentication") do
|
|
1119
|
+
@options.network.authenticate_negotiate.call(request)
|
|
1120
|
+
end
|
|
1121
|
+
token.nil? || token.to_s.empty? ? {} : { token: token.to_s }
|
|
1122
|
+
end
|
|
1123
|
+
|
|
1124
|
+
# Answers +network.authenticateBasic+ with +{ username:, password: }+ or +{}+. Only the
|
|
1125
|
+
# credential fields are sent, and they are never logged.
|
|
1126
|
+
def handle_network_authenticate_basic(params)
|
|
1127
|
+
request = NetworkAuthenticateBasicRequest.new(
|
|
1128
|
+
proxy: params["proxy"],
|
|
1129
|
+
realm: params["realm"],
|
|
1130
|
+
previous_rejected: params["previousRejected"] ? true : false
|
|
1131
|
+
)
|
|
1132
|
+
credentials = invoke_network_callback("Basic authentication") do
|
|
1133
|
+
@options.network.authenticate_basic.call(request)
|
|
1134
|
+
end
|
|
1135
|
+
return {} unless credentials
|
|
1136
|
+
|
|
1137
|
+
creds = credentials.respond_to?(:to_h) ? credentials.to_h : {}
|
|
1138
|
+
username = creds[:username] || creds["username"]
|
|
1139
|
+
password = creds[:password] || creds["password"]
|
|
1140
|
+
result = {}
|
|
1141
|
+
result[:username] = username.to_s unless username.nil?
|
|
1142
|
+
result[:password] = password.to_s unless password.nil?
|
|
1143
|
+
result
|
|
1144
|
+
end
|
|
1145
|
+
|
|
1146
|
+
# Runs a network callback. Failures are reported by type only so that secrets in an
|
|
1147
|
+
# error message are never logged or sent back over the wire.
|
|
1148
|
+
def invoke_network_callback(label)
|
|
1149
|
+
yield
|
|
1150
|
+
rescue StandardError => e
|
|
1151
|
+
raise "#{label} callback failed (#{e.class})"
|
|
1152
|
+
end
|
|
1153
|
+
|
|
829
1154
|
# ---- SessionFs provider request handlers ----
|
|
830
1155
|
#
|
|
831
1156
|
# These service the CLI's +sessionFs.*+ callbacks against the real
|
|
@@ -957,7 +1282,7 @@ module Copilot
|
|
|
957
1282
|
|
|
958
1283
|
# ---- Payload builders ----
|
|
959
1284
|
|
|
960
|
-
def build_create_session_payload(config)
|
|
1285
|
+
def build_create_session_payload(config, resume: false)
|
|
961
1286
|
payload = {}
|
|
962
1287
|
|
|
963
1288
|
payload[:model] = config[:model] if config[:model]
|
|
@@ -978,6 +1303,11 @@ module Copilot
|
|
|
978
1303
|
payload[:requestExitPlanMode] = true if config[:on_exit_plan_mode]
|
|
979
1304
|
payload[:workingDirectory] = config[:working_directory] if config[:working_directory]
|
|
980
1305
|
payload[:streaming] = config[:streaming] unless config[:streaming].nil?
|
|
1306
|
+
unless config[:include_sub_agent_streaming_events].nil?
|
|
1307
|
+
payload[:includeSubAgentStreamingEvents] = config[:include_sub_agent_streaming_events]
|
|
1308
|
+
end
|
|
1309
|
+
payload[:enableConfigDiscovery] = config[:enable_config_discovery] unless config[:enable_config_discovery].nil?
|
|
1310
|
+
payload[:instructionDirectories] = config[:instruction_directories] if config[:instruction_directories]
|
|
981
1311
|
|
|
982
1312
|
if config[:provider]
|
|
983
1313
|
payload[:provider] = config[:provider].respond_to?(:to_wire) ? config[:provider].to_wire : config[:provider]
|
|
@@ -1014,7 +1344,7 @@ module Copilot
|
|
|
1014
1344
|
payload[:memory] = mem.respond_to?(:to_wire) ? mem.to_wire : mem
|
|
1015
1345
|
end
|
|
1016
1346
|
payload[:otlpProtocol] = config[:otlp_protocol] if config[:otlp_protocol]
|
|
1017
|
-
|
|
1347
|
+
# enable_web_socket_responses is sent inside the +capi+ object (see below).
|
|
1018
1348
|
payload[:expAssignments] = config[:exp_assignments] if config[:exp_assignments]
|
|
1019
1349
|
# --- Second upstream-sync batch (2026-08 parity with @github/copilot-sdk) ---
|
|
1020
1350
|
payload[:rewindEnabled] = config[:rewind_enabled] unless config[:rewind_enabled].nil? # enable session rewind
|
|
@@ -1033,18 +1363,79 @@ module Copilot
|
|
|
1033
1363
|
# MCP OAuth host token handler: signal to the runtime that a handler is registered.
|
|
1034
1364
|
payload[:mcpAuthHandler] = true if config[:on_mcp_auth_request]
|
|
1035
1365
|
|
|
1366
|
+
# --- v2.6.0 upstream-sync session options (parity with @github/copilot-sdk) ---
|
|
1367
|
+
# Explicit +capi:+ ({ enabled: }) is merged with the Auto tier / WebSocket options.
|
|
1368
|
+
capi = {}
|
|
1369
|
+
if config[:capi]
|
|
1370
|
+
user_capi = config[:capi].respond_to?(:to_wire) ? config[:capi].to_wire : config[:capi]
|
|
1371
|
+
capi.merge!(user_capi.transform_keys(&:to_sym)) if user_capi.is_a?(Hash)
|
|
1372
|
+
end
|
|
1373
|
+
capi[:autoTier] = config[:auto_tier] if config[:auto_tier]
|
|
1374
|
+
capi[:enableWebSocketResponses] = config[:enable_web_socket_responses] unless config[:enable_web_socket_responses].nil?
|
|
1375
|
+
payload[:capi] = capi unless capi.empty?
|
|
1376
|
+
payload[:askUserVariant] = config[:ask_user_variant] if config[:ask_user_variant]
|
|
1377
|
+
if config[:sandbox_config]
|
|
1378
|
+
sc = config[:sandbox_config]
|
|
1379
|
+
payload[:sandboxConfig] = sc.respond_to?(:to_wire) ? sc.to_wire : sc
|
|
1380
|
+
end
|
|
1381
|
+
payload[:featureFlags] = config[:feature_flags] unless config[:feature_flags].nil?
|
|
1382
|
+
payload[:allowedModels] = config[:allowed_models] if config[:allowed_models]
|
|
1383
|
+
payload[:authClientIdMetadataUrl] = config[:auth_client_id_metadata_url] if config[:auth_client_id_metadata_url]
|
|
1384
|
+
if config[:model_capabilities]
|
|
1385
|
+
mc = config[:model_capabilities]
|
|
1386
|
+
payload[:modelCapabilities] = mc.respond_to?(:to_wire) ? mc.to_wire : mc
|
|
1387
|
+
end
|
|
1388
|
+
payload[:agentDirectories] = config[:agent_directories] if config[:agent_directories]
|
|
1389
|
+
payload[:hookDirectories] = config[:hook_directories] if config[:hook_directories]
|
|
1390
|
+
payload[:interactiveUser] = config[:interactive_user] unless config[:interactive_user].nil?
|
|
1391
|
+
payload[:excludedManagedSettings] = config[:excluded_managed_settings] if config[:excluded_managed_settings]
|
|
1392
|
+
payload[:enforceManagedModelDefaults] = config[:enforce_managed_model_defaults] unless config[:enforce_managed_model_defaults].nil?
|
|
1393
|
+
if config[:image_generation]
|
|
1394
|
+
ig = config[:image_generation]
|
|
1395
|
+
payload[:imageGeneration] = ig.respond_to?(:to_wire) ? ig.to_wire : ig
|
|
1396
|
+
end
|
|
1397
|
+
if config[:autopilot]
|
|
1398
|
+
ap = config[:autopilot]
|
|
1399
|
+
payload[:autopilot] = ap.respond_to?(:to_wire) ? ap.to_wire : ap
|
|
1400
|
+
end
|
|
1401
|
+
|
|
1402
|
+
# --- v3.0.0 (SDK protocol 4) session options ---
|
|
1403
|
+
if config[:providers]
|
|
1404
|
+
payload[:providers] = config[:providers].map { |p| p.respond_to?(:to_wire) ? p.to_wire : p }
|
|
1405
|
+
end
|
|
1406
|
+
if config[:models]
|
|
1407
|
+
payload[:models] = config[:models].map { |m| m.respond_to?(:to_wire) ? m.to_wire : m }
|
|
1408
|
+
end
|
|
1409
|
+
# On resume an explicit default_provider: nil clears the session's default provider.
|
|
1410
|
+
if config[:default_provider] || (resume && config.key?(:default_provider))
|
|
1411
|
+
payload[:defaultProvider] = config[:default_provider]
|
|
1412
|
+
end
|
|
1413
|
+
payload[:requestShellAuthentication] = true if config[:on_shell_authentication_request]
|
|
1414
|
+
payload[:hasMemoryStore] = true if config[:store_memory]
|
|
1415
|
+
payload[:permissionMode] = config[:permission_mode] if config[:permission_mode]
|
|
1416
|
+
|
|
1417
|
+
unless resume
|
|
1418
|
+
# Create-only options.
|
|
1419
|
+
payload[:name] = config[:name] if config[:name]
|
|
1420
|
+
payload[:trustWorkingDirectory] = config[:trust_working_directory] if config[:trust_working_directory]
|
|
1421
|
+
payload[:refreshCustomInstructions] = config[:refresh_custom_instructions] unless config[:refresh_custom_instructions].nil?
|
|
1422
|
+
end
|
|
1423
|
+
|
|
1036
1424
|
payload
|
|
1037
1425
|
end
|
|
1038
1426
|
|
|
1039
1427
|
def build_resume_session_payload(session_id, config)
|
|
1040
|
-
payload = build_create_session_payload(config)
|
|
1428
|
+
payload = build_create_session_payload(config, resume: true)
|
|
1041
1429
|
payload[:sessionId] = session_id
|
|
1042
1430
|
payload[:disableResume] = config[:disable_resume] if config[:disable_resume]
|
|
1431
|
+
payload[:allowTranscriptRecovery] = config[:allow_transcript_recovery] unless config[:allow_transcript_recovery].nil?
|
|
1043
1432
|
payload
|
|
1044
1433
|
end
|
|
1045
1434
|
|
|
1046
1435
|
def convert_system_message(msg)
|
|
1047
1436
|
return msg if msg.is_a?(Hash)
|
|
1437
|
+
# A plain String is appended to the default system message.
|
|
1438
|
+
return { mode: "append", content: msg } if msg.is_a?(String)
|
|
1048
1439
|
|
|
1049
1440
|
msg.respond_to?(:to_h) ? msg.to_h : msg
|
|
1050
1441
|
end
|
data/lib/copilot/define_tool.rb
CHANGED
|
@@ -25,19 +25,22 @@ module Copilot
|
|
|
25
25
|
# @param name [String] tool name
|
|
26
26
|
# @param description [String] human-readable description
|
|
27
27
|
# @param parameters [Hash, nil] JSON Schema for the tool parameters
|
|
28
|
+
# @param idempotent [Boolean, nil] true when repeating a call with the same arguments
|
|
29
|
+
# has no additional effect (sent as +idempotent+ in the tool definition)
|
|
28
30
|
# @yield [args, invocation] called when the tool is invoked
|
|
29
31
|
# @yieldparam args [Hash] parsed arguments from the LLM
|
|
30
32
|
# @yieldparam invocation [ToolInvocation] context about the invocation
|
|
31
33
|
# @yieldreturn [String, Hash, ToolResult] the tool result
|
|
32
34
|
# @return [Tool]
|
|
33
|
-
def self.define_tool(name:, description: nil, parameters: nil, &handler)
|
|
35
|
+
def self.define_tool(name:, description: nil, parameters: nil, idempotent: nil, &handler)
|
|
34
36
|
raise ArgumentError, "Block required for tool handler" unless handler
|
|
35
37
|
|
|
36
38
|
Tool.new(
|
|
37
39
|
name: name,
|
|
38
40
|
description: description,
|
|
39
41
|
parameters: parameters,
|
|
40
|
-
handler: handler
|
|
42
|
+
handler: handler,
|
|
43
|
+
idempotent: idempotent
|
|
41
44
|
)
|
|
42
45
|
end
|
|
43
46
|
|