Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions packages/opencode/migration/20260321160000_add_teams/migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
CREATE TABLE `team` (
`id` text PRIMARY KEY NOT NULL,
`session_id` text NOT NULL,
`name` text NOT NULL,
`status` text DEFAULT 'active' NOT NULL,
`time_created` integer NOT NULL,
`time_updated` integer NOT NULL,
FOREIGN KEY (`session_id`) REFERENCES `session`(`id`) ON UPDATE no action ON DELETE cascade
);--> statement-breakpoint
CREATE TABLE `team_member` (
`id` integer PRIMARY KEY AUTOINCREMENT NOT NULL,
`team_id` text NOT NULL,
`session_id` text NOT NULL,
`agent` text NOT NULL,
`role` text NOT NULL,
`status` text DEFAULT 'active' NOT NULL,
`time_created` integer NOT NULL,
`time_updated` integer NOT NULL,
FOREIGN KEY (`team_id`) REFERENCES `team`(`id`) ON UPDATE no action ON DELETE cascade,
FOREIGN KEY (`session_id`) REFERENCES `session`(`id`) ON UPDATE no action ON DELETE cascade
);--> statement-breakpoint
CREATE TABLE `team_task` (
`id` text PRIMARY KEY NOT NULL,
`team_id` text NOT NULL,
`subject` text NOT NULL,
`description` text,
`owner` text,
`status` text DEFAULT 'pending' NOT NULL,
`metadata` text,
`time_created` integer NOT NULL,
`time_updated` integer NOT NULL,
FOREIGN KEY (`team_id`) REFERENCES `team`(`id`) ON UPDATE no action ON DELETE cascade
);--> statement-breakpoint
CREATE TABLE `agent_memory` (
`id` text PRIMARY KEY NOT NULL,
`project_id` text NOT NULL,
`agent` text NOT NULL,
`content` text NOT NULL,
`time_created` integer NOT NULL,
`time_updated` integer NOT NULL,
FOREIGN KEY (`project_id`) REFERENCES `project`(`id`) ON UPDATE no action ON DELETE cascade
);--> statement-breakpoint
CREATE INDEX `team_session_idx` ON `team` (`session_id`);--> statement-breakpoint
CREATE INDEX `team_member_team_idx` ON `team_member` (`team_id`);--> statement-breakpoint
CREATE INDEX `team_member_session_idx` ON `team_member` (`session_id`);--> statement-breakpoint
CREATE INDEX `team_task_team_idx` ON `team_task` (`team_id`);--> statement-breakpoint
CREATE UNIQUE INDEX `agent_memory_project_agent_idx` ON `agent_memory` (`project_id`,`agent`);
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"version": "7",
"dialect": "sqlite",
"id": "add-teams-migration",
"prevIds": [],
"ddl": []
}
2 changes: 2 additions & 0 deletions packages/opencode/src/agent/agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export namespace Agent {
prompt: z.string().optional(),
options: z.record(z.string(), z.any()),
steps: z.number().int().positive().optional(),
memory: z.enum(["none", "local"]).optional(),
})
.meta({
ref: "Agent",
Expand Down Expand Up @@ -256,6 +257,7 @@ export namespace Agent {
item.hidden = value.hidden ?? item.hidden
item.name = value.name ?? item.name
item.steps = value.steps ?? item.steps
item.memory = value.memory ?? item.memory
item.options = mergeDeep(item.options, value.options ?? {})
item.permission = Permission.merge(item.permission, Permission.fromConfig(value.permission ?? {}))
}
Expand Down
109 changes: 109 additions & 0 deletions packages/opencode/src/agent/memory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
import z from "zod"
import { BusEvent } from "@/bus/bus-event"
import { Bus } from "@/bus"
import { Database, eq, and } from "../storage/db"
import { AgentMemoryTable } from "../team/team.sql"
import { MemoryID } from "../team/schema"
import { Instance } from "../project/instance"
import { Log } from "../util/log"

const log = Log.create({ service: "agent.memory" })

const MAX_SIZE = 102_400 // 100KB

export namespace AgentMemory {
export const Info = z
.object({
id: MemoryID.zod,
projectID: z.string(),
agent: z.string(),
content: z.string(),
time: z.object({
created: z.number(),
updated: z.number(),
}),
})
.meta({ ref: "AgentMemory" })
export type Info = z.infer<typeof Info>

export const Event = {
Updated: BusEvent.define(
"agent.memory.updated",
z.object({
agent: z.string(),
projectID: z.string(),
}),
),
}

function toInfo(row: typeof AgentMemoryTable.$inferSelect): Info {
return {
id: row.id,
projectID: row.project_id,
agent: row.agent,
content: row.content,
time: {
created: row.time_created,
updated: row.time_updated,
},
}
}

export function read(agent: string): Info | undefined {
const pid = Instance.project.id
const row = Database.use((db) =>
db
.select()
.from(AgentMemoryTable)
.where(and(eq(AgentMemoryTable.project_id, pid), eq(AgentMemoryTable.agent, agent)))
.get(),
)
if (!row) return undefined
return toInfo(row)
}

export function write(agent: string, content: string) {
if (Buffer.byteLength(content, "utf8") > MAX_SIZE) {
content = Buffer.from(content, "utf8").subarray(0, MAX_SIZE).toString("utf8")
log.warn("memory truncated to 100KB", { agent })
}
const pid = Instance.project.id
const now = Date.now()
const existing = read(agent)
if (existing) {
Database.use((db) =>
db
.update(AgentMemoryTable)
.set({ content, time_updated: now })
.where(eq(AgentMemoryTable.id, existing.id))
.run(),
)
} else {
const id = MemoryID.ascending()
Database.use((db) =>
db
.insert(AgentMemoryTable)
.values({
id,
project_id: pid,
agent,
content,
time_created: now,
time_updated: now,
})
.run(),
)
}
log.info("written", { agent, projectID: pid })
Database.effect(() => Bus.publish(Event.Updated, { agent, projectID: pid }))
}

export function append(agent: string, content: string) {
const existing = read(agent)
if (existing) {
write(agent, existing.content + "\n\n" + content)
} else {
write(agent, content)
}
}
}
80 changes: 80 additions & 0 deletions packages/opencode/src/cli/cmd/tui/context/sync.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,27 @@ import type {
ProviderAuthMethod,
VcsInfo,
} from "@opencode-ai/sdk/v2"

export interface TeamMember {
teamID: string
sessionID: string
agent: string
role: "lead" | "member"
status: "active" | "completed" | "failed" | "cancelled"
}

export interface TeamInfo {
id: string
sessionID: string
name: string
status: "active" | "disbanded"
time: { created: number; updated: number }
}

export interface TeamWithMembers {
team: TeamInfo
members: TeamMember[]
}
import { createStore, produce, reconcile } from "solid-js/store"
import { useSDK } from "@tui/context/sdk"
import { Binary } from "@opencode-ai/util/binary"
Expand Down Expand Up @@ -72,6 +93,7 @@ export const { use: useSync, provider: SyncProvider } = createSimpleContext({
[key: string]: McpResource
}
formatter: FormatterStatus[]
teams: TeamWithMembers[]
vcs: VcsInfo | undefined
path: Path
workspaceList: Workspace[]
Expand Down Expand Up @@ -100,6 +122,7 @@ export const { use: useSync, provider: SyncProvider } = createSimpleContext({
mcp: {},
mcp_resource: {},
formatter: [],
teams: [],
vcs: undefined,
path: { state: "", config: "", worktree: "", directory: "" },
workspaceList: [],
Expand All @@ -113,8 +136,64 @@ export const { use: useSync, provider: SyncProvider } = createSimpleContext({
setStore("workspaceList", reconcile(result.data))
}

async function syncTeams() {
const result = (await sdk
.fetch(`${sdk.url}/team/active`)
.then((r) => r.json())
.catch(() => undefined)) as TeamWithMembers[] | undefined
if (!result) return
setStore("teams", reconcile(result))
}

sdk.event.listen((e) => {
const event = e.details
// Handle team events (not yet in SDK types)
const type = event.type as string
if (type === "team.created") {
const team = (event as any).properties.team as TeamInfo
setStore(
"teams",
produce((draft) => {
draft.push({ team, members: [] })
}),
)
return
}
if (type === "team.disbanded") {
const teamID = (event as any).properties.teamID as string
setStore(
"teams",
produce((draft) => {
const idx = draft.findIndex((t) => t.team.id === teamID)
if (idx !== -1) draft.splice(idx, 1)
}),
)
return
}
if (type === "team.member.added") {
const member = (event as any).properties.member as TeamMember
setStore(
"teams",
produce((draft) => {
const entry = draft.find((t) => t.team.id === member.teamID)
if (entry) entry.members.push(member)
}),
)
return
}
if (type === "team.member.updated") {
const member = (event as any).properties.member as TeamMember
setStore(
"teams",
produce((draft) => {
const entry = draft.find((t) => t.team.id === member.teamID)
if (!entry) return
const idx = entry.members.findIndex((m) => m.sessionID === member.sessionID)
if (idx !== -1) entry.members[idx] = member
}),
)
return
}
switch (event.type) {
case "server.instance.disposed":
bootstrap()
Expand Down Expand Up @@ -423,6 +502,7 @@ export const { use: useSync, provider: SyncProvider } = createSimpleContext({
sdk.client.vcs.get().then((x) => setStore("vcs", reconcile(x.data))),
sdk.client.path.get().then((x) => setStore("path", reconcile(x.data!))),
syncWorkspaces(),
syncTeams(),
]).then(() => {
setStore("status", "complete")
})
Expand Down
Loading
Loading