mirror of
https://github.com/QwenLM/qwen-code.git
synced 2025-12-20 16:57:46 +00:00
Merge branch 'main' into chore/sync-gemini-cli-v0.3.4
This commit is contained in:
@@ -13,14 +13,14 @@ import { ReadFileTool } from '../tools/read-file.js';
|
||||
import { ReadManyFilesTool } from '../tools/read-many-files.js';
|
||||
import { GrepTool } from '../tools/grep.js';
|
||||
import { LruCache } from './LruCache.js';
|
||||
import { DEFAULT_GEMINI_FLASH_LITE_MODEL } from '../config/models.js';
|
||||
import { DEFAULT_QWEN_FLASH_MODEL } from '../config/models.js';
|
||||
import {
|
||||
isFunctionResponse,
|
||||
isFunctionCall,
|
||||
} from '../utils/messageInspectors.js';
|
||||
import * as fs from 'node:fs';
|
||||
|
||||
const EditModel = DEFAULT_GEMINI_FLASH_LITE_MODEL;
|
||||
const EditModel = DEFAULT_QWEN_FLASH_MODEL;
|
||||
const EditConfig: GenerateContentConfig = {
|
||||
thinkingConfig: {
|
||||
thinkingBudget: 0,
|
||||
|
||||
@@ -13,7 +13,7 @@ import { ToolErrorType } from '../tools/tool-error.js';
|
||||
import { BINARY_EXTENSIONS } from './ignorePatterns.js';
|
||||
|
||||
// Constants for text file processing
|
||||
const DEFAULT_MAX_LINES_TEXT_FILE = 2000;
|
||||
export const DEFAULT_MAX_LINES_TEXT_FILE = 2000;
|
||||
const MAX_LINE_LENGTH_TEXT_FILE = 2000;
|
||||
|
||||
// Default values for encoding and separator format
|
||||
|
||||
@@ -4,10 +4,17 @@
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
import type { Mock } from 'vitest';
|
||||
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
|
||||
import type { Content, GoogleGenAI, Models } from '@google/genai';
|
||||
import { DEFAULT_GEMINI_FLASH_MODEL } from '../config/models.js';
|
||||
import {
|
||||
describe,
|
||||
it,
|
||||
expect,
|
||||
vi,
|
||||
beforeEach,
|
||||
type Mock,
|
||||
afterEach,
|
||||
} from 'vitest';
|
||||
import { type Content, GoogleGenAI, Models } from '@google/genai';
|
||||
import { DEFAULT_QWEN_FLASH_MODEL } from '../config/models.js';
|
||||
import { GeminiClient } from '../core/client.js';
|
||||
import { Config } from '../config/config.js';
|
||||
import type { NextSpeakerResponse } from './nextSpeakerChecker.js';
|
||||
@@ -235,7 +242,7 @@ describe('checkNextSpeaker', () => {
|
||||
expect(result).toBeNull();
|
||||
});
|
||||
|
||||
it('should call generateJson with DEFAULT_GEMINI_FLASH_MODEL', async () => {
|
||||
it('should call generateJson with DEFAULT_QWEN_FLASH_MODEL', async () => {
|
||||
(chatInstance.getHistory as Mock).mockReturnValue([
|
||||
{ role: 'model', parts: [{ text: 'Some model output.' }] },
|
||||
] as Content[]);
|
||||
@@ -250,6 +257,6 @@ describe('checkNextSpeaker', () => {
|
||||
expect(mockGeminiClient.generateJson).toHaveBeenCalled();
|
||||
const generateJsonCall = (mockGeminiClient.generateJson as Mock).mock
|
||||
.calls[0];
|
||||
expect(generateJsonCall[3]).toBe(DEFAULT_GEMINI_FLASH_MODEL);
|
||||
expect(generateJsonCall[3]).toBe(DEFAULT_QWEN_FLASH_MODEL);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
*/
|
||||
|
||||
import type { Content } from '@google/genai';
|
||||
import { DEFAULT_GEMINI_FLASH_MODEL } from '../config/models.js';
|
||||
import { DEFAULT_QWEN_FLASH_MODEL } from '../config/models.js';
|
||||
import type { GeminiClient } from '../core/client.js';
|
||||
import type { GeminiChat } from '../core/geminiChat.js';
|
||||
import { isFunctionResponse } from './messageInspectors.js';
|
||||
@@ -112,7 +112,7 @@ export async function checkNextSpeaker(
|
||||
contents,
|
||||
RESPONSE_SCHEMA,
|
||||
abortSignal,
|
||||
DEFAULT_GEMINI_FLASH_MODEL,
|
||||
DEFAULT_QWEN_FLASH_MODEL,
|
||||
)) as unknown as NextSpeakerResponse;
|
||||
|
||||
if (
|
||||
|
||||
119
packages/core/src/utils/projectSummary.ts
Normal file
119
packages/core/src/utils/projectSummary.ts
Normal file
@@ -0,0 +1,119 @@
|
||||
/**
|
||||
* @license
|
||||
* Copyright 2025 Google LLC
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
import * as fs from 'fs/promises';
|
||||
import * as path from 'path';
|
||||
|
||||
export interface ProjectSummaryInfo {
|
||||
hasHistory: boolean;
|
||||
content?: string;
|
||||
timestamp?: string;
|
||||
timeAgo?: string;
|
||||
goalContent?: string;
|
||||
planContent?: string;
|
||||
totalTasks?: number;
|
||||
doneCount?: number;
|
||||
inProgressCount?: number;
|
||||
todoCount?: number;
|
||||
pendingTasks?: string[];
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads and parses the project summary file to extract structured information
|
||||
*/
|
||||
export async function getProjectSummaryInfo(): Promise<ProjectSummaryInfo> {
|
||||
const summaryPath = path.join(process.cwd(), '.qwen', 'PROJECT_SUMMARY.md');
|
||||
|
||||
try {
|
||||
await fs.access(summaryPath);
|
||||
} catch {
|
||||
return {
|
||||
hasHistory: false,
|
||||
};
|
||||
}
|
||||
|
||||
try {
|
||||
const content = await fs.readFile(summaryPath, 'utf-8');
|
||||
|
||||
// Extract timestamp if available
|
||||
const timestampMatch = content.match(/\*\*Update time\*\*: (.+)/);
|
||||
|
||||
const timestamp = timestampMatch
|
||||
? timestampMatch[1]
|
||||
: new Date().toISOString();
|
||||
|
||||
// Calculate time ago
|
||||
const getTimeAgo = (timestamp: string) => {
|
||||
const date = new Date(timestamp);
|
||||
const now = new Date();
|
||||
const diffMs = now.getTime() - date.getTime();
|
||||
const diffMinutes = Math.floor(diffMs / (1000 * 60));
|
||||
const diffHours = Math.floor(diffMs / (1000 * 60 * 60));
|
||||
const diffDays = Math.floor(diffMs / (1000 * 60 * 60 * 24));
|
||||
|
||||
if (diffDays > 0) {
|
||||
return `${diffDays} day${diffDays > 1 ? 's' : ''} ago`;
|
||||
} else if (diffHours > 0) {
|
||||
return `${diffHours} hour${diffHours > 1 ? 's' : ''} ago`;
|
||||
} else if (diffMinutes > 0) {
|
||||
return `${diffMinutes} minute${diffMinutes > 1 ? 's' : ''} ago`;
|
||||
} else {
|
||||
return 'just now';
|
||||
}
|
||||
};
|
||||
|
||||
const timeAgo = getTimeAgo(timestamp);
|
||||
|
||||
// Parse Overall Goal section
|
||||
const goalSection = content.match(
|
||||
/## Overall Goal\s*\n?([\s\S]*?)(?=\n## |$)/,
|
||||
);
|
||||
const goalContent = goalSection ? goalSection[1].trim() : '';
|
||||
|
||||
// Parse Current Plan section
|
||||
const planSection = content.match(
|
||||
/## Current Plan\s*\n?([\s\S]*?)(?=\n## |$)/,
|
||||
);
|
||||
const planContent = planSection ? planSection[1] : '';
|
||||
const planLines = planContent.split('\n').filter((line) => line.trim());
|
||||
const doneCount = planLines.filter((line) =>
|
||||
line.includes('[DONE]'),
|
||||
).length;
|
||||
const inProgressCount = planLines.filter((line) =>
|
||||
line.includes('[IN PROGRESS]'),
|
||||
).length;
|
||||
const todoCount = planLines.filter((line) =>
|
||||
line.includes('[TODO]'),
|
||||
).length;
|
||||
const totalTasks = doneCount + inProgressCount + todoCount;
|
||||
|
||||
// Extract pending tasks
|
||||
const pendingTasks = planLines
|
||||
.filter(
|
||||
(line) => line.includes('[TODO]') || line.includes('[IN PROGRESS]'),
|
||||
)
|
||||
.map((line) => line.replace(/^\d+\.\s*/, '').trim())
|
||||
.slice(0, 3);
|
||||
|
||||
return {
|
||||
hasHistory: true,
|
||||
content,
|
||||
timestamp,
|
||||
timeAgo,
|
||||
goalContent,
|
||||
planContent,
|
||||
totalTasks,
|
||||
doneCount,
|
||||
inProgressCount,
|
||||
todoCount,
|
||||
pendingTasks,
|
||||
};
|
||||
} catch (_error) {
|
||||
return {
|
||||
hasHistory: false,
|
||||
};
|
||||
}
|
||||
}
|
||||
262
packages/core/src/utils/subagentGenerator.test.ts
Normal file
262
packages/core/src/utils/subagentGenerator.test.ts
Normal file
@@ -0,0 +1,262 @@
|
||||
/**
|
||||
* @license
|
||||
* Copyright 2025 Qwen
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
import { describe, it, expect, vi, beforeEach, Mock, afterEach } from 'vitest';
|
||||
import { Content, GoogleGenAI, Models } from '@google/genai';
|
||||
import { DEFAULT_QWEN_MODEL } from '../config/models.js';
|
||||
import { GeminiClient } from '../core/client.js';
|
||||
import { Config } from '../config/config.js';
|
||||
import {
|
||||
subagentGenerator,
|
||||
SubagentGeneratedContent,
|
||||
} from './subagentGenerator.js';
|
||||
|
||||
// Mock GeminiClient and Config constructor
|
||||
vi.mock('../core/client.js');
|
||||
vi.mock('../config/config.js');
|
||||
|
||||
// Define mocks for GoogleGenAI and Models instances that will be used across tests
|
||||
const mockModelsInstance = {
|
||||
generateContent: vi.fn(),
|
||||
generateContentStream: vi.fn(),
|
||||
countTokens: vi.fn(),
|
||||
embedContent: vi.fn(),
|
||||
batchEmbedContents: vi.fn(),
|
||||
} as unknown as Models;
|
||||
|
||||
const mockGoogleGenAIInstance = {
|
||||
getGenerativeModel: vi.fn().mockReturnValue(mockModelsInstance),
|
||||
} as unknown as GoogleGenAI;
|
||||
|
||||
vi.mock('@google/genai', async () => {
|
||||
const actualGenAI =
|
||||
await vi.importActual<typeof import('@google/genai')>('@google/genai');
|
||||
return {
|
||||
...actualGenAI,
|
||||
GoogleGenAI: vi.fn(() => mockGoogleGenAIInstance),
|
||||
};
|
||||
});
|
||||
|
||||
describe('subagentGenerator', () => {
|
||||
let mockGeminiClient: GeminiClient;
|
||||
let MockConfig: Mock;
|
||||
const abortSignal = new AbortController().signal;
|
||||
|
||||
beforeEach(() => {
|
||||
MockConfig = vi.mocked(Config);
|
||||
const mockConfigInstance = new MockConfig(
|
||||
'test-api-key',
|
||||
'gemini-pro',
|
||||
false,
|
||||
'.',
|
||||
false,
|
||||
undefined,
|
||||
false,
|
||||
undefined,
|
||||
undefined,
|
||||
undefined,
|
||||
);
|
||||
|
||||
mockGeminiClient = new GeminiClient(mockConfigInstance);
|
||||
|
||||
// Reset mocks before each test to ensure test isolation
|
||||
vi.mocked(mockModelsInstance.generateContent).mockReset();
|
||||
vi.mocked(mockModelsInstance.generateContentStream).mockReset();
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
vi.clearAllMocks();
|
||||
});
|
||||
|
||||
it('should throw error for empty user description', async () => {
|
||||
await expect(
|
||||
subagentGenerator('', mockGeminiClient, abortSignal),
|
||||
).rejects.toThrow('User description cannot be empty');
|
||||
|
||||
await expect(
|
||||
subagentGenerator(' ', mockGeminiClient, abortSignal),
|
||||
).rejects.toThrow('User description cannot be empty');
|
||||
|
||||
expect(mockGeminiClient.generateJson).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should successfully generate content with valid LLM response', async () => {
|
||||
const userDescription = 'help with code reviews and suggestions';
|
||||
const mockApiResponse: SubagentGeneratedContent = {
|
||||
name: 'code-review-assistant',
|
||||
description:
|
||||
'A specialized subagent that helps with code reviews and provides improvement suggestions.',
|
||||
systemPrompt:
|
||||
'You are a code review expert. Analyze code for best practices, bugs, and improvements.',
|
||||
};
|
||||
|
||||
(mockGeminiClient.generateJson as Mock).mockResolvedValue(mockApiResponse);
|
||||
|
||||
const result = await subagentGenerator(
|
||||
userDescription,
|
||||
mockGeminiClient,
|
||||
abortSignal,
|
||||
);
|
||||
|
||||
expect(result).toEqual(mockApiResponse);
|
||||
expect(mockGeminiClient.generateJson).toHaveBeenCalledTimes(1);
|
||||
|
||||
// Verify the call parameters
|
||||
const generateJsonCall = (mockGeminiClient.generateJson as Mock).mock
|
||||
.calls[0];
|
||||
const contents = generateJsonCall[0] as Content[];
|
||||
|
||||
// Should have 1 user message with the query
|
||||
expect(contents).toHaveLength(1);
|
||||
expect(contents[0]?.role).toBe('user');
|
||||
expect(contents[0]?.parts?.[0]?.text).toContain(
|
||||
`Create an agent configuration based on this request: "${userDescription}"`,
|
||||
);
|
||||
|
||||
// Check that system prompt is passed in the config parameter
|
||||
expect(generateJsonCall[2]).toBe(abortSignal);
|
||||
expect(generateJsonCall[3]).toBe(DEFAULT_QWEN_MODEL);
|
||||
expect(generateJsonCall[4]).toEqual(
|
||||
expect.objectContaining({
|
||||
systemInstruction: expect.stringContaining(
|
||||
'You are an elite AI agent architect',
|
||||
),
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
it('should throw error when LLM response is missing required fields', async () => {
|
||||
const userDescription = 'help with documentation';
|
||||
const incompleteResponse = {
|
||||
name: 'doc-helper',
|
||||
description: 'Helps with documentation',
|
||||
// Missing systemPrompt
|
||||
};
|
||||
|
||||
(mockGeminiClient.generateJson as Mock).mockResolvedValue(
|
||||
incompleteResponse,
|
||||
);
|
||||
|
||||
await expect(
|
||||
subagentGenerator(userDescription, mockGeminiClient, abortSignal),
|
||||
).rejects.toThrow('Invalid response from LLM: missing required fields');
|
||||
|
||||
expect(mockGeminiClient.generateJson).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it('should throw error when LLM response has empty fields', async () => {
|
||||
const userDescription = 'database optimization';
|
||||
const emptyFieldsResponse = {
|
||||
name: '',
|
||||
description: 'Helps with database optimization',
|
||||
systemPrompt: 'You are a database expert.',
|
||||
};
|
||||
|
||||
(mockGeminiClient.generateJson as Mock).mockResolvedValue(
|
||||
emptyFieldsResponse,
|
||||
);
|
||||
|
||||
await expect(
|
||||
subagentGenerator(userDescription, mockGeminiClient, abortSignal),
|
||||
).rejects.toThrow('Invalid response from LLM: missing required fields');
|
||||
});
|
||||
|
||||
it('should throw error when generateJson throws an error', async () => {
|
||||
const userDescription = 'testing automation';
|
||||
(mockGeminiClient.generateJson as Mock).mockRejectedValue(
|
||||
new Error('API Error'),
|
||||
);
|
||||
|
||||
await expect(
|
||||
subagentGenerator(userDescription, mockGeminiClient, abortSignal),
|
||||
).rejects.toThrow('API Error');
|
||||
});
|
||||
|
||||
it('should call generateJson with correct schema and model', async () => {
|
||||
const userDescription = 'data analysis';
|
||||
const mockResponse: SubagentGeneratedContent = {
|
||||
name: 'data-analyst',
|
||||
description: 'Analyzes data and provides insights.',
|
||||
systemPrompt: 'You are a data analysis expert.',
|
||||
};
|
||||
|
||||
(mockGeminiClient.generateJson as Mock).mockResolvedValue(mockResponse);
|
||||
|
||||
await subagentGenerator(userDescription, mockGeminiClient, abortSignal);
|
||||
|
||||
expect(mockGeminiClient.generateJson).toHaveBeenCalledWith(
|
||||
expect.any(Array),
|
||||
expect.objectContaining({
|
||||
type: 'object',
|
||||
properties: expect.objectContaining({
|
||||
name: expect.objectContaining({ type: 'string' }),
|
||||
description: expect.objectContaining({ type: 'string' }),
|
||||
systemPrompt: expect.objectContaining({ type: 'string' }),
|
||||
}),
|
||||
required: ['name', 'description', 'systemPrompt'],
|
||||
}),
|
||||
abortSignal,
|
||||
DEFAULT_QWEN_MODEL,
|
||||
expect.objectContaining({
|
||||
systemInstruction: expect.stringContaining(
|
||||
'You are an elite AI agent architect',
|
||||
),
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
it('should include user description in the prompt', async () => {
|
||||
const userDescription = 'machine learning model training';
|
||||
const mockResponse: SubagentGeneratedContent = {
|
||||
name: 'ml-trainer',
|
||||
description: 'Trains machine learning models.',
|
||||
systemPrompt: 'You are an ML expert.',
|
||||
};
|
||||
|
||||
(mockGeminiClient.generateJson as Mock).mockResolvedValue(mockResponse);
|
||||
|
||||
await subagentGenerator(userDescription, mockGeminiClient, abortSignal);
|
||||
|
||||
const generateJsonCall = (mockGeminiClient.generateJson as Mock).mock
|
||||
.calls[0];
|
||||
const contents = generateJsonCall[0] as Content[];
|
||||
|
||||
// Check user query (only message)
|
||||
expect(contents).toHaveLength(1);
|
||||
const userQueryContent = contents[0]?.parts?.[0]?.text;
|
||||
expect(userQueryContent).toContain(userDescription);
|
||||
expect(userQueryContent).toContain(
|
||||
'Create an agent configuration based on this request:',
|
||||
);
|
||||
|
||||
// Check that system prompt is passed in the config parameter
|
||||
expect(generateJsonCall[4]).toEqual(
|
||||
expect.objectContaining({
|
||||
systemInstruction: expect.stringContaining(
|
||||
'You are an elite AI agent architect',
|
||||
),
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
it('should throw error for null response from generateJson', async () => {
|
||||
const userDescription = 'security auditing';
|
||||
(mockGeminiClient.generateJson as Mock).mockResolvedValue(null);
|
||||
|
||||
await expect(
|
||||
subagentGenerator(userDescription, mockGeminiClient, abortSignal),
|
||||
).rejects.toThrow('Invalid response from LLM: missing required fields');
|
||||
});
|
||||
|
||||
it('should throw error for undefined response from generateJson', async () => {
|
||||
const userDescription = 'api documentation';
|
||||
(mockGeminiClient.generateJson as Mock).mockResolvedValue(undefined);
|
||||
|
||||
await expect(
|
||||
subagentGenerator(userDescription, mockGeminiClient, abortSignal),
|
||||
).rejects.toThrow('Invalid response from LLM: missing required fields');
|
||||
});
|
||||
});
|
||||
148
packages/core/src/utils/subagentGenerator.ts
Normal file
148
packages/core/src/utils/subagentGenerator.ts
Normal file
@@ -0,0 +1,148 @@
|
||||
/**
|
||||
* @license
|
||||
* Copyright 2025 Qwen
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
import { Content } from '@google/genai';
|
||||
import { DEFAULT_QWEN_MODEL } from '../config/models.js';
|
||||
import { GeminiClient } from '../core/client.js';
|
||||
|
||||
const SYSTEM_PROMPT = `You are an elite AI agent architect specializing in crafting high-performance agent configurations. Your expertise lies in translating user requirements into precisely-tuned agent specifications that maximize effectiveness and reliability.
|
||||
|
||||
**Important Context**: You may have access to project-specific instructions from QWEN.md files and other context that may include coding standards, project structure, and custom requirements. Consider this context when creating agents to ensure they align with the project's established patterns and practices.
|
||||
|
||||
When a user describes what they want an agent to do, you will:
|
||||
|
||||
1. **Extract Core Intent**: Identify the fundamental purpose, key responsibilities, and success criteria for the agent. Look for both explicit requirements and implicit needs. Consider any project-specific context from QWEN.md files. For agents that are meant to review code, you should assume that the user is asking to review recently written code and not the whole codebase, unless the user has explicitly instructed you otherwise.
|
||||
|
||||
2. **Design Expert Persona**: Create a compelling expert identity that embodies deep domain knowledge relevant to the task. The persona should inspire confidence and guide the agent's decision-making approach.
|
||||
|
||||
3. **Architect Comprehensive Instructions**: Develop a system prompt that:
|
||||
- Establishes clear behavioral boundaries and operational parameters
|
||||
- Provides specific methodologies and best practices for task execution
|
||||
- Anticipates edge cases and provides guidance for handling them
|
||||
- Incorporates any specific requirements or preferences mentioned by the user
|
||||
- Defines output format expectations when relevant
|
||||
- Aligns with project-specific coding standards and patterns from QWEN.md
|
||||
|
||||
4. **Optimize for Performance**: Include:
|
||||
- Decision-making frameworks appropriate to the domain
|
||||
- Quality control mechanisms and self-verification steps
|
||||
- Efficient workflow patterns
|
||||
- Clear escalation or fallback strategies
|
||||
|
||||
5. **Create Identifier**: Design a concise, descriptive identifier that:
|
||||
- Uses lowercase letters, numbers, and hyphens only
|
||||
- Is typically 2-4 words joined by hyphens
|
||||
- Clearly indicates the agent's primary function
|
||||
- Is memorable and easy to type
|
||||
- Avoids generic terms like "helper" or "assistant"
|
||||
|
||||
6 **Example agent descriptions**:
|
||||
- in the 'whenToUse' field of the JSON object, you should include examples of when this agent should be used.
|
||||
- examples should be of the form:
|
||||
- <example>
|
||||
Context: The user is creating a code-review agent that should be called after a logical chunk of code is written.
|
||||
user: "Please write a function that checks if a number is prime"
|
||||
assistant: "Here is the relevant function: "
|
||||
<function call omitted for brevity only for this example>
|
||||
<commentary>
|
||||
Since the user is greeting, use the Task tool to launch the greeting-responder agent to respond with a friendly joke.
|
||||
</commentary>
|
||||
assistant: "Now let me use the code-reviewer agent to review the code"
|
||||
</example>
|
||||
- <example>
|
||||
Context: User is creating an agent to respond to the word "hello" with a friendly jok.
|
||||
user: "Hello"
|
||||
assistant: "I'm going to use the Task tool to launch the greeting-responder agent to respond with a friendly joke"
|
||||
<commentary>
|
||||
Since the user is greeting, use the greeting-responder agent to respond with a friendly joke.
|
||||
</commentary>
|
||||
</example>
|
||||
- If the user mentioned or implied that the agent should be used proactively, you should include examples of this.
|
||||
- NOTE: Ensure that in the examples, you are making the assistant use the Agent tool and not simply respond directly to the task.
|
||||
|
||||
Key principles for your system prompts:
|
||||
- Be specific rather than generic - avoid vague instructions
|
||||
- Include concrete examples when they would clarify behavior
|
||||
- Balance comprehensiveness with clarity - every instruction should add value
|
||||
- Ensure the agent has enough context to handle variations of the core task
|
||||
- Make the agent proactive in seeking clarification when needed
|
||||
- Build in quality assurance and self-correction mechanisms
|
||||
|
||||
Remember: The agents you create should be autonomous experts capable of handling their designated tasks with minimal additional guidance. Your system prompts are their complete operational manual.
|
||||
`;
|
||||
|
||||
const createUserPrompt = (userInput: string): string =>
|
||||
`Create an agent configuration based on this request: "${userInput}"`;
|
||||
|
||||
const RESPONSE_SCHEMA: Record<string, unknown> = {
|
||||
type: 'object',
|
||||
properties: {
|
||||
name: {
|
||||
type: 'string',
|
||||
description:
|
||||
"A unique, descriptive identifier using lowercase letters, numbers, and hyphens (e.g., 'code-reviewer', 'api-docs-writer', 'test-generator')",
|
||||
},
|
||||
description: {
|
||||
type: 'string',
|
||||
description:
|
||||
"A precise, actionable description starting with 'Use this agent when...' that clearly defines the triggering conditions and use cases",
|
||||
},
|
||||
systemPrompt: {
|
||||
type: 'string',
|
||||
description:
|
||||
"The complete system prompt that will govern the agent's behavior, written in second person ('You are...', 'You will...') and structured for maximum clarity and effectiveness",
|
||||
},
|
||||
},
|
||||
required: ['name', 'description', 'systemPrompt'],
|
||||
};
|
||||
|
||||
export interface SubagentGeneratedContent {
|
||||
name: string;
|
||||
description: string;
|
||||
systemPrompt: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates subagent configuration content using LLM.
|
||||
*
|
||||
* @param userDescription - The user's description of what the subagent should do
|
||||
* @param geminiClient - Initialized GeminiClient instance
|
||||
* @param abortSignal - AbortSignal for cancelling the request
|
||||
* @returns Promise resolving to generated subagent content
|
||||
*/
|
||||
export async function subagentGenerator(
|
||||
userDescription: string,
|
||||
geminiClient: GeminiClient,
|
||||
abortSignal: AbortSignal,
|
||||
): Promise<SubagentGeneratedContent> {
|
||||
if (!userDescription.trim()) {
|
||||
throw new Error('User description cannot be empty');
|
||||
}
|
||||
|
||||
const userPrompt = createUserPrompt(userDescription);
|
||||
const contents: Content[] = [{ role: 'user', parts: [{ text: userPrompt }] }];
|
||||
|
||||
const parsedResponse = (await geminiClient.generateJson(
|
||||
contents,
|
||||
RESPONSE_SCHEMA,
|
||||
abortSignal,
|
||||
DEFAULT_QWEN_MODEL,
|
||||
{
|
||||
systemInstruction: SYSTEM_PROMPT,
|
||||
},
|
||||
)) as unknown as SubagentGeneratedContent;
|
||||
|
||||
if (
|
||||
!parsedResponse ||
|
||||
!parsedResponse.name ||
|
||||
!parsedResponse.description ||
|
||||
!parsedResponse.systemPrompt
|
||||
) {
|
||||
throw new Error('Invalid response from LLM: missing required fields');
|
||||
}
|
||||
|
||||
return parsedResponse;
|
||||
}
|
||||
193
packages/core/src/utils/yaml-parser.test.ts
Normal file
193
packages/core/src/utils/yaml-parser.test.ts
Normal file
@@ -0,0 +1,193 @@
|
||||
/**
|
||||
* @license
|
||||
* Copyright 2025 Qwen
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import { parse, stringify } from './yaml-parser.js';
|
||||
|
||||
describe('yaml-parser', () => {
|
||||
describe('parse', () => {
|
||||
it('should parse simple key-value pairs', () => {
|
||||
const yaml = 'name: test\ndescription: A test config';
|
||||
const result = parse(yaml);
|
||||
expect(result).toEqual({
|
||||
name: 'test',
|
||||
description: 'A test config',
|
||||
});
|
||||
});
|
||||
|
||||
it('should parse arrays', () => {
|
||||
const yaml = 'tools:\n - file\n - shell';
|
||||
const result = parse(yaml);
|
||||
expect(result).toEqual({
|
||||
tools: ['file', 'shell'],
|
||||
});
|
||||
});
|
||||
|
||||
it('should parse nested objects', () => {
|
||||
const yaml = 'modelConfig:\n temperature: 0.7\n maxTokens: 1000';
|
||||
const result = parse(yaml);
|
||||
expect(result).toEqual({
|
||||
modelConfig: {
|
||||
temperature: 0.7,
|
||||
maxTokens: 1000,
|
||||
},
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('stringify', () => {
|
||||
it('should stringify simple objects', () => {
|
||||
const obj = { name: 'test', description: 'A test config' };
|
||||
const result = stringify(obj);
|
||||
expect(result).toBe('name: test\ndescription: A test config');
|
||||
});
|
||||
|
||||
it('should stringify arrays', () => {
|
||||
const obj = { tools: ['file', 'shell'] };
|
||||
const result = stringify(obj);
|
||||
expect(result).toBe('tools:\n - file\n - shell');
|
||||
});
|
||||
|
||||
it('should stringify nested objects', () => {
|
||||
const obj = {
|
||||
modelConfig: {
|
||||
temperature: 0.7,
|
||||
maxTokens: 1000,
|
||||
},
|
||||
};
|
||||
const result = stringify(obj);
|
||||
expect(result).toBe(
|
||||
'modelConfig:\n temperature: 0.7\n maxTokens: 1000',
|
||||
);
|
||||
});
|
||||
|
||||
describe('string escaping security', () => {
|
||||
it('should properly escape strings with quotes', () => {
|
||||
const obj = { key: 'value with "quotes"' };
|
||||
const result = stringify(obj);
|
||||
expect(result).toBe('key: "value with \\"quotes\\""');
|
||||
});
|
||||
|
||||
it('should properly escape strings with backslashes', () => {
|
||||
const obj = { key: 'value with \\ backslash' };
|
||||
const result = stringify(obj);
|
||||
expect(result).toBe('key: "value with \\\\ backslash"');
|
||||
});
|
||||
|
||||
it('should properly escape strings with backslash-quote sequences', () => {
|
||||
// This is the critical security test case
|
||||
const obj = { key: 'value with \\" sequence' };
|
||||
const result = stringify(obj);
|
||||
// Should escape backslashes first, then quotes
|
||||
expect(result).toBe('key: "value with \\\\\\" sequence"');
|
||||
});
|
||||
|
||||
it('should handle complex escaping scenarios', () => {
|
||||
const testCases = [
|
||||
{
|
||||
input: { path: 'C:\\Program Files\\"App"\\file.txt' },
|
||||
expected: 'path: "C:\\\\Program Files\\\\\\"App\\"\\\\file.txt"',
|
||||
},
|
||||
{
|
||||
input: { message: 'He said: \\"Hello\\"' },
|
||||
expected: 'message: "He said: \\\\\\"Hello\\\\\\""',
|
||||
},
|
||||
{
|
||||
input: { complex: 'Multiple \\\\ backslashes \\" and " quotes' },
|
||||
expected:
|
||||
'complex: "Multiple \\\\\\\\ backslashes \\\\\\" and \\" quotes"',
|
||||
},
|
||||
];
|
||||
|
||||
testCases.forEach(({ input, expected }) => {
|
||||
const result = stringify(input);
|
||||
expect(result).toBe(expected);
|
||||
});
|
||||
});
|
||||
|
||||
it('should maintain round-trip integrity for escaped strings', () => {
|
||||
const testStrings = [
|
||||
'simple string',
|
||||
'string with "quotes"',
|
||||
'string with \\ backslash',
|
||||
'string with \\" sequence',
|
||||
'path\\to\\"file".txt',
|
||||
'He said: \\"Hello\\"',
|
||||
'Multiple \\\\ backslashes \\" and " quotes',
|
||||
];
|
||||
|
||||
testStrings.forEach((testString) => {
|
||||
// Force quoting by adding a colon
|
||||
const originalObj = { key: testString + ':' };
|
||||
const yamlString = stringify(originalObj);
|
||||
const parsedObj = parse(yamlString);
|
||||
expect(parsedObj).toEqual(originalObj);
|
||||
});
|
||||
});
|
||||
|
||||
it('should not quote strings that do not need quoting', () => {
|
||||
const obj = { key: 'simplevalue' };
|
||||
const result = stringify(obj);
|
||||
expect(result).toBe('key: simplevalue');
|
||||
});
|
||||
|
||||
it('should quote strings with colons', () => {
|
||||
const obj = { key: 'value:with:colons' };
|
||||
const result = stringify(obj);
|
||||
expect(result).toBe('key: "value:with:colons"');
|
||||
});
|
||||
|
||||
it('should quote strings with hash symbols', () => {
|
||||
const obj = { key: 'value#with#hash' };
|
||||
const result = stringify(obj);
|
||||
expect(result).toBe('key: "value#with#hash"');
|
||||
});
|
||||
|
||||
it('should quote strings with leading/trailing whitespace', () => {
|
||||
const obj = { key: ' value with spaces ' };
|
||||
const result = stringify(obj);
|
||||
expect(result).toBe('key: " value with spaces "');
|
||||
});
|
||||
});
|
||||
|
||||
describe('numeric string handling', () => {
|
||||
it('should parse unquoted numeric values as numbers', () => {
|
||||
const yaml = 'name: 11\ndescription: 333';
|
||||
const result = parse(yaml);
|
||||
expect(result).toEqual({
|
||||
name: 11,
|
||||
description: 333,
|
||||
});
|
||||
expect(typeof result['name']).toBe('number');
|
||||
expect(typeof result['description']).toBe('number');
|
||||
});
|
||||
|
||||
it('should parse quoted numeric values as strings', () => {
|
||||
const yaml = 'name: "11"\ndescription: "333"';
|
||||
const result = parse(yaml);
|
||||
expect(result).toEqual({
|
||||
name: '11',
|
||||
description: '333',
|
||||
});
|
||||
expect(typeof result['name']).toBe('string');
|
||||
expect(typeof result['description']).toBe('string');
|
||||
});
|
||||
|
||||
it('should handle mixed numeric and string values', () => {
|
||||
const yaml = 'name: "11"\nage: 25\ndescription: "333"';
|
||||
const result = parse(yaml);
|
||||
expect(result).toEqual({
|
||||
name: '11',
|
||||
age: 25,
|
||||
description: '333',
|
||||
});
|
||||
expect(typeof result['name']).toBe('string');
|
||||
expect(typeof result['age']).toBe('number');
|
||||
expect(typeof result['description']).toBe('string');
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
192
packages/core/src/utils/yaml-parser.ts
Normal file
192
packages/core/src/utils/yaml-parser.ts
Normal file
@@ -0,0 +1,192 @@
|
||||
/**
|
||||
* @license
|
||||
* Copyright 2025 Qwen
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
/**
|
||||
* Simple YAML parser for subagent frontmatter.
|
||||
* This is a minimal implementation that handles the basic YAML structures
|
||||
* needed for subagent configuration files.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Parses a simple YAML string into a JavaScript object.
|
||||
* Supports basic key-value pairs, arrays, and nested objects.
|
||||
*
|
||||
* @param yamlString - YAML string to parse
|
||||
* @returns Parsed object
|
||||
*/
|
||||
export function parse(yamlString: string): Record<string, unknown> {
|
||||
const lines = yamlString
|
||||
.split('\n')
|
||||
.filter((line) => line.trim() && !line.trim().startsWith('#'));
|
||||
const result: Record<string, unknown> = {};
|
||||
|
||||
let currentKey = '';
|
||||
let currentArray: unknown[] = [];
|
||||
let inArray = false;
|
||||
let currentObject: Record<string, unknown> = {};
|
||||
let inObject = false;
|
||||
let objectKey = '';
|
||||
|
||||
for (let i = 0; i < lines.length; i++) {
|
||||
const line = lines[i];
|
||||
|
||||
// Handle array items
|
||||
if (line.startsWith(' - ')) {
|
||||
if (!inArray) {
|
||||
inArray = true;
|
||||
currentArray = [];
|
||||
}
|
||||
const itemRaw = line.substring(4).trim();
|
||||
currentArray.push(parseValue(itemRaw));
|
||||
continue;
|
||||
}
|
||||
|
||||
// End of array
|
||||
if (inArray && !line.startsWith(' - ')) {
|
||||
result[currentKey] = currentArray;
|
||||
inArray = false;
|
||||
currentArray = [];
|
||||
currentKey = '';
|
||||
}
|
||||
|
||||
// Handle nested object items (simple indentation)
|
||||
if (line.startsWith(' ') && inObject) {
|
||||
const [key, ...valueParts] = line.trim().split(':');
|
||||
const value = valueParts.join(':').trim();
|
||||
currentObject[key.trim()] = parseValue(value);
|
||||
continue;
|
||||
}
|
||||
|
||||
// End of object
|
||||
if (inObject && !line.startsWith(' ')) {
|
||||
result[objectKey] = currentObject;
|
||||
inObject = false;
|
||||
currentObject = {};
|
||||
objectKey = '';
|
||||
}
|
||||
|
||||
// Handle key-value pairs
|
||||
if (line.includes(':')) {
|
||||
const [key, ...valueParts] = line.split(':');
|
||||
const value = valueParts.join(':').trim();
|
||||
|
||||
if (value === '') {
|
||||
// This might be the start of an object or array
|
||||
currentKey = key.trim();
|
||||
|
||||
// Look ahead to determine if this is an array or object
|
||||
if (i + 1 < lines.length) {
|
||||
const nextLine = lines[i + 1];
|
||||
if (nextLine.startsWith(' - ')) {
|
||||
// Next line is an array item, so this will be handled in the next iteration
|
||||
continue;
|
||||
} else if (nextLine.startsWith(' ')) {
|
||||
// Next line is indented, so this is an object
|
||||
inObject = true;
|
||||
objectKey = currentKey;
|
||||
currentObject = {};
|
||||
currentKey = '';
|
||||
continue;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
result[key.trim()] = parseValue(value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Handle remaining array or object
|
||||
if (inArray) {
|
||||
result[currentKey] = currentArray;
|
||||
}
|
||||
if (inObject) {
|
||||
result[objectKey] = currentObject;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts a JavaScript object to a simple YAML string.
|
||||
*
|
||||
* @param obj - Object to stringify
|
||||
* @param options - Stringify options
|
||||
* @returns YAML string
|
||||
*/
|
||||
export function stringify(
|
||||
obj: Record<string, unknown>,
|
||||
_options?: { lineWidth?: number; minContentWidth?: number },
|
||||
): string {
|
||||
const lines: string[] = [];
|
||||
|
||||
for (const [key, value] of Object.entries(obj)) {
|
||||
if (Array.isArray(value)) {
|
||||
lines.push(`${key}:`);
|
||||
for (const item of value) {
|
||||
lines.push(` - ${formatValue(item)}`);
|
||||
}
|
||||
} else if (typeof value === 'object' && value !== null) {
|
||||
lines.push(`${key}:`);
|
||||
for (const [subKey, subValue] of Object.entries(
|
||||
value as Record<string, unknown>,
|
||||
)) {
|
||||
lines.push(` ${subKey}: ${formatValue(subValue)}`);
|
||||
}
|
||||
} else {
|
||||
lines.push(`${key}: ${formatValue(value)}`);
|
||||
}
|
||||
}
|
||||
|
||||
return lines.join('\n');
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses a value string into appropriate JavaScript type.
|
||||
*/
|
||||
function parseValue(value: string): unknown {
|
||||
if (value === 'true') return true;
|
||||
if (value === 'false') return false;
|
||||
if (value === 'null') return null;
|
||||
if (value === '') return '';
|
||||
|
||||
// Handle quoted strings
|
||||
if (value.startsWith('"') && value.endsWith('"') && value.length >= 2) {
|
||||
const unquoted = value.slice(1, -1);
|
||||
// Unescape quotes and backslashes
|
||||
return unquoted.replace(/\\"/g, '"').replace(/\\\\/g, '\\');
|
||||
}
|
||||
|
||||
// Try to parse as number
|
||||
const num = Number(value);
|
||||
if (!isNaN(num) && isFinite(num)) {
|
||||
return num;
|
||||
}
|
||||
|
||||
// Return as string
|
||||
return value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Formats a value for YAML output.
|
||||
*/
|
||||
function formatValue(value: unknown): string {
|
||||
if (typeof value === 'string') {
|
||||
// Quote strings that might be ambiguous or contain special characters
|
||||
if (
|
||||
value.includes(':') ||
|
||||
value.includes('#') ||
|
||||
value.includes('"') ||
|
||||
value.includes('\\') ||
|
||||
value.trim() !== value
|
||||
) {
|
||||
// Escape backslashes THEN quotes
|
||||
return `"${value.replace(/\\/g, '\\\\').replace(/"/g, '\\"')}"`;
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
return String(value);
|
||||
}
|
||||
Reference in New Issue
Block a user