maka-agent 0.2.0-dev.10.20260831 → 0.2.0-dev.12.20260901
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 +49 -2
- package/RUNTIME_HOST_PEER_DEPENDENCIES.rust.tsv +70 -0
- package/RUNTIME_HOST_PEER_THIRD_PARTY_NOTICES.txt +1839 -140
- package/dist/cli-core.js +25 -1
- package/dist/mcp-capability-provider.js +1 -1
- package/dist/onboarding-catalog.js +3 -5
- package/dist/pi-transcript.js +21 -17
- package/dist/pi-tui-layout.js +4 -2
- package/dist/pi-tui-mcp-status.js +115 -122
- package/dist/pi-tui-pickers.js +11 -5
- package/dist/pi-tui-runner.js +133 -102
- package/dist/runtime-host-cli-context.js +4 -0
- package/dist/runtime-host-cli.js +150 -4
- package/dist/runtime-host-lifecycle-transaction.js +5 -2
- package/dist/runtime-host-managed-lifecycle-manager.js +1 -0
- package/dist/runtime-host-onboarding.js +12 -16
- package/dist/runtime-host-peer-management-command.js +16 -4
- package/dist/runtime-host-plugin-command.js +101 -0
- package/dist/runtime-host-run-command.js +5 -1
- package/dist/runtime-host-service-management-command.js +2 -1
- package/dist/runtime-host-setup-command.js +1 -0
- package/dist/runtime-host-task-readiness.js +2 -3
- package/dist/runtime-host-tui-command.js +1 -1
- package/dist/runtime-host-tui-context.js +34 -6
- package/dist/runtime-host-update-command.js +13 -5
- package/dist/runtime-host-update-discovery.js +1 -0
- package/dist/tui-clipboard.js +86 -0
- package/dist/tui-copy-catalog.js +256 -0
- package/dist/tui-copy-command.js +103 -0
- package/dist/tui-mcp-control.js +46 -4
- package/dist/tui-mcp-remote-publication.js +395 -0
- package/native/runtime-host-peer/prebuilds/darwin-arm64/maka_runtime_host_peer.node +0 -0
- package/native/runtime-host-peer/prebuilds/linux-arm64/maka_runtime_host_peer.node +0 -0
- package/native/runtime-host-peer/prebuilds/linux-x64/maka_runtime_host_peer.node +0 -0
- package/native/runtime-host-peer/prebuilds/win32-x64/maka_runtime_host_peer.node +0 -0
- package/node_modules/@maka/core/dist/agent-run.js +5 -0
- package/node_modules/@maka/core/dist/attachments.js +80 -1
- package/node_modules/@maka/core/dist/bounded-response.js +71 -0
- package/node_modules/@maka/core/dist/chat-model-choice.js +8 -31
- package/node_modules/@maka/core/dist/client-capability-grant.js +149 -0
- package/node_modules/@maka/core/dist/connection-readiness.js +3 -31
- package/node_modules/@maka/core/dist/deep-research.js +1 -20
- package/node_modules/@maka/core/dist/events.js +22 -1
- package/node_modules/@maka/core/dist/execution-inspect.js +3 -1
- package/node_modules/@maka/core/dist/foreign-session.js +2 -2
- package/node_modules/@maka/core/dist/interaction.js +49 -2
- package/node_modules/@maka/core/dist/llm-connections.js +74 -82
- package/node_modules/@maka/core/dist/model-call-attempt.js +66 -0
- package/node_modules/@maka/core/dist/model-catalog.js +238 -313
- package/node_modules/@maka/core/dist/model-facts.js +280 -0
- package/node_modules/@maka/core/dist/model-metadata.generated.js +1907 -1872
- package/node_modules/@maka/core/dist/model-metadata.js +184 -199
- package/node_modules/@maka/core/dist/model-projection-transition.js +145 -0
- package/node_modules/@maka/core/dist/model-thinking.js +1 -1
- package/node_modules/@maka/core/dist/models-dev-projection.js +302 -0
- package/node_modules/@maka/core/dist/models-dev-refresh.js +55 -0
- package/node_modules/@maka/core/dist/onboarding.js +2 -1
- package/node_modules/@maka/core/dist/provider-auth.js +47 -291
- package/node_modules/@maka/core/dist/provider-registry.js +68 -357
- package/node_modules/@maka/core/dist/runtime-boundary.js +2 -2
- package/node_modules/@maka/core/dist/runtime-event.js +5 -1
- package/node_modules/@maka/core/dist/runtime-policy/connection-catalog-codec.js +82 -5
- package/node_modules/@maka/core/dist/runtime-policy/model-catalog-entry-codec.js +75 -0
- package/node_modules/@maka/core/dist/runtime-policy.js +2 -1
- package/node_modules/@maka/core/dist/session-send-projection.js +2 -1
- package/node_modules/@maka/core/dist/session-start-mode.js +42 -0
- package/node_modules/@maka/core/dist/session-todo.js +101 -0
- package/node_modules/@maka/core/dist/session.js +119 -5
- package/node_modules/@maka/core/dist/slash-command-catalog.js +1 -0
- package/node_modules/@maka/core/dist/task-submission-readiness.js +2 -1
- package/node_modules/@maka/core/dist/tool-activity-args.js +4 -0
- package/node_modules/@maka/core/dist/tool-quiet-preview.js +5 -5
- package/node_modules/@maka/core/dist/tool-result-record-schema.js +2 -1
- package/node_modules/@maka/core/dist/workhub-creation-intent.js +896 -0
- package/node_modules/@maka/core/package.json +9 -1
- package/node_modules/@maka/runtime/dist/active-tool-result-prune.js +45 -131
- package/node_modules/@maka/runtime/dist/agent-run.js +58 -44
- package/node_modules/@maka/runtime/dist/ai-sdk-backend.js +146 -201
- package/node_modules/@maka/runtime/dist/ai-sdk-compaction.js +235 -69
- package/node_modules/@maka/runtime/dist/context-budget-helpers.js +4 -41
- package/node_modules/@maka/runtime/dist/context-budget-policy.js +8 -1
- package/node_modules/@maka/runtime/dist/context-budget.js +9 -81
- package/node_modules/@maka/runtime/dist/context-diagnostics.js +63 -27
- package/node_modules/@maka/runtime/dist/continuation-replay.js +19 -11
- package/node_modules/@maka/runtime/dist/conversation-copy.js +179 -16
- package/node_modules/@maka/runtime/dist/deep-research-tools.js +2 -3
- package/node_modules/@maka/runtime/dist/durable-tool-result-projection.js +21 -0
- package/node_modules/@maka/runtime/dist/execution-inspect.js +13 -3
- package/node_modules/@maka/runtime/dist/history-compact-checkpoint.js +58 -7
- package/node_modules/@maka/runtime/dist/history-compact-ledger.js +9 -1
- package/node_modules/@maka/runtime/dist/history-compact-summarizer.js +2 -2
- package/node_modules/@maka/runtime/dist/history-compact-summary-validation.js +2 -1
- package/node_modules/@maka/runtime/dist/history-compaction.js +2 -1
- package/node_modules/@maka/runtime/dist/image-file.js +5 -20
- package/node_modules/@maka/runtime/dist/latest-context-snapshot.js +114 -20
- package/node_modules/@maka/runtime/dist/local-web-fetch.js +2 -43
- package/node_modules/@maka/runtime/dist/mcp-tools.js +28 -0
- package/node_modules/@maka/runtime/dist/model-adapter.js +47 -15
- package/node_modules/@maka/runtime/dist/model-factory.js +7 -1
- package/node_modules/@maka/runtime/dist/model-fetcher.js +15 -8
- package/node_modules/@maka/runtime/dist/model-history.js +125 -3
- package/node_modules/@maka/runtime/dist/model-projection-transition-ledger.js +207 -0
- package/node_modules/@maka/runtime/dist/model-runtime.js +7 -4
- package/node_modules/@maka/runtime/dist/openai-codex-history-compactor.js +12 -14
- package/node_modules/@maka/runtime/dist/plugin-composition-loader.js +173 -20
- package/node_modules/@maka/runtime/dist/plugin-runtime.js +219 -0
- package/node_modules/@maka/runtime/dist/prompt-composition.js +33 -21
- package/node_modules/@maka/runtime/dist/provider-request-telemetry.js +35 -74
- package/node_modules/@maka/runtime/dist/quiescent-session-snapshot.js +78 -0
- package/node_modules/@maka/runtime/dist/request-shape.js +256 -343
- package/node_modules/@maka/runtime/dist/run-trace.js +2 -24
- package/node_modules/@maka/runtime/dist/runtime-continuation-admission.js +1 -1
- package/node_modules/@maka/runtime/dist/runtime-event-backfill.js +3 -0
- package/node_modules/@maka/runtime/dist/runtime-event-read-model.js +1 -0
- package/node_modules/@maka/runtime/dist/runtime-kernel.js +77 -23
- package/node_modules/@maka/runtime/dist/runtime-resume.js +20 -11
- package/node_modules/@maka/runtime/dist/session-event-runtime-mapper.js +8 -4
- package/node_modules/@maka/runtime/dist/session-manager.js +45 -9
- package/node_modules/@maka/runtime/dist/session-recap.js +2 -1
- package/node_modules/@maka/runtime/dist/session-todo-tools.js +67 -0
- package/node_modules/@maka/runtime/dist/subagent-tools.js +2 -77
- package/node_modules/@maka/runtime/dist/system-prompt/main-session-prompt.js +1 -1
- package/node_modules/@maka/runtime/dist/telemetry/model-pricing.generated.js +93 -83
- package/node_modules/@maka/runtime/dist/test-connection.js +6 -6
- package/node_modules/@maka/runtime/dist/tool-result-archive-transition.js +227 -0
- package/node_modules/@maka/runtime/dist/tool-result-archive.js +40 -168
- package/node_modules/@maka/runtime/dist/tool-runtime.js +50 -6
- package/node_modules/@maka/runtime/dist/workers/filesystem-worker.js +39 -16
- package/node_modules/@maka/runtime/package.json +2 -1
- package/node_modules/@maka/runtime-host/dist/adapter/session-projector.js +10 -0
- package/node_modules/@maka/runtime-host/dist/client/catalog-reader.js +19 -5
- package/node_modules/@maka/runtime-host/dist/client/client-capability-channel.js +3 -2
- package/node_modules/@maka/runtime-host/dist/client/connection.js +22 -0
- package/node_modules/@maka/runtime-host/dist/client/host-profile.js +222 -19
- package/node_modules/@maka/runtime-host/dist/client/index.js +2 -1
- package/node_modules/@maka/runtime-host/dist/client/oauth-presentation.js +1 -1
- package/node_modules/@maka/runtime-host/dist/client/peer-client.js +4 -0
- package/node_modules/@maka/runtime-host/dist/client/reconnecting-connection.js +8 -0
- package/node_modules/@maka/runtime-host/dist/operator/index.js +2 -1
- package/node_modules/@maka/runtime-host/dist/operator/managed-deployment.js +2 -0
- package/node_modules/@maka/runtime-host/dist/operator/peer-management-frame.js +2 -0
- package/node_modules/@maka/runtime-host/dist/operator/service-management-frame.js +2 -0
- package/node_modules/@maka/runtime-host/dist/peer-mesh/owner.js +1 -0
- package/node_modules/@maka/runtime-host/dist/protocol/client-capability.js +22 -1
- package/node_modules/@maka/{storage/dist/task-ledger-store-internal.js → runtime-host/dist/protocol/connection-catalog-change.js} +7 -13
- package/node_modules/@maka/runtime-host/dist/protocol/index.js +43 -2
- package/node_modules/@maka/runtime-host/dist/protocol/operations.js +7 -3
- package/node_modules/@maka/runtime-host/dist/protocol/plugin-platform.js +512 -0
- package/node_modules/@maka/runtime-host/dist/protocol/runtime-policy.js +24 -12
- package/node_modules/@maka/runtime-host/dist/protocol/session-catalog.js +1 -1
- package/node_modules/@maka/runtime-host/dist/protocol/session-continuity.js +1 -1
- package/node_modules/@maka/runtime-host/dist/protocol/session-retirement.js +30 -3
- package/node_modules/@maka/runtime-host/dist/protocol/session-todo.js +53 -0
- package/node_modules/@maka/runtime-host/dist/protocol/turn.js +4 -1
- package/node_modules/@maka/runtime-host/dist/protocol/workhub-coordination.js +92 -4
- package/node_modules/@maka/runtime-host/dist/server/access-authority.js +15 -13
- package/node_modules/@maka/runtime-host/dist/server/access-credential-metadata.js +4 -2
- package/node_modules/@maka/runtime-host/dist/server/access-credential-store.js +119 -47
- package/node_modules/@maka/runtime-host/dist/server/bootstrap-runtime-policy.js +10 -4
- package/node_modules/@maka/runtime-host/dist/server/child-agent-composition.js +1 -1
- package/node_modules/@maka/runtime-host/dist/server/client-capability-coordinator.js +192 -18
- package/node_modules/@maka/runtime-host/dist/server/client-capability-invocation-broker.js +122 -48
- package/node_modules/@maka/runtime-host/dist/server/client-capability-provider-id.js +31 -0
- package/node_modules/@maka/runtime-host/dist/server/connection-effect-coordinator.js +6 -5
- package/node_modules/@maka/runtime-host/dist/server/connection-session.js +1 -0
- package/node_modules/@maka/runtime-host/dist/server/deep-research-coordinator.js +1 -1
- package/node_modules/@maka/runtime-host/dist/server/execution-composition.js +163 -66
- package/node_modules/@maka/runtime-host/dist/server/execution-model-authority.js +27 -9
- package/node_modules/@maka/runtime-host/dist/server/execution-model-composition.js +38 -43
- package/node_modules/@maka/runtime-host/dist/server/extension-bundle.js +249 -0
- package/node_modules/@maka/runtime-host/dist/server/extension-package-manifest.js +262 -0
- package/node_modules/@maka/runtime-host/dist/server/goal-coordinator.js +0 -23
- package/node_modules/@maka/runtime-host/dist/server/host-change-feed.js +11 -0
- package/node_modules/@maka/runtime-host/dist/server/index.js +5 -0
- package/node_modules/@maka/runtime-host/dist/server/interaction-coordinator.js +181 -13
- package/node_modules/@maka/runtime-host/dist/server/interaction-projection.js +6 -0
- package/node_modules/@maka/runtime-host/dist/server/interactive-run-composer.js +7 -9
- package/node_modules/@maka/runtime-host/dist/server/message-coordinator.js +88 -22
- package/node_modules/@maka/runtime-host/dist/server/model-metadata-refresh.js +96 -0
- package/node_modules/@maka/runtime-host/dist/server/operation-dispatcher.js +43 -118
- package/node_modules/@maka/runtime-host/dist/server/plugin-composition-patch.js +58 -0
- package/node_modules/@maka/runtime-host/dist/server/plugin-composition-store.js +141 -0
- package/node_modules/@maka/runtime-host/dist/server/plugin-package-loader.js +130 -0
- package/node_modules/@maka/runtime-host/dist/server/plugin-package-store.js +528 -0
- package/node_modules/@maka/runtime-host/dist/server/plugin-platform-coordinator.js +249 -0
- package/node_modules/@maka/runtime-host/dist/server/plugin-platform.js +1130 -0
- package/node_modules/@maka/runtime-host/dist/server/root-admission-owner.js +4 -0
- package/node_modules/@maka/runtime-host/dist/server/root-turn-coordinator.js +24 -5
- package/node_modules/@maka/runtime-host/dist/server/runtime-policy-coordinator.js +31 -2
- package/node_modules/@maka/runtime-host/dist/server/session-catalog-coordinator.js +2 -1
- package/node_modules/@maka/runtime-host/dist/server/session-retirement-coordinator.js +58 -9
- package/node_modules/@maka/runtime-host/dist/server/session-revision-coordinator.js +30 -13
- package/node_modules/@maka/runtime-host/dist/server/session-sidecar-purge.js +1 -1
- package/node_modules/@maka/runtime-host/dist/server/session-todo-coordinator.js +77 -0
- package/node_modules/@maka/runtime-host/dist/server/session-transcript-pager.js +10 -2
- package/node_modules/@maka/runtime-host/dist/server/web-fetch-tool.js +1 -1
- package/node_modules/@maka/runtime-host/dist/server/workhub-coordination-action-gate.js +276 -3
- package/node_modules/@maka/runtime-host/dist/server/workhub-coordination-coordinator.js +101 -1
- package/node_modules/@maka/runtime-host/dist/transport/peer-native.js +1 -0
- package/node_modules/@maka/runtime-host/dist/webrtc-stun-policy.js +114 -0
- package/node_modules/@maka/runtime-host/package.json +2 -0
- package/node_modules/@maka/storage/dist/agent-run-store.js +80 -15
- package/node_modules/@maka/storage/dist/artifact-attachments.js +29 -6
- package/node_modules/@maka/storage/dist/artifact-store.js +7 -23
- package/node_modules/@maka/storage/dist/artifact-stores.js +13 -2
- package/node_modules/@maka/storage/dist/conversation-operational-state.js +3 -0
- package/node_modules/@maka/storage/dist/credential-store.js +3 -0
- package/node_modules/@maka/storage/dist/execution-stores.js +4 -0
- package/node_modules/@maka/storage/dist/file-lifetime-owner.js +30 -2
- package/node_modules/@maka/storage/dist/interaction-store-public.js +1 -1
- package/node_modules/@maka/storage/dist/interaction-store.js +173 -0
- package/node_modules/@maka/storage/dist/model-facts-store.js +79 -0
- package/node_modules/@maka/storage/dist/production-session-snapshot.js +761 -0
- package/node_modules/@maka/storage/dist/quiescent-session-snapshot.js +1 -1
- package/node_modules/@maka/storage/dist/runtime-policy/connection-catalog-document.js +26 -26
- package/node_modules/@maka/storage/dist/runtime-policy/coordinator.js +114 -56
- package/node_modules/@maka/storage/dist/runtime-policy/document-io.js +22 -16
- package/node_modules/@maka/storage/dist/runtime-policy/onboarding-transaction.js +2 -2
- package/node_modules/@maka/storage/dist/runtime-policy-stores.js +1 -1
- package/node_modules/@maka/storage/dist/session-message-projection.js +1 -1
- package/node_modules/@maka/storage/dist/session-store.js +32 -6
- package/node_modules/@maka/storage/dist/{task-ledger-authority.js → session-todo-authority.js} +13 -28
- package/node_modules/@maka/storage/dist/session-todo-store.js +169 -0
- package/node_modules/@maka/storage/dist/sqlite-core-execution-schema.js +19 -0
- package/node_modules/@maka/storage/dist/sqlite-runtime-schema.js +42 -1
- package/node_modules/@maka/storage/dist/sqlite-session-metadata-schema.js +10 -1
- package/node_modules/@maka/storage/dist/sqlite-session-metadata-store.js +73 -3
- package/node_modules/@maka/storage/dist/sqlite-workflow-schema.js +5 -8
- package/node_modules/@maka/storage/dist/storage-writer-composition.js +3 -3
- package/node_modules/@maka/storage/package.json +3 -1
- package/package.json +1 -1
- package/node_modules/@maka/core/dist/task-ledger.js +0 -669
- package/node_modules/@maka/runtime/dist/task-ledger-tools.js +0 -244
- package/node_modules/@maka/runtime/dist/test-helpers.js +0 -61
- package/node_modules/@maka/runtime-host/dist/protocol/task-ledger.js +0 -321
- package/node_modules/@maka/runtime-host/dist/server/task-ledger-coordinator.js +0 -154
- package/node_modules/@maka/storage/dist/task-ledger-store.js +0 -663
|
@@ -26,19 +26,22 @@ import { buildLlmHistorySummarizer } from '@maka/runtime/history-compact-summari
|
|
|
26
26
|
import { buildOpenAiCodexHistoryCompactor, withOpenAiCodexHistoryCompactionFallback, } from '@maka/runtime/openai-codex-history-compactor';
|
|
27
27
|
import { buildPricingLookup, recordToolInvocation } from '@maka/runtime/telemetry';
|
|
28
28
|
import { buildProviderOptions, getAIModel } from '@maka/runtime/model-factory';
|
|
29
|
-
import { createProviderRequestCaptureRecorder } from '@maka/runtime/provider-request-telemetry';
|
|
30
29
|
import { createProxiedFetchTransport, } from '@maka/runtime/network/scoped-fetch-transport';
|
|
31
30
|
import { stableHash, toolCatalogHash } from '@maka/runtime/request-shape';
|
|
32
31
|
import { toolAvailabilityHash } from '@maka/runtime/tool-availability';
|
|
33
32
|
import { createAttachmentByteReader, createReadImageSnapshotPlanner, persistProviderRequestCaptureArtifact, } from '@maka/storage/artifact-stores';
|
|
34
33
|
import { createReadImageSnapshotReader } from '@maka/storage/read-image-snapshot-store';
|
|
35
34
|
import { createHostOAuthModelFetch, } from './oauth-execution-authority.js';
|
|
36
|
-
import { readDuringBackendCreation, resolveExecutionTarget } from './execution-model-authority.js';
|
|
35
|
+
import { readDuringBackendCreation, resolveExecutionTarget, } from './execution-model-authority.js';
|
|
37
36
|
import { toRuntimePolicyProxy } from './runtime-policy-proxy.js';
|
|
38
37
|
/** Builds one real provider backend from canonical Host state. */
|
|
39
38
|
export async function createHostAiSdkBackend(input) {
|
|
40
39
|
const createFetchTransport = input.createFetchTransport ?? createProxiedFetchTransport;
|
|
41
40
|
const target = await readDuringBackendCreation(() => resolveExecutionTarget(input.context.header, input.runtimePolicy, input.oauthCredentials, createFetchTransport), input.context.abortSignal);
|
|
41
|
+
return await buildHostAiSdkBackend(input, target);
|
|
42
|
+
}
|
|
43
|
+
async function buildHostAiSdkBackend(input, target) {
|
|
44
|
+
const createFetchTransport = input.createFetchTransport ?? createProxiedFetchTransport;
|
|
42
45
|
const pricingSnapshot = await readDuringBackendCreation(() => input.usage.pricing.snapshot(), input.context.abortSignal);
|
|
43
46
|
const pricing = buildPricingLookup(pricingSnapshot.overrides);
|
|
44
47
|
const runtimePolicySnapshot = await readDuringBackendCreation(() => input.runtimePolicy.runtimePolicy.getSnapshot(), input.context.abortSignal);
|
|
@@ -96,15 +99,18 @@ export async function createHostAiSdkBackend(input) {
|
|
|
96
99
|
resolveModel: resolveHistoryCompactModel,
|
|
97
100
|
providerOptions,
|
|
98
101
|
});
|
|
99
|
-
const summarizeHistoryCompact = target.connection.providerType === 'openai-codex'
|
|
102
|
+
const summarizeHistoryCompact = target.connection.providerType === 'openai-codex' && input.context.header.llmConnectionId
|
|
100
103
|
? withOpenAiCodexHistoryCompactionFallback(buildOpenAiCodexHistoryCompactor({
|
|
101
104
|
resolveModel: resolveHistoryCompactModel,
|
|
102
|
-
|
|
105
|
+
connectionId: input.context.header.llmConnectionId,
|
|
106
|
+
providerStateIdentity: target.providerStateIdentity,
|
|
103
107
|
modelId: target.model,
|
|
104
108
|
providerOptions,
|
|
105
109
|
}), textHistorySummarizer)
|
|
106
110
|
: textHistorySummarizer;
|
|
107
|
-
const historyCompactRoute = target.connection.providerType === 'openai-codex'
|
|
111
|
+
const historyCompactRoute = target.connection.providerType === 'openai-codex' && input.context.header.llmConnectionId
|
|
112
|
+
? 'provider_native'
|
|
113
|
+
: 'text_summary';
|
|
108
114
|
let telemetryDrainRequested = false;
|
|
109
115
|
const persistTelemetry = async (operation) => {
|
|
110
116
|
try {
|
|
@@ -164,33 +170,17 @@ export async function createHostAiSdkBackend(input) {
|
|
|
164
170
|
throw new Error('Canonical model-call accounting authority is unavailable');
|
|
165
171
|
}
|
|
166
172
|
};
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
now: Date.now(),
|
|
179
|
-
});
|
|
180
|
-
return { artifactId: artifact.id };
|
|
181
|
-
}
|
|
182
|
-
catch (error) {
|
|
183
|
-
if (!artifactDrainRequested) {
|
|
184
|
-
artifactDrainRequested = true;
|
|
185
|
-
input.requestDrain();
|
|
186
|
-
}
|
|
187
|
-
throw error;
|
|
188
|
-
}
|
|
189
|
-
},
|
|
190
|
-
recordLedger: input.context.recordProviderRequestCapture,
|
|
191
|
-
})
|
|
192
|
-
: undefined;
|
|
193
|
-
const recordProviderRequestAttempt = input.context.recordProviderRequestAttempt ?? (() => { });
|
|
173
|
+
const persistPreparedRequestArtifact = async (capture) => {
|
|
174
|
+
const artifact = await persistProviderRequestCaptureArtifact(input.artifacts, {
|
|
175
|
+
sessionId: input.context.sessionId,
|
|
176
|
+
turnId: capture.turnId,
|
|
177
|
+
captureId: capture.captureId,
|
|
178
|
+
step: capture.step,
|
|
179
|
+
serializedRequest: capture.serializedRequest,
|
|
180
|
+
now: Date.now(),
|
|
181
|
+
});
|
|
182
|
+
return { artifactId: artifact.id };
|
|
183
|
+
};
|
|
194
184
|
const resolveRunPrompt = async (context) => {
|
|
195
185
|
const resolved = await modelComposition.resolveSystemPrompt({
|
|
196
186
|
sessionId: input.context.sessionId,
|
|
@@ -223,7 +213,7 @@ export async function createHostAiSdkBackend(input) {
|
|
|
223
213
|
}));
|
|
224
214
|
}
|
|
225
215
|
: undefined;
|
|
226
|
-
const planProjectionImage = createReadImageSnapshotPlanner(input.artifacts);
|
|
216
|
+
const planProjectionImage = createReadImageSnapshotPlanner(input.artifacts, (sessionId, artifactId) => input.artifacts.deleteOwnedArtifactInSession(sessionId, artifactId, 'tool_result_projection'));
|
|
227
217
|
try {
|
|
228
218
|
return new HostAiSdkBackend({
|
|
229
219
|
sessionId: input.context.sessionId,
|
|
@@ -249,6 +239,7 @@ export async function createHostAiSdkBackend(input) {
|
|
|
249
239
|
}
|
|
250
240
|
: {}),
|
|
251
241
|
connection: target.connection,
|
|
242
|
+
providerStateIdentity: target.providerStateIdentity,
|
|
252
243
|
apiKey,
|
|
253
244
|
modelId: target.model,
|
|
254
245
|
modelFactory,
|
|
@@ -301,6 +292,8 @@ export async function createHostAiSdkBackend(input) {
|
|
|
301
292
|
summarizeHistoryCompact,
|
|
302
293
|
historyCompactRoute,
|
|
303
294
|
recordHistoryCompactCheckpoint: input.context.recordHistoryCompactCheckpoint,
|
|
295
|
+
loadModelProjectionTransitions: input.context.loadModelProjectionTransitions,
|
|
296
|
+
recordModelProjectionTransition: input.context.recordModelProjectionTransition,
|
|
304
297
|
loadTurnRuntimeEvents: input.context.loadTurnRuntimeEvents,
|
|
305
298
|
allowMidTurnHistoryCompaction: input.context.allowMidTurnHistoryCompaction,
|
|
306
299
|
recordRunTrace: input.context.recordRunTrace,
|
|
@@ -321,18 +314,9 @@ export async function createHostAiSdkBackend(input) {
|
|
|
321
314
|
lookupPricing: pricing,
|
|
322
315
|
recordModelCallAttempt,
|
|
323
316
|
assertModelCallAccountingReady,
|
|
317
|
+
persistPreparedRequestArtifact,
|
|
324
318
|
recordToolInvocation: (event) => recordToolInvocation({ repo: telemetry }, event),
|
|
325
319
|
...(input.runtimeCommitSink ? { runtimeCommitSink: input.runtimeCommitSink } : {}),
|
|
326
|
-
...(providerRequestCapture
|
|
327
|
-
? {
|
|
328
|
-
recordProviderRequestCapture: providerRequestCapture,
|
|
329
|
-
...(input.context.recordProviderRequestAttempt
|
|
330
|
-
? {
|
|
331
|
-
recordProviderRequestAttempt,
|
|
332
|
-
}
|
|
333
|
-
: {}),
|
|
334
|
-
}
|
|
335
|
-
: {}),
|
|
336
320
|
newId: randomUUID,
|
|
337
321
|
now: Date.now,
|
|
338
322
|
}, transport.close, () => modelComposition.release?.());
|
|
@@ -347,6 +331,17 @@ export async function createHostAiSdkBackend(input) {
|
|
|
347
331
|
throw error;
|
|
348
332
|
}
|
|
349
333
|
}
|
|
334
|
+
export async function prepareHostAiSdkBackend(input) {
|
|
335
|
+
const createFetchTransport = input.createFetchTransport ?? createProxiedFetchTransport;
|
|
336
|
+
const preparedTarget = await readDuringBackendCreation(() => resolveExecutionTarget(input.context.header, input.runtimePolicy, input.oauthCredentials, createFetchTransport), input.context.abortSignal);
|
|
337
|
+
return {
|
|
338
|
+
providerStateIdentity: preparedTarget.providerStateIdentity,
|
|
339
|
+
build: (context) => buildHostAiSdkBackend({
|
|
340
|
+
...input,
|
|
341
|
+
context,
|
|
342
|
+
}, preparedTarget),
|
|
343
|
+
};
|
|
344
|
+
}
|
|
350
345
|
class HostAiSdkBackend extends AiSdkBackend {
|
|
351
346
|
closeTransport;
|
|
352
347
|
releaseClientCapabilities;
|
|
@@ -0,0 +1,249 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Licensed to the Apache Software Foundation (ASF) under one
|
|
3
|
+
* or more contributor license agreements. See the NOTICE file
|
|
4
|
+
* distributed with this work for additional information
|
|
5
|
+
* regarding copyright ownership. The ASF licenses this file
|
|
6
|
+
* to you under the Apache License, Version 2.0 (the
|
|
7
|
+
* "License"); you may not use this file except in compliance
|
|
8
|
+
* with the License. You may obtain a copy of the License at
|
|
9
|
+
*
|
|
10
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
11
|
+
*
|
|
12
|
+
* Unless required by applicable law or agreed to in writing,
|
|
13
|
+
* software distributed under the License is distributed on an
|
|
14
|
+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
15
|
+
* KIND, either express or implied. See the License for the
|
|
16
|
+
* specific language governing permissions and limitations
|
|
17
|
+
* under the License.
|
|
18
|
+
*/
|
|
19
|
+
import { createHash, randomUUID } from 'node:crypto';
|
|
20
|
+
import { constants } from 'node:fs';
|
|
21
|
+
import { copyFile, mkdir, open, readdir, realpath, rm, stat } from 'node:fs/promises';
|
|
22
|
+
import { dirname, isAbsolute, join, posix, resolve } from 'node:path';
|
|
23
|
+
const MAX_FILES = 256;
|
|
24
|
+
const MAX_FILE_BYTES = 8 * 1024 * 1024;
|
|
25
|
+
const MAX_BUNDLE_BYTES = 16 * 1024 * 1024;
|
|
26
|
+
const BUNDLE_IMPORTS_DIRECTORY = 'bundle-imports-v1';
|
|
27
|
+
export class ExtensionBundleError extends Error {
|
|
28
|
+
name = 'ExtensionBundleError';
|
|
29
|
+
constructor(message, options) {
|
|
30
|
+
super(message, options);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
export async function exportExtensionBundle(sourceRoot, targetPath) {
|
|
34
|
+
if (!isAbsolute(targetPath))
|
|
35
|
+
throw invalid('Extension bundle targetPath must be absolute');
|
|
36
|
+
const files = await readDirectory(sourceRoot);
|
|
37
|
+
const document = Object.freeze({
|
|
38
|
+
schemaVersion: 1,
|
|
39
|
+
digest: extensionPackageContentDigest(files),
|
|
40
|
+
files: Object.freeze(files.map((file) => Object.freeze({
|
|
41
|
+
path: file.path,
|
|
42
|
+
sha256: createHash('sha256').update(file.content).digest('hex'),
|
|
43
|
+
content: file.content.toString('base64'),
|
|
44
|
+
}))),
|
|
45
|
+
});
|
|
46
|
+
const encoded = Buffer.from(`${JSON.stringify(document)}\n`, 'utf8');
|
|
47
|
+
if (encoded.byteLength > MAX_BUNDLE_BYTES * 2)
|
|
48
|
+
throw invalid('Encoded Extension bundle is too large');
|
|
49
|
+
await mkdir(dirname(targetPath), { recursive: true, mode: 0o700 });
|
|
50
|
+
const temporary = `${targetPath}.${randomUUID()}.tmp`;
|
|
51
|
+
let handle;
|
|
52
|
+
try {
|
|
53
|
+
handle = await open(temporary, 'wx', 0o600);
|
|
54
|
+
await handle.writeFile(encoded);
|
|
55
|
+
await handle.sync();
|
|
56
|
+
await handle.close();
|
|
57
|
+
handle = undefined;
|
|
58
|
+
await copyFile(temporary, targetPath, constants.COPYFILE_EXCL);
|
|
59
|
+
}
|
|
60
|
+
catch (error) {
|
|
61
|
+
throw invalid('Unable to export Extension bundle', error);
|
|
62
|
+
}
|
|
63
|
+
finally {
|
|
64
|
+
await handle?.close().catch(() => undefined);
|
|
65
|
+
await rm(temporary, { force: true }).catch(() => undefined);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
export async function materializeExtensionPackage(sourcePath, controlDirectory) {
|
|
69
|
+
if (!isAbsolute(sourcePath))
|
|
70
|
+
throw invalid('Extension package sourcePath must be absolute');
|
|
71
|
+
const canonical = await realpath(resolve(sourcePath)).catch((error) => {
|
|
72
|
+
throw invalid('Extension package source is unavailable', error);
|
|
73
|
+
});
|
|
74
|
+
const metadata = await stat(canonical);
|
|
75
|
+
if (metadata.isDirectory())
|
|
76
|
+
return { root: canonical, dispose: async () => undefined };
|
|
77
|
+
if (!metadata.isFile())
|
|
78
|
+
throw invalid('Extension package source must be a directory or bundle file');
|
|
79
|
+
if (metadata.size > MAX_BUNDLE_BYTES * 2)
|
|
80
|
+
throw invalid('Extension bundle exceeds its size limit');
|
|
81
|
+
const handle = await open(canonical, constants.O_RDONLY | constants.O_NOFOLLOW);
|
|
82
|
+
let document;
|
|
83
|
+
try {
|
|
84
|
+
document = decodeBundle(JSON.parse((await handle.readFile()).toString('utf8')));
|
|
85
|
+
}
|
|
86
|
+
catch (error) {
|
|
87
|
+
if (error instanceof ExtensionBundleError)
|
|
88
|
+
throw error;
|
|
89
|
+
throw invalid('Extension bundle is invalid', error);
|
|
90
|
+
}
|
|
91
|
+
finally {
|
|
92
|
+
await handle.close();
|
|
93
|
+
}
|
|
94
|
+
const imports = join(controlDirectory, BUNDLE_IMPORTS_DIRECTORY);
|
|
95
|
+
const root = join(imports, randomUUID());
|
|
96
|
+
await mkdir(root, { recursive: true, mode: 0o700 });
|
|
97
|
+
try {
|
|
98
|
+
for (const file of document.files) {
|
|
99
|
+
const target = join(root, ...file.path.split('/'));
|
|
100
|
+
await mkdir(dirname(target), { recursive: true, mode: 0o700 });
|
|
101
|
+
const output = await open(target, 'wx', 0o600);
|
|
102
|
+
try {
|
|
103
|
+
await output.writeFile(Buffer.from(file.content, 'base64'));
|
|
104
|
+
}
|
|
105
|
+
finally {
|
|
106
|
+
await output.close();
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
return { root, dispose: () => rm(root, { recursive: true, force: true }) };
|
|
110
|
+
}
|
|
111
|
+
catch (error) {
|
|
112
|
+
await rm(root, { recursive: true, force: true }).catch(() => undefined);
|
|
113
|
+
throw invalid('Unable to materialize Extension bundle', error);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
/** Removes bundle materializations left behind when the previous owner died. */
|
|
117
|
+
export async function recoverExtensionBundleImports(controlDirectory) {
|
|
118
|
+
try {
|
|
119
|
+
await rm(join(controlDirectory, BUNDLE_IMPORTS_DIRECTORY), {
|
|
120
|
+
recursive: true,
|
|
121
|
+
force: true,
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
catch (error) {
|
|
125
|
+
throw invalid('Unable to recover Extension bundle imports', error);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
async function readDirectory(rootValue) {
|
|
129
|
+
const root = await realpath(rootValue);
|
|
130
|
+
if (!(await stat(root)).isDirectory())
|
|
131
|
+
throw invalid('Extension bundle source is not a directory');
|
|
132
|
+
const paths = [];
|
|
133
|
+
await collect(root, '', paths);
|
|
134
|
+
if (paths.length === 0 || paths.length > MAX_FILES)
|
|
135
|
+
throw invalid('Extension bundle file count is invalid');
|
|
136
|
+
let total = 0;
|
|
137
|
+
const files = [];
|
|
138
|
+
for (const path of paths.sort()) {
|
|
139
|
+
const handle = await open(join(root, ...path.split('/')), constants.O_RDONLY | constants.O_NOFOLLOW);
|
|
140
|
+
try {
|
|
141
|
+
const metadata = await handle.stat();
|
|
142
|
+
if (!metadata.isFile() || metadata.size > MAX_FILE_BYTES)
|
|
143
|
+
throw invalid(`Extension bundle file is invalid: ${path}`);
|
|
144
|
+
const content = await handle.readFile();
|
|
145
|
+
total += content.byteLength;
|
|
146
|
+
if (total > MAX_BUNDLE_BYTES)
|
|
147
|
+
throw invalid('Extension bundle payload is too large');
|
|
148
|
+
files.push({ path, content });
|
|
149
|
+
}
|
|
150
|
+
finally {
|
|
151
|
+
await handle.close();
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
return files;
|
|
155
|
+
}
|
|
156
|
+
async function collect(root, directory, paths) {
|
|
157
|
+
const entries = await readdir(directory ? join(root, ...directory.split('/')) : root, {
|
|
158
|
+
withFileTypes: true,
|
|
159
|
+
});
|
|
160
|
+
for (const entry of entries.sort((left, right) => left.name.localeCompare(right.name))) {
|
|
161
|
+
if (entry.name === '.git')
|
|
162
|
+
continue;
|
|
163
|
+
const path = directory ? `${directory}/${entry.name}` : entry.name;
|
|
164
|
+
safePath(path);
|
|
165
|
+
if (entry.isSymbolicLink())
|
|
166
|
+
throw invalid(`Extension bundle may not contain symlinks: ${path}`);
|
|
167
|
+
if (entry.isDirectory())
|
|
168
|
+
await collect(root, path, paths);
|
|
169
|
+
else if (entry.isFile())
|
|
170
|
+
paths.push(path);
|
|
171
|
+
else
|
|
172
|
+
throw invalid(`Extension bundle contains an unsupported entry: ${path}`);
|
|
173
|
+
if (paths.length > MAX_FILES)
|
|
174
|
+
throw invalid('Extension bundle contains too many files');
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
function decodeBundle(value) {
|
|
178
|
+
if (!value || typeof value !== 'object' || Array.isArray(value))
|
|
179
|
+
throw invalid('Extension bundle must be an object');
|
|
180
|
+
const record = value;
|
|
181
|
+
if (Object.keys(record).sort().join() !== 'digest,files,schemaVersion' ||
|
|
182
|
+
record.schemaVersion !== 1 ||
|
|
183
|
+
!Array.isArray(record.files) ||
|
|
184
|
+
record.files.length === 0 ||
|
|
185
|
+
record.files.length > MAX_FILES) {
|
|
186
|
+
throw invalid('Extension bundle fields are invalid');
|
|
187
|
+
}
|
|
188
|
+
let total = 0;
|
|
189
|
+
const paths = new Set();
|
|
190
|
+
const files = record.files.map((value) => {
|
|
191
|
+
if (!value || typeof value !== 'object' || Array.isArray(value))
|
|
192
|
+
throw invalid('Extension bundle file is invalid');
|
|
193
|
+
const file = value;
|
|
194
|
+
if (Object.keys(file).sort().join() !== 'content,path,sha256' ||
|
|
195
|
+
typeof file.path !== 'string' ||
|
|
196
|
+
typeof file.content !== 'string' ||
|
|
197
|
+
typeof file.sha256 !== 'string')
|
|
198
|
+
throw invalid('Extension bundle file fields are invalid');
|
|
199
|
+
const path = safePath(file.path);
|
|
200
|
+
if (paths.has(path))
|
|
201
|
+
throw invalid(`Extension bundle repeats file: ${path}`);
|
|
202
|
+
paths.add(path);
|
|
203
|
+
const content = Buffer.from(file.content, 'base64');
|
|
204
|
+
total += content.byteLength;
|
|
205
|
+
if (content.byteLength > MAX_FILE_BYTES ||
|
|
206
|
+
total > MAX_BUNDLE_BYTES ||
|
|
207
|
+
createHash('sha256').update(content).digest('hex') !== file.sha256) {
|
|
208
|
+
throw invalid(`Extension bundle file integrity failed: ${path}`);
|
|
209
|
+
}
|
|
210
|
+
return { path, content };
|
|
211
|
+
});
|
|
212
|
+
if (typeof record.digest !== 'string' || extensionPackageContentDigest(files) !== record.digest)
|
|
213
|
+
throw invalid('Extension bundle digest is invalid');
|
|
214
|
+
return Object.freeze({
|
|
215
|
+
schemaVersion: 1,
|
|
216
|
+
digest: record.digest,
|
|
217
|
+
files: Object.freeze(files.map((file) => Object.freeze({
|
|
218
|
+
path: file.path,
|
|
219
|
+
sha256: createHash('sha256').update(file.content).digest('hex'),
|
|
220
|
+
content: file.content.toString('base64'),
|
|
221
|
+
}))),
|
|
222
|
+
});
|
|
223
|
+
}
|
|
224
|
+
export function extensionPackageContentDigest(files) {
|
|
225
|
+
const hash = createHash('sha256');
|
|
226
|
+
for (const file of files) {
|
|
227
|
+
const path = Buffer.from(file.path, 'utf8');
|
|
228
|
+
const length = Buffer.allocUnsafe(8);
|
|
229
|
+
length.writeBigUInt64BE(BigInt(path.byteLength));
|
|
230
|
+
hash.update(length).update(path);
|
|
231
|
+
length.writeBigUInt64BE(BigInt(file.content.byteLength));
|
|
232
|
+
hash.update(length).update(file.content);
|
|
233
|
+
}
|
|
234
|
+
return `sha256-${hash.digest('hex')}`;
|
|
235
|
+
}
|
|
236
|
+
function safePath(value) {
|
|
237
|
+
if (!value ||
|
|
238
|
+
value.length > 512 ||
|
|
239
|
+
value.includes('\\') ||
|
|
240
|
+
value.startsWith('/') ||
|
|
241
|
+
posix.normalize(value) !== value ||
|
|
242
|
+
value.split('/').some((part) => !part || part === '.' || part === '..')) {
|
|
243
|
+
throw invalid('Extension bundle path is invalid');
|
|
244
|
+
}
|
|
245
|
+
return value;
|
|
246
|
+
}
|
|
247
|
+
function invalid(message, cause) {
|
|
248
|
+
return new ExtensionBundleError(message, { cause });
|
|
249
|
+
}
|
|
@@ -0,0 +1,262 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Licensed to the Apache Software Foundation (ASF) under one
|
|
3
|
+
* or more contributor license agreements. See the NOTICE file
|
|
4
|
+
* distributed with this work for additional information
|
|
5
|
+
* regarding copyright ownership. The ASF licenses this file
|
|
6
|
+
* to you under the Apache License, Version 2.0 (the
|
|
7
|
+
* "License"); you may not use this file except in compliance
|
|
8
|
+
* with the License. You may obtain a copy of the License at
|
|
9
|
+
*
|
|
10
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
11
|
+
*
|
|
12
|
+
* Unless required by applicable law or agreed to in writing,
|
|
13
|
+
* software distributed under the License is distributed on an
|
|
14
|
+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
15
|
+
* KIND, either express or implied. See the License for the
|
|
16
|
+
* specific language governing permissions and limitations
|
|
17
|
+
* under the License.
|
|
18
|
+
*/
|
|
19
|
+
import { readFile } from 'node:fs/promises';
|
|
20
|
+
import { join } from 'node:path';
|
|
21
|
+
import { isCanonicalExtensionId } from '@maka/runtime/plugin-runtime';
|
|
22
|
+
export const EXTENSION_PACKAGE_MANIFEST_FILE = 'maka.extension.json';
|
|
23
|
+
const MAX_MANIFEST_BYTES = 256 * 1024;
|
|
24
|
+
const KEY_PATTERN = /^[A-Za-z][A-Za-z0-9._-]{0,127}$/u;
|
|
25
|
+
export class ExtensionPackageManifestError extends Error {
|
|
26
|
+
name = 'ExtensionPackageManifestError';
|
|
27
|
+
constructor(message, options) {
|
|
28
|
+
super(message, options);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
export async function loadExtensionPackageManifest(root) {
|
|
32
|
+
let encoded;
|
|
33
|
+
try {
|
|
34
|
+
encoded = await readFile(join(root, EXTENSION_PACKAGE_MANIFEST_FILE));
|
|
35
|
+
}
|
|
36
|
+
catch (error) {
|
|
37
|
+
if (error.code === 'ENOENT')
|
|
38
|
+
return undefined;
|
|
39
|
+
throw invalid('Unable to read unified Extension manifest', error);
|
|
40
|
+
}
|
|
41
|
+
if (encoded.byteLength > MAX_MANIFEST_BYTES) {
|
|
42
|
+
throw invalid('Unified Extension manifest exceeds its size limit');
|
|
43
|
+
}
|
|
44
|
+
try {
|
|
45
|
+
return decodeExtensionPackageManifest(JSON.parse(encoded.toString('utf8')));
|
|
46
|
+
}
|
|
47
|
+
catch (error) {
|
|
48
|
+
if (error instanceof ExtensionPackageManifestError)
|
|
49
|
+
throw error;
|
|
50
|
+
throw invalid('Unified Extension manifest is invalid JSON', error);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
export function decodeExtensionPackageManifest(value) {
|
|
54
|
+
const source = record(value, 'Extension manifest');
|
|
55
|
+
exactOptional(source, ['schemaVersion', 'id'], ['displayName', 'description', 'dependencies', 'configuration', 'runtime', 'composition']);
|
|
56
|
+
if (source.schemaVersion !== 1)
|
|
57
|
+
throw invalid('Extension manifest schemaVersion must be 1');
|
|
58
|
+
const id = extensionId(source.id);
|
|
59
|
+
const displayName = source.displayName === undefined ? id : text(source.displayName, 'displayName', 128);
|
|
60
|
+
const description = source.description === undefined ? '' : boundedDescription(source.description);
|
|
61
|
+
const dependencies = decodeDependencies(source.dependencies);
|
|
62
|
+
const configuration = decodeConfigurationSchema(source.configuration);
|
|
63
|
+
const runtime = decodeRuntime(source.runtime);
|
|
64
|
+
const composition = decodeComposition(source.composition);
|
|
65
|
+
return Object.freeze({
|
|
66
|
+
schemaVersion: 1,
|
|
67
|
+
id,
|
|
68
|
+
displayName,
|
|
69
|
+
description,
|
|
70
|
+
dependencies,
|
|
71
|
+
configuration,
|
|
72
|
+
...(runtime === undefined ? {} : { runtime }),
|
|
73
|
+
...(composition === undefined ? {} : { composition }),
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
function decodeComposition(value) {
|
|
77
|
+
if (value === undefined)
|
|
78
|
+
return undefined;
|
|
79
|
+
const composition = record(value, 'composition');
|
|
80
|
+
exactOptional(composition, ['patch'], ['structuralDependencies']);
|
|
81
|
+
const structuralDependencies = decodeExtensionIds(composition.structuralDependencies, 'composition.structuralDependencies');
|
|
82
|
+
return Object.freeze({
|
|
83
|
+
patch: packagePath(composition.patch, 'composition.patch'),
|
|
84
|
+
structuralDependencies,
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
function decodeRuntime(value) {
|
|
88
|
+
if (value === undefined)
|
|
89
|
+
return undefined;
|
|
90
|
+
const runtime = record(value, 'runtime');
|
|
91
|
+
if (runtime.entry === undefined)
|
|
92
|
+
return undefined;
|
|
93
|
+
return Object.freeze({ entry: packagePath(runtime.entry, 'runtime.entry') });
|
|
94
|
+
}
|
|
95
|
+
function packagePath(value, label) {
|
|
96
|
+
if (typeof value !== 'string' ||
|
|
97
|
+
value.length === 0 ||
|
|
98
|
+
value.length > 512 ||
|
|
99
|
+
value.includes('\\') ||
|
|
100
|
+
value.startsWith('/') ||
|
|
101
|
+
value.split('/').some((part) => !part || part === '.' || part === '..')) {
|
|
102
|
+
throw invalid(`Extension manifest ${label} is invalid`);
|
|
103
|
+
}
|
|
104
|
+
return value;
|
|
105
|
+
}
|
|
106
|
+
export function validateExtensionConfiguration(schema, value) {
|
|
107
|
+
const input = value === undefined ? {} : record(value, 'Extension configuration');
|
|
108
|
+
const unknown = Object.keys(input).find((key) => !Object.hasOwn(schema.properties, key));
|
|
109
|
+
if (unknown)
|
|
110
|
+
throw invalid(`Extension configuration key is not declared: ${unknown}`);
|
|
111
|
+
const result = {};
|
|
112
|
+
for (const [key, property] of Object.entries(schema.properties)) {
|
|
113
|
+
const configured = Object.hasOwn(input, key) ? input[key] : property.default;
|
|
114
|
+
if (configured === undefined) {
|
|
115
|
+
if (schema.required.includes(key)) {
|
|
116
|
+
throw invalid(`Extension configuration is missing required key: ${key}`);
|
|
117
|
+
}
|
|
118
|
+
continue;
|
|
119
|
+
}
|
|
120
|
+
if (typeof configured !== property.type || !isScalar(configured)) {
|
|
121
|
+
throw invalid(`Extension configuration type is invalid for key: ${key}`);
|
|
122
|
+
}
|
|
123
|
+
if (property.enum && !property.enum.some((candidate) => candidate === configured)) {
|
|
124
|
+
throw invalid(`Extension configuration value is not allowed for key: ${key}`);
|
|
125
|
+
}
|
|
126
|
+
result[key] = configured;
|
|
127
|
+
}
|
|
128
|
+
const encoded = JSON.stringify(result);
|
|
129
|
+
if (Buffer.byteLength(encoded, 'utf8') > 64 * 1024) {
|
|
130
|
+
throw invalid('Extension configuration exceeds its size limit');
|
|
131
|
+
}
|
|
132
|
+
return Object.freeze(result);
|
|
133
|
+
}
|
|
134
|
+
function decodeDependencies(value) {
|
|
135
|
+
if (value === undefined)
|
|
136
|
+
return Object.freeze([]);
|
|
137
|
+
if (!Array.isArray(value) || value.length > 64)
|
|
138
|
+
throw invalid('Extension dependencies are invalid');
|
|
139
|
+
const ids = new Set();
|
|
140
|
+
const dependencies = value.map((item, index) => {
|
|
141
|
+
const dependency = record(item, `dependencies[${index}]`);
|
|
142
|
+
exactOptional(dependency, ['id'], []);
|
|
143
|
+
const id = extensionId(dependency.id);
|
|
144
|
+
if (ids.has(id))
|
|
145
|
+
throw invalid(`Extension dependency repeats: ${id}`);
|
|
146
|
+
ids.add(id);
|
|
147
|
+
return Object.freeze({ id });
|
|
148
|
+
});
|
|
149
|
+
return Object.freeze(dependencies.sort((left, right) => left.id.localeCompare(right.id)));
|
|
150
|
+
}
|
|
151
|
+
function decodeExtensionIds(value, label) {
|
|
152
|
+
if (value === undefined)
|
|
153
|
+
return Object.freeze([]);
|
|
154
|
+
if (!Array.isArray(value) || value.length > 64)
|
|
155
|
+
throw invalid(`${label} is invalid`);
|
|
156
|
+
const ids = value.map((item) => extensionId(item));
|
|
157
|
+
if (new Set(ids).size !== ids.length)
|
|
158
|
+
throw invalid(`${label} repeats an identity`);
|
|
159
|
+
return Object.freeze(ids.sort((left, right) => left.localeCompare(right)));
|
|
160
|
+
}
|
|
161
|
+
function decodeConfigurationSchema(value) {
|
|
162
|
+
if (value === undefined)
|
|
163
|
+
return Object.freeze({ properties: Object.freeze({}), required: Object.freeze([]) });
|
|
164
|
+
const schema = record(value, 'configuration');
|
|
165
|
+
exactOptional(schema, ['properties'], ['required']);
|
|
166
|
+
const propertiesSource = record(schema.properties, 'configuration properties');
|
|
167
|
+
if (Object.keys(propertiesSource).length > 128)
|
|
168
|
+
throw invalid('Too many Extension configuration properties');
|
|
169
|
+
const properties = {};
|
|
170
|
+
for (const [key, value] of Object.entries(propertiesSource)) {
|
|
171
|
+
if (!KEY_PATTERN.test(key))
|
|
172
|
+
throw invalid(`Extension configuration key is invalid: ${key}`);
|
|
173
|
+
const property = record(value, `configuration.properties.${key}`);
|
|
174
|
+
exactOptional(property, ['type'], ['title', 'description', 'default', 'enum']);
|
|
175
|
+
if (property.type !== 'string' && property.type !== 'number' && property.type !== 'boolean') {
|
|
176
|
+
throw invalid(`Extension configuration property type is invalid: ${key}`);
|
|
177
|
+
}
|
|
178
|
+
const type = property.type;
|
|
179
|
+
const defaultValue = property.default;
|
|
180
|
+
if (defaultValue !== undefined && (typeof defaultValue !== type || !isScalar(defaultValue))) {
|
|
181
|
+
throw invalid(`Extension configuration default is invalid: ${key}`);
|
|
182
|
+
}
|
|
183
|
+
let values;
|
|
184
|
+
if (property.enum !== undefined) {
|
|
185
|
+
if (!Array.isArray(property.enum) ||
|
|
186
|
+
property.enum.length === 0 ||
|
|
187
|
+
property.enum.length > 64 ||
|
|
188
|
+
property.enum.some((item) => typeof item !== type || !isScalar(item)))
|
|
189
|
+
throw invalid(`Extension configuration enum is invalid: ${key}`);
|
|
190
|
+
values = Object.freeze([...new Set(property.enum)]);
|
|
191
|
+
if (defaultValue !== undefined &&
|
|
192
|
+
!values.includes(defaultValue)) {
|
|
193
|
+
throw invalid(`Extension configuration default is outside enum: ${key}`);
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
properties[key] = Object.freeze({
|
|
197
|
+
type,
|
|
198
|
+
...(property.title === undefined
|
|
199
|
+
? {}
|
|
200
|
+
: { title: text(property.title, 'configuration title', 128) }),
|
|
201
|
+
...(property.description === undefined
|
|
202
|
+
? {}
|
|
203
|
+
: { description: text(property.description, 'configuration description', 1024) }),
|
|
204
|
+
...(defaultValue === undefined
|
|
205
|
+
? {}
|
|
206
|
+
: { default: defaultValue }),
|
|
207
|
+
...(values ? { enum: values } : {}),
|
|
208
|
+
});
|
|
209
|
+
}
|
|
210
|
+
const required = schema.required === undefined ? [] : schema.required;
|
|
211
|
+
if (!Array.isArray(required) ||
|
|
212
|
+
required.length > Object.keys(properties).length ||
|
|
213
|
+
required.some((key) => typeof key !== 'string' || !Object.hasOwn(properties, key)) ||
|
|
214
|
+
new Set(required).size !== required.length)
|
|
215
|
+
throw invalid('Extension configuration required keys are invalid');
|
|
216
|
+
return Object.freeze({
|
|
217
|
+
properties: Object.freeze(properties),
|
|
218
|
+
required: Object.freeze(required),
|
|
219
|
+
});
|
|
220
|
+
}
|
|
221
|
+
function exactOptional(value, required, optional) {
|
|
222
|
+
const allowed = new Set([...required, ...optional]);
|
|
223
|
+
if (required.some((key) => !Object.hasOwn(value, key)) ||
|
|
224
|
+
Object.keys(value).some((key) => !allowed.has(key))) {
|
|
225
|
+
throw invalid('Extension manifest fields are invalid');
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
function record(value, label) {
|
|
229
|
+
if (!value || typeof value !== 'object' || Array.isArray(value))
|
|
230
|
+
throw invalid(`${label} must be an object`);
|
|
231
|
+
return value;
|
|
232
|
+
}
|
|
233
|
+
function extensionId(value) {
|
|
234
|
+
if (!isCanonicalExtensionId(value))
|
|
235
|
+
throw invalid('Extension manifest id is invalid');
|
|
236
|
+
return value;
|
|
237
|
+
}
|
|
238
|
+
function text(value, label, maxBytes) {
|
|
239
|
+
if (typeof value !== 'string' ||
|
|
240
|
+
value.length === 0 ||
|
|
241
|
+
Buffer.byteLength(value, 'utf8') > maxBytes ||
|
|
242
|
+
/[\0\r\n]/u.test(value)) {
|
|
243
|
+
throw invalid(`Extension manifest ${label} is invalid`);
|
|
244
|
+
}
|
|
245
|
+
return value;
|
|
246
|
+
}
|
|
247
|
+
function boundedDescription(value) {
|
|
248
|
+
if (typeof value !== 'string' ||
|
|
249
|
+
Buffer.byteLength(value, 'utf8') > 4096 ||
|
|
250
|
+
value.includes('\0')) {
|
|
251
|
+
throw invalid('Extension manifest description is invalid');
|
|
252
|
+
}
|
|
253
|
+
return value;
|
|
254
|
+
}
|
|
255
|
+
function isScalar(value) {
|
|
256
|
+
return (typeof value === 'string' ||
|
|
257
|
+
typeof value === 'boolean' ||
|
|
258
|
+
(typeof value === 'number' && Number.isFinite(value)));
|
|
259
|
+
}
|
|
260
|
+
function invalid(message, cause) {
|
|
261
|
+
return new ExtensionPackageManifestError(message, { cause });
|
|
262
|
+
}
|