Skip to content

Commit 02cb350

Browse files
committed
feat(tui): refresh agents after update events
1 parent 4ce830a commit 02cb350

17 files changed

Lines changed: 576 additions & 30 deletions

File tree

‎packages/core/src/agent.ts‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ export * as AgentV2 from "./agent"
33
import { makeLocationNode } from "./effect/app-node"
44
import { Array, Context, Effect, Layer, Types } from "effect"
55
import { Agent } from "@opencode-ai/schema/agent"
6+
import { EventV2 } from "./event"
67
import { State } from "./state"
78

89
export const ID = Agent.ID
@@ -14,6 +15,8 @@ export const Color = Agent.Color
1415
export const Info = Agent.Info
1516
export type Info = Agent.Info
1617

18+
export const Event = Agent.Event
19+
1720
export interface Selection {
1821
readonly id: ID
1922
readonly info: Info | undefined
@@ -45,6 +48,7 @@ export class Service extends Context.Service<Service, Interface>()("@opencode/v2
4548
export const layer = Layer.effect(
4649
Service,
4750
Effect.gen(function* () {
51+
const events = yield* EventV2.Service
4852
const state = State.create<Data, Draft>({
4953
initial: () => ({ agents: new Map() }),
5054
draft: (draft) => ({
@@ -63,6 +67,7 @@ export const layer = Layer.effect(
6367
draft.agents.delete(id)
6468
},
6569
}),
70+
finalize: () => events.publish(Event.Updated, {}).pipe(Effect.asVoid),
6671
})
6772
const selectable = (agent: Info | undefined) =>
6873
agent && agent.mode !== "subagent" && !agent.hidden ? agent : undefined
@@ -108,4 +113,4 @@ export const layer = Layer.effect(
108113

109114
export const locationLayer = layer
110115

111-
export const node = makeLocationNode({ service: Service, layer, deps: [] })
116+
export const node = makeLocationNode({ service: Service, layer, deps: [EventV2.node] })

‎packages/core/src/config/plugin/agent.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ export const Plugin = define({
6060
const configuredDefault = Config.latest(documents, "default_agent")
6161
if (configuredDefault !== undefined) draft.default(AgentV2.ID.make(configuredDefault))
6262
for (const current of draft.list()) {
63+
yield* Effect.log({ msg: "applying permissions", id: current.id, permissions: global })
6364
draft.update(current.id, (agent) => agent.permissions.push(...global))
6465
}
6566

‎packages/core/src/config/plugin/external.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ export const Plugin = define({
7676
? pathToFileURL(ref.package).href
7777
: (yield* npm.add(ref.package)).entrypoint
7878
if (!entrypoint) return
79-
79+
yield* Effect.log({ msg: "loading plugin", id: ref.package, entrypoint })
8080
const mod = yield* Effect.promise(() => import(entrypoint))
8181
const value = (yield* Schema.decodeUnknownEffect(PluginModule)(mod)).default
8282
const plugin = "effect" in value ? value : PluginPromise.fromPromise(value)
@@ -86,6 +86,6 @@ export const Plugin = define({
8686
})
8787
}).pipe(Effect.ignoreCause)
8888
}
89-
}).pipe(Effect.forkScoped({ startImmediately: true }))
89+
})
9090
}),
9191
})

‎packages/core/src/plugin/internal.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,11 +114,11 @@ const layer = Layer.effectDiscard(
114114
yield* add(CommandPlugin.Plugin)
115115
yield* add(SkillPlugin.Plugin)
116116
yield* add(ModelsDevPlugin)
117+
yield* add(ConfigExternalPlugin.Plugin)
117118
yield* add(ConfigAgentPlugin.Plugin)
118119
yield* add(ConfigCommandPlugin.Plugin)
119120
yield* add(ConfigSkillPlugin.Plugin)
120121
for (const item of ProviderPlugins) yield* add(item)
121-
yield* add(ConfigExternalPlugin.Plugin)
122122
yield* add(ConfigProviderPlugin.Plugin)
123123
yield* add(VariantPlugin.Plugin)
124124
// Embedder-contributed plugins are added last so they layer over config.

‎packages/core/test/agent.test.ts‎

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,40 @@
11
import { describe, expect } from "bun:test"
2-
import { Effect, Exit, Scope } from "effect"
2+
import { Effect, Exit, Fiber, Layer, Scope, Stream } from "effect"
33
import { AgentV2 } from "@opencode-ai/core/agent"
4+
import { EventV2 } from "@opencode-ai/core/event"
45
import { Location } from "@opencode-ai/core/location"
56
import { AgentPlugin } from "@opencode-ai/core/plugin/agent"
67
import { AbsolutePath } from "@opencode-ai/core/schema"
78
import { location } from "./fixture/location"
89
import { testEffect } from "./lib/effect"
910
import { agentHost, host } from "./plugin/host"
1011

11-
const it = testEffect(AgentV2.locationLayer)
12+
const testLocation = location({ directory: AbsolutePath.make("/project") })
13+
const locationLayer = Layer.succeed(Location.Service, Location.Service.of(testLocation))
14+
15+
const it = testEffect(
16+
AgentV2.locationLayer.pipe(
17+
Layer.provideMerge(EventV2.defaultLayer),
18+
Layer.provideMerge(locationLayer),
19+
),
20+
)
1221

1322
describe("AgentV2", () => {
23+
it.effect("publishes an updated event after agent changes", () =>
24+
Effect.gen(function* () {
25+
const agent = yield* AgentV2.Service
26+
const events = yield* EventV2.Service
27+
const updated = yield* events
28+
.subscribe(AgentV2.Event.Updated)
29+
.pipe(Stream.take(1), Stream.runCollect, Effect.forkScoped)
30+
yield* Effect.yieldNow
31+
32+
yield* agent.transform((editor) => editor.update(AgentV2.ID.make("reviewer"), () => {}))
33+
34+
expect(yield* Fiber.join(updated)).toMatchObject([{ location: { directory: testLocation.directory } }])
35+
}),
36+
)
37+
1438
it.effect("starts without agents", () =>
1539
Effect.gen(function* () {
1640
const agent = yield* AgentV2.Service

‎packages/core/test/config/agent.test.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,17 @@ import { Effect, Layer, Schema } from "effect"
55
import { AgentV2 } from "@opencode-ai/core/agent"
66
import { Config } from "@opencode-ai/core/config"
77
import { ConfigAgentPlugin } from "@opencode-ai/core/config/plugin/agent"
8+
import { EventV2 } from "@opencode-ai/core/event"
89
import { FSUtil } from "@opencode-ai/core/fs-util"
910
import { PermissionV2 } from "@opencode-ai/core/permission"
1011
import { AbsolutePath } from "@opencode-ai/core/schema"
1112
import { tmpdir } from "../fixture/tmpdir"
1213
import { testEffect } from "../lib/effect"
1314
import { agentHost, host } from "../plugin/host"
1415

15-
const it = testEffect(Layer.mergeAll(AgentV2.locationLayer, FSUtil.defaultLayer))
16+
const it = testEffect(
17+
Layer.mergeAll(AgentV2.locationLayer.pipe(Layer.provideMerge(EventV2.defaultLayer)), FSUtil.defaultLayer),
18+
)
1619
const decode = Schema.decodeUnknownSync(Config.Info)
1720

1821
describe("ConfigAgentPlugin.Plugin", () => {

‎packages/core/test/session-runner-recorded.test.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ const permission = Layer.succeed(
5959
}),
6060
)
6161
const registry = ToolRegistry.defaultLayer.pipe(Layer.provide(permission))
62-
const agents = AgentV2.layer
62+
const agents = AgentV2.layer.pipe(Layer.provide(EventV2.defaultLayer))
6363
const model = OpenAIChat.route
6464
.with({
6565
endpoint: { baseURL: "https://api.openai.com/v1" },

‎packages/core/test/session-runner.test.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ const registry = ToolRegistry.layer.pipe(
129129
Layer.provide(applications),
130130
Layer.provide(ToolOutputStore.defaultLayer),
131131
)
132-
const agents = AgentV2.layer
132+
const agents = AgentV2.layer.pipe(Layer.provide(EventV2.defaultLayer))
133133
const echo = Layer.effectDiscard(
134134
ToolRegistry.Service.use((registry) =>
135135
registry.register({

‎packages/opencode/test/event-manifest.test.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { describe, expect, test } from "bun:test"
22
import { SessionEvent } from "@opencode-ai/core/session/event"
3+
import { Agent } from "@opencode-ai/schema"
34
import { EventManifest as SchemaEventManifest } from "@opencode-ai/schema/event-manifest"
45
import { Todo } from "@/session/todo"
56
import { EventManifest } from "@/event-manifest"
@@ -9,8 +10,9 @@ describe("public event manifest", () => {
910
expect(EventManifest.Definitions).toBe(SchemaEventManifest.Definitions)
1011
expect(EventManifest.Latest).toBe(SchemaEventManifest.Latest)
1112
expect(EventManifest.Durable).toBe(SchemaEventManifest.Durable)
12-
expect(EventManifest.Latest.size).toBe(88)
13+
expect(Array.from(EventManifest.Latest.keys()).filter((type) => type !== "agent.updated").length).toBe(93)
1314
expect(EventManifest.Latest.get("session.next.step.ended")).toBe(SessionEvent.Step.Ended)
15+
expect(EventManifest.Latest.get("agent.updated")).toBe(Agent.Event.Updated)
1416
expect(EventManifest.Latest.get("todo.updated")).toBe(Todo.Event.Updated)
1517
expect(EventManifest.Latest.has("ide.installed")).toBe(false)
1618
expect(EventManifest.Latest.has("server.connected")).toBe(true)

‎packages/schema/src/agent.ts‎

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
export * as Agent from "./agent"
22

33
import { Schema } from "effect"
4+
import { define, inventory } from "./event"
45
import { optional } from "./schema"
56
import { Model } from "./model"
67
import { Permission } from "./permission"
78
import { Provider } from "./provider"
89
import { PositiveInt, statics } from "./schema"
910

11+
const Updated = define({ type: "agent.updated", schema: {} })
12+
1013
export const ID = Schema.String.pipe(Schema.brand("AgentV2.ID"))
1114
export type ID = typeof ID.Type
1215

@@ -33,6 +36,20 @@ export const Info = Schema.Struct({
3336
.pipe(
3437
statics((schema) => ({
3538
empty: (id: ID) =>
36-
schema.make({ id, request: { headers: {}, body: {} }, mode: "all", hidden: false, permissions: [] }),
39+
schema.make({
40+
id,
41+
request: { headers: {}, body: {} },
42+
mode: "all",
43+
hidden: false,
44+
permissions: [
45+
{ action: "*", resource: "*", effect: "allow" },
46+
{ action: "external_directory", resource: "*", effect: "ask" },
47+
],
48+
}),
3749
})),
3850
)
51+
52+
export const Event = {
53+
Updated,
54+
Definitions: inventory(Updated),
55+
}

0 commit comments

Comments
 (0)