fix: remove final planned as-any casts
This commit is contained in:
parent
ac807aeb08
commit
abde5e813a
|
|
@ -909,12 +909,12 @@ async function callInner(
|
||||||
resolvedFilePath,
|
resolvedFilePath,
|
||||||
parsedRange ?? undefined,
|
parsedRange ?? undefined,
|
||||||
)
|
)
|
||||||
if (!extractResult.success) {
|
if (extractResult.success === false) {
|
||||||
throw new Error((extractResult as any).error.message)
|
throw new Error(extractResult.error.message)
|
||||||
}
|
}
|
||||||
logEvent('tengu_pdf_page_extraction', {
|
logEvent('tengu_pdf_page_extraction', {
|
||||||
success: true,
|
success: true,
|
||||||
pageCount: (extractResult as any).data.file.count,
|
pageCount: extractResult.data.file.count,
|
||||||
fileSize: extractResult.data.file.originalSize,
|
fileSize: extractResult.data.file.originalSize,
|
||||||
hasPageRange: true,
|
hasPageRange: true,
|
||||||
})
|
})
|
||||||
|
|
@ -981,7 +981,9 @@ async function callInner(
|
||||||
} else {
|
} else {
|
||||||
logEvent('tengu_pdf_page_extraction', {
|
logEvent('tengu_pdf_page_extraction', {
|
||||||
success: false,
|
success: false,
|
||||||
available: (extractResult as any).error.reason !== 'unavailable',
|
available:
|
||||||
|
extractResult.success === false &&
|
||||||
|
extractResult.error.reason !== 'unavailable',
|
||||||
fileSize: stats.size,
|
fileSize: stats.size,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
@ -996,8 +998,8 @@ async function callInner(
|
||||||
}
|
}
|
||||||
|
|
||||||
const readResult = await readPDF(resolvedFilePath)
|
const readResult = await readPDF(resolvedFilePath)
|
||||||
if (!readResult.success) {
|
if (readResult.success === false) {
|
||||||
throw new Error((readResult as any).error.message)
|
throw new Error(readResult.error.message)
|
||||||
}
|
}
|
||||||
const pdfData = readResult.data
|
const pdfData = readResult.data
|
||||||
logFileOperation({
|
logFileOperation({
|
||||||
|
|
@ -1167,14 +1169,27 @@ export async function readImageWithTokenBudget(
|
||||||
// Fallback: heavily compressed version from the SAME buffer
|
// Fallback: heavily compressed version from the SAME buffer
|
||||||
try {
|
try {
|
||||||
const sharpModule = await import('sharp')
|
const sharpModule = await import('sharp')
|
||||||
|
type SharpFactory = (
|
||||||
|
input: Buffer,
|
||||||
|
) => {
|
||||||
|
resize(
|
||||||
|
width: number,
|
||||||
|
height: number,
|
||||||
|
options: {
|
||||||
|
fit: 'inside'
|
||||||
|
withoutEnlargement: boolean
|
||||||
|
},
|
||||||
|
): {
|
||||||
|
jpeg(options: { quality: number }): {
|
||||||
|
toBuffer(): Promise<Buffer>
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
const sharp =
|
const sharp =
|
||||||
(
|
((sharpModule as unknown as { default?: unknown }).default ??
|
||||||
sharpModule as unknown as {
|
sharpModule) as SharpFactory
|
||||||
default?: typeof sharpModule
|
|
||||||
} & typeof sharpModule
|
|
||||||
).default || sharpModule
|
|
||||||
|
|
||||||
const fallbackBuffer = await (sharp as any)(imageBuffer)
|
const fallbackBuffer = await sharp(imageBuffer)
|
||||||
.resize(400, 400, {
|
.resize(400, 400, {
|
||||||
fit: 'inside',
|
fit: 'inside',
|
||||||
withoutEnlargement: true,
|
withoutEnlargement: true,
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,8 @@
|
||||||
import { type Options as ExecaOptions, execaSync } from 'execa'
|
import {
|
||||||
|
type Options as ExecaOptions,
|
||||||
|
type ExecaSyncMethod,
|
||||||
|
execaSync,
|
||||||
|
} from 'execa'
|
||||||
import { getCwd } from '../utils/cwd.js'
|
import { getCwd } from '../utils/cwd.js'
|
||||||
import { slowLogging } from './slowOperations.js'
|
import { slowLogging } from './slowOperations.js'
|
||||||
|
|
||||||
|
|
@ -12,6 +16,11 @@ type ExecSyncOptions = {
|
||||||
stdio?: ExecaOptions['stdio']
|
stdio?: ExecaOptions['stdio']
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type ShellCommandExecaSync = (
|
||||||
|
command: string,
|
||||||
|
options: ExecaOptions,
|
||||||
|
) => ReturnType<ExecaSyncMethod>
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @deprecated Use `execa` directly with `{ shell: true, reject: false }` for non-blocking execution.
|
* @deprecated Use `execa` directly with `{ shell: true, reject: false }` for non-blocking execution.
|
||||||
* Sync exec calls block the event loop and cause performance issues.
|
* Sync exec calls block the event loop and cause performance issues.
|
||||||
|
|
@ -72,7 +81,8 @@ export function execSyncWithDefaults_DEPRECATED(
|
||||||
// DEPRECATED: caller is responsible for sanitizing shell metacharacters
|
// DEPRECATED: caller is responsible for sanitizing shell metacharacters
|
||||||
// in the command argument. This function uses shell: true which can lead
|
// in the command argument. This function uses shell: true which can lead
|
||||||
// to command injection if command contains untrusted input.
|
// to command injection if command contains untrusted input.
|
||||||
const result = (execaSync as any)(command, {
|
const runShellCommand = execaSync as unknown as ShellCommandExecaSync
|
||||||
|
const result = runShellCommand(command, {
|
||||||
env: process.env,
|
env: process.env,
|
||||||
maxBuffer: 1_000_000,
|
maxBuffer: 1_000_000,
|
||||||
timeout: finalTimeout,
|
timeout: finalTimeout,
|
||||||
|
|
@ -85,7 +95,15 @@ export function execSyncWithDefaults_DEPRECATED(
|
||||||
if (!result.stdout) {
|
if (!result.stdout) {
|
||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
return result.stdout.trim() || null
|
const stdout =
|
||||||
|
typeof result.stdout === 'string'
|
||||||
|
? result.stdout
|
||||||
|
: result.stdout instanceof Uint8Array
|
||||||
|
? new TextDecoder().decode(result.stdout)
|
||||||
|
: Array.isArray(result.stdout)
|
||||||
|
? result.stdout.join('')
|
||||||
|
: String(result.stdout)
|
||||||
|
return stdout.trim() || null
|
||||||
} catch {
|
} catch {
|
||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,6 @@
|
||||||
import type Anthropic from '@anthropic-ai/sdk'
|
import type Anthropic from '@anthropic-ai/sdk'
|
||||||
import type { BetaToolUnion } from '@anthropic-ai/sdk/resources/beta/messages.js'
|
import type { BetaToolUnion } from '@anthropic-ai/sdk/resources/beta/messages.js'
|
||||||
|
import type { ChatCompletionCreateParamsNonStreaming } from 'openai/resources/chat/completions'
|
||||||
import {
|
import {
|
||||||
getLastApiCompletionTimestamp,
|
getLastApiCompletionTimestamp,
|
||||||
getSessionId,
|
getSessionId,
|
||||||
|
|
@ -210,7 +211,7 @@ async function sideQueryViaOpenAICompatible(
|
||||||
|
|
||||||
const start = Date.now()
|
const start = Date.now()
|
||||||
|
|
||||||
const requestParams: Record<string, unknown> = {
|
const requestParams: ChatCompletionCreateParamsNonStreaming = {
|
||||||
model: openaiModel,
|
model: openaiModel,
|
||||||
messages: openaiMessages,
|
messages: openaiMessages,
|
||||||
max_tokens,
|
max_tokens,
|
||||||
|
|
@ -218,10 +219,13 @@ async function sideQueryViaOpenAICompatible(
|
||||||
if (temperature !== undefined) requestParams.temperature = temperature
|
if (temperature !== undefined) requestParams.temperature = temperature
|
||||||
if (openaiTools && openaiTools.length > 0) {
|
if (openaiTools && openaiTools.length > 0) {
|
||||||
requestParams.tools = openaiTools
|
requestParams.tools = openaiTools
|
||||||
if (openaiToolChoice) requestParams.tool_choice = openaiToolChoice
|
if (openaiToolChoice) {
|
||||||
|
requestParams.tool_choice =
|
||||||
|
openaiToolChoice as ChatCompletionCreateParamsNonStreaming['tool_choice']
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const response = await client.chat.completions.create(requestParams as any, {
|
const response = await client.chat.completions.create(requestParams, {
|
||||||
signal,
|
signal,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,7 @@
|
||||||
// teleportToRemote's CreateSession events array.
|
// teleportToRemote's CreateSession events array.
|
||||||
|
|
||||||
import type {
|
import type {
|
||||||
|
ContentBlock,
|
||||||
ToolResultBlockParam,
|
ToolResultBlockParam,
|
||||||
ToolUseBlock,
|
ToolUseBlock,
|
||||||
} from '@anthropic-ai/sdk/resources'
|
} from '@anthropic-ai/sdk/resources'
|
||||||
|
|
@ -23,6 +24,27 @@ const POLL_INTERVAL_MS = 3000
|
||||||
// at any nonzero 5xx rate one blip would kill the run.
|
// at any nonzero 5xx rate one blip would kill the run.
|
||||||
const MAX_CONSECUTIVE_FAILURES = 5
|
const MAX_CONSECUTIVE_FAILURES = 5
|
||||||
|
|
||||||
|
function getSDKMessageContent(message: SDKMessage): unknown {
|
||||||
|
const sdkMessage = message.message
|
||||||
|
if (typeof sdkMessage !== 'object' || sdkMessage === null) return undefined
|
||||||
|
return (sdkMessage as { content?: unknown }).content
|
||||||
|
}
|
||||||
|
|
||||||
|
function isToolUseBlock(block: ContentBlock): block is ToolUseBlock {
|
||||||
|
return block.type === 'tool_use'
|
||||||
|
}
|
||||||
|
|
||||||
|
function isToolResultBlock(block: unknown): block is ToolResultBlockParam {
|
||||||
|
return (
|
||||||
|
typeof block === 'object' &&
|
||||||
|
block !== null &&
|
||||||
|
'type' in block &&
|
||||||
|
block.type === 'tool_result' &&
|
||||||
|
'tool_use_id' in block &&
|
||||||
|
typeof block.tool_use_id === 'string'
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
export type PollFailReason =
|
export type PollFailReason =
|
||||||
| 'terminated'
|
| 'terminated'
|
||||||
| 'timeout_pending'
|
| 'timeout_pending'
|
||||||
|
|
@ -101,18 +123,19 @@ export class ExitPlanModeScanner {
|
||||||
ingest(newEvents: SDKMessage[]): ScanResult {
|
ingest(newEvents: SDKMessage[]): ScanResult {
|
||||||
for (const m of newEvents) {
|
for (const m of newEvents) {
|
||||||
if (m.type === 'assistant') {
|
if (m.type === 'assistant') {
|
||||||
for (const block of (m as any).message.content) {
|
const content = getSDKMessageContent(m)
|
||||||
if (block.type !== 'tool_use') continue
|
if (!Array.isArray(content)) continue
|
||||||
const tu = block as ToolUseBlock
|
for (const block of content as ContentBlock[]) {
|
||||||
if (tu.name === EXIT_PLAN_MODE_V2_TOOL_NAME) {
|
if (!isToolUseBlock(block)) continue
|
||||||
this.exitPlanCalls.push(tu.id)
|
if (block.name === EXIT_PLAN_MODE_V2_TOOL_NAME) {
|
||||||
|
this.exitPlanCalls.push(block.id)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if (m.type === 'user') {
|
} else if (m.type === 'user') {
|
||||||
const content = (m as any).message.content
|
const content = getSDKMessageContent(m)
|
||||||
if (!Array.isArray(content)) continue
|
if (!Array.isArray(content)) continue
|
||||||
for (const block of content) {
|
for (const block of content) {
|
||||||
if (block.type === 'tool_result') {
|
if (isToolResultBlock(block)) {
|
||||||
this.results.set(block.tool_use_id, block)
|
this.results.set(block.tool_use_id, block)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user