Skip to content

Apply schema defaults when converting node types #427

Closed

Description

@malibio

Problem

When a user converts a node to a different type (e.g., via slash command /task or pattern detection [ ]), the node should automatically receive appropriate default properties based on its schema definition.

Current Behavior:

  • Node type changes from text → task
  • Node persists with empty properties: {}
  • User must manually add task status, priority, etc.

Expected Behavior:

  • Node type changes from text → task
  • Node automatically receives default properties: { task: { status: 'todo', priority: 'medium' } }
  • User can immediately interact with task-specific features

Context

This issue was identified in PR #425 (Issue #424) where a test was added but skipped:

it.skip('should apply task schema defaults when converting to task', async () => {
  // TODO: Schema defaults not yet implemented - will be added in separate issue
  const persisted = store.getNode(placeholderNode.id);
  expect(persisted?.properties).toHaveProperty('task');
  expect(persisted?.properties.task).toHaveProperty('status');
  expect(persisted?.properties.task).toHaveProperty('priority');
});

Test Location: packages/desktop-app/src/tests/integration/slash-command-type-persistence.test.ts:296-333

Requirements

Schema Default Application

  • When updateNodeType() is called, fetch the schema for the new node type
  • If schema defines default property values, merge them into the node's properties field
  • Preserve existing properties (don't overwrite user data)
  • Apply defaults for both slash command conversions AND pattern-based conversions

Example Schemas with Defaults

Task Node:

{
  nodeType: 'task',
  properties: {
    task: {
      status: { type: 'select', default: 'todo', options: ['todo', 'in-progress', 'done'] },
      priority: { type: 'select', default: 'medium', options: ['low', 'medium', 'high'] },
      dueDate: { type: 'date', optional: true }
    }
  }
}

Expected Result after /task conversion:

{
  id: 'node-123',
  nodeType: 'task',
  content: 'Buy groceries',
  properties: {
    task: {
      status: 'todo',     // ✅ Default applied
      priority: 'medium'  // ✅ Default applied
      // dueDate not set (optional)
    }
  }
}

Implementation Locations

1. ReactiveNodeService (reactive-node-service.svelte.ts):

updateNodeType(nodeId: string, nodeType: NodeType) {
  const node = sharedNodeStore.getNode(nodeId);
  if (!node) return;

  // 🆕 NEW: Get schema and apply defaults
  const schema = schemaService.getSchemaForNodeType(nodeType);
  const defaultProperties = schemaService.extractDefaults(schema);
  const mergedProperties = { ...node.properties, ...defaultProperties };

  sharedNodeStore.updateNode(
    nodeId,
    { nodeType, properties: mergedProperties },
    this._viewerSource
  );
}

2. SchemaService (schema-service.ts):
Add utility methods:

extractDefaults(schema: NodeSchema): Record<string, any> {
  // Parse schema definition and extract default values
}

getSchemaForNodeType(nodeType: NodeType): NodeSchema | null {
  // Fetch schema from registry or database
}

Acceptance Criteria

  • Schema defaults are applied when converting node types via slash commands
  • Schema defaults are applied when converting node types via pattern detection
  • Existing properties are NOT overwritten (merge behavior)
  • Optional properties without defaults are NOT created
  • Un-skip the test in slash-command-type-persistence.test.ts:296 and verify it passes
  • Add tests for:
    • Default property application
    • Property merging (existing + defaults)
    • Optional property handling
    • Multiple node types with different schemas
  • Update documentation in /docs/architecture/business-logic/node-behavior-system.md

Out of Scope

Related Issues

Priority

Medium - Not blocking current functionality, but important for user experience. Users can manually add properties in the meantime.

Estimated Effort

Small-Medium (~2-4 hours)

  • Schema service utility methods: 1 hour
  • Integration with ReactiveNodeService: 1 hour
  • Test coverage: 1-2 hours

Metadata

Metadata

Assignees

Labels

backendenhancement

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions