anycodex 0.0.7 → 0.0.8

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.
@@ -0,0 +1,109 @@
1
+ #!/usr/bin/env node
2
+ import {
3
+ Schema_exports,
4
+ withStatics,
5
+ zod_default
6
+ } from "./chunk-3KUWIT4H.js";
7
+
8
+ // ../agent/dist/chunk-ERRKARGL.js
9
+ import { randomBytes } from "crypto";
10
+ var Identifier;
11
+ ((Identifier2) => {
12
+ const prefixes = {
13
+ session: "ses",
14
+ message: "msg",
15
+ question: "que",
16
+ user: "usr",
17
+ part: "prt",
18
+ pty: "pty",
19
+ tool: "tool",
20
+ workspace: "wrk"
21
+ };
22
+ function schema(prefix) {
23
+ return zod_default.string().startsWith(prefixes[prefix]);
24
+ }
25
+ Identifier2.schema = schema;
26
+ const LENGTH = 26;
27
+ let lastTimestamp = 0;
28
+ let counter = 0;
29
+ function ascending(prefix, given) {
30
+ return generateID(prefix, false, given);
31
+ }
32
+ Identifier2.ascending = ascending;
33
+ function descending(prefix, given) {
34
+ return generateID(prefix, true, given);
35
+ }
36
+ Identifier2.descending = descending;
37
+ function generateID(prefix, descending2, given) {
38
+ if (!given) {
39
+ return create(prefix, descending2);
40
+ }
41
+ if (!given.startsWith(prefixes[prefix])) {
42
+ throw new Error(`ID ${given} does not start with ${prefixes[prefix]}`);
43
+ }
44
+ return given;
45
+ }
46
+ function randomBase62(length) {
47
+ const chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
48
+ let result = "";
49
+ const bytes = randomBytes(length);
50
+ for (let i = 0; i < length; i++) {
51
+ result += chars[bytes[i] % 62];
52
+ }
53
+ return result;
54
+ }
55
+ function create(prefix, descending2, timestamp2) {
56
+ const currentTimestamp = timestamp2 ?? Date.now();
57
+ if (currentTimestamp !== lastTimestamp) {
58
+ lastTimestamp = currentTimestamp;
59
+ counter = 0;
60
+ }
61
+ counter++;
62
+ let now = BigInt(currentTimestamp) * BigInt(4096) + BigInt(counter);
63
+ now = descending2 ? ~now : now;
64
+ const timeBytes = Buffer.alloc(6);
65
+ for (let i = 0; i < 6; i++) {
66
+ timeBytes[i] = Number(now >> BigInt(40 - 8 * i) & BigInt(255));
67
+ }
68
+ return prefixes[prefix] + "_" + timeBytes.toString("hex") + randomBase62(LENGTH - 12);
69
+ }
70
+ Identifier2.create = create;
71
+ function timestamp(id) {
72
+ const prefix = id.split("_")[0];
73
+ const hex = id.slice(prefix.length + 1, prefix.length + 13);
74
+ const encoded = BigInt("0x" + hex);
75
+ return Number(encoded / BigInt(4096));
76
+ }
77
+ Identifier2.timestamp = timestamp;
78
+ })(Identifier || (Identifier = {}));
79
+ var sessionIdSchema = Schema_exports.String.pipe(Schema_exports.brand("SessionID"));
80
+ var SessionID = sessionIdSchema.pipe(
81
+ withStatics((schema) => ({
82
+ make: (id) => schema.makeUnsafe(id),
83
+ descending: (id) => schema.makeUnsafe(Identifier.descending("session", id)),
84
+ zod: Identifier.schema("session").pipe(zod_default.custom())
85
+ }))
86
+ );
87
+ var messageIdSchema = Schema_exports.String.pipe(Schema_exports.brand("MessageID"));
88
+ var MessageID = messageIdSchema.pipe(
89
+ withStatics((schema) => ({
90
+ make: (id) => schema.makeUnsafe(id),
91
+ ascending: (id) => schema.makeUnsafe(Identifier.ascending("message", id)),
92
+ zod: Identifier.schema("message").pipe(zod_default.custom())
93
+ }))
94
+ );
95
+ var partIdSchema = Schema_exports.String.pipe(Schema_exports.brand("PartID"));
96
+ var PartID = partIdSchema.pipe(
97
+ withStatics((schema) => ({
98
+ make: (id) => schema.makeUnsafe(id),
99
+ ascending: (id) => schema.makeUnsafe(Identifier.ascending("part", id)),
100
+ zod: Identifier.schema("part").pipe(zod_default.custom())
101
+ }))
102
+ );
103
+
104
+ export {
105
+ Identifier,
106
+ SessionID,
107
+ MessageID,
108
+ PartID
109
+ };