1.0.54 • Published 8 months ago

@dust-tt/client v1.0.54

Weekly downloads
-
License
ISC
Repository
github
Last release
8 months ago

Build status npm version

Source code available here.

Installation

npm install @dust-tt/client

Usage

Use Dust's Dust Development Platform to start learning about Dust API.

Import and initialize a client using an workspace api key or an OAuth access token (for now, requires talking to us).

Note: we use the Result pattern to handle errors (see more).

Setup

import { DustAPI } from "@dust-tt/client";

const dustAPI = new DustAPI(
  {
    url: "https://dust.tt",
  },
  {
    workspaceId: "YOUR_WORKSPACE_ID",
    apiKey: "YOUR_API_KEY_OR_ACCESS_TOKEN",
  },
  console
);

Get all agents

const r = await dustApi.getAgentConfigurations();

if (r.isErr()) {
  throw new Error(`API Error: ${r.error.message}`);
} else {
  const agents = r.value.filter((agent) => agent.status === "active");
}

Create a conversation

const context = {
  timezone: "UTC",
  username: user.firstName,
  email: user.email,
  fullName: user.fullName,
  profilePictureUrl: user.image,
  origin: "api", // Contact us to add more
};

const question = "Hello! What can you do for me ?";

const r = await dustApi.createConversation({
  title: null,
  visibility: "unlisted",
  message: {
    content: question,
    mentions: [
      {
        configurationId: agent.sId,
      },
    ],
    context,
  },
});

if (r.isErr()) {
  throw new Error(r.error.message);
} else {
  const { conversation, message } = r.value;

  try {
    const r = await dustApi.streamAgentAnswerEvents({
      conversation,
      userMessageId: message.sId,
      signal,
    });
    if (r.isErr()) {
      throw new Error(r.error.message);
    } else {
      const { eventStream } = r.value;

      let answer = "";
      let action: AgentActionPublicType | undefined = undefined;
      let chainOfThought = "";

      for await (const event of eventStream) {
        if (!event) {
          continue;
        }
        switch (event.type) {
          case "user_message_error": {
            console.error(
              `User message error: code: ${event.error.code} message: ${event.error.message}`
            );
            return;
          }
          case "agent_error": {
            console.error(
              `Agent message error: code: ${event.error.code} message: ${event.error.message}`
            );
            return;
          }
          case "agent_action_success": {
            action = event.action;
            break;
          }

          case "generation_tokens": {
            if (event.classification === "tokens") {
              answer = (answer + event.text).trim();
            } else if (event.classification === "chain_of_thought") {
              chainOfThought += event.text;
            }
            break;
          }
          case "agent_message_success": {
            answer = event.message.content ?? "";
            break;
          }
          default:
          // Nothing to do on unsupported events
        }
      }
    }
  } catch (error) {
    if (error instanceof Error && error.message.includes("AbortError")) {
      // Stream aborted
    } else {
      // Other error
    }
  }
}
1.0.54

8 months ago

1.0.53

8 months ago

1.0.52

9 months ago

1.0.51

9 months ago

1.0.50

9 months ago

1.0.49

9 months ago

1.0.48

9 months ago

1.0.47

9 months ago

1.0.46

9 months ago

1.0.45

9 months ago

1.0.44

9 months ago

1.0.43

9 months ago

1.0.41

10 months ago

1.0.40

10 months ago

1.0.39

10 months ago

1.0.38

10 months ago

1.0.37

10 months ago

1.0.36

11 months ago

1.0.35

11 months ago

1.0.34

11 months ago

1.0.33

11 months ago

1.0.32

11 months ago

1.0.31

11 months ago

1.0.30

11 months ago

1.0.29

11 months ago

1.0.28

11 months ago

1.0.27

12 months ago

1.0.26

1 year ago

1.0.25

1 year ago

1.0.24

1 year ago

1.0.23

1 year ago

1.0.22

1 year ago

1.0.21

1 year ago

1.0.20

1 year ago

1.0.19

1 year ago

1.0.18

1 year ago

1.0.17

1 year ago

1.0.16

1 year ago

1.0.15

1 year ago

1.0.14

1 year ago

1.0.13

1 year ago

1.0.12

1 year ago

1.0.11

1 year ago

1.0.10

1 year ago

1.0.9

1 year ago

1.0.8

1 year ago

1.0.7

1 year ago

1.0.6

1 year ago

1.0.5

1 year ago

1.0.4

1 year ago

1.0.3

1 year ago

1.0.2

1 year ago

1.0.1

1 year ago

1.0.0

1 year ago