mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-17 05:26:47 +03:00
fix(core): leave commentary text out of generated titles
This commit is contained in:
@@ -56,7 +56,8 @@ export const layer = Layer.effect(
|
||||
readonly text: string
|
||||
readonly model: SessionRunnerModel.Resolved
|
||||
}) {
|
||||
const chunks: string[] = []
|
||||
const chunks: Array<{ id: string; text: string }> = []
|
||||
const commentary = new Set<string>()
|
||||
let failed = false
|
||||
let usage: SessionUsage.Recorded | undefined
|
||||
const recordUsage = Effect.suspend(() =>
|
||||
@@ -76,9 +77,16 @@ export const layer = Layer.effect(
|
||||
messages: [Message.user(input.text)],
|
||||
})
|
||||
if (prepared.event.result !== undefined) return prepared.event.result
|
||||
const metadataKey = prepared.request.model.route.providerMetadataKey ?? prepared.request.model.provider
|
||||
yield* llm.stream(prepared.request, prepared.options).pipe(
|
||||
Stream.runForEach((event) => {
|
||||
if (LLMEvent.is.textDelta(event)) chunks.push(event.text)
|
||||
// Responses models may emit commentary-phase text, and the phase can arrive only when the block ends.
|
||||
if (
|
||||
(LLMEvent.is.textStart(event) || LLMEvent.is.textEnd(event)) &&
|
||||
event.providerMetadata?.[metadataKey]?.phase === "commentary"
|
||||
)
|
||||
commentary.add(event.id)
|
||||
if (LLMEvent.is.textDelta(event)) chunks.push({ id: event.id, text: event.text })
|
||||
if (LLMEvent.is.stepFinish(event)) {
|
||||
const step = SessionUsage.record(event.usage, input.model.cost)
|
||||
usage = usage ? SessionUsage.add(usage, step) : step
|
||||
@@ -95,6 +103,8 @@ export const layer = Layer.effect(
|
||||
yield* recordUsage
|
||||
if (failed) return
|
||||
return chunks
|
||||
.filter((chunk) => !commentary.has(chunk.id))
|
||||
.map((chunk) => chunk.text)
|
||||
.join("")
|
||||
.split("\n")
|
||||
.map((line) => line.trim())
|
||||
|
||||
@@ -10,7 +10,7 @@ import {
|
||||
TransportError,
|
||||
type LLMRequest,
|
||||
} from "@opencode/ai/effect"
|
||||
import { OpenAIChat } from "@opencode/ai/protocols"
|
||||
import { OpenAIChat, OpenAIResponses } from "@opencode/ai/protocols"
|
||||
import { Agent } from "@opencode/core/agent"
|
||||
import { Database } from "@opencode/core/database/database"
|
||||
import { AppNodeBuilder } from "@opencode/core/effect/app-node-builder"
|
||||
@@ -35,7 +35,7 @@ import { Model } from "@opencode/core/model"
|
||||
import { Provider } from "@opencode/core/provider"
|
||||
import { AbsolutePath } from "@opencode/core/schema"
|
||||
import { Money } from "@opencode/schema/money"
|
||||
import { Deferred, Effect, Fiber, Layer, Stream } from "effect"
|
||||
import { Deferred, Effect, Fiber, Layer, Schema, Stream } from "effect"
|
||||
import { testEffect } from "./lib/effect"
|
||||
|
||||
let requests: LLMRequest[] = []
|
||||
@@ -84,11 +84,11 @@ const successfulTitle = () =>
|
||||
reason: { normalized: "stop" },
|
||||
}),
|
||||
)
|
||||
let titleStream: () => Stream.Stream<LLMEvent, AIError> = successfulTitle
|
||||
let titleStream: (request: LLMRequest) => Stream.Stream<LLMEvent, AIError> = successfulTitle
|
||||
const client = Layer.mock(LLMClient.Service)({
|
||||
stream: (request: LLMRequest) => {
|
||||
requests.push(request)
|
||||
return titleStream()
|
||||
return titleStream(request)
|
||||
},
|
||||
generate: () => Effect.die("unused"),
|
||||
})
|
||||
@@ -237,6 +237,46 @@ it.effect("generates a title from the sole user message and renames the session"
|
||||
}),
|
||||
)
|
||||
|
||||
for (const timing of ["start", "end"] as const)
|
||||
it.effect(`excludes commentary from titles when the response phase arrives at ${timing}`, () =>
|
||||
Effect.gen(function* () {
|
||||
yield* enableTitleAgent
|
||||
const sessionID = Session.ID.make(`ses_title_phase_${timing}`)
|
||||
yield* insertSession(sessionID)
|
||||
yield* prompt(sessionID, "Investigate performance across four hosts without making changes")
|
||||
titleStream = (request) =>
|
||||
Stream.fromIterable([
|
||||
{
|
||||
type: "response.output_item.added",
|
||||
item: { type: "message", id: "commentary", ...(timing === "start" ? { phase: "commentary" } : {}) },
|
||||
},
|
||||
{
|
||||
type: "response.output_text.delta",
|
||||
item_id: "commentary",
|
||||
delta: "We need title only. Need capture investigation four machines performance read-only. Title concise.",
|
||||
},
|
||||
{ type: "response.output_item.done", item: { type: "message", id: "commentary", phase: "commentary" } },
|
||||
{ type: "response.output_item.added", item: { type: "message", id: "final", phase: "final_answer" } },
|
||||
{ type: "response.output_text.delta", item_id: "final", delta: "Read-only performance investigation" },
|
||||
{ type: "response.output_item.done", item: { type: "message", id: "final", phase: "final_answer" } },
|
||||
{ type: "response.completed", response: { id: "response_title" } },
|
||||
]).pipe(
|
||||
Stream.map((event) => Schema.decodeUnknownSync(OpenAIResponses.protocol.stream.event)(JSON.stringify(event))),
|
||||
Stream.mapAccumEffect(
|
||||
() => OpenAIResponses.protocol.stream.initial(request),
|
||||
OpenAIResponses.protocol.stream.step,
|
||||
),
|
||||
)
|
||||
|
||||
const title = yield* SessionTitle.Service
|
||||
yield* title.generate(sessionID)
|
||||
|
||||
const store = yield* SessionStore.Service
|
||||
expect(requests).toHaveLength(1)
|
||||
expect((yield* store.get(sessionID))?.title).toBe("Read-only performance investigation")
|
||||
}),
|
||||
)
|
||||
|
||||
it.effect("runs title hooks instead of context hooks", () =>
|
||||
Effect.gen(function* () {
|
||||
yield* enableTitleAgent
|
||||
|
||||
Reference in New Issue
Block a user