-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathask.ts
More file actions
119 lines (105 loc) · 3.75 KB
/
Copy pathask.ts
File metadata and controls
119 lines (105 loc) · 3.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/**
* `dosu ask` — ask a question and get an AI-generated answer.
*
* Calls the Python backend's /ask endpoint which runs the research workflow
* synchronously and returns the answer.
*/
import { Command } from "commander";
import pc from "picocolors";
import { loadConfig } from "../config/config";
import { getBackendURL } from "../config/constants";
import { logger } from "../debug/logger";
import { printResult } from "./output";
function requireConfig() {
const cfg = loadConfig();
if (!cfg.api_key) {
console.error(pc.red("Not configured. Run 'dosu setup' first."));
process.exit(1);
}
if (!cfg.deployment_id || !cfg.space_id) {
console.error(pc.red("Missing deployment config. Run 'dosu setup' to reconfigure."));
process.exit(1);
}
return cfg;
}
export function askCommand(): Command {
const cmd = new Command("ask")
.description("Ask a question and get an AI-generated answer")
.argument("<question>", "The question to ask")
.option("--session <id>", "Continue a previous ask session")
.option("--json", "Output as JSON")
.action(async (question: string, opts: { session?: string; json?: boolean }) => {
const cfg = requireConfig();
const backendURL = getBackendURL();
if (!backendURL) {
console.error(
pc.red("Backend URL not configured. Reinstall the CLI or set DOSU_BACKEND_URL_OVERRIDE."),
);
process.exit(1);
}
logger.debug("ask", `Asking: ${question}`);
const controller = new AbortController();
const timeout = setTimeout(() => controller.abort(), 120_000);
try {
const resp = await fetch(`${backendURL}/ask`, {
method: "POST",
headers: {
"Content-Type": "application/json",
// biome-ignore lint/style/noNonNullAssertion: checked in requireConfig
"X-Dosu-API-Key": cfg.api_key!,
},
body: JSON.stringify({
// biome-ignore lint/style/noNonNullAssertion: checked in requireConfig
deployment_id: cfg.deployment_id!,
question,
session_id: opts.session ?? undefined,
}),
signal: controller.signal,
});
if (!resp.ok) {
let detail = `Request failed with status ${resp.status}`;
try {
const errBody = await resp.json();
const raw = errBody.detail ?? detail;
detail = typeof raw === "string" ? raw : JSON.stringify(raw, null, 2);
} catch {}
console.error(pc.red(`Error: ${detail}`));
console.error(
pc.dim("Run `dosu logs --tail 30` for details, or `dosu status` to check auth."),
);
process.exit(1);
}
const body = await resp.json();
if (opts.json) {
printResult(body, opts);
return;
}
// Display the answer
if (body.answer) {
console.log(body.answer);
} else {
console.log(JSON.stringify(body, null, 2));
}
// Show session ID for follow-up
if (body.session_id) {
console.log(`\n${pc.dim(`Session: ${body.session_id}`)}`);
}
// Show observations if available
if (body.observations && body.observations.length > 0) {
console.log(`\n${pc.bold("Key observations:")}`);
for (const obs of body.observations) {
console.log(` ${pc.dim("•")} ${obs}`);
}
}
} catch (err: unknown) {
if (err instanceof Error && err.name === "AbortError") {
console.error(pc.red("Request timed out after 120s. Re-run, or simplify the question."));
process.exit(1);
}
throw err;
} finally {
clearTimeout(timeout);
}
});
return cmd;
}