# @dopt/ai-assistant-javascript

> An SDK for the AI assistant

Latest version **0.0.16** (published 2024-04-24) · MIT license · 0 weekly downloads

## Install

```sh
npm install @dopt/ai-assistant-javascript
pnpm add @dopt/ai-assistant-javascript
yarn add @dopt/ai-assistant-javascript
bun add @dopt/ai-assistant-javascript
```

## Health

**Score 30/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.16 |
| Published | 2024-04-24 |
| First published | 2024-02-07 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 5 |
| Unpacked size | 46.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Maintainers | kseth, alexchantastic, pankaj-avhad-dopt, chasebish, joemckenney |

## Links

- npm: https://www.npmjs.com/package/@dopt/ai-assistant-javascript
- Repository: https://github.com/dopt/odopt
- Homepage: https://github.com/dopt/odopt/tree/main/packages/%40dopt/ai-assistant/javascript
- Issues: https://github.com/dopt/odopt/issues
- npm.io page: https://npm.io/package/@dopt/ai-assistant-javascript

## Dependencies (5)

- [@dopt/logger](https://npm.io/package/@dopt/logger.md) 0.2.0
- [@sinclair/typebox](https://npm.io/package/@sinclair/typebox.md) 0.25.4
- [@dopt/ai-assistant-context](https://npm.io/package/@dopt/ai-assistant-context.md) 0.0.10
- [@dopt/ai-javascript-client](https://npm.io/package/@dopt/ai-javascript-client.md) 0.1.2
- [@dopt/ai-assistant-definition](https://npm.io/package/@dopt/ai-assistant-definition.md) 0.0.8

## Recent versions

- 0.0.16 (latest) — 2024-04-24
- 0.0.15 — 2024-04-18
- 0.0.13 — 2024-03-06
- 0.0.12 — 2024-03-01
- 0.0.11 — 2024-02-29
- 0.0.10 — 2024-02-27
- 0.0.9 — 2024-02-27
- 0.0.8 — 2024-02-23
- 0.0.7 — 2024-02-22
- 0.0.6 — 2024-02-20
- 0.0.5 — 2024-02-16
- 0.0.4 — 2024-02-13
- 0.0.3 — 2024-02-13
- 0.0.1 — 2024-02-07

## README

# @dopt/ai-assistant-javascript

## Overview

The AI Assistant JavaScript package is a thin abstraction on top of [`@dopt/ai-javascript-client`](https://www.npmjs.com/package/@dopt/ai-javascript-client) that helps with context formation.

It is published to npm as [`@dopt/ai-assistant-javascript`](https://www.npmjs.com/package/@dopt/ai-assistant-javascript).

## Installation

Via npm:

```bash
npm install @dopt/ai-assistant-javascript
```

Via Yarn:

```bash
yarn add @dopt/ai-assistant-javascript
```

Via pnpm:

```bash
pnpm add @dopt/ai-assistant-javascript
```

## Configuration

To configure the Dopt provider you will need:

1. An AI API key (generated in Dopt)
1. The Assistant identifier
1. A user identifier (user being an end-user you've identified to Dopt)

## Usage

### Initialization

```ts
import { Assistant } from '@dopt/ai-assistant-javascript';

const assistant = new Assistant({
  apiKey: process.env.DOPT_AI_API_KEY as string,
  userId,
});
```

### Context

The AI Assistant API provides context-dependent completions, where context is information gathered from the web page the user is on.

The following types outline the context the API expects.

```ts
type DocumentContext = {
  type: 'document';
  value: {
    url: string;
    title: string;
    width: number;
    height: number;
  };
};

type ElementContext = {
  type: 'element';
  value: {
    position: {
      top: number;
      left: number;
    };
    content: string;
    tag: string;
  };
};

type VisualContext = {
  type: 'visual';
  value: string;
};

type SemanticContext = {
  type: 'semantic';
  value: {
    semanticContent: string;
    neighboringSemanticContent: string;
  };
};
```

Creating this context manually is onerous, so we've simplified context creation in this package. See #Completions below for usage.

### Completions

Stream completions for an Assistant

```ts
export interface AssistantContext {
  document?: boolean | DocumentContext['value'];
  element?: Element | ElementContext['value'];
  semantic?: boolean | SemanticContext['value'];
  visual?: boolean | VisualContext['value'];
}

const context: AssistantContext = {
  document: true,
  element, // An HTML element
  semantic: true,
  visual: true,
};

const events = await assistant.completions.stream(sid, {
  query: 'Some question?',
  context,
});

let content = '';
for await (const event of events) {
  switch (event.type) {
    case 'status':
      console.log(event.status);
      break;
    case 'answer':
      console.log(event.answer);
      break;
    case 'content':
      content += event.content;
      console.log(content);
      break;
  }
}
```

---
_Source: https://npm.io/package/@dopt/ai-assistant-javascript · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
