@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,419 @@
|
|
|
1
|
+
import type { SessionUpdate, ToolCallContent, ToolCallLocation, ToolKind } from "@agentclientprotocol/sdk";
|
|
2
|
+
import type { AgentEvent, ToolCallContent as PrismToolCall, SecretRedactor, ToolResult, Usage } from "@arnilo/prism";
|
|
3
|
+
import type { AcpUsageSeam } from "./capabilities.js";
|
|
4
|
+
import { type AcpLimitOptions, resolveAcpLimits } from "./limits.js";
|
|
5
|
+
import type { AcpCoWorkEvent, AcpLifecycleEvent, AcpProjection } from "./projection.js";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Maps shipped `CodingLifecycleEvent` values to stable ACP v1 session updates
|
|
9
|
+
* (freeze `lifecycleEventMapping`). Deny-by-default: process/worktree events
|
|
10
|
+
* surface only through the shared projection allow-list; `file_changed` needs
|
|
11
|
+
* the event's toolCallId; diffs need the `fileDiff` allow-list hook and are
|
|
12
|
+
* byte-capped at `acpDiffBytes`. `configuration_changed` returns [] — the
|
|
13
|
+
* agent wires it to `config_option_update` (it owns the host configOptions
|
|
14
|
+
* seam and the per-session current values). Never raw file bodies, args, or
|
|
15
|
+
* secrets; all text passes the shared redactor.
|
|
16
|
+
*/
|
|
17
|
+
export function createAcpLifecycleMapper(options: AcpEventMapperOptions = {}): AcpLifecycleMapper {
|
|
18
|
+
const limits = resolveAcpLimits(options.limits);
|
|
19
|
+
const text = (value: string, maxBytes = limits.maxTextBytes) =>
|
|
20
|
+
truncate(options.redactor?.redact(value) ?? value, Math.min(maxBytes, limits.maxEventBytes));
|
|
21
|
+
const projectedText = async (event: AcpLifecycleEvent): Promise<string | undefined> => {
|
|
22
|
+
try {
|
|
23
|
+
const value = await options.projection?.lifecycle?.(event);
|
|
24
|
+
return typeof value === "string" ? text(value) : undefined;
|
|
25
|
+
} catch {
|
|
26
|
+
return undefined;
|
|
27
|
+
}
|
|
28
|
+
};
|
|
29
|
+
const projectedDiff = async (event: AcpLifecycleEvent): Promise<ToolCallContent | undefined> => {
|
|
30
|
+
try {
|
|
31
|
+
const value = await options.projection?.fileDiff?.(event);
|
|
32
|
+
if (!value || typeof value !== "object") return undefined;
|
|
33
|
+
const path = value.path;
|
|
34
|
+
const newText = value.newText;
|
|
35
|
+
if (typeof path !== "string" || path.length === 0 || typeof newText !== "string") return undefined;
|
|
36
|
+
const oldText = value.oldText;
|
|
37
|
+
const diff: ToolCallContent = {
|
|
38
|
+
type: "diff",
|
|
39
|
+
path: text(path),
|
|
40
|
+
...(typeof oldText === "string" && oldText.length > 0 ? { oldText: text(oldText) } : {}),
|
|
41
|
+
newText: text(newText),
|
|
42
|
+
};
|
|
43
|
+
return Buffer.byteLength(JSON.stringify(diff), "utf8") <= limits.acpDiffBytes ? diff : undefined;
|
|
44
|
+
} catch {
|
|
45
|
+
return undefined;
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
return {
|
|
49
|
+
async map(event) {
|
|
50
|
+
switch (event.type) {
|
|
51
|
+
case "file_changed": {
|
|
52
|
+
if (!event.toolCallId) return [];
|
|
53
|
+
const diff = await projectedDiff(event);
|
|
54
|
+
return [
|
|
55
|
+
{
|
|
56
|
+
sessionUpdate: "tool_call_update",
|
|
57
|
+
toolCallId: text(event.toolCallId),
|
|
58
|
+
...(diff ? { content: [diff] } : {}),
|
|
59
|
+
locations: [{ path: text(event.path ?? "") }],
|
|
60
|
+
},
|
|
61
|
+
];
|
|
62
|
+
}
|
|
63
|
+
case "worktree_changed":
|
|
64
|
+
case "process_started":
|
|
65
|
+
case "process_exited":
|
|
66
|
+
case "process_killed": {
|
|
67
|
+
const value = await projectedText(event);
|
|
68
|
+
if (value === undefined) return [];
|
|
69
|
+
const messageId =
|
|
70
|
+
event.type === "worktree_changed"
|
|
71
|
+
? `prism:worktree:${event.path ?? ""}`
|
|
72
|
+
: `prism:process:${event.sessionId ?? ""}:${event.processId ?? ""}`;
|
|
73
|
+
return [{ sessionUpdate: "agent_message_chunk", messageId: text(messageId), content: { type: "text", text: value } }];
|
|
74
|
+
}
|
|
75
|
+
case "permission_denied": {
|
|
76
|
+
const toolCallId = event.toolCallId ?? (event.approvalId ? `prism:denied:${event.approvalId}` : undefined);
|
|
77
|
+
if (!toolCallId) return [];
|
|
78
|
+
return [{ sessionUpdate: "tool_call_update", toolCallId: text(toolCallId), title: text(event.toolName ?? ""), status: "failed" }];
|
|
79
|
+
}
|
|
80
|
+
case "configuration_changed":
|
|
81
|
+
// Agent-wired: needs the host configOptions seam + per-session values (config_option_update).
|
|
82
|
+
return [];
|
|
83
|
+
case "plan_changed": {
|
|
84
|
+
// F5: complete entry list per update; the client replaces its plan wholesale.
|
|
85
|
+
const entries: Array<{
|
|
86
|
+
content: string;
|
|
87
|
+
priority: "high" | "medium" | "low";
|
|
88
|
+
status: "pending" | "in_progress" | "completed";
|
|
89
|
+
}> = (event.todos ?? []).map((todo) => ({
|
|
90
|
+
content: text(todo.text),
|
|
91
|
+
priority: "medium",
|
|
92
|
+
status: todo.done ? "completed" : "pending",
|
|
93
|
+
}));
|
|
94
|
+
return [
|
|
95
|
+
{
|
|
96
|
+
sessionUpdate: "plan_update",
|
|
97
|
+
plan: { type: "items", planId: text(event.planPath ?? ""), entries },
|
|
98
|
+
},
|
|
99
|
+
];
|
|
100
|
+
}
|
|
101
|
+
case "plan_removed":
|
|
102
|
+
return [{ sessionUpdate: "plan_removed", planId: text(event.planPath ?? "") }];
|
|
103
|
+
case "subagent_started":
|
|
104
|
+
case "subagent_stopped": {
|
|
105
|
+
const status = event.type === "subagent_started" ? "started" : event.status;
|
|
106
|
+
return [
|
|
107
|
+
{
|
|
108
|
+
sessionUpdate: "agent_message_chunk",
|
|
109
|
+
messageId: text(`prism:subagent:${event.delegationId ?? ""}`),
|
|
110
|
+
content: { type: "text", text: text(`Subagent ${event.childId ?? ""} ${status}`) },
|
|
111
|
+
},
|
|
112
|
+
];
|
|
113
|
+
}
|
|
114
|
+
default:
|
|
115
|
+
// process_released/expired/unknown are not shipped lifecycle mappings (freeze deferred).
|
|
116
|
+
return [];
|
|
117
|
+
}
|
|
118
|
+
},
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
export interface AcpLifecycleMapper {
|
|
123
|
+
/** Maps one coding lifecycle event to safe ACP session updates; empty when the event carries no consumer-safe update. */
|
|
124
|
+
map(event: AcpLifecycleEvent): Promise<readonly SessionUpdate[]>;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/** Options shared by the event and lifecycle mappers (redactor, projection allow-list, limits). */
|
|
128
|
+
export interface AcpEventMapperOptions {
|
|
129
|
+
readonly redactor?: SecretRedactor;
|
|
130
|
+
/** Shared host allow-list; tool text is omitted unless explicitly projected. */
|
|
131
|
+
readonly projection?: AcpProjection;
|
|
132
|
+
readonly limits?: AcpLimitOptions;
|
|
133
|
+
/** Host-reported context window (B1); absent => `usage_update` is omitted, never `size = used`. */
|
|
134
|
+
readonly usage?: AcpUsageSeam;
|
|
135
|
+
/** Run signal forwarded to the usage seam. */
|
|
136
|
+
readonly signal?: AbortSignal;
|
|
137
|
+
/** Explicit tool kinds (B4); consulted before the name heuristic. */
|
|
138
|
+
readonly toolKinds?: ReadonlyMap<string, ToolKind>;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
export interface AcpEventMapper {
|
|
142
|
+
map(event: AgentEvent): Promise<readonly SessionUpdate[]>;
|
|
143
|
+
/** Projects one co-work event to a safe ACP session update; malformed input yields none. */
|
|
144
|
+
mapCoWork(event: AcpCoWorkEvent): Promise<readonly SessionUpdate[]>;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/** Maps redacted Prism lifecycle events to stable ACP v1 session updates. */
|
|
148
|
+
export function createAcpEventMapper(options: AcpEventMapperOptions = {}): AcpEventMapper {
|
|
149
|
+
const limits = resolveAcpLimits(options.limits);
|
|
150
|
+
let messageId: string | undefined;
|
|
151
|
+
let textDeltaSeen = false;
|
|
152
|
+
let thoughtDeltaSeen = false;
|
|
153
|
+
|
|
154
|
+
const text = (value: string, maxBytes = limits.maxTextBytes) =>
|
|
155
|
+
truncate(options.redactor?.redact(value) ?? value, Math.min(maxBytes, limits.maxEventBytes));
|
|
156
|
+
const tool = async (call: PrismToolCall) => {
|
|
157
|
+
const input = await projected(() => options.projection?.toolArguments?.(call));
|
|
158
|
+
return {
|
|
159
|
+
toolCallId: text(call.id),
|
|
160
|
+
title: text(call.name),
|
|
161
|
+
kind: kind(call.name, options.toolKinds),
|
|
162
|
+
status: "in_progress" as const,
|
|
163
|
+
...(input ? { content: [content(input)] } : {}),
|
|
164
|
+
};
|
|
165
|
+
};
|
|
166
|
+
const finish = async (id: string, name: string, status: "completed" | "failed", result?: ToolResult): Promise<SessionUpdate> => {
|
|
167
|
+
const output = result ? await projectedResult(result) : undefined;
|
|
168
|
+
const locations = result ? await projectedLocations(result) : undefined;
|
|
169
|
+
const diff = result ? await projectedDiff(result) : undefined;
|
|
170
|
+
return {
|
|
171
|
+
sessionUpdate: "tool_call_update",
|
|
172
|
+
toolCallId: text(id),
|
|
173
|
+
title: text(name),
|
|
174
|
+
status,
|
|
175
|
+
...(output || diff ? { content: [...(output ? [output] : []), ...(diff ? [diff] : [])] } : {}),
|
|
176
|
+
...(locations ? { locations } : {}),
|
|
177
|
+
};
|
|
178
|
+
};
|
|
179
|
+
const projected = async (callback: () => string | undefined | Promise<string | undefined>): Promise<string | undefined> => {
|
|
180
|
+
try {
|
|
181
|
+
const value = await callback();
|
|
182
|
+
return typeof value === "string" ? text(value) : undefined;
|
|
183
|
+
} catch {
|
|
184
|
+
return undefined;
|
|
185
|
+
}
|
|
186
|
+
};
|
|
187
|
+
/** B1: host-reported context window; absent/undefined/throw/invalid => omit the update (never `size = used`). */
|
|
188
|
+
const contextWindow = async (model?: string): Promise<number | undefined> => {
|
|
189
|
+
try {
|
|
190
|
+
const size = await options.usage?.contextWindow?.({ model, signal: options.signal ?? new AbortController().signal });
|
|
191
|
+
return typeof size === "number" && Number.isFinite(size) && size > 0 ? size : undefined;
|
|
192
|
+
} catch {
|
|
193
|
+
return undefined;
|
|
194
|
+
}
|
|
195
|
+
};
|
|
196
|
+
const usage = async (value: Usage, model: string | undefined): Promise<readonly SessionUpdate[]> => {
|
|
197
|
+
const used = value.totalTokens ?? (value.inputTokens ?? 0) + (value.outputTokens ?? 0);
|
|
198
|
+
const size = await contextWindow(model);
|
|
199
|
+
return size === undefined ? [] : [{ sessionUpdate: "usage_update", used: Math.max(0, used), size: Math.max(1, size) }];
|
|
200
|
+
};
|
|
201
|
+
/** Host allow-list for tool results: string → text content; `{ type: "image" }` → image content (F8). */
|
|
202
|
+
const projectedResult = async (result: ToolResult): Promise<ToolCallContent | undefined> => {
|
|
203
|
+
try {
|
|
204
|
+
const value = await options.projection?.toolResult?.(result);
|
|
205
|
+
if (typeof value === "string") return content(text(value));
|
|
206
|
+
if (!value || typeof value !== "object" || value.type !== "image") return undefined;
|
|
207
|
+
const { data, mimeType } = value;
|
|
208
|
+
if (typeof data !== "string" || data.length === 0 || typeof mimeType !== "string" || mimeType.length === 0) return undefined;
|
|
209
|
+
// Drop, don't truncate: a sliced base64 payload is corrupt. Redactor stays off binary data.
|
|
210
|
+
if (Buffer.byteLength(data, "utf8") > limits.acpImageBytes) return undefined;
|
|
211
|
+
return { type: "content", content: { type: "image", data, mimeType } };
|
|
212
|
+
} catch {
|
|
213
|
+
return undefined;
|
|
214
|
+
}
|
|
215
|
+
};
|
|
216
|
+
/** Host allow-list for file locations: count-capped at acpLocationsPerUpdate, entries validated. */
|
|
217
|
+
const projectedLocations = async (result: ToolResult): Promise<ToolCallLocation[] | undefined> => {
|
|
218
|
+
try {
|
|
219
|
+
const value = await options.projection?.toolLocations?.(result);
|
|
220
|
+
if (!value) return undefined;
|
|
221
|
+
const capped = value.slice(0, limits.acpLocationsPerUpdate);
|
|
222
|
+
const entries: ToolCallLocation[] = [];
|
|
223
|
+
for (const entry of capped) {
|
|
224
|
+
if (typeof entry !== "object" || entry === null) continue;
|
|
225
|
+
const path = entry.path;
|
|
226
|
+
if (typeof path !== "string" || path.length === 0) continue;
|
|
227
|
+
const line = entry.line;
|
|
228
|
+
if (line !== undefined && line !== null && (!Number.isInteger(line) || line < 0)) continue;
|
|
229
|
+
entries.push({ path: text(path), ...(line === undefined || line === null ? {} : { line }) });
|
|
230
|
+
}
|
|
231
|
+
return entries.length > 0 ? entries : undefined;
|
|
232
|
+
} catch {
|
|
233
|
+
return undefined;
|
|
234
|
+
}
|
|
235
|
+
};
|
|
236
|
+
/** Host allow-list for file diffs: single diff, redacted, byte-capped at acpDiffBytes. */
|
|
237
|
+
const projectedDiff = async (result: ToolResult): Promise<ToolCallContent | undefined> => {
|
|
238
|
+
try {
|
|
239
|
+
const value = await options.projection?.toolDiff?.(result);
|
|
240
|
+
if (!value || typeof value !== "object") return undefined;
|
|
241
|
+
const path = value.path;
|
|
242
|
+
const newText = value.newText;
|
|
243
|
+
if (typeof path !== "string" || path.length === 0 || typeof newText !== "string") return undefined;
|
|
244
|
+
const oldText = value.oldText;
|
|
245
|
+
const diff: ToolCallContent = {
|
|
246
|
+
type: "diff",
|
|
247
|
+
path: text(path),
|
|
248
|
+
...(typeof oldText === "string" && oldText.length > 0 ? { oldText: text(oldText) } : {}),
|
|
249
|
+
newText: text(newText),
|
|
250
|
+
};
|
|
251
|
+
return Buffer.byteLength(JSON.stringify(diff), "utf8") <= limits.acpDiffBytes ? diff : undefined;
|
|
252
|
+
} catch {
|
|
253
|
+
return undefined;
|
|
254
|
+
}
|
|
255
|
+
};
|
|
256
|
+
|
|
257
|
+
return {
|
|
258
|
+
async mapCoWork(input) {
|
|
259
|
+
const payload = await projectCoWork(input, options.redactor, options.projection, limits.maxTextBytes);
|
|
260
|
+
if (payload === undefined) return [];
|
|
261
|
+
return [message(`prism:cowork:${input.kind}`, truncate(JSON.stringify(payload), limits.maxTextBytes))];
|
|
262
|
+
},
|
|
263
|
+
async map(input) {
|
|
264
|
+
const event = options.redactor?.redact(input) ?? input;
|
|
265
|
+
switch (event.type) {
|
|
266
|
+
case "message.delta":
|
|
267
|
+
// F1: thinking deltas ride agent_thought_chunk (same messageId scheme as
|
|
268
|
+
// text); image/audio deltas stay dropped.
|
|
269
|
+
if (event.content.type === "thinking") {
|
|
270
|
+
messageId ??= `${text(event.runId)}:message`;
|
|
271
|
+
thoughtDeltaSeen = true;
|
|
272
|
+
return [thought(messageId ?? `${text(event.runId)}:message`, text(event.content.text))];
|
|
273
|
+
}
|
|
274
|
+
if (event.content.type !== "text") return [];
|
|
275
|
+
messageId ??= `${text(event.runId)}:message`;
|
|
276
|
+
textDeltaSeen = true;
|
|
277
|
+
return [message(messageId ?? `${text(event.runId)}:message`, text(event.content.text))];
|
|
278
|
+
case "message.completed": {
|
|
279
|
+
if (event.message.role !== "assistant") return [];
|
|
280
|
+
const id = messageId ?? text(event.message.id ?? `${event.runId}:message`);
|
|
281
|
+
// F1: emit finished blocks only for the kinds that had no live delta,
|
|
282
|
+
// so a mixed text+thinking message never loses either channel.
|
|
283
|
+
const updates = event.message.content.flatMap((block) => {
|
|
284
|
+
if (block.type === "text" && !textDeltaSeen) return [message(id, text(block.text))];
|
|
285
|
+
if (block.type === "thinking" && !thoughtDeltaSeen) return [thought(id, text(block.text))];
|
|
286
|
+
return [];
|
|
287
|
+
});
|
|
288
|
+
messageId = undefined;
|
|
289
|
+
textDeltaSeen = false;
|
|
290
|
+
thoughtDeltaSeen = false;
|
|
291
|
+
return updates;
|
|
292
|
+
}
|
|
293
|
+
case "tool.started":
|
|
294
|
+
return [{ sessionUpdate: "tool_call", ...(await tool(event.call)) }];
|
|
295
|
+
case "tool.progress":
|
|
296
|
+
return [
|
|
297
|
+
{ sessionUpdate: "tool_call_update", toolCallId: text(event.toolCallId), title: text(event.name), status: "in_progress" },
|
|
298
|
+
];
|
|
299
|
+
case "tool.completed":
|
|
300
|
+
return [
|
|
301
|
+
await finish(
|
|
302
|
+
event.toolCallId || event.result?.toolCallId,
|
|
303
|
+
event.name || event.result?.name,
|
|
304
|
+
event.status === "finished" ? "completed" : "failed",
|
|
305
|
+
event.status === "finished" ? event.result : undefined,
|
|
306
|
+
),
|
|
307
|
+
];
|
|
308
|
+
case "turn.completed":
|
|
309
|
+
// B2: a failed provider turn is a request-level condition, not transcript content;
|
|
310
|
+
// the agent's forward() throws ERR_PRISM_ACP_RUN when the run ends in a terminal
|
|
311
|
+
// `error` event (retryable turn failures stay silent here and may recover).
|
|
312
|
+
return event.usage && event.metadata ? await usage(event.usage, event.metadata.model.model) : [];
|
|
313
|
+
case "decision.resolved":
|
|
314
|
+
return event.decisions.some((decision) => decision.outcome === "deny")
|
|
315
|
+
? [message(`${text(event.runId)}:status`, "Run denied")]
|
|
316
|
+
: [];
|
|
317
|
+
case "run.failed":
|
|
318
|
+
// B2: run-level errors fail the prompt request (forward() throws); never a fake chunk.
|
|
319
|
+
return [];
|
|
320
|
+
default:
|
|
321
|
+
return [];
|
|
322
|
+
}
|
|
323
|
+
},
|
|
324
|
+
};
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
function message(messageId: string, text: string): SessionUpdate {
|
|
328
|
+
return { sessionUpdate: "agent_message_chunk", messageId, content: { type: "text", text } };
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
// F1: thinking content rides the SDK's agent_thought_chunk variant (a ContentChunk
|
|
332
|
+
// with the thought text, same messageId scheme as text chunks).
|
|
333
|
+
function thought(messageId: string, text: string): SessionUpdate {
|
|
334
|
+
return { sessionUpdate: "agent_thought_chunk", messageId, content: { type: "text", text } };
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
function content(text: string): ToolCallContent {
|
|
338
|
+
return { type: "content", content: { type: "text", text } };
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
function kind(name: string, toolKinds?: ReadonlyMap<string, ToolKind>): ToolKind {
|
|
342
|
+
const explicit = toolKinds?.get(name);
|
|
343
|
+
if (explicit) return explicit;
|
|
344
|
+
if (name.includes("read")) return "read";
|
|
345
|
+
if (name.includes("edit") || name.includes("write")) return "edit";
|
|
346
|
+
if (name.includes("delete")) return "delete";
|
|
347
|
+
if (name.includes("search") || name.includes("list")) return "search";
|
|
348
|
+
if (name.includes("shell") || name.includes("exec") || name.includes("bash")) return "execute";
|
|
349
|
+
if (name.includes("fetch")) return "fetch";
|
|
350
|
+
return "other";
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
const COWORK_KINDS = new Set([
|
|
354
|
+
"artifact.progress",
|
|
355
|
+
"artifact.approval.requested",
|
|
356
|
+
"draft.connector.pending",
|
|
357
|
+
"browser.snapshot",
|
|
358
|
+
"artifact.download.link",
|
|
359
|
+
]);
|
|
360
|
+
|
|
361
|
+
/** Same fail-closed shape check the AG-UI projector uses. Kept here so this package does not import AG-UI. */
|
|
362
|
+
async function projectCoWork(
|
|
363
|
+
event: AcpCoWorkEvent,
|
|
364
|
+
redactor: SecretRedactor | undefined,
|
|
365
|
+
projection: AcpProjection | undefined,
|
|
366
|
+
maxBytes: number,
|
|
367
|
+
): Promise<Record<string, unknown> | undefined> {
|
|
368
|
+
if (!event || typeof event !== "object" || typeof event.kind !== "string" || !COWORK_KINDS.has(event.kind)) return undefined;
|
|
369
|
+
if (!coWorkShape(event)) return undefined;
|
|
370
|
+
try {
|
|
371
|
+
const shaped = (await projection?.coWork?.(event)) ?? event;
|
|
372
|
+
const redacted = redactor?.redact(shaped) ?? shaped;
|
|
373
|
+
const serialized = JSON.stringify(redacted);
|
|
374
|
+
if (serialized === undefined || Buffer.byteLength(serialized, "utf8") > maxBytes) return undefined;
|
|
375
|
+
const parsed = JSON.parse(serialized) as unknown;
|
|
376
|
+
if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) return undefined;
|
|
377
|
+
return { kind: event.kind, ...(parsed as Record<string, unknown>) };
|
|
378
|
+
} catch {
|
|
379
|
+
return undefined;
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
function coWorkShape(event: AcpCoWorkEvent): boolean {
|
|
384
|
+
const record = event as Record<string, unknown>;
|
|
385
|
+
const string = (key: string) => typeof record[key] === "string" && (record[key] as string).length > 0;
|
|
386
|
+
const number = (key: string) => typeof record[key] === "number" && Number.isFinite(record[key]);
|
|
387
|
+
switch (event.kind) {
|
|
388
|
+
case "artifact.progress":
|
|
389
|
+
return string("artifactId") && number("version") && string("status") && (record.progress === undefined || number("progress"));
|
|
390
|
+
case "artifact.approval.requested":
|
|
391
|
+
return (
|
|
392
|
+
string("artifactId") &&
|
|
393
|
+
number("version") &&
|
|
394
|
+
(record.reviewer === undefined || string("reviewer")) &&
|
|
395
|
+
(record.reason === undefined || string("reason"))
|
|
396
|
+
);
|
|
397
|
+
case "draft.connector.pending":
|
|
398
|
+
return string("connectorId") && string("scope") && string("status");
|
|
399
|
+
case "browser.snapshot":
|
|
400
|
+
return string("snapshotId") && string("summary");
|
|
401
|
+
case "artifact.download.link":
|
|
402
|
+
return string("artifactId") && number("version") && string("link") && string("expiresAt");
|
|
403
|
+
default:
|
|
404
|
+
return false;
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
function truncate(value: string, maxBytes: number): string {
|
|
409
|
+
if (Buffer.byteLength(value, "utf8") <= maxBytes) return value;
|
|
410
|
+
let bytes = 0;
|
|
411
|
+
let out = "";
|
|
412
|
+
for (const char of value) {
|
|
413
|
+
const size = Buffer.byteLength(char, "utf8");
|
|
414
|
+
if (bytes + size > maxBytes - 3) break;
|
|
415
|
+
bytes += size;
|
|
416
|
+
out += char;
|
|
417
|
+
}
|
|
418
|
+
return `${out}…`;
|
|
419
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import type { CreateElicitationRequest, ElicitationSchema, RequestPermissionResponse } from "@agentclientprotocol/sdk";
|
|
2
|
+
import type { ApprovalOutcome, Decision, PendingDecision } from "@arnilo/prism";
|
|
3
|
+
|
|
4
|
+
/** optionId is the SDK kind, so the response maps without a side table. */
|
|
5
|
+
export const PERMISSION_OPTIONS = [
|
|
6
|
+
{ optionId: "allow_once", name: "Allow once", kind: "allow_once" },
|
|
7
|
+
{ optionId: "allow_always", name: "Allow always", kind: "allow_always" },
|
|
8
|
+
{ optionId: "reject_once", name: "Reject once", kind: "reject_once" },
|
|
9
|
+
{ optionId: "reject_always", name: "Reject always", kind: "reject_always" },
|
|
10
|
+
] as const;
|
|
11
|
+
|
|
12
|
+
const OUTCOMES: Readonly<Record<string, ApprovalOutcome>> = {
|
|
13
|
+
allow_once: "allow_once",
|
|
14
|
+
allow_always: "allow_for_run",
|
|
15
|
+
reject_once: "reject_once",
|
|
16
|
+
reject_always: "reject_for_run",
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
/** Cancelled, unknown optionId, or a client error is `reject_once`. Never an allow. */
|
|
20
|
+
export function decisionForPermission(pending: PendingDecision, response: RequestPermissionResponse | undefined): Decision {
|
|
21
|
+
const selected = response?.outcome.outcome === "selected" ? response.outcome.optionId : undefined;
|
|
22
|
+
return { approvalId: pending.approvalId, outcome: (selected && OUTCOMES[selected]) || "reject_once" };
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export function elicitationRequest(
|
|
26
|
+
sessionId: string,
|
|
27
|
+
title: string,
|
|
28
|
+
detail: string | undefined,
|
|
29
|
+
schema: ElicitationSchema,
|
|
30
|
+
): CreateElicitationRequest {
|
|
31
|
+
return {
|
|
32
|
+
mode: "form",
|
|
33
|
+
message: detail && detail.length > 0 ? `${title}\n${detail}` : title,
|
|
34
|
+
requestedSchema: schema,
|
|
35
|
+
sessionId,
|
|
36
|
+
};
|
|
37
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import type { ToolCallContent, ToolResult } from "@arnilo/prism";
|
|
2
|
+
|
|
3
|
+
type Awaitable<T> = T | Promise<T>;
|
|
4
|
+
|
|
5
|
+
/** Coding lifecycle event fields the ACP mapper reads. `any` so the old AG-UI callers stay assignable until Task 3. */
|
|
6
|
+
export interface AcpLifecycleEvent {
|
|
7
|
+
readonly type: string;
|
|
8
|
+
readonly toolCallId?: string;
|
|
9
|
+
readonly path?: string;
|
|
10
|
+
readonly approvalId?: string;
|
|
11
|
+
readonly toolName?: string;
|
|
12
|
+
readonly sessionId?: string;
|
|
13
|
+
readonly processId?: string;
|
|
14
|
+
readonly planPath?: string;
|
|
15
|
+
readonly todos?: readonly { readonly text: string; readonly done: boolean; readonly id?: string }[];
|
|
16
|
+
readonly delegationId?: string;
|
|
17
|
+
readonly childId?: string;
|
|
18
|
+
readonly status?: string;
|
|
19
|
+
readonly [key: string]: any;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** Co-work event the mapper projects. Malformed kinds are dropped. */
|
|
23
|
+
export interface AcpCoWorkEvent {
|
|
24
|
+
readonly kind: string;
|
|
25
|
+
readonly [key: string]: unknown;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** Host allow-list. Absent callbacks omit the field. Structurally compatible with the AG-UI projection hooks. */
|
|
29
|
+
export interface AcpProjection {
|
|
30
|
+
toolArguments?(call: ToolCallContent): Awaitable<string | undefined>;
|
|
31
|
+
toolResult?(
|
|
32
|
+
result: ToolResult,
|
|
33
|
+
): Awaitable<string | { readonly type: "image"; readonly data: string; readonly mimeType: string } | undefined>;
|
|
34
|
+
toolLocations?(result: ToolResult): Awaitable<readonly { readonly path: string; readonly line?: number | null }[] | undefined>;
|
|
35
|
+
toolDiff?(result: ToolResult): Awaitable<{ readonly path: string; readonly oldText?: string; readonly newText: string } | undefined>;
|
|
36
|
+
lifecycle?(event: any): Awaitable<string | undefined>;
|
|
37
|
+
fileDiff?(event: any): Awaitable<{ readonly path: string; readonly oldText?: string; readonly newText: string } | undefined>;
|
|
38
|
+
coWork?(event: any): Awaitable<unknown>;
|
|
39
|
+
}
|
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Phase 10 Task 6 — rich prompt content.
|
|
3
|
+
*
|
|
4
|
+
* `projectAcpPrompt` turns a client `session/prompt` ContentBlock[] into a safe
|
|
5
|
+
* host-bound projection: baseline text + resource_link always allowed; image,
|
|
6
|
+
* audio, and embedded resources accepted only when the client advertised the
|
|
7
|
+
* matching `promptCapabilities` AND the host media policy still approves, then
|
|
8
|
+
* bounded by the frozen part/byte caps. ACP media arrives as base64 payloads
|
|
9
|
+
* (no URL fetching), so the only untrusted surface is MIME type and size —
|
|
10
|
+
* both validated here, fail-closed.
|
|
11
|
+
*/
|
|
12
|
+
import { AcpError } from "./errors.js";
|
|
13
|
+
|
|
14
|
+
/** One validated media part, forwarded to the host as base64 data. */
|
|
15
|
+
export interface AcpPromptMedia {
|
|
16
|
+
readonly type: "image" | "audio" | "file";
|
|
17
|
+
/** Validated MIME type (image/* or audio/* for image/audio; blob resources keep their own). */
|
|
18
|
+
readonly mediaType: string;
|
|
19
|
+
/** Base64-encoded payload. */
|
|
20
|
+
readonly data: string;
|
|
21
|
+
readonly name?: string;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** Safe host-bound projection of one ACP prompt. */
|
|
25
|
+
export interface AcpPromptResult {
|
|
26
|
+
/** Joined, redacted-free text (caller applies its own redactor) plus resource_link/embedded-text markers. */
|
|
27
|
+
readonly text: string;
|
|
28
|
+
readonly media?: readonly AcpPromptMedia[];
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export interface AcpPromptOptions {
|
|
32
|
+
readonly maxBlocks: number;
|
|
33
|
+
readonly maxTextBytes: number;
|
|
34
|
+
readonly maxMediaParts: number;
|
|
35
|
+
readonly maxMediaBytes: number;
|
|
36
|
+
readonly capabilities: { readonly image: boolean; readonly audio: boolean; readonly embeddedContext: boolean };
|
|
37
|
+
/** Re-checked at prompt time (the advertise-time gate alone can go stale). */
|
|
38
|
+
readonly policy?: { readonly media?: () => boolean | Promise<boolean>; readonly embedded?: () => boolean | Promise<boolean> };
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const BASE64 = /^[A-Za-z0-9+/]*={0,2}$/;
|
|
42
|
+
|
|
43
|
+
function estimateBase64DecodedBytes(value: string): number {
|
|
44
|
+
const padding = value.endsWith("==") ? 2 : value.endsWith("=") ? 1 : 0;
|
|
45
|
+
return Math.max(0, Math.floor((value.length * 3) / 4) - padding);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function mediaBlock(
|
|
49
|
+
kind: "image" | "audio" | "file",
|
|
50
|
+
mimeType: string,
|
|
51
|
+
data: string,
|
|
52
|
+
name: string | undefined,
|
|
53
|
+
count: number,
|
|
54
|
+
bytes: number,
|
|
55
|
+
options: AcpPromptOptions,
|
|
56
|
+
): AcpPromptMedia {
|
|
57
|
+
if (count >= options.maxMediaParts) throw new AcpError("ERR_PRISM_ACP_LIMIT", "ACP prompt media parts exceed the configured limit");
|
|
58
|
+
if (mimeType.length === 0 || (kind !== "file" && !mimeType.startsWith(`${kind}/`)) || mimeType.length > 256) {
|
|
59
|
+
throw new AcpError("ERR_PRISM_ACP_INPUT", `ACP prompt media has an invalid mimeType '${mimeType}'`);
|
|
60
|
+
}
|
|
61
|
+
if (data.length === 0 || data.length % 4 !== 0 || !BASE64.test(data)) {
|
|
62
|
+
throw new AcpError("ERR_PRISM_ACP_INPUT", "ACP prompt media data is not valid base64");
|
|
63
|
+
}
|
|
64
|
+
const decoded = estimateBase64DecodedBytes(data);
|
|
65
|
+
if (decoded <= 0 || bytes + decoded > options.maxMediaBytes) {
|
|
66
|
+
throw new AcpError("ERR_PRISM_ACP_LIMIT", "ACP prompt media exceeds the configured byte limit");
|
|
67
|
+
}
|
|
68
|
+
return { type: kind, mediaType: mimeType, data, ...(name ? { name } : {}) };
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Validates and projects one ACP prompt. Baseline: text + resource_link blocks.
|
|
73
|
+
* `image`/`audio` require the client advertisement AND the host media policy;
|
|
74
|
+
* `resource` requires `embeddedContext` (text resources join the text, blob
|
|
75
|
+
* resources become media parts). Throws AcpError on any violation — the prompt
|
|
76
|
+
* is rejected before any provider call.
|
|
77
|
+
*/
|
|
78
|
+
export async function projectAcpPrompt(prompt: readonly { readonly type: string }[], options: AcpPromptOptions): Promise<AcpPromptResult> {
|
|
79
|
+
if (prompt.length === 0 || prompt.length > options.maxBlocks) {
|
|
80
|
+
throw new AcpError("ERR_PRISM_ACP_LIMIT", "ACP prompt block count exceeds the configured limit");
|
|
81
|
+
}
|
|
82
|
+
let text = "";
|
|
83
|
+
let textBytes = 0;
|
|
84
|
+
let mediaBytes = 0;
|
|
85
|
+
const media: AcpPromptMedia[] = [];
|
|
86
|
+
for (const block of prompt) {
|
|
87
|
+
switch (block.type) {
|
|
88
|
+
case "text": {
|
|
89
|
+
const value = (block as { readonly text?: unknown }).text;
|
|
90
|
+
if (typeof value !== "string") throw new AcpError("ERR_PRISM_ACP_INPUT", "ACP prompt text block has no text");
|
|
91
|
+
textBytes += Buffer.byteLength(value, "utf8");
|
|
92
|
+
if (textBytes > options.maxTextBytes) throw new AcpError("ERR_PRISM_ACP_LIMIT", "ACP prompt text exceeds the configured limit");
|
|
93
|
+
text += value;
|
|
94
|
+
break;
|
|
95
|
+
}
|
|
96
|
+
case "resource_link": {
|
|
97
|
+
const link = block as { readonly name?: unknown; readonly uri?: unknown };
|
|
98
|
+
const name = typeof link.name === "string" ? link.name : "";
|
|
99
|
+
const uri = typeof link.uri === "string" ? link.uri : "";
|
|
100
|
+
const marker = `[resource_link: ${name}${uri ? ` (${uri})` : ""}]`;
|
|
101
|
+
textBytes += Buffer.byteLength(marker, "utf8");
|
|
102
|
+
if (textBytes > options.maxTextBytes) throw new AcpError("ERR_PRISM_ACP_LIMIT", "ACP prompt text exceeds the configured limit");
|
|
103
|
+
text += marker;
|
|
104
|
+
break;
|
|
105
|
+
}
|
|
106
|
+
case "image": {
|
|
107
|
+
if (!options.capabilities.image)
|
|
108
|
+
throw new AcpError("ERR_PRISM_ACP_CAPABILITY", "client sent image content without advertising promptCapabilities.image");
|
|
109
|
+
if (!(await options.policy?.media?.())) throw new AcpError("ERR_PRISM_ACP_POLICY", "host media policy denied ACP image content");
|
|
110
|
+
const blockValue = block as { readonly data?: unknown; readonly mimeType?: unknown; readonly uri?: unknown };
|
|
111
|
+
if (typeof blockValue.mimeType !== "string" || typeof blockValue.data !== "string") {
|
|
112
|
+
throw new AcpError("ERR_PRISM_ACP_INPUT", "ACP image block is missing data or mimeType");
|
|
113
|
+
}
|
|
114
|
+
media.push(mediaBlock("image", blockValue.mimeType, blockValue.data, undefined, media.length, mediaBytes, options));
|
|
115
|
+
mediaBytes += estimateBase64DecodedBytes(blockValue.data);
|
|
116
|
+
break;
|
|
117
|
+
}
|
|
118
|
+
case "audio": {
|
|
119
|
+
if (!options.capabilities.audio)
|
|
120
|
+
throw new AcpError("ERR_PRISM_ACP_CAPABILITY", "client sent audio content without advertising promptCapabilities.audio");
|
|
121
|
+
if (!(await options.policy?.media?.())) throw new AcpError("ERR_PRISM_ACP_POLICY", "host media policy denied ACP audio content");
|
|
122
|
+
const blockValue = block as { readonly data?: unknown; readonly mimeType?: unknown };
|
|
123
|
+
if (typeof blockValue.mimeType !== "string" || typeof blockValue.data !== "string") {
|
|
124
|
+
throw new AcpError("ERR_PRISM_ACP_INPUT", "ACP audio block is missing data or mimeType");
|
|
125
|
+
}
|
|
126
|
+
media.push(mediaBlock("audio", blockValue.mimeType, blockValue.data, undefined, media.length, mediaBytes, options));
|
|
127
|
+
mediaBytes += estimateBase64DecodedBytes(blockValue.data);
|
|
128
|
+
break;
|
|
129
|
+
}
|
|
130
|
+
case "resource": {
|
|
131
|
+
if (!options.capabilities.embeddedContext) {
|
|
132
|
+
throw new AcpError(
|
|
133
|
+
"ERR_PRISM_ACP_CAPABILITY",
|
|
134
|
+
"client sent embedded content without advertising promptCapabilities.embeddedContext",
|
|
135
|
+
);
|
|
136
|
+
}
|
|
137
|
+
if (!(await options.policy?.embedded?.()))
|
|
138
|
+
throw new AcpError("ERR_PRISM_ACP_POLICY", "host embedded policy denied ACP resource content");
|
|
139
|
+
const resource = (block as { readonly resource?: unknown }).resource;
|
|
140
|
+
if (!resource || typeof resource !== "object")
|
|
141
|
+
throw new AcpError("ERR_PRISM_ACP_INPUT", "ACP resource block has no resource payload");
|
|
142
|
+
const contents = resource as {
|
|
143
|
+
readonly text?: unknown;
|
|
144
|
+
readonly blob?: unknown;
|
|
145
|
+
readonly mimeType?: unknown;
|
|
146
|
+
readonly name?: unknown;
|
|
147
|
+
readonly uri?: unknown;
|
|
148
|
+
};
|
|
149
|
+
if (typeof contents.text === "string") {
|
|
150
|
+
const marker = contents.uri && typeof contents.uri === "string" ? `[resource: ${contents.uri}]\n${contents.text}` : contents.text;
|
|
151
|
+
textBytes += Buffer.byteLength(marker, "utf8");
|
|
152
|
+
if (textBytes > options.maxTextBytes) throw new AcpError("ERR_PRISM_ACP_LIMIT", "ACP prompt text exceeds the configured limit");
|
|
153
|
+
text += marker;
|
|
154
|
+
} else if (typeof contents.blob === "string") {
|
|
155
|
+
const mimeType =
|
|
156
|
+
typeof contents.mimeType === "string" && contents.mimeType.length > 0 ? contents.mimeType : "application/octet-stream";
|
|
157
|
+
const name = typeof contents.name === "string" ? contents.name : typeof contents.uri === "string" ? contents.uri : undefined;
|
|
158
|
+
media.push(mediaBlock("file", mimeType, contents.blob, name, media.length, mediaBytes, options));
|
|
159
|
+
mediaBytes += estimateBase64DecodedBytes(contents.blob);
|
|
160
|
+
} else {
|
|
161
|
+
throw new AcpError("ERR_PRISM_ACP_INPUT", "ACP embedded resource has neither text nor blob contents");
|
|
162
|
+
}
|
|
163
|
+
break;
|
|
164
|
+
}
|
|
165
|
+
default:
|
|
166
|
+
throw new AcpError("ERR_PRISM_ACP_INPUT", `ACP prompt contains unsupported block type '${String(block.type)}'`);
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
return { text, ...(media.length > 0 ? { media } : {}) };
|
|
170
|
+
}
|