Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

PiSessionCommands.ts

91 lines3.3 KB

1import type { ProviderSessionCommandInput, ProviderSessionCommandResult } from "@t3tools/contracts";
2import { resolveSpawnCommand } from "@t3tools/shared/shell";
3import * as Effect from "effect/Effect";
4import * as FileSystem from "effect/FileSystem";
5import * as Path from "effect/Path";
6import { ChildProcess } from "effect/unstable/process";
7​
8import { PiRpcError, piRecordString, type PiRpcConnection } from "./PiRpc.ts";
9import { spawnAndCollect } from "./providerSnapshot.ts";
10import { expandHomePathWith } from "../pathExpansion.ts";
11​
12/** Pi's TUI utilities need explicit RPC/client handling; they are not agent prompts. */
13export const runPiSessionCommand = Effect.fn("runPiSessionCommand")(function* (
14 input: ProviderSessionCommandInput,
15 connection: PiRpcConnection,
16 environment: NodeJS.ProcessEnv,
17 cwd: string | undefined,
18) {
19 if (input.command !== "export" && input.outputPath !== undefined) {
20 return yield* new PiRpcError({
21 operation: input.command,
22 detail: `/${input.command} does not accept arguments.`,
23 });
24 }
25 if (input.command === "copy") {
26 const data = yield* connection.request({ type: "get_last_assistant_text" });
27 const text = piRecordString(data, "text");
28 if (!text) {
29 return yield* new PiRpcError({ operation: "copy", detail: "No assistant response to copy" });
30 }
31 return { command: "copy", text } satisfies ProviderSessionCommandResult;
32 }
33​
34 const fs = yield* FileSystem.FileSystem;
35 const path = yield* Path.Path;
36 const directory = yield* fs.makeTempDirectoryScoped({ prefix: "t3-pi-export-" });
37 const outputPath =
38 input.command === "export" && input.outputPath
39 ? path.resolve(cwd ?? ".", expandHomePathWith(input.outputPath, path))
40 : path.join(directory, "session.html");
41 yield* connection.request({ type: "export_html", outputPath });
42​
43 if (input.command === "export") {
44 const html = yield* fs.readFileString(outputPath);
45 return {
46 command: "export",
47 fileName: path.basename(outputPath),
48 html,
49 ...(input.outputPath ? { outputPath } : {}),
50 } satisfies ProviderSessionCommandResult;
51 }
52​
53 // Pi's RPC has no share operation. Match its unlisted-gist sharing path,
54 // using the server's gh authentication rather than the client's credentials.
55 const launch = yield* resolveSpawnCommand(
56 "gh",
57 ["gist", "create", "--public=false", outputPath],
58 {
59 env: environment,
60 },
61 );
62 const result = yield* spawnAndCollect(
63 "gh",
64 ChildProcess.make(launch.command, launch.args, {
65 env: environment,
66 shell: launch.shell,
67 cwd,
68 }),
69 ).pipe(Effect.timeout("60 seconds"));
70 if (result.code !== 0) {
71 const stderr = result.stderr.trim();
72 return yield* new PiRpcError({
73 operation: "share",
74 detail: "GitHub CLI sharing failed",
75 ...(stderr.length > 0 ? { cause: new Error(stderr) } : {}),
76 });
77 }
78 const match = /^https:\/\/gist\.github\.com\/(?:[^/\s]+\/)?([\da-f]+)\/?$/i.exec(
79 result.stdout.trim(),
80 );
81 if (!match?.[1]) {
82 return yield* new PiRpcError({
83 operation: "share",
84 detail: "GitHub CLI returned an invalid gist URL",
85 });
86 }
87 return {
88 command: "share",
89 url: `${environment.PI_SHARE_VIEWER_URL || "https://pi.dev/session/"}#${match[1]}`,
90 } satisfies ProviderSessionCommandResult;
91});