@asterism-app/builtin-api 1.0.1 → 1.1.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/docs/agents.md +6 -0
- package/examples/package.json +1 -1
- package/package.json +1 -1
- package/src/context-transform.ts +74 -0
- package/src/runtime.ts +2 -0
package/docs/agents.md
CHANGED
|
@@ -78,6 +78,12 @@ Request preparation changes the skills and instruction blocks presented to an Ag
|
|
|
78
78
|
|
|
79
79
|
Keep preparation deterministic within the captured request environment. Changes to configuration or catalogs belong to later requests rather than altering the meaning of a request already being assembled.
|
|
80
80
|
|
|
81
|
+
## Transform model context without rewriting history
|
|
82
|
+
|
|
83
|
+
Agent context transforms compose in registration order after the built-in timeline projection. Each synchronous transform gets the preceding transform's standard context units. It can preserve, replace, add, or remove units; tool calls and results must stay paired within a unit. An invalid transform fails the request instead of silently restoring another projection. Keep transforms deterministic for a captured request, especially when reusing a provider's cached prefix.
|
|
84
|
+
|
|
85
|
+
Provenance accompanies the model items but is not sent to the model. Preserve a part's original identity only when forwarding that part. Synthetic or replaced parts do not claim its provenance. Treat a recall ticket as an address, not authority: resolve it on the calling Agent's request path and return the original content as a new tool result. Projection changes neither the saved timeline nor user-facing history.
|
|
86
|
+
|
|
81
87
|
## Extend presentation without owning execution
|
|
82
88
|
|
|
83
89
|
Use presentation extensions for the integration's controls and business state while preserving builtin's conversation and execution ownership. A view closing does not complete or cancel waiting backend work. Reopening should read the current state rather than depend on an earlier component instance.
|
package/examples/package.json
CHANGED
package/package.json
CHANGED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import type { ModelItem, SessionDirectory } from "@asterism-app/sdk/contracts";
|
|
2
|
+
import { defineModule, PluginApiError, type PluginContext } from "@asterism-app/sdk/runtime";
|
|
3
|
+
|
|
4
|
+
/** Provenance accompanies the projection; it is not model content or a writable timeline address. */
|
|
5
|
+
export interface AgentContextItem {
|
|
6
|
+
readonly item: ModelItem;
|
|
7
|
+
readonly origin?: {
|
|
8
|
+
readonly recordId: string;
|
|
9
|
+
/** Original part index for each current part, or null for a synthetic or replaced part. */
|
|
10
|
+
readonly parts: readonly (number | null)[];
|
|
11
|
+
};
|
|
12
|
+
}
|
|
13
|
+
export interface AgentContextUnit {
|
|
14
|
+
/** Append sequence of the turn's starting record; zero before the first recorded turn. */
|
|
15
|
+
readonly turn: number;
|
|
16
|
+
/** Tool calls and their results travel together in one unit. */
|
|
17
|
+
readonly items: readonly AgentContextItem[];
|
|
18
|
+
}
|
|
19
|
+
export interface AgentContextRequest {
|
|
20
|
+
readonly sessionId: string;
|
|
21
|
+
readonly agentId: string;
|
|
22
|
+
/** Starting sequence of the current turn, or a proposed boundary during admission. */
|
|
23
|
+
readonly turn: number;
|
|
24
|
+
}
|
|
25
|
+
interface Transform {
|
|
26
|
+
id: string;
|
|
27
|
+
transform(projection: readonly AgentContextUnit[], request: AgentContextRequest): readonly AgentContextUnit[];
|
|
28
|
+
}
|
|
29
|
+
const directories = new WeakMap<SessionDirectory, Map<string, Transform["transform"]>>();
|
|
30
|
+
|
|
31
|
+
/** Each synchronous stage receives the preceding stage's projection in registration order. */
|
|
32
|
+
export function defineAgentContextTransform({ id, transform }: Transform) {
|
|
33
|
+
return defineModule(plugin => {
|
|
34
|
+
let entries = directories.get(plugin.sessions);
|
|
35
|
+
if (!entries) directories.set(plugin.sessions, entries = new Map());
|
|
36
|
+
const key = plugin.id + ":" + id;
|
|
37
|
+
if (!id.trim() || entries.has(key)) throw new PluginApiError("conflict", "Duplicate or empty Agent context transform: " + key);
|
|
38
|
+
entries.set(key, transform);
|
|
39
|
+
plugin.lifetime.signal.addEventListener("abort", () => entries.delete(key), { once: true });
|
|
40
|
+
return {};
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function validUnit(unit: AgentContextUnit): boolean {
|
|
45
|
+
if (!Array.isArray(unit.items) || !unit.items.length) return false;
|
|
46
|
+
const calls = new Map<string, string>();
|
|
47
|
+
for (const { item } of unit.items) {
|
|
48
|
+
if (item.kind === "output") for (const part of item.parts) {
|
|
49
|
+
if (part.kind !== "toolcall") continue;
|
|
50
|
+
if (calls.has(part.callId)) return false;
|
|
51
|
+
calls.set(part.callId, part.name);
|
|
52
|
+
}
|
|
53
|
+
if (item.kind === "result") {
|
|
54
|
+
if (calls.get(item.callId) !== item.name || !item.outcome.model.length) return false;
|
|
55
|
+
calls.delete(item.callId);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
return calls.size === 0;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export function captureAgentContextTransforms(plugin: PluginContext) {
|
|
62
|
+
const entries = [...directories.get(plugin.sessions) ?? []];
|
|
63
|
+
if (!entries.length) return null;
|
|
64
|
+
return (base: readonly AgentContextUnit[], request: AgentContextRequest): readonly AgentContextUnit[] => {
|
|
65
|
+
let projection = structuredClone(base);
|
|
66
|
+
for (const [id, transform] of entries) {
|
|
67
|
+
const next = transform(projection, request);
|
|
68
|
+
if (!Array.isArray(next) || !next.every(validUnit))
|
|
69
|
+
throw new PluginApiError("invalid_argument", "Invalid Agent context projection from " + id + ". Tool calls and results must remain paired within a unit.");
|
|
70
|
+
projection = next;
|
|
71
|
+
}
|
|
72
|
+
return projection;
|
|
73
|
+
};
|
|
74
|
+
}
|
package/src/runtime.ts
CHANGED
|
@@ -4,6 +4,8 @@ import { defineModule, PluginApiError, type PluginContext, type PluginModule, ty
|
|
|
4
4
|
|
|
5
5
|
export { agentServices, registerAgentServices, type AgentServices } from "./agent-services.ts";
|
|
6
6
|
export { defineAgentPreparer, captureAgentPreparers, type AgentPreparation, type AgentPromptBlock } from "./preparation.ts";
|
|
7
|
+
export { defineAgentContextTransform, captureAgentContextTransforms,
|
|
8
|
+
type AgentContextUnit, type AgentContextItem, type AgentContextRequest } from "./context-transform.ts";
|
|
7
9
|
|
|
8
10
|
export interface AgentCreationDescription {
|
|
9
11
|
readonly id: string;
|