fix(cli): preserve instance context in async commands
This commit is contained in:
@@ -66,6 +66,8 @@ const AgentCreateCommand = effectCmd({
|
|||||||
if (!maybeCtx) return yield* Effect.die("InstanceRef not provided")
|
if (!maybeCtx) return yield* Effect.die("InstanceRef not provided")
|
||||||
const ctx = maybeCtx
|
const ctx = maybeCtx
|
||||||
const agentSvc = yield* Agent.Service
|
const agentSvc = yield* Agent.Service
|
||||||
|
const runLocalEffect = <A, E>(effect: Effect.Effect<A, E>) =>
|
||||||
|
Effect.runPromise(effect.pipe(Effect.provideService(InstanceRef, ctx)))
|
||||||
yield* Effect.promise(async () => {
|
yield* Effect.promise(async () => {
|
||||||
const cliPath = args.path
|
const cliPath = args.path
|
||||||
const cliDescription = args.description
|
const cliDescription = args.description
|
||||||
@@ -127,7 +129,7 @@ const AgentCreateCommand = effectCmd({
|
|||||||
const spinner = prompts.spinner()
|
const spinner = prompts.spinner()
|
||||||
spinner.start("Generating agent configuration...")
|
spinner.start("Generating agent configuration...")
|
||||||
const model = args.model ? Provider.parseModel(args.model) : undefined
|
const model = args.model ? Provider.parseModel(args.model) : undefined
|
||||||
const generated = await Effect.runPromise(agentSvc.generate({ description, model })).catch((error) => {
|
const generated = await runLocalEffect(agentSvc.generate({ description, model })).catch((error) => {
|
||||||
spinner.stop(`LLM failed to generate agent: ${error.message}`, 1)
|
spinner.stop(`LLM failed to generate agent: ${error.message}`, 1)
|
||||||
if (isFullyNonInteractive) process.exit(1)
|
if (isFullyNonInteractive) process.exit(1)
|
||||||
throw new UI.CancelledError()
|
throw new UI.CancelledError()
|
||||||
|
|||||||
@@ -435,6 +435,9 @@ export const GithubRunCommand = effectCmd({
|
|||||||
const sessionSvc = yield* Session.Service
|
const sessionSvc = yield* Session.Service
|
||||||
const sessionShare = yield* SessionShare.Service
|
const sessionShare = yield* SessionShare.Service
|
||||||
const sessionPrompt = yield* SessionPrompt.Service
|
const sessionPrompt = yield* SessionPrompt.Service
|
||||||
|
const busSvc = yield* Bus.Service
|
||||||
|
const runLocalEffect = <A, E>(effect: Effect.Effect<A, E>) =>
|
||||||
|
Effect.runPromise(effect.pipe(Effect.provideService(InstanceRef, ctx)))
|
||||||
yield* Effect.promise(async () => {
|
yield* Effect.promise(async () => {
|
||||||
const isMock = args.token || args.event
|
const isMock = args.token || args.event
|
||||||
|
|
||||||
@@ -548,7 +551,7 @@ export const GithubRunCommand = effectCmd({
|
|||||||
|
|
||||||
// Setup opencode session
|
// Setup opencode session
|
||||||
const repoData = await fetchRepo()
|
const repoData = await fetchRepo()
|
||||||
session = await Effect.runPromise(
|
session = await runLocalEffect(
|
||||||
sessionSvc.create({
|
sessionSvc.create({
|
||||||
permission: [
|
permission: [
|
||||||
{
|
{
|
||||||
@@ -559,11 +562,11 @@ export const GithubRunCommand = effectCmd({
|
|||||||
],
|
],
|
||||||
}),
|
}),
|
||||||
)
|
)
|
||||||
subscribeSessionEvents()
|
await subscribeSessionEvents()
|
||||||
shareId = await (async () => {
|
shareId = await (async () => {
|
||||||
if (share === false) return
|
if (share === false) return
|
||||||
if (!share && repoData.data.private) return
|
if (!share && repoData.data.private) return
|
||||||
await Effect.runPromise(sessionShare.share(session.id))
|
await runLocalEffect(sessionShare.share(session.id))
|
||||||
return session.id.slice(-8)
|
return session.id.slice(-8)
|
||||||
})()
|
})()
|
||||||
console.log("opencode session", session.id)
|
console.log("opencode session", session.id)
|
||||||
@@ -870,7 +873,7 @@ export const GithubRunCommand = effectCmd({
|
|||||||
return { userPrompt: prompt, promptFiles: imgData }
|
return { userPrompt: prompt, promptFiles: imgData }
|
||||||
}
|
}
|
||||||
|
|
||||||
function subscribeSessionEvents() {
|
async function subscribeSessionEvents() {
|
||||||
const TOOL: Record<string, [string, string]> = {
|
const TOOL: Record<string, [string, string]> = {
|
||||||
todowrite: ["Todo", UI.Style.TEXT_WARNING_BOLD],
|
todowrite: ["Todo", UI.Style.TEXT_WARNING_BOLD],
|
||||||
bash: ["Shell", UI.Style.TEXT_DANGER_BOLD],
|
bash: ["Shell", UI.Style.TEXT_DANGER_BOLD],
|
||||||
@@ -893,7 +896,7 @@ export const GithubRunCommand = effectCmd({
|
|||||||
}
|
}
|
||||||
|
|
||||||
let text = ""
|
let text = ""
|
||||||
Bus.subscribe(MessageV2.Event.PartUpdated, (evt) => {
|
await runLocalEffect(busSvc.subscribeCallback(MessageV2.Event.PartUpdated, (evt) => {
|
||||||
if (evt.properties.part.sessionID !== session.id) return
|
if (evt.properties.part.sessionID !== session.id) return
|
||||||
//if (evt.properties.part.messageID === messageID) return
|
//if (evt.properties.part.messageID === messageID) return
|
||||||
const part = evt.properties.part
|
const part = evt.properties.part
|
||||||
@@ -919,7 +922,7 @@ export const GithubRunCommand = effectCmd({
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
}))
|
||||||
}
|
}
|
||||||
|
|
||||||
async function summarize(response: string) {
|
async function summarize(response: string) {
|
||||||
@@ -936,7 +939,7 @@ export const GithubRunCommand = effectCmd({
|
|||||||
async function chat(message: string, files: PromptFiles = []) {
|
async function chat(message: string, files: PromptFiles = []) {
|
||||||
console.log("Sending message to opencode...")
|
console.log("Sending message to opencode...")
|
||||||
|
|
||||||
return Effect.runPromise(
|
return runLocalEffect(
|
||||||
Effect.gen(function* () {
|
Effect.gen(function* () {
|
||||||
const prompt = sessionPrompt
|
const prompt = sessionPrompt
|
||||||
const result = yield* prompt.prompt({
|
const result = yield* prompt.prompt({
|
||||||
|
|||||||
Reference in New Issue
Block a user