mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-06 12:54:20 +01:00
Update memory plugin (#160)
This commit is contained in:
1 parent
be7e64b07f
commit
1248d0b9e0
50 files changed
+5363
-291
No files matched your search
@@ -0,0 +1,186 @@
|
||||
import { useEffect, useState } from 'react'
|
||||
import { useForm } from 'react-hook-form'
|
||||
import { zodResolver } from '@hookform/resolvers/zod'
|
||||
import { z } from 'zod'
|
||||
import { useCreateKvEntry, useUpdateKvEntry } from '@/hooks/useMemories'
|
||||
import type { KvEntry, CreateKvEntryRequest, UpdateKvEntryRequest } from '@opencode-manager/shared/types'
|
||||
import {
|
||||
Dialog,
|
||||
DialogContent,
|
||||
DialogDescription,
|
||||
DialogFooter,
|
||||
DialogHeader,
|
||||
DialogTitle,
|
||||
} from '@/components/ui/dialog'
|
||||
import { Button } from '@/components/ui/button'
|
||||
import { Input } from '@/components/ui/input'
|
||||
import { Textarea } from '@/components/ui/textarea'
|
||||
import { Label } from '@/components/ui/label'
|
||||
|
||||
const kvSchema = z.object({
|
||||
key: z.string().min(1, 'Key is required'),
|
||||
data: z.string().min(1, 'Data is required'),
|
||||
ttlHours: z.number().optional(),
|
||||
})
|
||||
|
||||
type KvFormData = z.infer<typeof kvSchema>
|
||||
|
||||
interface KvFormDialogProps {
|
||||
entry?: KvEntry
|
||||
projectId?: string
|
||||
open: boolean
|
||||
onOpenChange: (open: boolean) => void
|
||||
}
|
||||
|
||||
export function KvFormDialog({ entry, projectId, open, onOpenChange }: KvFormDialogProps) {
|
||||
const createMutation = useCreateKvEntry()
|
||||
const updateMutation = useUpdateKvEntry()
|
||||
|
||||
const [jsonError, setJsonError] = useState<string | undefined>()
|
||||
|
||||
const {
|
||||
register,
|
||||
handleSubmit,
|
||||
reset,
|
||||
formState: { errors },
|
||||
} = useForm<KvFormData>({
|
||||
resolver: zodResolver(kvSchema),
|
||||
defaultValues: {
|
||||
key: '',
|
||||
data: '',
|
||||
ttlHours: undefined,
|
||||
},
|
||||
})
|
||||
|
||||
useEffect(() => {
|
||||
if (open) {
|
||||
if (entry) {
|
||||
const ttlMs = entry.expiresAt - entry.updatedAt
|
||||
const ttlHoursValue = ttlMs > 0 && ttlMs < Number.MAX_SAFE_INTEGER
|
||||
? Math.round(ttlMs / (1000 * 60 * 60))
|
||||
: undefined
|
||||
|
||||
reset({
|
||||
key: entry.key,
|
||||
data: JSON.stringify(entry.data, null, 2),
|
||||
ttlHours: ttlHoursValue,
|
||||
})
|
||||
} else {
|
||||
reset({
|
||||
key: '',
|
||||
data: '',
|
||||
ttlHours: undefined,
|
||||
})
|
||||
}
|
||||
setJsonError(undefined)
|
||||
}
|
||||
}, [open, entry, reset])
|
||||
|
||||
const validateJson = (value: string): boolean => {
|
||||
try {
|
||||
JSON.parse(value)
|
||||
setJsonError(undefined)
|
||||
return true
|
||||
} catch (e) {
|
||||
setJsonError(e instanceof Error ? e.message : 'Invalid JSON')
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
const onSubmit = async (data: KvFormData) => {
|
||||
if (!validateJson(data.data)) {
|
||||
return
|
||||
}
|
||||
|
||||
const parsedData = JSON.parse(data.data)
|
||||
const ttlMs = data.ttlHours ? data.ttlHours * 1000 * 60 * 60 : undefined
|
||||
|
||||
if (entry) {
|
||||
const updateData: UpdateKvEntryRequest = {
|
||||
data: parsedData,
|
||||
ttlMs,
|
||||
}
|
||||
await updateMutation.mutateAsync({ projectId: projectId!, key: entry.key, data: updateData })
|
||||
} else if (projectId) {
|
||||
const createData: CreateKvEntryRequest = {
|
||||
projectId,
|
||||
key: data.key,
|
||||
data: parsedData,
|
||||
ttlMs,
|
||||
}
|
||||
await createMutation.mutateAsync(createData)
|
||||
}
|
||||
onOpenChange(false)
|
||||
}
|
||||
|
||||
const isLoading = createMutation.isPending || updateMutation.isPending
|
||||
|
||||
return (
|
||||
<Dialog open={open} onOpenChange={onOpenChange}>
|
||||
<DialogContent className="sm:max-w-[500px]">
|
||||
<DialogHeader>
|
||||
<DialogTitle>{entry ? 'Edit KV Entry' : 'Create KV Entry'}</DialogTitle>
|
||||
<DialogDescription>
|
||||
{entry
|
||||
? 'Update the KV entry data and TTL.'
|
||||
: 'Add a new key-value entry to store project data.'}
|
||||
</DialogDescription>
|
||||
</DialogHeader>
|
||||
|
||||
<form onSubmit={handleSubmit(onSubmit)} className="space-y-4">
|
||||
<div className="space-y-2">
|
||||
<Label htmlFor="key">Key</Label>
|
||||
<Input
|
||||
id="key"
|
||||
{...register('key')}
|
||||
placeholder="Enter key..."
|
||||
disabled={!!entry}
|
||||
/>
|
||||
{errors.key && (
|
||||
<p className="text-sm text-destructive">{errors.key.message}</p>
|
||||
)}
|
||||
</div>
|
||||
|
||||
<div className="space-y-2">
|
||||
<Label htmlFor="data">Data (JSON)</Label>
|
||||
<Textarea
|
||||
id="data"
|
||||
{...register('data')}
|
||||
placeholder='{"key": "value"}'
|
||||
rows={6}
|
||||
className="resize-none font-mono"
|
||||
onBlur={(e) => validateJson(e.target.value)}
|
||||
/>
|
||||
{errors.data && (
|
||||
<p className="text-sm text-destructive">{errors.data.message}</p>
|
||||
)}
|
||||
{jsonError && (
|
||||
<p className="text-sm text-destructive">{jsonError}</p>
|
||||
)}
|
||||
</div>
|
||||
|
||||
<div className="space-y-2">
|
||||
<Label htmlFor="ttlHours">TTL (hours, optional)</Label>
|
||||
<Input
|
||||
id="ttlHours"
|
||||
type="number"
|
||||
min="0"
|
||||
step="1"
|
||||
{...register('ttlHours', { valueAsNumber: true })}
|
||||
placeholder="Enter TTL in hours..."
|
||||
/>
|
||||
</div>
|
||||
|
||||
<DialogFooter className='flex gap-2 flex-wrap'>
|
||||
<Button type="button" variant="outline" onClick={() => onOpenChange(false)}>
|
||||
Cancel
|
||||
</Button>
|
||||
<Button type="submit" disabled={isLoading || !!jsonError}>
|
||||
{isLoading ? 'Saving...' : entry ? 'Update' : 'Create'}
|
||||
</Button>
|
||||
</DialogFooter>
|
||||
</form>
|
||||
</DialogContent>
|
||||
</Dialog>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,182 @@
|
||||
import { useState } from 'react'
|
||||
import { useKvEntries, useDeleteKvEntry } from '@/hooks/useMemories'
|
||||
import { useDebounce } from '@/hooks/useDebounce'
|
||||
import type { KvEntry } from '@opencode-manager/shared/types'
|
||||
import { Button } from '@/components/ui/button'
|
||||
import { Badge } from '@/components/ui/badge'
|
||||
import { Input } from '@/components/ui/input'
|
||||
import { Loader2, Trash2, Key, ChevronDown, ChevronRight, Plus } from 'lucide-react'
|
||||
import { DeleteDialog } from '@/components/ui/delete-dialog'
|
||||
import { KvFormDialog } from './KvFormDialog'
|
||||
|
||||
interface KvListProps {
|
||||
projectId?: string
|
||||
}
|
||||
|
||||
function formatTimeRemaining(expiresAt: number): string {
|
||||
const now = Date.now()
|
||||
const diff = expiresAt - now
|
||||
|
||||
if (diff <= 0) {
|
||||
return 'expired'
|
||||
}
|
||||
|
||||
const hours = Math.floor(diff / (1000 * 60 * 60))
|
||||
const minutes = Math.floor((diff % (1000 * 60 * 60)) / (1000 * 60))
|
||||
|
||||
if (hours > 0) {
|
||||
return `${hours}h ${minutes}m`
|
||||
}
|
||||
|
||||
return `${minutes}m`
|
||||
}
|
||||
|
||||
export function KvList({ projectId }: KvListProps) {
|
||||
const [searchQuery, setSearchQuery] = useState<string>('')
|
||||
const debouncedSearch = useDebounce(searchQuery, 300)
|
||||
const [expandedKey, setExpandedKey] = useState<string | null>(null)
|
||||
const [deleteKey, setDeleteKey] = useState<string | null>(null)
|
||||
const [dialogOpen, setDialogOpen] = useState(false)
|
||||
const [editingEntry, setEditingEntry] = useState<KvEntry | undefined>(undefined)
|
||||
|
||||
const { data: entries, isLoading } = useKvEntries(projectId, debouncedSearch || undefined)
|
||||
const deleteMutation = useDeleteKvEntry()
|
||||
|
||||
const handleDelete = () => {
|
||||
if (deleteKey && projectId) {
|
||||
deleteMutation.mutate({ projectId, key: deleteKey })
|
||||
setDeleteKey(null)
|
||||
}
|
||||
}
|
||||
|
||||
const toggleExpand = (key: string) => {
|
||||
setExpandedKey(expandedKey === key ? null : key)
|
||||
}
|
||||
|
||||
const handleEdit = (entry: KvEntry) => {
|
||||
setEditingEntry(entry)
|
||||
setDialogOpen(true)
|
||||
}
|
||||
|
||||
const handleCreate = () => {
|
||||
setEditingEntry(undefined)
|
||||
setDialogOpen(true)
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="space-y-4">
|
||||
<div className="flex items-center gap-2">
|
||||
<Input
|
||||
placeholder="Filter by key prefix..."
|
||||
value={searchQuery}
|
||||
onChange={(e) => setSearchQuery(e.target.value)}
|
||||
className="w-64"
|
||||
/>
|
||||
<Button onClick={handleCreate} size="sm">
|
||||
<Plus className="w-4 h-4 mr-2" />
|
||||
Add KV Entry
|
||||
</Button>
|
||||
</div>
|
||||
|
||||
{isLoading ? (
|
||||
<div className="flex items-center justify-center p-8">
|
||||
<Loader2 className="w-6 h-6 animate-spin text-muted-foreground" />
|
||||
</div>
|
||||
) : !entries || entries.length === 0 ? (
|
||||
<div className="text-center p-8 text-muted-foreground">
|
||||
<Key className="w-8 h-8 mx-auto mb-2 opacity-50" />
|
||||
<p>No KV entries found</p>
|
||||
</div>
|
||||
) : (
|
||||
<div className="space-y-2">
|
||||
{entries.map((entry: KvEntry) => {
|
||||
const isExpanded = expandedKey === entry.key
|
||||
const dataPreview = JSON.stringify(entry.data)
|
||||
const formattedData = JSON.stringify(entry.data, null, 2)
|
||||
const truncatedPreview = dataPreview.length > 100
|
||||
? dataPreview.slice(0, 100) + '...'
|
||||
: dataPreview
|
||||
|
||||
return (
|
||||
<div
|
||||
key={entry.key}
|
||||
className="p-4 rounded-lg border border-border bg-card hover:bg-accent/50 transition-colors"
|
||||
>
|
||||
<div className="flex items-center justify-between gap-2 mb-2">
|
||||
<div className="flex items-center gap-2 flex-wrap min-w-0 flex-1">
|
||||
<Button
|
||||
variant="ghost"
|
||||
size="icon"
|
||||
className="h-6 w-6 p-0"
|
||||
onClick={() => toggleExpand(entry.key)}
|
||||
>
|
||||
{isExpanded ? (
|
||||
<ChevronDown className="w-4 h-4" />
|
||||
) : (
|
||||
<ChevronRight className="w-4 h-4" />
|
||||
)}
|
||||
</Button>
|
||||
<span className="font-medium truncate">{entry.key}</span>
|
||||
<Badge
|
||||
variant="outline"
|
||||
className="text-xs bg-amber-600/20 text-amber-400 border-amber-600/40"
|
||||
>
|
||||
{formatTimeRemaining(entry.expiresAt)}
|
||||
</Badge>
|
||||
</div>
|
||||
<div className="flex gap-1">
|
||||
<Button
|
||||
variant="ghost"
|
||||
size="icon"
|
||||
className="h-8 w-8"
|
||||
onClick={() => handleEdit(entry)}
|
||||
>
|
||||
<Key className="w-4 h-4" />
|
||||
</Button>
|
||||
<Button
|
||||
variant="ghost"
|
||||
size="icon"
|
||||
className="h-8 w-8 text-destructive"
|
||||
onClick={() => setDeleteKey(entry.key)}
|
||||
>
|
||||
<Trash2 className="w-4 h-4" />
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
<div className="text-sm text-muted-foreground font-mono break-all">
|
||||
{truncatedPreview}
|
||||
</div>
|
||||
{isExpanded && (
|
||||
<pre className="mt-2 p-2 bg-muted rounded text-xs overflow-auto max-h-64">
|
||||
{formattedData}
|
||||
</pre>
|
||||
)}
|
||||
<p className="text-xs text-muted-foreground mt-2">
|
||||
Updated: {new Date(entry.updatedAt).toLocaleString()}
|
||||
</p>
|
||||
</div>
|
||||
)
|
||||
})}
|
||||
</div>
|
||||
)}
|
||||
|
||||
<DeleteDialog
|
||||
open={deleteKey !== null}
|
||||
onOpenChange={(open: boolean) => !open && setDeleteKey(null)}
|
||||
onConfirm={handleDelete}
|
||||
onCancel={() => setDeleteKey(null)}
|
||||
title="Delete KV Entry"
|
||||
description="Are you sure you want to delete this KV entry? This action cannot be undone."
|
||||
itemName={deleteKey || undefined}
|
||||
isDeleting={deleteMutation.isPending}
|
||||
/>
|
||||
|
||||
<KvFormDialog
|
||||
entry={editingEntry}
|
||||
projectId={projectId}
|
||||
open={dialogOpen}
|
||||
onOpenChange={setDialogOpen}
|
||||
/>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
@@ -54,6 +54,7 @@ interface PromptInputProps {
|
||||
opcodeUrl: string
|
||||
directory?: string
|
||||
sessionID: string
|
||||
repoId?: number
|
||||
disabled?: boolean
|
||||
showScrollButton?: boolean
|
||||
hasActiveStream?: boolean
|
||||
@@ -69,7 +70,8 @@ interface PromptInputProps {
|
||||
export const PromptInput = memo(forwardRef<PromptInputHandle, PromptInputProps>(function PromptInput({
|
||||
opcodeUrl,
|
||||
directory,
|
||||
sessionID,
|
||||
sessionID,
|
||||
repoId,
|
||||
disabled,
|
||||
showScrollButton,
|
||||
hasActiveStream = false,
|
||||
@@ -139,7 +141,7 @@ export const PromptInput = memo(forwardRef<PromptInputHandle, PromptInputProps>(
|
||||
}), [imageAttachments, clearSTT, isRecording, abortRecording])
|
||||
const sendPrompt = useSendPrompt(opcodeUrl, directory)
|
||||
const sendShell = useSendShell(opcodeUrl, directory)
|
||||
const abortSession = useAbortSession(opcodeUrl, directory, sessionID)
|
||||
const abortSession = useAbortSession(opcodeUrl, directory, sessionID, repoId)
|
||||
const { filterCommands } = useCommands(opcodeUrl)
|
||||
const { executeCommand } = useCommandHandler({
|
||||
opcodeUrl,
|
||||
|
||||
Reference in new issue
Block a user