Skip to content

Commit ddb5f29

Browse files
committed
Merge branch 'dev' into opentui
2 parents 390032b + ca463a2 commit ddb5f29

File tree

2 files changed

+68
-29
lines changed

2 files changed

+68
-29
lines changed

packages/opencode/src/session/prompt.ts

Lines changed: 45 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,11 @@ export namespace SessionPrompt {
286286
OUTPUT_TOKEN_MAX,
287287
),
288288
abortSignal: abort.signal,
289-
providerOptions: ProviderTransform.providerOptions(model.npm, model.providerID, params.options),
289+
providerOptions: ProviderTransform.providerOptions(
290+
model.npm,
291+
model.providerID,
292+
params.options,
293+
),
290294
stopWhen: stepCountIs(1),
291295
temperature: params.temperature,
292296
topP: params.topP,
@@ -321,7 +325,11 @@ export namespace SessionPrompt {
321325
async transformParams(args) {
322326
if (args.type === "stream") {
323327
// @ts-expect-error
324-
args.params.prompt = ProviderTransform.message(args.params.prompt, model.providerID, model.modelID)
328+
args.params.prompt = ProviderTransform.message(
329+
args.params.prompt,
330+
model.providerID,
331+
model.modelID,
332+
)
325333
}
326334
return args.params
327335
},
@@ -504,7 +512,11 @@ export namespace SessionPrompt {
504512
)
505513
for (const item of await ToolRegistry.tools(input.providerID, input.modelID)) {
506514
if (Wildcard.all(item.id, enabledTools) === false) continue
507-
const schema = ProviderTransform.schema(input.providerID, input.modelID, z.toJSONSchema(item.parameters))
515+
const schema = ProviderTransform.schema(
516+
input.providerID,
517+
input.modelID,
518+
z.toJSONSchema(item.parameters),
519+
)
508520
tools[item.id] = tool({
509521
id: item.id as any,
510522
description: item.description,
@@ -585,17 +597,7 @@ export namespace SessionPrompt {
585597
args,
586598
},
587599
)
588-
const result = await execute(args, opts).catch((err: unknown) => {
589-
log.error("Error executing tool", { error: err, tool: key })
590-
return {
591-
content: [
592-
{
593-
type: "text",
594-
text: `Failed to execute tool: ${err instanceof Error ? err.message : String(err)}`,
595-
},
596-
],
597-
}
598-
})
600+
const result = await execute(args, opts)
599601

600602
await Plugin.trigger(
601603
"tool.execute.after",
@@ -809,7 +811,9 @@ export namespace SessionPrompt {
809811
messageID: info.id,
810812
sessionID: input.sessionID,
811813
type: "file",
812-
url: `data:${part.mime};base64,` + Buffer.from(await file.bytes()).toString("base64"),
814+
url:
815+
`data:${part.mime};base64,` +
816+
Buffer.from(await file.bytes()).toString("base64"),
813817
mime: part.mime,
814818
filename: part.filename!,
815819
source: part.source,
@@ -883,7 +887,9 @@ export namespace SessionPrompt {
883887
synthetic: true,
884888
})
885889
}
886-
const wasPlan = input.messages.some((msg) => msg.info.role === "assistant" && msg.info.mode === "plan")
890+
const wasPlan = input.messages.some(
891+
(msg) => msg.info.role === "assistant" && msg.info.mode === "plan",
892+
)
887893
if (wasPlan && input.agent.name === "build") {
888894
userMessage.parts.push({
889895
id: Identifier.ascending("part"),
@@ -963,7 +969,10 @@ export namespace SessionPrompt {
963969
partFromToolCall(toolCallID: string) {
964970
return toolcalls[toolCallID]
965971
},
966-
async process(stream: StreamTextResult<Record<string, AITool>, never>, retries: { count: number; max: number }) {
972+
async process(
973+
stream: StreamTextResult<Record<string, AITool>, never>,
974+
retries: { count: number; max: number },
975+
) {
967976
log.info("process")
968977
if (!assistantMsg) throw new Error("call next() first before processing")
969978
let shouldRetry = false
@@ -1096,7 +1105,10 @@ export namespace SessionPrompt {
10961105
status: "error",
10971106
input: value.input,
10981107
error: (value.error as any).toString(),
1099-
metadata: value.error instanceof Permission.RejectedError ? value.error.metadata : undefined,
1108+
metadata:
1109+
value.error instanceof Permission.RejectedError
1110+
? value.error.metadata
1111+
: undefined,
11001112
time: {
11011113
start: match.state.time.start,
11021114
end: Date.now(),
@@ -1220,7 +1232,11 @@ export namespace SessionPrompt {
12201232
error: e,
12211233
})
12221234
const error = MessageV2.fromError(e, { providerID: input.providerID })
1223-
if (retries.count < retries.max && MessageV2.APIError.isInstance(error) && error.data.isRetryable) {
1235+
if (
1236+
retries.count < retries.max &&
1237+
MessageV2.APIError.isInstance(error) &&
1238+
error.data.isRetryable
1239+
) {
12241240
shouldRetry = true
12251241
await Session.updatePart({
12261242
id: Identifier.ascending("part"),
@@ -1243,7 +1259,11 @@ export namespace SessionPrompt {
12431259
}
12441260
const p = await Session.getParts(assistantMsg.id)
12451261
for (const part of p) {
1246-
if (part.type === "tool" && part.state.status !== "completed" && part.state.status !== "error") {
1262+
if (
1263+
part.type === "tool" &&
1264+
part.state.status !== "completed" &&
1265+
part.state.status !== "error"
1266+
) {
12471267
await Session.updatePart({
12481268
...part,
12491269
state: {
@@ -1707,11 +1727,13 @@ export namespace SessionPrompt {
17071727
if (input.session.parentID) return
17081728
if (!Session.isDefaultTitle(input.session.title)) return
17091729
const isFirst =
1710-
input.history.filter((m) => m.info.role === "user" && !m.parts.every((p) => "synthetic" in p && p.synthetic))
1711-
.length === 1
1730+
input.history.filter(
1731+
(m) => m.info.role === "user" && !m.parts.every((p) => "synthetic" in p && p.synthetic),
1732+
).length === 1
17121733
if (!isFirst) return
17131734
const small =
1714-
(await Provider.getSmallModel(input.providerID)) ?? (await Provider.getModel(input.providerID, input.modelID))
1735+
(await Provider.getSmallModel(input.providerID)) ??
1736+
(await Provider.getModel(input.providerID, input.modelID))
17151737
const options = {
17161738
...ProviderTransform.options(small.providerID, small.modelID, input.session.id),
17171739
...small.info.options,

packages/opencode/src/session/summary.ts

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,16 @@ export namespace SessionSummary {
2929
)
3030

3131
async function summarizeSession(input: { sessionID: string; messages: MessageV2.WithParts[] }) {
32-
const diffs = await computeDiff({ messages: input.messages })
32+
const files = new Set(
33+
input.messages
34+
.flatMap((x) => x.parts)
35+
.filter((x) => x.type === "patch")
36+
.flatMap((x) => x.files),
37+
)
38+
console.log(files)
39+
const diffs = await computeDiff({ messages: input.messages }).then((x) =>
40+
x.filter((x) => files.has(x.file)),
41+
)
3342
await Session.update(input.sessionID, (draft) => {
3443
draft.summary = {
3544
diffs,
@@ -39,7 +48,9 @@ export namespace SessionSummary {
3948

4049
async function summarizeMessage(input: { messageID: string; messages: MessageV2.WithParts[] }) {
4150
const messages = input.messages.filter(
42-
(m) => m.info.id === input.messageID || (m.info.role === "assistant" && m.info.parentID === input.messageID),
51+
(m) =>
52+
m.info.id === input.messageID ||
53+
(m.info.role === "assistant" && m.info.parentID === input.messageID),
4354
)
4455
const msgWithParts = messages.find((m) => m.info.id === input.messageID)!
4556
const userMsg = msgWithParts.info as MessageV2.User
@@ -50,11 +61,14 @@ export namespace SessionSummary {
5061
}
5162
await Session.updateMessage(userMsg)
5263

53-
const assistantMsg = messages.find((m) => m.info.role === "assistant")!.info as MessageV2.Assistant
64+
const assistantMsg = messages.find((m) => m.info.role === "assistant")!
65+
.info as MessageV2.Assistant
5466
const small = await Provider.getSmallModel(assistantMsg.providerID)
5567
if (!small) return
5668

57-
const textPart = msgWithParts.parts.find((p) => p.type === "text" && !p.synthetic) as MessageV2.TextPart
69+
const textPart = msgWithParts.parts.find(
70+
(p) => p.type === "text" && !p.synthetic,
71+
) as MessageV2.TextPart
5872
if (textPart && !userMsg.summary?.title) {
5973
const result = await generateText({
6074
maxOutputTokens: small.info.reasoning ? 1500 : 20,
@@ -81,7 +95,8 @@ export namespace SessionSummary {
8195
if (
8296
messages.some(
8397
(m) =>
84-
m.info.role === "assistant" && m.parts.some((p) => p.type === "step-finish" && p.reason !== "tool-calls"),
98+
m.info.role === "assistant" &&
99+
m.parts.some((p) => p.type === "step-finish" && p.reason !== "tool-calls"),
85100
)
86101
) {
87102
const result = await generateText({
@@ -114,7 +129,9 @@ export namespace SessionSummary {
114129
let all = await Session.messages(input.sessionID)
115130
if (input.messageID)
116131
all = all.filter(
117-
(x) => x.info.id === input.messageID || (x.info.role === "assistant" && x.info.parentID === input.messageID),
132+
(x) =>
133+
x.info.id === input.messageID ||
134+
(x.info.role === "assistant" && x.info.parentID === input.messageID),
118135
)
119136

120137
return computeDiff({

0 commit comments

Comments
 (0)