initial
This commit is contained in:
38
apps/server/src/services/broker.ts
Normal file
38
apps/server/src/services/broker.ts
Normal file
@@ -0,0 +1,38 @@
|
||||
export type Frame = Record<string, unknown> & { type: string };
|
||||
export type Listener = (frame: Frame) => void;
|
||||
|
||||
export interface Broker {
|
||||
publish(sessionId: string, frame: Frame): void;
|
||||
subscribe(sessionId: string, listener: Listener): () => void;
|
||||
}
|
||||
|
||||
export function createBroker(): Broker {
|
||||
const topics = new Map<string, Set<Listener>>();
|
||||
return {
|
||||
publish(sessionId, frame) {
|
||||
const set = topics.get(sessionId);
|
||||
if (!set) return;
|
||||
for (const listener of set) {
|
||||
try {
|
||||
listener(frame);
|
||||
} catch {
|
||||
// ignore listener errors so one bad subscriber doesn't break the rest
|
||||
}
|
||||
}
|
||||
},
|
||||
subscribe(sessionId, listener) {
|
||||
let set = topics.get(sessionId);
|
||||
if (!set) {
|
||||
set = new Set();
|
||||
topics.set(sessionId, set);
|
||||
}
|
||||
set.add(listener);
|
||||
return () => {
|
||||
const s = topics.get(sessionId);
|
||||
if (!s) return;
|
||||
s.delete(listener);
|
||||
if (s.size === 0) topics.delete(sessionId);
|
||||
};
|
||||
},
|
||||
};
|
||||
}
|
||||
471
apps/server/src/services/inference.ts
Normal file
471
apps/server/src/services/inference.ts
Normal file
@@ -0,0 +1,471 @@
|
||||
import type { FastifyBaseLogger } from 'fastify';
|
||||
import type { Sql } from '../db.js';
|
||||
import type { Config } from '../config.js';
|
||||
import type { Message, Project, Session, ToolCall } from '../types/api.js';
|
||||
import { ALL_TOOLS, TOOLS_BY_NAME, toolJsonSchemas } from './tools.js';
|
||||
import { PathScopeError, resolveProjectRoot } from './path_guard.js';
|
||||
|
||||
const BASE_SYSTEM_PROMPT = (projectPath: string) =>
|
||||
`You are BooCode Chat, a code investigation assistant. The user is working on a project located at ${projectPath}. Use the file-read tools (view_file, list_dir, grep, find_files) to investigate code when needed. Be concise. Cite file paths and line numbers when discussing code. Do not hallucinate file contents — read the file first. Tool results may be truncated; if so, narrow your query rather than guessing.`;
|
||||
|
||||
const DB_FLUSH_INTERVAL_MS = 500;
|
||||
const MAX_TOOL_LOOP_DEPTH = 5;
|
||||
|
||||
export interface InferenceFrame {
|
||||
type: 'message_started' | 'delta' | 'tool_call' | 'tool_result' | 'message_complete' | 'error';
|
||||
message_id?: string;
|
||||
tool_message_id?: string;
|
||||
tool_call_id?: string;
|
||||
role?: 'assistant' | 'tool' | 'user';
|
||||
content?: string;
|
||||
tool_call?: ToolCall;
|
||||
output?: unknown;
|
||||
truncated?: boolean;
|
||||
error?: string;
|
||||
}
|
||||
|
||||
export type FramePublisher = (sessionId: string, frame: InferenceFrame) => void;
|
||||
|
||||
interface OpenAiMessage {
|
||||
role: 'system' | 'user' | 'assistant' | 'tool';
|
||||
content: string | null;
|
||||
tool_calls?: Array<{
|
||||
id: string;
|
||||
type: 'function';
|
||||
function: { name: string; arguments: string };
|
||||
}>;
|
||||
tool_call_id?: string;
|
||||
}
|
||||
|
||||
interface ChatCompletionDelta {
|
||||
role?: string;
|
||||
content?: string | null;
|
||||
tool_calls?: Array<{
|
||||
index: number;
|
||||
id?: string;
|
||||
type?: 'function';
|
||||
function?: { name?: string; arguments?: string };
|
||||
}>;
|
||||
}
|
||||
|
||||
interface ChatCompletionChunk {
|
||||
choices: Array<{
|
||||
delta: ChatCompletionDelta;
|
||||
finish_reason: string | null;
|
||||
}>;
|
||||
}
|
||||
|
||||
interface InferenceContext {
|
||||
sql: Sql;
|
||||
config: Config;
|
||||
log: FastifyBaseLogger;
|
||||
publish: FramePublisher;
|
||||
}
|
||||
|
||||
export function buildMessagesPayload(
|
||||
session: Session,
|
||||
project: Project,
|
||||
history: Message[]
|
||||
): OpenAiMessage[] {
|
||||
const out: OpenAiMessage[] = [];
|
||||
let systemPrompt = BASE_SYSTEM_PROMPT(project.path);
|
||||
if (session.system_prompt && session.system_prompt.trim().length > 0) {
|
||||
systemPrompt += '\n\n' + session.system_prompt.trim();
|
||||
}
|
||||
out.push({ role: 'system', content: systemPrompt });
|
||||
|
||||
for (const m of history) {
|
||||
if (m.role === 'assistant' && m.status === 'streaming') continue;
|
||||
if (m.role === 'tool') {
|
||||
const tr = m.tool_results;
|
||||
if (!tr) continue;
|
||||
const outputText = tr.error
|
||||
? `error: ${tr.error}`
|
||||
: typeof tr.output === 'string'
|
||||
? tr.output
|
||||
: JSON.stringify(tr.output);
|
||||
out.push({
|
||||
role: 'tool',
|
||||
content: outputText,
|
||||
tool_call_id: tr.tool_call_id,
|
||||
});
|
||||
continue;
|
||||
}
|
||||
if (m.role === 'assistant') {
|
||||
const msg: OpenAiMessage = {
|
||||
role: 'assistant',
|
||||
content: m.content && m.content.length > 0 ? m.content : null,
|
||||
};
|
||||
if (m.tool_calls && m.tool_calls.length > 0) {
|
||||
msg.tool_calls = m.tool_calls.map((tc) => ({
|
||||
id: tc.id,
|
||||
type: 'function' as const,
|
||||
function: { name: tc.name, arguments: JSON.stringify(tc.args) },
|
||||
}));
|
||||
}
|
||||
out.push(msg);
|
||||
continue;
|
||||
}
|
||||
out.push({ role: 'user', content: m.content });
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
async function loadContext(
|
||||
sql: Sql,
|
||||
sessionId: string
|
||||
): Promise<{ session: Session; project: Project; history: Message[] } | null> {
|
||||
const sessionRows = await sql<Session[]>`
|
||||
SELECT id, project_id, name, model, system_prompt, created_at, updated_at
|
||||
FROM sessions WHERE id = ${sessionId}
|
||||
`;
|
||||
if (sessionRows.length === 0) return null;
|
||||
const session = sessionRows[0]!;
|
||||
|
||||
const projectRows = await sql<Project[]>`
|
||||
SELECT id, name, path, added_at, last_session_id
|
||||
FROM projects WHERE id = ${session.project_id}
|
||||
`;
|
||||
if (projectRows.length === 0) return null;
|
||||
const project = projectRows[0]!;
|
||||
|
||||
const history = await sql<Message[]>`
|
||||
SELECT id, session_id, role, content, tool_calls, tool_results, status, last_seq, created_at
|
||||
FROM messages
|
||||
WHERE session_id = ${sessionId}
|
||||
ORDER BY created_at ASC, id ASC
|
||||
`;
|
||||
|
||||
return { session, project, history };
|
||||
}
|
||||
|
||||
async function* sseLines(stream: ReadableStream<Uint8Array>): AsyncGenerator<string> {
|
||||
const reader = stream.getReader();
|
||||
const decoder = new TextDecoder('utf-8');
|
||||
let buffer = '';
|
||||
try {
|
||||
while (true) {
|
||||
const { value, done } = await reader.read();
|
||||
if (done) break;
|
||||
buffer += decoder.decode(value, { stream: true });
|
||||
let idx;
|
||||
while ((idx = buffer.indexOf('\n')) >= 0) {
|
||||
const line = buffer.slice(0, idx).replace(/\r$/, '');
|
||||
buffer = buffer.slice(idx + 1);
|
||||
if (line.length === 0) continue;
|
||||
yield line;
|
||||
}
|
||||
}
|
||||
if (buffer.length > 0) yield buffer;
|
||||
} finally {
|
||||
reader.releaseLock();
|
||||
}
|
||||
}
|
||||
|
||||
async function streamCompletion(
|
||||
ctx: InferenceContext,
|
||||
model: string,
|
||||
messages: OpenAiMessage[],
|
||||
includeTools: boolean,
|
||||
onDelta: (content: string) => void
|
||||
): Promise<{ finishReason: string | null; content: string; toolCalls: ToolCall[] }> {
|
||||
const body: Record<string, unknown> = { model, messages, stream: true };
|
||||
if (includeTools) {
|
||||
body['tools'] = toolJsonSchemas();
|
||||
body['tool_choice'] = 'auto';
|
||||
}
|
||||
|
||||
const res = await fetch(`${ctx.config.LLAMA_SWAP_URL}/v1/chat/completions`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify(body),
|
||||
});
|
||||
if (!res.ok || !res.body) {
|
||||
const text = await res.text().catch(() => '');
|
||||
throw new Error(`llama-swap returned ${res.status}: ${text.slice(0, 200)}`);
|
||||
}
|
||||
|
||||
let content = '';
|
||||
let finishReason: string | null = null;
|
||||
const toolCallsBuffer = new Map<number, { id: string; name: string; argsText: string }>();
|
||||
|
||||
for await (const line of sseLines(res.body)) {
|
||||
if (!line.startsWith('data:')) continue;
|
||||
const payload = line.slice(5).trim();
|
||||
if (payload === '[DONE]') break;
|
||||
let parsed: ChatCompletionChunk;
|
||||
try {
|
||||
parsed = JSON.parse(payload);
|
||||
} catch {
|
||||
continue;
|
||||
}
|
||||
const choice = parsed.choices?.[0];
|
||||
if (!choice) continue;
|
||||
const delta = choice.delta ?? {};
|
||||
if (typeof delta.content === 'string' && delta.content.length > 0) {
|
||||
content += delta.content;
|
||||
onDelta(delta.content);
|
||||
}
|
||||
if (Array.isArray(delta.tool_calls)) {
|
||||
for (const tc of delta.tool_calls) {
|
||||
const idx = tc.index;
|
||||
const existing = toolCallsBuffer.get(idx) ?? { id: '', name: '', argsText: '' };
|
||||
if (tc.id) existing.id = tc.id;
|
||||
if (tc.function?.name) existing.name = tc.function.name;
|
||||
if (typeof tc.function?.arguments === 'string') existing.argsText += tc.function.arguments;
|
||||
toolCallsBuffer.set(idx, existing);
|
||||
}
|
||||
}
|
||||
if (choice.finish_reason) finishReason = choice.finish_reason;
|
||||
}
|
||||
|
||||
const toolCalls: ToolCall[] = [];
|
||||
for (const [, t] of [...toolCallsBuffer.entries()].sort(([a], [b]) => a - b)) {
|
||||
let args: Record<string, unknown> = {};
|
||||
if (t.argsText.length > 0) {
|
||||
try {
|
||||
args = JSON.parse(t.argsText);
|
||||
} catch {
|
||||
args = { _raw: t.argsText };
|
||||
}
|
||||
}
|
||||
toolCalls.push({ id: t.id || `call_${toolCalls.length}`, name: t.name, args });
|
||||
}
|
||||
|
||||
return { finishReason, content, toolCalls };
|
||||
}
|
||||
|
||||
async function executeToolCall(
|
||||
projectRoot: string,
|
||||
toolCall: ToolCall
|
||||
): Promise<{ output: unknown; truncated: boolean; error?: string }> {
|
||||
const tool = TOOLS_BY_NAME[toolCall.name];
|
||||
if (!tool) {
|
||||
return { output: null, truncated: false, error: `unknown tool: ${toolCall.name}` };
|
||||
}
|
||||
const parsed = tool.inputSchema.safeParse(toolCall.args);
|
||||
if (!parsed.success) {
|
||||
return {
|
||||
output: null,
|
||||
truncated: false,
|
||||
error: `invalid input: ${JSON.stringify(parsed.error.flatten())}`,
|
||||
};
|
||||
}
|
||||
try {
|
||||
const output = await tool.execute(parsed.data, projectRoot);
|
||||
const truncated =
|
||||
typeof output === 'object' && output !== null && 'truncated' in output
|
||||
? Boolean((output as { truncated: unknown }).truncated)
|
||||
: false;
|
||||
return { output, truncated };
|
||||
} catch (err) {
|
||||
if (err instanceof PathScopeError) {
|
||||
return { output: null, truncated: false, error: err.message };
|
||||
}
|
||||
return {
|
||||
output: null,
|
||||
truncated: false,
|
||||
error: err instanceof Error ? err.message : String(err),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
async function runAssistantTurn(
|
||||
ctx: InferenceContext,
|
||||
sessionId: string,
|
||||
assistantMessageId: string,
|
||||
depth: number
|
||||
): Promise<void> {
|
||||
if (depth > MAX_TOOL_LOOP_DEPTH) {
|
||||
await ctx.sql`
|
||||
UPDATE messages
|
||||
SET status = 'failed', content = ${'tool loop depth exceeded'}
|
||||
WHERE id = ${assistantMessageId}
|
||||
`;
|
||||
ctx.publish(sessionId, {
|
||||
type: 'error',
|
||||
message_id: assistantMessageId,
|
||||
error: 'tool loop depth exceeded',
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
const loaded = await loadContext(ctx.sql, sessionId);
|
||||
if (!loaded) {
|
||||
ctx.log.warn({ sessionId }, 'inference: session or project missing');
|
||||
return;
|
||||
}
|
||||
const { session, project, history } = loaded;
|
||||
const projectRoot = await resolveProjectRoot(project.path);
|
||||
const messages = buildMessagesPayload(session, project, history);
|
||||
|
||||
ctx.publish(sessionId, {
|
||||
type: 'message_started',
|
||||
message_id: assistantMessageId,
|
||||
role: 'assistant',
|
||||
});
|
||||
|
||||
let accumulated = '';
|
||||
let pendingFlushTimer: NodeJS.Timeout | null = null;
|
||||
let flushPromise: Promise<unknown> = Promise.resolve();
|
||||
|
||||
const flushNow = () => {
|
||||
if (pendingFlushTimer) {
|
||||
clearTimeout(pendingFlushTimer);
|
||||
pendingFlushTimer = null;
|
||||
}
|
||||
const snapshot = accumulated;
|
||||
flushPromise = flushPromise.then(() =>
|
||||
ctx.sql`UPDATE messages SET content = ${snapshot} WHERE id = ${assistantMessageId}`
|
||||
);
|
||||
};
|
||||
|
||||
const scheduleFlush = () => {
|
||||
if (pendingFlushTimer) return;
|
||||
pendingFlushTimer = setTimeout(() => {
|
||||
pendingFlushTimer = null;
|
||||
flushNow();
|
||||
}, DB_FLUSH_INTERVAL_MS);
|
||||
};
|
||||
|
||||
let content = '';
|
||||
let finishReason: string | null = null;
|
||||
let toolCalls: ToolCall[] = [];
|
||||
|
||||
try {
|
||||
const result = await streamCompletion(
|
||||
ctx,
|
||||
session.model,
|
||||
messages,
|
||||
true,
|
||||
(delta) => {
|
||||
accumulated += delta;
|
||||
ctx.publish(sessionId, {
|
||||
type: 'delta',
|
||||
message_id: assistantMessageId,
|
||||
content: delta,
|
||||
});
|
||||
ctx.log.debug({ sessionId, delta }, 'inference delta');
|
||||
scheduleFlush();
|
||||
}
|
||||
);
|
||||
content = result.content;
|
||||
finishReason = result.finishReason;
|
||||
toolCalls = result.toolCalls;
|
||||
} catch (err) {
|
||||
if (pendingFlushTimer) {
|
||||
clearTimeout(pendingFlushTimer);
|
||||
pendingFlushTimer = null;
|
||||
}
|
||||
const errMsg = err instanceof Error ? err.message : String(err);
|
||||
await ctx.sql`
|
||||
UPDATE messages
|
||||
SET status = 'failed', content = ${accumulated}
|
||||
WHERE id = ${assistantMessageId}
|
||||
`;
|
||||
ctx.publish(sessionId, {
|
||||
type: 'error',
|
||||
message_id: assistantMessageId,
|
||||
error: errMsg,
|
||||
});
|
||||
ctx.log.error({ err, sessionId, assistantMessageId }, 'inference failed');
|
||||
return;
|
||||
}
|
||||
|
||||
if (pendingFlushTimer) {
|
||||
clearTimeout(pendingFlushTimer);
|
||||
pendingFlushTimer = null;
|
||||
}
|
||||
await flushPromise;
|
||||
|
||||
if (toolCalls.length > 0) {
|
||||
await ctx.sql`
|
||||
UPDATE messages
|
||||
SET content = ${content}, status = 'complete',
|
||||
tool_calls = ${ctx.sql.json(toolCalls as never)}
|
||||
WHERE id = ${assistantMessageId}
|
||||
`;
|
||||
for (const tc of toolCalls) {
|
||||
ctx.publish(sessionId, {
|
||||
type: 'tool_call',
|
||||
message_id: assistantMessageId,
|
||||
tool_call: tc,
|
||||
});
|
||||
}
|
||||
ctx.publish(sessionId, {
|
||||
type: 'message_complete',
|
||||
message_id: assistantMessageId,
|
||||
});
|
||||
|
||||
await Promise.all(
|
||||
toolCalls.map(async (tc) => {
|
||||
const [toolRow] = await ctx.sql<{ id: string }[]>`
|
||||
INSERT INTO messages (session_id, role, content, status, created_at)
|
||||
VALUES (${sessionId}, 'tool', '', 'complete', clock_timestamp())
|
||||
RETURNING id
|
||||
`;
|
||||
const toolMessageId = toolRow!.id;
|
||||
const result = await executeToolCall(projectRoot, tc);
|
||||
const stored = {
|
||||
tool_call_id: tc.id,
|
||||
output: result.output,
|
||||
truncated: result.truncated,
|
||||
...(result.error ? { error: result.error } : {}),
|
||||
};
|
||||
await ctx.sql`
|
||||
UPDATE messages
|
||||
SET tool_results = ${ctx.sql.json(stored as never)}
|
||||
WHERE id = ${toolMessageId}
|
||||
`;
|
||||
ctx.publish(sessionId, {
|
||||
type: 'tool_result',
|
||||
tool_message_id: toolMessageId,
|
||||
tool_call_id: tc.id,
|
||||
output: result.output,
|
||||
truncated: result.truncated,
|
||||
...(result.error ? { error: result.error } : {}),
|
||||
});
|
||||
})
|
||||
);
|
||||
|
||||
const [nextAssistant] = await ctx.sql<{ id: string }[]>`
|
||||
INSERT INTO messages (session_id, role, content, status, created_at)
|
||||
VALUES (${sessionId}, 'assistant', '', 'streaming', clock_timestamp())
|
||||
RETURNING id
|
||||
`;
|
||||
await runAssistantTurn(ctx, sessionId, nextAssistant!.id, depth + 1);
|
||||
return;
|
||||
}
|
||||
|
||||
await ctx.sql`
|
||||
UPDATE messages
|
||||
SET content = ${content}, status = 'complete'
|
||||
WHERE id = ${assistantMessageId}
|
||||
`;
|
||||
ctx.publish(sessionId, {
|
||||
type: 'message_complete',
|
||||
message_id: assistantMessageId,
|
||||
});
|
||||
ctx.log.info({ sessionId, assistantMessageId, finishReason, chars: content.length }, 'inference complete');
|
||||
}
|
||||
|
||||
export async function runInference(
|
||||
ctx: InferenceContext,
|
||||
sessionId: string,
|
||||
assistantMessageId: string
|
||||
): Promise<void> {
|
||||
return runAssistantTurn(ctx, sessionId, assistantMessageId, 0);
|
||||
}
|
||||
|
||||
export function createInferenceRunner(ctx: InferenceContext) {
|
||||
return {
|
||||
enqueue(sessionId: string, assistantMessageId: string) {
|
||||
void runInference(ctx, sessionId, assistantMessageId).catch((err) => {
|
||||
ctx.log.error({ err }, 'unhandled inference error');
|
||||
});
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
// Reference to keep ALL_TOOLS imported for type checks if needed
|
||||
export const _toolNames = ALL_TOOLS.map((t) => t.name);
|
||||
39
apps/server/src/services/path_guard.ts
Normal file
39
apps/server/src/services/path_guard.ts
Normal file
@@ -0,0 +1,39 @@
|
||||
import { realpath } from 'node:fs/promises';
|
||||
import { isAbsolute, resolve, sep } from 'node:path';
|
||||
|
||||
export class PathScopeError extends Error {
|
||||
constructor(message: string) {
|
||||
super(message);
|
||||
this.name = 'PathScopeError';
|
||||
}
|
||||
}
|
||||
|
||||
export async function resolveProjectRoot(projectPath: string): Promise<string> {
|
||||
try {
|
||||
return await realpath(projectPath);
|
||||
} catch {
|
||||
throw new PathScopeError(`project path does not exist: ${projectPath}`);
|
||||
}
|
||||
}
|
||||
|
||||
export async function pathGuard(
|
||||
projectRoot: string,
|
||||
requested: string
|
||||
): Promise<string> {
|
||||
if (typeof requested !== 'string' || requested.length === 0) {
|
||||
throw new PathScopeError('path is required');
|
||||
}
|
||||
const candidate = isAbsolute(requested) ? requested : resolve(projectRoot, requested);
|
||||
let real: string;
|
||||
try {
|
||||
real = await realpath(candidate);
|
||||
} catch {
|
||||
throw new PathScopeError(`path does not exist: ${requested}`);
|
||||
}
|
||||
if (real !== projectRoot && !real.startsWith(projectRoot + sep)) {
|
||||
throw new PathScopeError(
|
||||
`path escapes project root: ${requested} -> ${real}`
|
||||
);
|
||||
}
|
||||
return real;
|
||||
}
|
||||
371
apps/server/src/services/tools.ts
Normal file
371
apps/server/src/services/tools.ts
Normal file
@@ -0,0 +1,371 @@
|
||||
import { readFile, readdir, stat } from 'node:fs/promises';
|
||||
import { resolve, basename, relative } from 'node:path';
|
||||
import { spawn } from 'node:child_process';
|
||||
import { z } from 'zod';
|
||||
import { pathGuard, PathScopeError } from './path_guard.js';
|
||||
|
||||
const MAX_FILE_BYTES = 5 * 1024 * 1024;
|
||||
const DEFAULT_VIEW_LINES = 200;
|
||||
const MAX_GREP_RESULTS = 200;
|
||||
const DEFAULT_GREP_RESULTS = 100;
|
||||
const MAX_FIND_RESULTS = 200;
|
||||
const DEFAULT_FIND_RESULTS = 100;
|
||||
const MAX_DIR_ENTRIES = 500;
|
||||
|
||||
export interface ToolJsonSchema {
|
||||
type: 'function';
|
||||
function: {
|
||||
name: string;
|
||||
description: string;
|
||||
parameters: Record<string, unknown>;
|
||||
};
|
||||
}
|
||||
|
||||
export interface ToolDef<TInput> {
|
||||
name: string;
|
||||
description: string;
|
||||
inputSchema: z.ZodType<TInput>;
|
||||
jsonSchema: ToolJsonSchema;
|
||||
execute(input: TInput, projectRoot: string): Promise<unknown>;
|
||||
}
|
||||
|
||||
const ViewFileInput = z.object({
|
||||
path: z.string().min(1),
|
||||
start_line: z.number().int().positive().optional(),
|
||||
end_line: z.number().int().positive().optional(),
|
||||
});
|
||||
type ViewFileInputT = z.infer<typeof ViewFileInput>;
|
||||
|
||||
export const viewFile: ToolDef<ViewFileInputT> = {
|
||||
name: 'view_file',
|
||||
description:
|
||||
"Read a file under the project. Returns first 200 lines by default, or a slice via start_line/end_line (1-indexed, inclusive). Files larger than 5MB are refused. Output is truncated if longer than the slice; the response indicates truncation.",
|
||||
inputSchema: ViewFileInput,
|
||||
jsonSchema: {
|
||||
type: 'function',
|
||||
function: {
|
||||
name: 'view_file',
|
||||
description:
|
||||
"Read a file under the project. Returns first 200 lines by default, or a slice via start_line/end_line (1-indexed, inclusive). Files larger than 5MB are refused.",
|
||||
parameters: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
path: { type: 'string', description: 'absolute or project-relative path' },
|
||||
start_line: { type: 'integer', description: 'first line (1-indexed)' },
|
||||
end_line: { type: 'integer', description: 'last line (1-indexed, inclusive)' },
|
||||
},
|
||||
required: ['path'],
|
||||
additionalProperties: false,
|
||||
},
|
||||
},
|
||||
},
|
||||
async execute(input, projectRoot) {
|
||||
const real = await pathGuard(projectRoot, input.path);
|
||||
const s = await stat(real);
|
||||
if (!s.isFile()) {
|
||||
throw new PathScopeError(`not a file: ${input.path}`);
|
||||
}
|
||||
if (s.size > MAX_FILE_BYTES) {
|
||||
throw new Error(`file too large (${s.size} bytes, max ${MAX_FILE_BYTES})`);
|
||||
}
|
||||
const raw = await readFile(real, 'utf8');
|
||||
const lines = raw.split('\n');
|
||||
const total = lines.length;
|
||||
let start = input.start_line ?? 1;
|
||||
let end = input.end_line ?? Math.min(total, start + DEFAULT_VIEW_LINES - 1);
|
||||
if (input.start_line == null && input.end_line == null) {
|
||||
end = Math.min(total, DEFAULT_VIEW_LINES);
|
||||
}
|
||||
if (start < 1) start = 1;
|
||||
if (end > total) end = total;
|
||||
if (end < start) end = start;
|
||||
const slice = lines.slice(start - 1, end);
|
||||
const content = slice.join('\n');
|
||||
const truncated = total > end || start > 1;
|
||||
return {
|
||||
path: relative(projectRoot, real) || basename(real),
|
||||
content,
|
||||
total_lines: total,
|
||||
returned_lines: [start, end],
|
||||
truncated,
|
||||
};
|
||||
},
|
||||
};
|
||||
|
||||
const ListDirInput = z.object({
|
||||
path: z.string().min(1),
|
||||
show_hidden: z.boolean().optional(),
|
||||
});
|
||||
type ListDirInputT = z.infer<typeof ListDirInput>;
|
||||
|
||||
export const listDir: ToolDef<ListDirInputT> = {
|
||||
name: 'list_dir',
|
||||
description: 'List entries in a directory (up to 500). Hidden files excluded unless show_hidden=true.',
|
||||
inputSchema: ListDirInput,
|
||||
jsonSchema: {
|
||||
type: 'function',
|
||||
function: {
|
||||
name: 'list_dir',
|
||||
description:
|
||||
'List entries in a directory (up to 500). Hidden files (dot-prefixed) excluded unless show_hidden=true.',
|
||||
parameters: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
path: { type: 'string' },
|
||||
show_hidden: { type: 'boolean' },
|
||||
},
|
||||
required: ['path'],
|
||||
additionalProperties: false,
|
||||
},
|
||||
},
|
||||
},
|
||||
async execute(input, projectRoot) {
|
||||
const real = await pathGuard(projectRoot, input.path);
|
||||
const s = await stat(real);
|
||||
if (!s.isDirectory()) {
|
||||
throw new PathScopeError(`not a directory: ${input.path}`);
|
||||
}
|
||||
const entries = await readdir(real, { withFileTypes: true });
|
||||
const filtered = input.show_hidden
|
||||
? entries
|
||||
: entries.filter((e) => !e.name.startsWith('.'));
|
||||
const total = filtered.length;
|
||||
const slice = filtered.slice(0, MAX_DIR_ENTRIES);
|
||||
const out = await Promise.all(
|
||||
slice.map(async (e) => {
|
||||
const child = resolve(real, e.name);
|
||||
let size: number | undefined;
|
||||
if (e.isFile()) {
|
||||
try {
|
||||
const cs = await stat(child);
|
||||
size = cs.size;
|
||||
} catch {
|
||||
/* ignore */
|
||||
}
|
||||
}
|
||||
return {
|
||||
name: e.name,
|
||||
type: e.isDirectory() ? ('dir' as const) : ('file' as const),
|
||||
...(size != null ? { size } : {}),
|
||||
};
|
||||
})
|
||||
);
|
||||
return {
|
||||
path: relative(projectRoot, real) || '.',
|
||||
entries: out,
|
||||
total,
|
||||
truncated: total > MAX_DIR_ENTRIES,
|
||||
};
|
||||
},
|
||||
};
|
||||
|
||||
const GrepInput = z.object({
|
||||
pattern: z.string().min(1),
|
||||
path: z.string().optional(),
|
||||
case_sensitive: z.boolean().optional(),
|
||||
max_results: z.number().int().positive().optional(),
|
||||
hidden: z.boolean().optional(),
|
||||
});
|
||||
type GrepInputT = z.infer<typeof GrepInput>;
|
||||
|
||||
interface RipgrepMatch {
|
||||
type: string;
|
||||
data?: {
|
||||
path?: { text?: string };
|
||||
line_number?: number;
|
||||
lines?: { text?: string };
|
||||
};
|
||||
}
|
||||
|
||||
export const grep: ToolDef<GrepInputT> = {
|
||||
name: 'grep',
|
||||
description:
|
||||
'Search file contents with ripgrep. Default path is project root. Max 100 results (200 cap).',
|
||||
inputSchema: GrepInput,
|
||||
jsonSchema: {
|
||||
type: 'function',
|
||||
function: {
|
||||
name: 'grep',
|
||||
description:
|
||||
'Search file contents with ripgrep. Returns up to 100 matches (cap 200). Set hidden=true to include dot-prefixed files.',
|
||||
parameters: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
pattern: { type: 'string' },
|
||||
path: { type: 'string' },
|
||||
case_sensitive: { type: 'boolean' },
|
||||
max_results: { type: 'integer' },
|
||||
hidden: { type: 'boolean' },
|
||||
},
|
||||
required: ['pattern'],
|
||||
additionalProperties: false,
|
||||
},
|
||||
},
|
||||
},
|
||||
async execute(input, projectRoot) {
|
||||
const target = await pathGuard(projectRoot, input.path ?? projectRoot);
|
||||
const limit = Math.min(
|
||||
Math.max(input.max_results ?? DEFAULT_GREP_RESULTS, 1),
|
||||
MAX_GREP_RESULTS
|
||||
);
|
||||
const args = [
|
||||
'--json',
|
||||
'--max-count',
|
||||
String(limit),
|
||||
'--max-columns',
|
||||
'300',
|
||||
];
|
||||
if (!input.case_sensitive) args.push('--ignore-case');
|
||||
if (input.hidden) args.push('--hidden');
|
||||
args.push('--', input.pattern, target);
|
||||
|
||||
return await new Promise((resolveP, rejectP) => {
|
||||
const child = spawn('rg', args, { cwd: projectRoot });
|
||||
const matches: Array<{ path: string; line: number; content: string }> = [];
|
||||
let buf = '';
|
||||
let stderr = '';
|
||||
child.stdout.setEncoding('utf8');
|
||||
child.stderr.setEncoding('utf8');
|
||||
child.stdout.on('data', (chunk: string) => {
|
||||
buf += chunk;
|
||||
let idx;
|
||||
while ((idx = buf.indexOf('\n')) >= 0) {
|
||||
const line = buf.slice(0, idx);
|
||||
buf = buf.slice(idx + 1);
|
||||
if (!line) continue;
|
||||
if (matches.length >= limit) continue;
|
||||
try {
|
||||
const parsed = JSON.parse(line) as RipgrepMatch;
|
||||
if (parsed.type !== 'match' || !parsed.data) continue;
|
||||
const path = parsed.data.path?.text ?? '';
|
||||
const lineNumber = parsed.data.line_number ?? 0;
|
||||
const content = parsed.data.lines?.text ?? '';
|
||||
matches.push({
|
||||
path: relative(projectRoot, path) || path,
|
||||
line: lineNumber,
|
||||
content: content.replace(/\n$/, ''),
|
||||
});
|
||||
} catch {
|
||||
/* ignore non-json */
|
||||
}
|
||||
}
|
||||
if (matches.length >= limit) {
|
||||
child.kill();
|
||||
}
|
||||
});
|
||||
child.stderr.on('data', (chunk: string) => {
|
||||
stderr += chunk;
|
||||
});
|
||||
child.on('error', (err) => rejectP(err));
|
||||
child.on('close', (code) => {
|
||||
// rg exits 1 when no matches, 2 on real error
|
||||
if (code === 2 && matches.length === 0) {
|
||||
rejectP(new Error(`ripgrep failed: ${stderr.slice(0, 300)}`));
|
||||
return;
|
||||
}
|
||||
resolveP({
|
||||
matches,
|
||||
total: matches.length,
|
||||
truncated: matches.length >= limit,
|
||||
});
|
||||
});
|
||||
});
|
||||
},
|
||||
};
|
||||
|
||||
const FindFilesInput = z.object({
|
||||
pattern: z.string().min(1),
|
||||
path: z.string().optional(),
|
||||
max_results: z.number().int().positive().optional(),
|
||||
});
|
||||
type FindFilesInputT = z.infer<typeof FindFilesInput>;
|
||||
|
||||
export const findFiles: ToolDef<FindFilesInputT> = {
|
||||
name: 'find_files',
|
||||
description: 'Glob for filenames. Default path is project root. Max 100 results (200 cap).',
|
||||
inputSchema: FindFilesInput,
|
||||
jsonSchema: {
|
||||
type: 'function',
|
||||
function: {
|
||||
name: 'find_files',
|
||||
description:
|
||||
'Glob for filenames under a directory. Default path is project root. Max 100 results (cap 200). Pattern uses standard glob (e.g. "**/*.ts").',
|
||||
parameters: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
pattern: { type: 'string' },
|
||||
path: { type: 'string' },
|
||||
max_results: { type: 'integer' },
|
||||
},
|
||||
required: ['pattern'],
|
||||
additionalProperties: false,
|
||||
},
|
||||
},
|
||||
},
|
||||
async execute(input, projectRoot) {
|
||||
const target = await pathGuard(projectRoot, input.path ?? projectRoot);
|
||||
const limit = Math.min(
|
||||
Math.max(input.max_results ?? DEFAULT_FIND_RESULTS, 1),
|
||||
MAX_FIND_RESULTS
|
||||
);
|
||||
return await new Promise((resolveP, rejectP) => {
|
||||
const args = ['--files', '--glob', input.pattern, target];
|
||||
const child = spawn('rg', args, { cwd: projectRoot });
|
||||
const paths: string[] = [];
|
||||
let total = 0;
|
||||
let buf = '';
|
||||
let stderr = '';
|
||||
child.stdout.setEncoding('utf8');
|
||||
child.stderr.setEncoding('utf8');
|
||||
child.stdout.on('data', (chunk: string) => {
|
||||
buf += chunk;
|
||||
let idx;
|
||||
while ((idx = buf.indexOf('\n')) >= 0) {
|
||||
const line = buf.slice(0, idx);
|
||||
buf = buf.slice(idx + 1);
|
||||
if (!line) continue;
|
||||
total++;
|
||||
if (paths.length < limit) {
|
||||
paths.push(relative(projectRoot, line) || line);
|
||||
}
|
||||
}
|
||||
});
|
||||
child.stderr.on('data', (chunk: string) => {
|
||||
stderr += chunk;
|
||||
});
|
||||
child.on('error', (err) => rejectP(err));
|
||||
child.on('close', (code) => {
|
||||
if (code === 2) {
|
||||
rejectP(new Error(`ripgrep failed: ${stderr.slice(0, 300)}`));
|
||||
return;
|
||||
}
|
||||
if (buf.length > 0) {
|
||||
total++;
|
||||
if (paths.length < limit) {
|
||||
paths.push(relative(projectRoot, buf) || buf);
|
||||
}
|
||||
}
|
||||
resolveP({
|
||||
paths,
|
||||
total,
|
||||
truncated: total > paths.length,
|
||||
});
|
||||
});
|
||||
});
|
||||
},
|
||||
};
|
||||
|
||||
export const ALL_TOOLS: ReadonlyArray<ToolDef<unknown>> = [
|
||||
viewFile as ToolDef<unknown>,
|
||||
listDir as ToolDef<unknown>,
|
||||
grep as ToolDef<unknown>,
|
||||
findFiles as ToolDef<unknown>,
|
||||
];
|
||||
|
||||
export const TOOLS_BY_NAME: Record<string, ToolDef<unknown>> = Object.fromEntries(
|
||||
ALL_TOOLS.map((t) => [t.name, t])
|
||||
);
|
||||
|
||||
export function toolJsonSchemas(): ToolJsonSchema[] {
|
||||
return ALL_TOOLS.map((t) => t.jsonSchema);
|
||||
}
|
||||
Reference in New Issue
Block a user