Skip to content

Commit

Permalink
feat(message): extract feature from command
Browse files Browse the repository at this point in the history
  • Loading branch information
PleahMaCaka committed Oct 31, 2023
1 parent 8a10765 commit e2315bb
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 9 deletions.
52 changes: 43 additions & 9 deletions src/lib/components/chat/messages/AssistantMessage.svelte
Original file line number Diff line number Diff line change
@@ -1,31 +1,65 @@
<script lang="ts">
import BaseMessage from "$lib/components/chat/messages/BaseMessage.svelte";
import { stateStore } from "$lib/stores/StateStore"
import { onMount } from "svelte"
import { Features } from "$lib/types/Features"
import { afterUpdate } from "svelte"
import { get } from "svelte/store"
export let message: string = ""
// extract contents within backticks
const regex = /`([^`]*)`/g
let commands: Array<string> = []
let raw_commands: Array<string> = [] // raw commands but extract only the first one
let command: string // the first element of commands if it exists
let args: Array<string> = []
let feature: Features
function getFeature(feature: string): Features {
return Object.values(Features).includes(feature as Features) ? feature as Features : Features.None
}
onMount(() => {
commands = message.match(regex) ?? []
// TODO make better
let executed = false // for lifecycle, must be executed only once
if (commands.length > 0) {
command = commands[0].slice(2, -1) // remove backticks
message = message.replaceAll(regex, "") // remove commands from the message
afterUpdate(() => { // It cannot be $: can't use global return
if (executed) {
// If executed feature is once over, We don't have to execute more but don't want to remove from history.
// We don't have a plan to remove from history remove from the message when history is updated
// ~ until find better way
message = message.replaceAll(regex, "").trim()
return
}
raw_commands = message.match(regex) ?? []
// preprocess message
if (raw_commands.length > 0) {
command = raw_commands[0].slice(2, -1) // remove backticks
message = message.replaceAll(regex, "").trim() // remove commands from the message
command = command.trim().toLowerCase() // TODO do not use like a buffer
const splitted = command.split(" ")
command = splitted[0] // root of cmd
args = splitted.slice(1) // else args
}
// fetch feature from command and check is that real exists one
feature = getFeature(command)
// debug and last processing stuffs
if (get(stateStore).debug)
console.log("Assisting!", {
message,
commands,
command
commands: raw_commands,
args,
command,
"feature": feature,
})
executed = true
})
</script>

Expand Down
4 changes: 4 additions & 0 deletions src/lib/types/Features.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export enum Features {
None = "None",
Weather = "weather"
}

0 comments on commit e2315bb

Please sign in to comment.