-
Notifications
You must be signed in to change notification settings - Fork 245
chore(compass-assistant): add compass AI assistant with placeholders COMPASS-9603 #7189
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
Merged
Merged
Changes from 6 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
a9f31a7
chore: generate workspace
gagik 2688960
chore(compass-assistant): add compass AI assistant with placeholders
gagik 2cb2cca
chore: add apache license header
gagik 989329b
chore: add feature flag and fix lint
gagik 27fe44a
chore: add missing dependency
gagik 9fc210d
chore: fix check
gagik 95ddec6
chore: fix check, add whiteSpace: 'pre-wrap' for nicer placeholder fo…
gagik 9ab5ebb
feat: add some tests
gagik 20e985e
chore: refactor to use atlas service and other feedback changes
gagik 354afd6
chore: fix check
gagik ac6fb52
chore: fix dependencies
gagik f5436f2
chore: remove eslint disables, fix types on plugin props
gagik 41cb841
chore: add compass-assistant dependency
gagik dbdee46
chore: simplify endpoint resolution
gagik 6212cb1
chore: move to dev dependency
gagik File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| ignores: | ||
| - '@mongodb-js/prettier-config-compass' | ||
| - '@mongodb-js/tsconfig-compass' | ||
| - '@types/chai' | ||
| - '@types/sinon-chai' | ||
| - 'sinon' | ||
| - '@types/chai-dom' | ||
| - '@types/react' | ||
| - '@types/react-dom' | ||
| ignore-patterns: | ||
| - 'dist' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| .nyc-output | ||
| dist |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| module.exports = { | ||
| root: true, | ||
| extends: ['@mongodb-js/eslint-config-compass/plugin'], | ||
| parserOptions: { | ||
| tsconfigRootDir: __dirname, | ||
| project: ['./tsconfig-lint.json'], | ||
| }, | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| module.exports = require('@mongodb-js/mocha-config-compass/compass-plugin'); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| { | ||
| "name": "@mongodb-js/compass-assistant", | ||
| "description": "Compass plugin for the AI Assistant", | ||
| "author": { | ||
| "name": "MongoDB Inc", | ||
| "email": "[email protected]" | ||
| }, | ||
| "private": true, | ||
| "bugs": { | ||
| "url": "https://jira.mongodb.org/projects/COMPASS/issues", | ||
| "email": "[email protected]" | ||
| }, | ||
| "homepage": "https://github.com/mongodb-js/compass", | ||
| "version": "1.0.0", | ||
| "repository": { | ||
| "type": "git", | ||
| "url": "https://github.com/mongodb-js/compass.git" | ||
| }, | ||
| "files": [ | ||
| "dist" | ||
| ], | ||
| "license": "SSPL", | ||
| "main": "dist/index.js", | ||
| "compass:main": "src/index.tsx", | ||
| "exports": { | ||
| "import": "./dist/.esm-wrapper.mjs", | ||
| "require": "./dist/index.js" | ||
| }, | ||
| "compass:exports": { | ||
| ".": "./src/index.tsx" | ||
| }, | ||
| "types": "./dist/index.d.ts", | ||
| "scripts": { | ||
| "bootstrap": "npm run compile", | ||
| "compile": "tsc -p tsconfig.json", | ||
| "typecheck": "tsc -p tsconfig-lint.json --noEmit", | ||
| "eslint": "eslint-compass", | ||
| "prettier": "prettier-compass", | ||
| "lint": "npm run eslint . && npm run prettier -- --check .", | ||
| "depcheck": "compass-scripts check-peer-deps && depcheck", | ||
| "check": "npm run typecheck && npm run lint && npm run depcheck", | ||
| "check-ci": "npm run check", | ||
| "test": "mocha", | ||
| "test-electron": "xvfb-maybe electron-mocha --no-sandbox", | ||
| "test-cov": "nyc --compact=false --produce-source-map=false -x \"**/*.spec.*\" --reporter=lcov --reporter=text --reporter=html npm run test", | ||
| "test-watch": "npm run test -- --watch", | ||
| "test-ci": "npm run test-cov", | ||
| "test-ci-electron": "npm run test-electron", | ||
| "reformat": "npm run eslint . -- --fix && npm run prettier -- --write ." | ||
| }, | ||
| "dependencies": { | ||
| "@ai-sdk/openai": "^2.0.4", | ||
| "@mongodb-js/compass-components": "^1.30.5", | ||
| "@mongodb-js/compass-logging": "^1.7.10", | ||
| "@mongodb-js/compass-app-registry": "^9.4.18", | ||
| "ai": "^5.0.5", | ||
| "compass-preferences-model": "^2.49.0", | ||
| "react": "^17.0.2", | ||
| "use-sync-external-store": "^1.5.0" | ||
| }, | ||
| "devDependencies": { | ||
| "react-dom": "^17.0.2", | ||
| "throttleit": "^2.1.0", | ||
| "@mongodb-js/eslint-config-compass": "^1.4.5", | ||
| "@mongodb-js/mocha-config-compass": "^1.7.0", | ||
| "@mongodb-js/prettier-config-compass": "^1.2.8", | ||
| "@mongodb-js/testing-library-compass": "^1.3.8", | ||
| "@mongodb-js/tsconfig-compass": "^1.2.9", | ||
| "@types/chai": "^4.2.21", | ||
| "@types/chai-dom": "^0.0.10", | ||
| "@types/mocha": "^9.0.0", | ||
| "@types/react": "^17.0.5", | ||
| "@types/react-dom": "^17.0.10", | ||
| "@types/sinon-chai": "^3.2.5", | ||
| "chai": "^4.3.6", | ||
| "depcheck": "^1.4.1", | ||
| "mocha": "^10.2.0", | ||
| "nyc": "^15.1.0", | ||
| "sinon": "^17.0.1", | ||
| "typescript": "^5.8.3", | ||
| "xvfb-maybe": "^0.2.1" | ||
| }, | ||
| "is_compass_plugin": true | ||
| } | ||
149 changes: 149 additions & 0 deletions
149
packages/compass-assistant/src/@ai-sdk/react/chat.react.ts
gagik marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,149 @@ | ||
| // Copyright 2023 Vercel, Inc. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
| import { | ||
| AbstractChat, | ||
| type ChatInit, | ||
| type ChatState, | ||
| type ChatStatus, | ||
| type UIMessage, | ||
| } from 'ai'; | ||
| import { throttle } from './throttle'; | ||
|
|
||
| class ReactChatState<UI_MESSAGE extends UIMessage> | ||
| implements ChatState<UI_MESSAGE> | ||
| { | ||
| #messages: UI_MESSAGE[]; | ||
| #status: ChatStatus = 'ready'; | ||
| #error: Error | undefined = undefined; | ||
|
|
||
| #messagesCallbacks = new Set<() => void>(); | ||
| #statusCallbacks = new Set<() => void>(); | ||
| #errorCallbacks = new Set<() => void>(); | ||
|
|
||
| constructor(initialMessages: UI_MESSAGE[] = []) { | ||
| this.#messages = initialMessages; | ||
| } | ||
|
|
||
| get status(): ChatStatus { | ||
| return this.#status; | ||
| } | ||
|
|
||
| set status(newStatus: ChatStatus) { | ||
| this.#status = newStatus; | ||
| this.#callStatusCallbacks(); | ||
| } | ||
|
|
||
| get error(): Error | undefined { | ||
| return this.#error; | ||
| } | ||
|
|
||
| set error(newError: Error | undefined) { | ||
| this.#error = newError; | ||
| this.#callErrorCallbacks(); | ||
| } | ||
|
|
||
| get messages(): UI_MESSAGE[] { | ||
| return this.#messages; | ||
| } | ||
|
|
||
| set messages(newMessages: UI_MESSAGE[]) { | ||
| this.#messages = [...newMessages]; | ||
| this.#callMessagesCallbacks(); | ||
| } | ||
|
|
||
| pushMessage = (message: UI_MESSAGE) => { | ||
| this.#messages = this.#messages.concat(message); | ||
| this.#callMessagesCallbacks(); | ||
| }; | ||
|
|
||
| popMessage = () => { | ||
| this.#messages = this.#messages.slice(0, -1); | ||
| this.#callMessagesCallbacks(); | ||
| }; | ||
|
|
||
| replaceMessage = (index: number, message: UI_MESSAGE) => { | ||
| this.#messages = [ | ||
| ...this.#messages.slice(0, index), | ||
| // We deep clone the message here to ensure the new React Compiler (currently in RC) detects deeply nested parts/metadata changes: | ||
| this.snapshot(message), | ||
| ...this.#messages.slice(index + 1), | ||
| ]; | ||
| this.#callMessagesCallbacks(); | ||
| }; | ||
|
|
||
| snapshot = <T>(value: T): T => structuredClone(value); | ||
|
|
||
| '~registerMessagesCallback' = ( | ||
| onChange: () => void, | ||
| throttleWaitMs?: number | ||
| ): (() => void) => { | ||
| const callback = throttleWaitMs | ||
| ? throttle(onChange, throttleWaitMs) | ||
| : onChange; | ||
| this.#messagesCallbacks.add(callback); | ||
| return () => { | ||
| this.#messagesCallbacks.delete(callback); | ||
| }; | ||
| }; | ||
|
|
||
| '~registerStatusCallback' = (onChange: () => void): (() => void) => { | ||
| this.#statusCallbacks.add(onChange); | ||
| return () => { | ||
| this.#statusCallbacks.delete(onChange); | ||
| }; | ||
| }; | ||
|
|
||
| '~registerErrorCallback' = (onChange: () => void): (() => void) => { | ||
| this.#errorCallbacks.add(onChange); | ||
| return () => { | ||
| this.#errorCallbacks.delete(onChange); | ||
| }; | ||
| }; | ||
|
|
||
| #callMessagesCallbacks = () => { | ||
| this.#messagesCallbacks.forEach((callback) => callback()); | ||
| }; | ||
|
|
||
| #callStatusCallbacks = () => { | ||
| this.#statusCallbacks.forEach((callback) => callback()); | ||
| }; | ||
|
|
||
| #callErrorCallbacks = () => { | ||
| this.#errorCallbacks.forEach((callback) => callback()); | ||
| }; | ||
| } | ||
|
|
||
| export class Chat< | ||
| UI_MESSAGE extends UIMessage | ||
| > extends AbstractChat<UI_MESSAGE> { | ||
| #state: ReactChatState<UI_MESSAGE>; | ||
|
|
||
| constructor({ messages, ...init }: ChatInit<UI_MESSAGE>) { | ||
| const state = new ReactChatState(messages); | ||
| super({ ...init, state }); | ||
| this.#state = state; | ||
| } | ||
|
|
||
| '~registerMessagesCallback' = ( | ||
| onChange: () => void, | ||
| throttleWaitMs?: number | ||
| ): (() => void) => | ||
| this.#state['~registerMessagesCallback'](onChange, throttleWaitMs); | ||
|
|
||
| '~registerStatusCallback' = (onChange: () => void): (() => void) => | ||
| this.#state['~registerStatusCallback'](onChange); | ||
|
|
||
| '~registerErrorCallback' = (onChange: () => void): (() => void) => | ||
| this.#state['~registerErrorCallback'](onChange); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| // Copyright 2023 Vercel, Inc. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
| import throttleFunction from 'throttleit'; | ||
|
|
||
| // eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
| export function throttle<T extends (...args: any[]) => any>( | ||
| fn: T, | ||
| waitMs: number | undefined | ||
| ): T { | ||
| return waitMs !== undefined ? throttleFunction(fn, waitMs) : fn; | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
depcheck forced me to move this throttleit to dev dependencies... not sure how right that is, could add that to depcheckrc ignores
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmmm, it shouldn't if the dependency is used in the source code, can you share the error you're seeing?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe
@in the filepath trips up the check, maybe worth removing it from the name and trying againThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
didn't help, very weird..
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tried it locally, it's not depcheck, it's our check-peer-deps script, seems like something is tripping up the module resolution logic there so it doesn't pick up the
./chat.reactfile for parsing (and so misses the throttleit import). Looking