mirror of
https://github.com/hanzoai/chat.git
synced 2026-09-30 20:51:17 +00:00
execute_code no longer POSTs /v1/exec behind a shared service key. It leases a Hanzo sandbox (cloud /v1/sandboxes) with the CALLER's own hanzo.id bearer, runs the program there, and reports what it printed and what it wrote. The session IS the sandbox. `session_id` is the sandbox id, the artifact directory is its workdir, an expired session is a lease the reaper ended. There is no session table here because there is no second object to keep in sync. Why the key had to be deleted rather than fixed ----------------------------------------------- A shared service key IS custom auth, and it is also why that surface could not bill: it carries no tenant, so cloud could not tell whose org a run belonged to — apps/exec's own header says so. With the user's bearer the run lands on the account that ran it, because the account is the only thing the call carries. It was never even wired: `EnvVar.CODE_API_KEY` does not exist in the shipped @hanzochat/agents, so `authFields: [undefined]` at every call site and setting the env var was a silent no-op. Deleted at all ~20 sites, plus the guard in loadAuthValues that existed only to survive the `undefined`, plus the client's key-entry path in the tool-auth contract (`fieldsMap[execute_code]` is now `[]` and verifyToolAuth never consults the credential store for it). What the tool does now ---------------------- One round trip. The program rides the exec channel's stdin, so the source is never quoted into a command line. Its stdout/stderr are redirected to files, so the command's own stdout carries only four framed lines — exit code, then base64 of out, err and the new-file list. That is unforgeable by construction: a delimiter would let a program that printed the delimiter name files it never wrote. Scratch lives in /tmp, never in /mnt/data, so the artifact scan cannot mistake this tool's own source for a user's output. The language menu is measured against the sandbox image rather than inherited: `d`, `f90` and `r` are dropped (no compiler), `rb` added, `ts` runs on bun. Files in and out ride the same three verbs: upload is POST /:id/fs, download is GET /:id/fs, and "is this session still alive?" is GET /:id — one question where there used to be a `lastModified` fetch and a 23-hour freshness rule that only together approximated it. PTC is declared and not executable: it is a different protocol (it suspends the run and resumes from a continuation token) and cloud answers it 501, so offering a tool that fails mid-run is worse than not offering one. De-branding: user-facing prose first, wire second. The `LIBRECHAT_TEST_RUN_HOOK` env var and the `@librechat/*` requires are renamed because BOTH ends live in this repo. The `application/vnd.librechat.*` artifact MIMEs are NOT renamed — they are identifiers on the wire, no user reads them, and a rename needs a migration story it would not buy anything. NOTICE and LICENSE are untouched.
270 lines
8.5 KiB
JavaScript
270 lines
8.5 KiB
JavaScript
const { nanoid } = require('nanoid');
|
|
const { logger } = require('@hanzochat/data-schemas');
|
|
const { checkAccess, loadWebSearchAuth } = require('@hanzochat/api');
|
|
const {
|
|
Tools,
|
|
AuthType,
|
|
Permissions,
|
|
ToolCallTypes,
|
|
PermissionTypes,
|
|
} = require('@hanzochat/data-provider');
|
|
const { processFileURL, uploadImageBuffer } = require('~/server/services/Files/process');
|
|
const { processCodeOutput } = require('~/server/services/Files/Code/process');
|
|
const { createToolCall, getToolCallsByConvo } = require('~/models/ToolCall');
|
|
const { loadAuthValues } = require('~/server/services/Tools/credentials');
|
|
const { loadTools } = require('~/app/clients/tools/util');
|
|
const { getRoleByName } = require('~/models/Role');
|
|
const { getMessage } = require('~/models/Message');
|
|
|
|
/**
|
|
* The credentials a directly-callable tool needs FROM THE USER, and the set of
|
|
* tools that can be called at all.
|
|
*
|
|
* `execute_code` needs none: the sandbox runs under the caller's own Hanzo IAM
|
|
* bearer, which the request already carries because `requireJwtAuth` let it
|
|
* through. There is nothing to type into a key dialog and nothing to store — an
|
|
* empty list says exactly that, where the old `[EnvVar.CODE_API_KEY]` claimed a
|
|
* per-user key existed for a shared service key that no user ever held (and that
|
|
* the agents package never even defined an enum member for).
|
|
*/
|
|
const fieldsMap = {
|
|
[Tools.execute_code]: [],
|
|
};
|
|
|
|
const toolAccessPermType = {
|
|
[Tools.execute_code]: PermissionTypes.RUN_CODE,
|
|
};
|
|
|
|
/**
|
|
* Verifies web search authentication, ensuring each category has at least
|
|
* one fully authenticated service.
|
|
*
|
|
* @param {ServerRequest} req - The request object
|
|
* @param {ServerResponse} res - The response object
|
|
* @returns {Promise<void>} A promise that resolves when the function has completed
|
|
*/
|
|
const verifyWebSearchAuth = async (req, res) => {
|
|
try {
|
|
const appConfig = req.config;
|
|
const userId = req.user.id;
|
|
/** @type {TCustomConfig['webSearch']} */
|
|
const webSearchConfig = appConfig?.webSearch || {};
|
|
const result = await loadWebSearchAuth({
|
|
userId,
|
|
loadAuthValues,
|
|
webSearchConfig,
|
|
throwError: false,
|
|
});
|
|
|
|
return res.status(200).json({
|
|
authenticated: result.authenticated,
|
|
authTypes: result.authTypes,
|
|
});
|
|
} catch (error) {
|
|
console.error('Error in verifyWebSearchAuth:', error);
|
|
return res.status(500).json({ message: error.message });
|
|
}
|
|
};
|
|
|
|
/**
|
|
* @param {ServerRequest} req - The request object, containing information about the HTTP request.
|
|
* @param {ServerResponse} res - The response object, used to send back the desired HTTP response.
|
|
* @returns {Promise<void>} A promise that resolves when the function has completed.
|
|
*/
|
|
const verifyToolAuth = async (req, res) => {
|
|
try {
|
|
const { toolId } = req.params;
|
|
if (toolId === Tools.web_search) {
|
|
return await verifyWebSearchAuth(req, res);
|
|
}
|
|
const authFields = fieldsMap[toolId];
|
|
if (!authFields) {
|
|
res.status(404).json({ message: 'Tool not found' });
|
|
return;
|
|
}
|
|
/* No user-held credential to check means nothing to ask the credential store,
|
|
* and asking it anyway is what put a key-entry dialog in front of a tool that
|
|
* has no key. */
|
|
if (authFields.length === 0) {
|
|
res.status(200).json({ authenticated: true, message: AuthType.SYSTEM_DEFINED });
|
|
return;
|
|
}
|
|
let result;
|
|
try {
|
|
result = await loadAuthValues({
|
|
userId: req.user.id,
|
|
authFields,
|
|
throwError: false,
|
|
});
|
|
} catch (error) {
|
|
logger.error('Error loading auth values', error);
|
|
res.status(200).json({ authenticated: false, message: AuthType.USER_PROVIDED });
|
|
return;
|
|
}
|
|
let isUserProvided = false;
|
|
for (const field of authFields) {
|
|
if (!result[field]) {
|
|
res.status(200).json({ authenticated: false, message: AuthType.USER_PROVIDED });
|
|
return;
|
|
}
|
|
if (!isUserProvided && process.env[field] !== result[field]) {
|
|
isUserProvided = true;
|
|
}
|
|
}
|
|
res.status(200).json({
|
|
authenticated: true,
|
|
message: isUserProvided ? AuthType.USER_PROVIDED : AuthType.SYSTEM_DEFINED,
|
|
});
|
|
} catch (error) {
|
|
res.status(500).json({ message: error.message });
|
|
}
|
|
};
|
|
|
|
/**
|
|
* @param {ServerRequest} req - The request object, containing information about the HTTP request.
|
|
* @param {ServerResponse} res - The response object, used to send back the desired HTTP response.
|
|
* @param {NextFunction} next - The next middleware function to call.
|
|
* @returns {Promise<void>} A promise that resolves when the function has completed.
|
|
*/
|
|
const callTool = async (req, res) => {
|
|
try {
|
|
const appConfig = req.config;
|
|
const { toolId = '' } = req.params;
|
|
if (!fieldsMap[toolId]) {
|
|
logger.warn(`[${toolId}/call] User ${req.user.id} attempted call to invalid tool`);
|
|
res.status(404).json({ message: 'Tool not found' });
|
|
return;
|
|
}
|
|
|
|
const { partIndex, blockIndex, messageId, conversationId, ...args } = req.body;
|
|
if (!messageId) {
|
|
logger.warn(`[${toolId}/call] User ${req.user.id} attempted call without message ID`);
|
|
res.status(400).json({ message: 'Message ID required' });
|
|
return;
|
|
}
|
|
|
|
const message = await getMessage({ user: req.user.id, messageId });
|
|
if (!message) {
|
|
logger.debug(`[${toolId}/call] User ${req.user.id} attempted call with invalid message ID`);
|
|
res.status(404).json({ message: 'Message not found' });
|
|
return;
|
|
}
|
|
logger.debug(`[${toolId}/call] User: ${req.user.id}`);
|
|
let hasAccess = true;
|
|
if (toolAccessPermType[toolId]) {
|
|
hasAccess = await checkAccess({
|
|
user: req.user,
|
|
permissionType: toolAccessPermType[toolId],
|
|
permissions: [Permissions.USE],
|
|
getRoleByName,
|
|
});
|
|
}
|
|
if (!hasAccess) {
|
|
logger.warn(
|
|
`[${toolAccessPermType[toolId]}] Forbidden: Insufficient permissions for User ${req.user.id}: ${Permissions.USE}`,
|
|
);
|
|
return res.status(403).json({ message: 'Forbidden: Insufficient permissions' });
|
|
}
|
|
const { loadedTools } = await loadTools({
|
|
user: req.user.id,
|
|
tools: [toolId],
|
|
functions: true,
|
|
options: {
|
|
req,
|
|
returnMetadata: true,
|
|
processFileURL,
|
|
uploadImageBuffer,
|
|
},
|
|
webSearch: appConfig.webSearch,
|
|
fileStrategy: appConfig.fileStrategy,
|
|
imageOutputType: appConfig.imageOutputType,
|
|
});
|
|
|
|
const tool = loadedTools[0];
|
|
const toolCallId = `${req.user.id}_${nanoid()}`;
|
|
const result = await tool.invoke({
|
|
args,
|
|
name: toolId,
|
|
id: toolCallId,
|
|
type: ToolCallTypes.TOOL_CALL,
|
|
});
|
|
|
|
const { content, artifact } = result;
|
|
const toolCallData = {
|
|
toolId,
|
|
messageId,
|
|
partIndex,
|
|
blockIndex,
|
|
conversationId,
|
|
result: content,
|
|
user: req.user.id,
|
|
};
|
|
|
|
if (!artifact || !artifact.files || toolId !== Tools.execute_code) {
|
|
createToolCall(toolCallData).catch((error) => {
|
|
logger.error(`Error creating tool call: ${error.message}`);
|
|
});
|
|
return res.status(200).json({
|
|
result: content,
|
|
});
|
|
}
|
|
|
|
const artifactPromises = [];
|
|
for (const file of artifact.files) {
|
|
const { id, name } = file;
|
|
artifactPromises.push(
|
|
(async () => {
|
|
const fileMetadata = await processCodeOutput({
|
|
req,
|
|
id,
|
|
name,
|
|
apiKey: tool.apiKey,
|
|
messageId,
|
|
toolCallId,
|
|
conversationId,
|
|
session_id: artifact.session_id,
|
|
});
|
|
|
|
if (!fileMetadata) {
|
|
return null;
|
|
}
|
|
|
|
return fileMetadata;
|
|
})().catch((error) => {
|
|
logger.error('Error processing code output:', error);
|
|
return null;
|
|
}),
|
|
);
|
|
}
|
|
const attachments = await Promise.all(artifactPromises);
|
|
toolCallData.attachments = attachments;
|
|
createToolCall(toolCallData).catch((error) => {
|
|
logger.error(`Error creating tool call: ${error.message}`);
|
|
});
|
|
res.status(200).json({
|
|
result: content,
|
|
attachments,
|
|
});
|
|
} catch (error) {
|
|
logger.error('Error calling tool', error);
|
|
res.status(500).json({ message: 'Error calling tool' });
|
|
}
|
|
};
|
|
|
|
const getToolCalls = async (req, res) => {
|
|
try {
|
|
const { conversationId } = req.query;
|
|
const toolCalls = await getToolCallsByConvo(conversationId, req.user.id);
|
|
res.status(200).json(toolCalls);
|
|
} catch (error) {
|
|
logger.error('Error getting tool calls', error);
|
|
res.status(500).json({ message: 'Error getting tool calls' });
|
|
}
|
|
};
|
|
|
|
module.exports = {
|
|
callTool,
|
|
getToolCalls,
|
|
verifyToolAuth,
|
|
};
|