diff options
| author | Dax Raad <[email protected]> | 2025-05-28 13:57:02 -0400 |
|---|---|---|
| committer | Dax Raad <[email protected]> | 2025-05-28 13:57:02 -0400 |
| commit | afe741b63e0569e46e5e586622eee5708f1bb3aa (patch) | |
| tree | 89884840e4621c4697c743243c08619fb5191e4f /js/src/session/session.ts | |
| parent | f3b224090cf6e719d58d9a36b3e07380aac68d31 (diff) | |
| download | opencode-afe741b63e0569e46e5e586622eee5708f1bb3aa.tar.gz opencode-afe741b63e0569e46e5e586622eee5708f1bb3aa.zip | |
add cost
Diffstat (limited to 'js/src/session/session.ts')
| -rw-r--r-- | js/src/session/session.ts | 19 |
1 files changed, 14 insertions, 5 deletions
diff --git a/js/src/session/session.ts b/js/src/session/session.ts index 06c51625f..406ab27f8 100644 --- a/js/src/session/session.ts +++ b/js/src/session/session.ts @@ -17,6 +17,7 @@ import { } from "ai"; import { z } from "zod"; import * as tools from "../tool"; +import { Decimal } from "decimal.js"; import PROMPT_ANTHROPIC from "./prompt/anthropic.txt"; import PROMPT_TITLE from "./prompt/title.txt"; @@ -31,6 +32,7 @@ export namespace Session { id: Identifier.schema("session"), shareID: z.string().optional(), title: z.string(), + cost: z.number().optional(), tokens: z.object({ input: z.number(), output: z.number(), @@ -188,7 +190,7 @@ export namespace Session { parts: input.parts, }, ]), - model, + model: model.instance, }).then((result) => { return Session.update(input.sessionID, (draft) => { draft.title = result.text; @@ -226,16 +228,23 @@ export namespace Session { const result = streamText({ onStepFinish: (step) => { update(input.sessionID, (draft) => { - draft.tokens.input += step.usage.inputTokens || 0; - draft.tokens.output += step.usage.outputTokens || 0; - draft.tokens.reasoning += step.usage.reasoningTokens || 0; + const input = step.usage.inputTokens ?? 0; + const output = step.usage.outputTokens ?? 0; + const reasoning = step.usage.reasoningTokens ?? 0; + draft.tokens.input += input; + draft.tokens.output += output; + draft.tokens.reasoning += reasoning; + draft.cost = new Decimal(draft.cost ?? 0) + .add(new Decimal(input).mul(model.info.cost.input)) + .add(new Decimal(output).mul(model.info.cost.output)) + .toNumber(); }); }, stopWhen: stepCountIs(1000), messages: convertToModelMessages(msgs), temperature: 0, tools, - model, + model: model.instance, }); msgs.push(next); |
