Bash Commands In TypeScript? (This Is Genius)

BBetter Stack
Computing/SoftwareSmall Business/StartupsInternet Technology

Transcript

00:00:00One of the simplest ways of making your AI agents more powerful is by simply giving them
00:00:03access to bash, but that can be a little complex. You need a real shell, you need a file system,
00:00:09servers and containers, and overall just a lot of infrastructure, or at least you did.
00:00:13But what if I told you there's a simpler way? You can actually just use a typescript implementation
00:00:18of bash. Now I know that might sound a little bit crazy, but trust me this is super cool
00:00:22and it could even save you money. Let me show you how.
00:00:30This is a new package called justbash and you can see on that readme it says "simulated
00:00:34bash environment with an in-memory virtual file system written in typescript. This is
00:00:38designed for AI agents that need a secure, sandboxed bash environment."
00:00:42But before I show you how you can actually link this up to your AI agents to give them
00:00:45more capabilities, I just want to show you some really simple code so you have some context
00:00:48for how this works. Here I'm starting a new bash environment from the justbash package
00:00:53and inside of that we can run our bash commands. So we're doing env.exec and here I'm simply
00:00:57running the bash command of echo hello and saying I want to save that to a file called
00:01:01greeting.txt. Then on the next line I'm running an exec again in the same environment saying
00:01:06I now want to read that greeting.txt file. And you can see we can actually log out the
00:01:10standard output of the result that we get back there, the exit code and also the environment
00:01:14variables and when I run this you can see it has successfully written that file and also
00:01:18read it. We get an exit code of zero and then the environment variables in our simulated
00:01:22bash shell. But the important thing to remember here is as I said this is just a simulated
00:01:26bash shell, this is literally a version of those bash commands written in typescript.
00:01:30So it's not actually attached to any real shell, it's simply converting the command that we
00:01:34give it here into typescript and running that. So this file as well called greeting.txt that
00:01:39doesn't exist on any file system, that's actually an in-memory virtual file system although there
00:01:44are options if you do want to attach a real one. So a simple way of thinking about what
00:01:47it's doing is it's simply taking the bash command that we write inside of the exec function
00:01:51and converting that into a javascript function which it then runs which means that it doesn't
00:01:55need a real shell and we can actually see that in that codebase here. When I put echo inside
00:01:59of exec it's actually running this javascript function and we can see this simply has logic
00:02:03for passing the flags that the echo command has in bash and turning them into javascript
00:02:08and then simply printing out the output of whatever we put after echo and you can see
00:02:11down here it simply returns an object that has the standard output with the output in
00:02:15javascript standard error and also an exit code. And if you're thinking that it can't
00:02:19possibly handle all of the basics of bash you can actually see a list here of all of the
00:02:23supported commands and it really has loads of them it has things like cat, copy, orc,
00:02:27base64, it has advanced ones for data processing like jq, python 3 and sqlite and you can even
00:02:33run network requests as well using curl you actually set up a whitelist so it's actually
00:02:36secure and there's also shell features down here so it has pipes, redirections, command
00:02:41chaining it really just gives you everything that a basic bash shell would. So you can see
00:02:45the package is really cool and it's got a lot of functionality but you're also probably still
00:02:48wondering why you'd actually want a bash implementation in typescript and how this can help your agents
00:02:53and save you money. Well to answer that let me show you one of its use cases with a very
00:02:57simple chat app. Now say in this chat app i wanted to talk about this json file this is
00:03:02a json file that contains loads of records and let's say i wanted to ask the ai to retrieve
00:03:06certain information within this or maybe do some analysis on some of these fields you can
00:03:11see it's an absolutely massive file one of the simplest but definitely wrong ways of doing
00:03:15that is you could get the contents of the entire file and put it inside of the prompt you can
00:03:19see here i'm using the ai sdk and also using gpt 5.2. If we actually run this agent and
00:03:24ask it to fetch us a specific record you can see the assistant is going to reply and i can
00:03:28confirm this is correct as these larger models are pretty good at retrieving values from their
00:03:33large context but the trouble is this used 133,000 tokens and it's also going to start
00:03:39to fall apart if you ask it some more advanced questions maybe you want to do some data manipulation
00:03:43or ask it about data within certain ranges. As i said though that is obviously the wrong
00:03:48way to do things so the next way that you may have attempted before is maybe some rag
00:03:51method or even giving the agent a sandbox so it can run bash commands and manipulate the
00:03:56data that way but the trouble with those approaches is you start to need a lot of infrastructure
00:04:00so instead let's just simulate this with just bash. To do this with the ai sdk you can actually
00:04:05use the bash tool package which is built on top of just bash and what this allows us to
00:04:09do is in our api endpoint for the chat we can create a new bash tool passes any files that
00:04:13we want to be in our simulated bash environment so in my case i'm passing through that json
00:04:17file that we had earlier the really large one then i'm also setting up the destination for
00:04:21where these files are going to go so it's going to go into a simulated slash workspace directory
00:04:26after that all we need to do in the stream that we have from our ai sdk is pass it through
00:04:31the bash tool that we have so that's just bash tool dot bash and i've also passed it some
00:04:34agent instructions which you can find in the node modules folder these are simply just instructions
00:04:39that the package has provided that can help your ai agent understand how to use the bash
00:04:42tool now with that very simple setup if we ask the assistant the exact same question
00:04:46to fetch us a piece of information from that json file instead of using its own context
00:04:50and needing to do some needle in the haystack finding you can see it actually goes off and
00:04:54runs some bash commands but again this is in our simulated typescript environment in
00:04:58this case it tried to run a jq command but it looks like it ran into an error so next
00:05:02it ran ahead bash command so we could see what this file actually looked like and that way
00:05:06it got the correct format the jq command and gave us our answer so yes while we did get
00:05:10the exact same answer out of this and maybe it did take a little bit longer the key here
00:05:15is you can actually see it took six thousand input tokens whereas the other method took
00:05:19a hundred and thirty three thousand this is just a way better method for handling long
00:05:24context and that's not even its only advantage it becomes a lot more powerful when you start
00:05:28to ask more deep questions about your data for example if i ask it how many records between
00:05:33a thousand and two thousand five hundred have metadata dot active true you can see you can
00:05:37just simply go off and run a bash command and get us the answer straight away if you actually
00:05:41tried this in the other version a it would take up a lot of context and b it probably
00:05:45just give you a wrong answer based on guessing this is going to be far more accurate as you're
00:05:49essentially just saying to the agent hey you have complete access to a bash shell even though
00:05:53it technically doesn't it's honestly just such a simple free value add that you can provide
00:05:57to your agents to make them more powerful with no additional infrastructure needed and that's
00:06:01what i really love about it hopefully i've showcased just one of its use cases but trust
00:06:05me there is so many more as we actually saw earlier you can get basic python sql and curl
00:06:10commands working in here and if you wanted to you can actually attach this to a real file
00:06:14system let me know what you think of just bash in the comments below while you're there subscribe
00:06:18and as always see you in the next one

Key Takeaway

Justbash offers a lightweight, TypeScript-native solution for giving AI agents powerful bash capabilities without the complexity and cost of managing traditional backend infrastructure.

Highlights

Introduction of justbash, a TypeScript-based simulated bash environment with an in-memory virtual file system.

Elimination of heavy infrastructure requirements like real shells, servers, or containers for AI agent tasks.

Support for a wide range of commands including cat, jq, curl, and even basic Python 3 and SQLite execution.

Significant reduction in LLM token usage by offloading data processing to bash commands instead of passing large files into the prompt context.

Enhanced security through a sandboxed environment and whitelisting for network requests.

Seamless integration with the AI SDK using the bash-tool package to provide agents with computational capabilities.

Timeline

Introduction to TypeScript-Simulated Bash

The speaker introduces the concept of making AI agents more powerful by granting them access to bash commands without typical infrastructure hurdles. Traditional methods require a real shell, file systems, and containers, which are often complex to manage and scale. The video proposes a simpler alternative using a TypeScript implementation of bash called justbash. This approach promises to be secure, sandboxed, and potentially more cost-effective for developers. It sets the stage for a shift from server-heavy AI tools to lightweight, simulated environments.

How justbash Works Under the Hood

The presenter demonstrates the basic syntax and functionality of the justbash package using simple code examples. By initializing a new environment, users can execute commands like echo and read files within a virtual, in-memory file system. The demonstration shows how the system captures standard output, exit codes, and environment variables just like a real terminal. This section emphasizes that 'greeting.txt' and other files created do not actually exist on the physical disk unless configured otherwise. This highlights the primary benefit of a simulated environment that behaves like a real shell without the overhead.

Command Support and Technical Architecture

The video explains that justbash essentially converts bash commands into JavaScript functions to be executed within the runtime. It showcases an extensive list of supported utilities including advanced data processing tools like jq and awk. The speaker notes that it even supports network requests via curl with security features like whitelisting to prevent unauthorized access. Technical features like pipes, redirections, and command chaining are also included to mimic a full shell experience. This architecture allows developers to run complex scripts safely within a TypeScript-controlled sandbox.

Solving the Token Bloat Problem

The speaker presents a practical use case involving a massive JSON file and a chat application using GPT-4o. In a traditional scenario, a developer might pass the entire file content into the prompt, which consumed 133,000 tokens in the example shown. This method is criticized as being inefficient, expensive, and prone to failure when performing complex data manipulation. The segment illustrates why simply increasing context size isn't always the best solution for data-heavy AI tasks. It introduces the need for a more surgical approach to data retrieval and processing.

Implementing the Bash Tool for AI Agents

The demonstration shifts to using the bash-tool package to integrate justbash into an AI SDK environment. The speaker shows how to pass a local JSON file into a simulated workspace directory so the agent can interact with it. By providing specific agent instructions, the AI learns how to use bash commands to query the file instead of reading the whole thing. The process is remarkably simple, requiring only a few lines of code to set up the API endpoint. This implementation allows the AI to self-correct, as seen when it switches from an errored jq command to a head command to understand file structure.

Efficiency Results and Final Comparison

The final section compares the results of the simulated bash method against the traditional 'context-stuffing' approach. Using justbash reduced token usage from 133,000 down to only 6,000 tokens while providing more accurate answers for complex queries. The agent was able to perform specific filtering tasks, such as counting active records within a specific range, with high precision. The speaker concludes that this is a 'free value add' that increases agent power without adding new infrastructure. He encourages viewers to experiment with the tool for Python, SQL, and network tasks before signing off.

Community Posts

View all posts