Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion vscode-extension/openclaude-vscode/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@
]
},
"scripts": {
"test": "node --test ./src/*.test.js",
"test": "bun test src",
"lint": "node scripts/lint.js",
"package": "npx @vscode/vsce package --no-dependencies"
},
Expand Down
47 changes: 27 additions & 20 deletions vscode-extension/openclaude-vscode/src/chat/chatProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
const vscode = require('vscode');
const crypto = require('crypto');
const { ProcessManager } = require('./processManager');
const { buildPermissionControlResult } = require('./permissionResponse');
const { toViewModel } = require('./messageParser');
const { renderChatHtml } = require('./chatRenderer');
const { isAssistantMessage, isPartialMessage, isStreamEvent,
Expand Down Expand Up @@ -50,6 +51,8 @@ class ChatController {
this._thinkingTokens = 0;
this._thinkingStartTime = null;
this._currentBlockType = null;
/** @type {Map<string, { input: Record<string, unknown>, permissionSuggestions: unknown[], toolUseId: string | null }>} */
this._pendingPermissions = new Map();

this._onDidChangeState = new vscode.EventEmitter();
this.onDidChangeState = this._onDidChangeState.event;
Expand Down Expand Up @@ -142,6 +145,7 @@ class ChatController {
this._process.dispose();
this._process = null;
}
this._pendingPermissions.clear();
}

async sendMessage(text) {
Expand Down Expand Up @@ -185,26 +189,15 @@ class ChatController {

sendPermissionResponse(requestId, action, toolUseId) {
if (!this._process) return;
if (action === 'deny') {
try {
this._process.write({
type: 'control_response',
response: {
subtype: 'error',
request_id: requestId,
error: 'User denied permission',
},
});
} catch (err) {
this._broadcast({ type: 'error', message: err.message });
}
return;
}
const pending = this._pendingPermissions.get(requestId);
this._pendingPermissions.delete(requestId);
const result = buildPermissionControlResult(action, {
input: pending?.input,
toolUseId: toolUseId || pending?.toolUseId || null,
permissionSuggestions: pending?.permissionSuggestions,
});
try {
this._process.sendControlResponse(requestId, {
toolUseID: toolUseId || undefined,
...(action === 'allow-session' ? { remember: true } : {}),
});
this._process.sendControlResponse(requestId, result);
} catch (err) {
this._broadcast({ type: 'error', message: err.message });
}
Expand All @@ -231,9 +224,23 @@ class ChatController {
if (msg.type === 'control_request' || isControlRequest(msg)) {
const req = msg.request || {};
const { toolDisplayName, parseToolInput } = require('./messageParser');
const requestId = msg.request_id;
const toolInput =
req.input && typeof req.input === 'object' && !Array.isArray(req.input)
? req.input
: {};
if (requestId) {
this._pendingPermissions.set(requestId, {
input: toolInput,
permissionSuggestions: Array.isArray(req.permission_suggestions)
? req.permission_suggestions
: [],
toolUseId: req.tool_use_id || null,
});
}
this._broadcast({
type: 'permission_request',
requestId: msg.request_id,
requestId,
toolName: req.tool_name || 'Unknown',
displayName: req.display_name || req.title || toolDisplayName(req.tool_name),
description: req.description || '',
Expand Down
46 changes: 46 additions & 0 deletions vscode-extension/openclaude-vscode/src/chat/permissionResponse.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/**
* Build SDK can_use_tool control_response payloads for the VS Code chat host.
*/

/**
* @param {'allow' | 'deny' | 'allow-session'} action
* @param {{
* input?: Record<string, unknown> | null,
* toolUseId?: string | null,
* permissionSuggestions?: unknown[] | null,
* }} ctx
*/
function buildPermissionControlResult(action, ctx = {}) {
const toolUseID = ctx.toolUseId || undefined;
const input =
ctx.input && typeof ctx.input === 'object' && !Array.isArray(ctx.input)
? ctx.input
: {};

if (action === 'deny') {
return {
behavior: 'deny',
message: 'User denied permission',
toolUseID,
};
}

const result = {
behavior: 'allow',
updatedInput: input,
toolUseID,
};

if (action === 'allow-session') {
const suggestions = Array.isArray(ctx.permissionSuggestions)
? ctx.permissionSuggestions
: [];
if (suggestions.length > 0) {
result.updatedPermissions = suggestions;
}
}

return result;
}

module.exports = { buildPermissionControlResult };
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
const test = require('node:test');
const assert = require('node:assert/strict');
const { buildPermissionControlResult } = require('./permissionResponse');

test('buildPermissionControlResult allow includes behavior and updatedInput', () => {
const input = { file_path: 'C:\\project\\foo.txt', old_string: 'a', new_string: 'b' };
assert.deepEqual(buildPermissionControlResult('allow', {
input,
toolUseId: 'toolu_123',
}), {
behavior: 'allow',
updatedInput: input,
toolUseID: 'toolu_123',
});
});

test('buildPermissionControlResult allow-session attaches permission suggestions', () => {
const suggestions = [{
type: 'addRules',
rules: [{ toolName: 'Edit', ruleContent: 'C:\\project' }],
behavior: 'allow',
destination: 'session',
}];
assert.deepEqual(buildPermissionControlResult('allow-session', {
input: { file_path: 'C:\\project\\foo.txt' },
toolUseId: 'toolu_456',
permissionSuggestions: suggestions,
}), {
behavior: 'allow',
updatedInput: { file_path: 'C:\\project\\foo.txt' },
toolUseID: 'toolu_456',
updatedPermissions: suggestions,
});
});

test('buildPermissionControlResult deny uses deny behavior', () => {
assert.deepEqual(buildPermissionControlResult('deny', { toolUseId: 'toolu_789' }), {
behavior: 'deny',
message: 'User denied permission',
toolUseID: 'toolu_789',
});
});