mirror of
https://github.com/QwenLM/qwen-code.git
synced 2025-12-31 05:59:14 +00:00
Compare commits
5 Commits
release/sd
...
fix/missin
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
15912892f2 | ||
|
|
105ad743fa | ||
|
|
ac3f7cb8c8 | ||
|
|
7b01b26ff5 | ||
|
|
0f3e97ea1c |
2
package-lock.json
generated
2
package-lock.json
generated
@@ -18593,7 +18593,7 @@
|
||||
},
|
||||
"packages/sdk-typescript": {
|
||||
"name": "@qwen-code/sdk",
|
||||
"version": "0.1.1",
|
||||
"version": "0.1.0",
|
||||
"license": "Apache-2.0",
|
||||
"dependencies": {
|
||||
"@modelcontextprotocol/sdk": "^1.25.1",
|
||||
|
||||
@@ -771,6 +771,52 @@ 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 () => {
|
||||
(mockConfig.getOutputFormat as Mock).mockReturnValue(OutputFormat.JSON);
|
||||
setupMetricsMock();
|
||||
|
||||
@@ -308,6 +308,8 @@ export async function runNonInteractive(
|
||||
config.getContentGeneratorConfig()?.authType,
|
||||
);
|
||||
process.stderr.write(`${errorText}\n`);
|
||||
// Throw error to exit with non-zero code
|
||||
throw new Error(errorText);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -169,6 +169,44 @@ describe('ShellTool', () => {
|
||||
});
|
||||
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', () => {
|
||||
|
||||
@@ -122,4 +122,91 @@ describe('SchemaValidator', () => {
|
||||
};
|
||||
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,14 +41,12 @@ export class SchemaValidator {
|
||||
return 'Value of params must be an object';
|
||||
}
|
||||
const validate = ajValidator.compile(schema);
|
||||
const valid = validate(data);
|
||||
let valid = validate(data);
|
||||
if (!valid && validate.errors) {
|
||||
// Find any True or False values and lowercase them
|
||||
fixBooleanCasing(data as Record<string, unknown>);
|
||||
|
||||
const validate = ajValidator.compile(schema);
|
||||
const valid = validate(data);
|
||||
// Coerce string boolean values ("true"/"false") to actual booleans
|
||||
fixBooleanValues(data as Record<string, unknown>);
|
||||
|
||||
valid = validate(data);
|
||||
if (!valid && validate.errors) {
|
||||
return ajValidator.errorsText(validate.errors, { dataVar: 'params' });
|
||||
}
|
||||
@@ -57,13 +55,29 @@ 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)) {
|
||||
if (!(key in data)) continue;
|
||||
const value = data[key];
|
||||
|
||||
if (typeof data[key] === 'object') {
|
||||
fixBooleanCasing(data[key] as Record<string, unknown>);
|
||||
} else if (data[key] === 'True') data[key] = 'true';
|
||||
else if (data[key] === 'False') data[key] = 'false';
|
||||
if (typeof value === 'object' && value !== null) {
|
||||
fixBooleanValues(value as Record<string, unknown>);
|
||||
} else if (typeof value === 'string') {
|
||||
const lower = value.toLowerCase();
|
||||
if (lower === 'true') {
|
||||
data[key] = true;
|
||||
} else if (lower === 'false') {
|
||||
data[key] = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@qwen-code/sdk",
|
||||
"version": "0.1.1",
|
||||
"version": "0.1.0",
|
||||
"description": "TypeScript SDK for programmatic access to qwen-code CLI",
|
||||
"main": "./dist/index.cjs",
|
||||
"module": "./dist/index.mjs",
|
||||
|
||||
Reference in New Issue
Block a user