MCPcopy Create free account
hub / github.com/anomalyco/opencode / serialize

Function serialize

packages/core/src/session/compaction.ts:91–117  ·  view source on GitHub ↗
(message: SessionMessage.Message)

Source from the content-addressed store, hash-verified

89 .join("\n")
90
91const serialize = (message: SessionMessage.Message) => {
92 if (message.type === "user") {
93 const files = message.files?.map((file) => `[Attached ${file.mime}: ${file.name ?? file.uri}]`) ?? []
94 return [`[User]: ${message.text}`, ...files].join("\n")
95 }
96 if (message.type === "assistant") {
97 return message.content
98 .flatMap((part) => {
99 if (part.type === "text") return [`[Assistant]: ${part.text}`]
100 if (part.type === "reasoning") return part.text ? [`[Assistant reasoning]: ${part.text}`] : []
101 const input = typeof part.state.input === "string" ? part.state.input : JSON.stringify(part.state.input)
102 if (part.state.status === "completed")
103 return [
104 `[Assistant tool call]: ${part.name}(${input})`,
105 `[Tool result]: ${truncate(serializeToolContent(part.state.content))}`,
106 ]
107 if (part.state.status === "error")
108 return [`[Assistant tool call]: ${part.name}(${input})`, `[Tool error]: ${part.state.error.message}`]
109 return [`[Assistant tool call]: ${part.name}(${input})`]
110 })
111 .join("\n")
112 }
113 if (message.type === "system") return `[System update]: ${message.text}`
114 if (message.type === "synthetic") return `[Synthetic context]: ${message.text}`
115 if (message.type === "shell") return `[Shell]: ${message.command}\n${truncate(message.output)}`
116 return ""
117}
118
119const settings = (documents: readonly Config.Entry[]) => {
120 const configured = documents

Callers 1

selectFunction · 0.70

Calls 2

serializeToolContentFunction · 0.85
truncateFunction · 0.70

Tested by

no test coverage detected