Skip to content

Commit 8cd05d6

Browse files
authored
Merge pull request #2161 from golemcloud/ts-base-image-in-sdk
Using the base wasm from the ts sdk
2 parents ca4bdbc + 187ff2e commit 8cd05d6

File tree

90 files changed

+522
-7661
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

90 files changed

+522
-7661
lines changed

Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ members = [
2727

2828
exclude = [
2929
"cli/desktop-app/src-tauri",
30-
"cli/template-golem-agent-ts",
3130
"test-components/app-and-library",
3231
"test-components/shopping-cart",
3332
"test-components/write-stdout",

Makefile.toml

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@ run_task = [
6666
"wit-wasm-rpc",
6767
"wit-golem-common",
6868
"wit-golem-cli",
69-
"wit-template-golem-agent-ts",
7069
"wit-test-components",
7170
"wit-debugging-service-test-components",
7271
"wit-golem-templates",
@@ -134,13 +133,6 @@ cp wit/deps/golem-agent cli/golem-cli/wit/deps
134133
cp wit/deps/logging cli/golem-cli/wit/deps
135134
"""
136135

137-
[tasks.wit-template-golem-agent-ts]
138-
private = true
139-
script_runner = "@duckscript"
140-
script = """
141-
glob_cp wit/deps/**/* cli/template-golem-agent-ts/wit/deps
142-
"""
143-
144136
[tasks.wit-test-components]
145137
private = true
146138
script_runner = "@duckscript"
@@ -1087,19 +1079,6 @@ install_crate = "miniserve"
10871079
command = "miniserve"
10881080
args = ["--interfaces", "127.0.0.1", "--port", "41357", "schema.golem.cloud"]
10891081

1090-
## Agent template management
1091-
[tasks.rebuild-ts-agent-template]
1092-
description = "Rebuilds the TypeScript agent template WASM"
1093-
script_runner = "@duckscript"
1094-
script = '''
1095-
cd cli/template-golem-agent-ts
1096-
exec --fail-on-error npm update
1097-
exec --fail-on-error npm install
1098-
exec --fail-on-error npm run generate
1099-
exec --fail-on-error npm run build
1100-
cp dist/wrapper/target/wasm32-wasip1/release/golem_agent.wasm ../golem-templates/templates/ts/ts-app-common/wasm/golem_agent.wasm
1101-
'''
1102-
11031082
## Cleanup
11041083
[tasks.clear-v8]
11051084
description = "Removes the v8 crate from the target directories"

cli/golem-cli/src/app/build/command.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use crate::app::build::task_result_marker::{
1818
InjectToPrebuiltQuickJsCommandMarkerHash, ResolvedExternalCommandMarkerHash, TaskResultMarker,
1919
};
2020
use crate::app::build::{delete_path_logged, is_up_to_date, new_task_up_to_date_check};
21-
use crate::app::context::ApplicationContext;
21+
use crate::app::context::{ApplicationContext, ToolsWithEnsuredCommonDeps};
2222
use crate::app::error::CustomCommandError;
2323
use crate::fs::compile_and_collect_globs;
2424
use crate::log::{
@@ -532,7 +532,11 @@ pub async fn execute_external_command(
532532
return Err(anyhow!("Empty command!"));
533533
}
534534

535-
ensure_common_deps_for_tool(ctx, command_tokens[0].as_str()).await?;
535+
ensure_common_deps_for_tool(
536+
&ctx.tools_with_ensured_common_deps,
537+
command_tokens[0].as_str(),
538+
)
539+
.await?;
536540

537541
Command::new(command_tokens[0].clone())
538542
.args(command_tokens.iter().skip(1))
@@ -544,7 +548,10 @@ pub async fn execute_external_command(
544548
)
545549
}
546550

547-
async fn ensure_common_deps_for_tool(ctx: &ApplicationContext, tool: &str) -> anyhow::Result<()> {
551+
pub async fn ensure_common_deps_for_tool(
552+
ctx: &ToolsWithEnsuredCommonDeps,
553+
tool: &str,
554+
) -> anyhow::Result<()> {
548555
match tool {
549556
"node" | "npx" => {
550557
ctx.ensure_common_deps_for_tool_once("node", || async {

cli/golem-cli/src/app/build/gen_rpc.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ pub async fn gen_rpc(ctx: &mut ApplicationContext) -> anyhow::Result<()> {
6767
any_changed |= changed;
6868
}
6969
if any_changed {
70-
ctx.update_wit_context()?;
70+
ctx.update_wit_context().await?;
7171
}
7272
}
7373

cli/golem-cli/src/app/context.rs

Lines changed: 102 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,52 @@ pub struct ApplicationContext {
4949
component_generated_base_wit_deps: HashMap<AppComponentName, WitDepsResolver>,
5050
selected_component_names: BTreeSet<AppComponentName>,
5151
remote_components: RemoteComponents,
52+
pub tools_with_ensured_common_deps: ToolsWithEnsuredCommonDeps,
53+
}
54+
55+
pub struct ToolsWithEnsuredCommonDeps {
5256
tools_with_ensured_common_deps: tokio::sync::RwLock<HashSet<String>>,
5357
}
5458

59+
impl Default for ToolsWithEnsuredCommonDeps {
60+
fn default() -> Self {
61+
Self::new()
62+
}
63+
}
64+
65+
impl ToolsWithEnsuredCommonDeps {
66+
pub fn new() -> ToolsWithEnsuredCommonDeps {
67+
ToolsWithEnsuredCommonDeps {
68+
tools_with_ensured_common_deps: tokio::sync::RwLock::new(HashSet::new()),
69+
}
70+
}
71+
72+
pub async fn ensure_common_deps_for_tool_once(
73+
&self,
74+
tool: &str,
75+
ensure: impl AsyncFnOnce() -> anyhow::Result<()>,
76+
) -> anyhow::Result<()> {
77+
if self
78+
.tools_with_ensured_common_deps
79+
.read()
80+
.await
81+
.contains(tool)
82+
{
83+
return Ok(());
84+
}
85+
86+
let mut lock = self.tools_with_ensured_common_deps.write().await;
87+
88+
let result = ensure().await;
89+
90+
if result.is_ok() {
91+
lock.insert(tool.to_string());
92+
}
93+
94+
result
95+
}
96+
}
97+
5598
pub struct ApplicationPreloadResult {
5699
pub source_mode: ApplicationSourceMode,
57100
pub loaded_with_warnings: bool,
@@ -81,7 +124,7 @@ impl ApplicationContext {
81124
}
82125
}
83126

84-
pub fn new(
127+
pub async fn new(
85128
available_profiles: &BTreeSet<ProfileName>,
86129
source_mode: ApplicationSourceMode,
87130
config: ApplicationConfig,
@@ -93,29 +136,7 @@ impl ApplicationContext {
93136

94137
let ctx = to_anyhow(
95138
"Failed to load application manifest, see problems above",
96-
app_and_calling_working_dir.and_then(|(application, calling_working_dir)| {
97-
ResolvedWitApplication::new(&application, config.build_profile.as_ref()).map({
98-
let temp_dir = application.temp_dir();
99-
let offline = config.offline;
100-
move |wit| ApplicationContext {
101-
loaded_with_warnings: false,
102-
config,
103-
application,
104-
wit,
105-
calling_working_dir,
106-
component_stub_defs: HashMap::new(),
107-
common_wit_deps: OnceLock::new(),
108-
component_generated_base_wit_deps: HashMap::new(),
109-
selected_component_names: BTreeSet::new(),
110-
remote_components: RemoteComponents::new(
111-
file_download_client,
112-
temp_dir,
113-
offline,
114-
),
115-
tools_with_ensured_common_deps: tokio::sync::RwLock::new(HashSet::new()),
116-
}
117-
})
118-
}),
139+
Self::create_context(app_and_calling_working_dir, config, file_download_client).await,
119140
Some(|mut app_ctx| {
120141
app_ctx.loaded_with_warnings = true;
121142
app_ctx
@@ -131,6 +152,55 @@ impl ApplicationContext {
131152
Ok(Some(ctx))
132153
}
133154

155+
async fn create_context(
156+
app_and_calling_working_dir: ValidatedResult<(Application, PathBuf)>,
157+
config: ApplicationConfig,
158+
file_download_client: reqwest::Client,
159+
) -> ValidatedResult<ApplicationContext> {
160+
let mut result = ValidatedResult::Ok(());
161+
162+
let (r, v) = result.merge_and_get(app_and_calling_working_dir);
163+
result = r;
164+
if let Some((application, calling_working_dir)) = v {
165+
let tools_with_ensured_common_deps = ToolsWithEnsuredCommonDeps::new();
166+
let wit_result = ResolvedWitApplication::new(
167+
&application,
168+
config.build_profile.as_ref(),
169+
&tools_with_ensured_common_deps,
170+
)
171+
.await;
172+
173+
let (r, v) = result.merge_and_get(wit_result);
174+
result = r;
175+
if let Some(wit) = v {
176+
let temp_dir = application.temp_dir();
177+
let offline = config.offline;
178+
let ctx = ApplicationContext {
179+
loaded_with_warnings: false,
180+
config,
181+
application,
182+
wit,
183+
calling_working_dir,
184+
component_stub_defs: HashMap::new(),
185+
common_wit_deps: OnceLock::new(),
186+
component_generated_base_wit_deps: HashMap::new(),
187+
selected_component_names: BTreeSet::new(),
188+
remote_components: RemoteComponents::new(
189+
file_download_client,
190+
temp_dir,
191+
offline,
192+
),
193+
tools_with_ensured_common_deps,
194+
};
195+
ValidatedResult::Ok(ctx)
196+
} else {
197+
result.expect_error()
198+
}
199+
} else {
200+
result.expect_error()
201+
}
202+
}
203+
134204
fn validate_build_profile(&self) -> anyhow::Result<()> {
135205
let Some(profile) = &self.config.build_profile else {
136206
return Ok(());
@@ -160,10 +230,16 @@ impl ApplicationContext {
160230
self.config.build_profile.as_ref()
161231
}
162232

163-
pub fn update_wit_context(&mut self) -> anyhow::Result<()> {
233+
pub async fn update_wit_context(&mut self) -> anyhow::Result<()> {
164234
to_anyhow(
165235
"Failed to update application wit context, see problems above",
166-
ResolvedWitApplication::new(&self.application, self.build_profile()).map(|wit| {
236+
ResolvedWitApplication::new(
237+
&self.application,
238+
self.build_profile(),
239+
&self.tools_with_ensured_common_deps,
240+
)
241+
.await
242+
.map(|wit| {
167243
self.wit = wit;
168244
}),
169245
None,
@@ -639,31 +715,6 @@ impl ApplicationContext {
639715

640716
Ok(())
641717
}
642-
643-
pub async fn ensure_common_deps_for_tool_once(
644-
&self,
645-
tool: &str,
646-
ensure: impl AsyncFnOnce() -> anyhow::Result<()>,
647-
) -> anyhow::Result<()> {
648-
if self
649-
.tools_with_ensured_common_deps
650-
.read()
651-
.await
652-
.contains(tool)
653-
{
654-
return Ok(());
655-
}
656-
657-
let mut lock = self.tools_with_ensured_common_deps.write().await;
658-
659-
let result = ensure().await;
660-
661-
if result.is_ok() {
662-
lock.insert(tool.to_string());
663-
}
664-
665-
result
666-
}
667718
}
668719

669720
fn load_app(

cli/golem-cli/src/context.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -409,11 +409,13 @@ impl Context {
409409
&self,
410410
) -> anyhow::Result<tokio::sync::RwLockWriteGuard<'_, ApplicationContextState>> {
411411
let mut state = self.app_context_state.write().await;
412-
state.init(
413-
&self.available_profile_names,
414-
&self.app_context_config,
415-
self.file_download_client.clone(),
416-
)?;
412+
state
413+
.init(
414+
&self.available_profile_names,
415+
&self.app_context_config,
416+
self.file_download_client.clone(),
417+
)
418+
.await?;
417419
Ok(state)
418420
}
419421

@@ -770,7 +772,7 @@ impl ApplicationContextState {
770772
}
771773
}
772774

773-
fn init(
775+
async fn init(
774776
&mut self,
775777
available_profile_names: &BTreeSet<ProfileName>,
776778
config: &ApplicationContextConfig,
@@ -804,6 +806,7 @@ impl ApplicationContextState {
804806
app_config,
805807
file_download_client,
806808
)
809+
.await
807810
.map_err(Arc::new),
808811
);
809812

cli/golem-cli/src/validation.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,33 @@ impl<T> ValidatedResult<T> {
123123
}
124124
self
125125
}
126+
127+
pub fn take(self) -> (ValidatedResult<()>, Option<T>) {
128+
match self {
129+
ValidatedResult::Ok(value) => (ValidatedResult::Ok(()), Some(value)),
130+
ValidatedResult::OkWithWarns(value, warns) => {
131+
(ValidatedResult::OkWithWarns((), warns), Some(value))
132+
}
133+
ValidatedResult::WarnsAndErrors(warns, errors) => {
134+
(ValidatedResult::WarnsAndErrors(warns, errors), None)
135+
}
136+
}
137+
}
138+
139+
pub fn merge_and_get<U>(self, other: ValidatedResult<U>) -> (ValidatedResult<()>, Option<U>) {
140+
let next = self.combine(other, |_, other| other);
141+
next.take()
142+
}
143+
144+
pub fn expect_error<U>(self) -> ValidatedResult<U> {
145+
match self {
146+
ValidatedResult::Ok(_) => panic!("Expected error"),
147+
ValidatedResult::OkWithWarns(_, _) => panic!("Expected error"),
148+
ValidatedResult::WarnsAndErrors(warns, errors) => {
149+
ValidatedResult::WarnsAndErrors(warns, errors)
150+
}
151+
}
152+
}
126153
}
127154

128155
impl<T, E> ValidatedResult<Result<T, E>> {

0 commit comments

Comments
 (0)