mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-16 05:01:03 +03:00
fix(core): count session generate requests in the session's cost (#63006)
This commit is contained in:
committed by
GitHub
Unverified
parent
f89d46016e
commit
df53325a12
Generated
+1
-1
@@ -1398,7 +1398,7 @@ export type ExperimentalSessionLogOutput =
|
||||
| undefined
|
||||
readonly data: {
|
||||
readonly sessionID: Session.ID
|
||||
readonly source: "title" | "compaction"
|
||||
readonly source: "title" | "compaction" | "generate"
|
||||
readonly cost: Money.USD
|
||||
readonly tokens: {
|
||||
readonly input: number
|
||||
|
||||
+1
-1
@@ -1651,7 +1651,7 @@ export type SessionUsageRecorded = {
|
||||
type: "session.usage.recorded"
|
||||
durable: { aggregateID: string; seq: EventSeq; version: 1 }
|
||||
location?: LocationRef
|
||||
data: { sessionID: SessionID; source: "title" | "compaction"; cost: MoneyUSD; tokens: TokenUsageInfo }
|
||||
data: { sessionID: SessionID; source: "title" | "compaction" | "generate"; cost: MoneyUSD; tokens: TokenUsageInfo }
|
||||
}
|
||||
|
||||
export type LocationShutdown = {
|
||||
|
||||
@@ -210,7 +210,7 @@ export interface Interface {
|
||||
readonly prompt: (
|
||||
input: Parameters<Session.Handle["prompt"]>[0] & { sessionID: SessionSchema.ID },
|
||||
) => ReturnType<Session.Handle["prompt"]>
|
||||
/** Generates text from current Session context without admitting input or mutating history. */
|
||||
/** Generates text from current Session context without admitting input or mutating history. Its usage counts toward the Session's totals. */
|
||||
readonly generate: (input: {
|
||||
sessionID: SessionSchema.ID
|
||||
prompt: string
|
||||
@@ -485,6 +485,7 @@ const layer = Layer.effect(
|
||||
Effect.provideService(Instance.Service, instances),
|
||||
Effect.provideService(Database.Service, database),
|
||||
Effect.provideService(LLMClient.Service, llm),
|
||||
Effect.provideService(Bus.Service, bus),
|
||||
)
|
||||
}),
|
||||
command: Effect.fn("Session.command")(function* (input) {
|
||||
|
||||
@@ -3,11 +3,13 @@ export * as SessionGenerate from "./generate.js"
|
||||
import type { FileSystem } from "../filesystem.js"
|
||||
import { LLMClient, Message, type AIError } from "@opencode/ai/effect"
|
||||
import { Effect } from "effect"
|
||||
import { Bus } from "../bus.js"
|
||||
import { Database } from "../database/database.js"
|
||||
import { Instance } from "../instance/service.js"
|
||||
import { Plugin } from "../plugin/service.js"
|
||||
import type { Instructions } from "../instructions/index.js"
|
||||
import { SessionContext } from "./context.js"
|
||||
import { SessionEvent } from "./event.js"
|
||||
import type { AgentNotFoundError } from "./error.js"
|
||||
import { SessionHistory } from "./history.js"
|
||||
import { SessionModelRequest } from "./model-request.js"
|
||||
@@ -25,7 +27,10 @@ export type Error =
|
||||
|
||||
export type Result = SessionUsage.Recorded & { readonly text: string }
|
||||
|
||||
/** Generates text from current Session context without mutating the Session. Usage is reported like a step's. */
|
||||
/**
|
||||
* Generates text from current Session context without mutating history.
|
||||
* Usage is reported like a step's and added to the Session's totals.
|
||||
*/
|
||||
export const generate = Effect.fn("SessionGenerate.generate")(function* (input: {
|
||||
session: SessionSchema.Info
|
||||
prompt: string
|
||||
@@ -33,6 +38,7 @@ export const generate = Effect.fn("SessionGenerate.generate")(function* (input:
|
||||
const instances = yield* Instance.Service
|
||||
const database = yield* Database.Service
|
||||
const llm = yield* LLMClient.Service
|
||||
const bus = yield* Bus.Service
|
||||
|
||||
return yield* Effect.gen(function* () {
|
||||
yield* Plugin.awaitActivation
|
||||
@@ -65,6 +71,8 @@ export const generate = Effect.fn("SessionGenerate.generate")(function* (input:
|
||||
modelID: model.ref.id,
|
||||
})
|
||||
const response = yield* llm.generate(prepared.request, prepared.options).pipe(ModelRetry.retry)
|
||||
return { text: response.text, ...SessionUsage.record(response.usage, model.cost) }
|
||||
const usage = SessionUsage.record(response.usage, model.cost)
|
||||
yield* bus.publish(SessionEvent.UsageRecorded, { sessionID: selection.session.id, source: "generate", ...usage })
|
||||
return { text: response.text, ...usage }
|
||||
}).pipe(instances.provide(input.session))
|
||||
})
|
||||
|
||||
@@ -20,6 +20,7 @@ import { AppNodeBuilder } from "@opencode/core/effect/app-node-builder"
|
||||
import { llmClient } from "@opencode/core/effect/app-node-platform"
|
||||
import { LayerNode } from "@opencode/util/effect/layer-node"
|
||||
import { Bus } from "@opencode/core/bus"
|
||||
import { Event } from "@opencode/schema/event"
|
||||
import { EventTable } from "@opencode/core/event/sql"
|
||||
import { InstructionDiscovery } from "@opencode/core/instruction-discovery"
|
||||
import { Instance } from "@opencode/core/instance/service"
|
||||
@@ -180,16 +181,9 @@ const it = testEffect(
|
||||
),
|
||||
)
|
||||
|
||||
// Session history generation must leave untouched; usage accounting is checked separately.
|
||||
const durableState = (db: Database.Interface["db"], sessionID: SessionSchema.ID) =>
|
||||
Effect.all({
|
||||
sequence: Bus.latestSequence(db, sessionID),
|
||||
bus: db
|
||||
.select()
|
||||
.from(EventTable)
|
||||
.where(eq(EventTable.aggregate_id, sessionID))
|
||||
.orderBy(asc(EventTable.seq))
|
||||
.all()
|
||||
.pipe(Effect.orDie),
|
||||
messages: db
|
||||
.select()
|
||||
.from(SessionMessageTable)
|
||||
@@ -211,9 +205,26 @@ const durableState = (db: Database.Interface["db"], sessionID: SessionSchema.ID)
|
||||
.get()
|
||||
.pipe(Effect.orDie),
|
||||
blobs: db.select().from(InstructionBlobTable).orderBy(asc(InstructionBlobTable.hash)).all().pipe(Effect.orDie),
|
||||
})
|
||||
|
||||
const usageState = (db: Database.Interface["db"], sessionID: SessionSchema.ID) =>
|
||||
Effect.all({
|
||||
sequence: Bus.latestSequence(db, sessionID),
|
||||
events: db
|
||||
.select()
|
||||
.from(EventTable)
|
||||
.where(eq(EventTable.aggregate_id, sessionID))
|
||||
.orderBy(asc(EventTable.seq))
|
||||
.all()
|
||||
.pipe(Effect.orDie),
|
||||
session: db.select().from(SessionTable).where(eq(SessionTable.id, sessionID)).get().pipe(Effect.orDie),
|
||||
})
|
||||
|
||||
const recordedUsage = (
|
||||
before: Effect.Success<ReturnType<typeof usageState>>,
|
||||
after: Effect.Success<ReturnType<typeof usageState>>,
|
||||
) => after.events.slice(before.events.length).map((event) => ({ type: event.type, data: event.data }))
|
||||
|
||||
const userTexts = (request: LLMRequest) =>
|
||||
request.messages.flatMap((message) =>
|
||||
message.role === "user"
|
||||
@@ -268,7 +279,7 @@ const setup = Effect.gen(function* () {
|
||||
})
|
||||
|
||||
it.effect(
|
||||
"generates from fresh settled Session context without durable mutation",
|
||||
"generates from fresh settled Session context and records only its usage",
|
||||
() =>
|
||||
Effect.gen(function* () {
|
||||
requests.length = 0
|
||||
@@ -339,6 +350,7 @@ it.effect(
|
||||
})
|
||||
instruction = "Changed context"
|
||||
const before = yield* durableState(db, sessionID)
|
||||
const usageBefore = yield* usageState(db, sessionID)
|
||||
|
||||
const result = yield* SessionGenerate.generate({ session, prompt: "Summarize privately" }).pipe(
|
||||
Effect.provideService(Instance.Service, instances),
|
||||
@@ -372,6 +384,21 @@ it.effect(
|
||||
expect(requests[0]?.toolChoice).toBeUndefined()
|
||||
expect(options[0]?.webSocket).toBeUndefined()
|
||||
expect(yield* durableState(db, sessionID)).toEqual(before)
|
||||
const usageAfter = yield* usageState(db, sessionID)
|
||||
expect(recordedUsage(usageBefore, usageAfter)).toEqual([
|
||||
{
|
||||
type: Event.versionedType(SessionEvent.UsageRecorded.type, SessionEvent.UsageRecorded.durable.version),
|
||||
data: { sessionID, source: "generate", cost: result.cost, tokens: result.tokens },
|
||||
},
|
||||
])
|
||||
expect(usageAfter.session?.cost).toBeCloseTo((usageBefore.session?.cost ?? 0) + result.cost)
|
||||
expect(usageAfter.session).toMatchObject({
|
||||
tokens_input: (usageBefore.session?.tokens_input ?? 0) + 60,
|
||||
tokens_output: (usageBefore.session?.tokens_output ?? 0) + 10,
|
||||
tokens_reasoning: usageBefore.session?.tokens_reasoning ?? 0,
|
||||
tokens_cache_read: (usageBefore.session?.tokens_cache_read ?? 0) + 30,
|
||||
tokens_cache_write: (usageBefore.session?.tokens_cache_write ?? 0) + 10,
|
||||
})
|
||||
}),
|
||||
{ timeout: 15_000 },
|
||||
)
|
||||
@@ -419,6 +446,7 @@ it.effect("regenerates a failed partial answer without mutating Session history"
|
||||
const { db, bus, instructions, session, instances } = yield* setup
|
||||
yield* InstructionState.prepare(db, bus, instructions, sessionID)
|
||||
const before = yield* durableState(db, sessionID)
|
||||
const usageBefore = yield* usageState(db, sessionID)
|
||||
const llm = yield* TestLLM.Test
|
||||
yield* llm.push(
|
||||
TestLLM.failAfter(
|
||||
@@ -443,6 +471,8 @@ it.effect("regenerates a failed partial answer without mutating Session history"
|
||||
expect(requests[1]).toBe(requests[0])
|
||||
expect(userTexts(requests[1])).toContain("Summarize privately")
|
||||
expect(yield* durableState(db, sessionID)).toEqual(before)
|
||||
// The retried attempt counts once.
|
||||
expect(recordedUsage(usageBefore, yield* usageState(db, sessionID))).toHaveLength(1)
|
||||
}).pipe(Effect.provide(TestLLM.testLayer())),
|
||||
)
|
||||
|
||||
@@ -454,6 +484,7 @@ it.effect(
|
||||
instruction = Instructions.unavailable
|
||||
const { db, session, instances } = yield* setup
|
||||
const before = yield* durableState(db, sessionID)
|
||||
const usageBefore = yield* usageState(db, sessionID)
|
||||
|
||||
const error = yield* SessionGenerate.generate({ session, prompt: "Summarize privately" }).pipe(
|
||||
Effect.provideService(Instance.Service, instances),
|
||||
@@ -463,6 +494,7 @@ it.effect(
|
||||
expect(error).toBeInstanceOf(Instructions.InitializationBlocked)
|
||||
expect(requests).toEqual([])
|
||||
expect(yield* durableState(db, sessionID)).toEqual(before)
|
||||
expect(yield* usageState(db, sessionID)).toEqual(usageBefore)
|
||||
}),
|
||||
{ timeout: 15_000 },
|
||||
)
|
||||
|
||||
Generated
+2
-2
@@ -3790,7 +3790,7 @@
|
||||
}
|
||||
}
|
||||
},
|
||||
"description": "Generate transient text from the current session context without mutating session history.",
|
||||
"description": "Generate transient text from the current session context without mutating session history. Its tokens and cost count toward the session's totals.",
|
||||
"summary": "Generate text from session context",
|
||||
"requestBody": {
|
||||
"content": {
|
||||
@@ -30132,7 +30132,7 @@
|
||||
},
|
||||
"source": {
|
||||
"type": "string",
|
||||
"enum": ["title", "compaction"]
|
||||
"enum": ["title", "compaction", "generate"]
|
||||
},
|
||||
"cost": {
|
||||
"$ref": "#/components/schemas/Money.USD"
|
||||
|
||||
@@ -647,7 +647,8 @@ export const makeSessionGroup = <I extends HttpApiMiddleware.AnyId, S, FormI ext
|
||||
OpenApi.annotations({
|
||||
identifier: "session.generate",
|
||||
summary: "Generate text from session context",
|
||||
description: "Generate transient text from the current session context without mutating session history.",
|
||||
description:
|
||||
"Generate transient text from the current session context without mutating session history. Its tokens and cost count toward the session's totals.",
|
||||
}),
|
||||
),
|
||||
)
|
||||
|
||||
@@ -162,7 +162,7 @@ export const UsageRecorded = Event.durable({
|
||||
...options,
|
||||
schema: {
|
||||
...Base,
|
||||
source: Schema.Literals(["title", "compaction"]),
|
||||
source: Schema.Literals(["title", "compaction", "generate"]),
|
||||
cost: Money.USD,
|
||||
tokens: TokenUsage.Info,
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user