@saturn-universe/model 0.0.0-stage → 0.1.0
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/LICENSE +661 -0
- package/dist/codec.d.ts +12 -0
- package/dist/codec.js +129 -0
- package/dist/errors.d.ts +7 -0
- package/dist/errors.js +36 -0
- package/dist/index.d.ts +24 -0
- package/dist/index.js +540 -0
- package/dist/provider-errors.d.ts +13 -0
- package/dist/provider-errors.js +170 -0
- package/dist/sdk-adapter.d.ts +33 -0
- package/dist/sdk-adapter.js +315 -0
- package/dist/sdk-messages.d.ts +23 -0
- package/dist/sdk-messages.js +154 -0
- package/dist/wire.d.ts +61 -0
- package/dist/wire.js +481 -0
- package/package.json +21 -3
- package/README.md +0 -3
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
import { GatewayError } from './errors.js';
|
|
3
|
+
const messageContent = z.array(z.record(z.string(), z.json()));
|
|
4
|
+
const objectJson = z.record(z.string(), z.json());
|
|
5
|
+
export function parseJson(value) {
|
|
6
|
+
try {
|
|
7
|
+
return JSON.parse(value);
|
|
8
|
+
}
|
|
9
|
+
catch {
|
|
10
|
+
return undefined;
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
function requiredString(value) {
|
|
14
|
+
if (typeof value !== 'string')
|
|
15
|
+
throw new GatewayError('invalid_request');
|
|
16
|
+
return value;
|
|
17
|
+
}
|
|
18
|
+
export function replayOptions(protocol, metadata) {
|
|
19
|
+
if (protocol === 'openai-chat')
|
|
20
|
+
return undefined;
|
|
21
|
+
const key = protocol === 'gemini' ? 'google' : 'anthropic';
|
|
22
|
+
const namespace = z.object({ [key]: z.unknown() }).safeParse(metadata).data?.[key];
|
|
23
|
+
if (protocol === 'gemini') {
|
|
24
|
+
const signature = z.object({ thoughtSignature: z.string() }).safeParse(namespace).data;
|
|
25
|
+
return signature ? { google: signature } : undefined;
|
|
26
|
+
}
|
|
27
|
+
const result = z
|
|
28
|
+
.object({ signature: z.string().optional(), redactedData: z.string().optional() })
|
|
29
|
+
.safeParse(namespace).data;
|
|
30
|
+
if (result?.signature !== undefined)
|
|
31
|
+
return { anthropic: { signature: result.signature } };
|
|
32
|
+
if (result?.redactedData !== undefined)
|
|
33
|
+
return { anthropic: { redactedData: result.redactedData } };
|
|
34
|
+
return undefined;
|
|
35
|
+
}
|
|
36
|
+
function userPart(part) {
|
|
37
|
+
if (part.type === 'input_text')
|
|
38
|
+
return { type: 'text', text: requiredString(part.text) };
|
|
39
|
+
if (part.type !== 'input_image' && part.type !== 'input_file')
|
|
40
|
+
throw new GatewayError('unsupported_item');
|
|
41
|
+
const source = requiredString(part.type === 'input_image' ? part.image_url : part.file_data);
|
|
42
|
+
const data = /^data:([\w.+-]+\/[\w.+-]+);base64,([a-z\d+/]*={0,2})$/i.exec(source);
|
|
43
|
+
if (!data?.[1] || data[2] === undefined)
|
|
44
|
+
throw new GatewayError('unsupported_item');
|
|
45
|
+
return {
|
|
46
|
+
type: 'file',
|
|
47
|
+
data: { type: 'data', data: data[2] },
|
|
48
|
+
mediaType: data[1],
|
|
49
|
+
...(typeof part.filename === 'string' ? { filename: part.filename } : {}),
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
export function sdkMessages(request, protocol) {
|
|
53
|
+
const system = [request.responses.instructions ?? ''];
|
|
54
|
+
const messages = [];
|
|
55
|
+
const callNames = new Map();
|
|
56
|
+
const assistant = (part) => {
|
|
57
|
+
const last = messages.at(-1);
|
|
58
|
+
if (last?.role === 'assistant' && Array.isArray(last.content))
|
|
59
|
+
last.content.push(part);
|
|
60
|
+
else
|
|
61
|
+
messages.push({ role: 'assistant', content: [part] });
|
|
62
|
+
};
|
|
63
|
+
const result = (part) => {
|
|
64
|
+
const last = messages.at(-1);
|
|
65
|
+
if (last?.role === 'tool')
|
|
66
|
+
last.content.push(part);
|
|
67
|
+
else
|
|
68
|
+
messages.push({ role: 'tool', content: [part] });
|
|
69
|
+
};
|
|
70
|
+
for (const item of request.responses.fullInput) {
|
|
71
|
+
switch (item.type) {
|
|
72
|
+
case 'message': {
|
|
73
|
+
const parts = messageContent.safeParse(item.content);
|
|
74
|
+
if (!parts.success)
|
|
75
|
+
throw new GatewayError('invalid_request');
|
|
76
|
+
if (item.role === 'system' || item.role === 'developer') {
|
|
77
|
+
for (const part of parts.data) {
|
|
78
|
+
if (part.type !== 'input_text')
|
|
79
|
+
throw new GatewayError('unsupported_item');
|
|
80
|
+
system.push(requiredString(part.text));
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
else if (item.role === 'user')
|
|
84
|
+
messages.push({ role: 'user', content: parts.data.map(userPart) });
|
|
85
|
+
else if (item.role === 'assistant') {
|
|
86
|
+
for (const part of parts.data) {
|
|
87
|
+
if (part.type !== 'output_text' && part.type !== 'refusal')
|
|
88
|
+
throw new GatewayError('unsupported_item');
|
|
89
|
+
const providerOptions = replayOptions(protocol, part.provider_options);
|
|
90
|
+
assistant({
|
|
91
|
+
type: 'text',
|
|
92
|
+
text: requiredString(part.type === 'refusal' ? part.refusal : part.text),
|
|
93
|
+
...(providerOptions ? { providerOptions } : {}),
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
else
|
|
98
|
+
throw new GatewayError('unsupported_item');
|
|
99
|
+
break;
|
|
100
|
+
}
|
|
101
|
+
case 'reasoning': {
|
|
102
|
+
const providerOptions = replayOptions(protocol, item.provider_options);
|
|
103
|
+
if (protocol === 'openai-chat' || providerOptions) {
|
|
104
|
+
const parts = messageContent.safeParse(item.summary);
|
|
105
|
+
if (!parts.success)
|
|
106
|
+
throw new GatewayError('invalid_request');
|
|
107
|
+
assistant({
|
|
108
|
+
type: 'reasoning',
|
|
109
|
+
text: parts.data.map((part) => requiredString(part.text)).join(''),
|
|
110
|
+
...(providerOptions ? { providerOptions } : {}),
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
break;
|
|
114
|
+
}
|
|
115
|
+
case 'function_call': {
|
|
116
|
+
const toolCallId = requiredString(item.call_id);
|
|
117
|
+
const toolName = requiredString(item.name);
|
|
118
|
+
if (callNames.has(toolCallId))
|
|
119
|
+
throw new GatewayError('continuation_mismatch');
|
|
120
|
+
callNames.set(toolCallId, toolName);
|
|
121
|
+
const providerOptions = replayOptions(protocol, item.provider_options);
|
|
122
|
+
const args = objectJson.safeParse(parseJson(requiredString(item.arguments)));
|
|
123
|
+
assistant({
|
|
124
|
+
type: 'tool-call',
|
|
125
|
+
toolCallId,
|
|
126
|
+
toolName,
|
|
127
|
+
// Invalid arguments are reported by Saturn in the associated tool result.
|
|
128
|
+
input: args.success ? args.data : {},
|
|
129
|
+
...(providerOptions ? { providerOptions } : {}),
|
|
130
|
+
});
|
|
131
|
+
break;
|
|
132
|
+
}
|
|
133
|
+
case 'function_call_output': {
|
|
134
|
+
const toolCallId = requiredString(item.call_id);
|
|
135
|
+
const toolName = callNames.get(toolCallId);
|
|
136
|
+
if (toolName === undefined)
|
|
137
|
+
throw new GatewayError('continuation_mismatch');
|
|
138
|
+
callNames.delete(toolCallId);
|
|
139
|
+
const value = requiredString(item.output);
|
|
140
|
+
const parsed = z.json().safeParse(parseJson(value));
|
|
141
|
+
result({
|
|
142
|
+
type: 'tool-result',
|
|
143
|
+
toolCallId,
|
|
144
|
+
toolName,
|
|
145
|
+
output: parsed.success ? { type: 'json', value: parsed.data } : { type: 'text', value },
|
|
146
|
+
});
|
|
147
|
+
break;
|
|
148
|
+
}
|
|
149
|
+
default:
|
|
150
|
+
throw new GatewayError('unsupported_item');
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
return { system: system.filter(Boolean).join('\n\n'), messages };
|
|
154
|
+
}
|
package/dist/wire.d.ts
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
import type { ModelRequest, ProtocolJsonObject, ProviderProfile, ModelCapabilities, ResponsesOutputItem, ModelStreamEvent, ModelFailureDetails, Usage } from '@saturn-universe/core';
|
|
3
|
+
import { GatewayError } from './errors.js';
|
|
4
|
+
declare const responseSchema: z.ZodObject<{
|
|
5
|
+
id: z.ZodString;
|
|
6
|
+
status: z.ZodEnum<{
|
|
7
|
+
completed: "completed";
|
|
8
|
+
incomplete: "incomplete";
|
|
9
|
+
}>;
|
|
10
|
+
output: z.ZodArray<z.ZodRecord<z.ZodString, z.ZodJSONSchema>>;
|
|
11
|
+
usage: z.ZodObject<{
|
|
12
|
+
input_tokens: z.ZodNumber;
|
|
13
|
+
output_tokens: z.ZodNumber;
|
|
14
|
+
input_tokens_details: z.ZodOptional<z.ZodNullable<z.ZodObject<{
|
|
15
|
+
cached_tokens: z.ZodOptional<z.ZodNumber>;
|
|
16
|
+
}, z.core.$strip>>>;
|
|
17
|
+
output_tokens_details: z.ZodOptional<z.ZodNullable<z.ZodObject<{
|
|
18
|
+
reasoning_tokens: z.ZodOptional<z.ZodNumber>;
|
|
19
|
+
}, z.core.$strip>>>;
|
|
20
|
+
}, z.core.$strip>;
|
|
21
|
+
incomplete_details: z.ZodOptional<z.ZodNullable<z.ZodObject<{
|
|
22
|
+
reason: z.ZodEnum<{
|
|
23
|
+
max_output_tokens: "max_output_tokens";
|
|
24
|
+
content_filter: "content_filter";
|
|
25
|
+
}>;
|
|
26
|
+
}, z.core.$strip>>>;
|
|
27
|
+
model: z.ZodOptional<z.ZodString>;
|
|
28
|
+
service_tier: z.ZodOptional<z.ZodNullable<z.ZodString>>;
|
|
29
|
+
reasoning: z.ZodOptional<z.ZodNullable<z.ZodObject<{
|
|
30
|
+
context: z.ZodOptional<z.ZodString>;
|
|
31
|
+
}, z.core.$strip>>>;
|
|
32
|
+
}, z.core.$strip>;
|
|
33
|
+
type NativeResponse = Omit<z.infer<typeof responseSchema>, 'output'> & {
|
|
34
|
+
output: readonly ProtocolJsonObject[];
|
|
35
|
+
outputItems: readonly ResponsesOutputItem[];
|
|
36
|
+
normalization?: {
|
|
37
|
+
version: 2;
|
|
38
|
+
removedPaths: string[];
|
|
39
|
+
reason: string;
|
|
40
|
+
};
|
|
41
|
+
};
|
|
42
|
+
export interface WireCapture {
|
|
43
|
+
source: 'wire_json' | 'wire_sse';
|
|
44
|
+
response?: NativeResponse;
|
|
45
|
+
error?: GatewayError;
|
|
46
|
+
usage?: Usage;
|
|
47
|
+
sent: number;
|
|
48
|
+
}
|
|
49
|
+
interface WireLifecycle {
|
|
50
|
+
readonly promptCacheOnly: boolean;
|
|
51
|
+
details(): ModelFailureDetails;
|
|
52
|
+
onActivity(phase?: ModelFailureDetails['phase']): void;
|
|
53
|
+
}
|
|
54
|
+
export type WireProgress = Extract<ModelStreamEvent, {
|
|
55
|
+
type: 'text_delta' | 'reasoning_summary_delta' | 'activity';
|
|
56
|
+
}>;
|
|
57
|
+
export declare const maxCaptureBytes: number;
|
|
58
|
+
export declare function createWireBridge(request: ModelRequest, profile: ProviderProfile, capabilities: ModelCapabilities, signal: AbortSignal, capture: WireCapture, secrets: readonly string[], lifecycle: WireLifecycle, transport: typeof fetch, onProgress?: (event: WireProgress) => void): typeof fetch;
|
|
59
|
+
export declare function observeBody(body: ReadableStream<Uint8Array>, failTransport: (error: unknown) => unknown): ReadableStream<Uint8Array<ArrayBufferLike>>;
|
|
60
|
+
export declare function readBounded(body: ReadableStream<Uint8Array> | null): Promise<string>;
|
|
61
|
+
export {};
|