Add websocket transport contracts and app bootstrap API
Co-authored-by: Theo Browne <[email protected]>
This commit is contained in:
1 parent
4d3ad5bdc7
commit
6614b93ccc
4 files changed
+139
-21
No files matched your search
@@ -4,3 +4,4 @@ export * from "./agent";
|
||||
export * from "./terminal";
|
||||
export * from "./provider";
|
||||
export * from "./model";
|
||||
export * from "./ws";
|
||||
@@ -1,6 +1,7 @@
|
||||
import type { AgentConfig, AgentExit, OutputChunk } from "./agent";
|
||||
import type {
|
||||
ProviderEvent,
|
||||
ProviderKind,
|
||||
ProviderInterruptTurnInput,
|
||||
ProviderRespondToRequestInput,
|
||||
ProviderSendTurnInput,
|
||||
@@ -19,29 +20,18 @@ export const EDITORS = [
|
||||
|
||||
export type EditorId = (typeof EDITORS)[number]["id"];
|
||||
|
||||
export const IPC_CHANNELS = {
|
||||
todosList: "todos:list",
|
||||
todosAdd: "todos:add",
|
||||
todosToggle: "todos:toggle",
|
||||
todosRemove: "todos:remove",
|
||||
dialogPickFolder: "dialog:pick-folder",
|
||||
terminalRun: "terminal:run",
|
||||
agentSpawn: "agent:spawn",
|
||||
agentKill: "agent:kill",
|
||||
agentWrite: "agent:write",
|
||||
agentOutput: "agent:output",
|
||||
agentExit: "agent:exit",
|
||||
providerSessionStart: "provider:session:start",
|
||||
providerTurnStart: "provider:turn:start",
|
||||
providerTurnInterrupt: "provider:turn:interrupt",
|
||||
providerSessionStop: "provider:session:stop",
|
||||
providerSessionList: "provider:session:list",
|
||||
providerRequestRespond: "provider:request:respond",
|
||||
providerEvent: "provider:event",
|
||||
shellOpenInEditor: "shell:open-in-editor",
|
||||
} as const;
|
||||
export interface AppBootstrapResult {
|
||||
launchCwd: string;
|
||||
projectName: string;
|
||||
provider: ProviderKind;
|
||||
model: string;
|
||||
session: ProviderSession;
|
||||
}
|
||||
|
||||
export interface NativeApi {
|
||||
app: {
|
||||
bootstrap: () => Promise<AppBootstrapResult>;
|
||||
};
|
||||
todos: {
|
||||
list: () => Promise<Todo[]>;
|
||||
add: (input: NewTodoInput) => Promise<Todo[]>;
|
||||
|
||||
@@ -0,0 +1,53 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
import {
|
||||
WS_EVENT_CHANNELS,
|
||||
wsClientMessageSchema,
|
||||
wsServerMessageSchema,
|
||||
} from "./ws";
|
||||
|
||||
describe("wsClientMessageSchema", () => {
|
||||
it("accepts request messages", () => {
|
||||
const parsed = wsClientMessageSchema.parse({
|
||||
type: "request",
|
||||
id: "req-1",
|
||||
method: "providers.startSession",
|
||||
params: { provider: "codex" },
|
||||
});
|
||||
|
||||
expect(parsed.method).toBe("providers.startSession");
|
||||
});
|
||||
});
|
||||
|
||||
describe("wsServerMessageSchema", () => {
|
||||
it("accepts successful response messages", () => {
|
||||
const parsed = wsServerMessageSchema.parse({
|
||||
type: "response",
|
||||
id: "req-1",
|
||||
ok: true,
|
||||
result: { sessionId: "sess-1" },
|
||||
});
|
||||
|
||||
expect(parsed.type).toBe("response");
|
||||
});
|
||||
|
||||
it("requires errors for failed responses", () => {
|
||||
expect(() =>
|
||||
wsServerMessageSchema.parse({
|
||||
type: "response",
|
||||
id: "req-1",
|
||||
ok: false,
|
||||
}),
|
||||
).toThrow();
|
||||
});
|
||||
|
||||
it("accepts typed event channels", () => {
|
||||
const parsed = wsServerMessageSchema.parse({
|
||||
type: "event",
|
||||
channel: WS_EVENT_CHANNELS.providerEvent,
|
||||
payload: { id: "evt-1" },
|
||||
});
|
||||
|
||||
expect(parsed.type).toBe("event");
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,74 @@
|
||||
import { z } from "zod";
|
||||
|
||||
export const WS_EVENT_CHANNELS = {
|
||||
providerEvent: "provider:event",
|
||||
agentOutput: "agent:output",
|
||||
agentExit: "agent:exit",
|
||||
} as const;
|
||||
|
||||
const wsRequestSchema = z.object({
|
||||
type: z.literal("request"),
|
||||
id: z.string().min(1),
|
||||
method: z.string().min(1),
|
||||
params: z.unknown().optional(),
|
||||
});
|
||||
|
||||
const wsResponseErrorSchema = z.object({
|
||||
code: z.string().min(1),
|
||||
message: z.string().min(1),
|
||||
});
|
||||
|
||||
const wsResponseSchema = z
|
||||
.object({
|
||||
type: z.literal("response"),
|
||||
id: z.string().min(1),
|
||||
ok: z.boolean(),
|
||||
result: z.unknown().optional(),
|
||||
error: wsResponseErrorSchema.optional(),
|
||||
})
|
||||
.superRefine((value, ctx) => {
|
||||
if (value.ok && value.error) {
|
||||
ctx.addIssue({
|
||||
code: z.ZodIssueCode.custom,
|
||||
message: "response.error must be undefined when ok=true",
|
||||
});
|
||||
}
|
||||
|
||||
if (!value.ok && !value.error) {
|
||||
ctx.addIssue({
|
||||
code: z.ZodIssueCode.custom,
|
||||
message: "response.error is required when ok=false",
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
const wsEventSchema = z.object({
|
||||
type: z.literal("event"),
|
||||
channel: z.enum([
|
||||
WS_EVENT_CHANNELS.providerEvent,
|
||||
WS_EVENT_CHANNELS.agentOutput,
|
||||
WS_EVENT_CHANNELS.agentExit,
|
||||
]),
|
||||
payload: z.unknown(),
|
||||
});
|
||||
|
||||
const wsHelloSchema = z.object({
|
||||
type: z.literal("hello"),
|
||||
version: z.literal(1),
|
||||
launchCwd: z.string().min(1),
|
||||
});
|
||||
|
||||
export const wsClientMessageSchema = wsRequestSchema;
|
||||
export const wsServerMessageSchema = z.union([
|
||||
wsResponseSchema,
|
||||
wsEventSchema,
|
||||
wsHelloSchema,
|
||||
]);
|
||||
|
||||
export type WsEventChannel = z.infer<typeof wsEventSchema>["channel"];
|
||||
export type WsRequestMessage = z.infer<typeof wsRequestSchema>;
|
||||
export type WsResponseMessage = z.infer<typeof wsResponseSchema>;
|
||||
export type WsEventMessage = z.infer<typeof wsEventSchema>;
|
||||
export type WsHelloMessage = z.infer<typeof wsHelloSchema>;
|
||||
export type WsClientMessage = z.infer<typeof wsClientMessageSchema>;
|
||||
export type WsServerMessage = z.infer<typeof wsServerMessageSchema>;
|
||||
Reference in new issue
Block a user