adaptive-bitmask 1.0.0-rc.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/LICENSE +21 -0
- package/README.md +454 -0
- package/dist/ai/index.d.mts +123 -0
- package/dist/ai/index.d.ts +123 -0
- package/dist/ai/index.js +1141 -0
- package/dist/ai/index.mjs +173 -0
- package/dist/chunk-ZWEXRT33.mjs +2402 -0
- package/dist/coordinator-Df48t6yJ.d.mts +521 -0
- package/dist/coordinator-Df48t6yJ.d.ts +521 -0
- package/dist/index.d.mts +431 -0
- package/dist/index.d.ts +431 -0
- package/dist/index.js +2484 -0
- package/dist/index.mjs +118 -0
- package/package.json +82 -0
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Arbiter,
|
|
3
|
+
BitmaskMessage,
|
|
4
|
+
Coordinator,
|
|
5
|
+
SchemaManager,
|
|
6
|
+
decode,
|
|
7
|
+
encode,
|
|
8
|
+
hasEmergency
|
|
9
|
+
} from "../chunk-ZWEXRT33.mjs";
|
|
10
|
+
|
|
11
|
+
// src/ai/session.ts
|
|
12
|
+
var CoordinationSession = class {
|
|
13
|
+
schema;
|
|
14
|
+
coordinator;
|
|
15
|
+
arbiter;
|
|
16
|
+
_agentIds = /* @__PURE__ */ new Map();
|
|
17
|
+
constructor(config) {
|
|
18
|
+
this.schema = new SchemaManager({
|
|
19
|
+
emergencyPrefix: config.emergencyPrefix ?? "EMERGENCY_",
|
|
20
|
+
emergencyFeatures: config.emergencyFeatures
|
|
21
|
+
});
|
|
22
|
+
this.schema.registerAll(config.features);
|
|
23
|
+
this.coordinator = new Coordinator({
|
|
24
|
+
...config.coordinatorConfig,
|
|
25
|
+
schemaVersion: this.schema.version
|
|
26
|
+
});
|
|
27
|
+
this.arbiter = new Arbiter(config.arbiterConfig);
|
|
28
|
+
}
|
|
29
|
+
/** Deterministic agent ID: FNV-1a hash of name → uint32. */
|
|
30
|
+
agentId(name) {
|
|
31
|
+
const cached = this._agentIds.get(name);
|
|
32
|
+
if (cached !== void 0) return cached;
|
|
33
|
+
let hash = 2166136261;
|
|
34
|
+
for (let i = 0; i < name.length; i++) {
|
|
35
|
+
hash ^= name.charCodeAt(i);
|
|
36
|
+
hash = Math.imul(hash, 16777619);
|
|
37
|
+
}
|
|
38
|
+
const id = hash >>> 0;
|
|
39
|
+
this._agentIds.set(name, id);
|
|
40
|
+
return id;
|
|
41
|
+
}
|
|
42
|
+
/** Start a new coordination round. Clears the coordinator buffer. */
|
|
43
|
+
startRound() {
|
|
44
|
+
this.coordinator.startRound();
|
|
45
|
+
}
|
|
46
|
+
/** Encode features + create message + receive in one call. */
|
|
47
|
+
report(agentName, features) {
|
|
48
|
+
const { mask, mapped, unmapped } = encode(
|
|
49
|
+
features,
|
|
50
|
+
this.schema.featureToBit
|
|
51
|
+
);
|
|
52
|
+
const msg = BitmaskMessage.now(
|
|
53
|
+
mask,
|
|
54
|
+
this.agentId(agentName),
|
|
55
|
+
this.schema.version
|
|
56
|
+
);
|
|
57
|
+
const accepted = this.coordinator.receive(msg);
|
|
58
|
+
return { accepted, mapped, unmapped };
|
|
59
|
+
}
|
|
60
|
+
/** Aggregate current buffer + score via arbiter. */
|
|
61
|
+
decide() {
|
|
62
|
+
const { aggregatedMask, confidence } = this.coordinator.aggregate();
|
|
63
|
+
const aggregatedFeatures = decode(aggregatedMask, this.schema.bitToFeatures);
|
|
64
|
+
const result = this.arbiter.score(aggregatedMask, confidence);
|
|
65
|
+
return {
|
|
66
|
+
decision: result.decision,
|
|
67
|
+
aggregatedFeatures,
|
|
68
|
+
confidence,
|
|
69
|
+
result
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
// src/ai/tools.ts
|
|
75
|
+
import { tool } from "ai";
|
|
76
|
+
import { z } from "zod";
|
|
77
|
+
function createCoordinationTools(session) {
|
|
78
|
+
const reportObservation = tool({
|
|
79
|
+
description: "Report observed features to the coordination layer. Each feature string is encoded into the shared bitmask.",
|
|
80
|
+
parameters: z.object({
|
|
81
|
+
agentName: z.string().describe("Name identifying this agent"),
|
|
82
|
+
features: z.array(z.string()).describe("Feature names observed by this agent")
|
|
83
|
+
}),
|
|
84
|
+
execute: async ({ agentName, features }) => {
|
|
85
|
+
const result = session.report(agentName, features);
|
|
86
|
+
return {
|
|
87
|
+
accepted: result.accepted,
|
|
88
|
+
mapped: result.mapped,
|
|
89
|
+
unmapped: result.unmapped
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
});
|
|
93
|
+
const getConsensus = tool({
|
|
94
|
+
description: "Query the current aggregated consensus state across all agents.",
|
|
95
|
+
parameters: z.object({}),
|
|
96
|
+
execute: async () => {
|
|
97
|
+
const { aggregatedMask, confidence, uniqueAgents } = session.coordinator.aggregate();
|
|
98
|
+
const features = decode(aggregatedMask, session.schema.bitToFeatures);
|
|
99
|
+
const confidenceObj = {};
|
|
100
|
+
for (const [bit, conf] of confidence) {
|
|
101
|
+
confidenceObj[String(bit)] = conf;
|
|
102
|
+
}
|
|
103
|
+
return {
|
|
104
|
+
features,
|
|
105
|
+
confidence: confidenceObj,
|
|
106
|
+
agentCount: uniqueAgents
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
});
|
|
110
|
+
const requestDecision = tool({
|
|
111
|
+
description: "Trigger the arbiter to score the current aggregated state and return a decision.",
|
|
112
|
+
parameters: z.object({}),
|
|
113
|
+
execute: async () => {
|
|
114
|
+
const { decision, aggregatedFeatures, result } = session.decide();
|
|
115
|
+
return {
|
|
116
|
+
decision,
|
|
117
|
+
score: result.finalScore,
|
|
118
|
+
features: aggregatedFeatures,
|
|
119
|
+
hasEmergency: result.hasEmergency
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
});
|
|
123
|
+
return { reportObservation, getConsensus, requestDecision };
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
// src/ai/middleware.ts
|
|
127
|
+
function createCoordinationMiddleware(session, options = {}) {
|
|
128
|
+
const { injectConsensus = false, autoEncodeToolCalls = false, agentName } = options;
|
|
129
|
+
if (autoEncodeToolCalls && !agentName) {
|
|
130
|
+
throw new Error("agentName is required when autoEncodeToolCalls is enabled");
|
|
131
|
+
}
|
|
132
|
+
const middleware = {};
|
|
133
|
+
if (injectConsensus) {
|
|
134
|
+
middleware.transformParams = async ({ params }) => {
|
|
135
|
+
const { aggregatedMask, confidence } = session.coordinator.aggregate();
|
|
136
|
+
const features = decode(aggregatedMask, session.schema.bitToFeatures);
|
|
137
|
+
const emergency = hasEmergency(aggregatedMask);
|
|
138
|
+
const confidenceEntries = Array.from(confidence.entries()).map(([bit, conf]) => ` bit ${bit}: ${(conf * 100).toFixed(0)}%`).join("\n");
|
|
139
|
+
const consensusText = [
|
|
140
|
+
"[Coordination Consensus]",
|
|
141
|
+
`Features: ${features.length > 0 ? features.join(", ") : "none"}`,
|
|
142
|
+
`Emergency: ${emergency}`,
|
|
143
|
+
confidenceEntries ? `Confidence:
|
|
144
|
+
${confidenceEntries}` : ""
|
|
145
|
+
].filter(Boolean).join("\n");
|
|
146
|
+
return {
|
|
147
|
+
...params,
|
|
148
|
+
prompt: [
|
|
149
|
+
{ role: "system", content: consensusText },
|
|
150
|
+
...params.prompt
|
|
151
|
+
]
|
|
152
|
+
};
|
|
153
|
+
};
|
|
154
|
+
}
|
|
155
|
+
if (autoEncodeToolCalls) {
|
|
156
|
+
middleware.wrapGenerate = async ({ doGenerate }) => {
|
|
157
|
+
const result = await doGenerate();
|
|
158
|
+
if (result.toolCalls && result.toolCalls.length > 0) {
|
|
159
|
+
const matchingFeatures = result.toolCalls.map((tc) => tc.toolName).filter((name) => session.schema.featureToBit.has(name));
|
|
160
|
+
if (matchingFeatures.length > 0) {
|
|
161
|
+
session.report(agentName, matchingFeatures);
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
return result;
|
|
165
|
+
};
|
|
166
|
+
}
|
|
167
|
+
return middleware;
|
|
168
|
+
}
|
|
169
|
+
export {
|
|
170
|
+
CoordinationSession,
|
|
171
|
+
createCoordinationMiddleware,
|
|
172
|
+
createCoordinationTools
|
|
173
|
+
};
|