refactor: narrow web image input v1

This commit is contained in:
Tianyi Cui
2026-07-29 23:18:57 +08:00
parent 8348a409aa
commit 3a4c436d83
69 changed files with 280 additions and 613 deletions

View File

@@ -25,11 +25,6 @@ const CHARS_PER_TOKEN = 4
/** Per-block structural overhead for JSON framing and type tags. */
const BLOCK_OVERHEAD = 4
/** Provider-neutral visual estimate: base cost plus one cost unit per 512px tile. */
const IMAGE_BASE_TOKENS = 85
const IMAGE_TILE_TOKENS = 170
const IMAGE_TILE_EDGE = 512
/** Role-field framing overhead added to every priced message. */
const ROLE_OVERHEAD = 4
@@ -362,12 +357,6 @@ export class TokenMeterService extends Service {
case 'reasoning':
tokens += Math.ceil(block.text.length / CHARS_PER_TOKEN) + BLOCK_OVERHEAD
break
case 'image': {
const tiles = Math.ceil(block.attachment.width / IMAGE_TILE_EDGE)
* Math.ceil(block.attachment.height / IMAGE_TILE_EDGE)
tokens += IMAGE_BASE_TOKENS + tiles * IMAGE_TILE_TOKENS + BLOCK_OVERHEAD
break
}
case 'tool-call':
tokens += Math.ceil(block.name.length / CHARS_PER_TOKEN)
+ Math.ceil(block.arguments.length / CHARS_PER_TOKEN)

View File

@@ -1,6 +1,5 @@
import { describe, expect, expectTypeOf, it } from 'vitest'
import { Context } from 'cordis'
import { AttachmentId } from '@deepseek-ai/dsh-attachment'
import { createUserMessage, CallId, createMessage } from '@deepseek-ai/dsh-llm'
import type { ContentBlock, Message, TokenUsage } from '@deepseek-ai/dsh-llm'
import SessionStore, { Session, SessionId, canonicalHeader } from '@deepseek-ai/dsh-session'
@@ -129,16 +128,6 @@ describe('TokenMeterService pricing', () => {
const blocks: ContentBlock[] = [
{ type: 'text', text: 'abcd' },
{ type: 'reasoning', text: 'ab' },
{
type: 'image',
attachment: {
attachmentId: AttachmentId(`sha256:${'a'.repeat(64)}`),
mediaType: 'image/png',
bytes: 1,
width: 1024,
height: 513,
},
},
{ type: 'tool-call', id: CallId('c'), name: 'read', arguments: '{"x":1}' },
{
type: 'tool-result',
@@ -152,7 +141,7 @@ describe('TokenMeterService pricing', () => {
role: 'assistant', content: blocks,
source: { kind: 'plugin', plugin: 'test' },
}))
expect(estimated).toBe(813)
expect(estimated).toBeGreaterThan(30)
expect(service.estimateMessage(textMessage('abcd'))).toBe(9)
})