wip: first run of implementation
This commit is contained in:
94
src/main/agentic/capabilities/registry.ts
Normal file
94
src/main/agentic/capabilities/registry.ts
Normal file
@@ -0,0 +1,94 @@
|
||||
import type { AgentSurface, ProtocolCapabilitySnapshot } from '../protocol/types';
|
||||
|
||||
interface CapabilityRegistryOptions {
|
||||
disabledActions?: string[];
|
||||
disabledWidgets?: string[];
|
||||
disabledTools?: string[];
|
||||
}
|
||||
|
||||
interface CapabilitySnapshotInput {
|
||||
surface: AgentSurface;
|
||||
}
|
||||
|
||||
const COMMON_WIDGETS = [
|
||||
'text',
|
||||
'metric',
|
||||
'list',
|
||||
'table',
|
||||
'action',
|
||||
'chart',
|
||||
'form',
|
||||
'input',
|
||||
'datePicker',
|
||||
'card',
|
||||
'image',
|
||||
'tabs',
|
||||
] as const;
|
||||
|
||||
const COMMON_ACTIONS = [
|
||||
'openSettings',
|
||||
'openPost',
|
||||
'openMedia',
|
||||
'openPanel',
|
||||
'setActiveView',
|
||||
'toggleSidebar',
|
||||
'togglePanel',
|
||||
'toggleAssistantSidebar',
|
||||
] as const;
|
||||
|
||||
const COMMON_TOOLS = [
|
||||
'search_posts',
|
||||
'read_post',
|
||||
'list_posts',
|
||||
'get_media',
|
||||
'list_media',
|
||||
'update_post_metadata',
|
||||
'update_media_metadata',
|
||||
'list_tags',
|
||||
'list_categories',
|
||||
'view_image',
|
||||
'get_post_backlinks',
|
||||
'get_post_outlinks',
|
||||
'get_post_media',
|
||||
'get_media_posts',
|
||||
] as const;
|
||||
|
||||
function unique(values: string[]): string[] {
|
||||
return Array.from(new Set(values));
|
||||
}
|
||||
|
||||
export class CapabilityRegistryService {
|
||||
private readonly disabledActions: Set<string>;
|
||||
private readonly disabledWidgets: Set<string>;
|
||||
private readonly disabledTools: Set<string>;
|
||||
|
||||
constructor(options: CapabilityRegistryOptions = {}) {
|
||||
this.disabledActions = new Set(options.disabledActions ?? []);
|
||||
this.disabledWidgets = new Set(options.disabledWidgets ?? []);
|
||||
this.disabledTools = new Set(options.disabledTools ?? []);
|
||||
}
|
||||
|
||||
getSnapshot(input: CapabilitySnapshotInput): ProtocolCapabilitySnapshot {
|
||||
const widgets = COMMON_WIDGETS.filter((widget) => !this.disabledWidgets.has(widget));
|
||||
|
||||
const surfaceActions = input.surface === 'tab'
|
||||
? COMMON_ACTIONS.filter((action) => action !== 'toggleAssistantSidebar')
|
||||
: COMMON_ACTIONS.filter((action) => action !== 'toggleSidebar');
|
||||
|
||||
const actions = surfaceActions.filter((action) => !this.disabledActions.has(action));
|
||||
const tools = COMMON_TOOLS.filter((tool) => !this.disabledTools.has(tool));
|
||||
|
||||
const disabled = unique([
|
||||
...Array.from(this.disabledActions).map((action) => `action:${action}`),
|
||||
...Array.from(this.disabledWidgets).map((widget) => `widget:${widget}`),
|
||||
...Array.from(this.disabledTools).map((tool) => `tool:${tool}`),
|
||||
]);
|
||||
|
||||
return {
|
||||
widgets: [...widgets],
|
||||
actions: [...actions],
|
||||
tools: [...tools],
|
||||
disabled,
|
||||
};
|
||||
}
|
||||
}
|
||||
63
src/main/agentic/observability/protocolTelemetry.ts
Normal file
63
src/main/agentic/observability/protocolTelemetry.ts
Normal file
@@ -0,0 +1,63 @@
|
||||
export interface ProtocolTurnTelemetryInput {
|
||||
validEnvelope: boolean;
|
||||
repairAttempted: boolean;
|
||||
fallbackUsed: boolean;
|
||||
blockedActions: number;
|
||||
}
|
||||
|
||||
export interface ProtocolTelemetrySnapshot {
|
||||
totalTurns: number;
|
||||
validEnvelopeTurns: number;
|
||||
repairAttempts: number;
|
||||
fallbackTurns: number;
|
||||
blockedActionCount: number;
|
||||
parseValidityRate: number;
|
||||
repairRate: number;
|
||||
fallbackRate: number;
|
||||
}
|
||||
|
||||
export class ProtocolTelemetryService {
|
||||
private totalTurns = 0;
|
||||
private validEnvelopeTurns = 0;
|
||||
private repairAttempts = 0;
|
||||
private fallbackTurns = 0;
|
||||
private blockedActionCount = 0;
|
||||
|
||||
recordTurn(input: ProtocolTurnTelemetryInput): void {
|
||||
this.totalTurns += 1;
|
||||
if (input.validEnvelope) {
|
||||
this.validEnvelopeTurns += 1;
|
||||
}
|
||||
if (input.repairAttempted) {
|
||||
this.repairAttempts += 1;
|
||||
}
|
||||
if (input.fallbackUsed) {
|
||||
this.fallbackTurns += 1;
|
||||
}
|
||||
this.blockedActionCount += input.blockedActions;
|
||||
}
|
||||
|
||||
getSnapshot(): ProtocolTelemetrySnapshot {
|
||||
const denominator = this.totalTurns || 1;
|
||||
return {
|
||||
totalTurns: this.totalTurns,
|
||||
validEnvelopeTurns: this.validEnvelopeTurns,
|
||||
repairAttempts: this.repairAttempts,
|
||||
fallbackTurns: this.fallbackTurns,
|
||||
blockedActionCount: this.blockedActionCount,
|
||||
parseValidityRate: this.validEnvelopeTurns / denominator,
|
||||
repairRate: this.repairAttempts / denominator,
|
||||
fallbackRate: this.fallbackTurns / denominator,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
let protocolTelemetryService: ProtocolTelemetryService | null = null;
|
||||
|
||||
export function getProtocolTelemetryService(): ProtocolTelemetryService {
|
||||
if (!protocolTelemetryService) {
|
||||
protocolTelemetryService = new ProtocolTelemetryService();
|
||||
}
|
||||
|
||||
return protocolTelemetryService;
|
||||
}
|
||||
30
src/main/agentic/policy/actionPolicy.ts
Normal file
30
src/main/agentic/policy/actionPolicy.ts
Normal file
@@ -0,0 +1,30 @@
|
||||
export type ActionPolicyLevel = 'silent' | 'confirm' | 'danger';
|
||||
|
||||
export interface ActionPolicyResolution {
|
||||
level: ActionPolicyLevel;
|
||||
requiresConfirmation: boolean;
|
||||
}
|
||||
|
||||
const ACTION_POLICY_MAP: Record<string, ActionPolicyLevel> = {
|
||||
openPost: 'silent',
|
||||
openMedia: 'silent',
|
||||
openPanel: 'silent',
|
||||
setActiveView: 'silent',
|
||||
toggleSidebar: 'silent',
|
||||
togglePanel: 'silent',
|
||||
toggleAssistantSidebar: 'silent',
|
||||
openSettings: 'confirm',
|
||||
updatePostMetadata: 'confirm',
|
||||
updateMediaMetadata: 'confirm',
|
||||
submitNeedsInput: 'confirm',
|
||||
deletePost: 'danger',
|
||||
deleteMedia: 'danger',
|
||||
};
|
||||
|
||||
export function resolveActionPolicy(action: string): ActionPolicyResolution {
|
||||
const level = ACTION_POLICY_MAP[action] ?? 'danger';
|
||||
return {
|
||||
level,
|
||||
requiresConfirmation: level !== 'silent',
|
||||
};
|
||||
}
|
||||
9
src/main/agentic/protocol/errors.ts
Normal file
9
src/main/agentic/protocol/errors.ts
Normal file
@@ -0,0 +1,9 @@
|
||||
import type { ProtocolValidationError } from './types';
|
||||
|
||||
export function createProtocolValidationError(message: string, details?: string[]): ProtocolValidationError {
|
||||
return {
|
||||
code: 'AGUI_PROTOCOL_VALIDATION_ERROR',
|
||||
message,
|
||||
details,
|
||||
};
|
||||
}
|
||||
312
src/main/agentic/protocol/responseBuilder.ts
Normal file
312
src/main/agentic/protocol/responseBuilder.ts
Normal file
@@ -0,0 +1,312 @@
|
||||
import { randomUUID } from 'crypto';
|
||||
import type {
|
||||
AgentSurface,
|
||||
ProtocolCapabilitySnapshot,
|
||||
ProtocolIntent,
|
||||
ProtocolResponseEnvelope,
|
||||
ProtocolValidationError,
|
||||
} from './types';
|
||||
import { validateProtocolResponseEnvelope } from './validator';
|
||||
import { extractAssistantUiSpec } from './uiSpecParser';
|
||||
import { resolveActionPolicy } from '../policy/actionPolicy';
|
||||
|
||||
export interface ProtocolResponseBuildInput {
|
||||
rawAssistantOutput: string;
|
||||
surface: AgentSurface;
|
||||
capabilities: ProtocolCapabilitySnapshot;
|
||||
}
|
||||
|
||||
export interface ProtocolResponseBuildResult {
|
||||
envelope: ProtocolResponseEnvelope;
|
||||
traceId: string;
|
||||
repairAttempted: boolean;
|
||||
warnings: string[];
|
||||
validationError?: ProtocolValidationError;
|
||||
}
|
||||
|
||||
export class ProtocolResponseBuilder {
|
||||
build(input: ProtocolResponseBuildInput): ProtocolResponseBuildResult {
|
||||
const warnings: string[] = [];
|
||||
|
||||
const directEnvelope = this.parseCanonicalEnvelope(input.rawAssistantOutput);
|
||||
if (directEnvelope) {
|
||||
const normalizedDirectEnvelope = this.applyActionPolicies(directEnvelope);
|
||||
const { filteredEnvelope, warnings: capabilityWarnings } = this.applyCapabilityGuards(normalizedDirectEnvelope, input.capabilities);
|
||||
warnings.push(...capabilityWarnings);
|
||||
const validated = validateProtocolResponseEnvelope(filteredEnvelope);
|
||||
if (validated.ok && validated.value) {
|
||||
return {
|
||||
envelope: validated.value,
|
||||
traceId: validated.value.traceId,
|
||||
repairAttempted: false,
|
||||
warnings,
|
||||
};
|
||||
}
|
||||
|
||||
const fallback = this.fallbackEnvelope(input.rawAssistantOutput);
|
||||
return {
|
||||
envelope: fallback,
|
||||
traceId: fallback.traceId,
|
||||
repairAttempted: true,
|
||||
warnings,
|
||||
validationError: validated.error,
|
||||
};
|
||||
}
|
||||
|
||||
const repaired = this.repairRawEnvelope(input.rawAssistantOutput);
|
||||
if (repaired) {
|
||||
const normalizedRepairedEnvelope = this.applyActionPolicies(repaired);
|
||||
const { filteredEnvelope, warnings: capabilityWarnings } = this.applyCapabilityGuards(normalizedRepairedEnvelope, input.capabilities);
|
||||
warnings.push(...capabilityWarnings);
|
||||
const validated = validateProtocolResponseEnvelope(filteredEnvelope);
|
||||
if (validated.ok && validated.value) {
|
||||
return {
|
||||
envelope: validated.value,
|
||||
traceId: validated.value.traceId,
|
||||
repairAttempted: true,
|
||||
warnings,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
const parsedUi = extractAssistantUiSpec(input.rawAssistantOutput);
|
||||
const jsonLikeOutput = input.rawAssistantOutput.trim().startsWith('{')
|
||||
|| input.rawAssistantOutput.trim().startsWith('[');
|
||||
const baseEnvelope: ProtocolResponseEnvelope = {
|
||||
protocolVersion: '2.0',
|
||||
assistantText: parsedUi.assistantText,
|
||||
ui: parsedUi.ui || undefined,
|
||||
intent: jsonLikeOutput
|
||||
? 'summarize'
|
||||
: this.deriveIntent(parsedUi.assistantText, Boolean(parsedUi.ui), false),
|
||||
needsInput: {
|
||||
required: false,
|
||||
fields: [],
|
||||
},
|
||||
actions: [],
|
||||
confidence: 0.7,
|
||||
traceId: randomUUID(),
|
||||
};
|
||||
|
||||
const normalizedBaseEnvelope = this.applyActionPolicies(baseEnvelope);
|
||||
const { filteredEnvelope, warnings: capabilityWarnings } = this.applyCapabilityGuards(normalizedBaseEnvelope, input.capabilities);
|
||||
warnings.push(...capabilityWarnings);
|
||||
|
||||
const validated = validateProtocolResponseEnvelope(filteredEnvelope);
|
||||
if (validated.ok && validated.value) {
|
||||
return {
|
||||
envelope: validated.value,
|
||||
traceId: validated.value.traceId,
|
||||
repairAttempted: false,
|
||||
warnings,
|
||||
};
|
||||
}
|
||||
|
||||
const fallback = this.fallbackEnvelope(input.rawAssistantOutput);
|
||||
return {
|
||||
envelope: fallback,
|
||||
traceId: fallback.traceId,
|
||||
repairAttempted: true,
|
||||
warnings,
|
||||
validationError: validated.error,
|
||||
};
|
||||
}
|
||||
|
||||
private parseCanonicalEnvelope(raw: string): ProtocolResponseEnvelope | null {
|
||||
try {
|
||||
const parsed = JSON.parse(raw);
|
||||
const validated = validateProtocolResponseEnvelope(parsed);
|
||||
return validated.ok && validated.value ? validated.value : null;
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private repairRawEnvelope(raw: string): ProtocolResponseEnvelope | null {
|
||||
try {
|
||||
const parsed = JSON.parse(raw) as Record<string, unknown>;
|
||||
const looksLikeEnvelope = Boolean(
|
||||
parsed.assistantText
|
||||
|| parsed.assistant_text
|
||||
|| parsed.intent
|
||||
|| parsed.needsInput
|
||||
|| parsed.needs_input
|
||||
|| parsed.actions,
|
||||
);
|
||||
|
||||
if (!looksLikeEnvelope) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const repaired: Record<string, unknown> = {
|
||||
protocolVersion: parsed.protocolVersion ?? parsed.protocol_version ?? '2.0',
|
||||
assistantText: parsed.assistantText ?? parsed.assistant_text ?? '',
|
||||
ui: parsed.ui,
|
||||
intent: parsed.intent ?? 'summarize',
|
||||
needsInput: parsed.needsInput ?? parsed.needs_input ?? { required: false, fields: [] },
|
||||
actions: parsed.actions ?? [],
|
||||
confidence: typeof parsed.confidence === 'number' ? parsed.confidence : 0.6,
|
||||
traceId: parsed.traceId ?? parsed.trace_id ?? randomUUID(),
|
||||
};
|
||||
|
||||
const validated = validateProtocolResponseEnvelope(repaired);
|
||||
return validated.ok && validated.value ? validated.value : null;
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private deriveIntent(text: string, hasUi: boolean, needsInput: boolean): ProtocolIntent {
|
||||
if (needsInput) {
|
||||
return 'ask_input';
|
||||
}
|
||||
|
||||
if (hasUi) {
|
||||
return 'propose_action';
|
||||
}
|
||||
|
||||
if (text.trim().length === 0) {
|
||||
return 'summarize';
|
||||
}
|
||||
|
||||
return 'analyze';
|
||||
}
|
||||
|
||||
private applyCapabilityGuards(
|
||||
envelope: ProtocolResponseEnvelope,
|
||||
capabilities: ProtocolCapabilitySnapshot,
|
||||
): { filteredEnvelope: ProtocolResponseEnvelope; warnings: string[] } {
|
||||
const warnings: string[] = [];
|
||||
|
||||
const filteredActions = envelope.actions.filter((action) => {
|
||||
const supported = capabilities.actions.includes(action.action);
|
||||
if (!supported) {
|
||||
warnings.push(`Blocked unsupported action: ${action.action}`);
|
||||
}
|
||||
return supported;
|
||||
});
|
||||
|
||||
const filteredUiElements = envelope.ui?.elements.filter((element) => {
|
||||
const typedElement = element as { type?: string };
|
||||
const elementType = typedElement?.type;
|
||||
if (!elementType) {
|
||||
return true;
|
||||
}
|
||||
|
||||
const supported = capabilities.widgets.includes(elementType);
|
||||
if (!supported) {
|
||||
warnings.push(`Blocked unsupported widget: ${elementType}`);
|
||||
}
|
||||
return supported;
|
||||
});
|
||||
|
||||
return {
|
||||
filteredEnvelope: {
|
||||
...envelope,
|
||||
ui: envelope.ui && filteredUiElements
|
||||
? {
|
||||
specVersion: '1',
|
||||
elements: filteredUiElements,
|
||||
}
|
||||
: envelope.ui,
|
||||
actions: filteredActions,
|
||||
},
|
||||
warnings,
|
||||
};
|
||||
}
|
||||
|
||||
private applyActionPolicies(envelope: ProtocolResponseEnvelope): ProtocolResponseEnvelope {
|
||||
const actionList = envelope.actions.length > 0
|
||||
? envelope.actions
|
||||
: this.extractActionsFromUi(envelope.ui?.elements ?? []);
|
||||
|
||||
const normalizedActions = actionList.map((action, index) => {
|
||||
const policy = resolveActionPolicy(action.action);
|
||||
return {
|
||||
id: action.id || `agui-action-${index + 1}`,
|
||||
action: action.action,
|
||||
label: action.label,
|
||||
payload: action.payload,
|
||||
policy: policy.level,
|
||||
requiresConfirmation: policy.requiresConfirmation,
|
||||
};
|
||||
});
|
||||
|
||||
return {
|
||||
...envelope,
|
||||
actions: normalizedActions,
|
||||
};
|
||||
}
|
||||
|
||||
private extractActionsFromUi(elements: unknown[]): Array<{
|
||||
id: string;
|
||||
action: string;
|
||||
label?: string;
|
||||
payload?: Record<string, unknown>;
|
||||
}> {
|
||||
const extracted: Array<{
|
||||
id: string;
|
||||
action: string;
|
||||
label?: string;
|
||||
payload?: Record<string, unknown>;
|
||||
}> = [];
|
||||
|
||||
const walk = (nodes: unknown[], parentId: string) => {
|
||||
nodes.forEach((node, index) => {
|
||||
const typedNode = node as Record<string, unknown>;
|
||||
const type = typeof typedNode?.type === 'string' ? typedNode.type : '';
|
||||
const nodeId = `${parentId}-${index + 1}`;
|
||||
|
||||
if ((type === 'action' || type === 'input' || type === 'datePicker' || type === 'form' || type === 'image') && typeof typedNode.action === 'string') {
|
||||
extracted.push({
|
||||
id: `ui-${nodeId}`,
|
||||
action: typedNode.action,
|
||||
label: typeof typedNode.label === 'string' ? typedNode.label : undefined,
|
||||
payload: typedNode.payload as Record<string, unknown> | undefined,
|
||||
});
|
||||
}
|
||||
|
||||
if (type === 'card' && Array.isArray(typedNode.actions)) {
|
||||
typedNode.actions.forEach((cardAction, cardActionIndex) => {
|
||||
const typedCardAction = cardAction as Record<string, unknown>;
|
||||
if (typeof typedCardAction.action === 'string') {
|
||||
extracted.push({
|
||||
id: `ui-${nodeId}-card-${cardActionIndex + 1}`,
|
||||
action: typedCardAction.action,
|
||||
label: typeof typedCardAction.label === 'string' ? typedCardAction.label : undefined,
|
||||
payload: typedCardAction.payload as Record<string, unknown> | undefined,
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
if (type === 'tabs' && Array.isArray(typedNode.tabs)) {
|
||||
typedNode.tabs.forEach((tabNode, tabIndex) => {
|
||||
const typedTab = tabNode as Record<string, unknown>;
|
||||
if (Array.isArray(typedTab.elements)) {
|
||||
walk(typedTab.elements, `${nodeId}-tab-${tabIndex + 1}`);
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
walk(elements, 'root');
|
||||
return extracted;
|
||||
}
|
||||
|
||||
private fallbackEnvelope(rawAssistantOutput: string): ProtocolResponseEnvelope {
|
||||
return {
|
||||
protocolVersion: '2.0',
|
||||
assistantText: rawAssistantOutput,
|
||||
intent: 'summarize',
|
||||
needsInput: {
|
||||
required: false,
|
||||
fields: [],
|
||||
},
|
||||
actions: [],
|
||||
confidence: 0.3,
|
||||
traceId: randomUUID(),
|
||||
};
|
||||
}
|
||||
}
|
||||
82
src/main/agentic/protocol/types.ts
Normal file
82
src/main/agentic/protocol/types.ts
Normal file
@@ -0,0 +1,82 @@
|
||||
export type AgentSurface = 'tab' | 'sidebar';
|
||||
|
||||
export type ProtocolIntent =
|
||||
| 'analyze'
|
||||
| 'ask_input'
|
||||
| 'propose_action'
|
||||
| 'execute_action'
|
||||
| 'summarize';
|
||||
|
||||
export type ActionPolicyLevel = 'silent' | 'confirm' | 'danger';
|
||||
|
||||
export interface ProtocolNeedsInputField {
|
||||
key: string;
|
||||
label: string;
|
||||
inputType: 'text' | 'textarea' | 'select' | 'checkbox' | 'date' | 'number';
|
||||
required?: boolean;
|
||||
options?: Array<{ label: string; value: string }>;
|
||||
placeholder?: string;
|
||||
defaultValue?: string | number | boolean;
|
||||
}
|
||||
|
||||
export interface ProtocolNeedsInput {
|
||||
required: boolean;
|
||||
fields: ProtocolNeedsInputField[];
|
||||
}
|
||||
|
||||
export interface ProtocolAction {
|
||||
id: string;
|
||||
action: string;
|
||||
label?: string;
|
||||
payload?: Record<string, unknown>;
|
||||
policy: ActionPolicyLevel;
|
||||
requiresConfirmation: boolean;
|
||||
}
|
||||
|
||||
export interface ProtocolUiSpec {
|
||||
specVersion: '1';
|
||||
elements: unknown[];
|
||||
}
|
||||
|
||||
export interface ProtocolResponseEnvelope {
|
||||
protocolVersion: '2.0';
|
||||
assistantText: string;
|
||||
ui?: ProtocolUiSpec;
|
||||
intent: ProtocolIntent;
|
||||
needsInput: ProtocolNeedsInput;
|
||||
actions: ProtocolAction[];
|
||||
confidence: number;
|
||||
traceId: string;
|
||||
}
|
||||
|
||||
export interface ProtocolRequestMessage {
|
||||
role: 'user' | 'assistant' | 'system' | 'tool';
|
||||
content: string;
|
||||
}
|
||||
|
||||
export interface ProtocolCapabilitySnapshot {
|
||||
widgets: string[];
|
||||
actions: string[];
|
||||
tools: string[];
|
||||
disabled?: string[];
|
||||
}
|
||||
|
||||
export interface ProtocolRequestEnvelope {
|
||||
protocolVersion: '2.0';
|
||||
surface: AgentSurface;
|
||||
messages: ProtocolRequestMessage[];
|
||||
context: Record<string, unknown>;
|
||||
capabilities: ProtocolCapabilitySnapshot;
|
||||
}
|
||||
|
||||
export interface ProtocolValidationError {
|
||||
code: 'AGUI_PROTOCOL_VALIDATION_ERROR';
|
||||
message: string;
|
||||
details?: string[];
|
||||
}
|
||||
|
||||
export interface ProtocolValidationResult<T> {
|
||||
ok: boolean;
|
||||
value?: T;
|
||||
error?: ProtocolValidationError;
|
||||
}
|
||||
154
src/main/agentic/protocol/uiSchema.ts
Normal file
154
src/main/agentic/protocol/uiSchema.ts
Normal file
@@ -0,0 +1,154 @@
|
||||
import { z } from 'zod';
|
||||
|
||||
const inputTypeSchema = z.enum(['text', 'textarea', 'select', 'checkbox', 'date', 'number']);
|
||||
|
||||
const inputOptionSchema = z.object({
|
||||
label: z.string().min(1),
|
||||
value: z.string(),
|
||||
}).strict();
|
||||
|
||||
const textElementSchema = z.object({
|
||||
type: z.literal('text'),
|
||||
text: z.string().min(1),
|
||||
}).strict();
|
||||
|
||||
const metricElementSchema = z.object({
|
||||
type: z.literal('metric'),
|
||||
label: z.string().min(1),
|
||||
value: z.string().min(1),
|
||||
}).strict();
|
||||
|
||||
const listElementSchema = z.object({
|
||||
type: z.literal('list'),
|
||||
title: z.string().optional(),
|
||||
items: z.array(z.string().min(1)).min(1),
|
||||
}).strict();
|
||||
|
||||
const tableElementSchema = z.object({
|
||||
type: z.literal('table'),
|
||||
columns: z.array(z.string().min(1)).min(1),
|
||||
rows: z.array(z.array(z.string())).min(1),
|
||||
}).strict();
|
||||
|
||||
const actionElementSchema = z.object({
|
||||
type: z.literal('action'),
|
||||
label: z.string().min(1),
|
||||
action: z.string().min(1),
|
||||
payload: z.record(z.string(), z.unknown()).optional(),
|
||||
}).strict();
|
||||
|
||||
const chartElementSchema = z.object({
|
||||
type: z.literal('chart'),
|
||||
chartType: z.enum(['bar', 'line', 'pie']),
|
||||
title: z.string().min(1).optional(),
|
||||
series: z.array(z.object({
|
||||
label: z.string().min(1),
|
||||
value: z.number(),
|
||||
}).strict()).min(1),
|
||||
}).strict();
|
||||
|
||||
const inputElementSchema = z.object({
|
||||
type: z.literal('input'),
|
||||
key: z.string().min(1),
|
||||
label: z.string().min(1),
|
||||
inputType: inputTypeSchema,
|
||||
placeholder: z.string().optional(),
|
||||
defaultValue: z.union([z.string(), z.number(), z.boolean()]).optional(),
|
||||
options: z.array(inputOptionSchema).optional(),
|
||||
action: z.string().min(1).optional(),
|
||||
submitLabel: z.string().min(1).optional(),
|
||||
payload: z.record(z.string(), z.unknown()).optional(),
|
||||
}).strict();
|
||||
|
||||
const datePickerElementSchema = z.object({
|
||||
type: z.literal('datePicker'),
|
||||
key: z.string().min(1),
|
||||
label: z.string().min(1),
|
||||
defaultValue: z.string().optional(),
|
||||
min: z.string().optional(),
|
||||
max: z.string().optional(),
|
||||
action: z.string().min(1).optional(),
|
||||
submitLabel: z.string().min(1).optional(),
|
||||
payload: z.record(z.string(), z.unknown()).optional(),
|
||||
}).strict();
|
||||
|
||||
const formFieldSchema = z.object({
|
||||
key: z.string().min(1),
|
||||
label: z.string().min(1),
|
||||
inputType: inputTypeSchema,
|
||||
placeholder: z.string().optional(),
|
||||
defaultValue: z.union([z.string(), z.number(), z.boolean()]).optional(),
|
||||
options: z.array(inputOptionSchema).optional(),
|
||||
required: z.boolean().optional(),
|
||||
}).strict();
|
||||
|
||||
const formElementSchema = z.object({
|
||||
type: z.literal('form'),
|
||||
formId: z.string().min(1),
|
||||
title: z.string().optional(),
|
||||
submitLabel: z.string().min(1),
|
||||
action: z.string().min(1),
|
||||
payload: z.record(z.string(), z.unknown()).optional(),
|
||||
fields: z.array(formFieldSchema).min(1),
|
||||
}).strict();
|
||||
|
||||
const cardActionSchema = z.object({
|
||||
label: z.string().min(1),
|
||||
action: z.string().min(1),
|
||||
payload: z.record(z.string(), z.unknown()).optional(),
|
||||
}).strict();
|
||||
|
||||
const cardElementSchema = z.object({
|
||||
type: z.literal('card'),
|
||||
title: z.string().min(1),
|
||||
body: z.string().min(1),
|
||||
subtitle: z.string().optional(),
|
||||
actions: z.array(cardActionSchema).optional(),
|
||||
}).strict();
|
||||
|
||||
const imageElementSchema = z.object({
|
||||
type: z.literal('image'),
|
||||
src: z.string().min(1),
|
||||
alt: z.string().optional(),
|
||||
caption: z.string().optional(),
|
||||
action: z.string().min(1).optional(),
|
||||
payload: z.record(z.string(), z.unknown()).optional(),
|
||||
}).strict();
|
||||
|
||||
let assistantPanelElementSchemaRef: z.ZodTypeAny;
|
||||
|
||||
const tabsElementSchema: z.ZodTypeAny = z.lazy(() => z.object({
|
||||
type: z.literal('tabs'),
|
||||
widgetId: z.string().min(1).optional(),
|
||||
defaultTabId: z.string().min(1).optional(),
|
||||
tabs: z.array(z.object({
|
||||
id: z.string().min(1),
|
||||
label: z.string().min(1),
|
||||
elements: z.array(assistantPanelElementSchemaRef).min(1),
|
||||
}).strict()).min(1),
|
||||
}).strict());
|
||||
|
||||
assistantPanelElementSchemaRef = z.union([
|
||||
textElementSchema,
|
||||
metricElementSchema,
|
||||
listElementSchema,
|
||||
tableElementSchema,
|
||||
actionElementSchema,
|
||||
chartElementSchema,
|
||||
inputElementSchema,
|
||||
formElementSchema,
|
||||
datePickerElementSchema,
|
||||
cardElementSchema,
|
||||
imageElementSchema,
|
||||
tabsElementSchema,
|
||||
]);
|
||||
|
||||
export const assistantPanelElementSchema = assistantPanelElementSchemaRef;
|
||||
|
||||
export const assistantPanelSpecSchema = z.object({
|
||||
specVersion: z.literal('1'),
|
||||
elements: z.array(assistantPanelElementSchema).min(1),
|
||||
}).strict();
|
||||
|
||||
export type AssistantPanelElement = z.infer<typeof assistantPanelElementSchema>;
|
||||
export type AssistantPanelSpec = z.infer<typeof assistantPanelSpecSchema>;
|
||||
235
src/main/agentic/protocol/uiSpecParser.ts
Normal file
235
src/main/agentic/protocol/uiSpecParser.ts
Normal file
@@ -0,0 +1,235 @@
|
||||
import { assistantPanelSpecSchema, type AssistantPanelSpec } from './uiSchema';
|
||||
|
||||
function toRecord(value: unknown): Record<string, unknown> | null {
|
||||
if (!value || typeof value !== 'object' || Array.isArray(value)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return value as Record<string, unknown>;
|
||||
}
|
||||
|
||||
function normalizeChartElement(record: Record<string, unknown>): Record<string, unknown> {
|
||||
const normalized: Record<string, unknown> = {
|
||||
...record,
|
||||
};
|
||||
|
||||
const dataRecord = toRecord(record.data);
|
||||
if (Array.isArray(record.series)) {
|
||||
return normalized;
|
||||
}
|
||||
|
||||
if (!dataRecord) {
|
||||
return normalized;
|
||||
}
|
||||
|
||||
const labels = Array.isArray(dataRecord.labels) ? dataRecord.labels : [];
|
||||
const datasets = Array.isArray(dataRecord.datasets) ? dataRecord.datasets : [];
|
||||
const firstDataset = toRecord(datasets[0]);
|
||||
const values = Array.isArray(firstDataset?.data) ? firstDataset?.data : [];
|
||||
|
||||
if (labels.length === 0 || values.length === 0) {
|
||||
return normalized;
|
||||
}
|
||||
|
||||
const series = labels
|
||||
.map((label, index) => ({
|
||||
label: String(label),
|
||||
value: Number(values[index]),
|
||||
}))
|
||||
.filter((entry) => Number.isFinite(entry.value));
|
||||
|
||||
if (series.length === 0) {
|
||||
return normalized;
|
||||
}
|
||||
|
||||
normalized.series = series;
|
||||
delete normalized.data;
|
||||
return normalized;
|
||||
}
|
||||
|
||||
function normalizeTabContent(tabValue: unknown): Record<string, unknown>[] {
|
||||
if (Array.isArray(tabValue)) {
|
||||
return tabValue
|
||||
.map((entry) => normalizeElement(entry))
|
||||
.filter((entry): entry is Record<string, unknown> => Boolean(entry));
|
||||
}
|
||||
|
||||
const normalized = normalizeElement(tabValue);
|
||||
return normalized ? [normalized] : [];
|
||||
}
|
||||
|
||||
function normalizeTabsElement(record: Record<string, unknown>): Record<string, unknown> | null {
|
||||
const tabs = Array.isArray(record.tabs) ? record.tabs : [];
|
||||
const normalizedTabs = tabs
|
||||
.map((tabValue, tabIndex) => {
|
||||
const tabRecord = toRecord(tabValue);
|
||||
if (!tabRecord) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const id = typeof tabRecord.id === 'string' && tabRecord.id.trim().length > 0
|
||||
? tabRecord.id
|
||||
: `tab-${tabIndex + 1}`;
|
||||
|
||||
const label = typeof tabRecord.label === 'string' && tabRecord.label.trim().length > 0
|
||||
? tabRecord.label
|
||||
: typeof tabRecord.title === 'string' && tabRecord.title.trim().length > 0
|
||||
? tabRecord.title
|
||||
: id;
|
||||
|
||||
const elements = Array.isArray(tabRecord.elements)
|
||||
? normalizeTabContent(tabRecord.elements)
|
||||
: normalizeTabContent(tabRecord.content);
|
||||
|
||||
if (elements.length === 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return { id, label, elements };
|
||||
})
|
||||
.filter((entry): entry is { id: string; label: string; elements: Record<string, unknown>[] } => Boolean(entry));
|
||||
|
||||
if (normalizedTabs.length === 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return {
|
||||
...record,
|
||||
tabs: normalizedTabs,
|
||||
};
|
||||
}
|
||||
|
||||
function normalizeElement(value: unknown): Record<string, unknown> | null {
|
||||
const record = toRecord(value);
|
||||
if (!record) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const type = typeof record.type === 'string' ? record.type : '';
|
||||
if (type === 'markdown') {
|
||||
const textValue = typeof record.content === 'string'
|
||||
? record.content
|
||||
: typeof record.text === 'string'
|
||||
? record.text
|
||||
: '';
|
||||
|
||||
if (!textValue.trim()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return {
|
||||
type: 'text',
|
||||
text: textValue,
|
||||
};
|
||||
}
|
||||
|
||||
if (type === 'chart') {
|
||||
return normalizeChartElement(record);
|
||||
}
|
||||
|
||||
if (type === 'tabs') {
|
||||
return normalizeTabsElement(record);
|
||||
}
|
||||
|
||||
return record;
|
||||
}
|
||||
|
||||
function normalizeCandidate(parsed: unknown): AssistantPanelSpec | null {
|
||||
const canonicalResult = assistantPanelSpecSchema.safeParse(parsed);
|
||||
if (canonicalResult.success) {
|
||||
return canonicalResult.data;
|
||||
}
|
||||
|
||||
const record = toRecord(parsed);
|
||||
if (!record) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (record.type === 'tab' && record.content) {
|
||||
return normalizeCandidate(record.content);
|
||||
}
|
||||
|
||||
if (record.type === 'tabs') {
|
||||
const tabsElement = normalizeTabsElement(record);
|
||||
if (!tabsElement) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const asSpec = {
|
||||
specVersion: '1' as const,
|
||||
elements: [tabsElement],
|
||||
};
|
||||
const normalizedResult = assistantPanelSpecSchema.safeParse(asSpec);
|
||||
return normalizedResult.success ? normalizedResult.data : null;
|
||||
}
|
||||
|
||||
if (Array.isArray(record.elements)) {
|
||||
const normalizedElements = record.elements
|
||||
.map((element) => normalizeElement(element))
|
||||
.filter((element): element is Record<string, unknown> => Boolean(element));
|
||||
|
||||
if (normalizedElements.length === 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const asSpec = {
|
||||
specVersion: '1' as const,
|
||||
elements: normalizedElements,
|
||||
};
|
||||
const normalizedResult = assistantPanelSpecSchema.safeParse(asSpec);
|
||||
return normalizedResult.success ? normalizedResult.data : null;
|
||||
}
|
||||
|
||||
const normalizedElement = normalizeElement(record);
|
||||
if (!normalizedElement) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const asSpec = {
|
||||
specVersion: '1' as const,
|
||||
elements: [normalizedElement],
|
||||
};
|
||||
const normalizedResult = assistantPanelSpecSchema.safeParse(asSpec);
|
||||
return normalizedResult.success ? normalizedResult.data : null;
|
||||
}
|
||||
|
||||
function parseSpecCandidate(raw: string): AssistantPanelSpec | null {
|
||||
try {
|
||||
const parsed = JSON.parse(raw);
|
||||
return normalizeCandidate(parsed);
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
export interface ParsedAssistantUiResult {
|
||||
assistantText: string;
|
||||
ui: AssistantPanelSpec | null;
|
||||
}
|
||||
|
||||
export function extractAssistantUiSpec(message: string): ParsedAssistantUiResult {
|
||||
const trimmed = message.trim();
|
||||
|
||||
const fencedMatches = [...trimmed.matchAll(/```(json)?\s*([\s\S]*?)```/gi)];
|
||||
for (const match of fencedMatches) {
|
||||
const candidate = match[2]?.trim();
|
||||
if (!candidate) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const parsed = parseSpecCandidate(candidate);
|
||||
if (parsed) {
|
||||
const assistantText = trimmed.replace(match[0], '').trim();
|
||||
return {
|
||||
assistantText,
|
||||
ui: parsed,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
const parsedWholeMessage = parseSpecCandidate(trimmed);
|
||||
return {
|
||||
assistantText: parsedWholeMessage ? '' : trimmed,
|
||||
ui: parsedWholeMessage,
|
||||
};
|
||||
}
|
||||
112
src/main/agentic/protocol/validator.ts
Normal file
112
src/main/agentic/protocol/validator.ts
Normal file
@@ -0,0 +1,112 @@
|
||||
import { z } from 'zod';
|
||||
import type {
|
||||
ProtocolRequestEnvelope,
|
||||
ProtocolResponseEnvelope,
|
||||
ProtocolValidationResult,
|
||||
} from './types';
|
||||
import { createProtocolValidationError } from './errors';
|
||||
|
||||
const needsInputFieldSchema = z.object({
|
||||
key: z.string().min(1),
|
||||
label: z.string().min(1),
|
||||
inputType: z.enum(['text', 'textarea', 'select', 'checkbox', 'date', 'number']),
|
||||
required: z.boolean().optional(),
|
||||
options: z.array(z.object({ label: z.string().min(1), value: z.string() })).optional(),
|
||||
placeholder: z.string().optional(),
|
||||
defaultValue: z.union([z.string(), z.number(), z.boolean()]).optional(),
|
||||
}).strict();
|
||||
|
||||
const needsInputSchema = z.object({
|
||||
required: z.boolean(),
|
||||
fields: z.array(needsInputFieldSchema),
|
||||
}).strict();
|
||||
|
||||
const protocolActionSchema = z.object({
|
||||
id: z.string().min(1),
|
||||
action: z.string().min(1),
|
||||
label: z.string().optional(),
|
||||
payload: z.record(z.string(), z.unknown()).optional(),
|
||||
policy: z.enum(['silent', 'confirm', 'danger']),
|
||||
requiresConfirmation: z.boolean(),
|
||||
}).strict();
|
||||
|
||||
const protocolUiSchema = z.object({
|
||||
specVersion: z.literal('1'),
|
||||
elements: z.array(z.unknown()),
|
||||
}).strict();
|
||||
|
||||
const protocolResponseEnvelopeSchema = z.object({
|
||||
protocolVersion: z.literal('2.0'),
|
||||
assistantText: z.string(),
|
||||
ui: protocolUiSchema.optional(),
|
||||
intent: z.enum(['analyze', 'ask_input', 'propose_action', 'execute_action', 'summarize']),
|
||||
needsInput: needsInputSchema,
|
||||
actions: z.array(protocolActionSchema),
|
||||
confidence: z.number().min(0).max(1),
|
||||
traceId: z.string().min(1),
|
||||
}).strict().superRefine((value, context) => {
|
||||
if (value.needsInput.required && value.needsInput.fields.length === 0) {
|
||||
context.addIssue({
|
||||
code: z.ZodIssueCode.custom,
|
||||
path: ['needsInput', 'fields'],
|
||||
message: 'needsInput.fields must include at least one field when needsInput.required is true',
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
const protocolRequestEnvelopeSchema = z.object({
|
||||
protocolVersion: z.literal('2.0'),
|
||||
surface: z.enum(['tab', 'sidebar']),
|
||||
messages: z.array(z.object({ role: z.enum(['user', 'assistant', 'system', 'tool']), content: z.string() }).strict()),
|
||||
context: z.record(z.string(), z.unknown()),
|
||||
capabilities: z.object({
|
||||
widgets: z.array(z.string().min(1)),
|
||||
actions: z.array(z.string().min(1)),
|
||||
tools: z.array(z.string().min(1)),
|
||||
disabled: z.array(z.string().min(1)).optional(),
|
||||
}).strict(),
|
||||
}).strict();
|
||||
|
||||
function toErrorMessage(prefix: string, issues: z.ZodIssue[]): string {
|
||||
const firstIssue = issues[0];
|
||||
const issuePath = firstIssue.path.length > 0 ? firstIssue.path.join('.') : 'root';
|
||||
return `${prefix}: ${issuePath} ${firstIssue.message}`;
|
||||
}
|
||||
|
||||
export function validateProtocolResponseEnvelope(input: unknown): ProtocolValidationResult<ProtocolResponseEnvelope> {
|
||||
const parsed = protocolResponseEnvelopeSchema.safeParse(input);
|
||||
if (!parsed.success) {
|
||||
return {
|
||||
ok: false,
|
||||
error: createProtocolValidationError(
|
||||
toErrorMessage('Invalid protocol response envelope', parsed.error.issues),
|
||||
parsed.error.issues.map((issue) => `${issue.path.join('.')}: ${issue.message}`),
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
ok: true,
|
||||
value: parsed.data,
|
||||
};
|
||||
}
|
||||
|
||||
export function validateProtocolRequestEnvelope(input: unknown): ProtocolValidationResult<ProtocolRequestEnvelope> {
|
||||
const parsed = protocolRequestEnvelopeSchema.safeParse(input);
|
||||
if (!parsed.success) {
|
||||
return {
|
||||
ok: false,
|
||||
error: createProtocolValidationError(
|
||||
toErrorMessage('Invalid protocol request envelope', parsed.error.issues),
|
||||
parsed.error.issues.map((issue) => `${issue.path.join('.')}: ${issue.message}`),
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
ok: true,
|
||||
value: parsed.data,
|
||||
};
|
||||
}
|
||||
|
||||
export type { ProtocolRequestEnvelope, ProtocolResponseEnvelope } from './types';
|
||||
50
src/main/agentic/workflow/checkpointStore.ts
Normal file
50
src/main/agentic/workflow/checkpointStore.ts
Normal file
@@ -0,0 +1,50 @@
|
||||
import type { AgentTurnState } from './turnStateMachine';
|
||||
|
||||
export interface WorkflowCheckpoint {
|
||||
conversationId: string;
|
||||
state: AgentTurnState;
|
||||
pendingFields: string[];
|
||||
lastTraceId: string;
|
||||
updatedAt: string;
|
||||
}
|
||||
|
||||
export interface WorkflowCheckpointSettingsAdapter {
|
||||
getSetting(key: string): Promise<string | null>;
|
||||
setSetting(key: string, value: string): Promise<void>;
|
||||
}
|
||||
|
||||
function keyForConversation(conversationId: string): string {
|
||||
return `agui.workflow.${conversationId}`;
|
||||
}
|
||||
|
||||
export class WorkflowCheckpointStore {
|
||||
private readonly adapter: WorkflowCheckpointSettingsAdapter;
|
||||
|
||||
constructor(adapter: WorkflowCheckpointSettingsAdapter) {
|
||||
this.adapter = adapter;
|
||||
}
|
||||
|
||||
async save(checkpoint: WorkflowCheckpoint): Promise<void> {
|
||||
await this.adapter.setSetting(
|
||||
keyForConversation(checkpoint.conversationId),
|
||||
JSON.stringify(checkpoint),
|
||||
);
|
||||
}
|
||||
|
||||
async load(conversationId: string): Promise<WorkflowCheckpoint | null> {
|
||||
const raw = await this.adapter.getSetting(keyForConversation(conversationId));
|
||||
if (!raw) {
|
||||
return null;
|
||||
}
|
||||
|
||||
try {
|
||||
const parsed = JSON.parse(raw) as WorkflowCheckpoint;
|
||||
if (!parsed || parsed.conversationId !== conversationId) {
|
||||
return null;
|
||||
}
|
||||
return parsed;
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
45
src/main/agentic/workflow/turnStateMachine.ts
Normal file
45
src/main/agentic/workflow/turnStateMachine.ts
Normal file
@@ -0,0 +1,45 @@
|
||||
export type AgentTurnState =
|
||||
| 'planning'
|
||||
| 'awaiting_input'
|
||||
| 'executing'
|
||||
| 'observing'
|
||||
| 'completed';
|
||||
|
||||
interface TurnStateEnvelopeInput {
|
||||
intent: 'analyze' | 'ask_input' | 'propose_action' | 'execute_action' | 'summarize';
|
||||
needsInput: {
|
||||
required: boolean;
|
||||
fields: Array<{ key: string }>;
|
||||
};
|
||||
}
|
||||
|
||||
interface TransitionInput {
|
||||
previousState: AgentTurnState;
|
||||
envelope: TurnStateEnvelopeInput;
|
||||
}
|
||||
|
||||
export class AgentTurnStateMachine {
|
||||
transition(input: TransitionInput): AgentTurnState {
|
||||
if (input.envelope.needsInput.required && input.envelope.needsInput.fields.length > 0) {
|
||||
return 'awaiting_input';
|
||||
}
|
||||
|
||||
if (input.envelope.intent === 'execute_action') {
|
||||
return 'executing';
|
||||
}
|
||||
|
||||
if (input.envelope.intent === 'propose_action') {
|
||||
return 'observing';
|
||||
}
|
||||
|
||||
if (input.envelope.intent === 'summarize') {
|
||||
return 'completed';
|
||||
}
|
||||
|
||||
if (input.previousState === 'awaiting_input') {
|
||||
return 'executing';
|
||||
}
|
||||
|
||||
return 'planning';
|
||||
}
|
||||
}
|
||||
@@ -16,6 +16,13 @@ import { ChatEngine } from './ChatEngine';
|
||||
import { PostEngine } from './PostEngine';
|
||||
import { MediaEngine } from './MediaEngine';
|
||||
import { getPostMediaEngine } from './PostMediaEngine';
|
||||
import { ProtocolResponseBuilder } from '../agentic/protocol/responseBuilder';
|
||||
import { CapabilityRegistryService } from '../agentic/capabilities/registry';
|
||||
import { validateProtocolRequestEnvelope } from '../agentic/protocol/validator';
|
||||
import type { ProtocolResponseEnvelope } from '../agentic/protocol/types';
|
||||
import { AgentTurnStateMachine, type AgentTurnState } from '../agentic/workflow/turnStateMachine';
|
||||
import { WorkflowCheckpointStore } from '../agentic/workflow/checkpointStore';
|
||||
import { getProtocolTelemetryService } from '../agentic/observability/protocolTelemetry';
|
||||
|
||||
// OpenCode Zen API endpoints
|
||||
const ZEN_ANTHROPIC_URL = 'https://opencode.ai/zen/v1/messages';
|
||||
@@ -77,6 +84,10 @@ export interface SendMessageOptions {
|
||||
export interface SendMessageResult {
|
||||
success: boolean;
|
||||
message?: string;
|
||||
envelope?: ProtocolResponseEnvelope;
|
||||
protocolVersion?: '2.0';
|
||||
traceId?: string;
|
||||
warnings?: string[];
|
||||
error?: string;
|
||||
toolCalls?: Array<{ name: string; args: unknown }>;
|
||||
}
|
||||
@@ -131,6 +142,10 @@ export class OpenCodeManager {
|
||||
private postEngine: PostEngine;
|
||||
private mediaEngine: MediaEngine;
|
||||
private getMainWindow: () => BrowserWindow | null;
|
||||
private protocolResponseBuilder: ProtocolResponseBuilder;
|
||||
private capabilityRegistry: CapabilityRegistryService;
|
||||
private turnStateMachine: AgentTurnStateMachine;
|
||||
private workflowCheckpointStore: WorkflowCheckpointStore;
|
||||
private apiKey: string = '';
|
||||
private abortControllers: Map<string, AbortController> = new Map();
|
||||
|
||||
@@ -144,6 +159,13 @@ export class OpenCodeManager {
|
||||
this.postEngine = postEngine;
|
||||
this.mediaEngine = mediaEngine;
|
||||
this.getMainWindow = getMainWindow;
|
||||
this.protocolResponseBuilder = new ProtocolResponseBuilder();
|
||||
this.capabilityRegistry = new CapabilityRegistryService();
|
||||
this.turnStateMachine = new AgentTurnStateMachine();
|
||||
this.workflowCheckpointStore = new WorkflowCheckpointStore({
|
||||
getSetting: async (key: string) => this.chatEngine.getSetting(key),
|
||||
setSetting: async (key: string, value: string) => this.chatEngine.setSetting(key, value),
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -275,10 +297,37 @@ export class OpenCodeManager {
|
||||
|
||||
// Build message history from DB (excluding system messages)
|
||||
const dbMessages = conversation.messages.filter(m => m.role !== 'system');
|
||||
const surface = metadata?.surface || 'tab';
|
||||
const capabilities = this.capabilityRegistry.getSnapshot({ surface });
|
||||
const requestEnvelope = {
|
||||
protocolVersion: '2.0' as const,
|
||||
surface,
|
||||
messages: dbMessages
|
||||
.filter((message) => message.role === 'user' || message.role === 'assistant' || message.role === 'system' || message.role === 'tool')
|
||||
.map((message) => ({
|
||||
role: message.role,
|
||||
content: message.content || '',
|
||||
})),
|
||||
context: {
|
||||
conversationId,
|
||||
modelId,
|
||||
},
|
||||
capabilities,
|
||||
};
|
||||
|
||||
const requestValidation = validateProtocolRequestEnvelope(requestEnvelope);
|
||||
if (!requestValidation.ok) {
|
||||
return {
|
||||
success: false,
|
||||
error: requestValidation.error?.message || 'Invalid protocol request envelope',
|
||||
};
|
||||
}
|
||||
|
||||
const surfaceHint = metadata?.surface
|
||||
? `\n\n[Client UI surface: ${metadata.surface}. Render response UI for this surface while keeping content functionally equivalent.]`
|
||||
: '';
|
||||
const userMessageForModel = `${userMessage}${surfaceHint}`;
|
||||
const capabilityHint = `\n\n[Protocol request envelope]\n${JSON.stringify(requestEnvelope, null, 2)}`;
|
||||
const userMessageForModel = `${userMessage}${surfaceHint}${capabilityHint}`;
|
||||
// Add the new user message
|
||||
dbMessages.push({
|
||||
conversationId,
|
||||
@@ -336,6 +385,38 @@ export class OpenCodeManager {
|
||||
});
|
||||
}
|
||||
|
||||
const protocolResult = this.protocolResponseBuilder.build({
|
||||
rawAssistantOutput: fullResponse,
|
||||
surface,
|
||||
capabilities,
|
||||
});
|
||||
|
||||
const previousCheckpoint = await this.workflowCheckpointStore.load(conversationId);
|
||||
const previousState: AgentTurnState = previousCheckpoint?.state || 'planning';
|
||||
const nextState = this.turnStateMachine.transition({
|
||||
previousState,
|
||||
envelope: {
|
||||
intent: protocolResult.envelope.intent,
|
||||
needsInput: protocolResult.envelope.needsInput,
|
||||
},
|
||||
});
|
||||
|
||||
await this.workflowCheckpointStore.save({
|
||||
conversationId,
|
||||
state: nextState,
|
||||
pendingFields: protocolResult.envelope.needsInput.fields.map((field) => field.key),
|
||||
lastTraceId: protocolResult.envelope.traceId,
|
||||
updatedAt: new Date().toISOString(),
|
||||
});
|
||||
|
||||
const blockedActionWarnings = protocolResult.warnings.filter((warning) => warning.includes('Blocked unsupported action'));
|
||||
getProtocolTelemetryService().recordTurn({
|
||||
validEnvelope: !protocolResult.validationError,
|
||||
repairAttempted: protocolResult.repairAttempted,
|
||||
fallbackUsed: Boolean(protocolResult.validationError),
|
||||
blockedActions: blockedActionWarnings.length,
|
||||
});
|
||||
|
||||
// Generate title after first exchange
|
||||
const userMsgCount = conversation.messages.filter(m => m.role === 'user').length;
|
||||
if (userMsgCount === 0 && fullResponse) {
|
||||
@@ -346,7 +427,11 @@ export class OpenCodeManager {
|
||||
|
||||
return {
|
||||
success: true,
|
||||
message: fullResponse,
|
||||
message: protocolResult.envelope.assistantText,
|
||||
envelope: protocolResult.envelope,
|
||||
protocolVersion: protocolResult.envelope.protocolVersion,
|
||||
traceId: protocolResult.traceId,
|
||||
warnings: protocolResult.warnings,
|
||||
toolCalls: toolCallsCollected.length > 0 ? toolCallsCollected : undefined,
|
||||
};
|
||||
} catch (error) {
|
||||
|
||||
@@ -8,6 +8,7 @@ import { OpenCodeManager } from '../engine/OpenCodeManager';
|
||||
import { getPostEngine } from '../engine/PostEngine';
|
||||
import { getMediaEngine } from '../engine/MediaEngine';
|
||||
import { getDatabase } from '../database';
|
||||
import { getProtocolTelemetryService } from '../agentic/observability/protocolTelemetry';
|
||||
|
||||
let chatEngine: ChatEngine | null = null;
|
||||
let openCodeManager: OpenCodeManager | null = null;
|
||||
@@ -135,6 +136,10 @@ export function registerChatHandlers(): void {
|
||||
|
||||
// ============ Chat Settings ============
|
||||
|
||||
ipcMain.handle('chat:getProtocolHealth', async () => {
|
||||
return getProtocolTelemetryService().getSnapshot();
|
||||
});
|
||||
|
||||
// Get available models
|
||||
ipcMain.handle('chat:getAvailableModels', async () => {
|
||||
try {
|
||||
|
||||
@@ -286,6 +286,7 @@ export const electronAPI: ElectronAPI = {
|
||||
getApiKey: () => ipcRenderer.invoke('chat:getApiKey'),
|
||||
|
||||
// Settings
|
||||
getProtocolHealth: () => ipcRenderer.invoke('chat:getProtocolHealth'),
|
||||
getAvailableModels: () => ipcRenderer.invoke('chat:getAvailableModels'),
|
||||
setDefaultModel: (modelId: string) => ipcRenderer.invoke('chat:setDefaultModel', modelId),
|
||||
getSystemPrompt: () => ipcRenderer.invoke('chat:getSystemPrompt'),
|
||||
|
||||
@@ -435,6 +435,53 @@ export interface ChatSendMetadata {
|
||||
surface?: 'tab' | 'sidebar';
|
||||
}
|
||||
|
||||
export interface ProtocolNeedsInputField {
|
||||
key: string;
|
||||
label: string;
|
||||
inputType: 'text' | 'textarea' | 'select' | 'checkbox' | 'date' | 'number';
|
||||
required?: boolean;
|
||||
options?: Array<{ label: string; value: string }>;
|
||||
placeholder?: string;
|
||||
defaultValue?: string | number | boolean;
|
||||
}
|
||||
|
||||
export interface ProtocolAction {
|
||||
id: string;
|
||||
action: string;
|
||||
label?: string;
|
||||
payload?: Record<string, unknown>;
|
||||
policy: 'silent' | 'confirm' | 'danger';
|
||||
requiresConfirmation: boolean;
|
||||
}
|
||||
|
||||
export interface ProtocolResponseEnvelope {
|
||||
protocolVersion: '2.0';
|
||||
assistantText: string;
|
||||
ui?: {
|
||||
specVersion: '1';
|
||||
elements: unknown[];
|
||||
};
|
||||
intent: 'analyze' | 'ask_input' | 'propose_action' | 'execute_action' | 'summarize';
|
||||
needsInput: {
|
||||
required: boolean;
|
||||
fields: ProtocolNeedsInputField[];
|
||||
};
|
||||
actions: ProtocolAction[];
|
||||
confidence: number;
|
||||
traceId: string;
|
||||
}
|
||||
|
||||
export interface ProtocolTelemetrySnapshot {
|
||||
totalTurns: number;
|
||||
validEnvelopeTurns: number;
|
||||
repairAttempts: number;
|
||||
fallbackTurns: number;
|
||||
blockedActionCount: number;
|
||||
parseValidityRate: number;
|
||||
repairRate: number;
|
||||
fallbackRate: number;
|
||||
}
|
||||
|
||||
export interface SiteValidationReport {
|
||||
sitemapPath: string;
|
||||
sitemapChanged: boolean;
|
||||
@@ -717,6 +764,7 @@ export interface ElectronAPI {
|
||||
getApiKey: () => Promise<ChatApiKeyStatus>;
|
||||
|
||||
// Settings
|
||||
getProtocolHealth: () => Promise<ProtocolTelemetrySnapshot>;
|
||||
getAvailableModels: () => Promise<{ success: boolean; models?: ChatModel[]; selectedModel?: string; error?: string }>;
|
||||
setDefaultModel: (modelId: string) => Promise<{ success: boolean; error?: string }>;
|
||||
getSystemPrompt: () => Promise<{ success: boolean; prompt?: string; error?: string }>;
|
||||
@@ -730,7 +778,7 @@ export interface ElectronAPI {
|
||||
deleteConversation: (id: string) => Promise<boolean>;
|
||||
|
||||
// Messaging
|
||||
sendMessage: (conversationId: string, message: string, metadata?: ChatSendMetadata) => Promise<{ success: boolean; message?: string; error?: string }>;
|
||||
sendMessage: (conversationId: string, message: string, metadata?: ChatSendMetadata) => Promise<{ success: boolean; message?: string; envelope?: ProtocolResponseEnvelope; protocolVersion?: '2.0'; traceId?: string; warnings?: string[]; error?: string }>;
|
||||
addSystemEvent: (conversationId: string, content: string) => Promise<{ success: boolean; error?: string }>;
|
||||
abortMessage: (conversationId: string) => Promise<void>;
|
||||
getHistory: (conversationId: string) => Promise<ChatMessage[]>;
|
||||
|
||||
Reference in New Issue
Block a user