(
ref: ChannelMessageRef,
creds: GraphCredentials,
config: FileDeliveryConfig = {},
)
| 153 | * note explains why (so the caller can tell the user to paste the data). |
| 154 | */ |
| 155 | export async function buildChannelFileContentParts( |
| 156 | ref: ChannelMessageRef, |
| 157 | creds: GraphCredentials, |
| 158 | config: FileDeliveryConfig = {}, |
| 159 | ): Promise<{ parts: AgentContentPart[]; notes: string[] }> { |
| 160 | const parts: AgentContentPart[] = []; |
| 161 | const notes: string[] = []; |
| 162 | |
| 163 | let token: string; |
| 164 | try { |
| 165 | token = await acquireAppToken(creds); |
| 166 | } catch (err) { |
| 167 | notes.push(`couldn't get a Graph token: ${(err as Error).message}`); |
| 168 | return { parts, notes }; |
| 169 | } |
| 170 | |
| 171 | let refs: GraphFileRef[]; |
| 172 | try { |
| 173 | refs = await getMessageFileRefs(ref, token); |
| 174 | } catch (err) { |
| 175 | notes.push( |
| 176 | `couldn't read channel-message files via Graph (is ChannelMessage.Read.Group granted?): ${(err as Error).message}`, |
| 177 | ); |
| 178 | return { parts, notes }; |
| 179 | } |
| 180 | |
| 181 | const maxFiles = config.maxFiles ?? 5; |
| 182 | for (const f of refs.slice(0, maxFiles)) { |
| 183 | let bytes: Buffer; |
| 184 | try { |
| 185 | bytes = await downloadSharedFile(f.contentUrl, token); |
| 186 | } catch (err) { |
| 187 | notes.push( |
| 188 | `skipped "${f.name}" (is Files.Read.All granted?): ${(err as Error).message}`, |
| 189 | ); |
| 190 | continue; |
| 191 | } |
| 192 | const mime = mimeFromName(f.name) ?? "application/octet-stream"; |
| 193 | const decoded = decodeFileBytes(f.name, mime, bytes, config); |
| 194 | if ("note" in decoded) notes.push(decoded.note); |
| 195 | else parts.push(decoded.part); |
| 196 | } |
| 197 | |
| 198 | return { parts, notes }; |
| 199 | } |
no test coverage detected
searching dependent graphs…