Skip to content

Commit 2aea693

Browse files
committed
Format pasted text token counts using display token count utility
- should use `packages/shared/src/utils/display-token-count.ts` for 'Pasted X tokens' symbol ├── apps │ └── editor │ └── src │ └── utils │ └── build-user-content.ts └── packages ├── shared │ └── src │ └── utils │ └── display-token-count.ts └── ui └── src └── components └── editor └── common └── prompts ├── PromptField │ └── hooks │ └── use-handlers │ └── handle-input-click.ts └── shared └── symbols ├── get-highlighted-text.tsx ├── index.ts ├── position-mapping.ts ├── prompt-symbols.module.scss ├── reconstruct-raw-value.ts └── use-symbol-deletion.ts
1 parent 88a16b6 commit 2aea693

3 files changed

Lines changed: 18 additions & 11 deletions

File tree

‎packages/ui/src/components/editor/common/prompts/shared/symbols/get-highlighted-text.tsx‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import cn from 'classnames'
22
import styles from './prompt-symbols.module.scss'
33
import pfStyles from '../../PromptField/PromptField.module.scss'
44
import { SelectionState } from '../../PromptField'
5+
import { display_token_count } from '@shared/utils/display-token-count'
56

67
const escape_html = (str: string): string => {
78
if (!str) return ''
@@ -256,12 +257,13 @@ export const get_highlighted_text = (params: {
256257
)
257258
if (part && pasted_text_match) {
258259
const hash = pasted_text_match[1]
259-
const token_count = pasted_text_match[2]
260-
const label = `Pasted ${token_count} tokens`
260+
const token_count_str = pasted_text_match[2]
261+
const token_count = parseInt(token_count_str)
262+
const label = `Pasted ${display_token_count(token_count)} tokens`
261263
return `<span class="${cn(
262264
styles['symbol'],
263265
styles['symbol--pasted-text']
264-
)}" data-type="pasted-text-symbol" data-hash="${hash}" data-token-count="${token_count}"><span class="${
266+
)}" data-type="pasted-text-symbol" data-hash="${hash}" data-token-count="${token_count_str}"><span class="${
265267
styles['symbol__icon']
266268
}" data-role="symbol-icon"></span><span class="${
267269
styles['symbol__text']

‎packages/ui/src/components/editor/common/prompts/shared/symbols/position-mapping.ts‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { display_token_count } from '@shared/utils/display-token-count'
2+
13
export const map_display_pos_to_raw_pos = (params: {
24
display_pos: number
35
raw_text: string
@@ -70,9 +72,9 @@ export const map_display_pos_to_raw_pos = (params: {
7072
const match = pasted_text_symbol.match(
7173
/^#PastedText\(([a-fA-F0-9]+):(\d+)\)$/
7274
)
73-
const token_count = match ? match[2] : null
75+
const token_count = match ? parseInt(match[2]) : null
7476
display_match_length = token_count
75-
? `Pasted ${token_count} tokens`.length
77+
? `Pasted ${display_token_count(token_count)} tokens`.length
7678
: 0
7779
is_replacement_match = true
7880
} else if (website_symbol) {
@@ -194,9 +196,9 @@ export const map_raw_pos_to_display_pos = (params: {
194196
const match = pasted_text_symbol.match(
195197
/^#PastedText\(([a-fA-F0-9]+):(\d+)\)$/
196198
)
197-
const token_count = match ? match[2] : null
199+
const token_count = match ? parseInt(match[2]) : null
198200
display_match_length = token_count
199-
? `Pasted ${token_count} tokens`.length
201+
? `Pasted ${display_token_count(token_count)} tokens`.length
200202
: 0
201203
is_replacement_match = true
202204
} else if (website_symbol) {

‎packages/ui/src/components/editor/common/prompts/shared/symbols/reconstruct-raw-value.ts‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { display_token_count } from '@shared/utils/display-token-count'
2+
13
export const reconstruct_raw_value_from_node = (node: Node): string => {
24
if (node.nodeType == Node.TEXT_NODE) {
35
return node.textContent || ''
@@ -126,15 +128,16 @@ export const reconstruct_raw_value_from_node = (node: Node): string => {
126128
}
127129
} else if (el.dataset.type == 'pasted-text-symbol') {
128130
const hash = el.dataset.hash
129-
const token_count = el.dataset.tokenCount
130-
if (!hash || !token_count) return ''
131+
const token_count_str = el.dataset.tokenCount
132+
if (!hash || !token_count_str) return ''
131133

132-
const expected_text = `Pasted ${token_count} tokens`
134+
const token_count = parseInt(token_count_str)
135+
const expected_text = `Pasted ${display_token_count(token_count)} tokens`
133136
const index = inner_content.indexOf(expected_text)
134137
if (index != -1) {
135138
const prefix = inner_content.substring(0, index)
136139
const suffix = inner_content.substring(index + expected_text.length)
137-
return `${prefix}#PastedText(${hash}:${token_count})${suffix}`
140+
return `${prefix}#PastedText(${hash}:${token_count_str})${suffix}`
138141
}
139142
} else if (el.dataset.type == 'website-symbol') {
140143
const url = el.dataset.url

0 commit comments

Comments
 (0)