@zenera/meta 1.3.1 → 1.3.2
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 +19 -6
- package/dist/chat/permissions.d.ts +37 -0
- package/dist/chat/permissions.js +144 -0
- package/dist/chat/run.d.ts +27 -0
- package/dist/chat/run.js +172 -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 +120 -149
- package/dist/host.d.ts +1 -1
- package/dist/host.js +1 -1
- package/dist/launch.d.ts +9 -0
- package/dist/launch.js +39 -3
- package/dist/prepare.d.ts +36 -0
- package/dist/prepare.js +131 -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 +7 -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
|
|
@@ -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,27 @@
|
|
|
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
|
+
}
|
|
14
|
+
export interface RunChat {
|
|
15
|
+
values: ChatFlags;
|
|
16
|
+
project: Projects.Project;
|
|
17
|
+
cwd: string;
|
|
18
|
+
/** the first question, from the command line */
|
|
19
|
+
first?: string;
|
|
20
|
+
retries: number;
|
|
21
|
+
/** injected by the tests, which want no runtime */
|
|
22
|
+
connect?: Connect;
|
|
23
|
+
/** likewise: the tests have no terminal to draw on */
|
|
24
|
+
screen?: (screen: Screen) => Promise<void>;
|
|
25
|
+
}
|
|
26
|
+
export declare function runChat(opts: RunChat): Promise<void>;
|
|
27
|
+
//# sourceMappingURL=run.d.ts.map
|
package/dist/chat/run.js
ADDED
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
// packages/meta/src/chat/run.ts
|
|
2
|
+
//
|
|
3
|
+
// `zen meta chat` from flags to the last line: prepare the model and key, open the log, start the Vertex relay
|
|
4
|
+
// when Claude needs it, open the SDK session, hand it to the chat screen, and on the way out record the
|
|
5
|
+
// session's usage and say how to carry on.
|
|
6
|
+
// Main: `runChat(opts)`.
|
|
7
|
+
// Used by: `command.ts`, whose `chat()` checks the terminal and the project and then calls this.
|
|
8
|
+
import { randomUUID } from 'node:crypto';
|
|
9
|
+
import { join, resolve } from 'node:path';
|
|
10
|
+
import { appendUsage, copilotEventLog, cyan, dim, History, LEDGER_ENV, ledgerPath, META_SESSION_ENV, note, resolveTheme, } from "../host.js";
|
|
11
|
+
import { absorb, openLog, recordSession } from "../launch.js";
|
|
12
|
+
import { announce, prepare } from "../prepare.js";
|
|
13
|
+
import { listPrompts, loadPrompt } from "../prompts.js";
|
|
14
|
+
import { startVertexRelay } from "../relay.js";
|
|
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";
|
|
20
|
+
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}/`, '');
|
|
60
|
+
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
|
+
let chat;
|
|
69
|
+
const startedAt = new Date();
|
|
70
|
+
try {
|
|
71
|
+
chat = await openChat({
|
|
72
|
+
dir: project.dir,
|
|
73
|
+
model: wiring.env.COPILOT_MODEL ?? wiring.model,
|
|
74
|
+
provider: providerConfig(wiring.env, wiring.keyFile ? googleToken(wiring.keyFile) : undefined),
|
|
75
|
+
env,
|
|
76
|
+
sessionId: session,
|
|
77
|
+
resume: Boolean(resumed),
|
|
78
|
+
agent: values.agent,
|
|
79
|
+
effort: values.effort,
|
|
80
|
+
addDirs: values['add-dir']?.map((d) => resolve(opts.cwd, d)),
|
|
81
|
+
decide,
|
|
82
|
+
retries: opts.retries,
|
|
83
|
+
connect: opts.connect,
|
|
84
|
+
onRaw: (event) => {
|
|
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
|
+
},
|
|
115
|
+
});
|
|
116
|
+
const screen = opts.screen ?? (await import("./tui.js")).chatScreen;
|
|
117
|
+
await screen({
|
|
118
|
+
chat,
|
|
119
|
+
bridge,
|
|
120
|
+
history: new History(join(project.dir, '.tmp', 'logs', 'meta.history')),
|
|
121
|
+
theme: await resolveTheme(),
|
|
122
|
+
title: {
|
|
123
|
+
owner: prepared.owner,
|
|
124
|
+
model: wiring.model,
|
|
125
|
+
project: project.name,
|
|
126
|
+
resumed: Boolean(resumed),
|
|
127
|
+
},
|
|
128
|
+
status: () => (tally.calls > 0 ? tally.toString() : ''),
|
|
129
|
+
prompts: {
|
|
130
|
+
list: () => listPrompts(project.dir),
|
|
131
|
+
load: (name) => loadPrompt(project.dir, name),
|
|
132
|
+
},
|
|
133
|
+
first: opts.first,
|
|
134
|
+
onSend: (shown, prompt) => {
|
|
135
|
+
log.line('');
|
|
136
|
+
log.line(`--- you: ${shown.split('\n')[0]} ---`);
|
|
137
|
+
if (prompt !== shown) {
|
|
138
|
+
log.line(prompt);
|
|
139
|
+
}
|
|
140
|
+
},
|
|
141
|
+
});
|
|
142
|
+
}
|
|
143
|
+
finally {
|
|
144
|
+
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
|
+
}
|
|
166
|
+
if (tally.calls > 0) {
|
|
167
|
+
note(dim(`tokens ${tally.toString()}`));
|
|
168
|
+
}
|
|
169
|
+
note(dim(`carry on: zen meta chat ${project.name} --resume ${session}`));
|
|
170
|
+
note(cyan(log.path));
|
|
171
|
+
}
|
|
172
|
+
//# sourceMappingURL=run.js.map
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
import type { BearerTokenProvider, CopilotClientOptions, PermissionRequest, PermissionRequestResult, ProviderConfig, ResumeSessionConfig, SessionConfig, SessionEvent } from '@github/copilot-sdk';
|
|
2
|
+
import type { MetaCall } from '../tokens.ts';
|
|
3
|
+
export type ChatEvent =
|
|
4
|
+
/** a piece of the message being written, by its id */
|
|
5
|
+
{
|
|
6
|
+
type: 'delta';
|
|
7
|
+
id: string;
|
|
8
|
+
text: string;
|
|
9
|
+
}
|
|
10
|
+
/** a whole message; `tools` > 0 means it was said on the way to a tool call */
|
|
11
|
+
| {
|
|
12
|
+
type: 'message';
|
|
13
|
+
id: string;
|
|
14
|
+
text: string;
|
|
15
|
+
tools: number;
|
|
16
|
+
} | {
|
|
17
|
+
type: 'tool';
|
|
18
|
+
id: string;
|
|
19
|
+
name: string;
|
|
20
|
+
subject: string;
|
|
21
|
+
} | {
|
|
22
|
+
type: 'tool-done';
|
|
23
|
+
id: string;
|
|
24
|
+
ok: boolean;
|
|
25
|
+
error?: string;
|
|
26
|
+
} | {
|
|
27
|
+
type: 'usage';
|
|
28
|
+
call: MetaCall;
|
|
29
|
+
} | {
|
|
30
|
+
type: 'warn';
|
|
31
|
+
text: string;
|
|
32
|
+
} | {
|
|
33
|
+
type: 'error';
|
|
34
|
+
text: string;
|
|
35
|
+
} | {
|
|
36
|
+
type: 'retry';
|
|
37
|
+
reason: string;
|
|
38
|
+
waitMs: number;
|
|
39
|
+
attempt: number;
|
|
40
|
+
of: number;
|
|
41
|
+
};
|
|
42
|
+
export interface TurnResult {
|
|
43
|
+
aborted: boolean;
|
|
44
|
+
/** why the turn failed for good, when it did */
|
|
45
|
+
failed?: string;
|
|
46
|
+
}
|
|
47
|
+
export interface SessionLike {
|
|
48
|
+
readonly sessionId: string;
|
|
49
|
+
on(handler: (event: SessionEvent) => void): () => void;
|
|
50
|
+
send(options: {
|
|
51
|
+
prompt: string;
|
|
52
|
+
}): Promise<string>;
|
|
53
|
+
abort(): Promise<void>;
|
|
54
|
+
disconnect(): Promise<void>;
|
|
55
|
+
}
|
|
56
|
+
export interface ClientLike {
|
|
57
|
+
createSession(config: SessionConfig): Promise<SessionLike>;
|
|
58
|
+
resumeSession(id: string, config: ResumeSessionConfig): Promise<SessionLike>;
|
|
59
|
+
stop(): Promise<Error[]>;
|
|
60
|
+
}
|
|
61
|
+
export type Connect = (options: CopilotClientOptions) => ClientLike | Promise<ClientLike>;
|
|
62
|
+
/**
|
|
63
|
+
* `token`, when given, replaces the key command: the SDK calls back into this
|
|
64
|
+
* process before each request instead of the runtime starting `zen key token`.
|
|
65
|
+
*/
|
|
66
|
+
export declare function providerConfig(env: Record<string, string>, token?: BearerTokenProvider): ProviderConfig;
|
|
67
|
+
/**
|
|
68
|
+
* A service account's access token, minted in this process. The key command
|
|
69
|
+
* starts a `zen` and asks Google for a new token before every model call, so
|
|
70
|
+
* one network blip in a long session failed the turn. Here the library keeps
|
|
71
|
+
* the token until shortly before it expires, and a failed exchange is tried
|
|
72
|
+
* again before the call is given up on.
|
|
73
|
+
*/
|
|
74
|
+
export declare function googleToken(keyFile: string, opts?: {
|
|
75
|
+
attempts?: number;
|
|
76
|
+
delayMs?: number;
|
|
77
|
+
}): BearerTokenProvider;
|
|
78
|
+
/** Everything else in the wiring's environment: what the provider object does not carry. */
|
|
79
|
+
export declare function runtimeEnv(env: Record<string, string>): Record<string, string>;
|
|
80
|
+
/** One model call, in the shape the ledger and the tally already take from the span file. */
|
|
81
|
+
export declare function toCall(event: SessionEvent, session: string): MetaCall | undefined;
|
|
82
|
+
/** The SDK's event as the screen's, or nothing. A sub-agent's own steps stay in the log. */
|
|
83
|
+
export declare function narrate(event: SessionEvent): ChatEvent | undefined;
|
|
84
|
+
export interface ChatOptions {
|
|
85
|
+
dir: string;
|
|
86
|
+
/** the model's id, as `SessionConfig.model` */
|
|
87
|
+
model: string;
|
|
88
|
+
provider: ProviderConfig;
|
|
89
|
+
/** the runtime's whole environment */
|
|
90
|
+
env: Record<string, string>;
|
|
91
|
+
sessionId: string;
|
|
92
|
+
/** carry on `sessionId` rather than start it */
|
|
93
|
+
resume: boolean;
|
|
94
|
+
agent?: string;
|
|
95
|
+
effort?: string;
|
|
96
|
+
addDirs?: readonly string[];
|
|
97
|
+
decide: (request: PermissionRequest) => Promise<PermissionRequestResult>;
|
|
98
|
+
onEvent: (event: ChatEvent) => void;
|
|
99
|
+
/** every SDK event, before it is narrated: the log and `--events` file */
|
|
100
|
+
onRaw?: (event: SessionEvent) => void;
|
|
101
|
+
/** resumes after a failure time can cure, per turn */
|
|
102
|
+
retries: number;
|
|
103
|
+
delayMs?: (attempt: number) => number;
|
|
104
|
+
connect?: Connect;
|
|
105
|
+
}
|
|
106
|
+
export interface Chat {
|
|
107
|
+
readonly sessionId: string;
|
|
108
|
+
/** resolves when the turn has settled: answered, failed for good, or stopped */
|
|
109
|
+
send(prompt: string): Promise<TurnResult>;
|
|
110
|
+
/** stops the turn in flight, or the wait before a resume */
|
|
111
|
+
abort(): Promise<void>;
|
|
112
|
+
close(): Promise<void>;
|
|
113
|
+
}
|
|
114
|
+
/** The reasoning levels `--effort` takes. */
|
|
115
|
+
export declare const EFFORTS: readonly string[];
|
|
116
|
+
export declare function openChat(opts: ChatOptions): Promise<Chat>;
|
|
117
|
+
//# sourceMappingURL=session.d.ts.map
|