GeezerVison is showing github.com — make /init a default slash command on server side (#3677) · kasbah/opencode@041353f
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: packages/opencode/src/acp/agent.ts +18-22Lines changed: 18 additions & 22 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -28,8 +28,6 @@ import { Storage } from "@/storage/storage" | 28 | 28 | import { Command } from "@/command" | 29 | 29 | import { Agent as Agents } from "@/agent/agent" | 30 | 30 | import { Permission } from "@/permission" | 31 | | -import { Session } from "@/session" | 32 | | -import { Identifier } from "@/id/id" | 33 | 31 | import { SessionCompaction } from "@/session/compaction" | 34 | 32 | import type { Config } from "@/config/config" | 35 | 33 | import { MCP } from "@/mcp" | @@ -89,7 +87,11 @@ export namespace ACP { | 89 | 87 | }) | 90 | 88 | if (!res) return | 91 | 89 | if (res.outcome.outcome !== "selected") { | 92 | | - Permission.respond({ sessionID: permission.sessionID, permissionID: permission.id, response: "reject" }) | | 90 | + Permission.respond({ | | 91 | + sessionID: permission.sessionID, | | 92 | + permissionID: permission.id, | | 93 | + response: "reject", | | 94 | + }) | 93 | 95 | return | 94 | 96 | } | 95 | 97 | Permission.respond({ | @@ -111,9 +113,11 @@ export namespace ACP { | 111 | 113 | const acpSession = this.sessionManager.get(part.sessionID) | 112 | 114 | if (!acpSession) return | 113 | 115 |
| 114 | | - const message = await Storage.read<MessageV2.Info>(["message", part.sessionID, part.messageID]).catch( | 115 | | - () => undefined, | 116 | | - ) | | 116 | + const message = await Storage.read<MessageV2.Info>([ | | 117 | + "message", | | 118 | + part.sessionID, | | 119 | + part.messageID, | | 120 | + ]).catch(() => undefined) | 117 | 121 | if (!message || message.role !== "assistant") return | 118 | 122 |
| 119 | 123 | if (part.type === "tool") { | @@ -192,7 +196,9 @@ export namespace ACP { | 192 | 196 | sessionUpdate: "plan", | 193 | 197 | entries: parsedTodos.data.map((todo) => { | 194 | 198 | const status: PlanEntry["status"] = | 195 | | - todo.status === "cancelled" ? "completed" : (todo.status as PlanEntry["status"]) | | 199 | + todo.status === "cancelled" | | 200 | + ? "completed" | | 201 | + : (todo.status as PlanEntry["status"]) | 196 | 202 | return { | 197 | 203 | priority: "medium", | 198 | 204 | status, | @@ -375,11 +381,6 @@ export namespace ACP { | 375 | 381 | description: command.description ?? "", | 376 | 382 | })) | 377 | 383 | const names = new Set(availableCommands.map((c) => c.name)) | 378 | | - if (!names.has("init")) | 379 | | - availableCommands.push({ | 380 | | - name: "init", | 381 | | - description: "create/update a AGENTS.md", | 382 | | - }) | 383 | 384 | if (!names.has("compact")) | 384 | 385 | availableCommands.push({ | 385 | 386 | name: "compact", | @@ -404,7 +405,8 @@ export namespace ACP { | 404 | 405 | description: agent.description, | 405 | 406 | })) | 406 | 407 |
| 407 | | - const currentModeId = availableModes.find((m) => m.name === "build")?.id ?? availableModes[0].id | | 408 | + const currentModeId = | | 409 | + availableModes.find((m) => m.name === "build")?.id ?? availableModes[0].id | 408 | 410 |
| 409 | 411 | const mcpServers: Record<string, Config.Mcp> = {} | 410 | 412 | for (const server of params.mcpServers) { | @@ -585,14 +587,6 @@ export namespace ACP { | 585 | 587 | } | 586 | 588 |
| 587 | 589 | switch (cmd.name) { | 588 | | - case "init": | 589 | | - await Session.initialize({ | 590 | | - sessionID, | 591 | | - messageID: Identifier.ascending("message"), | 592 | | - providerID: model.providerID, | 593 | | - modelID: model.modelID, | 594 | | - }) | 595 | | - break | 596 | 590 | case "compact": | 597 | 591 | await SessionCompaction.run({ | 598 | 592 | sessionID, | @@ -665,7 +659,9 @@ export namespace ACP { | 665 | 659 |
| 666 | 660 | function parseUri( | 667 | 661 | uri: string, | 668 | | - ): { type: "file"; url: string; filename: string; mime: string } | { type: "text"; text: string } { | | 662 | + ): | | 663 | + | { type: "file"; url: string; filename: string; mime: string } | | 664 | + | { type: "text"; text: string } { | 669 | 665 | try { | 670 | 666 | if (uri.startsWith("file://")) { | 671 | 667 | const path = uri.slice(7) | |
Collapse file Copy file name to clipboardExpand all lines: packages/opencode/src/command/index.ts +27Lines changed: 27 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | import z from "zod" | 2 | 2 | import { Config } from "../config/config" | 3 | 3 | import { Instance } from "../project/instance" | | 4 | +import PROMPT_INITIALIZE from "./template/initialize.txt" | | 5 | +import { Bus } from "../bus" | | 6 | +import { Identifier } from "../id/id" | 4 | 7 |
| 5 | 8 | export namespace Command { | | 9 | + export const Default = { | | 10 | + INIT: "init", | | 11 | + } as const | | 12 | + | | 13 | + export const Event = { | | 14 | + Executed: Bus.event( | | 15 | + "command.executed", | | 16 | + z.object({ | | 17 | + name: z.string(), | | 18 | + sessionID: Identifier.schema("session"), | | 19 | + arguments: z.string(), | | 20 | + messageID: Identifier.schema("message"), | | 21 | + }), | | 22 | + ), | | 23 | + } | | 24 | + | 6 | 25 | export const Info = z | 7 | 26 | .object({ | 8 | 27 | name: z.string(), | @@ -33,6 +52,14 @@ export namespace Command { | 33 | 52 | } | 34 | 53 | } | 35 | 54 |
| | 55 | + if (result[Default.INIT] === undefined) { | | 56 | + result[Default.INIT] = { | | 57 | + name: Default.INIT, | | 58 | + description: "create/update AGENTS.md", | | 59 | + template: PROMPT_INITIALIZE.replace("${path}", Instance.worktree), | | 60 | + } | | 61 | + } | | 62 | + | 36 | 63 | return result | 37 | 64 | }) | 38 | 65 |
| |
Collapse file Copy file name to clipboardExpand all lines: packages/opencode/src/command/template/initialize.txt +2Lines changed: 2 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -6,3 +6,5 @@ The file you create will be given to agentic coding agents (such as yourself) th | 6 | 6 | If there are Cursor rules (in .cursor/rules/ or .cursorrules) or Copilot rules (in .github/copilot-instructions.md), make sure to include them. | 7 | 7 |
| 8 | 8 | If there's already an AGENTS.md, improve it if it's located in ${path} | | 9 | + | | 10 | +$ARGUMENTS |
Collapse file Copy file name to clipboardExpand all lines: packages/opencode/src/project/bootstrap.ts +10Lines changed: 10 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -5,6 +5,10 @@ import { LSP } from "../lsp" | 5 | 5 | import { FileWatcher } from "../file/watcher" | 6 | 6 | import { File } from "../file" | 7 | 7 | import { Flag } from "../flag/flag" | | 8 | +import { Project } from "./project" | | 9 | +import { Bus } from "../bus" | | 10 | +import { Command } from "../command" | | 11 | +import { Instance } from "./instance" | 8 | 12 |
| 9 | 13 | export async function InstanceBootstrap() { | 10 | 14 | if (Flag.OPENCODE_EXPERIMENTAL_NO_BOOTSTRAP) return | @@ -14,4 +18,10 @@ export async function InstanceBootstrap() { | 14 | 18 | await LSP.init() | 15 | 19 | FileWatcher.init() | 16 | 20 | File.init() | | 21 | + | | 22 | + Bus.subscribe(Command.Event.Executed, async (payload) => { | | 23 | + if (payload.properties.name === Command.Default.INIT) { | | 24 | + await Project.setInitialized(Instance.project.id) | | 25 | + } | | 26 | + }) | 17 | 27 | } |
Collapse file Copy file name to clipboardExpand all lines: packages/opencode/src/session/index.ts +14-18Lines changed: 14 additions & 18 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -2,8 +2,6 @@ import { Decimal } from "decimal.js" | 2 | 2 | import z from "zod" | 3 | 3 | import { type LanguageModelUsage, type ProviderMetadata } from "ai" | 4 | 4 |
| 5 | | -import PROMPT_INITIALIZE from "../session/prompt/initialize.txt" | 6 | | - | 7 | 5 | import { Bus } from "../bus" | 8 | 6 | import { Config } from "../config/config" | 9 | 7 | import { Flag } from "../flag/flag" | @@ -14,11 +12,11 @@ import { Share } from "../share/share" | 14 | 12 | import { Storage } from "../storage/storage" | 15 | 13 | import { Log } from "../util/log" | 16 | 14 | import { MessageV2 } from "./message-v2" | 17 | | -import { Project } from "../project/project" | 18 | 15 | import { Instance } from "../project/instance" | 19 | 16 | import { SessionPrompt } from "./prompt" | 20 | 17 | import { fn } from "@/util/fn" | 21 | 18 | import { Snapshot } from "@/snapshot" | | 19 | +import { Command } from "../command" | 22 | 20 |
| 23 | 21 | export namespace Session { | 24 | 22 | const log = Log.create({ service: "session" }) | @@ -164,7 +162,12 @@ export namespace Session { | 164 | 162 | }) | 165 | 163 | }) | 166 | 164 |
| 167 | | - export async function createNext(input: { id?: string; title?: string; parentID?: string; directory: string }) { | | 165 | + export async function createNext(input: { | | 166 | + id?: string | | 167 | + title?: string | | 168 | + parentID?: string | | 169 | + directory: string | | 170 | + }) { | 168 | 171 | const result: Info = { | 169 | 172 | id: Identifier.descending("session", input.id), | 170 | 173 | version: Installation.VERSION, | @@ -402,7 +405,9 @@ export namespace Session { | 402 | 405 | .add(new Decimal(tokens.input).mul(input.model.cost?.input ?? 0).div(1_000_000)) | 403 | 406 | .add(new Decimal(tokens.output).mul(input.model.cost?.output ?? 0).div(1_000_000)) | 404 | 407 | .add(new Decimal(tokens.cache.read).mul(input.model.cost?.cache_read ?? 0).div(1_000_000)) | 405 | | - .add(new Decimal(tokens.cache.write).mul(input.model.cost?.cache_write ?? 0).div(1_000_000)) | | 408 | + .add( | | 409 | + new Decimal(tokens.cache.write).mul(input.model.cost?.cache_write ?? 0).div(1_000_000), | | 410 | + ) | 406 | 411 | .toNumber(), | 407 | 412 | tokens, | 408 | 413 | } | @@ -423,22 +428,13 @@ export namespace Session { | 423 | 428 | messageID: Identifier.schema("message"), | 424 | 429 | }), | 425 | 430 | async (input) => { | 426 | | - await SessionPrompt.prompt({ | | 431 | + await SessionPrompt.command({ | 427 | 432 | sessionID: input.sessionID, | 428 | 433 | messageID: input.messageID, | 429 | | - model: { | 430 | | - providerID: input.providerID, | 431 | | - modelID: input.modelID, | 432 | | - }, | 433 | | - parts: [ | 434 | | - { | 435 | | - id: Identifier.ascending("part"), | 436 | | - type: "text", | 437 | | - text: PROMPT_INITIALIZE.replace("${path}", Instance.worktree), | 438 | | - }, | 439 | | - ], | | 434 | + model: input.providerID + "/" + input.modelID, | | 435 | + command: Command.Default.INIT, | | 436 | + arguments: "", | 440 | 437 | }) | 441 | | - await Project.setInitialized(Instance.project.id) | 442 | 438 | }, | 443 | 439 | ) | 444 | 440 | } |
Collapse file Copy file name to clipboardExpand all lines: packages/opencode/src/session/prompt.ts +21-9Lines changed: 21 additions & 9 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -1593,6 +1593,7 @@ export namespace SessionPrompt { | 1593 | 1593 | let index = 0 | 1594 | 1594 | template = template.replace(bashRegex, () => results[index++]) | 1595 | 1595 | } | | 1596 | + template = template.trim() | 1596 | 1597 |
| 1597 | 1598 | const parts = [ | 1598 | 1599 | { | @@ -1657,6 +1658,8 @@ export namespace SessionPrompt { | 1657 | 1658 | })() | 1658 | 1659 |
| 1659 | 1660 | const agent = await Agent.get(agentName) | | 1661 | + let result: MessageV2.WithParts | | 1662 | + | 1660 | 1663 | if ((agent.mode === "subagent" && command.subtask !== false) || command.subtask === true) { | 1661 | 1664 | using abort = lock(input.sessionID) | 1662 | 1665 |
| @@ -1732,7 +1735,7 @@ export namespace SessionPrompt { | 1732 | 1735 | } | 1733 | 1736 | await Session.updatePart(toolPart) | 1734 | 1737 |
| 1735 | | - const result = await TaskTool.init().then((t) => | | 1738 | + const taskResult = await TaskTool.init().then((t) => | 1736 | 1739 | t.execute(args, { | 1737 | 1740 | sessionID: input.sessionID, | 1738 | 1741 | abort: abort.signal, | @@ -1760,22 +1763,31 @@ export namespace SessionPrompt { | 1760 | 1763 | }, | 1761 | 1764 | input: toolPart.state.input, | 1762 | 1765 | title: "", | 1763 | | - metadata: result.metadata, | 1764 | | - output: result.output, | | 1766 | + metadata: taskResult.metadata, | | 1767 | + output: taskResult.output, | 1765 | 1768 | } | 1766 | 1769 | await Session.updatePart(toolPart) | 1767 | 1770 | } | 1768 | 1771 |
| 1769 | | - return { info: assistantMsg, parts: [toolPart] } | | 1772 | + result = { info: assistantMsg, parts: [toolPart] } | | 1773 | + } else { | | 1774 | + result = await prompt({ | | 1775 | + sessionID: input.sessionID, | | 1776 | + messageID: input.messageID, | | 1777 | + model, | | 1778 | + agent: agentName, | | 1779 | + parts, | | 1780 | + }) | 1770 | 1781 | } | 1771 | 1782 |
| 1772 | | - return prompt({ | | 1783 | + Bus.publish(Command.Event.Executed, { | | 1784 | + name: input.command, | 1773 | 1785 | sessionID: input.sessionID, | 1774 | | - messageID: input.messageID, | 1775 | | - model, | 1776 | | - agent: agentName, | 1777 | | - parts, | | 1786 | + arguments: input.arguments, | | 1787 | + messageID: result.info.id, | 1778 | 1788 | }) | | 1789 | + | | 1790 | + return result | 1779 | 1791 | } | 1780 | 1792 |
| 1781 | 1793 | async function ensureTitle(input: { | |
|