feat(chat): the dashboard chat records voice, downloads files a turn sends, and offers /model and /personality choices
This commit is contained in:
1 parent
201529414b
commit
8a8c8cf7e9
16 files changed
+380
-92
No files matched your search
@@ -0,0 +1,38 @@
|
||||
<script setup lang="ts">
|
||||
import { computed } from "vue";
|
||||
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { selectorData, setModel, setPersona } from "./state";
|
||||
|
||||
const props = defineProps<{ choose: "model" | "personality" }>();
|
||||
|
||||
const options = computed(() =>
|
||||
props.choose === "model" ? (selectorData.value.models ?? []) : (selectorData.value.personas ?? []),
|
||||
);
|
||||
const active = computed(() =>
|
||||
props.choose === "model" ? selectorData.value.active_model : selectorData.value.active_persona,
|
||||
);
|
||||
|
||||
function pick(value: string) {
|
||||
if (props.choose === "model") void setModel(value);
|
||||
else setPersona(value);
|
||||
}
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<div class="text-sm">
|
||||
<p class="font-medium">{{ choose === "model" ? "Choose a model" : "Choose a personality" }}</p>
|
||||
<p v-if="!options.length" class="mt-1 text-fg-muted">None configured.</p>
|
||||
<div v-else class="mt-2 flex flex-wrap gap-2">
|
||||
<Button
|
||||
v-for="option in options"
|
||||
:key="option"
|
||||
size="sm"
|
||||
:variant="option === active ? 'default' : 'outline'"
|
||||
class="font-mono"
|
||||
@click="pick(option)"
|
||||
>{{ option }}</Button
|
||||
>
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
@@ -1,5 +1,7 @@
|
||||
<script setup lang="ts">
|
||||
import { nextTick, ref, watch } from "vue";
|
||||
import { computed, nextTick, ref, watch } from "vue";
|
||||
|
||||
import { Mic } from "@lucide/vue";
|
||||
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { Textarea } from "@/components/ui/textarea";
|
||||
@@ -15,6 +17,8 @@ import {
|
||||
isSending,
|
||||
moveCommandSelection,
|
||||
sendMessage,
|
||||
selectorData,
|
||||
sendVoice,
|
||||
stopTurn,
|
||||
syncCommandMenu,
|
||||
} from "./state";
|
||||
@@ -34,6 +38,33 @@ import {
|
||||
*/
|
||||
const props = defineProps<{ open: boolean }>();
|
||||
|
||||
// Voice: recorded in the browser, transcribed by the server like a Telegram
|
||||
// voice note. The mic shows only while the composer is empty.
|
||||
const canRecord = computed(
|
||||
() => !!selectorData.value.voice_available && typeof MediaRecorder !== "undefined" && !!navigator.mediaDevices?.getUserMedia,
|
||||
);
|
||||
const recording = ref(false);
|
||||
let recorder: MediaRecorder | null = null;
|
||||
async function toggleRecording() {
|
||||
if (recorder) {
|
||||
recorder.stop();
|
||||
return;
|
||||
}
|
||||
const stream = await navigator.mediaDevices.getUserMedia({ audio: true });
|
||||
const chunks: Blob[] = [];
|
||||
recorder = new MediaRecorder(stream);
|
||||
recorder.ondataavailable = (e) => chunks.push(e.data);
|
||||
recorder.onstop = () => {
|
||||
stream.getTracks().forEach((t) => t.stop());
|
||||
const clip = new Blob(chunks, { type: recorder?.mimeType || "audio/webm" });
|
||||
recorder = null;
|
||||
recording.value = false;
|
||||
if (clip.size) void sendVoice(clip);
|
||||
};
|
||||
recorder.start();
|
||||
recording.value = true;
|
||||
}
|
||||
|
||||
const field = ref<{ $el: HTMLTextAreaElement } | null>(null);
|
||||
|
||||
function focus(): void {
|
||||
@@ -156,6 +187,16 @@ function onKeydown(event: KeyboardEvent): void {
|
||||
>Stop</Button
|
||||
>
|
||||
<Button
|
||||
v-if="canRecord && !composerText.trim()"
|
||||
size="sm"
|
||||
:variant="recording ? 'destructive' : 'outline'"
|
||||
:disabled="isSending && !recording"
|
||||
:aria-label="recording ? 'Stop recording and send' : 'Record a voice message'"
|
||||
@click="toggleRecording"
|
||||
><Mic data-icon="inline-start" />{{ recording ? "Send" : "" }}</Button
|
||||
>
|
||||
<Button
|
||||
v-else
|
||||
size="sm"
|
||||
:disabled="isSending || !composerText.trim()"
|
||||
@click="sendMessage()"
|
||||
|
||||
@@ -12,6 +12,7 @@ import {
|
||||
} from "@/components/ui/message-scroller";
|
||||
import ChatAskCard from "./ChatAskCard.vue";
|
||||
import ChatBubble from "./ChatBubble.vue";
|
||||
import ChatChooseCard from "./ChatChooseCard.vue";
|
||||
import ChatUpdateCard from "./ChatUpdateCard.vue";
|
||||
import ChatEmptyState from "./ChatEmptyState.vue";
|
||||
import {
|
||||
@@ -44,6 +45,7 @@ function parts(message: ChatMessage) {
|
||||
media: message.media || [],
|
||||
assistant: from !== "web",
|
||||
update: message.update,
|
||||
choose: message.choose,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -73,6 +75,7 @@ const bubbles = computed(() => messages.value.map(parts));
|
||||
:assistant="bubble.assistant"
|
||||
>
|
||||
<ChatUpdateCard v-if="bubble.update" :update="bubble.update" />
|
||||
<ChatChooseCard v-if="bubble.choose" :choose="bubble.choose" />
|
||||
</ChatBubble>
|
||||
</MessageScrollerItem>
|
||||
|
||||
|
||||
@@ -93,6 +93,8 @@ export interface ChatMessage {
|
||||
MessageID?: string;
|
||||
/** An update check answered in the panel, with its install actions. */
|
||||
update?: ChatUpdate;
|
||||
/** A bare /model or /personality, answered with its choices. */
|
||||
choose?: "model" | "personality";
|
||||
}
|
||||
|
||||
/** The update snapshot /update shows, and whether this host can install it. */
|
||||
@@ -133,6 +135,7 @@ export interface ChatSelectorData {
|
||||
active_provider?: string;
|
||||
active_model?: string;
|
||||
commands?: Array<ChatCommandSpec | string>;
|
||||
voice_available?: boolean;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -422,6 +425,16 @@ export async function sendMessage(retryOpts?: RetryOptions): Promise<void> {
|
||||
composerText.value = "";
|
||||
}
|
||||
|
||||
// Bare /model and /personality offer their choices as buttons, as Telegram
|
||||
// does; with an argument they go to the gateway like any command.
|
||||
if (!isRetry && (text.trim() === "/model" || text.trim() === "/personality")) {
|
||||
const choose = text.trim() === "/model" ? "model" : "personality";
|
||||
messages.value = [...messages.value, { from: "assistant", text: "", choose }];
|
||||
isSending.value = false;
|
||||
statusText.value = "Ready";
|
||||
return;
|
||||
}
|
||||
|
||||
// /update answers here, where the panel can offer Install like Telegram does.
|
||||
if (!isRetry && text.trim() === "/update") {
|
||||
try {
|
||||
@@ -468,6 +481,7 @@ export async function sendMessage(retryOpts?: RetryOptions): Promise<void> {
|
||||
source_id: turn.sourceID,
|
||||
text: turn.text,
|
||||
page: currentPage(),
|
||||
voice: turn.voice,
|
||||
},
|
||||
{ signal: controller.signal },
|
||||
);
|
||||
@@ -586,3 +600,16 @@ export function useChat(): void {
|
||||
abortActiveTurn();
|
||||
});
|
||||
}
|
||||
|
||||
/** sendVoice sends a recorded clip as a turn; the server transcribes it. */
|
||||
export async function sendVoice(clip: Blob): Promise<void> {
|
||||
const bytes = new Uint8Array(await clip.arrayBuffer());
|
||||
let binary = "";
|
||||
for (let i = 0; i < bytes.length; i += 0x8000) {
|
||||
binary += String.fromCharCode(...bytes.subarray(i, i + 0x8000));
|
||||
}
|
||||
await sendMessage({
|
||||
textOverride: "🎤 Voice message",
|
||||
voice: { data: btoa(binary), mime_type: clip.type || "audio/webm" },
|
||||
});
|
||||
}
|
||||
+6
-3
@@ -7,10 +7,12 @@ import { randomUUID } from "@/lib/uuid";
|
||||
export interface ChatTurn {
|
||||
text: string;
|
||||
sourceID: string;
|
||||
/** A recorded clip, base64, transcribed by the server. */
|
||||
voice?: { data: string; mime_type: string };
|
||||
}
|
||||
|
||||
export function newChatTurn(text: string): ChatTurn {
|
||||
return { text, sourceID: randomUUID() };
|
||||
export function newChatTurn(text: string, voice?: ChatTurn["voice"]): ChatTurn {
|
||||
return { text, sourceID: randomUUID(), voice };
|
||||
}
|
||||
|
||||
export function retryChatTurn(turn: ChatTurn): ChatTurn {
|
||||
@@ -21,6 +23,7 @@ export function retryChatTurn(turn: ChatTurn): ChatTurn {
|
||||
export interface RetryOptions {
|
||||
textOverride?: string;
|
||||
turn?: ChatTurn;
|
||||
voice?: ChatTurn["voice"];
|
||||
}
|
||||
|
||||
// resolveTurn decides what to send: a real retry descriptor's original turn,
|
||||
@@ -42,6 +45,6 @@ export function resolveTurn(
|
||||
typeof retry.turn.text === "string"
|
||||
);
|
||||
const text = isRetry ? retry.turn!.text : composerValue.trim();
|
||||
const turn = isRetry ? retryChatTurn(retry.turn!) : newChatTurn(text);
|
||||
const turn = isRetry ? retryChatTurn(retry.turn!) : newChatTurn(text, retry?.voice);
|
||||
return { text, turn, isRetry };
|
||||
}
|
||||
Reference in new issue
Block a user