-
Notifications
You must be signed in to change notification settings - Fork 5
feat: add parallel session workflow with branch-synced session naming #6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| --- | ||
| description: Rename this session | ||
| --- | ||
|
|
||
| Rename this session to: $ARGUMENTS | ||
|
|
||
| Use the session-rename tool to update the session title. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| description: Sync session title with current git branch | ||
| --- | ||
|
|
||
| Use the session-rename_sync_branch tool to rename this session to match the current git branch name. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| import { tool } from "@opencode-ai/plugin" | ||
|
|
||
| export default tool({ | ||
| description: "Rename the current session to a new title. Use this after creating a git branch to sync the session name with the branch name.", | ||
| args: { | ||
| title: tool.schema.string().describe("New title for the session (e.g., branch name like 'feature/my-feature')"), | ||
| }, | ||
| async execute(args, context) { | ||
| const { sessionID } = context | ||
| const { title } = args | ||
|
|
||
| // Call the OpenCode API to update the session | ||
| // The server runs on localhost:4096 by default | ||
| const port = process.env.OPENCODE_PORT || "4096" | ||
| const baseUrl = `http://localhost:${port}` | ||
|
|
||
| try { | ||
| const response = await fetch(`${baseUrl}/session/${sessionID}`, { | ||
| method: "PATCH", | ||
| headers: { | ||
| "Content-Type": "application/json", | ||
| }, | ||
| body: JSON.stringify({ title }), | ||
| }) | ||
|
|
||
| if (!response.ok) { | ||
| const error = await response.text() | ||
| return `Failed to rename session: ${error}` | ||
| } | ||
|
|
||
| const session = await response.json() | ||
| return `Session renamed to: ${session.title || title}` | ||
| } catch (error) { | ||
| return `Error renaming session: ${error instanceof Error ? error.message : String(error)}` | ||
| } | ||
| }, | ||
| }) | ||
|
|
||
| // Also export a tool that syncs with the current git branch | ||
| export const sync_branch = tool({ | ||
| description: "Rename the current session to match the current git branch name. Call this after creating or switching branches.", | ||
| args: {}, | ||
| async execute(_args, context) { | ||
| const { sessionID } = context | ||
|
|
||
| // Get current branch name | ||
| const branchResult = await Bun.$`git branch --show-current`.text() | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| const branch = branchResult.trim() | ||
|
|
||
| if (!branch) { | ||
| return "Not in a git repository or no branch checked out" | ||
| } | ||
|
|
||
| // Call the OpenCode API to update the session | ||
| const port = process.env.OPENCODE_PORT || "4096" | ||
| const baseUrl = `http://localhost:${port}` | ||
|
|
||
| try { | ||
| const response = await fetch(`${baseUrl}/session/${sessionID}`, { | ||
| method: "PATCH", | ||
| headers: { | ||
| "Content-Type": "application/json", | ||
| }, | ||
| body: JSON.stringify({ title: branch }), | ||
| }) | ||
|
|
||
| if (!response.ok) { | ||
| const error = await response.text() | ||
| return `Failed to sync session with branch: ${error}` | ||
| } | ||
|
|
||
| const session = await response.json() | ||
| return `Session synced with branch: ${session.title || branch}` | ||
| } catch (error) { | ||
| return `Error syncing session: ${error instanceof Error ? error.message : String(error)}` | ||
| } | ||
| }, | ||
| }) | ||
|
Comment on lines
+1
to
+78
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This file has two main areas for improvement:
The suggested change below addresses both points by creating a import { tool } from "@opencode-ai/plugin"
async function renameSession(sessionID: string, title: string): Promise<string> {
// Call the OpenCode API to update the session
// The server runs on localhost:4096 by default
const port = process.env.OPENCODE_PORT || "4096"
const baseUrl = `http://localhost:${port}`
const response = await fetch(`${baseUrl}/session/${sessionID}`, {
method: "PATCH",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ title }),
})
if (!response.ok) {
const error = await response.text()
// Throwing an error to be caught by the caller
throw new Error(error)
}
const session = await response.json()
return session.title || title
}
export default tool({
description: "Rename the current session to a new title. Use this after creating a git branch to sync the session name with the branch name.",
args: {
title: tool.schema.string().describe("New title for the session (e.g., branch name like 'feature/my-feature')"),
},
async execute(args, context) {
const { sessionID } = context
const { title } = args
try {
const newTitle = await renameSession(sessionID, title)
return `Session renamed to: ${newTitle}`
} catch (error) {
return `Failed to rename session: ${error instanceof Error ? error.message : String(error)}`
}
},
})
// Also export a tool that syncs with the current git branch
export const sync_branch = tool({
description: "Rename the current session to match the current git branch name. Call this after creating or switching branches.",
args: {},
async execute(_args, context) {
const { sessionID } = context
try {
// Get current branch name
const branchResult = await Bun.$`git branch --show-current`.text()
const branch = branchResult.trim()
if (!branch) {
return "Not in a git repository or no branch checked out"
}
const newTitle = await renameSession(sessionID, branch)
return `Session synced with branch: ${newTitle}`
} catch (error) {
return `Error syncing session: ${error instanceof Error ? error.message : String(error)}`
}
},
}) |
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For clarity, the command in the documentation should be represented generically using a placeholder, rather than a specific example. This helps users understand that
feature/xyzis a placeholder for a value they need to provide.