Repository navigation
Expand file tree
/
Copy pathenter_worktree.py
More file actions
65 lines (50 loc) · 2.12 KB
/
Copy pathenter_worktree.py
File metadata and controls
65 lines (50 loc) · 2.12 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
# 来源:公众号@小林coding
# 后端八股网站:xiaolincoding.com
# Agent网站:xiaolinnote.com
# 简历模版:jianli.xiaolinnote.com
from __future__ import annotations
import secrets
from typing import TYPE_CHECKING
from pydantic import BaseModel, Field
from kkcode.tools.base import Tool, ToolResult
from kkcode.worktree.slug import validate_slug
if TYPE_CHECKING:
from kkcode.worktree.manager import WorktreeManager
class EnterWorktreeParams(BaseModel):
name: str | None = Field(
default=None,
description=(
'Optional name for the worktree. Each "/"-separated segment may '
"contain only letters, digits, dots, underscores, and dashes; "
"max 64 chars total. A random name is generated if not provided."
),
)
class EnterWorktreeTool(Tool):
name = "EnterWorktree"
description = (
"Creates an isolated worktree (via git) and switches the session into it"
)
params_model = EnterWorktreeParams
category = "command"
def __init__(self, worktree_manager: WorktreeManager) -> None:
self._manager = worktree_manager
async def execute(self, params: EnterWorktreeParams) -> ToolResult:
if self._manager.get_current_session() is not None:
return ToolResult(output="Already in a worktree session", is_error=True)
slug = params.name or f"wt-{secrets.token_hex(4)}"
err = validate_slug(slug)
if err:
return ToolResult(output=f"Invalid worktree name: {err}", is_error=True)
try:
wt = await self._manager.create(slug)
session = await self._manager.enter(slug)
except Exception as e:
return ToolResult(output=f"Error creating worktree: {e}", is_error=True)
branch_info = f" on branch {wt.branch}" if wt.branch else ""
return ToolResult(
output=(
f"Created worktree at {session.worktree_path}{branch_info}. "
"The session is now working in the worktree. "
"Use ExitWorktree to leave mid-session, or exit the session to be prompted."
)
)