6bf8bebf51
CI / Test and Build (push) Failing after 1s
CI / Migrate Dev DB (push) Has been skipped
CI / Migrate DB (push) Has been skipped
CodeQL / Analyze actions (push) Has been cancelled
CodeQL / Analyze javascript-typescript (push) Has been cancelled
CI / Detect Version (push) Has been cancelled
CI / Detect Desktop Changes (push) Has been cancelled
CI / Build AMD64 (blacksmith-2vcpu-ubuntu-2404, ./docker/cron.Dockerfile, ubuntu-latest, ghcr.io/simstudioai/cron) (push) Has been cancelled
CI / Build AMD64 (blacksmith-2vcpu-ubuntu-2404, ./docker/db.Dockerfile, ECR_MIGRATIONS, ubuntu-latest, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build AMD64 (blacksmith-4vcpu-ubuntu-2404, ./docker/pii.Dockerfile, ECR_PII, ubuntu-latest, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build AMD64 (blacksmith-4vcpu-ubuntu-2404, ./docker/realtime.Dockerfile, ECR_REALTIME, ubuntu-latest, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Build AMD64 (blacksmith-8vcpu-ubuntu-2404, ./docker/app.Dockerfile, ECR_APP, linux-x64-8-core, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (blacksmith-4vcpu-ubuntu-2404-arm, ./docker/cron.Dockerfile, ubuntu-24.04-arm, ghcr.io/simstudioai/cron) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (blacksmith-4vcpu-ubuntu-2404-arm, ./docker/db.Dockerfile, ubuntu-24.04-arm, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (blacksmith-4vcpu-ubuntu-2404-arm, ./docker/pii.Dockerfile, ubuntu-24.04-arm, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (blacksmith-4vcpu-ubuntu-2404-arm, ./docker/realtime.Dockerfile, ubuntu-24.04-arm, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (blacksmith-8vcpu-ubuntu-2404-arm, ./docker/app.Dockerfile, linux-arm64-8-core, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Check Docs Changes (push) Has been cancelled
Publish CLI Package / publish-npm (push) Has been cancelled
Publish Python SDK / publish-pypi (push) Has been cancelled
CI / Deploy Trigger.dev (Dev) (push) Has been cancelled
Helm Chart / Lint, test, and validate chart (push) Has been cancelled
Helm Chart / Chart version bumped (push) Has been cancelled
Publish TypeScript SDK / publish-npm (push) Has been cancelled
CI / Build Dev ECR (blacksmith-8vcpu-ubuntu-2404, ./docker/app.Dockerfile, ECR_APP, linux-x64-8-core) (push) Has been cancelled
CI / Promote Images (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/cron) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Build Dev ECR (blacksmith-2vcpu-ubuntu-2404, ./docker/db.Dockerfile, ECR_MIGRATIONS, ubuntu-latest) (push) Has been cancelled
CI / Build Dev ECR (blacksmith-4vcpu-ubuntu-2404, ./docker/pii.Dockerfile, ECR_PII, ubuntu-latest) (push) Has been cancelled
CI / Build Dev ECR (blacksmith-4vcpu-ubuntu-2404, ./docker/realtime.Dockerfile, ECR_REALTIME, ubuntu-latest) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Process Docs (push) Has been cancelled
CI / Create GitHub Release (push) Has been cancelled
CI / Check Desktop Signing Secrets (push) Has been cancelled
CI / Desktop Release (push) Has been cancelled
CI / Create Desktop Prerelease (push) Has been cancelled
CI / Desktop Prerelease Build (push) Has been cancelled
CI / Publish Desktop Prerelease (push) Has been cancelled
CI / Prune Desktop Prereleases (push) Has been cancelled
Helm Chart / Install on kind and run helm test (push) Has been cancelled
158 lines
4.5 KiB
TypeScript
158 lines
4.5 KiB
TypeScript
/**
|
|
* @vitest-environment node
|
|
*/
|
|
import type OpenAI from 'openai'
|
|
import { describe, expect, it } from 'vitest'
|
|
import { ResolvedSecretTraceRegistry } from '@/executor/utils/resolved-secret-trace-registry'
|
|
import {
|
|
buildResponsesInputFromMessages,
|
|
parseResponsesUsage,
|
|
toOpenAIModelUsage,
|
|
} from '@/providers/openai/utils'
|
|
import { runWithProviderRuntimeContext } from '@/providers/runtime-context'
|
|
|
|
describe('parseResponsesUsage', () => {
|
|
it('reads cache writes, which GPT-5.6+ bills at a premium', () => {
|
|
const usage = parseResponsesUsage({
|
|
input_tokens: 1000,
|
|
output_tokens: 100,
|
|
total_tokens: 1100,
|
|
input_tokens_details: { cached_tokens: 600, cache_write_tokens: 200 },
|
|
output_tokens_details: { reasoning_tokens: 0 },
|
|
} as OpenAI.Responses.ResponseUsage)
|
|
|
|
expect(usage).toMatchObject({ promptTokens: 1000, cachedTokens: 600, cacheWriteTokens: 200 })
|
|
})
|
|
|
|
it('reports zero cache writes on model families that do not charge for them', () => {
|
|
const usage = parseResponsesUsage({
|
|
input_tokens: 1000,
|
|
output_tokens: 100,
|
|
total_tokens: 1100,
|
|
input_tokens_details: { cached_tokens: 600 },
|
|
output_tokens_details: { reasoning_tokens: 0 },
|
|
} as OpenAI.Responses.ResponseUsage)
|
|
|
|
expect(usage?.cacheWriteTokens).toBe(0)
|
|
})
|
|
})
|
|
|
|
describe('toOpenAIModelUsage', () => {
|
|
/**
|
|
* OpenAI reports cached and written tokens as subsets of `input_tokens`;
|
|
* double-counting them would over-bill every cached request.
|
|
*/
|
|
it('subtracts cache buckets out of the prompt total', () => {
|
|
const usage = toOpenAIModelUsage({
|
|
promptTokens: 1000,
|
|
completionTokens: 100,
|
|
totalTokens: 1100,
|
|
cachedTokens: 600,
|
|
cacheWriteTokens: 200,
|
|
reasoningTokens: 0,
|
|
})
|
|
|
|
expect(usage).toEqual({
|
|
input: 200,
|
|
output: 100,
|
|
cacheRead: 600,
|
|
cacheWrites: [{ tokens: 200, inputRateMultiplier: 1.25 }],
|
|
})
|
|
})
|
|
|
|
/**
|
|
* OpenAI has shipped payloads where reads plus writes exceeded the prompt
|
|
* total. Left unclamped that bills more input than the request contained.
|
|
*/
|
|
it('never bills more cache tokens than the request reported', () => {
|
|
const usage = toOpenAIModelUsage({
|
|
promptTokens: 4583,
|
|
completionTokens: 15,
|
|
totalTokens: 4598,
|
|
cachedTokens: 3945,
|
|
cacheWriteTokens: 4580,
|
|
reasoningTokens: 0,
|
|
})
|
|
|
|
expect(usage.input).toBe(0)
|
|
expect(usage.cacheRead).toBe(3945)
|
|
expect(usage.cacheWrites?.[0].tokens).toBe(4583 - 3945)
|
|
})
|
|
})
|
|
|
|
describe('buildResponsesInputFromMessages', () => {
|
|
it('should convert user message files to Responses multipart content', () => {
|
|
const input = buildResponsesInputFromMessages([
|
|
{
|
|
role: 'user',
|
|
content: 'Analyze this image',
|
|
files: [
|
|
{
|
|
id: 'file-1',
|
|
key: 'workspace/ws-1/example.png',
|
|
name: 'example.png',
|
|
url: '/api/files/serve/workspace%2Fws-1%2Fexample.png?context=workspace',
|
|
size: 128,
|
|
type: 'image/png',
|
|
base64: 'iVBORw0KGgo=',
|
|
},
|
|
],
|
|
},
|
|
])
|
|
|
|
expect(input).toEqual([
|
|
{
|
|
role: 'user',
|
|
content: [
|
|
{ type: 'input_text', text: 'Analyze this image' },
|
|
{
|
|
type: 'input_image',
|
|
image_url: 'data:image/png;base64,iVBORw0KGgo=',
|
|
detail: 'auto',
|
|
},
|
|
],
|
|
},
|
|
])
|
|
})
|
|
|
|
it('preserves an ordinary document filename that collides with a configured secret', () => {
|
|
const registry = new ResolvedSecretTraceRegistry([
|
|
{ name: 'FILE_NAME', plaintext: 'report.pdf', encryptedValue: 'ciphertext' },
|
|
])
|
|
|
|
const input = runWithProviderRuntimeContext({ resolvedSecretTraceRegistry: registry }, () =>
|
|
buildResponsesInputFromMessages([
|
|
{
|
|
role: 'user',
|
|
content: 'Analyze this document',
|
|
files: [
|
|
{
|
|
id: 'file-1',
|
|
key: 'workspace/ws-1/report.pdf',
|
|
name: 'report.pdf',
|
|
url: '/api/files/serve/workspace%2Fws-1%2Freport.pdf?context=workspace',
|
|
size: 128,
|
|
type: 'application/octet-stream',
|
|
base64: 'cGRm',
|
|
},
|
|
],
|
|
},
|
|
])
|
|
)
|
|
|
|
expect(input).toEqual([
|
|
{
|
|
role: 'user',
|
|
content: [
|
|
{ type: 'input_text', text: 'Analyze this document' },
|
|
{
|
|
type: 'input_file',
|
|
filename: 'report.pdf',
|
|
file_data: 'data:application/pdf;base64,cGRm',
|
|
},
|
|
],
|
|
},
|
|
])
|
|
})
|
|
})
|