fix: use dynamic import helper for ESM-only claude-agent-sdk

tsc with module=commonjs converts `await import()` to require(),
which breaks ESM-only packages. Use Function('return import()')
workaround to preserve native dynamic import at runtime.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
hailin 2026-02-23 06:18:04 -08:00
parent b963b7d4da
commit 14e8d7019a
1 changed files with 7 additions and 3 deletions

View File

@ -26,6 +26,10 @@ import { AllowedToolsResolverService } from '../../../domain/services/allowed-to
import { TenantContextService } from '@it0/common'; import { TenantContextService } from '@it0/common';
import { ApprovalGate } from './approval-gate'; import { ApprovalGate } from './approval-gate';
// Dynamic import helper that survives tsc commonjs compilation
// (tsc converts `await import()` → require() which breaks ESM-only packages)
const dynamicImport = new Function('specifier', 'return import(specifier)') as (specifier: string) => Promise<any>;
/** Tracks an active SDK session for cancellation and approval resolution. */ /** Tracks an active SDK session for cancellation and approval resolution. */
interface ActiveSession { interface ActiveSession {
abort: AbortController; abort: AbortController;
@ -92,7 +96,7 @@ export class ClaudeAgentSdkEngine implements AgentEnginePort {
}; };
try { try {
const { query } = await import('@anthropic-ai/claude-agent-sdk'); const { query } = await dynamicImport('@anthropic-ai/claude-agent-sdk');
const sdkQuery = query({ const sdkQuery = query({
prompt: params.prompt, prompt: params.prompt,
@ -259,7 +263,7 @@ export class ClaudeAgentSdkEngine implements AgentEnginePort {
this.activeSessions.set(sessionId, { abort: abortController, gate, sdkSessionId }); this.activeSessions.set(sessionId, { abort: abortController, gate, sdkSessionId });
try { try {
const { query } = await import('@anthropic-ai/claude-agent-sdk'); const { query } = await dynamicImport('@anthropic-ai/claude-agent-sdk');
const sdkQuery = query({ const sdkQuery = query({
prompt: message, prompt: message,
@ -303,7 +307,7 @@ export class ClaudeAgentSdkEngine implements AgentEnginePort {
async healthCheck(): Promise<boolean> { async healthCheck(): Promise<boolean> {
try { try {
await import('@anthropic-ai/claude-agent-sdk'); await dynamicImport('@anthropic-ai/claude-agent-sdk');
return true; return true;
} catch { } catch {
return false; return false;