@oh-my-pi/pi-utils 17.2.9 → 17.2.11

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.
Files changed (84) hide show
  1. package/CHANGELOG.md +31 -0
  2. package/dist/types/acp/connection.d.ts +118 -0
  3. package/dist/types/acp/protocol.d.ts +526 -0
  4. package/dist/types/acp/schema.d.ts +41 -0
  5. package/dist/types/acp/stream.d.ts +8 -0
  6. package/dist/types/acp/transport.d.ts +81 -0
  7. package/dist/types/acp.d.ts +6 -0
  8. package/dist/types/browsers.d.ts +68 -0
  9. package/dist/types/chalk.d.ts +125 -0
  10. package/dist/types/dates.d.ts +7 -0
  11. package/dist/types/docx/converter.d.ts +46 -0
  12. package/dist/types/docx/xml.d.ts +26 -0
  13. package/dist/types/docx/zip.d.ts +6 -0
  14. package/dist/types/docx.d.ts +11 -0
  15. package/dist/types/dom/core.d.ts +431 -0
  16. package/dist/types/dom/parser.d.ts +7 -0
  17. package/dist/types/dom/selector.d.ts +5 -0
  18. package/dist/types/dom.d.ts +5 -0
  19. package/dist/types/frontmatter.d.ts +21 -0
  20. package/dist/types/headers.d.ts +34 -0
  21. package/dist/types/logger/rotating-file.d.ts +18 -0
  22. package/dist/types/lru.d.ts +46 -0
  23. package/dist/types/marked/core.d.ts +445 -0
  24. package/dist/types/marked.d.ts +2 -0
  25. package/dist/types/postmortem.d.ts +14 -0
  26. package/dist/types/procmgr.d.ts +16 -0
  27. package/dist/types/prompt.d.ts +2 -2
  28. package/dist/types/readability/readability.d.ts +9 -0
  29. package/dist/types/readability/readerable.d.ts +10 -0
  30. package/dist/types/readability/types.d.ts +70 -0
  31. package/dist/types/readability.d.ts +4 -0
  32. package/dist/types/template.d.ts +62 -0
  33. package/dist/types/turndown/gfm.d.ts +11 -0
  34. package/dist/types/turndown/html.d.ts +5 -0
  35. package/dist/types/turndown/service.d.ts +21 -0
  36. package/dist/types/turndown/types.d.ts +70 -0
  37. package/dist/types/turndown.d.ts +4 -0
  38. package/dist/types/vterm/buffer.d.ts +99 -0
  39. package/dist/types/vterm/terminal.d.ts +44 -0
  40. package/dist/types/vterm.d.ts +8 -0
  41. package/dist/types/xml.d.ts +31 -0
  42. package/package.json +2 -5
  43. package/src/acp/connection.ts +344 -0
  44. package/src/acp/protocol.ts +466 -0
  45. package/src/acp/schema.ts +160 -0
  46. package/src/acp/stream.ts +82 -0
  47. package/src/acp/transport.ts +213 -0
  48. package/src/acp.ts +6 -0
  49. package/src/browsers.ts +501 -0
  50. package/src/chalk.ts +312 -0
  51. package/src/dates.ts +194 -0
  52. package/src/docx/converter.ts +681 -0
  53. package/src/docx/xml.ts +166 -0
  54. package/src/docx/zip.ts +87 -0
  55. package/src/docx.ts +20 -0
  56. package/src/dom/core.ts +1254 -0
  57. package/src/dom/parser.ts +370 -0
  58. package/src/dom/selector.ts +290 -0
  59. package/src/dom.ts +33 -0
  60. package/src/frontmatter.ts +44 -14
  61. package/src/headers.ts +167 -0
  62. package/src/logger/rotating-file.ts +149 -0
  63. package/src/logger.ts +12 -28
  64. package/src/lru.ts +185 -0
  65. package/src/marked/core.ts +1576 -0
  66. package/src/marked.ts +2 -0
  67. package/src/postmortem.ts +44 -1
  68. package/src/procmgr.ts +21 -4
  69. package/src/prompt.ts +5 -22
  70. package/src/readability/readability.ts +533 -0
  71. package/src/readability/readerable.ts +51 -0
  72. package/src/readability/types.ts +72 -0
  73. package/src/readability.ts +11 -0
  74. package/src/template.ts +586 -0
  75. package/src/turndown/gfm.ts +106 -0
  76. package/src/turndown/html.ts +257 -0
  77. package/src/turndown/service.ts +334 -0
  78. package/src/turndown/types.ts +81 -0
  79. package/src/turndown.ts +5 -0
  80. package/src/vterm/buffer.ts +218 -0
  81. package/src/vterm/terminal.ts +773 -0
  82. package/src/vterm.ts +8 -0
  83. package/src/xml.ts +313 -0
  84. package/src/winston-daily-rotate-file.d.ts +0 -6
package/src/lru.ts ADDED
@@ -0,0 +1,185 @@
1
+ /** Behavior-compatible reimplementation of lru-cache's used surface. */
2
+
3
+ /** Why an entry left the cache. */
4
+ export type DisposeReason = "evict" | "set" | "delete" | "expire";
5
+
6
+ /** Options supported by {@link LRUCache}. */
7
+ export interface LRUCacheOptions<K, V> {
8
+ /** Maximum number of retained entries. */
9
+ max?: number;
10
+ /** Maximum aggregate calculated size. */
11
+ maxSize?: number;
12
+ /** Maximum calculated size of one entry. */
13
+ maxEntrySize?: number;
14
+ /** Calculates an entry's size. */
15
+ sizeCalculation?: (value: V, key: K) => number;
16
+ /** Entry lifetime in milliseconds; zero disables expiry. */
17
+ ttl?: number;
18
+ /** Refreshes an entry's lifetime when it is read. */
19
+ updateAgeOnGet?: boolean;
20
+ /** Called synchronously before an entry is removed. */
21
+ dispose?: (value: V, key: K, reason: DisposeReason) => void;
22
+ }
23
+
24
+ interface Entry<V> {
25
+ value: V;
26
+ size: number;
27
+ start: number;
28
+ }
29
+
30
+ function positiveInteger(value: number | undefined, name: string): number {
31
+ if (value === undefined) return 0;
32
+ if (!Number.isInteger(value) || value < 0) throw new TypeError(`${name} must be a non-negative integer`);
33
+ return value;
34
+ }
35
+
36
+ /** A bounded least-recently-used cache with optional size and lifetime limits. */
37
+ export class LRUCache<K, V> {
38
+ readonly #entries = new Map<K, Entry<V>>();
39
+ readonly #max: number;
40
+ readonly #maxSize: number;
41
+ readonly #maxEntrySize: number;
42
+ readonly #sizeCalculation: ((value: V, key: K) => number) | undefined;
43
+ readonly #ttl: number;
44
+ readonly #updateAgeOnGet: boolean;
45
+ readonly #dispose: ((value: V, key: K, reason: DisposeReason) => void) | undefined;
46
+ #calculatedSize = 0;
47
+
48
+ /** Creates an empty cache. */
49
+ constructor(options: LRUCacheOptions<K, V>) {
50
+ this.#max = positiveInteger(options.max, "max");
51
+ this.#maxSize = positiveInteger(options.maxSize, "maxSize");
52
+ const explicitMaxEntrySize = positiveInteger(options.maxEntrySize, "maxEntrySize");
53
+ this.#maxEntrySize = explicitMaxEntrySize || this.#maxSize;
54
+ this.#ttl = positiveInteger(options.ttl, "ttl");
55
+ if (this.#max === 0 && this.#maxSize === 0 && this.#ttl === 0) {
56
+ throw new TypeError("At least one of max, maxSize, or ttl is required");
57
+ }
58
+ if ((this.#maxSize !== 0 || this.#maxEntrySize !== 0) && options.sizeCalculation === undefined) {
59
+ throw new TypeError("sizeCalculation is required when a size limit is set");
60
+ }
61
+ this.#sizeCalculation = options.sizeCalculation;
62
+ this.#updateAgeOnGet = options.updateAgeOnGet === true;
63
+ this.#dispose = options.dispose;
64
+ }
65
+
66
+ /** Number of entries, including stale entries not yet removed by `get`. */
67
+ get size(): number {
68
+ return this.#entries.size;
69
+ }
70
+
71
+ /** Aggregate calculated size of retained entries. */
72
+ get calculatedSize(): number {
73
+ return this.#calculatedSize;
74
+ }
75
+
76
+ /** Stores a value and makes it most recently used. */
77
+ set(key: K, value: V | undefined): this {
78
+ if (value === undefined) {
79
+ this.delete(key);
80
+ return this;
81
+ }
82
+
83
+ const size = this.#entrySize(value, key);
84
+ const previous = this.#entries.get(key);
85
+ if (this.#maxEntrySize !== 0 && size > this.#maxEntrySize) {
86
+ if (previous !== undefined) this.#remove(key, previous, "set");
87
+ return this;
88
+ }
89
+
90
+ if (previous !== undefined) {
91
+ if (previous.value !== value) this.#dispose?.(previous.value, key, "set");
92
+ this.#calculatedSize -= previous.size;
93
+ this.#entries.delete(key);
94
+ }
95
+
96
+ while (
97
+ (this.#max !== 0 && this.#entries.size >= this.#max) ||
98
+ (this.#maxSize !== 0 && this.#calculatedSize + size > this.#maxSize)
99
+ ) {
100
+ const oldest = this.#entries.entries().next().value as [K, Entry<V>] | undefined;
101
+ if (oldest === undefined) break;
102
+ this.#remove(oldest[0], oldest[1], "evict");
103
+ }
104
+ this.#entries.set(key, { value, size, start: this.#ttl === 0 ? 0 : performance.now() });
105
+ this.#calculatedSize += size;
106
+ return this;
107
+ }
108
+
109
+ /** Returns a value and makes a fresh entry most recently used. */
110
+ get(key: K): V | undefined {
111
+ const entry = this.#entries.get(key);
112
+ if (entry === undefined) return undefined;
113
+ if (this.#isStale(entry)) {
114
+ this.#remove(key, entry, "expire");
115
+ return undefined;
116
+ }
117
+ if (this.#updateAgeOnGet && this.#ttl !== 0) entry.start = performance.now();
118
+ this.#entries.delete(key);
119
+ this.#entries.set(key, entry);
120
+ return entry.value;
121
+ }
122
+
123
+ /** Reports whether a fresh value is present without changing recency. */
124
+ has(key: K): boolean {
125
+ const entry = this.#entries.get(key);
126
+ return entry !== undefined && !this.#isStale(entry);
127
+ }
128
+
129
+ /** Returns a fresh value without changing recency or removing stale data. */
130
+ peek(key: K): V | undefined {
131
+ const entry = this.#entries.get(key);
132
+ return entry === undefined || this.#isStale(entry) ? undefined : entry.value;
133
+ }
134
+
135
+ /** Removes a value, returning whether one was present. */
136
+ delete(key: K): boolean {
137
+ const entry = this.#entries.get(key);
138
+ if (entry === undefined) return false;
139
+ this.#remove(key, entry, "delete");
140
+ return true;
141
+ }
142
+
143
+ /** Removes every value from least to most recently used. */
144
+ clear(): void {
145
+ for (const [key, entry] of this.#entries) this.#dispose?.(entry.value, key, "delete");
146
+ this.#entries.clear();
147
+ this.#calculatedSize = 0;
148
+ }
149
+
150
+ /** Iterates fresh keys from most to least recently used. */
151
+ *keys(): Generator<K, void, unknown> {
152
+ const entries = [...this.#entries.entries()];
153
+ for (let index = entries.length - 1; index >= 0; index--) {
154
+ const [key, entry] = entries[index]!;
155
+ if (!this.#isStale(entry)) yield key;
156
+ }
157
+ }
158
+
159
+ /** Iterates fresh values from most to least recently used. */
160
+ *values(): Generator<V, void, unknown> {
161
+ const entries = [...this.#entries.values()];
162
+ for (let index = entries.length - 1; index >= 0; index--) {
163
+ const entry = entries[index]!;
164
+ if (!this.#isStale(entry)) yield entry.value;
165
+ }
166
+ }
167
+
168
+ #entrySize(value: V, key: K): number {
169
+ if (this.#sizeCalculation === undefined) return 0;
170
+ const size = this.#sizeCalculation(value, key);
171
+ if (!Number.isInteger(size) || size <= 0)
172
+ throw new TypeError("sizeCalculation return invalid (expect positive integer)");
173
+ return size;
174
+ }
175
+
176
+ #isStale(entry: Entry<V>): boolean {
177
+ return this.#ttl !== 0 && performance.now() - entry.start > this.#ttl;
178
+ }
179
+
180
+ #remove(key: K, entry: Entry<V>, reason: DisposeReason): void {
181
+ this.#dispose?.(entry.value, key, reason);
182
+ this.#entries.delete(key);
183
+ this.#calculatedSize -= entry.size;
184
+ }
185
+ }