mirror of
https://github.com/leon-ai/leon.git
synced 2024-12-18 06:11:34 +03:00
feat: final widget new structure
This commit is contained in:
parent
7167849d6a
commit
05200a195b
@ -9,9 +9,7 @@ import '@leon-ai/aurora/style.css'
|
|||||||
|
|
||||||
import { createRoot } from 'react-dom/client'
|
import { createRoot } from 'react-dom/client'
|
||||||
import { createElement } from 'react'
|
import { createElement } from 'react'
|
||||||
import parse from 'html-react-parser'
|
|
||||||
import * as auroraComponents from '@leon-ai/aurora'
|
import * as auroraComponents from '@leon-ai/aurora'
|
||||||
import { WidgetWrapper, Button } from '@leon-ai/aurora'
|
|
||||||
|
|
||||||
export default class Chatbot {
|
export default class Chatbot {
|
||||||
constructor() {
|
constructor() {
|
||||||
@ -112,36 +110,6 @@ export default class Chatbot {
|
|||||||
|
|
||||||
const root = createRoot(container)
|
const root = createRoot(container)
|
||||||
|
|
||||||
/* string = {
|
|
||||||
"component": "WidgetWrapper",
|
|
||||||
"props": {
|
|
||||||
"children": [
|
|
||||||
{
|
|
||||||
"component": "Button",
|
|
||||||
"props": {
|
|
||||||
"children": "Hello World"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
]
|
|
||||||
}
|
|
||||||
}*/
|
|
||||||
|
|
||||||
// render WidgetWrapper component
|
|
||||||
|
|
||||||
/*if (string.component === 'WidgetWrapper') {
|
|
||||||
const WidgetWrapperComponent = auroraComponents[string.component]
|
|
||||||
|
|
||||||
console.log('WidgetWrapperComponent', WidgetWrapperComponent)
|
|
||||||
|
|
||||||
console.log('string.props', string.props)
|
|
||||||
|
|
||||||
const widgetWrapper = createElement(WidgetWrapperComponent, string.props)
|
|
||||||
|
|
||||||
root.render(widgetWrapper)
|
|
||||||
}*/
|
|
||||||
|
|
||||||
// should be recursive for all children props need to load component
|
|
||||||
|
|
||||||
const render = (component) => {
|
const render = (component) => {
|
||||||
if (component) {
|
if (component) {
|
||||||
const reactComponent = auroraComponents[component.component]
|
const reactComponent = auroraComponents[component.component]
|
||||||
|
@ -22,7 +22,7 @@ export type ActionFunction = (params: ActionParams) => Promise<void>
|
|||||||
*/
|
*/
|
||||||
export interface Answer {
|
export interface Answer {
|
||||||
key?: string
|
key?: string
|
||||||
widget?: Widget
|
widget?: Widget<unknown>
|
||||||
data?: AnswerData
|
data?: AnswerData
|
||||||
core?: SkillAnswerCoreData
|
core?: SkillAnswerCoreData
|
||||||
}
|
}
|
||||||
@ -30,7 +30,7 @@ export interface TextAnswer extends Answer {
|
|||||||
key: string
|
key: string
|
||||||
}
|
}
|
||||||
export interface WidgetAnswer extends Answer {
|
export interface WidgetAnswer extends Answer {
|
||||||
widget: Widget
|
widget: Widget<unknown>
|
||||||
key?: string
|
key?: string
|
||||||
}
|
}
|
||||||
export type AnswerData = Record<string, string | number> | null
|
export type AnswerData = Record<string, string | number> | null
|
||||||
|
@ -1,11 +1,25 @@
|
|||||||
import { type WidgetWrapperProps } from '@leon-ai/aurora'
|
import { type WidgetWrapperProps } from '@leon-ai/aurora'
|
||||||
|
|
||||||
interface WidgetOptions {
|
export interface WidgetOptions<T> {
|
||||||
wrapperProps: Omit<WidgetWrapperProps, 'children'>
|
wrapperProps?: Omit<WidgetWrapperProps, 'children'>
|
||||||
|
params?: T
|
||||||
}
|
}
|
||||||
|
|
||||||
export abstract class Widget {
|
export abstract class Widget<T> {
|
||||||
public abstract wrapperProps: WidgetOptions['wrapperProps']
|
public wrapperProps: WidgetOptions<T>['wrapperProps']
|
||||||
|
public params: WidgetOptions<T>['params']
|
||||||
|
|
||||||
|
protected constructor(options?: WidgetOptions<T>) {
|
||||||
|
if (options?.wrapperProps) {
|
||||||
|
this.wrapperProps = options.wrapperProps
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!options?.params) {
|
||||||
|
this.params = undefined
|
||||||
|
} else {
|
||||||
|
this.params = options.params
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
|
@ -1,13 +1,7 @@
|
|||||||
import fs from 'node:fs'
|
|
||||||
import path from 'node:path'
|
|
||||||
|
|
||||||
import type { ActionFunction } from '@sdk/types'
|
import type { ActionFunction } from '@sdk/types'
|
||||||
import { leon } from '@sdk/leon'
|
import { leon } from '@sdk/leon'
|
||||||
// TODO: import widgets @sdk/aurora
|
|
||||||
// import { Button, Card } from '@sdk/aurora'
|
|
||||||
|
|
||||||
import PlaygroundTestWidget from '../widgets/playground-test'
|
import PlaygroundTestWidget from '../widgets/playground-test'
|
||||||
// import tsxTemplate from '../widgets/playground-test'
|
|
||||||
|
|
||||||
export const run: ActionFunction = async function () {
|
export const run: ActionFunction = async function () {
|
||||||
/**
|
/**
|
||||||
@ -216,25 +210,6 @@ export const run: ActionFunction = async function () {
|
|||||||
|
|
||||||
// const widget = createElement('Card', null, createElement('Button', null, 'Click me'))
|
// const widget = createElement('Card', null, createElement('Button', null, 'Click me'))
|
||||||
|
|
||||||
// Problem with this solution: can't pass dynamic value to the widget
|
|
||||||
const widgetPath = path.resolve(__dirname, '../widgets/playground-test.tsx')
|
|
||||||
const fileContent = await fs.promises.readFile(widgetPath, 'utf-8')
|
|
||||||
|
|
||||||
const regex = /return\s*\(\s*([\s\S]*?)\s*\)/
|
|
||||||
const match = fileContent.match(regex)
|
|
||||||
let widgetContent = null
|
|
||||||
if (match) {
|
|
||||||
const [content] = match
|
|
||||||
|
|
||||||
widgetContent = content.slice(8, -1)
|
|
||||||
} else {
|
|
||||||
// TODO: error in skill not respecting guideline
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* TODO: non-JSX trial
|
|
||||||
*/
|
|
||||||
|
|
||||||
const playgroundTestWidget = new PlaygroundTestWidget({
|
const playgroundTestWidget = new PlaygroundTestWidget({
|
||||||
params: {
|
params: {
|
||||||
value1: 'Hello',
|
value1: 'Hello',
|
||||||
@ -245,13 +220,5 @@ export const run: ActionFunction = async function () {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
// TODO: from SDK, auto call render()
|
|
||||||
// await leon.answer({ widget })
|
|
||||||
await leon.answer({ widget: playgroundTestWidget })
|
await leon.answer({ widget: playgroundTestWidget })
|
||||||
|
|
||||||
/**
|
|
||||||
* TODO: think of parsing JSX at the server side instead
|
|
||||||
* No need to develop a parser for every future client
|
|
||||||
*/
|
|
||||||
// await leon.answer({ widget: widgetContent })
|
|
||||||
}
|
}
|
||||||
|
@ -1,52 +1,25 @@
|
|||||||
// TODO: "Widget" mother class from SDK
|
import { Widget, type WidgetOptions } from '@sdk/widget'
|
||||||
// export default class WidgetPlayground extends Widget {
|
import { Button } from '@sdk/aurora'
|
||||||
|
|
||||||
import { Button, Flexbox } from '@sdk/aurora'
|
|
||||||
import { Widget } from '@sdk/widget'
|
|
||||||
|
|
||||||
interface WidgetOptions {
|
|
||||||
wrapperProps: Widget['wrapperProps']
|
|
||||||
params: Params
|
|
||||||
}
|
|
||||||
|
|
||||||
interface Params {
|
interface Params {
|
||||||
value1: string
|
value1: string
|
||||||
value2: string
|
value2: string
|
||||||
}
|
}
|
||||||
|
|
||||||
export default class PlaygroundTestWidget extends Widget {
|
export default class PlaygroundTestWidget extends Widget<Params> {
|
||||||
public readonly wrapperProps: Widget['wrapperProps']
|
constructor(options?: WidgetOptions<Params>) {
|
||||||
private readonly params: Params
|
super(options)
|
||||||
|
|
||||||
constructor(options: WidgetOptions) {
|
|
||||||
super()
|
|
||||||
|
|
||||||
this.wrapperProps = options.wrapperProps
|
|
||||||
this.params = options.params
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public render() {
|
public render(): Button {
|
||||||
// TODO: force WidgetWrapper to be the root element (from mother class render method)
|
let children = 'Click me'
|
||||||
|
|
||||||
|
if (this.params) {
|
||||||
|
children = this.params.value1 + ' ' + this.params.value2
|
||||||
|
}
|
||||||
|
|
||||||
return new Button({
|
return new Button({
|
||||||
children: this.params.value1 + ' ' + this.params.value2
|
children
|
||||||
})
|
})
|
||||||
|
|
||||||
/*return new WidgetWrapper({
|
|
||||||
children: [
|
|
||||||
new Flexbox({
|
|
||||||
gap: 'md',
|
|
||||||
children: [
|
|
||||||
new Button({
|
|
||||||
children: this.params.value1 + ' ' + this.params.value2
|
|
||||||
}),
|
|
||||||
new Button({
|
|
||||||
danger: true,
|
|
||||||
children: 'Danger button'
|
|
||||||
})
|
|
||||||
]
|
|
||||||
})
|
|
||||||
]
|
|
||||||
})*/
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user