@nodes/agent 0.0.1 → 0.0.2
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/dist/cli.js +4 -1
- package/dist/core/cli/claude.d.ts +2 -0
- package/dist/core/cli/claude.js +107 -0
- package/dist/core/cli/codex.d.ts +2 -0
- package/dist/core/cli/codex.js +85 -0
- package/dist/core/cli/gemini.d.ts +2 -0
- package/dist/core/cli/gemini.js +82 -0
- package/dist/core/cli/shared.d.ts +18 -0
- package/dist/core/cli/shared.js +17 -0
- package/dist/core/cli-stream.d.ts +11 -0
- package/dist/core/cli-stream.js +21 -0
- package/dist/core/loop.d.ts +10 -1
- package/dist/core/loop.js +53 -13
- package/dist/core/serve.js +113 -2
- package/package.json +1 -1
package/dist/cli.js
CHANGED
|
@@ -11,6 +11,7 @@ const { values } = parseArgs({
|
|
|
11
11
|
options: {
|
|
12
12
|
prompt: { type: 'string', short: 'p' },
|
|
13
13
|
node: { type: 'string', short: 'n' },
|
|
14
|
+
provider: { type: 'string' },
|
|
14
15
|
serve: { type: 'boolean' },
|
|
15
16
|
simple: { type: 'boolean' },
|
|
16
17
|
help: { type: 'boolean', short: 'h' },
|
|
@@ -27,6 +28,7 @@ Usage:
|
|
|
27
28
|
nodes -n <nodeId> Resume interactive chat on existing node
|
|
28
29
|
nodes -p "prompt" Run a single prompt and exit
|
|
29
30
|
nodes -p "prompt" -n <id> Run prompt in existing chat and exit
|
|
31
|
+
nodes --provider claude-cli Use local Claude CLI (Max subscription)
|
|
30
32
|
nodes --serve Start MCP server (exposes local tools)
|
|
31
33
|
nodes --simple Use simple readline UI (no TUI)
|
|
32
34
|
nodes --help Show this help
|
|
@@ -68,8 +70,9 @@ else {
|
|
|
68
70
|
const userId = tokenMatch?.[1];
|
|
69
71
|
const prompt = values.prompt;
|
|
70
72
|
const nodeId = values.node;
|
|
73
|
+
const provider = values.provider;
|
|
71
74
|
const nodes = new NodesClient({ url, apiKey, agent });
|
|
72
|
-
const config = { nodes, agent, userId, prompt, nodeId };
|
|
75
|
+
const config = { nodes, agent, userId, prompt, nodeId, provider, nodesUrl: url, nodesApiKey: apiKey };
|
|
73
76
|
if (prompt) {
|
|
74
77
|
// One-shot mode
|
|
75
78
|
runOnce(config)
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import { spawn } from 'node:child_process';
|
|
2
|
+
import { createInterface } from 'node:readline';
|
|
3
|
+
import { createUIMessageStream } from 'ai';
|
|
4
|
+
import { waitForExit } from './shared.js';
|
|
5
|
+
export function createClaudeStream(options) {
|
|
6
|
+
const { prompt, cwd, systemPrompt, mcpConfig, model, sessionId, allowedTools, abortSignal } = options;
|
|
7
|
+
return createUIMessageStream({
|
|
8
|
+
async execute({ writer }) {
|
|
9
|
+
const args = buildArgs({ prompt, systemPrompt, mcpConfig, model, sessionId, allowedTools, bypassPermissions: options.bypassPermissions });
|
|
10
|
+
const child = spawn('claude', args, {
|
|
11
|
+
cwd: cwd || process.cwd(),
|
|
12
|
+
stdio: ['ignore', 'pipe', 'pipe'],
|
|
13
|
+
});
|
|
14
|
+
// Log stderr for debugging
|
|
15
|
+
let stderr = '';
|
|
16
|
+
child.stderr?.on('data', (chunk) => { stderr += chunk.toString(); });
|
|
17
|
+
if (abortSignal) {
|
|
18
|
+
abortSignal.addEventListener('abort', () => child.kill('SIGTERM'), { once: true });
|
|
19
|
+
}
|
|
20
|
+
let textPartId = 0;
|
|
21
|
+
let sessionResult;
|
|
22
|
+
const rl = createInterface({ input: child.stdout });
|
|
23
|
+
writer.write({ type: 'start' });
|
|
24
|
+
writer.write({ type: 'message-metadata', messageMetadata: { provider: options.provider, model: options.model || 'default' } });
|
|
25
|
+
for await (const line of rl) {
|
|
26
|
+
let event;
|
|
27
|
+
try {
|
|
28
|
+
event = JSON.parse(line);
|
|
29
|
+
}
|
|
30
|
+
catch {
|
|
31
|
+
continue;
|
|
32
|
+
}
|
|
33
|
+
if (event.type === 'assistant') {
|
|
34
|
+
for (const block of event.message.content) {
|
|
35
|
+
if (block.type === 'text') {
|
|
36
|
+
const id = `cli-text-${textPartId++}`;
|
|
37
|
+
writer.write({ type: 'text-start', id });
|
|
38
|
+
writer.write({ type: 'text-delta', id, delta: block.text });
|
|
39
|
+
writer.write({ type: 'text-end', id });
|
|
40
|
+
}
|
|
41
|
+
else if (block.type === 'tool_use') {
|
|
42
|
+
writer.write({ type: 'tool-input-start', toolCallId: block.id, toolName: block.name });
|
|
43
|
+
writer.write({ type: 'tool-input-available', toolCallId: block.id, toolName: block.name, input: block.input });
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
else if (event.type === 'user') {
|
|
48
|
+
const content = event.message.content;
|
|
49
|
+
if (Array.isArray(content)) {
|
|
50
|
+
for (const block of content) {
|
|
51
|
+
if (block.type === 'tool_result') {
|
|
52
|
+
writer.write({ type: 'tool-output-available', toolCallId: block.tool_use_id, output: block.content });
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
else if (event.type === 'result') {
|
|
58
|
+
sessionResult = event;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
await waitForExit(child, abortSignal, 'claude').catch(err => {
|
|
62
|
+
if (stderr)
|
|
63
|
+
console.error('[claude] stderr:', stderr);
|
|
64
|
+
throw err;
|
|
65
|
+
});
|
|
66
|
+
if (sessionResult?.usage) {
|
|
67
|
+
writer.write({ type: 'message-metadata', messageMetadata: {
|
|
68
|
+
provider: options.provider,
|
|
69
|
+
model: options.model || 'default',
|
|
70
|
+
totalUsage: {
|
|
71
|
+
inputTokens: sessionResult.usage.input_tokens || 0,
|
|
72
|
+
outputTokens: sessionResult.usage.output_tokens || 0,
|
|
73
|
+
reasoningTokens: 0,
|
|
74
|
+
cachedInputTokens: sessionResult.usage.cache_read_input_tokens || 0,
|
|
75
|
+
},
|
|
76
|
+
timings: { totalMs: sessionResult.duration_ms || 0 },
|
|
77
|
+
costUsd: sessionResult.total_cost_usd,
|
|
78
|
+
} });
|
|
79
|
+
}
|
|
80
|
+
writer.write({ type: 'finish', finishReason: sessionResult?.subtype === 'success' ? 'stop' : 'error' });
|
|
81
|
+
},
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
// --- Args ---
|
|
85
|
+
function buildArgs(opts) {
|
|
86
|
+
const args = ['-p', opts.prompt, '--output-format', 'stream-json', '--verbose'];
|
|
87
|
+
if (opts.systemPrompt) {
|
|
88
|
+
args.push('--append-system-prompt', opts.systemPrompt);
|
|
89
|
+
}
|
|
90
|
+
if (opts.mcpConfig) {
|
|
91
|
+
args.push('--mcp-config', JSON.stringify(opts.mcpConfig));
|
|
92
|
+
}
|
|
93
|
+
const MODELS = ['opus', 'sonnet', 'haiku'];
|
|
94
|
+
if (opts.model && MODELS.includes(opts.model)) {
|
|
95
|
+
args.push('--model', opts.model);
|
|
96
|
+
}
|
|
97
|
+
if (opts.sessionId) {
|
|
98
|
+
args.push('-r', opts.sessionId);
|
|
99
|
+
}
|
|
100
|
+
if (opts.allowedTools?.length) {
|
|
101
|
+
args.push('--allowedTools', opts.allowedTools.join(','));
|
|
102
|
+
}
|
|
103
|
+
if (opts.bypassPermissions) {
|
|
104
|
+
args.push('--dangerously-skip-permissions');
|
|
105
|
+
}
|
|
106
|
+
return args;
|
|
107
|
+
}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import { spawn } from 'node:child_process';
|
|
2
|
+
import { createInterface } from 'node:readline';
|
|
3
|
+
import { createUIMessageStream } from 'ai';
|
|
4
|
+
import { waitForExit } from './shared.js';
|
|
5
|
+
export function createCodexStream(options) {
|
|
6
|
+
const { prompt, cwd, model, abortSignal } = options;
|
|
7
|
+
return createUIMessageStream({
|
|
8
|
+
async execute({ writer }) {
|
|
9
|
+
const args = buildArgs({ prompt, model, systemPrompt: options.systemPrompt, bypassPermissions: options.bypassPermissions });
|
|
10
|
+
const child = spawn('codex', args, {
|
|
11
|
+
cwd: cwd || process.cwd(),
|
|
12
|
+
stdio: ['ignore', 'pipe', 'pipe'],
|
|
13
|
+
});
|
|
14
|
+
// Log stderr for debugging
|
|
15
|
+
let stderr = '';
|
|
16
|
+
child.stderr?.on('data', (chunk) => { stderr += chunk.toString(); });
|
|
17
|
+
if (abortSignal) {
|
|
18
|
+
abortSignal.addEventListener('abort', () => child.kill('SIGTERM'), { once: true });
|
|
19
|
+
}
|
|
20
|
+
let textPartId = 0;
|
|
21
|
+
let turnUsage;
|
|
22
|
+
const rl = createInterface({ input: child.stdout });
|
|
23
|
+
writer.write({ type: 'start' });
|
|
24
|
+
writer.write({ type: 'message-metadata', messageMetadata: { provider: options.provider, model: options.model || 'default' } });
|
|
25
|
+
for await (const line of rl) {
|
|
26
|
+
let event;
|
|
27
|
+
try {
|
|
28
|
+
event = JSON.parse(line);
|
|
29
|
+
}
|
|
30
|
+
catch {
|
|
31
|
+
continue;
|
|
32
|
+
}
|
|
33
|
+
if (event.type === 'item.completed' && event.item.type === 'agent_message') {
|
|
34
|
+
const id = `cli-text-${textPartId++}`;
|
|
35
|
+
writer.write({ type: 'text-start', id });
|
|
36
|
+
writer.write({ type: 'text-delta', id, delta: event.item.text });
|
|
37
|
+
writer.write({ type: 'text-end', id });
|
|
38
|
+
}
|
|
39
|
+
else if (event.type === 'item.started' && event.item.type === 'command_execution') {
|
|
40
|
+
writer.write({ type: 'tool-input-start', toolCallId: event.item.id, toolName: 'command_execution' });
|
|
41
|
+
writer.write({ type: 'tool-input-available', toolCallId: event.item.id, toolName: 'command_execution', input: { command: event.item.command } });
|
|
42
|
+
}
|
|
43
|
+
else if (event.type === 'item.completed' && event.item.type === 'command_execution') {
|
|
44
|
+
writer.write({ type: 'tool-output-available', toolCallId: event.item.id, output: { output: event.item.aggregated_output, exit_code: event.item.exit_code, status: event.item.status } });
|
|
45
|
+
}
|
|
46
|
+
else if (event.type === 'turn.completed') {
|
|
47
|
+
turnUsage = event.usage;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
await waitForExit(child, abortSignal, 'codex').catch(err => {
|
|
51
|
+
console.error('[codex] stderr:', stderr || '(empty)');
|
|
52
|
+
throw err;
|
|
53
|
+
});
|
|
54
|
+
if (turnUsage) {
|
|
55
|
+
writer.write({ type: 'message-metadata', messageMetadata: {
|
|
56
|
+
provider: options.provider,
|
|
57
|
+
model: options.model || 'default',
|
|
58
|
+
totalUsage: {
|
|
59
|
+
inputTokens: turnUsage.input_tokens || 0,
|
|
60
|
+
outputTokens: turnUsage.output_tokens || 0,
|
|
61
|
+
reasoningTokens: 0,
|
|
62
|
+
cachedInputTokens: turnUsage.cached_input_tokens || 0,
|
|
63
|
+
},
|
|
64
|
+
} });
|
|
65
|
+
}
|
|
66
|
+
writer.write({ type: 'finish', finishReason: 'stop' });
|
|
67
|
+
},
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
// --- Args ---
|
|
71
|
+
function buildArgs(opts) {
|
|
72
|
+
const args = ['exec', '--json', opts.prompt];
|
|
73
|
+
// Only pass model if it looks like a valid codex model, skip generic IDs
|
|
74
|
+
if (opts.model && !opts.model.includes('/')) {
|
|
75
|
+
args.push('-m', opts.model);
|
|
76
|
+
}
|
|
77
|
+
if (opts.systemPrompt) {
|
|
78
|
+
// Triple-quoted TOML string handles newlines and quotes safely
|
|
79
|
+
args.push('-c', `instructions="""${opts.systemPrompt}"""`);
|
|
80
|
+
}
|
|
81
|
+
if (opts.bypassPermissions) {
|
|
82
|
+
args.push('--dangerously-bypass-approvals-and-sandbox');
|
|
83
|
+
}
|
|
84
|
+
return args;
|
|
85
|
+
}
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { spawn } from 'node:child_process';
|
|
2
|
+
import { createInterface } from 'node:readline';
|
|
3
|
+
import { createUIMessageStream } from 'ai';
|
|
4
|
+
import { waitForExit } from './shared.js';
|
|
5
|
+
export function createGeminiStream(options) {
|
|
6
|
+
const { prompt, cwd, model, abortSignal } = options;
|
|
7
|
+
return createUIMessageStream({
|
|
8
|
+
async execute({ writer }) {
|
|
9
|
+
const args = buildArgs({ prompt, model, bypassPermissions: options.bypassPermissions });
|
|
10
|
+
const child = spawn('gemini', args, {
|
|
11
|
+
cwd: cwd || process.cwd(),
|
|
12
|
+
stdio: ['ignore', 'pipe', 'pipe'],
|
|
13
|
+
});
|
|
14
|
+
if (abortSignal) {
|
|
15
|
+
abortSignal.addEventListener('abort', () => child.kill('SIGTERM'), { once: true });
|
|
16
|
+
}
|
|
17
|
+
let stderr = '';
|
|
18
|
+
child.stderr?.on('data', (chunk) => { stderr += chunk.toString(); });
|
|
19
|
+
let textPartId = 0;
|
|
20
|
+
let resultStats;
|
|
21
|
+
const rl = createInterface({ input: child.stdout });
|
|
22
|
+
writer.write({ type: 'start' });
|
|
23
|
+
writer.write({ type: 'message-metadata', messageMetadata: { provider: options.provider, model: options.model || 'default' } });
|
|
24
|
+
for await (const line of rl) {
|
|
25
|
+
let event;
|
|
26
|
+
try {
|
|
27
|
+
event = JSON.parse(line);
|
|
28
|
+
}
|
|
29
|
+
catch {
|
|
30
|
+
continue;
|
|
31
|
+
}
|
|
32
|
+
if (event.type === 'message' && event.role === 'assistant') {
|
|
33
|
+
const id = `cli-text-${textPartId++}`;
|
|
34
|
+
writer.write({ type: 'text-start', id });
|
|
35
|
+
writer.write({ type: 'text-delta', id, delta: event.content });
|
|
36
|
+
writer.write({ type: 'text-end', id });
|
|
37
|
+
}
|
|
38
|
+
else if (event.type === 'tool_use') {
|
|
39
|
+
writer.write({ type: 'tool-input-start', toolCallId: event.tool_id, toolName: event.tool_name });
|
|
40
|
+
writer.write({ type: 'tool-input-available', toolCallId: event.tool_id, toolName: event.tool_name, input: event.parameters });
|
|
41
|
+
}
|
|
42
|
+
else if (event.type === 'tool_result') {
|
|
43
|
+
writer.write({ type: 'tool-output-available', toolCallId: event.tool_id, output: event.output });
|
|
44
|
+
}
|
|
45
|
+
else if (event.type === 'result') {
|
|
46
|
+
resultStats = event.stats;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
await waitForExit(child, abortSignal, 'gemini').catch(err => {
|
|
50
|
+
if (stderr)
|
|
51
|
+
console.error('[gemini] stderr:', stderr);
|
|
52
|
+
throw err;
|
|
53
|
+
});
|
|
54
|
+
if (resultStats) {
|
|
55
|
+
writer.write({ type: 'message-metadata', messageMetadata: {
|
|
56
|
+
provider: options.provider,
|
|
57
|
+
model: options.model || 'default',
|
|
58
|
+
totalUsage: {
|
|
59
|
+
inputTokens: resultStats.input_tokens || 0,
|
|
60
|
+
outputTokens: resultStats.output_tokens || 0,
|
|
61
|
+
reasoningTokens: 0,
|
|
62
|
+
cachedInputTokens: resultStats.cached || 0,
|
|
63
|
+
},
|
|
64
|
+
timings: { totalMs: resultStats.duration_ms || 0 },
|
|
65
|
+
} });
|
|
66
|
+
}
|
|
67
|
+
writer.write({ type: 'finish', finishReason: 'stop' });
|
|
68
|
+
},
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
// --- Args ---
|
|
72
|
+
function buildArgs(opts) {
|
|
73
|
+
const args = ['-p', opts.prompt, '-o', 'stream-json'];
|
|
74
|
+
// Skip -m for auto mode (let gemini CLI pick the best model)
|
|
75
|
+
if (opts.model && !opts.model.includes('/') && !opts.model.startsWith('auto')) {
|
|
76
|
+
args.push('-m', opts.model);
|
|
77
|
+
}
|
|
78
|
+
if (opts.bypassPermissions) {
|
|
79
|
+
args.push('-y'); // YOLO mode — auto-approve all tool calls
|
|
80
|
+
}
|
|
81
|
+
return args;
|
|
82
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { spawn } from 'node:child_process';
|
|
2
|
+
export declare const CLI_PROVIDERS: readonly ["claude-cli", "codex-cli", "gemini-cli"];
|
|
3
|
+
export type CLIProvider = (typeof CLI_PROVIDERS)[number];
|
|
4
|
+
export declare function isCLIProvider(provider: string): provider is CLIProvider;
|
|
5
|
+
export interface CLIStreamOptions {
|
|
6
|
+
provider: CLIProvider;
|
|
7
|
+
prompt: string;
|
|
8
|
+
cwd?: string;
|
|
9
|
+
systemPrompt?: string;
|
|
10
|
+
mcpConfig?: Record<string, unknown>;
|
|
11
|
+
model?: string;
|
|
12
|
+
sessionId?: string;
|
|
13
|
+
allowedTools?: string[];
|
|
14
|
+
/** Skip all permission prompts (for headless/webhook mode) */
|
|
15
|
+
bypassPermissions?: boolean;
|
|
16
|
+
abortSignal?: AbortSignal;
|
|
17
|
+
}
|
|
18
|
+
export declare function waitForExit(child: ReturnType<typeof spawn>, abortSignal?: AbortSignal, name?: string): Promise<void>;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export const CLI_PROVIDERS = ['claude-cli', 'codex-cli', 'gemini-cli'];
|
|
2
|
+
export function isCLIProvider(provider) {
|
|
3
|
+
return CLI_PROVIDERS.includes(provider);
|
|
4
|
+
}
|
|
5
|
+
export function waitForExit(child, abortSignal, name = 'cli') {
|
|
6
|
+
return new Promise((resolve, reject) => {
|
|
7
|
+
child.on('close', (code) => {
|
|
8
|
+
if (code !== 0 && !abortSignal?.aborted) {
|
|
9
|
+
reject(new Error(`${name} exited with code ${code}`));
|
|
10
|
+
}
|
|
11
|
+
else {
|
|
12
|
+
resolve();
|
|
13
|
+
}
|
|
14
|
+
});
|
|
15
|
+
child.on('error', reject);
|
|
16
|
+
});
|
|
17
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export { CLI_PROVIDERS, isCLIProvider } from './cli/shared.js';
|
|
2
|
+
export type { CLIProvider, CLIStreamOptions } from './cli/shared.js';
|
|
3
|
+
export { createClaudeStream } from './cli/claude.js';
|
|
4
|
+
export { createCodexStream } from './cli/codex.js';
|
|
5
|
+
export { createGeminiStream } from './cli/gemini.js';
|
|
6
|
+
import type { CLIStreamOptions } from './cli/shared.js';
|
|
7
|
+
/**
|
|
8
|
+
* Spawns a CLI agent (claude, codex, or gemini) and returns a UIMessageStream
|
|
9
|
+
* compatible with readUIMessageStream() from Vercel AI SDK.
|
|
10
|
+
*/
|
|
11
|
+
export declare function createCLIStream(options: CLIStreamOptions): ReadableStream<import("ai").InferUIMessageChunk<import("ai").UIMessage<unknown, import("ai").UIDataTypes, import("ai").UITools>>>;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
// Re-export from per-provider modules
|
|
2
|
+
export { CLI_PROVIDERS, isCLIProvider } from './cli/shared.js';
|
|
3
|
+
export { createClaudeStream } from './cli/claude.js';
|
|
4
|
+
export { createCodexStream } from './cli/codex.js';
|
|
5
|
+
export { createGeminiStream } from './cli/gemini.js';
|
|
6
|
+
import { createClaudeStream } from './cli/claude.js';
|
|
7
|
+
import { createCodexStream } from './cli/codex.js';
|
|
8
|
+
import { createGeminiStream } from './cli/gemini.js';
|
|
9
|
+
/**
|
|
10
|
+
* Spawns a CLI agent (claude, codex, or gemini) and returns a UIMessageStream
|
|
11
|
+
* compatible with readUIMessageStream() from Vercel AI SDK.
|
|
12
|
+
*/
|
|
13
|
+
export function createCLIStream(options) {
|
|
14
|
+
if (options.provider === 'claude-cli')
|
|
15
|
+
return createClaudeStream(options);
|
|
16
|
+
if (options.provider === 'codex-cli')
|
|
17
|
+
return createCodexStream(options);
|
|
18
|
+
if (options.provider === 'gemini-cli')
|
|
19
|
+
return createGeminiStream(options);
|
|
20
|
+
throw new Error(`CLI provider "${options.provider}" not supported.`);
|
|
21
|
+
}
|
package/dist/core/loop.d.ts
CHANGED
|
@@ -5,21 +5,30 @@ export interface AgentConfig {
|
|
|
5
5
|
/** Agent slug (e.g. "nodes/my-agent") or user ID */
|
|
6
6
|
agent: string;
|
|
7
7
|
model?: string;
|
|
8
|
+
/** Override provider (e.g. "claude-cli" to use local CLI subscription) */
|
|
9
|
+
provider?: string;
|
|
8
10
|
/** Human user ID — extracted from user-scoped API key (nodes_<userId>_<secret>) */
|
|
9
11
|
userId?: string;
|
|
10
12
|
/** One-shot prompt — run and exit */
|
|
11
13
|
prompt?: string;
|
|
12
14
|
/** Existing chat node ID to resume */
|
|
13
15
|
nodeId?: string;
|
|
16
|
+
/** Nodes instance URL — needed for CLI providers to build MCP config */
|
|
17
|
+
nodesUrl?: string;
|
|
18
|
+
/** Nodes API key — needed for CLI providers to build MCP config */
|
|
19
|
+
nodesApiKey?: string;
|
|
14
20
|
}
|
|
15
21
|
export interface AgentEnv {
|
|
16
22
|
modelId: string;
|
|
17
|
-
model
|
|
23
|
+
model?: LanguageModel;
|
|
18
24
|
provider: string;
|
|
19
25
|
allTools: Record<string, any>;
|
|
20
26
|
systemPrompt: string;
|
|
21
27
|
chatsNodeId?: string;
|
|
22
28
|
agentName: string;
|
|
29
|
+
/** Nodes connection info — used by CLI providers for MCP config */
|
|
30
|
+
nodesUrl?: string;
|
|
31
|
+
nodesApiKey?: string;
|
|
23
32
|
}
|
|
24
33
|
export declare function loadAgentEnv(config: AgentConfig): Promise<AgentEnv>;
|
|
25
34
|
export interface TurnOptions {
|
package/dist/core/loop.js
CHANGED
|
@@ -2,17 +2,20 @@ import { streamText, hasToolCall, stepCountIs, readUIMessageStream } from 'ai';
|
|
|
2
2
|
import { localTools } from './tools.js';
|
|
3
3
|
import { loadRemoteTools } from './remote-tools.js';
|
|
4
4
|
import { resolveModel } from './providers.js';
|
|
5
|
+
import { createCLIStream, isCLIProvider } from './cli-stream.js';
|
|
5
6
|
const DEFAULT_MAX_STEPS = 50;
|
|
6
7
|
const MAX_STEPS = Number(process.env.MAX_STEPS) || DEFAULT_MAX_STEPS;
|
|
7
8
|
const stopWhen = [hasToolCall('ai_end'), hasToolCall('ai_buttons'), stepCountIs(MAX_STEPS)];
|
|
8
9
|
export async function loadAgentEnv(config) {
|
|
9
10
|
const { nodes, agent } = config;
|
|
10
11
|
const agentInfo = await nodes.ai.readAgent(agent, { include: ['systemPrompt'] });
|
|
11
|
-
const provider = agentInfo.ai?.provider || 'gateway';
|
|
12
|
+
const provider = config.provider || agentInfo.ai?.provider || 'gateway';
|
|
12
13
|
const modelId = config.model || agentInfo.ai?.model || 'google/gemini-2.5-flash';
|
|
13
|
-
|
|
14
|
-
const
|
|
15
|
-
|
|
14
|
+
// CLI providers use the local CLI binary — no API model needed
|
|
15
|
+
const model = isCLIProvider(provider) ? undefined : resolveModel(provider, modelId);
|
|
16
|
+
// CLI providers handle their own tools — skip loading for them
|
|
17
|
+
const remoteTools = isCLIProvider(provider) ? {} : await loadRemoteTools(nodes, agentInfo.id);
|
|
18
|
+
const allTools = isCLIProvider(provider) ? {} : { ...localTools, ...remoteTools };
|
|
16
19
|
const systemPrompt = agentInfo.systemPrompt || 'You are a helpful AI agent.';
|
|
17
20
|
return {
|
|
18
21
|
modelId,
|
|
@@ -22,10 +25,39 @@ export async function loadAgentEnv(config) {
|
|
|
22
25
|
systemPrompt,
|
|
23
26
|
chatsNodeId: agentInfo.ai?.chatsNode,
|
|
24
27
|
agentName: agentInfo.name,
|
|
28
|
+
nodesUrl: config.nodesUrl,
|
|
29
|
+
nodesApiKey: config.nodesApiKey,
|
|
25
30
|
};
|
|
26
31
|
}
|
|
27
32
|
export async function runTurn(env, options) {
|
|
28
33
|
const startMs = Date.now();
|
|
34
|
+
// CLI providers: spawn local CLI binary and parse its output
|
|
35
|
+
if (isCLIProvider(env.provider)) {
|
|
36
|
+
// For CLI mode, combine message history into a single prompt
|
|
37
|
+
const prompt = options.messages
|
|
38
|
+
? options.messages.map(m => `${m.role}: ${m.content}`).join('\n\n') + '\n\nRespond to the last user message above.'
|
|
39
|
+
: options.prompt || '';
|
|
40
|
+
// Build MCP config so Claude CLI can access Nodes platform tools
|
|
41
|
+
const mcpConfig = (env.nodesUrl && env.nodesApiKey) ? {
|
|
42
|
+
mcpServers: {
|
|
43
|
+
nodes: {
|
|
44
|
+
type: 'http',
|
|
45
|
+
url: `${env.nodesUrl}/api/v1/mcp/`,
|
|
46
|
+
headers: { Authorization: `Bearer ${env.nodesApiKey}` },
|
|
47
|
+
},
|
|
48
|
+
},
|
|
49
|
+
} : undefined;
|
|
50
|
+
const cliStream = createCLIStream({
|
|
51
|
+
provider: env.provider,
|
|
52
|
+
prompt,
|
|
53
|
+
systemPrompt: env.systemPrompt,
|
|
54
|
+
model: env.modelId,
|
|
55
|
+
mcpConfig,
|
|
56
|
+
abortSignal: options.abortSignal,
|
|
57
|
+
});
|
|
58
|
+
return consumeUIStream(cliStream, env, options, startMs);
|
|
59
|
+
}
|
|
60
|
+
// API providers: use Vercel AI SDK streamText()
|
|
29
61
|
const result = streamText({
|
|
30
62
|
model: env.model,
|
|
31
63
|
system: env.systemPrompt,
|
|
@@ -37,7 +69,22 @@ export async function runTurn(env, options) {
|
|
|
37
69
|
: { prompt: options.prompt || '' }),
|
|
38
70
|
});
|
|
39
71
|
// Use native AI SDK UIMessage stream — same format the Nodes UI uses
|
|
40
|
-
const
|
|
72
|
+
const stream = result.toUIMessageStream();
|
|
73
|
+
const turnResult = await consumeUIStream(stream, env, options, startMs);
|
|
74
|
+
// API providers have precise usage stats from the SDK
|
|
75
|
+
const usage = await result.totalUsage;
|
|
76
|
+
turnResult.metadata.totalUsage = {
|
|
77
|
+
inputTokens: usage.inputTokens,
|
|
78
|
+
outputTokens: usage.outputTokens,
|
|
79
|
+
totalTokens: usage.totalTokens,
|
|
80
|
+
reasoningTokens: usage.outputTokenDetails?.reasoningTokens,
|
|
81
|
+
cachedInputTokens: usage.inputTokenDetails?.cacheReadTokens,
|
|
82
|
+
};
|
|
83
|
+
return turnResult;
|
|
84
|
+
}
|
|
85
|
+
// --- Shared stream consumer for both API and CLI paths ---
|
|
86
|
+
async function consumeUIStream(stream, env, options, startMs) {
|
|
87
|
+
const uiStream = readUIMessageStream({ stream });
|
|
41
88
|
let message;
|
|
42
89
|
let prevTextLen = 0;
|
|
43
90
|
let prevReasoningLen = 0;
|
|
@@ -79,7 +126,6 @@ export async function runTurn(env, options) {
|
|
|
79
126
|
}
|
|
80
127
|
}
|
|
81
128
|
const totalMs = Date.now() - startMs;
|
|
82
|
-
const usage = await result.totalUsage;
|
|
83
129
|
const parts = message?.parts || [];
|
|
84
130
|
const fullText = parts
|
|
85
131
|
.filter(p => p.type === 'text')
|
|
@@ -93,13 +139,7 @@ export async function runTurn(env, options) {
|
|
|
93
139
|
metadata: {
|
|
94
140
|
provider: env.provider,
|
|
95
141
|
model: env.modelId,
|
|
96
|
-
totalUsage: {
|
|
97
|
-
inputTokens: usage.inputTokens,
|
|
98
|
-
outputTokens: usage.outputTokens,
|
|
99
|
-
totalTokens: usage.totalTokens,
|
|
100
|
-
reasoningTokens: usage.outputTokenDetails?.reasoningTokens,
|
|
101
|
-
cachedInputTokens: usage.inputTokenDetails?.cacheReadTokens,
|
|
102
|
-
},
|
|
142
|
+
totalUsage: {},
|
|
103
143
|
timings: { totalMs },
|
|
104
144
|
},
|
|
105
145
|
};
|
package/dist/core/serve.js
CHANGED
|
@@ -4,6 +4,8 @@ import { StreamableHTTPServerTransport } from '@modelcontextprotocol/sdk/server/
|
|
|
4
4
|
import { createServer } from 'node:http';
|
|
5
5
|
import { z } from 'zod';
|
|
6
6
|
import { execBash, execReadFile, execWriteFile } from './tools.js';
|
|
7
|
+
import { createCLIStream, isCLIProvider } from './cli-stream.js';
|
|
8
|
+
import { JsonToSseTransformStream, readUIMessageStream } from 'ai';
|
|
7
9
|
const require = createRequire(import.meta.url);
|
|
8
10
|
const pkg = require('../../package.json');
|
|
9
11
|
export async function serve(options = {}) {
|
|
@@ -94,6 +96,113 @@ export async function serve(options = {}) {
|
|
|
94
96
|
}
|
|
95
97
|
return;
|
|
96
98
|
}
|
|
99
|
+
// Chat webhook — receives prompt, spawns CLI, returns SSE stream
|
|
100
|
+
if (url.pathname === '/chat' && req.method === 'POST') {
|
|
101
|
+
if (!checkAuth(req)) {
|
|
102
|
+
console.log(`[chat] ✗ Unauthorized request`);
|
|
103
|
+
res.writeHead(401, { 'Content-Type': 'application/json' });
|
|
104
|
+
res.end(JSON.stringify({ error: 'Unauthorized' }));
|
|
105
|
+
return;
|
|
106
|
+
}
|
|
107
|
+
const chunks = [];
|
|
108
|
+
for await (const chunk of req)
|
|
109
|
+
chunks.push(chunk);
|
|
110
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
111
|
+
let body;
|
|
112
|
+
try {
|
|
113
|
+
body = JSON.parse(Buffer.concat(chunks).toString());
|
|
114
|
+
}
|
|
115
|
+
catch {
|
|
116
|
+
res.writeHead(400, { 'Content-Type': 'application/json' });
|
|
117
|
+
res.end(JSON.stringify({ error: 'Invalid JSON' }));
|
|
118
|
+
return;
|
|
119
|
+
}
|
|
120
|
+
// Build prompt from messages array or direct prompt
|
|
121
|
+
let prompt = body.prompt;
|
|
122
|
+
if (!prompt && body.messages?.length) {
|
|
123
|
+
// Pass full message history as JSON for context preservation
|
|
124
|
+
prompt = JSON.stringify(body.messages, null, 2);
|
|
125
|
+
}
|
|
126
|
+
if (!prompt) {
|
|
127
|
+
res.writeHead(400, { 'Content-Type': 'application/json' });
|
|
128
|
+
res.end(JSON.stringify({ error: 'Missing prompt or messages' }));
|
|
129
|
+
return;
|
|
130
|
+
}
|
|
131
|
+
const provider = (body.provider || 'claude-cli');
|
|
132
|
+
if (!isCLIProvider(provider)) {
|
|
133
|
+
res.writeHead(400, { 'Content-Type': 'application/json' });
|
|
134
|
+
res.end(JSON.stringify({ error: `Unsupported CLI provider: ${provider}` }));
|
|
135
|
+
return;
|
|
136
|
+
}
|
|
137
|
+
const startTime = Date.now();
|
|
138
|
+
const shouldStream = body.stream !== false;
|
|
139
|
+
console.log(`[chat] → Received: provider=${provider}, model=${body.model || 'default'}, stream=${shouldStream}, prompt=${prompt.length} chars`);
|
|
140
|
+
// Create CLI stream (bypass permissions for headless webhook mode)
|
|
141
|
+
console.log(`[chat] Spawning CLI stream...`);
|
|
142
|
+
const cliStream = createCLIStream({
|
|
143
|
+
provider: provider,
|
|
144
|
+
prompt,
|
|
145
|
+
systemPrompt: body.systemPrompt,
|
|
146
|
+
model: body.model,
|
|
147
|
+
bypassPermissions: true,
|
|
148
|
+
});
|
|
149
|
+
if (shouldStream) {
|
|
150
|
+
// SSE streaming response
|
|
151
|
+
const sseStream = cliStream.pipeThrough(new JsonToSseTransformStream());
|
|
152
|
+
res.writeHead(200, {
|
|
153
|
+
'Content-Type': 'text/event-stream',
|
|
154
|
+
'Cache-Control': 'no-cache',
|
|
155
|
+
Connection: 'keep-alive',
|
|
156
|
+
'X-Accel-Buffering': 'no',
|
|
157
|
+
'X-Vercel-AI-UI-Message-Stream': 'v1',
|
|
158
|
+
});
|
|
159
|
+
res.flushHeaders();
|
|
160
|
+
console.log(`[chat] Streaming response...`);
|
|
161
|
+
let chunkCount = 0;
|
|
162
|
+
const reader = sseStream.getReader();
|
|
163
|
+
try {
|
|
164
|
+
while (true) {
|
|
165
|
+
const { done, value } = await reader.read();
|
|
166
|
+
if (done)
|
|
167
|
+
break;
|
|
168
|
+
chunkCount++;
|
|
169
|
+
res.write(value);
|
|
170
|
+
if (typeof res.flush === 'function') {
|
|
171
|
+
res.flush();
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
catch (err) {
|
|
176
|
+
console.error('[chat] ✗ Stream error:', err);
|
|
177
|
+
}
|
|
178
|
+
finally {
|
|
179
|
+
const elapsed = ((Date.now() - startTime) / 1000).toFixed(1);
|
|
180
|
+
console.log(`[chat] ← Done: ${chunkCount} chunks in ${elapsed}s`);
|
|
181
|
+
res.end();
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
else {
|
|
185
|
+
// Non-stream: consume CLI stream, return JSON with final UIMessage
|
|
186
|
+
try {
|
|
187
|
+
let lastAssistant;
|
|
188
|
+
for await (const snapshot of readUIMessageStream({ stream: cliStream })) {
|
|
189
|
+
if (snapshot.role === 'assistant')
|
|
190
|
+
lastAssistant = snapshot;
|
|
191
|
+
}
|
|
192
|
+
const message = lastAssistant || { id: 'assistant', role: 'assistant', parts: [] };
|
|
193
|
+
const elapsed = ((Date.now() - startTime) / 1000).toFixed(1);
|
|
194
|
+
console.log(`[chat] ← Done (JSON): ${message.parts.length} parts in ${elapsed}s`);
|
|
195
|
+
res.writeHead(200, { 'Content-Type': 'application/json' });
|
|
196
|
+
res.end(JSON.stringify({ message }));
|
|
197
|
+
}
|
|
198
|
+
catch (err) {
|
|
199
|
+
console.error('[chat] ✗ Error:', err);
|
|
200
|
+
res.writeHead(500, { 'Content-Type': 'application/json' });
|
|
201
|
+
res.end(JSON.stringify({ error: err instanceof Error ? err.message : 'CLI execution failed' }));
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
return;
|
|
205
|
+
}
|
|
97
206
|
// Health check
|
|
98
207
|
if (url.pathname === '/health') {
|
|
99
208
|
res.writeHead(200, { 'Content-Type': 'application/json' });
|
|
@@ -104,8 +213,10 @@ export async function serve(options = {}) {
|
|
|
104
213
|
res.end('Not found');
|
|
105
214
|
});
|
|
106
215
|
httpServer.listen(port, () => {
|
|
107
|
-
console.log(`[serve] Nodes Agent
|
|
108
|
-
console.log(`[serve]
|
|
216
|
+
console.log(`[serve] Nodes Agent server listening on http://0.0.0.0:${port}`);
|
|
217
|
+
console.log(`[serve] MCP: http://localhost:${port}/mcp`);
|
|
218
|
+
console.log(`[serve] Chat: http://localhost:${port}/chat`);
|
|
219
|
+
console.log(`[serve] Health: http://localhost:${port}/health`);
|
|
109
220
|
console.log(`[serve] Tools: bash, read_file, write_file`);
|
|
110
221
|
console.log(`[serve] Auth: ${apiKey ? 'Bearer token required' : 'OPEN (no AGENT_API_KEY set)'}`);
|
|
111
222
|
});
|