From 541f7ea42e1b283033f585cbd198fb763887f0c7 Mon Sep 17 00:00:00 2001 From: Wassim Chegham Date: Tue, 16 Apr 2024 17:00:37 +0200 Subject: [PATCH] migrate openai-asst-webpage-js to vite/esm --- .../.x/templates/openai-asst-webpage-js/.env | 2 +- .../{ai.png => favicon.png} | Bin .../openai-asst-webpage-js/index.html | 11 ++++---- .../openai-asst-webpage-js/package.json | 10 +++---- .../src/OpenAIAssistantsStreamingClass.js | 4 +-- .../openai-asst-webpage-js/src/script.js | 16 +++++------ .../openai-asst-webpage-js/vite.config.js | 3 +++ .../openai-asst-webpage-js/webpack.config.js | 25 ------------------ 8 files changed, 23 insertions(+), 48 deletions(-) rename src/ai/.x/templates/openai-asst-webpage-js/{ai.png => favicon.png} (100%) create mode 100644 src/ai/.x/templates/openai-asst-webpage-js/vite.config.js delete mode 100644 src/ai/.x/templates/openai-asst-webpage-js/webpack.config.js diff --git a/src/ai/.x/templates/openai-asst-webpage-js/.env b/src/ai/.x/templates/openai-asst-webpage-js/.env index b710b25e..80db87f2 100644 --- a/src/ai/.x/templates/openai-asst-webpage-js/.env +++ b/src/ai/.x/templates/openai-asst-webpage-js/.env @@ -15,4 +15,4 @@ AZURE_OPENAI_ENDPOINT=<#= AZURE_OPENAI_ENDPOINT #> AZURE_OPENAI_CHAT_DEPLOYMENT=<#= AZURE_OPENAI_CHAT_DEPLOYMENT #> OPENAI_API_KEY=<#= OPENAI_API_KEY #> OPENAI_ORG_ID=<#= OPENAI_ORG_ID #> -OPENAI_MODEL_NAME=<#= OPENAI_MODEL_NAME #> +OPENAI_MODEL_NAME=<#= OPENAI_MODEL_NAME #> \ No newline at end of file diff --git a/src/ai/.x/templates/openai-asst-webpage-js/ai.png b/src/ai/.x/templates/openai-asst-webpage-js/favicon.png similarity index 100% rename from src/ai/.x/templates/openai-asst-webpage-js/ai.png rename to src/ai/.x/templates/openai-asst-webpage-js/favicon.png diff --git a/src/ai/.x/templates/openai-asst-webpage-js/index.html b/src/ai/.x/templates/openai-asst-webpage-js/index.html index 994b6cbd..6287f6e7 100644 --- a/src/ai/.x/templates/openai-asst-webpage-js/index.html +++ b/src/ai/.x/templates/openai-asst-webpage-js/index.html @@ -2,6 +2,7 @@ + @@ -16,8 +17,8 @@
- @@ -41,7 +42,7 @@

chat.contoso.com

- +
@@ -53,7 +54,7 @@

chat.contoso.com

-
@@ -64,7 +65,7 @@

chat.contoso.com

- + \ No newline at end of file diff --git a/src/ai/.x/templates/openai-asst-webpage-js/package.json b/src/ai/.x/templates/openai-asst-webpage-js/package.json index 3051bcea..18a7a9f6 100644 --- a/src/ai/.x/templates/openai-asst-webpage-js/package.json +++ b/src/ai/.x/templates/openai-asst-webpage-js/package.json @@ -5,7 +5,9 @@ "main": "script.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1", - "webpack": "webpack" + "start": "vite", + "build": "vite build", + "preview": "vite preview" }, "author": "", "license": "MIT", @@ -16,8 +18,6 @@ }, "keywords": [], "devDependencies": { - "dotenv-webpack": "^7.0.3", - "webpack": "^5.89.0", - "webpack-cli": "^5.1.4" + "vite": "^5.2.8" } -} +} \ No newline at end of file diff --git a/src/ai/.x/templates/openai-asst-webpage-js/src/OpenAIAssistantsStreamingClass.js b/src/ai/.x/templates/openai-asst-webpage-js/src/OpenAIAssistantsStreamingClass.js index 6d387e85..f0b3f911 100644 --- a/src/ai/.x/templates/openai-asst-webpage-js/src/OpenAIAssistantsStreamingClass.js +++ b/src/ai/.x/templates/openai-asst-webpage-js/src/OpenAIAssistantsStreamingClass.js @@ -1,9 +1,7 @@ <#@ template hostspecific="true" #> <#@ output extension=".js" encoding="utf-8" #> <#@ parameter type="System.String" name="ClassName" #> -const { OpenAI } = require('openai'); - -class <#= ClassName #> { +export class <#= ClassName #> { // Constructor constructor(openAIAssistantId, openai, simulateTypingDelay = 0) { diff --git a/src/ai/.x/templates/openai-asst-webpage-js/src/script.js b/src/ai/.x/templates/openai-asst-webpage-js/src/script.js index aa8774a1..4691ef5f 100644 --- a/src/ai/.x/templates/openai-asst-webpage-js/src/script.js +++ b/src/ai/.x/templates/openai-asst-webpage-js/src/script.js @@ -9,11 +9,11 @@ <#@ parameter type="System.String" name="OPENAI_API_KEY" #> <#@ parameter type="System.String" name="OPENAI_ORG_ID" #> <#@ parameter type="System.String" name="OPENAI_MODEL_NAME" #> -const marked = require("marked"); -const hljs = require("highlight.js"); +import { setOptions, parse } from "marked"; +import * as hljs from "highlight.js"; -const { CreateOpenAI } = require("./CreateOpenAI"); -const { <#= ClassName #> } = require("./OpenAIAssistantsStreamingClass"); +import { CreateOpenAI } from "./CreateOpenAI"; +import { <#= ClassName #> } from "./OpenAIAssistantsStreamingClass"; // NOTE: Never deploy your key in client-side environments like browsers or mobile apps // SEE: https://help.openai.com/en/articles/5112595-best-practices-for-api-key-safety @@ -506,10 +506,8 @@ async function init() { userInputTextAreaFocus(); - window.newChat = newChat; - window.loadThread = loadThread; - window.sendMessage = sendMessage; - window.toggleTheme = toggleTheme; + document.querySelector('#sendButton').addEventListener('click', sendMessage); + document.querySelector('#newChatButton').addEventListener('click', newChat); } -init(); \ No newline at end of file +window.addEventListener('DOMContentLoaded', init); \ No newline at end of file diff --git a/src/ai/.x/templates/openai-asst-webpage-js/vite.config.js b/src/ai/.x/templates/openai-asst-webpage-js/vite.config.js new file mode 100644 index 00000000..0e35e81e --- /dev/null +++ b/src/ai/.x/templates/openai-asst-webpage-js/vite.config.js @@ -0,0 +1,3 @@ +export default { + envPrefix: ["ASSISTANT_ID", "AZURE_", "OPENAI_"], +}; \ No newline at end of file diff --git a/src/ai/.x/templates/openai-asst-webpage-js/webpack.config.js b/src/ai/.x/templates/openai-asst-webpage-js/webpack.config.js deleted file mode 100644 index 9ba185a5..00000000 --- a/src/ai/.x/templates/openai-asst-webpage-js/webpack.config.js +++ /dev/null @@ -1,25 +0,0 @@ -const path = require('path'); -const webpack = require('webpack'); -const Dotenv = require('dotenv-webpack'); - -module.exports = { - mode: 'development', - entry: './src/script.js', - output: { - filename: 'main.js', - path: path.resolve(__dirname, 'dist'), - }, - plugins: [ - new Dotenv(), - new webpack.DefinePlugin({ - 'process.env.ASSISTANT_ID': JSON.stringify(process.env.ASSISTANT_ID), - 'process.env.AZURE_OPENAI_API_KEY': JSON.stringify(process.env.AZURE_OPENAI_API_KEY), - 'process.env.AZURE_OPENAI_API_VERSION': JSON.stringify(process.env.AZURE_OPENAI_API_VERSION), - 'process.env.AZURE_OPENAI_ENDPOINT': JSON.stringify(process.env.AZURE_OPENAI_ENDPOINT), - 'process.env.AZURE_OPENAI_CHAT_DEPLOYMENT': JSON.stringify(process.env.AZURE_OPENAI_CHAT_DEPLOYMENT), - 'process.env.OPENAI_API_KEY': JSON.stringify(process.env.OPENAI_API_KEY), - 'process.env.OPENAI_ORG_ID': JSON.stringify(process.env.OPENAI_ORG_ID), - 'process.env.OPENAI_MODEL_NAME': JSON.stringify(process.env.OPENAI_MODEL_NAME), - }), - ], -}; \ No newline at end of file