@centlu/agent-core 0.3.0 → 1.0.0
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 +2 -0
- package/package.json +1 -1
- package/src/agent/oh-my-pi/history.ts +113 -0
- package/src/agent/oh-my-pi/index.ts +7 -2
- package/src/agent/store.ts +1 -0
- package/src/manage.ts +2 -1
package/README.md
CHANGED
|
@@ -16,6 +16,8 @@ Create the manager with `createManager(store, life)` and bind one listener per s
|
|
|
16
16
|
|
|
17
17
|
History stores receive the original non-delta events. Live delivery proceeds independently of history writes. The listener's `seq` orders live delivery within each binding and running session. Load the stored events for historical playback or to recover complete messages after reconnecting.
|
|
18
18
|
|
|
19
|
+
When `createManager` opens or reopens a session, it loads that session's events in append order and uses completed messages and tool results to seed the in-memory Pi context before processing input. Restoration does not emit historical events. Compaction summaries and provider-native metadata are unavailable in the stored event format.
|
|
20
|
+
|
|
19
21
|
## Session Close
|
|
20
22
|
|
|
21
23
|
`SessionManager.close(id)` gracefully drains the session. A close from another Pod uses the shared `HistoryStore` pending queue and waits up to 30 seconds for disposal to finish or for the targeted owner to leave. It throws on timeout or if the owner reports a disposal failure.
|
package/package.json
CHANGED
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import type {AssistantMessage, ImageContent, TextContent, ToolResultMessage} from "@oh-my-pi/pi-ai"
|
|
2
|
+
import type {SessionManager} from "@oh-my-pi/pi-coding-agent/session/session-manager"
|
|
3
|
+
import type {AgentEvent, JsonValue, Outcome} from "../../event.ts"
|
|
4
|
+
|
|
5
|
+
type ToolResultEvent = Extract<AgentEvent, {type: "tool.execution.ended"}>
|
|
6
|
+
type ToolReady = Extract<AgentEvent, {type: "tool.call.ready"}>["payload"]
|
|
7
|
+
|
|
8
|
+
function isObject(value: JsonValue | undefined): value is Record<string, JsonValue> {
|
|
9
|
+
return value != null && typeof value === "object" && !Array.isArray(value)
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
function toolKey(event: AgentEvent, toolCallId: string): string {
|
|
13
|
+
return JSON.stringify([event.runId, event.turnId, toolCallId])
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
function restoreToolResult(
|
|
17
|
+
toolCallId: string, toolName: string, timestamp: number, event?: ToolResultEvent,
|
|
18
|
+
): ToolResultMessage {
|
|
19
|
+
const result = event?.payload.result
|
|
20
|
+
const content: (TextContent | ImageContent)[] = []
|
|
21
|
+
if (isObject(result) && Array.isArray(result.content)) {
|
|
22
|
+
for (const part of result.content) {
|
|
23
|
+
if (!isObject(part)) continue
|
|
24
|
+
if (part.type === "text" && typeof part.text === "string") {
|
|
25
|
+
content.push({type: "text", text: part.text})
|
|
26
|
+
} else if (part.type === "image" && typeof part.data === "string" && typeof part.mimeType === "string") {
|
|
27
|
+
content.push({type: "image", data: part.data, mimeType: part.mimeType})
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
} else if (result !== undefined) {
|
|
31
|
+
content.push({type: "text", text: typeof result === "string" ? result : JSON.stringify(result)})
|
|
32
|
+
}
|
|
33
|
+
if (content.length === 0) {
|
|
34
|
+
const text = event?.payload.error?.message ?? (event ? "" : "No result was recorded for this tool call.")
|
|
35
|
+
content.push({type: "text", text})
|
|
36
|
+
}
|
|
37
|
+
return {
|
|
38
|
+
role: "toolResult", toolCallId, toolName, content,
|
|
39
|
+
isError: event?.payload.status !== "succeeded",
|
|
40
|
+
timestamp: event?.timestamp ?? timestamp,
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export function restoreHistory(sessionManager: SessionManager, history: readonly AgentEvent[]): void {
|
|
45
|
+
const calls = new Map<string, Map<string, ToolReady>>()
|
|
46
|
+
const results = new Map<string, ToolResultEvent>()
|
|
47
|
+
const turns = new Map<string, Outcome>()
|
|
48
|
+
for (const event of history) {
|
|
49
|
+
switch (event.type) {
|
|
50
|
+
case "tool.call.ready": {
|
|
51
|
+
const {messageId, toolCallId} = event.payload
|
|
52
|
+
const messageCalls = calls.get(messageId) ?? new Map<string, ToolReady>()
|
|
53
|
+
messageCalls.set(toolCallId, event.payload)
|
|
54
|
+
calls.set(messageId, messageCalls)
|
|
55
|
+
break
|
|
56
|
+
}
|
|
57
|
+
case "tool.execution.ended":
|
|
58
|
+
results.set(toolKey(event, event.payload.toolCallId), event)
|
|
59
|
+
break
|
|
60
|
+
case "turn.ended":
|
|
61
|
+
turns.set(event.payload.turnId, event.payload.status)
|
|
62
|
+
break
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
for (const event of history) {
|
|
67
|
+
if (event.type !== "message.ended") continue
|
|
68
|
+
const {role, messageId} = event.payload
|
|
69
|
+
const content: AssistantMessage["content"] = []
|
|
70
|
+
const toolResults: ToolResultMessage[] = []
|
|
71
|
+
for (const part of [...event.payload.content].sort((left, right) => left.partIndex - right.partIndex)) {
|
|
72
|
+
if (part.kind === "text") content.push({type: "text", text: part.text})
|
|
73
|
+
if (part.kind === "image") content.push({type: "image", data: part.data, mimeType: part.mimeType})
|
|
74
|
+
if (role !== "assistant") continue
|
|
75
|
+
if (part.kind === "thinking_summary") {
|
|
76
|
+
content.push({type: "thinking", thinking: part.text, summary: true})
|
|
77
|
+
}
|
|
78
|
+
if (part.kind === "tool_call") {
|
|
79
|
+
const {toolCallId} = part
|
|
80
|
+
const call = calls.get(messageId)?.get(toolCallId)
|
|
81
|
+
if (!call) continue
|
|
82
|
+
const {toolName, args} = call
|
|
83
|
+
content.push({type: "toolCall", id: toolCallId, name: toolName, arguments: isObject(args) ? args : {}})
|
|
84
|
+
const result = results.get(toolKey(event, toolCallId))
|
|
85
|
+
toolResults.push(restoreToolResult(toolCallId, toolName, event.timestamp, result))
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
const {timestamp} = event
|
|
90
|
+
if (role !== "assistant") {
|
|
91
|
+
sessionManager.appendMessage({
|
|
92
|
+
role: role === "system" ? "developer" : "user",
|
|
93
|
+
content: content.filter(part => part.type === "image" || part.type === "text"),
|
|
94
|
+
timestamp,
|
|
95
|
+
})
|
|
96
|
+
continue
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
const status = event.turnId === undefined ? undefined : turns.get(event.turnId)
|
|
100
|
+
if (content.length === 0 && status !== "failed" && status !== "cancelled") continue
|
|
101
|
+
sessionManager.appendMessage({
|
|
102
|
+
role, content, timestamp,
|
|
103
|
+
api: "openai-completions", provider: "agent-core-history", model: "<synthetic>",
|
|
104
|
+
usage: {
|
|
105
|
+
input: 0, output: 0, cacheRead: 0, cacheWrite: 0, totalTokens: 0,
|
|
106
|
+
cost: {input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0},
|
|
107
|
+
},
|
|
108
|
+
stopReason: status === "failed" ? "error" : status === "cancelled" ? "aborted"
|
|
109
|
+
: toolResults.length > 0 ? "toolUse" : "stop",
|
|
110
|
+
})
|
|
111
|
+
for (const result of toolResults) sessionManager.appendMessage(result)
|
|
112
|
+
}
|
|
113
|
+
}
|
|
@@ -6,11 +6,16 @@ import type {AgentEvent, AgentOptions, UserOperation} from "../../model.ts"
|
|
|
6
6
|
import {AbstractAgent, type AgentCore} from "../core.ts"
|
|
7
7
|
import {applyTurnConfig} from "./config.ts";
|
|
8
8
|
import {toEvents} from "./events.ts"
|
|
9
|
+
import {restoreHistory} from "./history.ts"
|
|
9
10
|
|
|
10
|
-
export async function CreateOhMyPiAgent(
|
|
11
|
+
export async function CreateOhMyPiAgent(
|
|
12
|
+
id: string, options: AgentOptions, history: readonly AgentEvent[] = [],
|
|
13
|
+
): Promise<AgentCore> {
|
|
14
|
+
const sessionManager = SessionManager.inMemory()
|
|
15
|
+
restoreHistory(sessionManager, history)
|
|
11
16
|
const {session} = await createAgentSession({
|
|
12
17
|
cwd: options.cwd,
|
|
13
|
-
sessionManager
|
|
18
|
+
sessionManager,
|
|
14
19
|
})
|
|
15
20
|
await applyTurnConfig(session, options.setting)
|
|
16
21
|
|
package/src/agent/store.ts
CHANGED
|
@@ -19,6 +19,7 @@ export type SessionCloseResult = {
|
|
|
19
19
|
export type PendingMessage = Submission | SessionCloseRequest | SessionCloseResult
|
|
20
20
|
|
|
21
21
|
export interface HistoryStore {
|
|
22
|
+
/** Load stored events for this session in append order. */
|
|
22
23
|
load_history_from_store(id: string): Promise<Array<AgentEvent>>
|
|
23
24
|
append_history_to_store(event: AgentEvent): Promise<void>
|
|
24
25
|
|
package/src/manage.ts
CHANGED
|
@@ -261,6 +261,7 @@ export function createManager(
|
|
|
261
261
|
life: AgentLife,
|
|
262
262
|
): SessionManager {
|
|
263
263
|
return new SessionManager(store, life, async (id, options) => {
|
|
264
|
-
|
|
264
|
+
const history = await store.load_history_from_store(id)
|
|
265
|
+
return CreateOhMyPiAgent(id, options, history)
|
|
265
266
|
})
|
|
266
267
|
}
|