@synergenius/flow-weaver-pack-weaver 0.9.0 → 0.9.4
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/bot/ai-client.d.ts +22 -2
- package/dist/bot/ai-client.d.ts.map +1 -1
- package/dist/bot/ai-client.js +168 -20
- package/dist/bot/ai-client.js.map +1 -1
- package/dist/bot/ansi.d.ts +13 -0
- package/dist/bot/ansi.d.ts.map +1 -0
- package/dist/bot/ansi.js +13 -0
- package/dist/bot/ansi.js.map +1 -0
- package/dist/bot/assistant-core.d.ts +25 -0
- package/dist/bot/assistant-core.d.ts.map +1 -0
- package/dist/bot/assistant-core.js +272 -0
- package/dist/bot/assistant-core.js.map +1 -0
- package/dist/bot/assistant-tools.d.ts +10 -0
- package/dist/bot/assistant-tools.d.ts.map +1 -0
- package/dist/bot/assistant-tools.js +324 -0
- package/dist/bot/assistant-tools.js.map +1 -0
- package/dist/bot/audit-logger.d.ts.map +1 -1
- package/dist/bot/audit-logger.js +9 -5
- package/dist/bot/audit-logger.js.map +1 -1
- package/dist/bot/bot-manager.d.ts +49 -0
- package/dist/bot/bot-manager.d.ts.map +1 -0
- package/dist/bot/bot-manager.js +279 -0
- package/dist/bot/bot-manager.js.map +1 -0
- package/dist/bot/child-process-tracker.d.ts +6 -0
- package/dist/bot/child-process-tracker.d.ts.map +1 -0
- package/dist/bot/child-process-tracker.js +35 -0
- package/dist/bot/child-process-tracker.js.map +1 -0
- package/dist/bot/cli-provider.d.ts.map +1 -1
- package/dist/bot/cli-provider.js +13 -8
- package/dist/bot/cli-provider.js.map +1 -1
- package/dist/bot/conversation-store.d.ts +40 -0
- package/dist/bot/conversation-store.d.ts.map +1 -0
- package/dist/bot/conversation-store.js +182 -0
- package/dist/bot/conversation-store.js.map +1 -0
- package/dist/bot/error-classifier.d.ts +27 -0
- package/dist/bot/error-classifier.d.ts.map +1 -0
- package/dist/bot/error-classifier.js +71 -0
- package/dist/bot/error-classifier.js.map +1 -0
- package/dist/bot/error-guide.d.ts +5 -0
- package/dist/bot/error-guide.d.ts.map +1 -0
- package/dist/bot/error-guide.js +5 -0
- package/dist/bot/error-guide.js.map +1 -0
- package/dist/bot/knowledge-store.d.ts +17 -0
- package/dist/bot/knowledge-store.d.ts.map +1 -0
- package/dist/bot/knowledge-store.js +53 -0
- package/dist/bot/knowledge-store.js.map +1 -0
- package/dist/bot/paths.d.ts +11 -0
- package/dist/bot/paths.d.ts.map +1 -0
- package/dist/bot/paths.js +26 -0
- package/dist/bot/paths.js.map +1 -0
- package/dist/bot/retry-utils.d.ts +5 -0
- package/dist/bot/retry-utils.d.ts.map +1 -0
- package/dist/bot/retry-utils.js +5 -0
- package/dist/bot/retry-utils.js.map +1 -0
- package/dist/bot/runner.d.ts.map +1 -1
- package/dist/bot/runner.js +12 -1
- package/dist/bot/runner.js.map +1 -1
- package/dist/bot/safety.d.ts +10 -0
- package/dist/bot/safety.d.ts.map +1 -0
- package/dist/bot/safety.js +14 -0
- package/dist/bot/safety.js.map +1 -0
- package/dist/bot/session-state.d.ts.map +1 -1
- package/dist/bot/session-state.js +3 -1
- package/dist/bot/session-state.js.map +1 -1
- package/dist/bot/steering.js +2 -2
- package/dist/bot/steering.js.map +1 -1
- package/dist/bot/step-executor.d.ts +10 -5
- package/dist/bot/step-executor.d.ts.map +1 -1
- package/dist/bot/step-executor.js +252 -3
- package/dist/bot/step-executor.js.map +1 -1
- package/dist/bot/system-prompt.d.ts +1 -1
- package/dist/bot/system-prompt.d.ts.map +1 -1
- package/dist/bot/system-prompt.js +69 -43
- package/dist/bot/system-prompt.js.map +1 -1
- package/dist/bot/task-decomposer.d.ts +24 -0
- package/dist/bot/task-decomposer.d.ts.map +1 -0
- package/dist/bot/task-decomposer.js +75 -0
- package/dist/bot/task-decomposer.js.map +1 -0
- package/dist/bot/task-queue.d.ts +17 -4
- package/dist/bot/task-queue.d.ts.map +1 -1
- package/dist/bot/task-queue.js +83 -5
- package/dist/bot/task-queue.js.map +1 -1
- package/dist/bot/terminal-renderer.d.ts +60 -0
- package/dist/bot/terminal-renderer.d.ts.map +1 -0
- package/dist/bot/terminal-renderer.js +204 -0
- package/dist/bot/terminal-renderer.js.map +1 -0
- package/dist/bot/tool-registry.d.ts +24 -0
- package/dist/bot/tool-registry.d.ts.map +1 -0
- package/dist/bot/tool-registry.js +458 -0
- package/dist/bot/tool-registry.js.map +1 -0
- package/dist/bot/types.d.ts +7 -0
- package/dist/bot/types.d.ts.map +1 -1
- package/dist/bot/weaver-tools.d.ts +18 -0
- package/dist/bot/weaver-tools.d.ts.map +1 -0
- package/dist/bot/weaver-tools.js +124 -0
- package/dist/bot/weaver-tools.js.map +1 -0
- package/dist/cli-bridge.d.ts.map +1 -1
- package/dist/cli-bridge.js +5 -1
- package/dist/cli-bridge.js.map +1 -1
- package/dist/cli-handlers.d.ts +13 -1
- package/dist/cli-handlers.d.ts.map +1 -1
- package/dist/cli-handlers.js +615 -48
- package/dist/cli-handlers.js.map +1 -1
- package/dist/mcp-tools.js +2 -2
- package/dist/mcp-tools.js.map +1 -1
- package/dist/node-types/abort-task.d.ts.map +1 -1
- package/dist/node-types/abort-task.js +4 -3
- package/dist/node-types/abort-task.js.map +1 -1
- package/dist/node-types/agent-execute.d.ts +38 -0
- package/dist/node-types/agent-execute.d.ts.map +1 -0
- package/dist/node-types/agent-execute.js +252 -0
- package/dist/node-types/agent-execute.js.map +1 -0
- package/dist/node-types/bot-report.d.ts +5 -3
- package/dist/node-types/bot-report.d.ts.map +1 -1
- package/dist/node-types/bot-report.js +39 -7
- package/dist/node-types/bot-report.js.map +1 -1
- package/dist/node-types/build-context.d.ts +3 -3
- package/dist/node-types/build-context.d.ts.map +1 -1
- package/dist/node-types/build-context.js +108 -24
- package/dist/node-types/build-context.js.map +1 -1
- package/dist/node-types/detect-provider.d.ts +2 -2
- package/dist/node-types/detect-provider.d.ts.map +1 -1
- package/dist/node-types/detect-provider.js +3 -1
- package/dist/node-types/detect-provider.js.map +1 -1
- package/dist/node-types/exec-validate-retry.d.ts.map +1 -1
- package/dist/node-types/exec-validate-retry.js +43 -6
- package/dist/node-types/exec-validate-retry.js.map +1 -1
- package/dist/node-types/execute-plan.d.ts.map +1 -1
- package/dist/node-types/execute-plan.js +31 -8
- package/dist/node-types/execute-plan.js.map +1 -1
- package/dist/node-types/execute-target.d.ts.map +1 -1
- package/dist/node-types/execute-target.js +3 -1
- package/dist/node-types/execute-target.js.map +1 -1
- package/dist/node-types/fix-errors.d.ts.map +1 -1
- package/dist/node-types/fix-errors.js +21 -5
- package/dist/node-types/fix-errors.js.map +1 -1
- package/dist/node-types/genesis-observe.d.ts.map +1 -1
- package/dist/node-types/genesis-observe.js +3 -1
- package/dist/node-types/genesis-observe.js.map +1 -1
- package/dist/node-types/genesis-report.js +4 -1
- package/dist/node-types/genesis-report.js.map +1 -1
- package/dist/node-types/git-ops.d.ts.map +1 -1
- package/dist/node-types/git-ops.js +98 -4
- package/dist/node-types/git-ops.js.map +1 -1
- package/dist/node-types/index.d.ts +2 -0
- package/dist/node-types/index.d.ts.map +1 -1
- package/dist/node-types/index.js +2 -0
- package/dist/node-types/index.js.map +1 -1
- package/dist/node-types/load-config.d.ts +2 -2
- package/dist/node-types/load-config.d.ts.map +1 -1
- package/dist/node-types/load-config.js.map +1 -1
- package/dist/node-types/plan-task.d.ts.map +1 -1
- package/dist/node-types/plan-task.js +14 -2
- package/dist/node-types/plan-task.js.map +1 -1
- package/dist/node-types/read-workflow.js +8 -2
- package/dist/node-types/read-workflow.js.map +1 -1
- package/dist/node-types/receive-task.d.ts.map +1 -1
- package/dist/node-types/receive-task.js +35 -26
- package/dist/node-types/receive-task.js.map +1 -1
- package/dist/node-types/send-notify.js +2 -1
- package/dist/node-types/send-notify.js.map +1 -1
- package/dist/node-types/validate-gate.d.ts +18 -0
- package/dist/node-types/validate-gate.d.ts.map +1 -0
- package/dist/node-types/validate-gate.js +96 -0
- package/dist/node-types/validate-gate.js.map +1 -0
- package/dist/workflows/genesis-task.d.ts +20 -12
- package/dist/workflows/genesis-task.d.ts.map +1 -1
- package/dist/workflows/genesis-task.js +20 -12
- package/dist/workflows/genesis-task.js.map +1 -1
- package/dist/workflows/weaver-agent.d.ts +35 -0
- package/dist/workflows/weaver-agent.d.ts.map +1 -0
- package/dist/workflows/weaver-agent.js +777 -0
- package/dist/workflows/weaver-agent.js.map +1 -0
- package/dist/workflows/weaver-bot-batch.d.ts +19 -26
- package/dist/workflows/weaver-bot-batch.d.ts.map +1 -1
- package/dist/workflows/weaver-bot-batch.js +1043 -27
- package/dist/workflows/weaver-bot-batch.js.map +1 -1
- package/dist/workflows/weaver-bot.d.ts +21 -35
- package/dist/workflows/weaver-bot.d.ts.map +1 -1
- package/dist/workflows/weaver-bot.js +1119 -36
- package/dist/workflows/weaver-bot.js.map +1 -1
- package/flowweaver.manifest.json +21 -1
- package/package.json +5 -2
- package/src/bot/ai-client.ts +180 -19
- package/src/bot/ansi.ts +12 -0
- package/src/bot/assistant-core.ts +312 -0
- package/src/bot/assistant-tools.ts +318 -0
- package/src/bot/audit-logger.ts +6 -5
- package/src/bot/bot-manager.ts +293 -0
- package/src/bot/child-process-tracker.ts +40 -0
- package/src/bot/cli-provider.ts +13 -8
- package/src/bot/conversation-store.ts +222 -0
- package/src/bot/error-classifier.ts +90 -0
- package/src/bot/error-guide.ts +4 -0
- package/src/bot/knowledge-store.ts +59 -0
- package/src/bot/paths.ts +27 -0
- package/src/bot/retry-utils.ts +4 -0
- package/src/bot/runner.ts +12 -1
- package/src/bot/safety.ts +16 -0
- package/src/bot/session-state.ts +2 -1
- package/src/bot/steering.ts +2 -2
- package/src/bot/step-executor.ts +313 -5
- package/src/bot/system-prompt.ts +70 -47
- package/src/bot/task-decomposer.ts +100 -0
- package/src/bot/task-queue.ts +100 -8
- package/src/bot/terminal-renderer.ts +238 -0
- package/src/bot/tool-registry.ts +477 -0
- package/src/bot/types.ts +8 -0
- package/src/bot/weaver-tools.ts +134 -0
- package/src/cli-bridge.ts +7 -1
- package/src/cli-handlers.ts +624 -48
- package/src/mcp-tools.ts +2 -2
- package/src/node-types/abort-task.ts +5 -4
- package/src/node-types/agent-execute.ts +303 -0
- package/src/node-types/bot-report.ts +40 -9
- package/src/node-types/build-context.ts +112 -25
- package/src/node-types/detect-provider.ts +4 -3
- package/src/node-types/exec-validate-retry.ts +47 -8
- package/src/node-types/execute-plan.ts +32 -8
- package/src/node-types/execute-target.ts +2 -1
- package/src/node-types/fix-errors.ts +20 -5
- package/src/node-types/genesis-observe.ts +2 -1
- package/src/node-types/genesis-report.ts +1 -1
- package/src/node-types/git-ops.ts +93 -4
- package/src/node-types/index.ts +2 -0
- package/src/node-types/load-config.ts +3 -3
- package/src/node-types/plan-task.ts +15 -3
- package/src/node-types/read-workflow.ts +2 -2
- package/src/node-types/receive-task.ts +31 -26
- package/src/node-types/send-notify.ts +1 -1
- package/src/node-types/validate-gate.ts +112 -0
- package/src/workflows/genesis-task.ts +20 -12
- package/src/workflows/weaver-agent.ts +799 -0
- package/src/workflows/weaver-bot-batch.ts +1049 -27
- package/src/workflows/weaver-bot.ts +1123 -36
|
@@ -0,0 +1,312 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Assistant Core — provider-agnostic conversational loop.
|
|
3
|
+
* The user types, the assistant responds with text and tool calls.
|
|
4
|
+
* UI-agnostic: terminal is one frontend, platform AI chat is another.
|
|
5
|
+
*
|
|
6
|
+
* Supports two provider modes:
|
|
7
|
+
* - CLI provider: tools handled internally via MCP bridge (tool_result events)
|
|
8
|
+
* - API provider: tools collected and executed manually (tool_use_start/end events)
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import * as readline from 'node:readline';
|
|
12
|
+
import {
|
|
13
|
+
runAgentLoop,
|
|
14
|
+
type AgentProvider,
|
|
15
|
+
type AgentMessage,
|
|
16
|
+
type ToolDefinition,
|
|
17
|
+
type ToolExecutor,
|
|
18
|
+
type StreamEvent,
|
|
19
|
+
} from '@synergenius/flow-weaver/agent';
|
|
20
|
+
import { c } from './ansi.js';
|
|
21
|
+
import { VERBOSE_TOOL_NAMES } from './tool-registry.js';
|
|
22
|
+
import { generateToolPromptSection, generateVerboseToolList } from './tool-registry.js';
|
|
23
|
+
import { CHARS_PER_TOKEN } from './safety.js';
|
|
24
|
+
|
|
25
|
+
export interface AssistantOptions {
|
|
26
|
+
provider: AgentProvider;
|
|
27
|
+
tools: ToolDefinition[];
|
|
28
|
+
executor: ToolExecutor;
|
|
29
|
+
projectDir: string;
|
|
30
|
+
systemPrompt?: string;
|
|
31
|
+
/** Override for testing — provide messages instead of reading stdin */
|
|
32
|
+
inputMessages?: string[];
|
|
33
|
+
/** Resume a specific conversation by ID */
|
|
34
|
+
resumeId?: string;
|
|
35
|
+
/** Always start a fresh conversation */
|
|
36
|
+
newConversation?: boolean;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const DEFAULT_SYSTEM_PROMPT = `You are Weaver Assistant — a director-level AI that manages bot workers and the flow-weaver ecosystem.
|
|
40
|
+
|
|
41
|
+
You help users with the following tools (grouped by category):
|
|
42
|
+
|
|
43
|
+
${generateToolPromptSection()}
|
|
44
|
+
USE TOOLS to fulfill requests. Don't describe what you'd do — actually do it.
|
|
45
|
+
When the user asks to "start a bot", call bot_spawn.
|
|
46
|
+
When they ask for status, call bot_list or bot_status.
|
|
47
|
+
When they ask to add tasks, call queue_add or queue_add_batch.
|
|
48
|
+
|
|
49
|
+
Be concise. Show results, not explanations.
|
|
50
|
+
The user is a senior engineer — don't over-explain.
|
|
51
|
+
|
|
52
|
+
CRITICAL: You are running in a terminal. Do NOT use markdown formatting.
|
|
53
|
+
- No **bold**, no _italic_, no \`backticks\`, no tables with |pipes|
|
|
54
|
+
- No emoji (✅, 🔴, etc.)
|
|
55
|
+
- Use plain text with indentation for structure
|
|
56
|
+
- Use UPPERCASE or quotes for emphasis instead of markdown
|
|
57
|
+
- For lists, use simple dashes: - item
|
|
58
|
+
- For key-value pairs, use: key: value (one per line)
|
|
59
|
+
- Keep output scannable and clean
|
|
60
|
+
|
|
61
|
+
IMPORTANT: Some tool results are displayed DIRECTLY to the user in the terminal.
|
|
62
|
+
These tools show FULL output — the user already sees everything:
|
|
63
|
+
${generateVerboseToolList()}
|
|
64
|
+
For these: do NOT repeat, summarize, or reformat the output. Just add a brief comment if needed.
|
|
65
|
+
Never re-type ASCII art, diagrams, or large text blocks that were already printed.
|
|
66
|
+
|
|
67
|
+
Other tools show only a short preview.
|
|
68
|
+
For those: you may summarize or explain the result as needed.`;
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
export async function runAssistant(opts: AssistantOptions): Promise<void> {
|
|
72
|
+
const { provider, tools, executor, projectDir } = opts;
|
|
73
|
+
|
|
74
|
+
// Build system prompt — include project plan if it exists
|
|
75
|
+
let systemPrompt = opts.systemPrompt ?? DEFAULT_SYSTEM_PROMPT;
|
|
76
|
+
try {
|
|
77
|
+
const fs = await import('node:fs');
|
|
78
|
+
const path = await import('node:path');
|
|
79
|
+
const planPath = path.resolve(projectDir, '.weaver-plan.md');
|
|
80
|
+
if (fs.existsSync(planPath)) {
|
|
81
|
+
const plan = fs.readFileSync(planPath, 'utf-8').trim();
|
|
82
|
+
systemPrompt += '\n\n## Project Plan & Vision\n\nAll bots you spawn and tasks you queue MUST align with this plan.\n\n' + plan;
|
|
83
|
+
}
|
|
84
|
+
} catch { /* plan not available */ }
|
|
85
|
+
|
|
86
|
+
const out = (s: string) => process.stderr.write(s);
|
|
87
|
+
|
|
88
|
+
// Persistent conversation store
|
|
89
|
+
const { ConversationStore } = await import('./conversation-store.js');
|
|
90
|
+
const store = new ConversationStore();
|
|
91
|
+
|
|
92
|
+
// Resolve conversation: resume, new, or auto
|
|
93
|
+
let conversation: { id: string; title: string; messageCount: number };
|
|
94
|
+
const history: AgentMessage[] = [];
|
|
95
|
+
|
|
96
|
+
if (opts.resumeId) {
|
|
97
|
+
const existing = store.get(opts.resumeId);
|
|
98
|
+
if (!existing) {
|
|
99
|
+
out(` ${c.red('Conversation not found:')} ${opts.resumeId}\n`);
|
|
100
|
+
return;
|
|
101
|
+
}
|
|
102
|
+
conversation = existing;
|
|
103
|
+
history.push(...store.loadMessages(existing.id));
|
|
104
|
+
compressHistory(history);
|
|
105
|
+
} else if (opts.newConversation) {
|
|
106
|
+
conversation = store.create(projectDir);
|
|
107
|
+
} else {
|
|
108
|
+
// Auto-resume most recent if within 1 hour, else create new
|
|
109
|
+
const recent = store.getMostRecent();
|
|
110
|
+
if (recent && Date.now() - recent.lastMessageAt < 3600_000) {
|
|
111
|
+
conversation = recent;
|
|
112
|
+
history.push(...store.loadMessages(recent.id));
|
|
113
|
+
compressHistory(history);
|
|
114
|
+
} else {
|
|
115
|
+
conversation = store.create(projectDir);
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
// Welcome
|
|
120
|
+
out(`\n ${c.bold('weaver assistant')}\n`);
|
|
121
|
+
out(` ${c.dim(`Project: ${projectDir}`)}\n`);
|
|
122
|
+
if (conversation.title) {
|
|
123
|
+
out(` ${c.dim(`Resuming: "${conversation.title}" (${conversation.messageCount} messages)`)}\n`);
|
|
124
|
+
} else {
|
|
125
|
+
out(` ${c.dim(`Conversation: ${conversation.id}`)}\n`);
|
|
126
|
+
}
|
|
127
|
+
out(` ${c.dim('Type your request. Ctrl+C to exit.')}\n\n`);
|
|
128
|
+
|
|
129
|
+
// Input source
|
|
130
|
+
const rl = opts.inputMessages
|
|
131
|
+
? null
|
|
132
|
+
: readline.createInterface({ input: process.stdin, output: process.stderr, prompt: `${c.cyan('❯')} ` });
|
|
133
|
+
|
|
134
|
+
const getNextInput = opts.inputMessages
|
|
135
|
+
? (() => {
|
|
136
|
+
let i = 0;
|
|
137
|
+
return (): Promise<string | null> => Promise.resolve(opts.inputMessages![i++] ?? null);
|
|
138
|
+
})()
|
|
139
|
+
: (): Promise<string | null> => new Promise<string | null>((resolve) => {
|
|
140
|
+
rl!.prompt();
|
|
141
|
+
rl!.once('line', (line) => resolve(line.trim() || null));
|
|
142
|
+
rl!.once('close', () => resolve(null));
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
const onStreamEvent = (event: StreamEvent) => {
|
|
146
|
+
if (event.type === 'text_delta') {
|
|
147
|
+
out(event.text);
|
|
148
|
+
} else if (event.type === 'thinking_delta') {
|
|
149
|
+
out(c.dim(event.text));
|
|
150
|
+
}
|
|
151
|
+
};
|
|
152
|
+
|
|
153
|
+
const onToolEvent = (event: { type: string; name: string; args?: Record<string, unknown>; result?: string; isError?: boolean }) => {
|
|
154
|
+
if (event.type === 'tool_call_start') {
|
|
155
|
+
const preview = toolPreview(event.name, event.args ?? {});
|
|
156
|
+
out(`\n ${c.cyan('◆')} ${event.name}${preview ? c.dim(`(${preview})`) : ''}\n`);
|
|
157
|
+
}
|
|
158
|
+
if (event.type === 'tool_call_result') {
|
|
159
|
+
const icon = event.isError ? c.red('✗') : c.dim('→');
|
|
160
|
+
const raw = event.result ?? '';
|
|
161
|
+
// Show full output for diagram/describe/docs tools; truncate others
|
|
162
|
+
const isVerboseTool = VERBOSE_TOOL_NAMES.has(event.name);
|
|
163
|
+
if (isVerboseTool && raw.length > 150) {
|
|
164
|
+
out(` ${icon}\n${raw}\n`);
|
|
165
|
+
} else {
|
|
166
|
+
const result = raw.replace(/\n/g, ' ').slice(0, 200);
|
|
167
|
+
out(` ${icon} ${result}\n`);
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
};
|
|
171
|
+
|
|
172
|
+
// Main conversation loop
|
|
173
|
+
while (true) {
|
|
174
|
+
const input = await getNextInput();
|
|
175
|
+
if (input === null) break;
|
|
176
|
+
if (!input.trim()) continue;
|
|
177
|
+
|
|
178
|
+
out('\n');
|
|
179
|
+
|
|
180
|
+
// Add user message to history
|
|
181
|
+
history.push({ role: 'user', content: input });
|
|
182
|
+
|
|
183
|
+
try {
|
|
184
|
+
const result = await runAgentLoop(
|
|
185
|
+
provider,
|
|
186
|
+
tools,
|
|
187
|
+
executor,
|
|
188
|
+
history, // full conversation history
|
|
189
|
+
{
|
|
190
|
+
systemPrompt,
|
|
191
|
+
maxIterations: 20,
|
|
192
|
+
onStreamEvent,
|
|
193
|
+
onToolEvent,
|
|
194
|
+
},
|
|
195
|
+
);
|
|
196
|
+
|
|
197
|
+
// Collect new messages from the agent loop
|
|
198
|
+
const newMessages: AgentMessage[] = [];
|
|
199
|
+
if (result.messages.length > history.length) {
|
|
200
|
+
for (let i = history.length; i < result.messages.length; i++) {
|
|
201
|
+
history.push(result.messages[i]);
|
|
202
|
+
newMessages.push(result.messages[i]);
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
// Persist to disk
|
|
207
|
+
const tokensUsed = result.usage.promptTokens + result.usage.completionTokens;
|
|
208
|
+
store.appendMessages(conversation.id, [{ role: 'user', content: input }, ...newMessages]);
|
|
209
|
+
store.updateAfterTurn(conversation.id, [{ role: 'user', content: input }, ...newMessages], tokensUsed);
|
|
210
|
+
|
|
211
|
+
// Auto-title from first assistant response
|
|
212
|
+
if (!conversation.title) {
|
|
213
|
+
const firstAssistant = newMessages.find(m => m.role === 'assistant');
|
|
214
|
+
if (firstAssistant && typeof firstAssistant.content === 'string') {
|
|
215
|
+
const title = firstAssistant.content.split('\n')[0].slice(0, 80).trim();
|
|
216
|
+
if (title) {
|
|
217
|
+
conversation.title = title;
|
|
218
|
+
store.setTitle(conversation.id, title);
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
// Token-aware compression
|
|
224
|
+
compressHistory(history);
|
|
225
|
+
|
|
226
|
+
if (!result.success && result.summary) {
|
|
227
|
+
out(`\n ${c.red(result.summary)}\n`);
|
|
228
|
+
}
|
|
229
|
+
} catch (err: unknown) {
|
|
230
|
+
const msg = err instanceof Error ? err.message : String(err);
|
|
231
|
+
out(`\n ${c.red('Error:')} ${msg}\n`);
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
out('\n');
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
rl?.close();
|
|
238
|
+
out(`\n ${c.dim('Goodbye.')}\n\n`);
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
function toolPreview(name: string, args: Record<string, unknown>): string {
|
|
242
|
+
if (args.name) return String(args.name);
|
|
243
|
+
if (args.bot) return String(args.bot);
|
|
244
|
+
if (args.file) return String(args.file).split('/').pop() ?? '';
|
|
245
|
+
if (args.path) return String(args.path).split('/').pop() ?? '';
|
|
246
|
+
if (args.instruction) return String(args.instruction).slice(0, 40);
|
|
247
|
+
if (args.command) return String(args.command).slice(0, 40);
|
|
248
|
+
return '';
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
// --- Token-aware history compression ---
|
|
252
|
+
|
|
253
|
+
// Budget: leave room for system prompt (~2k) + tools (~3k) + response (~4k)
|
|
254
|
+
const MAX_HISTORY_TOKENS = 80_000;
|
|
255
|
+
// When compressing, truncate tool results to this size
|
|
256
|
+
const COMPRESSED_TOOL_RESULT_SIZE = 200;
|
|
257
|
+
|
|
258
|
+
function estimateTokens(msg: AgentMessage): number {
|
|
259
|
+
const content = typeof msg.content === 'string' ? msg.content : JSON.stringify(msg.content);
|
|
260
|
+
return Math.ceil(content.length / CHARS_PER_TOKEN);
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
function totalTokens(history: AgentMessage[]): number {
|
|
264
|
+
return history.reduce((sum, m) => sum + estimateTokens(m), 0);
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
/**
|
|
268
|
+
* Compress conversation history to stay within token budget.
|
|
269
|
+
* Strategy (progressive, stops as soon as under budget):
|
|
270
|
+
* 1. Truncate long tool results to 200 chars (keep tool call structure)
|
|
271
|
+
* 2. Summarize old tool results to just "[tool_name: ok/error]"
|
|
272
|
+
* 3. Drop oldest turns (keep most recent 10 turns)
|
|
273
|
+
*/
|
|
274
|
+
function compressHistory(history: AgentMessage[]): void {
|
|
275
|
+
if (totalTokens(history) <= MAX_HISTORY_TOKENS) return;
|
|
276
|
+
|
|
277
|
+
// Phase 1: Truncate tool results older than last 6 messages
|
|
278
|
+
const cutoff = Math.max(0, history.length - 6);
|
|
279
|
+
for (let i = 0; i < cutoff; i++) {
|
|
280
|
+
const msg = history[i];
|
|
281
|
+
if (msg.role === 'tool' && typeof msg.content === 'string' && msg.content.length > COMPRESSED_TOOL_RESULT_SIZE) {
|
|
282
|
+
msg.content = msg.content.slice(0, COMPRESSED_TOOL_RESULT_SIZE) + '... (truncated)';
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
if (totalTokens(history) <= MAX_HISTORY_TOKENS) return;
|
|
286
|
+
|
|
287
|
+
// Phase 2: Summarize all tool results older than last 10 messages
|
|
288
|
+
const summaryCutoff = Math.max(0, history.length - 10);
|
|
289
|
+
for (let i = 0; i < summaryCutoff; i++) {
|
|
290
|
+
const msg = history[i];
|
|
291
|
+
if (msg.role === 'tool' && typeof msg.content === 'string') {
|
|
292
|
+
const isError = msg.content.toLowerCase().includes('error') || msg.content.toLowerCase().includes('not found');
|
|
293
|
+
msg.content = isError ? '(error — details truncated)' : '(ok — details truncated)';
|
|
294
|
+
}
|
|
295
|
+
// Also truncate long assistant messages
|
|
296
|
+
if (msg.role === 'assistant' && typeof msg.content === 'string' && msg.content.length > 500) {
|
|
297
|
+
msg.content = msg.content.slice(0, 500) + '... (truncated)';
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
if (totalTokens(history) <= MAX_HISTORY_TOKENS) return;
|
|
301
|
+
|
|
302
|
+
// Phase 3: Drop oldest turns, keep last 10 messages
|
|
303
|
+
const keep = 10;
|
|
304
|
+
if (history.length > keep) {
|
|
305
|
+
// Insert a summary of what was dropped
|
|
306
|
+
const dropped = history.length - keep;
|
|
307
|
+
history.splice(0, dropped, {
|
|
308
|
+
role: 'user',
|
|
309
|
+
content: `(${dropped} earlier messages compressed. Key context: this is an ongoing assistant session managing bots and workflows.)`,
|
|
310
|
+
});
|
|
311
|
+
}
|
|
312
|
+
}
|
|
@@ -0,0 +1,318 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Assistant tool definitions and executor.
|
|
3
|
+
* These are the tools the AI assistant uses to manage bots,
|
|
4
|
+
* queues, and the flow-weaver ecosystem.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import type { ToolDefinition, ToolExecutor } from '@synergenius/flow-weaver/agent';
|
|
8
|
+
import { BotManager } from './bot-manager.js';
|
|
9
|
+
import { execFileSync } from 'node:child_process';
|
|
10
|
+
import * as fs from 'node:fs';
|
|
11
|
+
import * as os from 'node:os';
|
|
12
|
+
import * as path from 'node:path';
|
|
13
|
+
import { ASSISTANT_TOOLS } from './tool-registry.js';
|
|
14
|
+
import { isBlockedCommand, isBlockedUrl } from './safety.js';
|
|
15
|
+
|
|
16
|
+
export { ASSISTANT_TOOLS };
|
|
17
|
+
|
|
18
|
+
// Shared bot manager instance
|
|
19
|
+
let manager: BotManager | null = null;
|
|
20
|
+
function getManager(): BotManager {
|
|
21
|
+
if (!manager) manager = new BotManager();
|
|
22
|
+
return manager;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export function createAssistantExecutor(projectDir: string): ToolExecutor {
|
|
26
|
+
const mgr = getManager();
|
|
27
|
+
|
|
28
|
+
return async (name: string, args: Record<string, unknown>) => {
|
|
29
|
+
try {
|
|
30
|
+
switch (name) {
|
|
31
|
+
// Bot management
|
|
32
|
+
case 'bot_spawn': {
|
|
33
|
+
const botName = String(args.name ?? `bot-${Date.now()}`);
|
|
34
|
+
const dir = String(args.project_dir ?? projectDir);
|
|
35
|
+
const bot = mgr.spawn(botName, {
|
|
36
|
+
projectDir: dir,
|
|
37
|
+
parallel: args.parallel as number | undefined,
|
|
38
|
+
deadline: args.deadline as string | undefined,
|
|
39
|
+
branch: args.branch as string | undefined,
|
|
40
|
+
});
|
|
41
|
+
return { result: JSON.stringify(bot), isError: false };
|
|
42
|
+
}
|
|
43
|
+
case 'bot_list': {
|
|
44
|
+
const bots = mgr.list();
|
|
45
|
+
if (bots.length === 0) return { result: 'No bots running.', isError: false };
|
|
46
|
+
const lines = bots.map(b => {
|
|
47
|
+
const uptime = Math.round((Date.now() - b.startedAt) / 1000);
|
|
48
|
+
return `${b.name}: ${b.status} (pid ${b.pid}, ${uptime}s uptime)`;
|
|
49
|
+
});
|
|
50
|
+
return { result: lines.join('\n'), isError: false };
|
|
51
|
+
}
|
|
52
|
+
case 'bot_status': {
|
|
53
|
+
const botName = String(args.name);
|
|
54
|
+
const bot = mgr.get(botName);
|
|
55
|
+
if (!bot) return { result: `Bot "${botName}" not found.`, isError: true };
|
|
56
|
+
const queue = mgr.getQueue(botName);
|
|
57
|
+
const tasks = await queue.list();
|
|
58
|
+
const pending = tasks.filter(t => t.status === 'pending').length;
|
|
59
|
+
const running = tasks.filter(t => t.status === 'running').length;
|
|
60
|
+
const completed = tasks.filter(t => t.status === 'completed').length;
|
|
61
|
+
const failed = tasks.filter(t => t.status === 'failed').length;
|
|
62
|
+
const failedTasks = tasks.filter(t => t.status === 'failed');
|
|
63
|
+
let result = `Bot "${botName}": ${bot.status}\n`;
|
|
64
|
+
result += `Tasks: ${completed} completed, ${failed} failed, ${running} running, ${pending} pending\n`;
|
|
65
|
+
if (failedTasks.length > 0) {
|
|
66
|
+
result += `\nFailed tasks:\n`;
|
|
67
|
+
for (const t of failedTasks) {
|
|
68
|
+
result += ` - ${t.instruction.slice(0, 80)}\n`;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
return { result, isError: false };
|
|
72
|
+
}
|
|
73
|
+
case 'bot_pause': {
|
|
74
|
+
await mgr.steer(String(args.name), 'pause');
|
|
75
|
+
return { result: `Paused bot "${args.name}".`, isError: false };
|
|
76
|
+
}
|
|
77
|
+
case 'bot_resume': {
|
|
78
|
+
await mgr.steer(String(args.name), 'resume');
|
|
79
|
+
return { result: `Resumed bot "${args.name}".`, isError: false };
|
|
80
|
+
}
|
|
81
|
+
case 'bot_stop': {
|
|
82
|
+
mgr.stop(String(args.name));
|
|
83
|
+
return { result: `Stopping bot "${args.name}" (will finish current task).`, isError: false };
|
|
84
|
+
}
|
|
85
|
+
case 'bot_logs': {
|
|
86
|
+
const logs = mgr.logs(String(args.name), (args.lines as number) ?? 30);
|
|
87
|
+
return { result: logs || '(no output yet)', isError: false };
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
// Queue management
|
|
91
|
+
case 'queue_add': {
|
|
92
|
+
const queue = mgr.getQueue(String(args.bot));
|
|
93
|
+
const { id, duplicate } = await queue.add({
|
|
94
|
+
instruction: String(args.instruction),
|
|
95
|
+
targets: args.targets as string[] | undefined,
|
|
96
|
+
priority: 0,
|
|
97
|
+
});
|
|
98
|
+
if (duplicate) return { result: `Task already queued (${id}).`, isError: false };
|
|
99
|
+
return { result: `Added task ${id} to "${args.bot}" queue.`, isError: false };
|
|
100
|
+
}
|
|
101
|
+
case 'queue_add_batch': {
|
|
102
|
+
const queue = mgr.getQueue(String(args.bot));
|
|
103
|
+
const tasks = args.tasks as Array<{ instruction: string; targets?: string[] }>;
|
|
104
|
+
let added = 0, skipped = 0;
|
|
105
|
+
for (const t of tasks) {
|
|
106
|
+
const { duplicate } = await queue.add({ instruction: t.instruction, targets: t.targets, priority: 0 });
|
|
107
|
+
if (duplicate) skipped++; else added++;
|
|
108
|
+
}
|
|
109
|
+
const msg = skipped > 0 ? `Added ${added} tasks, ${skipped} duplicates skipped.` : `Added ${added} tasks to "${args.bot}" queue.`;
|
|
110
|
+
return { result: msg, isError: false };
|
|
111
|
+
}
|
|
112
|
+
case 'queue_list': {
|
|
113
|
+
const queue = mgr.getQueue(String(args.bot));
|
|
114
|
+
const tasks = await queue.list();
|
|
115
|
+
if (tasks.length === 0) return { result: 'Queue is empty.', isError: false };
|
|
116
|
+
const lines = tasks.map(t => `[${t.status}] ${t.instruction.slice(0, 70)}`);
|
|
117
|
+
return { result: lines.join('\n'), isError: false };
|
|
118
|
+
}
|
|
119
|
+
case 'queue_retry': {
|
|
120
|
+
const queue = mgr.getQueue(String(args.bot));
|
|
121
|
+
const count = await queue.retryAll();
|
|
122
|
+
return { result: `Reset ${count} failed task(s) to pending.`, isError: false };
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
// Flow-weaver tools
|
|
126
|
+
case 'fw_validate': {
|
|
127
|
+
const output = execFileSync('npx', ['flow-weaver', 'validate', String(args.path)], {
|
|
128
|
+
encoding: 'utf-8', cwd: projectDir, timeout: 30_000, stdio: ['pipe', 'pipe', 'pipe'],
|
|
129
|
+
});
|
|
130
|
+
return { result: output.trim() || 'Validation complete.', isError: false };
|
|
131
|
+
}
|
|
132
|
+
case 'fw_diagram': {
|
|
133
|
+
const output = execFileSync('npx', ['flow-weaver', 'diagram', String(args.file), '--format', 'ascii-compact'], {
|
|
134
|
+
encoding: 'utf-8', cwd: projectDir, timeout: 15_000, stdio: ['pipe', 'pipe', 'pipe'],
|
|
135
|
+
});
|
|
136
|
+
return { result: output.trim(), isError: false };
|
|
137
|
+
}
|
|
138
|
+
case 'fw_describe': {
|
|
139
|
+
const output = execFileSync('npx', ['flow-weaver', 'describe', String(args.file)], {
|
|
140
|
+
encoding: 'utf-8', cwd: projectDir, timeout: 15_000, stdio: ['pipe', 'pipe', 'pipe'],
|
|
141
|
+
});
|
|
142
|
+
return { result: output.trim(), isError: false };
|
|
143
|
+
}
|
|
144
|
+
case 'fw_docs': {
|
|
145
|
+
const output = execFileSync('npx', ['flow-weaver', 'docs', String(args.topic), '--compact'], {
|
|
146
|
+
encoding: 'utf-8', cwd: projectDir, timeout: 15_000, stdio: ['pipe', 'pipe', 'pipe'],
|
|
147
|
+
});
|
|
148
|
+
return { result: output.trim().slice(0, 5000), isError: false };
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
// Project tools
|
|
152
|
+
case 'read_file': {
|
|
153
|
+
const filePath = path.isAbsolute(String(args.file)) ? String(args.file) : path.resolve(projectDir, String(args.file));
|
|
154
|
+
const stat = fs.statSync(filePath);
|
|
155
|
+
if (stat.isDirectory()) {
|
|
156
|
+
const entries = fs.readdirSync(filePath).slice(0, 100);
|
|
157
|
+
return { result: `Directory listing (${entries.length} entries):\n${entries.join('\n')}`, isError: false };
|
|
158
|
+
}
|
|
159
|
+
if (stat.size > 1_048_576) return { result: 'File too large (>1MB).', isError: true };
|
|
160
|
+
return { result: fs.readFileSync(filePath, 'utf-8'), isError: false };
|
|
161
|
+
}
|
|
162
|
+
case 'list_files': {
|
|
163
|
+
const dir = path.isAbsolute(String(args.directory)) ? String(args.directory) : path.resolve(projectDir, String(args.directory));
|
|
164
|
+
if (!fs.existsSync(dir)) return { result: `Directory not found: ${dir}`, isError: true };
|
|
165
|
+
let entries = fs.readdirSync(dir, { recursive: false }) as string[];
|
|
166
|
+
if (args.pattern) {
|
|
167
|
+
const re = new RegExp(String(args.pattern));
|
|
168
|
+
entries = entries.filter(e => re.test(e));
|
|
169
|
+
}
|
|
170
|
+
return { result: entries.slice(0, 200).join('\n') || '(empty)', isError: false };
|
|
171
|
+
}
|
|
172
|
+
case 'run_shell': {
|
|
173
|
+
const cmd = String(args.command);
|
|
174
|
+
// Safety: block destructive commands
|
|
175
|
+
if (isBlockedCommand(cmd)) {
|
|
176
|
+
return { result: `Blocked: "${cmd}" is not allowed.`, isError: true };
|
|
177
|
+
}
|
|
178
|
+
const output = execFileSync('sh', ['-c', cmd], {
|
|
179
|
+
encoding: 'utf-8', cwd: projectDir, timeout: 30_000, stdio: ['pipe', 'pipe', 'pipe'],
|
|
180
|
+
});
|
|
181
|
+
return { result: output.trim().slice(0, 5000) || '(no output)', isError: false };
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
// Conversation management
|
|
185
|
+
case 'conversation_list': {
|
|
186
|
+
const { ConversationStore } = await import('./conversation-store.js');
|
|
187
|
+
const cStore = new ConversationStore();
|
|
188
|
+
const convos = cStore.list();
|
|
189
|
+
if (convos.length === 0) return { result: 'No saved conversations.', isError: false };
|
|
190
|
+
const lines = convos.map(cv => {
|
|
191
|
+
const ago = Math.round((Date.now() - cv.lastMessageAt) / 60_000);
|
|
192
|
+
const agoStr = ago < 60 ? `${ago}m ago` : ago < 1440 ? `${Math.round(ago / 60)}h ago` : `${Math.round(ago / 1440)}d ago`;
|
|
193
|
+
const title = cv.title || '(untitled)';
|
|
194
|
+
return `${cv.id} "${title}" ${cv.messageCount} msgs ${agoStr}`;
|
|
195
|
+
});
|
|
196
|
+
return { result: `Conversations (${convos.length}):\n${lines.join('\n')}`, isError: false };
|
|
197
|
+
}
|
|
198
|
+
case 'conversation_delete': {
|
|
199
|
+
const { ConversationStore } = await import('./conversation-store.js');
|
|
200
|
+
const cStore = new ConversationStore();
|
|
201
|
+
const existing = cStore.get(String(args.id));
|
|
202
|
+
if (!existing) return { result: `Conversation "${args.id}" not found.`, isError: true };
|
|
203
|
+
cStore.delete(String(args.id));
|
|
204
|
+
return { result: `Deleted conversation "${args.id}" (${existing.title || 'untitled'}).`, isError: false };
|
|
205
|
+
}
|
|
206
|
+
case 'conversation_summary': {
|
|
207
|
+
const { ConversationStore } = await import('./conversation-store.js');
|
|
208
|
+
const cStore = new ConversationStore();
|
|
209
|
+
const recent = cStore.getMostRecent();
|
|
210
|
+
if (!recent) return { result: 'No active conversation.', isError: false };
|
|
211
|
+
const elapsed = Math.round((Date.now() - recent.createdAt) / 60_000);
|
|
212
|
+
return {
|
|
213
|
+
result: `Current conversation: ${recent.id}\n Title: ${recent.title || '(untitled)'}\n Messages: ${recent.messageCount}\n Tokens: ${recent.totalTokens}\n Bots: ${recent.botIds.length > 0 ? recent.botIds.join(', ') : 'none'}\n Duration: ${elapsed}m`,
|
|
214
|
+
isError: false,
|
|
215
|
+
};
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
case 'web_fetch': {
|
|
219
|
+
const url = String(args.url);
|
|
220
|
+
if (isBlockedUrl(url)) {
|
|
221
|
+
return { result: 'Blocked: cannot fetch internal/localhost URLs.', isError: true };
|
|
222
|
+
}
|
|
223
|
+
const resp = await fetch(url, { method: (args.method as string) ?? 'GET', signal: AbortSignal.timeout(15_000) });
|
|
224
|
+
const text = await resp.text();
|
|
225
|
+
return { result: text.slice(0, 10_000), isError: !resp.ok };
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
case 'github_status': {
|
|
229
|
+
const ghArgs = args.pr
|
|
230
|
+
? ['pr', 'checks', String(args.pr), '--json', 'name,state,conclusion']
|
|
231
|
+
: ['run', 'list', '--branch', String(args.branch ?? ''), '--json', 'status,conclusion,name,headBranch', '--limit', '5'];
|
|
232
|
+
try {
|
|
233
|
+
const output = execFileSync('gh', ghArgs, { encoding: 'utf-8', cwd: projectDir, timeout: 15_000, stdio: ['pipe', 'pipe', 'pipe'] });
|
|
234
|
+
return { result: output.trim(), isError: false };
|
|
235
|
+
} catch (err: any) {
|
|
236
|
+
return { result: `gh CLI error: ${(err.message ?? '').slice(0, 300)}`, isError: true };
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
case 'project_list': {
|
|
241
|
+
const projectsDir = path.join(os.homedir(), '.weaver', 'projects');
|
|
242
|
+
if (!fs.existsSync(projectsDir)) return { result: 'No projects found.', isError: false };
|
|
243
|
+
const dirs = fs.readdirSync(projectsDir);
|
|
244
|
+
// Each dir is a hash — try to find meta or queue files
|
|
245
|
+
const projects = dirs.map(d => {
|
|
246
|
+
const queuePath = path.join(projectsDir, d, 'task-queue.ndjson');
|
|
247
|
+
const exists = fs.existsSync(queuePath);
|
|
248
|
+
return `${d}: ${exists ? 'has queue' : 'empty'}`;
|
|
249
|
+
});
|
|
250
|
+
return { result: projects.join('\n') || 'No projects found.', isError: false };
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
case 'project_context': {
|
|
254
|
+
const dir = String(args.directory);
|
|
255
|
+
const parts: string[] = [];
|
|
256
|
+
const pkgPath = path.join(dir, 'package.json');
|
|
257
|
+
if (fs.existsSync(pkgPath)) {
|
|
258
|
+
try {
|
|
259
|
+
const pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf-8'));
|
|
260
|
+
parts.push(`Package: ${pkg.name}@${pkg.version}`);
|
|
261
|
+
parts.push(`Description: ${pkg.description ?? 'none'}`);
|
|
262
|
+
} catch { parts.push('package.json: parse error'); }
|
|
263
|
+
}
|
|
264
|
+
const planPath = path.join(dir, '.weaver-plan.md');
|
|
265
|
+
if (fs.existsSync(planPath)) {
|
|
266
|
+
parts.push(`Plan:\n${fs.readFileSync(planPath, 'utf-8').slice(0, 2000)}`);
|
|
267
|
+
}
|
|
268
|
+
const configPath = path.join(dir, '.weaver.json');
|
|
269
|
+
if (fs.existsSync(configPath)) {
|
|
270
|
+
parts.push(`Weaver config: ${fs.readFileSync(configPath, 'utf-8').trim()}`);
|
|
271
|
+
}
|
|
272
|
+
return { result: parts.join('\n') || `No context found in ${dir}`, isError: false };
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
case 'fw_diagram_mermaid': {
|
|
276
|
+
try {
|
|
277
|
+
// Try mermaid format first, fall back to text
|
|
278
|
+
let output: string;
|
|
279
|
+
try {
|
|
280
|
+
output = execFileSync('npx', ['flow-weaver', 'diagram', String(args.file), '--format', 'mermaid'], {
|
|
281
|
+
encoding: 'utf-8', cwd: projectDir, timeout: 15_000, stdio: ['pipe', 'pipe', 'pipe'],
|
|
282
|
+
}).trim();
|
|
283
|
+
} catch {
|
|
284
|
+
output = execFileSync('npx', ['flow-weaver', 'diagram', String(args.file)], {
|
|
285
|
+
encoding: 'utf-8', cwd: projectDir, timeout: 15_000, stdio: ['pipe', 'pipe', 'pipe'],
|
|
286
|
+
}).trim();
|
|
287
|
+
}
|
|
288
|
+
return { result: output, isError: false };
|
|
289
|
+
} catch (err: any) {
|
|
290
|
+
return { result: (err.message ?? '').slice(0, 500), isError: true };
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
case 'knowledge_list': {
|
|
295
|
+
const { KnowledgeStore } = await import('./knowledge-store.js');
|
|
296
|
+
const kStore = new KnowledgeStore(projectDir);
|
|
297
|
+
const entries = kStore.list();
|
|
298
|
+
if (entries.length === 0) return { result: 'No stored knowledge.', isError: false };
|
|
299
|
+
return { result: entries.map(e => `${e.key}: ${e.value}`).join('\n'), isError: false };
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
case 'knowledge_search': {
|
|
303
|
+
const { KnowledgeStore } = await import('./knowledge-store.js');
|
|
304
|
+
const kStore = new KnowledgeStore(projectDir);
|
|
305
|
+
const entries = kStore.recall(String(args.query));
|
|
306
|
+
if (entries.length === 0) return { result: 'No matching knowledge found.', isError: false };
|
|
307
|
+
return { result: entries.map(e => `${e.key}: ${e.value}`).join('\n'), isError: false };
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
default:
|
|
311
|
+
return { result: `Unknown tool: ${name}`, isError: true };
|
|
312
|
+
}
|
|
313
|
+
} catch (err: unknown) {
|
|
314
|
+
const msg = err instanceof Error ? err.message : String(err);
|
|
315
|
+
return { result: msg.slice(0, 500), isError: true };
|
|
316
|
+
}
|
|
317
|
+
};
|
|
318
|
+
}
|
package/src/bot/audit-logger.ts
CHANGED
|
@@ -8,7 +8,8 @@ let onEvent: AuditEventCallback | undefined;
|
|
|
8
8
|
export function initAuditLogger(runId: string, callback?: AuditEventCallback): void {
|
|
9
9
|
try {
|
|
10
10
|
store = new AuditStore();
|
|
11
|
-
} catch {
|
|
11
|
+
} catch (err) {
|
|
12
|
+
if (process.env.WEAVER_VERBOSE) process.stderr.write(`[weaver] audit store init failed: ${err}\n`);
|
|
12
13
|
store = null;
|
|
13
14
|
}
|
|
14
15
|
currentRunId = runId;
|
|
@@ -27,14 +28,14 @@ export function auditEmit(type: AuditEventType, data?: Record<string, unknown>):
|
|
|
27
28
|
|
|
28
29
|
try {
|
|
29
30
|
store?.emit(event);
|
|
30
|
-
} catch {
|
|
31
|
-
|
|
31
|
+
} catch (err) {
|
|
32
|
+
if (process.env.WEAVER_VERBOSE) process.stderr.write(`[weaver] audit emit failed: ${err}\n`);
|
|
32
33
|
}
|
|
33
34
|
|
|
34
35
|
try {
|
|
35
36
|
onEvent?.(event);
|
|
36
|
-
} catch {
|
|
37
|
-
|
|
37
|
+
} catch (err) {
|
|
38
|
+
if (process.env.WEAVER_VERBOSE) process.stderr.write(`[weaver] audit callback failed: ${err}\n`);
|
|
38
39
|
}
|
|
39
40
|
}
|
|
40
41
|
|