Skip to main content

Mountain/Track/Effect/CreateEffectForRequest/
Debug.rs

1use std::sync::Arc;
2
3use CommonLibrary::{Debug::DebugService::DebugService, Environment::Requires::Requires};
4use serde_json::{Value, json};
5use tauri::{Emitter, Runtime};
6use url::Url;
7
8use crate::Track::Effect::{
9	CreateEffectForRequest::Utilities::Params::{i64_at_or, str_at, string_at, string_at_or},
10	MappedEffectType::MappedEffect,
11};
12
13pub fn CreateEffect<R:Runtime>(MethodName:&str, Parameters:Value) -> Option<Result<MappedEffect, String>> {
14	match MethodName {
15		// Cocoon's `Debug/Namespace.ts:63` sends `debug.dap-response` as a
16		// fire-and-forget notification carrying a DAP response message
17		// emitted by an inline-implementation adapter (one that runs
18		// inside the extension host, not as a spawned process). Forward
19		// to the renderer via `sky://debug/dap-message` so the workbench's
20		// RawDebugSession sequencer can correlate it against the pending
21		// request by `request_seq`. Payload: `{ sessionId, message }`.
22		"debug.dap-response" => {
23			crate::effect!(run_time, {
24				let session_id = Parameters.get("sessionId").and_then(Value::as_str).unwrap_or("").to_string();
25
26				if session_id.is_empty() {
27					return Err("debug.dap-response: missing 'sessionId' field".to_string());
28				}
29
30				let message = Parameters.get("message").cloned().unwrap_or(Value::Null);
31
32				let _ = run_time.Environment.ApplicationHandle.emit(
33					"sky://debug/dap-message",
34					json!({
35						"sessionId": session_id,
36						"sidecarId": "cocoon-main",
37						"message": message,
38					}),
39				);
40
41				Ok(json!(null))
42			})
43		},
44
45		"Debug.Start" => {
46			crate::effect!(run_time, {
47				let provider:Arc<dyn DebugService> = run_time.Environment.Require();
48
49				let folder_uri_str = str_at(&Parameters, 0);
50
51				let folder_uri = if folder_uri_str.is_empty() { None } else { Url::parse(folder_uri_str).ok() };
52
53				let configuration = Parameters.get(1).cloned().unwrap_or_else(|| json!({ "type": "node" }));
54
55				provider
56					.StartDebugging(folder_uri, configuration)
57					.await
58					.map(|session_id| json!(session_id))
59					.map_err(|e| e.to_string())
60			})
61		},
62
63		"Debug.RegisterConfigurationProvider" => {
64			crate::effect!(run_time, {
65				let provider:Arc<dyn DebugService> = run_time.Environment.Require();
66
67				let debug_type = string_at_or(&Parameters, 0, "node");
68
69				let provider_handle = i64_at_or(&Parameters, 1, 1) as u32;
70
71				let sidecar_id = string_at_or(&Parameters, 2, "cocoon-main");
72
73				provider
74					.RegisterDebugConfigurationProvider(debug_type, provider_handle, sidecar_id)
75					.await
76					.map(|_| json!(null))
77					.map_err(|e| e.to_string())
78			})
79		},
80
81		"Debug.Stop" => {
82			crate::effect!(run_time, {
83				let provider:Arc<dyn DebugService> = run_time.Environment.Require();
84
85				let SessionId = string_at(&Parameters, 0);
86
87				provider
88					.StopDebugging(SessionId)
89					.await
90					.map(|_| json!(null))
91					.map_err(|e| e.to_string())
92			})
93		},
94
95		_ => None,
96	}
97}