File size: 4,265 Bytes
84aa3bf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/**
 * @license
 * Copyright 2026 Google LLC
 * SPDX-License-Identifier: Apache-2.0
 */

import {
  listInboxMemoryPatches,
  listInboxSkills,
  listInboxPatches,
  listMemoryFiles,
  refreshMemory,
  showMemory,
} from '@google/gemini-cli-core';
import type {
  Command,
  CommandContext,
  CommandExecutionResponse,
} from './types.js';

export class MemoryCommand implements Command {
  readonly name = 'memory';
  readonly description = 'Manage memory.';
  readonly subCommands = [
    new ShowMemoryCommand(),
    new RefreshMemoryCommand(),
    new ListMemoryCommand(),
    new InboxMemoryCommand(),
  ];
  readonly requiresWorkspace = true;

  async execute(
    context: CommandContext,
    _: string[],
  ): Promise<CommandExecutionResponse> {
    return new ShowMemoryCommand().execute(context, _);
  }
}

export class ShowMemoryCommand implements Command {
  readonly name = 'memory show';
  readonly description = 'Shows the current memory contents.';

  async execute(
    context: CommandContext,
    _: string[],
  ): Promise<CommandExecutionResponse> {
    const result = showMemory(context.agentContext.config);
    return { name: this.name, data: result.content };
  }
}

export class RefreshMemoryCommand implements Command {
  readonly name = 'memory refresh';
  readonly aliases = ['memory reload'];
  readonly description = 'Refreshes the memory from the source.';

  async execute(
    context: CommandContext,
    _: string[],
  ): Promise<CommandExecutionResponse> {
    const result = await refreshMemory(context.agentContext.config);
    return { name: this.name, data: result.content };
  }
}

export class ListMemoryCommand implements Command {
  readonly name = 'memory list';
  readonly description = 'Lists the paths of the GEMINI.md files in use.';

  async execute(
    context: CommandContext,
    _: string[],
  ): Promise<CommandExecutionResponse> {
    const result = listMemoryFiles(context.agentContext.config);
    return { name: this.name, data: result.content };
  }
}

export class InboxMemoryCommand implements Command {
  readonly name = 'memory inbox';
  readonly description =
    'Lists memory items extracted from past sessions that are pending review.';

  async execute(
    context: CommandContext,
    _: string[],
  ): Promise<CommandExecutionResponse> {
    if (!context.agentContext.config.isAutoMemoryEnabled()) {
      return {
        name: this.name,
        data: 'The memory inbox requires Auto Memory. Enable it with: experimental.autoMemory = true in settings.',
      };
    }

    const [skills, patches, memoryPatches] = await Promise.all([
      listInboxSkills(context.agentContext.config),
      listInboxPatches(context.agentContext.config),
      listInboxMemoryPatches(context.agentContext.config),
    ]);

    if (
      skills.length === 0 &&
      patches.length === 0 &&
      memoryPatches.length === 0
    ) {
      return { name: this.name, data: 'No items in inbox.' };
    }

    const lines: string[] = [];
    for (const s of skills) {
      const date = s.extractedAt
        ? ` (extracted: ${new Date(s.extractedAt).toLocaleDateString()})`
        : '';
      lines.push(`- **${s.name}**: ${s.description}${date}`);
    }
    for (const p of patches) {
      const targets = p.entries.map((e) => e.targetPath).join(', ');
      const date = p.extractedAt
        ? ` (extracted: ${new Date(p.extractedAt).toLocaleDateString()})`
        : '';
      lines.push(`- **${p.name}** (update): patches ${targets}${date}`);
    }
    for (const memoryPatch of memoryPatches) {
      const targets = memoryPatch.entries.map((e) => e.targetPath).join(', ');
      const date = memoryPatch.extractedAt
        ? ` (latest extract: ${new Date(memoryPatch.extractedAt).toLocaleDateString()})`
        : '';
      const sourceCount = memoryPatch.sourceFiles.length;
      const sourceLabel = sourceCount === 1 ? 'patch' : 'patches';
      lines.push(
        `- **${memoryPatch.name}** (${sourceCount} source ${sourceLabel}, ${memoryPatch.entries.length} hunks): targets ${targets}${date}`,
      );
    }

    const total = skills.length + patches.length + memoryPatches.length;
    return {
      name: this.name,
      data: `Memory inbox (${total}):\n${lines.join('\n')}`,
    };
  }
}