feat(core): return usage from session generate (#62835)

This commit is contained in:
Aiden Cline
2026-11-09 09:38:13 -06:00
committed by GitHub
Unverified
parent 17afb4c7c3
commit 28928c397f
12 changed files with 65 additions and 19 deletions
+11 -2
View File
@@ -14,9 +14,9 @@ import type { SessionInbox } from "@opencode/schema/session-inbox"
import type { PromptInput } from "@opencode/schema/prompt-input"
import type { AgentAttachment } from "@opencode/schema/prompt"
import type { FileDiff } from "@opencode/schema/file-diff"
import type { Money } from "@opencode/schema/money"
import type { Form } from "@opencode/schema/form"
import type { DateTime } from "effect"
import type { Money } from "@opencode/schema/money"
import type { Skill } from "@opencode/schema/skill"
import type { InstructionEntry } from "@opencode/schema/instruction-entry"
import type { Schema } from "effect"
@@ -322,7 +322,16 @@ export type SessionInboxUpdateOperation<E = never> = (
) => Effect.Effect<SessionInboxUpdateOutput, E>
export type SessionGenerateInput = { readonly sessionID: Session.ID; readonly prompt: string }
export type SessionGenerateOutput = { readonly text: string }
export type SessionGenerateOutput = {
readonly text: string
readonly tokens: {
readonly input: number
readonly output: number
readonly reasoning: number
readonly cache: { readonly read: number; readonly write: number }
}
readonly cost: Money.USD
}
export type SessionGenerateOperation<E = never> = (
input: SessionGenerateInput,
) => Effect.Effect<SessionGenerateOutput, E>
+2 -2
View File
@@ -121,8 +121,6 @@ export type SessionStructuredError = {
retryAfterMs?: number
}
export type SessionGenerateResponse = { data: { text: string } }
export type SessionInterruptResponse = { interrupted: boolean }
export type FormID = string
@@ -456,6 +454,8 @@ export type SessionMessageCompactionRunning = {
export type UsageLimitsBudgetModel = { providerID: ProviderID; modelID: string; name: string; spent?: MoneyUSD }
export type SessionGenerateResponse = { data: { text: string; tokens: TokenUsageInfo; cost: MoneyUSD } }
export type FileSystemEntry = { path: RelativePath; type: "file" | "directory" }
export type SessionRevert = {
+9 -1
View File
@@ -1050,7 +1050,14 @@ test("session methods use the public HTTP contract", async () => {
})
}
if (url.includes("/prompt")) return Response.json(admission)
if (url.includes("/generate")) return Response.json({ data: { text: "A transient answer" } })
if (url.includes("/generate"))
return Response.json({
data: {
text: "A transient answer",
tokens: { input: 12, output: 3, reasoning: 0, cache: { read: 40, write: 0 } },
cost: 0.0001,
},
})
if (url.includes("/synthetic")) return Response.json(syntheticAdmission)
if (url.endsWith("/compact")) return Response.json(compactionAdmission)
if (url.includes("/context")) return Response.json({ data: [] })
@@ -1098,6 +1105,7 @@ test("session methods use the public HTTP contract", async () => {
expect(created.id).toBe("ses_test")
expect(admitted.id).toBe("msg_test")
expect(generated.text).toBe("A transient answer")
expect(generated.tokens.cache.read).toBe(40)
expect(interrupted).toEqual({ interrupted: true })
expect(synthetic).toMatchObject({ type: "synthetic", data: { text: "Completed" }, delivery: "queue" })
expect(context).toEqual([])
+1 -1
View File
@@ -588,7 +588,7 @@ export const make = Effect.fn("PluginHost.make")(function* (plugin: Pick<Interfa
switchAgent: sessions.switchAgent,
switchModel: sessions.switchModel,
prompt: sessions.prompt,
generate: (input) => sessions.generate(input).pipe(Effect.map((text) => ({ text }))),
generate: sessions.generate,
command: (input) => sessions.command({ ...input, command: input.name }),
compact: sessions.compact,
update: Effect.fn(function* (input) {
+1 -1
View File
@@ -214,7 +214,7 @@ export interface Interface {
readonly generate: (input: {
sessionID: SessionSchema.ID
prompt: string
}) => Effect.Effect<string, NotFoundError | SessionGenerate.Error>
}) => Effect.Effect<SessionGenerate.Result, NotFoundError | SessionGenerate.Error>
readonly command: (input: {
sessionID: SessionSchema.ID
command: string
+5 -3
View File
@@ -14,6 +14,7 @@ import { SessionModelRequest } from "./model-request.js"
import { ModelRetry } from "../model-retry.js"
import type { SessionRunnerModel } from "./runner/model.js"
import type { SessionSchema } from "./schema.js"
import { SessionUsage } from "./usage.js"
export type Error =
| AgentNotFoundError
@@ -22,7 +23,9 @@ export type Error =
| AIError
| FileSystem.DirectoryNotFoundError
/** Generates text from current Session context without mutating the Session. */
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. */
export const generate = Effect.fn("SessionGenerate.generate")(function* (input: {
session: SessionSchema.Info
prompt: string
@@ -62,7 +65,6 @@ 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)
yield* Effect.logInfo("session generation usage diagnostic", { usage: response.usage })
return response.text
return { text: response.text, ...SessionUsage.record(response.usage, model.cost) }
}).pipe(instances.provide(input.session))
})
+19 -4
View File
@@ -53,6 +53,7 @@ import { Plugin } from "@opencode/core/plugin"
import { PluginHooks } from "@opencode/core/plugin/hooks"
import { PluginSupervisor } from "@opencode/core/plugin/supervisor"
import { Tool } from "@opencode/core/tool"
import { Money } from "@opencode/schema/money"
import { asc, eq } from "drizzle-orm"
import { Effect, Fiber, Layer, Schema, Stream } from "effect"
import { TestClock } from "effect/testing"
@@ -78,7 +79,13 @@ const client = Layer.mock(LLMClient.Service)({
LLMEvent.stepFinish({
index: 0,
reason: { normalized: "stop" },
usage: { inputTokens: 100, outputTokens: 10 },
usage: {
inputTokens: 100,
outputTokens: 10,
nonCachedInputTokens: 60,
cacheReadInputTokens: 30,
cacheWriteInputTokens: 10,
},
}),
LLMEvent.finish({ reason: { normalized: "stop" } }),
])
@@ -91,7 +98,13 @@ const models = Layer.mock(SessionRunnerModel.Service)({
Effect.succeed(
SessionRunnerModel.resolved(model, {
capabilities: { tools: true, input: ["text", "image"], output: ["text"] },
cost: [],
cost: [
{
input: Money.USDPerMillionTokens.make(1),
output: Money.USDPerMillionTokens.make(2),
cache: { read: Money.USDPerMillionTokens.make(0.1), write: Money.USDPerMillionTokens.make(0.5) },
},
],
limit: { context: 200_000, output: 32_000 },
}),
),
@@ -331,7 +344,9 @@ it.effect(
Effect.provideService(Instance.Service, instances),
)
expect(result).toBe("Transient answer")
expect(result.text).toBe("Transient answer")
expect(result.tokens).toEqual({ input: 60, output: 10, reasoning: 0, cache: { read: 30, write: 10 } })
expect(result.cost).toBeCloseTo(0.000088)
expect(requests).toHaveLength(1)
expect(requests[0]?.model).toBe(model)
expect(requests[0]?.system.map((part) => part.text)).toContain("Initial context")
@@ -422,7 +437,7 @@ it.effect("regenerates a failed partial answer without mutating Session history"
)
yield* llm.wait(1)
yield* TestClock.adjust("3 seconds")
expect(yield* Fiber.join(result)).toBe("Fresh answer")
expect((yield* Fiber.join(result)).text).toBe("Fresh answer")
const requests = yield* llm.requests()
expect(requests).toHaveLength(2)
expect(requests[1]).toBe(requests[0])
+7 -1
View File
@@ -24388,9 +24388,15 @@
"properties": {
"text": {
"type": "string"
},
"tokens": {
"$ref": "#/components/schemas/TokenUsage.Info"
},
"cost": {
"$ref": "#/components/schemas/Money.USD"
}
},
"required": ["text"],
"required": ["text", "tokens", "cost"],
"additionalProperties": false
}
},
+4 -1
View File
@@ -3,6 +3,8 @@ import { SessionInbox } from "@opencode/schema/session-inbox"
import { PromptInput } from "@opencode/schema/prompt-input"
import { Session } from "@opencode/schema/session"
import { SessionStats } from "@opencode/schema/session-stats"
import { TokenUsage } from "@opencode/schema/token-usage"
import { Money } from "@opencode/schema/money"
import { InstructionEntry } from "@opencode/schema/instruction-entry"
import { Project } from "@opencode/schema/project"
import { DateTimeUtcFromMillis, NonNegativeInt, PositiveInt } from "@opencode/schema/schema"
@@ -635,7 +637,8 @@ export const makeSessionGroup = <I extends HttpApiMiddleware.AnyId, S, FormI ext
params: { sessionID: Session.ID },
payload: Schema.Struct({ prompt: Schema.String }),
success: Schema.Struct({
data: Schema.Struct({ text: Schema.String }),
// Same `tokens` and `cost` an assistant message reports for a step.
data: Schema.Struct({ text: Schema.String, tokens: TokenUsage.Info, cost: Money.USD }),
}).annotate({ identifier: "SessionGenerateResponse" }),
error: [SessionNotFoundError, ServiceUnavailableError],
})
+2 -2
View File
@@ -583,7 +583,7 @@ export const SessionHandler = HttpApiBuilder.group(Api, "server.session", (handl
.handle(
"session.generate",
Effect.fn(function* (ctx) {
const text = yield* session
const result = yield* session
.generate({ sessionID: ctx.params.sessionID, prompt: ctx.payload.prompt })
.pipe(
Effect.mapError((error) =>
@@ -592,7 +592,7 @@ export const SessionHandler = HttpApiBuilder.group(Api, "server.session", (handl
: new ServiceUnavailableError({ message: error.message, service: "session generation" }),
),
)
return { data: { text } }
return { data: result }
}),
)
.handle(
@@ -216,7 +216,7 @@ it.live(
for (const config of configs) {
yield* llm.push(TestLLM.text(`generated ${config.id}`, config.tool))
expect(yield* sessions.generate({ sessionID: config.id, prompt: "summarize" })).toBe(`generated ${config.id}`)
expect((yield* sessions.generate({ sessionID: config.id, prompt: "summarize" })).text).toBe(`generated ${config.id}`)
yield* sessions.command({ sessionID: config.id, command: "instance-check", text: "" })
expect(yield* sessions.inbox(config.id)).toMatchObject([
{ type: "user", payload: { text: `command ${config.tool} [${config.tool}]` } },
@@ -802,6 +802,9 @@ await ctx.session.command({ sessionID, name: "review", text: "--staged" })
const synthetic = await ctx.session.synthetic({ sessionID, text: "Deployment completed" })
```
`generate` returns the `text` along with the call's `tokens` and `cost`, in the same shape an assistant message
reports them.
Rename, interrupt, or wait for a session.
```ts