@openrouter/sdk 0.3.1 → 0.3.7
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/esm/core.js +1 -0
- package/esm/funcs/analyticsGetUserActivity.js +1 -0
- package/esm/funcs/apiKeysCreate.js +1 -0
- package/esm/funcs/apiKeysDelete.js +1 -0
- package/esm/funcs/apiKeysGet.js +1 -0
- package/esm/funcs/apiKeysGetCurrentKeyMetadata.js +1 -0
- package/esm/funcs/apiKeysList.js +1 -0
- package/esm/funcs/apiKeysUpdate.js +1 -0
- package/esm/funcs/betaResponsesSend.js +1 -0
- package/esm/funcs/call-model.d.ts +33 -0
- package/esm/funcs/call-model.js +155 -0
- package/esm/funcs/chatSend.js +1 -0
- package/esm/funcs/completionsGenerate.js +1 -0
- package/esm/funcs/creditsCreateCoinbaseCharge.js +1 -0
- package/esm/funcs/creditsGetCredits.js +1 -0
- package/esm/funcs/embeddingsGenerate.js +1 -0
- package/esm/funcs/embeddingsListModels.js +1 -0
- package/esm/funcs/endpointsList.js +1 -0
- package/esm/funcs/endpointsListZdrEndpoints.js +1 -0
- package/esm/funcs/generationsGetGeneration.js +1 -0
- package/esm/funcs/modelsCount.js +1 -0
- package/esm/funcs/modelsList.js +1 -0
- package/esm/funcs/modelsListForUser.js +1 -0
- package/esm/funcs/oAuthCreateAuthCode.js +1 -0
- package/esm/funcs/oAuthExchangeAuthCodeForAPIKey.js +1 -0
- package/esm/funcs/parametersGetParameters.js +1 -0
- package/esm/funcs/providersList.js +1 -0
- package/esm/hooks/hooks.js +1 -0
- package/esm/hooks/index.js +1 -0
- package/esm/hooks/types.js +1 -0
- package/esm/index.d.ts +3 -0
- package/esm/index.js +4 -0
- package/esm/lib/anthropic-compat.d.ts +46 -0
- package/esm/lib/anthropic-compat.js +204 -0
- package/esm/lib/anthropic-compat.test.d.ts +2 -0
- package/esm/lib/anthropic-compat.test.js +479 -0
- package/esm/lib/base64.js +1 -0
- package/esm/lib/chat-compat.d.ts +46 -0
- package/esm/lib/chat-compat.js +108 -0
- package/esm/lib/chat-compat.test.d.ts +2 -0
- package/esm/lib/chat-compat.test.js +282 -0
- package/esm/lib/config.d.ts +3 -3
- package/esm/lib/config.js +4 -3
- package/esm/lib/dlv.js +1 -0
- package/esm/lib/encodings.js +1 -0
- package/esm/lib/env.js +1 -0
- package/esm/lib/event-streams.js +2 -1
- package/esm/lib/files.js +1 -0
- package/esm/lib/http.js +1 -0
- package/esm/lib/is-plain-object.js +1 -0
- package/esm/lib/logger.js +1 -0
- package/esm/lib/matchers.js +1 -0
- package/esm/lib/{response-wrapper.d.ts → model-result.d.ts} +19 -26
- package/esm/lib/{response-wrapper.js → model-result.js} +93 -83
- package/esm/lib/primitives.js +1 -0
- package/esm/lib/retries.js +1 -0
- package/esm/lib/schemas.js +1 -0
- package/esm/lib/sdks.js +1 -0
- package/esm/lib/security.js +1 -0
- package/esm/lib/stream-transformers.d.ts +27 -1
- package/esm/lib/stream-transformers.js +319 -1
- package/esm/lib/tool-types.d.ts +3 -3
- package/esm/lib/url.js +1 -0
- package/esm/models/activityitem.js +1 -0
- package/esm/models/assistantmessage.js +1 -0
- package/esm/models/badgatewayresponseerrordata.js +1 -0
- package/esm/models/badrequestresponseerrordata.js +1 -0
- package/esm/models/chatcompletionfinishreason.js +1 -0
- package/esm/models/chaterror.js +1 -0
- package/esm/models/chatgenerationparams.js +1 -0
- package/esm/models/chatgenerationtokenusage.js +1 -0
- package/esm/models/chatmessagecontentitem.js +1 -0
- package/esm/models/chatmessagecontentitemaudio.js +1 -0
- package/esm/models/chatmessagecontentitemcachecontrol.js +1 -0
- package/esm/models/chatmessagecontentitemimage.js +1 -0
- package/esm/models/chatmessagecontentitemtext.js +1 -0
- package/esm/models/chatmessagecontentitemvideo.js +1 -0
- package/esm/models/chatmessagetokenlogprob.js +1 -0
- package/esm/models/chatmessagetokenlogprobs.js +1 -0
- package/esm/models/chatmessagetoolcall.js +1 -0
- package/esm/models/chatresponse.js +1 -0
- package/esm/models/chatresponsechoice.js +1 -0
- package/esm/models/chatstreamingchoice.js +1 -0
- package/esm/models/chatstreamingmessagechunk.js +1 -0
- package/esm/models/chatstreamingmessagetoolcall.js +1 -0
- package/esm/models/chatstreamingresponsechunk.js +1 -0
- package/esm/models/chatstreamoptions.js +1 -0
- package/esm/models/claude-message.d.ts +218 -0
- package/esm/models/claude-message.js +6 -0
- package/esm/models/completionchoice.js +1 -0
- package/esm/models/completioncreateparams.js +1 -0
- package/esm/models/completionlogprobs.js +1 -0
- package/esm/models/completionresponse.js +1 -0
- package/esm/models/completionusage.js +1 -0
- package/esm/models/createchargerequest.js +1 -0
- package/esm/models/datacollection.js +1 -0
- package/esm/models/defaultparameters.js +1 -0
- package/esm/models/edgenetworktimeoutresponseerrordata.js +1 -0
- package/esm/models/endpointstatus.js +1 -0
- package/esm/models/errors/badgatewayresponseerror.js +1 -0
- package/esm/models/errors/badrequestresponseerror.js +1 -0
- package/esm/models/errors/chaterror.js +1 -0
- package/esm/models/errors/edgenetworktimeoutresponseerror.js +1 -0
- package/esm/models/errors/forbiddenresponseerror.js +1 -0
- package/esm/models/errors/httpclienterrors.js +1 -0
- package/esm/models/errors/index.js +1 -0
- package/esm/models/errors/internalserverresponseerror.js +1 -0
- package/esm/models/errors/notfoundresponseerror.js +1 -0
- package/esm/models/errors/openrouterdefaulterror.js +1 -0
- package/esm/models/errors/openroutererror.js +1 -0
- package/esm/models/errors/payloadtoolargeresponseerror.js +1 -0
- package/esm/models/errors/paymentrequiredresponseerror.js +1 -0
- package/esm/models/errors/provideroverloadedresponseerror.js +1 -0
- package/esm/models/errors/requesttimeoutresponseerror.js +1 -0
- package/esm/models/errors/responsevalidationerror.js +1 -0
- package/esm/models/errors/sdkvalidationerror.js +1 -0
- package/esm/models/errors/serviceunavailableresponseerror.js +1 -0
- package/esm/models/errors/toomanyrequestsresponseerror.js +1 -0
- package/esm/models/errors/unauthorizedresponseerror.js +1 -0
- package/esm/models/errors/unprocessableentityresponseerror.js +1 -0
- package/esm/models/filecitation.js +1 -0
- package/esm/models/filepath.js +1 -0
- package/esm/models/forbiddenresponseerrordata.js +1 -0
- package/esm/models/imagegenerationstatus.js +1 -0
- package/esm/models/index.d.ts +1 -0
- package/esm/models/index.js +2 -0
- package/esm/models/inputmodality.js +1 -0
- package/esm/models/instructtype.js +1 -0
- package/esm/models/internalserverresponseerrordata.js +1 -0
- package/esm/models/jsonschemaconfig.js +1 -0
- package/esm/models/listendpointsresponse.js +1 -0
- package/esm/models/message.js +1 -0
- package/esm/models/model.js +1 -0
- package/esm/models/modelarchitecture.js +1 -0
- package/esm/models/modelgroup.js +1 -0
- package/esm/models/modelscountresponse.js +1 -0
- package/esm/models/modelslistresponse.js +1 -0
- package/esm/models/namedtoolchoice.js +1 -0
- package/esm/models/notfoundresponseerrordata.js +1 -0
- package/esm/models/openairesponsesannotation.js +1 -0
- package/esm/models/openairesponsesincludable.js +1 -0
- package/esm/models/openairesponsesincompletedetails.js +1 -0
- package/esm/models/openairesponsesinputunion.js +1 -0
- package/esm/models/openairesponsesprompt.js +1 -0
- package/esm/models/openairesponsesreasoningconfig.js +1 -0
- package/esm/models/openairesponsesreasoningeffort.js +1 -0
- package/esm/models/openairesponsesrefusalcontent.js +1 -0
- package/esm/models/openairesponsesresponsestatus.js +1 -0
- package/esm/models/openairesponsesservicetier.js +1 -0
- package/esm/models/openairesponsestoolchoiceunion.js +1 -0
- package/esm/models/openairesponsestruncation.js +1 -0
- package/esm/models/openresponseseasyinputmessage.js +1 -0
- package/esm/models/openresponseserrorevent.js +1 -0
- package/esm/models/openresponsesfunctioncalloutput.js +1 -0
- package/esm/models/openresponsesfunctiontoolcall.js +1 -0
- package/esm/models/openresponsesimagegencallcompleted.js +1 -0
- package/esm/models/openresponsesimagegencallgenerating.js +1 -0
- package/esm/models/openresponsesimagegencallinprogress.js +1 -0
- package/esm/models/openresponsesimagegencallpartialimage.js +1 -0
- package/esm/models/openresponsesinput.js +1 -0
- package/esm/models/openresponsesinputmessageitem.js +1 -0
- package/esm/models/openresponseslogprobs.js +1 -0
- package/esm/models/openresponsesnonstreamingresponse.js +1 -0
- package/esm/models/openresponsesreasoning.js +1 -0
- package/esm/models/openresponsesreasoningconfig.js +1 -0
- package/esm/models/openresponsesreasoningdeltaevent.js +1 -0
- package/esm/models/openresponsesreasoningdoneevent.js +1 -0
- package/esm/models/openresponsesreasoningsummarypartaddedevent.js +1 -0
- package/esm/models/openresponsesreasoningsummarytextdeltaevent.js +1 -0
- package/esm/models/openresponsesreasoningsummarytextdoneevent.js +1 -0
- package/esm/models/openresponsesrequest.js +1 -0
- package/esm/models/openresponsesresponsetext.js +1 -0
- package/esm/models/openresponsesstreamevent.js +1 -0
- package/esm/models/openresponsestoplogprobs.js +1 -0
- package/esm/models/openresponsesusage.js +1 -0
- package/esm/models/openresponseswebsearch20250826tool.js +1 -0
- package/esm/models/openresponseswebsearchpreview20250311tool.js +1 -0
- package/esm/models/openresponseswebsearchpreviewtool.js +1 -0
- package/esm/models/openresponseswebsearchtool.js +1 -0
- package/esm/models/operations/createauthkeyscode.js +1 -0
- package/esm/models/operations/createcoinbasecharge.js +1 -0
- package/esm/models/operations/createembeddings.js +1 -0
- package/esm/models/operations/createkeys.js +1 -0
- package/esm/models/operations/createresponses.js +1 -0
- package/esm/models/operations/deletekeys.js +1 -0
- package/esm/models/operations/exchangeauthcodeforapikey.js +1 -0
- package/esm/models/operations/getcredits.js +1 -0
- package/esm/models/operations/getcurrentkey.js +1 -0
- package/esm/models/operations/getgeneration.js +1 -0
- package/esm/models/operations/getkey.js +1 -0
- package/esm/models/operations/getmodels.js +1 -0
- package/esm/models/operations/getparameters.js +1 -0
- package/esm/models/operations/getuseractivity.js +1 -0
- package/esm/models/operations/index.js +1 -0
- package/esm/models/operations/list.js +1 -0
- package/esm/models/operations/listendpoints.js +1 -0
- package/esm/models/operations/listendpointszdr.js +1 -0
- package/esm/models/operations/listmodelsuser.js +1 -0
- package/esm/models/operations/listproviders.js +1 -0
- package/esm/models/operations/sendchatcompletionrequest.js +1 -0
- package/esm/models/operations/updatekeys.js +1 -0
- package/esm/models/outputitemimagegenerationcall.d.ts +1 -1
- package/esm/models/outputitemimagegenerationcall.js +1 -0
- package/esm/models/outputmessage.js +1 -0
- package/esm/models/outputmodality.js +1 -0
- package/esm/models/parameter.js +1 -0
- package/esm/models/payloadtoolargeresponseerrordata.js +1 -0
- package/esm/models/paymentrequiredresponseerrordata.js +1 -0
- package/esm/models/pdfparserengine.js +1 -0
- package/esm/models/pdfparseroptions.js +1 -0
- package/esm/models/perrequestlimits.js +1 -0
- package/esm/models/providername.js +1 -0
- package/esm/models/provideroverloadedresponseerrordata.js +1 -0
- package/esm/models/providerpreferences.js +1 -0
- package/esm/models/providersort.js +1 -0
- package/esm/models/providersortconfig.js +1 -0
- package/esm/models/providersortunion.js +1 -0
- package/esm/models/publicendpoint.js +1 -0
- package/esm/models/publicpricing.js +1 -0
- package/esm/models/quantization.js +1 -0
- package/esm/models/reasoningsummarytext.js +1 -0
- package/esm/models/reasoningsummaryverbosity.js +1 -0
- package/esm/models/reasoningtextcontent.js +1 -0
- package/esm/models/requesttimeoutresponseerrordata.js +1 -0
- package/esm/models/responseformatjsonschema.js +1 -0
- package/esm/models/responseformattextconfig.js +1 -0
- package/esm/models/responseformattextgrammar.js +1 -0
- package/esm/models/responseinputaudio.js +1 -0
- package/esm/models/responseinputfile.js +1 -0
- package/esm/models/responseinputimage.js +1 -0
- package/esm/models/responseinputtext.js +1 -0
- package/esm/models/responseoutputtext.js +1 -0
- package/esm/models/responseserrorfield.js +1 -0
- package/esm/models/responsesformatjsonobject.js +1 -0
- package/esm/models/responsesformattext.js +1 -0
- package/esm/models/responsesformattextjsonschemaconfig.js +1 -0
- package/esm/models/responsesimagegenerationcall.js +1 -0
- package/esm/models/responsesoutputitem.js +1 -0
- package/esm/models/responsesoutputitemfilesearchcall.js +1 -0
- package/esm/models/responsesoutputitemfunctioncall.js +1 -0
- package/esm/models/responsesoutputitemreasoning.js +1 -0
- package/esm/models/responsesoutputmessage.js +1 -0
- package/esm/models/responsessearchcontextsize.js +1 -0
- package/esm/models/responseswebsearchcalloutput.js +1 -0
- package/esm/models/responseswebsearchuserlocation.js +1 -0
- package/esm/models/responsetextconfig.js +1 -0
- package/esm/models/schema0.js +1 -0
- package/esm/models/schema3.js +1 -0
- package/esm/models/security.js +1 -0
- package/esm/models/serviceunavailableresponseerrordata.js +1 -0
- package/esm/models/systemmessage.js +1 -0
- package/esm/models/toolcallstatus.js +1 -0
- package/esm/models/tooldefinitionjson.js +1 -0
- package/esm/models/toolresponsemessage.js +1 -0
- package/esm/models/toomanyrequestsresponseerrordata.js +1 -0
- package/esm/models/topproviderinfo.js +1 -0
- package/esm/models/unauthorizedresponseerrordata.js +1 -0
- package/esm/models/unprocessableentityresponseerrordata.js +1 -0
- package/esm/models/urlcitation.js +1 -0
- package/esm/models/usermessage.js +1 -0
- package/esm/models/websearchengine.js +1 -0
- package/esm/models/websearchpreviewtooluserlocation.js +1 -0
- package/esm/models/websearchstatus.js +1 -0
- package/esm/sdk/analytics.js +1 -0
- package/esm/sdk/apikeys.js +1 -0
- package/esm/sdk/beta.js +1 -0
- package/esm/sdk/chat.js +1 -0
- package/esm/sdk/completions.js +1 -0
- package/esm/sdk/credits.js +1 -0
- package/esm/sdk/embeddings.js +1 -0
- package/esm/sdk/endpoints.js +1 -0
- package/esm/sdk/generations.js +1 -0
- package/esm/sdk/index.js +1 -0
- package/esm/sdk/models.js +1 -0
- package/esm/sdk/oauth.js +1 -0
- package/esm/sdk/parameters.js +1 -0
- package/esm/sdk/providers.js +1 -0
- package/esm/sdk/responses.js +1 -0
- package/esm/sdk/sdk.d.ts +3 -3
- package/esm/sdk/sdk.js +2 -1
- package/esm/types/async.js +1 -0
- package/esm/types/blobs.js +1 -0
- package/esm/types/constdatetime.js +1 -0
- package/esm/types/discriminatedUnion.js +1 -0
- package/esm/types/enums.js +1 -0
- package/esm/types/fp.js +1 -0
- package/esm/types/index.js +1 -0
- package/esm/types/operations.js +1 -0
- package/esm/types/rfcdate.js +1 -0
- package/esm/types/streams.js +1 -0
- package/esm/types/unrecognized.js +1 -0
- package/jsr.json +1 -1
- package/package.json +1 -1
- package/tsconfig.json +1 -1
- package/esm/funcs/callModel.d.ts +0 -87
- package/esm/funcs/callModel.js +0 -215
|
@@ -40,6 +40,66 @@ export async function* extractToolDeltas(stream) {
|
|
|
40
40
|
}
|
|
41
41
|
}
|
|
42
42
|
}
|
|
43
|
+
/**
|
|
44
|
+
* Build incremental message updates from responses stream events
|
|
45
|
+
* Returns ResponsesOutputMessage (assistant/responses format)
|
|
46
|
+
*/
|
|
47
|
+
export async function* buildResponsesMessageStream(stream) {
|
|
48
|
+
const consumer = stream.createConsumer();
|
|
49
|
+
// Track the accumulated text and message info
|
|
50
|
+
let currentText = '';
|
|
51
|
+
let currentId = '';
|
|
52
|
+
let hasStarted = false;
|
|
53
|
+
for await (const event of consumer) {
|
|
54
|
+
if (!('type' in event)) {
|
|
55
|
+
continue;
|
|
56
|
+
}
|
|
57
|
+
switch (event.type) {
|
|
58
|
+
case 'response.output_item.added': {
|
|
59
|
+
const itemEvent = event;
|
|
60
|
+
if (itemEvent.item && 'type' in itemEvent.item && itemEvent.item.type === 'message') {
|
|
61
|
+
hasStarted = true;
|
|
62
|
+
currentText = '';
|
|
63
|
+
const msgItem = itemEvent.item;
|
|
64
|
+
currentId = msgItem.id;
|
|
65
|
+
}
|
|
66
|
+
break;
|
|
67
|
+
}
|
|
68
|
+
case 'response.output_text.delta': {
|
|
69
|
+
const deltaEvent = event;
|
|
70
|
+
if (hasStarted && deltaEvent.delta) {
|
|
71
|
+
currentText += deltaEvent.delta;
|
|
72
|
+
// Yield updated message in ResponsesOutputMessage format
|
|
73
|
+
yield {
|
|
74
|
+
id: currentId,
|
|
75
|
+
type: 'message',
|
|
76
|
+
role: 'assistant',
|
|
77
|
+
status: 'in_progress',
|
|
78
|
+
content: [
|
|
79
|
+
{
|
|
80
|
+
type: 'output_text',
|
|
81
|
+
text: currentText,
|
|
82
|
+
annotations: [],
|
|
83
|
+
},
|
|
84
|
+
],
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
break;
|
|
88
|
+
}
|
|
89
|
+
case 'response.output_item.done': {
|
|
90
|
+
const itemDoneEvent = event;
|
|
91
|
+
if (itemDoneEvent.item &&
|
|
92
|
+
'type' in itemDoneEvent.item &&
|
|
93
|
+
itemDoneEvent.item.type === 'message') {
|
|
94
|
+
// Yield final complete message in ResponsesOutputMessage format
|
|
95
|
+
const outputMessage = itemDoneEvent.item;
|
|
96
|
+
yield outputMessage;
|
|
97
|
+
}
|
|
98
|
+
break;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
}
|
|
43
103
|
/**
|
|
44
104
|
* Build incremental message updates from responses stream events
|
|
45
105
|
* Returns AssistantMessage (chat format) instead of ResponsesOutputMessage
|
|
@@ -129,7 +189,7 @@ function convertToAssistantMessage(outputMessage) {
|
|
|
129
189
|
};
|
|
130
190
|
}
|
|
131
191
|
/**
|
|
132
|
-
* Extract the first message from a completed response
|
|
192
|
+
* Extract the first message from a completed response (chat format)
|
|
133
193
|
*/
|
|
134
194
|
export function extractMessageFromResponse(response) {
|
|
135
195
|
const messageItem = response.output.find((item) => 'type' in item && item.type === 'message');
|
|
@@ -138,6 +198,16 @@ export function extractMessageFromResponse(response) {
|
|
|
138
198
|
}
|
|
139
199
|
return convertToAssistantMessage(messageItem);
|
|
140
200
|
}
|
|
201
|
+
/**
|
|
202
|
+
* Extract the first message from a completed response (responses format)
|
|
203
|
+
*/
|
|
204
|
+
export function extractResponsesMessageFromResponse(response) {
|
|
205
|
+
const messageItem = response.output.find((item) => 'type' in item && item.type === 'message');
|
|
206
|
+
if (!messageItem) {
|
|
207
|
+
throw new Error('No message found in response output');
|
|
208
|
+
}
|
|
209
|
+
return messageItem;
|
|
210
|
+
}
|
|
141
211
|
/**
|
|
142
212
|
* Extract text from a response, either from outputText or by concatenating message content
|
|
143
213
|
*/
|
|
@@ -279,4 +349,252 @@ export async function* buildToolCallStream(stream) {
|
|
|
279
349
|
export function responseHasToolCalls(response) {
|
|
280
350
|
return response.output.some((item) => 'type' in item && item.type === 'function_call');
|
|
281
351
|
}
|
|
352
|
+
/**
|
|
353
|
+
* Convert OpenRouter annotations to Claude citations
|
|
354
|
+
*/
|
|
355
|
+
function mapAnnotationsToCitations(annotations) {
|
|
356
|
+
if (!annotations || annotations.length === 0) {
|
|
357
|
+
return undefined;
|
|
358
|
+
}
|
|
359
|
+
const citations = [];
|
|
360
|
+
for (const annotation of annotations) {
|
|
361
|
+
if (!('type' in annotation)) {
|
|
362
|
+
continue;
|
|
363
|
+
}
|
|
364
|
+
switch (annotation.type) {
|
|
365
|
+
case 'file_citation': {
|
|
366
|
+
const fileCite = annotation;
|
|
367
|
+
citations.push({
|
|
368
|
+
type: 'char_location',
|
|
369
|
+
cited_text: '',
|
|
370
|
+
document_index: fileCite.index,
|
|
371
|
+
document_title: fileCite.filename,
|
|
372
|
+
file_id: fileCite.fileId,
|
|
373
|
+
start_char_index: 0,
|
|
374
|
+
end_char_index: 0,
|
|
375
|
+
});
|
|
376
|
+
break;
|
|
377
|
+
}
|
|
378
|
+
case 'url_citation': {
|
|
379
|
+
const urlCite = annotation;
|
|
380
|
+
citations.push({
|
|
381
|
+
type: 'web_search_result_location',
|
|
382
|
+
cited_text: '',
|
|
383
|
+
title: urlCite.title,
|
|
384
|
+
url: urlCite.url,
|
|
385
|
+
encrypted_index: '',
|
|
386
|
+
});
|
|
387
|
+
break;
|
|
388
|
+
}
|
|
389
|
+
case 'file_path': {
|
|
390
|
+
const pathCite = annotation;
|
|
391
|
+
citations.push({
|
|
392
|
+
type: 'char_location',
|
|
393
|
+
cited_text: '',
|
|
394
|
+
document_index: pathCite.index,
|
|
395
|
+
document_title: '',
|
|
396
|
+
file_id: pathCite.fileId,
|
|
397
|
+
start_char_index: 0,
|
|
398
|
+
end_char_index: 0,
|
|
399
|
+
});
|
|
400
|
+
break;
|
|
401
|
+
}
|
|
402
|
+
default: {
|
|
403
|
+
const _exhaustiveCheck = annotation;
|
|
404
|
+
throw new Error(`Unhandled annotation type: ${_exhaustiveCheck.type}`);
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
}
|
|
408
|
+
return citations.length > 0 ? citations : undefined;
|
|
409
|
+
}
|
|
410
|
+
/**
|
|
411
|
+
* Map OpenResponses status to Claude stop reason
|
|
412
|
+
*/
|
|
413
|
+
function mapStopReason(response) {
|
|
414
|
+
// Check if any tool calls exist in the response
|
|
415
|
+
const hasToolCalls = response.output.some((item) => 'type' in item && item.type === 'function_call');
|
|
416
|
+
if (hasToolCalls) {
|
|
417
|
+
return 'tool_use';
|
|
418
|
+
}
|
|
419
|
+
// Check the response status
|
|
420
|
+
if (response.status === 'completed') {
|
|
421
|
+
return 'end_turn';
|
|
422
|
+
}
|
|
423
|
+
if (response.status === 'incomplete') {
|
|
424
|
+
// Check incomplete reason if available
|
|
425
|
+
const incompleteReason = response.incompleteDetails?.reason;
|
|
426
|
+
if (incompleteReason === 'max_output_tokens') {
|
|
427
|
+
return 'max_tokens';
|
|
428
|
+
}
|
|
429
|
+
return 'end_turn';
|
|
430
|
+
}
|
|
431
|
+
return 'end_turn';
|
|
432
|
+
}
|
|
433
|
+
/**
|
|
434
|
+
* Convert OpenResponsesNonStreamingResponse to ClaudeMessage format
|
|
435
|
+
* Compatible with the Anthropic SDK BetaMessage type
|
|
436
|
+
*/
|
|
437
|
+
export function convertToClaudeMessage(response) {
|
|
438
|
+
const content = [];
|
|
439
|
+
const unsupportedContent = [];
|
|
440
|
+
for (const item of response.output) {
|
|
441
|
+
if (!('type' in item)) {
|
|
442
|
+
continue;
|
|
443
|
+
}
|
|
444
|
+
switch (item.type) {
|
|
445
|
+
case 'message': {
|
|
446
|
+
const msgItem = item;
|
|
447
|
+
for (const part of msgItem.content) {
|
|
448
|
+
if (!('type' in part)) {
|
|
449
|
+
continue;
|
|
450
|
+
}
|
|
451
|
+
if (part.type === 'output_text') {
|
|
452
|
+
const textPart = part;
|
|
453
|
+
const citations = mapAnnotationsToCitations(textPart.annotations);
|
|
454
|
+
content.push({
|
|
455
|
+
type: 'text',
|
|
456
|
+
text: textPart.text,
|
|
457
|
+
...(citations && { citations }),
|
|
458
|
+
});
|
|
459
|
+
}
|
|
460
|
+
else if (part.type === 'refusal') {
|
|
461
|
+
const refusalPart = part;
|
|
462
|
+
unsupportedContent.push({
|
|
463
|
+
original_type: 'refusal',
|
|
464
|
+
data: { refusal: refusalPart.refusal },
|
|
465
|
+
reason: 'Claude does not have a native refusal content type',
|
|
466
|
+
});
|
|
467
|
+
}
|
|
468
|
+
}
|
|
469
|
+
break;
|
|
470
|
+
}
|
|
471
|
+
case 'function_call': {
|
|
472
|
+
const fnCall = item;
|
|
473
|
+
let parsedInput = {};
|
|
474
|
+
try {
|
|
475
|
+
parsedInput = JSON.parse(fnCall.arguments);
|
|
476
|
+
}
|
|
477
|
+
catch {
|
|
478
|
+
parsedInput = {};
|
|
479
|
+
}
|
|
480
|
+
content.push({
|
|
481
|
+
type: 'tool_use',
|
|
482
|
+
id: fnCall.callId,
|
|
483
|
+
name: fnCall.name,
|
|
484
|
+
input: parsedInput,
|
|
485
|
+
});
|
|
486
|
+
break;
|
|
487
|
+
}
|
|
488
|
+
case 'reasoning': {
|
|
489
|
+
const reasoningItem = item;
|
|
490
|
+
if (reasoningItem.summary && reasoningItem.summary.length > 0) {
|
|
491
|
+
for (const summaryItem of reasoningItem.summary) {
|
|
492
|
+
if (summaryItem.type === 'summary_text' && summaryItem.text) {
|
|
493
|
+
content.push({
|
|
494
|
+
type: 'thinking',
|
|
495
|
+
thinking: summaryItem.text,
|
|
496
|
+
signature: '',
|
|
497
|
+
});
|
|
498
|
+
}
|
|
499
|
+
}
|
|
500
|
+
}
|
|
501
|
+
if (reasoningItem.encryptedContent) {
|
|
502
|
+
unsupportedContent.push({
|
|
503
|
+
original_type: 'reasoning_encrypted',
|
|
504
|
+
data: {
|
|
505
|
+
id: reasoningItem.id,
|
|
506
|
+
encrypted_content: reasoningItem.encryptedContent,
|
|
507
|
+
},
|
|
508
|
+
reason: 'Encrypted reasoning content preserved for round-trip',
|
|
509
|
+
});
|
|
510
|
+
}
|
|
511
|
+
break;
|
|
512
|
+
}
|
|
513
|
+
case 'web_search_call': {
|
|
514
|
+
const webSearchItem = item;
|
|
515
|
+
content.push({
|
|
516
|
+
type: 'server_tool_use',
|
|
517
|
+
id: webSearchItem.id,
|
|
518
|
+
name: 'web_search',
|
|
519
|
+
input: { status: webSearchItem.status },
|
|
520
|
+
});
|
|
521
|
+
break;
|
|
522
|
+
}
|
|
523
|
+
case 'file_search_call': {
|
|
524
|
+
const fileSearchItem = item;
|
|
525
|
+
content.push({
|
|
526
|
+
type: 'tool_use',
|
|
527
|
+
id: fileSearchItem.id,
|
|
528
|
+
name: 'file_search',
|
|
529
|
+
input: {
|
|
530
|
+
queries: fileSearchItem.queries,
|
|
531
|
+
status: fileSearchItem.status,
|
|
532
|
+
},
|
|
533
|
+
});
|
|
534
|
+
break;
|
|
535
|
+
}
|
|
536
|
+
case 'image_generation_call': {
|
|
537
|
+
const imageGenItem = item;
|
|
538
|
+
unsupportedContent.push({
|
|
539
|
+
original_type: 'image_generation_call',
|
|
540
|
+
data: {
|
|
541
|
+
id: imageGenItem.id,
|
|
542
|
+
result: imageGenItem.result,
|
|
543
|
+
status: imageGenItem.status,
|
|
544
|
+
},
|
|
545
|
+
reason: 'Claude does not support image outputs in assistant messages',
|
|
546
|
+
});
|
|
547
|
+
break;
|
|
548
|
+
}
|
|
549
|
+
default: {
|
|
550
|
+
const _exhaustiveCheck = item;
|
|
551
|
+
throw new Error(`Unhandled output item type: ${_exhaustiveCheck.type}`);
|
|
552
|
+
}
|
|
553
|
+
}
|
|
554
|
+
}
|
|
555
|
+
return {
|
|
556
|
+
id: response.id,
|
|
557
|
+
type: 'message',
|
|
558
|
+
role: 'assistant',
|
|
559
|
+
model: response.model ?? 'unknown',
|
|
560
|
+
content,
|
|
561
|
+
stop_reason: mapStopReason(response),
|
|
562
|
+
stop_sequence: null,
|
|
563
|
+
usage: {
|
|
564
|
+
input_tokens: response.usage?.inputTokens ?? 0,
|
|
565
|
+
output_tokens: response.usage?.outputTokens ?? 0,
|
|
566
|
+
cache_creation_input_tokens: response.usage?.inputTokensDetails?.cachedTokens ?? 0,
|
|
567
|
+
cache_read_input_tokens: 0,
|
|
568
|
+
},
|
|
569
|
+
...(unsupportedContent.length > 0 && { unsupported_content: unsupportedContent }),
|
|
570
|
+
};
|
|
571
|
+
}
|
|
572
|
+
/**
|
|
573
|
+
* Extract unsupported content by original type
|
|
574
|
+
*/
|
|
575
|
+
export function extractUnsupportedContent(message, originalType) {
|
|
576
|
+
if (!message.unsupported_content) {
|
|
577
|
+
return [];
|
|
578
|
+
}
|
|
579
|
+
return message.unsupported_content.filter(item => item.original_type === originalType);
|
|
580
|
+
}
|
|
581
|
+
/**
|
|
582
|
+
* Check if message has any unsupported content
|
|
583
|
+
*/
|
|
584
|
+
export function hasUnsupportedContent(message) {
|
|
585
|
+
return !!(message.unsupported_content && message.unsupported_content.length > 0);
|
|
586
|
+
}
|
|
587
|
+
/**
|
|
588
|
+
* Get summary of unsupported content types
|
|
589
|
+
*/
|
|
590
|
+
export function getUnsupportedContentSummary(message) {
|
|
591
|
+
if (!message.unsupported_content) {
|
|
592
|
+
return {};
|
|
593
|
+
}
|
|
594
|
+
const summary = {};
|
|
595
|
+
for (const item of message.unsupported_content) {
|
|
596
|
+
summary[item.original_type] = (summary[item.original_type] || 0) + 1;
|
|
597
|
+
}
|
|
598
|
+
return summary;
|
|
599
|
+
}
|
|
282
600
|
//# sourceMappingURL=stream-transformers.js.map
|
package/esm/lib/tool-types.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { ZodObject, ZodRawShape, ZodType, z } from 'zod/v4';
|
|
2
2
|
import type * as models from '../models/index.js';
|
|
3
3
|
import type { OpenResponsesStreamEvent } from '../models/index.js';
|
|
4
|
-
import type {
|
|
4
|
+
import type { ModelResult } from './model-result.js';
|
|
5
5
|
/**
|
|
6
6
|
* Tool type enum for enhanced tools
|
|
7
7
|
*/
|
|
@@ -129,8 +129,8 @@ export type MaxToolRounds = number | ((context: TurnContext) => boolean);
|
|
|
129
129
|
* Result of executeTools operation
|
|
130
130
|
*/
|
|
131
131
|
export interface ExecuteToolsResult {
|
|
132
|
-
finalResponse:
|
|
133
|
-
allResponses:
|
|
132
|
+
finalResponse: ModelResult;
|
|
133
|
+
allResponses: ModelResult[];
|
|
134
134
|
toolResults: Map<string, {
|
|
135
135
|
result: unknown;
|
|
136
136
|
preliminaryResults?: unknown[];
|
package/esm/lib/url.js
CHANGED
package/esm/models/chaterror.js
CHANGED