Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate API client & server to mongodb-chatbot-api #419

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
768 changes: 745 additions & 23 deletions package-lock.json

Large diffs are not rendered by default.

31 changes: 31 additions & 0 deletions packages/mongodb-chatbot-api/.eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
module.exports = {
env: {
browser: true,
es2020: true,
},
root: true,
parser: "@typescript-eslint/parser",
extends: [
"eslint:recommended",
"plugin:@typescript-eslint/recommended",
"plugin:react-hooks/recommended",
"plugin:storybook/recommended",
"prettier",
],
parserOptions: {
ecmaVersion: "latest",
sourceType: "module",
},
plugins: ["react-refresh"],
rules: {
"react-refresh/only-export-components": "warn",
"@typescript-eslint/no-unused-vars": [
"warn",
{
varsIgnorePattern: "^_",
argsIgnorePattern: "^_",
ignoreRestSiblings: true,
},
],
},
};
29 changes: 29 additions & 0 deletions packages/mongodb-chatbot-api/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*
lerna-debug.log*

node_modules
build
build-ssr
*.local

!.env.staging

# Editor directories and files
.vscode/*
!.vscode/extensions.json
.idea
.DS_Store
*.suo
*.ntvs*
*.njsproj
*.sln
*.sw?

# Build analysis
stats.html
6 changes: 6 additions & 0 deletions packages/mongodb-chatbot-api/.prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
node_modules
build

package-lock.json
pnpm-lock.yaml
yarn.lock
6 changes: 6 additions & 0 deletions packages/mongodb-chatbot-api/.prettierrc.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const baseConfig = require(`${__dirname}/../../.prettierrc.cjs`);

module.exports = {
...baseConfig,
printWidth: 90,
};
24 changes: 24 additions & 0 deletions packages/mongodb-chatbot-api/.release-it.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"git": {
"commitArgs": ["-a"],
"commitMessage": "Release ${npm.name} v${version}",
"changelog": "git log --pretty=format:'* %s (%h)' ${latestTag}..HEAD -- .",
"tagMatch": "${npm.name}-*",
"getLatestTagFromAllRefs": true,
"tag": true,
"push": true,
"pushRepo": "upstream",
"tagName": "${npm.name}-v${version}"
},
"npm": {
"publish": false
},
"github": {
"releaseName": "${npm.name}-v${version}",
"draft": true,
"release": true
},
"hooks": {
"before:init": ["npm run lint", "npm run build"]
}
}
57 changes: 57 additions & 0 deletions packages/mongodb-chatbot-api/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# MongoDB Chatbot UI

The React components for the [MongoDB Chatbot Framework](https://mongodb.github.io/chatbot/).

## Documentation

To learn more about the MongoDB Chatbot UI components, check out the [documentation](https://mongodb.github.io/chatbot/ui/).

For more information about the available components and props, refer to [the documentation](https://mongodb.github.io/chatbot/ui/).

## Install

Install the `mongodb-chatbot-ui` package from npm. This contains the React.js components that you can use to build a chatbot UI.

```shell
npm install mongodb-chatbot-ui
```

## Usage

```tsx
import Chatbot, {
FloatingActionButtonTrigger,
InputBarTrigger,
ModalView,
MongoDbLegalDisclosure,
mongoDbVerifyInformationMessage,
} from "mongodb-chatbot-ui";

function MyApp() {
const suggestedPrompts = [
"How do I create a new MongoDB Atlas cluster?",
"Can MongoDB store lists of data?",
"How does vector search work?",
];
return (
<div>
<Chatbot
name="MongoDB AI"
maxInputCharacters={300}
>
<InputBarTrigger
bottomContent={<MongoDbLegalDisclosure />}
suggestedPrompts={suggestedPrompts}
/>
<FloatingActionButtonTrigger text="Ask My MongoDB AI" />
<ModalView
disclaimer={<MongoDbLegalDisclosure />}
initialMessageText="Welcome to my MongoDB AI Assistant. What can I help you with?"
initialMessageSuggestedPrompts={suggestedPrompts}
inputBottomText={mongoDbVerifyInformationMessage}
/>
</Chatbot>
</div>
);
}
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import { vi } from "vitest";
import { type EventSourceMessage } from "@microsoft/fetch-event-source";

type MockEvent = {
id?: string;
type?: string;
data: string | Record<string, unknown>;
};

function createEventSourceMessage(
data: string | Record<string, unknown>,
{ type, id }: { type?: string; id?: string } = {}
): EventSourceMessage {
return {
id: id ?? "",
event: type ?? "",
data: JSON.stringify(data),
retry: 0,
};
}

let events: EventSourceMessage[] = [];

export function __addMockEvents(...newEvents: MockEvent[]) {
events = events.concat(
newEvents.map((e) => createEventSourceMessage(e.data, { type: e.type, id: e.id }))
);
}

export function __clearMockEvents() {
events = [];
}

export const fetchEventSource = vi.fn(async (_url, options) => {
const {
// signal,
// method,
// headers,
// body,
// openWhenHidden,
// onerror,
onmessage,
onopen,
onclose,
} = options;

return new Promise((resolve, _reject) => {
const body = new ReadableStream();
const res = {
ok: true,
headers: new Headers({
"content-type": "text/event-stream",
}),
redirected: false,
status: 200,
statusText: "",
type: "cors",
url: "",
clone: () => res,
body,
bodyUsed: true,
arrayBuffer: async () => new ArrayBuffer(0),
blob: async () => new Blob(),
formData: async () => new FormData(),
json: async () => ({}),
text: async () => "",
} satisfies Response;
onopen(res);
for (const [i, event] of Object.entries(events)) {
setTimeout(() => {
onmessage(event);
if (Number(i) === events.length - 1) {
onclose(res);
resolve(undefined);
}
}, 10);
}
});
});
13 changes: 13 additions & 0 deletions packages/mongodb-chatbot-api/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>MongoDB Chatbot Tester</title>
</head>
<body>
<div id="root"></div>
<script type="module" src="/src/main.tsx"></script>
</body>
</html>
108 changes: 108 additions & 0 deletions packages/mongodb-chatbot-api/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
{
"name": "mongodb-chatbot-api",
"version": "0.0.0",
"description": "A framework-agnostic TypeScript client library for the MongoDB AI Chatbot API.",
"author": "MongoDB, Inc.",
"license": "Apache-2.0",
"keywords": [
"retrieval-augmented-generation",
"rag",
"chatbot",
"mongodb",
"mongodb-chatbot-server",
"mongodb-chatbot-framework"
],
"repository": {
"type": "git",
"url": "https://github.com/mongodb/chatbot.git"
},
"homepage": "https://mongodb.github.io/chatbot/ui/",
"engines": {
"node": ">=18",
"npm": ">=8"
},
"type": "module",
"files": [
"build",
"src",
"README.md"
],
"exports": {
".": {
"import": "./build/mongodb-chatbot-api.es.js",
"require": "./build/mongodb-chatbot-api.cjs.js",
"types": "./build/index.d.ts"
},
"./client": {
"import": "./build/client/mongodb-chatbot-api-client.es.js",
"require": "./build/client/mongodb-chatbot-api-client.cjs.js",
"types": "./build/client/index.d.ts"
},
"./server": {
"import": "./build/server/mongodb-chatbot-api-server.es.js",
"require": "./build/server/mongodb-chatbot-api-server.cjs.js",
"types": "./build/server/index.d.ts"
}
},
"scripts": {
"build": "rm -rf build && mkdir build && npm run build:combined && npm run build:client && npm run build:server && cp -r src build/src",
"build:combined": "tsc && find build -maxdepth 1 -type f && vite build --config vite.config.ts",
"build:client": "tsc && rm -rf build/client/* && VITE_BUILD_TARGET=client vite build --config vite.config.ts",
"build:server": "tsc && rm -rf build/server/* && VITE_BUILD_TARGET=server vite build --config vite.config.ts",
"dev": "VITE_GIT_COMMIT=$(npm run --silent env:get-git-commit) vite --config vite.config.component.js --mode development",
"env:get-git-commit": "if [ -z \"$VITE_GIT_COMMIT\" ]; then echo \"$(git rev-parse --short HEAD)\"; else echo \"$VITE_GIT_COMMIT\"; fi",
"format": "prettier ./src --write",
"format:check": "prettier ./src --check",
"lint": "eslint ./src --ext ts,tsx,js,jsx --report-unused-disable-directives",
"lint:fix": "npm run lint -- --fix && prettier ./src --check --write",
"package": "rm -rf build && npm run build:component && npm pack",
"release": "release-it",
"test": "vitest run",
"test:watch": "vitest"
},
"dependencies": {
"@microsoft/fetch-event-source": "^2.0.1",
"merge-headers": "^1.1.1",
"prettier": "^2.8.8",
"zod": "^3.23.8"
},
"devDependencies": {
"@rollup/plugin-inject": "^5.0.3",
"@storybook/addon-essentials": "^7.0.20",
"@storybook/addon-interactions": "^7.0.20",
"@storybook/addon-links": "^7.0.20",
"@storybook/blocks": "^7.0.20",
"@storybook/react": "^7.0.20",
"@storybook/react-vite": "^7.0.20",
"@storybook/testing-library": "^0.0.14-next.2",
"@types/react": "^18.0.37",
"@types/react-dom": "^18.0.11",
"@types/react-transition-group": "^4.4.6",
"@typescript-eslint/eslint-plugin": "^5.59.0",
"@typescript-eslint/parser": "^5.59.0",
"@vitejs/plugin-react": "^4.0.0",
"eslint": "^8.38.0",
"eslint-config-prettier": "^8.8.0",
"eslint-plugin-react-hooks": "^4.6.0",
"eslint-plugin-react-refresh": "^0.3.4",
"eslint-plugin-storybook": "^0.6.12",
"jsdom": "^22.1.0",
"mocksse": "^1.0.4",
"node-stdlib-browser": "^1.2.0",
"postcss": "^8.4.27",
"prop-types": "^15.8.1",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-refresh": "^0.14.0",
"release-it": "^16.1.5",
"rollup": "^3.26.3",
"rollup-plugin-visualizer": "^5.12.0",
"storybook": "^7.0.20",
"typescript": "^5.1.6",
"vite": "^4.4.7",
"vite-plugin-dts": "^3.9.1",
"vite-plugin-libcss": "^1.1.0",
"vite-plugin-node-polyfills": "^0.9.0",
"vitest": "^0.34.4"
}
}
Loading