mirror of
https://github.com/claude-code-best/claude-code.git
synced 2026-06-17 22:05:50 +00:00
feat: 大规模清理 claude 的类型问题及依赖
This commit is contained in:
@@ -935,9 +935,9 @@ export async function runHeadless(
|
||||
switch (lastMessage.subtype) {
|
||||
case 'success':
|
||||
writeToStdout(
|
||||
lastMessage.result.endsWith('\n')
|
||||
? lastMessage.result
|
||||
: lastMessage.result + '\n',
|
||||
(lastMessage.result as string).endsWith('\n')
|
||||
? (lastMessage.result as string)
|
||||
: (lastMessage.result as string) + '\n',
|
||||
)
|
||||
break
|
||||
case 'error_during_execution':
|
||||
@@ -1203,6 +1203,7 @@ function runHeadlessStreaming(
|
||||
const hasFastMode = isFastModeSupportedByModel(option.value)
|
||||
const hasAutoMode = modelSupportsAutoMode(resolvedModel)
|
||||
return {
|
||||
name: modelId,
|
||||
value: modelId,
|
||||
displayName: option.label,
|
||||
description: option.description,
|
||||
@@ -1235,6 +1236,7 @@ function runHeadlessStreaming(
|
||||
) {
|
||||
output.enqueue({
|
||||
type: 'user',
|
||||
content: crumb.message.content,
|
||||
message: crumb.message,
|
||||
session_id: getSessionId(),
|
||||
parent_tool_use_id: null,
|
||||
@@ -1646,10 +1648,11 @@ function runHeadlessStreaming(
|
||||
connection.config.type === 'stdio' ||
|
||||
connection.config.type === undefined
|
||||
) {
|
||||
const stdioConfig = connection.config as { command: string; args: string[] }
|
||||
config = {
|
||||
type: 'stdio' as const,
|
||||
command: connection.config.command,
|
||||
args: connection.config.args,
|
||||
command: stdioConfig.command,
|
||||
args: stdioConfig.args,
|
||||
}
|
||||
}
|
||||
const serverTools =
|
||||
@@ -1688,7 +1691,7 @@ function runHeadlessStreaming(
|
||||
}
|
||||
return {
|
||||
name: connection.name,
|
||||
status: connection.type,
|
||||
status: connection.type as McpServerStatus['status'],
|
||||
serverInfo:
|
||||
connection.type === 'connected' ? connection.serverInfo : undefined,
|
||||
error: connection.type === 'failed' ? connection.error : undefined,
|
||||
@@ -1697,7 +1700,7 @@ function runHeadlessStreaming(
|
||||
tools: serverTools,
|
||||
capabilities,
|
||||
}
|
||||
})
|
||||
}) as McpServerStatus[]
|
||||
}
|
||||
|
||||
// NOTE: Nested function required - needs closure access to applyMcpServerChanges and updateSdkMcp
|
||||
@@ -1802,12 +1805,12 @@ function runHeadlessStreaming(
|
||||
type === 'http' ||
|
||||
type === 'sdk'
|
||||
) {
|
||||
supportedConfigs[name] = config
|
||||
supportedConfigs[name] = config as McpServerConfigForProcessTransport
|
||||
}
|
||||
}
|
||||
for (const [name, config] of Object.entries(sdkMcpConfigs)) {
|
||||
if (config.type === 'sdk' && !(name in supportedConfigs)) {
|
||||
supportedConfigs[name] = config
|
||||
supportedConfigs[name] = config as unknown as McpServerConfigForProcessTransport
|
||||
}
|
||||
}
|
||||
const { response, sdkServersChanged } =
|
||||
@@ -1971,10 +1974,11 @@ function runHeadlessStreaming(
|
||||
if (c.uuid && c.uuid !== command.uuid) {
|
||||
output.enqueue({
|
||||
type: 'user',
|
||||
content: c.value,
|
||||
message: { role: 'user', content: c.value },
|
||||
session_id: getSessionId(),
|
||||
parent_tool_use_id: null,
|
||||
uuid: c.uuid,
|
||||
uuid: c.uuid as string,
|
||||
isReplay: true,
|
||||
} satisfies SDKUserMessageReplay)
|
||||
}
|
||||
@@ -2255,14 +2259,14 @@ function runHeadlessStreaming(
|
||||
|
||||
if (feature('FILE_PERSISTENCE') && turnStartTime !== undefined) {
|
||||
void executeFilePersistence(
|
||||
turnStartTime,
|
||||
{ turnStartTime } as import('src/utils/filePersistence/types.js').TurnStartTime,
|
||||
abortController.signal,
|
||||
result => {
|
||||
output.enqueue({
|
||||
type: 'system' as const,
|
||||
subtype: 'files_persisted' as const,
|
||||
files: result.files,
|
||||
failed: result.failed,
|
||||
files: result.persistedFiles,
|
||||
failed: result.failedFiles,
|
||||
processed_at: new Date().toISOString(),
|
||||
uuid: randomUUID(),
|
||||
session_id: getSessionId(),
|
||||
@@ -3005,7 +3009,7 @@ function runHeadlessStreaming(
|
||||
} else {
|
||||
sendControlResponseError(
|
||||
message,
|
||||
result.error ?? 'Unexpected error',
|
||||
(result.error as string) ?? 'Unexpected error',
|
||||
)
|
||||
}
|
||||
} else if (message.request.subtype === 'cancel_async_message') {
|
||||
@@ -4077,13 +4081,14 @@ function runHeadlessStreaming(
|
||||
)
|
||||
output.enqueue({
|
||||
type: 'user',
|
||||
content: message.message?.content ?? '',
|
||||
message: message.message,
|
||||
session_id: sessionId,
|
||||
parent_tool_use_id: null,
|
||||
uuid: message.uuid,
|
||||
timestamp: message.timestamp,
|
||||
isReplay: true,
|
||||
} as SDKUserMessageReplay)
|
||||
} as unknown as SDKUserMessageReplay)
|
||||
}
|
||||
// Historical dup = transcript already has this turn's output, so it
|
||||
// ran but its lifecycle was never closed (interrupted before ack).
|
||||
@@ -4434,7 +4439,7 @@ async function handleInitializeRequest(
|
||||
const accountInfo = getAccountInformation()
|
||||
if (request.hooks) {
|
||||
const hooks: Partial<Record<HookEvent, HookCallbackMatcher[]>> = {}
|
||||
for (const [event, matchers] of Object.entries(request.hooks)) {
|
||||
for (const [event, matchers] of Object.entries(request.hooks) as [string, Array<{ hookCallbackIds: string[]; timeout?: number; matcher?: string }>][]) {
|
||||
hooks[event as HookEvent] = matchers.map(matcher => {
|
||||
const callbacks = matcher.hookCallbackIds.map(callbackId => {
|
||||
return structuredIO.createHookCallback(callbackId, matcher.timeout)
|
||||
@@ -4524,12 +4529,13 @@ async function handleRewindFiles(
|
||||
dryRun: boolean,
|
||||
): Promise<RewindFilesResult> {
|
||||
if (!fileHistoryEnabled()) {
|
||||
return { canRewind: false, error: 'File rewinding is not enabled.' }
|
||||
return { canRewind: false, error: 'File rewinding is not enabled.', filesChanged: [] }
|
||||
}
|
||||
if (!fileHistoryCanRestore(appState.fileHistory, userMessageId)) {
|
||||
return {
|
||||
canRewind: false,
|
||||
error: 'No file checkpoint found for this message.',
|
||||
filesChanged: [],
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4559,10 +4565,11 @@ async function handleRewindFiles(
|
||||
return {
|
||||
canRewind: false,
|
||||
error: `Failed to rewind: ${errorMessage(error)}`,
|
||||
filesChanged: [],
|
||||
}
|
||||
}
|
||||
|
||||
return { canRewind: true }
|
||||
return { canRewind: true, filesChanged: [] }
|
||||
}
|
||||
|
||||
function handleSetPermissionMode(
|
||||
@@ -4751,7 +4758,7 @@ function handleChannelEnable(
|
||||
value: wrapChannelMessage(serverName, content, meta),
|
||||
priority: 'next',
|
||||
isMeta: true,
|
||||
origin: { kind: 'channel', server: serverName },
|
||||
origin: { kind: 'channel', server: serverName } as unknown as string,
|
||||
skipSlashCommands: true,
|
||||
})
|
||||
},
|
||||
@@ -4827,7 +4834,7 @@ function reregisterChannelHandlerAfterReconnect(
|
||||
value: wrapChannelMessage(connection.name, content, meta),
|
||||
priority: 'next',
|
||||
isMeta: true,
|
||||
origin: { kind: 'channel', server: connection.name },
|
||||
origin: { kind: 'channel', server: connection.name } as unknown as string,
|
||||
skipSlashCommands: true,
|
||||
})
|
||||
},
|
||||
@@ -5210,6 +5217,8 @@ function getStructuredIO(
|
||||
inputStream = fromArray([
|
||||
jsonStringify({
|
||||
type: 'user',
|
||||
content: inputPrompt,
|
||||
uuid: '',
|
||||
session_id: '',
|
||||
message: {
|
||||
role: 'user',
|
||||
@@ -5249,19 +5258,20 @@ export async function handleOrphanedPermissionResponse({
|
||||
onEnqueued?: () => void
|
||||
handledToolUseIds: Set<string>
|
||||
}): Promise<boolean> {
|
||||
const responseInner = message.response as { subtype?: string; response?: Record<string, unknown>; request_id?: string } | undefined
|
||||
if (
|
||||
message.response.subtype === 'success' &&
|
||||
message.response.response?.toolUseID &&
|
||||
typeof message.response.response.toolUseID === 'string'
|
||||
responseInner?.subtype === 'success' &&
|
||||
responseInner.response?.toolUseID &&
|
||||
typeof responseInner.response.toolUseID === 'string'
|
||||
) {
|
||||
const permissionResult = message.response.response as PermissionResult
|
||||
const { toolUseID } = permissionResult
|
||||
const permissionResult = responseInner.response as PermissionResult & { toolUseID?: string }
|
||||
const toolUseID = permissionResult.toolUseID
|
||||
if (!toolUseID) {
|
||||
return false
|
||||
}
|
||||
|
||||
logForDebugging(
|
||||
`handleOrphanedPermissionResponse: received orphaned control_response for toolUseID=${toolUseID} request_id=${message.response.request_id}`,
|
||||
`handleOrphanedPermissionResponse: received orphaned control_response for toolUseID=${toolUseID} request_id=${responseInner.request_id}`,
|
||||
)
|
||||
|
||||
// Prevent re-processing the same orphaned tool_use. Without this guard,
|
||||
@@ -5373,8 +5383,8 @@ export async function handleMcpSetServers(
|
||||
const processServers: Record<string, McpServerConfigForProcessTransport> = {}
|
||||
|
||||
for (const [name, config] of Object.entries(allowedServers)) {
|
||||
if (config.type === 'sdk') {
|
||||
sdkServers[name] = config
|
||||
if ((config.type as string) === 'sdk') {
|
||||
sdkServers[name] = config as unknown as McpSdkServerConfig
|
||||
} else {
|
||||
processServers[name] = config
|
||||
}
|
||||
@@ -5515,7 +5525,7 @@ export async function reconcileMcpServers(
|
||||
|
||||
// SDK servers are managed by the SDK process, not the CLI.
|
||||
// Just track them without trying to connect.
|
||||
if (config.type === 'sdk') {
|
||||
if ((config.type as string) === 'sdk') {
|
||||
added.push(name)
|
||||
continue
|
||||
}
|
||||
|
||||
@@ -1,2 +1,2 @@
|
||||
// Auto-generated stub
|
||||
export {};
|
||||
export async function rollback(target?: string, options?: { list?: boolean; dryRun?: boolean; safe?: boolean }): Promise<void> {}
|
||||
|
||||
@@ -8,7 +8,7 @@ import type { AssistantMessage } from 'src//types/message.js'
|
||||
import type {
|
||||
HookInput,
|
||||
HookJSONOutput,
|
||||
PermissionUpdate,
|
||||
PermissionUpdate as SDKPermissionUpdate,
|
||||
SDKMessage,
|
||||
SDKUserMessage,
|
||||
} from 'src/entrypoints/agentSdkTypes.js'
|
||||
@@ -19,6 +19,7 @@ import type {
|
||||
StdinMessage,
|
||||
StdoutMessage,
|
||||
} from 'src/entrypoints/sdk/controlTypes.js'
|
||||
import type { PermissionUpdate as InternalPermissionUpdate } from 'src/types/permissions.js'
|
||||
import type { CanUseToolFn } from 'src/hooks/useCanUseTool.js'
|
||||
import type { Tool, ToolUseContext } from 'src/Tool.js'
|
||||
import { type HookCallback, hookJSONOutputSchema } from 'src/types/hooks.js'
|
||||
@@ -174,8 +175,9 @@ export class StructuredIO {
|
||||
* messages for the same tool are ignored by the orphan handler.
|
||||
*/
|
||||
private trackResolvedToolUseId(request: SDKControlRequest): void {
|
||||
if (request.request.subtype === 'can_use_tool') {
|
||||
this.resolvedToolUseIds.add(request.request.tool_use_id)
|
||||
const inner = request.request as { subtype?: string; tool_use_id?: string }
|
||||
if (inner.subtype === 'can_use_tool') {
|
||||
this.resolvedToolUseIds.add(inner.tool_use_id as string)
|
||||
if (this.resolvedToolUseIds.size > MAX_RESOLVED_TOOL_USE_IDS) {
|
||||
// Evict the oldest entry (Sets iterate in insertion order)
|
||||
const first = this.resolvedToolUseIds.values().next().value
|
||||
@@ -205,6 +207,8 @@ export class StructuredIO {
|
||||
this.prependedLines.push(
|
||||
jsonStringify({
|
||||
type: 'user',
|
||||
content,
|
||||
uuid: '',
|
||||
session_id: '',
|
||||
message: { role: 'user', content },
|
||||
parent_tool_use_id: null,
|
||||
@@ -263,7 +267,7 @@ export class StructuredIO {
|
||||
getPendingPermissionRequests() {
|
||||
return Array.from(this.pendingRequests.values())
|
||||
.map(entry => entry.request)
|
||||
.filter(pr => pr.request.subtype === 'can_use_tool')
|
||||
.filter(pr => (pr.request as { subtype?: string }).subtype === 'can_use_tool')
|
||||
}
|
||||
|
||||
setUnexpectedResponseCallback(
|
||||
@@ -281,21 +285,22 @@ export class StructuredIO {
|
||||
* callback is aborted via the signal — otherwise the callback hangs.
|
||||
*/
|
||||
injectControlResponse(response: SDKControlResponse): void {
|
||||
const requestId = response.response?.request_id
|
||||
const responseInner = response.response as { request_id?: string; subtype?: string; error?: string; response?: unknown } | undefined
|
||||
const requestId = responseInner?.request_id
|
||||
if (!requestId) return
|
||||
const request = this.pendingRequests.get(requestId)
|
||||
const request = this.pendingRequests.get(requestId as string)
|
||||
if (!request) return
|
||||
this.trackResolvedToolUseId(request.request)
|
||||
this.pendingRequests.delete(requestId)
|
||||
this.pendingRequests.delete(requestId as string)
|
||||
// Cancel the SDK consumer's canUseTool callback — the bridge won.
|
||||
void this.write({
|
||||
type: 'control_cancel_request',
|
||||
request_id: requestId,
|
||||
})
|
||||
if (response.response.subtype === 'error') {
|
||||
request.reject(new Error(response.response.error))
|
||||
if (responseInner.subtype === 'error') {
|
||||
request.reject(new Error(responseInner.error as string))
|
||||
} else {
|
||||
const result = response.response.response
|
||||
const result = responseInner.response
|
||||
if (request.schema) {
|
||||
try {
|
||||
request.resolve(request.schema.parse(result))
|
||||
@@ -350,8 +355,9 @@ export class StructuredIO {
|
||||
// Used by bridge session runner for auth token refresh
|
||||
// (CLAUDE_CODE_SESSION_ACCESS_TOKEN) which must be readable
|
||||
// by the REPL process itself, not just child Bash commands.
|
||||
const keys = Object.keys(message.variables)
|
||||
for (const [key, value] of Object.entries(message.variables)) {
|
||||
const variables = message.variables as Record<string, string>
|
||||
const keys = Object.keys(variables)
|
||||
for (const [key, value] of Object.entries(variables)) {
|
||||
process.env[key] = value
|
||||
}
|
||||
logForDebugging(
|
||||
@@ -402,7 +408,7 @@ export class StructuredIO {
|
||||
// Notify the bridge when the SDK consumer resolves a can_use_tool
|
||||
// request, so it can cancel the stale permission prompt on claude.ai.
|
||||
if (
|
||||
request.request.request.subtype === 'can_use_tool' &&
|
||||
(request.request.request as { subtype?: string }).subtype === 'can_use_tool' &&
|
||||
this.onControlRequestResolved
|
||||
) {
|
||||
this.onControlRequestResolved(message.response.request_id)
|
||||
@@ -484,7 +490,7 @@ export class StructuredIO {
|
||||
throw new Error('Request aborted')
|
||||
}
|
||||
this.outbound.enqueue(message)
|
||||
if (request.subtype === 'can_use_tool' && this.onControlRequestSent) {
|
||||
if ((request as { subtype?: string }).subtype === 'can_use_tool' && this.onControlRequestSent) {
|
||||
this.onControlRequestSent(message)
|
||||
}
|
||||
const aborted = () => {
|
||||
@@ -789,7 +795,7 @@ async function executePermissionRequestHooksForSDK(
|
||||
toolUseID: string,
|
||||
input: Record<string, unknown>,
|
||||
toolUseContext: ToolUseContext,
|
||||
suggestions: PermissionUpdate[] | undefined,
|
||||
suggestions: InternalPermissionUpdate[] | undefined,
|
||||
): Promise<PermissionDecision | undefined> {
|
||||
const appState = toolUseContext.getAppState()
|
||||
const permissionMode = appState.toolPermissionContext.mode
|
||||
@@ -801,7 +807,7 @@ async function executePermissionRequestHooksForSDK(
|
||||
input,
|
||||
toolUseContext,
|
||||
permissionMode,
|
||||
suggestions,
|
||||
suggestions as unknown as SDKPermissionUpdate[] | undefined,
|
||||
toolUseContext.abortController.signal,
|
||||
)
|
||||
|
||||
@@ -816,7 +822,7 @@ async function executePermissionRequestHooksForSDK(
|
||||
const finalInput = decision.updatedInput || input
|
||||
|
||||
// Apply permission updates if provided by hook ("always allow")
|
||||
const permissionUpdates = decision.updatedPermissions ?? []
|
||||
const permissionUpdates = (decision.updatedPermissions ?? []) as unknown as InternalPermissionUpdate[]
|
||||
if (permissionUpdates.length > 0) {
|
||||
persistPermissionUpdates(permissionUpdates)
|
||||
const currentAppState = toolUseContext.getAppState()
|
||||
|
||||
@@ -1,2 +1,2 @@
|
||||
// Auto-generated stub
|
||||
export {};
|
||||
export async function up(): Promise<void> {}
|
||||
|
||||
Reference in New Issue
Block a user