refactor: remove legacy patterns system, unify on workflows

Remove the entire patterns domain model, IPC channels, sidecar services,
renderer components, and tests. Sessions now bind exclusively to workflows
via workflowId. Builtin workflows replace builtin patterns.

Backend:
- Make AgentNodeConfig standalone (no longer extends PatternAgentDefinition)
- Add WorkflowOrchestrationMode and WorkflowExecutionDefinition
- Create builtin workflows (single-agent, sequential, concurrent, handoff, group-chat)
- Rewrite session model config helpers for workflow-only
- Remove pattern IPC channels, handlers, and preload bindings
- Merge createSession/createWorkflowSession into single method
- Remove sidecar PatternGraphResolver, PatternValidator, pattern DTOs
- Add workspace migration for legacy sessions (patternId -> workflowId)

Frontend:
- Delete PatternEditor, pattern-graph components, patternGraph lib
- Delete NewSessionModal (session creation uses workflows directly)
- Remove PatternsSection from SettingsPanel
- Update App.tsx, ChatPane, ActivityPanel, Sidebar, RunTimeline,
  AgentConfigFields, InlinePills, sessionActivity to use workflow types
- Delete pattern.ts domain module

78 files changed across backend and frontend.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
David KayaandCopilot committed 2026-04-06 22:37:00 +02:00
1 parent 69d0804161
commit 805e369b67
78 files changed
+2303 -4441

No files matched your search

-5
View File
@@ -12,9 +12,6 @@ export const ipcChannels = {
rescanProjectCustomization: 'project:rescan-customization',
resolveProjectDiscoveredTooling: 'project:resolve-discovered-tooling',
setProjectAgentProfileEnabled: 'project:set-agent-profile-enabled',
savePattern: 'patterns:save',
deletePattern: 'patterns:delete',
setPatternFavorite: 'patterns:set-favorite',
saveWorkflow: 'workflows:save',
saveWorkflowTemplate: 'workflows:save-template',
deleteWorkflow: 'workflows:delete',
@@ -22,7 +19,6 @@ export const ipcChannels = {
createWorkflowFromTemplate: 'workflows:create-from-template',
exportWorkflow: 'workflows:export',
importWorkflow: 'workflows:import',
upgradePatternToWorkflow: 'workflows:upgrade-pattern',
createWorkflowSession: 'workflows:create-session',
setTheme: 'settings:set-theme',
setTerminalHeight: 'settings:set-terminal-height',
@@ -77,7 +73,6 @@ export const ipcChannels = {
switchProjectGitBranch: 'git:switch-branch',
deleteProjectGitBranch: 'git:delete-branch',
selectProject: 'selection:project',
selectPattern: 'selection:pattern',
selectSession: 'selection:session',
openAppDataFolder: 'troubleshooting:open-app-data-folder',
resetLocalWorkspace: 'troubleshooting:reset-local-workspace',
+3 -31
View File
@@ -1,9 +1,8 @@
import type { ApprovalDecision } from '@shared/domain/approval';
import type { SidecarCapabilities, InteractionMode, MessageMode, QuotaSnapshot } from '@shared/contracts/sidecar';
import type { PatternDefinition, ReasoningEffort } from '@shared/domain/pattern';
import type { WorkflowExportFormat, WorkflowExportResult } from '@shared/domain/workflowSerialization';
import type { WorkflowTemplateCategory } from '@shared/domain/workflowTemplate';
import type { WorkflowDefinition, WorkflowReference } from '@shared/domain/workflow';
import type { ReasoningEffort, WorkflowDefinition, WorkflowReference } from '@shared/domain/workflow';
import type {
ProjectGitBranchSummary,
ProjectGitCommitMessageSuggestion,
@@ -27,18 +26,11 @@ import type { ProjectPromptInvocation } from '@shared/domain/projectCustomizatio
import type { WorkspaceAgentDefinition } from '@shared/domain/workspaceAgent';
export interface CreateSessionInput {
projectId: string;
patternId: string;
}
export interface CreateWorkflowSessionInput {
projectId: string;
workflowId: string;
}
export interface SavePatternInput {
pattern: PatternDefinition;
}
export type CreateWorkflowSessionInput = CreateSessionInput;
export interface SaveWorkflowInput {
workflow: WorkflowDefinition;
@@ -76,16 +68,6 @@ export interface ImportWorkflowInput {
};
}
export interface UpgradePatternToWorkflowInput {
patternId: string;
options?: {
workflowId?: string;
name?: string;
description?: string;
save?: boolean;
};
}
export interface ImportWorkflowResult {
workflow: WorkflowDefinition;
workspace?: WorkspaceState;
@@ -165,11 +147,6 @@ export interface SetSessionArchivedInput {
isArchived: boolean;
}
export interface SetPatternFavoriteInput {
patternId: string;
isFavorite: boolean;
}
export interface SaveMcpServerInput {
server: McpServerDefinition;
}
@@ -331,8 +308,6 @@ export interface ElectronApi {
rescanProjectCustomization(input: RescanProjectCustomizationInput): Promise<WorkspaceState>;
resolveProjectDiscoveredTooling(input: ResolveProjectDiscoveredToolingInput): Promise<WorkspaceState>;
setProjectAgentProfileEnabled(input: SetProjectAgentProfileEnabledInput): Promise<WorkspaceState>;
savePattern(input: SavePatternInput): Promise<WorkspaceState>;
deletePattern(patternId: string): Promise<WorkspaceState>;
saveWorkflow(input: SaveWorkflowInput): Promise<WorkspaceState>;
saveWorkflowTemplate(input: SaveWorkflowTemplateInput): Promise<WorkspaceState>;
deleteWorkflow(workflowId: string): Promise<WorkspaceState>;
@@ -340,7 +315,6 @@ export interface ElectronApi {
createWorkflowFromTemplate(input: CreateWorkflowFromTemplateInput): Promise<WorkspaceState>;
exportWorkflow(input: ExportWorkflowInput): Promise<WorkflowExportResult>;
importWorkflow(input: ImportWorkflowInput): Promise<ImportWorkflowResult>;
upgradePatternToWorkflow(input: UpgradePatternToWorkflowInput): Promise<ImportWorkflowResult>;
saveMcpServer(input: SaveMcpServerInput): Promise<WorkspaceState>;
deleteMcpServer(serverId: string): Promise<WorkspaceState>;
saveLspProfile(input: SaveLspProfileInput): Promise<WorkspaceState>;
@@ -371,9 +345,7 @@ export interface ElectronApi {
updateSessionModelConfig(input: UpdateSessionModelConfigInput): Promise<WorkspaceState>;
querySessions(input: QuerySessionsInput): Promise<SessionQueryResult[]>;
selectProject(projectId?: string): Promise<WorkspaceState>;
selectPattern(patternId?: string): Promise<WorkspaceState>;
selectSession(sessionId?: string): Promise<WorkspaceState>;
setPatternFavorite(input: SetPatternFavoriteInput): Promise<WorkspaceState>;
setTheme(theme: AppearanceTheme): Promise<WorkspaceState>;
setTerminalHeight(input: SetTerminalHeightInput): Promise<WorkspaceState>;
setNotificationsEnabled(enabled: boolean): Promise<WorkspaceState>;
@@ -413,5 +385,5 @@ export interface ElectronApi {
export interface RendererSelectionState {
selectedProject?: ProjectRecord;
selectedPattern?: PatternDefinition;
selectedWorkflow?: WorkflowDefinition;
}
+8 -19
View File
@@ -1,10 +1,14 @@
import type { PatternDefinition, PatternValidationIssue, ReasoningEffort } from '@shared/domain/pattern';
import type { ApprovalCheckpointKind, ApprovalDecision } from '@shared/domain/approval';
import type { ChatMessageRecord } from '@shared/domain/session';
import type { RuntimeToolDefinition } from '@shared/domain/tooling';
import type { ChatMessageAttachment } from '@shared/domain/attachment';
import type { ProjectPromptInvocation } from '@shared/domain/projectCustomization';
import type { WorkflowDefinition, WorkflowValidationIssue } from '@shared/domain/workflow';
import type {
ReasoningEffort,
WorkflowDefinition,
WorkflowOrchestrationMode,
WorkflowValidationIssue,
} from '@shared/domain/workflow';
export interface SidecarModeCapability {
available: boolean;
@@ -54,7 +58,7 @@ export interface SidecarModelCapability {
export interface SidecarCapabilities {
runtime: 'dotnet-maf';
modes: Record<PatternDefinition['mode'], SidecarModeCapability>;
modes: Record<WorkflowOrchestrationMode | 'magentic', SidecarModeCapability>;
models: SidecarModelCapability[];
runtimeTools: RuntimeToolDefinition[];
connection: SidecarConnectionDiagnostics;
@@ -65,12 +69,6 @@ export interface DescribeCapabilitiesCommand {
requestId: string;
}
export interface ValidatePatternCommand {
type: 'validate-pattern';
requestId: string;
pattern: PatternDefinition;
}
export interface ValidateWorkflowCommand {
type: 'validate-workflow';
requestId: string;
@@ -96,8 +94,7 @@ export interface RunTurnCommand {
mode?: InteractionMode;
messageMode?: MessageMode;
projectInstructions?: string;
pattern: PatternDefinition;
workflow?: WorkflowDefinition;
workflow: WorkflowDefinition;
workflowLibrary?: WorkflowDefinition[];
messages: ChatMessageRecord[];
attachments?: ChatMessageAttachment[];
@@ -156,7 +153,6 @@ export interface CopilotSessionListFilter {
export type SidecarCommand =
| DescribeCapabilitiesCommand
| ValidatePatternCommand
| ValidateWorkflowCommand
| RunTurnCommand
| CancelTurnCommand
@@ -235,12 +231,6 @@ export interface CapabilitiesEvent {
capabilities: SidecarCapabilities;
}
export interface PatternValidationEvent {
type: 'pattern-validation';
requestId: string;
issues: PatternValidationIssue[];
}
export interface WorkflowValidationEvent {
type: 'workflow-validation';
requestId: string;
@@ -608,7 +598,6 @@ export interface CommandCompleteEvent {
export type SidecarEvent =
| CapabilitiesEvent
| PatternValidationEvent
| WorkflowValidationEvent
| TurnDeltaEvent
| TurnCompleteEvent
+24 -10
View File
@@ -1,5 +1,5 @@
import type { SidecarModelCapability } from '@shared/contracts/sidecar';
import type { PatternDefinition, ReasoningEffort } from '@shared/domain/pattern';
import type { ReasoningEffort, WorkflowDefinition } from '@shared/domain/workflow';
export type ModelProvider = 'openai' | 'anthropic' | 'google';
@@ -270,17 +270,31 @@ export function resolveReasoningEffort(
return supported[0];
}
export function normalizePatternModels(
pattern: PatternDefinition,
export function normalizeWorkflowModels(
workflow: WorkflowDefinition,
models: ReadonlyArray<ModelDefinition>,
): PatternDefinition {
): WorkflowDefinition {
return {
...pattern,
agents: pattern.agents.map((agent) => {
const model = findModel(agent.model, models);
const reasoningEffort = resolveReasoningEffort(model, agent.reasoningEffort);
...workflow,
graph: {
...workflow.graph,
nodes: workflow.graph.nodes.map((node) => {
if (node.kind !== 'agent' || node.config.kind !== 'agent') {
return node;
}
return reasoningEffort === agent.reasoningEffort ? agent : { ...agent, reasoningEffort };
}),
const model = findModel(node.config.model, models);
const reasoningEffort = resolveReasoningEffort(model, node.config.reasoningEffort);
return reasoningEffort === node.config.reasoningEffort
? node
: {
...node,
config: {
...node.config,
reasoningEffort,
},
};
}),
},
};
}
+44 -21
View File
@@ -4,7 +4,6 @@ import type {
PendingApprovalRecord,
} from '@shared/domain/approval';
import type { ToolCallFileChangePreview } from '@shared/contracts/sidecar';
import type { PatternDefinition, ReasoningEffort } from '@shared/domain/pattern';
import type {
ProjectGitBaselineFile,
ProjectGitChangeSummary,
@@ -17,7 +16,13 @@ import type {
ProjectGitWorkingTreeSnapshot,
ProjectRecord,
} from '@shared/domain/project';
import type { WorkflowDefinition } from '@shared/domain/workflow';
import type {
AgentNodeConfig,
ReasoningEffort,
WorkflowDefinition,
WorkflowOrchestrationMode,
} from '@shared/domain/workflow';
import { resolveWorkflowAgents } from '@shared/domain/workflow';
import { createId } from '@shared/utils/ids';
export type SessionRunStatus = 'running' | 'completed' | 'cancelled' | 'error';
@@ -74,11 +79,9 @@ export interface SessionRunRecord {
projectPath: string;
workingDirectory?: string;
workspaceKind: SessionRunWorkspaceKind;
patternId: string;
patternName: string;
patternMode: PatternDefinition['mode'];
workflowId?: string;
workflowName?: string;
workflowId: string;
workflowName: string;
workflowMode: WorkflowOrchestrationMode;
triggerMessageId: string;
startedAt: string;
completedAt?: string;
@@ -90,13 +93,18 @@ export interface SessionRunRecord {
postRunGitSummary?: ProjectGitRunChangeSummary;
}
export type SessionRunWorkflowInput =
| WorkflowDefinition
| (Pick<WorkflowDefinition, 'id' | 'name' | 'settings'> & {
agents: Pick<AgentNodeConfig, 'id' | 'name' | 'model' | 'reasoningEffort'>[];
});
export interface CreateSessionRunRecordInput {
requestId: string;
project: Pick<ProjectRecord, 'id' | 'path'>;
workingDirectory?: string;
workspaceKind: SessionRunWorkspaceKind;
pattern: Pick<PatternDefinition, 'id' | 'name' | 'mode' | 'agents'>;
workflow?: Pick<WorkflowDefinition, 'id' | 'name'>;
workflow: SessionRunWorkflowInput;
triggerMessageId: string;
startedAt: string;
preRunGitSnapshot?: ProjectGitWorkingTreeSnapshot;
@@ -603,7 +611,23 @@ function settleOpenMessageEvents(
};
}
function resolveSessionRunWorkflowAgents(
workflow: SessionRunWorkflowInput,
): ReadonlyArray<Pick<AgentNodeConfig, 'id' | 'name' | 'model' | 'reasoningEffort'>> {
if ('graph' in workflow) {
return resolveWorkflowAgents(workflow).map((agent) => ({
id: agent.id,
name: agent.name,
model: agent.model,
reasoningEffort: agent.reasoningEffort,
}));
}
return workflow.agents;
}
export function createSessionRunRecord(input: CreateSessionRunRecordInput): SessionRunRecord {
const agents = resolveSessionRunWorkflowAgents(input.workflow);
return {
id: createId('run'),
requestId: input.requestId,
@@ -611,11 +635,9 @@ export function createSessionRunRecord(input: CreateSessionRunRecordInput): Sess
projectPath: input.project.path,
workingDirectory: normalizeOptionalString(input.workingDirectory),
workspaceKind: input.workspaceKind,
patternId: input.pattern.id,
patternName: input.pattern.name,
patternMode: input.pattern.mode,
workflowId: normalizeOptionalString(input.workflow?.id),
workflowName: normalizeOptionalString(input.workflow?.name),
workflowId: input.workflow.id,
workflowName: input.workflow.name,
workflowMode: input.workflow.settings.orchestrationMode ?? 'single',
triggerMessageId: input.triggerMessageId,
startedAt: input.startedAt,
status: 'running',
@@ -623,7 +645,7 @@ export function createSessionRunRecord(input: CreateSessionRunRecordInput): Sess
preRunGitSnapshot: normalizeWorkingTreeSnapshot(input.preRunGitSnapshot),
preRunGitBaselineFiles: normalizeGitBaselineFiles(input.preRunGitBaselineFiles),
postRunGitSummary: undefined,
agents: input.pattern.agents
agents: agents
.map((agent): RunTimelineAgentRecord => ({
agentId: agent.id,
agentName: agent.name,
@@ -659,13 +681,11 @@ export function normalizeSessionRunRecords(
const projectId = normalizeOptionalString(run.projectId);
const projectPath = normalizeOptionalString(run.projectPath);
const workingDirectory = normalizeOptionalString(run.workingDirectory);
const patternId = normalizeOptionalString(run.patternId);
const patternName = normalizeOptionalString(run.patternName);
const workflowId = normalizeOptionalString(run.workflowId);
const workflowName = normalizeOptionalString(run.workflowName);
const triggerMessageId = normalizeOptionalString(run.triggerMessageId);
const startedAt = normalizeOptionalString(run.startedAt);
if (!id || !requestId || !projectId || !projectPath || !patternId || !patternName || !triggerMessageId || !startedAt) {
if (!id || !requestId || !projectId || !projectPath || !workflowId || !workflowName || !triggerMessageId || !startedAt) {
return [];
}
@@ -677,11 +697,14 @@ export function normalizeSessionRunRecords(
projectPath,
workingDirectory,
workspaceKind: run.workspaceKind === 'scratchpad' ? 'scratchpad' : 'project',
patternId,
patternName,
patternMode: run.patternMode,
workflowId,
workflowName,
workflowMode: run.workflowMode === 'concurrent'
|| run.workflowMode === 'handoff'
|| run.workflowMode === 'group-chat'
|| run.workflowMode === 'single'
? run.workflowMode
: 'sequential',
triggerMessageId,
startedAt,
completedAt: normalizeOptionalString(run.completedAt),
+68 -38
View File
@@ -1,25 +1,32 @@
import { buildSessionTitle, type PatternDefinition, type ReasoningEffort } from '@shared/domain/pattern';
import {
createSessionToolingSelection,
normalizeSessionToolingSelection,
type SessionToolingSelection,
} from '@shared/domain/tooling';
import {
normalizeSessionApprovalSettings,
resolveEffectiveApprovalPolicy,
type PendingApprovalRecord,
type SessionApprovalSettings,
} from '@shared/domain/approval';
import type { SessionRunRecord } from '@shared/domain/runTimeline';
import type { PendingUserInputRecord } from '@shared/domain/userInput';
import type { PendingPlanReviewRecord } from '@shared/domain/planReview';
import type { PendingMcpAuthRecord } from '@shared/domain/mcpAuth';
import type { ChatMessageAttachment } from '@shared/domain/attachment';
import type { InteractionMode } from '@shared/contracts/sidecar';
import type { PendingMcpAuthRecord } from '@shared/domain/mcpAuth';
import type { PendingPlanReviewRecord } from '@shared/domain/planReview';
import {
normalizeProjectPromptInvocation,
type ProjectPromptInvocation,
} from '@shared/domain/projectCustomization';
import type { SessionRunRecord } from '@shared/domain/runTimeline';
import {
createSessionToolingSelection,
normalizeSessionToolingSelection,
type SessionToolingSelection,
} from '@shared/domain/tooling';
import type { PendingUserInputRecord } from '@shared/domain/userInput';
import {
buildWorkflowExecutionDefinition,
resolveWorkflowAgentNodes,
type ReasoningEffort,
type WorkflowDefinition,
type WorkflowResolutionOptions,
} from '@shared/domain/workflow';
import type { InteractionMode } from '@shared/contracts/sidecar';
import { buildMarkdownExcerpt } from '@shared/utils/markdownText';
export type ChatRole = 'system' | 'user' | 'assistant';
export type ChatMessageKind = 'response' | 'thinking';
@@ -56,8 +63,7 @@ export interface SessionBranchOrigin {
export interface SessionRecord {
id: string;
projectId: string;
patternId: string;
workflowId?: string;
workflowId: string;
title: string;
titleSource?: SessionTitleSource;
createdAt: string;
@@ -120,20 +126,26 @@ export function normalizeSessionBranchOrigin(
export function resolveSessionTitle(
session: Pick<SessionRecord, 'title' | 'titleSource'>,
pattern: PatternDefinition,
workflow: Pick<WorkflowDefinition, 'name'>,
messages: ChatMessageRecord[],
): string {
if (session.titleSource === 'manual') {
return session.title;
}
return buildSessionTitle(pattern, messages);
const firstUserMessage = messages.find((message) => message.role === 'user');
if (!firstUserMessage) {
return workflow.name;
}
return buildMarkdownExcerpt(firstUserMessage.content, 48) ?? workflow.name;
}
export function createSessionModelConfig(
pattern: PatternDefinition,
workflow: WorkflowDefinition,
options?: WorkflowResolutionOptions,
): SessionModelConfig | undefined {
const primaryAgent = pattern.agents[0];
const primaryAgent = buildWorkflowExecutionDefinition(workflow, options).agents[0];
if (!primaryAgent) {
return undefined;
}
@@ -158,9 +170,9 @@ export function resolveSessionApprovalSettings(
export function resolveSessionModelConfig(
session: SessionRecord,
pattern: PatternDefinition,
workflow: WorkflowDefinition,
): SessionModelConfig | undefined {
const defaults = createSessionModelConfig(pattern);
const defaults = createSessionModelConfig(workflow);
if (!defaults) {
return undefined;
}
@@ -187,38 +199,56 @@ export function normalizeChatMessageRecord(message: ChatMessageRecord): ChatMess
}
export function applySessionModelConfig(
pattern: PatternDefinition,
workflow: WorkflowDefinition,
session: SessionRecord,
): PatternDefinition {
const config = resolveSessionModelConfig(session, pattern);
const primaryAgent = pattern.agents[0];
if (!config || !primaryAgent) {
return pattern;
): WorkflowDefinition {
const config = resolveSessionModelConfig(session, workflow);
if (!config) {
return workflow;
}
return {
...pattern,
agents: [
{
...primaryAgent,
let applied = false;
const nodes = workflow.graph.nodes.map((node, index) => {
if (applied || node.kind !== 'agent' || node.config.kind !== 'agent') {
return node;
}
applied = true;
return {
...node,
config: {
...node.config,
model: config.model,
reasoningEffort: config.reasoningEffort,
},
...pattern.agents.slice(1),
],
};
};
});
return applied
? {
...workflow,
graph: {
...workflow.graph,
nodes,
},
}
: workflow;
}
export function applySessionApprovalSettings(
pattern: PatternDefinition,
workflow: WorkflowDefinition,
session: Pick<SessionRecord, 'approvalSettings'>,
): PatternDefinition {
): WorkflowDefinition {
if (session.approvalSettings === undefined) {
return pattern;
return workflow;
}
const execution = buildWorkflowExecutionDefinition(workflow);
return {
...pattern,
approvalPolicy: resolveEffectiveApprovalPolicy(pattern.approvalPolicy, session.approvalSettings),
...workflow,
settings: {
...workflow.settings,
approvalPolicy: resolveEffectiveApprovalPolicy(execution.approvalPolicy, session.approvalSettings),
},
};
}
+23 -28
View File
@@ -1,4 +1,3 @@
import type { PatternDefinition } from '@shared/domain/pattern';
import { isScratchpadProject, type ProjectRecord } from '@shared/domain/project';
import type { ChatMessageAttachment } from '@shared/domain/attachment';
import {
@@ -14,16 +13,16 @@ import {
type SessionStatus,
} from '@shared/domain/session';
import type { WorkspaceState } from '@shared/domain/workspace';
import { buildWorkflowExecutionPattern } from '@shared/domain/workflow';
import type { WorkflowDefinition } from '@shared/domain/workflow';
export type SessionQueryMatchField = 'title' | 'message' | 'project' | 'pattern';
export type SessionQueryMatchField = 'title' | 'message' | 'project' | 'workflow';
export type SessionWorkspaceKind = 'project' | 'scratchpad';
export interface QuerySessionsInput {
searchText?: string;
statuses?: SessionStatus[];
projectIds?: string[];
patternIds?: string[];
workflowIds?: string[];
workspaceKinds?: SessionWorkspaceKind[];
includeArchived?: boolean;
onlyPinned?: boolean;
@@ -38,17 +37,17 @@ export interface SessionQueryResult {
const fieldWeights: Record<SessionQueryMatchField, number> = {
title: 12,
project: 8,
pattern: 7,
workflow: 7,
message: 4,
};
const orderedMatchFields: SessionQueryMatchField[] = ['title', 'message', 'project', 'pattern'];
const orderedMatchFields: SessionQueryMatchField[] = ['title', 'message', 'project', 'workflow'];
interface SessionSearchFields {
title: string;
message: string;
project: string;
pattern: string;
workflow: string;
}
function normalizeSearchText(value: string): string {
@@ -68,18 +67,21 @@ function tokenizeSearchText(value?: string): string[] {
function buildSearchFields(
session: SessionRecord,
project?: ProjectRecord,
pattern?: PatternDefinition,
workflow?: WorkflowDefinition,
): SessionSearchFields {
return {
title: normalizeSearchText(session.title),
message: normalizeSearchText(session.messages.map((message) => message.content).join('\n')),
project: normalizeSearchText([project?.name, project?.path].filter(Boolean).join('\n')),
pattern: normalizeSearchText(
workflow: normalizeSearchText(
[
pattern?.name,
pattern?.description,
pattern?.mode,
...(pattern?.agents.flatMap((agent) => [agent.name, agent.description]) ?? []),
workflow?.name,
workflow?.description,
workflow?.settings.orchestrationMode,
...(workflow?.graph.nodes.flatMap((node) =>
node.kind === 'agent' && node.config.kind === 'agent'
? [node.config.name, node.config.description]
: []) ?? []),
]
.filter(Boolean)
.join('\n'),
@@ -146,7 +148,7 @@ function matchesFilters(
return false;
}
if (input.patternIds && input.patternIds.length > 0 && !input.patternIds.includes(session.patternId)) {
if (input.workflowIds && input.workflowIds.length > 0 && !input.workflowIds.includes(session.workflowId)) {
return false;
}
@@ -280,7 +282,7 @@ export function duplicateSessionRecord(
export function branchSessionRecord(
session: SessionRecord,
pattern: PatternDefinition,
workflow: WorkflowDefinition,
sessionId: string,
messageId: string,
branchedAt: string,
@@ -299,7 +301,7 @@ export function branchSessionRecord(
return {
...createDerivedSessionRecord(session, sessionId, branchedAt),
title: resolveSessionTitle(session, pattern, branchedMessages),
title: resolveSessionTitle(session, workflow, branchedMessages),
messages: branchedMessages,
branchOrigin: createBranchOrigin(session, messageId, sourceMessageIndex, branchedAt, 'branch'),
};
@@ -338,7 +340,7 @@ export function setSessionMessagePinnedRecord(
export function regenerateSessionRecord(
session: SessionRecord,
pattern: PatternDefinition,
workflow: WorkflowDefinition,
sessionId: string,
messageId: string,
regeneratedAt: string,
@@ -373,7 +375,7 @@ export function regenerateSessionRecord(
return {
...createDerivedSessionRecord(session, sessionId, regeneratedAt),
title: resolveSessionTitle(session, pattern, regeneratedMessages),
title: resolveSessionTitle(session, workflow, regeneratedMessages),
messages: regeneratedMessages,
branchOrigin: createBranchOrigin(session, messageId, sourceMessageIndex, regeneratedAt, 'regenerate'),
};
@@ -381,7 +383,7 @@ export function regenerateSessionRecord(
export function editAndResendSessionRecord(
session: SessionRecord,
pattern: PatternDefinition,
workflow: WorkflowDefinition,
sessionId: string,
messageId: string,
content: string,
@@ -410,7 +412,7 @@ export function editAndResendSessionRecord(
return {
...createDerivedSessionRecord(session, sessionId, editedAt),
title: resolveSessionTitle(session, pattern, editedMessages),
title: resolveSessionTitle(session, workflow, editedMessages),
messages: editedMessages,
branchOrigin: createBranchOrigin(session, messageId, sourceMessageIndex, editedAt, 'edit-and-resend'),
};
@@ -456,7 +458,6 @@ export function listPinnedMessages(workspace: WorkspaceState): PinnedMessageHit[
export function querySessions(workspace: WorkspaceState, input: QuerySessionsInput): SessionQueryResult[] {
const projectsById = new Map<string, ProjectRecord>(workspace.projects.map((project) => [project.id, project]));
const patternsById = new Map<string, PatternDefinition>(workspace.patterns.map((pattern) => [pattern.id, pattern]));
const workflowsById = new Map(workspace.workflows.map((workflow) => [workflow.id, workflow]));
const searchTokens = tokenizeSearchText(input.searchText);
@@ -471,13 +472,7 @@ export function querySessions(workspace: WorkspaceState, input: QuerySessionsInp
buildSearchFields(
session,
projectsById.get(session.projectId),
patternsById.get(session.patternId)
?? (session.workflowId
? (() => {
const workflow = workflowsById.get(session.workflowId);
return workflow ? buildWorkflowExecutionPattern(workflow) : undefined;
})()
: undefined),
workflowsById.get(session.workflowId),
),
searchTokens,
);
+416 -20
View File
@@ -1,10 +1,23 @@
import type { PatternAgentCopilotConfig } from '@shared/contracts/sidecar';
import type { ApprovalPolicy } from '@shared/domain/approval';
import {
syncPatternGraph,
type PatternDefinition,
type PatternGraph,
type PatternAgentDefinition,
} from '@shared/domain/pattern';
export type ReasoningEffort = 'low' | 'medium' | 'high' | 'xhigh';
export type WorkflowOrchestrationMode = 'single' | 'sequential' | 'concurrent' | 'handoff' | 'group-chat';
export interface WorkflowAgentOverrides {
name?: string;
description?: string;
instructions?: string;
model?: string;
reasoningEffort?: ReasoningEffort;
}
export const reasoningEffortOptions: ReadonlyArray<{ value: ReasoningEffort; label: string }> = [
{ value: 'low', label: 'Low' },
{ value: 'medium', label: 'Medium' },
{ value: 'high', label: 'High' },
{ value: 'xhigh', label: 'Very High' },
];
export type WorkflowNodeKind =
| 'start'
@@ -40,6 +53,7 @@ export interface WorkflowStateScope {
export interface WorkflowSettings {
checkpointing: WorkflowCheckpointSettings;
executionMode: WorkflowExecutionMode;
orchestrationMode?: WorkflowOrchestrationMode;
maxIterations?: number;
approvalPolicy?: ApprovalPolicy;
stateScopes?: WorkflowStateScope[];
@@ -56,8 +70,17 @@ export interface EndNodeConfig {
outputType?: string;
}
export interface AgentNodeConfig extends PatternAgentDefinition {
export interface AgentNodeConfig {
kind: 'agent';
id: string;
name: string;
description: string;
instructions: string;
model: string;
reasoningEffort?: ReasoningEffort;
copilot?: PatternAgentCopilotConfig;
workspaceAgentId?: string;
overrides?: WorkflowAgentOverrides;
}
export interface InvokeFunctionConfig {
@@ -163,6 +186,16 @@ export interface WorkflowResolutionOptions {
resolveWorkflow?: (workflowId: string) => WorkflowDefinition | undefined;
}
export interface WorkflowExecutionDefinition {
id: string;
name: string;
description: string;
orchestrationMode: WorkflowOrchestrationMode;
maxIterations: number;
approvalPolicy?: ApprovalPolicy;
agents: AgentNodeConfig[];
}
const executableNodeKinds = new Set<WorkflowNodeKind>([
'start',
'end',
@@ -177,6 +210,25 @@ function normalizeOptionalString(value?: string): string | undefined {
return trimmed ? trimmed : undefined;
}
function normalizeWorkflowOrchestrationMode(
mode?: WorkflowOrchestrationMode,
): WorkflowOrchestrationMode | undefined {
switch (mode) {
case 'single':
case 'sequential':
case 'concurrent':
case 'handoff':
case 'group-chat':
return mode;
default:
return undefined;
}
}
export function isReasoningEffort(value: string | undefined): value is ReasoningEffort {
return reasoningEffortOptions.some((option) => option.value === value);
}
function normalizePosition(position?: Partial<WorkflowPosition>): WorkflowPosition {
return {
x: typeof position?.x === 'number' && Number.isFinite(position.x) ? position.x : 0,
@@ -311,6 +363,7 @@ export function normalizeWorkflowDefinition(workflow: WorkflowDefinition): Workf
enabled: workflow.settings?.checkpointing?.enabled ?? false,
},
executionMode: workflow.settings?.executionMode === 'lockstep' ? 'lockstep' : 'off-thread',
orchestrationMode: normalizeWorkflowOrchestrationMode(workflow.settings?.orchestrationMode),
maxIterations:
typeof workflow.settings?.maxIterations === 'number' && workflow.settings.maxIterations > 0
? Math.round(workflow.settings.maxIterations)
@@ -346,13 +399,14 @@ export function resolveWorkflowAgentNodes(workflow: WorkflowDefinition): Workflo
});
}
export function resolveWorkflowAgents(workflow: WorkflowDefinition): PatternAgentDefinition[] {
export function resolveWorkflowAgents(workflow: WorkflowDefinition): AgentNodeConfig[] {
return resolveWorkflowAgentNodes(workflow).flatMap((node) => {
if (node.config.kind !== 'agent') {
return [];
}
return [{
kind: 'agent',
id: node.config.id || node.id,
name: node.config.name,
description: node.config.description,
@@ -410,7 +464,7 @@ function resolveWorkflowExecutionAgents(
options?: WorkflowResolutionOptions,
visitedReferencedWorkflowIds = new Set<string>([workflow.id]),
visitedInlineWorkflows = new Set<WorkflowDefinition>(),
): PatternAgentDefinition[] {
): AgentNodeConfig[] {
const agents = [...resolveWorkflowAgents(workflow)];
for (const node of workflow.graph.nodes) {
@@ -448,10 +502,15 @@ function resolveWorkflowExecutionAgents(
return agents;
}
function inferWorkflowPatternMode(
export function inferWorkflowOrchestrationMode(
workflow: WorkflowDefinition,
options?: WorkflowResolutionOptions,
): PatternDefinition['mode'] {
): WorkflowOrchestrationMode {
const configuredMode = normalizeWorkflowOrchestrationMode(workflow.settings?.orchestrationMode);
if (configuredMode) {
return configuredMode;
}
const hasFanEdges = hasWorkflowExecutionFanEdges(workflow, options);
const agentCount = resolveWorkflowExecutionAgents(workflow, options).length;
if (hasFanEdges) {
@@ -461,30 +520,366 @@ function inferWorkflowPatternMode(
return agentCount <= 1 ? 'single' : 'sequential';
}
export function buildWorkflowExecutionPattern(
export function buildWorkflowExecutionDefinition(
workflow: WorkflowDefinition,
options?: WorkflowResolutionOptions,
): PatternDefinition {
): WorkflowExecutionDefinition {
const agents = resolveWorkflowExecutionAgents(workflow, options);
const pattern: PatternDefinition = {
return {
id: workflow.id,
name: workflow.name,
description: workflow.description,
mode: inferWorkflowPatternMode(workflow, options),
availability: 'available',
orchestrationMode: inferWorkflowOrchestrationMode(workflow, options),
maxIterations: workflow.settings.maxIterations ?? 5,
approvalPolicy: workflow.settings.approvalPolicy,
agents,
createdAt: workflow.createdAt,
updatedAt: workflow.updatedAt,
};
}
const builtinWorkflowModels = {
claude: 'claude-opus-4.5',
gpt54: 'gpt-5.4',
gpt53: 'gpt-5.3-codex',
} as const;
function createStartNode(x: number, y: number): WorkflowNode {
return { id: 'start', kind: 'start', label: 'Start', position: { x, y }, config: { kind: 'start' } };
}
function createEndNode(x: number, y: number): WorkflowNode {
return { id: 'end', kind: 'end', label: 'End', position: { x, y }, config: { kind: 'end' } };
}
function createAgentNode(
id: string,
label: string,
description: string,
instructions: string,
model: string,
reasoningEffort: ReasoningEffort | undefined,
x: number,
y: number,
order: number,
): WorkflowNode {
return {
...pattern,
graph: syncPatternGraph(pattern).graph as PatternGraph,
id,
kind: 'agent',
label,
position: { x, y },
order,
config: {
kind: 'agent',
id,
name: label,
description,
instructions,
model,
reasoningEffort,
},
};
}
function createWorkflowEdge(
id: string,
source: string,
target: string,
kind: WorkflowEdgeKind = 'direct',
overrides?: Partial<Omit<WorkflowEdge, 'id' | 'source' | 'target' | 'kind'>>,
): WorkflowEdge {
return {
id,
source,
target,
kind,
...overrides,
};
}
function createBuiltinWorkflow(
workflow: Omit<WorkflowDefinition, 'createdAt' | 'updatedAt'>,
timestamp: string,
): WorkflowDefinition {
return normalizeWorkflowDefinition({
...workflow,
createdAt: timestamp,
updatedAt: timestamp,
});
}
export function createBuiltinWorkflows(timestamp: string): WorkflowDefinition[] {
return [
createBuiltinWorkflow({
id: 'workflow-single-chat',
name: '1-on-1 Copilot Chat',
description: 'Direct human-agent conversation for a selected project.',
graph: {
nodes: [
createStartNode(0, 0),
createAgentNode(
'agent-single-primary',
'Primary Agent',
'General-purpose project assistant.',
'You are a helpful coding assistant working inside the selected project.',
builtinWorkflowModels.gpt54,
'high',
220,
0,
0,
),
createEndNode(440, 0),
],
edges: [
createWorkflowEdge('edge-start-to-agent-single-primary', 'start', 'agent-single-primary'),
createWorkflowEdge('edge-agent-single-primary-to-end', 'agent-single-primary', 'end'),
],
},
settings: {
checkpointing: { enabled: false },
executionMode: 'off-thread',
orchestrationMode: 'single',
maxIterations: 1,
},
}, timestamp),
createBuiltinWorkflow({
id: 'workflow-sequential-review',
name: 'Sequential Trio Review',
description: 'Agents execute in order, each seeing the full conversation and appending to a shared transcript.',
graph: {
nodes: [
createStartNode(0, 0),
createAgentNode(
'agent-sequential-analyst',
'Analyst',
'Breaks the task down and captures risks.',
'Analyze the request, identify constraints, and produce a short working plan.',
builtinWorkflowModels.gpt54,
'high',
220,
0,
0,
),
createAgentNode(
'agent-sequential-builder',
'Builder',
'Translates the plan into a practical implementation.',
'Use the prior context to propose a concrete implementation.',
builtinWorkflowModels.gpt53,
'medium',
440,
0,
1,
),
createAgentNode(
'agent-sequential-reviewer',
'Reviewer',
'Checks the proposal for gaps and edge cases.',
'Review the previous answer, tighten it, and call out any missing edge cases.',
builtinWorkflowModels.claude,
'medium',
660,
0,
2,
),
createEndNode(880, 0),
],
edges: [
createWorkflowEdge('edge-start-to-agent-sequential-analyst', 'start', 'agent-sequential-analyst'),
createWorkflowEdge('edge-agent-sequential-analyst-to-agent-sequential-builder', 'agent-sequential-analyst', 'agent-sequential-builder'),
createWorkflowEdge('edge-agent-sequential-builder-to-agent-sequential-reviewer', 'agent-sequential-builder', 'agent-sequential-reviewer'),
createWorkflowEdge('edge-agent-sequential-reviewer-to-end', 'agent-sequential-reviewer', 'end'),
],
},
settings: {
checkpointing: { enabled: false },
executionMode: 'off-thread',
orchestrationMode: 'sequential',
maxIterations: 1,
},
}, timestamp),
createBuiltinWorkflow({
id: 'workflow-concurrent-brainstorm',
name: 'Concurrent Brainstorm',
description: 'Agents work independently in parallel and the final conversation aggregates every response.',
graph: {
nodes: [
createStartNode(0, 120),
createAgentNode(
'agent-concurrent-architect',
'Architect',
'Focuses on architecture and boundaries.',
'Answer from an architecture-first perspective.',
builtinWorkflowModels.gpt54,
'high',
260,
0,
0,
),
createAgentNode(
'agent-concurrent-product',
'Product',
'Focuses on UX and scope.',
'Answer from a product and UX perspective.',
builtinWorkflowModels.claude,
'medium',
260,
120,
1,
),
createAgentNode(
'agent-concurrent-implementer',
'Implementer',
'Focuses on practical delivery.',
'Answer from an implementation and testing perspective.',
builtinWorkflowModels.gpt53,
'medium',
260,
240,
2,
),
createEndNode(520, 120),
],
edges: [
createWorkflowEdge('edge-start-to-agent-concurrent-architect', 'start', 'agent-concurrent-architect', 'fan-out', { fanOutConfig: { strategy: 'broadcast' } }),
createWorkflowEdge('edge-start-to-agent-concurrent-product', 'start', 'agent-concurrent-product', 'fan-out', { fanOutConfig: { strategy: 'broadcast' } }),
createWorkflowEdge('edge-start-to-agent-concurrent-implementer', 'start', 'agent-concurrent-implementer', 'fan-out', { fanOutConfig: { strategy: 'broadcast' } }),
createWorkflowEdge('edge-agent-concurrent-architect-to-end', 'agent-concurrent-architect', 'end', 'fan-in'),
createWorkflowEdge('edge-agent-concurrent-product-to-end', 'agent-concurrent-product', 'end', 'fan-in'),
createWorkflowEdge('edge-agent-concurrent-implementer-to-end', 'agent-concurrent-implementer', 'end', 'fan-in'),
],
},
settings: {
checkpointing: { enabled: false },
executionMode: 'off-thread',
orchestrationMode: 'concurrent',
maxIterations: 1,
},
}, timestamp),
createBuiltinWorkflow({
id: 'workflow-handoff-support',
name: 'Handoff Support Flow',
description: 'A triage agent routes work to specialists, and the next user turn continues when more input is needed.',
graph: {
nodes: [
createStartNode(0, 120),
createAgentNode(
'agent-handoff-triage',
'Triage',
'Routes the request to the right specialist.',
'You triage requests and must hand them off to the most appropriate specialist. For any substantive task, hand off before inspecting files, calling tools, or drafting the implementation yourself. Do not claim that you delegated unless you actually executed the handoff.',
builtinWorkflowModels.gpt54,
'medium',
240,
120,
0,
),
createAgentNode(
'agent-handoff-ux',
'UX Specialist',
'Handles user experience questions.',
'You focus on navigation, UX, and interaction details. Once triage hands work to you, you own the substantive answer.',
builtinWorkflowModels.claude,
'medium',
520,
0,
1,
),
createAgentNode(
'agent-handoff-runtime',
'Runtime Specialist',
'Handles backend and execution details.',
'You focus on runtime, orchestration, and backend integration details. Once triage hands work to you, you own the substantive answer.',
builtinWorkflowModels.gpt53,
'medium',
520,
240,
2,
),
createEndNode(800, 120),
],
edges: [
createWorkflowEdge('edge-start-to-agent-handoff-triage', 'start', 'agent-handoff-triage'),
createWorkflowEdge('edge-agent-handoff-triage-to-end', 'agent-handoff-triage', 'end'),
createWorkflowEdge('edge-agent-handoff-triage-to-agent-handoff-ux', 'agent-handoff-triage', 'agent-handoff-ux'),
createWorkflowEdge('edge-agent-handoff-triage-to-agent-handoff-runtime', 'agent-handoff-triage', 'agent-handoff-runtime'),
createWorkflowEdge('edge-agent-handoff-ux-to-agent-handoff-triage', 'agent-handoff-ux', 'agent-handoff-triage', 'direct', {
isLoop: true,
maxIterations: 4,
condition: { type: 'always' },
}),
createWorkflowEdge('edge-agent-handoff-runtime-to-agent-handoff-triage', 'agent-handoff-runtime', 'agent-handoff-triage', 'direct', {
isLoop: true,
maxIterations: 4,
condition: { type: 'always' },
}),
createWorkflowEdge('edge-agent-handoff-ux-to-end', 'agent-handoff-ux', 'end'),
createWorkflowEdge('edge-agent-handoff-runtime-to-end', 'agent-handoff-runtime', 'end'),
],
},
settings: {
checkpointing: { enabled: true },
executionMode: 'off-thread',
orchestrationMode: 'handoff',
maxIterations: 4,
},
}, timestamp),
createBuiltinWorkflow({
id: 'workflow-group-chat',
name: 'Collaborative Group Chat',
description: 'Agents take turns under a round-robin manager, iteratively refining a shared conversation.',
graph: {
nodes: [
createStartNode(0, 0),
createAgentNode(
'agent-group-writer',
'Writer',
'Produces candidate answers.',
'You draft a concise, useful answer for the task. On later turns, refine your earlier draft based on peer feedback rather than restarting.',
builtinWorkflowModels.gpt54,
'medium',
240,
0,
0,
),
createAgentNode(
'agent-group-reviewer',
'Reviewer',
'Critiques and refines the answer.',
'You review the latest draft and offer specific improvements. Focus on critique and refinement instead of restarting the conversation.',
builtinWorkflowModels.claude,
'medium',
480,
0,
1,
),
createEndNode(720, 0),
],
edges: [
createWorkflowEdge('edge-start-to-agent-group-writer', 'start', 'agent-group-writer'),
createWorkflowEdge('edge-agent-group-writer-to-agent-group-reviewer', 'agent-group-writer', 'agent-group-reviewer', 'direct', {
isLoop: true,
maxIterations: 5,
condition: { type: 'always' },
}),
createWorkflowEdge('edge-agent-group-reviewer-to-agent-group-writer', 'agent-group-reviewer', 'agent-group-writer', 'direct', {
isLoop: true,
maxIterations: 5,
condition: { type: 'always' },
label: 'Loop',
}),
createWorkflowEdge('edge-agent-group-reviewer-to-end', 'agent-group-reviewer', 'end'),
],
},
settings: {
checkpointing: { enabled: false },
executionMode: 'off-thread',
orchestrationMode: 'group-chat',
maxIterations: 5,
},
}, timestamp),
];
}
function addIssue(
issues: WorkflowValidationIssue[],
issue: WorkflowValidationIssue,
@@ -1091,3 +1486,4 @@ export function validateWorkflowDefinition(workflow: WorkflowDefinition): Workfl
return issues;
}
-291
View File
@@ -1,9 +1,3 @@
import {
resolvePatternGraph,
type PatternAgentDefinition,
type PatternDefinition,
type PatternGraphNode,
} from '@shared/domain/pattern';
import {
normalizeWorkflowDefinition,
type WorkflowDefinition,
@@ -45,47 +39,6 @@ function normalizeRequiredString(value: string | undefined, fallback: string): s
return normalizeOptionalString(value) ?? fallback;
}
function toWorkflowIdFromPatternId(patternId: string): string {
const trimmed = patternId.trim();
return trimmed.startsWith('pattern-')
? `workflow-${trimmed.slice('pattern-'.length)}`
: `${trimmed}-workflow`;
}
function createWorkflowEdge(
source: string,
target: string,
kind: WorkflowEdge['kind'],
overrides?: Partial<WorkflowEdge>,
): WorkflowEdge {
return {
id: overrides?.id?.trim() || `workflow-edge-${source}-to-${target}`,
source,
target,
kind,
label: normalizeOptionalString(overrides?.label),
condition: overrides?.condition,
fanOutConfig: overrides?.fanOutConfig,
isLoop: overrides?.isLoop,
maxIterations: overrides?.maxIterations,
};
}
function sortAgentNodes(nodes: ReadonlyArray<PatternGraphNode>): PatternGraphNode[] {
return nodes
.filter((node) => node.kind === 'agent' && node.agentId)
.slice()
.sort((left, right) => {
const leftOrder = left.order ?? Number.MAX_SAFE_INTEGER;
const rightOrder = right.order ?? Number.MAX_SAFE_INTEGER;
if (leftOrder !== rightOrder) {
return leftOrder - rightOrder;
}
return left.id.localeCompare(right.id);
});
}
function normalizeTemplateWorkflow(
workflow: WorkflowDefinition | Partial<WorkflowDefinition> | undefined,
): WorkflowDefinition {
@@ -115,115 +68,6 @@ function normalizeTemplateWorkflow(
});
}
function createWorkflowAgentNode(
patternNode: PatternGraphNode,
agent: PatternAgentDefinition,
fallbackOrder: number,
): WorkflowNode {
return {
id: patternNode.id.trim(),
kind: 'agent',
label: normalizeRequiredString(agent.name, `Agent ${fallbackOrder + 1}`),
position: {
x: patternNode.position.x,
y: patternNode.position.y,
},
order: patternNode.order ?? fallbackOrder,
config: {
kind: 'agent',
...agent,
},
};
}
function mapPatternNodeId(node: PatternGraphNode | undefined): string | undefined {
if (!node) {
return undefined;
}
switch (node.kind) {
case 'user-input':
return 'start';
case 'user-output':
return 'end';
case 'agent':
return node.id.trim();
default:
return undefined;
}
}
function buildWorkflowAdjacency(
edges: ReadonlyArray<WorkflowEdge>,
excludedEdgeId?: string,
): Map<string, string[]> {
const adjacency = new Map<string, string[]>();
for (const edge of edges) {
if (edge.id === excludedEdgeId) {
continue;
}
const next = adjacency.get(edge.source);
if (next) {
next.push(edge.target);
} else {
adjacency.set(edge.source, [edge.target]);
}
}
return adjacency;
}
function canReachWorkflowNode(
edges: ReadonlyArray<WorkflowEdge>,
startNodeId: string,
targetNodeId: string,
excludedEdgeId?: string,
): boolean {
if (startNodeId === targetNodeId) {
return true;
}
const adjacency = buildWorkflowAdjacency(edges, excludedEdgeId);
const queue = [startNodeId];
const visited = new Set<string>();
while (queue.length > 0) {
const current = queue.shift()!;
if (visited.has(current)) {
continue;
}
visited.add(current);
for (const candidate of adjacency.get(current) ?? []) {
if (candidate === targetNodeId) {
return true;
}
queue.push(candidate);
}
}
return false;
}
function markLoopEdges(
edges: ReadonlyArray<WorkflowEdge>,
maxIterations: number,
): WorkflowEdge[] {
return edges.map((edge) => {
const participatesInCycle = canReachWorkflowNode(edges, edge.target, edge.source, edge.id);
if (!participatesInCycle) {
return edge;
}
return {
...edge,
isLoop: true,
maxIterations,
condition: edge.condition ?? { type: 'always' },
};
});
}
export function normalizeWorkflowTemplateDefinition(template: WorkflowTemplateDefinition): WorkflowTemplateDefinition {
const workflow = normalizeTemplateWorkflow(template.workflow);
@@ -240,141 +84,6 @@ export function normalizeWorkflowTemplateDefinition(template: WorkflowTemplateDe
};
}
export function buildWorkflowFromPattern(pattern: PatternDefinition): WorkflowDefinition {
const resolvedGraph = resolvePatternGraph(pattern);
const patternNodesById = new Map(resolvedGraph.nodes.map((node) => [node.id, node]));
const agentsById = new Map(pattern.agents.map((agent) => [agent.id, agent]));
const orderedAgentNodes = sortAgentNodes(resolvedGraph.nodes);
const startPosition = patternNodesById.get('system-user-input')?.position ?? { x: 0, y: 0 };
const endPosition = patternNodesById.get('system-user-output')?.position ?? { x: 500, y: 0 };
const workflowNodes: WorkflowNode[] = [
{
id: 'start',
kind: 'start',
label: 'Start',
position: { x: startPosition.x, y: startPosition.y },
config: { kind: 'start' },
},
...orderedAgentNodes.map((node, index) => {
const agent = node.agentId ? agentsById.get(node.agentId) : undefined;
if (!agent) {
throw new Error(`Pattern "${pattern.id}" references missing agent "${node.agentId ?? node.id}".`);
}
return createWorkflowAgentNode(node, agent, index);
}),
{
id: 'end',
kind: 'end',
label: 'End',
position: { x: endPosition.x, y: endPosition.y },
config: { kind: 'end' },
},
];
const orderedAgentIds = workflowNodes.filter((node) => node.kind === 'agent').map((node) => node.id);
const edges: WorkflowEdge[] = [];
const seenEdges = new Set<string>();
const pushEdge = (edge: WorkflowEdge) => {
const dedupeKey = `${edge.source}:${edge.target}:${edge.kind}:${edge.isLoop === true ? 'loop' : 'plain'}`;
if (seenEdges.has(dedupeKey)) {
return;
}
seenEdges.add(dedupeKey);
edges.push(edge);
};
switch (pattern.mode) {
case 'single':
case 'sequential':
case 'magentic': {
const path = ['start', ...orderedAgentIds, 'end'];
for (let index = 0; index < path.length - 1; index += 1) {
pushEdge(createWorkflowEdge(path[index]!, path[index + 1]!, 'direct'));
}
break;
}
case 'concurrent': {
for (const agentId of orderedAgentIds) {
pushEdge(createWorkflowEdge('start', agentId, 'fan-out', {
fanOutConfig: { strategy: 'broadcast' },
}));
pushEdge(createWorkflowEdge(agentId, 'end', 'fan-in'));
}
break;
}
case 'handoff': {
for (const patternEdge of resolvedGraph.edges) {
const sourceNode = patternNodesById.get(patternEdge.source);
const targetNode = patternNodesById.get(patternEdge.target);
const source = mapPatternNodeId(sourceNode);
const target = mapPatternNodeId(targetNode);
if (!source || !target) {
continue;
}
pushEdge(createWorkflowEdge(source, target, 'direct', {
id: `workflow-${patternEdge.id.trim()}`,
}));
}
break;
}
case 'group-chat': {
const path = ['start', ...orderedAgentIds, 'end'];
for (let index = 0; index < path.length - 1; index += 1) {
const source = path[index]!;
const target = path[index + 1]!;
pushEdge(createWorkflowEdge(source, target, 'direct', source !== 'start' && target !== 'end'
? {
isLoop: true,
maxIterations: pattern.maxIterations,
condition: { type: 'always' },
}
: undefined));
}
if (orderedAgentIds.length > 0) {
pushEdge(createWorkflowEdge(
orderedAgentIds[orderedAgentIds.length - 1]!,
orderedAgentIds[0]!,
'direct',
{
id: `workflow-edge-${orderedAgentIds[orderedAgentIds.length - 1]}-loop-${orderedAgentIds[0]}`,
isLoop: true,
maxIterations: pattern.maxIterations,
condition: { type: 'always' },
label: 'Loop',
},
));
}
break;
}
}
const finalizedEdges = pattern.mode === 'handoff'
? markLoopEdges(edges, Math.max(pattern.maxIterations, 1))
: edges;
return normalizeWorkflowDefinition({
id: toWorkflowIdFromPatternId(pattern.id),
name: pattern.name,
description: pattern.description,
graph: {
nodes: workflowNodes,
edges: finalizedEdges,
},
settings: {
checkpointing: { enabled: false },
executionMode: 'off-thread',
maxIterations: pattern.maxIterations,
approvalPolicy: pattern.approvalPolicy,
},
createdAt: pattern.createdAt,
updatedAt: pattern.updatedAt,
});
}
export function createBuiltinWorkflowTemplates(timestamp: string): WorkflowTemplateDefinition[] {
return [
createCodeReviewPipeline(timestamp),
+2 -9
View File
@@ -1,5 +1,3 @@
import type { PatternDefinition } from '@shared/domain/pattern';
import { createBuiltinPatterns } from '@shared/domain/pattern';
import type { ProjectRecord } from '@shared/domain/project';
import type { SessionRecord } from '@shared/domain/session';
import { createWorkspaceSettings, type WorkspaceSettings } from '@shared/domain/tooling';
@@ -7,22 +5,18 @@ import {
createBuiltinWorkflowTemplates,
type WorkflowTemplateDefinition,
} from '@shared/domain/workflowTemplate';
import type { WorkflowDefinition } from '@shared/domain/workflow';
import { createBuiltinWorkflows, type WorkflowDefinition } from '@shared/domain/workflow';
import { nowIso } from '@shared/utils/ids';
export interface WorkspaceState {
projects: ProjectRecord[];
patterns: PatternDefinition[];
workflows: WorkflowDefinition[];
workflowTemplates: WorkflowTemplateDefinition[];
sessions: SessionRecord[];
settings: WorkspaceSettings;
/** IDs of built-in patterns the user has deleted. Prevents re-adding on load. */
deletedBuiltinPatternIds?: string[];
/** Runtime-only MCP probe progress for live UI updates. */
mcpProbingServerIds?: string[];
selectedProjectId?: string;
selectedPatternId?: string;
selectedWorkflowId?: string;
selectedSessionId?: string;
lastUpdatedAt: string;
@@ -32,8 +26,7 @@ export function createWorkspaceSeed(): WorkspaceState {
const timestamp = nowIso();
return {
projects: [],
patterns: createBuiltinPatterns(timestamp),
workflows: [],
workflows: createBuiltinWorkflows(timestamp),
workflowTemplates: createBuiltinWorkflowTemplates(timestamp),
sessions: [],
settings: createWorkspaceSettings(),
+44 -40
View File
@@ -1,5 +1,9 @@
import type { PatternAgentCopilotConfig } from '@shared/contracts/sidecar';
import type { PatternAgentDefinition, PatternDefinition, ReasoningEffort } from '@shared/domain/pattern';
import type {
AgentNodeConfig,
ReasoningEffort,
WorkflowDefinition,
} from '@shared/domain/workflow';
export interface WorkspaceAgentDefinition {
id: string;
@@ -13,38 +17,32 @@ export interface WorkspaceAgentDefinition {
updatedAt: string;
}
export interface PatternAgentOverrides {
name?: string;
description?: string;
instructions?: string;
model?: string;
reasoningEffort?: ReasoningEffort;
export interface WorkflowAgentUsage {
workflowId: string;
workflowName: string;
}
export interface WorkspaceAgentUsage {
patternId: string;
patternName: string;
function normalizeOptionalString(value?: string): string | undefined {
const trimmed = value?.trim();
return trimmed ? trimmed : undefined;
}
/**
* Resolves a single pattern agent by merging its workspace agent base with
* per-pattern overrides. Returns the agent unchanged if it is inline.
*/
export function resolvePatternAgent(
agent: PatternAgentDefinition,
export function resolveWorkflowAgentNode(
agent: AgentNodeConfig,
workspaceAgents: ReadonlyArray<WorkspaceAgentDefinition>,
): PatternAgentDefinition {
): AgentNodeConfig {
if (!agent.workspaceAgentId) {
return agent;
}
const base = workspaceAgents.find((wa) => wa.id === agent.workspaceAgentId);
const base = workspaceAgents.find((workspaceAgent) => workspaceAgent.id === agent.workspaceAgentId);
if (!base) {
return agent;
}
const overrides = agent.overrides ?? {};
return {
...agent,
id: agent.id,
name: overrides.name ?? base.name,
description: overrides.description ?? base.description,
@@ -57,40 +55,45 @@ export function resolvePatternAgent(
};
}
/**
* Resolves all agents in a pattern, producing a new pattern whose agents
* have workspace-agent references merged with their base definitions.
*/
export function resolvePatternAgents(
pattern: PatternDefinition,
export function resolveWorkflowAgents(
workflow: WorkflowDefinition,
workspaceAgents: ReadonlyArray<WorkspaceAgentDefinition>,
): PatternDefinition {
): WorkflowDefinition {
return {
...pattern,
agents: pattern.agents.map((agent) => resolvePatternAgent(agent, workspaceAgents)),
...workflow,
graph: {
...workflow.graph,
nodes: workflow.graph.nodes.map((node) => {
if (node.kind !== 'agent' || node.config.kind !== 'agent') {
return node;
}
return {
...node,
config: resolveWorkflowAgentNode(node.config, workspaceAgents),
};
}),
},
};
}
/**
* Returns every pattern that references the given workspace agent.
*/
export function findWorkspaceAgentUsages(
agentId: string,
patterns: ReadonlyArray<PatternDefinition>,
): WorkspaceAgentUsage[] {
const usages: WorkspaceAgentUsage[] = [];
for (const pattern of patterns) {
if (pattern.agents.some((a) => a.workspaceAgentId === agentId)) {
usages.push({ patternId: pattern.id, patternName: pattern.name });
workflows: ReadonlyArray<WorkflowDefinition>,
): WorkflowAgentUsage[] {
const usages: WorkflowAgentUsage[] = [];
for (const workflow of workflows) {
if (workflow.graph.nodes.some((node) =>
node.kind === 'agent'
&& node.config.kind === 'agent'
&& node.config.workspaceAgentId === agentId)) {
usages.push({ workflowId: workflow.id, workflowName: workflow.name });
}
}
return usages;
}
/**
* Normalizes a workspace agent definition, trimming string fields and
* ensuring consistent shape.
*/
export function normalizeWorkspaceAgentDefinition(
agent: WorkspaceAgentDefinition,
): WorkspaceAgentDefinition {
@@ -100,5 +103,6 @@ export function normalizeWorkspaceAgentDefinition(
description: agent.description.trim(),
instructions: agent.instructions.trim(),
model: agent.model.trim(),
reasoningEffort: normalizeOptionalString(agent.reasoningEffort) as ReasoningEffort | undefined,
};
}