@fragno-dev/pi-harness 0.0.0-stage → 0.0.3
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/LICENSE.md +16 -0
- package/README.md +181 -2
- package/dist/browser/agent-harness-event-protocol-CuKubox1.js +666 -0
- package/dist/browser/agent-harness-event-protocol-CuKubox1.js.map +1 -0
- package/dist/browser/agent-harness-event-protocol-d6mH_9n3.d.ts +261 -0
- package/dist/browser/agent-harness-event-protocol-d6mH_9n3.d.ts.map +1 -0
- package/dist/browser/client/clients.d.ts +2 -0
- package/dist/browser/client/clients.js +2 -0
- package/dist/browser/client/pi-workflow-emission-stream.d.ts +13 -0
- package/dist/browser/client/pi-workflow-emission-stream.d.ts.map +1 -0
- package/dist/browser/client/pi-workflow-emission-stream.js +29 -0
- package/dist/browser/client/pi-workflow-emission-stream.js.map +1 -0
- package/dist/browser/client/react.d.ts +147 -0
- package/dist/browser/client/react.d.ts.map +1 -0
- package/dist/browser/client/react.js +187 -0
- package/dist/browser/client/react.js.map +1 -0
- package/dist/browser/client/solid.d.ts +150 -0
- package/dist/browser/client/solid.d.ts.map +1 -0
- package/dist/browser/client/solid.js +130 -0
- package/dist/browser/client/solid.js.map +1 -0
- package/dist/browser/client/svelte.d.ts +150 -0
- package/dist/browser/client/svelte.d.ts.map +1 -0
- package/dist/browser/client/svelte.js +132 -0
- package/dist/browser/client/svelte.js.map +1 -0
- package/dist/browser/client/vanilla.d.ts +177 -0
- package/dist/browser/client/vanilla.d.ts.map +1 -0
- package/dist/browser/client/vanilla.js +159 -0
- package/dist/browser/client/vanilla.js.map +1 -0
- package/dist/browser/client/vue.d.ts +150 -0
- package/dist/browser/client/vue.d.ts.map +1 -0
- package/dist/browser/client/vue.js +128 -0
- package/dist/browser/client/vue.js.map +1 -0
- package/dist/browser/client/workflow-lofi-session-projection.d.ts +17 -0
- package/dist/browser/client/workflow-lofi-session-projection.d.ts.map +1 -0
- package/dist/browser/client/workflow-lofi-session-projection.js +107 -0
- package/dist/browser/client/workflow-lofi-session-projection.js.map +1 -0
- package/dist/browser/clients-DYXj19je.d.ts +247 -0
- package/dist/browser/clients-DYXj19je.d.ts.map +1 -0
- package/dist/browser/clients-pIAk6ClP.js +1853 -0
- package/dist/browser/clients-pIAk6ClP.js.map +1 -0
- package/dist/browser/pi/context-usage.d.ts +20 -0
- package/dist/browser/pi/context-usage.d.ts.map +1 -0
- package/dist/browser/pi/context-usage.js +84 -0
- package/dist/browser/pi/context-usage.js.map +1 -0
- package/dist/browser/pi/harness/agent-harness-event-protocol.d.ts +2 -0
- package/dist/browser/pi/harness/agent-harness-event-protocol.js +2 -0
- package/dist/browser/pi/route-schemas.d.ts +2 -0
- package/dist/browser/pi/route-schemas.js +130 -0
- package/dist/browser/pi/route-schemas.js.map +1 -0
- package/dist/browser/pi/workflow-session-projection.d.ts +2 -0
- package/dist/browser/pi/workflow-session-projection.js +2 -0
- package/dist/browser/route-schemas-DQB5ZR1E.d.ts +109 -0
- package/dist/browser/route-schemas-DQB5ZR1E.d.ts.map +1 -0
- package/dist/browser/types-8q4RWFXT.js +23 -0
- package/dist/browser/types-8q4RWFXT.js.map +1 -0
- package/dist/browser/types-Dk856kV1.d.ts +527 -0
- package/dist/browser/types-Dk856kV1.d.ts.map +1 -0
- package/dist/browser/workflow-session-projection-CJ4MUkpJ.js +443 -0
- package/dist/browser/workflow-session-projection-CJ4MUkpJ.js.map +1 -0
- package/dist/browser/workflow-session-projection-Dj_a79GF.d.ts +93 -0
- package/dist/browser/workflow-session-projection-Dj_a79GF.d.ts.map +1 -0
- package/dist/node/pi/compact-command-outcome-projection.js +41 -0
- package/dist/node/pi/compact-command-outcome-projection.js.map +1 -0
- package/dist/node/pi/context-usage.d.ts +20 -0
- package/dist/node/pi/context-usage.d.ts.map +1 -0
- package/dist/node/pi/context-usage.js +84 -0
- package/dist/node/pi/context-usage.js.map +1 -0
- package/dist/node/pi/definition.d.ts +535 -0
- package/dist/node/pi/definition.d.ts.map +1 -0
- package/dist/node/pi/definition.js +71 -0
- package/dist/node/pi/definition.js.map +1 -0
- package/dist/node/pi/factory.d.ts +142 -0
- package/dist/node/pi/factory.d.ts.map +1 -0
- package/dist/node/pi/factory.js +22 -0
- package/dist/node/pi/factory.js.map +1 -0
- package/dist/node/pi/harness/agent-harness-event-protocol.d.ts +261 -0
- package/dist/node/pi/harness/agent-harness-event-protocol.d.ts.map +1 -0
- package/dist/node/pi/harness/agent-harness-event-protocol.js +588 -0
- package/dist/node/pi/harness/agent-harness-event-protocol.js.map +1 -0
- package/dist/node/pi/harness/json-value.js +82 -0
- package/dist/node/pi/harness/json-value.js.map +1 -0
- package/dist/node/pi/harness/pi-operation-completed.d.ts +16 -0
- package/dist/node/pi/harness/pi-operation-completed.d.ts.map +1 -0
- package/dist/node/pi/harness/pi-operation-completed.js +71 -0
- package/dist/node/pi/harness/pi-operation-completed.js.map +1 -0
- package/dist/node/pi/harness/session-storage.d.ts +62 -0
- package/dist/node/pi/harness/session-storage.d.ts.map +1 -0
- package/dist/node/pi/harness/session-storage.js +174 -0
- package/dist/node/pi/harness/session-storage.js.map +1 -0
- package/dist/node/pi/pi-jsonl-export.d.ts +11 -0
- package/dist/node/pi/pi-jsonl-export.d.ts.map +1 -0
- package/dist/node/pi/pi-jsonl-export.js +31 -0
- package/dist/node/pi/pi-jsonl-export.js.map +1 -0
- package/dist/node/pi/route-schemas.d.ts +109 -0
- package/dist/node/pi/route-schemas.d.ts.map +1 -0
- package/dist/node/pi/route-schemas.js +130 -0
- package/dist/node/pi/route-schemas.js.map +1 -0
- package/dist/node/pi/session-command-protocol.d.ts +14 -0
- package/dist/node/pi/session-command-protocol.d.ts.map +1 -0
- package/dist/node/pi/session-command-protocol.js +25 -0
- package/dist/node/pi/session-command-protocol.js.map +1 -0
- package/dist/node/pi/session-entry-projection.js +79 -0
- package/dist/node/pi/session-entry-projection.js.map +1 -0
- package/dist/node/pi/skills.d.ts +70 -0
- package/dist/node/pi/skills.d.ts.map +1 -0
- package/dist/node/pi/skills.js +150 -0
- package/dist/node/pi/skills.js.map +1 -0
- package/dist/node/pi/tools.d.ts +12 -0
- package/dist/node/pi/tools.d.ts.map +1 -0
- package/dist/node/pi/tools.js +8 -0
- package/dist/node/pi/tools.js.map +1 -0
- package/dist/node/pi/types.d.ts +165 -0
- package/dist/node/pi/types.d.ts.map +1 -0
- package/dist/node/pi/types.js +49 -0
- package/dist/node/pi/types.js.map +1 -0
- package/dist/node/pi/wait-for-command-step.js +46 -0
- package/dist/node/pi/wait-for-command-step.js.map +1 -0
- package/dist/node/pi/workflow-session-live-projection.js +220 -0
- package/dist/node/pi/workflow-session-live-projection.js.map +1 -0
- package/dist/node/pi/workflow-session-projection.d.ts +84 -0
- package/dist/node/pi/workflow-session-projection.d.ts.map +1 -0
- package/dist/node/pi/workflow-session-projection.js +100 -0
- package/dist/node/pi/workflow-session-projection.js.map +1 -0
- package/dist/node/pi/workflows/interactive-chat-workflow.d.ts +37 -0
- package/dist/node/pi/workflows/interactive-chat-workflow.d.ts.map +1 -0
- package/dist/node/pi/workflows/interactive-chat-workflow.js +219 -0
- package/dist/node/pi/workflows/interactive-chat-workflow.js.map +1 -0
- package/dist/node/pi/workflows/workflow-agent-harness.d.ts +142 -0
- package/dist/node/pi/workflows/workflow-agent-harness.d.ts.map +1 -0
- package/dist/node/pi/workflows/workflow-agent-harness.js +450 -0
- package/dist/node/pi/workflows/workflow-agent-harness.js.map +1 -0
- package/dist/node/routes.d.ts +631 -0
- package/dist/node/routes.d.ts.map +1 -0
- package/dist/node/routes.js +355 -0
- package/dist/node/routes.js.map +1 -0
- package/dist/node/schema.d.ts +5 -0
- package/dist/node/schema.d.ts.map +1 -0
- package/dist/node/schema.js +7 -0
- package/dist/node/schema.js.map +1 -0
- package/package.json +175 -3
|
@@ -0,0 +1,588 @@
|
|
|
1
|
+
import { assertPiHarnessJsonValue, freezePiHarnessJsonValue, snapshotPiHarnessJsonValue } from "./json-value.js";
|
|
2
|
+
//#region src/pi/harness/agent-harness-event-protocol.ts
|
|
3
|
+
const piToolCallArgumentsText = (toolCall) => {
|
|
4
|
+
const partialJson = toolCall.partialJson;
|
|
5
|
+
return typeof partialJson === "string" ? partialJson : void 0;
|
|
6
|
+
};
|
|
7
|
+
const assistantMessageMetadata = ({ usage, stopReason, errorMessage, timestamp }) => ({
|
|
8
|
+
usage,
|
|
9
|
+
stopReason,
|
|
10
|
+
errorMessage,
|
|
11
|
+
timestamp
|
|
12
|
+
});
|
|
13
|
+
const compactAssistantMessageMetadataUpdate = (previous, next) => {
|
|
14
|
+
const update = {
|
|
15
|
+
...previous.usage.input === next.usage.input && previous.usage.output === next.usage.output && previous.usage.cacheRead === next.usage.cacheRead && previous.usage.cacheWrite === next.usage.cacheWrite && previous.usage.cacheWrite1h === next.usage.cacheWrite1h && previous.usage.reasoning === next.usage.reasoning && previous.usage.totalTokens === next.usage.totalTokens && previous.usage.cost.input === next.usage.cost.input && previous.usage.cost.output === next.usage.cost.output && previous.usage.cost.cacheRead === next.usage.cost.cacheRead && previous.usage.cost.cacheWrite === next.usage.cost.cacheWrite && previous.usage.cost.total === next.usage.cost.total ? {} : { usage: next.usage },
|
|
16
|
+
...previous.stopReason === next.stopReason ? {} : { stopReason: next.stopReason },
|
|
17
|
+
...previous.errorMessage === next.errorMessage ? {} : { errorMessage: next.errorMessage ?? null },
|
|
18
|
+
...previous.timestamp === next.timestamp ? {} : { timestamp: next.timestamp }
|
|
19
|
+
};
|
|
20
|
+
return Object.keys(update).length === 0 ? void 0 : update;
|
|
21
|
+
};
|
|
22
|
+
const assistantMessageWithMetadataUpdate = (message, update) => ({
|
|
23
|
+
...message,
|
|
24
|
+
...update.usage === void 0 ? {} : { usage: update.usage },
|
|
25
|
+
...update.stopReason === void 0 ? {} : { stopReason: update.stopReason },
|
|
26
|
+
...update.errorMessage === void 0 ? {} : update.errorMessage === null ? { errorMessage: void 0 } : { errorMessage: update.errorMessage },
|
|
27
|
+
...update.timestamp === void 0 ? {} : { timestamp: update.timestamp }
|
|
28
|
+
});
|
|
29
|
+
const streamedMessageUpdateEvent = (event) => {
|
|
30
|
+
switch (event.type) {
|
|
31
|
+
case "start":
|
|
32
|
+
case "done":
|
|
33
|
+
case "error": throw new Error("PI_HARNESS_EVENT_PROTOCOL_NON_STREAMING_MESSAGE_UPDATE");
|
|
34
|
+
case "text_start":
|
|
35
|
+
case "text_delta":
|
|
36
|
+
case "text_end":
|
|
37
|
+
case "thinking_start":
|
|
38
|
+
case "thinking_delta":
|
|
39
|
+
case "thinking_end":
|
|
40
|
+
case "toolcall_start":
|
|
41
|
+
case "toolcall_delta":
|
|
42
|
+
case "toolcall_end": return event;
|
|
43
|
+
}
|
|
44
|
+
throw new Error("PI_HARNESS_EVENT_PROTOCOL_UNKNOWN_MESSAGE_UPDATE");
|
|
45
|
+
};
|
|
46
|
+
const thinkingContentFromPartial = (event) => {
|
|
47
|
+
const content = event.partial.content[event.contentIndex];
|
|
48
|
+
if (content?.type !== "thinking") throw new Error("PI_HARNESS_EVENT_PROTOCOL_THINKING_UPDATE_WITHOUT_THINKING_BLOCK");
|
|
49
|
+
return content;
|
|
50
|
+
};
|
|
51
|
+
const toolCallFromPartial = (event) => {
|
|
52
|
+
const content = event.partial.content[event.contentIndex];
|
|
53
|
+
if (content?.type !== "toolCall") throw new Error("PI_HARNESS_EVENT_PROTOCOL_TOOL_CALL_UPDATE_WITHOUT_TOOL_CALL_BLOCK");
|
|
54
|
+
return content;
|
|
55
|
+
};
|
|
56
|
+
const thinkingMetadataFields = (content) => content.redacted === void 0 ? {} : { redacted: content.redacted };
|
|
57
|
+
const projectToolCall = ({ thoughtSignature: _thoughtSignature, ...toolCall }) => toolCall;
|
|
58
|
+
const projectAssistantMessage = ({ api: _api, provider: _provider, model: _model, responseModel: _responseModel, responseId: _responseId, diagnostics: _diagnostics, content, ...message }) => ({
|
|
59
|
+
...message,
|
|
60
|
+
content: content.map((block) => {
|
|
61
|
+
switch (block.type) {
|
|
62
|
+
case "text": {
|
|
63
|
+
const { textSignature: _textSignature, ...text } = block;
|
|
64
|
+
return text;
|
|
65
|
+
}
|
|
66
|
+
case "thinking": {
|
|
67
|
+
const { thinkingSignature: _thinkingSignature, ...thinking } = block;
|
|
68
|
+
return thinking;
|
|
69
|
+
}
|
|
70
|
+
case "toolCall": return projectToolCall(block);
|
|
71
|
+
}
|
|
72
|
+
throw new Error("PI_HARNESS_FRONTEND_PROTOCOL_UNKNOWN_ASSISTANT_CONTENT");
|
|
73
|
+
})
|
|
74
|
+
});
|
|
75
|
+
const projectAgentMessage = (message) => message.role === "assistant" ? projectAssistantMessage(message) : message;
|
|
76
|
+
const compactAssistantMessageEvent = (event) => {
|
|
77
|
+
switch (event.type) {
|
|
78
|
+
case "text_start": return {
|
|
79
|
+
type: event.type,
|
|
80
|
+
contentIndex: event.contentIndex
|
|
81
|
+
};
|
|
82
|
+
case "text_delta": return {
|
|
83
|
+
type: event.type,
|
|
84
|
+
contentIndex: event.contentIndex,
|
|
85
|
+
delta: event.delta
|
|
86
|
+
};
|
|
87
|
+
case "text_end": return {
|
|
88
|
+
type: event.type,
|
|
89
|
+
contentIndex: event.contentIndex,
|
|
90
|
+
content: event.content
|
|
91
|
+
};
|
|
92
|
+
case "thinking_start": {
|
|
93
|
+
const content = thinkingContentFromPartial(event);
|
|
94
|
+
return {
|
|
95
|
+
type: event.type,
|
|
96
|
+
contentIndex: event.contentIndex,
|
|
97
|
+
...thinkingMetadataFields(content)
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
case "thinking_delta": {
|
|
101
|
+
const content = thinkingContentFromPartial(event);
|
|
102
|
+
return {
|
|
103
|
+
type: event.type,
|
|
104
|
+
contentIndex: event.contentIndex,
|
|
105
|
+
delta: event.delta,
|
|
106
|
+
...thinkingMetadataFields(content)
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
case "thinking_end": {
|
|
110
|
+
const content = thinkingContentFromPartial(event);
|
|
111
|
+
return {
|
|
112
|
+
type: event.type,
|
|
113
|
+
contentIndex: event.contentIndex,
|
|
114
|
+
content: event.content,
|
|
115
|
+
...thinkingMetadataFields(content)
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
case "toolcall_start": return {
|
|
119
|
+
type: event.type,
|
|
120
|
+
contentIndex: event.contentIndex,
|
|
121
|
+
toolCall: projectToolCall(toolCallFromPartial(event))
|
|
122
|
+
};
|
|
123
|
+
case "toolcall_delta": return {
|
|
124
|
+
type: event.type,
|
|
125
|
+
contentIndex: event.contentIndex,
|
|
126
|
+
delta: event.delta
|
|
127
|
+
};
|
|
128
|
+
case "toolcall_end": return {
|
|
129
|
+
type: event.type,
|
|
130
|
+
contentIndex: event.contentIndex,
|
|
131
|
+
toolCall: projectToolCall(event.toolCall)
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
throw new Error("PI_HARNESS_EVENT_PROTOCOL_UNKNOWN_STREAMING_MESSAGE_UPDATE");
|
|
135
|
+
};
|
|
136
|
+
function isPrimitiveOnlyMessageUpdate(event) {
|
|
137
|
+
if (event.type !== "message_update") return false;
|
|
138
|
+
if (event.metadata !== void 0) return false;
|
|
139
|
+
switch (event.update.type) {
|
|
140
|
+
case "text_start":
|
|
141
|
+
case "text_delta":
|
|
142
|
+
case "text_end":
|
|
143
|
+
case "thinking_start":
|
|
144
|
+
case "thinking_delta":
|
|
145
|
+
case "thinking_end":
|
|
146
|
+
case "toolcall_delta": return true;
|
|
147
|
+
case "toolcall_start":
|
|
148
|
+
case "toolcall_end": return false;
|
|
149
|
+
}
|
|
150
|
+
throw new Error("PI_HARNESS_EVENT_PROTOCOL_UNKNOWN_COMPACT_MESSAGE_UPDATE");
|
|
151
|
+
}
|
|
152
|
+
const streamedAssistantMessageEvent = (update, partial) => {
|
|
153
|
+
switch (update.type) {
|
|
154
|
+
case "text_start":
|
|
155
|
+
case "thinking_start":
|
|
156
|
+
case "toolcall_start": return {
|
|
157
|
+
type: update.type,
|
|
158
|
+
contentIndex: update.contentIndex,
|
|
159
|
+
partial
|
|
160
|
+
};
|
|
161
|
+
case "text_delta":
|
|
162
|
+
case "thinking_delta":
|
|
163
|
+
case "toolcall_delta": return {
|
|
164
|
+
type: update.type,
|
|
165
|
+
contentIndex: update.contentIndex,
|
|
166
|
+
delta: update.delta,
|
|
167
|
+
partial
|
|
168
|
+
};
|
|
169
|
+
case "text_end":
|
|
170
|
+
case "thinking_end": return {
|
|
171
|
+
type: update.type,
|
|
172
|
+
contentIndex: update.contentIndex,
|
|
173
|
+
content: update.content,
|
|
174
|
+
partial
|
|
175
|
+
};
|
|
176
|
+
case "toolcall_end": return {
|
|
177
|
+
type: update.type,
|
|
178
|
+
contentIndex: update.contentIndex,
|
|
179
|
+
toolCall: update.toolCall,
|
|
180
|
+
partial
|
|
181
|
+
};
|
|
182
|
+
}
|
|
183
|
+
throw new Error("PI_HARNESS_EVENT_PROTOCOL_UNKNOWN_COMPACT_MESSAGE_UPDATE");
|
|
184
|
+
};
|
|
185
|
+
const applyCompactAssistantMessageEvent = (message, update, toolCallJsonByContentIndex) => {
|
|
186
|
+
if (update.contentIndex > message.content.length) throw new Error(`PI_HARNESS_EVENT_PROTOCOL_SPARSE_CONTENT_INDEX:${update.contentIndex}`);
|
|
187
|
+
switch (update.type) {
|
|
188
|
+
case "text_start":
|
|
189
|
+
message.content[update.contentIndex] = {
|
|
190
|
+
type: "text",
|
|
191
|
+
text: ""
|
|
192
|
+
};
|
|
193
|
+
return;
|
|
194
|
+
case "text_delta": {
|
|
195
|
+
const content = message.content[update.contentIndex];
|
|
196
|
+
if (content?.type !== "text") throw new Error("PI_HARNESS_EVENT_PROTOCOL_TEXT_DELTA_WITHOUT_TEXT_BLOCK");
|
|
197
|
+
message.content[update.contentIndex] = {
|
|
198
|
+
type: "text",
|
|
199
|
+
text: content.text + update.delta
|
|
200
|
+
};
|
|
201
|
+
return;
|
|
202
|
+
}
|
|
203
|
+
case "text_end":
|
|
204
|
+
message.content[update.contentIndex] = {
|
|
205
|
+
type: "text",
|
|
206
|
+
text: update.content
|
|
207
|
+
};
|
|
208
|
+
return;
|
|
209
|
+
case "thinking_start":
|
|
210
|
+
message.content[update.contentIndex] = {
|
|
211
|
+
type: "thinking",
|
|
212
|
+
thinking: "",
|
|
213
|
+
...update.redacted === void 0 ? {} : { redacted: update.redacted }
|
|
214
|
+
};
|
|
215
|
+
return;
|
|
216
|
+
case "thinking_delta": {
|
|
217
|
+
const content = message.content[update.contentIndex];
|
|
218
|
+
if (content?.type !== "thinking") throw new Error("PI_HARNESS_EVENT_PROTOCOL_THINKING_DELTA_WITHOUT_THINKING_BLOCK");
|
|
219
|
+
message.content[update.contentIndex] = {
|
|
220
|
+
type: "thinking",
|
|
221
|
+
thinking: content.thinking + update.delta,
|
|
222
|
+
...update.redacted === void 0 ? {} : { redacted: update.redacted }
|
|
223
|
+
};
|
|
224
|
+
return;
|
|
225
|
+
}
|
|
226
|
+
case "thinking_end":
|
|
227
|
+
message.content[update.contentIndex] = {
|
|
228
|
+
type: "thinking",
|
|
229
|
+
thinking: update.content,
|
|
230
|
+
...update.redacted === void 0 ? {} : { redacted: update.redacted }
|
|
231
|
+
};
|
|
232
|
+
return;
|
|
233
|
+
case "toolcall_start":
|
|
234
|
+
toolCallJsonByContentIndex.set(update.contentIndex, piToolCallArgumentsText(update.toolCall) ?? "");
|
|
235
|
+
message.content[update.contentIndex] = update.toolCall;
|
|
236
|
+
return;
|
|
237
|
+
case "toolcall_delta": {
|
|
238
|
+
const content = message.content[update.contentIndex];
|
|
239
|
+
if (content?.type !== "toolCall") throw new Error("PI_HARNESS_EVENT_PROTOCOL_TOOL_CALL_DELTA_WITHOUT_TOOL_CALL_BLOCK");
|
|
240
|
+
const previousJson = toolCallJsonByContentIndex.get(update.contentIndex);
|
|
241
|
+
if (previousJson === void 0) throw new Error("PI_HARNESS_EVENT_PROTOCOL_TOOL_CALL_DELTA_WITHOUT_TOOL_CALL_START");
|
|
242
|
+
const partialJson = previousJson + update.delta;
|
|
243
|
+
toolCallJsonByContentIndex.set(update.contentIndex, partialJson);
|
|
244
|
+
message.content[update.contentIndex] = {
|
|
245
|
+
...content,
|
|
246
|
+
partialJson
|
|
247
|
+
};
|
|
248
|
+
return;
|
|
249
|
+
}
|
|
250
|
+
case "toolcall_end":
|
|
251
|
+
toolCallJsonByContentIndex.delete(update.contentIndex);
|
|
252
|
+
message.content[update.contentIndex] = update.toolCall;
|
|
253
|
+
}
|
|
254
|
+
};
|
|
255
|
+
var ValueEncoder = class {
|
|
256
|
+
#idsByValue = /* @__PURE__ */ new Map();
|
|
257
|
+
encode(value) {
|
|
258
|
+
const existingId = this.#idsByValue.get(value);
|
|
259
|
+
if (existingId !== void 0) return {
|
|
260
|
+
kind: "reference",
|
|
261
|
+
id: existingId
|
|
262
|
+
};
|
|
263
|
+
const id = this.#idsByValue.size;
|
|
264
|
+
this.#idsByValue.set(value, id);
|
|
265
|
+
return {
|
|
266
|
+
kind: "value",
|
|
267
|
+
id,
|
|
268
|
+
value
|
|
269
|
+
};
|
|
270
|
+
}
|
|
271
|
+
};
|
|
272
|
+
var ValueDecoder = class {
|
|
273
|
+
#valuesById = /* @__PURE__ */ new Map();
|
|
274
|
+
decode(encoded) {
|
|
275
|
+
if (encoded.kind === "value") {
|
|
276
|
+
this.#valuesById.set(encoded.id, encoded.value);
|
|
277
|
+
return encoded.value;
|
|
278
|
+
}
|
|
279
|
+
if (!this.#valuesById.has(encoded.id)) throw new Error(`PI_HARNESS_EVENT_PROTOCOL_UNKNOWN_VALUE_REFERENCE:${encoded.id}`);
|
|
280
|
+
return this.#valuesById.get(encoded.id);
|
|
281
|
+
}
|
|
282
|
+
};
|
|
283
|
+
var MessageEncoder = class {
|
|
284
|
+
#idsByMessage = /* @__PURE__ */ new Map();
|
|
285
|
+
encode(message) {
|
|
286
|
+
const existingId = this.#idsByMessage.get(message);
|
|
287
|
+
if (existingId !== void 0) return {
|
|
288
|
+
kind: "reference",
|
|
289
|
+
id: existingId
|
|
290
|
+
};
|
|
291
|
+
const id = this.#idsByMessage.size;
|
|
292
|
+
this.#idsByMessage.set(message, id);
|
|
293
|
+
return {
|
|
294
|
+
kind: "message",
|
|
295
|
+
id,
|
|
296
|
+
message: projectAgentMessage(message)
|
|
297
|
+
};
|
|
298
|
+
}
|
|
299
|
+
};
|
|
300
|
+
var MessageDecoder = class {
|
|
301
|
+
#messagesById = /* @__PURE__ */ new Map();
|
|
302
|
+
decode(encoded) {
|
|
303
|
+
if (encoded.kind === "message") {
|
|
304
|
+
this.#messagesById.set(encoded.id, encoded.message);
|
|
305
|
+
return encoded.message;
|
|
306
|
+
}
|
|
307
|
+
const message = this.#messagesById.get(encoded.id);
|
|
308
|
+
if (!message) throw new Error(`PI_HARNESS_EVENT_PROTOCOL_UNKNOWN_MESSAGE_REFERENCE:${encoded.id}`);
|
|
309
|
+
return message;
|
|
310
|
+
}
|
|
311
|
+
};
|
|
312
|
+
const piHarnessEventStreamKey = ({ stepKey, executionId, epoch }) => `${stepKey}\u0000${executionId}\u0000${epoch}`;
|
|
313
|
+
function assertPiHarnessEncodedEvent(value) {
|
|
314
|
+
assertPiHarnessJsonValue(value);
|
|
315
|
+
if (typeof value !== "object" || value === null || !("protocol" in value) || value.protocol !== "pi-harness-event" || !("version" in value) || value.version !== 2 || !("event" in value) || typeof value.event !== "object" || value.event === null || !("type" in value.event) || typeof value.event.type !== "string") throw new Error("PI_HARNESS_EVENT_PROTOCOL_UNSUPPORTED_PROTOCOL");
|
|
316
|
+
}
|
|
317
|
+
var PiHarnessEventEncoder = class {
|
|
318
|
+
#messages = new MessageEncoder();
|
|
319
|
+
#values = new ValueEncoder();
|
|
320
|
+
#activeAssistantMetadata;
|
|
321
|
+
encode(event) {
|
|
322
|
+
const compactEvent = this.#encodeEvent(event);
|
|
323
|
+
const envelope = {
|
|
324
|
+
protocol: "pi-harness-event",
|
|
325
|
+
version: 2,
|
|
326
|
+
event: compactEvent
|
|
327
|
+
};
|
|
328
|
+
const encoded = isPrimitiveOnlyMessageUpdate(compactEvent) ? envelope : snapshotPiHarnessJsonValue(envelope, `$event.${event.type}`);
|
|
329
|
+
this.#advanceAssistantMetadata(event, compactEvent);
|
|
330
|
+
return encoded;
|
|
331
|
+
}
|
|
332
|
+
#advanceAssistantMetadata(event, compactEvent) {
|
|
333
|
+
if (event.type === "message_start") this.#activeAssistantMetadata = event.message.role === "assistant" ? snapshotPiHarnessJsonValue(assistantMessageMetadata(event.message)) : void 0;
|
|
334
|
+
else if (event.type === "message_update") {
|
|
335
|
+
if (compactEvent.type !== "message_update") throw new Error("PI_HARNESS_EVENT_PROTOCOL_INVALID_COMPACT_MESSAGE_UPDATE");
|
|
336
|
+
if (compactEvent.metadata === void 0) return;
|
|
337
|
+
this.#activeAssistantMetadata = snapshotPiHarnessJsonValue(assistantMessageMetadata(event.assistantMessageEvent.partial));
|
|
338
|
+
} else if (event.type === "message_end") this.#activeAssistantMetadata = void 0;
|
|
339
|
+
}
|
|
340
|
+
#encodeEvent(event) {
|
|
341
|
+
switch (event.type) {
|
|
342
|
+
case "agent_start":
|
|
343
|
+
case "turn_start": return { type: event.type };
|
|
344
|
+
case "agent_end": return {
|
|
345
|
+
type: event.type,
|
|
346
|
+
messages: event.messages.map((message) => this.#messages.encode(message))
|
|
347
|
+
};
|
|
348
|
+
case "turn_end": return {
|
|
349
|
+
type: event.type,
|
|
350
|
+
message: this.#messages.encode(event.message),
|
|
351
|
+
toolResults: event.toolResults.map((message) => this.#messages.encode(message))
|
|
352
|
+
};
|
|
353
|
+
case "message_start": return {
|
|
354
|
+
type: event.type,
|
|
355
|
+
message: this.#messages.encode(event.message)
|
|
356
|
+
};
|
|
357
|
+
case "message_update": {
|
|
358
|
+
if (!this.#activeAssistantMetadata) throw new Error("PI_HARNESS_EVENT_PROTOCOL_MESSAGE_UPDATE_WITHOUT_ASSISTANT_START");
|
|
359
|
+
if (event.message.role !== "assistant") throw new Error("PI_HARNESS_EVENT_PROTOCOL_MESSAGE_UPDATE_NOT_ASSISTANT");
|
|
360
|
+
const assistantMessageEvent = streamedMessageUpdateEvent(event.assistantMessageEvent);
|
|
361
|
+
const update = compactAssistantMessageEvent(assistantMessageEvent);
|
|
362
|
+
const metadata = compactAssistantMessageMetadataUpdate(this.#activeAssistantMetadata, assistantMessageEvent.partial);
|
|
363
|
+
return {
|
|
364
|
+
type: event.type,
|
|
365
|
+
update,
|
|
366
|
+
...metadata ? { metadata } : {}
|
|
367
|
+
};
|
|
368
|
+
}
|
|
369
|
+
case "message_end": return {
|
|
370
|
+
type: event.type,
|
|
371
|
+
message: this.#messages.encode(event.message)
|
|
372
|
+
};
|
|
373
|
+
case "tool_execution_start": return {
|
|
374
|
+
...event,
|
|
375
|
+
args: this.#values.encode(event.args)
|
|
376
|
+
};
|
|
377
|
+
case "tool_execution_update": return {
|
|
378
|
+
...event,
|
|
379
|
+
args: this.#values.encode(event.args),
|
|
380
|
+
partialResult: this.#values.encode(snapshotPiHarnessJsonValue(event.partialResult, "$event.tool_execution_update.partialResult"))
|
|
381
|
+
};
|
|
382
|
+
case "tool_execution_end": return {
|
|
383
|
+
...event,
|
|
384
|
+
result: this.#values.encode(event.result)
|
|
385
|
+
};
|
|
386
|
+
case "queue_update": return {
|
|
387
|
+
type: event.type,
|
|
388
|
+
steer: event.steer.map((message) => this.#messages.encode(message)),
|
|
389
|
+
followUp: event.followUp.map((message) => this.#messages.encode(message)),
|
|
390
|
+
nextTurn: event.nextTurn.map((message) => this.#messages.encode(message))
|
|
391
|
+
};
|
|
392
|
+
case "save_point":
|
|
393
|
+
case "settled":
|
|
394
|
+
case "retry_scheduled":
|
|
395
|
+
case "retry_attempt_start":
|
|
396
|
+
case "retry_finished":
|
|
397
|
+
case "thinking_level_update":
|
|
398
|
+
case "tools_update": return event;
|
|
399
|
+
case "abort": return {
|
|
400
|
+
type: event.type,
|
|
401
|
+
clearedSteer: event.clearedSteer.map((message) => this.#messages.encode(message)),
|
|
402
|
+
clearedFollowUp: event.clearedFollowUp.map((message) => this.#messages.encode(message))
|
|
403
|
+
};
|
|
404
|
+
case "after_provider_response": return {
|
|
405
|
+
type: event.type,
|
|
406
|
+
status: event.status,
|
|
407
|
+
headers: this.#values.encode(event.headers)
|
|
408
|
+
};
|
|
409
|
+
case "session_compact": return {
|
|
410
|
+
type: event.type,
|
|
411
|
+
compactionEntry: this.#values.encode(event.compactionEntry),
|
|
412
|
+
fromHook: event.fromHook
|
|
413
|
+
};
|
|
414
|
+
case "session_tree": return {
|
|
415
|
+
type: event.type,
|
|
416
|
+
newLeafId: event.newLeafId,
|
|
417
|
+
oldLeafId: event.oldLeafId,
|
|
418
|
+
...event.summaryEntry ? { summaryEntry: this.#values.encode(event.summaryEntry) } : {},
|
|
419
|
+
...event.fromHook === void 0 ? {} : { fromHook: event.fromHook }
|
|
420
|
+
};
|
|
421
|
+
case "model_update": return {
|
|
422
|
+
type: event.type,
|
|
423
|
+
model: this.#values.encode(event.model),
|
|
424
|
+
...event.previousModel ? { previousModel: this.#values.encode(event.previousModel) } : {},
|
|
425
|
+
source: event.source
|
|
426
|
+
};
|
|
427
|
+
case "resources_update": return {
|
|
428
|
+
type: event.type,
|
|
429
|
+
resources: this.#values.encode(event.resources),
|
|
430
|
+
previousResources: this.#values.encode(event.previousResources)
|
|
431
|
+
};
|
|
432
|
+
default: return {
|
|
433
|
+
type: "raw",
|
|
434
|
+
event
|
|
435
|
+
};
|
|
436
|
+
}
|
|
437
|
+
}
|
|
438
|
+
};
|
|
439
|
+
const piHarnessEventProtocol = {
|
|
440
|
+
createEncoder: () => new PiHarnessEventEncoder(),
|
|
441
|
+
createDecoder: () => new PiHarnessEventDecoder(),
|
|
442
|
+
eventType: (value) => {
|
|
443
|
+
assertPiHarnessEncodedEvent(value);
|
|
444
|
+
return value.event.type === "raw" ? value.event.event.type : value.event.type;
|
|
445
|
+
}
|
|
446
|
+
};
|
|
447
|
+
var PiHarnessEventDecoder = class {
|
|
448
|
+
#messages = new MessageDecoder();
|
|
449
|
+
#values = new ValueDecoder();
|
|
450
|
+
#toolCallJsonByContentIndex = /* @__PURE__ */ new Map();
|
|
451
|
+
#activeAssistantMessage;
|
|
452
|
+
decode(value) {
|
|
453
|
+
const encoded = freezePiHarnessJsonValue(value);
|
|
454
|
+
assertPiHarnessEncodedEvent(encoded);
|
|
455
|
+
return this.#decodeEvent(encoded.event);
|
|
456
|
+
}
|
|
457
|
+
#decodeEvent(encoded) {
|
|
458
|
+
switch (encoded.type) {
|
|
459
|
+
case "raw": return encoded.event;
|
|
460
|
+
case "agent_start":
|
|
461
|
+
case "turn_start": return { type: encoded.type };
|
|
462
|
+
case "agent_end": return {
|
|
463
|
+
type: encoded.type,
|
|
464
|
+
messages: encoded.messages.map((message) => this.#messages.decode(message))
|
|
465
|
+
};
|
|
466
|
+
case "turn_end": return {
|
|
467
|
+
type: encoded.type,
|
|
468
|
+
message: this.#messages.decode(encoded.message),
|
|
469
|
+
toolResults: encoded.toolResults.map((message) => this.#messages.decode(message))
|
|
470
|
+
};
|
|
471
|
+
case "message_start": {
|
|
472
|
+
const message = this.#messages.decode(encoded.message);
|
|
473
|
+
this.#toolCallJsonByContentIndex.clear();
|
|
474
|
+
this.#activeAssistantMessage = message.role === "assistant" ? message : void 0;
|
|
475
|
+
return {
|
|
476
|
+
type: encoded.type,
|
|
477
|
+
message
|
|
478
|
+
};
|
|
479
|
+
}
|
|
480
|
+
case "message_update": {
|
|
481
|
+
if (!this.#activeAssistantMessage) throw new Error("PI_HARNESS_EVENT_PROTOCOL_MESSAGE_UPDATE_WITHOUT_ASSISTANT_START");
|
|
482
|
+
const reconstructedMessage = snapshotPiHarnessJsonValue(this.#activeAssistantMessage);
|
|
483
|
+
applyCompactAssistantMessageEvent(reconstructedMessage, encoded.update, this.#toolCallJsonByContentIndex);
|
|
484
|
+
const message = encoded.metadata ? assistantMessageWithMetadataUpdate(reconstructedMessage, encoded.metadata) : reconstructedMessage;
|
|
485
|
+
this.#activeAssistantMessage = message;
|
|
486
|
+
return {
|
|
487
|
+
type: encoded.type,
|
|
488
|
+
message,
|
|
489
|
+
assistantMessageEvent: streamedAssistantMessageEvent(encoded.update, message)
|
|
490
|
+
};
|
|
491
|
+
}
|
|
492
|
+
case "message_end": {
|
|
493
|
+
const message = this.#messages.decode(encoded.message);
|
|
494
|
+
this.#toolCallJsonByContentIndex.clear();
|
|
495
|
+
this.#activeAssistantMessage = void 0;
|
|
496
|
+
return {
|
|
497
|
+
type: encoded.type,
|
|
498
|
+
message
|
|
499
|
+
};
|
|
500
|
+
}
|
|
501
|
+
case "tool_execution_start": return {
|
|
502
|
+
...encoded,
|
|
503
|
+
args: this.#values.decode(encoded.args)
|
|
504
|
+
};
|
|
505
|
+
case "tool_execution_update": return {
|
|
506
|
+
...encoded,
|
|
507
|
+
args: this.#values.decode(encoded.args),
|
|
508
|
+
partialResult: this.#values.decode(encoded.partialResult)
|
|
509
|
+
};
|
|
510
|
+
case "tool_execution_end": return {
|
|
511
|
+
...encoded,
|
|
512
|
+
result: this.#values.decode(encoded.result)
|
|
513
|
+
};
|
|
514
|
+
case "queue_update": return {
|
|
515
|
+
type: encoded.type,
|
|
516
|
+
steer: encoded.steer.map((message) => this.#messages.decode(message)),
|
|
517
|
+
followUp: encoded.followUp.map((message) => this.#messages.decode(message)),
|
|
518
|
+
nextTurn: encoded.nextTurn.map((message) => this.#messages.decode(message))
|
|
519
|
+
};
|
|
520
|
+
case "save_point":
|
|
521
|
+
case "settled":
|
|
522
|
+
case "retry_scheduled":
|
|
523
|
+
case "retry_attempt_start":
|
|
524
|
+
case "retry_finished":
|
|
525
|
+
case "thinking_level_update": return encoded;
|
|
526
|
+
case "tools_update": return {
|
|
527
|
+
...encoded,
|
|
528
|
+
toolNames: [...encoded.toolNames],
|
|
529
|
+
previousToolNames: [...encoded.previousToolNames],
|
|
530
|
+
activeToolNames: [...encoded.activeToolNames],
|
|
531
|
+
previousActiveToolNames: [...encoded.previousActiveToolNames]
|
|
532
|
+
};
|
|
533
|
+
case "abort": return {
|
|
534
|
+
type: encoded.type,
|
|
535
|
+
clearedSteer: encoded.clearedSteer.map((message) => this.#messages.decode(message)),
|
|
536
|
+
clearedFollowUp: encoded.clearedFollowUp.map((message) => this.#messages.decode(message))
|
|
537
|
+
};
|
|
538
|
+
case "after_provider_response": return {
|
|
539
|
+
type: encoded.type,
|
|
540
|
+
status: encoded.status,
|
|
541
|
+
headers: this.#values.decode(encoded.headers)
|
|
542
|
+
};
|
|
543
|
+
case "session_compact": return {
|
|
544
|
+
type: encoded.type,
|
|
545
|
+
compactionEntry: this.#values.decode(encoded.compactionEntry),
|
|
546
|
+
fromHook: encoded.fromHook
|
|
547
|
+
};
|
|
548
|
+
case "session_tree": return {
|
|
549
|
+
type: encoded.type,
|
|
550
|
+
newLeafId: encoded.newLeafId,
|
|
551
|
+
oldLeafId: encoded.oldLeafId,
|
|
552
|
+
...encoded.summaryEntry ? { summaryEntry: this.#values.decode(encoded.summaryEntry) } : {},
|
|
553
|
+
...encoded.fromHook === void 0 ? {} : { fromHook: encoded.fromHook }
|
|
554
|
+
};
|
|
555
|
+
case "model_update": return {
|
|
556
|
+
type: encoded.type,
|
|
557
|
+
model: this.#values.decode(encoded.model),
|
|
558
|
+
previousModel: encoded.previousModel ? this.#values.decode(encoded.previousModel) : void 0,
|
|
559
|
+
source: encoded.source
|
|
560
|
+
};
|
|
561
|
+
case "resources_update": return {
|
|
562
|
+
type: encoded.type,
|
|
563
|
+
resources: this.#values.decode(encoded.resources),
|
|
564
|
+
previousResources: this.#values.decode(encoded.previousResources)
|
|
565
|
+
};
|
|
566
|
+
}
|
|
567
|
+
throw new Error("PI_HARNESS_EVENT_PROTOCOL_UNKNOWN_EVENT");
|
|
568
|
+
}
|
|
569
|
+
};
|
|
570
|
+
var PiHarnessEventStreamDecoders = class {
|
|
571
|
+
#decodersByStream = /* @__PURE__ */ new Map();
|
|
572
|
+
start(identity) {
|
|
573
|
+
this.#decodersByStream.set(piHarnessEventStreamKey(identity), new PiHarnessEventDecoder());
|
|
574
|
+
}
|
|
575
|
+
finish(identity) {
|
|
576
|
+
this.#decodersByStream.delete(piHarnessEventStreamKey(identity));
|
|
577
|
+
}
|
|
578
|
+
decode(identity, event) {
|
|
579
|
+
const streamKey = piHarnessEventStreamKey(identity);
|
|
580
|
+
const decoder = this.#decodersByStream.get(streamKey);
|
|
581
|
+
if (!decoder) throw new Error(`Pi harness event stream ${JSON.stringify(streamKey)} was not started.`);
|
|
582
|
+
return decoder.decode(event);
|
|
583
|
+
}
|
|
584
|
+
};
|
|
585
|
+
//#endregion
|
|
586
|
+
export { PiHarnessEventDecoder, PiHarnessEventEncoder, PiHarnessEventStreamDecoders, piHarnessEventProtocol, piToolCallArgumentsText };
|
|
587
|
+
|
|
588
|
+
//# sourceMappingURL=agent-harness-event-protocol.js.map
|