tinker-agent 1.0.65 → 1.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 (203) hide show
  1. package/README.md +66 -7
  2. package/node_modules/ansi-regex/index.d.ts +33 -0
  3. package/node_modules/ansi-regex/index.js +14 -0
  4. package/node_modules/ansi-regex/license +9 -0
  5. package/node_modules/ansi-regex/package.json +61 -0
  6. package/node_modules/ansi-regex/readme.md +66 -0
  7. package/node_modules/ansi-styles/index.d.ts +236 -0
  8. package/node_modules/ansi-styles/index.js +223 -0
  9. package/node_modules/ansi-styles/license +9 -0
  10. package/node_modules/ansi-styles/package.json +54 -0
  11. package/node_modules/ansi-styles/readme.md +173 -0
  12. package/node_modules/chalk/license +9 -0
  13. package/node_modules/chalk/package.json +83 -0
  14. package/node_modules/chalk/readme.md +297 -0
  15. package/node_modules/chalk/source/index.d.ts +325 -0
  16. package/node_modules/chalk/source/index.js +225 -0
  17. package/node_modules/chalk/source/utilities.js +33 -0
  18. package/node_modules/chalk/source/vendor/ansi-styles/index.d.ts +236 -0
  19. package/node_modules/chalk/source/vendor/ansi-styles/index.js +223 -0
  20. package/node_modules/chalk/source/vendor/supports-color/browser.d.ts +1 -0
  21. package/node_modules/chalk/source/vendor/supports-color/browser.js +34 -0
  22. package/node_modules/chalk/source/vendor/supports-color/index.d.ts +55 -0
  23. package/node_modules/chalk/source/vendor/supports-color/index.js +190 -0
  24. package/node_modules/character-entities/index.d.ts +6 -0
  25. package/node_modules/character-entities/index.js +2132 -0
  26. package/node_modules/character-entities/license +22 -0
  27. package/node_modules/character-entities/package.json +78 -0
  28. package/node_modules/character-entities/readme.md +152 -0
  29. package/node_modules/decode-named-character-reference/index.d.ts +13 -0
  30. package/node_modules/decode-named-character-reference/index.d.ts.map +1 -0
  31. package/node_modules/decode-named-character-reference/index.dom.d.ts +6 -0
  32. package/node_modules/decode-named-character-reference/index.dom.d.ts.map +1 -0
  33. package/node_modules/decode-named-character-reference/index.dom.js +32 -0
  34. package/node_modules/decode-named-character-reference/index.js +19 -0
  35. package/node_modules/decode-named-character-reference/license +22 -0
  36. package/node_modules/decode-named-character-reference/package.json +90 -0
  37. package/node_modules/decode-named-character-reference/readme.md +136 -0
  38. package/node_modules/get-east-asian-width/index.d.ts +60 -0
  39. package/node_modules/get-east-asian-width/index.js +30 -0
  40. package/node_modules/get-east-asian-width/license +9 -0
  41. package/node_modules/get-east-asian-width/lookup-data.js +21 -0
  42. package/node_modules/get-east-asian-width/lookup.js +138 -0
  43. package/node_modules/get-east-asian-width/package.json +71 -0
  44. package/node_modules/get-east-asian-width/readme.md +65 -0
  45. package/node_modules/get-east-asian-width/utilities.js +24 -0
  46. package/node_modules/has-flag/index.d.ts +39 -0
  47. package/node_modules/has-flag/index.js +8 -0
  48. package/node_modules/has-flag/license +9 -0
  49. package/node_modules/has-flag/package.json +49 -0
  50. package/node_modules/has-flag/readme.md +74 -0
  51. package/node_modules/is-fullwidth-code-point/index.d.ts +17 -0
  52. package/node_modules/is-fullwidth-code-point/index.js +12 -0
  53. package/node_modules/is-fullwidth-code-point/license +9 -0
  54. package/node_modules/is-fullwidth-code-point/package.json +53 -0
  55. package/node_modules/is-fullwidth-code-point/readme.md +31 -0
  56. package/node_modules/markdansi/LICENSE +21 -0
  57. package/node_modules/markdansi/README.md +167 -0
  58. package/node_modules/markdansi/dist/ast.d.ts +99 -0
  59. package/node_modules/markdansi/dist/ast.js +1 -0
  60. package/node_modules/markdansi/dist/cli.d.ts +17 -0
  61. package/node_modules/markdansi/dist/cli.js +208 -0
  62. package/node_modules/markdansi/dist/hyperlink.d.ts +9 -0
  63. package/node_modules/markdansi/dist/hyperlink.js +26 -0
  64. package/node_modules/markdansi/dist/index.d.ts +10 -0
  65. package/node_modules/markdansi/dist/index.js +16 -0
  66. package/node_modules/markdansi/dist/parser.d.ts +2 -0
  67. package/node_modules/markdansi/dist/parser.js +205 -0
  68. package/node_modules/markdansi/dist/render.d.ts +9 -0
  69. package/node_modules/markdansi/dist/render.js +680 -0
  70. package/node_modules/markdansi/dist/stream.d.ts +39 -0
  71. package/node_modules/markdansi/dist/stream.js +192 -0
  72. package/node_modules/markdansi/dist/theme.d.ts +18 -0
  73. package/node_modules/markdansi/dist/theme.js +105 -0
  74. package/node_modules/markdansi/dist/types.d.ts +58 -0
  75. package/node_modules/markdansi/dist/types.js +1 -0
  76. package/node_modules/markdansi/dist/wrap.d.ts +10 -0
  77. package/node_modules/markdansi/dist/wrap.js +73 -0
  78. package/node_modules/markdansi/docs/spec.md +110 -0
  79. package/node_modules/markdansi/package.json +78 -0
  80. package/node_modules/markdansi/tsconfig.json +18 -0
  81. package/node_modules/marked/LICENSE +44 -0
  82. package/node_modules/marked/README.md +115 -0
  83. package/node_modules/marked/bin/main.js +284 -0
  84. package/node_modules/marked/bin/marked.js +16 -0
  85. package/node_modules/marked/lib/marked.d.ts +760 -0
  86. package/node_modules/marked/lib/marked.esm.js +77 -0
  87. package/node_modules/marked/lib/marked.esm.js.map +7 -0
  88. package/node_modules/marked/lib/marked.umd.js +79 -0
  89. package/node_modules/marked/lib/marked.umd.js.map +7 -0
  90. package/node_modules/marked/man/marked.1 +113 -0
  91. package/node_modules/marked/man/marked.1.md +93 -0
  92. package/node_modules/marked/package.json +103 -0
  93. package/node_modules/slice-ansi/index.d.ts +19 -0
  94. package/node_modules/slice-ansi/index.js +317 -0
  95. package/node_modules/slice-ansi/license +10 -0
  96. package/node_modules/slice-ansi/package.json +59 -0
  97. package/node_modules/slice-ansi/readme.md +55 -0
  98. package/node_modules/slice-ansi/tokenize-ansi.js +752 -0
  99. package/node_modules/string-width/index.d.ts +39 -0
  100. package/node_modules/string-width/index.js +203 -0
  101. package/node_modules/string-width/license +9 -0
  102. package/node_modules/string-width/package.json +65 -0
  103. package/node_modules/string-width/readme.md +66 -0
  104. package/node_modules/strip-ansi/index.d.ts +15 -0
  105. package/node_modules/strip-ansi/index.js +19 -0
  106. package/node_modules/strip-ansi/license +9 -0
  107. package/node_modules/strip-ansi/package.json +59 -0
  108. package/node_modules/strip-ansi/readme.md +37 -0
  109. package/node_modules/supports-color/browser.d.ts +1 -0
  110. package/node_modules/supports-color/browser.js +35 -0
  111. package/node_modules/supports-color/index.d.ts +55 -0
  112. package/node_modules/supports-color/index.js +202 -0
  113. package/node_modules/supports-color/license +9 -0
  114. package/node_modules/supports-color/package.json +64 -0
  115. package/node_modules/supports-color/readme.md +75 -0
  116. package/node_modules/supports-hyperlinks/browser.js +10 -0
  117. package/node_modules/supports-hyperlinks/index.d.ts +25 -0
  118. package/node_modules/supports-hyperlinks/index.js +163 -0
  119. package/node_modules/supports-hyperlinks/license +10 -0
  120. package/node_modules/supports-hyperlinks/package.json +47 -0
  121. package/node_modules/supports-hyperlinks/readme.md +45 -0
  122. package/package.json +32 -8
  123. package/patches/markdansi@0.3.2.patch +22 -2
  124. package/src/agent/context-builder.ts +24 -10
  125. package/src/agent/context-meter.ts +89 -5
  126. package/src/agent/loop.ts +107 -10
  127. package/src/agent/runtime-session.ts +1437 -82
  128. package/src/agent/session-ledger.ts +169 -29
  129. package/src/agent/types.ts +12 -1
  130. package/src/agent/user-prompt-projection.ts +62 -0
  131. package/src/cli/config.ts +57 -14
  132. package/src/cli/model-profiles.ts +124 -0
  133. package/src/cli/run-runner.ts +4 -1
  134. package/src/cli/tui-runner.tsx +68 -30
  135. package/src/context/compiled-context-hash.ts +1 -1
  136. package/src/context/compiled-context-validator.ts +92 -15
  137. package/src/context/context-automation-policy.ts +121 -0
  138. package/src/context/context-manager.ts +390 -11
  139. package/src/context/context-policy.ts +8 -0
  140. package/src/context/context-protocol-validator.ts +11 -1
  141. package/src/context/context-revision-compiler.ts +190 -27
  142. package/src/context/context-revision.ts +105 -19
  143. package/src/context/context-surface.ts +329 -0
  144. package/src/context/prefix-retirement-planner.ts +570 -0
  145. package/src/context/protocol-frame.ts +30 -3
  146. package/src/context/recall-retirement-contract.ts +27 -0
  147. package/src/context/swap-planner.ts +34 -10
  148. package/src/events/observation-text-log.ts +3 -3
  149. package/src/events/stdout-event-printer.ts +39 -3
  150. package/src/events/types.ts +172 -35
  151. package/src/ids/runtime-id.ts +6 -0
  152. package/src/ids/uuid-v7.ts +7 -0
  153. package/src/image/image-asset-store.ts +319 -0
  154. package/src/image/image-input-policy.ts +14 -0
  155. package/src/image/image-probe.ts +233 -0
  156. package/src/image/image-types.ts +262 -0
  157. package/src/instructions/project-instructions.ts +4 -1
  158. package/src/mcp/mcp-manager.ts +83 -19
  159. package/src/model/fake-model-client.ts +9 -1
  160. package/src/model/input-token-estimator.ts +25 -0
  161. package/src/model/model-client.ts +60 -0
  162. package/src/model/model-request-preflight.ts +1 -0
  163. package/src/model/moonshot-input-token-estimator.ts +107 -0
  164. package/src/model/openai-chat-mapping.ts +77 -1
  165. package/src/model/openai-chat-model-client.ts +389 -59
  166. package/src/model/openai-chat-stream.ts +272 -0
  167. package/src/model/prompt-prefix-hash.ts +2 -4
  168. package/src/model/token-estimator.ts +4 -0
  169. package/src/observation/observation-builder.ts +29 -0
  170. package/src/session/resume-projection.ts +94 -34
  171. package/src/session/session-catalog.ts +4 -4
  172. package/src/session/session-errors.ts +1 -1
  173. package/src/session/session-history-reader.ts +151 -4
  174. package/src/session/session-last-response-reader.ts +189 -0
  175. package/src/session/session-schema.ts +349 -35
  176. package/src/session/session-store.ts +3266 -255
  177. package/src/session/sqlite-session-ledger.ts +9 -4
  178. package/src/skills/skill-catalog.ts +182 -0
  179. package/src/skills/skill-context.ts +364 -0
  180. package/src/skills/skill-loader.ts +779 -0
  181. package/src/skills/skill-tool.ts +297 -0
  182. package/src/tools/edit.ts +51 -64
  183. package/src/tools/ensure-parent-directory.ts +6 -0
  184. package/src/tools/read.ts +2 -2
  185. package/src/tools/recall.ts +43 -40
  186. package/src/tools/registry.ts +24 -3
  187. package/src/tools/types.ts +43 -4
  188. package/src/tools/write.ts +20 -31
  189. package/src/tui/app.tsx +333 -96
  190. package/src/tui/clipboard.ts +5 -0
  191. package/src/tui/components/mcp-panel.tsx +28 -0
  192. package/src/tui/components/prompt-input.tsx +790 -257
  193. package/src/tui/components/skills-panel.tsx +31 -0
  194. package/src/tui/components/timeline.tsx +30 -1
  195. package/src/tui/event-store.ts +85 -2
  196. package/src/tui/file-mention.ts +234 -0
  197. package/src/tui/line-editor.ts +1 -0
  198. package/src/tui/project-slash-commands.ts +229 -0
  199. package/src/tui/prompt-draft.ts +510 -0
  200. package/src/tui/prompt-history.ts +294 -22
  201. package/src/tui/slash-commands.ts +46 -2
  202. package/src/tui/tui-session-controller.ts +110 -48
  203. package/src/tui/workspace-file-search.ts +94 -0
package/README.md CHANGED
@@ -16,6 +16,9 @@ Built with [Bun](https://bun.sh) + TypeScript ESM, powered by [Ink](https://gith
16
16
  - `WebSearch` — Search the web via Exa API
17
17
  - `WebFetch` — Fetch and refine web page content (local, browser, or Exa backend)
18
18
  - `Recall` — Search or retrieve model-visible history from the current session
19
+ - **Agent Skills**: Discover compatible `SKILL.md` packages at project and user
20
+ scope, disclose their catalog progressively, and keep activated instructions
21
+ durable across context compaction and session resume.
19
22
  - **MCP integration**: Connect external [Model Context Protocol](https://modelcontextprotocol.io) servers — their tools are dynamically registered as `mcp__<server>__<tool>`.
20
23
  - **Session persistence**: Sessions are persisted via SQLite, supporting session resume, history recall, and a catalog to browse and switch between sessions.
21
24
  - **Observation system**: Tool execution results are formatted into structured text that the model sees, with separate raw results for event logs and TUI display.
@@ -27,30 +30,58 @@ Built with [Bun](https://bun.sh) + TypeScript ESM, powered by [Ink](https://gith
27
30
  ## Quick Start
28
31
 
29
32
  ```bash
30
- # Clone and install
31
- git clone <repo>
32
- cd tinker
33
- bun install
33
+ # Bun 1.3 or later is required: https://bun.sh
34
+ npm install --global tinker-agent
34
35
 
35
36
  # Start the interactive TUI
36
- bun run tinker
37
+ tinker
37
38
 
38
39
  # Run a one-shot prompt
39
- bun run tinker run "explain the project structure"
40
+ tinker run "explain the project structure"
40
41
  ```
41
42
 
43
+ To run Tinker from a source checkout instead, use `bun install` followed by
44
+ `bun run tinker`.
45
+
42
46
  ### Slash Commands
43
47
 
44
48
  - `/view <path>` — Open a readable UTF-8 text file in a full-window viewer. Relative
45
49
  paths must remain inside the workspace; absolute paths may point outside it. Use
46
50
  the keyboard or mouse wheel to scroll and press `Esc` to close the viewer.
51
+ - `/copy` — Copy the current session's last completed assistant response to the
52
+ system clipboard as raw Markdown.
47
53
  - `/status` — Show session and context details.
48
- - `/compact` — Deterministically compact eligible historical tool output while the session is idle.
54
+ - `/skills` — Show available skills, their scope and active state, plus user skills
55
+ shadowed by a project skill.
56
+ - `/compact` — Deterministically compact eligible historical tool output while the
57
+ session is idle.
58
+ - `/compact retire` — Retire a complete cold history prefix from the active request;
59
+ the original history remains available through `Recall`.
49
60
  - `/model [profile-name]` — Choose a model profile for a new session.
50
61
  - `/resume [session-id]` — Choose or directly resume a stored session.
51
62
  - `/session delete <session-id> --confirm` — Delete a stored session.
52
63
  - `/quit` — Exit the TUI.
53
64
 
65
+ ### Agent Skills
66
+
67
+ Tinker scans `<workspace>/.agents/skills/` and `~/.agents/skills/` once when a new
68
+ or resumed runtime starts. Each direct child skill must contain a strictly valid
69
+ `SKILL.md`; an invalid discovered skill stops activation with a clear error. A
70
+ project skill wins when both scopes define the same name.
71
+
72
+ Only skill names and descriptions are initially exposed to the model. When the
73
+ model selects a matching skill through the conditional `Skill` tool, Tinker sends
74
+ the complete snapshotted `SKILL.md` and a bounded listing of its standard resource
75
+ directories. Relative resource paths are resolved from that skill's directory and
76
+ are read or executed only through Tinker's existing tools. Activated instructions
77
+ remain in the current system surface across later turns and compaction; resume
78
+ rebinds them to the current validated files.
79
+
80
+ Skill content is not local-only after activation: it is sent to the configured
81
+ model provider and persisted in the private session SQLite database, event log,
82
+ and observation log, just like other model-visible file content. `/skills` is
83
+ read-only and does not rescan, install, activate, or remove skills.
84
+
54
85
  ## Configuration
55
86
 
56
87
  Tinker is configured via environment variables:
@@ -117,6 +148,33 @@ longer present or its runtime contract has changed. Older sessions without a
117
148
  stored profile name can resume only when their model name uniquely matches one
118
149
  configured profile.
119
150
 
151
+ ### Project Custom Slash Commands
152
+
153
+ The TUI loads optional project-scoped prompt aliases from `.tinker.json` in the
154
+ workspace root:
155
+
156
+ ```json
157
+ {
158
+ "version": 1,
159
+ "slashCommands": [
160
+ {
161
+ "name": "git-commit-and-push",
162
+ "description": "Commit and push workspace changes",
163
+ "prompt": "Please inspect the workspace changes, create an appropriate commit, and push it."
164
+ }
165
+ ]
166
+ }
167
+ ```
168
+
169
+ Enter `/git-commit-and-push` to submit its configured prompt as an ordinary user
170
+ turn. Built-in slash commands appear first in suggestions, followed by project
171
+ commands in configuration order. Custom commands accept no arguments and cannot
172
+ override built-ins. The optional configuration is loaded once at TUI startup,
173
+ must be valid when present, and has a 1 MiB size limit. It is not loaded by the
174
+ one-shot `tinker run` command. See
175
+ [`docs/project-custom-slash-commands-design.md`](docs/project-custom-slash-commands-design.md)
176
+ for the full contract.
177
+
120
178
  `Read` has a fixed 262144-byte (256 KiB) content limit per call. A successful
121
179
  call always returns the complete requested line range. Use `offset` and `limit`
122
180
  to page through larger files; oversized requests fail instead of returning
@@ -146,6 +204,7 @@ tinker/
146
204
  │ ├── model/ # Model clients (OpenAI-compatible, fake), chat mapping, preflight
147
205
  │ ├── mcp/ # MCP server management, tool executor adapter
148
206
  │ ├── observation/ # Tool result → model-visible text
207
+ │ ├── skills/ # Agent Skills discovery, catalog, activation, and context
149
208
  │ ├── session/ # SQLite session store, catalog, history reader, resume
150
209
  │ ├── events/ # Event sinks, JSONL log, stdout printer
151
210
  │ ├── tui/ # Ink/React UI components, projection store, session controller
@@ -0,0 +1,33 @@
1
+ export type Options = {
2
+ /**
3
+ Match only the first ANSI escape.
4
+
5
+ @default false
6
+ */
7
+ readonly onlyFirst: boolean;
8
+ };
9
+
10
+ /**
11
+ Regular expression for matching ANSI escape codes.
12
+
13
+ @example
14
+ ```
15
+ import ansiRegex from 'ansi-regex';
16
+
17
+ ansiRegex().test('\u001B[4mcake\u001B[0m');
18
+ //=> true
19
+
20
+ ansiRegex().test('cake');
21
+ //=> false
22
+
23
+ '\u001B[4mcake\u001B[0m'.match(ansiRegex());
24
+ //=> ['\u001B[4m', '\u001B[0m']
25
+
26
+ '\u001B[4mcake\u001B[0m'.match(ansiRegex({onlyFirst: true}));
27
+ //=> ['\u001B[4m']
28
+
29
+ '\u001B]8;;https://github.com\u0007click\u001B]8;;\u0007'.match(ansiRegex());
30
+ //=> ['\u001B]8;;https://github.com\u0007', '\u001B]8;;\u0007']
31
+ ```
32
+ */
33
+ export default function ansiRegex(options?: Options): RegExp;
@@ -0,0 +1,14 @@
1
+ export default function ansiRegex({onlyFirst = false} = {}) {
2
+ // Valid string terminator sequences are BEL, ESC\, and 0x9c
3
+ const ST = '(?:\\u0007|\\u001B\\u005C|\\u009C)';
4
+
5
+ // OSC sequences only: ESC ] ... ST (non-greedy until the first ST)
6
+ const osc = `(?:\\u001B\\][\\s\\S]*?${ST})`;
7
+
8
+ // CSI and related: ESC/C1, optional intermediates, optional params (supports ; and :) then final byte
9
+ const csi = '[\\u001B\\u009B][[\\]()#;?]*(?:\\d{1,4}(?:[;:]\\d{0,4})*)?[\\dA-PR-TZcf-nq-uy=><~]';
10
+
11
+ const pattern = `${osc}|${csi}`;
12
+
13
+ return new RegExp(pattern, onlyFirst ? undefined : 'g');
14
+ }
@@ -0,0 +1,9 @@
1
+ MIT License
2
+
3
+ Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (https://sindresorhus.com)
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6
+
7
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8
+
9
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,61 @@
1
+ {
2
+ "name": "ansi-regex",
3
+ "version": "6.2.2",
4
+ "description": "Regular expression for matching ANSI escape codes",
5
+ "license": "MIT",
6
+ "repository": "chalk/ansi-regex",
7
+ "funding": "https://github.com/chalk/ansi-regex?sponsor=1",
8
+ "author": {
9
+ "name": "Sindre Sorhus",
10
+ "email": "sindresorhus@gmail.com",
11
+ "url": "https://sindresorhus.com"
12
+ },
13
+ "type": "module",
14
+ "exports": "./index.js",
15
+ "types": "./index.d.ts",
16
+ "sideEffects": false,
17
+ "engines": {
18
+ "node": ">=12"
19
+ },
20
+ "scripts": {
21
+ "test": "xo && ava && tsd",
22
+ "view-supported": "node fixtures/view-codes.js"
23
+ },
24
+ "files": [
25
+ "index.js",
26
+ "index.d.ts"
27
+ ],
28
+ "keywords": [
29
+ "ansi",
30
+ "styles",
31
+ "color",
32
+ "colour",
33
+ "colors",
34
+ "terminal",
35
+ "console",
36
+ "cli",
37
+ "string",
38
+ "tty",
39
+ "escape",
40
+ "formatting",
41
+ "rgb",
42
+ "256",
43
+ "shell",
44
+ "xterm",
45
+ "command-line",
46
+ "text",
47
+ "regex",
48
+ "regexp",
49
+ "re",
50
+ "match",
51
+ "test",
52
+ "find",
53
+ "pattern"
54
+ ],
55
+ "devDependencies": {
56
+ "ansi-escapes": "^5.0.0",
57
+ "ava": "^3.15.0",
58
+ "tsd": "^0.21.0",
59
+ "xo": "^0.54.2"
60
+ }
61
+ }
@@ -0,0 +1,66 @@
1
+ # ansi-regex
2
+
3
+ > Regular expression for matching [ANSI escape codes](https://en.wikipedia.org/wiki/ANSI_escape_code)
4
+
5
+ ## Install
6
+
7
+ ```sh
8
+ npm install ansi-regex
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ ```js
14
+ import ansiRegex from 'ansi-regex';
15
+
16
+ ansiRegex().test('\u001B[4mcake\u001B[0m');
17
+ //=> true
18
+
19
+ ansiRegex().test('cake');
20
+ //=> false
21
+
22
+ '\u001B[4mcake\u001B[0m'.match(ansiRegex());
23
+ //=> ['\u001B[4m', '\u001B[0m']
24
+
25
+ '\u001B[4mcake\u001B[0m'.match(ansiRegex({onlyFirst: true}));
26
+ //=> ['\u001B[4m']
27
+
28
+ '\u001B]8;;https://github.com\u0007click\u001B]8;;\u0007'.match(ansiRegex());
29
+ //=> ['\u001B]8;;https://github.com\u0007', '\u001B]8;;\u0007']
30
+ ```
31
+
32
+ ## API
33
+
34
+ ### ansiRegex(options?)
35
+
36
+ Returns a regex for matching ANSI escape codes.
37
+
38
+ #### options
39
+
40
+ Type: `object`
41
+
42
+ ##### onlyFirst
43
+
44
+ Type: `boolean`\
45
+ Default: `false` *(Matches any ANSI escape codes in a string)*
46
+
47
+ Match only the first ANSI escape.
48
+
49
+ ## Important
50
+
51
+ If you run the regex against untrusted user input in a server context, you should [give it a timeout](https://github.com/sindresorhus/super-regex).
52
+
53
+ **I do not consider [ReDoS](https://blog.yossarian.net/2022/12/28/ReDoS-vulnerabilities-and-misaligned-incentives) a valid vulnerability for this package.**
54
+
55
+ ## FAQ
56
+
57
+ ### Why do you test for codes not in the ECMA 48 standard?
58
+
59
+ Some of the codes we run as a test are codes that we acquired finding various lists of non-standard or manufacturer specific codes. We test for both standard and non-standard codes, as most of them follow the same or similar format and can be safely matched in strings without the risk of removing actual string content. There are a few non-standard control codes that do not follow the traditional format (i.e. they end in numbers) thus forcing us to exclude them from the test because we cannot reliably match them.
60
+
61
+ On the historical side, those ECMA standards were established in the early 90's whereas the VT100, for example, was designed in the mid/late 70's. At that point in time, control codes were still pretty ungoverned and engineers used them for a multitude of things, namely to activate hardware ports that may have been proprietary. Somewhere else you see a similar 'anarchy' of codes is in the x86 architecture for processors; there are a ton of "interrupts" that can mean different things on certain brands of processors, most of which have been phased out.
62
+
63
+ ## Maintainers
64
+
65
+ - [Sindre Sorhus](https://github.com/sindresorhus)
66
+ - [Josh Junon](https://github.com/qix-)
@@ -0,0 +1,236 @@
1
+ export type CSPair = { // eslint-disable-line @typescript-eslint/naming-convention
2
+ /**
3
+ The ANSI terminal control sequence for starting this style.
4
+ */
5
+ readonly open: string;
6
+
7
+ /**
8
+ The ANSI terminal control sequence for ending this style.
9
+ */
10
+ readonly close: string;
11
+ };
12
+
13
+ export type ColorBase = {
14
+ /**
15
+ The ANSI terminal control sequence for ending this color.
16
+ */
17
+ readonly close: string;
18
+
19
+ ansi(code: number): string;
20
+
21
+ ansi256(code: number): string;
22
+
23
+ ansi16m(red: number, green: number, blue: number): string;
24
+ };
25
+
26
+ export type Modifier = {
27
+ /**
28
+ Resets the current color chain.
29
+ */
30
+ readonly reset: CSPair;
31
+
32
+ /**
33
+ Make text bold.
34
+ */
35
+ readonly bold: CSPair;
36
+
37
+ /**
38
+ Emitting only a small amount of light.
39
+ */
40
+ readonly dim: CSPair;
41
+
42
+ /**
43
+ Make text italic. (Not widely supported)
44
+ */
45
+ readonly italic: CSPair;
46
+
47
+ /**
48
+ Make text underline. (Not widely supported)
49
+ */
50
+ readonly underline: CSPair;
51
+
52
+ /**
53
+ Make text overline.
54
+
55
+ Supported on VTE-based terminals, the GNOME terminal, mintty, and Git Bash.
56
+ */
57
+ readonly overline: CSPair;
58
+
59
+ /**
60
+ Inverse background and foreground colors.
61
+ */
62
+ readonly inverse: CSPair;
63
+
64
+ /**
65
+ Prints the text, but makes it invisible.
66
+ */
67
+ readonly hidden: CSPair;
68
+
69
+ /**
70
+ Puts a horizontal line through the center of the text. (Not widely supported)
71
+ */
72
+ readonly strikethrough: CSPair;
73
+ };
74
+
75
+ export type ForegroundColor = {
76
+ readonly black: CSPair;
77
+ readonly red: CSPair;
78
+ readonly green: CSPair;
79
+ readonly yellow: CSPair;
80
+ readonly blue: CSPair;
81
+ readonly cyan: CSPair;
82
+ readonly magenta: CSPair;
83
+ readonly white: CSPair;
84
+
85
+ /**
86
+ Alias for `blackBright`.
87
+ */
88
+ readonly gray: CSPair;
89
+
90
+ /**
91
+ Alias for `blackBright`.
92
+ */
93
+ readonly grey: CSPair;
94
+
95
+ readonly blackBright: CSPair;
96
+ readonly redBright: CSPair;
97
+ readonly greenBright: CSPair;
98
+ readonly yellowBright: CSPair;
99
+ readonly blueBright: CSPair;
100
+ readonly cyanBright: CSPair;
101
+ readonly magentaBright: CSPair;
102
+ readonly whiteBright: CSPair;
103
+ };
104
+
105
+ export type BackgroundColor = {
106
+ readonly bgBlack: CSPair;
107
+ readonly bgRed: CSPair;
108
+ readonly bgGreen: CSPair;
109
+ readonly bgYellow: CSPair;
110
+ readonly bgBlue: CSPair;
111
+ readonly bgCyan: CSPair;
112
+ readonly bgMagenta: CSPair;
113
+ readonly bgWhite: CSPair;
114
+
115
+ /**
116
+ Alias for `bgBlackBright`.
117
+ */
118
+ readonly bgGray: CSPair;
119
+
120
+ /**
121
+ Alias for `bgBlackBright`.
122
+ */
123
+ readonly bgGrey: CSPair;
124
+
125
+ readonly bgBlackBright: CSPair;
126
+ readonly bgRedBright: CSPair;
127
+ readonly bgGreenBright: CSPair;
128
+ readonly bgYellowBright: CSPair;
129
+ readonly bgBlueBright: CSPair;
130
+ readonly bgCyanBright: CSPair;
131
+ readonly bgMagentaBright: CSPair;
132
+ readonly bgWhiteBright: CSPair;
133
+ };
134
+
135
+ export type ConvertColor = {
136
+ /**
137
+ Convert from the RGB color space to the ANSI 256 color space.
138
+
139
+ @param red - (`0...255`)
140
+ @param green - (`0...255`)
141
+ @param blue - (`0...255`)
142
+ */
143
+ rgbToAnsi256(red: number, green: number, blue: number): number;
144
+
145
+ /**
146
+ Convert from the RGB HEX color space to the RGB color space.
147
+
148
+ @param hex - A hexadecimal string containing RGB data.
149
+ */
150
+ hexToRgb(hex: string): [red: number, green: number, blue: number];
151
+
152
+ /**
153
+ Convert from the RGB HEX color space to the ANSI 256 color space.
154
+
155
+ @param hex - A hexadecimal string containing RGB data.
156
+ */
157
+ hexToAnsi256(hex: string): number;
158
+
159
+ /**
160
+ Convert from the ANSI 256 color space to the ANSI 16 color space.
161
+
162
+ @param code - A number representing the ANSI 256 color.
163
+ */
164
+ ansi256ToAnsi(code: number): number;
165
+
166
+ /**
167
+ Convert from the RGB color space to the ANSI 16 color space.
168
+
169
+ @param red - (`0...255`)
170
+ @param green - (`0...255`)
171
+ @param blue - (`0...255`)
172
+ */
173
+ rgbToAnsi(red: number, green: number, blue: number): number;
174
+
175
+ /**
176
+ Convert from the RGB HEX color space to the ANSI 16 color space.
177
+
178
+ @param hex - A hexadecimal string containing RGB data.
179
+ */
180
+ hexToAnsi(hex: string): number;
181
+ };
182
+
183
+ /**
184
+ Basic modifier names.
185
+ */
186
+ export type ModifierName = keyof Modifier;
187
+
188
+ /**
189
+ Basic foreground color names.
190
+
191
+ [More colors here.](https://github.com/chalk/chalk/blob/main/readme.md#256-and-truecolor-color-support)
192
+ */
193
+ export type ForegroundColorName = keyof ForegroundColor;
194
+
195
+ /**
196
+ Basic background color names.
197
+
198
+ [More colors here.](https://github.com/chalk/chalk/blob/main/readme.md#256-and-truecolor-color-support)
199
+ */
200
+ export type BackgroundColorName = keyof BackgroundColor;
201
+
202
+ /**
203
+ Basic color names. The combination of foreground and background color names.
204
+
205
+ [More colors here.](https://github.com/chalk/chalk/blob/main/readme.md#256-and-truecolor-color-support)
206
+ */
207
+ export type ColorName = ForegroundColorName | BackgroundColorName;
208
+
209
+ /**
210
+ Basic modifier names.
211
+ */
212
+ export const modifierNames: readonly ModifierName[];
213
+
214
+ /**
215
+ Basic foreground color names.
216
+ */
217
+ export const foregroundColorNames: readonly ForegroundColorName[];
218
+
219
+ /**
220
+ Basic background color names.
221
+ */
222
+ export const backgroundColorNames: readonly BackgroundColorName[];
223
+
224
+ /*
225
+ Basic color names. The combination of foreground and background color names.
226
+ */
227
+ export const colorNames: readonly ColorName[];
228
+
229
+ declare const ansiStyles: {
230
+ readonly modifier: Modifier;
231
+ readonly color: ColorBase & ForegroundColor;
232
+ readonly bgColor: ColorBase & BackgroundColor;
233
+ readonly codes: ReadonlyMap<number, number>;
234
+ } & ForegroundColor & BackgroundColor & Modifier & ConvertColor;
235
+
236
+ export default ansiStyles;