2026-02-08 13:57:26 +09:00
|
|
|
import type { OpencodeClient } from "./types"
|
2026-05-08 20:41:05 +08:00
|
|
|
import type { DelegatedModelConfig } from "../../shared/model-resolution-types"
|
2026-03-11 16:40:30 +09:00
|
|
|
import { QUESTION_DENIED_SESSION_PERMISSION } from "../../shared/question-denied-session-permission"
|
2026-02-08 13:57:26 +09:00
|
|
|
|
|
|
|
|
export async function createSyncSession(
|
|
|
|
|
client: OpencodeClient,
|
2026-05-08 20:41:05 +08:00
|
|
|
input: {
|
|
|
|
|
parentSessionID: string
|
|
|
|
|
agentToUse: string
|
|
|
|
|
description: string
|
|
|
|
|
defaultDirectory: string
|
|
|
|
|
categoryModel?: DelegatedModelConfig
|
|
|
|
|
}
|
2026-02-08 13:57:26 +09:00
|
|
|
): Promise<{ ok: true; sessionID: string; parentDirectory: string } | { ok: false; error: string }> {
|
|
|
|
|
const parentSession = client.session.get
|
|
|
|
|
? await client.session.get({ path: { id: input.parentSessionID } }).catch(() => null)
|
|
|
|
|
: null
|
|
|
|
|
const parentDirectory = parentSession?.data?.directory ?? input.defaultDirectory
|
|
|
|
|
|
|
|
|
|
const createResult = await client.session.create({
|
|
|
|
|
body: {
|
|
|
|
|
parentID: input.parentSessionID,
|
|
|
|
|
title: `${input.description} (@${input.agentToUse} subagent)`,
|
2026-03-11 16:40:30 +09:00
|
|
|
permission: QUESTION_DENIED_SESSION_PERMISSION,
|
2026-05-08 20:41:05 +08:00
|
|
|
...(input.categoryModel
|
|
|
|
|
? {
|
|
|
|
|
model: {
|
|
|
|
|
id: input.categoryModel.modelID,
|
|
|
|
|
providerID: input.categoryModel.providerID,
|
|
|
|
|
...(input.categoryModel.variant ? { variant: input.categoryModel.variant } : {}),
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
: {}),
|
2026-02-23 02:42:38 +09:00
|
|
|
} as Record<string, unknown>,
|
2026-02-08 13:57:26 +09:00
|
|
|
query: {
|
|
|
|
|
directory: parentDirectory,
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
if (createResult.error) {
|
|
|
|
|
return { ok: false, error: `Failed to create session: ${createResult.error}` }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return { ok: true, sessionID: createResult.data.id, parentDirectory }
|
|
|
|
|
}
|