mirror of
https://github.com/QwenLM/qwen-code.git
synced 2026-01-22 08:46:19 +00:00
Compare commits
1 Commits
fix/missin
...
fix/missin
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
61aad5a162 |
@@ -630,6 +630,67 @@ describe('BaseJsonOutputAdapter', () => {
|
|||||||
|
|
||||||
expect(state.blocks).toHaveLength(0);
|
expect(state.blocks).toHaveLength(0);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should preserve whitespace in thinking content', () => {
|
||||||
|
const state = adapter.exposeCreateMessageState();
|
||||||
|
adapter.startAssistantMessage();
|
||||||
|
|
||||||
|
adapter.exposeAppendThinking(
|
||||||
|
state,
|
||||||
|
'',
|
||||||
|
'The user just said "Hello"',
|
||||||
|
null,
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(state.blocks).toHaveLength(1);
|
||||||
|
expect(state.blocks[0]).toMatchObject({
|
||||||
|
type: 'thinking',
|
||||||
|
thinking: 'The user just said "Hello"',
|
||||||
|
});
|
||||||
|
// Verify spaces are preserved
|
||||||
|
const block = state.blocks[0] as { thinking: string };
|
||||||
|
expect(block.thinking).toContain('user just');
|
||||||
|
expect(block.thinking).not.toContain('userjust');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should preserve whitespace when appending multiple thinking fragments', () => {
|
||||||
|
const state = adapter.exposeCreateMessageState();
|
||||||
|
adapter.startAssistantMessage();
|
||||||
|
|
||||||
|
// Simulate streaming thinking content in fragments
|
||||||
|
adapter.exposeAppendThinking(state, '', 'The user just', null);
|
||||||
|
adapter.exposeAppendThinking(state, '', ' said "Hello"', null);
|
||||||
|
adapter.exposeAppendThinking(
|
||||||
|
state,
|
||||||
|
'',
|
||||||
|
'. This is a simple greeting',
|
||||||
|
null,
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(state.blocks).toHaveLength(1);
|
||||||
|
const block = state.blocks[0] as { thinking: string };
|
||||||
|
// Verify the complete text with all spaces preserved
|
||||||
|
expect(block.thinking).toBe(
|
||||||
|
'The user just said "Hello". This is a simple greeting',
|
||||||
|
);
|
||||||
|
// Verify specific space preservation
|
||||||
|
expect(block.thinking).toContain('user just ');
|
||||||
|
expect(block.thinking).toContain(' said');
|
||||||
|
expect(block.thinking).toContain('". This');
|
||||||
|
expect(block.thinking).not.toContain('userjust');
|
||||||
|
expect(block.thinking).not.toContain('justsaid');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should preserve leading and trailing whitespace in description', () => {
|
||||||
|
const state = adapter.exposeCreateMessageState();
|
||||||
|
adapter.startAssistantMessage();
|
||||||
|
|
||||||
|
adapter.exposeAppendThinking(state, '', ' content with spaces ', null);
|
||||||
|
|
||||||
|
expect(state.blocks).toHaveLength(1);
|
||||||
|
const block = state.blocks[0] as { thinking: string };
|
||||||
|
expect(block.thinking).toBe(' content with spaces ');
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('appendToolUse', () => {
|
describe('appendToolUse', () => {
|
||||||
|
|||||||
@@ -816,9 +816,18 @@ export abstract class BaseJsonOutputAdapter {
|
|||||||
parentToolUseId?: string | null,
|
parentToolUseId?: string | null,
|
||||||
): void {
|
): void {
|
||||||
const actualParentToolUseId = parentToolUseId ?? null;
|
const actualParentToolUseId = parentToolUseId ?? null;
|
||||||
const fragment = [subject?.trim(), description?.trim()]
|
|
||||||
.filter((value) => value && value.length > 0)
|
// Build fragment without trimming to preserve whitespace in streaming content
|
||||||
.join(': ');
|
// Only filter out null/undefined/empty values
|
||||||
|
const parts: string[] = [];
|
||||||
|
if (subject && subject.length > 0) {
|
||||||
|
parts.push(subject);
|
||||||
|
}
|
||||||
|
if (description && description.length > 0) {
|
||||||
|
parts.push(description);
|
||||||
|
}
|
||||||
|
|
||||||
|
const fragment = parts.join(': ');
|
||||||
if (!fragment) {
|
if (!fragment) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -323,6 +323,68 @@ describe('StreamJsonOutputAdapter', () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should preserve whitespace in thinking content (issue #1356)', () => {
|
||||||
|
adapter.processEvent({
|
||||||
|
type: GeminiEventType.Thought,
|
||||||
|
value: {
|
||||||
|
subject: '',
|
||||||
|
description: 'The user just said "Hello"',
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
const message = adapter.finalizeAssistantMessage();
|
||||||
|
expect(message.message.content).toHaveLength(1);
|
||||||
|
const block = message.message.content[0] as {
|
||||||
|
type: string;
|
||||||
|
thinking: string;
|
||||||
|
};
|
||||||
|
expect(block.type).toBe('thinking');
|
||||||
|
expect(block.thinking).toBe('The user just said "Hello"');
|
||||||
|
// Verify spaces are preserved
|
||||||
|
expect(block.thinking).toContain('user just');
|
||||||
|
expect(block.thinking).not.toContain('userjust');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should preserve whitespace when streaming multiple thinking fragments (issue #1356)', () => {
|
||||||
|
// Simulate streaming thinking content in multiple events
|
||||||
|
adapter.processEvent({
|
||||||
|
type: GeminiEventType.Thought,
|
||||||
|
value: {
|
||||||
|
subject: '',
|
||||||
|
description: 'The user just',
|
||||||
|
},
|
||||||
|
});
|
||||||
|
adapter.processEvent({
|
||||||
|
type: GeminiEventType.Thought,
|
||||||
|
value: {
|
||||||
|
subject: '',
|
||||||
|
description: ' said "Hello"',
|
||||||
|
},
|
||||||
|
});
|
||||||
|
adapter.processEvent({
|
||||||
|
type: GeminiEventType.Thought,
|
||||||
|
value: {
|
||||||
|
subject: '',
|
||||||
|
description: '. This is a simple greeting',
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
const message = adapter.finalizeAssistantMessage();
|
||||||
|
expect(message.message.content).toHaveLength(1);
|
||||||
|
const block = message.message.content[0] as {
|
||||||
|
type: string;
|
||||||
|
thinking: string;
|
||||||
|
};
|
||||||
|
expect(block.thinking).toBe(
|
||||||
|
'The user just said "Hello". This is a simple greeting',
|
||||||
|
);
|
||||||
|
// Verify specific spaces are preserved
|
||||||
|
expect(block.thinking).toContain('user just ');
|
||||||
|
expect(block.thinking).toContain(' said');
|
||||||
|
expect(block.thinking).not.toContain('userjust');
|
||||||
|
expect(block.thinking).not.toContain('justsaid');
|
||||||
|
});
|
||||||
|
|
||||||
it('should append tool use from ToolCallRequest events', () => {
|
it('should append tool use from ToolCallRequest events', () => {
|
||||||
adapter.processEvent({
|
adapter.processEvent({
|
||||||
type: GeminiEventType.ToolCallRequest,
|
type: GeminiEventType.ToolCallRequest,
|
||||||
|
|||||||
@@ -771,52 +771,6 @@ describe('runNonInteractive', () => {
|
|||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should handle API errors in text mode and exit with error code', async () => {
|
|
||||||
(mockConfig.getOutputFormat as Mock).mockReturnValue(OutputFormat.TEXT);
|
|
||||||
setupMetricsMock();
|
|
||||||
|
|
||||||
// Simulate an API error event (like 401 unauthorized)
|
|
||||||
const apiErrorEvent: ServerGeminiStreamEvent = {
|
|
||||||
type: GeminiEventType.Error,
|
|
||||||
value: {
|
|
||||||
error: {
|
|
||||||
message: '401 Incorrect API key provided',
|
|
||||||
status: 401,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
mockGeminiClient.sendMessageStream.mockReturnValue(
|
|
||||||
createStreamFromEvents([apiErrorEvent]),
|
|
||||||
);
|
|
||||||
|
|
||||||
let thrownError: Error | null = null;
|
|
||||||
try {
|
|
||||||
await runNonInteractive(
|
|
||||||
mockConfig,
|
|
||||||
mockSettings,
|
|
||||||
'Test input',
|
|
||||||
'prompt-id-api-error',
|
|
||||||
);
|
|
||||||
// Should not reach here
|
|
||||||
expect.fail('Expected error to be thrown');
|
|
||||||
} catch (error) {
|
|
||||||
thrownError = error as Error;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Should throw with the API error message
|
|
||||||
expect(thrownError).toBeTruthy();
|
|
||||||
expect(thrownError?.message).toContain('401');
|
|
||||||
expect(thrownError?.message).toContain('Incorrect API key provided');
|
|
||||||
|
|
||||||
// Verify error was written to stderr
|
|
||||||
expect(processStderrSpy).toHaveBeenCalled();
|
|
||||||
const stderrCalls = processStderrSpy.mock.calls;
|
|
||||||
const errorOutput = stderrCalls.map((call) => call[0]).join('');
|
|
||||||
expect(errorOutput).toContain('401');
|
|
||||||
expect(errorOutput).toContain('Incorrect API key provided');
|
|
||||||
});
|
|
||||||
|
|
||||||
it('should handle FatalInputError with custom exit code in JSON format', async () => {
|
it('should handle FatalInputError with custom exit code in JSON format', async () => {
|
||||||
(mockConfig.getOutputFormat as Mock).mockReturnValue(OutputFormat.JSON);
|
(mockConfig.getOutputFormat as Mock).mockReturnValue(OutputFormat.JSON);
|
||||||
setupMetricsMock();
|
setupMetricsMock();
|
||||||
|
|||||||
@@ -308,8 +308,6 @@ export async function runNonInteractive(
|
|||||||
config.getContentGeneratorConfig()?.authType,
|
config.getContentGeneratorConfig()?.authType,
|
||||||
);
|
);
|
||||||
process.stderr.write(`${errorText}\n`);
|
process.stderr.write(`${errorText}\n`);
|
||||||
// Throw error to exit with non-zero code
|
|
||||||
throw new Error(errorText);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -169,44 +169,6 @@ describe('ShellTool', () => {
|
|||||||
});
|
});
|
||||||
expect(invocation.getDescription()).not.toContain('[background]');
|
expect(invocation.getDescription()).not.toContain('[background]');
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('is_background parameter coercion', () => {
|
|
||||||
it('should accept string "true" as boolean true', () => {
|
|
||||||
const invocation = shellTool.build({
|
|
||||||
command: 'npm run dev',
|
|
||||||
is_background: 'true' as unknown as boolean,
|
|
||||||
});
|
|
||||||
expect(invocation).toBeDefined();
|
|
||||||
expect(invocation.getDescription()).toContain('[background]');
|
|
||||||
});
|
|
||||||
|
|
||||||
it('should accept string "false" as boolean false', () => {
|
|
||||||
const invocation = shellTool.build({
|
|
||||||
command: 'npm run build',
|
|
||||||
is_background: 'false' as unknown as boolean,
|
|
||||||
});
|
|
||||||
expect(invocation).toBeDefined();
|
|
||||||
expect(invocation.getDescription()).not.toContain('[background]');
|
|
||||||
});
|
|
||||||
|
|
||||||
it('should accept string "True" as boolean true', () => {
|
|
||||||
const invocation = shellTool.build({
|
|
||||||
command: 'npm run dev',
|
|
||||||
is_background: 'True' as unknown as boolean,
|
|
||||||
});
|
|
||||||
expect(invocation).toBeDefined();
|
|
||||||
expect(invocation.getDescription()).toContain('[background]');
|
|
||||||
});
|
|
||||||
|
|
||||||
it('should accept string "False" as boolean false', () => {
|
|
||||||
const invocation = shellTool.build({
|
|
||||||
command: 'npm run build',
|
|
||||||
is_background: 'False' as unknown as boolean,
|
|
||||||
});
|
|
||||||
expect(invocation).toBeDefined();
|
|
||||||
expect(invocation.getDescription()).not.toContain('[background]');
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('execute', () => {
|
describe('execute', () => {
|
||||||
|
|||||||
@@ -122,91 +122,4 @@ describe('SchemaValidator', () => {
|
|||||||
};
|
};
|
||||||
expect(SchemaValidator.validate(schema, params)).not.toBeNull();
|
expect(SchemaValidator.validate(schema, params)).not.toBeNull();
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('boolean string coercion', () => {
|
|
||||||
const booleanSchema = {
|
|
||||||
type: 'object',
|
|
||||||
properties: {
|
|
||||||
is_background: {
|
|
||||||
type: 'boolean',
|
|
||||||
},
|
|
||||||
},
|
|
||||||
required: ['is_background'],
|
|
||||||
};
|
|
||||||
|
|
||||||
it('should coerce string "true" to boolean true', () => {
|
|
||||||
const params = { is_background: 'true' };
|
|
||||||
expect(SchemaValidator.validate(booleanSchema, params)).toBeNull();
|
|
||||||
expect(params.is_background).toBe(true);
|
|
||||||
});
|
|
||||||
|
|
||||||
it('should coerce string "True" to boolean true', () => {
|
|
||||||
const params = { is_background: 'True' };
|
|
||||||
expect(SchemaValidator.validate(booleanSchema, params)).toBeNull();
|
|
||||||
expect(params.is_background).toBe(true);
|
|
||||||
});
|
|
||||||
|
|
||||||
it('should coerce string "TRUE" to boolean true', () => {
|
|
||||||
const params = { is_background: 'TRUE' };
|
|
||||||
expect(SchemaValidator.validate(booleanSchema, params)).toBeNull();
|
|
||||||
expect(params.is_background).toBe(true);
|
|
||||||
});
|
|
||||||
|
|
||||||
it('should coerce string "false" to boolean false', () => {
|
|
||||||
const params = { is_background: 'false' };
|
|
||||||
expect(SchemaValidator.validate(booleanSchema, params)).toBeNull();
|
|
||||||
expect(params.is_background).toBe(false);
|
|
||||||
});
|
|
||||||
|
|
||||||
it('should coerce string "False" to boolean false', () => {
|
|
||||||
const params = { is_background: 'False' };
|
|
||||||
expect(SchemaValidator.validate(booleanSchema, params)).toBeNull();
|
|
||||||
expect(params.is_background).toBe(false);
|
|
||||||
});
|
|
||||||
|
|
||||||
it('should coerce string "FALSE" to boolean false', () => {
|
|
||||||
const params = { is_background: 'FALSE' };
|
|
||||||
expect(SchemaValidator.validate(booleanSchema, params)).toBeNull();
|
|
||||||
expect(params.is_background).toBe(false);
|
|
||||||
});
|
|
||||||
|
|
||||||
it('should handle nested objects with string booleans', () => {
|
|
||||||
const nestedSchema = {
|
|
||||||
type: 'object',
|
|
||||||
properties: {
|
|
||||||
options: {
|
|
||||||
type: 'object',
|
|
||||||
properties: {
|
|
||||||
enabled: { type: 'boolean' },
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
};
|
|
||||||
const params = { options: { enabled: 'true' } };
|
|
||||||
expect(SchemaValidator.validate(nestedSchema, params)).toBeNull();
|
|
||||||
expect((params.options as unknown as { enabled: boolean }).enabled).toBe(
|
|
||||||
true,
|
|
||||||
);
|
|
||||||
});
|
|
||||||
|
|
||||||
it('should not affect non-boolean strings', () => {
|
|
||||||
const mixedSchema = {
|
|
||||||
type: 'object',
|
|
||||||
properties: {
|
|
||||||
name: { type: 'string' },
|
|
||||||
is_active: { type: 'boolean' },
|
|
||||||
},
|
|
||||||
};
|
|
||||||
const params = { name: 'trueman', is_active: 'true' };
|
|
||||||
expect(SchemaValidator.validate(mixedSchema, params)).toBeNull();
|
|
||||||
expect(params.name).toBe('trueman');
|
|
||||||
expect(params.is_active).toBe(true);
|
|
||||||
});
|
|
||||||
|
|
||||||
it('should pass through actual boolean values unchanged', () => {
|
|
||||||
const params = { is_background: true };
|
|
||||||
expect(SchemaValidator.validate(booleanSchema, params)).toBeNull();
|
|
||||||
expect(params.is_background).toBe(true);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -41,12 +41,14 @@ export class SchemaValidator {
|
|||||||
return 'Value of params must be an object';
|
return 'Value of params must be an object';
|
||||||
}
|
}
|
||||||
const validate = ajValidator.compile(schema);
|
const validate = ajValidator.compile(schema);
|
||||||
let valid = validate(data);
|
const valid = validate(data);
|
||||||
if (!valid && validate.errors) {
|
if (!valid && validate.errors) {
|
||||||
// Coerce string boolean values ("true"/"false") to actual booleans
|
// Find any True or False values and lowercase them
|
||||||
fixBooleanValues(data as Record<string, unknown>);
|
fixBooleanCasing(data as Record<string, unknown>);
|
||||||
|
|
||||||
|
const validate = ajValidator.compile(schema);
|
||||||
|
const valid = validate(data);
|
||||||
|
|
||||||
valid = validate(data);
|
|
||||||
if (!valid && validate.errors) {
|
if (!valid && validate.errors) {
|
||||||
return ajValidator.errorsText(validate.errors, { dataVar: 'params' });
|
return ajValidator.errorsText(validate.errors, { dataVar: 'params' });
|
||||||
}
|
}
|
||||||
@@ -55,29 +57,13 @@ export class SchemaValidator {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
function fixBooleanCasing(data: Record<string, unknown>) {
|
||||||
* Coerces string boolean values to actual booleans.
|
|
||||||
* This handles cases where LLMs return "true"/"false" strings instead of boolean values,
|
|
||||||
* which is common with self-hosted LLMs.
|
|
||||||
*
|
|
||||||
* Converts:
|
|
||||||
* - "true", "True", "TRUE" -> true
|
|
||||||
* - "false", "False", "FALSE" -> false
|
|
||||||
*/
|
|
||||||
function fixBooleanValues(data: Record<string, unknown>) {
|
|
||||||
for (const key of Object.keys(data)) {
|
for (const key of Object.keys(data)) {
|
||||||
if (!(key in data)) continue;
|
if (!(key in data)) continue;
|
||||||
const value = data[key];
|
|
||||||
|
|
||||||
if (typeof value === 'object' && value !== null) {
|
if (typeof data[key] === 'object') {
|
||||||
fixBooleanValues(value as Record<string, unknown>);
|
fixBooleanCasing(data[key] as Record<string, unknown>);
|
||||||
} else if (typeof value === 'string') {
|
} else if (data[key] === 'True') data[key] = 'true';
|
||||||
const lower = value.toLowerCase();
|
else if (data[key] === 'False') data[key] = 'false';
|
||||||
if (lower === 'true') {
|
|
||||||
data[key] = true;
|
|
||||||
} else if (lower === 'false') {
|
|
||||||
data[key] = false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user