101 lines
4.3 KiB
Markdown
101 lines
4.3 KiB
Markdown
# Phase 0 Task 2b: TypeScript Error Fixes
|
|
|
|
## Summary
|
|
|
|
Fixed all 6 TypeScript errors in `src/sdk/worker.ts` that were introduced after adding logging functionality. All logging has been preserved.
|
|
|
|
## Errors Fixed
|
|
|
|
### 1. Line 283 (now 338): Type error with AsyncIterable - missing `parent_tool_use_id` property
|
|
|
|
**Error**: Return type was `AsyncIterable<{ type: 'user'; message: { role: 'user'; content: string } }>` which didn't match the SDK's `SDKUserMessage` type.
|
|
|
|
**Fix**:
|
|
- Changed return type to `AsyncIterable<SDKUserMessage>`
|
|
- Added required `session_id` and `parent_tool_use_id: null` properties to all yielded messages
|
|
- Updated all three yield statements in the generator (initial prompt, finalize prompt, and observation prompt)
|
|
|
|
**Changes**:
|
|
- Line 338: Updated function signature
|
|
- Lines 348-356: Added `session_id` and `parent_tool_use_id` to initial prompt yield
|
|
- Lines 385-393: Added `session_id` and `parent_tool_use_id` to finalize prompt yield
|
|
- Lines 416-424: Added `session_id` and `parent_tool_use_id` to observation prompt yield
|
|
|
|
### 2. Line 289 (now removed): `onSystemInitMessage` doesn't exist in type 'Options'
|
|
|
|
**Error**: The `Options` type from the Claude Agent SDK doesn't have an `onSystemInitMessage` callback property.
|
|
|
|
**Fix**:
|
|
- Removed the invalid callback options from the `query()` call
|
|
- Changed to iterate over the returned `Query` async generator
|
|
- Handle system init messages in the iteration loop by checking message type
|
|
|
|
**Changes**:
|
|
- Lines 290-298: Removed callback options, kept valid options only
|
|
- Lines 300-312: Added iteration loop to handle system init messages
|
|
- The session ID is now captured when processing messages with `type === 'system' && subtype === 'init'`
|
|
|
|
### 3. Line 289 (now removed): Parameter 'msg' implicitly has 'any' type
|
|
|
|
**Error**: The callback parameter didn't have a type annotation.
|
|
|
|
**Fix**: This error was resolved by removing the invalid callback entirely (see fix #2).
|
|
|
|
### 4. Line 300 (now removed): Parameter 'msg' implicitly has 'any' type
|
|
|
|
**Error**: The callback parameter didn't have a type annotation.
|
|
|
|
**Fix**: This error was resolved by removing the invalid callback entirely (see fix #2).
|
|
|
|
### 5. Line 380 (now 404): Argument type error for Observation - missing `id` and `created_at_epoch`
|
|
|
|
**Error**: The `buildObservationPrompt()` function expects an `Observation` type with `id` and `created_at_epoch` properties, but the code was only passing `tool_name`, `tool_input`, and `tool_output`.
|
|
|
|
**Fix**:
|
|
- Added the missing `id: 0` (with comment explaining it's not needed for prompt generation)
|
|
- Added `created_at_epoch: Date.now()` to provide the current timestamp
|
|
|
|
**Changes**:
|
|
- Lines 404-410: Complete Observation object with all required properties
|
|
|
|
### 6. Line 527 (now 555): Property 'main' does not exist on type 'ImportMeta'
|
|
|
|
**Error**: TypeScript's default `ImportMeta` interface doesn't include Bun's custom `main` property.
|
|
|
|
**Fix**:
|
|
- Added a global type declaration to extend the `ImportMeta` interface with Bun's `main` property
|
|
- Used TypeScript's declaration merging to add the property type-safely
|
|
|
|
**Changes**:
|
|
- Lines 7-12: Added global declaration block extending `ImportMeta` with `main: boolean`
|
|
|
|
## Additional Changes
|
|
|
|
### Import Updates
|
|
- Line 17: Added import of `SDKUserMessage` and `SDKSystemMessage` types from the SDK package
|
|
|
|
### SDK Message Handling
|
|
- Lines 300-331: Refactored from callback-based approach to iteration-based approach
|
|
- Added proper message type checking and handling for both system and assistant messages
|
|
- Added content extraction logic for assistant messages (lines 316-320) to handle both array and string content types
|
|
|
|
## Verification
|
|
|
|
All TypeScript errors have been resolved:
|
|
- ✅ AsyncIterable type now matches SDK expectations
|
|
- ✅ No invalid callback options used
|
|
- ✅ All parameters have explicit types
|
|
- ✅ Observation objects have all required properties
|
|
- ✅ ImportMeta.main property is properly typed for Bun
|
|
|
|
## Logging Preservation
|
|
|
|
All logging statements have been preserved:
|
|
- ✅ All `console.error()` statements remain intact
|
|
- ✅ Debug logging for socket operations preserved
|
|
- ✅ Worker lifecycle logging preserved
|
|
- ✅ Message processing logging preserved
|
|
- ✅ SDK agent interaction logging preserved
|
|
|
|
The refactoring from callbacks to iteration actually improved logging by making the message handling flow more explicit and easier to follow.
|