@osovv/vv-opencode 2.0.1 → 2.1.1
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 +51 -0
- package/README.md +88 -87
- package/dist/commands/completion.js +0 -1
- package/dist/commands/completion.js.map +1 -1
- package/dist/commands/doctor.js +6 -9
- package/dist/commands/doctor.js.map +1 -1
- package/dist/commands/init.d.ts +2 -1
- package/dist/commands/init.js +7 -12
- package/dist/commands/init.js.map +1 -1
- package/dist/commands/install.d.ts +4 -0
- package/dist/commands/install.js +40 -37
- package/dist/commands/install.js.map +1 -1
- package/dist/commands/launch.d.ts +2 -1
- package/dist/commands/launch.js +18 -14
- package/dist/commands/launch.js.map +1 -1
- package/dist/commands/patch-provider.d.ts +225 -307
- package/dist/commands/patch-provider.js +92 -296
- package/dist/commands/patch-provider.js.map +1 -1
- package/dist/commands/status.js +7 -10
- package/dist/commands/status.js.map +1 -1
- package/dist/commands/sync.d.ts +4 -0
- package/dist/commands/sync.js +38 -35
- package/dist/commands/sync.js.map +1 -1
- package/dist/index.d.ts +25 -5
- package/dist/index.js +73 -34
- package/dist/index.js.map +1 -1
- package/dist/lib/agent-tool-catalog.d.ts +1 -1
- package/dist/lib/agent-tool-catalog.js +3 -6
- package/dist/lib/agent-tool-catalog.js.map +1 -1
- package/dist/lib/agent-tool-contract.d.ts +22 -13
- package/dist/lib/agent-tool-contract.js +20 -20
- package/dist/lib/agent-tool-contract.js.map +1 -1
- package/dist/lib/analytics/types.d.ts +7 -1
- package/dist/lib/analytics/types.js +2 -2
- package/dist/lib/analytics/types.js.map +1 -1
- package/dist/lib/config-layers.d.ts +28 -6
- package/dist/lib/config-layers.js +268 -73
- package/dist/lib/config-layers.js.map +1 -1
- package/dist/lib/managed-agents.d.ts +24 -2
- package/dist/lib/managed-agents.js +45 -15
- package/dist/lib/managed-agents.js.map +1 -1
- package/dist/lib/model-roles.d.ts +5 -0
- package/dist/lib/model-roles.js +22 -1
- package/dist/lib/model-roles.js.map +1 -1
- package/dist/lib/opencode/agent-registrations.d.ts +4 -4
- package/dist/lib/opencode/agent-registrations.js +386 -197
- package/dist/lib/opencode/agent-registrations.js.map +1 -1
- package/dist/lib/opencode/inspection.d.ts +13 -13
- package/dist/lib/opencode/inspection.js +69 -106
- package/dist/lib/opencode/inspection.js.map +1 -1
- package/dist/lib/opencode/model-overrides.d.ts +2 -2
- package/dist/lib/opencode/model-overrides.js +192 -47
- package/dist/lib/opencode/model-overrides.js.map +1 -1
- package/dist/lib/opencode/paths.d.ts +6 -2
- package/dist/lib/opencode/paths.js +12 -11
- package/dist/lib/opencode/paths.js.map +1 -1
- package/dist/lib/opencode/plugin-registration.d.ts +10 -11
- package/dist/lib/opencode/plugin-registration.js +76 -138
- package/dist/lib/opencode/plugin-registration.js.map +1 -1
- package/dist/lib/opencode/shared-utils.d.ts +47 -3
- package/dist/lib/opencode/shared-utils.js +346 -15
- package/dist/lib/opencode/shared-utils.js.map +1 -1
- package/dist/lib/opencode.d.ts +5 -6
- package/dist/lib/opencode.js +10 -10
- package/dist/lib/opencode.js.map +1 -1
- package/dist/lib/vvoc-config.js +4 -4
- package/dist/lib/vvoc-config.js.map +1 -1
- package/dist/lib/vvoc-paths.d.ts +7 -0
- package/dist/lib/vvoc-paths.js +15 -1
- package/dist/lib/vvoc-paths.js.map +1 -1
- package/dist/lib/vvoc-preset-registry.d.ts +38 -50
- package/dist/lib/vvoc-preset-registry.js +38 -48
- package/dist/lib/vvoc-preset-registry.js.map +1 -1
- package/dist/plugins/analytics/index.d.ts +20 -14
- package/dist/plugins/analytics/index.js +281 -133
- package/dist/plugins/analytics/index.js.map +1 -1
- package/dist/plugins/guardian/index.d.ts +63 -42
- package/dist/plugins/guardian/index.js +217 -759
- package/dist/plugins/guardian/index.js.map +1 -1
- package/dist/plugins/hashline-edit/index.d.ts +145 -31
- package/dist/plugins/hashline-edit/index.js +679 -244
- package/dist/plugins/hashline-edit/index.js.map +1 -1
- package/dist/plugins/hashline-edit/schemas.d.ts +1 -1
- package/dist/plugins/hashline-edit/schemas.js +3 -3
- package/dist/plugins/hashline-edit/schemas.js.map +1 -1
- package/dist/plugins/model-roles/index.d.ts +18 -4
- package/dist/plugins/model-roles/index.js +44 -181
- package/dist/plugins/model-roles/index.js.map +1 -1
- package/dist/plugins/peak-hours/index.d.ts +23 -23
- package/dist/plugins/peak-hours/index.js +211 -180
- package/dist/plugins/peak-hours/index.js.map +1 -1
- package/dist/plugins/secrets-redaction/deep.js +35 -51
- package/dist/plugins/secrets-redaction/deep.js.map +1 -1
- package/dist/plugins/secrets-redaction/index.d.ts +96 -8
- package/dist/plugins/secrets-redaction/index.js +290 -102
- package/dist/plugins/secrets-redaction/index.js.map +1 -1
- package/dist/plugins/secrets-redaction/session.d.ts +9 -0
- package/dist/plugins/secrets-redaction/session.js +21 -4
- package/dist/plugins/secrets-redaction/session.js.map +1 -1
- package/dist/plugins/secrets-redaction/stream.d.ts +92 -0
- package/dist/plugins/secrets-redaction/stream.js +772 -0
- package/dist/plugins/secrets-redaction/stream.js.map +1 -0
- package/dist/plugins/spec-guard/index.d.ts +49 -18
- package/dist/plugins/spec-guard/index.js +231 -129
- package/dist/plugins/spec-guard/index.js.map +1 -1
- package/dist/plugins/system-context-injection/index.d.ts +10 -4
- package/dist/plugins/system-context-injection/index.js +271 -78
- package/dist/plugins/system-context-injection/index.js.map +1 -1
- package/dist/plugins/tool-history-compaction/index.d.ts +26 -4
- package/dist/plugins/tool-history-compaction/index.js +170 -32
- package/dist/plugins/tool-history-compaction/index.js.map +1 -1
- package/dist/plugins/tool-history-compaction/transform.d.ts +47 -16
- package/dist/plugins/tool-history-compaction/transform.js +173 -55
- package/dist/plugins/tool-history-compaction/transform.js.map +1 -1
- package/dist/plugins/web-tools/fetch-service.d.ts +51 -13
- package/dist/plugins/web-tools/fetch-service.js +106 -75
- package/dist/plugins/web-tools/fetch-service.js.map +1 -1
- package/dist/plugins/web-tools/index.d.ts +58 -16
- package/dist/plugins/web-tools/index.js +202 -91
- package/dist/plugins/web-tools/index.js.map +1 -1
- package/dist/plugins/web-tools/media-loader.d.ts +3 -3
- package/dist/plugins/web-tools/media-loader.js +3 -3
- package/dist/plugins/web-tools/media-loader.js.map +1 -1
- package/dist/plugins/web-tools/providers/native-fetch.d.ts +2 -2
- package/dist/plugins/web-tools/providers/native-fetch.js +1 -1
- package/dist/plugins/web-tools/providers/native-fetch.js.map +1 -1
- package/dist/plugins/web-tools/providers/spider.d.ts +2 -2
- package/dist/plugins/web-tools/providers/spider.js +1 -1
- package/dist/plugins/web-tools/providers/spider.js.map +1 -1
- package/dist/plugins/web-tools/providers/zai.d.ts +2 -2
- package/dist/plugins/web-tools/providers/zai.js +1 -1
- package/dist/plugins/web-tools/providers/zai.js.map +1 -1
- package/dist/plugins/web-tools/schemas.d.ts +1 -1
- package/dist/plugins/web-tools/schemas.js +3 -3
- package/dist/plugins/web-tools/schemas.js.map +1 -1
- package/dist/plugins/web-tools/search-service.d.ts +81 -12
- package/dist/plugins/web-tools/search-service.js +104 -57
- package/dist/plugins/web-tools/search-service.js.map +1 -1
- package/dist/plugins/workflow/authorization.d.ts +44 -10
- package/dist/plugins/workflow/authorization.js +113 -73
- package/dist/plugins/workflow/authorization.js.map +1 -1
- package/dist/plugins/workflow/cancellation.d.ts +86 -0
- package/dist/plugins/workflow/cancellation.js +202 -0
- package/dist/plugins/workflow/cancellation.js.map +1 -0
- package/dist/plugins/workflow/checkpoints.d.ts +7 -1
- package/dist/plugins/workflow/checkpoints.js +141 -21
- package/dist/plugins/workflow/checkpoints.js.map +1 -1
- package/dist/plugins/workflow/delegated.d.ts +14 -0
- package/dist/plugins/workflow/delegated.js +6 -5
- package/dist/plugins/workflow/delegated.js.map +1 -1
- package/dist/plugins/workflow/host.d.ts +81 -0
- package/dist/plugins/workflow/host.js +189 -0
- package/dist/plugins/workflow/host.js.map +1 -0
- package/dist/plugins/workflow/index.d.ts +69 -4
- package/dist/plugins/workflow/index.js +1391 -1026
- package/dist/plugins/workflow/index.js.map +1 -1
- package/dist/plugins/workflow/recovery.d.ts +3 -5
- package/dist/plugins/workflow/recovery.js +18 -33
- package/dist/plugins/workflow/recovery.js.map +1 -1
- package/dist/plugins/workflow/repair.d.ts +31 -5
- package/dist/plugins/workflow/repair.js +116 -48
- package/dist/plugins/workflow/repair.js.map +1 -1
- package/dist/plugins/workflow/results.d.ts +31 -31
- package/dist/plugins/workflow/results.js +4 -4
- package/dist/plugins/workflow/results.js.map +1 -1
- package/dist/plugins/workflow/schemas.d.ts +3 -3
- package/dist/plugins/workflow/schemas.js +5 -5
- package/dist/plugins/workflow/schemas.js.map +1 -1
- package/dist/plugins/workflow/system-instruction.md +4 -0
- package/dist/plugins/workflow/tooling.d.ts +6 -0
- package/dist/plugins/workflow/tooling.js +3 -1
- package/dist/plugins/workflow/tooling.js.map +1 -1
- package/dist/runtime/auxiliary.d.ts +13 -0
- package/dist/runtime/auxiliary.js +108 -0
- package/dist/runtime/auxiliary.js.map +1 -0
- package/dist/runtime/client.d.ts +88 -0
- package/dist/runtime/client.js +174 -0
- package/dist/runtime/client.js.map +1 -0
- package/dist/runtime/context-inspection-contract.d.ts +397 -0
- package/dist/runtime/context-inspection-contract.js +622 -0
- package/dist/runtime/context-inspection-contract.js.map +1 -0
- package/dist/runtime/context-inspection.d.ts +52 -0
- package/dist/runtime/context-inspection.js +324 -0
- package/dist/runtime/context-inspection.js.map +1 -0
- package/dist/runtime/context.d.ts +192 -0
- package/dist/runtime/context.js +1246 -0
- package/dist/runtime/context.js.map +1 -0
- package/dist/runtime/coordination.d.ts +40 -0
- package/dist/runtime/coordination.js +87 -0
- package/dist/runtime/coordination.js.map +1 -0
- package/dist/runtime/model-registry.d.ts +34 -0
- package/dist/runtime/model-registry.js +227 -0
- package/dist/runtime/model-registry.js.map +1 -0
- package/dist/runtime/permissions.d.ts +20 -0
- package/dist/runtime/permissions.js +250 -0
- package/dist/runtime/permissions.js.map +1 -0
- package/dist/runtime/snapshot-config.d.ts +80 -0
- package/dist/runtime/snapshot-config.js +268 -0
- package/dist/runtime/snapshot-config.js.map +1 -0
- package/dist/runtime/snapshot-store.d.ts +25 -0
- package/dist/runtime/snapshot-store.js +519 -0
- package/dist/runtime/snapshot-store.js.map +1 -0
- package/dist/runtime/snapshots.d.ts +59 -0
- package/dist/runtime/snapshots.js +721 -0
- package/dist/runtime/snapshots.js.map +1 -0
- package/dist/runtime/types.d.ts +713 -0
- package/dist/runtime/types.js +190 -0
- package/dist/runtime/types.js.map +1 -0
- package/dist/tui/analytics/indicator.d.ts +25 -26
- package/dist/tui/analytics/indicator.js +66 -74
- package/dist/tui/analytics/indicator.js.map +1 -1
- package/dist/tui/branding/footer.d.ts +7 -12
- package/dist/tui/branding/footer.js +16 -24
- package/dist/tui/branding/footer.js.map +1 -1
- package/dist/tui/context/analyze.d.ts +20 -9
- package/dist/tui/context/analyze.js +295 -257
- package/dist/tui/context/analyze.js.map +1 -1
- package/dist/tui/context/collect.d.ts +29 -3
- package/dist/tui/context/collect.js +450 -66
- package/dist/tui/context/collect.js.map +1 -1
- package/dist/tui/context/plugin.d.ts +21 -7
- package/dist/tui/context/plugin.js +65 -70
- package/dist/tui/context/plugin.js.map +1 -1
- package/dist/tui/context/types.d.ts +174 -43
- package/dist/tui/context/types.js +25 -19
- package/dist/tui/context/types.js.map +1 -1
- package/dist/tui/context/view.d.ts +33 -7
- package/dist/tui/context/view.js +136 -81
- package/dist/tui/context/view.js.map +1 -1
- package/dist/tui/peak-hours/banner.d.ts +13 -11
- package/dist/tui/peak-hours/banner.js +48 -80
- package/dist/tui/peak-hours/banner.js.map +1 -1
- package/dist/tui/policy.d.ts +69 -0
- package/dist/tui/policy.js +169 -0
- package/dist/tui/policy.js.map +1 -0
- package/dist/tui.d.ts +3 -8
- package/dist/tui.js +72 -68
- package/dist/tui.js.map +1 -1
- package/package.json +18 -14
- package/schemas/vvoc/v3.json +1 -1
- package/server.js +3 -5
- package/templates/agents/enhancer.md +10 -5
- package/templates/agents/guardian.md +10 -4
- package/templates/agents/investigator.md +4 -2
- package/templates/agents/vv-code-reviewer.md +4 -2
- package/templates/agents/vv-spec-reviewer.md +4 -2
- package/templates/skills/vv-execute/references/tool-contracts.md +1 -1
- package/tui.js +3 -5
- package/dist/plugins/analytics/v2.d.ts +0 -21
- package/dist/plugins/analytics/v2.js +0 -133
- package/dist/plugins/analytics/v2.js.map +0 -1
- package/dist/plugins/guardian/v2.d.ts +0 -13
- package/dist/plugins/guardian/v2.js +0 -204
- package/dist/plugins/guardian/v2.js.map +0 -1
- package/dist/plugins/hashline-edit/v2.d.ts +0 -12
- package/dist/plugins/hashline-edit/v2.js +0 -145
- package/dist/plugins/hashline-edit/v2.js.map +0 -1
- package/dist/plugins/model-roles/v2.d.ts +0 -24
- package/dist/plugins/model-roles/v2.js +0 -274
- package/dist/plugins/model-roles/v2.js.map +0 -1
- package/dist/plugins/peak-hours/v2.d.ts +0 -12
- package/dist/plugins/peak-hours/v2.js +0 -99
- package/dist/plugins/peak-hours/v2.js.map +0 -1
- package/dist/plugins/secrets-redaction/v2.d.ts +0 -19
- package/dist/plugins/secrets-redaction/v2.js +0 -138
- package/dist/plugins/secrets-redaction/v2.js.map +0 -1
- package/dist/plugins/spec-guard/v2.d.ts +0 -12
- package/dist/plugins/spec-guard/v2.js +0 -139
- package/dist/plugins/spec-guard/v2.js.map +0 -1
- package/dist/plugins/system-context-injection/agent-contexts.d.ts +0 -5
- package/dist/plugins/system-context-injection/agent-contexts.js +0 -164
- package/dist/plugins/system-context-injection/agent-contexts.js.map +0 -1
- package/dist/plugins/system-context-injection/v2.d.ts +0 -13
- package/dist/plugins/system-context-injection/v2.js +0 -60
- package/dist/plugins/system-context-injection/v2.js.map +0 -1
- package/dist/plugins/tool-history-compaction/v2.d.ts +0 -11
- package/dist/plugins/tool-history-compaction/v2.js +0 -56
- package/dist/plugins/tool-history-compaction/v2.js.map +0 -1
- package/dist/plugins/v2-runtime/config-watcher.d.ts +0 -26
- package/dist/plugins/v2-runtime/config-watcher.js +0 -111
- package/dist/plugins/v2-runtime/config-watcher.js.map +0 -1
- package/dist/plugins/v2-runtime/index.d.ts +0 -50
- package/dist/plugins/v2-runtime/index.js +0 -114
- package/dist/plugins/v2-runtime/index.js.map +0 -1
- package/dist/plugins/v2-runtime/location-config.d.ts +0 -51
- package/dist/plugins/v2-runtime/location-config.js +0 -91
- package/dist/plugins/v2-runtime/location-config.js.map +0 -1
- package/dist/plugins/v2-runtime/setup.d.ts +0 -48
- package/dist/plugins/v2-runtime/setup.js +0 -174
- package/dist/plugins/v2-runtime/setup.js.map +0 -1
- package/dist/plugins/web-tools/v2.d.ts +0 -11
- package/dist/plugins/web-tools/v2.js +0 -99
- package/dist/plugins/web-tools/v2.js.map +0 -1
- package/dist/plugins/workflow/v2.d.ts +0 -76
- package/dist/plugins/workflow/v2.js +0 -283
- package/dist/plugins/workflow/v2.js.map +0 -1
|
@@ -1,162 +1,310 @@
|
|
|
1
1
|
// FILE: src/plugins/analytics/index.ts
|
|
2
|
-
// VERSION: 1.0
|
|
2
|
+
// VERSION: 2.1.0
|
|
3
3
|
// START_MODULE_CONTRACT
|
|
4
|
-
// PURPOSE: Collect per-step token usage telemetry and session metadata into the analytics store with vvoc and
|
|
5
|
-
// SCOPE: Plugin
|
|
6
|
-
// DEPENDS: [@opencode
|
|
7
|
-
// LINKS: [M-PLUGIN-ANALYTICS, M-ANALYTICS-STORE, M-ANALYTICS-TYPES, M-PLUGIN-
|
|
4
|
+
// PURPOSE: Collect per-step native token usage telemetry and session metadata into the analytics store with vvoc and native host version attribution, gated per bound family by its immutable captured policy.
|
|
5
|
+
// SCOPE: Native Plugin.define entry, one native event subscription with per-event fail-soft handling, native session/step/renamed decoding, assistant-message attribution from session.step.started, usage records from session.step.ended/session.step.failed and session.compaction.ended/session.compaction.failed, buffered session records emitted once a session's family is bound, and lifecycle cleanup. No V1 event.properties.* shapes, no startup-global toggle, no fabricated zero tokens for unreported usage, and no double counting of internal session.usage.recorded or aggregate session.usage.updated. Disclosed native telemetry gap: session.generate is stateless and emits no durable usage event, so direct generate usage is not recordable from the native stream.
|
|
6
|
+
// DEPENDS: [@opencode/plugin, src/lib/plugin-toggle-config.ts, src/lib/package.ts, src/runtime/context.ts, src/runtime/types.ts, src/lib/analytics/store.ts, src/lib/analytics/types.ts]
|
|
7
|
+
// LINKS: [M-PLUGIN-ANALYTICS, M-ANALYTICS-STORE, M-ANALYTICS-TYPES, M-NATIVE-RUNTIME, V-M-PLUGIN-ANALYTICS]
|
|
8
8
|
// ROLE: RUNTIME
|
|
9
9
|
// MAP_MODE: EXPORTS
|
|
10
10
|
// END_MODULE_CONTRACT
|
|
11
11
|
//
|
|
12
12
|
// START_MODULE_MAP
|
|
13
|
-
// AnalyticsPluginDependencies - Injectable
|
|
14
|
-
// createAnalyticsPlugin - Builds
|
|
15
|
-
// AnalyticsPlugin - Default production analytics
|
|
16
|
-
// default -
|
|
13
|
+
// AnalyticsPluginDependencies - Injectable append, clock, runtime acquisition and diagnostic sink for focused tests.
|
|
14
|
+
// createAnalyticsPlugin - Builds a native OpenCode analytics plugin with injectable dependencies.
|
|
15
|
+
// AnalyticsPlugin - Default production native analytics plugin.
|
|
16
|
+
// default - Default export alias of AnalyticsPlugin.
|
|
17
17
|
// END_MODULE_MAP
|
|
18
18
|
//
|
|
19
19
|
// START_CHANGE_SUMMARY
|
|
20
|
-
// LAST_CHANGE: [C-OPENCODE-V2-
|
|
20
|
+
// LAST_CHANGE: [C-OPENCODE-V2-NATIVE T-006 attempt 2 - Added provider-reported usage from native session.step.failed and session.compaction.ended/failed with native event identity and compaction message-id projection; disclosed the stateless generate telemetry gap.]
|
|
21
21
|
// END_CHANGE_SUMMARY
|
|
22
|
-
import {
|
|
23
|
-
import { getPackageVersionSync } from "../../lib/package.js";
|
|
22
|
+
import { Plugin } from "@opencode/plugin";
|
|
24
23
|
import { isVvocPluginEnabled } from "../../lib/plugin-toggle-config.js";
|
|
24
|
+
import { getPackageVersionSync } from "../../lib/package.js";
|
|
25
|
+
import { acquireNativeSnapshotRuntime, } from "../../runtime/context.js";
|
|
25
26
|
import { appendAnalyticsRecord } from "../../lib/analytics/store.js";
|
|
26
27
|
const DEFAULT_DEPENDENCIES = {
|
|
27
|
-
enabled: async (directory) => {
|
|
28
|
-
const vvoc = await loadVvocConfig({ cwd: directory });
|
|
29
|
-
return isVvocPluginEnabled(vvoc.config, "analytics");
|
|
30
|
-
},
|
|
31
28
|
append: (record) => appendAnalyticsRecord(record),
|
|
29
|
+
now: () => new Date(),
|
|
32
30
|
};
|
|
31
|
+
// START_BLOCK_DECODERS
|
|
32
|
+
function asRecord(value) {
|
|
33
|
+
return typeof value === "object" && value !== null
|
|
34
|
+
? value
|
|
35
|
+
: undefined;
|
|
36
|
+
}
|
|
37
|
+
function readString(value) {
|
|
38
|
+
return typeof value === "string" ? value : undefined;
|
|
39
|
+
}
|
|
40
|
+
/** Native step usage tokens; every reported counter must be a finite number. */
|
|
41
|
+
function readTokens(value) {
|
|
42
|
+
const record = asRecord(value);
|
|
43
|
+
if (record === undefined)
|
|
44
|
+
return undefined;
|
|
45
|
+
const cache = asRecord(record.cache);
|
|
46
|
+
if (cache === undefined)
|
|
47
|
+
return undefined;
|
|
48
|
+
const input = record.input;
|
|
49
|
+
const output = record.output;
|
|
50
|
+
const reasoning = record.reasoning;
|
|
51
|
+
const cacheRead = cache.read;
|
|
52
|
+
const cacheWrite = cache.write;
|
|
53
|
+
for (const counter of [input, output, reasoning, cacheRead, cacheWrite]) {
|
|
54
|
+
if (typeof counter !== "number" || !Number.isFinite(counter))
|
|
55
|
+
return undefined;
|
|
56
|
+
}
|
|
57
|
+
return {
|
|
58
|
+
input: input,
|
|
59
|
+
output: output,
|
|
60
|
+
reasoning: reasoning,
|
|
61
|
+
cacheRead: cacheRead,
|
|
62
|
+
cacheWrite: cacheWrite,
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
// END_BLOCK_DECODERS
|
|
33
66
|
// START_BLOCK_CREATE_ANALYTICS_PLUGIN
|
|
34
67
|
/**
|
|
35
|
-
* Builds the analytics
|
|
36
|
-
*
|
|
37
|
-
*
|
|
38
|
-
*
|
|
39
|
-
*
|
|
40
|
-
*
|
|
41
|
-
*
|
|
42
|
-
*
|
|
68
|
+
* Builds the native analytics plugin.
|
|
69
|
+
* One event subscription records one usage record per native `session.step.ended`
|
|
70
|
+
* and session records for `session.created`/`session.renamed`. The native event
|
|
71
|
+
* envelope id is the stable usage dedupe identity (`partID`), replacing the V1
|
|
72
|
+
* step-finish part id; `messageID` is the native assistantMessageID. Agent/
|
|
73
|
+
* provider/model attribution comes from `session.step.started` keyed by
|
|
74
|
+
* assistantMessageID. All handler errors are contained per event so one
|
|
75
|
+
* transient append or decode failure never ends later collection.
|
|
43
76
|
*/
|
|
44
77
|
export function createAnalyticsPlugin(dependencies = {}) {
|
|
45
|
-
const deps = {
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
78
|
+
const deps = {
|
|
79
|
+
...DEFAULT_DEPENDENCIES,
|
|
80
|
+
...dependencies,
|
|
81
|
+
};
|
|
82
|
+
return Plugin.define({
|
|
83
|
+
id: "vvoc.analytics",
|
|
84
|
+
setup: async (ctx) => {
|
|
85
|
+
const acquire = deps.acquireRuntime ?? ((c) => acquireNativeSnapshotRuntime(c));
|
|
86
|
+
const runtime = await acquire(ctx);
|
|
87
|
+
const log = deps.log ??
|
|
88
|
+
((level, message) => console.error(`[vvoc analytics][${level}] ${message}`));
|
|
89
|
+
const vvocVersion = getPackageVersionSync();
|
|
90
|
+
const projectID = ctx.location.project.id;
|
|
91
|
+
const projectDirectory = ctx.location.directory;
|
|
92
|
+
const messageAttribution = new Map();
|
|
93
|
+
const sessionMetadata = new Map();
|
|
94
|
+
const sessionPolicy = new Map();
|
|
95
|
+
const resolveEnabled = async (sessionID) => {
|
|
96
|
+
const cached = sessionPolicy.get(sessionID);
|
|
97
|
+
if (cached !== undefined)
|
|
98
|
+
return cached;
|
|
99
|
+
const read = async () => {
|
|
100
|
+
try {
|
|
101
|
+
return await runtime.snapshots.configFor(sessionID);
|
|
102
|
+
}
|
|
103
|
+
catch {
|
|
104
|
+
return undefined;
|
|
105
|
+
}
|
|
106
|
+
};
|
|
107
|
+
let capture = await read();
|
|
108
|
+
if (capture === undefined) {
|
|
109
|
+
try {
|
|
110
|
+
await runtime.snapshots.accept({ sessionID });
|
|
111
|
+
}
|
|
112
|
+
catch {
|
|
113
|
+
// fall through to the second read; absence is unknown policy below
|
|
114
|
+
}
|
|
115
|
+
capture = await read();
|
|
62
116
|
}
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
117
|
+
if (capture === undefined)
|
|
118
|
+
return undefined;
|
|
119
|
+
const enabled = isVvocPluginEnabled(capture.vvoc, "analytics");
|
|
120
|
+
sessionPolicy.set(sessionID, enabled);
|
|
121
|
+
return enabled;
|
|
122
|
+
};
|
|
123
|
+
const emitSessionRecord = async (sessionID) => {
|
|
124
|
+
const meta = sessionMetadata.get(sessionID);
|
|
125
|
+
if (meta === undefined)
|
|
126
|
+
return;
|
|
127
|
+
const enabled = await resolveEnabled(sessionID);
|
|
128
|
+
if (enabled !== true)
|
|
129
|
+
return;
|
|
130
|
+
if (meta.emittedTitle === meta.title)
|
|
131
|
+
return;
|
|
132
|
+
const record = {
|
|
133
|
+
kind: "session",
|
|
134
|
+
ts: deps.now().toISOString(),
|
|
135
|
+
sessionID,
|
|
136
|
+
projectID,
|
|
137
|
+
title: meta.title,
|
|
138
|
+
};
|
|
139
|
+
await deps.append(record);
|
|
140
|
+
meta.emittedTitle = meta.title;
|
|
141
|
+
};
|
|
142
|
+
const handleEvent = async (event) => {
|
|
143
|
+
const type = readString(event.type);
|
|
144
|
+
const data = asRecord(event.data);
|
|
145
|
+
if (type === undefined || data === undefined)
|
|
146
|
+
return;
|
|
147
|
+
if (type === "session.created") {
|
|
148
|
+
const sessionID = readString(data.sessionID);
|
|
149
|
+
if (sessionID === undefined)
|
|
70
150
|
return;
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
151
|
+
const title = readString(data.title) ?? "";
|
|
152
|
+
sessionMetadata.set(sessionID, {
|
|
153
|
+
title,
|
|
154
|
+
version: readString(data.version),
|
|
155
|
+
emittedTitle: undefined,
|
|
156
|
+
});
|
|
157
|
+
await emitSessionRecord(sessionID);
|
|
158
|
+
return;
|
|
159
|
+
}
|
|
160
|
+
if (type === "session.renamed") {
|
|
161
|
+
const sessionID = readString(data.sessionID);
|
|
162
|
+
if (sessionID === undefined)
|
|
82
163
|
return;
|
|
164
|
+
const meta = sessionMetadata.get(sessionID);
|
|
165
|
+
const title = readString(data.title) ?? meta?.title ?? "";
|
|
166
|
+
if (meta === undefined) {
|
|
167
|
+
sessionMetadata.set(sessionID, { title, version: undefined, emittedTitle: undefined });
|
|
83
168
|
}
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
sessionOpencodeVersions.set(info.id, info.version || "unknown");
|
|
87
|
-
await deps.append({
|
|
88
|
-
kind: "session",
|
|
89
|
-
ts: new Date().toISOString(),
|
|
90
|
-
sessionID: info.id,
|
|
91
|
-
projectID: project.id,
|
|
92
|
-
title: info.title ?? "",
|
|
93
|
-
});
|
|
169
|
+
else {
|
|
170
|
+
meta.title = title;
|
|
94
171
|
}
|
|
172
|
+
await emitSessionRecord(sessionID);
|
|
173
|
+
return;
|
|
174
|
+
}
|
|
175
|
+
if (type === "session.step.started") {
|
|
176
|
+
const assistantMessageID = readString(data.assistantMessageID);
|
|
177
|
+
if (assistantMessageID === undefined)
|
|
178
|
+
return;
|
|
179
|
+
const model = asRecord(data.model);
|
|
180
|
+
messageAttribution.set(assistantMessageID, {
|
|
181
|
+
providerID: readString(model?.providerID) ?? "",
|
|
182
|
+
modelID: readString(model?.id) ?? "",
|
|
183
|
+
agent: readString(data.agent) ?? "",
|
|
184
|
+
});
|
|
185
|
+
return;
|
|
186
|
+
}
|
|
187
|
+
if (type === "session.step.ended" || type === "session.step.failed") {
|
|
188
|
+
await handleStepUsage(event, data);
|
|
189
|
+
return;
|
|
190
|
+
}
|
|
191
|
+
if (type === "session.compaction.ended" || type === "session.compaction.failed") {
|
|
192
|
+
await handleCompactionUsage(event, data);
|
|
193
|
+
}
|
|
194
|
+
};
|
|
195
|
+
const appendUsage = async (input) => {
|
|
196
|
+
const record = {
|
|
197
|
+
kind: "usage",
|
|
198
|
+
ts: deps.now().toISOString(),
|
|
199
|
+
projectID,
|
|
200
|
+
projectDirectory,
|
|
201
|
+
sessionID: input.sessionID,
|
|
202
|
+
messageID: input.messageID,
|
|
203
|
+
// Stable native observation identity: the durable event envelope id.
|
|
204
|
+
partID: input.eventId ?? input.messageID,
|
|
205
|
+
providerID: input.providerID,
|
|
206
|
+
modelID: input.modelID,
|
|
207
|
+
agent: input.agent,
|
|
208
|
+
tokens: input.tokens,
|
|
209
|
+
cost: typeof input.cost === "number" && Number.isFinite(input.cost) ? input.cost : 0,
|
|
210
|
+
vvocVersion,
|
|
211
|
+
opencodeVersion: sessionMetadata.get(input.sessionID)?.version ?? "unknown",
|
|
212
|
+
};
|
|
213
|
+
await deps.append(record);
|
|
214
|
+
};
|
|
215
|
+
/**
|
|
216
|
+
* Record one completed or failed native model step. Both boundaries carry
|
|
217
|
+
* provider-reported usage; unreported or malformed usage produces no record
|
|
218
|
+
* rather than a fabricated zero. Attribution comes from the matching
|
|
219
|
+
* `session.step.started` message id.
|
|
220
|
+
*/
|
|
221
|
+
const handleStepUsage = async (event, data) => {
|
|
222
|
+
const sessionID = readString(data.sessionID);
|
|
223
|
+
const assistantMessageID = readString(data.assistantMessageID);
|
|
224
|
+
if (sessionID === undefined || assistantMessageID === undefined)
|
|
225
|
+
return;
|
|
226
|
+
const tokens = readTokens(data.tokens);
|
|
227
|
+
if (tokens === undefined)
|
|
228
|
+
return;
|
|
229
|
+
const enabled = await resolveEnabled(sessionID);
|
|
230
|
+
if (enabled !== true)
|
|
231
|
+
return;
|
|
232
|
+
await emitSessionRecord(sessionID);
|
|
233
|
+
const attribution = messageAttribution.get(assistantMessageID);
|
|
234
|
+
await appendUsage({
|
|
235
|
+
eventId: readString(event.id),
|
|
236
|
+
sessionID,
|
|
237
|
+
messageID: assistantMessageID,
|
|
238
|
+
providerID: attribution?.providerID ?? "",
|
|
239
|
+
modelID: attribution?.modelID ?? "",
|
|
240
|
+
agent: attribution?.agent ?? "",
|
|
241
|
+
tokens,
|
|
242
|
+
cost: data.cost,
|
|
95
243
|
});
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
244
|
+
};
|
|
245
|
+
/**
|
|
246
|
+
* Record one completed or failed native compaction request. The event
|
|
247
|
+
* carries its own reported usage and, on success, the compaction model; the
|
|
248
|
+
* session-message identity is derived the same way the native projector
|
|
249
|
+
* does (`SessionMessage.ID.fromEvent`, core/session/message-updater.ts).
|
|
250
|
+
* Agent is unknown at this boundary and is reported as empty.
|
|
251
|
+
*/
|
|
252
|
+
const handleCompactionUsage = async (event, data) => {
|
|
253
|
+
const sessionID = readString(data.sessionID);
|
|
254
|
+
if (sessionID === undefined)
|
|
255
|
+
return;
|
|
256
|
+
const tokens = readTokens(data.tokens);
|
|
257
|
+
if (tokens === undefined)
|
|
258
|
+
return;
|
|
259
|
+
const enabled = await resolveEnabled(sessionID);
|
|
260
|
+
if (enabled !== true)
|
|
261
|
+
return;
|
|
262
|
+
await emitSessionRecord(sessionID);
|
|
263
|
+
const eventId = readString(event.id);
|
|
264
|
+
const messageID = eventId === undefined ? "" : eventId.replace(/^evt_/, "msg_");
|
|
265
|
+
const model = asRecord(data.model);
|
|
266
|
+
await appendUsage({
|
|
267
|
+
eventId,
|
|
268
|
+
sessionID,
|
|
269
|
+
messageID,
|
|
270
|
+
providerID: readString(model?.providerID) ?? "",
|
|
271
|
+
modelID: readString(model?.id) ?? "",
|
|
272
|
+
agent: "",
|
|
273
|
+
tokens,
|
|
274
|
+
cost: data.cost,
|
|
275
|
+
});
|
|
276
|
+
};
|
|
277
|
+
const abort = new AbortController();
|
|
278
|
+
const iterator = ctx.event.subscribe({ signal: abort.signal })[Symbol.asyncIterator]();
|
|
279
|
+
const pump = (async () => {
|
|
280
|
+
try {
|
|
281
|
+
for (;;) {
|
|
282
|
+
const next = await iterator.next();
|
|
283
|
+
if (next.done)
|
|
284
|
+
break;
|
|
285
|
+
try {
|
|
286
|
+
await handleEvent(next.value);
|
|
287
|
+
}
|
|
288
|
+
catch (error) {
|
|
289
|
+
// Contain one event's failure; later events are still observed.
|
|
290
|
+
log("warn", `collection error: ${error instanceof Error ? error.name : typeof error}`);
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
catch {
|
|
295
|
+
// A closed or overflowed native stream ends collection; cleanup aborts it.
|
|
296
|
+
}
|
|
297
|
+
})();
|
|
298
|
+
void pump;
|
|
299
|
+
return async () => {
|
|
300
|
+
abort.abort();
|
|
301
|
+
await iterator.return?.();
|
|
302
|
+
await runtime.release();
|
|
303
|
+
};
|
|
140
304
|
},
|
|
141
|
-
|
|
142
|
-
vvocVersion: input.vvocVersion,
|
|
143
|
-
opencodeVersion: input.sessionOpencodeVersions.get(part.sessionID) ?? "unknown",
|
|
144
|
-
};
|
|
145
|
-
await input.append(record);
|
|
146
|
-
}
|
|
147
|
-
/** Normalizes an untrusted token counter to a finite non-negative number. */
|
|
148
|
-
function toCount(value) {
|
|
149
|
-
return typeof value === "number" && Number.isFinite(value) && value > 0 ? value : 0;
|
|
305
|
+
});
|
|
150
306
|
}
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
import { createV2Adapter } from "../v2-runtime/setup.js";
|
|
155
|
-
import { setupAnalyticsV2 } from "./v2.js";
|
|
156
|
-
export default defineDualPlugin({
|
|
157
|
-
id: "vvoc.analytics",
|
|
158
|
-
v1: AnalyticsPlugin,
|
|
159
|
-
v2: (ctx) => setupAnalyticsV2(createV2Adapter(ctx)),
|
|
160
|
-
});
|
|
161
|
-
// END_BLOCK_DUAL_SUBPATH_ENTRY
|
|
307
|
+
export const AnalyticsPlugin = createAnalyticsPlugin();
|
|
308
|
+
export default AnalyticsPlugin;
|
|
309
|
+
// END_BLOCK_CREATE_ANALYTICS_PLUGIN
|
|
162
310
|
//# sourceMappingURL=index.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/plugins/analytics/index.ts"],"names":[],"mappings":"AAAA,uCAAuC;AACvC,iBAAiB;AACjB,wBAAwB;AACxB,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/plugins/analytics/index.ts"],"names":[],"mappings":"AAAA,uCAAuC;AACvC,iBAAiB;AACjB,wBAAwB;AACxB,iNAAiN;AAC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C,KAAK,IAAI,EAAE;gBACvB,IAAI,CAAC;oBACH,SAAS,CAAC;wBACR,MAAM,IAAI,GAAG,MAAM,QAAQ,CAAC,IAAI,EAAE,CAAC;wBACnC,IAAI,IAAI,CAAC,IAAI;4BAAE,MAAM;wBACrB,IAAI,CAAC;4BACH,MAAM,WAAW,CAAC,IAAI,CAAC,KAAyD,CAAC,CAAC;wBACpF,CAAC;wBAAC,OAAO,KAAK,EAAE,CAAC;4BACf,gEAAgE;4BAChE,GAAG,CACD,MAAM,EACN,qBAAqB,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,CAAC,OAAO,KAAK,EAAE,CAC1E,CAAC;wBACJ,CAAC;oBACH,CAAC;gBACH,CAAC;gBAAC,MAAM,CAAC;oBACP,2EAA2E;gBAC7E,CAAC;YACH,CAAC,CAAC,EAAE,CAAC;YACL,KAAK,IAAI,CAAC;YAEV,OAAO,KAAK,IAAI,EAAE;gBAChB,KAAK,CAAC,KAAK,EAAE,CAAC;gBACd,MAAM,QAAQ,CAAC,MAAM,EAAE,EAAE,CAAC;gBAC1B,MAAM,OAAO,CAAC,OAAO,EAAE,CAAC;YAC1B,CAAC,CAAC;QACJ,CAAC;KACF,CAAC,CAAC;AACL,CAAC;AAED,MAAM,CAAC,MAAM,eAAe,GAAkB,qBAAqB,EAAE,CAAC;AACtE,eAAe,eAAe,CAAC;AAC/B,oCAAoC"}
|
|
@@ -1,48 +1,69 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { type
|
|
3
|
-
|
|
4
|
-
export
|
|
5
|
-
type PermissionAskedEvent = {
|
|
6
|
-
id: string;
|
|
7
|
-
sessionID: string;
|
|
8
|
-
permission?: string;
|
|
9
|
-
patterns?: string[];
|
|
10
|
-
metadata?: Record<string, unknown>;
|
|
11
|
-
always?: string[];
|
|
12
|
-
tool?: {
|
|
13
|
-
messageID: string;
|
|
14
|
-
callID: string;
|
|
15
|
-
};
|
|
16
|
-
};
|
|
17
|
-
type ToolIntent = {
|
|
18
|
-
sessionID: string;
|
|
19
|
-
tool: string;
|
|
20
|
-
callID: string;
|
|
21
|
-
args: unknown;
|
|
22
|
-
time: number;
|
|
23
|
-
};
|
|
24
|
-
type CommandIntent = {
|
|
25
|
-
sessionID: string;
|
|
26
|
-
command: string;
|
|
27
|
-
arguments: string;
|
|
28
|
-
time: number;
|
|
29
|
-
};
|
|
30
|
-
type ActiveReview = {
|
|
31
|
-
cancelled: boolean;
|
|
32
|
-
internalReply: boolean;
|
|
33
|
-
cancel?: () => void;
|
|
34
|
-
cancellationNoticeShown: boolean;
|
|
35
|
-
};
|
|
36
|
-
type GuardianRuntimeConfig = {
|
|
1
|
+
import { Plugin } from "@opencode/plugin";
|
|
2
|
+
import { type NativeSnapshotContext, type NativeSnapshotRuntime } from "../../runtime/context.js";
|
|
3
|
+
/** Resolved Guardian thresholds, model and diagnostics provenance. */
|
|
4
|
+
export interface GuardianRuntimeConfig {
|
|
37
5
|
model?: string;
|
|
38
6
|
timeoutMs: number;
|
|
39
7
|
approvalRiskThreshold: number;
|
|
40
8
|
reviewToastDurationMs: number;
|
|
41
9
|
sources: string[];
|
|
42
10
|
warnings: string[];
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
export
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
11
|
+
}
|
|
12
|
+
/** Narrow native permission.evaluate event shape (only `effect`/`message` are mutable). */
|
|
13
|
+
export interface GuardianPermissionEvaluation {
|
|
14
|
+
readonly sessionID: string;
|
|
15
|
+
readonly agent?: string;
|
|
16
|
+
readonly action: string;
|
|
17
|
+
readonly resources: ReadonlyArray<string>;
|
|
18
|
+
readonly metadata?: Record<string, unknown>;
|
|
19
|
+
readonly source?: {
|
|
20
|
+
readonly type: "tool";
|
|
21
|
+
readonly messageID: string;
|
|
22
|
+
readonly id: string;
|
|
23
|
+
};
|
|
24
|
+
effect: "allow" | "deny" | "ask";
|
|
25
|
+
message?: string;
|
|
26
|
+
}
|
|
27
|
+
/** Bounded host-history transcript for one review. */
|
|
28
|
+
export interface GuardianReviewHistory {
|
|
29
|
+
readonly lines: string[];
|
|
30
|
+
readonly omissionNote?: string;
|
|
31
|
+
}
|
|
32
|
+
/** Resolved family policy (family id, config, policy prompt). */
|
|
33
|
+
export interface GuardianReviewPolicy {
|
|
34
|
+
readonly familyId: string;
|
|
35
|
+
readonly config: GuardianRuntimeConfig;
|
|
36
|
+
readonly prompt: string;
|
|
37
|
+
}
|
|
38
|
+
/** Injectable policy, history, inference and diagnostic seam. */
|
|
39
|
+
export interface GuardianReviewDependencies {
|
|
40
|
+
policyFor(sessionID: string): Promise<GuardianReviewPolicy | undefined>;
|
|
41
|
+
history(sessionID: string): Promise<GuardianReviewHistory>;
|
|
42
|
+
infer(input: {
|
|
43
|
+
readonly sessionID: string;
|
|
44
|
+
readonly prompt: string;
|
|
45
|
+
readonly role: string;
|
|
46
|
+
readonly timeoutMs: number;
|
|
47
|
+
}): Promise<string | undefined>;
|
|
48
|
+
log(event: {
|
|
49
|
+
readonly level: "debug" | "info" | "warn" | "error";
|
|
50
|
+
readonly message: string;
|
|
51
|
+
readonly extra?: Record<string, unknown>;
|
|
52
|
+
}): void;
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Build the native `permission.evaluate` handler. It only ever changes a
|
|
56
|
+
* resolved `"ask"` effect to `"allow"` for a bounded low-risk verdict; it never
|
|
57
|
+
* overrides a `"deny"`, never overrides an explicit `"allow"`, and leaves every
|
|
58
|
+
* uncertain, failing or invalid outcome as manual. A per-family recursion guard
|
|
59
|
+
* keeps the snapshot-bound auxiliary inference from re-reviewing itself.
|
|
60
|
+
*/
|
|
61
|
+
export declare function createGuardianEvaluateHandler(deps: GuardianReviewDependencies): (event: GuardianPermissionEvaluation) => Promise<void>;
|
|
62
|
+
export interface GuardianPluginOptions {
|
|
63
|
+
/** Test-only injectable runtime acquisition. Default acquires the real shared runtime. */
|
|
64
|
+
acquireRuntime?: (ctx: NativeSnapshotContext) => Promise<NativeSnapshotRuntime>;
|
|
65
|
+
}
|
|
66
|
+
/** Native guardian plugin factory; the default export uses the real shared runtime. */
|
|
67
|
+
export declare function createGuardianPlugin(options?: GuardianPluginOptions): Plugin.Plugin;
|
|
68
|
+
export declare const GuardianPlugin: Plugin.Plugin;
|
|
69
|
+
export default GuardianPlugin;
|