forked from ultraworkers/claw-code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTeamCreateTool.ts
More file actions
240 lines (211 loc) · 7.49 KB
/
Copy pathTeamCreateTool.ts
File metadata and controls
240 lines (211 loc) · 7.49 KB
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
import { z } from 'zod/v4'
import { getSessionId } from '../../bootstrap/state.js'
import { logEvent } from '../../services/analytics/index.js'
import type { AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS } from '../../services/analytics/metadata.js'
import type { Tool } from '../../Tool.js'
import { buildTool, type ToolDef } from '../../Tool.js'
import { formatAgentId } from '../../utils/agentId.js'
import { isAgentSwarmsEnabled } from '../../utils/agentSwarmsEnabled.js'
import { getCwd } from '../../utils/cwd.js'
import { lazySchema } from '../../utils/lazySchema.js'
import {
getDefaultMainLoopModel,
parseUserSpecifiedModel,
} from '../../utils/model/model.js'
import { jsonStringify } from '../../utils/slowOperations.js'
import { getResolvedTeammateMode } from '../../utils/swarm/backends/registry.js'
import { TEAM_LEAD_NAME } from '../../utils/swarm/constants.js'
import type { TeamFile } from '../../utils/swarm/teamHelpers.js'
import {
getTeamFilePath,
readTeamFile,
registerTeamForSessionCleanup,
sanitizeName,
writeTeamFileAsync,
} from '../../utils/swarm/teamHelpers.js'
import { assignTeammateColor } from '../../utils/swarm/teammateLayoutManager.js'
import {
ensureTasksDir,
resetTaskList,
setLeaderTeamName,
} from '../../utils/tasks.js'
import { generateWordSlug } from '../../utils/words.js'
import { TEAM_CREATE_TOOL_NAME } from './constants.js'
import { getPrompt } from './prompt.js'
import { renderToolUseMessage } from './UI.js'
const inputSchema = lazySchema(() =>
z.strictObject({
team_name: z.string().describe('Name for the new team to create.'),
description: z.string().optional().describe('Team description/purpose.'),
agent_type: z
.string()
.optional()
.describe(
'Type/role of the team lead (e.g., "researcher", "test-runner"). ' +
'Used for team file and inter-agent coordination.',
),
}),
)
type InputSchema = ReturnType<typeof inputSchema>
export type Output = {
team_name: string
team_file_path: string
lead_agent_id: string
}
export type Input = z.infer<InputSchema>
/**
* Generates a unique team name by checking if the provided name already exists.
* If the name already exists, generates a new word slug.
*/
function generateUniqueTeamName(providedName: string): string {
// If the team doesn't exist, use the provided name
if (!readTeamFile(providedName)) {
return providedName
}
// Team exists, generate a new unique name
return generateWordSlug()
}
export const TeamCreateTool: Tool<InputSchema, Output> = buildTool({
name: TEAM_CREATE_TOOL_NAME,
searchHint: 'create a multi-agent swarm team',
maxResultSizeChars: 100_000,
shouldDefer: true,
userFacingName() {
return ''
},
get inputSchema(): InputSchema {
return inputSchema()
},
isEnabled() {
return isAgentSwarmsEnabled()
},
toAutoClassifierInput(input) {
return input.team_name
},
async validateInput(input, _context) {
if (!input.team_name || input.team_name.trim().length === 0) {
return {
result: false,
message: 'team_name is required for TeamCreate',
errorCode: 9,
}
}
return { result: true }
},
async description() {
return 'Create a new team for coordinating multiple agents'
},
async prompt() {
return getPrompt()
},
mapToolResultToToolResultBlockParam(data, toolUseID) {
return {
tool_use_id: toolUseID,
type: 'tool_result' as const,
content: [
{
type: 'text' as const,
text: jsonStringify(data),
},
],
}
},
async call(input, context) {
const { setAppState, getAppState } = context
const { team_name, description: _description, agent_type } = input
// Check if already in a team - restrict to one team per leader
const appState = getAppState()
const existingTeam = appState.teamContext?.teamName
if (existingTeam) {
throw new Error(
`Already leading team "${existingTeam}". A leader can only manage one team at a time. Use TeamDelete to end the current team before creating a new one.`,
)
}
// If team already exists, generate a unique name instead of failing
const finalTeamName = generateUniqueTeamName(team_name)
// Generate a deterministic agent ID for the team lead
const leadAgentId = formatAgentId(TEAM_LEAD_NAME, finalTeamName)
const leadAgentType = agent_type || TEAM_LEAD_NAME
// Get the team lead's current model from AppState (handles session model, settings, CLI override)
const leadModel = parseUserSpecifiedModel(
appState.mainLoopModelForSession ??
appState.mainLoopModel ??
getDefaultMainLoopModel(),
)
const teamFilePath = getTeamFilePath(finalTeamName)
const teamFile: TeamFile = {
name: finalTeamName,
description: _description,
createdAt: Date.now(),
leadAgentId,
leadSessionId: getSessionId(), // Store actual session ID for team discovery
members: [
{
agentId: leadAgentId,
name: TEAM_LEAD_NAME,
agentType: leadAgentType,
model: leadModel,
joinedAt: Date.now(),
tmuxPaneId: '',
cwd: getCwd(),
subscriptions: [],
},
],
}
await writeTeamFileAsync(finalTeamName, teamFile)
// Track for session-end cleanup — teams were left on disk forever
// unless explicitly TeamDelete'd (gh-32730).
registerTeamForSessionCleanup(finalTeamName)
// Reset and create the corresponding task list directory (Team = Project = TaskList)
// This ensures task numbering starts fresh at 1 for each new swarm
const taskListId = sanitizeName(finalTeamName)
await resetTaskList(taskListId)
await ensureTasksDir(taskListId)
// Register the team name so getTaskListId() returns it for the leader.
// Without this, the leader falls through to getSessionId() and writes tasks
// to a different directory than tmux/iTerm2 teammates expect.
setLeaderTeamName(sanitizeName(finalTeamName))
// Update AppState with team context
setAppState(prev => ({
...prev,
teamContext: {
teamName: finalTeamName,
teamFilePath,
leadAgentId,
teammates: {
[leadAgentId]: {
name: TEAM_LEAD_NAME,
agentType: leadAgentType,
color: assignTeammateColor(leadAgentId),
tmuxSessionName: '',
tmuxPaneId: '',
cwd: getCwd(),
spawnedAt: Date.now(),
},
},
},
}))
logEvent('tengu_team_created', {
team_name:
finalTeamName as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS,
teammate_count: 1,
lead_agent_type:
leadAgentType as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS,
teammate_mode:
getResolvedTeammateMode() as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS,
})
// Note: We intentionally don't set CLAUDE_CODE_AGENT_ID for the team lead because:
// 1. The lead is not a "teammate" - isTeammate() should return false for them
// 2. Their ID is deterministic (team-lead@teamName) and can be derived when needed
// 3. Setting it would cause isTeammate() to return true, breaking inbox polling
// Team name is stored in AppState.teamContext, not process.env
return {
data: {
team_name: finalTeamName,
team_file_path: teamFilePath,
lead_agent_id: leadAgentId,
},
}
},
renderToolUseMessage,
} satisfies ToolDef<InputSchema, Output>)