From d5c1b72c526c4d111b72cdc09893d600507977da Mon Sep 17 00:00:00 2001 From: Bobby Christopher Date: Thu, 14 Aug 2025 09:28:20 -0400 Subject: [PATCH 01/10] so close --- cmd/dev.go | 54 +++++++++++++++++++++---------------- internal/bundler/bundler.go | 1 + internal/bundler/shim.go | 2 +- 3 files changed, 33 insertions(+), 24 deletions(-) diff --git a/cmd/dev.go b/cmd/dev.go index 06af5e4b..a7140928 100644 --- a/cmd/dev.go +++ b/cmd/dev.go @@ -40,11 +40,13 @@ Flags: Examples: agentuity dev - agentuity dev --dir /path/to/project`, + agentuity dev --dir /path/to/project + agentuity dev --no-build`, Run: func(cmd *cobra.Command, args []string) { log := env.NewLogger(cmd) logLevel := env.LogLevel(cmd) apiUrl, appUrl, transportUrl := util.GetURLs(log) + noBuild, _ := cmd.Flags().GetBool("no-build") ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGINT, syscall.SIGTERM) defer cancel() @@ -142,29 +144,32 @@ Examples: errsystem.New(errsystem.ErrInvalidConfiguration, err, errsystem.WithContextMessage("Failed to run project")).ShowErrorAndExit() } - build := func(initial bool) bool { - started := time.Now() - var ok bool - tui.ShowSpinner("Building project ...", func() { - if err := bundler.Bundle(bundler.BundleContext{ - Context: ctx, - Logger: log, - ProjectDir: dir, - Production: false, - DevMode: true, - Writer: os.Stdout, - }); err != nil { - if err == bundler.ErrBuildFailed { - return + var build func(initial bool) bool + if !noBuild { + build = func(initial bool) bool { + started := time.Now() + var ok bool + tui.ShowSpinner("Building project ...", func() { + if err := bundler.Bundle(bundler.BundleContext{ + Context: ctx, + Logger: log, + ProjectDir: dir, + Production: false, + DevMode: true, + Writer: os.Stdout, + }); err != nil { + if err == bundler.ErrBuildFailed { + return + } + errsystem.New(errsystem.ErrInvalidConfiguration, err, errsystem.WithContextMessage(fmt.Sprintf("Failed to bundle project: %s", err))).ShowErrorAndExit() } - errsystem.New(errsystem.ErrInvalidConfiguration, err, errsystem.WithContextMessage(fmt.Sprintf("Failed to bundle project: %s", err))).ShowErrorAndExit() + ok = true + }) + if ok && !initial { + log.Info("✨ Built in %s", time.Since(started).Round(time.Millisecond)) } - ok = true - }) - if ok && !initial { - log.Info("✨ Built in %s", time.Since(started).Round(time.Millisecond)) + return ok } - return ok } runServer := func() { @@ -190,8 +195,10 @@ Examples: } // Initial build must exit if it fails - if !build(true) { - return + if !noBuild { + if !build(true) { + return + } } var restartingLock sync.Mutex @@ -264,4 +271,5 @@ func init() { rootCmd.AddCommand(devCmd) devCmd.Flags().StringP("dir", "d", ".", "The directory to run the development server in") devCmd.Flags().Int("port", 0, "The port to run the development server on (uses project default if not provided)") + devCmd.Flags().Bool("no-build", false, "Do not build the project before running it") } diff --git a/internal/bundler/bundler.go b/internal/bundler/bundler.go index 9f6b4542..15328680 100644 --- a/internal/bundler/bundler.go +++ b/internal/bundler/bundler.go @@ -296,6 +296,7 @@ func bundleJavascript(ctx BundleContext, dir string, outdir string, theproject * } if ctx.DevMode { + ctx.Logger.Debug("build failed: %v", result.Errors) return ErrBuildFailed } diff --git a/internal/bundler/shim.go b/internal/bundler/shim.go index 78b1fd68..c0035665 100644 --- a/internal/bundler/shim.go +++ b/internal/bundler/shim.go @@ -10,7 +10,7 @@ import { dirname as __agentuity_dirname } from 'path'; import { readFileSync as __agentuity_readFileSync, existsSync as __agentuity_existsSync } from 'fs'; const __filename = __agentuity_fileURLToPath(import.meta.url); -const __dirname = __agentuity_dirname(__filename); +const __ag_dirname = __agentuity_dirname(__filename); // List of Node.js built-in modules that might be dynamically required const nodeBuiltins = [ From f1e9456016410530875f56d737c4e9c0f44a7ee0 Mon Sep 17 00:00:00 2001 From: Jeff Haynie Date: Thu, 14 Aug 2025 09:28:05 -0700 Subject: [PATCH 02/10] Add node-fetch patch for isAbortSignal --- internal/bundler/node-fetch.go | 17 +++++++++++++++++ internal/bundler/patch.go | 29 +++++++++++++++++++++++++---- 2 files changed, 42 insertions(+), 4 deletions(-) create mode 100644 internal/bundler/node-fetch.go diff --git a/internal/bundler/node-fetch.go b/internal/bundler/node-fetch.go new file mode 100644 index 00000000..45a62d37 --- /dev/null +++ b/internal/bundler/node-fetch.go @@ -0,0 +1,17 @@ +package bundler + +func init() { + patches["node-fetch"] = patchModule{ + Module: "node-fetch", + Filename: "src/utils/is", + Functions: map[string]patchAction{ + "isAbortSignal": { + After: `if (result) { return true; } + if (_args[0] && _args[0].constructor.name === 'AbortSignal') { + return true; + } + `, + }, + }, + } +} diff --git a/internal/bundler/patch.go b/internal/bundler/patch.go index d5386d7c..d25f4aa4 100644 --- a/internal/bundler/patch.go +++ b/internal/bundler/patch.go @@ -101,11 +101,18 @@ func createPlugin(logger logger.Logger, dir string, shimSourceMap bool) api.Plug } contents := string(buf) var suffix strings.Builder + isJS := strings.HasSuffix(args.Path, ".js") for fn, mod := range mod.Functions { fnname := "function " + fn index := strings.Index(contents, fnname) + var isConstVariable bool if index == -1 { - continue + fnname = "const " + fn + " = " + index = strings.Index(contents, fnname) + isConstVariable = true + if index == -1 { + continue + } } eol := searchBackwards(contents, index, '\n') if eol < 0 { @@ -113,14 +120,28 @@ func createPlugin(logger logger.Logger, dir string, shimSourceMap bool) api.Plug } prefix := strings.TrimSpace(contents[eol+1 : index]) isAsync := strings.Contains(prefix, "async") + isExport := strings.Contains(prefix, "export") newname := "__agentuity_" + fn - newfnname := "function " + newname + var newfnname string + if isConstVariable { + newfnname = "const " + newname + " = " + } else { + newfnname = "function " + newname + } var fnprefix string if isAsync { fnprefix = "async " } + if isExport { + fnprefix += "export " + fnprefix + } contents = strings.Replace(contents, fnname, newfnname, 1) - suffix.WriteString(fnprefix + fnname + "(...args) {\n") + if isJS { + suffix.WriteString(fnprefix + fnname + "() => {\n") + suffix.WriteString("let args = arguments;\n") + } else { + suffix.WriteString(fnprefix + fnname + "(...args) {\n") + } suffix.WriteString("\tlet _args = args;\n") if mod.Before != "" { suffix.WriteString(mod.Before) @@ -150,7 +171,7 @@ func createPlugin(logger logger.Logger, dir string, shimSourceMap bool) api.Plug } } loader := api.LoaderJS - if strings.HasSuffix(args.Path, ".ts") { + if !isJS { loader = api.LoaderTS } return api.OnLoadResult{ From b44e7321d061a0e5d15792cb5b447007e7aa9cc5 Mon Sep 17 00:00:00 2001 From: Bobby Christopher Date: Thu, 14 Aug 2025 12:33:33 -0400 Subject: [PATCH 03/10] no-build for deploy --- cmd/cloud.go | 4 +++- internal/deployer/deployer.go | 8 +++++--- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/cmd/cloud.go b/cmd/cloud.go index 8e0f0da9..c59d44a1 100644 --- a/cmd/cloud.go +++ b/cmd/cloud.go @@ -198,6 +198,7 @@ Examples: description, _ := cmd.Flags().GetString("description") message, _ := cmd.Flags().GetString("message") dryRun, _ := cmd.Flags().GetString("dry-run") + noBuild, _ := cmd.Flags().GetBool("no-build") // remove duplicates and empty strings tags = util.RemoveDuplicates(tags) @@ -308,7 +309,7 @@ Examples: Config: deploymentConfig, OSEnvironment: loadOSEnv(), PromptHelpers: createPromptHelper(), - }) + }, noBuild) if err != nil { errsystem.New(errsystem.ErrDeployProject, err).ShowErrorAndExit() } @@ -1016,6 +1017,7 @@ func init() { cloudDeployCmd.Flags().MarkHidden("ci-message") cloudDeployCmd.Flags().MarkHidden("ci-git-provider") cloudDeployCmd.Flags().MarkHidden("ci-logs-url") + cloudDeployCmd.Flags().Bool("no-build", false, "Do not build the project before running it") cloudDeployCmd.Flags().String("format", "text", "The output format to use for results which can be either 'text' or 'json'") cloudDeployCmd.Flags().String("org-id", "", "The organization to create the project in") diff --git a/internal/deployer/deployer.go b/internal/deployer/deployer.go index ef6c7dc1..1f71d655 100644 --- a/internal/deployer/deployer.go +++ b/internal/deployer/deployer.go @@ -68,7 +68,7 @@ type DeployPreflightCheckData struct { OSEnvironment map[string]string } -func PreflightCheck(ctx context.Context, logger logger.Logger, data DeployPreflightCheckData) (util.ZipDirCallbackMutator, error) { +func PreflightCheck(ctx context.Context, logger logger.Logger, data DeployPreflightCheckData, noBuild bool) (util.ZipDirCallbackMutator, error) { started := time.Now() bundleCtx := bundler.BundleContext{ Context: context.Background(), @@ -78,8 +78,10 @@ func PreflightCheck(ctx context.Context, logger logger.Logger, data DeployPrefli Project: data.Project, Writer: os.Stderr, } - if err := bundler.Bundle(bundleCtx); err != nil { - return nil, err + if !noBuild { + if err := bundler.Bundle(bundleCtx); err != nil { + return nil, err + } } logger.Debug("bundled in %s", time.Since(started)) return bundler.CreateDeploymentMutator(bundleCtx), nil From caa95c5c1acddd9fed096fba42f11a54a640073b Mon Sep 17 00:00:00 2001 From: Bobby Christopher Date: Thu, 14 Aug 2025 14:36:47 -0400 Subject: [PATCH 04/10] added patch for node fetch --- internal/bundler/node-fetch.go | 9 +++++++-- internal/bundler/patch.go | 10 +++++++++- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/internal/bundler/node-fetch.go b/internal/bundler/node-fetch.go index 45a62d37..592ad171 100644 --- a/internal/bundler/node-fetch.go +++ b/internal/bundler/node-fetch.go @@ -1,17 +1,22 @@ package bundler +import "fmt" + func init() { + fmt.Println("patching node-fetch") patches["node-fetch"] = patchModule{ Module: "node-fetch", - Filename: "src/utils/is", + Filename: "lib/index", Functions: map[string]patchAction{ "isAbortSignal": { After: `if (result) { return true; } - if (_args[0] && _args[0].constructor.name === 'AbortSignal') { + console.log(_args[0]); + if (typeof _args[0] === 'object') { return true; } `, }, }, } + } diff --git a/internal/bundler/patch.go b/internal/bundler/patch.go index d25f4aa4..8226d507 100644 --- a/internal/bundler/patch.go +++ b/internal/bundler/patch.go @@ -100,6 +100,7 @@ func createPlugin(logger logger.Logger, dir string, shimSourceMap bool) api.Plug return api.OnLoadResult{}, err } contents := string(buf) + // fmt.Println(contents) var suffix strings.Builder isJS := strings.HasSuffix(args.Path, ".js") for fn, mod := range mod.Functions { @@ -121,6 +122,9 @@ func createPlugin(logger logger.Logger, dir string, shimSourceMap bool) api.Plug prefix := strings.TrimSpace(contents[eol+1 : index]) isAsync := strings.Contains(prefix, "async") isExport := strings.Contains(prefix, "export") + isArrow := strings.Contains(contents[index:], "=>") + fmt.Println(fnname, contents[index:index+20]) + fmt.Println(isArrow) newname := "__agentuity_" + fn var newfnname string if isConstVariable { @@ -137,7 +141,11 @@ func createPlugin(logger logger.Logger, dir string, shimSourceMap bool) api.Plug } contents = strings.Replace(contents, fnname, newfnname, 1) if isJS { - suffix.WriteString(fnprefix + fnname + "() => {\n") + if isArrow { + suffix.WriteString(fnprefix + fnname + "() => {\n") + } else { + suffix.WriteString(fnprefix + fnname + "() {\n") + } suffix.WriteString("let args = arguments;\n") } else { suffix.WriteString(fnprefix + fnname + "(...args) {\n") From f27f7883e241ee8ea976af6b501b5c34cf745fda Mon Sep 17 00:00:00 2001 From: Bobby Christopher Date: Thu, 14 Aug 2025 14:39:23 -0400 Subject: [PATCH 05/10] remove prints --- internal/bundler/node-fetch.go | 3 --- internal/bundler/patch.go | 3 --- 2 files changed, 6 deletions(-) diff --git a/internal/bundler/node-fetch.go b/internal/bundler/node-fetch.go index 592ad171..148ed166 100644 --- a/internal/bundler/node-fetch.go +++ b/internal/bundler/node-fetch.go @@ -1,9 +1,6 @@ package bundler -import "fmt" - func init() { - fmt.Println("patching node-fetch") patches["node-fetch"] = patchModule{ Module: "node-fetch", Filename: "lib/index", diff --git a/internal/bundler/patch.go b/internal/bundler/patch.go index 8226d507..2117f8e7 100644 --- a/internal/bundler/patch.go +++ b/internal/bundler/patch.go @@ -100,7 +100,6 @@ func createPlugin(logger logger.Logger, dir string, shimSourceMap bool) api.Plug return api.OnLoadResult{}, err } contents := string(buf) - // fmt.Println(contents) var suffix strings.Builder isJS := strings.HasSuffix(args.Path, ".js") for fn, mod := range mod.Functions { @@ -123,8 +122,6 @@ func createPlugin(logger logger.Logger, dir string, shimSourceMap bool) api.Plug isAsync := strings.Contains(prefix, "async") isExport := strings.Contains(prefix, "export") isArrow := strings.Contains(contents[index:], "=>") - fmt.Println(fnname, contents[index:index+20]) - fmt.Println(isArrow) newname := "__agentuity_" + fn var newfnname string if isConstVariable { From 8e51bcfbd8c039c6d18106b2d23f1d52eaf5ff23 Mon Sep 17 00:00:00 2001 From: Bobby Christopher Date: Thu, 14 Aug 2025 15:01:02 -0400 Subject: [PATCH 06/10] remove logs sdk --- internal/bundler/node-fetch.go | 1 - 1 file changed, 1 deletion(-) diff --git a/internal/bundler/node-fetch.go b/internal/bundler/node-fetch.go index 148ed166..e220f121 100644 --- a/internal/bundler/node-fetch.go +++ b/internal/bundler/node-fetch.go @@ -7,7 +7,6 @@ func init() { Functions: map[string]patchAction{ "isAbortSignal": { After: `if (result) { return true; } - console.log(_args[0]); if (typeof _args[0] === 'object') { return true; } From e6f9e271fa1939270a5eb720e6b617eda8ba1c1f Mon Sep 17 00:00:00 2001 From: Bobby Christopher Date: Thu, 14 Aug 2025 15:07:23 -0400 Subject: [PATCH 07/10] add patch for other versoin --- internal/bundler/node-fetch.go | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/internal/bundler/node-fetch.go b/internal/bundler/node-fetch.go index e220f121..c9fce2b4 100644 --- a/internal/bundler/node-fetch.go +++ b/internal/bundler/node-fetch.go @@ -1,16 +1,28 @@ package bundler +var patchCode = ` +if (result) { return true; } +if (typeof _args[0] === 'object') { + return true; +} +` + func init() { - patches["node-fetch"] = patchModule{ + patches["node-fetch-2.7.0"] = patchModule{ Module: "node-fetch", Filename: "lib/index", Functions: map[string]patchAction{ "isAbortSignal": { - After: `if (result) { return true; } - if (typeof _args[0] === 'object') { - return true; - } - `, + After: patchCode, + }, + }, + } + patches["node-fetch-3.3.2"] = patchModule{ + Module: "node-fetch", + Filename: "src/utils/is", + Functions: map[string]patchAction{ + "isAbortSignal": { + After: patchCode, }, }, } From 3459ff013a7074ea4adcad1dade3556f38e2c96e Mon Sep 17 00:00:00 2001 From: Bobby Christopher Date: Thu, 14 Aug 2025 15:13:50 -0400 Subject: [PATCH 08/10] mark flags as hidden --- cmd/cloud.go | 3 ++- cmd/dev.go | 57 +++++++++++++++++++++++++++------------------------- 2 files changed, 32 insertions(+), 28 deletions(-) diff --git a/cmd/cloud.go b/cmd/cloud.go index c59d44a1..00beab5d 100644 --- a/cmd/cloud.go +++ b/cmd/cloud.go @@ -1017,7 +1017,8 @@ func init() { cloudDeployCmd.Flags().MarkHidden("ci-message") cloudDeployCmd.Flags().MarkHidden("ci-git-provider") cloudDeployCmd.Flags().MarkHidden("ci-logs-url") - cloudDeployCmd.Flags().Bool("no-build", false, "Do not build the project before running it") + cloudDeployCmd.Flags().Bool("no-build", false, "Do not build the project before running it (useful for debugging)") + cloudDeployCmd.Flags().MarkHidden("no-build") cloudDeployCmd.Flags().String("format", "text", "The output format to use for results which can be either 'text' or 'json'") cloudDeployCmd.Flags().String("org-id", "", "The organization to create the project in") diff --git a/cmd/dev.go b/cmd/dev.go index a7140928..de9f6d8a 100644 --- a/cmd/dev.go +++ b/cmd/dev.go @@ -145,31 +145,34 @@ Examples: } var build func(initial bool) bool - if !noBuild { - build = func(initial bool) bool { - started := time.Now() - var ok bool - tui.ShowSpinner("Building project ...", func() { - if err := bundler.Bundle(bundler.BundleContext{ - Context: ctx, - Logger: log, - ProjectDir: dir, - Production: false, - DevMode: true, - Writer: os.Stdout, - }); err != nil { - if err == bundler.ErrBuildFailed { - return - } - errsystem.New(errsystem.ErrInvalidConfiguration, err, errsystem.WithContextMessage(fmt.Sprintf("Failed to bundle project: %s", err))).ShowErrorAndExit() + + build = func(initial bool) bool { + if noBuild { + log.Info("Skipping build (no-build flag set)") + return true + } + started := time.Now() + var ok bool + tui.ShowSpinner("Building project ...", func() { + if err := bundler.Bundle(bundler.BundleContext{ + Context: ctx, + Logger: log, + ProjectDir: dir, + Production: false, + DevMode: true, + Writer: os.Stdout, + }); err != nil { + if err == bundler.ErrBuildFailed { + return } - ok = true - }) - if ok && !initial { - log.Info("✨ Built in %s", time.Since(started).Round(time.Millisecond)) + errsystem.New(errsystem.ErrInvalidConfiguration, err, errsystem.WithContextMessage(fmt.Sprintf("Failed to bundle project: %s", err))).ShowErrorAndExit() } - return ok + ok = true + }) + if ok && !initial { + log.Info("✨ Built in %s", time.Since(started).Round(time.Millisecond)) } + return ok } runServer := func() { @@ -195,10 +198,9 @@ Examples: } // Initial build must exit if it fails - if !noBuild { - if !build(true) { - return - } + + if !build(true) { + return } var restartingLock sync.Mutex @@ -271,5 +273,6 @@ func init() { rootCmd.AddCommand(devCmd) devCmd.Flags().StringP("dir", "d", ".", "The directory to run the development server in") devCmd.Flags().Int("port", 0, "The port to run the development server on (uses project default if not provided)") - devCmd.Flags().Bool("no-build", false, "Do not build the project before running it") + devCmd.Flags().Bool("no-build", false, "Do not build the project before running it (useful for debugging)") + devCmd.Flags().MarkHidden("no-build") } From f01ffbc23541ed5b99fc490d9546050a966aece5 Mon Sep 17 00:00:00 2001 From: Bobby Christopher Date: Fri, 15 Aug 2025 16:45:01 -0400 Subject: [PATCH 09/10] added rabbit fix --- internal/bundler/patch.go | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/internal/bundler/patch.go b/internal/bundler/patch.go index 2117f8e7..10f6a7e6 100644 --- a/internal/bundler/patch.go +++ b/internal/bundler/patch.go @@ -121,7 +121,6 @@ func createPlugin(logger logger.Logger, dir string, shimSourceMap bool) api.Plug prefix := strings.TrimSpace(contents[eol+1 : index]) isAsync := strings.Contains(prefix, "async") isExport := strings.Contains(prefix, "export") - isArrow := strings.Contains(contents[index:], "=>") newname := "__agentuity_" + fn var newfnname string if isConstVariable { @@ -138,11 +137,7 @@ func createPlugin(logger logger.Logger, dir string, shimSourceMap bool) api.Plug } contents = strings.Replace(contents, fnname, newfnname, 1) if isJS { - if isArrow { - suffix.WriteString(fnprefix + fnname + "() => {\n") - } else { - suffix.WriteString(fnprefix + fnname + "() {\n") - } + suffix.WriteString(fnprefix + "function " + fn + "() {\n") suffix.WriteString("let args = arguments;\n") } else { suffix.WriteString(fnprefix + fnname + "(...args) {\n") @@ -152,7 +147,15 @@ func createPlugin(logger logger.Logger, dir string, shimSourceMap bool) api.Plug suffix.WriteString(mod.Before) suffix.WriteString("\n") } - suffix.WriteString("\tlet result = " + newname + "(..._args);\n") + + if isJS { + // For JS: use .apply to preserve 'this' context + suffix.WriteString("\tlet result = " + newname + ".apply(this, _args);\n") + } else { + // For TS: use spread operator + suffix.WriteString("\tlet result = " + newname + "(..._args);\n") + } + if isAsync { suffix.WriteString("\tif (result instanceof Promise) {\n") suffix.WriteString("\t\tresult = await result;\n") From 8a0bdccb355ab30313e386892471cd938c9df18c Mon Sep 17 00:00:00 2001 From: Bobby Christopher Date: Mon, 18 Aug 2025 13:21:21 -0400 Subject: [PATCH 10/10] I think this should work --- internal/bundler/shim.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/internal/bundler/shim.go b/internal/bundler/shim.go index c0035665..33ed8dc6 100644 --- a/internal/bundler/shim.go +++ b/internal/bundler/shim.go @@ -10,7 +10,9 @@ import { dirname as __agentuity_dirname } from 'path'; import { readFileSync as __agentuity_readFileSync, existsSync as __agentuity_existsSync } from 'fs'; const __filename = __agentuity_fileURLToPath(import.meta.url); -const __ag_dirname = __agentuity_dirname(__filename); +if (!('__dirname' in globalThis)) { + globalThis.__dirname = __agentuity_dirname(__filename); +} // List of Node.js built-in modules that might be dynamically required const nodeBuiltins = [