@vsuryav/agent-sim 0.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.
Files changed (154) hide show
  1. package/README.md +25 -0
  2. package/bin/agent-sim.js +25 -0
  3. package/package.json +72 -0
  4. package/src/app-paths.ts +29 -0
  5. package/src/app-sync.test.ts +75 -0
  6. package/src/app-sync.ts +110 -0
  7. package/src/cli.ts +129 -0
  8. package/src/collector/claude-code.test.ts +102 -0
  9. package/src/collector/claude-code.ts +133 -0
  10. package/src/collector/codex-cli.test.ts +116 -0
  11. package/src/collector/codex-cli.ts +149 -0
  12. package/src/collector/db.test.ts +59 -0
  13. package/src/collector/db.ts +125 -0
  14. package/src/collector/names.test.ts +21 -0
  15. package/src/collector/names.ts +28 -0
  16. package/src/collector/personality.test.ts +40 -0
  17. package/src/collector/personality.ts +46 -0
  18. package/src/collector/remote-sync.test.ts +31 -0
  19. package/src/collector/remote-sync.ts +171 -0
  20. package/src/collector/sync.test.ts +67 -0
  21. package/src/collector/sync.ts +148 -0
  22. package/src/collector/types.ts +1 -0
  23. package/src/engine/bootstrap/state.ts +3 -0
  24. package/src/engine/buddy/CompanionSprite.tsx +371 -0
  25. package/src/engine/buddy/companion.ts +133 -0
  26. package/src/engine/buddy/prompt.ts +36 -0
  27. package/src/engine/buddy/sprites.ts +514 -0
  28. package/src/engine/buddy/types.ts +148 -0
  29. package/src/engine/buddy/useBuddyNotification.tsx +98 -0
  30. package/src/engine/ink/Ansi.tsx +292 -0
  31. package/src/engine/ink/bidi.ts +139 -0
  32. package/src/engine/ink/clearTerminal.ts +74 -0
  33. package/src/engine/ink/colorize.ts +231 -0
  34. package/src/engine/ink/components/AlternateScreen.tsx +80 -0
  35. package/src/engine/ink/components/App.tsx +658 -0
  36. package/src/engine/ink/components/AppContext.ts +21 -0
  37. package/src/engine/ink/components/Box.tsx +214 -0
  38. package/src/engine/ink/components/Button.tsx +192 -0
  39. package/src/engine/ink/components/ClockContext.tsx +112 -0
  40. package/src/engine/ink/components/CursorDeclarationContext.ts +32 -0
  41. package/src/engine/ink/components/ErrorOverview.tsx +109 -0
  42. package/src/engine/ink/components/Link.tsx +42 -0
  43. package/src/engine/ink/components/Newline.tsx +39 -0
  44. package/src/engine/ink/components/NoSelect.tsx +68 -0
  45. package/src/engine/ink/components/RawAnsi.tsx +57 -0
  46. package/src/engine/ink/components/ScrollBox.tsx +237 -0
  47. package/src/engine/ink/components/Spacer.tsx +20 -0
  48. package/src/engine/ink/components/StdinContext.ts +49 -0
  49. package/src/engine/ink/components/TerminalFocusContext.tsx +52 -0
  50. package/src/engine/ink/components/TerminalSizeContext.tsx +7 -0
  51. package/src/engine/ink/components/Text.tsx +254 -0
  52. package/src/engine/ink/constants.ts +2 -0
  53. package/src/engine/ink/dom.ts +484 -0
  54. package/src/engine/ink/events/click-event.ts +38 -0
  55. package/src/engine/ink/events/dispatcher.ts +233 -0
  56. package/src/engine/ink/events/emitter.ts +39 -0
  57. package/src/engine/ink/events/event-handlers.ts +73 -0
  58. package/src/engine/ink/events/event.ts +11 -0
  59. package/src/engine/ink/events/focus-event.ts +21 -0
  60. package/src/engine/ink/events/input-event.ts +205 -0
  61. package/src/engine/ink/events/keyboard-event.ts +51 -0
  62. package/src/engine/ink/events/terminal-event.ts +107 -0
  63. package/src/engine/ink/events/terminal-focus-event.ts +19 -0
  64. package/src/engine/ink/focus.ts +181 -0
  65. package/src/engine/ink/frame.ts +124 -0
  66. package/src/engine/ink/get-max-width.ts +27 -0
  67. package/src/engine/ink/global.d.ts +18 -0
  68. package/src/engine/ink/hit-test.ts +130 -0
  69. package/src/engine/ink/hooks/use-animation-frame.ts +57 -0
  70. package/src/engine/ink/hooks/use-app.ts +8 -0
  71. package/src/engine/ink/hooks/use-declared-cursor.ts +73 -0
  72. package/src/engine/ink/hooks/use-input.ts +92 -0
  73. package/src/engine/ink/hooks/use-interval.ts +67 -0
  74. package/src/engine/ink/hooks/use-search-highlight.ts +53 -0
  75. package/src/engine/ink/hooks/use-selection.ts +104 -0
  76. package/src/engine/ink/hooks/use-stdin.ts +8 -0
  77. package/src/engine/ink/hooks/use-tab-status.ts +72 -0
  78. package/src/engine/ink/hooks/use-terminal-focus.ts +16 -0
  79. package/src/engine/ink/hooks/use-terminal-title.ts +31 -0
  80. package/src/engine/ink/hooks/use-terminal-viewport.ts +96 -0
  81. package/src/engine/ink/ink.tsx +1723 -0
  82. package/src/engine/ink/instances.ts +10 -0
  83. package/src/engine/ink/layout/engine.ts +6 -0
  84. package/src/engine/ink/layout/geometry.ts +97 -0
  85. package/src/engine/ink/layout/node.ts +152 -0
  86. package/src/engine/ink/layout/yoga.ts +308 -0
  87. package/src/engine/ink/line-width-cache.ts +24 -0
  88. package/src/engine/ink/log-update.ts +773 -0
  89. package/src/engine/ink/measure-element.ts +23 -0
  90. package/src/engine/ink/measure-text.ts +47 -0
  91. package/src/engine/ink/node-cache.ts +54 -0
  92. package/src/engine/ink/optimizer.ts +93 -0
  93. package/src/engine/ink/output.ts +797 -0
  94. package/src/engine/ink/parse-keypress.ts +801 -0
  95. package/src/engine/ink/reconciler.ts +512 -0
  96. package/src/engine/ink/render-border.ts +231 -0
  97. package/src/engine/ink/render-node-to-output.ts +1462 -0
  98. package/src/engine/ink/render-to-screen.ts +231 -0
  99. package/src/engine/ink/renderer.ts +178 -0
  100. package/src/engine/ink/root.ts +184 -0
  101. package/src/engine/ink/screen.ts +1486 -0
  102. package/src/engine/ink/searchHighlight.ts +93 -0
  103. package/src/engine/ink/selection.ts +917 -0
  104. package/src/engine/ink/squash-text-nodes.ts +92 -0
  105. package/src/engine/ink/stringWidth.ts +222 -0
  106. package/src/engine/ink/styles.ts +771 -0
  107. package/src/engine/ink/supports-hyperlinks.ts +57 -0
  108. package/src/engine/ink/tabstops.ts +46 -0
  109. package/src/engine/ink/terminal-focus-state.ts +47 -0
  110. package/src/engine/ink/terminal-querier.ts +212 -0
  111. package/src/engine/ink/terminal.ts +248 -0
  112. package/src/engine/ink/termio/ansi.ts +75 -0
  113. package/src/engine/ink/termio/csi.ts +319 -0
  114. package/src/engine/ink/termio/dec.ts +60 -0
  115. package/src/engine/ink/termio/esc.ts +67 -0
  116. package/src/engine/ink/termio/osc.ts +493 -0
  117. package/src/engine/ink/termio/parser.ts +394 -0
  118. package/src/engine/ink/termio/sgr.ts +308 -0
  119. package/src/engine/ink/termio/tokenize.ts +319 -0
  120. package/src/engine/ink/termio/types.ts +236 -0
  121. package/src/engine/ink/useTerminalNotification.ts +126 -0
  122. package/src/engine/ink/warn.ts +9 -0
  123. package/src/engine/ink/widest-line.ts +19 -0
  124. package/src/engine/ink/wrap-text.ts +74 -0
  125. package/src/engine/ink/wrapAnsi.ts +20 -0
  126. package/src/engine/native-ts/yoga-layout/enums.ts +134 -0
  127. package/src/engine/native-ts/yoga-layout/index.ts +2578 -0
  128. package/src/engine/stubs/bootstrap-state.ts +4 -0
  129. package/src/engine/stubs/debug.ts +6 -0
  130. package/src/engine/stubs/log.ts +4 -0
  131. package/src/engine/utils/debug.ts +5 -0
  132. package/src/engine/utils/earlyInput.ts +4 -0
  133. package/src/engine/utils/env.ts +15 -0
  134. package/src/engine/utils/envUtils.ts +4 -0
  135. package/src/engine/utils/execFileNoThrow.ts +24 -0
  136. package/src/engine/utils/fullscreen.ts +4 -0
  137. package/src/engine/utils/intl.ts +9 -0
  138. package/src/engine/utils/log.ts +3 -0
  139. package/src/engine/utils/semver.ts +13 -0
  140. package/src/engine/utils/sliceAnsi.ts +10 -0
  141. package/src/engine/utils/theme.ts +17 -0
  142. package/src/game/App.tsx +141 -0
  143. package/src/game/agents/behavior.ts +249 -0
  144. package/src/game/agents/speech.ts +57 -0
  145. package/src/game/canvas.ts +98 -0
  146. package/src/game/launch.ts +36 -0
  147. package/src/game/ship/ShipView.tsx +145 -0
  148. package/src/game/ship/ship-map.ts +172 -0
  149. package/src/game/ui/AgentBio.tsx +72 -0
  150. package/src/game/ui/HUD.tsx +63 -0
  151. package/src/game/ui/StatusBar.tsx +49 -0
  152. package/src/game/useKeyboard.ts +62 -0
  153. package/src/main.tsx +22 -0
  154. package/src/run-interactive.ts +74 -0
@@ -0,0 +1,92 @@
1
+ import type { DOMElement } from './dom.js'
2
+ import type { TextStyles } from './styles.js'
3
+
4
+ /**
5
+ * A segment of text with its associated styles.
6
+ * Used for structured rendering without ANSI string transforms.
7
+ */
8
+ export type StyledSegment = {
9
+ text: string
10
+ styles: TextStyles
11
+ hyperlink?: string
12
+ }
13
+
14
+ /**
15
+ * Squash text nodes into styled segments, propagating styles down through the tree.
16
+ * This allows structured styling without relying on ANSI string transforms.
17
+ */
18
+ export function squashTextNodesToSegments(
19
+ node: DOMElement,
20
+ inheritedStyles: TextStyles = {},
21
+ inheritedHyperlink?: string,
22
+ out: StyledSegment[] = [],
23
+ ): StyledSegment[] {
24
+ const mergedStyles = node.textStyles
25
+ ? { ...inheritedStyles, ...node.textStyles }
26
+ : inheritedStyles
27
+
28
+ for (const childNode of node.childNodes) {
29
+ if (childNode === undefined) {
30
+ continue
31
+ }
32
+
33
+ if (childNode.nodeName === '#text') {
34
+ if (childNode.nodeValue.length > 0) {
35
+ out.push({
36
+ text: childNode.nodeValue,
37
+ styles: mergedStyles,
38
+ hyperlink: inheritedHyperlink,
39
+ })
40
+ }
41
+ } else if (
42
+ childNode.nodeName === 'ink-text' ||
43
+ childNode.nodeName === 'ink-virtual-text'
44
+ ) {
45
+ squashTextNodesToSegments(
46
+ childNode,
47
+ mergedStyles,
48
+ inheritedHyperlink,
49
+ out,
50
+ )
51
+ } else if (childNode.nodeName === 'ink-link') {
52
+ const href = childNode.attributes['href'] as string | undefined
53
+ squashTextNodesToSegments(
54
+ childNode,
55
+ mergedStyles,
56
+ href || inheritedHyperlink,
57
+ out,
58
+ )
59
+ }
60
+ }
61
+
62
+ return out
63
+ }
64
+
65
+ /**
66
+ * Squash text nodes into a plain string (without styles).
67
+ * Used for text measurement in layout calculations.
68
+ */
69
+ function squashTextNodes(node: DOMElement): string {
70
+ let text = ''
71
+
72
+ for (const childNode of node.childNodes) {
73
+ if (childNode === undefined) {
74
+ continue
75
+ }
76
+
77
+ if (childNode.nodeName === '#text') {
78
+ text += childNode.nodeValue
79
+ } else if (
80
+ childNode.nodeName === 'ink-text' ||
81
+ childNode.nodeName === 'ink-virtual-text'
82
+ ) {
83
+ text += squashTextNodes(childNode)
84
+ } else if (childNode.nodeName === 'ink-link') {
85
+ text += squashTextNodes(childNode)
86
+ }
87
+ }
88
+
89
+ return text
90
+ }
91
+
92
+ export default squashTextNodes
@@ -0,0 +1,222 @@
1
+ import emojiRegex from 'emoji-regex'
2
+ import { eastAsianWidth } from 'get-east-asian-width'
3
+ import stripAnsi from 'strip-ansi'
4
+ import { getGraphemeSegmenter } from '../utils/intl.js'
5
+
6
+ const EMOJI_REGEX = emojiRegex()
7
+
8
+ /**
9
+ * Fallback JavaScript implementation of stringWidth when Bun.stringWidth is not available.
10
+ *
11
+ * Get the display width of a string as it would appear in a terminal.
12
+ *
13
+ * This is a more accurate alternative to the string-width package that correctly handles
14
+ * characters like ⚠ (U+26A0) which string-width incorrectly reports as width 2.
15
+ *
16
+ * The implementation uses eastAsianWidth directly with ambiguousAsWide: false,
17
+ * which correctly treats ambiguous-width characters as narrow (width 1) as
18
+ * recommended by the Unicode standard for Western contexts.
19
+ */
20
+ function stringWidthJavaScript(str: string): number {
21
+ if (typeof str !== 'string' || str.length === 0) {
22
+ return 0
23
+ }
24
+
25
+ // Fast path: pure ASCII string (no ANSI codes, no wide chars)
26
+ let isPureAscii = true
27
+ for (let i = 0; i < str.length; i++) {
28
+ const code = str.charCodeAt(i)
29
+ // Check for non-ASCII or ANSI escape (0x1b)
30
+ if (code >= 127 || code === 0x1b) {
31
+ isPureAscii = false
32
+ break
33
+ }
34
+ }
35
+ if (isPureAscii) {
36
+ // Count printable characters (exclude control chars)
37
+ let width = 0
38
+ for (let i = 0; i < str.length; i++) {
39
+ const code = str.charCodeAt(i)
40
+ if (code > 0x1f) {
41
+ width++
42
+ }
43
+ }
44
+ return width
45
+ }
46
+
47
+ // Strip ANSI if escape character is present
48
+ if (str.includes('\x1b')) {
49
+ str = stripAnsi(str)
50
+ if (str.length === 0) {
51
+ return 0
52
+ }
53
+ }
54
+
55
+ // Fast path: simple Unicode (no emoji, variation selectors, or joiners)
56
+ if (!needsSegmentation(str)) {
57
+ let width = 0
58
+ for (const char of str) {
59
+ const codePoint = char.codePointAt(0)!
60
+ if (!isZeroWidth(codePoint)) {
61
+ width += eastAsianWidth(codePoint, { ambiguousAsWide: false })
62
+ }
63
+ }
64
+ return width
65
+ }
66
+
67
+ let width = 0
68
+
69
+ for (const { segment: grapheme } of getGraphemeSegmenter().segment(str)) {
70
+ // Check for emoji first (most emoji sequences are width 2)
71
+ EMOJI_REGEX.lastIndex = 0
72
+ if (EMOJI_REGEX.test(grapheme)) {
73
+ width += getEmojiWidth(grapheme)
74
+ continue
75
+ }
76
+
77
+ // Calculate width for non-emoji graphemes
78
+ // For grapheme clusters (like Devanagari conjuncts with virama+ZWJ), only count
79
+ // the first non-zero-width character's width since the cluster renders as one glyph
80
+ for (const char of grapheme) {
81
+ const codePoint = char.codePointAt(0)!
82
+ if (!isZeroWidth(codePoint)) {
83
+ width += eastAsianWidth(codePoint, { ambiguousAsWide: false })
84
+ break
85
+ }
86
+ }
87
+ }
88
+
89
+ return width
90
+ }
91
+
92
+ function needsSegmentation(str: string): boolean {
93
+ for (const char of str) {
94
+ const cp = char.codePointAt(0)!
95
+ // Emoji ranges
96
+ if (cp >= 0x1f300 && cp <= 0x1faff) return true
97
+ if (cp >= 0x2600 && cp <= 0x27bf) return true
98
+ if (cp >= 0x1f1e6 && cp <= 0x1f1ff) return true
99
+ // Variation selectors, ZWJ
100
+ if (cp >= 0xfe00 && cp <= 0xfe0f) return true
101
+ if (cp === 0x200d) return true
102
+ }
103
+ return false
104
+ }
105
+
106
+ function getEmojiWidth(grapheme: string): number {
107
+ // Regional indicators: single = 1, pair = 2
108
+ const first = grapheme.codePointAt(0)!
109
+ if (first >= 0x1f1e6 && first <= 0x1f1ff) {
110
+ let count = 0
111
+ for (const _ of grapheme) count++
112
+ return count === 1 ? 1 : 2
113
+ }
114
+
115
+ // Incomplete keycap: digit/symbol + VS16 without U+20E3
116
+ if (grapheme.length === 2) {
117
+ const second = grapheme.codePointAt(1)
118
+ if (
119
+ second === 0xfe0f &&
120
+ ((first >= 0x30 && first <= 0x39) || first === 0x23 || first === 0x2a)
121
+ ) {
122
+ return 1
123
+ }
124
+ }
125
+
126
+ return 2
127
+ }
128
+
129
+ function isZeroWidth(codePoint: number): boolean {
130
+ // Fast path for common printable range
131
+ if (codePoint >= 0x20 && codePoint < 0x7f) return false
132
+ if (codePoint >= 0xa0 && codePoint < 0x0300) return codePoint === 0x00ad
133
+
134
+ // Control characters
135
+ if (codePoint <= 0x1f || (codePoint >= 0x7f && codePoint <= 0x9f)) return true
136
+
137
+ // Zero-width and invisible characters
138
+ if (
139
+ (codePoint >= 0x200b && codePoint <= 0x200d) || // ZW space/joiner
140
+ codePoint === 0xfeff || // BOM
141
+ (codePoint >= 0x2060 && codePoint <= 0x2064) // Word joiner etc.
142
+ ) {
143
+ return true
144
+ }
145
+
146
+ // Variation selectors
147
+ if (
148
+ (codePoint >= 0xfe00 && codePoint <= 0xfe0f) ||
149
+ (codePoint >= 0xe0100 && codePoint <= 0xe01ef)
150
+ ) {
151
+ return true
152
+ }
153
+
154
+ // Combining diacritical marks
155
+ if (
156
+ (codePoint >= 0x0300 && codePoint <= 0x036f) ||
157
+ (codePoint >= 0x1ab0 && codePoint <= 0x1aff) ||
158
+ (codePoint >= 0x1dc0 && codePoint <= 0x1dff) ||
159
+ (codePoint >= 0x20d0 && codePoint <= 0x20ff) ||
160
+ (codePoint >= 0xfe20 && codePoint <= 0xfe2f)
161
+ ) {
162
+ return true
163
+ }
164
+
165
+ // Indic script combining marks (covers Devanagari through Malayalam)
166
+ if (codePoint >= 0x0900 && codePoint <= 0x0d4f) {
167
+ // Signs and vowel marks at start of each script block
168
+ const offset = codePoint & 0x7f
169
+ if (offset <= 0x03) return true // Signs at block start
170
+ if (offset >= 0x3a && offset <= 0x4f) return true // Vowel signs, virama
171
+ if (offset >= 0x51 && offset <= 0x57) return true // Stress signs
172
+ if (offset >= 0x62 && offset <= 0x63) return true // Vowel signs
173
+ }
174
+
175
+ // Thai/Lao combining marks
176
+ // Note: U+0E32 (SARA AA), U+0E33 (SARA AM), U+0EB2, U+0EB3 are spacing vowels (width 1), not combining marks
177
+ if (
178
+ codePoint === 0x0e31 || // Thai MAI HAN-AKAT
179
+ (codePoint >= 0x0e34 && codePoint <= 0x0e3a) || // Thai vowel signs (skip U+0E32, U+0E33)
180
+ (codePoint >= 0x0e47 && codePoint <= 0x0e4e) || // Thai vowel signs and marks
181
+ codePoint === 0x0eb1 || // Lao MAI KAN
182
+ (codePoint >= 0x0eb4 && codePoint <= 0x0ebc) || // Lao vowel signs (skip U+0EB2, U+0EB3)
183
+ (codePoint >= 0x0ec8 && codePoint <= 0x0ecd) // Lao tone marks
184
+ ) {
185
+ return true
186
+ }
187
+
188
+ // Arabic formatting
189
+ if (
190
+ (codePoint >= 0x0600 && codePoint <= 0x0605) ||
191
+ codePoint === 0x06dd ||
192
+ codePoint === 0x070f ||
193
+ codePoint === 0x08e2
194
+ ) {
195
+ return true
196
+ }
197
+
198
+ // Surrogates, tag characters
199
+ if (codePoint >= 0xd800 && codePoint <= 0xdfff) return true
200
+ if (codePoint >= 0xe0000 && codePoint <= 0xe007f) return true
201
+
202
+ return false
203
+ }
204
+
205
+ // Note: complex-script graphemes like Devanagari क्ष (ka+virama+ZWJ+ssa) render
206
+ // as a single ligature glyph but occupy 2 terminal cells (wcwidth sums the base
207
+ // consonants). Bun.stringWidth=2 matches terminal cell allocation, which is what
208
+ // we need for cursor positioning — the JS fallback's grapheme-cluster width of 1
209
+ // would desync Ink's layout from the terminal.
210
+ //
211
+ // Bun.stringWidth is resolved once at module scope rather than checked on every
212
+ // call — typeof guards deopt property access and this is a hot path (~100k calls/frame).
213
+ const bunStringWidth =
214
+ typeof Bun !== 'undefined' && typeof Bun.stringWidth === 'function'
215
+ ? Bun.stringWidth
216
+ : null
217
+
218
+ const BUN_STRING_WIDTH_OPTS = { ambiguousIsNarrow: true } as const
219
+
220
+ export const stringWidth: (str: string) => number = bunStringWidth
221
+ ? str => bunStringWidth(str, BUN_STRING_WIDTH_OPTS)
222
+ : stringWidthJavaScript