scai
Version:
> **AI-powered CLI for local code analysis, commit message suggestions, and natural-language queries.** 100% local, private, GDPR-friendly, made in Denmark/EU with ❤️.
73 lines (69 loc) • 2.63 kB
JavaScript
// File: src/agents/steps/understandIntentStep.ts
import { generate } from "../lib/generate.js";
import { logInputOutput } from "../utils/promptLogHelper.js";
export const understandIntentStep = {
name: "understandIntent",
description: "Analyze the user query and determine its intent, type, and the appropriate task category.",
/**
* Run the step
*/
run: async (input) => {
const { context } = input;
const prompt = `
You are an AI assistant whose job is to determine the user's intent.
User Query:
${context.initContext?.userQuery}
Return a STRICT JSON object with the following fields:
{
"intent": "short sentence summarizing the user's intent",
"intentCategory": "one of: question, request, codingTask, refactorTask, explanation, debugging, planning, writing, other",
"normalizedQuery": "a cleaned and direct restatement of the user query",
"confidence": 0-1 // float
}
Do not include commentary. Emit ONLY valid JSON.
`.trim();
try {
const genInput = {
query: context.initContext?.userQuery ?? '',
content: prompt
};
const genOutput = await generate(genInput);
let raw = genOutput.data;
if (typeof raw !== "string")
raw = JSON.stringify(raw ?? "{}");
logInputOutput("understandIntent", "output", raw);
let parsed;
try {
parsed = JSON.parse(raw);
}
catch {
parsed = {
intent: "unknown",
intentCategory: "other",
normalizedQuery: context.initContext?.userQuery,
confidence: 0.3
};
}
// Ensure the analysis object exists
context.analysis ?? (context.analysis = {});
// Store intent inside a dedicated object
context.analysis.intent = {
intent: parsed.intent,
intentCategory: parsed.intentCategory,
normalizedQuery: parsed.normalizedQuery,
confidence: parsed.confidence
};
}
catch (err) {
console.error("understandIntent error:", err);
// Ensure the analysis object exists
context.analysis ?? (context.analysis = {});
context.analysis.intent = {
intent: "unknown",
intentCategory: "other",
normalizedQuery: context.initContext?.userQuery ?? '',
confidence: 0.0
};
}
}
};