@nia-teams/four-tools 0.0.0-stage → 0.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/LICENSE +21 -0
- package/README.md +143 -2
- package/README_CN.md +144 -0
- package/dist/core/contracts.d.ts +118 -0
- package/dist/core/contracts.js +125 -0
- package/dist/core/edit-engine.d.ts +11 -0
- package/dist/core/edit-engine.js +93 -0
- package/dist/core/env.d.ts +149 -0
- package/dist/core/env.js +1 -0
- package/dist/core/index.d.ts +23 -0
- package/dist/core/index.js +33 -0
- package/dist/core/mutation-queue.d.ts +6 -0
- package/dist/core/mutation-queue.js +24 -0
- package/dist/core/tools/edit.d.ts +4 -0
- package/dist/core/tools/edit.js +43 -0
- package/dist/core/tools/read.d.ts +10 -0
- package/dist/core/tools/read.js +45 -0
- package/dist/core/tools/shared.d.ts +20 -0
- package/dist/core/tools/shared.js +51 -0
- package/dist/core/tools/shell.d.ts +10 -0
- package/dist/core/tools/shell.js +166 -0
- package/dist/core/tools/write.d.ts +4 -0
- package/dist/core/tools/write.js +33 -0
- package/dist/core/types.d.ts +65 -0
- package/dist/core/types.js +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/node/index.d.ts +16 -0
- package/dist/node/index.js +16 -0
- package/dist/node/posix-env.d.ts +46 -0
- package/dist/node/posix-env.js +239 -0
- package/dist/node/process-group.d.ts +2 -0
- package/dist/node/process-group.js +30 -0
- package/dist/node/process-runner.d.ts +43 -0
- package/dist/node/process-runner.js +221 -0
- package/dist/node/read-range.d.ts +7 -0
- package/dist/node/read-range.js +69 -0
- package/dist/node/terminal-manager.d.ts +36 -0
- package/dist/node/terminal-manager.js +228 -0
- package/examples/local-agent.ts +164 -0
- package/examples/quickstart.ts +30 -0
- package/examples/web-agent.ts +479 -0
- package/package.json +44 -4
- package/skills/four-tools/SKILL.md +21 -0
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
import { createInterface } from 'node:readline';
|
|
2
|
+
import {
|
|
3
|
+
createAgentHost,
|
|
4
|
+
createMemoryDocStore,
|
|
5
|
+
createMemoryPubSub,
|
|
6
|
+
} from '@nia-teams/agent-core/host';
|
|
7
|
+
import {
|
|
8
|
+
AnthropicClient,
|
|
9
|
+
OpenAIClient,
|
|
10
|
+
GeminiClient,
|
|
11
|
+
type ILLMClient,
|
|
12
|
+
} from '@nia-teams/ai';
|
|
13
|
+
import { createFourTools } from '@nia-teams/four-tools';
|
|
14
|
+
import { createNodeEnv } from '@nia-teams/four-tools/node';
|
|
15
|
+
|
|
16
|
+
const vars = process.env;
|
|
17
|
+
function selectClient(): ILLMClient {
|
|
18
|
+
if (vars.ANTHROPIC_API_KEY)
|
|
19
|
+
return new AnthropicClient({
|
|
20
|
+
apiKey: vars.ANTHROPIC_API_KEY,
|
|
21
|
+
model: vars.MODEL ?? 'claude-sonnet-4-5',
|
|
22
|
+
});
|
|
23
|
+
if (vars.OPENAI_API_KEY)
|
|
24
|
+
return new OpenAIClient({
|
|
25
|
+
apiKey: vars.OPENAI_API_KEY,
|
|
26
|
+
baseURL: vars.OPENAI_BASE_URL,
|
|
27
|
+
model: vars.MODEL ?? 'gpt-4.1',
|
|
28
|
+
});
|
|
29
|
+
if (vars.GEMINI_API_KEY)
|
|
30
|
+
return new GeminiClient({
|
|
31
|
+
apiKey: vars.GEMINI_API_KEY,
|
|
32
|
+
model: vars.MODEL ?? 'gemini-2.5-flash',
|
|
33
|
+
});
|
|
34
|
+
throw new Error(
|
|
35
|
+
'Set ANTHROPIC_API_KEY, OPENAI_API_KEY, or GEMINI_API_KEY; MODEL overrides the default.',
|
|
36
|
+
);
|
|
37
|
+
}
|
|
38
|
+
const upstream = selectClient();
|
|
39
|
+
const sessionId = 'local';
|
|
40
|
+
let active: AbortController | undefined;
|
|
41
|
+
let closing = false;
|
|
42
|
+
let lastInterrupt = 0;
|
|
43
|
+
// agent-core 0.1.x exposes injectMessage, not submit, and has no turn-cancel API.
|
|
44
|
+
// A shared controller cancels both model requests and tool operations for this local session.
|
|
45
|
+
const client: ILLMClient = {
|
|
46
|
+
getProviderName: () => upstream.getProviderName(),
|
|
47
|
+
getModelName: () => upstream.getModelName(),
|
|
48
|
+
chat: (request) =>
|
|
49
|
+
upstream.chat({
|
|
50
|
+
...request,
|
|
51
|
+
signal: combine(request.signal, active?.signal),
|
|
52
|
+
}),
|
|
53
|
+
};
|
|
54
|
+
function combine(a?: AbortSignal, b?: AbortSignal): AbortSignal | undefined {
|
|
55
|
+
const signals = [a, b].filter((s): s is AbortSignal => Boolean(s));
|
|
56
|
+
return signals.length ? AbortSignal.any(signals) : undefined;
|
|
57
|
+
}
|
|
58
|
+
const env = createNodeEnv({
|
|
59
|
+
cwd: process.cwd(),
|
|
60
|
+
onTerminalExit: async (event) => {
|
|
61
|
+
if (closing) return;
|
|
62
|
+
console.log(
|
|
63
|
+
`\n[terminal ${event.name || '(unnamed)'}] ${event.status} (${event.exitCode ?? event.signal})`,
|
|
64
|
+
);
|
|
65
|
+
await host.injectMessage({
|
|
66
|
+
sessionId,
|
|
67
|
+
userId: 'terminal',
|
|
68
|
+
userName: 'Terminal',
|
|
69
|
+
eventType: 'terminal_exit',
|
|
70
|
+
content: JSON.stringify(event),
|
|
71
|
+
});
|
|
72
|
+
},
|
|
73
|
+
onTerminalError: (error) => console.error('\n[terminal error]', error),
|
|
74
|
+
});
|
|
75
|
+
const tools = createFourTools(env).map((tool) => ({
|
|
76
|
+
...tool,
|
|
77
|
+
execute: (
|
|
78
|
+
input: Record<string, unknown>,
|
|
79
|
+
ctx: Parameters<typeof tool.execute>[1] = {},
|
|
80
|
+
) =>
|
|
81
|
+
tool.execute(input, {
|
|
82
|
+
...ctx,
|
|
83
|
+
signal: combine(ctx.signal, active?.signal),
|
|
84
|
+
}),
|
|
85
|
+
}));
|
|
86
|
+
const host = createAgentHost({
|
|
87
|
+
docStore: createMemoryDocStore(),
|
|
88
|
+
pubSub: createMemoryPubSub(),
|
|
89
|
+
resolveAgentConfig: () => ({
|
|
90
|
+
client,
|
|
91
|
+
tools,
|
|
92
|
+
systemPrompt:
|
|
93
|
+
'You are a local coding assistant. Read before editing. Ask before destructive or external actions. Report terminal_exit events to the user. Commands that exceed the shell timeout continue in managed terminals.',
|
|
94
|
+
}),
|
|
95
|
+
onEvent({ event }) {
|
|
96
|
+
if (event.type === 'turn_start') {
|
|
97
|
+
active = new AbortController();
|
|
98
|
+
process.stdout.write('\nAgent> ');
|
|
99
|
+
}
|
|
100
|
+
if (event.type === 'text_delta') process.stdout.write(event.delta);
|
|
101
|
+
if (event.type === 'thinking_delta')
|
|
102
|
+
process.stdout.write(`\x1b[2m${event.delta}\x1b[0m`);
|
|
103
|
+
if (event.type === 'tool_call_start')
|
|
104
|
+
console.log(`\n[${event.name}]`, event.input);
|
|
105
|
+
if (event.type === 'tool_call_end' && event.error)
|
|
106
|
+
console.error(`\n[tool error] ${event.error}`);
|
|
107
|
+
if (event.type === 'error') console.error(`\n[error] ${event.message}`);
|
|
108
|
+
if (event.type === 'turn_end') {
|
|
109
|
+
active = undefined;
|
|
110
|
+
rl.prompt();
|
|
111
|
+
}
|
|
112
|
+
},
|
|
113
|
+
});
|
|
114
|
+
async function shutdown() {
|
|
115
|
+
if (closing) return;
|
|
116
|
+
closing = true;
|
|
117
|
+
active?.abort();
|
|
118
|
+
rl.close();
|
|
119
|
+
process.stdin.destroy();
|
|
120
|
+
await env.dispose();
|
|
121
|
+
await host.stop();
|
|
122
|
+
if ('close' in upstream && typeof upstream.close === 'function')
|
|
123
|
+
await upstream.close();
|
|
124
|
+
}
|
|
125
|
+
function interrupt() {
|
|
126
|
+
const now = Date.now();
|
|
127
|
+
if (now - lastInterrupt < 1200 || !active) {
|
|
128
|
+
void shutdown().catch(console.error);
|
|
129
|
+
return;
|
|
130
|
+
}
|
|
131
|
+
lastInterrupt = now;
|
|
132
|
+
active.abort();
|
|
133
|
+
console.log('\n[interrupted; Ctrl+C again to exit]');
|
|
134
|
+
}
|
|
135
|
+
await host.start();
|
|
136
|
+
const rl = createInterface({
|
|
137
|
+
input: process.stdin,
|
|
138
|
+
output: process.stdout,
|
|
139
|
+
prompt: '\nYou> ',
|
|
140
|
+
});
|
|
141
|
+
rl.on('SIGINT', interrupt);
|
|
142
|
+
process.on('SIGINT', interrupt);
|
|
143
|
+
rl.on('line', (line) => {
|
|
144
|
+
if (line.trim() === '/exit') {
|
|
145
|
+
void shutdown().catch(console.error);
|
|
146
|
+
return;
|
|
147
|
+
}
|
|
148
|
+
if (!line.trim() || closing) return;
|
|
149
|
+
void host
|
|
150
|
+
.injectMessage({
|
|
151
|
+
sessionId,
|
|
152
|
+
userId: 'local-user',
|
|
153
|
+
userName: 'You',
|
|
154
|
+
content: line,
|
|
155
|
+
})
|
|
156
|
+
.catch(console.error);
|
|
157
|
+
});
|
|
158
|
+
rl.on('close', () => {
|
|
159
|
+
void shutdown().catch(console.error);
|
|
160
|
+
});
|
|
161
|
+
console.log(
|
|
162
|
+
`Local agent · ${upstream.getModelName()} · ${env.cwd}\nTools run with your local account permissions. /exit quits.`,
|
|
163
|
+
);
|
|
164
|
+
rl.prompt();
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { AgentRuntime, InMemoryConversationStore } from '@nia-teams/agent-core';
|
|
2
|
+
import { OpenAIClient } from '@nia-teams/ai';
|
|
3
|
+
import { createNodeTools } from '@nia-teams/four-tools/node';
|
|
4
|
+
|
|
5
|
+
if (!process.env.DEEPSEEK_API_KEY) throw new Error('Set DEEPSEEK_API_KEY');
|
|
6
|
+
const client = new OpenAIClient({
|
|
7
|
+
apiKey: process.env.DEEPSEEK_API_KEY,
|
|
8
|
+
baseURL: 'https://api.deepseek.com/v1',
|
|
9
|
+
model: 'deepseek-chat',
|
|
10
|
+
});
|
|
11
|
+
const tools = createNodeTools({ cwd: process.cwd(), terminals: false });
|
|
12
|
+
const store = InMemoryConversationStore.fromUserMessage(
|
|
13
|
+
'Read package.json and summarize this project. Do not modify files.',
|
|
14
|
+
);
|
|
15
|
+
const runtime = new AgentRuntime({ store, sessionId: 'quickstart' });
|
|
16
|
+
try {
|
|
17
|
+
await runtime.run({
|
|
18
|
+
client,
|
|
19
|
+
systemPrompt: 'You are a coding assistant. Inspect files before answering.',
|
|
20
|
+
tools,
|
|
21
|
+
onEvent(event) {
|
|
22
|
+
if (event.type === 'text_delta') process.stdout.write(event.delta);
|
|
23
|
+
if (event.type === 'tool_call_start') console.log(`\n[${event.name}]`, event.input);
|
|
24
|
+
},
|
|
25
|
+
});
|
|
26
|
+
console.log();
|
|
27
|
+
} finally {
|
|
28
|
+
await tools.dispose();
|
|
29
|
+
await client.close();
|
|
30
|
+
}
|