Open History to see the last commit
1export type DevinResourceContent = {
2 readonly uri: string;
3 readonly name?: string;
4 readonly description?: string;
5 readonly mimeType?: string;
6 readonly text?: string;
7};
8
9export type DevinResourceNormalization =
10 | { readonly kind: "resource"; readonly resource: DevinResourceContent }
11 | { readonly kind: "unsupported"; readonly reason: "invalid" | "oversized" | "binary" };
12
13/** Matches the ACP tool-output retention limit before resource data enters tool event state. */
14export const DEVIN_RESOURCE_TEXT_MAX_CHARS = 8_000;
15
16function isRecord(value: unknown): value is Record<string, unknown> {
17 return typeof value === "object" && value !== null && !Array.isArray(value);
18}
19
20function boundedString(value: unknown): string | undefined {
21 return typeof value === "string" && value.length <= DEVIN_RESOURCE_TEXT_MAX_CHARS
22 ? value
23 : undefined;
24}
25
26function resourceLink(content: Record<string, unknown>): DevinResourceNormalization {
27 const uri = boundedString(content.uri);
28 const name = boundedString(content.name);
29 const description = content.description === null ? undefined : boundedString(content.description);
30 const mimeType = content.mimeType === null ? undefined : boundedString(content.mimeType);
31 if (
32 !uri ||
33 !name ||
34 (content.description !== undefined &&
35 content.description !== null &&
36 description === undefined) ||
37 (content.mimeType !== undefined && content.mimeType !== null && mimeType === undefined)
38 ) {
39 return {
40 kind: "unsupported",
41 reason: [content.uri, content.name, content.description, content.mimeType].some(
42 (value) => typeof value === "string" && value.length > DEVIN_RESOURCE_TEXT_MAX_CHARS,
43 )
44 ? "oversized"
45 : "invalid",
46 };
47 }
48 return {
49 kind: "resource",
50 resource: {
51 uri,
52 name,
53 ...(description !== undefined ? { description } : {}),
54 ...(mimeType !== undefined ? { mimeType } : {}),
55 },
56 };
57}
58
59function embeddedResource(content: Record<string, unknown>): DevinResourceNormalization {
60 const resource = content.resource;
61 if (!isRecord(resource)) {
62 return { kind: "unsupported", reason: "invalid" };
63 }
64 const uri = boundedString(resource.uri);
65 const mimeType = resource.mimeType === null ? undefined : boundedString(resource.mimeType);
66 if (
67 !uri ||
68 (resource.mimeType !== undefined && resource.mimeType !== null && mimeType === undefined)
69 ) {
70 return {
71 kind: "unsupported",
72 reason: [resource.uri, resource.mimeType].some(
73 (value) => typeof value === "string" && value.length > DEVIN_RESOURCE_TEXT_MAX_CHARS,
74 )
75 ? "oversized"
76 : "invalid",
77 };
78 }
79 if ("blob" in resource) {
80 return typeof resource.blob === "string"
81 ? { kind: "unsupported", reason: "binary" }
82 : { kind: "unsupported", reason: "invalid" };
83 }
84 const text = boundedString(resource.text);
85 if (text === undefined) {
86 return {
87 kind: "unsupported",
88 reason:
89 typeof resource.text === "string" && resource.text.length > DEVIN_RESOURCE_TEXT_MAX_CHARS
90 ? "oversized"
91 : "invalid",
92 };
93 }
94 return {
95 kind: "resource",
96 resource: {
97 uri,
98 ...(mimeType !== undefined ? { mimeType } : {}),
99 text,
100 },
101 };
102}
103
104/**
105 * Selects the ACP resource forms that the typed runtime can carry. It intentionally
106 * ignores ACP metadata and never reads or decodes binary resource blobs.
107 */
108export function normalizeDevinResourceContent(content: unknown): DevinResourceNormalization {
109 if (!isRecord(content) || content.type !== "content" || !isRecord(content.content)) {
110 return { kind: "unsupported", reason: "invalid" };
111 }
112 switch (content.content.type) {
113 case "resource_link":
114 return resourceLink(content.content);
115 case "resource":
116 return embeddedResource(content.content);
117 default:
118 return { kind: "unsupported", reason: "invalid" };
119 }
120}