d25d482dc2
Publish CLI Package / publish-npm (push) Waiting to run
Publish Python SDK / publish-pypi (push) Waiting to run
Publish TypeScript SDK / publish-npm (push) Waiting to run
CI / Migrate Dev DB (push) Has been skipped
CI / Detect Version (push) Has been cancelled
CI / Migrate DB (push) Has been cancelled
CI / Build Dev ECR (./docker/app.Dockerfile, ECR_APP) (push) Has been cancelled
CI / Build Dev ECR (./docker/db.Dockerfile, ECR_MIGRATIONS) (push) Has been cancelled
CI / Build Dev ECR (./docker/pii.Dockerfile, ECR_PII) (push) Has been cancelled
CI / Build Dev ECR (./docker/realtime.Dockerfile, ECR_REALTIME) (push) Has been cancelled
CI / Deploy Trigger.dev (Dev) (push) Has been cancelled
CI / Build AMD64 (./docker/app.Dockerfile, ECR_APP, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build AMD64 (./docker/db.Dockerfile, ECR_MIGRATIONS, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build AMD64 (./docker/pii.Dockerfile, ECR_PII, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build AMD64 (./docker/realtime.Dockerfile, ECR_REALTIME, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/app.Dockerfile, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/db.Dockerfile, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/pii.Dockerfile, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/realtime.Dockerfile, ghcr.io/simstudioai/realtime) (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 / Create GHCR Manifests (ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Check Docs Changes (push) Has been cancelled
CI / Process Docs (push) Has been cancelled
CI / Create GitHub Release (push) Has been cancelled
CI / Test and Build (push) Has been cancelled
86 lines
2.6 KiB
TypeScript
86 lines
2.6 KiB
TypeScript
import type {
|
|
RawMessageDeltaEvent,
|
|
RawMessageStartEvent,
|
|
RawMessageStreamEvent,
|
|
Usage,
|
|
} from '@anthropic-ai/sdk/resources'
|
|
import { createLogger } from '@sim/logger'
|
|
import { randomFloat } from '@sim/utils/random'
|
|
import { trackForcedToolUsage } from '@/providers/utils'
|
|
|
|
const logger = createLogger('AnthropicUtils')
|
|
|
|
export interface AnthropicStreamUsage {
|
|
input_tokens: number
|
|
output_tokens: number
|
|
}
|
|
|
|
export function createReadableStreamFromAnthropicStream(
|
|
anthropicStream: AsyncIterable<RawMessageStreamEvent>,
|
|
onComplete?: (content: string, usage: AnthropicStreamUsage) => void
|
|
): ReadableStream<Uint8Array> {
|
|
let fullContent = ''
|
|
let inputTokens = 0
|
|
let outputTokens = 0
|
|
|
|
return new ReadableStream({
|
|
async start(controller) {
|
|
try {
|
|
for await (const event of anthropicStream) {
|
|
if (event.type === 'message_start') {
|
|
const startEvent = event as RawMessageStartEvent
|
|
const usage: Usage = startEvent.message.usage
|
|
inputTokens = usage.input_tokens
|
|
} else if (event.type === 'message_delta') {
|
|
const deltaEvent = event as RawMessageDeltaEvent
|
|
outputTokens = deltaEvent.usage.output_tokens
|
|
} else if (event.type === 'content_block_delta' && event.delta.type === 'text_delta') {
|
|
const text = event.delta.text
|
|
fullContent += text
|
|
controller.enqueue(new TextEncoder().encode(text))
|
|
}
|
|
}
|
|
|
|
if (onComplete) {
|
|
onComplete(fullContent, { input_tokens: inputTokens, output_tokens: outputTokens })
|
|
}
|
|
|
|
controller.close()
|
|
} catch (err) {
|
|
controller.error(err)
|
|
}
|
|
},
|
|
})
|
|
}
|
|
|
|
export function generateToolUseId(toolName: string): string {
|
|
return `${toolName}-${Date.now()}-${randomFloat().toString(36).substring(2, 7)}`
|
|
}
|
|
|
|
export function checkForForcedToolUsage(
|
|
response: any,
|
|
toolChoice: any,
|
|
forcedTools: string[],
|
|
usedForcedTools: string[]
|
|
): { hasUsedForcedTool: boolean; usedForcedTools: string[] } | null {
|
|
if (typeof toolChoice === 'object' && toolChoice !== null && Array.isArray(response.content)) {
|
|
const toolUses = response.content.filter((item: any) => item.type === 'tool_use')
|
|
|
|
if (toolUses.length > 0) {
|
|
const adaptedToolCalls = toolUses.map((tool: any) => ({ name: tool.name }))
|
|
const adaptedToolChoice =
|
|
toolChoice.type === 'tool' ? { function: { name: toolChoice.name } } : toolChoice
|
|
|
|
return trackForcedToolUsage(
|
|
adaptedToolCalls,
|
|
adaptedToolChoice,
|
|
logger,
|
|
'anthropic',
|
|
forcedTools,
|
|
usedForcedTools
|
|
)
|
|
}
|
|
}
|
|
return null
|
|
}
|