import { useEffect, useRef } from 'react';
import { X } from 'lucide-react';
import { Dialog, DialogOverlay, DialogPortal } from '@/components/ui/dialog';
import * as DialogPrimitive from '@radix-ui/react-dialog';
import { cardStyle } from '@/components/Card';
import type { TaskInfo } from 'apps/Chat';
import { usePi, EmbeddableChat } from 'apps/Chat';
import { useVisiblePiModels } from '@/state/useModels';
import { useSettings } from '@/state/useSettings';
import type { TaskSummary } from './useTasks';
const playDing = () => {
const ctx = new AudioContext();
const t = ctx.currentTime;
const play = (freq: number, start: number, dur: number) => {
const osc = ctx.createOscillator();
const gain = ctx.createGain();
osc.type = 'sine';
osc.frequency.setValueAtTime(freq, t + start);
gain.gain.setValueAtTime(0.8, t + start);
gain.gain.setValueAtTime(0.8, t + start + dur * 0.6);
gain.gain.exponentialRampToValueAtTime(0.001, t + start + dur);
osc.connect(gain).connect(ctx.destination);
osc.start(t + start);
osc.stop(t + start + dur);
};
play(880, 0, 0.4);
play(1100, 0.25, 0.4);
play(1320, 0.5, 0.6);
setTimeout(() => ctx.close(), 1500);
};
type PiMonoInnerProps = {
defaultInput: string;
cwd: { root?: string; path: string };
initialModel: string | null;
taskInfo: TaskInfo;
};
const PiMonoInner = ({
defaultInput,
cwd,
initialModel,
taskInfo,
}: PiMonoInnerProps) => {
const chat = usePi(undefined, initialModel, { replaceUrl: false, taskInfo });
const models = useVisiblePiModels();
const wasGenerating = useRef(false);
useEffect(() => {
if (wasGenerating.current && !chat.isGenerating) playDing();
wasGenerating.current = chat.isGenerating;
}, [chat.isGenerating]);
return (
);
};
type TaskRunnerModalProps = {
open: boolean;
onOpenChange: (open: boolean) => void;
task: TaskSummary;
entryName?: string;
entryType?: 'file' | 'directory';
cwd?: { root?: string; path: string };
promptOverride?: string;
};
export const TaskRunnerModal = ({ open, onOpenChange, task, entryName, entryType, cwd = { path: '' }, promptOverride }: TaskRunnerModalProps) => {
const { settings } = useSettings();
const taskSettings = settings.tasks;
const defaultInput = promptOverride
?? (entryName && entryType
? `Read the task instructions at ${task.filePath} and execute them on the ${entryType}: ${entryName}`
: `Read the task instructions at ${task.filePath} and execute them`);
const taskInfo: TaskInfo = { taskName: task.name, taskDirName: task.dirName, entryName: entryName ?? '', entryType: entryType ?? 'file' };
return (
);
};