hive-swarm 2.0.0 → 2.1.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/.codex-plugin/plugin.json +1 -1
- package/README.md +3 -3
- package/board.css +391 -186
- package/board.html +136 -44
- package/board.js +1203 -122
- package/codex.mjs +44 -13
- package/core.mjs +154 -6
- package/engine.mjs +188 -9
- package/hive-bees.js +110 -64
- package/hive-gif.js +756 -0
- package/hive-graph.js +1421 -383
- package/installer.mjs +36 -16
- package/package.json +2 -1
- package/server.mjs +59 -14
- package/skills/swarm/SKILL.md +7 -6
- package/worktrees.mjs +167 -98
package/hive-gif.js
ADDED
|
@@ -0,0 +1,756 @@
|
|
|
1
|
+
(() => {
|
|
2
|
+
'use strict';
|
|
3
|
+
|
|
4
|
+
function resolveGraph(explicit) {
|
|
5
|
+
if (explicit) return explicit;
|
|
6
|
+
if (typeof window !== 'undefined' && window.HiveGraph) return window.HiveGraph;
|
|
7
|
+
return globalThis.HiveGraph;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
function resolveBees(explicit) {
|
|
11
|
+
if (explicit) return explicit;
|
|
12
|
+
if (typeof window !== 'undefined' && window.HiveBee) return window.HiveBee;
|
|
13
|
+
return globalThis.HiveBee;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
function buildPalette(frames, maxColors = 128) {
|
|
17
|
+
const limit = Math.max(2, Math.min(256, maxColors | 0));
|
|
18
|
+
const counts = new Map();
|
|
19
|
+
const sampleCount = Math.min(frames.length, 16);
|
|
20
|
+
const step = frames.length <= sampleCount ? 1 : (frames.length - 1) / (sampleCount - 1);
|
|
21
|
+
const sampledIndices = new Set();
|
|
22
|
+
for (let i = 0; i < sampleCount; i++) sampledIndices.add(Math.round(i * step));
|
|
23
|
+
sampledIndices.add(0);
|
|
24
|
+
sampledIndices.add(frames.length - 1);
|
|
25
|
+
|
|
26
|
+
const first = frames[0].rgba;
|
|
27
|
+
const bgRgb = first.length >= 4 && first[3] >= 128
|
|
28
|
+
? ((first[0] << 16) | (first[1] << 8) | first[2])
|
|
29
|
+
: 0xFFFFFF;
|
|
30
|
+
|
|
31
|
+
for (const idx of sampledIndices) {
|
|
32
|
+
const rgba = frames[idx].rgba;
|
|
33
|
+
let lastRgb = -1;
|
|
34
|
+
let run = 0;
|
|
35
|
+
for (let p = 0; p + 3 < rgba.length; p += 4) {
|
|
36
|
+
const rgb = rgba[p + 3] < 128
|
|
37
|
+
? bgRgb
|
|
38
|
+
: ((rgba[p] << 16) | (rgba[p + 1] << 8) | rgba[p + 2]);
|
|
39
|
+
if (rgb === lastRgb) {
|
|
40
|
+
run++;
|
|
41
|
+
} else {
|
|
42
|
+
if (lastRgb !== -1) counts.set(lastRgb, (counts.get(lastRgb) ?? 0) + run);
|
|
43
|
+
lastRgb = rgb;
|
|
44
|
+
run = 1;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
if (lastRgb !== -1) counts.set(lastRgb, (counts.get(lastRgb) ?? 0) + run);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
let paletteColors;
|
|
51
|
+
if (counts.size <= limit) {
|
|
52
|
+
paletteColors = [...counts.keys()];
|
|
53
|
+
} else {
|
|
54
|
+
const sorted = [...counts.entries()].sort((a, b) => b[1] - a[1]);
|
|
55
|
+
const preserveCount = Math.min(sorted.length, Math.max(2, Math.floor(limit * 0.48)));
|
|
56
|
+
paletteColors = sorted.slice(0, preserveCount).map(entry => entry[0]);
|
|
57
|
+
const preservedSet = new Set(paletteColors);
|
|
58
|
+
|
|
59
|
+
// Bucket remaining colors into 5-bit RGB space for fast weighted median-cut.
|
|
60
|
+
const bucketWeights = new Map();
|
|
61
|
+
for (let i = preserveCount; i < sorted.length; i++) {
|
|
62
|
+
const [rgb, weight] = sorted[i];
|
|
63
|
+
const r5 = (rgb >> 19) & 31;
|
|
64
|
+
const g5 = (rgb >> 11) & 31;
|
|
65
|
+
const b5 = (rgb >> 3) & 31;
|
|
66
|
+
const key5 = (r5 << 10) | (g5 << 5) | b5;
|
|
67
|
+
let entry = bucketWeights.get(key5);
|
|
68
|
+
if (!entry) {
|
|
69
|
+
entry = { rSum: 0, gSum: 0, bSum: 0, w: 0 };
|
|
70
|
+
bucketWeights.set(key5, entry);
|
|
71
|
+
}
|
|
72
|
+
entry.rSum += ((rgb >> 16) & 255) * weight;
|
|
73
|
+
entry.gSum += ((rgb >> 8) & 255) * weight;
|
|
74
|
+
entry.bSum += (rgb & 255) * weight;
|
|
75
|
+
entry.w += weight;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
const items = [];
|
|
79
|
+
for (const entry of bucketWeights.values()) {
|
|
80
|
+
items.push({
|
|
81
|
+
r: Math.round(entry.rSum / entry.w),
|
|
82
|
+
g: Math.round(entry.gSum / entry.w),
|
|
83
|
+
b: Math.round(entry.bSum / entry.w),
|
|
84
|
+
w: entry.w,
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
const remainingSlots = limit - paletteColors.length;
|
|
89
|
+
if (items.length <= remainingSlots) {
|
|
90
|
+
for (const item of items) {
|
|
91
|
+
const rgb = (item.r << 16) | (item.g << 8) | item.b;
|
|
92
|
+
if (!preservedSet.has(rgb)) {
|
|
93
|
+
preservedSet.add(rgb);
|
|
94
|
+
paletteColors.push(rgb);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
} else {
|
|
98
|
+
const makeBox = list => {
|
|
99
|
+
let rMin = 255, rMax = 0, gMin = 255, gMax = 0, bMin = 255, bMax = 0, totalW = 0;
|
|
100
|
+
for (const it of list) {
|
|
101
|
+
if (it.r < rMin) rMin = it.r;
|
|
102
|
+
if (it.r > rMax) rMax = it.r;
|
|
103
|
+
if (it.g < gMin) gMin = it.g;
|
|
104
|
+
if (it.g > gMax) gMax = it.g;
|
|
105
|
+
if (it.b < bMin) bMin = it.b;
|
|
106
|
+
if (it.b > bMax) bMax = it.b;
|
|
107
|
+
totalW += it.w;
|
|
108
|
+
}
|
|
109
|
+
const spanR = rMax - rMin;
|
|
110
|
+
const spanG = gMax - gMin;
|
|
111
|
+
const spanB = bMax - bMin;
|
|
112
|
+
const axis = spanG >= spanR && spanG >= spanB ? 'g' : (spanR >= spanB ? 'r' : 'b');
|
|
113
|
+
return { list, span: Math.max(spanR, spanG, spanB), axis, totalW };
|
|
114
|
+
};
|
|
115
|
+
|
|
116
|
+
const boxes = [makeBox(items)];
|
|
117
|
+
while (boxes.length < remainingSlots) {
|
|
118
|
+
let bestIdx = -1;
|
|
119
|
+
let bestScore = -1;
|
|
120
|
+
for (let i = 0; i < boxes.length; i++) {
|
|
121
|
+
const box = boxes[i];
|
|
122
|
+
if (box.list.length > 1 && box.span > 0) {
|
|
123
|
+
const score = box.span * Math.log2(box.totalW + 2);
|
|
124
|
+
if (score > bestScore) {
|
|
125
|
+
bestScore = score;
|
|
126
|
+
bestIdx = i;
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
if (bestIdx === -1) break;
|
|
131
|
+
const chosen = boxes.splice(bestIdx, 1)[0];
|
|
132
|
+
const axis = chosen.axis;
|
|
133
|
+
chosen.list.sort((a, b) => a[axis] - b[axis]);
|
|
134
|
+
let half = chosen.totalW / 2;
|
|
135
|
+
let acc = 0;
|
|
136
|
+
let split = 1;
|
|
137
|
+
for (let i = 0; i < chosen.list.length - 1; i++) {
|
|
138
|
+
acc += chosen.list[i].w;
|
|
139
|
+
if (acc >= half) {
|
|
140
|
+
split = i + 1;
|
|
141
|
+
break;
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
boxes.push(makeBox(chosen.list.slice(0, split)), makeBox(chosen.list.slice(split)));
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
for (const box of boxes) {
|
|
148
|
+
let rSum = 0, gSum = 0, bSum = 0, wSum = 0;
|
|
149
|
+
for (const it of box.list) {
|
|
150
|
+
rSum += it.r * it.w;
|
|
151
|
+
gSum += it.g * it.w;
|
|
152
|
+
bSum += it.b * it.w;
|
|
153
|
+
wSum += it.w;
|
|
154
|
+
}
|
|
155
|
+
const r = Math.round(rSum / wSum);
|
|
156
|
+
const g = Math.round(gSum / wSum);
|
|
157
|
+
const b = Math.round(bSum / wSum);
|
|
158
|
+
const rgb = (r << 16) | (g << 8) | b;
|
|
159
|
+
if (!preservedSet.has(rgb)) {
|
|
160
|
+
preservedSet.add(rgb);
|
|
161
|
+
paletteColors.push(rgb);
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
if (!paletteColors.length) paletteColors = [0xFFFFFF];
|
|
168
|
+
let bits = 1;
|
|
169
|
+
while ((1 << bits) < paletteColors.length && bits < 8) bits++;
|
|
170
|
+
const tableLength = 1 << bits;
|
|
171
|
+
const minCodeSize = Math.max(2, bits);
|
|
172
|
+
const tableSizeFlag = bits - 1;
|
|
173
|
+
|
|
174
|
+
const paletteR = new Uint8Array(paletteColors.length);
|
|
175
|
+
const paletteG = new Uint8Array(paletteColors.length);
|
|
176
|
+
const paletteB = new Uint8Array(paletteColors.length);
|
|
177
|
+
const exactLookup = new Map();
|
|
178
|
+
for (let i = 0; i < paletteColors.length; i++) {
|
|
179
|
+
const rgb = paletteColors[i];
|
|
180
|
+
paletteR[i] = (rgb >> 16) & 255;
|
|
181
|
+
paletteG[i] = (rgb >> 8) & 255;
|
|
182
|
+
paletteB[i] = rgb & 255;
|
|
183
|
+
exactLookup.set(rgb, i);
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
return {
|
|
187
|
+
colors: paletteColors,
|
|
188
|
+
paletteR,
|
|
189
|
+
paletteG,
|
|
190
|
+
paletteB,
|
|
191
|
+
tableLength,
|
|
192
|
+
minCodeSize,
|
|
193
|
+
tableSizeFlag,
|
|
194
|
+
bgRgb,
|
|
195
|
+
exactLookup,
|
|
196
|
+
};
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
function mapPixels(rgba, pixelCount, palette) {
|
|
200
|
+
const { paletteR, paletteG, paletteB, bgRgb, exactLookup } = palette;
|
|
201
|
+
const len = paletteR.length;
|
|
202
|
+
const indices = new Uint8Array(pixelCount);
|
|
203
|
+
let lastRgb = -1;
|
|
204
|
+
let lastIndex = 0;
|
|
205
|
+
|
|
206
|
+
for (let i = 0, p = 0; i < pixelCount; i++, p += 4) {
|
|
207
|
+
const rgb = rgba[p + 3] < 128
|
|
208
|
+
? bgRgb
|
|
209
|
+
: ((rgba[p] << 16) | (rgba[p + 1] << 8) | rgba[p + 2]);
|
|
210
|
+
if (rgb === lastRgb) {
|
|
211
|
+
indices[i] = lastIndex;
|
|
212
|
+
continue;
|
|
213
|
+
}
|
|
214
|
+
let idx = exactLookup.get(rgb);
|
|
215
|
+
if (idx === undefined) {
|
|
216
|
+
const r = (rgb >> 16) & 255;
|
|
217
|
+
const g = (rgb >> 8) & 255;
|
|
218
|
+
const b = rgb & 255;
|
|
219
|
+
let bestDist = Infinity;
|
|
220
|
+
idx = 0;
|
|
221
|
+
for (let k = 0; k < len; k++) {
|
|
222
|
+
const dr = r - paletteR[k];
|
|
223
|
+
const dg = g - paletteG[k];
|
|
224
|
+
const db = b - paletteB[k];
|
|
225
|
+
const dist = dr * dr + dg * dg + db * db;
|
|
226
|
+
if (dist < bestDist) {
|
|
227
|
+
bestDist = dist;
|
|
228
|
+
idx = k;
|
|
229
|
+
if (dist === 0) break;
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
if (exactLookup.size < 65536) exactLookup.set(rgb, idx);
|
|
233
|
+
}
|
|
234
|
+
lastRgb = rgb;
|
|
235
|
+
lastIndex = idx;
|
|
236
|
+
indices[i] = idx;
|
|
237
|
+
}
|
|
238
|
+
return indices;
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
class ByteWriter {
|
|
242
|
+
constructor(initialCapacity = 32768) {
|
|
243
|
+
this.buf = new Uint8Array(initialCapacity);
|
|
244
|
+
this.pos = 0;
|
|
245
|
+
}
|
|
246
|
+
ensure(extra) {
|
|
247
|
+
if (this.pos + extra <= this.buf.length) return;
|
|
248
|
+
let nextLen = this.buf.length * 2;
|
|
249
|
+
while (nextLen < this.pos + extra) nextLen *= 2;
|
|
250
|
+
const next = new Uint8Array(nextLen);
|
|
251
|
+
next.set(this.buf.subarray(0, this.pos));
|
|
252
|
+
this.buf = next;
|
|
253
|
+
}
|
|
254
|
+
byte(value) {
|
|
255
|
+
this.ensure(1);
|
|
256
|
+
this.buf[this.pos++] = value & 0xFF;
|
|
257
|
+
}
|
|
258
|
+
u16(value) {
|
|
259
|
+
this.ensure(2);
|
|
260
|
+
this.buf[this.pos++] = value & 0xFF;
|
|
261
|
+
this.buf[this.pos++] = (value >> 8) & 0xFF;
|
|
262
|
+
}
|
|
263
|
+
bytes(arr) {
|
|
264
|
+
this.ensure(arr.length);
|
|
265
|
+
this.buf.set(arr, this.pos);
|
|
266
|
+
this.pos += arr.length;
|
|
267
|
+
}
|
|
268
|
+
ascii(str) {
|
|
269
|
+
this.ensure(str.length);
|
|
270
|
+
for (let i = 0; i < str.length; i++) this.buf[this.pos++] = str.charCodeAt(i);
|
|
271
|
+
}
|
|
272
|
+
finish() {
|
|
273
|
+
return this.buf.slice(0, this.pos);
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
function lzwEncode(indices, minCodeSize, writer) {
|
|
278
|
+
const clearCode = 1 << minCodeSize;
|
|
279
|
+
const eoiCode = clearCode + 1;
|
|
280
|
+
const HASH_SIZE = 8192;
|
|
281
|
+
const HASH_MASK = HASH_SIZE - 1;
|
|
282
|
+
const htab = new Int32Array(HASH_SIZE).fill(-1);
|
|
283
|
+
const codetab = new Int16Array(HASH_SIZE);
|
|
284
|
+
const subBlock = new Uint8Array(255);
|
|
285
|
+
let subLen = 0;
|
|
286
|
+
let curAccum = 0;
|
|
287
|
+
let curBits = 0;
|
|
288
|
+
|
|
289
|
+
function flushSubBlock() {
|
|
290
|
+
if (!subLen) return;
|
|
291
|
+
writer.byte(subLen);
|
|
292
|
+
writer.bytes(subBlock.subarray(0, subLen));
|
|
293
|
+
subLen = 0;
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
function outputCode(code, codeSize) {
|
|
297
|
+
curAccum |= code << curBits;
|
|
298
|
+
curBits += codeSize;
|
|
299
|
+
while (curBits >= 8) {
|
|
300
|
+
subBlock[subLen++] = curAccum & 0xFF;
|
|
301
|
+
if (subLen === 255) flushSubBlock();
|
|
302
|
+
curAccum >>>= 8;
|
|
303
|
+
curBits -= 8;
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
let codeSize = minCodeSize + 1;
|
|
308
|
+
let nextCode = eoiCode + 1;
|
|
309
|
+
let maxCode = 1 << codeSize;
|
|
310
|
+
|
|
311
|
+
outputCode(clearCode, codeSize);
|
|
312
|
+
if (!indices.length) {
|
|
313
|
+
outputCode(eoiCode, codeSize);
|
|
314
|
+
if (curBits > 0) subBlock[subLen++] = curAccum & 0xFF;
|
|
315
|
+
flushSubBlock();
|
|
316
|
+
writer.byte(0);
|
|
317
|
+
return;
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
let prefix = indices[0];
|
|
321
|
+
for (let i = 1; i < indices.length; i++) {
|
|
322
|
+
const k = indices[i];
|
|
323
|
+
const key = (prefix << 8) | k;
|
|
324
|
+
let hash = ((prefix << 5) ^ k ^ (prefix >>> 3)) & HASH_MASK;
|
|
325
|
+
// Odd step guarantees full-period probing over power-of-2 HASH_SIZE (8192).
|
|
326
|
+
const step = ((prefix ^ (k << 3)) | 1) & HASH_MASK;
|
|
327
|
+
let found = false;
|
|
328
|
+
while (htab[hash] !== -1) {
|
|
329
|
+
if (htab[hash] === key) {
|
|
330
|
+
prefix = codetab[hash];
|
|
331
|
+
found = true;
|
|
332
|
+
break;
|
|
333
|
+
}
|
|
334
|
+
hash = (hash + step) & HASH_MASK;
|
|
335
|
+
}
|
|
336
|
+
if (found) continue;
|
|
337
|
+
|
|
338
|
+
outputCode(prefix, codeSize);
|
|
339
|
+
if (nextCode < 4096) {
|
|
340
|
+
htab[hash] = key;
|
|
341
|
+
codetab[hash] = nextCode++;
|
|
342
|
+
if (nextCode > maxCode && codeSize < 12) {
|
|
343
|
+
codeSize++;
|
|
344
|
+
maxCode = 1 << codeSize;
|
|
345
|
+
}
|
|
346
|
+
} else {
|
|
347
|
+
outputCode(clearCode, codeSize);
|
|
348
|
+
htab.fill(-1);
|
|
349
|
+
codeSize = minCodeSize + 1;
|
|
350
|
+
nextCode = eoiCode + 1;
|
|
351
|
+
maxCode = 1 << codeSize;
|
|
352
|
+
}
|
|
353
|
+
prefix = k;
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
outputCode(prefix, codeSize);
|
|
357
|
+
outputCode(eoiCode, codeSize);
|
|
358
|
+
if (curBits > 0) {
|
|
359
|
+
subBlock[subLen++] = curAccum & 0xFF;
|
|
360
|
+
if (subLen === 255) flushSubBlock();
|
|
361
|
+
}
|
|
362
|
+
flushSubBlock();
|
|
363
|
+
writer.byte(0);
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
function encodeGif(frames, { width, height, loop = 0, maxColors = 128 } = {}) {
|
|
367
|
+
if (!Array.isArray(frames) || !frames.length) {
|
|
368
|
+
throw new Error('encodeGif requires at least one frame.');
|
|
369
|
+
}
|
|
370
|
+
const w = Math.floor(Number(width));
|
|
371
|
+
const h = Math.floor(Number(height));
|
|
372
|
+
if (!Number.isFinite(w) || w <= 0 || w > 65535 || !Number.isFinite(h) || h <= 0 || h > 65535) {
|
|
373
|
+
throw new Error('encodeGif requires valid width and height (1..65535).');
|
|
374
|
+
}
|
|
375
|
+
const pixelCount = w * h;
|
|
376
|
+
for (let i = 0; i < frames.length; i++) {
|
|
377
|
+
if (!frames[i]?.rgba || frames[i].rgba.length < pixelCount * 4) {
|
|
378
|
+
throw new Error(`Frame ${i} RGBA buffer is smaller than ${w}x${h}x4 bytes.`);
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
const palette = buildPalette(frames, maxColors);
|
|
383
|
+
const writer = new ByteWriter(Math.max(16384, pixelCount));
|
|
384
|
+
|
|
385
|
+
// GIF89a header + Logical Screen Descriptor.
|
|
386
|
+
writer.ascii('GIF89a');
|
|
387
|
+
writer.u16(w);
|
|
388
|
+
writer.u16(h);
|
|
389
|
+
writer.byte(0x80 | (0x07 << 4) | palette.tableSizeFlag);
|
|
390
|
+
writer.byte(0);
|
|
391
|
+
writer.byte(0);
|
|
392
|
+
|
|
393
|
+
// Global Color Table.
|
|
394
|
+
for (let i = 0; i < palette.tableLength; i++) {
|
|
395
|
+
if (i < palette.colors.length) {
|
|
396
|
+
writer.byte(palette.paletteR[i]);
|
|
397
|
+
writer.byte(palette.paletteG[i]);
|
|
398
|
+
writer.byte(palette.paletteB[i]);
|
|
399
|
+
} else {
|
|
400
|
+
writer.byte(0);
|
|
401
|
+
writer.byte(0);
|
|
402
|
+
writer.byte(0);
|
|
403
|
+
}
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
// NETSCAPE2.0 looping application extension.
|
|
407
|
+
if (loop !== false && loop !== null && loop !== undefined) {
|
|
408
|
+
const loopCount = Math.max(0, Math.min(65535, Math.floor(Number(loop))));
|
|
409
|
+
writer.byte(0x21);
|
|
410
|
+
writer.byte(0xFF);
|
|
411
|
+
writer.byte(0x0B);
|
|
412
|
+
writer.ascii('NETSCAPE2.0');
|
|
413
|
+
writer.byte(0x03);
|
|
414
|
+
writer.byte(0x01);
|
|
415
|
+
writer.u16(loopCount);
|
|
416
|
+
writer.byte(0x00);
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
for (const frame of frames) {
|
|
420
|
+
const delayCs = Math.max(1, Math.min(65535, Math.round(frame.delayCs ?? 50)));
|
|
421
|
+
// Graphic Control Extension.
|
|
422
|
+
writer.byte(0x21);
|
|
423
|
+
writer.byte(0xF9);
|
|
424
|
+
writer.byte(0x04);
|
|
425
|
+
writer.byte(0x04); // Disposal method 1 (do not dispose)
|
|
426
|
+
writer.u16(delayCs);
|
|
427
|
+
writer.byte(0x00);
|
|
428
|
+
writer.byte(0x00);
|
|
429
|
+
|
|
430
|
+
// Image Descriptor.
|
|
431
|
+
writer.byte(0x2C);
|
|
432
|
+
writer.u16(0);
|
|
433
|
+
writer.u16(0);
|
|
434
|
+
writer.u16(w);
|
|
435
|
+
writer.u16(h);
|
|
436
|
+
writer.byte(0x00);
|
|
437
|
+
|
|
438
|
+
// LZW compressed pixel indices.
|
|
439
|
+
writer.byte(palette.minCodeSize);
|
|
440
|
+
const indices = mapPixels(frame.rgba, pixelCount, palette);
|
|
441
|
+
lzwEncode(indices, palette.minCodeSize, writer);
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
writer.byte(0x3B);
|
|
445
|
+
return writer.finish();
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
function parseCssColor(color) {
|
|
449
|
+
const str = String(color ?? '').trim();
|
|
450
|
+
if (str.startsWith('#')) {
|
|
451
|
+
const hex = str.slice(1);
|
|
452
|
+
if (hex.length === 3) {
|
|
453
|
+
return [
|
|
454
|
+
Number.parseInt(hex[0] + hex[0], 16),
|
|
455
|
+
Number.parseInt(hex[1] + hex[1], 16),
|
|
456
|
+
Number.parseInt(hex[2] + hex[2], 16),
|
|
457
|
+
255,
|
|
458
|
+
];
|
|
459
|
+
}
|
|
460
|
+
if (hex.length >= 6) {
|
|
461
|
+
return [
|
|
462
|
+
Number.parseInt(hex.slice(0, 2), 16),
|
|
463
|
+
Number.parseInt(hex.slice(2, 4), 16),
|
|
464
|
+
Number.parseInt(hex.slice(4, 6), 16),
|
|
465
|
+
hex.length === 8 ? Number.parseInt(hex.slice(6, 8), 16) : 255,
|
|
466
|
+
];
|
|
467
|
+
}
|
|
468
|
+
}
|
|
469
|
+
const rgbMatch = /^rgba?\(([^)]+)\)$/i.exec(str);
|
|
470
|
+
if (rgbMatch) {
|
|
471
|
+
const parts = rgbMatch[1].split(',').map(s => Number.parseFloat(s.trim()));
|
|
472
|
+
return [
|
|
473
|
+
parts[0] || 0,
|
|
474
|
+
parts[1] || 0,
|
|
475
|
+
parts[2] || 0,
|
|
476
|
+
parts.length >= 4 ? Math.round(parts[3] * 255) : 255,
|
|
477
|
+
];
|
|
478
|
+
}
|
|
479
|
+
return [0, 0, 0, 255];
|
|
480
|
+
}
|
|
481
|
+
|
|
482
|
+
// Minimal software 2D canvas fallback for Node environments without DOM canvas.
|
|
483
|
+
function createSoftwareCanvas(width, height) {
|
|
484
|
+
const data = new Uint8ClampedArray(width * height * 4);
|
|
485
|
+
const stack = [];
|
|
486
|
+
let state = { fillStyle: '#000000', strokeStyle: '#000000', lineWidth: 1, globalAlpha: 1 };
|
|
487
|
+
let path = [];
|
|
488
|
+
let curX = 0, curY = 0, startX = 0, startY = 0;
|
|
489
|
+
|
|
490
|
+
function blendPixel(px, py, rgba) {
|
|
491
|
+
const ix = Math.round(px), iy = Math.round(py);
|
|
492
|
+
if (ix < 0 || ix >= width || iy < 0 || iy >= height) return;
|
|
493
|
+
const alpha = (rgba[3] / 255) * state.globalAlpha;
|
|
494
|
+
if (alpha <= 0) return;
|
|
495
|
+
const off = (iy * width + ix) * 4;
|
|
496
|
+
if (alpha >= 0.995) {
|
|
497
|
+
data[off] = rgba[0]; data[off + 1] = rgba[1]; data[off + 2] = rgba[2]; data[off + 3] = 255;
|
|
498
|
+
} else {
|
|
499
|
+
const inv = 1 - alpha;
|
|
500
|
+
data[off] = Math.round(data[off] * inv + rgba[0] * alpha);
|
|
501
|
+
data[off + 1] = Math.round(data[off + 1] * inv + rgba[1] * alpha);
|
|
502
|
+
data[off + 2] = Math.round(data[off + 2] * inv + rgba[2] * alpha);
|
|
503
|
+
data[off + 3] = 255;
|
|
504
|
+
}
|
|
505
|
+
}
|
|
506
|
+
|
|
507
|
+
function drawSeg(x0, y0, x1, y1, rgba, lw) {
|
|
508
|
+
const steps = Math.max(1, Math.ceil(Math.hypot(x1 - x0, y1 - y0)));
|
|
509
|
+
const rad = Math.max(0, Math.floor((lw - 1) / 2));
|
|
510
|
+
for (let i = 0; i <= steps; i++) {
|
|
511
|
+
const x = x0 + ((x1 - x0) * i) / steps;
|
|
512
|
+
const y = y0 + ((y1 - y0) * i) / steps;
|
|
513
|
+
for (let dy = -rad; dy <= rad; dy++) {
|
|
514
|
+
for (let dx = -rad; dx <= rad; dx++) blendPixel(x + dx, y + dy, rgba);
|
|
515
|
+
}
|
|
516
|
+
}
|
|
517
|
+
}
|
|
518
|
+
|
|
519
|
+
const ctx = {
|
|
520
|
+
get fillStyle() { return state.fillStyle; },
|
|
521
|
+
set fillStyle(v) { state.fillStyle = v; },
|
|
522
|
+
get strokeStyle() { return state.strokeStyle; },
|
|
523
|
+
set strokeStyle(v) { state.strokeStyle = v; },
|
|
524
|
+
get lineWidth() { return state.lineWidth; },
|
|
525
|
+
set lineWidth(v) { state.lineWidth = v; },
|
|
526
|
+
get globalAlpha() { return state.globalAlpha; },
|
|
527
|
+
set globalAlpha(v) { state.globalAlpha = v; },
|
|
528
|
+
font: '',
|
|
529
|
+
textAlign: 'left',
|
|
530
|
+
textBaseline: 'alphabetic',
|
|
531
|
+
lineCap: 'butt',
|
|
532
|
+
save() { stack.push({ ...state }); },
|
|
533
|
+
restore() { if (stack.length) state = stack.pop(); },
|
|
534
|
+
setLineDash() {},
|
|
535
|
+
clearRect(x, y, w, h) {
|
|
536
|
+
const x0 = Math.max(0, Math.floor(x)), y0 = Math.max(0, Math.floor(y));
|
|
537
|
+
const x1 = Math.min(width, Math.ceil(x + w)), y1 = Math.min(height, Math.ceil(y + h));
|
|
538
|
+
for (let iy = y0; iy < y1; iy++) {
|
|
539
|
+
for (let ix = x0; ix < x1; ix++) {
|
|
540
|
+
const off = (iy * width + ix) * 4;
|
|
541
|
+
data[off] = data[off + 1] = data[off + 2] = data[off + 3] = 0;
|
|
542
|
+
}
|
|
543
|
+
}
|
|
544
|
+
},
|
|
545
|
+
fillRect(x, y, w, h) {
|
|
546
|
+
const rgba = parseCssColor(state.fillStyle);
|
|
547
|
+
const x0 = Math.max(0, Math.floor(x)), y0 = Math.max(0, Math.floor(y));
|
|
548
|
+
const x1 = Math.min(width, Math.ceil(x + w)), y1 = Math.min(height, Math.ceil(y + h));
|
|
549
|
+
for (let iy = y0; iy < y1; iy++) {
|
|
550
|
+
for (let ix = x0; ix < x1; ix++) blendPixel(ix, iy, rgba);
|
|
551
|
+
}
|
|
552
|
+
},
|
|
553
|
+
beginPath() { path = []; },
|
|
554
|
+
moveTo(x, y) { curX = startX = x; curY = startY = y; path.push([{ x, y }]); },
|
|
555
|
+
lineTo(x, y) {
|
|
556
|
+
if (!path.length) path.push([{ x: curX, y: curY }]);
|
|
557
|
+
path[path.length - 1].push({ x, y });
|
|
558
|
+
curX = x; curY = y;
|
|
559
|
+
},
|
|
560
|
+
quadraticCurveTo(cx, cy, x, y) {
|
|
561
|
+
if (!path.length) path.push([{ x: curX, y: curY }]);
|
|
562
|
+
const sub = path[path.length - 1];
|
|
563
|
+
const x0 = curX, y0 = curY;
|
|
564
|
+
for (let s = 1; s <= 12; s++) {
|
|
565
|
+
const t = s / 12, u = 1 - t;
|
|
566
|
+
sub.push({ x: u * u * x0 + 2 * u * t * cx + t * t * x, y: u * u * y0 + 2 * u * t * cy + t * t * y });
|
|
567
|
+
}
|
|
568
|
+
curX = x; curY = y;
|
|
569
|
+
},
|
|
570
|
+
arc(cx, cy, r, a0, a1) {
|
|
571
|
+
const sub = [];
|
|
572
|
+
const steps = 18;
|
|
573
|
+
for (let s = 0; s <= steps; s++) {
|
|
574
|
+
const a = a0 + ((a1 - a0) * s) / steps;
|
|
575
|
+
sub.push({ x: cx + r * Math.cos(a), y: cy + r * Math.sin(a) });
|
|
576
|
+
}
|
|
577
|
+
path.push(sub);
|
|
578
|
+
if (sub.length) { curX = sub.at(-1).x; curY = sub.at(-1).y; startX = sub[0].x; startY = sub[0].y; }
|
|
579
|
+
},
|
|
580
|
+
closePath() {
|
|
581
|
+
if (path.length && path[path.length - 1].length) {
|
|
582
|
+
path[path.length - 1].push({ x: startX, y: startY });
|
|
583
|
+
curX = startX; curY = startY;
|
|
584
|
+
}
|
|
585
|
+
},
|
|
586
|
+
fill() {
|
|
587
|
+
const rgba = parseCssColor(state.fillStyle);
|
|
588
|
+
for (const poly of path) {
|
|
589
|
+
if (poly.length < 3) continue;
|
|
590
|
+
let minY = height, maxY = 0;
|
|
591
|
+
for (const pt of poly) {
|
|
592
|
+
if (pt.y < minY) minY = pt.y;
|
|
593
|
+
if (pt.y > maxY) maxY = pt.y;
|
|
594
|
+
}
|
|
595
|
+
const y0 = Math.max(0, Math.floor(minY)), y1 = Math.min(height - 1, Math.ceil(maxY));
|
|
596
|
+
for (let y = y0; y <= y1; y++) {
|
|
597
|
+
const xs = [];
|
|
598
|
+
for (let i = 0, j = poly.length - 1; i < poly.length; j = i++) {
|
|
599
|
+
const p1 = poly[i], p2 = poly[j];
|
|
600
|
+
if ((p1.y <= y && p2.y > y) || (p2.y <= y && p1.y > y)) {
|
|
601
|
+
xs.push(p1.x + ((y - p1.y) / (p2.y - p1.y)) * (p2.x - p1.x));
|
|
602
|
+
}
|
|
603
|
+
}
|
|
604
|
+
xs.sort((a, b) => a - b);
|
|
605
|
+
for (let k = 0; k + 1 < xs.length; k += 2) {
|
|
606
|
+
const xStart = Math.max(0, Math.ceil(xs[k]));
|
|
607
|
+
const xEnd = Math.min(width - 1, Math.floor(xs[k + 1]));
|
|
608
|
+
for (let x = xStart; x <= xEnd; x++) blendPixel(x, y, rgba);
|
|
609
|
+
}
|
|
610
|
+
}
|
|
611
|
+
}
|
|
612
|
+
},
|
|
613
|
+
stroke() {
|
|
614
|
+
const rgba = parseCssColor(state.strokeStyle);
|
|
615
|
+
const lw = state.lineWidth || 1;
|
|
616
|
+
for (const poly of path) {
|
|
617
|
+
for (let i = 1; i < poly.length; i++) {
|
|
618
|
+
drawSeg(poly[i - 1].x, poly[i - 1].y, poly[i].x, poly[i].y, rgba, lw);
|
|
619
|
+
}
|
|
620
|
+
}
|
|
621
|
+
},
|
|
622
|
+
fillText(text, x, y) {
|
|
623
|
+
const str = String(text ?? '');
|
|
624
|
+
if (!str) return;
|
|
625
|
+
const rgba = parseCssColor(state.fillStyle);
|
|
626
|
+
const w = Math.min(str.length * 5, 120);
|
|
627
|
+
const ox = state.textAlign === 'center' ? x - w / 2 : (state.textAlign === 'right' ? x - w : x);
|
|
628
|
+
const oy = y - 3;
|
|
629
|
+
for (let ix = Math.max(0, Math.floor(ox)); ix < Math.min(width, Math.ceil(ox + w)); ix += 2) {
|
|
630
|
+
for (let iy = Math.max(0, Math.floor(oy)); iy < Math.min(height, Math.ceil(oy + 5)); iy += 2) {
|
|
631
|
+
blendPixel(ix, iy, rgba);
|
|
632
|
+
}
|
|
633
|
+
}
|
|
634
|
+
},
|
|
635
|
+
drawImage() {},
|
|
636
|
+
getImageData(x, y, w, h) {
|
|
637
|
+
if (x === 0 && y === 0 && w === width && h === height) return { data: new Uint8ClampedArray(data), width, height };
|
|
638
|
+
return { data: new Uint8ClampedArray(w * h * 4), width: w, height: h };
|
|
639
|
+
},
|
|
640
|
+
};
|
|
641
|
+
|
|
642
|
+
return {
|
|
643
|
+
width,
|
|
644
|
+
height,
|
|
645
|
+
getContext: () => ctx,
|
|
646
|
+
};
|
|
647
|
+
}
|
|
648
|
+
|
|
649
|
+
function exportGraphReplayGif({
|
|
650
|
+
mode = 'hive',
|
|
651
|
+
posts = [],
|
|
652
|
+
members = [],
|
|
653
|
+
phase = 'done',
|
|
654
|
+
pacing = 'even',
|
|
655
|
+
speed = 1,
|
|
656
|
+
hideInactive = false,
|
|
657
|
+
expandBroadcast = false,
|
|
658
|
+
focus = null,
|
|
659
|
+
theme = 'light',
|
|
660
|
+
width = 680,
|
|
661
|
+
height = 460,
|
|
662
|
+
graph,
|
|
663
|
+
bees,
|
|
664
|
+
createCanvas,
|
|
665
|
+
maxFrames = 90,
|
|
666
|
+
} = {}) {
|
|
667
|
+
const graphLib = resolveGraph(graph);
|
|
668
|
+
const beesLib = resolveBees(bees);
|
|
669
|
+
if (typeof graphLib?.renderGraphFrame !== 'function') {
|
|
670
|
+
throw new Error('HiveGraph.renderGraphFrame is required to export a graph replay GIF.');
|
|
671
|
+
}
|
|
672
|
+
const isPost = graphLib.isPost ?? (e => e && (e.kind === 'brief' || e.kind === 'message' || (!e.kind && e.author !== 'system')));
|
|
673
|
+
const timeline = (posts ?? []).filter(isPost).slice().sort((a, b) => (a.seq ?? 0) - (b.seq ?? 0));
|
|
674
|
+
|
|
675
|
+
const snapshots = [{ seq: 0, post: null }];
|
|
676
|
+
const limit = Math.max(2, Math.floor(Number(maxFrames) || 90));
|
|
677
|
+
if (timeline.length > 0) {
|
|
678
|
+
if (timeline.length + 1 <= limit) {
|
|
679
|
+
for (const post of timeline) snapshots.push({ seq: post.seq, post });
|
|
680
|
+
} else {
|
|
681
|
+
const target = limit - 1;
|
|
682
|
+
const chosen = new Set([0, timeline.length - 1]);
|
|
683
|
+
for (let i = 1; i < target - 1; i++) {
|
|
684
|
+
chosen.add(Math.round((i * (timeline.length - 1)) / (target - 1)));
|
|
685
|
+
}
|
|
686
|
+
for (const idx of [...chosen].sort((a, b) => a - b)) {
|
|
687
|
+
snapshots.push({ seq: timeline[idx].seq, post: timeline[idx] });
|
|
688
|
+
}
|
|
689
|
+
}
|
|
690
|
+
}
|
|
691
|
+
|
|
692
|
+
const makeCanvas = createCanvas
|
|
693
|
+
?? (typeof document !== 'undefined'
|
|
694
|
+
? (w, h) => {
|
|
695
|
+
const c = document.createElement('canvas');
|
|
696
|
+
c.width = w;
|
|
697
|
+
c.height = h;
|
|
698
|
+
return c;
|
|
699
|
+
}
|
|
700
|
+
: createSoftwareCanvas);
|
|
701
|
+
|
|
702
|
+
const canvas = makeCanvas(width, height);
|
|
703
|
+
if (canvas.width !== width) canvas.width = width;
|
|
704
|
+
if (canvas.height !== height) canvas.height = height;
|
|
705
|
+
const ctx = canvas.getContext('2d');
|
|
706
|
+
if (!ctx) throw new Error('Could not obtain 2D canvas context for GIF export.');
|
|
707
|
+
|
|
708
|
+
const speedFactor = Math.max(0.25, Number(speed) || 1);
|
|
709
|
+
const frames = [];
|
|
710
|
+
for (let i = 0; i < snapshots.length; i++) {
|
|
711
|
+
const snap = snapshots[i];
|
|
712
|
+
const isLast = i === snapshots.length - 1;
|
|
713
|
+
const prevPost = i > 0 ? snapshots[i - 1].post : null;
|
|
714
|
+
let delayMs = 620;
|
|
715
|
+
if (pacing === 'real' && snap.post?.time && prevPost?.time) {
|
|
716
|
+
const gap = Date.parse(snap.post.time) - Date.parse(prevPost.time);
|
|
717
|
+
if (Number.isFinite(gap)) delayMs = Math.max(110, Math.min(gap, 2000));
|
|
718
|
+
}
|
|
719
|
+
const rawCs = Math.round(delayMs / speedFactor / 10);
|
|
720
|
+
const delayCs = isLast ? Math.max(150, rawCs) : Math.max(4, Math.min(300, rawCs));
|
|
721
|
+
|
|
722
|
+
graphLib.renderGraphFrame(ctx, {
|
|
723
|
+
mode,
|
|
724
|
+
posts,
|
|
725
|
+
members,
|
|
726
|
+
cursor: isLast && timeline.length ? null : snap.seq,
|
|
727
|
+
phase: isLast ? phase : 'working',
|
|
728
|
+
width,
|
|
729
|
+
height,
|
|
730
|
+
theme,
|
|
731
|
+
hideInactive,
|
|
732
|
+
expandBroadcast,
|
|
733
|
+
focus,
|
|
734
|
+
bees: beesLib,
|
|
735
|
+
});
|
|
736
|
+
|
|
737
|
+
const image = ctx.getImageData(0, 0, width, height);
|
|
738
|
+
frames.push({
|
|
739
|
+
rgba: image.data instanceof Uint8ClampedArray ? image.data : new Uint8ClampedArray(image.data),
|
|
740
|
+
delayCs,
|
|
741
|
+
});
|
|
742
|
+
}
|
|
743
|
+
|
|
744
|
+
const bytes = encodeGif(frames, { width, height, loop: 0 });
|
|
745
|
+
const blob = typeof Blob !== 'undefined' ? new Blob([bytes], { type: 'image/gif' }) : undefined;
|
|
746
|
+
return {
|
|
747
|
+
bytes,
|
|
748
|
+
blob,
|
|
749
|
+
frameCount: frames.length,
|
|
750
|
+
width,
|
|
751
|
+
height,
|
|
752
|
+
};
|
|
753
|
+
}
|
|
754
|
+
|
|
755
|
+
globalThis.HiveGif = Object.freeze({ encodeGif, exportGraphReplayGif });
|
|
756
|
+
})();
|