fix: resolve confirmation (#2161)

This commit is contained in:
Yingjie He
2025-04-11 13:58:54 -07:00
committed by GitHub
parent 454e4a47f4
commit 79e65ec3de
10 changed files with 160 additions and 70 deletions
+59
View File
@@ -351,6 +351,40 @@
}
}
}
},
"/confirm": {
"post": {
"tags": [
"super::routes::reply"
],
"operationId": "confirm_permission",
"requestBody": {
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/PermissionConfirmationRequest"
}
}
},
"required": true
},
"responses": {
"200": {
"description": "Permission action is confirmed",
"content": {
"application/json": {
"schema": {}
}
}
},
"401": {
"description": "Unauthorized - invalid secret key"
},
"500": {
"description": "Internal server error"
}
}
}
}
},
"components": {
@@ -635,6 +669,24 @@
}
}
},
"PermissionConfirmationRequest": {
"type": "object",
"required": [
"id",
"action"
],
"properties": {
"action": {
"type": "string"
},
"id": {
"type": "string"
},
"principal_type": {
"$ref": "#/components/schemas/PrincipalType"
}
}
},
"PermissionLevel": {
"type": "string",
"description": "Enum representing the possible permission levels for a tool.",
@@ -644,6 +696,13 @@
"never_allow"
]
},
"PrincipalType": {
"type": "string",
"enum": [
"Extension",
"Tool"
]
},
"ProviderDetails": {
"type": "object",
"required": [
+12 -1
View File
@@ -1,7 +1,7 @@
// This file is auto-generated by @hey-api/openapi-ts
import type { Options as ClientOptions, TDataShape, Client } from '@hey-api/client-fetch';
import type { GetToolsData, GetToolsResponse, ReadAllConfigData, ReadAllConfigResponse, GetExtensionsData, GetExtensionsResponse, AddExtensionData, AddExtensionResponse, RemoveExtensionData, RemoveExtensionResponse, InitConfigData, InitConfigResponse, UpsertPermissionsData, UpsertPermissionsResponse, ProvidersData, ProvidersResponse2, ReadConfigData, RemoveConfigData, RemoveConfigResponse, UpsertConfigData, UpsertConfigResponse } from './types.gen';
import type { GetToolsData, GetToolsResponse, ReadAllConfigData, ReadAllConfigResponse, GetExtensionsData, GetExtensionsResponse, AddExtensionData, AddExtensionResponse, RemoveExtensionData, RemoveExtensionResponse, InitConfigData, InitConfigResponse, UpsertPermissionsData, UpsertPermissionsResponse, ProvidersData, ProvidersResponse2, ReadConfigData, RemoveConfigData, RemoveConfigResponse, UpsertConfigData, UpsertConfigResponse, ConfirmPermissionData } from './types.gen';
import { client as _heyApiClient } from './client.gen';
export type Options<TData extends TDataShape = TDataShape, ThrowOnError extends boolean = boolean> = ClientOptions<TData, ThrowOnError> & {
@@ -113,4 +113,15 @@ export const upsertConfig = <ThrowOnError extends boolean = false>(options: Opti
...options?.headers
}
});
};
export const confirmPermission = <ThrowOnError extends boolean = false>(options: Options<ConfirmPermissionData, ThrowOnError>) => {
return (options.client ?? _heyApiClient).post<unknown, unknown, ThrowOnError>({
url: '/confirm',
...options,
headers: {
'Content-Type': 'application/json',
...options?.headers
}
});
};
+33
View File
@@ -100,11 +100,19 @@ export type ExtensionResponse = {
extensions: Array<ExtensionEntry>;
};
export type PermissionConfirmationRequest = {
action: string;
id: string;
principal_type?: PrincipalType;
};
/**
* Enum representing the possible permission levels for a tool.
*/
export type PermissionLevel = 'always_allow' | 'ask_before' | 'never_allow';
export type PrincipalType = 'Extension' | 'Tool';
export type ProviderDetails = {
/**
* Indicates whether the provider is fully configured
@@ -521,6 +529,31 @@ export type UpsertConfigResponses = {
export type UpsertConfigResponse = UpsertConfigResponses[keyof UpsertConfigResponses];
export type ConfirmPermissionData = {
body: PermissionConfirmationRequest;
path?: never;
query?: never;
url: '/confirm';
};
export type ConfirmPermissionErrors = {
/**
* Unauthorized - invalid secret key
*/
401: unknown;
/**
* Internal server error
*/
500: unknown;
};
export type ConfirmPermissionResponses = {
/**
* Permission action is confirmed
*/
200: unknown;
};
export type ClientOptions = {
baseUrl: `${string}://${string}` | (string & {});
};
@@ -1,20 +1,39 @@
import React, { useState } from 'react';
import { ConfirmExtensionRequest } from '../utils/extensionConfirm';
import { snakeToTitleCase } from '../utils';
import { confirmPermission } from '../api';
interface ExtensionConfirmationProps {
isCancelledMessage: boolean;
isClicked: boolean;
extensionConfirmationId: string;
extensionName: string;
}
export default function ExtensionConfirmation({
isCancelledMessage,
isClicked,
extensionConfirmationId,
extensionName,
}) {
}: ExtensionConfirmationProps) {
const [clicked, setClicked] = useState(isClicked);
const [status, setStatus] = useState('unknown');
const handleButtonClick = (confirmed) => {
const handleButtonClick = async (confirmed: boolean) => {
setClicked(true);
setStatus(confirmed ? 'approved' : 'denied');
ConfirmExtensionRequest(extensionConfirmationId, confirmed);
try {
const response = await confirmPermission({
body: {
id: extensionConfirmationId,
action: confirmed ? 'allow_once' : 'deny',
principal_type: 'Extension',
},
});
if (response.error) {
console.error('Failed to confirm permission: ', response.error);
}
} catch (err) {
console.error('Error fetching tools:', err);
}
};
return isCancelledMessage ? (
@@ -1,8 +1,8 @@
import { useState } from 'react';
import { ConfirmToolRequest } from '../utils/toolConfirm';
import { snakeToTitleCase } from '../utils';
import PermissionModal from './settings_v2/permission/PermissionModal';
import { ChevronRight } from 'lucide-react';
import { confirmPermission } from '../api';
const ALWAYS_ALLOW = 'always_allow';
const ALLOW_ONCE = 'allow_once';
@@ -26,7 +26,7 @@ export default function ToolConfirmation({
const [actionDisplay, setActionDisplay] = useState('');
const [isModalOpen, setIsModalOpen] = useState(false);
const handleButtonClick = (action: string) => {
const handleButtonClick = async (action: string) => {
setClicked(true);
setStatus(action);
if (action === ALWAYS_ALLOW) {
@@ -36,7 +36,16 @@ export default function ToolConfirmation({
} else {
setActionDisplay('denied');
}
ConfirmToolRequest(toolConfirmationId, action);
try {
const response = await confirmPermission({
body: { id: toolConfirmationId, action, principal_type: 'Tool' },
});
if (response.error) {
console.error('Failed to confirm permission: ', response.error);
}
} catch (err) {
console.error('Error fetching tools:', err);
}
};
const handleModalClose = () => {
-26
View File
@@ -1,26 +0,0 @@
import { getApiUrl, getSecretKey } from '../config';
export async function ConfirmExtensionRequest(requestId: string, confirmed: boolean) {
try {
const response = await fetch(getApiUrl('/confirm'), {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-Secret-Key': getSecretKey(),
},
body: JSON.stringify({
id: requestId,
confirmed,
principal_type: 'Extension',
}),
});
if (!response.ok) {
const errorText = await response.text();
console.error('Delete response error: ', errorText);
throw new Error('Failed to confirm extension enablement');
}
} catch (error) {
console.error('Error confirming extension enablement: ', error);
}
}
-26
View File
@@ -1,26 +0,0 @@
import { getApiUrl, getSecretKey } from '../config';
export async function ConfirmToolRequest(requesyId: string, action: string) {
try {
const response = await fetch(getApiUrl('/confirm'), {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-Secret-Key': getSecretKey(),
},
body: JSON.stringify({
id: requesyId,
action,
principal_type: 'Tool',
}),
});
if (!response.ok) {
const errorText = await response.text();
console.error('Delete response error: ', errorText);
throw new Error('Failed to confirm tool');
}
} catch (error) {
console.error('Error confirm tool: ', error);
}
}