Skip to content

Commit 91ac485

Browse files
authored
feat(tokenjuice): bundle the native adapter (#69946)
* feat(plugins): register embedded extension factories * feat(tokenjuice): bundle the native adapter * fix(tokenjuice): gate the bundled embedded extension seam * fix(tokenjuice): refresh runtime sidecar baseline * fix(plugins): harden bundled embedded extensions * fix(plugins): install source bundled runtime deps * fix(tokenjuice): sync lockfile importer * fix(plugins): validate reused runtime dep versions * fix(plugins): restore tokenjuice CI contract * fix(plugins): remove tokenjuice dts bridge * fix(tokenjuice): repair openclaw type shim * fix(plugins): harden bundled runtime deps * fix(plugins): keep source checkout runtime deps local * fix(plugins): isolate bundled runtime dep installs * fix(cli): keep plugin startup registration non-activating * fix(cli): keep loader overrides out of plugin cli options
1 parent 2013855 commit 91ac485

38 files changed

Lines changed: 1338 additions & 133 deletions

‎.github/labeler.yml‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,10 @@
241241
- changed-files:
242242
- any-glob-to-any-file:
243243
- "extensions/open-prose/**"
244+
"extensions: tokenjuice":
245+
- changed-files:
246+
- any-glob-to-any-file:
247+
- "extensions/tokenjuice/**"
244248
"extensions: webhooks":
245249
- changed-files:
246250
- any-glob-to-any-file:
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
bd14f9118c8359c8ab0a7da984be28a319e82fadb004f55dc5888c0a07d411d3 plugin-sdk-api-baseline.json
2-
ef09464bba3712998c0accf9a4e551ba31af4d7a2f77ce01120a1f4b48ca4ac5 plugin-sdk-api-baseline.jsonl
1+
6f605be396ee42efbe26cfd0cc90d7710ca378959aecd6388dd81a5b97996b43 plugin-sdk-api-baseline.json
2+
9c34c7c068f6d3bc5cf44817fe14c470c1c091595296f829e1efb4d6e7ba3599 plugin-sdk-api-baseline.jsonl

‎docs/plugins/building-plugins.md‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ A single plugin can register any number of capabilities via the `api` object:
162162
| Video generation | `api.registerVideoGenerationProvider(...)` | [Provider Plugins](/plugins/sdk-provider-plugins#step-5-add-extra-capabilities) |
163163
| Web fetch | `api.registerWebFetchProvider(...)` | [Provider Plugins](/plugins/sdk-provider-plugins#step-5-add-extra-capabilities) |
164164
| Web search | `api.registerWebSearchProvider(...)` | [Provider Plugins](/plugins/sdk-provider-plugins#step-5-add-extra-capabilities) |
165+
| Embedded Pi extension | `api.registerEmbeddedExtensionFactory(...)` | [SDK Overview](/plugins/sdk-overview#registration-api) |
165166
| Agent tools | `api.registerTool(...)` | Below |
166167
| Custom commands | `api.registerCommand(...)` | [Entry Points](/plugins/sdk-entrypoints) |
167168
| Event hooks | `api.registerHook(...)` | [Entry Points](/plugins/sdk-entrypoints) |
@@ -170,6 +171,11 @@ A single plugin can register any number of capabilities via the `api` object:
170171

171172
For the full registration API, see [SDK Overview](/plugins/sdk-overview#registration-api).
172173

174+
Use `api.registerEmbeddedExtensionFactory(...)` when a plugin needs Pi-native
175+
embedded-runner hooks such as async `tool_result` rewriting before the final
176+
tool result message is emitted. Prefer regular OpenClaw plugin hooks when the
177+
work does not need Pi extension timing.
178+
173179
If your plugin registers custom gateway RPC methods, keep them on a
174180
plugin-specific prefix. Core admin namespaces (`config.*`,
175181
`exec.approvals.*`, `wizard.*`, `update.*`) stay reserved and always resolve to

‎docs/plugins/manifest.md‎

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,7 @@ read without importing the plugin runtime.
382382
```json
383383
{
384384
"contracts": {
385+
"embeddedExtensionFactories": ["pi"],
385386
"speechProviders": ["openai"],
386387
"realtimeTranscriptionProviders": ["openai"],
387388
"realtimeVoiceProviders": ["openai"],
@@ -397,17 +398,18 @@ read without importing the plugin runtime.
397398

398399
Each list is optional:
399400

400-
| Field | Type | What it means |
401-
| -------------------------------- | ---------- | -------------------------------------------------------------- |
402-
| `speechProviders` | `string[]` | Speech provider ids this plugin owns. |
403-
| `realtimeTranscriptionProviders` | `string[]` | Realtime-transcription provider ids this plugin owns. |
404-
| `realtimeVoiceProviders` | `string[]` | Realtime-voice provider ids this plugin owns. |
405-
| `mediaUnderstandingProviders` | `string[]` | Media-understanding provider ids this plugin owns. |
406-
| `imageGenerationProviders` | `string[]` | Image-generation provider ids this plugin owns. |
407-
| `videoGenerationProviders` | `string[]` | Video-generation provider ids this plugin owns. |
408-
| `webFetchProviders` | `string[]` | Web-fetch provider ids this plugin owns. |
409-
| `webSearchProviders` | `string[]` | Web-search provider ids this plugin owns. |
410-
| `tools` | `string[]` | Agent tool names this plugin owns for bundled contract checks. |
401+
| Field | Type | What it means |
402+
| -------------------------------- | ---------- | ----------------------------------------------------------------- |
403+
| `embeddedExtensionFactories` | `string[]` | Embedded runtime ids a bundled plugin may register factories for. |
404+
| `speechProviders` | `string[]` | Speech provider ids this plugin owns. |
405+
| `realtimeTranscriptionProviders` | `string[]` | Realtime-transcription provider ids this plugin owns. |
406+
| `realtimeVoiceProviders` | `string[]` | Realtime-voice provider ids this plugin owns. |
407+
| `mediaUnderstandingProviders` | `string[]` | Media-understanding provider ids this plugin owns. |
408+
| `imageGenerationProviders` | `string[]` | Image-generation provider ids this plugin owns. |
409+
| `videoGenerationProviders` | `string[]` | Video-generation provider ids this plugin owns. |
410+
| `webFetchProviders` | `string[]` | Web-fetch provider ids this plugin owns. |
411+
| `webSearchProviders` | `string[]` | Web-search provider ids this plugin owns. |
412+
| `tools` | `string[]` | Agent tool names this plugin owns for bundled contract checks. |
411413

412414
## mediaUnderstandingProviderMetadata reference
413415

‎docs/plugins/sdk-overview.md‎

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -343,22 +343,31 @@ methods:
343343

344344
### Infrastructure
345345

346-
| Method | What it registers |
347-
| ---------------------------------------------- | --------------------------------------- |
348-
| `api.registerHook(events, handler, opts?)` | Event hook |
349-
| `api.registerHttpRoute(params)` | Gateway HTTP endpoint |
350-
| `api.registerGatewayMethod(name, handler)` | Gateway RPC method |
351-
| `api.registerCli(registrar, opts?)` | CLI subcommand |
352-
| `api.registerService(service)` | Background service |
353-
| `api.registerInteractiveHandler(registration)` | Interactive handler |
354-
| `api.registerMemoryPromptSupplement(builder)` | Additive memory-adjacent prompt section |
355-
| `api.registerMemoryCorpusSupplement(adapter)` | Additive memory search/read corpus |
346+
| Method | What it registers |
347+
| ----------------------------------------------- | --------------------------------------- |
348+
| `api.registerHook(events, handler, opts?)` | Event hook |
349+
| `api.registerHttpRoute(params)` | Gateway HTTP endpoint |
350+
| `api.registerGatewayMethod(name, handler)` | Gateway RPC method |
351+
| `api.registerCli(registrar, opts?)` | CLI subcommand |
352+
| `api.registerService(service)` | Background service |
353+
| `api.registerInteractiveHandler(registration)` | Interactive handler |
354+
| `api.registerEmbeddedExtensionFactory(factory)` | Pi embedded-runner extension factory |
355+
| `api.registerMemoryPromptSupplement(builder)` | Additive memory-adjacent prompt section |
356+
| `api.registerMemoryCorpusSupplement(adapter)` | Additive memory search/read corpus |
356357

357358
Reserved core admin namespaces (`config.*`, `exec.approvals.*`, `wizard.*`,
358359
`update.*`) always stay `operator.admin`, even if a plugin tries to assign a
359360
narrower gateway method scope. Prefer plugin-specific prefixes for
360361
plugin-owned methods.
361362

363+
Use `api.registerEmbeddedExtensionFactory(...)` when a plugin needs Pi-native
364+
event timing during OpenClaw embedded runs, for example async `tool_result`
365+
rewrites that must happen before the final tool-result message is emitted.
366+
This is a bundled-plugin seam today: only bundled plugins may register one, and
367+
they must declare `contracts.embeddedExtensionFactories: ["pi"]` in
368+
`openclaw.plugin.json`. Keep normal OpenClaw plugin hooks for everything that
369+
does not require that lower-level seam.
370+
362371
### CLI registration metadata
363372

364373
`api.registerCli(registrar, opts?)` accepts two kinds of top-level metadata:
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import fs from "node:fs";
2+
import { beforeEach, describe, expect, it, vi } from "vitest";
3+
import { createTestPluginApi } from "../../test/helpers/plugins/plugin-api.js";
4+
5+
const { tokenjuiceFactory, createTokenjuiceOpenClawEmbeddedExtension } = vi.hoisted(() => {
6+
const tokenjuiceFactory = vi.fn();
7+
const createTokenjuiceOpenClawEmbeddedExtension = vi.fn(() => tokenjuiceFactory);
8+
return {
9+
tokenjuiceFactory,
10+
createTokenjuiceOpenClawEmbeddedExtension,
11+
};
12+
});
13+
14+
vi.mock("./runtime-api.js", () => ({
15+
createTokenjuiceOpenClawEmbeddedExtension,
16+
}));
17+
18+
import plugin from "./index.js";
19+
20+
describe("tokenjuice bundled plugin", () => {
21+
beforeEach(() => {
22+
createTokenjuiceOpenClawEmbeddedExtension.mockClear();
23+
tokenjuiceFactory.mockClear();
24+
});
25+
26+
it("is opt-in by default", () => {
27+
const manifest = JSON.parse(
28+
fs.readFileSync(new URL("./openclaw.plugin.json", import.meta.url), "utf8"),
29+
) as { enabledByDefault?: unknown };
30+
31+
expect(manifest.enabledByDefault).toBeUndefined();
32+
});
33+
34+
it("registers the tokenjuice embedded extension factory", () => {
35+
const registerEmbeddedExtensionFactory = vi.fn();
36+
37+
plugin.register(
38+
createTestPluginApi({
39+
id: "tokenjuice",
40+
name: "tokenjuice",
41+
source: "test",
42+
config: {},
43+
pluginConfig: {},
44+
runtime: {} as never,
45+
registerEmbeddedExtensionFactory,
46+
}),
47+
);
48+
49+
expect(createTokenjuiceOpenClawEmbeddedExtension).toHaveBeenCalledTimes(1);
50+
expect(registerEmbeddedExtensionFactory).toHaveBeenCalledWith(tokenjuiceFactory);
51+
});
52+
});

‎extensions/tokenjuice/index.ts‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { definePluginEntry } from "openclaw/plugin-sdk/plugin-entry";
2+
import { createTokenjuiceOpenClawEmbeddedExtension } from "./runtime-api.js";
3+
4+
export default definePluginEntry({
5+
id: "tokenjuice",
6+
name: "tokenjuice",
7+
description: "Compacts exec and bash tool results with tokenjuice reducers.",
8+
register(api) {
9+
api.registerEmbeddedExtensionFactory(createTokenjuiceOpenClawEmbeddedExtension());
10+
},
11+
});
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import fs from "node:fs";
2+
import { describe, expect, it } from "vitest";
3+
4+
type TokenjuicePackageManifest = {
5+
dependencies?: Record<string, string>;
6+
openclaw?: {
7+
bundle?: {
8+
stageRuntimeDependencies?: boolean;
9+
};
10+
};
11+
};
12+
13+
type TokenjuicePluginManifest = {
14+
contracts?: {
15+
embeddedExtensionFactories?: string[];
16+
};
17+
};
18+
19+
describe("tokenjuice package manifest", () => {
20+
it("opts into staging bundled runtime dependencies", () => {
21+
const packageJson = JSON.parse(
22+
fs.readFileSync(new URL("./package.json", import.meta.url), "utf8"),
23+
) as TokenjuicePackageManifest;
24+
25+
expect(packageJson.dependencies?.tokenjuice).toBe("0.6.1");
26+
expect(packageJson.openclaw?.bundle?.stageRuntimeDependencies).toBe(true);
27+
});
28+
29+
it("declares Pi embedded extension factory ownership in the manifest contract", () => {
30+
const manifest = JSON.parse(
31+
fs.readFileSync(new URL("./openclaw.plugin.json", import.meta.url), "utf8"),
32+
) as TokenjuicePluginManifest;
33+
34+
expect(manifest.contracts?.embeddedExtensionFactories).toEqual(["pi"]);
35+
});
36+
});
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"id": "tokenjuice",
3+
"name": "tokenjuice",
4+
"description": "Compacts exec and bash tool results with tokenjuice reducers.",
5+
"contracts": {
6+
"embeddedExtensionFactories": ["pi"]
7+
},
8+
"configSchema": {
9+
"type": "object",
10+
"additionalProperties": false,
11+
"properties": {}
12+
}
13+
}

‎extensions/tokenjuice/package.json‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"name": "@openclaw/tokenjuice",
3+
"version": "2026.4.21",
4+
"description": "Bundled tokenjuice exec output compaction plugin",
5+
"type": "module",
6+
"dependencies": {
7+
"tokenjuice": "0.6.1"
8+
},
9+
"devDependencies": {
10+
"@openclaw/plugin-sdk": "workspace:*"
11+
},
12+
"openclaw": {
13+
"extensions": [
14+
"./index.ts"
15+
],
16+
"bundle": {
17+
"stageRuntimeDependencies": true
18+
}
19+
}
20+
}

0 commit comments

Comments
 (0)