@arnilo/prism-hosts 0.0.0-stage → 0.13.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 +394 -0
- package/LICENSE +21 -0
- package/README.md +34 -2
- package/acp/README.md +87 -0
- package/acp/src/capabilities.ts +184 -0
- package/acp/src/errors.ts +24 -0
- package/acp/src/host.ts +36 -0
- package/acp/src/index.ts +25 -0
- package/acp/src/limits.ts +49 -0
- package/acp/src/mapper.ts +419 -0
- package/acp/src/permission.ts +37 -0
- package/acp/src/projection.ts +39 -0
- package/acp/src/prompt.ts +170 -0
- package/acp/src/serve.ts +345 -0
- package/acp/src/session-map.ts +49 -0
- package/ag-ui/README.md +56 -0
- package/ag-ui/src/a2ui.ts +570 -0
- package/ag-ui/src/ag-ui-mapper.ts +507 -0
- package/ag-ui/src/encrypted-value.ts +40 -0
- package/ag-ui/src/errors.ts +29 -0
- package/ag-ui/src/handler.ts +642 -0
- package/ag-ui/src/index.ts +177 -0
- package/ag-ui/src/input.ts +190 -0
- package/ag-ui/src/limits.ts +279 -0
- package/ag-ui/src/mcp-apps.ts +256 -0
- package/ag-ui/src/mcp.ts +94 -0
- package/ag-ui/src/projection.ts +201 -0
- package/ag-ui/src/projectors.ts +272 -0
- package/ag-ui/src/renderer/bind.ts +152 -0
- package/ag-ui/src/renderer/core.ts +270 -0
- package/ag-ui/src/renderer/index.ts +165 -0
- package/ag-ui/src/replay.ts +189 -0
- package/ag-ui/src/types.ts +66 -0
- package/channels/README.md +139 -0
- package/channels/src/admission.ts +8 -0
- package/channels/src/approvals.ts +262 -0
- package/channels/src/checkpoint-contract.ts +97 -0
- package/channels/src/delivery.ts +116 -0
- package/channels/src/host-ui.ts +356 -0
- package/channels/src/identity.ts +46 -0
- package/channels/src/index.ts +82 -0
- package/channels/src/limits.ts +49 -0
- package/channels/src/memory-stores.ts +112 -0
- package/channels/src/pairing.ts +178 -0
- package/channels/src/runtime-admit.ts +389 -0
- package/channels/src/runtime-core.ts +621 -0
- package/channels/src/runtime-reconcile.ts +299 -0
- package/channels/src/runtime-turn.ts +664 -0
- package/channels/src/runtime-types.ts +95 -0
- package/channels/src/runtime.ts +86 -0
- package/channels/src/secret-ref.ts +42 -0
- package/channels/src/signal.ts +790 -0
- package/channels/src/state.ts +355 -0
- package/channels/src/telegram.ts +1317 -0
- package/channels/src/types.ts +442 -0
- package/devtools/README.md +92 -0
- package/devtools/bin/prism-devtools.ts +63 -0
- package/devtools/src/args.ts +71 -0
- package/devtools/src/bun.d.ts +27 -0
- package/devtools/src/compare.ts +190 -0
- package/devtools/src/errors.ts +15 -0
- package/devtools/src/index.ts +25 -0
- package/devtools/src/replay.ts +36 -0
- package/devtools/src/security.ts +80 -0
- package/devtools/src/serve.ts +259 -0
- package/devtools/src/ui/assets.ts +108 -0
- package/devtools/src/ui/inspector.ts +182 -0
- package/driver-process/README.md +197 -0
- package/driver-process/src/abort.ts +74 -0
- package/driver-process/src/adapter.ts +211 -0
- package/driver-process/src/driver.ts +303 -0
- package/driver-process/src/errors.ts +17 -0
- package/driver-process/src/extend.ts +72 -0
- package/driver-process/src/index.ts +5 -0
- package/driver-process/src/inspect.ts +33 -0
- package/driver-process/src/session.ts +72 -0
- package/driver-process/src/testing.ts +394 -0
- package/driver-process/src/transports/json-rpc.ts +147 -0
- package/driver-process/src/transports/jsonl-stream.ts +12 -0
- package/driver-process/src/transports/types.ts +11 -0
- package/driver-process/src/turn.ts +106 -0
- package/evals/README.md +843 -0
- package/evals/src/calibration.ts +179 -0
- package/evals/src/citation-integrity.ts +98 -0
- package/evals/src/citations.ts +45 -0
- package/evals/src/compare-experiments.ts +153 -0
- package/evals/src/comparison.ts +230 -0
- package/evals/src/curate.ts +257 -0
- package/evals/src/dataset.ts +81 -0
- package/evals/src/erp-invariants.ts +163 -0
- package/evals/src/errors.ts +23 -0
- package/evals/src/experiment.ts +537 -0
- package/evals/src/failure-injection.ts +77 -0
- package/evals/src/feedback.ts +53 -0
- package/evals/src/index.ts +198 -0
- package/evals/src/injection.ts +247 -0
- package/evals/src/judge-panel.ts +109 -0
- package/evals/src/judge.ts +89 -0
- package/evals/src/limits.ts +50 -0
- package/evals/src/report-record.ts +47 -0
- package/evals/src/rubric-judge.ts +334 -0
- package/evals/src/scenario-runner.ts +317 -0
- package/evals/src/scenarios.ts +81 -0
- package/evals/src/score.ts +328 -0
- package/evals/src/scorer.ts +21 -0
- package/evals/src/state-match.ts +130 -0
- package/evals/src/stats.ts +635 -0
- package/evals/src/threshold.ts +117 -0
- package/evals/src/timeline.ts +38 -0
- package/evals/src/toolless-session.ts +33 -0
- package/evals/src/trace.ts +80 -0
- package/evals/src/trajectory.ts +623 -0
- package/evals/src/types.ts +672 -0
- package/evals/src/user-simulator.ts +101 -0
- package/evals/src/util.ts +207 -0
- package/headless/README.md +172 -0
- package/headless/src/answers.ts +91 -0
- package/headless/src/authority.ts +32 -0
- package/headless/src/errors.ts +36 -0
- package/headless/src/index.ts +17 -0
- package/headless/src/limits.ts +11 -0
- package/headless/src/pending.ts +16 -0
- package/headless/src/policy.ts +36 -0
- package/headless/src/resume.ts +103 -0
- package/headless/src/route.ts +153 -0
- package/headless/src/scheduler/clock.ts +22 -0
- package/headless/src/scheduler/cron.ts +19 -0
- package/headless/src/scheduler/firing.ts +58 -0
- package/headless/src/scheduler/index.ts +399 -0
- package/headless/src/scheduler/lease.ts +38 -0
- package/headless/src/scheduler/missed.ts +12 -0
- package/headless/src/scheduler/state.ts +132 -0
- package/host-builder/README.md +251 -0
- package/host-builder/bin/prism-host-builder.ts +169 -0
- package/host-builder/data/api.json +6197 -0
- package/host-builder/data/errors.json +558 -0
- package/host-builder/data/examples/acp.ts +63 -0
- package/host-builder/data/examples/adapters/conformance.ts +34 -0
- package/host-builder/data/examples/adapters/fixtures/rpc-server.json +26 -0
- package/host-builder/data/examples/adapters/fixtures/stream-cli.json +27 -0
- package/host-builder/data/examples/adapters/fixtures/workspace-config.json +28 -0
- package/host-builder/data/examples/adapters/rpc-server.dialect.ts +218 -0
- package/host-builder/data/examples/adapters/rpc-server.ts +309 -0
- package/host-builder/data/examples/adapters/stream-cli.dialect.ts +165 -0
- package/host-builder/data/examples/adapters/stream-cli.ts +256 -0
- package/host-builder/data/examples/adapters/workspace-config.dialect.ts +172 -0
- package/host-builder/data/examples/adapters/workspace-config.ts +251 -0
- package/host-builder/data/examples/advisor.ts +94 -0
- package/host-builder/data/examples/ag-ui-a2ui.ts +80 -0
- package/host-builder/data/examples/ag-ui-mcp-apps.ts +44 -0
- package/host-builder/data/examples/ag-ui-server.ts +118 -0
- package/host-builder/data/examples/agent-durable-approval.ts +43 -0
- package/host-builder/data/examples/api-key-auth.ts +23 -0
- package/host-builder/data/examples/approval.ts +92 -0
- package/host-builder/data/examples/attention-compiler.ts +81 -0
- package/host-builder/data/examples/autonomous-coding-loop.ts +343 -0
- package/host-builder/data/examples/behavior-evaluation.ts +303 -0
- package/host-builder/data/examples/build-workflow.ts +334 -0
- package/host-builder/data/examples/cache-aware-prompt-assembly.ts +139 -0
- package/host-builder/data/examples/caveman-ponytail.ts +197 -0
- package/host-builder/data/examples/cli.ts +31 -0
- package/host-builder/data/examples/coding-approval.ts +115 -0
- package/host-builder/data/examples/coding-browser-evaluation.ts +208 -0
- package/host-builder/data/examples/coding-compaction.ts +30 -0
- package/host-builder/data/examples/coding-goal-verify.ts +121 -0
- package/host-builder/data/examples/coding-tools-capability-gaps.ts +75 -0
- package/host-builder/data/examples/compaction.ts +36 -0
- package/host-builder/data/examples/connected-slack-mcp.ts +67 -0
- package/host-builder/data/examples/context.ts +29 -0
- package/host-builder/data/examples/custom-adapter.ts +256 -0
- package/host-builder/data/examples/custom-builders.ts +66 -0
- package/host-builder/data/examples/custom-loop.ts +89 -0
- package/host-builder/data/examples/custom-provider.ts +70 -0
- package/host-builder/data/examples/custom-session-store.ts +72 -0
- package/host-builder/data/examples/custom-tools-skills-context.ts +97 -0
- package/host-builder/data/examples/decision-models.ts +98 -0
- package/host-builder/data/examples/define-package.ts +188 -0
- package/host-builder/data/examples/delegate.ts +200 -0
- package/host-builder/data/examples/discover-skills.ts +65 -0
- package/host-builder/data/examples/drive-rag-sync.ts +44 -0
- package/host-builder/data/examples/durable-approval.ts +139 -0
- package/host-builder/data/examples/durable-investigation.ts +93 -0
- package/host-builder/data/examples/durable-loops-and-approvals.ts +94 -0
- package/host-builder/data/examples/enterprise-identity.ts +21 -0
- package/host-builder/data/examples/enterprise-policy-audit.ts +50 -0
- package/host-builder/data/examples/enterprise-work-connectors.ts +103 -0
- package/host-builder/data/examples/eval-injection.ts +73 -0
- package/host-builder/data/examples/eval-judges.ts +145 -0
- package/host-builder/data/examples/evals.ts +86 -0
- package/host-builder/data/examples/evaluation-gate.ts +40 -0
- package/host-builder/data/examples/execution-timeline.ts +173 -0
- package/host-builder/data/examples/external-agent.ts +541 -0
- package/host-builder/data/examples/external-app-db-backed.ts +344 -0
- package/host-builder/data/examples/governance-deny.ts +122 -0
- package/host-builder/data/examples/guardrail-packs.ts +240 -0
- package/host-builder/data/examples/headless-host.ts +245 -0
- package/host-builder/data/examples/hooks-audit-command.mjs +31 -0
- package/host-builder/data/examples/hooks-json.ts +87 -0
- package/host-builder/data/examples/host-builder-agent.ts +150 -0
- package/host-builder/data/examples/host-hooks.ts +71 -0
- package/host-builder/data/examples/host-step-loop-timeline.ts +212 -0
- package/host-builder/data/examples/hosted-sandbox.ts +48 -0
- package/host-builder/data/examples/instruction-injection.ts +127 -0
- package/host-builder/data/examples/jsonl-stores-branching.ts +49 -0
- package/host-builder/data/examples/long-term-memory.ts +158 -0
- package/host-builder/data/examples/manifest.ts +112 -0
- package/host-builder/data/examples/mcp-server.ts +48 -0
- package/host-builder/data/examples/mcp-trust.ts +173 -0
- package/host-builder/data/examples/memory-and-wiki.ts +315 -0
- package/host-builder/data/examples/memory-lifecycle.ts +302 -0
- package/host-builder/data/examples/memory-system.ts +255 -0
- package/host-builder/data/examples/messaging-agent.ts +70 -0
- package/host-builder/data/examples/minimal-host-app.ts +41 -0
- package/host-builder/data/examples/model-router-aggregate-budgets.ts +364 -0
- package/host-builder/data/examples/model-router.ts +106 -0
- package/host-builder/data/examples/obscura.ts +48 -0
- package/host-builder/data/examples/observability.ts +116 -0
- package/host-builder/data/examples/open-connector-sidecar.ts +105 -0
- package/host-builder/data/examples/openai-compatible.ts +70 -0
- package/host-builder/data/examples/package-skills.ts +85 -0
- package/host-builder/data/examples/phase9-coding-intelligence.ts +221 -0
- package/host-builder/data/examples/provider-registration.ts +29 -0
- package/host-builder/data/examples/provider-resolver.ts +39 -0
- package/host-builder/data/examples/rag.ts +40 -0
- package/host-builder/data/examples/remote-agent-server.ts +45 -0
- package/host-builder/data/examples/remote-agent.ts +124 -0
- package/host-builder/data/examples/research-ledger.ts +79 -0
- package/host-builder/data/examples/revocation-propagation.ts +287 -0
- package/host-builder/data/examples/run-feedback.ts +93 -0
- package/host-builder/data/examples/scanned-document-rag.ts +48 -0
- package/host-builder/data/examples/sdk-basics.ts +21 -0
- package/host-builder/data/examples/secure-agent.ts +55 -0
- package/host-builder/data/examples/semantic-memory.ts +52 -0
- package/host-builder/data/examples/serve-tools.ts +495 -0
- package/host-builder/data/examples/session-memory.ts +318 -0
- package/host-builder/data/examples/session-search.ts +131 -0
- package/host-builder/data/examples/session.ts +52 -0
- package/host-builder/data/examples/side-call.ts +68 -0
- package/host-builder/data/examples/signal-agent.ts +9 -0
- package/host-builder/data/examples/skills-progressive-disclosure.ts +88 -0
- package/host-builder/data/examples/skills.ts +19 -0
- package/host-builder/data/examples/sqlite-resume.ts +147 -0
- package/host-builder/data/examples/system-project-prompts.ts +74 -0
- package/host-builder/data/examples/system-prompts.ts +19 -0
- package/host-builder/data/examples/telegram-agent.ts +30 -0
- package/host-builder/data/examples/tool-narrowing-planes.ts +99 -0
- package/host-builder/data/examples/tools.ts +29 -0
- package/host-builder/data/examples/validator.ts +67 -0
- package/host-builder/data/examples/web-no-key.ts +85 -0
- package/host-builder/data/examples/web-research.ts +35 -0
- package/host-builder/data/examples/workflow-event-sink.ts +114 -0
- package/host-builder/data/examples/workflow-loop.ts +93 -0
- package/host-builder/data/examples/workflow-multimodal-document.ts +94 -0
- package/host-builder/data/examples/workflow-parallel-research.ts +111 -0
- package/host-builder/data/examples/workflow-postgres-resume.ts +75 -0
- package/host-builder/data/examples/workflow-research-and-review.ts +98 -0
- package/host-builder/data/examples/workflow-sqlite-resume.ts +163 -0
- package/host-builder/data/examples.json +130 -0
- package/host-builder/skills/prism-host-builder/SKILL.md +85 -0
- package/host-builder/skills/prism-host-builder/references/choosing.md +101 -0
- package/host-builder/skills/prism-host-builder/references/layout.md +52 -0
- package/host-builder/skills/prism-host-builder/references/pitfalls.md +50 -0
- package/host-builder/skills/prism-host-builder/references/recipes.md +129 -0
- package/host-builder/skills/prism-host-builder/references/security.md +52 -0
- package/host-builder/skills/prism-host-builder/references/upgrading.md +67 -0
- package/host-builder/skills/prism-host-builder/references/verification.md +108 -0
- package/host-builder/src/check/child.ts +108 -0
- package/host-builder/src/check/conformance.ts +13 -0
- package/host-builder/src/check/inspect.ts +20 -0
- package/host-builder/src/check/manifest.ts +23 -0
- package/host-builder/src/check/runner.ts +181 -0
- package/host-builder/src/check/targets.ts +65 -0
- package/host-builder/src/errors.ts +35 -0
- package/host-builder/src/index.ts +25 -0
- package/host-builder/src/indexes.ts +145 -0
- package/host-builder/src/install/install.ts +400 -0
- package/host-builder/src/install/targets.ts +50 -0
- package/host-builder/src/lookup/api.ts +127 -0
- package/host-builder/src/lookup/bound.ts +38 -0
- package/host-builder/src/lookup/docs.ts +238 -0
- package/host-builder/src/lookup/errors.ts +92 -0
- package/host-builder/src/lookup/examples.ts +117 -0
- package/host-builder/src/lookup/text.ts +60 -0
- package/host-builder/src/server.ts +300 -0
- package/host-builder/src/version.ts +38 -0
- package/mcp-server/README.md +219 -0
- package/mcp-server/src/bounds.ts +23 -0
- package/mcp-server/src/host.ts +31 -0
- package/mcp-server/src/index.ts +27 -0
- package/mcp-server/src/serve-tools.ts +408 -0
- package/mcp-server/src/serve.ts +329 -0
- package/mcp-server/src/server.ts +331 -0
- package/mcp-server/src/session-map.ts +47 -0
- package/mcp-server/src/skill-resources.ts +85 -0
- package/mcp-server/src/types.ts +215 -0
- package/package.json +130 -4
- package/remote/README.md +244 -0
- package/remote/src/attribution.ts +22 -0
- package/remote/src/auth.ts +30 -0
- package/remote/src/bun-module.d.ts +34 -0
- package/remote/src/channel.ts +86 -0
- package/remote/src/connect.ts +212 -0
- package/remote/src/dispatcher.ts +282 -0
- package/remote/src/errors.ts +59 -0
- package/remote/src/host-ui.ts +152 -0
- package/remote/src/http-gate.ts +43 -0
- package/remote/src/index.ts +53 -0
- package/remote/src/jsonl.ts +69 -0
- package/remote/src/limits.ts +43 -0
- package/remote/src/mirror.ts +90 -0
- package/remote/src/pending.ts +22 -0
- package/remote/src/protocol.ts +107 -0
- package/remote/src/serve.ts +560 -0
- package/remote/src/session-proxy.ts +219 -0
- package/remote/src/sse.ts +38 -0
- package/remote/src/stdio-client.ts +41 -0
- package/remote/src/testing-fixtures/serve-stdio.ts +32 -0
- package/remote/src/testing.ts +230 -0
- package/remote/src/transport.ts +139 -0
- package/remote/src/transports/client.ts +126 -0
- package/remote/src/transports/network.ts +268 -0
- package/remote/src/wire.ts +244 -0
|
@@ -0,0 +1,570 @@
|
|
|
1
|
+
import { type ActivityDeltaEvent, type AGUIEvent, EventType } from "@ag-ui/core";
|
|
2
|
+
import type { SecretRedactor, ToolCallDeltaContent, ToolResult } from "@arnilo/prism";
|
|
3
|
+
import { AgUiError } from "./errors.js";
|
|
4
|
+
import type { ParsedAgUiInput } from "./input.js";
|
|
5
|
+
import { assertBoundedJson } from "./input.js";
|
|
6
|
+
import type { ResolvedAgUiLimits } from "./limits.js";
|
|
7
|
+
import { projectAgUiJson, projectAgUiPatch } from "./projection.js";
|
|
8
|
+
|
|
9
|
+
export const A2UI_ACTIVITY_TYPE = "a2ui-surface";
|
|
10
|
+
export const A2UI_OPERATIONS_KEY = "a2ui_operations";
|
|
11
|
+
export const A2UI_VERSION = "v0.9";
|
|
12
|
+
export const A2UI_ERROR_EVENT = "prism.a2ui.error";
|
|
13
|
+
|
|
14
|
+
const OP_KEYS = ["createSurface", "updateComponents", "updateDataModel", "deleteSurface"] as const;
|
|
15
|
+
|
|
16
|
+
export const DEFAULT_MAX_A2UI_OPS_PER_MESSAGE = 64;
|
|
17
|
+
export const HARD_MAX_A2UI_OPS_PER_MESSAGE = 512;
|
|
18
|
+
export const DEFAULT_MAX_A2UI_OPERATION_BYTES = 64 * 1024;
|
|
19
|
+
export const HARD_MAX_A2UI_OPERATION_BYTES = 1024 * 1024;
|
|
20
|
+
export const DEFAULT_MAX_A2UI_SURFACES_PER_RUN = 16;
|
|
21
|
+
export const HARD_MAX_A2UI_SURFACES_PER_RUN = 64;
|
|
22
|
+
export const DEFAULT_MAX_A2UI_COMPONENT_DEPTH = 32;
|
|
23
|
+
export const HARD_MAX_A2UI_COMPONENT_DEPTH = 64;
|
|
24
|
+
|
|
25
|
+
export interface AgUiA2UiLimitOptions {
|
|
26
|
+
readonly maxOperationsPerMessage?: number;
|
|
27
|
+
readonly maxOperationBytes?: number;
|
|
28
|
+
readonly maxSurfacesPerRun?: number;
|
|
29
|
+
readonly maxComponentDepth?: number;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export interface ResolvedAgUiA2UiLimits {
|
|
33
|
+
readonly maxOperationsPerMessage: number;
|
|
34
|
+
readonly maxOperationBytes: number;
|
|
35
|
+
readonly maxSurfacesPerRun: number;
|
|
36
|
+
readonly maxComponentDepth: number;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export interface AgUiA2UiOptions {
|
|
40
|
+
readonly catalogId: string;
|
|
41
|
+
readonly allowedCatalogIds?: readonly string[];
|
|
42
|
+
readonly mode: "fixed-schema" | "streaming" | "both";
|
|
43
|
+
/** Default `render_a2ui`; streaming-mode source tool name. */
|
|
44
|
+
readonly renderToolName?: string;
|
|
45
|
+
readonly limits?: AgUiA2UiLimitOptions;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** Documented untrusted action shape delivered to `input.project`. */
|
|
49
|
+
export interface AgUiA2UiAction {
|
|
50
|
+
readonly type: "a2ui-action";
|
|
51
|
+
readonly surfaceId: string;
|
|
52
|
+
readonly actionName: string;
|
|
53
|
+
readonly payload?: unknown;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export interface AgUiA2UiPainter {
|
|
57
|
+
/** Fixed-schema paint from a dispatched tool result carrying `a2ui_operations`. */
|
|
58
|
+
onToolFinished(result: ToolResult): readonly AGUIEvent[];
|
|
59
|
+
/** Streaming paint: buffer `tool_call_delta.argumentsText` for the render tool. */
|
|
60
|
+
onToolCallDelta(delta: ToolCallDeltaContent): readonly AGUIEvent[];
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export function resolveAgUiA2UiLimits(options: AgUiA2UiLimitOptions = {}): ResolvedAgUiA2UiLimits {
|
|
64
|
+
return {
|
|
65
|
+
maxOperationsPerMessage: clamp(
|
|
66
|
+
options.maxOperationsPerMessage ?? DEFAULT_MAX_A2UI_OPS_PER_MESSAGE,
|
|
67
|
+
1,
|
|
68
|
+
HARD_MAX_A2UI_OPS_PER_MESSAGE,
|
|
69
|
+
"maxOperationsPerMessage",
|
|
70
|
+
),
|
|
71
|
+
maxOperationBytes: clamp(
|
|
72
|
+
options.maxOperationBytes ?? DEFAULT_MAX_A2UI_OPERATION_BYTES,
|
|
73
|
+
1_024,
|
|
74
|
+
HARD_MAX_A2UI_OPERATION_BYTES,
|
|
75
|
+
"maxOperationBytes",
|
|
76
|
+
),
|
|
77
|
+
maxSurfacesPerRun: clamp(
|
|
78
|
+
options.maxSurfacesPerRun ?? DEFAULT_MAX_A2UI_SURFACES_PER_RUN,
|
|
79
|
+
1,
|
|
80
|
+
HARD_MAX_A2UI_SURFACES_PER_RUN,
|
|
81
|
+
"maxSurfacesPerRun",
|
|
82
|
+
),
|
|
83
|
+
maxComponentDepth: clamp(
|
|
84
|
+
options.maxComponentDepth ?? DEFAULT_MAX_A2UI_COMPONENT_DEPTH,
|
|
85
|
+
1,
|
|
86
|
+
HARD_MAX_A2UI_COMPONENT_DEPTH,
|
|
87
|
+
"maxComponentDepth",
|
|
88
|
+
),
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/** Stateful painter: one instance per AG-UI run stream. */
|
|
93
|
+
export function createAgUiA2UiPainter(options: AgUiA2UiOptions, limits: ResolvedAgUiLimits, redactor?: SecretRedactor): AgUiA2UiPainter {
|
|
94
|
+
if (typeof options.catalogId !== "string" || options.catalogId.length === 0 || options.catalogId.length > 512) {
|
|
95
|
+
throw new AgUiError("ERR_PRISM_AG_UI_INPUT", "a2ui.catalogId is required");
|
|
96
|
+
}
|
|
97
|
+
const a2uiLimits = resolveAgUiA2UiLimits(options.limits);
|
|
98
|
+
const allowed = new Set(options.allowedCatalogIds?.length ? options.allowedCatalogIds : [options.catalogId]);
|
|
99
|
+
const renderToolName = options.renderToolName ?? "render_a2ui";
|
|
100
|
+
const fixed = options.mode === "fixed-schema" || options.mode === "both";
|
|
101
|
+
const streaming = options.mode === "streaming" || options.mode === "both";
|
|
102
|
+
|
|
103
|
+
const surfaces = new Map<string, { messageId: string; ops: unknown[] }>();
|
|
104
|
+
const streamBuffers = new Map<string, { name?: string; args: string; painted: number }>();
|
|
105
|
+
const streamedSurfaceIds = new Set<string>();
|
|
106
|
+
|
|
107
|
+
const redact = (value: unknown): unknown => redactor?.redact(value) ?? value;
|
|
108
|
+
|
|
109
|
+
const errorEvent = (code: string, message: string): AGUIEvent => ({
|
|
110
|
+
type: EventType.CUSTOM,
|
|
111
|
+
name: A2UI_ERROR_EVENT,
|
|
112
|
+
value: {
|
|
113
|
+
code: truncateA2UiText(code, 128),
|
|
114
|
+
message: truncateA2UiText(message, Math.min(limits.maxErrorBytes, 2_048)),
|
|
115
|
+
},
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
const stamp = (ops: readonly Record<string, unknown>[]): Record<string, unknown>[] | undefined => {
|
|
119
|
+
const out: Record<string, unknown>[] = [];
|
|
120
|
+
for (const op of ops) {
|
|
121
|
+
const stamped = stampCatalog(op, options.catalogId, allowed);
|
|
122
|
+
if (!stamped) return undefined;
|
|
123
|
+
out.push(stamped);
|
|
124
|
+
}
|
|
125
|
+
return out;
|
|
126
|
+
};
|
|
127
|
+
|
|
128
|
+
const paintOps = (toolCallId: string, rawOps: unknown[], mode: "fixed" | "stream"): AGUIEvent[] => {
|
|
129
|
+
if (rawOps.length === 0) return [];
|
|
130
|
+
if (rawOps.length > a2uiLimits.maxOperationsPerMessage) {
|
|
131
|
+
return [errorEvent("ERR_PRISM_A2UI_LIMIT", "Too many A2UI operations")];
|
|
132
|
+
}
|
|
133
|
+
const validated: Record<string, unknown>[] = [];
|
|
134
|
+
for (const item of rawOps) {
|
|
135
|
+
const op = validateA2UiOp(item, a2uiLimits, limits);
|
|
136
|
+
if (!op) return [errorEvent("ERR_PRISM_A2UI_OP", "Invalid A2UI operation")];
|
|
137
|
+
validated.push(op);
|
|
138
|
+
}
|
|
139
|
+
const stamped = stamp(validated);
|
|
140
|
+
if (!stamped) return [errorEvent("ERR_PRISM_A2UI_OP", "Invalid A2UI operation")];
|
|
141
|
+
|
|
142
|
+
const bySurface = groupBySurface(stamped);
|
|
143
|
+
const events: AGUIEvent[] = [];
|
|
144
|
+
for (const [surfaceId, ops] of bySurface) {
|
|
145
|
+
const existing = surfaces.get(surfaceId);
|
|
146
|
+
if (!existing) {
|
|
147
|
+
if (!ops.some((op) => "createSurface" in op)) {
|
|
148
|
+
return [errorEvent("ERR_PRISM_A2UI_OP", "createSurface required before updates")];
|
|
149
|
+
}
|
|
150
|
+
if (surfaces.size >= a2uiLimits.maxSurfacesPerRun) {
|
|
151
|
+
return [errorEvent("ERR_PRISM_A2UI_LIMIT", "Too many A2UI surfaces")];
|
|
152
|
+
}
|
|
153
|
+
const messageId = `a2ui-surface-${surfaceId}-${toolCallId}`;
|
|
154
|
+
const content = projectAgUiJson(redact({ [A2UI_OPERATIONS_KEY]: ops }), limits.maxActivityBytes, limits, "a2ui");
|
|
155
|
+
if (!content || typeof content !== "object" || Array.isArray(content)) {
|
|
156
|
+
return [errorEvent("ERR_PRISM_A2UI_LIMIT", "A2UI activity exceeds bounds")];
|
|
157
|
+
}
|
|
158
|
+
surfaces.set(surfaceId, { messageId, ops: [...ops] });
|
|
159
|
+
if (mode === "stream") streamedSurfaceIds.add(surfaceId);
|
|
160
|
+
events.push({
|
|
161
|
+
type: EventType.ACTIVITY_SNAPSHOT,
|
|
162
|
+
messageId,
|
|
163
|
+
activityType: A2UI_ACTIVITY_TYPE,
|
|
164
|
+
content: content as Record<string, unknown>,
|
|
165
|
+
replace: mode === "stream",
|
|
166
|
+
});
|
|
167
|
+
continue;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
if (mode === "stream") {
|
|
171
|
+
streamedSurfaceIds.add(surfaceId);
|
|
172
|
+
// Progressive replace: cumulative ops for this surface under the stable messageId.
|
|
173
|
+
const merged = [...existing.ops, ...ops];
|
|
174
|
+
existing.ops = merged;
|
|
175
|
+
const content = projectAgUiJson(redact({ [A2UI_OPERATIONS_KEY]: merged }), limits.maxActivityBytes, limits, "a2ui");
|
|
176
|
+
if (!content || typeof content !== "object" || Array.isArray(content)) {
|
|
177
|
+
return [errorEvent("ERR_PRISM_A2UI_LIMIT", "A2UI activity exceeds bounds")];
|
|
178
|
+
}
|
|
179
|
+
events.push({
|
|
180
|
+
type: EventType.ACTIVITY_SNAPSHOT,
|
|
181
|
+
messageId: existing.messageId,
|
|
182
|
+
activityType: A2UI_ACTIVITY_TYPE,
|
|
183
|
+
content: content as Record<string, unknown>,
|
|
184
|
+
replace: true,
|
|
185
|
+
});
|
|
186
|
+
continue;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
// Fixed-schema: subsequent batches become RFC 6902 deltas; drop duplicate createSurface.
|
|
190
|
+
const deltaOps = ops.filter((op) => !("createSurface" in op));
|
|
191
|
+
if (deltaOps.length === 0) continue;
|
|
192
|
+
const patch = deltaOps.map((op) => ({ op: "add", path: `/${A2UI_OPERATIONS_KEY}/-`, value: op }));
|
|
193
|
+
const projected = projectAgUiPatch(patch, limits.maxPatchOperations, limits.maxActivityBytes, limits, "a2uiDelta");
|
|
194
|
+
if (!projected) return [errorEvent("ERR_PRISM_A2UI_LIMIT", "A2UI delta exceeds bounds")];
|
|
195
|
+
existing.ops.push(...deltaOps);
|
|
196
|
+
events.push({
|
|
197
|
+
type: EventType.ACTIVITY_DELTA,
|
|
198
|
+
messageId: existing.messageId,
|
|
199
|
+
activityType: A2UI_ACTIVITY_TYPE,
|
|
200
|
+
patch: [...projected] as ActivityDeltaEvent["patch"],
|
|
201
|
+
});
|
|
202
|
+
}
|
|
203
|
+
return events;
|
|
204
|
+
};
|
|
205
|
+
|
|
206
|
+
return {
|
|
207
|
+
onToolFinished(result) {
|
|
208
|
+
if (!fixed) return [];
|
|
209
|
+
const ops = readOperations(result.value);
|
|
210
|
+
if (ops === undefined) return [];
|
|
211
|
+
if (ops === null) return [errorEvent("ERR_PRISM_A2UI_OP", "Invalid a2ui_operations envelope")];
|
|
212
|
+
// Surfaces already painted via streaming this run are not re-painted from the final envelope.
|
|
213
|
+
const remaining = ops.filter((item) => {
|
|
214
|
+
if (!item || typeof item !== "object" || Array.isArray(item)) return true;
|
|
215
|
+
const key = OP_KEYS.find((name) => name in (item as object));
|
|
216
|
+
if (!key) return true;
|
|
217
|
+
const body = (item as Record<string, unknown>)[key];
|
|
218
|
+
const surfaceId =
|
|
219
|
+
body && typeof body === "object" && !Array.isArray(body) ? (body as Record<string, unknown>).surfaceId : undefined;
|
|
220
|
+
return typeof surfaceId !== "string" || !streamedSurfaceIds.has(surfaceId);
|
|
221
|
+
});
|
|
222
|
+
if (remaining.length === 0) return [];
|
|
223
|
+
return paintOps(result.toolCallId, remaining, "fixed");
|
|
224
|
+
},
|
|
225
|
+
onToolCallDelta(delta) {
|
|
226
|
+
if (!streaming) return [];
|
|
227
|
+
const id = delta.id;
|
|
228
|
+
if (!id) return [];
|
|
229
|
+
const entry = streamBuffers.get(id) ?? { name: delta.name, args: "", painted: 0 };
|
|
230
|
+
if (delta.name) entry.name = delta.name;
|
|
231
|
+
if (delta.argumentsText) entry.args += delta.argumentsText;
|
|
232
|
+
streamBuffers.set(id, entry);
|
|
233
|
+
if (entry.name !== undefined && entry.name !== renderToolName) return [];
|
|
234
|
+
|
|
235
|
+
// Only probe when the delta could close a structure (official framing rule).
|
|
236
|
+
const chunk = delta.argumentsText ?? "";
|
|
237
|
+
if (!/[}\]"]/.test(chunk)) return [];
|
|
238
|
+
|
|
239
|
+
const extracted = extractCompleteOperations(entry.args);
|
|
240
|
+
if (extracted.length <= entry.painted) return [];
|
|
241
|
+
const next = extracted.slice(entry.painted);
|
|
242
|
+
entry.painted = extracted.length;
|
|
243
|
+
return paintOps(id, next, "stream");
|
|
244
|
+
},
|
|
245
|
+
};
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
/**
|
|
249
|
+
* Parse A2UI user-action envelopes from untrusted RunAgentInput.
|
|
250
|
+
* Returns [] when none present; oversized/malformed actions are dropped closed.
|
|
251
|
+
*/
|
|
252
|
+
export function extractAgUiA2UiActions(
|
|
253
|
+
input: ParsedAgUiInput,
|
|
254
|
+
limits: ResolvedAgUiLimits,
|
|
255
|
+
maxPayloadBytes = 16 * 1024,
|
|
256
|
+
): readonly AgUiA2UiAction[] {
|
|
257
|
+
const actions: AgUiA2UiAction[] = [];
|
|
258
|
+
const push = (raw: unknown): void => {
|
|
259
|
+
const action = normalizeAction(raw, limits, maxPayloadBytes);
|
|
260
|
+
if (action) actions.push(action);
|
|
261
|
+
};
|
|
262
|
+
|
|
263
|
+
const forwarded = input.forwardedProps;
|
|
264
|
+
if (forwarded && typeof forwarded === "object" && !Array.isArray(forwarded)) {
|
|
265
|
+
const props = forwarded as Record<string, unknown>;
|
|
266
|
+
if (props.a2uiAction) push(props.a2uiAction);
|
|
267
|
+
if (props.a2uiActions && Array.isArray(props.a2uiActions)) {
|
|
268
|
+
for (const item of props.a2uiActions) push(item);
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
for (const message of input.messages) {
|
|
273
|
+
if (message.role === "tool") {
|
|
274
|
+
try {
|
|
275
|
+
const parsed = typeof message.content === "string" ? JSON.parse(message.content) : message.content;
|
|
276
|
+
if (parsed && typeof parsed === "object" && !Array.isArray(parsed)) {
|
|
277
|
+
const record = parsed as Record<string, unknown>;
|
|
278
|
+
if (record.type === "a2ui-action" || record.a2uiAction || record.surfaceId) push(record.a2uiAction ?? record);
|
|
279
|
+
}
|
|
280
|
+
} catch {
|
|
281
|
+
// ignore non-JSON tool content
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
if (message.role === "user" && Array.isArray(message.content)) {
|
|
285
|
+
for (const part of message.content) {
|
|
286
|
+
if (part && typeof part === "object" && (part as { type?: unknown }).type === "activity") {
|
|
287
|
+
const activity = part as { activityType?: unknown; content?: unknown };
|
|
288
|
+
if (activity.activityType === A2UI_ACTIVITY_TYPE || activity.activityType === "a2ui-action") {
|
|
289
|
+
push(activity.content);
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
return actions;
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
function readOperations(value: unknown): unknown[] | null | undefined {
|
|
299
|
+
if (value === undefined || value === null) return undefined;
|
|
300
|
+
let current: unknown = value;
|
|
301
|
+
if (typeof current === "string") {
|
|
302
|
+
try {
|
|
303
|
+
current = JSON.parse(current);
|
|
304
|
+
} catch {
|
|
305
|
+
return undefined;
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
if (!current || typeof current !== "object" || Array.isArray(current)) return undefined;
|
|
309
|
+
if (!Object.hasOwn(current, A2UI_OPERATIONS_KEY)) return undefined;
|
|
310
|
+
const ops = (current as Record<string, unknown>)[A2UI_OPERATIONS_KEY];
|
|
311
|
+
return Array.isArray(ops) ? ops : null;
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
export function validateA2UiOp(
|
|
315
|
+
value: unknown,
|
|
316
|
+
a2uiLimits: ResolvedAgUiA2UiLimits,
|
|
317
|
+
limits: ResolvedAgUiLimits,
|
|
318
|
+
): Record<string, unknown> | undefined {
|
|
319
|
+
if (!value || typeof value !== "object" || Array.isArray(value)) return undefined;
|
|
320
|
+
const op = value as Record<string, unknown>;
|
|
321
|
+
if (op.version !== A2UI_VERSION && op.version !== "v1.0") return undefined;
|
|
322
|
+
const keys = OP_KEYS.filter((key) => Object.hasOwn(op, key));
|
|
323
|
+
if (keys.length !== 1) return undefined;
|
|
324
|
+
const key = keys[0]!;
|
|
325
|
+
const body = op[key];
|
|
326
|
+
if (!body || typeof body !== "object" || Array.isArray(body)) return undefined;
|
|
327
|
+
const surfaceId = (body as Record<string, unknown>).surfaceId;
|
|
328
|
+
if (typeof surfaceId !== "string" || surfaceId.length === 0 || surfaceId.length > 128) return undefined;
|
|
329
|
+
if (!/^[A-Za-z0-9][A-Za-z0-9._:-]*$/.test(surfaceId)) return undefined;
|
|
330
|
+
try {
|
|
331
|
+
assertBoundedJson(op, a2uiLimits.maxOperationBytes, limits, "a2ui op");
|
|
332
|
+
} catch {
|
|
333
|
+
return undefined;
|
|
334
|
+
}
|
|
335
|
+
if (jsonDepth(op) > a2uiLimits.maxComponentDepth) return undefined;
|
|
336
|
+
return { version: op.version, [key]: body };
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
function stampCatalog(op: Record<string, unknown>, catalogId: string, allowed: ReadonlySet<string>): Record<string, unknown> | undefined {
|
|
340
|
+
if (!("createSurface" in op)) return op;
|
|
341
|
+
const body = op.createSurface;
|
|
342
|
+
if (!body || typeof body !== "object" || Array.isArray(body)) return undefined;
|
|
343
|
+
const surface = { ...(body as Record<string, unknown>) };
|
|
344
|
+
const current = surface.catalogId;
|
|
345
|
+
if (typeof current !== "string" || current.length === 0 || !allowed.has(current)) {
|
|
346
|
+
surface.catalogId = catalogId;
|
|
347
|
+
}
|
|
348
|
+
return { ...op, createSurface: surface };
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
function groupBySurface(ops: readonly Record<string, unknown>[]): Map<string, Record<string, unknown>[]> {
|
|
352
|
+
const grouped = new Map<string, Record<string, unknown>[]>();
|
|
353
|
+
for (const op of ops) {
|
|
354
|
+
const key = OP_KEYS.find((name) => name in op);
|
|
355
|
+
if (!key) continue;
|
|
356
|
+
const body = op[key] as Record<string, unknown>;
|
|
357
|
+
const surfaceId = String(body.surfaceId);
|
|
358
|
+
const list = grouped.get(surfaceId) ?? [];
|
|
359
|
+
list.push(op);
|
|
360
|
+
grouped.set(surfaceId, list);
|
|
361
|
+
}
|
|
362
|
+
return grouped;
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
/**
|
|
366
|
+
* Extract complete A2UI op objects from a partial JSON args buffer.
|
|
367
|
+
* Never returns a partial object — brace-balanced scan only.
|
|
368
|
+
*/
|
|
369
|
+
function extractCompleteOperations(buffer: string): unknown[] {
|
|
370
|
+
const opsKey = buffer.indexOf(`"${A2UI_OPERATIONS_KEY}"`);
|
|
371
|
+
if (opsKey >= 0) {
|
|
372
|
+
const arrayStart = buffer.indexOf("[", opsKey);
|
|
373
|
+
if (arrayStart >= 0) return extractCompleteObjects(buffer.slice(arrayStart));
|
|
374
|
+
}
|
|
375
|
+
// Render-tool shape: synthesize ops once surfaceId + closed components are present.
|
|
376
|
+
const synthesized = synthesizeFromRenderArgs(buffer);
|
|
377
|
+
return synthesized ?? [];
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
function extractCompleteObjects(arraySlice: string): unknown[] {
|
|
381
|
+
if (!arraySlice.startsWith("[")) return [];
|
|
382
|
+
const items: unknown[] = [];
|
|
383
|
+
let i = 1;
|
|
384
|
+
while (i < arraySlice.length) {
|
|
385
|
+
while (i < arraySlice.length && /[\s,]/.test(arraySlice[i]!)) i += 1;
|
|
386
|
+
if (i >= arraySlice.length || arraySlice[i] === "]") break;
|
|
387
|
+
if (arraySlice[i] !== "{") break;
|
|
388
|
+
const start = i;
|
|
389
|
+
let depth = 0;
|
|
390
|
+
let inString = false;
|
|
391
|
+
let escape = false;
|
|
392
|
+
for (; i < arraySlice.length; i += 1) {
|
|
393
|
+
const ch = arraySlice[i]!;
|
|
394
|
+
if (inString) {
|
|
395
|
+
if (escape) escape = false;
|
|
396
|
+
else if (ch === "\\") escape = true;
|
|
397
|
+
else if (ch === '"') inString = false;
|
|
398
|
+
continue;
|
|
399
|
+
}
|
|
400
|
+
if (ch === '"') {
|
|
401
|
+
inString = true;
|
|
402
|
+
continue;
|
|
403
|
+
}
|
|
404
|
+
if (ch === "{") depth += 1;
|
|
405
|
+
else if (ch === "}") {
|
|
406
|
+
depth -= 1;
|
|
407
|
+
if (depth === 0) {
|
|
408
|
+
i += 1;
|
|
409
|
+
try {
|
|
410
|
+
items.push(JSON.parse(arraySlice.slice(start, i)) as unknown);
|
|
411
|
+
} catch {
|
|
412
|
+
return items;
|
|
413
|
+
}
|
|
414
|
+
break;
|
|
415
|
+
}
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
if (depth !== 0) break;
|
|
419
|
+
}
|
|
420
|
+
return items;
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
function synthesizeFromRenderArgs(buffer: string): unknown[] | undefined {
|
|
424
|
+
const surfaceId = extractStringField(buffer, "surfaceId");
|
|
425
|
+
if (!surfaceId) return undefined;
|
|
426
|
+
const components = extractClosedArray(buffer, "components");
|
|
427
|
+
if (!components) return undefined;
|
|
428
|
+
const catalogId = extractStringField(buffer, "catalogId");
|
|
429
|
+
const ops: unknown[] = [
|
|
430
|
+
{
|
|
431
|
+
version: A2UI_VERSION,
|
|
432
|
+
createSurface: { surfaceId, ...(catalogId ? { catalogId } : {}) },
|
|
433
|
+
},
|
|
434
|
+
{
|
|
435
|
+
version: A2UI_VERSION,
|
|
436
|
+
updateComponents: { surfaceId, components },
|
|
437
|
+
},
|
|
438
|
+
];
|
|
439
|
+
const data = extractClosedObjectField(buffer, "data");
|
|
440
|
+
if (data !== undefined) {
|
|
441
|
+
ops.push({ version: A2UI_VERSION, updateDataModel: { surfaceId, value: data } });
|
|
442
|
+
}
|
|
443
|
+
return ops;
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
function extractStringField(buffer: string, field: string): string | undefined {
|
|
447
|
+
const re = new RegExp(`"${field}"\\s*:\\s*"((?:\\\\.|[^"\\\\])*)"`);
|
|
448
|
+
const match = re.exec(buffer);
|
|
449
|
+
if (!match) return undefined;
|
|
450
|
+
try {
|
|
451
|
+
return JSON.parse(`"${match[1]}"`) as string;
|
|
452
|
+
} catch {
|
|
453
|
+
return undefined;
|
|
454
|
+
}
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
function extractClosedArray(buffer: string, field: string): unknown[] | undefined {
|
|
458
|
+
const key = buffer.indexOf(`"${field}"`);
|
|
459
|
+
if (key < 0) return undefined;
|
|
460
|
+
const start = buffer.indexOf("[", key);
|
|
461
|
+
if (start < 0) return undefined;
|
|
462
|
+
const end = findMatching(buffer, start, "[", "]");
|
|
463
|
+
if (end < 0) return undefined;
|
|
464
|
+
try {
|
|
465
|
+
const value = JSON.parse(buffer.slice(start, end + 1)) as unknown;
|
|
466
|
+
return Array.isArray(value) ? value : undefined;
|
|
467
|
+
} catch {
|
|
468
|
+
return undefined;
|
|
469
|
+
}
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
function extractClosedObjectField(buffer: string, field: string): unknown | undefined {
|
|
473
|
+
const key = buffer.indexOf(`"${field}"`);
|
|
474
|
+
if (key < 0) return undefined;
|
|
475
|
+
const start = buffer.indexOf("{", key);
|
|
476
|
+
if (start < 0) return undefined;
|
|
477
|
+
const end = findMatching(buffer, start, "{", "}");
|
|
478
|
+
if (end < 0) return undefined;
|
|
479
|
+
try {
|
|
480
|
+
return JSON.parse(buffer.slice(start, end + 1)) as unknown;
|
|
481
|
+
} catch {
|
|
482
|
+
return undefined;
|
|
483
|
+
}
|
|
484
|
+
}
|
|
485
|
+
|
|
486
|
+
function findMatching(buffer: string, start: number, open: string, close: string): number {
|
|
487
|
+
let depth = 0;
|
|
488
|
+
let inString = false;
|
|
489
|
+
let escape = false;
|
|
490
|
+
for (let i = start; i < buffer.length; i += 1) {
|
|
491
|
+
const ch = buffer[i]!;
|
|
492
|
+
if (inString) {
|
|
493
|
+
if (escape) escape = false;
|
|
494
|
+
else if (ch === "\\") escape = true;
|
|
495
|
+
else if (ch === '"') inString = false;
|
|
496
|
+
continue;
|
|
497
|
+
}
|
|
498
|
+
if (ch === '"') {
|
|
499
|
+
inString = true;
|
|
500
|
+
continue;
|
|
501
|
+
}
|
|
502
|
+
if (ch === open) depth += 1;
|
|
503
|
+
else if (ch === close) {
|
|
504
|
+
depth -= 1;
|
|
505
|
+
if (depth === 0) return i;
|
|
506
|
+
}
|
|
507
|
+
}
|
|
508
|
+
return -1;
|
|
509
|
+
}
|
|
510
|
+
|
|
511
|
+
function normalizeAction(raw: unknown, limits: ResolvedAgUiLimits, maxPayloadBytes: number): AgUiA2UiAction | undefined {
|
|
512
|
+
if (!raw || typeof raw !== "object" || Array.isArray(raw)) return undefined;
|
|
513
|
+
const record = raw as Record<string, unknown>;
|
|
514
|
+
const userAction =
|
|
515
|
+
record.userAction && typeof record.userAction === "object" && !Array.isArray(record.userAction)
|
|
516
|
+
? (record.userAction as Record<string, unknown>)
|
|
517
|
+
: record;
|
|
518
|
+
const surfaceId = typeof userAction.surfaceId === "string" ? userAction.surfaceId : undefined;
|
|
519
|
+
const actionName =
|
|
520
|
+
typeof userAction.actionName === "string" ? userAction.actionName : typeof userAction.name === "string" ? userAction.name : undefined;
|
|
521
|
+
if (!surfaceId || !actionName) return undefined;
|
|
522
|
+
if (surfaceId.length > 128 || actionName.length > 256) return undefined;
|
|
523
|
+
if (!/^[A-Za-z0-9][A-Za-z0-9._:-]*$/.test(surfaceId)) return undefined;
|
|
524
|
+
const payload = userAction.payload ?? userAction.context;
|
|
525
|
+
if (payload !== undefined) {
|
|
526
|
+
try {
|
|
527
|
+
assertBoundedJson(payload, maxPayloadBytes, limits, "a2ui action");
|
|
528
|
+
} catch {
|
|
529
|
+
return undefined;
|
|
530
|
+
}
|
|
531
|
+
}
|
|
532
|
+
return {
|
|
533
|
+
type: "a2ui-action",
|
|
534
|
+
surfaceId,
|
|
535
|
+
actionName,
|
|
536
|
+
...(payload === undefined ? {} : { payload }),
|
|
537
|
+
};
|
|
538
|
+
}
|
|
539
|
+
|
|
540
|
+
function jsonDepth(value: unknown, depth = 0): number {
|
|
541
|
+
if (value === null || typeof value !== "object") return depth;
|
|
542
|
+
if (Array.isArray(value)) {
|
|
543
|
+
let max = depth;
|
|
544
|
+
for (const item of value) max = Math.max(max, jsonDepth(item, depth + 1));
|
|
545
|
+
return max;
|
|
546
|
+
}
|
|
547
|
+
let max = depth;
|
|
548
|
+
for (const item of Object.values(value as Record<string, unknown>)) max = Math.max(max, jsonDepth(item, depth + 1));
|
|
549
|
+
return max;
|
|
550
|
+
}
|
|
551
|
+
|
|
552
|
+
function clamp(value: number, min: number, hard: number, name: string): number {
|
|
553
|
+
if (!Number.isSafeInteger(value) || value < min || value > hard) {
|
|
554
|
+
throw new AgUiError("ERR_PRISM_AG_UI_LIMIT", `${name} must be a safe integer from ${min} through ${hard}`);
|
|
555
|
+
}
|
|
556
|
+
return value;
|
|
557
|
+
}
|
|
558
|
+
|
|
559
|
+
export function truncateA2UiText(value: string, maxBytes: number): string {
|
|
560
|
+
if (Buffer.byteLength(value) <= maxBytes) return value;
|
|
561
|
+
let bytes = 0;
|
|
562
|
+
let end = 0;
|
|
563
|
+
for (const char of value) {
|
|
564
|
+
const size = Buffer.byteLength(char);
|
|
565
|
+
if (bytes + size > maxBytes - 1) break;
|
|
566
|
+
bytes += size;
|
|
567
|
+
end += char.length;
|
|
568
|
+
}
|
|
569
|
+
return `${value.slice(0, end)}…`;
|
|
570
|
+
}
|