@taylorsatula/pi-timestamp 0.2.0 → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +20 -7
- package/extensions/timestamp.ts +57 -15
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -12,13 +12,20 @@ They're injected poorly if they're that annoying.
|
|
|
12
12
|
[11:34pm - 9/1/26]
|
|
13
13
|
```
|
|
14
14
|
|
|
15
|
-
Injection happens only at **turn boundaries**, never mid-tool-loop
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
15
|
+
Injection happens only at **turn boundaries**, never mid-tool-loop, and handles both wire
|
|
16
|
+
formats the hook can deliver (the payload is the provider-serialized request body, which
|
|
17
|
+
differs per API):
|
|
18
|
+
|
|
19
|
+
- **Chat Completions / Anthropic** (conversation under `messages`): last message is a
|
|
20
|
+
**user message** → append the timestamp to its content. Last message is an
|
|
21
|
+
**assistant message with text content** (a vocalization between tool steps) → append
|
|
22
|
+
to its content. Last message is an assistant message carrying `tool_calls`, a tool
|
|
23
|
+
result, or an Anthropic user message containing a `tool_result` block (that API's
|
|
24
|
+
tool-result encoding) → skip.
|
|
25
|
+
- **OpenAI Responses API** (conversation under `input`, typed items): last item is a
|
|
26
|
+
user item → append to its content blocks. Last item is an assistant `message` item
|
|
27
|
+
(vocalization) → append to its `output_text` blocks. Last item is a `function_call` or
|
|
28
|
+
`function_call_output` item → skip.
|
|
22
29
|
|
|
23
30
|
## Why plain-text tail append (design notes)
|
|
24
31
|
|
|
@@ -35,6 +42,9 @@ Injection happens only at **turn boundaries**, never mid-tool-loop:
|
|
|
35
42
|
- **Content-shape aware.** String content concatenates directly; block-array content
|
|
36
43
|
(Anthropic-shaped) appends into or after the last text block; unknown shapes are left
|
|
37
44
|
untouched rather than guessed at.
|
|
45
|
+
- **Wire-format aware.** Chat Completions/Anthropic payloads (`messages`) and OpenAI
|
|
46
|
+
Responses payloads (`input`, typed items) are both handled; any other payload shape
|
|
47
|
+
passes through untouched.
|
|
38
48
|
|
|
39
49
|
## Installation
|
|
40
50
|
|
|
@@ -61,6 +71,9 @@ Or add to `~/.pi/agent/settings.json` under `packages`:
|
|
|
61
71
|
- Version 0.2.0 changed the mechanism (plain-text tail append replacing the synthetic
|
|
62
72
|
tool-call pair). Sessions started before the upgrade may still show one old-style pair
|
|
63
73
|
until restart.
|
|
74
|
+
- Version 0.2.1 fixed a crash on OpenAI Responses-API models (`before_provider_request`
|
|
75
|
+
payloads carry the conversation under `input`, not `messages`) and added
|
|
76
|
+
Anthropic `tool_result` skipping.
|
|
64
77
|
|
|
65
78
|
## Development
|
|
66
79
|
|
package/extensions/timestamp.ts
CHANGED
|
@@ -14,6 +14,15 @@ import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
|
|
|
14
14
|
// never after tool results (mid-loop churn).
|
|
15
15
|
// - Idempotent per request: a previously appended tag is stripped before the
|
|
16
16
|
// fresh one is written.
|
|
17
|
+
//
|
|
18
|
+
// Payload shapes (before_provider_request delivers the provider-serialized
|
|
19
|
+
// request body, which differs per API):
|
|
20
|
+
// - Chat Completions / Anthropic: conversation lives under `messages`.
|
|
21
|
+
// - OpenAI Responses API: conversation lives under `input`, with typed items
|
|
22
|
+
// (input_text / output_text content blocks, function_call and
|
|
23
|
+
// function_call_output tool traffic) instead of chat messages.
|
|
24
|
+
|
|
25
|
+
type Rec = Record<string, unknown>;
|
|
17
26
|
|
|
18
27
|
const TS_STRIP = /\n\[\d{1,2}:\d{2}(am|pm) - \d{1,2}\/\d{1,2}\/\d{2}\]\s*$/;
|
|
19
28
|
|
|
@@ -30,40 +39,73 @@ function formatTimestamp(): string {
|
|
|
30
39
|
return `[${hours}:${String(minutes).padStart(2, "0")}${ampm} - ${month}/${day}/${year}]`;
|
|
31
40
|
}
|
|
32
41
|
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
42
|
+
// Chat Completions / Anthropic shape: payload.messages
|
|
43
|
+
// user: { role: "user", content: string | blocks }
|
|
44
|
+
// assistant vocalization: { role: "assistant", content: string | blocks, no tool_calls }
|
|
45
|
+
// assistant tool call: { role: "assistant", tool_calls: [...] }
|
|
46
|
+
// Anthropic tool results arrive as user messages containing a tool_result
|
|
47
|
+
// content block; treat them as mid-loop traffic, not turn boundaries.
|
|
38
48
|
|
|
49
|
+
function chatShouldInject(last: Rec): boolean {
|
|
50
|
+
if (last.role === "user") {
|
|
51
|
+
const content = last.content;
|
|
52
|
+
if (Array.isArray(content) && content.some((b) => (b as Rec).type === "tool_result")) {
|
|
53
|
+
return false;
|
|
54
|
+
}
|
|
55
|
+
return true;
|
|
56
|
+
}
|
|
39
57
|
if (last.role === "assistant") {
|
|
40
58
|
const hasToolCalls = Array.isArray(last.tool_calls) && last.tool_calls.length > 0;
|
|
41
59
|
const hasContent = last.content != null && last.content !== "";
|
|
42
60
|
return !hasToolCalls && hasContent; // vocalization → inject; tool call → never
|
|
43
61
|
}
|
|
62
|
+
return false;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// OpenAI Responses shape: payload.input
|
|
66
|
+
// user: { role: "user", content: [{ type: "input_text", text }, ...] }
|
|
67
|
+
// vocalization: { type: "message", role: "assistant", content: [{ type: "output_text", ... }] }
|
|
68
|
+
// tool call: { type: "function_call", ... } / result: { type: "function_call_output", ... }
|
|
44
69
|
|
|
45
|
-
|
|
70
|
+
function responsesShouldInject(last: Rec): boolean {
|
|
71
|
+
if (last.role === "user") return true;
|
|
72
|
+
if (last.type === "message" && last.role === "assistant") {
|
|
73
|
+
return Array.isArray(last.content) && last.content.length > 0;
|
|
74
|
+
}
|
|
75
|
+
return false; // function_call / function_call_output → mid-loop, skip
|
|
46
76
|
}
|
|
47
77
|
|
|
48
78
|
export default function (pi: ExtensionAPI) {
|
|
49
79
|
pi.on("before_provider_request", (event) => {
|
|
50
|
-
const payload = event.payload as
|
|
51
|
-
const
|
|
52
|
-
|
|
80
|
+
const payload = event.payload as Rec;
|
|
81
|
+
const isResponses = Array.isArray(payload.input);
|
|
82
|
+
const items = isResponses
|
|
83
|
+
? (payload.input as Rec[])
|
|
84
|
+
: Array.isArray(payload.messages)
|
|
85
|
+
? (payload.messages as Rec[])
|
|
86
|
+
: undefined;
|
|
87
|
+
if (!items) return; // unknown payload shape → leave untouched
|
|
88
|
+
|
|
89
|
+
const last = items[items.length - 1];
|
|
90
|
+
if (!last) return;
|
|
91
|
+
if (!(isResponses ? responsesShouldInject(last) : chatShouldInject(last))) return;
|
|
53
92
|
|
|
54
93
|
const ts = `\n${formatTimestamp()}`;
|
|
55
|
-
const last = msgs[msgs.length - 1];
|
|
56
94
|
|
|
57
95
|
if (typeof last.content === "string") {
|
|
58
96
|
last.content = last.content.replace(TS_STRIP, "") + ts;
|
|
59
97
|
} else if (Array.isArray(last.content)) {
|
|
60
|
-
// Provider-shaped block arrays
|
|
61
|
-
//
|
|
62
|
-
const blocks = [...(last.content as
|
|
98
|
+
// Provider-shaped block arrays: normalize the last text-bearing block in
|
|
99
|
+
// place, or append a typed text block when none exists.
|
|
100
|
+
const blocks = [...(last.content as Rec[])];
|
|
63
101
|
const idx = blocks.length - 1;
|
|
64
|
-
const lastBlock = blocks[idx] as {
|
|
65
|
-
if (lastBlock &&
|
|
102
|
+
const lastBlock = blocks[idx] as { text?: unknown } | undefined;
|
|
103
|
+
if (lastBlock && typeof lastBlock.text === "string") {
|
|
66
104
|
blocks[idx] = { ...lastBlock, text: lastBlock.text.replace(TS_STRIP, "") + ts };
|
|
105
|
+
} else if (isResponses && last.role === "assistant") {
|
|
106
|
+
blocks.push({ type: "output_text", text: ts, annotations: [] });
|
|
107
|
+
} else if (isResponses) {
|
|
108
|
+
blocks.push({ type: "input_text", text: ts });
|
|
67
109
|
} else {
|
|
68
110
|
blocks.push({ type: "text", text: ts });
|
|
69
111
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@taylorsatula/pi-timestamp",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.1",
|
|
4
4
|
"description": "Timestamp injection extension for Pi — appends the current wall-clock time to the tail of user messages and vocalizations just-in-time (cache-safe, no synthetic tool calls), so the model always knows what time it is.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"pi-package"
|