-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagent-flow.mjs
More file actions
76 lines (69 loc) · 2.86 KB
/
Copy pathagent-flow.mjs
File metadata and controls
76 lines (69 loc) · 2.86 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
import { applyFirstUserMessage, buildInputMessage } from './prompt-builder.mjs';
import { join } from 'node:path';
import { homedir } from 'node:os';
import { readJson } from './runtime.mjs';
import { getHomeDirectory } from './platform.mjs';
export function resolveAgentApiKey(env = process.env) {
const apiKey = String(env.agentx_api_key || env.AGENTX_API_KEY || '').trim();
if (apiKey) return apiKey;
throw new Error('Set agentx_api_key or AGENTX_API_KEY in your shell environment.');
}
export async function loadPromptTemplate(promptPath, mcpPath = join(getHomeDirectory() || homedir(), '.agentx.mcp.json'), env = process.env) {
try {
const template = await readJson(promptPath);
let mcpTools = null;
try {
const configuredTools = await readJson(mcpPath);
mcpTools = Array.isArray(configuredTools) ? configuredTools : configuredTools?.tools || [];
} catch (error) {
if (error?.code !== 'ENOENT') throw error;
}
const merged = mcpTools === null ? template : { ...template, tools: [...(template.tools || []), ...mcpTools] };
if (!env?.AGENTX_WORKER_ID) return merged;
return { ...merged, tools: (merged.tools || []).filter((tool) => !['spawn_agent', 'agent_status', 'cancel_agent'].includes(tool?.name)) };
} catch (error) {
throw new Error(`Unable to read prompt template at ${promptPath}: ${error?.message || String(error)}`);
}
}
function formatShellCommandOutput(output) {
if (output && typeof output === 'object' && !Array.isArray(output)) {
const stdout = String(output.stdout ?? '').trimEnd();
const stderr = String(output.stderr ?? '').trimEnd();
const parts = [];
if (stdout) parts.push(stdout);
if (stderr) parts.push(stdout ? `stderr:\n${stderr}` : stderr);
return parts.join('\n\n').trimEnd();
}
return String(output ?? '').trimEnd();
}
export function appendCliTranscript(existingTranscript, command, outputText) {
const entry = [`! ${command}`];
const trimmedOutput = formatShellCommandOutput(outputText);
if (trimmedOutput) entry.push(trimmedOutput);
return [existingTranscript, entry.join('\n')].filter(Boolean).join('\n\n');
}
export function buildRequestMessage({ pendingCliTranscript, cwdNote, message }) {
const contextParts = [];
if (pendingCliTranscript) {
contextParts.push(`Local shell commands and output since the last assistant message:\n\n${pendingCliTranscript}`);
}
if (cwdNote) {
contextParts.push(cwdNote);
}
contextParts.push(message);
return contextParts.join('\n\n');
}
export function buildRequestOverride(template, userMessage, agentsText, cwd, previousResponseId) {
if (previousResponseId) {
return {
...template,
input: [buildInputMessage(userMessage)],
store: true,
previous_response_id: previousResponseId,
};
}
return {
...applyFirstUserMessage(template, userMessage, agentsText, cwd),
store: true,
};
}