-
Notifications
You must be signed in to change notification settings - Fork 40
/
selectGenerativeInfo.ts
181 lines (160 loc) · 5.21 KB
/
selectGenerativeInfo.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/**
* Copyright 2023 Adobe
* All Rights Reserved.
*
* NOTICE: Adobe permits you to use, modify, and distribute this file in
* accordance with the terms of the Adobe license agreement accompanying
* it.
*/
import type {
ActionV1,
Assertion,
C2paActionsAssertion,
C2paActionsAssertionV2,
GeneratorInfoMap,
ManifestAssertion,
} from '@contentauth/toolkit';
import { ActionV2 } from '@contentauth/toolkit';
import type { Manifest } from '../manifest';
const genAiDigitalSourceTypes = [
'http://cv.iptc.org/newscodes/digitalsourcetype/trainedAlgorithmicMedia',
'https://cv.iptc.org/newscodes/digitalsourcetype/trainedAlgorithmicMedia',
'http://cv.iptc.org/newscodes/digitalsourcetype/compositeWithTrainedAlgorithmicMedia',
'https://cv.iptc.org/newscodes/digitalsourcetype/compositeWithTrainedAlgorithmicMedia',
];
function formatGenAiDigitalSourceTypes(type: string) {
return type.substring(type.lastIndexOf('/') + 1);
}
export type LegacyAssertion = Assertion<
'com.adobe.generative-ai',
{
description: string;
version: string;
prompt?: string;
}
>;
export type GenAiAssertion = ManifestAssertion | LegacyAssertion;
export interface GenerativeInfo {
assertion: GenAiAssertion;
action?: ActionV1 | ActionV2;
type:
| 'legacy'
| 'trainedAlgorithmicMedia'
| 'compositeWithTrainedAlgorithmicMedia';
softwareAgent: GeneratorInfoMap;
}
/**
* Gets any generative AI information from the manifest.
*
* @param manifest - Manifest to derive data from
*/
export function selectGenerativeInfo(
manifest: Manifest,
): GenerativeInfo[] | null {
const data = manifest.assertions.data.reduce<GenerativeInfo[]>(
(acc, assertion: Assertion<any, any>) => {
// Check for legacy assertion
if (assertion.label === 'com.adobe.generative-ai') {
const { description, version } = (assertion as LegacyAssertion).data;
const softwareAgent = [description, version]
.map((x) => x?.trim() ?? '')
.join(' ');
return [
...acc,
{
assertion,
type: 'legacy',
softwareAgent: { name: softwareAgent },
},
];
}
// Check for actions v1 assertion
if (assertion.label === 'c2pa.actions') {
const { actions } = (assertion as C2paActionsAssertion).data;
const genAiActions: GenerativeInfo[] = actions.reduce<GenerativeInfo[]>(
(actionAcc, action: ActionV1) => {
const { digitalSourceType, softwareAgent } = action;
if (
digitalSourceType &&
genAiDigitalSourceTypes.includes(digitalSourceType)
) {
actionAcc.push({
assertion,
action: action,
type: formatGenAiDigitalSourceTypes(digitalSourceType),
softwareAgent: { name: softwareAgent },
} as GenerativeInfo);
}
return actionAcc;
},
[],
);
return [...acc, ...genAiActions];
}
// Check for actions v2 assertion
if (assertion.label === 'c2pa.actions.v2') {
const { actions } = (assertion as C2paActionsAssertionV2).data;
const genAiActions: GenerativeInfo[] = actions.reduce<GenerativeInfo[]>(
(actionAcc, action: ActionV2) => {
const { digitalSourceType, softwareAgent } = action;
if (
digitalSourceType &&
genAiDigitalSourceTypes.includes(digitalSourceType)
) {
actionAcc.push({
assertion,
action: action,
type: formatGenAiDigitalSourceTypes(digitalSourceType),
softwareAgent,
} as GenerativeInfo);
}
return actionAcc;
},
[],
);
return [...acc, ...genAiActions];
}
return acc;
},
[],
);
return data.length ? data : null;
}
/**
* Returns a set of software agents
* @param generativeInfo - generative info from manifest
*/
export function selectGenerativeSoftwareAgents(
generativeInfo: GenerativeInfo[],
): string[] {
const softwareAgents = generativeInfo?.length
? [
...new Set(
generativeInfo
.map((assertion) =>
typeof assertion?.softwareAgent?.name === 'string'
? assertion?.softwareAgent?.name
: typeof assertion?.softwareAgent === 'string'
? assertion?.softwareAgent
: undefined,
)
.filter((element) => typeof element !== 'undefined'),
),
]
: [];
//if there are undefined software agents remove them from the array
return softwareAgents;
}
/**
* Returns the generative type (trained , legacy or composite)
* @param generativeInfo - generative info from manifest
*/
export function selectGenerativeType(generativeInfo: GenerativeInfo[]) {
const result =
// Try to see if we have any composite assertions
generativeInfo.find(
(assertion) => assertion.type === 'compositeWithTrainedAlgorithmicMedia',
// If not, fall back to whichever one the first item is, which should be the trained or legacy assertion
) ?? generativeInfo[0];
return result?.type ?? null;
}