-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Closed
Description
Bug Description:
The writeFileContent tool fails with the error Cannot read properties of undefined (reading 'id') when invoked by AI agents. This issue affects all writeFileContent calls across all tested agents.
Root Cause:
The issue is in packages/ai-ide/src/browser/file-changeset-functions.ts in the WriteFileContent class handler (around line 167-180):
handler: async (args: string, ctx: MutableChatRequestModel): Promise<string> => {
// ...
const fileElement = this.fileChangeFactory({
uri: uri,
type: type as 'modify' | 'add' | 'delete',
state: 'pending',
targetState: content,
requestId: ctx.id, // <-- ERROR: ctx is undefined
chatSessionId: ctx.session.id // <-- Also fails
});
// ...
}The handler expects ctx to be a MutableChatRequestModel with valid id and session.id properties. However:
- The
ToolRequest.handlersignature inpackages/ai-core/src/common/language-model.tsdefinesctxas optional:handler: (arg_string: string, ctx?: unknown) - When tools are invoked by language models directly (not through the chat service), the
ctxparameter may beundefined, aToolInvocationContextobject (which only hastoolCallId), or some other context type - The code does not validate that
ctxis actually aMutableChatRequestModelbefore accessing its properties
Affected Tools:
All file changeset tools in packages/ai-ide/src/browser/file-changeset-functions.ts are affected:
WriteFileContentSuggestFileContentSimpleSuggestFileReplacementsSimpleWriteFileReplacementsSuggestFileReplacements_SimpleWriteFileReplacements_SimpleClearFileChangesGetProposedFileStateSuggestFileReplacementsWriteFileReplacements
Steps to Reproduce:
- Start a Theia IDE instance with AI features enabled
- Interact with any AI agent that uses the
writeFileContenttool (e.g., ask the agent to create a new file) - Observe the error:
Error executing tool 'writeFileContent': Cannot read properties of undefined (reading 'id')
Additional Information
- Operating System: All (tested on multiple platforms)
- Theia Version: Current master
Error Message:
Error executing tool 'writeFileContent': Cannot read properties of undefined (reading 'id')
Suggested Fix:
Add proper validation for the ctx parameter before accessing its properties:
handler: async (args: string, ctx: MutableChatRequestModel): Promise<string> => {
if (ctx?.response?.cancellationToken?.isCancellationRequested) {
return JSON.stringify({ error: 'Operation cancelled by user' });
}
// Add validation for ctx
if (!ctx || !ctx.id || !ctx.session?.id) {
return JSON.stringify({
error: 'writeFileContent requires a valid chat context. This tool can only be used within a chat session.'
});
}
const { path, content } = JSON.parse(args);
// ... rest of the handler
}Related Files:
packages/ai-ide/src/browser/file-changeset-functions.ts- Tool implementationspackages/ai-core/src/common/language-model.ts-ToolRequestinterface definitionpackages/ai-chat/src/browser/change-set-file-element.ts-ChangeSetFileElementfactorypackages/ai-chat/src/common/chat-model.ts-MutableChatRequestModelclass
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels