@denizokcu/haze 0.6.0 → 0.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +19 -0
- package/README.md +59 -12
- package/dist/cli/chat/TaskBar.d.ts +9 -0
- package/dist/cli/chat/TaskBar.js +40 -0
- package/dist/cli/chat/chatMetrics.d.ts +11 -0
- package/dist/cli/chat/chatMetrics.js +50 -0
- package/dist/cli/chat/inputSuggestions.d.ts +16 -0
- package/dist/cli/chat/inputSuggestions.js +55 -0
- package/dist/cli/chat/messages.d.ts +9 -0
- package/dist/cli/chat/messages.js +76 -0
- package/dist/cli/chat/sessionRecorder.d.ts +12 -0
- package/dist/cli/chat/sessionRecorder.js +21 -0
- package/dist/cli/chat/startupInfo.d.ts +4 -0
- package/dist/cli/chat/startupInfo.js +53 -0
- package/dist/cli/chat/turnState.d.ts +5 -0
- package/dist/cli/chat/turnState.js +20 -0
- package/dist/cli/commands/chat.d.ts +0 -1
- package/dist/cli/commands/chat.js +293 -894
- package/dist/cli/commands/chatModes.d.ts +8 -0
- package/dist/cli/commands/chatModes.js +44 -0
- package/dist/cli/commands/commandHelp.d.ts +6 -0
- package/dist/cli/commands/commandHelp.js +25 -0
- package/dist/cli/commands/commands.d.ts +1 -1
- package/dist/cli/commands/commands.js +41 -267
- package/dist/cli/commands/formatters.js +12 -4
- package/dist/cli/commands/initCommand.d.ts +2 -0
- package/dist/cli/commands/initCommand.js +19 -0
- package/dist/cli/commands/logsCommand.d.ts +2 -0
- package/dist/cli/commands/logsCommand.js +63 -0
- package/dist/cli/commands/lspWizard.d.ts +14 -0
- package/dist/cli/commands/lspWizard.js +44 -0
- package/dist/cli/commands/mcpWizard.d.ts +15 -0
- package/dist/cli/commands/mcpWizard.js +69 -0
- package/dist/cli/commands/modelCommand.d.ts +2 -0
- package/dist/cli/commands/modelCommand.js +36 -0
- package/dist/cli/commands/providerWizard.d.ts +30 -0
- package/dist/cli/commands/providerWizard.js +114 -0
- package/dist/cli/commands/runCommand.d.ts +16 -0
- package/dist/cli/commands/runCommand.js +170 -0
- package/dist/cli/commands/settingsSummary.d.ts +3 -0
- package/dist/cli/commands/settingsSummary.js +40 -0
- package/dist/cli/commands/skillConfirmRemove.d.ts +11 -0
- package/dist/cli/commands/skillConfirmRemove.js +19 -0
- package/dist/cli/commands/skillCreation.d.ts +12 -0
- package/dist/cli/commands/skillCreation.js +14 -0
- package/dist/cli/commands/skillWizard.d.ts +15 -0
- package/dist/cli/commands/skillWizard.js +38 -0
- package/dist/cli/commands/streaming/assistantText.d.ts +17 -0
- package/dist/cli/commands/streaming/assistantText.js +115 -0
- package/dist/cli/commands/streaming/toolGroupRenderer.d.ts +55 -0
- package/dist/cli/commands/streaming/toolGroupRenderer.js +102 -0
- package/dist/cli/commands/streaming/toolResultState.d.ts +13 -0
- package/dist/cli/commands/streaming/toolResultState.js +25 -0
- package/dist/cli/commands/streaming/turnRuntime.d.ts +78 -0
- package/dist/cli/commands/streaming/turnRuntime.js +86 -0
- package/dist/cli/commands/streaming.d.ts +9 -25
- package/dist/cli/commands/streaming.js +82 -347
- package/dist/cli/commands/wizardActions.d.ts +40 -0
- package/dist/cli/commands/wizardActions.js +40 -0
- package/dist/cli/commands/wizardInput.d.ts +4 -0
- package/dist/cli/commands/wizardInput.js +19 -0
- package/dist/cli/commands/wizardPrompts.d.ts +22 -0
- package/dist/cli/commands/wizardPrompts.js +52 -0
- package/dist/cli/commands/wizardSuggestions.d.ts +23 -0
- package/dist/cli/commands/wizardSuggestions.js +139 -0
- package/dist/cli/index.js +62 -4
- package/dist/config/contextFiles.d.ts +8 -1
- package/dist/config/contextFiles.js +25 -12
- package/dist/config/lspSettings.js +3 -6
- package/dist/config/mcpSettings.js +5 -5
- package/dist/config/providers.js +4 -4
- package/dist/config/skillSettings.js +4 -3
- package/dist/core/agent/budgets.d.ts +6 -0
- package/dist/core/agent/budgets.js +6 -0
- package/dist/core/agent/compaction.js +1 -1
- package/dist/core/agent/requestAssembly.d.ts +0 -7
- package/dist/core/agent/requestAssembly.js +27 -5
- package/dist/core/agent/toolOutputStore.js +5 -4
- package/dist/core/agent/toolResults.d.ts +31 -0
- package/dist/core/agent/toolResults.js +42 -0
- package/dist/core/agent/turnPolicy.d.ts +8 -0
- package/dist/core/agent/turnPolicy.js +23 -0
- package/dist/core/agent/workState.d.ts +18 -5
- package/dist/core/agent/workState.js +39 -20
- package/dist/core/bashOutput/reducers/content.d.ts +1 -0
- package/dist/core/bashOutput/reducers/content.js +1 -1
- package/dist/core/bashOutput/reducers/gh.d.ts +1 -0
- package/dist/core/bashOutput/reducers/gh.js +77 -0
- package/dist/core/bashOutput/registry.js +8 -0
- package/dist/core/goal/sessionGoal.d.ts +6 -30
- package/dist/core/goal/sessionGoal.js +3 -68
- package/dist/core/safety/bashClassifier.js +30 -0
- package/dist/core/safety/urlGuard.d.ts +1 -0
- package/dist/core/safety/urlGuard.js +6 -1
- package/dist/core/session/sessionSlimming.d.ts +5 -0
- package/dist/core/session/sessionSlimming.js +78 -0
- package/dist/core/session/sessionStore.d.ts +23 -3
- package/dist/core/session/sessionStore.js +30 -10
- package/dist/core/subagent/subagentRunner.d.ts +11 -1
- package/dist/core/subagent/subagentRunner.js +1 -0
- package/dist/llm/capabilities.d.ts +12 -0
- package/dist/llm/capabilities.js +10 -0
- package/dist/llm/client.d.ts +1 -0
- package/dist/llm/client.js +11 -2
- package/dist/llm/hazeTools.d.ts +31 -32
- package/dist/llm/hazeTools.js +40 -567
- package/dist/llm/lsp.d.ts +46 -1
- package/dist/llm/lsp.js +9 -9
- package/dist/llm/lspTools.d.ts +2 -20
- package/dist/llm/mcp.js +9 -0
- package/dist/llm/requestContext.js +7 -22
- package/dist/llm/systemPrompt.d.ts +1 -0
- package/dist/llm/systemPrompt.js +1 -1
- package/dist/llm/tools/bashTool.d.ts +5 -0
- package/dist/llm/tools/bashTool.js +81 -0
- package/dist/llm/tools/editMatch.d.ts +55 -0
- package/dist/llm/tools/editMatch.js +99 -0
- package/dist/llm/tools/failures.d.ts +36 -0
- package/dist/llm/tools/failures.js +38 -0
- package/dist/llm/tools/fetchTool.d.ts +65 -0
- package/dist/llm/tools/fetchTool.js +45 -0
- package/dist/llm/tools/fileToolShared.d.ts +10 -0
- package/dist/llm/tools/fileToolShared.js +43 -0
- package/dist/llm/tools/grepParse.d.ts +13 -0
- package/dist/llm/tools/grepParse.js +67 -0
- package/dist/llm/tools/outputCap.d.ts +53 -0
- package/dist/llm/tools/outputCap.js +67 -0
- package/dist/llm/tools/storedOutputTool.d.ts +10 -0
- package/dist/llm/tools/storedOutputTool.js +17 -0
- package/dist/llm/tools/taskTool.d.ts +28 -0
- package/dist/llm/tools/taskTool.js +41 -0
- package/dist/llm/tools/toolContext.d.ts +69 -0
- package/dist/llm/tools/toolContext.js +187 -0
- package/dist/llm/tools/workspaceFile.d.ts +33 -0
- package/dist/llm/tools/workspaceFile.js +27 -0
- package/dist/llm/webFetch.d.ts +13 -1
- package/dist/llm/webFetch.js +91 -7
- package/dist/skills/builder/SkillBuilder.d.ts +3 -0
- package/dist/skills/builder/SkillBuilder.js +1 -1
- package/dist/ui/components/TextInput.js +1 -99
- package/dist/ui/inputBuffer.d.ts +28 -0
- package/dist/ui/inputBuffer.js +99 -0
- package/dist/utils/collections.d.ts +6 -0
- package/dist/utils/collections.js +9 -0
- package/dist/utils/path.d.ts +2 -0
- package/dist/utils/path.js +40 -0
- package/package.json +1 -2
package/dist/llm/lsp.d.ts
CHANGED
|
@@ -1,8 +1,16 @@
|
|
|
1
|
+
import { type ChildProcessWithoutNullStreams } from 'node:child_process';
|
|
1
2
|
import type { HazeLspServer } from '../config/lspSettings.js';
|
|
3
|
+
type Json = null | boolean | number | string | Json[] | {
|
|
4
|
+
[key: string]: Json;
|
|
5
|
+
};
|
|
2
6
|
export declare class LspError extends Error {
|
|
3
7
|
constructor(message: string);
|
|
4
8
|
}
|
|
5
|
-
declare function
|
|
9
|
+
export declare function languageId(filePath: string): string;
|
|
10
|
+
export declare function toUri(absolutePath: string): string;
|
|
11
|
+
export declare function fromUri(uri: string): string;
|
|
12
|
+
export declare function isObject(value: unknown): value is Record<string, unknown>;
|
|
13
|
+
export declare function asRange(value: unknown): {
|
|
6
14
|
start: {
|
|
7
15
|
line: number;
|
|
8
16
|
character: number;
|
|
@@ -12,7 +20,44 @@ declare function asRange(value: unknown): {
|
|
|
12
20
|
character: number;
|
|
13
21
|
};
|
|
14
22
|
} | undefined;
|
|
23
|
+
export declare function locationToResult(value: unknown): {
|
|
24
|
+
path: string;
|
|
25
|
+
range: {
|
|
26
|
+
start: {
|
|
27
|
+
line: number;
|
|
28
|
+
character: number;
|
|
29
|
+
};
|
|
30
|
+
end: {
|
|
31
|
+
line: number;
|
|
32
|
+
character: number;
|
|
33
|
+
};
|
|
34
|
+
};
|
|
35
|
+
} | undefined;
|
|
36
|
+
export declare function flattenSymbols(symbols: unknown[], filePath: string, limit: number): {
|
|
37
|
+
name: string;
|
|
38
|
+
kind?: number;
|
|
39
|
+
path: string;
|
|
40
|
+
range?: ReturnType<typeof asRange>;
|
|
41
|
+
selectionRange?: ReturnType<typeof asRange>;
|
|
42
|
+
}[];
|
|
15
43
|
export declare function pickLspServer(servers: HazeLspServer[], filePath: string): HazeLspServer | undefined;
|
|
44
|
+
export declare class StdioLspClient {
|
|
45
|
+
private server;
|
|
46
|
+
private child;
|
|
47
|
+
private id;
|
|
48
|
+
private buffer;
|
|
49
|
+
private pending;
|
|
50
|
+
constructor(server: HazeLspServer, child: ChildProcessWithoutNullStreams);
|
|
51
|
+
static start(server: HazeLspServer): StdioLspClient;
|
|
52
|
+
private rejectAll;
|
|
53
|
+
private onData;
|
|
54
|
+
private send;
|
|
55
|
+
request(method: string, params?: Json, timeoutMs?: number): Promise<unknown>;
|
|
56
|
+
notify(method: string, params?: Json): void;
|
|
57
|
+
initialize(): Promise<void>;
|
|
58
|
+
openDocument(absolutePath: string): Promise<void>;
|
|
59
|
+
close(): Promise<void>;
|
|
60
|
+
}
|
|
16
61
|
export declare function lspDocumentSymbols(server: HazeLspServer, filePath: string, limit: number): Promise<{
|
|
17
62
|
name: string;
|
|
18
63
|
kind?: number;
|
package/dist/llm/lsp.js
CHANGED
|
@@ -9,7 +9,7 @@ export class LspError extends Error {
|
|
|
9
9
|
this.name = 'LspError';
|
|
10
10
|
}
|
|
11
11
|
}
|
|
12
|
-
function languageId(filePath) {
|
|
12
|
+
export function languageId(filePath) {
|
|
13
13
|
const ext = path.extname(filePath).toLowerCase();
|
|
14
14
|
if (ext === '.ts')
|
|
15
15
|
return 'typescript';
|
|
@@ -27,18 +27,18 @@ function languageId(filePath) {
|
|
|
27
27
|
return 'go';
|
|
28
28
|
return ext.replace(/^\./, '') || 'plaintext';
|
|
29
29
|
}
|
|
30
|
-
function toUri(absolutePath) {
|
|
30
|
+
export function toUri(absolutePath) {
|
|
31
31
|
return pathToFileURL(absolutePath).toString();
|
|
32
32
|
}
|
|
33
|
-
function fromUri(uri) {
|
|
33
|
+
export function fromUri(uri) {
|
|
34
34
|
if (!uri.startsWith('file://'))
|
|
35
35
|
return uri;
|
|
36
36
|
return workspaceRelativePath(new URL(uri).pathname);
|
|
37
37
|
}
|
|
38
|
-
function isObject(value) {
|
|
38
|
+
export function isObject(value) {
|
|
39
39
|
return typeof value === 'object' && value != null && !Array.isArray(value);
|
|
40
40
|
}
|
|
41
|
-
function asRange(value) {
|
|
41
|
+
export function asRange(value) {
|
|
42
42
|
if (!isObject(value) || !isObject(value.start) || !isObject(value.end))
|
|
43
43
|
return undefined;
|
|
44
44
|
const start = value.start;
|
|
@@ -48,16 +48,16 @@ function asRange(value) {
|
|
|
48
48
|
end: { line: typeof end.line === 'number' ? end.line + 1 : 1, character: typeof end.character === 'number' ? end.character + 1 : 1 },
|
|
49
49
|
};
|
|
50
50
|
}
|
|
51
|
-
function locationToResult(value) {
|
|
51
|
+
export function locationToResult(value) {
|
|
52
52
|
if (!isObject(value))
|
|
53
53
|
return undefined;
|
|
54
|
-
const uri = typeof value.uri === 'string' ? value.uri : (
|
|
54
|
+
const uri = typeof value.uri === 'string' ? value.uri : (typeof value.targetUri === 'string' ? value.targetUri : undefined);
|
|
55
55
|
const range = asRange(value.range ?? value.targetSelectionRange ?? value.targetRange);
|
|
56
56
|
if (!uri || !range)
|
|
57
57
|
return undefined;
|
|
58
58
|
return { path: fromUri(uri), range };
|
|
59
59
|
}
|
|
60
|
-
function flattenSymbols(symbols, filePath, limit) {
|
|
60
|
+
export function flattenSymbols(symbols, filePath, limit) {
|
|
61
61
|
const out = [];
|
|
62
62
|
const visit = (items) => {
|
|
63
63
|
for (const item of items) {
|
|
@@ -83,7 +83,7 @@ export function pickLspServer(servers, filePath) {
|
|
|
83
83
|
const ext = path.extname(filePath).toLowerCase();
|
|
84
84
|
return servers.find(server => server.enabled !== false && (server.extensions ?? []).map(e => e.toLowerCase()).includes(ext));
|
|
85
85
|
}
|
|
86
|
-
class StdioLspClient {
|
|
86
|
+
export class StdioLspClient {
|
|
87
87
|
server;
|
|
88
88
|
child;
|
|
89
89
|
id = 0;
|
package/dist/llm/lspTools.d.ts
CHANGED
|
@@ -41,26 +41,8 @@ export declare const lspTools: {
|
|
|
41
41
|
name: string;
|
|
42
42
|
kind?: number;
|
|
43
43
|
path: string;
|
|
44
|
-
range?: ReturnType<(
|
|
45
|
-
|
|
46
|
-
line: number;
|
|
47
|
-
character: number;
|
|
48
|
-
};
|
|
49
|
-
end: {
|
|
50
|
-
line: number;
|
|
51
|
-
character: number;
|
|
52
|
-
};
|
|
53
|
-
} | undefined>;
|
|
54
|
-
selectionRange?: ReturnType<(value: unknown) => {
|
|
55
|
-
start: {
|
|
56
|
-
line: number;
|
|
57
|
-
character: number;
|
|
58
|
-
};
|
|
59
|
-
end: {
|
|
60
|
-
line: number;
|
|
61
|
-
character: number;
|
|
62
|
-
};
|
|
63
|
-
} | undefined>;
|
|
44
|
+
range?: ReturnType<typeof import("./lsp.js").asRange>;
|
|
45
|
+
selectionRange?: ReturnType<typeof import("./lsp.js").asRange>;
|
|
64
46
|
}[];
|
|
65
47
|
truncated: boolean;
|
|
66
48
|
}>;
|
package/dist/llm/mcp.js
CHANGED
|
@@ -42,6 +42,15 @@ export async function loadMcpTools(servers, reserved = new Set()) {
|
|
|
42
42
|
}
|
|
43
43
|
return { tools, clients, errors };
|
|
44
44
|
}
|
|
45
|
+
/**
|
|
46
|
+
* Transport-dispatching wrapper around `createMCPClient` (imported from
|
|
47
|
+
* `@ai-sdk/mcp`, capitalised). Translates a `HazeMcpServer` config into the
|
|
48
|
+
* transport shape the SDK expects — stdio (via `Experimental_StdioMCPTransport`)
|
|
49
|
+
* vs HTTP/SSE — and validates required fields before opening a transport.
|
|
50
|
+
*
|
|
51
|
+
* Not dead code: `loadMcpTools` calls this lowercase wrapper, not the imported
|
|
52
|
+
* `createMCPClient` directly (the import cannot take a `HazeMcpServer`).
|
|
53
|
+
*/
|
|
45
54
|
async function createMcpClient(server) {
|
|
46
55
|
if (server.transport === 'stdio') {
|
|
47
56
|
if (!server.command)
|
|
@@ -9,6 +9,7 @@ import { loadSkillRegistry } from '../skills/SkillRegistry.js';
|
|
|
9
9
|
import { buildSkillTools } from '../skills/skillTools.js';
|
|
10
10
|
import { isSkillEnabled } from '../config/skillSettings.js';
|
|
11
11
|
import { createSubagentTool } from '../core/subagent/subagentRunner.js';
|
|
12
|
+
import { addCapabilityTools } from './capabilities.js';
|
|
12
13
|
/**
|
|
13
14
|
* Assemble the system prompt and full tool set (built-in + LSP + subagent +
|
|
14
15
|
* skills + MCP) for a model request. This is the single source of truth shared
|
|
@@ -24,31 +25,15 @@ export async function assembleRequestContext(input) {
|
|
|
24
25
|
const hasInstalledLsp = (await installedLspServers(settings)).length > 0;
|
|
25
26
|
const toolCategories = new Map();
|
|
26
27
|
const availableTools = {};
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
availableTools
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
for (const [name, value] of Object.entries(lspTools)) {
|
|
33
|
-
toolCategories.set(name, 'lsp');
|
|
34
|
-
availableTools[name] = value;
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
const subagentTool = createSubagentTool({ model: input.model, contextFiles: input.contextFiles, session: input.session });
|
|
38
|
-
toolCategories.set('subagent', 'subagent');
|
|
39
|
-
availableTools.subagent = subagentTool;
|
|
40
|
-
const skillTools = buildSkillTools({ skills: enabledSkills });
|
|
41
|
-
for (const [name, value] of Object.entries(skillTools)) {
|
|
42
|
-
toolCategories.set(name, 'skill');
|
|
43
|
-
availableTools[name] = value;
|
|
44
|
-
}
|
|
28
|
+
addCapabilityTools({ availableTools, toolCategories, loaded: { category: 'builtin', tools: hazeTools } });
|
|
29
|
+
if (hasInstalledLsp)
|
|
30
|
+
addCapabilityTools({ availableTools, toolCategories, loaded: { category: 'lsp', tools: lspTools } });
|
|
31
|
+
addCapabilityTools({ availableTools, toolCategories, loaded: { category: 'subagent', tools: { subagent: createSubagentTool({ model: input.model, contextFiles: input.contextFiles, session: input.session }) } } });
|
|
32
|
+
addCapabilityTools({ availableTools, toolCategories, loaded: { category: 'skill', tools: buildSkillTools({ skills: enabledSkills }) } });
|
|
45
33
|
const mcpServers = configuredMcpServers(settings).filter(server => server.enabled !== false);
|
|
46
34
|
const loadedMcp = mcpServers.length > 0 ? await loadMcpTools(mcpServers, new Set(Object.keys(availableTools))) : undefined;
|
|
47
35
|
if (loadedMcp && Object.keys(loadedMcp.tools).length > 0) {
|
|
48
|
-
|
|
49
|
-
toolCategories.set(name, 'mcp');
|
|
50
|
-
availableTools[name] = value;
|
|
51
|
-
}
|
|
36
|
+
addCapabilityTools({ availableTools, toolCategories, loaded: { category: 'mcp', tools: loadedMcp.tools }, skipCollisions: true });
|
|
52
37
|
}
|
|
53
38
|
const mcpAvailable = Boolean(loadedMcp && Object.keys(loadedMcp.tools).length > 0);
|
|
54
39
|
const systemPrompt = buildSystemPrompt(input.contextFiles, input.session, { lspAvailable: hasInstalledLsp, mcpAvailable });
|
|
@@ -3,6 +3,7 @@ export interface PromptSession {
|
|
|
3
3
|
start?: Date;
|
|
4
4
|
cwd?: string;
|
|
5
5
|
}
|
|
6
|
+
export declare function projectContextSection(contextFiles: ContextFile[]): string;
|
|
6
7
|
export declare function buildSystemPrompt(contextFiles?: ContextFile[], session?: PromptSession, options?: {
|
|
7
8
|
lspAvailable?: boolean;
|
|
8
9
|
mcpAvailable?: boolean;
|
package/dist/llm/systemPrompt.js
CHANGED
|
@@ -3,7 +3,7 @@ function escapeContextContent(content) {
|
|
|
3
3
|
.replaceAll('</project_context>', '<\\/project_context>')
|
|
4
4
|
.replaceAll('</project_instructions>', '<\\/project_instructions>');
|
|
5
5
|
}
|
|
6
|
-
function projectContextSection(contextFiles) {
|
|
6
|
+
export function projectContextSection(contextFiles) {
|
|
7
7
|
if (contextFiles.length === 0)
|
|
8
8
|
return '';
|
|
9
9
|
const files = contextFiles.map(file => `<project_instructions path="${file.path}">\n${escapeContextContent(file.content)}\n</project_instructions>`).join('\n\n');
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { spawn } from 'node:child_process';
|
|
2
|
+
import { tool } from 'ai';
|
|
3
|
+
import { z } from 'zod';
|
|
4
|
+
import { classifyBashCommand, isValidationClassification } from '../../core/safety/bashClassifier.js';
|
|
5
|
+
import { parseValidationOutput } from '../../core/validation/outputParser.js';
|
|
6
|
+
import { filterBashOutput } from '../../core/bashOutput/registry.js';
|
|
7
|
+
import { storeToolOutput } from '../../core/agent/toolOutputStore.js';
|
|
8
|
+
import { workspaceRoot } from '../../utils/path.js';
|
|
9
|
+
import { compactStoredOutput, COMPACT_COMMAND_CHARS } from './outputCap.js';
|
|
10
|
+
import { runDedupedTool } from './toolContext.js';
|
|
11
|
+
const SHORT_VALIDATION_CHARS = 2_000;
|
|
12
|
+
export const bashTool = tool({
|
|
13
|
+
description: 'Run workspace tests, builds, validation, or inspection. Use file tools for edits.',
|
|
14
|
+
inputSchema: z.object({
|
|
15
|
+
command: z.string().min(1).describe('Command to execute with bash -lc'),
|
|
16
|
+
timeoutSeconds: z.number().int().positive().max(600).optional().describe('Timeout in seconds; defaults to 60'),
|
|
17
|
+
allowMutation: z.boolean().default(false).describe('Deprecated compatibility flag. Commands run without confirmation; retained for compatibility.'),
|
|
18
|
+
}),
|
|
19
|
+
execute: async ({ command, timeoutSeconds, allowMutation }, context) => runDedupedTool('bash', { command, timeoutSeconds, allowMutation }, context, async () => {
|
|
20
|
+
const cwd = workspaceRoot();
|
|
21
|
+
const classification = classifyBashCommand(command);
|
|
22
|
+
const timeoutMs = (timeoutSeconds ?? 60) * 1000;
|
|
23
|
+
const startedAt = Date.now();
|
|
24
|
+
return await new Promise(resolve => {
|
|
25
|
+
const child = spawn('bash', ['-lc', command], { cwd, stdio: ['ignore', 'pipe', 'pipe'] });
|
|
26
|
+
let stdout = '';
|
|
27
|
+
let stderr = '';
|
|
28
|
+
let settled = false;
|
|
29
|
+
let timedOut = false;
|
|
30
|
+
const timer = setTimeout(() => {
|
|
31
|
+
if (!settled) {
|
|
32
|
+
timedOut = true;
|
|
33
|
+
child.kill('SIGTERM');
|
|
34
|
+
}
|
|
35
|
+
}, timeoutMs);
|
|
36
|
+
const abort = () => child.kill('SIGTERM');
|
|
37
|
+
context.abortSignal?.addEventListener('abort', abort, { once: true });
|
|
38
|
+
child.stdout.on('data', data => stdout += data.toString());
|
|
39
|
+
child.stderr.on('data', data => stderr += data.toString());
|
|
40
|
+
child.on('close', code => {
|
|
41
|
+
settled = true;
|
|
42
|
+
clearTimeout(timer);
|
|
43
|
+
context.abortSignal?.removeEventListener('abort', abort);
|
|
44
|
+
const validationSummary = isValidationClassification(classification)
|
|
45
|
+
? parseValidationOutput({ command, code, stdout, stderr, timedOut, stdoutTruncated: stdout.length > COMPACT_COMMAND_CHARS, stderrTruncated: stderr.length > COMPACT_COMMAND_CHARS, classification })
|
|
46
|
+
: undefined;
|
|
47
|
+
const validationPassed = validationSummary?.status === 'passed';
|
|
48
|
+
const output = filterBashOutput({
|
|
49
|
+
command,
|
|
50
|
+
code,
|
|
51
|
+
stdout,
|
|
52
|
+
stderr,
|
|
53
|
+
timedOut,
|
|
54
|
+
classification,
|
|
55
|
+
validationSummary,
|
|
56
|
+
storeRawOutput: storeToolOutput,
|
|
57
|
+
fallbackCompact: compactStoredOutput,
|
|
58
|
+
compactMaxChars: validationPassed ? SHORT_VALIDATION_CHARS : COMPACT_COMMAND_CHARS,
|
|
59
|
+
});
|
|
60
|
+
resolve({
|
|
61
|
+
ok: code === 0 && !timedOut,
|
|
62
|
+
code,
|
|
63
|
+
command,
|
|
64
|
+
cwd,
|
|
65
|
+
classification,
|
|
66
|
+
durationMs: Date.now() - startedAt,
|
|
67
|
+
timedOut,
|
|
68
|
+
stdout: output.stdout,
|
|
69
|
+
stderr: output.stderr,
|
|
70
|
+
validationSummary,
|
|
71
|
+
});
|
|
72
|
+
});
|
|
73
|
+
child.on('error', error => {
|
|
74
|
+
settled = true;
|
|
75
|
+
clearTimeout(timer);
|
|
76
|
+
context.abortSignal?.removeEventListener('abort', abort);
|
|
77
|
+
resolve({ ok: false, command, cwd, classification, durationMs: Date.now() - startedAt, error: error.message });
|
|
78
|
+
});
|
|
79
|
+
});
|
|
80
|
+
}),
|
|
81
|
+
});
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import type { ToolDiffLine } from '../toolResultTypes.js';
|
|
2
|
+
/**
|
|
3
|
+
* Pure text-matching and diff helpers for the `editFile` / `replaceLines` tools.
|
|
4
|
+
*
|
|
5
|
+
* `findEditRange` locates `oldText` within a file, tolerating readFile
|
|
6
|
+
* line-number prefixes and trailing-whitespace-only differences (only when the
|
|
7
|
+
* match stays unique). Diff helpers render compact before/after line views.
|
|
8
|
+
*/
|
|
9
|
+
/** Strip a readFile-style ` 123 | ` prefix from each line. */
|
|
10
|
+
export declare function stripLineNumberPrefixes(text: string): string;
|
|
11
|
+
/**
|
|
12
|
+
* Locate `oldText` in `original`. Tries an exact match first, then a
|
|
13
|
+
* line-number-prefix-stripped match, finally a trailing-whitespace-tolerant
|
|
14
|
+
* line match. Returns `multiple` when the text is ambiguous (not unique).
|
|
15
|
+
*/
|
|
16
|
+
export declare function findEditRange(original: string, oldText: string): {
|
|
17
|
+
kind: "multiple";
|
|
18
|
+
start?: undefined;
|
|
19
|
+
end?: undefined;
|
|
20
|
+
approximate?: undefined;
|
|
21
|
+
} | {
|
|
22
|
+
kind: "found";
|
|
23
|
+
start: number;
|
|
24
|
+
end: number;
|
|
25
|
+
approximate: boolean;
|
|
26
|
+
} | {
|
|
27
|
+
kind: "missing";
|
|
28
|
+
start?: undefined;
|
|
29
|
+
end?: undefined;
|
|
30
|
+
approximate?: undefined;
|
|
31
|
+
};
|
|
32
|
+
/** Split text into diff lines, dropping a single trailing newline. */
|
|
33
|
+
export declare function splitDiffLines(text: string): string[];
|
|
34
|
+
/** 1-based line number at a character offset. */
|
|
35
|
+
export declare function lineNumberAtOffset(text: string, offset: number): number;
|
|
36
|
+
/**
|
|
37
|
+
* Build a remove/add diff with optional surrounding context lines, plus the
|
|
38
|
+
* added/removed line counts.
|
|
39
|
+
*/
|
|
40
|
+
export declare function replacementDiff(oldText: string, newText: string, oldStartLine: number, newStartLine: number, context?: {
|
|
41
|
+
before?: {
|
|
42
|
+
oldLine: number;
|
|
43
|
+
newLine: number;
|
|
44
|
+
text: string;
|
|
45
|
+
};
|
|
46
|
+
after?: {
|
|
47
|
+
oldLine: number;
|
|
48
|
+
newLine: number;
|
|
49
|
+
text: string;
|
|
50
|
+
};
|
|
51
|
+
}): {
|
|
52
|
+
diff: ToolDiffLine[];
|
|
53
|
+
addedLines: number;
|
|
54
|
+
removedLines: number;
|
|
55
|
+
};
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Pure text-matching and diff helpers for the `editFile` / `replaceLines` tools.
|
|
3
|
+
*
|
|
4
|
+
* `findEditRange` locates `oldText` within a file, tolerating readFile
|
|
5
|
+
* line-number prefixes and trailing-whitespace-only differences (only when the
|
|
6
|
+
* match stays unique). Diff helpers render compact before/after line views.
|
|
7
|
+
*/
|
|
8
|
+
/** Strip a readFile-style ` 123 | ` prefix from each line. */
|
|
9
|
+
export function stripLineNumberPrefixes(text) {
|
|
10
|
+
return text.replace(/^\s*\d+\s+\| ?/gm, '');
|
|
11
|
+
}
|
|
12
|
+
/** Character offsets of the start of each line (for mapping offsets back to lines). */
|
|
13
|
+
function lineStartOffsets(text) {
|
|
14
|
+
const offsets = [0];
|
|
15
|
+
for (let index = 0; index < text.length; index++) {
|
|
16
|
+
if (text[index] === '\n')
|
|
17
|
+
offsets.push(index + 1);
|
|
18
|
+
}
|
|
19
|
+
return offsets;
|
|
20
|
+
}
|
|
21
|
+
function findLineTrimmedRange(original, oldText) {
|
|
22
|
+
const wantedLines = oldText.replace(/\r\n/g, '\n').split('\n').map(line => line.trimEnd());
|
|
23
|
+
if (wantedLines.at(-1) === '')
|
|
24
|
+
wantedLines.pop();
|
|
25
|
+
if (wantedLines.length === 0)
|
|
26
|
+
return undefined;
|
|
27
|
+
const originalLines = original.replace(/\r\n/g, '\n').split('\n');
|
|
28
|
+
const hasTrailingNewline = original.endsWith('\n');
|
|
29
|
+
if (hasTrailingNewline)
|
|
30
|
+
originalLines.pop();
|
|
31
|
+
const offsets = lineStartOffsets(original);
|
|
32
|
+
const matches = [];
|
|
33
|
+
for (let lineIndex = 0; lineIndex <= originalLines.length - wantedLines.length; lineIndex++) {
|
|
34
|
+
const window = originalLines.slice(lineIndex, lineIndex + wantedLines.length).map(line => line.trimEnd());
|
|
35
|
+
if (window.every((line, index) => line === wantedLines[index])) {
|
|
36
|
+
const start = offsets[lineIndex] ?? 0;
|
|
37
|
+
const endLineIndex = lineIndex + wantedLines.length;
|
|
38
|
+
const end = endLineIndex < offsets.length ? (offsets[endLineIndex] ?? original.length) : original.length;
|
|
39
|
+
matches.push({ start, end });
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
if (matches.length !== 1)
|
|
43
|
+
return undefined;
|
|
44
|
+
return matches[0];
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Locate `oldText` in `original`. Tries an exact match first, then a
|
|
48
|
+
* line-number-prefix-stripped match, finally a trailing-whitespace-tolerant
|
|
49
|
+
* line match. Returns `multiple` when the text is ambiguous (not unique).
|
|
50
|
+
*/
|
|
51
|
+
export function findEditRange(original, oldText) {
|
|
52
|
+
const candidates = [oldText, stripLineNumberPrefixes(oldText)].filter((candidate, index, all) => candidate.length > 0 && all.indexOf(candidate) === index);
|
|
53
|
+
for (const candidate of candidates) {
|
|
54
|
+
const first = original.indexOf(candidate);
|
|
55
|
+
if (first !== -1) {
|
|
56
|
+
const second = original.indexOf(candidate, first + candidate.length);
|
|
57
|
+
if (second !== -1)
|
|
58
|
+
return { kind: 'multiple' };
|
|
59
|
+
return { kind: 'found', start: first, end: first + candidate.length, approximate: candidate !== oldText };
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
for (const candidate of candidates) {
|
|
63
|
+
const range = findLineTrimmedRange(original, candidate);
|
|
64
|
+
if (range)
|
|
65
|
+
return { kind: 'found', ...range, approximate: true };
|
|
66
|
+
}
|
|
67
|
+
return { kind: 'missing' };
|
|
68
|
+
}
|
|
69
|
+
/** Split text into diff lines, dropping a single trailing newline. */
|
|
70
|
+
export function splitDiffLines(text) {
|
|
71
|
+
const lines = text.split(/\r?\n/);
|
|
72
|
+
if (text.endsWith('\n') || text.endsWith('\r\n'))
|
|
73
|
+
lines.pop();
|
|
74
|
+
return lines;
|
|
75
|
+
}
|
|
76
|
+
/** 1-based line number at a character offset. */
|
|
77
|
+
export function lineNumberAtOffset(text, offset) {
|
|
78
|
+
let line = 1;
|
|
79
|
+
for (let index = 0; index < offset; index++) {
|
|
80
|
+
if (text.charCodeAt(index) === 10)
|
|
81
|
+
line += 1;
|
|
82
|
+
}
|
|
83
|
+
return line;
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Build a remove/add diff with optional surrounding context lines, plus the
|
|
87
|
+
* added/removed line counts.
|
|
88
|
+
*/
|
|
89
|
+
export function replacementDiff(oldText, newText, oldStartLine, newStartLine, context) {
|
|
90
|
+
const oldLines = splitDiffLines(oldText);
|
|
91
|
+
const newLines = splitDiffLines(newText);
|
|
92
|
+
const diff = [];
|
|
93
|
+
if (context?.before)
|
|
94
|
+
diff.push({ type: 'context', ...context.before });
|
|
95
|
+
diff.push(...oldLines.map((text, index) => ({ type: 'remove', oldLine: oldStartLine + index, text })), ...newLines.map((text, index) => ({ type: 'add', newLine: newStartLine + index, text })));
|
|
96
|
+
if (context?.after)
|
|
97
|
+
diff.push({ type: 'context', ...context.after });
|
|
98
|
+
return { diff, addedLines: newLines.length, removedLines: oldLines.length };
|
|
99
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import type { ToolFailureReasonCode } from '../toolResultTypes.js';
|
|
2
|
+
/**
|
|
3
|
+
* Error thrown by built-in tools to carry a structured failure reason and an
|
|
4
|
+
* optional recovery hint (next tool + input). Caught and normalized into a
|
|
5
|
+
* structured tool-result object by {@link structuredToolFailure}.
|
|
6
|
+
*/
|
|
7
|
+
export declare class HazeToolError extends Error {
|
|
8
|
+
reasonCode: ToolFailureReasonCode;
|
|
9
|
+
recoveryTool?: string;
|
|
10
|
+
recoveryInput?: unknown;
|
|
11
|
+
constructor(message: string, reasonCode: ToolFailureReasonCode, options?: {
|
|
12
|
+
recoveryTool?: string;
|
|
13
|
+
recoveryInput?: unknown;
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Normalize a thrown error (or raw value) into the structured `{ok: false}`
|
|
18
|
+
* tool-result shape every built-in tool returns on failure. Honors
|
|
19
|
+
* `HazeToolError` reason/recovery hints when present and lets callers override
|
|
20
|
+
* the reason code (e.g. fetch's `blocked_url`).
|
|
21
|
+
*/
|
|
22
|
+
export declare function structuredToolFailure(toolName: string, error: unknown, suggestedNextStep: string, pathForError?: string, options?: {
|
|
23
|
+
reasonCode?: ToolFailureReasonCode;
|
|
24
|
+
recoveryTool?: string;
|
|
25
|
+
recoveryInput?: unknown;
|
|
26
|
+
}): {
|
|
27
|
+
ok: boolean;
|
|
28
|
+
toolName: string;
|
|
29
|
+
path: string | undefined;
|
|
30
|
+
error: string;
|
|
31
|
+
reasonCode: ToolFailureReasonCode | undefined;
|
|
32
|
+
recoverable: boolean;
|
|
33
|
+
suggestedNextStep: string;
|
|
34
|
+
recoveryTool: string | undefined;
|
|
35
|
+
recoveryInput: unknown;
|
|
36
|
+
};
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Error thrown by built-in tools to carry a structured failure reason and an
|
|
3
|
+
* optional recovery hint (next tool + input). Caught and normalized into a
|
|
4
|
+
* structured tool-result object by {@link structuredToolFailure}.
|
|
5
|
+
*/
|
|
6
|
+
export class HazeToolError extends Error {
|
|
7
|
+
reasonCode;
|
|
8
|
+
recoveryTool;
|
|
9
|
+
recoveryInput;
|
|
10
|
+
constructor(message, reasonCode, options) {
|
|
11
|
+
super(message);
|
|
12
|
+
this.name = 'HazeToolError';
|
|
13
|
+
this.reasonCode = reasonCode;
|
|
14
|
+
this.recoveryTool = options?.recoveryTool;
|
|
15
|
+
this.recoveryInput = options?.recoveryInput;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Normalize a thrown error (or raw value) into the structured `{ok: false}`
|
|
20
|
+
* tool-result shape every built-in tool returns on failure. Honors
|
|
21
|
+
* `HazeToolError` reason/recovery hints when present and lets callers override
|
|
22
|
+
* the reason code (e.g. fetch's `blocked_url`).
|
|
23
|
+
*/
|
|
24
|
+
export function structuredToolFailure(toolName, error, suggestedNextStep, pathForError, options) {
|
|
25
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
26
|
+
const hazeError = error instanceof HazeToolError ? error : undefined;
|
|
27
|
+
return {
|
|
28
|
+
ok: false,
|
|
29
|
+
toolName,
|
|
30
|
+
path: pathForError,
|
|
31
|
+
error: message,
|
|
32
|
+
reasonCode: options?.reasonCode ?? hazeError?.reasonCode,
|
|
33
|
+
recoverable: true,
|
|
34
|
+
suggestedNextStep,
|
|
35
|
+
recoveryTool: options?.recoveryTool ?? hazeError?.recoveryTool,
|
|
36
|
+
recoveryInput: options?.recoveryInput ?? hazeError?.recoveryInput,
|
|
37
|
+
};
|
|
38
|
+
}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
export declare const fetchTool: import("ai").Tool<{
|
|
2
|
+
url: string;
|
|
3
|
+
format: "auto" | "text";
|
|
4
|
+
}, {
|
|
5
|
+
ok: boolean;
|
|
6
|
+
toolName: string;
|
|
7
|
+
path: string | undefined;
|
|
8
|
+
error: string;
|
|
9
|
+
reasonCode: import("../toolResultTypes.js").ToolFailureReasonCode | undefined;
|
|
10
|
+
recoverable: boolean;
|
|
11
|
+
suggestedNextStep: string;
|
|
12
|
+
recoveryTool: string | undefined;
|
|
13
|
+
recoveryInput: unknown;
|
|
14
|
+
} | {
|
|
15
|
+
ok: true;
|
|
16
|
+
duplicateSkipped: true;
|
|
17
|
+
toolName: string;
|
|
18
|
+
reason: string;
|
|
19
|
+
} | {
|
|
20
|
+
handle: string;
|
|
21
|
+
rawHandle: string;
|
|
22
|
+
omittedChars: number;
|
|
23
|
+
rawChars: number;
|
|
24
|
+
returnedChars: number;
|
|
25
|
+
rawTokensEstimate: number;
|
|
26
|
+
returnedTokensEstimate: number;
|
|
27
|
+
estimatedSavedTokens: number;
|
|
28
|
+
savingsPct: number;
|
|
29
|
+
ok: boolean;
|
|
30
|
+
url: string;
|
|
31
|
+
status: number;
|
|
32
|
+
statusText: string;
|
|
33
|
+
contentType: string;
|
|
34
|
+
bytes: number;
|
|
35
|
+
redirected: boolean;
|
|
36
|
+
extractionMethod: "json" | "text" | "markdown";
|
|
37
|
+
truncated: boolean;
|
|
38
|
+
content: string;
|
|
39
|
+
reducerName: string;
|
|
40
|
+
contentKind: string;
|
|
41
|
+
lossy: boolean;
|
|
42
|
+
parseTier: string;
|
|
43
|
+
} | {
|
|
44
|
+
omittedChars: number;
|
|
45
|
+
rawChars: number;
|
|
46
|
+
returnedChars: number;
|
|
47
|
+
rawTokensEstimate: number;
|
|
48
|
+
returnedTokensEstimate: number;
|
|
49
|
+
estimatedSavedTokens: number;
|
|
50
|
+
savingsPct: number;
|
|
51
|
+
ok: boolean;
|
|
52
|
+
url: string;
|
|
53
|
+
status: number;
|
|
54
|
+
statusText: string;
|
|
55
|
+
contentType: string;
|
|
56
|
+
bytes: number;
|
|
57
|
+
redirected: boolean;
|
|
58
|
+
extractionMethod: "json" | "text" | "markdown";
|
|
59
|
+
truncated: boolean;
|
|
60
|
+
content: string;
|
|
61
|
+
reducerName: string;
|
|
62
|
+
contentKind: string;
|
|
63
|
+
lossy: boolean;
|
|
64
|
+
parseTier: string;
|
|
65
|
+
}>;
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { tool } from 'ai';
|
|
2
|
+
import { z } from 'zod';
|
|
3
|
+
import { reductionMetrics } from '../../core/toolOutput/reduction.js';
|
|
4
|
+
import { fetchUrlContent, BlockedUrlError } from '../webFetch.js';
|
|
5
|
+
import { structuredToolFailure } from './failures.js';
|
|
6
|
+
import { compactStoredOutput } from './outputCap.js';
|
|
7
|
+
import { runDedupedTool } from './toolContext.js';
|
|
8
|
+
const MAX_OUTPUT_CHARS = 50_000;
|
|
9
|
+
export const fetchTool = tool({
|
|
10
|
+
description: 'Fetch a public http(s) URL and return readable content. Use for current docs, API references, error lookups, or CI logs. Private/loopback/metadata hosts are blocked.',
|
|
11
|
+
inputSchema: z.object({
|
|
12
|
+
url: z.string().url().describe('Absolute http(s) URL to fetch'),
|
|
13
|
+
format: z.enum(['auto', 'text']).default('auto').describe('auto = markdown for HTML, pretty for JSON, passthrough for text; text = raw text only'),
|
|
14
|
+
}),
|
|
15
|
+
execute: async ({ url, format }, context) => runDedupedTool('fetch', { url, format }, context, async () => {
|
|
16
|
+
try {
|
|
17
|
+
const result = await fetchUrlContent(url, { signal: context.abortSignal, format });
|
|
18
|
+
const capped = compactStoredOutput(result.content, MAX_OUTPUT_CHARS);
|
|
19
|
+
const extractionMethod = format === 'text' ? 'text' : result.extractionMethod;
|
|
20
|
+
const fetchMetrics = reductionMetrics(result.content, capped.text);
|
|
21
|
+
return {
|
|
22
|
+
ok: true,
|
|
23
|
+
url: result.url,
|
|
24
|
+
status: result.status,
|
|
25
|
+
statusText: result.statusText,
|
|
26
|
+
contentType: result.contentType,
|
|
27
|
+
bytes: result.bytes,
|
|
28
|
+
redirected: result.redirected,
|
|
29
|
+
extractionMethod,
|
|
30
|
+
truncated: capped.truncated,
|
|
31
|
+
content: capped.text,
|
|
32
|
+
reducerName: extractionMethod === 'markdown' ? 'web-html-extract' : 'web-content-cap',
|
|
33
|
+
contentKind: 'web',
|
|
34
|
+
lossy: capped.truncated || extractionMethod === 'markdown',
|
|
35
|
+
parseTier: 'full',
|
|
36
|
+
...fetchMetrics,
|
|
37
|
+
...(capped.handle ? { handle: capped.handle, rawHandle: capped.handle, omittedChars: capped.omittedChars } : { omittedChars: 0 }),
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
catch (error) {
|
|
41
|
+
const reasonCode = error instanceof BlockedUrlError ? 'blocked_url' : undefined;
|
|
42
|
+
return structuredToolFailure('fetch', error, 'Check the URL is correct and public. Private/localhost/metadata hosts and non-http(s) schemes are blocked.', url, { reasonCode });
|
|
43
|
+
}
|
|
44
|
+
}),
|
|
45
|
+
});
|