Browse Source

feat: add MCP server configuration and enhance scheduler task modal with advanced settings

main
npmrun 4 weeks ago
parent
commit
a1eeb0683f
  1. 12
      .mcp.json
  2. 6
      AGENTS.md
  3. 174
      app/components/SchedulerTaskModal.vue
  4. 6
      app/pages/admin/scheduler/[id]/index.vue
  5. 82
      app/pages/admin/scheduler/index.vue
  6. BIN
      packages/drizzle-pkg/db.sqlite
  7. 9
      server/scheduler/engine.ts

12
.mcp.json

@ -0,0 +1,12 @@
{
"mcpServers": {
"nuxt-remote": {
"type": "http",
"url": "https://nuxt.com/mcp"
},
"nuxt-ui-remote": {
"type": "http",
"url": "https://ui.nuxt.com/mcp"
}
}
}

6
AGENTS.md

@ -1,4 +1,4 @@
## 开发页面时必须加载
## 开发页面时必须加载如下mcp
- nuxt-ui
- nuxt
- nuxt-remote
- nuxt-ui-remote

174
app/components/SchedulerTaskModal.vue

@ -1,5 +1,6 @@
<script setup lang="ts">
import type { PropType } from "vue"
import type { SelectItem } from "@nuxt/ui"
const props = defineProps({
task: { type: Object as PropType<any>, default: null },
@ -29,6 +30,23 @@ const form = reactive({
const saving = ref(false)
const error = ref("")
const showAdvanced = ref(false)
const typeItems: SelectItem[] = [
{ label: "Function", value: "function" },
{ label: "HTTP Request", value: "http" },
]
const httpMethodItems: SelectItem[] = [
{ label: "GET", value: "GET" },
{ label: "POST", value: "POST" },
{ label: "PUT", value: "PUT" },
{ label: "DELETE", value: "DELETE" },
]
const functionItems = computed<SelectItem[]>(() =>
props.registeredFunctions.map((f) => ({ label: f, value: f }))
)
const cronPresets = [
{ label: "Every minute", value: "* * * * *" },
@ -68,37 +86,51 @@ async function save() {
saving.value = false
}
}
function onOverlayClick(e: MouseEvent) {
if ((e.target as HTMLElement).dataset.overlay === "true") {
emit("close")
}
}
</script>
<template>
<UModal :open="true" @close="emit('close')">
<template #header>
<div
data-overlay="true"
class="fixed inset-0 z-50 flex items-start justify-center pt-[10vh] bg-black/40"
@click="onOverlayClick"
>
<div class="bg-white rounded-xl shadow-xl w-full max-w-lg max-h-[80vh] overflow-y-auto">
<!-- Header -->
<div class="flex items-center justify-between px-6 py-4 border-b">
<h2 class="text-lg font-semibold">{{ isEdit ? 'Edit' : 'Create' }} Task</h2>
</template>
<UButton
variant="ghost"
color="neutral"
size="sm"
icon="i-lucide-x"
square
@click="emit('close')"
/>
</div>
<div class="space-y-4 p-4">
<UAlert v-if="error" color="red" :title="error" />
<!-- Body -->
<div class="space-y-4 p-6">
<div v-if="error" class="bg-red-50 text-red-700 rounded-lg px-4 py-3 text-sm">
{{ error }}
</div>
<div>
<label class="block text-sm font-medium mb-1">Name</label>
<UFormField label="Name" required>
<UInput v-model="form.name" placeholder="Task name" />
</div>
</UFormField>
<div>
<label class="block text-sm font-medium mb-1">Type</label>
<USelect
v-model="form.type"
:options="[
{ label: 'Function', value: 'function' },
{ label: 'HTTP Request', value: 'http' },
]"
/>
</div>
<UFormField label="Type" required>
<USelect v-model="form.type" :items="typeItems" />
</UFormField>
<div>
<label class="block text-sm font-medium mb-1">Cron Expression</label>
<UFormField label="Cron Expression" required>
<UInput v-model="form.cronExpression" placeholder="0 9 * * *" />
<div class="flex gap-1 mt-1 flex-wrap">
<div class="flex gap-1 mt-1.5 flex-wrap">
<UButton
v-for="preset in cronPresets"
:key="preset.value"
@ -109,86 +141,80 @@ async function save() {
{{ preset.label }}
</UButton>
</div>
</div>
</UFormField>
<!-- Function fields -->
<template v-if="form.type === 'function'">
<div>
<label class="block text-sm font-medium mb-1">Function</label>
<UFormField label="Function" required>
<USelect
v-model="form.functionName"
:options="registeredFunctions.map((f: string) => ({ label: f, value: f }))"
:items="functionItems"
placeholder="Select function"
/>
</div>
<div>
<label class="block text-sm font-medium mb-1">Payload (JSON)</label>
</UFormField>
<UFormField label="Payload (JSON)">
<UInput v-model="form.functionPayload" placeholder='{"key": "value"}' />
</div>
</UFormField>
</template>
<!-- HTTP fields -->
<template v-if="form.type === 'http'">
<div class="grid grid-cols-4 gap-2">
<div class="col-span-1">
<label class="block text-sm font-medium mb-1">Method</label>
<USelect
v-model="form.httpMethod"
:options="['GET','POST','PUT','DELETE'].map(m => ({ label: m, value: m }))"
/>
</div>
<div class="col-span-3">
<label class="block text-sm font-medium mb-1">URL</label>
<div class="grid grid-cols-4 gap-3">
<UFormField label="Method" class="col-span-1">
<USelect v-model="form.httpMethod" :items="httpMethodItems" />
</UFormField>
<UFormField label="URL" required class="col-span-3">
<UInput v-model="form.httpUrl" placeholder="https://example.com/api" />
</UFormField>
</div>
</div>
<div>
<label class="block text-sm font-medium mb-1">Headers (JSON)</label>
<UFormField label="Headers (JSON)">
<UInput v-model="form.httpHeaders" placeholder='{"Authorization": "Bearer ..."}' />
</div>
<div>
<label class="block text-sm font-medium mb-1">Body</label>
<UInput v-model="form.httpBody" />
</div>
</UFormField>
<UFormField label="Body">
<UTextarea v-model="form.httpBody" :rows="3" />
</UFormField>
</template>
<!-- Advanced settings -->
<details class="border rounded-lg p-3">
<summary class="text-sm font-medium cursor-pointer">Advanced</summary>
<div class="space-y-3 mt-3">
<label class="flex items-center gap-2">
<input type="checkbox" v-model="form.catchUp" />
<span class="text-sm">Catch up missed executions after restart</span>
</label>
<label class="flex items-center gap-2">
<input type="checkbox" v-model="form.enabled" />
<span class="text-sm">Enabled</span>
</label>
<div class="grid grid-cols-3 gap-3">
<div>
<label class="block text-xs font-medium mb-1">Max Retries</label>
<UInput v-model.number="form.maxRetries" type="number" min="0" />
</div>
<div>
<label class="block text-xs font-medium mb-1">Retry Delay (s)</label>
<UInput v-model.number="form.retryDelaySeconds" type="number" min="1" />
<div class="border rounded-lg">
<button
type="button"
class="w-full flex items-center justify-between px-4 py-2.5 text-sm font-medium hover:bg-gray-50 rounded-lg transition-colors"
@click="showAdvanced = !showAdvanced"
>
<span>Advanced</span>
<UIcon
:name="showAdvanced ? 'i-lucide-chevron-up' : 'i-lucide-chevron-down'"
class="size-4 text-gray-400"
/>
</button>
<div v-if="showAdvanced" class="space-y-4 px-4 pb-4 border-t pt-4">
<div class="flex flex-col gap-3">
<UCheckbox v-model="form.catchUp" label="Catch up missed executions after restart" />
<UCheckbox v-model="form.enabled" label="Enabled" />
</div>
<div>
<label class="block text-xs font-medium mb-1">Timeout (s)</label>
<UInput v-model.number="form.timeoutSeconds" type="number" min="1" />
<div class="grid grid-cols-3 gap-3">
<UFormField label="Max Retries">
<UInput v-model.number="form.maxRetries" type="number" :min="0" />
</UFormField>
<UFormField label="Retry Delay (s)">
<UInput v-model.number="form.retryDelaySeconds" type="number" :min="1" />
</UFormField>
<UFormField label="Timeout (s)">
<UInput v-model.number="form.timeoutSeconds" type="number" :min="1" />
</UFormField>
</div>
</div>
</div>
</details>
</div>
<template #footer>
<div class="flex justify-end gap-2">
<!-- Footer -->
<div class="flex justify-end gap-2 px-6 py-4 border-t bg-gray-50 rounded-b-xl">
<UButton variant="ghost" @click="emit('close')">Cancel</UButton>
<UButton :loading="saving" @click="save">
{{ isEdit ? 'Update' : 'Create' }}
</UButton>
</div>
</template>
</UModal>
</div>
</div>
</template>

6
app/pages/admin/scheduler/[id]/index.vue

@ -28,8 +28,10 @@ function statusColor(status: string): "success" | "error" | "warning" | "neutral
}
}
function timeAgo(ts: number): string {
const diff = Date.now() - ts
function timeAgo(ts: number | string): string {
const t = typeof ts === "string" ? new Date(ts).getTime() : ts
const diff = Date.now() - t
if (isNaN(diff)) return ""
const mins = Math.floor(diff / 60000)
if (mins < 1) return "just now"
if (mins < 60) return `${mins}m ago`

82
app/pages/admin/scheduler/index.vue

@ -22,14 +22,6 @@ const statsData = computed(() => (stats.data.value ?? {}) as {
const showCreateModal = ref(false)
const editingTask = ref<TaskRow | null>(null)
const columns: any[] = [
{ key: "name", label: "Name" },
{ key: "cronExpression", label: "Cron" },
{ key: "type", label: "Type" },
{ key: "enabled", label: "Status" },
{ key: "actions", label: "" },
]
function statusBadge(enabled: number) {
return enabled
? { label: "Active", color: "success" as const }
@ -98,42 +90,52 @@ function onModalClose() {
</div>
<!-- Task table -->
<div class="rounded-lg border">
<UTable :rows="taskList" :columns="columns">
<template #enabled-data="{ row }">
<UBadge :color="statusBadge((row as unknown as TaskRow).enabled).color" variant="subtle">
{{ statusBadge((row as unknown as TaskRow).enabled).label }}
<div class="rounded-lg border overflow-hidden">
<table class="w-full">
<thead>
<tr class="bg-gray-50 border-b">
<th class="text-left px-4 py-3 text-sm font-medium text-gray-500">Name</th>
<th class="text-left px-4 py-3 text-sm font-medium text-gray-500">Cron</th>
<th class="text-left px-4 py-3 text-sm font-medium text-gray-500">Type</th>
<th class="text-left px-4 py-3 text-sm font-medium text-gray-500">Status</th>
<th class="text-right px-4 py-3 text-sm font-medium text-gray-500">Actions</th>
</tr>
</thead>
<tbody>
<tr v-if="taskList.length === 0">
<td colspan="5" class="text-center px-4 py-8 text-sm text-gray-400">No tasks yet</td>
</tr>
<tr
v-for="t in taskList"
:key="t.id"
class="border-b last:border-b-0 hover:bg-gray-50"
>
<td class="px-4 py-3 text-sm">{{ t.name }}</td>
<td class="px-4 py-3 text-sm font-mono">{{ t.cronExpression }}</td>
<td class="px-4 py-3 text-sm">
<UBadge :color="t.type === 'function' ? 'info' : 'primary'" variant="subtle" size="sm">
{{ t.type }}
</UBadge>
</template>
<template #actions-data="{ row: r }">
</td>
<td class="px-4 py-3">
<UBadge :color="statusBadge(t.enabled).color" variant="subtle" size="sm">
{{ statusBadge(t.enabled).label }}
</UBadge>
</td>
<td class="px-4 py-3">
<div class="flex gap-1 justify-end">
<UButton
size="xs"
variant="ghost"
@click="handleTrigger((r as unknown as TaskRow).id)"
>Trigger</UButton>
<UButton
size="xs"
variant="ghost"
@click="handleToggle((r as unknown as TaskRow).id, !(r as unknown as TaskRow).enabled)"
>
{{ (r as unknown as TaskRow).enabled ? "Pause" : "Resume" }}
<UButton size="xs" variant="ghost" @click="handleTrigger(t.id)">Trigger</UButton>
<UButton size="xs" variant="ghost" @click="handleToggle(t.id, !t.enabled)">
{{ t.enabled ? 'Pause' : 'Resume' }}
</UButton>
<UButton
size="xs"
variant="ghost"
:to="`/admin/scheduler/${(r as unknown as TaskRow).id}`"
>Detail</UButton>
<UButton size="xs" variant="ghost" @click="openEdit(r as unknown as TaskRow)">Edit</UButton>
<UButton
size="xs"
variant="ghost"
color="error"
@click="handleDelete((r as unknown as TaskRow).id)"
>Delete</UButton>
<UButton size="xs" variant="ghost" :to="`/admin/scheduler/${t.id}`">Detail</UButton>
<UButton size="xs" variant="ghost" @click="openEdit(t)">Edit</UButton>
<UButton size="xs" variant="ghost" color="error" @click="handleDelete(t.id)">Delete</UButton>
</div>
</template>
</UTable>
</td>
</tr>
</tbody>
</table>
</div>
<!-- Create/Edit Modal -->

BIN
packages/drizzle-pkg/db.sqlite

Binary file not shown.

9
server/scheduler/engine.ts

@ -133,10 +133,13 @@ async function executeHttpTask(task: TaskRow): Promise<void> {
const timeoutSignal = AbortSignal.timeout(task.timeoutSeconds * 1000);
const method = (task.httpMethod ?? "GET").toUpperCase();
const hasBody = ["POST", "PUT", "PATCH", "DELETE"].includes(method);
const response = await fetch(task.httpUrl, {
method: (task.httpMethod ?? "GET").toUpperCase(),
headers: { "Content-Type": "application/json", ...headers },
body: task.httpBody ?? undefined,
method,
headers: hasBody ? { "Content-Type": "application/json", ...headers } : headers,
body: hasBody ? (task.httpBody ?? undefined) : undefined,
signal: timeoutSignal,
});

Loading…
Cancel
Save