chore: import upstream snapshot with attribution
This commit is contained in:
@@ -0,0 +1,44 @@
|
||||
import { buildChart, ChartConfig } from './utils'
|
||||
import * as bp from '.botpress'
|
||||
|
||||
export const generateBarChart: bp.IntegrationProps['actions']['generateBarChart'] = async ({
|
||||
client,
|
||||
input,
|
||||
logger,
|
||||
type,
|
||||
ctx,
|
||||
}) => {
|
||||
logger.forBot().debug('Generating bar chart', { input, type })
|
||||
|
||||
const chartConfig: ChartConfig = {
|
||||
type: 'bar',
|
||||
data: {
|
||||
labels: input.xData,
|
||||
datasets: [
|
||||
{
|
||||
label: input.title || 'Bar Chart',
|
||||
data: input.yData!,
|
||||
},
|
||||
],
|
||||
},
|
||||
options: {
|
||||
scales: {
|
||||
x: {
|
||||
title: {
|
||||
display: true,
|
||||
text: input.xAxisTitle || 'X Axis',
|
||||
},
|
||||
},
|
||||
y: {
|
||||
title: {
|
||||
display: true,
|
||||
text: input.yAxisTitle || 'Y Axis',
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
const imageUrl = await buildChart({ chartConfig, botId: ctx.botId, client, fileName: 'bar_chart' })
|
||||
return { imageUrl }
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
import { buildChart, ChartConfig } from './utils'
|
||||
import * as bp from '.botpress'
|
||||
|
||||
export const generateBubbleChart: bp.IntegrationProps['actions']['generateBubbleChart'] = async ({
|
||||
client,
|
||||
input,
|
||||
logger,
|
||||
type,
|
||||
ctx,
|
||||
}) => {
|
||||
logger.forBot().debug('Generating bubble chart', { input, type })
|
||||
|
||||
const chartConfig: ChartConfig = {
|
||||
type: 'bubble',
|
||||
data: {
|
||||
datasets: [
|
||||
{
|
||||
label: input.title || 'Bubble Chart',
|
||||
data: input.data!,
|
||||
},
|
||||
],
|
||||
},
|
||||
options: {
|
||||
scales: {
|
||||
x: {
|
||||
title: {
|
||||
display: true,
|
||||
text: input.xAxisTitle || 'X Axis',
|
||||
},
|
||||
},
|
||||
y: {
|
||||
title: {
|
||||
display: true,
|
||||
text: input.yAxisTitle || 'Y Axis',
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
const imageUrl = await buildChart({ chartConfig, botId: ctx.botId, client, fileName: 'bubble_chart' })
|
||||
return { imageUrl }
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
import { buildChart, ChartConfig } from './utils'
|
||||
import * as bp from '.botpress'
|
||||
|
||||
export const generateDoughnutChart: bp.IntegrationProps['actions']['generateDoughnutChart'] = async ({
|
||||
client,
|
||||
input,
|
||||
logger,
|
||||
type,
|
||||
ctx,
|
||||
}) => {
|
||||
logger.forBot().debug('Generating doughnut chart', { input, type })
|
||||
|
||||
const chartConfig: ChartConfig = {
|
||||
type: 'doughnut',
|
||||
data: {
|
||||
labels: input.labels,
|
||||
datasets: [
|
||||
{
|
||||
label: input.title || 'Doughnut Chart',
|
||||
data: input.data!,
|
||||
},
|
||||
],
|
||||
},
|
||||
}
|
||||
|
||||
const imageUrl = await buildChart({ chartConfig, botId: ctx.botId, client, fileName: 'doughnut_chart' })
|
||||
return { imageUrl }
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
import { buildChart, ChartConfig } from './utils'
|
||||
import * as bp from '.botpress'
|
||||
|
||||
export const generateHorizontalBarChart: bp.IntegrationProps['actions']['generateHorizontalBarChart'] = async ({
|
||||
client,
|
||||
input,
|
||||
logger,
|
||||
type,
|
||||
ctx,
|
||||
}) => {
|
||||
logger.forBot().debug('Generating horizontal bar chart', { input, type })
|
||||
|
||||
const chartConfig: ChartConfig = {
|
||||
type: 'bar',
|
||||
data: {
|
||||
labels: input.xData,
|
||||
datasets: [
|
||||
{
|
||||
label: input.title || 'Horizontal Bar Chart',
|
||||
data: input.yData!,
|
||||
},
|
||||
],
|
||||
},
|
||||
options: {
|
||||
indexAxis: 'y',
|
||||
scales: {
|
||||
x: {
|
||||
title: {
|
||||
display: true,
|
||||
text: input.xAxisTitle || 'X Axis',
|
||||
},
|
||||
},
|
||||
y: {
|
||||
title: {
|
||||
display: true,
|
||||
text: input.yAxisTitle || 'Y Axis',
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
const imageUrl = await buildChart({ chartConfig, botId: ctx.botId, client, fileName: 'horizontal_bar_chart' })
|
||||
return { imageUrl }
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
import { buildChart, ChartConfig } from './utils'
|
||||
import * as bp from '.botpress'
|
||||
|
||||
export const generateLinePlot: bp.IntegrationProps['actions']['generateLinePlot'] = async ({
|
||||
client,
|
||||
input,
|
||||
logger,
|
||||
type,
|
||||
ctx,
|
||||
}) => {
|
||||
logger.forBot().debug('Generating line plot', { input, type })
|
||||
|
||||
const chartConfig: ChartConfig = {
|
||||
type: 'line',
|
||||
data: {
|
||||
labels: input.xData,
|
||||
datasets: [
|
||||
{
|
||||
label: input.title || 'Line Plot',
|
||||
data: input.yData!,
|
||||
},
|
||||
],
|
||||
},
|
||||
options: {
|
||||
scales: {
|
||||
x: {
|
||||
title: {
|
||||
display: true,
|
||||
text: input.xAxisTitle || 'X Axis',
|
||||
},
|
||||
},
|
||||
y: {
|
||||
title: {
|
||||
display: true,
|
||||
text: input.yAxisTitle || 'Y Axis',
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
const imageUrl = await buildChart({ chartConfig, botId: ctx.botId, client, fileName: 'line_plot' })
|
||||
return { imageUrl }
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
import { buildChart, ChartConfig } from './utils'
|
||||
import * as bp from '.botpress'
|
||||
|
||||
export const generatePieChart: bp.IntegrationProps['actions']['generatePieChart'] = async ({
|
||||
client,
|
||||
input,
|
||||
logger,
|
||||
type,
|
||||
ctx,
|
||||
}) => {
|
||||
logger.forBot().debug('Generating pie chart', { input, type })
|
||||
|
||||
const chartConfig: ChartConfig = {
|
||||
type: 'pie',
|
||||
data: {
|
||||
labels: input.labels,
|
||||
datasets: [
|
||||
{
|
||||
label: input.title || 'Pie Chart',
|
||||
data: input.data!,
|
||||
},
|
||||
],
|
||||
},
|
||||
}
|
||||
|
||||
const imageUrl = await buildChart({ chartConfig, botId: ctx.botId, client, fileName: 'pie_chart' })
|
||||
return { imageUrl }
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
import { buildChart, ChartConfig } from './utils'
|
||||
import * as bp from '.botpress'
|
||||
|
||||
export const generateRadarChart: bp.IntegrationProps['actions']['generateRadarChart'] = async ({
|
||||
client,
|
||||
input,
|
||||
logger,
|
||||
type,
|
||||
ctx,
|
||||
}) => {
|
||||
logger.forBot().debug('Generating radar chart', { input, type })
|
||||
|
||||
const chartConfig: ChartConfig = {
|
||||
type: 'radar',
|
||||
data: {
|
||||
labels: input.labels,
|
||||
datasets: [
|
||||
{
|
||||
label: input.title || 'Radar Chart',
|
||||
data: input.data!,
|
||||
},
|
||||
],
|
||||
},
|
||||
options: {
|
||||
scales: {
|
||||
r: {
|
||||
title: {
|
||||
display: true,
|
||||
text: input.axisTitle || 'Axis',
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
const imageUrl = await buildChart({ chartConfig, botId: ctx.botId, client, fileName: 'radar_chart' })
|
||||
return { imageUrl }
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
import { buildChart, ChartConfig } from './utils'
|
||||
import * as bp from '.botpress'
|
||||
|
||||
export const generateScatterPlot: bp.IntegrationProps['actions']['generateScatterPlot'] = async ({
|
||||
client,
|
||||
input,
|
||||
logger,
|
||||
type,
|
||||
ctx,
|
||||
}) => {
|
||||
logger.forBot().debug('Generating scatter plot', { input, type })
|
||||
|
||||
const chartConfig: ChartConfig = {
|
||||
type: 'scatter',
|
||||
data: {
|
||||
datasets: [
|
||||
{
|
||||
label: input.title || 'Scatter Plot',
|
||||
data: input.data!,
|
||||
},
|
||||
],
|
||||
},
|
||||
options: {
|
||||
scales: {
|
||||
x: {
|
||||
title: {
|
||||
display: true,
|
||||
text: input.xAxisTitle || 'X Axis',
|
||||
},
|
||||
},
|
||||
y: {
|
||||
title: {
|
||||
display: true,
|
||||
text: input.yAxisTitle || 'Y Axis',
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
const imageUrl = await buildChart({ chartConfig, botId: ctx.botId, client, fileName: 'scatter_plot' })
|
||||
return { imageUrl }
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
import { generateBarChart } from './generate-bar-chart'
|
||||
import { generateBubbleChart } from './generate-bubble-chart'
|
||||
import { generateDoughnutChart } from './generate-doughnut-chart'
|
||||
import { generateHorizontalBarChart } from './generate-horizontal-bar-chart'
|
||||
import { generateLinePlot } from './generate-line-plot'
|
||||
import { generatePieChart } from './generate-pie-chart'
|
||||
import { generateRadarChart } from './generate-radar-chart'
|
||||
import { generateScatterPlot } from './generate-scatter-plot'
|
||||
|
||||
export const actionImplementations = {
|
||||
generateLinePlot,
|
||||
generateBarChart,
|
||||
generatePieChart,
|
||||
generateScatterPlot,
|
||||
generateBubbleChart,
|
||||
generateDoughnutChart,
|
||||
generateRadarChart,
|
||||
generateHorizontalBarChart,
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
import axios from 'axios'
|
||||
import type { ChartConfiguration } from 'chart.js' // do not import the implementation, only the types
|
||||
import * as bp from '.botpress'
|
||||
|
||||
export type ChartConfig = ChartConfiguration
|
||||
export type BuildChartProps = {
|
||||
chartConfig: ChartConfig
|
||||
client: bp.Client
|
||||
botId: string
|
||||
fileName: string
|
||||
}
|
||||
export const buildChart = async (props: BuildChartProps) => {
|
||||
try {
|
||||
const response = await axios.get('https://quickchart.io/chart', {
|
||||
params: {
|
||||
key: bp.secrets.QUICKCHARTS_API_KEY,
|
||||
c: JSON.stringify(props.chartConfig),
|
||||
},
|
||||
responseType: 'arraybuffer',
|
||||
})
|
||||
|
||||
const { file } = await props.client.uploadFile({
|
||||
key: `${props.fileName}_${Date.now()}.png`,
|
||||
content: response.data,
|
||||
index: false,
|
||||
accessPolicies: ['public_content'],
|
||||
publicContentImmediatelyAccessible: true,
|
||||
})
|
||||
|
||||
return file.url!
|
||||
} catch (err) {
|
||||
console.error(`Error generating ${props.fileName}:`, err)
|
||||
throw err
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,227 @@
|
||||
import { z } from '@botpress/sdk'
|
||||
|
||||
const generateLinePlot = {
|
||||
title: 'Line Plot',
|
||||
description: 'Generate a line plot',
|
||||
input: {
|
||||
schema: z.object({
|
||||
xData: z
|
||||
.array(z.string().or(z.number()))
|
||||
.catch(() => [1, 2, 3, 4, 5])
|
||||
.describe('The data for the x axis')
|
||||
.title('X Data'),
|
||||
yData: z
|
||||
.array(z.number())
|
||||
.catch(() => [1, 2, 3, 4, 5])
|
||||
.describe('the data for the y axis')
|
||||
.title('Y Data'),
|
||||
title: z.string().optional().describe('The title of the plot').title('Line Plot Title'),
|
||||
xAxisTitle: z.string().optional().describe('The title of the x axis').title('X Axis Title'),
|
||||
yAxisTitle: z.string().optional().describe('The title of the y axis').title('Y Axis Title'),
|
||||
}),
|
||||
},
|
||||
output: {
|
||||
schema: z.object({
|
||||
imageUrl: z.string().describe('The url of the generated image').title('Image Url'),
|
||||
}),
|
||||
},
|
||||
}
|
||||
|
||||
const generateBarChart = {
|
||||
title: 'Bar Chart',
|
||||
description: 'Generate a Bar chart',
|
||||
input: {
|
||||
schema: z.object({
|
||||
xData: z
|
||||
.array(z.string().or(z.number()))
|
||||
.catch(() => [1, 2, 3, 4, 5])
|
||||
.describe('The data for the x axis')
|
||||
.title('X Data'),
|
||||
yData: z
|
||||
.array(z.number())
|
||||
.catch(() => [1, 2, 3, 4, 5])
|
||||
.describe('The data for the y axis')
|
||||
.title('Y Data'),
|
||||
title: z.string().optional().describe('The title of the Bar Chart').title('Bar Chart Title'),
|
||||
xAxisTitle: z.string().optional().describe('The title of the x axis').title('X Axis Title'),
|
||||
yAxisTitle: z.string().optional().describe('The title of the y axis').title('Y Axis Title'),
|
||||
}),
|
||||
},
|
||||
output: {
|
||||
schema: z.object({
|
||||
imageUrl: z.string().describe('The url of the generated image').title('Image Url'),
|
||||
}),
|
||||
},
|
||||
}
|
||||
|
||||
const generatePieChart = {
|
||||
title: 'Pie Chart',
|
||||
description: 'Generate a pie chart',
|
||||
input: {
|
||||
schema: z.object({
|
||||
labels: z
|
||||
.array(z.string())
|
||||
.catch(() => ['Label 1', 'Label 2', 'Label 3'])
|
||||
.describe('The labels for the data')
|
||||
.title('Labels'),
|
||||
data: z
|
||||
.array(z.number())
|
||||
.catch(() => [10, 20, 30])
|
||||
.describe('The data to plot')
|
||||
.title('Data'),
|
||||
title: z.string().optional().describe('The title of the pie chart').title('Pie Chart Title'),
|
||||
}),
|
||||
},
|
||||
output: {
|
||||
schema: z.object({
|
||||
imageUrl: z.string().describe('The url of the generated image').title('Image Url'),
|
||||
}),
|
||||
},
|
||||
}
|
||||
|
||||
export const generateScatterPlot = {
|
||||
title: 'Scatter Plot',
|
||||
description: 'Generate a scatter plot',
|
||||
input: {
|
||||
schema: z.object({
|
||||
data: z
|
||||
.array(z.object({ x: z.number(), y: z.number() }))
|
||||
.catch(() => [
|
||||
{ x: 1, y: 2 },
|
||||
{ x: 2, y: 3 },
|
||||
{ x: 3, y: 4 },
|
||||
])
|
||||
.describe('The data to plot')
|
||||
.title('Data'),
|
||||
title: z.string().optional().describe('The title of the scatter plot').title('Scatter Plot Title'),
|
||||
xAxisTitle: z.string().optional().describe('The title of the x axis').title('X Axis Title'),
|
||||
yAxisTitle: z.string().optional().describe('The title of the y axis').title('Y Axis Title'),
|
||||
}),
|
||||
},
|
||||
output: {
|
||||
schema: z.object({
|
||||
imageUrl: z.string().describe('The url of the generated image').title('Image Url'),
|
||||
}),
|
||||
},
|
||||
}
|
||||
|
||||
const generateDoughnutChart = {
|
||||
title: 'Doughnut Chart',
|
||||
description: 'Generate a Doughnut Chart',
|
||||
input: {
|
||||
schema: z.object({
|
||||
labels: z
|
||||
.array(z.string())
|
||||
.catch(() => ['Label 1', 'Label 2', 'Label 3'])
|
||||
.describe('The labels for the data')
|
||||
.title('Labels'),
|
||||
data: z
|
||||
.array(z.number())
|
||||
.catch(() => [10, 20, 30])
|
||||
.describe('The data to plot')
|
||||
.title('Data'),
|
||||
title: z.string().optional().describe('The title of the doughnut chart').title('Doughnut Chart Title'),
|
||||
}),
|
||||
},
|
||||
output: {
|
||||
schema: z.object({
|
||||
imageUrl: z.string().describe('The url of the generated image').title('Image Url'),
|
||||
}),
|
||||
},
|
||||
}
|
||||
|
||||
const generateRadarChart = {
|
||||
title: 'Radar Chart',
|
||||
description: 'Generate a radar Chart',
|
||||
input: {
|
||||
schema: z.object({
|
||||
labels: z
|
||||
.array(z.string())
|
||||
.catch(() => ['Label 1', 'Label 2', 'Label 3'])
|
||||
.describe('The labels for the data')
|
||||
.title('Labels'),
|
||||
data: z
|
||||
.array(z.number())
|
||||
.catch(() => [10, 20, 30])
|
||||
.describe('The data to plot')
|
||||
.title('Data'),
|
||||
title: z.string().optional().describe('The title of the radar chart').title('Radar Chart Title'),
|
||||
axisTitle: z.string().optional().describe('The title of the axis').title('Axis Title'),
|
||||
}),
|
||||
},
|
||||
output: {
|
||||
schema: z.object({
|
||||
imageUrl: z.string().describe('The url of the generated image').title('Image Url'),
|
||||
}),
|
||||
},
|
||||
}
|
||||
|
||||
const generateBubbleChart = {
|
||||
title: 'Bubble Chart',
|
||||
description: 'Generate a bubble Chart',
|
||||
input: {
|
||||
schema: z.object({
|
||||
data: z
|
||||
.array(
|
||||
z.object({
|
||||
x: z.number(),
|
||||
y: z.number(),
|
||||
r: z.number(),
|
||||
})
|
||||
)
|
||||
.catch(() => [
|
||||
{ x: 1, y: 2, r: 5 },
|
||||
{ x: 2, y: 3, r: 10 },
|
||||
{ x: 3, y: 4, r: 15 },
|
||||
])
|
||||
.describe('The data to plot')
|
||||
.title('Data'),
|
||||
title: z.string().optional().describe('The title of the bubble chart').title('Bubble Chart Title'),
|
||||
xAxisTitle: z.string().optional().describe('The title of the x axis').title('X Axis Title'),
|
||||
yAxisTitle: z.string().optional().describe('The title of the y axis').title('Y Axis Title'),
|
||||
}),
|
||||
},
|
||||
output: {
|
||||
schema: z.object({
|
||||
imageUrl: z.string().describe('The url of the generated image').title('Image Url'),
|
||||
}),
|
||||
},
|
||||
}
|
||||
|
||||
const generateHorizontalBarChart = {
|
||||
title: 'Horizontal Bar Chart',
|
||||
description: 'Generate a horizontal Chart',
|
||||
input: {
|
||||
schema: z.object({
|
||||
xData: z
|
||||
.array(z.string().or(z.number()))
|
||||
.catch(() => [1, 2, 3, 4, 5])
|
||||
.describe('The data for the x axis')
|
||||
.title('X Data'),
|
||||
yData: z
|
||||
.array(z.number())
|
||||
.catch(() => [1, 2, 3, 4, 5])
|
||||
.describe('The data for the y axis')
|
||||
.title('Y Data'),
|
||||
title: z.string().optional().describe('The title of the bar chart').title('Bar Chart Title'),
|
||||
xAxisTitle: z.string().optional().describe('The title of the x axis').title('X Axis Title'),
|
||||
yAxisTitle: z.string().optional().describe('The title of the y axis').title('Y Axis Title'),
|
||||
}),
|
||||
},
|
||||
output: {
|
||||
schema: z.object({
|
||||
imageUrl: z.string().describe('The url of the generated image').title('Image Url'),
|
||||
}),
|
||||
},
|
||||
}
|
||||
|
||||
export const actionDefinitions = {
|
||||
generateLinePlot,
|
||||
generateBarChart,
|
||||
generatePieChart,
|
||||
generateScatterPlot,
|
||||
generateDoughnutChart,
|
||||
generateRadarChart,
|
||||
generateBubbleChart,
|
||||
generateHorizontalBarChart,
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
import { actionImplementations } from './actions'
|
||||
|
||||
import * as bp from '.botpress'
|
||||
|
||||
export default new bp.Integration({
|
||||
register: async () => {},
|
||||
unregister: async () => {},
|
||||
actions: actionImplementations,
|
||||
channels: {},
|
||||
handler: async () => {},
|
||||
})
|
||||
Reference in New Issue
Block a user