DEV Community

Cover image for Using the Pieces SDK Copilot Wrapper in JavaScript
Ellie
Ellie

Posted on

Using the Pieces SDK Copilot Wrapper in JavaScript

While participating in Global Hack Week, I noticed one of the challenges (the Hello World using Pieces challenge) didn't have a walkthrough. So I decided to make a simple "how-to" for this challenge.

In this article, we will be using the TypeScript SDK to ask Pieces this question:

What is the most common first project for software devs? (hint it starts with hello and ends with world)

Then we will log Pieces' response to the console. Also, even though we are using the TypeScript SDK, this challenge will be completed using JavaScript. So even if you don't know TypeScript you can try to make some simple projects using the Pieces SDK😄

There are also Python, Dart, and Kotlin SDKs, which can be implemented similarly. If you find anything confusing, you can always check the Pieces build docs, Mason's SDK repos, or ask for help in the Discord.

Let's get started!

Using the Pieces SDK with a Copilot Wrapper

Here, we will use a Copilot wrapper (which simplifies the interaction with the Pieces SDK) to ask a simple question and log the response to the console. The only prerequisite is that you have Pieces installed on your device.

Start by installing Pieces SDK:
npm install pieces-copilot-sdk

Then we need to require the Pieces SDK:
const { PiecesClient } = require('pieces-copilot-sdk');

Next, create an instance of PiecesClient:
const piecesClient = new PiecesClient({ baseUrl: 'http://localhost:1000' });

The baseURL will be localhost:1000 for mac/windows but for Linux users it is localhost:5323 (so just make sure you have the right one!)

With the client set up, we can now write a function to ask Pieces a question using the askQuestion() method provided by the Pieces SDK:

async function askPieces() {
  try {
    const response = await piecesClient.askQuestion({
      question: "What is the most common first project for software devs? 
(hint it starts with hello and ends with world)"
    });
    console.log(response);
  } catch (error) {
    console.error("Error:", error);
  }
}

askPieces();
Enter fullscreen mode Exit fullscreen mode

In this function, the askQuestion() method takes a single parameter, the question you want to ask. The response is logged to the console, and any errors are caught and printed.

This method provides a straightforward way to interact with the Pieces API using a wrapper. Also, if you are just trying to complete the challenge for Hack Week, this would be totally acceptable!

If you are interested in learning about the underlying implementation for the askQuestion endpoint check out the index.ts file from the wrapper SDK code

However, if you would like to try and use the Pieces SDK without the wrapper, check out the next article in this Learn Pieces SDK with me series👀

Console Output

In the console you should see a response similar to this:

The most common first project for software developers is the "Hello, World!" program. 
It is a simple program that outputs or displays the text "Hello, World!" to the user. 
This project is often used as a beginner's introduction to programming languages and serves as a way to verify that the development environment is set up correctly.
Here is an example of a "Hello, World!" program in Python:

print("Hello, World!")

This project is a great starting point for beginners to get familiar with the syntax and structure of a programming language.
Enter fullscreen mode Exit fullscreen mode

Well, that's it for this article. I would love to start building some cool projects with the Pieces SDK, do you have any suggestions?

Whatever I build will be turned into a tutorial and added to this article series, so we can all learn together😄

If you have any questions or want to connect, the best place to reach me is on X/Twitter.

Happy coding!

Top comments (4)

Collapse
 
sophyia profile image
Sophia Iroegbu

Yayyy! Let's goooo

Collapse
 
elliezub profile image
Ellie

Yay! Thanks for reading Sophia😊

Collapse
 
krishnasarathi profile image
Krishna Sarathi Ghosh

This is such a good article! Looking forward to more in this series!!

Collapse
 
shivamkatare profile image
Shivam Katare

Great Read! Thanks for sharing 🙌🙌