u-foo 1.9.8 → 2.1.0
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/package.json +2 -4
- package/src/agent/claudeEventTranslator.js +267 -0
- package/src/agent/claudeOauthTokenReader.js +52 -0
- package/src/agent/claudeThreadProvider.js +343 -0
- package/src/agent/cliRunner.js +4 -16
- package/src/agent/codexEventTranslator.js +78 -0
- package/src/agent/codexThreadProvider.js +181 -0
- package/src/agent/controllerToolExecutor.js +233 -0
- package/src/agent/credentials/claude.js +324 -0
- package/src/agent/credentials/codex.js +203 -0
- package/src/agent/credentials/index.js +106 -0
- package/src/agent/internalRunner.js +333 -2
- package/src/agent/loopObservability.js +190 -0
- package/src/agent/loopRuntime.js +457 -0
- package/src/agent/ufooAgent.js +178 -120
- package/src/agent/upstreamTransport.js +464 -0
- package/src/bus/utils.js +3 -2
- package/src/chat/dashboardView.js +51 -1
- package/src/chat/index.js +3 -1
- package/src/config.js +53 -17
- package/src/controller/flags.js +160 -0
- package/src/controller/gateRouter.js +201 -0
- package/src/controller/routerFastPath.js +22 -0
- package/src/controller/shadowGuard.js +280 -0
- package/src/daemon/index.js +2 -3
- package/src/daemon/promptLoop.js +33 -224
- package/src/daemon/promptRequest.js +360 -5
- package/src/daemon/status.js +2 -0
- package/src/history/inputTimeline.js +9 -4
- package/src/memory/index.js +24 -0
- package/src/providerapi/redactor.js +87 -0
- package/src/providerapi/shadowDiff.js +174 -0
- package/src/report/store.js +4 -3
- package/src/tools/handlers/ackBus.js +26 -0
- package/src/tools/handlers/common.js +64 -0
- package/src/tools/handlers/dispatchMessage.js +81 -0
- package/src/tools/handlers/listAgents.js +14 -0
- package/src/tools/handlers/readBusSummary.js +34 -0
- package/src/tools/handlers/readOpenDecisions.js +26 -0
- package/src/tools/handlers/readProjectRegistry.js +20 -0
- package/src/tools/handlers/readPromptHistory.js +123 -0
- package/src/tools/handlers/tier2.js +134 -0
- package/src/tools/index.js +55 -0
- package/src/tools/registry.js +69 -0
- package/src/tools/schemaFixtures.js +415 -0
- package/src/tools/tier0/listAgents.js +14 -0
- package/src/tools/tier0/readBusSummary.js +14 -0
- package/src/tools/tier0/readOpenDecisions.js +14 -0
- package/src/tools/tier0/readProjectRegistry.js +14 -0
- package/src/tools/tier0/readPromptHistory.js +14 -0
- package/src/tools/tier1/ackBus.js +14 -0
- package/src/tools/tier1/dispatchMessage.js +14 -0
- package/src/tools/tier1/routeAgent.js +14 -0
- package/src/tools/tier2/closeAgent.js +14 -0
- package/src/tools/tier2/launchAgent.js +14 -0
- package/src/tools/tier2/manageCron.js +14 -0
- package/src/tools/tier2/renameAgent.js +14 -0
- package/src/tools/types.js +75 -0
- package/src/tools/unimplemented.js +13 -0
- package/src/ufoo/paths.js +4 -0
- package/bin/ufoo-assistant-agent.js +0 -5
- package/bin/ufoo-engine.js +0 -25
- package/src/assistant/agent.js +0 -261
- package/src/assistant/bridge.js +0 -178
- package/src/assistant/constants.js +0 -15
- package/src/assistant/engine.js +0 -252
- package/src/assistant/stdio.js +0 -58
- package/src/assistant/ufooEngineCli.js +0 -312
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
const { READ_OPEN_DECISIONS_SCHEMA } = require("../schemaFixtures");
|
|
2
|
+
const { CALLER_TIERS, TOOL_TIERS, createToolDefinition } = require("../types");
|
|
3
|
+
const { readOpenDecisionsHandler } = require("../handlers/readOpenDecisions");
|
|
4
|
+
|
|
5
|
+
module.exports = createToolDefinition({
|
|
6
|
+
name: READ_OPEN_DECISIONS_SCHEMA.name,
|
|
7
|
+
description: READ_OPEN_DECISIONS_SCHEMA.description,
|
|
8
|
+
tier: TOOL_TIERS.TIER_0,
|
|
9
|
+
allowedCallerTiers: [CALLER_TIERS.CONTROLLER, CALLER_TIERS.WORKER],
|
|
10
|
+
inputSchema: READ_OPEN_DECISIONS_SCHEMA.input_schema,
|
|
11
|
+
outputSchema: READ_OPEN_DECISIONS_SCHEMA.output_schema,
|
|
12
|
+
schemaVersion: READ_OPEN_DECISIONS_SCHEMA.schema_version,
|
|
13
|
+
handler: readOpenDecisionsHandler,
|
|
14
|
+
});
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
const { READ_PROJECT_REGISTRY_SCHEMA } = require("../schemaFixtures");
|
|
2
|
+
const { CALLER_TIERS, TOOL_TIERS, createToolDefinition } = require("../types");
|
|
3
|
+
const { readProjectRegistryHandler } = require("../handlers/readProjectRegistry");
|
|
4
|
+
|
|
5
|
+
module.exports = createToolDefinition({
|
|
6
|
+
name: READ_PROJECT_REGISTRY_SCHEMA.name,
|
|
7
|
+
description: READ_PROJECT_REGISTRY_SCHEMA.description,
|
|
8
|
+
tier: TOOL_TIERS.TIER_0,
|
|
9
|
+
allowedCallerTiers: [CALLER_TIERS.CONTROLLER, CALLER_TIERS.WORKER],
|
|
10
|
+
inputSchema: READ_PROJECT_REGISTRY_SCHEMA.input_schema,
|
|
11
|
+
outputSchema: READ_PROJECT_REGISTRY_SCHEMA.output_schema,
|
|
12
|
+
schemaVersion: READ_PROJECT_REGISTRY_SCHEMA.schema_version,
|
|
13
|
+
handler: readProjectRegistryHandler,
|
|
14
|
+
});
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
const { READ_PROMPT_HISTORY_SCHEMA } = require("../schemaFixtures");
|
|
2
|
+
const { CALLER_TIERS, TOOL_TIERS, createToolDefinition } = require("../types");
|
|
3
|
+
const { readPromptHistoryHandler } = require("../handlers/readPromptHistory");
|
|
4
|
+
|
|
5
|
+
module.exports = createToolDefinition({
|
|
6
|
+
name: READ_PROMPT_HISTORY_SCHEMA.name,
|
|
7
|
+
description: READ_PROMPT_HISTORY_SCHEMA.description,
|
|
8
|
+
tier: TOOL_TIERS.TIER_0,
|
|
9
|
+
allowedCallerTiers: [CALLER_TIERS.CONTROLLER, CALLER_TIERS.WORKER],
|
|
10
|
+
inputSchema: READ_PROMPT_HISTORY_SCHEMA.input_schema,
|
|
11
|
+
outputSchema: READ_PROMPT_HISTORY_SCHEMA.output_schema,
|
|
12
|
+
schemaVersion: READ_PROMPT_HISTORY_SCHEMA.schema_version,
|
|
13
|
+
handler: readPromptHistoryHandler,
|
|
14
|
+
});
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
const { ACK_BUS_SCHEMA } = require("../schemaFixtures");
|
|
2
|
+
const { CALLER_TIERS, TOOL_TIERS, createToolDefinition } = require("../types");
|
|
3
|
+
const { ackBusHandler } = require("../handlers/ackBus");
|
|
4
|
+
|
|
5
|
+
module.exports = createToolDefinition({
|
|
6
|
+
name: ACK_BUS_SCHEMA.name,
|
|
7
|
+
description: ACK_BUS_SCHEMA.description,
|
|
8
|
+
tier: TOOL_TIERS.TIER_1,
|
|
9
|
+
allowedCallerTiers: [CALLER_TIERS.CONTROLLER, CALLER_TIERS.WORKER],
|
|
10
|
+
inputSchema: ACK_BUS_SCHEMA.input_schema,
|
|
11
|
+
outputSchema: ACK_BUS_SCHEMA.output_schema,
|
|
12
|
+
schemaVersion: ACK_BUS_SCHEMA.schema_version,
|
|
13
|
+
handler: ackBusHandler,
|
|
14
|
+
});
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
const { DISPATCH_MESSAGE_SCHEMA } = require("../schemaFixtures");
|
|
2
|
+
const { CALLER_TIERS, TOOL_TIERS, createToolDefinition } = require("../types");
|
|
3
|
+
const { dispatchMessageHandler } = require("../handlers/dispatchMessage");
|
|
4
|
+
|
|
5
|
+
module.exports = createToolDefinition({
|
|
6
|
+
name: DISPATCH_MESSAGE_SCHEMA.name,
|
|
7
|
+
description: DISPATCH_MESSAGE_SCHEMA.description,
|
|
8
|
+
tier: TOOL_TIERS.TIER_1,
|
|
9
|
+
allowedCallerTiers: [CALLER_TIERS.CONTROLLER, CALLER_TIERS.WORKER],
|
|
10
|
+
inputSchema: DISPATCH_MESSAGE_SCHEMA.input_schema,
|
|
11
|
+
outputSchema: DISPATCH_MESSAGE_SCHEMA.output_schema,
|
|
12
|
+
schemaVersion: DISPATCH_MESSAGE_SCHEMA.schema_version,
|
|
13
|
+
handler: dispatchMessageHandler,
|
|
14
|
+
});
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
const { ROUTE_AGENT_SCHEMA } = require("../schemaFixtures");
|
|
2
|
+
const { CALLER_TIERS, TOOL_TIERS, createToolDefinition } = require("../types");
|
|
3
|
+
const { buildDormantHandler } = require("../unimplemented");
|
|
4
|
+
|
|
5
|
+
module.exports = createToolDefinition({
|
|
6
|
+
name: ROUTE_AGENT_SCHEMA.name,
|
|
7
|
+
description: ROUTE_AGENT_SCHEMA.description,
|
|
8
|
+
tier: TOOL_TIERS.TIER_1,
|
|
9
|
+
allowedCallerTiers: [CALLER_TIERS.CONTROLLER, CALLER_TIERS.WORKER],
|
|
10
|
+
inputSchema: ROUTE_AGENT_SCHEMA.input_schema,
|
|
11
|
+
outputSchema: ROUTE_AGENT_SCHEMA.output_schema,
|
|
12
|
+
schemaVersion: ROUTE_AGENT_SCHEMA.schema_version,
|
|
13
|
+
handler: buildDormantHandler(ROUTE_AGENT_SCHEMA.name),
|
|
14
|
+
});
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
const { CLOSE_AGENT_SCHEMA } = require("../schemaFixtures");
|
|
2
|
+
const { CALLER_TIERS, TOOL_TIERS, createToolDefinition } = require("../types");
|
|
3
|
+
const { closeAgentHandler } = require("../handlers/tier2");
|
|
4
|
+
|
|
5
|
+
module.exports = createToolDefinition({
|
|
6
|
+
name: CLOSE_AGENT_SCHEMA.name,
|
|
7
|
+
description: CLOSE_AGENT_SCHEMA.description,
|
|
8
|
+
tier: TOOL_TIERS.TIER_2,
|
|
9
|
+
allowedCallerTiers: [CALLER_TIERS.CONTROLLER],
|
|
10
|
+
inputSchema: CLOSE_AGENT_SCHEMA.input_schema,
|
|
11
|
+
outputSchema: CLOSE_AGENT_SCHEMA.output_schema,
|
|
12
|
+
schemaVersion: CLOSE_AGENT_SCHEMA.schema_version,
|
|
13
|
+
handler: closeAgentHandler,
|
|
14
|
+
});
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
const { LAUNCH_AGENT_SCHEMA } = require("../schemaFixtures");
|
|
2
|
+
const { CALLER_TIERS, TOOL_TIERS, createToolDefinition } = require("../types");
|
|
3
|
+
const { launchAgentHandler } = require("../handlers/tier2");
|
|
4
|
+
|
|
5
|
+
module.exports = createToolDefinition({
|
|
6
|
+
name: LAUNCH_AGENT_SCHEMA.name,
|
|
7
|
+
description: LAUNCH_AGENT_SCHEMA.description,
|
|
8
|
+
tier: TOOL_TIERS.TIER_2,
|
|
9
|
+
allowedCallerTiers: [CALLER_TIERS.CONTROLLER],
|
|
10
|
+
inputSchema: LAUNCH_AGENT_SCHEMA.input_schema,
|
|
11
|
+
outputSchema: LAUNCH_AGENT_SCHEMA.output_schema,
|
|
12
|
+
schemaVersion: LAUNCH_AGENT_SCHEMA.schema_version,
|
|
13
|
+
handler: launchAgentHandler,
|
|
14
|
+
});
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
const { MANAGE_CRON_SCHEMA } = require("../schemaFixtures");
|
|
2
|
+
const { CALLER_TIERS, TOOL_TIERS, createToolDefinition } = require("../types");
|
|
3
|
+
const { manageCronHandler } = require("../handlers/tier2");
|
|
4
|
+
|
|
5
|
+
module.exports = createToolDefinition({
|
|
6
|
+
name: MANAGE_CRON_SCHEMA.name,
|
|
7
|
+
description: MANAGE_CRON_SCHEMA.description,
|
|
8
|
+
tier: TOOL_TIERS.TIER_2,
|
|
9
|
+
allowedCallerTiers: [CALLER_TIERS.CONTROLLER],
|
|
10
|
+
inputSchema: MANAGE_CRON_SCHEMA.input_schema,
|
|
11
|
+
outputSchema: MANAGE_CRON_SCHEMA.output_schema,
|
|
12
|
+
schemaVersion: MANAGE_CRON_SCHEMA.schema_version,
|
|
13
|
+
handler: manageCronHandler,
|
|
14
|
+
});
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
const { RENAME_AGENT_SCHEMA } = require("../schemaFixtures");
|
|
2
|
+
const { CALLER_TIERS, TOOL_TIERS, createToolDefinition } = require("../types");
|
|
3
|
+
const { renameAgentHandler } = require("../handlers/tier2");
|
|
4
|
+
|
|
5
|
+
module.exports = createToolDefinition({
|
|
6
|
+
name: RENAME_AGENT_SCHEMA.name,
|
|
7
|
+
description: RENAME_AGENT_SCHEMA.description,
|
|
8
|
+
tier: TOOL_TIERS.TIER_2,
|
|
9
|
+
allowedCallerTiers: [CALLER_TIERS.CONTROLLER],
|
|
10
|
+
inputSchema: RENAME_AGENT_SCHEMA.input_schema,
|
|
11
|
+
outputSchema: RENAME_AGENT_SCHEMA.output_schema,
|
|
12
|
+
schemaVersion: RENAME_AGENT_SCHEMA.schema_version,
|
|
13
|
+
handler: renameAgentHandler,
|
|
14
|
+
});
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
const TOOL_TIERS = Object.freeze({
|
|
2
|
+
TIER_0: "tier0-read",
|
|
3
|
+
TIER_1: "tier1-coordination",
|
|
4
|
+
TIER_2: "tier2-orchestration",
|
|
5
|
+
});
|
|
6
|
+
|
|
7
|
+
const CALLER_TIERS = Object.freeze({
|
|
8
|
+
CONTROLLER: "controller",
|
|
9
|
+
WORKER: "worker",
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
const DEFAULT_SCHEMA_VERSION = "1.0";
|
|
13
|
+
|
|
14
|
+
function normalizeCallerTier(value) {
|
|
15
|
+
return String(value || "").trim().toLowerCase();
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
function createToolDefinition({
|
|
19
|
+
name,
|
|
20
|
+
description,
|
|
21
|
+
tier,
|
|
22
|
+
allowedCallerTiers,
|
|
23
|
+
inputSchema,
|
|
24
|
+
outputSchema,
|
|
25
|
+
handler,
|
|
26
|
+
schemaVersion,
|
|
27
|
+
}) {
|
|
28
|
+
const allowed = Object.freeze([...(allowedCallerTiers || [])]);
|
|
29
|
+
return Object.freeze({
|
|
30
|
+
name,
|
|
31
|
+
description,
|
|
32
|
+
tier,
|
|
33
|
+
schema_version: String(schemaVersion || DEFAULT_SCHEMA_VERSION),
|
|
34
|
+
allowedCallerTiers: allowed,
|
|
35
|
+
allowed_tiers: allowed,
|
|
36
|
+
input_schema: Object.freeze(inputSchema),
|
|
37
|
+
output_schema: outputSchema ? Object.freeze(outputSchema) : null,
|
|
38
|
+
handler,
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function buildCallerTierError(toolDef, callerTier, auditCtx = {}) {
|
|
43
|
+
const tier = normalizeCallerTier(callerTier) || "unknown";
|
|
44
|
+
const message = `caller_tier "${tier}" is not allowed to invoke tool "${toolDef.name}"`;
|
|
45
|
+
const err = new Error(message);
|
|
46
|
+
err.code = "forbidden_caller_tier";
|
|
47
|
+
err.tool_name = toolDef.name;
|
|
48
|
+
err.caller_tier = tier;
|
|
49
|
+
err.allowed_tiers = Array.isArray(toolDef.allowed_tiers) ? toolDef.allowed_tiers.slice() : [];
|
|
50
|
+
if (auditCtx && auditCtx.turn_id) err.turn_id = String(auditCtx.turn_id);
|
|
51
|
+
if (auditCtx && auditCtx.tool_call_id) err.tool_call_id = String(auditCtx.tool_call_id);
|
|
52
|
+
return err;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function assertCallerTierAllowed(toolDef, callerTier, auditCtx = {}) {
|
|
56
|
+
if (!toolDef || !Array.isArray(toolDef.allowed_tiers)) {
|
|
57
|
+
const err = new Error("tool definition missing allowed_tiers metadata");
|
|
58
|
+
err.code = "invalid_tool_definition";
|
|
59
|
+
throw err;
|
|
60
|
+
}
|
|
61
|
+
const tier = normalizeCallerTier(callerTier);
|
|
62
|
+
if (!tier || !toolDef.allowed_tiers.includes(tier)) {
|
|
63
|
+
throw buildCallerTierError(toolDef, callerTier, auditCtx);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
module.exports = {
|
|
68
|
+
TOOL_TIERS,
|
|
69
|
+
CALLER_TIERS,
|
|
70
|
+
DEFAULT_SCHEMA_VERSION,
|
|
71
|
+
createToolDefinition,
|
|
72
|
+
assertCallerTierAllowed,
|
|
73
|
+
buildCallerTierError,
|
|
74
|
+
normalizeCallerTier,
|
|
75
|
+
};
|
package/src/ufoo/paths.js
CHANGED
|
@@ -4,7 +4,9 @@ function getUfooPaths(projectRoot) {
|
|
|
4
4
|
const ufooDir = path.join(projectRoot, ".ufoo");
|
|
5
5
|
const busDir = path.join(ufooDir, "bus");
|
|
6
6
|
const agentDir = path.join(ufooDir, "agent");
|
|
7
|
+
const memoryDir = path.join(ufooDir, "memory");
|
|
7
8
|
const agentsFile = path.join(agentDir, "all-agents.json");
|
|
9
|
+
const memoryFile = path.join(memoryDir, "memory.jsonl");
|
|
8
10
|
|
|
9
11
|
const busQueuesDir = path.join(busDir, "queues");
|
|
10
12
|
const busEventsDir = path.join(busDir, "events");
|
|
@@ -27,7 +29,9 @@ function getUfooPaths(projectRoot) {
|
|
|
27
29
|
ufooDir,
|
|
28
30
|
busDir,
|
|
29
31
|
agentDir,
|
|
32
|
+
memoryDir,
|
|
30
33
|
agentsFile,
|
|
34
|
+
memoryFile,
|
|
31
35
|
busQueuesDir,
|
|
32
36
|
busEventsDir,
|
|
33
37
|
busLogsDir,
|
package/bin/ufoo-engine.js
DELETED
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
|
|
3
|
-
const { runUfooEngineCli } = require("../src/assistant/ufooEngineCli");
|
|
4
|
-
|
|
5
|
-
function readStdin() {
|
|
6
|
-
return new Promise((resolve) => {
|
|
7
|
-
let data = "";
|
|
8
|
-
process.stdin.setEncoding("utf8");
|
|
9
|
-
process.stdin.on("data", (chunk) => {
|
|
10
|
-
data += chunk;
|
|
11
|
-
});
|
|
12
|
-
process.stdin.on("end", () => resolve(data));
|
|
13
|
-
process.stdin.resume();
|
|
14
|
-
});
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
(async () => {
|
|
18
|
-
const stdinText = await readStdin();
|
|
19
|
-
const result = await runUfooEngineCli({
|
|
20
|
-
argv: process.argv.slice(2),
|
|
21
|
-
stdinText,
|
|
22
|
-
});
|
|
23
|
-
process.stdout.write(result.output);
|
|
24
|
-
process.exitCode = result.exitCode;
|
|
25
|
-
})();
|
package/src/assistant/agent.js
DELETED
|
@@ -1,261 +0,0 @@
|
|
|
1
|
-
const fs = require("fs");
|
|
2
|
-
const path = require("path");
|
|
3
|
-
const { runCliAgent } = require("../agent/cliRunner");
|
|
4
|
-
const { normalizeCliOutput } = require("../agent/normalizeOutput");
|
|
5
|
-
const { resolveAssistantEngine, runExternalAssistantEngine } = require("./engine");
|
|
6
|
-
const { DEFAULT_ASSISTANT_TIMEOUT_MS, normalizeAssistantTimeoutMs } = require("./constants");
|
|
7
|
-
const { getUfooPaths } = require("../ufoo/paths");
|
|
8
|
-
|
|
9
|
-
const ASSISTANT_JSON_SCHEMA = {
|
|
10
|
-
type: "object",
|
|
11
|
-
properties: {
|
|
12
|
-
ok: { type: "boolean" },
|
|
13
|
-
summary: { type: "string" },
|
|
14
|
-
artifacts: {
|
|
15
|
-
type: "array",
|
|
16
|
-
items: { type: "string" },
|
|
17
|
-
},
|
|
18
|
-
logs: {
|
|
19
|
-
type: "array",
|
|
20
|
-
items: { type: "string" },
|
|
21
|
-
},
|
|
22
|
-
error: { type: "string" },
|
|
23
|
-
metrics: {
|
|
24
|
-
type: "object",
|
|
25
|
-
additionalProperties: true,
|
|
26
|
-
},
|
|
27
|
-
},
|
|
28
|
-
required: ["ok", "summary"],
|
|
29
|
-
};
|
|
30
|
-
|
|
31
|
-
function parseTaskPayload(payload = {}) {
|
|
32
|
-
const projectRoot = typeof payload.project_root === "string" ? payload.project_root : process.cwd();
|
|
33
|
-
const provider = typeof payload.provider === "string" ? payload.provider : "";
|
|
34
|
-
const fallbackProvider = typeof payload.fallback_provider === "string" ? payload.fallback_provider : "";
|
|
35
|
-
const model = typeof payload.model === "string" ? payload.model : "";
|
|
36
|
-
const task = typeof payload.task === "string" ? payload.task.trim() : "";
|
|
37
|
-
const kind = typeof payload.kind === "string" && payload.kind ? payload.kind : "mixed";
|
|
38
|
-
const context = typeof payload.context === "string" ? payload.context : "";
|
|
39
|
-
const expectText = typeof payload.expect === "string" ? payload.expect : "";
|
|
40
|
-
const timeoutMs = normalizeAssistantTimeoutMs(payload.timeout_ms, DEFAULT_ASSISTANT_TIMEOUT_MS);
|
|
41
|
-
|
|
42
|
-
return {
|
|
43
|
-
projectRoot,
|
|
44
|
-
provider,
|
|
45
|
-
fallbackProvider,
|
|
46
|
-
model,
|
|
47
|
-
task,
|
|
48
|
-
kind,
|
|
49
|
-
context,
|
|
50
|
-
expect: expectText,
|
|
51
|
-
timeoutMs,
|
|
52
|
-
};
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
function normalizeAssistantPayload(parsed, fallbackError = "") {
|
|
56
|
-
if (!parsed || typeof parsed !== "object") {
|
|
57
|
-
const text = String(parsed || "").trim();
|
|
58
|
-
if (text) {
|
|
59
|
-
return {
|
|
60
|
-
ok: true,
|
|
61
|
-
summary: text,
|
|
62
|
-
artifacts: [],
|
|
63
|
-
logs: [],
|
|
64
|
-
error: "",
|
|
65
|
-
metrics: {},
|
|
66
|
-
};
|
|
67
|
-
}
|
|
68
|
-
return {
|
|
69
|
-
ok: false,
|
|
70
|
-
summary: "",
|
|
71
|
-
artifacts: [],
|
|
72
|
-
logs: [],
|
|
73
|
-
error: fallbackError || "assistant returned invalid payload",
|
|
74
|
-
metrics: {},
|
|
75
|
-
};
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
return {
|
|
79
|
-
ok: parsed.ok !== false,
|
|
80
|
-
summary: typeof parsed.summary === "string" ? parsed.summary : "",
|
|
81
|
-
artifacts: Array.isArray(parsed.artifacts) ? parsed.artifacts : [],
|
|
82
|
-
logs: Array.isArray(parsed.logs) ? parsed.logs : [],
|
|
83
|
-
error: typeof parsed.error === "string" ? parsed.error : "",
|
|
84
|
-
metrics: parsed.metrics && typeof parsed.metrics === "object" ? parsed.metrics : {},
|
|
85
|
-
};
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
function buildAssistantSystemPrompt(taskInput) {
|
|
89
|
-
return [
|
|
90
|
-
"You are ufoo-assistant-agent, a private helper for ufoo-agent.",
|
|
91
|
-
"You are NOT exposed on the event bus.",
|
|
92
|
-
"Execute the requested task using local project context and shell/tool access as needed.",
|
|
93
|
-
"Return ONLY JSON that matches schema: {ok, summary, artifacts, logs, error, metrics}.",
|
|
94
|
-
"Rules:",
|
|
95
|
-
"- summary: concise factual result for ufoo-agent to consume.",
|
|
96
|
-
"- artifacts: key files/commands/findings (short strings).",
|
|
97
|
-
"- logs: optional concise trace points.",
|
|
98
|
-
"- error: non-empty only when ok=false.",
|
|
99
|
-
"- Do not include markdown or prose outside JSON.",
|
|
100
|
-
"",
|
|
101
|
-
"Task input:",
|
|
102
|
-
JSON.stringify(taskInput),
|
|
103
|
-
].join("\n");
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
function getAssistantStatePaths(projectRoot) {
|
|
107
|
-
const dir = getUfooPaths(projectRoot).agentDir;
|
|
108
|
-
return {
|
|
109
|
-
sessionDir: path.join(dir, "sessions"),
|
|
110
|
-
};
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
function getAssistantSessionStateFile(projectRoot, engine = "assistant") {
|
|
114
|
-
const { sessionDir } = getAssistantStatePaths(projectRoot);
|
|
115
|
-
return path.join(sessionDir, `ufoo-assistant-${engine}.json`);
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
function loadAssistantState(projectRoot, engine = "assistant") {
|
|
119
|
-
const file = getAssistantSessionStateFile(projectRoot, engine);
|
|
120
|
-
try {
|
|
121
|
-
return JSON.parse(fs.readFileSync(file, "utf8"));
|
|
122
|
-
} catch {
|
|
123
|
-
return null;
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
function saveAssistantState(projectRoot, engine = "assistant", state = {}) {
|
|
128
|
-
const { sessionDir } = getAssistantStatePaths(projectRoot);
|
|
129
|
-
fs.mkdirSync(sessionDir, { recursive: true });
|
|
130
|
-
const file = getAssistantSessionStateFile(projectRoot, engine);
|
|
131
|
-
fs.writeFileSync(file, JSON.stringify(state, null, 2));
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
function isSessionError(errorText = "") {
|
|
135
|
-
const text = String(errorText || "").toLowerCase();
|
|
136
|
-
return text.includes("session id")
|
|
137
|
-
|| text.includes("session-id")
|
|
138
|
-
|| text.includes("already in use");
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
async function runAssistantAgentTask(payload = {}) {
|
|
142
|
-
const taskInput = parseTaskPayload(payload);
|
|
143
|
-
const startedAt = Date.now();
|
|
144
|
-
|
|
145
|
-
if (!taskInput.task) {
|
|
146
|
-
return {
|
|
147
|
-
ok: false,
|
|
148
|
-
summary: "",
|
|
149
|
-
artifacts: [],
|
|
150
|
-
logs: [],
|
|
151
|
-
error: "missing task",
|
|
152
|
-
metrics: { duration_ms: Date.now() - startedAt },
|
|
153
|
-
};
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
const systemPrompt = buildAssistantSystemPrompt(taskInput);
|
|
157
|
-
const engine = resolveAssistantEngine({
|
|
158
|
-
projectRoot: taskInput.projectRoot,
|
|
159
|
-
requestedProvider: taskInput.provider,
|
|
160
|
-
requestedModel: taskInput.model,
|
|
161
|
-
fallbackProvider: taskInput.fallbackProvider,
|
|
162
|
-
});
|
|
163
|
-
const assistantState = loadAssistantState(taskInput.projectRoot, engine.engine);
|
|
164
|
-
|
|
165
|
-
let cliRes = null;
|
|
166
|
-
if (engine.kind === "external") {
|
|
167
|
-
cliRes = await runExternalAssistantEngine({
|
|
168
|
-
engine,
|
|
169
|
-
timeoutMs: taskInput.timeoutMs,
|
|
170
|
-
payload: {
|
|
171
|
-
request_type: "assistant_task",
|
|
172
|
-
schema_version: 1,
|
|
173
|
-
engine: engine.engine,
|
|
174
|
-
project_root: taskInput.projectRoot,
|
|
175
|
-
task: taskInput.task,
|
|
176
|
-
kind: taskInput.kind,
|
|
177
|
-
context: taskInput.context,
|
|
178
|
-
expect: taskInput.expect,
|
|
179
|
-
model: engine.model || "",
|
|
180
|
-
session_id: assistantState && typeof assistantState.sessionId === "string" ? assistantState.sessionId : "",
|
|
181
|
-
timeout_ms: taskInput.timeoutMs,
|
|
182
|
-
},
|
|
183
|
-
});
|
|
184
|
-
} else {
|
|
185
|
-
const runCli = async (sessionId) => runCliAgent({
|
|
186
|
-
provider: engine.provider,
|
|
187
|
-
model: engine.model,
|
|
188
|
-
prompt: taskInput.task,
|
|
189
|
-
systemPrompt,
|
|
190
|
-
jsonSchema: ASSISTANT_JSON_SCHEMA,
|
|
191
|
-
disableSession: false,
|
|
192
|
-
sessionId,
|
|
193
|
-
cwd: taskInput.projectRoot,
|
|
194
|
-
timeoutMs: taskInput.timeoutMs,
|
|
195
|
-
sandbox: taskInput.kind === "explore" ? "read-only" : "workspace-write",
|
|
196
|
-
});
|
|
197
|
-
|
|
198
|
-
const preferredSession = assistantState && typeof assistantState.sessionId === "string"
|
|
199
|
-
? assistantState.sessionId
|
|
200
|
-
: undefined;
|
|
201
|
-
cliRes = await runCli(preferredSession);
|
|
202
|
-
if (!cliRes.ok && preferredSession && isSessionError(cliRes.error)) {
|
|
203
|
-
cliRes = await runCli(undefined);
|
|
204
|
-
}
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
if (!cliRes || cliRes.ok === false) {
|
|
208
|
-
return {
|
|
209
|
-
ok: false,
|
|
210
|
-
summary: "",
|
|
211
|
-
artifacts: [],
|
|
212
|
-
logs: [],
|
|
213
|
-
error: (cliRes && cliRes.error) || "assistant cli failed",
|
|
214
|
-
metrics: { duration_ms: Date.now() - startedAt },
|
|
215
|
-
};
|
|
216
|
-
}
|
|
217
|
-
|
|
218
|
-
let result;
|
|
219
|
-
if (engine.kind === "external") {
|
|
220
|
-
result = normalizeAssistantPayload(cliRes);
|
|
221
|
-
} else {
|
|
222
|
-
const normalized = normalizeCliOutput(cliRes.output);
|
|
223
|
-
let parsed;
|
|
224
|
-
try {
|
|
225
|
-
parsed = JSON.parse(normalized);
|
|
226
|
-
} catch {
|
|
227
|
-
parsed = normalized;
|
|
228
|
-
}
|
|
229
|
-
result = normalizeAssistantPayload(parsed);
|
|
230
|
-
}
|
|
231
|
-
|
|
232
|
-
result.metrics = {
|
|
233
|
-
...result.metrics,
|
|
234
|
-
duration_ms: Date.now() - startedAt,
|
|
235
|
-
};
|
|
236
|
-
if (!result.ok && !result.error) {
|
|
237
|
-
result.error = "assistant task failed";
|
|
238
|
-
}
|
|
239
|
-
|
|
240
|
-
saveAssistantState(taskInput.projectRoot, engine.engine, {
|
|
241
|
-
engine: engine.engine,
|
|
242
|
-
provider: engine.provider || "",
|
|
243
|
-
model: engine.model || "",
|
|
244
|
-
sessionId: cliRes && typeof cliRes.sessionId === "string" ? cliRes.sessionId : "",
|
|
245
|
-
updated_at: new Date().toISOString(),
|
|
246
|
-
});
|
|
247
|
-
|
|
248
|
-
return result;
|
|
249
|
-
}
|
|
250
|
-
|
|
251
|
-
module.exports = {
|
|
252
|
-
runAssistantAgentTask,
|
|
253
|
-
parseTaskPayload,
|
|
254
|
-
normalizeAssistantPayload,
|
|
255
|
-
buildAssistantSystemPrompt,
|
|
256
|
-
getAssistantSessionStateFile,
|
|
257
|
-
loadAssistantState,
|
|
258
|
-
saveAssistantState,
|
|
259
|
-
isSessionError,
|
|
260
|
-
ASSISTANT_JSON_SCHEMA,
|
|
261
|
-
};
|