@byokit/openclaw 0.3.5 → 0.4.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/CHANGELOG.md +19 -0
- package/README.md +24 -2
- package/dist/device.d.ts +7 -2
- package/dist/device.js +5 -4
- package/dist/engine.js +20 -10
- package/dist/index.d.ts +1 -0
- package/dist/keys.d.ts +20 -0
- package/dist/keys.js +73 -0
- package/dist/kit.d.ts +14 -2
- package/dist/kit.js +37 -4
- package/dist/link.js +7 -5
- package/dist/members.d.ts +2 -0
- package/dist/members.js +3 -1
- package/dist/routes.json +271 -58
- package/dist/runs.d.ts +1 -1
- package/dist/runs.js +5 -4
- package/dist/testing/fake-gateway.js +33 -1
- package/dist/types.d.ts +10 -0
- package/dist/words.json +8 -1
- package/package.json +5 -5
- package/plugin/index.js +2 -0
- package/plugin/keys.js +101 -0
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,25 @@
|
|
|
2
2
|
|
|
3
3
|
## Unreleased
|
|
4
4
|
|
|
5
|
+
## 0.4.0 (2026-10-01)
|
|
6
|
+
|
|
7
|
+
- Dependency update: pins @byokit/ui-core 0.6.0.
|
|
8
|
+
- Dependency update: pins @byokit/reach 0.6.0.
|
|
9
|
+
|
|
10
|
+
- Key readiness waits through the engine's retryable restart refusal after activation; missing keys still fail without another account.
|
|
11
|
+
- API key (billed per use): `addKey` verifies and stores an explicitly entered key in a separate member-owned agent, with copying disabled and no automatic subscription fallback. `run({ auth: 'apiKey' })` selects it explicitly with separate history; key-entry labels, errors and route revision dates are included.
|
|
12
|
+
- Offer subscription sign-ins, including pasted tokens and native Claude Code, by default and allow their provider plugins. API key (billed per use) routes remain app opt-in; proxies, compatibility aliases and local routes stay off.
|
|
13
|
+
|
|
14
|
+
## 0.3.6 (2026-10-01)
|
|
15
|
+
|
|
16
|
+
- FIX: Live orphan gateways recover after the engine rewrites its process title. Recovery verifies the recorded launch pid and process start time with the executable and isolated store paths; ambiguous ownership preserves the saved sign-in state.
|
|
17
|
+
|
|
18
|
+
- Dependency update: pins @byokit/ui-core 0.5.0.
|
|
19
|
+
- Dependency update: pins @byokit/reach 0.5.0.
|
|
20
|
+
- Dependency update: pins @byokit/relay 0.5.0.
|
|
21
|
+
- Dependency update: pins @byokit/link 0.7.0.
|
|
22
|
+
- SECURITY: (from @byokit/link 0.7.0) `Host.shortCode()` adds a machine-key commitment to typed pairing; `pairWithCode()` verifies it before disclosing device identity or asking for approval. Legacy codes remain compatible; use the full new code when relay lookup is untrusted.
|
|
23
|
+
|
|
5
24
|
## 0.3.5 (2026-10-01)
|
|
6
25
|
|
|
7
26
|
|
package/README.md
CHANGED
|
@@ -69,6 +69,23 @@ provider or model. If the member isn't signed in to it, the run ends `{ ok: fals
|
|
|
69
69
|
engine is never called. Leave `model` out to keep the engine's own choice. A specific sign-in (`@profile`) can't be
|
|
70
70
|
picked per run: the pinned engine may still switch to another sign-in for the same provider.
|
|
71
71
|
|
|
72
|
+
|
|
73
|
+
For an owner who explicitly offers keys, `kit.addKey('ana', { authChoice: 'openai-api-key', apiKey })` returns
|
|
74
|
+
`ok`, `invalid` or `not_included`. Label the option **API key (billed per use)**. Select only `routes()` rows with
|
|
75
|
+
`keyEntry: true`; API rows remain `offer: false`. Each route also carries its engine `revision`, `checked` date,
|
|
76
|
+
label and key error word names. Submit the input directly, clear the field afterwards, and never log or save it in
|
|
77
|
+
app state. The kit never returns engine key-check errors or the key. `ui-core/kits` supplies `keyStep`/`keyView`
|
|
78
|
+
for entry, checking, success and failure states, using the kit's `key.*` words.
|
|
79
|
+
|
|
80
|
+
To use the saved key for one run, pass `auth: 'apiKey'`. The key lives in the member's separate
|
|
81
|
+
`byokit-key-<member>` agent and workspace, with `copyToAgents: false` and only that key in its local auth order.
|
|
82
|
+
Its selected model is used; omit `model` or pass that same model. Key runs have separate history. To steer or stop
|
|
83
|
+
one, pass `{ auth: 'apiKey' }` to `steer` or `abort` too (the device helpers accept the same option).
|
|
84
|
+
Ordinary runs never enter the key agent, even when the subscription is resting. This agent boundary is necessary
|
|
85
|
+
because the engine's profile pins can rotate. The `byokit-key-` member prefix is reserved. Adding another key
|
|
86
|
+
replaces the previous key; a failed replacement leaves this option unavailable. If the normal agent already has
|
|
87
|
+
an API key added through pass-through calls, `addKey` returns `invalid` and leaves that key untouched.
|
|
88
|
+
|
|
72
89
|
A run spec also takes `system`, `images` (`{ data, mimeType }[]`), `thinking` and `tools`, a subset of the app tools
|
|
73
90
|
(`KitOptions.tools` names) this run may call; any other app tool is refused at the gate before `ToolHost.gate` sees
|
|
74
91
|
it. Tool events carry the engine's call `id`, the `input` on `start` and the `output` and `error` on `end`. A run that
|
|
@@ -120,10 +137,15 @@ route: github-copilot subscription code
|
|
|
120
137
|
route: github-copilot-enterprise subscription code
|
|
121
138
|
route: minimax-global-oauth subscription code
|
|
122
139
|
route: minimax-cn-oauth subscription code
|
|
140
|
+
route: anthropic-cli subscription browser
|
|
141
|
+
route: setup-token subscription browser
|
|
123
142
|
```
|
|
124
143
|
|
|
125
|
-
|
|
126
|
-
|
|
144
|
+
Subscription sign-ins are offered by default. API-billed routes remain in `kit.routes()` with `offer: false`;
|
|
145
|
+
apps can offer them when the app or person opts in, labelled API key (billed per use). Each `Route` carries `billing` (`subscription`, `api` or
|
|
146
|
+
`local`) so the app can label what is billed. Proxy routes, compatibility aliases, local runtimes stay off. Native Claude Code sign-in is offered by default. The pinned Gateway guides only some routes; other choices need manual paste or key entry. For an opted-in route, allow its `plugin` id
|
|
147
|
+
in the app configuration and pass its `choice` explicitly to `kit.signIn()`.
|
|
148
|
+
Offered provider plugins are allowed by default alongside the app's plugin ids.
|
|
127
149
|
|
|
128
150
|
## API at a glance
|
|
129
151
|
|
package/dist/device.d.ts
CHANGED
|
@@ -23,6 +23,7 @@ export type SessionRow = {
|
|
|
23
23
|
export type DeviceRunOptions = {
|
|
24
24
|
sessionKey?: string;
|
|
25
25
|
model?: string;
|
|
26
|
+
auth?: 'apiKey';
|
|
26
27
|
system?: string;
|
|
27
28
|
images?: {
|
|
28
29
|
data: string;
|
|
@@ -64,8 +65,12 @@ export declare function openclawDevice(link: DeviceLink): {
|
|
|
64
65
|
type: 'end';
|
|
65
66
|
end: RunEnd;
|
|
66
67
|
}>;
|
|
67
|
-
steer(k: string, t: string
|
|
68
|
-
|
|
68
|
+
steer(k: string, t: string, o?: {
|
|
69
|
+
auth?: 'apiKey';
|
|
70
|
+
}): Promise<void>;
|
|
71
|
+
abort(k: string, o?: {
|
|
72
|
+
auth?: 'apiKey';
|
|
73
|
+
}): Promise<void>;
|
|
69
74
|
approvals(): Promise<Approval[]>;
|
|
70
75
|
decide(id: string, d: Decision): Promise<void>;
|
|
71
76
|
events(): AsyncIterable<OpenClawLinkEvent>;
|
package/dist/device.js
CHANGED
|
@@ -139,6 +139,7 @@ export function openclawDevice(link) {
|
|
|
139
139
|
run: (message, o) => {
|
|
140
140
|
const inner = liveStream(() => link.stream('oc.run', { message, ...(o?.sessionKey ? { sessionKey: o.sessionKey } : {}),
|
|
141
141
|
...(o?.model !== undefined ? { model: o.model } : {}), ...(o?.system !== undefined ? { system: o.system } : {}),
|
|
142
|
+
...(o?.auth !== undefined ? { auth: o.auth } : {}),
|
|
142
143
|
...(o?.images !== undefined ? { images: o.images } : {}), ...(o?.thinking !== undefined ? { thinking: o.thinking } : {}),
|
|
143
144
|
...(o?.tools !== undefined ? { tools: o.tools } : {}) }));
|
|
144
145
|
let finished = false;
|
|
@@ -168,11 +169,11 @@ export function openclawDevice(link) {
|
|
|
168
169
|
},
|
|
169
170
|
};
|
|
170
171
|
},
|
|
171
|
-
steer: async (k, t) => {
|
|
172
|
-
await link.request('oc.steer', { sessionKey: k, text: t });
|
|
172
|
+
steer: async (k, t, o) => {
|
|
173
|
+
await link.request('oc.steer', { sessionKey: k, text: t, ...(o?.auth ? { auth: o.auth } : {}) });
|
|
173
174
|
},
|
|
174
|
-
abort: async (k) => {
|
|
175
|
-
await link.request('oc.abort', { sessionKey: k });
|
|
175
|
+
abort: async (k, o) => {
|
|
176
|
+
await link.request('oc.abort', { sessionKey: k, ...(o?.auth ? { auth: o.auth } : {}) });
|
|
176
177
|
},
|
|
177
178
|
approvals: () => link.request('oc.approvals'),
|
|
178
179
|
decide: async (id, d) => {
|
package/dist/engine.js
CHANGED
|
@@ -17,6 +17,14 @@ const putChanged = (path, data) => {
|
|
|
17
17
|
if (!existsSync(path) || readFileSync(path, 'utf8') !== data)
|
|
18
18
|
writeFileSync(path, data, { mode: 0o600 });
|
|
19
19
|
};
|
|
20
|
+
function processStartTime(pid) {
|
|
21
|
+
const stat = readFileSync(`/proc/${pid}/stat`, 'utf8');
|
|
22
|
+
// comm can contain spaces and parentheses; field 22 is index 19 after its final ')'.
|
|
23
|
+
const startTime = stat.slice(stat.lastIndexOf(')') + 2).split(' ')[19];
|
|
24
|
+
if (!startTime || !/^\d+$/.test(startTime))
|
|
25
|
+
throw new Error('unverifiable engine start time');
|
|
26
|
+
return startTime;
|
|
27
|
+
}
|
|
20
28
|
const supports = (list, value) => !list ||
|
|
21
29
|
(!list.includes(`!${value}`) && (list.includes('any') || list.every(item => item.startsWith('!')) || list.includes(value)));
|
|
22
30
|
function installMatches(dir) {
|
|
@@ -140,7 +148,7 @@ export class Engine {
|
|
|
140
148
|
const pluginDir = join(this.root, 'plugin');
|
|
141
149
|
mkdirSync(pluginDir, { recursive: true, mode: 0o700 });
|
|
142
150
|
// The shipped plugin follows the kit on every prepare too: a state dir from an older kit must not keep its gate.
|
|
143
|
-
for (const f of ['package.json', 'index.js']) {
|
|
151
|
+
for (const f of ['package.json', 'index.js', 'keys.js']) {
|
|
144
152
|
const source = join(kitDir, 'plugin', f);
|
|
145
153
|
putChanged(join(pluginDir, f), readFileSync(source, 'utf8'));
|
|
146
154
|
}
|
|
@@ -175,13 +183,14 @@ export class Engine {
|
|
|
175
183
|
return false;
|
|
176
184
|
try {
|
|
177
185
|
const proc = `/proc/${pid}`;
|
|
178
|
-
const
|
|
186
|
+
const identity = JSON.parse(readFileSync(join(this.root, 'gateway.identity'), 'utf8'));
|
|
179
187
|
const env = readFileSync(`${proc}/environ`, 'utf8').split('\0');
|
|
180
188
|
const home = join(this.root, 'home');
|
|
181
189
|
const owner = join(this.root, 'auth-store.lock', 'pid');
|
|
182
190
|
return statSync(proc).uid === process.getuid()
|
|
191
|
+
&& Number(readFileSync(join(this.root, 'gateway.pid'), 'utf8')) === pid
|
|
192
|
+
&& identity.pid === pid && identity.startTime === processStartTime(pid)
|
|
183
193
|
&& realpathSync(`${proc}/exe`) === realpathSync(process.execPath)
|
|
184
|
-
&& cmd.indexOf(this.entry) > 0 && cmd[cmd.indexOf(this.entry) + 1] === 'gateway'
|
|
185
194
|
&& realpathSync(`${proc}/cwd`) === realpathSync(home)
|
|
186
195
|
&& env.includes(`HOME=${home}`)
|
|
187
196
|
&& env.includes(`OPENCLAW_STATE_DIR=${join(this.root, 'state')}`)
|
|
@@ -205,13 +214,9 @@ export class Engine {
|
|
|
205
214
|
if (!this.verifiedOrphan(pid))
|
|
206
215
|
throw occupied();
|
|
207
216
|
// Verify a stable process identity again immediately before signalling this pid, never its group.
|
|
208
|
-
const identity = () => {
|
|
209
|
-
const stat = readFileSync(`/proc/${pid}/stat`, 'utf8');
|
|
210
|
-
return stat.slice(stat.lastIndexOf(')') + 2).split(' ')[19]; // starttime, not a reusable pid alone
|
|
211
|
-
};
|
|
212
217
|
try {
|
|
213
|
-
const before =
|
|
214
|
-
if (!
|
|
218
|
+
const before = processStartTime(pid);
|
|
219
|
+
if (!this.verifiedOrphan(pid) || before !== processStartTime(pid))
|
|
215
220
|
throw occupied();
|
|
216
221
|
}
|
|
217
222
|
catch {
|
|
@@ -301,6 +306,9 @@ export class Engine {
|
|
|
301
306
|
this.state('failed', 'exited');
|
|
302
307
|
throw new Error('engine spawn failed');
|
|
303
308
|
}
|
|
309
|
+
if (process.platform === 'linux') {
|
|
310
|
+
writeFileSync(join(this.root, 'gateway.identity'), JSON.stringify({ pid: child.pid, startTime: processStartTime(child.pid) }), { mode: 0o600 });
|
|
311
|
+
}
|
|
304
312
|
writeFileSync(join(this.root, 'gateway.pid'), String(child.pid), { mode: 0o600 });
|
|
305
313
|
child.once('exit', (code) => {
|
|
306
314
|
if (this.child !== child || this.stopping)
|
|
@@ -328,8 +336,10 @@ export class Engine {
|
|
|
328
336
|
}
|
|
329
337
|
removeOwnedPid(pid) {
|
|
330
338
|
const path = join(this.root, 'gateway.pid');
|
|
331
|
-
if (pid && existsSync(path) && readFileSync(path, 'utf8') === String(pid))
|
|
339
|
+
if (pid && existsSync(path) && readFileSync(path, 'utf8') === String(pid)) {
|
|
332
340
|
rmSync(path, { force: true });
|
|
341
|
+
rmSync(join(this.root, 'gateway.identity'), { force: true });
|
|
342
|
+
}
|
|
333
343
|
}
|
|
334
344
|
withAuthStore(task) { return this.authStore.offline(task); }
|
|
335
345
|
async stop() {
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
export { ENGINE_VERSION, OPERATOR_SCOPES, PROTOCOL_VERSION } from './constants.ts';
|
|
2
2
|
export type { Approval, CallOptions, Decision, GatewayEventName, GatewayEventPayload, GatewayMethod, GatewayParams, GatewayResult, GatewayTransport, GateResult, Hello, KitState, Member, PlanWindow, Route, RunEnd, RunEvent, RunRef, RunSpec, RunUsage, SignInOptions, SignInView, ToolHost, ToolSpec, } from './types.ts';
|
|
3
3
|
export { OpenClawKit, type KitOptions, type RetainedLogin } from './kit.ts';
|
|
4
|
+
export type { AddKeyResult } from './keys.ts';
|
|
4
5
|
export { stateWords, toAccountView, words, type AccountView, type WordKey } from './words.ts';
|
|
5
6
|
export { EngineAlreadyRunningError } from './engine-status.ts';
|
package/dist/keys.d.ts
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { GatewayTransport, Member } from './types.ts';
|
|
2
|
+
export type AddKeyResult = 'ok' | 'invalid' | 'not_included';
|
|
3
|
+
export declare function createKeys(ctx: {
|
|
4
|
+
root: string;
|
|
5
|
+
request: GatewayTransport['request'];
|
|
6
|
+
ensure(member: Member): Promise<{
|
|
7
|
+
agentId: string;
|
|
8
|
+
}>;
|
|
9
|
+
restarting?(): boolean;
|
|
10
|
+
}): {
|
|
11
|
+
exclusive: <T>(member: Member, work: () => Promise<T>) => Promise<T>;
|
|
12
|
+
add(member: Member, o: {
|
|
13
|
+
authChoice: string;
|
|
14
|
+
apiKey: string;
|
|
15
|
+
}): Promise<AddKeyResult>;
|
|
16
|
+
ready(member: Member): Promise<{
|
|
17
|
+
agentId: string;
|
|
18
|
+
model: string;
|
|
19
|
+
} | undefined>;
|
|
20
|
+
};
|
package/dist/keys.js
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { join } from 'node:path';
|
|
2
|
+
import { keyAgentId, MEMBER_ID, KEY_PREFIX } from "./members.js";
|
|
3
|
+
import { routes } from "./routes.js";
|
|
4
|
+
export function createKeys(ctx) {
|
|
5
|
+
// Serialize key replacement and key runs, so an in-flight run never sees a half-installed replacement.
|
|
6
|
+
const tails = new Map();
|
|
7
|
+
const exclusive = async (member, work) => {
|
|
8
|
+
const previous = tails.get(member) ?? Promise.resolve();
|
|
9
|
+
const next = previous.catch(() => { }).then(work);
|
|
10
|
+
tails.set(member, next);
|
|
11
|
+
try {
|
|
12
|
+
return await next;
|
|
13
|
+
}
|
|
14
|
+
finally {
|
|
15
|
+
if (tails.get(member) === next)
|
|
16
|
+
tails.delete(member);
|
|
17
|
+
}
|
|
18
|
+
};
|
|
19
|
+
const check = (member) => MEMBER_ID.test(member) && !member.startsWith(KEY_PREFIX);
|
|
20
|
+
const control = (member, action, provider) => ctx.request('byokit.keys', { member, action, ...(provider ? { provider } : {}) });
|
|
21
|
+
return {
|
|
22
|
+
exclusive,
|
|
23
|
+
async add(member, o) {
|
|
24
|
+
if (!check(member) || !o.apiKey.trim())
|
|
25
|
+
return 'invalid';
|
|
26
|
+
const route = routes().find((r) => r.choice === o.authChoice && r.keyEntry);
|
|
27
|
+
if (!route)
|
|
28
|
+
return 'not_included';
|
|
29
|
+
return exclusive(member, async () => {
|
|
30
|
+
try {
|
|
31
|
+
await ctx.ensure(member);
|
|
32
|
+
const agentId = keyAgentId(member);
|
|
33
|
+
const list = await ctx.request('agents.list');
|
|
34
|
+
if (!list.agents.some((a) => a.id === agentId))
|
|
35
|
+
await ctx.request('agents.create', { name: agentId, workspace: join(ctx.root, 'key-workspaces', member) });
|
|
36
|
+
if (!(await control(member, 'prepare', route.provider)).ok)
|
|
37
|
+
return 'invalid';
|
|
38
|
+
// The public pin's typed API-key path, never a CLI-login import. Discard the engine's lines/errors.
|
|
39
|
+
const params = {
|
|
40
|
+
kind: 'api-key', agentId, authChoice: o.authChoice, apiKey: o.apiKey,
|
|
41
|
+
};
|
|
42
|
+
const result = (await ctx.request('openclaw.setup.activate', params, { timeoutMs: 180_000 }));
|
|
43
|
+
if (!result.ok)
|
|
44
|
+
return result.status === 'unavailable' ? 'not_included' : 'invalid';
|
|
45
|
+
return (await control(member, 'seal')).ok ? 'ok' : 'invalid';
|
|
46
|
+
}
|
|
47
|
+
catch {
|
|
48
|
+
return 'invalid';
|
|
49
|
+
}
|
|
50
|
+
});
|
|
51
|
+
},
|
|
52
|
+
async ready(member) {
|
|
53
|
+
if (!check(member))
|
|
54
|
+
return undefined;
|
|
55
|
+
const deadline = Date.now() + 60_000;
|
|
56
|
+
for (;;) {
|
|
57
|
+
try {
|
|
58
|
+
const ready = await control(member, 'ready');
|
|
59
|
+
return ready.ok && ready.model ? { agentId: keyAgentId(member), model: ready.model } : undefined;
|
|
60
|
+
}
|
|
61
|
+
catch (error) {
|
|
62
|
+
// Config writes may drain/restart the gateway after activation. This read is safe to retry;
|
|
63
|
+
// an absent profile or a terminal refusal still fails closed immediately.
|
|
64
|
+
const refusal = error;
|
|
65
|
+
const transient = ctx.restarting?.() || (refusal?.code === 'UNAVAILABLE' && refusal.retryable === true);
|
|
66
|
+
if (!transient || Date.now() >= deadline)
|
|
67
|
+
return undefined;
|
|
68
|
+
await new Promise((resolve) => setTimeout(resolve, 250));
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
},
|
|
72
|
+
};
|
|
73
|
+
}
|
package/dist/kit.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { SealingAdapter } from '@byokit/secrets';
|
|
2
|
+
import { type AddKeyResult } from './keys.ts';
|
|
2
3
|
import type { Approval, CallOptions, Decision, GatewayEventName, GatewayEventPayload, GatewayMethod, GatewayParams, GatewayResult, GatewayTransport, Hello, KitState, Member, Route, RunEnd, RunEvent, RunSpec, SignInOptions, SignInView, ToolHost, ToolSpec } from './types.ts';
|
|
3
4
|
export type KitOptions = {
|
|
4
5
|
stateDir: string;
|
|
@@ -47,6 +48,7 @@ export declare class OpenClawKit {
|
|
|
47
48
|
private greeting?;
|
|
48
49
|
private members?;
|
|
49
50
|
private readonly bridge;
|
|
51
|
+
private readonly keys;
|
|
50
52
|
private readonly approvalsCtl;
|
|
51
53
|
private listeners;
|
|
52
54
|
private off;
|
|
@@ -81,14 +83,24 @@ export declare class OpenClawKit {
|
|
|
81
83
|
done: Promise<SignInView>;
|
|
82
84
|
};
|
|
83
85
|
signOut(member: Member, provider: string): Promise<void>;
|
|
86
|
+
/** Save an explicitly supplied API key (billed per use) in this member's separate key agent. */
|
|
87
|
+
addKey(agentId: Member, o: {
|
|
88
|
+
authChoice: string;
|
|
89
|
+
apiKey: string;
|
|
90
|
+
}): Promise<AddKeyResult>;
|
|
84
91
|
migrateRetainedLogin(member: Member, source: RetainedLogin): Promise<'staged' | 'nothing' | 'failed'>;
|
|
85
92
|
confirmRetainedLogin(member: Member, source: RetainedLogin): Promise<boolean>;
|
|
86
93
|
private runs;
|
|
87
94
|
/** The app tool names (`KitOptions.tools`): the only names `RunSpec.tools` may carry. */
|
|
88
95
|
toolNames(): string[];
|
|
89
96
|
run(spec: RunSpec, on?: (e: RunEvent) => void): Promise<RunEnd>;
|
|
90
|
-
|
|
91
|
-
|
|
97
|
+
private runKey;
|
|
98
|
+
steer(sessionKey: string, text: string, o?: {
|
|
99
|
+
auth?: 'apiKey';
|
|
100
|
+
}): Promise<void>;
|
|
101
|
+
abort(sessionKey: string, o?: {
|
|
102
|
+
auth?: 'apiKey';
|
|
103
|
+
}): Promise<void>;
|
|
92
104
|
approvals(member?: Member): Approval[];
|
|
93
105
|
onApproval(fn: (a: Approval, change: 'added' | 'resolved') => void): () => void;
|
|
94
106
|
decide(id: string, d: Decision): Promise<void>;
|
package/dist/kit.js
CHANGED
|
@@ -8,8 +8,10 @@ import { Bridge } from "./bridge.js";
|
|
|
8
8
|
import { Engine } from "./engine.js";
|
|
9
9
|
import { gatewayTransport } from "./transport.js";
|
|
10
10
|
import { createMembers } from "./members.js";
|
|
11
|
+
import { keyAgentId, KEY_PREFIX, MEMBER_ID } from "./members.js";
|
|
11
12
|
import { confirmRetainedLogin as confirmLogin, migrateRetainedLogin as migrateLogin } from "./migrate.js";
|
|
12
13
|
import { createRuns } from "./runs.js";
|
|
14
|
+
import { createKeys } from "./keys.js";
|
|
13
15
|
import { routes as routeTable } from "./routes.js";
|
|
14
16
|
import { providers as engineProviders, signIn as startSignIn, signOut as engineSignOut } from "./signin.js";
|
|
15
17
|
import { reconcileConfig, memoryLimited as configMemoryLimited } from "./config.js";
|
|
@@ -420,6 +422,7 @@ export class OpenClawKit {
|
|
|
420
422
|
// One bridge and one approval surface per kit: listeners and run registrations survive reconnects (B3).
|
|
421
423
|
// Only the socket itself is per-connection. Approvals always reaches the live transport through this.request().
|
|
422
424
|
bridge;
|
|
425
|
+
keys;
|
|
423
426
|
approvalsCtl;
|
|
424
427
|
listeners = new Set();
|
|
425
428
|
off = [];
|
|
@@ -439,6 +442,10 @@ export class OpenClawKit {
|
|
|
439
442
|
this.engine = new Engine({ ...o, pluginId: o.plugin?.id ?? 'byokit', tools: o.tools ?? [],
|
|
440
443
|
gateBuiltins: o.gateBuiltins !== false, spawnEngine: o.spawnEngine !== false,
|
|
441
444
|
onState: (s) => this.setState(s), onExit: () => this.closed('engine exited') });
|
|
445
|
+
this.keys = createKeys({ root: this.engine.root,
|
|
446
|
+
restarting: () => this.current.phase === 'restarting' || this.current.phase === 'starting',
|
|
447
|
+
request: (method, params, options) => this.request()(method, params, options),
|
|
448
|
+
ensure: (member) => this.ensureMember(member) });
|
|
442
449
|
const slot = {};
|
|
443
450
|
this.approvalsCtl = new Approvals({
|
|
444
451
|
request: (method, params, co) => this.request()(method, params, co),
|
|
@@ -599,6 +606,10 @@ export class OpenClawKit {
|
|
|
599
606
|
signOut(member, provider) {
|
|
600
607
|
return engineSignOut(this.signInCtx(), member, provider);
|
|
601
608
|
}
|
|
609
|
+
/** Save an explicitly supplied API key (billed per use) in this member's separate key agent. */
|
|
610
|
+
addKey(agentId, o) {
|
|
611
|
+
return this.keys.add(agentId, o);
|
|
612
|
+
}
|
|
602
613
|
migrateRetainedLogin(member, source) {
|
|
603
614
|
return migrateLogin({ root: this.engine.root, prepare: () => this.prepare(), doctor: () => this.engine.doctor(120_000),
|
|
604
615
|
seal: this.o.authSeal, log: this.o.log, withStore: (task) => this.engine.withAuthStore(task) }, member, source);
|
|
@@ -627,13 +638,35 @@ export class OpenClawKit {
|
|
|
627
638
|
return (this.o.tools ?? []).map((t) => t.name);
|
|
628
639
|
}
|
|
629
640
|
run(spec, on) {
|
|
641
|
+
if (spec.auth !== undefined && spec.auth !== 'apiKey')
|
|
642
|
+
return Promise.reject(new Error('Choose a supported account option.'));
|
|
643
|
+
if (spec.auth === 'apiKey')
|
|
644
|
+
return this.keys.exclusive(spec.member, async () => {
|
|
645
|
+
if (!spec.sessionKey.startsWith(`agent:${spec.member}:`))
|
|
646
|
+
throw new Error('This conversation belongs to another member.');
|
|
647
|
+
const selected = await this.keys.ready(spec.member);
|
|
648
|
+
if (!selected)
|
|
649
|
+
return { ok: false, kind: 'signed-out', message: 'Add an API key to use this option.' };
|
|
650
|
+
if (spec.model !== undefined && spec.model !== selected.model)
|
|
651
|
+
return { ok: false, kind: 'plan', message: 'Choose the model saved with this key.' };
|
|
652
|
+
// RunRef.member stays the person for app gates; the engine receives the isolated agent and history.
|
|
653
|
+
return this.runs().run({ ...spec, model: selected.model }, on, selected.agentId);
|
|
654
|
+
});
|
|
630
655
|
return this.runs().run(spec, on);
|
|
631
656
|
}
|
|
632
|
-
|
|
633
|
-
|
|
657
|
+
runKey(sessionKey, o) {
|
|
658
|
+
if (o?.auth !== 'apiKey')
|
|
659
|
+
return sessionKey;
|
|
660
|
+
const [, member, tail] = /^agent:([^:]+):(.*)$/.exec(sessionKey) ?? [];
|
|
661
|
+
if (!member || !MEMBER_ID.test(member) || member.startsWith(KEY_PREFIX))
|
|
662
|
+
throw new Error('Choose your own conversation.');
|
|
663
|
+
return `agent:${keyAgentId(member)}:${tail}`;
|
|
664
|
+
}
|
|
665
|
+
steer(sessionKey, text, o) {
|
|
666
|
+
return this.runs().steer(this.runKey(sessionKey, o), text);
|
|
634
667
|
}
|
|
635
|
-
abort(sessionKey) {
|
|
636
|
-
return this.runs().abort(sessionKey);
|
|
668
|
+
abort(sessionKey, o) {
|
|
669
|
+
return this.runs().abort(this.runKey(sessionKey, o));
|
|
637
670
|
}
|
|
638
671
|
// approvals (5.9)
|
|
639
672
|
approvals(member) {
|
package/dist/link.js
CHANGED
|
@@ -25,9 +25,10 @@ function memberSessions(result, member) {
|
|
|
25
25
|
}
|
|
26
26
|
/** `oc.run`'s run options, type-checked; the kit's own checks (account, tool names) still apply. */
|
|
27
27
|
function runOptions(args) {
|
|
28
|
-
const { message, model, system, images, thinking, tools } = args;
|
|
28
|
+
const { message, model, auth, system, images, thinking, tools } = args;
|
|
29
29
|
const ok = typeof message === 'string'
|
|
30
30
|
&& (model === undefined || typeof model === 'string')
|
|
31
|
+
&& (auth === undefined || auth === 'apiKey')
|
|
31
32
|
&& (system === undefined || typeof system === 'string')
|
|
32
33
|
&& (images === undefined || (Array.isArray(images) && images.every((i) => isRecord(i) && typeof i.data === 'string' && typeof i.mimeType === 'string')))
|
|
33
34
|
&& (thinking === undefined || (typeof thinking === 'string' && THINKING.has(thinking)))
|
|
@@ -37,6 +38,7 @@ function runOptions(args) {
|
|
|
37
38
|
return {
|
|
38
39
|
message: message,
|
|
39
40
|
...(model === undefined ? {} : { model: model }),
|
|
41
|
+
...(auth === undefined ? {} : { auth: 'apiKey' }),
|
|
40
42
|
...(system === undefined ? {} : { system: system }),
|
|
41
43
|
...(images === undefined ? {} : { images: images
|
|
42
44
|
.map((i) => ({ data: i.data, mimeType: i.mimeType })) }),
|
|
@@ -180,19 +182,19 @@ export function openclawLink(kit, o) {
|
|
|
180
182
|
return memberSessions(result, member);
|
|
181
183
|
}
|
|
182
184
|
case 'oc.steer': {
|
|
183
|
-
if (typeof args.sessionKey !== 'string' || typeof args.text !== 'string')
|
|
185
|
+
if (typeof args.sessionKey !== 'string' || typeof args.text !== 'string' || (args.auth !== undefined && args.auth !== 'apiKey'))
|
|
184
186
|
throw new Error('oc.steer needs { sessionKey, text }');
|
|
185
187
|
if (!memberKey(member, args.sessionKey))
|
|
186
188
|
throw refused();
|
|
187
|
-
await kit.steer(args.sessionKey, args.text);
|
|
189
|
+
await kit.steer(args.sessionKey, args.text, args.auth === 'apiKey' ? { auth: 'apiKey' } : undefined);
|
|
188
190
|
return null;
|
|
189
191
|
}
|
|
190
192
|
case 'oc.abort': {
|
|
191
|
-
if (typeof args.sessionKey !== 'string')
|
|
193
|
+
if (typeof args.sessionKey !== 'string' || (args.auth !== undefined && args.auth !== 'apiKey'))
|
|
192
194
|
throw new Error('oc.abort needs { sessionKey }');
|
|
193
195
|
if (!memberKey(member, args.sessionKey))
|
|
194
196
|
throw refused();
|
|
195
|
-
await kit.abort(args.sessionKey);
|
|
197
|
+
await kit.abort(args.sessionKey, args.auth === 'apiKey' ? { auth: 'apiKey' } : undefined);
|
|
196
198
|
return null;
|
|
197
199
|
}
|
|
198
200
|
case 'oc.approvals':
|
package/dist/members.d.ts
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import type { GatewayTransport, Member } from './types.ts';
|
|
2
2
|
export declare const MEMBER_ID: RegExp;
|
|
3
|
+
export declare const KEY_PREFIX = "byokit-key-";
|
|
4
|
+
export declare const keyAgentId: (member: Member) => string;
|
|
3
5
|
export declare function createMembers(ctx: {
|
|
4
6
|
request: GatewayTransport['request'];
|
|
5
7
|
root: string;
|
package/dist/members.js
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
import { join } from 'node:path';
|
|
2
2
|
export const MEMBER_ID = /^[a-z][a-z0-9-]{0,31}$/;
|
|
3
|
+
export const KEY_PREFIX = 'byokit-key-';
|
|
4
|
+
export const keyAgentId = (member) => `${KEY_PREFIX}${member}`;
|
|
3
5
|
export function createMembers(ctx) {
|
|
4
6
|
const cache = new Map();
|
|
5
7
|
return { ensure(member) {
|
|
6
|
-
if (!MEMBER_ID.test(member))
|
|
8
|
+
if (!MEMBER_ID.test(member) || member.startsWith(KEY_PREFIX))
|
|
7
9
|
return Promise.reject(new Error(`invalid member id: ${member}`));
|
|
8
10
|
const existing = cache.get(member);
|
|
9
11
|
if (existing)
|