Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

fix(responses-chat-bridge): compact oversized exec descriptions (#2890)

ZJPex5c679586fba7
diff --git a/packages/responses-chat-bridge/src/__tests__/translate-request.test.ts b/packages/responses-chat-bridge/src/__tests__/translate-request.test.ts
index 601dd2da00..ef7f07784c 100644
--- a/packages/responses-chat-bridge/src/__tests__/translate-request.test.ts
+++ b/packages/responses-chat-bridge/src/__tests__/translate-request.test.ts
@@ -895,6 +895,79 @@ describe('translateResponsesRequest', () => {
});
});
+ it('compacts only oversized top-level custom exec descriptions', () => {
+ const execCatalog = `EXEC_CATALOG_START:${'nested-tool-doc;'.repeat(40_000)}:EXEC_CATALOG_END`;
+ const ordinaryFunctionDescription = `FUNCTION_DOCS_START:${'function-doc;'.repeat(8_000)}`;
+ const ordinaryCustomDescription = `CUSTOM_DOCS_START:${'custom-doc;'.repeat(8_000)}`;
+ const out = translateResponsesRequest(base({
+ tools: [
+ { type: 'custom', name: 'exec', description: execCatalog },
+ {
+ type: 'function',
+ name: 'large_function',
+ description: ordinaryFunctionDescription,
+ parameters: { type: 'object' },
+ },
+ { type: 'custom', name: 'large_custom', description: ordinaryCustomDescription },
+ ],
+ tool_choice: { type: 'custom', name: 'exec' },
+ }));
+
+ const execTool = out.tools?.[0]?.function;
+ expect(execTool).toMatchObject({
+ name: 'exec',
+ parameters: {
+ type: 'object',
+ properties: { input: { type: 'string', description: expect.any(String) } },
+ required: ['input'],
+ },
+ });
+ expect(execTool?.description).not.toContain('EXEC_CATALOG_START');
+ expect(execTool?.description).toContain('global `tools`');
+ expect(execTool?.description).toContain('`ALL_TOOLS`');
+ for (const protocol of [
+ '`await tools.<name>(...)`',
+ '`await tools.<namespace>__<name>(...)`',
+ '`yield_control()`',
+ '`exit()`',
+ '`text(...)`',
+ '`image(...)`',
+ '`audio(...)`',
+ '`generatedImage(...)`',
+ '`store(key, value)`',
+ '`load(key)`',
+ '`notify(value)`',
+ ]) {
+ expect(execTool?.description).toContain(protocol);
+ }
+ expect(execTool?.description).not.toContain('`await tools.<namespace>.<name>(...)`');
+ expect(Buffer.byteLength(JSON.stringify(out.tools?.[0]), 'utf8')).toBeLessThan(4_096);
+ expect(out.tool_choice).toEqual({ type: 'function', function: { name: 'exec' } });
+
+ expect(out.tools?.[1]?.function.description).toBe(ordinaryFunctionDescription);
+ const customDescription = out.tools?.[2]?.function.description ?? '';
+ expect(customDescription).toContain(ordinaryCustomDescription);
+ expect(customDescription.indexOf(ordinaryCustomDescription)).toBe(
+ customDescription.lastIndexOf(ordinaryCustomDescription),
+ );
+ });
+
+ it('does not apply the top-level exec adapter to a namespaced custom exec', () => {
+ const namespacedDescription = `NAMESPACED_EXEC:${'namespace-doc;'.repeat(4_000)}`;
+ const out = translateResponsesRequest(base({
+ tools: [{
+ type: 'namespace',
+ name: 'plugin',
+ tools: [{ type: 'custom', name: 'exec', description: namespacedDescription }],
+ }],
+ }));
+
+ expect(out.tools?.[0]?.function).toMatchObject({
+ name: 'plugin__exec',
+ description: expect.stringContaining(namespacedDescription),
+ });
+ });
+
it('keeps the built-in tool-search adapter distinct from a user tool_search name', () => {
const out = translateResponsesRequest(base({
tools: [
diff --git a/packages/responses-chat-bridge/src/tool-context.ts b/packages/responses-chat-bridge/src/tool-context.ts
index 7c841cd1fa..4914ce9482 100644
--- a/packages/responses-chat-bridge/src/tool-context.ts
+++ b/packages/responses-chat-bridge/src/tool-context.ts
@@ -1,3 +1,4 @@
+import { Buffer } from 'node:buffer';
import { createHash } from 'node:crypto';
import type {
@@ -13,6 +14,17 @@ const CHAT_TOOL_NAME_MAX_LENGTH = 64;
const TOOL_SEARCH_CHAT_NAME = 'tool_search';
const CUSTOM_TOOL_INPUT_DESCRIPTION =
'Raw string input for the original custom tool. Preserve formatting exactly.';
+const CUSTOM_EXEC_DESCRIPTION_MAX_BYTES = 32 * 1024;
+const COMPACT_CUSTOM_EXEC_DESCRIPTION = [
+ 'Execute JavaScript in the Code Mode runtime. Pass the complete JavaScript source in `input`.',
+ 'Call tools on the global `tools` object with `await tools.<name>(...)`; namespace tools use',
+ 'the flattened `await tools.<namespace>__<name>(...)` form.',
+ 'Inspect `ALL_TOOLS` for available shell, patch, plugin, connector, and MCP tools.',
+ 'Use `yield_control()` to yield while execution continues, and `exit()` to stop early.',
+ 'Return user-visible output with `text(...)`, `image(...)`, `audio(...)`, or',
+ '`generatedImage(...)`. Share serializable state across exec calls with `store(key, value)`',
+ 'and `load(key)`, and report progress with `notify(value)`.',
+].join(' ');
function isPlainObject(value: unknown): value is Record<string, unknown> {
return typeof value === 'object' && value !== null && !Array.isArray(value);
@@ -35,14 +47,27 @@ function normalizeFunctionParameters(parameters: unknown): Record<string, unknow
: { ...parameters, type: 'object' };
}
-function stableToolDescription(tool: unknown): string {
+function stableToolDescription(tool: ResponsesCustomTool): string {
+ const metadata: Record<string, unknown> = { ...tool };
+ delete metadata.description;
try {
- return `Original Responses custom tool definition:\n${JSON.stringify(tool)}`;
+ return `Original Responses custom tool metadata:\n${JSON.stringify(metadata)}`;
} catch {
- return 'Original Responses custom tool definition is unavailable.';
+ return 'Original Responses custom tool metadata is unavailable.';
}
}
+function chatCustomToolDescription(tool: ResponsesCustomTool, namespace?: string): string {
+ const metadata = stableToolDescription(tool);
+ if (!tool.description) return metadata;
+ const description = namespace === undefined
+ && tool.name === 'exec'
+ && Buffer.byteLength(tool.description, 'utf8') > CUSTOM_EXEC_DESCRIPTION_MAX_BYTES
+ ? COMPACT_CUSTOM_EXEC_DESCRIPTION
+ : tool.description;
+ return `${description}\n\n${metadata}`;
+}
+
export type ChatBridgeToolKind = 'function' | 'namespace' | 'custom' | 'tool_search';
type ResponseToolIdentityKind = 'function' | 'custom' | 'tool_search';
@@ -169,9 +194,7 @@ export class ChatBridgeToolContext {
type: 'function',
function: {
name: chatName,
- description: tool.description
- ? `${tool.description}\n\n${stableToolDescription(tool)}`
- : stableToolDescription(tool),
+ description: chatCustomToolDescription(tool, namespace),
parameters: {
type: 'object',
properties: {