@codehz/ai 0.4.1 → 0.4.3
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.
- package/dist/index.d.mts +1 -1
- package/dist/index.mjs +132 -6
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
- package/src/adapters/responses.ts +210 -9
package/dist/index.d.mts
CHANGED
|
@@ -606,7 +606,7 @@ type ResponsesTool = {
|
|
|
606
606
|
type: "function";
|
|
607
607
|
name: string;
|
|
608
608
|
description?: string;
|
|
609
|
-
|
|
609
|
+
parameters: Record<string, unknown>;
|
|
610
610
|
};
|
|
611
611
|
declare class ResponsesAdapter extends AdapterBase {
|
|
612
612
|
readonly kind: "responses";
|
package/dist/index.mjs
CHANGED
|
@@ -1732,6 +1732,12 @@ const KNOWN_RESPONSES_SSE_TYPES = /* @__PURE__ */ new Set([
|
|
|
1732
1732
|
"response.output_text.done",
|
|
1733
1733
|
"response.reasoning.delta",
|
|
1734
1734
|
"response.reasoning.done",
|
|
1735
|
+
"response.reasoning_summary_part.added",
|
|
1736
|
+
"response.reasoning_summary_part.done",
|
|
1737
|
+
"response.reasoning_summary_text.delta",
|
|
1738
|
+
"response.reasoning_summary_text.done",
|
|
1739
|
+
"response.reasoning_text.delta",
|
|
1740
|
+
"response.reasoning_text.done",
|
|
1735
1741
|
"response.function_call_arguments.delta",
|
|
1736
1742
|
"response.function_call_arguments.done",
|
|
1737
1743
|
"response.content_part.added",
|
|
@@ -1740,11 +1746,46 @@ const KNOWN_RESPONSES_SSE_TYPES = /* @__PURE__ */ new Set([
|
|
|
1740
1746
|
"response.refusal.done",
|
|
1741
1747
|
"response.in_progress",
|
|
1742
1748
|
"response.created",
|
|
1749
|
+
"response.queued",
|
|
1743
1750
|
"response.completed",
|
|
1744
1751
|
"response.failed",
|
|
1745
1752
|
"response.incomplete",
|
|
1746
1753
|
"error"
|
|
1747
1754
|
]);
|
|
1755
|
+
function createReasoningState(visibility = "summary") {
|
|
1756
|
+
return {
|
|
1757
|
+
visibility,
|
|
1758
|
+
hasDelta: false,
|
|
1759
|
+
doneTexts: [],
|
|
1760
|
+
completed: false
|
|
1761
|
+
};
|
|
1762
|
+
}
|
|
1763
|
+
function extractReasoningFromOutputItem(item) {
|
|
1764
|
+
const contentTexts = [];
|
|
1765
|
+
if (Array.isArray(item.content)) {
|
|
1766
|
+
for (const part of item.content) if (part && typeof part === "object" && part.type === "reasoning_text" && typeof part.text === "string") contentTexts.push(part.text);
|
|
1767
|
+
}
|
|
1768
|
+
const summaryTexts = [];
|
|
1769
|
+
if (Array.isArray(item.summary)) {
|
|
1770
|
+
for (const part of item.summary) if (part && typeof part === "object" && part.type === "summary_text" && typeof part.text === "string") summaryTexts.push(part.text);
|
|
1771
|
+
}
|
|
1772
|
+
if (contentTexts.length > 0) return {
|
|
1773
|
+
text: contentTexts.join("\n"),
|
|
1774
|
+
visibility: "full"
|
|
1775
|
+
};
|
|
1776
|
+
if (summaryTexts.length > 0) return {
|
|
1777
|
+
text: summaryTexts.join("\n"),
|
|
1778
|
+
visibility: "summary"
|
|
1779
|
+
};
|
|
1780
|
+
if (typeof item.encrypted_content === "string" && item.encrypted_content.length > 0) return {
|
|
1781
|
+
text: "",
|
|
1782
|
+
visibility: "opaque"
|
|
1783
|
+
};
|
|
1784
|
+
return {
|
|
1785
|
+
text: "",
|
|
1786
|
+
visibility: "summary"
|
|
1787
|
+
};
|
|
1788
|
+
}
|
|
1748
1789
|
function isReplayCanonicalInput(item) {
|
|
1749
1790
|
return item.type === "message" && item.role === "assistant" || item.type === "reasoning" || item.type === "function_call";
|
|
1750
1791
|
}
|
|
@@ -1846,7 +1887,7 @@ var ResponsesAdapter = class extends AdapterBase {
|
|
|
1846
1887
|
type: "function",
|
|
1847
1888
|
name: t.name,
|
|
1848
1889
|
description: t.description,
|
|
1849
|
-
|
|
1890
|
+
parameters: t.inputSchema
|
|
1850
1891
|
}));
|
|
1851
1892
|
body.tool_choice = mapper$3.mapToolChoice(request.toolChoice, {
|
|
1852
1893
|
auto: "auto",
|
|
@@ -1880,6 +1921,23 @@ var ResponsesAdapter = class extends AdapterBase {
|
|
|
1880
1921
|
let unknownEventsWarned = false;
|
|
1881
1922
|
const messageItemsWithDelta = /* @__PURE__ */ new Set();
|
|
1882
1923
|
const toolCallNames = /* @__PURE__ */ new Map();
|
|
1924
|
+
const reasoningStates = /* @__PURE__ */ new Map();
|
|
1925
|
+
const ensureReasoningState = (itemId, visibility = "summary") => {
|
|
1926
|
+
let state = reasoningStates.get(itemId);
|
|
1927
|
+
if (!state) {
|
|
1928
|
+
state = createReasoningState(visibility);
|
|
1929
|
+
reasoningStates.set(itemId, state);
|
|
1930
|
+
}
|
|
1931
|
+
return state;
|
|
1932
|
+
};
|
|
1933
|
+
const completeReasoning = function* (itemId, text, visibility) {
|
|
1934
|
+
const state = ensureReasoningState(itemId, visibility);
|
|
1935
|
+
if (state.completed) return;
|
|
1936
|
+
state.completed = true;
|
|
1937
|
+
state.visibility = visibility;
|
|
1938
|
+
yield factory.reasoningCompleted(itemId);
|
|
1939
|
+
output.push(reasoningItem(text ? [textBlock(text)] : [], visibility, itemId));
|
|
1940
|
+
};
|
|
1883
1941
|
for await (const batch of iterateProviderStreamBatches({
|
|
1884
1942
|
reader,
|
|
1885
1943
|
parser,
|
|
@@ -1901,9 +1959,12 @@ var ResponsesAdapter = class extends AdapterBase {
|
|
|
1901
1959
|
case "message":
|
|
1902
1960
|
yield factory.messageStarted(item.id);
|
|
1903
1961
|
break;
|
|
1904
|
-
case "reasoning":
|
|
1905
|
-
|
|
1962
|
+
case "reasoning": {
|
|
1963
|
+
const visibility = extractReasoningFromOutputItem(item).visibility;
|
|
1964
|
+
ensureReasoningState(item.id, visibility);
|
|
1965
|
+
yield factory.reasoningStarted(item.id, visibility);
|
|
1906
1966
|
break;
|
|
1967
|
+
}
|
|
1907
1968
|
case "function_call": {
|
|
1908
1969
|
const name = typeof item.name === "string" ? item.name : "unknown";
|
|
1909
1970
|
toolCallNames.set(item.id, name);
|
|
@@ -1913,6 +1974,16 @@ var ResponsesAdapter = class extends AdapterBase {
|
|
|
1913
1974
|
}
|
|
1914
1975
|
continue;
|
|
1915
1976
|
}
|
|
1977
|
+
if (sseEvent.type === "response.output_item.done") {
|
|
1978
|
+
const item = sseEvent.data.item;
|
|
1979
|
+
if (item.type === "reasoning") {
|
|
1980
|
+
const extracted = extractReasoningFromOutputItem(item);
|
|
1981
|
+
const state = ensureReasoningState(item.id, extracted.visibility);
|
|
1982
|
+
const text = extracted.text || (state.doneTexts.length > 0 ? state.doneTexts.join("\n") : "");
|
|
1983
|
+
yield* completeReasoning(item.id, text, extracted.visibility || state.visibility);
|
|
1984
|
+
}
|
|
1985
|
+
continue;
|
|
1986
|
+
}
|
|
1916
1987
|
if (sseEvent.type === "response.output_text.delta") {
|
|
1917
1988
|
const data = sseEvent.data;
|
|
1918
1989
|
yield factory.messageDelta(data.item_id, textBlock(data.delta));
|
|
@@ -1926,15 +1997,62 @@ var ResponsesAdapter = class extends AdapterBase {
|
|
|
1926
1997
|
output.push(messageItem([textBlock(data.text)], { id: data.item_id }));
|
|
1927
1998
|
continue;
|
|
1928
1999
|
}
|
|
2000
|
+
if (sseEvent.type === "response.reasoning_summary_text.delta") {
|
|
2001
|
+
const data = sseEvent.data;
|
|
2002
|
+
const state = ensureReasoningState(data.item_id, "summary");
|
|
2003
|
+
if (state.visibility === "opaque") state.visibility = "summary";
|
|
2004
|
+
if (data.delta) {
|
|
2005
|
+
state.hasDelta = true;
|
|
2006
|
+
yield factory.reasoningDelta(data.item_id, textBlock(data.delta));
|
|
2007
|
+
}
|
|
2008
|
+
continue;
|
|
2009
|
+
}
|
|
2010
|
+
if (sseEvent.type === "response.reasoning_summary_text.done") {
|
|
2011
|
+
const data = sseEvent.data;
|
|
2012
|
+
const state = ensureReasoningState(data.item_id, "summary");
|
|
2013
|
+
if (state.visibility === "opaque") state.visibility = "summary";
|
|
2014
|
+
if (!state.hasDelta && data.text) {
|
|
2015
|
+
state.doneTexts.push(data.text);
|
|
2016
|
+
yield factory.reasoningDelta(data.item_id, textBlock(data.text));
|
|
2017
|
+
} else if (data.text) state.doneTexts.push(data.text);
|
|
2018
|
+
continue;
|
|
2019
|
+
}
|
|
2020
|
+
if (sseEvent.type === "response.reasoning_text.delta") {
|
|
2021
|
+
const data = sseEvent.data;
|
|
2022
|
+
const state = ensureReasoningState(data.item_id, "full");
|
|
2023
|
+
state.visibility = "full";
|
|
2024
|
+
if (data.delta) {
|
|
2025
|
+
state.hasDelta = true;
|
|
2026
|
+
yield factory.reasoningDelta(data.item_id, textBlock(data.delta));
|
|
2027
|
+
}
|
|
2028
|
+
continue;
|
|
2029
|
+
}
|
|
2030
|
+
if (sseEvent.type === "response.reasoning_text.done") {
|
|
2031
|
+
const data = sseEvent.data;
|
|
2032
|
+
const state = ensureReasoningState(data.item_id, "full");
|
|
2033
|
+
state.visibility = "full";
|
|
2034
|
+
if (!state.hasDelta && data.text) {
|
|
2035
|
+
state.doneTexts.push(data.text);
|
|
2036
|
+
yield factory.reasoningDelta(data.item_id, textBlock(data.text));
|
|
2037
|
+
} else if (data.text) state.doneTexts.push(data.text);
|
|
2038
|
+
continue;
|
|
2039
|
+
}
|
|
2040
|
+
if (sseEvent.type === "response.reasoning_summary_part.added" || sseEvent.type === "response.reasoning_summary_part.done") continue;
|
|
1929
2041
|
if (sseEvent.type === "response.reasoning.delta") {
|
|
1930
2042
|
const data = sseEvent.data;
|
|
1931
|
-
|
|
2043
|
+
const state = ensureReasoningState(data.item_id, "full");
|
|
2044
|
+
state.visibility = "full";
|
|
2045
|
+
if (data.delta) {
|
|
2046
|
+
state.hasDelta = true;
|
|
2047
|
+
yield factory.reasoningDelta(data.item_id, textBlock(data.delta));
|
|
2048
|
+
}
|
|
1932
2049
|
continue;
|
|
1933
2050
|
}
|
|
1934
2051
|
if (sseEvent.type === "response.reasoning.done") {
|
|
1935
2052
|
const data = sseEvent.data;
|
|
1936
|
-
|
|
1937
|
-
|
|
2053
|
+
const state = ensureReasoningState(data.item_id, "full");
|
|
2054
|
+
if (!state.hasDelta && data.text) yield factory.reasoningDelta(data.item_id, textBlock(data.text));
|
|
2055
|
+
yield* completeReasoning(data.item_id, data.text ?? state.doneTexts.join("\n"), "full");
|
|
1938
2056
|
continue;
|
|
1939
2057
|
}
|
|
1940
2058
|
if (sseEvent.type === "response.function_call_arguments.delta") {
|
|
@@ -1956,6 +2074,14 @@ var ResponsesAdapter = class extends AdapterBase {
|
|
|
1956
2074
|
continue;
|
|
1957
2075
|
}
|
|
1958
2076
|
completedResponse = data.response;
|
|
2077
|
+
if (Array.isArray(data.response.output)) for (const item of data.response.output) {
|
|
2078
|
+
if (item?.type !== "reasoning" || !item.id) continue;
|
|
2079
|
+
const state = reasoningStates.get(item.id);
|
|
2080
|
+
if (state?.completed) continue;
|
|
2081
|
+
const extracted = extractReasoningFromOutputItem(item);
|
|
2082
|
+
const text = extracted.text || (state && state.doneTexts.length > 0 ? state.doneTexts.join("\n") : "");
|
|
2083
|
+
if (state || reasoningStates.has(item.id)) yield* completeReasoning(item.id, text, extracted.visibility);
|
|
2084
|
+
}
|
|
1959
2085
|
if (sseEvent.type === "response.failed") yield factory.responseWarning(`Response failed: ${extractFailureMessage(data.response)}`, "PROVIDER_FAILURE");
|
|
1960
2086
|
continue;
|
|
1961
2087
|
}
|