MCPcopy Create free account
hub / github.com/anus-dev/ANUS / runNonInteractive

Function runNonInteractive

packages/cli/src/nonInteractiveCli.ts:22–147  ·  view source on GitHub ↗
(
  config: Config,
  input: string,
  prompt_id: string,
)

Source from the content-addressed store, hash-verified

20import { ConsolePatcher } from './ui/utils/ConsolePatcher.js';
21
22export async function runNonInteractive(
23 config: Config,
24 input: string,
25 prompt_id: string,
26): Promise<void> {
27 const consolePatcher = new ConsolePatcher({
28 stderr: true,
29 debugMode: config.getDebugMode(),
30 });
31
32 try {
33 consolePatcher.patch();
34 // Handle EPIPE errors when the output is piped to a command that closes early.
35 process.stdout.on('error', (err: NodeJS.ErrnoException) => {
36 if (err.code === 'EPIPE') {
37 // Exit gracefully if the pipe is closed.
38 process.exit(0);
39 }
40 });
41
42 const anusClient = config.getAnusClient();
43 const toolRegistry: ToolRegistry = await config.getToolRegistry();
44
45 const abortController = new AbortController();
46 let currentMessages: Content[] = [
47 { role: 'user', parts: [{ text: input }] },
48 ];
49 let turnCount = 0;
50 while (true) {
51 turnCount++;
52 if (
53 config.getMaxSessionTurns() >= 0 &&
54 turnCount > config.getMaxSessionTurns()
55 ) {
56 console.error(
57 '\n Reached max session turns for this session. Increase the number of turns by specifying maxSessionTurns in settings.json.',
58 );
59 return;
60 }
61 const functionCalls: FunctionCall[] = [];
62
63 const responseStream = anusClient.sendMessageStream(
64 currentMessages[0]?.parts || [],
65 abortController.signal,
66 prompt_id,
67 );
68
69 for await (const event of responseStream) {
70 if (abortController.signal.aborted) {
71 console.error('Operation cancelled.');
72 return;
73 }
74
75 if (event.type === AnusEventType.Content) {
76 process.stdout.write(event.value);
77 } else if (event.type === AnusEventType.ToolCallRequest) {
78 const toolCallRequest = event.value;
79 const fc: FunctionCall = {

Callers 2

mainFunction · 0.85

Calls 12

patchMethod · 0.95
executeToolCallFunction · 0.85
parseAndFormatApiErrorFunction · 0.85
shutdownTelemetryFunction · 0.85
getDebugModeMethod · 0.80
getAnusClientMethod · 0.80
getToolRegistryMethod · 0.80
getMaxSessionTurnsMethod · 0.80
sendMessageStreamMethod · 0.45
cleanupMethod · 0.45

Tested by

no test coverage detected