@shareai-lab/kode 1.0.69 → 1.0.71

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (253) hide show
  1. package/README.md +205 -72
  2. package/README.zh-CN.md +246 -0
  3. package/cli.js +62 -0
  4. package/package.json +45 -25
  5. package/scripts/postinstall.js +56 -0
  6. package/src/ProjectOnboarding.tsx +180 -0
  7. package/src/Tool.ts +53 -0
  8. package/src/commands/approvedTools.ts +53 -0
  9. package/src/commands/bug.tsx +20 -0
  10. package/src/commands/clear.ts +43 -0
  11. package/src/commands/compact.ts +120 -0
  12. package/src/commands/config.tsx +19 -0
  13. package/src/commands/cost.ts +18 -0
  14. package/src/commands/ctx_viz.ts +209 -0
  15. package/src/commands/doctor.ts +24 -0
  16. package/src/commands/help.tsx +19 -0
  17. package/src/commands/init.ts +37 -0
  18. package/src/commands/listen.ts +42 -0
  19. package/src/commands/login.tsx +51 -0
  20. package/src/commands/logout.tsx +40 -0
  21. package/src/commands/mcp.ts +41 -0
  22. package/src/commands/model.tsx +40 -0
  23. package/src/commands/modelstatus.tsx +20 -0
  24. package/src/commands/onboarding.tsx +34 -0
  25. package/src/commands/pr_comments.ts +59 -0
  26. package/src/commands/refreshCommands.ts +54 -0
  27. package/src/commands/release-notes.ts +34 -0
  28. package/src/commands/resume.tsx +30 -0
  29. package/src/commands/review.ts +49 -0
  30. package/src/commands/terminalSetup.ts +221 -0
  31. package/src/commands.ts +136 -0
  32. package/src/components/ApproveApiKey.tsx +93 -0
  33. package/src/components/AsciiLogo.tsx +13 -0
  34. package/src/components/AutoUpdater.tsx +148 -0
  35. package/src/components/Bug.tsx +367 -0
  36. package/src/components/Config.tsx +289 -0
  37. package/src/components/ConsoleOAuthFlow.tsx +326 -0
  38. package/src/components/Cost.tsx +23 -0
  39. package/src/components/CostThresholdDialog.tsx +46 -0
  40. package/src/components/CustomSelect/option-map.ts +42 -0
  41. package/src/components/CustomSelect/select-option.tsx +52 -0
  42. package/src/components/CustomSelect/select.tsx +143 -0
  43. package/src/components/CustomSelect/use-select-state.ts +414 -0
  44. package/src/components/CustomSelect/use-select.ts +35 -0
  45. package/src/components/FallbackToolUseRejectedMessage.tsx +15 -0
  46. package/src/components/FileEditToolUpdatedMessage.tsx +66 -0
  47. package/src/components/Help.tsx +215 -0
  48. package/src/components/HighlightedCode.tsx +33 -0
  49. package/src/components/InvalidConfigDialog.tsx +113 -0
  50. package/src/components/Link.tsx +32 -0
  51. package/src/components/LogSelector.tsx +86 -0
  52. package/src/components/Logo.tsx +145 -0
  53. package/src/components/MCPServerApprovalDialog.tsx +100 -0
  54. package/src/components/MCPServerDialogCopy.tsx +25 -0
  55. package/src/components/MCPServerMultiselectDialog.tsx +109 -0
  56. package/src/components/Message.tsx +219 -0
  57. package/src/components/MessageResponse.tsx +15 -0
  58. package/src/components/MessageSelector.tsx +211 -0
  59. package/src/components/ModeIndicator.tsx +88 -0
  60. package/src/components/ModelConfig.tsx +301 -0
  61. package/src/components/ModelListManager.tsx +223 -0
  62. package/src/components/ModelSelector.tsx +3208 -0
  63. package/src/components/ModelStatusDisplay.tsx +228 -0
  64. package/src/components/Onboarding.tsx +274 -0
  65. package/src/components/PressEnterToContinue.tsx +11 -0
  66. package/src/components/PromptInput.tsx +710 -0
  67. package/src/components/SentryErrorBoundary.ts +33 -0
  68. package/src/components/Spinner.tsx +129 -0
  69. package/src/components/StructuredDiff.tsx +184 -0
  70. package/src/components/TextInput.tsx +246 -0
  71. package/src/components/TokenWarning.tsx +31 -0
  72. package/src/components/ToolUseLoader.tsx +40 -0
  73. package/src/components/TrustDialog.tsx +106 -0
  74. package/src/components/binary-feedback/BinaryFeedback.tsx +63 -0
  75. package/src/components/binary-feedback/BinaryFeedbackOption.tsx +111 -0
  76. package/src/components/binary-feedback/BinaryFeedbackView.tsx +172 -0
  77. package/src/components/binary-feedback/utils.ts +220 -0
  78. package/src/components/messages/AssistantBashOutputMessage.tsx +22 -0
  79. package/src/components/messages/AssistantLocalCommandOutputMessage.tsx +45 -0
  80. package/src/components/messages/AssistantRedactedThinkingMessage.tsx +19 -0
  81. package/src/components/messages/AssistantTextMessage.tsx +144 -0
  82. package/src/components/messages/AssistantThinkingMessage.tsx +40 -0
  83. package/src/components/messages/AssistantToolUseMessage.tsx +123 -0
  84. package/src/components/messages/UserBashInputMessage.tsx +28 -0
  85. package/src/components/messages/UserCommandMessage.tsx +30 -0
  86. package/src/components/messages/UserKodingInputMessage.tsx +28 -0
  87. package/src/components/messages/UserPromptMessage.tsx +35 -0
  88. package/src/components/messages/UserTextMessage.tsx +39 -0
  89. package/src/components/messages/UserToolResultMessage/UserToolCanceledMessage.tsx +12 -0
  90. package/src/components/messages/UserToolResultMessage/UserToolErrorMessage.tsx +36 -0
  91. package/src/components/messages/UserToolResultMessage/UserToolRejectMessage.tsx +31 -0
  92. package/src/components/messages/UserToolResultMessage/UserToolResultMessage.tsx +57 -0
  93. package/src/components/messages/UserToolResultMessage/UserToolSuccessMessage.tsx +35 -0
  94. package/src/components/messages/UserToolResultMessage/utils.tsx +56 -0
  95. package/src/components/permissions/BashPermissionRequest/BashPermissionRequest.tsx +121 -0
  96. package/src/components/permissions/FallbackPermissionRequest.tsx +155 -0
  97. package/src/components/permissions/FileEditPermissionRequest/FileEditPermissionRequest.tsx +182 -0
  98. package/src/components/permissions/FileEditPermissionRequest/FileEditToolDiff.tsx +75 -0
  99. package/src/components/permissions/FileWritePermissionRequest/FileWritePermissionRequest.tsx +164 -0
  100. package/src/components/permissions/FileWritePermissionRequest/FileWriteToolDiff.tsx +81 -0
  101. package/src/components/permissions/FilesystemPermissionRequest/FilesystemPermissionRequest.tsx +242 -0
  102. package/src/components/permissions/PermissionRequest.tsx +103 -0
  103. package/src/components/permissions/PermissionRequestTitle.tsx +69 -0
  104. package/src/components/permissions/hooks.ts +44 -0
  105. package/src/components/permissions/toolUseOptions.ts +59 -0
  106. package/src/components/permissions/utils.ts +23 -0
  107. package/src/constants/betas.ts +5 -0
  108. package/src/constants/claude-asterisk-ascii-art.tsx +238 -0
  109. package/src/constants/figures.ts +4 -0
  110. package/src/constants/keys.ts +3 -0
  111. package/src/constants/macros.ts +6 -0
  112. package/src/constants/models.ts +935 -0
  113. package/src/constants/oauth.ts +18 -0
  114. package/src/constants/product.ts +17 -0
  115. package/src/constants/prompts.ts +177 -0
  116. package/src/constants/releaseNotes.ts +7 -0
  117. package/src/context/PermissionContext.tsx +149 -0
  118. package/src/context.ts +278 -0
  119. package/src/cost-tracker.ts +84 -0
  120. package/src/entrypoints/cli.tsx +1498 -0
  121. package/src/entrypoints/mcp.ts +176 -0
  122. package/src/history.ts +25 -0
  123. package/src/hooks/useApiKeyVerification.ts +59 -0
  124. package/src/hooks/useArrowKeyHistory.ts +55 -0
  125. package/src/hooks/useCanUseTool.ts +138 -0
  126. package/src/hooks/useCancelRequest.ts +39 -0
  127. package/src/hooks/useDoublePress.ts +42 -0
  128. package/src/hooks/useExitOnCtrlCD.ts +31 -0
  129. package/src/hooks/useInterval.ts +25 -0
  130. package/src/hooks/useLogMessages.ts +16 -0
  131. package/src/hooks/useLogStartupTime.ts +12 -0
  132. package/src/hooks/useNotifyAfterTimeout.ts +65 -0
  133. package/src/hooks/usePermissionRequestLogging.ts +44 -0
  134. package/src/hooks/useSlashCommandTypeahead.ts +137 -0
  135. package/src/hooks/useTerminalSize.ts +49 -0
  136. package/src/hooks/useTextInput.ts +315 -0
  137. package/src/messages.ts +37 -0
  138. package/src/permissions.ts +268 -0
  139. package/src/query.ts +704 -0
  140. package/src/screens/ConfigureNpmPrefix.tsx +197 -0
  141. package/src/screens/Doctor.tsx +219 -0
  142. package/src/screens/LogList.tsx +68 -0
  143. package/src/screens/REPL.tsx +792 -0
  144. package/src/screens/ResumeConversation.tsx +68 -0
  145. package/src/services/browserMocks.ts +66 -0
  146. package/src/services/claude.ts +1947 -0
  147. package/src/services/customCommands.ts +683 -0
  148. package/src/services/fileFreshness.ts +377 -0
  149. package/src/services/mcpClient.ts +564 -0
  150. package/src/services/mcpServerApproval.tsx +50 -0
  151. package/src/services/notifier.ts +40 -0
  152. package/src/services/oauth.ts +357 -0
  153. package/src/services/openai.ts +796 -0
  154. package/src/services/sentry.ts +3 -0
  155. package/src/services/statsig.ts +171 -0
  156. package/src/services/statsigStorage.ts +86 -0
  157. package/src/services/systemReminder.ts +406 -0
  158. package/src/services/vcr.ts +161 -0
  159. package/src/tools/ArchitectTool/ArchitectTool.tsx +122 -0
  160. package/src/tools/ArchitectTool/prompt.ts +15 -0
  161. package/src/tools/AskExpertModelTool/AskExpertModelTool.tsx +505 -0
  162. package/src/tools/BashTool/BashTool.tsx +270 -0
  163. package/src/tools/BashTool/BashToolResultMessage.tsx +38 -0
  164. package/src/tools/BashTool/OutputLine.tsx +48 -0
  165. package/src/tools/BashTool/prompt.ts +174 -0
  166. package/src/tools/BashTool/utils.ts +56 -0
  167. package/src/tools/FileEditTool/FileEditTool.tsx +316 -0
  168. package/src/tools/FileEditTool/prompt.ts +51 -0
  169. package/src/tools/FileEditTool/utils.ts +58 -0
  170. package/src/tools/FileReadTool/FileReadTool.tsx +371 -0
  171. package/src/tools/FileReadTool/prompt.ts +7 -0
  172. package/src/tools/FileWriteTool/FileWriteTool.tsx +297 -0
  173. package/src/tools/FileWriteTool/prompt.ts +10 -0
  174. package/src/tools/GlobTool/GlobTool.tsx +119 -0
  175. package/src/tools/GlobTool/prompt.ts +8 -0
  176. package/src/tools/GrepTool/GrepTool.tsx +147 -0
  177. package/src/tools/GrepTool/prompt.ts +11 -0
  178. package/src/tools/MCPTool/MCPTool.tsx +106 -0
  179. package/src/tools/MCPTool/prompt.ts +3 -0
  180. package/src/tools/MemoryReadTool/MemoryReadTool.tsx +127 -0
  181. package/src/tools/MemoryReadTool/prompt.ts +3 -0
  182. package/src/tools/MemoryWriteTool/MemoryWriteTool.tsx +89 -0
  183. package/src/tools/MemoryWriteTool/prompt.ts +3 -0
  184. package/src/tools/MultiEditTool/MultiEditTool.tsx +366 -0
  185. package/src/tools/MultiEditTool/prompt.ts +45 -0
  186. package/src/tools/NotebookEditTool/NotebookEditTool.tsx +298 -0
  187. package/src/tools/NotebookEditTool/prompt.ts +3 -0
  188. package/src/tools/NotebookReadTool/NotebookReadTool.tsx +266 -0
  189. package/src/tools/NotebookReadTool/prompt.ts +3 -0
  190. package/src/tools/StickerRequestTool/StickerRequestTool.tsx +93 -0
  191. package/src/tools/StickerRequestTool/prompt.ts +19 -0
  192. package/src/tools/TaskTool/TaskTool.tsx +382 -0
  193. package/src/tools/TaskTool/constants.ts +1 -0
  194. package/src/tools/TaskTool/prompt.ts +56 -0
  195. package/src/tools/ThinkTool/ThinkTool.tsx +56 -0
  196. package/src/tools/ThinkTool/prompt.ts +12 -0
  197. package/src/tools/TodoWriteTool/TodoWriteTool.tsx +289 -0
  198. package/src/tools/TodoWriteTool/prompt.ts +63 -0
  199. package/src/tools/lsTool/lsTool.tsx +269 -0
  200. package/src/tools/lsTool/prompt.ts +2 -0
  201. package/src/tools.ts +63 -0
  202. package/src/types/PermissionMode.ts +120 -0
  203. package/src/types/RequestContext.ts +72 -0
  204. package/src/utils/Cursor.ts +436 -0
  205. package/src/utils/PersistentShell.ts +373 -0
  206. package/src/utils/agentStorage.ts +97 -0
  207. package/src/utils/array.ts +3 -0
  208. package/src/utils/ask.tsx +98 -0
  209. package/src/utils/auth.ts +13 -0
  210. package/src/utils/autoCompactCore.ts +223 -0
  211. package/src/utils/autoUpdater.ts +318 -0
  212. package/src/utils/betas.ts +20 -0
  213. package/src/utils/browser.ts +14 -0
  214. package/src/utils/cleanup.ts +72 -0
  215. package/src/utils/commands.ts +261 -0
  216. package/src/utils/config.ts +771 -0
  217. package/src/utils/conversationRecovery.ts +54 -0
  218. package/src/utils/debugLogger.ts +1123 -0
  219. package/src/utils/diff.ts +42 -0
  220. package/src/utils/env.ts +57 -0
  221. package/src/utils/errors.ts +21 -0
  222. package/src/utils/exampleCommands.ts +108 -0
  223. package/src/utils/execFileNoThrow.ts +51 -0
  224. package/src/utils/expertChatStorage.ts +136 -0
  225. package/src/utils/file.ts +402 -0
  226. package/src/utils/fileRecoveryCore.ts +71 -0
  227. package/src/utils/format.tsx +44 -0
  228. package/src/utils/generators.ts +62 -0
  229. package/src/utils/git.ts +92 -0
  230. package/src/utils/globalLogger.ts +77 -0
  231. package/src/utils/http.ts +10 -0
  232. package/src/utils/imagePaste.ts +38 -0
  233. package/src/utils/json.ts +13 -0
  234. package/src/utils/log.ts +382 -0
  235. package/src/utils/markdown.ts +213 -0
  236. package/src/utils/messageContextManager.ts +289 -0
  237. package/src/utils/messages.tsx +938 -0
  238. package/src/utils/model.ts +836 -0
  239. package/src/utils/permissions/filesystem.ts +118 -0
  240. package/src/utils/ripgrep.ts +167 -0
  241. package/src/utils/sessionState.ts +49 -0
  242. package/src/utils/state.ts +25 -0
  243. package/src/utils/style.ts +29 -0
  244. package/src/utils/terminal.ts +49 -0
  245. package/src/utils/theme.ts +122 -0
  246. package/src/utils/thinking.ts +144 -0
  247. package/src/utils/todoStorage.ts +431 -0
  248. package/src/utils/tokens.ts +43 -0
  249. package/src/utils/toolExecutionController.ts +163 -0
  250. package/src/utils/unaryLogging.ts +26 -0
  251. package/src/utils/user.ts +37 -0
  252. package/src/utils/validate.ts +165 -0
  253. package/cli.mjs +0 -1803
@@ -0,0 +1,35 @@
1
+ import { ToolResultBlockParam } from '@anthropic-ai/sdk/resources/index.mjs'
2
+ import { Box } from 'ink'
3
+ import * as React from 'react'
4
+ import { Tool } from '../../../Tool'
5
+ import { Message, UserMessage } from '../../../query'
6
+ import { useGetToolFromMessages } from './utils'
7
+
8
+ type Props = {
9
+ param: ToolResultBlockParam
10
+ message: UserMessage
11
+ messages: Message[]
12
+ verbose: boolean
13
+ tools: Tool[]
14
+ width: number | string
15
+ }
16
+
17
+ export function UserToolSuccessMessage({
18
+ param,
19
+ message,
20
+ messages,
21
+ tools,
22
+ verbose,
23
+ width,
24
+ }: Props): React.ReactNode {
25
+ const { tool } = useGetToolFromMessages(param.tool_use_id, tools, messages)
26
+
27
+ return (
28
+ // TODO: Distinguish UserMessage from UserToolResultMessage
29
+ <Box flexDirection="column" width={width}>
30
+ {tool.renderToolResultMessage?.(message.toolUseResult!.data as never, {
31
+ verbose,
32
+ })}
33
+ </Box>
34
+ )
35
+ }
@@ -0,0 +1,56 @@
1
+ import { ToolUseBlockParam } from '@anthropic-ai/sdk/resources/index.mjs'
2
+ import { Message } from '../../../query'
3
+ import { useMemo } from 'react'
4
+ import { Tool } from '../../../Tool'
5
+ import { GlobTool } from '../../../tools/GlobTool/GlobTool'
6
+ import { GrepTool } from '../../../tools/GrepTool/GrepTool'
7
+ import { logEvent } from '../../../services/statsig'
8
+
9
+ function getToolUseFromMessages(
10
+ toolUseID: string,
11
+ messages: Message[],
12
+ ): ToolUseBlockParam | null {
13
+ let toolUse: ToolUseBlockParam | null = null
14
+ for (const message of messages) {
15
+ if (
16
+ message.type !== 'assistant' ||
17
+ !Array.isArray(message.message.content)
18
+ ) {
19
+ continue
20
+ }
21
+ for (const content of message.message.content) {
22
+ if (content.type === 'tool_use' && content.id === toolUseID) {
23
+ toolUse = content
24
+ }
25
+ }
26
+ }
27
+ return toolUse
28
+ }
29
+
30
+ export function useGetToolFromMessages(
31
+ toolUseID: string,
32
+ tools: Tool[],
33
+ messages: Message[],
34
+ ) {
35
+ return useMemo(() => {
36
+ const toolUse = getToolUseFromMessages(toolUseID, messages)
37
+ if (!toolUse) {
38
+ throw new ReferenceError(
39
+ `Tool use not found for tool_use_id ${toolUseID}`,
40
+ )
41
+ }
42
+ // Hack: we don't expose GlobTool and GrepTool in getTools anymore,
43
+ // but we still want to be able to load old transcripts.
44
+ // TODO: Remove this when logging hits zero
45
+ const tool = [...tools, GlobTool, GrepTool].find(
46
+ _ => _.name === toolUse.name,
47
+ )
48
+ if (tool === GlobTool || tool === GrepTool) {
49
+ logEvent('tengu_legacy_tool_lookup', {})
50
+ }
51
+ if (!tool) {
52
+ throw new ReferenceError(`Tool not found for ${toolUse.name}`)
53
+ }
54
+ return { tool, toolUse }
55
+ }, [toolUseID, messages, tools])
56
+ }
@@ -0,0 +1,121 @@
1
+ import { Box, Text } from 'ink'
2
+ import React, { useMemo } from 'react'
3
+ import { UnaryEvent } from '../../../hooks/usePermissionRequestLogging'
4
+ import { savePermission } from '../../../permissions'
5
+ import { BashTool } from '../../../tools/BashTool/BashTool'
6
+ import { getTheme } from '../../../utils/theme'
7
+ import { usePermissionRequestLogging } from '../hooks'
8
+ import {
9
+ type ToolUseConfirm,
10
+ toolUseConfirmGetPrefix,
11
+ } from '../PermissionRequest.js'
12
+ import { PermissionRequestTitle } from '../PermissionRequestTitle'
13
+ import { logUnaryPermissionEvent } from '../utils'
14
+ import { Select } from '../../CustomSelect/select'
15
+ import { toolUseOptions } from '../toolUseOptions'
16
+
17
+ type Props = {
18
+ toolUseConfirm: ToolUseConfirm
19
+ onDone(): void
20
+ }
21
+
22
+ export function BashPermissionRequest({
23
+ toolUseConfirm,
24
+ onDone,
25
+ }: Props): React.ReactNode {
26
+ const theme = getTheme()
27
+
28
+ // ok to use parse since we've already validated args earliers
29
+ const { command } = BashTool.inputSchema.parse(toolUseConfirm.input)
30
+
31
+ const unaryEvent = useMemo<UnaryEvent>(
32
+ () => ({ completion_type: 'tool_use_single', language_name: 'none' }),
33
+ [],
34
+ )
35
+
36
+ usePermissionRequestLogging(toolUseConfirm, unaryEvent)
37
+
38
+ return (
39
+ <Box
40
+ flexDirection="column"
41
+ borderStyle="round"
42
+ borderColor={theme.permission}
43
+ marginTop={1}
44
+ paddingLeft={1}
45
+ paddingRight={1}
46
+ paddingBottom={1}
47
+ >
48
+ <PermissionRequestTitle
49
+ title="Bash command"
50
+ riskScore={toolUseConfirm.riskScore}
51
+ />
52
+ <Box flexDirection="column" paddingX={2} paddingY={1}>
53
+ <Text>{BashTool.renderToolUseMessage({ command })}</Text>
54
+ <Text color={theme.secondaryText}>{toolUseConfirm.description}</Text>
55
+ </Box>
56
+
57
+ <Box flexDirection="column">
58
+ <Text>Do you want to proceed?</Text>
59
+ <Select
60
+ options={toolUseOptions({ toolUseConfirm, command })}
61
+ onChange={newValue => {
62
+ switch (newValue) {
63
+ case 'yes':
64
+ logUnaryPermissionEvent(
65
+ 'tool_use_single',
66
+ toolUseConfirm,
67
+ 'accept',
68
+ )
69
+ toolUseConfirm.onAllow('temporary')
70
+ onDone()
71
+ break
72
+ case 'yes-dont-ask-again-prefix': {
73
+ const prefix = toolUseConfirmGetPrefix(toolUseConfirm)
74
+ if (prefix !== null) {
75
+ logUnaryPermissionEvent(
76
+ 'tool_use_single',
77
+ toolUseConfirm,
78
+ 'accept',
79
+ )
80
+ savePermission(
81
+ toolUseConfirm.tool,
82
+ toolUseConfirm.input,
83
+ prefix,
84
+ ).then(() => {
85
+ toolUseConfirm.onAllow('permanent')
86
+ onDone()
87
+ })
88
+ }
89
+ break
90
+ }
91
+ case 'yes-dont-ask-again-full':
92
+ logUnaryPermissionEvent(
93
+ 'tool_use_single',
94
+ toolUseConfirm,
95
+ 'accept',
96
+ )
97
+ savePermission(
98
+ toolUseConfirm.tool,
99
+ toolUseConfirm.input,
100
+ null, // Save without prefix
101
+ ).then(() => {
102
+ toolUseConfirm.onAllow('permanent')
103
+ onDone()
104
+ })
105
+ break
106
+ case 'no':
107
+ logUnaryPermissionEvent(
108
+ 'tool_use_single',
109
+ toolUseConfirm,
110
+ 'reject',
111
+ )
112
+ toolUseConfirm.onReject()
113
+ onDone()
114
+ break
115
+ }
116
+ }}
117
+ />
118
+ </Box>
119
+ </Box>
120
+ )
121
+ }
@@ -0,0 +1,155 @@
1
+ import { Box, Text } from 'ink'
2
+ import React, { useMemo } from 'react'
3
+ import { Select } from '@inkjs/ui'
4
+ import { getTheme } from '../../utils/theme'
5
+ import {
6
+ PermissionRequestTitle,
7
+ textColorForRiskScore,
8
+ } from './PermissionRequestTitle.js'
9
+ import { logUnaryEvent } from '../../utils/unaryLogging'
10
+ import { env } from '../../utils/env'
11
+ import { getCwd } from '../../utils/state'
12
+ import { savePermission } from '../../permissions'
13
+ import {
14
+ type ToolUseConfirm,
15
+ toolUseConfirmGetPrefix,
16
+ } from './PermissionRequest.js'
17
+ import chalk from 'chalk'
18
+ import {
19
+ UnaryEvent,
20
+ usePermissionRequestLogging,
21
+ } from '../../hooks/usePermissionRequestLogging.js'
22
+
23
+ type Props = {
24
+ toolUseConfirm: ToolUseConfirm
25
+ onDone(): void
26
+ verbose: boolean
27
+ }
28
+
29
+ export function FallbackPermissionRequest({
30
+ toolUseConfirm,
31
+ onDone,
32
+ verbose,
33
+ }: Props): React.ReactNode {
34
+ const theme = getTheme()
35
+
36
+ // TODO: Avoid these special cases
37
+ const originalUserFacingName = toolUseConfirm.tool.userFacingName(
38
+ toolUseConfirm.input as never,
39
+ )
40
+ const userFacingName = originalUserFacingName.endsWith(' (MCP)')
41
+ ? originalUserFacingName.slice(0, -6)
42
+ : originalUserFacingName
43
+
44
+ const unaryEvent = useMemo<UnaryEvent>(
45
+ () => ({
46
+ completion_type: 'tool_use_single',
47
+ language_name: 'none',
48
+ }),
49
+ [],
50
+ )
51
+
52
+ usePermissionRequestLogging(toolUseConfirm, unaryEvent)
53
+
54
+ return (
55
+ <Box
56
+ flexDirection="column"
57
+ borderStyle="round"
58
+ borderColor={textColorForRiskScore(toolUseConfirm.riskScore)}
59
+ marginTop={1}
60
+ paddingLeft={1}
61
+ paddingRight={1}
62
+ paddingBottom={1}
63
+ >
64
+ <PermissionRequestTitle
65
+ title="Tool use"
66
+ riskScore={toolUseConfirm.riskScore}
67
+ />
68
+ <Box flexDirection="column" paddingX={2} paddingY={1}>
69
+ <Text>
70
+ {userFacingName}(
71
+ {toolUseConfirm.tool.renderToolUseMessage(
72
+ toolUseConfirm.input as never,
73
+ { verbose },
74
+ )}
75
+ )
76
+ {originalUserFacingName.endsWith(' (MCP)') ? (
77
+ <Text color={theme.secondaryText}> (MCP)</Text>
78
+ ) : (
79
+ ''
80
+ )}
81
+ </Text>
82
+ <Text color={theme.secondaryText}>{toolUseConfirm.description}</Text>
83
+ </Box>
84
+
85
+ <Box flexDirection="column">
86
+ <Text>Do you want to proceed?</Text>
87
+ <Select
88
+ options={[
89
+ {
90
+ label: 'Yes',
91
+ value: 'yes',
92
+ },
93
+ {
94
+ label: `Yes, and don't ask again for ${chalk.bold(userFacingName)} commands in ${chalk.bold(getCwd())}`,
95
+ value: 'yes-dont-ask-again',
96
+ },
97
+ {
98
+ label: `No, and provide instructions (${chalk.bold.hex(getTheme().warning)('esc')})`,
99
+ value: 'no',
100
+ },
101
+ ]}
102
+ onChange={newValue => {
103
+ switch (newValue) {
104
+ case 'yes':
105
+ logUnaryEvent({
106
+ completion_type: 'tool_use_single',
107
+ event: 'accept',
108
+ metadata: {
109
+ language_name: 'none',
110
+ message_id: toolUseConfirm.assistantMessage.message.id,
111
+ platform: env.platform,
112
+ },
113
+ })
114
+ toolUseConfirm.onAllow('temporary')
115
+ onDone()
116
+ break
117
+ case 'yes-dont-ask-again':
118
+ logUnaryEvent({
119
+ completion_type: 'tool_use_single',
120
+ event: 'accept',
121
+ metadata: {
122
+ language_name: 'none',
123
+ message_id: toolUseConfirm.assistantMessage.message.id,
124
+ platform: env.platform,
125
+ },
126
+ })
127
+ savePermission(
128
+ toolUseConfirm.tool,
129
+ toolUseConfirm.input,
130
+ toolUseConfirmGetPrefix(toolUseConfirm),
131
+ ).then(() => {
132
+ toolUseConfirm.onAllow('permanent')
133
+ onDone()
134
+ })
135
+ break
136
+ case 'no':
137
+ logUnaryEvent({
138
+ completion_type: 'tool_use_single',
139
+ event: 'reject',
140
+ metadata: {
141
+ language_name: 'none',
142
+ message_id: toolUseConfirm.assistantMessage.message.id,
143
+ platform: env.platform,
144
+ },
145
+ })
146
+ toolUseConfirm.onReject()
147
+ onDone()
148
+ break
149
+ }
150
+ }}
151
+ />
152
+ </Box>
153
+ </Box>
154
+ )
155
+ }
@@ -0,0 +1,182 @@
1
+ import { Select } from '@inkjs/ui'
2
+ import chalk from 'chalk'
3
+ import { Box, Text } from 'ink'
4
+ import { basename, extname } from 'path'
5
+ import React, { useMemo } from 'react'
6
+ import {
7
+ UnaryEvent,
8
+ usePermissionRequestLogging,
9
+ } from '../../../hooks/usePermissionRequestLogging.js'
10
+ import { savePermission } from '../../../permissions'
11
+ import { env } from '../../../utils/env'
12
+ import { getTheme } from '../../../utils/theme'
13
+ import { logUnaryEvent } from '../../../utils/unaryLogging'
14
+ import {
15
+ type ToolUseConfirm,
16
+ toolUseConfirmGetPrefix,
17
+ } from '../PermissionRequest.js'
18
+ import {
19
+ PermissionRequestTitle,
20
+ textColorForRiskScore,
21
+ } from '../PermissionRequestTitle.js'
22
+ import { FileEditToolDiff } from './FileEditToolDiff'
23
+ import { useTerminalSize } from '../../../hooks/useTerminalSize'
24
+ import { pathInOriginalCwd } from '../../../utils/permissions/filesystem'
25
+
26
+ function getOptions(path: string) {
27
+ // Only show don't ask again option for edits in original working directory
28
+ const showDontAskAgainOptions = pathInOriginalCwd(path)
29
+ ? [
30
+ {
31
+ label: "Yes, and don't ask again this session",
32
+ value: 'yes-dont-ask-again',
33
+ },
34
+ ]
35
+ : []
36
+
37
+ return [
38
+ {
39
+ label: 'Yes',
40
+ value: 'yes',
41
+ },
42
+ ...showDontAskAgainOptions,
43
+ {
44
+ label: `No, and provide instructions (${chalk.bold.hex(getTheme().warning)('esc')})`,
45
+ value: 'no',
46
+ },
47
+ ]
48
+ }
49
+
50
+ type Props = {
51
+ toolUseConfirm: ToolUseConfirm
52
+ onDone(): void
53
+ verbose: boolean
54
+ }
55
+
56
+ export function FileEditPermissionRequest({
57
+ toolUseConfirm,
58
+ onDone,
59
+ verbose,
60
+ }: Props): React.ReactNode {
61
+ const { columns } = useTerminalSize()
62
+ const { file_path, new_string, old_string } = toolUseConfirm.input as {
63
+ file_path: string
64
+ new_string: string
65
+ old_string: string
66
+ }
67
+
68
+ const unaryEvent = useMemo<UnaryEvent>(
69
+ () => ({
70
+ completion_type: 'str_replace_single',
71
+ language_name: extractLanguageName(file_path),
72
+ }),
73
+ [file_path],
74
+ )
75
+
76
+ usePermissionRequestLogging(toolUseConfirm, unaryEvent)
77
+
78
+ return (
79
+ <Box
80
+ flexDirection="column"
81
+ borderStyle="round"
82
+ borderColor={textColorForRiskScore(toolUseConfirm.riskScore)}
83
+ marginTop={1}
84
+ paddingLeft={1}
85
+ paddingRight={1}
86
+ paddingBottom={1}
87
+ >
88
+ <PermissionRequestTitle
89
+ title="Edit file"
90
+ riskScore={toolUseConfirm.riskScore}
91
+ />
92
+ <FileEditToolDiff
93
+ file_path={file_path}
94
+ new_string={new_string}
95
+ old_string={old_string}
96
+ verbose={verbose}
97
+ width={columns - 12}
98
+ />
99
+ <Box flexDirection="column">
100
+ <Text>
101
+ Do you want to make this edit to{' '}
102
+ <Text bold>{basename(file_path)}</Text>?
103
+ </Text>
104
+ <Select
105
+ options={getOptions(file_path)}
106
+ onChange={newValue => {
107
+ switch (newValue) {
108
+ case 'yes':
109
+ extractLanguageName(file_path).then(language => {
110
+ logUnaryEvent({
111
+ completion_type: 'str_replace_single',
112
+ event: 'accept',
113
+ metadata: {
114
+ language_name: language,
115
+ message_id: toolUseConfirm.assistantMessage.message.id,
116
+ platform: env.platform,
117
+ },
118
+ })
119
+ })
120
+ // Note: We call onDone before onAllow to hide the
121
+ // permission request before we render the next message
122
+ onDone()
123
+ toolUseConfirm.onAllow('temporary')
124
+ break
125
+ case 'yes-dont-ask-again':
126
+ extractLanguageName(file_path).then(language => {
127
+ logUnaryEvent({
128
+ completion_type: 'str_replace_single',
129
+ event: 'accept',
130
+ metadata: {
131
+ language_name: language,
132
+ message_id: toolUseConfirm.assistantMessage.message.id,
133
+ platform: env.platform,
134
+ },
135
+ })
136
+ })
137
+ savePermission(
138
+ toolUseConfirm.tool,
139
+ toolUseConfirm.input,
140
+ toolUseConfirmGetPrefix(toolUseConfirm),
141
+ ).then(() => {
142
+ // Note: We call onDone before onAllow to hide the
143
+ // permission request before we render the next message
144
+ onDone()
145
+ toolUseConfirm.onAllow('permanent')
146
+ })
147
+ break
148
+ case 'no':
149
+ extractLanguageName(file_path).then(language => {
150
+ logUnaryEvent({
151
+ completion_type: 'str_replace_single',
152
+ event: 'reject',
153
+ metadata: {
154
+ language_name: language,
155
+ message_id: toolUseConfirm.assistantMessage.message.id,
156
+ platform: env.platform,
157
+ },
158
+ })
159
+ })
160
+ // Note: We call onDone before onAllow to hide the
161
+ // permission request before we render the next message
162
+ onDone()
163
+ toolUseConfirm.onReject()
164
+ break
165
+ }
166
+ }}
167
+ />
168
+ </Box>
169
+ </Box>
170
+ )
171
+ }
172
+
173
+ async function extractLanguageName(file_path: string): Promise<string> {
174
+ const ext = extname(file_path)
175
+ if (!ext) {
176
+ return 'unknown'
177
+ }
178
+ const Highlight = (await import('highlight.js')) as unknown as {
179
+ default: { getLanguage(ext: string): { name: string | undefined } }
180
+ }
181
+ return Highlight.default.getLanguage(ext.slice(1))?.name ?? 'unknown'
182
+ }
@@ -0,0 +1,75 @@
1
+ import * as React from 'react'
2
+ import { existsSync, readFileSync } from 'fs'
3
+ import { useMemo } from 'react'
4
+ import { StructuredDiff } from '../../StructuredDiff'
5
+ import { Box, Text } from 'ink'
6
+ import { getTheme } from '../../../utils/theme'
7
+ import { intersperse } from '../../../utils/array'
8
+ import { getCwd } from '../../../utils/state'
9
+ import { relative } from 'path'
10
+ import { getPatch } from '../../../utils/diff'
11
+
12
+ type Props = {
13
+ file_path: string
14
+ new_string: string
15
+ old_string: string
16
+ verbose: boolean
17
+ useBorder?: boolean
18
+ width: number
19
+ }
20
+
21
+ export function FileEditToolDiff({
22
+ file_path,
23
+ new_string,
24
+ old_string,
25
+ verbose,
26
+ useBorder = true,
27
+ width,
28
+ }: Props): React.ReactNode {
29
+ const file = useMemo(
30
+ () => (existsSync(file_path) ? readFileSync(file_path, 'utf8') : ''),
31
+ [file_path],
32
+ )
33
+ const patch = useMemo(
34
+ () =>
35
+ getPatch({
36
+ filePath: file_path,
37
+ fileContents: file,
38
+ oldStr: old_string,
39
+ newStr: new_string,
40
+ }),
41
+ [file_path, file, old_string, new_string],
42
+ )
43
+
44
+ return (
45
+ <Box flexDirection="column">
46
+ <Box
47
+ borderColor={getTheme().secondaryBorder}
48
+ borderStyle={useBorder ? 'round' : undefined}
49
+ flexDirection="column"
50
+ paddingX={1}
51
+ >
52
+ <Box paddingBottom={1}>
53
+ <Text bold>
54
+ {verbose ? file_path : relative(getCwd(), file_path)}
55
+ </Text>
56
+ </Box>
57
+ {intersperse(
58
+ patch.map(_ => (
59
+ <StructuredDiff
60
+ key={_.newStart}
61
+ patch={_}
62
+ dim={false}
63
+ width={width}
64
+ />
65
+ )),
66
+ i => (
67
+ <Text color={getTheme().secondaryText} key={`ellipsis-${i}`}>
68
+ ...
69
+ </Text>
70
+ ),
71
+ )}
72
+ </Box>
73
+ </Box>
74
+ )
75
+ }