@arnilo/prism-hosts 0.0.0-stage → 0.13.0
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 +6167 -0
- package/host-builder/data/errors.json +546 -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 +252 -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 +317 -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 +64 -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,94 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Advisor Demo (plan 191 Task 8).
|
|
3
|
+
*
|
|
4
|
+
* `@arnilo/prism-extensions/advisor` gives an agent a second opinion. The parent model calls `advise` once and gets
|
|
5
|
+
* one model call's worth of advice, then `triggers.beforeFinish` reviews the answer at the natural end of
|
|
6
|
+
* the run: the first review says `revise`, so the run continues with that advice as a steer; the second
|
|
7
|
+
* says `proceed`. Every consult is an `advisor.consult` package event.
|
|
8
|
+
*
|
|
9
|
+
* The advisor is advisory: it has no tools, cannot act, and a failure never blocks a run.
|
|
10
|
+
*
|
|
11
|
+
* Run: bun examples/advisor.ts
|
|
12
|
+
*/
|
|
13
|
+
import {
|
|
14
|
+
type AgentEvent,
|
|
15
|
+
createAgent,
|
|
16
|
+
createMemorySessionStore,
|
|
17
|
+
type ModelProvider,
|
|
18
|
+
providerDone,
|
|
19
|
+
providerTextDelta,
|
|
20
|
+
providerToolCall,
|
|
21
|
+
toolCallContent,
|
|
22
|
+
} from "@arnilo/prism";
|
|
23
|
+
import { providerPackage } from "@arnilo/prism/provider-kit";
|
|
24
|
+
import advisor from "@arnilo/prism-extensions/advisor";
|
|
25
|
+
|
|
26
|
+
function mockProvider(): ModelProvider {
|
|
27
|
+
let parentCalls = 0;
|
|
28
|
+
let finishReviews = 0;
|
|
29
|
+
return {
|
|
30
|
+
id: "mock",
|
|
31
|
+
async *generate(request) {
|
|
32
|
+
const prompts = JSON.stringify(request.messages);
|
|
33
|
+
if ((request.tools ?? []).some((tool) => tool.name === "advise")) {
|
|
34
|
+
parentCalls += 1;
|
|
35
|
+
if (parentCalls === 1) {
|
|
36
|
+
yield providerToolCall(toolCallContent("call_advice", "advise", { question: "Patch the parser or the caller?" }));
|
|
37
|
+
} else {
|
|
38
|
+
yield providerTextDelta("Patched the caller and finished.");
|
|
39
|
+
}
|
|
40
|
+
yield providerDone({ inputTokens: 4, outputTokens: 2, totalTokens: 6 });
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
if (prompts.includes("about to finish")) {
|
|
44
|
+
finishReviews += 1;
|
|
45
|
+
yield providerTextDelta(
|
|
46
|
+
finishReviews === 1
|
|
47
|
+
? '{"verdict":"revise","advice":"Add a regression test for the parse edge case, then finish."}'
|
|
48
|
+
: '{"verdict":"proceed","advice":""}',
|
|
49
|
+
);
|
|
50
|
+
} else if (prompts.includes("about to call the tool")) {
|
|
51
|
+
yield providerTextDelta('{"verdict":"proceed","advice":""}');
|
|
52
|
+
} else {
|
|
53
|
+
yield providerTextDelta("Patch the caller: the parser accepts the input the spec allows, and the caller is what broke.");
|
|
54
|
+
}
|
|
55
|
+
yield providerDone({ inputTokens: 4, outputTokens: 2, totalTokens: 6 });
|
|
56
|
+
},
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export async function demo() {
|
|
61
|
+
const consults: unknown[] = [];
|
|
62
|
+
let advice = "";
|
|
63
|
+
const agent = await createAgent({
|
|
64
|
+
id: "advisor-demo",
|
|
65
|
+
model: { provider: "mock", model: "demo" },
|
|
66
|
+
slots: { store: createMemorySessionStore() },
|
|
67
|
+
hooks: (hooks) => {
|
|
68
|
+
hooks.on("tool.completed", (event) => {
|
|
69
|
+
if (event.name !== "advise") return;
|
|
70
|
+
const block = event.result.content?.[0];
|
|
71
|
+
advice = block?.type === "text" ? (block.text.split("\n")[0] ?? "") : "";
|
|
72
|
+
});
|
|
73
|
+
hooks.on("package.event", (event: Extract<AgentEvent, { type: "package.event" }>) => {
|
|
74
|
+
if (event.name === "advisor.consult") consults.push(event.data);
|
|
75
|
+
});
|
|
76
|
+
},
|
|
77
|
+
packages: [
|
|
78
|
+
providerPackage("mock-provider", [mockProvider()]),
|
|
79
|
+
advisor({
|
|
80
|
+
model: { provider: "mock", model: "demo" },
|
|
81
|
+
context: { lastTurns: 8 },
|
|
82
|
+
triggers: { beforeFinish: true },
|
|
83
|
+
maxConsultsPerRun: 4,
|
|
84
|
+
}),
|
|
85
|
+
],
|
|
86
|
+
});
|
|
87
|
+
const session = await agent.session();
|
|
88
|
+
const result = await session.run("The parser rejects valid input; find the right fix.");
|
|
89
|
+
await session.close();
|
|
90
|
+
await agent.dispose();
|
|
91
|
+
return { status: result.status, advice, consults };
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
if (import.meta.main) console.log(JSON.stringify(await demo(), null, 2));
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { createAgent, providerDone, providerTextDelta, toolCallContent } from "@arnilo/prism";
|
|
2
|
+
import { providerPackage } from "@arnilo/prism/provider-kit";
|
|
3
|
+
import { createAgUiHandler } from "@arnilo/prism-hosts/ag-ui";
|
|
4
|
+
|
|
5
|
+
const catalogId = "https://a2ui.org/specification/v0_9/basic_catalog.json";
|
|
6
|
+
|
|
7
|
+
/** Compile-checked A2UI painting demo for `@arnilo/prism-hosts/ag-ui`. */
|
|
8
|
+
export async function demo() {
|
|
9
|
+
let turn = 0;
|
|
10
|
+
const agent = await createAgent({
|
|
11
|
+
model: { provider: "mock", model: "mock" },
|
|
12
|
+
packages: [
|
|
13
|
+
providerPackage("test-provider", [
|
|
14
|
+
{
|
|
15
|
+
id: "mock",
|
|
16
|
+
async *generate() {
|
|
17
|
+
if (++turn === 1) {
|
|
18
|
+
yield { type: "tool_call" as const, call: toolCallContent("paint-1", "paint", {}) };
|
|
19
|
+
} else {
|
|
20
|
+
yield providerTextDelta("done");
|
|
21
|
+
}
|
|
22
|
+
yield providerDone();
|
|
23
|
+
},
|
|
24
|
+
},
|
|
25
|
+
]),
|
|
26
|
+
],
|
|
27
|
+
tools: {
|
|
28
|
+
add: [
|
|
29
|
+
{
|
|
30
|
+
name: "paint",
|
|
31
|
+
parameters: { type: "object" },
|
|
32
|
+
execute: () => ({
|
|
33
|
+
toolCallId: "paint-1",
|
|
34
|
+
name: "paint",
|
|
35
|
+
value: {
|
|
36
|
+
a2ui_operations: [
|
|
37
|
+
{ version: "v0.9", createSurface: { surfaceId: "demo" } },
|
|
38
|
+
{
|
|
39
|
+
version: "v0.9",
|
|
40
|
+
updateComponents: {
|
|
41
|
+
surfaceId: "demo",
|
|
42
|
+
components: [{ id: "root", component: "Text", text: "Hello A2UI" }],
|
|
43
|
+
},
|
|
44
|
+
},
|
|
45
|
+
],
|
|
46
|
+
},
|
|
47
|
+
}),
|
|
48
|
+
},
|
|
49
|
+
],
|
|
50
|
+
},
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
const handle = createAgUiHandler(agent, {
|
|
54
|
+
authorize: () => ({ ownership: { userId: "demo" } }),
|
|
55
|
+
a2ui: { catalogId, mode: "fixed-schema" },
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
const response = await handle(
|
|
59
|
+
new Request("https://example.test/ag-ui", {
|
|
60
|
+
method: "POST",
|
|
61
|
+
headers: { "content-type": "application/json" },
|
|
62
|
+
body: JSON.stringify({
|
|
63
|
+
threadId: "thread-1",
|
|
64
|
+
runId: "run-1",
|
|
65
|
+
state: {},
|
|
66
|
+
messages: [{ id: "message-1", role: "user", content: "paint" }],
|
|
67
|
+
tools: [],
|
|
68
|
+
context: [],
|
|
69
|
+
forwardedProps: {},
|
|
70
|
+
}),
|
|
71
|
+
}),
|
|
72
|
+
);
|
|
73
|
+
const text = await response.text();
|
|
74
|
+
return {
|
|
75
|
+
status: response.status,
|
|
76
|
+
painted: text.includes("ACTIVITY_SNAPSHOT") && text.includes("a2ui-surface"),
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
if (import.meta.url === `file://${process.argv[1]}`) console.log(JSON.stringify(await demo()));
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { createAgUiMcpAppHandler, createAgUiMcpAppSandbox } from "@arnilo/prism-hosts/ag-ui";
|
|
2
|
+
import type { McpAppsBridge } from "@arnilo/prism-extensions/mcp";
|
|
3
|
+
|
|
4
|
+
const apps: McpAppsBridge = {
|
|
5
|
+
serverId: "demo",
|
|
6
|
+
negotiated: true,
|
|
7
|
+
tools: [
|
|
8
|
+
{ name: "refresh", prismName: "mcp:demo:refresh", inputSchema: { type: "object" }, resourceUri: "ui://demo/card", visibility: ["app"] },
|
|
9
|
+
],
|
|
10
|
+
listResources: async () => [{ uri: "ui://demo/card", name: "demo", mimeType: "text/html;profile=mcp-app" }],
|
|
11
|
+
readResource: async () => ({
|
|
12
|
+
uri: "ui://demo/card",
|
|
13
|
+
name: "demo",
|
|
14
|
+
mimeType: "text/html;profile=mcp-app",
|
|
15
|
+
html: "<!doctype html><html><body>demo</body></html>",
|
|
16
|
+
ui: { csp: { connectDomains: ["https://api.example.test"] } },
|
|
17
|
+
}),
|
|
18
|
+
callTool: async (_name, _args, context) => ({ toolCallId: context.toolCallId, name: "mcp:demo:refresh", value: { refreshed: true } }),
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
export async function demo() {
|
|
22
|
+
const proxy = createAgUiMcpAppHandler({
|
|
23
|
+
apps,
|
|
24
|
+
authorize: () => ({ ownership: { tenantId: "demo", userId: "demo" } }),
|
|
25
|
+
context: ({ messageId }) => ({ sessionId: "demo-session", runId: "demo-run", toolCallId: `app:${messageId}` }),
|
|
26
|
+
approveToolCall: () => true,
|
|
27
|
+
allowedOrigins: ["https://host.example.test"],
|
|
28
|
+
});
|
|
29
|
+
const response = await proxy(
|
|
30
|
+
new Request("https://host.example.test/mcp-app", {
|
|
31
|
+
method: "POST",
|
|
32
|
+
headers: { "content-type": "application/json", origin: "https://host.example.test" },
|
|
33
|
+
body: JSON.stringify({
|
|
34
|
+
serverId: "demo",
|
|
35
|
+
message: { jsonrpc: "2.0", id: 1, method: "resources/read", params: { uri: "ui://demo/card" } },
|
|
36
|
+
}),
|
|
37
|
+
}),
|
|
38
|
+
);
|
|
39
|
+
const resource = (await response.json()) as { result: { contents: Array<{ text: string }> } };
|
|
40
|
+
const sandbox = createAgUiMcpAppSandbox(await apps.readResource("ui://demo/card"));
|
|
41
|
+
return { status: response.status, html: resource.result.contents[0]?.text.includes("demo"), sandbox: sandbox.sandbox };
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
if (import.meta.url === `file://${process.argv[1]}`) console.log(JSON.stringify(await demo()));
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import {
|
|
2
|
+
askEveryTool,
|
|
3
|
+
createAgent,
|
|
4
|
+
createMemorySessionStore,
|
|
5
|
+
type JsonObject,
|
|
6
|
+
providerDone,
|
|
7
|
+
providerTextDelta,
|
|
8
|
+
toolCallContent,
|
|
9
|
+
} from "@arnilo/prism";
|
|
10
|
+
import { providerPackage } from "@arnilo/prism/provider-kit";
|
|
11
|
+
import { createAgUiHandler } from "@arnilo/prism-hosts/ag-ui";
|
|
12
|
+
|
|
13
|
+
export async function demo() {
|
|
14
|
+
const executed: string[] = [];
|
|
15
|
+
let turn = 0;
|
|
16
|
+
|
|
17
|
+
const agent = await createAgent({
|
|
18
|
+
id: "approvals-agent",
|
|
19
|
+
model: { provider: "mock", model: "mock" },
|
|
20
|
+
slots: { store: createMemorySessionStore() },
|
|
21
|
+
packages: [
|
|
22
|
+
providerPackage("test-provider", [
|
|
23
|
+
{
|
|
24
|
+
id: "mock",
|
|
25
|
+
async *generate() {
|
|
26
|
+
if (++turn === 1) {
|
|
27
|
+
yield {
|
|
28
|
+
type: "tool_call" as const,
|
|
29
|
+
call: toolCallContent("call-1", "publish_draft", { title: "Draft Post", content: "Unchecked draft" }),
|
|
30
|
+
};
|
|
31
|
+
yield providerDone();
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
yield providerTextDelta("Publication complete.");
|
|
35
|
+
yield providerDone();
|
|
36
|
+
},
|
|
37
|
+
},
|
|
38
|
+
]),
|
|
39
|
+
],
|
|
40
|
+
hooks: askEveryTool(),
|
|
41
|
+
tools: {
|
|
42
|
+
add: [
|
|
43
|
+
{
|
|
44
|
+
name: "publish_draft",
|
|
45
|
+
parameters: {},
|
|
46
|
+
execute: (args: JsonObject, context: { toolCallId: string }) => {
|
|
47
|
+
executed.push(`${context.toolCallId}:${JSON.stringify(args)}`);
|
|
48
|
+
return { toolCallId: context.toolCallId, name: "publish_draft", value: "published" };
|
|
49
|
+
},
|
|
50
|
+
},
|
|
51
|
+
],
|
|
52
|
+
},
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
const handle = createAgUiHandler(agent, {
|
|
56
|
+
authorize: () => ({ ownership: { userId: "demo", tenantId: "demo-tenant" } }),
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
const parse = async (response: Response) =>
|
|
60
|
+
(await response.text())
|
|
61
|
+
.trim()
|
|
62
|
+
.split("\n\n")
|
|
63
|
+
.filter(Boolean)
|
|
64
|
+
.map((line) => JSON.parse(line.replace(/^data:\s*/, "")));
|
|
65
|
+
|
|
66
|
+
// 1. Initial run: the agent asks to call a tool and the run suspends as an AG-UI interrupt.
|
|
67
|
+
const startResponse = await handle(
|
|
68
|
+
new Request("https://example.test/ag-ui", {
|
|
69
|
+
method: "POST",
|
|
70
|
+
headers: { "content-type": "application/json" },
|
|
71
|
+
body: JSON.stringify({
|
|
72
|
+
threadId: "ag-ui-thread",
|
|
73
|
+
runId: "ag-ui-run-1",
|
|
74
|
+
state: {},
|
|
75
|
+
messages: [{ id: "m-1", role: "user", content: "Publish the blog post" }],
|
|
76
|
+
tools: [],
|
|
77
|
+
context: [],
|
|
78
|
+
forwardedProps: {},
|
|
79
|
+
}),
|
|
80
|
+
}),
|
|
81
|
+
);
|
|
82
|
+
|
|
83
|
+
const startEvents = await parse(startResponse);
|
|
84
|
+
const finish = startEvents.at(-1);
|
|
85
|
+
const interrupt = finish?.outcome?.interrupts?.[0];
|
|
86
|
+
|
|
87
|
+
// 2. Resume with one decision: the reviewer approves the pending tool call.
|
|
88
|
+
const resumeResponse = await handle(
|
|
89
|
+
new Request("https://example.test/ag-ui", {
|
|
90
|
+
method: "POST",
|
|
91
|
+
headers: { "content-type": "application/json" },
|
|
92
|
+
body: JSON.stringify({
|
|
93
|
+
threadId: "ag-ui-thread",
|
|
94
|
+
runId: "ag-ui-run-2",
|
|
95
|
+
state: {},
|
|
96
|
+
messages: [],
|
|
97
|
+
tools: [],
|
|
98
|
+
context: [],
|
|
99
|
+
forwardedProps: {},
|
|
100
|
+
resume: [{ interruptId: interrupt?.id, status: "resolved", payload: { outcome: "allow_once" } }],
|
|
101
|
+
}),
|
|
102
|
+
}),
|
|
103
|
+
);
|
|
104
|
+
|
|
105
|
+
const resumeEvents = await parse(resumeResponse);
|
|
106
|
+
|
|
107
|
+
return {
|
|
108
|
+
interruptAdvertised: handle.capabilities.humanInTheLoop?.interrupts ?? false,
|
|
109
|
+
startStatus: startResponse.status,
|
|
110
|
+
startEventsCount: startEvents.length,
|
|
111
|
+
interruptType: finish?.outcome?.type,
|
|
112
|
+
resumeStatus: resumeResponse.status,
|
|
113
|
+
resumeEventsCount: resumeEvents.length,
|
|
114
|
+
executedCalls: executed,
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
if (import.meta.url === `file://${process.argv[1]}`) console.log(JSON.stringify(await demo()));
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { askEveryTool, createAgent, createMemorySessionStore, providerDone, providerTextDelta, toolCallContent } from "@arnilo/prism";
|
|
2
|
+
import { providerPackage } from "@arnilo/prism/provider-kit";
|
|
3
|
+
|
|
4
|
+
// Network-free durable approval: no tool side effect occurs before resume approval. The suspension
|
|
5
|
+
// lives in the store slot's `suspended_run` entry (plan 168 Task 5), so `session.resume` is the only
|
|
6
|
+
// durable path a host needs.
|
|
7
|
+
export async function demo() {
|
|
8
|
+
let turn = 0;
|
|
9
|
+
let writes = 0;
|
|
10
|
+
const agent = await createAgent({
|
|
11
|
+
id: "approval-demo",
|
|
12
|
+
model: { provider: "mock", model: "demo" },
|
|
13
|
+
slots: { store: createMemorySessionStore() },
|
|
14
|
+
hooks: askEveryTool(),
|
|
15
|
+
packages: [
|
|
16
|
+
providerPackage("test-provider", [
|
|
17
|
+
{
|
|
18
|
+
id: "mock",
|
|
19
|
+
async *generate() {
|
|
20
|
+
if (++turn === 1) yield { type: "tool_call" as const, call: toolCallContent("write-1", "write", { value: "approved" }) };
|
|
21
|
+
else yield providerTextDelta("finished");
|
|
22
|
+
yield providerDone();
|
|
23
|
+
},
|
|
24
|
+
},
|
|
25
|
+
]),
|
|
26
|
+
],
|
|
27
|
+
tools: {
|
|
28
|
+
add: [{ name: "write", parameters: { type: "object" }, execute: () => ({ toolCallId: "write-1", name: "write", value: ++writes }) }],
|
|
29
|
+
},
|
|
30
|
+
});
|
|
31
|
+
const session = await agent.session({ id: "approval-session", create: true });
|
|
32
|
+
const suspended = await session.run("write");
|
|
33
|
+
if (suspended.status !== "suspended" || writes !== 0) throw new Error("expected pre-tool suspension");
|
|
34
|
+
let last: string | undefined;
|
|
35
|
+
for await (const event of session.resume(
|
|
36
|
+
suspended.pending!.map((pending) => ({ approvalId: pending.approvalId, outcome: "allow_once" as const })),
|
|
37
|
+
)) {
|
|
38
|
+
last = event.type;
|
|
39
|
+
}
|
|
40
|
+
return { status: last === "run.completed" ? "completed" : last, writes };
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
if (import.meta.url === `file://${process.argv[1]}`) console.log(JSON.stringify(await demo()));
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { createEnvRefCredentialResolver, type CredentialResolver, type Secret } from "@arnilo/prism";
|
|
2
|
+
|
|
3
|
+
// Compose an explicit API-key resolver order through the credentials slot. Prism
|
|
4
|
+
// never reads process.env on its own; the host supplies the env object and a
|
|
5
|
+
// small name-keyed store, then chains them in the order it wants.
|
|
6
|
+
export function demo() {
|
|
7
|
+
const env = { OPENAI_API_KEY: "fake-openai-key", OPENROUTER_API_KEY: "fake-openrouter-key" };
|
|
8
|
+
const records = new Map<string, Secret>([["keyring:openai", { type: "api_key", value: "fake-openai-key" }]]);
|
|
9
|
+
const store: CredentialResolver = {
|
|
10
|
+
resolve: (ref) => records.get(ref),
|
|
11
|
+
store: (ref, secret) => void records.set(ref, secret),
|
|
12
|
+
delete: (ref) => void records.delete(ref),
|
|
13
|
+
};
|
|
14
|
+
const envResolver = createEnvRefCredentialResolver(env);
|
|
15
|
+
// Order is host-controlled: try the explicit store first, then env.
|
|
16
|
+
const chained: CredentialResolver = {
|
|
17
|
+
async resolve(ref, ctx) {
|
|
18
|
+
return (await store.resolve(ref, ctx)) ?? envResolver.resolve(ref, ctx);
|
|
19
|
+
},
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
return { chained };
|
|
23
|
+
}
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import { createAgent, createMemorySessionStore, type Decision } from "@arnilo/prism";
|
|
2
|
+
import {
|
|
3
|
+
mockProvider,
|
|
4
|
+
createScriptedHostUi,
|
|
5
|
+
providerDone,
|
|
6
|
+
providerTextDelta,
|
|
7
|
+
providerToolCall,
|
|
8
|
+
toolCallContent,
|
|
9
|
+
} from "@arnilo/prism/testing";
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* A tool gated by a host `tool.call` hook returning `{ ask }` (roadmap §2.4), resolved both ways:
|
|
13
|
+
* - with a `HostUi` the kernel sends a `confirm` request inline and the run goes on;
|
|
14
|
+
* - with no UI the run suspends (`status: "suspended"`, `pending`), the suspension is written through the
|
|
15
|
+
* `store` slot, and `agent.session({ id })` plus `session.resume(decisions)` finishes it, here from a
|
|
16
|
+
* brand-new agent over the same store, as a new process would.
|
|
17
|
+
* Imports only `@arnilo/prism` and `@arnilo/prism/testing`; no credential. Network-free.
|
|
18
|
+
* Run: npm run build:core && node examples/approval.ts
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
const MODEL = { provider: "mock", model: "demo" } as const;
|
|
22
|
+
|
|
23
|
+
function build(options: {
|
|
24
|
+
readonly executed: string[];
|
|
25
|
+
readonly ui?: ReturnType<typeof createScriptedHostUi>["ui"];
|
|
26
|
+
readonly store: ReturnType<typeof createMemorySessionStore>;
|
|
27
|
+
readonly finalOnly?: boolean;
|
|
28
|
+
}) {
|
|
29
|
+
return createAgent({
|
|
30
|
+
id: "approval-demo",
|
|
31
|
+
model: MODEL,
|
|
32
|
+
slots: { store: options.store },
|
|
33
|
+
...(options.ui ? { host: { ui: options.ui } } : {}),
|
|
34
|
+
// The host gate: every `delete_file` call needs a person's yes.
|
|
35
|
+
hooks: (hooks) => hooks.on("tool.call", (call) => (call.name === "delete_file" ? { ask: "Delete a file?" } : undefined)),
|
|
36
|
+
packages: [
|
|
37
|
+
mockProvider(
|
|
38
|
+
options.finalOnly
|
|
39
|
+
? [[providerTextDelta("Deleted."), providerDone()]]
|
|
40
|
+
: [
|
|
41
|
+
[providerToolCall(toolCallContent("call-1", "delete_file", { path: "tmp/scratch.txt" })), providerDone()],
|
|
42
|
+
[providerTextDelta("Deleted."), providerDone()],
|
|
43
|
+
],
|
|
44
|
+
),
|
|
45
|
+
],
|
|
46
|
+
tools: {
|
|
47
|
+
add: [
|
|
48
|
+
{
|
|
49
|
+
name: "delete_file",
|
|
50
|
+
description: "Deletes a file",
|
|
51
|
+
parameters: { type: "object", properties: { path: { type: "string" } } },
|
|
52
|
+
effects: ["write"],
|
|
53
|
+
execute: (args, context) => {
|
|
54
|
+
options.executed.push(String(args.path));
|
|
55
|
+
return { toolCallId: context.toolCallId, name: "delete_file", value: "deleted" };
|
|
56
|
+
},
|
|
57
|
+
},
|
|
58
|
+
],
|
|
59
|
+
},
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export async function demo(): Promise<Record<string, unknown>> {
|
|
64
|
+
// 1. A UI answers inline: the run never suspends.
|
|
65
|
+
const inlineRuns: string[] = [];
|
|
66
|
+
const scripted = createScriptedHostUi({ confirm: true });
|
|
67
|
+
const inline = await build({ executed: inlineRuns, ui: scripted.ui, store: createMemorySessionStore() });
|
|
68
|
+
const inlineResult = await (await inline.session()).run("Clean up tmp/scratch.txt");
|
|
69
|
+
|
|
70
|
+
// 2. No UI: the run suspends, and a new agent over the same store resumes it.
|
|
71
|
+
const store = createMemorySessionStore();
|
|
72
|
+
const executed: string[] = [];
|
|
73
|
+
const first = await build({ executed, store });
|
|
74
|
+
const session = await first.session();
|
|
75
|
+
const suspended = await session.run("Clean up tmp/scratch.txt");
|
|
76
|
+
const pending = suspended.pending ?? [];
|
|
77
|
+
const executedWhileSuspended = executed.length;
|
|
78
|
+
|
|
79
|
+
const second = await build({ executed, store, finalOnly: true });
|
|
80
|
+
const reopened = await second.session({ id: session.id });
|
|
81
|
+
const decisions: Decision[] = pending.map((entry) => ({ approvalId: entry.approvalId, outcome: "allow_once" }));
|
|
82
|
+
const eventTypes: string[] = [];
|
|
83
|
+
for await (const event of reopened.resume(decisions)) eventTypes.push(event.type);
|
|
84
|
+
|
|
85
|
+
return {
|
|
86
|
+
inline: { status: inlineResult.status, executed: inlineRuns, confirmRequests: scripted.requestsOf("confirm").length },
|
|
87
|
+
suspended: { status: suspended.status, pending: pending.map((entry) => entry.scope.toolName), executedWhileSuspended },
|
|
88
|
+
resumed: { events: eventTypes, executed, final: (await reopened.messages()).at(-1)?.role },
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
if (import.meta.url === `file://${process.argv[1]}`) console.log(JSON.stringify(await demo()));
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Attention compiler, no credentials and no network: one pass stays byte-identical because the
|
|
3
|
+
* request is under the ratio, the next one is over it, so the oldest tool bodies become
|
|
4
|
+
* deterministic stubs and the caller gets one report.
|
|
5
|
+
*
|
|
6
|
+
* The compiler lives in `@arnilo/prism-extensions/context-engineering` (plan 158): a session gets it by
|
|
7
|
+
* installing the package — `contextEngineering({ attention: { maxInputTokens: 2_000 } })` runs
|
|
8
|
+
* this same pass inside a `context.build` handler and emits `attention.compiled` as a package
|
|
9
|
+
* event. This file calls the library directly so the pass is visible on its own.
|
|
10
|
+
*
|
|
11
|
+
* Run: npm run build:core && node examples/attention-compiler.ts
|
|
12
|
+
*/
|
|
13
|
+
import type { Message, ModelConfig } from "@arnilo/prism";
|
|
14
|
+
import { type AttentionReport, compileAttention, createAttentionCompiler } from "@arnilo/prism-extensions/context-engineering";
|
|
15
|
+
|
|
16
|
+
/** Cap 2,000 tokens, default reserve 1,024: the ratio is compared against 976 input tokens. */
|
|
17
|
+
const model: ModelConfig = { provider: "mock", model: "demo" };
|
|
18
|
+
|
|
19
|
+
const big = (label: string) => `${label} ${"x".repeat(4_000)}`;
|
|
20
|
+
|
|
21
|
+
/** Short enough to stay under the ratio: ~40 tokens against a 1,500-token trigger. */
|
|
22
|
+
const smallHistory: Message[] = [
|
|
23
|
+
{ role: "tool", content: [{ type: "tool_result", toolCallId: "call_old", name: "lookup", result: "row: ready" }] },
|
|
24
|
+
];
|
|
25
|
+
|
|
26
|
+
/** The same shape with a payload aged into the thousands of tokens. */
|
|
27
|
+
const bigHistory: Message[] = [
|
|
28
|
+
{
|
|
29
|
+
role: "assistant",
|
|
30
|
+
content: [
|
|
31
|
+
{ type: "thinking", text: big("reasoning") },
|
|
32
|
+
{ type: "text", text: "Looking it up." },
|
|
33
|
+
],
|
|
34
|
+
},
|
|
35
|
+
{ role: "tool", content: [{ type: "tool_result", toolCallId: "call_old", name: "lookup", result: big("row") }] },
|
|
36
|
+
];
|
|
37
|
+
|
|
38
|
+
const turnInput = (text: string): Message => ({ role: "user", content: [{ type: "text", text }] });
|
|
39
|
+
|
|
40
|
+
/** One validated handle: the package builds it from the same options a session would pass. */
|
|
41
|
+
const compiler = createAttentionCompiler({ maxInputTokens: 2_000, triggerRatio: 0.75, compactRatio: 0.9, keepLast: 0 }, { model });
|
|
42
|
+
|
|
43
|
+
/** Stub shape: `Tool result lookup [call_old]: omitted 4006 bytes (sha256 …)`. */
|
|
44
|
+
const stubOf = (messages: readonly Message[], toolCallId: string) => {
|
|
45
|
+
for (const message of messages) {
|
|
46
|
+
for (const block of message.content) {
|
|
47
|
+
if (block.type === "tool_result" && block.toolCallId === toolCallId) return String(block.result);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
return undefined;
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
// Turn 2: same model, same history — the pass mutates nothing, so the bytes a host sends are those
|
|
54
|
+
// of a compiler-off run and no report is raised.
|
|
55
|
+
const underRatio = await compileAttention({ compiler, messages: [...smallHistory, turnInput("continue")], turn: 2 });
|
|
56
|
+
|
|
57
|
+
// Turn 3: over the ratio, so the aged row is stubbed for this pass only.
|
|
58
|
+
const overRatio = await compileAttention({ compiler, messages: [...bigHistory, turnInput("continue")], turn: 3 });
|
|
59
|
+
|
|
60
|
+
const report: AttentionReport = overRatio.report;
|
|
61
|
+
|
|
62
|
+
console.log(
|
|
63
|
+
JSON.stringify(
|
|
64
|
+
{
|
|
65
|
+
underRatio: {
|
|
66
|
+
identicalToCompilerOff: JSON.stringify(underRatio.messages) === JSON.stringify([...smallHistory, turnInput("continue")]),
|
|
67
|
+
mutated: underRatio.mutated,
|
|
68
|
+
stub: stubOf(underRatio.messages, "call_old"),
|
|
69
|
+
},
|
|
70
|
+
overRatio: {
|
|
71
|
+
mutated: overRatio.mutated,
|
|
72
|
+
stub: stubOf(overRatio.messages, "call_old"),
|
|
73
|
+
report,
|
|
74
|
+
// Projection only: the row the caller handed in still holds every byte.
|
|
75
|
+
sourceHistoryUnchanged: JSON.stringify(bigHistory[1]).includes("x".repeat(100)),
|
|
76
|
+
},
|
|
77
|
+
},
|
|
78
|
+
null,
|
|
79
|
+
2,
|
|
80
|
+
),
|
|
81
|
+
);
|