Skip to content

Commit c373e22

Browse files
Start using pi
1 parent 35e76be commit c373e22

3 files changed

Lines changed: 103 additions & 0 deletions

File tree

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ The actual dotfiles for various tools
6363
| glide |
6464
| nvim |
6565
| ollama |
66+
| pi |
6667
| termux |
6768
| tmux |
6869
| tools |
@@ -104,6 +105,7 @@ Helpful automation for various tasks
104105
| nvim-plugins | |
105106
| nvims | |
106107
| passphrase | |
108+
| print-pi-session | |
107109
| profile-nvim | [demo](.tapes/profile-nvim.gif) |
108110
| reprint | |
109111
| ssh | |

pi/save-last-extension.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import type { ExtensionAPI } from "@mariozechner/pi-coding-agent";
2+
import { writeFile } from "node:fs/promises";
3+
import { join } from "node:path";
4+
5+
export default function (pi: ExtensionAPI) {
6+
pi.registerCommand("save-last", {
7+
description: "Export the most recent AI response to a txt file",
8+
handler: async (args, ctx) => {
9+
ctx.ui.notify(`Export started`, "success");
10+
const entries = ctx.sessionManager.getBranch();
11+
12+
let lastAssistantText = null;
13+
for (let i = entries.length - 1; i >= 0; i--) {
14+
const entry = entries[i];
15+
if (entry.type === "message" && entry.message.role !== "user") {
16+
const textParts = entry.message.content
17+
.filter(block => block.type === "text")
18+
.map(block => block.text);
19+
if (textParts.length > 0) {
20+
lastAssistantText = textParts.join("\n");
21+
break;
22+
}
23+
}
24+
}
25+
26+
if (!lastAssistantText) {
27+
ctx.ui.notify("No AI response found in this session.", "warn");
28+
return;
29+
}
30+
31+
const timestamp = new Date().toISOString().replace(/[:.]/g, "-");
32+
const filename = args.trim() || `ai-response-${timestamp}.txt`;
33+
const filepath = join(ctx.cwd, filename);
34+
35+
await writeFile(filepath, lastAssistantText, "utf8");
36+
ctx.ui.notify(`Exported to ${filename}`, "success");
37+
},
38+
});
39+
}

scripts/print-pi-session

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#!/usr/bin/env nu
2+
3+
# print-pi-session | bat --language md
4+
def main [] {
5+
select-session
6+
| get data
7+
| where type == message
8+
| get message
9+
| structure-messages
10+
| each { format-message }
11+
| to text
12+
}
13+
14+
def select-session [] {
15+
ls ~/.pi/agent/sessions/*/*.jsonl
16+
| get name
17+
| each {|file|
18+
open
19+
| lines
20+
| each { from json }
21+
| {
22+
file: $file
23+
data: $in
24+
}
25+
}
26+
| each {|thing|
27+
{
28+
cwd: ($thing.data | first | get cwd | str replace "/home/bolen/projects/" "")
29+
age: ($thing.data | first | get timestamp | into datetime | date humanize)
30+
first-prompt: ($thing.data | where type == message | get message | where role == user | first | get content.text.0)
31+
file: $thing.file
32+
data: $thing.data
33+
}
34+
}
35+
| input list --display {|thing|
36+
$"[($thing.age)] ($thing.cwd): ($thing.first-prompt)"
37+
}
38+
}
39+
40+
def structure-messages [] {
41+
each {|msg|
42+
{
43+
role: $msg.role
44+
content: ($msg | get content | flatten | where type == text | get text | to text)
45+
}
46+
}
47+
| where content != ""
48+
}
49+
50+
def format-message [] {
51+
$"(to-color $in.role)($in.role)(ansi reset): ($in.content)"
52+
}
53+
54+
def to-color [role] {
55+
{
56+
user: (ansi green)
57+
assistant: (ansi yellow)
58+
toolResult: (ansi blue)
59+
}
60+
| get -o $role
61+
| default (ansi default)
62+
}

0 commit comments

Comments
 (0)