Skip to content

Commit f99904b

Browse files
committed
track version on session info
1 parent b796d67 commit f99904b

File tree

6 files changed

+51
-19
lines changed

6 files changed

+51
-19
lines changed

packages/opencode/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ const cli = yargs(hideBin(process.argv))
120120
.command(ScrapCommand)
121121
.command(AuthCommand)
122122
.command(UpgradeCommand)
123-
.fail((msg, err) => {
123+
.fail((msg) => {
124124
if (
125125
msg.startsWith("Unknown argument") ||
126126
msg.startsWith("Not enough non-option arguments")

packages/opencode/src/provider/models.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { Global } from "../global"
2-
import { lazy } from "../util/lazy"
32
import { Log } from "../util/log"
43
import path from "path"
54
import { z } from "zod"

packages/opencode/src/session/index.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import { SystemPrompt } from "./system"
3131
import { Flag } from "../flag/flag"
3232
import type { ModelsDev } from "../provider/models"
3333
import { GlobalConfig } from "../global/config"
34+
import { Installation } from "../installation"
3435

3536
export namespace Session {
3637
const log = Log.create({ service: "session" })
@@ -46,6 +47,7 @@ export namespace Session {
4647
})
4748
.optional(),
4849
title: z.string(),
50+
version: z.string(),
4951
time: z.object({
5052
created: z.number(),
5153
updated: z.number(),
@@ -84,6 +86,7 @@ export namespace Session {
8486
export async function create(parentID?: string) {
8587
const result: Info = {
8688
id: Identifier.descending("session"),
89+
version: Installation.VERSION,
8790
parentID,
8891
title:
8992
(parentID ? "Child session - " : "New Session - ") +
@@ -331,6 +334,16 @@ export namespace Session {
331334
sessionID: input.sessionID,
332335
abort: abort.signal,
333336
messageID: next.id,
337+
metadata: async (val) => {
338+
next.metadata.tool[opts.toolCallId] = {
339+
...val,
340+
time: {
341+
start: 0,
342+
end: 0,
343+
},
344+
}
345+
await updateMessage(next)
346+
},
334347
})
335348
next.metadata!.tool![opts.toolCallId] = {
336349
...result.metadata,

packages/opencode/src/tool/task.ts

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import { Tool } from "./tool"
22
import DESCRIPTION from "./task.txt"
33
import { z } from "zod"
44
import { Session } from "../session"
5+
import { Bus } from "../bus"
6+
import { Message } from "../session/message"
57

68
export const TaskTool = Tool.define({
79
id: "opencode.task",
@@ -17,6 +19,28 @@ export const TaskTool = Tool.define({
1719
const msg = await Session.getMessage(ctx.sessionID, ctx.messageID)
1820
const metadata = msg.metadata.assistant!
1921

22+
function summary(input: Message.Info) {
23+
const result = []
24+
25+
for (const part of input.parts) {
26+
if (part.type === "tool-invocation") {
27+
result.push({
28+
toolInvocation: part.toolInvocation,
29+
metadata: input.metadata.tool[part.toolInvocation.toolCallId],
30+
})
31+
}
32+
}
33+
return result
34+
}
35+
36+
const unsub = Bus.subscribe(Message.Event.Updated, async (evt) => {
37+
if (evt.properties.info.metadata.sessionID !== ctx.sessionID) return
38+
ctx.metadata({
39+
title: params.description,
40+
summary: summary(evt.properties.info),
41+
})
42+
})
43+
2044
const result = await Session.chat({
2145
sessionID: session.id,
2246
modelID: metadata.modelID,
@@ -28,10 +52,11 @@ export const TaskTool = Tool.define({
2852
},
2953
],
3054
})
31-
55+
unsub()
3256
return {
3357
metadata: {
3458
title: params.description,
59+
summary: summary(result),
3560
},
3661
output: result.parts.findLast((x) => x.type === "text")!.text,
3762
}

packages/opencode/src/tool/tool.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@ export namespace Tool {
55
title: string
66
[key: string]: any
77
}
8-
export type Context = {
8+
export type Context<M extends Metadata = Metadata> = {
99
sessionID: string
1010
messageID: string
1111
abort: AbortSignal
12+
metadata(meta: M): void
1213
}
1314
export interface Info<
1415
Parameters extends StandardSchemaV1 = StandardSchemaV1,

packages/opencode/test/tool/tool.test.ts

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@ import { App } from "../../src/app/app"
33
import { GlobTool } from "../../src/tool/glob"
44
import { ListTool } from "../../src/tool/ls"
55

6+
const ctx = {
7+
sessionID: "test",
8+
messageID: "",
9+
abort: AbortSignal.any([]),
10+
metadata: () => {},
11+
}
612
describe("tool.glob", () => {
713
test("truncate", async () => {
814
await App.provide({ cwd: process.cwd() }, async () => {
@@ -11,11 +17,7 @@ describe("tool.glob", () => {
1117
pattern: "./node_modules/**/*",
1218
path: undefined,
1319
},
14-
{
15-
sessionID: "test",
16-
messageID: "",
17-
abort: AbortSignal.any([]),
18-
},
20+
ctx,
1921
)
2022
expect(result.metadata.truncated).toBe(true)
2123
})
@@ -27,11 +29,7 @@ describe("tool.glob", () => {
2729
pattern: "*.json",
2830
path: undefined,
2931
},
30-
{
31-
sessionID: "test",
32-
messageID: "",
33-
abort: AbortSignal.any([]),
34-
},
32+
ctx,
3533
)
3634
expect(result.metadata).toMatchObject({
3735
truncated: false,
@@ -46,11 +44,7 @@ describe("tool.ls", () => {
4644
const result = await App.provide({ cwd: process.cwd() }, async () => {
4745
return await ListTool.execute(
4846
{ path: "./example", ignore: [".git"] },
49-
{
50-
sessionID: "test",
51-
messageID: "",
52-
abort: AbortSignal.any([]),
53-
},
47+
ctx,
5448
)
5549
})
5650
expect(result.output).toMatchSnapshot()

0 commit comments

Comments
 (0)