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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
|
/**
* @license
* Copyright 2025 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import { SlashCommand, CommandContext } from './types.js';
import {
Config,
BackgroundAgentMessage,
partListUnionToString,
} from '@google/gemini-cli-core';
const MAX_STATUS_MESSAGE_LENGTH = 100;
function toMessageString(message?: BackgroundAgentMessage): string {
return partListUnionToString(message?.parts ?? []).trim();
}
function toOneliner(input: string, maxlength: number) {
let output = input.replace(/\r?\n|\r/g, ' ');
if (output.length > maxlength) {
output = output.substring(0, maxlength) + '...';
}
return output;
}
function getActiveAgent(context: CommandContext) {
const agent =
context.services.config?.getBackgroundAgentManager()?.activeAgent;
if (!agent) {
throw Error('There is no active background agent.');
}
return agent;
}
function addClientHistory(context: CommandContext, text: string) {
context.services.config!.getGeminiClient().addHistory({
role: 'user',
parts: [{ text }],
});
context.services.config!.getGeminiClient().addHistory({
role: 'model',
parts: [{ text: 'Got it.' }],
});
}
const startSubcommand: SlashCommand = {
name: 'start',
description:
'Start a new task with the provided prompt. Usage: /bg start <prompt>',
action: async (context, args) => {
if (!args || args.trim() === '') {
return {
type: 'message',
messageType: 'error',
content: 'The `start` command requires a prompt.',
};
}
const agent = getActiveAgent(context);
const task = await agent.startTask(args);
addClientHistory(
context,
`I started a background task with id '${task.id}' and prompt:\n${args}`,
);
return {
type: 'message',
messageType: 'info',
content: `Started background task with id '${task.id}' and prompt:\n${args}`,
};
},
};
const stopSubcommand: SlashCommand = {
name: 'stop',
description: 'Stops a running task. Usage: /bg stop <task_id>',
action: async (context, args) => {
if (!args || args.trim() === '') {
return {
type: 'message',
messageType: 'error',
content: 'The `stop` command requires a task id.',
};
}
const agent = getActiveAgent(context);
await agent.cancelTask(args);
addClientHistory(context, `I canceled the background task with id ${args}`);
return {
type: 'message',
messageType: 'info',
content: `Stopped background task with id ${args}.`,
};
},
};
const listSubcommand: SlashCommand = {
name: 'list',
description: 'List all tasks',
action: async (context, args) => {
if (args && args.trim() !== '') {
return {
type: 'message',
messageType: 'error',
content: 'The `list` command takes no arguments.',
};
}
const agent = getActiveAgent(context);
const tasks = await agent.listTasks();
let content: string;
if (tasks.length === 0) {
content = 'No background tasks found.';
} else {
const taskList = tasks
.map((task) => {
const shortStatus = toOneliner(
toMessageString(task.status.message),
MAX_STATUS_MESSAGE_LENGTH,
);
return ` - ${task.id}: (${task.status.state}) ${shortStatus}`;
})
.join('\n');
content = `Background tasks:\n${taskList}`;
}
return {
type: 'message',
messageType: 'info',
content,
};
},
};
const getSubcommand: SlashCommand = {
name: 'get',
description: 'View a task. Usage: /bg get <task_id>',
action: async (context, args) => {
if (!args || args.trim() === '') {
return {
type: 'message',
messageType: 'error',
content: 'The `get` command requires a task id.',
};
}
const agent = getActiveAgent(context);
const task = await agent.getTask(args);
const content = `Task Details for ${task.id}:
Status: (${task.status.state}) ${toMessageString(task.status.message)}}`;
return {
type: 'message',
messageType: 'info',
content,
};
},
};
const logsSubcommand: SlashCommand = {
name: 'logs',
description: "View a task's recent logs. Usage: /bg log <task_id>",
action: async (context, args) => {
if (!args || args.trim() === '') {
return {
type: 'message',
messageType: 'error',
content: 'The `log` command requires a task id.',
};
}
const agent = getActiveAgent(context);
const task = await agent.getTask(args, 5);
const contents = [
`Task logs for ${task.id}. status: (${task.status.state})`,
];
(task.history ?? []).forEach((message) => {
contents.push(toMessageString(message));
});
return {
type: 'message',
messageType: 'info',
content: contents.join('\n\n'),
};
},
};
const messageSubcommand: SlashCommand = {
name: 'message',
description:
'Send a message to a task. Usage: /bg message <task_id> <message>',
action: async (context, args) => {
if (!args || args.trim() === '' || !args.trim().includes(' ')) {
return {
type: 'message',
messageType: 'error',
content: 'The `message` command requires a task id and a message.',
};
}
const firstSpaceIndex = args.indexOf(' ');
const id = args.substring(0, firstSpaceIndex);
const message = args.substring(firstSpaceIndex + 1);
const agent = getActiveAgent(context);
await agent.messageTask(id, message);
addClientHistory(
context,
`I sent a message to the background task with id '${id}':\n${message}`,
);
return {
type: 'message',
messageType: 'info',
content: `Sent a message to the background task with id '${id}':\n${message}`,
};
},
};
const deleteSubcommand: SlashCommand = {
name: 'delete',
description: 'Deletes a task. Usage: /bg delete <task_id>',
action: async (context, args) => {
if (!args) {
return {
type: 'message',
messageType: 'error',
content: 'The `delete` command requires a task id.',
};
}
const agent = getActiveAgent(context);
await agent.deleteTask(args);
addClientHistory(context, `I deleted the background task with id ${args}`);
return {
type: 'message',
messageType: 'info',
content: `Task ${args} deleted.`,
};
},
};
export const backgroundCommand = (
config: Config | null,
): SlashCommand | null => {
if (!config?.getBackgroundAgentManager()?.activeAgent) {
return null;
}
return {
name: 'background',
altName: 'bg',
description: "Commands for managing the background agent's tasks",
subCommands: [
startSubcommand,
stopSubcommand,
listSubcommand,
getSubcommand,
logsSubcommand,
messageSubcommand,
deleteSubcommand,
],
};
};
|