@zenmux/rosetta-ai 1.0.32 → 1.0.33
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/README.md +89 -0
- package/dist/chat-completions/interactions-request.d.ts +5 -0
- package/dist/chat-completions/interactions-request.js +285 -0
- package/dist/chat-completions/interactions.d.ts +39 -0
- package/dist/chat-completions/interactions.js +376 -0
- package/dist/index.d.ts +16 -0
- package/dist/index.js +34 -1
- package/dist/interactions/events.d.ts +6 -0
- package/dist/interactions/events.js +19 -0
- package/dist/interactions/steps.d.ts +84 -0
- package/dist/interactions/steps.js +44 -0
- package/dist/interactions/types.d.ts +42 -0
- package/dist/interactions/types.js +19 -0
- package/dist/interactions/usage.d.ts +11 -0
- package/dist/interactions/usage.js +74 -0
- package/dist/messages/interactions-request.d.ts +4 -0
- package/dist/messages/interactions-request.js +278 -0
- package/dist/messages/interactions.d.ts +42 -0
- package/dist/messages/interactions.js +390 -0
- package/dist/responses/interactions-request.d.ts +7 -0
- package/dist/responses/interactions-request.js +346 -0
- package/dist/responses/interactions.d.ts +49 -0
- package/dist/responses/interactions.js +591 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -29,6 +29,7 @@ One converter instance handles the full round-trip for a single gateway route.
|
|
|
29
29
|
| `ChatCompletionToMessagesConverter` | OpenAI Chat Completions | Anthropic Messages |
|
|
30
30
|
| `ChatCompletionToResponsesConverter` | OpenAI Chat Completions | OpenAI Responses |
|
|
31
31
|
| `ChatCompletionToGeminiConverter` | OpenAI Chat Completions | Google Gemini |
|
|
32
|
+
| `ChatCompletionToInteractionsConverter` | OpenAI Chat Completions | Google Interactions V1 |
|
|
32
33
|
| `MessagesToChatCompletionConverter` | Anthropic Messages | OpenAI Chat Completions |
|
|
33
34
|
| `MessagesToResponsesConverter` | Anthropic Messages | OpenAI Responses |
|
|
34
35
|
| `MessagesToGeminiConverter` | Anthropic Messages | Google Gemini |
|
|
@@ -41,6 +42,41 @@ One converter instance handles the full round-trip for a single gateway route.
|
|
|
41
42
|
|
|
42
43
|
## Usage
|
|
43
44
|
|
|
45
|
+
### OpenAI Chat Completions → Google Interactions V1
|
|
46
|
+
|
|
47
|
+
```typescript
|
|
48
|
+
import { ChatCompletionToInteractionsConverter } from "@zenmux/rosetta-ai";
|
|
49
|
+
|
|
50
|
+
const converter = new ChatCompletionToInteractionsConverter();
|
|
51
|
+
const request = converter.convertRequest(openaiRequest);
|
|
52
|
+
// Send request to POST /v1/interactions using your own transport.
|
|
53
|
+
const completion = converter.convertResponse(interaction);
|
|
54
|
+
// Pass parsed V1 events, not raw SSE bytes:
|
|
55
|
+
for await (const chunk of converter.convertStream(interactionEvents)) {
|
|
56
|
+
// Consume CC chunks.
|
|
57
|
+
}
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
This converter uses the [V1 steps/step.delta contract](https://ai.google.dev/api/interactions-api-v1),
|
|
61
|
+
not the outputs/content.delta types shipped in `@google/genai@1.52`. Use one instance per request.
|
|
62
|
+
Request conversion supports full-history steps, function calls/results, standard CC image/audio/file
|
|
63
|
+
inputs, JSON output, tool choice and supported generation settings. Explicit settings without a V1
|
|
64
|
+
equivalent (including temperature/top_p, n>1 and parallel_tool_calls=false) raise `InteractionsRequestError`.
|
|
65
|
+
CC's default `store=false` is sent explicitly.
|
|
66
|
+
|
|
67
|
+
Media output is rejected in both JSON and SSE. CC reasoning content and native step/event
|
|
68
|
+
extensions are not emitted. Standard `reasoning_effort` remains supported, and thought tokens
|
|
69
|
+
are included in completion usage. Only standard CC message fields can be used for history.
|
|
70
|
+
|
|
71
|
+
Client usage contains standard protocol counters. `getUsage()` exposes a cloned source snapshot
|
|
72
|
+
captured by the converter, including unknown fields, for internal logging and billing. Prompt
|
|
73
|
+
tokens come from `total_input_tokens`; completion tokens are visible output plus thought tokens.
|
|
74
|
+
Tool-use tokens are not added again, following the official V1 example (100 input, 25 output,
|
|
75
|
+
50 tool-use, 125 total). Cumulative SSE usage replaces counters instead of being summed.
|
|
76
|
+
Missing counters stay missing, so usage is typed as partial; callers must check completeness
|
|
77
|
+
before billing. The caller remains responsible for retaining the complete original response.
|
|
78
|
+
When driving `convertStreamEvent()` manually, call `finishStream()` at EOF to detect truncation.
|
|
79
|
+
|
|
44
80
|
### OpenAI Chat Completions → Anthropic Messages
|
|
45
81
|
|
|
46
82
|
```typescript
|
|
@@ -466,3 +502,56 @@ npm run build # Build
|
|
|
466
502
|
## License
|
|
467
503
|
|
|
468
504
|
Apache-2.0
|
|
505
|
+
|
|
506
|
+
### OpenAI Responses ↔ Google Interactions V1
|
|
507
|
+
|
|
508
|
+
The development branch exports `ResponsesToInteractionsConverter` and
|
|
509
|
+
`convertInteractionResponseUsage`. Conversion is direct and uses the V1
|
|
510
|
+
`steps` / `step.delta` contract.
|
|
511
|
+
|
|
512
|
+
```typescript
|
|
513
|
+
import { ResponsesToInteractionsConverter } from "@zenmux/rosetta-ai";
|
|
514
|
+
|
|
515
|
+
const converter = new ResponsesToInteractionsConverter();
|
|
516
|
+
const request = converter.convertRequest({
|
|
517
|
+
model: "your-interactions-model",
|
|
518
|
+
input: "Hello",
|
|
519
|
+
store: false,
|
|
520
|
+
});
|
|
521
|
+
// Send request to the Interactions endpoint, then use one response path:
|
|
522
|
+
const response = converter.convertResponse(nativeInteraction);
|
|
523
|
+
// Or: for await (const event of converter.convertStream(nativeEvents)) { ... }
|
|
524
|
+
```
|
|
525
|
+
|
|
526
|
+
Create a converter per request. It maps Responses items, call IDs, instructions,
|
|
527
|
+
function tools, structured output and supported reasoning controls. Responses
|
|
528
|
+
`store` defaults to true. Unsupported stateful/background execution, strict
|
|
529
|
+
constraints, sampling controls and provider-owned file IDs fail explicitly.
|
|
530
|
+
Function arguments remain JSON objects, and streamed item identities remain
|
|
531
|
+
stable. Terminals wait until EOF so late cumulative usage and malformed streams
|
|
532
|
+
can be checked before reporting completion.
|
|
533
|
+
|
|
534
|
+
`convertInteractionResponseUsage` returns standard usage counters; output tokens include thought
|
|
535
|
+
tokens exactly once. `getUsage()` retains the full raw usage snapshot internally. Missing usage
|
|
536
|
+
returns null and partial snapshots remain partial.
|
|
537
|
+
|
|
538
|
+
Only standard text/image/file request content is accepted. Media output, raw native history,
|
|
539
|
+
built-in search tools without an implemented equivalent output mapping, and
|
|
540
|
+
`reasoning.encrypted_content` are rejected. Reasoning summaries use standard Responses items;
|
|
541
|
+
no native-step envelope is encoded as encrypted content. These summary items can be replayed
|
|
542
|
+
as standard history, without a native signature. After applying request defaults, callers may
|
|
543
|
+
use `setEffectiveRequest(request)` to keep response configuration echoes consistent.
|
|
544
|
+
|
|
545
|
+
This implementation is not yet published as a Responses-capable npm release.
|
|
546
|
+
|
|
547
|
+
## Messages → Google Interactions (development)
|
|
548
|
+
|
|
549
|
+
`MessagesToInteractionsConverter` directly translates Messages requests and Interactions V1 JSON/SSE responses. It supports text, image/document inputs, function calls/results (including `is_error`), system text, stop sequences, JSON Schema, and adaptive thinking with low/medium/high effort. Model support is separate from protocol support. Unsupported sampling, thinking budgets/disable, strict guarantees, cache controls, built-in Anthropic tools and stored-history parameters are rejected explicitly.
|
|
550
|
+
|
|
551
|
+
Messages `input_tokens` excludes reported cache reads: `total_input_tokens - (total_cached_tokens ?? 0)`. `cache_read_input_tokens` is emitted only when reported, `output_tokens` includes output plus thought once, and `getUsage()` retains the complete original usage internally. Client usage contains only standard counters. Missing counters are not filled with zero; cache creation is not inferred. Stream usage is cumulative and completed only after the native terminal event and EOF, including late metadata.
|
|
552
|
+
|
|
553
|
+
Thinking uses standard `thinking` and `signature` fields. The native opaque signature is returned
|
|
554
|
+
and can be sent back unchanged in standard Messages history; no custom step envelope is needed.
|
|
555
|
+
Media outputs and audio/video input blocks are rejected. Empty stateless native IDs are accepted
|
|
556
|
+
and a client message ID is generated. Unspecified native `incomplete` results retain a null stop
|
|
557
|
+
reason instead of inventing a token-limit cause.
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type OpenAI from "openai";
|
|
2
|
+
import { type InteractionCreateParams } from "../interactions/types";
|
|
3
|
+
/** Validate parameter semantics before gateways filter unsupported model fields. */
|
|
4
|
+
export declare function validateChatCompletionInteractionsParameters(params: OpenAI.ChatCompletionCreateParams): void;
|
|
5
|
+
export declare function convertChatCompletionRequest(params: OpenAI.ChatCompletionCreateParams): InteractionCreateParams;
|
|
@@ -0,0 +1,285 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.validateChatCompletionInteractionsParameters = validateChatCompletionInteractionsParameters;
|
|
4
|
+
exports.convertChatCompletionRequest = convertChatCompletionRequest;
|
|
5
|
+
const types_1 = require("../interactions/types");
|
|
6
|
+
/** Validate parameter semantics before gateways filter unsupported model fields. */
|
|
7
|
+
function validateChatCompletionInteractionsParameters(params) {
|
|
8
|
+
// These parameters are not supported by this converter.
|
|
9
|
+
// Reject explicit requests instead of silently changing sampling/format semantics.
|
|
10
|
+
for (const key of [
|
|
11
|
+
"temperature",
|
|
12
|
+
"top_p",
|
|
13
|
+
"frequency_penalty",
|
|
14
|
+
"presence_penalty",
|
|
15
|
+
"logit_bias",
|
|
16
|
+
"logprobs",
|
|
17
|
+
"top_logprobs",
|
|
18
|
+
"prediction",
|
|
19
|
+
"audio",
|
|
20
|
+
]) {
|
|
21
|
+
if (params[key] != null)
|
|
22
|
+
fail(`${key} is not supported by the Interactions converter`);
|
|
23
|
+
}
|
|
24
|
+
if (params.response_format != null &&
|
|
25
|
+
!["text", "json_object", "json_schema"].includes(params.response_format.type))
|
|
26
|
+
fail("Unsupported response_format");
|
|
27
|
+
if (params.response_format?.type === "json_schema" &&
|
|
28
|
+
params.response_format.json_schema.strict === true)
|
|
29
|
+
fail("Interactions cannot guarantee json_schema strict=true");
|
|
30
|
+
for (const tool of params.tools ?? [])
|
|
31
|
+
if (tool.type === "function" && tool.function.strict === true)
|
|
32
|
+
fail("Interactions cannot guarantee function strict=true");
|
|
33
|
+
if (params.n != null && params.n !== 1)
|
|
34
|
+
fail("Interactions V1 supports only n=1");
|
|
35
|
+
if (params.parallel_tool_calls === false)
|
|
36
|
+
fail("Interactions V1 cannot enforce parallel_tool_calls=false");
|
|
37
|
+
if (params.modalities?.some(modality => modality !== "text")) {
|
|
38
|
+
fail("Interactions conversion supports text output only");
|
|
39
|
+
}
|
|
40
|
+
if (params.reasoning_effort != null &&
|
|
41
|
+
!["minimal", "low", "medium", "high"].includes(params.reasoning_effort)) {
|
|
42
|
+
fail(`reasoning_effort=${params.reasoning_effort} has no equivalent in Interactions V1`);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
function convertChatCompletionRequest(params) {
|
|
46
|
+
validateChatCompletionInteractionsParameters(params);
|
|
47
|
+
const input = [];
|
|
48
|
+
const system = [];
|
|
49
|
+
const names = new Map();
|
|
50
|
+
if (!Array.isArray(params.messages))
|
|
51
|
+
fail("messages must be an array");
|
|
52
|
+
for (const message of params.messages) {
|
|
53
|
+
if (!message || typeof message !== "object")
|
|
54
|
+
fail("messages must contain objects");
|
|
55
|
+
switch (message.role) {
|
|
56
|
+
case "system":
|
|
57
|
+
case "developer":
|
|
58
|
+
system.push(textContent(message.content));
|
|
59
|
+
break;
|
|
60
|
+
case "user": {
|
|
61
|
+
const content = typeof message.content === "string"
|
|
62
|
+
? [{ type: "text", text: message.content }]
|
|
63
|
+
: message.content.map(convertContent);
|
|
64
|
+
input.push({ type: "user_input", content });
|
|
65
|
+
break;
|
|
66
|
+
}
|
|
67
|
+
case "assistant": {
|
|
68
|
+
if (message.audio)
|
|
69
|
+
fail("assistant.audio history requires an inline Interactions audio input");
|
|
70
|
+
if (message.content != null) {
|
|
71
|
+
const text = textContent(message.content);
|
|
72
|
+
if (text)
|
|
73
|
+
input.push({ type: "model_output", content: [{ type: "text", text }] });
|
|
74
|
+
}
|
|
75
|
+
for (const call of message.tool_calls ?? []) {
|
|
76
|
+
if (call.type !== "function")
|
|
77
|
+
fail("custom tool calls are not supported by Interactions V1");
|
|
78
|
+
names.set(call.id, call.function.name);
|
|
79
|
+
input.push({
|
|
80
|
+
type: "function_call",
|
|
81
|
+
id: call.id,
|
|
82
|
+
name: call.function.name,
|
|
83
|
+
arguments: parseArguments(call.function.arguments),
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
if (message.function_call) {
|
|
87
|
+
const id = `call_${input.length}`;
|
|
88
|
+
names.set(message.function_call.name, id);
|
|
89
|
+
input.push({
|
|
90
|
+
type: "function_call",
|
|
91
|
+
id,
|
|
92
|
+
name: message.function_call.name,
|
|
93
|
+
arguments: parseArguments(message.function_call.arguments),
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
break;
|
|
97
|
+
}
|
|
98
|
+
case "tool":
|
|
99
|
+
input.push({
|
|
100
|
+
type: "function_result",
|
|
101
|
+
call_id: message.tool_call_id,
|
|
102
|
+
...(names.has(message.tool_call_id) && { name: names.get(message.tool_call_id) }),
|
|
103
|
+
result: textContent(message.content),
|
|
104
|
+
});
|
|
105
|
+
break;
|
|
106
|
+
case "function": {
|
|
107
|
+
const callId = names.get(message.name);
|
|
108
|
+
if (!callId)
|
|
109
|
+
fail(`No preceding function call for ${message.name}`);
|
|
110
|
+
input.push({
|
|
111
|
+
type: "function_result",
|
|
112
|
+
call_id: callId,
|
|
113
|
+
name: message.name,
|
|
114
|
+
result: message.content ?? "",
|
|
115
|
+
});
|
|
116
|
+
break;
|
|
117
|
+
}
|
|
118
|
+
default:
|
|
119
|
+
fail("Unsupported message role");
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
const config = {};
|
|
123
|
+
const maxTokens = params.max_completion_tokens ?? params.max_tokens;
|
|
124
|
+
if (maxTokens != null)
|
|
125
|
+
config.max_output_tokens = maxTokens;
|
|
126
|
+
if (params.seed != null)
|
|
127
|
+
config.seed = params.seed;
|
|
128
|
+
if (params.stop != null)
|
|
129
|
+
config.stop_sequences = typeof params.stop === "string" ? [params.stop] : [...params.stop];
|
|
130
|
+
if (params.reasoning_effort != null) {
|
|
131
|
+
config.thinking_level =
|
|
132
|
+
params.reasoning_effort;
|
|
133
|
+
}
|
|
134
|
+
const tools = [];
|
|
135
|
+
for (const tool of params.tools ?? []) {
|
|
136
|
+
if (tool.type !== "function")
|
|
137
|
+
fail("custom tools are not supported by Interactions V1");
|
|
138
|
+
tools.push({
|
|
139
|
+
type: "function",
|
|
140
|
+
name: tool.function.name,
|
|
141
|
+
description: tool.function.description,
|
|
142
|
+
parameters: tool.function.parameters,
|
|
143
|
+
});
|
|
144
|
+
}
|
|
145
|
+
if (!params.tools && params.functions) {
|
|
146
|
+
tools.push(...params.functions.map(fn => ({
|
|
147
|
+
type: "function",
|
|
148
|
+
name: fn.name,
|
|
149
|
+
description: fn.description,
|
|
150
|
+
parameters: fn.parameters,
|
|
151
|
+
})));
|
|
152
|
+
}
|
|
153
|
+
if (params.web_search_options != null)
|
|
154
|
+
tools.push({ type: "google_search" });
|
|
155
|
+
if (params.tool_choice != null)
|
|
156
|
+
config.tool_choice = convertToolChoice(params.tool_choice);
|
|
157
|
+
else if (params.function_call != null) {
|
|
158
|
+
config.tool_choice =
|
|
159
|
+
typeof params.function_call === "string"
|
|
160
|
+
? params.function_call
|
|
161
|
+
: { allowed_tools: { mode: "any", tools: [params.function_call.name] } };
|
|
162
|
+
}
|
|
163
|
+
const result = {
|
|
164
|
+
model: params.model,
|
|
165
|
+
input,
|
|
166
|
+
// CC defaults to false, unlike Interactions' stored-session workflow.
|
|
167
|
+
store: params.store ?? false,
|
|
168
|
+
stream: params.stream ?? false,
|
|
169
|
+
...(system.length > 0 && { system_instruction: system.join("\n") }),
|
|
170
|
+
...(Object.keys(config).length > 0 && { generation_config: config }),
|
|
171
|
+
...(tools.length > 0 && { tools }),
|
|
172
|
+
};
|
|
173
|
+
if (params.metadata != null) {
|
|
174
|
+
if (Object.values(params.metadata).some(value => typeof value !== "string"))
|
|
175
|
+
fail("metadata values must be strings");
|
|
176
|
+
result.labels = { ...params.metadata };
|
|
177
|
+
}
|
|
178
|
+
if (params.response_format?.type === "text")
|
|
179
|
+
result.response_format = { type: "text", mime_type: "text/plain" };
|
|
180
|
+
else if (params.response_format?.type === "json_object")
|
|
181
|
+
result.response_format = { type: "text", mime_type: "application/json" };
|
|
182
|
+
else if (params.response_format?.type === "json_schema") {
|
|
183
|
+
result.response_format = {
|
|
184
|
+
type: "text",
|
|
185
|
+
mime_type: "application/json",
|
|
186
|
+
schema: params.response_format.json_schema.schema,
|
|
187
|
+
};
|
|
188
|
+
}
|
|
189
|
+
return result;
|
|
190
|
+
}
|
|
191
|
+
function convertToolChoice(choice) {
|
|
192
|
+
if (typeof choice === "string")
|
|
193
|
+
return choice === "required" ? "any" : choice;
|
|
194
|
+
if (choice.type === "function")
|
|
195
|
+
return { allowed_tools: { mode: "any", tools: [choice.function.name] } };
|
|
196
|
+
if (choice.type === "allowed_tools") {
|
|
197
|
+
return {
|
|
198
|
+
allowed_tools: {
|
|
199
|
+
mode: choice.allowed_tools.mode === "required" ? "any" : "auto",
|
|
200
|
+
tools: choice.allowed_tools.tools.map(tool => {
|
|
201
|
+
if (tool.type !== "function" ||
|
|
202
|
+
typeof tool.function !== "object" ||
|
|
203
|
+
tool.function == null ||
|
|
204
|
+
!("name" in tool.function) ||
|
|
205
|
+
typeof tool.function.name !== "string")
|
|
206
|
+
fail("allowed_tools must contain named functions");
|
|
207
|
+
return tool.function.name;
|
|
208
|
+
}),
|
|
209
|
+
},
|
|
210
|
+
};
|
|
211
|
+
}
|
|
212
|
+
return fail("Unsupported Interactions tool_choice");
|
|
213
|
+
}
|
|
214
|
+
function textContent(content) {
|
|
215
|
+
if (typeof content === "string")
|
|
216
|
+
return content;
|
|
217
|
+
if (!Array.isArray(content))
|
|
218
|
+
return fail("Message content must be text or an array of text parts");
|
|
219
|
+
return content
|
|
220
|
+
.map(part => {
|
|
221
|
+
if (part?.type === "text" && typeof part.text === "string")
|
|
222
|
+
return part.text;
|
|
223
|
+
if (part?.type === "refusal" && typeof part.refusal === "string")
|
|
224
|
+
return part.refusal;
|
|
225
|
+
return fail("Expected a text message part");
|
|
226
|
+
})
|
|
227
|
+
.join("");
|
|
228
|
+
}
|
|
229
|
+
function convertContent(part) {
|
|
230
|
+
if (!part)
|
|
231
|
+
return fail("Message content parts must be objects");
|
|
232
|
+
if (part.type === "text")
|
|
233
|
+
return { type: "text", text: part.text };
|
|
234
|
+
if (part.type === "image_url")
|
|
235
|
+
return mediaContent("image", part.image_url.url);
|
|
236
|
+
if (part.type === "input_audio")
|
|
237
|
+
return {
|
|
238
|
+
type: "audio",
|
|
239
|
+
data: part.input_audio.data,
|
|
240
|
+
mime_type: part.input_audio.format === "mp3" ? "audio/mp3" : "audio/wav",
|
|
241
|
+
};
|
|
242
|
+
if (part.type === "file") {
|
|
243
|
+
if (part.file.file_id)
|
|
244
|
+
return fail("OpenAI file_id cannot be resolved by Interactions; use file_data");
|
|
245
|
+
if (!part.file.file_data)
|
|
246
|
+
return fail("file.file_data is required");
|
|
247
|
+
const mime = part.file.filename?.endsWith(".csv")
|
|
248
|
+
? "text/csv"
|
|
249
|
+
: part.file.filename?.endsWith(".pdf")
|
|
250
|
+
? "application/pdf"
|
|
251
|
+
: undefined;
|
|
252
|
+
if (!part.file.file_data.startsWith("data:") && !mime)
|
|
253
|
+
return fail("file_data requires a MIME data URL or a .pdf/.csv filename");
|
|
254
|
+
return part.file.file_data.startsWith("data:")
|
|
255
|
+
? mediaContent("document", part.file.file_data)
|
|
256
|
+
: { type: "document", data: part.file.file_data, mime_type: mime };
|
|
257
|
+
}
|
|
258
|
+
return fail("Unsupported CC content part");
|
|
259
|
+
}
|
|
260
|
+
function mediaContent(type, value) {
|
|
261
|
+
if (typeof value !== "string")
|
|
262
|
+
return fail("Media URL must be a string");
|
|
263
|
+
if (!value.startsWith("data:"))
|
|
264
|
+
return { type, uri: value };
|
|
265
|
+
const match = /^data:([^;,]+);base64,([\s\S]*)$/.exec(value);
|
|
266
|
+
if (!match)
|
|
267
|
+
return fail("Expected a base64 media data URL");
|
|
268
|
+
return { type, mime_type: match[1], data: match[2] };
|
|
269
|
+
}
|
|
270
|
+
function parseArguments(value) {
|
|
271
|
+
try {
|
|
272
|
+
const result = JSON.parse(value);
|
|
273
|
+
if (!result || typeof result !== "object" || Array.isArray(result))
|
|
274
|
+
return fail("Function arguments must be a JSON object");
|
|
275
|
+
return result;
|
|
276
|
+
}
|
|
277
|
+
catch (error) {
|
|
278
|
+
if (error instanceof types_1.InteractionsRequestError)
|
|
279
|
+
throw error;
|
|
280
|
+
return fail("Function arguments must be valid JSON");
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
function fail(message) {
|
|
284
|
+
throw new types_1.InteractionsRequestError(message);
|
|
285
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import type OpenAI from "openai";
|
|
2
|
+
import { type Interaction, type InteractionCreateParams, type InteractionEvent, type InteractionUsage } from "../interactions/types";
|
|
3
|
+
import { type InteractionCompletionUsage } from "../interactions/usage";
|
|
4
|
+
export type InteractionChatCompletion = Omit<OpenAI.ChatCompletion, "usage"> & {
|
|
5
|
+
usage?: InteractionCompletionUsage;
|
|
6
|
+
};
|
|
7
|
+
export type InteractionChatCompletionChunk = Omit<OpenAI.ChatCompletionChunk, "usage"> & {
|
|
8
|
+
usage?: InteractionCompletionUsage;
|
|
9
|
+
};
|
|
10
|
+
export declare class ChatCompletionToInteractionsConverter {
|
|
11
|
+
private id;
|
|
12
|
+
private model;
|
|
13
|
+
private created;
|
|
14
|
+
private started;
|
|
15
|
+
private completed;
|
|
16
|
+
private steps;
|
|
17
|
+
private eventIds;
|
|
18
|
+
private toolCount;
|
|
19
|
+
private textLength;
|
|
20
|
+
private usage?;
|
|
21
|
+
convertRequest(params: OpenAI.ChatCompletionCreateParams): InteractionCreateParams;
|
|
22
|
+
convertResponse(response: Interaction): InteractionChatCompletion;
|
|
23
|
+
convertStream(stream: AsyncIterable<InteractionEvent>): AsyncIterable<InteractionChatCompletionChunk>;
|
|
24
|
+
convertStreamEvent(event: InteractionEvent): InteractionChatCompletionChunk[];
|
|
25
|
+
finishStream(): void;
|
|
26
|
+
getUsage(): InteractionUsage | undefined;
|
|
27
|
+
private startStep;
|
|
28
|
+
private stopStep;
|
|
29
|
+
private validateToolArguments;
|
|
30
|
+
private chunk;
|
|
31
|
+
private timestamp;
|
|
32
|
+
private generateId;
|
|
33
|
+
private checkStatus;
|
|
34
|
+
private finishReason;
|
|
35
|
+
private checkFunction;
|
|
36
|
+
private validateHiddenStep;
|
|
37
|
+
private unsupportedContent;
|
|
38
|
+
private convertAnnotations;
|
|
39
|
+
}
|