@saturn-universe/model 0.1.0 → 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/dist/codec.d.ts CHANGED
@@ -1,5 +1,15 @@
1
+ import { z } from 'zod';
1
2
  import type { ModelRequest, ModelToolCall, ResponsesCodec, ResponsesOutputItem } from '@saturn-universe/core';
2
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;
3
13
  export declare const responsesCodec: ResponsesCodec;
4
14
  export declare const responsesCodecs: {
5
15
  get(id: string, version: number): ResponsesCodec;
package/dist/codec.js CHANGED
@@ -3,6 +3,14 @@ import { canonicalJson } from '@saturn-universe/core';
3
3
  import { z } from 'zod';
4
4
  import { GatewayError } from './errors.js';
5
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
+ }
6
14
  const item = z.discriminatedUnion('type', [
7
15
  z
8
16
  .object({
@@ -16,14 +24,7 @@ const item = z.discriminatedUnion('type', [
16
24
  ])),
17
25
  })
18
26
  .passthrough(),
19
- z
20
- .object({
21
- type: z.literal('reasoning'),
22
- id: z.string().min(1),
23
- summary: z.array(z.object({ type: z.literal('summary_text'), text: z.string() }).passthrough()),
24
- encrypted_content: z.string().min(1),
25
- })
26
- .passthrough(),
27
+ reasoningItemSchema.extend({ encrypted_content: z.string().min(1) }).passthrough(),
27
28
  z
28
29
  .object({
29
30
  type: z.literal('function_call'),
@@ -43,8 +44,7 @@ export const responsesCodec = {
43
44
  throw new GatewayError('unsupported_item');
44
45
  if (value.type === 'reasoning' && ('content' in value || 'reasoning_text' in value))
45
46
  throw new GatewayError('unsupported_capability');
46
- if (value.type === 'reasoning' &&
47
- (typeof value.encrypted_content !== 'string' || !value.encrypted_content.trim()))
47
+ if (value.type === 'reasoning' && !hasEncryptedReasoning(value))
48
48
  throw new GatewayError('continuation_unavailable');
49
49
  if (!item.safeParse(value).success)
50
50
  throw new GatewayError('invalid_output');
@@ -103,8 +103,7 @@ export const responsesCodecV2 = {
103
103
  ...responsesCodec,
104
104
  version: 2,
105
105
  decodeOutput(items, binding) {
106
- if (items.some((value) => value.type === 'reasoning' &&
107
- (typeof value.encrypted_content !== 'string' || !value.encrypted_content.trim())))
106
+ if (items.some((value) => value.type === 'reasoning' && !hasEncryptedReasoning(value)))
108
107
  throw new GatewayError('continuation_unavailable');
109
108
  return responsesCodec.decodeOutput(items.map((value) => normalizeReasoning(value).value), binding);
110
109
  },
package/dist/index.js CHANGED
@@ -492,6 +492,7 @@ function accept(request, capture, capabilities, sdk) {
492
492
  outputDigest: digest(outputItems.map((item) => item.value)),
493
493
  providerMetadata: {
494
494
  responseId: raw.id,
495
+ ...(raw.reasoningSummary === undefined ? {} : { reasoningSummary: raw.reasoningSummary }),
495
496
  ...(raw.normalization === undefined ? {} : { normalization: raw.normalization }),
496
497
  ...(raw.reasoning?.context === undefined ? {} : { reasoningContext: raw.reasoning.context }),
497
498
  ...(raw.model === undefined ? {} : { model: raw.model }),
package/dist/wire.d.ts CHANGED
@@ -33,6 +33,7 @@ declare const responseSchema: z.ZodObject<{
33
33
  type NativeResponse = Omit<z.infer<typeof responseSchema>, 'output'> & {
34
34
  output: readonly ProtocolJsonObject[];
35
35
  outputItems: readonly ResponsesOutputItem[];
36
+ reasoningSummary?: string;
36
37
  normalization?: {
37
38
  version: 2;
38
39
  removedPaths: string[];
package/dist/wire.js CHANGED
@@ -1,8 +1,8 @@
1
- import { canonicalJson } from '@saturn-universe/core';
1
+ import { canonicalJson, maxModelReasoningSummaryLength } from '@saturn-universe/core';
2
2
  import { z } from 'zod';
3
3
  import { GatewayError } from './errors.js';
4
4
  import { providerFailure, transportFailure } from './provider-errors.js';
5
- import { responsesCodecs, normalizeReasoning } from './codec.js';
5
+ import { hasEncryptedReasoning, normalizeReasoning, reasoningItemSchema, responsesCodecs } from './codec.js';
6
6
  const tokens = z.number().int().nonnegative().safe();
7
7
  const usageSchema = z.object({
8
8
  input_tokens: tokens,
@@ -152,7 +152,7 @@ export function createWireBridge(request, profile, capabilities, signal, capture
152
152
  if (!bodyStream)
153
153
  throw new GatewayError('native_capture_incomplete');
154
154
  capture.source = 'wire_sse';
155
- return new Response(bodyStream.pipeThrough(captureSse(capture, request, secrets, lifecycle, onProgress)), {
155
+ return new Response(bodyStream.pipeThrough(captureSse(capture, request, capabilities, secrets, lifecycle, onProgress)), {
156
156
  headers: response.headers,
157
157
  status: response.status,
158
158
  });
@@ -162,7 +162,7 @@ export function createWireBridge(request, profile, capabilities, signal, capture
162
162
  let value;
163
163
  try {
164
164
  value = parseWireJson(text, secrets, (json) => captureUsage(capture, json));
165
- capture.response = decodeResponse(value, request.binding);
165
+ capture.response = decodeResponse(value, request.binding, capabilities.responses.encryptedReasoning);
166
166
  }
167
167
  catch (error) {
168
168
  if (error instanceof GatewayError)
@@ -285,7 +285,7 @@ function parseWireJson(text, secrets, observe, allowErrorFrames = false) {
285
285
  throw new GatewayError('invalid_output');
286
286
  return value;
287
287
  }
288
- function decodeResponse(value, binding) {
288
+ function decodeResponse(value, binding, encryptedReasoning) {
289
289
  const decoded = responseSchema.safeParse(value);
290
290
  if (!decoded.success)
291
291
  throw new GatewayError('invalid_output');
@@ -295,25 +295,42 @@ function decodeResponse(value, binding) {
295
295
  if ((raw.usage.input_tokens_details?.cached_tokens ?? 0) > raw.usage.input_tokens ||
296
296
  (raw.usage.output_tokens_details?.reasoning_tokens ?? 0) > raw.usage.output_tokens)
297
297
  throw new GatewayError('invalid_output');
298
+ const replayable = encryptedReasoning === 'required'
299
+ ? raw.output
300
+ : raw.output.filter((value) => value.type !== 'reasoning' || hasEncryptedReasoning(value));
301
+ const omittedReasoning = replayable.length !== raw.output.length;
298
302
  const removedPaths = [];
299
303
  const outputItems = responsesCodecs
300
304
  .get(binding.codecId, binding.codecVersion)
301
- .decodeOutput(raw.output, binding);
302
- if (binding.codecVersion === 2)
303
- raw.output.forEach((value, index) => {
304
- if (value.type === 'reasoning')
305
- removedPaths.push(...['content', 'reasoning_text']
306
- .filter((key) => key in value)
307
- .map((key) => `output[${index}].${key}`));
308
- });
309
- const output = outputItems.map((item) => item.value);
310
- if (raw.status === 'completed' &&
311
- outputItems.some((item) => 'status' in item.value && item.value.status !== 'completed'))
305
+ .decodeOutput(replayable, binding);
306
+ // SSE completeness and the SDK both use the full provider output.
307
+ const output = binding.codecVersion === 2
308
+ ? raw.output.map((value, index) => {
309
+ const normalized = normalizeReasoning(value);
310
+ removedPaths.push(...normalized.removed.map((key) => `output[${index}].${key}`));
311
+ return normalized.value;
312
+ })
313
+ : raw.output;
314
+ if (raw.status === 'completed' && output.some((item) => 'status' in item && item.status !== 'completed'))
312
315
  throw new GatewayError('native_capture_incomplete');
316
+ const reasoningSummary = omittedReasoning
317
+ ? output
318
+ .flatMap((value) => {
319
+ if (value.type !== 'reasoning')
320
+ return [];
321
+ const item = reasoningItemSchema.safeParse(value);
322
+ if (!item.success)
323
+ throw new GatewayError('invalid_output');
324
+ return item.data.summary.map((part) => part.text);
325
+ })
326
+ .join('\n')
327
+ .slice(0, maxModelReasoningSummaryLength)
328
+ : undefined;
313
329
  return {
314
330
  ...raw,
315
331
  output,
316
332
  outputItems,
333
+ ...(reasoningSummary?.trim() ? { reasoningSummary } : {}),
317
334
  ...(binding.codecVersion === 2
318
335
  ? { normalization: { version: 2, removedPaths, reason: 'private_reasoning_excluded' } }
319
336
  : {}),
@@ -327,7 +344,7 @@ const eventSchema = z.object({
327
344
  item: z.record(z.string(), z.json()).optional(),
328
345
  response: z.unknown().optional(),
329
346
  });
330
- function captureSse(capture, request, secrets, lifecycle, onProgress) {
347
+ function captureSse(capture, request, capabilities, secrets, lifecycle, onProgress) {
331
348
  const decoder = new TextDecoder('utf-8', { fatal: true });
332
349
  let buffer = '';
333
350
  let bytes = 0;
@@ -429,7 +446,7 @@ function captureSse(capture, request, secrets, lifecycle, onProgress) {
429
446
  hasTools: request.tools.length > 0,
430
447
  });
431
448
  if (event.type === 'response.completed' || event.type === 'response.incomplete') {
432
- const response = decodeResponse(event.response, binding);
449
+ const response = decodeResponse(event.response, binding, capabilities.responses.encryptedReasoning);
433
450
  const output = [...done.entries()].sort(([a], [b]) => a - b);
434
451
  if (response.status !== (event.type === 'response.completed' ? 'completed' : 'incomplete') ||
435
452
  added.size !== done.size ||
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@saturn-universe/model",
3
- "version": "0.1.0",
3
+ "version": "0.1.1",
4
4
  "license": "AGPL-3.0-only",
5
5
  "type": "module",
6
6
  "files": [
@@ -17,7 +17,7 @@
17
17
  "@ai-sdk/google": "4.0.114",
18
18
  "@ai-sdk/openai": "4.0.111",
19
19
  "@ai-sdk/openai-compatible": "3.0.87",
20
- "@saturn-universe/core": "0.1.0",
20
+ "@saturn-universe/core": "0.1.1",
21
21
  "ai": "7.0.165",
22
22
  "zod": "4.6.0"
23
23
  }