@saturn-universe/model 0.0.0-stage → 0.1.1
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 +22 -0
- package/dist/codec.js +128 -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 +541 -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 +62 -0
- package/dist/wire.js +498 -0
- package/package.json +21 -3
- package/README.md +0 -3
package/dist/codec.d.ts
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
import type { ModelRequest, ModelToolCall, ResponsesCodec, ResponsesOutputItem } from '@saturn-universe/core';
|
|
3
|
+
export declare const digest: (value: unknown) => string;
|
|
4
|
+
export declare const reasoningItemSchema: z.ZodObject<{
|
|
5
|
+
type: z.ZodLiteral<"reasoning">;
|
|
6
|
+
id: z.ZodString;
|
|
7
|
+
summary: z.ZodArray<z.ZodObject<{
|
|
8
|
+
type: z.ZodLiteral<"summary_text">;
|
|
9
|
+
text: z.ZodString;
|
|
10
|
+
}, z.core.$loose>>;
|
|
11
|
+
}, z.core.$strip>;
|
|
12
|
+
export declare function hasEncryptedReasoning(value: import('@saturn-universe/core').ProtocolJsonObject): boolean;
|
|
13
|
+
export declare const responsesCodec: ResponsesCodec;
|
|
14
|
+
export declare const responsesCodecs: {
|
|
15
|
+
get(id: string, version: number): ResponsesCodec;
|
|
16
|
+
};
|
|
17
|
+
export declare function normalizeReasoning<T extends import('@saturn-universe/core').ProtocolJsonObject>(value: T): {
|
|
18
|
+
value: T;
|
|
19
|
+
removed: string[];
|
|
20
|
+
};
|
|
21
|
+
export declare const responsesCodecV2: ResponsesCodec;
|
|
22
|
+
export declare function decodeToolCalls(request: ModelRequest, outputItems: readonly ResponsesOutputItem[]): ModelToolCall[];
|
package/dist/codec.js
ADDED
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
import { createHash } from 'node:crypto';
|
|
2
|
+
import { canonicalJson } from '@saturn-universe/core';
|
|
3
|
+
import { z } from 'zod';
|
|
4
|
+
import { GatewayError } from './errors.js';
|
|
5
|
+
export const digest = (value) => createHash('sha256').update(canonicalJson(value)).digest('hex');
|
|
6
|
+
export const reasoningItemSchema = z.object({
|
|
7
|
+
type: z.literal('reasoning'),
|
|
8
|
+
id: z.string().min(1),
|
|
9
|
+
summary: z.array(z.object({ type: z.literal('summary_text'), text: z.string() }).passthrough()),
|
|
10
|
+
});
|
|
11
|
+
export function hasEncryptedReasoning(value) {
|
|
12
|
+
return typeof value.encrypted_content === 'string' && Boolean(value.encrypted_content.trim());
|
|
13
|
+
}
|
|
14
|
+
const item = z.discriminatedUnion('type', [
|
|
15
|
+
z
|
|
16
|
+
.object({
|
|
17
|
+
type: z.literal('message'),
|
|
18
|
+
id: z.string().min(1),
|
|
19
|
+
role: z.literal('assistant'),
|
|
20
|
+
phase: z.enum(['commentary', 'final_answer']).nullish(),
|
|
21
|
+
content: z.array(z.discriminatedUnion('type', [
|
|
22
|
+
z.object({ type: z.literal('output_text'), text: z.string() }).passthrough(),
|
|
23
|
+
z.object({ type: z.literal('refusal'), refusal: z.string() }).passthrough(),
|
|
24
|
+
])),
|
|
25
|
+
})
|
|
26
|
+
.passthrough(),
|
|
27
|
+
reasoningItemSchema.extend({ encrypted_content: z.string().min(1) }).passthrough(),
|
|
28
|
+
z
|
|
29
|
+
.object({
|
|
30
|
+
type: z.literal('function_call'),
|
|
31
|
+
id: z.string().min(1),
|
|
32
|
+
call_id: z.string().min(1),
|
|
33
|
+
name: z.string().min(1),
|
|
34
|
+
arguments: z.string(),
|
|
35
|
+
})
|
|
36
|
+
.passthrough(),
|
|
37
|
+
]);
|
|
38
|
+
export const responsesCodec = {
|
|
39
|
+
id: 'saturn-responses',
|
|
40
|
+
version: 1,
|
|
41
|
+
decodeOutput(items) {
|
|
42
|
+
return items.map((value) => {
|
|
43
|
+
if (!['message', 'reasoning', 'function_call'].includes(String(value.type)))
|
|
44
|
+
throw new GatewayError('unsupported_item');
|
|
45
|
+
if (value.type === 'reasoning' && ('content' in value || 'reasoning_text' in value))
|
|
46
|
+
throw new GatewayError('unsupported_capability');
|
|
47
|
+
if (value.type === 'reasoning' && !hasEncryptedReasoning(value))
|
|
48
|
+
throw new GatewayError('continuation_unavailable');
|
|
49
|
+
if (!item.safeParse(value).success)
|
|
50
|
+
throw new GatewayError('invalid_output');
|
|
51
|
+
return { kind: value.type, value: structuredClone(value) };
|
|
52
|
+
});
|
|
53
|
+
},
|
|
54
|
+
encodePart(part, bytes, capabilities) {
|
|
55
|
+
if (part.type === 'image') {
|
|
56
|
+
if (!capabilities.images)
|
|
57
|
+
throw new GatewayError('unsupported_capability');
|
|
58
|
+
return {
|
|
59
|
+
type: 'input_image',
|
|
60
|
+
image_url: `data:${part.artifact.mediaType};base64,${Buffer.from(bytes).toString('base64')}`,
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
if (part.type !== 'file' || !capabilities.files)
|
|
64
|
+
throw new GatewayError('unsupported_capability');
|
|
65
|
+
return {
|
|
66
|
+
type: 'input_file',
|
|
67
|
+
file_data: `data:${part.artifact.mediaType};base64,${Buffer.from(bytes).toString('base64')}`,
|
|
68
|
+
filename: part.name,
|
|
69
|
+
};
|
|
70
|
+
},
|
|
71
|
+
replayItem(output) {
|
|
72
|
+
const input = { ...output.value };
|
|
73
|
+
const removedOutputOnlyPaths = [];
|
|
74
|
+
for (const key of ['status', 'created_by', 'parsed'])
|
|
75
|
+
if (key in input) {
|
|
76
|
+
delete input[key];
|
|
77
|
+
removedOutputOnlyPaths.push(key);
|
|
78
|
+
}
|
|
79
|
+
return { input: input, removedOutputOnlyPaths };
|
|
80
|
+
},
|
|
81
|
+
};
|
|
82
|
+
export const responsesCodecs = {
|
|
83
|
+
get(id, version) {
|
|
84
|
+
if (id !== responsesCodec.id || ![1, 2].includes(version))
|
|
85
|
+
throw new GatewayError('unsupported_capability');
|
|
86
|
+
return version === 1 ? responsesCodec : responsesCodecV2;
|
|
87
|
+
},
|
|
88
|
+
};
|
|
89
|
+
const privateContent = z.array(z.object({ type: z.literal('reasoning_text'), text: z.string() }).strict());
|
|
90
|
+
export function normalizeReasoning(value) {
|
|
91
|
+
if (value.type !== 'reasoning')
|
|
92
|
+
return { value, removed: [] };
|
|
93
|
+
if (('content' in value && value.content !== null && !privateContent.safeParse(value.content).success) ||
|
|
94
|
+
('reasoning_text' in value && value.reasoning_text !== null && typeof value.reasoning_text !== 'string'))
|
|
95
|
+
throw new GatewayError('invalid_output');
|
|
96
|
+
const normalized = { ...value };
|
|
97
|
+
const removed = ['content', 'reasoning_text'].filter((key) => key in normalized);
|
|
98
|
+
for (const key of removed)
|
|
99
|
+
delete normalized[key];
|
|
100
|
+
return { value: normalized, removed };
|
|
101
|
+
}
|
|
102
|
+
export const responsesCodecV2 = {
|
|
103
|
+
...responsesCodec,
|
|
104
|
+
version: 2,
|
|
105
|
+
decodeOutput(items, binding) {
|
|
106
|
+
if (items.some((value) => value.type === 'reasoning' && !hasEncryptedReasoning(value)))
|
|
107
|
+
throw new GatewayError('continuation_unavailable');
|
|
108
|
+
return responsesCodec.decodeOutput(items.map((value) => normalizeReasoning(value).value), binding);
|
|
109
|
+
},
|
|
110
|
+
};
|
|
111
|
+
export function decodeToolCalls(request, outputItems) {
|
|
112
|
+
const toolCalls = outputItems.flatMap((item, outputIndex) => {
|
|
113
|
+
if (item.kind !== 'function_call')
|
|
114
|
+
return [];
|
|
115
|
+
const name = request.tools.find((tool) => tool.wireName === item.value.name)?.canonicalName ?? item.value.name;
|
|
116
|
+
let args;
|
|
117
|
+
try {
|
|
118
|
+
args = { kind: 'json', value: JSON.parse(item.value.arguments) };
|
|
119
|
+
}
|
|
120
|
+
catch {
|
|
121
|
+
args = { kind: 'invalid', raw: item.value.arguments, reason: 'Invalid JSON arguments' };
|
|
122
|
+
}
|
|
123
|
+
return [{ outputIndex, providerCallId: item.value.call_id, toolName: name, arguments: args }];
|
|
124
|
+
});
|
|
125
|
+
if (new Set(toolCalls.map((call) => call.providerCallId)).size !== toolCalls.length)
|
|
126
|
+
throw new GatewayError('invalid_output');
|
|
127
|
+
return toolCalls;
|
|
128
|
+
}
|
package/dist/errors.d.ts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { ModelError, ModelErrorCode } from '@saturn-universe/core';
|
|
2
|
+
export declare class GatewayError extends Error {
|
|
3
|
+
readonly error: ModelError;
|
|
4
|
+
constructor(code: ModelErrorCode, retryable?: boolean, outcomeUnknown?: boolean, retryAfterMs?: number, message?: string, extra?: Pick<ModelError, 'details' | 'usage'> & ErrorOptions);
|
|
5
|
+
withObservation(details: ModelError['details'], usage: ModelError['usage']): GatewayError;
|
|
6
|
+
}
|
|
7
|
+
export declare function classify(error: unknown, signal: AbortSignal): GatewayError;
|
package/dist/errors.js
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
export class GatewayError extends Error {
|
|
2
|
+
error;
|
|
3
|
+
constructor(code, retryable = false, outcomeUnknown = false, retryAfterMs, message = `Model request failed: ${code}`, extra) {
|
|
4
|
+
super(message, extra?.cause === undefined ? undefined : { cause: extra.cause });
|
|
5
|
+
this.name = 'ModelGatewayError';
|
|
6
|
+
this.error = {
|
|
7
|
+
code,
|
|
8
|
+
message: this.message,
|
|
9
|
+
retryable,
|
|
10
|
+
outcomeUnknown,
|
|
11
|
+
...(retryAfterMs === undefined ? {} : { retryAfterMs }),
|
|
12
|
+
...(extra?.details ? { details: extra.details } : {}),
|
|
13
|
+
...(extra?.usage ? { usage: extra.usage } : {}),
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
withObservation(details, usage) {
|
|
17
|
+
const error = this.error;
|
|
18
|
+
const observedDetails = error.details ?? details;
|
|
19
|
+
const observedUsage = error.usage ?? usage;
|
|
20
|
+
return new GatewayError(error.code, error.retryable, error.outcomeUnknown, error.retryAfterMs, error.message, {
|
|
21
|
+
...(observedDetails ? { details: observedDetails } : {}),
|
|
22
|
+
...(observedUsage ? { usage: observedUsage } : {}),
|
|
23
|
+
cause: this.cause,
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
export function classify(error, signal) {
|
|
28
|
+
if (signal.aborted) {
|
|
29
|
+
if (signal.reason instanceof GatewayError)
|
|
30
|
+
return signal.reason;
|
|
31
|
+
return new GatewayError(signal.reason?.name === 'TimeoutError' ? 'timeout' : 'cancelled', signal.reason?.name === 'TimeoutError', true);
|
|
32
|
+
}
|
|
33
|
+
if (error instanceof GatewayError)
|
|
34
|
+
return error;
|
|
35
|
+
return new GatewayError('protocol');
|
|
36
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { ModelCapabilities, ProviderBinding, ProviderProfile, ModelRef, ModelRequest, ModelResponse, ModelInvocationOptions, ModelGateway } from '@saturn-universe/core';
|
|
2
|
+
export { GatewayError } from './errors.js';
|
|
3
|
+
export { digest, responsesCodec, responsesCodecV2, responsesCodecs } from './codec.js';
|
|
4
|
+
export interface CredentialVault {
|
|
5
|
+
resolve(ref: string): Promise<string>;
|
|
6
|
+
}
|
|
7
|
+
export declare class ProviderRegistry {
|
|
8
|
+
private readonly profiles;
|
|
9
|
+
register(profile: ProviderProfile): () => void;
|
|
10
|
+
deactivate(profileRef: string): void;
|
|
11
|
+
activate(profileRef: string): void;
|
|
12
|
+
isActive(profileRef: string): boolean;
|
|
13
|
+
listActive(): readonly ProviderProfile[];
|
|
14
|
+
/** Remove the profile after callers that need the old revision have drained. */
|
|
15
|
+
release(profileRef: string): boolean;
|
|
16
|
+
resolve(model: ModelRef): {
|
|
17
|
+
profile: ProviderProfile;
|
|
18
|
+
binding: ProviderBinding;
|
|
19
|
+
capabilities: ModelCapabilities;
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
export declare function createModelGateway(registry: ProviderRegistry, credentials: CredentialVault, transport?: typeof fetch): ModelGateway & {
|
|
23
|
+
execute(request: ModelRequest, options: ModelInvocationOptions): Promise<ModelResponse>;
|
|
24
|
+
};
|