1
1
mirror of https://github.com/leon-ai/leon.git synced 2024-12-18 14:21:32 +03:00

Merge branch 'skill-widget-ideas' into develop

This commit is contained in:
louistiti 2023-05-16 23:08:00 +08:00
commit 259dc53213
8 changed files with 42 additions and 55 deletions

View File

@ -79,7 +79,15 @@ export default class Client {
const root = createRoot(container)
// TODO: widget: pass props and dynamic component loading according to type
root.render(Button({ children: 'OK' }))
const widgets = {
Button: (options) => {
return Button({
children: options.text
})
}
}
root.render(widgets[data.type](data.options))
})
this.socket.on('audio-forwarded', (data, cb) => {

View File

@ -1,14 +1,12 @@
import { Widget } from '../widget'
// TODO: contains the button API. rendering engine <-> SDK
interface Options {
interface ButtonOptions {
text: string
}
export class Button {
private readonly text: string
constructor(options: Options) {
this.text = options.text
console.log('Button constructor', this.text)
export class Button extends Widget<ButtonOptions> {
public constructor(options: ButtonOptions) {
super(options)
}
}

View File

@ -8,7 +8,7 @@ interface MemoryOptions<T> {
defaultMemory?: T
}
export class Memory<T> {
export class Memory<T = unknown> {
private readonly memoryPath: string | undefined
private readonly name: string
private readonly defaultMemory: T | undefined
@ -55,12 +55,14 @@ export class Memory<T> {
* Read the memory
* @example read()
*/
public async read(): Promise<T | undefined> {
try {
if (!this.memoryPath) {
return
}
public async read(): Promise<T> {
if (!this.memoryPath) {
throw new Error(
`You cannot read the memory "${this.name}" as it belongs to another skill which haven't written to this memory yet`
)
}
try {
if (!fs.existsSync(this.memoryPath)) {
await this.clear()
}

View File

@ -3,17 +3,10 @@
*/
import type { ActionParams, IntentObject } from '@/core/brain/types'
import type { Button } from '@sdk/aurora/button'
export type { ActionParams, IntentObject }
export type ActionFunction = (params: ActionParams) => Promise<void>
/**
* Aurora
*/
export type AuroraComponent = Button // TODO
/**
* Answer types
*/

View File

@ -1,23 +1,9 @@
import type { AuroraComponent } from '@sdk/types'
export abstract class Widget<T> {
public readonly type: string
public readonly options: T
interface SerializedAuroraComponent {
type: string
props: Record<string, unknown>
}
export class Widget {
private readonly components: SerializedAuroraComponent[]
constructor(components: AuroraComponent[]) {
this.components = components.map((component) => {
return {
type: component.constructor.name,
props: {
...component
}
}
})
console.log('Widget constructor', this.components)
protected constructor(options: T) {
this.type = this.constructor.name
this.options = options
}
}

View File

@ -159,11 +159,11 @@ dotenv.config()
if (Math.round(freeRAMInGB) < MINIMUM_REQUIRED_RAM) {
report.can_run.v = false
LogHelper.error(
`Free RAM: ${freeRAMInGB} | Total RAM: ${totalRAMInGB} GB. Leon needs at least ${MINIMUM_REQUIRED_RAM} GB of RAM`
`Free RAM: ${freeRAMInGB} GB | Total RAM: ${totalRAMInGB} GB. Leon needs at least ${MINIMUM_REQUIRED_RAM} GB of RAM`
)
} else {
LogHelper.success(
`Free RAM: ${freeRAMInGB} | Total RAM: ${totalRAMInGB} GB`
`Free RAM: ${freeRAMInGB} GB | Total RAM: ${totalRAMInGB} GB`
)
}

View File

@ -101,11 +101,11 @@ const GLOBAL_DATA_SCHEMAS = {
if (freeRAMInGB < MINIMUM_REQUIRED_RAM) {
LogHelper.warning(
`Free RAM: ${freeRAMInGB} | Total RAM: ${totalRAMInGB} GB. Leon needs at least ${MINIMUM_REQUIRED_RAM} GB of RAM. It may not work as expected.`
`Free RAM: ${freeRAMInGB} GB | Total RAM: ${totalRAMInGB} GB. Leon needs at least ${MINIMUM_REQUIRED_RAM} GB of RAM. It may not work as expected.`
)
} else {
LogHelper.success(
`Minimum required RAM: ${MINIMUM_REQUIRED_RAM} GB | Free RAM: ${freeRAMInGB} | Total RAM: ${totalRAMInGB} GB`
`Minimum required RAM: ${MINIMUM_REQUIRED_RAM} GB | Free RAM: ${freeRAMInGB} GB | Total RAM: ${totalRAMInGB} GB`
)
}

View File

@ -2,7 +2,6 @@ import utility from 'utility' // TODO
import type { ActionFunction } from '@sdk/types'
import { leon } from '@sdk/leon'
import { Widget } from '@sdk/widget'
import { Network } from '@sdk/network'
import { Button } from '@sdk/aurora/button'
import { Memory } from '@sdk/memory'
@ -21,20 +20,21 @@ export const run: ActionFunction = async function () {
const button = new Button({
text: 'Hello world from action skill'
})
const widget = new Widget([button])
await leon.answer({ widget })
await leon.answer({ widget: button })
///
const otherSkillMemory = new Memory<unknown>({
const otherSkillMemory = new Memory({
name: 'productivity:todo_list:db'
})
try {
const todoLists = await otherSkillMemory.read()
console.log('todoLists', todoLists)
} catch {
console.log('todoLists', [])
}
const postsMemory = new Memory<Post[]>({ name: 'posts', defaultMemory: [] })
const todoLists = await otherSkillMemory.read()
console.log('todoLists', todoLists)
await postsMemory.write([
{
id: 0,