@zenera/meta 1.3.2 → 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 +5 -0
- 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/run.d.ts +3 -2
- package/dist/chat/run.js +40 -134
- package/dist/command.js +7 -39
- package/dist/launch.d.ts +2 -1
- package/dist/launch.js +1 -3
- package/dist/prepare.d.ts +9 -0
- package/dist/prepare.js +22 -3
- package/package.json +9 -5
package/README.md
CHANGED
|
@@ -153,6 +153,11 @@ holds that line. The CLI keeps what `zen init` and `zen open` also need - the
|
|
|
153
153
|
`.github/` editor tree and its templates, and the usage ledger. See §7.6 of the
|
|
154
154
|
[CLI design notes](https://github.com/zenera-ai/ZeneraNeo/blob/main/packages/cli/DESIGN.md).
|
|
155
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
|
+
|
|
156
161
|
## The rest of the family
|
|
157
162
|
|
|
158
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
|
package/dist/chat/run.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type * as Projects from '@zenera/engine';
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
2
|
+
import type { Selection } from '../prepare.ts';
|
|
3
|
+
import type { Connect } from './session.ts';
|
|
4
4
|
import type { Screen } from './tui.tsx';
|
|
5
5
|
export interface ChatFlags extends Selection {
|
|
6
6
|
agent?: string;
|
|
@@ -10,6 +10,7 @@ export interface ChatFlags extends Selection {
|
|
|
10
10
|
'allow-tool'?: string;
|
|
11
11
|
ask?: boolean;
|
|
12
12
|
'add-dir'?: string[];
|
|
13
|
+
'no-refresh'?: boolean;
|
|
13
14
|
}
|
|
14
15
|
export interface RunChat {
|
|
15
16
|
values: ChatFlags;
|
package/dist/chat/run.js
CHANGED
|
@@ -1,172 +1,78 @@
|
|
|
1
1
|
// packages/meta/src/chat/run.ts
|
|
2
2
|
//
|
|
3
|
-
// `zen meta chat` from flags to the last line:
|
|
4
|
-
//
|
|
5
|
-
// session's usage and say how to carry on.
|
|
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.
|
|
6
5
|
// Main: `runChat(opts)`.
|
|
7
6
|
// Used by: `command.ts`, whose `chat()` checks the terminal and the project and then calls this.
|
|
8
|
-
import { randomUUID } from 'node:crypto';
|
|
9
7
|
import { join, resolve } from 'node:path';
|
|
10
|
-
import {
|
|
11
|
-
import { absorb, openLog, recordSession } from "../launch.js";
|
|
12
|
-
import { announce, prepare } from "../prepare.js";
|
|
8
|
+
import { cyan, dim, History, note, resolveTheme } from "../host.js";
|
|
13
9
|
import { listPrompts, loadPrompt } from "../prompts.js";
|
|
14
|
-
import {
|
|
15
|
-
import { usageReporter } from "../reporter.js";
|
|
16
|
-
import { SOURCE_LABELS } from "../model.js";
|
|
17
|
-
import { sessionTotals, Tally } from "../tokens.js";
|
|
18
|
-
import { permissionPolicy } from "./permissions.js";
|
|
19
|
-
import { googleToken, openChat, providerConfig, runtimeEnv, } from "./session.js";
|
|
10
|
+
import { openMetaChat } from "./open.js";
|
|
20
11
|
export async function runChat(opts) {
|
|
21
|
-
const { project } = opts;
|
|
22
|
-
const prepared = await prepare(opts.values, project);
|
|
23
|
-
const { values, wiring, chosen } = prepared;
|
|
24
|
-
const resumed = values.resume;
|
|
25
|
-
// `prepare` names a fresh session unless --continue, which the caller has already resolved.
|
|
26
|
-
const session = prepared.session ?? randomUUID();
|
|
27
|
-
announce(prepared, project);
|
|
28
|
-
recordSession(project.dir, session);
|
|
29
|
-
const log = openLog(project.dir, new Date(), session);
|
|
30
|
-
note(`log ${log.path}`);
|
|
31
|
-
log.line(`zen meta chat ${new Date().toISOString()}`);
|
|
32
|
-
log.line(`project ${project.name} ${project.dir}`);
|
|
33
|
-
log.line(`model ${chosen.ref} from ${SOURCE_LABELS[chosen.from]}`);
|
|
34
|
-
log.line(`session ${session}${resumed ? ' (resumed)' : ''}`);
|
|
35
|
-
const relay = wiring.relay ? await startVertexRelay(wiring.relay) : undefined;
|
|
36
|
-
if (relay) {
|
|
37
|
-
wiring.env.COPILOT_PROVIDER_BASE_URL = relay.url;
|
|
38
|
-
log.line(`relay ${relay.url} -> vertex rawPredict`);
|
|
39
|
-
}
|
|
40
|
-
// As with `run`: every zen command the agent runs lands in the same ledger, tagged with this session.
|
|
41
|
-
const ledger = ledgerPath(project.dir);
|
|
42
|
-
const env = {
|
|
43
|
-
...runtimeEnv(wiring.env),
|
|
44
|
-
...(ledger ? { [LEDGER_ENV]: ledger } : {}),
|
|
45
|
-
[META_SESSION_ENV]: session,
|
|
46
|
-
};
|
|
47
|
-
const tally = new Tally();
|
|
48
|
-
const record = values.events ? copilotEventLog(resolve(opts.cwd, values.events)) : undefined;
|
|
49
|
-
const reporter = usageReporter(project.dir);
|
|
50
|
-
// The log keeps what `run`'s log keeps, through the same folding.
|
|
51
|
-
const logSink = {
|
|
52
|
-
answer: () => undefined,
|
|
53
|
-
narrate: (line) => log.line(line),
|
|
54
|
-
detail: (line) => log.line(line),
|
|
55
|
-
warn: (line) => log.line(`! ${line}`),
|
|
56
|
-
};
|
|
57
|
-
const folded = { answer: '', exitCode: 0, events: [] };
|
|
58
|
-
// Paths inside the project read better from where the agent stands.
|
|
59
|
-
const relative = (text) => text.replaceAll(`${project.dir}/`, '');
|
|
12
|
+
const { values, project } = opts;
|
|
60
13
|
const bridge = {};
|
|
61
|
-
const decide = permissionPolicy({
|
|
62
|
-
allowAll: values['allow-all'],
|
|
63
|
-
ask: values.ask,
|
|
64
|
-
allowTool: values['allow-tool'],
|
|
65
|
-
prompt: (request) => bridge.onAsk?.({ ...request, subject: relative(request.subject) }) ??
|
|
66
|
-
Promise.resolve('no'),
|
|
67
|
-
});
|
|
68
14
|
let chat;
|
|
69
|
-
const startedAt = new Date();
|
|
70
15
|
try {
|
|
71
|
-
chat = await
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
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'],
|
|
78
23
|
agent: values.agent,
|
|
79
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'),
|
|
80
29
|
addDirs: values['add-dir']?.map((d) => resolve(opts.cwd, d)),
|
|
81
|
-
|
|
30
|
+
events: values.events ? resolve(opts.cwd, values.events) : undefined,
|
|
82
31
|
retries: opts.retries,
|
|
32
|
+
refresh: !values['no-refresh'],
|
|
83
33
|
connect: opts.connect,
|
|
84
|
-
|
|
85
|
-
// The log helpers take copilot's JSONL shape, which an SDK event already is.
|
|
86
|
-
const line = event;
|
|
87
|
-
record?.event(line);
|
|
88
|
-
folded.events.length = 0;
|
|
89
|
-
absorb(line, folded, logSink, Infinity);
|
|
90
|
-
},
|
|
91
|
-
onEvent: (event) => {
|
|
92
|
-
if (event.type === 'usage') {
|
|
93
|
-
tally.add(event.call);
|
|
94
|
-
record?.call(event.call);
|
|
95
|
-
appendUsage(project.dir, {
|
|
96
|
-
kind: 'meta.call',
|
|
97
|
-
ts: new Date(Date.parse(event.call.startedAt) + event.call.durationMs).toISOString(),
|
|
98
|
-
...event.call,
|
|
99
|
-
});
|
|
100
|
-
}
|
|
101
|
-
else if (event.type === 'message' && event.tools === 0 && event.text.trim()) {
|
|
102
|
-
log.line('');
|
|
103
|
-
log.line('--- answer ---');
|
|
104
|
-
log.line(event.text.trim());
|
|
105
|
-
log.saveAnswer(event.text.trim());
|
|
106
|
-
}
|
|
107
|
-
else if (event.type === 'retry') {
|
|
108
|
-
log.line(`--- resume ${event.attempt}: ${event.reason} ---`);
|
|
109
|
-
}
|
|
110
|
-
else if (event.type === 'tool') {
|
|
111
|
-
event = { ...event, subject: relative(event.subject) };
|
|
112
|
-
}
|
|
113
|
-
bridge.onEvent?.(event);
|
|
114
|
-
},
|
|
34
|
+
onEvent: (event) => bridge.onEvent?.(event),
|
|
115
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
|
+
};
|
|
116
45
|
const screen = opts.screen ?? (await import("./tui.js")).chatScreen;
|
|
117
46
|
await screen({
|
|
118
|
-
chat,
|
|
47
|
+
chat: screenChat,
|
|
119
48
|
bridge,
|
|
120
49
|
history: new History(join(project.dir, '.tmp', 'logs', 'meta.history')),
|
|
121
50
|
theme: await resolveTheme(),
|
|
122
51
|
title: {
|
|
123
|
-
owner:
|
|
124
|
-
model:
|
|
52
|
+
owner: meta.owner,
|
|
53
|
+
model: meta.model,
|
|
125
54
|
project: project.name,
|
|
126
|
-
resumed:
|
|
55
|
+
resumed: meta.resumed,
|
|
127
56
|
},
|
|
128
|
-
status: () =>
|
|
57
|
+
status: () => meta.tokens(),
|
|
129
58
|
prompts: {
|
|
130
59
|
list: () => listPrompts(project.dir),
|
|
131
60
|
load: (name) => loadPrompt(project.dir, name),
|
|
132
61
|
},
|
|
133
62
|
first: opts.first,
|
|
134
|
-
onSend: (
|
|
135
|
-
|
|
136
|
-
log.line(`--- you: ${shown.split('\n')[0]} ---`);
|
|
137
|
-
if (prompt !== shown) {
|
|
138
|
-
log.line(prompt);
|
|
139
|
-
}
|
|
63
|
+
onSend: (typed) => {
|
|
64
|
+
shown = typed;
|
|
140
65
|
},
|
|
141
66
|
});
|
|
142
67
|
}
|
|
143
68
|
finally {
|
|
144
69
|
await chat?.close();
|
|
145
|
-
const models = sessionTotals(session);
|
|
146
|
-
if (models) {
|
|
147
|
-
appendUsage(project.dir, {
|
|
148
|
-
kind: 'meta.session',
|
|
149
|
-
ts: new Date().toISOString(),
|
|
150
|
-
session,
|
|
151
|
-
models,
|
|
152
|
-
exitCode: 0,
|
|
153
|
-
resumes: 0,
|
|
154
|
-
startedAt: startedAt.toISOString(),
|
|
155
|
-
durationMs: Date.now() - startedAt.getTime(),
|
|
156
|
-
});
|
|
157
|
-
}
|
|
158
|
-
if (tally.calls > 0) {
|
|
159
|
-
log.line('');
|
|
160
|
-
log.line(`tokens ${tally.toString()}`);
|
|
161
|
-
}
|
|
162
|
-
reporter.stop();
|
|
163
|
-
await relay?.close();
|
|
164
|
-
log.close();
|
|
165
70
|
}
|
|
166
|
-
|
|
167
|
-
|
|
71
|
+
const tokens = chat.tokens();
|
|
72
|
+
if (tokens) {
|
|
73
|
+
note(dim(`tokens ${tokens}`));
|
|
168
74
|
}
|
|
169
|
-
note(dim(`carry on: zen meta chat ${project.name} --resume ${
|
|
170
|
-
note(cyan(
|
|
75
|
+
note(dim(`carry on: zen meta chat ${project.name} --resume ${chat.sessionId}`));
|
|
76
|
+
note(cyan(chat.logPath));
|
|
171
77
|
}
|
|
172
78
|
//# sourceMappingURL=run.js.map
|
package/dist/command.js
CHANGED
|
@@ -12,10 +12,10 @@ import { gatewayOf } from '@zenera/neo';
|
|
|
12
12
|
import { readFileSync } from 'node:fs';
|
|
13
13
|
import { join, resolve } from 'node:path';
|
|
14
14
|
import { setTimeout as sleep } from 'node:timers/promises';
|
|
15
|
-
import { appendUsage, bold, browseModels, choose, CliError, copilotEventLog, credentialError, cyan, dim, duration,
|
|
15
|
+
import { appendUsage, bold, browseModels, choose, CliError, copilotEventLog, credentialError, cyan, dim, duration, EXIT, green, isInteractive, json, LEDGER_ENV, ledgerPath, matching, META_PROMPT_ENV, META_SESSION_ENV, note, pad, parse, probeChat, probeModel, progress, projectNames, readStdin, resolveProject, splitRef, table, TEMPLATES, usageError, warn, wrap, write, writeAll, } from "./host.js";
|
|
16
16
|
import { answerBox, chooseModel, copilotVersion, DATASET_DETAILS, DATASET_SUMMARY, DATASET_USAGE, datasetProjectFlag, DEFAULT_RESUMES, fallbackRef, FINETUNE_DETAILS, FINETUNE_SUMMARY, FINETUNE_USAGE, finetuneProjectFlag, lastSession, launch, listPrompts, loadPrompt, masked, metaCompatible, misspelledProvider, MODEL_ENV, openLog, ORDER, SKILL_DIR, readMeta, recordSession, RESUME_PROMPT, resumeDelayMs, runDataset, runFinetune, sdkVersion, sessionTotals, SOURCE_LABELS, startVertexRelay, tailSpans, Tally, terminalSink, transient, writeMeta, } from "./index.js";
|
|
17
17
|
import { EFFORTS } from "./chat/session.js";
|
|
18
|
-
import { announce, prepare, projectModel } from "./prepare.js";
|
|
18
|
+
import { announce, prepare, projectModel, refreshEditor } from "./prepare.js";
|
|
19
19
|
import { usageReporter } from "./reporter.js";
|
|
20
20
|
const USAGE = 'zen meta run [project] [prompt] [options]';
|
|
21
21
|
const VERBS = new Set(['model', 'run', 'chat', 'prompts', 'resume', 'dataset', 'finetune']);
|
|
@@ -310,25 +310,6 @@ async function where(ctx, flag, positionals) {
|
|
|
310
310
|
});
|
|
311
311
|
return { project, rest: named ? tail : positionals };
|
|
312
312
|
}
|
|
313
|
-
/**
|
|
314
|
-
* The editor's files, rewritten before the agent reads them — the same refresh
|
|
315
|
-
* `zen open` does, for the same reason. `.github/` is the brief this agent works
|
|
316
|
-
* from and the prompts it is offered, and it describes the version of `zen` in
|
|
317
|
-
* hand; a project scaffolded by an older one would otherwise be briefed on a
|
|
318
|
-
* runtime that has moved. A failure is narrated, not raised: a read-only
|
|
319
|
-
* checkout is no reason to refuse to answer a question.
|
|
320
|
-
*/
|
|
321
|
-
function refresh(project) {
|
|
322
|
-
try {
|
|
323
|
-
const written = editorFiles(project.dir);
|
|
324
|
-
if (written.length) {
|
|
325
|
-
note(dim(`refreshed ${written.length} editor files in ${project.name}`));
|
|
326
|
-
}
|
|
327
|
-
}
|
|
328
|
-
catch (err) {
|
|
329
|
-
warn(`could not refresh the editor files — ${err.message}`);
|
|
330
|
-
}
|
|
331
|
-
}
|
|
332
313
|
// ---------------------------------------------------------------------------
|
|
333
314
|
// zen meta run [project] [/<name>|<question>] [words...]
|
|
334
315
|
//
|
|
@@ -357,7 +338,7 @@ async function stored(ctx, values, args) {
|
|
|
357
338
|
}
|
|
358
339
|
const { project } = await where(ctx, values.project, head);
|
|
359
340
|
if (!values['no-refresh']) {
|
|
360
|
-
|
|
341
|
+
refreshEditor(project);
|
|
361
342
|
}
|
|
362
343
|
if (name) {
|
|
363
344
|
return await runPrompt(ctx, values, project, name, rest);
|
|
@@ -508,7 +489,7 @@ export async function completeMeta(args, cur, cwd) {
|
|
|
508
489
|
// ---------------------------------------------------------------------------
|
|
509
490
|
async function prompts(ctx, values, args) {
|
|
510
491
|
const { project } = await where(ctx, values.project, args);
|
|
511
|
-
|
|
492
|
+
refreshEditor(project);
|
|
512
493
|
const names = await listPrompts(project.dir);
|
|
513
494
|
const found = await Promise.all(names.map((name) => loadPrompt(project.dir, name)));
|
|
514
495
|
if (ctx.json) {
|
|
@@ -592,22 +573,9 @@ async function chat(ctx, values, args) {
|
|
|
592
573
|
throw usageError('zen meta chat needs a terminal to talk in', 'for one answer: zen meta run "<question>"');
|
|
593
574
|
}
|
|
594
575
|
const { project, rest } = await where(ctx, values.project, args);
|
|
595
|
-
if (!values['no-refresh']) {
|
|
596
|
-
refresh(project);
|
|
597
|
-
}
|
|
598
|
-
let resume = values.resume;
|
|
599
|
-
if (values.continue && !resume) {
|
|
600
|
-
resume = lastSession(project.dir);
|
|
601
|
-
if (!resume) {
|
|
602
|
-
note(dim(`no meta session recorded in ${project.name} - starting a new one`));
|
|
603
|
-
}
|
|
604
|
-
}
|
|
605
|
-
if (resume) {
|
|
606
|
-
note(dim(`resuming session ${resume}`));
|
|
607
|
-
}
|
|
608
576
|
const { runChat } = await import("./chat/run.js");
|
|
609
577
|
await runChat({
|
|
610
|
-
values
|
|
578
|
+
values,
|
|
611
579
|
project,
|
|
612
580
|
cwd: ctx.cwd,
|
|
613
581
|
first: (values.prompt ?? rest.join(' ')).trim() || undefined,
|
|
@@ -631,7 +599,7 @@ async function resumeRun(ctx, values, args) {
|
|
|
631
599
|
}
|
|
632
600
|
note(dim(`resuming session ${session}`));
|
|
633
601
|
if (!values['no-refresh']) {
|
|
634
|
-
|
|
602
|
+
refreshEditor(project);
|
|
635
603
|
}
|
|
636
604
|
const message = withFile(values, (values.prompt ?? words.join(' ')).trim()) || RESUME_PROMPT;
|
|
637
605
|
await go(ctx, { ...values, resume: session, continue: false }, project, message);
|
|
@@ -809,7 +777,7 @@ async function go(ctx, values, project, prompt, promptName) {
|
|
|
809
777
|
tail.stop();
|
|
810
778
|
reporter.stop();
|
|
811
779
|
await relay?.close();
|
|
812
|
-
log.close();
|
|
780
|
+
await log.close();
|
|
813
781
|
}
|
|
814
782
|
}
|
|
815
783
|
/** The wait before a resume, cut short by Ctrl-C - which still says how to resume. */
|
package/dist/launch.d.ts
CHANGED
|
@@ -49,7 +49,8 @@ export interface Log {
|
|
|
49
49
|
readonly answerPath: string;
|
|
50
50
|
line(text: string): void;
|
|
51
51
|
saveAnswer(text: string): void;
|
|
52
|
-
|
|
52
|
+
/** resolves once every line is on disk */
|
|
53
|
+
close(): Promise<void>;
|
|
53
54
|
}
|
|
54
55
|
/**
|
|
55
56
|
* `<project>/.tmp/logs/meta.<when>[-<session>].log`, opened for append. The
|
package/dist/launch.js
CHANGED
|
@@ -93,9 +93,7 @@ export function openLog(dir, now = new Date(), session) {
|
|
|
93
93
|
saveAnswer: (text) => {
|
|
94
94
|
writeFileSync(answerPath, text.endsWith('\n') ? text : `${text}\n`);
|
|
95
95
|
},
|
|
96
|
-
close: () =>
|
|
97
|
-
file.end();
|
|
98
|
-
},
|
|
96
|
+
close: () => new Promise((done) => file.end(done)),
|
|
99
97
|
};
|
|
100
98
|
}
|
|
101
99
|
/** The project's last meta session, so `zen meta resume` needs no id. */
|
package/dist/prepare.d.ts
CHANGED
|
@@ -28,6 +28,15 @@ export interface Prepared<F extends Selection> {
|
|
|
28
28
|
* continue, so a model change refuses it instead of starting over.
|
|
29
29
|
*/
|
|
30
30
|
export declare function prepare<F extends Selection>(values: F, project: Projects.Project, prompt?: string): Promise<Prepared<F>>;
|
|
31
|
+
/**
|
|
32
|
+
* The editor's files, rewritten before the agent reads them — the same refresh
|
|
33
|
+
* `zen open` does, for the same reason. `.github/` is the brief this agent works
|
|
34
|
+
* from and the prompts it is offered, and it describes the version of `zen` in
|
|
35
|
+
* hand; a project scaffolded by an older one would otherwise be briefed on a
|
|
36
|
+
* runtime that has moved. A failure is narrated, not raised: a read-only
|
|
37
|
+
* checkout is no reason to refuse to answer a question.
|
|
38
|
+
*/
|
|
39
|
+
export declare function refreshEditor(project: Projects.Project): void;
|
|
31
40
|
/** What a session says on stderr before it starts: the warnings, then which model on which key. */
|
|
32
41
|
export declare function announce(p: Prepared<Selection>, project: Projects.Project): void;
|
|
33
42
|
/** The project's own `model:`, with a `models:` alias resolved to what it names. */
|
package/dist/prepare.js
CHANGED
|
@@ -3,13 +3,13 @@
|
|
|
3
3
|
// Everything a meta agent session needs before it starts, shared by `zen meta run` and `zen meta chat`.
|
|
4
4
|
// Main: `prepare(values, project)` - load the project's .env, open the keyring, choose the model, wire the key,
|
|
5
5
|
// settle the session id (refusing a resume across models). Also `announce` (the stderr lines a run opens with),
|
|
6
|
-
// `projectModel` (agents.yaml's `model:`) and `asProvider`.
|
|
7
|
-
// Used by: `command.ts` (`go()` for run/resume,
|
|
6
|
+
// `refreshEditor` (rewrite the project's editor files), `projectModel` (agents.yaml's `model:`) and `asProvider`.
|
|
7
|
+
// Used by: `command.ts` (`go()` for run/resume, and `model()` for `projectModel`) and `chat/open.ts`.
|
|
8
8
|
import * as Projects from '@zenera/engine';
|
|
9
9
|
import { assertOwner, ensureHome, isProvider, KeyStore, loadProjectEnv, PROVIDERS, } from '@zenera/engine';
|
|
10
10
|
import { gatewayOf, readProjectConfig } from '@zenera/neo';
|
|
11
11
|
import { randomUUID } from 'node:crypto';
|
|
12
|
-
import { credentialError, cyan, dim, note, splitRef, usageError, warn } from "./host.js";
|
|
12
|
+
import { credentialError, cyan, dim, editorFiles, note, splitRef, usageError, warn, } from "./host.js";
|
|
13
13
|
import { locate, RESUME_PROMPT } from "./launch.js";
|
|
14
14
|
import { chooseModel, fallbackRef, misspelledProvider, MODEL_ENV, readMeta } from "./model.js";
|
|
15
15
|
import { sameModel, sessionModel } from "./tokens.js";
|
|
@@ -84,6 +84,25 @@ export async function prepare(values, project, prompt) {
|
|
|
84
84
|
session: values.resume ?? (values.continue ? undefined : (fresh ?? randomUUID())),
|
|
85
85
|
};
|
|
86
86
|
}
|
|
87
|
+
/**
|
|
88
|
+
* The editor's files, rewritten before the agent reads them — the same refresh
|
|
89
|
+
* `zen open` does, for the same reason. `.github/` is the brief this agent works
|
|
90
|
+
* from and the prompts it is offered, and it describes the version of `zen` in
|
|
91
|
+
* hand; a project scaffolded by an older one would otherwise be briefed on a
|
|
92
|
+
* runtime that has moved. A failure is narrated, not raised: a read-only
|
|
93
|
+
* checkout is no reason to refuse to answer a question.
|
|
94
|
+
*/
|
|
95
|
+
export function refreshEditor(project) {
|
|
96
|
+
try {
|
|
97
|
+
const written = editorFiles(project.dir);
|
|
98
|
+
if (written.length) {
|
|
99
|
+
note(dim(`refreshed ${written.length} editor files in ${project.name}`));
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
catch (err) {
|
|
103
|
+
warn(`could not refresh the editor files — ${err.message}`);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
87
106
|
/** What a session says on stderr before it starts: the warnings, then which model on which key. */
|
|
88
107
|
export function announce(p, project) {
|
|
89
108
|
for (const line of p.wiring.warnings) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zenera/meta",
|
|
3
|
-
"version": "1.3.
|
|
3
|
+
"version": "1.3.3",
|
|
4
4
|
"description": "The meta agent for zen: GitHub Copilot over a zen project on your own keys, plus the dataset and fine-tuning loop.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"agents",
|
|
@@ -28,6 +28,10 @@
|
|
|
28
28
|
"types": "./dist/command.d.ts",
|
|
29
29
|
"default": "./dist/command.js"
|
|
30
30
|
},
|
|
31
|
+
"./chat": {
|
|
32
|
+
"types": "./dist/chat/index.d.ts",
|
|
33
|
+
"default": "./dist/chat/index.js"
|
|
34
|
+
},
|
|
31
35
|
"./package.json": "./package.json"
|
|
32
36
|
},
|
|
33
37
|
"files": [
|
|
@@ -45,10 +49,10 @@
|
|
|
45
49
|
"dependencies": {
|
|
46
50
|
"@github/copilot-sdk": "^1.0.32",
|
|
47
51
|
"@inkjs/ui": "^2.0.0",
|
|
48
|
-
"@zenera/cli": "^1.3.
|
|
49
|
-
"@zenera/engine": "^1.3.
|
|
50
|
-
"@zenera/neo": "^1.3.
|
|
51
|
-
"@zenera/view": "^1.3.
|
|
52
|
+
"@zenera/cli": "^1.3.3",
|
|
53
|
+
"@zenera/engine": "^1.3.3",
|
|
54
|
+
"@zenera/neo": "^1.3.3",
|
|
55
|
+
"@zenera/view": "^1.3.3",
|
|
52
56
|
"google-auth-library": "^10.0.0",
|
|
53
57
|
"ink": "^7.1.1",
|
|
54
58
|
"react": "^19.2.8",
|