@noopolis/daimon 0.1.1 → 0.1.2
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 +54 -10
- package/dist/core/siblingBuildFreshness.d.ts +21 -0
- package/dist/core/siblingBuildFreshness.d.ts.map +1 -0
- package/dist/core/siblingBuildFreshness.js +25 -0
- package/dist/core/types.d.ts +10 -1
- package/dist/core/types.d.ts.map +1 -1
- package/dist/examples/exampleCausalId.d.ts +3 -0
- package/dist/examples/exampleCausalId.d.ts.map +1 -0
- package/dist/examples/exampleCausalId.js +8 -0
- package/dist/examples/jungian-play-org.js +3 -2
- package/dist/examples/jungian-triad-org.js +3 -2
- package/dist/examples/jungianPlayAgent.d.ts +1 -1
- package/dist/examples/jungianPlayAgent.d.ts.map +1 -1
- package/dist/examples/jungianPlayAgent.js +1 -1
- package/dist/examples/jungianProfiles.d.ts +1 -1
- package/dist/examples/jungianProfiles.d.ts.map +1 -1
- package/dist/examples/jungianTriadProfiles.d.ts +1 -1
- package/dist/examples/jungianTriadProfiles.d.ts.map +1 -1
- package/dist/examples/pi-agent.js +3 -2
- package/dist/examples/pi-memory-org.js +6 -5
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/mcp/toolServer.d.ts +14 -0
- package/dist/mcp/toolServer.d.ts.map +1 -0
- package/dist/mcp/toolServer.js +105 -0
- package/dist/observability/causalEvents.d.ts +118 -0
- package/dist/observability/causalEvents.d.ts.map +1 -0
- package/dist/observability/causalEvents.js +135 -0
- package/dist/observability/controlCausal.d.ts +117 -0
- package/dist/observability/controlCausal.d.ts.map +1 -0
- package/dist/observability/controlCausal.js +107 -0
- package/dist/observability/emitCausalFixture.d.ts +25 -0
- package/dist/observability/emitCausalFixture.d.ts.map +1 -0
- package/dist/observability/emitCausalFixture.js +98 -0
- package/dist/observability/index.d.ts +2 -0
- package/dist/observability/index.d.ts.map +1 -1
- package/dist/observability/index.js +2 -0
- package/dist/pi/cliSession.d.ts +46 -0
- package/dist/pi/cliSession.d.ts.map +1 -0
- package/dist/pi/cliSession.js +224 -0
- package/dist/pi/index.d.ts +5 -0
- package/dist/pi/index.d.ts.map +1 -1
- package/dist/pi/index.js +5 -0
- package/dist/pi/memoryTools.d.ts +55 -1
- package/dist/pi/memoryTools.d.ts.map +1 -1
- package/dist/pi/memoryTools.js +144 -24
- package/dist/pi/modelRegistry.d.ts +13 -0
- package/dist/pi/modelRegistry.d.ts.map +1 -0
- package/dist/pi/modelRegistry.js +19 -0
- package/dist/pi/piAgentHandle.d.ts +45 -0
- package/dist/pi/piAgentHandle.d.ts.map +1 -0
- package/dist/pi/piAgentHandle.js +324 -0
- package/dist/pi/piAgentWakeSupport.d.ts +68 -0
- package/dist/pi/piAgentWakeSupport.d.ts.map +1 -0
- package/dist/pi/piAgentWakeSupport.js +147 -0
- package/dist/pi/piHarness.d.ts +30 -4
- package/dist/pi/piHarness.d.ts.map +1 -1
- package/dist/pi/piHarness.js +97 -255
- package/dist/pi/prompts.d.ts +9 -0
- package/dist/pi/prompts.d.ts.map +1 -0
- package/dist/pi/prompts.js +39 -0
- package/dist/pi/rawTrainingCapture.d.ts +59 -0
- package/dist/pi/rawTrainingCapture.d.ts.map +1 -0
- package/dist/pi/rawTrainingCapture.js +175 -0
- package/dist/pi/turnCausal.d.ts +73 -0
- package/dist/pi/turnCausal.d.ts.map +1 -0
- package/dist/pi/turnCausal.js +75 -0
- package/dist/pi/turnTrace.d.ts +128 -0
- package/dist/pi/turnTrace.d.ts.map +1 -0
- package/dist/pi/turnTrace.js +141 -0
- package/dist/pi/wakeAcceptance.d.ts +49 -0
- package/dist/pi/wakeAcceptance.d.ts.map +1 -0
- package/dist/pi/wakeAcceptance.js +281 -0
- package/dist/pi/wakeAcceptanceFs.d.ts +44 -0
- package/dist/pi/wakeAcceptanceFs.d.ts.map +1 -0
- package/dist/pi/wakeAcceptanceFs.js +318 -0
- package/dist/pi/wakeAcceptanceSchema.d.ts +75 -0
- package/dist/pi/wakeAcceptanceSchema.d.ts.map +1 -0
- package/dist/pi/wakeAcceptanceSchema.js +265 -0
- package/dist/pi/wakeModes.d.ts +9 -0
- package/dist/pi/wakeModes.d.ts.map +1 -0
- package/dist/pi/wakeModes.js +19 -0
- package/dist/pi/worldNudge.d.ts +22 -0
- package/dist/pi/worldNudge.d.ts.map +1 -0
- package/dist/pi/worldNudge.js +62 -0
- package/dist/pi/worldToolProtocol.d.ts +13 -0
- package/dist/pi/worldToolProtocol.d.ts.map +1 -0
- package/dist/pi/worldToolProtocol.js +49 -0
- package/dist/pi/worldTools.d.ts +32 -0
- package/dist/pi/worldTools.d.ts.map +1 -0
- package/dist/pi/worldTools.js +373 -0
- package/dist/pi/worldTrajectory.d.ts +52 -0
- package/dist/pi/worldTrajectory.d.ts.map +1 -0
- package/dist/pi/worldTrajectory.js +131 -0
- package/package.json +10 -13
- package/dist/examples/mixed-engine-org.d.ts +0 -2
- package/dist/examples/mixed-engine-org.d.ts.map +0 -1
- package/dist/examples/mixed-engine-org.js +0 -351
- package/dist/examples/mixedEngineCli.d.ts +0 -15
- package/dist/examples/mixedEngineCli.d.ts.map +0 -1
- package/dist/examples/mixedEngineCli.js +0 -174
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import type { AgentHandle, AgentStatus, WakeEvent, WakeResult } from "../core/types.js";
|
|
2
|
+
import { type PiMemoryToolContextRef } from "./memoryTools.js";
|
|
3
|
+
import { stampTurnInputSubmitted, stampTurnOutputCompleted } from "./turnCausal.js";
|
|
4
|
+
import { type WakeAcceptanceStoreLike } from "./wakeAcceptance.js";
|
|
5
|
+
import { persistPiTurnTrace, type PiTurnTraceModel } from "./turnTrace.js";
|
|
6
|
+
import { type PiRawTrainingCaptureOptions, type PiRawTrainingCaptureRef } from "./rawTrainingCapture.js";
|
|
7
|
+
import { type PiWorldToolContextRef } from "./worldNudge.js";
|
|
8
|
+
import { type PiWorldTrajectoryIdentity } from "./worldTrajectory.js";
|
|
9
|
+
import { type PiNativeSessionCreator, type PiSession, type PiSessionCreator, type PiSessionLike } from "./piAgentWakeSupport.js";
|
|
10
|
+
import { type MemoryRuntime } from "@noopolis/mneme";
|
|
11
|
+
export type { PiSession, PiSessionLike, PiSessionCreator, PiNativeSessionCreator } from "./piAgentWakeSupport.js";
|
|
12
|
+
export type WakeAcceptanceInput = {
|
|
13
|
+
runWake?: typeof stampTurnInputSubmitted;
|
|
14
|
+
completeTurn?: typeof stampTurnOutputCompleted;
|
|
15
|
+
traceTurn?: typeof persistPiTurnTrace;
|
|
16
|
+
createWakeAcceptance?: (runtimeHomePath: string, agentId: string) => WakeAcceptanceStoreLike;
|
|
17
|
+
};
|
|
18
|
+
export declare class PiAgentHandle implements AgentHandle {
|
|
19
|
+
readonly id: string;
|
|
20
|
+
private readonly session;
|
|
21
|
+
private readonly createSession;
|
|
22
|
+
private readonly runtimeHomePath;
|
|
23
|
+
private readonly traceModel;
|
|
24
|
+
private readonly memory?;
|
|
25
|
+
private readonly memoryToolContext?;
|
|
26
|
+
private readonly worldToolContext?;
|
|
27
|
+
private readonly rawTrainingCaptureRef?;
|
|
28
|
+
private readonly rawTrainingCaptureOptions?;
|
|
29
|
+
private readonly worldTrajectoryIdentity?;
|
|
30
|
+
private readonly piSessionForRawCapture?;
|
|
31
|
+
private state;
|
|
32
|
+
private lastWakeAt;
|
|
33
|
+
private lastError;
|
|
34
|
+
private readonly wakeDeliveryQueue;
|
|
35
|
+
private readonly stampTurnInputSubmitted;
|
|
36
|
+
private readonly stampTurnOutputCompleted;
|
|
37
|
+
private readonly persistTrace;
|
|
38
|
+
constructor(id: string, session: PiSession, createSession: PiNativeSessionCreator, runtimeHomePath: string, traceModel: PiTurnTraceModel, memory?: MemoryRuntime, memoryToolContext?: PiMemoryToolContextRef, dependencies?: WakeAcceptanceInput, worldToolContext?: PiWorldToolContextRef, rawTrainingCaptureRef?: PiRawTrainingCaptureRef, rawTrainingCaptureOptions?: PiRawTrainingCaptureOptions, worldTrajectoryIdentity?: PiWorldTrajectoryIdentity, rawTrainingCaptureSession?: PiSession);
|
|
39
|
+
constructor(id: string, session: PiSessionLike, createSession: PiSessionCreator, runtimeHomePath: string, traceModel: PiTurnTraceModel, memory?: MemoryRuntime, memoryToolContext?: PiMemoryToolContextRef, dependencies?: WakeAcceptanceInput, worldToolContext?: PiWorldToolContextRef, rawTrainingCaptureRef?: never, rawTrainingCaptureOptions?: never, worldTrajectoryIdentity?: PiWorldTrajectoryIdentity, rawTrainingCaptureSession?: never);
|
|
40
|
+
wake(event: WakeEvent): Promise<WakeResult>;
|
|
41
|
+
private runWake;
|
|
42
|
+
status(): AgentStatus;
|
|
43
|
+
stop(): Promise<void>;
|
|
44
|
+
}
|
|
45
|
+
//# sourceMappingURL=piAgentHandle.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"piAgentHandle.d.ts","sourceRoot":"","sources":["../../src/pi/piAgentHandle.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,WAAW,EAAE,SAAS,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC;AAExF,OAAO,EAAoC,KAAK,sBAAsB,EAAE,MAAM,kBAAkB,CAAC;AAEjG,OAAO,EACL,uBAAuB,EACvB,wBAAwB,EAGzB,MAAM,iBAAiB,CAAC;AACzB,OAAO,EAGL,KAAK,uBAAuB,EAC7B,MAAM,qBAAqB,CAAC;AAC7B,OAAO,EAAE,kBAAkB,EAAkC,KAAK,gBAAgB,EAA6B,MAAM,gBAAgB,CAAC;AAEtI,OAAO,EAAyD,KAAK,2BAA2B,EAAE,KAAK,uBAAuB,EAAE,MAAM,yBAAyB,CAAC;AAChK,OAAO,EAA2C,KAAK,qBAAqB,EAAE,MAAM,iBAAiB,CAAC;AACtG,OAAO,EAAkC,KAAK,yBAAyB,EAAE,MAAM,sBAAsB,CAAC;AACtG,OAAO,EAML,KAAK,sBAAsB,EAC3B,KAAK,SAAS,EACd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CAAC,QAAQ,CAAC,wBAAwB,CAAkC;IAC3E,OAAO,CAAC,QAAQ,CAAC,YAAY,CAA4B;gBAGvD,EAAE,EAAE,MAAM,EACV,OAAO,EAAE,SAAS,EAClB,aAAa,EAAE,sBAAsB,EACrC,eAAe,EAAE,MAAM,EACvB,UAAU,EAAE,gBAAgB,EAC5B,MAAM,CAAC,EAAE,aAAa,EACtB,iBAAiB,CAAC,EAAE,sBAAsB,EAC1C,YAAY,CAAC,EAAE,mBAAmB,EAClC,gBAAgB,CAAC,EAAE,qBAAqB,EACxC,qBAAqB,CAAC,EAAE,uBAAuB,EAC/C,yBAAyB,CAAC,EAAE,2BAA2B,EACvD,uBAAuB,CAAC,EAAE,yBAAyB,EACnD,yBAAyB,CAAC,EAAE,SAAS;gBAGrC,EAAE,EAAE,MAAM,EACV,OAAO,EAAE,aAAa,EACtB,aAAa,EAAE,gBAAgB,EAC/B,eAAe,EAAE,MAAM,EACvB,UAAU,EAAE,gBAAgB,EAC5B,MAAM,CAAC,EAAE,aAAa,EACtB,iBAAiB,CAAC,EAAE,sBAAsB,EAC1C,YAAY,CAAC,EAAE,mBAAmB,EAClC,gBAAgB,CAAC,EAAE,qBAAqB,EACxC,qBAAqB,CAAC,EAAE,KAAK,EAC7B,yBAAyB,CAAC,EAAE,KAAK,EACjC,uBAAuB,CAAC,EAAE,yBAAyB,EACnD,yBAAyB,CAAC,EAAE,KAAK;IA0B7B,IAAI,CAAC,KAAK,EAAE,SAAS,GAAG,OAAO,CAAC,UAAU,CAAC;YAQnC,OAAO;IAkRrB,MAAM,IAAI,WAAW;IASf,IAAI,IAAI,OAAO,CAAC,IAAI,CAAC;CAI5B"}
|
|
@@ -0,0 +1,324 @@
|
|
|
1
|
+
import { createTrustedPiMemoryToolContext } from "./memoryTools.js";
|
|
2
|
+
import { formatWakePrompt } from "./prompts.js";
|
|
3
|
+
import { stampTurnInputSubmitted, stampTurnOutputCompleted } from "./turnCausal.js";
|
|
4
|
+
import { WakeAcceptanceStore } from "./wakeAcceptance.js";
|
|
5
|
+
import { persistPiTurnTrace } from "./turnTrace.js";
|
|
6
|
+
import { formatDreamPrompt } from "./wakeModes.js";
|
|
7
|
+
import { createPiRawTrainingCapture } from "./rawTrainingCapture.js";
|
|
8
|
+
import { formatWorldWakePrompt, worldWakeContext } from "./worldNudge.js";
|
|
9
|
+
import { createPiWorldTrajectoryCapture } from "./worldTrajectory.js";
|
|
10
|
+
import { cloneWakeEvent, persistPiTurnArtifacts, PiWakeDeliveryQueue, selectPiSessionForWake, subscribeToPiTurnEvents } from "./piAgentWakeSupport.js";
|
|
11
|
+
import { readMemoryContext } from "@noopolis/mneme";
|
|
12
|
+
export class PiAgentHandle {
|
|
13
|
+
id;
|
|
14
|
+
session;
|
|
15
|
+
createSession;
|
|
16
|
+
runtimeHomePath;
|
|
17
|
+
traceModel;
|
|
18
|
+
memory;
|
|
19
|
+
memoryToolContext;
|
|
20
|
+
worldToolContext;
|
|
21
|
+
rawTrainingCaptureRef;
|
|
22
|
+
rawTrainingCaptureOptions;
|
|
23
|
+
worldTrajectoryIdentity;
|
|
24
|
+
piSessionForRawCapture;
|
|
25
|
+
state = "idle";
|
|
26
|
+
lastWakeAt;
|
|
27
|
+
lastError;
|
|
28
|
+
wakeDeliveryQueue;
|
|
29
|
+
stampTurnInputSubmitted;
|
|
30
|
+
stampTurnOutputCompleted;
|
|
31
|
+
persistTrace;
|
|
32
|
+
constructor(id, session, createSession, runtimeHomePath, traceModel, memory, memoryToolContext, dependencies = {}, worldToolContext, rawTrainingCaptureRef, rawTrainingCaptureOptions, worldTrajectoryIdentity, piSessionForRawCapture) {
|
|
33
|
+
this.id = id;
|
|
34
|
+
this.session = session;
|
|
35
|
+
this.createSession = createSession;
|
|
36
|
+
this.runtimeHomePath = runtimeHomePath;
|
|
37
|
+
this.traceModel = traceModel;
|
|
38
|
+
this.memory = memory;
|
|
39
|
+
this.memoryToolContext = memoryToolContext;
|
|
40
|
+
this.worldToolContext = worldToolContext;
|
|
41
|
+
this.rawTrainingCaptureRef = rawTrainingCaptureRef;
|
|
42
|
+
this.rawTrainingCaptureOptions = rawTrainingCaptureOptions;
|
|
43
|
+
this.worldTrajectoryIdentity = worldTrajectoryIdentity;
|
|
44
|
+
this.piSessionForRawCapture = piSessionForRawCapture;
|
|
45
|
+
this.stampTurnInputSubmitted = dependencies.runWake ?? stampTurnInputSubmitted;
|
|
46
|
+
this.stampTurnOutputCompleted = dependencies.completeTurn ?? stampTurnOutputCompleted;
|
|
47
|
+
this.persistTrace = dependencies.traceTurn ?? persistPiTurnTrace;
|
|
48
|
+
const wakeAcceptance = dependencies.createWakeAcceptance?.(runtimeHomePath, id) ??
|
|
49
|
+
new WakeAcceptanceStore(runtimeHomePath, id);
|
|
50
|
+
this.wakeDeliveryQueue = new PiWakeDeliveryQueue(id, wakeAcceptance);
|
|
51
|
+
}
|
|
52
|
+
async wake(event) {
|
|
53
|
+
const wakeEvent = cloneWakeEvent(event);
|
|
54
|
+
return this.wakeDeliveryQueue.wake(wakeEvent, (queuedEvent, transition) => this.runWake(queuedEvent, transition));
|
|
55
|
+
}
|
|
56
|
+
async runWake(event, transitionToInvoking) {
|
|
57
|
+
const startedAt = new Date();
|
|
58
|
+
const startedAtMs = Date.now();
|
|
59
|
+
const chunks = [];
|
|
60
|
+
const tools = [];
|
|
61
|
+
let enginePromptMs;
|
|
62
|
+
let memoryPrepare;
|
|
63
|
+
let selectedSession;
|
|
64
|
+
let rawTrainingCapture;
|
|
65
|
+
let rawTrainingCapturePersistAttempted = false;
|
|
66
|
+
let unsubscribe;
|
|
67
|
+
let stage = "select_session";
|
|
68
|
+
let prepared;
|
|
69
|
+
this.state = "running";
|
|
70
|
+
this.lastWakeAt = new Date().toISOString();
|
|
71
|
+
this.lastError = undefined;
|
|
72
|
+
const memoryContext = readMemoryContext({
|
|
73
|
+
kind: event.kind,
|
|
74
|
+
id: event.id,
|
|
75
|
+
from: event.from,
|
|
76
|
+
text: event.text,
|
|
77
|
+
context: event.context
|
|
78
|
+
});
|
|
79
|
+
const worldContext = this.worldToolContext === undefined
|
|
80
|
+
? undefined
|
|
81
|
+
: worldWakeContext(event);
|
|
82
|
+
const safeWakeText = worldContext === undefined
|
|
83
|
+
? event.text
|
|
84
|
+
: event.kind === "message" && event.delivery !== undefined && event.transportText !== undefined
|
|
85
|
+
? `${formatWorldWakePrompt(worldContext)}\n\n${formatWakePrompt(event)}`
|
|
86
|
+
: formatWorldWakePrompt(worldContext);
|
|
87
|
+
const worldTrajectory = worldContext?.decisionToken === undefined
|
|
88
|
+
? undefined
|
|
89
|
+
: createPiWorldTrajectoryCapture();
|
|
90
|
+
const request = {
|
|
91
|
+
eventId: event.id,
|
|
92
|
+
kind: event.kind,
|
|
93
|
+
text: safeWakeText,
|
|
94
|
+
from: event.from,
|
|
95
|
+
context: memoryContext
|
|
96
|
+
};
|
|
97
|
+
let promptText = worldContext === undefined
|
|
98
|
+
? formatWakePrompt(event)
|
|
99
|
+
: safeWakeText;
|
|
100
|
+
try {
|
|
101
|
+
if (this.worldToolContext !== undefined) {
|
|
102
|
+
this.worldToolContext.current = worldContext;
|
|
103
|
+
}
|
|
104
|
+
selectedSession = await selectPiSessionForWake({
|
|
105
|
+
agentId: this.id,
|
|
106
|
+
createSession: this.createSession,
|
|
107
|
+
event,
|
|
108
|
+
memoryContext,
|
|
109
|
+
runtimeHomePath: this.runtimeHomePath,
|
|
110
|
+
session: this.session
|
|
111
|
+
});
|
|
112
|
+
if (this.rawTrainingCaptureRef !== undefined
|
|
113
|
+
&& this.rawTrainingCaptureOptions !== undefined) {
|
|
114
|
+
rawTrainingCapture = createPiRawTrainingCapture();
|
|
115
|
+
this.rawTrainingCaptureRef.current = rawTrainingCapture;
|
|
116
|
+
}
|
|
117
|
+
unsubscribe = subscribeToPiTurnEvents({
|
|
118
|
+
chunks,
|
|
119
|
+
rawTrainingCapture,
|
|
120
|
+
session: selectedSession.session,
|
|
121
|
+
tools,
|
|
122
|
+
worldTrajectory
|
|
123
|
+
});
|
|
124
|
+
if (this.memory !== undefined) {
|
|
125
|
+
stage = "memory_prepare";
|
|
126
|
+
const memoryStartedAt = Date.now();
|
|
127
|
+
try {
|
|
128
|
+
prepared = await this.memory.prepareTurn(request);
|
|
129
|
+
}
|
|
130
|
+
catch (error) {
|
|
131
|
+
memoryPrepare = {
|
|
132
|
+
durationMs: Date.now() - memoryStartedAt,
|
|
133
|
+
status: "failed"
|
|
134
|
+
};
|
|
135
|
+
throw error;
|
|
136
|
+
}
|
|
137
|
+
memoryPrepare = {
|
|
138
|
+
durationMs: Date.now() - memoryStartedAt,
|
|
139
|
+
prepared,
|
|
140
|
+
status: "completed"
|
|
141
|
+
};
|
|
142
|
+
promptText = prepared.promptText;
|
|
143
|
+
if (this.memoryToolContext !== undefined) {
|
|
144
|
+
this.memoryToolContext.observeTool = (toolEvent) => tools.push(toolEvent);
|
|
145
|
+
this.memoryToolContext.current = createTrustedPiMemoryToolContext({
|
|
146
|
+
agentId: this.id,
|
|
147
|
+
memory: this.memory,
|
|
148
|
+
mode: selectedSession.mode,
|
|
149
|
+
prepared,
|
|
150
|
+
threadId: selectedSession.threadId,
|
|
151
|
+
wakeId: event.id
|
|
152
|
+
});
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
if (selectedSession.mode === "dream") {
|
|
156
|
+
promptText = formatDreamPrompt(promptText, selectedSession.threadId);
|
|
157
|
+
}
|
|
158
|
+
stage = "causal_input";
|
|
159
|
+
const turnInput = await this.stampTurnInputSubmitted({
|
|
160
|
+
agentId: this.id,
|
|
161
|
+
event,
|
|
162
|
+
prepared,
|
|
163
|
+
promptText,
|
|
164
|
+
runtimeHomePath: this.runtimeHomePath
|
|
165
|
+
});
|
|
166
|
+
stage = "invoking";
|
|
167
|
+
if (transitionToInvoking !== undefined) {
|
|
168
|
+
await transitionToInvoking();
|
|
169
|
+
}
|
|
170
|
+
stage = "engine_prompt";
|
|
171
|
+
const engineStartedAt = Date.now();
|
|
172
|
+
await selectedSession.session.prompt(promptText, { expandPromptTemplates: false });
|
|
173
|
+
enginePromptMs = Date.now() - engineStartedAt;
|
|
174
|
+
this.state = "idle";
|
|
175
|
+
const outputText = chunks.join("\n").trim();
|
|
176
|
+
stage = "causal_output";
|
|
177
|
+
await this.stampTurnOutputCompleted({
|
|
178
|
+
agentId: this.id,
|
|
179
|
+
causeEventId: turnInput.event_id,
|
|
180
|
+
outputText,
|
|
181
|
+
runtimeHomePath: this.runtimeHomePath,
|
|
182
|
+
turnId: event.id
|
|
183
|
+
});
|
|
184
|
+
await this.persistTrace({
|
|
185
|
+
agentId: this.id,
|
|
186
|
+
enginePromptMs,
|
|
187
|
+
event,
|
|
188
|
+
memoryPrepare,
|
|
189
|
+
memoryEnabled: Boolean(this.memory),
|
|
190
|
+
model: this.traceModel,
|
|
191
|
+
outputText,
|
|
192
|
+
promptText,
|
|
193
|
+
runtimeHomePath: this.runtimeHomePath,
|
|
194
|
+
session: selectedSession,
|
|
195
|
+
startedAt,
|
|
196
|
+
status: "completed",
|
|
197
|
+
tools,
|
|
198
|
+
totalMs: Date.now() - startedAtMs,
|
|
199
|
+
...(this.worldToolContext === undefined
|
|
200
|
+
? {}
|
|
201
|
+
: { worldContextBound: worldContext !== undefined })
|
|
202
|
+
});
|
|
203
|
+
if (rawTrainingCapture !== undefined
|
|
204
|
+
&& this.rawTrainingCaptureOptions !== undefined
|
|
205
|
+
&& this.piSessionForRawCapture !== undefined) {
|
|
206
|
+
// Do not retry a partially failed private capture in the catch path.
|
|
207
|
+
// The first failure is authoritative and retrying the same immutable
|
|
208
|
+
// turn path would only mask it with an EEXIST/partial-write error.
|
|
209
|
+
rawTrainingCapturePersistAttempted = true;
|
|
210
|
+
}
|
|
211
|
+
await persistPiTurnArtifacts({
|
|
212
|
+
agentId: this.id,
|
|
213
|
+
completedAt: new Date(),
|
|
214
|
+
model: this.traceModel,
|
|
215
|
+
piSessionForRawCapture: this.piSessionForRawCapture,
|
|
216
|
+
promptText,
|
|
217
|
+
rawTrainingCapture,
|
|
218
|
+
rawTrainingCaptureOptions: this.rawTrainingCaptureOptions,
|
|
219
|
+
runtimeHomePath: this.runtimeHomePath,
|
|
220
|
+
startedAt,
|
|
221
|
+
status: "completed",
|
|
222
|
+
totalMs: Date.now() - startedAtMs,
|
|
223
|
+
turnId: event.id,
|
|
224
|
+
worldContext,
|
|
225
|
+
worldTrajectory,
|
|
226
|
+
worldTrajectoryIdentity: this.worldTrajectoryIdentity
|
|
227
|
+
});
|
|
228
|
+
return {
|
|
229
|
+
agentId: this.id,
|
|
230
|
+
text: outputText,
|
|
231
|
+
durationMs: Date.now() - startedAtMs
|
|
232
|
+
};
|
|
233
|
+
}
|
|
234
|
+
catch (error) {
|
|
235
|
+
this.state = "failed";
|
|
236
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
237
|
+
this.lastError = message;
|
|
238
|
+
if (memoryPrepare === undefined && this.memory !== undefined) {
|
|
239
|
+
memoryPrepare = {
|
|
240
|
+
prepared,
|
|
241
|
+
status: "failed"
|
|
242
|
+
};
|
|
243
|
+
}
|
|
244
|
+
await this.persistTrace({
|
|
245
|
+
agentId: this.id,
|
|
246
|
+
enginePromptMs,
|
|
247
|
+
error: {
|
|
248
|
+
message,
|
|
249
|
+
stage
|
|
250
|
+
},
|
|
251
|
+
event,
|
|
252
|
+
memoryPrepare,
|
|
253
|
+
memoryEnabled: Boolean(this.memory),
|
|
254
|
+
model: this.traceModel,
|
|
255
|
+
outputText: chunks.join("\n").trim(),
|
|
256
|
+
promptText,
|
|
257
|
+
runtimeHomePath: this.runtimeHomePath,
|
|
258
|
+
session: selectedSession,
|
|
259
|
+
startedAt,
|
|
260
|
+
status: "failed",
|
|
261
|
+
tools,
|
|
262
|
+
totalMs: Date.now() - startedAtMs,
|
|
263
|
+
...(this.worldToolContext === undefined
|
|
264
|
+
? {}
|
|
265
|
+
: { worldContextBound: worldContext !== undefined })
|
|
266
|
+
}).catch(() => undefined);
|
|
267
|
+
const persistRawCapture = !rawTrainingCapturePersistAttempted
|
|
268
|
+
&& selectedSession !== undefined;
|
|
269
|
+
if (persistRawCapture && rawTrainingCapture !== undefined) {
|
|
270
|
+
rawTrainingCapturePersistAttempted = true;
|
|
271
|
+
}
|
|
272
|
+
await persistPiTurnArtifacts({
|
|
273
|
+
agentId: this.id,
|
|
274
|
+
completedAt: new Date(),
|
|
275
|
+
model: this.traceModel,
|
|
276
|
+
piSessionForRawCapture: persistRawCapture
|
|
277
|
+
? this.piSessionForRawCapture
|
|
278
|
+
: undefined,
|
|
279
|
+
promptText,
|
|
280
|
+
rawTrainingCapture: persistRawCapture ? rawTrainingCapture : undefined,
|
|
281
|
+
rawTrainingCaptureOptions: persistRawCapture
|
|
282
|
+
? this.rawTrainingCaptureOptions
|
|
283
|
+
: undefined,
|
|
284
|
+
runtimeHomePath: this.runtimeHomePath,
|
|
285
|
+
startedAt,
|
|
286
|
+
status: "failed",
|
|
287
|
+
totalMs: Date.now() - startedAtMs,
|
|
288
|
+
turnId: event.id,
|
|
289
|
+
worldContext,
|
|
290
|
+
worldTrajectory,
|
|
291
|
+
worldTrajectoryIdentity: this.worldTrajectoryIdentity
|
|
292
|
+
});
|
|
293
|
+
throw error;
|
|
294
|
+
}
|
|
295
|
+
finally {
|
|
296
|
+
if (this.memoryToolContext !== undefined) {
|
|
297
|
+
this.memoryToolContext.current = undefined;
|
|
298
|
+
this.memoryToolContext.observeTool = undefined;
|
|
299
|
+
}
|
|
300
|
+
if (this.worldToolContext) {
|
|
301
|
+
this.worldToolContext.current = undefined;
|
|
302
|
+
}
|
|
303
|
+
if (this.rawTrainingCaptureRef) {
|
|
304
|
+
this.rawTrainingCaptureRef.current = undefined;
|
|
305
|
+
}
|
|
306
|
+
unsubscribe?.();
|
|
307
|
+
if (selectedSession?.disposeAfterWake) {
|
|
308
|
+
selectedSession.session.dispose();
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
status() {
|
|
313
|
+
return {
|
|
314
|
+
agentId: this.id,
|
|
315
|
+
state: this.state,
|
|
316
|
+
lastWakeAt: this.lastWakeAt,
|
|
317
|
+
lastError: this.lastError
|
|
318
|
+
};
|
|
319
|
+
}
|
|
320
|
+
async stop() {
|
|
321
|
+
this.session.dispose();
|
|
322
|
+
this.state = "stopped";
|
|
323
|
+
}
|
|
324
|
+
}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import type { createAgentSession } from "@earendil-works/pi-coding-agent";
|
|
2
|
+
import type { WakeMemoryContext, MemoryWakeMode } from "@noopolis/mneme";
|
|
3
|
+
import type { WakeEvent, WakeResult } from "../core/types.js";
|
|
4
|
+
import { type PiRawTrainingCapture, type PiRawTrainingCaptureOptions } from "./rawTrainingCapture.js";
|
|
5
|
+
import { type PiTurnTraceModel, type PiTurnTraceToolEvent } from "./turnTrace.js";
|
|
6
|
+
import type { PiWorldTurnContext } from "./worldNudge.js";
|
|
7
|
+
import { type WakeAcceptanceCapability, type WakeAcceptanceStoreLike } from "./wakeAcceptance.js";
|
|
8
|
+
import { type PiWorldTrajectoryCapture, type PiWorldTrajectoryIdentity } from "./worldTrajectory.js";
|
|
9
|
+
export type PiSession = Awaited<ReturnType<typeof createAgentSession>>["session"];
|
|
10
|
+
export interface PiSessionLike {
|
|
11
|
+
subscribe(listener: Parameters<PiSession["subscribe"]>[0]): () => void;
|
|
12
|
+
prompt(text: string, options?: Parameters<PiSession["prompt"]>[1]): Promise<void>;
|
|
13
|
+
dispose(): void;
|
|
14
|
+
}
|
|
15
|
+
export type PiSessionCreator = (mode: MemoryWakeMode, sessionDirectory: string) => Promise<PiSessionLike>;
|
|
16
|
+
export type PiNativeSessionCreator = (mode: MemoryWakeMode, sessionDirectory: string) => Promise<PiSession>;
|
|
17
|
+
export interface WakeSessionSelection {
|
|
18
|
+
disposeAfterWake: boolean;
|
|
19
|
+
mode: MemoryWakeMode;
|
|
20
|
+
session: PiSessionLike;
|
|
21
|
+
threadId: string;
|
|
22
|
+
}
|
|
23
|
+
type WakeRunner = (event: WakeEvent, transitionToInvoking?: () => Promise<WakeAcceptanceCapability>) => Promise<WakeResult>;
|
|
24
|
+
export declare class PiWakeDeliveryQueue {
|
|
25
|
+
private readonly agentId;
|
|
26
|
+
private readonly acceptance;
|
|
27
|
+
private queue;
|
|
28
|
+
private readonly inProgress;
|
|
29
|
+
constructor(agentId: string, acceptance: WakeAcceptanceStoreLike);
|
|
30
|
+
wake(event: WakeEvent, run: WakeRunner): Promise<WakeResult>;
|
|
31
|
+
private enqueue;
|
|
32
|
+
private runDelivery;
|
|
33
|
+
}
|
|
34
|
+
export declare const cloneWakeEvent: (event: WakeEvent) => WakeEvent;
|
|
35
|
+
export declare const selectPiSessionForWake: (input: {
|
|
36
|
+
agentId: string;
|
|
37
|
+
createSession: PiSessionCreator;
|
|
38
|
+
event: WakeEvent;
|
|
39
|
+
memoryContext: WakeMemoryContext;
|
|
40
|
+
runtimeHomePath: string;
|
|
41
|
+
session: PiSessionLike;
|
|
42
|
+
}) => Promise<WakeSessionSelection>;
|
|
43
|
+
export declare const subscribeToPiTurnEvents: (input: {
|
|
44
|
+
chunks: string[];
|
|
45
|
+
rawTrainingCapture?: PiRawTrainingCapture;
|
|
46
|
+
session: PiSessionLike;
|
|
47
|
+
tools: PiTurnTraceToolEvent[];
|
|
48
|
+
worldTrajectory?: PiWorldTrajectoryCapture;
|
|
49
|
+
}) => (() => void);
|
|
50
|
+
export declare const persistPiTurnArtifacts: (input: {
|
|
51
|
+
agentId: string;
|
|
52
|
+
completedAt: Date;
|
|
53
|
+
model: PiTurnTraceModel;
|
|
54
|
+
piSessionForRawCapture?: PiSession;
|
|
55
|
+
promptText: string;
|
|
56
|
+
rawTrainingCapture?: PiRawTrainingCapture;
|
|
57
|
+
rawTrainingCaptureOptions?: PiRawTrainingCaptureOptions;
|
|
58
|
+
runtimeHomePath: string;
|
|
59
|
+
startedAt: Date;
|
|
60
|
+
status: "completed" | "failed";
|
|
61
|
+
totalMs: number;
|
|
62
|
+
turnId: string;
|
|
63
|
+
worldContext?: PiWorldTurnContext;
|
|
64
|
+
worldTrajectory?: PiWorldTrajectoryCapture;
|
|
65
|
+
worldTrajectoryIdentity?: PiWorldTrajectoryIdentity;
|
|
66
|
+
}) => Promise<void>;
|
|
67
|
+
export {};
|
|
68
|
+
//# sourceMappingURL=piAgentWakeSupport.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"piAgentWakeSupport.d.ts","sourceRoot":"","sources":["../../src/pi/piAgentWakeSupport.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,iCAAiC,CAAC;AAC1E,OAAO,KAAK,EAAE,iBAAiB,EAAE,cAAc,EAAE,MAAM,iBAAiB,CAAC;AAEzE,OAAO,KAAK,EAAE,SAAS,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC;AAE9D,OAAO,EAGL,KAAK,oBAAoB,EACzB,KAAK,2BAA2B,EACjC,MAAM,yBAAyB,CAAC;AACjC,OAAO,EAEL,KAAK,gBAAgB,EACrB,KAAK,oBAAoB,EAC1B,MAAM,gBAAgB,CAAC;AAOxB,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,iBAAiB,CAAC;AAC1D,OAAO,EAEL,KAAK,wBAAwB,EAC7B,KAAK,uBAAuB,EAC7B,MAAM,qBAAqB,CAAC;AAC7B,OAAO,EAGL,KAAK,wBAAwB,EAC7B,KAAK,yBAAyB,EAC/B,MAAM,sBAAsB,CAAC;AAE9B,MAAM,MAAM,SAAS,GAAG,OAAO,CAAC,UAAU,CAAC,OAAO,kBAAkB,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC;AAClF,MAAM,WAAW,aAAa;IAC5B,SAAS,CAAC,QAAQ,EAAE,UAAU,CAAC,SAAS,CAAC,WAAW,CAAC,CAAC,CAAC,CAAC,CAAC,GAAG,MAAM,IAAI,CAAC;IACvE,MAAM,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,UAAU,CA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Aa,EAAE,iBAAiB,CAAC;IACjC,eAAe,EAAE,MAAM,CAAC;IACxB,OAAO,EAAE,aAAa,CAAC;CACxB,KAAG,OAAO,CAAC,oBAAoB,CAmB/B,CAAC;AAEF,eAAO,MAAM,uBAAuB,GAAI,OAAO;IAC7C,MAAM,EAAE,MAAM,EAAE,CAAC;IACjB,kBAAkB,CAAC,EAAE,oBAAoB,CAAC;IAC1C,OAAO,EAAE,aAAa,CAAC;IACvB,KAAK,EAAE,oBAAoB,EAAE,CAAC;IAC9B,eAAe,CAAC,EAAE,wBAAwB,CAAC;CAC5C,KAAG,CAAC,MAAM,IAAI,CAkBd,CAAC;AAEF,eAAO,MAAM,sBAAsB,GAAU,OAAO;IAClD,OAAO,EAAE,MAAM,CAAC;IAChB,WAAW,EAAE,IAAI,CAAC;IAClB,KAAK,EAAE,gBAAgB,CAAC;IACxB,sBAAsB,CAAC,EAAE,SAAS,CAAC;IACnC,UAAU,EAAE,MAAM,CAAC;IACnB,kBAAkB,CAAC,EAAE,oBAAoB,CAAC;IAC1C,yBAAyB,CAAC,EAAE,2BAA2B,CAAC;IACxD,eAAe,EAAE,MAAM,CAAC;IACxB,SAAS,EAAE,IAAI,CAAC;IAChB,MAAM,EAAE,WAAW,GAAG,QAAQ,CAAC;IAC/B,OAAO,EAAE,MAAM,CAAC;IAChB,MAAM,EAAE,MAAM,CAAC;IACf,YAAY,CAAC,EAAE,kBAAkB,CAAC;IAClC,eAAe,CAAC,EAAE,wBAAwB,CAAC;IAC3C,uBAAuB,CAAC,EAAE,yBAAyB,CAAC;CACrD,KAAG,OAAO,CAAC,IAAI,CAqCf,CAAC"}
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
import { capturePiRawTrainingEvent, persistPiRawTrainingCapture, } from "./rawTrainingCapture.js";
|
|
2
|
+
import { summarizeSessionEvent, } from "./turnTrace.js";
|
|
3
|
+
import { createAwakeThreadId, createDreamSessionDirectory, createDreamSessionKey, createDreamThreadId, } from "./wakeModes.js";
|
|
4
|
+
import { WakeAcceptanceError, } from "./wakeAcceptance.js";
|
|
5
|
+
import { capturePiWorldTrajectoryEvent, persistPiWorldTrajectory, } from "./worldTrajectory.js";
|
|
6
|
+
export class PiWakeDeliveryQueue {
|
|
7
|
+
agentId;
|
|
8
|
+
acceptance;
|
|
9
|
+
queue = Promise.resolve();
|
|
10
|
+
inProgress = new Map();
|
|
11
|
+
constructor(agentId, acceptance) {
|
|
12
|
+
this.agentId = agentId;
|
|
13
|
+
this.acceptance = acceptance;
|
|
14
|
+
}
|
|
15
|
+
async wake(event, run) {
|
|
16
|
+
const candidate = event.delivery === undefined
|
|
17
|
+
? undefined
|
|
18
|
+
: this.acceptance.candidateFromDelivery(event);
|
|
19
|
+
if (candidate === undefined)
|
|
20
|
+
return this.enqueue(() => run(event));
|
|
21
|
+
const active = this.inProgress.get(candidate.identity);
|
|
22
|
+
if (active !== undefined) {
|
|
23
|
+
if (active.digest !== candidate.digest) {
|
|
24
|
+
throw new WakeAcceptanceError("wake_delivery_conflict");
|
|
25
|
+
}
|
|
26
|
+
return active.promise;
|
|
27
|
+
}
|
|
28
|
+
const queued = this.enqueue(() => this.runDelivery(event, run));
|
|
29
|
+
const promise = queued.finally(() => {
|
|
30
|
+
if (this.inProgress.get(candidate.identity)?.promise === promise) {
|
|
31
|
+
this.inProgress.delete(candidate.identity);
|
|
32
|
+
}
|
|
33
|
+
});
|
|
34
|
+
this.inProgress.set(candidate.identity, { digest: candidate.digest, promise });
|
|
35
|
+
return promise;
|
|
36
|
+
}
|
|
37
|
+
enqueue(run) {
|
|
38
|
+
const queued = this.queue.then(run, run);
|
|
39
|
+
this.queue = queued.then(() => undefined, () => undefined);
|
|
40
|
+
return queued;
|
|
41
|
+
}
|
|
42
|
+
async runDelivery(event, run) {
|
|
43
|
+
const admission = await this.acceptance.begin(event);
|
|
44
|
+
if (admission.mode === "replay") {
|
|
45
|
+
return { agentId: this.agentId, text: "", durationMs: 0 };
|
|
46
|
+
}
|
|
47
|
+
let capability = admission.capability;
|
|
48
|
+
try {
|
|
49
|
+
const result = await run(event, async () => {
|
|
50
|
+
capability = await this.acceptance.markInvoking(capability);
|
|
51
|
+
return capability;
|
|
52
|
+
});
|
|
53
|
+
await this.acceptance.markCompleted(capability);
|
|
54
|
+
return result;
|
|
55
|
+
}
|
|
56
|
+
catch (error) {
|
|
57
|
+
await this.acceptance.markIncomplete(capability).catch(() => undefined);
|
|
58
|
+
throw error;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
const cloneContext = (context) => ({
|
|
63
|
+
...context,
|
|
64
|
+
...(context?.pairPeers === undefined ? {} : { pairPeers: [...context.pairPeers] }),
|
|
65
|
+
...(context?.artifactPaths === undefined ? {} : { artifactPaths: [...context.artifactPaths] }),
|
|
66
|
+
});
|
|
67
|
+
export const cloneWakeEvent = (event) => ({
|
|
68
|
+
...event,
|
|
69
|
+
...(event.delivery === undefined ? {} : { delivery: { ...event.delivery } }),
|
|
70
|
+
...(event.context === undefined ? {} : { context: cloneContext(event.context) }),
|
|
71
|
+
});
|
|
72
|
+
export const selectPiSessionForWake = async (input) => {
|
|
73
|
+
if (input.event.kind !== "dream") {
|
|
74
|
+
return {
|
|
75
|
+
disposeAfterWake: false,
|
|
76
|
+
mode: "awake",
|
|
77
|
+
session: input.session,
|
|
78
|
+
threadId: createAwakeThreadId(input.memoryContext, input.agentId),
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
const sessionKey = createDreamSessionKey(input.event);
|
|
82
|
+
return {
|
|
83
|
+
disposeAfterWake: true,
|
|
84
|
+
mode: "dream",
|
|
85
|
+
session: await input.createSession("dream", createDreamSessionDirectory(input.runtimeHomePath, sessionKey)),
|
|
86
|
+
threadId: createDreamThreadId(sessionKey),
|
|
87
|
+
};
|
|
88
|
+
};
|
|
89
|
+
export const subscribeToPiTurnEvents = (input) => {
|
|
90
|
+
const unsubscribe = input.session.subscribe((event) => {
|
|
91
|
+
if (input.rawTrainingCapture !== undefined) {
|
|
92
|
+
capturePiRawTrainingEvent(input.rawTrainingCapture, event);
|
|
93
|
+
}
|
|
94
|
+
if (input.worldTrajectory !== undefined) {
|
|
95
|
+
capturePiWorldTrajectoryEvent(input.worldTrajectory, event);
|
|
96
|
+
}
|
|
97
|
+
const toolEvent = summarizeSessionEvent(event);
|
|
98
|
+
if (toolEvent !== undefined)
|
|
99
|
+
input.tools.push(toolEvent);
|
|
100
|
+
if (event.type !== "turn_end" || !("content" in event.message))
|
|
101
|
+
return;
|
|
102
|
+
const { content } = event.message;
|
|
103
|
+
input.chunks.push(typeof content === "string"
|
|
104
|
+
? content
|
|
105
|
+
: content.filter((entry) => entry.type === "text")
|
|
106
|
+
.map((entry) => entry.text).join(""));
|
|
107
|
+
});
|
|
108
|
+
return unsubscribe;
|
|
109
|
+
};
|
|
110
|
+
export const persistPiTurnArtifacts = async (input) => {
|
|
111
|
+
if (input.rawTrainingCapture !== undefined
|
|
112
|
+
&& input.rawTrainingCaptureOptions !== undefined
|
|
113
|
+
&& input.piSessionForRawCapture !== undefined) {
|
|
114
|
+
await persistPiRawTrainingCapture({
|
|
115
|
+
agentId: input.agentId,
|
|
116
|
+
capture: input.rawTrainingCapture,
|
|
117
|
+
completedAt: input.completedAt,
|
|
118
|
+
options: input.rawTrainingCaptureOptions,
|
|
119
|
+
runtimeHomePath: input.runtimeHomePath,
|
|
120
|
+
session: input.piSessionForRawCapture,
|
|
121
|
+
startedAt: input.startedAt,
|
|
122
|
+
status: input.status,
|
|
123
|
+
totalMs: input.totalMs,
|
|
124
|
+
turnId: input.turnId,
|
|
125
|
+
world: input.worldContext,
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
if (input.worldContext !== undefined
|
|
129
|
+
&& input.worldTrajectory !== undefined
|
|
130
|
+
&& input.worldTrajectoryIdentity !== undefined) {
|
|
131
|
+
await persistPiWorldTrajectory({
|
|
132
|
+
agentId: input.agentId,
|
|
133
|
+
capture: input.worldTrajectory,
|
|
134
|
+
completedAt: input.completedAt,
|
|
135
|
+
context: input.worldContext,
|
|
136
|
+
instructions: input.worldTrajectoryIdentity.instructions,
|
|
137
|
+
model: input.model,
|
|
138
|
+
promptText: input.promptText,
|
|
139
|
+
runtimeHomePath: input.runtimeHomePath,
|
|
140
|
+
startedAt: input.startedAt,
|
|
141
|
+
status: input.status,
|
|
142
|
+
thinkingLevel: input.worldTrajectoryIdentity.thinkingLevel,
|
|
143
|
+
totalMs: input.totalMs,
|
|
144
|
+
turnId: input.turnId,
|
|
145
|
+
});
|
|
146
|
+
}
|
|
147
|
+
};
|
package/dist/pi/piHarness.d.ts
CHANGED
|
@@ -1,8 +1,16 @@
|
|
|
1
1
|
import { createAgentSession } from "@earendil-works/pi-coding-agent";
|
|
2
|
+
import { type MemoryAuthorityConfig } from "@noopolis/mneme";
|
|
2
3
|
import type { AgentHandle, AgentHarnessAdapter, AgentStartInput, HarnessModelSpec } from "../core/types.js";
|
|
3
|
-
|
|
4
|
+
import { type PiSessionLike } from "./piAgentHandle.js";
|
|
5
|
+
import { type PiWorldBinding } from "./worldTools.js";
|
|
6
|
+
import { type PiRawTrainingCaptureOptions } from "./rawTrainingCapture.js";
|
|
7
|
+
type HarnessMemoryEmbeddingProvider = {
|
|
8
|
+
dimensions?: number;
|
|
9
|
+
embed(text: string): Promise<number[]>;
|
|
10
|
+
};
|
|
11
|
+
export type PiThinkingLevel = NonNullable<NonNullable<Parameters<typeof createAgentSession>[0]>["thinkingLevel"]>;
|
|
12
|
+
type PiHarnessBaseOptions = {
|
|
4
13
|
authPath: string;
|
|
5
|
-
sessionFactory?: PiSessionFactory;
|
|
6
14
|
model?: {
|
|
7
15
|
auth?: HarnessModelSpec["auth"];
|
|
8
16
|
endpoint?: HarnessModelSpec["endpoint"];
|
|
@@ -11,11 +19,28 @@ export interface PiHarnessOptions {
|
|
|
11
19
|
};
|
|
12
20
|
modelsPath?: string;
|
|
13
21
|
memory?: {
|
|
22
|
+
authority?: MemoryAuthorityConfig;
|
|
23
|
+
embeddingProvider?: HarnessMemoryEmbeddingProvider;
|
|
14
24
|
source?: string;
|
|
15
25
|
tokenBudget?: number;
|
|
26
|
+
runtimeHomePath?: string;
|
|
16
27
|
};
|
|
17
|
-
|
|
18
|
-
|
|
28
|
+
thinkingLevel?: PiThinkingLevel;
|
|
29
|
+
world?: PiWorldBinding;
|
|
30
|
+
};
|
|
31
|
+
export type PiHarnessOptions = PiHarnessBaseOptions & ({
|
|
32
|
+
rawTrainingCapture?: PiRawTrainingCaptureOptions;
|
|
33
|
+
sessionFactory?: never;
|
|
34
|
+
} | {
|
|
35
|
+
rawTrainingCapture?: never;
|
|
36
|
+
sessionFactory: PiSessionFactory;
|
|
37
|
+
});
|
|
38
|
+
export type PiSessionFactoryInput = Exclude<Parameters<typeof createAgentSession>[0], undefined> & {
|
|
39
|
+
daimonSecretEnvironmentNames?: readonly string[];
|
|
40
|
+
};
|
|
41
|
+
export type PiSessionFactory = (input: PiSessionFactoryInput) => Promise<{
|
|
42
|
+
session: PiSessionLike;
|
|
43
|
+
}>;
|
|
19
44
|
export declare class PiHarnessAdapter implements AgentHarnessAdapter {
|
|
20
45
|
private readonly options;
|
|
21
46
|
private readonly authStorage;
|
|
@@ -24,4 +49,5 @@ export declare class PiHarnessAdapter implements AgentHarnessAdapter {
|
|
|
24
49
|
constructor(options: PiHarnessOptions);
|
|
25
50
|
startAgent(input: AgentStartInput): Promise<AgentHandle>;
|
|
26
51
|
}
|
|
52
|
+
export {};
|
|
27
53
|
//# sourceMappingURL=piHarness.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"piHarness.d.ts","sourceRoot":"","sources":["../../src/pi/piHarness.ts"],"names":[],"mappings":"AAGA,OAAO,EAEL,kBAAkB,
|
|
1
|
+
{"version":3,"file":"piHarness.d.ts","sourceRoot":"","sources":["../../src/pi/piHarness.ts"],"names":[],"mappings":"AAGA,OAAO,EAEL,kBAAkB,EAInB,MAAM,iCAAiC,CAAC;AACzC,OA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