@adaptcom/core
v0.4.4
Published
Framework for file-based AI agents.
Keywords
Readme
@adaptcom/core
The Adapt agent runtime: harnesses, tools, channels, connections, persistent
conversations, and deployment APIs. Requires Node 24 or later. The HTTP API
and its browser client live in @adaptcom/api.
pnpm add @adaptcom/core
pnpm add -D @adaptcom/cliimport { defineAgent, createHttpChannel } from "@adaptcom/core";Channel and connection subpaths are also available, for example
@adaptcom/core/channels/slack. @adaptcom/api/client is the browser-safe
API client.
Use @adaptcom/cli for the adapt command. Core does not depend on the CLI.
See the repository guides
and examples.
Compaction
Configure compaction on the harness, not on defineAgent. The provider receives
active ModelMessage[] history and returns a smaller array of the same type.
Preserve current intent, required context, and complete tool-call/result groups.
Return the input unchanged if no safe reduction is possible. Honor cancellation.
import { createAiSdkHarness, defineAgent, type CompactionProvider } from "@adaptcom/core";
import type { LanguageModel } from "ai";
export function agent(model: Exclude<LanguageModel, string>, compaction: CompactionProvider) {
return defineAgent({
harness: createAiSdkHarness({
model,
contextWindow: 128_000, // Use your model's actual capacity.
compaction, // Optional compaction.threshold defaults to 0.8.
}),
});
}CompactionProvider.threshold is optional, defaults to 0.8, and must be between
0 and 1 inclusive. The AI SDK harness checks before each request, including
after completed tools, and compacts when the request token estimate reaches
contextWindow * threshold. Zero attempts compaction before every request; one
waits until the estimated full context window. A no-op reduction proceeds without
looping. Compaction stays opt-in; a context window alone does not enable it.
The harness estimates tokens from serialized messages, instructions (including
skills), and tool definitions at roughly three UTF-8 bytes per token. Within a
turn, reported input usage corrects underestimates, with new history added to that
count. This is approximate, not a model-specific tokenizer. contextWindow stays
local to the harness and is required when configuring compaction; it is not passed
through project or runtime configuration.
A recognized context-overflow rejection still allows one compaction-and-retry of that request, without rerunning completed tools or consuming another step. No reduction rethrows the original overflow. Other errors are not retried. Invalid/failed/aborted replacements leave the original history intact.
createGitHubAiSdkHarness accepts the same settings and preserves them when tools
are injected. For an environment-selected model, use
createEnvironmentHarness(process.env, { compaction, contextWindow: 128_000 }).
A project harness factory can resolve provider credentials at startup. Codex
harnesses own native conversation state and do not implement this compaction loop.
A smaller serialized history is not proof it will fit the model's limit. A summarizing provider must also be able to process the history it receives, using its own chunking or a larger-context model when necessary.
Successful reductions append a session.compacted event through the ordinary
store commit. On replay, the harness replaces its messages with that event's
history. Revisions and retry receipts keep their existing append-only semantics;
runtime state, resource handles, and other sessions are unchanged. Compaction
rejects unresolved tool calls and clears completed tool state.
The Next.js chat example replaces earlier messages with a
"Conversation compacted" marker and the replacement's public text. System
messages, reasoning, tool payloads, and provider metadata are not projected.
Custom chat clients should reset their transcript on a compaction event.
This compacts model context, not disk usage. Original events and receipts
remain in the journal for idempotent retries, including sensitive original
history; there is no automatic retention policy. No archives or special store
methods are needed. Custom stores must support the session.compacted event
and its tool-state invariant, as exercised by the shared store contract.
Custom harnesses expose their provider as AgentHarness.compaction, reset history
on that event when reading context.events(), and invoke context.compaction.compact()
only at a safe boundary. The runtime validates and checkpoints before returning.
