@zenera/meta 1.3.1 → 1.3.3
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/README.md +24 -6
- package/dist/chat/index.d.ts +5 -0
- package/dist/chat/index.js +8 -0
- package/dist/chat/open.d.ts +46 -0
- package/dist/chat/open.js +184 -0
- package/dist/chat/permissions.d.ts +37 -0
- package/dist/chat/permissions.js +144 -0
- package/dist/chat/run.d.ts +28 -0
- package/dist/chat/run.js +78 -0
- package/dist/chat/session.d.ts +117 -0
- package/dist/chat/session.js +303 -0
- package/dist/chat/transcript.d.ts +18 -0
- package/dist/chat/transcript.js +39 -0
- package/dist/chat/tui.d.ts +37 -0
- package/dist/chat/tui.js +237 -0
- package/dist/command.js +112 -173
- package/dist/host.d.ts +1 -1
- package/dist/host.js +1 -1
- package/dist/launch.d.ts +11 -1
- package/dist/launch.js +40 -6
- package/dist/prepare.d.ts +45 -0
- package/dist/prepare.js +150 -0
- package/dist/reporter.d.ts +9 -0
- package/dist/reporter.js +46 -0
- package/dist/wire.d.ts +2 -0
- package/dist/wire.js +8 -1
- package/package.json +11 -5
package/README.md
CHANGED
|
@@ -27,8 +27,9 @@
|
|
|
27
27
|
the project to change it - generate it from its specification, review it,
|
|
28
28
|
build its evaluation dataset and fine-tune its prompts against that dataset.**
|
|
29
29
|
|
|
30
|
-
It drives [GitHub Copilot CLI](https://github.com/github/copilot-cli)
|
|
31
|
-
|
|
30
|
+
It drives [GitHub Copilot CLI](https://github.com/github/copilot-cli) - and, for
|
|
31
|
+
a conversation, the [Copilot SDK](https://github.com/github/copilot-sdk) - with
|
|
32
|
+
your provider keys, never a Copilot subscription.
|
|
32
33
|
|
|
33
34
|
> Part of [ZeneraNeo](https://github.com/zenera-ai/ZeneraNeo). It ships no
|
|
34
35
|
> binary of its own: installing it adds a `meta` **subcommand** to
|
|
@@ -40,6 +41,9 @@ provider keys, never a Copilot subscription.
|
|
|
40
41
|
- **Specification-driven builds** - `/spec-sync-project` reads
|
|
41
42
|
`SPECIFICATION.md` and writes the agents, prompts, tools and skills, then asks
|
|
42
43
|
what it could not decide in `SPECIFICATION-FEEDBACK.md`.
|
|
44
|
+
- **A chat in the terminal** - `zen meta chat` is the same agent turn by turn:
|
|
45
|
+
the answer streams in, each tool call is a line, and `--ask` has it stop for a
|
|
46
|
+
yes or no before it touches anything.
|
|
43
47
|
- **Commands from the project** - every `.github/skills/<name>/SKILL.md` the
|
|
44
48
|
project carries is a `/<name>` you can run from the terminal, the same ones
|
|
45
49
|
the editor offers.
|
|
@@ -62,8 +66,9 @@ zen key add openai # the keyring `zen` already uses
|
|
|
62
66
|
```
|
|
63
67
|
|
|
64
68
|
Copilot CLI is used from `PATH` when it is there (`npm i -g @github/copilot`)
|
|
65
|
-
and fetched with `npx` otherwise. `zen
|
|
66
|
-
|
|
69
|
+
and fetched with `npx` otherwise. `zen meta chat` needs neither: the Copilot SDK
|
|
70
|
+
it runs on is a dependency and brings its own runtime. `zen version` shows
|
|
71
|
+
both, and every `@zenera/*` package version beside them.
|
|
67
72
|
|
|
68
73
|
## Use
|
|
69
74
|
|
|
@@ -73,6 +78,7 @@ zen meta run "what does this project do?" # ask it something
|
|
|
73
78
|
zen meta run acme /spec-sync-project # run one of the project's commands
|
|
74
79
|
git diff | zen meta run "what broke?" --allow-tool read
|
|
75
80
|
zen meta resume acme # carry on the last session
|
|
81
|
+
zen meta chat acme # talk it through, turn by turn
|
|
76
82
|
```
|
|
77
83
|
|
|
78
84
|
The answer goes to stdout and the progress to stderr, so `> out.md` keeps the
|
|
@@ -83,6 +89,12 @@ It may use every tool without asking: a prompt written for an editor expects
|
|
|
83
89
|
to read, write and run things, and a terminal has nobody watching to answer.
|
|
84
90
|
Narrow it with `--allow-tool`, or restore the asking with `--ask`.
|
|
85
91
|
|
|
92
|
+
`zen meta chat` takes the same model, key, tools and log as `run`, in a
|
|
93
|
+
conversation: the answer streams in, Esc stops a turn, `/help` lists the
|
|
94
|
+
chat's commands and `/<name>` runs a project command inside it. It ends by
|
|
95
|
+
printing the session, which `zen meta chat --resume` - or `--continue`, for
|
|
96
|
+
the last one - and `zen meta resume` both take.
|
|
97
|
+
|
|
86
98
|
The editor files `zen init` writes (`.vscode/` and the `.github/` tree) are
|
|
87
99
|
refreshed in the project before each run, because they are the brief the agent
|
|
88
100
|
works from and they describe this version of `zen`.
|
|
@@ -93,6 +105,7 @@ works from and they describe this version of `zen`.
|
|
|
93
105
|
zen meta run [project] "<question>" Ask it something.
|
|
94
106
|
zen meta run [project] /<name> [words] Run .github/skills/<name>/SKILL.md.
|
|
95
107
|
zen meta run [project] Pick one of those commands.
|
|
108
|
+
zen meta chat [project] [question] Talk it through, turn by turn.
|
|
96
109
|
zen meta prompts [project] List those commands.
|
|
97
110
|
zen meta resume [project] [session] Carry on a session that stopped.
|
|
98
111
|
zen meta model [ref] Show or set the model it uses.
|
|
@@ -103,8 +116,8 @@ zen meta finetune <verb> Tune the project on those cases.
|
|
|
103
116
|
Useful options: `--project`, `--model`, `--provider`, `--prompt-file`,
|
|
104
117
|
`--agent`, `--effort`, `--allow-tool`, `--ask`, `--add-dir`, `--retries`,
|
|
105
118
|
`--share`, `--events`, `--dry-run` (print the command, secrets masked),
|
|
106
|
-
`--json`. `zen meta --help`, `zen meta run --help`, `zen meta
|
|
107
|
-
and `zen meta finetune --help` print the full tables. The tuning loop is
|
|
119
|
+
`--json`. `zen meta --help`, `zen meta run --help`, `zen meta chat --help`,
|
|
120
|
+
`zen meta dataset --help` and `zen meta finetune --help` print the full tables. The tuning loop is
|
|
108
121
|
described in [docs/finetuning.md](https://github.com/zenera-ai/ZeneraNeo/blob/main/docs/finetuning.md).
|
|
109
122
|
|
|
110
123
|
## Models
|
|
@@ -140,6 +153,11 @@ holds that line. The CLI keeps what `zen init` and `zen open` also need - the
|
|
|
140
153
|
`.github/` editor tree and its templates, and the usage ledger. See §7.6 of the
|
|
141
154
|
[CLI design notes](https://github.com/zenera-ai/ZeneraNeo/blob/main/packages/cli/DESIGN.md).
|
|
142
155
|
|
|
156
|
+
`@zenera/meta/chat` exports `openMetaChat`, the session behind `zen meta chat`
|
|
157
|
+
without its screen, for a host that draws its own chat. It refreshes the editor
|
|
158
|
+
files, picks the model and key, logs and records usage as the terminal chat
|
|
159
|
+
does, and reports each turn as `ChatEvent`s.
|
|
160
|
+
|
|
143
161
|
## The rest of the family
|
|
144
162
|
|
|
145
163
|
| Package | What it is |
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export { openMetaChat, type MetaChat, type MetaChatOptions } from './open.ts';
|
|
2
|
+
export { EFFORTS, type ChatEvent, type Connect, type TurnResult } from './session.ts';
|
|
3
|
+
export type { Answer, Described } from './permissions.ts';
|
|
4
|
+
export type { MetaCall } from '../tokens.ts';
|
|
5
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
// packages/meta/src/chat/index.ts
|
|
2
|
+
//
|
|
3
|
+
// `@zenera/meta/chat`: the meta agent as a conversation, for a host that draws its own chat (no terminal, no Ink).
|
|
4
|
+
// Main: `openMetaChat`, and the event and permission types it speaks in.
|
|
5
|
+
// Used by: hosts such as the ZeneraLite project manager; `zen meta chat` itself goes through `chat/run.ts`.
|
|
6
|
+
export { openMetaChat } from "./open.js";
|
|
7
|
+
export { EFFORTS } from "./session.js";
|
|
8
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import type { SessionEvent } from '@github/copilot-sdk';
|
|
2
|
+
import type * as Projects from '@zenera/engine';
|
|
3
|
+
import { type Selection } from '../prepare.ts';
|
|
4
|
+
import { type Answer, type Described } from './permissions.ts';
|
|
5
|
+
import { type Chat, type ChatEvent, type Connect, type TurnResult } from './session.ts';
|
|
6
|
+
export interface MetaChatOptions extends Selection {
|
|
7
|
+
/** the project, or a directory inside one */
|
|
8
|
+
project: Projects.Project | string;
|
|
9
|
+
/** a `.github/agents/<name>.agent.md` */
|
|
10
|
+
agent?: string;
|
|
11
|
+
effort?: string;
|
|
12
|
+
/** every tool, whatever `allowTool` and `ask` say */
|
|
13
|
+
allowAll?: boolean;
|
|
14
|
+
/** `--allow-tool`'s list: read, shell(git:*), write(README.md) */
|
|
15
|
+
allowTool?: string;
|
|
16
|
+
/** ask `prompt` about any tool `allowTool` leaves out */
|
|
17
|
+
ask?: boolean;
|
|
18
|
+
prompt?: (request: Described) => Promise<Answer>;
|
|
19
|
+
addDirs?: readonly string[];
|
|
20
|
+
/** an absolute path for `--events`' JSONL */
|
|
21
|
+
events?: string;
|
|
22
|
+
/** resumes after a failure time can cure, per turn; default `DEFAULT_RESUMES` */
|
|
23
|
+
retries?: number;
|
|
24
|
+
/** rewrite the editor files first, as `zen open` does; default true */
|
|
25
|
+
refresh?: boolean;
|
|
26
|
+
onEvent: (event: ChatEvent) => void;
|
|
27
|
+
/** every SDK event, before it is narrated */
|
|
28
|
+
onRaw?: (event: SessionEvent) => void;
|
|
29
|
+
/** stands in for the SDK in tests */
|
|
30
|
+
connect?: Connect;
|
|
31
|
+
}
|
|
32
|
+
export interface MetaChat extends Chat {
|
|
33
|
+
readonly project: Projects.Project;
|
|
34
|
+
/** the model as wired */
|
|
35
|
+
readonly model: string;
|
|
36
|
+
/** the provider whose key it runs on */
|
|
37
|
+
readonly owner: string;
|
|
38
|
+
readonly resumed: boolean;
|
|
39
|
+
readonly logPath: string;
|
|
40
|
+
/** `shown` is what the person typed, when `prompt` is the command it expanded to */
|
|
41
|
+
send(prompt: string, shown?: string): Promise<TurnResult>;
|
|
42
|
+
/** the token line so far; empty before the first model call */
|
|
43
|
+
tokens(): string;
|
|
44
|
+
}
|
|
45
|
+
export declare function openMetaChat(opts: MetaChatOptions): Promise<MetaChat>;
|
|
46
|
+
//# sourceMappingURL=open.d.ts.map
|
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
// packages/meta/src/chat/open.ts
|
|
2
|
+
//
|
|
3
|
+
// A `zen meta chat` session with no screen: what the chat sets up before it draws (editor refresh, model and key,
|
|
4
|
+
// log, Vertex relay, ledger) and, on `close`, what it records after (the session's usage, the token line).
|
|
5
|
+
// Main: `openMetaChat(opts)` -> `MetaChat` (`send`, `abort`, `close`, `tokens`).
|
|
6
|
+
// Used by: `chat/run.ts` (the terminal chat) and, through the `@zenera/meta/chat` export, hosts that draw their
|
|
7
|
+
// own chat - the ZeneraLite web editor.
|
|
8
|
+
import { randomUUID } from 'node:crypto';
|
|
9
|
+
import { appendUsage, copilotEventLog, dim, LEDGER_ENV, ledgerPath, META_SESSION_ENV, note, resolveProject, } from "../host.js";
|
|
10
|
+
import { absorb, DEFAULT_RESUMES, lastSession, openLog, recordSession, } from "../launch.js";
|
|
11
|
+
import { SOURCE_LABELS } from "../model.js";
|
|
12
|
+
import { announce, prepare, refreshEditor } from "../prepare.js";
|
|
13
|
+
import { startVertexRelay } from "../relay.js";
|
|
14
|
+
import { usageReporter } from "../reporter.js";
|
|
15
|
+
import { sessionTotals, Tally } from "../tokens.js";
|
|
16
|
+
import { permissionPolicy } from "./permissions.js";
|
|
17
|
+
import { googleToken, openChat, providerConfig, runtimeEnv, } from "./session.js";
|
|
18
|
+
export async function openMetaChat(opts) {
|
|
19
|
+
const project = typeof opts.project === 'string'
|
|
20
|
+
? await resolveProject({ cwd: opts.project })
|
|
21
|
+
: opts.project;
|
|
22
|
+
if (opts.refresh !== false) {
|
|
23
|
+
refreshEditor(project);
|
|
24
|
+
}
|
|
25
|
+
let resume = opts.resume;
|
|
26
|
+
if (opts.continue && !resume) {
|
|
27
|
+
resume = lastSession(project.dir);
|
|
28
|
+
if (!resume) {
|
|
29
|
+
note(dim(`no meta session recorded in ${project.name} - starting a new one`));
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
if (resume) {
|
|
33
|
+
note(dim(`resuming session ${resume}`));
|
|
34
|
+
}
|
|
35
|
+
const prepared = await prepare({
|
|
36
|
+
provider: opts.provider,
|
|
37
|
+
model: opts.model,
|
|
38
|
+
resume,
|
|
39
|
+
'session-id': opts['session-id'],
|
|
40
|
+
}, project);
|
|
41
|
+
const { values, wiring, chosen } = prepared;
|
|
42
|
+
const resumed = Boolean(values.resume);
|
|
43
|
+
const session = prepared.session ?? randomUUID();
|
|
44
|
+
announce(prepared, project);
|
|
45
|
+
recordSession(project.dir, session);
|
|
46
|
+
const log = openLog(project.dir, new Date(), session);
|
|
47
|
+
note(`log ${log.path}`);
|
|
48
|
+
log.line(`zen meta chat ${new Date().toISOString()}`);
|
|
49
|
+
log.line(`project ${project.name} ${project.dir}`);
|
|
50
|
+
log.line(`model ${chosen.ref} from ${SOURCE_LABELS[chosen.from]}`);
|
|
51
|
+
log.line(`session ${session}${resumed ? ' (resumed)' : ''}`);
|
|
52
|
+
const relay = wiring.relay ? await startVertexRelay(wiring.relay) : undefined;
|
|
53
|
+
if (relay) {
|
|
54
|
+
wiring.env.COPILOT_PROVIDER_BASE_URL = relay.url;
|
|
55
|
+
log.line(`relay ${relay.url} -> vertex rawPredict`);
|
|
56
|
+
}
|
|
57
|
+
// As with `run`: every zen command the agent runs lands in the same ledger, tagged with this session.
|
|
58
|
+
const ledger = ledgerPath(project.dir);
|
|
59
|
+
const env = {
|
|
60
|
+
...runtimeEnv(wiring.env),
|
|
61
|
+
...(ledger ? { [LEDGER_ENV]: ledger } : {}),
|
|
62
|
+
[META_SESSION_ENV]: session,
|
|
63
|
+
};
|
|
64
|
+
const tally = new Tally();
|
|
65
|
+
const record = opts.events ? copilotEventLog(opts.events) : undefined;
|
|
66
|
+
const reporter = usageReporter(project.dir);
|
|
67
|
+
// The log keeps what `run`'s log keeps, through the same folding.
|
|
68
|
+
const logSink = {
|
|
69
|
+
answer: () => undefined,
|
|
70
|
+
narrate: (line) => log.line(line),
|
|
71
|
+
detail: (line) => log.line(line),
|
|
72
|
+
warn: (line) => log.line(`! ${line}`),
|
|
73
|
+
};
|
|
74
|
+
const folded = { answer: '', exitCode: 0, events: [] };
|
|
75
|
+
// Paths inside the project read better from where the agent stands.
|
|
76
|
+
const relative = (text) => text.replaceAll(`${project.dir}/`, '');
|
|
77
|
+
const decide = permissionPolicy({
|
|
78
|
+
allowAll: opts.allowAll,
|
|
79
|
+
ask: opts.ask,
|
|
80
|
+
allowTool: opts.allowTool,
|
|
81
|
+
prompt: (request) => opts.prompt?.({ ...request, subject: relative(request.subject) }) ??
|
|
82
|
+
Promise.resolve('no'),
|
|
83
|
+
});
|
|
84
|
+
const startedAt = new Date();
|
|
85
|
+
const release = async () => {
|
|
86
|
+
reporter.stop();
|
|
87
|
+
await relay?.close();
|
|
88
|
+
await log.close();
|
|
89
|
+
};
|
|
90
|
+
let chat;
|
|
91
|
+
try {
|
|
92
|
+
chat = await openChat({
|
|
93
|
+
dir: project.dir,
|
|
94
|
+
model: wiring.env.COPILOT_MODEL ?? wiring.model,
|
|
95
|
+
provider: providerConfig(wiring.env, wiring.keyFile ? googleToken(wiring.keyFile) : undefined),
|
|
96
|
+
env,
|
|
97
|
+
sessionId: session,
|
|
98
|
+
resume: resumed,
|
|
99
|
+
agent: opts.agent,
|
|
100
|
+
effort: opts.effort,
|
|
101
|
+
addDirs: opts.addDirs,
|
|
102
|
+
decide,
|
|
103
|
+
retries: opts.retries ?? DEFAULT_RESUMES,
|
|
104
|
+
connect: opts.connect,
|
|
105
|
+
onRaw: (event) => {
|
|
106
|
+
opts.onRaw?.(event);
|
|
107
|
+
// The log helpers take copilot's JSONL shape, which an SDK event already is.
|
|
108
|
+
const line = event;
|
|
109
|
+
record?.event(line);
|
|
110
|
+
folded.events.length = 0;
|
|
111
|
+
absorb(line, folded, logSink, Infinity);
|
|
112
|
+
},
|
|
113
|
+
onEvent: (event) => {
|
|
114
|
+
if (event.type === 'usage') {
|
|
115
|
+
tally.add(event.call);
|
|
116
|
+
record?.call(event.call);
|
|
117
|
+
appendUsage(project.dir, {
|
|
118
|
+
kind: 'meta.call',
|
|
119
|
+
ts: new Date(Date.parse(event.call.startedAt) + event.call.durationMs).toISOString(),
|
|
120
|
+
...event.call,
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
else if (event.type === 'message' && event.tools === 0 && event.text.trim()) {
|
|
124
|
+
log.line('');
|
|
125
|
+
log.line('--- answer ---');
|
|
126
|
+
log.line(event.text.trim());
|
|
127
|
+
log.saveAnswer(event.text.trim());
|
|
128
|
+
}
|
|
129
|
+
else if (event.type === 'retry') {
|
|
130
|
+
log.line(`--- resume ${event.attempt}: ${event.reason} ---`);
|
|
131
|
+
}
|
|
132
|
+
else if (event.type === 'tool') {
|
|
133
|
+
event = { ...event, subject: relative(event.subject) };
|
|
134
|
+
}
|
|
135
|
+
opts.onEvent(event);
|
|
136
|
+
},
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
catch (err) {
|
|
140
|
+
await release();
|
|
141
|
+
throw err;
|
|
142
|
+
}
|
|
143
|
+
let closing;
|
|
144
|
+
return {
|
|
145
|
+
sessionId: chat.sessionId,
|
|
146
|
+
project,
|
|
147
|
+
model: wiring.model,
|
|
148
|
+
owner: prepared.owner,
|
|
149
|
+
resumed,
|
|
150
|
+
logPath: log.path,
|
|
151
|
+
tokens: () => (tally.calls > 0 ? tally.toString() : ''),
|
|
152
|
+
send: (prompt, shown = prompt) => {
|
|
153
|
+
log.line('');
|
|
154
|
+
log.line(`--- you: ${shown.split('\n')[0]} ---`);
|
|
155
|
+
if (prompt !== shown) {
|
|
156
|
+
log.line(prompt);
|
|
157
|
+
}
|
|
158
|
+
return chat.send(prompt);
|
|
159
|
+
},
|
|
160
|
+
abort: () => chat.abort(),
|
|
161
|
+
close: () => (closing ??= (async () => {
|
|
162
|
+
await chat.close();
|
|
163
|
+
const models = sessionTotals(session);
|
|
164
|
+
if (models) {
|
|
165
|
+
appendUsage(project.dir, {
|
|
166
|
+
kind: 'meta.session',
|
|
167
|
+
ts: new Date().toISOString(),
|
|
168
|
+
session,
|
|
169
|
+
models,
|
|
170
|
+
exitCode: 0,
|
|
171
|
+
resumes: 0,
|
|
172
|
+
startedAt: startedAt.toISOString(),
|
|
173
|
+
durationMs: Date.now() - startedAt.getTime(),
|
|
174
|
+
});
|
|
175
|
+
}
|
|
176
|
+
if (tally.calls > 0) {
|
|
177
|
+
log.line('');
|
|
178
|
+
log.line(`tokens ${tally.toString()}`);
|
|
179
|
+
}
|
|
180
|
+
await release();
|
|
181
|
+
})()),
|
|
182
|
+
};
|
|
183
|
+
}
|
|
184
|
+
//# sourceMappingURL=open.js.map
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import type { PermissionRequest, PermissionRequestResult } from '@github/copilot-sdk';
|
|
2
|
+
/** One entry of `--allow-tool`: `read`, `shell(git:*)`, `write(README.md)`, `url(github.com)`. */
|
|
3
|
+
export interface Rule {
|
|
4
|
+
kind: string;
|
|
5
|
+
pattern?: string;
|
|
6
|
+
}
|
|
7
|
+
/** What the person at the chat said to one request. */
|
|
8
|
+
export type Answer = 'yes' | 'no' | 'always';
|
|
9
|
+
/** A request as a person reads it: which kind of tool, and on what. */
|
|
10
|
+
export interface Described {
|
|
11
|
+
kind: string;
|
|
12
|
+
subject: string;
|
|
13
|
+
intention?: string;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Copilot's own list syntax, so a flag copied from a `zen meta run` line means
|
|
17
|
+
* the same thing here. Separators inside a pattern's parentheses do not split it.
|
|
18
|
+
*/
|
|
19
|
+
export declare function parseRules(list: string | undefined): Rule[];
|
|
20
|
+
export declare function describe(request: PermissionRequest): Described;
|
|
21
|
+
/** Whether any rule covers the request. A compound shell line needs every command covered. */
|
|
22
|
+
export declare function allows(rules: readonly Rule[], request: PermissionRequest): boolean;
|
|
23
|
+
export interface PolicyOptions {
|
|
24
|
+
allowAll?: boolean;
|
|
25
|
+
ask?: boolean;
|
|
26
|
+
allowTool?: string;
|
|
27
|
+
/** asks the person at the chat; only called with `ask` */
|
|
28
|
+
prompt?: (request: Described) => Promise<Answer>;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* The callback for the SDK, with `zen meta run`'s defaults: every tool unless
|
|
32
|
+
* `--allow-tool` narrows it or `--ask` restores the asking. Named tools win over
|
|
33
|
+
* asking, and anything they leave out is asked about with `--ask` and refused
|
|
34
|
+
* without it. "Always" covers that kind of tool for the rest of the session.
|
|
35
|
+
*/
|
|
36
|
+
export declare function permissionPolicy(opts: PolicyOptions): (request: PermissionRequest) => Promise<PermissionRequestResult>;
|
|
37
|
+
//# sourceMappingURL=permissions.d.ts.map
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
// packages/meta/src/chat/permissions.ts
|
|
2
|
+
//
|
|
3
|
+
// Whether `zen meta chat` lets the agent use a tool: the same three answers `zen meta run` gives copilot's flags
|
|
4
|
+
// (every tool, the ones `--allow-tool` names, or ask), decided in-process for the SDK's permission callback.
|
|
5
|
+
// Main: `permissionPolicy(opts)`, which returns the callback; also `parseRules`, `allows` and `describe`.
|
|
6
|
+
// Used by: `chat/run.ts`, which hands the callback to `openChat` and the asking to the chat screen.
|
|
7
|
+
/**
|
|
8
|
+
* Copilot's own list syntax, so a flag copied from a `zen meta run` line means
|
|
9
|
+
* the same thing here. Separators inside a pattern's parentheses do not split it.
|
|
10
|
+
*/
|
|
11
|
+
export function parseRules(list) {
|
|
12
|
+
const rules = [];
|
|
13
|
+
let depth = 0;
|
|
14
|
+
let word = '';
|
|
15
|
+
const push = () => {
|
|
16
|
+
const text = word.trim();
|
|
17
|
+
word = '';
|
|
18
|
+
if (!text) {
|
|
19
|
+
return;
|
|
20
|
+
}
|
|
21
|
+
const m = /^([\w-]+)\s*(?:\((.*)\))?$/s.exec(text);
|
|
22
|
+
rules.push(m ? { kind: m[1], pattern: m[2]?.trim() || undefined } : { kind: text });
|
|
23
|
+
};
|
|
24
|
+
for (const ch of list ?? '') {
|
|
25
|
+
if (ch === '(') {
|
|
26
|
+
depth += 1;
|
|
27
|
+
}
|
|
28
|
+
else if (ch === ')') {
|
|
29
|
+
depth = Math.max(0, depth - 1);
|
|
30
|
+
}
|
|
31
|
+
if ((ch === ',' || ch === ' ') && depth === 0) {
|
|
32
|
+
push();
|
|
33
|
+
}
|
|
34
|
+
else {
|
|
35
|
+
word += ch;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
push();
|
|
39
|
+
return rules;
|
|
40
|
+
}
|
|
41
|
+
export function describe(request) {
|
|
42
|
+
const intention = 'intention' in request && typeof request.intention === 'string'
|
|
43
|
+
? request.intention
|
|
44
|
+
: undefined;
|
|
45
|
+
switch (request.kind) {
|
|
46
|
+
case 'shell':
|
|
47
|
+
return { kind: 'shell', subject: request.fullCommandText, intention };
|
|
48
|
+
case 'write':
|
|
49
|
+
return { kind: 'write', subject: request.fileName, intention };
|
|
50
|
+
case 'read':
|
|
51
|
+
return { kind: 'read', subject: request.path, intention };
|
|
52
|
+
case 'url':
|
|
53
|
+
return { kind: 'url', subject: request.url, intention };
|
|
54
|
+
case 'mcp':
|
|
55
|
+
return { kind: 'mcp', subject: `${request.serverName}/${request.toolName}`, intention };
|
|
56
|
+
default: {
|
|
57
|
+
const r = request;
|
|
58
|
+
return { kind: request.kind, subject: String(r.toolName ?? r.name ?? ''), intention };
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
/** `git:*` is "git, with anything after"; a trailing `*` is a prefix; otherwise the whole of it. */
|
|
63
|
+
function matches(pattern, value) {
|
|
64
|
+
if (pattern.endsWith(':*')) {
|
|
65
|
+
const head = pattern.slice(0, -2);
|
|
66
|
+
return value === head || value.startsWith(`${head} `);
|
|
67
|
+
}
|
|
68
|
+
if (pattern.endsWith('*')) {
|
|
69
|
+
return value.startsWith(pattern.slice(0, -1));
|
|
70
|
+
}
|
|
71
|
+
return value === pattern;
|
|
72
|
+
}
|
|
73
|
+
const pathMatches = (pattern, path) => matches(pattern, path) ||
|
|
74
|
+
matches(pattern, path.split('/').pop() ?? path) ||
|
|
75
|
+
path.endsWith(`/${pattern}`);
|
|
76
|
+
/** Whether any rule covers the request. A compound shell line needs every command covered. */
|
|
77
|
+
export function allows(rules, request) {
|
|
78
|
+
return rules.some((rule) => {
|
|
79
|
+
if (rule.kind !== request.kind) {
|
|
80
|
+
return false;
|
|
81
|
+
}
|
|
82
|
+
const pattern = rule.pattern;
|
|
83
|
+
if (!pattern) {
|
|
84
|
+
return true;
|
|
85
|
+
}
|
|
86
|
+
switch (request.kind) {
|
|
87
|
+
case 'shell': {
|
|
88
|
+
const segments = request.commandSegments?.map((s) => s.fullCommandText.trim());
|
|
89
|
+
const lines = segments?.length ? segments : [request.fullCommandText.trim()];
|
|
90
|
+
return lines.every((line) => matches(pattern, line));
|
|
91
|
+
}
|
|
92
|
+
case 'write':
|
|
93
|
+
return pathMatches(pattern, request.fileName);
|
|
94
|
+
case 'read':
|
|
95
|
+
return pathMatches(pattern, request.path);
|
|
96
|
+
case 'url': {
|
|
97
|
+
let host = request.url;
|
|
98
|
+
try {
|
|
99
|
+
host = new URL(request.url).hostname;
|
|
100
|
+
}
|
|
101
|
+
catch {
|
|
102
|
+
// Not a url the parser takes: the pattern is matched against it as written.
|
|
103
|
+
}
|
|
104
|
+
return matches(pattern, host) || host.endsWith(`.${pattern}`);
|
|
105
|
+
}
|
|
106
|
+
case 'mcp':
|
|
107
|
+
return pattern === request.serverName || pattern === request.toolName;
|
|
108
|
+
default:
|
|
109
|
+
return false;
|
|
110
|
+
}
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* The callback for the SDK, with `zen meta run`'s defaults: every tool unless
|
|
115
|
+
* `--allow-tool` narrows it or `--ask` restores the asking. Named tools win over
|
|
116
|
+
* asking, and anything they leave out is asked about with `--ask` and refused
|
|
117
|
+
* without it. "Always" covers that kind of tool for the rest of the session.
|
|
118
|
+
*/
|
|
119
|
+
export function permissionPolicy(opts) {
|
|
120
|
+
const rules = parseRules(opts.allowTool);
|
|
121
|
+
const everything = opts.allowAll || (!opts.ask && rules.length === 0);
|
|
122
|
+
const granted = new Set();
|
|
123
|
+
return async (request) => {
|
|
124
|
+
if (request.managedApprovalRequired) {
|
|
125
|
+
return { kind: 'no-result' };
|
|
126
|
+
}
|
|
127
|
+
if (everything || granted.has(request.kind) || allows(rules, request)) {
|
|
128
|
+
return { kind: 'approve-once' };
|
|
129
|
+
}
|
|
130
|
+
if (!opts.ask || !opts.prompt) {
|
|
131
|
+
return {
|
|
132
|
+
kind: 'reject',
|
|
133
|
+
feedback: `Not allowed: this chat only allows ${opts.allowTool}.`,
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
const answer = await opts.prompt(describe(request));
|
|
137
|
+
if (answer === 'always') {
|
|
138
|
+
granted.add(request.kind);
|
|
139
|
+
}
|
|
140
|
+
// Copilot tells the model a person said no; there is nothing to add to that.
|
|
141
|
+
return answer === 'no' ? { kind: 'reject' } : { kind: 'approve-once' };
|
|
142
|
+
};
|
|
143
|
+
}
|
|
144
|
+
//# sourceMappingURL=permissions.js.map
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type * as Projects from '@zenera/engine';
|
|
2
|
+
import type { Selection } from '../prepare.ts';
|
|
3
|
+
import type { Connect } from './session.ts';
|
|
4
|
+
import type { Screen } from './tui.tsx';
|
|
5
|
+
export interface ChatFlags extends Selection {
|
|
6
|
+
agent?: string;
|
|
7
|
+
effort?: string;
|
|
8
|
+
events?: string;
|
|
9
|
+
'allow-all'?: boolean;
|
|
10
|
+
'allow-tool'?: string;
|
|
11
|
+
ask?: boolean;
|
|
12
|
+
'add-dir'?: string[];
|
|
13
|
+
'no-refresh'?: boolean;
|
|
14
|
+
}
|
|
15
|
+
export interface RunChat {
|
|
16
|
+
values: ChatFlags;
|
|
17
|
+
project: Projects.Project;
|
|
18
|
+
cwd: string;
|
|
19
|
+
/** the first question, from the command line */
|
|
20
|
+
first?: string;
|
|
21
|
+
retries: number;
|
|
22
|
+
/** injected by the tests, which want no runtime */
|
|
23
|
+
connect?: Connect;
|
|
24
|
+
/** likewise: the tests have no terminal to draw on */
|
|
25
|
+
screen?: (screen: Screen) => Promise<void>;
|
|
26
|
+
}
|
|
27
|
+
export declare function runChat(opts: RunChat): Promise<void>;
|
|
28
|
+
//# sourceMappingURL=run.d.ts.map
|
package/dist/chat/run.js
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
// packages/meta/src/chat/run.ts
|
|
2
|
+
//
|
|
3
|
+
// `zen meta chat` from flags to the last line: open the session (`openMetaChat` does the model, key, log, relay
|
|
4
|
+
// and ledger), hand it to the chat screen, and on the way out close it and say how to carry on.
|
|
5
|
+
// Main: `runChat(opts)`.
|
|
6
|
+
// Used by: `command.ts`, whose `chat()` checks the terminal and the project and then calls this.
|
|
7
|
+
import { join, resolve } from 'node:path';
|
|
8
|
+
import { cyan, dim, History, note, resolveTheme } from "../host.js";
|
|
9
|
+
import { listPrompts, loadPrompt } from "../prompts.js";
|
|
10
|
+
import { openMetaChat } from "./open.js";
|
|
11
|
+
export async function runChat(opts) {
|
|
12
|
+
const { values, project } = opts;
|
|
13
|
+
const bridge = {};
|
|
14
|
+
let chat;
|
|
15
|
+
try {
|
|
16
|
+
chat = await openMetaChat({
|
|
17
|
+
project,
|
|
18
|
+
provider: values.provider,
|
|
19
|
+
model: values.model,
|
|
20
|
+
resume: values.resume,
|
|
21
|
+
continue: values.continue,
|
|
22
|
+
'session-id': values['session-id'],
|
|
23
|
+
agent: values.agent,
|
|
24
|
+
effort: values.effort,
|
|
25
|
+
allowAll: values['allow-all'],
|
|
26
|
+
allowTool: values['allow-tool'],
|
|
27
|
+
ask: values.ask,
|
|
28
|
+
prompt: (request) => bridge.onAsk?.(request) ?? Promise.resolve('no'),
|
|
29
|
+
addDirs: values['add-dir']?.map((d) => resolve(opts.cwd, d)),
|
|
30
|
+
events: values.events ? resolve(opts.cwd, values.events) : undefined,
|
|
31
|
+
retries: opts.retries,
|
|
32
|
+
refresh: !values['no-refresh'],
|
|
33
|
+
connect: opts.connect,
|
|
34
|
+
onEvent: (event) => bridge.onEvent?.(event),
|
|
35
|
+
});
|
|
36
|
+
const meta = chat;
|
|
37
|
+
// The screen reports what was typed just before it sends what that expanded to.
|
|
38
|
+
let shown;
|
|
39
|
+
const screenChat = {
|
|
40
|
+
sessionId: meta.sessionId,
|
|
41
|
+
send: (prompt) => meta.send(prompt, shown ?? prompt),
|
|
42
|
+
abort: () => meta.abort(),
|
|
43
|
+
close: () => meta.close(),
|
|
44
|
+
};
|
|
45
|
+
const screen = opts.screen ?? (await import("./tui.js")).chatScreen;
|
|
46
|
+
await screen({
|
|
47
|
+
chat: screenChat,
|
|
48
|
+
bridge,
|
|
49
|
+
history: new History(join(project.dir, '.tmp', 'logs', 'meta.history')),
|
|
50
|
+
theme: await resolveTheme(),
|
|
51
|
+
title: {
|
|
52
|
+
owner: meta.owner,
|
|
53
|
+
model: meta.model,
|
|
54
|
+
project: project.name,
|
|
55
|
+
resumed: meta.resumed,
|
|
56
|
+
},
|
|
57
|
+
status: () => meta.tokens(),
|
|
58
|
+
prompts: {
|
|
59
|
+
list: () => listPrompts(project.dir),
|
|
60
|
+
load: (name) => loadPrompt(project.dir, name),
|
|
61
|
+
},
|
|
62
|
+
first: opts.first,
|
|
63
|
+
onSend: (typed) => {
|
|
64
|
+
shown = typed;
|
|
65
|
+
},
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
finally {
|
|
69
|
+
await chat?.close();
|
|
70
|
+
}
|
|
71
|
+
const tokens = chat.tokens();
|
|
72
|
+
if (tokens) {
|
|
73
|
+
note(dim(`tokens ${tokens}`));
|
|
74
|
+
}
|
|
75
|
+
note(dim(`carry on: zen meta chat ${project.name} --resume ${chat.sessionId}`));
|
|
76
|
+
note(cyan(chat.logPath));
|
|
77
|
+
}
|
|
78
|
+
//# sourceMappingURL=run.js.map
|