mirror of
https://github.com/CJackHwang/ds2api.git
synced 2026-05-07 09:55:29 +08:00
44 lines
974 B
JavaScript
44 lines
974 B
JavaScript
'use strict';
|
|
|
|
const crypto = require('crypto');
|
|
|
|
function formatOpenAIStreamToolCalls(calls, idStore) {
|
|
if (!Array.isArray(calls) || calls.length === 0) {
|
|
return [];
|
|
}
|
|
return calls.map((c, idx) => ({
|
|
index: idx,
|
|
id: ensureStreamToolCallID(idStore, idx),
|
|
type: 'function',
|
|
function: {
|
|
name: c.name,
|
|
arguments: JSON.stringify(c.input || {}),
|
|
},
|
|
}));
|
|
}
|
|
|
|
function ensureStreamToolCallID(idStore, index) {
|
|
if (!(idStore instanceof Map)) {
|
|
return `call_${newCallID()}`;
|
|
}
|
|
const key = Number.isInteger(index) ? index : 0;
|
|
const existing = idStore.get(key);
|
|
if (existing) {
|
|
return existing;
|
|
}
|
|
const next = `call_${newCallID()}`;
|
|
idStore.set(key, next);
|
|
return next;
|
|
}
|
|
|
|
function newCallID() {
|
|
if (typeof crypto.randomUUID === 'function') {
|
|
return crypto.randomUUID().replace(/-/g, '');
|
|
}
|
|
return `${Date.now()}${Math.floor(Math.random() * 1e9)}`;
|
|
}
|
|
|
|
module.exports = {
|
|
formatOpenAIStreamToolCalls,
|
|
};
|