mirror of
https://github.com/catlog22/Claude-Code-Workflow.git
synced 2026-02-12 02:37:45 +08:00
feat: add orchestrator execution engine, observability panel, and LSP document caching
Wire FlowExecutor into orchestrator routes for actual flow execution with
pause/resume/stop lifecycle management. Add CLI session audit system with
audit-routes backend and Observability tab in IssueHub frontend. Introduce
cli-session-mux for cross-workspace session routing and QueueSendToOrchestrator
UI component. Normalize frontend API response handling for { data: ... }
wrapper format and propagate projectPath through flow hooks.
In codex-lens, add per-server opened-document cache in StandaloneLspManager
to avoid redundant didOpen notifications (using didChange for updates), and
skip warmup delay for already-warmed LSP server instances in ChainSearchEngine.
This commit is contained in:
@@ -4,9 +4,9 @@
|
||||
// Dynamic header component for IssueHub
|
||||
|
||||
import { useIntl } from 'react-intl';
|
||||
import { AlertCircle, Radar, ListTodo, LayoutGrid } from 'lucide-react';
|
||||
import { AlertCircle, Radar, ListTodo, LayoutGrid, Activity } from 'lucide-react';
|
||||
|
||||
type IssueTab = 'issues' | 'board' | 'queue' | 'discovery';
|
||||
type IssueTab = 'issues' | 'board' | 'queue' | 'discovery' | 'observability';
|
||||
|
||||
interface IssueHubHeaderProps {
|
||||
currentTab: IssueTab;
|
||||
@@ -37,6 +37,11 @@ export function IssueHubHeader({ currentTab }: IssueHubHeaderProps) {
|
||||
title: formatMessage({ id: 'issues.discovery.pageTitle' }),
|
||||
description: formatMessage({ id: 'issues.discovery.description' }),
|
||||
},
|
||||
observability: {
|
||||
icon: <Activity className="w-6 h-6 text-primary" />,
|
||||
title: formatMessage({ id: 'issues.observability.pageTitle' }),
|
||||
description: formatMessage({ id: 'issues.observability.description' }),
|
||||
},
|
||||
};
|
||||
|
||||
const config = tabConfig[currentTab];
|
||||
|
||||
@@ -7,7 +7,8 @@ import { useIntl } from 'react-intl';
|
||||
import { Button } from '@/components/ui/Button';
|
||||
import { cn } from '@/lib/utils';
|
||||
|
||||
export type IssueTab = 'issues' | 'board' | 'queue' | 'discovery';
|
||||
// Keep in sync with IssueHubHeader/IssueHubPage
|
||||
export type IssueTab = 'issues' | 'board' | 'queue' | 'discovery' | 'observability';
|
||||
|
||||
interface IssueHubTabsProps {
|
||||
currentTab: IssueTab;
|
||||
@@ -22,6 +23,7 @@ export function IssueHubTabs({ currentTab, onTabChange }: IssueHubTabsProps) {
|
||||
{ value: 'board', label: formatMessage({ id: 'issues.hub.tabs.board' }) },
|
||||
{ value: 'queue', label: formatMessage({ id: 'issues.hub.tabs.queue' }) },
|
||||
{ value: 'discovery', label: formatMessage({ id: 'issues.hub.tabs.discovery' }) },
|
||||
{ value: 'observability', label: formatMessage({ id: 'issues.hub.tabs.observability' }) },
|
||||
];
|
||||
|
||||
return (
|
||||
|
||||
294
ccw/frontend/src/components/issue/hub/ObservabilityPanel.tsx
Normal file
294
ccw/frontend/src/components/issue/hub/ObservabilityPanel.tsx
Normal file
@@ -0,0 +1,294 @@
|
||||
// ========================================
|
||||
// Observability Panel
|
||||
// ========================================
|
||||
// Audit log UI for issue workbench (read-only)
|
||||
|
||||
import { useEffect, useMemo, useState } from 'react';
|
||||
import { useIntl } from 'react-intl';
|
||||
import { ChevronDown, ChevronRight, RefreshCw } from 'lucide-react';
|
||||
import { Card } from '@/components/ui/Card';
|
||||
import { Badge } from '@/components/ui/Badge';
|
||||
import { Button } from '@/components/ui/Button';
|
||||
import { Input } from '@/components/ui/Input';
|
||||
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from '@/components/ui/Select';
|
||||
import { useCliSessionAudit } from '@/hooks';
|
||||
import { cn } from '@/lib/utils';
|
||||
import type { CliSessionAuditEvent, CliSessionAuditEventType } from '@/lib/api';
|
||||
|
||||
const EVENT_TYPES: CliSessionAuditEventType[] = [
|
||||
'session_created',
|
||||
'session_closed',
|
||||
'session_send',
|
||||
'session_execute',
|
||||
'session_resize',
|
||||
'session_share_created',
|
||||
'session_share_revoked',
|
||||
'session_idle_reaped',
|
||||
];
|
||||
|
||||
function badgeVariantForType(type: CliSessionAuditEventType): 'default' | 'secondary' | 'destructive' | 'outline' | 'success' | 'warning' | 'info' {
|
||||
switch (type) {
|
||||
case 'session_created':
|
||||
return 'success';
|
||||
case 'session_closed':
|
||||
case 'session_idle_reaped':
|
||||
return 'secondary';
|
||||
case 'session_execute':
|
||||
return 'info';
|
||||
case 'session_send':
|
||||
case 'session_resize':
|
||||
return 'outline';
|
||||
case 'session_share_created':
|
||||
return 'warning';
|
||||
case 'session_share_revoked':
|
||||
return 'secondary';
|
||||
default:
|
||||
return 'default';
|
||||
}
|
||||
}
|
||||
|
||||
function stableEventKey(ev: CliSessionAuditEvent, index: number): string {
|
||||
return `${ev.timestamp}|${ev.type}|${ev.sessionKey ?? ''}|${index}`;
|
||||
}
|
||||
|
||||
export function ObservabilityPanel() {
|
||||
const { formatMessage } = useIntl();
|
||||
|
||||
const [q, setQ] = useState('');
|
||||
const [sessionKey, setSessionKey] = useState('');
|
||||
const [type, setType] = useState<string>('');
|
||||
const [limit, setLimit] = useState<number>(200);
|
||||
const [offset, setOffset] = useState<number>(0);
|
||||
const [expandedKey, setExpandedKey] = useState<string | null>(null);
|
||||
|
||||
// Reset paging when filters change
|
||||
useEffect(() => {
|
||||
setOffset(0);
|
||||
}, [q, sessionKey, type, limit]);
|
||||
|
||||
const query = useCliSessionAudit({
|
||||
q: q.trim() || undefined,
|
||||
sessionKey: sessionKey.trim() || undefined,
|
||||
type: type ? (type as CliSessionAuditEventType) : undefined,
|
||||
limit,
|
||||
offset,
|
||||
});
|
||||
|
||||
const events = query.data?.data.events ?? [];
|
||||
const total = query.data?.data.total ?? 0;
|
||||
const hasMore = query.data?.data.hasMore ?? false;
|
||||
|
||||
const headerRight = useMemo(() => {
|
||||
const start = total === 0 ? 0 : offset + 1;
|
||||
const end = Math.min(total, offset + limit);
|
||||
return `${start}-${end} / ${total}`;
|
||||
}, [limit, offset, total]);
|
||||
|
||||
const handleRefresh = async () => {
|
||||
try {
|
||||
await query.refetch();
|
||||
} catch (e) {
|
||||
// Errors are surfaced by query.error
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<div className="space-y-4">
|
||||
<Card className="p-4">
|
||||
<div className="flex flex-col gap-3">
|
||||
<div className="flex items-center justify-between gap-3">
|
||||
<div className="text-sm font-semibold text-foreground">
|
||||
{formatMessage({ id: 'issues.observability.audit.title' })}
|
||||
</div>
|
||||
<div className="flex items-center gap-2">
|
||||
<div className="text-xs text-muted-foreground font-mono">
|
||||
{headerRight}
|
||||
</div>
|
||||
<Button
|
||||
variant="outline"
|
||||
size="sm"
|
||||
onClick={handleRefresh}
|
||||
disabled={query.isFetching}
|
||||
className="gap-2"
|
||||
>
|
||||
<RefreshCw className={cn('h-4 w-4', query.isFetching && 'animate-spin')} />
|
||||
{formatMessage({ id: 'common.actions.refresh' })}
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="grid grid-cols-1 md:grid-cols-4 gap-3">
|
||||
<div className="md:col-span-2">
|
||||
<label className="block text-xs font-medium text-muted-foreground mb-1">
|
||||
{formatMessage({ id: 'issues.observability.filters.search' })}
|
||||
</label>
|
||||
<Input
|
||||
value={q}
|
||||
onChange={(e) => setQ(e.target.value)}
|
||||
placeholder={formatMessage({ id: 'issues.observability.filters.searchPlaceholder' })}
|
||||
/>
|
||||
</div>
|
||||
<div>
|
||||
<label className="block text-xs font-medium text-muted-foreground mb-1">
|
||||
{formatMessage({ id: 'issues.observability.filters.sessionKey' })}
|
||||
</label>
|
||||
<Input
|
||||
value={sessionKey}
|
||||
onChange={(e) => setSessionKey(e.target.value)}
|
||||
placeholder={formatMessage({ id: 'issues.observability.filters.sessionKeyPlaceholder' })}
|
||||
/>
|
||||
</div>
|
||||
<div>
|
||||
<label className="block text-xs font-medium text-muted-foreground mb-1">
|
||||
{formatMessage({ id: 'issues.observability.filters.type' })}
|
||||
</label>
|
||||
<Select value={type} onValueChange={(v) => setType(v)}>
|
||||
<SelectTrigger>
|
||||
<SelectValue placeholder={formatMessage({ id: 'issues.observability.filters.typeAll' })} />
|
||||
</SelectTrigger>
|
||||
<SelectContent>
|
||||
<SelectItem value="">{formatMessage({ id: 'issues.observability.filters.typeAll' })}</SelectItem>
|
||||
{EVENT_TYPES.map((t) => (
|
||||
<SelectItem key={t} value={t}>
|
||||
{t}
|
||||
</SelectItem>
|
||||
))}
|
||||
</SelectContent>
|
||||
</Select>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="flex flex-wrap items-center justify-between gap-2">
|
||||
<div className="flex items-center gap-2">
|
||||
<label className="block text-xs font-medium text-muted-foreground">
|
||||
{formatMessage({ id: 'issues.observability.filters.limit' })}
|
||||
</label>
|
||||
<Select value={String(limit)} onValueChange={(v) => setLimit(parseInt(v, 10))}>
|
||||
<SelectTrigger className="w-[140px]">
|
||||
<SelectValue />
|
||||
</SelectTrigger>
|
||||
<SelectContent>
|
||||
{[50, 100, 200, 500, 1000].map((n) => (
|
||||
<SelectItem key={n} value={String(n)}>{n}</SelectItem>
|
||||
))}
|
||||
</SelectContent>
|
||||
</Select>
|
||||
</div>
|
||||
|
||||
<div className="flex items-center gap-2">
|
||||
<Button
|
||||
variant="outline"
|
||||
size="sm"
|
||||
onClick={() => setOffset(Math.max(0, offset - limit))}
|
||||
disabled={offset === 0 || query.isFetching}
|
||||
>
|
||||
{formatMessage({ id: 'common.actions.previous' })}
|
||||
</Button>
|
||||
<Button
|
||||
variant="outline"
|
||||
size="sm"
|
||||
onClick={() => setOffset(offset + limit)}
|
||||
disabled={!hasMore || query.isFetching}
|
||||
>
|
||||
{formatMessage({ id: 'common.actions.next' })}
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</Card>
|
||||
|
||||
{query.error && (
|
||||
<Card className="p-6 border-destructive/50 bg-destructive/5">
|
||||
<div className="text-sm text-destructive">
|
||||
{(query.error as Error).message || formatMessage({ id: 'issues.observability.error' })}
|
||||
</div>
|
||||
</Card>
|
||||
)}
|
||||
|
||||
{!query.isLoading && events.length === 0 && (
|
||||
<Card className="p-10 text-center text-muted-foreground">
|
||||
{formatMessage({ id: 'issues.observability.empty' })}
|
||||
</Card>
|
||||
)}
|
||||
|
||||
{events.length > 0 && (
|
||||
<Card className="p-0 overflow-hidden">
|
||||
<div className="px-4 py-2 border-b border-border bg-card/50 text-xs font-medium text-muted-foreground grid grid-cols-12 gap-3">
|
||||
<div className="col-span-3">{formatMessage({ id: 'issues.observability.table.timestamp' })}</div>
|
||||
<div className="col-span-2">{formatMessage({ id: 'issues.observability.table.type' })}</div>
|
||||
<div className="col-span-3">{formatMessage({ id: 'issues.observability.table.sessionKey' })}</div>
|
||||
<div className="col-span-2">{formatMessage({ id: 'issues.observability.table.tool' })}</div>
|
||||
<div className="col-span-2">{formatMessage({ id: 'issues.observability.table.resumeKey' })}</div>
|
||||
</div>
|
||||
|
||||
<div className="divide-y divide-border">
|
||||
{events.map((ev, index) => {
|
||||
const key = stableEventKey(ev, index);
|
||||
const expanded = expandedKey === key;
|
||||
return (
|
||||
<div key={key} className="px-4 py-2">
|
||||
<button
|
||||
type="button"
|
||||
className="w-full text-left grid grid-cols-12 gap-3 items-center hover:bg-muted/40 rounded-md px-2 py-2"
|
||||
onClick={() => setExpandedKey(expanded ? null : key)}
|
||||
>
|
||||
<div className="col-span-3 flex items-center gap-2 min-w-0">
|
||||
{expanded ? (
|
||||
<ChevronDown className="h-4 w-4 text-muted-foreground shrink-0" />
|
||||
) : (
|
||||
<ChevronRight className="h-4 w-4 text-muted-foreground shrink-0" />
|
||||
)}
|
||||
<span className="font-mono text-xs text-foreground truncate">
|
||||
{ev.timestamp}
|
||||
</span>
|
||||
</div>
|
||||
<div className="col-span-2">
|
||||
<Badge variant={badgeVariantForType(ev.type)} className="font-mono text-xs">
|
||||
{ev.type}
|
||||
</Badge>
|
||||
</div>
|
||||
<div className="col-span-3 font-mono text-xs text-muted-foreground truncate">
|
||||
{ev.sessionKey || '-'}
|
||||
</div>
|
||||
<div className="col-span-2 font-mono text-xs text-muted-foreground truncate">
|
||||
{ev.tool || '-'}
|
||||
</div>
|
||||
<div className="col-span-2 font-mono text-xs text-muted-foreground truncate">
|
||||
{ev.resumeKey || '-'}
|
||||
</div>
|
||||
</button>
|
||||
|
||||
{expanded && (
|
||||
<div className="mt-2 ml-6 space-y-2">
|
||||
<div className="grid grid-cols-1 md:grid-cols-2 gap-2 text-xs">
|
||||
<div className="text-muted-foreground">
|
||||
<span className="font-medium text-foreground">{formatMessage({ id: 'issues.observability.table.workingDir' })}: </span>
|
||||
<span className="font-mono break-all">{ev.workingDir || '-'}</span>
|
||||
</div>
|
||||
<div className="text-muted-foreground">
|
||||
<span className="font-medium text-foreground">{formatMessage({ id: 'issues.observability.table.ip' })}: </span>
|
||||
<span className="font-mono break-all">{ev.ip || '-'}</span>
|
||||
</div>
|
||||
</div>
|
||||
{ev.userAgent && (
|
||||
<div className="text-xs text-muted-foreground">
|
||||
<span className="font-medium text-foreground">{formatMessage({ id: 'issues.observability.table.userAgent' })}: </span>
|
||||
<span className="font-mono break-all">{ev.userAgent}</span>
|
||||
</div>
|
||||
)}
|
||||
<pre className="text-xs bg-muted/50 rounded-md p-3 overflow-x-auto">
|
||||
{JSON.stringify(ev.details ?? {}, null, 2)}
|
||||
</pre>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
})}
|
||||
</div>
|
||||
</Card>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
export default ObservabilityPanel;
|
||||
@@ -0,0 +1,355 @@
|
||||
// ========================================
|
||||
// QueueSendToOrchestrator
|
||||
// ========================================
|
||||
// Create a flow from a queue item and execute it via Orchestrator (tmux-like delivery to PTY session).
|
||||
|
||||
import { useEffect, useMemo, useState } from 'react';
|
||||
import { useIntl } from 'react-intl';
|
||||
import { useNavigate } from 'react-router-dom';
|
||||
import { Plus, RefreshCw, Workflow } from 'lucide-react';
|
||||
import { Button } from '@/components/ui/Button';
|
||||
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from '@/components/ui/Select';
|
||||
import { cn } from '@/lib/utils';
|
||||
import { useWorkflowStore, selectProjectPath } from '@/stores/workflowStore';
|
||||
import { toast, useExecutionStore, useFlowStore } from '@/stores';
|
||||
import { useIssues } from '@/hooks';
|
||||
import {
|
||||
createCliSession,
|
||||
createOrchestratorFlow,
|
||||
executeOrchestratorFlow,
|
||||
fetchCliSessions,
|
||||
type CliSession,
|
||||
type QueueItem,
|
||||
} from '@/lib/api';
|
||||
import { useCliSessionStore } from '@/stores/cliSessionStore';
|
||||
|
||||
type ToolName = 'claude' | 'codex' | 'gemini' | 'qwen';
|
||||
type ResumeStrategy = 'nativeResume' | 'promptConcat';
|
||||
|
||||
function buildQueueItemInstruction(item: QueueItem, issue: any | undefined): string {
|
||||
const lines: string[] = [];
|
||||
lines.push(`Queue Item: ${item.item_id}`);
|
||||
lines.push(`Issue: ${item.issue_id}`);
|
||||
lines.push(`Solution: ${item.solution_id}`);
|
||||
if (item.task_id) lines.push(`Task: ${item.task_id}`);
|
||||
lines.push('');
|
||||
|
||||
if (issue) {
|
||||
if (issue.title) lines.push(`Title: ${issue.title}`);
|
||||
if (issue.context) {
|
||||
lines.push('');
|
||||
lines.push('Context:');
|
||||
lines.push(String(issue.context));
|
||||
}
|
||||
|
||||
const solution = Array.isArray(issue.solutions)
|
||||
? issue.solutions.find((s: any) => s?.id === item.solution_id)
|
||||
: undefined;
|
||||
if (solution) {
|
||||
lines.push('');
|
||||
lines.push('Solution Description:');
|
||||
if (solution.description) lines.push(String(solution.description));
|
||||
if (solution.approach) {
|
||||
lines.push('');
|
||||
lines.push('Approach:');
|
||||
lines.push(String(solution.approach));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
lines.push('');
|
||||
lines.push('Instruction:');
|
||||
lines.push(
|
||||
'Implement the above queue item in this repository. Prefer small, testable changes; run relevant tests; report blockers if any.'
|
||||
);
|
||||
|
||||
return lines.join('\n');
|
||||
}
|
||||
|
||||
function generateId(prefix: string): string {
|
||||
return `${prefix}-${Date.now()}-${Math.random().toString(36).slice(2, 10)}`;
|
||||
}
|
||||
|
||||
export function QueueSendToOrchestrator({ item, className }: { item: QueueItem; className?: string }) {
|
||||
const { formatMessage } = useIntl();
|
||||
const navigate = useNavigate();
|
||||
const projectPath = useWorkflowStore(selectProjectPath);
|
||||
|
||||
const { issues } = useIssues();
|
||||
const issue = useMemo(() => issues.find((i) => i.id === item.issue_id) as any, [issues, item.issue_id]);
|
||||
|
||||
const sessionsByKey = useCliSessionStore((s) => s.sessions);
|
||||
const setSessions = useCliSessionStore((s) => s.setSessions);
|
||||
const upsertSession = useCliSessionStore((s) => s.upsertSession);
|
||||
|
||||
const sessions = useMemo(
|
||||
() => Object.values(sessionsByKey).sort((a, b) => a.createdAt.localeCompare(b.createdAt)),
|
||||
[sessionsByKey]
|
||||
);
|
||||
|
||||
const [selectedSessionKey, setSelectedSessionKey] = useState<string>('');
|
||||
const [tool, setTool] = useState<ToolName>('claude');
|
||||
const [mode, setMode] = useState<'analysis' | 'write'>('write');
|
||||
const [resumeStrategy, setResumeStrategy] = useState<ResumeStrategy>('nativeResume');
|
||||
const [isLoading, setIsLoading] = useState(false);
|
||||
const [isSending, setIsSending] = useState(false);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
const [lastResult, setLastResult] = useState<{ flowId: string; execId: string } | null>(null);
|
||||
|
||||
const refreshSessions = async () => {
|
||||
setIsLoading(true);
|
||||
setError(null);
|
||||
try {
|
||||
const r = await fetchCliSessions(projectPath || undefined);
|
||||
setSessions(r.sessions as unknown as CliSession[]);
|
||||
} catch (e) {
|
||||
setError(e instanceof Error ? e.message : String(e));
|
||||
} finally {
|
||||
setIsLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
void refreshSessions();
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
}, [projectPath]);
|
||||
|
||||
useEffect(() => {
|
||||
if (selectedSessionKey) return;
|
||||
if (sessions.length === 0) return;
|
||||
setSelectedSessionKey(sessions[sessions.length - 1]?.sessionKey ?? '');
|
||||
}, [sessions, selectedSessionKey]);
|
||||
|
||||
const ensureSession = async (): Promise<string> => {
|
||||
if (selectedSessionKey) return selectedSessionKey;
|
||||
if (!projectPath) throw new Error('No project path selected');
|
||||
const created = await createCliSession({
|
||||
workingDir: projectPath,
|
||||
preferredShell: 'bash',
|
||||
resumeKey: item.issue_id,
|
||||
}, projectPath);
|
||||
upsertSession(created.session as unknown as CliSession);
|
||||
setSelectedSessionKey(created.session.sessionKey);
|
||||
return created.session.sessionKey;
|
||||
};
|
||||
|
||||
const handleCreateSession = async () => {
|
||||
setError(null);
|
||||
try {
|
||||
if (!projectPath) throw new Error('No project path selected');
|
||||
const created = await createCliSession({
|
||||
workingDir: projectPath,
|
||||
preferredShell: 'bash',
|
||||
resumeKey: item.issue_id,
|
||||
}, projectPath);
|
||||
upsertSession(created.session as unknown as CliSession);
|
||||
setSelectedSessionKey(created.session.sessionKey);
|
||||
await refreshSessions();
|
||||
} catch (e) {
|
||||
setError(e instanceof Error ? e.message : String(e));
|
||||
}
|
||||
};
|
||||
|
||||
const handleSend = async () => {
|
||||
setIsSending(true);
|
||||
setError(null);
|
||||
setLastResult(null);
|
||||
try {
|
||||
const sessionKey = await ensureSession();
|
||||
const instruction = buildQueueItemInstruction(item, issue);
|
||||
|
||||
const nodeId = generateId('node');
|
||||
const flowName = `Queue ${item.issue_id} / ${item.solution_id}${item.task_id ? ` / ${item.task_id}` : ''}`;
|
||||
const flowDescription = `Queue item ${item.item_id} -> Orchestrator`;
|
||||
|
||||
const created = await createOrchestratorFlow({
|
||||
name: flowName,
|
||||
description: flowDescription,
|
||||
version: '1.0.0',
|
||||
nodes: [
|
||||
{
|
||||
id: nodeId,
|
||||
type: 'prompt-template',
|
||||
position: { x: 100, y: 100 },
|
||||
data: {
|
||||
label: flowName,
|
||||
instruction,
|
||||
tool,
|
||||
mode,
|
||||
delivery: 'sendToSession',
|
||||
targetSessionKey: sessionKey,
|
||||
resumeKey: item.issue_id,
|
||||
resumeStrategy,
|
||||
tags: ['queue', item.item_id, item.issue_id, item.solution_id].filter(Boolean),
|
||||
},
|
||||
},
|
||||
],
|
||||
edges: [],
|
||||
variables: {},
|
||||
metadata: {
|
||||
source: 'local',
|
||||
tags: ['queue', item.item_id, item.issue_id, item.solution_id].filter(Boolean),
|
||||
},
|
||||
}, projectPath || undefined);
|
||||
|
||||
if (!created.success) {
|
||||
throw new Error('Failed to create flow');
|
||||
}
|
||||
|
||||
// Best-effort: hydrate Orchestrator stores so the user lands on the created flow.
|
||||
const flowDto = created.data as any;
|
||||
const parsedVersion = parseInt(String(flowDto.version ?? '1'), 10);
|
||||
const flowForStore = {
|
||||
...flowDto,
|
||||
version: Number.isFinite(parsedVersion) ? parsedVersion : 1,
|
||||
} as any;
|
||||
useFlowStore.getState().setCurrentFlow(flowForStore);
|
||||
|
||||
// Trigger execution (backend returns execId; engine wiring may run async).
|
||||
const executed = await executeOrchestratorFlow(created.data.id, {}, projectPath || undefined);
|
||||
if (!executed.success) {
|
||||
throw new Error('Failed to execute flow');
|
||||
}
|
||||
|
||||
const execId = executed.data.execId;
|
||||
useExecutionStore.getState().startExecution(execId, created.data.id);
|
||||
useExecutionStore.getState().setMonitorPanelOpen(true);
|
||||
|
||||
setLastResult({ flowId: created.data.id, execId });
|
||||
toast.success(
|
||||
formatMessage({ id: 'issues.queue.orchestrator.sentTitle' }),
|
||||
formatMessage({ id: 'issues.queue.orchestrator.sentDesc' }, { flowId: created.data.id })
|
||||
);
|
||||
|
||||
navigate('/orchestrator');
|
||||
} catch (e) {
|
||||
const message = e instanceof Error ? e.message : String(e);
|
||||
setError(message);
|
||||
toast.error(formatMessage({ id: 'issues.queue.orchestrator.sendFailed' }), message);
|
||||
} finally {
|
||||
setIsSending(false);
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<div className={cn('space-y-3', className)}>
|
||||
<div className="flex items-center justify-between gap-2">
|
||||
<h3 className="text-sm font-semibold text-foreground flex items-center gap-2">
|
||||
<Workflow className="h-4 w-4" />
|
||||
{formatMessage({ id: 'issues.queue.orchestrator.title' })}
|
||||
</h3>
|
||||
<div className="flex items-center gap-2">
|
||||
<Button
|
||||
variant="outline"
|
||||
size="sm"
|
||||
onClick={refreshSessions}
|
||||
disabled={isLoading}
|
||||
className="gap-2"
|
||||
>
|
||||
<RefreshCw className={cn('h-4 w-4', isLoading && 'animate-spin')} />
|
||||
{formatMessage({ id: 'issues.terminal.session.refresh' })}
|
||||
</Button>
|
||||
<Button variant="outline" size="sm" onClick={handleCreateSession} className="gap-2">
|
||||
<Plus className="h-4 w-4" />
|
||||
{formatMessage({ id: 'issues.terminal.session.new' })}
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="grid grid-cols-1 md:grid-cols-2 gap-3">
|
||||
<div>
|
||||
<label className="block text-xs font-medium text-muted-foreground mb-1">
|
||||
{formatMessage({ id: 'issues.queue.orchestrator.targetSession' })}
|
||||
</label>
|
||||
<Select value={selectedSessionKey} onValueChange={(v) => setSelectedSessionKey(v)}>
|
||||
<SelectTrigger>
|
||||
<SelectValue placeholder={formatMessage({ id: 'issues.terminal.session.none' })} />
|
||||
</SelectTrigger>
|
||||
<SelectContent>
|
||||
{sessions.length === 0 ? (
|
||||
<SelectItem value="" disabled>
|
||||
{formatMessage({ id: 'issues.terminal.session.none' })}
|
||||
</SelectItem>
|
||||
) : (
|
||||
sessions.map((s) => (
|
||||
<SelectItem key={s.sessionKey} value={s.sessionKey}>
|
||||
{(s.tool || 'cli') + ' · ' + s.sessionKey}
|
||||
</SelectItem>
|
||||
))
|
||||
)}
|
||||
</SelectContent>
|
||||
</Select>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<label className="block text-xs font-medium text-muted-foreground mb-1">
|
||||
{formatMessage({ id: 'issues.queue.orchestrator.tool' })}
|
||||
</label>
|
||||
<Select value={tool} onValueChange={(v) => setTool(v as ToolName)}>
|
||||
<SelectTrigger>
|
||||
<SelectValue />
|
||||
</SelectTrigger>
|
||||
<SelectContent>
|
||||
<SelectItem value="claude">claude</SelectItem>
|
||||
<SelectItem value="codex">codex</SelectItem>
|
||||
<SelectItem value="gemini">gemini</SelectItem>
|
||||
<SelectItem value="qwen">qwen</SelectItem>
|
||||
</SelectContent>
|
||||
</Select>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<label className="block text-xs font-medium text-muted-foreground mb-1">
|
||||
{formatMessage({ id: 'issues.queue.orchestrator.mode' })}
|
||||
</label>
|
||||
<Select value={mode} onValueChange={(v) => setMode(v as 'analysis' | 'write')}>
|
||||
<SelectTrigger>
|
||||
<SelectValue />
|
||||
</SelectTrigger>
|
||||
<SelectContent>
|
||||
<SelectItem value="analysis">analysis</SelectItem>
|
||||
<SelectItem value="write">write</SelectItem>
|
||||
</SelectContent>
|
||||
</Select>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<label className="block text-xs font-medium text-muted-foreground mb-1">
|
||||
{formatMessage({ id: 'issues.queue.orchestrator.resumeStrategy' })}
|
||||
</label>
|
||||
<Select value={resumeStrategy} onValueChange={(v) => setResumeStrategy(v as ResumeStrategy)}>
|
||||
<SelectTrigger>
|
||||
<SelectValue />
|
||||
</SelectTrigger>
|
||||
<SelectContent>
|
||||
<SelectItem value="nativeResume">nativeResume</SelectItem>
|
||||
<SelectItem value="promptConcat">promptConcat</SelectItem>
|
||||
</SelectContent>
|
||||
</Select>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{error && <div className="text-sm text-destructive">{error}</div>}
|
||||
{lastResult && (
|
||||
<div className="text-xs text-muted-foreground font-mono break-all">
|
||||
{lastResult.flowId} · {lastResult.execId}
|
||||
</div>
|
||||
)}
|
||||
|
||||
<div className="flex items-center justify-end">
|
||||
<Button onClick={handleSend} disabled={isSending || !projectPath} className="gap-2">
|
||||
{isSending ? (
|
||||
<>
|
||||
<RefreshCw className="h-4 w-4 animate-spin" />
|
||||
{formatMessage({ id: 'issues.queue.orchestrator.sending' })}
|
||||
</>
|
||||
) : (
|
||||
formatMessage({ id: 'issues.queue.orchestrator.send' })
|
||||
)}
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
export default QueueSendToOrchestrator;
|
||||
@@ -10,6 +10,7 @@ import { Badge } from '@/components/ui/Badge';
|
||||
import { Button } from '@/components/ui/Button';
|
||||
import { Tabs, TabsList, TabsTrigger, TabsContent } from '@/components/ui/Tabs';
|
||||
import { QueueExecuteInSession } from '@/components/issue/queue/QueueExecuteInSession';
|
||||
import { QueueSendToOrchestrator } from '@/components/issue/queue/QueueSendToOrchestrator';
|
||||
import { IssueTerminalTab } from '@/components/issue/hub/IssueTerminalTab';
|
||||
import { useIssueQueue } from '@/hooks';
|
||||
import { cn } from '@/lib/utils';
|
||||
@@ -179,6 +180,9 @@ export function SolutionDrawer({ item, isOpen, onClose }: SolutionDrawerProps) {
|
||||
{/* Execute in Session */}
|
||||
<QueueExecuteInSession item={item} />
|
||||
|
||||
{/* Send to Orchestrator */}
|
||||
<QueueSendToOrchestrator item={item} />
|
||||
|
||||
{/* Dependencies */}
|
||||
{item.depends_on && item.depends_on.length > 0 && (
|
||||
<div>
|
||||
|
||||
@@ -91,6 +91,14 @@ export type {
|
||||
UseIssueDiscoveryReturn,
|
||||
} from './useIssues';
|
||||
|
||||
// ========== Audit ==========
|
||||
export {
|
||||
useCliSessionAudit,
|
||||
} from './useAudit';
|
||||
export type {
|
||||
UseCliSessionAuditOptions,
|
||||
} from './useAudit';
|
||||
|
||||
// ========== Skills ==========
|
||||
export {
|
||||
useSkills,
|
||||
|
||||
57
ccw/frontend/src/hooks/useAudit.ts
Normal file
57
ccw/frontend/src/hooks/useAudit.ts
Normal file
@@ -0,0 +1,57 @@
|
||||
// ========================================
|
||||
// useAudit Hooks
|
||||
// ========================================
|
||||
// TanStack Query hooks for audit/observability APIs
|
||||
|
||||
import { useQuery, type UseQueryResult } from '@tanstack/react-query';
|
||||
import { useWorkflowStore, selectProjectPath } from '@/stores/workflowStore';
|
||||
import { workspaceQueryKeys } from '@/lib/queryKeys';
|
||||
import {
|
||||
fetchCliSessionAudit,
|
||||
type CliSessionAuditEventType,
|
||||
type CliSessionAuditListResponse,
|
||||
} from '@/lib/api';
|
||||
|
||||
export interface UseCliSessionAuditOptions {
|
||||
sessionKey?: string;
|
||||
type?: CliSessionAuditEventType | CliSessionAuditEventType[];
|
||||
q?: string;
|
||||
limit?: number;
|
||||
offset?: number;
|
||||
enabled?: boolean;
|
||||
}
|
||||
|
||||
export function useCliSessionAudit(
|
||||
options: UseCliSessionAuditOptions = {}
|
||||
): UseQueryResult<{ success: boolean; data: CliSessionAuditListResponse }> {
|
||||
const projectPath = useWorkflowStore(selectProjectPath);
|
||||
const enabled = (options.enabled ?? true) && !!projectPath;
|
||||
|
||||
const typeParam = Array.isArray(options.type)
|
||||
? options.type.join(',')
|
||||
: options.type;
|
||||
|
||||
return useQuery({
|
||||
queryKey: projectPath
|
||||
? workspaceQueryKeys.cliSessionAudit(projectPath, {
|
||||
sessionKey: options.sessionKey,
|
||||
type: typeParam,
|
||||
q: options.q,
|
||||
limit: options.limit,
|
||||
offset: options.offset,
|
||||
})
|
||||
: ['audit', 'cliSessions', 'no-project'],
|
||||
queryFn: () => fetchCliSessionAudit({
|
||||
projectPath: projectPath ?? undefined,
|
||||
sessionKey: options.sessionKey,
|
||||
type: options.type,
|
||||
q: options.q,
|
||||
limit: options.limit,
|
||||
offset: options.offset,
|
||||
}),
|
||||
enabled,
|
||||
staleTime: 10_000,
|
||||
retry: 1,
|
||||
});
|
||||
}
|
||||
|
||||
@@ -5,10 +5,18 @@
|
||||
|
||||
import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query';
|
||||
import type { Flow } from '../types/flow';
|
||||
import { useWorkflowStore, selectProjectPath } from '@/stores/workflowStore';
|
||||
|
||||
// API base URL
|
||||
const API_BASE = '/api/orchestrator';
|
||||
|
||||
function withPath(url: string, projectPath?: string | null): string {
|
||||
const p = typeof projectPath === 'string' ? projectPath.trim() : '';
|
||||
if (!p) return url;
|
||||
const sep = url.includes('?') ? '&' : '?';
|
||||
return `${url}${sep}path=${encodeURIComponent(p)}`;
|
||||
}
|
||||
|
||||
// Query keys
|
||||
export const flowKeys = {
|
||||
all: ['flows'] as const,
|
||||
@@ -30,32 +38,36 @@ interface FlowsListResponse {
|
||||
interface ExecutionStartResponse {
|
||||
execId: string;
|
||||
flowId: string;
|
||||
status: 'running';
|
||||
status: 'pending' | 'running' | 'paused' | 'completed' | 'failed';
|
||||
startedAt: string;
|
||||
}
|
||||
|
||||
interface ExecutionControlResponse {
|
||||
execId: string;
|
||||
status: 'paused' | 'running' | 'stopped';
|
||||
status: 'pending' | 'running' | 'paused' | 'completed' | 'failed';
|
||||
message: string;
|
||||
}
|
||||
|
||||
// ========== Fetch Functions ==========
|
||||
|
||||
async function fetchFlows(): Promise<FlowsListResponse> {
|
||||
const response = await fetch(`${API_BASE}/flows`);
|
||||
const response = await fetch(`${API_BASE}/flows`, { credentials: 'same-origin' });
|
||||
if (!response.ok) {
|
||||
throw new Error(`Failed to fetch flows: ${response.statusText}`);
|
||||
}
|
||||
return response.json();
|
||||
const json = await response.json();
|
||||
const flows = Array.isArray(json?.data) ? json.data : (json?.flows || []);
|
||||
const total = typeof json?.total === 'number' ? json.total : flows.length;
|
||||
return { flows, total };
|
||||
}
|
||||
|
||||
async function fetchFlow(id: string): Promise<Flow> {
|
||||
const response = await fetch(`${API_BASE}/flows/${id}`);
|
||||
const response = await fetch(`${API_BASE}/flows/${id}`, { credentials: 'same-origin' });
|
||||
if (!response.ok) {
|
||||
throw new Error(`Failed to fetch flow: ${response.statusText}`);
|
||||
}
|
||||
return response.json();
|
||||
const json = await response.json();
|
||||
return (json && typeof json === 'object' && 'data' in json) ? json.data : json;
|
||||
}
|
||||
|
||||
async function createFlow(flow: Omit<Flow, 'id' | 'created_at' | 'updated_at'>): Promise<Flow> {
|
||||
@@ -63,11 +75,13 @@ async function createFlow(flow: Omit<Flow, 'id' | 'created_at' | 'updated_at'>):
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify(flow),
|
||||
credentials: 'same-origin',
|
||||
});
|
||||
if (!response.ok) {
|
||||
throw new Error(`Failed to create flow: ${response.statusText}`);
|
||||
}
|
||||
return response.json();
|
||||
const json = await response.json();
|
||||
return (json && typeof json === 'object' && 'data' in json) ? json.data : json;
|
||||
}
|
||||
|
||||
async function updateFlow(id: string, flow: Partial<Flow>): Promise<Flow> {
|
||||
@@ -75,16 +89,19 @@ async function updateFlow(id: string, flow: Partial<Flow>): Promise<Flow> {
|
||||
method: 'PUT',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify(flow),
|
||||
credentials: 'same-origin',
|
||||
});
|
||||
if (!response.ok) {
|
||||
throw new Error(`Failed to update flow: ${response.statusText}`);
|
||||
}
|
||||
return response.json();
|
||||
const json = await response.json();
|
||||
return (json && typeof json === 'object' && 'data' in json) ? json.data : json;
|
||||
}
|
||||
|
||||
async function deleteFlow(id: string): Promise<void> {
|
||||
const response = await fetch(`${API_BASE}/flows/${id}`, {
|
||||
method: 'DELETE',
|
||||
credentials: 'same-origin',
|
||||
});
|
||||
if (!response.ok) {
|
||||
throw new Error(`Failed to delete flow: ${response.statusText}`);
|
||||
@@ -94,53 +111,72 @@ async function deleteFlow(id: string): Promise<void> {
|
||||
async function duplicateFlow(id: string): Promise<Flow> {
|
||||
const response = await fetch(`${API_BASE}/flows/${id}/duplicate`, {
|
||||
method: 'POST',
|
||||
credentials: 'same-origin',
|
||||
});
|
||||
if (!response.ok) {
|
||||
throw new Error(`Failed to duplicate flow: ${response.statusText}`);
|
||||
}
|
||||
return response.json();
|
||||
const json = await response.json();
|
||||
return (json && typeof json === 'object' && 'data' in json) ? json.data : json;
|
||||
}
|
||||
|
||||
// ========== Execution Functions ==========
|
||||
|
||||
async function executeFlow(flowId: string): Promise<ExecutionStartResponse> {
|
||||
const response = await fetch(`${API_BASE}/flows/${flowId}/execute`, {
|
||||
async function executeFlow(flowId: string, projectPath?: string | null): Promise<ExecutionStartResponse> {
|
||||
const response = await fetch(withPath(`${API_BASE}/flows/${flowId}/execute`, projectPath), {
|
||||
method: 'POST',
|
||||
credentials: 'same-origin',
|
||||
});
|
||||
if (!response.ok) {
|
||||
throw new Error(`Failed to execute flow: ${response.statusText}`);
|
||||
}
|
||||
return response.json();
|
||||
const json = await response.json();
|
||||
return (json && typeof json === 'object' && 'data' in json) ? json.data : json;
|
||||
}
|
||||
|
||||
async function pauseExecution(execId: string): Promise<ExecutionControlResponse> {
|
||||
const response = await fetch(`${API_BASE}/executions/${execId}/pause`, {
|
||||
async function pauseExecution(execId: string, projectPath?: string | null): Promise<ExecutionControlResponse> {
|
||||
const response = await fetch(withPath(`${API_BASE}/executions/${execId}/pause`, projectPath), {
|
||||
method: 'POST',
|
||||
credentials: 'same-origin',
|
||||
});
|
||||
if (!response.ok) {
|
||||
throw new Error(`Failed to pause execution: ${response.statusText}`);
|
||||
}
|
||||
return response.json();
|
||||
const json = await response.json();
|
||||
if (json?.data?.id) {
|
||||
return { execId: json.data.id, status: json.data.status, message: json.message || 'Execution paused' };
|
||||
}
|
||||
return json;
|
||||
}
|
||||
|
||||
async function resumeExecution(execId: string): Promise<ExecutionControlResponse> {
|
||||
const response = await fetch(`${API_BASE}/executions/${execId}/resume`, {
|
||||
async function resumeExecution(execId: string, projectPath?: string | null): Promise<ExecutionControlResponse> {
|
||||
const response = await fetch(withPath(`${API_BASE}/executions/${execId}/resume`, projectPath), {
|
||||
method: 'POST',
|
||||
credentials: 'same-origin',
|
||||
});
|
||||
if (!response.ok) {
|
||||
throw new Error(`Failed to resume execution: ${response.statusText}`);
|
||||
}
|
||||
return response.json();
|
||||
const json = await response.json();
|
||||
if (json?.data?.id) {
|
||||
return { execId: json.data.id, status: json.data.status, message: json.message || 'Execution resumed' };
|
||||
}
|
||||
return json;
|
||||
}
|
||||
|
||||
async function stopExecution(execId: string): Promise<ExecutionControlResponse> {
|
||||
const response = await fetch(`${API_BASE}/executions/${execId}/stop`, {
|
||||
async function stopExecution(execId: string, projectPath?: string | null): Promise<ExecutionControlResponse> {
|
||||
const response = await fetch(withPath(`${API_BASE}/executions/${execId}/stop`, projectPath), {
|
||||
method: 'POST',
|
||||
credentials: 'same-origin',
|
||||
});
|
||||
if (!response.ok) {
|
||||
throw new Error(`Failed to stop execution: ${response.statusText}`);
|
||||
}
|
||||
return response.json();
|
||||
const json = await response.json();
|
||||
if (json?.data?.id) {
|
||||
return { execId: json.data.id, status: json.data.status, message: json.message || 'Execution stopped' };
|
||||
}
|
||||
return json;
|
||||
}
|
||||
|
||||
// ========== Query Hooks ==========
|
||||
@@ -265,8 +301,9 @@ export function useDuplicateFlow() {
|
||||
* Execute a flow
|
||||
*/
|
||||
export function useExecuteFlow() {
|
||||
const projectPath = useWorkflowStore(selectProjectPath);
|
||||
return useMutation({
|
||||
mutationFn: executeFlow,
|
||||
mutationFn: (flowId: string) => executeFlow(flowId, projectPath),
|
||||
});
|
||||
}
|
||||
|
||||
@@ -274,8 +311,9 @@ export function useExecuteFlow() {
|
||||
* Pause execution
|
||||
*/
|
||||
export function usePauseExecution() {
|
||||
const projectPath = useWorkflowStore(selectProjectPath);
|
||||
return useMutation({
|
||||
mutationFn: pauseExecution,
|
||||
mutationFn: (execId: string) => pauseExecution(execId, projectPath),
|
||||
});
|
||||
}
|
||||
|
||||
@@ -283,8 +321,9 @@ export function usePauseExecution() {
|
||||
* Resume execution
|
||||
*/
|
||||
export function useResumeExecution() {
|
||||
const projectPath = useWorkflowStore(selectProjectPath);
|
||||
return useMutation({
|
||||
mutationFn: resumeExecution,
|
||||
mutationFn: (execId: string) => resumeExecution(execId, projectPath),
|
||||
});
|
||||
}
|
||||
|
||||
@@ -292,15 +331,19 @@ export function useResumeExecution() {
|
||||
* Stop execution
|
||||
*/
|
||||
export function useStopExecution() {
|
||||
const projectPath = useWorkflowStore(selectProjectPath);
|
||||
return useMutation({
|
||||
mutationFn: stopExecution,
|
||||
mutationFn: (execId: string) => stopExecution(execId, projectPath),
|
||||
});
|
||||
}
|
||||
|
||||
// ========== Execution Monitoring Fetch Functions ==========
|
||||
|
||||
async function fetchExecutionStateById(execId: string): Promise<{ success: boolean; data: { execId: string; flowId: string; status: string; currentNodeId?: string; startedAt: string; completedAt?: string; elapsedMs: number } }> {
|
||||
const response = await fetch(`${API_BASE}/executions/${execId}`);
|
||||
async function fetchExecutionStateById(
|
||||
execId: string,
|
||||
projectPath?: string | null
|
||||
): Promise<{ success: boolean; data: { execId: string; flowId: string; status: string; currentNodeId?: string; startedAt: string; completedAt?: string; elapsedMs: number } }> {
|
||||
const response = await fetch(withPath(`${API_BASE}/executions/${execId}`, projectPath), { credentials: 'same-origin' });
|
||||
if (!response.ok) {
|
||||
throw new Error(`Failed to fetch execution state: ${response.statusText}`);
|
||||
}
|
||||
@@ -314,7 +357,8 @@ async function fetchExecutionLogsById(
|
||||
offset?: number;
|
||||
level?: string;
|
||||
nodeId?: string;
|
||||
}
|
||||
},
|
||||
projectPath?: string | null
|
||||
): Promise<{ success: boolean; data: { execId: string; logs: unknown[]; total: number; limit: number; offset: number; hasMore: boolean } }> {
|
||||
const params = new URLSearchParams();
|
||||
if (options?.limit) params.append('limit', String(options.limit));
|
||||
@@ -323,7 +367,11 @@ async function fetchExecutionLogsById(
|
||||
if (options?.nodeId) params.append('nodeId', options.nodeId);
|
||||
|
||||
const queryString = params.toString();
|
||||
const response = await fetch(`${API_BASE}/executions/${execId}/logs${queryString ? `?${queryString}` : ''}`);
|
||||
const url = withPath(
|
||||
`${API_BASE}/executions/${execId}/logs${queryString ? `?${queryString}` : ''}`,
|
||||
projectPath
|
||||
);
|
||||
const response = await fetch(url, { credentials: 'same-origin' });
|
||||
if (!response.ok) {
|
||||
throw new Error(`Failed to fetch execution logs: ${response.statusText}`);
|
||||
}
|
||||
@@ -337,9 +385,10 @@ async function fetchExecutionLogsById(
|
||||
* Uses useQuery to get execution state, enabled when execId exists
|
||||
*/
|
||||
export function useExecutionState(execId: string | null) {
|
||||
const projectPath = useWorkflowStore(selectProjectPath);
|
||||
return useQuery({
|
||||
queryKey: flowKeys.executionState(execId ?? ''),
|
||||
queryFn: () => fetchExecutionStateById(execId!),
|
||||
queryKey: [...flowKeys.executionState(execId ?? ''), projectPath],
|
||||
queryFn: () => fetchExecutionStateById(execId!, projectPath),
|
||||
enabled: !!execId,
|
||||
staleTime: 5000, // 5 seconds - needs more frequent updates for monitoring
|
||||
});
|
||||
@@ -358,9 +407,10 @@ export function useExecutionLogs(
|
||||
nodeId?: string;
|
||||
}
|
||||
) {
|
||||
const projectPath = useWorkflowStore(selectProjectPath);
|
||||
return useQuery({
|
||||
queryKey: flowKeys.executionLogs(execId ?? '', options),
|
||||
queryFn: () => fetchExecutionLogsById(execId!, options),
|
||||
queryKey: [...flowKeys.executionLogs(execId ?? '', options), projectPath],
|
||||
queryFn: () => fetchExecutionLogsById(execId!, options, projectPath),
|
||||
enabled: !!execId,
|
||||
staleTime: 10000, // 10 seconds
|
||||
});
|
||||
|
||||
@@ -43,15 +43,66 @@ interface ExportTemplateResponse {
|
||||
|
||||
// ========== Fetch Functions ==========
|
||||
|
||||
function toFlowTemplate(raw: any): FlowTemplate {
|
||||
const meta = raw?.template_metadata ?? {};
|
||||
const nodes = Array.isArray(raw?.nodes) ? raw.nodes : [];
|
||||
const edges = Array.isArray(raw?.edges) ? raw.edges : [];
|
||||
|
||||
return {
|
||||
id: String(raw?.id ?? ''),
|
||||
name: String(raw?.name ?? ''),
|
||||
description: (typeof meta.description === 'string' ? meta.description : raw?.description) || undefined,
|
||||
category: typeof meta.category === 'string' ? meta.category : undefined,
|
||||
tags: Array.isArray(meta.tags) ? meta.tags : undefined,
|
||||
author: typeof meta.author === 'string' ? meta.author : undefined,
|
||||
version: String(meta.version ?? raw?.version ?? '1.0.0'),
|
||||
created_at: String(raw?.created_at ?? new Date().toISOString()),
|
||||
updated_at: String(raw?.updated_at ?? new Date().toISOString()),
|
||||
nodeCount: nodes.length,
|
||||
edgeCount: edges.length,
|
||||
};
|
||||
}
|
||||
|
||||
function toFlowFromTemplate(raw: any): Flow {
|
||||
const meta = raw?.template_metadata ?? {};
|
||||
const now = new Date().toISOString();
|
||||
return {
|
||||
id: `flow-${Date.now()}-${Math.random().toString(36).slice(2, 10)}`,
|
||||
name: String(raw?.name ?? 'Template Flow'),
|
||||
description: (typeof meta.description === 'string' ? meta.description : raw?.description) || undefined,
|
||||
version: String(meta.version ?? raw?.version ?? '1.0.0'),
|
||||
created_at: String(raw?.created_at ?? now),
|
||||
updated_at: String(raw?.updated_at ?? now),
|
||||
nodes: Array.isArray(raw?.nodes) ? raw.nodes : [],
|
||||
edges: Array.isArray(raw?.edges) ? raw.edges : [],
|
||||
variables: typeof raw?.variables === 'object' && raw.variables ? raw.variables : {},
|
||||
metadata: {
|
||||
source: 'template',
|
||||
templateId: typeof raw?.id === 'string' ? raw.id : undefined,
|
||||
tags: Array.isArray(meta.tags) ? meta.tags : undefined,
|
||||
category: typeof meta.category === 'string' ? meta.category : undefined,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
async function fetchTemplates(category?: string): Promise<TemplatesListResponse> {
|
||||
const url = category
|
||||
? `${API_BASE}/templates?category=${encodeURIComponent(category)}`
|
||||
: `${API_BASE}/templates`;
|
||||
const response = await fetch(url);
|
||||
const response = await fetch(url, { credentials: 'same-origin' });
|
||||
if (!response.ok) {
|
||||
throw new Error(`Failed to fetch templates: ${response.statusText}`);
|
||||
}
|
||||
return response.json();
|
||||
const json = await response.json();
|
||||
const rawTemplates: any[] = Array.isArray(json?.data) ? json.data : (json?.templates || []);
|
||||
const templates: FlowTemplate[] = rawTemplates.map(toFlowTemplate);
|
||||
const total = typeof json?.total === 'number' ? json.total : templates.length;
|
||||
const categories = Array.from(new Set(
|
||||
templates
|
||||
.map((t) => t.category)
|
||||
.filter((c): c is string => typeof c === 'string' && c.trim().length > 0)
|
||||
));
|
||||
return { templates, total, categories };
|
||||
}
|
||||
|
||||
async function fetchTemplate(id: string): Promise<TemplateDetailResponse> {
|
||||
@@ -67,11 +118,14 @@ async function installTemplate(request: TemplateInstallRequest): Promise<Install
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify(request),
|
||||
credentials: 'same-origin',
|
||||
});
|
||||
if (!response.ok) {
|
||||
throw new Error(`Failed to install template: ${response.statusText}`);
|
||||
}
|
||||
return response.json();
|
||||
const json = await response.json();
|
||||
const template = (json && typeof json === 'object' && 'data' in json) ? json.data : json;
|
||||
return { flow: toFlowFromTemplate(template), message: json?.message || 'Template installed' };
|
||||
}
|
||||
|
||||
async function exportTemplate(request: TemplateExportRequest): Promise<ExportTemplateResponse> {
|
||||
@@ -79,16 +133,20 @@ async function exportTemplate(request: TemplateExportRequest): Promise<ExportTem
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify(request),
|
||||
credentials: 'same-origin',
|
||||
});
|
||||
if (!response.ok) {
|
||||
throw new Error(`Failed to export template: ${response.statusText}`);
|
||||
}
|
||||
return response.json();
|
||||
const json = await response.json();
|
||||
const template = (json && typeof json === 'object' && 'data' in json) ? json.data : json;
|
||||
return { template: toFlowTemplate(template), message: json?.message || 'Template exported' };
|
||||
}
|
||||
|
||||
async function deleteTemplate(id: string): Promise<void> {
|
||||
const response = await fetch(`${API_BASE}/templates/${id}`, {
|
||||
method: 'DELETE',
|
||||
credentials: 'same-origin',
|
||||
});
|
||||
if (!response.ok) {
|
||||
throw new Error(`Failed to delete template: ${response.statusText}`);
|
||||
|
||||
@@ -5451,6 +5451,52 @@ export interface ExecutionLogsResponse {
|
||||
hasMore: boolean;
|
||||
}
|
||||
|
||||
// ========== Orchestrator Flow API (Create/Execute) ==========
|
||||
|
||||
export interface OrchestratorFlowDto {
|
||||
id: string;
|
||||
name: string;
|
||||
description?: string;
|
||||
version: string;
|
||||
created_at: string;
|
||||
updated_at: string;
|
||||
nodes: Array<Record<string, unknown>>;
|
||||
edges: Array<Record<string, unknown>>;
|
||||
variables: Record<string, unknown>;
|
||||
metadata: Record<string, unknown>;
|
||||
}
|
||||
|
||||
export interface CreateOrchestratorFlowRequest {
|
||||
name: string;
|
||||
description?: string;
|
||||
version?: string;
|
||||
nodes?: Array<Record<string, unknown>>;
|
||||
edges?: Array<Record<string, unknown>>;
|
||||
variables?: Record<string, unknown>;
|
||||
metadata?: Record<string, unknown>;
|
||||
}
|
||||
|
||||
export async function createOrchestratorFlow(
|
||||
request: CreateOrchestratorFlowRequest,
|
||||
projectPath?: string
|
||||
): Promise<{ success: boolean; data: OrchestratorFlowDto }> {
|
||||
return fetchApi(withPath('/api/orchestrator/flows', projectPath), {
|
||||
method: 'POST',
|
||||
body: JSON.stringify(request),
|
||||
});
|
||||
}
|
||||
|
||||
export async function executeOrchestratorFlow(
|
||||
flowId: string,
|
||||
request?: { variables?: Record<string, unknown> },
|
||||
projectPath?: string
|
||||
): Promise<{ success: boolean; data: { execId: string; flowId: string; status: string; startedAt: string } }> {
|
||||
return fetchApi(withPath(`/api/orchestrator/flows/${encodeURIComponent(flowId)}/execute`, projectPath), {
|
||||
method: 'POST',
|
||||
body: JSON.stringify(request ?? {}),
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch execution state by execId
|
||||
* @param execId - Execution ID
|
||||
@@ -5824,3 +5870,62 @@ export async function revokeCliSessionShareToken(
|
||||
{ method: 'POST', body: JSON.stringify(input) }
|
||||
);
|
||||
}
|
||||
|
||||
// ========== Audit (Observability) API ==========
|
||||
|
||||
export type CliSessionAuditEventType =
|
||||
| 'session_created'
|
||||
| 'session_closed'
|
||||
| 'session_send'
|
||||
| 'session_execute'
|
||||
| 'session_resize'
|
||||
| 'session_share_created'
|
||||
| 'session_share_revoked'
|
||||
| 'session_idle_reaped';
|
||||
|
||||
export interface CliSessionAuditEvent {
|
||||
type: CliSessionAuditEventType;
|
||||
timestamp: string;
|
||||
projectRoot: string;
|
||||
sessionKey?: string;
|
||||
tool?: string;
|
||||
resumeKey?: string;
|
||||
workingDir?: string;
|
||||
ip?: string;
|
||||
userAgent?: string;
|
||||
details?: Record<string, unknown>;
|
||||
}
|
||||
|
||||
export interface CliSessionAuditListResponse {
|
||||
events: CliSessionAuditEvent[];
|
||||
total: number;
|
||||
limit: number;
|
||||
offset: number;
|
||||
hasMore: boolean;
|
||||
}
|
||||
|
||||
export async function fetchCliSessionAudit(
|
||||
options?: {
|
||||
projectPath?: string;
|
||||
sessionKey?: string;
|
||||
type?: CliSessionAuditEventType | CliSessionAuditEventType[];
|
||||
q?: string;
|
||||
limit?: number;
|
||||
offset?: number;
|
||||
}
|
||||
): Promise<{ success: boolean; data: CliSessionAuditListResponse }> {
|
||||
const params = new URLSearchParams();
|
||||
if (options?.sessionKey) params.set('sessionKey', options.sessionKey);
|
||||
if (options?.q) params.set('q', options.q);
|
||||
if (typeof options?.limit === 'number') params.set('limit', String(options.limit));
|
||||
if (typeof options?.offset === 'number') params.set('offset', String(options.offset));
|
||||
if (options?.type) {
|
||||
const types = Array.isArray(options.type) ? options.type : [options.type];
|
||||
params.set('type', types.join(','));
|
||||
}
|
||||
|
||||
const queryString = params.toString();
|
||||
return fetchApi<{ success: boolean; data: CliSessionAuditListResponse }>(
|
||||
withPath(`/api/audit/cli-sessions${queryString ? `?${queryString}` : ''}`, options?.projectPath)
|
||||
);
|
||||
}
|
||||
|
||||
@@ -116,6 +116,19 @@ export const workspaceQueryKeys = {
|
||||
cliHistoryList: (projectPath: string) => [...workspaceQueryKeys.cliHistory(projectPath), 'list'] as const,
|
||||
cliExecutionDetail: (projectPath: string, executionId: string) =>
|
||||
[...workspaceQueryKeys.cliHistory(projectPath), 'detail', executionId] as const,
|
||||
|
||||
// ========== Audit ==========
|
||||
audit: (projectPath: string) => [...workspaceQueryKeys.all(projectPath), 'audit'] as const,
|
||||
cliSessionAudit: (
|
||||
projectPath: string,
|
||||
options?: {
|
||||
sessionKey?: string;
|
||||
type?: string;
|
||||
q?: string;
|
||||
limit?: number;
|
||||
offset?: number;
|
||||
}
|
||||
) => [...workspaceQueryKeys.audit(projectPath), 'cliSessions', options] as const,
|
||||
};
|
||||
|
||||
// ========== API Settings Keys ==========
|
||||
|
||||
@@ -159,6 +159,18 @@
|
||||
"exec": {
|
||||
"title": "Execute in Session"
|
||||
},
|
||||
"orchestrator": {
|
||||
"title": "Send to Orchestrator",
|
||||
"targetSession": "Target session",
|
||||
"tool": "Tool",
|
||||
"mode": "Mode",
|
||||
"resumeStrategy": "resumeStrategy",
|
||||
"send": "Send",
|
||||
"sending": "Sending...",
|
||||
"sentTitle": "Sent to Orchestrator",
|
||||
"sentDesc": "Flow created: {flowId}",
|
||||
"sendFailed": "Failed to send"
|
||||
},
|
||||
"status": {
|
||||
"pending": "Pending",
|
||||
"ready": "Ready",
|
||||
@@ -359,9 +371,38 @@
|
||||
"issues": "Issues",
|
||||
"board": "Board",
|
||||
"queue": "Queue",
|
||||
"discovery": "Discovery"
|
||||
"discovery": "Discovery",
|
||||
"observability": "Observability"
|
||||
}
|
||||
},
|
||||
"observability": {
|
||||
"pageTitle": "Observability",
|
||||
"description": "Audit and inspect automated deliveries and CLI session activity",
|
||||
"audit": {
|
||||
"title": "CLI Session Audit"
|
||||
},
|
||||
"filters": {
|
||||
"search": "Search",
|
||||
"searchPlaceholder": "Search type/sessionKey/resumeKey/details...",
|
||||
"sessionKey": "Session Key",
|
||||
"sessionKeyPlaceholder": "e.g. cli-xxxx",
|
||||
"type": "Type",
|
||||
"typeAll": "All types",
|
||||
"limit": "Limit"
|
||||
},
|
||||
"table": {
|
||||
"timestamp": "Timestamp",
|
||||
"type": "Type",
|
||||
"sessionKey": "Session",
|
||||
"tool": "Tool",
|
||||
"resumeKey": "Resume Key",
|
||||
"workingDir": "Working Dir",
|
||||
"ip": "IP",
|
||||
"userAgent": "User Agent"
|
||||
},
|
||||
"empty": "No audit events",
|
||||
"error": "Failed to load audit events"
|
||||
},
|
||||
"board": {
|
||||
"pageTitle": "Issue Board",
|
||||
"description": "Visualize and manage issues in a kanban board",
|
||||
|
||||
@@ -159,6 +159,18 @@
|
||||
"exec": {
|
||||
"title": "在会话中执行"
|
||||
},
|
||||
"orchestrator": {
|
||||
"title": "发送到编排器",
|
||||
"targetSession": "目标会话",
|
||||
"tool": "工具",
|
||||
"mode": "模式",
|
||||
"resumeStrategy": "resumeStrategy",
|
||||
"send": "发送",
|
||||
"sending": "发送中...",
|
||||
"sentTitle": "已发送到编排器",
|
||||
"sentDesc": "已创建 flow: {flowId}",
|
||||
"sendFailed": "发送失败"
|
||||
},
|
||||
"status": {
|
||||
"pending": "待处理",
|
||||
"ready": "就绪",
|
||||
@@ -359,9 +371,38 @@
|
||||
"issues": "问题列表",
|
||||
"board": "看板",
|
||||
"queue": "执行队列",
|
||||
"discovery": "问题发现"
|
||||
"discovery": "问题发现",
|
||||
"observability": "可观测"
|
||||
}
|
||||
},
|
||||
"observability": {
|
||||
"pageTitle": "可观测面板",
|
||||
"description": "审计并查看自动投递与 CLI 会话活动",
|
||||
"audit": {
|
||||
"title": "CLI 会话审计"
|
||||
},
|
||||
"filters": {
|
||||
"search": "搜索",
|
||||
"searchPlaceholder": "搜索 type/sessionKey/resumeKey/details...",
|
||||
"sessionKey": "会话 Key",
|
||||
"sessionKeyPlaceholder": "例如 cli-xxxx",
|
||||
"type": "类型",
|
||||
"typeAll": "全部类型",
|
||||
"limit": "条数"
|
||||
},
|
||||
"table": {
|
||||
"timestamp": "时间戳",
|
||||
"type": "类型",
|
||||
"sessionKey": "会话",
|
||||
"tool": "工具",
|
||||
"resumeKey": "resumeKey",
|
||||
"workingDir": "工作目录",
|
||||
"ip": "IP",
|
||||
"userAgent": "User-Agent"
|
||||
},
|
||||
"empty": "暂无审计事件",
|
||||
"error": "加载审计事件失败"
|
||||
},
|
||||
"board": {
|
||||
"pageTitle": "问题看板",
|
||||
"description": "以看板方式可视化管理问题",
|
||||
|
||||
@@ -18,6 +18,7 @@ import { IssuesPanel } from '@/components/issue/hub/IssuesPanel';
|
||||
import { IssueBoardPanel } from '@/components/issue/hub/IssueBoardPanel';
|
||||
import { QueuePanel } from '@/components/issue/hub/QueuePanel';
|
||||
import { DiscoveryPanel } from '@/components/issue/hub/DiscoveryPanel';
|
||||
import { ObservabilityPanel } from '@/components/issue/hub/ObservabilityPanel';
|
||||
import { Button } from '@/components/ui/Button';
|
||||
import { Input } from '@/components/ui/Input';
|
||||
import { Dialog, DialogContent, DialogHeader, DialogTitle } from '@/components/ui/Dialog';
|
||||
@@ -193,6 +194,9 @@ export function IssueHubPage() {
|
||||
case 'discovery':
|
||||
return null; // Discovery panel has its own controls
|
||||
|
||||
case 'observability':
|
||||
return null; // Observability panel has its own controls
|
||||
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
@@ -217,6 +221,7 @@ export function IssueHubPage() {
|
||||
{currentTab === 'board' && <IssueBoardPanel />}
|
||||
{currentTab === 'queue' && <QueuePanel />}
|
||||
{currentTab === 'discovery' && <DiscoveryPanel />}
|
||||
{currentTab === 'observability' && <ObservabilityPanel />}
|
||||
|
||||
<NewIssueDialog open={isNewIssueOpen} onOpenChange={setIsNewIssueOpen} onSubmit={handleCreateIssue} isCreating={isCreating} />
|
||||
</div>
|
||||
|
||||
@@ -186,13 +186,18 @@ export const useFlowStore = create<FlowStore>()(
|
||||
method,
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify(flowToSave),
|
||||
credentials: 'same-origin',
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(`Failed to save flow: ${response.statusText}`);
|
||||
}
|
||||
|
||||
const savedFlow = await response.json();
|
||||
const payload = await response.json();
|
||||
const savedFlow = (payload && typeof payload === 'object' && 'data' in payload)
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
? (payload as any).data
|
||||
: payload;
|
||||
|
||||
set(
|
||||
(state) => ({
|
||||
@@ -215,12 +220,16 @@ export const useFlowStore = create<FlowStore>()(
|
||||
|
||||
loadFlow: async (id: string): Promise<boolean> => {
|
||||
try {
|
||||
const response = await fetch(`${API_BASE}/flows/${id}`);
|
||||
const response = await fetch(`${API_BASE}/flows/${id}`, { credentials: 'same-origin' });
|
||||
if (!response.ok) {
|
||||
throw new Error(`Failed to load flow: ${response.statusText}`);
|
||||
}
|
||||
|
||||
const flow: Flow = await response.json();
|
||||
const payload = await response.json();
|
||||
const flow: Flow = (payload && typeof payload === 'object' && 'data' in payload)
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
? (payload as any).data
|
||||
: payload;
|
||||
|
||||
set(
|
||||
{
|
||||
@@ -246,6 +255,7 @@ export const useFlowStore = create<FlowStore>()(
|
||||
try {
|
||||
const response = await fetch(`${API_BASE}/flows/${id}`, {
|
||||
method: 'DELETE',
|
||||
credentials: 'same-origin',
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
@@ -274,13 +284,18 @@ export const useFlowStore = create<FlowStore>()(
|
||||
try {
|
||||
const response = await fetch(`${API_BASE}/flows/${id}/duplicate`, {
|
||||
method: 'POST',
|
||||
credentials: 'same-origin',
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(`Failed to duplicate flow: ${response.statusText}`);
|
||||
}
|
||||
|
||||
const duplicatedFlow: Flow = await response.json();
|
||||
const payload = await response.json();
|
||||
const duplicatedFlow: Flow = (payload && typeof payload === 'object' && 'data' in payload)
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
? (payload as any).data
|
||||
: payload;
|
||||
|
||||
set(
|
||||
(state) => ({
|
||||
@@ -478,13 +493,13 @@ export const useFlowStore = create<FlowStore>()(
|
||||
set({ isLoadingFlows: true }, false, 'fetchFlows/start');
|
||||
|
||||
try {
|
||||
const response = await fetch(`${API_BASE}/flows`);
|
||||
const response = await fetch(`${API_BASE}/flows`, { credentials: 'same-origin' });
|
||||
if (!response.ok) {
|
||||
throw new Error(`Failed to fetch flows: ${response.statusText}`);
|
||||
}
|
||||
|
||||
const data = await response.json();
|
||||
const flows: Flow[] = data.flows || [];
|
||||
const flows: Flow[] = Array.isArray(data?.data) ? data.data : (data?.flows || []);
|
||||
|
||||
set({ flows, isLoadingFlows: false }, false, 'fetchFlows/success');
|
||||
} catch (error) {
|
||||
|
||||
@@ -206,7 +206,7 @@ export interface Flow {
|
||||
id: string;
|
||||
name: string;
|
||||
description?: string;
|
||||
version: number;
|
||||
version: string | number;
|
||||
created_at: string;
|
||||
updated_at: string;
|
||||
nodes: FlowNode[];
|
||||
|
||||
154
ccw/src/core/routes/audit-routes.ts
Normal file
154
ccw/src/core/routes/audit-routes.ts
Normal file
@@ -0,0 +1,154 @@
|
||||
/**
|
||||
* Audit Routes Module
|
||||
* Read-only APIs for audit/observability panels.
|
||||
*
|
||||
* Currently supported:
|
||||
* - GET /api/audit/cli-sessions - Read CLI session (PTY) audit events (JSONL)
|
||||
*/
|
||||
|
||||
import { existsSync } from 'fs';
|
||||
import { readFile } from 'fs/promises';
|
||||
import { join } from 'path';
|
||||
import { validatePath as validateAllowedPath } from '../../utils/path-validator.js';
|
||||
import type { CliSessionAuditEvent, CliSessionAuditEventType } from '../services/cli-session-audit.js';
|
||||
import type { RouteContext } from './types.js';
|
||||
|
||||
function clampInt(value: number, min: number, max: number): number {
|
||||
if (!Number.isFinite(value)) return min;
|
||||
return Math.min(max, Math.max(min, Math.trunc(value)));
|
||||
}
|
||||
|
||||
function parseCsvParam(value: string | null): string[] {
|
||||
if (!value) return [];
|
||||
return value
|
||||
.split(',')
|
||||
.map((v) => v.trim())
|
||||
.filter(Boolean);
|
||||
}
|
||||
|
||||
function isCliSessionAuditEventType(value: string): value is CliSessionAuditEventType {
|
||||
return [
|
||||
'session_created',
|
||||
'session_closed',
|
||||
'session_send',
|
||||
'session_execute',
|
||||
'session_resize',
|
||||
'session_share_created',
|
||||
'session_share_revoked',
|
||||
'session_idle_reaped',
|
||||
].includes(value);
|
||||
}
|
||||
|
||||
function matchesSearch(event: CliSessionAuditEvent, qLower: string): boolean {
|
||||
if (!qLower) return true;
|
||||
|
||||
const haystacks: string[] = [];
|
||||
if (event.type) haystacks.push(event.type);
|
||||
if (event.timestamp) haystacks.push(event.timestamp);
|
||||
if (event.sessionKey) haystacks.push(event.sessionKey);
|
||||
if (event.tool) haystacks.push(event.tool);
|
||||
if (event.resumeKey) haystacks.push(event.resumeKey);
|
||||
if (event.workingDir) haystacks.push(event.workingDir);
|
||||
if (event.ip) haystacks.push(event.ip);
|
||||
if (event.userAgent) haystacks.push(event.userAgent);
|
||||
if (event.details) {
|
||||
try {
|
||||
haystacks.push(JSON.stringify(event.details));
|
||||
} catch {
|
||||
// Ignore non-serializable details
|
||||
}
|
||||
}
|
||||
|
||||
return haystacks.some((h) => h.toLowerCase().includes(qLower));
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle audit routes
|
||||
* @returns true if route was handled, false otherwise
|
||||
*/
|
||||
export async function handleAuditRoutes(ctx: RouteContext): Promise<boolean> {
|
||||
const { pathname, url, req, res, initialPath } = ctx;
|
||||
|
||||
// GET /api/audit/cli-sessions
|
||||
if (pathname === '/api/audit/cli-sessions' && req.method === 'GET') {
|
||||
const projectPathParam = url.searchParams.get('path') || initialPath;
|
||||
|
||||
const limit = clampInt(parseInt(url.searchParams.get('limit') || '200', 10), 1, 1000);
|
||||
const offset = clampInt(parseInt(url.searchParams.get('offset') || '0', 10), 0, Number.MAX_SAFE_INTEGER);
|
||||
|
||||
const sessionKey = url.searchParams.get('sessionKey');
|
||||
const qLower = (url.searchParams.get('q') || '').trim().toLowerCase();
|
||||
|
||||
const typeFilters = parseCsvParam(url.searchParams.get('type'))
|
||||
.filter(isCliSessionAuditEventType);
|
||||
const typeFilterSet = typeFilters.length > 0 ? new Set<CliSessionAuditEventType>(typeFilters) : null;
|
||||
|
||||
try {
|
||||
const projectRoot = await validateAllowedPath(projectPathParam, {
|
||||
mustExist: true,
|
||||
allowedDirectories: [initialPath],
|
||||
});
|
||||
|
||||
const filePath = join(projectRoot, '.workflow', 'audit', 'cli-sessions.jsonl');
|
||||
if (!existsSync(filePath)) {
|
||||
res.writeHead(200, { 'Content-Type': 'application/json; charset=utf-8' });
|
||||
res.end(JSON.stringify({
|
||||
success: true,
|
||||
data: { events: [], total: 0, limit, offset, hasMore: false },
|
||||
}));
|
||||
return true;
|
||||
}
|
||||
|
||||
const raw = await readFile(filePath, 'utf-8');
|
||||
const parsed: CliSessionAuditEvent[] = [];
|
||||
for (const line of raw.split('\n')) {
|
||||
const trimmed = line.trim();
|
||||
if (!trimmed) continue;
|
||||
try {
|
||||
parsed.push(JSON.parse(trimmed) as CliSessionAuditEvent);
|
||||
} catch {
|
||||
// Skip invalid JSONL line
|
||||
}
|
||||
}
|
||||
|
||||
const filtered = parsed.filter((ev) => {
|
||||
if (sessionKey && ev.sessionKey !== sessionKey) return false;
|
||||
if (typeFilterSet && !typeFilterSet.has(ev.type)) return false;
|
||||
if (qLower && !matchesSearch(ev, qLower)) return false;
|
||||
return true;
|
||||
});
|
||||
|
||||
// Best-effort: file is append-only, so reverse for newest-first.
|
||||
filtered.reverse();
|
||||
|
||||
const total = filtered.length;
|
||||
const page = filtered.slice(offset, offset + limit);
|
||||
|
||||
res.writeHead(200, { 'Content-Type': 'application/json; charset=utf-8' });
|
||||
res.end(JSON.stringify({
|
||||
success: true,
|
||||
data: {
|
||||
events: page,
|
||||
total,
|
||||
limit,
|
||||
offset,
|
||||
hasMore: offset + limit < total,
|
||||
},
|
||||
}));
|
||||
return true;
|
||||
} catch (err: unknown) {
|
||||
const message = err instanceof Error ? err.message : String(err);
|
||||
const lowered = message.toLowerCase();
|
||||
const status = lowered.includes('access denied') ? 403 : 400;
|
||||
res.writeHead(status, { 'Content-Type': 'application/json; charset=utf-8' });
|
||||
res.end(JSON.stringify({
|
||||
success: false,
|
||||
error: status === 403 ? 'Access denied' : 'Invalid request',
|
||||
}));
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -33,11 +33,16 @@ import { join, dirname } from 'path';
|
||||
import { randomBytes } from 'crypto';
|
||||
import { fileURLToPath } from 'url';
|
||||
import type { RouteContext } from './types.js';
|
||||
import { FlowExecutor } from '../services/flow-executor.js';
|
||||
import { validatePath as validateAllowedPath } from '../../utils/path-validator.js';
|
||||
|
||||
// ES Module __dirname equivalent
|
||||
const __filename = fileURLToPath(import.meta.url);
|
||||
const __dirname = dirname(__filename);
|
||||
|
||||
// In-memory execution engines for pause/resume/stop (best-effort; resets on server restart)
|
||||
const activeExecutors = new Map<string, FlowExecutor>();
|
||||
|
||||
// ============================================================================
|
||||
// TypeScript Interfaces
|
||||
// ============================================================================
|
||||
@@ -847,8 +852,25 @@ function flowToTemplate(
|
||||
export async function handleOrchestratorRoutes(ctx: RouteContext): Promise<boolean> {
|
||||
const { pathname, req, res, initialPath, handlePostRequest, broadcastToClients } = ctx;
|
||||
|
||||
// Get workflow directory from initialPath
|
||||
const workflowDir = initialPath || process.cwd();
|
||||
// Get workflow directory from initialPath, optionally overridden by ?path= (scoped to allowed dirs)
|
||||
const allowedRoot = initialPath || process.cwd();
|
||||
let workflowDir = allowedRoot;
|
||||
|
||||
const projectPathParam = ctx.url.searchParams.get('path');
|
||||
if (projectPathParam && projectPathParam.trim()) {
|
||||
try {
|
||||
workflowDir = await validateAllowedPath(projectPathParam, {
|
||||
mustExist: true,
|
||||
allowedDirectories: [allowedRoot],
|
||||
});
|
||||
} catch (err: unknown) {
|
||||
const message = err instanceof Error ? err.message : String(err);
|
||||
const status = message.toLowerCase().includes('access denied') ? 403 : 400;
|
||||
res.writeHead(status, { 'Content-Type': 'application/json; charset=utf-8' });
|
||||
res.end(JSON.stringify({ success: false, error: status === 403 ? 'Access denied' : 'Invalid path' }));
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
// ==== LIST FLOWS ====
|
||||
// GET /api/orchestrator/flows
|
||||
@@ -1209,9 +1231,24 @@ export async function handleOrchestratorRoutes(ctx: RouteContext): Promise<boole
|
||||
// Broadcast execution created
|
||||
broadcastExecutionStateUpdate(execution);
|
||||
|
||||
// TODO: Trigger actual flow executor (future enhancement)
|
||||
// For now, just create the execution in pending state
|
||||
// The executor will be implemented in a later task
|
||||
// Trigger actual flow executor (best-effort, async)
|
||||
// Execution state is persisted by FlowExecutor and updates are broadcast via WebSocket.
|
||||
try {
|
||||
const executor = new FlowExecutor(flow, execId, workflowDir);
|
||||
activeExecutors.set(execId, executor);
|
||||
|
||||
void executor.execute(inputVariables).then((finalState) => {
|
||||
// Keep executor instance if paused, so it can be resumed.
|
||||
if (finalState.status !== 'paused') {
|
||||
activeExecutors.delete(execId);
|
||||
}
|
||||
}).catch(() => {
|
||||
// Best-effort cleanup on unexpected failures.
|
||||
activeExecutors.delete(execId);
|
||||
});
|
||||
} catch {
|
||||
// If executor bootstrap fails, keep the pending execution for inspection.
|
||||
}
|
||||
|
||||
return {
|
||||
success: true,
|
||||
@@ -1241,6 +1278,19 @@ export async function handleOrchestratorRoutes(ctx: RouteContext): Promise<boole
|
||||
}
|
||||
|
||||
try {
|
||||
const executor = activeExecutors.get(execId);
|
||||
if (executor) {
|
||||
executor.pause();
|
||||
const execution = await readExecutionStorage(workflowDir, execId);
|
||||
res.writeHead(200, { 'Content-Type': 'application/json' });
|
||||
res.end(JSON.stringify({
|
||||
success: true,
|
||||
data: execution ?? executor.getState(),
|
||||
message: 'Pause requested'
|
||||
}));
|
||||
return true;
|
||||
}
|
||||
|
||||
const execution = await readExecutionStorage(workflowDir, execId);
|
||||
if (!execution) {
|
||||
res.writeHead(404, { 'Content-Type': 'application/json' });
|
||||
@@ -1294,6 +1344,36 @@ export async function handleOrchestratorRoutes(ctx: RouteContext): Promise<boole
|
||||
}
|
||||
|
||||
try {
|
||||
const executor = activeExecutors.get(execId);
|
||||
if (executor) {
|
||||
const current = executor.getState();
|
||||
if (current.status !== 'paused') {
|
||||
res.writeHead(400, { 'Content-Type': 'application/json' });
|
||||
res.end(JSON.stringify({
|
||||
success: false,
|
||||
error: `Cannot resume execution with status: ${current.status}`
|
||||
}));
|
||||
return true;
|
||||
}
|
||||
|
||||
void executor.resume().then((finalState) => {
|
||||
if (finalState.status !== 'paused') {
|
||||
activeExecutors.delete(execId);
|
||||
}
|
||||
}).catch(() => {
|
||||
// Best-effort: keep executor for inspection/resume retries.
|
||||
});
|
||||
|
||||
const execution = await readExecutionStorage(workflowDir, execId);
|
||||
res.writeHead(200, { 'Content-Type': 'application/json' });
|
||||
res.end(JSON.stringify({
|
||||
success: true,
|
||||
data: execution ?? executor.getState(),
|
||||
message: 'Resume requested'
|
||||
}));
|
||||
return true;
|
||||
}
|
||||
|
||||
const execution = await readExecutionStorage(workflowDir, execId);
|
||||
if (!execution) {
|
||||
res.writeHead(404, { 'Content-Type': 'application/json' });
|
||||
@@ -1347,6 +1427,36 @@ export async function handleOrchestratorRoutes(ctx: RouteContext): Promise<boole
|
||||
}
|
||||
|
||||
try {
|
||||
const executor = activeExecutors.get(execId);
|
||||
if (executor) {
|
||||
executor.stop();
|
||||
|
||||
// If currently paused, mark as failed immediately (no running loop to observe stop flag).
|
||||
const current = executor.getState();
|
||||
if (current.status === 'paused') {
|
||||
const now = new Date().toISOString();
|
||||
current.status = 'failed';
|
||||
current.completedAt = now;
|
||||
current.logs.push({
|
||||
timestamp: now,
|
||||
level: 'warn',
|
||||
message: 'Execution manually stopped by user'
|
||||
});
|
||||
await writeExecutionStorage(workflowDir, current);
|
||||
broadcastExecutionStateUpdate(current);
|
||||
activeExecutors.delete(execId);
|
||||
}
|
||||
|
||||
const execution = await readExecutionStorage(workflowDir, execId);
|
||||
res.writeHead(200, { 'Content-Type': 'application/json' });
|
||||
res.end(JSON.stringify({
|
||||
success: true,
|
||||
data: execution ?? current,
|
||||
message: 'Stop requested'
|
||||
}));
|
||||
return true;
|
||||
}
|
||||
|
||||
const execution = await readExecutionStorage(workflowDir, execId);
|
||||
if (!execution) {
|
||||
res.writeHead(404, { 'Content-Type': 'application/json' });
|
||||
|
||||
@@ -9,6 +9,7 @@ import { handleStatusRoutes } from './routes/status-routes.js';
|
||||
import { handleCliRoutes, cleanupStaleExecutions } from './routes/cli-routes.js';
|
||||
import { handleCliSettingsRoutes } from './routes/cli-settings-routes.js';
|
||||
import { handleCliSessionsRoutes } from './routes/cli-sessions-routes.js';
|
||||
import { handleAuditRoutes } from './routes/audit-routes.js';
|
||||
import { handleProviderRoutes } from './routes/provider-routes.js';
|
||||
import { handleMemoryRoutes } from './routes/memory-routes.js';
|
||||
import { handleCoreMemoryRoutes } from './routes/core-memory-routes.js';
|
||||
@@ -615,6 +616,11 @@ export async function startServer(options: ServerOptions = {}): Promise<http.Ser
|
||||
if (await handleCliSessionsRoutes(routeContext)) return;
|
||||
}
|
||||
|
||||
// Audit routes (/api/audit/*)
|
||||
if (pathname.startsWith('/api/audit')) {
|
||||
if (await handleAuditRoutes(routeContext)) return;
|
||||
}
|
||||
|
||||
// CLI routes (/api/cli/*)
|
||||
if (pathname.startsWith('/api/cli/')) {
|
||||
// CLI Settings routes first (more specific path /api/cli/settings/*)
|
||||
|
||||
@@ -177,6 +177,14 @@ export class CliSessionManager {
|
||||
return Array.from(this.sessions.values()).map(({ pty: _pty, buffer: _buffer, bufferBytes: _bytes, ...rest }) => rest);
|
||||
}
|
||||
|
||||
getProjectRoot(): string {
|
||||
return this.projectRoot;
|
||||
}
|
||||
|
||||
hasSession(sessionKey: string): boolean {
|
||||
return this.sessions.has(sessionKey);
|
||||
}
|
||||
|
||||
getSession(sessionKey: string): CliSession | null {
|
||||
const session = this.sessions.get(sessionKey);
|
||||
if (!session) return null;
|
||||
@@ -398,3 +406,15 @@ export function getCliSessionManager(projectRoot: string = process.cwd()): CliSe
|
||||
managersByRoot.set(resolved, created);
|
||||
return created;
|
||||
}
|
||||
|
||||
/**
|
||||
* Find the manager that owns a given sessionKey.
|
||||
* Useful for cross-workspace routing (tmux-like send) where the executor
|
||||
* may not share the same workflowDir/projectRoot as the target session.
|
||||
*/
|
||||
export function findCliSessionManager(sessionKey: string): CliSessionManager | null {
|
||||
for (const manager of managersByRoot.values()) {
|
||||
if (manager.hasSession(sessionKey)) return manager;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
24
ccw/src/core/services/cli-session-mux.ts
Normal file
24
ccw/src/core/services/cli-session-mux.ts
Normal file
@@ -0,0 +1,24 @@
|
||||
/**
|
||||
* CliSessionMux
|
||||
*
|
||||
* A tiny indirection layer used by FlowExecutor (and potentially others) to
|
||||
* route commands to existing PTY sessions in a testable way.
|
||||
*
|
||||
* Why this exists:
|
||||
* - ESM module namespace exports are immutable, which makes it hard to mock
|
||||
* named exports in node:test without special loaders.
|
||||
* - Exporting a mutable object lets tests override behavior by swapping
|
||||
* functions on the object.
|
||||
*/
|
||||
|
||||
import type { CliSessionManager } from './cli-session-manager.js';
|
||||
import { findCliSessionManager, getCliSessionManager } from './cli-session-manager.js';
|
||||
|
||||
export const cliSessionMux: {
|
||||
findCliSessionManager: (sessionKey: string) => CliSessionManager | null;
|
||||
getCliSessionManager: (projectRoot?: string) => CliSessionManager;
|
||||
} = {
|
||||
findCliSessionManager,
|
||||
getCliSessionManager,
|
||||
};
|
||||
|
||||
@@ -19,7 +19,8 @@ import { existsSync } from 'fs';
|
||||
import { join } from 'path';
|
||||
import { broadcastToClients } from '../websocket.js';
|
||||
import { executeCliTool } from '../../tools/cli-executor-core.js';
|
||||
import { getCliSessionManager } from './cli-session-manager.js';
|
||||
import { cliSessionMux } from './cli-session-mux.js';
|
||||
import { appendCliSessionAudit } from './cli-session-audit.js';
|
||||
import type {
|
||||
Flow,
|
||||
FlowNode,
|
||||
@@ -255,16 +256,46 @@ export class NodeRunner {
|
||||
};
|
||||
}
|
||||
|
||||
const manager = getCliSessionManager(this.context.workingDir || process.cwd());
|
||||
const manager = cliSessionMux.findCliSessionManager(targetSessionKey)
|
||||
?? cliSessionMux.getCliSessionManager(this.context.workingDir || process.cwd());
|
||||
if (!manager.hasSession(targetSessionKey)) {
|
||||
return {
|
||||
success: false,
|
||||
error: `Target session not found: ${targetSessionKey}`
|
||||
};
|
||||
}
|
||||
const routed = manager.execute(targetSessionKey, {
|
||||
tool,
|
||||
prompt: instruction,
|
||||
mode,
|
||||
workingDir: this.context.workingDir,
|
||||
resumeKey: data.resumeKey,
|
||||
resumeStrategy: data.resumeStrategy === 'promptConcat' ? 'promptConcat' : 'nativeResume'
|
||||
});
|
||||
|
||||
// Best-effort: record audit event so Observability panel includes orchestrator-routed executions.
|
||||
try {
|
||||
const session = manager.getSession(targetSessionKey);
|
||||
appendCliSessionAudit({
|
||||
type: 'session_execute',
|
||||
timestamp: new Date().toISOString(),
|
||||
projectRoot: manager.getProjectRoot(),
|
||||
sessionKey: targetSessionKey,
|
||||
tool,
|
||||
resumeKey: data.resumeKey,
|
||||
workingDir: session?.workingDir,
|
||||
details: {
|
||||
executionId: routed.executionId,
|
||||
mode,
|
||||
resumeStrategy: data.resumeStrategy ?? 'nativeResume',
|
||||
delivery: 'sendToSession',
|
||||
flowId: this.context.flowId,
|
||||
nodeId: node.id
|
||||
}
|
||||
});
|
||||
} catch {
|
||||
// ignore
|
||||
}
|
||||
|
||||
const outputKey = data.outputName || `${node.id}_output`;
|
||||
this.context.variables[outputKey] = {
|
||||
delivery: 'sendToSession',
|
||||
|
||||
157
ccw/tests/audit-routes.test.js
Normal file
157
ccw/tests/audit-routes.test.js
Normal file
@@ -0,0 +1,157 @@
|
||||
/**
|
||||
* Integration tests for audit routes.
|
||||
*
|
||||
* Targets runtime implementation shipped in `ccw/dist`.
|
||||
*/
|
||||
|
||||
import { after, before, describe, it } from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import http from 'node:http';
|
||||
import { mkdtempSync, mkdirSync, writeFileSync, rmSync } from 'node:fs';
|
||||
import { tmpdir } from 'node:os';
|
||||
import { join } from 'node:path';
|
||||
|
||||
const auditRoutesUrl = new URL('../dist/core/routes/audit-routes.js', import.meta.url);
|
||||
auditRoutesUrl.searchParams.set('t', String(Date.now()));
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
let mod;
|
||||
|
||||
async function requestJson(baseUrl, method, path) {
|
||||
const url = new URL(path, baseUrl);
|
||||
return new Promise((resolve, reject) => {
|
||||
const req = http.request(
|
||||
url,
|
||||
{ method, headers: { Accept: 'application/json' } },
|
||||
(res) => {
|
||||
let responseBody = '';
|
||||
res.on('data', (chunk) => {
|
||||
responseBody += chunk.toString();
|
||||
});
|
||||
res.on('end', () => {
|
||||
let json = null;
|
||||
try {
|
||||
json = responseBody ? JSON.parse(responseBody) : null;
|
||||
} catch {
|
||||
json = null;
|
||||
}
|
||||
resolve({ status: res.statusCode || 0, json, text: responseBody });
|
||||
});
|
||||
}
|
||||
);
|
||||
req.on('error', reject);
|
||||
req.end();
|
||||
});
|
||||
}
|
||||
|
||||
describe('audit routes integration', async () => {
|
||||
let server = null;
|
||||
let baseUrl = '';
|
||||
let projectRoot = '';
|
||||
|
||||
before(async () => {
|
||||
projectRoot = mkdtempSync(join(tmpdir(), 'ccw-audit-routes-project-'));
|
||||
|
||||
mod = await import(auditRoutesUrl.href);
|
||||
|
||||
server = http.createServer(async (req, res) => {
|
||||
const url = new URL(req.url || '/', 'http://localhost');
|
||||
const pathname = url.pathname;
|
||||
|
||||
const ctx = {
|
||||
pathname,
|
||||
url,
|
||||
req,
|
||||
res,
|
||||
initialPath: projectRoot,
|
||||
handlePostRequest() {},
|
||||
broadcastToClients() {},
|
||||
};
|
||||
|
||||
try {
|
||||
const handled = await mod.handleAuditRoutes(ctx);
|
||||
if (!handled) {
|
||||
res.writeHead(404, { 'Content-Type': 'application/json' });
|
||||
res.end(JSON.stringify({ error: 'Not Found' }));
|
||||
}
|
||||
} catch (err) {
|
||||
res.writeHead(500, { 'Content-Type': 'application/json' });
|
||||
res.end(JSON.stringify({ error: err instanceof Error ? err.message : String(err) }));
|
||||
}
|
||||
});
|
||||
|
||||
await new Promise((resolve) => server.listen(0, () => resolve()));
|
||||
const addr = server.address();
|
||||
const port = typeof addr === 'object' && addr ? addr.port : 0;
|
||||
baseUrl = `http://127.0.0.1:${port}`;
|
||||
});
|
||||
|
||||
after(() => {
|
||||
if (server) server.close();
|
||||
if (projectRoot) rmSync(projectRoot, { recursive: true, force: true });
|
||||
});
|
||||
|
||||
it('returns empty list when audit file is missing', async () => {
|
||||
const r = await requestJson(baseUrl, 'GET', `/api/audit/cli-sessions?path=${encodeURIComponent(projectRoot)}`);
|
||||
assert.equal(r.status, 200);
|
||||
assert.equal(r.json.success, true);
|
||||
assert.deepEqual(r.json.data.events, []);
|
||||
assert.equal(r.json.data.total, 0);
|
||||
});
|
||||
|
||||
it('lists events newest-first and supports filters', async () => {
|
||||
const auditDir = join(projectRoot, '.workflow', 'audit');
|
||||
mkdirSync(auditDir, { recursive: true });
|
||||
const filePath = join(auditDir, 'cli-sessions.jsonl');
|
||||
|
||||
const ev1 = {
|
||||
type: 'session_created',
|
||||
timestamp: '2026-02-09T00:00:00.000Z',
|
||||
projectRoot,
|
||||
sessionKey: 's-1',
|
||||
tool: 'claude',
|
||||
resumeKey: 'ISSUE-1',
|
||||
details: { a: 1 },
|
||||
};
|
||||
const ev2 = {
|
||||
type: 'session_execute',
|
||||
timestamp: '2026-02-09T00:00:01.000Z',
|
||||
projectRoot,
|
||||
sessionKey: 's-1',
|
||||
tool: 'claude',
|
||||
resumeKey: 'ISSUE-1',
|
||||
details: { q: 'hello' },
|
||||
};
|
||||
const ev3 = {
|
||||
type: 'session_send',
|
||||
timestamp: '2026-02-09T00:00:02.000Z',
|
||||
projectRoot,
|
||||
sessionKey: 's-2',
|
||||
tool: 'codex',
|
||||
resumeKey: 'ISSUE-2',
|
||||
details: { bytes: 10 },
|
||||
};
|
||||
|
||||
writeFileSync(filePath, [ev1, ev2, ev3].map((e) => JSON.stringify(e)).join('\n') + '\n', 'utf8');
|
||||
|
||||
const all = await requestJson(baseUrl, 'GET', `/api/audit/cli-sessions?path=${encodeURIComponent(projectRoot)}&limit=10&offset=0`);
|
||||
assert.equal(all.status, 200);
|
||||
assert.equal(all.json.success, true);
|
||||
assert.equal(all.json.data.total, 3);
|
||||
// Newest-first
|
||||
assert.equal(all.json.data.events[0].type, 'session_send');
|
||||
assert.equal(all.json.data.events[0].sessionKey, 's-2');
|
||||
|
||||
const bySession = await requestJson(baseUrl, 'GET', `/api/audit/cli-sessions?path=${encodeURIComponent(projectRoot)}&sessionKey=s-1`);
|
||||
assert.equal(bySession.json.data.total, 2);
|
||||
assert.equal(bySession.json.data.events[0].type, 'session_execute');
|
||||
|
||||
const byType = await requestJson(baseUrl, 'GET', `/api/audit/cli-sessions?path=${encodeURIComponent(projectRoot)}&type=session_created`);
|
||||
assert.equal(byType.json.data.total, 1);
|
||||
assert.equal(byType.json.data.events[0].type, 'session_created');
|
||||
|
||||
const bySearch = await requestJson(baseUrl, 'GET', `/api/audit/cli-sessions?path=${encodeURIComponent(projectRoot)}&q=hello`);
|
||||
assert.equal(bySearch.json.data.total, 1);
|
||||
assert.equal(bySearch.json.data.events[0].type, 'session_execute');
|
||||
});
|
||||
});
|
||||
112
ccw/tests/flow-executor-send-to-session.test.js
Normal file
112
ccw/tests/flow-executor-send-to-session.test.js
Normal file
@@ -0,0 +1,112 @@
|
||||
/**
|
||||
* Integration test for FlowExecutor tmux-like routing to PTY sessions.
|
||||
*
|
||||
* Ensures that delivery=sendToSession:
|
||||
* - locates the target PTY session even if the executor workflowDir differs
|
||||
* - records a session_execute audit event for Observability panel
|
||||
*/
|
||||
|
||||
import { after, before, describe, it } from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import { mkdtempSync, rmSync, readFileSync } from 'node:fs';
|
||||
import { tmpdir } from 'node:os';
|
||||
import { join } from 'node:path';
|
||||
|
||||
const flowExecutorUrl = new URL('../dist/core/services/flow-executor.js', import.meta.url);
|
||||
flowExecutorUrl.searchParams.set('t', String(Date.now()));
|
||||
|
||||
const cliSessionMuxFileUrl = new URL('../dist/core/services/cli-session-mux.js', import.meta.url).href;
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
let FlowExecutorMod;
|
||||
|
||||
describe('flow-executor sendToSession routing', async () => {
|
||||
let workflowDir = '';
|
||||
let sessionRoot = '';
|
||||
let sessionKey = '';
|
||||
|
||||
before(async () => {
|
||||
workflowDir = mkdtempSync(join(tmpdir(), 'ccw-flowexec-workflow-'));
|
||||
sessionRoot = mkdtempSync(join(tmpdir(), 'ccw-flowexec-sessionroot-'));
|
||||
|
||||
sessionKey = 'cli-session-test-1';
|
||||
|
||||
const fakeManager = {
|
||||
hasSession: (key) => key === sessionKey,
|
||||
getProjectRoot: () => sessionRoot,
|
||||
getSession: (key) => (key === sessionKey ? { sessionKey, workingDir: sessionRoot, tool: 'claude' } : null),
|
||||
execute: (key, options) => {
|
||||
if (key !== sessionKey) throw new Error('Session not found');
|
||||
return { executionId: 'exec-routed-1', command: `echo routed ${options?.tool || ''}` };
|
||||
},
|
||||
};
|
||||
|
||||
const muxMod = await import(cliSessionMuxFileUrl);
|
||||
muxMod.cliSessionMux.findCliSessionManager = (key) => (key === sessionKey ? fakeManager : null);
|
||||
muxMod.cliSessionMux.getCliSessionManager = () => fakeManager;
|
||||
|
||||
FlowExecutorMod = await import(flowExecutorUrl.href);
|
||||
});
|
||||
|
||||
after(() => {
|
||||
if (workflowDir) rmSync(workflowDir, { recursive: true, force: true });
|
||||
if (sessionRoot) rmSync(sessionRoot, { recursive: true, force: true });
|
||||
});
|
||||
|
||||
it('routes execution to the target session and appends audit event', async () => {
|
||||
const flowId = 'flow-test-send-to-session';
|
||||
const execId = `exec-${Date.now()}`;
|
||||
const now = new Date().toISOString();
|
||||
|
||||
const flow = {
|
||||
id: flowId,
|
||||
name: 'SendToSession Flow',
|
||||
description: '',
|
||||
version: '1.0.0',
|
||||
created_at: now,
|
||||
updated_at: now,
|
||||
nodes: [
|
||||
{
|
||||
id: 'node-1',
|
||||
type: 'prompt-template',
|
||||
position: { x: 0, y: 0 },
|
||||
data: {
|
||||
label: 'SendToSession',
|
||||
instruction: 'echo hello',
|
||||
tool: 'claude',
|
||||
mode: 'analysis',
|
||||
delivery: 'sendToSession',
|
||||
targetSessionKey: sessionKey,
|
||||
resumeKey: 'ISSUE-TEST',
|
||||
resumeStrategy: 'nativeResume',
|
||||
},
|
||||
},
|
||||
],
|
||||
edges: [],
|
||||
variables: {},
|
||||
metadata: { source: 'local' },
|
||||
};
|
||||
|
||||
const executor = new FlowExecutorMod.FlowExecutor(flow, execId, workflowDir);
|
||||
const state = await executor.execute({});
|
||||
|
||||
assert.equal(state.status, 'completed');
|
||||
|
||||
const auditPath = join(sessionRoot, '.workflow', 'audit', 'cli-sessions.jsonl');
|
||||
const raw = readFileSync(auditPath, 'utf8');
|
||||
const events = raw
|
||||
.split('\n')
|
||||
.map((l) => l.trim())
|
||||
.filter(Boolean)
|
||||
.map((l) => JSON.parse(l));
|
||||
|
||||
const ev = events.find((e) => e.type === 'session_execute' && e.sessionKey === sessionKey);
|
||||
assert.ok(ev, 'expected session_execute audit event');
|
||||
assert.equal(ev.tool, 'claude');
|
||||
assert.equal(ev.resumeKey, 'ISSUE-TEST');
|
||||
assert.ok(ev.details);
|
||||
assert.equal(ev.details.delivery, 'sendToSession');
|
||||
assert.equal(ev.details.flowId, flowId);
|
||||
assert.equal(ev.details.nodeId, 'node-1');
|
||||
});
|
||||
});
|
||||
183
ccw/tests/orchestrator-execution.test.js
Normal file
183
ccw/tests/orchestrator-execution.test.js
Normal file
@@ -0,0 +1,183 @@
|
||||
/**
|
||||
* Integration tests for orchestrator execution wiring.
|
||||
*
|
||||
* Verifies that POST /api/orchestrator/flows/:id/execute triggers the FlowExecutor
|
||||
* and persists a completed execution for an empty flow (no nodes).
|
||||
*/
|
||||
|
||||
import { after, before, describe, it, mock } from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import http from 'node:http';
|
||||
import { mkdtempSync, mkdirSync, rmSync, writeFileSync } from 'node:fs';
|
||||
import { tmpdir } from 'node:os';
|
||||
import { join } from 'node:path';
|
||||
|
||||
const orchestratorRoutesUrl = new URL('../dist/core/routes/orchestrator-routes.js', import.meta.url);
|
||||
orchestratorRoutesUrl.searchParams.set('t', String(Date.now()));
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
let mod;
|
||||
|
||||
async function requestJson(baseUrl, method, path, body) {
|
||||
const url = new URL(path, baseUrl);
|
||||
const payload = body === undefined ? null : Buffer.from(JSON.stringify(body), 'utf8');
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
const req = http.request(
|
||||
url,
|
||||
{
|
||||
method,
|
||||
headers: {
|
||||
Accept: 'application/json',
|
||||
...(payload
|
||||
? { 'Content-Type': 'application/json', 'Content-Length': String(payload.length) }
|
||||
: {}),
|
||||
},
|
||||
},
|
||||
(res) => {
|
||||
let responseBody = '';
|
||||
res.on('data', (chunk) => {
|
||||
responseBody += chunk.toString();
|
||||
});
|
||||
res.on('end', () => {
|
||||
let json = null;
|
||||
try {
|
||||
json = responseBody ? JSON.parse(responseBody) : null;
|
||||
} catch {
|
||||
json = null;
|
||||
}
|
||||
resolve({ status: res.statusCode || 0, json, text: responseBody });
|
||||
});
|
||||
}
|
||||
);
|
||||
req.on('error', reject);
|
||||
if (payload) req.write(payload);
|
||||
req.end();
|
||||
});
|
||||
}
|
||||
|
||||
function handlePostRequest(req, res, handler) {
|
||||
let body = '';
|
||||
req.on('data', (chunk) => {
|
||||
body += chunk.toString();
|
||||
});
|
||||
req.on('end', async () => {
|
||||
try {
|
||||
const parsed = body ? JSON.parse(body) : {};
|
||||
const result = await handler(parsed);
|
||||
|
||||
if (result?.error) {
|
||||
res.writeHead(result.status || 500, { 'Content-Type': 'application/json' });
|
||||
res.end(JSON.stringify({ error: result.error }));
|
||||
} else {
|
||||
res.writeHead(200, { 'Content-Type': 'application/json' });
|
||||
res.end(JSON.stringify(result));
|
||||
}
|
||||
} catch (err) {
|
||||
res.writeHead(500, { 'Content-Type': 'application/json' });
|
||||
res.end(JSON.stringify({ error: err instanceof Error ? err.message : String(err) }));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
describe('orchestrator execution integration', async () => {
|
||||
let server = null;
|
||||
let baseUrl = '';
|
||||
let projectRoot = '';
|
||||
|
||||
before(async () => {
|
||||
projectRoot = mkdtempSync(join(tmpdir(), 'ccw-orchestrator-project-'));
|
||||
|
||||
// Reduce noise from executor internals during tests
|
||||
mock.method(console, 'log', () => {});
|
||||
mock.method(console, 'error', () => {});
|
||||
|
||||
mod = await import(orchestratorRoutesUrl.href);
|
||||
|
||||
// Seed a minimal empty flow (no nodes)
|
||||
const flowsDir = join(projectRoot, '.workflow', '.orchestrator', 'flows');
|
||||
mkdirSync(flowsDir, { recursive: true });
|
||||
const flowId = 'flow-test-empty';
|
||||
const now = new Date().toISOString();
|
||||
writeFileSync(
|
||||
join(flowsDir, `${flowId}.json`),
|
||||
JSON.stringify({
|
||||
id: flowId,
|
||||
name: 'Empty Flow',
|
||||
description: '',
|
||||
version: '1.0.0',
|
||||
created_at: now,
|
||||
updated_at: now,
|
||||
nodes: [],
|
||||
edges: [],
|
||||
variables: {},
|
||||
metadata: { source: 'local' },
|
||||
}, null, 2),
|
||||
'utf8'
|
||||
);
|
||||
|
||||
server = http.createServer(async (req, res) => {
|
||||
const url = new URL(req.url || '/', 'http://localhost');
|
||||
const pathname = url.pathname;
|
||||
|
||||
const ctx = {
|
||||
pathname,
|
||||
url,
|
||||
req,
|
||||
res,
|
||||
initialPath: projectRoot,
|
||||
handlePostRequest,
|
||||
broadcastToClients() {},
|
||||
};
|
||||
|
||||
try {
|
||||
const handled = await mod.handleOrchestratorRoutes(ctx);
|
||||
if (!handled) {
|
||||
res.writeHead(404, { 'Content-Type': 'application/json' });
|
||||
res.end(JSON.stringify({ error: 'Not Found' }));
|
||||
}
|
||||
} catch (err) {
|
||||
res.writeHead(500, { 'Content-Type': 'application/json' });
|
||||
res.end(JSON.stringify({ error: err instanceof Error ? err.message : String(err) }));
|
||||
}
|
||||
});
|
||||
|
||||
await new Promise((resolve) => server.listen(0, () => resolve()));
|
||||
const addr = server.address();
|
||||
const port = typeof addr === 'object' && addr ? addr.port : 0;
|
||||
baseUrl = `http://127.0.0.1:${port}`;
|
||||
});
|
||||
|
||||
after(() => {
|
||||
if (server) server.close();
|
||||
if (projectRoot) rmSync(projectRoot, { recursive: true, force: true });
|
||||
});
|
||||
|
||||
it('executes an empty flow to completion', async () => {
|
||||
const start = await requestJson(baseUrl, 'POST', '/api/orchestrator/flows/flow-test-empty/execute', {});
|
||||
assert.equal(start.status, 200);
|
||||
assert.equal(start.json.success, true);
|
||||
assert.ok(start.json.data.execId);
|
||||
|
||||
const execId = start.json.data.execId;
|
||||
|
||||
// Poll until completed (should be very fast for empty flow)
|
||||
let state = null;
|
||||
for (let i = 0; i < 50; i++) {
|
||||
// eslint-disable-next-line no-await-in-loop
|
||||
await new Promise((r) => setTimeout(r, 20));
|
||||
// eslint-disable-next-line no-await-in-loop
|
||||
const r = await requestJson(baseUrl, 'GET', `/api/orchestrator/executions/${encodeURIComponent(execId)}`);
|
||||
if (r.status === 200 && r.json?.success) {
|
||||
state = r.json.data;
|
||||
if (state.status === 'completed') break;
|
||||
}
|
||||
}
|
||||
|
||||
assert.ok(state, 'expected execution state to be readable');
|
||||
assert.equal(state.status, 'completed');
|
||||
assert.ok(state.startedAt);
|
||||
assert.ok(state.completedAt);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user