-
Notifications
You must be signed in to change notification settings - Fork 37
Add Tasks panel infrastructure with type-safe IPC protocol #772
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
Open
EhabY
wants to merge
5
commits into
main
Choose a base branch
from
tasks/infrastructure
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| { | ||
| "name": "@repo/shared", | ||
| "version": "1.0.0", | ||
| "description": "Shared types and utilities for extension and webviews", | ||
| "private": true, | ||
| "type": "module", | ||
| "exports": { | ||
| ".": { | ||
| "types": "./src/index.ts", | ||
| "default": "./src/index.ts" | ||
| } | ||
| }, | ||
| "devDependencies": { | ||
| "typescript": "catalog:" | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| // IPC protocol types | ||
| export * from "./ipc/protocol"; | ||
|
|
||
| // Tasks types and API | ||
| export * from "./tasks/types"; | ||
| export * from "./tasks/api"; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| /** | ||
| * Type-safe IPC protocol for VS Code webview communication. | ||
| * | ||
| * Inspired by tRPC's approach: types are carried in a phantom `_types` property | ||
| * that exists only for TypeScript inference, not at runtime. | ||
| */ | ||
|
|
||
| // --- Message definitions --- | ||
|
|
||
| /** Request definition: params P, response R */ | ||
| export interface RequestDef<P = void, R = void> { | ||
| readonly method: string; | ||
| /** @internal Phantom types for inference - not present at runtime */ | ||
| readonly _types?: { params: P; response: R }; | ||
| } | ||
|
|
||
| /** Command definition: params P, no response */ | ||
| export interface CommandDef<P = void> { | ||
| readonly method: string; | ||
| /** @internal Phantom type for inference - not present at runtime */ | ||
| readonly _types?: { params: P }; | ||
| } | ||
|
|
||
| /** Notification definition: data D (extension to webview) */ | ||
| export interface NotificationDef<D = void> { | ||
| readonly method: string; | ||
| /** @internal Phantom type for inference - not present at runtime */ | ||
| readonly _types?: { data: D }; | ||
| } | ||
|
|
||
| // --- Factory functions --- | ||
|
|
||
| /** Define a request with typed params and response */ | ||
| export function defineRequest<P = void, R = void>( | ||
| method: string, | ||
| ): RequestDef<P, R> { | ||
| return { method } as RequestDef<P, R>; | ||
| } | ||
|
|
||
| /** Define a fire-and-forget command */ | ||
| export function defineCommand<P = void>(method: string): CommandDef<P> { | ||
| return { method } as CommandDef<P>; | ||
| } | ||
|
|
||
| /** Define a push notification (extension to webview) */ | ||
| export function defineNotification<D = void>( | ||
| method: string, | ||
| ): NotificationDef<D> { | ||
| return { method } as NotificationDef<D>; | ||
| } | ||
|
|
||
| // --- Wire format --- | ||
|
|
||
| /** Request from webview to extension */ | ||
| export interface IpcRequest<P = unknown> { | ||
| readonly requestId: string; | ||
| readonly method: string; | ||
| readonly params?: P; | ||
| } | ||
|
|
||
| /** Response from extension to webview */ | ||
| export interface IpcResponse<T = unknown> { | ||
| readonly requestId: string; | ||
| readonly method: string; | ||
| readonly success: boolean; | ||
| readonly data?: T; | ||
| readonly error?: string; | ||
| } | ||
|
|
||
| /** Push notification from extension to webview */ | ||
| export interface IpcNotification<D = unknown> { | ||
| readonly type: string; | ||
| readonly data?: D; | ||
| } | ||
|
|
||
| // --- Handler utilities --- | ||
|
|
||
| /** Extract params type from a request/command definition */ | ||
| export type ParamsOf<T> = T extends { _types?: { params: infer P } } ? P : void; | ||
|
|
||
| /** Extract response type from a request definition */ | ||
| export type ResponseOf<T> = T extends { _types?: { response: infer R } } | ||
| ? R | ||
| : void; | ||
|
|
||
| /** Type-safe request handler - infers params and return type from definition */ | ||
| export function requestHandler<P, R>( | ||
| _def: RequestDef<P, R>, | ||
| fn: (params: P) => Promise<R>, | ||
| ): (params: unknown) => Promise<unknown> { | ||
| return fn as (params: unknown) => Promise<unknown>; | ||
| } | ||
|
|
||
| /** Type-safe command handler - infers params type from definition */ | ||
| export function commandHandler<P>( | ||
| _def: CommandDef<P>, | ||
| fn: (params: P) => void | Promise<void>, | ||
| ): (params: unknown) => void | Promise<void> { | ||
| return fn as (params: unknown) => void | Promise<void>; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| /** | ||
| * Tasks API - Type-safe message definitions for the Tasks webview. | ||
| * | ||
| * Usage: | ||
| * ```tsx | ||
| * const ipc = useIpc(); | ||
| * const tasks = await ipc.request(TasksApi.getTasks); // Returns Task[] | ||
| * ipc.command(TasksApi.viewInCoder, { taskId: "..." }); // Fire-and-forget | ||
| * ``` | ||
| */ | ||
|
|
||
| import { | ||
| defineCommand, | ||
| defineNotification, | ||
| defineRequest, | ||
| } from "../ipc/protocol"; | ||
|
|
||
| import type { Task, TaskDetails, TaskLogEntry, TaskTemplate } from "./types"; | ||
|
|
||
| export interface InitResponse { | ||
| tasks: readonly Task[]; | ||
| templates: readonly TaskTemplate[]; | ||
| baseUrl: string; | ||
| tasksSupported: boolean; | ||
| } | ||
|
|
||
| const init = defineRequest<void, InitResponse>("init"); | ||
| const getTasks = defineRequest<void, Task[]>("getTasks"); | ||
| const getTemplates = defineRequest<void, TaskTemplate[]>("getTemplates"); | ||
| const getTask = defineRequest<{ taskId: string }, Task>("getTask"); | ||
| const getTaskDetails = defineRequest<{ taskId: string }, TaskDetails>( | ||
| "getTaskDetails", | ||
| ); | ||
|
|
||
| export interface CreateTaskParams { | ||
| templateVersionId: string; | ||
| prompt: string; | ||
| presetId?: string; | ||
| } | ||
| const createTask = defineRequest<CreateTaskParams, Task>("createTask"); | ||
|
|
||
| const deleteTask = defineRequest<{ taskId: string }, void>("deleteTask"); | ||
| const pauseTask = defineRequest<{ taskId: string }, void>("pauseTask"); | ||
| const resumeTask = defineRequest<{ taskId: string }, void>("resumeTask"); | ||
|
|
||
| const viewInCoder = defineCommand<{ taskId: string }>("viewInCoder"); | ||
| const viewLogs = defineCommand<{ taskId: string }>("viewLogs"); | ||
| const downloadLogs = defineCommand<{ taskId: string }>("downloadLogs"); | ||
| const sendTaskMessage = defineCommand<{ | ||
| taskId: string; | ||
| message: string; | ||
| }>("sendTaskMessage"); | ||
|
|
||
| const taskUpdated = defineNotification<Task>("taskUpdated"); | ||
| const tasksUpdated = defineNotification<Task[]>("tasksUpdated"); | ||
| const logsAppend = defineNotification<TaskLogEntry[]>("logsAppend"); | ||
| const refresh = defineNotification<void>("refresh"); | ||
| const showCreateForm = defineNotification<void>("showCreateForm"); | ||
|
|
||
| export const TasksApi = { | ||
| // Requests | ||
| init, | ||
| getTasks, | ||
| getTemplates, | ||
| getTask, | ||
| getTaskDetails, | ||
| createTask, | ||
| deleteTask, | ||
| pauseTask, | ||
| resumeTask, | ||
| // Commands | ||
| viewInCoder, | ||
| viewLogs, | ||
| downloadLogs, | ||
| sendTaskMessage, | ||
| // Notifications | ||
| taskUpdated, | ||
| tasksUpdated, | ||
| logsAppend, | ||
| refresh, | ||
| showCreateForm, | ||
| } as const; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.