Skip to content

Commit

Permalink
Adds a watch script to make it easier to develop and iterate on the p…
Browse files Browse the repository at this point in the history
…lugin
  • Loading branch information
hosein1984 committed Nov 20, 2023
1 parent 9d6d6cd commit 3130c23
Show file tree
Hide file tree
Showing 3 changed files with 538 additions and 10 deletions.
8 changes: 6 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@
"e2e:update": "yarn cypress install && yarn grafana-e2e run --update-screenshots",
"server": "docker-compose up --build",
"sign": "npx --yes @grafana/sign-plugin",
"copy": "docker cp ./dist portfolio-grafana-grafana-1:/var/lib/grafana-plugins/chatbot-panel",
"pack": "npm-run-all build copy"
"copy-to-container": "docker cp ./dist portfolio-grafana-grafana-1:/var/lib/grafana-plugins/chatbot-panel",
"restart-container": "docker restart portfolio-grafana-grafana-1",
"local-deploy": "npm-run-all build copy-to-container restart-container",
"watch-dist": "nodemon --watch ./dist --exec \"yarn run copy-to-container && yarn run restart-container\""
},
"author": "Target Energy Solutions",
"license": "Apache-2.0",
Expand Down Expand Up @@ -51,6 +53,8 @@
"identity-obj-proxy": "3.0.0",
"jest": "^29.3.1",
"jest-environment-jsdom": "^29.3.1",
"nodemon": "^3.0.1",
"npm-run-all": "^4.1.5",
"prettier": "^2.5.0",
"replace-in-file-webpack-plugin": "^1.0.6",
"sass": "1.56.1",
Expand Down
40 changes: 37 additions & 3 deletions src/components/dso-chat-bot/DsoChatbot.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,46 @@ export const DsoChatBot = () => {
method: 'POST',
headers: {
'Content-Type': 'application/json',
mode: 'no-cors',
},
body: JSON.stringify({ messages: messages, functions: [] }),
}
const response = await fetch(url, options)
console.log('response:::::::::', response)

try {
const response = await fetch(url, options)
if (!response.ok) {
console.log('Request to the generate endpoint failed')
return
}
const reader = response.body!.getReader()
const decoder = new TextDecoder('utf-8')
let resultText = ''

while (true) {
const { done, value } = await reader.read()
if (done) {
break
}
// Massage and parse the chunk of data
const chunk = decoder.decode(value)
const lines = chunk.split('\\n')
const parsedLines = lines
.map((line) => line.replace(/^data: /, '').trim()) // Remove the "data: " prefix
.filter((line) => line !== '' && line !== '[DONE]') // Remove empty lines and "[DONE]"
.map((line) => JSON.parse(line)) // Parse the JSON string

for (const parsedLine of parsedLines) {
const { choices } = parsedLine
const { delta } = choices[0]
const { content } = delta
// Update the UI with the new content
if (content) {
resultText += content
}
}
}

console.log('Generate response:', resultText)
} catch (err) {}
}, [addMessageToChatContent, chatContent, text])

/** Callbacks */
Expand Down
Loading

0 comments on commit 3130c23

Please sign in to comment.