@agentbelt/core 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +176 -0
- package/dist/audit.d.ts +19 -0
- package/dist/audit.js +40 -0
- package/dist/audit.js.map +1 -0
- package/dist/broker.d.ts +16 -0
- package/dist/broker.js +153 -0
- package/dist/broker.js.map +1 -0
- package/dist/context.d.ts +43 -0
- package/dist/context.js +88 -0
- package/dist/context.js.map +1 -0
- package/dist/env.d.ts +5 -0
- package/dist/env.js +48 -0
- package/dist/env.js.map +1 -0
- package/dist/external.d.ts +39 -0
- package/dist/external.js +237 -0
- package/dist/external.js.map +1 -0
- package/dist/index.d.ts +15 -0
- package/dist/index.js +16 -0
- package/dist/index.js.map +1 -0
- package/dist/model.d.ts +89 -0
- package/dist/model.js +158 -0
- package/dist/model.js.map +1 -0
- package/dist/models.d.ts +51 -0
- package/dist/models.js +180 -0
- package/dist/models.js.map +1 -0
- package/dist/plugin-host.d.ts +1 -0
- package/dist/plugin-host.js +83 -0
- package/dist/plugin-host.js.map +1 -0
- package/dist/plugin.d.ts +88 -0
- package/dist/plugin.js +26 -0
- package/dist/plugin.js.map +1 -0
- package/dist/policy.d.ts +28 -0
- package/dist/policy.js +416 -0
- package/dist/policy.js.map +1 -0
- package/dist/project-state.d.ts +107 -0
- package/dist/project-state.js +366 -0
- package/dist/project-state.js.map +1 -0
- package/dist/redact.d.ts +1 -0
- package/dist/redact.js +23 -0
- package/dist/redact.js.map +1 -0
- package/dist/sandbox.d.ts +16 -0
- package/dist/sandbox.js +203 -0
- package/dist/sandbox.js.map +1 -0
- package/dist/scheduler.d.ts +80 -0
- package/dist/scheduler.js +273 -0
- package/dist/scheduler.js.map +1 -0
- package/dist/server.d.ts +40 -0
- package/dist/server.js +280 -0
- package/dist/server.js.map +1 -0
- package/dist/status.d.ts +8 -0
- package/dist/status.js +99 -0
- package/dist/status.js.map +1 -0
- package/package.json +29 -4
- package/README.md +0 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to the Licensor for inclusion in the Work by the copyright
|
|
52
|
+
owner or by an individual or Legal Entity authorized to submit on behalf
|
|
53
|
+
of the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
package/dist/audit.d.ts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
export type Isolation = 'policy-only' | 'os-sandbox' | 'none';
|
|
2
|
+
export interface AuditEntry {
|
|
3
|
+
ts: string;
|
|
4
|
+
kind: 'tool' | 'exec' | 'file' | 'denied' | 'model';
|
|
5
|
+
client?: string;
|
|
6
|
+
plugin: string;
|
|
7
|
+
tool?: string;
|
|
8
|
+
detail: string;
|
|
9
|
+
ok: boolean;
|
|
10
|
+
isolation?: Isolation;
|
|
11
|
+
ms?: number;
|
|
12
|
+
}
|
|
13
|
+
export declare class AuditLog {
|
|
14
|
+
private readonly client;
|
|
15
|
+
readonly file: string;
|
|
16
|
+
constructor(workspace: string, client?: () => string);
|
|
17
|
+
write(entry: Omit<AuditEntry, 'ts' | 'client'>): void;
|
|
18
|
+
tail(n?: number): AuditEntry[];
|
|
19
|
+
}
|
package/dist/audit.js
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
// Audit log: one JSON line per tool call, command and denial, in <workspace>/.agentbelt/audit.jsonl.
|
|
2
|
+
// Arguments and output are redacted and truncated. `isolation` says honestly how an action ran:
|
|
3
|
+
// "policy-only" means it passed the permission and command checks but ran without an OS sandbox.
|
|
4
|
+
import { appendFileSync, existsSync, mkdirSync, readFileSync } from 'node:fs';
|
|
5
|
+
import { join } from 'node:path';
|
|
6
|
+
import { redact } from './redact.js';
|
|
7
|
+
export class AuditLog {
|
|
8
|
+
client;
|
|
9
|
+
file;
|
|
10
|
+
constructor(workspace, client = () => 'unknown') {
|
|
11
|
+
this.client = client;
|
|
12
|
+
this.file = join(workspace, '.agentbelt', 'audit.jsonl');
|
|
13
|
+
}
|
|
14
|
+
write(entry) {
|
|
15
|
+
const line = {
|
|
16
|
+
ts: new Date().toISOString(),
|
|
17
|
+
client: this.client(),
|
|
18
|
+
...entry,
|
|
19
|
+
detail: redact(entry.detail).slice(0, 1000),
|
|
20
|
+
};
|
|
21
|
+
try {
|
|
22
|
+
mkdirSync(join(this.file, '..'), { recursive: true });
|
|
23
|
+
appendFileSync(this.file, `${JSON.stringify(line)}\n`);
|
|
24
|
+
}
|
|
25
|
+
catch {
|
|
26
|
+
// A read-only workspace must not break the tool call; the call itself still reports.
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
tail(n = 50) {
|
|
30
|
+
if (!existsSync(this.file))
|
|
31
|
+
return [];
|
|
32
|
+
return readFileSync(this.file, 'utf8')
|
|
33
|
+
.trim()
|
|
34
|
+
.split('\n')
|
|
35
|
+
.filter(Boolean)
|
|
36
|
+
.slice(-n)
|
|
37
|
+
.map((l) => JSON.parse(l));
|
|
38
|
+
}
|
|
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C,IAAI,CAAC,IAAI,CAAC;YAAE,OAAO,EAAE,CAAC;QACtC,OAAO,YAAY,CAAC,IAAI,CAAC,IAAI,EAAE,MAAM,CAAC;aACnC,IAAI,EAAE;aACN,KAAK,CAAC,IAAI,CAAC;aACX,MAAM,CAAC,OAAO,CAAC;aACf,KAAK,CAAC,CAAC,CAAC,CAAC;aACT,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAe,CAAC,CAAC;IAC7C,CAAC;CACF"}
|
package/dist/broker.d.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { AuditLog, Isolation } from './audit.js';
|
|
2
|
+
import type { ModelClient } from './model.js';
|
|
3
|
+
import type { Plugin, PluginContext, ToolResult } from './plugin.js';
|
|
4
|
+
export declare class BrokerDenied extends Error {
|
|
5
|
+
}
|
|
6
|
+
export interface BrokerDeps {
|
|
7
|
+
workspace: string;
|
|
8
|
+
audit: AuditLog;
|
|
9
|
+
/** Runs another plugin's tool by its client-facing id. */
|
|
10
|
+
callTool: (id: string, args: Record<string, unknown>, via: string) => Promise<ToolResult>;
|
|
11
|
+
listTools: () => string[];
|
|
12
|
+
isolation?: Isolation;
|
|
13
|
+
/** The user's configured model, if any (optional coding agent). */
|
|
14
|
+
model?: ModelClient | null;
|
|
15
|
+
}
|
|
16
|
+
export declare function createContext(plugin: Plugin, deps: BrokerDeps): PluginContext;
|
package/dist/broker.js
ADDED
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
// The broker builds each plugin's context. Every action is checked when it happens:
|
|
2
|
+
// exec → the program must be in the plugin's permissions AND pass the core command policy
|
|
3
|
+
// read/write → the permission must be granted AND the path must stay inside the workspace
|
|
4
|
+
// (no .git, no .agentbelt, no symlink escapes)
|
|
5
|
+
// callTool → the permission must be granted; the called tool runs with its own plugin's rules
|
|
6
|
+
// Denials throw a BrokerDenied error (shown to the model as a tool error) and are audited.
|
|
7
|
+
import { mkdirSync, readdirSync, readFileSync, realpathSync, renameSync, writeFileSync, } from 'node:fs';
|
|
8
|
+
import { dirname, join, resolve } from 'node:path';
|
|
9
|
+
import { childEnv } from './env.js';
|
|
10
|
+
import { checkCommand, checkPath, DEFAULT_COMMAND_TIMEOUT_MS, runCommand } from './policy.js';
|
|
11
|
+
import { redact } from './redact.js';
|
|
12
|
+
export class BrokerDenied extends Error {
|
|
13
|
+
}
|
|
14
|
+
const KEY = /^[A-Za-z0-9][A-Za-z0-9_.-]{0,120}$/;
|
|
15
|
+
export function createContext(plugin, deps) {
|
|
16
|
+
const { audit } = deps;
|
|
17
|
+
// Path checks compare real (symlink-resolved) paths, so the workspace must be one too.
|
|
18
|
+
const workspace = realpathSync(deps.workspace);
|
|
19
|
+
const isolation = deps.isolation ?? 'policy-only';
|
|
20
|
+
const perms = plugin.permissions;
|
|
21
|
+
const deny = (detail) => {
|
|
22
|
+
audit.write({ kind: 'denied', plugin: plugin.name, detail, ok: false });
|
|
23
|
+
throw new BrokerDenied(`denied: ${detail}`);
|
|
24
|
+
};
|
|
25
|
+
const filePath = (path, write) => {
|
|
26
|
+
if (write ? !perms.write : !perms.read)
|
|
27
|
+
deny(`${plugin.name} has no ${write ? 'write' : 'read'} permission (${path})`);
|
|
28
|
+
const reason = checkPath(workspace, workspace, path);
|
|
29
|
+
if (reason)
|
|
30
|
+
deny(reason);
|
|
31
|
+
return resolve(workspace, path);
|
|
32
|
+
};
|
|
33
|
+
const stateDir = join(workspace, '.agentbelt', 'state', plugin.name);
|
|
34
|
+
const stateFile = (key) => {
|
|
35
|
+
if (!KEY.test(key))
|
|
36
|
+
throw new Error(`invalid state key "${key}"`);
|
|
37
|
+
return join(stateDir, `${key}.json`);
|
|
38
|
+
};
|
|
39
|
+
return {
|
|
40
|
+
workspace,
|
|
41
|
+
async exec(program, args, options = {}) {
|
|
42
|
+
if (!(perms.exec ?? []).includes(program))
|
|
43
|
+
deny(`${plugin.name} may not run ${program} (allowed: ${(perms.exec ?? []).join(', ') || 'none'})`);
|
|
44
|
+
const cwd = resolve(workspace, options.cwd ?? '.');
|
|
45
|
+
const reason = checkCommand(workspace, cwd, program, args);
|
|
46
|
+
if (reason)
|
|
47
|
+
deny(`${program} ${args.join(' ')}: ${reason}`);
|
|
48
|
+
const result = await runCommand(program, args, cwd, options.timeoutMs ?? DEFAULT_COMMAND_TIMEOUT_MS, childEnv());
|
|
49
|
+
audit.write({
|
|
50
|
+
kind: 'exec',
|
|
51
|
+
plugin: plugin.name,
|
|
52
|
+
detail: `${program} ${args.join(' ')} → exit ${result.exitCode}`,
|
|
53
|
+
ok: result.exitCode === 0,
|
|
54
|
+
isolation,
|
|
55
|
+
ms: result.durationMs,
|
|
56
|
+
});
|
|
57
|
+
return { ...result, stdout: redact(result.stdout), stderr: redact(result.stderr) };
|
|
58
|
+
},
|
|
59
|
+
async readFile(path) {
|
|
60
|
+
const full = filePath(path, false);
|
|
61
|
+
return readFileSync(full, 'utf8');
|
|
62
|
+
},
|
|
63
|
+
async listFiles(dir) {
|
|
64
|
+
const full = filePath(dir, false);
|
|
65
|
+
try {
|
|
66
|
+
return readdirSync(full).sort();
|
|
67
|
+
}
|
|
68
|
+
catch {
|
|
69
|
+
return [];
|
|
70
|
+
}
|
|
71
|
+
},
|
|
72
|
+
async writeFile(path, text) {
|
|
73
|
+
const full = filePath(path, true);
|
|
74
|
+
mkdirSync(dirname(full), { recursive: true });
|
|
75
|
+
const tmp = `${full}.${process.pid}.tmp`;
|
|
76
|
+
writeFileSync(tmp, text);
|
|
77
|
+
renameSync(tmp, full);
|
|
78
|
+
audit.write({
|
|
79
|
+
kind: 'file',
|
|
80
|
+
plugin: plugin.name,
|
|
81
|
+
detail: `wrote ${path}`,
|
|
82
|
+
ok: true,
|
|
83
|
+
isolation,
|
|
84
|
+
});
|
|
85
|
+
},
|
|
86
|
+
state: {
|
|
87
|
+
get(key) {
|
|
88
|
+
try {
|
|
89
|
+
return JSON.parse(readFileSync(stateFile(key), 'utf8'));
|
|
90
|
+
}
|
|
91
|
+
catch {
|
|
92
|
+
return undefined;
|
|
93
|
+
}
|
|
94
|
+
},
|
|
95
|
+
set(key, value) {
|
|
96
|
+
const file = stateFile(key);
|
|
97
|
+
mkdirSync(stateDir, { recursive: true });
|
|
98
|
+
writeFileSync(`${file}.tmp`, JSON.stringify(value, null, 2));
|
|
99
|
+
renameSync(`${file}.tmp`, file);
|
|
100
|
+
},
|
|
101
|
+
list() {
|
|
102
|
+
try {
|
|
103
|
+
return readdirSync(stateDir)
|
|
104
|
+
.filter((f) => f.endsWith('.json'))
|
|
105
|
+
.map((f) => f.slice(0, -5))
|
|
106
|
+
.sort();
|
|
107
|
+
}
|
|
108
|
+
catch {
|
|
109
|
+
return [];
|
|
110
|
+
}
|
|
111
|
+
},
|
|
112
|
+
},
|
|
113
|
+
async callTool(id, args) {
|
|
114
|
+
if (!perms.callTools)
|
|
115
|
+
deny(`${plugin.name} may not call other tools (${id})`);
|
|
116
|
+
return deps.callTool(id, args, plugin.name);
|
|
117
|
+
},
|
|
118
|
+
listTools: deps.listTools,
|
|
119
|
+
model: deps.model
|
|
120
|
+
? {
|
|
121
|
+
name: deps.model.model,
|
|
122
|
+
endpoint: deps.model.endpoint,
|
|
123
|
+
async chat(request) {
|
|
124
|
+
if (!perms.model)
|
|
125
|
+
deny(`${plugin.name} has no model permission`);
|
|
126
|
+
const started = Date.now();
|
|
127
|
+
try {
|
|
128
|
+
const r = await deps.model.chat(request);
|
|
129
|
+
audit.write({
|
|
130
|
+
kind: 'model',
|
|
131
|
+
plugin: plugin.name,
|
|
132
|
+
detail: `${deps.model.model} @ ${deps.model.endpoint}: ${r.toolCalls.length} tool call(s), finish ${r.finishReason}, tokens ${r.usage.inputTokens ?? '?'}/${r.usage.outputTokens ?? '?'}`,
|
|
133
|
+
ok: true,
|
|
134
|
+
ms: Date.now() - started,
|
|
135
|
+
});
|
|
136
|
+
return r;
|
|
137
|
+
}
|
|
138
|
+
catch (error) {
|
|
139
|
+
audit.write({
|
|
140
|
+
kind: 'model',
|
|
141
|
+
plugin: plugin.name,
|
|
142
|
+
detail: `${deps.model.model} @ ${deps.model.endpoint}: ${redact(error.message)}`,
|
|
143
|
+
ok: false,
|
|
144
|
+
ms: Date.now() - started,
|
|
145
|
+
});
|
|
146
|
+
throw error;
|
|
147
|
+
}
|
|
148
|
+
},
|
|
149
|
+
}
|
|
150
|
+
: null,
|
|
151
|
+
};
|
|
152
|
+
}
|
|
153
|
+
//# sourceMappingURL=broker.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"broker.js","sourceRoot":"","sources":["../src/broker.ts"],"names":[],"mappings":"AAAA,oFAAoF;AACpF,kGAAkG;AAClG,4FAA4F;AAC5F,8DAA8D;AAC9D,kGAAkG;AAClG,2FAA2F;AAC3F,OAAO,EACL,SAAS,EACT,WAAW,EACX,YAAY,EACZ,YAAY,EACZ,UAAU,EACV,aAAa,GACd,MAAM,SAAS,CAAC;AACjB,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AAEnD,OAAO,EAAE,QAAQ,EAAE,MAAM,UAAU,CAAC;AAGpC,OAAO,EAAE,YAAY,EAAE,SAAS,EAAE,0BAA0B,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAC9F,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAErC,MAAM,OAAO,YAAa,SAAQ,KAAK;CAAG;AAE1C,MAAM,GAAG,GAAG,oCAAoC,CAAC;AAajD,MAAM,UAAU,aAAa,CAAC,MAAc,EAAE,IAAgB;IAC5D,MAAM,EAAE,KAAK,EAAE,GAAG,IAAI,CAAC;IACvB,uFAAuF;IACvF,MAAM,SAAS,GAAG,YAAY,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC;IAC/C,MAAM,SAAS,GAAG,IAAI,CAAC,SAAS,IAAI,aAAa,CAAC;IAClD,MAAM,KAAK,GAAG,MAAM,CAAC,WAAW,CAAC;IACjC,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
|
+
client: string;
|
|
4
|
+
model: string | null;
|
|
5
|
+
limit: number | null;
|
|
6
|
+
used: number | null;
|
|
7
|
+
/** Where the numbers came from, e.g. "claude-code statusline (context_window)". */
|
|
8
|
+
source: string;
|
|
9
|
+
at: string;
|
|
10
|
+
}
|
|
11
|
+
export declare function contextView(c: ContextStatus | null): {
|
|
12
|
+
client: string;
|
|
13
|
+
model: string;
|
|
14
|
+
limit: string | number;
|
|
15
|
+
used: string | number;
|
|
16
|
+
remaining: string | number;
|
|
17
|
+
remainingPercent: string;
|
|
18
|
+
warning: boolean;
|
|
19
|
+
source: string;
|
|
20
|
+
at: string | null;
|
|
21
|
+
};
|
|
22
|
+
export declare function readContext(root: string): ContextStatus | null;
|
|
23
|
+
/**
|
|
24
|
+
* Records reported usage. Returns the view; when it crosses the warning threshold, saves a
|
|
25
|
+
* checkpoint and refreshes HANDOFF.md so a new session or compaction loses nothing.
|
|
26
|
+
*/
|
|
27
|
+
export declare function recordContext(root: string, c: Omit<ContextStatus, 'at'>): {
|
|
28
|
+
client: string;
|
|
29
|
+
model: string;
|
|
30
|
+
limit: string | number;
|
|
31
|
+
used: string | number;
|
|
32
|
+
remaining: string | number;
|
|
33
|
+
remainingPercent: string;
|
|
34
|
+
warning: boolean;
|
|
35
|
+
source: string;
|
|
36
|
+
at: string | null;
|
|
37
|
+
};
|
|
38
|
+
/**
|
|
39
|
+
* Reads the JSON Claude Code passes to a status-line command. Uses only the context fields if the
|
|
40
|
+
* installed Claude Code version provides them; otherwise the usage stays UNKNOWN.
|
|
41
|
+
*/
|
|
42
|
+
export declare function fromClaudeStatusline(json: unknown): Omit<ContextStatus, 'at'>;
|
|
43
|
+
export declare const contextFileExists: (root: string) => boolean;
|
package/dist/context.js
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
// Context-window status, recorded only from data a client or provider actually reports.
|
|
2
|
+
// Nothing here estimates: if a value isn't reported it is shown as UNKNOWN. Context window
|
|
3
|
+
// (tokens in the current conversation) is a different thing from rate limits or account quotas,
|
|
4
|
+
// and this file only tracks the context window.
|
|
5
|
+
// At 80% used (20% left) a warning is raised and the project state is checkpointed with a handoff.
|
|
6
|
+
import { existsSync, mkdirSync, readFileSync, renameSync, writeFileSync } from 'node:fs';
|
|
7
|
+
import { join } from 'node:path';
|
|
8
|
+
import { clean, ProjectState } from './project-state.js';
|
|
9
|
+
export const WARN_AT = 0.8;
|
|
10
|
+
export function contextView(c) {
|
|
11
|
+
const known = c && c.limit && c.used !== null && c.limit > 0;
|
|
12
|
+
const remaining = known ? c.limit - c.used : null;
|
|
13
|
+
const ratio = known ? c.used / c.limit : null;
|
|
14
|
+
return {
|
|
15
|
+
client: c?.client ?? 'UNKNOWN',
|
|
16
|
+
model: c?.model ?? 'UNKNOWN',
|
|
17
|
+
limit: c?.limit ?? 'UNKNOWN',
|
|
18
|
+
used: c?.used ?? 'UNKNOWN',
|
|
19
|
+
remaining: remaining ?? 'UNKNOWN',
|
|
20
|
+
remainingPercent: ratio === null ? 'UNKNOWN' : `${Math.round((1 - ratio) * 100)}%`,
|
|
21
|
+
warning: ratio !== null && ratio >= WARN_AT,
|
|
22
|
+
source: c?.source ?? 'no client has reported usage',
|
|
23
|
+
at: c?.at ?? null,
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
export function readContext(root) {
|
|
27
|
+
try {
|
|
28
|
+
return JSON.parse(readFileSync(join(root, '.agentbelt', 'CONTEXT_STATUS.json'), 'utf8'));
|
|
29
|
+
}
|
|
30
|
+
catch {
|
|
31
|
+
return null;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Records reported usage. Returns the view; when it crosses the warning threshold, saves a
|
|
36
|
+
* checkpoint and refreshes HANDOFF.md so a new session or compaction loses nothing.
|
|
37
|
+
*/
|
|
38
|
+
export function recordContext(root, c) {
|
|
39
|
+
const entry = {
|
|
40
|
+
...c,
|
|
41
|
+
client: clean(c.client),
|
|
42
|
+
model: c.model && clean(c.model),
|
|
43
|
+
at: new Date().toISOString(),
|
|
44
|
+
};
|
|
45
|
+
for (const [k, v] of Object.entries({ limit: entry.limit, used: entry.used }))
|
|
46
|
+
if (v !== null && (!Number.isFinite(v) || v < 0))
|
|
47
|
+
throw new Error(`invalid ${k}: ${v}`);
|
|
48
|
+
const dir = join(root, '.agentbelt');
|
|
49
|
+
mkdirSync(dir, { recursive: true });
|
|
50
|
+
const file = join(dir, 'CONTEXT_STATUS.json');
|
|
51
|
+
const before = readContext(root);
|
|
52
|
+
writeFileSync(`${file}.tmp`, `${JSON.stringify(entry, null, 2)}\n`);
|
|
53
|
+
renameSync(`${file}.tmp`, file);
|
|
54
|
+
const view = contextView(entry);
|
|
55
|
+
const wasWarning = before ? contextView(before).warning && before.client === entry.client : false;
|
|
56
|
+
if (view.warning && !wasWarning) {
|
|
57
|
+
const state = new ProjectState(root);
|
|
58
|
+
if (state.exists())
|
|
59
|
+
state.checkpoint(`context ${view.remainingPercent} left in ${entry.client} — saved state and handoff; compact or start a new session, then run \`agentbelt resume\``, undefined, entry.client);
|
|
60
|
+
}
|
|
61
|
+
return view;
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Reads the JSON Claude Code passes to a status-line command. Uses only the context fields if the
|
|
65
|
+
* installed Claude Code version provides them; otherwise the usage stays UNKNOWN.
|
|
66
|
+
*/
|
|
67
|
+
export function fromClaudeStatusline(json) {
|
|
68
|
+
const j = (json ?? {});
|
|
69
|
+
const cw = j.context_window ?? {};
|
|
70
|
+
const limit = num(cw.context_window_size);
|
|
71
|
+
let used = num(cw.total_input_tokens ?? cw.used_tokens);
|
|
72
|
+
if (used === null && limit !== null && num(cw.used_percentage) !== null)
|
|
73
|
+
used = Math.round((num(cw.used_percentage) / 100) * limit);
|
|
74
|
+
return {
|
|
75
|
+
client: 'claude-code',
|
|
76
|
+
model: j.model?.id ?? j.model?.display_name ?? null,
|
|
77
|
+
limit,
|
|
78
|
+
used,
|
|
79
|
+
source: limit !== null && used !== null
|
|
80
|
+
? 'claude-code statusline (context_window)'
|
|
81
|
+
: 'claude-code statusline (no context data in this version)',
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
function num(v) {
|
|
85
|
+
return typeof v === 'number' && Number.isFinite(v) ? v : null;
|
|
86
|
+
}
|
|
87
|
+
export const contextFileExists = (root) => existsSync(join(root, '.agentbelt', 'CONTEXT_STATUS.json'));
|
|
88
|
+
//# sourceMappingURL=context.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"context.js","sourceRoot":"","sources":["../src/context.ts"],"names":[],"mappings":"AAAA,wFAAwF;AACxF,2FAA2F;AAC3F,gGAAgG;AAChG,gDAAgD;AAChD,mGAAmG;AACnG,OAAO,EAAE,UAAU,EAAE,SAAS,EAAE,YAAY,EAAE,UAAU,EAAE,aAAa,EAAE,MAAM,SAAS,CAAC;AACzF,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AACjC,OAAO,EAAE,KAAK,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAEzD,MAAM,CAAC,MAAM,OAAO,GAAG,GAAG,CAAC;AAY3B,MAAM,UAAU,WAAW,CAAC,CAAuB;IACjD,MAAM,KAAK,GAAG,CAAC,IAAI,CAAC,CAAC,KAAK,IAAI,CAAC,CAAC,IAAI,KAAK,IAAI,IAAI,CAAC,CAAC,KAAK,GAAG,CAAC,CAAC;IAC7D,MAAM,SAAS,GAAG,KAAK,CAAC,CAAC,CAAC,CAAE,CAAC,KAAM,GAAG,CAAE,CAAC,IAAK,CAAC,CAAC,CAAC,IAAI,CAAC;IACtD,MAAM,KAAK,GAAG,KAAK,CAAC,CAAC,CAAC,CAAE,CAAC,IAAK,GAAG,CAAE,CAAC,KAAM,CAAC,CAAC,CAAC,IAAI,CAAC;IAClD,OAAO;QACL,MAAM,EAAE,CAAC,EAAE,MAAM,IAAI,SAAS;QAC9B,KAAK,EAAE,CAAC,EAAE,KAAK,IAAI,SAAS;QAC5B,K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
+
* into, then the fixed settings. NODE_OPTIONS and similar injection points are never passed.
|
|
4
|
+
*/
|
|
5
|
+
export declare function childEnv(source?: NodeJS.ProcessEnv, extra?: readonly string[]): NodeJS.ProcessEnv;
|
package/dist/env.js
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
// The environment a child command sees. Only variables that tools need to work (paths, locale,
|
|
2
|
+
// toolchain locations, git identity) pass through; tokens and keys in the user's shell do not.
|
|
3
|
+
const PASSTHROUGH = [
|
|
4
|
+
'PATH',
|
|
5
|
+
'HOME',
|
|
6
|
+
'USER',
|
|
7
|
+
'LOGNAME',
|
|
8
|
+
'SHELL',
|
|
9
|
+
'TMPDIR',
|
|
10
|
+
'LANG',
|
|
11
|
+
'LC_ALL',
|
|
12
|
+
'LC_CTYPE',
|
|
13
|
+
'TERM',
|
|
14
|
+
'TZ',
|
|
15
|
+
'DEVELOPER_DIR',
|
|
16
|
+
'SDKROOT',
|
|
17
|
+
'XDG_CONFIG_HOME',
|
|
18
|
+
'XDG_CACHE_HOME',
|
|
19
|
+
'JAVA_HOME',
|
|
20
|
+
'GOPATH',
|
|
21
|
+
'CARGO_HOME',
|
|
22
|
+
'RUSTUP_HOME',
|
|
23
|
+
'GIT_AUTHOR_NAME',
|
|
24
|
+
'GIT_AUTHOR_EMAIL',
|
|
25
|
+
'GIT_COMMITTER_NAME',
|
|
26
|
+
'GIT_COMMITTER_EMAIL',
|
|
27
|
+
];
|
|
28
|
+
/** Fixed settings so nothing waits for input or opens an editor or pager. */
|
|
29
|
+
const FIXED = {
|
|
30
|
+
CI: '1',
|
|
31
|
+
GIT_EDITOR: 'true',
|
|
32
|
+
GIT_PAGER: 'cat',
|
|
33
|
+
PAGER: 'cat',
|
|
34
|
+
GIT_TERMINAL_PROMPT: '0',
|
|
35
|
+
NO_COLOR: '1',
|
|
36
|
+
};
|
|
37
|
+
/**
|
|
38
|
+
* Builds a child environment from `source`: the allowlist plus `extra` names the caller opted
|
|
39
|
+
* into, then the fixed settings. NODE_OPTIONS and similar injection points are never passed.
|
|
40
|
+
*/
|
|
41
|
+
export function childEnv(source = process.env, extra = []) {
|
|
42
|
+
const env = {};
|
|
43
|
+
for (const name of [...PASSTHROUGH, ...extra])
|
|
44
|
+
if (source[name] !== undefined)
|
|
45
|
+
env[name] = source[name];
|
|
46
|
+
return { ...env, ...FIXED };
|
|
47
|
+
}
|
|
48
|
+
//# sourceMappingURL=env.js.map
|
package/dist/env.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"env.js","sourceRoot":"","sources":["../src/env.ts"],"names":[],"mappings":"AAAA,+FAA+F;AAC/F,+FAA+F;AAC/F,MAAM,WAAW,GAAG;IAClB,MAAM;IACN,MAAM;IACN,MAAM;IACN,SAAS;IACT,OAAO;IACP,QAAQ;IACR,MAAM;IACN,QAAQ;IACR,UAAU;IACV,MAAM;IACN,IAAI;IACJ,eAAe;IACf,SAAS;IACT,iBAAiB;IACjB,gBAAgB;IAChB,WAAW;IACX,QAAQ;IACR,YAAY;IACZ,aAAa;IACb,iBAAiB;IACjB,kBAAkB;IAClB,oBAAoB;IACpB,qBAAqB;CACtB,CAAC;AAEF,6EAA6E;AAC7E,MAAM,KAAK,GAAG;IACZ,EAAE,EAAE,GAAG;IACP,UAAU,EAAE,MAAM;IAClB,SAAS,EAAE,KAAK;IAChB,KAAK,EAAE,KAAK;IACZ,mBAAmB,EAAE,GAAG;IACxB,QAAQ,EAAE,GAAG;CACd,CAAC;AAEF;;;GAGG;AACH,MAAM,UAAU,QAAQ,CACtB,SAA4B,OAAO,CAAC,GAAG,EACvC,QAA2B,EAAE;IAE7B,MAAM,GAAG,GAAsB,EAAE,CAAC;IAClC,KAAK,MAAM,IAAI,IAAI,CAAC,GAAG,WAAW,EAAE,GAAG,KAAK,CAAC;QAC3C,IAAI,MAAM,CAAC,IAAI,CAAC,KAAK,SAAS;YAAE,GAAG,CAAC,IAAI,CAAC,GAAG,MAAM,CAAC,IAAI,CAAC,CAAC;IAC3D,OAAO,EAAE,GAAG,GAAG,EAAE,GAAG,KAAK,EAAE,CAAC;AAC9B,CAAC"}
|