refactor(tui): migrate v2 calls to new client
This commit is contained in:
@@ -1,12 +1,13 @@
|
||||
import { describe, expect, test } from "bun:test"
|
||||
import Notifications from "../../../../src/feature-plugins/system/notifications"
|
||||
import type { PermissionRequest, QuestionRequest, Session, V2Event } from "@opencode-ai/sdk/v2"
|
||||
import type { OpenCodeEvent } from "@opencode-ai/client/promise"
|
||||
import type { PermissionRequest, QuestionRequest, Session } from "@opencode-ai/sdk/v2"
|
||||
import type { TuiAttentionNotifyInput } from "@opencode-ai/plugin/tui"
|
||||
import { createTuiPluginApi } from "../../../fixture/tui-plugin"
|
||||
|
||||
async function setup() {
|
||||
const notifications: TuiAttentionNotifyInput[] = []
|
||||
const handlers = new Map<V2Event["type"], ((event: V2Event) => void)[]>()
|
||||
const handlers = new Map<OpenCodeEvent["type"], ((event: OpenCodeEvent) => void)[]>()
|
||||
const session = (id: string, title: string, parentID?: string): Session => ({
|
||||
id,
|
||||
title,
|
||||
@@ -33,9 +34,12 @@ async function setup() {
|
||||
},
|
||||
},
|
||||
event: {
|
||||
on: <Type extends V2Event["type"]>(type: Type, handler: (event: Extract<V2Event, { type: Type }>) => void) => {
|
||||
on: <Type extends OpenCodeEvent["type"]>(
|
||||
type: Type,
|
||||
handler: (event: Extract<OpenCodeEvent, { type: Type }>) => void,
|
||||
) => {
|
||||
const list = handlers.get(type) ?? []
|
||||
const wrapped = handler as (event: V2Event) => void
|
||||
const wrapped = handler as (event: OpenCodeEvent) => void
|
||||
list.push(wrapped)
|
||||
handlers.set(type, list)
|
||||
return () => {
|
||||
@@ -59,7 +63,7 @@ async function setup() {
|
||||
|
||||
return {
|
||||
notifications,
|
||||
emit(event: V2Event) {
|
||||
emit(event: OpenCodeEvent) {
|
||||
for (const handler of handlers.get(event.type) ?? []) handler(event)
|
||||
},
|
||||
}
|
||||
@@ -73,7 +77,10 @@ function question(id: string, sessionID = "session"): QuestionRequest {
|
||||
}
|
||||
}
|
||||
|
||||
function form(id: string, sessionID = "session"): Extract<V2Event, { type: "form.created" }>["data"]["form"] {
|
||||
function form(
|
||||
id: string,
|
||||
sessionID = "session",
|
||||
): Extract<OpenCodeEvent, { type: "form.created" }>["data"]["form"] {
|
||||
return {
|
||||
id,
|
||||
sessionID,
|
||||
@@ -98,7 +105,7 @@ function durable(sessionID: string): { aggregateID: string; seq: number; version
|
||||
return { aggregateID: sessionID, seq: 0, version: 1 }
|
||||
}
|
||||
|
||||
function executionStarted(id: string, sessionID = "session"): V2Event {
|
||||
function executionStarted(id: string, sessionID = "session"): OpenCodeEvent {
|
||||
return {
|
||||
id,
|
||||
created: 0,
|
||||
@@ -108,7 +115,7 @@ function executionStarted(id: string, sessionID = "session"): V2Event {
|
||||
}
|
||||
}
|
||||
|
||||
function executionSucceeded(id: string, sessionID = "session"): V2Event {
|
||||
function executionSucceeded(id: string, sessionID = "session"): OpenCodeEvent {
|
||||
return {
|
||||
id,
|
||||
created: 0,
|
||||
@@ -118,7 +125,7 @@ function executionSucceeded(id: string, sessionID = "session"): V2Event {
|
||||
}
|
||||
}
|
||||
|
||||
function executionFailed(id: string, sessionID = "session"): V2Event {
|
||||
function executionFailed(id: string, sessionID = "session"): OpenCodeEvent {
|
||||
return {
|
||||
id,
|
||||
created: 0,
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
/** @jsxImportSource @opentui/solid */
|
||||
import { expect, test } from "bun:test"
|
||||
import { testRender } from "@opentui/solid"
|
||||
import type { V2Event } from "@opencode-ai/sdk/v2"
|
||||
import type { OpenCodeEvent } from "@opencode-ai/client/promise"
|
||||
import { SessionMessage } from "@opencode-ai/core/session/message"
|
||||
import { EventV2 } from "@opencode-ai/core/event"
|
||||
import { onMount } from "solid-js"
|
||||
@@ -20,7 +20,7 @@ async function wait(fn: () => boolean, timeout = 2000) {
|
||||
}
|
||||
}
|
||||
|
||||
function emitEvent(events: ReturnType<typeof createEventStream>, event: V2Event) {
|
||||
function emitEvent(events: ReturnType<typeof createEventStream>, event: OpenCodeEvent) {
|
||||
events.emit({ ...event, location: { directory } })
|
||||
}
|
||||
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
/** @jsxImportSource @opentui/solid */
|
||||
import { describe, expect, test } from "bun:test"
|
||||
import type { OpenCodeClient } from "@opencode-ai/client/promise"
|
||||
import type { OpenCodeClient, OpenCodeEvent } from "@opencode-ai/client/promise"
|
||||
import { testRender } from "@opentui/solid"
|
||||
import type { OpencodeClient, V2Event } from "@opencode-ai/sdk/v2"
|
||||
import type { OpencodeClient } from "@opencode-ai/sdk/v2"
|
||||
import { onMount } from "solid-js"
|
||||
import { ProjectProvider, useProject } from "../../../src/context/project"
|
||||
import { SDKProvider, useSDK } from "../../../src/context/sdk"
|
||||
@@ -21,14 +21,17 @@ async function wait(fn: () => boolean, timeout = 2000) {
|
||||
}
|
||||
}
|
||||
|
||||
function event(payload: V2Event, input: { directory: string; project?: string; workspace?: string }): V2Event {
|
||||
function event(
|
||||
payload: OpenCodeEvent,
|
||||
input: { directory: string; project?: string; workspace?: string },
|
||||
): OpenCodeEvent {
|
||||
return {
|
||||
...payload,
|
||||
location: { directory: input.directory, workspaceID: input.workspace },
|
||||
}
|
||||
}
|
||||
|
||||
function vcs(branch: string): V2Event {
|
||||
function vcs(branch: string): OpenCodeEvent {
|
||||
return {
|
||||
id: `evt_vcs_${branch}`,
|
||||
created: 0,
|
||||
@@ -39,7 +42,7 @@ function vcs(branch: string): V2Event {
|
||||
}
|
||||
}
|
||||
|
||||
function update(version: string): V2Event {
|
||||
function update(version: string): OpenCodeEvent {
|
||||
return {
|
||||
id: `evt_update_${version}`,
|
||||
created: 0,
|
||||
@@ -56,7 +59,7 @@ async function mount(
|
||||
) {
|
||||
const events = createEventStream()
|
||||
const calls = createFetch(undefined, events)
|
||||
const seen: V2Event[] = []
|
||||
const seen: OpenCodeEvent[] = []
|
||||
const workspaces: Array<string | undefined> = []
|
||||
let project!: ReturnType<typeof useProject>
|
||||
let sdk!: ReturnType<typeof useSDK>
|
||||
@@ -89,7 +92,7 @@ async function mount(
|
||||
}
|
||||
|
||||
function Probe(props: {
|
||||
seen: V2Event[]
|
||||
seen: OpenCodeEvent[]
|
||||
workspaces: Array<string | undefined>
|
||||
onReady: (ctx: { project: ReturnType<typeof useProject>; sdk: ReturnType<typeof useSDK> }) => void
|
||||
}) {
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
import { OpenCode } from "@opencode-ai/client/promise"
|
||||
import { OpenCode, type OpenCodeEvent } from "@opencode-ai/client/promise"
|
||||
import { createOpencodeClient } from "@opencode-ai/sdk/v2"
|
||||
import type { V2Event } from "@opencode-ai/sdk/v2"
|
||||
|
||||
export const worktree = "/tmp/opencode"
|
||||
export const directory = `${worktree}/packages/tui`
|
||||
@@ -51,7 +50,7 @@ export function createEventStream() {
|
||||
}
|
||||
|
||||
return {
|
||||
emit(event: V2Event) {
|
||||
emit(event: OpenCodeEvent) {
|
||||
send(v2, pending, event)
|
||||
},
|
||||
v2() {
|
||||
|
||||
Reference in New Issue
Block a user