run method

  1. @override
Future<int> run()
override

Runs this command.

The return value is wrapped in a Future if necessary and returned by CommandRunner.runCommand.

Implementation

@override
Future<int> run() async {
  final queryArg = (argResults?['query'] as String?) ??
      (argResults?.rest.isNotEmpty == true
          ? argResults!.rest.join(' ')
          : null);
  final typeStr = argResults?['type'] as String?;
  final sessionId = argResults?['session'] as String?;
  final scope = argResults?['scope'] as String?;
  final dirPath = argResults?['dir'] as String? ?? '.';
  final jsonOutput = argResults?['json'] as bool? ?? false;
  final includeExpired = argResults?['include-expired'] as bool? ?? false;

  if (queryArg == null || queryArg.trim().isEmpty) {
    print('Error: --query argument (or trailing query string) is required.');
    return 1;
  }

  final projectDir = Directory(dirPath);
  if (!projectDir.existsSync()) {
    print('Error: Target directory "$dirPath" does not exist.');
    return 1;
  }

  final engine = SessionMemoryEngine(projectRoot: projectDir.path);
  const renderer = SessionMemoryRenderer();

  final req = MemoryQueryRequest(
    query: queryArg,
    type: KnowledgeEntryType.tryParse(typeStr),
    sessionId: sessionId,
    scope: scope,
    includeExpired: includeExpired,
  );

  final result = await engine.query(req);

  if (jsonOutput) {
    print(renderer.renderQueryJson(result));
  } else {
    print(renderer.renderQueryMarkdown(result));
  }

  return result.isSuccess ? 0 : 1;
}