Skip to content

Commit e5b06a2

Browse files
committed
initialzie
1 parent fae49aa commit e5b06a2

6 files changed

Lines changed: 113 additions & 9 deletions

File tree

packages/opencode/AGENTS.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# OpenCode Agent Guidelines
2+
3+
## Build/Test Commands
4+
- **Install**: `bun install`
5+
- **Run**: `bun run index.ts`
6+
- **Typecheck**: `bun run typecheck` (npm run typecheck)
7+
- **Test**: `bun test` (runs all tests)
8+
- **Single test**: `bun test test/tool/tool.test.ts` (specific test file)
9+
10+
## Code Style
11+
- **Runtime**: Bun with TypeScript ESM modules
12+
- **Imports**: Use relative imports for local modules, named imports preferred
13+
- **Types**: Zod schemas for validation, TypeScript interfaces for structure
14+
- **Naming**: camelCase for variables/functions, PascalCase for classes/namespaces
15+
- **Error handling**: Use Result patterns, avoid throwing exceptions in tools
16+
- **File structure**: Namespace-based organization (e.g., `Tool.define()`, `Session.create()`)
17+
18+
## Architecture
19+
- **Tools**: Implement `Tool.Info` interface with `execute()` method
20+
- **Context**: Pass `sessionID` in tool context, use `App.provide()` for DI
21+
- **Validation**: All inputs validated with Zod schemas
22+
- **Logging**: Use `Log.create({ service: "name" })` pattern
23+
- **Storage**: Use `Storage` namespace for persistence

packages/opencode/src/index.ts

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -84,17 +84,11 @@ cli
8484
unsub()
8585
})
8686

87-
const [provider] = await Provider.active().then((val) =>
88-
val.values().toArray(),
89-
)
90-
if (!provider) throw new Error("no providers found")
91-
const model = provider.models[0]
92-
if (!model) throw new Error("no models found")
93-
console.log("using", provider.id, model.id)
87+
const { providerID, modelID } = await Provider.defaultModel()
9488
const result = await Session.chat({
9589
sessionID: session.id,
96-
providerID: provider.id,
97-
modelID: model.id,
90+
providerID,
91+
modelID,
9892
parts: [
9993
{
10094
type: "text",
@@ -115,6 +109,27 @@ cli
115109
})
116110
})
117111

112+
cli.command("init", "Run a chat message").action(async () => {
113+
await App.provide({ cwd: process.cwd(), version }, async () => {
114+
const { modelID, providerID } = await Provider.defaultModel()
115+
console.log("Initializing...")
116+
117+
const session = await Session.create()
118+
119+
const unsub = Bus.subscribe(Session.Event.Updated, async (message) => {
120+
if (message.properties.info.share?.url)
121+
console.log("Share:", message.properties.info.share.url)
122+
unsub()
123+
})
124+
125+
await Session.initialize({
126+
sessionID: session.id,
127+
modelID,
128+
providerID,
129+
})
130+
})
131+
})
132+
118133
cli.version(typeof OPENCODE_VERSION === "string" ? OPENCODE_VERSION : "dev")
119134
cli.help()
120135
cli.parse()

packages/opencode/src/provider/provider.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,17 @@ export namespace Provider {
141141
}
142142
}
143143

144+
export async function defaultModel() {
145+
const [provider] = await active().then((val) => val.values().toArray())
146+
if (!provider) throw new Error("no providers found")
147+
const model = provider.models[0]
148+
if (!model) throw new Error("no models found")
149+
return {
150+
providerID: provider.id,
151+
modelID: model.id,
152+
}
153+
}
154+
144155
const TOOLS = [
145156
BashTool,
146157
EditTool,

packages/opencode/src/server/server.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,35 @@ export namespace Server {
9393
return c.json(App.info())
9494
},
9595
)
96+
.post(
97+
"/session_initialize",
98+
describeRoute({
99+
description: "Analyze the app and create an AGENTS.md file",
100+
responses: {
101+
200: {
102+
description: "200",
103+
content: {
104+
"application/json": {
105+
schema: resolver(z.boolean()),
106+
},
107+
},
108+
},
109+
},
110+
}),
111+
zValidator(
112+
"json",
113+
z.object({
114+
sessionID: z.string(),
115+
providerID: z.string(),
116+
modelID: z.string(),
117+
}),
118+
),
119+
async (c) => {
120+
const body = c.req.valid("json")
121+
await Session.initialize(body)
122+
return c.json(true)
123+
},
124+
)
96125
.post(
97126
"/path_get",
98127
describeRoute({
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Please analyze this codebase and create an AGENTS.md file containing:
2+
1. Build/lint/test commands - especially for running a single test
3+
2. Code style guidelines including imports, formatting, types, naming conventions, error handling, etc.
4+
5+
The file you create will be given to agentic coding agents (such as yourself) that operate in this repository. Make it about 20 lines long.
6+
If there's already an AGENTS.md, improve it.
7+
If there are Cursor rules (in .cursor/rules/ or .cursorrules) or Copilot rules (in .github/copilot-instructions.md), make sure to include them.

packages/opencode/src/session/session.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import { Decimal } from "decimal.js"
1818
import PROMPT_ANTHROPIC from "./prompt/anthropic.txt"
1919
import PROMPT_TITLE from "./prompt/title.txt"
2020
import PROMPT_SUMMARIZE from "./prompt/summarize.txt"
21+
import PROMPT_INITIALIZE from "../session/prompt/initialize.txt"
2122

2223
import { Share } from "../share/share"
2324
import { Message } from "./message"
@@ -531,4 +532,22 @@ export namespace Session {
531532
super(`Session ${sessionID} is busy`)
532533
}
533534
}
535+
536+
export async function initialize(input: {
537+
sessionID: string
538+
modelID: string
539+
providerID: string
540+
}) {
541+
await Session.chat({
542+
sessionID: input.sessionID,
543+
providerID: input.providerID,
544+
modelID: input.modelID,
545+
parts: [
546+
{
547+
type: "text",
548+
text: PROMPT_INITIALIZE,
549+
},
550+
],
551+
})
552+
}
534553
}

0 commit comments

Comments
 (0)