gaoding-cli 1.0.0-alpha.17 → 1.0.0-alpha.18
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.
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { buildCreativeRequest, creativeContextId, creativeTurnMessages, normalizeCreativeMessages } from "./creative-protocol.js";
|
|
1
|
+
import { buildCreativeRequest, creativeContextId, creativeTurnMessages, normalizeCreativeMessages, restoreCreativeResourceUrls } from "./creative-protocol.js";
|
|
2
2
|
import { parseCreativeStream } from "./creative-stream.js";
|
|
3
3
|
export function createCreativeAgentAdapter(dependencies) {
|
|
4
4
|
return {
|
|
@@ -29,7 +29,7 @@ export function createCreativeAgentAdapter(dependencies) {
|
|
|
29
29
|
organizationId: access.organizationId,
|
|
30
30
|
signal
|
|
31
31
|
});
|
|
32
|
-
return normalizeCreativeMessages(creativeTurnMessages(messages, ids.localMessageId));
|
|
32
|
+
return normalizeCreativeMessages(restoreCreativeResourceUrls(creativeTurnMessages(messages, ids.localMessageId), streamed));
|
|
33
33
|
});
|
|
34
34
|
}
|
|
35
35
|
};
|
|
@@ -190,6 +190,115 @@ function functionResponseParts(raw) {
|
|
|
190
190
|
}
|
|
191
191
|
return parts;
|
|
192
192
|
}
|
|
193
|
+
function hasAuthKey(url) {
|
|
194
|
+
try {
|
|
195
|
+
const value = new URL(url).searchParams.get("auth_key");
|
|
196
|
+
return value !== null && value.trim() !== "";
|
|
197
|
+
}
|
|
198
|
+
catch {
|
|
199
|
+
return false;
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
function resourceUrlIdentity(url) {
|
|
203
|
+
try {
|
|
204
|
+
const parsed = new URL(url);
|
|
205
|
+
parsed.searchParams.delete("auth_key");
|
|
206
|
+
return parsed.toString();
|
|
207
|
+
}
|
|
208
|
+
catch {
|
|
209
|
+
return undefined;
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
function requiresAuthKey(url) {
|
|
213
|
+
try {
|
|
214
|
+
return /^gd-ai-[a-z0-9-]+\.dancf\.com$/iu.test(new URL(url).hostname);
|
|
215
|
+
}
|
|
216
|
+
catch {
|
|
217
|
+
return false;
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
export function restoreCreativeResourceUrls(messages, streamed) {
|
|
221
|
+
const signedUrls = new Map();
|
|
222
|
+
for (const message of streamed) {
|
|
223
|
+
const messageId = nonEmptyString(message.message_id);
|
|
224
|
+
if (messageId === undefined)
|
|
225
|
+
continue;
|
|
226
|
+
const content = contentOf(message);
|
|
227
|
+
if (content?.type !== "function_response")
|
|
228
|
+
continue;
|
|
229
|
+
for (const part of functionResponseParts(content.text)) {
|
|
230
|
+
if (!("url" in part) || !hasAuthKey(part.url))
|
|
231
|
+
continue;
|
|
232
|
+
const identity = resourceUrlIdentity(part.url);
|
|
233
|
+
if (identity === undefined)
|
|
234
|
+
continue;
|
|
235
|
+
let messageUrls = signedUrls.get(messageId);
|
|
236
|
+
if (messageUrls === undefined) {
|
|
237
|
+
messageUrls = new Map();
|
|
238
|
+
signedUrls.set(messageId, messageUrls);
|
|
239
|
+
}
|
|
240
|
+
messageUrls.set(identity, part.url);
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
const restored = messages.map((message) => {
|
|
244
|
+
const content = contentOf(message);
|
|
245
|
+
const messageId = nonEmptyString(message.message_id);
|
|
246
|
+
if (content?.type !== "function_response" || messageId === undefined)
|
|
247
|
+
return message;
|
|
248
|
+
const messageUrls = signedUrls.get(messageId);
|
|
249
|
+
if (messageUrls === undefined)
|
|
250
|
+
return message;
|
|
251
|
+
const parsed = parseJson(content.text);
|
|
252
|
+
const values = Array.isArray(parsed) ? parsed : parsed === undefined ? [] : [parsed];
|
|
253
|
+
let changed = false;
|
|
254
|
+
const nextValues = values.map((value) => {
|
|
255
|
+
const item = record(value);
|
|
256
|
+
if (item === undefined)
|
|
257
|
+
return value;
|
|
258
|
+
const uri = nonEmptyString(item.uri);
|
|
259
|
+
const fallbackUrl = nonEmptyString(item.url);
|
|
260
|
+
let field;
|
|
261
|
+
let url;
|
|
262
|
+
if (uri !== undefined) {
|
|
263
|
+
field = "uri";
|
|
264
|
+
url = uri;
|
|
265
|
+
}
|
|
266
|
+
else if (fallbackUrl !== undefined) {
|
|
267
|
+
field = "url";
|
|
268
|
+
url = fallbackUrl;
|
|
269
|
+
}
|
|
270
|
+
else {
|
|
271
|
+
return value;
|
|
272
|
+
}
|
|
273
|
+
if (hasAuthKey(url))
|
|
274
|
+
return value;
|
|
275
|
+
const identity = resourceUrlIdentity(url);
|
|
276
|
+
const signedUrl = identity === undefined ? undefined : messageUrls.get(identity);
|
|
277
|
+
if (signedUrl === undefined)
|
|
278
|
+
return value;
|
|
279
|
+
changed = true;
|
|
280
|
+
return { ...item, [field]: signedUrl };
|
|
281
|
+
});
|
|
282
|
+
if (!changed)
|
|
283
|
+
return message;
|
|
284
|
+
const nextValue = Array.isArray(parsed) ? nextValues : nextValues[0];
|
|
285
|
+
return {
|
|
286
|
+
...message,
|
|
287
|
+
content: {
|
|
288
|
+
...content,
|
|
289
|
+
text: typeof content.text === "string" ? JSON.stringify(nextValue) : nextValue
|
|
290
|
+
}
|
|
291
|
+
};
|
|
292
|
+
});
|
|
293
|
+
for (const message of restored) {
|
|
294
|
+
const content = contentOf(message);
|
|
295
|
+
if (content?.type !== "function_response")
|
|
296
|
+
continue;
|
|
297
|
+
if (functionResponseParts(content.text).some((part) => ("url" in part && requiresAuthKey(part.url) && !hasAuthKey(part.url))))
|
|
298
|
+
return fail();
|
|
299
|
+
}
|
|
300
|
+
return restored;
|
|
301
|
+
}
|
|
193
302
|
function parseQuestionPart(message) {
|
|
194
303
|
const payload = functionPayload(message);
|
|
195
304
|
if (payload?.name !== "ask_user_question")
|